]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch13.adb
[Ada] Disallow Predicate_Failure without predicate
[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 Resolve_Iterable_Operation
246 (N : Node_Id;
247 Cursor : Entity_Id;
248 Typ : Entity_Id;
249 Nam : Name_Id);
250 -- If the name of a primitive operation for an Iterable aspect is
251 -- overloaded, resolve according to required signature.
252
253 procedure Set_Biased
254 (E : Entity_Id;
255 N : Node_Id;
256 Msg : String;
257 Biased : Boolean := True);
258 -- If Biased is True, sets Has_Biased_Representation flag for E, and
259 -- outputs a warning message at node N if Warn_On_Biased_Representation is
260 -- is True. This warning inserts the string Msg to describe the construct
261 -- causing biasing.
262
263 -----------------------------------------------------------
264 -- Visibility of Discriminants in Aspect Specifications --
265 -----------------------------------------------------------
266
267 -- The discriminants of a type are visible when analyzing the aspect
268 -- specifications of a type declaration or protected type declaration,
269 -- but not when analyzing those of a subtype declaration. The following
270 -- routines enforce this distinction.
271
272 procedure Push_Type (E : Entity_Id);
273 -- Push scope E and make visible the discriminants of type entity E if E
274 -- has discriminants and is not a subtype.
275
276 procedure Pop_Type (E : Entity_Id);
277 -- Remove visibility to the discriminants of type entity E and pop the
278 -- scope stack if E has discriminants and is not a subtype.
279
280 ----------------------------------------------
281 -- Table for Validate_Unchecked_Conversions --
282 ----------------------------------------------
283
284 -- The following table collects unchecked conversions for validation.
285 -- Entries are made by Validate_Unchecked_Conversion and then the call
286 -- to Validate_Unchecked_Conversions does the actual error checking and
287 -- posting of warnings. The reason for this delayed processing is to take
288 -- advantage of back-annotations of size and alignment values performed by
289 -- the back end.
290
291 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
292 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
293 -- already have modified all Sloc values if the -gnatD option is set.
294
295 type UC_Entry is record
296 Eloc : Source_Ptr; -- node used for posting warnings
297 Source : Entity_Id; -- source type for unchecked conversion
298 Target : Entity_Id; -- target type for unchecked conversion
299 Act_Unit : Entity_Id; -- actual function instantiated
300 end record;
301
302 package Unchecked_Conversions is new Table.Table (
303 Table_Component_Type => UC_Entry,
304 Table_Index_Type => Int,
305 Table_Low_Bound => 1,
306 Table_Initial => 50,
307 Table_Increment => 200,
308 Table_Name => "Unchecked_Conversions");
309
310 ----------------------------------------
311 -- Table for Validate_Address_Clauses --
312 ----------------------------------------
313
314 -- If an address clause has the form
315
316 -- for X'Address use Expr
317
318 -- where Expr has a value known at compile time or is of the form Y'Address
319 -- or recursively is a reference to a constant initialized with either of
320 -- these forms, and the value of Expr is not a multiple of X's alignment,
321 -- or if Y has a smaller alignment than X, then that merits a warning about
322 -- possible bad alignment. The following table collects address clauses of
323 -- this kind. We put these in a table so that they can be checked after the
324 -- back end has completed annotation of the alignments of objects, since we
325 -- can catch more cases that way.
326
327 type Address_Clause_Check_Record is record
328 N : Node_Id;
329 -- The address clause
330
331 X : Entity_Id;
332 -- The entity of the object subject to the address clause
333
334 A : Uint;
335 -- The value of the address in the first case
336
337 Y : Entity_Id;
338 -- The entity of the object being overlaid in the second case
339
340 Off : Boolean;
341 -- Whether the address is offset within Y in the second case
342
343 Alignment_Checks_Suppressed : Boolean;
344 -- Whether alignment checks are suppressed by an active scope suppress
345 -- setting. We need to save the value in order to be able to reuse it
346 -- after the back end has been run.
347 end record;
348
349 package Address_Clause_Checks is new Table.Table (
350 Table_Component_Type => Address_Clause_Check_Record,
351 Table_Index_Type => Int,
352 Table_Low_Bound => 1,
353 Table_Initial => 20,
354 Table_Increment => 200,
355 Table_Name => "Address_Clause_Checks");
356
357 function Alignment_Checks_Suppressed
358 (ACCR : Address_Clause_Check_Record) return Boolean;
359 -- Return whether the alignment check generated for the address clause
360 -- is suppressed.
361
362 ---------------------------------
363 -- Alignment_Checks_Suppressed --
364 ---------------------------------
365
366 function Alignment_Checks_Suppressed
367 (ACCR : Address_Clause_Check_Record) return Boolean
368 is
369 begin
370 if Checks_May_Be_Suppressed (ACCR.X) then
371 return Is_Check_Suppressed (ACCR.X, Alignment_Check);
372 else
373 return ACCR.Alignment_Checks_Suppressed;
374 end if;
375 end Alignment_Checks_Suppressed;
376
377 -----------------------------------------
378 -- Adjust_Record_For_Reverse_Bit_Order --
379 -----------------------------------------
380
381 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
382 Max_Machine_Scalar_Size : constant Uint :=
383 UI_From_Int
384 (Standard_Long_Long_Integer_Size);
385 -- We use this as the maximum machine scalar size
386
387 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
388
389 CC : Node_Id;
390 Comp : Node_Id;
391 Num_CC : Natural;
392
393 begin
394 -- The processing done here used to depend on the Ada version, but the
395 -- behavior has been changed by AI95-0133. However this AI is a Binding
396 -- Interpretation, so we now implement it even in Ada 95 mode. But the
397 -- original behavior from unamended Ada 95 is available for the sake of
398 -- compatibility under the debugging switch -gnatd.p in Ada 95 mode.
399
400 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
401 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
402 return;
403 end if;
404
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
409
410 -- Note that component clauses found on record types may be inherited,
411 -- in which case the layout of the component with such a clause still
412 -- has to be done at this point. Therefore, the processing done here
413 -- must exclusively rely on the Component_Clause of the component.
414
415 -- This first loop through components does two things. First it deals
416 -- with the case of components with component clauses whose length is
417 -- greater than the maximum machine scalar size (either accepting them
418 -- or rejecting as needed). Second, it counts the number of components
419 -- with component clauses whose length does not exceed this maximum for
420 -- later processing.
421
422 Num_CC := 0;
423 Comp := First_Component_Or_Discriminant (R);
424 while Present (Comp) loop
425 CC := Component_Clause (Comp);
426
427 if Present (CC) then
428 declare
429 Fbit : constant Uint := Static_Integer (First_Bit (CC));
430 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
431
432 begin
433 -- Case of component with last bit >= max machine scalar
434
435 if Lbit >= Max_Machine_Scalar_Size then
436
437 -- This is allowed only if first bit is zero, and last bit
438 -- + 1 is a multiple of storage unit size.
439
440 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
441
442 -- This is the case to give a warning if enabled
443
444 if Warn_On_Reverse_Bit_Order then
445 Error_Msg_N
446 ("info: multi-byte field specified with "
447 & "non-standard Bit_Order?V?", CC);
448
449 if Bytes_Big_Endian then
450 Error_Msg_N
451 ("\bytes are not reversed "
452 & "(component is big-endian)?V?", CC);
453 else
454 Error_Msg_N
455 ("\bytes are not reversed "
456 & "(component is little-endian)?V?", CC);
457 end if;
458 end if;
459
460 -- Give error message for RM 13.5.1(10) violation
461
462 else
463 Error_Msg_FE
464 ("machine scalar rules not followed for&",
465 First_Bit (CC), Comp);
466
467 Error_Msg_Uint_1 := Lbit + 1;
468 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
469 Error_Msg_F
470 ("\last bit + 1 (^) exceeds maximum machine scalar "
471 & "size (^)", First_Bit (CC));
472
473 if (Lbit + 1) mod SSU /= 0 then
474 Error_Msg_Uint_1 := SSU;
475 Error_Msg_F
476 ("\and is not a multiple of Storage_Unit (^) "
477 & "(RM 13.5.1(10))", First_Bit (CC));
478
479 else
480 Error_Msg_Uint_1 := Fbit;
481 Error_Msg_F
482 ("\and first bit (^) is non-zero "
483 & "(RM 13.4.1(10))", First_Bit (CC));
484 end if;
485 end if;
486
487 -- OK case of machine scalar related component clause. For now,
488 -- just count them.
489
490 else
491 Num_CC := Num_CC + 1;
492 end if;
493 end;
494 end if;
495
496 Next_Component_Or_Discriminant (Comp);
497 end loop;
498
499 -- We need to sort the component clauses on the basis of the Position
500 -- values in the clause, so we can group clauses with the same Position
501 -- together to determine the relevant machine scalar size.
502
503 Sort_CC : declare
504 Comps : array (0 .. Num_CC) of Entity_Id;
505 -- Array to collect component and discriminant entities. The data
506 -- starts at index 1, the 0'th entry is for the sort routine.
507
508 function CP_Lt (Op1, Op2 : Natural) return Boolean;
509 -- Compare routine for Sort
510
511 procedure CP_Move (From : Natural; To : Natural);
512 -- Move routine for Sort
513
514 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
515
516 MaxL : Uint;
517 -- Maximum last bit value of any component in this set
518
519 MSS : Uint;
520 -- Corresponding machine scalar size
521
522 Start : Natural;
523 Stop : Natural;
524 -- Start and stop positions in the component list of the set of
525 -- components with the same starting position (that constitute
526 -- components in a single machine scalar).
527
528 -----------
529 -- CP_Lt --
530 -----------
531
532 function CP_Lt (Op1, Op2 : Natural) return Boolean is
533 begin
534 return
535 Position (Component_Clause (Comps (Op1))) <
536 Position (Component_Clause (Comps (Op2)));
537 end CP_Lt;
538
539 -------------
540 -- CP_Move --
541 -------------
542
543 procedure CP_Move (From : Natural; To : Natural) is
544 begin
545 Comps (To) := Comps (From);
546 end CP_Move;
547
548 -- Start of processing for Sort_CC
549
550 begin
551 -- Collect the machine scalar relevant component clauses
552
553 Num_CC := 0;
554 Comp := First_Component_Or_Discriminant (R);
555 while Present (Comp) loop
556 declare
557 CC : constant Node_Id := Component_Clause (Comp);
558
559 begin
560 -- Collect only component clauses whose last bit is less than
561 -- machine scalar size. Any component clause whose last bit
562 -- exceeds this value does not take part in machine scalar
563 -- layout considerations. The test for Error_Posted makes sure
564 -- we exclude component clauses for which we already posted an
565 -- error.
566
567 if Present (CC)
568 and then not Error_Posted (Last_Bit (CC))
569 and then Static_Integer (Last_Bit (CC)) <
570 Max_Machine_Scalar_Size
571 then
572 Num_CC := Num_CC + 1;
573 Comps (Num_CC) := Comp;
574 end if;
575 end;
576
577 Next_Component_Or_Discriminant (Comp);
578 end loop;
579
580 -- Sort by ascending position number
581
582 Sorting.Sort (Num_CC);
583
584 -- We now have all the components whose size does not exceed the max
585 -- machine scalar value, sorted by starting position. In this loop we
586 -- gather groups of clauses starting at the same position, to process
587 -- them in accordance with AI-133.
588
589 Stop := 0;
590 while Stop < Num_CC loop
591 Start := Stop + 1;
592 Stop := Start;
593 MaxL :=
594 Static_Integer
595 (Last_Bit (Component_Clause (Comps (Start))));
596 while Stop < Num_CC loop
597 if Static_Integer
598 (Position (Component_Clause (Comps (Stop + 1)))) =
599 Static_Integer
600 (Position (Component_Clause (Comps (Stop))))
601 then
602 Stop := Stop + 1;
603 MaxL :=
604 UI_Max
605 (MaxL,
606 Static_Integer
607 (Last_Bit
608 (Component_Clause (Comps (Stop)))));
609 else
610 exit;
611 end if;
612 end loop;
613
614 -- Now we have a group of component clauses from Start to Stop
615 -- whose positions are identical, and MaxL is the maximum last
616 -- bit value of any of these components.
617
618 -- We need to determine the corresponding machine scalar size.
619 -- This loop assumes that machine scalar sizes are even, and that
620 -- each possible machine scalar has twice as many bits as the next
621 -- smaller one.
622
623 MSS := Max_Machine_Scalar_Size;
624 while MSS mod 2 = 0
625 and then (MSS / 2) >= SSU
626 and then (MSS / 2) > MaxL
627 loop
628 MSS := MSS / 2;
629 end loop;
630
631 -- Here is where we fix up the Component_Bit_Offset value to
632 -- account for the reverse bit order. Some examples of what needs
633 -- to be done for the case of a machine scalar size of 8 are:
634
635 -- First_Bit .. Last_Bit Component_Bit_Offset
636 -- old new old new
637
638 -- 0 .. 0 7 .. 7 0 7
639 -- 0 .. 1 6 .. 7 0 6
640 -- 0 .. 2 5 .. 7 0 5
641 -- 0 .. 7 0 .. 7 0 4
642
643 -- 1 .. 1 6 .. 6 1 6
644 -- 1 .. 4 3 .. 6 1 3
645 -- 4 .. 7 0 .. 3 4 0
646
647 -- The rule is that the first bit is obtained by subtracting the
648 -- old ending bit from machine scalar size - 1.
649
650 for C in Start .. Stop loop
651 declare
652 Comp : constant Entity_Id := Comps (C);
653 CC : constant Node_Id := Component_Clause (Comp);
654
655 FB : constant Uint := Static_Integer (First_Bit (CC));
656 LB : constant Uint := Static_Integer (Last_Bit (CC));
657 NFB : constant Uint := MSS - 1 - LB;
658 NLB : constant Uint := NFB + LB - FB;
659 Pos : constant Uint := Static_Integer (Position (CC));
660
661 begin
662 -- Do not warn for the artificial clause built for the tag
663 -- in Check_Record_Representation_Clause if it is inherited.
664
665 if Warn_On_Reverse_Bit_Order
666 and then Chars (Comp) /= Name_uTag
667 then
668 Error_Msg_Uint_1 := MSS;
669 Error_Msg_N
670 ("info: reverse bit order in machine scalar of "
671 & "length^?V?", First_Bit (CC));
672 Error_Msg_Uint_1 := NFB;
673 Error_Msg_Uint_2 := NLB;
674
675 if Bytes_Big_Endian then
676 Error_Msg_NE
677 ("\big-endian range for component & is ^ .. ^?V?",
678 First_Bit (CC), Comp);
679 else
680 Error_Msg_NE
681 ("\little-endian range for component & is ^ .. ^?V?",
682 First_Bit (CC), Comp);
683 end if;
684 end if;
685
686 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
687 Set_Esize (Comp, 1 + (NLB - NFB));
688 Set_Normalized_First_Bit (Comp, NFB mod SSU);
689 Set_Normalized_Position (Comp, Pos + NFB / SSU);
690 end;
691 end loop;
692 end loop;
693 end Sort_CC;
694 end Adjust_Record_For_Reverse_Bit_Order;
695
696 ------------------------------------------------
697 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
698 ------------------------------------------------
699
700 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
701 CC : Node_Id;
702 Comp : Node_Id;
703
704 begin
705 -- For Ada 95, we just renumber bits within a storage unit. We do the
706 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
707 -- Ada 83, and are free to add this extension.
708
709 Comp := First_Component_Or_Discriminant (R);
710 while Present (Comp) loop
711 CC := Component_Clause (Comp);
712
713 -- If component clause is present, then deal with the non-default
714 -- bit order case for Ada 95 mode.
715
716 -- We only do this processing for the base type, and in fact that
717 -- is important, since otherwise if there are record subtypes, we
718 -- could reverse the bits once for each subtype, which is wrong.
719
720 if Present (CC) and then Ekind (R) = E_Record_Type then
721 declare
722 CFB : constant Uint := Component_Bit_Offset (Comp);
723 CSZ : constant Uint := Esize (Comp);
724 CLC : constant Node_Id := Component_Clause (Comp);
725 Pos : constant Node_Id := Position (CLC);
726 FB : constant Node_Id := First_Bit (CLC);
727
728 Storage_Unit_Offset : constant Uint :=
729 CFB / System_Storage_Unit;
730
731 Start_Bit : constant Uint :=
732 CFB mod System_Storage_Unit;
733
734 begin
735 -- Cases where field goes over storage unit boundary
736
737 if Start_Bit + CSZ > System_Storage_Unit then
738
739 -- Allow multi-byte field but generate warning
740
741 if Start_Bit mod System_Storage_Unit = 0
742 and then CSZ mod System_Storage_Unit = 0
743 then
744 Error_Msg_N
745 ("info: multi-byte field specified with non-standard "
746 & "Bit_Order?V?", CLC);
747
748 if Bytes_Big_Endian then
749 Error_Msg_N
750 ("\bytes are not reversed "
751 & "(component is big-endian)?V?", CLC);
752 else
753 Error_Msg_N
754 ("\bytes are not reversed "
755 & "(component is little-endian)?V?", CLC);
756 end if;
757
758 -- Do not allow non-contiguous field
759
760 else
761 Error_Msg_N
762 ("attempt to specify non-contiguous field not "
763 & "permitted", CLC);
764 Error_Msg_N
765 ("\caused by non-standard Bit_Order specified in "
766 & "legacy Ada 95 mode", CLC);
767 end if;
768
769 -- Case where field fits in one storage unit
770
771 else
772 -- Give warning if suspicious component clause
773
774 if Intval (FB) >= System_Storage_Unit
775 and then Warn_On_Reverse_Bit_Order
776 then
777 Error_Msg_N
778 ("info: Bit_Order clause does not affect byte "
779 & "ordering?V?", Pos);
780 Error_Msg_Uint_1 :=
781 Intval (Pos) + Intval (FB) /
782 System_Storage_Unit;
783 Error_Msg_N
784 ("info: position normalized to ^ before bit order "
785 & "interpreted?V?", Pos);
786 end if;
787
788 -- Here is where we fix up the Component_Bit_Offset value
789 -- to account for the reverse bit order. Some examples of
790 -- what needs to be done are:
791
792 -- First_Bit .. Last_Bit Component_Bit_Offset
793 -- old new old new
794
795 -- 0 .. 0 7 .. 7 0 7
796 -- 0 .. 1 6 .. 7 0 6
797 -- 0 .. 2 5 .. 7 0 5
798 -- 0 .. 7 0 .. 7 0 4
799
800 -- 1 .. 1 6 .. 6 1 6
801 -- 1 .. 4 3 .. 6 1 3
802 -- 4 .. 7 0 .. 3 4 0
803
804 -- The rule is that the first bit is obtained by subtracting
805 -- the old ending bit from storage_unit - 1.
806
807 Set_Component_Bit_Offset (Comp,
808 (Storage_Unit_Offset * System_Storage_Unit) +
809 (System_Storage_Unit - 1) -
810 (Start_Bit + CSZ - 1));
811
812 Set_Normalized_Position (Comp,
813 Component_Bit_Offset (Comp) / System_Storage_Unit);
814
815 Set_Normalized_First_Bit (Comp,
816 Component_Bit_Offset (Comp) mod System_Storage_Unit);
817 end if;
818 end;
819 end if;
820
821 Next_Component_Or_Discriminant (Comp);
822 end loop;
823 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
824
825 -------------------------------------
826 -- Alignment_Check_For_Size_Change --
827 -------------------------------------
828
829 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
830 begin
831 -- If the alignment is known, and not set by a rep clause, and is
832 -- inconsistent with the size being set, then reset it to unknown,
833 -- we assume in this case that the size overrides the inherited
834 -- alignment, and that the alignment must be recomputed.
835
836 if Known_Alignment (Typ)
837 and then not Has_Alignment_Clause (Typ)
838 and then Size mod (Alignment (Typ) * SSU) /= 0
839 then
840 Init_Alignment (Typ);
841 end if;
842 end Alignment_Check_For_Size_Change;
843
844 -----------------------------------
845 -- All_Membership_Choices_Static --
846 -----------------------------------
847
848 function All_Membership_Choices_Static (Expr : Node_Id) return Boolean is
849 pragma Assert (Nkind (Expr) in N_Membership_Test);
850 begin
851 pragma Assert
852 (Present (Right_Opnd (Expr))
853 xor
854 Present (Alternatives (Expr)));
855
856 if Present (Right_Opnd (Expr)) then
857 return Is_Static_Choice (Right_Opnd (Expr));
858 else
859 return All_Static_Choices (Alternatives (Expr));
860 end if;
861 end All_Membership_Choices_Static;
862
863 ------------------------
864 -- All_Static_Choices --
865 ------------------------
866
867 function All_Static_Choices (L : List_Id) return Boolean is
868 N : Node_Id;
869
870 begin
871 N := First (L);
872 while Present (N) loop
873 if not Is_Static_Choice (N) then
874 return False;
875 end if;
876
877 Next (N);
878 end loop;
879
880 return True;
881 end All_Static_Choices;
882
883 -------------------------------------
884 -- Analyze_Aspects_At_Freeze_Point --
885 -------------------------------------
886
887 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
888 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
889 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
890 -- the aspect specification node ASN.
891
892 procedure Check_Aspect_Too_Late (N : Node_Id);
893 -- This procedure is similar to Rep_Item_Too_Late for representation
894 -- aspects that apply to type and that do not have a corresponding
895 -- pragma.
896 -- Used to check in particular that the expression associated with
897 -- aspect node N for the given type (entity) of the aspect does not
898 -- appear too late according to the rules in RM 13.1(9) and 13.1(10).
899
900 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
901 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
902 -- a derived type can inherit aspects from its parent which have been
903 -- specified at the time of the derivation using an aspect, as in:
904 --
905 -- type A is range 1 .. 10
906 -- with Size => Not_Defined_Yet;
907 -- ..
908 -- type B is new A;
909 -- ..
910 -- Not_Defined_Yet : constant := 64;
911 --
912 -- In this example, the Size of A is considered to be specified prior
913 -- to the derivation, and thus inherited, even though the value is not
914 -- known at the time of derivation. To deal with this, we use two entity
915 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
916 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
917 -- the derived type (B here). If this flag is set when the derived type
918 -- is frozen, then this procedure is called to ensure proper inheritance
919 -- of all delayed aspects from the parent type. The derived type is E,
920 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
921 -- aspect specification node in the Rep_Item chain for the parent type.
922
923 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
924 -- Given an aspect specification node ASN whose expression is an
925 -- optional Boolean, this routines creates the corresponding pragma
926 -- at the freezing point.
927
928 ----------------------------------
929 -- Analyze_Aspect_Default_Value --
930 ----------------------------------
931
932 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
933 Ent : constant Entity_Id := Entity (ASN);
934 Expr : constant Node_Id := Expression (ASN);
935
936 begin
937 Set_Has_Default_Aspect (Base_Type (Ent));
938
939 if Is_Scalar_Type (Ent) then
940 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
941 else
942 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
943 end if;
944
945 Check_Aspect_Too_Late (ASN);
946 end Analyze_Aspect_Default_Value;
947
948 ---------------------------
949 -- Check_Aspect_Too_Late --
950 ---------------------------
951
952 procedure Check_Aspect_Too_Late (N : Node_Id) is
953 Typ : constant Entity_Id := Entity (N);
954 Expr : constant Node_Id := Expression (N);
955
956 function Find_Type_Reference
957 (Typ : Entity_Id; Expr : Node_Id) return Boolean;
958 -- Return True if a reference to type Typ is found in the expression
959 -- Expr.
960
961 -------------------------
962 -- Find_Type_Reference --
963 -------------------------
964
965 function Find_Type_Reference
966 (Typ : Entity_Id; Expr : Node_Id) return Boolean
967 is
968 function Find_Type (N : Node_Id) return Traverse_Result;
969 -- Set Found to True if N refers to Typ
970
971 ---------------
972 -- Find_Type --
973 ---------------
974
975 function Find_Type (N : Node_Id) return Traverse_Result is
976 begin
977 if N = Typ
978 or else (Nkind_In (N, N_Identifier, N_Expanded_Name)
979 and then Present (Entity (N))
980 and then Entity (N) = Typ)
981 then
982 return Abandon;
983 else
984 return OK;
985 end if;
986 end Find_Type;
987
988 function Search_Type_Reference is new Traverse_Func (Find_Type);
989
990 begin
991 return Search_Type_Reference (Expr) = Abandon;
992 end Find_Type_Reference;
993
994 Parent_Type : Entity_Id;
995
996 begin
997 -- Ensure Expr is analyzed so that e.g. all types are properly
998 -- resolved for Find_Type_Reference.
999
1000 Analyze (Expr);
1001
1002 -- A self-referential aspect is illegal if it forces freezing the
1003 -- entity before the corresponding aspect has been analyzed.
1004
1005 if Find_Type_Reference (Typ, Expr) then
1006 Error_Msg_NE
1007 ("aspect specification causes premature freezing of&", N, Typ);
1008 end if;
1009
1010 -- For representation aspects, check for case of untagged derived
1011 -- type whose parent either has primitive operations (pre Ada 202x),
1012 -- or is a by-reference type (RM 13.1(10)).
1013 -- Strictly speaking the check also applies to Ada 2012 but it is
1014 -- really too constraining for existing code already, so relax it.
1015 -- ??? Confirming aspects should be allowed here.
1016
1017 if Is_Representation_Aspect (Get_Aspect_Id (N))
1018 and then Is_Derived_Type (Typ)
1019 and then not Is_Tagged_Type (Typ)
1020 then
1021 Parent_Type := Etype (Base_Type (Typ));
1022
1023 if Ada_Version <= Ada_2012
1024 and then Has_Primitive_Operations (Parent_Type)
1025 then
1026 Error_Msg_N
1027 ("|representation aspect not permitted before Ada 202x!", N);
1028 Error_Msg_NE
1029 ("\parent type & has primitive operations!", N, Parent_Type);
1030
1031 elsif Is_By_Reference_Type (Parent_Type) then
1032 No_Type_Rep_Item (N);
1033 Error_Msg_NE
1034 ("\parent type & is a by-reference type!", N, Parent_Type);
1035 end if;
1036 end if;
1037 end Check_Aspect_Too_Late;
1038
1039 ---------------------------------
1040 -- Inherit_Delayed_Rep_Aspects --
1041 ---------------------------------
1042
1043 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
1044 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
1045 P : constant Entity_Id := Entity (ASN);
1046 -- Entity for parent type
1047
1048 N : Node_Id;
1049 -- Item from Rep_Item chain
1050
1051 A : Aspect_Id;
1052
1053 begin
1054 -- Loop through delayed aspects for the parent type
1055
1056 N := ASN;
1057 while Present (N) loop
1058 if Nkind (N) = N_Aspect_Specification then
1059 exit when Entity (N) /= P;
1060
1061 if Is_Delayed_Aspect (N) then
1062 A := Get_Aspect_Id (Chars (Identifier (N)));
1063
1064 -- Process delayed rep aspect. For Boolean attributes it is
1065 -- not possible to cancel an attribute once set (the attempt
1066 -- to use an aspect with xxx => False is an error) for a
1067 -- derived type. So for those cases, we do not have to check
1068 -- if a clause has been given for the derived type, since it
1069 -- is harmless to set it again if it is already set.
1070
1071 case A is
1072
1073 -- Alignment
1074
1075 when Aspect_Alignment =>
1076 if not Has_Alignment_Clause (E) then
1077 Set_Alignment (E, Alignment (P));
1078 end if;
1079
1080 -- Atomic
1081
1082 when Aspect_Atomic =>
1083 if Is_Atomic (P) then
1084 Set_Is_Atomic (E);
1085 end if;
1086
1087 -- Atomic_Components
1088
1089 when Aspect_Atomic_Components =>
1090 if Has_Atomic_Components (P) then
1091 Set_Has_Atomic_Components (Base_Type (E));
1092 end if;
1093
1094 -- Bit_Order
1095
1096 when Aspect_Bit_Order =>
1097 if Is_Record_Type (E)
1098 and then No (Get_Attribute_Definition_Clause
1099 (E, Attribute_Bit_Order))
1100 and then Reverse_Bit_Order (P)
1101 then
1102 Set_Reverse_Bit_Order (Base_Type (E));
1103 end if;
1104
1105 -- Component_Size
1106
1107 when Aspect_Component_Size =>
1108 if Is_Array_Type (E)
1109 and then not Has_Component_Size_Clause (E)
1110 then
1111 Set_Component_Size
1112 (Base_Type (E), Component_Size (P));
1113 end if;
1114
1115 -- Machine_Radix
1116
1117 when Aspect_Machine_Radix =>
1118 if Is_Decimal_Fixed_Point_Type (E)
1119 and then not Has_Machine_Radix_Clause (E)
1120 then
1121 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
1122 end if;
1123
1124 -- Object_Size (also Size which also sets Object_Size)
1125
1126 when Aspect_Object_Size
1127 | Aspect_Size
1128 =>
1129 if not Has_Size_Clause (E)
1130 and then
1131 No (Get_Attribute_Definition_Clause
1132 (E, Attribute_Object_Size))
1133 then
1134 Set_Esize (E, Esize (P));
1135 end if;
1136
1137 -- Pack
1138
1139 when Aspect_Pack =>
1140 if not Is_Packed (E) then
1141 Set_Is_Packed (Base_Type (E));
1142
1143 if Is_Bit_Packed_Array (P) then
1144 Set_Is_Bit_Packed_Array (Base_Type (E));
1145 Set_Packed_Array_Impl_Type
1146 (E, Packed_Array_Impl_Type (P));
1147 end if;
1148 end if;
1149
1150 -- Scalar_Storage_Order
1151
1152 when Aspect_Scalar_Storage_Order =>
1153 if (Is_Record_Type (E) or else Is_Array_Type (E))
1154 and then No (Get_Attribute_Definition_Clause
1155 (E, Attribute_Scalar_Storage_Order))
1156 and then Reverse_Storage_Order (P)
1157 then
1158 Set_Reverse_Storage_Order (Base_Type (E));
1159
1160 -- Clear default SSO indications, since the aspect
1161 -- overrides the default.
1162
1163 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
1164 Set_SSO_Set_High_By_Default (Base_Type (E), False);
1165 end if;
1166
1167 -- Small
1168
1169 when Aspect_Small =>
1170 if Is_Fixed_Point_Type (E)
1171 and then not Has_Small_Clause (E)
1172 then
1173 Set_Small_Value (E, Small_Value (P));
1174 end if;
1175
1176 -- Storage_Size
1177
1178 when Aspect_Storage_Size =>
1179 if (Is_Access_Type (E) or else Is_Task_Type (E))
1180 and then not Has_Storage_Size_Clause (E)
1181 then
1182 Set_Storage_Size_Variable
1183 (Base_Type (E), Storage_Size_Variable (P));
1184 end if;
1185
1186 -- Value_Size
1187
1188 when Aspect_Value_Size =>
1189
1190 -- Value_Size is never inherited, it is either set by
1191 -- default, or it is explicitly set for the derived
1192 -- type. So nothing to do here.
1193
1194 null;
1195
1196 -- Volatile
1197
1198 when Aspect_Volatile =>
1199 if Is_Volatile (P) then
1200 Set_Is_Volatile (E);
1201 end if;
1202
1203 -- Volatile_Full_Access
1204
1205 when Aspect_Volatile_Full_Access =>
1206 if Is_Volatile_Full_Access (P) then
1207 Set_Is_Volatile_Full_Access (E);
1208 end if;
1209
1210 -- Volatile_Components
1211
1212 when Aspect_Volatile_Components =>
1213 if Has_Volatile_Components (P) then
1214 Set_Has_Volatile_Components (Base_Type (E));
1215 end if;
1216
1217 -- That should be all the Rep Aspects
1218
1219 when others =>
1220 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1221 null;
1222 end case;
1223 end if;
1224 end if;
1225
1226 Next_Rep_Item (N);
1227 end loop;
1228 end Inherit_Delayed_Rep_Aspects;
1229
1230 -------------------------------------
1231 -- Make_Pragma_From_Boolean_Aspect --
1232 -------------------------------------
1233
1234 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1235 Ident : constant Node_Id := Identifier (ASN);
1236 A_Name : constant Name_Id := Chars (Ident);
1237 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1238 Ent : constant Entity_Id := Entity (ASN);
1239 Expr : constant Node_Id := Expression (ASN);
1240 Loc : constant Source_Ptr := Sloc (ASN);
1241
1242 procedure Check_False_Aspect_For_Derived_Type;
1243 -- This procedure checks for the case of a false aspect for a derived
1244 -- type, which improperly tries to cancel an aspect inherited from
1245 -- the parent.
1246
1247 -----------------------------------------
1248 -- Check_False_Aspect_For_Derived_Type --
1249 -----------------------------------------
1250
1251 procedure Check_False_Aspect_For_Derived_Type is
1252 Par : Node_Id;
1253
1254 begin
1255 -- We are only checking derived types
1256
1257 if not Is_Derived_Type (E) then
1258 return;
1259 end if;
1260
1261 Par := Nearest_Ancestor (E);
1262
1263 case A_Id is
1264 when Aspect_Atomic
1265 | Aspect_Shared
1266 =>
1267 if not Is_Atomic (Par) then
1268 return;
1269 end if;
1270
1271 when Aspect_Atomic_Components =>
1272 if not Has_Atomic_Components (Par) then
1273 return;
1274 end if;
1275
1276 when Aspect_Discard_Names =>
1277 if not Discard_Names (Par) then
1278 return;
1279 end if;
1280
1281 when Aspect_Pack =>
1282 if not Is_Packed (Par) then
1283 return;
1284 end if;
1285
1286 when Aspect_Unchecked_Union =>
1287 if not Is_Unchecked_Union (Par) then
1288 return;
1289 end if;
1290
1291 when Aspect_Volatile =>
1292 if not Is_Volatile (Par) then
1293 return;
1294 end if;
1295
1296 when Aspect_Volatile_Components =>
1297 if not Has_Volatile_Components (Par) then
1298 return;
1299 end if;
1300
1301 when Aspect_Volatile_Full_Access =>
1302 if not Is_Volatile_Full_Access (Par) then
1303 return;
1304 end if;
1305
1306 when others =>
1307 return;
1308 end case;
1309
1310 -- Fall through means we are canceling an inherited aspect
1311
1312 Error_Msg_Name_1 := A_Name;
1313 Error_Msg_NE
1314 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1315 end Check_False_Aspect_For_Derived_Type;
1316
1317 -- Local variables
1318
1319 Prag : Node_Id;
1320
1321 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1322
1323 begin
1324 -- Note that we know Expr is present, because for a missing Expr
1325 -- argument, we knew it was True and did not need to delay the
1326 -- evaluation to the freeze point.
1327
1328 if Is_False (Static_Boolean (Expr)) then
1329 Check_False_Aspect_For_Derived_Type;
1330
1331 else
1332 Prag :=
1333 Make_Pragma (Loc,
1334 Pragma_Identifier =>
1335 Make_Identifier (Sloc (Ident), Chars (Ident)),
1336 Pragma_Argument_Associations => New_List (
1337 Make_Pragma_Argument_Association (Sloc (Ident),
1338 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1339
1340 Set_From_Aspect_Specification (Prag, True);
1341 Set_Corresponding_Aspect (Prag, ASN);
1342 Set_Aspect_Rep_Item (ASN, Prag);
1343 Set_Is_Delayed_Aspect (Prag);
1344 Set_Parent (Prag, ASN);
1345 end if;
1346 end Make_Pragma_From_Boolean_Aspect;
1347
1348 -- Local variables
1349
1350 A_Id : Aspect_Id;
1351 ASN : Node_Id;
1352 Ritem : Node_Id;
1353
1354 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1355
1356 begin
1357 -- Must be visible in current scope, but if this is a type from a nested
1358 -- package it may be frozen from an object declaration in the enclosing
1359 -- scope, so install the package declarations to complete the analysis
1360 -- of the aspects, if any. If the package itself is frozen the type will
1361 -- have been frozen as well.
1362
1363 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1364 if Is_Type (E) and then From_Nested_Package (E) then
1365 declare
1366 Pack : constant Entity_Id := Scope (E);
1367
1368 begin
1369 Push_Scope (Pack);
1370 Install_Visible_Declarations (Pack);
1371 Install_Private_Declarations (Pack);
1372 Analyze_Aspects_At_Freeze_Point (E);
1373
1374 if Is_Private_Type (E)
1375 and then Present (Full_View (E))
1376 then
1377 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1378 end if;
1379
1380 End_Package_Scope (Pack);
1381 return;
1382 end;
1383
1384 -- Aspects from other entities in different contexts are analyzed
1385 -- elsewhere.
1386
1387 else
1388 return;
1389 end if;
1390 end if;
1391
1392 -- Look for aspect specification entries for this entity
1393
1394 ASN := First_Rep_Item (E);
1395 while Present (ASN) loop
1396 if Nkind (ASN) = N_Aspect_Specification then
1397 exit when Entity (ASN) /= E;
1398
1399 if Is_Delayed_Aspect (ASN) then
1400 A_Id := Get_Aspect_Id (ASN);
1401
1402 case A_Id is
1403
1404 -- For aspects whose expression is an optional Boolean, make
1405 -- the corresponding pragma at the freeze point.
1406
1407 when Boolean_Aspects
1408 | Library_Unit_Aspects
1409 =>
1410 -- Aspects Export and Import require special handling.
1411 -- Both are by definition Boolean and may benefit from
1412 -- forward references, however their expressions are
1413 -- treated as static. In addition, the syntax of their
1414 -- corresponding pragmas requires extra "pieces" which
1415 -- may also contain forward references. To account for
1416 -- all of this, the corresponding pragma is created by
1417 -- Analyze_Aspect_Export_Import, but is not analyzed as
1418 -- the complete analysis must happen now.
1419
1420 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1421 null;
1422
1423 -- Otherwise create a corresponding pragma
1424
1425 else
1426 Make_Pragma_From_Boolean_Aspect (ASN);
1427 end if;
1428
1429 -- Special handling for aspects that don't correspond to
1430 -- pragmas/attributes.
1431
1432 when Aspect_Default_Value
1433 | Aspect_Default_Component_Value
1434 =>
1435 -- Do not inherit aspect for anonymous base type of a
1436 -- scalar or array type, because they apply to the first
1437 -- subtype of the type, and will be processed when that
1438 -- first subtype is frozen.
1439
1440 if Is_Derived_Type (E)
1441 and then not Comes_From_Source (E)
1442 and then E /= First_Subtype (E)
1443 then
1444 null;
1445 else
1446 Analyze_Aspect_Default_Value (ASN);
1447 end if;
1448
1449 -- Ditto for iterator aspects, because the corresponding
1450 -- attributes may not have been analyzed yet.
1451
1452 when Aspect_Constant_Indexing
1453 | Aspect_Default_Iterator
1454 | Aspect_Iterator_Element
1455 | Aspect_Variable_Indexing
1456 =>
1457 Analyze (Expression (ASN));
1458
1459 if Etype (Expression (ASN)) = Any_Type then
1460 Error_Msg_NE
1461 ("\aspect must be fully defined before & is frozen",
1462 ASN, E);
1463 end if;
1464
1465 when Aspect_Integer_Literal
1466 | Aspect_Real_Literal
1467 | Aspect_String_Literal
1468 =>
1469 Validate_Literal_Aspect (E, ASN);
1470
1471 when Aspect_Iterable =>
1472 Validate_Iterable_Aspect (E, ASN);
1473
1474 when others =>
1475 null;
1476 end case;
1477
1478 Ritem := Aspect_Rep_Item (ASN);
1479
1480 if Present (Ritem) then
1481 Analyze (Ritem);
1482 end if;
1483 end if;
1484 end if;
1485
1486 Next_Rep_Item (ASN);
1487 end loop;
1488
1489 -- This is where we inherit delayed rep aspects from our parent. Note
1490 -- that if we fell out of the above loop with ASN non-empty, it means
1491 -- we hit an aspect for an entity other than E, and it must be the
1492 -- type from which we were derived.
1493
1494 if May_Inherit_Delayed_Rep_Aspects (E) then
1495 Inherit_Delayed_Rep_Aspects (ASN);
1496 end if;
1497
1498 if In_Instance
1499 and then E /= Base_Type (E)
1500 and then Is_First_Subtype (E)
1501 then
1502 Inherit_Rep_Item_Chain (Base_Type (E), E);
1503 end if;
1504 end Analyze_Aspects_At_Freeze_Point;
1505
1506 -----------------------------------
1507 -- Analyze_Aspect_Specifications --
1508 -----------------------------------
1509
1510 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1511 pragma Assert (Present (E));
1512
1513 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1514 -- Establish linkages between an aspect and its corresponding pragma
1515
1516 procedure Insert_Pragma
1517 (Prag : Node_Id;
1518 Is_Instance : Boolean := False);
1519 -- Subsidiary to the analysis of aspects
1520 -- Abstract_State
1521 -- Attach_Handler
1522 -- Contract_Cases
1523 -- Depends
1524 -- Ghost
1525 -- Global
1526 -- Initial_Condition
1527 -- Initializes
1528 -- Post
1529 -- Pre
1530 -- Refined_Depends
1531 -- Refined_Global
1532 -- Refined_State
1533 -- SPARK_Mode
1534 -- Warnings
1535 -- Insert pragma Prag such that it mimics the placement of a source
1536 -- pragma of the same kind. Flag Is_Generic should be set when the
1537 -- context denotes a generic instance.
1538
1539 --------------
1540 -- Decorate --
1541 --------------
1542
1543 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1544 begin
1545 Set_Aspect_Rep_Item (Asp, Prag);
1546 Set_Corresponding_Aspect (Prag, Asp);
1547 Set_From_Aspect_Specification (Prag);
1548 Set_Parent (Prag, Asp);
1549 end Decorate;
1550
1551 -------------------
1552 -- Insert_Pragma --
1553 -------------------
1554
1555 procedure Insert_Pragma
1556 (Prag : Node_Id;
1557 Is_Instance : Boolean := False)
1558 is
1559 Aux : Node_Id;
1560 Decl : Node_Id;
1561 Decls : List_Id;
1562 Def : Node_Id;
1563 Inserted : Boolean := False;
1564
1565 begin
1566 -- When the aspect appears on an entry, package, protected unit,
1567 -- subprogram, or task unit body, insert the generated pragma at the
1568 -- top of the body declarations to emulate the behavior of a source
1569 -- pragma.
1570
1571 -- package body Pack with Aspect is
1572
1573 -- package body Pack is
1574 -- pragma Prag;
1575
1576 if Nkind_In (N, N_Entry_Body,
1577 N_Package_Body,
1578 N_Protected_Body,
1579 N_Subprogram_Body,
1580 N_Task_Body)
1581 then
1582 Decls := Declarations (N);
1583
1584 if No (Decls) then
1585 Decls := New_List;
1586 Set_Declarations (N, Decls);
1587 end if;
1588
1589 Prepend_To (Decls, Prag);
1590
1591 -- When the aspect is associated with a [generic] package declaration
1592 -- insert the generated pragma at the top of the visible declarations
1593 -- to emulate the behavior of a source pragma.
1594
1595 -- package Pack with Aspect is
1596
1597 -- package Pack is
1598 -- pragma Prag;
1599
1600 elsif Nkind_In (N, N_Generic_Package_Declaration,
1601 N_Package_Declaration)
1602 then
1603 Decls := Visible_Declarations (Specification (N));
1604
1605 if No (Decls) then
1606 Decls := New_List;
1607 Set_Visible_Declarations (Specification (N), Decls);
1608 end if;
1609
1610 -- The visible declarations of a generic instance have the
1611 -- following structure:
1612
1613 -- <renamings of generic formals>
1614 -- <renamings of internally-generated spec and body>
1615 -- <first source declaration>
1616
1617 -- Insert the pragma before the first source declaration by
1618 -- skipping the instance "header" to ensure proper visibility of
1619 -- all formals.
1620
1621 if Is_Instance then
1622 Decl := First (Decls);
1623 while Present (Decl) loop
1624 if Comes_From_Source (Decl) then
1625 Insert_Before (Decl, Prag);
1626 Inserted := True;
1627 exit;
1628 else
1629 Next (Decl);
1630 end if;
1631 end loop;
1632
1633 -- The pragma is placed after the instance "header"
1634
1635 if not Inserted then
1636 Append_To (Decls, Prag);
1637 end if;
1638
1639 -- Otherwise this is not a generic instance
1640
1641 else
1642 Prepend_To (Decls, Prag);
1643 end if;
1644
1645 -- When the aspect is associated with a protected unit declaration,
1646 -- insert the generated pragma at the top of the visible declarations
1647 -- the emulate the behavior of a source pragma.
1648
1649 -- protected [type] Prot with Aspect is
1650
1651 -- protected [type] Prot is
1652 -- pragma Prag;
1653
1654 elsif Nkind (N) = N_Protected_Type_Declaration then
1655 Def := Protected_Definition (N);
1656
1657 if No (Def) then
1658 Def :=
1659 Make_Protected_Definition (Sloc (N),
1660 Visible_Declarations => New_List,
1661 End_Label => Empty);
1662
1663 Set_Protected_Definition (N, Def);
1664 end if;
1665
1666 Decls := Visible_Declarations (Def);
1667
1668 if No (Decls) then
1669 Decls := New_List;
1670 Set_Visible_Declarations (Def, Decls);
1671 end if;
1672
1673 Prepend_To (Decls, Prag);
1674
1675 -- When the aspect is associated with a task unit declaration, insert
1676 -- insert the generated pragma at the top of the visible declarations
1677 -- the emulate the behavior of a source pragma.
1678
1679 -- task [type] Prot with Aspect is
1680
1681 -- task [type] Prot is
1682 -- pragma Prag;
1683
1684 elsif Nkind (N) = N_Task_Type_Declaration then
1685 Def := Task_Definition (N);
1686
1687 if No (Def) then
1688 Def :=
1689 Make_Task_Definition (Sloc (N),
1690 Visible_Declarations => New_List,
1691 End_Label => Empty);
1692
1693 Set_Task_Definition (N, Def);
1694 end if;
1695
1696 Decls := Visible_Declarations (Def);
1697
1698 if No (Decls) then
1699 Decls := New_List;
1700 Set_Visible_Declarations (Def, Decls);
1701 end if;
1702
1703 Prepend_To (Decls, Prag);
1704
1705 -- When the context is a library unit, the pragma is added to the
1706 -- Pragmas_After list.
1707
1708 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1709 Aux := Aux_Decls_Node (Parent (N));
1710
1711 if No (Pragmas_After (Aux)) then
1712 Set_Pragmas_After (Aux, New_List);
1713 end if;
1714
1715 Prepend (Prag, Pragmas_After (Aux));
1716
1717 -- Default, the pragma is inserted after the context
1718
1719 else
1720 Insert_After (N, Prag);
1721 end if;
1722 end Insert_Pragma;
1723
1724 -- Local variables
1725
1726 Aspect : Node_Id;
1727 Aitem : Node_Id := Empty;
1728 Ent : Node_Id;
1729
1730 L : constant List_Id := Aspect_Specifications (N);
1731 pragma Assert (Present (L));
1732
1733 Ins_Node : Node_Id := N;
1734 -- Insert pragmas/attribute definition clause after this node when no
1735 -- delayed analysis is required.
1736
1737 -- Start of processing for Analyze_Aspect_Specifications
1738
1739 begin
1740 -- The general processing involves building an attribute definition
1741 -- clause or a pragma node that corresponds to the aspect. Then in order
1742 -- to delay the evaluation of this aspect to the freeze point, we attach
1743 -- the corresponding pragma/attribute definition clause to the aspect
1744 -- specification node, which is then placed in the Rep Item chain. In
1745 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1746 -- and we evaluate the rep item at the freeze point. When the aspect
1747 -- doesn't have a corresponding pragma/attribute definition clause, then
1748 -- its analysis is simply delayed at the freeze point.
1749
1750 -- Some special cases don't require delay analysis, thus the aspect is
1751 -- analyzed right now.
1752
1753 -- Note that there is a special handling for Pre, Post, Test_Case,
1754 -- Contract_Cases aspects. In these cases, we do not have to worry
1755 -- about delay issues, since the pragmas themselves deal with delay
1756 -- of visibility for the expression analysis. Thus, we just insert
1757 -- the pragma after the node N.
1758
1759 -- Loop through aspects
1760
1761 Aspect := First (L);
1762 Aspect_Loop : while Present (Aspect) loop
1763 Analyze_One_Aspect : declare
1764 Expr : constant Node_Id := Expression (Aspect);
1765 Id : constant Node_Id := Identifier (Aspect);
1766 Loc : constant Source_Ptr := Sloc (Aspect);
1767 Nam : constant Name_Id := Chars (Id);
1768 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1769 Anod : Node_Id;
1770
1771 Delay_Required : Boolean;
1772 -- Set False if delay is not required
1773
1774 Eloc : Source_Ptr := No_Location;
1775 -- Source location of expression, modified when we split PPC's. It
1776 -- is set below when Expr is present.
1777
1778 procedure Analyze_Aspect_Convention;
1779 -- Perform analysis of aspect Convention
1780
1781 procedure Analyze_Aspect_Disable_Controlled;
1782 -- Perform analysis of aspect Disable_Controlled
1783
1784 procedure Analyze_Aspect_Export_Import;
1785 -- Perform analysis of aspects Export or Import
1786
1787 procedure Analyze_Aspect_External_Link_Name;
1788 -- Perform analysis of aspects External_Name or Link_Name
1789
1790 procedure Analyze_Aspect_Implicit_Dereference;
1791 -- Perform analysis of the Implicit_Dereference aspects
1792
1793 procedure Analyze_Aspect_Relaxed_Initialization;
1794 -- Perform analysis of aspect Relaxed_Initialization
1795
1796 procedure Analyze_Aspect_Yield;
1797 -- Perform analysis of aspect Yield
1798
1799 procedure Analyze_Aspect_Static;
1800 -- Ada 202x (AI12-0075): Perform analysis of aspect Static
1801
1802 procedure Make_Aitem_Pragma
1803 (Pragma_Argument_Associations : List_Id;
1804 Pragma_Name : Name_Id);
1805 -- This is a wrapper for Make_Pragma used for converting aspects
1806 -- to pragmas. It takes care of Sloc (set from Loc) and building
1807 -- the pragma identifier from the given name. In addition the
1808 -- flags Class_Present and Split_PPC are set from the aspect
1809 -- node, as well as Is_Ignored. This routine also sets the
1810 -- From_Aspect_Specification in the resulting pragma node to
1811 -- True, and sets Corresponding_Aspect to point to the aspect.
1812 -- The resulting pragma is assigned to Aitem.
1813
1814 -------------------------------
1815 -- Analyze_Aspect_Convention --
1816 -------------------------------
1817
1818 procedure Analyze_Aspect_Convention is
1819 Conv : Node_Id;
1820 Dummy_1 : Node_Id;
1821 Dummy_2 : Node_Id;
1822 Dummy_3 : Node_Id;
1823 Expo : Node_Id;
1824 Imp : Node_Id;
1825
1826 begin
1827 -- Obtain all interfacing aspects that apply to the related
1828 -- entity.
1829
1830 Get_Interfacing_Aspects
1831 (Iface_Asp => Aspect,
1832 Conv_Asp => Dummy_1,
1833 EN_Asp => Dummy_2,
1834 Expo_Asp => Expo,
1835 Imp_Asp => Imp,
1836 LN_Asp => Dummy_3,
1837 Do_Checks => True);
1838
1839 -- The related entity is subject to aspect Export or Import.
1840 -- Do not process Convention now because it must be analysed
1841 -- as part of Export or Import.
1842
1843 if Present (Expo) or else Present (Imp) then
1844 return;
1845
1846 -- Otherwise Convention appears by itself
1847
1848 else
1849 -- The aspect specifies a particular convention
1850
1851 if Present (Expr) then
1852 Conv := New_Copy_Tree (Expr);
1853
1854 -- Otherwise assume convention Ada
1855
1856 else
1857 Conv := Make_Identifier (Loc, Name_Ada);
1858 end if;
1859
1860 -- Generate:
1861 -- pragma Convention (<Conv>, <E>);
1862
1863 Make_Aitem_Pragma
1864 (Pragma_Name => Name_Convention,
1865 Pragma_Argument_Associations => New_List (
1866 Make_Pragma_Argument_Association (Loc,
1867 Expression => Conv),
1868 Make_Pragma_Argument_Association (Loc,
1869 Expression => New_Occurrence_Of (E, Loc))));
1870
1871 Decorate (Aspect, Aitem);
1872 Insert_Pragma (Aitem);
1873 end if;
1874 end Analyze_Aspect_Convention;
1875
1876 ---------------------------------------
1877 -- Analyze_Aspect_Disable_Controlled --
1878 ---------------------------------------
1879
1880 procedure Analyze_Aspect_Disable_Controlled is
1881 begin
1882 -- The aspect applies only to controlled records
1883
1884 if not (Ekind (E) = E_Record_Type
1885 and then Is_Controlled_Active (E))
1886 then
1887 Error_Msg_N
1888 ("aspect % requires controlled record type", Aspect);
1889 return;
1890 end if;
1891
1892 -- Preanalyze the expression (if any) when the aspect resides
1893 -- in a generic unit.
1894
1895 if Inside_A_Generic then
1896 if Present (Expr) then
1897 Preanalyze_And_Resolve (Expr, Any_Boolean);
1898 end if;
1899
1900 -- Otherwise the aspect resides in a nongeneric context
1901
1902 else
1903 -- A controlled record type loses its controlled semantics
1904 -- when the expression statically evaluates to True.
1905
1906 if Present (Expr) then
1907 Analyze_And_Resolve (Expr, Any_Boolean);
1908
1909 if Is_OK_Static_Expression (Expr) then
1910 if Is_True (Static_Boolean (Expr)) then
1911 Set_Disable_Controlled (E);
1912 end if;
1913
1914 -- Otherwise the expression is not static
1915
1916 else
1917 Error_Msg_N
1918 ("expression of aspect % must be static", Aspect);
1919 end if;
1920
1921 -- Otherwise the aspect appears without an expression and
1922 -- defaults to True.
1923
1924 else
1925 Set_Disable_Controlled (E);
1926 end if;
1927 end if;
1928 end Analyze_Aspect_Disable_Controlled;
1929
1930 ----------------------------------
1931 -- Analyze_Aspect_Export_Import --
1932 ----------------------------------
1933
1934 procedure Analyze_Aspect_Export_Import is
1935 Dummy_1 : Node_Id;
1936 Dummy_2 : Node_Id;
1937 Dummy_3 : Node_Id;
1938 Expo : Node_Id;
1939 Imp : Node_Id;
1940
1941 begin
1942 -- Obtain all interfacing aspects that apply to the related
1943 -- entity.
1944
1945 Get_Interfacing_Aspects
1946 (Iface_Asp => Aspect,
1947 Conv_Asp => Dummy_1,
1948 EN_Asp => Dummy_2,
1949 Expo_Asp => Expo,
1950 Imp_Asp => Imp,
1951 LN_Asp => Dummy_3,
1952 Do_Checks => True);
1953
1954 -- The related entity cannot be subject to both aspects Export
1955 -- and Import.
1956
1957 if Present (Expo) and then Present (Imp) then
1958 Error_Msg_N
1959 ("incompatible interfacing aspects given for &", E);
1960 Error_Msg_Sloc := Sloc (Expo);
1961 Error_Msg_N ("\aspect `Export` #", E);
1962 Error_Msg_Sloc := Sloc (Imp);
1963 Error_Msg_N ("\aspect `Import` #", E);
1964 end if;
1965
1966 -- A variable is most likely modified from the outside. Take
1967 -- the optimistic approach to avoid spurious errors.
1968
1969 if Ekind (E) = E_Variable then
1970 Set_Never_Set_In_Source (E, False);
1971 end if;
1972
1973 -- Resolve the expression of an Import or Export here, and
1974 -- require it to be of type Boolean and static. This is not
1975 -- quite right, because in general this should be delayed,
1976 -- but that seems tricky for these, because normally Boolean
1977 -- aspects are replaced with pragmas at the freeze point in
1978 -- Make_Pragma_From_Boolean_Aspect.
1979
1980 if not Present (Expr)
1981 or else Is_True (Static_Boolean (Expr))
1982 then
1983 if A_Id = Aspect_Import then
1984 Set_Has_Completion (E);
1985 Set_Is_Imported (E);
1986
1987 -- An imported object cannot be explicitly initialized
1988
1989 if Nkind (N) = N_Object_Declaration
1990 and then Present (Expression (N))
1991 then
1992 Error_Msg_N
1993 ("imported entities cannot be initialized "
1994 & "(RM B.1(24))", Expression (N));
1995 end if;
1996
1997 else
1998 pragma Assert (A_Id = Aspect_Export);
1999 Set_Is_Exported (E);
2000 end if;
2001
2002 -- Create the proper form of pragma Export or Import taking
2003 -- into account Conversion, External_Name, and Link_Name.
2004
2005 Aitem := Build_Export_Import_Pragma (Aspect, E);
2006
2007 -- Otherwise the expression is either False or erroneous. There
2008 -- is no corresponding pragma.
2009
2010 else
2011 Aitem := Empty;
2012 end if;
2013 end Analyze_Aspect_Export_Import;
2014
2015 ---------------------------------------
2016 -- Analyze_Aspect_External_Link_Name --
2017 ---------------------------------------
2018
2019 procedure Analyze_Aspect_External_Link_Name is
2020 Dummy_1 : Node_Id;
2021 Dummy_2 : Node_Id;
2022 Dummy_3 : Node_Id;
2023 Expo : Node_Id;
2024 Imp : Node_Id;
2025
2026 begin
2027 -- Obtain all interfacing aspects that apply to the related
2028 -- entity.
2029
2030 Get_Interfacing_Aspects
2031 (Iface_Asp => Aspect,
2032 Conv_Asp => Dummy_1,
2033 EN_Asp => Dummy_2,
2034 Expo_Asp => Expo,
2035 Imp_Asp => Imp,
2036 LN_Asp => Dummy_3,
2037 Do_Checks => True);
2038
2039 -- Ensure that aspect External_Name applies to aspect Export or
2040 -- Import.
2041
2042 if A_Id = Aspect_External_Name then
2043 if No (Expo) and then No (Imp) then
2044 Error_Msg_N
2045 ("aspect `External_Name` requires aspect `Import` or "
2046 & "`Export`", Aspect);
2047 end if;
2048
2049 -- Otherwise ensure that aspect Link_Name applies to aspect
2050 -- Export or Import.
2051
2052 else
2053 pragma Assert (A_Id = Aspect_Link_Name);
2054 if No (Expo) and then No (Imp) then
2055 Error_Msg_N
2056 ("aspect `Link_Name` requires aspect `Import` or "
2057 & "`Export`", Aspect);
2058 end if;
2059 end if;
2060 end Analyze_Aspect_External_Link_Name;
2061
2062 -----------------------------------------
2063 -- Analyze_Aspect_Implicit_Dereference --
2064 -----------------------------------------
2065
2066 procedure Analyze_Aspect_Implicit_Dereference is
2067 begin
2068 if not Is_Type (E) or else not Has_Discriminants (E) then
2069 Error_Msg_N
2070 ("aspect must apply to a type with discriminants", Expr);
2071
2072 elsif not Is_Entity_Name (Expr) then
2073 Error_Msg_N
2074 ("aspect must name a discriminant of current type", Expr);
2075
2076 else
2077 -- Discriminant type be an anonymous access type or an
2078 -- anonymous access to subprogram.
2079
2080 -- Missing synchronized types???
2081
2082 declare
2083 Disc : Entity_Id := First_Discriminant (E);
2084 begin
2085 while Present (Disc) loop
2086 if Chars (Expr) = Chars (Disc)
2087 and then Ekind_In
2088 (Etype (Disc),
2089 E_Anonymous_Access_Subprogram_Type,
2090 E_Anonymous_Access_Type)
2091 then
2092 Set_Has_Implicit_Dereference (E);
2093 Set_Has_Implicit_Dereference (Disc);
2094 exit;
2095 end if;
2096
2097 Next_Discriminant (Disc);
2098 end loop;
2099
2100 -- Error if no proper access discriminant
2101
2102 if Present (Disc) then
2103 -- For a type extension, check whether parent has
2104 -- a reference discriminant, to verify that use is
2105 -- proper.
2106
2107 if Is_Derived_Type (E)
2108 and then Has_Discriminants (Etype (E))
2109 then
2110 declare
2111 Parent_Disc : constant Entity_Id :=
2112 Get_Reference_Discriminant (Etype (E));
2113 begin
2114 if Present (Parent_Disc)
2115 and then Corresponding_Discriminant (Disc) /=
2116 Parent_Disc
2117 then
2118 Error_Msg_N
2119 ("reference discriminant does not match "
2120 & "discriminant of parent type", Expr);
2121 end if;
2122 end;
2123 end if;
2124
2125 else
2126 Error_Msg_NE
2127 ("not an access discriminant of&", Expr, E);
2128 end if;
2129 end;
2130 end if;
2131
2132 end Analyze_Aspect_Implicit_Dereference;
2133
2134 -------------------------------------------
2135 -- Analyze_Aspect_Relaxed_Initialization --
2136 -------------------------------------------
2137
2138 procedure Analyze_Aspect_Relaxed_Initialization is
2139 procedure Analyze_Relaxed_Parameter
2140 (Subp_Id : Entity_Id;
2141 Param : Node_Id;
2142 Seen : in out Elist_Id);
2143 -- Analyze parameter that appears in the expression of the
2144 -- aspect Relaxed_Initialization.
2145
2146 -------------------------------
2147 -- Analyze_Relaxed_Parameter --
2148 -------------------------------
2149
2150 procedure Analyze_Relaxed_Parameter
2151 (Subp_Id : Entity_Id;
2152 Param : Node_Id;
2153 Seen : in out Elist_Id)
2154 is
2155 begin
2156 -- The relaxed parameter is a formal parameter
2157
2158 if Nkind_In (Param, N_Identifier, N_Expanded_Name) then
2159 Analyze (Param);
2160
2161 declare
2162 Item : constant Entity_Id := Entity (Param);
2163 begin
2164 -- It must be a formal of the analyzed subprogram
2165
2166 if Scope (Item) = Subp_Id then
2167
2168 pragma Assert (Is_Formal (Item));
2169
2170 -- Detect duplicated items
2171
2172 if Contains (Seen, Item) then
2173 Error_Msg_N ("duplicate aspect % item", Param);
2174 else
2175 Append_New_Elmt (Item, Seen);
2176 end if;
2177 else
2178 Error_Msg_N ("illegal aspect % item", Param);
2179 end if;
2180 end;
2181
2182 -- The relaxed parameter is the function's Result attribute
2183
2184 elsif Is_Attribute_Result (Param) then
2185 Analyze (Param);
2186
2187 declare
2188 Pref : constant Node_Id := Prefix (Param);
2189 begin
2190 if Present (Pref)
2191 and then
2192 Nkind_In (Pref, N_Identifier, N_Expanded_Name)
2193 and then
2194 Entity (Pref) = Subp_Id
2195 then
2196 -- Detect duplicated items
2197
2198 if Contains (Seen, Subp_Id) then
2199 Error_Msg_N ("duplicate aspect % item", Param);
2200 else
2201 Append_New_Elmt (Entity (Pref), Seen);
2202 end if;
2203
2204 else
2205 Error_Msg_N ("illegal aspect % item", Param);
2206 end if;
2207 end;
2208 else
2209 Error_Msg_N ("illegal aspect % item", Param);
2210 end if;
2211 end Analyze_Relaxed_Parameter;
2212
2213 -- Local variables
2214
2215 Seen : Elist_Id := No_Elist;
2216 -- Items that appear in the relaxed initialization aspect
2217 -- expression of a subprogram; for detecting duplicates.
2218
2219 Restore_Scope : Boolean;
2220 -- Will be set to True if we need to restore the scope table
2221 -- after analyzing the aspect expression.
2222
2223 Prev_Id : Entity_Id;
2224
2225 -- Start of processing for Analyze_Aspect_Relaxed_Initialization
2226
2227 begin
2228 -- Set name of the aspect for error messages
2229 Error_Msg_Name_1 := Nam;
2230
2231 -- Annotation of a type; no aspect expression is allowed.
2232 -- For a private type, the aspect must be attached to the
2233 -- partial view.
2234 --
2235 -- ??? Once the exact rule for this aspect is ready, we will
2236 -- likely reject concurrent types, etc., so let's keep the code
2237 -- for types and variable separate.
2238
2239 if Is_First_Subtype (E) then
2240 Prev_Id := Incomplete_Or_Partial_View (E);
2241 if Present (Prev_Id) then
2242
2243 -- Aspect may appear on the full view of an incomplete
2244 -- type because the incomplete declaration cannot have
2245 -- any aspects.
2246
2247 if Ekind (Prev_Id) = E_Incomplete_Type then
2248 null;
2249 else
2250 Error_Msg_N ("aspect % must apply to partial view", N);
2251 end if;
2252
2253 elsif Present (Expr) then
2254 Error_Msg_N ("illegal aspect % expression", Expr);
2255 end if;
2256
2257 -- Annotation of a variable; no aspect expression is allowed
2258
2259 elsif Ekind (E) = E_Variable then
2260 if Present (Expr) then
2261 Error_Msg_N ("illegal aspect % expression", Expr);
2262 end if;
2263
2264 -- Annotation of a constant; no aspect expression is allowed.
2265 -- For a deferred constant, the aspect must be attached to the
2266 -- partial view.
2267
2268 elsif Ekind (E) = E_Constant then
2269 if Present (Incomplete_Or_Partial_View (E)) then
2270 Error_Msg_N
2271 ("aspect % must apply to deferred constant", N);
2272
2273 elsif Present (Expr) then
2274 Error_Msg_N ("illegal aspect % expression", Expr);
2275 end if;
2276
2277 -- Annotation of a subprogram; aspect expression is required
2278
2279 elsif Is_Subprogram_Or_Entry (E) then
2280 if Present (Expr) then
2281
2282 -- If we analyze subprogram body that acts as its own
2283 -- spec, then the subprogram itself and its formals are
2284 -- already installed; otherwise, we need to install them,
2285 -- as they must be visible when analyzing the aspect
2286 -- expression.
2287
2288 if In_Open_Scopes (E) then
2289 Restore_Scope := False;
2290 else
2291 Restore_Scope := True;
2292 Push_Scope (E);
2293
2294 if Is_Generic_Subprogram (E) then
2295 Install_Generic_Formals (E);
2296 else
2297 Install_Formals (E);
2298 end if;
2299 end if;
2300
2301 -- Aspect expression is either an aggregate with list of
2302 -- parameters (and possibly the Result attribute for a
2303 -- function).
2304
2305 if Nkind (Expr) = N_Aggregate then
2306
2307 -- Component associations in the aggregate must be a
2308 -- parameter name followed by a static boolean
2309 -- expression.
2310
2311 if Present (Component_Associations (Expr)) then
2312 declare
2313 Assoc : Node_Id :=
2314 First (Component_Associations (Expr));
2315 begin
2316 while Present (Assoc) loop
2317 if List_Length (Choices (Assoc)) = 1 then
2318 Analyze_Relaxed_Parameter
2319 (E, First (Choices (Assoc)), Seen);
2320
2321 if Inside_A_Generic then
2322 Preanalyze_And_Resolve
2323 (Expression (Assoc), Any_Boolean);
2324 else
2325 Analyze_And_Resolve
2326 (Expression (Assoc), Any_Boolean);
2327 end if;
2328
2329 if not Is_OK_Static_Expression
2330 (Expression (Assoc))
2331 then
2332 Error_Msg_N
2333 ("expression of aspect %" &
2334 "must be static", Aspect);
2335 end if;
2336
2337 else
2338 Error_Msg_N
2339 ("illegal aspect % expression", Expr);
2340 end if;
2341 Next (Assoc);
2342 end loop;
2343 end;
2344 end if;
2345
2346 -- Expressions of the aggregate are parameter names
2347
2348 if Present (Expressions (Expr)) then
2349 declare
2350 Param : Node_Id := First (Expressions (Expr));
2351
2352 begin
2353 while Present (Param) loop
2354 Analyze_Relaxed_Parameter (E, Param, Seen);
2355 Next (Param);
2356 end loop;
2357 end;
2358 end if;
2359
2360 -- Mark the aggregate expression itself as analyzed;
2361 -- its subexpressions were marked when they themselves
2362 -- were analyzed.
2363
2364 Set_Analyzed (Expr);
2365
2366 -- Otherwise, it is a single name of a subprogram
2367 -- parameter (or possibly the Result attribute for
2368 -- a function).
2369
2370 else
2371 Analyze_Relaxed_Parameter (E, Expr, Seen);
2372 end if;
2373
2374 if Restore_Scope then
2375 End_Scope;
2376 end if;
2377 else
2378 Error_Msg_N ("missing expression for aspect %", N);
2379 end if;
2380
2381 else
2382 Error_Msg_N ("inappropriate entity for aspect %", E);
2383 end if;
2384 end Analyze_Aspect_Relaxed_Initialization;
2385
2386 ---------------------------
2387 -- Analyze_Aspect_Static --
2388 ---------------------------
2389
2390 procedure Analyze_Aspect_Static is
2391 begin
2392 if Ada_Version < Ada_2020 then
2393 Error_Msg_N
2394 ("aspect % is an Ada 202x feature", Aspect);
2395 Error_Msg_N ("\compile with -gnat2020", Aspect);
2396
2397 return;
2398
2399 -- The aspect applies only to expression functions that
2400 -- statisfy the requirements for a static expression function
2401 -- (such as having an expression that is predicate-static).
2402
2403 elsif not Is_Expression_Function (E) then
2404 Error_Msg_N
2405 ("aspect % requires expression function", Aspect);
2406
2407 return;
2408
2409 -- Ada 202x (AI12-0075): Check that the function satisfies
2410 -- several requirements of static expression functions as
2411 -- specified in RM 6.8(5.1-5.8). Note that some of the
2412 -- requirements given there are checked elsewhere.
2413
2414 else
2415 -- The expression of the expression function must be a
2416 -- potentially static expression (RM 202x 6.8(3.2-3.4)).
2417 -- That's checked in Sem_Ch6.Analyze_Expression_Function.
2418
2419 -- The function must not contain any calls to itself, which
2420 -- is checked in Sem_Res.Resolve_Call.
2421
2422 -- Each formal must be of mode in and have a static subtype
2423
2424 declare
2425 Formal : Entity_Id := First_Formal (E);
2426 begin
2427 while Present (Formal) loop
2428 if Ekind (Formal) /= E_In_Parameter then
2429 Error_Msg_N
2430 ("aspect % requires formals of mode IN",
2431 Aspect);
2432
2433 return;
2434 end if;
2435
2436 if not Is_Static_Subtype (Etype (Formal)) then
2437 Error_Msg_N
2438 ("aspect % requires formals with static subtypes",
2439 Aspect);
2440
2441 return;
2442 end if;
2443
2444 Next_Formal (Formal);
2445 end loop;
2446 end;
2447
2448 -- The function's result subtype must be a static subtype
2449
2450 if not Is_Static_Subtype (Etype (E)) then
2451 Error_Msg_N
2452 ("aspect % requires function with result of "
2453 & "a static subtype",
2454 Aspect);
2455
2456 return;
2457 end if;
2458
2459 -- Check that the function does not have any applicable
2460 -- precondition or postcondition expression.
2461
2462 for Asp in Pre_Post_Aspects loop
2463 if Has_Aspect (E, Asp) then
2464 Error_Msg_N
2465 ("this aspect not allowed for static expression "
2466 & "functions", Find_Aspect (E, Asp));
2467
2468 return;
2469 end if;
2470 end loop;
2471
2472 -- ??? TBD: Must check that "for result type R, if the
2473 -- function is a boundary entity for type R (see 7.3.2),
2474 -- no type invariant applies to type R; if R has a
2475 -- component type C, a similar rule applies to C."
2476 end if;
2477
2478 -- Preanalyze the expression (if any) when the aspect resides
2479 -- in a generic unit. (Is this generic-related code necessary
2480 -- for this aspect? It's modeled on what's done for aspect
2481 -- Disable_Controlled. ???)
2482
2483 if Inside_A_Generic then
2484 if Present (Expr) then
2485 Preanalyze_And_Resolve (Expr, Any_Boolean);
2486 end if;
2487
2488 -- Otherwise the aspect resides in a nongeneric context
2489
2490 else
2491 -- When the expression statically evaluates to True, the
2492 -- expression function is treated as a static function.
2493 -- Otherwise the aspect appears without an expression and
2494 -- defaults to True.
2495
2496 if Present (Expr) then
2497 Analyze_And_Resolve (Expr, Any_Boolean);
2498
2499 -- Error if the boolean expression is not static
2500
2501 if not Is_OK_Static_Expression (Expr) then
2502 Error_Msg_N
2503 ("expression of aspect % must be static", Aspect);
2504 end if;
2505 end if;
2506 end if;
2507 end Analyze_Aspect_Static;
2508
2509 --------------------------
2510 -- Analyze_Aspect_Yield --
2511 --------------------------
2512
2513 procedure Analyze_Aspect_Yield is
2514 Expr_Value : Boolean := False;
2515
2516 begin
2517 -- Check valid declarations for 'Yield
2518
2519 if (Nkind_In (N, N_Abstract_Subprogram_Declaration,
2520 N_Entry_Declaration,
2521 N_Generic_Subprogram_Declaration,
2522 N_Subprogram_Declaration)
2523 or else Nkind (N) in N_Formal_Subprogram_Declaration)
2524 and then not Within_Protected_Type (E)
2525 then
2526 null;
2527
2528 elsif Within_Protected_Type (E) then
2529 Error_Msg_N
2530 ("aspect% not applicable to protected operations", Id);
2531 return;
2532
2533 else
2534 Error_Msg_N
2535 ("aspect% only applicable to subprogram and entry "
2536 & "declarations", Id);
2537 return;
2538 end if;
2539
2540 -- Evaluate its static expression (if available); otherwise it
2541 -- defaults to True.
2542
2543 if No (Expr) then
2544 Expr_Value := True;
2545
2546 -- Otherwise it must have a static boolean expression
2547
2548 else
2549 if Inside_A_Generic then
2550 Preanalyze_And_Resolve (Expr, Any_Boolean);
2551 else
2552 Analyze_And_Resolve (Expr, Any_Boolean);
2553 end if;
2554
2555 if Is_OK_Static_Expression (Expr) then
2556 if Is_True (Static_Boolean (Expr)) then
2557 Expr_Value := True;
2558 end if;
2559 else
2560 Error_Msg_N
2561 ("expression of aspect % must be static", Aspect);
2562 end if;
2563 end if;
2564
2565 if Expr_Value then
2566
2567 -- Adding minimum decoration to generic subprograms to set
2568 -- the Yield attribute (since at this stage it may not be
2569 -- set; see Analyze_Generic_Subprogram_Declaration).
2570
2571 if Nkind (N) in N_Generic_Subprogram_Declaration
2572 and then Ekind (E) = E_Void
2573 then
2574 if Nkind (Specification (N)) = N_Function_Specification
2575 then
2576 Set_Ekind (E, E_Function);
2577 else
2578 Set_Ekind (E, E_Procedure);
2579 end if;
2580 end if;
2581
2582 Set_Has_Yield_Aspect (E);
2583 end if;
2584
2585 -- If the Yield aspect is specified for a dispatching
2586 -- subprogram that inherits the aspect, the specified
2587 -- value shall be confirming.
2588
2589 if Present (Expr)
2590 and then Is_Dispatching_Operation (E)
2591 and then Present (Overridden_Operation (E))
2592 and then Has_Yield_Aspect (Overridden_Operation (E))
2593 /= Is_True (Static_Boolean (Expr))
2594 then
2595 Error_Msg_N ("specification of inherited aspect% can only " &
2596 "confirm parent value", Id);
2597 end if;
2598 end Analyze_Aspect_Yield;
2599
2600 -----------------------
2601 -- Make_Aitem_Pragma --
2602 -----------------------
2603
2604 procedure Make_Aitem_Pragma
2605 (Pragma_Argument_Associations : List_Id;
2606 Pragma_Name : Name_Id)
2607 is
2608 Args : List_Id := Pragma_Argument_Associations;
2609
2610 begin
2611 -- We should never get here if aspect was disabled
2612
2613 pragma Assert (not Is_Disabled (Aspect));
2614
2615 -- Certain aspects allow for an optional name or expression. Do
2616 -- not generate a pragma with empty argument association list.
2617
2618 if No (Args) or else No (Expression (First (Args))) then
2619 Args := No_List;
2620 end if;
2621
2622 -- Build the pragma
2623
2624 Aitem :=
2625 Make_Pragma (Loc,
2626 Pragma_Argument_Associations => Args,
2627 Pragma_Identifier =>
2628 Make_Identifier (Sloc (Id), Pragma_Name),
2629 Class_Present => Class_Present (Aspect),
2630 Split_PPC => Split_PPC (Aspect));
2631
2632 -- Set additional semantic fields
2633
2634 if Is_Ignored (Aspect) then
2635 Set_Is_Ignored (Aitem);
2636 elsif Is_Checked (Aspect) then
2637 Set_Is_Checked (Aitem);
2638 end if;
2639
2640 Set_Corresponding_Aspect (Aitem, Aspect);
2641 Set_From_Aspect_Specification (Aitem);
2642 end Make_Aitem_Pragma;
2643
2644 -- Start of processing for Analyze_One_Aspect
2645
2646 begin
2647 -- Skip aspect if already analyzed, to avoid looping in some cases
2648
2649 if Analyzed (Aspect) then
2650 goto Continue;
2651 end if;
2652
2653 -- Skip looking at aspect if it is totally disabled. Just mark it
2654 -- as such for later reference in the tree. This also sets the
2655 -- Is_Ignored and Is_Checked flags appropriately.
2656
2657 Check_Applicable_Policy (Aspect);
2658
2659 if Is_Disabled (Aspect) then
2660 goto Continue;
2661 end if;
2662
2663 -- Set the source location of expression, used in the case of
2664 -- a failed precondition/postcondition or invariant. Note that
2665 -- the source location of the expression is not usually the best
2666 -- choice here. For example, it gets located on the last AND
2667 -- keyword in a chain of boolean expressiond AND'ed together.
2668 -- It is best to put the message on the first character of the
2669 -- assertion, which is the effect of the First_Node call here.
2670
2671 if Present (Expr) then
2672 Eloc := Sloc (First_Node (Expr));
2673 end if;
2674
2675 -- Check restriction No_Implementation_Aspect_Specifications
2676
2677 if Implementation_Defined_Aspect (A_Id) then
2678 Check_Restriction
2679 (No_Implementation_Aspect_Specifications, Aspect);
2680 end if;
2681
2682 -- Check restriction No_Specification_Of_Aspect
2683
2684 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2685
2686 -- Mark aspect analyzed (actual analysis is delayed till later)
2687
2688 Set_Analyzed (Aspect);
2689 Set_Entity (Aspect, E);
2690
2691 -- Build the reference to E that will be used in the built pragmas
2692
2693 Ent := New_Occurrence_Of (E, Sloc (Id));
2694
2695 if A_Id = Aspect_Attach_Handler
2696 or else A_Id = Aspect_Interrupt_Handler
2697 then
2698
2699 -- Treat the specification as a reference to the protected
2700 -- operation, which might otherwise appear unreferenced and
2701 -- generate spurious warnings.
2702
2703 Generate_Reference (E, Id);
2704 end if;
2705
2706 -- Check for duplicate aspect. Note that the Comes_From_Source
2707 -- test allows duplicate Pre/Post's that we generate internally
2708 -- to escape being flagged here.
2709
2710 if No_Duplicates_Allowed (A_Id) then
2711 Anod := First (L);
2712 while Anod /= Aspect loop
2713 if Comes_From_Source (Aspect)
2714 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
2715 then
2716 Error_Msg_Name_1 := Nam;
2717 Error_Msg_Sloc := Sloc (Anod);
2718
2719 -- Case of same aspect specified twice
2720
2721 if Class_Present (Anod) = Class_Present (Aspect) then
2722 if not Class_Present (Anod) then
2723 Error_Msg_NE
2724 ("aspect% for & previously given#",
2725 Id, E);
2726 else
2727 Error_Msg_NE
2728 ("aspect `%''Class` for & previously given#",
2729 Id, E);
2730 end if;
2731 end if;
2732 end if;
2733
2734 Next (Anod);
2735 end loop;
2736 end if;
2737
2738 -- Check some general restrictions on language defined aspects
2739
2740 if not Implementation_Defined_Aspect (A_Id)
2741 or else A_Id = Aspect_Async_Readers
2742 or else A_Id = Aspect_Async_Writers
2743 or else A_Id = Aspect_Effective_Reads
2744 or else A_Id = Aspect_Effective_Reads
2745 then
2746 Error_Msg_Name_1 := Nam;
2747
2748 -- Not allowed for renaming declarations. Examine the original
2749 -- node because a subprogram renaming may have been rewritten
2750 -- as a body.
2751
2752 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
2753 Error_Msg_N
2754 ("aspect % not allowed for renaming declaration",
2755 Aspect);
2756 end if;
2757
2758 -- Not allowed for formal type declarations in previous
2759 -- versions of the language. Allowed for them only for
2760 -- shared variable control aspects.
2761
2762 if Nkind (N) = N_Formal_Type_Declaration then
2763 if Ada_Version < Ada_2020 then
2764 Error_Msg_N
2765 ("aspect % not allowed for formal type declaration",
2766 Aspect);
2767
2768 elsif A_Id /= Aspect_Atomic
2769 and then A_Id /= Aspect_Volatile
2770 and then A_Id /= Aspect_Independent
2771 and then A_Id /= Aspect_Atomic_Components
2772 and then A_Id /= Aspect_Independent_Components
2773 and then A_Id /= Aspect_Volatile_Components
2774 and then A_Id /= Aspect_Async_Readers
2775 and then A_Id /= Aspect_Async_Writers
2776 and then A_Id /= Aspect_Effective_Reads
2777 and then A_Id /= Aspect_Effective_Reads
2778 then
2779 Error_Msg_N
2780 ("aspect % not allowed for formal type declaration",
2781 Aspect);
2782 end if;
2783 end if;
2784 end if;
2785
2786 -- Copy expression for later processing by the procedures
2787 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2788
2789 Set_Entity (Id, New_Copy_Tree (Expr));
2790
2791 -- Set Delay_Required as appropriate to aspect
2792
2793 case Aspect_Delay (A_Id) is
2794 when Always_Delay =>
2795 Delay_Required := True;
2796
2797 when Never_Delay =>
2798 Delay_Required := False;
2799
2800 when Rep_Aspect =>
2801
2802 -- If expression has the form of an integer literal, then
2803 -- do not delay, since we know the value cannot change.
2804 -- This optimization catches most rep clause cases.
2805
2806 -- For Boolean aspects, don't delay if no expression
2807
2808 if A_Id in Boolean_Aspects and then No (Expr) then
2809 Delay_Required := False;
2810
2811 -- For non-Boolean aspects, don't delay if integer literal,
2812 -- unless the aspect is Alignment, which affects the
2813 -- freezing of an initialized object.
2814
2815 elsif A_Id not in Boolean_Aspects
2816 and then A_Id /= Aspect_Alignment
2817 and then Present (Expr)
2818 and then Nkind (Expr) = N_Integer_Literal
2819 then
2820 Delay_Required := False;
2821
2822 -- All other cases are delayed
2823
2824 else
2825 Delay_Required := True;
2826 Set_Has_Delayed_Rep_Aspects (E);
2827 end if;
2828 end case;
2829
2830 -- Check 13.1(9.2/5): A representation aspect of a subtype or type
2831 -- shall not be specified (whether by a representation item or an
2832 -- aspect_specification) before the type is completely defined
2833 -- (see 3.11.1).
2834
2835 if Is_Representation_Aspect (A_Id)
2836 and then Rep_Item_Too_Early (E, N)
2837 then
2838 goto Continue;
2839 end if;
2840
2841 -- Processing based on specific aspect
2842
2843 case A_Id is
2844 when Aspect_Unimplemented =>
2845 null; -- ??? temp for now
2846
2847 -- No_Aspect should be impossible
2848
2849 when No_Aspect =>
2850 raise Program_Error;
2851
2852 -- Case 1: Aspects corresponding to attribute definition
2853 -- clauses.
2854
2855 when Aspect_Address
2856 | Aspect_Alignment
2857 | Aspect_Bit_Order
2858 | Aspect_Component_Size
2859 | Aspect_Constant_Indexing
2860 | Aspect_Default_Iterator
2861 | Aspect_Dispatching_Domain
2862 | Aspect_External_Tag
2863 | Aspect_Input
2864 | Aspect_Iterable
2865 | Aspect_Iterator_Element
2866 | Aspect_Machine_Radix
2867 | Aspect_Object_Size
2868 | Aspect_Output
2869 | Aspect_Put_Image
2870 | Aspect_Read
2871 | Aspect_Scalar_Storage_Order
2872 | Aspect_Simple_Storage_Pool
2873 | Aspect_Size
2874 | Aspect_Small
2875 | Aspect_Storage_Pool
2876 | Aspect_Stream_Size
2877 | Aspect_Value_Size
2878 | Aspect_Variable_Indexing
2879 | Aspect_Write
2880 =>
2881 -- Indexing aspects apply only to tagged type
2882
2883 if (A_Id = Aspect_Constant_Indexing
2884 or else
2885 A_Id = Aspect_Variable_Indexing)
2886 and then not (Is_Type (E)
2887 and then Is_Tagged_Type (E))
2888 then
2889 Error_Msg_N
2890 ("indexing aspect can only apply to a tagged type",
2891 Aspect);
2892 goto Continue;
2893 end if;
2894
2895 -- For the case of aspect Address, we don't consider that we
2896 -- know the entity is never set in the source, since it is
2897 -- is likely aliasing is occurring.
2898
2899 -- Note: one might think that the analysis of the resulting
2900 -- attribute definition clause would take care of that, but
2901 -- that's not the case since it won't be from source.
2902
2903 if A_Id = Aspect_Address then
2904 Set_Never_Set_In_Source (E, False);
2905 end if;
2906
2907 -- Correctness of the profile of a stream operation is
2908 -- verified at the freeze point, but we must detect the
2909 -- illegal specification of this aspect for a subtype now,
2910 -- to prevent malformed rep_item chains.
2911
2912 if A_Id = Aspect_Input or else
2913 A_Id = Aspect_Output or else
2914 A_Id = Aspect_Read or else
2915 A_Id = Aspect_Write
2916 then
2917 if not Is_First_Subtype (E) then
2918 Error_Msg_N
2919 ("local name must be a first subtype", Aspect);
2920 goto Continue;
2921
2922 -- If stream aspect applies to the class-wide type,
2923 -- the generated attribute definition applies to the
2924 -- class-wide type as well.
2925
2926 elsif Class_Present (Aspect) then
2927 Ent :=
2928 Make_Attribute_Reference (Loc,
2929 Prefix => Ent,
2930 Attribute_Name => Name_Class);
2931 end if;
2932 end if;
2933
2934 -- Construct the attribute_definition_clause. The expression
2935 -- in the aspect specification is simply shared with the
2936 -- constructed attribute, because it will be fully analyzed
2937 -- when the attribute is processed.
2938
2939 Aitem :=
2940 Make_Attribute_Definition_Clause (Loc,
2941 Name => Ent,
2942 Chars => Chars (Id),
2943 Expression => Relocate_Node (Expr));
2944
2945 -- If the address is specified, then we treat the entity as
2946 -- referenced, to avoid spurious warnings. This is analogous
2947 -- to what is done with an attribute definition clause, but
2948 -- here we don't want to generate a reference because this
2949 -- is the point of definition of the entity.
2950
2951 if A_Id = Aspect_Address then
2952 Set_Referenced (E);
2953 end if;
2954
2955 -- Case 2: Aspects corresponding to pragmas
2956
2957 -- Case 2a: Aspects corresponding to pragmas with two
2958 -- arguments, where the first argument is a local name
2959 -- referring to the entity, and the second argument is the
2960 -- aspect definition expression.
2961
2962 -- Linker_Section/Suppress/Unsuppress
2963
2964 when Aspect_Linker_Section
2965 | Aspect_Suppress
2966 | Aspect_Unsuppress
2967 =>
2968 Make_Aitem_Pragma
2969 (Pragma_Argument_Associations => New_List (
2970 Make_Pragma_Argument_Association (Loc,
2971 Expression => New_Occurrence_Of (E, Loc)),
2972 Make_Pragma_Argument_Association (Sloc (Expr),
2973 Expression => Relocate_Node (Expr))),
2974 Pragma_Name => Chars (Id));
2975
2976 -- Linker_Section does not need delaying, as its argument
2977 -- must be a static string. Furthermore, if applied to
2978 -- an object with an explicit initialization, the object
2979 -- must be frozen in order to elaborate the initialization
2980 -- code. (This is already done for types with implicit
2981 -- initialization, such as protected types.)
2982
2983 if A_Id = Aspect_Linker_Section
2984 and then Nkind (N) = N_Object_Declaration
2985 and then Has_Init_Expression (N)
2986 then
2987 Delay_Required := False;
2988 end if;
2989
2990 -- Synchronization
2991
2992 -- Corresponds to pragma Implemented, construct the pragma
2993
2994 when Aspect_Synchronization =>
2995 Make_Aitem_Pragma
2996 (Pragma_Argument_Associations => New_List (
2997 Make_Pragma_Argument_Association (Loc,
2998 Expression => New_Occurrence_Of (E, Loc)),
2999 Make_Pragma_Argument_Association (Sloc (Expr),
3000 Expression => Relocate_Node (Expr))),
3001 Pragma_Name => Name_Implemented);
3002
3003 -- Attach_Handler
3004
3005 when Aspect_Attach_Handler =>
3006 Make_Aitem_Pragma
3007 (Pragma_Argument_Associations => New_List (
3008 Make_Pragma_Argument_Association (Sloc (Ent),
3009 Expression => Ent),
3010 Make_Pragma_Argument_Association (Sloc (Expr),
3011 Expression => Relocate_Node (Expr))),
3012 Pragma_Name => Name_Attach_Handler);
3013
3014 -- We need to insert this pragma into the tree to get proper
3015 -- processing and to look valid from a placement viewpoint.
3016
3017 Insert_Pragma (Aitem);
3018 goto Continue;
3019
3020 -- Dynamic_Predicate, Predicate, Static_Predicate
3021
3022 when Aspect_Dynamic_Predicate
3023 | Aspect_Predicate
3024 | Aspect_Static_Predicate
3025 =>
3026 -- These aspects apply only to subtypes
3027
3028 if not Is_Type (E) then
3029 Error_Msg_N
3030 ("predicate can only be specified for a subtype",
3031 Aspect);
3032 goto Continue;
3033
3034 elsif Is_Incomplete_Type (E) then
3035 Error_Msg_N
3036 ("predicate cannot apply to incomplete view", Aspect);
3037
3038 elsif Is_Generic_Type (E) then
3039 Error_Msg_N
3040 ("predicate cannot apply to formal type", Aspect);
3041 goto Continue;
3042 end if;
3043
3044 -- Construct the pragma (always a pragma Predicate, with
3045 -- flags recording whether it is static/dynamic). We also
3046 -- set flags recording this in the type itself.
3047
3048 Make_Aitem_Pragma
3049 (Pragma_Argument_Associations => New_List (
3050 Make_Pragma_Argument_Association (Sloc (Ent),
3051 Expression => Ent),
3052 Make_Pragma_Argument_Association (Sloc (Expr),
3053 Expression => Relocate_Node (Expr))),
3054 Pragma_Name => Name_Predicate);
3055
3056 -- Mark type has predicates, and remember what kind of
3057 -- aspect lead to this predicate (we need this to access
3058 -- the right set of check policies later on).
3059
3060 Set_Has_Predicates (E);
3061
3062 if A_Id = Aspect_Dynamic_Predicate then
3063 Set_Has_Dynamic_Predicate_Aspect (E);
3064
3065 -- If the entity has a dynamic predicate, any inherited
3066 -- static predicate becomes dynamic as well, and the
3067 -- predicate function includes the conjunction of both.
3068
3069 Set_Has_Static_Predicate_Aspect (E, False);
3070
3071 elsif A_Id = Aspect_Static_Predicate then
3072 Set_Has_Static_Predicate_Aspect (E);
3073 end if;
3074
3075 -- If the type is private, indicate that its completion
3076 -- has a freeze node, because that is the one that will
3077 -- be visible at freeze time.
3078
3079 if Is_Private_Type (E) and then Present (Full_View (E)) then
3080 Set_Has_Predicates (Full_View (E));
3081
3082 if A_Id = Aspect_Dynamic_Predicate then
3083 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
3084 elsif A_Id = Aspect_Static_Predicate then
3085 Set_Has_Static_Predicate_Aspect (Full_View (E));
3086 end if;
3087
3088 Set_Has_Delayed_Aspects (Full_View (E));
3089 Ensure_Freeze_Node (Full_View (E));
3090
3091 -- If there is an Underlying_Full_View, also create a
3092 -- freeze node for that one.
3093
3094 if Is_Private_Type (Full_View (E)) then
3095 declare
3096 U_Full : constant Entity_Id :=
3097 Underlying_Full_View (Full_View (E));
3098 begin
3099 if Present (U_Full) then
3100 Set_Has_Delayed_Aspects (U_Full);
3101 Ensure_Freeze_Node (U_Full);
3102 end if;
3103 end;
3104 end if;
3105 end if;
3106
3107 -- Predicate_Failure
3108
3109 when Aspect_Predicate_Failure =>
3110
3111 -- This aspect applies only to subtypes
3112
3113 if not Is_Type (E) then
3114 Error_Msg_N
3115 ("predicate can only be specified for a subtype",
3116 Aspect);
3117 goto Continue;
3118
3119 elsif Is_Incomplete_Type (E) then
3120 Error_Msg_N
3121 ("predicate cannot apply to incomplete view", Aspect);
3122 goto Continue;
3123
3124 elsif not Has_Predicates (E) then
3125 Error_Msg_N
3126 ("Predicate_Failure requires previous predicate" &
3127 " specification", Aspect);
3128 goto Continue;
3129 end if;
3130
3131 -- Construct the pragma
3132
3133 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_Predicate_Failure);
3140
3141 -- Case 2b: Aspects corresponding to pragmas with two
3142 -- arguments, where the second argument is a local name
3143 -- referring to the entity, and the first argument is the
3144 -- aspect definition expression.
3145
3146 -- Convention
3147
3148 when Aspect_Convention =>
3149 Analyze_Aspect_Convention;
3150 goto Continue;
3151
3152 -- External_Name, Link_Name
3153
3154 when Aspect_External_Name
3155 | Aspect_Link_Name
3156 =>
3157 Analyze_Aspect_External_Link_Name;
3158 goto Continue;
3159
3160 -- CPU, Interrupt_Priority, Priority
3161
3162 -- These three aspects can be specified for a subprogram spec
3163 -- or body, in which case we analyze the expression and export
3164 -- the value of the aspect.
3165
3166 -- Previously, we generated an equivalent pragma for bodies
3167 -- (note that the specs cannot contain these pragmas). The
3168 -- pragma was inserted ahead of local declarations, rather than
3169 -- after the body. This leads to a certain duplication between
3170 -- the processing performed for the aspect and the pragma, but
3171 -- given the straightforward handling required it is simpler
3172 -- to duplicate than to translate the aspect in the spec into
3173 -- a pragma in the declarative part of the body.
3174
3175 when Aspect_CPU
3176 | Aspect_Interrupt_Priority
3177 | Aspect_Priority
3178 =>
3179 if Nkind_In (N, N_Subprogram_Body,
3180 N_Subprogram_Declaration)
3181 then
3182 -- Analyze the aspect expression
3183
3184 Analyze_And_Resolve (Expr, Standard_Integer);
3185
3186 -- Interrupt_Priority aspect not allowed for main
3187 -- subprograms. RM D.1 does not forbid this explicitly,
3188 -- but RM J.15.11(6/3) does not permit pragma
3189 -- Interrupt_Priority for subprograms.
3190
3191 if A_Id = Aspect_Interrupt_Priority then
3192 Error_Msg_N
3193 ("Interrupt_Priority aspect cannot apply to "
3194 & "subprogram", Expr);
3195
3196 -- The expression must be static
3197
3198 elsif not Is_OK_Static_Expression (Expr) then
3199 Flag_Non_Static_Expr
3200 ("aspect requires static expression!", Expr);
3201
3202 -- Check whether this is the main subprogram. Issue a
3203 -- warning only if it is obviously not a main program
3204 -- (when it has parameters or when the subprogram is
3205 -- within a package).
3206
3207 elsif Present (Parameter_Specifications
3208 (Specification (N)))
3209 or else not Is_Compilation_Unit (Defining_Entity (N))
3210 then
3211 -- See RM D.1(14/3) and D.16(12/3)
3212
3213 Error_Msg_N
3214 ("aspect applied to subprogram other than the "
3215 & "main subprogram has no effect??", Expr);
3216
3217 -- Otherwise check in range and export the value
3218
3219 -- For the CPU aspect
3220
3221 elsif A_Id = Aspect_CPU then
3222 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
3223
3224 -- Value is correct so we export the value to make
3225 -- it available at execution time.
3226
3227 Set_Main_CPU
3228 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3229
3230 else
3231 Error_Msg_N
3232 ("main subprogram CPU is out of range", Expr);
3233 end if;
3234
3235 -- For the Priority aspect
3236
3237 elsif A_Id = Aspect_Priority then
3238 if Is_In_Range (Expr, RTE (RE_Priority)) then
3239
3240 -- Value is correct so we export the value to make
3241 -- it available at execution time.
3242
3243 Set_Main_Priority
3244 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
3245
3246 -- Ignore pragma if Relaxed_RM_Semantics to support
3247 -- other targets/non GNAT compilers.
3248
3249 elsif not Relaxed_RM_Semantics then
3250 Error_Msg_N
3251 ("main subprogram priority is out of range",
3252 Expr);
3253 end if;
3254 end if;
3255
3256 -- Load an arbitrary entity from System.Tasking.Stages
3257 -- or System.Tasking.Restricted.Stages (depending on
3258 -- the supported profile) to make sure that one of these
3259 -- packages is implicitly with'ed, since we need to have
3260 -- the tasking run time active for the pragma Priority to
3261 -- have any effect. Previously we with'ed the package
3262 -- System.Tasking, but this package does not trigger the
3263 -- required initialization of the run-time library.
3264
3265 declare
3266 Discard : Entity_Id;
3267 begin
3268 if Restricted_Profile then
3269 Discard := RTE (RE_Activate_Restricted_Tasks);
3270 else
3271 Discard := RTE (RE_Activate_Tasks);
3272 end if;
3273 end;
3274
3275 -- Handling for these aspects in subprograms is complete
3276
3277 goto Continue;
3278
3279 -- For task and protected types pass the aspect as an
3280 -- attribute.
3281
3282 else
3283 Aitem :=
3284 Make_Attribute_Definition_Clause (Loc,
3285 Name => Ent,
3286 Chars => Chars (Id),
3287 Expression => Relocate_Node (Expr));
3288 end if;
3289
3290 -- Warnings
3291
3292 when Aspect_Warnings =>
3293 Make_Aitem_Pragma
3294 (Pragma_Argument_Associations => New_List (
3295 Make_Pragma_Argument_Association (Sloc (Expr),
3296 Expression => Relocate_Node (Expr)),
3297 Make_Pragma_Argument_Association (Loc,
3298 Expression => New_Occurrence_Of (E, Loc))),
3299 Pragma_Name => Chars (Id));
3300
3301 Decorate (Aspect, Aitem);
3302 Insert_Pragma (Aitem);
3303 goto Continue;
3304
3305 -- Case 2c: Aspects corresponding to pragmas with three
3306 -- arguments.
3307
3308 -- Invariant aspects have a first argument that references the
3309 -- entity, a second argument that is the expression and a third
3310 -- argument that is an appropriate message.
3311
3312 -- Invariant, Type_Invariant
3313
3314 when Aspect_Invariant
3315 | Aspect_Type_Invariant
3316 =>
3317 -- Analysis of the pragma will verify placement legality:
3318 -- an invariant must apply to a private type, or appear in
3319 -- the private part of a spec and apply to a completion.
3320
3321 Make_Aitem_Pragma
3322 (Pragma_Argument_Associations => New_List (
3323 Make_Pragma_Argument_Association (Sloc (Ent),
3324 Expression => Ent),
3325 Make_Pragma_Argument_Association (Sloc (Expr),
3326 Expression => Relocate_Node (Expr))),
3327 Pragma_Name => Name_Invariant);
3328
3329 -- Add message unless exception messages are suppressed
3330
3331 if not Opt.Exception_Locations_Suppressed then
3332 Append_To (Pragma_Argument_Associations (Aitem),
3333 Make_Pragma_Argument_Association (Eloc,
3334 Chars => Name_Message,
3335 Expression =>
3336 Make_String_Literal (Eloc,
3337 Strval => "failed invariant from "
3338 & Build_Location_String (Eloc))));
3339 end if;
3340
3341 -- For Invariant case, insert immediately after the entity
3342 -- declaration. We do not have to worry about delay issues
3343 -- since the pragma processing takes care of this.
3344
3345 Delay_Required := False;
3346
3347 -- Case 2d : Aspects that correspond to a pragma with one
3348 -- argument.
3349
3350 -- Abstract_State
3351
3352 -- Aspect Abstract_State introduces implicit declarations for
3353 -- all state abstraction entities it defines. To emulate this
3354 -- behavior, insert the pragma at the beginning of the visible
3355 -- declarations of the related package so that it is analyzed
3356 -- immediately.
3357
3358 when Aspect_Abstract_State => Abstract_State : declare
3359 Context : Node_Id := N;
3360
3361 begin
3362 -- When aspect Abstract_State appears on a generic package,
3363 -- it is propageted to the package instance. The context in
3364 -- this case is the instance spec.
3365
3366 if Nkind (Context) = N_Package_Instantiation then
3367 Context := Instance_Spec (Context);
3368 end if;
3369
3370 if Nkind_In (Context, N_Generic_Package_Declaration,
3371 N_Package_Declaration)
3372 then
3373 Make_Aitem_Pragma
3374 (Pragma_Argument_Associations => New_List (
3375 Make_Pragma_Argument_Association (Loc,
3376 Expression => Relocate_Node (Expr))),
3377 Pragma_Name => Name_Abstract_State);
3378
3379 Decorate (Aspect, Aitem);
3380 Insert_Pragma
3381 (Prag => Aitem,
3382 Is_Instance =>
3383 Is_Generic_Instance (Defining_Entity (Context)));
3384
3385 else
3386 Error_Msg_NE
3387 ("aspect & must apply to a package declaration",
3388 Aspect, Id);
3389 end if;
3390
3391 goto Continue;
3392 end Abstract_State;
3393
3394 -- Aspect Async_Readers is never delayed because it is
3395 -- equivalent to a source pragma which appears after the
3396 -- related object declaration.
3397
3398 when Aspect_Async_Readers =>
3399 Make_Aitem_Pragma
3400 (Pragma_Argument_Associations => New_List (
3401 Make_Pragma_Argument_Association (Loc,
3402 Expression => Relocate_Node (Expr))),
3403 Pragma_Name => Name_Async_Readers);
3404
3405 Decorate (Aspect, Aitem);
3406 Insert_Pragma (Aitem);
3407 goto Continue;
3408
3409 -- Aspect Async_Writers is never delayed because it is
3410 -- equivalent to a source pragma which appears after the
3411 -- related object declaration.
3412
3413 when Aspect_Async_Writers =>
3414 Make_Aitem_Pragma
3415 (Pragma_Argument_Associations => New_List (
3416 Make_Pragma_Argument_Association (Loc,
3417 Expression => Relocate_Node (Expr))),
3418 Pragma_Name => Name_Async_Writers);
3419
3420 Decorate (Aspect, Aitem);
3421 Insert_Pragma (Aitem);
3422 goto Continue;
3423
3424 -- Aspect Constant_After_Elaboration is never delayed because
3425 -- it is equivalent to a source pragma which appears after the
3426 -- related object declaration.
3427
3428 when Aspect_Constant_After_Elaboration =>
3429 Make_Aitem_Pragma
3430 (Pragma_Argument_Associations => New_List (
3431 Make_Pragma_Argument_Association (Loc,
3432 Expression => Relocate_Node (Expr))),
3433 Pragma_Name =>
3434 Name_Constant_After_Elaboration);
3435
3436 Decorate (Aspect, Aitem);
3437 Insert_Pragma (Aitem);
3438 goto Continue;
3439
3440 -- Aspect Default_Internal_Condition is never delayed because
3441 -- it is equivalent to a source pragma which appears after the
3442 -- related private type. To deal with forward references, the
3443 -- generated pragma is stored in the rep chain of the related
3444 -- private type as types do not carry contracts. The pragma is
3445 -- wrapped inside of a procedure at the freeze point of the
3446 -- private type's full view.
3447
3448 when Aspect_Default_Initial_Condition =>
3449 Make_Aitem_Pragma
3450 (Pragma_Argument_Associations => New_List (
3451 Make_Pragma_Argument_Association (Loc,
3452 Expression => Relocate_Node (Expr))),
3453 Pragma_Name =>
3454 Name_Default_Initial_Condition);
3455
3456 Decorate (Aspect, Aitem);
3457 Insert_Pragma (Aitem);
3458 goto Continue;
3459
3460 -- Default_Storage_Pool
3461
3462 when Aspect_Default_Storage_Pool =>
3463 Make_Aitem_Pragma
3464 (Pragma_Argument_Associations => New_List (
3465 Make_Pragma_Argument_Association (Loc,
3466 Expression => Relocate_Node (Expr))),
3467 Pragma_Name =>
3468 Name_Default_Storage_Pool);
3469
3470 Decorate (Aspect, Aitem);
3471 Insert_Pragma (Aitem);
3472 goto Continue;
3473
3474 -- Depends
3475
3476 -- Aspect Depends is never delayed because it is equivalent to
3477 -- a source pragma which appears after the related subprogram.
3478 -- To deal with forward references, the generated pragma is
3479 -- stored in the contract of the related subprogram and later
3480 -- analyzed at the end of the declarative region. See routine
3481 -- Analyze_Depends_In_Decl_Part for details.
3482
3483 when Aspect_Depends =>
3484 Make_Aitem_Pragma
3485 (Pragma_Argument_Associations => New_List (
3486 Make_Pragma_Argument_Association (Loc,
3487 Expression => Relocate_Node (Expr))),
3488 Pragma_Name => Name_Depends);
3489
3490 Decorate (Aspect, Aitem);
3491 Insert_Pragma (Aitem);
3492 goto Continue;
3493
3494 -- Aspect Effective_Reads is never delayed because it is
3495 -- equivalent to a source pragma which appears after the
3496 -- related object declaration.
3497
3498 when Aspect_Effective_Reads =>
3499 Make_Aitem_Pragma
3500 (Pragma_Argument_Associations => New_List (
3501 Make_Pragma_Argument_Association (Loc,
3502 Expression => Relocate_Node (Expr))),
3503 Pragma_Name => Name_Effective_Reads);
3504
3505 Decorate (Aspect, Aitem);
3506 Insert_Pragma (Aitem);
3507 goto Continue;
3508
3509 -- Aspect Effective_Writes is never delayed because it is
3510 -- equivalent to a source pragma which appears after the
3511 -- related object declaration.
3512
3513 when Aspect_Effective_Writes =>
3514 Make_Aitem_Pragma
3515 (Pragma_Argument_Associations => New_List (
3516 Make_Pragma_Argument_Association (Loc,
3517 Expression => Relocate_Node (Expr))),
3518 Pragma_Name => Name_Effective_Writes);
3519
3520 Decorate (Aspect, Aitem);
3521 Insert_Pragma (Aitem);
3522 goto Continue;
3523
3524 -- Aspect Extensions_Visible is never delayed because it is
3525 -- equivalent to a source pragma which appears after the
3526 -- related subprogram.
3527
3528 when Aspect_Extensions_Visible =>
3529 Make_Aitem_Pragma
3530 (Pragma_Argument_Associations => New_List (
3531 Make_Pragma_Argument_Association (Loc,
3532 Expression => Relocate_Node (Expr))),
3533 Pragma_Name => Name_Extensions_Visible);
3534
3535 Decorate (Aspect, Aitem);
3536 Insert_Pragma (Aitem);
3537 goto Continue;
3538
3539 -- Aspect Ghost is never delayed because it is equivalent to a
3540 -- source pragma which appears at the top of [generic] package
3541 -- declarations or after an object, a [generic] subprogram, or
3542 -- a type declaration.
3543
3544 when Aspect_Ghost =>
3545 Make_Aitem_Pragma
3546 (Pragma_Argument_Associations => New_List (
3547 Make_Pragma_Argument_Association (Loc,
3548 Expression => Relocate_Node (Expr))),
3549 Pragma_Name => Name_Ghost);
3550
3551 Decorate (Aspect, Aitem);
3552 Insert_Pragma (Aitem);
3553 goto Continue;
3554
3555 -- Global
3556
3557 -- Aspect Global is never delayed because it is equivalent to
3558 -- a source pragma which appears after the related subprogram.
3559 -- To deal with forward references, the generated pragma is
3560 -- stored in the contract of the related subprogram and later
3561 -- analyzed at the end of the declarative region. See routine
3562 -- Analyze_Global_In_Decl_Part for details.
3563
3564 when Aspect_Global =>
3565 Make_Aitem_Pragma
3566 (Pragma_Argument_Associations => New_List (
3567 Make_Pragma_Argument_Association (Loc,
3568 Expression => Relocate_Node (Expr))),
3569 Pragma_Name => Name_Global);
3570
3571 Decorate (Aspect, Aitem);
3572 Insert_Pragma (Aitem);
3573 goto Continue;
3574
3575 -- Initial_Condition
3576
3577 -- Aspect Initial_Condition is never delayed because it is
3578 -- equivalent to a source pragma which appears after the
3579 -- related package. To deal with forward references, the
3580 -- generated pragma is stored in the contract of the related
3581 -- package and later analyzed at the end of the declarative
3582 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
3583 -- for details.
3584
3585 when Aspect_Initial_Condition => Initial_Condition : declare
3586 Context : Node_Id := N;
3587
3588 begin
3589 -- When aspect Initial_Condition appears on a generic
3590 -- package, it is propageted to the package instance. The
3591 -- context in this case is the instance spec.
3592
3593 if Nkind (Context) = N_Package_Instantiation then
3594 Context := Instance_Spec (Context);
3595 end if;
3596
3597 if Nkind_In (Context, N_Generic_Package_Declaration,
3598 N_Package_Declaration)
3599 then
3600 Make_Aitem_Pragma
3601 (Pragma_Argument_Associations => New_List (
3602 Make_Pragma_Argument_Association (Loc,
3603 Expression => Relocate_Node (Expr))),
3604 Pragma_Name =>
3605 Name_Initial_Condition);
3606
3607 Decorate (Aspect, Aitem);
3608 Insert_Pragma
3609 (Prag => Aitem,
3610 Is_Instance =>
3611 Is_Generic_Instance (Defining_Entity (Context)));
3612
3613 -- Otherwise the context is illegal
3614
3615 else
3616 Error_Msg_NE
3617 ("aspect & must apply to a package declaration",
3618 Aspect, Id);
3619 end if;
3620
3621 goto Continue;
3622 end Initial_Condition;
3623
3624 -- Initializes
3625
3626 -- Aspect Initializes is never delayed because it is equivalent
3627 -- to a source pragma appearing after the related package. To
3628 -- deal with forward references, the generated pragma is stored
3629 -- in the contract of the related package and later analyzed at
3630 -- the end of the declarative region. For details, see routine
3631 -- Analyze_Initializes_In_Decl_Part.
3632
3633 when Aspect_Initializes => Initializes : declare
3634 Context : Node_Id := N;
3635
3636 begin
3637 -- When aspect Initializes appears on a generic package,
3638 -- it is propageted to the package instance. The context
3639 -- in this case is the instance spec.
3640
3641 if Nkind (Context) = N_Package_Instantiation then
3642 Context := Instance_Spec (Context);
3643 end if;
3644
3645 if Nkind_In (Context, N_Generic_Package_Declaration,
3646 N_Package_Declaration)
3647 then
3648 Make_Aitem_Pragma
3649 (Pragma_Argument_Associations => New_List (
3650 Make_Pragma_Argument_Association (Loc,
3651 Expression => Relocate_Node (Expr))),
3652 Pragma_Name => Name_Initializes);
3653
3654 Decorate (Aspect, Aitem);
3655 Insert_Pragma
3656 (Prag => Aitem,
3657 Is_Instance =>
3658 Is_Generic_Instance (Defining_Entity (Context)));
3659
3660 -- Otherwise the context is illegal
3661
3662 else
3663 Error_Msg_NE
3664 ("aspect & must apply to a package declaration",
3665 Aspect, Id);
3666 end if;
3667
3668 goto Continue;
3669 end Initializes;
3670
3671 -- Max_Entry_Queue_Depth
3672
3673 when Aspect_Max_Entry_Queue_Depth =>
3674 Make_Aitem_Pragma
3675 (Pragma_Argument_Associations => New_List (
3676 Make_Pragma_Argument_Association (Loc,
3677 Expression => Relocate_Node (Expr))),
3678 Pragma_Name => Name_Max_Entry_Queue_Depth);
3679
3680 Decorate (Aspect, Aitem);
3681 Insert_Pragma (Aitem);
3682 goto Continue;
3683
3684 -- Max_Entry_Queue_Length
3685
3686 when Aspect_Max_Entry_Queue_Length =>
3687 Make_Aitem_Pragma
3688 (Pragma_Argument_Associations => New_List (
3689 Make_Pragma_Argument_Association (Loc,
3690 Expression => Relocate_Node (Expr))),
3691 Pragma_Name => Name_Max_Entry_Queue_Length);
3692
3693 Decorate (Aspect, Aitem);
3694 Insert_Pragma (Aitem);
3695 goto Continue;
3696
3697 -- Max_Queue_Length
3698
3699 when Aspect_Max_Queue_Length =>
3700 Make_Aitem_Pragma
3701 (Pragma_Argument_Associations => New_List (
3702 Make_Pragma_Argument_Association (Loc,
3703 Expression => Relocate_Node (Expr))),
3704 Pragma_Name => Name_Max_Queue_Length);
3705
3706 Decorate (Aspect, Aitem);
3707 Insert_Pragma (Aitem);
3708 goto Continue;
3709
3710 -- Aspect No_Caching is never delayed because it is equivalent
3711 -- to a source pragma which appears after the related object
3712 -- declaration.
3713
3714 when Aspect_No_Caching =>
3715 Make_Aitem_Pragma
3716 (Pragma_Argument_Associations => New_List (
3717 Make_Pragma_Argument_Association (Loc,
3718 Expression => Relocate_Node (Expr))),
3719 Pragma_Name => Name_No_Caching);
3720
3721 Decorate (Aspect, Aitem);
3722 Insert_Pragma (Aitem);
3723 goto Continue;
3724
3725 -- Obsolescent
3726
3727 when Aspect_Obsolescent => declare
3728 Args : List_Id;
3729
3730 begin
3731 if No (Expr) then
3732 Args := No_List;
3733 else
3734 Args := New_List (
3735 Make_Pragma_Argument_Association (Sloc (Expr),
3736 Expression => Relocate_Node (Expr)));
3737 end if;
3738
3739 Make_Aitem_Pragma
3740 (Pragma_Argument_Associations => Args,
3741 Pragma_Name => Chars (Id));
3742 end;
3743
3744 -- Part_Of
3745
3746 when Aspect_Part_Of =>
3747 if Nkind_In (N, N_Object_Declaration,
3748 N_Package_Instantiation)
3749 or else Is_Single_Concurrent_Type_Declaration (N)
3750 then
3751 Make_Aitem_Pragma
3752 (Pragma_Argument_Associations => New_List (
3753 Make_Pragma_Argument_Association (Loc,
3754 Expression => Relocate_Node (Expr))),
3755 Pragma_Name => Name_Part_Of);
3756
3757 Decorate (Aspect, Aitem);
3758 Insert_Pragma (Aitem);
3759
3760 else
3761 Error_Msg_NE
3762 ("aspect & must apply to package instantiation, "
3763 & "object, single protected type or single task type",
3764 Aspect, Id);
3765 end if;
3766
3767 goto Continue;
3768
3769 -- SPARK_Mode
3770
3771 when Aspect_SPARK_Mode =>
3772 Make_Aitem_Pragma
3773 (Pragma_Argument_Associations => New_List (
3774 Make_Pragma_Argument_Association (Loc,
3775 Expression => Relocate_Node (Expr))),
3776 Pragma_Name => Name_SPARK_Mode);
3777
3778 Decorate (Aspect, Aitem);
3779 Insert_Pragma (Aitem);
3780 goto Continue;
3781
3782 -- Refined_Depends
3783
3784 -- Aspect Refined_Depends is never delayed because it is
3785 -- equivalent to a source pragma which appears in the
3786 -- declarations of the related subprogram body. To deal with
3787 -- forward references, the generated pragma is stored in the
3788 -- contract of the related subprogram body and later analyzed
3789 -- at the end of the declarative region. For details, see
3790 -- routine Analyze_Refined_Depends_In_Decl_Part.
3791
3792 when Aspect_Refined_Depends =>
3793 Make_Aitem_Pragma
3794 (Pragma_Argument_Associations => New_List (
3795 Make_Pragma_Argument_Association (Loc,
3796 Expression => Relocate_Node (Expr))),
3797 Pragma_Name => Name_Refined_Depends);
3798
3799 Decorate (Aspect, Aitem);
3800 Insert_Pragma (Aitem);
3801 goto Continue;
3802
3803 -- Refined_Global
3804
3805 -- Aspect Refined_Global is never delayed because it is
3806 -- equivalent to a source pragma which appears in the
3807 -- declarations of the related subprogram body. To deal with
3808 -- forward references, the generated pragma is stored in the
3809 -- contract of the related subprogram body and later analyzed
3810 -- at the end of the declarative region. For details, see
3811 -- routine Analyze_Refined_Global_In_Decl_Part.
3812
3813 when Aspect_Refined_Global =>
3814 Make_Aitem_Pragma
3815 (Pragma_Argument_Associations => New_List (
3816 Make_Pragma_Argument_Association (Loc,
3817 Expression => Relocate_Node (Expr))),
3818 Pragma_Name => Name_Refined_Global);
3819
3820 Decorate (Aspect, Aitem);
3821 Insert_Pragma (Aitem);
3822 goto Continue;
3823
3824 -- Refined_Post
3825
3826 when Aspect_Refined_Post =>
3827 Make_Aitem_Pragma
3828 (Pragma_Argument_Associations => New_List (
3829 Make_Pragma_Argument_Association (Loc,
3830 Expression => Relocate_Node (Expr))),
3831 Pragma_Name => Name_Refined_Post);
3832
3833 Decorate (Aspect, Aitem);
3834 Insert_Pragma (Aitem);
3835 goto Continue;
3836
3837 -- Refined_State
3838
3839 when Aspect_Refined_State =>
3840
3841 -- The corresponding pragma for Refined_State is inserted in
3842 -- the declarations of the related package body. This action
3843 -- synchronizes both the source and from-aspect versions of
3844 -- the pragma.
3845
3846 if Nkind (N) = N_Package_Body then
3847 Make_Aitem_Pragma
3848 (Pragma_Argument_Associations => New_List (
3849 Make_Pragma_Argument_Association (Loc,
3850 Expression => Relocate_Node (Expr))),
3851 Pragma_Name => Name_Refined_State);
3852
3853 Decorate (Aspect, Aitem);
3854 Insert_Pragma (Aitem);
3855
3856 -- Otherwise the context is illegal
3857
3858 else
3859 Error_Msg_NE
3860 ("aspect & must apply to a package body", Aspect, Id);
3861 end if;
3862
3863 goto Continue;
3864
3865 -- Relative_Deadline
3866
3867 when Aspect_Relative_Deadline =>
3868 Make_Aitem_Pragma
3869 (Pragma_Argument_Associations => New_List (
3870 Make_Pragma_Argument_Association (Loc,
3871 Expression => Relocate_Node (Expr))),
3872 Pragma_Name => Name_Relative_Deadline);
3873
3874 -- If the aspect applies to a task, the corresponding pragma
3875 -- must appear within its declarations, not after.
3876
3877 if Nkind (N) = N_Task_Type_Declaration then
3878 declare
3879 Def : Node_Id;
3880 V : List_Id;
3881
3882 begin
3883 if No (Task_Definition (N)) then
3884 Set_Task_Definition (N,
3885 Make_Task_Definition (Loc,
3886 Visible_Declarations => New_List,
3887 End_Label => Empty));
3888 end if;
3889
3890 Def := Task_Definition (N);
3891 V := Visible_Declarations (Def);
3892 if not Is_Empty_List (V) then
3893 Insert_Before (First (V), Aitem);
3894
3895 else
3896 Set_Visible_Declarations (Def, New_List (Aitem));
3897 end if;
3898
3899 goto Continue;
3900 end;
3901 end if;
3902
3903 -- Relaxed_Initialization
3904
3905 when Aspect_Relaxed_Initialization =>
3906 Analyze_Aspect_Relaxed_Initialization;
3907 goto Continue;
3908
3909 -- Secondary_Stack_Size
3910
3911 -- Aspect Secondary_Stack_Size needs to be converted into a
3912 -- pragma for two reasons: the attribute is not analyzed until
3913 -- after the expansion of the task type declaration and the
3914 -- attribute does not have visibility on the discriminant.
3915
3916 when Aspect_Secondary_Stack_Size =>
3917 Make_Aitem_Pragma
3918 (Pragma_Argument_Associations => New_List (
3919 Make_Pragma_Argument_Association (Loc,
3920 Expression => Relocate_Node (Expr))),
3921 Pragma_Name =>
3922 Name_Secondary_Stack_Size);
3923
3924 Decorate (Aspect, Aitem);
3925 Insert_Pragma (Aitem);
3926 goto Continue;
3927
3928 -- Volatile_Function
3929
3930 -- Aspect Volatile_Function is never delayed because it is
3931 -- equivalent to a source pragma which appears after the
3932 -- related subprogram.
3933
3934 when Aspect_Volatile_Function =>
3935 Make_Aitem_Pragma
3936 (Pragma_Argument_Associations => New_List (
3937 Make_Pragma_Argument_Association (Loc,
3938 Expression => Relocate_Node (Expr))),
3939 Pragma_Name => Name_Volatile_Function);
3940
3941 Decorate (Aspect, Aitem);
3942 Insert_Pragma (Aitem);
3943 goto Continue;
3944
3945 -- Case 2e: Annotate aspect
3946
3947 when Aspect_Annotate =>
3948 declare
3949 Args : List_Id;
3950 Pargs : List_Id;
3951 Arg : Node_Id;
3952
3953 begin
3954 -- The argument can be a single identifier
3955
3956 if Nkind (Expr) = N_Identifier then
3957
3958 -- One level of parens is allowed
3959
3960 if Paren_Count (Expr) > 1 then
3961 Error_Msg_F ("extra parentheses ignored", Expr);
3962 end if;
3963
3964 Set_Paren_Count (Expr, 0);
3965
3966 -- Add the single item to the list
3967
3968 Args := New_List (Expr);
3969
3970 -- Otherwise we must have an aggregate
3971
3972 elsif Nkind (Expr) = N_Aggregate then
3973
3974 -- Must be positional
3975
3976 if Present (Component_Associations (Expr)) then
3977 Error_Msg_F
3978 ("purely positional aggregate required", Expr);
3979 goto Continue;
3980 end if;
3981
3982 -- Must not be parenthesized
3983
3984 if Paren_Count (Expr) /= 0 then
3985 Error_Msg_F ("extra parentheses ignored", Expr);
3986 end if;
3987
3988 -- List of arguments is list of aggregate expressions
3989
3990 Args := Expressions (Expr);
3991
3992 -- Anything else is illegal
3993
3994 else
3995 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3996 goto Continue;
3997 end if;
3998
3999 -- Prepare pragma arguments
4000
4001 Pargs := New_List;
4002 Arg := First (Args);
4003 while Present (Arg) loop
4004 Append_To (Pargs,
4005 Make_Pragma_Argument_Association (Sloc (Arg),
4006 Expression => Relocate_Node (Arg)));
4007 Next (Arg);
4008 end loop;
4009
4010 Append_To (Pargs,
4011 Make_Pragma_Argument_Association (Sloc (Ent),
4012 Chars => Name_Entity,
4013 Expression => Ent));
4014
4015 Make_Aitem_Pragma
4016 (Pragma_Argument_Associations => Pargs,
4017 Pragma_Name => Name_Annotate);
4018 end;
4019
4020 -- Case 3 : Aspects that don't correspond to pragma/attribute
4021 -- definition clause.
4022
4023 -- Case 3a: The aspects listed below don't correspond to
4024 -- pragmas/attributes but do require delayed analysis.
4025
4026 when Aspect_Default_Value | Aspect_Default_Component_Value =>
4027 Error_Msg_Name_1 := Chars (Id);
4028
4029 if not Is_Type (E) then
4030 Error_Msg_N ("aspect% can only apply to a type", Id);
4031 goto Continue;
4032
4033 elsif not Is_First_Subtype (E) then
4034 Error_Msg_N ("aspect% cannot apply to subtype", Id);
4035 goto Continue;
4036
4037 elsif A_Id = Aspect_Default_Value
4038 and then not Is_Scalar_Type (E)
4039 then
4040 Error_Msg_N ("aspect% can only be applied to scalar type",
4041 Id);
4042 goto Continue;
4043
4044 elsif A_Id = Aspect_Default_Component_Value then
4045 if not Is_Array_Type (E) then
4046 Error_Msg_N ("aspect% can only be applied to array " &
4047 "type", Id);
4048 goto Continue;
4049
4050 elsif not Is_Scalar_Type (Component_Type (E)) then
4051 Error_Msg_N ("aspect% requires scalar components", Id);
4052 goto Continue;
4053 end if;
4054 end if;
4055
4056 Aitem := Empty;
4057
4058 when Aspect_Integer_Literal
4059 | Aspect_Real_Literal
4060 | Aspect_String_Literal
4061 =>
4062
4063 if not Is_First_Subtype (E) then
4064 Error_Msg_N
4065 ("may only be specified for a first subtype", Aspect);
4066 goto Continue;
4067 end if;
4068
4069 if Ada_Version < Ada_2020 then
4070 Check_Restriction
4071 (No_Implementation_Aspect_Specifications, N);
4072 end if;
4073
4074 Aitem := Empty;
4075
4076 -- Case 3b: The aspects listed below don't correspond to
4077 -- pragmas/attributes and don't need delayed analysis.
4078
4079 -- Implicit_Dereference
4080
4081 -- For Implicit_Dereference, External_Name and Link_Name, only
4082 -- the legality checks are done during the analysis, thus no
4083 -- delay is required.
4084
4085 when Aspect_Implicit_Dereference =>
4086 Analyze_Aspect_Implicit_Dereference;
4087 goto Continue;
4088
4089 -- Dimension
4090
4091 when Aspect_Dimension =>
4092 Analyze_Aspect_Dimension (N, Id, Expr);
4093 goto Continue;
4094
4095 -- Dimension_System
4096
4097 when Aspect_Dimension_System =>
4098 Analyze_Aspect_Dimension_System (N, Id, Expr);
4099 goto Continue;
4100
4101 -- Case 4: Aspects requiring special handling
4102
4103 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
4104 -- pragmas take care of the delay.
4105
4106 -- Pre/Post
4107
4108 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
4109 -- with a first argument that is the expression, and a second
4110 -- argument that is an informative message if the test fails.
4111 -- This is inserted right after the declaration, to get the
4112 -- required pragma placement. The processing for the pragmas
4113 -- takes care of the required delay.
4114
4115 when Pre_Post_Aspects => Pre_Post : declare
4116 Pname : Name_Id;
4117
4118 begin
4119 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
4120 Pname := Name_Precondition;
4121 else
4122 Pname := Name_Postcondition;
4123 end if;
4124
4125 -- Check that the class-wide predicate cannot be applied to
4126 -- an operation of a synchronized type. AI12-0182 forbids
4127 -- these altogether, while earlier language semantics made
4128 -- them legal on tagged synchronized types.
4129
4130 -- Other legality checks are performed when analyzing the
4131 -- contract of the operation.
4132
4133 if Class_Present (Aspect)
4134 and then Is_Concurrent_Type (Current_Scope)
4135 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
4136 then
4137 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
4138 Error_Msg_N
4139 ("aspect % can only be specified for a primitive "
4140 & "operation of a tagged type", Aspect);
4141
4142 goto Continue;
4143 end if;
4144
4145 -- If the expressions is of the form A and then B, then
4146 -- we generate separate Pre/Post aspects for the separate
4147 -- clauses. Since we allow multiple pragmas, there is no
4148 -- problem in allowing multiple Pre/Post aspects internally.
4149 -- These should be treated in reverse order (B first and
4150 -- A second) since they are later inserted just after N in
4151 -- the order they are treated. This way, the pragma for A
4152 -- ends up preceding the pragma for B, which may have an
4153 -- importance for the error raised (either constraint error
4154 -- or precondition error).
4155
4156 -- We do not do this for Pre'Class, since we have to put
4157 -- these conditions together in a complex OR expression.
4158
4159 -- We don't do this in GNATprove mode, because it brings no
4160 -- benefit for proof and causes annoyance for flow analysis,
4161 -- which prefers to be as close to the original source code
4162 -- as possible. Also we don't do this when analyzing generic
4163 -- units since it causes spurious visibility errors in the
4164 -- preanalysis of instantiations.
4165
4166 if not GNATprove_Mode
4167 and then (Pname = Name_Postcondition
4168 or else not Class_Present (Aspect))
4169 and then not Inside_A_Generic
4170 then
4171 while Nkind (Expr) = N_And_Then loop
4172 Insert_After (Aspect,
4173 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
4174 Identifier => Identifier (Aspect),
4175 Expression => Relocate_Node (Left_Opnd (Expr)),
4176 Class_Present => Class_Present (Aspect),
4177 Split_PPC => True));
4178 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
4179 Eloc := Sloc (Expr);
4180 end loop;
4181 end if;
4182
4183 -- Build the precondition/postcondition pragma
4184
4185 -- We use Relocate_Node here rather than New_Copy_Tree
4186 -- because subsequent visibility analysis of the aspect
4187 -- depends on this sharing. This should be cleaned up???
4188
4189 -- If the context is generic, we want to preserve the
4190 -- original tree, and simply share it between aspect and
4191 -- generated attribute. This parallels what is done in
4192 -- sem_prag.adb (see Get_Argument).
4193
4194 declare
4195 New_Expr : Node_Id;
4196
4197 begin
4198 if Inside_A_Generic then
4199 New_Expr := Expr;
4200 else
4201 New_Expr := Relocate_Node (Expr);
4202 end if;
4203
4204 Make_Aitem_Pragma
4205 (Pragma_Argument_Associations => New_List (
4206 Make_Pragma_Argument_Association (Eloc,
4207 Chars => Name_Check,
4208 Expression => New_Expr)),
4209 Pragma_Name => Pname);
4210 end;
4211
4212 -- Add message unless exception messages are suppressed
4213
4214 if not Opt.Exception_Locations_Suppressed then
4215 Append_To (Pragma_Argument_Associations (Aitem),
4216 Make_Pragma_Argument_Association (Eloc,
4217 Chars => Name_Message,
4218 Expression =>
4219 Make_String_Literal (Eloc,
4220 Strval => "failed "
4221 & Get_Name_String (Pname)
4222 & " from "
4223 & Build_Location_String (Eloc))));
4224 end if;
4225
4226 Set_Is_Delayed_Aspect (Aspect);
4227
4228 -- For Pre/Post cases, insert immediately after the entity
4229 -- declaration, since that is the required pragma placement.
4230 -- Note that for these aspects, we do not have to worry
4231 -- about delay issues, since the pragmas themselves deal
4232 -- with delay of visibility for the expression analysis.
4233
4234 Insert_Pragma (Aitem);
4235
4236 goto Continue;
4237 end Pre_Post;
4238
4239 -- Test_Case
4240
4241 when Aspect_Test_Case => Test_Case : declare
4242 Args : List_Id;
4243 Comp_Expr : Node_Id;
4244 Comp_Assn : Node_Id;
4245
4246 begin
4247 Args := New_List;
4248
4249 if Nkind (Parent (N)) = N_Compilation_Unit then
4250 Error_Msg_Name_1 := Nam;
4251 Error_Msg_N ("incorrect placement of aspect `%`", E);
4252 goto Continue;
4253 end if;
4254
4255 if Nkind (Expr) /= N_Aggregate then
4256 Error_Msg_Name_1 := Nam;
4257 Error_Msg_NE
4258 ("wrong syntax for aspect `%` for &", Id, E);
4259 goto Continue;
4260 end if;
4261
4262 -- Create the list of arguments for building the Test_Case
4263 -- pragma.
4264
4265 Comp_Expr := First (Expressions (Expr));
4266 while Present (Comp_Expr) loop
4267 Append_To (Args,
4268 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
4269 Expression => Relocate_Node (Comp_Expr)));
4270 Next (Comp_Expr);
4271 end loop;
4272
4273 Comp_Assn := First (Component_Associations (Expr));
4274 while Present (Comp_Assn) loop
4275 if List_Length (Choices (Comp_Assn)) /= 1
4276 or else
4277 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
4278 then
4279 Error_Msg_Name_1 := Nam;
4280 Error_Msg_NE
4281 ("wrong syntax for aspect `%` for &", Id, E);
4282 goto Continue;
4283 end if;
4284
4285 Append_To (Args,
4286 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
4287 Chars => Chars (First (Choices (Comp_Assn))),
4288 Expression =>
4289 Relocate_Node (Expression (Comp_Assn))));
4290 Next (Comp_Assn);
4291 end loop;
4292
4293 -- Build the test-case pragma
4294
4295 Make_Aitem_Pragma
4296 (Pragma_Argument_Associations => Args,
4297 Pragma_Name => Nam);
4298 end Test_Case;
4299
4300 -- Contract_Cases
4301
4302 when Aspect_Contract_Cases =>
4303 Make_Aitem_Pragma
4304 (Pragma_Argument_Associations => New_List (
4305 Make_Pragma_Argument_Association (Loc,
4306 Expression => Relocate_Node (Expr))),
4307 Pragma_Name => Nam);
4308
4309 Decorate (Aspect, Aitem);
4310 Insert_Pragma (Aitem);
4311 goto Continue;
4312
4313 -- Case 5: Special handling for aspects with an optional
4314 -- boolean argument.
4315
4316 -- In the delayed case, the corresponding pragma cannot be
4317 -- generated yet because the evaluation of the boolean needs
4318 -- to be delayed till the freeze point.
4319
4320 when Boolean_Aspects
4321 | Library_Unit_Aspects
4322 =>
4323 Set_Is_Boolean_Aspect (Aspect);
4324
4325 -- Lock_Free aspect only apply to protected objects
4326
4327 if A_Id = Aspect_Lock_Free then
4328 if Ekind (E) /= E_Protected_Type then
4329 Error_Msg_Name_1 := Nam;
4330 Error_Msg_N
4331 ("aspect % only applies to a protected object",
4332 Aspect);
4333
4334 else
4335 -- Set the Uses_Lock_Free flag to True if there is no
4336 -- expression or if the expression is True. The
4337 -- evaluation of this aspect should be delayed to the
4338 -- freeze point (why???)
4339
4340 if No (Expr)
4341 or else Is_True (Static_Boolean (Expr))
4342 then
4343 Set_Uses_Lock_Free (E);
4344 end if;
4345
4346 Record_Rep_Item (E, Aspect);
4347 end if;
4348
4349 goto Continue;
4350
4351 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
4352 Analyze_Aspect_Export_Import;
4353
4354 -- Disable_Controlled
4355
4356 elsif A_Id = Aspect_Disable_Controlled then
4357 Analyze_Aspect_Disable_Controlled;
4358 goto Continue;
4359
4360 -- Ada 202x (AI12-0075): static expression functions
4361
4362 elsif A_Id = Aspect_Static then
4363 Analyze_Aspect_Static;
4364 goto Continue;
4365
4366 -- Ada 2020 (AI12-0279)
4367
4368 elsif A_Id = Aspect_Yield then
4369 Analyze_Aspect_Yield;
4370 goto Continue;
4371 end if;
4372
4373 -- Library unit aspects require special handling in the case
4374 -- of a package declaration, the pragma needs to be inserted
4375 -- in the list of declarations for the associated package.
4376 -- There is no issue of visibility delay for these aspects.
4377
4378 if A_Id in Library_Unit_Aspects
4379 and then
4380 Nkind_In (N, N_Package_Declaration,
4381 N_Generic_Package_Declaration)
4382 and then Nkind (Parent (N)) /= N_Compilation_Unit
4383
4384 -- Aspect is legal on a local instantiation of a library-
4385 -- level generic unit.
4386
4387 and then not Is_Generic_Instance (Defining_Entity (N))
4388 then
4389 Error_Msg_N
4390 ("incorrect context for library unit aspect&", Id);
4391 goto Continue;
4392 end if;
4393
4394 -- Cases where we do not delay, includes all cases where the
4395 -- expression is missing other than the above cases.
4396
4397 if not Delay_Required or else No (Expr) then
4398
4399 -- Exclude aspects Export and Import because their pragma
4400 -- syntax does not map directly to a Boolean aspect.
4401
4402 if A_Id /= Aspect_Export
4403 and then A_Id /= Aspect_Import
4404 then
4405 Make_Aitem_Pragma
4406 (Pragma_Argument_Associations => New_List (
4407 Make_Pragma_Argument_Association (Sloc (Ent),
4408 Expression => Ent)),
4409 Pragma_Name => Chars (Id));
4410 end if;
4411
4412 Delay_Required := False;
4413
4414 -- In general cases, the corresponding pragma/attribute
4415 -- definition clause will be inserted later at the freezing
4416 -- point, and we do not need to build it now.
4417
4418 else
4419 Aitem := Empty;
4420 end if;
4421
4422 -- Storage_Size
4423
4424 -- This is special because for access types we need to generate
4425 -- an attribute definition clause. This also works for single
4426 -- task declarations, but it does not work for task type
4427 -- declarations, because we have the case where the expression
4428 -- references a discriminant of the task type. That can't use
4429 -- an attribute definition clause because we would not have
4430 -- visibility on the discriminant. For that case we must
4431 -- generate a pragma in the task definition.
4432
4433 when Aspect_Storage_Size =>
4434
4435 -- Task type case
4436
4437 if Ekind (E) = E_Task_Type then
4438 declare
4439 Decl : constant Node_Id := Declaration_Node (E);
4440
4441 begin
4442 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
4443
4444 -- If no task definition, create one
4445
4446 if No (Task_Definition (Decl)) then
4447 Set_Task_Definition (Decl,
4448 Make_Task_Definition (Loc,
4449 Visible_Declarations => Empty_List,
4450 End_Label => Empty));
4451 end if;
4452
4453 -- Create a pragma and put it at the start of the task
4454 -- definition for the task type declaration.
4455
4456 Make_Aitem_Pragma
4457 (Pragma_Argument_Associations => New_List (
4458 Make_Pragma_Argument_Association (Loc,
4459 Expression => Relocate_Node (Expr))),
4460 Pragma_Name => Name_Storage_Size);
4461
4462 Prepend
4463 (Aitem,
4464 Visible_Declarations (Task_Definition (Decl)));
4465 goto Continue;
4466 end;
4467
4468 -- All other cases, generate attribute definition
4469
4470 else
4471 Aitem :=
4472 Make_Attribute_Definition_Clause (Loc,
4473 Name => Ent,
4474 Chars => Chars (Id),
4475 Expression => Relocate_Node (Expr));
4476 end if;
4477 end case;
4478
4479 -- Attach the corresponding pragma/attribute definition clause to
4480 -- the aspect specification node.
4481
4482 if Present (Aitem) then
4483 Set_From_Aspect_Specification (Aitem);
4484 end if;
4485
4486 -- For an aspect that applies to a type, indicate whether it
4487 -- appears on a partial view of the type.
4488
4489 if Is_Type (E)
4490 and then Is_Private_Type (E)
4491 then
4492 Set_Aspect_On_Partial_View (Aspect);
4493 end if;
4494
4495 -- In the context of a compilation unit, we directly put the
4496 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
4497 -- node (no delay is required here) except for aspects on a
4498 -- subprogram body (see below) and a generic package, for which we
4499 -- need to introduce the pragma before building the generic copy
4500 -- (see sem_ch12), and for package instantiations, where the
4501 -- library unit pragmas are better handled early.
4502
4503 if Nkind (Parent (N)) = N_Compilation_Unit
4504 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
4505 then
4506 declare
4507 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
4508
4509 begin
4510 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
4511
4512 -- For a Boolean aspect, create the corresponding pragma if
4513 -- no expression or if the value is True.
4514
4515 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
4516 if Is_True (Static_Boolean (Expr)) then
4517 Make_Aitem_Pragma
4518 (Pragma_Argument_Associations => New_List (
4519 Make_Pragma_Argument_Association (Sloc (Ent),
4520 Expression => Ent)),
4521 Pragma_Name => Chars (Id));
4522
4523 Set_From_Aspect_Specification (Aitem, True);
4524 Set_Corresponding_Aspect (Aitem, Aspect);
4525
4526 else
4527 goto Continue;
4528 end if;
4529 end if;
4530
4531 -- If the aspect is on a subprogram body (relevant aspect
4532 -- is Inline), add the pragma in front of the declarations.
4533
4534 if Nkind (N) = N_Subprogram_Body then
4535 if No (Declarations (N)) then
4536 Set_Declarations (N, New_List);
4537 end if;
4538
4539 Prepend (Aitem, Declarations (N));
4540
4541 elsif Nkind (N) = N_Generic_Package_Declaration then
4542 if No (Visible_Declarations (Specification (N))) then
4543 Set_Visible_Declarations (Specification (N), New_List);
4544 end if;
4545
4546 Prepend (Aitem,
4547 Visible_Declarations (Specification (N)));
4548
4549 elsif Nkind (N) = N_Package_Instantiation then
4550 declare
4551 Spec : constant Node_Id :=
4552 Specification (Instance_Spec (N));
4553 begin
4554 if No (Visible_Declarations (Spec)) then
4555 Set_Visible_Declarations (Spec, New_List);
4556 end if;
4557
4558 Prepend (Aitem, Visible_Declarations (Spec));
4559 end;
4560
4561 else
4562 if No (Pragmas_After (Aux)) then
4563 Set_Pragmas_After (Aux, New_List);
4564 end if;
4565
4566 Append (Aitem, Pragmas_After (Aux));
4567 end if;
4568
4569 goto Continue;
4570 end;
4571 end if;
4572
4573 -- The evaluation of the aspect is delayed to the freezing point.
4574 -- The pragma or attribute clause if there is one is then attached
4575 -- to the aspect specification which is put in the rep item list.
4576
4577 if Delay_Required then
4578 if Present (Aitem) then
4579 Set_Is_Delayed_Aspect (Aitem);
4580 Set_Aspect_Rep_Item (Aspect, Aitem);
4581 Set_Parent (Aitem, Aspect);
4582 end if;
4583
4584 Set_Is_Delayed_Aspect (Aspect);
4585
4586 -- In the case of Default_Value, link the aspect to base type
4587 -- as well, even though it appears on a first subtype. This is
4588 -- mandated by the semantics of the aspect. Do not establish
4589 -- the link when processing the base type itself as this leads
4590 -- to a rep item circularity.
4591
4592 if A_Id = Aspect_Default_Value and then Base_Type (E) /= E then
4593 Set_Has_Delayed_Aspects (Base_Type (E));
4594 Record_Rep_Item (Base_Type (E), Aspect);
4595 end if;
4596
4597 Set_Has_Delayed_Aspects (E);
4598 Record_Rep_Item (E, Aspect);
4599
4600 -- When delay is not required and the context is a package or a
4601 -- subprogram body, insert the pragma in the body declarations.
4602
4603 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
4604 if No (Declarations (N)) then
4605 Set_Declarations (N, New_List);
4606 end if;
4607
4608 -- The pragma is added before source declarations
4609
4610 Prepend_To (Declarations (N), Aitem);
4611
4612 -- When delay is not required and the context is not a compilation
4613 -- unit, we simply insert the pragma/attribute definition clause
4614 -- in sequence.
4615
4616 elsif Present (Aitem) then
4617 Insert_After (Ins_Node, Aitem);
4618 Ins_Node := Aitem;
4619 end if;
4620 end Analyze_One_Aspect;
4621
4622 <<Continue>>
4623 Next (Aspect);
4624 end loop Aspect_Loop;
4625
4626 if Has_Delayed_Aspects (E) then
4627 Ensure_Freeze_Node (E);
4628 end if;
4629 end Analyze_Aspect_Specifications;
4630
4631 ------------------------------------------------
4632 -- Analyze_Aspects_On_Subprogram_Body_Or_Stub --
4633 ------------------------------------------------
4634
4635 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id) is
4636 Body_Id : constant Entity_Id := Defining_Entity (N);
4637
4638 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
4639 -- Body [stub] N has aspects, but they are not properly placed. Emit an
4640 -- error message depending on the aspects involved. Spec_Id denotes the
4641 -- entity of the corresponding spec.
4642
4643 --------------------------------
4644 -- Diagnose_Misplaced_Aspects --
4645 --------------------------------
4646
4647 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
4648 procedure Misplaced_Aspect_Error
4649 (Asp : Node_Id;
4650 Ref_Nam : Name_Id);
4651 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
4652 -- the name of the refined version of the aspect.
4653
4654 ----------------------------
4655 -- Misplaced_Aspect_Error --
4656 ----------------------------
4657
4658 procedure Misplaced_Aspect_Error
4659 (Asp : Node_Id;
4660 Ref_Nam : Name_Id)
4661 is
4662 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
4663 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
4664
4665 begin
4666 -- The corresponding spec already contains the aspect in question
4667 -- and the one appearing on the body must be the refined form:
4668
4669 -- procedure P with Global ...;
4670 -- procedure P with Global ... is ... end P;
4671 -- ^
4672 -- Refined_Global
4673
4674 if Has_Aspect (Spec_Id, Asp_Id) then
4675 Error_Msg_Name_1 := Asp_Nam;
4676
4677 -- Subunits cannot carry aspects that apply to a subprogram
4678 -- declaration.
4679
4680 if Nkind (Parent (N)) = N_Subunit then
4681 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
4682
4683 -- Otherwise suggest the refined form
4684
4685 else
4686 Error_Msg_Name_2 := Ref_Nam;
4687 Error_Msg_N ("aspect % should be %", Asp);
4688 end if;
4689
4690 -- Otherwise the aspect must appear on the spec, not on the body
4691
4692 -- procedure P;
4693 -- procedure P with Global ... is ... end P;
4694
4695 else
4696 Error_Msg_N
4697 ("aspect specification must appear on initial declaration",
4698 Asp);
4699 end if;
4700 end Misplaced_Aspect_Error;
4701
4702 -- Local variables
4703
4704 Asp : Node_Id;
4705 Asp_Nam : Name_Id;
4706
4707 -- Start of processing for Diagnose_Misplaced_Aspects
4708
4709 begin
4710 -- Iterate over the aspect specifications and emit specific errors
4711 -- where applicable.
4712
4713 Asp := First (Aspect_Specifications (N));
4714 while Present (Asp) loop
4715 Asp_Nam := Chars (Identifier (Asp));
4716
4717 -- Do not emit errors on aspects that can appear on a subprogram
4718 -- body. This scenario occurs when the aspect specification list
4719 -- contains both misplaced and properly placed aspects.
4720
4721 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
4722 null;
4723
4724 -- Special diagnostics for SPARK aspects
4725
4726 elsif Asp_Nam = Name_Depends then
4727 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
4728
4729 elsif Asp_Nam = Name_Global then
4730 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
4731
4732 elsif Asp_Nam = Name_Post then
4733 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
4734
4735 -- Otherwise a language-defined aspect is misplaced
4736
4737 else
4738 Error_Msg_N
4739 ("aspect specification must appear on initial declaration",
4740 Asp);
4741 end if;
4742
4743 Next (Asp);
4744 end loop;
4745 end Diagnose_Misplaced_Aspects;
4746
4747 -- Local variables
4748
4749 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
4750
4751 -- Start of processing for Analyze_Aspects_On_Subprogram_Body_Or_Stub
4752
4753 begin
4754 -- Language-defined aspects cannot be associated with a subprogram body
4755 -- [stub] if the subprogram has a spec. Certain implementation defined
4756 -- aspects are allowed to break this rule (for all applicable cases, see
4757 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
4758
4759 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
4760 Diagnose_Misplaced_Aspects (Spec_Id);
4761 else
4762 Analyze_Aspect_Specifications (N, Body_Id);
4763 end if;
4764 end Analyze_Aspects_On_Subprogram_Body_Or_Stub;
4765
4766 -----------------------
4767 -- Analyze_At_Clause --
4768 -----------------------
4769
4770 -- An at clause is replaced by the corresponding Address attribute
4771 -- definition clause that is the preferred approach in Ada 95.
4772
4773 procedure Analyze_At_Clause (N : Node_Id) is
4774 CS : constant Boolean := Comes_From_Source (N);
4775
4776 begin
4777 -- This is an obsolescent feature
4778
4779 Check_Restriction (No_Obsolescent_Features, N);
4780
4781 if Warn_On_Obsolescent_Feature then
4782 Error_Msg_N
4783 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
4784 Error_Msg_N
4785 ("\?j?use address attribute definition clause instead", N);
4786 end if;
4787
4788 -- Rewrite as address clause
4789
4790 Rewrite (N,
4791 Make_Attribute_Definition_Clause (Sloc (N),
4792 Name => Identifier (N),
4793 Chars => Name_Address,
4794 Expression => Expression (N)));
4795
4796 -- We preserve Comes_From_Source, since logically the clause still comes
4797 -- from the source program even though it is changed in form.
4798
4799 Set_Comes_From_Source (N, CS);
4800
4801 -- Analyze rewritten clause
4802
4803 Analyze_Attribute_Definition_Clause (N);
4804 end Analyze_At_Clause;
4805
4806 -----------------------------------------
4807 -- Analyze_Attribute_Definition_Clause --
4808 -----------------------------------------
4809
4810 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
4811 Loc : constant Source_Ptr := Sloc (N);
4812 Nam : constant Node_Id := Name (N);
4813 Attr : constant Name_Id := Chars (N);
4814 Expr : constant Node_Id := Expression (N);
4815 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
4816
4817 Ent : Entity_Id;
4818 -- The entity of Nam after it is analyzed. In the case of an incomplete
4819 -- type, this is the underlying type.
4820
4821 U_Ent : Entity_Id;
4822 -- The underlying entity to which the attribute applies. Generally this
4823 -- is the Underlying_Type of Ent, except in the case where the clause
4824 -- applies to the full view of an incomplete or private type, in which
4825 -- case U_Ent is just a copy of Ent.
4826
4827 FOnly : Boolean := False;
4828 -- Reset to True for subtype specific attribute (Alignment, Size)
4829 -- and for stream attributes, i.e. those cases where in the call to
4830 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
4831 -- are checked. Note that the case of stream attributes is not clear
4832 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
4833 -- Storage_Size for derived task types, but that is also clearly
4834 -- unintentional.
4835
4836 procedure Analyze_Put_Image_TSS_Definition;
4837
4838 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
4839 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
4840 -- definition clauses.
4841
4842 function Duplicate_Clause return Boolean;
4843 -- This routine checks if the aspect for U_Ent being given by attribute
4844 -- definition clause N is for an aspect that has already been specified,
4845 -- and if so gives an error message. If there is a duplicate, True is
4846 -- returned, otherwise if there is no error, False is returned.
4847
4848 procedure Check_Indexing_Functions;
4849 -- Check that the function in Constant_Indexing or Variable_Indexing
4850 -- attribute has the proper type structure. If the name is overloaded,
4851 -- check that some interpretation is legal.
4852
4853 procedure Check_Iterator_Functions;
4854 -- Check that there is a single function in Default_Iterator attribute
4855 -- that has the proper type structure.
4856
4857 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
4858 -- Common legality check for the previous two
4859
4860 -----------------------------------
4861 -- Analyze_Put_Image_TSS_Definition --
4862 -----------------------------------
4863
4864 procedure Analyze_Put_Image_TSS_Definition is
4865 Subp : Entity_Id := Empty;
4866 I : Interp_Index;
4867 It : Interp;
4868 Pnam : Entity_Id;
4869
4870 function Has_Good_Profile
4871 (Subp : Entity_Id;
4872 Report : Boolean := False) return Boolean;
4873 -- Return true if the entity is a subprogram with an appropriate
4874 -- profile for the attribute being defined. If result is False and
4875 -- Report is True, function emits appropriate error.
4876
4877 ----------------------
4878 -- Has_Good_Profile --
4879 ----------------------
4880
4881 function Has_Good_Profile
4882 (Subp : Entity_Id;
4883 Report : Boolean := False) return Boolean
4884 is
4885 F : Entity_Id;
4886 Typ : Entity_Id;
4887
4888 begin
4889 if Ekind (Subp) /= E_Procedure then
4890 return False;
4891 end if;
4892
4893 F := First_Formal (Subp);
4894
4895 if No (F) or else Etype (F) /= Class_Wide_Type (RTE (RE_Sink)) then
4896 return False;
4897 end if;
4898
4899 Next_Formal (F);
4900
4901 if Parameter_Mode (F) /= E_In_Parameter then
4902 return False;
4903 end if;
4904
4905 Typ := Etype (F);
4906
4907 -- Verify that the prefix of the attribute and the local name for
4908 -- the type of the formal match.
4909
4910 if Typ /= Ent then
4911 return False;
4912 end if;
4913
4914 if Present (Next_Formal (F)) then
4915 return False;
4916
4917 elsif not Is_Scalar_Type (Typ)
4918 and then not Is_First_Subtype (Typ)
4919 then
4920 if Report and not Is_First_Subtype (Typ) then
4921 Error_Msg_N
4922 ("subtype of formal in Put_Image operation must be a "
4923 & "first subtype", Parameter_Type (Parent (F)));
4924 end if;
4925
4926 return False;
4927
4928 else
4929 return True;
4930 end if;
4931 end Has_Good_Profile;
4932
4933 -- Start of processing for Analyze_Put_Image_TSS_Definition
4934
4935 begin
4936 if not Is_Type (U_Ent) then
4937 Error_Msg_N ("local name must be a subtype", Nam);
4938 return;
4939
4940 elsif not Is_First_Subtype (U_Ent) then
4941 Error_Msg_N ("local name must be a first subtype", Nam);
4942 return;
4943 end if;
4944
4945 Pnam := TSS (Base_Type (U_Ent), TSS_Put_Image);
4946
4947 -- If Pnam is present, it can be either inherited from an ancestor
4948 -- type (in which case it is legal to redefine it for this type), or
4949 -- be a previous definition of the attribute for the same type (in
4950 -- which case it is illegal).
4951
4952 -- In the first case, it will have been analyzed already, and we can
4953 -- check that its profile does not match the expected profile for the
4954 -- Put_Image attribute of U_Ent. In the second case, either Pnam has
4955 -- been analyzed (and has the expected profile), or it has not been
4956 -- analyzed yet (case of a type that has not been frozen yet and for
4957 -- which Put_Image has been set using Set_TSS).
4958
4959 if Present (Pnam)
4960 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4961 then
4962 Error_Msg_Sloc := Sloc (Pnam);
4963 Error_Msg_Name_1 := Attr;
4964 Error_Msg_N ("% attribute already defined #", Nam);
4965 return;
4966 end if;
4967
4968 Analyze (Expr);
4969
4970 if Is_Entity_Name (Expr) then
4971 if not Is_Overloaded (Expr) then
4972 if Has_Good_Profile (Entity (Expr), Report => True) then
4973 Subp := Entity (Expr);
4974 end if;
4975
4976 else
4977 Get_First_Interp (Expr, I, It);
4978 while Present (It.Nam) loop
4979 if Has_Good_Profile (It.Nam) then
4980 Subp := It.Nam;
4981 exit;
4982 end if;
4983
4984 Get_Next_Interp (I, It);
4985 end loop;
4986 end if;
4987 end if;
4988
4989 if Present (Subp) then
4990 if Is_Abstract_Subprogram (Subp) then
4991 Error_Msg_N ("Put_Image subprogram must not be abstract", Expr);
4992 return;
4993 end if;
4994
4995 Set_Entity (Expr, Subp);
4996 Set_Etype (Expr, Etype (Subp));
4997
4998 New_Put_Image_Subprogram (N, U_Ent, Subp);
4999
5000 else
5001 Error_Msg_Name_1 := Attr;
5002 Error_Msg_N ("incorrect expression for% attribute", Expr);
5003 end if;
5004 end Analyze_Put_Image_TSS_Definition;
5005
5006 -----------------------------------
5007 -- Analyze_Stream_TSS_Definition --
5008 -----------------------------------
5009
5010 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
5011 Subp : Entity_Id := Empty;
5012 I : Interp_Index;
5013 It : Interp;
5014 Pnam : Entity_Id;
5015
5016 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
5017 -- True for Read attribute, False for other attributes
5018
5019 function Has_Good_Profile
5020 (Subp : Entity_Id;
5021 Report : Boolean := False) return Boolean;
5022 -- Return true if the entity is a subprogram with an appropriate
5023 -- profile for the attribute being defined. If result is False and
5024 -- Report is True, function emits appropriate error.
5025
5026 ----------------------
5027 -- Has_Good_Profile --
5028 ----------------------
5029
5030 function Has_Good_Profile
5031 (Subp : Entity_Id;
5032 Report : Boolean := False) return Boolean
5033 is
5034 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
5035 (False => E_Procedure, True => E_Function);
5036 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
5037 F : Entity_Id;
5038 Typ : Entity_Id;
5039
5040 begin
5041 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
5042 return False;
5043 end if;
5044
5045 F := First_Formal (Subp);
5046
5047 if No (F)
5048 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
5049 or else Designated_Type (Etype (F)) /=
5050 Class_Wide_Type (RTE (RE_Root_Stream_Type))
5051 then
5052 return False;
5053 end if;
5054
5055 if not Is_Function then
5056 Next_Formal (F);
5057
5058 declare
5059 Expected_Mode : constant array (Boolean) of Entity_Kind :=
5060 (False => E_In_Parameter,
5061 True => E_Out_Parameter);
5062 begin
5063 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
5064 return False;
5065 end if;
5066 end;
5067
5068 Typ := Etype (F);
5069
5070 else
5071 Typ := Etype (Subp);
5072 end if;
5073
5074 -- Verify that the prefix of the attribute and the local name for
5075 -- the type of the formal match.
5076
5077 if Base_Type (Typ) /= Base_Type (Ent) then
5078 return False;
5079 end if;
5080
5081 if Present (Next_Formal (F)) then
5082 return False;
5083
5084 elsif not Is_Scalar_Type (Typ)
5085 and then not Is_First_Subtype (Typ)
5086 and then not Is_Class_Wide_Type (Typ)
5087 then
5088 if Report and not Is_First_Subtype (Typ) then
5089 Error_Msg_N
5090 ("subtype of formal in stream operation must be a first "
5091 & "subtype", Parameter_Type (Parent (F)));
5092 end if;
5093
5094 return False;
5095
5096 else
5097 return True;
5098 end if;
5099 end Has_Good_Profile;
5100
5101 -- Start of processing for Analyze_Stream_TSS_Definition
5102
5103 begin
5104 FOnly := True;
5105
5106 if not Is_Type (U_Ent) then
5107 Error_Msg_N ("local name must be a subtype", Nam);
5108 return;
5109
5110 elsif not Is_First_Subtype (U_Ent) then
5111 Error_Msg_N ("local name must be a first subtype", Nam);
5112 return;
5113 end if;
5114
5115 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
5116
5117 -- If Pnam is present, it can be either inherited from an ancestor
5118 -- type (in which case it is legal to redefine it for this type), or
5119 -- be a previous definition of the attribute for the same type (in
5120 -- which case it is illegal).
5121
5122 -- In the first case, it will have been analyzed already, and we
5123 -- can check that its profile does not match the expected profile
5124 -- for a stream attribute of U_Ent. In the second case, either Pnam
5125 -- has been analyzed (and has the expected profile), or it has not
5126 -- been analyzed yet (case of a type that has not been frozen yet
5127 -- and for which the stream attribute has been set using Set_TSS).
5128
5129 if Present (Pnam)
5130 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
5131 then
5132 Error_Msg_Sloc := Sloc (Pnam);
5133 Error_Msg_Name_1 := Attr;
5134 Error_Msg_N ("% attribute already defined #", Nam);
5135 return;
5136 end if;
5137
5138 Analyze (Expr);
5139
5140 if Is_Entity_Name (Expr) then
5141 if not Is_Overloaded (Expr) then
5142 if Has_Good_Profile (Entity (Expr), Report => True) then
5143 Subp := Entity (Expr);
5144 end if;
5145
5146 else
5147 Get_First_Interp (Expr, I, It);
5148 while Present (It.Nam) loop
5149 if Has_Good_Profile (It.Nam) then
5150 Subp := It.Nam;
5151 exit;
5152 end if;
5153
5154 Get_Next_Interp (I, It);
5155 end loop;
5156 end if;
5157 end if;
5158
5159 if Present (Subp) then
5160 if Is_Abstract_Subprogram (Subp) then
5161 Error_Msg_N ("stream subprogram must not be abstract", Expr);
5162 return;
5163
5164 -- A stream subprogram for an interface type must be a null
5165 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
5166 -- of an interface is not an interface type (3.9.4 (6.b/2)).
5167
5168 elsif Is_Interface (U_Ent)
5169 and then not Is_Class_Wide_Type (U_Ent)
5170 and then not Inside_A_Generic
5171 and then
5172 (Ekind (Subp) = E_Function
5173 or else
5174 not Null_Present
5175 (Specification
5176 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
5177 then
5178 Error_Msg_N
5179 ("stream subprogram for interface type must be null "
5180 & "procedure", Expr);
5181 end if;
5182
5183 Set_Entity (Expr, Subp);
5184 Set_Etype (Expr, Etype (Subp));
5185
5186 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
5187
5188 else
5189 Error_Msg_Name_1 := Attr;
5190
5191 if Is_Class_Wide_Type (Base_Type (Ent)) then
5192 Error_Msg_N
5193 ("incorrect expression for class-wide% attribute", Expr);
5194 else
5195 Error_Msg_N ("incorrect expression for% attribute", Expr);
5196 end if;
5197 end if;
5198 end Analyze_Stream_TSS_Definition;
5199
5200 ------------------------------
5201 -- Check_Indexing_Functions --
5202 ------------------------------
5203
5204 procedure Check_Indexing_Functions is
5205 Indexing_Found : Boolean := False;
5206
5207 procedure Check_Inherited_Indexing;
5208 -- For a derived type, check that no indexing aspect is specified
5209 -- for the type if it is also inherited
5210 -- AI12-0160: verify that an indexing cannot be specified for
5211 -- a derived type unless it is specified for the parent.
5212
5213 procedure Check_One_Function (Subp : Entity_Id);
5214 -- Check one possible interpretation. Sets Indexing_Found True if a
5215 -- legal indexing function is found.
5216
5217 procedure Illegal_Indexing (Msg : String);
5218 -- Diagnose illegal indexing function if not overloaded. In the
5219 -- overloaded case indicate that no legal interpretation exists.
5220
5221 ------------------------------
5222 -- Check_Inherited_Indexing --
5223 ------------------------------
5224
5225 procedure Check_Inherited_Indexing is
5226 Inherited : Node_Id;
5227 Other_Indexing : Node_Id;
5228
5229 begin
5230 if Attr = Name_Constant_Indexing then
5231 Inherited :=
5232 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5233 Other_Indexing :=
5234 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5235
5236 else pragma Assert (Attr = Name_Variable_Indexing);
5237 Inherited :=
5238 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
5239 Other_Indexing :=
5240 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
5241 end if;
5242
5243 if Present (Inherited) then
5244 if Debug_Flag_Dot_XX then
5245 null;
5246
5247 -- OK if current attribute_definition_clause is expansion of
5248 -- inherited aspect.
5249
5250 elsif Aspect_Rep_Item (Inherited) = N then
5251 null;
5252
5253 -- Check if this is a confirming specification. The name
5254 -- may be overloaded between the parent operation and the
5255 -- inherited one, so we check that the Chars fields match.
5256
5257 elsif Is_Entity_Name (Expression (Inherited))
5258 and then Chars (Entity (Expression (Inherited))) =
5259 Chars (Entity (Expression (N)))
5260 then
5261 Indexing_Found := True;
5262
5263 -- Indicate the operation that must be overridden, rather than
5264 -- redefining the indexing aspect.
5265
5266 else
5267 Illegal_Indexing
5268 ("indexing function already inherited from parent type");
5269 Error_Msg_NE
5270 ("!override & instead",
5271 N, Entity (Expression (Inherited)));
5272 end if;
5273
5274 -- If not inherited and the parent has another indexing function
5275 -- this is illegal, because it leads to inconsistent results in
5276 -- class-wide calls.
5277
5278 elsif Present (Other_Indexing) then
5279 Error_Msg_N
5280 ("cannot specify indexing operation on derived type"
5281 & " if not specified for parent", N);
5282 end if;
5283 end Check_Inherited_Indexing;
5284
5285 ------------------------
5286 -- Check_One_Function --
5287 ------------------------
5288
5289 procedure Check_One_Function (Subp : Entity_Id) is
5290 Default_Element : Node_Id;
5291 Ret_Type : constant Entity_Id := Etype (Subp);
5292
5293 begin
5294 if not Is_Overloadable (Subp) then
5295 Illegal_Indexing ("illegal indexing function for type&");
5296 return;
5297
5298 elsif Scope (Subp) /= Scope (Ent) then
5299 if Nkind (Expr) = N_Expanded_Name then
5300
5301 -- Indexing function can't be declared elsewhere
5302
5303 Illegal_Indexing
5304 ("indexing function must be declared"
5305 & " in scope of type&");
5306 end if;
5307
5308 if Is_Derived_Type (Ent) then
5309 Check_Inherited_Indexing;
5310 end if;
5311
5312 return;
5313
5314 elsif No (First_Formal (Subp)) then
5315 Illegal_Indexing
5316 ("Indexing requires a function that applies to type&");
5317 return;
5318
5319 elsif No (Next_Formal (First_Formal (Subp))) then
5320 Illegal_Indexing
5321 ("indexing function must have at least two parameters");
5322 return;
5323
5324 elsif Is_Derived_Type (Ent) then
5325 Check_Inherited_Indexing;
5326 end if;
5327
5328 if not Check_Primitive_Function (Subp) then
5329 Illegal_Indexing
5330 ("Indexing aspect requires a function that applies to type&");
5331 return;
5332 end if;
5333
5334 -- If partial declaration exists, verify that it is not tagged.
5335
5336 if Ekind (Current_Scope) = E_Package
5337 and then Has_Private_Declaration (Ent)
5338 and then From_Aspect_Specification (N)
5339 and then
5340 List_Containing (Parent (Ent)) =
5341 Private_Declarations
5342 (Specification (Unit_Declaration_Node (Current_Scope)))
5343 and then Nkind (N) = N_Attribute_Definition_Clause
5344 then
5345 declare
5346 Decl : Node_Id;
5347
5348 begin
5349 Decl :=
5350 First (Visible_Declarations
5351 (Specification
5352 (Unit_Declaration_Node (Current_Scope))));
5353
5354 while Present (Decl) loop
5355 if Nkind (Decl) = N_Private_Type_Declaration
5356 and then Ent = Full_View (Defining_Identifier (Decl))
5357 and then Tagged_Present (Decl)
5358 and then No (Aspect_Specifications (Decl))
5359 then
5360 Illegal_Indexing
5361 ("Indexing aspect cannot be specified on full view "
5362 & "if partial view is tagged");
5363 return;
5364 end if;
5365
5366 Next (Decl);
5367 end loop;
5368 end;
5369 end if;
5370
5371 -- An indexing function must return either the default element of
5372 -- the container, or a reference type. For variable indexing it
5373 -- must be the latter.
5374
5375 Default_Element :=
5376 Find_Value_Of_Aspect
5377 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
5378
5379 if Present (Default_Element) then
5380 Analyze (Default_Element);
5381 end if;
5382
5383 -- For variable_indexing the return type must be a reference type
5384
5385 if Attr = Name_Variable_Indexing then
5386 if not Has_Implicit_Dereference (Ret_Type) then
5387 Illegal_Indexing
5388 ("variable indexing must return a reference type");
5389 return;
5390
5391 elsif Is_Access_Constant
5392 (Etype (First_Discriminant (Ret_Type)))
5393 then
5394 Illegal_Indexing
5395 ("variable indexing must return an access to variable");
5396 return;
5397 end if;
5398
5399 else
5400 if Has_Implicit_Dereference (Ret_Type)
5401 and then not
5402 Is_Access_Constant
5403 (Etype (Get_Reference_Discriminant (Ret_Type)))
5404 then
5405 Illegal_Indexing
5406 ("constant indexing must return an access to constant");
5407 return;
5408
5409 elsif Is_Access_Type (Etype (First_Formal (Subp)))
5410 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
5411 then
5412 Illegal_Indexing
5413 ("constant indexing must apply to an access to constant");
5414 return;
5415 end if;
5416 end if;
5417
5418 -- All checks succeeded.
5419
5420 Indexing_Found := True;
5421 end Check_One_Function;
5422
5423 -----------------------
5424 -- Illegal_Indexing --
5425 -----------------------
5426
5427 procedure Illegal_Indexing (Msg : String) is
5428 begin
5429 Error_Msg_NE (Msg, N, Ent);
5430 end Illegal_Indexing;
5431
5432 -- Start of processing for Check_Indexing_Functions
5433
5434 begin
5435 if In_Instance then
5436 Check_Inherited_Indexing;
5437 end if;
5438
5439 Analyze (Expr);
5440
5441 if not Is_Overloaded (Expr) then
5442 Check_One_Function (Entity (Expr));
5443
5444 else
5445 declare
5446 I : Interp_Index;
5447 It : Interp;
5448
5449 begin
5450 Indexing_Found := False;
5451 Get_First_Interp (Expr, I, It);
5452 while Present (It.Nam) loop
5453
5454 -- Note that analysis will have added the interpretation
5455 -- that corresponds to the dereference. We only check the
5456 -- subprogram itself. Ignore homonyms that may come from
5457 -- derived types in the context.
5458
5459 if Is_Overloadable (It.Nam)
5460 and then Comes_From_Source (It.Nam)
5461 then
5462 Check_One_Function (It.Nam);
5463 end if;
5464
5465 Get_Next_Interp (I, It);
5466 end loop;
5467 end;
5468 end if;
5469
5470 if not Indexing_Found and then not Error_Posted (N) then
5471 Error_Msg_NE
5472 ("aspect Indexing requires a local function that applies to "
5473 & "type&", Expr, Ent);
5474 end if;
5475 end Check_Indexing_Functions;
5476
5477 ------------------------------
5478 -- Check_Iterator_Functions --
5479 ------------------------------
5480
5481 procedure Check_Iterator_Functions is
5482 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
5483 -- Check one possible interpretation for validity
5484
5485 ----------------------------
5486 -- Valid_Default_Iterator --
5487 ----------------------------
5488
5489 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
5490 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
5491 Formal : Entity_Id;
5492
5493 begin
5494 if not Check_Primitive_Function (Subp) then
5495 return False;
5496
5497 -- The return type must be derived from a type in an instance
5498 -- of Iterator.Interfaces, and thus its root type must have a
5499 -- predefined name.
5500
5501 elsif Chars (Root_T) /= Name_Forward_Iterator
5502 and then Chars (Root_T) /= Name_Reversible_Iterator
5503 then
5504 return False;
5505
5506 else
5507 Formal := First_Formal (Subp);
5508 end if;
5509
5510 -- False if any subsequent formal has no default expression
5511
5512 Next_Formal (Formal);
5513 while Present (Formal) loop
5514 if No (Expression (Parent (Formal))) then
5515 return False;
5516 end if;
5517
5518 Next_Formal (Formal);
5519 end loop;
5520
5521 -- True if all subsequent formals have default expressions
5522
5523 return True;
5524 end Valid_Default_Iterator;
5525
5526 -- Start of processing for Check_Iterator_Functions
5527
5528 begin
5529 Analyze (Expr);
5530
5531 if not Is_Entity_Name (Expr) then
5532 Error_Msg_N ("aspect Iterator must be a function name", Expr);
5533 end if;
5534
5535 if not Is_Overloaded (Expr) then
5536 if Entity (Expr) /= Any_Id
5537 and then not Check_Primitive_Function (Entity (Expr))
5538 then
5539 Error_Msg_NE
5540 ("aspect Indexing requires a function that applies to type&",
5541 Entity (Expr), Ent);
5542 end if;
5543
5544 -- Flag the default_iterator as well as the denoted function.
5545
5546 if not Valid_Default_Iterator (Entity (Expr)) then
5547 Error_Msg_N ("improper function for default iterator!", Expr);
5548 end if;
5549
5550 else
5551 declare
5552 Default : Entity_Id := Empty;
5553 I : Interp_Index;
5554 It : Interp;
5555
5556 begin
5557 Get_First_Interp (Expr, I, It);
5558 while Present (It.Nam) loop
5559 if not Check_Primitive_Function (It.Nam)
5560 or else not Valid_Default_Iterator (It.Nam)
5561 then
5562 Remove_Interp (I);
5563
5564 elsif Present (Default) then
5565
5566 -- An explicit one should override an implicit one
5567
5568 if Comes_From_Source (Default) =
5569 Comes_From_Source (It.Nam)
5570 then
5571 Error_Msg_N ("default iterator must be unique", Expr);
5572 Error_Msg_Sloc := Sloc (Default);
5573 Error_Msg_N ("\\possible interpretation#", Expr);
5574 Error_Msg_Sloc := Sloc (It.Nam);
5575 Error_Msg_N ("\\possible interpretation#", Expr);
5576
5577 elsif Comes_From_Source (It.Nam) then
5578 Default := It.Nam;
5579 end if;
5580 else
5581 Default := It.Nam;
5582 end if;
5583
5584 Get_Next_Interp (I, It);
5585 end loop;
5586
5587 if Present (Default) then
5588 Set_Entity (Expr, Default);
5589 Set_Is_Overloaded (Expr, False);
5590 else
5591 Error_Msg_N
5592 ("no interpretation is a valid default iterator!", Expr);
5593 end if;
5594 end;
5595 end if;
5596 end Check_Iterator_Functions;
5597
5598 -------------------------------
5599 -- Check_Primitive_Function --
5600 -------------------------------
5601
5602 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
5603 Ctrl : Entity_Id;
5604
5605 begin
5606 if Ekind (Subp) /= E_Function then
5607 return False;
5608 end if;
5609
5610 if No (First_Formal (Subp)) then
5611 return False;
5612 else
5613 Ctrl := Etype (First_Formal (Subp));
5614 end if;
5615
5616 -- To be a primitive operation subprogram has to be in same scope.
5617
5618 if Scope (Ctrl) /= Scope (Subp) then
5619 return False;
5620 end if;
5621
5622 -- Type of formal may be the class-wide type, an access to such,
5623 -- or an incomplete view.
5624
5625 if Ctrl = Ent
5626 or else Ctrl = Class_Wide_Type (Ent)
5627 or else
5628 (Ekind (Ctrl) = E_Anonymous_Access_Type
5629 and then (Designated_Type (Ctrl) = Ent
5630 or else
5631 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
5632 or else
5633 (Ekind (Ctrl) = E_Incomplete_Type
5634 and then Full_View (Ctrl) = Ent)
5635 then
5636 null;
5637 else
5638 return False;
5639 end if;
5640
5641 return True;
5642 end Check_Primitive_Function;
5643
5644 ----------------------
5645 -- Duplicate_Clause --
5646 ----------------------
5647
5648 function Duplicate_Clause return Boolean is
5649 A : Node_Id;
5650
5651 begin
5652 -- Nothing to do if this attribute definition clause comes from
5653 -- an aspect specification, since we could not be duplicating an
5654 -- explicit clause, and we dealt with the case of duplicated aspects
5655 -- in Analyze_Aspect_Specifications.
5656
5657 if From_Aspect_Specification (N) then
5658 return False;
5659 end if;
5660
5661 -- Otherwise current clause may duplicate previous clause, or a
5662 -- previously given pragma or aspect specification for the same
5663 -- aspect.
5664
5665 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
5666
5667 if Present (A) then
5668 Error_Msg_Name_1 := Chars (N);
5669 Error_Msg_Sloc := Sloc (A);
5670
5671 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
5672 return True;
5673 end if;
5674
5675 return False;
5676 end Duplicate_Clause;
5677
5678 -- Start of processing for Analyze_Attribute_Definition_Clause
5679
5680 begin
5681 -- The following code is a defense against recursion. Not clear that
5682 -- this can happen legitimately, but perhaps some error situations can
5683 -- cause it, and we did see this recursion during testing.
5684
5685 if Analyzed (N) then
5686 return;
5687 else
5688 Set_Analyzed (N, True);
5689 end if;
5690
5691 Check_Restriction_No_Use_Of_Attribute (N);
5692
5693 if Get_Aspect_Id (Chars (N)) /= No_Aspect then
5694 -- 6.1/3 No_Specification_of_Aspect: Identifies an aspect for which
5695 -- no aspect_specification, attribute_definition_clause, or pragma
5696 -- is given.
5697 Check_Restriction_No_Specification_Of_Aspect (N);
5698 end if;
5699
5700 -- Ignore some selected attributes in CodePeer mode since they are not
5701 -- relevant in this context.
5702
5703 if CodePeer_Mode then
5704 case Id is
5705
5706 -- Ignore Component_Size in CodePeer mode, to avoid changing the
5707 -- internal representation of types by implicitly packing them.
5708
5709 when Attribute_Component_Size =>
5710 Rewrite (N, Make_Null_Statement (Sloc (N)));
5711 return;
5712
5713 when others =>
5714 null;
5715 end case;
5716 end if;
5717
5718 -- Process Ignore_Rep_Clauses option
5719
5720 if Ignore_Rep_Clauses then
5721 case Id is
5722
5723 -- The following should be ignored. They do not affect legality
5724 -- and may be target dependent. The basic idea of -gnatI is to
5725 -- ignore any rep clauses that may be target dependent but do not
5726 -- affect legality (except possibly to be rejected because they
5727 -- are incompatible with the compilation target).
5728
5729 when Attribute_Alignment
5730 | Attribute_Bit_Order
5731 | Attribute_Component_Size
5732 | Attribute_Default_Scalar_Storage_Order
5733 | Attribute_Machine_Radix
5734 | Attribute_Object_Size
5735 | Attribute_Scalar_Storage_Order
5736 | Attribute_Size
5737 | Attribute_Small
5738 | Attribute_Stream_Size
5739 | Attribute_Value_Size
5740 =>
5741 Kill_Rep_Clause (N);
5742 return;
5743
5744 -- The following should not be ignored, because in the first place
5745 -- they are reasonably portable, and should not cause problems
5746 -- in compiling code from another target, and also they do affect
5747 -- legality, e.g. failing to provide a stream attribute for a type
5748 -- may make a program illegal.
5749
5750 when Attribute_External_Tag
5751 | Attribute_Input
5752 | Attribute_Output
5753 | Attribute_Put_Image
5754 | Attribute_Read
5755 | Attribute_Simple_Storage_Pool
5756 | Attribute_Storage_Pool
5757 | Attribute_Storage_Size
5758 | Attribute_Write
5759 =>
5760 null;
5761
5762 -- We do not do anything here with address clauses, they will be
5763 -- removed by Freeze later on, but for now, it works better to
5764 -- keep them in the tree.
5765
5766 when Attribute_Address =>
5767 null;
5768
5769 -- Other cases are errors ("attribute& cannot be set with
5770 -- definition clause"), which will be caught below.
5771
5772 when others =>
5773 null;
5774 end case;
5775 end if;
5776
5777 Analyze (Nam);
5778 Ent := Entity (Nam);
5779
5780 if Rep_Item_Too_Early (Ent, N) then
5781 return;
5782 end if;
5783
5784 -- Rep clause applies to (underlying) full view of private or incomplete
5785 -- type if we have one (if not, this is a premature use of the type).
5786 -- However, some semantic checks need to be done on the specified entity
5787 -- i.e. the private view, so we save it in Ent.
5788
5789 if Is_Private_Type (Ent)
5790 and then Is_Derived_Type (Ent)
5791 and then not Is_Tagged_Type (Ent)
5792 and then No (Full_View (Ent))
5793 and then No (Underlying_Full_View (Ent))
5794 then
5795 U_Ent := Ent;
5796
5797 elsif Ekind (Ent) = E_Incomplete_Type then
5798
5799 -- The attribute applies to the full view, set the entity of the
5800 -- attribute definition accordingly.
5801
5802 Ent := Underlying_Type (Ent);
5803 U_Ent := Ent;
5804 Set_Entity (Nam, Ent);
5805
5806 else
5807 U_Ent := Underlying_Type (Ent);
5808 end if;
5809
5810 -- Avoid cascaded error
5811
5812 if Etype (Nam) = Any_Type then
5813 return;
5814
5815 -- Must be declared in current scope or in case of an aspect
5816 -- specification, must be visible in current scope.
5817
5818 elsif Scope (Ent) /= Current_Scope
5819 and then
5820 not (From_Aspect_Specification (N)
5821 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
5822 then
5823 Error_Msg_N ("entity must be declared in this scope", Nam);
5824 return;
5825
5826 -- Must not be a source renaming (we do have some cases where the
5827 -- expander generates a renaming, and those cases are OK, in such
5828 -- cases any attribute applies to the renamed object as well).
5829
5830 elsif Is_Object (Ent)
5831 and then Present (Renamed_Object (Ent))
5832 then
5833 -- In the case of a renamed object from source, this is an error
5834 -- unless the object is an aggregate and the renaming is created
5835 -- for an object declaration.
5836
5837 if Comes_From_Source (Renamed_Object (Ent))
5838 and then Nkind (Renamed_Object (Ent)) /= N_Aggregate
5839 then
5840 Get_Name_String (Chars (N));
5841 Error_Msg_Strlen := Name_Len;
5842 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
5843 Error_Msg_N
5844 ("~ clause not allowed for a renaming declaration "
5845 & "(RM 13.1(6))", Nam);
5846 return;
5847
5848 -- For the case of a compiler generated renaming, the attribute
5849 -- definition clause applies to the renamed object created by the
5850 -- expander. The easiest general way to handle this is to create a
5851 -- copy of the attribute definition clause for this object.
5852
5853 elsif Is_Entity_Name (Renamed_Object (Ent)) then
5854 Insert_Action (N,
5855 Make_Attribute_Definition_Clause (Loc,
5856 Name =>
5857 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
5858 Chars => Chars (N),
5859 Expression => Duplicate_Subexpr (Expression (N))));
5860
5861 -- If the renamed object is not an entity, it must be a dereference
5862 -- of an unconstrained function call, and we must introduce a new
5863 -- declaration to capture the expression. This is needed in the case
5864 -- of 'Alignment, where the original declaration must be rewritten.
5865
5866 else
5867 pragma Assert
5868 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
5869 null;
5870 end if;
5871
5872 -- If no underlying entity, use entity itself, applies to some
5873 -- previously detected error cases ???
5874
5875 elsif No (U_Ent) then
5876 U_Ent := Ent;
5877
5878 -- Cannot specify for a subtype (exception Object/Value_Size)
5879
5880 elsif Is_Type (U_Ent)
5881 and then not Is_First_Subtype (U_Ent)
5882 and then Id /= Attribute_Object_Size
5883 and then Id /= Attribute_Value_Size
5884 and then not From_At_Mod (N)
5885 then
5886 Error_Msg_N ("cannot specify attribute for subtype", Nam);
5887 return;
5888 end if;
5889
5890 Set_Entity (N, U_Ent);
5891
5892 -- Switch on particular attribute
5893
5894 case Id is
5895
5896 -------------
5897 -- Address --
5898 -------------
5899
5900 -- Address attribute definition clause
5901
5902 when Attribute_Address => Address : begin
5903
5904 -- A little error check, catch for X'Address use X'Address;
5905
5906 if Nkind (Nam) = N_Identifier
5907 and then Nkind (Expr) = N_Attribute_Reference
5908 and then Attribute_Name (Expr) = Name_Address
5909 and then Nkind (Prefix (Expr)) = N_Identifier
5910 and then Chars (Nam) = Chars (Prefix (Expr))
5911 then
5912 Error_Msg_NE
5913 ("address for & is self-referencing", Prefix (Expr), Ent);
5914 return;
5915 end if;
5916
5917 -- Not that special case, carry on with analysis of expression
5918
5919 Analyze_And_Resolve (Expr, RTE (RE_Address));
5920
5921 -- Even when ignoring rep clauses we need to indicate that the
5922 -- entity has an address clause and thus it is legal to declare
5923 -- it imported. Freeze will get rid of the address clause later.
5924 -- Also call Set_Address_Taken to indicate that an address clause
5925 -- was present, even if we are about to remove it.
5926
5927 if Ignore_Rep_Clauses then
5928 Set_Address_Taken (U_Ent);
5929
5930 if Ekind_In (U_Ent, E_Variable, E_Constant) then
5931 Record_Rep_Item (U_Ent, N);
5932 end if;
5933
5934 return;
5935 end if;
5936
5937 if Duplicate_Clause then
5938 null;
5939
5940 -- Case of address clause for subprogram
5941
5942 elsif Is_Subprogram (U_Ent) then
5943 if Has_Homonym (U_Ent) then
5944 Error_Msg_N
5945 ("address clause cannot be given for overloaded "
5946 & "subprogram", Nam);
5947 return;
5948 end if;
5949
5950 -- For subprograms, all address clauses are permitted, and we
5951 -- mark the subprogram as having a deferred freeze so that Gigi
5952 -- will not elaborate it too soon.
5953
5954 -- Above needs more comments, what is too soon about???
5955
5956 Set_Has_Delayed_Freeze (U_Ent);
5957
5958 -- Case of address clause for entry
5959
5960 elsif Ekind (U_Ent) = E_Entry then
5961 if Nkind (Parent (N)) = N_Task_Body then
5962 Error_Msg_N
5963 ("entry address must be specified in task spec", Nam);
5964 return;
5965 end if;
5966
5967 -- For entries, we require a constant address
5968
5969 Check_Constant_Address_Clause (Expr, U_Ent);
5970
5971 -- Special checks for task types
5972
5973 if Is_Task_Type (Scope (U_Ent))
5974 and then Comes_From_Source (Scope (U_Ent))
5975 then
5976 Error_Msg_N
5977 ("??entry address declared for entry in task type", N);
5978 Error_Msg_N
5979 ("\??only one task can be declared of this type", N);
5980 end if;
5981
5982 -- Entry address clauses are obsolescent
5983
5984 Check_Restriction (No_Obsolescent_Features, N);
5985
5986 if Warn_On_Obsolescent_Feature then
5987 Error_Msg_N
5988 ("?j?attaching interrupt to task entry is an obsolescent "
5989 & "feature (RM J.7.1)", N);
5990 Error_Msg_N
5991 ("\?j?use interrupt procedure instead", N);
5992 end if;
5993
5994 -- Case of an address clause for a class-wide object, which is
5995 -- considered erroneous.
5996
5997 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
5998 Error_Msg_NE
5999 ("??class-wide object & must not be overlaid", Nam, U_Ent);
6000 Error_Msg_N
6001 ("\??Program_Error will be raised at run time", Nam);
6002 Insert_Action (Declaration_Node (U_Ent),
6003 Make_Raise_Program_Error (Loc,
6004 Reason => PE_Overlaid_Controlled_Object));
6005 return;
6006
6007 -- Case of address clause for an object
6008
6009 elsif Ekind_In (U_Ent, E_Constant, E_Variable) then
6010 declare
6011 Expr : constant Node_Id := Expression (N);
6012 O_Ent : Entity_Id;
6013 Off : Boolean;
6014
6015 begin
6016 -- Exported variables cannot have an address clause, because
6017 -- this cancels the effect of the pragma Export.
6018
6019 if Is_Exported (U_Ent) then
6020 Error_Msg_N
6021 ("cannot export object with address clause", Nam);
6022 return;
6023 end if;
6024
6025 Find_Overlaid_Entity (N, O_Ent, Off);
6026
6027 if Present (O_Ent) then
6028
6029 -- If the object overlays a constant object, mark it so
6030
6031 if Is_Constant_Object (O_Ent) then
6032 Set_Overlays_Constant (U_Ent);
6033 end if;
6034
6035 -- If the address clause is of the form:
6036
6037 -- for X'Address use Y'Address;
6038
6039 -- or
6040
6041 -- C : constant Address := Y'Address;
6042 -- ...
6043 -- for X'Address use C;
6044
6045 -- then we make an entry in the table to check the size
6046 -- and alignment of the overlaying variable. But we defer
6047 -- this check till after code generation to take full
6048 -- advantage of the annotation done by the back end.
6049
6050 -- If the entity has a generic type, the check will be
6051 -- performed in the instance if the actual type justifies
6052 -- it, and we do not insert the clause in the table to
6053 -- prevent spurious warnings.
6054
6055 -- Note: we used to test Comes_From_Source and only give
6056 -- this warning for source entities, but we have removed
6057 -- this test. It really seems bogus to generate overlays
6058 -- that would trigger this warning in generated code.
6059 -- Furthermore, by removing the test, we handle the
6060 -- aspect case properly.
6061
6062 if Is_Object (O_Ent)
6063 and then not Is_Generic_Formal (O_Ent)
6064 and then not Is_Generic_Type (Etype (U_Ent))
6065 and then Address_Clause_Overlay_Warnings
6066 then
6067 Register_Address_Clause_Check
6068 (N, U_Ent, No_Uint, O_Ent, Off);
6069 end if;
6070
6071 -- If the overlay changes the storage order, mark the
6072 -- entity as being volatile to block any optimization
6073 -- for it since the construct is not really supported
6074 -- by the back end.
6075
6076 if (Is_Record_Type (Etype (U_Ent))
6077 or else Is_Array_Type (Etype (U_Ent)))
6078 and then (Is_Record_Type (Etype (O_Ent))
6079 or else Is_Array_Type (Etype (O_Ent)))
6080 and then Reverse_Storage_Order (Etype (U_Ent)) /=
6081 Reverse_Storage_Order (Etype (O_Ent))
6082 then
6083 Set_Treat_As_Volatile (U_Ent);
6084 end if;
6085
6086 else
6087 -- If this is not an overlay, mark a variable as being
6088 -- volatile to prevent unwanted optimizations. It's a
6089 -- conservative interpretation of RM 13.3(19) for the
6090 -- cases where the compiler cannot detect potential
6091 -- aliasing issues easily and it also covers the case
6092 -- of an absolute address where the volatile aspect is
6093 -- kind of implicit.
6094
6095 if Ekind (U_Ent) = E_Variable then
6096 Set_Treat_As_Volatile (U_Ent);
6097 end if;
6098
6099 -- Make an entry in the table for an absolute address as
6100 -- above to check that the value is compatible with the
6101 -- alignment of the object.
6102
6103 declare
6104 Addr : constant Node_Id := Address_Value (Expr);
6105 begin
6106 if Compile_Time_Known_Value (Addr)
6107 and then Address_Clause_Overlay_Warnings
6108 then
6109 Register_Address_Clause_Check
6110 (N, U_Ent, Expr_Value (Addr), Empty, False);
6111 end if;
6112 end;
6113 end if;
6114
6115 -- Issue an unconditional warning for a constant overlaying
6116 -- a variable. For the reverse case, we will issue it only
6117 -- if the variable is modified.
6118
6119 if Ekind (U_Ent) = E_Constant
6120 and then Present (O_Ent)
6121 and then not Overlays_Constant (U_Ent)
6122 and then Address_Clause_Overlay_Warnings
6123 then
6124 Error_Msg_N ("??constant overlays a variable", Expr);
6125
6126 -- Imported variables can have an address clause, but then
6127 -- the import is pretty meaningless except to suppress
6128 -- initializations, so we do not need such variables to
6129 -- be statically allocated (and in fact it causes trouble
6130 -- if the address clause is a local value).
6131
6132 elsif Is_Imported (U_Ent) then
6133 Set_Is_Statically_Allocated (U_Ent, False);
6134 end if;
6135
6136 -- We mark a possible modification of a variable with an
6137 -- address clause, since it is likely aliasing is occurring.
6138
6139 Note_Possible_Modification (Nam, Sure => False);
6140
6141 -- Legality checks on the address clause for initialized
6142 -- objects is deferred until the freeze point, because
6143 -- a subsequent pragma might indicate that the object
6144 -- is imported and thus not initialized. Also, the address
6145 -- clause might involve entities that have yet to be
6146 -- elaborated.
6147
6148 Set_Has_Delayed_Freeze (U_Ent);
6149
6150 -- If an initialization call has been generated for this
6151 -- object, it needs to be deferred to after the freeze node
6152 -- we have just now added, otherwise GIGI will see a
6153 -- reference to the variable (as actual to the IP call)
6154 -- before its definition.
6155
6156 declare
6157 Init_Call : constant Node_Id :=
6158 Remove_Init_Call (U_Ent, N);
6159
6160 begin
6161 if Present (Init_Call) then
6162 Append_Freeze_Action (U_Ent, Init_Call);
6163
6164 -- Reset Initialization_Statements pointer so that
6165 -- if there is a pragma Import further down, it can
6166 -- clear any default initialization.
6167
6168 Set_Initialization_Statements (U_Ent, Init_Call);
6169 end if;
6170 end;
6171
6172 -- Entity has delayed freeze, so we will generate an
6173 -- alignment check at the freeze point unless suppressed.
6174
6175 if not Range_Checks_Suppressed (U_Ent)
6176 and then not Alignment_Checks_Suppressed (U_Ent)
6177 then
6178 Set_Check_Address_Alignment (N);
6179 end if;
6180
6181 -- Kill the size check code, since we are not allocating
6182 -- the variable, it is somewhere else.
6183
6184 Kill_Size_Check_Code (U_Ent);
6185 end;
6186
6187 -- Not a valid entity for an address clause
6188
6189 else
6190 Error_Msg_N ("address cannot be given for &", Nam);
6191 end if;
6192 end Address;
6193
6194 ---------------
6195 -- Alignment --
6196 ---------------
6197
6198 -- Alignment attribute definition clause
6199
6200 when Attribute_Alignment => Alignment : declare
6201 Align : constant Uint := Get_Alignment_Value (Expr);
6202 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
6203
6204 begin
6205 FOnly := True;
6206
6207 if not Is_Type (U_Ent)
6208 and then Ekind (U_Ent) /= E_Variable
6209 and then Ekind (U_Ent) /= E_Constant
6210 then
6211 Error_Msg_N ("alignment cannot be given for &", Nam);
6212
6213 elsif Duplicate_Clause then
6214 null;
6215
6216 elsif Align /= No_Uint then
6217 Set_Has_Alignment_Clause (U_Ent);
6218
6219 -- Tagged type case, check for attempt to set alignment to a
6220 -- value greater than Max_Align, and reset if so.
6221
6222 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
6223 Error_Msg_N
6224 ("alignment for & set to Maximum_Aligment??", Nam);
6225 Set_Alignment (U_Ent, Max_Align);
6226
6227 -- All other cases
6228
6229 else
6230 Set_Alignment (U_Ent, Align);
6231 end if;
6232
6233 -- For an array type, U_Ent is the first subtype. In that case,
6234 -- also set the alignment of the anonymous base type so that
6235 -- other subtypes (such as the itypes for aggregates of the
6236 -- type) also receive the expected alignment.
6237
6238 if Is_Array_Type (U_Ent) then
6239 Set_Alignment (Base_Type (U_Ent), Align);
6240 end if;
6241 end if;
6242 end Alignment;
6243
6244 ---------------
6245 -- Bit_Order --
6246 ---------------
6247
6248 -- Bit_Order attribute definition clause
6249
6250 when Attribute_Bit_Order =>
6251 if not Is_Record_Type (U_Ent) then
6252 Error_Msg_N
6253 ("Bit_Order can only be defined for record type", Nam);
6254
6255 elsif Is_Tagged_Type (U_Ent) and then Is_Derived_Type (U_Ent) then
6256 Error_Msg_N
6257 ("Bit_Order cannot be defined for record extensions", Nam);
6258
6259 elsif Duplicate_Clause then
6260 null;
6261
6262 else
6263 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6264
6265 if Etype (Expr) = Any_Type then
6266 return;
6267
6268 elsif not Is_OK_Static_Expression (Expr) then
6269 Flag_Non_Static_Expr
6270 ("Bit_Order requires static expression!", Expr);
6271
6272 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6273 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
6274 end if;
6275 end if;
6276
6277 --------------------
6278 -- Component_Size --
6279 --------------------
6280
6281 -- Component_Size attribute definition clause
6282
6283 when Attribute_Component_Size => Component_Size_Case : declare
6284 Csize : constant Uint := Static_Integer (Expr);
6285 Ctyp : Entity_Id;
6286 Btype : Entity_Id;
6287 Biased : Boolean;
6288 New_Ctyp : Entity_Id;
6289 Decl : Node_Id;
6290
6291 begin
6292 if not Is_Array_Type (U_Ent) then
6293 Error_Msg_N ("component size requires array type", Nam);
6294 return;
6295 end if;
6296
6297 Btype := Base_Type (U_Ent);
6298 Ctyp := Component_Type (Btype);
6299
6300 if Duplicate_Clause then
6301 null;
6302
6303 elsif Rep_Item_Too_Early (Btype, N) then
6304 null;
6305
6306 elsif Csize /= No_Uint then
6307 Check_Size (Expr, Ctyp, Csize, Biased);
6308
6309 -- For the biased case, build a declaration for a subtype that
6310 -- will be used to represent the biased subtype that reflects
6311 -- the biased representation of components. We need the subtype
6312 -- to get proper conversions on referencing elements of the
6313 -- array.
6314
6315 if Biased then
6316 New_Ctyp :=
6317 Make_Defining_Identifier (Loc,
6318 Chars =>
6319 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
6320
6321 Decl :=
6322 Make_Subtype_Declaration (Loc,
6323 Defining_Identifier => New_Ctyp,
6324 Subtype_Indication =>
6325 New_Occurrence_Of (Component_Type (Btype), Loc));
6326
6327 Set_Parent (Decl, N);
6328 Analyze (Decl, Suppress => All_Checks);
6329
6330 Set_Has_Delayed_Freeze (New_Ctyp, False);
6331 Init_Esize (New_Ctyp);
6332 Set_RM_Size (New_Ctyp, Csize);
6333 Init_Alignment (New_Ctyp);
6334 Set_Is_Itype (New_Ctyp, True);
6335 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
6336
6337 Set_Component_Type (Btype, New_Ctyp);
6338 Set_Biased (New_Ctyp, N, "component size clause");
6339 end if;
6340
6341 Set_Component_Size (Btype, Csize);
6342
6343 -- Deal with warning on overridden size
6344
6345 if Warn_On_Overridden_Size
6346 and then Has_Size_Clause (Ctyp)
6347 and then RM_Size (Ctyp) /= Csize
6348 then
6349 Error_Msg_NE
6350 ("component size overrides size clause for&?S?", N, Ctyp);
6351 end if;
6352
6353 Set_Has_Component_Size_Clause (Btype, True);
6354 Set_Has_Non_Standard_Rep (Btype, True);
6355 end if;
6356 end Component_Size_Case;
6357
6358 -----------------------
6359 -- Constant_Indexing --
6360 -----------------------
6361
6362 when Attribute_Constant_Indexing =>
6363 Check_Indexing_Functions;
6364
6365 ---------
6366 -- CPU --
6367 ---------
6368
6369 when Attribute_CPU =>
6370
6371 -- CPU attribute definition clause not allowed except from aspect
6372 -- specification.
6373
6374 if From_Aspect_Specification (N) then
6375 if not Is_Task_Type (U_Ent) then
6376 Error_Msg_N ("CPU can only be defined for task", Nam);
6377
6378 elsif Duplicate_Clause then
6379 null;
6380
6381 else
6382 -- The expression must be analyzed in the special manner
6383 -- described in "Handling of Default and Per-Object
6384 -- Expressions" in sem.ads.
6385
6386 -- The visibility to the components must be established
6387 -- and restored before and after analysis.
6388
6389 Push_Type (U_Ent);
6390 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
6391 Pop_Type (U_Ent);
6392
6393 if not Is_OK_Static_Expression (Expr) then
6394 Check_Restriction (Static_Priorities, Expr);
6395 end if;
6396 end if;
6397
6398 else
6399 Error_Msg_N
6400 ("attribute& cannot be set with definition clause", N);
6401 end if;
6402
6403 ----------------------
6404 -- Default_Iterator --
6405 ----------------------
6406
6407 when Attribute_Default_Iterator => Default_Iterator : declare
6408 Func : Entity_Id;
6409 Typ : Entity_Id;
6410
6411 begin
6412 -- If target type is untagged, further checks are irrelevant
6413
6414 if not Is_Tagged_Type (U_Ent) then
6415 Error_Msg_N
6416 ("aspect Default_Iterator applies to tagged type", Nam);
6417 return;
6418 end if;
6419
6420 Check_Iterator_Functions;
6421
6422 Analyze (Expr);
6423
6424 if not Is_Entity_Name (Expr)
6425 or else Ekind (Entity (Expr)) /= E_Function
6426 then
6427 Error_Msg_N ("aspect Iterator must be a function", Expr);
6428 return;
6429 else
6430 Func := Entity (Expr);
6431 end if;
6432
6433 -- The type of the first parameter must be T, T'class, or a
6434 -- corresponding access type (5.5.1 (8/3). If function is
6435 -- parameterless label type accordingly.
6436
6437 if No (First_Formal (Func)) then
6438 Typ := Any_Type;
6439 else
6440 Typ := Etype (First_Formal (Func));
6441 end if;
6442
6443 if Typ = U_Ent
6444 or else Typ = Class_Wide_Type (U_Ent)
6445 or else (Is_Access_Type (Typ)
6446 and then Designated_Type (Typ) = U_Ent)
6447 or else (Is_Access_Type (Typ)
6448 and then Designated_Type (Typ) =
6449 Class_Wide_Type (U_Ent))
6450 then
6451 null;
6452
6453 else
6454 Error_Msg_NE
6455 ("Default Iterator must be a primitive of&", Func, U_Ent);
6456 end if;
6457 end Default_Iterator;
6458
6459 ------------------------
6460 -- Dispatching_Domain --
6461 ------------------------
6462
6463 when Attribute_Dispatching_Domain =>
6464
6465 -- Dispatching_Domain attribute definition clause not allowed
6466 -- except from aspect specification.
6467
6468 if From_Aspect_Specification (N) then
6469 if not Is_Task_Type (U_Ent) then
6470 Error_Msg_N
6471 ("Dispatching_Domain can only be defined for task", Nam);
6472
6473 elsif Duplicate_Clause then
6474 null;
6475
6476 else
6477 -- The expression must be analyzed in the special manner
6478 -- described in "Handling of Default and Per-Object
6479 -- Expressions" in sem.ads.
6480
6481 -- The visibility to the components must be restored
6482
6483 Push_Type (U_Ent);
6484
6485 Preanalyze_Spec_Expression
6486 (Expr, RTE (RE_Dispatching_Domain));
6487
6488 Pop_Type (U_Ent);
6489 end if;
6490
6491 else
6492 Error_Msg_N
6493 ("attribute& cannot be set with definition clause", N);
6494 end if;
6495
6496 ------------------
6497 -- External_Tag --
6498 ------------------
6499
6500 when Attribute_External_Tag =>
6501 if not Is_Tagged_Type (U_Ent) then
6502 Error_Msg_N ("should be a tagged type", Nam);
6503 end if;
6504
6505 if Duplicate_Clause then
6506 null;
6507
6508 else
6509 Analyze_And_Resolve (Expr, Standard_String);
6510
6511 if not Is_OK_Static_Expression (Expr) then
6512 Flag_Non_Static_Expr
6513 ("static string required for tag name!", Nam);
6514 end if;
6515
6516 if not Is_Library_Level_Entity (U_Ent) then
6517 Error_Msg_NE
6518 ("??non-unique external tag supplied for &", N, U_Ent);
6519 Error_Msg_N
6520 ("\??same external tag applies to all subprogram calls",
6521 N);
6522 Error_Msg_N
6523 ("\??corresponding internal tag cannot be obtained", N);
6524 end if;
6525 end if;
6526
6527 --------------------------
6528 -- Implicit_Dereference --
6529 --------------------------
6530
6531 when Attribute_Implicit_Dereference =>
6532
6533 -- Legality checks already performed at the point of the type
6534 -- declaration, aspect is not delayed.
6535
6536 null;
6537
6538 -----------
6539 -- Input --
6540 -----------
6541
6542 when Attribute_Input =>
6543 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
6544 Set_Has_Specified_Stream_Input (Ent);
6545
6546 ------------------------
6547 -- Interrupt_Priority --
6548 ------------------------
6549
6550 when Attribute_Interrupt_Priority =>
6551
6552 -- Interrupt_Priority attribute definition clause not allowed
6553 -- except from aspect specification.
6554
6555 if From_Aspect_Specification (N) then
6556 if not Is_Concurrent_Type (U_Ent) then
6557 Error_Msg_N
6558 ("Interrupt_Priority can only be defined for task and "
6559 & "protected object", Nam);
6560
6561 elsif Duplicate_Clause then
6562 null;
6563
6564 else
6565 -- The expression must be analyzed in the special manner
6566 -- described in "Handling of Default and Per-Object
6567 -- Expressions" in sem.ads.
6568
6569 -- The visibility to the components must be restored
6570
6571 Push_Type (U_Ent);
6572
6573 Preanalyze_Spec_Expression
6574 (Expr, RTE (RE_Interrupt_Priority));
6575
6576 Pop_Type (U_Ent);
6577
6578 -- Check the No_Task_At_Interrupt_Priority restriction
6579
6580 if Is_Task_Type (U_Ent) then
6581 Check_Restriction (No_Task_At_Interrupt_Priority, N);
6582 end if;
6583 end if;
6584
6585 else
6586 Error_Msg_N
6587 ("attribute& cannot be set with definition clause", N);
6588 end if;
6589
6590 --------------
6591 -- Iterable --
6592 --------------
6593
6594 when Attribute_Iterable =>
6595 Analyze (Expr);
6596
6597 if Nkind (Expr) /= N_Aggregate then
6598 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
6599 end if;
6600
6601 declare
6602 Assoc : Node_Id;
6603
6604 begin
6605 Assoc := First (Component_Associations (Expr));
6606 while Present (Assoc) loop
6607 Analyze (Expression (Assoc));
6608
6609 if not Is_Entity_Name (Expression (Assoc)) then
6610 Error_Msg_N ("value must be a function", Assoc);
6611 end if;
6612
6613 Next (Assoc);
6614 end loop;
6615 end;
6616
6617 ----------------------
6618 -- Iterator_Element --
6619 ----------------------
6620
6621 when Attribute_Iterator_Element =>
6622 Analyze (Expr);
6623
6624 if not Is_Entity_Name (Expr)
6625 or else not Is_Type (Entity (Expr))
6626 then
6627 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
6628 end if;
6629
6630 -------------------
6631 -- Machine_Radix --
6632 -------------------
6633
6634 -- Machine radix attribute definition clause
6635
6636 when Attribute_Machine_Radix => Machine_Radix : declare
6637 Radix : constant Uint := Static_Integer (Expr);
6638
6639 begin
6640 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
6641 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
6642
6643 elsif Duplicate_Clause then
6644 null;
6645
6646 elsif Radix /= No_Uint then
6647 Set_Has_Machine_Radix_Clause (U_Ent);
6648 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
6649
6650 if Radix = 2 then
6651 null;
6652
6653 elsif Radix = 10 then
6654 Set_Machine_Radix_10 (U_Ent);
6655
6656 else
6657 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
6658 end if;
6659 end if;
6660 end Machine_Radix;
6661
6662 -----------------
6663 -- Object_Size --
6664 -----------------
6665
6666 -- Object_Size attribute definition clause
6667
6668 when Attribute_Object_Size => Object_Size : declare
6669 Size : constant Uint := Static_Integer (Expr);
6670
6671 Biased : Boolean;
6672 pragma Warnings (Off, Biased);
6673
6674 begin
6675 if not Is_Type (U_Ent) then
6676 Error_Msg_N ("Object_Size cannot be given for &", Nam);
6677
6678 elsif Duplicate_Clause then
6679 null;
6680
6681 else
6682 Check_Size (Expr, U_Ent, Size, Biased);
6683
6684 if Size <= 0 then
6685 Error_Msg_N ("Object_Size must be positive", Expr);
6686
6687 elsif Is_Scalar_Type (U_Ent) then
6688 if Size /= 8 and then Size /= 16 and then Size /= 32
6689 and then UI_Mod (Size, 64) /= 0
6690 then
6691 Error_Msg_N
6692 ("Object_Size must be 8, 16, 32, or multiple of 64",
6693 Expr);
6694 end if;
6695
6696 elsif Size mod 8 /= 0 then
6697 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
6698 end if;
6699
6700 Set_Esize (U_Ent, Size);
6701 Set_Has_Object_Size_Clause (U_Ent);
6702 Alignment_Check_For_Size_Change (U_Ent, Size);
6703 end if;
6704 end Object_Size;
6705
6706 ------------
6707 -- Output --
6708 ------------
6709
6710 when Attribute_Output =>
6711 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
6712 Set_Has_Specified_Stream_Output (Ent);
6713
6714 --------------
6715 -- Priority --
6716 --------------
6717
6718 when Attribute_Priority =>
6719
6720 -- Priority attribute definition clause not allowed except from
6721 -- aspect specification.
6722
6723 if From_Aspect_Specification (N) then
6724 if not (Is_Concurrent_Type (U_Ent)
6725 or else Ekind (U_Ent) = E_Procedure)
6726 then
6727 Error_Msg_N
6728 ("Priority can only be defined for task and protected "
6729 & "object", Nam);
6730
6731 elsif Duplicate_Clause then
6732 null;
6733
6734 else
6735 -- The expression must be analyzed in the special manner
6736 -- described in "Handling of Default and Per-Object
6737 -- Expressions" in sem.ads.
6738
6739 -- The visibility to the components must be restored
6740
6741 Push_Type (U_Ent);
6742 Preanalyze_Spec_Expression (Expr, Standard_Integer);
6743 Pop_Type (U_Ent);
6744
6745 if not Is_OK_Static_Expression (Expr) then
6746 Check_Restriction (Static_Priorities, Expr);
6747 end if;
6748 end if;
6749
6750 else
6751 Error_Msg_N
6752 ("attribute& cannot be set with definition clause", N);
6753 end if;
6754
6755 ---------------
6756 -- Put_Image --
6757 ---------------
6758
6759 when Attribute_Put_Image =>
6760 Analyze_Put_Image_TSS_Definition;
6761
6762 ----------
6763 -- Read --
6764 ----------
6765
6766 when Attribute_Read =>
6767 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
6768 Set_Has_Specified_Stream_Read (Ent);
6769
6770 --------------------------
6771 -- Scalar_Storage_Order --
6772 --------------------------
6773
6774 -- Scalar_Storage_Order attribute definition clause
6775
6776 when Attribute_Scalar_Storage_Order =>
6777 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
6778 Error_Msg_N
6779 ("Scalar_Storage_Order can only be defined for record or "
6780 & "array type", Nam);
6781
6782 elsif Duplicate_Clause then
6783 null;
6784
6785 else
6786 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
6787
6788 if Etype (Expr) = Any_Type then
6789 return;
6790
6791 elsif not Is_OK_Static_Expression (Expr) then
6792 Flag_Non_Static_Expr
6793 ("Scalar_Storage_Order requires static expression!", Expr);
6794
6795 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
6796
6797 -- Here for the case of a non-default (i.e. non-confirming)
6798 -- Scalar_Storage_Order attribute definition.
6799
6800 if Support_Nondefault_SSO_On_Target then
6801 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
6802 else
6803 Error_Msg_N
6804 ("non-default Scalar_Storage_Order not supported on "
6805 & "target", Expr);
6806 end if;
6807 end if;
6808
6809 -- Clear SSO default indications since explicit setting of the
6810 -- order overrides the defaults.
6811
6812 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
6813 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
6814 end if;
6815
6816 ----------
6817 -- Size --
6818 ----------
6819
6820 -- Size attribute definition clause
6821
6822 when Attribute_Size => Size : declare
6823 Size : constant Uint := Static_Integer (Expr);
6824 Etyp : Entity_Id;
6825 Biased : Boolean;
6826
6827 begin
6828 FOnly := True;
6829
6830 if Duplicate_Clause then
6831 null;
6832
6833 elsif not Is_Type (U_Ent)
6834 and then Ekind (U_Ent) /= E_Variable
6835 and then Ekind (U_Ent) /= E_Constant
6836 then
6837 Error_Msg_N ("size cannot be given for &", Nam);
6838
6839 elsif Is_Array_Type (U_Ent)
6840 and then not Is_Constrained (U_Ent)
6841 then
6842 Error_Msg_N
6843 ("size cannot be given for unconstrained array", Nam);
6844
6845 elsif Size /= No_Uint then
6846 if Is_Type (U_Ent) then
6847 Etyp := U_Ent;
6848 else
6849 Etyp := Etype (U_Ent);
6850 end if;
6851
6852 -- Check size, note that Gigi is in charge of checking that the
6853 -- size of an array or record type is OK. Also we do not check
6854 -- the size in the ordinary fixed-point case, since it is too
6855 -- early to do so (there may be subsequent small clause that
6856 -- affects the size). We can check the size if a small clause
6857 -- has already been given.
6858
6859 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
6860 or else Has_Small_Clause (U_Ent)
6861 then
6862 Check_Size (Expr, Etyp, Size, Biased);
6863 Set_Biased (U_Ent, N, "size clause", Biased);
6864 end if;
6865
6866 -- For types set RM_Size and Esize if possible
6867
6868 if Is_Type (U_Ent) then
6869 Set_RM_Size (U_Ent, Size);
6870
6871 -- For elementary types, increase Object_Size to power of 2,
6872 -- but not less than a storage unit in any case (normally
6873 -- this means it will be byte addressable).
6874
6875 -- For all other types, nothing else to do, we leave Esize
6876 -- (object size) unset, the back end will set it from the
6877 -- size and alignment in an appropriate manner.
6878
6879 -- In both cases, we check whether the alignment must be
6880 -- reset in the wake of the size change.
6881
6882 if Is_Elementary_Type (U_Ent) then
6883 if Size <= System_Storage_Unit then
6884 Init_Esize (U_Ent, System_Storage_Unit);
6885 elsif Size <= 16 then
6886 Init_Esize (U_Ent, 16);
6887 elsif Size <= 32 then
6888 Init_Esize (U_Ent, 32);
6889 else
6890 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
6891 end if;
6892
6893 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
6894 else
6895 Alignment_Check_For_Size_Change (U_Ent, Size);
6896 end if;
6897
6898 -- For objects, set Esize only
6899
6900 else
6901 if Is_Elementary_Type (Etyp)
6902 and then Size /= System_Storage_Unit
6903 and then Size /= System_Storage_Unit * 2
6904 and then Size /= System_Storage_Unit * 4
6905 and then Size /= System_Storage_Unit * 8
6906 then
6907 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6908 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
6909 Error_Msg_N
6910 ("size for primitive object must be a power of 2 in "
6911 & "the range ^-^", N);
6912 end if;
6913
6914 Set_Esize (U_Ent, Size);
6915 end if;
6916
6917 Set_Has_Size_Clause (U_Ent);
6918 end if;
6919 end Size;
6920
6921 -----------
6922 -- Small --
6923 -----------
6924
6925 -- Small attribute definition clause
6926
6927 when Attribute_Small => Small : declare
6928 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
6929 Small : Ureal;
6930
6931 begin
6932 Analyze_And_Resolve (Expr, Any_Real);
6933
6934 if Etype (Expr) = Any_Type then
6935 return;
6936
6937 elsif not Is_OK_Static_Expression (Expr) then
6938 Flag_Non_Static_Expr
6939 ("small requires static expression!", Expr);
6940 return;
6941
6942 else
6943 Small := Expr_Value_R (Expr);
6944
6945 if Small <= Ureal_0 then
6946 Error_Msg_N ("small value must be greater than zero", Expr);
6947 return;
6948 end if;
6949
6950 end if;
6951
6952 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
6953 Error_Msg_N
6954 ("small requires an ordinary fixed point type", Nam);
6955
6956 elsif Has_Small_Clause (U_Ent) then
6957 Error_Msg_N ("small already given for &", Nam);
6958
6959 elsif Small > Delta_Value (U_Ent) then
6960 Error_Msg_N
6961 ("small value must not be greater than delta value", Nam);
6962
6963 else
6964 Set_Small_Value (U_Ent, Small);
6965 Set_Small_Value (Implicit_Base, Small);
6966 Set_Has_Small_Clause (U_Ent);
6967 Set_Has_Small_Clause (Implicit_Base);
6968 Set_Has_Non_Standard_Rep (Implicit_Base);
6969 end if;
6970 end Small;
6971
6972 ------------------
6973 -- Storage_Pool --
6974 ------------------
6975
6976 -- Storage_Pool attribute definition clause
6977
6978 when Attribute_Simple_Storage_Pool
6979 | Attribute_Storage_Pool
6980 =>
6981 Storage_Pool : declare
6982 Pool : Entity_Id;
6983 T : Entity_Id;
6984
6985 begin
6986 if Ekind (U_Ent) = E_Access_Subprogram_Type then
6987 Error_Msg_N
6988 ("storage pool cannot be given for access-to-subprogram type",
6989 Nam);
6990 return;
6991
6992 elsif not Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
6993 then
6994 Error_Msg_N
6995 ("storage pool can only be given for access types", Nam);
6996 return;
6997
6998 elsif Is_Derived_Type (U_Ent) then
6999 Error_Msg_N
7000 ("storage pool cannot be given for a derived access type",
7001 Nam);
7002
7003 elsif Duplicate_Clause then
7004 return;
7005
7006 elsif Present (Associated_Storage_Pool (U_Ent)) then
7007 Error_Msg_N ("storage pool already given for &", Nam);
7008 return;
7009 end if;
7010
7011 -- Check for Storage_Size previously given
7012
7013 declare
7014 SS : constant Node_Id :=
7015 Get_Attribute_Definition_Clause
7016 (U_Ent, Attribute_Storage_Size);
7017 begin
7018 if Present (SS) then
7019 Check_Pool_Size_Clash (U_Ent, N, SS);
7020 end if;
7021 end;
7022
7023 -- Storage_Pool case
7024
7025 if Id = Attribute_Storage_Pool then
7026 Analyze_And_Resolve
7027 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
7028
7029 -- In the Simple_Storage_Pool case, we allow a variable of any
7030 -- simple storage pool type, so we Resolve without imposing an
7031 -- expected type.
7032
7033 else
7034 Analyze_And_Resolve (Expr);
7035
7036 if not Present (Get_Rep_Pragma
7037 (Etype (Expr), Name_Simple_Storage_Pool_Type))
7038 then
7039 Error_Msg_N
7040 ("expression must be of a simple storage pool type", Expr);
7041 end if;
7042 end if;
7043
7044 if not Denotes_Variable (Expr) then
7045 Error_Msg_N ("storage pool must be a variable", Expr);
7046 return;
7047 end if;
7048
7049 if Nkind (Expr) = N_Type_Conversion then
7050 T := Etype (Expression (Expr));
7051 else
7052 T := Etype (Expr);
7053 end if;
7054
7055 -- The Stack_Bounded_Pool is used internally for implementing
7056 -- access types with a Storage_Size. Since it only work properly
7057 -- when used on one specific type, we need to check that it is not
7058 -- hijacked improperly:
7059
7060 -- type T is access Integer;
7061 -- for T'Storage_Size use n;
7062 -- type Q is access Float;
7063 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
7064
7065 if RTE_Available (RE_Stack_Bounded_Pool)
7066 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
7067 then
7068 Error_Msg_N ("non-shareable internal Pool", Expr);
7069 return;
7070 end if;
7071
7072 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7073 -- Storage_Pool since this attribute cannot be defined for such
7074 -- types (RM E.2.2(17)).
7075
7076 Validate_Remote_Access_To_Class_Wide_Type (N);
7077
7078 -- If the argument is a name that is not an entity name, then
7079 -- we construct a renaming operation to define an entity of
7080 -- type storage pool.
7081
7082 if not Is_Entity_Name (Expr)
7083 and then Is_Object_Reference (Expr)
7084 then
7085 Pool := Make_Temporary (Loc, 'P', Expr);
7086
7087 declare
7088 Rnode : constant Node_Id :=
7089 Make_Object_Renaming_Declaration (Loc,
7090 Defining_Identifier => Pool,
7091 Subtype_Mark =>
7092 New_Occurrence_Of (Etype (Expr), Loc),
7093 Name => Expr);
7094
7095 begin
7096 -- If the attribute definition clause comes from an aspect
7097 -- clause, then insert the renaming before the associated
7098 -- entity's declaration, since the attribute clause has
7099 -- not yet been appended to the declaration list.
7100
7101 if From_Aspect_Specification (N) then
7102 Insert_Before (Parent (Entity (N)), Rnode);
7103 else
7104 Insert_Before (N, Rnode);
7105 end if;
7106
7107 Analyze (Rnode);
7108 Set_Associated_Storage_Pool (U_Ent, Pool);
7109 end;
7110
7111 elsif Is_Entity_Name (Expr) then
7112 Pool := Entity (Expr);
7113
7114 -- If pool is a renamed object, get original one. This can
7115 -- happen with an explicit renaming, and within instances.
7116
7117 while Present (Renamed_Object (Pool))
7118 and then Is_Entity_Name (Renamed_Object (Pool))
7119 loop
7120 Pool := Entity (Renamed_Object (Pool));
7121 end loop;
7122
7123 if Present (Renamed_Object (Pool))
7124 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
7125 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
7126 then
7127 Pool := Entity (Expression (Renamed_Object (Pool)));
7128 end if;
7129
7130 Set_Associated_Storage_Pool (U_Ent, Pool);
7131
7132 elsif Nkind (Expr) = N_Type_Conversion
7133 and then Is_Entity_Name (Expression (Expr))
7134 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
7135 then
7136 Pool := Entity (Expression (Expr));
7137 Set_Associated_Storage_Pool (U_Ent, Pool);
7138
7139 else
7140 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
7141 return;
7142 end if;
7143 end Storage_Pool;
7144
7145 ------------------
7146 -- Storage_Size --
7147 ------------------
7148
7149 -- Storage_Size attribute definition clause
7150
7151 when Attribute_Storage_Size => Storage_Size : declare
7152 Btype : constant Entity_Id := Base_Type (U_Ent);
7153
7154 begin
7155 if Is_Task_Type (U_Ent) then
7156
7157 -- Check obsolescent (but never obsolescent if from aspect)
7158
7159 if not From_Aspect_Specification (N) then
7160 Check_Restriction (No_Obsolescent_Features, N);
7161
7162 if Warn_On_Obsolescent_Feature then
7163 Error_Msg_N
7164 ("?j?storage size clause for task is an obsolescent "
7165 & "feature (RM J.9)", N);
7166 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
7167 end if;
7168 end if;
7169
7170 FOnly := True;
7171 end if;
7172
7173 if not Is_Access_Type (U_Ent)
7174 and then Ekind (U_Ent) /= E_Task_Type
7175 then
7176 Error_Msg_N ("storage size cannot be given for &", Nam);
7177
7178 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
7179 Error_Msg_N
7180 ("storage size cannot be given for a derived access type",
7181 Nam);
7182
7183 elsif Duplicate_Clause then
7184 null;
7185
7186 else
7187 -- Validate_Remote_Access_To_Class_Wide_Type for attribute
7188 -- Storage_Size since this attribute cannot be defined for such
7189 -- types (RM E.2.2(17)).
7190
7191 Validate_Remote_Access_To_Class_Wide_Type (N);
7192
7193 Analyze_And_Resolve (Expr, Any_Integer);
7194
7195 if Is_Access_Type (U_Ent) then
7196
7197 -- Check for Storage_Pool previously given
7198
7199 declare
7200 SP : constant Node_Id :=
7201 Get_Attribute_Definition_Clause
7202 (U_Ent, Attribute_Storage_Pool);
7203
7204 begin
7205 if Present (SP) then
7206 Check_Pool_Size_Clash (U_Ent, SP, N);
7207 end if;
7208 end;
7209
7210 -- Special case of for x'Storage_Size use 0
7211
7212 if Is_OK_Static_Expression (Expr)
7213 and then Expr_Value (Expr) = 0
7214 then
7215 Set_No_Pool_Assigned (Btype);
7216 end if;
7217 end if;
7218
7219 Set_Has_Storage_Size_Clause (Btype);
7220 end if;
7221 end Storage_Size;
7222
7223 -----------------
7224 -- Stream_Size --
7225 -----------------
7226
7227 when Attribute_Stream_Size => Stream_Size : declare
7228 Size : constant Uint := Static_Integer (Expr);
7229
7230 begin
7231 if Ada_Version <= Ada_95 then
7232 Check_Restriction (No_Implementation_Attributes, N);
7233 end if;
7234
7235 if Duplicate_Clause then
7236 null;
7237
7238 elsif Is_Elementary_Type (U_Ent) then
7239 if Size /= System_Storage_Unit
7240 and then Size /= System_Storage_Unit * 2
7241 and then Size /= System_Storage_Unit * 3
7242 and then Size /= System_Storage_Unit * 4
7243 and then Size /= System_Storage_Unit * 8
7244 then
7245 Error_Msg_N
7246 ("stream size for elementary type must be 8, 16, 24, " &
7247 "32 or 64", N);
7248
7249 elsif RM_Size (U_Ent) > Size then
7250 Error_Msg_Uint_1 := RM_Size (U_Ent);
7251 Error_Msg_N
7252 ("stream size for elementary type must be 8, 16, 24, " &
7253 "32 or 64 and at least ^", N);
7254 end if;
7255
7256 Set_Has_Stream_Size_Clause (U_Ent);
7257
7258 else
7259 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
7260 end if;
7261 end Stream_Size;
7262
7263 ----------------
7264 -- Value_Size --
7265 ----------------
7266
7267 -- Value_Size attribute definition clause
7268
7269 when Attribute_Value_Size => Value_Size : declare
7270 Size : constant Uint := Static_Integer (Expr);
7271 Biased : Boolean;
7272
7273 begin
7274 if not Is_Type (U_Ent) then
7275 Error_Msg_N ("Value_Size cannot be given for &", Nam);
7276
7277 elsif Duplicate_Clause then
7278 null;
7279
7280 elsif Is_Array_Type (U_Ent)
7281 and then not Is_Constrained (U_Ent)
7282 then
7283 Error_Msg_N
7284 ("Value_Size cannot be given for unconstrained array", Nam);
7285
7286 else
7287 if Is_Elementary_Type (U_Ent) then
7288 Check_Size (Expr, U_Ent, Size, Biased);
7289 Set_Biased (U_Ent, N, "value size clause", Biased);
7290 end if;
7291
7292 Set_RM_Size (U_Ent, Size);
7293 end if;
7294 end Value_Size;
7295
7296 -----------------------
7297 -- Variable_Indexing --
7298 -----------------------
7299
7300 when Attribute_Variable_Indexing =>
7301 Check_Indexing_Functions;
7302
7303 -----------
7304 -- Write --
7305 -----------
7306
7307 when Attribute_Write =>
7308 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
7309 Set_Has_Specified_Stream_Write (Ent);
7310
7311 -- All other attributes cannot be set
7312
7313 when others =>
7314 Error_Msg_N
7315 ("attribute& cannot be set with definition clause", N);
7316 end case;
7317
7318 -- The test for the type being frozen must be performed after any
7319 -- expression the clause has been analyzed since the expression itself
7320 -- might cause freezing that makes the clause illegal.
7321
7322 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
7323 return;
7324 end if;
7325 end Analyze_Attribute_Definition_Clause;
7326
7327 ----------------------------
7328 -- Analyze_Code_Statement --
7329 ----------------------------
7330
7331 procedure Analyze_Code_Statement (N : Node_Id) is
7332 HSS : constant Node_Id := Parent (N);
7333 SBody : constant Node_Id := Parent (HSS);
7334 Subp : constant Entity_Id := Current_Scope;
7335 Stmt : Node_Id;
7336 Decl : Node_Id;
7337 StmtO : Node_Id;
7338 DeclO : Node_Id;
7339
7340 begin
7341 -- Accept foreign code statements for CodePeer. The analysis is skipped
7342 -- to avoid rejecting unrecognized constructs.
7343
7344 if CodePeer_Mode then
7345 Set_Analyzed (N);
7346 return;
7347 end if;
7348
7349 -- Analyze and check we get right type, note that this implements the
7350 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
7351 -- the only way that Asm_Insn could possibly be visible.
7352
7353 Analyze_And_Resolve (Expression (N));
7354
7355 if Etype (Expression (N)) = Any_Type then
7356 return;
7357 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
7358 Error_Msg_N ("incorrect type for code statement", N);
7359 return;
7360 end if;
7361
7362 Check_Code_Statement (N);
7363
7364 -- Make sure we appear in the handled statement sequence of a subprogram
7365 -- (RM 13.8(3)).
7366
7367 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
7368 or else Nkind (SBody) /= N_Subprogram_Body
7369 then
7370 Error_Msg_N
7371 ("code statement can only appear in body of subprogram", N);
7372 return;
7373 end if;
7374
7375 -- Do remaining checks (RM 13.8(3)) if not already done
7376
7377 if not Is_Machine_Code_Subprogram (Subp) then
7378 Set_Is_Machine_Code_Subprogram (Subp);
7379
7380 -- No exception handlers allowed
7381
7382 if Present (Exception_Handlers (HSS)) then
7383 Error_Msg_N
7384 ("exception handlers not permitted in machine code subprogram",
7385 First (Exception_Handlers (HSS)));
7386 end if;
7387
7388 -- No declarations other than use clauses and pragmas (we allow
7389 -- certain internally generated declarations as well).
7390
7391 Decl := First (Declarations (SBody));
7392 while Present (Decl) loop
7393 DeclO := Original_Node (Decl);
7394 if Comes_From_Source (DeclO)
7395 and not Nkind_In (DeclO, N_Pragma,
7396 N_Use_Package_Clause,
7397 N_Use_Type_Clause,
7398 N_Implicit_Label_Declaration)
7399 then
7400 Error_Msg_N
7401 ("this declaration not allowed in machine code subprogram",
7402 DeclO);
7403 end if;
7404
7405 Next (Decl);
7406 end loop;
7407
7408 -- No statements other than code statements, pragmas, and labels.
7409 -- Again we allow certain internally generated statements.
7410
7411 -- In Ada 2012, qualified expressions are names, and the code
7412 -- statement is initially parsed as a procedure call.
7413
7414 Stmt := First (Statements (HSS));
7415 while Present (Stmt) loop
7416 StmtO := Original_Node (Stmt);
7417
7418 -- A procedure call transformed into a code statement is OK
7419
7420 if Ada_Version >= Ada_2012
7421 and then Nkind (StmtO) = N_Procedure_Call_Statement
7422 and then Nkind (Name (StmtO)) = N_Qualified_Expression
7423 then
7424 null;
7425
7426 elsif Comes_From_Source (StmtO)
7427 and then not Nkind_In (StmtO, N_Pragma,
7428 N_Label,
7429 N_Code_Statement)
7430 then
7431 Error_Msg_N
7432 ("this statement is not allowed in machine code subprogram",
7433 StmtO);
7434 end if;
7435
7436 Next (Stmt);
7437 end loop;
7438 end if;
7439 end Analyze_Code_Statement;
7440
7441 -----------------------------------------------
7442 -- Analyze_Enumeration_Representation_Clause --
7443 -----------------------------------------------
7444
7445 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
7446 Ident : constant Node_Id := Identifier (N);
7447 Aggr : constant Node_Id := Array_Aggregate (N);
7448 Enumtype : Entity_Id;
7449 Elit : Entity_Id;
7450 Expr : Node_Id;
7451 Assoc : Node_Id;
7452 Choice : Node_Id;
7453 Val : Uint;
7454
7455 Err : Boolean := False;
7456 -- Set True to avoid cascade errors and crashes on incorrect source code
7457
7458 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
7459 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
7460 -- Allowed range of universal integer (= allowed range of enum lit vals)
7461
7462 Min : Uint;
7463 Max : Uint;
7464 -- Minimum and maximum values of entries
7465
7466 Max_Node : Node_Id := Empty; -- init to avoid warning
7467 -- Pointer to node for literal providing max value
7468
7469 begin
7470 if Ignore_Rep_Clauses then
7471 Kill_Rep_Clause (N);
7472 return;
7473 end if;
7474
7475 -- Ignore enumeration rep clauses by default in CodePeer mode,
7476 -- unless -gnatd.I is specified, as a work around for potential false
7477 -- positive messages.
7478
7479 if CodePeer_Mode and not Debug_Flag_Dot_II then
7480 return;
7481 end if;
7482
7483 -- First some basic error checks
7484
7485 Find_Type (Ident);
7486 Enumtype := Entity (Ident);
7487
7488 if Enumtype = Any_Type
7489 or else Rep_Item_Too_Early (Enumtype, N)
7490 then
7491 return;
7492 else
7493 Enumtype := Underlying_Type (Enumtype);
7494 end if;
7495
7496 if not Is_Enumeration_Type (Enumtype) then
7497 Error_Msg_NE
7498 ("enumeration type required, found}",
7499 Ident, First_Subtype (Enumtype));
7500 return;
7501 end if;
7502
7503 -- Ignore rep clause on generic actual type. This will already have
7504 -- been flagged on the template as an error, and this is the safest
7505 -- way to ensure we don't get a junk cascaded message in the instance.
7506
7507 if Is_Generic_Actual_Type (Enumtype) then
7508 return;
7509
7510 -- Type must be in current scope
7511
7512 elsif Scope (Enumtype) /= Current_Scope then
7513 Error_Msg_N ("type must be declared in this scope", Ident);
7514 return;
7515
7516 -- Type must be a first subtype
7517
7518 elsif not Is_First_Subtype (Enumtype) then
7519 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
7520 return;
7521
7522 -- Ignore duplicate rep clause
7523
7524 elsif Has_Enumeration_Rep_Clause (Enumtype) then
7525 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
7526 return;
7527
7528 -- Don't allow rep clause for standard [wide_[wide_]]character
7529
7530 elsif Is_Standard_Character_Type (Enumtype) then
7531 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
7532 return;
7533
7534 -- Check that the expression is a proper aggregate (no parentheses)
7535
7536 elsif Paren_Count (Aggr) /= 0 then
7537 Error_Msg
7538 ("extra parentheses surrounding aggregate not allowed",
7539 First_Sloc (Aggr));
7540 return;
7541
7542 -- All tests passed, so set rep clause in place
7543
7544 else
7545 Set_Has_Enumeration_Rep_Clause (Enumtype);
7546 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
7547 end if;
7548
7549 -- Now we process the aggregate. Note that we don't use the normal
7550 -- aggregate code for this purpose, because we don't want any of the
7551 -- normal expansion activities, and a number of special semantic
7552 -- rules apply (including the component type being any integer type)
7553
7554 Elit := First_Literal (Enumtype);
7555
7556 -- First the positional entries if any
7557
7558 if Present (Expressions (Aggr)) then
7559 Expr := First (Expressions (Aggr));
7560 while Present (Expr) loop
7561 if No (Elit) then
7562 Error_Msg_N ("too many entries in aggregate", Expr);
7563 return;
7564 end if;
7565
7566 Val := Static_Integer (Expr);
7567
7568 -- Err signals that we found some incorrect entries processing
7569 -- the list. The final checks for completeness and ordering are
7570 -- skipped in this case.
7571
7572 if Val = No_Uint then
7573 Err := True;
7574
7575 elsif Val < Lo or else Hi < Val then
7576 Error_Msg_N ("value outside permitted range", Expr);
7577 Err := True;
7578 end if;
7579
7580 Set_Enumeration_Rep (Elit, Val);
7581 Set_Enumeration_Rep_Expr (Elit, Expr);
7582 Next (Expr);
7583 Next (Elit);
7584 end loop;
7585 end if;
7586
7587 -- Now process the named entries if present
7588
7589 if Present (Component_Associations (Aggr)) then
7590 Assoc := First (Component_Associations (Aggr));
7591 while Present (Assoc) loop
7592 Choice := First (Choices (Assoc));
7593
7594 if Present (Next (Choice)) then
7595 Error_Msg_N
7596 ("multiple choice not allowed here", Next (Choice));
7597 Err := True;
7598 end if;
7599
7600 if Nkind (Choice) = N_Others_Choice then
7601 Error_Msg_N ("others choice not allowed here", Choice);
7602 Err := True;
7603
7604 elsif Nkind (Choice) = N_Range then
7605
7606 -- ??? should allow zero/one element range here
7607
7608 Error_Msg_N ("range not allowed here", Choice);
7609 Err := True;
7610
7611 else
7612 Analyze_And_Resolve (Choice, Enumtype);
7613
7614 if Error_Posted (Choice) then
7615 Err := True;
7616 end if;
7617
7618 if not Err then
7619 if Is_Entity_Name (Choice)
7620 and then Is_Type (Entity (Choice))
7621 then
7622 Error_Msg_N ("subtype name not allowed here", Choice);
7623 Err := True;
7624
7625 -- ??? should allow static subtype with zero/one entry
7626
7627 elsif Etype (Choice) = Base_Type (Enumtype) then
7628 if not Is_OK_Static_Expression (Choice) then
7629 Flag_Non_Static_Expr
7630 ("non-static expression used for choice!", Choice);
7631 Err := True;
7632
7633 else
7634 Elit := Expr_Value_E (Choice);
7635
7636 if Present (Enumeration_Rep_Expr (Elit)) then
7637 Error_Msg_Sloc :=
7638 Sloc (Enumeration_Rep_Expr (Elit));
7639 Error_Msg_NE
7640 ("representation for& previously given#",
7641 Choice, Elit);
7642 Err := True;
7643 end if;
7644
7645 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
7646
7647 Expr := Expression (Assoc);
7648 Val := Static_Integer (Expr);
7649
7650 if Val = No_Uint then
7651 Err := True;
7652
7653 elsif Val < Lo or else Hi < Val then
7654 Error_Msg_N ("value outside permitted range", Expr);
7655 Err := True;
7656 end if;
7657
7658 Set_Enumeration_Rep (Elit, Val);
7659 end if;
7660 end if;
7661 end if;
7662 end if;
7663
7664 Next (Assoc);
7665 end loop;
7666 end if;
7667
7668 -- Aggregate is fully processed. Now we check that a full set of
7669 -- representations was given, and that they are in range and in order.
7670 -- These checks are only done if no other errors occurred.
7671
7672 if not Err then
7673 Min := No_Uint;
7674 Max := No_Uint;
7675
7676 Elit := First_Literal (Enumtype);
7677 while Present (Elit) loop
7678 if No (Enumeration_Rep_Expr (Elit)) then
7679 Error_Msg_NE ("missing representation for&!", N, Elit);
7680
7681 else
7682 Val := Enumeration_Rep (Elit);
7683
7684 if Min = No_Uint then
7685 Min := Val;
7686 end if;
7687
7688 if Val /= No_Uint then
7689 if Max /= No_Uint and then Val <= Max then
7690 Error_Msg_NE
7691 ("enumeration value for& not ordered!",
7692 Enumeration_Rep_Expr (Elit), Elit);
7693 end if;
7694
7695 Max_Node := Enumeration_Rep_Expr (Elit);
7696 Max := Val;
7697 end if;
7698
7699 -- If there is at least one literal whose representation is not
7700 -- equal to the Pos value, then note that this enumeration type
7701 -- has a non-standard representation.
7702
7703 if Val /= Enumeration_Pos (Elit) then
7704 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
7705 end if;
7706 end if;
7707
7708 Next (Elit);
7709 end loop;
7710
7711 -- Now set proper size information
7712
7713 declare
7714 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
7715
7716 begin
7717 if Has_Size_Clause (Enumtype) then
7718
7719 -- All OK, if size is OK now
7720
7721 if RM_Size (Enumtype) >= Minsize then
7722 null;
7723
7724 else
7725 -- Try if we can get by with biasing
7726
7727 Minsize :=
7728 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
7729
7730 -- Error message if even biasing does not work
7731
7732 if RM_Size (Enumtype) < Minsize then
7733 Error_Msg_Uint_1 := RM_Size (Enumtype);
7734 Error_Msg_Uint_2 := Max;
7735 Error_Msg_N
7736 ("previously given size (^) is too small "
7737 & "for this value (^)", Max_Node);
7738
7739 -- If biasing worked, indicate that we now have biased rep
7740
7741 else
7742 Set_Biased
7743 (Enumtype, Size_Clause (Enumtype), "size clause");
7744 end if;
7745 end if;
7746
7747 else
7748 Set_RM_Size (Enumtype, Minsize);
7749 Set_Enum_Esize (Enumtype);
7750 end if;
7751
7752 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
7753 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
7754 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
7755 end;
7756 end if;
7757
7758 -- We repeat the too late test in case it froze itself
7759
7760 if Rep_Item_Too_Late (Enumtype, N) then
7761 null;
7762 end if;
7763 end Analyze_Enumeration_Representation_Clause;
7764
7765 ----------------------------
7766 -- Analyze_Free_Statement --
7767 ----------------------------
7768
7769 procedure Analyze_Free_Statement (N : Node_Id) is
7770 begin
7771 Analyze (Expression (N));
7772 end Analyze_Free_Statement;
7773
7774 ---------------------------
7775 -- Analyze_Freeze_Entity --
7776 ---------------------------
7777
7778 procedure Analyze_Freeze_Entity (N : Node_Id) is
7779 begin
7780 Freeze_Entity_Checks (N);
7781 end Analyze_Freeze_Entity;
7782
7783 -----------------------------------
7784 -- Analyze_Freeze_Generic_Entity --
7785 -----------------------------------
7786
7787 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
7788 E : constant Entity_Id := Entity (N);
7789
7790 begin
7791 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
7792 Analyze_Aspects_At_Freeze_Point (E);
7793 end if;
7794
7795 Freeze_Entity_Checks (N);
7796 end Analyze_Freeze_Generic_Entity;
7797
7798 ------------------------------------------
7799 -- Analyze_Record_Representation_Clause --
7800 ------------------------------------------
7801
7802 -- Note: we check as much as we can here, but we can't do any checks
7803 -- based on the position values (e.g. overlap checks) until freeze time
7804 -- because especially in Ada 2005 (machine scalar mode), the processing
7805 -- for non-standard bit order can substantially change the positions.
7806 -- See procedure Check_Record_Representation_Clause (called from Freeze)
7807 -- for the remainder of this processing.
7808
7809 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
7810 Ident : constant Node_Id := Identifier (N);
7811 Biased : Boolean;
7812 CC : Node_Id;
7813 Comp : Entity_Id;
7814 Fbit : Uint;
7815 Lbit : Uint;
7816 Ocomp : Entity_Id;
7817 Posit : Uint;
7818 Rectype : Entity_Id;
7819 Recdef : Node_Id;
7820
7821 function Is_Inherited (Comp : Entity_Id) return Boolean;
7822 -- True if Comp is an inherited component in a record extension
7823
7824 ------------------
7825 -- Is_Inherited --
7826 ------------------
7827
7828 function Is_Inherited (Comp : Entity_Id) return Boolean is
7829 Comp_Base : Entity_Id;
7830
7831 begin
7832 if Ekind (Rectype) = E_Record_Subtype then
7833 Comp_Base := Original_Record_Component (Comp);
7834 else
7835 Comp_Base := Comp;
7836 end if;
7837
7838 return Comp_Base /= Original_Record_Component (Comp_Base);
7839 end Is_Inherited;
7840
7841 -- Local variables
7842
7843 Is_Record_Extension : Boolean;
7844 -- True if Rectype is a record extension
7845
7846 CR_Pragma : Node_Id := Empty;
7847 -- Points to N_Pragma node if Complete_Representation pragma present
7848
7849 -- Start of processing for Analyze_Record_Representation_Clause
7850
7851 begin
7852 if Ignore_Rep_Clauses then
7853 Kill_Rep_Clause (N);
7854 return;
7855 end if;
7856
7857 Find_Type (Ident);
7858 Rectype := Entity (Ident);
7859
7860 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
7861 return;
7862 else
7863 Rectype := Underlying_Type (Rectype);
7864 end if;
7865
7866 -- First some basic error checks
7867
7868 if not Is_Record_Type (Rectype) then
7869 Error_Msg_NE
7870 ("record type required, found}", Ident, First_Subtype (Rectype));
7871 return;
7872
7873 elsif Scope (Rectype) /= Current_Scope then
7874 Error_Msg_N ("type must be declared in this scope", N);
7875 return;
7876
7877 elsif not Is_First_Subtype (Rectype) then
7878 Error_Msg_N ("cannot give record rep clause for subtype", N);
7879 return;
7880
7881 elsif Has_Record_Rep_Clause (Rectype) then
7882 Error_Msg_N ("duplicate record rep clause ignored", N);
7883 return;
7884
7885 elsif Rep_Item_Too_Late (Rectype, N) then
7886 return;
7887 end if;
7888
7889 -- We know we have a first subtype, now possibly go to the anonymous
7890 -- base type to determine whether Rectype is a record extension.
7891
7892 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
7893 Is_Record_Extension :=
7894 Nkind (Recdef) = N_Derived_Type_Definition
7895 and then Present (Record_Extension_Part (Recdef));
7896
7897 if Present (Mod_Clause (N)) then
7898 declare
7899 M : constant Node_Id := Mod_Clause (N);
7900 P : constant List_Id := Pragmas_Before (M);
7901 Ignore : Uint;
7902
7903 begin
7904 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
7905
7906 if Warn_On_Obsolescent_Feature then
7907 Error_Msg_N
7908 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
7909 Error_Msg_N
7910 ("\?j?use alignment attribute definition clause instead", N);
7911 end if;
7912
7913 if Present (P) then
7914 Analyze_List (P);
7915 end if;
7916
7917 -- Get the alignment value to perform error checking
7918
7919 Ignore := Get_Alignment_Value (Expression (M));
7920 end;
7921 end if;
7922
7923 -- For untagged types, clear any existing component clauses for the
7924 -- type. If the type is derived, this is what allows us to override
7925 -- a rep clause for the parent. For type extensions, the representation
7926 -- of the inherited components is inherited, so we want to keep previous
7927 -- component clauses for completeness.
7928
7929 if not Is_Tagged_Type (Rectype) then
7930 Comp := First_Component_Or_Discriminant (Rectype);
7931 while Present (Comp) loop
7932 Set_Component_Clause (Comp, Empty);
7933 Next_Component_Or_Discriminant (Comp);
7934 end loop;
7935 end if;
7936
7937 -- All done if no component clauses
7938
7939 CC := First (Component_Clauses (N));
7940
7941 if No (CC) then
7942 return;
7943 end if;
7944
7945 -- A representation like this applies to the base type
7946
7947 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
7948 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
7949 Set_Has_Specified_Layout (Base_Type (Rectype));
7950
7951 -- Process the component clauses
7952
7953 while Present (CC) loop
7954
7955 -- Pragma
7956
7957 if Nkind (CC) = N_Pragma then
7958 Analyze (CC);
7959
7960 -- The only pragma of interest is Complete_Representation
7961
7962 if Pragma_Name (CC) = Name_Complete_Representation then
7963 CR_Pragma := CC;
7964 end if;
7965
7966 -- Processing for real component clause
7967
7968 else
7969 Posit := Static_Integer (Position (CC));
7970 Fbit := Static_Integer (First_Bit (CC));
7971 Lbit := Static_Integer (Last_Bit (CC));
7972
7973 if Posit /= No_Uint
7974 and then Fbit /= No_Uint
7975 and then Lbit /= No_Uint
7976 then
7977 if Posit < 0 then
7978 Error_Msg_N ("position cannot be negative", Position (CC));
7979
7980 elsif Fbit < 0 then
7981 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
7982
7983 -- The Last_Bit specified in a component clause must not be
7984 -- less than the First_Bit minus one (RM-13.5.1(10)).
7985
7986 elsif Lbit < Fbit - 1 then
7987 Error_Msg_N
7988 ("last bit cannot be less than first bit minus one",
7989 Last_Bit (CC));
7990
7991 -- Values look OK, so find the corresponding record component
7992 -- Even though the syntax allows an attribute reference for
7993 -- implementation-defined components, GNAT does not allow the
7994 -- tag to get an explicit position.
7995
7996 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
7997 if Attribute_Name (Component_Name (CC)) = Name_Tag then
7998 Error_Msg_N ("position of tag cannot be specified", CC);
7999 else
8000 Error_Msg_N ("illegal component name", CC);
8001 end if;
8002
8003 else
8004 Comp := First_Entity (Rectype);
8005 while Present (Comp) loop
8006 exit when Chars (Comp) = Chars (Component_Name (CC));
8007 Next_Entity (Comp);
8008 end loop;
8009
8010 if No (Comp) then
8011
8012 -- Maybe component of base type that is absent from
8013 -- statically constrained first subtype.
8014
8015 Comp := First_Entity (Base_Type (Rectype));
8016 while Present (Comp) loop
8017 exit when Chars (Comp) = Chars (Component_Name (CC));
8018 Next_Entity (Comp);
8019 end loop;
8020 end if;
8021
8022 if No (Comp) then
8023 Error_Msg_N
8024 ("component clause is for non-existent field", CC);
8025
8026 -- Ada 2012 (AI05-0026): Any name that denotes a
8027 -- discriminant of an object of an unchecked union type
8028 -- shall not occur within a record_representation_clause.
8029
8030 -- The general restriction of using record rep clauses on
8031 -- Unchecked_Union types has now been lifted. Since it is
8032 -- possible to introduce a record rep clause which mentions
8033 -- the discriminant of an Unchecked_Union in non-Ada 2012
8034 -- code, this check is applied to all versions of the
8035 -- language.
8036
8037 elsif Ekind (Comp) = E_Discriminant
8038 and then Is_Unchecked_Union (Rectype)
8039 then
8040 Error_Msg_N
8041 ("cannot reference discriminant of unchecked union",
8042 Component_Name (CC));
8043
8044 elsif Is_Record_Extension and then Is_Inherited (Comp) then
8045 Error_Msg_NE
8046 ("component clause not allowed for inherited "
8047 & "component&", CC, Comp);
8048
8049 elsif Present (Component_Clause (Comp)) then
8050
8051 -- Diagnose duplicate rep clause, or check consistency
8052 -- if this is an inherited component. In a double fault,
8053 -- there may be a duplicate inconsistent clause for an
8054 -- inherited component.
8055
8056 if Scope (Original_Record_Component (Comp)) = Rectype
8057 or else Parent (Component_Clause (Comp)) = N
8058 then
8059 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
8060 Error_Msg_N ("component clause previously given#", CC);
8061
8062 else
8063 declare
8064 Rep1 : constant Node_Id := Component_Clause (Comp);
8065 begin
8066 if Intval (Position (Rep1)) /=
8067 Intval (Position (CC))
8068 or else Intval (First_Bit (Rep1)) /=
8069 Intval (First_Bit (CC))
8070 or else Intval (Last_Bit (Rep1)) /=
8071 Intval (Last_Bit (CC))
8072 then
8073 Error_Msg_N
8074 ("component clause inconsistent with "
8075 & "representation of ancestor", CC);
8076
8077 elsif Warn_On_Redundant_Constructs then
8078 Error_Msg_N
8079 ("?r?redundant confirming component clause "
8080 & "for component!", CC);
8081 end if;
8082 end;
8083 end if;
8084
8085 -- Normal case where this is the first component clause we
8086 -- have seen for this entity, so set it up properly.
8087
8088 else
8089 -- Make reference for field in record rep clause and set
8090 -- appropriate entity field in the field identifier.
8091
8092 Generate_Reference
8093 (Comp, Component_Name (CC), Set_Ref => False);
8094 Set_Entity (Component_Name (CC), Comp);
8095
8096 -- Update Fbit and Lbit to the actual bit number
8097
8098 Fbit := Fbit + UI_From_Int (SSU) * Posit;
8099 Lbit := Lbit + UI_From_Int (SSU) * Posit;
8100
8101 if Has_Size_Clause (Rectype)
8102 and then RM_Size (Rectype) <= Lbit
8103 then
8104 Error_Msg_Uint_1 := RM_Size (Rectype);
8105 Error_Msg_Uint_2 := Lbit + 1;
8106 Error_Msg_N ("bit number out of range of specified "
8107 & "size (expected ^, got ^)",
8108 Last_Bit (CC));
8109 else
8110 Set_Component_Clause (Comp, CC);
8111 Set_Component_Bit_Offset (Comp, Fbit);
8112 Set_Esize (Comp, 1 + (Lbit - Fbit));
8113 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
8114 Set_Normalized_Position (Comp, Fbit / SSU);
8115
8116 Set_Normalized_Position_Max
8117 (Comp, Normalized_Position (Comp));
8118
8119 if Warn_On_Overridden_Size
8120 and then Has_Size_Clause (Etype (Comp))
8121 and then RM_Size (Etype (Comp)) /= Esize (Comp)
8122 then
8123 Error_Msg_NE
8124 ("?S?component size overrides size clause for&",
8125 Component_Name (CC), Etype (Comp));
8126 end if;
8127
8128 Check_Size
8129 (Component_Name (CC),
8130 Etype (Comp),
8131 Esize (Comp),
8132 Biased);
8133
8134 Set_Biased
8135 (Comp, First_Node (CC), "component clause", Biased);
8136
8137 -- This information is also set in the corresponding
8138 -- component of the base type, found by accessing the
8139 -- Original_Record_Component link if it is present.
8140
8141 Ocomp := Original_Record_Component (Comp);
8142
8143 if Present (Ocomp) and then Ocomp /= Comp then
8144 Set_Component_Clause (Ocomp, CC);
8145 Set_Component_Bit_Offset (Ocomp, Fbit);
8146 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
8147 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
8148 Set_Normalized_Position (Ocomp, Fbit / SSU);
8149
8150 Set_Normalized_Position_Max
8151 (Ocomp, Normalized_Position (Ocomp));
8152
8153 -- Note: we don't use Set_Biased here, because we
8154 -- already gave a warning above if needed, and we
8155 -- would get a duplicate for the same name here.
8156
8157 Set_Has_Biased_Representation
8158 (Ocomp, Has_Biased_Representation (Comp));
8159 end if;
8160
8161 if Esize (Comp) < 0 then
8162 Error_Msg_N ("component size is negative", CC);
8163 end if;
8164 end if;
8165 end if;
8166 end if;
8167 end if;
8168 end if;
8169
8170 Next (CC);
8171 end loop;
8172
8173 -- Check missing components if Complete_Representation pragma appeared
8174
8175 if Present (CR_Pragma) then
8176 Comp := First_Component_Or_Discriminant (Rectype);
8177 while Present (Comp) loop
8178 if No (Component_Clause (Comp)) then
8179 Error_Msg_NE
8180 ("missing component clause for &", CR_Pragma, Comp);
8181 end if;
8182
8183 Next_Component_Or_Discriminant (Comp);
8184 end loop;
8185
8186 -- Give missing components warning if required
8187
8188 elsif Warn_On_Unrepped_Components then
8189 declare
8190 Num_Repped_Components : Nat := 0;
8191 Num_Unrepped_Components : Nat := 0;
8192
8193 begin
8194 -- First count number of repped and unrepped components
8195
8196 Comp := First_Component_Or_Discriminant (Rectype);
8197 while Present (Comp) loop
8198 if Present (Component_Clause (Comp)) then
8199 Num_Repped_Components := Num_Repped_Components + 1;
8200 else
8201 Num_Unrepped_Components := Num_Unrepped_Components + 1;
8202 end if;
8203
8204 Next_Component_Or_Discriminant (Comp);
8205 end loop;
8206
8207 -- We are only interested in the case where there is at least one
8208 -- unrepped component, and at least half the components have rep
8209 -- clauses. We figure that if less than half have them, then the
8210 -- partial rep clause is really intentional. If the component
8211 -- type has no underlying type set at this point (as for a generic
8212 -- formal type), we don't know enough to give a warning on the
8213 -- component.
8214
8215 if Num_Unrepped_Components > 0
8216 and then Num_Unrepped_Components < Num_Repped_Components
8217 then
8218 Comp := First_Component_Or_Discriminant (Rectype);
8219 while Present (Comp) loop
8220 if No (Component_Clause (Comp))
8221 and then Comes_From_Source (Comp)
8222 and then Present (Underlying_Type (Etype (Comp)))
8223 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
8224 or else Size_Known_At_Compile_Time
8225 (Underlying_Type (Etype (Comp))))
8226 and then not Has_Warnings_Off (Rectype)
8227
8228 -- Ignore discriminant in unchecked union, since it is
8229 -- not there, and cannot have a component clause.
8230
8231 and then (not Is_Unchecked_Union (Rectype)
8232 or else Ekind (Comp) /= E_Discriminant)
8233 then
8234 Error_Msg_Sloc := Sloc (Comp);
8235 Error_Msg_NE
8236 ("?C?no component clause given for & declared #",
8237 N, Comp);
8238 end if;
8239
8240 Next_Component_Or_Discriminant (Comp);
8241 end loop;
8242 end if;
8243 end;
8244 end if;
8245 end Analyze_Record_Representation_Clause;
8246
8247 -------------------------------------
8248 -- Build_Discrete_Static_Predicate --
8249 -------------------------------------
8250
8251 procedure Build_Discrete_Static_Predicate
8252 (Typ : Entity_Id;
8253 Expr : Node_Id;
8254 Nam : Name_Id)
8255 is
8256 Loc : constant Source_Ptr := Sloc (Expr);
8257
8258 Non_Static : exception;
8259 -- Raised if something non-static is found
8260
8261 Btyp : constant Entity_Id := Base_Type (Typ);
8262
8263 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
8264 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
8265 -- Low bound and high bound value of base type of Typ
8266
8267 TLo : Uint;
8268 THi : Uint;
8269 -- Bounds for constructing the static predicate. We use the bound of the
8270 -- subtype if it is static, otherwise the corresponding base type bound.
8271 -- Note: a non-static subtype can have a static predicate.
8272
8273 type REnt is record
8274 Lo, Hi : Uint;
8275 end record;
8276 -- One entry in a Rlist value, a single REnt (range entry) value denotes
8277 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
8278 -- value.
8279
8280 type RList is array (Nat range <>) of REnt;
8281 -- A list of ranges. The ranges are sorted in increasing order, and are
8282 -- disjoint (there is a gap of at least one value between each range in
8283 -- the table). A value is in the set of ranges in Rlist if it lies
8284 -- within one of these ranges.
8285
8286 False_Range : constant RList :=
8287 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
8288 -- An empty set of ranges represents a range list that can never be
8289 -- satisfied, since there are no ranges in which the value could lie,
8290 -- so it does not lie in any of them. False_Range is a canonical value
8291 -- for this empty set, but general processing should test for an Rlist
8292 -- with length zero (see Is_False predicate), since other null ranges
8293 -- may appear which must be treated as False.
8294
8295 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
8296 -- Range representing True, value must be in the base range
8297
8298 function "and" (Left : RList; Right : RList) return RList;
8299 -- And's together two range lists, returning a range list. This is a set
8300 -- intersection operation.
8301
8302 function "or" (Left : RList; Right : RList) return RList;
8303 -- Or's together two range lists, returning a range list. This is a set
8304 -- union operation.
8305
8306 function "not" (Right : RList) return RList;
8307 -- Returns complement of a given range list, i.e. a range list
8308 -- representing all the values in TLo .. THi that are not in the input
8309 -- operand Right.
8310
8311 function Build_Val (V : Uint) return Node_Id;
8312 -- Return an analyzed N_Identifier node referencing this value, suitable
8313 -- for use as an entry in the Static_Discrte_Predicate list. This node
8314 -- is typed with the base type.
8315
8316 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
8317 -- Return an analyzed N_Range node referencing this range, suitable for
8318 -- use as an entry in the Static_Discrete_Predicate list. This node is
8319 -- typed with the base type.
8320
8321 function Get_RList (Exp : Node_Id) return RList;
8322 -- This is a recursive routine that converts the given expression into a
8323 -- list of ranges, suitable for use in building the static predicate.
8324
8325 function Is_False (R : RList) return Boolean;
8326 pragma Inline (Is_False);
8327 -- Returns True if the given range list is empty, and thus represents a
8328 -- False list of ranges that can never be satisfied.
8329
8330 function Is_True (R : RList) return Boolean;
8331 -- Returns True if R trivially represents the True predicate by having a
8332 -- single range from BLo to BHi.
8333
8334 function Is_Type_Ref (N : Node_Id) return Boolean;
8335 pragma Inline (Is_Type_Ref);
8336 -- Returns if True if N is a reference to the type for the predicate in
8337 -- the expression (i.e. if it is an identifier whose Chars field matches
8338 -- the Nam given in the call). N must not be parenthesized, if the type
8339 -- name appears in parens, this routine will return False.
8340
8341 function Lo_Val (N : Node_Id) return Uint;
8342 -- Given an entry from a Static_Discrete_Predicate list that is either
8343 -- a static expression or static range, gets either the expression value
8344 -- or the low bound of the range.
8345
8346 function Hi_Val (N : Node_Id) return Uint;
8347 -- Given an entry from a Static_Discrete_Predicate list that is either
8348 -- a static expression or static range, gets either the expression value
8349 -- or the high bound of the range.
8350
8351 function Membership_Entry (N : Node_Id) return RList;
8352 -- Given a single membership entry (range, value, or subtype), returns
8353 -- the corresponding range list. Raises Static_Error if not static.
8354
8355 function Membership_Entries (N : Node_Id) return RList;
8356 -- Given an element on an alternatives list of a membership operation,
8357 -- returns the range list corresponding to this entry and all following
8358 -- entries (i.e. returns the "or" of this list of values).
8359
8360 function Stat_Pred (Typ : Entity_Id) return RList;
8361 -- Given a type, if it has a static predicate, then return the predicate
8362 -- as a range list, otherwise raise Non_Static.
8363
8364 -----------
8365 -- "and" --
8366 -----------
8367
8368 function "and" (Left : RList; Right : RList) return RList is
8369 FEnt : REnt;
8370 -- First range of result
8371
8372 SLeft : Nat := Left'First;
8373 -- Start of rest of left entries
8374
8375 SRight : Nat := Right'First;
8376 -- Start of rest of right entries
8377
8378 begin
8379 -- If either range is True, return the other
8380
8381 if Is_True (Left) then
8382 return Right;
8383 elsif Is_True (Right) then
8384 return Left;
8385 end if;
8386
8387 -- If either range is False, return False
8388
8389 if Is_False (Left) or else Is_False (Right) then
8390 return False_Range;
8391 end if;
8392
8393 -- Loop to remove entries at start that are disjoint, and thus just
8394 -- get discarded from the result entirely.
8395
8396 loop
8397 -- If no operands left in either operand, result is false
8398
8399 if SLeft > Left'Last or else SRight > Right'Last then
8400 return False_Range;
8401
8402 -- Discard first left operand entry if disjoint with right
8403
8404 elsif Left (SLeft).Hi < Right (SRight).Lo then
8405 SLeft := SLeft + 1;
8406
8407 -- Discard first right operand entry if disjoint with left
8408
8409 elsif Right (SRight).Hi < Left (SLeft).Lo then
8410 SRight := SRight + 1;
8411
8412 -- Otherwise we have an overlapping entry
8413
8414 else
8415 exit;
8416 end if;
8417 end loop;
8418
8419 -- Now we have two non-null operands, and first entries overlap. The
8420 -- first entry in the result will be the overlapping part of these
8421 -- two entries.
8422
8423 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
8424 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
8425
8426 -- Now we can remove the entry that ended at a lower value, since its
8427 -- contribution is entirely contained in Fent.
8428
8429 if Left (SLeft).Hi <= Right (SRight).Hi then
8430 SLeft := SLeft + 1;
8431 else
8432 SRight := SRight + 1;
8433 end if;
8434
8435 -- Compute result by concatenating this first entry with the "and" of
8436 -- the remaining parts of the left and right operands. Note that if
8437 -- either of these is empty, "and" will yield empty, so that we will
8438 -- end up with just Fent, which is what we want in that case.
8439
8440 return
8441 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
8442 end "and";
8443
8444 -----------
8445 -- "not" --
8446 -----------
8447
8448 function "not" (Right : RList) return RList is
8449 begin
8450 -- Return True if False range
8451
8452 if Is_False (Right) then
8453 return True_Range;
8454 end if;
8455
8456 -- Return False if True range
8457
8458 if Is_True (Right) then
8459 return False_Range;
8460 end if;
8461
8462 -- Here if not trivial case
8463
8464 declare
8465 Result : RList (1 .. Right'Length + 1);
8466 -- May need one more entry for gap at beginning and end
8467
8468 Count : Nat := 0;
8469 -- Number of entries stored in Result
8470
8471 begin
8472 -- Gap at start
8473
8474 if Right (Right'First).Lo > TLo then
8475 Count := Count + 1;
8476 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
8477 end if;
8478
8479 -- Gaps between ranges
8480
8481 for J in Right'First .. Right'Last - 1 loop
8482 Count := Count + 1;
8483 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
8484 end loop;
8485
8486 -- Gap at end
8487
8488 if Right (Right'Last).Hi < THi then
8489 Count := Count + 1;
8490 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
8491 end if;
8492
8493 return Result (1 .. Count);
8494 end;
8495 end "not";
8496
8497 ----------
8498 -- "or" --
8499 ----------
8500
8501 function "or" (Left : RList; Right : RList) return RList is
8502 FEnt : REnt;
8503 -- First range of result
8504
8505 SLeft : Nat := Left'First;
8506 -- Start of rest of left entries
8507
8508 SRight : Nat := Right'First;
8509 -- Start of rest of right entries
8510
8511 begin
8512 -- If either range is True, return True
8513
8514 if Is_True (Left) or else Is_True (Right) then
8515 return True_Range;
8516 end if;
8517
8518 -- If either range is False (empty), return the other
8519
8520 if Is_False (Left) then
8521 return Right;
8522 elsif Is_False (Right) then
8523 return Left;
8524 end if;
8525
8526 -- Initialize result first entry from left or right operand depending
8527 -- on which starts with the lower range.
8528
8529 if Left (SLeft).Lo < Right (SRight).Lo then
8530 FEnt := Left (SLeft);
8531 SLeft := SLeft + 1;
8532 else
8533 FEnt := Right (SRight);
8534 SRight := SRight + 1;
8535 end if;
8536
8537 -- This loop eats ranges from left and right operands that are
8538 -- contiguous with the first range we are gathering.
8539
8540 loop
8541 -- Eat first entry in left operand if contiguous or overlapped by
8542 -- gathered first operand of result.
8543
8544 if SLeft <= Left'Last
8545 and then Left (SLeft).Lo <= FEnt.Hi + 1
8546 then
8547 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
8548 SLeft := SLeft + 1;
8549
8550 -- Eat first entry in right operand if contiguous or overlapped by
8551 -- gathered right operand of result.
8552
8553 elsif SRight <= Right'Last
8554 and then Right (SRight).Lo <= FEnt.Hi + 1
8555 then
8556 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
8557 SRight := SRight + 1;
8558
8559 -- All done if no more entries to eat
8560
8561 else
8562 exit;
8563 end if;
8564 end loop;
8565
8566 -- Obtain result as the first entry we just computed, concatenated
8567 -- to the "or" of the remaining results (if one operand is empty,
8568 -- this will just concatenate with the other
8569
8570 return
8571 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
8572 end "or";
8573
8574 -----------------
8575 -- Build_Range --
8576 -----------------
8577
8578 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
8579 Result : Node_Id;
8580 begin
8581 Result :=
8582 Make_Range (Loc,
8583 Low_Bound => Build_Val (Lo),
8584 High_Bound => Build_Val (Hi));
8585 Set_Etype (Result, Btyp);
8586 Set_Analyzed (Result);
8587 return Result;
8588 end Build_Range;
8589
8590 ---------------
8591 -- Build_Val --
8592 ---------------
8593
8594 function Build_Val (V : Uint) return Node_Id is
8595 Result : Node_Id;
8596
8597 begin
8598 if Is_Enumeration_Type (Typ) then
8599 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
8600 else
8601 Result := Make_Integer_Literal (Loc, V);
8602 end if;
8603
8604 Set_Etype (Result, Btyp);
8605 Set_Is_Static_Expression (Result);
8606 Set_Analyzed (Result);
8607 return Result;
8608 end Build_Val;
8609
8610 ---------------
8611 -- Get_RList --
8612 ---------------
8613
8614 function Get_RList (Exp : Node_Id) return RList is
8615 Op : Node_Kind;
8616 Val : Uint;
8617
8618 begin
8619 -- Static expression can only be true or false
8620
8621 if Is_OK_Static_Expression (Exp) then
8622 if Expr_Value (Exp) = 0 then
8623 return False_Range;
8624 else
8625 return True_Range;
8626 end if;
8627 end if;
8628
8629 -- Otherwise test node type
8630
8631 Op := Nkind (Exp);
8632
8633 case Op is
8634
8635 -- And
8636
8637 when N_And_Then
8638 | N_Op_And
8639 =>
8640 return Get_RList (Left_Opnd (Exp))
8641 and
8642 Get_RList (Right_Opnd (Exp));
8643
8644 -- Or
8645
8646 when N_Op_Or
8647 | N_Or_Else
8648 =>
8649 return Get_RList (Left_Opnd (Exp))
8650 or
8651 Get_RList (Right_Opnd (Exp));
8652
8653 -- Not
8654
8655 when N_Op_Not =>
8656 return not Get_RList (Right_Opnd (Exp));
8657
8658 -- Comparisons of type with static value
8659
8660 when N_Op_Compare =>
8661
8662 -- Type is left operand
8663
8664 if Is_Type_Ref (Left_Opnd (Exp))
8665 and then Is_OK_Static_Expression (Right_Opnd (Exp))
8666 then
8667 Val := Expr_Value (Right_Opnd (Exp));
8668
8669 -- Typ is right operand
8670
8671 elsif Is_Type_Ref (Right_Opnd (Exp))
8672 and then Is_OK_Static_Expression (Left_Opnd (Exp))
8673 then
8674 Val := Expr_Value (Left_Opnd (Exp));
8675
8676 -- Invert sense of comparison
8677
8678 case Op is
8679 when N_Op_Gt => Op := N_Op_Lt;
8680 when N_Op_Lt => Op := N_Op_Gt;
8681 when N_Op_Ge => Op := N_Op_Le;
8682 when N_Op_Le => Op := N_Op_Ge;
8683 when others => null;
8684 end case;
8685
8686 -- Other cases are non-static
8687
8688 else
8689 raise Non_Static;
8690 end if;
8691
8692 -- Construct range according to comparison operation
8693
8694 case Op is
8695 when N_Op_Eq =>
8696 return RList'(1 => REnt'(Val, Val));
8697
8698 when N_Op_Ge =>
8699 return RList'(1 => REnt'(Val, BHi));
8700
8701 when N_Op_Gt =>
8702 return RList'(1 => REnt'(Val + 1, BHi));
8703
8704 when N_Op_Le =>
8705 return RList'(1 => REnt'(BLo, Val));
8706
8707 when N_Op_Lt =>
8708 return RList'(1 => REnt'(BLo, Val - 1));
8709
8710 when N_Op_Ne =>
8711 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
8712
8713 when others =>
8714 raise Program_Error;
8715 end case;
8716
8717 -- Membership (IN)
8718
8719 when N_In =>
8720 if not Is_Type_Ref (Left_Opnd (Exp)) then
8721 raise Non_Static;
8722 end if;
8723
8724 if Present (Right_Opnd (Exp)) then
8725 return Membership_Entry (Right_Opnd (Exp));
8726 else
8727 return Membership_Entries (First (Alternatives (Exp)));
8728 end if;
8729
8730 -- Negative membership (NOT IN)
8731
8732 when N_Not_In =>
8733 if not Is_Type_Ref (Left_Opnd (Exp)) then
8734 raise Non_Static;
8735 end if;
8736
8737 if Present (Right_Opnd (Exp)) then
8738 return not Membership_Entry (Right_Opnd (Exp));
8739 else
8740 return not Membership_Entries (First (Alternatives (Exp)));
8741 end if;
8742
8743 -- Function call, may be call to static predicate
8744
8745 when N_Function_Call =>
8746 if Is_Entity_Name (Name (Exp)) then
8747 declare
8748 Ent : constant Entity_Id := Entity (Name (Exp));
8749 begin
8750 if Is_Predicate_Function (Ent)
8751 or else
8752 Is_Predicate_Function_M (Ent)
8753 then
8754 return Stat_Pred (Etype (First_Formal (Ent)));
8755 end if;
8756 end;
8757 end if;
8758
8759 -- Other function call cases are non-static
8760
8761 raise Non_Static;
8762
8763 -- Qualified expression, dig out the expression
8764
8765 when N_Qualified_Expression =>
8766 return Get_RList (Expression (Exp));
8767
8768 when N_Case_Expression =>
8769 declare
8770 Alt : Node_Id;
8771 Choices : List_Id;
8772 Dep : Node_Id;
8773
8774 begin
8775 if not Is_Entity_Name (Expression (Expr))
8776 or else Etype (Expression (Expr)) /= Typ
8777 then
8778 Error_Msg_N
8779 ("expression must denaote subtype", Expression (Expr));
8780 return False_Range;
8781 end if;
8782
8783 -- Collect discrete choices in all True alternatives
8784
8785 Choices := New_List;
8786 Alt := First (Alternatives (Exp));
8787 while Present (Alt) loop
8788 Dep := Expression (Alt);
8789
8790 if not Is_OK_Static_Expression (Dep) then
8791 raise Non_Static;
8792
8793 elsif Is_True (Expr_Value (Dep)) then
8794 Append_List_To (Choices,
8795 New_Copy_List (Discrete_Choices (Alt)));
8796 end if;
8797
8798 Next (Alt);
8799 end loop;
8800
8801 return Membership_Entries (First (Choices));
8802 end;
8803
8804 -- Expression with actions: if no actions, dig out expression
8805
8806 when N_Expression_With_Actions =>
8807 if Is_Empty_List (Actions (Exp)) then
8808 return Get_RList (Expression (Exp));
8809 else
8810 raise Non_Static;
8811 end if;
8812
8813 -- Xor operator
8814
8815 when N_Op_Xor =>
8816 return (Get_RList (Left_Opnd (Exp))
8817 and not Get_RList (Right_Opnd (Exp)))
8818 or (Get_RList (Right_Opnd (Exp))
8819 and not Get_RList (Left_Opnd (Exp)));
8820
8821 -- Any other node type is non-static
8822
8823 when others =>
8824 raise Non_Static;
8825 end case;
8826 end Get_RList;
8827
8828 ------------
8829 -- Hi_Val --
8830 ------------
8831
8832 function Hi_Val (N : Node_Id) return Uint is
8833 begin
8834 if Is_OK_Static_Expression (N) then
8835 return Expr_Value (N);
8836 else
8837 pragma Assert (Nkind (N) = N_Range);
8838 return Expr_Value (High_Bound (N));
8839 end if;
8840 end Hi_Val;
8841
8842 --------------
8843 -- Is_False --
8844 --------------
8845
8846 function Is_False (R : RList) return Boolean is
8847 begin
8848 return R'Length = 0;
8849 end Is_False;
8850
8851 -------------
8852 -- Is_True --
8853 -------------
8854
8855 function Is_True (R : RList) return Boolean is
8856 begin
8857 return R'Length = 1
8858 and then R (R'First).Lo = BLo
8859 and then R (R'First).Hi = BHi;
8860 end Is_True;
8861
8862 -----------------
8863 -- Is_Type_Ref --
8864 -----------------
8865
8866 function Is_Type_Ref (N : Node_Id) return Boolean is
8867 begin
8868 return Nkind (N) = N_Identifier
8869 and then Chars (N) = Nam
8870 and then Paren_Count (N) = 0;
8871 end Is_Type_Ref;
8872
8873 ------------
8874 -- Lo_Val --
8875 ------------
8876
8877 function Lo_Val (N : Node_Id) return Uint is
8878 begin
8879 if Is_OK_Static_Expression (N) then
8880 return Expr_Value (N);
8881 else
8882 pragma Assert (Nkind (N) = N_Range);
8883 return Expr_Value (Low_Bound (N));
8884 end if;
8885 end Lo_Val;
8886
8887 ------------------------
8888 -- Membership_Entries --
8889 ------------------------
8890
8891 function Membership_Entries (N : Node_Id) return RList is
8892 begin
8893 if No (Next (N)) then
8894 return Membership_Entry (N);
8895 else
8896 return Membership_Entry (N) or Membership_Entries (Next (N));
8897 end if;
8898 end Membership_Entries;
8899
8900 ----------------------
8901 -- Membership_Entry --
8902 ----------------------
8903
8904 function Membership_Entry (N : Node_Id) return RList is
8905 Val : Uint;
8906 SLo : Uint;
8907 SHi : Uint;
8908
8909 begin
8910 -- Range case
8911
8912 if Nkind (N) = N_Range then
8913 if not Is_OK_Static_Expression (Low_Bound (N))
8914 or else
8915 not Is_OK_Static_Expression (High_Bound (N))
8916 then
8917 raise Non_Static;
8918 else
8919 SLo := Expr_Value (Low_Bound (N));
8920 SHi := Expr_Value (High_Bound (N));
8921 return RList'(1 => REnt'(SLo, SHi));
8922 end if;
8923
8924 -- Static expression case
8925
8926 elsif Is_OK_Static_Expression (N) then
8927 Val := Expr_Value (N);
8928 return RList'(1 => REnt'(Val, Val));
8929
8930 -- Identifier (other than static expression) case
8931
8932 else pragma Assert (Nkind_In (N, N_Expanded_Name, N_Identifier));
8933
8934 -- Type case
8935
8936 if Is_Type (Entity (N)) then
8937
8938 -- If type has predicates, process them
8939
8940 if Has_Predicates (Entity (N)) then
8941 return Stat_Pred (Entity (N));
8942
8943 -- For static subtype without predicates, get range
8944
8945 elsif Is_OK_Static_Subtype (Entity (N)) then
8946 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
8947 SHi := Expr_Value (Type_High_Bound (Entity (N)));
8948 return RList'(1 => REnt'(SLo, SHi));
8949
8950 -- Any other type makes us non-static
8951
8952 else
8953 raise Non_Static;
8954 end if;
8955
8956 -- Any other kind of identifier in predicate (e.g. a non-static
8957 -- expression value) means this is not a static predicate.
8958
8959 else
8960 raise Non_Static;
8961 end if;
8962 end if;
8963 end Membership_Entry;
8964
8965 ---------------
8966 -- Stat_Pred --
8967 ---------------
8968
8969 function Stat_Pred (Typ : Entity_Id) return RList is
8970 begin
8971 -- Not static if type does not have static predicates
8972
8973 if not Has_Static_Predicate (Typ) then
8974 raise Non_Static;
8975 end if;
8976
8977 -- Otherwise we convert the predicate list to a range list
8978
8979 declare
8980 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
8981 Result : RList (1 .. List_Length (Spred));
8982 P : Node_Id;
8983
8984 begin
8985 P := First (Static_Discrete_Predicate (Typ));
8986 for J in Result'Range loop
8987 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
8988 Next (P);
8989 end loop;
8990
8991 return Result;
8992 end;
8993 end Stat_Pred;
8994
8995 -- Start of processing for Build_Discrete_Static_Predicate
8996
8997 begin
8998 -- Establish bounds for the predicate
8999
9000 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
9001 TLo := Expr_Value (Type_Low_Bound (Typ));
9002 else
9003 TLo := BLo;
9004 end if;
9005
9006 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
9007 THi := Expr_Value (Type_High_Bound (Typ));
9008 else
9009 THi := BHi;
9010 end if;
9011
9012 -- Analyze the expression to see if it is a static predicate
9013
9014 declare
9015 Ranges : constant RList := Get_RList (Expr);
9016 -- Range list from expression if it is static
9017
9018 Plist : List_Id;
9019
9020 begin
9021 -- Convert range list into a form for the static predicate. In the
9022 -- Ranges array, we just have raw ranges, these must be converted
9023 -- to properly typed and analyzed static expressions or range nodes.
9024
9025 -- Note: here we limit ranges to the ranges of the subtype, so that
9026 -- a predicate is always false for values outside the subtype. That
9027 -- seems fine, such values are invalid anyway, and considering them
9028 -- to fail the predicate seems allowed and friendly, and furthermore
9029 -- simplifies processing for case statements and loops.
9030
9031 Plist := New_List;
9032
9033 for J in Ranges'Range loop
9034 declare
9035 Lo : Uint := Ranges (J).Lo;
9036 Hi : Uint := Ranges (J).Hi;
9037
9038 begin
9039 -- Ignore completely out of range entry
9040
9041 if Hi < TLo or else Lo > THi then
9042 null;
9043
9044 -- Otherwise process entry
9045
9046 else
9047 -- Adjust out of range value to subtype range
9048
9049 if Lo < TLo then
9050 Lo := TLo;
9051 end if;
9052
9053 if Hi > THi then
9054 Hi := THi;
9055 end if;
9056
9057 -- Convert range into required form
9058
9059 Append_To (Plist, Build_Range (Lo, Hi));
9060 end if;
9061 end;
9062 end loop;
9063
9064 -- Processing was successful and all entries were static, so now we
9065 -- can store the result as the predicate list.
9066
9067 Set_Static_Discrete_Predicate (Typ, Plist);
9068
9069 -- Within a generic the predicate functions themselves need not
9070 -- be constructed.
9071
9072 if Inside_A_Generic then
9073 return;
9074 end if;
9075
9076 -- The processing for static predicates put the expression into
9077 -- canonical form as a series of ranges. It also eliminated
9078 -- duplicates and collapsed and combined ranges. We might as well
9079 -- replace the alternatives list of the right operand of the
9080 -- membership test with the static predicate list, which will
9081 -- usually be more efficient.
9082
9083 declare
9084 New_Alts : constant List_Id := New_List;
9085 Old_Node : Node_Id;
9086 New_Node : Node_Id;
9087
9088 begin
9089 Old_Node := First (Plist);
9090 while Present (Old_Node) loop
9091 New_Node := New_Copy (Old_Node);
9092
9093 if Nkind (New_Node) = N_Range then
9094 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
9095 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
9096 end if;
9097
9098 Append_To (New_Alts, New_Node);
9099 Next (Old_Node);
9100 end loop;
9101
9102 -- If empty list, replace by False
9103
9104 if Is_Empty_List (New_Alts) then
9105 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
9106
9107 -- Else replace by set membership test
9108
9109 else
9110 Rewrite (Expr,
9111 Make_In (Loc,
9112 Left_Opnd => Make_Identifier (Loc, Nam),
9113 Right_Opnd => Empty,
9114 Alternatives => New_Alts));
9115
9116 -- Resolve new expression in function context
9117
9118 Install_Formals (Predicate_Function (Typ));
9119 Push_Scope (Predicate_Function (Typ));
9120 Analyze_And_Resolve (Expr, Standard_Boolean);
9121 Pop_Scope;
9122 end if;
9123 end;
9124 end;
9125
9126 -- If non-static, return doing nothing
9127
9128 exception
9129 when Non_Static =>
9130 return;
9131 end Build_Discrete_Static_Predicate;
9132
9133 --------------------------------
9134 -- Build_Export_Import_Pragma --
9135 --------------------------------
9136
9137 function Build_Export_Import_Pragma
9138 (Asp : Node_Id;
9139 Id : Entity_Id) return Node_Id
9140 is
9141 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
9142 Expr : constant Node_Id := Expression (Asp);
9143 Loc : constant Source_Ptr := Sloc (Asp);
9144
9145 Args : List_Id;
9146 Conv : Node_Id;
9147 Conv_Arg : Node_Id;
9148 Dummy_1 : Node_Id;
9149 Dummy_2 : Node_Id;
9150 EN : Node_Id;
9151 LN : Node_Id;
9152 Prag : Node_Id;
9153
9154 Create_Pragma : Boolean := False;
9155 -- This flag is set when the aspect form is such that it warrants the
9156 -- creation of a corresponding pragma.
9157
9158 begin
9159 if Present (Expr) then
9160 if Error_Posted (Expr) then
9161 null;
9162
9163 elsif Is_True (Expr_Value (Expr)) then
9164 Create_Pragma := True;
9165 end if;
9166
9167 -- Otherwise the aspect defaults to True
9168
9169 else
9170 Create_Pragma := True;
9171 end if;
9172
9173 -- Nothing to do when the expression is False or is erroneous
9174
9175 if not Create_Pragma then
9176 return Empty;
9177 end if;
9178
9179 -- Obtain all interfacing aspects that apply to the related entity
9180
9181 Get_Interfacing_Aspects
9182 (Iface_Asp => Asp,
9183 Conv_Asp => Conv,
9184 EN_Asp => EN,
9185 Expo_Asp => Dummy_1,
9186 Imp_Asp => Dummy_2,
9187 LN_Asp => LN);
9188
9189 Args := New_List;
9190
9191 -- Handle the convention argument
9192
9193 if Present (Conv) then
9194 Conv_Arg := New_Copy_Tree (Expression (Conv));
9195
9196 -- Assume convention "Ada' when aspect Convention is missing
9197
9198 else
9199 Conv_Arg := Make_Identifier (Loc, Name_Ada);
9200 end if;
9201
9202 Append_To (Args,
9203 Make_Pragma_Argument_Association (Loc,
9204 Chars => Name_Convention,
9205 Expression => Conv_Arg));
9206
9207 -- Handle the entity argument
9208
9209 Append_To (Args,
9210 Make_Pragma_Argument_Association (Loc,
9211 Chars => Name_Entity,
9212 Expression => New_Occurrence_Of (Id, Loc)));
9213
9214 -- Handle the External_Name argument
9215
9216 if Present (EN) then
9217 Append_To (Args,
9218 Make_Pragma_Argument_Association (Loc,
9219 Chars => Name_External_Name,
9220 Expression => New_Copy_Tree (Expression (EN))));
9221 end if;
9222
9223 -- Handle the Link_Name argument
9224
9225 if Present (LN) then
9226 Append_To (Args,
9227 Make_Pragma_Argument_Association (Loc,
9228 Chars => Name_Link_Name,
9229 Expression => New_Copy_Tree (Expression (LN))));
9230 end if;
9231
9232 -- Generate:
9233 -- pragma Export/Import
9234 -- (Convention => <Conv>/Ada,
9235 -- Entity => <Id>,
9236 -- [External_Name => <EN>,]
9237 -- [Link_Name => <LN>]);
9238
9239 Prag :=
9240 Make_Pragma (Loc,
9241 Pragma_Identifier =>
9242 Make_Identifier (Loc, Chars (Identifier (Asp))),
9243 Pragma_Argument_Associations => Args);
9244
9245 -- Decorate the relevant aspect and the pragma
9246
9247 Set_Aspect_Rep_Item (Asp, Prag);
9248
9249 Set_Corresponding_Aspect (Prag, Asp);
9250 Set_From_Aspect_Specification (Prag);
9251 Set_Parent (Prag, Asp);
9252
9253 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
9254 Set_Import_Pragma (Id, Prag);
9255 end if;
9256
9257 return Prag;
9258 end Build_Export_Import_Pragma;
9259
9260 -------------------------------
9261 -- Build_Predicate_Functions --
9262 -------------------------------
9263
9264 -- The functions that are constructed here have the form:
9265
9266 -- function typPredicate (Ixxx : typ) return Boolean is
9267 -- begin
9268 -- return
9269 -- typ1Predicate (typ1 (Ixxx))
9270 -- and then typ2Predicate (typ2 (Ixxx))
9271 -- and then ...
9272 -- and then exp1 and then exp2 and then ...;
9273 -- end typPredicate;
9274
9275 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
9276 -- this is the point at which these expressions get analyzed, providing the
9277 -- required delay, and typ1, typ2, are entities from which predicates are
9278 -- inherited. Note that we do NOT generate Check pragmas, that's because we
9279 -- use this function even if checks are off, e.g. for membership tests.
9280
9281 -- Note that the inherited predicates are evaluated first, as required by
9282 -- AI12-0071-1.
9283
9284 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
9285 -- the form of this return expression.
9286
9287 -- If the expression has at least one Raise_Expression, then we also build
9288 -- the typPredicateM version of the function, in which any occurrence of a
9289 -- Raise_Expression is converted to "return False".
9290
9291 -- WARNING: This routine manages Ghost regions. Return statements must be
9292 -- replaced by gotos which jump to the end of the routine and restore the
9293 -- Ghost mode.
9294
9295 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
9296 Loc : constant Source_Ptr := Sloc (Typ);
9297
9298 Expr : Node_Id;
9299 -- This is the expression for the result of the function. It is
9300 -- is build by connecting the component predicates with AND THEN.
9301
9302 Expr_M : Node_Id := Empty; -- init to avoid warning
9303 -- This is the corresponding return expression for the Predicate_M
9304 -- function. It differs in that raise expressions are marked for
9305 -- special expansion (see Process_REs).
9306
9307 Object_Name : Name_Id;
9308 -- Name for argument of Predicate procedure. Note that we use the same
9309 -- name for both predicate functions. That way the reference within the
9310 -- predicate expression is the same in both functions.
9311
9312 Object_Entity : Entity_Id;
9313 -- Entity for argument of Predicate procedure
9314
9315 Object_Entity_M : Entity_Id;
9316 -- Entity for argument of separate Predicate procedure when exceptions
9317 -- are present in expression.
9318
9319 FDecl : Node_Id;
9320 -- The function declaration
9321
9322 SId : Entity_Id;
9323 -- Its entity
9324
9325 Raise_Expression_Present : Boolean := False;
9326 -- Set True if Expr has at least one Raise_Expression
9327
9328 procedure Add_Condition (Cond : Node_Id);
9329 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
9330 -- Expr is empty).
9331
9332 procedure Add_Predicates;
9333 -- Appends expressions for any Predicate pragmas in the rep item chain
9334 -- Typ to Expr. Note that we look only at items for this exact entity.
9335 -- Inheritance of predicates for the parent type is done by calling the
9336 -- Predicate_Function of the parent type, using Add_Call above.
9337
9338 procedure Add_Call (T : Entity_Id);
9339 -- Includes a call to the predicate function for type T in Expr if T
9340 -- has predicates and Predicate_Function (T) is non-empty.
9341
9342 function Process_RE (N : Node_Id) return Traverse_Result;
9343 -- Used in Process REs, tests if node N is a raise expression, and if
9344 -- so, marks it to be converted to return False.
9345
9346 procedure Process_REs is new Traverse_Proc (Process_RE);
9347 -- Marks any raise expressions in Expr_M to return False
9348
9349 function Test_RE (N : Node_Id) return Traverse_Result;
9350 -- Used in Test_REs, tests one node for being a raise expression, and if
9351 -- so sets Raise_Expression_Present True.
9352
9353 procedure Test_REs is new Traverse_Proc (Test_RE);
9354 -- Tests to see if Expr contains any raise expressions
9355
9356 --------------
9357 -- Add_Call --
9358 --------------
9359
9360 procedure Add_Call (T : Entity_Id) is
9361 Exp : Node_Id;
9362
9363 begin
9364 if Present (T) and then Present (Predicate_Function (T)) then
9365 Set_Has_Predicates (Typ);
9366
9367 -- Build the call to the predicate function of T. The type may be
9368 -- derived, so use an unchecked conversion for the actual.
9369
9370 Exp :=
9371 Make_Predicate_Call
9372 (Typ => T,
9373 Expr =>
9374 Unchecked_Convert_To (T,
9375 Make_Identifier (Loc, Object_Name)));
9376
9377 -- "and"-in the call to evolving expression
9378
9379 Add_Condition (Exp);
9380
9381 -- Output info message on inheritance if required. Note we do not
9382 -- give this information for generic actual types, since it is
9383 -- unwelcome noise in that case in instantiations. We also
9384 -- generally suppress the message in instantiations, and also
9385 -- if it involves internal names.
9386
9387 if Opt.List_Inherited_Aspects
9388 and then not Is_Generic_Actual_Type (Typ)
9389 and then Instantiation_Depth (Sloc (Typ)) = 0
9390 and then not Is_Internal_Name (Chars (T))
9391 and then not Is_Internal_Name (Chars (Typ))
9392 then
9393 Error_Msg_Sloc := Sloc (Predicate_Function (T));
9394 Error_Msg_Node_2 := T;
9395 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
9396 end if;
9397 end if;
9398 end Add_Call;
9399
9400 -------------------
9401 -- Add_Condition --
9402 -------------------
9403
9404 procedure Add_Condition (Cond : Node_Id) is
9405 begin
9406 -- This is the first predicate expression
9407
9408 if No (Expr) then
9409 Expr := Cond;
9410
9411 -- Otherwise concatenate to the existing predicate expressions by
9412 -- using "and then".
9413
9414 else
9415 Expr :=
9416 Make_And_Then (Loc,
9417 Left_Opnd => Relocate_Node (Expr),
9418 Right_Opnd => Cond);
9419 end if;
9420 end Add_Condition;
9421
9422 --------------------
9423 -- Add_Predicates --
9424 --------------------
9425
9426 procedure Add_Predicates is
9427 procedure Add_Predicate (Prag : Node_Id);
9428 -- Concatenate the expression of predicate pragma Prag to Expr by
9429 -- using a short circuit "and then" operator.
9430
9431 -------------------
9432 -- Add_Predicate --
9433 -------------------
9434
9435 procedure Add_Predicate (Prag : Node_Id) is
9436 procedure Replace_Type_Reference (N : Node_Id);
9437 -- Replace a single occurrence N of the subtype name with a
9438 -- reference to the formal of the predicate function. N can be an
9439 -- identifier referencing the subtype, or a selected component,
9440 -- representing an appropriately qualified occurrence of the
9441 -- subtype name.
9442
9443 procedure Replace_Type_References is
9444 new Replace_Type_References_Generic (Replace_Type_Reference);
9445 -- Traverse an expression changing every occurrence of an
9446 -- identifier whose name matches the name of the subtype with a
9447 -- reference to the formal parameter of the predicate function.
9448
9449 ----------------------------
9450 -- Replace_Type_Reference --
9451 ----------------------------
9452
9453 procedure Replace_Type_Reference (N : Node_Id) is
9454 begin
9455 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
9456 -- Use the Sloc of the usage name, not the defining name
9457
9458 Set_Etype (N, Typ);
9459 Set_Entity (N, Object_Entity);
9460 end Replace_Type_Reference;
9461
9462 -- Local variables
9463
9464 Asp : constant Node_Id := Corresponding_Aspect (Prag);
9465 Arg1 : Node_Id;
9466 Arg2 : Node_Id;
9467
9468 -- Start of processing for Add_Predicate
9469
9470 begin
9471 -- Mark corresponding SCO as enabled
9472
9473 Set_SCO_Pragma_Enabled (Sloc (Prag));
9474
9475 -- Extract the arguments of the pragma. The expression itself
9476 -- is copied for use in the predicate function, to preserve the
9477 -- original version for ASIS use.
9478 -- Is this still needed???
9479
9480 Arg1 := First (Pragma_Argument_Associations (Prag));
9481 Arg2 := Next (Arg1);
9482
9483 Arg1 := Get_Pragma_Arg (Arg1);
9484 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
9485
9486 -- When the predicate pragma applies to the current type or its
9487 -- full view, replace all occurrences of the subtype name with
9488 -- references to the formal parameter of the predicate function.
9489
9490 if Entity (Arg1) = Typ
9491 or else Full_View (Entity (Arg1)) = Typ
9492 then
9493 Replace_Type_References (Arg2, Typ);
9494
9495 -- If the predicate pragma comes from an aspect, replace the
9496 -- saved expression because we need the subtype references
9497 -- replaced for the calls to Preanalyze_Spec_Expression in
9498 -- Check_Aspect_At_xxx routines.
9499
9500 if Present (Asp) then
9501 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
9502 end if;
9503
9504 -- "and"-in the Arg2 condition to evolving expression
9505
9506 Add_Condition (Relocate_Node (Arg2));
9507 end if;
9508 end Add_Predicate;
9509
9510 -- Local variables
9511
9512 Ritem : Node_Id;
9513
9514 -- Start of processing for Add_Predicates
9515
9516 begin
9517 Ritem := First_Rep_Item (Typ);
9518
9519 -- If the type is private, check whether full view has inherited
9520 -- predicates.
9521
9522 if Is_Private_Type (Typ) and then No (Ritem) then
9523 Ritem := First_Rep_Item (Full_View (Typ));
9524 end if;
9525
9526 while Present (Ritem) loop
9527 if Nkind (Ritem) = N_Pragma
9528 and then Pragma_Name (Ritem) = Name_Predicate
9529 then
9530 Add_Predicate (Ritem);
9531
9532 -- If the type is declared in an inner package it may be frozen
9533 -- outside of the package, and the generated pragma has not been
9534 -- analyzed yet, so capture the expression for the predicate
9535 -- function at this point.
9536
9537 elsif Nkind (Ritem) = N_Aspect_Specification
9538 and then Present (Aspect_Rep_Item (Ritem))
9539 and then Scope (Typ) /= Current_Scope
9540 then
9541 declare
9542 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
9543
9544 begin
9545 if Nkind (Prag) = N_Pragma
9546 and then Pragma_Name (Prag) = Name_Predicate
9547 then
9548 Add_Predicate (Prag);
9549 end if;
9550 end;
9551 end if;
9552
9553 Next_Rep_Item (Ritem);
9554 end loop;
9555 end Add_Predicates;
9556
9557 ----------------
9558 -- Process_RE --
9559 ----------------
9560
9561 function Process_RE (N : Node_Id) return Traverse_Result is
9562 begin
9563 if Nkind (N) = N_Raise_Expression then
9564 Set_Convert_To_Return_False (N);
9565 return Skip;
9566 else
9567 return OK;
9568 end if;
9569 end Process_RE;
9570
9571 -------------
9572 -- Test_RE --
9573 -------------
9574
9575 function Test_RE (N : Node_Id) return Traverse_Result is
9576 begin
9577 if Nkind (N) = N_Raise_Expression then
9578 Raise_Expression_Present := True;
9579 return Abandon;
9580 else
9581 return OK;
9582 end if;
9583 end Test_RE;
9584
9585 -- Local variables
9586
9587 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9588 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9589 -- Save the Ghost-related attributes to restore on exit
9590
9591 -- Start of processing for Build_Predicate_Functions
9592
9593 begin
9594 -- Return if already built or if type does not have predicates
9595
9596 SId := Predicate_Function (Typ);
9597 if not Has_Predicates (Typ)
9598 or else (Present (SId) and then Has_Completion (SId))
9599 then
9600 return;
9601
9602 -- Do not generate predicate bodies within a generic unit. The
9603 -- expressions have been analyzed already, and the bodies play
9604 -- no role if not within an executable unit. However, if a statc
9605 -- predicate is present it must be processed for legality checks
9606 -- such as case coverage in an expression.
9607
9608 elsif Inside_A_Generic
9609 and then not Has_Static_Predicate_Aspect (Typ)
9610 then
9611 return;
9612 end if;
9613
9614 -- The related type may be subject to pragma Ghost. Set the mode now to
9615 -- ensure that the predicate functions are properly marked as Ghost.
9616
9617 Set_Ghost_Mode (Typ);
9618
9619 -- Prepare to construct predicate expression
9620
9621 Expr := Empty;
9622
9623 if Present (SId) then
9624 FDecl := Unit_Declaration_Node (SId);
9625
9626 else
9627 FDecl := Build_Predicate_Function_Declaration (Typ);
9628 SId := Defining_Entity (FDecl);
9629 end if;
9630
9631 -- Recover name of formal parameter of function that replaces references
9632 -- to the type in predicate expressions.
9633
9634 Object_Entity :=
9635 Defining_Identifier
9636 (First (Parameter_Specifications (Specification (FDecl))));
9637
9638 Object_Name := Chars (Object_Entity);
9639 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
9640
9641 -- Add predicates for ancestor if present. These must come before the
9642 -- ones for the current type, as required by AI12-0071-1.
9643
9644 -- Looks like predicates aren't added for case of inheriting from
9645 -- multiple progenitors???
9646
9647 declare
9648 Atyp : Entity_Id;
9649 begin
9650 Atyp := Nearest_Ancestor (Typ);
9651
9652 -- The type may be private but the full view may inherit predicates
9653
9654 if No (Atyp) and then Is_Private_Type (Typ) then
9655 Atyp := Nearest_Ancestor (Full_View (Typ));
9656 end if;
9657
9658 if Present (Atyp) then
9659 Add_Call (Atyp);
9660 end if;
9661 end;
9662
9663 -- Add Predicates for the current type
9664
9665 Add_Predicates;
9666
9667 -- Case where predicates are present
9668
9669 if Present (Expr) then
9670
9671 -- Test for raise expression present
9672
9673 Test_REs (Expr);
9674
9675 -- If raise expression is present, capture a copy of Expr for use
9676 -- in building the predicateM function version later on. For this
9677 -- copy we replace references to Object_Entity by Object_Entity_M.
9678
9679 if Raise_Expression_Present then
9680 declare
9681 function Reset_Loop_Variable
9682 (N : Node_Id) return Traverse_Result;
9683
9684 procedure Reset_Loop_Variables is
9685 new Traverse_Proc (Reset_Loop_Variable);
9686
9687 ------------------------
9688 -- Reset_Loop_Variable --
9689 ------------------------
9690
9691 function Reset_Loop_Variable
9692 (N : Node_Id) return Traverse_Result
9693 is
9694 begin
9695 if Nkind (N) = N_Iterator_Specification then
9696 Set_Defining_Identifier (N,
9697 Make_Defining_Identifier
9698 (Sloc (N), Chars (Defining_Identifier (N))));
9699 end if;
9700
9701 return OK;
9702 end Reset_Loop_Variable;
9703
9704 -- Local variables
9705
9706 Map : constant Elist_Id := New_Elmt_List;
9707
9708 begin
9709 Append_Elmt (Object_Entity, Map);
9710 Append_Elmt (Object_Entity_M, Map);
9711 Expr_M := New_Copy_Tree (Expr, Map => Map);
9712
9713 -- The unanalyzed expression will be copied and appear in
9714 -- both functions. Normally expressions do not declare new
9715 -- entities, but quantified expressions do, so we need to
9716 -- create new entities for their bound variables, to prevent
9717 -- multiple definitions in gigi.
9718
9719 Reset_Loop_Variables (Expr_M);
9720 end;
9721 end if;
9722
9723 -- Build the main predicate function
9724
9725 declare
9726 SIdB : constant Entity_Id :=
9727 Make_Defining_Identifier (Loc,
9728 Chars => New_External_Name (Chars (Typ), "Predicate"));
9729 -- The entity for the function body
9730
9731 Spec : Node_Id;
9732 FBody : Node_Id;
9733
9734 begin
9735 Set_Ekind (SIdB, E_Function);
9736 Set_Is_Predicate_Function (SIdB);
9737
9738 -- Build function body
9739
9740 Spec :=
9741 Make_Function_Specification (Loc,
9742 Defining_Unit_Name => SIdB,
9743 Parameter_Specifications => New_List (
9744 Make_Parameter_Specification (Loc,
9745 Defining_Identifier =>
9746 Make_Defining_Identifier (Loc, Object_Name),
9747 Parameter_Type =>
9748 New_Occurrence_Of (Typ, Loc))),
9749 Result_Definition =>
9750 New_Occurrence_Of (Standard_Boolean, Loc));
9751
9752 FBody :=
9753 Make_Subprogram_Body (Loc,
9754 Specification => Spec,
9755 Declarations => Empty_List,
9756 Handled_Statement_Sequence =>
9757 Make_Handled_Sequence_Of_Statements (Loc,
9758 Statements => New_List (
9759 Make_Simple_Return_Statement (Loc,
9760 Expression => Expr))));
9761
9762 -- The declaration has been analyzed when created, and placed
9763 -- after type declaration. Insert body itself after freeze node,
9764 -- unless subprogram declaration is already there, in which case
9765 -- body better be placed afterwards.
9766
9767 if FDecl = Next (N) then
9768 Insert_After_And_Analyze (FDecl, FBody);
9769 else
9770 Insert_After_And_Analyze (N, FBody);
9771 end if;
9772
9773 -- The defining identifier of a quantified expression carries the
9774 -- scope in which the type appears, but when unnesting we need
9775 -- to indicate that its proper scope is the constructed predicate
9776 -- function. The quantified expressions have been converted into
9777 -- loops during analysis and expansion.
9778
9779 declare
9780 function Reset_Quantified_Variable_Scope
9781 (N : Node_Id) return Traverse_Result;
9782
9783 procedure Reset_Quantified_Variables_Scope is
9784 new Traverse_Proc (Reset_Quantified_Variable_Scope);
9785
9786 -------------------------------------
9787 -- Reset_Quantified_Variable_Scope --
9788 -------------------------------------
9789
9790 function Reset_Quantified_Variable_Scope
9791 (N : Node_Id) return Traverse_Result
9792 is
9793 begin
9794 if Nkind_In (N, N_Iterator_Specification,
9795 N_Loop_Parameter_Specification)
9796 then
9797 Set_Scope (Defining_Identifier (N),
9798 Predicate_Function (Typ));
9799 end if;
9800
9801 return OK;
9802 end Reset_Quantified_Variable_Scope;
9803
9804 begin
9805 if Unnest_Subprogram_Mode then
9806 Reset_Quantified_Variables_Scope (Expr);
9807 end if;
9808 end;
9809
9810 -- within a generic unit, prevent a double analysis of the body
9811 -- which will not be marked analyzed yet. This will happen when
9812 -- the freeze node is created during the preanalysis of an
9813 -- expression function.
9814
9815 if Inside_A_Generic then
9816 Set_Analyzed (FBody);
9817 end if;
9818
9819 -- Static predicate functions are always side-effect free, and
9820 -- in most cases dynamic predicate functions are as well. Mark
9821 -- them as such whenever possible, so redundant predicate checks
9822 -- can be optimized. If there is a variable reference within the
9823 -- expression, the function is not pure.
9824
9825 if Expander_Active then
9826 Set_Is_Pure (SId,
9827 Side_Effect_Free (Expr, Variable_Ref => True));
9828 Set_Is_Inlined (SId);
9829 end if;
9830 end;
9831
9832 -- Test for raise expressions present and if so build M version
9833
9834 if Raise_Expression_Present then
9835 declare
9836 SId : constant Entity_Id :=
9837 Make_Defining_Identifier (Loc,
9838 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9839 -- The entity for the function spec
9840
9841 SIdB : constant Entity_Id :=
9842 Make_Defining_Identifier (Loc,
9843 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9844 -- The entity for the function body
9845
9846 Spec : Node_Id;
9847 FBody : Node_Id;
9848 FDecl : Node_Id;
9849 BTemp : Entity_Id;
9850
9851 CRec_Typ : Entity_Id;
9852 -- The corresponding record type of Full_Typ
9853
9854 Full_Typ : Entity_Id;
9855 -- The full view of Typ
9856
9857 Priv_Typ : Entity_Id;
9858 -- The partial view of Typ
9859
9860 UFull_Typ : Entity_Id;
9861 -- The underlying full view of Full_Typ
9862
9863 begin
9864 -- Mark any raise expressions for special expansion
9865
9866 Process_REs (Expr_M);
9867
9868 -- Build function declaration
9869
9870 Set_Ekind (SId, E_Function);
9871 Set_Is_Predicate_Function_M (SId);
9872 Set_Predicate_Function_M (Typ, SId);
9873
9874 -- Obtain all views of the input type
9875
9876 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
9877
9878 -- Associate the predicate function with all views
9879
9880 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
9881 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
9882 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
9883 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
9884
9885 Spec :=
9886 Make_Function_Specification (Loc,
9887 Defining_Unit_Name => SId,
9888 Parameter_Specifications => New_List (
9889 Make_Parameter_Specification (Loc,
9890 Defining_Identifier => Object_Entity_M,
9891 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9892 Result_Definition =>
9893 New_Occurrence_Of (Standard_Boolean, Loc));
9894
9895 FDecl :=
9896 Make_Subprogram_Declaration (Loc,
9897 Specification => Spec);
9898
9899 -- Build function body
9900
9901 Spec :=
9902 Make_Function_Specification (Loc,
9903 Defining_Unit_Name => SIdB,
9904 Parameter_Specifications => New_List (
9905 Make_Parameter_Specification (Loc,
9906 Defining_Identifier =>
9907 Make_Defining_Identifier (Loc, Object_Name),
9908 Parameter_Type =>
9909 New_Occurrence_Of (Typ, Loc))),
9910 Result_Definition =>
9911 New_Occurrence_Of (Standard_Boolean, Loc));
9912
9913 -- Build the body, we declare the boolean expression before
9914 -- doing the return, because we are not really confident of
9915 -- what happens if a return appears within a return.
9916
9917 BTemp :=
9918 Make_Defining_Identifier (Loc,
9919 Chars => New_Internal_Name ('B'));
9920
9921 FBody :=
9922 Make_Subprogram_Body (Loc,
9923 Specification => Spec,
9924
9925 Declarations => New_List (
9926 Make_Object_Declaration (Loc,
9927 Defining_Identifier => BTemp,
9928 Constant_Present => True,
9929 Object_Definition =>
9930 New_Occurrence_Of (Standard_Boolean, Loc),
9931 Expression => Expr_M)),
9932
9933 Handled_Statement_Sequence =>
9934 Make_Handled_Sequence_Of_Statements (Loc,
9935 Statements => New_List (
9936 Make_Simple_Return_Statement (Loc,
9937 Expression => New_Occurrence_Of (BTemp, Loc)))));
9938
9939 -- Insert declaration before freeze node and body after
9940
9941 Insert_Before_And_Analyze (N, FDecl);
9942 Insert_After_And_Analyze (N, FBody);
9943
9944 -- Should quantified expressions be handled here as well ???
9945 end;
9946 end if;
9947
9948 -- See if we have a static predicate. Note that the answer may be
9949 -- yes even if we have an explicit Dynamic_Predicate present.
9950
9951 declare
9952 PS : Boolean;
9953 EN : Node_Id;
9954
9955 begin
9956 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
9957 PS := False;
9958 else
9959 PS := Is_Predicate_Static (Expr, Object_Name);
9960 end if;
9961
9962 -- Case where we have a predicate-static aspect
9963
9964 if PS then
9965
9966 -- We don't set Has_Static_Predicate_Aspect, since we can have
9967 -- any of the three cases (Predicate, Dynamic_Predicate, or
9968 -- Static_Predicate) generating a predicate with an expression
9969 -- that is predicate-static. We just indicate that we have a
9970 -- predicate that can be treated as static.
9971
9972 Set_Has_Static_Predicate (Typ);
9973
9974 -- For discrete subtype, build the static predicate list
9975
9976 if Is_Discrete_Type (Typ) then
9977 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
9978
9979 -- If we don't get a static predicate list, it means that we
9980 -- have a case where this is not possible, most typically in
9981 -- the case where we inherit a dynamic predicate. We do not
9982 -- consider this an error, we just leave the predicate as
9983 -- dynamic. But if we do succeed in building the list, then
9984 -- we mark the predicate as static.
9985
9986 if No (Static_Discrete_Predicate (Typ)) then
9987 Set_Has_Static_Predicate (Typ, False);
9988 end if;
9989
9990 -- For real or string subtype, save predicate expression
9991
9992 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9993 Set_Static_Real_Or_String_Predicate (Typ, Expr);
9994 end if;
9995
9996 -- Case of dynamic predicate (expression is not predicate-static)
9997
9998 else
9999 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
10000 -- is only set if we have an explicit Dynamic_Predicate aspect
10001 -- given. Here we may simply have a Predicate aspect where the
10002 -- expression happens not to be predicate-static.
10003
10004 -- Emit an error when the predicate is categorized as static
10005 -- but its expression is not predicate-static.
10006
10007 -- First a little fiddling to get a nice location for the
10008 -- message. If the expression is of the form (A and then B),
10009 -- where A is an inherited predicate, then use the right
10010 -- operand for the Sloc. This avoids getting confused by a call
10011 -- to an inherited predicate with a less convenient source
10012 -- location.
10013
10014 EN := Expr;
10015 while Nkind (EN) = N_And_Then
10016 and then Nkind (Left_Opnd (EN)) = N_Function_Call
10017 and then Is_Predicate_Function
10018 (Entity (Name (Left_Opnd (EN))))
10019 loop
10020 EN := Right_Opnd (EN);
10021 end loop;
10022
10023 -- Now post appropriate message
10024
10025 if Has_Static_Predicate_Aspect (Typ) then
10026 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
10027 Error_Msg_F
10028 ("expression is not predicate-static (RM 3.2.4(16-22))",
10029 EN);
10030 else
10031 Error_Msg_F
10032 ("static predicate requires scalar or string type", EN);
10033 end if;
10034 end if;
10035 end if;
10036 end;
10037 end if;
10038
10039 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10040 end Build_Predicate_Functions;
10041
10042 ------------------------------------------
10043 -- Build_Predicate_Function_Declaration --
10044 ------------------------------------------
10045
10046 -- WARNING: This routine manages Ghost regions. Return statements must be
10047 -- replaced by gotos which jump to the end of the routine and restore the
10048 -- Ghost mode.
10049
10050 function Build_Predicate_Function_Declaration
10051 (Typ : Entity_Id) return Node_Id
10052 is
10053 Loc : constant Source_Ptr := Sloc (Typ);
10054
10055 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10056 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
10057 -- Save the Ghost-related attributes to restore on exit
10058
10059 Func_Decl : Node_Id;
10060 Func_Id : Entity_Id;
10061 Spec : Node_Id;
10062
10063 CRec_Typ : Entity_Id;
10064 -- The corresponding record type of Full_Typ
10065
10066 Full_Typ : Entity_Id;
10067 -- The full view of Typ
10068
10069 Priv_Typ : Entity_Id;
10070 -- The partial view of Typ
10071
10072 UFull_Typ : Entity_Id;
10073 -- The underlying full view of Full_Typ
10074
10075 begin
10076 -- The related type may be subject to pragma Ghost. Set the mode now to
10077 -- ensure that the predicate functions are properly marked as Ghost.
10078
10079 Set_Ghost_Mode (Typ);
10080
10081 Func_Id :=
10082 Make_Defining_Identifier (Loc,
10083 Chars => New_External_Name (Chars (Typ), "Predicate"));
10084
10085 Set_Ekind (Func_Id, E_Function);
10086 Set_Etype (Func_Id, Standard_Boolean);
10087 Set_Is_Internal (Func_Id);
10088 Set_Is_Predicate_Function (Func_Id);
10089 Set_Predicate_Function (Typ, Func_Id);
10090
10091 -- The predicate function requires debug info when the predicates are
10092 -- subject to Source Coverage Obligations.
10093
10094 if Opt.Generate_SCO then
10095 Set_Debug_Info_Needed (Func_Id);
10096 end if;
10097
10098 -- Obtain all views of the input type
10099
10100 Get_Views (Typ, Priv_Typ, Full_Typ, UFull_Typ, CRec_Typ);
10101
10102 -- Associate the predicate function and various flags with all views
10103
10104 Propagate_Predicate_Attributes (Priv_Typ, From_Typ => Typ);
10105 Propagate_Predicate_Attributes (Full_Typ, From_Typ => Typ);
10106 Propagate_Predicate_Attributes (UFull_Typ, From_Typ => Typ);
10107 Propagate_Predicate_Attributes (CRec_Typ, From_Typ => Typ);
10108
10109 Spec :=
10110 Make_Function_Specification (Loc,
10111 Defining_Unit_Name => Func_Id,
10112 Parameter_Specifications => New_List (
10113 Make_Parameter_Specification (Loc,
10114 Defining_Identifier => Make_Temporary (Loc, 'I'),
10115 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
10116 Result_Definition =>
10117 New_Occurrence_Of (Standard_Boolean, Loc));
10118
10119 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
10120
10121 Insert_After (Parent (Typ), Func_Decl);
10122 Analyze (Func_Decl);
10123
10124 Restore_Ghost_Region (Saved_GM, Saved_IGR);
10125
10126 return Func_Decl;
10127 end Build_Predicate_Function_Declaration;
10128
10129 -----------------------------------------
10130 -- Check_Aspect_At_End_Of_Declarations --
10131 -----------------------------------------
10132
10133 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
10134 Ent : constant Entity_Id := Entity (ASN);
10135 Ident : constant Node_Id := Identifier (ASN);
10136 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10137
10138 End_Decl_Expr : constant Node_Id := Entity (Ident);
10139 -- Expression to be analyzed at end of declarations
10140
10141 Freeze_Expr : constant Node_Id := Expression (ASN);
10142 -- Expression from call to Check_Aspect_At_Freeze_Point.
10143
10144 T : constant Entity_Id := Etype (Original_Node (Freeze_Expr));
10145 -- Type required for preanalyze call. We use the original expression to
10146 -- get the proper type, to prevent cascaded errors when the expression
10147 -- is constant-folded.
10148
10149 Err : Boolean;
10150 -- Set False if error
10151
10152 -- On entry to this procedure, Entity (Ident) contains a copy of the
10153 -- original expression from the aspect, saved for this purpose, and
10154 -- but Expression (Ident) is a preanalyzed copy of the expression,
10155 -- preanalyzed just after the freeze point.
10156
10157 procedure Check_Overloaded_Name;
10158 -- For aspects whose expression is simply a name, this routine checks if
10159 -- the name is overloaded or not. If so, it verifies there is an
10160 -- interpretation that matches the entity obtained at the freeze point,
10161 -- otherwise the compiler complains.
10162
10163 ---------------------------
10164 -- Check_Overloaded_Name --
10165 ---------------------------
10166
10167 procedure Check_Overloaded_Name is
10168 begin
10169 if not Is_Overloaded (End_Decl_Expr) then
10170 Err := not Is_Entity_Name (End_Decl_Expr)
10171 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
10172
10173 else
10174 Err := True;
10175
10176 declare
10177 Index : Interp_Index;
10178 It : Interp;
10179
10180 begin
10181 Get_First_Interp (End_Decl_Expr, Index, It);
10182 while Present (It.Typ) loop
10183 if It.Nam = Entity (Freeze_Expr) then
10184 Err := False;
10185 exit;
10186 end if;
10187
10188 Get_Next_Interp (Index, It);
10189 end loop;
10190 end;
10191 end if;
10192 end Check_Overloaded_Name;
10193
10194 -- Start of processing for Check_Aspect_At_End_Of_Declarations
10195
10196 begin
10197 -- In an instance we do not perform the consistency check between freeze
10198 -- point and end of declarations, because it was done already in the
10199 -- analysis of the generic. Furthermore, the delayed analysis of an
10200 -- aspect of the instance may produce spurious errors when the generic
10201 -- is a child unit that references entities in the parent (which might
10202 -- not be in scope at the freeze point of the instance).
10203
10204 if In_Instance then
10205 return;
10206
10207 -- The enclosing scope may have been rewritten during expansion (.e.g. a
10208 -- task body is rewritten as a procedure) after this conformance check
10209 -- has been performed, so do not perform it again (it may not easily be
10210 -- done if full visibility of local entities is not available).
10211
10212 elsif not Comes_From_Source (Current_Scope) then
10213 return;
10214
10215 -- Case of aspects Dimension, Dimension_System and Synchronization
10216
10217 elsif A_Id = Aspect_Synchronization then
10218 return;
10219
10220 -- Case of stream attributes and Put_Image, just have to compare
10221 -- entities. However, the expression is just a possibly-overloaded
10222 -- name, so we need to verify that one of these interpretations is
10223 -- the one available at at the freeze point.
10224
10225 elsif A_Id = Aspect_Input or else
10226 A_Id = Aspect_Output or else
10227 A_Id = Aspect_Read or else
10228 A_Id = Aspect_Write or else
10229 A_Id = Aspect_Put_Image
10230 then
10231 Analyze (End_Decl_Expr);
10232 Check_Overloaded_Name;
10233
10234 elsif A_Id = Aspect_Variable_Indexing or else
10235 A_Id = Aspect_Constant_Indexing or else
10236 A_Id = Aspect_Default_Iterator or else
10237 A_Id = Aspect_Iterator_Element or else
10238 A_Id = Aspect_Integer_Literal or else
10239 A_Id = Aspect_Real_Literal or else
10240 A_Id = Aspect_String_Literal
10241 then
10242 -- Make type unfrozen before analysis, to prevent spurious errors
10243 -- about late attributes.
10244
10245 Set_Is_Frozen (Ent, False);
10246 Analyze (End_Decl_Expr);
10247 Set_Is_Frozen (Ent, True);
10248
10249 -- If the end of declarations comes before any other freeze point,
10250 -- the Freeze_Expr is not analyzed: no check needed.
10251
10252 if Analyzed (Freeze_Expr) and then not In_Instance then
10253 Check_Overloaded_Name;
10254 else
10255 Err := False;
10256 end if;
10257
10258 -- All other cases
10259
10260 else
10261 -- In a generic context freeze nodes are not always generated, so
10262 -- analyze the expression now. If the aspect is for a type, we must
10263 -- also make its potential components accessible.
10264
10265 if not Analyzed (Freeze_Expr) and then Inside_A_Generic then
10266 if A_Id = Aspect_Dynamic_Predicate
10267 or else A_Id = Aspect_Predicate
10268 then
10269 Push_Type (Ent);
10270 Preanalyze_Spec_Expression (Freeze_Expr, Standard_Boolean);
10271 Pop_Type (Ent);
10272
10273 elsif A_Id = Aspect_Priority then
10274 Push_Type (Ent);
10275 Preanalyze_Spec_Expression (Freeze_Expr, Any_Integer);
10276 Pop_Type (Ent);
10277
10278 else
10279 Preanalyze (Freeze_Expr);
10280 end if;
10281 end if;
10282
10283 -- Indicate that the expression comes from an aspect specification,
10284 -- which is used in subsequent analysis even if expansion is off.
10285
10286 Set_Parent (End_Decl_Expr, ASN);
10287
10288 -- In a generic context the original aspect expressions have not
10289 -- been preanalyzed, so do it now. There are no conformance checks
10290 -- to perform in this case. As before, we have to make components
10291 -- visible for aspects that may reference them.
10292
10293 if No (T) then
10294 if A_Id = Aspect_Dynamic_Predicate
10295 or else A_Id = Aspect_Predicate
10296 or else A_Id = Aspect_Priority
10297 then
10298 Push_Type (Ent);
10299 Check_Aspect_At_Freeze_Point (ASN);
10300 Pop_Type (Ent);
10301
10302 else
10303 Check_Aspect_At_Freeze_Point (ASN);
10304 end if;
10305 return;
10306
10307 -- The default values attributes may be defined in the private part,
10308 -- and the analysis of the expression may take place when only the
10309 -- partial view is visible. The expression must be scalar, so use
10310 -- the full view to resolve.
10311
10312 elsif (A_Id = Aspect_Default_Value
10313 or else
10314 A_Id = Aspect_Default_Component_Value)
10315 and then Is_Private_Type (T)
10316 then
10317 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
10318
10319 -- The following aspect expressions may contain references to
10320 -- components and discriminants of the type.
10321
10322 elsif A_Id = Aspect_Dynamic_Predicate
10323 or else A_Id = Aspect_Predicate
10324 or else A_Id = Aspect_Priority
10325 or else A_Id = Aspect_CPU
10326 then
10327 Push_Type (Ent);
10328 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10329 Pop_Type (Ent);
10330
10331 else
10332 Preanalyze_Spec_Expression (End_Decl_Expr, T);
10333 end if;
10334
10335 Err :=
10336 not Fully_Conformant_Expressions
10337 (End_Decl_Expr, Freeze_Expr, Report => True);
10338 end if;
10339
10340 -- Output error message if error. Force error on aspect specification
10341 -- even if there is an error on the expression itself.
10342
10343 if Err then
10344 Error_Msg_NE
10345 ("!visibility of aspect for& changes after freeze point",
10346 ASN, Ent);
10347 Error_Msg_NE
10348 ("info: & is frozen here, (RM 13.1.1 (13/3))??",
10349 Freeze_Node (Ent), Ent);
10350 end if;
10351 end Check_Aspect_At_End_Of_Declarations;
10352
10353 ----------------------------------
10354 -- Check_Aspect_At_Freeze_Point --
10355 ----------------------------------
10356
10357 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
10358 Ident : constant Node_Id := Identifier (ASN);
10359 -- Identifier (use Entity field to save expression)
10360
10361 Expr : constant Node_Id := Expression (ASN);
10362 -- For cases where using Entity (Identifier) doesn't work
10363
10364 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
10365
10366 T : Entity_Id := Empty;
10367 -- Type required for preanalyze call
10368
10369 begin
10370 -- On entry to this procedure, Entity (Ident) contains a copy of the
10371 -- original expression from the aspect, saved for this purpose.
10372
10373 -- On exit from this procedure Entity (Ident) is unchanged, still
10374 -- containing that copy, but Expression (Ident) is a preanalyzed copy
10375 -- of the expression, preanalyzed just after the freeze point.
10376
10377 -- Make a copy of the expression to be preanalyzed
10378
10379 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
10380
10381 -- Find type for preanalyze call
10382
10383 case A_Id is
10384
10385 -- No_Aspect should be impossible
10386
10387 when No_Aspect =>
10388 raise Program_Error;
10389
10390 -- Aspects taking an optional boolean argument
10391
10392 when Boolean_Aspects
10393 | Library_Unit_Aspects
10394 =>
10395 T := Standard_Boolean;
10396
10397 -- Aspects corresponding to attribute definition clauses
10398
10399 when Aspect_Address =>
10400 T := RTE (RE_Address);
10401
10402 when Aspect_Attach_Handler =>
10403 T := RTE (RE_Interrupt_ID);
10404
10405 when Aspect_Bit_Order
10406 | Aspect_Scalar_Storage_Order
10407 =>
10408 T := RTE (RE_Bit_Order);
10409
10410 when Aspect_Convention =>
10411 return;
10412
10413 when Aspect_CPU =>
10414 T := RTE (RE_CPU_Range);
10415
10416 -- Default_Component_Value is resolved with the component type
10417
10418 when Aspect_Default_Component_Value =>
10419 T := Component_Type (Entity (ASN));
10420
10421 when Aspect_Default_Storage_Pool =>
10422 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10423
10424 -- Default_Value is resolved with the type entity in question
10425
10426 when Aspect_Default_Value =>
10427 T := Entity (ASN);
10428
10429 when Aspect_Dispatching_Domain =>
10430 T := RTE (RE_Dispatching_Domain);
10431
10432 when Aspect_External_Tag =>
10433 T := Standard_String;
10434
10435 when Aspect_External_Name =>
10436 T := Standard_String;
10437
10438 when Aspect_Link_Name =>
10439 T := Standard_String;
10440
10441 when Aspect_Interrupt_Priority
10442 | Aspect_Priority
10443 =>
10444 T := Standard_Integer;
10445
10446 when Aspect_Relative_Deadline =>
10447 T := RTE (RE_Time_Span);
10448
10449 when Aspect_Secondary_Stack_Size =>
10450 T := Standard_Integer;
10451
10452 when Aspect_Small =>
10453
10454 -- Note that the expression can be of any real type (not just a
10455 -- real universal literal) as long as it is a static constant.
10456
10457 T := Any_Real;
10458
10459 -- For a simple storage pool, we have to retrieve the type of the
10460 -- pool object associated with the aspect's corresponding attribute
10461 -- definition clause.
10462
10463 when Aspect_Simple_Storage_Pool =>
10464 T := Etype (Expression (Aspect_Rep_Item (ASN)));
10465
10466 when Aspect_Storage_Pool =>
10467 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
10468
10469 when Aspect_Alignment
10470 | Aspect_Component_Size
10471 | Aspect_Machine_Radix
10472 | Aspect_Object_Size
10473 | Aspect_Size
10474 | Aspect_Storage_Size
10475 | Aspect_Stream_Size
10476 | Aspect_Value_Size
10477 =>
10478 T := Any_Integer;
10479
10480 when Aspect_Linker_Section =>
10481 T := Standard_String;
10482
10483 when Aspect_Synchronization =>
10484 return;
10485
10486 -- Special case, the expression of these aspects is just an entity
10487 -- that does not need any resolution, so just analyze.
10488
10489 when Aspect_Input
10490 | Aspect_Output
10491 | Aspect_Put_Image
10492 | Aspect_Read
10493 | Aspect_Suppress
10494 | Aspect_Unsuppress
10495 | Aspect_Warnings
10496 | Aspect_Write
10497 =>
10498 Analyze (Expression (ASN));
10499 return;
10500
10501 -- Same for Iterator aspects, where the expression is a function
10502 -- name. Legality rules are checked separately.
10503
10504 when Aspect_Constant_Indexing
10505 | Aspect_Default_Iterator
10506 | Aspect_Iterator_Element
10507 | Aspect_Variable_Indexing
10508 =>
10509 Analyze (Expression (ASN));
10510 return;
10511
10512 -- Same for Literal aspects, where the expression is a function
10513 -- name. Legality rules are checked separately. Use Expr to avoid
10514 -- losing track of the previous resolution of Expression.
10515
10516 when Aspect_Integer_Literal
10517 | Aspect_Real_Literal
10518 | Aspect_String_Literal
10519 =>
10520 Set_Entity (Expression (ASN), Entity (Expr));
10521 Set_Etype (Expression (ASN), Etype (Expr));
10522 Set_Is_Overloaded (Expression (ASN), False);
10523 Analyze (Expression (ASN));
10524 return;
10525
10526 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
10527
10528 when Aspect_Iterable =>
10529 T := Entity (ASN);
10530
10531 declare
10532 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
10533 Assoc : Node_Id;
10534 Expr : Node_Id;
10535
10536 begin
10537 if Cursor = Any_Type then
10538 return;
10539 end if;
10540
10541 Assoc := First (Component_Associations (Expression (ASN)));
10542 while Present (Assoc) loop
10543 Expr := Expression (Assoc);
10544 Analyze (Expr);
10545
10546 if not Error_Posted (Expr) then
10547 Resolve_Iterable_Operation
10548 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
10549 end if;
10550
10551 Next (Assoc);
10552 end loop;
10553 end;
10554
10555 return;
10556
10557 -- Invariant/Predicate take boolean expressions
10558
10559 when Aspect_Dynamic_Predicate
10560 | Aspect_Invariant
10561 | Aspect_Predicate
10562 | Aspect_Static_Predicate
10563 | Aspect_Type_Invariant
10564 =>
10565 T := Standard_Boolean;
10566
10567 when Aspect_Predicate_Failure =>
10568 T := Standard_String;
10569
10570 -- Here is the list of aspects that don't require delay analysis
10571
10572 when Aspect_Abstract_State
10573 | Aspect_Annotate
10574 | Aspect_Async_Readers
10575 | Aspect_Async_Writers
10576 | Aspect_Constant_After_Elaboration
10577 | Aspect_Contract_Cases
10578 | Aspect_Default_Initial_Condition
10579 | Aspect_Depends
10580 | Aspect_Dimension
10581 | Aspect_Dimension_System
10582 | Aspect_Effective_Reads
10583 | Aspect_Effective_Writes
10584 | Aspect_Extensions_Visible
10585 | Aspect_Ghost
10586 | Aspect_Global
10587 | Aspect_Implicit_Dereference
10588 | Aspect_Initial_Condition
10589 | Aspect_Initializes
10590 | Aspect_Max_Entry_Queue_Depth
10591 | Aspect_Max_Entry_Queue_Length
10592 | Aspect_Max_Queue_Length
10593 | Aspect_No_Caching
10594 | Aspect_Obsolescent
10595 | Aspect_Part_Of
10596 | Aspect_Post
10597 | Aspect_Postcondition
10598 | Aspect_Pre
10599 | Aspect_Precondition
10600 | Aspect_Refined_Depends
10601 | Aspect_Refined_Global
10602 | Aspect_Refined_Post
10603 | Aspect_Refined_State
10604 | Aspect_Relaxed_Initialization
10605 | Aspect_SPARK_Mode
10606 | Aspect_Test_Case
10607 | Aspect_Unimplemented
10608 | Aspect_Volatile_Function
10609 =>
10610 raise Program_Error;
10611
10612 end case;
10613
10614 -- Do the preanalyze call
10615
10616 Preanalyze_Spec_Expression (Expression (ASN), T);
10617 end Check_Aspect_At_Freeze_Point;
10618
10619 -----------------------------------
10620 -- Check_Constant_Address_Clause --
10621 -----------------------------------
10622
10623 procedure Check_Constant_Address_Clause
10624 (Expr : Node_Id;
10625 U_Ent : Entity_Id)
10626 is
10627 procedure Check_At_Constant_Address (Nod : Node_Id);
10628 -- Checks that the given node N represents a name whose 'Address is
10629 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
10630 -- address value is the same at the point of declaration of U_Ent and at
10631 -- the time of elaboration of the address clause.
10632
10633 procedure Check_Expr_Constants (Nod : Node_Id);
10634 -- Checks that Nod meets the requirements for a constant address clause
10635 -- in the sense of the enclosing procedure.
10636
10637 procedure Check_List_Constants (Lst : List_Id);
10638 -- Check that all elements of list Lst meet the requirements for a
10639 -- constant address clause in the sense of the enclosing procedure.
10640
10641 -------------------------------
10642 -- Check_At_Constant_Address --
10643 -------------------------------
10644
10645 procedure Check_At_Constant_Address (Nod : Node_Id) is
10646 begin
10647 if Is_Entity_Name (Nod) then
10648 if Present (Address_Clause (Entity ((Nod)))) then
10649 Error_Msg_NE
10650 ("invalid address clause for initialized object &!",
10651 Nod, U_Ent);
10652 Error_Msg_NE
10653 ("address for& cannot depend on another address clause! "
10654 & "(RM 13.1(22))!", Nod, U_Ent);
10655
10656 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
10657 and then Sloc (U_Ent) < Sloc (Entity (Nod))
10658 then
10659 Error_Msg_NE
10660 ("invalid address clause for initialized object &!",
10661 Nod, U_Ent);
10662 Error_Msg_Node_2 := U_Ent;
10663 Error_Msg_NE
10664 ("\& must be defined before & (RM 13.1(22))!",
10665 Nod, Entity (Nod));
10666 end if;
10667
10668 elsif Nkind (Nod) = N_Selected_Component then
10669 declare
10670 T : constant Entity_Id := Etype (Prefix (Nod));
10671
10672 begin
10673 if (Is_Record_Type (T)
10674 and then Has_Discriminants (T))
10675 or else
10676 (Is_Access_Type (T)
10677 and then Is_Record_Type (Designated_Type (T))
10678 and then Has_Discriminants (Designated_Type (T)))
10679 then
10680 Error_Msg_NE
10681 ("invalid address clause for initialized object &!",
10682 Nod, U_Ent);
10683 Error_Msg_N
10684 ("\address cannot depend on component of discriminated "
10685 & "record (RM 13.1(22))!", Nod);
10686 else
10687 Check_At_Constant_Address (Prefix (Nod));
10688 end if;
10689 end;
10690
10691 elsif Nkind (Nod) = N_Indexed_Component then
10692 Check_At_Constant_Address (Prefix (Nod));
10693 Check_List_Constants (Expressions (Nod));
10694
10695 else
10696 Check_Expr_Constants (Nod);
10697 end if;
10698 end Check_At_Constant_Address;
10699
10700 --------------------------
10701 -- Check_Expr_Constants --
10702 --------------------------
10703
10704 procedure Check_Expr_Constants (Nod : Node_Id) is
10705 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
10706 Ent : Entity_Id := Empty;
10707
10708 begin
10709 if Nkind (Nod) in N_Has_Etype
10710 and then Etype (Nod) = Any_Type
10711 then
10712 return;
10713 end if;
10714
10715 case Nkind (Nod) is
10716 when N_Empty
10717 | N_Error
10718 =>
10719 return;
10720
10721 when N_Expanded_Name
10722 | N_Identifier
10723 =>
10724 Ent := Entity (Nod);
10725
10726 -- We need to look at the original node if it is different
10727 -- from the node, since we may have rewritten things and
10728 -- substituted an identifier representing the rewrite.
10729
10730 if Is_Rewrite_Substitution (Nod) then
10731 Check_Expr_Constants (Original_Node (Nod));
10732
10733 -- If the node is an object declaration without initial
10734 -- value, some code has been expanded, and the expression
10735 -- is not constant, even if the constituents might be
10736 -- acceptable, as in A'Address + offset.
10737
10738 if Ekind (Ent) = E_Variable
10739 and then
10740 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
10741 and then
10742 No (Expression (Declaration_Node (Ent)))
10743 then
10744 Error_Msg_NE
10745 ("invalid address clause for initialized object &!",
10746 Nod, U_Ent);
10747
10748 -- If entity is constant, it may be the result of expanding
10749 -- a check. We must verify that its declaration appears
10750 -- before the object in question, else we also reject the
10751 -- address clause.
10752
10753 elsif Ekind (Ent) = E_Constant
10754 and then In_Same_Source_Unit (Ent, U_Ent)
10755 and then Sloc (Ent) > Loc_U_Ent
10756 then
10757 Error_Msg_NE
10758 ("invalid address clause for initialized object &!",
10759 Nod, U_Ent);
10760 end if;
10761
10762 return;
10763 end if;
10764
10765 -- Otherwise look at the identifier and see if it is OK
10766
10767 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
10768 or else Is_Type (Ent)
10769 then
10770 return;
10771
10772 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
10773
10774 -- This is the case where we must have Ent defined before
10775 -- U_Ent. Clearly if they are in different units this
10776 -- requirement is met since the unit containing Ent is
10777 -- already processed.
10778
10779 if not In_Same_Source_Unit (Ent, U_Ent) then
10780 return;
10781
10782 -- Otherwise location of Ent must be before the location
10783 -- of U_Ent, that's what prior defined means.
10784
10785 elsif Sloc (Ent) < Loc_U_Ent then
10786 return;
10787
10788 else
10789 Error_Msg_NE
10790 ("invalid address clause for initialized object &!",
10791 Nod, U_Ent);
10792 Error_Msg_Node_2 := U_Ent;
10793 Error_Msg_NE
10794 ("\& must be defined before & (RM 13.1(22))!",
10795 Nod, Ent);
10796 end if;
10797
10798 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
10799 Check_Expr_Constants (Original_Node (Nod));
10800
10801 else
10802 Error_Msg_NE
10803 ("invalid address clause for initialized object &!",
10804 Nod, U_Ent);
10805
10806 if Comes_From_Source (Ent) then
10807 Error_Msg_NE
10808 ("\reference to variable& not allowed"
10809 & " (RM 13.1(22))!", Nod, Ent);
10810 else
10811 Error_Msg_N
10812 ("non-static expression not allowed"
10813 & " (RM 13.1(22))!", Nod);
10814 end if;
10815 end if;
10816
10817 when N_Integer_Literal =>
10818
10819 -- If this is a rewritten unchecked conversion, in a system
10820 -- where Address is an integer type, always use the base type
10821 -- for a literal value. This is user-friendly and prevents
10822 -- order-of-elaboration issues with instances of unchecked
10823 -- conversion.
10824
10825 if Nkind (Original_Node (Nod)) = N_Function_Call then
10826 Set_Etype (Nod, Base_Type (Etype (Nod)));
10827 end if;
10828
10829 when N_Character_Literal
10830 | N_Real_Literal
10831 | N_String_Literal
10832 =>
10833 return;
10834
10835 when N_Range =>
10836 Check_Expr_Constants (Low_Bound (Nod));
10837 Check_Expr_Constants (High_Bound (Nod));
10838
10839 when N_Explicit_Dereference =>
10840 Check_Expr_Constants (Prefix (Nod));
10841
10842 when N_Indexed_Component =>
10843 Check_Expr_Constants (Prefix (Nod));
10844 Check_List_Constants (Expressions (Nod));
10845
10846 when N_Slice =>
10847 Check_Expr_Constants (Prefix (Nod));
10848 Check_Expr_Constants (Discrete_Range (Nod));
10849
10850 when N_Selected_Component =>
10851 Check_Expr_Constants (Prefix (Nod));
10852
10853 when N_Attribute_Reference =>
10854 if Nam_In (Attribute_Name (Nod), Name_Address,
10855 Name_Access,
10856 Name_Unchecked_Access,
10857 Name_Unrestricted_Access)
10858 then
10859 Check_At_Constant_Address (Prefix (Nod));
10860
10861 -- Normally, System'To_Address will have been transformed into
10862 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
10863 -- and we don't want to give an error, because the whole point
10864 -- of 'To_Address is that it is static.
10865
10866 elsif Attribute_Name (Nod) = Name_To_Address then
10867 pragma Assert (Operating_Mode = Check_Semantics);
10868 null;
10869
10870 else
10871 Check_Expr_Constants (Prefix (Nod));
10872 Check_List_Constants (Expressions (Nod));
10873 end if;
10874
10875 when N_Aggregate =>
10876 Check_List_Constants (Component_Associations (Nod));
10877 Check_List_Constants (Expressions (Nod));
10878
10879 when N_Component_Association =>
10880 Check_Expr_Constants (Expression (Nod));
10881
10882 when N_Extension_Aggregate =>
10883 Check_Expr_Constants (Ancestor_Part (Nod));
10884 Check_List_Constants (Component_Associations (Nod));
10885 Check_List_Constants (Expressions (Nod));
10886
10887 when N_Null =>
10888 return;
10889
10890 when N_Binary_Op
10891 | N_Membership_Test
10892 | N_Short_Circuit
10893 =>
10894 Check_Expr_Constants (Left_Opnd (Nod));
10895 Check_Expr_Constants (Right_Opnd (Nod));
10896
10897 when N_Unary_Op =>
10898 Check_Expr_Constants (Right_Opnd (Nod));
10899
10900 when N_Allocator
10901 | N_Qualified_Expression
10902 | N_Type_Conversion
10903 | N_Unchecked_Type_Conversion
10904 =>
10905 Check_Expr_Constants (Expression (Nod));
10906
10907 when N_Function_Call =>
10908 if not Is_Pure (Entity (Name (Nod))) then
10909 Error_Msg_NE
10910 ("invalid address clause for initialized object &!",
10911 Nod, U_Ent);
10912
10913 Error_Msg_NE
10914 ("\function & is not pure (RM 13.1(22))!",
10915 Nod, Entity (Name (Nod)));
10916
10917 else
10918 Check_List_Constants (Parameter_Associations (Nod));
10919 end if;
10920
10921 when N_Parameter_Association =>
10922 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
10923
10924 when others =>
10925 Error_Msg_NE
10926 ("invalid address clause for initialized object &!",
10927 Nod, U_Ent);
10928 Error_Msg_NE
10929 ("\must be constant defined before& (RM 13.1(22))!",
10930 Nod, U_Ent);
10931 end case;
10932 end Check_Expr_Constants;
10933
10934 --------------------------
10935 -- Check_List_Constants --
10936 --------------------------
10937
10938 procedure Check_List_Constants (Lst : List_Id) is
10939 Nod1 : Node_Id;
10940
10941 begin
10942 if Present (Lst) then
10943 Nod1 := First (Lst);
10944 while Present (Nod1) loop
10945 Check_Expr_Constants (Nod1);
10946 Next (Nod1);
10947 end loop;
10948 end if;
10949 end Check_List_Constants;
10950
10951 -- Start of processing for Check_Constant_Address_Clause
10952
10953 begin
10954 -- If rep_clauses are to be ignored, no need for legality checks. In
10955 -- particular, no need to pester user about rep clauses that violate the
10956 -- rule on constant addresses, given that these clauses will be removed
10957 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
10958 -- we want to relax these checks.
10959
10960 if not Ignore_Rep_Clauses and not CodePeer_Mode then
10961 Check_Expr_Constants (Expr);
10962 end if;
10963 end Check_Constant_Address_Clause;
10964
10965 ---------------------------
10966 -- Check_Pool_Size_Clash --
10967 ---------------------------
10968
10969 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
10970 Post : Node_Id;
10971
10972 begin
10973 -- We need to find out which one came first. Note that in the case of
10974 -- aspects mixed with pragmas there are cases where the processing order
10975 -- is reversed, which is why we do the check here.
10976
10977 if Sloc (SP) < Sloc (SS) then
10978 Error_Msg_Sloc := Sloc (SP);
10979 Post := SS;
10980 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
10981
10982 else
10983 Error_Msg_Sloc := Sloc (SS);
10984 Post := SP;
10985 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
10986 end if;
10987
10988 Error_Msg_N
10989 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
10990 end Check_Pool_Size_Clash;
10991
10992 ----------------------------------------
10993 -- Check_Record_Representation_Clause --
10994 ----------------------------------------
10995
10996 procedure Check_Record_Representation_Clause (N : Node_Id) is
10997 Loc : constant Source_Ptr := Sloc (N);
10998 Ident : constant Node_Id := Identifier (N);
10999 Rectype : Entity_Id;
11000 Fent : Entity_Id;
11001 CC : Node_Id;
11002 Fbit : Uint := No_Uint;
11003 Lbit : Uint := No_Uint;
11004 Hbit : Uint := Uint_0;
11005 Comp : Entity_Id;
11006 Pcomp : Entity_Id;
11007
11008 Max_Bit_So_Far : Uint;
11009 -- Records the maximum bit position so far. If all field positions
11010 -- are monotonically increasing, then we can skip the circuit for
11011 -- checking for overlap, since no overlap is possible.
11012
11013 Tagged_Parent : Entity_Id := Empty;
11014 -- This is set in the case of an extension for which we have either a
11015 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
11016 -- components are positioned by record representation clauses) on the
11017 -- parent type. In this case we check for overlap between components of
11018 -- this tagged type and the parent component. Tagged_Parent will point
11019 -- to this parent type. For all other cases, Tagged_Parent is Empty.
11020
11021 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
11022 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
11023 -- last bit position for any field in the parent type. We only need to
11024 -- check overlap for fields starting below this point.
11025
11026 Overlap_Check_Required : Boolean;
11027 -- Used to keep track of whether or not an overlap check is required
11028
11029 Overlap_Detected : Boolean := False;
11030 -- Set True if an overlap is detected
11031
11032 Ccount : Natural := 0;
11033 -- Number of component clauses in record rep clause
11034
11035 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
11036 -- Given two entities for record components or discriminants, checks
11037 -- if they have overlapping component clauses and issues errors if so.
11038
11039 procedure Find_Component;
11040 -- Finds component entity corresponding to current component clause (in
11041 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
11042 -- start/stop bits for the field. If there is no matching component or
11043 -- if the matching component does not have a component clause, then
11044 -- that's an error and Comp is set to Empty, but no error message is
11045 -- issued, since the message was already given. Comp is also set to
11046 -- Empty if the current "component clause" is in fact a pragma.
11047
11048 procedure Record_Hole_Check
11049 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean);
11050 -- Checks for gaps in the given Rectype. Compute After_Last, the bit
11051 -- number after the last component. Warn is True on the initial call,
11052 -- and warnings are given for gaps. For a type extension, this is called
11053 -- recursively to compute After_Last for the parent type; in this case
11054 -- Warn is False and the warnings are suppressed.
11055
11056 procedure Component_Order_Check (Rectype : Entity_Id);
11057 -- Check that the order of component clauses agrees with the order of
11058 -- component declarations, and that the component clauses are given in
11059 -- increasing order of bit offset.
11060
11061 -----------------------------
11062 -- Check_Component_Overlap --
11063 -----------------------------
11064
11065 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
11066 CC1 : constant Node_Id := Component_Clause (C1_Ent);
11067 CC2 : constant Node_Id := Component_Clause (C2_Ent);
11068
11069 begin
11070 if Present (CC1) and then Present (CC2) then
11071
11072 -- Exclude odd case where we have two tag components in the same
11073 -- record, both at location zero. This seems a bit strange, but
11074 -- it seems to happen in some circumstances, perhaps on an error.
11075
11076 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
11077 return;
11078 end if;
11079
11080 -- Here we check if the two fields overlap
11081
11082 declare
11083 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
11084 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
11085 E1 : constant Uint := S1 + Esize (C1_Ent);
11086 E2 : constant Uint := S2 + Esize (C2_Ent);
11087
11088 begin
11089 if E2 <= S1 or else E1 <= S2 then
11090 null;
11091 else
11092 Error_Msg_Node_2 := Component_Name (CC2);
11093 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
11094 Error_Msg_Node_1 := Component_Name (CC1);
11095 Error_Msg_N
11096 ("component& overlaps & #", Component_Name (CC1));
11097 Overlap_Detected := True;
11098 end if;
11099 end;
11100 end if;
11101 end Check_Component_Overlap;
11102
11103 ---------------------------
11104 -- Component_Order_Check --
11105 ---------------------------
11106
11107 procedure Component_Order_Check (Rectype : Entity_Id) is
11108 Comp : Entity_Id := First_Component (Rectype);
11109 Clause : Node_Id := First (Component_Clauses (N));
11110 Prev_Bit_Offset : Uint := Uint_0;
11111 OOO : constant String :=
11112 "?component clause out of order with respect to declaration";
11113
11114 begin
11115 -- Step Comp through components and Clause through component clauses,
11116 -- skipping pragmas. We ignore discriminants and variant parts,
11117 -- because we get most of the benefit from the plain vanilla
11118 -- component cases, without the extra complexity. If we find a Comp
11119 -- and Clause that don't match, give a warning on both and quit. If
11120 -- we find two subsequent clauses out of order by bit layout, give
11121 -- warning and quit. On each iteration, Prev_Bit_Offset is the one
11122 -- from the previous iteration (or 0 to start).
11123
11124 while Present (Comp) and then Present (Clause) loop
11125 if Nkind (Clause) = N_Component_Clause
11126 and then Ekind (Entity (Component_Name (Clause))) = E_Component
11127 then
11128 if Entity (Component_Name (Clause)) /= Comp then
11129 Error_Msg_N (OOO, Comp);
11130 Error_Msg_N (OOO, Clause);
11131 exit;
11132 end if;
11133
11134 if not Reverse_Bit_Order (Rectype)
11135 and then not Reverse_Storage_Order (Rectype)
11136 and then Component_Bit_Offset (Comp) < Prev_Bit_Offset
11137 then
11138 Error_Msg_N ("?memory layout out of order", Clause);
11139 exit;
11140 end if;
11141
11142 Prev_Bit_Offset := Component_Bit_Offset (Comp);
11143 Next_Component (Comp);
11144 end if;
11145
11146 Next (Clause);
11147 end loop;
11148 end Component_Order_Check;
11149
11150 --------------------
11151 -- Find_Component --
11152 --------------------
11153
11154 procedure Find_Component is
11155
11156 procedure Search_Component (R : Entity_Id);
11157 -- Search components of R for a match. If found, Comp is set
11158
11159 ----------------------
11160 -- Search_Component --
11161 ----------------------
11162
11163 procedure Search_Component (R : Entity_Id) is
11164 begin
11165 Comp := First_Component_Or_Discriminant (R);
11166 while Present (Comp) loop
11167
11168 -- Ignore error of attribute name for component name (we
11169 -- already gave an error message for this, so no need to
11170 -- complain here)
11171
11172 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
11173 null;
11174 else
11175 exit when Chars (Comp) = Chars (Component_Name (CC));
11176 end if;
11177
11178 Next_Component_Or_Discriminant (Comp);
11179 end loop;
11180 end Search_Component;
11181
11182 -- Start of processing for Find_Component
11183
11184 begin
11185 -- Return with Comp set to Empty if we have a pragma
11186
11187 if Nkind (CC) = N_Pragma then
11188 Comp := Empty;
11189 return;
11190 end if;
11191
11192 -- Search current record for matching component
11193
11194 Search_Component (Rectype);
11195
11196 -- If not found, maybe component of base type discriminant that is
11197 -- absent from statically constrained first subtype.
11198
11199 if No (Comp) then
11200 Search_Component (Base_Type (Rectype));
11201 end if;
11202
11203 -- If no component, or the component does not reference the component
11204 -- clause in question, then there was some previous error for which
11205 -- we already gave a message, so just return with Comp Empty.
11206
11207 if No (Comp) or else Component_Clause (Comp) /= CC then
11208 Check_Error_Detected;
11209 Comp := Empty;
11210
11211 -- Normal case where we have a component clause
11212
11213 else
11214 Fbit := Component_Bit_Offset (Comp);
11215 Lbit := Fbit + Esize (Comp) - 1;
11216 end if;
11217 end Find_Component;
11218
11219 -----------------------
11220 -- Record_Hole_Check --
11221 -----------------------
11222
11223 procedure Record_Hole_Check
11224 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean)
11225 is
11226 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
11227 -- Full declaration of record type
11228
11229 procedure Check_Component_List
11230 (DS : List_Id;
11231 CL : Node_Id;
11232 Sbit : Uint;
11233 Abit : out Uint);
11234 -- Check component list CL for holes. DS is a list of discriminant
11235 -- specifications to be included in the consideration of components.
11236 -- Sbit is the starting bit, which is zero if there are no preceding
11237 -- components (before a variant part, or a parent type, or a tag
11238 -- field). If there are preceding components, Sbit is the bit just
11239 -- after the last such component. Abit is set to the bit just after
11240 -- the last component of DS and CL.
11241
11242 --------------------------
11243 -- Check_Component_List --
11244 --------------------------
11245
11246 procedure Check_Component_List
11247 (DS : List_Id;
11248 CL : Node_Id;
11249 Sbit : Uint;
11250 Abit : out Uint)
11251 is
11252 Compl : Integer;
11253
11254 begin
11255 Compl := Integer (List_Length (Component_Items (CL)));
11256
11257 if DS /= No_List then
11258 Compl := Compl + Integer (List_Length (DS));
11259 end if;
11260
11261 declare
11262 Comps : array (Natural range 0 .. Compl) of Entity_Id;
11263 -- Gather components (zero entry is for sort routine)
11264
11265 Ncomps : Natural := 0;
11266 -- Number of entries stored in Comps (starting at Comps (1))
11267
11268 Citem : Node_Id;
11269 -- One component item or discriminant specification
11270
11271 Nbit : Uint;
11272 -- Starting bit for next component
11273
11274 CEnt : Entity_Id;
11275 -- Component entity
11276
11277 Variant : Node_Id;
11278 -- One variant
11279
11280 function Lt (Op1, Op2 : Natural) return Boolean;
11281 -- Compare routine for Sort
11282
11283 procedure Move (From : Natural; To : Natural);
11284 -- Move routine for Sort
11285
11286 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
11287
11288 --------
11289 -- Lt --
11290 --------
11291
11292 function Lt (Op1, Op2 : Natural) return Boolean is
11293 begin
11294 return Component_Bit_Offset (Comps (Op1))
11295 < Component_Bit_Offset (Comps (Op2));
11296 end Lt;
11297
11298 ----------
11299 -- Move --
11300 ----------
11301
11302 procedure Move (From : Natural; To : Natural) is
11303 begin
11304 Comps (To) := Comps (From);
11305 end Move;
11306
11307 begin
11308 -- Gather discriminants into Comp
11309
11310 if DS /= No_List then
11311 Citem := First (DS);
11312 while Present (Citem) loop
11313 if Nkind (Citem) = N_Discriminant_Specification then
11314 declare
11315 Ent : constant Entity_Id :=
11316 Defining_Identifier (Citem);
11317 begin
11318 if Ekind (Ent) = E_Discriminant then
11319 Ncomps := Ncomps + 1;
11320 Comps (Ncomps) := Ent;
11321 end if;
11322 end;
11323 end if;
11324
11325 Next (Citem);
11326 end loop;
11327 end if;
11328
11329 -- Gather component entities into Comp
11330
11331 Citem := First (Component_Items (CL));
11332 while Present (Citem) loop
11333 if Nkind (Citem) = N_Component_Declaration then
11334 Ncomps := Ncomps + 1;
11335 Comps (Ncomps) := Defining_Identifier (Citem);
11336 end if;
11337
11338 Next (Citem);
11339 end loop;
11340
11341 -- Now sort the component entities based on the first bit.
11342 -- Note we already know there are no overlapping components.
11343
11344 Sorting.Sort (Ncomps);
11345
11346 -- Loop through entries checking for holes
11347
11348 Nbit := Sbit;
11349 for J in 1 .. Ncomps loop
11350 CEnt := Comps (J);
11351 pragma Annotate (CodePeer, Modified, CEnt);
11352
11353 declare
11354 CBO : constant Uint := Component_Bit_Offset (CEnt);
11355
11356 begin
11357 -- Skip components with unknown offsets
11358
11359 if CBO /= No_Uint and then CBO >= 0 then
11360 Error_Msg_Uint_1 := CBO - Nbit;
11361
11362 if Warn and then Error_Msg_Uint_1 > 0 then
11363 Error_Msg_NE
11364 ("?H?^-bit gap before component&",
11365 Component_Name (Component_Clause (CEnt)),
11366 CEnt);
11367 end if;
11368
11369 Nbit := CBO + Esize (CEnt);
11370 end if;
11371 end;
11372 end loop;
11373
11374 -- Set Abit to just after the last nonvariant component
11375
11376 Abit := Nbit;
11377
11378 -- Process variant parts recursively if present. Set Abit to
11379 -- the maximum for all variant parts.
11380
11381 if Present (Variant_Part (CL)) then
11382 declare
11383 Var_Start : constant Uint := Nbit;
11384 begin
11385 Variant := First (Variants (Variant_Part (CL)));
11386 while Present (Variant) loop
11387 Check_Component_List
11388 (No_List, Component_List (Variant), Var_Start, Nbit);
11389 Next (Variant);
11390 if Nbit > Abit then
11391 Abit := Nbit;
11392 end if;
11393 end loop;
11394 end;
11395 end if;
11396 end;
11397 end Check_Component_List;
11398
11399 Sbit : Uint;
11400 -- Starting bit for call to Check_Component_List. Zero for an
11401 -- untagged type. The size of the Tag for a nonderived tagged
11402 -- type. Parent size for a type extension.
11403
11404 Record_Definition : Node_Id;
11405 -- Record_Definition containing Component_List to pass to
11406 -- Check_Component_List.
11407
11408 -- Start of processing for Record_Hole_Check
11409
11410 begin
11411 if Is_Tagged_Type (Rectype) then
11412 Sbit := UI_From_Int (System_Address_Size);
11413 else
11414 Sbit := Uint_0;
11415 end if;
11416
11417 After_Last := Uint_0;
11418
11419 if Nkind (Decl) = N_Full_Type_Declaration then
11420 Record_Definition := Type_Definition (Decl);
11421
11422 -- If we have a record extension, set Sbit to point after the last
11423 -- component of the parent type, by calling Record_Hole_Check
11424 -- recursively.
11425
11426 if Nkind (Record_Definition) = N_Derived_Type_Definition then
11427 Record_Definition := Record_Extension_Part (Record_Definition);
11428 Record_Hole_Check (Underlying_Type (Parent_Subtype (Rectype)),
11429 After_Last => Sbit, Warn => False);
11430 end if;
11431
11432 if Nkind (Record_Definition) = N_Record_Definition then
11433 Check_Component_List
11434 (Discriminant_Specifications (Decl),
11435 Component_List (Record_Definition),
11436 Sbit, After_Last);
11437 end if;
11438 end if;
11439 end Record_Hole_Check;
11440
11441 -- Start of processing for Check_Record_Representation_Clause
11442
11443 begin
11444 Find_Type (Ident);
11445 Rectype := Entity (Ident);
11446
11447 if Rectype = Any_Type then
11448 return;
11449 end if;
11450
11451 Rectype := Underlying_Type (Rectype);
11452
11453 -- See if we have a fully repped derived tagged type
11454
11455 declare
11456 PS : constant Entity_Id := Parent_Subtype (Rectype);
11457
11458 begin
11459 if Present (PS) and then Known_Static_RM_Size (PS) then
11460 Tagged_Parent := PS;
11461 Parent_Last_Bit := RM_Size (PS) - 1;
11462
11463 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
11464 Tagged_Parent := PS;
11465
11466 -- Find maximum bit of any component of the parent type
11467
11468 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
11469 Pcomp := First_Entity (Tagged_Parent);
11470 while Present (Pcomp) loop
11471 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
11472 if Component_Bit_Offset (Pcomp) /= No_Uint
11473 and then Known_Static_Esize (Pcomp)
11474 then
11475 Parent_Last_Bit :=
11476 UI_Max
11477 (Parent_Last_Bit,
11478 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
11479 end if;
11480 else
11481
11482 -- Skip anonymous types generated for constrained array
11483 -- or record components.
11484
11485 null;
11486 end if;
11487
11488 Next_Entity (Pcomp);
11489 end loop;
11490 end if;
11491 end;
11492
11493 -- All done if no component clauses
11494
11495 CC := First (Component_Clauses (N));
11496
11497 if No (CC) then
11498 return;
11499 end if;
11500
11501 -- If a tag is present, then create a component clause that places it
11502 -- at the start of the record (otherwise gigi may place it after other
11503 -- fields that have rep clauses).
11504
11505 Fent := First_Entity (Rectype);
11506
11507 if Nkind (Fent) = N_Defining_Identifier
11508 and then Chars (Fent) = Name_uTag
11509 then
11510 Set_Component_Bit_Offset (Fent, Uint_0);
11511 Set_Normalized_Position (Fent, Uint_0);
11512 Set_Normalized_First_Bit (Fent, Uint_0);
11513 Set_Normalized_Position_Max (Fent, Uint_0);
11514 Init_Esize (Fent, System_Address_Size);
11515
11516 Set_Component_Clause (Fent,
11517 Make_Component_Clause (Loc,
11518 Component_Name => Make_Identifier (Loc, Name_uTag),
11519
11520 Position => Make_Integer_Literal (Loc, Uint_0),
11521 First_Bit => Make_Integer_Literal (Loc, Uint_0),
11522 Last_Bit =>
11523 Make_Integer_Literal (Loc,
11524 UI_From_Int (System_Address_Size - 1))));
11525
11526 Ccount := Ccount + 1;
11527 end if;
11528
11529 Max_Bit_So_Far := Uint_Minus_1;
11530 Overlap_Check_Required := False;
11531
11532 -- Process the component clauses
11533
11534 while Present (CC) loop
11535 Find_Component;
11536
11537 if Present (Comp) then
11538 Ccount := Ccount + 1;
11539
11540 -- We need a full overlap check if record positions non-monotonic
11541
11542 if Fbit <= Max_Bit_So_Far then
11543 Overlap_Check_Required := True;
11544 end if;
11545
11546 Max_Bit_So_Far := Lbit;
11547
11548 -- Check bit position out of range of specified size
11549
11550 if Has_Size_Clause (Rectype)
11551 and then RM_Size (Rectype) <= Lbit
11552 then
11553 Error_Msg_Uint_1 := RM_Size (Rectype);
11554 Error_Msg_Uint_2 := Lbit + 1;
11555 Error_Msg_N ("bit number out of range of specified "
11556 & "size (expected ^, got ^)",
11557 Last_Bit (CC));
11558
11559 -- Check for overlap with tag or parent component
11560
11561 else
11562 if Is_Tagged_Type (Rectype)
11563 and then Fbit < System_Address_Size
11564 then
11565 Error_Msg_NE
11566 ("component overlaps tag field of&",
11567 Component_Name (CC), Rectype);
11568 Overlap_Detected := True;
11569
11570 elsif Present (Tagged_Parent)
11571 and then Fbit <= Parent_Last_Bit
11572 then
11573 Error_Msg_NE
11574 ("component overlaps parent field of&",
11575 Component_Name (CC), Rectype);
11576 Overlap_Detected := True;
11577 end if;
11578
11579 if Hbit < Lbit then
11580 Hbit := Lbit;
11581 end if;
11582 end if;
11583 end if;
11584
11585 Next (CC);
11586 end loop;
11587
11588 -- Now that we have processed all the component clauses, check for
11589 -- overlap. We have to leave this till last, since the components can
11590 -- appear in any arbitrary order in the representation clause.
11591
11592 -- We do not need this check if all specified ranges were monotonic,
11593 -- as recorded by Overlap_Check_Required being False at this stage.
11594
11595 -- This first section checks if there are any overlapping entries at
11596 -- all. It does this by sorting all entries and then seeing if there are
11597 -- any overlaps. If there are none, then that is decisive, but if there
11598 -- are overlaps, they may still be OK (they may result from fields in
11599 -- different variants).
11600
11601 if Overlap_Check_Required then
11602 Overlap_Check1 : declare
11603
11604 OC_Fbit : array (0 .. Ccount) of Uint;
11605 -- First-bit values for component clauses, the value is the offset
11606 -- of the first bit of the field from start of record. The zero
11607 -- entry is for use in sorting.
11608
11609 OC_Lbit : array (0 .. Ccount) of Uint;
11610 -- Last-bit values for component clauses, the value is the offset
11611 -- of the last bit of the field from start of record. The zero
11612 -- entry is for use in sorting.
11613
11614 OC_Count : Natural := 0;
11615 -- Count of entries in OC_Fbit and OC_Lbit
11616
11617 function OC_Lt (Op1, Op2 : Natural) return Boolean;
11618 -- Compare routine for Sort
11619
11620 procedure OC_Move (From : Natural; To : Natural);
11621 -- Move routine for Sort
11622
11623 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
11624
11625 -----------
11626 -- OC_Lt --
11627 -----------
11628
11629 function OC_Lt (Op1, Op2 : Natural) return Boolean is
11630 begin
11631 return OC_Fbit (Op1) < OC_Fbit (Op2);
11632 end OC_Lt;
11633
11634 -------------
11635 -- OC_Move --
11636 -------------
11637
11638 procedure OC_Move (From : Natural; To : Natural) is
11639 begin
11640 OC_Fbit (To) := OC_Fbit (From);
11641 OC_Lbit (To) := OC_Lbit (From);
11642 end OC_Move;
11643
11644 -- Start of processing for Overlap_Check
11645
11646 begin
11647 CC := First (Component_Clauses (N));
11648 while Present (CC) loop
11649
11650 -- Exclude component clause already marked in error
11651
11652 if not Error_Posted (CC) then
11653 Find_Component;
11654
11655 if Present (Comp) then
11656 OC_Count := OC_Count + 1;
11657 OC_Fbit (OC_Count) := Fbit;
11658 OC_Lbit (OC_Count) := Lbit;
11659 end if;
11660 end if;
11661
11662 Next (CC);
11663 end loop;
11664
11665 Sorting.Sort (OC_Count);
11666
11667 Overlap_Check_Required := False;
11668 for J in 1 .. OC_Count - 1 loop
11669 if OC_Lbit (J) >= OC_Fbit (J + 1) then
11670 Overlap_Check_Required := True;
11671 exit;
11672 end if;
11673 end loop;
11674 end Overlap_Check1;
11675 end if;
11676
11677 -- If Overlap_Check_Required is still True, then we have to do the full
11678 -- scale overlap check, since we have at least two fields that do
11679 -- overlap, and we need to know if that is OK since they are in
11680 -- different variant, or whether we have a definite problem.
11681
11682 if Overlap_Check_Required then
11683 Overlap_Check2 : declare
11684 C1_Ent, C2_Ent : Entity_Id;
11685 -- Entities of components being checked for overlap
11686
11687 Clist : Node_Id;
11688 -- Component_List node whose Component_Items are being checked
11689
11690 Citem : Node_Id;
11691 -- Component declaration for component being checked
11692
11693 begin
11694 C1_Ent := First_Entity (Base_Type (Rectype));
11695
11696 -- Loop through all components in record. For each component check
11697 -- for overlap with any of the preceding elements on the component
11698 -- list containing the component and also, if the component is in
11699 -- a variant, check against components outside the case structure.
11700 -- This latter test is repeated recursively up the variant tree.
11701
11702 Main_Component_Loop : while Present (C1_Ent) loop
11703 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
11704 goto Continue_Main_Component_Loop;
11705 end if;
11706
11707 -- Skip overlap check if entity has no declaration node. This
11708 -- happens with discriminants in constrained derived types.
11709 -- Possibly we are missing some checks as a result, but that
11710 -- does not seem terribly serious.
11711
11712 if No (Declaration_Node (C1_Ent)) then
11713 goto Continue_Main_Component_Loop;
11714 end if;
11715
11716 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
11717
11718 -- Loop through component lists that need checking. Check the
11719 -- current component list and all lists in variants above us.
11720
11721 Component_List_Loop : loop
11722
11723 -- If derived type definition, go to full declaration
11724 -- If at outer level, check discriminants if there are any.
11725
11726 if Nkind (Clist) = N_Derived_Type_Definition then
11727 Clist := Parent (Clist);
11728 end if;
11729
11730 -- Outer level of record definition, check discriminants
11731 -- but be careful not to flag a non-girder discriminant
11732 -- and the girder discriminant it renames as overlapping.
11733
11734 if Nkind_In (Clist, N_Full_Type_Declaration,
11735 N_Private_Type_Declaration)
11736 then
11737 if Has_Discriminants (Defining_Identifier (Clist)) then
11738 C2_Ent :=
11739 First_Discriminant (Defining_Identifier (Clist));
11740 while Present (C2_Ent) loop
11741 exit when
11742 Original_Record_Component (C1_Ent) =
11743 Original_Record_Component (C2_Ent);
11744 Check_Component_Overlap (C1_Ent, C2_Ent);
11745 Next_Discriminant (C2_Ent);
11746 end loop;
11747 end if;
11748
11749 -- Record extension case
11750
11751 elsif Nkind (Clist) = N_Derived_Type_Definition then
11752 Clist := Empty;
11753
11754 -- Otherwise check one component list
11755
11756 else
11757 Citem := First (Component_Items (Clist));
11758 while Present (Citem) loop
11759 if Nkind (Citem) = N_Component_Declaration then
11760 C2_Ent := Defining_Identifier (Citem);
11761 exit when C1_Ent = C2_Ent;
11762 Check_Component_Overlap (C1_Ent, C2_Ent);
11763 end if;
11764
11765 Next (Citem);
11766 end loop;
11767 end if;
11768
11769 -- Check for variants above us (the parent of the Clist can
11770 -- be a variant, in which case its parent is a variant part,
11771 -- and the parent of the variant part is a component list
11772 -- whose components must all be checked against the current
11773 -- component for overlap).
11774
11775 if Nkind (Parent (Clist)) = N_Variant then
11776 Clist := Parent (Parent (Parent (Clist)));
11777
11778 -- Check for possible discriminant part in record, this
11779 -- is treated essentially as another level in the
11780 -- recursion. For this case the parent of the component
11781 -- list is the record definition, and its parent is the
11782 -- full type declaration containing the discriminant
11783 -- specifications.
11784
11785 elsif Nkind (Parent (Clist)) = N_Record_Definition then
11786 Clist := Parent (Parent ((Clist)));
11787
11788 -- If neither of these two cases, we are at the top of
11789 -- the tree.
11790
11791 else
11792 exit Component_List_Loop;
11793 end if;
11794 end loop Component_List_Loop;
11795
11796 <<Continue_Main_Component_Loop>>
11797 Next_Entity (C1_Ent);
11798
11799 end loop Main_Component_Loop;
11800 end Overlap_Check2;
11801 end if;
11802
11803 -- Skip the following warnings if overlap was detected; programmer
11804 -- should fix the errors first.
11805
11806 if not Overlap_Detected then
11807 -- Check for record holes (gaps)
11808
11809 if Warn_On_Record_Holes then
11810 declare
11811 Ignore : Uint;
11812 begin
11813 Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True);
11814 end;
11815 end if;
11816
11817 -- Check for out-of-order component clauses
11818
11819 if Warn_On_Component_Order then
11820 Component_Order_Check (Rectype);
11821 end if;
11822 end if;
11823
11824 -- For records that have component clauses for all components, and whose
11825 -- size is less than or equal to 32, and which can be fully packed, we
11826 -- need to know the size in the front end to activate possible packed
11827 -- array processing where the component type is a record.
11828
11829 -- At this stage Hbit + 1 represents the first unused bit from all the
11830 -- component clauses processed, so if the component clauses are
11831 -- complete, then this is the length of the record.
11832
11833 -- For records longer than System.Storage_Unit, and for those where not
11834 -- all components have component clauses, the back end determines the
11835 -- length (it may for example be appropriate to round up the size
11836 -- to some convenient boundary, based on alignment considerations, etc).
11837
11838 if Unknown_RM_Size (Rectype)
11839 and then Hbit + 1 <= 32
11840 and then not Strict_Alignment (Rectype)
11841 then
11842
11843 -- Nothing to do if at least one component has no component clause
11844
11845 Comp := First_Component_Or_Discriminant (Rectype);
11846 while Present (Comp) loop
11847 exit when No (Component_Clause (Comp));
11848 Next_Component_Or_Discriminant (Comp);
11849 end loop;
11850
11851 -- If we fall out of loop, all components have component clauses
11852 -- and so we can set the size to the maximum value.
11853
11854 if No (Comp) then
11855 Set_RM_Size (Rectype, Hbit + 1);
11856 end if;
11857 end if;
11858 end Check_Record_Representation_Clause;
11859
11860 ----------------
11861 -- Check_Size --
11862 ----------------
11863
11864 procedure Check_Size
11865 (N : Node_Id;
11866 T : Entity_Id;
11867 Siz : Uint;
11868 Biased : out Boolean)
11869 is
11870 procedure Size_Too_Small_Error (Min_Siz : Uint);
11871 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
11872 -- minimum size.
11873
11874 --------------------------
11875 -- Size_Too_Small_Error --
11876 --------------------------
11877
11878 procedure Size_Too_Small_Error (Min_Siz : Uint) is
11879 begin
11880 Error_Msg_Uint_1 := Min_Siz;
11881 Error_Msg_NE (Size_Too_Small_Message, N, T);
11882 end Size_Too_Small_Error;
11883
11884 -- Local variables
11885
11886 UT : constant Entity_Id := Underlying_Type (T);
11887 M : Uint;
11888
11889 -- Start of processing for Check_Size
11890
11891 begin
11892 Biased := False;
11893
11894 -- Reject patently improper size values
11895
11896 if Is_Elementary_Type (T)
11897 and then Siz > UI_From_Int (Int'Last)
11898 then
11899 Error_Msg_N ("Size value too large for elementary type", N);
11900
11901 if Nkind (Original_Node (N)) = N_Op_Expon then
11902 Error_Msg_N
11903 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
11904 end if;
11905 end if;
11906
11907 -- Dismiss generic types
11908
11909 if Is_Generic_Type (T)
11910 or else
11911 Is_Generic_Type (UT)
11912 or else
11913 Is_Generic_Type (Root_Type (UT))
11914 then
11915 return;
11916
11917 -- Guard against previous errors
11918
11919 elsif No (UT) or else UT = Any_Type then
11920 Check_Error_Detected;
11921 return;
11922
11923 -- Check case of bit packed array
11924
11925 elsif Is_Array_Type (UT)
11926 and then Known_Static_Component_Size (UT)
11927 and then Is_Bit_Packed_Array (UT)
11928 then
11929 declare
11930 Asiz : Uint;
11931 Indx : Node_Id;
11932 Ityp : Entity_Id;
11933
11934 begin
11935 Asiz := Component_Size (UT);
11936 Indx := First_Index (UT);
11937 loop
11938 Ityp := Etype (Indx);
11939
11940 -- If non-static bound, then we are not in the business of
11941 -- trying to check the length, and indeed an error will be
11942 -- issued elsewhere, since sizes of non-static array types
11943 -- cannot be set implicitly or explicitly.
11944
11945 if not Is_OK_Static_Subtype (Ityp) then
11946 return;
11947 end if;
11948
11949 -- Otherwise accumulate next dimension
11950
11951 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
11952 Expr_Value (Type_Low_Bound (Ityp)) +
11953 Uint_1);
11954
11955 Next_Index (Indx);
11956 exit when No (Indx);
11957 end loop;
11958
11959 if Asiz <= Siz then
11960 return;
11961
11962 else
11963 Size_Too_Small_Error (Asiz);
11964 Set_Esize (T, Asiz);
11965 Set_RM_Size (T, Asiz);
11966 end if;
11967 end;
11968
11969 -- All other composite types are ignored
11970
11971 elsif Is_Composite_Type (UT) then
11972 return;
11973
11974 -- For fixed-point types, don't check minimum if type is not frozen,
11975 -- since we don't know all the characteristics of the type that can
11976 -- affect the size (e.g. a specified small) till freeze time.
11977
11978 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
11979 null;
11980
11981 -- Cases for which a minimum check is required
11982
11983 else
11984 -- Ignore if specified size is correct for the type
11985
11986 if Known_Esize (UT) and then Siz = Esize (UT) then
11987 return;
11988 end if;
11989
11990 -- Otherwise get minimum size
11991
11992 M := UI_From_Int (Minimum_Size (UT));
11993
11994 if Siz < M then
11995
11996 -- Size is less than minimum size, but one possibility remains
11997 -- that we can manage with the new size if we bias the type.
11998
11999 M := UI_From_Int (Minimum_Size (UT, Biased => True));
12000
12001 if Siz < M then
12002 Size_Too_Small_Error (M);
12003 Set_Esize (T, M);
12004 Set_RM_Size (T, M);
12005 else
12006 Biased := True;
12007 end if;
12008 end if;
12009 end if;
12010 end Check_Size;
12011
12012 --------------------------
12013 -- Freeze_Entity_Checks --
12014 --------------------------
12015
12016 procedure Freeze_Entity_Checks (N : Node_Id) is
12017 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
12018 -- Inspect the primitive operations of type Typ and hide all pairs of
12019 -- implicitly declared non-overridden non-fully conformant homographs
12020 -- (Ada RM 8.3 12.3/2).
12021
12022 -------------------------------------
12023 -- Hide_Non_Overridden_Subprograms --
12024 -------------------------------------
12025
12026 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
12027 procedure Hide_Matching_Homographs
12028 (Subp_Id : Entity_Id;
12029 Start_Elmt : Elmt_Id);
12030 -- Inspect a list of primitive operations starting with Start_Elmt
12031 -- and find matching implicitly declared non-overridden non-fully
12032 -- conformant homographs of Subp_Id. If found, all matches along
12033 -- with Subp_Id are hidden from all visibility.
12034
12035 function Is_Non_Overridden_Or_Null_Procedure
12036 (Subp_Id : Entity_Id) return Boolean;
12037 -- Determine whether subprogram Subp_Id is implicitly declared non-
12038 -- overridden subprogram or an implicitly declared null procedure.
12039
12040 ------------------------------
12041 -- Hide_Matching_Homographs --
12042 ------------------------------
12043
12044 procedure Hide_Matching_Homographs
12045 (Subp_Id : Entity_Id;
12046 Start_Elmt : Elmt_Id)
12047 is
12048 Prim : Entity_Id;
12049 Prim_Elmt : Elmt_Id;
12050
12051 begin
12052 Prim_Elmt := Start_Elmt;
12053 while Present (Prim_Elmt) loop
12054 Prim := Node (Prim_Elmt);
12055
12056 -- The current primitive is implicitly declared non-overridden
12057 -- non-fully conformant homograph of Subp_Id. Both subprograms
12058 -- must be hidden from visibility.
12059
12060 if Chars (Prim) = Chars (Subp_Id)
12061 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
12062 and then not Fully_Conformant (Prim, Subp_Id)
12063 then
12064 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
12065 Set_Is_Immediately_Visible (Prim, False);
12066 Set_Is_Potentially_Use_Visible (Prim, False);
12067
12068 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
12069 Set_Is_Immediately_Visible (Subp_Id, False);
12070 Set_Is_Potentially_Use_Visible (Subp_Id, False);
12071 end if;
12072
12073 Next_Elmt (Prim_Elmt);
12074 end loop;
12075 end Hide_Matching_Homographs;
12076
12077 -----------------------------------------
12078 -- Is_Non_Overridden_Or_Null_Procedure --
12079 -----------------------------------------
12080
12081 function Is_Non_Overridden_Or_Null_Procedure
12082 (Subp_Id : Entity_Id) return Boolean
12083 is
12084 Alias_Id : Entity_Id;
12085
12086 begin
12087 -- The subprogram is inherited (implicitly declared), it does not
12088 -- override and does not cover a primitive of an interface.
12089
12090 if Ekind_In (Subp_Id, E_Function, E_Procedure)
12091 and then Present (Alias (Subp_Id))
12092 and then No (Interface_Alias (Subp_Id))
12093 and then No (Overridden_Operation (Subp_Id))
12094 then
12095 Alias_Id := Alias (Subp_Id);
12096
12097 if Requires_Overriding (Alias_Id) then
12098 return True;
12099
12100 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
12101 and then Null_Present (Parent (Alias_Id))
12102 then
12103 return True;
12104 end if;
12105 end if;
12106
12107 return False;
12108 end Is_Non_Overridden_Or_Null_Procedure;
12109
12110 -- Local variables
12111
12112 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
12113 Prim : Entity_Id;
12114 Prim_Elmt : Elmt_Id;
12115
12116 -- Start of processing for Hide_Non_Overridden_Subprograms
12117
12118 begin
12119 -- Inspect the list of primitives looking for non-overridden
12120 -- subprograms.
12121
12122 if Present (Prim_Ops) then
12123 Prim_Elmt := First_Elmt (Prim_Ops);
12124 while Present (Prim_Elmt) loop
12125 Prim := Node (Prim_Elmt);
12126 Next_Elmt (Prim_Elmt);
12127
12128 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
12129 Hide_Matching_Homographs
12130 (Subp_Id => Prim,
12131 Start_Elmt => Prim_Elmt);
12132 end if;
12133 end loop;
12134 end if;
12135 end Hide_Non_Overridden_Subprograms;
12136
12137 -- Local variables
12138
12139 E : constant Entity_Id := Entity (N);
12140
12141 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
12142 -- True in nongeneric case. Some of the processing here is skipped
12143 -- for the generic case since it is not needed. Basically in the
12144 -- generic case, we only need to do stuff that might generate error
12145 -- messages or warnings.
12146
12147 -- Start of processing for Freeze_Entity_Checks
12148
12149 begin
12150 -- Remember that we are processing a freezing entity. Required to
12151 -- ensure correct decoration of internal entities associated with
12152 -- interfaces (see New_Overloaded_Entity).
12153
12154 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
12155
12156 -- For tagged types covering interfaces add internal entities that link
12157 -- the primitives of the interfaces with the primitives that cover them.
12158 -- Note: These entities were originally generated only when generating
12159 -- code because their main purpose was to provide support to initialize
12160 -- the secondary dispatch tables. They are also used to locate
12161 -- primitives covering interfaces when processing generics (see
12162 -- Derive_Subprograms).
12163
12164 -- This is not needed in the generic case
12165
12166 if Ada_Version >= Ada_2005
12167 and then Nongeneric_Case
12168 and then Ekind (E) = E_Record_Type
12169 and then Is_Tagged_Type (E)
12170 and then not Is_Interface (E)
12171 and then Has_Interfaces (E)
12172 then
12173 -- This would be a good common place to call the routine that checks
12174 -- overriding of interface primitives (and thus factorize calls to
12175 -- Check_Abstract_Overriding located at different contexts in the
12176 -- compiler). However, this is not possible because it causes
12177 -- spurious errors in case of late overriding.
12178
12179 Add_Internal_Interface_Entities (E);
12180 end if;
12181
12182 -- After all forms of overriding have been resolved, a tagged type may
12183 -- be left with a set of implicitly declared and possibly erroneous
12184 -- abstract subprograms, null procedures and subprograms that require
12185 -- overriding. If this set contains fully conformant homographs, then
12186 -- one is chosen arbitrarily (already done during resolution), otherwise
12187 -- all remaining non-fully conformant homographs are hidden from
12188 -- visibility (Ada RM 8.3 12.3/2).
12189
12190 if Is_Tagged_Type (E) then
12191 Hide_Non_Overridden_Subprograms (E);
12192 end if;
12193
12194 -- Check CPP types
12195
12196 if Ekind (E) = E_Record_Type
12197 and then Is_CPP_Class (E)
12198 and then Is_Tagged_Type (E)
12199 and then Tagged_Type_Expansion
12200 then
12201 if CPP_Num_Prims (E) = 0 then
12202
12203 -- If the CPP type has user defined components then it must import
12204 -- primitives from C++. This is required because if the C++ class
12205 -- has no primitives then the C++ compiler does not added the _tag
12206 -- component to the type.
12207
12208 if First_Entity (E) /= Last_Entity (E) then
12209 Error_Msg_N
12210 ("'C'P'P type must import at least one primitive from C++??",
12211 E);
12212 end if;
12213 end if;
12214
12215 -- Check that all its primitives are abstract or imported from C++.
12216 -- Check also availability of the C++ constructor.
12217
12218 declare
12219 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
12220 Elmt : Elmt_Id;
12221 Error_Reported : Boolean := False;
12222 Prim : Node_Id;
12223
12224 begin
12225 Elmt := First_Elmt (Primitive_Operations (E));
12226 while Present (Elmt) loop
12227 Prim := Node (Elmt);
12228
12229 if Comes_From_Source (Prim) then
12230 if Is_Abstract_Subprogram (Prim) then
12231 null;
12232
12233 elsif not Is_Imported (Prim)
12234 or else Convention (Prim) /= Convention_CPP
12235 then
12236 Error_Msg_N
12237 ("primitives of 'C'P'P types must be imported from C++ "
12238 & "or abstract??", Prim);
12239
12240 elsif not Has_Constructors
12241 and then not Error_Reported
12242 then
12243 Error_Msg_Name_1 := Chars (E);
12244 Error_Msg_N
12245 ("??'C'P'P constructor required for type %", Prim);
12246 Error_Reported := True;
12247 end if;
12248 end if;
12249
12250 Next_Elmt (Elmt);
12251 end loop;
12252 end;
12253 end if;
12254
12255 -- Check Ada derivation of CPP type
12256
12257 if Expander_Active -- why? losing errors in -gnatc mode???
12258 and then Present (Etype (E)) -- defend against errors
12259 and then Tagged_Type_Expansion
12260 and then Ekind (E) = E_Record_Type
12261 and then Etype (E) /= E
12262 and then Is_CPP_Class (Etype (E))
12263 and then CPP_Num_Prims (Etype (E)) > 0
12264 and then not Is_CPP_Class (E)
12265 and then not Has_CPP_Constructors (Etype (E))
12266 then
12267 -- If the parent has C++ primitives but it has no constructor then
12268 -- check that all the primitives are overridden in this derivation;
12269 -- otherwise the constructor of the parent is needed to build the
12270 -- dispatch table.
12271
12272 declare
12273 Elmt : Elmt_Id;
12274 Prim : Node_Id;
12275
12276 begin
12277 Elmt := First_Elmt (Primitive_Operations (E));
12278 while Present (Elmt) loop
12279 Prim := Node (Elmt);
12280
12281 if not Is_Abstract_Subprogram (Prim)
12282 and then No (Interface_Alias (Prim))
12283 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
12284 then
12285 Error_Msg_Name_1 := Chars (Etype (E));
12286 Error_Msg_N
12287 ("'C'P'P constructor required for parent type %", E);
12288 exit;
12289 end if;
12290
12291 Next_Elmt (Elmt);
12292 end loop;
12293 end;
12294 end if;
12295
12296 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
12297
12298 -- If we have a type with predicates, build predicate function. This is
12299 -- not needed in the generic case, nor within TSS subprograms and other
12300 -- predefined primitives. For a derived type, ensure that the parent
12301 -- type is already frozen so that its predicate function has been
12302 -- constructed already. This is necessary if the parent is declared
12303 -- in a nested package and its own freeze point has not been reached.
12304
12305 if Is_Type (E)
12306 and then Nongeneric_Case
12307 and then not Within_Internal_Subprogram
12308 and then Has_Predicates (E)
12309 then
12310 declare
12311 Atyp : constant Entity_Id := Nearest_Ancestor (E);
12312 begin
12313 if Present (Atyp)
12314 and then Has_Predicates (Atyp)
12315 and then not Is_Frozen (Atyp)
12316 then
12317 Freeze_Before (N, Atyp);
12318 end if;
12319 end;
12320
12321 Build_Predicate_Functions (E, N);
12322 end if;
12323
12324 -- If type has delayed aspects, this is where we do the preanalysis at
12325 -- the freeze point, as part of the consistent visibility check. Note
12326 -- that this must be done after calling Build_Predicate_Functions or
12327 -- Build_Invariant_Procedure since these subprograms fix occurrences of
12328 -- the subtype name in the saved expression so that they will not cause
12329 -- trouble in the preanalysis.
12330
12331 -- This is also not needed in the generic case
12332
12333 if Nongeneric_Case
12334 and then Has_Delayed_Aspects (E)
12335 and then Scope (E) = Current_Scope
12336 then
12337 declare
12338 A_Id : Aspect_Id;
12339 Ritem : Node_Id;
12340
12341 begin
12342 -- Look for aspect specification entries for this entity
12343
12344 Ritem := First_Rep_Item (E);
12345 while Present (Ritem) loop
12346 if Nkind (Ritem) = N_Aspect_Specification
12347 and then Entity (Ritem) = E
12348 and then Is_Delayed_Aspect (Ritem)
12349 then
12350 A_Id := Get_Aspect_Id (Ritem);
12351
12352 if A_Id = Aspect_Dynamic_Predicate
12353 or else A_Id = Aspect_Predicate
12354 or else A_Id = Aspect_Priority
12355 or else A_Id = Aspect_CPU
12356 then
12357 -- Retrieve the visibility to components and discriminants
12358 -- in order to properly analyze the aspects.
12359
12360 Push_Type (E);
12361 Check_Aspect_At_Freeze_Point (Ritem);
12362 Pop_Type (E);
12363
12364 else
12365 Check_Aspect_At_Freeze_Point (Ritem);
12366 end if;
12367 end if;
12368
12369 Next_Rep_Item (Ritem);
12370 end loop;
12371 end;
12372
12373 end if;
12374
12375 -- For a record type, deal with variant parts. This has to be delayed to
12376 -- this point, because of the issue of statically predicated subtypes,
12377 -- which we have to ensure are frozen before checking choices, since we
12378 -- need to have the static choice list set.
12379
12380 if Is_Record_Type (E) then
12381 Check_Variant_Part : declare
12382 D : constant Node_Id := Declaration_Node (E);
12383 T : Node_Id;
12384 C : Node_Id;
12385 VP : Node_Id;
12386
12387 Others_Present : Boolean;
12388 pragma Warnings (Off, Others_Present);
12389 -- Indicates others present, not used in this case
12390
12391 procedure Non_Static_Choice_Error (Choice : Node_Id);
12392 -- Error routine invoked by the generic instantiation below when
12393 -- the variant part has a non static choice.
12394
12395 procedure Process_Declarations (Variant : Node_Id);
12396 -- Processes declarations associated with a variant. We analyzed
12397 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
12398 -- but we still need the recursive call to Check_Choices for any
12399 -- nested variant to get its choices properly processed. This is
12400 -- also where we expand out the choices if expansion is active.
12401
12402 package Variant_Choices_Processing is new
12403 Generic_Check_Choices
12404 (Process_Empty_Choice => No_OP,
12405 Process_Non_Static_Choice => Non_Static_Choice_Error,
12406 Process_Associated_Node => Process_Declarations);
12407 use Variant_Choices_Processing;
12408
12409 -----------------------------
12410 -- Non_Static_Choice_Error --
12411 -----------------------------
12412
12413 procedure Non_Static_Choice_Error (Choice : Node_Id) is
12414 begin
12415 Flag_Non_Static_Expr
12416 ("choice given in variant part is not static!", Choice);
12417 end Non_Static_Choice_Error;
12418
12419 --------------------------
12420 -- Process_Declarations --
12421 --------------------------
12422
12423 procedure Process_Declarations (Variant : Node_Id) is
12424 CL : constant Node_Id := Component_List (Variant);
12425 VP : Node_Id;
12426
12427 begin
12428 -- Check for static predicate present in this variant
12429
12430 if Has_SP_Choice (Variant) then
12431
12432 -- Here we expand. You might expect to find this call in
12433 -- Expand_N_Variant_Part, but that is called when we first
12434 -- see the variant part, and we cannot do this expansion
12435 -- earlier than the freeze point, since for statically
12436 -- predicated subtypes, the predicate is not known till
12437 -- the freeze point.
12438
12439 -- Furthermore, we do this expansion even if the expander
12440 -- is not active, because other semantic processing, e.g.
12441 -- for aggregates, requires the expanded list of choices.
12442
12443 -- If the expander is not active, then we can't just clobber
12444 -- the list since it would invalidate the tree.
12445 -- So we have to rewrite the variant part with a Rewrite
12446 -- call that replaces it with a copy and clobber the copy.
12447
12448 if not Expander_Active then
12449 declare
12450 NewV : constant Node_Id := New_Copy (Variant);
12451 begin
12452 Set_Discrete_Choices
12453 (NewV, New_Copy_List (Discrete_Choices (Variant)));
12454 Rewrite (Variant, NewV);
12455 end;
12456 end if;
12457
12458 Expand_Static_Predicates_In_Choices (Variant);
12459 end if;
12460
12461 -- We don't need to worry about the declarations in the variant
12462 -- (since they were analyzed by Analyze_Choices when we first
12463 -- encountered the variant), but we do need to take care of
12464 -- expansion of any nested variants.
12465
12466 if not Null_Present (CL) then
12467 VP := Variant_Part (CL);
12468
12469 if Present (VP) then
12470 Check_Choices
12471 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
12472 end if;
12473 end if;
12474 end Process_Declarations;
12475
12476 -- Start of processing for Check_Variant_Part
12477
12478 begin
12479 -- Find component list
12480
12481 C := Empty;
12482
12483 if Nkind (D) = N_Full_Type_Declaration then
12484 T := Type_Definition (D);
12485
12486 if Nkind (T) = N_Record_Definition then
12487 C := Component_List (T);
12488
12489 elsif Nkind (T) = N_Derived_Type_Definition
12490 and then Present (Record_Extension_Part (T))
12491 then
12492 C := Component_List (Record_Extension_Part (T));
12493 end if;
12494 end if;
12495
12496 -- Case of variant part present
12497
12498 if Present (C) and then Present (Variant_Part (C)) then
12499 VP := Variant_Part (C);
12500
12501 -- Check choices
12502
12503 Check_Choices
12504 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
12505
12506 -- If the last variant does not contain the Others choice,
12507 -- replace it with an N_Others_Choice node since Gigi always
12508 -- wants an Others. Note that we do not bother to call Analyze
12509 -- on the modified variant part, since its only effect would be
12510 -- to compute the Others_Discrete_Choices node laboriously, and
12511 -- of course we already know the list of choices corresponding
12512 -- to the others choice (it's the list we're replacing).
12513
12514 -- We only want to do this if the expander is active, since
12515 -- we do not want to clobber the tree.
12516
12517 if Expander_Active then
12518 declare
12519 Last_Var : constant Node_Id :=
12520 Last_Non_Pragma (Variants (VP));
12521
12522 Others_Node : Node_Id;
12523
12524 begin
12525 if Nkind (First (Discrete_Choices (Last_Var))) /=
12526 N_Others_Choice
12527 then
12528 Others_Node := Make_Others_Choice (Sloc (Last_Var));
12529 Set_Others_Discrete_Choices
12530 (Others_Node, Discrete_Choices (Last_Var));
12531 Set_Discrete_Choices
12532 (Last_Var, New_List (Others_Node));
12533 end if;
12534 end;
12535 end if;
12536 end if;
12537 end Check_Variant_Part;
12538 end if;
12539 end Freeze_Entity_Checks;
12540
12541 -------------------------
12542 -- Get_Alignment_Value --
12543 -------------------------
12544
12545 function Get_Alignment_Value (Expr : Node_Id) return Uint is
12546 Align : constant Uint := Static_Integer (Expr);
12547
12548 begin
12549 if Align = No_Uint then
12550 return No_Uint;
12551
12552 elsif Align < 0 then
12553 Error_Msg_N ("alignment value must be positive", Expr);
12554 return No_Uint;
12555
12556 -- If Alignment is specified to be 0, we treat it the same as 1
12557
12558 elsif Align = 0 then
12559 return Uint_1;
12560
12561 else
12562 for J in Int range 0 .. 64 loop
12563 declare
12564 M : constant Uint := Uint_2 ** J;
12565
12566 begin
12567 exit when M = Align;
12568
12569 if M > Align then
12570 Error_Msg_N ("alignment value must be power of 2", Expr);
12571 return No_Uint;
12572 end if;
12573 end;
12574 end loop;
12575
12576 return Align;
12577 end if;
12578 end Get_Alignment_Value;
12579
12580 -------------------------------------
12581 -- Inherit_Aspects_At_Freeze_Point --
12582 -------------------------------------
12583
12584 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
12585 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12586 (Rep_Item : Node_Id) return Boolean;
12587 -- This routine checks if Rep_Item is either a pragma or an aspect
12588 -- specification node whose correponding pragma (if any) is present in
12589 -- the Rep Item chain of the entity it has been specified to.
12590
12591 function Rep_Item_Entity (Rep_Item : Node_Id) return Entity_Id;
12592 -- Return the entity for which Rep_Item is specified
12593
12594 --------------------------------------------------
12595 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
12596 --------------------------------------------------
12597
12598 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12599 (Rep_Item : Node_Id) return Boolean
12600 is
12601 begin
12602 return
12603 Nkind (Rep_Item) = N_Pragma
12604 or else Present_In_Rep_Item
12605 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
12606 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
12607
12608 ---------------------
12609 -- Rep_Item_Entity --
12610 ---------------------
12611
12612 function Rep_Item_Entity (Rep_Item : Node_Id) return Entity_Id is
12613 begin
12614 if Nkind (Rep_Item) = N_Aspect_Specification then
12615 return Entity (Rep_Item);
12616
12617 else
12618 pragma Assert (Nkind_In (Rep_Item,
12619 N_Attribute_Definition_Clause,
12620 N_Pragma));
12621 return Entity (Name (Rep_Item));
12622 end if;
12623 end Rep_Item_Entity;
12624
12625 -- Start of processing for Inherit_Aspects_At_Freeze_Point
12626
12627 begin
12628 -- A representation item is either subtype-specific (Size and Alignment
12629 -- clauses) or type-related (all others). Subtype-specific aspects may
12630 -- differ for different subtypes of the same type (RM 13.1.8).
12631
12632 -- A derived type inherits each type-related representation aspect of
12633 -- its parent type that was directly specified before the declaration of
12634 -- the derived type (RM 13.1.15).
12635
12636 -- A derived subtype inherits each subtype-specific representation
12637 -- aspect of its parent subtype that was directly specified before the
12638 -- declaration of the derived type (RM 13.1.15).
12639
12640 -- The general processing involves inheriting a representation aspect
12641 -- from a parent type whenever the first rep item (aspect specification,
12642 -- attribute definition clause, pragma) corresponding to the given
12643 -- representation aspect in the rep item chain of Typ, if any, isn't
12644 -- directly specified to Typ but to one of its parents.
12645
12646 -- ??? Note that, for now, just a limited number of representation
12647 -- aspects have been inherited here so far. Many of them are
12648 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
12649 -- a non- exhaustive list of aspects that likely also need to
12650 -- be moved to this routine: Alignment, Component_Alignment,
12651 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
12652 -- Preelaborable_Initialization, RM_Size and Small.
12653
12654 -- In addition, Convention must be propagated from base type to subtype,
12655 -- because the subtype may have been declared on an incomplete view.
12656
12657 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
12658 return;
12659 end if;
12660
12661 -- Ada_05/Ada_2005
12662
12663 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
12664 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
12665 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12666 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
12667 then
12668 Set_Is_Ada_2005_Only (Typ);
12669 end if;
12670
12671 -- Ada_12/Ada_2012
12672
12673 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
12674 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
12675 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12676 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
12677 then
12678 Set_Is_Ada_2012_Only (Typ);
12679 end if;
12680
12681 -- Atomic/Shared
12682
12683 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
12684 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
12685 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12686 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
12687 then
12688 Set_Is_Atomic (Typ);
12689 Set_Is_Volatile (Typ);
12690 Set_Treat_As_Volatile (Typ);
12691 end if;
12692
12693 -- Convention
12694
12695 if Is_Record_Type (Typ)
12696 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
12697 then
12698 Set_Convention (Typ, Convention (Base_Type (Typ)));
12699 end if;
12700
12701 -- Default_Component_Value
12702
12703 -- Verify that there is no rep_item declared for the type, and there
12704 -- is one coming from an ancestor.
12705
12706 if Is_Array_Type (Typ)
12707 and then Is_Base_Type (Typ)
12708 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
12709 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
12710 then
12711 Set_Default_Aspect_Component_Value (Typ,
12712 Default_Aspect_Component_Value
12713 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
12714 end if;
12715
12716 -- Default_Value
12717
12718 if Is_Scalar_Type (Typ)
12719 and then Is_Base_Type (Typ)
12720 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
12721 and then Has_Rep_Item (Typ, Name_Default_Value)
12722 then
12723 Set_Has_Default_Aspect (Typ);
12724 Set_Default_Aspect_Value (Typ,
12725 Default_Aspect_Value
12726 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
12727 end if;
12728
12729 -- Discard_Names
12730
12731 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
12732 and then Has_Rep_Item (Typ, Name_Discard_Names)
12733 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12734 (Get_Rep_Item (Typ, Name_Discard_Names))
12735 then
12736 Set_Discard_Names (Typ);
12737 end if;
12738
12739 -- Volatile
12740
12741 if not Has_Rep_Item (Typ, Name_Volatile, False)
12742 and then Has_Rep_Item (Typ, Name_Volatile)
12743 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12744 (Get_Rep_Item (Typ, Name_Volatile))
12745 then
12746 Set_Is_Volatile (Typ);
12747 Set_Treat_As_Volatile (Typ);
12748 end if;
12749
12750 -- Volatile_Full_Access
12751
12752 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
12753 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
12754 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12755 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
12756 then
12757 Set_Is_Volatile_Full_Access (Typ);
12758 Set_Is_Volatile (Typ);
12759 Set_Treat_As_Volatile (Typ);
12760 end if;
12761
12762 -- Inheritance for derived types only
12763
12764 if Is_Derived_Type (Typ) then
12765 declare
12766 Bas_Typ : constant Entity_Id := Base_Type (Typ);
12767 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
12768
12769 begin
12770 -- Atomic_Components
12771
12772 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
12773 and then Has_Rep_Item (Typ, Name_Atomic_Components)
12774 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12775 (Get_Rep_Item (Typ, Name_Atomic_Components))
12776 then
12777 Set_Has_Atomic_Components (Imp_Bas_Typ);
12778 end if;
12779
12780 -- Volatile_Components
12781
12782 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
12783 and then Has_Rep_Item (Typ, Name_Volatile_Components)
12784 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12785 (Get_Rep_Item (Typ, Name_Volatile_Components))
12786 then
12787 Set_Has_Volatile_Components (Imp_Bas_Typ);
12788 end if;
12789
12790 -- Finalize_Storage_Only
12791
12792 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
12793 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
12794 then
12795 Set_Finalize_Storage_Only (Bas_Typ);
12796 end if;
12797
12798 -- Universal_Aliasing
12799
12800 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
12801 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
12802 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
12803 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
12804 then
12805 Set_Universal_Aliasing (Imp_Bas_Typ);
12806 end if;
12807
12808 -- Bit_Order
12809
12810 if Is_Record_Type (Typ) then
12811 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
12812 and then Has_Rep_Item (Typ, Name_Bit_Order)
12813 then
12814 Set_Reverse_Bit_Order (Bas_Typ,
12815 Reverse_Bit_Order (Rep_Item_Entity
12816 (Get_Rep_Item (Typ, Name_Bit_Order))));
12817 end if;
12818 end if;
12819
12820 -- Scalar_Storage_Order
12821
12822 -- Note: the aspect is specified on a first subtype, but recorded
12823 -- in a flag of the base type!
12824
12825 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
12826 and then Typ = Bas_Typ
12827 then
12828 -- For a type extension, always inherit from parent; otherwise
12829 -- inherit if no default applies. Note: we do not check for
12830 -- an explicit rep item on the parent type when inheriting,
12831 -- because the parent SSO may itself have been set by default.
12832
12833 if not Has_Rep_Item (First_Subtype (Typ),
12834 Name_Scalar_Storage_Order, False)
12835 and then (Is_Tagged_Type (Bas_Typ)
12836 or else not (SSO_Set_Low_By_Default (Bas_Typ)
12837 or else
12838 SSO_Set_High_By_Default (Bas_Typ)))
12839 then
12840 Set_Reverse_Storage_Order (Bas_Typ,
12841 Reverse_Storage_Order
12842 (Implementation_Base_Type (Etype (Bas_Typ))));
12843
12844 -- Clear default SSO indications, since the inherited aspect
12845 -- which was set explicitly overrides the default.
12846
12847 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
12848 Set_SSO_Set_High_By_Default (Bas_Typ, False);
12849 end if;
12850 end if;
12851 end;
12852 end if;
12853 end Inherit_Aspects_At_Freeze_Point;
12854
12855 ----------------
12856 -- Initialize --
12857 ----------------
12858
12859 procedure Initialize is
12860 begin
12861 Address_Clause_Checks.Init;
12862 Unchecked_Conversions.Init;
12863
12864 -- ??? Might be needed in the future for some non GCC back-ends
12865 -- if AAMP_On_Target then
12866 -- Independence_Checks.Init;
12867 -- end if;
12868 end Initialize;
12869
12870 ---------------------------
12871 -- Install_Discriminants --
12872 ---------------------------
12873
12874 procedure Install_Discriminants (E : Entity_Id) is
12875 Disc : Entity_Id;
12876 Prev : Entity_Id;
12877 begin
12878 Disc := First_Discriminant (E);
12879 while Present (Disc) loop
12880 Prev := Current_Entity (Disc);
12881 Set_Current_Entity (Disc);
12882 Set_Is_Immediately_Visible (Disc);
12883 Set_Homonym (Disc, Prev);
12884 Next_Discriminant (Disc);
12885 end loop;
12886 end Install_Discriminants;
12887
12888 -------------------------
12889 -- Is_Operational_Item --
12890 -------------------------
12891
12892 function Is_Operational_Item (N : Node_Id) return Boolean is
12893 begin
12894 if Nkind (N) /= N_Attribute_Definition_Clause then
12895 return False;
12896
12897 else
12898 declare
12899 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
12900 begin
12901
12902 -- List of operational items is given in AARM 13.1(8.mm/1).
12903 -- It is clearly incomplete, as it does not include iterator
12904 -- aspects, among others.
12905
12906 return Id = Attribute_Constant_Indexing
12907 or else Id = Attribute_Default_Iterator
12908 or else Id = Attribute_Implicit_Dereference
12909 or else Id = Attribute_Input
12910 or else Id = Attribute_Iterator_Element
12911 or else Id = Attribute_Iterable
12912 or else Id = Attribute_Output
12913 or else Id = Attribute_Read
12914 or else Id = Attribute_Variable_Indexing
12915 or else Id = Attribute_Write
12916 or else Id = Attribute_External_Tag;
12917 end;
12918 end if;
12919 end Is_Operational_Item;
12920
12921 -------------------------
12922 -- Is_Predicate_Static --
12923 -------------------------
12924
12925 -- Note: the basic legality of the expression has already been checked, so
12926 -- we don't need to worry about cases or ranges on strings for example.
12927
12928 function Is_Predicate_Static
12929 (Expr : Node_Id;
12930 Nam : Name_Id) return Boolean
12931 is
12932 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
12933 -- Given a list of case expression alternatives, returns True if all
12934 -- the alternatives are static (have all static choices, and a static
12935 -- expression).
12936
12937 function Is_Type_Ref (N : Node_Id) return Boolean;
12938 pragma Inline (Is_Type_Ref);
12939 -- Returns True if N is a reference to the type for the predicate in the
12940 -- expression (i.e. if it is an identifier whose Chars field matches the
12941 -- Nam given in the call). N must not be parenthesized, if the type name
12942 -- appears in parens, this routine will return False.
12943 --
12944 -- The routine also returns True for function calls generated during the
12945 -- expansion of comparison operators on strings, which are intended to
12946 -- be legal in static predicates, and are converted into calls to array
12947 -- comparison routines in the body of the corresponding predicate
12948 -- function.
12949
12950 ----------------------------------
12951 -- All_Static_Case_Alternatives --
12952 ----------------------------------
12953
12954 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
12955 N : Node_Id;
12956
12957 begin
12958 N := First (L);
12959 while Present (N) loop
12960 if not (All_Static_Choices (Discrete_Choices (N))
12961 and then Is_OK_Static_Expression (Expression (N)))
12962 then
12963 return False;
12964 end if;
12965
12966 Next (N);
12967 end loop;
12968
12969 return True;
12970 end All_Static_Case_Alternatives;
12971
12972 -----------------
12973 -- Is_Type_Ref --
12974 -----------------
12975
12976 function Is_Type_Ref (N : Node_Id) return Boolean is
12977 begin
12978 return (Nkind (N) = N_Identifier
12979 and then Chars (N) = Nam
12980 and then Paren_Count (N) = 0)
12981 or else Nkind (N) = N_Function_Call;
12982 end Is_Type_Ref;
12983
12984 -- Start of processing for Is_Predicate_Static
12985
12986 begin
12987 -- Predicate_Static means one of the following holds. Numbers are the
12988 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
12989
12990 -- 16: A static expression
12991
12992 if Is_OK_Static_Expression (Expr) then
12993 return True;
12994
12995 -- 17: A membership test whose simple_expression is the current
12996 -- instance, and whose membership_choice_list meets the requirements
12997 -- for a static membership test.
12998
12999 elsif Nkind (Expr) in N_Membership_Test
13000 and then All_Membership_Choices_Static (Expr)
13001 then
13002 return True;
13003
13004 -- 18. A case_expression whose selecting_expression is the current
13005 -- instance, and whose dependent expressions are static expressions.
13006
13007 elsif Nkind (Expr) = N_Case_Expression
13008 and then Is_Type_Ref (Expression (Expr))
13009 and then All_Static_Case_Alternatives (Alternatives (Expr))
13010 then
13011 return True;
13012
13013 -- 19. A call to a predefined equality or ordering operator, where one
13014 -- operand is the current instance, and the other is a static
13015 -- expression.
13016
13017 -- Note: the RM is clearly wrong here in not excluding string types.
13018 -- Without this exclusion, we would allow expressions like X > "ABC"
13019 -- to be considered as predicate-static, which is clearly not intended,
13020 -- since the idea is for predicate-static to be a subset of normal
13021 -- static expressions (and "DEF" > "ABC" is not a static expression).
13022
13023 -- However, we do allow internally generated (not from source) equality
13024 -- and inequality operations to be valid on strings (this helps deal
13025 -- with cases where we transform A in "ABC" to A = "ABC).
13026
13027 -- In fact, it appears that the intent of the ARG is to extend static
13028 -- predicates to strings, and that the extension should probably apply
13029 -- to static expressions themselves. The code below accepts comparison
13030 -- operators that apply to static strings.
13031
13032 elsif Nkind (Expr) in N_Op_Compare
13033 and then ((Is_Type_Ref (Left_Opnd (Expr))
13034 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
13035 or else
13036 (Is_Type_Ref (Right_Opnd (Expr))
13037 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
13038 then
13039 return True;
13040
13041 -- 20. A call to a predefined boolean logical operator, where each
13042 -- operand is predicate-static.
13043
13044 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
13045 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
13046 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
13047 or else
13048 (Nkind (Expr) = N_Op_Not
13049 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
13050 then
13051 return True;
13052
13053 -- 21. A short-circuit control form where both operands are
13054 -- predicate-static.
13055
13056 elsif Nkind (Expr) in N_Short_Circuit
13057 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
13058 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
13059 then
13060 return True;
13061
13062 -- 22. A parenthesized predicate-static expression. This does not
13063 -- require any special test, since we just ignore paren levels in
13064 -- all the cases above.
13065
13066 -- One more test that is an implementation artifact caused by the fact
13067 -- that we are analyzing not the original expression, but the generated
13068 -- expression in the body of the predicate function. This can include
13069 -- references to inherited predicates, so that the expression we are
13070 -- processing looks like:
13071
13072 -- xxPredicate (typ (Inns)) and then expression
13073
13074 -- Where the call is to a Predicate function for an inherited predicate.
13075 -- We simply ignore such a call, which could be to either a dynamic or
13076 -- a static predicate. Note that if the parent predicate is dynamic then
13077 -- eventually this type will be marked as dynamic, but you are allowed
13078 -- to specify a static predicate for a subtype which is inheriting a
13079 -- dynamic predicate, so the static predicate validation here ignores
13080 -- the inherited predicate even if it is dynamic.
13081 -- In all cases, a static predicate can only apply to a scalar type.
13082
13083 elsif Nkind (Expr) = N_Function_Call
13084 and then Is_Predicate_Function (Entity (Name (Expr)))
13085 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
13086 then
13087 return True;
13088
13089 elsif Is_Entity_Name (Expr)
13090 and then Entity (Expr) = Standard_True
13091 then
13092 Error_Msg_N ("predicate is redundant (always True)?", Expr);
13093 return True;
13094
13095 -- That's an exhaustive list of tests, all other cases are not
13096 -- predicate-static, so we return False.
13097
13098 else
13099 return False;
13100 end if;
13101 end Is_Predicate_Static;
13102
13103 ----------------------
13104 -- Is_Static_Choice --
13105 ----------------------
13106
13107 function Is_Static_Choice (N : Node_Id) return Boolean is
13108 begin
13109 return Nkind (N) = N_Others_Choice
13110 or else Is_OK_Static_Expression (N)
13111 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
13112 and then Is_OK_Static_Subtype (Entity (N)))
13113 or else (Nkind (N) = N_Subtype_Indication
13114 and then Is_OK_Static_Subtype (Entity (N)))
13115 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
13116 end Is_Static_Choice;
13117
13118 ------------------------------
13119 -- Is_Type_Related_Rep_Item --
13120 ------------------------------
13121
13122 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean is
13123 begin
13124 case Nkind (N) is
13125 when N_Attribute_Definition_Clause =>
13126 declare
13127 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
13128 -- See AARM 13.1(8.f-8.x) list items that end in "clause"
13129 -- ???: include any GNAT-defined attributes here?
13130 begin
13131 return Id = Attribute_Component_Size
13132 or else Id = Attribute_Bit_Order
13133 or else Id = Attribute_Storage_Pool
13134 or else Id = Attribute_Stream_Size
13135 or else Id = Attribute_Machine_Radix;
13136 end;
13137
13138 when N_Pragma =>
13139 case Get_Pragma_Id (N) is
13140 -- See AARM 13.1(8.f-8.x) list items that start with "pragma"
13141 -- ???: include any GNAT-defined pragmas here?
13142 when Pragma_Pack
13143 | Pragma_Import
13144 | Pragma_Export
13145 | Pragma_Convention
13146 | Pragma_Atomic
13147 | Pragma_Independent
13148 | Pragma_Volatile
13149 | Pragma_Atomic_Components
13150 | Pragma_Independent_Components
13151 | Pragma_Volatile_Components
13152 | Pragma_Discard_Names
13153 =>
13154 return True;
13155 when others =>
13156 null;
13157 end case;
13158
13159 when N_Enumeration_Representation_Clause
13160 | N_Record_Representation_Clause
13161 =>
13162 return True;
13163
13164 when others =>
13165 null;
13166 end case;
13167
13168 return False;
13169 end Is_Type_Related_Rep_Item;
13170
13171 ---------------------
13172 -- Kill_Rep_Clause --
13173 ---------------------
13174
13175 procedure Kill_Rep_Clause (N : Node_Id) is
13176 begin
13177 pragma Assert (Ignore_Rep_Clauses);
13178
13179 -- Note: we use Replace rather than Rewrite, because we don't want
13180 -- tools to be able to use Original_Node to dig out the (undecorated)
13181 -- rep clause that is being replaced.
13182
13183 Replace (N, Make_Null_Statement (Sloc (N)));
13184
13185 -- The null statement must be marked as not coming from source. This is
13186 -- so that tools ignore it, and also the back end does not expect bogus
13187 -- "from source" null statements in weird places (e.g. in declarative
13188 -- regions where such null statements are not allowed).
13189
13190 Set_Comes_From_Source (N, False);
13191 end Kill_Rep_Clause;
13192
13193 ------------------
13194 -- Minimum_Size --
13195 ------------------
13196
13197 function Minimum_Size
13198 (T : Entity_Id;
13199 Biased : Boolean := False) return Nat
13200 is
13201 Lo : Uint := No_Uint;
13202 Hi : Uint := No_Uint;
13203 LoR : Ureal := No_Ureal;
13204 HiR : Ureal := No_Ureal;
13205 LoSet : Boolean := False;
13206 HiSet : Boolean := False;
13207 B : Uint;
13208 S : Nat;
13209 Ancest : Entity_Id;
13210 R_Typ : constant Entity_Id := Root_Type (T);
13211
13212 begin
13213 -- If bad type, return 0
13214
13215 if T = Any_Type then
13216 return 0;
13217
13218 -- For generic types, just return zero. There cannot be any legitimate
13219 -- need to know such a size, but this routine may be called with a
13220 -- generic type as part of normal processing.
13221
13222 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
13223 return 0;
13224
13225 -- Access types (cannot have size smaller than System.Address)
13226
13227 elsif Is_Access_Type (T) then
13228 return System_Address_Size;
13229
13230 -- Floating-point types
13231
13232 elsif Is_Floating_Point_Type (T) then
13233 return UI_To_Int (Esize (R_Typ));
13234
13235 -- Discrete types
13236
13237 elsif Is_Discrete_Type (T) then
13238
13239 -- The following loop is looking for the nearest compile time known
13240 -- bounds following the ancestor subtype chain. The idea is to find
13241 -- the most restrictive known bounds information.
13242
13243 Ancest := T;
13244 loop
13245 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
13246 return 0;
13247 end if;
13248
13249 if not LoSet then
13250 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
13251 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
13252 LoSet := True;
13253 exit when HiSet;
13254 end if;
13255 end if;
13256
13257 if not HiSet then
13258 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
13259 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
13260 HiSet := True;
13261 exit when LoSet;
13262 end if;
13263 end if;
13264
13265 Ancest := Ancestor_Subtype (Ancest);
13266
13267 if No (Ancest) then
13268 Ancest := Base_Type (T);
13269
13270 if Is_Generic_Type (Ancest) then
13271 return 0;
13272 end if;
13273 end if;
13274 end loop;
13275
13276 -- Fixed-point types. We can't simply use Expr_Value to get the
13277 -- Corresponding_Integer_Value values of the bounds, since these do not
13278 -- get set till the type is frozen, and this routine can be called
13279 -- before the type is frozen. Similarly the test for bounds being static
13280 -- needs to include the case where we have unanalyzed real literals for
13281 -- the same reason.
13282
13283 elsif Is_Fixed_Point_Type (T) then
13284
13285 -- The following loop is looking for the nearest compile time known
13286 -- bounds following the ancestor subtype chain. The idea is to find
13287 -- the most restrictive known bounds information.
13288
13289 Ancest := T;
13290 loop
13291 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
13292 return 0;
13293 end if;
13294
13295 -- Note: In the following two tests for LoSet and HiSet, it may
13296 -- seem redundant to test for N_Real_Literal here since normally
13297 -- one would assume that the test for the value being known at
13298 -- compile time includes this case. However, there is a glitch.
13299 -- If the real literal comes from folding a non-static expression,
13300 -- then we don't consider any non- static expression to be known
13301 -- at compile time if we are in configurable run time mode (needed
13302 -- in some cases to give a clearer definition of what is and what
13303 -- is not accepted). So the test is indeed needed. Without it, we
13304 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
13305
13306 if not LoSet then
13307 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
13308 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
13309 then
13310 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
13311 LoSet := True;
13312 exit when HiSet;
13313 end if;
13314 end if;
13315
13316 if not HiSet then
13317 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
13318 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
13319 then
13320 HiR := Expr_Value_R (Type_High_Bound (Ancest));
13321 HiSet := True;
13322 exit when LoSet;
13323 end if;
13324 end if;
13325
13326 Ancest := Ancestor_Subtype (Ancest);
13327
13328 if No (Ancest) then
13329 Ancest := Base_Type (T);
13330
13331 if Is_Generic_Type (Ancest) then
13332 return 0;
13333 end if;
13334 end if;
13335 end loop;
13336
13337 Lo := UR_To_Uint (LoR / Small_Value (T));
13338 Hi := UR_To_Uint (HiR / Small_Value (T));
13339
13340 -- No other types allowed
13341
13342 else
13343 raise Program_Error;
13344 end if;
13345
13346 -- Fall through with Hi and Lo set. Deal with biased case
13347
13348 if (Biased
13349 and then not Is_Fixed_Point_Type (T)
13350 and then not (Is_Enumeration_Type (T)
13351 and then Has_Non_Standard_Rep (T)))
13352 or else Has_Biased_Representation (T)
13353 then
13354 Hi := Hi - Lo;
13355 Lo := Uint_0;
13356 end if;
13357
13358 -- Null range case, size is always zero. We only do this in the discrete
13359 -- type case, since that's the odd case that came up. Probably we should
13360 -- also do this in the fixed-point case, but doing so causes peculiar
13361 -- gigi failures, and it is not worth worrying about this incredibly
13362 -- marginal case (explicit null-range fixed-point type declarations)???
13363
13364 if Lo > Hi and then Is_Discrete_Type (T) then
13365 S := 0;
13366
13367 -- Signed case. Note that we consider types like range 1 .. -1 to be
13368 -- signed for the purpose of computing the size, since the bounds have
13369 -- to be accommodated in the base type.
13370
13371 elsif Lo < 0 or else Hi < 0 then
13372 S := 1;
13373 B := Uint_1;
13374
13375 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
13376 -- Note that we accommodate the case where the bounds cross. This
13377 -- can happen either because of the way the bounds are declared
13378 -- or because of the algorithm in Freeze_Fixed_Point_Type.
13379
13380 while Lo < -B
13381 or else Hi < -B
13382 or else Lo >= B
13383 or else Hi >= B
13384 loop
13385 B := Uint_2 ** S;
13386 S := S + 1;
13387 end loop;
13388
13389 -- Unsigned case
13390
13391 else
13392 -- If both bounds are positive, make sure that both are represen-
13393 -- table in the case where the bounds are crossed. This can happen
13394 -- either because of the way the bounds are declared, or because of
13395 -- the algorithm in Freeze_Fixed_Point_Type.
13396
13397 if Lo > Hi then
13398 Hi := Lo;
13399 end if;
13400
13401 -- S = size, (can accommodate 0 .. (2**size - 1))
13402
13403 S := 0;
13404 while Hi >= Uint_2 ** S loop
13405 S := S + 1;
13406 end loop;
13407 end if;
13408
13409 return S;
13410 end Minimum_Size;
13411
13412 ------------------------------
13413 -- New_Put_Image_Subprogram --
13414 ------------------------------
13415
13416 procedure New_Put_Image_Subprogram
13417 (N : Node_Id;
13418 Ent : Entity_Id;
13419 Subp : Entity_Id)
13420 is
13421 Loc : constant Source_Ptr := Sloc (N);
13422 Sname : constant Name_Id :=
13423 Make_TSS_Name (Base_Type (Ent), TSS_Put_Image);
13424 Subp_Id : Entity_Id;
13425 Subp_Decl : Node_Id;
13426 F : Entity_Id;
13427 Etyp : Entity_Id;
13428
13429 Defer_Declaration : constant Boolean :=
13430 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
13431 -- For a tagged type, there is a declaration at the freeze point, and
13432 -- we must generate only a completion of this declaration. We do the
13433 -- same for private types, because the full view might be tagged.
13434 -- Otherwise we generate a declaration at the point of the attribute
13435 -- definition clause. If the attribute definition comes from an aspect
13436 -- specification the declaration is part of the freeze actions of the
13437 -- type.
13438
13439 function Build_Spec return Node_Id;
13440 -- Used for declaration and renaming declaration, so that this is
13441 -- treated as a renaming_as_body.
13442
13443 ----------------
13444 -- Build_Spec --
13445 ----------------
13446
13447 function Build_Spec return Node_Id is
13448 Formals : List_Id;
13449 Spec : Node_Id;
13450 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
13451
13452 begin
13453 Subp_Id := Make_Defining_Identifier (Loc, Sname);
13454
13455 -- S : Sink'Class
13456
13457 Formals := New_List (
13458 Make_Parameter_Specification (Loc,
13459 Defining_Identifier =>
13460 Make_Defining_Identifier (Loc, Name_S),
13461 In_Present => True,
13462 Out_Present => True,
13463 Parameter_Type =>
13464 New_Occurrence_Of (Etype (F), Loc)));
13465
13466 -- V : T
13467
13468 Append_To (Formals,
13469 Make_Parameter_Specification (Loc,
13470 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
13471 Parameter_Type => T_Ref));
13472
13473 Spec :=
13474 Make_Procedure_Specification (Loc,
13475 Defining_Unit_Name => Subp_Id,
13476 Parameter_Specifications => Formals);
13477
13478 return Spec;
13479 end Build_Spec;
13480
13481 -- Start of processing for New_Put_Image_Subprogram
13482
13483 begin
13484 F := First_Formal (Subp);
13485
13486 Etyp := Etype (Next_Formal (F));
13487
13488 -- Prepare subprogram declaration and insert it as an action on the
13489 -- clause node. The visibility for this entity is used to test for
13490 -- visibility of the attribute definition clause (in the sense of
13491 -- 8.3(23) as amended by AI-195).
13492
13493 if not Defer_Declaration then
13494 Subp_Decl :=
13495 Make_Subprogram_Declaration (Loc,
13496 Specification => Build_Spec);
13497
13498 -- For a tagged type, there is always a visible declaration for the
13499 -- Put_Image TSS (it is a predefined primitive operation), and the
13500 -- completion of this declaration occurs at the freeze point, which is
13501 -- not always visible at places where the attribute definition clause is
13502 -- visible. So, we create a dummy entity here for the purpose of
13503 -- tracking the visibility of the attribute definition clause itself.
13504
13505 else
13506 Subp_Id :=
13507 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
13508 Subp_Decl :=
13509 Make_Object_Declaration (Loc,
13510 Defining_Identifier => Subp_Id,
13511 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
13512 end if;
13513
13514 if not Defer_Declaration
13515 and then From_Aspect_Specification (N)
13516 and then Has_Delayed_Freeze (Ent)
13517 then
13518 Append_Freeze_Action (Ent, Subp_Decl);
13519
13520 else
13521 Insert_Action (N, Subp_Decl);
13522 Set_Entity (N, Subp_Id);
13523 end if;
13524
13525 Subp_Decl :=
13526 Make_Subprogram_Renaming_Declaration (Loc,
13527 Specification => Build_Spec,
13528 Name => New_Occurrence_Of (Subp, Loc));
13529
13530 if Defer_Declaration then
13531 Set_TSS (Base_Type (Ent), Subp_Id);
13532
13533 else
13534 if From_Aspect_Specification (N) then
13535 Append_Freeze_Action (Ent, Subp_Decl);
13536 else
13537 Insert_Action (N, Subp_Decl);
13538 end if;
13539
13540 Copy_TSS (Subp_Id, Base_Type (Ent));
13541 end if;
13542 end New_Put_Image_Subprogram;
13543
13544 ---------------------------
13545 -- New_Stream_Subprogram --
13546 ---------------------------
13547
13548 procedure New_Stream_Subprogram
13549 (N : Node_Id;
13550 Ent : Entity_Id;
13551 Subp : Entity_Id;
13552 Nam : TSS_Name_Type)
13553 is
13554 Loc : constant Source_Ptr := Sloc (N);
13555 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
13556 Subp_Id : Entity_Id;
13557 Subp_Decl : Node_Id;
13558 F : Entity_Id;
13559 Etyp : Entity_Id;
13560
13561 Defer_Declaration : constant Boolean :=
13562 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
13563 -- For a tagged type, there is a declaration for each stream attribute
13564 -- at the freeze point, and we must generate only a completion of this
13565 -- declaration. We do the same for private types, because the full view
13566 -- might be tagged. Otherwise we generate a declaration at the point of
13567 -- the attribute definition clause. If the attribute definition comes
13568 -- from an aspect specification the declaration is part of the freeze
13569 -- actions of the type.
13570
13571 function Build_Spec return Node_Id;
13572 -- Used for declaration and renaming declaration, so that this is
13573 -- treated as a renaming_as_body.
13574
13575 ----------------
13576 -- Build_Spec --
13577 ----------------
13578
13579 function Build_Spec return Node_Id is
13580 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
13581 Formals : List_Id;
13582 Spec : Node_Id;
13583 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
13584
13585 begin
13586 Subp_Id := Make_Defining_Identifier (Loc, Sname);
13587
13588 -- S : access Root_Stream_Type'Class
13589
13590 Formals := New_List (
13591 Make_Parameter_Specification (Loc,
13592 Defining_Identifier =>
13593 Make_Defining_Identifier (Loc, Name_S),
13594 Parameter_Type =>
13595 Make_Access_Definition (Loc,
13596 Subtype_Mark =>
13597 New_Occurrence_Of (
13598 Designated_Type (Etype (F)), Loc))));
13599
13600 if Nam = TSS_Stream_Input then
13601 Spec :=
13602 Make_Function_Specification (Loc,
13603 Defining_Unit_Name => Subp_Id,
13604 Parameter_Specifications => Formals,
13605 Result_Definition => T_Ref);
13606 else
13607 -- V : [out] T
13608
13609 Append_To (Formals,
13610 Make_Parameter_Specification (Loc,
13611 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
13612 Out_Present => Out_P,
13613 Parameter_Type => T_Ref));
13614
13615 Spec :=
13616 Make_Procedure_Specification (Loc,
13617 Defining_Unit_Name => Subp_Id,
13618 Parameter_Specifications => Formals);
13619 end if;
13620
13621 return Spec;
13622 end Build_Spec;
13623
13624 -- Start of processing for New_Stream_Subprogram
13625
13626 begin
13627 F := First_Formal (Subp);
13628
13629 if Ekind (Subp) = E_Procedure then
13630 Etyp := Etype (Next_Formal (F));
13631 else
13632 Etyp := Etype (Subp);
13633 end if;
13634
13635 -- Prepare subprogram declaration and insert it as an action on the
13636 -- clause node. The visibility for this entity is used to test for
13637 -- visibility of the attribute definition clause (in the sense of
13638 -- 8.3(23) as amended by AI-195).
13639
13640 if not Defer_Declaration then
13641 Subp_Decl :=
13642 Make_Subprogram_Declaration (Loc,
13643 Specification => Build_Spec);
13644
13645 -- For a tagged type, there is always a visible declaration for each
13646 -- stream TSS (it is a predefined primitive operation), and the
13647 -- completion of this declaration occurs at the freeze point, which is
13648 -- not always visible at places where the attribute definition clause is
13649 -- visible. So, we create a dummy entity here for the purpose of
13650 -- tracking the visibility of the attribute definition clause itself.
13651
13652 else
13653 Subp_Id :=
13654 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
13655 Subp_Decl :=
13656 Make_Object_Declaration (Loc,
13657 Defining_Identifier => Subp_Id,
13658 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
13659 end if;
13660
13661 if not Defer_Declaration
13662 and then From_Aspect_Specification (N)
13663 and then Has_Delayed_Freeze (Ent)
13664 then
13665 Append_Freeze_Action (Ent, Subp_Decl);
13666
13667 else
13668 Insert_Action (N, Subp_Decl);
13669 Set_Entity (N, Subp_Id);
13670 end if;
13671
13672 Subp_Decl :=
13673 Make_Subprogram_Renaming_Declaration (Loc,
13674 Specification => Build_Spec,
13675 Name => New_Occurrence_Of (Subp, Loc));
13676
13677 if Defer_Declaration then
13678 Set_TSS (Base_Type (Ent), Subp_Id);
13679
13680 else
13681 if From_Aspect_Specification (N) then
13682 Append_Freeze_Action (Ent, Subp_Decl);
13683 else
13684 Insert_Action (N, Subp_Decl);
13685 end if;
13686
13687 Copy_TSS (Subp_Id, Base_Type (Ent));
13688 end if;
13689 end New_Stream_Subprogram;
13690
13691 ----------------------
13692 -- No_Type_Rep_Item --
13693 ----------------------
13694
13695 procedure No_Type_Rep_Item (N : Node_Id) is
13696 begin
13697 Error_Msg_N ("|type-related representation item not permitted!", N);
13698 end No_Type_Rep_Item;
13699
13700 --------------
13701 -- Pop_Type --
13702 --------------
13703
13704 procedure Pop_Type (E : Entity_Id) is
13705 begin
13706 if Ekind (E) = E_Record_Type and then E = Current_Scope then
13707 End_Scope;
13708
13709 elsif Is_Type (E)
13710 and then Has_Discriminants (E)
13711 and then Nkind (Parent (E)) /= N_Subtype_Declaration
13712 then
13713 Uninstall_Discriminants (E);
13714 Pop_Scope;
13715 end if;
13716 end Pop_Type;
13717
13718 ---------------
13719 -- Push_Type --
13720 ---------------
13721
13722 procedure Push_Type (E : Entity_Id) is
13723 Comp : Entity_Id;
13724
13725 begin
13726 if Ekind (E) = E_Record_Type then
13727 Push_Scope (E);
13728
13729 Comp := First_Component (E);
13730 while Present (Comp) loop
13731 Install_Entity (Comp);
13732 Next_Component (Comp);
13733 end loop;
13734
13735 if Has_Discriminants (E) then
13736 Install_Discriminants (E);
13737 end if;
13738
13739 elsif Is_Type (E)
13740 and then Has_Discriminants (E)
13741 and then Nkind (Parent (E)) /= N_Subtype_Declaration
13742 then
13743 Push_Scope (E);
13744 Install_Discriminants (E);
13745 end if;
13746 end Push_Type;
13747
13748 -----------------------------------
13749 -- Register_Address_Clause_Check --
13750 -----------------------------------
13751
13752 procedure Register_Address_Clause_Check
13753 (N : Node_Id;
13754 X : Entity_Id;
13755 A : Uint;
13756 Y : Entity_Id;
13757 Off : Boolean)
13758 is
13759 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
13760 begin
13761 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
13762 end Register_Address_Clause_Check;
13763
13764 ------------------------
13765 -- Rep_Item_Too_Early --
13766 ------------------------
13767
13768 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
13769 function Has_Generic_Parent (E : Entity_Id) return Boolean;
13770 -- Return True if R or any ancestor is a generic type
13771
13772 ------------------------
13773 -- Has_Generic_Parent --
13774 ------------------------
13775
13776 function Has_Generic_Parent (E : Entity_Id) return Boolean is
13777 Ancestor_Type : Entity_Id := Etype (E);
13778
13779 begin
13780 if Is_Generic_Type (E) then
13781 return True;
13782 end if;
13783
13784 while Present (Ancestor_Type)
13785 and then not Is_Generic_Type (Ancestor_Type)
13786 and then Etype (Ancestor_Type) /= Ancestor_Type
13787 loop
13788 Ancestor_Type := Etype (Ancestor_Type);
13789 end loop;
13790
13791 return
13792 Present (Ancestor_Type) and then Is_Generic_Type (Ancestor_Type);
13793 end Has_Generic_Parent;
13794
13795 -- Start of processing for Rep_Item_Too_Early
13796
13797 begin
13798 -- Cannot apply non-operational rep items to generic types
13799
13800 if Is_Operational_Item (N) then
13801 return False;
13802
13803 elsif Is_Type (T)
13804 and then Has_Generic_Parent (T)
13805 and then (Nkind (N) /= N_Pragma
13806 or else Get_Pragma_Id (N) /= Pragma_Convention)
13807 then
13808 if Ada_Version < Ada_2020 then
13809 Error_Msg_N
13810 ("representation item not allowed for generic type", N);
13811 return True;
13812 else
13813 return False;
13814 end if;
13815 end if;
13816
13817 -- Otherwise check for incomplete type
13818
13819 if Is_Incomplete_Or_Private_Type (T)
13820 and then No (Underlying_Type (T))
13821 and then
13822 (Nkind (N) /= N_Pragma
13823 or else Get_Pragma_Id (N) /= Pragma_Import)
13824 then
13825 Error_Msg_N
13826 ("representation item must be after full type declaration", N);
13827 return True;
13828
13829 -- If the type has incomplete components, a representation clause is
13830 -- illegal but stream attributes and Convention pragmas are correct.
13831
13832 elsif Has_Private_Component (T) then
13833 if Nkind (N) = N_Pragma then
13834 return False;
13835
13836 else
13837 Error_Msg_N
13838 ("representation item must appear after type is fully defined",
13839 N);
13840 return True;
13841 end if;
13842 else
13843 return False;
13844 end if;
13845 end Rep_Item_Too_Early;
13846
13847 -----------------------
13848 -- Rep_Item_Too_Late --
13849 -----------------------
13850
13851 function Rep_Item_Too_Late
13852 (T : Entity_Id;
13853 N : Node_Id;
13854 FOnly : Boolean := False) return Boolean
13855 is
13856 procedure Too_Late;
13857 -- Output message for an aspect being specified too late
13858
13859 -- Note that neither of the above errors is considered a serious one,
13860 -- since the effect is simply that we ignore the representation clause
13861 -- in these cases.
13862 -- Is this really true? In any case if we make this change we must
13863 -- document the requirement in the spec of Rep_Item_Too_Late that
13864 -- if True is returned, then the rep item must be completely ignored???
13865
13866 --------------
13867 -- Too_Late --
13868 --------------
13869
13870 procedure Too_Late is
13871 begin
13872 -- Other compilers seem more relaxed about rep items appearing too
13873 -- late. Since analysis tools typically don't care about rep items
13874 -- anyway, no reason to be too strict about this.
13875
13876 if not Relaxed_RM_Semantics then
13877 Error_Msg_N ("|representation item appears too late!", N);
13878 end if;
13879 end Too_Late;
13880
13881 -- Local variables
13882
13883 Parent_Type : Entity_Id;
13884 S : Entity_Id;
13885
13886 -- Start of processing for Rep_Item_Too_Late
13887
13888 begin
13889 -- First make sure entity is not frozen (RM 13.1(9))
13890
13891 if Is_Frozen (T)
13892
13893 -- Exclude imported types, which may be frozen if they appear in a
13894 -- representation clause for a local type.
13895
13896 and then not From_Limited_With (T)
13897
13898 -- Exclude generated entities (not coming from source). The common
13899 -- case is when we generate a renaming which prematurely freezes the
13900 -- renamed internal entity, but we still want to be able to set copies
13901 -- of attribute values such as Size/Alignment.
13902
13903 and then Comes_From_Source (T)
13904 then
13905 -- A self-referential aspect is illegal if it forces freezing the
13906 -- entity before the corresponding pragma has been analyzed.
13907
13908 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
13909 and then From_Aspect_Specification (N)
13910 then
13911 Error_Msg_NE
13912 ("aspect specification causes premature freezing of&", N, T);
13913 Set_Has_Delayed_Freeze (T, False);
13914 return True;
13915 end if;
13916
13917 Too_Late;
13918 S := First_Subtype (T);
13919
13920 if Present (Freeze_Node (S)) then
13921 if not Relaxed_RM_Semantics then
13922 Error_Msg_NE
13923 ("??no more representation items for }", Freeze_Node (S), S);
13924 end if;
13925 end if;
13926
13927 return True;
13928
13929 -- Check for case of untagged derived type whose parent either has
13930 -- primitive operations (pre Ada 202x), or is a by-reference type (RM
13931 -- 13.1(10)). In this case we do not output a Too_Late message, since
13932 -- there is no earlier point where the rep item could be placed to make
13933 -- it legal.
13934 -- ??? Confirming representation clauses should be allowed here.
13935
13936 elsif Is_Type (T)
13937 and then not FOnly
13938 and then Is_Derived_Type (T)
13939 and then not Is_Tagged_Type (T)
13940 then
13941 Parent_Type := Etype (Base_Type (T));
13942
13943 if Relaxed_RM_Semantics then
13944 null;
13945
13946 elsif Ada_Version <= Ada_2012
13947 and then Has_Primitive_Operations (Parent_Type)
13948 then
13949 Error_Msg_N
13950 ("|representation item not permitted before Ada 202x!", N);
13951 Error_Msg_NE
13952 ("\parent type & has primitive operations!", N, Parent_Type);
13953 return True;
13954
13955 elsif Is_By_Reference_Type (Parent_Type) then
13956 No_Type_Rep_Item (N);
13957 Error_Msg_NE
13958 ("\parent type & is a by-reference type!", N, Parent_Type);
13959 return True;
13960 end if;
13961 end if;
13962
13963 -- No error, but one more warning to consider. The RM (surprisingly)
13964 -- allows this pattern in some cases:
13965
13966 -- type S is ...
13967 -- primitive operations for S
13968 -- type R is new S;
13969 -- rep clause for S
13970
13971 -- Meaning that calls on the primitive operations of S for values of
13972 -- type R may require possibly expensive implicit conversion operations.
13973 -- So even when this is not an error, it is still worth a warning.
13974
13975 if not Relaxed_RM_Semantics and then Is_Type (T) then
13976 declare
13977 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
13978
13979 begin
13980 if Present (DTL)
13981
13982 -- For now, do not generate this warning for the case of
13983 -- aspect specification using Ada 2012 syntax, since we get
13984 -- wrong messages we do not understand. The whole business
13985 -- of derived types and rep items seems a bit confused when
13986 -- aspects are used, since the aspects are not evaluated
13987 -- till freeze time. However, AI12-0109 confirms (in an AARM
13988 -- ramification) that inheritance in this case is required
13989 -- to work.
13990
13991 and then not From_Aspect_Specification (N)
13992 then
13993 if Is_By_Reference_Type (T)
13994 and then not Is_Tagged_Type (T)
13995 and then Is_Type_Related_Rep_Item (N)
13996 and then (Ada_Version >= Ada_2012
13997 or else Has_Primitive_Operations (Base_Type (T)))
13998 then
13999 -- Treat as hard error (AI12-0109, binding interpretation).
14000 -- Implementing a change of representation is not really
14001 -- an option in the case of a by-reference type, so we
14002 -- take this path for all Ada dialects if primitive
14003 -- operations are present.
14004 Error_Msg_Sloc := Sloc (DTL);
14005 Error_Msg_N
14006 ("representation item for& appears after derived type "
14007 & "declaration#", N);
14008
14009 elsif Has_Primitive_Operations (Base_Type (T)) then
14010 Error_Msg_Sloc := Sloc (DTL);
14011
14012 Error_Msg_N
14013 ("representation item for& appears after derived type "
14014 & "declaration#??", N);
14015 Error_Msg_NE
14016 ("\may result in implicit conversions for primitive "
14017 & "operations of&??", N, T);
14018 Error_Msg_NE
14019 ("\to change representations when called with arguments "
14020 & "of type&??", N, DTL);
14021 end if;
14022 end if;
14023 end;
14024 end if;
14025
14026 -- No error, link item into head of chain of rep items for the entity,
14027 -- but avoid chaining if we have an overloadable entity, and the pragma
14028 -- is one that can apply to multiple overloaded entities.
14029
14030 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
14031 declare
14032 Pname : constant Name_Id := Pragma_Name (N);
14033 begin
14034 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
14035 Name_External, Name_Interface)
14036 then
14037 return False;
14038 end if;
14039 end;
14040 end if;
14041
14042 Record_Rep_Item (T, N);
14043 return False;
14044 end Rep_Item_Too_Late;
14045
14046 -------------------------------------
14047 -- Replace_Type_References_Generic --
14048 -------------------------------------
14049
14050 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
14051 TName : constant Name_Id := Chars (T);
14052
14053 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
14054 -- Processes a single node in the traversal procedure below, checking
14055 -- if node N should be replaced, and if so, doing the replacement.
14056
14057 function Visible_Component (Comp : Name_Id) return Entity_Id;
14058 -- Given an identifier in the expression, check whether there is a
14059 -- discriminant or component of the type that is directy visible, and
14060 -- rewrite it as the corresponding selected component of the formal of
14061 -- the subprogram. The entity is located by a sequential search, which
14062 -- seems acceptable given the typical size of component lists and check
14063 -- expressions. Possible optimization ???
14064
14065 ----------------------
14066 -- Replace_Type_Ref --
14067 ----------------------
14068
14069 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
14070 Loc : constant Source_Ptr := Sloc (N);
14071
14072 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
14073 -- Add the proper prefix to a reference to a component of the type
14074 -- when it is not already a selected component.
14075
14076 ----------------
14077 -- Add_Prefix --
14078 ----------------
14079
14080 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
14081 begin
14082 Rewrite (Ref,
14083 Make_Selected_Component (Loc,
14084 Prefix => New_Occurrence_Of (T, Loc),
14085 Selector_Name => New_Occurrence_Of (Comp, Loc)));
14086 Replace_Type_Reference (Prefix (Ref));
14087 end Add_Prefix;
14088
14089 -- Local variables
14090
14091 Comp : Entity_Id;
14092 Pref : Node_Id;
14093 Scop : Entity_Id;
14094
14095 -- Start of processing for Replace_Type_Ref
14096
14097 begin
14098 if Nkind (N) = N_Identifier then
14099
14100 -- If not the type name, check whether it is a reference to some
14101 -- other type, which must be frozen before the predicate function
14102 -- is analyzed, i.e. before the freeze node of the type to which
14103 -- the predicate applies.
14104
14105 if Chars (N) /= TName then
14106 if Present (Current_Entity (N))
14107 and then Is_Type (Current_Entity (N))
14108 then
14109 Freeze_Before (Freeze_Node (T), Current_Entity (N));
14110 end if;
14111
14112 -- The components of the type are directly visible and can
14113 -- be referenced without a prefix.
14114
14115 if Nkind (Parent (N)) = N_Selected_Component then
14116 null;
14117
14118 -- In expression C (I), C may be a directly visible function
14119 -- or a visible component that has an array type. Disambiguate
14120 -- by examining the component type.
14121
14122 elsif Nkind (Parent (N)) = N_Indexed_Component
14123 and then N = Prefix (Parent (N))
14124 then
14125 Comp := Visible_Component (Chars (N));
14126
14127 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
14128 Add_Prefix (N, Comp);
14129 end if;
14130
14131 else
14132 Comp := Visible_Component (Chars (N));
14133
14134 if Present (Comp) then
14135 Add_Prefix (N, Comp);
14136 end if;
14137 end if;
14138
14139 return Skip;
14140
14141 -- Otherwise do the replacement if this is not a qualified
14142 -- reference to a homograph of the type itself. Note that the
14143 -- current instance could not appear in such a context, e.g.
14144 -- the prefix of a type conversion.
14145
14146 else
14147 if Nkind (Parent (N)) /= N_Selected_Component
14148 or else N /= Selector_Name (Parent (N))
14149 then
14150 Replace_Type_Reference (N);
14151 end if;
14152
14153 return Skip;
14154 end if;
14155
14156 -- Case of selected component, which may be a subcomponent of the
14157 -- current instance, or an expanded name which is still unanalyzed.
14158
14159 elsif Nkind (N) = N_Selected_Component then
14160
14161 -- If selector name is not our type, keep going (we might still
14162 -- have an occurrence of the type in the prefix). If it is a
14163 -- subcomponent of the current entity, add prefix.
14164
14165 if Nkind (Selector_Name (N)) /= N_Identifier
14166 or else Chars (Selector_Name (N)) /= TName
14167 then
14168 if Nkind (Prefix (N)) = N_Identifier then
14169 Comp := Visible_Component (Chars (Prefix (N)));
14170
14171 if Present (Comp) then
14172 Add_Prefix (Prefix (N), Comp);
14173 end if;
14174 end if;
14175
14176 return OK;
14177
14178 -- Selector name is our type, check qualification
14179
14180 else
14181 -- Loop through scopes and prefixes, doing comparison
14182
14183 Scop := Current_Scope;
14184 Pref := Prefix (N);
14185 loop
14186 -- Continue if no more scopes or scope with no name
14187
14188 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
14189 return OK;
14190 end if;
14191
14192 -- Do replace if prefix is an identifier matching the scope
14193 -- that we are currently looking at.
14194
14195 if Nkind (Pref) = N_Identifier
14196 and then Chars (Pref) = Chars (Scop)
14197 then
14198 Replace_Type_Reference (N);
14199 return Skip;
14200 end if;
14201
14202 -- Go check scope above us if prefix is itself of the form
14203 -- of a selected component, whose selector matches the scope
14204 -- we are currently looking at.
14205
14206 if Nkind (Pref) = N_Selected_Component
14207 and then Nkind (Selector_Name (Pref)) = N_Identifier
14208 and then Chars (Selector_Name (Pref)) = Chars (Scop)
14209 then
14210 Scop := Scope (Scop);
14211 Pref := Prefix (Pref);
14212
14213 -- For anything else, we don't have a match, so keep on
14214 -- going, there are still some weird cases where we may
14215 -- still have a replacement within the prefix.
14216
14217 else
14218 return OK;
14219 end if;
14220 end loop;
14221 end if;
14222
14223 -- Continue for any other node kind
14224
14225 else
14226 return OK;
14227 end if;
14228 end Replace_Type_Ref;
14229
14230 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
14231
14232 -----------------------
14233 -- Visible_Component --
14234 -----------------------
14235
14236 function Visible_Component (Comp : Name_Id) return Entity_Id is
14237 E : Entity_Id;
14238
14239 begin
14240 -- Types with nameable components are records and discriminated
14241 -- private types.
14242
14243 if Ekind (T) = E_Record_Type
14244 or else (Is_Private_Type (T) and then Has_Discriminants (T))
14245 then
14246 E := First_Entity (T);
14247 while Present (E) loop
14248 if Comes_From_Source (E) and then Chars (E) = Comp then
14249 return E;
14250 end if;
14251
14252 Next_Entity (E);
14253 end loop;
14254 end if;
14255
14256 -- Nothing by that name, or the type has no components
14257
14258 return Empty;
14259 end Visible_Component;
14260
14261 -- Start of processing for Replace_Type_References_Generic
14262
14263 begin
14264 Replace_Type_Refs (N);
14265 end Replace_Type_References_Generic;
14266
14267 --------------------------------
14268 -- Resolve_Aspect_Expressions --
14269 --------------------------------
14270
14271 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
14272 function Resolve_Name (N : Node_Id) return Traverse_Result;
14273 -- Verify that all identifiers in the expression, with the exception
14274 -- of references to the current entity, denote visible entities. This
14275 -- is done only to detect visibility errors, as the expression will be
14276 -- properly analyzed/expanded during analysis of the predicate function
14277 -- body. We omit quantified expressions from this test, given that they
14278 -- introduce a local identifier that would require proper expansion to
14279 -- handle properly.
14280
14281 ------------------
14282 -- Resolve_Name --
14283 ------------------
14284
14285 function Resolve_Name (N : Node_Id) return Traverse_Result is
14286 Dummy : Traverse_Result;
14287
14288 begin
14289 if Nkind (N) = N_Selected_Component then
14290 if Nkind (Prefix (N)) = N_Identifier
14291 and then Chars (Prefix (N)) /= Chars (E)
14292 then
14293 Find_Selected_Component (N);
14294 end if;
14295
14296 return Skip;
14297
14298 -- Resolve identifiers that are not selectors in parameter
14299 -- associations (these are never resolved by visibility).
14300
14301 elsif Nkind (N) = N_Identifier
14302 and then Chars (N) /= Chars (E)
14303 and then (Nkind (Parent (N)) /= N_Parameter_Association
14304 or else N /= Selector_Name (Parent (N)))
14305 then
14306 Find_Direct_Name (N);
14307 Set_Entity (N, Empty);
14308
14309 -- The name is component association needs no resolution.
14310
14311 elsif Nkind (N) = N_Component_Association then
14312 Dummy := Resolve_Name (Expression (N));
14313 return Skip;
14314
14315 elsif Nkind (N) = N_Quantified_Expression then
14316 return Skip;
14317 end if;
14318
14319 return OK;
14320 end Resolve_Name;
14321
14322 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
14323
14324 -- Local variables
14325
14326 ASN : Node_Id := First_Rep_Item (E);
14327
14328 -- Start of processing for Resolve_Aspect_Expressions
14329
14330 begin
14331 if No (ASN) then
14332 return;
14333 end if;
14334
14335 while Present (ASN) loop
14336 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
14337 declare
14338 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
14339 Expr : constant Node_Id := Expression (ASN);
14340
14341 begin
14342 case A_Id is
14343
14344 -- For now we only deal with aspects that do not generate
14345 -- subprograms, or that may mention current instances of
14346 -- types. These will require special handling (???TBD).
14347
14348 when Aspect_Invariant
14349 | Aspect_Predicate
14350 | Aspect_Predicate_Failure
14351 =>
14352 null;
14353
14354 when Aspect_Dynamic_Predicate
14355 | Aspect_Static_Predicate
14356 =>
14357 -- Build predicate function specification and preanalyze
14358 -- expression after type replacement. The function
14359 -- declaration must be analyzed in the scope of the type,
14360 -- but the expression can reference components and
14361 -- discriminants of the type.
14362
14363 if No (Predicate_Function (E)) then
14364 declare
14365 FDecl : constant Node_Id :=
14366 Build_Predicate_Function_Declaration (E);
14367 pragma Unreferenced (FDecl);
14368
14369 begin
14370 Push_Type (E);
14371 Resolve_Aspect_Expression (Expr);
14372 Pop_Type (E);
14373 end;
14374 end if;
14375
14376 when Pre_Post_Aspects =>
14377 null;
14378
14379 when Aspect_Iterable =>
14380 if Nkind (Expr) = N_Aggregate then
14381 declare
14382 Assoc : Node_Id;
14383
14384 begin
14385 Assoc := First (Component_Associations (Expr));
14386 while Present (Assoc) loop
14387 Find_Direct_Name (Expression (Assoc));
14388 Next (Assoc);
14389 end loop;
14390 end;
14391 end if;
14392
14393 -- The expression for Default_Value is a static expression
14394 -- of the type, but this expression does not freeze the
14395 -- type, so it can still appear in a representation clause
14396 -- before the actual freeze point.
14397
14398 when Aspect_Default_Value =>
14399 Set_Must_Not_Freeze (Expr);
14400 Preanalyze_Spec_Expression (Expr, E);
14401
14402 when Aspect_Priority =>
14403 Push_Type (E);
14404 Preanalyze_Spec_Expression (Expr, Any_Integer);
14405 Pop_Type (E);
14406
14407 -- Ditto for Storage_Size. Any other aspects that carry
14408 -- expressions that should not freeze ??? This is only
14409 -- relevant to the misuse of deferred constants.
14410
14411 when Aspect_Storage_Size =>
14412 Set_Must_Not_Freeze (Expr);
14413 Preanalyze_Spec_Expression (Expr, Any_Integer);
14414
14415 when others =>
14416 if Present (Expr) then
14417 case Aspect_Argument (A_Id) is
14418 when Expression
14419 | Optional_Expression
14420 =>
14421 Analyze_And_Resolve (Expr);
14422
14423 when Name
14424 | Optional_Name
14425 =>
14426 if Nkind (Expr) = N_Identifier then
14427 Find_Direct_Name (Expr);
14428
14429 elsif Nkind (Expr) = N_Selected_Component then
14430 Find_Selected_Component (Expr);
14431 end if;
14432 end case;
14433 end if;
14434 end case;
14435 end;
14436 end if;
14437
14438 Next_Rep_Item (ASN);
14439 end loop;
14440 end Resolve_Aspect_Expressions;
14441
14442 -------------------------
14443 -- Same_Representation --
14444 -------------------------
14445
14446 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
14447 T1 : constant Entity_Id := Underlying_Type (Typ1);
14448 T2 : constant Entity_Id := Underlying_Type (Typ2);
14449
14450 begin
14451 -- A quick check, if base types are the same, then we definitely have
14452 -- the same representation, because the subtype specific representation
14453 -- attributes (Size and Alignment) do not affect representation from
14454 -- the point of view of this test.
14455
14456 if Base_Type (T1) = Base_Type (T2) then
14457 return True;
14458
14459 elsif Is_Private_Type (Base_Type (T2))
14460 and then Base_Type (T1) = Full_View (Base_Type (T2))
14461 then
14462 return True;
14463
14464 -- If T2 is a generic actual it is declared as a subtype, so
14465 -- check against its base type.
14466
14467 elsif Is_Generic_Actual_Type (T1)
14468 and then Same_Representation (Base_Type (T1), T2)
14469 then
14470 return True;
14471 end if;
14472
14473 -- Tagged types always have the same representation, because it is not
14474 -- possible to specify different representations for common fields.
14475
14476 if Is_Tagged_Type (T1) then
14477 return True;
14478 end if;
14479
14480 -- Representations are definitely different if conventions differ
14481
14482 if Convention (T1) /= Convention (T2) then
14483 return False;
14484 end if;
14485
14486 -- Representations are different if component alignments or scalar
14487 -- storage orders differ.
14488
14489 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
14490 and then
14491 (Is_Record_Type (T2) or else Is_Array_Type (T2))
14492 and then
14493 (Component_Alignment (T1) /= Component_Alignment (T2)
14494 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
14495 then
14496 return False;
14497 end if;
14498
14499 -- For arrays, the only real issue is component size. If we know the
14500 -- component size for both arrays, and it is the same, then that's
14501 -- good enough to know we don't have a change of representation.
14502
14503 if Is_Array_Type (T1) then
14504 if Known_Component_Size (T1)
14505 and then Known_Component_Size (T2)
14506 and then Component_Size (T1) = Component_Size (T2)
14507 then
14508 return True;
14509 end if;
14510 end if;
14511
14512 -- For records, representations are different if reorderings differ
14513
14514 if Is_Record_Type (T1)
14515 and then Is_Record_Type (T2)
14516 and then No_Reordering (T1) /= No_Reordering (T2)
14517 then
14518 return False;
14519 end if;
14520
14521 -- Types definitely have same representation if neither has non-standard
14522 -- representation since default representations are always consistent.
14523 -- If only one has non-standard representation, and the other does not,
14524 -- then we consider that they do not have the same representation. They
14525 -- might, but there is no way of telling early enough.
14526
14527 if Has_Non_Standard_Rep (T1) then
14528 if not Has_Non_Standard_Rep (T2) then
14529 return False;
14530 end if;
14531 else
14532 return not Has_Non_Standard_Rep (T2);
14533 end if;
14534
14535 -- Here the two types both have non-standard representation, and we need
14536 -- to determine if they have the same non-standard representation.
14537
14538 -- For arrays, we simply need to test if the component sizes are the
14539 -- same. Pragma Pack is reflected in modified component sizes, so this
14540 -- check also deals with pragma Pack.
14541
14542 if Is_Array_Type (T1) then
14543 return Component_Size (T1) = Component_Size (T2);
14544
14545 -- Case of record types
14546
14547 elsif Is_Record_Type (T1) then
14548
14549 -- Packed status must conform
14550
14551 if Is_Packed (T1) /= Is_Packed (T2) then
14552 return False;
14553
14554 -- Otherwise we must check components. Typ2 maybe a constrained
14555 -- subtype with fewer components, so we compare the components
14556 -- of the base types.
14557
14558 else
14559 Record_Case : declare
14560 CD1, CD2 : Entity_Id;
14561
14562 function Same_Rep return Boolean;
14563 -- CD1 and CD2 are either components or discriminants. This
14564 -- function tests whether they have the same representation.
14565
14566 --------------
14567 -- Same_Rep --
14568 --------------
14569
14570 function Same_Rep return Boolean is
14571 begin
14572 if No (Component_Clause (CD1)) then
14573 return No (Component_Clause (CD2));
14574 else
14575 -- Note: at this point, component clauses have been
14576 -- normalized to the default bit order, so that the
14577 -- comparison of Component_Bit_Offsets is meaningful.
14578
14579 return
14580 Present (Component_Clause (CD2))
14581 and then
14582 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
14583 and then
14584 Esize (CD1) = Esize (CD2);
14585 end if;
14586 end Same_Rep;
14587
14588 -- Start of processing for Record_Case
14589
14590 begin
14591 if Has_Discriminants (T1) then
14592
14593 -- The number of discriminants may be different if the
14594 -- derived type has fewer (constrained by values). The
14595 -- invisible discriminants retain the representation of
14596 -- the original, so the discrepancy does not per se
14597 -- indicate a different representation.
14598
14599 CD1 := First_Discriminant (T1);
14600 CD2 := First_Discriminant (T2);
14601 while Present (CD1) and then Present (CD2) loop
14602 if not Same_Rep then
14603 return False;
14604 else
14605 Next_Discriminant (CD1);
14606 Next_Discriminant (CD2);
14607 end if;
14608 end loop;
14609 end if;
14610
14611 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
14612 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
14613 while Present (CD1) loop
14614 if not Same_Rep then
14615 return False;
14616 else
14617 Next_Component (CD1);
14618 Next_Component (CD2);
14619 end if;
14620 end loop;
14621
14622 return True;
14623 end Record_Case;
14624 end if;
14625
14626 -- For enumeration types, we must check each literal to see if the
14627 -- representation is the same. Note that we do not permit enumeration
14628 -- representation clauses for Character and Wide_Character, so these
14629 -- cases were already dealt with.
14630
14631 elsif Is_Enumeration_Type (T1) then
14632 Enumeration_Case : declare
14633 L1, L2 : Entity_Id;
14634
14635 begin
14636 L1 := First_Literal (T1);
14637 L2 := First_Literal (T2);
14638 while Present (L1) loop
14639 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
14640 return False;
14641 else
14642 Next_Literal (L1);
14643 Next_Literal (L2);
14644 end if;
14645 end loop;
14646
14647 return True;
14648 end Enumeration_Case;
14649
14650 -- Any other types have the same representation for these purposes
14651
14652 else
14653 return True;
14654 end if;
14655 end Same_Representation;
14656
14657 --------------------------------
14658 -- Resolve_Iterable_Operation --
14659 --------------------------------
14660
14661 procedure Resolve_Iterable_Operation
14662 (N : Node_Id;
14663 Cursor : Entity_Id;
14664 Typ : Entity_Id;
14665 Nam : Name_Id)
14666 is
14667 Ent : Entity_Id;
14668 F1 : Entity_Id;
14669 F2 : Entity_Id;
14670
14671 begin
14672 if not Is_Overloaded (N) then
14673 if not Is_Entity_Name (N)
14674 or else Ekind (Entity (N)) /= E_Function
14675 or else Scope (Entity (N)) /= Scope (Typ)
14676 or else No (First_Formal (Entity (N)))
14677 or else Etype (First_Formal (Entity (N))) /= Typ
14678 then
14679 Error_Msg_N
14680 ("iterable primitive must be local function name whose first "
14681 & "formal is an iterable type", N);
14682 return;
14683 end if;
14684
14685 Ent := Entity (N);
14686 F1 := First_Formal (Ent);
14687
14688 if Nam = Name_First or else Nam = Name_Last then
14689
14690 -- First or Last (Container) => Cursor
14691
14692 if Etype (Ent) /= Cursor then
14693 Error_Msg_N ("primitive for First must yield a curosr", N);
14694 end if;
14695
14696 elsif Nam = Name_Next then
14697
14698 -- Next (Container, Cursor) => Cursor
14699
14700 F2 := Next_Formal (F1);
14701
14702 if Etype (F2) /= Cursor
14703 or else Etype (Ent) /= Cursor
14704 or else Present (Next_Formal (F2))
14705 then
14706 Error_Msg_N ("no match for Next iterable primitive", N);
14707 end if;
14708
14709 elsif Nam = Name_Previous then
14710
14711 -- Previous (Container, Cursor) => Cursor
14712
14713 F2 := Next_Formal (F1);
14714
14715 if Etype (F2) /= Cursor
14716 or else Etype (Ent) /= Cursor
14717 or else Present (Next_Formal (F2))
14718 then
14719 Error_Msg_N ("no match for Previous iterable primitive", N);
14720 end if;
14721
14722 elsif Nam = Name_Has_Element then
14723
14724 -- Has_Element (Container, Cursor) => Boolean
14725
14726 F2 := Next_Formal (F1);
14727
14728 if Etype (F2) /= Cursor
14729 or else Etype (Ent) /= Standard_Boolean
14730 or else Present (Next_Formal (F2))
14731 then
14732 Error_Msg_N ("no match for Has_Element iterable primitive", N);
14733 end if;
14734
14735 elsif Nam = Name_Element then
14736 F2 := Next_Formal (F1);
14737
14738 if No (F2)
14739 or else Etype (F2) /= Cursor
14740 or else Present (Next_Formal (F2))
14741 then
14742 Error_Msg_N ("no match for Element iterable primitive", N);
14743 end if;
14744
14745 else
14746 raise Program_Error;
14747 end if;
14748
14749 else
14750 -- Overloaded case: find subprogram with proper signature. Caller
14751 -- will report error if no match is found.
14752
14753 declare
14754 I : Interp_Index;
14755 It : Interp;
14756
14757 begin
14758 Get_First_Interp (N, I, It);
14759 while Present (It.Typ) loop
14760 if Ekind (It.Nam) = E_Function
14761 and then Scope (It.Nam) = Scope (Typ)
14762 and then Etype (First_Formal (It.Nam)) = Typ
14763 then
14764 F1 := First_Formal (It.Nam);
14765
14766 if Nam = Name_First then
14767 if Etype (It.Nam) = Cursor
14768 and then No (Next_Formal (F1))
14769 then
14770 Set_Entity (N, It.Nam);
14771 exit;
14772 end if;
14773
14774 elsif Nam = Name_Next then
14775 F2 := Next_Formal (F1);
14776
14777 if Present (F2)
14778 and then No (Next_Formal (F2))
14779 and then Etype (F2) = Cursor
14780 and then Etype (It.Nam) = Cursor
14781 then
14782 Set_Entity (N, It.Nam);
14783 exit;
14784 end if;
14785
14786 elsif Nam = Name_Has_Element then
14787 F2 := Next_Formal (F1);
14788
14789 if Present (F2)
14790 and then No (Next_Formal (F2))
14791 and then Etype (F2) = Cursor
14792 and then Etype (It.Nam) = Standard_Boolean
14793 then
14794 Set_Entity (N, It.Nam);
14795 F2 := Next_Formal (F1);
14796 exit;
14797 end if;
14798
14799 elsif Nam = Name_Element then
14800 F2 := Next_Formal (F1);
14801
14802 if Present (F2)
14803 and then No (Next_Formal (F2))
14804 and then Etype (F2) = Cursor
14805 then
14806 Set_Entity (N, It.Nam);
14807 exit;
14808 end if;
14809 end if;
14810 end if;
14811
14812 Get_Next_Interp (I, It);
14813 end loop;
14814 end;
14815 end if;
14816 end Resolve_Iterable_Operation;
14817
14818 ----------------
14819 -- Set_Biased --
14820 ----------------
14821
14822 procedure Set_Biased
14823 (E : Entity_Id;
14824 N : Node_Id;
14825 Msg : String;
14826 Biased : Boolean := True)
14827 is
14828 begin
14829 if Biased then
14830 Set_Has_Biased_Representation (E);
14831
14832 if Warn_On_Biased_Representation then
14833 Error_Msg_NE
14834 ("?B?" & Msg & " forces biased representation for&", N, E);
14835 end if;
14836 end if;
14837 end Set_Biased;
14838
14839 --------------------
14840 -- Set_Enum_Esize --
14841 --------------------
14842
14843 procedure Set_Enum_Esize (T : Entity_Id) is
14844 Lo : Uint;
14845 Hi : Uint;
14846 Sz : Nat;
14847
14848 begin
14849 Init_Alignment (T);
14850
14851 -- Find the minimum standard size (8,16,32,64) that fits
14852
14853 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
14854 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
14855
14856 if Lo < 0 then
14857 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
14858 Sz := Standard_Character_Size; -- May be > 8 on some targets
14859
14860 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
14861 Sz := 16;
14862
14863 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
14864 Sz := 32;
14865
14866 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
14867 Sz := 64;
14868 end if;
14869
14870 else
14871 if Hi < Uint_2**08 then
14872 Sz := Standard_Character_Size; -- May be > 8 on some targets
14873
14874 elsif Hi < Uint_2**16 then
14875 Sz := 16;
14876
14877 elsif Hi < Uint_2**32 then
14878 Sz := 32;
14879
14880 else pragma Assert (Hi < Uint_2**63);
14881 Sz := 64;
14882 end if;
14883 end if;
14884
14885 -- That minimum is the proper size unless we have a foreign convention
14886 -- and the size required is 32 or less, in which case we bump the size
14887 -- up to 32. This is required for C and C++ and seems reasonable for
14888 -- all other foreign conventions.
14889
14890 if Has_Foreign_Convention (T)
14891 and then Esize (T) < Standard_Integer_Size
14892
14893 -- Don't do this if Short_Enums on target
14894
14895 and then not Target_Short_Enums
14896 then
14897 Init_Esize (T, Standard_Integer_Size);
14898 else
14899 Init_Esize (T, Sz);
14900 end if;
14901 end Set_Enum_Esize;
14902
14903 -----------------------------
14904 -- Uninstall_Discriminants --
14905 -----------------------------
14906
14907 procedure Uninstall_Discriminants (E : Entity_Id) is
14908 Disc : Entity_Id;
14909 Prev : Entity_Id;
14910 Outer : Entity_Id;
14911
14912 begin
14913 -- Discriminants have been made visible for type declarations and
14914 -- protected type declarations, not for subtype declarations.
14915
14916 if Nkind (Parent (E)) /= N_Subtype_Declaration then
14917 Disc := First_Discriminant (E);
14918 while Present (Disc) loop
14919 if Disc /= Current_Entity (Disc) then
14920 Prev := Current_Entity (Disc);
14921 while Present (Prev)
14922 and then Present (Homonym (Prev))
14923 and then Homonym (Prev) /= Disc
14924 loop
14925 Prev := Homonym (Prev);
14926 end loop;
14927 else
14928 Prev := Empty;
14929 end if;
14930
14931 Set_Is_Immediately_Visible (Disc, False);
14932
14933 Outer := Homonym (Disc);
14934 while Present (Outer) and then Scope (Outer) = E loop
14935 Outer := Homonym (Outer);
14936 end loop;
14937
14938 -- Reset homonym link of other entities, but do not modify link
14939 -- between entities in current scope, so that the back end can
14940 -- have a proper count of local overloadings.
14941
14942 if No (Prev) then
14943 Set_Name_Entity_Id (Chars (Disc), Outer);
14944
14945 elsif Scope (Prev) /= Scope (Disc) then
14946 Set_Homonym (Prev, Outer);
14947 end if;
14948
14949 Next_Discriminant (Disc);
14950 end loop;
14951 end if;
14952 end Uninstall_Discriminants;
14953
14954 ------------------------------
14955 -- Validate_Address_Clauses --
14956 ------------------------------
14957
14958 procedure Validate_Address_Clauses is
14959 function Offset_Value (Expr : Node_Id) return Uint;
14960 -- Given an Address attribute reference, return the value in bits of its
14961 -- offset from the first bit of the underlying entity, or 0 if it is not
14962 -- known at compile time.
14963
14964 ------------------
14965 -- Offset_Value --
14966 ------------------
14967
14968 function Offset_Value (Expr : Node_Id) return Uint is
14969 N : Node_Id := Prefix (Expr);
14970 Off : Uint;
14971 Val : Uint := Uint_0;
14972
14973 begin
14974 -- Climb the prefix chain and compute the cumulative offset
14975
14976 loop
14977 if Is_Entity_Name (N) then
14978 return Val;
14979
14980 elsif Nkind (N) = N_Selected_Component then
14981 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
14982 if Off /= No_Uint and then Off >= Uint_0 then
14983 Val := Val + Off;
14984 N := Prefix (N);
14985 else
14986 return Uint_0;
14987 end if;
14988
14989 elsif Nkind (N) = N_Indexed_Component then
14990 Off := Indexed_Component_Bit_Offset (N);
14991 if Off /= No_Uint then
14992 Val := Val + Off;
14993 N := Prefix (N);
14994 else
14995 return Uint_0;
14996 end if;
14997
14998 else
14999 return Uint_0;
15000 end if;
15001 end loop;
15002 end Offset_Value;
15003
15004 -- Start of processing for Validate_Address_Clauses
15005
15006 begin
15007 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
15008 declare
15009 ACCR : Address_Clause_Check_Record
15010 renames Address_Clause_Checks.Table (J);
15011
15012 Expr : Node_Id;
15013
15014 X_Alignment : Uint;
15015 Y_Alignment : Uint := Uint_0;
15016
15017 X_Size : Uint;
15018 Y_Size : Uint := Uint_0;
15019
15020 X_Offs : Uint;
15021
15022 begin
15023 -- Skip processing of this entry if warning already posted
15024
15025 if not Address_Warning_Posted (ACCR.N) then
15026 Expr := Original_Node (Expression (ACCR.N));
15027
15028 -- Get alignments, sizes and offset, if any
15029
15030 X_Alignment := Alignment (ACCR.X);
15031 X_Size := Esize (ACCR.X);
15032
15033 if Present (ACCR.Y) then
15034 Y_Alignment := Alignment (ACCR.Y);
15035 Y_Size := Esize (ACCR.Y);
15036 end if;
15037
15038 if ACCR.Off
15039 and then Nkind (Expr) = N_Attribute_Reference
15040 and then Attribute_Name (Expr) = Name_Address
15041 then
15042 X_Offs := Offset_Value (Expr);
15043 else
15044 X_Offs := Uint_0;
15045 end if;
15046
15047 -- Check for known value not multiple of alignment
15048
15049 if No (ACCR.Y) then
15050 if not Alignment_Checks_Suppressed (ACCR)
15051 and then X_Alignment /= 0
15052 and then ACCR.A mod X_Alignment /= 0
15053 then
15054 Error_Msg_NE
15055 ("??specified address for& is inconsistent with "
15056 & "alignment", ACCR.N, ACCR.X);
15057 Error_Msg_N
15058 ("\??program execution may be erroneous (RM 13.3(27))",
15059 ACCR.N);
15060
15061 Error_Msg_Uint_1 := X_Alignment;
15062 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
15063 end if;
15064
15065 -- Check for large object overlaying smaller one
15066
15067 elsif Y_Size > Uint_0
15068 and then X_Size > Uint_0
15069 and then X_Offs + X_Size > Y_Size
15070 then
15071 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
15072 Error_Msg_N
15073 ("\??program execution may be erroneous", ACCR.N);
15074
15075 Error_Msg_Uint_1 := X_Size;
15076 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
15077
15078 Error_Msg_Uint_1 := Y_Size;
15079 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
15080
15081 if Y_Size >= X_Size then
15082 Error_Msg_Uint_1 := X_Offs;
15083 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
15084 end if;
15085
15086 -- Check for inadequate alignment, both of the base object
15087 -- and of the offset, if any. We only do this check if the
15088 -- run-time Alignment_Check is active. No point in warning
15089 -- if this check has been suppressed (or is suppressed by
15090 -- default in the non-strict alignment machine case).
15091
15092 -- Note: we do not check the alignment if we gave a size
15093 -- warning, since it would likely be redundant.
15094
15095 elsif not Alignment_Checks_Suppressed (ACCR)
15096 and then Y_Alignment /= Uint_0
15097 and then
15098 (Y_Alignment < X_Alignment
15099 or else
15100 (ACCR.Off
15101 and then Nkind (Expr) = N_Attribute_Reference
15102 and then Attribute_Name (Expr) = Name_Address
15103 and then Has_Compatible_Alignment
15104 (ACCR.X, Prefix (Expr), True) /=
15105 Known_Compatible))
15106 then
15107 Error_Msg_NE
15108 ("??specified address for& may be inconsistent with "
15109 & "alignment", ACCR.N, ACCR.X);
15110 Error_Msg_N
15111 ("\??program execution may be erroneous (RM 13.3(27))",
15112 ACCR.N);
15113
15114 Error_Msg_Uint_1 := X_Alignment;
15115 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
15116
15117 Error_Msg_Uint_1 := Y_Alignment;
15118 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
15119
15120 if Y_Alignment >= X_Alignment then
15121 Error_Msg_N
15122 ("\??but offset is not multiple of alignment", ACCR.N);
15123 end if;
15124 end if;
15125 end if;
15126 end;
15127 end loop;
15128 end Validate_Address_Clauses;
15129
15130 ---------------------------
15131 -- Validate_Independence --
15132 ---------------------------
15133
15134 procedure Validate_Independence is
15135 SU : constant Uint := UI_From_Int (System_Storage_Unit);
15136 N : Node_Id;
15137 E : Entity_Id;
15138 IC : Boolean;
15139 Comp : Entity_Id;
15140 Addr : Node_Id;
15141 P : Node_Id;
15142
15143 procedure Check_Array_Type (Atyp : Entity_Id);
15144 -- Checks if the array type Atyp has independent components, and
15145 -- if not, outputs an appropriate set of error messages.
15146
15147 procedure No_Independence;
15148 -- Output message that independence cannot be guaranteed
15149
15150 function OK_Component (C : Entity_Id) return Boolean;
15151 -- Checks one component to see if it is independently accessible, and
15152 -- if so yields True, otherwise yields False if independent access
15153 -- cannot be guaranteed. This is a conservative routine, it only
15154 -- returns True if it knows for sure, it returns False if it knows
15155 -- there is a problem, or it cannot be sure there is no problem.
15156
15157 procedure Reason_Bad_Component (C : Entity_Id);
15158 -- Outputs continuation message if a reason can be determined for
15159 -- the component C being bad.
15160
15161 ----------------------
15162 -- Check_Array_Type --
15163 ----------------------
15164
15165 procedure Check_Array_Type (Atyp : Entity_Id) is
15166 Ctyp : constant Entity_Id := Component_Type (Atyp);
15167
15168 begin
15169 -- OK if no alignment clause, no pack, and no component size
15170
15171 if not Has_Component_Size_Clause (Atyp)
15172 and then not Has_Alignment_Clause (Atyp)
15173 and then not Is_Packed (Atyp)
15174 then
15175 return;
15176 end if;
15177
15178 -- Case of component size is greater than or equal to 64 and the
15179 -- alignment of the array is at least as large as the alignment
15180 -- of the component. We are definitely OK in this situation.
15181
15182 if Known_Component_Size (Atyp)
15183 and then Component_Size (Atyp) >= 64
15184 and then Known_Alignment (Atyp)
15185 and then Known_Alignment (Ctyp)
15186 and then Alignment (Atyp) >= Alignment (Ctyp)
15187 then
15188 return;
15189 end if;
15190
15191 -- Check actual component size
15192
15193 if not Known_Component_Size (Atyp)
15194 or else not (Addressable (Component_Size (Atyp))
15195 and then Component_Size (Atyp) < 64)
15196 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
15197 then
15198 No_Independence;
15199
15200 -- Bad component size, check reason
15201
15202 if Has_Component_Size_Clause (Atyp) then
15203 P := Get_Attribute_Definition_Clause
15204 (Atyp, Attribute_Component_Size);
15205
15206 if Present (P) then
15207 Error_Msg_Sloc := Sloc (P);
15208 Error_Msg_N ("\because of Component_Size clause#", N);
15209 return;
15210 end if;
15211 end if;
15212
15213 if Is_Packed (Atyp) then
15214 P := Get_Rep_Pragma (Atyp, Name_Pack);
15215
15216 if Present (P) then
15217 Error_Msg_Sloc := Sloc (P);
15218 Error_Msg_N ("\because of pragma Pack#", N);
15219 return;
15220 end if;
15221 end if;
15222
15223 -- No reason found, just return
15224
15225 return;
15226 end if;
15227
15228 -- Array type is OK independence-wise
15229
15230 return;
15231 end Check_Array_Type;
15232
15233 ---------------------
15234 -- No_Independence --
15235 ---------------------
15236
15237 procedure No_Independence is
15238 begin
15239 if Pragma_Name (N) = Name_Independent then
15240 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
15241 else
15242 Error_Msg_NE
15243 ("independent components cannot be guaranteed for&", N, E);
15244 end if;
15245 end No_Independence;
15246
15247 ------------------
15248 -- OK_Component --
15249 ------------------
15250
15251 function OK_Component (C : Entity_Id) return Boolean is
15252 Rec : constant Entity_Id := Scope (C);
15253 Ctyp : constant Entity_Id := Etype (C);
15254
15255 begin
15256 -- OK if no component clause, no Pack, and no alignment clause
15257
15258 if No (Component_Clause (C))
15259 and then not Is_Packed (Rec)
15260 and then not Has_Alignment_Clause (Rec)
15261 then
15262 return True;
15263 end if;
15264
15265 -- Here we look at the actual component layout. A component is
15266 -- addressable if its size is a multiple of the Esize of the
15267 -- component type, and its starting position in the record has
15268 -- appropriate alignment, and the record itself has appropriate
15269 -- alignment to guarantee the component alignment.
15270
15271 -- Make sure sizes are static, always assume the worst for any
15272 -- cases where we cannot check static values.
15273
15274 if not (Known_Static_Esize (C)
15275 and then
15276 Known_Static_Esize (Ctyp))
15277 then
15278 return False;
15279 end if;
15280
15281 -- Size of component must be addressable or greater than 64 bits
15282 -- and a multiple of bytes.
15283
15284 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
15285 return False;
15286 end if;
15287
15288 -- Check size is proper multiple
15289
15290 if Esize (C) mod Esize (Ctyp) /= 0 then
15291 return False;
15292 end if;
15293
15294 -- Check alignment of component is OK
15295
15296 if not Known_Component_Bit_Offset (C)
15297 or else Component_Bit_Offset (C) < Uint_0
15298 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
15299 then
15300 return False;
15301 end if;
15302
15303 -- Check alignment of record type is OK
15304
15305 if not Known_Alignment (Rec)
15306 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
15307 then
15308 return False;
15309 end if;
15310
15311 -- All tests passed, component is addressable
15312
15313 return True;
15314 end OK_Component;
15315
15316 --------------------------
15317 -- Reason_Bad_Component --
15318 --------------------------
15319
15320 procedure Reason_Bad_Component (C : Entity_Id) is
15321 Rec : constant Entity_Id := Scope (C);
15322 Ctyp : constant Entity_Id := Etype (C);
15323
15324 begin
15325 -- If component clause present assume that's the problem
15326
15327 if Present (Component_Clause (C)) then
15328 Error_Msg_Sloc := Sloc (Component_Clause (C));
15329 Error_Msg_N ("\because of Component_Clause#", N);
15330 return;
15331 end if;
15332
15333 -- If pragma Pack clause present, assume that's the problem
15334
15335 if Is_Packed (Rec) then
15336 P := Get_Rep_Pragma (Rec, Name_Pack);
15337
15338 if Present (P) then
15339 Error_Msg_Sloc := Sloc (P);
15340 Error_Msg_N ("\because of pragma Pack#", N);
15341 return;
15342 end if;
15343 end if;
15344
15345 -- See if record has bad alignment clause
15346
15347 if Has_Alignment_Clause (Rec)
15348 and then Known_Alignment (Rec)
15349 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
15350 then
15351 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
15352
15353 if Present (P) then
15354 Error_Msg_Sloc := Sloc (P);
15355 Error_Msg_N ("\because of Alignment clause#", N);
15356 end if;
15357 end if;
15358
15359 -- Couldn't find a reason, so return without a message
15360
15361 return;
15362 end Reason_Bad_Component;
15363
15364 -- Start of processing for Validate_Independence
15365
15366 begin
15367 for J in Independence_Checks.First .. Independence_Checks.Last loop
15368 N := Independence_Checks.Table (J).N;
15369 E := Independence_Checks.Table (J).E;
15370 IC := Pragma_Name (N) = Name_Independent_Components;
15371
15372 -- Deal with component case
15373
15374 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
15375 if not OK_Component (E) then
15376 No_Independence;
15377 Reason_Bad_Component (E);
15378 goto Continue;
15379 end if;
15380 end if;
15381
15382 -- Deal with record with Independent_Components
15383
15384 if IC and then Is_Record_Type (E) then
15385 Comp := First_Component_Or_Discriminant (E);
15386 while Present (Comp) loop
15387 if not OK_Component (Comp) then
15388 No_Independence;
15389 Reason_Bad_Component (Comp);
15390 goto Continue;
15391 end if;
15392
15393 Next_Component_Or_Discriminant (Comp);
15394 end loop;
15395 end if;
15396
15397 -- Deal with address clause case
15398
15399 if Is_Object (E) then
15400 Addr := Address_Clause (E);
15401
15402 if Present (Addr) then
15403 No_Independence;
15404 Error_Msg_Sloc := Sloc (Addr);
15405 Error_Msg_N ("\because of Address clause#", N);
15406 goto Continue;
15407 end if;
15408 end if;
15409
15410 -- Deal with independent components for array type
15411
15412 if IC and then Is_Array_Type (E) then
15413 Check_Array_Type (E);
15414 end if;
15415
15416 -- Deal with independent components for array object
15417
15418 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
15419 Check_Array_Type (Etype (E));
15420 end if;
15421
15422 <<Continue>> null;
15423 end loop;
15424 end Validate_Independence;
15425
15426 ------------------------------
15427 -- Validate_Iterable_Aspect --
15428 ------------------------------
15429
15430 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
15431 Assoc : Node_Id;
15432 Expr : Node_Id;
15433
15434 Prim : Node_Id;
15435 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
15436
15437 First_Id : Entity_Id;
15438 Last_Id : Entity_Id;
15439 Next_Id : Entity_Id;
15440 Has_Element_Id : Entity_Id;
15441 Element_Id : Entity_Id;
15442
15443 begin
15444 -- If previous error aspect is unusable
15445
15446 if Cursor = Any_Type then
15447 return;
15448 end if;
15449
15450 First_Id := Empty;
15451 Last_Id := Empty;
15452 Next_Id := Empty;
15453 Has_Element_Id := Empty;
15454 Element_Id := Empty;
15455
15456 -- Each expression must resolve to a function with the proper signature
15457
15458 Assoc := First (Component_Associations (Expression (ASN)));
15459 while Present (Assoc) loop
15460 Expr := Expression (Assoc);
15461 Analyze (Expr);
15462
15463 Prim := First (Choices (Assoc));
15464
15465 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
15466 Error_Msg_N ("illegal name in association", Prim);
15467
15468 elsif Chars (Prim) = Name_First then
15469 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
15470 First_Id := Entity (Expr);
15471
15472 elsif Chars (Prim) = Name_Last then
15473 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
15474 Last_Id := Entity (Expr);
15475
15476 elsif Chars (Prim) = Name_Previous then
15477 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
15478 Last_Id := Entity (Expr);
15479
15480 elsif Chars (Prim) = Name_Next then
15481 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
15482 Next_Id := Entity (Expr);
15483
15484 elsif Chars (Prim) = Name_Has_Element then
15485 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
15486 Has_Element_Id := Entity (Expr);
15487
15488 elsif Chars (Prim) = Name_Element then
15489 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
15490 Element_Id := Entity (Expr);
15491
15492 else
15493 Error_Msg_N ("invalid name for iterable function", Prim);
15494 end if;
15495
15496 Next (Assoc);
15497 end loop;
15498
15499 if No (First_Id) then
15500 Error_Msg_N ("match for First primitive not found", ASN);
15501
15502 elsif No (Next_Id) then
15503 Error_Msg_N ("match for Next primitive not found", ASN);
15504
15505 elsif No (Has_Element_Id) then
15506 Error_Msg_N ("match for Has_Element primitive not found", ASN);
15507
15508 elsif No (Element_Id) or else No (Last_Id) then
15509 null; -- optional
15510 end if;
15511 end Validate_Iterable_Aspect;
15512
15513 ------------------------------
15514 -- Validate_Literal_Aspect --
15515 ------------------------------
15516
15517 procedure Validate_Literal_Aspect (Typ : Entity_Id; ASN : Node_Id) is
15518 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
15519 pragma Assert ((A_Id = Aspect_Integer_Literal) or
15520 (A_Id = Aspect_Real_Literal) or
15521 (A_Id = Aspect_String_Literal));
15522 Func_Name : constant Node_Id := Expression (ASN);
15523 Overloaded : Boolean := Is_Overloaded (Func_Name);
15524
15525 I : Interp_Index;
15526 It : Interp;
15527 Param_Type : Entity_Id;
15528 Match_Found : Boolean := False;
15529 Is_Match : Boolean;
15530 Match : Interp;
15531 begin
15532 if not Is_Type (Typ) then
15533 Error_Msg_N ("aspect can only be specified for a type", ASN);
15534 return;
15535 elsif not Is_First_Subtype (Typ) then
15536 Error_Msg_N ("aspect cannot be specified for a subtype", ASN);
15537 return;
15538 end if;
15539
15540 if A_Id = Aspect_String_Literal then
15541 if Is_String_Type (Typ) then
15542 Error_Msg_N ("aspect cannot be specified for a string type", ASN);
15543 return;
15544 end if;
15545 Param_Type := Standard_Wide_Wide_String;
15546 else
15547 if Is_Numeric_Type (Typ) then
15548 Error_Msg_N ("aspect cannot be specified for a numeric type", ASN);
15549 return;
15550 end if;
15551 Param_Type := Standard_String;
15552 end if;
15553
15554 if not Overloaded and then not Present (Entity (Func_Name)) then
15555 Analyze (Func_Name);
15556 Overloaded := Is_Overloaded (Func_Name);
15557 end if;
15558
15559 if Overloaded then
15560 Get_First_Interp (Func_Name, I => I, It => It);
15561 else
15562 -- only one possible interpretation
15563 It.Nam := Entity (Func_Name);
15564 pragma Assert (Present (It.Nam));
15565 end if;
15566
15567 while It.Nam /= Empty loop
15568 Is_Match := False;
15569
15570 if Ekind (It.Nam) = E_Function
15571 and then Base_Type (Etype (It.Nam)) = Typ
15572 then
15573 declare
15574 Params : constant List_Id :=
15575 Parameter_Specifications (Parent (It.Nam));
15576 Param_Spec : Node_Id;
15577 Param_Id : Entity_Id;
15578 begin
15579 if List_Length (Params) = 1 then
15580 Param_Spec := First (Params);
15581 if not More_Ids (Param_Spec) then
15582 Param_Id := Defining_Identifier (Param_Spec);
15583 if Base_Type (Etype (Param_Id)) = Param_Type
15584 and then Ekind (Param_Id) = E_In_Parameter
15585 then
15586 Is_Match := True;
15587 end if;
15588 end if;
15589 end if;
15590 end;
15591 end if;
15592
15593 if Is_Match then
15594 if Match_Found then
15595 Error_Msg_N ("aspect specification is ambiguous", ASN);
15596 return;
15597 end if;
15598 Match_Found := True;
15599 Match := It;
15600 end if;
15601
15602 exit when not Overloaded;
15603
15604 if not Is_Match then
15605 Remove_Interp (I => I);
15606 end if;
15607
15608 Get_Next_Interp (I => I, It => It);
15609 end loop;
15610
15611 if not Match_Found then
15612 Error_Msg_N
15613 ("function name in aspect specification cannot be resolved", ASN);
15614 return;
15615 end if;
15616
15617 Set_Entity (Func_Name, Match.Nam);
15618 Set_Etype (Func_Name, Etype (Match.Nam));
15619 Set_Is_Overloaded (Func_Name, False);
15620 end Validate_Literal_Aspect;
15621
15622 -----------------------------------
15623 -- Validate_Unchecked_Conversion --
15624 -----------------------------------
15625
15626 procedure Validate_Unchecked_Conversion
15627 (N : Node_Id;
15628 Act_Unit : Entity_Id)
15629 is
15630 Source : Entity_Id;
15631 Target : Entity_Id;
15632 Vnode : Node_Id;
15633
15634 begin
15635 -- Obtain source and target types. Note that we call Ancestor_Subtype
15636 -- here because the processing for generic instantiation always makes
15637 -- subtypes, and we want the original frozen actual types.
15638
15639 -- If we are dealing with private types, then do the check on their
15640 -- fully declared counterparts if the full declarations have been
15641 -- encountered (they don't have to be visible, but they must exist).
15642
15643 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
15644
15645 if Is_Private_Type (Source)
15646 and then Present (Underlying_Type (Source))
15647 then
15648 Source := Underlying_Type (Source);
15649 end if;
15650
15651 Target := Ancestor_Subtype (Etype (Act_Unit));
15652
15653 -- If either type is generic, the instantiation happens within a generic
15654 -- unit, and there is nothing to check. The proper check will happen
15655 -- when the enclosing generic is instantiated.
15656
15657 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
15658 return;
15659 end if;
15660
15661 if Is_Private_Type (Target)
15662 and then Present (Underlying_Type (Target))
15663 then
15664 Target := Underlying_Type (Target);
15665 end if;
15666
15667 -- Source may be unconstrained array, but not target, except in relaxed
15668 -- semantics mode.
15669
15670 if Is_Array_Type (Target)
15671 and then not Is_Constrained (Target)
15672 and then not Relaxed_RM_Semantics
15673 then
15674 Error_Msg_N
15675 ("unchecked conversion to unconstrained array not allowed", N);
15676 return;
15677 end if;
15678
15679 -- Warn if conversion between two different convention pointers
15680
15681 if Is_Access_Type (Target)
15682 and then Is_Access_Type (Source)
15683 and then Convention (Target) /= Convention (Source)
15684 and then Warn_On_Unchecked_Conversion
15685 then
15686 -- Give warnings for subprogram pointers only on most targets
15687
15688 if Is_Access_Subprogram_Type (Target)
15689 or else Is_Access_Subprogram_Type (Source)
15690 then
15691 Error_Msg_N
15692 ("?z?conversion between pointers with different conventions!",
15693 N);
15694 end if;
15695 end if;
15696
15697 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
15698 -- warning when compiling GNAT-related sources.
15699
15700 if Warn_On_Unchecked_Conversion
15701 and then not In_Predefined_Unit (N)
15702 and then RTU_Loaded (Ada_Calendar)
15703 and then (Chars (Source) = Name_Time
15704 or else
15705 Chars (Target) = Name_Time)
15706 then
15707 -- If Ada.Calendar is loaded and the name of one of the operands is
15708 -- Time, there is a good chance that this is Ada.Calendar.Time.
15709
15710 declare
15711 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
15712 begin
15713 pragma Assert (Present (Calendar_Time));
15714
15715 if Source = Calendar_Time or else Target = Calendar_Time then
15716 Error_Msg_N
15717 ("?z?representation of 'Time values may change between "
15718 & "'G'N'A'T versions", N);
15719 end if;
15720 end;
15721 end if;
15722
15723 -- Make entry in unchecked conversion table for later processing by
15724 -- Validate_Unchecked_Conversions, which will check sizes and alignments
15725 -- (using values set by the back end where possible). This is only done
15726 -- if the appropriate warning is active.
15727
15728 if Warn_On_Unchecked_Conversion then
15729 Unchecked_Conversions.Append
15730 (New_Val => UC_Entry'(Eloc => Sloc (N),
15731 Source => Source,
15732 Target => Target,
15733 Act_Unit => Act_Unit));
15734
15735 -- If both sizes are known statically now, then back-end annotation
15736 -- is not required to do a proper check but if either size is not
15737 -- known statically, then we need the annotation.
15738
15739 if Known_Static_RM_Size (Source)
15740 and then
15741 Known_Static_RM_Size (Target)
15742 then
15743 null;
15744 else
15745 Back_Annotate_Rep_Info := True;
15746 end if;
15747 end if;
15748
15749 -- If unchecked conversion to access type, and access type is declared
15750 -- in the same unit as the unchecked conversion, then set the flag
15751 -- No_Strict_Aliasing (no strict aliasing is implicit here)
15752
15753 if Is_Access_Type (Target) and then
15754 In_Same_Source_Unit (Target, N)
15755 then
15756 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
15757 end if;
15758
15759 -- Generate N_Validate_Unchecked_Conversion node for back end in case
15760 -- the back end needs to perform special validation checks.
15761
15762 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
15763 -- have full expansion and the back end is called ???
15764
15765 Vnode :=
15766 Make_Validate_Unchecked_Conversion (Sloc (N));
15767 Set_Source_Type (Vnode, Source);
15768 Set_Target_Type (Vnode, Target);
15769
15770 -- If the unchecked conversion node is in a list, just insert before it.
15771 -- If not we have some strange case, not worth bothering about.
15772
15773 if Is_List_Member (N) then
15774 Insert_After (N, Vnode);
15775 end if;
15776 end Validate_Unchecked_Conversion;
15777
15778 ------------------------------------
15779 -- Validate_Unchecked_Conversions --
15780 ------------------------------------
15781
15782 procedure Validate_Unchecked_Conversions is
15783 function Is_Null_Array (T : Entity_Id) return Boolean;
15784 -- We want to warn in the case of converting to a wrong-sized array of
15785 -- bytes, including the zero-size case. This returns True in that case,
15786 -- which is necessary because a size of 0 is used to indicate both an
15787 -- unknown size and a size of 0. It's OK for this to return True in
15788 -- other zero-size cases, but we don't go out of our way; for example,
15789 -- we don't bother with multidimensional arrays.
15790
15791 function Is_Null_Array (T : Entity_Id) return Boolean is
15792 begin
15793 if Is_Array_Type (T) and then Is_Constrained (T) then
15794 declare
15795 Index : constant Node_Id := First_Index (T);
15796 R : Node_Id; -- N_Range
15797 begin
15798 case Nkind (Index) is
15799 when N_Range =>
15800 R := Index;
15801 when N_Subtype_Indication =>
15802 R := Range_Expression (Constraint (Index));
15803 when N_Identifier | N_Expanded_Name =>
15804 R := Scalar_Range (Entity (Index));
15805 when others =>
15806 raise Program_Error;
15807 end case;
15808
15809 return Is_Null_Range (Low_Bound (R), High_Bound (R));
15810 end;
15811 end if;
15812
15813 return False;
15814 end Is_Null_Array;
15815
15816 begin
15817 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
15818 declare
15819 T : UC_Entry renames Unchecked_Conversions.Table (N);
15820
15821 Act_Unit : constant Entity_Id := T.Act_Unit;
15822 Eloc : constant Source_Ptr := T.Eloc;
15823 Source : constant Entity_Id := T.Source;
15824 Target : constant Entity_Id := T.Target;
15825
15826 Source_Siz : Uint;
15827 Target_Siz : Uint;
15828
15829 begin
15830 -- Skip if function marked as warnings off
15831
15832 if Warnings_Off (Act_Unit) or else Serious_Errors_Detected > 0 then
15833 goto Continue;
15834 end if;
15835
15836 -- Don't do the check if warnings off for either type, note the
15837 -- deliberate use of OR here instead of OR ELSE to get the flag
15838 -- Warnings_Off_Used set for both types if appropriate.
15839
15840 if Has_Warnings_Off (Source) or Has_Warnings_Off (Target) then
15841 goto Continue;
15842 end if;
15843
15844 if (Known_Static_RM_Size (Source)
15845 and then Known_Static_RM_Size (Target))
15846 or else Is_Null_Array (Target)
15847 then
15848 -- This validation check, which warns if we have unequal sizes
15849 -- for unchecked conversion, and thus implementation dependent
15850 -- semantics, is one of the few occasions on which we use the
15851 -- official RM size instead of Esize. See description in Einfo
15852 -- "Handling of Type'Size Values" for details.
15853
15854 Source_Siz := RM_Size (Source);
15855 Target_Siz := RM_Size (Target);
15856
15857 if Source_Siz /= Target_Siz then
15858 Error_Msg
15859 ("?z?types for unchecked conversion have different sizes!",
15860 Eloc, Act_Unit);
15861
15862 if All_Errors_Mode then
15863 Error_Msg_Name_1 := Chars (Source);
15864 Error_Msg_Uint_1 := Source_Siz;
15865 Error_Msg_Name_2 := Chars (Target);
15866 Error_Msg_Uint_2 := Target_Siz;
15867 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
15868
15869 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
15870
15871 if Is_Discrete_Type (Source)
15872 and then
15873 Is_Discrete_Type (Target)
15874 then
15875 if Source_Siz > Target_Siz then
15876 Error_Msg
15877 ("\?z?^ high order bits of source will "
15878 & "be ignored!", Eloc);
15879
15880 elsif Is_Unsigned_Type (Source) then
15881 Error_Msg
15882 ("\?z?source will be extended with ^ high order "
15883 & "zero bits!", Eloc);
15884
15885 else
15886 Error_Msg
15887 ("\?z?source will be extended with ^ high order "
15888 & "sign bits!", Eloc);
15889 end if;
15890
15891 elsif Source_Siz < Target_Siz then
15892 if Is_Discrete_Type (Target) then
15893 if Bytes_Big_Endian then
15894 Error_Msg
15895 ("\?z?target value will include ^ undefined "
15896 & "low order bits!", Eloc, Act_Unit);
15897 else
15898 Error_Msg
15899 ("\?z?target value will include ^ undefined "
15900 & "high order bits!", Eloc, Act_Unit);
15901 end if;
15902
15903 else
15904 Error_Msg
15905 ("\?z?^ trailing bits of target value will be "
15906 & "undefined!", Eloc, Act_Unit);
15907 end if;
15908
15909 else pragma Assert (Source_Siz > Target_Siz);
15910 if Is_Discrete_Type (Source) then
15911 if Bytes_Big_Endian then
15912 Error_Msg
15913 ("\?z?^ low order bits of source will be "
15914 & "ignored!", Eloc, Act_Unit);
15915 else
15916 Error_Msg
15917 ("\?z?^ high order bits of source will be "
15918 & "ignored!", Eloc, Act_Unit);
15919 end if;
15920
15921 else
15922 Error_Msg
15923 ("\?z?^ trailing bits of source will be "
15924 & "ignored!", Eloc, Act_Unit);
15925 end if;
15926 end if;
15927 end if;
15928 end if;
15929 end if;
15930
15931 -- If both types are access types, we need to check the alignment.
15932 -- If the alignment of both is specified, we can do it here.
15933
15934 if Serious_Errors_Detected = 0
15935 and then Is_Access_Type (Source)
15936 and then Is_Access_Type (Target)
15937 and then Target_Strict_Alignment
15938 and then Present (Designated_Type (Source))
15939 and then Present (Designated_Type (Target))
15940 then
15941 declare
15942 D_Source : constant Entity_Id := Designated_Type (Source);
15943 D_Target : constant Entity_Id := Designated_Type (Target);
15944
15945 begin
15946 if Known_Alignment (D_Source)
15947 and then
15948 Known_Alignment (D_Target)
15949 then
15950 declare
15951 Source_Align : constant Uint := Alignment (D_Source);
15952 Target_Align : constant Uint := Alignment (D_Target);
15953
15954 begin
15955 if Source_Align < Target_Align
15956 and then not Is_Tagged_Type (D_Source)
15957
15958 -- Suppress warning if warnings suppressed on either
15959 -- type or either designated type. Note the use of
15960 -- OR here instead of OR ELSE. That is intentional,
15961 -- we would like to set flag Warnings_Off_Used in
15962 -- all types for which warnings are suppressed.
15963
15964 and then not (Has_Warnings_Off (D_Source)
15965 or
15966 Has_Warnings_Off (D_Target)
15967 or
15968 Has_Warnings_Off (Source)
15969 or
15970 Has_Warnings_Off (Target))
15971 then
15972 Error_Msg_Uint_1 := Target_Align;
15973 Error_Msg_Uint_2 := Source_Align;
15974 Error_Msg_Node_1 := D_Target;
15975 Error_Msg_Node_2 := D_Source;
15976 Error_Msg
15977 ("?z?alignment of & (^) is stricter than "
15978 & "alignment of & (^)!", Eloc, Act_Unit);
15979 Error_Msg
15980 ("\?z?resulting access value may have invalid "
15981 & "alignment!", Eloc, Act_Unit);
15982 end if;
15983 end;
15984 end if;
15985 end;
15986 end if;
15987 end;
15988
15989 <<Continue>>
15990 null;
15991 end loop;
15992 end Validate_Unchecked_Conversions;
15993
15994 end Sem_Ch13;
This page took 0.760286 seconds and 5 git commands to generate.