]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch13.adb
5944ba5453d254141d28b07824b36243e3efc4ee
[thirdparty/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-2019, 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_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Dim; use Sem_Dim;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Sinfo; use Sinfo;
64 with Sinput; use Sinput;
65 with Snames; use Snames;
66 with Stand; use Stand;
67 with Targparm; use Targparm;
68 with Ttypes; use Ttypes;
69 with Tbuild; use Tbuild;
70 with Urealp; use Urealp;
71 with Warnsw; use Warnsw;
72
73 with GNAT.Heap_Sort_G;
74
75 package body Sem_Ch13 is
76
77 SSU : constant Pos := System_Storage_Unit;
78 -- Convenient short hand for commonly used constant
79
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
83
84 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id);
85 -- Helper routine providing the original (pre-AI95-0133) behavior for
86 -- Adjust_Record_For_Reverse_Bit_Order.
87
88 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
89 -- This routine is called after setting one of the sizes of type entity
90 -- Typ to Size. The purpose is to deal with the situation of a derived
91 -- type whose inherited alignment is no longer appropriate for the new
92 -- size value. In this case, we reset the Alignment to unknown.
93
94 procedure Build_Discrete_Static_Predicate
95 (Typ : Entity_Id;
96 Expr : Node_Id;
97 Nam : Name_Id);
98 -- Given a predicated type Typ, where Typ is a discrete static subtype,
99 -- whose predicate expression is Expr, tests if Expr is a static predicate,
100 -- and if so, builds the predicate range list. Nam is the name of the one
101 -- argument to the predicate function. Occurrences of the type name in the
102 -- predicate expression have been replaced by identifier references to this
103 -- name, which is unique, so any identifier with Chars matching Nam must be
104 -- a reference to the type. If the predicate is non-static, this procedure
105 -- returns doing nothing. If the predicate is static, then the predicate
106 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
107 -- rewritten as a canonicalized membership operation.
108
109 function Build_Export_Import_Pragma
110 (Asp : Node_Id;
111 Id : Entity_Id) return Node_Id;
112 -- Create the corresponding pragma for aspect Export or Import denoted by
113 -- Asp. Id is the related entity subject to the aspect. Return Empty when
114 -- the expression of aspect Asp evaluates to False or is erroneous.
115
116 function Build_Predicate_Function_Declaration
117 (Typ : Entity_Id) return Node_Id;
118 -- Build the declaration for a predicate function. The declaration is built
119 -- at the end of the declarative part containing the type definition, which
120 -- may be before the freeze point of the type. The predicate expression is
121 -- preanalyzed at this point, to catch visibility errors.
122
123 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
124 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
125 -- then either there are pragma Predicate entries on the rep chain for the
126 -- type (note that Predicate aspects are converted to pragma Predicate), or
127 -- there are inherited aspects from a parent type, or ancestor subtypes.
128 -- This procedure builds body for the Predicate function that tests these
129 -- predicates. N is the freeze node for the type. The spec of the function
130 -- is inserted before the freeze node, and the body of the function is
131 -- inserted after the freeze node. If the predicate expression has a least
132 -- one Raise_Expression, then this procedure also builds the M version of
133 -- the predicate function for use in membership tests.
134
135 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
136 -- Called if both Storage_Pool and Storage_Size attribute definition
137 -- clauses (SP and SS) are present for entity Ent. Issue error message.
138
139 procedure Freeze_Entity_Checks (N : Node_Id);
140 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
141 -- to generate appropriate semantic checks that are delayed until this
142 -- point (they had to be delayed this long for cases of delayed aspects,
143 -- e.g. analysis of statically predicated subtypes in choices, for which
144 -- we have to be sure the subtypes in question are frozen before checking).
145
146 function Get_Alignment_Value (Expr : Node_Id) return Uint;
147 -- Given the expression for an alignment value, returns the corresponding
148 -- Uint value. If the value is inappropriate, then error messages are
149 -- posted as required, and a value of No_Uint is returned.
150
151 function Is_Operational_Item (N : Node_Id) return Boolean;
152 -- A specification for a stream attribute is allowed before the full type
153 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
154 -- that do not specify a representation characteristic are operational
155 -- attributes.
156
157 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean;
158 -- Returns True for a representation clause/pragma that specifies a
159 -- type-related representation (as opposed to operational) aspect.
160
161 function Is_Predicate_Static
162 (Expr : Node_Id;
163 Nam : Name_Id) return Boolean;
164 -- Given predicate expression Expr, tests if Expr is predicate-static in
165 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
166 -- name in the predicate expression have been replaced by references to
167 -- an identifier whose Chars field is Nam. This name is unique, so any
168 -- identifier with Chars matching Nam must be a reference to the type.
169 -- Returns True if the expression is predicate-static and False otherwise,
170 -- but is not in the business of setting flags or issuing error messages.
171 --
172 -- Only scalar types can have static predicates, so False is always
173 -- returned for non-scalar types.
174 --
175 -- Note: the RM seems to suggest that string types can also have static
176 -- predicates. But that really makes lttle sense as very few useful
177 -- predicates can be constructed for strings. Remember that:
178 --
179 -- "ABC" < "DEF"
180 --
181 -- is not a static expression. So even though the clearly faulty RM wording
182 -- allows the following:
183 --
184 -- subtype S is String with Static_Predicate => S < "DEF"
185 --
186 -- We can't allow this, otherwise we have predicate-static applying to a
187 -- larger class than static expressions, which was never intended.
188
189 procedure New_Stream_Subprogram
190 (N : Node_Id;
191 Ent : Entity_Id;
192 Subp : Entity_Id;
193 Nam : TSS_Name_Type);
194 -- Create a subprogram renaming of a given stream attribute to the
195 -- designated subprogram and then in the tagged case, provide this as a
196 -- primitive operation, or in the untagged case make an appropriate TSS
197 -- entry. This is more properly an expansion activity than just semantics,
198 -- but the presence of user-defined stream functions for limited types
199 -- is a legality check, which is why this takes place here rather than in
200 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
201 -- function to be generated.
202 --
203 -- To avoid elaboration anomalies with freeze nodes, for untagged types
204 -- we generate both a subprogram declaration and a subprogram renaming
205 -- declaration, so that the attribute specification is handled as a
206 -- renaming_as_body. For tagged types, the specification is one of the
207 -- primitive specs.
208
209 procedure Register_Address_Clause_Check
210 (N : Node_Id;
211 X : Entity_Id;
212 A : Uint;
213 Y : Entity_Id;
214 Off : Boolean);
215 -- Register a check for the address clause N. The rest of the parameters
216 -- are in keeping with the components of Address_Clause_Check_Record below.
217
218 procedure Resolve_Iterable_Operation
219 (N : Node_Id;
220 Cursor : Entity_Id;
221 Typ : Entity_Id;
222 Nam : Name_Id);
223 -- If the name of a primitive operation for an Iterable aspect is
224 -- overloaded, resolve according to required signature.
225
226 procedure Set_Biased
227 (E : Entity_Id;
228 N : Node_Id;
229 Msg : String;
230 Biased : Boolean := True);
231 -- If Biased is True, sets Has_Biased_Representation flag for E, and
232 -- outputs a warning message at node N if Warn_On_Biased_Representation is
233 -- is True. This warning inserts the string Msg to describe the construct
234 -- causing biasing.
235
236 -----------------------------------------------------------
237 -- Visibility of Discriminants in Aspect Specifications --
238 -----------------------------------------------------------
239
240 -- The discriminants of a type are visible when analyzing the aspect
241 -- specifications of a type declaration or protected type declaration,
242 -- but not when analyzing those of a subtype declaration. The following
243 -- routines enforce this distinction.
244
245 procedure Push_Type (E : Entity_Id);
246 -- Push scope E and make visible the discriminants of type entity E if E
247 -- has discriminants and is not a subtype.
248
249 procedure Pop_Type (E : Entity_Id);
250 -- Remove visibility to the discriminants of type entity E and pop the
251 -- scope stack if E has discriminants and is not a subtype.
252
253 ----------------------------------------------
254 -- Table for Validate_Unchecked_Conversions --
255 ----------------------------------------------
256
257 -- The following table collects unchecked conversions for validation.
258 -- Entries are made by Validate_Unchecked_Conversion and then the call
259 -- to Validate_Unchecked_Conversions does the actual error checking and
260 -- posting of warnings. The reason for this delayed processing is to take
261 -- advantage of back-annotations of size and alignment values performed by
262 -- the back end.
263
264 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
265 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
266 -- already have modified all Sloc values if the -gnatD option is set.
267
268 type UC_Entry is record
269 Eloc : Source_Ptr; -- node used for posting warnings
270 Source : Entity_Id; -- source type for unchecked conversion
271 Target : Entity_Id; -- target type for unchecked conversion
272 Act_Unit : Entity_Id; -- actual function instantiated
273 end record;
274
275 package Unchecked_Conversions is new Table.Table (
276 Table_Component_Type => UC_Entry,
277 Table_Index_Type => Int,
278 Table_Low_Bound => 1,
279 Table_Initial => 50,
280 Table_Increment => 200,
281 Table_Name => "Unchecked_Conversions");
282
283 ----------------------------------------
284 -- Table for Validate_Address_Clauses --
285 ----------------------------------------
286
287 -- If an address clause has the form
288
289 -- for X'Address use Expr
290
291 -- where Expr has a value known at compile time or is of the form Y'Address
292 -- or recursively is a reference to a constant initialized with either of
293 -- these forms, and the value of Expr is not a multiple of X's alignment,
294 -- or if Y has a smaller alignment than X, then that merits a warning about
295 -- possible bad alignment. The following table collects address clauses of
296 -- this kind. We put these in a table so that they can be checked after the
297 -- back end has completed annotation of the alignments of objects, since we
298 -- can catch more cases that way.
299
300 type Address_Clause_Check_Record is record
301 N : Node_Id;
302 -- The address clause
303
304 X : Entity_Id;
305 -- The entity of the object subject to the address clause
306
307 A : Uint;
308 -- The value of the address in the first case
309
310 Y : Entity_Id;
311 -- The entity of the object being overlaid in the second case
312
313 Off : Boolean;
314 -- Whether the address is offset within Y in the second case
315
316 Alignment_Checks_Suppressed : Boolean;
317 -- Whether alignment checks are suppressed by an active scope suppress
318 -- setting. We need to save the value in order to be able to reuse it
319 -- after the back end has been run.
320 end record;
321
322 package Address_Clause_Checks is new Table.Table (
323 Table_Component_Type => Address_Clause_Check_Record,
324 Table_Index_Type => Int,
325 Table_Low_Bound => 1,
326 Table_Initial => 20,
327 Table_Increment => 200,
328 Table_Name => "Address_Clause_Checks");
329
330 function Alignment_Checks_Suppressed
331 (ACCR : Address_Clause_Check_Record) return Boolean;
332 -- Return whether the alignment check generated for the address clause
333 -- is suppressed.
334
335 ---------------------------------
336 -- Alignment_Checks_Suppressed --
337 ---------------------------------
338
339 function Alignment_Checks_Suppressed
340 (ACCR : Address_Clause_Check_Record) return Boolean
341 is
342 begin
343 if Checks_May_Be_Suppressed (ACCR.X) then
344 return Is_Check_Suppressed (ACCR.X, Alignment_Check);
345 else
346 return ACCR.Alignment_Checks_Suppressed;
347 end if;
348 end Alignment_Checks_Suppressed;
349
350 -----------------------------------------
351 -- Adjust_Record_For_Reverse_Bit_Order --
352 -----------------------------------------
353
354 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
355 Max_Machine_Scalar_Size : constant Uint :=
356 UI_From_Int
357 (Standard_Long_Long_Integer_Size);
358 -- We use this as the maximum machine scalar size
359
360 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
361
362 CC : Node_Id;
363 Comp : Node_Id;
364 Num_CC : Natural;
365
366 begin
367 -- The processing done here used to depend on the Ada version, but the
368 -- behavior has been changed by AI95-0133. However this AI is a Binding
369 -- Interpretation, so we now implement it even in Ada 95 mode. But the
370 -- original behavior from unamended Ada 95 is available for the sake of
371 -- compatibility under the debugging switch -gnatd.p in Ada 95 mode.
372
373 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
374 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
375 return;
376 end if;
377
378 -- For Ada 2005, we do machine scalar processing, as fully described In
379 -- AI-133. This involves gathering all components which start at the
380 -- same byte offset and processing them together. Same approach is still
381 -- valid in later versions including Ada 2012.
382
383 -- Note that component clauses found on record types may be inherited,
384 -- in which case the layout of the component with such a clause still
385 -- has to be done at this point. Therefore, the processing done here
386 -- must exclusively rely on the Component_Clause of the component.
387
388 -- This first loop through components does two things. First it deals
389 -- with the case of components with component clauses whose length is
390 -- greater than the maximum machine scalar size (either accepting them
391 -- or rejecting as needed). Second, it counts the number of components
392 -- with component clauses whose length does not exceed this maximum for
393 -- later processing.
394
395 Num_CC := 0;
396 Comp := First_Component_Or_Discriminant (R);
397 while Present (Comp) loop
398 CC := Component_Clause (Comp);
399
400 if Present (CC) then
401 declare
402 Fbit : constant Uint := Static_Integer (First_Bit (CC));
403 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
404
405 begin
406 -- Case of component with last bit >= max machine scalar
407
408 if Lbit >= Max_Machine_Scalar_Size then
409
410 -- This is allowed only if first bit is zero, and last bit
411 -- + 1 is a multiple of storage unit size.
412
413 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
414
415 -- This is the case to give a warning if enabled
416
417 if Warn_On_Reverse_Bit_Order then
418 Error_Msg_N
419 ("info: multi-byte field specified with "
420 & "non-standard Bit_Order?V?", CC);
421
422 if Bytes_Big_Endian then
423 Error_Msg_N
424 ("\bytes are not reversed "
425 & "(component is big-endian)?V?", CC);
426 else
427 Error_Msg_N
428 ("\bytes are not reversed "
429 & "(component is little-endian)?V?", CC);
430 end if;
431 end if;
432
433 -- Give error message for RM 13.5.1(10) violation
434
435 else
436 Error_Msg_FE
437 ("machine scalar rules not followed for&",
438 First_Bit (CC), Comp);
439
440 Error_Msg_Uint_1 := Lbit + 1;
441 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
442 Error_Msg_F
443 ("\last bit + 1 (^) exceeds maximum machine scalar "
444 & "size (^)", First_Bit (CC));
445
446 if (Lbit + 1) mod SSU /= 0 then
447 Error_Msg_Uint_1 := SSU;
448 Error_Msg_F
449 ("\and is not a multiple of Storage_Unit (^) "
450 & "(RM 13.5.1(10))", First_Bit (CC));
451
452 else
453 Error_Msg_Uint_1 := Fbit;
454 Error_Msg_F
455 ("\and first bit (^) is non-zero "
456 & "(RM 13.4.1(10))", First_Bit (CC));
457 end if;
458 end if;
459
460 -- OK case of machine scalar related component clause. For now,
461 -- just count them.
462
463 else
464 Num_CC := Num_CC + 1;
465 end if;
466 end;
467 end if;
468
469 Next_Component_Or_Discriminant (Comp);
470 end loop;
471
472 -- We need to sort the component clauses on the basis of the Position
473 -- values in the clause, so we can group clauses with the same Position
474 -- together to determine the relevant machine scalar size.
475
476 Sort_CC : declare
477 Comps : array (0 .. Num_CC) of Entity_Id;
478 -- Array to collect component and discriminant entities. The data
479 -- starts at index 1, the 0'th entry is for the sort routine.
480
481 function CP_Lt (Op1, Op2 : Natural) return Boolean;
482 -- Compare routine for Sort
483
484 procedure CP_Move (From : Natural; To : Natural);
485 -- Move routine for Sort
486
487 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
488
489 MaxL : Uint;
490 -- Maximum last bit value of any component in this set
491
492 MSS : Uint;
493 -- Corresponding machine scalar size
494
495 Start : Natural;
496 Stop : Natural;
497 -- Start and stop positions in the component list of the set of
498 -- components with the same starting position (that constitute
499 -- components in a single machine scalar).
500
501 -----------
502 -- CP_Lt --
503 -----------
504
505 function CP_Lt (Op1, Op2 : Natural) return Boolean is
506 begin
507 return
508 Position (Component_Clause (Comps (Op1))) <
509 Position (Component_Clause (Comps (Op2)));
510 end CP_Lt;
511
512 -------------
513 -- CP_Move --
514 -------------
515
516 procedure CP_Move (From : Natural; To : Natural) is
517 begin
518 Comps (To) := Comps (From);
519 end CP_Move;
520
521 -- Start of processing for Sort_CC
522
523 begin
524 -- Collect the machine scalar relevant component clauses
525
526 Num_CC := 0;
527 Comp := First_Component_Or_Discriminant (R);
528 while Present (Comp) loop
529 declare
530 CC : constant Node_Id := Component_Clause (Comp);
531
532 begin
533 -- Collect only component clauses whose last bit is less than
534 -- machine scalar size. Any component clause whose last bit
535 -- exceeds this value does not take part in machine scalar
536 -- layout considerations. The test for Error_Posted makes sure
537 -- we exclude component clauses for which we already posted an
538 -- error.
539
540 if Present (CC)
541 and then not Error_Posted (Last_Bit (CC))
542 and then Static_Integer (Last_Bit (CC)) <
543 Max_Machine_Scalar_Size
544 then
545 Num_CC := Num_CC + 1;
546 Comps (Num_CC) := Comp;
547 end if;
548 end;
549
550 Next_Component_Or_Discriminant (Comp);
551 end loop;
552
553 -- Sort by ascending position number
554
555 Sorting.Sort (Num_CC);
556
557 -- We now have all the components whose size does not exceed the max
558 -- machine scalar value, sorted by starting position. In this loop we
559 -- gather groups of clauses starting at the same position, to process
560 -- them in accordance with AI-133.
561
562 Stop := 0;
563 while Stop < Num_CC loop
564 Start := Stop + 1;
565 Stop := Start;
566 MaxL :=
567 Static_Integer
568 (Last_Bit (Component_Clause (Comps (Start))));
569 while Stop < Num_CC loop
570 if Static_Integer
571 (Position (Component_Clause (Comps (Stop + 1)))) =
572 Static_Integer
573 (Position (Component_Clause (Comps (Stop))))
574 then
575 Stop := Stop + 1;
576 MaxL :=
577 UI_Max
578 (MaxL,
579 Static_Integer
580 (Last_Bit
581 (Component_Clause (Comps (Stop)))));
582 else
583 exit;
584 end if;
585 end loop;
586
587 -- Now we have a group of component clauses from Start to Stop
588 -- whose positions are identical, and MaxL is the maximum last
589 -- bit value of any of these components.
590
591 -- We need to determine the corresponding machine scalar size.
592 -- This loop assumes that machine scalar sizes are even, and that
593 -- each possible machine scalar has twice as many bits as the next
594 -- smaller one.
595
596 MSS := Max_Machine_Scalar_Size;
597 while MSS mod 2 = 0
598 and then (MSS / 2) >= SSU
599 and then (MSS / 2) > MaxL
600 loop
601 MSS := MSS / 2;
602 end loop;
603
604 -- Here is where we fix up the Component_Bit_Offset value to
605 -- account for the reverse bit order. Some examples of what needs
606 -- to be done for the case of a machine scalar size of 8 are:
607
608 -- First_Bit .. Last_Bit Component_Bit_Offset
609 -- old new old new
610
611 -- 0 .. 0 7 .. 7 0 7
612 -- 0 .. 1 6 .. 7 0 6
613 -- 0 .. 2 5 .. 7 0 5
614 -- 0 .. 7 0 .. 7 0 4
615
616 -- 1 .. 1 6 .. 6 1 6
617 -- 1 .. 4 3 .. 6 1 3
618 -- 4 .. 7 0 .. 3 4 0
619
620 -- The rule is that the first bit is obtained by subtracting the
621 -- old ending bit from machine scalar size - 1.
622
623 for C in Start .. Stop loop
624 declare
625 Comp : constant Entity_Id := Comps (C);
626 CC : constant Node_Id := Component_Clause (Comp);
627
628 FB : constant Uint := Static_Integer (First_Bit (CC));
629 LB : constant Uint := Static_Integer (Last_Bit (CC));
630 NFB : constant Uint := MSS - 1 - LB;
631 NLB : constant Uint := NFB + LB - FB;
632 Pos : constant Uint := Static_Integer (Position (CC));
633
634 begin
635 -- Do not warn for the artificial clause built for the tag
636 -- in Check_Record_Representation_Clause if it is inherited.
637
638 if Warn_On_Reverse_Bit_Order
639 and then Chars (Comp) /= Name_uTag
640 then
641 Error_Msg_Uint_1 := MSS;
642 Error_Msg_N
643 ("info: reverse bit order in machine scalar of "
644 & "length^?V?", First_Bit (CC));
645 Error_Msg_Uint_1 := NFB;
646 Error_Msg_Uint_2 := NLB;
647
648 if Bytes_Big_Endian then
649 Error_Msg_NE
650 ("\big-endian range for component & is ^ .. ^?V?",
651 First_Bit (CC), Comp);
652 else
653 Error_Msg_NE
654 ("\little-endian range for component & is ^ .. ^?V?",
655 First_Bit (CC), Comp);
656 end if;
657 end if;
658
659 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
660 Set_Esize (Comp, 1 + (NLB - NFB));
661 Set_Normalized_First_Bit (Comp, NFB mod SSU);
662 Set_Normalized_Position (Comp, Pos + NFB / SSU);
663 end;
664 end loop;
665 end loop;
666 end Sort_CC;
667 end Adjust_Record_For_Reverse_Bit_Order;
668
669 ------------------------------------------------
670 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
671 ------------------------------------------------
672
673 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
674 CC : Node_Id;
675 Comp : Node_Id;
676
677 begin
678 -- For Ada 95, we just renumber bits within a storage unit. We do the
679 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
680 -- Ada 83, and are free to add this extension.
681
682 Comp := First_Component_Or_Discriminant (R);
683 while Present (Comp) loop
684 CC := Component_Clause (Comp);
685
686 -- If component clause is present, then deal with the non-default
687 -- bit order case for Ada 95 mode.
688
689 -- We only do this processing for the base type, and in fact that
690 -- is important, since otherwise if there are record subtypes, we
691 -- could reverse the bits once for each subtype, which is wrong.
692
693 if Present (CC) and then Ekind (R) = E_Record_Type then
694 declare
695 CFB : constant Uint := Component_Bit_Offset (Comp);
696 CSZ : constant Uint := Esize (Comp);
697 CLC : constant Node_Id := Component_Clause (Comp);
698 Pos : constant Node_Id := Position (CLC);
699 FB : constant Node_Id := First_Bit (CLC);
700
701 Storage_Unit_Offset : constant Uint :=
702 CFB / System_Storage_Unit;
703
704 Start_Bit : constant Uint :=
705 CFB mod System_Storage_Unit;
706
707 begin
708 -- Cases where field goes over storage unit boundary
709
710 if Start_Bit + CSZ > System_Storage_Unit then
711
712 -- Allow multi-byte field but generate warning
713
714 if Start_Bit mod System_Storage_Unit = 0
715 and then CSZ mod System_Storage_Unit = 0
716 then
717 Error_Msg_N
718 ("info: multi-byte field specified with non-standard "
719 & "Bit_Order?V?", CLC);
720
721 if Bytes_Big_Endian then
722 Error_Msg_N
723 ("\bytes are not reversed "
724 & "(component is big-endian)?V?", CLC);
725 else
726 Error_Msg_N
727 ("\bytes are not reversed "
728 & "(component is little-endian)?V?", CLC);
729 end if;
730
731 -- Do not allow non-contiguous field
732
733 else
734 Error_Msg_N
735 ("attempt to specify non-contiguous field not "
736 & "permitted", CLC);
737 Error_Msg_N
738 ("\caused by non-standard Bit_Order specified in "
739 & "legacy Ada 95 mode", CLC);
740 end if;
741
742 -- Case where field fits in one storage unit
743
744 else
745 -- Give warning if suspicious component clause
746
747 if Intval (FB) >= System_Storage_Unit
748 and then Warn_On_Reverse_Bit_Order
749 then
750 Error_Msg_N
751 ("info: Bit_Order clause does not affect byte "
752 & "ordering?V?", Pos);
753 Error_Msg_Uint_1 :=
754 Intval (Pos) + Intval (FB) /
755 System_Storage_Unit;
756 Error_Msg_N
757 ("info: position normalized to ^ before bit order "
758 & "interpreted?V?", Pos);
759 end if;
760
761 -- Here is where we fix up the Component_Bit_Offset value
762 -- to account for the reverse bit order. Some examples of
763 -- what needs to be done are:
764
765 -- First_Bit .. Last_Bit Component_Bit_Offset
766 -- old new old new
767
768 -- 0 .. 0 7 .. 7 0 7
769 -- 0 .. 1 6 .. 7 0 6
770 -- 0 .. 2 5 .. 7 0 5
771 -- 0 .. 7 0 .. 7 0 4
772
773 -- 1 .. 1 6 .. 6 1 6
774 -- 1 .. 4 3 .. 6 1 3
775 -- 4 .. 7 0 .. 3 4 0
776
777 -- The rule is that the first bit is obtained by subtracting
778 -- the old ending bit from storage_unit - 1.
779
780 Set_Component_Bit_Offset (Comp,
781 (Storage_Unit_Offset * System_Storage_Unit) +
782 (System_Storage_Unit - 1) -
783 (Start_Bit + CSZ - 1));
784
785 Set_Normalized_Position (Comp,
786 Component_Bit_Offset (Comp) / System_Storage_Unit);
787
788 Set_Normalized_First_Bit (Comp,
789 Component_Bit_Offset (Comp) mod System_Storage_Unit);
790 end if;
791 end;
792 end if;
793
794 Next_Component_Or_Discriminant (Comp);
795 end loop;
796 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
797
798 -------------------------------------
799 -- Alignment_Check_For_Size_Change --
800 -------------------------------------
801
802 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
803 begin
804 -- If the alignment is known, and not set by a rep clause, and is
805 -- inconsistent with the size being set, then reset it to unknown,
806 -- we assume in this case that the size overrides the inherited
807 -- alignment, and that the alignment must be recomputed.
808
809 if Known_Alignment (Typ)
810 and then not Has_Alignment_Clause (Typ)
811 and then Size mod (Alignment (Typ) * SSU) /= 0
812 then
813 Init_Alignment (Typ);
814 end if;
815 end Alignment_Check_For_Size_Change;
816
817 -------------------------------------
818 -- Analyze_Aspects_At_Freeze_Point --
819 -------------------------------------
820
821 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
822 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
823 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
824 -- the aspect specification node ASN.
825
826 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
827 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
828 -- a derived type can inherit aspects from its parent which have been
829 -- specified at the time of the derivation using an aspect, as in:
830 --
831 -- type A is range 1 .. 10
832 -- with Size => Not_Defined_Yet;
833 -- ..
834 -- type B is new A;
835 -- ..
836 -- Not_Defined_Yet : constant := 64;
837 --
838 -- In this example, the Size of A is considered to be specified prior
839 -- to the derivation, and thus inherited, even though the value is not
840 -- known at the time of derivation. To deal with this, we use two entity
841 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
842 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
843 -- the derived type (B here). If this flag is set when the derived type
844 -- is frozen, then this procedure is called to ensure proper inheritance
845 -- of all delayed aspects from the parent type. The derived type is E,
846 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
847 -- aspect specification node in the Rep_Item chain for the parent type.
848
849 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
850 -- Given an aspect specification node ASN whose expression is an
851 -- optional Boolean, this routines creates the corresponding pragma
852 -- at the freezing point.
853
854 ----------------------------------
855 -- Analyze_Aspect_Default_Value --
856 ----------------------------------
857
858 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
859 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
860 Ent : constant Entity_Id := Entity (ASN);
861 Expr : constant Node_Id := Expression (ASN);
862 Id : constant Node_Id := Identifier (ASN);
863
864 begin
865 Error_Msg_Name_1 := Chars (Id);
866
867 if not Is_Type (Ent) then
868 Error_Msg_N ("aspect% can only apply to a type", Id);
869 return;
870
871 elsif not Is_First_Subtype (Ent) then
872 Error_Msg_N ("aspect% cannot apply to subtype", Id);
873 return;
874
875 elsif A_Id = Aspect_Default_Value
876 and then not Is_Scalar_Type (Ent)
877 then
878 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
879 return;
880
881 elsif A_Id = Aspect_Default_Component_Value then
882 if not Is_Array_Type (Ent) then
883 Error_Msg_N ("aspect% can only be applied to array type", Id);
884 return;
885
886 elsif not Is_Scalar_Type (Component_Type (Ent)) then
887 Error_Msg_N ("aspect% requires scalar components", Id);
888 return;
889 end if;
890 end if;
891
892 Set_Has_Default_Aspect (Base_Type (Ent));
893
894 if Is_Scalar_Type (Ent) then
895 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
896 else
897 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
898 end if;
899 end Analyze_Aspect_Default_Value;
900
901 ---------------------------------
902 -- Inherit_Delayed_Rep_Aspects --
903 ---------------------------------
904
905 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
906 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
907 P : constant Entity_Id := Entity (ASN);
908 -- Entithy for parent type
909
910 N : Node_Id;
911 -- Item from Rep_Item chain
912
913 A : Aspect_Id;
914
915 begin
916 -- Loop through delayed aspects for the parent type
917
918 N := ASN;
919 while Present (N) loop
920 if Nkind (N) = N_Aspect_Specification then
921 exit when Entity (N) /= P;
922
923 if Is_Delayed_Aspect (N) then
924 A := Get_Aspect_Id (Chars (Identifier (N)));
925
926 -- Process delayed rep aspect. For Boolean attributes it is
927 -- not possible to cancel an attribute once set (the attempt
928 -- to use an aspect with xxx => False is an error) for a
929 -- derived type. So for those cases, we do not have to check
930 -- if a clause has been given for the derived type, since it
931 -- is harmless to set it again if it is already set.
932
933 case A is
934
935 -- Alignment
936
937 when Aspect_Alignment =>
938 if not Has_Alignment_Clause (E) then
939 Set_Alignment (E, Alignment (P));
940 end if;
941
942 -- Atomic
943
944 when Aspect_Atomic =>
945 if Is_Atomic (P) then
946 Set_Is_Atomic (E);
947 end if;
948
949 -- Atomic_Components
950
951 when Aspect_Atomic_Components =>
952 if Has_Atomic_Components (P) then
953 Set_Has_Atomic_Components (Base_Type (E));
954 end if;
955
956 -- Bit_Order
957
958 when Aspect_Bit_Order =>
959 if Is_Record_Type (E)
960 and then No (Get_Attribute_Definition_Clause
961 (E, Attribute_Bit_Order))
962 and then Reverse_Bit_Order (P)
963 then
964 Set_Reverse_Bit_Order (Base_Type (E));
965 end if;
966
967 -- Component_Size
968
969 when Aspect_Component_Size =>
970 if Is_Array_Type (E)
971 and then not Has_Component_Size_Clause (E)
972 then
973 Set_Component_Size
974 (Base_Type (E), Component_Size (P));
975 end if;
976
977 -- Machine_Radix
978
979 when Aspect_Machine_Radix =>
980 if Is_Decimal_Fixed_Point_Type (E)
981 and then not Has_Machine_Radix_Clause (E)
982 then
983 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
984 end if;
985
986 -- Object_Size (also Size which also sets Object_Size)
987
988 when Aspect_Object_Size
989 | Aspect_Size
990 =>
991 if not Has_Size_Clause (E)
992 and then
993 No (Get_Attribute_Definition_Clause
994 (E, Attribute_Object_Size))
995 then
996 Set_Esize (E, Esize (P));
997 end if;
998
999 -- Pack
1000
1001 when Aspect_Pack =>
1002 if not Is_Packed (E) then
1003 Set_Is_Packed (Base_Type (E));
1004
1005 if Is_Bit_Packed_Array (P) then
1006 Set_Is_Bit_Packed_Array (Base_Type (E));
1007 Set_Packed_Array_Impl_Type
1008 (E, Packed_Array_Impl_Type (P));
1009 end if;
1010 end if;
1011
1012 -- Scalar_Storage_Order
1013
1014 when Aspect_Scalar_Storage_Order =>
1015 if (Is_Record_Type (E) or else Is_Array_Type (E))
1016 and then No (Get_Attribute_Definition_Clause
1017 (E, Attribute_Scalar_Storage_Order))
1018 and then Reverse_Storage_Order (P)
1019 then
1020 Set_Reverse_Storage_Order (Base_Type (E));
1021
1022 -- Clear default SSO indications, since the aspect
1023 -- overrides the default.
1024
1025 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
1026 Set_SSO_Set_High_By_Default (Base_Type (E), False);
1027 end if;
1028
1029 -- Small
1030
1031 when Aspect_Small =>
1032 if Is_Fixed_Point_Type (E)
1033 and then not Has_Small_Clause (E)
1034 then
1035 Set_Small_Value (E, Small_Value (P));
1036 end if;
1037
1038 -- Storage_Size
1039
1040 when Aspect_Storage_Size =>
1041 if (Is_Access_Type (E) or else Is_Task_Type (E))
1042 and then not Has_Storage_Size_Clause (E)
1043 then
1044 Set_Storage_Size_Variable
1045 (Base_Type (E), Storage_Size_Variable (P));
1046 end if;
1047
1048 -- Value_Size
1049
1050 when Aspect_Value_Size =>
1051
1052 -- Value_Size is never inherited, it is either set by
1053 -- default, or it is explicitly set for the derived
1054 -- type. So nothing to do here.
1055
1056 null;
1057
1058 -- Volatile
1059
1060 when Aspect_Volatile =>
1061 if Is_Volatile (P) then
1062 Set_Is_Volatile (E);
1063 end if;
1064
1065 -- Volatile_Full_Access
1066
1067 when Aspect_Volatile_Full_Access =>
1068 if Is_Volatile_Full_Access (P) then
1069 Set_Is_Volatile_Full_Access (E);
1070 end if;
1071
1072 -- Volatile_Components
1073
1074 when Aspect_Volatile_Components =>
1075 if Has_Volatile_Components (P) then
1076 Set_Has_Volatile_Components (Base_Type (E));
1077 end if;
1078
1079 -- That should be all the Rep Aspects
1080
1081 when others =>
1082 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1083 null;
1084 end case;
1085 end if;
1086 end if;
1087
1088 N := Next_Rep_Item (N);
1089 end loop;
1090 end Inherit_Delayed_Rep_Aspects;
1091
1092 -------------------------------------
1093 -- Make_Pragma_From_Boolean_Aspect --
1094 -------------------------------------
1095
1096 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1097 Ident : constant Node_Id := Identifier (ASN);
1098 A_Name : constant Name_Id := Chars (Ident);
1099 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1100 Ent : constant Entity_Id := Entity (ASN);
1101 Expr : constant Node_Id := Expression (ASN);
1102 Loc : constant Source_Ptr := Sloc (ASN);
1103
1104 procedure Check_False_Aspect_For_Derived_Type;
1105 -- This procedure checks for the case of a false aspect for a derived
1106 -- type, which improperly tries to cancel an aspect inherited from
1107 -- the parent.
1108
1109 -----------------------------------------
1110 -- Check_False_Aspect_For_Derived_Type --
1111 -----------------------------------------
1112
1113 procedure Check_False_Aspect_For_Derived_Type is
1114 Par : Node_Id;
1115
1116 begin
1117 -- We are only checking derived types
1118
1119 if not Is_Derived_Type (E) then
1120 return;
1121 end if;
1122
1123 Par := Nearest_Ancestor (E);
1124
1125 case A_Id is
1126 when Aspect_Atomic
1127 | Aspect_Shared
1128 =>
1129 if not Is_Atomic (Par) then
1130 return;
1131 end if;
1132
1133 when Aspect_Atomic_Components =>
1134 if not Has_Atomic_Components (Par) then
1135 return;
1136 end if;
1137
1138 when Aspect_Discard_Names =>
1139 if not Discard_Names (Par) then
1140 return;
1141 end if;
1142
1143 when Aspect_Pack =>
1144 if not Is_Packed (Par) then
1145 return;
1146 end if;
1147
1148 when Aspect_Unchecked_Union =>
1149 if not Is_Unchecked_Union (Par) then
1150 return;
1151 end if;
1152
1153 when Aspect_Volatile =>
1154 if not Is_Volatile (Par) then
1155 return;
1156 end if;
1157
1158 when Aspect_Volatile_Components =>
1159 if not Has_Volatile_Components (Par) then
1160 return;
1161 end if;
1162
1163 when Aspect_Volatile_Full_Access =>
1164 if not Is_Volatile_Full_Access (Par) then
1165 return;
1166 end if;
1167
1168 when others =>
1169 return;
1170 end case;
1171
1172 -- Fall through means we are canceling an inherited aspect
1173
1174 Error_Msg_Name_1 := A_Name;
1175 Error_Msg_NE
1176 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1177 end Check_False_Aspect_For_Derived_Type;
1178
1179 -- Local variables
1180
1181 Prag : Node_Id;
1182
1183 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1184
1185 begin
1186 -- Note that we know Expr is present, because for a missing Expr
1187 -- argument, we knew it was True and did not need to delay the
1188 -- evaluation to the freeze point.
1189
1190 if Is_False (Static_Boolean (Expr)) then
1191 Check_False_Aspect_For_Derived_Type;
1192
1193 else
1194 Prag :=
1195 Make_Pragma (Loc,
1196 Pragma_Identifier =>
1197 Make_Identifier (Sloc (Ident), Chars (Ident)),
1198 Pragma_Argument_Associations => New_List (
1199 Make_Pragma_Argument_Association (Sloc (Ident),
1200 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1201
1202 Set_From_Aspect_Specification (Prag, True);
1203 Set_Corresponding_Aspect (Prag, ASN);
1204 Set_Aspect_Rep_Item (ASN, Prag);
1205 Set_Is_Delayed_Aspect (Prag);
1206 Set_Parent (Prag, ASN);
1207 end if;
1208 end Make_Pragma_From_Boolean_Aspect;
1209
1210 -- Local variables
1211
1212 A_Id : Aspect_Id;
1213 ASN : Node_Id;
1214 Ritem : Node_Id;
1215
1216 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1217
1218 begin
1219 -- Must be visible in current scope, but if this is a type from a nested
1220 -- package it may be frozen from an object declaration in the enclosing
1221 -- scope, so install the package declarations to complete the analysis
1222 -- of the aspects, if any. If the package itself is frozen the type will
1223 -- have been frozen as well.
1224
1225 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1226 if Is_Type (E) and then From_Nested_Package (E) then
1227 declare
1228 Pack : constant Entity_Id := Scope (E);
1229
1230 begin
1231 Push_Scope (Pack);
1232 Install_Visible_Declarations (Pack);
1233 Install_Private_Declarations (Pack);
1234 Analyze_Aspects_At_Freeze_Point (E);
1235
1236 if Is_Private_Type (E)
1237 and then Present (Full_View (E))
1238 then
1239 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1240 end if;
1241
1242 End_Package_Scope (Pack);
1243 return;
1244 end;
1245
1246 -- Aspects from other entities in different contexts are analyzed
1247 -- elsewhere.
1248
1249 else
1250 return;
1251 end if;
1252 end if;
1253
1254 -- Look for aspect specification entries for this entity
1255
1256 ASN := First_Rep_Item (E);
1257 while Present (ASN) loop
1258 if Nkind (ASN) = N_Aspect_Specification then
1259 exit when Entity (ASN) /= E;
1260
1261 if Is_Delayed_Aspect (ASN) then
1262 A_Id := Get_Aspect_Id (ASN);
1263
1264 case A_Id is
1265
1266 -- For aspects whose expression is an optional Boolean, make
1267 -- the corresponding pragma at the freeze point.
1268
1269 when Boolean_Aspects
1270 | Library_Unit_Aspects
1271 =>
1272 -- Aspects Export and Import require special handling.
1273 -- Both are by definition Boolean and may benefit from
1274 -- forward references, however their expressions are
1275 -- treated as static. In addition, the syntax of their
1276 -- corresponding pragmas requires extra "pieces" which
1277 -- may also contain forward references. To account for
1278 -- all of this, the corresponding pragma is created by
1279 -- Analyze_Aspect_Export_Import, but is not analyzed as
1280 -- the complete analysis must happen now.
1281
1282 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1283 null;
1284
1285 -- Otherwise create a corresponding pragma
1286
1287 else
1288 Make_Pragma_From_Boolean_Aspect (ASN);
1289 end if;
1290
1291 -- Special handling for aspects that don't correspond to
1292 -- pragmas/attributes.
1293
1294 when Aspect_Default_Value
1295 | Aspect_Default_Component_Value
1296 =>
1297 -- Do not inherit aspect for anonymous base type of a
1298 -- scalar or array type, because they apply to the first
1299 -- subtype of the type, and will be processed when that
1300 -- first subtype is frozen.
1301
1302 if Is_Derived_Type (E)
1303 and then not Comes_From_Source (E)
1304 and then E /= First_Subtype (E)
1305 then
1306 null;
1307 else
1308 Analyze_Aspect_Default_Value (ASN);
1309 end if;
1310
1311 -- Ditto for iterator aspects, because the corresponding
1312 -- attributes may not have been analyzed yet.
1313
1314 when Aspect_Constant_Indexing
1315 | Aspect_Default_Iterator
1316 | Aspect_Iterator_Element
1317 | Aspect_Variable_Indexing
1318 =>
1319 Analyze (Expression (ASN));
1320
1321 if Etype (Expression (ASN)) = Any_Type then
1322 Error_Msg_NE
1323 ("\aspect must be fully defined before & is frozen",
1324 ASN, E);
1325 end if;
1326
1327 when Aspect_Iterable =>
1328 Validate_Iterable_Aspect (E, ASN);
1329
1330 when others =>
1331 null;
1332 end case;
1333
1334 Ritem := Aspect_Rep_Item (ASN);
1335
1336 if Present (Ritem) then
1337 Analyze (Ritem);
1338 end if;
1339 end if;
1340 end if;
1341
1342 Next_Rep_Item (ASN);
1343 end loop;
1344
1345 -- This is where we inherit delayed rep aspects from our parent. Note
1346 -- that if we fell out of the above loop with ASN non-empty, it means
1347 -- we hit an aspect for an entity other than E, and it must be the
1348 -- type from which we were derived.
1349
1350 if May_Inherit_Delayed_Rep_Aspects (E) then
1351 Inherit_Delayed_Rep_Aspects (ASN);
1352 end if;
1353
1354 if In_Instance
1355 and then E /= Base_Type (E)
1356 and then Is_First_Subtype (E)
1357 then
1358 Inherit_Rep_Item_Chain (Base_Type (E), E);
1359 end if;
1360 end Analyze_Aspects_At_Freeze_Point;
1361
1362 -----------------------------------
1363 -- Analyze_Aspect_Specifications --
1364 -----------------------------------
1365
1366 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1367 pragma Assert (Present (E));
1368
1369 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1370 -- Establish linkages between an aspect and its corresponding pragma
1371
1372 procedure Insert_Pragma
1373 (Prag : Node_Id;
1374 Is_Instance : Boolean := False);
1375 -- Subsidiary to the analysis of aspects
1376 -- Abstract_State
1377 -- Attach_Handler
1378 -- Contract_Cases
1379 -- Depends
1380 -- Ghost
1381 -- Global
1382 -- Initial_Condition
1383 -- Initializes
1384 -- Post
1385 -- Pre
1386 -- Refined_Depends
1387 -- Refined_Global
1388 -- Refined_State
1389 -- SPARK_Mode
1390 -- Warnings
1391 -- Insert pragma Prag such that it mimics the placement of a source
1392 -- pragma of the same kind. Flag Is_Generic should be set when the
1393 -- context denotes a generic instance.
1394
1395 --------------
1396 -- Decorate --
1397 --------------
1398
1399 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1400 begin
1401 Set_Aspect_Rep_Item (Asp, Prag);
1402 Set_Corresponding_Aspect (Prag, Asp);
1403 Set_From_Aspect_Specification (Prag);
1404 Set_Parent (Prag, Asp);
1405 end Decorate;
1406
1407 -------------------
1408 -- Insert_Pragma --
1409 -------------------
1410
1411 procedure Insert_Pragma
1412 (Prag : Node_Id;
1413 Is_Instance : Boolean := False)
1414 is
1415 Aux : Node_Id;
1416 Decl : Node_Id;
1417 Decls : List_Id;
1418 Def : Node_Id;
1419 Inserted : Boolean := False;
1420
1421 begin
1422 -- When the aspect appears on an entry, package, protected unit,
1423 -- subprogram, or task unit body, insert the generated pragma at the
1424 -- top of the body declarations to emulate the behavior of a source
1425 -- pragma.
1426
1427 -- package body Pack with Aspect is
1428
1429 -- package body Pack is
1430 -- pragma Prag;
1431
1432 if Nkind_In (N, N_Entry_Body,
1433 N_Package_Body,
1434 N_Protected_Body,
1435 N_Subprogram_Body,
1436 N_Task_Body)
1437 then
1438 Decls := Declarations (N);
1439
1440 if No (Decls) then
1441 Decls := New_List;
1442 Set_Declarations (N, Decls);
1443 end if;
1444
1445 Prepend_To (Decls, Prag);
1446
1447 -- When the aspect is associated with a [generic] package declaration
1448 -- insert the generated pragma at the top of the visible declarations
1449 -- to emulate the behavior of a source pragma.
1450
1451 -- package Pack with Aspect is
1452
1453 -- package Pack is
1454 -- pragma Prag;
1455
1456 elsif Nkind_In (N, N_Generic_Package_Declaration,
1457 N_Package_Declaration)
1458 then
1459 Decls := Visible_Declarations (Specification (N));
1460
1461 if No (Decls) then
1462 Decls := New_List;
1463 Set_Visible_Declarations (Specification (N), Decls);
1464 end if;
1465
1466 -- The visible declarations of a generic instance have the
1467 -- following structure:
1468
1469 -- <renamings of generic formals>
1470 -- <renamings of internally-generated spec and body>
1471 -- <first source declaration>
1472
1473 -- Insert the pragma before the first source declaration by
1474 -- skipping the instance "header" to ensure proper visibility of
1475 -- all formals.
1476
1477 if Is_Instance then
1478 Decl := First (Decls);
1479 while Present (Decl) loop
1480 if Comes_From_Source (Decl) then
1481 Insert_Before (Decl, Prag);
1482 Inserted := True;
1483 exit;
1484 else
1485 Next (Decl);
1486 end if;
1487 end loop;
1488
1489 -- The pragma is placed after the instance "header"
1490
1491 if not Inserted then
1492 Append_To (Decls, Prag);
1493 end if;
1494
1495 -- Otherwise this is not a generic instance
1496
1497 else
1498 Prepend_To (Decls, Prag);
1499 end if;
1500
1501 -- When the aspect is associated with a protected unit declaration,
1502 -- insert the generated pragma at the top of the visible declarations
1503 -- the emulate the behavior of a source pragma.
1504
1505 -- protected [type] Prot with Aspect is
1506
1507 -- protected [type] Prot is
1508 -- pragma Prag;
1509
1510 elsif Nkind (N) = N_Protected_Type_Declaration then
1511 Def := Protected_Definition (N);
1512
1513 if No (Def) then
1514 Def :=
1515 Make_Protected_Definition (Sloc (N),
1516 Visible_Declarations => New_List,
1517 End_Label => Empty);
1518
1519 Set_Protected_Definition (N, Def);
1520 end if;
1521
1522 Decls := Visible_Declarations (Def);
1523
1524 if No (Decls) then
1525 Decls := New_List;
1526 Set_Visible_Declarations (Def, Decls);
1527 end if;
1528
1529 Prepend_To (Decls, Prag);
1530
1531 -- When the aspect is associated with a task unit declaration, insert
1532 -- insert the generated pragma at the top of the visible declarations
1533 -- the emulate the behavior of a source pragma.
1534
1535 -- task [type] Prot with Aspect is
1536
1537 -- task [type] Prot is
1538 -- pragma Prag;
1539
1540 elsif Nkind (N) = N_Task_Type_Declaration then
1541 Def := Task_Definition (N);
1542
1543 if No (Def) then
1544 Def :=
1545 Make_Task_Definition (Sloc (N),
1546 Visible_Declarations => New_List,
1547 End_Label => Empty);
1548
1549 Set_Task_Definition (N, Def);
1550 end if;
1551
1552 Decls := Visible_Declarations (Def);
1553
1554 if No (Decls) then
1555 Decls := New_List;
1556 Set_Visible_Declarations (Def, Decls);
1557 end if;
1558
1559 Prepend_To (Decls, Prag);
1560
1561 -- When the context is a library unit, the pragma is added to the
1562 -- Pragmas_After list.
1563
1564 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1565 Aux := Aux_Decls_Node (Parent (N));
1566
1567 if No (Pragmas_After (Aux)) then
1568 Set_Pragmas_After (Aux, New_List);
1569 end if;
1570
1571 Prepend (Prag, Pragmas_After (Aux));
1572
1573 -- Default, the pragma is inserted after the context
1574
1575 else
1576 Insert_After (N, Prag);
1577 end if;
1578 end Insert_Pragma;
1579
1580 -- Local variables
1581
1582 Aspect : Node_Id;
1583 Aitem : Node_Id;
1584 Ent : Node_Id;
1585
1586 L : constant List_Id := Aspect_Specifications (N);
1587 pragma Assert (Present (L));
1588
1589 Ins_Node : Node_Id := N;
1590 -- Insert pragmas/attribute definition clause after this node when no
1591 -- delayed analysis is required.
1592
1593 -- Start of processing for Analyze_Aspect_Specifications
1594
1595 begin
1596 -- The general processing involves building an attribute definition
1597 -- clause or a pragma node that corresponds to the aspect. Then in order
1598 -- to delay the evaluation of this aspect to the freeze point, we attach
1599 -- the corresponding pragma/attribute definition clause to the aspect
1600 -- specification node, which is then placed in the Rep Item chain. In
1601 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1602 -- and we evaluate the rep item at the freeze point. When the aspect
1603 -- doesn't have a corresponding pragma/attribute definition clause, then
1604 -- its analysis is simply delayed at the freeze point.
1605
1606 -- Some special cases don't require delay analysis, thus the aspect is
1607 -- analyzed right now.
1608
1609 -- Note that there is a special handling for Pre, Post, Test_Case,
1610 -- Contract_Cases aspects. In these cases, we do not have to worry
1611 -- about delay issues, since the pragmas themselves deal with delay
1612 -- of visibility for the expression analysis. Thus, we just insert
1613 -- the pragma after the node N.
1614
1615 -- Loop through aspects
1616
1617 Aspect := First (L);
1618 Aspect_Loop : while Present (Aspect) loop
1619 Analyze_One_Aspect : declare
1620 Expr : constant Node_Id := Expression (Aspect);
1621 Id : constant Node_Id := Identifier (Aspect);
1622 Loc : constant Source_Ptr := Sloc (Aspect);
1623 Nam : constant Name_Id := Chars (Id);
1624 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1625 Anod : Node_Id;
1626
1627 Delay_Required : Boolean;
1628 -- Set False if delay is not required
1629
1630 Eloc : Source_Ptr := No_Location;
1631 -- Source location of expression, modified when we split PPC's. It
1632 -- is set below when Expr is present.
1633
1634 procedure Analyze_Aspect_Convention;
1635 -- Perform analysis of aspect Convention
1636
1637 procedure Analyze_Aspect_Disable_Controlled;
1638 -- Perform analysis of aspect Disable_Controlled
1639
1640 procedure Analyze_Aspect_Export_Import;
1641 -- Perform analysis of aspects Export or Import
1642
1643 procedure Analyze_Aspect_External_Link_Name;
1644 -- Perform analysis of aspects External_Name or Link_Name
1645
1646 procedure Analyze_Aspect_Implicit_Dereference;
1647 -- Perform analysis of the Implicit_Dereference aspects
1648
1649 procedure Make_Aitem_Pragma
1650 (Pragma_Argument_Associations : List_Id;
1651 Pragma_Name : Name_Id);
1652 -- This is a wrapper for Make_Pragma used for converting aspects
1653 -- to pragmas. It takes care of Sloc (set from Loc) and building
1654 -- the pragma identifier from the given name. In addition the
1655 -- flags Class_Present and Split_PPC are set from the aspect
1656 -- node, as well as Is_Ignored. This routine also sets the
1657 -- From_Aspect_Specification in the resulting pragma node to
1658 -- True, and sets Corresponding_Aspect to point to the aspect.
1659 -- The resulting pragma is assigned to Aitem.
1660
1661 -------------------------------
1662 -- Analyze_Aspect_Convention --
1663 -------------------------------
1664
1665 procedure Analyze_Aspect_Convention is
1666 Conv : Node_Id;
1667 Dummy_1 : Node_Id;
1668 Dummy_2 : Node_Id;
1669 Dummy_3 : Node_Id;
1670 Expo : Node_Id;
1671 Imp : Node_Id;
1672
1673 begin
1674 -- Obtain all interfacing aspects that apply to the related
1675 -- entity.
1676
1677 Get_Interfacing_Aspects
1678 (Iface_Asp => Aspect,
1679 Conv_Asp => Dummy_1,
1680 EN_Asp => Dummy_2,
1681 Expo_Asp => Expo,
1682 Imp_Asp => Imp,
1683 LN_Asp => Dummy_3,
1684 Do_Checks => True);
1685
1686 -- The related entity is subject to aspect Export or Import.
1687 -- Do not process Convention now because it must be analysed
1688 -- as part of Export or Import.
1689
1690 if Present (Expo) or else Present (Imp) then
1691 return;
1692
1693 -- Otherwise Convention appears by itself
1694
1695 else
1696 -- The aspect specifies a particular convention
1697
1698 if Present (Expr) then
1699 Conv := New_Copy_Tree (Expr);
1700
1701 -- Otherwise assume convention Ada
1702
1703 else
1704 Conv := Make_Identifier (Loc, Name_Ada);
1705 end if;
1706
1707 -- Generate:
1708 -- pragma Convention (<Conv>, <E>);
1709
1710 Make_Aitem_Pragma
1711 (Pragma_Name => Name_Convention,
1712 Pragma_Argument_Associations => New_List (
1713 Make_Pragma_Argument_Association (Loc,
1714 Expression => Conv),
1715 Make_Pragma_Argument_Association (Loc,
1716 Expression => New_Occurrence_Of (E, Loc))));
1717
1718 Decorate (Aspect, Aitem);
1719 Insert_Pragma (Aitem);
1720 end if;
1721 end Analyze_Aspect_Convention;
1722
1723 ---------------------------------------
1724 -- Analyze_Aspect_Disable_Controlled --
1725 ---------------------------------------
1726
1727 procedure Analyze_Aspect_Disable_Controlled is
1728 begin
1729 -- The aspect applies only to controlled records
1730
1731 if not (Ekind (E) = E_Record_Type
1732 and then Is_Controlled_Active (E))
1733 then
1734 Error_Msg_N
1735 ("aspect % requires controlled record type", Aspect);
1736 return;
1737 end if;
1738
1739 -- Preanalyze the expression (if any) when the aspect resides
1740 -- in a generic unit.
1741
1742 if Inside_A_Generic then
1743 if Present (Expr) then
1744 Preanalyze_And_Resolve (Expr, Any_Boolean);
1745 end if;
1746
1747 -- Otherwise the aspect resides in a nongeneric context
1748
1749 else
1750 -- A controlled record type loses its controlled semantics
1751 -- when the expression statically evaluates to True.
1752
1753 if Present (Expr) then
1754 Analyze_And_Resolve (Expr, Any_Boolean);
1755
1756 if Is_OK_Static_Expression (Expr) then
1757 if Is_True (Static_Boolean (Expr)) then
1758 Set_Disable_Controlled (E);
1759 end if;
1760
1761 -- Otherwise the expression is not static
1762
1763 else
1764 Error_Msg_N
1765 ("expression of aspect % must be static", Aspect);
1766 end if;
1767
1768 -- Otherwise the aspect appears without an expression and
1769 -- defaults to True.
1770
1771 else
1772 Set_Disable_Controlled (E);
1773 end if;
1774 end if;
1775 end Analyze_Aspect_Disable_Controlled;
1776
1777 ----------------------------------
1778 -- Analyze_Aspect_Export_Import --
1779 ----------------------------------
1780
1781 procedure Analyze_Aspect_Export_Import is
1782 Dummy_1 : Node_Id;
1783 Dummy_2 : Node_Id;
1784 Dummy_3 : Node_Id;
1785 Expo : Node_Id;
1786 Imp : Node_Id;
1787
1788 begin
1789 -- Obtain all interfacing aspects that apply to the related
1790 -- entity.
1791
1792 Get_Interfacing_Aspects
1793 (Iface_Asp => Aspect,
1794 Conv_Asp => Dummy_1,
1795 EN_Asp => Dummy_2,
1796 Expo_Asp => Expo,
1797 Imp_Asp => Imp,
1798 LN_Asp => Dummy_3,
1799 Do_Checks => True);
1800
1801 -- The related entity cannot be subject to both aspects Export
1802 -- and Import.
1803
1804 if Present (Expo) and then Present (Imp) then
1805 Error_Msg_N
1806 ("incompatible interfacing aspects given for &", E);
1807 Error_Msg_Sloc := Sloc (Expo);
1808 Error_Msg_N ("\aspect `Export` #", E);
1809 Error_Msg_Sloc := Sloc (Imp);
1810 Error_Msg_N ("\aspect `Import` #", E);
1811 end if;
1812
1813 -- A variable is most likely modified from the outside. Take
1814 -- the optimistic approach to avoid spurious errors.
1815
1816 if Ekind (E) = E_Variable then
1817 Set_Never_Set_In_Source (E, False);
1818 end if;
1819
1820 -- Resolve the expression of an Import or Export here, and
1821 -- require it to be of type Boolean and static. This is not
1822 -- quite right, because in general this should be delayed,
1823 -- but that seems tricky for these, because normally Boolean
1824 -- aspects are replaced with pragmas at the freeze point in
1825 -- Make_Pragma_From_Boolean_Aspect.
1826
1827 if not Present (Expr)
1828 or else Is_True (Static_Boolean (Expr))
1829 then
1830 if A_Id = Aspect_Import then
1831 Set_Has_Completion (E);
1832 Set_Is_Imported (E);
1833
1834 -- An imported object cannot be explicitly initialized
1835
1836 if Nkind (N) = N_Object_Declaration
1837 and then Present (Expression (N))
1838 then
1839 Error_Msg_N
1840 ("imported entities cannot be initialized "
1841 & "(RM B.1(24))", Expression (N));
1842 end if;
1843
1844 else
1845 pragma Assert (A_Id = Aspect_Export);
1846 Set_Is_Exported (E);
1847 end if;
1848
1849 -- Create the proper form of pragma Export or Import taking
1850 -- into account Conversion, External_Name, and Link_Name.
1851
1852 Aitem := Build_Export_Import_Pragma (Aspect, E);
1853
1854 -- Otherwise the expression is either False or erroneous. There
1855 -- is no corresponding pragma.
1856
1857 else
1858 Aitem := Empty;
1859 end if;
1860 end Analyze_Aspect_Export_Import;
1861
1862 ---------------------------------------
1863 -- Analyze_Aspect_External_Link_Name --
1864 ---------------------------------------
1865
1866 procedure Analyze_Aspect_External_Link_Name is
1867 Dummy_1 : Node_Id;
1868 Dummy_2 : Node_Id;
1869 Dummy_3 : Node_Id;
1870 Expo : Node_Id;
1871 Imp : Node_Id;
1872
1873 begin
1874 -- Obtain all interfacing aspects that apply to the related
1875 -- entity.
1876
1877 Get_Interfacing_Aspects
1878 (Iface_Asp => Aspect,
1879 Conv_Asp => Dummy_1,
1880 EN_Asp => Dummy_2,
1881 Expo_Asp => Expo,
1882 Imp_Asp => Imp,
1883 LN_Asp => Dummy_3,
1884 Do_Checks => True);
1885
1886 -- Ensure that aspect External_Name applies to aspect Export or
1887 -- Import.
1888
1889 if A_Id = Aspect_External_Name then
1890 if No (Expo) and then No (Imp) then
1891 Error_Msg_N
1892 ("aspect `External_Name` requires aspect `Import` or "
1893 & "`Export`", Aspect);
1894 end if;
1895
1896 -- Otherwise ensure that aspect Link_Name applies to aspect
1897 -- Export or Import.
1898
1899 else
1900 pragma Assert (A_Id = Aspect_Link_Name);
1901 if No (Expo) and then No (Imp) then
1902 Error_Msg_N
1903 ("aspect `Link_Name` requires aspect `Import` or "
1904 & "`Export`", Aspect);
1905 end if;
1906 end if;
1907 end Analyze_Aspect_External_Link_Name;
1908
1909 -----------------------------------------
1910 -- Analyze_Aspect_Implicit_Dereference --
1911 -----------------------------------------
1912
1913 procedure Analyze_Aspect_Implicit_Dereference is
1914 begin
1915 if not Is_Type (E) or else not Has_Discriminants (E) then
1916 Error_Msg_N
1917 ("aspect must apply to a type with discriminants", Expr);
1918
1919 elsif not Is_Entity_Name (Expr) then
1920 Error_Msg_N
1921 ("aspect must name a discriminant of current type", Expr);
1922
1923 else
1924 -- Discriminant type be an anonymous access type or an
1925 -- anonymous access to subprogram.
1926
1927 -- Missing synchronized types???
1928
1929 declare
1930 Disc : Entity_Id := First_Discriminant (E);
1931 begin
1932 while Present (Disc) loop
1933 if Chars (Expr) = Chars (Disc)
1934 and then Ekind_In
1935 (Etype (Disc),
1936 E_Anonymous_Access_Subprogram_Type,
1937 E_Anonymous_Access_Type)
1938 then
1939 Set_Has_Implicit_Dereference (E);
1940 Set_Has_Implicit_Dereference (Disc);
1941 exit;
1942 end if;
1943
1944 Next_Discriminant (Disc);
1945 end loop;
1946
1947 -- Error if no proper access discriminant
1948
1949 if Present (Disc) then
1950 -- For a type extension, check whether parent has
1951 -- a reference discriminant, to verify that use is
1952 -- proper.
1953
1954 if Is_Derived_Type (E)
1955 and then Has_Discriminants (Etype (E))
1956 then
1957 declare
1958 Parent_Disc : constant Entity_Id :=
1959 Get_Reference_Discriminant (Etype (E));
1960 begin
1961 if Present (Parent_Disc)
1962 and then Corresponding_Discriminant (Disc) /=
1963 Parent_Disc
1964 then
1965 Error_Msg_N
1966 ("reference discriminant does not match "
1967 & "discriminant of parent type", Expr);
1968 end if;
1969 end;
1970 end if;
1971
1972 else
1973 Error_Msg_NE
1974 ("not an access discriminant of&", Expr, E);
1975 end if;
1976 end;
1977 end if;
1978
1979 end Analyze_Aspect_Implicit_Dereference;
1980
1981 -----------------------
1982 -- Make_Aitem_Pragma --
1983 -----------------------
1984
1985 procedure Make_Aitem_Pragma
1986 (Pragma_Argument_Associations : List_Id;
1987 Pragma_Name : Name_Id)
1988 is
1989 Args : List_Id := Pragma_Argument_Associations;
1990
1991 begin
1992 -- We should never get here if aspect was disabled
1993
1994 pragma Assert (not Is_Disabled (Aspect));
1995
1996 -- Certain aspects allow for an optional name or expression. Do
1997 -- not generate a pragma with empty argument association list.
1998
1999 if No (Args) or else No (Expression (First (Args))) then
2000 Args := No_List;
2001 end if;
2002
2003 -- Build the pragma
2004
2005 Aitem :=
2006 Make_Pragma (Loc,
2007 Pragma_Argument_Associations => Args,
2008 Pragma_Identifier =>
2009 Make_Identifier (Sloc (Id), Pragma_Name),
2010 Class_Present => Class_Present (Aspect),
2011 Split_PPC => Split_PPC (Aspect));
2012
2013 -- Set additional semantic fields
2014
2015 if Is_Ignored (Aspect) then
2016 Set_Is_Ignored (Aitem);
2017 elsif Is_Checked (Aspect) then
2018 Set_Is_Checked (Aitem);
2019 end if;
2020
2021 Set_Corresponding_Aspect (Aitem, Aspect);
2022 Set_From_Aspect_Specification (Aitem);
2023 end Make_Aitem_Pragma;
2024
2025 -- Start of processing for Analyze_One_Aspect
2026
2027 begin
2028 -- Skip aspect if already analyzed, to avoid looping in some cases
2029
2030 if Analyzed (Aspect) then
2031 goto Continue;
2032 end if;
2033
2034 -- Skip looking at aspect if it is totally disabled. Just mark it
2035 -- as such for later reference in the tree. This also sets the
2036 -- Is_Ignored and Is_Checked flags appropriately.
2037
2038 Check_Applicable_Policy (Aspect);
2039
2040 if Is_Disabled (Aspect) then
2041 goto Continue;
2042 end if;
2043
2044 -- Set the source location of expression, used in the case of
2045 -- a failed precondition/postcondition or invariant. Note that
2046 -- the source location of the expression is not usually the best
2047 -- choice here. For example, it gets located on the last AND
2048 -- keyword in a chain of boolean expressiond AND'ed together.
2049 -- It is best to put the message on the first character of the
2050 -- assertion, which is the effect of the First_Node call here.
2051
2052 if Present (Expr) then
2053 Eloc := Sloc (First_Node (Expr));
2054 end if;
2055
2056 -- Check restriction No_Implementation_Aspect_Specifications
2057
2058 if Implementation_Defined_Aspect (A_Id) then
2059 Check_Restriction
2060 (No_Implementation_Aspect_Specifications, Aspect);
2061 end if;
2062
2063 -- Check restriction No_Specification_Of_Aspect
2064
2065 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2066
2067 -- Mark aspect analyzed (actual analysis is delayed till later)
2068
2069 Set_Analyzed (Aspect);
2070 Set_Entity (Aspect, E);
2071
2072 -- Build the reference to E that will be used in the built pragmas
2073
2074 Ent := New_Occurrence_Of (E, Sloc (Id));
2075
2076 if A_Id = Aspect_Attach_Handler
2077 or else A_Id = Aspect_Interrupt_Handler
2078 then
2079
2080 -- Treat the specification as a reference to the protected
2081 -- operation, which might otherwise appear unreferenced and
2082 -- generate spurious warnings.
2083
2084 Generate_Reference (E, Id);
2085 end if;
2086
2087 -- Check for duplicate aspect. Note that the Comes_From_Source
2088 -- test allows duplicate Pre/Post's that we generate internally
2089 -- to escape being flagged here.
2090
2091 if No_Duplicates_Allowed (A_Id) then
2092 Anod := First (L);
2093 while Anod /= Aspect loop
2094 if Comes_From_Source (Aspect)
2095 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
2096 then
2097 Error_Msg_Name_1 := Nam;
2098 Error_Msg_Sloc := Sloc (Anod);
2099
2100 -- Case of same aspect specified twice
2101
2102 if Class_Present (Anod) = Class_Present (Aspect) then
2103 if not Class_Present (Anod) then
2104 Error_Msg_NE
2105 ("aspect% for & previously given#",
2106 Id, E);
2107 else
2108 Error_Msg_NE
2109 ("aspect `%''Class` for & previously given#",
2110 Id, E);
2111 end if;
2112 end if;
2113 end if;
2114
2115 Next (Anod);
2116 end loop;
2117 end if;
2118
2119 -- Check some general restrictions on language defined aspects
2120
2121 if not Implementation_Defined_Aspect (A_Id) then
2122 Error_Msg_Name_1 := Nam;
2123
2124 -- Not allowed for renaming declarations. Examine the original
2125 -- node because a subprogram renaming may have been rewritten
2126 -- as a body.
2127
2128 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
2129 Error_Msg_N
2130 ("aspect % not allowed for renaming declaration",
2131 Aspect);
2132 end if;
2133
2134 -- Not allowed for formal type declarations in previous
2135 -- versions of the language. Allowed for them only for
2136 -- shared variable control aspects.
2137
2138 if Nkind (N) = N_Formal_Type_Declaration then
2139 if Ada_Version < Ada_2020 then
2140 Error_Msg_N
2141 ("aspect % not allowed for formal type declaration",
2142 Aspect);
2143
2144 elsif A_Id /= Aspect_Atomic
2145 and then A_Id /= Aspect_Volatile
2146 and then A_Id /= Aspect_Independent
2147 and then A_Id /= Aspect_Atomic_Components
2148 and then A_Id /= Aspect_Independent_Components
2149 and then A_Id /= Aspect_Volatile_Components
2150 then
2151 Error_Msg_N
2152 ("aspect % not allowed for formal type declaration",
2153 Aspect);
2154 end if;
2155 end if;
2156 end if;
2157
2158 -- Copy expression for later processing by the procedures
2159 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2160
2161 Set_Entity (Id, New_Copy_Tree (Expr));
2162
2163 -- Set Delay_Required as appropriate to aspect
2164
2165 case Aspect_Delay (A_Id) is
2166 when Always_Delay =>
2167 Delay_Required := True;
2168
2169 when Never_Delay =>
2170 Delay_Required := False;
2171
2172 when Rep_Aspect =>
2173
2174 -- If expression has the form of an integer literal, then
2175 -- do not delay, since we know the value cannot change.
2176 -- This optimization catches most rep clause cases.
2177
2178 -- For Boolean aspects, don't delay if no expression
2179
2180 if A_Id in Boolean_Aspects and then No (Expr) then
2181 Delay_Required := False;
2182
2183 -- For non-Boolean aspects, don't delay if integer literal,
2184 -- unless the aspect is Alignment, which affects the
2185 -- freezing of an initialized object.
2186
2187 elsif A_Id not in Boolean_Aspects
2188 and then A_Id /= Aspect_Alignment
2189 and then Present (Expr)
2190 and then Nkind (Expr) = N_Integer_Literal
2191 then
2192 Delay_Required := False;
2193
2194 -- All other cases are delayed
2195
2196 else
2197 Delay_Required := True;
2198 Set_Has_Delayed_Rep_Aspects (E);
2199 end if;
2200 end case;
2201
2202 -- Processing based on specific aspect
2203
2204 case A_Id is
2205 when Aspect_Unimplemented =>
2206 null; -- ??? temp for now
2207
2208 -- No_Aspect should be impossible
2209
2210 when No_Aspect =>
2211 raise Program_Error;
2212
2213 -- Case 1: Aspects corresponding to attribute definition
2214 -- clauses.
2215
2216 when Aspect_Address
2217 | Aspect_Alignment
2218 | Aspect_Bit_Order
2219 | Aspect_Component_Size
2220 | Aspect_Constant_Indexing
2221 | Aspect_Default_Iterator
2222 | Aspect_Dispatching_Domain
2223 | Aspect_External_Tag
2224 | Aspect_Input
2225 | Aspect_Iterable
2226 | Aspect_Iterator_Element
2227 | Aspect_Machine_Radix
2228 | Aspect_Object_Size
2229 | Aspect_Output
2230 | Aspect_Read
2231 | Aspect_Scalar_Storage_Order
2232 | Aspect_Simple_Storage_Pool
2233 | Aspect_Size
2234 | Aspect_Small
2235 | Aspect_Storage_Pool
2236 | Aspect_Stream_Size
2237 | Aspect_Value_Size
2238 | Aspect_Variable_Indexing
2239 | Aspect_Write
2240 =>
2241 -- Indexing aspects apply only to tagged type
2242
2243 if (A_Id = Aspect_Constant_Indexing
2244 or else
2245 A_Id = Aspect_Variable_Indexing)
2246 and then not (Is_Type (E)
2247 and then Is_Tagged_Type (E))
2248 then
2249 Error_Msg_N
2250 ("indexing aspect can only apply to a tagged type",
2251 Aspect);
2252 goto Continue;
2253 end if;
2254
2255 -- For the case of aspect Address, we don't consider that we
2256 -- know the entity is never set in the source, since it is
2257 -- is likely aliasing is occurring.
2258
2259 -- Note: one might think that the analysis of the resulting
2260 -- attribute definition clause would take care of that, but
2261 -- that's not the case since it won't be from source.
2262
2263 if A_Id = Aspect_Address then
2264 Set_Never_Set_In_Source (E, False);
2265 end if;
2266
2267 -- Correctness of the profile of a stream operation is
2268 -- verified at the freeze point, but we must detect the
2269 -- illegal specification of this aspect for a subtype now,
2270 -- to prevent malformed rep_item chains.
2271
2272 if A_Id = Aspect_Input or else
2273 A_Id = Aspect_Output or else
2274 A_Id = Aspect_Read or else
2275 A_Id = Aspect_Write
2276 then
2277 if not Is_First_Subtype (E) then
2278 Error_Msg_N
2279 ("local name must be a first subtype", Aspect);
2280 goto Continue;
2281
2282 -- If stream aspect applies to the class-wide type,
2283 -- the generated attribute definition applies to the
2284 -- class-wide type as well.
2285
2286 elsif Class_Present (Aspect) then
2287 Ent :=
2288 Make_Attribute_Reference (Loc,
2289 Prefix => Ent,
2290 Attribute_Name => Name_Class);
2291 end if;
2292 end if;
2293
2294 -- Construct the attribute_definition_clause. The expression
2295 -- in the aspect specification is simply shared with the
2296 -- constructed attribute, because it will be fully analyzed
2297 -- when the attribute is processed. However, in ASIS mode
2298 -- the aspect expression itself is preanalyzed and resolved
2299 -- to catch visibility errors that are otherwise caught
2300 -- later, and we create a separate copy of the expression
2301 -- to prevent analysis of a malformed tree (e.g. a function
2302 -- call with parameter associations).
2303
2304 if ASIS_Mode then
2305 Aitem :=
2306 Make_Attribute_Definition_Clause (Loc,
2307 Name => Ent,
2308 Chars => Chars (Id),
2309 Expression => New_Copy_Tree (Expr));
2310 else
2311 Aitem :=
2312 Make_Attribute_Definition_Clause (Loc,
2313 Name => Ent,
2314 Chars => Chars (Id),
2315 Expression => Relocate_Node (Expr));
2316 end if;
2317
2318 -- If the address is specified, then we treat the entity as
2319 -- referenced, to avoid spurious warnings. This is analogous
2320 -- to what is done with an attribute definition clause, but
2321 -- here we don't want to generate a reference because this
2322 -- is the point of definition of the entity.
2323
2324 if A_Id = Aspect_Address then
2325 Set_Referenced (E);
2326 end if;
2327
2328 -- Case 2: Aspects corresponding to pragmas
2329
2330 -- Case 2a: Aspects corresponding to pragmas with two
2331 -- arguments, where the first argument is a local name
2332 -- referring to the entity, and the second argument is the
2333 -- aspect definition expression.
2334
2335 -- Linker_Section/Suppress/Unsuppress
2336
2337 when Aspect_Linker_Section
2338 | Aspect_Suppress
2339 | Aspect_Unsuppress
2340 =>
2341 Make_Aitem_Pragma
2342 (Pragma_Argument_Associations => New_List (
2343 Make_Pragma_Argument_Association (Loc,
2344 Expression => New_Occurrence_Of (E, Loc)),
2345 Make_Pragma_Argument_Association (Sloc (Expr),
2346 Expression => Relocate_Node (Expr))),
2347 Pragma_Name => Chars (Id));
2348
2349 -- Linker_Section does not need delaying, as its argument
2350 -- must be a static string. Furthermore, if applied to
2351 -- an object with an explicit initialization, the object
2352 -- must be frozen in order to elaborate the initialization
2353 -- code. (This is already done for types with implicit
2354 -- initialization, such as protected types.)
2355
2356 if A_Id = Aspect_Linker_Section
2357 and then Nkind (N) = N_Object_Declaration
2358 and then Has_Init_Expression (N)
2359 then
2360 Delay_Required := False;
2361 end if;
2362
2363 -- Synchronization
2364
2365 -- Corresponds to pragma Implemented, construct the pragma
2366
2367 when Aspect_Synchronization =>
2368 Make_Aitem_Pragma
2369 (Pragma_Argument_Associations => New_List (
2370 Make_Pragma_Argument_Association (Loc,
2371 Expression => New_Occurrence_Of (E, Loc)),
2372 Make_Pragma_Argument_Association (Sloc (Expr),
2373 Expression => Relocate_Node (Expr))),
2374 Pragma_Name => Name_Implemented);
2375
2376 -- Attach_Handler
2377
2378 when Aspect_Attach_Handler =>
2379 Make_Aitem_Pragma
2380 (Pragma_Argument_Associations => New_List (
2381 Make_Pragma_Argument_Association (Sloc (Ent),
2382 Expression => Ent),
2383 Make_Pragma_Argument_Association (Sloc (Expr),
2384 Expression => Relocate_Node (Expr))),
2385 Pragma_Name => Name_Attach_Handler);
2386
2387 -- We need to insert this pragma into the tree to get proper
2388 -- processing and to look valid from a placement viewpoint.
2389
2390 Insert_Pragma (Aitem);
2391 goto Continue;
2392
2393 -- Dynamic_Predicate, Predicate, Static_Predicate
2394
2395 when Aspect_Dynamic_Predicate
2396 | Aspect_Predicate
2397 | Aspect_Static_Predicate
2398 =>
2399 -- These aspects apply only to subtypes
2400
2401 if not Is_Type (E) then
2402 Error_Msg_N
2403 ("predicate can only be specified for a subtype",
2404 Aspect);
2405 goto Continue;
2406
2407 elsif Is_Incomplete_Type (E) then
2408 Error_Msg_N
2409 ("predicate cannot apply to incomplete view", Aspect);
2410
2411 elsif Is_Generic_Type (E) then
2412 Error_Msg_N
2413 ("predicate cannot apply to formal type", Aspect);
2414 goto Continue;
2415 end if;
2416
2417 -- Construct the pragma (always a pragma Predicate, with
2418 -- flags recording whether it is static/dynamic). We also
2419 -- set flags recording this in the type itself.
2420
2421 Make_Aitem_Pragma
2422 (Pragma_Argument_Associations => New_List (
2423 Make_Pragma_Argument_Association (Sloc (Ent),
2424 Expression => Ent),
2425 Make_Pragma_Argument_Association (Sloc (Expr),
2426 Expression => Relocate_Node (Expr))),
2427 Pragma_Name => Name_Predicate);
2428
2429 -- Mark type has predicates, and remember what kind of
2430 -- aspect lead to this predicate (we need this to access
2431 -- the right set of check policies later on).
2432
2433 Set_Has_Predicates (E);
2434
2435 if A_Id = Aspect_Dynamic_Predicate then
2436 Set_Has_Dynamic_Predicate_Aspect (E);
2437
2438 -- If the entity has a dynamic predicate, any inherited
2439 -- static predicate becomes dynamic as well, and the
2440 -- predicate function includes the conjunction of both.
2441
2442 Set_Has_Static_Predicate_Aspect (E, False);
2443
2444 elsif A_Id = Aspect_Static_Predicate then
2445 Set_Has_Static_Predicate_Aspect (E);
2446 end if;
2447
2448 -- If the type is private, indicate that its completion
2449 -- has a freeze node, because that is the one that will
2450 -- be visible at freeze time.
2451
2452 if Is_Private_Type (E) and then Present (Full_View (E)) then
2453 Set_Has_Predicates (Full_View (E));
2454
2455 if A_Id = Aspect_Dynamic_Predicate then
2456 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2457 elsif A_Id = Aspect_Static_Predicate then
2458 Set_Has_Static_Predicate_Aspect (Full_View (E));
2459 end if;
2460
2461 Set_Has_Delayed_Aspects (Full_View (E));
2462 Ensure_Freeze_Node (Full_View (E));
2463 end if;
2464
2465 -- Predicate_Failure
2466
2467 when Aspect_Predicate_Failure =>
2468
2469 -- This aspect applies only to subtypes
2470
2471 if not Is_Type (E) then
2472 Error_Msg_N
2473 ("predicate can only be specified for a subtype",
2474 Aspect);
2475 goto Continue;
2476
2477 elsif Is_Incomplete_Type (E) then
2478 Error_Msg_N
2479 ("predicate cannot apply to incomplete view", Aspect);
2480 goto Continue;
2481 end if;
2482
2483 -- Construct the pragma
2484
2485 Make_Aitem_Pragma
2486 (Pragma_Argument_Associations => New_List (
2487 Make_Pragma_Argument_Association (Sloc (Ent),
2488 Expression => Ent),
2489 Make_Pragma_Argument_Association (Sloc (Expr),
2490 Expression => Relocate_Node (Expr))),
2491 Pragma_Name => Name_Predicate_Failure);
2492
2493 Set_Has_Predicates (E);
2494
2495 -- If the type is private, indicate that its completion
2496 -- has a freeze node, because that is the one that will
2497 -- be visible at freeze time.
2498
2499 if Is_Private_Type (E) and then Present (Full_View (E)) then
2500 Set_Has_Predicates (Full_View (E));
2501 Set_Has_Delayed_Aspects (Full_View (E));
2502 Ensure_Freeze_Node (Full_View (E));
2503 end if;
2504
2505 -- Case 2b: Aspects corresponding to pragmas with two
2506 -- arguments, where the second argument is a local name
2507 -- referring to the entity, and the first argument is the
2508 -- aspect definition expression.
2509
2510 -- Convention
2511
2512 when Aspect_Convention =>
2513 Analyze_Aspect_Convention;
2514 goto Continue;
2515
2516 -- External_Name, Link_Name
2517
2518 when Aspect_External_Name
2519 | Aspect_Link_Name
2520 =>
2521 Analyze_Aspect_External_Link_Name;
2522 goto Continue;
2523
2524 -- CPU, Interrupt_Priority, Priority
2525
2526 -- These three aspects can be specified for a subprogram spec
2527 -- or body, in which case we analyze the expression and export
2528 -- the value of the aspect.
2529
2530 -- Previously, we generated an equivalent pragma for bodies
2531 -- (note that the specs cannot contain these pragmas). The
2532 -- pragma was inserted ahead of local declarations, rather than
2533 -- after the body. This leads to a certain duplication between
2534 -- the processing performed for the aspect and the pragma, but
2535 -- given the straightforward handling required it is simpler
2536 -- to duplicate than to translate the aspect in the spec into
2537 -- a pragma in the declarative part of the body.
2538
2539 when Aspect_CPU
2540 | Aspect_Interrupt_Priority
2541 | Aspect_Priority
2542 =>
2543 if Nkind_In (N, N_Subprogram_Body,
2544 N_Subprogram_Declaration)
2545 then
2546 -- Analyze the aspect expression
2547
2548 Analyze_And_Resolve (Expr, Standard_Integer);
2549
2550 -- Interrupt_Priority aspect not allowed for main
2551 -- subprograms. RM D.1 does not forbid this explicitly,
2552 -- but RM J.15.11(6/3) does not permit pragma
2553 -- Interrupt_Priority for subprograms.
2554
2555 if A_Id = Aspect_Interrupt_Priority then
2556 Error_Msg_N
2557 ("Interrupt_Priority aspect cannot apply to "
2558 & "subprogram", Expr);
2559
2560 -- The expression must be static
2561
2562 elsif not Is_OK_Static_Expression (Expr) then
2563 Flag_Non_Static_Expr
2564 ("aspect requires static expression!", Expr);
2565
2566 -- Check whether this is the main subprogram. Issue a
2567 -- warning only if it is obviously not a main program
2568 -- (when it has parameters or when the subprogram is
2569 -- within a package).
2570
2571 elsif Present (Parameter_Specifications
2572 (Specification (N)))
2573 or else not Is_Compilation_Unit (Defining_Entity (N))
2574 then
2575 -- See RM D.1(14/3) and D.16(12/3)
2576
2577 Error_Msg_N
2578 ("aspect applied to subprogram other than the "
2579 & "main subprogram has no effect??", Expr);
2580
2581 -- Otherwise check in range and export the value
2582
2583 -- For the CPU aspect
2584
2585 elsif A_Id = Aspect_CPU then
2586 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2587
2588 -- Value is correct so we export the value to make
2589 -- it available at execution time.
2590
2591 Set_Main_CPU
2592 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2593
2594 else
2595 Error_Msg_N
2596 ("main subprogram CPU is out of range", Expr);
2597 end if;
2598
2599 -- For the Priority aspect
2600
2601 elsif A_Id = Aspect_Priority then
2602 if Is_In_Range (Expr, RTE (RE_Priority)) then
2603
2604 -- Value is correct so we export the value to make
2605 -- it available at execution time.
2606
2607 Set_Main_Priority
2608 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2609
2610 -- Ignore pragma if Relaxed_RM_Semantics to support
2611 -- other targets/non GNAT compilers.
2612
2613 elsif not Relaxed_RM_Semantics then
2614 Error_Msg_N
2615 ("main subprogram priority is out of range",
2616 Expr);
2617 end if;
2618 end if;
2619
2620 -- Load an arbitrary entity from System.Tasking.Stages
2621 -- or System.Tasking.Restricted.Stages (depending on
2622 -- the supported profile) to make sure that one of these
2623 -- packages is implicitly with'ed, since we need to have
2624 -- the tasking run time active for the pragma Priority to
2625 -- have any effect. Previously we with'ed the package
2626 -- System.Tasking, but this package does not trigger the
2627 -- required initialization of the run-time library.
2628
2629 declare
2630 Discard : Entity_Id;
2631 begin
2632 if Restricted_Profile then
2633 Discard := RTE (RE_Activate_Restricted_Tasks);
2634 else
2635 Discard := RTE (RE_Activate_Tasks);
2636 end if;
2637 end;
2638
2639 -- Handling for these aspects in subprograms is complete
2640
2641 goto Continue;
2642
2643 -- For task and protected types pass the aspect as an
2644 -- attribute.
2645
2646 else
2647 Aitem :=
2648 Make_Attribute_Definition_Clause (Loc,
2649 Name => Ent,
2650 Chars => Chars (Id),
2651 Expression => Relocate_Node (Expr));
2652 end if;
2653
2654 -- Warnings
2655
2656 when Aspect_Warnings =>
2657 Make_Aitem_Pragma
2658 (Pragma_Argument_Associations => New_List (
2659 Make_Pragma_Argument_Association (Sloc (Expr),
2660 Expression => Relocate_Node (Expr)),
2661 Make_Pragma_Argument_Association (Loc,
2662 Expression => New_Occurrence_Of (E, Loc))),
2663 Pragma_Name => Chars (Id));
2664
2665 Decorate (Aspect, Aitem);
2666 Insert_Pragma (Aitem);
2667 goto Continue;
2668
2669 -- Case 2c: Aspects corresponding to pragmas with three
2670 -- arguments.
2671
2672 -- Invariant aspects have a first argument that references the
2673 -- entity, a second argument that is the expression and a third
2674 -- argument that is an appropriate message.
2675
2676 -- Invariant, Type_Invariant
2677
2678 when Aspect_Invariant
2679 | Aspect_Type_Invariant
2680 =>
2681 -- Analysis of the pragma will verify placement legality:
2682 -- an invariant must apply to a private type, or appear in
2683 -- the private part of a spec and apply to a completion.
2684
2685 Make_Aitem_Pragma
2686 (Pragma_Argument_Associations => New_List (
2687 Make_Pragma_Argument_Association (Sloc (Ent),
2688 Expression => Ent),
2689 Make_Pragma_Argument_Association (Sloc (Expr),
2690 Expression => Relocate_Node (Expr))),
2691 Pragma_Name => Name_Invariant);
2692
2693 -- Add message unless exception messages are suppressed
2694
2695 if not Opt.Exception_Locations_Suppressed then
2696 Append_To (Pragma_Argument_Associations (Aitem),
2697 Make_Pragma_Argument_Association (Eloc,
2698 Chars => Name_Message,
2699 Expression =>
2700 Make_String_Literal (Eloc,
2701 Strval => "failed invariant from "
2702 & Build_Location_String (Eloc))));
2703 end if;
2704
2705 -- For Invariant case, insert immediately after the entity
2706 -- declaration. We do not have to worry about delay issues
2707 -- since the pragma processing takes care of this.
2708
2709 Delay_Required := False;
2710
2711 -- Case 2d : Aspects that correspond to a pragma with one
2712 -- argument.
2713
2714 -- Abstract_State
2715
2716 -- Aspect Abstract_State introduces implicit declarations for
2717 -- all state abstraction entities it defines. To emulate this
2718 -- behavior, insert the pragma at the beginning of the visible
2719 -- declarations of the related package so that it is analyzed
2720 -- immediately.
2721
2722 when Aspect_Abstract_State => Abstract_State : declare
2723 Context : Node_Id := N;
2724
2725 begin
2726 -- When aspect Abstract_State appears on a generic package,
2727 -- it is propageted to the package instance. The context in
2728 -- this case is the instance spec.
2729
2730 if Nkind (Context) = N_Package_Instantiation then
2731 Context := Instance_Spec (Context);
2732 end if;
2733
2734 if Nkind_In (Context, N_Generic_Package_Declaration,
2735 N_Package_Declaration)
2736 then
2737 Make_Aitem_Pragma
2738 (Pragma_Argument_Associations => New_List (
2739 Make_Pragma_Argument_Association (Loc,
2740 Expression => Relocate_Node (Expr))),
2741 Pragma_Name => Name_Abstract_State);
2742
2743 Decorate (Aspect, Aitem);
2744 Insert_Pragma
2745 (Prag => Aitem,
2746 Is_Instance =>
2747 Is_Generic_Instance (Defining_Entity (Context)));
2748
2749 else
2750 Error_Msg_NE
2751 ("aspect & must apply to a package declaration",
2752 Aspect, Id);
2753 end if;
2754
2755 goto Continue;
2756 end Abstract_State;
2757
2758 -- Aspect Async_Readers is never delayed because it is
2759 -- equivalent to a source pragma which appears after the
2760 -- related object declaration.
2761
2762 when Aspect_Async_Readers =>
2763 Make_Aitem_Pragma
2764 (Pragma_Argument_Associations => New_List (
2765 Make_Pragma_Argument_Association (Loc,
2766 Expression => Relocate_Node (Expr))),
2767 Pragma_Name => Name_Async_Readers);
2768
2769 Decorate (Aspect, Aitem);
2770 Insert_Pragma (Aitem);
2771 goto Continue;
2772
2773 -- Aspect Async_Writers is never delayed because it is
2774 -- equivalent to a source pragma which appears after the
2775 -- related object declaration.
2776
2777 when Aspect_Async_Writers =>
2778 Make_Aitem_Pragma
2779 (Pragma_Argument_Associations => New_List (
2780 Make_Pragma_Argument_Association (Loc,
2781 Expression => Relocate_Node (Expr))),
2782 Pragma_Name => Name_Async_Writers);
2783
2784 Decorate (Aspect, Aitem);
2785 Insert_Pragma (Aitem);
2786 goto Continue;
2787
2788 -- Aspect Constant_After_Elaboration is never delayed because
2789 -- it is equivalent to a source pragma which appears after the
2790 -- related object declaration.
2791
2792 when Aspect_Constant_After_Elaboration =>
2793 Make_Aitem_Pragma
2794 (Pragma_Argument_Associations => New_List (
2795 Make_Pragma_Argument_Association (Loc,
2796 Expression => Relocate_Node (Expr))),
2797 Pragma_Name =>
2798 Name_Constant_After_Elaboration);
2799
2800 Decorate (Aspect, Aitem);
2801 Insert_Pragma (Aitem);
2802 goto Continue;
2803
2804 -- Aspect Default_Internal_Condition is never delayed because
2805 -- it is equivalent to a source pragma which appears after the
2806 -- related private type. To deal with forward references, the
2807 -- generated pragma is stored in the rep chain of the related
2808 -- private type as types do not carry contracts. The pragma is
2809 -- wrapped inside of a procedure at the freeze point of the
2810 -- private type's full view.
2811
2812 when Aspect_Default_Initial_Condition =>
2813 Make_Aitem_Pragma
2814 (Pragma_Argument_Associations => New_List (
2815 Make_Pragma_Argument_Association (Loc,
2816 Expression => Relocate_Node (Expr))),
2817 Pragma_Name =>
2818 Name_Default_Initial_Condition);
2819
2820 Decorate (Aspect, Aitem);
2821 Insert_Pragma (Aitem);
2822 goto Continue;
2823
2824 -- Default_Storage_Pool
2825
2826 when Aspect_Default_Storage_Pool =>
2827 Make_Aitem_Pragma
2828 (Pragma_Argument_Associations => New_List (
2829 Make_Pragma_Argument_Association (Loc,
2830 Expression => Relocate_Node (Expr))),
2831 Pragma_Name =>
2832 Name_Default_Storage_Pool);
2833
2834 Decorate (Aspect, Aitem);
2835 Insert_Pragma (Aitem);
2836 goto Continue;
2837
2838 -- Depends
2839
2840 -- Aspect Depends is never delayed because it is equivalent to
2841 -- a source pragma which appears after the related subprogram.
2842 -- To deal with forward references, the generated pragma is
2843 -- stored in the contract of the related subprogram and later
2844 -- analyzed at the end of the declarative region. See routine
2845 -- Analyze_Depends_In_Decl_Part for details.
2846
2847 when Aspect_Depends =>
2848 Make_Aitem_Pragma
2849 (Pragma_Argument_Associations => New_List (
2850 Make_Pragma_Argument_Association (Loc,
2851 Expression => Relocate_Node (Expr))),
2852 Pragma_Name => Name_Depends);
2853
2854 Decorate (Aspect, Aitem);
2855 Insert_Pragma (Aitem);
2856 goto Continue;
2857
2858 -- Aspect Effective_Reads is never delayed because it is
2859 -- equivalent to a source pragma which appears after the
2860 -- related object declaration.
2861
2862 when Aspect_Effective_Reads =>
2863 Make_Aitem_Pragma
2864 (Pragma_Argument_Associations => New_List (
2865 Make_Pragma_Argument_Association (Loc,
2866 Expression => Relocate_Node (Expr))),
2867 Pragma_Name => Name_Effective_Reads);
2868
2869 Decorate (Aspect, Aitem);
2870 Insert_Pragma (Aitem);
2871 goto Continue;
2872
2873 -- Aspect Effective_Writes is never delayed because it is
2874 -- equivalent to a source pragma which appears after the
2875 -- related object declaration.
2876
2877 when Aspect_Effective_Writes =>
2878 Make_Aitem_Pragma
2879 (Pragma_Argument_Associations => New_List (
2880 Make_Pragma_Argument_Association (Loc,
2881 Expression => Relocate_Node (Expr))),
2882 Pragma_Name => Name_Effective_Writes);
2883
2884 Decorate (Aspect, Aitem);
2885 Insert_Pragma (Aitem);
2886 goto Continue;
2887
2888 -- Aspect Extensions_Visible is never delayed because it is
2889 -- equivalent to a source pragma which appears after the
2890 -- related subprogram.
2891
2892 when Aspect_Extensions_Visible =>
2893 Make_Aitem_Pragma
2894 (Pragma_Argument_Associations => New_List (
2895 Make_Pragma_Argument_Association (Loc,
2896 Expression => Relocate_Node (Expr))),
2897 Pragma_Name => Name_Extensions_Visible);
2898
2899 Decorate (Aspect, Aitem);
2900 Insert_Pragma (Aitem);
2901 goto Continue;
2902
2903 -- Aspect Ghost is never delayed because it is equivalent to a
2904 -- source pragma which appears at the top of [generic] package
2905 -- declarations or after an object, a [generic] subprogram, or
2906 -- a type declaration.
2907
2908 when Aspect_Ghost =>
2909 Make_Aitem_Pragma
2910 (Pragma_Argument_Associations => New_List (
2911 Make_Pragma_Argument_Association (Loc,
2912 Expression => Relocate_Node (Expr))),
2913 Pragma_Name => Name_Ghost);
2914
2915 Decorate (Aspect, Aitem);
2916 Insert_Pragma (Aitem);
2917 goto Continue;
2918
2919 -- Global
2920
2921 -- Aspect Global is never delayed because it is equivalent to
2922 -- a source pragma which appears after the related subprogram.
2923 -- To deal with forward references, the generated pragma is
2924 -- stored in the contract of the related subprogram and later
2925 -- analyzed at the end of the declarative region. See routine
2926 -- Analyze_Global_In_Decl_Part for details.
2927
2928 when Aspect_Global =>
2929 Make_Aitem_Pragma
2930 (Pragma_Argument_Associations => New_List (
2931 Make_Pragma_Argument_Association (Loc,
2932 Expression => Relocate_Node (Expr))),
2933 Pragma_Name => Name_Global);
2934
2935 Decorate (Aspect, Aitem);
2936 Insert_Pragma (Aitem);
2937 goto Continue;
2938
2939 -- Initial_Condition
2940
2941 -- Aspect Initial_Condition is never delayed because it is
2942 -- equivalent to a source pragma which appears after the
2943 -- related package. To deal with forward references, the
2944 -- generated pragma is stored in the contract of the related
2945 -- package and later analyzed at the end of the declarative
2946 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2947 -- for details.
2948
2949 when Aspect_Initial_Condition => Initial_Condition : declare
2950 Context : Node_Id := N;
2951
2952 begin
2953 -- When aspect Initial_Condition appears on a generic
2954 -- package, it is propageted to the package instance. The
2955 -- context in this case is the instance spec.
2956
2957 if Nkind (Context) = N_Package_Instantiation then
2958 Context := Instance_Spec (Context);
2959 end if;
2960
2961 if Nkind_In (Context, N_Generic_Package_Declaration,
2962 N_Package_Declaration)
2963 then
2964 Make_Aitem_Pragma
2965 (Pragma_Argument_Associations => New_List (
2966 Make_Pragma_Argument_Association (Loc,
2967 Expression => Relocate_Node (Expr))),
2968 Pragma_Name =>
2969 Name_Initial_Condition);
2970
2971 Decorate (Aspect, Aitem);
2972 Insert_Pragma
2973 (Prag => Aitem,
2974 Is_Instance =>
2975 Is_Generic_Instance (Defining_Entity (Context)));
2976
2977 -- Otherwise the context is illegal
2978
2979 else
2980 Error_Msg_NE
2981 ("aspect & must apply to a package declaration",
2982 Aspect, Id);
2983 end if;
2984
2985 goto Continue;
2986 end Initial_Condition;
2987
2988 -- Initializes
2989
2990 -- Aspect Initializes is never delayed because it is equivalent
2991 -- to a source pragma appearing after the related package. To
2992 -- deal with forward references, the generated pragma is stored
2993 -- in the contract of the related package and later analyzed at
2994 -- the end of the declarative region. For details, see routine
2995 -- Analyze_Initializes_In_Decl_Part.
2996
2997 when Aspect_Initializes => Initializes : declare
2998 Context : Node_Id := N;
2999
3000 begin
3001 -- When aspect Initializes appears on a generic package,
3002 -- it is propageted to the package instance. The context
3003 -- in this case is the instance spec.
3004
3005 if Nkind (Context) = N_Package_Instantiation then
3006 Context := Instance_Spec (Context);
3007 end if;
3008
3009 if Nkind_In (Context, N_Generic_Package_Declaration,
3010 N_Package_Declaration)
3011 then
3012 Make_Aitem_Pragma
3013 (Pragma_Argument_Associations => New_List (
3014 Make_Pragma_Argument_Association (Loc,
3015 Expression => Relocate_Node (Expr))),
3016 Pragma_Name => Name_Initializes);
3017
3018 Decorate (Aspect, Aitem);
3019 Insert_Pragma
3020 (Prag => Aitem,
3021 Is_Instance =>
3022 Is_Generic_Instance (Defining_Entity (Context)));
3023
3024 -- Otherwise the context is illegal
3025
3026 else
3027 Error_Msg_NE
3028 ("aspect & must apply to a package declaration",
3029 Aspect, Id);
3030 end if;
3031
3032 goto Continue;
3033 end Initializes;
3034
3035 -- Max_Entry_Queue_Depth
3036
3037 when Aspect_Max_Entry_Queue_Depth =>
3038 Make_Aitem_Pragma
3039 (Pragma_Argument_Associations => New_List (
3040 Make_Pragma_Argument_Association (Loc,
3041 Expression => Relocate_Node (Expr))),
3042 Pragma_Name => Name_Max_Entry_Queue_Depth);
3043
3044 Decorate (Aspect, Aitem);
3045 Insert_Pragma (Aitem);
3046 goto Continue;
3047
3048 -- Max_Entry_Queue_Length
3049
3050 when Aspect_Max_Entry_Queue_Length =>
3051 Make_Aitem_Pragma
3052 (Pragma_Argument_Associations => New_List (
3053 Make_Pragma_Argument_Association (Loc,
3054 Expression => Relocate_Node (Expr))),
3055 Pragma_Name => Name_Max_Entry_Queue_Length);
3056
3057 Decorate (Aspect, Aitem);
3058 Insert_Pragma (Aitem);
3059 goto Continue;
3060
3061 -- Max_Queue_Length
3062
3063 when Aspect_Max_Queue_Length =>
3064 Make_Aitem_Pragma
3065 (Pragma_Argument_Associations => New_List (
3066 Make_Pragma_Argument_Association (Loc,
3067 Expression => Relocate_Node (Expr))),
3068 Pragma_Name => Name_Max_Queue_Length);
3069
3070 Decorate (Aspect, Aitem);
3071 Insert_Pragma (Aitem);
3072 goto Continue;
3073
3074 -- Aspect No_Caching is never delayed because it is equivalent
3075 -- to a source pragma which appears after the related object
3076 -- declaration.
3077
3078 when Aspect_No_Caching =>
3079 Make_Aitem_Pragma
3080 (Pragma_Argument_Associations => New_List (
3081 Make_Pragma_Argument_Association (Loc,
3082 Expression => Relocate_Node (Expr))),
3083 Pragma_Name => Name_No_Caching);
3084
3085 Decorate (Aspect, Aitem);
3086 Insert_Pragma (Aitem);
3087 goto Continue;
3088
3089 -- Obsolescent
3090
3091 when Aspect_Obsolescent => declare
3092 Args : List_Id;
3093
3094 begin
3095 if No (Expr) then
3096 Args := No_List;
3097 else
3098 Args := New_List (
3099 Make_Pragma_Argument_Association (Sloc (Expr),
3100 Expression => Relocate_Node (Expr)));
3101 end if;
3102
3103 Make_Aitem_Pragma
3104 (Pragma_Argument_Associations => Args,
3105 Pragma_Name => Chars (Id));
3106 end;
3107
3108 -- Part_Of
3109
3110 when Aspect_Part_Of =>
3111 if Nkind_In (N, N_Object_Declaration,
3112 N_Package_Instantiation)
3113 or else Is_Single_Concurrent_Type_Declaration (N)
3114 then
3115 Make_Aitem_Pragma
3116 (Pragma_Argument_Associations => New_List (
3117 Make_Pragma_Argument_Association (Loc,
3118 Expression => Relocate_Node (Expr))),
3119 Pragma_Name => Name_Part_Of);
3120
3121 Decorate (Aspect, Aitem);
3122 Insert_Pragma (Aitem);
3123
3124 else
3125 Error_Msg_NE
3126 ("aspect & must apply to package instantiation, "
3127 & "object, single protected type or single task type",
3128 Aspect, Id);
3129 end if;
3130
3131 goto Continue;
3132
3133 -- SPARK_Mode
3134
3135 when Aspect_SPARK_Mode =>
3136 Make_Aitem_Pragma
3137 (Pragma_Argument_Associations => New_List (
3138 Make_Pragma_Argument_Association (Loc,
3139 Expression => Relocate_Node (Expr))),
3140 Pragma_Name => Name_SPARK_Mode);
3141
3142 Decorate (Aspect, Aitem);
3143 Insert_Pragma (Aitem);
3144 goto Continue;
3145
3146 -- Refined_Depends
3147
3148 -- Aspect Refined_Depends is never delayed because it is
3149 -- equivalent to a source pragma which appears in the
3150 -- declarations of the related subprogram body. To deal with
3151 -- forward references, the generated pragma is stored in the
3152 -- contract of the related subprogram body and later analyzed
3153 -- at the end of the declarative region. For details, see
3154 -- routine Analyze_Refined_Depends_In_Decl_Part.
3155
3156 when Aspect_Refined_Depends =>
3157 Make_Aitem_Pragma
3158 (Pragma_Argument_Associations => New_List (
3159 Make_Pragma_Argument_Association (Loc,
3160 Expression => Relocate_Node (Expr))),
3161 Pragma_Name => Name_Refined_Depends);
3162
3163 Decorate (Aspect, Aitem);
3164 Insert_Pragma (Aitem);
3165 goto Continue;
3166
3167 -- Refined_Global
3168
3169 -- Aspect Refined_Global is never delayed because it is
3170 -- equivalent to a source pragma which appears in the
3171 -- declarations of the related subprogram body. To deal with
3172 -- forward references, the generated pragma is stored in the
3173 -- contract of the related subprogram body and later analyzed
3174 -- at the end of the declarative region. For details, see
3175 -- routine Analyze_Refined_Global_In_Decl_Part.
3176
3177 when Aspect_Refined_Global =>
3178 Make_Aitem_Pragma
3179 (Pragma_Argument_Associations => New_List (
3180 Make_Pragma_Argument_Association (Loc,
3181 Expression => Relocate_Node (Expr))),
3182 Pragma_Name => Name_Refined_Global);
3183
3184 Decorate (Aspect, Aitem);
3185 Insert_Pragma (Aitem);
3186 goto Continue;
3187
3188 -- Refined_Post
3189
3190 when Aspect_Refined_Post =>
3191 Make_Aitem_Pragma
3192 (Pragma_Argument_Associations => New_List (
3193 Make_Pragma_Argument_Association (Loc,
3194 Expression => Relocate_Node (Expr))),
3195 Pragma_Name => Name_Refined_Post);
3196
3197 Decorate (Aspect, Aitem);
3198 Insert_Pragma (Aitem);
3199 goto Continue;
3200
3201 -- Refined_State
3202
3203 when Aspect_Refined_State =>
3204
3205 -- The corresponding pragma for Refined_State is inserted in
3206 -- the declarations of the related package body. This action
3207 -- synchronizes both the source and from-aspect versions of
3208 -- the pragma.
3209
3210 if Nkind (N) = N_Package_Body then
3211 Make_Aitem_Pragma
3212 (Pragma_Argument_Associations => New_List (
3213 Make_Pragma_Argument_Association (Loc,
3214 Expression => Relocate_Node (Expr))),
3215 Pragma_Name => Name_Refined_State);
3216
3217 Decorate (Aspect, Aitem);
3218 Insert_Pragma (Aitem);
3219
3220 -- Otherwise the context is illegal
3221
3222 else
3223 Error_Msg_NE
3224 ("aspect & must apply to a package body", Aspect, Id);
3225 end if;
3226
3227 goto Continue;
3228
3229 -- Relative_Deadline
3230
3231 when Aspect_Relative_Deadline =>
3232 Make_Aitem_Pragma
3233 (Pragma_Argument_Associations => New_List (
3234 Make_Pragma_Argument_Association (Loc,
3235 Expression => Relocate_Node (Expr))),
3236 Pragma_Name => Name_Relative_Deadline);
3237
3238 -- If the aspect applies to a task, the corresponding pragma
3239 -- must appear within its declarations, not after.
3240
3241 if Nkind (N) = N_Task_Type_Declaration then
3242 declare
3243 Def : Node_Id;
3244 V : List_Id;
3245
3246 begin
3247 if No (Task_Definition (N)) then
3248 Set_Task_Definition (N,
3249 Make_Task_Definition (Loc,
3250 Visible_Declarations => New_List,
3251 End_Label => Empty));
3252 end if;
3253
3254 Def := Task_Definition (N);
3255 V := Visible_Declarations (Def);
3256 if not Is_Empty_List (V) then
3257 Insert_Before (First (V), Aitem);
3258
3259 else
3260 Set_Visible_Declarations (Def, New_List (Aitem));
3261 end if;
3262
3263 goto Continue;
3264 end;
3265 end if;
3266
3267 -- Secondary_Stack_Size
3268
3269 -- Aspect Secondary_Stack_Size needs to be converted into a
3270 -- pragma for two reasons: the attribute is not analyzed until
3271 -- after the expansion of the task type declaration and the
3272 -- attribute does not have visibility on the discriminant.
3273
3274 when Aspect_Secondary_Stack_Size =>
3275 Make_Aitem_Pragma
3276 (Pragma_Argument_Associations => New_List (
3277 Make_Pragma_Argument_Association (Loc,
3278 Expression => Relocate_Node (Expr))),
3279 Pragma_Name =>
3280 Name_Secondary_Stack_Size);
3281
3282 Decorate (Aspect, Aitem);
3283 Insert_Pragma (Aitem);
3284 goto Continue;
3285
3286 -- Volatile_Function
3287
3288 -- Aspect Volatile_Function is never delayed because it is
3289 -- equivalent to a source pragma which appears after the
3290 -- related subprogram.
3291
3292 when Aspect_Volatile_Function =>
3293 Make_Aitem_Pragma
3294 (Pragma_Argument_Associations => New_List (
3295 Make_Pragma_Argument_Association (Loc,
3296 Expression => Relocate_Node (Expr))),
3297 Pragma_Name => Name_Volatile_Function);
3298
3299 Decorate (Aspect, Aitem);
3300 Insert_Pragma (Aitem);
3301 goto Continue;
3302
3303 -- Case 2e: Annotate aspect
3304
3305 when Aspect_Annotate =>
3306 declare
3307 Args : List_Id;
3308 Pargs : List_Id;
3309 Arg : Node_Id;
3310
3311 begin
3312 -- The argument can be a single identifier
3313
3314 if Nkind (Expr) = N_Identifier then
3315
3316 -- One level of parens is allowed
3317
3318 if Paren_Count (Expr) > 1 then
3319 Error_Msg_F ("extra parentheses ignored", Expr);
3320 end if;
3321
3322 Set_Paren_Count (Expr, 0);
3323
3324 -- Add the single item to the list
3325
3326 Args := New_List (Expr);
3327
3328 -- Otherwise we must have an aggregate
3329
3330 elsif Nkind (Expr) = N_Aggregate then
3331
3332 -- Must be positional
3333
3334 if Present (Component_Associations (Expr)) then
3335 Error_Msg_F
3336 ("purely positional aggregate required", Expr);
3337 goto Continue;
3338 end if;
3339
3340 -- Must not be parenthesized
3341
3342 if Paren_Count (Expr) /= 0 then
3343 Error_Msg_F ("extra parentheses ignored", Expr);
3344 end if;
3345
3346 -- List of arguments is list of aggregate expressions
3347
3348 Args := Expressions (Expr);
3349
3350 -- Anything else is illegal
3351
3352 else
3353 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3354 goto Continue;
3355 end if;
3356
3357 -- Prepare pragma arguments
3358
3359 Pargs := New_List;
3360 Arg := First (Args);
3361 while Present (Arg) loop
3362 Append_To (Pargs,
3363 Make_Pragma_Argument_Association (Sloc (Arg),
3364 Expression => Relocate_Node (Arg)));
3365 Next (Arg);
3366 end loop;
3367
3368 Append_To (Pargs,
3369 Make_Pragma_Argument_Association (Sloc (Ent),
3370 Chars => Name_Entity,
3371 Expression => Ent));
3372
3373 Make_Aitem_Pragma
3374 (Pragma_Argument_Associations => Pargs,
3375 Pragma_Name => Name_Annotate);
3376 end;
3377
3378 -- Case 3 : Aspects that don't correspond to pragma/attribute
3379 -- definition clause.
3380
3381 -- Case 3a: The aspects listed below don't correspond to
3382 -- pragmas/attributes but do require delayed analysis.
3383
3384 -- Default_Value can only apply to a scalar type
3385
3386 when Aspect_Default_Value =>
3387 if not Is_Scalar_Type (E) then
3388 Error_Msg_N
3389 ("aspect Default_Value must apply to a scalar type", N);
3390 end if;
3391
3392 Aitem := Empty;
3393
3394 -- Default_Component_Value can only apply to an array type
3395 -- with scalar components.
3396
3397 when Aspect_Default_Component_Value =>
3398 if not (Is_Array_Type (E)
3399 and then Is_Scalar_Type (Component_Type (E)))
3400 then
3401 Error_Msg_N
3402 ("aspect Default_Component_Value can only apply to an "
3403 & "array of scalar components", N);
3404 end if;
3405
3406 Aitem := Empty;
3407
3408 -- Case 3b: The aspects listed below don't correspond to
3409 -- pragmas/attributes and don't need delayed analysis.
3410
3411 -- Implicit_Dereference
3412
3413 -- For Implicit_Dereference, External_Name and Link_Name, only
3414 -- the legality checks are done during the analysis, thus no
3415 -- delay is required.
3416
3417 when Aspect_Implicit_Dereference =>
3418 Analyze_Aspect_Implicit_Dereference;
3419 goto Continue;
3420
3421 -- Dimension
3422
3423 when Aspect_Dimension =>
3424 Analyze_Aspect_Dimension (N, Id, Expr);
3425 goto Continue;
3426
3427 -- Dimension_System
3428
3429 when Aspect_Dimension_System =>
3430 Analyze_Aspect_Dimension_System (N, Id, Expr);
3431 goto Continue;
3432
3433 -- Case 4: Aspects requiring special handling
3434
3435 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3436 -- pragmas take care of the delay.
3437
3438 -- Pre/Post
3439
3440 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3441 -- with a first argument that is the expression, and a second
3442 -- argument that is an informative message if the test fails.
3443 -- This is inserted right after the declaration, to get the
3444 -- required pragma placement. The processing for the pragmas
3445 -- takes care of the required delay.
3446
3447 when Pre_Post_Aspects => Pre_Post : declare
3448 Pname : Name_Id;
3449
3450 begin
3451 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
3452 Pname := Name_Precondition;
3453 else
3454 Pname := Name_Postcondition;
3455 end if;
3456
3457 -- Check that the class-wide predicate cannot be applied to
3458 -- an operation of a synchronized type. AI12-0182 forbids
3459 -- these altogether, while earlier language semantics made
3460 -- them legal on tagged synchronized types.
3461
3462 -- Other legality checks are performed when analyzing the
3463 -- contract of the operation.
3464
3465 if Class_Present (Aspect)
3466 and then Is_Concurrent_Type (Current_Scope)
3467 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3468 then
3469 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3470 Error_Msg_N
3471 ("aspect % can only be specified for a primitive "
3472 & "operation of a tagged type", Aspect);
3473
3474 goto Continue;
3475 end if;
3476
3477 -- If the expressions is of the form A and then B, then
3478 -- we generate separate Pre/Post aspects for the separate
3479 -- clauses. Since we allow multiple pragmas, there is no
3480 -- problem in allowing multiple Pre/Post aspects internally.
3481 -- These should be treated in reverse order (B first and
3482 -- A second) since they are later inserted just after N in
3483 -- the order they are treated. This way, the pragma for A
3484 -- ends up preceding the pragma for B, which may have an
3485 -- importance for the error raised (either constraint error
3486 -- or precondition error).
3487
3488 -- We do not do this for Pre'Class, since we have to put
3489 -- these conditions together in a complex OR expression.
3490
3491 -- We do not do this in ASIS mode, as ASIS relies on the
3492 -- original node representing the complete expression, when
3493 -- retrieving it through the source aspect table. Also, we
3494 -- don't do this in GNATprove mode, because it brings no
3495 -- benefit for proof and causes annoynace for flow analysis,
3496 -- which prefers to be as close to the original source code
3497 -- as possible. Also we don't do this when analyzing generic
3498 -- units since it causes spurious visibility errors in the
3499 -- preanalysis of instantiations.
3500
3501 if not (ASIS_Mode or GNATprove_Mode)
3502 and then (Pname = Name_Postcondition
3503 or else not Class_Present (Aspect))
3504 and then not Inside_A_Generic
3505 then
3506 while Nkind (Expr) = N_And_Then loop
3507 Insert_After (Aspect,
3508 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
3509 Identifier => Identifier (Aspect),
3510 Expression => Relocate_Node (Left_Opnd (Expr)),
3511 Class_Present => Class_Present (Aspect),
3512 Split_PPC => True));
3513 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
3514 Eloc := Sloc (Expr);
3515 end loop;
3516 end if;
3517
3518 -- Build the precondition/postcondition pragma
3519
3520 -- We use Relocate_Node here rather than New_Copy_Tree
3521 -- because subsequent visibility analysis of the aspect
3522 -- depends on this sharing. This should be cleaned up???
3523
3524 -- If the context is generic or involves ASIS, we want
3525 -- to preserve the original tree, and simply share it
3526 -- between aspect and generated attribute. This parallels
3527 -- what is done in sem_prag.adb (see Get_Argument).
3528
3529 declare
3530 New_Expr : Node_Id;
3531
3532 begin
3533 if ASIS_Mode or else Inside_A_Generic then
3534 New_Expr := Expr;
3535 else
3536 New_Expr := Relocate_Node (Expr);
3537 end if;
3538
3539 Make_Aitem_Pragma
3540 (Pragma_Argument_Associations => New_List (
3541 Make_Pragma_Argument_Association (Eloc,
3542 Chars => Name_Check,
3543 Expression => New_Expr)),
3544 Pragma_Name => Pname);
3545 end;
3546
3547 -- Add message unless exception messages are suppressed
3548
3549 if not Opt.Exception_Locations_Suppressed then
3550 Append_To (Pragma_Argument_Associations (Aitem),
3551 Make_Pragma_Argument_Association (Eloc,
3552 Chars => Name_Message,
3553 Expression =>
3554 Make_String_Literal (Eloc,
3555 Strval => "failed "
3556 & Get_Name_String (Pname)
3557 & " from "
3558 & Build_Location_String (Eloc))));
3559 end if;
3560
3561 Set_Is_Delayed_Aspect (Aspect);
3562
3563 -- For Pre/Post cases, insert immediately after the entity
3564 -- declaration, since that is the required pragma placement.
3565 -- Note that for these aspects, we do not have to worry
3566 -- about delay issues, since the pragmas themselves deal
3567 -- with delay of visibility for the expression analysis.
3568
3569 Insert_Pragma (Aitem);
3570
3571 goto Continue;
3572 end Pre_Post;
3573
3574 -- Test_Case
3575
3576 when Aspect_Test_Case => Test_Case : declare
3577 Args : List_Id;
3578 Comp_Expr : Node_Id;
3579 Comp_Assn : Node_Id;
3580 New_Expr : Node_Id;
3581
3582 begin
3583 Args := New_List;
3584
3585 if Nkind (Parent (N)) = N_Compilation_Unit then
3586 Error_Msg_Name_1 := Nam;
3587 Error_Msg_N ("incorrect placement of aspect `%`", E);
3588 goto Continue;
3589 end if;
3590
3591 if Nkind (Expr) /= N_Aggregate then
3592 Error_Msg_Name_1 := Nam;
3593 Error_Msg_NE
3594 ("wrong syntax for aspect `%` for &", Id, E);
3595 goto Continue;
3596 end if;
3597
3598 -- Make pragma expressions refer to the original aspect
3599 -- expressions through the Original_Node link. This is used
3600 -- in semantic analysis for ASIS mode, so that the original
3601 -- expression also gets analyzed.
3602
3603 Comp_Expr := First (Expressions (Expr));
3604 while Present (Comp_Expr) loop
3605 New_Expr := Relocate_Node (Comp_Expr);
3606 Append_To (Args,
3607 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3608 Expression => New_Expr));
3609 Next (Comp_Expr);
3610 end loop;
3611
3612 Comp_Assn := First (Component_Associations (Expr));
3613 while Present (Comp_Assn) loop
3614 if List_Length (Choices (Comp_Assn)) /= 1
3615 or else
3616 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3617 then
3618 Error_Msg_Name_1 := Nam;
3619 Error_Msg_NE
3620 ("wrong syntax for aspect `%` for &", Id, E);
3621 goto Continue;
3622 end if;
3623
3624 Append_To (Args,
3625 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3626 Chars => Chars (First (Choices (Comp_Assn))),
3627 Expression =>
3628 Relocate_Node (Expression (Comp_Assn))));
3629 Next (Comp_Assn);
3630 end loop;
3631
3632 -- Build the test-case pragma
3633
3634 Make_Aitem_Pragma
3635 (Pragma_Argument_Associations => Args,
3636 Pragma_Name => Nam);
3637 end Test_Case;
3638
3639 -- Contract_Cases
3640
3641 when Aspect_Contract_Cases =>
3642 Make_Aitem_Pragma
3643 (Pragma_Argument_Associations => New_List (
3644 Make_Pragma_Argument_Association (Loc,
3645 Expression => Relocate_Node (Expr))),
3646 Pragma_Name => Nam);
3647
3648 Decorate (Aspect, Aitem);
3649 Insert_Pragma (Aitem);
3650 goto Continue;
3651
3652 -- Case 5: Special handling for aspects with an optional
3653 -- boolean argument.
3654
3655 -- In the delayed case, the corresponding pragma cannot be
3656 -- generated yet because the evaluation of the boolean needs
3657 -- to be delayed till the freeze point.
3658
3659 when Boolean_Aspects
3660 | Library_Unit_Aspects
3661 =>
3662 Set_Is_Boolean_Aspect (Aspect);
3663
3664 -- Lock_Free aspect only apply to protected objects
3665
3666 if A_Id = Aspect_Lock_Free then
3667 if Ekind (E) /= E_Protected_Type then
3668 Error_Msg_Name_1 := Nam;
3669 Error_Msg_N
3670 ("aspect % only applies to a protected object",
3671 Aspect);
3672
3673 else
3674 -- Set the Uses_Lock_Free flag to True if there is no
3675 -- expression or if the expression is True. The
3676 -- evaluation of this aspect should be delayed to the
3677 -- freeze point (why???)
3678
3679 if No (Expr)
3680 or else Is_True (Static_Boolean (Expr))
3681 then
3682 Set_Uses_Lock_Free (E);
3683 end if;
3684
3685 Record_Rep_Item (E, Aspect);
3686 end if;
3687
3688 goto Continue;
3689
3690 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3691 Analyze_Aspect_Export_Import;
3692
3693 -- Disable_Controlled
3694
3695 elsif A_Id = Aspect_Disable_Controlled then
3696 Analyze_Aspect_Disable_Controlled;
3697 goto Continue;
3698 end if;
3699
3700 -- Library unit aspects require special handling in the case
3701 -- of a package declaration, the pragma needs to be inserted
3702 -- in the list of declarations for the associated package.
3703 -- There is no issue of visibility delay for these aspects.
3704
3705 if A_Id in Library_Unit_Aspects
3706 and then
3707 Nkind_In (N, N_Package_Declaration,
3708 N_Generic_Package_Declaration)
3709 and then Nkind (Parent (N)) /= N_Compilation_Unit
3710
3711 -- Aspect is legal on a local instantiation of a library-
3712 -- level generic unit.
3713
3714 and then not Is_Generic_Instance (Defining_Entity (N))
3715 then
3716 Error_Msg_N
3717 ("incorrect context for library unit aspect&", Id);
3718 goto Continue;
3719 end if;
3720
3721 -- Cases where we do not delay, includes all cases where the
3722 -- expression is missing other than the above cases.
3723
3724 if not Delay_Required or else No (Expr) then
3725
3726 -- Exclude aspects Export and Import because their pragma
3727 -- syntax does not map directly to a Boolean aspect.
3728
3729 if A_Id /= Aspect_Export
3730 and then A_Id /= Aspect_Import
3731 then
3732 Make_Aitem_Pragma
3733 (Pragma_Argument_Associations => New_List (
3734 Make_Pragma_Argument_Association (Sloc (Ent),
3735 Expression => Ent)),
3736 Pragma_Name => Chars (Id));
3737 end if;
3738
3739 Delay_Required := False;
3740
3741 -- In general cases, the corresponding pragma/attribute
3742 -- definition clause will be inserted later at the freezing
3743 -- point, and we do not need to build it now.
3744
3745 else
3746 Aitem := Empty;
3747 end if;
3748
3749 -- Storage_Size
3750
3751 -- This is special because for access types we need to generate
3752 -- an attribute definition clause. This also works for single
3753 -- task declarations, but it does not work for task type
3754 -- declarations, because we have the case where the expression
3755 -- references a discriminant of the task type. That can't use
3756 -- an attribute definition clause because we would not have
3757 -- visibility on the discriminant. For that case we must
3758 -- generate a pragma in the task definition.
3759
3760 when Aspect_Storage_Size =>
3761
3762 -- Task type case
3763
3764 if Ekind (E) = E_Task_Type then
3765 declare
3766 Decl : constant Node_Id := Declaration_Node (E);
3767
3768 begin
3769 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3770
3771 -- If no task definition, create one
3772
3773 if No (Task_Definition (Decl)) then
3774 Set_Task_Definition (Decl,
3775 Make_Task_Definition (Loc,
3776 Visible_Declarations => Empty_List,
3777 End_Label => Empty));
3778 end if;
3779
3780 -- Create a pragma and put it at the start of the task
3781 -- definition for the task type declaration.
3782
3783 Make_Aitem_Pragma
3784 (Pragma_Argument_Associations => New_List (
3785 Make_Pragma_Argument_Association (Loc,
3786 Expression => Relocate_Node (Expr))),
3787 Pragma_Name => Name_Storage_Size);
3788
3789 Prepend
3790 (Aitem,
3791 Visible_Declarations (Task_Definition (Decl)));
3792 goto Continue;
3793 end;
3794
3795 -- All other cases, generate attribute definition
3796
3797 else
3798 Aitem :=
3799 Make_Attribute_Definition_Clause (Loc,
3800 Name => Ent,
3801 Chars => Chars (Id),
3802 Expression => Relocate_Node (Expr));
3803 end if;
3804 end case;
3805
3806 -- Attach the corresponding pragma/attribute definition clause to
3807 -- the aspect specification node.
3808
3809 if Present (Aitem) then
3810 Set_From_Aspect_Specification (Aitem);
3811 end if;
3812
3813 -- For an aspect that applies to a type, indicate whether it
3814 -- appears on a partial view of the type.
3815
3816 if Is_Type (E)
3817 and then Is_Private_Type (E)
3818 then
3819 Set_Aspect_On_Partial_View (Aspect);
3820 end if;
3821
3822 -- In the context of a compilation unit, we directly put the
3823 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3824 -- node (no delay is required here) except for aspects on a
3825 -- subprogram body (see below) and a generic package, for which we
3826 -- need to introduce the pragma before building the generic copy
3827 -- (see sem_ch12), and for package instantiations, where the
3828 -- library unit pragmas are better handled early.
3829
3830 if Nkind (Parent (N)) = N_Compilation_Unit
3831 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3832 then
3833 declare
3834 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3835
3836 begin
3837 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3838
3839 -- For a Boolean aspect, create the corresponding pragma if
3840 -- no expression or if the value is True.
3841
3842 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3843 if Is_True (Static_Boolean (Expr)) then
3844 Make_Aitem_Pragma
3845 (Pragma_Argument_Associations => New_List (
3846 Make_Pragma_Argument_Association (Sloc (Ent),
3847 Expression => Ent)),
3848 Pragma_Name => Chars (Id));
3849
3850 Set_From_Aspect_Specification (Aitem, True);
3851 Set_Corresponding_Aspect (Aitem, Aspect);
3852
3853 else
3854 goto Continue;
3855 end if;
3856 end if;
3857
3858 -- If the aspect is on a subprogram body (relevant aspect
3859 -- is Inline), add the pragma in front of the declarations.
3860
3861 if Nkind (N) = N_Subprogram_Body then
3862 if No (Declarations (N)) then
3863 Set_Declarations (N, New_List);
3864 end if;
3865
3866 Prepend (Aitem, Declarations (N));
3867
3868 elsif Nkind (N) = N_Generic_Package_Declaration then
3869 if No (Visible_Declarations (Specification (N))) then
3870 Set_Visible_Declarations (Specification (N), New_List);
3871 end if;
3872
3873 Prepend (Aitem,
3874 Visible_Declarations (Specification (N)));
3875
3876 elsif Nkind (N) = N_Package_Instantiation then
3877 declare
3878 Spec : constant Node_Id :=
3879 Specification (Instance_Spec (N));
3880 begin
3881 if No (Visible_Declarations (Spec)) then
3882 Set_Visible_Declarations (Spec, New_List);
3883 end if;
3884
3885 Prepend (Aitem, Visible_Declarations (Spec));
3886 end;
3887
3888 else
3889 if No (Pragmas_After (Aux)) then
3890 Set_Pragmas_After (Aux, New_List);
3891 end if;
3892
3893 Append (Aitem, Pragmas_After (Aux));
3894 end if;
3895
3896 goto Continue;
3897 end;
3898 end if;
3899
3900 -- The evaluation of the aspect is delayed to the freezing point.
3901 -- The pragma or attribute clause if there is one is then attached
3902 -- to the aspect specification which is put in the rep item list.
3903
3904 if Delay_Required then
3905 if Present (Aitem) then
3906 Set_Is_Delayed_Aspect (Aitem);
3907 Set_Aspect_Rep_Item (Aspect, Aitem);
3908 Set_Parent (Aitem, Aspect);
3909 end if;
3910
3911 Set_Is_Delayed_Aspect (Aspect);
3912
3913 -- In the case of Default_Value, link the aspect to base type
3914 -- as well, even though it appears on a first subtype. This is
3915 -- mandated by the semantics of the aspect. Do not establish
3916 -- the link when processing the base type itself as this leads
3917 -- to a rep item circularity. Verify that we are dealing with
3918 -- a scalar type to prevent cascaded errors.
3919
3920 if A_Id = Aspect_Default_Value
3921 and then Is_Scalar_Type (E)
3922 and then Base_Type (E) /= E
3923 then
3924 Set_Has_Delayed_Aspects (Base_Type (E));
3925 Record_Rep_Item (Base_Type (E), Aspect);
3926 end if;
3927
3928 Set_Has_Delayed_Aspects (E);
3929 Record_Rep_Item (E, Aspect);
3930
3931 -- When delay is not required and the context is a package or a
3932 -- subprogram body, insert the pragma in the body declarations.
3933
3934 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3935 if No (Declarations (N)) then
3936 Set_Declarations (N, New_List);
3937 end if;
3938
3939 -- The pragma is added before source declarations
3940
3941 Prepend_To (Declarations (N), Aitem);
3942
3943 -- When delay is not required and the context is not a compilation
3944 -- unit, we simply insert the pragma/attribute definition clause
3945 -- in sequence.
3946
3947 elsif Present (Aitem) then
3948 Insert_After (Ins_Node, Aitem);
3949 Ins_Node := Aitem;
3950 end if;
3951 end Analyze_One_Aspect;
3952
3953 <<Continue>>
3954 Next (Aspect);
3955 end loop Aspect_Loop;
3956
3957 if Has_Delayed_Aspects (E) then
3958 Ensure_Freeze_Node (E);
3959 end if;
3960 end Analyze_Aspect_Specifications;
3961
3962 ------------------------------------------------
3963 -- Analyze_Aspects_On_Subprogram_Body_Or_Stub --
3964 ------------------------------------------------
3965
3966 procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id) is
3967 Body_Id : constant Entity_Id := Defining_Entity (N);
3968
3969 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3970 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3971 -- error message depending on the aspects involved. Spec_Id denotes the
3972 -- entity of the corresponding spec.
3973
3974 --------------------------------
3975 -- Diagnose_Misplaced_Aspects --
3976 --------------------------------
3977
3978 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3979 procedure Misplaced_Aspect_Error
3980 (Asp : Node_Id;
3981 Ref_Nam : Name_Id);
3982 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3983 -- the name of the refined version of the aspect.
3984
3985 ----------------------------
3986 -- Misplaced_Aspect_Error --
3987 ----------------------------
3988
3989 procedure Misplaced_Aspect_Error
3990 (Asp : Node_Id;
3991 Ref_Nam : Name_Id)
3992 is
3993 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3994 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3995
3996 begin
3997 -- The corresponding spec already contains the aspect in question
3998 -- and the one appearing on the body must be the refined form:
3999
4000 -- procedure P with Global ...;
4001 -- procedure P with Global ... is ... end P;
4002 -- ^
4003 -- Refined_Global
4004
4005 if Has_Aspect (Spec_Id, Asp_Id) then
4006 Error_Msg_Name_1 := Asp_Nam;
4007
4008 -- Subunits cannot carry aspects that apply to a subprogram
4009 -- declaration.
4010
4011 if Nkind (Parent (N)) = N_Subunit then
4012 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
4013
4014 -- Otherwise suggest the refined form
4015
4016 else
4017 Error_Msg_Name_2 := Ref_Nam;
4018 Error_Msg_N ("aspect % should be %", Asp);
4019 end if;
4020
4021 -- Otherwise the aspect must appear on the spec, not on the body
4022
4023 -- procedure P;
4024 -- procedure P with Global ... is ... end P;
4025
4026 else
4027 Error_Msg_N
4028 ("aspect specification must appear on initial declaration",
4029 Asp);
4030 end if;
4031 end Misplaced_Aspect_Error;
4032
4033 -- Local variables
4034
4035 Asp : Node_Id;
4036 Asp_Nam : Name_Id;
4037
4038 -- Start of processing for Diagnose_Misplaced_Aspects
4039
4040 begin
4041 -- Iterate over the aspect specifications and emit specific errors
4042 -- where applicable.
4043
4044 Asp := First (Aspect_Specifications (N));
4045 while Present (Asp) loop
4046 Asp_Nam := Chars (Identifier (Asp));
4047
4048 -- Do not emit errors on aspects that can appear on a subprogram
4049 -- body. This scenario occurs when the aspect specification list
4050 -- contains both misplaced and properly placed aspects.
4051
4052 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
4053 null;
4054
4055 -- Special diagnostics for SPARK aspects
4056
4057 elsif Asp_Nam = Name_Depends then
4058 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
4059
4060 elsif Asp_Nam = Name_Global then
4061 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
4062
4063 elsif Asp_Nam = Name_Post then
4064 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
4065
4066 -- Otherwise a language-defined aspect is misplaced
4067
4068 else
4069 Error_Msg_N
4070 ("aspect specification must appear on initial declaration",
4071 Asp);
4072 end if;
4073
4074 Next (Asp);
4075 end loop;
4076 end Diagnose_Misplaced_Aspects;
4077
4078 -- Local variables
4079
4080 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
4081
4082 -- Start of processing for Analyze_Aspects_On_Subprogram_Body_Or_Stub
4083
4084 begin
4085 -- Language-defined aspects cannot be associated with a subprogram body
4086 -- [stub] if the subprogram has a spec. Certain implementation defined
4087 -- aspects are allowed to break this rule (for all applicable cases, see
4088 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
4089
4090 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
4091 Diagnose_Misplaced_Aspects (Spec_Id);
4092 else
4093 Analyze_Aspect_Specifications (N, Body_Id);
4094 end if;
4095 end Analyze_Aspects_On_Subprogram_Body_Or_Stub;
4096
4097 -----------------------
4098 -- Analyze_At_Clause --
4099 -----------------------
4100
4101 -- An at clause is replaced by the corresponding Address attribute
4102 -- definition clause that is the preferred approach in Ada 95.
4103
4104 procedure Analyze_At_Clause (N : Node_Id) is
4105 CS : constant Boolean := Comes_From_Source (N);
4106
4107 begin
4108 -- This is an obsolescent feature
4109
4110 Check_Restriction (No_Obsolescent_Features, N);
4111
4112 if Warn_On_Obsolescent_Feature then
4113 Error_Msg_N
4114 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
4115 Error_Msg_N
4116 ("\?j?use address attribute definition clause instead", N);
4117 end if;
4118
4119 -- Rewrite as address clause
4120
4121 Rewrite (N,
4122 Make_Attribute_Definition_Clause (Sloc (N),
4123 Name => Identifier (N),
4124 Chars => Name_Address,
4125 Expression => Expression (N)));
4126
4127 -- We preserve Comes_From_Source, since logically the clause still comes
4128 -- from the source program even though it is changed in form.
4129
4130 Set_Comes_From_Source (N, CS);
4131
4132 -- Analyze rewritten clause
4133
4134 Analyze_Attribute_Definition_Clause (N);
4135 end Analyze_At_Clause;
4136
4137 -----------------------------------------
4138 -- Analyze_Attribute_Definition_Clause --
4139 -----------------------------------------
4140
4141 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
4142 Loc : constant Source_Ptr := Sloc (N);
4143 Nam : constant Node_Id := Name (N);
4144 Attr : constant Name_Id := Chars (N);
4145 Expr : constant Node_Id := Expression (N);
4146 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
4147
4148 Ent : Entity_Id;
4149 -- The entity of Nam after it is analyzed. In the case of an incomplete
4150 -- type, this is the underlying type.
4151
4152 U_Ent : Entity_Id;
4153 -- The underlying entity to which the attribute applies. Generally this
4154 -- is the Underlying_Type of Ent, except in the case where the clause
4155 -- applies to the full view of an incomplete or private type, in which
4156 -- case U_Ent is just a copy of Ent.
4157
4158 FOnly : Boolean := False;
4159 -- Reset to True for subtype specific attribute (Alignment, Size)
4160 -- and for stream attributes, i.e. those cases where in the call to
4161 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
4162 -- are checked. Note that the case of stream attributes is not clear
4163 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
4164 -- Storage_Size for derived task types, but that is also clearly
4165 -- unintentional.
4166
4167 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
4168 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
4169 -- definition clauses.
4170
4171 function Duplicate_Clause return Boolean;
4172 -- This routine checks if the aspect for U_Ent being given by attribute
4173 -- definition clause N is for an aspect that has already been specified,
4174 -- and if so gives an error message. If there is a duplicate, True is
4175 -- returned, otherwise if there is no error, False is returned.
4176
4177 procedure Check_Indexing_Functions;
4178 -- Check that the function in Constant_Indexing or Variable_Indexing
4179 -- attribute has the proper type structure. If the name is overloaded,
4180 -- check that some interpretation is legal.
4181
4182 procedure Check_Iterator_Functions;
4183 -- Check that there is a single function in Default_Iterator attribute
4184 -- that has the proper type structure.
4185
4186 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
4187 -- Common legality check for the previous two
4188
4189 -----------------------------------
4190 -- Analyze_Stream_TSS_Definition --
4191 -----------------------------------
4192
4193 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
4194 Subp : Entity_Id := Empty;
4195 I : Interp_Index;
4196 It : Interp;
4197 Pnam : Entity_Id;
4198
4199 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
4200 -- True for Read attribute, False for other attributes
4201
4202 function Has_Good_Profile
4203 (Subp : Entity_Id;
4204 Report : Boolean := False) return Boolean;
4205 -- Return true if the entity is a subprogram with an appropriate
4206 -- profile for the attribute being defined. If result is False and
4207 -- Report is True, function emits appropriate error.
4208
4209 ----------------------
4210 -- Has_Good_Profile --
4211 ----------------------
4212
4213 function Has_Good_Profile
4214 (Subp : Entity_Id;
4215 Report : Boolean := False) return Boolean
4216 is
4217 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
4218 (False => E_Procedure, True => E_Function);
4219 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
4220 F : Entity_Id;
4221 Typ : Entity_Id;
4222
4223 begin
4224 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
4225 return False;
4226 end if;
4227
4228 F := First_Formal (Subp);
4229
4230 if No (F)
4231 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
4232 or else Designated_Type (Etype (F)) /=
4233 Class_Wide_Type (RTE (RE_Root_Stream_Type))
4234 then
4235 return False;
4236 end if;
4237
4238 if not Is_Function then
4239 Next_Formal (F);
4240
4241 declare
4242 Expected_Mode : constant array (Boolean) of Entity_Kind :=
4243 (False => E_In_Parameter,
4244 True => E_Out_Parameter);
4245 begin
4246 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
4247 return False;
4248 end if;
4249 end;
4250
4251 Typ := Etype (F);
4252
4253 -- If the attribute specification comes from an aspect
4254 -- specification for a class-wide stream, the parameter must be
4255 -- a class-wide type of the entity to which the aspect applies.
4256
4257 if From_Aspect_Specification (N)
4258 and then Class_Present (Parent (N))
4259 and then Is_Class_Wide_Type (Typ)
4260 then
4261 Typ := Etype (Typ);
4262 end if;
4263
4264 else
4265 Typ := Etype (Subp);
4266 end if;
4267
4268 -- Verify that the prefix of the attribute and the local name for
4269 -- the type of the formal match, or one is the class-wide of the
4270 -- other, in the case of a class-wide stream operation.
4271
4272 if Base_Type (Typ) = Base_Type (Ent)
4273 or else (Is_Class_Wide_Type (Typ)
4274 and then Typ = Class_Wide_Type (Base_Type (Ent)))
4275 or else (Is_Class_Wide_Type (Ent)
4276 and then Ent = Class_Wide_Type (Base_Type (Typ)))
4277 then
4278 null;
4279 else
4280 return False;
4281 end if;
4282
4283 if Present (Next_Formal (F)) then
4284 return False;
4285
4286 elsif not Is_Scalar_Type (Typ)
4287 and then not Is_First_Subtype (Typ)
4288 and then not Is_Class_Wide_Type (Typ)
4289 then
4290 if Report and not Is_First_Subtype (Typ) then
4291 Error_Msg_N
4292 ("subtype of formal in stream operation must be a first "
4293 & "subtype", Parameter_Type (Parent (F)));
4294 end if;
4295
4296 return False;
4297
4298 else
4299 return True;
4300 end if;
4301 end Has_Good_Profile;
4302
4303 -- Start of processing for Analyze_Stream_TSS_Definition
4304
4305 begin
4306 FOnly := True;
4307
4308 if not Is_Type (U_Ent) then
4309 Error_Msg_N ("local name must be a subtype", Nam);
4310 return;
4311
4312 elsif not Is_First_Subtype (U_Ent) then
4313 Error_Msg_N ("local name must be a first subtype", Nam);
4314 return;
4315 end if;
4316
4317 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
4318
4319 -- If Pnam is present, it can be either inherited from an ancestor
4320 -- type (in which case it is legal to redefine it for this type), or
4321 -- be a previous definition of the attribute for the same type (in
4322 -- which case it is illegal).
4323
4324 -- In the first case, it will have been analyzed already, and we
4325 -- can check that its profile does not match the expected profile
4326 -- for a stream attribute of U_Ent. In the second case, either Pnam
4327 -- has been analyzed (and has the expected profile), or it has not
4328 -- been analyzed yet (case of a type that has not been frozen yet
4329 -- and for which the stream attribute has been set using Set_TSS).
4330
4331 if Present (Pnam)
4332 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4333 then
4334 Error_Msg_Sloc := Sloc (Pnam);
4335 Error_Msg_Name_1 := Attr;
4336 Error_Msg_N ("% attribute already defined #", Nam);
4337 return;
4338 end if;
4339
4340 Analyze (Expr);
4341
4342 if Is_Entity_Name (Expr) then
4343 if not Is_Overloaded (Expr) then
4344 if Has_Good_Profile (Entity (Expr), Report => True) then
4345 Subp := Entity (Expr);
4346 end if;
4347
4348 else
4349 Get_First_Interp (Expr, I, It);
4350 while Present (It.Nam) loop
4351 if Has_Good_Profile (It.Nam) then
4352 Subp := It.Nam;
4353 exit;
4354 end if;
4355
4356 Get_Next_Interp (I, It);
4357 end loop;
4358 end if;
4359 end if;
4360
4361 if Present (Subp) then
4362 if Is_Abstract_Subprogram (Subp) then
4363 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4364 return;
4365
4366 -- A stream subprogram for an interface type must be a null
4367 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4368 -- of an interface is not an interface type (3.9.4 (6.b/2)).
4369
4370 elsif Is_Interface (U_Ent)
4371 and then not Is_Class_Wide_Type (U_Ent)
4372 and then not Inside_A_Generic
4373 and then
4374 (Ekind (Subp) = E_Function
4375 or else
4376 not Null_Present
4377 (Specification
4378 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
4379 then
4380 Error_Msg_N
4381 ("stream subprogram for interface type must be null "
4382 & "procedure", Expr);
4383 end if;
4384
4385 Set_Entity (Expr, Subp);
4386 Set_Etype (Expr, Etype (Subp));
4387
4388 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
4389
4390 else
4391 Error_Msg_Name_1 := Attr;
4392 Error_Msg_N ("incorrect expression for% attribute", Expr);
4393 end if;
4394 end Analyze_Stream_TSS_Definition;
4395
4396 ------------------------------
4397 -- Check_Indexing_Functions --
4398 ------------------------------
4399
4400 procedure Check_Indexing_Functions is
4401 Indexing_Found : Boolean := False;
4402
4403 procedure Check_Inherited_Indexing;
4404 -- For a derived type, check that no indexing aspect is specified
4405 -- for the type if it is also inherited
4406
4407 procedure Check_One_Function (Subp : Entity_Id);
4408 -- Check one possible interpretation. Sets Indexing_Found True if a
4409 -- legal indexing function is found.
4410
4411 procedure Illegal_Indexing (Msg : String);
4412 -- Diagnose illegal indexing function if not overloaded. In the
4413 -- overloaded case indicate that no legal interpretation exists.
4414
4415 ------------------------------
4416 -- Check_Inherited_Indexing --
4417 ------------------------------
4418
4419 procedure Check_Inherited_Indexing is
4420 Inherited : Node_Id;
4421
4422 begin
4423 if Attr = Name_Constant_Indexing then
4424 Inherited :=
4425 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4426 else pragma Assert (Attr = Name_Variable_Indexing);
4427 Inherited :=
4428 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4429 end if;
4430
4431 if Present (Inherited) then
4432 if Debug_Flag_Dot_XX then
4433 null;
4434
4435 -- OK if current attribute_definition_clause is expansion of
4436 -- inherited aspect.
4437
4438 elsif Aspect_Rep_Item (Inherited) = N then
4439 null;
4440
4441 -- Indicate the operation that must be overridden, rather than
4442 -- redefining the indexing aspect.
4443
4444 else
4445 Illegal_Indexing
4446 ("indexing function already inherited from parent type");
4447 Error_Msg_NE
4448 ("!override & instead",
4449 N, Entity (Expression (Inherited)));
4450 end if;
4451 end if;
4452 end Check_Inherited_Indexing;
4453
4454 ------------------------
4455 -- Check_One_Function --
4456 ------------------------
4457
4458 procedure Check_One_Function (Subp : Entity_Id) is
4459 Default_Element : Node_Id;
4460 Ret_Type : constant Entity_Id := Etype (Subp);
4461
4462 begin
4463 if not Is_Overloadable (Subp) then
4464 Illegal_Indexing ("illegal indexing function for type&");
4465 return;
4466
4467 elsif Scope (Subp) /= Scope (Ent) then
4468 if Nkind (Expr) = N_Expanded_Name then
4469
4470 -- Indexing function can't be declared elsewhere
4471
4472 Illegal_Indexing
4473 ("indexing function must be declared in scope of type&");
4474 end if;
4475
4476 return;
4477
4478 elsif No (First_Formal (Subp)) then
4479 Illegal_Indexing
4480 ("Indexing requires a function that applies to type&");
4481 return;
4482
4483 elsif No (Next_Formal (First_Formal (Subp))) then
4484 Illegal_Indexing
4485 ("indexing function must have at least two parameters");
4486 return;
4487
4488 elsif Is_Derived_Type (Ent) then
4489 Check_Inherited_Indexing;
4490 end if;
4491
4492 if not Check_Primitive_Function (Subp) then
4493 Illegal_Indexing
4494 ("Indexing aspect requires a function that applies to type&");
4495 return;
4496 end if;
4497
4498 -- If partial declaration exists, verify that it is not tagged.
4499
4500 if Ekind (Current_Scope) = E_Package
4501 and then Has_Private_Declaration (Ent)
4502 and then From_Aspect_Specification (N)
4503 and then
4504 List_Containing (Parent (Ent)) =
4505 Private_Declarations
4506 (Specification (Unit_Declaration_Node (Current_Scope)))
4507 and then Nkind (N) = N_Attribute_Definition_Clause
4508 then
4509 declare
4510 Decl : Node_Id;
4511
4512 begin
4513 Decl :=
4514 First (Visible_Declarations
4515 (Specification
4516 (Unit_Declaration_Node (Current_Scope))));
4517
4518 while Present (Decl) loop
4519 if Nkind (Decl) = N_Private_Type_Declaration
4520 and then Ent = Full_View (Defining_Identifier (Decl))
4521 and then Tagged_Present (Decl)
4522 and then No (Aspect_Specifications (Decl))
4523 then
4524 Illegal_Indexing
4525 ("Indexing aspect cannot be specified on full view "
4526 & "if partial view is tagged");
4527 return;
4528 end if;
4529
4530 Next (Decl);
4531 end loop;
4532 end;
4533 end if;
4534
4535 -- An indexing function must return either the default element of
4536 -- the container, or a reference type. For variable indexing it
4537 -- must be the latter.
4538
4539 Default_Element :=
4540 Find_Value_Of_Aspect
4541 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4542
4543 if Present (Default_Element) then
4544 Analyze (Default_Element);
4545 end if;
4546
4547 -- For variable_indexing the return type must be a reference type
4548
4549 if Attr = Name_Variable_Indexing then
4550 if not Has_Implicit_Dereference (Ret_Type) then
4551 Illegal_Indexing
4552 ("variable indexing must return a reference type");
4553 return;
4554
4555 elsif Is_Access_Constant
4556 (Etype (First_Discriminant (Ret_Type)))
4557 then
4558 Illegal_Indexing
4559 ("variable indexing must return an access to variable");
4560 return;
4561 end if;
4562
4563 else
4564 if Has_Implicit_Dereference (Ret_Type)
4565 and then not
4566 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4567 then
4568 Illegal_Indexing
4569 ("constant indexing must return an access to constant");
4570 return;
4571
4572 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4573 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4574 then
4575 Illegal_Indexing
4576 ("constant indexing must apply to an access to constant");
4577 return;
4578 end if;
4579 end if;
4580
4581 -- All checks succeeded.
4582
4583 Indexing_Found := True;
4584 end Check_One_Function;
4585
4586 -----------------------
4587 -- Illegal_Indexing --
4588 -----------------------
4589
4590 procedure Illegal_Indexing (Msg : String) is
4591 begin
4592 Error_Msg_NE (Msg, N, Ent);
4593 end Illegal_Indexing;
4594
4595 -- Start of processing for Check_Indexing_Functions
4596
4597 begin
4598 if In_Instance then
4599 Check_Inherited_Indexing;
4600 end if;
4601
4602 Analyze (Expr);
4603
4604 if not Is_Overloaded (Expr) then
4605 Check_One_Function (Entity (Expr));
4606
4607 else
4608 declare
4609 I : Interp_Index;
4610 It : Interp;
4611
4612 begin
4613 Indexing_Found := False;
4614 Get_First_Interp (Expr, I, It);
4615 while Present (It.Nam) loop
4616
4617 -- Note that analysis will have added the interpretation
4618 -- that corresponds to the dereference. We only check the
4619 -- subprogram itself. Ignore homonyms that may come from
4620 -- derived types in the context.
4621
4622 if Is_Overloadable (It.Nam)
4623 and then Comes_From_Source (It.Nam)
4624 then
4625 Check_One_Function (It.Nam);
4626 end if;
4627
4628 Get_Next_Interp (I, It);
4629 end loop;
4630 end;
4631 end if;
4632
4633 if not Indexing_Found and then not Error_Posted (N) then
4634 Error_Msg_NE
4635 ("aspect Indexing requires a local function that applies to "
4636 & "type&", Expr, Ent);
4637 end if;
4638 end Check_Indexing_Functions;
4639
4640 ------------------------------
4641 -- Check_Iterator_Functions --
4642 ------------------------------
4643
4644 procedure Check_Iterator_Functions is
4645 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4646 -- Check one possible interpretation for validity
4647
4648 ----------------------------
4649 -- Valid_Default_Iterator --
4650 ----------------------------
4651
4652 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4653 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
4654 Formal : Entity_Id;
4655
4656 begin
4657 if not Check_Primitive_Function (Subp) then
4658 return False;
4659
4660 -- The return type must be derived from a type in an instance
4661 -- of Iterator.Interfaces, and thus its root type must have a
4662 -- predefined name.
4663
4664 elsif Chars (Root_T) /= Name_Forward_Iterator
4665 and then Chars (Root_T) /= Name_Reversible_Iterator
4666 then
4667 return False;
4668
4669 else
4670 Formal := First_Formal (Subp);
4671 end if;
4672
4673 -- False if any subsequent formal has no default expression
4674
4675 Formal := Next_Formal (Formal);
4676 while Present (Formal) loop
4677 if No (Expression (Parent (Formal))) then
4678 return False;
4679 end if;
4680
4681 Next_Formal (Formal);
4682 end loop;
4683
4684 -- True if all subsequent formals have default expressions
4685
4686 return True;
4687 end Valid_Default_Iterator;
4688
4689 -- Start of processing for Check_Iterator_Functions
4690
4691 begin
4692 Analyze (Expr);
4693
4694 if not Is_Entity_Name (Expr) then
4695 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4696 end if;
4697
4698 if not Is_Overloaded (Expr) then
4699 if Entity (Expr) /= Any_Id
4700 and then not Check_Primitive_Function (Entity (Expr))
4701 then
4702 Error_Msg_NE
4703 ("aspect Indexing requires a function that applies to type&",
4704 Entity (Expr), Ent);
4705 end if;
4706
4707 -- Flag the default_iterator as well as the denoted function.
4708
4709 if not Valid_Default_Iterator (Entity (Expr)) then
4710 Error_Msg_N ("improper function for default iterator!", Expr);
4711 end if;
4712
4713 else
4714 declare
4715 Default : Entity_Id := Empty;
4716 I : Interp_Index;
4717 It : Interp;
4718
4719 begin
4720 Get_First_Interp (Expr, I, It);
4721 while Present (It.Nam) loop
4722 if not Check_Primitive_Function (It.Nam)
4723 or else not Valid_Default_Iterator (It.Nam)
4724 then
4725 Remove_Interp (I);
4726
4727 elsif Present (Default) then
4728
4729 -- An explicit one should override an implicit one
4730
4731 if Comes_From_Source (Default) =
4732 Comes_From_Source (It.Nam)
4733 then
4734 Error_Msg_N ("default iterator must be unique", Expr);
4735 Error_Msg_Sloc := Sloc (Default);
4736 Error_Msg_N ("\\possible interpretation#", Expr);
4737 Error_Msg_Sloc := Sloc (It.Nam);
4738 Error_Msg_N ("\\possible interpretation#", Expr);
4739
4740 elsif Comes_From_Source (It.Nam) then
4741 Default := It.Nam;
4742 end if;
4743 else
4744 Default := It.Nam;
4745 end if;
4746
4747 Get_Next_Interp (I, It);
4748 end loop;
4749
4750 if Present (Default) then
4751 Set_Entity (Expr, Default);
4752 Set_Is_Overloaded (Expr, False);
4753 else
4754 Error_Msg_N
4755 ("no interpretation is a valid default iterator!", Expr);
4756 end if;
4757 end;
4758 end if;
4759 end Check_Iterator_Functions;
4760
4761 -------------------------------
4762 -- Check_Primitive_Function --
4763 -------------------------------
4764
4765 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4766 Ctrl : Entity_Id;
4767
4768 begin
4769 if Ekind (Subp) /= E_Function then
4770 return False;
4771 end if;
4772
4773 if No (First_Formal (Subp)) then
4774 return False;
4775 else
4776 Ctrl := Etype (First_Formal (Subp));
4777 end if;
4778
4779 -- To be a primitive operation subprogram has to be in same scope.
4780
4781 if Scope (Ctrl) /= Scope (Subp) then
4782 return False;
4783 end if;
4784
4785 -- Type of formal may be the class-wide type, an access to such,
4786 -- or an incomplete view.
4787
4788 if Ctrl = Ent
4789 or else Ctrl = Class_Wide_Type (Ent)
4790 or else
4791 (Ekind (Ctrl) = E_Anonymous_Access_Type
4792 and then (Designated_Type (Ctrl) = Ent
4793 or else
4794 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4795 or else
4796 (Ekind (Ctrl) = E_Incomplete_Type
4797 and then Full_View (Ctrl) = Ent)
4798 then
4799 null;
4800 else
4801 return False;
4802 end if;
4803
4804 return True;
4805 end Check_Primitive_Function;
4806
4807 ----------------------
4808 -- Duplicate_Clause --
4809 ----------------------
4810
4811 function Duplicate_Clause return Boolean is
4812 A : Node_Id;
4813
4814 begin
4815 -- Nothing to do if this attribute definition clause comes from
4816 -- an aspect specification, since we could not be duplicating an
4817 -- explicit clause, and we dealt with the case of duplicated aspects
4818 -- in Analyze_Aspect_Specifications.
4819
4820 if From_Aspect_Specification (N) then
4821 return False;
4822 end if;
4823
4824 -- Otherwise current clause may duplicate previous clause, or a
4825 -- previously given pragma or aspect specification for the same
4826 -- aspect.
4827
4828 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4829
4830 if Present (A) then
4831 Error_Msg_Name_1 := Chars (N);
4832 Error_Msg_Sloc := Sloc (A);
4833
4834 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4835 return True;
4836 end if;
4837
4838 return False;
4839 end Duplicate_Clause;
4840
4841 -- Start of processing for Analyze_Attribute_Definition_Clause
4842
4843 begin
4844 -- The following code is a defense against recursion. Not clear that
4845 -- this can happen legitimately, but perhaps some error situations can
4846 -- cause it, and we did see this recursion during testing.
4847
4848 if Analyzed (N) then
4849 return;
4850 else
4851 Set_Analyzed (N, True);
4852 end if;
4853
4854 Check_Restriction_No_Use_Of_Attribute (N);
4855
4856 -- Ignore some selected attributes in CodePeer mode since they are not
4857 -- relevant in this context.
4858
4859 if CodePeer_Mode then
4860 case Id is
4861
4862 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4863 -- internal representation of types by implicitly packing them.
4864
4865 when Attribute_Component_Size =>
4866 Rewrite (N, Make_Null_Statement (Sloc (N)));
4867 return;
4868
4869 when others =>
4870 null;
4871 end case;
4872 end if;
4873
4874 -- Process Ignore_Rep_Clauses option
4875
4876 if Ignore_Rep_Clauses then
4877 case Id is
4878
4879 -- The following should be ignored. They do not affect legality
4880 -- and may be target dependent. The basic idea of -gnatI is to
4881 -- ignore any rep clauses that may be target dependent but do not
4882 -- affect legality (except possibly to be rejected because they
4883 -- are incompatible with the compilation target).
4884
4885 when Attribute_Alignment
4886 | Attribute_Bit_Order
4887 | Attribute_Component_Size
4888 | Attribute_Default_Scalar_Storage_Order
4889 | Attribute_Machine_Radix
4890 | Attribute_Object_Size
4891 | Attribute_Scalar_Storage_Order
4892 | Attribute_Size
4893 | Attribute_Small
4894 | Attribute_Stream_Size
4895 | Attribute_Value_Size
4896 =>
4897 Kill_Rep_Clause (N);
4898 return;
4899
4900 -- The following should not be ignored, because in the first place
4901 -- they are reasonably portable, and should not cause problems
4902 -- in compiling code from another target, and also they do affect
4903 -- legality, e.g. failing to provide a stream attribute for a type
4904 -- may make a program illegal.
4905
4906 when Attribute_External_Tag
4907 | Attribute_Input
4908 | Attribute_Output
4909 | Attribute_Read
4910 | Attribute_Simple_Storage_Pool
4911 | Attribute_Storage_Pool
4912 | Attribute_Storage_Size
4913 | Attribute_Write
4914 =>
4915 null;
4916
4917 -- We do not do anything here with address clauses, they will be
4918 -- removed by Freeze later on, but for now, it works better to
4919 -- keep them in the tree.
4920
4921 when Attribute_Address =>
4922 null;
4923
4924 -- Other cases are errors ("attribute& cannot be set with
4925 -- definition clause"), which will be caught below.
4926
4927 when others =>
4928 null;
4929 end case;
4930 end if;
4931
4932 Analyze (Nam);
4933 Ent := Entity (Nam);
4934
4935 if Rep_Item_Too_Early (Ent, N) then
4936 return;
4937 end if;
4938
4939 -- Rep clause applies to full view of incomplete type or private type if
4940 -- we have one (if not, this is a premature use of the type). However,
4941 -- certain semantic checks need to be done on the specified entity (i.e.
4942 -- the private view), so we save it in Ent.
4943
4944 if Is_Private_Type (Ent)
4945 and then Is_Derived_Type (Ent)
4946 and then not Is_Tagged_Type (Ent)
4947 and then No (Full_View (Ent))
4948 then
4949 -- If this is a private type whose completion is a derivation from
4950 -- another private type, there is no full view, and the attribute
4951 -- belongs to the type itself, not its underlying parent.
4952
4953 U_Ent := Ent;
4954
4955 elsif Ekind (Ent) = E_Incomplete_Type then
4956
4957 -- The attribute applies to the full view, set the entity of the
4958 -- attribute definition accordingly.
4959
4960 Ent := Underlying_Type (Ent);
4961 U_Ent := Ent;
4962 Set_Entity (Nam, Ent);
4963
4964 else
4965 U_Ent := Underlying_Type (Ent);
4966 end if;
4967
4968 -- Avoid cascaded error
4969
4970 if Etype (Nam) = Any_Type then
4971 return;
4972
4973 -- Must be declared in current scope or in case of an aspect
4974 -- specification, must be visible in current scope.
4975
4976 elsif Scope (Ent) /= Current_Scope
4977 and then
4978 not (From_Aspect_Specification (N)
4979 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4980 then
4981 Error_Msg_N ("entity must be declared in this scope", Nam);
4982 return;
4983
4984 -- Must not be a source renaming (we do have some cases where the
4985 -- expander generates a renaming, and those cases are OK, in such
4986 -- cases any attribute applies to the renamed object as well).
4987
4988 elsif Is_Object (Ent)
4989 and then Present (Renamed_Object (Ent))
4990 then
4991 -- In the case of a renamed object from source, this is an error
4992 -- unless the object is an aggregate and the renaming is created
4993 -- for an object declaration.
4994
4995 if Comes_From_Source (Renamed_Object (Ent))
4996 and then Nkind (Renamed_Object (Ent)) /= N_Aggregate
4997 then
4998 Get_Name_String (Chars (N));
4999 Error_Msg_Strlen := Name_Len;
5000 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
5001 Error_Msg_N
5002 ("~ clause not allowed for a renaming declaration "
5003 & "(RM 13.1(6))", Nam);
5004 return;
5005
5006 -- For the case of a compiler generated renaming, the attribute
5007 -- definition clause applies to the renamed object created by the
5008 -- expander. The easiest general way to handle this is to create a
5009 -- copy of the attribute definition clause for this object.
5010
5011 elsif Is_Entity_Name (Renamed_Object (Ent)) then
5012 Insert_Action (N,
5013 Make_Attribute_Definition_Clause (Loc,
5014 Name =>
5015 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
5016 Chars => Chars (N),
5017 Expression => Duplicate_Subexpr (Expression (N))));
5018
5019 -- If the renamed object is not an entity, it must be a dereference
5020 -- of an unconstrained function call, and we must introduce a new
5021 -- declaration to capture the expression. This is needed in the case
5022 -- of 'Alignment, where the original declaration must be rewritten.
5023
5024 else
5025 pragma Assert
5026 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
5027 null;
5028 end if;
5029
5030 -- If no underlying entity, use entity itself, applies to some
5031 -- previously detected error cases ???
5032
5033 elsif No (U_Ent) then
5034 U_Ent := Ent;
5035
5036 -- Cannot specify for a subtype (exception Object/Value_Size)
5037
5038 elsif Is_Type (U_Ent)
5039 and then not Is_First_Subtype (U_Ent)
5040 and then Id /= Attribute_Object_Size
5041 and then Id /= Attribute_Value_Size
5042 and then not From_At_Mod (N)
5043 then
5044 Error_Msg_N ("cannot specify attribute for subtype", Nam);
5045 return;
5046 end if;
5047
5048 Set_Entity (N, U_Ent);
5049
5050 -- Switch on particular attribute
5051
5052 case Id is
5053
5054 -------------
5055 -- Address --
5056 -------------
5057
5058 -- Address attribute definition clause
5059
5060 when Attribute_Address => Address : begin
5061
5062 -- A little error check, catch for X'Address use X'Address;
5063
5064 if Nkind (Nam) = N_Identifier
5065 and then Nkind (Expr) = N_Attribute_Reference
5066 and then Attribute_Name (Expr) = Name_Address
5067 and then Nkind (Prefix (Expr)) = N_Identifier
5068 and then Chars (Nam) = Chars (Prefix (Expr))
5069 then
5070 Error_Msg_NE
5071 ("address for & is self-referencing", Prefix (Expr), Ent);
5072 return;
5073 end if;
5074
5075 -- Not that special case, carry on with analysis of expression
5076
5077 Analyze_And_Resolve (Expr, RTE (RE_Address));
5078
5079 -- Even when ignoring rep clauses we need to indicate that the
5080 -- entity has an address clause and thus it is legal to declare
5081 -- it imported. Freeze will get rid of the address clause later.
5082 -- Also call Set_Address_Taken to indicate that an address clause
5083 -- was present, even if we are about to remove it.
5084
5085 if Ignore_Rep_Clauses then
5086 Set_Address_Taken (U_Ent);
5087
5088 if Ekind_In (U_Ent, E_Variable, E_Constant) then
5089 Record_Rep_Item (U_Ent, N);
5090 end if;
5091
5092 return;
5093 end if;
5094
5095 if Duplicate_Clause then
5096 null;
5097
5098 -- Case of address clause for subprogram
5099
5100 elsif Is_Subprogram (U_Ent) then
5101 if Has_Homonym (U_Ent) then
5102 Error_Msg_N
5103 ("address clause cannot be given for overloaded "
5104 & "subprogram", Nam);
5105 return;
5106 end if;
5107
5108 -- For subprograms, all address clauses are permitted, and we
5109 -- mark the subprogram as having a deferred freeze so that Gigi
5110 -- will not elaborate it too soon.
5111
5112 -- Above needs more comments, what is too soon about???
5113
5114 Set_Has_Delayed_Freeze (U_Ent);
5115
5116 -- Case of address clause for entry
5117
5118 elsif Ekind (U_Ent) = E_Entry then
5119 if Nkind (Parent (N)) = N_Task_Body then
5120 Error_Msg_N
5121 ("entry address must be specified in task spec", Nam);
5122 return;
5123 end if;
5124
5125 -- For entries, we require a constant address
5126
5127 Check_Constant_Address_Clause (Expr, U_Ent);
5128
5129 -- Special checks for task types
5130
5131 if Is_Task_Type (Scope (U_Ent))
5132 and then Comes_From_Source (Scope (U_Ent))
5133 then
5134 Error_Msg_N
5135 ("??entry address declared for entry in task type", N);
5136 Error_Msg_N
5137 ("\??only one task can be declared of this type", N);
5138 end if;
5139
5140 -- Entry address clauses are obsolescent
5141
5142 Check_Restriction (No_Obsolescent_Features, N);
5143
5144 if Warn_On_Obsolescent_Feature then
5145 Error_Msg_N
5146 ("?j?attaching interrupt to task entry is an obsolescent "
5147 & "feature (RM J.7.1)", N);
5148 Error_Msg_N
5149 ("\?j?use interrupt procedure instead", N);
5150 end if;
5151
5152 -- Case of an address clause for a class-wide object, which is
5153 -- considered erroneous.
5154
5155 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
5156 Error_Msg_NE
5157 ("??class-wide object & must not be overlaid", Nam, U_Ent);
5158 Error_Msg_N
5159 ("\??Program_Error will be raised at run time", Nam);
5160 Insert_Action (Declaration_Node (U_Ent),
5161 Make_Raise_Program_Error (Loc,
5162 Reason => PE_Overlaid_Controlled_Object));
5163 return;
5164
5165 -- Case of address clause for an object
5166
5167 elsif Ekind_In (U_Ent, E_Constant, E_Variable) then
5168 declare
5169 Expr : constant Node_Id := Expression (N);
5170 O_Ent : Entity_Id;
5171 Off : Boolean;
5172
5173 begin
5174 -- Exported variables cannot have an address clause, because
5175 -- this cancels the effect of the pragma Export.
5176
5177 if Is_Exported (U_Ent) then
5178 Error_Msg_N
5179 ("cannot export object with address clause", Nam);
5180 return;
5181 end if;
5182
5183 Find_Overlaid_Entity (N, O_Ent, Off);
5184
5185 if Present (O_Ent) then
5186
5187 -- If the object overlays a constant object, mark it so
5188
5189 if Is_Constant_Object (O_Ent) then
5190 Set_Overlays_Constant (U_Ent);
5191 end if;
5192
5193 -- If the address clause is of the form:
5194
5195 -- for X'Address use Y'Address;
5196
5197 -- or
5198
5199 -- C : constant Address := Y'Address;
5200 -- ...
5201 -- for X'Address use C;
5202
5203 -- then we make an entry in the table to check the size
5204 -- and alignment of the overlaying variable. But we defer
5205 -- this check till after code generation to take full
5206 -- advantage of the annotation done by the back end.
5207
5208 -- If the entity has a generic type, the check will be
5209 -- performed in the instance if the actual type justifies
5210 -- it, and we do not insert the clause in the table to
5211 -- prevent spurious warnings.
5212
5213 -- Note: we used to test Comes_From_Source and only give
5214 -- this warning for source entities, but we have removed
5215 -- this test. It really seems bogus to generate overlays
5216 -- that would trigger this warning in generated code.
5217 -- Furthermore, by removing the test, we handle the
5218 -- aspect case properly.
5219
5220 if Is_Object (O_Ent)
5221 and then not Is_Generic_Formal (O_Ent)
5222 and then not Is_Generic_Type (Etype (U_Ent))
5223 and then Address_Clause_Overlay_Warnings
5224 then
5225 Register_Address_Clause_Check
5226 (N, U_Ent, No_Uint, O_Ent, Off);
5227 end if;
5228
5229 -- If the overlay changes the storage order, mark the
5230 -- entity as being volatile to block any optimization
5231 -- for it since the construct is not really supported
5232 -- by the back end.
5233
5234 if (Is_Record_Type (Etype (U_Ent))
5235 or else Is_Array_Type (Etype (U_Ent)))
5236 and then (Is_Record_Type (Etype (O_Ent))
5237 or else Is_Array_Type (Etype (O_Ent)))
5238 and then Reverse_Storage_Order (Etype (U_Ent)) /=
5239 Reverse_Storage_Order (Etype (O_Ent))
5240 then
5241 Set_Treat_As_Volatile (U_Ent);
5242 end if;
5243
5244 else
5245 -- If this is not an overlay, mark a variable as being
5246 -- volatile to prevent unwanted optimizations. It's a
5247 -- conservative interpretation of RM 13.3(19) for the
5248 -- cases where the compiler cannot detect potential
5249 -- aliasing issues easily and it also covers the case
5250 -- of an absolute address where the volatile aspect is
5251 -- kind of implicit.
5252
5253 if Ekind (U_Ent) = E_Variable then
5254 Set_Treat_As_Volatile (U_Ent);
5255 end if;
5256
5257 -- Make an entry in the table for an absolute address as
5258 -- above to check that the value is compatible with the
5259 -- alignment of the object.
5260
5261 declare
5262 Addr : constant Node_Id := Address_Value (Expr);
5263 begin
5264 if Compile_Time_Known_Value (Addr)
5265 and then Address_Clause_Overlay_Warnings
5266 then
5267 Register_Address_Clause_Check
5268 (N, U_Ent, Expr_Value (Addr), Empty, False);
5269 end if;
5270 end;
5271 end if;
5272
5273 -- Issue an unconditional warning for a constant overlaying
5274 -- a variable. For the reverse case, we will issue it only
5275 -- if the variable is modified.
5276
5277 if Ekind (U_Ent) = E_Constant
5278 and then Present (O_Ent)
5279 and then not Overlays_Constant (U_Ent)
5280 and then Address_Clause_Overlay_Warnings
5281 then
5282 Error_Msg_N ("??constant overlays a variable", Expr);
5283
5284 -- Imported variables can have an address clause, but then
5285 -- the import is pretty meaningless except to suppress
5286 -- initializations, so we do not need such variables to
5287 -- be statically allocated (and in fact it causes trouble
5288 -- if the address clause is a local value).
5289
5290 elsif Is_Imported (U_Ent) then
5291 Set_Is_Statically_Allocated (U_Ent, False);
5292 end if;
5293
5294 -- We mark a possible modification of a variable with an
5295 -- address clause, since it is likely aliasing is occurring.
5296
5297 Note_Possible_Modification (Nam, Sure => False);
5298
5299 -- Legality checks on the address clause for initialized
5300 -- objects is deferred until the freeze point, because
5301 -- a subsequent pragma might indicate that the object
5302 -- is imported and thus not initialized. Also, the address
5303 -- clause might involve entities that have yet to be
5304 -- elaborated.
5305
5306 Set_Has_Delayed_Freeze (U_Ent);
5307
5308 -- If an initialization call has been generated for this
5309 -- object, it needs to be deferred to after the freeze node
5310 -- we have just now added, otherwise GIGI will see a
5311 -- reference to the variable (as actual to the IP call)
5312 -- before its definition.
5313
5314 declare
5315 Init_Call : constant Node_Id :=
5316 Remove_Init_Call (U_Ent, N);
5317
5318 begin
5319 if Present (Init_Call) then
5320 Append_Freeze_Action (U_Ent, Init_Call);
5321
5322 -- Reset Initialization_Statements pointer so that
5323 -- if there is a pragma Import further down, it can
5324 -- clear any default initialization.
5325
5326 Set_Initialization_Statements (U_Ent, Init_Call);
5327 end if;
5328 end;
5329
5330 -- Entity has delayed freeze, so we will generate an
5331 -- alignment check at the freeze point unless suppressed.
5332
5333 if not Range_Checks_Suppressed (U_Ent)
5334 and then not Alignment_Checks_Suppressed (U_Ent)
5335 then
5336 Set_Check_Address_Alignment (N);
5337 end if;
5338
5339 -- Kill the size check code, since we are not allocating
5340 -- the variable, it is somewhere else.
5341
5342 Kill_Size_Check_Code (U_Ent);
5343 end;
5344
5345 -- Not a valid entity for an address clause
5346
5347 else
5348 Error_Msg_N ("address cannot be given for &", Nam);
5349 end if;
5350 end Address;
5351
5352 ---------------
5353 -- Alignment --
5354 ---------------
5355
5356 -- Alignment attribute definition clause
5357
5358 when Attribute_Alignment => Alignment : declare
5359 Align : constant Uint := Get_Alignment_Value (Expr);
5360 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
5361
5362 begin
5363 FOnly := True;
5364
5365 if not Is_Type (U_Ent)
5366 and then Ekind (U_Ent) /= E_Variable
5367 and then Ekind (U_Ent) /= E_Constant
5368 then
5369 Error_Msg_N ("alignment cannot be given for &", Nam);
5370
5371 elsif Duplicate_Clause then
5372 null;
5373
5374 elsif Align /= No_Uint then
5375 Set_Has_Alignment_Clause (U_Ent);
5376
5377 -- Tagged type case, check for attempt to set alignment to a
5378 -- value greater than Max_Align, and reset if so. This error
5379 -- is suppressed in ASIS mode to allow for different ASIS
5380 -- back ends or ASIS-based tools to query the illegal clause.
5381
5382 if Is_Tagged_Type (U_Ent)
5383 and then Align > Max_Align
5384 and then not ASIS_Mode
5385 then
5386 Error_Msg_N
5387 ("alignment for & set to Maximum_Aligment??", Nam);
5388 Set_Alignment (U_Ent, Max_Align);
5389
5390 -- All other cases
5391
5392 else
5393 Set_Alignment (U_Ent, Align);
5394 end if;
5395
5396 -- For an array type, U_Ent is the first subtype. In that case,
5397 -- also set the alignment of the anonymous base type so that
5398 -- other subtypes (such as the itypes for aggregates of the
5399 -- type) also receive the expected alignment.
5400
5401 if Is_Array_Type (U_Ent) then
5402 Set_Alignment (Base_Type (U_Ent), Align);
5403 end if;
5404 end if;
5405 end Alignment;
5406
5407 ---------------
5408 -- Bit_Order --
5409 ---------------
5410
5411 -- Bit_Order attribute definition clause
5412
5413 when Attribute_Bit_Order =>
5414 if not Is_Record_Type (U_Ent) then
5415 Error_Msg_N
5416 ("Bit_Order can only be defined for record type", Nam);
5417
5418 elsif Is_Tagged_Type (U_Ent) and then Is_Derived_Type (U_Ent) then
5419 Error_Msg_N
5420 ("Bit_Order cannot be defined for record extensions", Nam);
5421
5422 elsif Duplicate_Clause then
5423 null;
5424
5425 else
5426 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5427
5428 if Etype (Expr) = Any_Type then
5429 return;
5430
5431 elsif not Is_OK_Static_Expression (Expr) then
5432 Flag_Non_Static_Expr
5433 ("Bit_Order requires static expression!", Expr);
5434
5435 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5436 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
5437 end if;
5438 end if;
5439
5440 --------------------
5441 -- Component_Size --
5442 --------------------
5443
5444 -- Component_Size attribute definition clause
5445
5446 when Attribute_Component_Size => Component_Size_Case : declare
5447 Csize : constant Uint := Static_Integer (Expr);
5448 Ctyp : Entity_Id;
5449 Btype : Entity_Id;
5450 Biased : Boolean;
5451 New_Ctyp : Entity_Id;
5452 Decl : Node_Id;
5453
5454 begin
5455 if not Is_Array_Type (U_Ent) then
5456 Error_Msg_N ("component size requires array type", Nam);
5457 return;
5458 end if;
5459
5460 Btype := Base_Type (U_Ent);
5461 Ctyp := Component_Type (Btype);
5462
5463 if Duplicate_Clause then
5464 null;
5465
5466 elsif Rep_Item_Too_Early (Btype, N) then
5467 null;
5468
5469 elsif Csize /= No_Uint then
5470 Check_Size (Expr, Ctyp, Csize, Biased);
5471
5472 -- For the biased case, build a declaration for a subtype that
5473 -- will be used to represent the biased subtype that reflects
5474 -- the biased representation of components. We need the subtype
5475 -- to get proper conversions on referencing elements of the
5476 -- array.
5477
5478 if Biased then
5479 New_Ctyp :=
5480 Make_Defining_Identifier (Loc,
5481 Chars =>
5482 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
5483
5484 Decl :=
5485 Make_Subtype_Declaration (Loc,
5486 Defining_Identifier => New_Ctyp,
5487 Subtype_Indication =>
5488 New_Occurrence_Of (Component_Type (Btype), Loc));
5489
5490 Set_Parent (Decl, N);
5491 Analyze (Decl, Suppress => All_Checks);
5492
5493 Set_Has_Delayed_Freeze (New_Ctyp, False);
5494 Init_Esize (New_Ctyp);
5495 Set_RM_Size (New_Ctyp, Csize);
5496 Init_Alignment (New_Ctyp);
5497 Set_Is_Itype (New_Ctyp, True);
5498 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5499
5500 Set_Component_Type (Btype, New_Ctyp);
5501 Set_Biased (New_Ctyp, N, "component size clause");
5502 end if;
5503
5504 Set_Component_Size (Btype, Csize);
5505
5506 -- Deal with warning on overridden size
5507
5508 if Warn_On_Overridden_Size
5509 and then Has_Size_Clause (Ctyp)
5510 and then RM_Size (Ctyp) /= Csize
5511 then
5512 Error_Msg_NE
5513 ("component size overrides size clause for&?S?", N, Ctyp);
5514 end if;
5515
5516 Set_Has_Component_Size_Clause (Btype, True);
5517 Set_Has_Non_Standard_Rep (Btype, True);
5518 end if;
5519 end Component_Size_Case;
5520
5521 -----------------------
5522 -- Constant_Indexing --
5523 -----------------------
5524
5525 when Attribute_Constant_Indexing =>
5526 Check_Indexing_Functions;
5527
5528 ---------
5529 -- CPU --
5530 ---------
5531
5532 when Attribute_CPU =>
5533
5534 -- CPU attribute definition clause not allowed except from aspect
5535 -- specification.
5536
5537 if From_Aspect_Specification (N) then
5538 if not Is_Task_Type (U_Ent) then
5539 Error_Msg_N ("CPU can only be defined for task", Nam);
5540
5541 elsif Duplicate_Clause then
5542 null;
5543
5544 else
5545 -- The expression must be analyzed in the special manner
5546 -- described in "Handling of Default and Per-Object
5547 -- Expressions" in sem.ads.
5548
5549 -- The visibility to the components must be established
5550 -- and restored before and after analysis.
5551
5552 Push_Type (U_Ent);
5553 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5554 Pop_Type (U_Ent);
5555
5556 if not Is_OK_Static_Expression (Expr) then
5557 Check_Restriction (Static_Priorities, Expr);
5558 end if;
5559 end if;
5560
5561 else
5562 Error_Msg_N
5563 ("attribute& cannot be set with definition clause", N);
5564 end if;
5565
5566 ----------------------
5567 -- Default_Iterator --
5568 ----------------------
5569
5570 when Attribute_Default_Iterator => Default_Iterator : declare
5571 Func : Entity_Id;
5572 Typ : Entity_Id;
5573
5574 begin
5575 -- If target type is untagged, further checks are irrelevant
5576
5577 if not Is_Tagged_Type (U_Ent) then
5578 Error_Msg_N
5579 ("aspect Default_Iterator applies to tagged type", Nam);
5580 return;
5581 end if;
5582
5583 Check_Iterator_Functions;
5584
5585 Analyze (Expr);
5586
5587 if not Is_Entity_Name (Expr)
5588 or else Ekind (Entity (Expr)) /= E_Function
5589 then
5590 Error_Msg_N ("aspect Iterator must be a function", Expr);
5591 return;
5592 else
5593 Func := Entity (Expr);
5594 end if;
5595
5596 -- The type of the first parameter must be T, T'class, or a
5597 -- corresponding access type (5.5.1 (8/3). If function is
5598 -- parameterless label type accordingly.
5599
5600 if No (First_Formal (Func)) then
5601 Typ := Any_Type;
5602 else
5603 Typ := Etype (First_Formal (Func));
5604 end if;
5605
5606 if Typ = U_Ent
5607 or else Typ = Class_Wide_Type (U_Ent)
5608 or else (Is_Access_Type (Typ)
5609 and then Designated_Type (Typ) = U_Ent)
5610 or else (Is_Access_Type (Typ)
5611 and then Designated_Type (Typ) =
5612 Class_Wide_Type (U_Ent))
5613 then
5614 null;
5615
5616 else
5617 Error_Msg_NE
5618 ("Default Iterator must be a primitive of&", Func, U_Ent);
5619 end if;
5620 end Default_Iterator;
5621
5622 ------------------------
5623 -- Dispatching_Domain --
5624 ------------------------
5625
5626 when Attribute_Dispatching_Domain =>
5627
5628 -- Dispatching_Domain attribute definition clause not allowed
5629 -- except from aspect specification.
5630
5631 if From_Aspect_Specification (N) then
5632 if not Is_Task_Type (U_Ent) then
5633 Error_Msg_N
5634 ("Dispatching_Domain can only be defined for task", Nam);
5635
5636 elsif Duplicate_Clause then
5637 null;
5638
5639 else
5640 -- The expression must be analyzed in the special manner
5641 -- described in "Handling of Default and Per-Object
5642 -- Expressions" in sem.ads.
5643
5644 -- The visibility to the components must be restored
5645
5646 Push_Type (U_Ent);
5647
5648 Preanalyze_Spec_Expression
5649 (Expr, RTE (RE_Dispatching_Domain));
5650
5651 Pop_Type (U_Ent);
5652 end if;
5653
5654 else
5655 Error_Msg_N
5656 ("attribute& cannot be set with definition clause", N);
5657 end if;
5658
5659 ------------------
5660 -- External_Tag --
5661 ------------------
5662
5663 when Attribute_External_Tag =>
5664 if not Is_Tagged_Type (U_Ent) then
5665 Error_Msg_N ("should be a tagged type", Nam);
5666 end if;
5667
5668 if Duplicate_Clause then
5669 null;
5670
5671 else
5672 Analyze_And_Resolve (Expr, Standard_String);
5673
5674 if not Is_OK_Static_Expression (Expr) then
5675 Flag_Non_Static_Expr
5676 ("static string required for tag name!", Nam);
5677 end if;
5678
5679 if not Is_Library_Level_Entity (U_Ent) then
5680 Error_Msg_NE
5681 ("??non-unique external tag supplied for &", N, U_Ent);
5682 Error_Msg_N
5683 ("\??same external tag applies to all subprogram calls",
5684 N);
5685 Error_Msg_N
5686 ("\??corresponding internal tag cannot be obtained", N);
5687 end if;
5688 end if;
5689
5690 --------------------------
5691 -- Implicit_Dereference --
5692 --------------------------
5693
5694 when Attribute_Implicit_Dereference =>
5695
5696 -- Legality checks already performed at the point of the type
5697 -- declaration, aspect is not delayed.
5698
5699 null;
5700
5701 -----------
5702 -- Input --
5703 -----------
5704
5705 when Attribute_Input =>
5706 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5707 Set_Has_Specified_Stream_Input (Ent);
5708
5709 ------------------------
5710 -- Interrupt_Priority --
5711 ------------------------
5712
5713 when Attribute_Interrupt_Priority =>
5714
5715 -- Interrupt_Priority attribute definition clause not allowed
5716 -- except from aspect specification.
5717
5718 if From_Aspect_Specification (N) then
5719 if not Is_Concurrent_Type (U_Ent) then
5720 Error_Msg_N
5721 ("Interrupt_Priority can only be defined for task and "
5722 & "protected object", Nam);
5723
5724 elsif Duplicate_Clause then
5725 null;
5726
5727 else
5728 -- The expression must be analyzed in the special manner
5729 -- described in "Handling of Default and Per-Object
5730 -- Expressions" in sem.ads.
5731
5732 -- The visibility to the components must be restored
5733
5734 Push_Type (U_Ent);
5735
5736 Preanalyze_Spec_Expression
5737 (Expr, RTE (RE_Interrupt_Priority));
5738
5739 Pop_Type (U_Ent);
5740
5741 -- Check the No_Task_At_Interrupt_Priority restriction
5742
5743 if Is_Task_Type (U_Ent) then
5744 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5745 end if;
5746 end if;
5747
5748 else
5749 Error_Msg_N
5750 ("attribute& cannot be set with definition clause", N);
5751 end if;
5752
5753 --------------
5754 -- Iterable --
5755 --------------
5756
5757 when Attribute_Iterable =>
5758 Analyze (Expr);
5759
5760 if Nkind (Expr) /= N_Aggregate then
5761 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5762 end if;
5763
5764 declare
5765 Assoc : Node_Id;
5766
5767 begin
5768 Assoc := First (Component_Associations (Expr));
5769 while Present (Assoc) loop
5770 Analyze (Expression (Assoc));
5771
5772 if not Is_Entity_Name (Expression (Assoc)) then
5773 Error_Msg_N ("value must be a function", Assoc);
5774 end if;
5775
5776 Next (Assoc);
5777 end loop;
5778 end;
5779
5780 ----------------------
5781 -- Iterator_Element --
5782 ----------------------
5783
5784 when Attribute_Iterator_Element =>
5785 Analyze (Expr);
5786
5787 if not Is_Entity_Name (Expr)
5788 or else not Is_Type (Entity (Expr))
5789 then
5790 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5791 end if;
5792
5793 -------------------
5794 -- Machine_Radix --
5795 -------------------
5796
5797 -- Machine radix attribute definition clause
5798
5799 when Attribute_Machine_Radix => Machine_Radix : declare
5800 Radix : constant Uint := Static_Integer (Expr);
5801
5802 begin
5803 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5804 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5805
5806 elsif Duplicate_Clause then
5807 null;
5808
5809 elsif Radix /= No_Uint then
5810 Set_Has_Machine_Radix_Clause (U_Ent);
5811 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5812
5813 if Radix = 2 then
5814 null;
5815
5816 elsif Radix = 10 then
5817 Set_Machine_Radix_10 (U_Ent);
5818
5819 -- The following error is suppressed in ASIS mode to allow for
5820 -- different ASIS back ends or ASIS-based tools to query the
5821 -- illegal clause.
5822
5823 elsif not ASIS_Mode then
5824 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5825 end if;
5826 end if;
5827 end Machine_Radix;
5828
5829 -----------------
5830 -- Object_Size --
5831 -----------------
5832
5833 -- Object_Size attribute definition clause
5834
5835 when Attribute_Object_Size => Object_Size : declare
5836 Size : constant Uint := Static_Integer (Expr);
5837
5838 Biased : Boolean;
5839 pragma Warnings (Off, Biased);
5840
5841 begin
5842 if not Is_Type (U_Ent) then
5843 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5844
5845 elsif Duplicate_Clause then
5846 null;
5847
5848 else
5849 Check_Size (Expr, U_Ent, Size, Biased);
5850
5851 -- The following errors are suppressed in ASIS mode to allow
5852 -- for different ASIS back ends or ASIS-based tools to query
5853 -- the illegal clause.
5854
5855 if ASIS_Mode then
5856 null;
5857
5858 elsif Size <= 0 then
5859 Error_Msg_N ("Object_Size must be positive", Expr);
5860
5861 elsif Is_Scalar_Type (U_Ent) then
5862 if Size /= 8 and then Size /= 16 and then Size /= 32
5863 and then UI_Mod (Size, 64) /= 0
5864 then
5865 Error_Msg_N
5866 ("Object_Size must be 8, 16, 32, or multiple of 64",
5867 Expr);
5868 end if;
5869
5870 elsif Size mod 8 /= 0 then
5871 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5872 end if;
5873
5874 Set_Esize (U_Ent, Size);
5875 Set_Has_Object_Size_Clause (U_Ent);
5876 Alignment_Check_For_Size_Change (U_Ent, Size);
5877 end if;
5878 end Object_Size;
5879
5880 ------------
5881 -- Output --
5882 ------------
5883
5884 when Attribute_Output =>
5885 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5886 Set_Has_Specified_Stream_Output (Ent);
5887
5888 --------------
5889 -- Priority --
5890 --------------
5891
5892 when Attribute_Priority =>
5893
5894 -- Priority attribute definition clause not allowed except from
5895 -- aspect specification.
5896
5897 if From_Aspect_Specification (N) then
5898 if not (Is_Concurrent_Type (U_Ent)
5899 or else Ekind (U_Ent) = E_Procedure)
5900 then
5901 Error_Msg_N
5902 ("Priority can only be defined for task and protected "
5903 & "object", Nam);
5904
5905 elsif Duplicate_Clause then
5906 null;
5907
5908 else
5909 -- The expression must be analyzed in the special manner
5910 -- described in "Handling of Default and Per-Object
5911 -- Expressions" in sem.ads.
5912
5913 -- The visibility to the components must be restored
5914
5915 Push_Type (U_Ent);
5916 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5917 Pop_Type (U_Ent);
5918
5919 if not Is_OK_Static_Expression (Expr) then
5920 Check_Restriction (Static_Priorities, Expr);
5921 end if;
5922 end if;
5923
5924 else
5925 Error_Msg_N
5926 ("attribute& cannot be set with definition clause", N);
5927 end if;
5928
5929 ----------
5930 -- Read --
5931 ----------
5932
5933 when Attribute_Read =>
5934 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5935 Set_Has_Specified_Stream_Read (Ent);
5936
5937 --------------------------
5938 -- Scalar_Storage_Order --
5939 --------------------------
5940
5941 -- Scalar_Storage_Order attribute definition clause
5942
5943 when Attribute_Scalar_Storage_Order =>
5944 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5945 Error_Msg_N
5946 ("Scalar_Storage_Order can only be defined for record or "
5947 & "array type", Nam);
5948
5949 elsif Duplicate_Clause then
5950 null;
5951
5952 else
5953 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5954
5955 if Etype (Expr) = Any_Type then
5956 return;
5957
5958 elsif not Is_OK_Static_Expression (Expr) then
5959 Flag_Non_Static_Expr
5960 ("Scalar_Storage_Order requires static expression!", Expr);
5961
5962 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5963
5964 -- Here for the case of a non-default (i.e. non-confirming)
5965 -- Scalar_Storage_Order attribute definition.
5966
5967 if Support_Nondefault_SSO_On_Target then
5968 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5969 else
5970 Error_Msg_N
5971 ("non-default Scalar_Storage_Order not supported on "
5972 & "target", Expr);
5973 end if;
5974 end if;
5975
5976 -- Clear SSO default indications since explicit setting of the
5977 -- order overrides the defaults.
5978
5979 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5980 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5981 end if;
5982
5983 ----------
5984 -- Size --
5985 ----------
5986
5987 -- Size attribute definition clause
5988
5989 when Attribute_Size => Size : declare
5990 Size : constant Uint := Static_Integer (Expr);
5991 Etyp : Entity_Id;
5992 Biased : Boolean;
5993
5994 begin
5995 FOnly := True;
5996
5997 if Duplicate_Clause then
5998 null;
5999
6000 elsif not Is_Type (U_Ent)
6001 and then Ekind (U_Ent) /= E_Variable
6002 and then Ekind (U_Ent) /= E_Constant
6003 then
6004 Error_Msg_N ("size cannot be given for &", Nam);
6005
6006 elsif Is_Array_Type (U_Ent)
6007 and then not Is_Constrained (U_Ent)
6008 then
6009 Error_Msg_N
6010 ("size cannot be given for unconstrained array", Nam);
6011
6012 elsif Size /= No_Uint then
6013 if Is_Type (U_Ent) then
6014 Etyp := U_Ent;
6015 else
6016 Etyp := Etype (U_Ent);
6017 end if;
6018
6019 -- Check size, note that Gigi is in charge of checking that the
6020 -- size of an array or record type is OK. Also we do not check
6021 -- the size in the ordinary fixed-point case, since it is too
6022 -- early to do so (there may be subsequent small clause that
6023 -- affects the size). We can check the size if a small clause
6024 -- has already been given.
6025
6026 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
6027 or else Has_Small_Clause (U_Ent)
6028 then
6029 Check_Size (Expr, Etyp, Size, Biased);
6030 Set_Biased (U_Ent, N, "size clause", Biased);
6031 end if;
6032
6033 -- For types set RM_Size and Esize if possible
6034
6035 if Is_Type (U_Ent) then
6036 Set_RM_Size (U_Ent, Size);
6037
6038 -- For elementary types, increase Object_Size to power of 2,
6039 -- but not less than a storage unit in any case (normally
6040 -- this means it will be byte addressable).
6041
6042 -- For all other types, nothing else to do, we leave Esize
6043 -- (object size) unset, the back end will set it from the
6044 -- size and alignment in an appropriate manner.
6045
6046 -- In both cases, we check whether the alignment must be
6047 -- reset in the wake of the size change.
6048
6049 if Is_Elementary_Type (U_Ent) then
6050 if Size <= System_Storage_Unit then
6051 Init_Esize (U_Ent, System_Storage_Unit);
6052 elsif Size <= 16 then
6053 Init_Esize (U_Ent, 16);
6054 elsif Size <= 32 then
6055 Init_Esize (U_Ent, 32);
6056 else
6057 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
6058 end if;
6059
6060 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
6061 else
6062 Alignment_Check_For_Size_Change (U_Ent, Size);
6063 end if;
6064
6065 -- For objects, set Esize only
6066
6067 else
6068 -- The following error is suppressed in ASIS mode to allow
6069 -- for different ASIS back ends or ASIS-based tools to query
6070 -- the illegal clause.
6071
6072 if Is_Elementary_Type (Etyp)
6073 and then Size /= System_Storage_Unit
6074 and then Size /= System_Storage_Unit * 2
6075 and then Size /= System_Storage_Unit * 4
6076 and then Size /= System_Storage_Unit * 8
6077 and then not ASIS_Mode
6078 then
6079 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6080 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
6081 Error_Msg_N
6082 ("size for primitive object must be a power of 2 in "
6083 & "the range ^-^", N);
6084 end if;
6085
6086 Set_Esize (U_Ent, Size);
6087 end if;
6088
6089 Set_Has_Size_Clause (U_Ent);
6090 end if;
6091 end Size;
6092
6093 -----------
6094 -- Small --
6095 -----------
6096
6097 -- Small attribute definition clause
6098
6099 when Attribute_Small => Small : declare
6100 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
6101 Small : Ureal;
6102
6103 begin
6104 Analyze_And_Resolve (Expr, Any_Real);
6105
6106 if Etype (Expr) = Any_Type then
6107 return;
6108
6109 elsif not Is_OK_Static_Expression (Expr) then
6110 Flag_Non_Static_Expr
6111 ("small requires static expression!", Expr);
6112 return;
6113
6114 else
6115 Small := Expr_Value_R (Expr);
6116
6117 if Small <= Ureal_0 then
6118 Error_Msg_N ("small value must be greater than zero", Expr);
6119 return;
6120 end if;
6121
6122 end if;
6123
6124 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
6125 Error_Msg_N
6126 ("small requires an ordinary fixed point type", Nam);
6127
6128 elsif Has_Small_Clause (U_Ent) then
6129 Error_Msg_N ("small already given for &", Nam);
6130
6131 elsif Small > Delta_Value (U_Ent) then
6132 Error_Msg_N
6133 ("small value must not be greater than delta value", Nam);
6134
6135 else
6136 Set_Small_Value (U_Ent, Small);
6137 Set_Small_Value (Implicit_Base, Small);
6138 Set_Has_Small_Clause (U_Ent);
6139 Set_Has_Small_Clause (Implicit_Base);
6140 Set_Has_Non_Standard_Rep (Implicit_Base);
6141 end if;
6142 end Small;
6143
6144 ------------------
6145 -- Storage_Pool --
6146 ------------------
6147
6148 -- Storage_Pool attribute definition clause
6149
6150 when Attribute_Simple_Storage_Pool
6151 | Attribute_Storage_Pool
6152 =>
6153 Storage_Pool : declare
6154 Pool : Entity_Id;
6155 T : Entity_Id;
6156
6157 begin
6158 if Ekind (U_Ent) = E_Access_Subprogram_Type then
6159 Error_Msg_N
6160 ("storage pool cannot be given for access-to-subprogram type",
6161 Nam);
6162 return;
6163
6164 elsif not Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
6165 then
6166 Error_Msg_N
6167 ("storage pool can only be given for access types", Nam);
6168 return;
6169
6170 elsif Is_Derived_Type (U_Ent) then
6171 Error_Msg_N
6172 ("storage pool cannot be given for a derived access type",
6173 Nam);
6174
6175 elsif Duplicate_Clause then
6176 return;
6177
6178 elsif Present (Associated_Storage_Pool (U_Ent)) then
6179 Error_Msg_N ("storage pool already given for &", Nam);
6180 return;
6181 end if;
6182
6183 -- Check for Storage_Size previously given
6184
6185 declare
6186 SS : constant Node_Id :=
6187 Get_Attribute_Definition_Clause
6188 (U_Ent, Attribute_Storage_Size);
6189 begin
6190 if Present (SS) then
6191 Check_Pool_Size_Clash (U_Ent, N, SS);
6192 end if;
6193 end;
6194
6195 -- Storage_Pool case
6196
6197 if Id = Attribute_Storage_Pool then
6198 Analyze_And_Resolve
6199 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6200
6201 -- In the Simple_Storage_Pool case, we allow a variable of any
6202 -- simple storage pool type, so we Resolve without imposing an
6203 -- expected type.
6204
6205 else
6206 Analyze_And_Resolve (Expr);
6207
6208 if not Present (Get_Rep_Pragma
6209 (Etype (Expr), Name_Simple_Storage_Pool_Type))
6210 then
6211 Error_Msg_N
6212 ("expression must be of a simple storage pool type", Expr);
6213 end if;
6214 end if;
6215
6216 if not Denotes_Variable (Expr) then
6217 Error_Msg_N ("storage pool must be a variable", Expr);
6218 return;
6219 end if;
6220
6221 if Nkind (Expr) = N_Type_Conversion then
6222 T := Etype (Expression (Expr));
6223 else
6224 T := Etype (Expr);
6225 end if;
6226
6227 -- The Stack_Bounded_Pool is used internally for implementing
6228 -- access types with a Storage_Size. Since it only work properly
6229 -- when used on one specific type, we need to check that it is not
6230 -- hijacked improperly:
6231
6232 -- type T is access Integer;
6233 -- for T'Storage_Size use n;
6234 -- type Q is access Float;
6235 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
6236
6237 if RTE_Available (RE_Stack_Bounded_Pool)
6238 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
6239 then
6240 Error_Msg_N ("non-shareable internal Pool", Expr);
6241 return;
6242 end if;
6243
6244 -- If the argument is a name that is not an entity name, then
6245 -- we construct a renaming operation to define an entity of
6246 -- type storage pool.
6247
6248 if not Is_Entity_Name (Expr)
6249 and then Is_Object_Reference (Expr)
6250 then
6251 Pool := Make_Temporary (Loc, 'P', Expr);
6252
6253 declare
6254 Rnode : constant Node_Id :=
6255 Make_Object_Renaming_Declaration (Loc,
6256 Defining_Identifier => Pool,
6257 Subtype_Mark =>
6258 New_Occurrence_Of (Etype (Expr), Loc),
6259 Name => Expr);
6260
6261 begin
6262 -- If the attribute definition clause comes from an aspect
6263 -- clause, then insert the renaming before the associated
6264 -- entity's declaration, since the attribute clause has
6265 -- not yet been appended to the declaration list.
6266
6267 if From_Aspect_Specification (N) then
6268 Insert_Before (Parent (Entity (N)), Rnode);
6269 else
6270 Insert_Before (N, Rnode);
6271 end if;
6272
6273 Analyze (Rnode);
6274 Set_Associated_Storage_Pool (U_Ent, Pool);
6275 end;
6276
6277 elsif Is_Entity_Name (Expr) then
6278 Pool := Entity (Expr);
6279
6280 -- If pool is a renamed object, get original one. This can
6281 -- happen with an explicit renaming, and within instances.
6282
6283 while Present (Renamed_Object (Pool))
6284 and then Is_Entity_Name (Renamed_Object (Pool))
6285 loop
6286 Pool := Entity (Renamed_Object (Pool));
6287 end loop;
6288
6289 if Present (Renamed_Object (Pool))
6290 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
6291 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
6292 then
6293 Pool := Entity (Expression (Renamed_Object (Pool)));
6294 end if;
6295
6296 Set_Associated_Storage_Pool (U_Ent, Pool);
6297
6298 elsif Nkind (Expr) = N_Type_Conversion
6299 and then Is_Entity_Name (Expression (Expr))
6300 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
6301 then
6302 Pool := Entity (Expression (Expr));
6303 Set_Associated_Storage_Pool (U_Ent, Pool);
6304
6305 else
6306 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
6307 return;
6308 end if;
6309 end Storage_Pool;
6310
6311 ------------------
6312 -- Storage_Size --
6313 ------------------
6314
6315 -- Storage_Size attribute definition clause
6316
6317 when Attribute_Storage_Size => Storage_Size : declare
6318 Btype : constant Entity_Id := Base_Type (U_Ent);
6319
6320 begin
6321 if Is_Task_Type (U_Ent) then
6322
6323 -- Check obsolescent (but never obsolescent if from aspect)
6324
6325 if not From_Aspect_Specification (N) then
6326 Check_Restriction (No_Obsolescent_Features, N);
6327
6328 if Warn_On_Obsolescent_Feature then
6329 Error_Msg_N
6330 ("?j?storage size clause for task is an obsolescent "
6331 & "feature (RM J.9)", N);
6332 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
6333 end if;
6334 end if;
6335
6336 FOnly := True;
6337 end if;
6338
6339 if not Is_Access_Type (U_Ent)
6340 and then Ekind (U_Ent) /= E_Task_Type
6341 then
6342 Error_Msg_N ("storage size cannot be given for &", Nam);
6343
6344 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6345 Error_Msg_N
6346 ("storage size cannot be given for a derived access type",
6347 Nam);
6348
6349 elsif Duplicate_Clause then
6350 null;
6351
6352 else
6353 Analyze_And_Resolve (Expr, Any_Integer);
6354
6355 if Is_Access_Type (U_Ent) then
6356
6357 -- Check for Storage_Pool previously given
6358
6359 declare
6360 SP : constant Node_Id :=
6361 Get_Attribute_Definition_Clause
6362 (U_Ent, Attribute_Storage_Pool);
6363
6364 begin
6365 if Present (SP) then
6366 Check_Pool_Size_Clash (U_Ent, SP, N);
6367 end if;
6368 end;
6369
6370 -- Special case of for x'Storage_Size use 0
6371
6372 if Is_OK_Static_Expression (Expr)
6373 and then Expr_Value (Expr) = 0
6374 then
6375 Set_No_Pool_Assigned (Btype);
6376 end if;
6377 end if;
6378
6379 Set_Has_Storage_Size_Clause (Btype);
6380 end if;
6381 end Storage_Size;
6382
6383 -----------------
6384 -- Stream_Size --
6385 -----------------
6386
6387 when Attribute_Stream_Size => Stream_Size : declare
6388 Size : constant Uint := Static_Integer (Expr);
6389
6390 begin
6391 if Ada_Version <= Ada_95 then
6392 Check_Restriction (No_Implementation_Attributes, N);
6393 end if;
6394
6395 if Duplicate_Clause then
6396 null;
6397
6398 elsif Is_Elementary_Type (U_Ent) then
6399
6400 -- The following errors are suppressed in ASIS mode to allow
6401 -- for different ASIS back ends or ASIS-based tools to query
6402 -- the illegal clause.
6403
6404 if ASIS_Mode then
6405 null;
6406
6407 elsif Size /= System_Storage_Unit
6408 and then Size /= System_Storage_Unit * 2
6409 and then Size /= System_Storage_Unit * 4
6410 and then Size /= System_Storage_Unit * 8
6411 then
6412 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6413 Error_Msg_N
6414 ("stream size for elementary type must be a power of 2 "
6415 & "and at least ^", N);
6416
6417 elsif RM_Size (U_Ent) > Size then
6418 Error_Msg_Uint_1 := RM_Size (U_Ent);
6419 Error_Msg_N
6420 ("stream size for elementary type must be a power of 2 "
6421 & "and at least ^", N);
6422 end if;
6423
6424 Set_Has_Stream_Size_Clause (U_Ent);
6425
6426 else
6427 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6428 end if;
6429 end Stream_Size;
6430
6431 ----------------
6432 -- Value_Size --
6433 ----------------
6434
6435 -- Value_Size attribute definition clause
6436
6437 when Attribute_Value_Size => Value_Size : declare
6438 Size : constant Uint := Static_Integer (Expr);
6439 Biased : Boolean;
6440
6441 begin
6442 if not Is_Type (U_Ent) then
6443 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6444
6445 elsif Duplicate_Clause then
6446 null;
6447
6448 elsif Is_Array_Type (U_Ent)
6449 and then not Is_Constrained (U_Ent)
6450 then
6451 Error_Msg_N
6452 ("Value_Size cannot be given for unconstrained array", Nam);
6453
6454 else
6455 if Is_Elementary_Type (U_Ent) then
6456 Check_Size (Expr, U_Ent, Size, Biased);
6457 Set_Biased (U_Ent, N, "value size clause", Biased);
6458 end if;
6459
6460 Set_RM_Size (U_Ent, Size);
6461 end if;
6462 end Value_Size;
6463
6464 -----------------------
6465 -- Variable_Indexing --
6466 -----------------------
6467
6468 when Attribute_Variable_Indexing =>
6469 Check_Indexing_Functions;
6470
6471 -----------
6472 -- Write --
6473 -----------
6474
6475 when Attribute_Write =>
6476 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6477 Set_Has_Specified_Stream_Write (Ent);
6478
6479 -- All other attributes cannot be set
6480
6481 when others =>
6482 Error_Msg_N
6483 ("attribute& cannot be set with definition clause", N);
6484 end case;
6485
6486 -- The test for the type being frozen must be performed after any
6487 -- expression the clause has been analyzed since the expression itself
6488 -- might cause freezing that makes the clause illegal.
6489
6490 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6491 return;
6492 end if;
6493 end Analyze_Attribute_Definition_Clause;
6494
6495 ----------------------------
6496 -- Analyze_Code_Statement --
6497 ----------------------------
6498
6499 procedure Analyze_Code_Statement (N : Node_Id) is
6500 HSS : constant Node_Id := Parent (N);
6501 SBody : constant Node_Id := Parent (HSS);
6502 Subp : constant Entity_Id := Current_Scope;
6503 Stmt : Node_Id;
6504 Decl : Node_Id;
6505 StmtO : Node_Id;
6506 DeclO : Node_Id;
6507
6508 begin
6509 -- Accept foreign code statements for CodePeer. The analysis is skipped
6510 -- to avoid rejecting unrecognized constructs.
6511
6512 if CodePeer_Mode then
6513 Set_Analyzed (N);
6514 return;
6515 end if;
6516
6517 -- Analyze and check we get right type, note that this implements the
6518 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6519 -- the only way that Asm_Insn could possibly be visible.
6520
6521 Analyze_And_Resolve (Expression (N));
6522
6523 if Etype (Expression (N)) = Any_Type then
6524 return;
6525 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6526 Error_Msg_N ("incorrect type for code statement", N);
6527 return;
6528 end if;
6529
6530 Check_Code_Statement (N);
6531
6532 -- Make sure we appear in the handled statement sequence of a subprogram
6533 -- (RM 13.8(3)).
6534
6535 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6536 or else Nkind (SBody) /= N_Subprogram_Body
6537 then
6538 Error_Msg_N
6539 ("code statement can only appear in body of subprogram", N);
6540 return;
6541 end if;
6542
6543 -- Do remaining checks (RM 13.8(3)) if not already done
6544
6545 if not Is_Machine_Code_Subprogram (Subp) then
6546 Set_Is_Machine_Code_Subprogram (Subp);
6547
6548 -- No exception handlers allowed
6549
6550 if Present (Exception_Handlers (HSS)) then
6551 Error_Msg_N
6552 ("exception handlers not permitted in machine code subprogram",
6553 First (Exception_Handlers (HSS)));
6554 end if;
6555
6556 -- No declarations other than use clauses and pragmas (we allow
6557 -- certain internally generated declarations as well).
6558
6559 Decl := First (Declarations (SBody));
6560 while Present (Decl) loop
6561 DeclO := Original_Node (Decl);
6562 if Comes_From_Source (DeclO)
6563 and not Nkind_In (DeclO, N_Pragma,
6564 N_Use_Package_Clause,
6565 N_Use_Type_Clause,
6566 N_Implicit_Label_Declaration)
6567 then
6568 Error_Msg_N
6569 ("this declaration not allowed in machine code subprogram",
6570 DeclO);
6571 end if;
6572
6573 Next (Decl);
6574 end loop;
6575
6576 -- No statements other than code statements, pragmas, and labels.
6577 -- Again we allow certain internally generated statements.
6578
6579 -- In Ada 2012, qualified expressions are names, and the code
6580 -- statement is initially parsed as a procedure call.
6581
6582 Stmt := First (Statements (HSS));
6583 while Present (Stmt) loop
6584 StmtO := Original_Node (Stmt);
6585
6586 -- A procedure call transformed into a code statement is OK
6587
6588 if Ada_Version >= Ada_2012
6589 and then Nkind (StmtO) = N_Procedure_Call_Statement
6590 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6591 then
6592 null;
6593
6594 elsif Comes_From_Source (StmtO)
6595 and then not Nkind_In (StmtO, N_Pragma,
6596 N_Label,
6597 N_Code_Statement)
6598 then
6599 Error_Msg_N
6600 ("this statement is not allowed in machine code subprogram",
6601 StmtO);
6602 end if;
6603
6604 Next (Stmt);
6605 end loop;
6606 end if;
6607 end Analyze_Code_Statement;
6608
6609 -----------------------------------------------
6610 -- Analyze_Enumeration_Representation_Clause --
6611 -----------------------------------------------
6612
6613 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6614 Ident : constant Node_Id := Identifier (N);
6615 Aggr : constant Node_Id := Array_Aggregate (N);
6616 Enumtype : Entity_Id;
6617 Elit : Entity_Id;
6618 Expr : Node_Id;
6619 Assoc : Node_Id;
6620 Choice : Node_Id;
6621 Val : Uint;
6622
6623 Err : Boolean := False;
6624 -- Set True to avoid cascade errors and crashes on incorrect source code
6625
6626 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6627 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6628 -- Allowed range of universal integer (= allowed range of enum lit vals)
6629
6630 Min : Uint;
6631 Max : Uint;
6632 -- Minimum and maximum values of entries
6633
6634 Max_Node : Node_Id := Empty; -- init to avoid warning
6635 -- Pointer to node for literal providing max value
6636
6637 begin
6638 if Ignore_Rep_Clauses then
6639 Kill_Rep_Clause (N);
6640 return;
6641 end if;
6642
6643 -- Ignore enumeration rep clauses by default in CodePeer mode,
6644 -- unless -gnatd.I is specified, as a work around for potential false
6645 -- positive messages.
6646
6647 if CodePeer_Mode and not Debug_Flag_Dot_II then
6648 return;
6649 end if;
6650
6651 -- First some basic error checks
6652
6653 Find_Type (Ident);
6654 Enumtype := Entity (Ident);
6655
6656 if Enumtype = Any_Type
6657 or else Rep_Item_Too_Early (Enumtype, N)
6658 then
6659 return;
6660 else
6661 Enumtype := Underlying_Type (Enumtype);
6662 end if;
6663
6664 if not Is_Enumeration_Type (Enumtype) then
6665 Error_Msg_NE
6666 ("enumeration type required, found}",
6667 Ident, First_Subtype (Enumtype));
6668 return;
6669 end if;
6670
6671 -- Ignore rep clause on generic actual type. This will already have
6672 -- been flagged on the template as an error, and this is the safest
6673 -- way to ensure we don't get a junk cascaded message in the instance.
6674
6675 if Is_Generic_Actual_Type (Enumtype) then
6676 return;
6677
6678 -- Type must be in current scope
6679
6680 elsif Scope (Enumtype) /= Current_Scope then
6681 Error_Msg_N ("type must be declared in this scope", Ident);
6682 return;
6683
6684 -- Type must be a first subtype
6685
6686 elsif not Is_First_Subtype (Enumtype) then
6687 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6688 return;
6689
6690 -- Ignore duplicate rep clause
6691
6692 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6693 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6694 return;
6695
6696 -- Don't allow rep clause for standard [wide_[wide_]]character
6697
6698 elsif Is_Standard_Character_Type (Enumtype) then
6699 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6700 return;
6701
6702 -- Check that the expression is a proper aggregate (no parentheses)
6703
6704 elsif Paren_Count (Aggr) /= 0 then
6705 Error_Msg
6706 ("extra parentheses surrounding aggregate not allowed",
6707 First_Sloc (Aggr));
6708 return;
6709
6710 -- All tests passed, so set rep clause in place
6711
6712 else
6713 Set_Has_Enumeration_Rep_Clause (Enumtype);
6714 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6715 end if;
6716
6717 -- Now we process the aggregate. Note that we don't use the normal
6718 -- aggregate code for this purpose, because we don't want any of the
6719 -- normal expansion activities, and a number of special semantic
6720 -- rules apply (including the component type being any integer type)
6721
6722 Elit := First_Literal (Enumtype);
6723
6724 -- First the positional entries if any
6725
6726 if Present (Expressions (Aggr)) then
6727 Expr := First (Expressions (Aggr));
6728 while Present (Expr) loop
6729 if No (Elit) then
6730 Error_Msg_N ("too many entries in aggregate", Expr);
6731 return;
6732 end if;
6733
6734 Val := Static_Integer (Expr);
6735
6736 -- Err signals that we found some incorrect entries processing
6737 -- the list. The final checks for completeness and ordering are
6738 -- skipped in this case.
6739
6740 if Val = No_Uint then
6741 Err := True;
6742
6743 elsif Val < Lo or else Hi < Val then
6744 Error_Msg_N ("value outside permitted range", Expr);
6745 Err := True;
6746 end if;
6747
6748 Set_Enumeration_Rep (Elit, Val);
6749 Set_Enumeration_Rep_Expr (Elit, Expr);
6750 Next (Expr);
6751 Next (Elit);
6752 end loop;
6753 end if;
6754
6755 -- Now process the named entries if present
6756
6757 if Present (Component_Associations (Aggr)) then
6758 Assoc := First (Component_Associations (Aggr));
6759 while Present (Assoc) loop
6760 Choice := First (Choices (Assoc));
6761
6762 if Present (Next (Choice)) then
6763 Error_Msg_N
6764 ("multiple choice not allowed here", Next (Choice));
6765 Err := True;
6766 end if;
6767
6768 if Nkind (Choice) = N_Others_Choice then
6769 Error_Msg_N ("others choice not allowed here", Choice);
6770 Err := True;
6771
6772 elsif Nkind (Choice) = N_Range then
6773
6774 -- ??? should allow zero/one element range here
6775
6776 Error_Msg_N ("range not allowed here", Choice);
6777 Err := True;
6778
6779 else
6780 Analyze_And_Resolve (Choice, Enumtype);
6781
6782 if Error_Posted (Choice) then
6783 Err := True;
6784 end if;
6785
6786 if not Err then
6787 if Is_Entity_Name (Choice)
6788 and then Is_Type (Entity (Choice))
6789 then
6790 Error_Msg_N ("subtype name not allowed here", Choice);
6791 Err := True;
6792
6793 -- ??? should allow static subtype with zero/one entry
6794
6795 elsif Etype (Choice) = Base_Type (Enumtype) then
6796 if not Is_OK_Static_Expression (Choice) then
6797 Flag_Non_Static_Expr
6798 ("non-static expression used for choice!", Choice);
6799 Err := True;
6800
6801 else
6802 Elit := Expr_Value_E (Choice);
6803
6804 if Present (Enumeration_Rep_Expr (Elit)) then
6805 Error_Msg_Sloc :=
6806 Sloc (Enumeration_Rep_Expr (Elit));
6807 Error_Msg_NE
6808 ("representation for& previously given#",
6809 Choice, Elit);
6810 Err := True;
6811 end if;
6812
6813 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6814
6815 Expr := Expression (Assoc);
6816 Val := Static_Integer (Expr);
6817
6818 if Val = No_Uint then
6819 Err := True;
6820
6821 elsif Val < Lo or else Hi < Val then
6822 Error_Msg_N ("value outside permitted range", Expr);
6823 Err := True;
6824 end if;
6825
6826 Set_Enumeration_Rep (Elit, Val);
6827 end if;
6828 end if;
6829 end if;
6830 end if;
6831
6832 Next (Assoc);
6833 end loop;
6834 end if;
6835
6836 -- Aggregate is fully processed. Now we check that a full set of
6837 -- representations was given, and that they are in range and in order.
6838 -- These checks are only done if no other errors occurred.
6839
6840 if not Err then
6841 Min := No_Uint;
6842 Max := No_Uint;
6843
6844 Elit := First_Literal (Enumtype);
6845 while Present (Elit) loop
6846 if No (Enumeration_Rep_Expr (Elit)) then
6847 Error_Msg_NE ("missing representation for&!", N, Elit);
6848
6849 else
6850 Val := Enumeration_Rep (Elit);
6851
6852 if Min = No_Uint then
6853 Min := Val;
6854 end if;
6855
6856 if Val /= No_Uint then
6857 if Max /= No_Uint and then Val <= Max then
6858 Error_Msg_NE
6859 ("enumeration value for& not ordered!",
6860 Enumeration_Rep_Expr (Elit), Elit);
6861 end if;
6862
6863 Max_Node := Enumeration_Rep_Expr (Elit);
6864 Max := Val;
6865 end if;
6866
6867 -- If there is at least one literal whose representation is not
6868 -- equal to the Pos value, then note that this enumeration type
6869 -- has a non-standard representation.
6870
6871 if Val /= Enumeration_Pos (Elit) then
6872 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6873 end if;
6874 end if;
6875
6876 Next (Elit);
6877 end loop;
6878
6879 -- Now set proper size information
6880
6881 declare
6882 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6883
6884 begin
6885 if Has_Size_Clause (Enumtype) then
6886
6887 -- All OK, if size is OK now
6888
6889 if RM_Size (Enumtype) >= Minsize then
6890 null;
6891
6892 else
6893 -- Try if we can get by with biasing
6894
6895 Minsize :=
6896 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6897
6898 -- Error message if even biasing does not work
6899
6900 if RM_Size (Enumtype) < Minsize then
6901 Error_Msg_Uint_1 := RM_Size (Enumtype);
6902 Error_Msg_Uint_2 := Max;
6903 Error_Msg_N
6904 ("previously given size (^) is too small "
6905 & "for this value (^)", Max_Node);
6906
6907 -- If biasing worked, indicate that we now have biased rep
6908
6909 else
6910 Set_Biased
6911 (Enumtype, Size_Clause (Enumtype), "size clause");
6912 end if;
6913 end if;
6914
6915 else
6916 Set_RM_Size (Enumtype, Minsize);
6917 Set_Enum_Esize (Enumtype);
6918 end if;
6919
6920 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6921 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6922 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6923 end;
6924 end if;
6925
6926 -- We repeat the too late test in case it froze itself
6927
6928 if Rep_Item_Too_Late (Enumtype, N) then
6929 null;
6930 end if;
6931 end Analyze_Enumeration_Representation_Clause;
6932
6933 ----------------------------
6934 -- Analyze_Free_Statement --
6935 ----------------------------
6936
6937 procedure Analyze_Free_Statement (N : Node_Id) is
6938 begin
6939 Analyze (Expression (N));
6940 end Analyze_Free_Statement;
6941
6942 ---------------------------
6943 -- Analyze_Freeze_Entity --
6944 ---------------------------
6945
6946 procedure Analyze_Freeze_Entity (N : Node_Id) is
6947 begin
6948 Freeze_Entity_Checks (N);
6949 end Analyze_Freeze_Entity;
6950
6951 -----------------------------------
6952 -- Analyze_Freeze_Generic_Entity --
6953 -----------------------------------
6954
6955 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6956 E : constant Entity_Id := Entity (N);
6957
6958 begin
6959 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
6960 Analyze_Aspects_At_Freeze_Point (E);
6961 end if;
6962
6963 Freeze_Entity_Checks (N);
6964 end Analyze_Freeze_Generic_Entity;
6965
6966 ------------------------------------------
6967 -- Analyze_Record_Representation_Clause --
6968 ------------------------------------------
6969
6970 -- Note: we check as much as we can here, but we can't do any checks
6971 -- based on the position values (e.g. overlap checks) until freeze time
6972 -- because especially in Ada 2005 (machine scalar mode), the processing
6973 -- for non-standard bit order can substantially change the positions.
6974 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6975 -- for the remainder of this processing.
6976
6977 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6978 Ident : constant Node_Id := Identifier (N);
6979 Biased : Boolean;
6980 CC : Node_Id;
6981 Comp : Entity_Id;
6982 Fbit : Uint;
6983 Lbit : Uint;
6984 Ocomp : Entity_Id;
6985 Posit : Uint;
6986 Rectype : Entity_Id;
6987 Recdef : Node_Id;
6988
6989 function Is_Inherited (Comp : Entity_Id) return Boolean;
6990 -- True if Comp is an inherited component in a record extension
6991
6992 ------------------
6993 -- Is_Inherited --
6994 ------------------
6995
6996 function Is_Inherited (Comp : Entity_Id) return Boolean is
6997 Comp_Base : Entity_Id;
6998
6999 begin
7000 if Ekind (Rectype) = E_Record_Subtype then
7001 Comp_Base := Original_Record_Component (Comp);
7002 else
7003 Comp_Base := Comp;
7004 end if;
7005
7006 return Comp_Base /= Original_Record_Component (Comp_Base);
7007 end Is_Inherited;
7008
7009 -- Local variables
7010
7011 Is_Record_Extension : Boolean;
7012 -- True if Rectype is a record extension
7013
7014 CR_Pragma : Node_Id := Empty;
7015 -- Points to N_Pragma node if Complete_Representation pragma present
7016
7017 -- Start of processing for Analyze_Record_Representation_Clause
7018
7019 begin
7020 if Ignore_Rep_Clauses then
7021 Kill_Rep_Clause (N);
7022 return;
7023 end if;
7024
7025 Find_Type (Ident);
7026 Rectype := Entity (Ident);
7027
7028 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
7029 return;
7030 else
7031 Rectype := Underlying_Type (Rectype);
7032 end if;
7033
7034 -- First some basic error checks
7035
7036 if not Is_Record_Type (Rectype) then
7037 Error_Msg_NE
7038 ("record type required, found}", Ident, First_Subtype (Rectype));
7039 return;
7040
7041 elsif Scope (Rectype) /= Current_Scope then
7042 Error_Msg_N ("type must be declared in this scope", N);
7043 return;
7044
7045 elsif not Is_First_Subtype (Rectype) then
7046 Error_Msg_N ("cannot give record rep clause for subtype", N);
7047 return;
7048
7049 elsif Has_Record_Rep_Clause (Rectype) then
7050 Error_Msg_N ("duplicate record rep clause ignored", N);
7051 return;
7052
7053 elsif Rep_Item_Too_Late (Rectype, N) then
7054 return;
7055 end if;
7056
7057 -- We know we have a first subtype, now possibly go to the anonymous
7058 -- base type to determine whether Rectype is a record extension.
7059
7060 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
7061 Is_Record_Extension :=
7062 Nkind (Recdef) = N_Derived_Type_Definition
7063 and then Present (Record_Extension_Part (Recdef));
7064
7065 if Present (Mod_Clause (N)) then
7066 declare
7067 Loc : constant Source_Ptr := Sloc (N);
7068 M : constant Node_Id := Mod_Clause (N);
7069 P : constant List_Id := Pragmas_Before (M);
7070 AtM_Nod : Node_Id;
7071
7072 Mod_Val : Uint;
7073 pragma Warnings (Off, Mod_Val);
7074
7075 begin
7076 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
7077
7078 if Warn_On_Obsolescent_Feature then
7079 Error_Msg_N
7080 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
7081 Error_Msg_N
7082 ("\?j?use alignment attribute definition clause instead", N);
7083 end if;
7084
7085 if Present (P) then
7086 Analyze_List (P);
7087 end if;
7088
7089 -- In ASIS_Mode mode, expansion is disabled, but we must convert
7090 -- the Mod clause into an alignment clause anyway, so that the
7091 -- back end can compute and back-annotate properly the size and
7092 -- alignment of types that may include this record.
7093
7094 -- This seems dubious, this destroys the source tree in a manner
7095 -- not detectable by ASIS ???
7096
7097 if Operating_Mode = Check_Semantics and then ASIS_Mode then
7098 AtM_Nod :=
7099 Make_Attribute_Definition_Clause (Loc,
7100 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
7101 Chars => Name_Alignment,
7102 Expression => Relocate_Node (Expression (M)));
7103
7104 Set_From_At_Mod (AtM_Nod);
7105 Insert_After (N, AtM_Nod);
7106 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
7107 Set_Mod_Clause (N, Empty);
7108
7109 else
7110 -- Get the alignment value to perform error checking
7111
7112 Mod_Val := Get_Alignment_Value (Expression (M));
7113 end if;
7114 end;
7115 end if;
7116
7117 -- For untagged types, clear any existing component clauses for the
7118 -- type. If the type is derived, this is what allows us to override
7119 -- a rep clause for the parent. For type extensions, the representation
7120 -- of the inherited components is inherited, so we want to keep previous
7121 -- component clauses for completeness.
7122
7123 if not Is_Tagged_Type (Rectype) then
7124 Comp := First_Component_Or_Discriminant (Rectype);
7125 while Present (Comp) loop
7126 Set_Component_Clause (Comp, Empty);
7127 Next_Component_Or_Discriminant (Comp);
7128 end loop;
7129 end if;
7130
7131 -- All done if no component clauses
7132
7133 CC := First (Component_Clauses (N));
7134
7135 if No (CC) then
7136 return;
7137 end if;
7138
7139 -- A representation like this applies to the base type
7140
7141 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
7142 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
7143 Set_Has_Specified_Layout (Base_Type (Rectype));
7144
7145 -- Process the component clauses
7146
7147 while Present (CC) loop
7148
7149 -- Pragma
7150
7151 if Nkind (CC) = N_Pragma then
7152 Analyze (CC);
7153
7154 -- The only pragma of interest is Complete_Representation
7155
7156 if Pragma_Name (CC) = Name_Complete_Representation then
7157 CR_Pragma := CC;
7158 end if;
7159
7160 -- Processing for real component clause
7161
7162 else
7163 Posit := Static_Integer (Position (CC));
7164 Fbit := Static_Integer (First_Bit (CC));
7165 Lbit := Static_Integer (Last_Bit (CC));
7166
7167 if Posit /= No_Uint
7168 and then Fbit /= No_Uint
7169 and then Lbit /= No_Uint
7170 then
7171 if Posit < 0 then
7172 Error_Msg_N ("position cannot be negative", Position (CC));
7173
7174 elsif Fbit < 0 then
7175 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
7176
7177 -- The Last_Bit specified in a component clause must not be
7178 -- less than the First_Bit minus one (RM-13.5.1(10)).
7179
7180 elsif Lbit < Fbit - 1 then
7181 Error_Msg_N
7182 ("last bit cannot be less than first bit minus one",
7183 Last_Bit (CC));
7184
7185 -- Values look OK, so find the corresponding record component
7186 -- Even though the syntax allows an attribute reference for
7187 -- implementation-defined components, GNAT does not allow the
7188 -- tag to get an explicit position.
7189
7190 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
7191 if Attribute_Name (Component_Name (CC)) = Name_Tag then
7192 Error_Msg_N ("position of tag cannot be specified", CC);
7193 else
7194 Error_Msg_N ("illegal component name", CC);
7195 end if;
7196
7197 else
7198 Comp := First_Entity (Rectype);
7199 while Present (Comp) loop
7200 exit when Chars (Comp) = Chars (Component_Name (CC));
7201 Next_Entity (Comp);
7202 end loop;
7203
7204 if No (Comp) then
7205
7206 -- Maybe component of base type that is absent from
7207 -- statically constrained first subtype.
7208
7209 Comp := First_Entity (Base_Type (Rectype));
7210 while Present (Comp) loop
7211 exit when Chars (Comp) = Chars (Component_Name (CC));
7212 Next_Entity (Comp);
7213 end loop;
7214 end if;
7215
7216 if No (Comp) then
7217 Error_Msg_N
7218 ("component clause is for non-existent field", CC);
7219
7220 -- Ada 2012 (AI05-0026): Any name that denotes a
7221 -- discriminant of an object of an unchecked union type
7222 -- shall not occur within a record_representation_clause.
7223
7224 -- The general restriction of using record rep clauses on
7225 -- Unchecked_Union types has now been lifted. Since it is
7226 -- possible to introduce a record rep clause which mentions
7227 -- the discriminant of an Unchecked_Union in non-Ada 2012
7228 -- code, this check is applied to all versions of the
7229 -- language.
7230
7231 elsif Ekind (Comp) = E_Discriminant
7232 and then Is_Unchecked_Union (Rectype)
7233 then
7234 Error_Msg_N
7235 ("cannot reference discriminant of unchecked union",
7236 Component_Name (CC));
7237
7238 elsif Is_Record_Extension and then Is_Inherited (Comp) then
7239 Error_Msg_NE
7240 ("component clause not allowed for inherited "
7241 & "component&", CC, Comp);
7242
7243 elsif Present (Component_Clause (Comp)) then
7244
7245 -- Diagnose duplicate rep clause, or check consistency
7246 -- if this is an inherited component. In a double fault,
7247 -- there may be a duplicate inconsistent clause for an
7248 -- inherited component.
7249
7250 if Scope (Original_Record_Component (Comp)) = Rectype
7251 or else Parent (Component_Clause (Comp)) = N
7252 then
7253 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
7254 Error_Msg_N ("component clause previously given#", CC);
7255
7256 else
7257 declare
7258 Rep1 : constant Node_Id := Component_Clause (Comp);
7259 begin
7260 if Intval (Position (Rep1)) /=
7261 Intval (Position (CC))
7262 or else Intval (First_Bit (Rep1)) /=
7263 Intval (First_Bit (CC))
7264 or else Intval (Last_Bit (Rep1)) /=
7265 Intval (Last_Bit (CC))
7266 then
7267 Error_Msg_N
7268 ("component clause inconsistent with "
7269 & "representation of ancestor", CC);
7270
7271 elsif Warn_On_Redundant_Constructs then
7272 Error_Msg_N
7273 ("?r?redundant confirming component clause "
7274 & "for component!", CC);
7275 end if;
7276 end;
7277 end if;
7278
7279 -- Normal case where this is the first component clause we
7280 -- have seen for this entity, so set it up properly.
7281
7282 else
7283 -- Make reference for field in record rep clause and set
7284 -- appropriate entity field in the field identifier.
7285
7286 Generate_Reference
7287 (Comp, Component_Name (CC), Set_Ref => False);
7288 Set_Entity (Component_Name (CC), Comp);
7289
7290 -- Update Fbit and Lbit to the actual bit number
7291
7292 Fbit := Fbit + UI_From_Int (SSU) * Posit;
7293 Lbit := Lbit + UI_From_Int (SSU) * Posit;
7294
7295 if Has_Size_Clause (Rectype)
7296 and then RM_Size (Rectype) <= Lbit
7297 then
7298 Error_Msg_N
7299 ("bit number out of range of specified size",
7300 Last_Bit (CC));
7301 else
7302 Set_Component_Clause (Comp, CC);
7303 Set_Component_Bit_Offset (Comp, Fbit);
7304 Set_Esize (Comp, 1 + (Lbit - Fbit));
7305 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
7306 Set_Normalized_Position (Comp, Fbit / SSU);
7307
7308 Set_Normalized_Position_Max
7309 (Comp, Normalized_Position (Comp));
7310
7311 if Warn_On_Overridden_Size
7312 and then Has_Size_Clause (Etype (Comp))
7313 and then RM_Size (Etype (Comp)) /= Esize (Comp)
7314 then
7315 Error_Msg_NE
7316 ("?S?component size overrides size clause for&",
7317 Component_Name (CC), Etype (Comp));
7318 end if;
7319
7320 Check_Size
7321 (Component_Name (CC),
7322 Etype (Comp),
7323 Esize (Comp),
7324 Biased);
7325
7326 Set_Biased
7327 (Comp, First_Node (CC), "component clause", Biased);
7328
7329 -- This information is also set in the corresponding
7330 -- component of the base type, found by accessing the
7331 -- Original_Record_Component link if it is present.
7332
7333 Ocomp := Original_Record_Component (Comp);
7334
7335 if Present (Ocomp) and then Ocomp /= Comp then
7336 Set_Component_Clause (Ocomp, CC);
7337 Set_Component_Bit_Offset (Ocomp, Fbit);
7338 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7339 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
7340 Set_Normalized_Position (Ocomp, Fbit / SSU);
7341
7342 Set_Normalized_Position_Max
7343 (Ocomp, Normalized_Position (Ocomp));
7344
7345 -- Note: we don't use Set_Biased here, because we
7346 -- already gave a warning above if needed, and we
7347 -- would get a duplicate for the same name here.
7348
7349 Set_Has_Biased_Representation
7350 (Ocomp, Has_Biased_Representation (Comp));
7351 end if;
7352
7353 if Esize (Comp) < 0 then
7354 Error_Msg_N ("component size is negative", CC);
7355 end if;
7356 end if;
7357 end if;
7358 end if;
7359 end if;
7360 end if;
7361
7362 Next (CC);
7363 end loop;
7364
7365 -- Check missing components if Complete_Representation pragma appeared
7366
7367 if Present (CR_Pragma) then
7368 Comp := First_Component_Or_Discriminant (Rectype);
7369 while Present (Comp) loop
7370 if No (Component_Clause (Comp)) then
7371 Error_Msg_NE
7372 ("missing component clause for &", CR_Pragma, Comp);
7373 end if;
7374
7375 Next_Component_Or_Discriminant (Comp);
7376 end loop;
7377
7378 -- Give missing components warning if required
7379
7380 elsif Warn_On_Unrepped_Components then
7381 declare
7382 Num_Repped_Components : Nat := 0;
7383 Num_Unrepped_Components : Nat := 0;
7384
7385 begin
7386 -- First count number of repped and unrepped components
7387
7388 Comp := First_Component_Or_Discriminant (Rectype);
7389 while Present (Comp) loop
7390 if Present (Component_Clause (Comp)) then
7391 Num_Repped_Components := Num_Repped_Components + 1;
7392 else
7393 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7394 end if;
7395
7396 Next_Component_Or_Discriminant (Comp);
7397 end loop;
7398
7399 -- We are only interested in the case where there is at least one
7400 -- unrepped component, and at least half the components have rep
7401 -- clauses. We figure that if less than half have them, then the
7402 -- partial rep clause is really intentional. If the component
7403 -- type has no underlying type set at this point (as for a generic
7404 -- formal type), we don't know enough to give a warning on the
7405 -- component.
7406
7407 if Num_Unrepped_Components > 0
7408 and then Num_Unrepped_Components < Num_Repped_Components
7409 then
7410 Comp := First_Component_Or_Discriminant (Rectype);
7411 while Present (Comp) loop
7412 if No (Component_Clause (Comp))
7413 and then Comes_From_Source (Comp)
7414 and then Present (Underlying_Type (Etype (Comp)))
7415 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
7416 or else Size_Known_At_Compile_Time
7417 (Underlying_Type (Etype (Comp))))
7418 and then not Has_Warnings_Off (Rectype)
7419
7420 -- Ignore discriminant in unchecked union, since it is
7421 -- not there, and cannot have a component clause.
7422
7423 and then (not Is_Unchecked_Union (Rectype)
7424 or else Ekind (Comp) /= E_Discriminant)
7425 then
7426 Error_Msg_Sloc := Sloc (Comp);
7427 Error_Msg_NE
7428 ("?C?no component clause given for & declared #",
7429 N, Comp);
7430 end if;
7431
7432 Next_Component_Or_Discriminant (Comp);
7433 end loop;
7434 end if;
7435 end;
7436 end if;
7437 end Analyze_Record_Representation_Clause;
7438
7439 -------------------------------------
7440 -- Build_Discrete_Static_Predicate --
7441 -------------------------------------
7442
7443 procedure Build_Discrete_Static_Predicate
7444 (Typ : Entity_Id;
7445 Expr : Node_Id;
7446 Nam : Name_Id)
7447 is
7448 Loc : constant Source_Ptr := Sloc (Expr);
7449
7450 Non_Static : exception;
7451 -- Raised if something non-static is found
7452
7453 Btyp : constant Entity_Id := Base_Type (Typ);
7454
7455 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7456 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7457 -- Low bound and high bound value of base type of Typ
7458
7459 TLo : Uint;
7460 THi : Uint;
7461 -- Bounds for constructing the static predicate. We use the bound of the
7462 -- subtype if it is static, otherwise the corresponding base type bound.
7463 -- Note: a non-static subtype can have a static predicate.
7464
7465 type REnt is record
7466 Lo, Hi : Uint;
7467 end record;
7468 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7469 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7470 -- value.
7471
7472 type RList is array (Nat range <>) of REnt;
7473 -- A list of ranges. The ranges are sorted in increasing order, and are
7474 -- disjoint (there is a gap of at least one value between each range in
7475 -- the table). A value is in the set of ranges in Rlist if it lies
7476 -- within one of these ranges.
7477
7478 False_Range : constant RList :=
7479 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7480 -- An empty set of ranges represents a range list that can never be
7481 -- satisfied, since there are no ranges in which the value could lie,
7482 -- so it does not lie in any of them. False_Range is a canonical value
7483 -- for this empty set, but general processing should test for an Rlist
7484 -- with length zero (see Is_False predicate), since other null ranges
7485 -- may appear which must be treated as False.
7486
7487 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7488 -- Range representing True, value must be in the base range
7489
7490 function "and" (Left : RList; Right : RList) return RList;
7491 -- And's together two range lists, returning a range list. This is a set
7492 -- intersection operation.
7493
7494 function "or" (Left : RList; Right : RList) return RList;
7495 -- Or's together two range lists, returning a range list. This is a set
7496 -- union operation.
7497
7498 function "not" (Right : RList) return RList;
7499 -- Returns complement of a given range list, i.e. a range list
7500 -- representing all the values in TLo .. THi that are not in the input
7501 -- operand Right.
7502
7503 function Build_Val (V : Uint) return Node_Id;
7504 -- Return an analyzed N_Identifier node referencing this value, suitable
7505 -- for use as an entry in the Static_Discrte_Predicate list. This node
7506 -- is typed with the base type.
7507
7508 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7509 -- Return an analyzed N_Range node referencing this range, suitable for
7510 -- use as an entry in the Static_Discrete_Predicate list. This node is
7511 -- typed with the base type.
7512
7513 function Get_RList (Exp : Node_Id) return RList;
7514 -- This is a recursive routine that converts the given expression into a
7515 -- list of ranges, suitable for use in building the static predicate.
7516
7517 function Is_False (R : RList) return Boolean;
7518 pragma Inline (Is_False);
7519 -- Returns True if the given range list is empty, and thus represents a
7520 -- False list of ranges that can never be satisfied.
7521
7522 function Is_True (R : RList) return Boolean;
7523 -- Returns True if R trivially represents the True predicate by having a
7524 -- single range from BLo to BHi.
7525
7526 function Is_Type_Ref (N : Node_Id) return Boolean;
7527 pragma Inline (Is_Type_Ref);
7528 -- Returns if True if N is a reference to the type for the predicate in
7529 -- the expression (i.e. if it is an identifier whose Chars field matches
7530 -- the Nam given in the call). N must not be parenthesized, if the type
7531 -- name appears in parens, this routine will return False.
7532
7533 function Lo_Val (N : Node_Id) return Uint;
7534 -- Given an entry from a Static_Discrete_Predicate list that is either
7535 -- a static expression or static range, gets either the expression value
7536 -- or the low bound of the range.
7537
7538 function Hi_Val (N : Node_Id) return Uint;
7539 -- Given an entry from a Static_Discrete_Predicate list that is either
7540 -- a static expression or static range, gets either the expression value
7541 -- or the high bound of the range.
7542
7543 function Membership_Entry (N : Node_Id) return RList;
7544 -- Given a single membership entry (range, value, or subtype), returns
7545 -- the corresponding range list. Raises Static_Error if not static.
7546
7547 function Membership_Entries (N : Node_Id) return RList;
7548 -- Given an element on an alternatives list of a membership operation,
7549 -- returns the range list corresponding to this entry and all following
7550 -- entries (i.e. returns the "or" of this list of values).
7551
7552 function Stat_Pred (Typ : Entity_Id) return RList;
7553 -- Given a type, if it has a static predicate, then return the predicate
7554 -- as a range list, otherwise raise Non_Static.
7555
7556 -----------
7557 -- "and" --
7558 -----------
7559
7560 function "and" (Left : RList; Right : RList) return RList is
7561 FEnt : REnt;
7562 -- First range of result
7563
7564 SLeft : Nat := Left'First;
7565 -- Start of rest of left entries
7566
7567 SRight : Nat := Right'First;
7568 -- Start of rest of right entries
7569
7570 begin
7571 -- If either range is True, return the other
7572
7573 if Is_True (Left) then
7574 return Right;
7575 elsif Is_True (Right) then
7576 return Left;
7577 end if;
7578
7579 -- If either range is False, return False
7580
7581 if Is_False (Left) or else Is_False (Right) then
7582 return False_Range;
7583 end if;
7584
7585 -- Loop to remove entries at start that are disjoint, and thus just
7586 -- get discarded from the result entirely.
7587
7588 loop
7589 -- If no operands left in either operand, result is false
7590
7591 if SLeft > Left'Last or else SRight > Right'Last then
7592 return False_Range;
7593
7594 -- Discard first left operand entry if disjoint with right
7595
7596 elsif Left (SLeft).Hi < Right (SRight).Lo then
7597 SLeft := SLeft + 1;
7598
7599 -- Discard first right operand entry if disjoint with left
7600
7601 elsif Right (SRight).Hi < Left (SLeft).Lo then
7602 SRight := SRight + 1;
7603
7604 -- Otherwise we have an overlapping entry
7605
7606 else
7607 exit;
7608 end if;
7609 end loop;
7610
7611 -- Now we have two non-null operands, and first entries overlap. The
7612 -- first entry in the result will be the overlapping part of these
7613 -- two entries.
7614
7615 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7616 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7617
7618 -- Now we can remove the entry that ended at a lower value, since its
7619 -- contribution is entirely contained in Fent.
7620
7621 if Left (SLeft).Hi <= Right (SRight).Hi then
7622 SLeft := SLeft + 1;
7623 else
7624 SRight := SRight + 1;
7625 end if;
7626
7627 -- Compute result by concatenating this first entry with the "and" of
7628 -- the remaining parts of the left and right operands. Note that if
7629 -- either of these is empty, "and" will yield empty, so that we will
7630 -- end up with just Fent, which is what we want in that case.
7631
7632 return
7633 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7634 end "and";
7635
7636 -----------
7637 -- "not" --
7638 -----------
7639
7640 function "not" (Right : RList) return RList is
7641 begin
7642 -- Return True if False range
7643
7644 if Is_False (Right) then
7645 return True_Range;
7646 end if;
7647
7648 -- Return False if True range
7649
7650 if Is_True (Right) then
7651 return False_Range;
7652 end if;
7653
7654 -- Here if not trivial case
7655
7656 declare
7657 Result : RList (1 .. Right'Length + 1);
7658 -- May need one more entry for gap at beginning and end
7659
7660 Count : Nat := 0;
7661 -- Number of entries stored in Result
7662
7663 begin
7664 -- Gap at start
7665
7666 if Right (Right'First).Lo > TLo then
7667 Count := Count + 1;
7668 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7669 end if;
7670
7671 -- Gaps between ranges
7672
7673 for J in Right'First .. Right'Last - 1 loop
7674 Count := Count + 1;
7675 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7676 end loop;
7677
7678 -- Gap at end
7679
7680 if Right (Right'Last).Hi < THi then
7681 Count := Count + 1;
7682 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7683 end if;
7684
7685 return Result (1 .. Count);
7686 end;
7687 end "not";
7688
7689 ----------
7690 -- "or" --
7691 ----------
7692
7693 function "or" (Left : RList; Right : RList) return RList is
7694 FEnt : REnt;
7695 -- First range of result
7696
7697 SLeft : Nat := Left'First;
7698 -- Start of rest of left entries
7699
7700 SRight : Nat := Right'First;
7701 -- Start of rest of right entries
7702
7703 begin
7704 -- If either range is True, return True
7705
7706 if Is_True (Left) or else Is_True (Right) then
7707 return True_Range;
7708 end if;
7709
7710 -- If either range is False (empty), return the other
7711
7712 if Is_False (Left) then
7713 return Right;
7714 elsif Is_False (Right) then
7715 return Left;
7716 end if;
7717
7718 -- Initialize result first entry from left or right operand depending
7719 -- on which starts with the lower range.
7720
7721 if Left (SLeft).Lo < Right (SRight).Lo then
7722 FEnt := Left (SLeft);
7723 SLeft := SLeft + 1;
7724 else
7725 FEnt := Right (SRight);
7726 SRight := SRight + 1;
7727 end if;
7728
7729 -- This loop eats ranges from left and right operands that are
7730 -- contiguous with the first range we are gathering.
7731
7732 loop
7733 -- Eat first entry in left operand if contiguous or overlapped by
7734 -- gathered first operand of result.
7735
7736 if SLeft <= Left'Last
7737 and then Left (SLeft).Lo <= FEnt.Hi + 1
7738 then
7739 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7740 SLeft := SLeft + 1;
7741
7742 -- Eat first entry in right operand if contiguous or overlapped by
7743 -- gathered right operand of result.
7744
7745 elsif SRight <= Right'Last
7746 and then Right (SRight).Lo <= FEnt.Hi + 1
7747 then
7748 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7749 SRight := SRight + 1;
7750
7751 -- All done if no more entries to eat
7752
7753 else
7754 exit;
7755 end if;
7756 end loop;
7757
7758 -- Obtain result as the first entry we just computed, concatenated
7759 -- to the "or" of the remaining results (if one operand is empty,
7760 -- this will just concatenate with the other
7761
7762 return
7763 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7764 end "or";
7765
7766 -----------------
7767 -- Build_Range --
7768 -----------------
7769
7770 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7771 Result : Node_Id;
7772 begin
7773 Result :=
7774 Make_Range (Loc,
7775 Low_Bound => Build_Val (Lo),
7776 High_Bound => Build_Val (Hi));
7777 Set_Etype (Result, Btyp);
7778 Set_Analyzed (Result);
7779 return Result;
7780 end Build_Range;
7781
7782 ---------------
7783 -- Build_Val --
7784 ---------------
7785
7786 function Build_Val (V : Uint) return Node_Id is
7787 Result : Node_Id;
7788
7789 begin
7790 if Is_Enumeration_Type (Typ) then
7791 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7792 else
7793 Result := Make_Integer_Literal (Loc, V);
7794 end if;
7795
7796 Set_Etype (Result, Btyp);
7797 Set_Is_Static_Expression (Result);
7798 Set_Analyzed (Result);
7799 return Result;
7800 end Build_Val;
7801
7802 ---------------
7803 -- Get_RList --
7804 ---------------
7805
7806 function Get_RList (Exp : Node_Id) return RList is
7807 Op : Node_Kind;
7808 Val : Uint;
7809
7810 begin
7811 -- Static expression can only be true or false
7812
7813 if Is_OK_Static_Expression (Exp) then
7814 if Expr_Value (Exp) = 0 then
7815 return False_Range;
7816 else
7817 return True_Range;
7818 end if;
7819 end if;
7820
7821 -- Otherwise test node type
7822
7823 Op := Nkind (Exp);
7824
7825 case Op is
7826
7827 -- And
7828
7829 when N_And_Then
7830 | N_Op_And
7831 =>
7832 return Get_RList (Left_Opnd (Exp))
7833 and
7834 Get_RList (Right_Opnd (Exp));
7835
7836 -- Or
7837
7838 when N_Op_Or
7839 | N_Or_Else
7840 =>
7841 return Get_RList (Left_Opnd (Exp))
7842 or
7843 Get_RList (Right_Opnd (Exp));
7844
7845 -- Not
7846
7847 when N_Op_Not =>
7848 return not Get_RList (Right_Opnd (Exp));
7849
7850 -- Comparisons of type with static value
7851
7852 when N_Op_Compare =>
7853
7854 -- Type is left operand
7855
7856 if Is_Type_Ref (Left_Opnd (Exp))
7857 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7858 then
7859 Val := Expr_Value (Right_Opnd (Exp));
7860
7861 -- Typ is right operand
7862
7863 elsif Is_Type_Ref (Right_Opnd (Exp))
7864 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7865 then
7866 Val := Expr_Value (Left_Opnd (Exp));
7867
7868 -- Invert sense of comparison
7869
7870 case Op is
7871 when N_Op_Gt => Op := N_Op_Lt;
7872 when N_Op_Lt => Op := N_Op_Gt;
7873 when N_Op_Ge => Op := N_Op_Le;
7874 when N_Op_Le => Op := N_Op_Ge;
7875 when others => null;
7876 end case;
7877
7878 -- Other cases are non-static
7879
7880 else
7881 raise Non_Static;
7882 end if;
7883
7884 -- Construct range according to comparison operation
7885
7886 case Op is
7887 when N_Op_Eq =>
7888 return RList'(1 => REnt'(Val, Val));
7889
7890 when N_Op_Ge =>
7891 return RList'(1 => REnt'(Val, BHi));
7892
7893 when N_Op_Gt =>
7894 return RList'(1 => REnt'(Val + 1, BHi));
7895
7896 when N_Op_Le =>
7897 return RList'(1 => REnt'(BLo, Val));
7898
7899 when N_Op_Lt =>
7900 return RList'(1 => REnt'(BLo, Val - 1));
7901
7902 when N_Op_Ne =>
7903 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7904
7905 when others =>
7906 raise Program_Error;
7907 end case;
7908
7909 -- Membership (IN)
7910
7911 when N_In =>
7912 if not Is_Type_Ref (Left_Opnd (Exp)) then
7913 raise Non_Static;
7914 end if;
7915
7916 if Present (Right_Opnd (Exp)) then
7917 return Membership_Entry (Right_Opnd (Exp));
7918 else
7919 return Membership_Entries (First (Alternatives (Exp)));
7920 end if;
7921
7922 -- Negative membership (NOT IN)
7923
7924 when N_Not_In =>
7925 if not Is_Type_Ref (Left_Opnd (Exp)) then
7926 raise Non_Static;
7927 end if;
7928
7929 if Present (Right_Opnd (Exp)) then
7930 return not Membership_Entry (Right_Opnd (Exp));
7931 else
7932 return not Membership_Entries (First (Alternatives (Exp)));
7933 end if;
7934
7935 -- Function call, may be call to static predicate
7936
7937 when N_Function_Call =>
7938 if Is_Entity_Name (Name (Exp)) then
7939 declare
7940 Ent : constant Entity_Id := Entity (Name (Exp));
7941 begin
7942 if Is_Predicate_Function (Ent)
7943 or else
7944 Is_Predicate_Function_M (Ent)
7945 then
7946 return Stat_Pred (Etype (First_Formal (Ent)));
7947 end if;
7948 end;
7949 end if;
7950
7951 -- Other function call cases are non-static
7952
7953 raise Non_Static;
7954
7955 -- Qualified expression, dig out the expression
7956
7957 when N_Qualified_Expression =>
7958 return Get_RList (Expression (Exp));
7959
7960 when N_Case_Expression =>
7961 declare
7962 Alt : Node_Id;
7963 Choices : List_Id;
7964 Dep : Node_Id;
7965
7966 begin
7967 if not Is_Entity_Name (Expression (Expr))
7968 or else Etype (Expression (Expr)) /= Typ
7969 then
7970 Error_Msg_N
7971 ("expression must denaote subtype", Expression (Expr));
7972 return False_Range;
7973 end if;
7974
7975 -- Collect discrete choices in all True alternatives
7976
7977 Choices := New_List;
7978 Alt := First (Alternatives (Exp));
7979 while Present (Alt) loop
7980 Dep := Expression (Alt);
7981
7982 if not Is_OK_Static_Expression (Dep) then
7983 raise Non_Static;
7984
7985 elsif Is_True (Expr_Value (Dep)) then
7986 Append_List_To (Choices,
7987 New_Copy_List (Discrete_Choices (Alt)));
7988 end if;
7989
7990 Next (Alt);
7991 end loop;
7992
7993 return Membership_Entries (First (Choices));
7994 end;
7995
7996 -- Expression with actions: if no actions, dig out expression
7997
7998 when N_Expression_With_Actions =>
7999 if Is_Empty_List (Actions (Exp)) then
8000 return Get_RList (Expression (Exp));
8001 else
8002 raise Non_Static;
8003 end if;
8004
8005 -- Xor operator
8006
8007 when N_Op_Xor =>
8008 return (Get_RList (Left_Opnd (Exp))
8009 and not Get_RList (Right_Opnd (Exp)))
8010 or (Get_RList (Right_Opnd (Exp))
8011 and not Get_RList (Left_Opnd (Exp)));
8012
8013 -- Any other node type is non-static
8014
8015 when others =>
8016 raise Non_Static;
8017 end case;
8018 end Get_RList;
8019
8020 ------------
8021 -- Hi_Val --
8022 ------------
8023
8024 function Hi_Val (N : Node_Id) return Uint is
8025 begin
8026 if Is_OK_Static_Expression (N) then
8027 return Expr_Value (N);
8028 else
8029 pragma Assert (Nkind (N) = N_Range);
8030 return Expr_Value (High_Bound (N));
8031 end if;
8032 end Hi_Val;
8033
8034 --------------
8035 -- Is_False --
8036 --------------
8037
8038 function Is_False (R : RList) return Boolean is
8039 begin
8040 return R'Length = 0;
8041 end Is_False;
8042
8043 -------------
8044 -- Is_True --
8045 -------------
8046
8047 function Is_True (R : RList) return Boolean is
8048 begin
8049 return R'Length = 1
8050 and then R (R'First).Lo = BLo
8051 and then R (R'First).Hi = BHi;
8052 end Is_True;
8053
8054 -----------------
8055 -- Is_Type_Ref --
8056 -----------------
8057
8058 function Is_Type_Ref (N : Node_Id) return Boolean is
8059 begin
8060 return Nkind (N) = N_Identifier
8061 and then Chars (N) = Nam
8062 and then Paren_Count (N) = 0;
8063 end Is_Type_Ref;
8064
8065 ------------
8066 -- Lo_Val --
8067 ------------
8068
8069 function Lo_Val (N : Node_Id) return Uint is
8070 begin
8071 if Is_OK_Static_Expression (N) then
8072 return Expr_Value (N);
8073 else
8074 pragma Assert (Nkind (N) = N_Range);
8075 return Expr_Value (Low_Bound (N));
8076 end if;
8077 end Lo_Val;
8078
8079 ------------------------
8080 -- Membership_Entries --
8081 ------------------------
8082
8083 function Membership_Entries (N : Node_Id) return RList is
8084 begin
8085 if No (Next (N)) then
8086 return Membership_Entry (N);
8087 else
8088 return Membership_Entry (N) or Membership_Entries (Next (N));
8089 end if;
8090 end Membership_Entries;
8091
8092 ----------------------
8093 -- Membership_Entry --
8094 ----------------------
8095
8096 function Membership_Entry (N : Node_Id) return RList is
8097 Val : Uint;
8098 SLo : Uint;
8099 SHi : Uint;
8100
8101 begin
8102 -- Range case
8103
8104 if Nkind (N) = N_Range then
8105 if not Is_OK_Static_Expression (Low_Bound (N))
8106 or else
8107 not Is_OK_Static_Expression (High_Bound (N))
8108 then
8109 raise Non_Static;
8110 else
8111 SLo := Expr_Value (Low_Bound (N));
8112 SHi := Expr_Value (High_Bound (N));
8113 return RList'(1 => REnt'(SLo, SHi));
8114 end if;
8115
8116 -- Static expression case
8117
8118 elsif Is_OK_Static_Expression (N) then
8119 Val := Expr_Value (N);
8120 return RList'(1 => REnt'(Val, Val));
8121
8122 -- Identifier (other than static expression) case
8123
8124 else pragma Assert (Nkind (N) = N_Identifier);
8125
8126 -- Type case
8127
8128 if Is_Type (Entity (N)) then
8129
8130 -- If type has predicates, process them
8131
8132 if Has_Predicates (Entity (N)) then
8133 return Stat_Pred (Entity (N));
8134
8135 -- For static subtype without predicates, get range
8136
8137 elsif Is_OK_Static_Subtype (Entity (N)) then
8138 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
8139 SHi := Expr_Value (Type_High_Bound (Entity (N)));
8140 return RList'(1 => REnt'(SLo, SHi));
8141
8142 -- Any other type makes us non-static
8143
8144 else
8145 raise Non_Static;
8146 end if;
8147
8148 -- Any other kind of identifier in predicate (e.g. a non-static
8149 -- expression value) means this is not a static predicate.
8150
8151 else
8152 raise Non_Static;
8153 end if;
8154 end if;
8155 end Membership_Entry;
8156
8157 ---------------
8158 -- Stat_Pred --
8159 ---------------
8160
8161 function Stat_Pred (Typ : Entity_Id) return RList is
8162 begin
8163 -- Not static if type does not have static predicates
8164
8165 if not Has_Static_Predicate (Typ) then
8166 raise Non_Static;
8167 end if;
8168
8169 -- Otherwise we convert the predicate list to a range list
8170
8171 declare
8172 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
8173 Result : RList (1 .. List_Length (Spred));
8174 P : Node_Id;
8175
8176 begin
8177 P := First (Static_Discrete_Predicate (Typ));
8178 for J in Result'Range loop
8179 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
8180 Next (P);
8181 end loop;
8182
8183 return Result;
8184 end;
8185 end Stat_Pred;
8186
8187 -- Start of processing for Build_Discrete_Static_Predicate
8188
8189 begin
8190 -- Establish bounds for the predicate
8191
8192 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8193 TLo := Expr_Value (Type_Low_Bound (Typ));
8194 else
8195 TLo := BLo;
8196 end if;
8197
8198 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8199 THi := Expr_Value (Type_High_Bound (Typ));
8200 else
8201 THi := BHi;
8202 end if;
8203
8204 -- Analyze the expression to see if it is a static predicate
8205
8206 declare
8207 Ranges : constant RList := Get_RList (Expr);
8208 -- Range list from expression if it is static
8209
8210 Plist : List_Id;
8211
8212 begin
8213 -- Convert range list into a form for the static predicate. In the
8214 -- Ranges array, we just have raw ranges, these must be converted
8215 -- to properly typed and analyzed static expressions or range nodes.
8216
8217 -- Note: here we limit ranges to the ranges of the subtype, so that
8218 -- a predicate is always false for values outside the subtype. That
8219 -- seems fine, such values are invalid anyway, and considering them
8220 -- to fail the predicate seems allowed and friendly, and furthermore
8221 -- simplifies processing for case statements and loops.
8222
8223 Plist := New_List;
8224
8225 for J in Ranges'Range loop
8226 declare
8227 Lo : Uint := Ranges (J).Lo;
8228 Hi : Uint := Ranges (J).Hi;
8229
8230 begin
8231 -- Ignore completely out of range entry
8232
8233 if Hi < TLo or else Lo > THi then
8234 null;
8235
8236 -- Otherwise process entry
8237
8238 else
8239 -- Adjust out of range value to subtype range
8240
8241 if Lo < TLo then
8242 Lo := TLo;
8243 end if;
8244
8245 if Hi > THi then
8246 Hi := THi;
8247 end if;
8248
8249 -- Convert range into required form
8250
8251 Append_To (Plist, Build_Range (Lo, Hi));
8252 end if;
8253 end;
8254 end loop;
8255
8256 -- Processing was successful and all entries were static, so now we
8257 -- can store the result as the predicate list.
8258
8259 Set_Static_Discrete_Predicate (Typ, Plist);
8260
8261 -- Within a generic the predicate functions themselves need not
8262 -- be constructed.
8263
8264 if Inside_A_Generic then
8265 return;
8266 end if;
8267
8268 -- The processing for static predicates put the expression into
8269 -- canonical form as a series of ranges. It also eliminated
8270 -- duplicates and collapsed and combined ranges. We might as well
8271 -- replace the alternatives list of the right operand of the
8272 -- membership test with the static predicate list, which will
8273 -- usually be more efficient.
8274
8275 declare
8276 New_Alts : constant List_Id := New_List;
8277 Old_Node : Node_Id;
8278 New_Node : Node_Id;
8279
8280 begin
8281 Old_Node := First (Plist);
8282 while Present (Old_Node) loop
8283 New_Node := New_Copy (Old_Node);
8284
8285 if Nkind (New_Node) = N_Range then
8286 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
8287 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
8288 end if;
8289
8290 Append_To (New_Alts, New_Node);
8291 Next (Old_Node);
8292 end loop;
8293
8294 -- If empty list, replace by False
8295
8296 if Is_Empty_List (New_Alts) then
8297 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
8298
8299 -- Else replace by set membership test
8300
8301 else
8302 Rewrite (Expr,
8303 Make_In (Loc,
8304 Left_Opnd => Make_Identifier (Loc, Nam),
8305 Right_Opnd => Empty,
8306 Alternatives => New_Alts));
8307
8308 -- Resolve new expression in function context
8309
8310 Install_Formals (Predicate_Function (Typ));
8311 Push_Scope (Predicate_Function (Typ));
8312 Analyze_And_Resolve (Expr, Standard_Boolean);
8313 Pop_Scope;
8314 end if;
8315 end;
8316 end;
8317
8318 -- If non-static, return doing nothing
8319
8320 exception
8321 when Non_Static =>
8322 return;
8323 end Build_Discrete_Static_Predicate;
8324
8325 --------------------------------
8326 -- Build_Export_Import_Pragma --
8327 --------------------------------
8328
8329 function Build_Export_Import_Pragma
8330 (Asp : Node_Id;
8331 Id : Entity_Id) return Node_Id
8332 is
8333 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
8334 Expr : constant Node_Id := Expression (Asp);
8335 Loc : constant Source_Ptr := Sloc (Asp);
8336
8337 Args : List_Id;
8338 Conv : Node_Id;
8339 Conv_Arg : Node_Id;
8340 Dummy_1 : Node_Id;
8341 Dummy_2 : Node_Id;
8342 EN : Node_Id;
8343 LN : Node_Id;
8344 Prag : Node_Id;
8345
8346 Create_Pragma : Boolean := False;
8347 -- This flag is set when the aspect form is such that it warrants the
8348 -- creation of a corresponding pragma.
8349
8350 begin
8351 if Present (Expr) then
8352 if Error_Posted (Expr) then
8353 null;
8354
8355 elsif Is_True (Expr_Value (Expr)) then
8356 Create_Pragma := True;
8357 end if;
8358
8359 -- Otherwise the aspect defaults to True
8360
8361 else
8362 Create_Pragma := True;
8363 end if;
8364
8365 -- Nothing to do when the expression is False or is erroneous
8366
8367 if not Create_Pragma then
8368 return Empty;
8369 end if;
8370
8371 -- Obtain all interfacing aspects that apply to the related entity
8372
8373 Get_Interfacing_Aspects
8374 (Iface_Asp => Asp,
8375 Conv_Asp => Conv,
8376 EN_Asp => EN,
8377 Expo_Asp => Dummy_1,
8378 Imp_Asp => Dummy_2,
8379 LN_Asp => LN);
8380
8381 Args := New_List;
8382
8383 -- Handle the convention argument
8384
8385 if Present (Conv) then
8386 Conv_Arg := New_Copy_Tree (Expression (Conv));
8387
8388 -- Assume convention "Ada' when aspect Convention is missing
8389
8390 else
8391 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8392 end if;
8393
8394 Append_To (Args,
8395 Make_Pragma_Argument_Association (Loc,
8396 Chars => Name_Convention,
8397 Expression => Conv_Arg));
8398
8399 -- Handle the entity argument
8400
8401 Append_To (Args,
8402 Make_Pragma_Argument_Association (Loc,
8403 Chars => Name_Entity,
8404 Expression => New_Occurrence_Of (Id, Loc)));
8405
8406 -- Handle the External_Name argument
8407
8408 if Present (EN) then
8409 Append_To (Args,
8410 Make_Pragma_Argument_Association (Loc,
8411 Chars => Name_External_Name,
8412 Expression => New_Copy_Tree (Expression (EN))));
8413 end if;
8414
8415 -- Handle the Link_Name argument
8416
8417 if Present (LN) then
8418 Append_To (Args,
8419 Make_Pragma_Argument_Association (Loc,
8420 Chars => Name_Link_Name,
8421 Expression => New_Copy_Tree (Expression (LN))));
8422 end if;
8423
8424 -- Generate:
8425 -- pragma Export/Import
8426 -- (Convention => <Conv>/Ada,
8427 -- Entity => <Id>,
8428 -- [External_Name => <EN>,]
8429 -- [Link_Name => <LN>]);
8430
8431 Prag :=
8432 Make_Pragma (Loc,
8433 Pragma_Identifier =>
8434 Make_Identifier (Loc, Chars (Identifier (Asp))),
8435 Pragma_Argument_Associations => Args);
8436
8437 -- Decorate the relevant aspect and the pragma
8438
8439 Set_Aspect_Rep_Item (Asp, Prag);
8440
8441 Set_Corresponding_Aspect (Prag, Asp);
8442 Set_From_Aspect_Specification (Prag);
8443 Set_Parent (Prag, Asp);
8444
8445 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8446 Set_Import_Pragma (Id, Prag);
8447 end if;
8448
8449 return Prag;
8450 end Build_Export_Import_Pragma;
8451
8452 -------------------------------
8453 -- Build_Predicate_Functions --
8454 -------------------------------
8455
8456 -- The functions that are constructed here have the form:
8457
8458 -- function typPredicate (Ixxx : typ) return Boolean is
8459 -- begin
8460 -- return
8461 -- typ1Predicate (typ1 (Ixxx))
8462 -- and then typ2Predicate (typ2 (Ixxx))
8463 -- and then ...
8464 -- and then exp1 and then exp2 and then ...;
8465 -- end typPredicate;
8466
8467 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8468 -- this is the point at which these expressions get analyzed, providing the
8469 -- required delay, and typ1, typ2, are entities from which predicates are
8470 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8471 -- use this function even if checks are off, e.g. for membership tests.
8472
8473 -- Note that the inherited predicates are evaluated first, as required by
8474 -- AI12-0071-1.
8475
8476 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8477 -- the form of this return expression.
8478
8479 -- If the expression has at least one Raise_Expression, then we also build
8480 -- the typPredicateM version of the function, in which any occurrence of a
8481 -- Raise_Expression is converted to "return False".
8482
8483 -- WARNING: This routine manages Ghost regions. Return statements must be
8484 -- replaced by gotos which jump to the end of the routine and restore the
8485 -- Ghost mode.
8486
8487 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8488 Loc : constant Source_Ptr := Sloc (Typ);
8489
8490 Expr : Node_Id;
8491 -- This is the expression for the result of the function. It is
8492 -- is build by connecting the component predicates with AND THEN.
8493
8494 Expr_M : Node_Id := Empty; -- init to avoid warning
8495 -- This is the corresponding return expression for the Predicate_M
8496 -- function. It differs in that raise expressions are marked for
8497 -- special expansion (see Process_REs).
8498
8499 Object_Name : Name_Id;
8500 -- Name for argument of Predicate procedure. Note that we use the same
8501 -- name for both predicate functions. That way the reference within the
8502 -- predicate expression is the same in both functions.
8503
8504 Object_Entity : Entity_Id;
8505 -- Entity for argument of Predicate procedure
8506
8507 Object_Entity_M : Entity_Id;
8508 -- Entity for argument of separate Predicate procedure when exceptions
8509 -- are present in expression.
8510
8511 FDecl : Node_Id;
8512 -- The function declaration
8513
8514 SId : Entity_Id;
8515 -- Its entity
8516
8517 Raise_Expression_Present : Boolean := False;
8518 -- Set True if Expr has at least one Raise_Expression
8519
8520 procedure Add_Condition (Cond : Node_Id);
8521 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8522 -- Expr is empty).
8523
8524 procedure Add_Predicates;
8525 -- Appends expressions for any Predicate pragmas in the rep item chain
8526 -- Typ to Expr. Note that we look only at items for this exact entity.
8527 -- Inheritance of predicates for the parent type is done by calling the
8528 -- Predicate_Function of the parent type, using Add_Call above.
8529
8530 procedure Add_Call (T : Entity_Id);
8531 -- Includes a call to the predicate function for type T in Expr if T
8532 -- has predicates and Predicate_Function (T) is non-empty.
8533
8534 function Process_RE (N : Node_Id) return Traverse_Result;
8535 -- Used in Process REs, tests if node N is a raise expression, and if
8536 -- so, marks it to be converted to return False.
8537
8538 procedure Process_REs is new Traverse_Proc (Process_RE);
8539 -- Marks any raise expressions in Expr_M to return False
8540
8541 function Test_RE (N : Node_Id) return Traverse_Result;
8542 -- Used in Test_REs, tests one node for being a raise expression, and if
8543 -- so sets Raise_Expression_Present True.
8544
8545 procedure Test_REs is new Traverse_Proc (Test_RE);
8546 -- Tests to see if Expr contains any raise expressions
8547
8548 --------------
8549 -- Add_Call --
8550 --------------
8551
8552 procedure Add_Call (T : Entity_Id) is
8553 Exp : Node_Id;
8554
8555 begin
8556 if Present (T) and then Present (Predicate_Function (T)) then
8557 Set_Has_Predicates (Typ);
8558
8559 -- Build the call to the predicate function of T. The type may be
8560 -- derived, so use an unchecked conversion for the actual.
8561
8562 Exp :=
8563 Make_Predicate_Call
8564 (Typ => T,
8565 Expr =>
8566 Unchecked_Convert_To (T,
8567 Make_Identifier (Loc, Object_Name)));
8568
8569 -- "and"-in the call to evolving expression
8570
8571 Add_Condition (Exp);
8572
8573 -- Output info message on inheritance if required. Note we do not
8574 -- give this information for generic actual types, since it is
8575 -- unwelcome noise in that case in instantiations. We also
8576 -- generally suppress the message in instantiations, and also
8577 -- if it involves internal names.
8578
8579 if Opt.List_Inherited_Aspects
8580 and then not Is_Generic_Actual_Type (Typ)
8581 and then Instantiation_Depth (Sloc (Typ)) = 0
8582 and then not Is_Internal_Name (Chars (T))
8583 and then not Is_Internal_Name (Chars (Typ))
8584 then
8585 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8586 Error_Msg_Node_2 := T;
8587 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8588 end if;
8589 end if;
8590 end Add_Call;
8591
8592 -------------------
8593 -- Add_Condition --
8594 -------------------
8595
8596 procedure Add_Condition (Cond : Node_Id) is
8597 begin
8598 -- This is the first predicate expression
8599
8600 if No (Expr) then
8601 Expr := Cond;
8602
8603 -- Otherwise concatenate to the existing predicate expressions by
8604 -- using "and then".
8605
8606 else
8607 Expr :=
8608 Make_And_Then (Loc,
8609 Left_Opnd => Relocate_Node (Expr),
8610 Right_Opnd => Cond);
8611 end if;
8612 end Add_Condition;
8613
8614 --------------------
8615 -- Add_Predicates --
8616 --------------------
8617
8618 procedure Add_Predicates is
8619 procedure Add_Predicate (Prag : Node_Id);
8620 -- Concatenate the expression of predicate pragma Prag to Expr by
8621 -- using a short circuit "and then" operator.
8622
8623 -------------------
8624 -- Add_Predicate --
8625 -------------------
8626
8627 procedure Add_Predicate (Prag : Node_Id) is
8628 procedure Replace_Type_Reference (N : Node_Id);
8629 -- Replace a single occurrence N of the subtype name with a
8630 -- reference to the formal of the predicate function. N can be an
8631 -- identifier referencing the subtype, or a selected component,
8632 -- representing an appropriately qualified occurrence of the
8633 -- subtype name.
8634
8635 procedure Replace_Type_References is
8636 new Replace_Type_References_Generic (Replace_Type_Reference);
8637 -- Traverse an expression changing every occurrence of an
8638 -- identifier whose name matches the name of the subtype with a
8639 -- reference to the formal parameter of the predicate function.
8640
8641 ----------------------------
8642 -- Replace_Type_Reference --
8643 ----------------------------
8644
8645 procedure Replace_Type_Reference (N : Node_Id) is
8646 begin
8647 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8648 -- Use the Sloc of the usage name, not the defining name
8649
8650 Set_Etype (N, Typ);
8651 Set_Entity (N, Object_Entity);
8652
8653 -- We want to treat the node as if it comes from source, so
8654 -- that ASIS will not ignore it.
8655
8656 Set_Comes_From_Source (N, True);
8657 end Replace_Type_Reference;
8658
8659 -- Local variables
8660
8661 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8662 Arg1 : Node_Id;
8663 Arg2 : Node_Id;
8664
8665 -- Start of processing for Add_Predicate
8666
8667 begin
8668 -- Mark corresponding SCO as enabled
8669
8670 Set_SCO_Pragma_Enabled (Sloc (Prag));
8671
8672 -- Extract the arguments of the pragma. The expression itself
8673 -- is copied for use in the predicate function, to preserve the
8674 -- original version for ASIS use.
8675
8676 Arg1 := First (Pragma_Argument_Associations (Prag));
8677 Arg2 := Next (Arg1);
8678
8679 Arg1 := Get_Pragma_Arg (Arg1);
8680 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8681
8682 -- When the predicate pragma applies to the current type or its
8683 -- full view, replace all occurrences of the subtype name with
8684 -- references to the formal parameter of the predicate function.
8685
8686 if Entity (Arg1) = Typ
8687 or else Full_View (Entity (Arg1)) = Typ
8688 then
8689 Replace_Type_References (Arg2, Typ);
8690
8691 -- If the predicate pragma comes from an aspect, replace the
8692 -- saved expression because we need the subtype references
8693 -- replaced for the calls to Preanalyze_Spec_Expression in
8694 -- Check_Aspect_At_xxx routines.
8695
8696 if Present (Asp) then
8697 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8698 end if;
8699
8700 -- "and"-in the Arg2 condition to evolving expression
8701
8702 Add_Condition (Relocate_Node (Arg2));
8703 end if;
8704 end Add_Predicate;
8705
8706 -- Local variables
8707
8708 Ritem : Node_Id;
8709
8710 -- Start of processing for Add_Predicates
8711
8712 begin
8713 Ritem := First_Rep_Item (Typ);
8714
8715 -- If the type is private, check whether full view has inherited
8716 -- predicates.
8717
8718 if Is_Private_Type (Typ) and then No (Ritem) then
8719 Ritem := First_Rep_Item (Full_View (Typ));
8720 end if;
8721
8722 while Present (Ritem) loop
8723 if Nkind (Ritem) = N_Pragma
8724 and then Pragma_Name (Ritem) = Name_Predicate
8725 then
8726 Add_Predicate (Ritem);
8727
8728 -- If the type is declared in an inner package it may be frozen
8729 -- outside of the package, and the generated pragma has not been
8730 -- analyzed yet, so capture the expression for the predicate
8731 -- function at this point.
8732
8733 elsif Nkind (Ritem) = N_Aspect_Specification
8734 and then Present (Aspect_Rep_Item (Ritem))
8735 and then Scope (Typ) /= Current_Scope
8736 then
8737 declare
8738 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8739
8740 begin
8741 if Nkind (Prag) = N_Pragma
8742 and then Pragma_Name (Prag) = Name_Predicate
8743 then
8744 Add_Predicate (Prag);
8745 end if;
8746 end;
8747 end if;
8748
8749 Next_Rep_Item (Ritem);
8750 end loop;
8751 end Add_Predicates;
8752
8753 ----------------
8754 -- Process_RE --
8755 ----------------
8756
8757 function Process_RE (N : Node_Id) return Traverse_Result is
8758 begin
8759 if Nkind (N) = N_Raise_Expression then
8760 Set_Convert_To_Return_False (N);
8761 return Skip;
8762 else
8763 return OK;
8764 end if;
8765 end Process_RE;
8766
8767 -------------
8768 -- Test_RE --
8769 -------------
8770
8771 function Test_RE (N : Node_Id) return Traverse_Result is
8772 begin
8773 if Nkind (N) = N_Raise_Expression then
8774 Raise_Expression_Present := True;
8775 return Abandon;
8776 else
8777 return OK;
8778 end if;
8779 end Test_RE;
8780
8781 -- Local variables
8782
8783 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
8784 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
8785 -- Save the Ghost-related attributes to restore on exit
8786
8787 -- Start of processing for Build_Predicate_Functions
8788
8789 begin
8790 -- Return if already built or if type does not have predicates
8791
8792 SId := Predicate_Function (Typ);
8793 if not Has_Predicates (Typ)
8794 or else (Present (SId) and then Has_Completion (SId))
8795 then
8796 return;
8797
8798 -- Do not generate predicate bodies within a generic unit. The
8799 -- expressions have been analyzed already, and the bodies play
8800 -- no role if not within an executable unit. However, if a statc
8801 -- predicate is present it must be processed for legality checks
8802 -- such as case coverage in an expression.
8803
8804 elsif Inside_A_Generic
8805 and then not Has_Static_Predicate_Aspect (Typ)
8806 then
8807 return;
8808 end if;
8809
8810 -- The related type may be subject to pragma Ghost. Set the mode now to
8811 -- ensure that the predicate functions are properly marked as Ghost.
8812
8813 Set_Ghost_Mode (Typ);
8814
8815 -- Prepare to construct predicate expression
8816
8817 Expr := Empty;
8818
8819 if Present (SId) then
8820 FDecl := Unit_Declaration_Node (SId);
8821
8822 else
8823 FDecl := Build_Predicate_Function_Declaration (Typ);
8824 SId := Defining_Entity (FDecl);
8825 end if;
8826
8827 -- Recover name of formal parameter of function that replaces references
8828 -- to the type in predicate expressions.
8829
8830 Object_Entity :=
8831 Defining_Identifier
8832 (First (Parameter_Specifications (Specification (FDecl))));
8833
8834 Object_Name := Chars (Object_Entity);
8835 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8836
8837 -- Add predicates for ancestor if present. These must come before the
8838 -- ones for the current type, as required by AI12-0071-1.
8839
8840 declare
8841 Atyp : Entity_Id;
8842 begin
8843 Atyp := Nearest_Ancestor (Typ);
8844
8845 -- The type may be private but the full view may inherit predicates
8846
8847 if No (Atyp) and then Is_Private_Type (Typ) then
8848 Atyp := Nearest_Ancestor (Full_View (Typ));
8849 end if;
8850
8851 if Present (Atyp) then
8852 Add_Call (Atyp);
8853 end if;
8854 end;
8855
8856 -- Add Predicates for the current type
8857
8858 Add_Predicates;
8859
8860 -- Case where predicates are present
8861
8862 if Present (Expr) then
8863
8864 -- Test for raise expression present
8865
8866 Test_REs (Expr);
8867
8868 -- If raise expression is present, capture a copy of Expr for use
8869 -- in building the predicateM function version later on. For this
8870 -- copy we replace references to Object_Entity by Object_Entity_M.
8871
8872 if Raise_Expression_Present then
8873 declare
8874 function Reset_Loop_Variable
8875 (N : Node_Id) return Traverse_Result;
8876
8877 procedure Reset_Loop_Variables is
8878 new Traverse_Proc (Reset_Loop_Variable);
8879
8880 ------------------------
8881 -- Reset_Loop_Variable --
8882 ------------------------
8883
8884 function Reset_Loop_Variable
8885 (N : Node_Id) return Traverse_Result
8886 is
8887 begin
8888 if Nkind (N) = N_Iterator_Specification then
8889 Set_Defining_Identifier (N,
8890 Make_Defining_Identifier
8891 (Sloc (N), Chars (Defining_Identifier (N))));
8892 end if;
8893
8894 return OK;
8895 end Reset_Loop_Variable;
8896
8897 -- Local variables
8898
8899 Map : constant Elist_Id := New_Elmt_List;
8900
8901 begin
8902 Append_Elmt (Object_Entity, Map);
8903 Append_Elmt (Object_Entity_M, Map);
8904 Expr_M := New_Copy_Tree (Expr, Map => Map);
8905
8906 -- The unanalyzed expression will be copied and appear in
8907 -- both functions. Normally expressions do not declare new
8908 -- entities, but quantified expressions do, so we need to
8909 -- create new entities for their bound variables, to prevent
8910 -- multiple definitions in gigi.
8911
8912 Reset_Loop_Variables (Expr_M);
8913 end;
8914 end if;
8915
8916 -- Build the main predicate function
8917
8918 declare
8919 SIdB : constant Entity_Id :=
8920 Make_Defining_Identifier (Loc,
8921 Chars => New_External_Name (Chars (Typ), "Predicate"));
8922 -- The entity for the function body
8923
8924 Spec : Node_Id;
8925 FBody : Node_Id;
8926
8927 begin
8928 Set_Ekind (SIdB, E_Function);
8929 Set_Is_Predicate_Function (SIdB);
8930
8931 -- The predicate function is shared between views of a type
8932
8933 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8934 Set_Predicate_Function (Full_View (Typ), SId);
8935 end if;
8936
8937 -- Build function body
8938
8939 Spec :=
8940 Make_Function_Specification (Loc,
8941 Defining_Unit_Name => SIdB,
8942 Parameter_Specifications => New_List (
8943 Make_Parameter_Specification (Loc,
8944 Defining_Identifier =>
8945 Make_Defining_Identifier (Loc, Object_Name),
8946 Parameter_Type =>
8947 New_Occurrence_Of (Typ, Loc))),
8948 Result_Definition =>
8949 New_Occurrence_Of (Standard_Boolean, Loc));
8950
8951 FBody :=
8952 Make_Subprogram_Body (Loc,
8953 Specification => Spec,
8954 Declarations => Empty_List,
8955 Handled_Statement_Sequence =>
8956 Make_Handled_Sequence_Of_Statements (Loc,
8957 Statements => New_List (
8958 Make_Simple_Return_Statement (Loc,
8959 Expression => Expr))));
8960
8961 -- The declaration has been analyzed when created, and placed
8962 -- after type declaration. Insert body itself after freeze node,
8963 -- unless subprogram declaration is already there, in which case
8964 -- body better be placed afterwards.
8965
8966 if FDecl = Next (N) then
8967 Insert_After_And_Analyze (FDecl, FBody);
8968 else
8969 Insert_After_And_Analyze (N, FBody);
8970 end if;
8971
8972 -- The defining identifier of a quantified expression carries the
8973 -- scope in which the type appears, but when unnesting we need
8974 -- to indicate that its proper scope is the constructed predicate
8975 -- function. The quantified expressions have been converted into
8976 -- loops during analysis and expansion.
8977
8978 declare
8979 function Reset_Quantified_Variable_Scope
8980 (N : Node_Id) return Traverse_Result;
8981
8982 procedure Reset_Quantified_Variables_Scope is
8983 new Traverse_Proc (Reset_Quantified_Variable_Scope);
8984
8985 -------------------------------------
8986 -- Reset_Quantified_Variable_Scope --
8987 -------------------------------------
8988
8989 function Reset_Quantified_Variable_Scope
8990 (N : Node_Id) return Traverse_Result
8991 is
8992 begin
8993 if Nkind_In (N, N_Iterator_Specification,
8994 N_Loop_Parameter_Specification)
8995 then
8996 Set_Scope (Defining_Identifier (N),
8997 Predicate_Function (Typ));
8998 end if;
8999
9000 return OK;
9001 end Reset_Quantified_Variable_Scope;
9002
9003 begin
9004 if Unnest_Subprogram_Mode then
9005 Reset_Quantified_Variables_Scope (Expr);
9006 end if;
9007 end;
9008
9009 -- within a generic unit, prevent a double analysis of the body
9010 -- which will not be marked analyzed yet. This will happen when
9011 -- the freeze node is created during the preanalysis of an
9012 -- expression function.
9013
9014 if Inside_A_Generic then
9015 Set_Analyzed (FBody);
9016 end if;
9017
9018 -- Static predicate functions are always side-effect free, and
9019 -- in most cases dynamic predicate functions are as well. Mark
9020 -- them as such whenever possible, so redundant predicate checks
9021 -- can be optimized. If there is a variable reference within the
9022 -- expression, the function is not pure.
9023
9024 if Expander_Active then
9025 Set_Is_Pure (SId,
9026 Side_Effect_Free (Expr, Variable_Ref => True));
9027 Set_Is_Inlined (SId);
9028 end if;
9029 end;
9030
9031 -- Test for raise expressions present and if so build M version
9032
9033 if Raise_Expression_Present then
9034 declare
9035 SId : constant Entity_Id :=
9036 Make_Defining_Identifier (Loc,
9037 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9038 -- The entity for the function spec
9039
9040 SIdB : constant Entity_Id :=
9041 Make_Defining_Identifier (Loc,
9042 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9043 -- The entity for the function body
9044
9045 Spec : Node_Id;
9046 FBody : Node_Id;
9047 FDecl : Node_Id;
9048 BTemp : Entity_Id;
9049
9050 begin
9051 -- Mark any raise expressions for special expansion
9052
9053 Process_REs (Expr_M);
9054
9055 -- Build function declaration
9056
9057 Set_Ekind (SId, E_Function);
9058 Set_Is_Predicate_Function_M (SId);
9059 Set_Predicate_Function_M (Typ, SId);
9060
9061 -- The predicate function is shared between views of a type
9062
9063 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
9064 Set_Predicate_Function_M (Full_View (Typ), SId);
9065 end if;
9066
9067 Spec :=
9068 Make_Function_Specification (Loc,
9069 Defining_Unit_Name => SId,
9070 Parameter_Specifications => New_List (
9071 Make_Parameter_Specification (Loc,
9072 Defining_Identifier => Object_Entity_M,
9073 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9074 Result_Definition =>
9075 New_Occurrence_Of (Standard_Boolean, Loc));
9076
9077 FDecl :=
9078 Make_Subprogram_Declaration (Loc,
9079 Specification => Spec);
9080
9081 -- Build function body
9082
9083 Spec :=
9084 Make_Function_Specification (Loc,
9085 Defining_Unit_Name => SIdB,
9086 Parameter_Specifications => New_List (
9087 Make_Parameter_Specification (Loc,
9088 Defining_Identifier =>
9089 Make_Defining_Identifier (Loc, Object_Name),
9090 Parameter_Type =>
9091 New_Occurrence_Of (Typ, Loc))),
9092 Result_Definition =>
9093 New_Occurrence_Of (Standard_Boolean, Loc));
9094
9095 -- Build the body, we declare the boolean expression before
9096 -- doing the return, because we are not really confident of
9097 -- what happens if a return appears within a return.
9098
9099 BTemp :=
9100 Make_Defining_Identifier (Loc,
9101 Chars => New_Internal_Name ('B'));
9102
9103 FBody :=
9104 Make_Subprogram_Body (Loc,
9105 Specification => Spec,
9106
9107 Declarations => New_List (
9108 Make_Object_Declaration (Loc,
9109 Defining_Identifier => BTemp,
9110 Constant_Present => True,
9111 Object_Definition =>
9112 New_Occurrence_Of (Standard_Boolean, Loc),
9113 Expression => Expr_M)),
9114
9115 Handled_Statement_Sequence =>
9116 Make_Handled_Sequence_Of_Statements (Loc,
9117 Statements => New_List (
9118 Make_Simple_Return_Statement (Loc,
9119 Expression => New_Occurrence_Of (BTemp, Loc)))));
9120
9121 -- Insert declaration before freeze node and body after
9122
9123 Insert_Before_And_Analyze (N, FDecl);
9124 Insert_After_And_Analyze (N, FBody);
9125
9126 -- Should quantified expressions be handled here as well ???
9127 end;
9128 end if;
9129
9130 -- See if we have a static predicate. Note that the answer may be
9131 -- yes even if we have an explicit Dynamic_Predicate present.
9132
9133 declare
9134 PS : Boolean;
9135 EN : Node_Id;
9136
9137 begin
9138 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
9139 PS := False;
9140 else
9141 PS := Is_Predicate_Static (Expr, Object_Name);
9142 end if;
9143
9144 -- Case where we have a predicate-static aspect
9145
9146 if PS then
9147
9148 -- We don't set Has_Static_Predicate_Aspect, since we can have
9149 -- any of the three cases (Predicate, Dynamic_Predicate, or
9150 -- Static_Predicate) generating a predicate with an expression
9151 -- that is predicate-static. We just indicate that we have a
9152 -- predicate that can be treated as static.
9153
9154 Set_Has_Static_Predicate (Typ);
9155
9156 -- For discrete subtype, build the static predicate list
9157
9158 if Is_Discrete_Type (Typ) then
9159 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
9160
9161 -- If we don't get a static predicate list, it means that we
9162 -- have a case where this is not possible, most typically in
9163 -- the case where we inherit a dynamic predicate. We do not
9164 -- consider this an error, we just leave the predicate as
9165 -- dynamic. But if we do succeed in building the list, then
9166 -- we mark the predicate as static.
9167
9168 if No (Static_Discrete_Predicate (Typ)) then
9169 Set_Has_Static_Predicate (Typ, False);
9170 end if;
9171
9172 -- For real or string subtype, save predicate expression
9173
9174 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9175 Set_Static_Real_Or_String_Predicate (Typ, Expr);
9176 end if;
9177
9178 -- Case of dynamic predicate (expression is not predicate-static)
9179
9180 else
9181 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
9182 -- is only set if we have an explicit Dynamic_Predicate aspect
9183 -- given. Here we may simply have a Predicate aspect where the
9184 -- expression happens not to be predicate-static.
9185
9186 -- Emit an error when the predicate is categorized as static
9187 -- but its expression is not predicate-static.
9188
9189 -- First a little fiddling to get a nice location for the
9190 -- message. If the expression is of the form (A and then B),
9191 -- where A is an inherited predicate, then use the right
9192 -- operand for the Sloc. This avoids getting confused by a call
9193 -- to an inherited predicate with a less convenient source
9194 -- location.
9195
9196 EN := Expr;
9197 while Nkind (EN) = N_And_Then
9198 and then Nkind (Left_Opnd (EN)) = N_Function_Call
9199 and then Is_Predicate_Function
9200 (Entity (Name (Left_Opnd (EN))))
9201 loop
9202 EN := Right_Opnd (EN);
9203 end loop;
9204
9205 -- Now post appropriate message
9206
9207 if Has_Static_Predicate_Aspect (Typ) then
9208 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
9209 Error_Msg_F
9210 ("expression is not predicate-static (RM 3.2.4(16-22))",
9211 EN);
9212 else
9213 Error_Msg_F
9214 ("static predicate requires scalar or string type", EN);
9215 end if;
9216 end if;
9217 end if;
9218 end;
9219 end if;
9220
9221 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9222 end Build_Predicate_Functions;
9223
9224 ------------------------------------------
9225 -- Build_Predicate_Function_Declaration --
9226 ------------------------------------------
9227
9228 -- WARNING: This routine manages Ghost regions. Return statements must be
9229 -- replaced by gotos which jump to the end of the routine and restore the
9230 -- Ghost mode.
9231
9232 function Build_Predicate_Function_Declaration
9233 (Typ : Entity_Id) return Node_Id
9234 is
9235 Loc : constant Source_Ptr := Sloc (Typ);
9236
9237 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9238 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
9239 -- Save the Ghost-related attributes to restore on exit
9240
9241 Func_Decl : Node_Id;
9242 Func_Id : Entity_Id;
9243 Spec : Node_Id;
9244
9245 begin
9246 -- The related type may be subject to pragma Ghost. Set the mode now to
9247 -- ensure that the predicate functions are properly marked as Ghost.
9248
9249 Set_Ghost_Mode (Typ);
9250
9251 Func_Id :=
9252 Make_Defining_Identifier (Loc,
9253 Chars => New_External_Name (Chars (Typ), "Predicate"));
9254
9255 -- The predicate function requires debug info when the predicates are
9256 -- subject to Source Coverage Obligations.
9257
9258 if Opt.Generate_SCO then
9259 Set_Debug_Info_Needed (Func_Id);
9260 end if;
9261
9262 Spec :=
9263 Make_Function_Specification (Loc,
9264 Defining_Unit_Name => Func_Id,
9265 Parameter_Specifications => New_List (
9266 Make_Parameter_Specification (Loc,
9267 Defining_Identifier => Make_Temporary (Loc, 'I'),
9268 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9269 Result_Definition =>
9270 New_Occurrence_Of (Standard_Boolean, Loc));
9271
9272 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
9273
9274 Set_Ekind (Func_Id, E_Function);
9275 Set_Etype (Func_Id, Standard_Boolean);
9276 Set_Is_Internal (Func_Id);
9277 Set_Is_Predicate_Function (Func_Id);
9278 Set_Predicate_Function (Typ, Func_Id);
9279
9280 Insert_After (Parent (Typ), Func_Decl);
9281 Analyze (Func_Decl);
9282
9283 Restore_Ghost_Region (Saved_GM, Saved_IGR);
9284
9285 return Func_Decl;
9286 end Build_Predicate_Function_Declaration;
9287
9288 -----------------------------------------
9289 -- Check_Aspect_At_End_Of_Declarations --
9290 -----------------------------------------
9291
9292 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9293 Ent : constant Entity_Id := Entity (ASN);
9294 Ident : constant Node_Id := Identifier (ASN);
9295 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9296
9297 End_Decl_Expr : constant Node_Id := Entity (Ident);
9298 -- Expression to be analyzed at end of declarations
9299
9300 Freeze_Expr : constant Node_Id := Expression (ASN);
9301 -- Expression from call to Check_Aspect_At_Freeze_Point.
9302
9303 T : constant Entity_Id := Etype (Original_Node (Freeze_Expr));
9304 -- Type required for preanalyze call. We use the original expression to
9305 -- get the proper type, to prevent cascaded errors when the expression
9306 -- is constant-folded.
9307
9308 Err : Boolean;
9309 -- Set False if error
9310
9311 -- On entry to this procedure, Entity (Ident) contains a copy of the
9312 -- original expression from the aspect, saved for this purpose, and
9313 -- but Expression (Ident) is a preanalyzed copy of the expression,
9314 -- preanalyzed just after the freeze point.
9315
9316 procedure Check_Overloaded_Name;
9317 -- For aspects whose expression is simply a name, this routine checks if
9318 -- the name is overloaded or not. If so, it verifies there is an
9319 -- interpretation that matches the entity obtained at the freeze point,
9320 -- otherwise the compiler complains.
9321
9322 ---------------------------
9323 -- Check_Overloaded_Name --
9324 ---------------------------
9325
9326 procedure Check_Overloaded_Name is
9327 begin
9328 if not Is_Overloaded (End_Decl_Expr) then
9329 Err := not Is_Entity_Name (End_Decl_Expr)
9330 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
9331
9332 else
9333 Err := True;
9334
9335 declare
9336 Index : Interp_Index;
9337 It : Interp;
9338
9339 begin
9340 Get_First_Interp (End_Decl_Expr, Index, It);
9341 while Present (It.Typ) loop
9342 if It.Nam = Entity (Freeze_Expr) then
9343 Err := False;
9344 exit;
9345 end if;
9346
9347 Get_Next_Interp (Index, It);
9348 end loop;
9349 end;
9350 end if;
9351 end Check_Overloaded_Name;
9352
9353 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9354
9355 begin
9356 -- In an instance we do not perform the consistency check between freeze
9357 -- point and end of declarations, because it was done already in the
9358 -- analysis of the generic. Furthermore, the delayed analysis of an
9359 -- aspect of the instance may produce spurious errors when the generic
9360 -- is a child unit that references entities in the parent (which might
9361 -- not be in scope at the freeze point of the instance).
9362
9363 if In_Instance then
9364 return;
9365
9366 -- The enclosing scope may have been rewritten during expansion (.e.g. a
9367 -- task body is rewritten as a procedure) after this conformance check
9368 -- has been performed, so do not perform it again (it may not easily be
9369 -- done if full visibility of local entities is not available).
9370
9371 elsif not Comes_From_Source (Current_Scope) then
9372 return;
9373
9374 -- Case of aspects Dimension, Dimension_System and Synchronization
9375
9376 elsif A_Id = Aspect_Synchronization then
9377 return;
9378
9379 -- Case of stream attributes, just have to compare entities. However,
9380 -- the expression is just a name (possibly overloaded), and there may
9381 -- be stream operations declared for unrelated types, so we just need
9382 -- to verify that one of these interpretations is the one available at
9383 -- at the freeze point.
9384
9385 elsif A_Id = Aspect_Input or else
9386 A_Id = Aspect_Output or else
9387 A_Id = Aspect_Read or else
9388 A_Id = Aspect_Write
9389 then
9390 Analyze (End_Decl_Expr);
9391 Check_Overloaded_Name;
9392
9393 elsif A_Id = Aspect_Variable_Indexing or else
9394 A_Id = Aspect_Constant_Indexing or else
9395 A_Id = Aspect_Default_Iterator or else
9396 A_Id = Aspect_Iterator_Element
9397 then
9398 -- Make type unfrozen before analysis, to prevent spurious errors
9399 -- about late attributes.
9400
9401 Set_Is_Frozen (Ent, False);
9402 Analyze (End_Decl_Expr);
9403 Set_Is_Frozen (Ent, True);
9404
9405 -- If the end of declarations comes before any other freeze point,
9406 -- the Freeze_Expr is not analyzed: no check needed.
9407
9408 if Analyzed (Freeze_Expr) and then not In_Instance then
9409 Check_Overloaded_Name;
9410 else
9411 Err := False;
9412 end if;
9413
9414 -- All other cases
9415
9416 else
9417 -- In a generic context freeze nodes are not always generated, so
9418 -- analyze the expression now. If the aspect is for a type, we must
9419 -- also make its potential components accessible.
9420
9421 if not Analyzed (Freeze_Expr) and then Inside_A_Generic then
9422 if A_Id = Aspect_Dynamic_Predicate
9423 or else A_Id = Aspect_Predicate
9424 then
9425 Push_Type (Ent);
9426 Preanalyze_Spec_Expression (Freeze_Expr, Standard_Boolean);
9427 Pop_Type (Ent);
9428
9429 elsif A_Id = Aspect_Priority then
9430 Push_Type (Ent);
9431 Preanalyze_Spec_Expression (Freeze_Expr, Any_Integer);
9432 Pop_Type (Ent);
9433
9434 else
9435 Preanalyze (Freeze_Expr);
9436 end if;
9437 end if;
9438
9439 -- Indicate that the expression comes from an aspect specification,
9440 -- which is used in subsequent analysis even if expansion is off.
9441
9442 Set_Parent (End_Decl_Expr, ASN);
9443
9444 -- In a generic context the original aspect expressions have not
9445 -- been preanalyzed, so do it now. There are no conformance checks
9446 -- to perform in this case. As before, we have to make components
9447 -- visible for aspects that may reference them.
9448
9449 if No (T) then
9450 if A_Id = Aspect_Dynamic_Predicate
9451 or else A_Id = Aspect_Predicate
9452 or else A_Id = Aspect_Priority
9453 then
9454 Push_Type (Ent);
9455 Check_Aspect_At_Freeze_Point (ASN);
9456 Pop_Type (Ent);
9457
9458 else
9459 Check_Aspect_At_Freeze_Point (ASN);
9460 end if;
9461 return;
9462
9463 -- The default values attributes may be defined in the private part,
9464 -- and the analysis of the expression may take place when only the
9465 -- partial view is visible. The expression must be scalar, so use
9466 -- the full view to resolve.
9467
9468 elsif (A_Id = Aspect_Default_Value
9469 or else
9470 A_Id = Aspect_Default_Component_Value)
9471 and then Is_Private_Type (T)
9472 then
9473 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9474
9475 -- The following aspect expressions may contain references to
9476 -- components and discriminants of the type.
9477
9478 elsif A_Id = Aspect_Dynamic_Predicate
9479 or else A_Id = Aspect_Predicate
9480 or else A_Id = Aspect_Priority
9481 or else A_Id = Aspect_CPU
9482 then
9483 Push_Type (Ent);
9484 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9485 Pop_Type (Ent);
9486
9487 else
9488 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9489 end if;
9490
9491 Err :=
9492 not Fully_Conformant_Expressions
9493 (End_Decl_Expr, Freeze_Expr, Report => True);
9494 end if;
9495
9496 -- Output error message if error. Force error on aspect specification
9497 -- even if there is an error on the expression itself.
9498
9499 if Err then
9500 Error_Msg_NE
9501 ("!visibility of aspect for& changes after freeze point",
9502 ASN, Ent);
9503 Error_Msg_NE
9504 ("info: & is frozen here, (RM 13.1.1 (13/3))??",
9505 Freeze_Node (Ent), Ent);
9506 end if;
9507 end Check_Aspect_At_End_Of_Declarations;
9508
9509 ----------------------------------
9510 -- Check_Aspect_At_Freeze_Point --
9511 ----------------------------------
9512
9513 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9514 Ident : constant Node_Id := Identifier (ASN);
9515 -- Identifier (use Entity field to save expression)
9516
9517 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9518
9519 T : Entity_Id := Empty;
9520 -- Type required for preanalyze call
9521
9522 begin
9523 -- On entry to this procedure, Entity (Ident) contains a copy of the
9524 -- original expression from the aspect, saved for this purpose.
9525
9526 -- On exit from this procedure Entity (Ident) is unchanged, still
9527 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9528 -- of the expression, preanalyzed just after the freeze point.
9529
9530 -- Make a copy of the expression to be preanalyzed
9531
9532 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9533
9534 -- Find type for preanalyze call
9535
9536 case A_Id is
9537
9538 -- No_Aspect should be impossible
9539
9540 when No_Aspect =>
9541 raise Program_Error;
9542
9543 -- Aspects taking an optional boolean argument
9544
9545 when Boolean_Aspects
9546 | Library_Unit_Aspects
9547 =>
9548 T := Standard_Boolean;
9549
9550 -- Aspects corresponding to attribute definition clauses
9551
9552 when Aspect_Address =>
9553 T := RTE (RE_Address);
9554
9555 when Aspect_Attach_Handler =>
9556 T := RTE (RE_Interrupt_ID);
9557
9558 when Aspect_Bit_Order
9559 | Aspect_Scalar_Storage_Order
9560 =>
9561 T := RTE (RE_Bit_Order);
9562
9563 when Aspect_Convention =>
9564 return;
9565
9566 when Aspect_CPU =>
9567 T := RTE (RE_CPU_Range);
9568
9569 -- Default_Component_Value is resolved with the component type
9570
9571 when Aspect_Default_Component_Value =>
9572 T := Component_Type (Entity (ASN));
9573
9574 when Aspect_Default_Storage_Pool =>
9575 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9576
9577 -- Default_Value is resolved with the type entity in question
9578
9579 when Aspect_Default_Value =>
9580 T := Entity (ASN);
9581
9582 when Aspect_Dispatching_Domain =>
9583 T := RTE (RE_Dispatching_Domain);
9584
9585 when Aspect_External_Tag =>
9586 T := Standard_String;
9587
9588 when Aspect_External_Name =>
9589 T := Standard_String;
9590
9591 when Aspect_Link_Name =>
9592 T := Standard_String;
9593
9594 when Aspect_Interrupt_Priority
9595 | Aspect_Priority
9596 =>
9597 T := Standard_Integer;
9598
9599 when Aspect_Relative_Deadline =>
9600 T := RTE (RE_Time_Span);
9601
9602 when Aspect_Secondary_Stack_Size =>
9603 T := Standard_Integer;
9604
9605 when Aspect_Small =>
9606
9607 -- Note that the expression can be of any real type (not just a
9608 -- real universal literal) as long as it is a static constant.
9609
9610 T := Any_Real;
9611
9612 -- For a simple storage pool, we have to retrieve the type of the
9613 -- pool object associated with the aspect's corresponding attribute
9614 -- definition clause.
9615
9616 when Aspect_Simple_Storage_Pool =>
9617 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9618
9619 when Aspect_Storage_Pool =>
9620 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9621
9622 when Aspect_Alignment
9623 | Aspect_Component_Size
9624 | Aspect_Machine_Radix
9625 | Aspect_Object_Size
9626 | Aspect_Size
9627 | Aspect_Storage_Size
9628 | Aspect_Stream_Size
9629 | Aspect_Value_Size
9630 =>
9631 T := Any_Integer;
9632
9633 when Aspect_Linker_Section =>
9634 T := Standard_String;
9635
9636 when Aspect_Synchronization =>
9637 return;
9638
9639 -- Special case, the expression of these aspects is just an entity
9640 -- that does not need any resolution, so just analyze.
9641
9642 when Aspect_Input
9643 | Aspect_Output
9644 | Aspect_Read
9645 | Aspect_Suppress
9646 | Aspect_Unsuppress
9647 | Aspect_Warnings
9648 | Aspect_Write
9649 =>
9650 Analyze (Expression (ASN));
9651 return;
9652
9653 -- Same for Iterator aspects, where the expression is a function
9654 -- name. Legality rules are checked separately.
9655
9656 when Aspect_Constant_Indexing
9657 | Aspect_Default_Iterator
9658 | Aspect_Iterator_Element
9659 | Aspect_Variable_Indexing
9660 =>
9661 Analyze (Expression (ASN));
9662 return;
9663
9664 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9665
9666 when Aspect_Iterable =>
9667 T := Entity (ASN);
9668
9669 declare
9670 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9671 Assoc : Node_Id;
9672 Expr : Node_Id;
9673
9674 begin
9675 if Cursor = Any_Type then
9676 return;
9677 end if;
9678
9679 Assoc := First (Component_Associations (Expression (ASN)));
9680 while Present (Assoc) loop
9681 Expr := Expression (Assoc);
9682 Analyze (Expr);
9683
9684 if not Error_Posted (Expr) then
9685 Resolve_Iterable_Operation
9686 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9687 end if;
9688
9689 Next (Assoc);
9690 end loop;
9691 end;
9692
9693 return;
9694
9695 -- Invariant/Predicate take boolean expressions
9696
9697 when Aspect_Dynamic_Predicate
9698 | Aspect_Invariant
9699 | Aspect_Predicate
9700 | Aspect_Static_Predicate
9701 | Aspect_Type_Invariant
9702 =>
9703 T := Standard_Boolean;
9704
9705 when Aspect_Predicate_Failure =>
9706 T := Standard_String;
9707
9708 -- Here is the list of aspects that don't require delay analysis
9709
9710 when Aspect_Abstract_State
9711 | Aspect_Annotate
9712 | Aspect_Async_Readers
9713 | Aspect_Async_Writers
9714 | Aspect_Constant_After_Elaboration
9715 | Aspect_Contract_Cases
9716 | Aspect_Default_Initial_Condition
9717 | Aspect_Depends
9718 | Aspect_Dimension
9719 | Aspect_Dimension_System
9720 | Aspect_Effective_Reads
9721 | Aspect_Effective_Writes
9722 | Aspect_Extensions_Visible
9723 | Aspect_Ghost
9724 | Aspect_Global
9725 | Aspect_Implicit_Dereference
9726 | Aspect_Initial_Condition
9727 | Aspect_Initializes
9728 | Aspect_Max_Entry_Queue_Depth
9729 | Aspect_Max_Entry_Queue_Length
9730 | Aspect_Max_Queue_Length
9731 | Aspect_No_Caching
9732 | Aspect_Obsolescent
9733 | Aspect_Part_Of
9734 | Aspect_Post
9735 | Aspect_Postcondition
9736 | Aspect_Pre
9737 | Aspect_Precondition
9738 | Aspect_Refined_Depends
9739 | Aspect_Refined_Global
9740 | Aspect_Refined_Post
9741 | Aspect_Refined_State
9742 | Aspect_SPARK_Mode
9743 | Aspect_Test_Case
9744 | Aspect_Unimplemented
9745 | Aspect_Volatile_Function
9746 =>
9747 raise Program_Error;
9748
9749 end case;
9750
9751 -- Do the preanalyze call
9752
9753 Preanalyze_Spec_Expression (Expression (ASN), T);
9754 end Check_Aspect_At_Freeze_Point;
9755
9756 -----------------------------------
9757 -- Check_Constant_Address_Clause --
9758 -----------------------------------
9759
9760 procedure Check_Constant_Address_Clause
9761 (Expr : Node_Id;
9762 U_Ent : Entity_Id)
9763 is
9764 procedure Check_At_Constant_Address (Nod : Node_Id);
9765 -- Checks that the given node N represents a name whose 'Address is
9766 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9767 -- address value is the same at the point of declaration of U_Ent and at
9768 -- the time of elaboration of the address clause.
9769
9770 procedure Check_Expr_Constants (Nod : Node_Id);
9771 -- Checks that Nod meets the requirements for a constant address clause
9772 -- in the sense of the enclosing procedure.
9773
9774 procedure Check_List_Constants (Lst : List_Id);
9775 -- Check that all elements of list Lst meet the requirements for a
9776 -- constant address clause in the sense of the enclosing procedure.
9777
9778 -------------------------------
9779 -- Check_At_Constant_Address --
9780 -------------------------------
9781
9782 procedure Check_At_Constant_Address (Nod : Node_Id) is
9783 begin
9784 if Is_Entity_Name (Nod) then
9785 if Present (Address_Clause (Entity ((Nod)))) then
9786 Error_Msg_NE
9787 ("invalid address clause for initialized object &!",
9788 Nod, U_Ent);
9789 Error_Msg_NE
9790 ("address for& cannot depend on another address clause! "
9791 & "(RM 13.1(22))!", Nod, U_Ent);
9792
9793 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9794 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9795 then
9796 Error_Msg_NE
9797 ("invalid address clause for initialized object &!",
9798 Nod, U_Ent);
9799 Error_Msg_Node_2 := U_Ent;
9800 Error_Msg_NE
9801 ("\& must be defined before & (RM 13.1(22))!",
9802 Nod, Entity (Nod));
9803 end if;
9804
9805 elsif Nkind (Nod) = N_Selected_Component then
9806 declare
9807 T : constant Entity_Id := Etype (Prefix (Nod));
9808
9809 begin
9810 if (Is_Record_Type (T)
9811 and then Has_Discriminants (T))
9812 or else
9813 (Is_Access_Type (T)
9814 and then Is_Record_Type (Designated_Type (T))
9815 and then Has_Discriminants (Designated_Type (T)))
9816 then
9817 Error_Msg_NE
9818 ("invalid address clause for initialized object &!",
9819 Nod, U_Ent);
9820 Error_Msg_N
9821 ("\address cannot depend on component of discriminated "
9822 & "record (RM 13.1(22))!", Nod);
9823 else
9824 Check_At_Constant_Address (Prefix (Nod));
9825 end if;
9826 end;
9827
9828 elsif Nkind (Nod) = N_Indexed_Component then
9829 Check_At_Constant_Address (Prefix (Nod));
9830 Check_List_Constants (Expressions (Nod));
9831
9832 else
9833 Check_Expr_Constants (Nod);
9834 end if;
9835 end Check_At_Constant_Address;
9836
9837 --------------------------
9838 -- Check_Expr_Constants --
9839 --------------------------
9840
9841 procedure Check_Expr_Constants (Nod : Node_Id) is
9842 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9843 Ent : Entity_Id := Empty;
9844
9845 begin
9846 if Nkind (Nod) in N_Has_Etype
9847 and then Etype (Nod) = Any_Type
9848 then
9849 return;
9850 end if;
9851
9852 case Nkind (Nod) is
9853 when N_Empty
9854 | N_Error
9855 =>
9856 return;
9857
9858 when N_Expanded_Name
9859 | N_Identifier
9860 =>
9861 Ent := Entity (Nod);
9862
9863 -- We need to look at the original node if it is different
9864 -- from the node, since we may have rewritten things and
9865 -- substituted an identifier representing the rewrite.
9866
9867 if Is_Rewrite_Substitution (Nod) then
9868 Check_Expr_Constants (Original_Node (Nod));
9869
9870 -- If the node is an object declaration without initial
9871 -- value, some code has been expanded, and the expression
9872 -- is not constant, even if the constituents might be
9873 -- acceptable, as in A'Address + offset.
9874
9875 if Ekind (Ent) = E_Variable
9876 and then
9877 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9878 and then
9879 No (Expression (Declaration_Node (Ent)))
9880 then
9881 Error_Msg_NE
9882 ("invalid address clause for initialized object &!",
9883 Nod, U_Ent);
9884
9885 -- If entity is constant, it may be the result of expanding
9886 -- a check. We must verify that its declaration appears
9887 -- before the object in question, else we also reject the
9888 -- address clause.
9889
9890 elsif Ekind (Ent) = E_Constant
9891 and then In_Same_Source_Unit (Ent, U_Ent)
9892 and then Sloc (Ent) > Loc_U_Ent
9893 then
9894 Error_Msg_NE
9895 ("invalid address clause for initialized object &!",
9896 Nod, U_Ent);
9897 end if;
9898
9899 return;
9900 end if;
9901
9902 -- Otherwise look at the identifier and see if it is OK
9903
9904 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9905 or else Is_Type (Ent)
9906 then
9907 return;
9908
9909 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9910
9911 -- This is the case where we must have Ent defined before
9912 -- U_Ent. Clearly if they are in different units this
9913 -- requirement is met since the unit containing Ent is
9914 -- already processed.
9915
9916 if not In_Same_Source_Unit (Ent, U_Ent) then
9917 return;
9918
9919 -- Otherwise location of Ent must be before the location
9920 -- of U_Ent, that's what prior defined means.
9921
9922 elsif Sloc (Ent) < Loc_U_Ent then
9923 return;
9924
9925 else
9926 Error_Msg_NE
9927 ("invalid address clause for initialized object &!",
9928 Nod, U_Ent);
9929 Error_Msg_Node_2 := U_Ent;
9930 Error_Msg_NE
9931 ("\& must be defined before & (RM 13.1(22))!",
9932 Nod, Ent);
9933 end if;
9934
9935 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9936 Check_Expr_Constants (Original_Node (Nod));
9937
9938 else
9939 Error_Msg_NE
9940 ("invalid address clause for initialized object &!",
9941 Nod, U_Ent);
9942
9943 if Comes_From_Source (Ent) then
9944 Error_Msg_NE
9945 ("\reference to variable& not allowed"
9946 & " (RM 13.1(22))!", Nod, Ent);
9947 else
9948 Error_Msg_N
9949 ("non-static expression not allowed"
9950 & " (RM 13.1(22))!", Nod);
9951 end if;
9952 end if;
9953
9954 when N_Integer_Literal =>
9955
9956 -- If this is a rewritten unchecked conversion, in a system
9957 -- where Address is an integer type, always use the base type
9958 -- for a literal value. This is user-friendly and prevents
9959 -- order-of-elaboration issues with instances of unchecked
9960 -- conversion.
9961
9962 if Nkind (Original_Node (Nod)) = N_Function_Call then
9963 Set_Etype (Nod, Base_Type (Etype (Nod)));
9964 end if;
9965
9966 when N_Character_Literal
9967 | N_Real_Literal
9968 | N_String_Literal
9969 =>
9970 return;
9971
9972 when N_Range =>
9973 Check_Expr_Constants (Low_Bound (Nod));
9974 Check_Expr_Constants (High_Bound (Nod));
9975
9976 when N_Explicit_Dereference =>
9977 Check_Expr_Constants (Prefix (Nod));
9978
9979 when N_Indexed_Component =>
9980 Check_Expr_Constants (Prefix (Nod));
9981 Check_List_Constants (Expressions (Nod));
9982
9983 when N_Slice =>
9984 Check_Expr_Constants (Prefix (Nod));
9985 Check_Expr_Constants (Discrete_Range (Nod));
9986
9987 when N_Selected_Component =>
9988 Check_Expr_Constants (Prefix (Nod));
9989
9990 when N_Attribute_Reference =>
9991 if Nam_In (Attribute_Name (Nod), Name_Address,
9992 Name_Access,
9993 Name_Unchecked_Access,
9994 Name_Unrestricted_Access)
9995 then
9996 Check_At_Constant_Address (Prefix (Nod));
9997
9998 -- Normally, System'To_Address will have been transformed into
9999 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
10000 -- and we don't want to give an error, because the whole point
10001 -- of 'To_Address is that it is static.
10002
10003 elsif Attribute_Name (Nod) = Name_To_Address then
10004 pragma Assert (Operating_Mode = Check_Semantics);
10005 null;
10006
10007 else
10008 Check_Expr_Constants (Prefix (Nod));
10009 Check_List_Constants (Expressions (Nod));
10010 end if;
10011
10012 when N_Aggregate =>
10013 Check_List_Constants (Component_Associations (Nod));
10014 Check_List_Constants (Expressions (Nod));
10015
10016 when N_Component_Association =>
10017 Check_Expr_Constants (Expression (Nod));
10018
10019 when N_Extension_Aggregate =>
10020 Check_Expr_Constants (Ancestor_Part (Nod));
10021 Check_List_Constants (Component_Associations (Nod));
10022 Check_List_Constants (Expressions (Nod));
10023
10024 when N_Null =>
10025 return;
10026
10027 when N_Binary_Op
10028 | N_Membership_Test
10029 | N_Short_Circuit
10030 =>
10031 Check_Expr_Constants (Left_Opnd (Nod));
10032 Check_Expr_Constants (Right_Opnd (Nod));
10033
10034 when N_Unary_Op =>
10035 Check_Expr_Constants (Right_Opnd (Nod));
10036
10037 when N_Allocator
10038 | N_Qualified_Expression
10039 | N_Type_Conversion
10040 | N_Unchecked_Type_Conversion
10041 =>
10042 Check_Expr_Constants (Expression (Nod));
10043
10044 when N_Function_Call =>
10045 if not Is_Pure (Entity (Name (Nod))) then
10046 Error_Msg_NE
10047 ("invalid address clause for initialized object &!",
10048 Nod, U_Ent);
10049
10050 Error_Msg_NE
10051 ("\function & is not pure (RM 13.1(22))!",
10052 Nod, Entity (Name (Nod)));
10053
10054 else
10055 Check_List_Constants (Parameter_Associations (Nod));
10056 end if;
10057
10058 when N_Parameter_Association =>
10059 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
10060
10061 when others =>
10062 Error_Msg_NE
10063 ("invalid address clause for initialized object &!",
10064 Nod, U_Ent);
10065 Error_Msg_NE
10066 ("\must be constant defined before& (RM 13.1(22))!",
10067 Nod, U_Ent);
10068 end case;
10069 end Check_Expr_Constants;
10070
10071 --------------------------
10072 -- Check_List_Constants --
10073 --------------------------
10074
10075 procedure Check_List_Constants (Lst : List_Id) is
10076 Nod1 : Node_Id;
10077
10078 begin
10079 if Present (Lst) then
10080 Nod1 := First (Lst);
10081 while Present (Nod1) loop
10082 Check_Expr_Constants (Nod1);
10083 Next (Nod1);
10084 end loop;
10085 end if;
10086 end Check_List_Constants;
10087
10088 -- Start of processing for Check_Constant_Address_Clause
10089
10090 begin
10091 -- If rep_clauses are to be ignored, no need for legality checks. In
10092 -- particular, no need to pester user about rep clauses that violate the
10093 -- rule on constant addresses, given that these clauses will be removed
10094 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
10095 -- we want to relax these checks.
10096
10097 if not Ignore_Rep_Clauses and not CodePeer_Mode then
10098 Check_Expr_Constants (Expr);
10099 end if;
10100 end Check_Constant_Address_Clause;
10101
10102 ---------------------------
10103 -- Check_Pool_Size_Clash --
10104 ---------------------------
10105
10106 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
10107 Post : Node_Id;
10108
10109 begin
10110 -- We need to find out which one came first. Note that in the case of
10111 -- aspects mixed with pragmas there are cases where the processing order
10112 -- is reversed, which is why we do the check here.
10113
10114 if Sloc (SP) < Sloc (SS) then
10115 Error_Msg_Sloc := Sloc (SP);
10116 Post := SS;
10117 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
10118
10119 else
10120 Error_Msg_Sloc := Sloc (SS);
10121 Post := SP;
10122 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
10123 end if;
10124
10125 Error_Msg_N
10126 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
10127 end Check_Pool_Size_Clash;
10128
10129 ----------------------------------------
10130 -- Check_Record_Representation_Clause --
10131 ----------------------------------------
10132
10133 procedure Check_Record_Representation_Clause (N : Node_Id) is
10134 Loc : constant Source_Ptr := Sloc (N);
10135 Ident : constant Node_Id := Identifier (N);
10136 Rectype : Entity_Id;
10137 Fent : Entity_Id;
10138 CC : Node_Id;
10139 Fbit : Uint;
10140 Lbit : Uint;
10141 Hbit : Uint := Uint_0;
10142 Comp : Entity_Id;
10143 Pcomp : Entity_Id;
10144
10145 Max_Bit_So_Far : Uint;
10146 -- Records the maximum bit position so far. If all field positions
10147 -- are monotonically increasing, then we can skip the circuit for
10148 -- checking for overlap, since no overlap is possible.
10149
10150 Tagged_Parent : Entity_Id := Empty;
10151 -- This is set in the case of an extension for which we have either a
10152 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
10153 -- components are positioned by record representation clauses) on the
10154 -- parent type. In this case we check for overlap between components of
10155 -- this tagged type and the parent component. Tagged_Parent will point
10156 -- to this parent type. For all other cases, Tagged_Parent is Empty.
10157
10158 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
10159 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
10160 -- last bit position for any field in the parent type. We only need to
10161 -- check overlap for fields starting below this point.
10162
10163 Overlap_Check_Required : Boolean;
10164 -- Used to keep track of whether or not an overlap check is required
10165
10166 Overlap_Detected : Boolean := False;
10167 -- Set True if an overlap is detected
10168
10169 Ccount : Natural := 0;
10170 -- Number of component clauses in record rep clause
10171
10172 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
10173 -- Given two entities for record components or discriminants, checks
10174 -- if they have overlapping component clauses and issues errors if so.
10175
10176 procedure Find_Component;
10177 -- Finds component entity corresponding to current component clause (in
10178 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
10179 -- start/stop bits for the field. If there is no matching component or
10180 -- if the matching component does not have a component clause, then
10181 -- that's an error and Comp is set to Empty, but no error message is
10182 -- issued, since the message was already given. Comp is also set to
10183 -- Empty if the current "component clause" is in fact a pragma.
10184
10185 procedure Record_Hole_Check
10186 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean);
10187 -- Checks for gaps in the given Rectype. Compute After_Last, the bit
10188 -- number after the last component. Warn is True on the initial call,
10189 -- and warnings are given for gaps. For a type extension, this is called
10190 -- recursively to compute After_Last for the parent type; in this case
10191 -- Warn is False and the warnings are suppressed.
10192
10193 procedure Component_Order_Check (Rectype : Entity_Id);
10194 -- Check that the order of component clauses agrees with the order of
10195 -- component declarations, and that the component clauses are given in
10196 -- increasing order of bit offset.
10197
10198 -----------------------------
10199 -- Check_Component_Overlap --
10200 -----------------------------
10201
10202 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
10203 CC1 : constant Node_Id := Component_Clause (C1_Ent);
10204 CC2 : constant Node_Id := Component_Clause (C2_Ent);
10205
10206 begin
10207 if Present (CC1) and then Present (CC2) then
10208
10209 -- Exclude odd case where we have two tag components in the same
10210 -- record, both at location zero. This seems a bit strange, but
10211 -- it seems to happen in some circumstances, perhaps on an error.
10212
10213 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
10214 return;
10215 end if;
10216
10217 -- Here we check if the two fields overlap
10218
10219 declare
10220 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
10221 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
10222 E1 : constant Uint := S1 + Esize (C1_Ent);
10223 E2 : constant Uint := S2 + Esize (C2_Ent);
10224
10225 begin
10226 if E2 <= S1 or else E1 <= S2 then
10227 null;
10228 else
10229 Error_Msg_Node_2 := Component_Name (CC2);
10230 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
10231 Error_Msg_Node_1 := Component_Name (CC1);
10232 Error_Msg_N
10233 ("component& overlaps & #", Component_Name (CC1));
10234 Overlap_Detected := True;
10235 end if;
10236 end;
10237 end if;
10238 end Check_Component_Overlap;
10239
10240 ---------------------------
10241 -- Component_Order_Check --
10242 ---------------------------
10243
10244 procedure Component_Order_Check (Rectype : Entity_Id) is
10245 Comp : Entity_Id := First_Component (Rectype);
10246 Clause : Node_Id := First (Component_Clauses (N));
10247 Prev_Bit_Offset : Uint := Uint_0;
10248 OOO : constant String :=
10249 "?component clause out of order with respect to declaration";
10250
10251 begin
10252 -- Step Comp through components and Clause through component clauses,
10253 -- skipping pragmas. We ignore discriminants and variant parts,
10254 -- because we get most of the benefit from the plain vanilla
10255 -- component cases, without the extra complexity. If we find a Comp
10256 -- and Clause that don't match, give a warning on both and quit. If
10257 -- we find two subsequent clauses out of order by bit layout, give
10258 -- warning and quit. On each iteration, Prev_Bit_Offset is the one
10259 -- from the previous iteration (or 0 to start).
10260
10261 while Present (Comp) and then Present (Clause) loop
10262 if Nkind (Clause) = N_Component_Clause
10263 and then Ekind (Entity (Component_Name (Clause))) = E_Component
10264 then
10265 if Entity (Component_Name (Clause)) /= Comp then
10266 Error_Msg_N (OOO, Comp);
10267 Error_Msg_N (OOO, Clause);
10268 exit;
10269 end if;
10270
10271 if not Reverse_Bit_Order (Rectype)
10272 and then not Reverse_Storage_Order (Rectype)
10273 and then Component_Bit_Offset (Comp) < Prev_Bit_Offset
10274 then
10275 Error_Msg_N ("?memory layout out of order", Clause);
10276 exit;
10277 end if;
10278
10279 Prev_Bit_Offset := Component_Bit_Offset (Comp);
10280 Comp := Next_Component (Comp);
10281 end if;
10282
10283 Next (Clause);
10284 end loop;
10285 end Component_Order_Check;
10286
10287 --------------------
10288 -- Find_Component --
10289 --------------------
10290
10291 procedure Find_Component is
10292
10293 procedure Search_Component (R : Entity_Id);
10294 -- Search components of R for a match. If found, Comp is set
10295
10296 ----------------------
10297 -- Search_Component --
10298 ----------------------
10299
10300 procedure Search_Component (R : Entity_Id) is
10301 begin
10302 Comp := First_Component_Or_Discriminant (R);
10303 while Present (Comp) loop
10304
10305 -- Ignore error of attribute name for component name (we
10306 -- already gave an error message for this, so no need to
10307 -- complain here)
10308
10309 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
10310 null;
10311 else
10312 exit when Chars (Comp) = Chars (Component_Name (CC));
10313 end if;
10314
10315 Next_Component_Or_Discriminant (Comp);
10316 end loop;
10317 end Search_Component;
10318
10319 -- Start of processing for Find_Component
10320
10321 begin
10322 -- Return with Comp set to Empty if we have a pragma
10323
10324 if Nkind (CC) = N_Pragma then
10325 Comp := Empty;
10326 return;
10327 end if;
10328
10329 -- Search current record for matching component
10330
10331 Search_Component (Rectype);
10332
10333 -- If not found, maybe component of base type discriminant that is
10334 -- absent from statically constrained first subtype.
10335
10336 if No (Comp) then
10337 Search_Component (Base_Type (Rectype));
10338 end if;
10339
10340 -- If no component, or the component does not reference the component
10341 -- clause in question, then there was some previous error for which
10342 -- we already gave a message, so just return with Comp Empty.
10343
10344 if No (Comp) or else Component_Clause (Comp) /= CC then
10345 Check_Error_Detected;
10346 Comp := Empty;
10347
10348 -- Normal case where we have a component clause
10349
10350 else
10351 Fbit := Component_Bit_Offset (Comp);
10352 Lbit := Fbit + Esize (Comp) - 1;
10353 end if;
10354 end Find_Component;
10355
10356 -----------------------
10357 -- Record_Hole_Check --
10358 -----------------------
10359
10360 procedure Record_Hole_Check
10361 (Rectype : Entity_Id; After_Last : out Uint; Warn : Boolean)
10362 is
10363 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10364 -- Full declaration of record type
10365
10366 procedure Check_Component_List
10367 (DS : List_Id;
10368 CL : Node_Id;
10369 Sbit : Uint;
10370 Abit : out Uint);
10371 -- Check component list CL for holes. DS is a list of discriminant
10372 -- specifications to be included in the consideration of components.
10373 -- Sbit is the starting bit, which is zero if there are no preceding
10374 -- components (before a variant part, or a parent type, or a tag
10375 -- field). If there are preceding components, Sbit is the bit just
10376 -- after the last such component. Abit is set to the bit just after
10377 -- the last component of DS and CL.
10378
10379 --------------------------
10380 -- Check_Component_List --
10381 --------------------------
10382
10383 procedure Check_Component_List
10384 (DS : List_Id;
10385 CL : Node_Id;
10386 Sbit : Uint;
10387 Abit : out Uint)
10388 is
10389 Compl : Integer;
10390
10391 begin
10392 Compl := Integer (List_Length (Component_Items (CL)));
10393
10394 if DS /= No_List then
10395 Compl := Compl + Integer (List_Length (DS));
10396 end if;
10397
10398 declare
10399 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10400 -- Gather components (zero entry is for sort routine)
10401
10402 Ncomps : Natural := 0;
10403 -- Number of entries stored in Comps (starting at Comps (1))
10404
10405 Citem : Node_Id;
10406 -- One component item or discriminant specification
10407
10408 Nbit : Uint;
10409 -- Starting bit for next component
10410
10411 CEnt : Entity_Id;
10412 -- Component entity
10413
10414 Variant : Node_Id;
10415 -- One variant
10416
10417 function Lt (Op1, Op2 : Natural) return Boolean;
10418 -- Compare routine for Sort
10419
10420 procedure Move (From : Natural; To : Natural);
10421 -- Move routine for Sort
10422
10423 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10424
10425 --------
10426 -- Lt --
10427 --------
10428
10429 function Lt (Op1, Op2 : Natural) return Boolean is
10430 begin
10431 return Component_Bit_Offset (Comps (Op1))
10432 < Component_Bit_Offset (Comps (Op2));
10433 end Lt;
10434
10435 ----------
10436 -- Move --
10437 ----------
10438
10439 procedure Move (From : Natural; To : Natural) is
10440 begin
10441 Comps (To) := Comps (From);
10442 end Move;
10443
10444 begin
10445 -- Gather discriminants into Comp
10446
10447 if DS /= No_List then
10448 Citem := First (DS);
10449 while Present (Citem) loop
10450 if Nkind (Citem) = N_Discriminant_Specification then
10451 declare
10452 Ent : constant Entity_Id :=
10453 Defining_Identifier (Citem);
10454 begin
10455 if Ekind (Ent) = E_Discriminant then
10456 Ncomps := Ncomps + 1;
10457 Comps (Ncomps) := Ent;
10458 end if;
10459 end;
10460 end if;
10461
10462 Next (Citem);
10463 end loop;
10464 end if;
10465
10466 -- Gather component entities into Comp
10467
10468 Citem := First (Component_Items (CL));
10469 while Present (Citem) loop
10470 if Nkind (Citem) = N_Component_Declaration then
10471 Ncomps := Ncomps + 1;
10472 Comps (Ncomps) := Defining_Identifier (Citem);
10473 end if;
10474
10475 Next (Citem);
10476 end loop;
10477
10478 -- Now sort the component entities based on the first bit.
10479 -- Note we already know there are no overlapping components.
10480
10481 Sorting.Sort (Ncomps);
10482
10483 -- Loop through entries checking for holes
10484
10485 Nbit := Sbit;
10486 for J in 1 .. Ncomps loop
10487 CEnt := Comps (J);
10488
10489 declare
10490 CBO : constant Uint := Component_Bit_Offset (CEnt);
10491
10492 begin
10493 -- Skip components with unknown offsets
10494
10495 if CBO /= No_Uint and then CBO >= 0 then
10496 Error_Msg_Uint_1 := CBO - Nbit;
10497
10498 if Warn and then Error_Msg_Uint_1 > 0 then
10499 Error_Msg_NE
10500 ("?H?^-bit gap before component&",
10501 Component_Name (Component_Clause (CEnt)),
10502 CEnt);
10503 end if;
10504
10505 Nbit := CBO + Esize (CEnt);
10506 end if;
10507 end;
10508 end loop;
10509
10510 -- Set Abit to just after the last nonvariant component
10511
10512 Abit := Nbit;
10513
10514 -- Process variant parts recursively if present. Set Abit to
10515 -- the maximum for all variant parts.
10516
10517 if Present (Variant_Part (CL)) then
10518 declare
10519 Var_Start : constant Uint := Nbit;
10520 begin
10521 Variant := First (Variants (Variant_Part (CL)));
10522 while Present (Variant) loop
10523 Check_Component_List
10524 (No_List, Component_List (Variant), Var_Start, Nbit);
10525 Next (Variant);
10526 if Nbit > Abit then
10527 Abit := Nbit;
10528 end if;
10529 end loop;
10530 end;
10531 end if;
10532 end;
10533 end Check_Component_List;
10534
10535 Sbit : Uint;
10536 -- Starting bit for call to Check_Component_List. Zero for an
10537 -- untagged type. The size of the Tag for a nonderived tagged
10538 -- type. Parent size for a type extension.
10539
10540 Record_Definition : Node_Id;
10541 -- Record_Definition containing Component_List to pass to
10542 -- Check_Component_List.
10543
10544 -- Start of processing for Record_Hole_Check
10545
10546 begin
10547 if Is_Tagged_Type (Rectype) then
10548 Sbit := UI_From_Int (System_Address_Size);
10549 else
10550 Sbit := Uint_0;
10551 end if;
10552
10553 After_Last := Uint_0;
10554
10555 if Nkind (Decl) = N_Full_Type_Declaration then
10556 Record_Definition := Type_Definition (Decl);
10557
10558 -- If we have a record extension, set Sbit to point after the last
10559 -- component of the parent type, by calling Record_Hole_Check
10560 -- recursively.
10561
10562 if Nkind (Record_Definition) = N_Derived_Type_Definition then
10563 Record_Definition := Record_Extension_Part (Record_Definition);
10564 Record_Hole_Check (Underlying_Type (Parent_Subtype (Rectype)),
10565 After_Last => Sbit, Warn => False);
10566 end if;
10567
10568 if Nkind (Record_Definition) = N_Record_Definition then
10569 Check_Component_List
10570 (Discriminant_Specifications (Decl),
10571 Component_List (Record_Definition),
10572 Sbit, After_Last);
10573 end if;
10574 end if;
10575 end Record_Hole_Check;
10576
10577 -- Start of processing for Check_Record_Representation_Clause
10578
10579 begin
10580 Find_Type (Ident);
10581 Rectype := Entity (Ident);
10582
10583 if Rectype = Any_Type then
10584 return;
10585 end if;
10586
10587 Rectype := Underlying_Type (Rectype);
10588
10589 -- See if we have a fully repped derived tagged type
10590
10591 declare
10592 PS : constant Entity_Id := Parent_Subtype (Rectype);
10593
10594 begin
10595 if Present (PS) and then Known_Static_RM_Size (PS) then
10596 Tagged_Parent := PS;
10597 Parent_Last_Bit := RM_Size (PS) - 1;
10598
10599 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
10600 Tagged_Parent := PS;
10601
10602 -- Find maximum bit of any component of the parent type
10603
10604 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
10605 Pcomp := First_Entity (Tagged_Parent);
10606 while Present (Pcomp) loop
10607 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
10608 if Component_Bit_Offset (Pcomp) /= No_Uint
10609 and then Known_Static_Esize (Pcomp)
10610 then
10611 Parent_Last_Bit :=
10612 UI_Max
10613 (Parent_Last_Bit,
10614 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
10615 end if;
10616 else
10617
10618 -- Skip anonymous types generated for constrained array
10619 -- or record components.
10620
10621 null;
10622 end if;
10623
10624 Next_Entity (Pcomp);
10625 end loop;
10626 end if;
10627 end;
10628
10629 -- All done if no component clauses
10630
10631 CC := First (Component_Clauses (N));
10632
10633 if No (CC) then
10634 return;
10635 end if;
10636
10637 -- If a tag is present, then create a component clause that places it
10638 -- at the start of the record (otherwise gigi may place it after other
10639 -- fields that have rep clauses).
10640
10641 Fent := First_Entity (Rectype);
10642
10643 if Nkind (Fent) = N_Defining_Identifier
10644 and then Chars (Fent) = Name_uTag
10645 then
10646 Set_Component_Bit_Offset (Fent, Uint_0);
10647 Set_Normalized_Position (Fent, Uint_0);
10648 Set_Normalized_First_Bit (Fent, Uint_0);
10649 Set_Normalized_Position_Max (Fent, Uint_0);
10650 Init_Esize (Fent, System_Address_Size);
10651
10652 Set_Component_Clause (Fent,
10653 Make_Component_Clause (Loc,
10654 Component_Name => Make_Identifier (Loc, Name_uTag),
10655
10656 Position => Make_Integer_Literal (Loc, Uint_0),
10657 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10658 Last_Bit =>
10659 Make_Integer_Literal (Loc,
10660 UI_From_Int (System_Address_Size - 1))));
10661
10662 Ccount := Ccount + 1;
10663 end if;
10664
10665 Max_Bit_So_Far := Uint_Minus_1;
10666 Overlap_Check_Required := False;
10667
10668 -- Process the component clauses
10669
10670 while Present (CC) loop
10671 Find_Component;
10672
10673 if Present (Comp) then
10674 Ccount := Ccount + 1;
10675
10676 -- We need a full overlap check if record positions non-monotonic
10677
10678 if Fbit <= Max_Bit_So_Far then
10679 Overlap_Check_Required := True;
10680 end if;
10681
10682 Max_Bit_So_Far := Lbit;
10683
10684 -- Check bit position out of range of specified size
10685
10686 if Has_Size_Clause (Rectype)
10687 and then RM_Size (Rectype) <= Lbit
10688 then
10689 Error_Msg_N
10690 ("bit number out of range of specified size",
10691 Last_Bit (CC));
10692
10693 -- Check for overlap with tag or parent component
10694
10695 else
10696 if Is_Tagged_Type (Rectype)
10697 and then Fbit < System_Address_Size
10698 then
10699 Error_Msg_NE
10700 ("component overlaps tag field of&",
10701 Component_Name (CC), Rectype);
10702 Overlap_Detected := True;
10703
10704 elsif Present (Tagged_Parent)
10705 and then Fbit <= Parent_Last_Bit
10706 then
10707 Error_Msg_NE
10708 ("component overlaps parent field of&",
10709 Component_Name (CC), Rectype);
10710 Overlap_Detected := True;
10711 end if;
10712
10713 if Hbit < Lbit then
10714 Hbit := Lbit;
10715 end if;
10716 end if;
10717 end if;
10718
10719 Next (CC);
10720 end loop;
10721
10722 -- Now that we have processed all the component clauses, check for
10723 -- overlap. We have to leave this till last, since the components can
10724 -- appear in any arbitrary order in the representation clause.
10725
10726 -- We do not need this check if all specified ranges were monotonic,
10727 -- as recorded by Overlap_Check_Required being False at this stage.
10728
10729 -- This first section checks if there are any overlapping entries at
10730 -- all. It does this by sorting all entries and then seeing if there are
10731 -- any overlaps. If there are none, then that is decisive, but if there
10732 -- are overlaps, they may still be OK (they may result from fields in
10733 -- different variants).
10734
10735 if Overlap_Check_Required then
10736 Overlap_Check1 : declare
10737
10738 OC_Fbit : array (0 .. Ccount) of Uint;
10739 -- First-bit values for component clauses, the value is the offset
10740 -- of the first bit of the field from start of record. The zero
10741 -- entry is for use in sorting.
10742
10743 OC_Lbit : array (0 .. Ccount) of Uint;
10744 -- Last-bit values for component clauses, the value is the offset
10745 -- of the last bit of the field from start of record. The zero
10746 -- entry is for use in sorting.
10747
10748 OC_Count : Natural := 0;
10749 -- Count of entries in OC_Fbit and OC_Lbit
10750
10751 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10752 -- Compare routine for Sort
10753
10754 procedure OC_Move (From : Natural; To : Natural);
10755 -- Move routine for Sort
10756
10757 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
10758
10759 -----------
10760 -- OC_Lt --
10761 -----------
10762
10763 function OC_Lt (Op1, Op2 : Natural) return Boolean is
10764 begin
10765 return OC_Fbit (Op1) < OC_Fbit (Op2);
10766 end OC_Lt;
10767
10768 -------------
10769 -- OC_Move --
10770 -------------
10771
10772 procedure OC_Move (From : Natural; To : Natural) is
10773 begin
10774 OC_Fbit (To) := OC_Fbit (From);
10775 OC_Lbit (To) := OC_Lbit (From);
10776 end OC_Move;
10777
10778 -- Start of processing for Overlap_Check
10779
10780 begin
10781 CC := First (Component_Clauses (N));
10782 while Present (CC) loop
10783
10784 -- Exclude component clause already marked in error
10785
10786 if not Error_Posted (CC) then
10787 Find_Component;
10788
10789 if Present (Comp) then
10790 OC_Count := OC_Count + 1;
10791 OC_Fbit (OC_Count) := Fbit;
10792 OC_Lbit (OC_Count) := Lbit;
10793 end if;
10794 end if;
10795
10796 Next (CC);
10797 end loop;
10798
10799 Sorting.Sort (OC_Count);
10800
10801 Overlap_Check_Required := False;
10802 for J in 1 .. OC_Count - 1 loop
10803 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10804 Overlap_Check_Required := True;
10805 exit;
10806 end if;
10807 end loop;
10808 end Overlap_Check1;
10809 end if;
10810
10811 -- If Overlap_Check_Required is still True, then we have to do the full
10812 -- scale overlap check, since we have at least two fields that do
10813 -- overlap, and we need to know if that is OK since they are in
10814 -- different variant, or whether we have a definite problem.
10815
10816 if Overlap_Check_Required then
10817 Overlap_Check2 : declare
10818 C1_Ent, C2_Ent : Entity_Id;
10819 -- Entities of components being checked for overlap
10820
10821 Clist : Node_Id;
10822 -- Component_List node whose Component_Items are being checked
10823
10824 Citem : Node_Id;
10825 -- Component declaration for component being checked
10826
10827 begin
10828 C1_Ent := First_Entity (Base_Type (Rectype));
10829
10830 -- Loop through all components in record. For each component check
10831 -- for overlap with any of the preceding elements on the component
10832 -- list containing the component and also, if the component is in
10833 -- a variant, check against components outside the case structure.
10834 -- This latter test is repeated recursively up the variant tree.
10835
10836 Main_Component_Loop : while Present (C1_Ent) loop
10837 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10838 goto Continue_Main_Component_Loop;
10839 end if;
10840
10841 -- Skip overlap check if entity has no declaration node. This
10842 -- happens with discriminants in constrained derived types.
10843 -- Possibly we are missing some checks as a result, but that
10844 -- does not seem terribly serious.
10845
10846 if No (Declaration_Node (C1_Ent)) then
10847 goto Continue_Main_Component_Loop;
10848 end if;
10849
10850 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10851
10852 -- Loop through component lists that need checking. Check the
10853 -- current component list and all lists in variants above us.
10854
10855 Component_List_Loop : loop
10856
10857 -- If derived type definition, go to full declaration
10858 -- If at outer level, check discriminants if there are any.
10859
10860 if Nkind (Clist) = N_Derived_Type_Definition then
10861 Clist := Parent (Clist);
10862 end if;
10863
10864 -- Outer level of record definition, check discriminants
10865
10866 if Nkind_In (Clist, N_Full_Type_Declaration,
10867 N_Private_Type_Declaration)
10868 then
10869 if Has_Discriminants (Defining_Identifier (Clist)) then
10870 C2_Ent :=
10871 First_Discriminant (Defining_Identifier (Clist));
10872 while Present (C2_Ent) loop
10873 exit when C1_Ent = C2_Ent;
10874 Check_Component_Overlap (C1_Ent, C2_Ent);
10875 Next_Discriminant (C2_Ent);
10876 end loop;
10877 end if;
10878
10879 -- Record extension case
10880
10881 elsif Nkind (Clist) = N_Derived_Type_Definition then
10882 Clist := Empty;
10883
10884 -- Otherwise check one component list
10885
10886 else
10887 Citem := First (Component_Items (Clist));
10888 while Present (Citem) loop
10889 if Nkind (Citem) = N_Component_Declaration then
10890 C2_Ent := Defining_Identifier (Citem);
10891 exit when C1_Ent = C2_Ent;
10892 Check_Component_Overlap (C1_Ent, C2_Ent);
10893 end if;
10894
10895 Next (Citem);
10896 end loop;
10897 end if;
10898
10899 -- Check for variants above us (the parent of the Clist can
10900 -- be a variant, in which case its parent is a variant part,
10901 -- and the parent of the variant part is a component list
10902 -- whose components must all be checked against the current
10903 -- component for overlap).
10904
10905 if Nkind (Parent (Clist)) = N_Variant then
10906 Clist := Parent (Parent (Parent (Clist)));
10907
10908 -- Check for possible discriminant part in record, this
10909 -- is treated essentially as another level in the
10910 -- recursion. For this case the parent of the component
10911 -- list is the record definition, and its parent is the
10912 -- full type declaration containing the discriminant
10913 -- specifications.
10914
10915 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10916 Clist := Parent (Parent ((Clist)));
10917
10918 -- If neither of these two cases, we are at the top of
10919 -- the tree.
10920
10921 else
10922 exit Component_List_Loop;
10923 end if;
10924 end loop Component_List_Loop;
10925
10926 <<Continue_Main_Component_Loop>>
10927 Next_Entity (C1_Ent);
10928
10929 end loop Main_Component_Loop;
10930 end Overlap_Check2;
10931 end if;
10932
10933 -- Skip the following warnings if overlap was detected; programmer
10934 -- should fix the errors first.
10935
10936 if not Overlap_Detected then
10937 -- Check for record holes (gaps)
10938
10939 if Warn_On_Record_Holes then
10940 declare
10941 Ignore : Uint;
10942 begin
10943 Record_Hole_Check (Rectype, After_Last => Ignore, Warn => True);
10944 end;
10945 end if;
10946
10947 -- Check for out-of-order component clauses
10948
10949 if Warn_On_Component_Order then
10950 Component_Order_Check (Rectype);
10951 end if;
10952 end if;
10953
10954 -- For records that have component clauses for all components, and whose
10955 -- size is less than or equal to 32, and which can be fully packed, we
10956 -- need to know the size in the front end to activate possible packed
10957 -- array processing where the component type is a record.
10958
10959 -- At this stage Hbit + 1 represents the first unused bit from all the
10960 -- component clauses processed, so if the component clauses are
10961 -- complete, then this is the length of the record.
10962
10963 -- For records longer than System.Storage_Unit, and for those where not
10964 -- all components have component clauses, the back end determines the
10965 -- length (it may for example be appropriate to round up the size
10966 -- to some convenient boundary, based on alignment considerations, etc).
10967
10968 if Unknown_RM_Size (Rectype)
10969 and then Hbit + 1 <= 32
10970 and then not Strict_Alignment (Rectype)
10971 then
10972
10973 -- Nothing to do if at least one component has no component clause
10974
10975 Comp := First_Component_Or_Discriminant (Rectype);
10976 while Present (Comp) loop
10977 exit when No (Component_Clause (Comp));
10978 Next_Component_Or_Discriminant (Comp);
10979 end loop;
10980
10981 -- If we fall out of loop, all components have component clauses
10982 -- and so we can set the size to the maximum value.
10983
10984 if No (Comp) then
10985 Set_RM_Size (Rectype, Hbit + 1);
10986 end if;
10987 end if;
10988 end Check_Record_Representation_Clause;
10989
10990 ----------------
10991 -- Check_Size --
10992 ----------------
10993
10994 procedure Check_Size
10995 (N : Node_Id;
10996 T : Entity_Id;
10997 Siz : Uint;
10998 Biased : out Boolean)
10999 is
11000 procedure Size_Too_Small_Error (Min_Siz : Uint);
11001 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
11002 -- minimum size.
11003
11004 --------------------------
11005 -- Size_Too_Small_Error --
11006 --------------------------
11007
11008 procedure Size_Too_Small_Error (Min_Siz : Uint) is
11009 begin
11010 -- This error is suppressed in ASIS mode to allow for different ASIS
11011 -- back ends or ASIS-based tools to query the illegal clause.
11012
11013 if not ASIS_Mode then
11014 Error_Msg_Uint_1 := Min_Siz;
11015 Error_Msg_NE (Size_Too_Small_Message, N, T);
11016 end if;
11017 end Size_Too_Small_Error;
11018
11019 -- Local variables
11020
11021 UT : constant Entity_Id := Underlying_Type (T);
11022 M : Uint;
11023
11024 -- Start of processing for Check_Size
11025
11026 begin
11027 Biased := False;
11028
11029 -- Reject patently improper size values
11030
11031 if Is_Elementary_Type (T)
11032 and then Siz > UI_From_Int (Int'Last)
11033 then
11034 Error_Msg_N ("Size value too large for elementary type", N);
11035
11036 if Nkind (Original_Node (N)) = N_Op_Expon then
11037 Error_Msg_N
11038 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
11039 end if;
11040 end if;
11041
11042 -- Dismiss generic types
11043
11044 if Is_Generic_Type (T)
11045 or else
11046 Is_Generic_Type (UT)
11047 or else
11048 Is_Generic_Type (Root_Type (UT))
11049 then
11050 return;
11051
11052 -- Guard against previous errors
11053
11054 elsif No (UT) or else UT = Any_Type then
11055 Check_Error_Detected;
11056 return;
11057
11058 -- Check case of bit packed array
11059
11060 elsif Is_Array_Type (UT)
11061 and then Known_Static_Component_Size (UT)
11062 and then Is_Bit_Packed_Array (UT)
11063 then
11064 declare
11065 Asiz : Uint;
11066 Indx : Node_Id;
11067 Ityp : Entity_Id;
11068
11069 begin
11070 Asiz := Component_Size (UT);
11071 Indx := First_Index (UT);
11072 loop
11073 Ityp := Etype (Indx);
11074
11075 -- If non-static bound, then we are not in the business of
11076 -- trying to check the length, and indeed an error will be
11077 -- issued elsewhere, since sizes of non-static array types
11078 -- cannot be set implicitly or explicitly.
11079
11080 if not Is_OK_Static_Subtype (Ityp) then
11081 return;
11082 end if;
11083
11084 -- Otherwise accumulate next dimension
11085
11086 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
11087 Expr_Value (Type_Low_Bound (Ityp)) +
11088 Uint_1);
11089
11090 Next_Index (Indx);
11091 exit when No (Indx);
11092 end loop;
11093
11094 if Asiz <= Siz then
11095 return;
11096
11097 else
11098 Size_Too_Small_Error (Asiz);
11099 Set_Esize (T, Asiz);
11100 Set_RM_Size (T, Asiz);
11101 end if;
11102 end;
11103
11104 -- All other composite types are ignored
11105
11106 elsif Is_Composite_Type (UT) then
11107 return;
11108
11109 -- For fixed-point types, don't check minimum if type is not frozen,
11110 -- since we don't know all the characteristics of the type that can
11111 -- affect the size (e.g. a specified small) till freeze time.
11112
11113 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
11114 null;
11115
11116 -- Cases for which a minimum check is required
11117
11118 else
11119 -- Ignore if specified size is correct for the type
11120
11121 if Known_Esize (UT) and then Siz = Esize (UT) then
11122 return;
11123 end if;
11124
11125 -- Otherwise get minimum size
11126
11127 M := UI_From_Int (Minimum_Size (UT));
11128
11129 if Siz < M then
11130
11131 -- Size is less than minimum size, but one possibility remains
11132 -- that we can manage with the new size if we bias the type.
11133
11134 M := UI_From_Int (Minimum_Size (UT, Biased => True));
11135
11136 if Siz < M then
11137 Size_Too_Small_Error (M);
11138 Set_Esize (T, M);
11139 Set_RM_Size (T, M);
11140 else
11141 Biased := True;
11142 end if;
11143 end if;
11144 end if;
11145 end Check_Size;
11146
11147 --------------------------
11148 -- Freeze_Entity_Checks --
11149 --------------------------
11150
11151 procedure Freeze_Entity_Checks (N : Node_Id) is
11152 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
11153 -- Inspect the primitive operations of type Typ and hide all pairs of
11154 -- implicitly declared non-overridden non-fully conformant homographs
11155 -- (Ada RM 8.3 12.3/2).
11156
11157 -------------------------------------
11158 -- Hide_Non_Overridden_Subprograms --
11159 -------------------------------------
11160
11161 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
11162 procedure Hide_Matching_Homographs
11163 (Subp_Id : Entity_Id;
11164 Start_Elmt : Elmt_Id);
11165 -- Inspect a list of primitive operations starting with Start_Elmt
11166 -- and find matching implicitly declared non-overridden non-fully
11167 -- conformant homographs of Subp_Id. If found, all matches along
11168 -- with Subp_Id are hidden from all visibility.
11169
11170 function Is_Non_Overridden_Or_Null_Procedure
11171 (Subp_Id : Entity_Id) return Boolean;
11172 -- Determine whether subprogram Subp_Id is implicitly declared non-
11173 -- overridden subprogram or an implicitly declared null procedure.
11174
11175 ------------------------------
11176 -- Hide_Matching_Homographs --
11177 ------------------------------
11178
11179 procedure Hide_Matching_Homographs
11180 (Subp_Id : Entity_Id;
11181 Start_Elmt : Elmt_Id)
11182 is
11183 Prim : Entity_Id;
11184 Prim_Elmt : Elmt_Id;
11185
11186 begin
11187 Prim_Elmt := Start_Elmt;
11188 while Present (Prim_Elmt) loop
11189 Prim := Node (Prim_Elmt);
11190
11191 -- The current primitive is implicitly declared non-overridden
11192 -- non-fully conformant homograph of Subp_Id. Both subprograms
11193 -- must be hidden from visibility.
11194
11195 if Chars (Prim) = Chars (Subp_Id)
11196 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
11197 and then not Fully_Conformant (Prim, Subp_Id)
11198 then
11199 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
11200 Set_Is_Immediately_Visible (Prim, False);
11201 Set_Is_Potentially_Use_Visible (Prim, False);
11202
11203 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
11204 Set_Is_Immediately_Visible (Subp_Id, False);
11205 Set_Is_Potentially_Use_Visible (Subp_Id, False);
11206 end if;
11207
11208 Next_Elmt (Prim_Elmt);
11209 end loop;
11210 end Hide_Matching_Homographs;
11211
11212 -----------------------------------------
11213 -- Is_Non_Overridden_Or_Null_Procedure --
11214 -----------------------------------------
11215
11216 function Is_Non_Overridden_Or_Null_Procedure
11217 (Subp_Id : Entity_Id) return Boolean
11218 is
11219 Alias_Id : Entity_Id;
11220
11221 begin
11222 -- The subprogram is inherited (implicitly declared), it does not
11223 -- override and does not cover a primitive of an interface.
11224
11225 if Ekind_In (Subp_Id, E_Function, E_Procedure)
11226 and then Present (Alias (Subp_Id))
11227 and then No (Interface_Alias (Subp_Id))
11228 and then No (Overridden_Operation (Subp_Id))
11229 then
11230 Alias_Id := Alias (Subp_Id);
11231
11232 if Requires_Overriding (Alias_Id) then
11233 return True;
11234
11235 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
11236 and then Null_Present (Parent (Alias_Id))
11237 then
11238 return True;
11239 end if;
11240 end if;
11241
11242 return False;
11243 end Is_Non_Overridden_Or_Null_Procedure;
11244
11245 -- Local variables
11246
11247 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
11248 Prim : Entity_Id;
11249 Prim_Elmt : Elmt_Id;
11250
11251 -- Start of processing for Hide_Non_Overridden_Subprograms
11252
11253 begin
11254 -- Inspect the list of primitives looking for non-overridden
11255 -- subprograms.
11256
11257 if Present (Prim_Ops) then
11258 Prim_Elmt := First_Elmt (Prim_Ops);
11259 while Present (Prim_Elmt) loop
11260 Prim := Node (Prim_Elmt);
11261 Next_Elmt (Prim_Elmt);
11262
11263 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
11264 Hide_Matching_Homographs
11265 (Subp_Id => Prim,
11266 Start_Elmt => Prim_Elmt);
11267 end if;
11268 end loop;
11269 end if;
11270 end Hide_Non_Overridden_Subprograms;
11271
11272 -- Local variables
11273
11274 E : constant Entity_Id := Entity (N);
11275
11276 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
11277 -- True in nongeneric case. Some of the processing here is skipped
11278 -- for the generic case since it is not needed. Basically in the
11279 -- generic case, we only need to do stuff that might generate error
11280 -- messages or warnings.
11281
11282 -- Start of processing for Freeze_Entity_Checks
11283
11284 begin
11285 -- Remember that we are processing a freezing entity. Required to
11286 -- ensure correct decoration of internal entities associated with
11287 -- interfaces (see New_Overloaded_Entity).
11288
11289 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
11290
11291 -- For tagged types covering interfaces add internal entities that link
11292 -- the primitives of the interfaces with the primitives that cover them.
11293 -- Note: These entities were originally generated only when generating
11294 -- code because their main purpose was to provide support to initialize
11295 -- the secondary dispatch tables. They are now generated also when
11296 -- compiling with no code generation to provide ASIS the relationship
11297 -- between interface primitives and tagged type primitives. They are
11298 -- also used to locate primitives covering interfaces when processing
11299 -- generics (see Derive_Subprograms).
11300
11301 -- This is not needed in the generic case
11302
11303 if Ada_Version >= Ada_2005
11304 and then Nongeneric_Case
11305 and then Ekind (E) = E_Record_Type
11306 and then Is_Tagged_Type (E)
11307 and then not Is_Interface (E)
11308 and then Has_Interfaces (E)
11309 then
11310 -- This would be a good common place to call the routine that checks
11311 -- overriding of interface primitives (and thus factorize calls to
11312 -- Check_Abstract_Overriding located at different contexts in the
11313 -- compiler). However, this is not possible because it causes
11314 -- spurious errors in case of late overriding.
11315
11316 Add_Internal_Interface_Entities (E);
11317 end if;
11318
11319 -- After all forms of overriding have been resolved, a tagged type may
11320 -- be left with a set of implicitly declared and possibly erroneous
11321 -- abstract subprograms, null procedures and subprograms that require
11322 -- overriding. If this set contains fully conformant homographs, then
11323 -- one is chosen arbitrarily (already done during resolution), otherwise
11324 -- all remaining non-fully conformant homographs are hidden from
11325 -- visibility (Ada RM 8.3 12.3/2).
11326
11327 if Is_Tagged_Type (E) then
11328 Hide_Non_Overridden_Subprograms (E);
11329 end if;
11330
11331 -- Check CPP types
11332
11333 if Ekind (E) = E_Record_Type
11334 and then Is_CPP_Class (E)
11335 and then Is_Tagged_Type (E)
11336 and then Tagged_Type_Expansion
11337 then
11338 if CPP_Num_Prims (E) = 0 then
11339
11340 -- If the CPP type has user defined components then it must import
11341 -- primitives from C++. This is required because if the C++ class
11342 -- has no primitives then the C++ compiler does not added the _tag
11343 -- component to the type.
11344
11345 if First_Entity (E) /= Last_Entity (E) then
11346 Error_Msg_N
11347 ("'C'P'P type must import at least one primitive from C++??",
11348 E);
11349 end if;
11350 end if;
11351
11352 -- Check that all its primitives are abstract or imported from C++.
11353 -- Check also availability of the C++ constructor.
11354
11355 declare
11356 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
11357 Elmt : Elmt_Id;
11358 Error_Reported : Boolean := False;
11359 Prim : Node_Id;
11360
11361 begin
11362 Elmt := First_Elmt (Primitive_Operations (E));
11363 while Present (Elmt) loop
11364 Prim := Node (Elmt);
11365
11366 if Comes_From_Source (Prim) then
11367 if Is_Abstract_Subprogram (Prim) then
11368 null;
11369
11370 elsif not Is_Imported (Prim)
11371 or else Convention (Prim) /= Convention_CPP
11372 then
11373 Error_Msg_N
11374 ("primitives of 'C'P'P types must be imported from C++ "
11375 & "or abstract??", Prim);
11376
11377 elsif not Has_Constructors
11378 and then not Error_Reported
11379 then
11380 Error_Msg_Name_1 := Chars (E);
11381 Error_Msg_N
11382 ("??'C'P'P constructor required for type %", Prim);
11383 Error_Reported := True;
11384 end if;
11385 end if;
11386
11387 Next_Elmt (Elmt);
11388 end loop;
11389 end;
11390 end if;
11391
11392 -- Check Ada derivation of CPP type
11393
11394 if Expander_Active -- why? losing errors in -gnatc mode???
11395 and then Present (Etype (E)) -- defend against errors
11396 and then Tagged_Type_Expansion
11397 and then Ekind (E) = E_Record_Type
11398 and then Etype (E) /= E
11399 and then Is_CPP_Class (Etype (E))
11400 and then CPP_Num_Prims (Etype (E)) > 0
11401 and then not Is_CPP_Class (E)
11402 and then not Has_CPP_Constructors (Etype (E))
11403 then
11404 -- If the parent has C++ primitives but it has no constructor then
11405 -- check that all the primitives are overridden in this derivation;
11406 -- otherwise the constructor of the parent is needed to build the
11407 -- dispatch table.
11408
11409 declare
11410 Elmt : Elmt_Id;
11411 Prim : Node_Id;
11412
11413 begin
11414 Elmt := First_Elmt (Primitive_Operations (E));
11415 while Present (Elmt) loop
11416 Prim := Node (Elmt);
11417
11418 if not Is_Abstract_Subprogram (Prim)
11419 and then No (Interface_Alias (Prim))
11420 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
11421 then
11422 Error_Msg_Name_1 := Chars (Etype (E));
11423 Error_Msg_N
11424 ("'C'P'P constructor required for parent type %", E);
11425 exit;
11426 end if;
11427
11428 Next_Elmt (Elmt);
11429 end loop;
11430 end;
11431 end if;
11432
11433 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
11434
11435 -- If we have a type with predicates, build predicate function. This is
11436 -- not needed in the generic case, nor within TSS subprograms and other
11437 -- predefined primitives. For a derived type, ensure that the parent
11438 -- type is already frozen so that its predicate function has been
11439 -- constructed already. This is necessary if the parent is declared
11440 -- in a nested package and its own freeze point has not been reached.
11441
11442 if Is_Type (E)
11443 and then Nongeneric_Case
11444 and then not Within_Internal_Subprogram
11445 and then Has_Predicates (E)
11446 then
11447 declare
11448 Atyp : constant Entity_Id := Nearest_Ancestor (E);
11449 begin
11450 if Present (Atyp)
11451 and then Has_Predicates (Atyp)
11452 and then not Is_Frozen (Atyp)
11453 then
11454 Freeze_Before (N, Atyp);
11455 end if;
11456 end;
11457
11458 Build_Predicate_Functions (E, N);
11459 end if;
11460
11461 -- If type has delayed aspects, this is where we do the preanalysis at
11462 -- the freeze point, as part of the consistent visibility check. Note
11463 -- that this must be done after calling Build_Predicate_Functions or
11464 -- Build_Invariant_Procedure since these subprograms fix occurrences of
11465 -- the subtype name in the saved expression so that they will not cause
11466 -- trouble in the preanalysis.
11467
11468 -- This is also not needed in the generic case
11469
11470 if Nongeneric_Case
11471 and then Has_Delayed_Aspects (E)
11472 and then Scope (E) = Current_Scope
11473 then
11474 declare
11475 A_Id : Aspect_Id;
11476 Ritem : Node_Id;
11477
11478 begin
11479 -- Look for aspect specification entries for this entity
11480
11481 Ritem := First_Rep_Item (E);
11482 while Present (Ritem) loop
11483 if Nkind (Ritem) = N_Aspect_Specification
11484 and then Entity (Ritem) = E
11485 and then Is_Delayed_Aspect (Ritem)
11486 then
11487 A_Id := Get_Aspect_Id (Ritem);
11488
11489 if A_Id = Aspect_Dynamic_Predicate
11490 or else A_Id = Aspect_Predicate
11491 or else A_Id = Aspect_Priority
11492 or else A_Id = Aspect_CPU
11493 then
11494 -- Retrieve the visibility to components and discriminants
11495 -- in order to properly analyze the aspects.
11496
11497 Push_Type (E);
11498 Check_Aspect_At_Freeze_Point (Ritem);
11499 Pop_Type (E);
11500
11501 else
11502 Check_Aspect_At_Freeze_Point (Ritem);
11503 end if;
11504 end if;
11505
11506 Next_Rep_Item (Ritem);
11507 end loop;
11508 end;
11509
11510 end if;
11511
11512 -- For a record type, deal with variant parts. This has to be delayed to
11513 -- this point, because of the issue of statically predicated subtypes,
11514 -- which we have to ensure are frozen before checking choices, since we
11515 -- need to have the static choice list set.
11516
11517 if Is_Record_Type (E) then
11518 Check_Variant_Part : declare
11519 D : constant Node_Id := Declaration_Node (E);
11520 T : Node_Id;
11521 C : Node_Id;
11522 VP : Node_Id;
11523
11524 Others_Present : Boolean;
11525 pragma Warnings (Off, Others_Present);
11526 -- Indicates others present, not used in this case
11527
11528 procedure Non_Static_Choice_Error (Choice : Node_Id);
11529 -- Error routine invoked by the generic instantiation below when
11530 -- the variant part has a non static choice.
11531
11532 procedure Process_Declarations (Variant : Node_Id);
11533 -- Processes declarations associated with a variant. We analyzed
11534 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11535 -- but we still need the recursive call to Check_Choices for any
11536 -- nested variant to get its choices properly processed. This is
11537 -- also where we expand out the choices if expansion is active.
11538
11539 package Variant_Choices_Processing is new
11540 Generic_Check_Choices
11541 (Process_Empty_Choice => No_OP,
11542 Process_Non_Static_Choice => Non_Static_Choice_Error,
11543 Process_Associated_Node => Process_Declarations);
11544 use Variant_Choices_Processing;
11545
11546 -----------------------------
11547 -- Non_Static_Choice_Error --
11548 -----------------------------
11549
11550 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11551 begin
11552 Flag_Non_Static_Expr
11553 ("choice given in variant part is not static!", Choice);
11554 end Non_Static_Choice_Error;
11555
11556 --------------------------
11557 -- Process_Declarations --
11558 --------------------------
11559
11560 procedure Process_Declarations (Variant : Node_Id) is
11561 CL : constant Node_Id := Component_List (Variant);
11562 VP : Node_Id;
11563
11564 begin
11565 -- Check for static predicate present in this variant
11566
11567 if Has_SP_Choice (Variant) then
11568
11569 -- Here we expand. You might expect to find this call in
11570 -- Expand_N_Variant_Part, but that is called when we first
11571 -- see the variant part, and we cannot do this expansion
11572 -- earlier than the freeze point, since for statically
11573 -- predicated subtypes, the predicate is not known till
11574 -- the freeze point.
11575
11576 -- Furthermore, we do this expansion even if the expander
11577 -- is not active, because other semantic processing, e.g.
11578 -- for aggregates, requires the expanded list of choices.
11579
11580 -- If the expander is not active, then we can't just clobber
11581 -- the list since it would invalidate the ASIS -gnatct tree.
11582 -- So we have to rewrite the variant part with a Rewrite
11583 -- call that replaces it with a copy and clobber the copy.
11584
11585 if not Expander_Active then
11586 declare
11587 NewV : constant Node_Id := New_Copy (Variant);
11588 begin
11589 Set_Discrete_Choices
11590 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11591 Rewrite (Variant, NewV);
11592 end;
11593 end if;
11594
11595 Expand_Static_Predicates_In_Choices (Variant);
11596 end if;
11597
11598 -- We don't need to worry about the declarations in the variant
11599 -- (since they were analyzed by Analyze_Choices when we first
11600 -- encountered the variant), but we do need to take care of
11601 -- expansion of any nested variants.
11602
11603 if not Null_Present (CL) then
11604 VP := Variant_Part (CL);
11605
11606 if Present (VP) then
11607 Check_Choices
11608 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11609 end if;
11610 end if;
11611 end Process_Declarations;
11612
11613 -- Start of processing for Check_Variant_Part
11614
11615 begin
11616 -- Find component list
11617
11618 C := Empty;
11619
11620 if Nkind (D) = N_Full_Type_Declaration then
11621 T := Type_Definition (D);
11622
11623 if Nkind (T) = N_Record_Definition then
11624 C := Component_List (T);
11625
11626 elsif Nkind (T) = N_Derived_Type_Definition
11627 and then Present (Record_Extension_Part (T))
11628 then
11629 C := Component_List (Record_Extension_Part (T));
11630 end if;
11631 end if;
11632
11633 -- Case of variant part present
11634
11635 if Present (C) and then Present (Variant_Part (C)) then
11636 VP := Variant_Part (C);
11637
11638 -- Check choices
11639
11640 Check_Choices
11641 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11642
11643 -- If the last variant does not contain the Others choice,
11644 -- replace it with an N_Others_Choice node since Gigi always
11645 -- wants an Others. Note that we do not bother to call Analyze
11646 -- on the modified variant part, since its only effect would be
11647 -- to compute the Others_Discrete_Choices node laboriously, and
11648 -- of course we already know the list of choices corresponding
11649 -- to the others choice (it's the list we're replacing).
11650
11651 -- We only want to do this if the expander is active, since
11652 -- we do not want to clobber the ASIS tree.
11653
11654 if Expander_Active then
11655 declare
11656 Last_Var : constant Node_Id :=
11657 Last_Non_Pragma (Variants (VP));
11658
11659 Others_Node : Node_Id;
11660
11661 begin
11662 if Nkind (First (Discrete_Choices (Last_Var))) /=
11663 N_Others_Choice
11664 then
11665 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11666 Set_Others_Discrete_Choices
11667 (Others_Node, Discrete_Choices (Last_Var));
11668 Set_Discrete_Choices
11669 (Last_Var, New_List (Others_Node));
11670 end if;
11671 end;
11672 end if;
11673 end if;
11674 end Check_Variant_Part;
11675 end if;
11676 end Freeze_Entity_Checks;
11677
11678 -------------------------
11679 -- Get_Alignment_Value --
11680 -------------------------
11681
11682 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11683 Align : constant Uint := Static_Integer (Expr);
11684
11685 begin
11686 if Align = No_Uint then
11687 return No_Uint;
11688
11689 elsif Align < 0 then
11690
11691 -- This error is suppressed in ASIS mode to allow for different ASIS
11692 -- back ends or ASIS-based tools to query the illegal clause.
11693
11694 if not ASIS_Mode then
11695 Error_Msg_N ("alignment value must be positive", Expr);
11696 end if;
11697
11698 return No_Uint;
11699
11700 -- If Alignment is specified to be 0, we treat it the same as 1
11701
11702 elsif Align = 0 then
11703 return Uint_1;
11704
11705 else
11706 for J in Int range 0 .. 64 loop
11707 declare
11708 M : constant Uint := Uint_2 ** J;
11709
11710 begin
11711 exit when M = Align;
11712
11713 if M > Align then
11714
11715 -- This error is suppressed in ASIS mode to allow for
11716 -- different ASIS back ends or ASIS-based tools to query the
11717 -- illegal clause.
11718
11719 if not ASIS_Mode then
11720 Error_Msg_N ("alignment value must be power of 2", Expr);
11721 end if;
11722
11723 return No_Uint;
11724 end if;
11725 end;
11726 end loop;
11727
11728 return Align;
11729 end if;
11730 end Get_Alignment_Value;
11731
11732 -------------------------------------
11733 -- Inherit_Aspects_At_Freeze_Point --
11734 -------------------------------------
11735
11736 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11737 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11738 (Rep_Item : Node_Id) return Boolean;
11739 -- This routine checks if Rep_Item is either a pragma or an aspect
11740 -- specification node whose correponding pragma (if any) is present in
11741 -- the Rep Item chain of the entity it has been specified to.
11742
11743 function Rep_Item_Entity (Rep_Item : Node_Id) return Entity_Id;
11744 -- Return the entity for which Rep_Item is specified
11745
11746 --------------------------------------------------
11747 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11748 --------------------------------------------------
11749
11750 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11751 (Rep_Item : Node_Id) return Boolean
11752 is
11753 begin
11754 return
11755 Nkind (Rep_Item) = N_Pragma
11756 or else Present_In_Rep_Item
11757 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11758 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11759
11760 ---------------------
11761 -- Rep_Item_Entity --
11762 ---------------------
11763
11764 function Rep_Item_Entity (Rep_Item : Node_Id) return Entity_Id is
11765 begin
11766 if Nkind (Rep_Item) = N_Aspect_Specification then
11767 return Entity (Rep_Item);
11768
11769 else
11770 pragma Assert (Nkind_In (Rep_Item,
11771 N_Attribute_Definition_Clause,
11772 N_Pragma));
11773 return Entity (Name (Rep_Item));
11774 end if;
11775 end Rep_Item_Entity;
11776
11777 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11778
11779 begin
11780 -- A representation item is either subtype-specific (Size and Alignment
11781 -- clauses) or type-related (all others). Subtype-specific aspects may
11782 -- differ for different subtypes of the same type (RM 13.1.8).
11783
11784 -- A derived type inherits each type-related representation aspect of
11785 -- its parent type that was directly specified before the declaration of
11786 -- the derived type (RM 13.1.15).
11787
11788 -- A derived subtype inherits each subtype-specific representation
11789 -- aspect of its parent subtype that was directly specified before the
11790 -- declaration of the derived type (RM 13.1.15).
11791
11792 -- The general processing involves inheriting a representation aspect
11793 -- from a parent type whenever the first rep item (aspect specification,
11794 -- attribute definition clause, pragma) corresponding to the given
11795 -- representation aspect in the rep item chain of Typ, if any, isn't
11796 -- directly specified to Typ but to one of its parents.
11797
11798 -- ??? Note that, for now, just a limited number of representation
11799 -- aspects have been inherited here so far. Many of them are
11800 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11801 -- a non- exhaustive list of aspects that likely also need to
11802 -- be moved to this routine: Alignment, Component_Alignment,
11803 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11804 -- Preelaborable_Initialization, RM_Size and Small.
11805
11806 -- In addition, Convention must be propagated from base type to subtype,
11807 -- because the subtype may have been declared on an incomplete view.
11808
11809 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11810 return;
11811 end if;
11812
11813 -- Ada_05/Ada_2005
11814
11815 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11816 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11817 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11818 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11819 then
11820 Set_Is_Ada_2005_Only (Typ);
11821 end if;
11822
11823 -- Ada_12/Ada_2012
11824
11825 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11826 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11827 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11828 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11829 then
11830 Set_Is_Ada_2012_Only (Typ);
11831 end if;
11832
11833 -- Atomic/Shared
11834
11835 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11836 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11837 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11838 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11839 then
11840 Set_Is_Atomic (Typ);
11841 Set_Is_Volatile (Typ);
11842 Set_Treat_As_Volatile (Typ);
11843 end if;
11844
11845 -- Convention
11846
11847 if Is_Record_Type (Typ)
11848 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11849 then
11850 Set_Convention (Typ, Convention (Base_Type (Typ)));
11851 end if;
11852
11853 -- Default_Component_Value
11854
11855 -- Verify that there is no rep_item declared for the type, and there
11856 -- is one coming from an ancestor.
11857
11858 if Is_Array_Type (Typ)
11859 and then Is_Base_Type (Typ)
11860 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11861 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11862 then
11863 Set_Default_Aspect_Component_Value (Typ,
11864 Default_Aspect_Component_Value
11865 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11866 end if;
11867
11868 -- Default_Value
11869
11870 if Is_Scalar_Type (Typ)
11871 and then Is_Base_Type (Typ)
11872 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11873 and then Has_Rep_Item (Typ, Name_Default_Value)
11874 then
11875 Set_Has_Default_Aspect (Typ);
11876 Set_Default_Aspect_Value (Typ,
11877 Default_Aspect_Value
11878 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11879 end if;
11880
11881 -- Discard_Names
11882
11883 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11884 and then Has_Rep_Item (Typ, Name_Discard_Names)
11885 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11886 (Get_Rep_Item (Typ, Name_Discard_Names))
11887 then
11888 Set_Discard_Names (Typ);
11889 end if;
11890
11891 -- Volatile
11892
11893 if not Has_Rep_Item (Typ, Name_Volatile, False)
11894 and then Has_Rep_Item (Typ, Name_Volatile)
11895 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11896 (Get_Rep_Item (Typ, Name_Volatile))
11897 then
11898 Set_Is_Volatile (Typ);
11899 Set_Treat_As_Volatile (Typ);
11900 end if;
11901
11902 -- Volatile_Full_Access
11903
11904 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11905 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11906 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11907 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11908 then
11909 Set_Is_Volatile_Full_Access (Typ);
11910 Set_Is_Volatile (Typ);
11911 Set_Treat_As_Volatile (Typ);
11912 end if;
11913
11914 -- Inheritance for derived types only
11915
11916 if Is_Derived_Type (Typ) then
11917 declare
11918 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11919 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11920
11921 begin
11922 -- Atomic_Components
11923
11924 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11925 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11926 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11927 (Get_Rep_Item (Typ, Name_Atomic_Components))
11928 then
11929 Set_Has_Atomic_Components (Imp_Bas_Typ);
11930 end if;
11931
11932 -- Volatile_Components
11933
11934 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11935 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11936 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11937 (Get_Rep_Item (Typ, Name_Volatile_Components))
11938 then
11939 Set_Has_Volatile_Components (Imp_Bas_Typ);
11940 end if;
11941
11942 -- Finalize_Storage_Only
11943
11944 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11945 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11946 then
11947 Set_Finalize_Storage_Only (Bas_Typ);
11948 end if;
11949
11950 -- Universal_Aliasing
11951
11952 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11953 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11954 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11955 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11956 then
11957 Set_Universal_Aliasing (Imp_Bas_Typ);
11958 end if;
11959
11960 -- Bit_Order
11961
11962 if Is_Record_Type (Typ) then
11963 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11964 and then Has_Rep_Item (Typ, Name_Bit_Order)
11965 then
11966 Set_Reverse_Bit_Order (Bas_Typ,
11967 Reverse_Bit_Order (Rep_Item_Entity
11968 (Get_Rep_Item (Typ, Name_Bit_Order))));
11969 end if;
11970 end if;
11971
11972 -- Scalar_Storage_Order
11973
11974 -- Note: the aspect is specified on a first subtype, but recorded
11975 -- in a flag of the base type!
11976
11977 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11978 and then Typ = Bas_Typ
11979 then
11980 -- For a type extension, always inherit from parent; otherwise
11981 -- inherit if no default applies. Note: we do not check for
11982 -- an explicit rep item on the parent type when inheriting,
11983 -- because the parent SSO may itself have been set by default.
11984
11985 if not Has_Rep_Item (First_Subtype (Typ),
11986 Name_Scalar_Storage_Order, False)
11987 and then (Is_Tagged_Type (Bas_Typ)
11988 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11989 or else
11990 SSO_Set_High_By_Default (Bas_Typ)))
11991 then
11992 Set_Reverse_Storage_Order (Bas_Typ,
11993 Reverse_Storage_Order
11994 (Implementation_Base_Type (Etype (Bas_Typ))));
11995
11996 -- Clear default SSO indications, since the inherited aspect
11997 -- which was set explicitly overrides the default.
11998
11999 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
12000 Set_SSO_Set_High_By_Default (Bas_Typ, False);
12001 end if;
12002 end if;
12003 end;
12004 end if;
12005 end Inherit_Aspects_At_Freeze_Point;
12006
12007 ----------------
12008 -- Initialize --
12009 ----------------
12010
12011 procedure Initialize is
12012 begin
12013 Address_Clause_Checks.Init;
12014 Unchecked_Conversions.Init;
12015
12016 -- ??? Might be needed in the future for some non GCC back-ends
12017 -- if AAMP_On_Target then
12018 -- Independence_Checks.Init;
12019 -- end if;
12020 end Initialize;
12021
12022 ---------------------------
12023 -- Install_Discriminants --
12024 ---------------------------
12025
12026 procedure Install_Discriminants (E : Entity_Id) is
12027 Disc : Entity_Id;
12028 Prev : Entity_Id;
12029 begin
12030 Disc := First_Discriminant (E);
12031 while Present (Disc) loop
12032 Prev := Current_Entity (Disc);
12033 Set_Current_Entity (Disc);
12034 Set_Is_Immediately_Visible (Disc);
12035 Set_Homonym (Disc, Prev);
12036 Next_Discriminant (Disc);
12037 end loop;
12038 end Install_Discriminants;
12039
12040 -------------------------
12041 -- Is_Operational_Item --
12042 -------------------------
12043
12044 function Is_Operational_Item (N : Node_Id) return Boolean is
12045 begin
12046 if Nkind (N) /= N_Attribute_Definition_Clause then
12047 return False;
12048
12049 else
12050 declare
12051 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
12052 begin
12053
12054 -- List of operational items is given in AARM 13.1(8.mm/1).
12055 -- It is clearly incomplete, as it does not include iterator
12056 -- aspects, among others.
12057
12058 return Id = Attribute_Constant_Indexing
12059 or else Id = Attribute_Default_Iterator
12060 or else Id = Attribute_Implicit_Dereference
12061 or else Id = Attribute_Input
12062 or else Id = Attribute_Iterator_Element
12063 or else Id = Attribute_Iterable
12064 or else Id = Attribute_Output
12065 or else Id = Attribute_Read
12066 or else Id = Attribute_Variable_Indexing
12067 or else Id = Attribute_Write
12068 or else Id = Attribute_External_Tag;
12069 end;
12070 end if;
12071 end Is_Operational_Item;
12072
12073 -------------------------
12074 -- Is_Predicate_Static --
12075 -------------------------
12076
12077 -- Note: the basic legality of the expression has already been checked, so
12078 -- we don't need to worry about cases or ranges on strings for example.
12079
12080 function Is_Predicate_Static
12081 (Expr : Node_Id;
12082 Nam : Name_Id) return Boolean
12083 is
12084 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
12085 -- Given a list of case expression alternatives, returns True if all
12086 -- the alternatives are static (have all static choices, and a static
12087 -- expression).
12088
12089 function All_Static_Choices (L : List_Id) return Boolean;
12090 -- Returns true if all elements of the list are OK static choices
12091 -- as defined below for Is_Static_Choice. Used for case expression
12092 -- alternatives and for the right operand of a membership test. An
12093 -- others_choice is static if the corresponding expression is static.
12094 -- The staticness of the bounds is checked separately.
12095
12096 function Is_Static_Choice (N : Node_Id) return Boolean;
12097 -- Returns True if N represents a static choice (static subtype, or
12098 -- static subtype indication, or static expression, or static range).
12099 --
12100 -- Note that this is a bit more inclusive than we actually need
12101 -- (in particular membership tests do not allow the use of subtype
12102 -- indications). But that doesn't matter, we have already checked
12103 -- that the construct is legal to get this far.
12104
12105 function Is_Type_Ref (N : Node_Id) return Boolean;
12106 pragma Inline (Is_Type_Ref);
12107 -- Returns True if N is a reference to the type for the predicate in the
12108 -- expression (i.e. if it is an identifier whose Chars field matches the
12109 -- Nam given in the call). N must not be parenthesized, if the type name
12110 -- appears in parens, this routine will return False.
12111 --
12112 -- The routine also returns True for function calls generated during the
12113 -- expansion of comparison operators on strings, which are intended to
12114 -- be legal in static predicates, and are converted into calls to array
12115 -- comparison routines in the body of the corresponding predicate
12116 -- function.
12117
12118 ----------------------------------
12119 -- All_Static_Case_Alternatives --
12120 ----------------------------------
12121
12122 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
12123 N : Node_Id;
12124
12125 begin
12126 N := First (L);
12127 while Present (N) loop
12128 if not (All_Static_Choices (Discrete_Choices (N))
12129 and then Is_OK_Static_Expression (Expression (N)))
12130 then
12131 return False;
12132 end if;
12133
12134 Next (N);
12135 end loop;
12136
12137 return True;
12138 end All_Static_Case_Alternatives;
12139
12140 ------------------------
12141 -- All_Static_Choices --
12142 ------------------------
12143
12144 function All_Static_Choices (L : List_Id) return Boolean is
12145 N : Node_Id;
12146
12147 begin
12148 N := First (L);
12149 while Present (N) loop
12150 if not Is_Static_Choice (N) then
12151 return False;
12152 end if;
12153
12154 Next (N);
12155 end loop;
12156
12157 return True;
12158 end All_Static_Choices;
12159
12160 ----------------------
12161 -- Is_Static_Choice --
12162 ----------------------
12163
12164 function Is_Static_Choice (N : Node_Id) return Boolean is
12165 begin
12166 return Nkind (N) = N_Others_Choice
12167 or else Is_OK_Static_Expression (N)
12168 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
12169 and then Is_OK_Static_Subtype (Entity (N)))
12170 or else (Nkind (N) = N_Subtype_Indication
12171 and then Is_OK_Static_Subtype (Entity (N)))
12172 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
12173 end Is_Static_Choice;
12174
12175 -----------------
12176 -- Is_Type_Ref --
12177 -----------------
12178
12179 function Is_Type_Ref (N : Node_Id) return Boolean is
12180 begin
12181 return (Nkind (N) = N_Identifier
12182 and then Chars (N) = Nam
12183 and then Paren_Count (N) = 0)
12184 or else Nkind (N) = N_Function_Call;
12185 end Is_Type_Ref;
12186
12187 -- Start of processing for Is_Predicate_Static
12188
12189 begin
12190 -- Predicate_Static means one of the following holds. Numbers are the
12191 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
12192
12193 -- 16: A static expression
12194
12195 if Is_OK_Static_Expression (Expr) then
12196 return True;
12197
12198 -- 17: A membership test whose simple_expression is the current
12199 -- instance, and whose membership_choice_list meets the requirements
12200 -- for a static membership test.
12201
12202 elsif Nkind (Expr) in N_Membership_Test
12203 and then ((Present (Right_Opnd (Expr))
12204 and then Is_Static_Choice (Right_Opnd (Expr)))
12205 or else
12206 (Present (Alternatives (Expr))
12207 and then All_Static_Choices (Alternatives (Expr))))
12208 then
12209 return True;
12210
12211 -- 18. A case_expression whose selecting_expression is the current
12212 -- instance, and whose dependent expressions are static expressions.
12213
12214 elsif Nkind (Expr) = N_Case_Expression
12215 and then Is_Type_Ref (Expression (Expr))
12216 and then All_Static_Case_Alternatives (Alternatives (Expr))
12217 then
12218 return True;
12219
12220 -- 19. A call to a predefined equality or ordering operator, where one
12221 -- operand is the current instance, and the other is a static
12222 -- expression.
12223
12224 -- Note: the RM is clearly wrong here in not excluding string types.
12225 -- Without this exclusion, we would allow expressions like X > "ABC"
12226 -- to be considered as predicate-static, which is clearly not intended,
12227 -- since the idea is for predicate-static to be a subset of normal
12228 -- static expressions (and "DEF" > "ABC" is not a static expression).
12229
12230 -- However, we do allow internally generated (not from source) equality
12231 -- and inequality operations to be valid on strings (this helps deal
12232 -- with cases where we transform A in "ABC" to A = "ABC).
12233
12234 -- In fact, it appears that the intent of the ARG is to extend static
12235 -- predicates to strings, and that the extension should probably apply
12236 -- to static expressions themselves. The code below accepts comparison
12237 -- operators that apply to static strings.
12238
12239 elsif Nkind (Expr) in N_Op_Compare
12240 and then ((Is_Type_Ref (Left_Opnd (Expr))
12241 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
12242 or else
12243 (Is_Type_Ref (Right_Opnd (Expr))
12244 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
12245 then
12246 return True;
12247
12248 -- 20. A call to a predefined boolean logical operator, where each
12249 -- operand is predicate-static.
12250
12251 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
12252 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
12253 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
12254 or else
12255 (Nkind (Expr) = N_Op_Not
12256 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
12257 then
12258 return True;
12259
12260 -- 21. A short-circuit control form where both operands are
12261 -- predicate-static.
12262
12263 elsif Nkind (Expr) in N_Short_Circuit
12264 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
12265 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
12266 then
12267 return True;
12268
12269 -- 22. A parenthesized predicate-static expression. This does not
12270 -- require any special test, since we just ignore paren levels in
12271 -- all the cases above.
12272
12273 -- One more test that is an implementation artifact caused by the fact
12274 -- that we are analyzing not the original expression, but the generated
12275 -- expression in the body of the predicate function. This can include
12276 -- references to inherited predicates, so that the expression we are
12277 -- processing looks like:
12278
12279 -- xxPredicate (typ (Inns)) and then expression
12280
12281 -- Where the call is to a Predicate function for an inherited predicate.
12282 -- We simply ignore such a call, which could be to either a dynamic or
12283 -- a static predicate. Note that if the parent predicate is dynamic then
12284 -- eventually this type will be marked as dynamic, but you are allowed
12285 -- to specify a static predicate for a subtype which is inheriting a
12286 -- dynamic predicate, so the static predicate validation here ignores
12287 -- the inherited predicate even if it is dynamic.
12288 -- In all cases, a static predicate can only apply to a scalar type.
12289
12290 elsif Nkind (Expr) = N_Function_Call
12291 and then Is_Predicate_Function (Entity (Name (Expr)))
12292 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
12293 then
12294 return True;
12295
12296 elsif Is_Entity_Name (Expr)
12297 and then Entity (Expr) = Standard_True
12298 then
12299 Error_Msg_N ("predicate is redundant (always True)?", Expr);
12300 return True;
12301
12302 -- That's an exhaustive list of tests, all other cases are not
12303 -- predicate-static, so we return False.
12304
12305 else
12306 return False;
12307 end if;
12308 end Is_Predicate_Static;
12309
12310 ------------------------------
12311 -- Is_Type_Related_Rep_Item --
12312 ------------------------------
12313
12314 function Is_Type_Related_Rep_Item (N : Node_Id) return Boolean is
12315 begin
12316 case Nkind (N) is
12317 when N_Attribute_Definition_Clause =>
12318 declare
12319 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
12320 -- See AARM 13.1(8.f-8.x) list items that end in "clause"
12321 -- ???: include any GNAT-defined attributes here?
12322 begin
12323 return Id = Attribute_Component_Size
12324 or else Id = Attribute_Bit_Order
12325 or else Id = Attribute_Storage_Pool
12326 or else Id = Attribute_Stream_Size
12327 or else Id = Attribute_Machine_Radix;
12328 end;
12329
12330 when N_Pragma =>
12331 case Get_Pragma_Id (N) is
12332 -- See AARM 13.1(8.f-8.x) list items that start with "pragma"
12333 -- ???: include any GNAT-defined pragmas here?
12334 when Pragma_Pack
12335 | Pragma_Import
12336 | Pragma_Export
12337 | Pragma_Convention
12338 | Pragma_Atomic
12339 | Pragma_Independent
12340 | Pragma_Volatile
12341 | Pragma_Atomic_Components
12342 | Pragma_Independent_Components
12343 | Pragma_Volatile_Components
12344 | Pragma_Discard_Names
12345 =>
12346 return True;
12347 when others =>
12348 null;
12349 end case;
12350
12351 when N_Enumeration_Representation_Clause
12352 | N_Record_Representation_Clause
12353 =>
12354 return True;
12355
12356 when others =>
12357 null;
12358 end case;
12359
12360 return False;
12361 end Is_Type_Related_Rep_Item;
12362
12363 ---------------------
12364 -- Kill_Rep_Clause --
12365 ---------------------
12366
12367 procedure Kill_Rep_Clause (N : Node_Id) is
12368 begin
12369 pragma Assert (Ignore_Rep_Clauses);
12370
12371 -- Note: we use Replace rather than Rewrite, because we don't want
12372 -- ASIS to be able to use Original_Node to dig out the (undecorated)
12373 -- rep clause that is being replaced.
12374
12375 Replace (N, Make_Null_Statement (Sloc (N)));
12376
12377 -- The null statement must be marked as not coming from source. This is
12378 -- so that ASIS ignores it, and also the back end does not expect bogus
12379 -- "from source" null statements in weird places (e.g. in declarative
12380 -- regions where such null statements are not allowed).
12381
12382 Set_Comes_From_Source (N, False);
12383 end Kill_Rep_Clause;
12384
12385 ------------------
12386 -- Minimum_Size --
12387 ------------------
12388
12389 function Minimum_Size
12390 (T : Entity_Id;
12391 Biased : Boolean := False) return Nat
12392 is
12393 Lo : Uint := No_Uint;
12394 Hi : Uint := No_Uint;
12395 LoR : Ureal := No_Ureal;
12396 HiR : Ureal := No_Ureal;
12397 LoSet : Boolean := False;
12398 HiSet : Boolean := False;
12399 B : Uint;
12400 S : Nat;
12401 Ancest : Entity_Id;
12402 R_Typ : constant Entity_Id := Root_Type (T);
12403
12404 begin
12405 -- If bad type, return 0
12406
12407 if T = Any_Type then
12408 return 0;
12409
12410 -- For generic types, just return zero. There cannot be any legitimate
12411 -- need to know such a size, but this routine may be called with a
12412 -- generic type as part of normal processing.
12413
12414 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
12415 return 0;
12416
12417 -- Access types (cannot have size smaller than System.Address)
12418
12419 elsif Is_Access_Type (T) then
12420 return System_Address_Size;
12421
12422 -- Floating-point types
12423
12424 elsif Is_Floating_Point_Type (T) then
12425 return UI_To_Int (Esize (R_Typ));
12426
12427 -- Discrete types
12428
12429 elsif Is_Discrete_Type (T) then
12430
12431 -- The following loop is looking for the nearest compile time known
12432 -- bounds following the ancestor subtype chain. The idea is to find
12433 -- the most restrictive known bounds information.
12434
12435 Ancest := T;
12436 loop
12437 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12438 return 0;
12439 end if;
12440
12441 if not LoSet then
12442 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
12443 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
12444 LoSet := True;
12445 exit when HiSet;
12446 end if;
12447 end if;
12448
12449 if not HiSet then
12450 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
12451 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
12452 HiSet := True;
12453 exit when LoSet;
12454 end if;
12455 end if;
12456
12457 Ancest := Ancestor_Subtype (Ancest);
12458
12459 if No (Ancest) then
12460 Ancest := Base_Type (T);
12461
12462 if Is_Generic_Type (Ancest) then
12463 return 0;
12464 end if;
12465 end if;
12466 end loop;
12467
12468 -- Fixed-point types. We can't simply use Expr_Value to get the
12469 -- Corresponding_Integer_Value values of the bounds, since these do not
12470 -- get set till the type is frozen, and this routine can be called
12471 -- before the type is frozen. Similarly the test for bounds being static
12472 -- needs to include the case where we have unanalyzed real literals for
12473 -- the same reason.
12474
12475 elsif Is_Fixed_Point_Type (T) then
12476
12477 -- The following loop is looking for the nearest compile time known
12478 -- bounds following the ancestor subtype chain. The idea is to find
12479 -- the most restrictive known bounds information.
12480
12481 Ancest := T;
12482 loop
12483 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12484 return 0;
12485 end if;
12486
12487 -- Note: In the following two tests for LoSet and HiSet, it may
12488 -- seem redundant to test for N_Real_Literal here since normally
12489 -- one would assume that the test for the value being known at
12490 -- compile time includes this case. However, there is a glitch.
12491 -- If the real literal comes from folding a non-static expression,
12492 -- then we don't consider any non- static expression to be known
12493 -- at compile time if we are in configurable run time mode (needed
12494 -- in some cases to give a clearer definition of what is and what
12495 -- is not accepted). So the test is indeed needed. Without it, we
12496 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
12497
12498 if not LoSet then
12499 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
12500 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
12501 then
12502 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
12503 LoSet := True;
12504 exit when HiSet;
12505 end if;
12506 end if;
12507
12508 if not HiSet then
12509 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
12510 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
12511 then
12512 HiR := Expr_Value_R (Type_High_Bound (Ancest));
12513 HiSet := True;
12514 exit when LoSet;
12515 end if;
12516 end if;
12517
12518 Ancest := Ancestor_Subtype (Ancest);
12519
12520 if No (Ancest) then
12521 Ancest := Base_Type (T);
12522
12523 if Is_Generic_Type (Ancest) then
12524 return 0;
12525 end if;
12526 end if;
12527 end loop;
12528
12529 Lo := UR_To_Uint (LoR / Small_Value (T));
12530 Hi := UR_To_Uint (HiR / Small_Value (T));
12531
12532 -- No other types allowed
12533
12534 else
12535 raise Program_Error;
12536 end if;
12537
12538 -- Fall through with Hi and Lo set. Deal with biased case
12539
12540 if (Biased
12541 and then not Is_Fixed_Point_Type (T)
12542 and then not (Is_Enumeration_Type (T)
12543 and then Has_Non_Standard_Rep (T)))
12544 or else Has_Biased_Representation (T)
12545 then
12546 Hi := Hi - Lo;
12547 Lo := Uint_0;
12548 end if;
12549
12550 -- Null range case, size is always zero. We only do this in the discrete
12551 -- type case, since that's the odd case that came up. Probably we should
12552 -- also do this in the fixed-point case, but doing so causes peculiar
12553 -- gigi failures, and it is not worth worrying about this incredibly
12554 -- marginal case (explicit null-range fixed-point type declarations)???
12555
12556 if Lo > Hi and then Is_Discrete_Type (T) then
12557 S := 0;
12558
12559 -- Signed case. Note that we consider types like range 1 .. -1 to be
12560 -- signed for the purpose of computing the size, since the bounds have
12561 -- to be accommodated in the base type.
12562
12563 elsif Lo < 0 or else Hi < 0 then
12564 S := 1;
12565 B := Uint_1;
12566
12567 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12568 -- Note that we accommodate the case where the bounds cross. This
12569 -- can happen either because of the way the bounds are declared
12570 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12571
12572 while Lo < -B
12573 or else Hi < -B
12574 or else Lo >= B
12575 or else Hi >= B
12576 loop
12577 B := Uint_2 ** S;
12578 S := S + 1;
12579 end loop;
12580
12581 -- Unsigned case
12582
12583 else
12584 -- If both bounds are positive, make sure that both are represen-
12585 -- table in the case where the bounds are crossed. This can happen
12586 -- either because of the way the bounds are declared, or because of
12587 -- the algorithm in Freeze_Fixed_Point_Type.
12588
12589 if Lo > Hi then
12590 Hi := Lo;
12591 end if;
12592
12593 -- S = size, (can accommodate 0 .. (2**size - 1))
12594
12595 S := 0;
12596 while Hi >= Uint_2 ** S loop
12597 S := S + 1;
12598 end loop;
12599 end if;
12600
12601 return S;
12602 end Minimum_Size;
12603
12604 ---------------------------
12605 -- New_Stream_Subprogram --
12606 ---------------------------
12607
12608 procedure New_Stream_Subprogram
12609 (N : Node_Id;
12610 Ent : Entity_Id;
12611 Subp : Entity_Id;
12612 Nam : TSS_Name_Type)
12613 is
12614 Loc : constant Source_Ptr := Sloc (N);
12615 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
12616 Subp_Id : Entity_Id;
12617 Subp_Decl : Node_Id;
12618 F : Entity_Id;
12619 Etyp : Entity_Id;
12620
12621 Defer_Declaration : constant Boolean :=
12622 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12623 -- For a tagged type, there is a declaration for each stream attribute
12624 -- at the freeze point, and we must generate only a completion of this
12625 -- declaration. We do the same for private types, because the full view
12626 -- might be tagged. Otherwise we generate a declaration at the point of
12627 -- the attribute definition clause. If the attribute definition comes
12628 -- from an aspect specification the declaration is part of the freeze
12629 -- actions of the type.
12630
12631 function Build_Spec return Node_Id;
12632 -- Used for declaration and renaming declaration, so that this is
12633 -- treated as a renaming_as_body.
12634
12635 ----------------
12636 -- Build_Spec --
12637 ----------------
12638
12639 function Build_Spec return Node_Id is
12640 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12641 Formals : List_Id;
12642 Spec : Node_Id;
12643 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
12644
12645 begin
12646 Subp_Id := Make_Defining_Identifier (Loc, Sname);
12647
12648 -- S : access Root_Stream_Type'Class
12649
12650 Formals := New_List (
12651 Make_Parameter_Specification (Loc,
12652 Defining_Identifier =>
12653 Make_Defining_Identifier (Loc, Name_S),
12654 Parameter_Type =>
12655 Make_Access_Definition (Loc,
12656 Subtype_Mark =>
12657 New_Occurrence_Of (
12658 Designated_Type (Etype (F)), Loc))));
12659
12660 if Nam = TSS_Stream_Input then
12661 Spec :=
12662 Make_Function_Specification (Loc,
12663 Defining_Unit_Name => Subp_Id,
12664 Parameter_Specifications => Formals,
12665 Result_Definition => T_Ref);
12666 else
12667 -- V : [out] T
12668
12669 Append_To (Formals,
12670 Make_Parameter_Specification (Loc,
12671 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12672 Out_Present => Out_P,
12673 Parameter_Type => T_Ref));
12674
12675 Spec :=
12676 Make_Procedure_Specification (Loc,
12677 Defining_Unit_Name => Subp_Id,
12678 Parameter_Specifications => Formals);
12679 end if;
12680
12681 return Spec;
12682 end Build_Spec;
12683
12684 -- Start of processing for New_Stream_Subprogram
12685
12686 begin
12687 F := First_Formal (Subp);
12688
12689 if Ekind (Subp) = E_Procedure then
12690 Etyp := Etype (Next_Formal (F));
12691 else
12692 Etyp := Etype (Subp);
12693 end if;
12694
12695 -- Prepare subprogram declaration and insert it as an action on the
12696 -- clause node. The visibility for this entity is used to test for
12697 -- visibility of the attribute definition clause (in the sense of
12698 -- 8.3(23) as amended by AI-195).
12699
12700 if not Defer_Declaration then
12701 Subp_Decl :=
12702 Make_Subprogram_Declaration (Loc,
12703 Specification => Build_Spec);
12704
12705 -- For a tagged type, there is always a visible declaration for each
12706 -- stream TSS (it is a predefined primitive operation), and the
12707 -- completion of this declaration occurs at the freeze point, which is
12708 -- not always visible at places where the attribute definition clause is
12709 -- visible. So, we create a dummy entity here for the purpose of
12710 -- tracking the visibility of the attribute definition clause itself.
12711
12712 else
12713 Subp_Id :=
12714 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
12715 Subp_Decl :=
12716 Make_Object_Declaration (Loc,
12717 Defining_Identifier => Subp_Id,
12718 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
12719 end if;
12720
12721 if not Defer_Declaration
12722 and then From_Aspect_Specification (N)
12723 and then Has_Delayed_Freeze (Ent)
12724 then
12725 Append_Freeze_Action (Ent, Subp_Decl);
12726
12727 else
12728 Insert_Action (N, Subp_Decl);
12729 Set_Entity (N, Subp_Id);
12730 end if;
12731
12732 Subp_Decl :=
12733 Make_Subprogram_Renaming_Declaration (Loc,
12734 Specification => Build_Spec,
12735 Name => New_Occurrence_Of (Subp, Loc));
12736
12737 if Defer_Declaration then
12738 Set_TSS (Base_Type (Ent), Subp_Id);
12739
12740 else
12741 if From_Aspect_Specification (N) then
12742 Append_Freeze_Action (Ent, Subp_Decl);
12743 else
12744 Insert_Action (N, Subp_Decl);
12745 end if;
12746
12747 Copy_TSS (Subp_Id, Base_Type (Ent));
12748 end if;
12749 end New_Stream_Subprogram;
12750
12751 --------------
12752 -- Pop_Type --
12753 --------------
12754
12755 procedure Pop_Type (E : Entity_Id) is
12756 begin
12757 if Ekind (E) = E_Record_Type and then E = Current_Scope then
12758 End_Scope;
12759
12760 elsif Is_Type (E)
12761 and then Has_Discriminants (E)
12762 and then Nkind (Parent (E)) /= N_Subtype_Declaration
12763 then
12764 Uninstall_Discriminants (E);
12765 Pop_Scope;
12766 end if;
12767 end Pop_Type;
12768
12769 ---------------
12770 -- Push_Type --
12771 ---------------
12772
12773 procedure Push_Type (E : Entity_Id) is
12774 Comp : Entity_Id;
12775
12776 begin
12777 if Ekind (E) = E_Record_Type then
12778 Push_Scope (E);
12779
12780 Comp := First_Component (E);
12781 while Present (Comp) loop
12782 Install_Entity (Comp);
12783 Next_Component (Comp);
12784 end loop;
12785
12786 if Has_Discriminants (E) then
12787 Install_Discriminants (E);
12788 end if;
12789
12790 elsif Is_Type (E)
12791 and then Has_Discriminants (E)
12792 and then Nkind (Parent (E)) /= N_Subtype_Declaration
12793 then
12794 Push_Scope (E);
12795 Install_Discriminants (E);
12796 end if;
12797 end Push_Type;
12798
12799 -----------------------------------
12800 -- Register_Address_Clause_Check --
12801 -----------------------------------
12802
12803 procedure Register_Address_Clause_Check
12804 (N : Node_Id;
12805 X : Entity_Id;
12806 A : Uint;
12807 Y : Entity_Id;
12808 Off : Boolean)
12809 is
12810 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
12811 begin
12812 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
12813 end Register_Address_Clause_Check;
12814
12815 ------------------------
12816 -- Rep_Item_Too_Early --
12817 ------------------------
12818
12819 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12820 function Has_Generic_Parent (E : Entity_Id) return Boolean;
12821 -- Return True if any ancestor is a generic type
12822
12823 ------------------------
12824 -- Has_Generic_Parent --
12825 ------------------------
12826
12827 function Has_Generic_Parent (E : Entity_Id) return Boolean is
12828 Ancestor_Type : Entity_Id := Etype (E);
12829
12830 begin
12831 while Present (Ancestor_Type)
12832 and then not Is_Generic_Type (Ancestor_Type)
12833 and then Etype (Ancestor_Type) /= Ancestor_Type
12834 loop
12835 Ancestor_Type := Etype (Ancestor_Type);
12836 end loop;
12837
12838 return
12839 Present (Ancestor_Type) and then Is_Generic_Type (Ancestor_Type);
12840 end Has_Generic_Parent;
12841
12842 -- Start of processing for Rep_Item_Too_Early
12843
12844 begin
12845 -- Cannot apply non-operational rep items to generic types
12846
12847 if Is_Operational_Item (N) then
12848 return False;
12849
12850 elsif Is_Type (T)
12851 and then Has_Generic_Parent (T)
12852 and then (Nkind (N) /= N_Pragma
12853 or else Get_Pragma_Id (N) /= Pragma_Convention)
12854 then
12855 if Ada_Version < Ada_2020 then
12856 Error_Msg_N
12857 ("representation item not allowed for generic type", N);
12858 return True;
12859 else
12860 return False;
12861 end if;
12862 end if;
12863
12864 -- Otherwise check for incomplete type
12865
12866 if Is_Incomplete_Or_Private_Type (T)
12867 and then No (Underlying_Type (T))
12868 and then
12869 (Nkind (N) /= N_Pragma
12870 or else Get_Pragma_Id (N) /= Pragma_Import)
12871 then
12872 Error_Msg_N
12873 ("representation item must be after full type declaration", N);
12874 return True;
12875
12876 -- If the type has incomplete components, a representation clause is
12877 -- illegal but stream attributes and Convention pragmas are correct.
12878
12879 elsif Has_Private_Component (T) then
12880 if Nkind (N) = N_Pragma then
12881 return False;
12882
12883 else
12884 Error_Msg_N
12885 ("representation item must appear after type is fully defined",
12886 N);
12887 return True;
12888 end if;
12889 else
12890 return False;
12891 end if;
12892 end Rep_Item_Too_Early;
12893
12894 -----------------------
12895 -- Rep_Item_Too_Late --
12896 -----------------------
12897
12898 function Rep_Item_Too_Late
12899 (T : Entity_Id;
12900 N : Node_Id;
12901 FOnly : Boolean := False) return Boolean
12902 is
12903 function Is_Derived_Type_With_Constraint return Boolean;
12904 -- Check whether T is a derived type with an explicit constraint, in
12905 -- which case the constraint has frozen the type and the item is too
12906 -- late. This compensates for the fact that for derived scalar types
12907 -- we freeze the base type unconditionally on account of a long-standing
12908 -- issue in gigi.
12909
12910 procedure No_Type_Rep_Item;
12911 -- Output message indicating that no type-related aspects can be
12912 -- specified due to some property of the parent type.
12913
12914 procedure Too_Late;
12915 -- Output message for an aspect being specified too late
12916
12917 -- Note that neither of the above errors is considered a serious one,
12918 -- since the effect is simply that we ignore the representation clause
12919 -- in these cases.
12920 -- Is this really true? In any case if we make this change we must
12921 -- document the requirement in the spec of Rep_Item_Too_Late that
12922 -- if True is returned, then the rep item must be completely ignored???
12923
12924 --------------------------------------
12925 -- Is_Derived_Type_With_Constraint --
12926 --------------------------------------
12927
12928 function Is_Derived_Type_With_Constraint return Boolean is
12929 Decl : constant Node_Id := Declaration_Node (T);
12930
12931 begin
12932 return Is_Derived_Type (T)
12933 and then Is_Frozen (Base_Type (T))
12934 and then Is_Enumeration_Type (T)
12935 and then False
12936 and then Nkind (N) = N_Enumeration_Representation_Clause
12937 and then Nkind (Decl) = N_Subtype_Declaration
12938 and then not Is_Entity_Name (Subtype_Indication (Decl));
12939 end Is_Derived_Type_With_Constraint;
12940
12941 ----------------------
12942 -- No_Type_Rep_Item --
12943 ----------------------
12944
12945 procedure No_Type_Rep_Item is
12946 begin
12947 Error_Msg_N ("|type-related representation item not permitted!", N);
12948 end No_Type_Rep_Item;
12949
12950 --------------
12951 -- Too_Late --
12952 --------------
12953
12954 procedure Too_Late is
12955 begin
12956 -- Other compilers seem more relaxed about rep items appearing too
12957 -- late. Since analysis tools typically don't care about rep items
12958 -- anyway, no reason to be too strict about this.
12959
12960 if not Relaxed_RM_Semantics then
12961 Error_Msg_N ("|representation item appears too late!", N);
12962 end if;
12963 end Too_Late;
12964
12965 -- Local variables
12966
12967 Parent_Type : Entity_Id;
12968 S : Entity_Id;
12969
12970 -- Start of processing for Rep_Item_Too_Late
12971
12972 begin
12973 -- First make sure entity is not frozen (RM 13.1(9))
12974
12975 if (Is_Frozen (T)
12976 or else (Is_Type (T)
12977 and then Is_Derived_Type_With_Constraint))
12978
12979 -- Exclude imported types, which may be frozen if they appear in a
12980 -- representation clause for a local type.
12981
12982 and then not From_Limited_With (T)
12983
12984 -- Exclude generated entities (not coming from source). The common
12985 -- case is when we generate a renaming which prematurely freezes the
12986 -- renamed internal entity, but we still want to be able to set copies
12987 -- of attribute values such as Size/Alignment.
12988
12989 and then Comes_From_Source (T)
12990 then
12991 -- A self-referential aspect is illegal if it forces freezing the
12992 -- entity before the corresponding pragma has been analyzed.
12993
12994 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12995 and then From_Aspect_Specification (N)
12996 then
12997 Error_Msg_NE
12998 ("aspect specification causes premature freezing of&", N, T);
12999 Set_Has_Delayed_Freeze (T, False);
13000 return True;
13001 end if;
13002
13003 Too_Late;
13004 S := First_Subtype (T);
13005
13006 if Present (Freeze_Node (S)) then
13007 if not Relaxed_RM_Semantics then
13008 Error_Msg_NE
13009 ("??no more representation items for }", Freeze_Node (S), S);
13010 end if;
13011 end if;
13012
13013 return True;
13014
13015 -- Check for case of untagged derived type whose parent either has
13016 -- primitive operations, or is a by reference type (RM 13.1(10)). In
13017 -- this case we do not output a Too_Late message, since there is no
13018 -- earlier point where the rep item could be placed to make it legal.
13019
13020 elsif Is_Type (T)
13021 and then not FOnly
13022 and then Is_Derived_Type (T)
13023 and then not Is_Tagged_Type (T)
13024 then
13025 Parent_Type := Etype (Base_Type (T));
13026
13027 if Has_Primitive_Operations (Parent_Type) then
13028 No_Type_Rep_Item;
13029
13030 if not Relaxed_RM_Semantics then
13031 Error_Msg_NE
13032 ("\parent type & has primitive operations!", N, Parent_Type);
13033 end if;
13034
13035 return True;
13036
13037 elsif Is_By_Reference_Type (Parent_Type) then
13038 No_Type_Rep_Item;
13039
13040 if not Relaxed_RM_Semantics then
13041 Error_Msg_NE
13042 ("\parent type & is a by reference type!", N, Parent_Type);
13043 end if;
13044
13045 return True;
13046 end if;
13047 end if;
13048
13049 -- No error, but one more warning to consider. The RM (surprisingly)
13050 -- allows this pattern in some cases:
13051
13052 -- type S is ...
13053 -- primitive operations for S
13054 -- type R is new S;
13055 -- rep clause for S
13056
13057 -- Meaning that calls on the primitive operations of S for values of
13058 -- type R may require possibly expensive implicit conversion operations.
13059 -- So even when this is not an error, it is still worth a warning.
13060
13061 if not Relaxed_RM_Semantics and then Is_Type (T) then
13062 declare
13063 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
13064
13065 begin
13066 if Present (DTL)
13067
13068 -- For now, do not generate this warning for the case of
13069 -- aspect specification using Ada 2012 syntax, since we get
13070 -- wrong messages we do not understand. The whole business
13071 -- of derived types and rep items seems a bit confused when
13072 -- aspects are used, since the aspects are not evaluated
13073 -- till freeze time. However, AI12-0109 confirms (in an AARM
13074 -- ramification) that inheritance in this case is required
13075 -- to work.
13076
13077 and then not From_Aspect_Specification (N)
13078 then
13079 if Is_By_Reference_Type (T)
13080 and then not Is_Tagged_Type (T)
13081 and then Is_Type_Related_Rep_Item (N)
13082 and then (Ada_Version >= Ada_2012
13083 or else Has_Primitive_Operations (Base_Type (T)))
13084 then
13085 -- Treat as hard error (AI12-0109, binding interpretation).
13086 -- Implementing a change of representation is not really
13087 -- an option in the case of a by-reference type, so we
13088 -- take this path for all Ada dialects if primitive
13089 -- operations are present.
13090 Error_Msg_Sloc := Sloc (DTL);
13091 Error_Msg_N
13092 ("representation item for& appears after derived type "
13093 & "declaration#", N);
13094
13095 elsif Has_Primitive_Operations (Base_Type (T)) then
13096 Error_Msg_Sloc := Sloc (DTL);
13097
13098 Error_Msg_N
13099 ("representation item for& appears after derived type "
13100 & "declaration#??", N);
13101 Error_Msg_NE
13102 ("\may result in implicit conversions for primitive "
13103 & "operations of&??", N, T);
13104 Error_Msg_NE
13105 ("\to change representations when called with arguments "
13106 & "of type&??", N, DTL);
13107 end if;
13108 end if;
13109 end;
13110 end if;
13111
13112 -- No error, link item into head of chain of rep items for the entity,
13113 -- but avoid chaining if we have an overloadable entity, and the pragma
13114 -- is one that can apply to multiple overloaded entities.
13115
13116 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
13117 declare
13118 Pname : constant Name_Id := Pragma_Name (N);
13119 begin
13120 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
13121 Name_External, Name_Interface)
13122 then
13123 return False;
13124 end if;
13125 end;
13126 end if;
13127
13128 Record_Rep_Item (T, N);
13129 return False;
13130 end Rep_Item_Too_Late;
13131
13132 -------------------------------------
13133 -- Replace_Type_References_Generic --
13134 -------------------------------------
13135
13136 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
13137 TName : constant Name_Id := Chars (T);
13138
13139 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
13140 -- Processes a single node in the traversal procedure below, checking
13141 -- if node N should be replaced, and if so, doing the replacement.
13142
13143 function Visible_Component (Comp : Name_Id) return Entity_Id;
13144 -- Given an identifier in the expression, check whether there is a
13145 -- discriminant or component of the type that is directy visible, and
13146 -- rewrite it as the corresponding selected component of the formal of
13147 -- the subprogram. The entity is located by a sequential search, which
13148 -- seems acceptable given the typical size of component lists and check
13149 -- expressions. Possible optimization ???
13150
13151 ----------------------
13152 -- Replace_Type_Ref --
13153 ----------------------
13154
13155 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
13156 Loc : constant Source_Ptr := Sloc (N);
13157
13158 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
13159 -- Add the proper prefix to a reference to a component of the type
13160 -- when it is not already a selected component.
13161
13162 ----------------
13163 -- Add_Prefix --
13164 ----------------
13165
13166 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
13167 begin
13168 Rewrite (Ref,
13169 Make_Selected_Component (Loc,
13170 Prefix => New_Occurrence_Of (T, Loc),
13171 Selector_Name => New_Occurrence_Of (Comp, Loc)));
13172 Replace_Type_Reference (Prefix (Ref));
13173 end Add_Prefix;
13174
13175 -- Local variables
13176
13177 Comp : Entity_Id;
13178 Pref : Node_Id;
13179 Scop : Entity_Id;
13180
13181 -- Start of processing for Replace_Type_Ref
13182
13183 begin
13184 if Nkind (N) = N_Identifier then
13185
13186 -- If not the type name, check whether it is a reference to some
13187 -- other type, which must be frozen before the predicate function
13188 -- is analyzed, i.e. before the freeze node of the type to which
13189 -- the predicate applies.
13190
13191 if Chars (N) /= TName then
13192 if Present (Current_Entity (N))
13193 and then Is_Type (Current_Entity (N))
13194 then
13195 Freeze_Before (Freeze_Node (T), Current_Entity (N));
13196 end if;
13197
13198 -- The components of the type are directly visible and can
13199 -- be referenced without a prefix.
13200
13201 if Nkind (Parent (N)) = N_Selected_Component then
13202 null;
13203
13204 -- In expression C (I), C may be a directly visible function
13205 -- or a visible component that has an array type. Disambiguate
13206 -- by examining the component type.
13207
13208 elsif Nkind (Parent (N)) = N_Indexed_Component
13209 and then N = Prefix (Parent (N))
13210 then
13211 Comp := Visible_Component (Chars (N));
13212
13213 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
13214 Add_Prefix (N, Comp);
13215 end if;
13216
13217 else
13218 Comp := Visible_Component (Chars (N));
13219
13220 if Present (Comp) then
13221 Add_Prefix (N, Comp);
13222 end if;
13223 end if;
13224
13225 return Skip;
13226
13227 -- Otherwise do the replacement if this is not a qualified
13228 -- reference to a homograph of the type itself. Note that the
13229 -- current instance could not appear in such a context, e.g.
13230 -- the prefix of a type conversion.
13231
13232 else
13233 if Nkind (Parent (N)) /= N_Selected_Component
13234 or else N /= Selector_Name (Parent (N))
13235 then
13236 Replace_Type_Reference (N);
13237 end if;
13238
13239 return Skip;
13240 end if;
13241
13242 -- Case of selected component, which may be a subcomponent of the
13243 -- current instance, or an expanded name which is still unanalyzed.
13244
13245 elsif Nkind (N) = N_Selected_Component then
13246
13247 -- If selector name is not our type, keep going (we might still
13248 -- have an occurrence of the type in the prefix). If it is a
13249 -- subcomponent of the current entity, add prefix.
13250
13251 if Nkind (Selector_Name (N)) /= N_Identifier
13252 or else Chars (Selector_Name (N)) /= TName
13253 then
13254 if Nkind (Prefix (N)) = N_Identifier then
13255 Comp := Visible_Component (Chars (Prefix (N)));
13256
13257 if Present (Comp) then
13258 Add_Prefix (Prefix (N), Comp);
13259 end if;
13260 end if;
13261
13262 return OK;
13263
13264 -- Selector name is our type, check qualification
13265
13266 else
13267 -- Loop through scopes and prefixes, doing comparison
13268
13269 Scop := Current_Scope;
13270 Pref := Prefix (N);
13271 loop
13272 -- Continue if no more scopes or scope with no name
13273
13274 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
13275 return OK;
13276 end if;
13277
13278 -- Do replace if prefix is an identifier matching the scope
13279 -- that we are currently looking at.
13280
13281 if Nkind (Pref) = N_Identifier
13282 and then Chars (Pref) = Chars (Scop)
13283 then
13284 Replace_Type_Reference (N);
13285 return Skip;
13286 end if;
13287
13288 -- Go check scope above us if prefix is itself of the form
13289 -- of a selected component, whose selector matches the scope
13290 -- we are currently looking at.
13291
13292 if Nkind (Pref) = N_Selected_Component
13293 and then Nkind (Selector_Name (Pref)) = N_Identifier
13294 and then Chars (Selector_Name (Pref)) = Chars (Scop)
13295 then
13296 Scop := Scope (Scop);
13297 Pref := Prefix (Pref);
13298
13299 -- For anything else, we don't have a match, so keep on
13300 -- going, there are still some weird cases where we may
13301 -- still have a replacement within the prefix.
13302
13303 else
13304 return OK;
13305 end if;
13306 end loop;
13307 end if;
13308
13309 -- Continue for any other node kind
13310
13311 else
13312 return OK;
13313 end if;
13314 end Replace_Type_Ref;
13315
13316 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
13317
13318 -----------------------
13319 -- Visible_Component --
13320 -----------------------
13321
13322 function Visible_Component (Comp : Name_Id) return Entity_Id is
13323 E : Entity_Id;
13324
13325 begin
13326 -- Types with nameable components are records and discriminated
13327 -- private types.
13328
13329 if Ekind (T) = E_Record_Type
13330 or else (Is_Private_Type (T) and then Has_Discriminants (T))
13331 then
13332 E := First_Entity (T);
13333 while Present (E) loop
13334 if Comes_From_Source (E) and then Chars (E) = Comp then
13335 return E;
13336 end if;
13337
13338 Next_Entity (E);
13339 end loop;
13340 end if;
13341
13342 -- Nothing by that name, or the type has no components
13343
13344 return Empty;
13345 end Visible_Component;
13346
13347 -- Start of processing for Replace_Type_References_Generic
13348
13349 begin
13350 Replace_Type_Refs (N);
13351 end Replace_Type_References_Generic;
13352
13353 --------------------------------
13354 -- Resolve_Aspect_Expressions --
13355 --------------------------------
13356
13357 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
13358 function Resolve_Name (N : Node_Id) return Traverse_Result;
13359 -- Verify that all identifiers in the expression, with the exception
13360 -- of references to the current entity, denote visible entities. This
13361 -- is done only to detect visibility errors, as the expression will be
13362 -- properly analyzed/expanded during analysis of the predicate function
13363 -- body. We omit quantified expressions from this test, given that they
13364 -- introduce a local identifier that would require proper expansion to
13365 -- handle properly.
13366
13367 -- In ASIS_Mode we preserve the entity in the source because there is
13368 -- no subsequent expansion to decorate the tree.
13369
13370 ------------------
13371 -- Resolve_Name --
13372 ------------------
13373
13374 function Resolve_Name (N : Node_Id) return Traverse_Result is
13375 Dummy : Traverse_Result;
13376
13377 begin
13378 if Nkind (N) = N_Selected_Component then
13379 if Nkind (Prefix (N)) = N_Identifier
13380 and then Chars (Prefix (N)) /= Chars (E)
13381 then
13382 Find_Selected_Component (N);
13383 end if;
13384
13385 return Skip;
13386
13387 -- Resolve identifiers that are not selectors in parameter
13388 -- associations (these are never resolved by visibility).
13389
13390 elsif Nkind (N) = N_Identifier
13391 and then Chars (N) /= Chars (E)
13392 and then (Nkind (Parent (N)) /= N_Parameter_Association
13393 or else N /= Selector_Name (Parent (N)))
13394 then
13395 Find_Direct_Name (N);
13396
13397 -- In ASIS mode we must analyze overloaded identifiers to ensure
13398 -- their correct decoration because expansion is disabled (and
13399 -- the expansion of freeze nodes takes care of resolving aspect
13400 -- expressions).
13401
13402 if ASIS_Mode then
13403 if Is_Overloaded (N) then
13404 Analyze (Parent (N));
13405 end if;
13406 else
13407 Set_Entity (N, Empty);
13408 end if;
13409
13410 -- The name is component association needs no resolution.
13411
13412 elsif Nkind (N) = N_Component_Association then
13413 Dummy := Resolve_Name (Expression (N));
13414 return Skip;
13415
13416 elsif Nkind (N) = N_Quantified_Expression then
13417 return Skip;
13418 end if;
13419
13420 return OK;
13421 end Resolve_Name;
13422
13423 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
13424
13425 -- Local variables
13426
13427 ASN : Node_Id := First_Rep_Item (E);
13428
13429 -- Start of processing for Resolve_Aspect_Expressions
13430
13431 begin
13432 if No (ASN) then
13433 return;
13434 end if;
13435
13436 while Present (ASN) loop
13437 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
13438 declare
13439 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
13440 Expr : constant Node_Id := Expression (ASN);
13441
13442 begin
13443 case A_Id is
13444
13445 -- For now we only deal with aspects that do not generate
13446 -- subprograms, or that may mention current instances of
13447 -- types. These will require special handling (???TBD).
13448
13449 when Aspect_Invariant
13450 | Aspect_Predicate
13451 | Aspect_Predicate_Failure
13452 =>
13453 null;
13454
13455 when Aspect_Dynamic_Predicate
13456 | Aspect_Static_Predicate
13457 =>
13458 -- Build predicate function specification and preanalyze
13459 -- expression after type replacement. The function
13460 -- declaration must be analyzed in the scope of the type,
13461 -- but the expression can reference components and
13462 -- discriminants of the type.
13463
13464 if No (Predicate_Function (E)) then
13465 declare
13466 FDecl : constant Node_Id :=
13467 Build_Predicate_Function_Declaration (E);
13468 pragma Unreferenced (FDecl);
13469
13470 begin
13471 Push_Type (E);
13472 Resolve_Aspect_Expression (Expr);
13473 Pop_Type (E);
13474 end;
13475 end if;
13476
13477 when Pre_Post_Aspects =>
13478 null;
13479
13480 when Aspect_Iterable =>
13481 if Nkind (Expr) = N_Aggregate then
13482 declare
13483 Assoc : Node_Id;
13484
13485 begin
13486 Assoc := First (Component_Associations (Expr));
13487 while Present (Assoc) loop
13488 Find_Direct_Name (Expression (Assoc));
13489 Next (Assoc);
13490 end loop;
13491 end;
13492 end if;
13493
13494 -- The expression for Default_Value is a static expression
13495 -- of the type, but this expression does not freeze the
13496 -- type, so it can still appear in a representation clause
13497 -- before the actual freeze point.
13498
13499 when Aspect_Default_Value =>
13500 Set_Must_Not_Freeze (Expr);
13501 Preanalyze_Spec_Expression (Expr, E);
13502
13503 when Aspect_Priority =>
13504 Push_Type (E);
13505 Preanalyze_Spec_Expression (Expr, Any_Integer);
13506 Pop_Type (E);
13507
13508 -- Ditto for Storage_Size. Any other aspects that carry
13509 -- expressions that should not freeze ??? This is only
13510 -- relevant to the misuse of deferred constants.
13511
13512 when Aspect_Storage_Size =>
13513 Set_Must_Not_Freeze (Expr);
13514 Preanalyze_Spec_Expression (Expr, Any_Integer);
13515
13516 when others =>
13517 if Present (Expr) then
13518 case Aspect_Argument (A_Id) is
13519 when Expression
13520 | Optional_Expression
13521 =>
13522 Analyze_And_Resolve (Expr);
13523
13524 when Name
13525 | Optional_Name
13526 =>
13527 if Nkind (Expr) = N_Identifier then
13528 Find_Direct_Name (Expr);
13529
13530 elsif Nkind (Expr) = N_Selected_Component then
13531 Find_Selected_Component (Expr);
13532 end if;
13533 end case;
13534 end if;
13535 end case;
13536 end;
13537 end if;
13538
13539 ASN := Next_Rep_Item (ASN);
13540 end loop;
13541 end Resolve_Aspect_Expressions;
13542
13543 -------------------------
13544 -- Same_Representation --
13545 -------------------------
13546
13547 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
13548 T1 : constant Entity_Id := Underlying_Type (Typ1);
13549 T2 : constant Entity_Id := Underlying_Type (Typ2);
13550
13551 begin
13552 -- A quick check, if base types are the same, then we definitely have
13553 -- the same representation, because the subtype specific representation
13554 -- attributes (Size and Alignment) do not affect representation from
13555 -- the point of view of this test.
13556
13557 if Base_Type (T1) = Base_Type (T2) then
13558 return True;
13559
13560 elsif Is_Private_Type (Base_Type (T2))
13561 and then Base_Type (T1) = Full_View (Base_Type (T2))
13562 then
13563 return True;
13564
13565 -- If T2 is a generic actual it is declared as a subtype, so
13566 -- check against its base type.
13567
13568 elsif Is_Generic_Actual_Type (T1)
13569 and then Same_Representation (Base_Type (T1), T2)
13570 then
13571 return True;
13572 end if;
13573
13574 -- Tagged types always have the same representation, because it is not
13575 -- possible to specify different representations for common fields.
13576
13577 if Is_Tagged_Type (T1) then
13578 return True;
13579 end if;
13580
13581 -- Representations are definitely different if conventions differ
13582
13583 if Convention (T1) /= Convention (T2) then
13584 return False;
13585 end if;
13586
13587 -- Representations are different if component alignments or scalar
13588 -- storage orders differ.
13589
13590 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
13591 and then
13592 (Is_Record_Type (T2) or else Is_Array_Type (T2))
13593 and then
13594 (Component_Alignment (T1) /= Component_Alignment (T2)
13595 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13596 then
13597 return False;
13598 end if;
13599
13600 -- For arrays, the only real issue is component size. If we know the
13601 -- component size for both arrays, and it is the same, then that's
13602 -- good enough to know we don't have a change of representation.
13603
13604 if Is_Array_Type (T1) then
13605 if Known_Component_Size (T1)
13606 and then Known_Component_Size (T2)
13607 and then Component_Size (T1) = Component_Size (T2)
13608 then
13609 return True;
13610 end if;
13611 end if;
13612
13613 -- For records, representations are different if reorderings differ
13614
13615 if Is_Record_Type (T1)
13616 and then Is_Record_Type (T2)
13617 and then No_Reordering (T1) /= No_Reordering (T2)
13618 then
13619 return False;
13620 end if;
13621
13622 -- Types definitely have same representation if neither has non-standard
13623 -- representation since default representations are always consistent.
13624 -- If only one has non-standard representation, and the other does not,
13625 -- then we consider that they do not have the same representation. They
13626 -- might, but there is no way of telling early enough.
13627
13628 if Has_Non_Standard_Rep (T1) then
13629 if not Has_Non_Standard_Rep (T2) then
13630 return False;
13631 end if;
13632 else
13633 return not Has_Non_Standard_Rep (T2);
13634 end if;
13635
13636 -- Here the two types both have non-standard representation, and we need
13637 -- to determine if they have the same non-standard representation.
13638
13639 -- For arrays, we simply need to test if the component sizes are the
13640 -- same. Pragma Pack is reflected in modified component sizes, so this
13641 -- check also deals with pragma Pack.
13642
13643 if Is_Array_Type (T1) then
13644 return Component_Size (T1) = Component_Size (T2);
13645
13646 -- Case of record types
13647
13648 elsif Is_Record_Type (T1) then
13649
13650 -- Packed status must conform
13651
13652 if Is_Packed (T1) /= Is_Packed (T2) then
13653 return False;
13654
13655 -- Otherwise we must check components. Typ2 maybe a constrained
13656 -- subtype with fewer components, so we compare the components
13657 -- of the base types.
13658
13659 else
13660 Record_Case : declare
13661 CD1, CD2 : Entity_Id;
13662
13663 function Same_Rep return Boolean;
13664 -- CD1 and CD2 are either components or discriminants. This
13665 -- function tests whether they have the same representation.
13666
13667 --------------
13668 -- Same_Rep --
13669 --------------
13670
13671 function Same_Rep return Boolean is
13672 begin
13673 if No (Component_Clause (CD1)) then
13674 return No (Component_Clause (CD2));
13675 else
13676 -- Note: at this point, component clauses have been
13677 -- normalized to the default bit order, so that the
13678 -- comparison of Component_Bit_Offsets is meaningful.
13679
13680 return
13681 Present (Component_Clause (CD2))
13682 and then
13683 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13684 and then
13685 Esize (CD1) = Esize (CD2);
13686 end if;
13687 end Same_Rep;
13688
13689 -- Start of processing for Record_Case
13690
13691 begin
13692 if Has_Discriminants (T1) then
13693
13694 -- The number of discriminants may be different if the
13695 -- derived type has fewer (constrained by values). The
13696 -- invisible discriminants retain the representation of
13697 -- the original, so the discrepancy does not per se
13698 -- indicate a different representation.
13699
13700 CD1 := First_Discriminant (T1);
13701 CD2 := First_Discriminant (T2);
13702 while Present (CD1) and then Present (CD2) loop
13703 if not Same_Rep then
13704 return False;
13705 else
13706 Next_Discriminant (CD1);
13707 Next_Discriminant (CD2);
13708 end if;
13709 end loop;
13710 end if;
13711
13712 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13713 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13714 while Present (CD1) loop
13715 if not Same_Rep then
13716 return False;
13717 else
13718 Next_Component (CD1);
13719 Next_Component (CD2);
13720 end if;
13721 end loop;
13722
13723 return True;
13724 end Record_Case;
13725 end if;
13726
13727 -- For enumeration types, we must check each literal to see if the
13728 -- representation is the same. Note that we do not permit enumeration
13729 -- representation clauses for Character and Wide_Character, so these
13730 -- cases were already dealt with.
13731
13732 elsif Is_Enumeration_Type (T1) then
13733 Enumeration_Case : declare
13734 L1, L2 : Entity_Id;
13735
13736 begin
13737 L1 := First_Literal (T1);
13738 L2 := First_Literal (T2);
13739 while Present (L1) loop
13740 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13741 return False;
13742 else
13743 Next_Literal (L1);
13744 Next_Literal (L2);
13745 end if;
13746 end loop;
13747
13748 return True;
13749 end Enumeration_Case;
13750
13751 -- Any other types have the same representation for these purposes
13752
13753 else
13754 return True;
13755 end if;
13756 end Same_Representation;
13757
13758 --------------------------------
13759 -- Resolve_Iterable_Operation --
13760 --------------------------------
13761
13762 procedure Resolve_Iterable_Operation
13763 (N : Node_Id;
13764 Cursor : Entity_Id;
13765 Typ : Entity_Id;
13766 Nam : Name_Id)
13767 is
13768 Ent : Entity_Id;
13769 F1 : Entity_Id;
13770 F2 : Entity_Id;
13771
13772 begin
13773 if not Is_Overloaded (N) then
13774 if not Is_Entity_Name (N)
13775 or else Ekind (Entity (N)) /= E_Function
13776 or else Scope (Entity (N)) /= Scope (Typ)
13777 or else No (First_Formal (Entity (N)))
13778 or else Etype (First_Formal (Entity (N))) /= Typ
13779 then
13780 Error_Msg_N
13781 ("iterable primitive must be local function name whose first "
13782 & "formal is an iterable type", N);
13783 return;
13784 end if;
13785
13786 Ent := Entity (N);
13787 F1 := First_Formal (Ent);
13788
13789 if Nam = Name_First or else Nam = Name_Last then
13790
13791 -- First or Last (Container) => Cursor
13792
13793 if Etype (Ent) /= Cursor then
13794 Error_Msg_N ("primitive for First must yield a curosr", N);
13795 end if;
13796
13797 elsif Nam = Name_Next then
13798
13799 -- Next (Container, Cursor) => Cursor
13800
13801 F2 := Next_Formal (F1);
13802
13803 if Etype (F2) /= Cursor
13804 or else Etype (Ent) /= Cursor
13805 or else Present (Next_Formal (F2))
13806 then
13807 Error_Msg_N ("no match for Next iterable primitive", N);
13808 end if;
13809
13810 elsif Nam = Name_Previous then
13811
13812 -- Previous (Container, Cursor) => Cursor
13813
13814 F2 := Next_Formal (F1);
13815
13816 if Etype (F2) /= Cursor
13817 or else Etype (Ent) /= Cursor
13818 or else Present (Next_Formal (F2))
13819 then
13820 Error_Msg_N ("no match for Previous iterable primitive", N);
13821 end if;
13822
13823 elsif Nam = Name_Has_Element then
13824
13825 -- Has_Element (Container, Cursor) => Boolean
13826
13827 F2 := Next_Formal (F1);
13828
13829 if Etype (F2) /= Cursor
13830 or else Etype (Ent) /= Standard_Boolean
13831 or else Present (Next_Formal (F2))
13832 then
13833 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13834 end if;
13835
13836 elsif Nam = Name_Element then
13837 F2 := Next_Formal (F1);
13838
13839 if No (F2)
13840 or else Etype (F2) /= Cursor
13841 or else Present (Next_Formal (F2))
13842 then
13843 Error_Msg_N ("no match for Element iterable primitive", N);
13844 end if;
13845
13846 else
13847 raise Program_Error;
13848 end if;
13849
13850 else
13851 -- Overloaded case: find subprogram with proper signature. Caller
13852 -- will report error if no match is found.
13853
13854 declare
13855 I : Interp_Index;
13856 It : Interp;
13857
13858 begin
13859 Get_First_Interp (N, I, It);
13860 while Present (It.Typ) loop
13861 if Ekind (It.Nam) = E_Function
13862 and then Scope (It.Nam) = Scope (Typ)
13863 and then Etype (First_Formal (It.Nam)) = Typ
13864 then
13865 F1 := First_Formal (It.Nam);
13866
13867 if Nam = Name_First then
13868 if Etype (It.Nam) = Cursor
13869 and then No (Next_Formal (F1))
13870 then
13871 Set_Entity (N, It.Nam);
13872 exit;
13873 end if;
13874
13875 elsif Nam = Name_Next then
13876 F2 := Next_Formal (F1);
13877
13878 if Present (F2)
13879 and then No (Next_Formal (F2))
13880 and then Etype (F2) = Cursor
13881 and then Etype (It.Nam) = Cursor
13882 then
13883 Set_Entity (N, It.Nam);
13884 exit;
13885 end if;
13886
13887 elsif Nam = Name_Has_Element then
13888 F2 := Next_Formal (F1);
13889
13890 if Present (F2)
13891 and then No (Next_Formal (F2))
13892 and then Etype (F2) = Cursor
13893 and then Etype (It.Nam) = Standard_Boolean
13894 then
13895 Set_Entity (N, It.Nam);
13896 F2 := Next_Formal (F1);
13897 exit;
13898 end if;
13899
13900 elsif Nam = Name_Element then
13901 F2 := Next_Formal (F1);
13902
13903 if Present (F2)
13904 and then No (Next_Formal (F2))
13905 and then Etype (F2) = Cursor
13906 then
13907 Set_Entity (N, It.Nam);
13908 exit;
13909 end if;
13910 end if;
13911 end if;
13912
13913 Get_Next_Interp (I, It);
13914 end loop;
13915 end;
13916 end if;
13917 end Resolve_Iterable_Operation;
13918
13919 ----------------
13920 -- Set_Biased --
13921 ----------------
13922
13923 procedure Set_Biased
13924 (E : Entity_Id;
13925 N : Node_Id;
13926 Msg : String;
13927 Biased : Boolean := True)
13928 is
13929 begin
13930 if Biased then
13931 Set_Has_Biased_Representation (E);
13932
13933 if Warn_On_Biased_Representation then
13934 Error_Msg_NE
13935 ("?B?" & Msg & " forces biased representation for&", N, E);
13936 end if;
13937 end if;
13938 end Set_Biased;
13939
13940 --------------------
13941 -- Set_Enum_Esize --
13942 --------------------
13943
13944 procedure Set_Enum_Esize (T : Entity_Id) is
13945 Lo : Uint;
13946 Hi : Uint;
13947 Sz : Nat;
13948
13949 begin
13950 Init_Alignment (T);
13951
13952 -- Find the minimum standard size (8,16,32,64) that fits
13953
13954 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13955 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13956
13957 if Lo < 0 then
13958 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
13959 Sz := Standard_Character_Size; -- May be > 8 on some targets
13960
13961 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13962 Sz := 16;
13963
13964 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13965 Sz := 32;
13966
13967 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13968 Sz := 64;
13969 end if;
13970
13971 else
13972 if Hi < Uint_2**08 then
13973 Sz := Standard_Character_Size; -- May be > 8 on some targets
13974
13975 elsif Hi < Uint_2**16 then
13976 Sz := 16;
13977
13978 elsif Hi < Uint_2**32 then
13979 Sz := 32;
13980
13981 else pragma Assert (Hi < Uint_2**63);
13982 Sz := 64;
13983 end if;
13984 end if;
13985
13986 -- That minimum is the proper size unless we have a foreign convention
13987 -- and the size required is 32 or less, in which case we bump the size
13988 -- up to 32. This is required for C and C++ and seems reasonable for
13989 -- all other foreign conventions.
13990
13991 if Has_Foreign_Convention (T)
13992 and then Esize (T) < Standard_Integer_Size
13993
13994 -- Don't do this if Short_Enums on target
13995
13996 and then not Target_Short_Enums
13997 then
13998 Init_Esize (T, Standard_Integer_Size);
13999 else
14000 Init_Esize (T, Sz);
14001 end if;
14002 end Set_Enum_Esize;
14003
14004 -----------------------------
14005 -- Uninstall_Discriminants --
14006 -----------------------------
14007
14008 procedure Uninstall_Discriminants (E : Entity_Id) is
14009 Disc : Entity_Id;
14010 Prev : Entity_Id;
14011 Outer : Entity_Id;
14012
14013 begin
14014 -- Discriminants have been made visible for type declarations and
14015 -- protected type declarations, not for subtype declarations.
14016
14017 if Nkind (Parent (E)) /= N_Subtype_Declaration then
14018 Disc := First_Discriminant (E);
14019 while Present (Disc) loop
14020 if Disc /= Current_Entity (Disc) then
14021 Prev := Current_Entity (Disc);
14022 while Present (Prev)
14023 and then Present (Homonym (Prev))
14024 and then Homonym (Prev) /= Disc
14025 loop
14026 Prev := Homonym (Prev);
14027 end loop;
14028 else
14029 Prev := Empty;
14030 end if;
14031
14032 Set_Is_Immediately_Visible (Disc, False);
14033
14034 Outer := Homonym (Disc);
14035 while Present (Outer) and then Scope (Outer) = E loop
14036 Outer := Homonym (Outer);
14037 end loop;
14038
14039 -- Reset homonym link of other entities, but do not modify link
14040 -- between entities in current scope, so that the back end can
14041 -- have a proper count of local overloadings.
14042
14043 if No (Prev) then
14044 Set_Name_Entity_Id (Chars (Disc), Outer);
14045
14046 elsif Scope (Prev) /= Scope (Disc) then
14047 Set_Homonym (Prev, Outer);
14048 end if;
14049
14050 Next_Discriminant (Disc);
14051 end loop;
14052 end if;
14053 end Uninstall_Discriminants;
14054
14055 ------------------------------
14056 -- Validate_Address_Clauses --
14057 ------------------------------
14058
14059 procedure Validate_Address_Clauses is
14060 function Offset_Value (Expr : Node_Id) return Uint;
14061 -- Given an Address attribute reference, return the value in bits of its
14062 -- offset from the first bit of the underlying entity, or 0 if it is not
14063 -- known at compile time.
14064
14065 ------------------
14066 -- Offset_Value --
14067 ------------------
14068
14069 function Offset_Value (Expr : Node_Id) return Uint is
14070 N : Node_Id := Prefix (Expr);
14071 Off : Uint;
14072 Val : Uint := Uint_0;
14073
14074 begin
14075 -- Climb the prefix chain and compute the cumulative offset
14076
14077 loop
14078 if Is_Entity_Name (N) then
14079 return Val;
14080
14081 elsif Nkind (N) = N_Selected_Component then
14082 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
14083 if Off /= No_Uint and then Off >= Uint_0 then
14084 Val := Val + Off;
14085 N := Prefix (N);
14086 else
14087 return Uint_0;
14088 end if;
14089
14090 elsif Nkind (N) = N_Indexed_Component then
14091 Off := Indexed_Component_Bit_Offset (N);
14092 if Off /= No_Uint then
14093 Val := Val + Off;
14094 N := Prefix (N);
14095 else
14096 return Uint_0;
14097 end if;
14098
14099 else
14100 return Uint_0;
14101 end if;
14102 end loop;
14103 end Offset_Value;
14104
14105 -- Start of processing for Validate_Address_Clauses
14106
14107 begin
14108 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
14109 declare
14110 ACCR : Address_Clause_Check_Record
14111 renames Address_Clause_Checks.Table (J);
14112
14113 Expr : Node_Id;
14114
14115 X_Alignment : Uint;
14116 Y_Alignment : Uint := Uint_0;
14117
14118 X_Size : Uint;
14119 Y_Size : Uint := Uint_0;
14120
14121 X_Offs : Uint;
14122
14123 begin
14124 -- Skip processing of this entry if warning already posted
14125
14126 if not Address_Warning_Posted (ACCR.N) then
14127 Expr := Original_Node (Expression (ACCR.N));
14128
14129 -- Get alignments, sizes and offset, if any
14130
14131 X_Alignment := Alignment (ACCR.X);
14132 X_Size := Esize (ACCR.X);
14133
14134 if Present (ACCR.Y) then
14135 Y_Alignment := Alignment (ACCR.Y);
14136 Y_Size := Esize (ACCR.Y);
14137 end if;
14138
14139 if ACCR.Off
14140 and then Nkind (Expr) = N_Attribute_Reference
14141 and then Attribute_Name (Expr) = Name_Address
14142 then
14143 X_Offs := Offset_Value (Expr);
14144 else
14145 X_Offs := Uint_0;
14146 end if;
14147
14148 -- Check for known value not multiple of alignment
14149
14150 if No (ACCR.Y) then
14151 if not Alignment_Checks_Suppressed (ACCR)
14152 and then X_Alignment /= 0
14153 and then ACCR.A mod X_Alignment /= 0
14154 then
14155 Error_Msg_NE
14156 ("??specified address for& is inconsistent with "
14157 & "alignment", ACCR.N, ACCR.X);
14158 Error_Msg_N
14159 ("\??program execution may be erroneous (RM 13.3(27))",
14160 ACCR.N);
14161
14162 Error_Msg_Uint_1 := X_Alignment;
14163 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
14164 end if;
14165
14166 -- Check for large object overlaying smaller one
14167
14168 elsif Y_Size > Uint_0
14169 and then X_Size > Uint_0
14170 and then X_Offs + X_Size > Y_Size
14171 then
14172 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
14173 Error_Msg_N
14174 ("\??program execution may be erroneous", ACCR.N);
14175
14176 Error_Msg_Uint_1 := X_Size;
14177 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
14178
14179 Error_Msg_Uint_1 := Y_Size;
14180 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
14181
14182 if Y_Size >= X_Size then
14183 Error_Msg_Uint_1 := X_Offs;
14184 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
14185 end if;
14186
14187 -- Check for inadequate alignment, both of the base object
14188 -- and of the offset, if any. We only do this check if the
14189 -- run-time Alignment_Check is active. No point in warning
14190 -- if this check has been suppressed (or is suppressed by
14191 -- default in the non-strict alignment machine case).
14192
14193 -- Note: we do not check the alignment if we gave a size
14194 -- warning, since it would likely be redundant.
14195
14196 elsif not Alignment_Checks_Suppressed (ACCR)
14197 and then Y_Alignment /= Uint_0
14198 and then
14199 (Y_Alignment < X_Alignment
14200 or else
14201 (ACCR.Off
14202 and then Nkind (Expr) = N_Attribute_Reference
14203 and then Attribute_Name (Expr) = Name_Address
14204 and then Has_Compatible_Alignment
14205 (ACCR.X, Prefix (Expr), True) /=
14206 Known_Compatible))
14207 then
14208 Error_Msg_NE
14209 ("??specified address for& may be inconsistent with "
14210 & "alignment", ACCR.N, ACCR.X);
14211 Error_Msg_N
14212 ("\??program execution may be erroneous (RM 13.3(27))",
14213 ACCR.N);
14214
14215 Error_Msg_Uint_1 := X_Alignment;
14216 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
14217
14218 Error_Msg_Uint_1 := Y_Alignment;
14219 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
14220
14221 if Y_Alignment >= X_Alignment then
14222 Error_Msg_N
14223 ("\??but offset is not multiple of alignment", ACCR.N);
14224 end if;
14225 end if;
14226 end if;
14227 end;
14228 end loop;
14229 end Validate_Address_Clauses;
14230
14231 ---------------------------
14232 -- Validate_Independence --
14233 ---------------------------
14234
14235 procedure Validate_Independence is
14236 SU : constant Uint := UI_From_Int (System_Storage_Unit);
14237 N : Node_Id;
14238 E : Entity_Id;
14239 IC : Boolean;
14240 Comp : Entity_Id;
14241 Addr : Node_Id;
14242 P : Node_Id;
14243
14244 procedure Check_Array_Type (Atyp : Entity_Id);
14245 -- Checks if the array type Atyp has independent components, and
14246 -- if not, outputs an appropriate set of error messages.
14247
14248 procedure No_Independence;
14249 -- Output message that independence cannot be guaranteed
14250
14251 function OK_Component (C : Entity_Id) return Boolean;
14252 -- Checks one component to see if it is independently accessible, and
14253 -- if so yields True, otherwise yields False if independent access
14254 -- cannot be guaranteed. This is a conservative routine, it only
14255 -- returns True if it knows for sure, it returns False if it knows
14256 -- there is a problem, or it cannot be sure there is no problem.
14257
14258 procedure Reason_Bad_Component (C : Entity_Id);
14259 -- Outputs continuation message if a reason can be determined for
14260 -- the component C being bad.
14261
14262 ----------------------
14263 -- Check_Array_Type --
14264 ----------------------
14265
14266 procedure Check_Array_Type (Atyp : Entity_Id) is
14267 Ctyp : constant Entity_Id := Component_Type (Atyp);
14268
14269 begin
14270 -- OK if no alignment clause, no pack, and no component size
14271
14272 if not Has_Component_Size_Clause (Atyp)
14273 and then not Has_Alignment_Clause (Atyp)
14274 and then not Is_Packed (Atyp)
14275 then
14276 return;
14277 end if;
14278
14279 -- Case of component size is greater than or equal to 64 and the
14280 -- alignment of the array is at least as large as the alignment
14281 -- of the component. We are definitely OK in this situation.
14282
14283 if Known_Component_Size (Atyp)
14284 and then Component_Size (Atyp) >= 64
14285 and then Known_Alignment (Atyp)
14286 and then Known_Alignment (Ctyp)
14287 and then Alignment (Atyp) >= Alignment (Ctyp)
14288 then
14289 return;
14290 end if;
14291
14292 -- Check actual component size
14293
14294 if not Known_Component_Size (Atyp)
14295 or else not (Addressable (Component_Size (Atyp))
14296 and then Component_Size (Atyp) < 64)
14297 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
14298 then
14299 No_Independence;
14300
14301 -- Bad component size, check reason
14302
14303 if Has_Component_Size_Clause (Atyp) then
14304 P := Get_Attribute_Definition_Clause
14305 (Atyp, Attribute_Component_Size);
14306
14307 if Present (P) then
14308 Error_Msg_Sloc := Sloc (P);
14309 Error_Msg_N ("\because of Component_Size clause#", N);
14310 return;
14311 end if;
14312 end if;
14313
14314 if Is_Packed (Atyp) then
14315 P := Get_Rep_Pragma (Atyp, Name_Pack);
14316
14317 if Present (P) then
14318 Error_Msg_Sloc := Sloc (P);
14319 Error_Msg_N ("\because of pragma Pack#", N);
14320 return;
14321 end if;
14322 end if;
14323
14324 -- No reason found, just return
14325
14326 return;
14327 end if;
14328
14329 -- Array type is OK independence-wise
14330
14331 return;
14332 end Check_Array_Type;
14333
14334 ---------------------
14335 -- No_Independence --
14336 ---------------------
14337
14338 procedure No_Independence is
14339 begin
14340 if Pragma_Name (N) = Name_Independent then
14341 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
14342 else
14343 Error_Msg_NE
14344 ("independent components cannot be guaranteed for&", N, E);
14345 end if;
14346 end No_Independence;
14347
14348 ------------------
14349 -- OK_Component --
14350 ------------------
14351
14352 function OK_Component (C : Entity_Id) return Boolean is
14353 Rec : constant Entity_Id := Scope (C);
14354 Ctyp : constant Entity_Id := Etype (C);
14355
14356 begin
14357 -- OK if no component clause, no Pack, and no alignment clause
14358
14359 if No (Component_Clause (C))
14360 and then not Is_Packed (Rec)
14361 and then not Has_Alignment_Clause (Rec)
14362 then
14363 return True;
14364 end if;
14365
14366 -- Here we look at the actual component layout. A component is
14367 -- addressable if its size is a multiple of the Esize of the
14368 -- component type, and its starting position in the record has
14369 -- appropriate alignment, and the record itself has appropriate
14370 -- alignment to guarantee the component alignment.
14371
14372 -- Make sure sizes are static, always assume the worst for any
14373 -- cases where we cannot check static values.
14374
14375 if not (Known_Static_Esize (C)
14376 and then
14377 Known_Static_Esize (Ctyp))
14378 then
14379 return False;
14380 end if;
14381
14382 -- Size of component must be addressable or greater than 64 bits
14383 -- and a multiple of bytes.
14384
14385 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
14386 return False;
14387 end if;
14388
14389 -- Check size is proper multiple
14390
14391 if Esize (C) mod Esize (Ctyp) /= 0 then
14392 return False;
14393 end if;
14394
14395 -- Check alignment of component is OK
14396
14397 if not Known_Component_Bit_Offset (C)
14398 or else Component_Bit_Offset (C) < Uint_0
14399 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
14400 then
14401 return False;
14402 end if;
14403
14404 -- Check alignment of record type is OK
14405
14406 if not Known_Alignment (Rec)
14407 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
14408 then
14409 return False;
14410 end if;
14411
14412 -- All tests passed, component is addressable
14413
14414 return True;
14415 end OK_Component;
14416
14417 --------------------------
14418 -- Reason_Bad_Component --
14419 --------------------------
14420
14421 procedure Reason_Bad_Component (C : Entity_Id) is
14422 Rec : constant Entity_Id := Scope (C);
14423 Ctyp : constant Entity_Id := Etype (C);
14424
14425 begin
14426 -- If component clause present assume that's the problem
14427
14428 if Present (Component_Clause (C)) then
14429 Error_Msg_Sloc := Sloc (Component_Clause (C));
14430 Error_Msg_N ("\because of Component_Clause#", N);
14431 return;
14432 end if;
14433
14434 -- If pragma Pack clause present, assume that's the problem
14435
14436 if Is_Packed (Rec) then
14437 P := Get_Rep_Pragma (Rec, Name_Pack);
14438
14439 if Present (P) then
14440 Error_Msg_Sloc := Sloc (P);
14441 Error_Msg_N ("\because of pragma Pack#", N);
14442 return;
14443 end if;
14444 end if;
14445
14446 -- See if record has bad alignment clause
14447
14448 if Has_Alignment_Clause (Rec)
14449 and then Known_Alignment (Rec)
14450 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
14451 then
14452 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
14453
14454 if Present (P) then
14455 Error_Msg_Sloc := Sloc (P);
14456 Error_Msg_N ("\because of Alignment clause#", N);
14457 end if;
14458 end if;
14459
14460 -- Couldn't find a reason, so return without a message
14461
14462 return;
14463 end Reason_Bad_Component;
14464
14465 -- Start of processing for Validate_Independence
14466
14467 begin
14468 for J in Independence_Checks.First .. Independence_Checks.Last loop
14469 N := Independence_Checks.Table (J).N;
14470 E := Independence_Checks.Table (J).E;
14471 IC := Pragma_Name (N) = Name_Independent_Components;
14472
14473 -- Deal with component case
14474
14475 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
14476 if not OK_Component (E) then
14477 No_Independence;
14478 Reason_Bad_Component (E);
14479 goto Continue;
14480 end if;
14481 end if;
14482
14483 -- Deal with record with Independent_Components
14484
14485 if IC and then Is_Record_Type (E) then
14486 Comp := First_Component_Or_Discriminant (E);
14487 while Present (Comp) loop
14488 if not OK_Component (Comp) then
14489 No_Independence;
14490 Reason_Bad_Component (Comp);
14491 goto Continue;
14492 end if;
14493
14494 Next_Component_Or_Discriminant (Comp);
14495 end loop;
14496 end if;
14497
14498 -- Deal with address clause case
14499
14500 if Is_Object (E) then
14501 Addr := Address_Clause (E);
14502
14503 if Present (Addr) then
14504 No_Independence;
14505 Error_Msg_Sloc := Sloc (Addr);
14506 Error_Msg_N ("\because of Address clause#", N);
14507 goto Continue;
14508 end if;
14509 end if;
14510
14511 -- Deal with independent components for array type
14512
14513 if IC and then Is_Array_Type (E) then
14514 Check_Array_Type (E);
14515 end if;
14516
14517 -- Deal with independent components for array object
14518
14519 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
14520 Check_Array_Type (Etype (E));
14521 end if;
14522
14523 <<Continue>> null;
14524 end loop;
14525 end Validate_Independence;
14526
14527 ------------------------------
14528 -- Validate_Iterable_Aspect --
14529 ------------------------------
14530
14531 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
14532 Assoc : Node_Id;
14533 Expr : Node_Id;
14534
14535 Prim : Node_Id;
14536 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
14537
14538 First_Id : Entity_Id;
14539 Last_Id : Entity_Id;
14540 Next_Id : Entity_Id;
14541 Has_Element_Id : Entity_Id;
14542 Element_Id : Entity_Id;
14543
14544 begin
14545 -- If previous error aspect is unusable
14546
14547 if Cursor = Any_Type then
14548 return;
14549 end if;
14550
14551 First_Id := Empty;
14552 Last_Id := Empty;
14553 Next_Id := Empty;
14554 Has_Element_Id := Empty;
14555 Element_Id := Empty;
14556
14557 -- Each expression must resolve to a function with the proper signature
14558
14559 Assoc := First (Component_Associations (Expression (ASN)));
14560 while Present (Assoc) loop
14561 Expr := Expression (Assoc);
14562 Analyze (Expr);
14563
14564 Prim := First (Choices (Assoc));
14565
14566 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
14567 Error_Msg_N ("illegal name in association", Prim);
14568
14569 elsif Chars (Prim) = Name_First then
14570 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
14571 First_Id := Entity (Expr);
14572
14573 elsif Chars (Prim) = Name_Last then
14574 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
14575 Last_Id := Entity (Expr);
14576
14577 elsif Chars (Prim) = Name_Previous then
14578 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
14579 Last_Id := Entity (Expr);
14580
14581 elsif Chars (Prim) = Name_Next then
14582 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
14583 Next_Id := Entity (Expr);
14584
14585 elsif Chars (Prim) = Name_Has_Element then
14586 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
14587 Has_Element_Id := Entity (Expr);
14588
14589 elsif Chars (Prim) = Name_Element then
14590 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
14591 Element_Id := Entity (Expr);
14592
14593 else
14594 Error_Msg_N ("invalid name for iterable function", Prim);
14595 end if;
14596
14597 Next (Assoc);
14598 end loop;
14599
14600 if No (First_Id) then
14601 Error_Msg_N ("match for First primitive not found", ASN);
14602
14603 elsif No (Next_Id) then
14604 Error_Msg_N ("match for Next primitive not found", ASN);
14605
14606 elsif No (Has_Element_Id) then
14607 Error_Msg_N ("match for Has_Element primitive not found", ASN);
14608
14609 elsif No (Element_Id) or else No (Last_Id) then
14610 null; -- optional
14611 end if;
14612 end Validate_Iterable_Aspect;
14613
14614 -----------------------------------
14615 -- Validate_Unchecked_Conversion --
14616 -----------------------------------
14617
14618 procedure Validate_Unchecked_Conversion
14619 (N : Node_Id;
14620 Act_Unit : Entity_Id)
14621 is
14622 Source : Entity_Id;
14623 Target : Entity_Id;
14624 Vnode : Node_Id;
14625
14626 begin
14627 -- Obtain source and target types. Note that we call Ancestor_Subtype
14628 -- here because the processing for generic instantiation always makes
14629 -- subtypes, and we want the original frozen actual types.
14630
14631 -- If we are dealing with private types, then do the check on their
14632 -- fully declared counterparts if the full declarations have been
14633 -- encountered (they don't have to be visible, but they must exist).
14634
14635 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
14636
14637 if Is_Private_Type (Source)
14638 and then Present (Underlying_Type (Source))
14639 then
14640 Source := Underlying_Type (Source);
14641 end if;
14642
14643 Target := Ancestor_Subtype (Etype (Act_Unit));
14644
14645 -- If either type is generic, the instantiation happens within a generic
14646 -- unit, and there is nothing to check. The proper check will happen
14647 -- when the enclosing generic is instantiated.
14648
14649 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
14650 return;
14651 end if;
14652
14653 if Is_Private_Type (Target)
14654 and then Present (Underlying_Type (Target))
14655 then
14656 Target := Underlying_Type (Target);
14657 end if;
14658
14659 -- Source may be unconstrained array, but not target, except in relaxed
14660 -- semantics mode.
14661
14662 if Is_Array_Type (Target)
14663 and then not Is_Constrained (Target)
14664 and then not Relaxed_RM_Semantics
14665 then
14666 Error_Msg_N
14667 ("unchecked conversion to unconstrained array not allowed", N);
14668 return;
14669 end if;
14670
14671 -- Warn if conversion between two different convention pointers
14672
14673 if Is_Access_Type (Target)
14674 and then Is_Access_Type (Source)
14675 and then Convention (Target) /= Convention (Source)
14676 and then Warn_On_Unchecked_Conversion
14677 then
14678 -- Give warnings for subprogram pointers only on most targets
14679
14680 if Is_Access_Subprogram_Type (Target)
14681 or else Is_Access_Subprogram_Type (Source)
14682 then
14683 Error_Msg_N
14684 ("?z?conversion between pointers with different conventions!",
14685 N);
14686 end if;
14687 end if;
14688
14689 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
14690 -- warning when compiling GNAT-related sources.
14691
14692 if Warn_On_Unchecked_Conversion
14693 and then not In_Predefined_Unit (N)
14694 and then RTU_Loaded (Ada_Calendar)
14695 and then (Chars (Source) = Name_Time
14696 or else
14697 Chars (Target) = Name_Time)
14698 then
14699 -- If Ada.Calendar is loaded and the name of one of the operands is
14700 -- Time, there is a good chance that this is Ada.Calendar.Time.
14701
14702 declare
14703 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
14704 begin
14705 pragma Assert (Present (Calendar_Time));
14706
14707 if Source = Calendar_Time or else Target = Calendar_Time then
14708 Error_Msg_N
14709 ("?z?representation of 'Time values may change between "
14710 & "'G'N'A'T versions", N);
14711 end if;
14712 end;
14713 end if;
14714
14715 -- Make entry in unchecked conversion table for later processing by
14716 -- Validate_Unchecked_Conversions, which will check sizes and alignments
14717 -- (using values set by the back end where possible). This is only done
14718 -- if the appropriate warning is active.
14719
14720 if Warn_On_Unchecked_Conversion then
14721 Unchecked_Conversions.Append
14722 (New_Val => UC_Entry'(Eloc => Sloc (N),
14723 Source => Source,
14724 Target => Target,
14725 Act_Unit => Act_Unit));
14726
14727 -- If both sizes are known statically now, then back-end annotation
14728 -- is not required to do a proper check but if either size is not
14729 -- known statically, then we need the annotation.
14730
14731 if Known_Static_RM_Size (Source)
14732 and then
14733 Known_Static_RM_Size (Target)
14734 then
14735 null;
14736 else
14737 Back_Annotate_Rep_Info := True;
14738 end if;
14739 end if;
14740
14741 -- If unchecked conversion to access type, and access type is declared
14742 -- in the same unit as the unchecked conversion, then set the flag
14743 -- No_Strict_Aliasing (no strict aliasing is implicit here)
14744
14745 if Is_Access_Type (Target) and then
14746 In_Same_Source_Unit (Target, N)
14747 then
14748 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14749 end if;
14750
14751 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14752 -- the back end needs to perform special validation checks.
14753
14754 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14755 -- have full expansion and the back end is called ???
14756
14757 Vnode :=
14758 Make_Validate_Unchecked_Conversion (Sloc (N));
14759 Set_Source_Type (Vnode, Source);
14760 Set_Target_Type (Vnode, Target);
14761
14762 -- If the unchecked conversion node is in a list, just insert before it.
14763 -- If not we have some strange case, not worth bothering about.
14764
14765 if Is_List_Member (N) then
14766 Insert_After (N, Vnode);
14767 end if;
14768 end Validate_Unchecked_Conversion;
14769
14770 ------------------------------------
14771 -- Validate_Unchecked_Conversions --
14772 ------------------------------------
14773
14774 procedure Validate_Unchecked_Conversions is
14775 function Is_Null_Array (T : Entity_Id) return Boolean;
14776 -- We want to warn in the case of converting to a wrong-sized array of
14777 -- bytes, including the zero-size case. This returns True in that case,
14778 -- which is necessary because a size of 0 is used to indicate both an
14779 -- unknown size and a size of 0. It's OK for this to return True in
14780 -- other zero-size cases, but we don't go out of our way; for example,
14781 -- we don't bother with multidimensional arrays.
14782
14783 function Is_Null_Array (T : Entity_Id) return Boolean is
14784 begin
14785 if Is_Array_Type (T) and then Is_Constrained (T) then
14786 declare
14787 Index : constant Node_Id := First_Index (T);
14788 R : Node_Id; -- N_Range
14789 begin
14790 case Nkind (Index) is
14791 when N_Range =>
14792 R := Index;
14793 when N_Subtype_Indication =>
14794 R := Range_Expression (Constraint (Index));
14795 when N_Identifier | N_Expanded_Name =>
14796 R := Scalar_Range (Entity (Index));
14797 when others =>
14798 raise Program_Error;
14799 end case;
14800
14801 return Is_Null_Range (Low_Bound (R), High_Bound (R));
14802 end;
14803 end if;
14804
14805 return False;
14806 end Is_Null_Array;
14807
14808 begin
14809 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14810 declare
14811 T : UC_Entry renames Unchecked_Conversions.Table (N);
14812
14813 Act_Unit : constant Entity_Id := T.Act_Unit;
14814 Eloc : constant Source_Ptr := T.Eloc;
14815 Source : constant Entity_Id := T.Source;
14816 Target : constant Entity_Id := T.Target;
14817
14818 Source_Siz : Uint;
14819 Target_Siz : Uint;
14820
14821 begin
14822 -- Skip if function marked as warnings off
14823
14824 if Warnings_Off (Act_Unit) or else Serious_Errors_Detected > 0 then
14825 goto Continue;
14826 end if;
14827
14828 -- Don't do the check if warnings off for either type, note the
14829 -- deliberate use of OR here instead of OR ELSE to get the flag
14830 -- Warnings_Off_Used set for both types if appropriate.
14831
14832 if Has_Warnings_Off (Source) or Has_Warnings_Off (Target) then
14833 goto Continue;
14834 end if;
14835
14836 if (Known_Static_RM_Size (Source)
14837 and then Known_Static_RM_Size (Target))
14838 or else Is_Null_Array (Target)
14839 then
14840 -- This validation check, which warns if we have unequal sizes
14841 -- for unchecked conversion, and thus implementation dependent
14842 -- semantics, is one of the few occasions on which we use the
14843 -- official RM size instead of Esize. See description in Einfo
14844 -- "Handling of Type'Size Values" for details.
14845
14846 Source_Siz := RM_Size (Source);
14847 Target_Siz := RM_Size (Target);
14848
14849 if Source_Siz /= Target_Siz then
14850 Error_Msg
14851 ("?z?types for unchecked conversion have different sizes!",
14852 Eloc, Act_Unit);
14853
14854 if All_Errors_Mode then
14855 Error_Msg_Name_1 := Chars (Source);
14856 Error_Msg_Uint_1 := Source_Siz;
14857 Error_Msg_Name_2 := Chars (Target);
14858 Error_Msg_Uint_2 := Target_Siz;
14859 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
14860
14861 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14862
14863 if Is_Discrete_Type (Source)
14864 and then
14865 Is_Discrete_Type (Target)
14866 then
14867 if Source_Siz > Target_Siz then
14868 Error_Msg
14869 ("\?z?^ high order bits of source will "
14870 & "be ignored!", Eloc);
14871
14872 elsif Is_Unsigned_Type (Source) then
14873 Error_Msg
14874 ("\?z?source will be extended with ^ high order "
14875 & "zero bits!", Eloc);
14876
14877 else
14878 Error_Msg
14879 ("\?z?source will be extended with ^ high order "
14880 & "sign bits!", Eloc);
14881 end if;
14882
14883 elsif Source_Siz < Target_Siz then
14884 if Is_Discrete_Type (Target) then
14885 if Bytes_Big_Endian then
14886 Error_Msg
14887 ("\?z?target value will include ^ undefined "
14888 & "low order bits!", Eloc, Act_Unit);
14889 else
14890 Error_Msg
14891 ("\?z?target value will include ^ undefined "
14892 & "high order bits!", Eloc, Act_Unit);
14893 end if;
14894
14895 else
14896 Error_Msg
14897 ("\?z?^ trailing bits of target value will be "
14898 & "undefined!", Eloc, Act_Unit);
14899 end if;
14900
14901 else pragma Assert (Source_Siz > Target_Siz);
14902 if Is_Discrete_Type (Source) then
14903 if Bytes_Big_Endian then
14904 Error_Msg
14905 ("\?z?^ low order bits of source will be "
14906 & "ignored!", Eloc, Act_Unit);
14907 else
14908 Error_Msg
14909 ("\?z?^ high order bits of source will be "
14910 & "ignored!", Eloc, Act_Unit);
14911 end if;
14912
14913 else
14914 Error_Msg
14915 ("\?z?^ trailing bits of source will be "
14916 & "ignored!", Eloc, Act_Unit);
14917 end if;
14918 end if;
14919 end if;
14920 end if;
14921 end if;
14922
14923 -- If both types are access types, we need to check the alignment.
14924 -- If the alignment of both is specified, we can do it here.
14925
14926 if Serious_Errors_Detected = 0
14927 and then Is_Access_Type (Source)
14928 and then Is_Access_Type (Target)
14929 and then Target_Strict_Alignment
14930 and then Present (Designated_Type (Source))
14931 and then Present (Designated_Type (Target))
14932 then
14933 declare
14934 D_Source : constant Entity_Id := Designated_Type (Source);
14935 D_Target : constant Entity_Id := Designated_Type (Target);
14936
14937 begin
14938 if Known_Alignment (D_Source)
14939 and then
14940 Known_Alignment (D_Target)
14941 then
14942 declare
14943 Source_Align : constant Uint := Alignment (D_Source);
14944 Target_Align : constant Uint := Alignment (D_Target);
14945
14946 begin
14947 if Source_Align < Target_Align
14948 and then not Is_Tagged_Type (D_Source)
14949
14950 -- Suppress warning if warnings suppressed on either
14951 -- type or either designated type. Note the use of
14952 -- OR here instead of OR ELSE. That is intentional,
14953 -- we would like to set flag Warnings_Off_Used in
14954 -- all types for which warnings are suppressed.
14955
14956 and then not (Has_Warnings_Off (D_Source)
14957 or
14958 Has_Warnings_Off (D_Target)
14959 or
14960 Has_Warnings_Off (Source)
14961 or
14962 Has_Warnings_Off (Target))
14963 then
14964 Error_Msg_Uint_1 := Target_Align;
14965 Error_Msg_Uint_2 := Source_Align;
14966 Error_Msg_Node_1 := D_Target;
14967 Error_Msg_Node_2 := D_Source;
14968 Error_Msg
14969 ("?z?alignment of & (^) is stricter than "
14970 & "alignment of & (^)!", Eloc, Act_Unit);
14971 Error_Msg
14972 ("\?z?resulting access value may have invalid "
14973 & "alignment!", Eloc, Act_Unit);
14974 end if;
14975 end;
14976 end if;
14977 end;
14978 end if;
14979 end;
14980
14981 <<Continue>>
14982 null;
14983 end loop;
14984 end Validate_Unchecked_Conversions;
14985
14986 end Sem_Ch13;