dbfc215378d6fd71bfffe1f10c0be26767c6b4ff
[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-2013, 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 Lib; use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch9; use Sem_Ch9;
52 with Sem_Dim; use Sem_Dim;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Eval; use Sem_Eval;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Type; use Sem_Type;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Sinput; use Sinput;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Ttypes; use Ttypes;
67 with Tbuild; use Tbuild;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
70
71 with GNAT.Heap_Sort_G;
72
73 package body Sem_Ch13 is
74
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
77
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
81
82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
87
88 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
89 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
90 -- then either there are pragma Predicate entries on the rep chain for the
91 -- type (note that Predicate aspects are converted to pragma Predicate), or
92 -- there are inherited aspects from a parent type, or ancestor subtypes.
93 -- This procedure builds the spec and body for the Predicate function that
94 -- tests these predicates. N is the freeze node for the type. The spec of
95 -- the function is inserted before the freeze node, and the body of the
96 -- function is inserted after the freeze node. If the predicate expression
97 -- has at least one Raise_Expression, then this procedure also builds the
98 -- M version of the predicate function for use in membership tests.
99
100 procedure Build_Static_Predicate
101 (Typ : Entity_Id;
102 Expr : Node_Id;
103 Nam : Name_Id);
104 -- Given a predicated type Typ, where Typ is a discrete static subtype,
105 -- whose predicate expression is Expr, tests if Expr is a static predicate,
106 -- and if so, builds the predicate range list. Nam is the name of the one
107 -- argument to the predicate function. Occurrences of the type name in the
108 -- predicate expression have been replaced by identifier references to this
109 -- name, which is unique, so any identifier with Chars matching Nam must be
110 -- a reference to the type. If the predicate is non-static, this procedure
111 -- returns doing nothing. If the predicate is static, then the predicate
112 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
113 -- a canonicalized membership operation.
114
115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
118
119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
125
126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
130
131 function Is_Operational_Item (N : Node_Id) return Boolean;
132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
135 -- attributes.
136
137 procedure New_Stream_Subprogram
138 (N : Node_Id;
139 Ent : Entity_Id;
140 Subp : Entity_Id;
141 Nam : TSS_Name_Type);
142 -- Create a subprogram renaming of a given stream attribute to the
143 -- designated subprogram and then in the tagged case, provide this as a
144 -- primitive operation, or in the non-tagged case make an appropriate TSS
145 -- entry. This is more properly an expansion activity than just semantics,
146 -- but the presence of user-defined stream functions for limited types is a
147 -- legality check, which is why this takes place here rather than in
148 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
149 -- function to be generated.
150 --
151 -- To avoid elaboration anomalies with freeze nodes, for untagged types
152 -- we generate both a subprogram declaration and a subprogram renaming
153 -- declaration, so that the attribute specification is handled as a
154 -- renaming_as_body. For tagged types, the specification is one of the
155 -- primitive specs.
156
157 generic
158 with procedure Replace_Type_Reference (N : Node_Id);
159 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
160 -- This is used to scan an expression for a predicate or invariant aspect
161 -- replacing occurrences of the name TName (the name of the subtype to
162 -- which the aspect applies) with appropriate references to the parameter
163 -- of the predicate function or invariant procedure. The procedure passed
164 -- as a generic parameter does the actual replacement of node N, which is
165 -- either a simple direct reference to TName, or a selected component that
166 -- represents an appropriately qualified occurrence of TName.
167
168 procedure Set_Biased
169 (E : Entity_Id;
170 N : Node_Id;
171 Msg : String;
172 Biased : Boolean := True);
173 -- If Biased is True, sets Has_Biased_Representation flag for E, and
174 -- outputs a warning message at node N if Warn_On_Biased_Representation is
175 -- is True. This warning inserts the string Msg to describe the construct
176 -- causing biasing.
177
178 ----------------------------------------------
179 -- Table for Validate_Unchecked_Conversions --
180 ----------------------------------------------
181
182 -- The following table collects unchecked conversions for validation.
183 -- Entries are made by Validate_Unchecked_Conversion and then the call
184 -- to Validate_Unchecked_Conversions does the actual error checking and
185 -- posting of warnings. The reason for this delayed processing is to take
186 -- advantage of back-annotations of size and alignment values performed by
187 -- the back end.
188
189 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
190 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
191 -- already have modified all Sloc values if the -gnatD option is set.
192
193 type UC_Entry is record
194 Eloc : Source_Ptr; -- node used for posting warnings
195 Source : Entity_Id; -- source type for unchecked conversion
196 Target : Entity_Id; -- target type for unchecked conversion
197 end record;
198
199 package Unchecked_Conversions is new Table.Table (
200 Table_Component_Type => UC_Entry,
201 Table_Index_Type => Int,
202 Table_Low_Bound => 1,
203 Table_Initial => 50,
204 Table_Increment => 200,
205 Table_Name => "Unchecked_Conversions");
206
207 ----------------------------------------
208 -- Table for Validate_Address_Clauses --
209 ----------------------------------------
210
211 -- If an address clause has the form
212
213 -- for X'Address use Expr
214
215 -- where Expr is of the form Y'Address or recursively is a reference to a
216 -- constant of either of these forms, and X and Y are entities of objects,
217 -- then if Y has a smaller alignment than X, that merits a warning about
218 -- possible bad alignment. The following table collects address clauses of
219 -- this kind. We put these in a table so that they can be checked after the
220 -- back end has completed annotation of the alignments of objects, since we
221 -- can catch more cases that way.
222
223 type Address_Clause_Check_Record is record
224 N : Node_Id;
225 -- The address clause
226
227 X : Entity_Id;
228 -- The entity of the object overlaying Y
229
230 Y : Entity_Id;
231 -- The entity of the object being overlaid
232
233 Off : Boolean;
234 -- Whether the address is offset within Y
235 end record;
236
237 package Address_Clause_Checks is new Table.Table (
238 Table_Component_Type => Address_Clause_Check_Record,
239 Table_Index_Type => Int,
240 Table_Low_Bound => 1,
241 Table_Initial => 20,
242 Table_Increment => 200,
243 Table_Name => "Address_Clause_Checks");
244
245 -----------------------------------------
246 -- Adjust_Record_For_Reverse_Bit_Order --
247 -----------------------------------------
248
249 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
250 Comp : Node_Id;
251 CC : Node_Id;
252
253 begin
254 -- Processing depends on version of Ada
255
256 -- For Ada 95, we just renumber bits within a storage unit. We do the
257 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
258 -- Ada 83, and are free to add this extension.
259
260 if Ada_Version < Ada_2005 then
261 Comp := First_Component_Or_Discriminant (R);
262 while Present (Comp) loop
263 CC := Component_Clause (Comp);
264
265 -- If component clause is present, then deal with the non-default
266 -- bit order case for Ada 95 mode.
267
268 -- We only do this processing for the base type, and in fact that
269 -- is important, since otherwise if there are record subtypes, we
270 -- could reverse the bits once for each subtype, which is wrong.
271
272 if Present (CC) and then Ekind (R) = E_Record_Type then
273 declare
274 CFB : constant Uint := Component_Bit_Offset (Comp);
275 CSZ : constant Uint := Esize (Comp);
276 CLC : constant Node_Id := Component_Clause (Comp);
277 Pos : constant Node_Id := Position (CLC);
278 FB : constant Node_Id := First_Bit (CLC);
279
280 Storage_Unit_Offset : constant Uint :=
281 CFB / System_Storage_Unit;
282
283 Start_Bit : constant Uint :=
284 CFB mod System_Storage_Unit;
285
286 begin
287 -- Cases where field goes over storage unit boundary
288
289 if Start_Bit + CSZ > System_Storage_Unit then
290
291 -- Allow multi-byte field but generate warning
292
293 if Start_Bit mod System_Storage_Unit = 0
294 and then CSZ mod System_Storage_Unit = 0
295 then
296 Error_Msg_N
297 ("multi-byte field specified with non-standard"
298 & " Bit_Order??", CLC);
299
300 if Bytes_Big_Endian then
301 Error_Msg_N
302 ("bytes are not reversed "
303 & "(component is big-endian)??", CLC);
304 else
305 Error_Msg_N
306 ("bytes are not reversed "
307 & "(component is little-endian)??", CLC);
308 end if;
309
310 -- Do not allow non-contiguous field
311
312 else
313 Error_Msg_N
314 ("attempt to specify non-contiguous field "
315 & "not permitted", CLC);
316 Error_Msg_N
317 ("\caused by non-standard Bit_Order "
318 & "specified", CLC);
319 Error_Msg_N
320 ("\consider possibility of using "
321 & "Ada 2005 mode here", CLC);
322 end if;
323
324 -- Case where field fits in one storage unit
325
326 else
327 -- Give warning if suspicious component clause
328
329 if Intval (FB) >= System_Storage_Unit
330 and then Warn_On_Reverse_Bit_Order
331 then
332 Error_Msg_N
333 ("Bit_Order clause does not affect " &
334 "byte ordering?V?", Pos);
335 Error_Msg_Uint_1 :=
336 Intval (Pos) + Intval (FB) /
337 System_Storage_Unit;
338 Error_Msg_N
339 ("position normalized to ^ before bit " &
340 "order interpreted?V?", Pos);
341 end if;
342
343 -- Here is where we fix up the Component_Bit_Offset value
344 -- to account for the reverse bit order. Some examples of
345 -- what needs to be done are:
346
347 -- First_Bit .. Last_Bit Component_Bit_Offset
348 -- old new old new
349
350 -- 0 .. 0 7 .. 7 0 7
351 -- 0 .. 1 6 .. 7 0 6
352 -- 0 .. 2 5 .. 7 0 5
353 -- 0 .. 7 0 .. 7 0 4
354
355 -- 1 .. 1 6 .. 6 1 6
356 -- 1 .. 4 3 .. 6 1 3
357 -- 4 .. 7 0 .. 3 4 0
358
359 -- The rule is that the first bit is is obtained by
360 -- subtracting the old ending bit from storage_unit - 1.
361
362 Set_Component_Bit_Offset
363 (Comp,
364 (Storage_Unit_Offset * System_Storage_Unit) +
365 (System_Storage_Unit - 1) -
366 (Start_Bit + CSZ - 1));
367
368 Set_Normalized_First_Bit
369 (Comp,
370 Component_Bit_Offset (Comp) mod
371 System_Storage_Unit);
372 end if;
373 end;
374 end if;
375
376 Next_Component_Or_Discriminant (Comp);
377 end loop;
378
379 -- For Ada 2005, we do machine scalar processing, as fully described In
380 -- AI-133. This involves gathering all components which start at the
381 -- same byte offset and processing them together. Same approach is still
382 -- valid in later versions including Ada 2012.
383
384 else
385 declare
386 Max_Machine_Scalar_Size : constant Uint :=
387 UI_From_Int
388 (Standard_Long_Long_Integer_Size);
389 -- We use this as the maximum machine scalar size
390
391 Num_CC : Natural;
392 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
393
394 begin
395 -- This first loop through components does two things. First it
396 -- deals with the case of components with component clauses whose
397 -- length is greater than the maximum machine scalar size (either
398 -- accepting them or rejecting as needed). Second, it counts the
399 -- number of components with component clauses whose length does
400 -- not exceed this maximum for later processing.
401
402 Num_CC := 0;
403 Comp := First_Component_Or_Discriminant (R);
404 while Present (Comp) loop
405 CC := Component_Clause (Comp);
406
407 if Present (CC) then
408 declare
409 Fbit : constant Uint := Static_Integer (First_Bit (CC));
410 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
411
412 begin
413 -- Case of component with last bit >= max machine scalar
414
415 if Lbit >= Max_Machine_Scalar_Size then
416
417 -- This is allowed only if first bit is zero, and
418 -- last bit + 1 is a multiple of storage unit size.
419
420 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
421
422 -- This is the case to give a warning if enabled
423
424 if Warn_On_Reverse_Bit_Order then
425 Error_Msg_N
426 ("multi-byte field specified with "
427 & " non-standard Bit_Order?V?", CC);
428
429 if Bytes_Big_Endian then
430 Error_Msg_N
431 ("\bytes are not reversed "
432 & "(component is big-endian)?V?", CC);
433 else
434 Error_Msg_N
435 ("\bytes are not reversed "
436 & "(component is little-endian)?V?", CC);
437 end if;
438 end if;
439
440 -- Give error message for RM 13.5.1(10) violation
441
442 else
443 Error_Msg_FE
444 ("machine scalar rules not followed for&",
445 First_Bit (CC), Comp);
446
447 Error_Msg_Uint_1 := Lbit;
448 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
449 Error_Msg_F
450 ("\last bit (^) exceeds maximum machine "
451 & "scalar size (^)",
452 First_Bit (CC));
453
454 if (Lbit + 1) mod SSU /= 0 then
455 Error_Msg_Uint_1 := SSU;
456 Error_Msg_F
457 ("\and is not a multiple of Storage_Unit (^) "
458 & "(RM 13.4.1(10))",
459 First_Bit (CC));
460
461 else
462 Error_Msg_Uint_1 := Fbit;
463 Error_Msg_F
464 ("\and first bit (^) is non-zero "
465 & "(RM 13.4.1(10))",
466 First_Bit (CC));
467 end if;
468 end if;
469
470 -- OK case of machine scalar related component clause,
471 -- For now, just count them.
472
473 else
474 Num_CC := Num_CC + 1;
475 end if;
476 end;
477 end if;
478
479 Next_Component_Or_Discriminant (Comp);
480 end loop;
481
482 -- We need to sort the component clauses on the basis of the
483 -- Position values in the clause, so we can group clauses with
484 -- the same Position. together to determine the relevant machine
485 -- scalar size.
486
487 Sort_CC : declare
488 Comps : array (0 .. Num_CC) of Entity_Id;
489 -- Array to collect component and discriminant entities. The
490 -- data starts at index 1, the 0'th entry is for the sort
491 -- routine.
492
493 function CP_Lt (Op1, Op2 : Natural) return Boolean;
494 -- Compare routine for Sort
495
496 procedure CP_Move (From : Natural; To : Natural);
497 -- Move routine for Sort
498
499 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
500
501 Start : Natural;
502 Stop : Natural;
503 -- Start and stop positions in the component list of the set of
504 -- components with the same starting position (that constitute
505 -- components in a single machine scalar).
506
507 MaxL : Uint;
508 -- Maximum last bit value of any component in this set
509
510 MSS : Uint;
511 -- Corresponding machine scalar size
512
513 -----------
514 -- CP_Lt --
515 -----------
516
517 function CP_Lt (Op1, Op2 : Natural) return Boolean is
518 begin
519 return Position (Component_Clause (Comps (Op1))) <
520 Position (Component_Clause (Comps (Op2)));
521 end CP_Lt;
522
523 -------------
524 -- CP_Move --
525 -------------
526
527 procedure CP_Move (From : Natural; To : Natural) is
528 begin
529 Comps (To) := Comps (From);
530 end CP_Move;
531
532 -- Start of processing for Sort_CC
533
534 begin
535 -- Collect the machine scalar relevant component clauses
536
537 Num_CC := 0;
538 Comp := First_Component_Or_Discriminant (R);
539 while Present (Comp) loop
540 declare
541 CC : constant Node_Id := Component_Clause (Comp);
542
543 begin
544 -- Collect only component clauses whose last bit is less
545 -- than machine scalar size. Any component clause whose
546 -- last bit exceeds this value does not take part in
547 -- machine scalar layout considerations. The test for
548 -- Error_Posted makes sure we exclude component clauses
549 -- for which we already posted an error.
550
551 if Present (CC)
552 and then not Error_Posted (Last_Bit (CC))
553 and then Static_Integer (Last_Bit (CC)) <
554 Max_Machine_Scalar_Size
555 then
556 Num_CC := Num_CC + 1;
557 Comps (Num_CC) := Comp;
558 end if;
559 end;
560
561 Next_Component_Or_Discriminant (Comp);
562 end loop;
563
564 -- Sort by ascending position number
565
566 Sorting.Sort (Num_CC);
567
568 -- We now have all the components whose size does not exceed
569 -- the max machine scalar value, sorted by starting position.
570 -- In this loop we gather groups of clauses starting at the
571 -- same position, to process them in accordance with AI-133.
572
573 Stop := 0;
574 while Stop < Num_CC loop
575 Start := Stop + 1;
576 Stop := Start;
577 MaxL :=
578 Static_Integer
579 (Last_Bit (Component_Clause (Comps (Start))));
580 while Stop < Num_CC loop
581 if Static_Integer
582 (Position (Component_Clause (Comps (Stop + 1)))) =
583 Static_Integer
584 (Position (Component_Clause (Comps (Stop))))
585 then
586 Stop := Stop + 1;
587 MaxL :=
588 UI_Max
589 (MaxL,
590 Static_Integer
591 (Last_Bit
592 (Component_Clause (Comps (Stop)))));
593 else
594 exit;
595 end if;
596 end loop;
597
598 -- Now we have a group of component clauses from Start to
599 -- Stop whose positions are identical, and MaxL is the
600 -- maximum last bit value of any of these components.
601
602 -- We need to determine the corresponding machine scalar
603 -- size. This loop assumes that machine scalar sizes are
604 -- even, and that each possible machine scalar has twice
605 -- as many bits as the next smaller one.
606
607 MSS := Max_Machine_Scalar_Size;
608 while MSS mod 2 = 0
609 and then (MSS / 2) >= SSU
610 and then (MSS / 2) > MaxL
611 loop
612 MSS := MSS / 2;
613 end loop;
614
615 -- Here is where we fix up the Component_Bit_Offset value
616 -- to account for the reverse bit order. Some examples of
617 -- what needs to be done for the case of a machine scalar
618 -- size of 8 are:
619
620 -- First_Bit .. Last_Bit Component_Bit_Offset
621 -- old new old new
622
623 -- 0 .. 0 7 .. 7 0 7
624 -- 0 .. 1 6 .. 7 0 6
625 -- 0 .. 2 5 .. 7 0 5
626 -- 0 .. 7 0 .. 7 0 4
627
628 -- 1 .. 1 6 .. 6 1 6
629 -- 1 .. 4 3 .. 6 1 3
630 -- 4 .. 7 0 .. 3 4 0
631
632 -- The rule is that the first bit is obtained by subtracting
633 -- the old ending bit from machine scalar size - 1.
634
635 for C in Start .. Stop loop
636 declare
637 Comp : constant Entity_Id := Comps (C);
638 CC : constant Node_Id := Component_Clause (Comp);
639
640 LB : constant Uint := Static_Integer (Last_Bit (CC));
641 NFB : constant Uint := MSS - Uint_1 - LB;
642 NLB : constant Uint := NFB + Esize (Comp) - 1;
643 Pos : constant Uint := Static_Integer (Position (CC));
644
645 begin
646 if Warn_On_Reverse_Bit_Order then
647 Error_Msg_Uint_1 := MSS;
648 Error_Msg_N
649 ("info: reverse bit order in machine " &
650 "scalar of length^?V?", First_Bit (CC));
651 Error_Msg_Uint_1 := NFB;
652 Error_Msg_Uint_2 := NLB;
653
654 if Bytes_Big_Endian then
655 Error_Msg_NE
656 ("\info: big-endian range for "
657 & "component & is ^ .. ^?V?",
658 First_Bit (CC), Comp);
659 else
660 Error_Msg_NE
661 ("\info: little-endian range "
662 & "for component & is ^ .. ^?V?",
663 First_Bit (CC), Comp);
664 end if;
665 end if;
666
667 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
668 Set_Normalized_First_Bit (Comp, NFB mod SSU);
669 end;
670 end loop;
671 end loop;
672 end Sort_CC;
673 end;
674 end if;
675 end Adjust_Record_For_Reverse_Bit_Order;
676
677 -------------------------------------
678 -- Alignment_Check_For_Size_Change --
679 -------------------------------------
680
681 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
682 begin
683 -- If the alignment is known, and not set by a rep clause, and is
684 -- inconsistent with the size being set, then reset it to unknown,
685 -- we assume in this case that the size overrides the inherited
686 -- alignment, and that the alignment must be recomputed.
687
688 if Known_Alignment (Typ)
689 and then not Has_Alignment_Clause (Typ)
690 and then Size mod (Alignment (Typ) * SSU) /= 0
691 then
692 Init_Alignment (Typ);
693 end if;
694 end Alignment_Check_For_Size_Change;
695
696 -------------------------------------
697 -- Analyze_Aspects_At_Freeze_Point --
698 -------------------------------------
699
700 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
701 ASN : Node_Id;
702 A_Id : Aspect_Id;
703 Ritem : Node_Id;
704
705 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
706 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
707 -- the aspect specification node ASN.
708
709 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
710 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
711 -- a derived type can inherit aspects from its parent which have been
712 -- specified at the time of the derivation using an aspect, as in:
713 --
714 -- type A is range 1 .. 10
715 -- with Size => Not_Defined_Yet;
716 -- ..
717 -- type B is new A;
718 -- ..
719 -- Not_Defined_Yet : constant := 64;
720 --
721 -- In this example, the Size of A is considered to be specified prior
722 -- to the derivation, and thus inherited, even though the value is not
723 -- known at the time of derivation. To deal with this, we use two entity
724 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
725 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
726 -- the derived type (B here). If this flag is set when the derived type
727 -- is frozen, then this procedure is called to ensure proper inheritance
728 -- of all delayed aspects from the parent type. The derived type is E,
729 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
730 -- aspect specification node in the Rep_Item chain for the parent type.
731
732 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
733 -- Given an aspect specification node ASN whose expression is an
734 -- optional Boolean, this routines creates the corresponding pragma
735 -- at the freezing point.
736
737 ----------------------------------
738 -- Analyze_Aspect_Default_Value --
739 ----------------------------------
740
741 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
742 Ent : constant Entity_Id := Entity (ASN);
743 Expr : constant Node_Id := Expression (ASN);
744 Id : constant Node_Id := Identifier (ASN);
745
746 begin
747 Error_Msg_Name_1 := Chars (Id);
748
749 if not Is_Type (Ent) then
750 Error_Msg_N ("aspect% can only apply to a type", Id);
751 return;
752
753 elsif not Is_First_Subtype (Ent) then
754 Error_Msg_N ("aspect% cannot apply to subtype", Id);
755 return;
756
757 elsif A_Id = Aspect_Default_Value
758 and then not Is_Scalar_Type (Ent)
759 then
760 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
761 return;
762
763 elsif A_Id = Aspect_Default_Component_Value then
764 if not Is_Array_Type (Ent) then
765 Error_Msg_N ("aspect% can only be applied to array type", Id);
766 return;
767
768 elsif not Is_Scalar_Type (Component_Type (Ent)) then
769 Error_Msg_N ("aspect% requires scalar components", Id);
770 return;
771 end if;
772 end if;
773
774 Set_Has_Default_Aspect (Base_Type (Ent));
775
776 if Is_Scalar_Type (Ent) then
777 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
778 else
779 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
780 end if;
781 end Analyze_Aspect_Default_Value;
782
783 ---------------------------------
784 -- Inherit_Delayed_Rep_Aspects --
785 ---------------------------------
786
787 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
788 P : constant Entity_Id := Entity (ASN);
789 -- Entithy for parent type
790
791 N : Node_Id;
792 -- Item from Rep_Item chain
793
794 A : Aspect_Id;
795
796 begin
797 -- Loop through delayed aspects for the parent type
798
799 N := ASN;
800 while Present (N) loop
801 if Nkind (N) = N_Aspect_Specification then
802 exit when Entity (N) /= P;
803
804 if Is_Delayed_Aspect (N) then
805 A := Get_Aspect_Id (Chars (Identifier (N)));
806
807 -- Process delayed rep aspect. For Boolean attributes it is
808 -- not possible to cancel an attribute once set (the attempt
809 -- to use an aspect with xxx => False is an error) for a
810 -- derived type. So for those cases, we do not have to check
811 -- if a clause has been given for the derived type, since it
812 -- is harmless to set it again if it is already set.
813
814 case A is
815
816 -- Alignment
817
818 when Aspect_Alignment =>
819 if not Has_Alignment_Clause (E) then
820 Set_Alignment (E, Alignment (P));
821 end if;
822
823 -- Atomic
824
825 when Aspect_Atomic =>
826 if Is_Atomic (P) then
827 Set_Is_Atomic (E);
828 end if;
829
830 -- Atomic_Components
831
832 when Aspect_Atomic_Components =>
833 if Has_Atomic_Components (P) then
834 Set_Has_Atomic_Components (Base_Type (E));
835 end if;
836
837 -- Bit_Order
838
839 when Aspect_Bit_Order =>
840 if Is_Record_Type (E)
841 and then No (Get_Attribute_Definition_Clause
842 (E, Attribute_Bit_Order))
843 and then Reverse_Bit_Order (P)
844 then
845 Set_Reverse_Bit_Order (Base_Type (E));
846 end if;
847
848 -- Component_Size
849
850 when Aspect_Component_Size =>
851 if Is_Array_Type (E)
852 and then not Has_Component_Size_Clause (E)
853 then
854 Set_Component_Size
855 (Base_Type (E), Component_Size (P));
856 end if;
857
858 -- Machine_Radix
859
860 when Aspect_Machine_Radix =>
861 if Is_Decimal_Fixed_Point_Type (E)
862 and then not Has_Machine_Radix_Clause (E)
863 then
864 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
865 end if;
866
867 -- Object_Size (also Size which also sets Object_Size)
868
869 when Aspect_Object_Size | Aspect_Size =>
870 if not Has_Size_Clause (E)
871 and then
872 No (Get_Attribute_Definition_Clause
873 (E, Attribute_Object_Size))
874 then
875 Set_Esize (E, Esize (P));
876 end if;
877
878 -- Pack
879
880 when Aspect_Pack =>
881 if not Is_Packed (E) then
882 Set_Is_Packed (Base_Type (E));
883
884 if Is_Bit_Packed_Array (P) then
885 Set_Is_Bit_Packed_Array (Base_Type (E));
886 Set_Packed_Array_Type (E, Packed_Array_Type (P));
887 end if;
888 end if;
889
890 -- Scalar_Storage_Order
891
892 when Aspect_Scalar_Storage_Order =>
893 if (Is_Record_Type (E) or else Is_Array_Type (E))
894 and then No (Get_Attribute_Definition_Clause
895 (E, Attribute_Scalar_Storage_Order))
896 and then Reverse_Storage_Order (P)
897 then
898 Set_Reverse_Storage_Order (Base_Type (E));
899 end if;
900
901 -- Small
902
903 when Aspect_Small =>
904 if Is_Fixed_Point_Type (E)
905 and then not Has_Small_Clause (E)
906 then
907 Set_Small_Value (E, Small_Value (P));
908 end if;
909
910 -- Storage_Size
911
912 when Aspect_Storage_Size =>
913 if (Is_Access_Type (E) or else Is_Task_Type (E))
914 and then not Has_Storage_Size_Clause (E)
915 then
916 Set_Storage_Size_Variable
917 (Base_Type (E), Storage_Size_Variable (P));
918 end if;
919
920 -- Value_Size
921
922 when Aspect_Value_Size =>
923
924 -- Value_Size is never inherited, it is either set by
925 -- default, or it is explicitly set for the derived
926 -- type. So nothing to do here.
927
928 null;
929
930 -- Volatile
931
932 when Aspect_Volatile =>
933 if Is_Volatile (P) then
934 Set_Is_Volatile (E);
935 end if;
936
937 -- Volatile_Components
938
939 when Aspect_Volatile_Components =>
940 if Has_Volatile_Components (P) then
941 Set_Has_Volatile_Components (Base_Type (E));
942 end if;
943
944 -- That should be all the Rep Aspects
945
946 when others =>
947 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
948 null;
949
950 end case;
951 end if;
952 end if;
953
954 N := Next_Rep_Item (N);
955 end loop;
956 end Inherit_Delayed_Rep_Aspects;
957
958 -------------------------------------
959 -- Make_Pragma_From_Boolean_Aspect --
960 -------------------------------------
961
962 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
963 Ident : constant Node_Id := Identifier (ASN);
964 A_Name : constant Name_Id := Chars (Ident);
965 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
966 Ent : constant Entity_Id := Entity (ASN);
967 Expr : constant Node_Id := Expression (ASN);
968 Loc : constant Source_Ptr := Sloc (ASN);
969
970 Prag : Node_Id;
971
972 procedure Check_False_Aspect_For_Derived_Type;
973 -- This procedure checks for the case of a false aspect for a derived
974 -- type, which improperly tries to cancel an aspect inherited from
975 -- the parent.
976
977 -----------------------------------------
978 -- Check_False_Aspect_For_Derived_Type --
979 -----------------------------------------
980
981 procedure Check_False_Aspect_For_Derived_Type is
982 Par : Node_Id;
983
984 begin
985 -- We are only checking derived types
986
987 if not Is_Derived_Type (E) then
988 return;
989 end if;
990
991 Par := Nearest_Ancestor (E);
992
993 case A_Id is
994 when Aspect_Atomic | Aspect_Shared =>
995 if not Is_Atomic (Par) then
996 return;
997 end if;
998
999 when Aspect_Atomic_Components =>
1000 if not Has_Atomic_Components (Par) then
1001 return;
1002 end if;
1003
1004 when Aspect_Discard_Names =>
1005 if not Discard_Names (Par) then
1006 return;
1007 end if;
1008
1009 when Aspect_Pack =>
1010 if not Is_Packed (Par) then
1011 return;
1012 end if;
1013
1014 when Aspect_Unchecked_Union =>
1015 if not Is_Unchecked_Union (Par) then
1016 return;
1017 end if;
1018
1019 when Aspect_Volatile =>
1020 if not Is_Volatile (Par) then
1021 return;
1022 end if;
1023
1024 when Aspect_Volatile_Components =>
1025 if not Has_Volatile_Components (Par) then
1026 return;
1027 end if;
1028
1029 when others =>
1030 return;
1031 end case;
1032
1033 -- Fall through means we are canceling an inherited aspect
1034
1035 Error_Msg_Name_1 := A_Name;
1036 Error_Msg_NE
1037 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1038
1039 end Check_False_Aspect_For_Derived_Type;
1040
1041 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1042
1043 begin
1044 -- Note that we know Expr is present, because for a missing Expr
1045 -- argument, we knew it was True and did not need to delay the
1046 -- evaluation to the freeze point.
1047
1048 if Is_False (Static_Boolean (Expr)) then
1049 Check_False_Aspect_For_Derived_Type;
1050
1051 else
1052 Prag :=
1053 Make_Pragma (Loc,
1054 Pragma_Argument_Associations => New_List (
1055 Make_Pragma_Argument_Association (Sloc (Ident),
1056 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1057
1058 Pragma_Identifier =>
1059 Make_Identifier (Sloc (Ident), Chars (Ident)));
1060
1061 Set_From_Aspect_Specification (Prag, True);
1062 Set_Corresponding_Aspect (Prag, ASN);
1063 Set_Aspect_Rep_Item (ASN, Prag);
1064 Set_Is_Delayed_Aspect (Prag);
1065 Set_Parent (Prag, ASN);
1066 end if;
1067 end Make_Pragma_From_Boolean_Aspect;
1068
1069 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1070
1071 begin
1072 -- Must be visible in current scope
1073
1074 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1075 return;
1076 end if;
1077
1078 -- Look for aspect specification entries for this entity
1079
1080 ASN := First_Rep_Item (E);
1081 while Present (ASN) loop
1082 if Nkind (ASN) = N_Aspect_Specification then
1083 exit when Entity (ASN) /= E;
1084
1085 if Is_Delayed_Aspect (ASN) then
1086 A_Id := Get_Aspect_Id (ASN);
1087
1088 case A_Id is
1089
1090 -- For aspects whose expression is an optional Boolean, make
1091 -- the corresponding pragma at the freezing point.
1092
1093 when Boolean_Aspects |
1094 Library_Unit_Aspects =>
1095 Make_Pragma_From_Boolean_Aspect (ASN);
1096
1097 -- Special handling for aspects that don't correspond to
1098 -- pragmas/attributes.
1099
1100 when Aspect_Default_Value |
1101 Aspect_Default_Component_Value =>
1102 Analyze_Aspect_Default_Value (ASN);
1103
1104 -- Ditto for iterator aspects, because the corresponding
1105 -- attributes may not have been analyzed yet.
1106
1107 when Aspect_Constant_Indexing |
1108 Aspect_Variable_Indexing |
1109 Aspect_Default_Iterator |
1110 Aspect_Iterator_Element =>
1111 Analyze (Expression (ASN));
1112
1113 when others =>
1114 null;
1115 end case;
1116
1117 Ritem := Aspect_Rep_Item (ASN);
1118
1119 if Present (Ritem) then
1120 Analyze (Ritem);
1121 end if;
1122 end if;
1123 end if;
1124
1125 Next_Rep_Item (ASN);
1126 end loop;
1127
1128 -- This is where we inherit delayed rep aspects from our parent. Note
1129 -- that if we fell out of the above loop with ASN non-empty, it means
1130 -- we hit an aspect for an entity other than E, and it must be the
1131 -- type from which we were derived.
1132
1133 if May_Inherit_Delayed_Rep_Aspects (E) then
1134 Inherit_Delayed_Rep_Aspects (ASN);
1135 end if;
1136 end Analyze_Aspects_At_Freeze_Point;
1137
1138 -----------------------------------
1139 -- Analyze_Aspect_Specifications --
1140 -----------------------------------
1141
1142 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1143 procedure Decorate_Delayed_Aspect_And_Pragma
1144 (Asp : Node_Id;
1145 Prag : Node_Id);
1146 -- Establish the linkages between a delayed aspect and its corresponding
1147 -- pragma. Set all delay-related flags on both constructs.
1148
1149 procedure Insert_Delayed_Pragma (Prag : Node_Id);
1150 -- Insert a postcondition-like pragma into the tree depending on the
1151 -- context. Prag must denote one of the following: Pre, Post, Depends,
1152 -- Global or Contract_Cases.
1153
1154 ----------------------------------------
1155 -- Decorate_Delayed_Aspect_And_Pragma --
1156 ----------------------------------------
1157
1158 procedure Decorate_Delayed_Aspect_And_Pragma
1159 (Asp : Node_Id;
1160 Prag : Node_Id)
1161 is
1162 begin
1163 Set_Aspect_Rep_Item (Asp, Prag);
1164 Set_Corresponding_Aspect (Prag, Asp);
1165 Set_From_Aspect_Specification (Prag);
1166 Set_Is_Delayed_Aspect (Prag);
1167 Set_Is_Delayed_Aspect (Asp);
1168 Set_Parent (Prag, Asp);
1169 end Decorate_Delayed_Aspect_And_Pragma;
1170
1171 ---------------------------
1172 -- Insert_Delayed_Pragma --
1173 ---------------------------
1174
1175 procedure Insert_Delayed_Pragma (Prag : Node_Id) is
1176 Aux : Node_Id;
1177
1178 begin
1179 -- When the context is a library unit, the pragma is added to the
1180 -- Pragmas_After list.
1181
1182 if Nkind (Parent (N)) = N_Compilation_Unit then
1183 Aux := Aux_Decls_Node (Parent (N));
1184
1185 if No (Pragmas_After (Aux)) then
1186 Set_Pragmas_After (Aux, New_List);
1187 end if;
1188
1189 Prepend (Prag, Pragmas_After (Aux));
1190
1191 -- Pragmas associated with subprogram bodies are inserted in the
1192 -- declarative part.
1193
1194 elsif Nkind (N) = N_Subprogram_Body then
1195 if No (Declarations (N)) then
1196 Set_Declarations (N, New_List (Prag));
1197 else
1198 declare
1199 D : Node_Id;
1200 begin
1201
1202 -- There may be several aspects associated with the body;
1203 -- preserve the ordering of the corresponding pragmas.
1204
1205 D := First (Declarations (N));
1206 while Present (D) loop
1207 exit when Nkind (D) /= N_Pragma
1208 or else not From_Aspect_Specification (D);
1209 Next (D);
1210 end loop;
1211
1212 if No (D) then
1213 Append (Prag, Declarations (N));
1214 else
1215 Insert_Before (D, Prag);
1216 end if;
1217 end;
1218 end if;
1219
1220 -- Default
1221
1222 else
1223 Insert_After (N, Prag);
1224
1225 -- Analyze the pragma before analyzing the proper body of a stub.
1226 -- This ensures that the pragma will appear on the proper contract
1227 -- list (see N_Contract).
1228
1229 if Nkind (N) = N_Subprogram_Body_Stub then
1230 Analyze (Prag);
1231 end if;
1232 end if;
1233 end Insert_Delayed_Pragma;
1234
1235 -- Local variables
1236
1237 Aspect : Node_Id;
1238 Aitem : Node_Id;
1239 Ent : Node_Id;
1240
1241 L : constant List_Id := Aspect_Specifications (N);
1242
1243 Ins_Node : Node_Id := N;
1244 -- Insert pragmas/attribute definition clause after this node when no
1245 -- delayed analysis is required.
1246
1247 -- Start of processing for Analyze_Aspect_Specifications
1248
1249 -- The general processing involves building an attribute definition
1250 -- clause or a pragma node that corresponds to the aspect. Then in order
1251 -- to delay the evaluation of this aspect to the freeze point, we attach
1252 -- the corresponding pragma/attribute definition clause to the aspect
1253 -- specification node, which is then placed in the Rep Item chain. In
1254 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1255 -- and we evaluate the rep item at the freeze point. When the aspect
1256 -- doesn't have a corresponding pragma/attribute definition clause, then
1257 -- its analysis is simply delayed at the freeze point.
1258
1259 -- Some special cases don't require delay analysis, thus the aspect is
1260 -- analyzed right now.
1261
1262 -- Note that there is a special handling for Pre, Post, Test_Case,
1263 -- Contract_Cases aspects. In these cases, we do not have to worry
1264 -- about delay issues, since the pragmas themselves deal with delay
1265 -- of visibility for the expression analysis. Thus, we just insert
1266 -- the pragma after the node N.
1267
1268 begin
1269 pragma Assert (Present (L));
1270
1271 -- Loop through aspects
1272
1273 Aspect := First (L);
1274 Aspect_Loop : while Present (Aspect) loop
1275 Analyze_One_Aspect : declare
1276 Expr : constant Node_Id := Expression (Aspect);
1277 Id : constant Node_Id := Identifier (Aspect);
1278 Loc : constant Source_Ptr := Sloc (Aspect);
1279 Nam : constant Name_Id := Chars (Id);
1280 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1281 Anod : Node_Id;
1282
1283 Delay_Required : Boolean;
1284 -- Set False if delay is not required
1285
1286 Eloc : Source_Ptr := No_Location;
1287 -- Source location of expression, modified when we split PPC's. It
1288 -- is set below when Expr is present.
1289
1290 procedure Analyze_Aspect_External_Or_Link_Name;
1291 -- Perform analysis of the External_Name or Link_Name aspects
1292
1293 procedure Analyze_Aspect_Implicit_Dereference;
1294 -- Perform analysis of the Implicit_Dereference aspects
1295
1296 procedure Make_Aitem_Pragma
1297 (Pragma_Argument_Associations : List_Id;
1298 Pragma_Name : Name_Id);
1299 -- This is a wrapper for Make_Pragma used for converting aspects
1300 -- to pragmas. It takes care of Sloc (set from Loc) and building
1301 -- the pragma identifier from the given name. In addition the
1302 -- flags Class_Present and Split_PPC are set from the aspect
1303 -- node, as well as Is_Ignored. This routine also sets the
1304 -- From_Aspect_Specification in the resulting pragma node to
1305 -- True, and sets Corresponding_Aspect to point to the aspect.
1306 -- The resulting pragma is assigned to Aitem.
1307
1308 ------------------------------------------
1309 -- Analyze_Aspect_External_Or_Link_Name --
1310 ------------------------------------------
1311
1312 procedure Analyze_Aspect_External_Or_Link_Name is
1313 begin
1314 -- Verify that there is an Import/Export aspect defined for the
1315 -- entity. The processing of that aspect in turn checks that
1316 -- there is a Convention aspect declared. The pragma is
1317 -- constructed when processing the Convention aspect.
1318
1319 declare
1320 A : Node_Id;
1321
1322 begin
1323 A := First (L);
1324 while Present (A) loop
1325 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1326 Name_Import);
1327 Next (A);
1328 end loop;
1329
1330 if No (A) then
1331 Error_Msg_N
1332 ("missing Import/Export for Link/External name",
1333 Aspect);
1334 end if;
1335 end;
1336 end Analyze_Aspect_External_Or_Link_Name;
1337
1338 -----------------------------------------
1339 -- Analyze_Aspect_Implicit_Dereference --
1340 -----------------------------------------
1341
1342 procedure Analyze_Aspect_Implicit_Dereference is
1343 begin
1344 if not Is_Type (E) or else not Has_Discriminants (E) then
1345 Error_Msg_N
1346 ("aspect must apply to a type with discriminants", N);
1347
1348 else
1349 declare
1350 Disc : Entity_Id;
1351
1352 begin
1353 Disc := First_Discriminant (E);
1354 while Present (Disc) loop
1355 if Chars (Expr) = Chars (Disc)
1356 and then Ekind (Etype (Disc)) =
1357 E_Anonymous_Access_Type
1358 then
1359 Set_Has_Implicit_Dereference (E);
1360 Set_Has_Implicit_Dereference (Disc);
1361 return;
1362 end if;
1363
1364 Next_Discriminant (Disc);
1365 end loop;
1366
1367 -- Error if no proper access discriminant.
1368
1369 Error_Msg_NE
1370 ("not an access discriminant of&", Expr, E);
1371 end;
1372 end if;
1373 end Analyze_Aspect_Implicit_Dereference;
1374
1375 -----------------------
1376 -- Make_Aitem_Pragma --
1377 -----------------------
1378
1379 procedure Make_Aitem_Pragma
1380 (Pragma_Argument_Associations : List_Id;
1381 Pragma_Name : Name_Id)
1382 is
1383 Args : List_Id := Pragma_Argument_Associations;
1384
1385 begin
1386 -- We should never get here if aspect was disabled
1387
1388 pragma Assert (not Is_Disabled (Aspect));
1389
1390 -- Certain aspects allow for an optional name or expression. Do
1391 -- not generate a pragma with empty argument association list.
1392
1393 if No (Args) or else No (Expression (First (Args))) then
1394 Args := No_List;
1395 end if;
1396
1397 -- Build the pragma
1398
1399 Aitem :=
1400 Make_Pragma (Loc,
1401 Pragma_Argument_Associations => Args,
1402 Pragma_Identifier =>
1403 Make_Identifier (Sloc (Id), Pragma_Name),
1404 Class_Present => Class_Present (Aspect),
1405 Split_PPC => Split_PPC (Aspect));
1406
1407 -- Set additional semantic fields
1408
1409 if Is_Ignored (Aspect) then
1410 Set_Is_Ignored (Aitem);
1411 elsif Is_Checked (Aspect) then
1412 Set_Is_Checked (Aitem);
1413 end if;
1414
1415 Set_Corresponding_Aspect (Aitem, Aspect);
1416 Set_From_Aspect_Specification (Aitem, True);
1417 end Make_Aitem_Pragma;
1418
1419 -- Start of processing for Analyze_One_Aspect
1420
1421 begin
1422 -- Skip aspect if already analyzed (not clear if this is needed)
1423
1424 if Analyzed (Aspect) then
1425 goto Continue;
1426 end if;
1427
1428 -- Skip looking at aspect if it is totally disabled. Just mark it
1429 -- as such for later reference in the tree. This also sets the
1430 -- Is_Ignored and Is_Checked flags appropriately.
1431
1432 Check_Applicable_Policy (Aspect);
1433
1434 if Is_Disabled (Aspect) then
1435 goto Continue;
1436 end if;
1437
1438 -- Set the source location of expression, used in the case of
1439 -- a failed precondition/postcondition or invariant. Note that
1440 -- the source location of the expression is not usually the best
1441 -- choice here. For example, it gets located on the last AND
1442 -- keyword in a chain of boolean expressiond AND'ed together.
1443 -- It is best to put the message on the first character of the
1444 -- assertion, which is the effect of the First_Node call here.
1445
1446 if Present (Expr) then
1447 Eloc := Sloc (First_Node (Expr));
1448 end if;
1449
1450 -- Check restriction No_Implementation_Aspect_Specifications
1451
1452 if Implementation_Defined_Aspect (A_Id) then
1453 Check_Restriction
1454 (No_Implementation_Aspect_Specifications, Aspect);
1455 end if;
1456
1457 -- Check restriction No_Specification_Of_Aspect
1458
1459 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1460
1461 -- Analyze this aspect (actual analysis is delayed till later)
1462
1463 Set_Analyzed (Aspect);
1464 Set_Entity (Aspect, E);
1465 Ent := New_Occurrence_Of (E, Sloc (Id));
1466
1467 -- Check for duplicate aspect. Note that the Comes_From_Source
1468 -- test allows duplicate Pre/Post's that we generate internally
1469 -- to escape being flagged here.
1470
1471 if No_Duplicates_Allowed (A_Id) then
1472 Anod := First (L);
1473 while Anod /= Aspect loop
1474 if Comes_From_Source (Aspect)
1475 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1476 then
1477 Error_Msg_Name_1 := Nam;
1478 Error_Msg_Sloc := Sloc (Anod);
1479
1480 -- Case of same aspect specified twice
1481
1482 if Class_Present (Anod) = Class_Present (Aspect) then
1483 if not Class_Present (Anod) then
1484 Error_Msg_NE
1485 ("aspect% for & previously given#",
1486 Id, E);
1487 else
1488 Error_Msg_NE
1489 ("aspect `%''Class` for & previously given#",
1490 Id, E);
1491 end if;
1492 end if;
1493 end if;
1494
1495 Next (Anod);
1496 end loop;
1497 end if;
1498
1499 -- Check some general restrictions on language defined aspects
1500
1501 if not Implementation_Defined_Aspect (A_Id) then
1502 Error_Msg_Name_1 := Nam;
1503
1504 -- Not allowed for renaming declarations
1505
1506 if Nkind (N) in N_Renaming_Declaration then
1507 Error_Msg_N
1508 ("aspect % not allowed for renaming declaration",
1509 Aspect);
1510 end if;
1511
1512 -- Not allowed for formal type declarations
1513
1514 if Nkind (N) = N_Formal_Type_Declaration then
1515 Error_Msg_N
1516 ("aspect % not allowed for formal type declaration",
1517 Aspect);
1518 end if;
1519 end if;
1520
1521 -- Copy expression for later processing by the procedures
1522 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1523
1524 Set_Entity (Id, New_Copy_Tree (Expr));
1525
1526 -- Set Delay_Required as appropriate to aspect
1527
1528 case Aspect_Delay (A_Id) is
1529 when Always_Delay =>
1530 Delay_Required := True;
1531
1532 when Never_Delay =>
1533 Delay_Required := False;
1534
1535 when Rep_Aspect =>
1536
1537 -- If expression has the form of an integer literal, then
1538 -- do not delay, since we know the value cannot change.
1539 -- This optimization catches most rep clause cases.
1540
1541 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1542 or else (A_Id in Boolean_Aspects and then No (Expr))
1543 then
1544 Delay_Required := False;
1545 else
1546 Delay_Required := True;
1547 Set_Has_Delayed_Rep_Aspects (E);
1548 end if;
1549 end case;
1550
1551 -- Processing based on specific aspect
1552
1553 case A_Id is
1554
1555 -- No_Aspect should be impossible
1556
1557 when No_Aspect =>
1558 raise Program_Error;
1559
1560 -- Case 1: Aspects corresponding to attribute definition
1561 -- clauses.
1562
1563 when Aspect_Address |
1564 Aspect_Alignment |
1565 Aspect_Bit_Order |
1566 Aspect_Component_Size |
1567 Aspect_Constant_Indexing |
1568 Aspect_Default_Iterator |
1569 Aspect_Dispatching_Domain |
1570 Aspect_External_Tag |
1571 Aspect_Input |
1572 Aspect_Iterator_Element |
1573 Aspect_Machine_Radix |
1574 Aspect_Object_Size |
1575 Aspect_Output |
1576 Aspect_Read |
1577 Aspect_Scalar_Storage_Order |
1578 Aspect_Size |
1579 Aspect_Small |
1580 Aspect_Simple_Storage_Pool |
1581 Aspect_Storage_Pool |
1582 Aspect_Stream_Size |
1583 Aspect_Value_Size |
1584 Aspect_Variable_Indexing |
1585 Aspect_Write =>
1586
1587 -- Indexing aspects apply only to tagged type
1588
1589 if (A_Id = Aspect_Constant_Indexing
1590 or else
1591 A_Id = Aspect_Variable_Indexing)
1592 and then not (Is_Type (E)
1593 and then Is_Tagged_Type (E))
1594 then
1595 Error_Msg_N ("indexing applies to a tagged type", N);
1596 goto Continue;
1597 end if;
1598
1599 -- For case of address aspect, we don't consider that we
1600 -- know the entity is never set in the source, since it is
1601 -- is likely aliasing is occurring.
1602
1603 -- Note: one might think that the analysis of the resulting
1604 -- attribute definition clause would take care of that, but
1605 -- that's not the case since it won't be from source.
1606
1607 if A_Id = Aspect_Address then
1608 Set_Never_Set_In_Source (E, False);
1609 end if;
1610
1611 -- Construct the attribute definition clause
1612
1613 Aitem :=
1614 Make_Attribute_Definition_Clause (Loc,
1615 Name => Ent,
1616 Chars => Chars (Id),
1617 Expression => Relocate_Node (Expr));
1618
1619 -- If the address is specified, then we treat the entity as
1620 -- referenced, to avoid spurious warnings. This is analogous
1621 -- to what is done with an attribute definition clause, but
1622 -- here we don't want to generate a reference because this
1623 -- is the point of definition of the entity.
1624
1625 if A_Id = Aspect_Address then
1626 Set_Referenced (E);
1627 end if;
1628
1629 -- Case 2: Aspects corresponding to pragmas
1630
1631 -- Case 2a: Aspects corresponding to pragmas with two
1632 -- arguments, where the first argument is a local name
1633 -- referring to the entity, and the second argument is the
1634 -- aspect definition expression.
1635
1636 -- Suppress/Unsuppress
1637
1638 when Aspect_Suppress |
1639 Aspect_Unsuppress =>
1640
1641 Make_Aitem_Pragma
1642 (Pragma_Argument_Associations => New_List (
1643 Make_Pragma_Argument_Association (Loc,
1644 Expression => New_Occurrence_Of (E, Loc)),
1645 Make_Pragma_Argument_Association (Sloc (Expr),
1646 Expression => Relocate_Node (Expr))),
1647 Pragma_Name => Chars (Id));
1648
1649 -- Synchronization
1650
1651 -- Corresponds to pragma Implemented, construct the pragma
1652
1653 when Aspect_Synchronization =>
1654
1655 Make_Aitem_Pragma
1656 (Pragma_Argument_Associations => New_List (
1657 Make_Pragma_Argument_Association (Loc,
1658 Expression => New_Occurrence_Of (E, Loc)),
1659 Make_Pragma_Argument_Association (Sloc (Expr),
1660 Expression => Relocate_Node (Expr))),
1661 Pragma_Name => Name_Implemented);
1662
1663 -- Attach Handler
1664
1665 when Aspect_Attach_Handler =>
1666 Make_Aitem_Pragma
1667 (Pragma_Argument_Associations => New_List (
1668 Make_Pragma_Argument_Association (Sloc (Ent),
1669 Expression => Ent),
1670 Make_Pragma_Argument_Association (Sloc (Expr),
1671 Expression => Relocate_Node (Expr))),
1672 Pragma_Name => Name_Attach_Handler);
1673
1674 -- Dynamic_Predicate, Predicate, Static_Predicate
1675
1676 when Aspect_Dynamic_Predicate |
1677 Aspect_Predicate |
1678 Aspect_Static_Predicate =>
1679
1680 -- Construct the pragma (always a pragma Predicate, with
1681 -- flags recording whether it is static/dynamic). We also
1682 -- set flags recording this in the type itself.
1683
1684 Make_Aitem_Pragma
1685 (Pragma_Argument_Associations => New_List (
1686 Make_Pragma_Argument_Association (Sloc (Ent),
1687 Expression => Ent),
1688 Make_Pragma_Argument_Association (Sloc (Expr),
1689 Expression => Relocate_Node (Expr))),
1690 Pragma_Name => Name_Predicate);
1691
1692 -- Mark type has predicates, and remember what kind of
1693 -- aspect lead to this predicate (we need this to access
1694 -- the right set of check policies later on).
1695
1696 Set_Has_Predicates (E);
1697
1698 if A_Id = Aspect_Dynamic_Predicate then
1699 Set_Has_Dynamic_Predicate_Aspect (E);
1700 elsif A_Id = Aspect_Static_Predicate then
1701 Set_Has_Static_Predicate_Aspect (E);
1702 end if;
1703
1704 -- If the type is private, indicate that its completion
1705 -- has a freeze node, because that is the one that will
1706 -- be visible at freeze time.
1707
1708 if Is_Private_Type (E) and then Present (Full_View (E)) then
1709 Set_Has_Predicates (Full_View (E));
1710
1711 if A_Id = Aspect_Dynamic_Predicate then
1712 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1713 elsif A_Id = Aspect_Static_Predicate then
1714 Set_Has_Static_Predicate_Aspect (Full_View (E));
1715 end if;
1716
1717 Set_Has_Delayed_Aspects (Full_View (E));
1718 Ensure_Freeze_Node (Full_View (E));
1719 end if;
1720
1721 -- Case 2b: Aspects corresponding to pragmas with two
1722 -- arguments, where the second argument is a local name
1723 -- referring to the entity, and the first argument is the
1724 -- aspect definition expression.
1725
1726 -- Convention
1727
1728 when Aspect_Convention =>
1729
1730 -- The aspect may be part of the specification of an import
1731 -- or export pragma. Scan the aspect list to gather the
1732 -- other components, if any. The name of the generated
1733 -- pragma is one of Convention/Import/Export.
1734
1735 declare
1736 P_Name : Name_Id;
1737 A_Name : Name_Id;
1738 A : Node_Id;
1739 Arg_List : List_Id;
1740 Found : Boolean;
1741 L_Assoc : Node_Id;
1742 E_Assoc : Node_Id;
1743
1744 begin
1745 P_Name := Chars (Id);
1746 Found := False;
1747 Arg_List := New_List;
1748 L_Assoc := Empty;
1749 E_Assoc := Empty;
1750
1751 A := First (L);
1752 while Present (A) loop
1753 A_Name := Chars (Identifier (A));
1754
1755 if Nam_In (A_Name, Name_Import, Name_Export) then
1756 if Found then
1757 Error_Msg_N ("conflicting", A);
1758 else
1759 Found := True;
1760 end if;
1761
1762 P_Name := A_Name;
1763
1764 elsif A_Name = Name_Link_Name then
1765 L_Assoc :=
1766 Make_Pragma_Argument_Association (Loc,
1767 Chars => A_Name,
1768 Expression => Relocate_Node (Expression (A)));
1769
1770 elsif A_Name = Name_External_Name then
1771 E_Assoc :=
1772 Make_Pragma_Argument_Association (Loc,
1773 Chars => A_Name,
1774 Expression => Relocate_Node (Expression (A)));
1775 end if;
1776
1777 Next (A);
1778 end loop;
1779
1780 Arg_List := New_List (
1781 Make_Pragma_Argument_Association (Sloc (Expr),
1782 Expression => Relocate_Node (Expr)),
1783 Make_Pragma_Argument_Association (Sloc (Ent),
1784 Expression => Ent));
1785
1786 if Present (L_Assoc) then
1787 Append_To (Arg_List, L_Assoc);
1788 end if;
1789
1790 if Present (E_Assoc) then
1791 Append_To (Arg_List, E_Assoc);
1792 end if;
1793
1794 Make_Aitem_Pragma
1795 (Pragma_Argument_Associations => Arg_List,
1796 Pragma_Name => P_Name);
1797 end;
1798
1799 -- CPU, Interrupt_Priority, Priority
1800
1801 -- These three aspects can be specified for a subprogram spec
1802 -- or body, in which case we analyze the expression and export
1803 -- the value of the aspect.
1804
1805 -- Previously, we generated an equivalent pragma for bodies
1806 -- (note that the specs cannot contain these pragmas). The
1807 -- pragma was inserted ahead of local declarations, rather than
1808 -- after the body. This leads to a certain duplication between
1809 -- the processing performed for the aspect and the pragma, but
1810 -- given the straightforward handling required it is simpler
1811 -- to duplicate than to translate the aspect in the spec into
1812 -- a pragma in the declarative part of the body.
1813
1814 when Aspect_CPU |
1815 Aspect_Interrupt_Priority |
1816 Aspect_Priority =>
1817
1818 if Nkind_In (N, N_Subprogram_Body,
1819 N_Subprogram_Declaration)
1820 then
1821 -- Analyze the aspect expression
1822
1823 Analyze_And_Resolve (Expr, Standard_Integer);
1824
1825 -- Interrupt_Priority aspect not allowed for main
1826 -- subprograms. ARM D.1 does not forbid this explicitly,
1827 -- but ARM J.15.11 (6/3) does not permit pragma
1828 -- Interrupt_Priority for subprograms.
1829
1830 if A_Id = Aspect_Interrupt_Priority then
1831 Error_Msg_N
1832 ("Interrupt_Priority aspect cannot apply to "
1833 & "subprogram", Expr);
1834
1835 -- The expression must be static
1836
1837 elsif not Is_Static_Expression (Expr) then
1838 Flag_Non_Static_Expr
1839 ("aspect requires static expression!", Expr);
1840
1841 -- Check whether this is the main subprogram. Issue a
1842 -- warning only if it is obviously not a main program
1843 -- (when it has parameters or when the subprogram is
1844 -- within a package).
1845
1846 elsif Present (Parameter_Specifications
1847 (Specification (N)))
1848 or else not Is_Compilation_Unit (Defining_Entity (N))
1849 then
1850 -- See ARM D.1 (14/3) and D.16 (12/3)
1851
1852 Error_Msg_N
1853 ("aspect applied to subprogram other than the "
1854 & "main subprogram has no effect??", Expr);
1855
1856 -- Otherwise check in range and export the value
1857
1858 -- For the CPU aspect
1859
1860 elsif A_Id = Aspect_CPU then
1861 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
1862
1863 -- Value is correct so we export the value to make
1864 -- it available at execution time.
1865
1866 Set_Main_CPU
1867 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
1868
1869 else
1870 Error_Msg_N
1871 ("main subprogram CPU is out of range", Expr);
1872 end if;
1873
1874 -- For the Priority aspect
1875
1876 elsif A_Id = Aspect_Priority then
1877 if Is_In_Range (Expr, RTE (RE_Priority)) then
1878
1879 -- Value is correct so we export the value to make
1880 -- it available at execution time.
1881
1882 Set_Main_Priority
1883 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
1884
1885 else
1886 Error_Msg_N
1887 ("main subprogram priority is out of range",
1888 Expr);
1889 end if;
1890 end if;
1891
1892 -- Load an arbitrary entity from System.Tasking.Stages
1893 -- or System.Tasking.Restricted.Stages (depending on
1894 -- the supported profile) to make sure that one of these
1895 -- packages is implicitly with'ed, since we need to have
1896 -- the tasking run time active for the pragma Priority to
1897 -- have any effect. Previously with with'ed the package
1898 -- System.Tasking, but this package does not trigger the
1899 -- required initialization of the run-time library.
1900
1901 declare
1902 Discard : Entity_Id;
1903 pragma Warnings (Off, Discard);
1904 begin
1905 if Restricted_Profile then
1906 Discard := RTE (RE_Activate_Restricted_Tasks);
1907 else
1908 Discard := RTE (RE_Activate_Tasks);
1909 end if;
1910 end;
1911
1912 -- Handling for these Aspects in subprograms is complete
1913
1914 goto Continue;
1915
1916 -- For tasks
1917
1918 else
1919 -- Pass the aspect as an attribute
1920
1921 Aitem :=
1922 Make_Attribute_Definition_Clause (Loc,
1923 Name => Ent,
1924 Chars => Chars (Id),
1925 Expression => Relocate_Node (Expr));
1926 end if;
1927
1928 -- Warnings
1929
1930 when Aspect_Warnings =>
1931 Make_Aitem_Pragma
1932 (Pragma_Argument_Associations => New_List (
1933 Make_Pragma_Argument_Association (Sloc (Expr),
1934 Expression => Relocate_Node (Expr)),
1935 Make_Pragma_Argument_Association (Loc,
1936 Expression => New_Occurrence_Of (E, Loc))),
1937 Pragma_Name => Chars (Id));
1938
1939 -- Case 2c: Aspects corresponding to pragmas with three
1940 -- arguments.
1941
1942 -- Invariant aspects have a first argument that references the
1943 -- entity, a second argument that is the expression and a third
1944 -- argument that is an appropriate message.
1945
1946 -- Invariant, Type_Invariant
1947
1948 when Aspect_Invariant |
1949 Aspect_Type_Invariant =>
1950
1951 -- Analysis of the pragma will verify placement legality:
1952 -- an invariant must apply to a private type, or appear in
1953 -- the private part of a spec and apply to a completion.
1954
1955 Make_Aitem_Pragma
1956 (Pragma_Argument_Associations => New_List (
1957 Make_Pragma_Argument_Association (Sloc (Ent),
1958 Expression => Ent),
1959 Make_Pragma_Argument_Association (Sloc (Expr),
1960 Expression => Relocate_Node (Expr))),
1961 Pragma_Name => Name_Invariant);
1962
1963 -- Add message unless exception messages are suppressed
1964
1965 if not Opt.Exception_Locations_Suppressed then
1966 Append_To (Pragma_Argument_Associations (Aitem),
1967 Make_Pragma_Argument_Association (Eloc,
1968 Chars => Name_Message,
1969 Expression =>
1970 Make_String_Literal (Eloc,
1971 Strval => "failed invariant from "
1972 & Build_Location_String (Eloc))));
1973 end if;
1974
1975 -- For Invariant case, insert immediately after the entity
1976 -- declaration. We do not have to worry about delay issues
1977 -- since the pragma processing takes care of this.
1978
1979 Delay_Required := False;
1980
1981 -- Case 2d : Aspects that correspond to a pragma with one
1982 -- argument.
1983
1984 -- Abstract_State
1985
1986 -- Aspect Abstract_State introduces implicit declarations for
1987 -- all state abstraction entities it defines. To emulate this
1988 -- behavior, insert the pragma at the beginning of the visible
1989 -- declarations of the related package so that it is analyzed
1990 -- immediately.
1991
1992 when Aspect_Abstract_State => Abstract_State : declare
1993 Decls : List_Id;
1994
1995 begin
1996 if Nkind_In (N, N_Generic_Package_Declaration,
1997 N_Package_Declaration)
1998 then
1999 Decls := Visible_Declarations (Specification (N));
2000
2001 Make_Aitem_Pragma
2002 (Pragma_Argument_Associations => New_List (
2003 Make_Pragma_Argument_Association (Loc,
2004 Expression => Relocate_Node (Expr))),
2005 Pragma_Name => Name_Abstract_State);
2006 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2007
2008 if No (Decls) then
2009 Decls := New_List;
2010 Set_Visible_Declarations (N, Decls);
2011 end if;
2012
2013 Prepend_To (Decls, Aitem);
2014
2015 else
2016 Error_Msg_NE
2017 ("aspect & must apply to a package declaration",
2018 Aspect, Id);
2019 end if;
2020
2021 goto Continue;
2022 end Abstract_State;
2023
2024 -- Depends
2025
2026 -- Aspect Depends must be delayed because it mentions names
2027 -- of inputs and output that are classified by aspect Global.
2028 -- The aspect and pragma are treated the same way as a post
2029 -- condition.
2030
2031 when Aspect_Depends =>
2032 Make_Aitem_Pragma
2033 (Pragma_Argument_Associations => New_List (
2034 Make_Pragma_Argument_Association (Loc,
2035 Expression => Relocate_Node (Expr))),
2036 Pragma_Name => Name_Depends);
2037
2038 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2039 Insert_Delayed_Pragma (Aitem);
2040 goto Continue;
2041
2042 -- Global
2043
2044 -- Aspect Global must be delayed because it can mention names
2045 -- and benefit from the forward visibility rules applicable to
2046 -- aspects of subprograms. The aspect and pragma are treated
2047 -- the same way as a post condition.
2048
2049 when Aspect_Global =>
2050 Make_Aitem_Pragma
2051 (Pragma_Argument_Associations => New_List (
2052 Make_Pragma_Argument_Association (Loc,
2053 Expression => Relocate_Node (Expr))),
2054 Pragma_Name => Name_Global);
2055
2056 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2057 Insert_Delayed_Pragma (Aitem);
2058 goto Continue;
2059
2060 -- Initial_Condition
2061
2062 -- Aspect Initial_Condition covers the visible declarations of
2063 -- a package and all hidden states through functions. As such,
2064 -- it must be evaluated at the end of the said declarations.
2065
2066 when Aspect_Initial_Condition => Initial_Condition : declare
2067 Decls : List_Id;
2068
2069 begin
2070 if Nkind_In (N, N_Generic_Package_Declaration,
2071 N_Package_Declaration)
2072 then
2073 Decls := Visible_Declarations (Specification (N));
2074
2075 Make_Aitem_Pragma
2076 (Pragma_Argument_Associations => New_List (
2077 Make_Pragma_Argument_Association (Loc,
2078 Expression => Relocate_Node (Expr))),
2079 Pragma_Name =>
2080 Name_Initial_Condition);
2081 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2082
2083 if No (Decls) then
2084 Decls := New_List;
2085 Set_Visible_Declarations (N, Decls);
2086 end if;
2087
2088 Prepend_To (Decls, Aitem);
2089
2090 else
2091 Error_Msg_NE
2092 ("aspect & must apply to a package declaration",
2093 Aspect, Id);
2094 end if;
2095
2096 goto Continue;
2097 end Initial_Condition;
2098
2099 -- Initializes
2100
2101 -- Aspect Initializes coverts the visible declarations of a
2102 -- package. As such, it must be evaluated at the end of the
2103 -- said declarations.
2104
2105 when Aspect_Initializes => Initializes : declare
2106 Decls : List_Id;
2107
2108 begin
2109 if Nkind_In (N, N_Generic_Package_Declaration,
2110 N_Package_Declaration)
2111 then
2112 Decls := Visible_Declarations (Specification (N));
2113
2114 Make_Aitem_Pragma
2115 (Pragma_Argument_Associations => New_List (
2116 Make_Pragma_Argument_Association (Loc,
2117 Expression => Relocate_Node (Expr))),
2118 Pragma_Name => Name_Initializes);
2119 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2120
2121 if No (Decls) then
2122 Decls := New_List;
2123 Set_Visible_Declarations (N, Decls);
2124 end if;
2125
2126 Prepend_To (Decls, Aitem);
2127
2128 else
2129 Error_Msg_NE
2130 ("aspect & must apply to a package declaration",
2131 Aspect, Id);
2132 end if;
2133
2134 goto Continue;
2135 end Initializes;
2136
2137 -- SPARK_Mode
2138
2139 when Aspect_SPARK_Mode => SPARK_Mode : declare
2140 Decls : List_Id;
2141
2142 begin
2143 Make_Aitem_Pragma
2144 (Pragma_Argument_Associations => New_List (
2145 Make_Pragma_Argument_Association (Loc,
2146 Expression => Relocate_Node (Expr))),
2147 Pragma_Name => Name_SPARK_Mode);
2148
2149 -- When the aspect appears on a package body, insert the
2150 -- generated pragma at the top of the body declarations to
2151 -- emulate the behavior of a source pragma.
2152
2153 if Nkind (N) = N_Package_Body then
2154 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2155 Decls := Declarations (N);
2156
2157 if No (Decls) then
2158 Decls := New_List;
2159 Set_Declarations (N, Decls);
2160 end if;
2161
2162 Prepend_To (Decls, Aitem);
2163 goto Continue;
2164
2165 -- When the aspect is associated with package declaration,
2166 -- insert the generated pragma at the top of the visible
2167 -- declarations to emulate the behavior of a source pragma.
2168
2169 elsif Nkind (N) = N_Package_Declaration then
2170 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2171 Decls := Visible_Declarations (Specification (N));
2172
2173 if No (Decls) then
2174 Decls := New_List;
2175 Set_Visible_Declarations (Specification (N), Decls);
2176 end if;
2177
2178 Prepend_To (Decls, Aitem);
2179 goto Continue;
2180 end if;
2181 end SPARK_Mode;
2182
2183 -- Refined_Depends
2184
2185 -- Aspect Refined_Depends must be delayed because it can
2186 -- mention state refinements introduced by aspect Refined_State
2187 -- and further classified by aspect Refined_Global. Since both
2188 -- those aspects are delayed, so is Refined_Depends.
2189
2190 when Aspect_Refined_Depends =>
2191 Make_Aitem_Pragma
2192 (Pragma_Argument_Associations => New_List (
2193 Make_Pragma_Argument_Association (Loc,
2194 Expression => Relocate_Node (Expr))),
2195 Pragma_Name => Name_Refined_Depends);
2196
2197 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2198 Insert_Delayed_Pragma (Aitem);
2199 goto Continue;
2200
2201 -- Refined_Global
2202
2203 -- Aspect Refined_Global must be delayed because it can mention
2204 -- state refinements introduced by aspect Refined_State. Since
2205 -- Refined_State is already delayed due to forward references,
2206 -- so is Refined_Global.
2207
2208 when Aspect_Refined_Global =>
2209 Make_Aitem_Pragma
2210 (Pragma_Argument_Associations => New_List (
2211 Make_Pragma_Argument_Association (Loc,
2212 Expression => Relocate_Node (Expr))),
2213 Pragma_Name => Name_Refined_Global);
2214
2215 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2216 Insert_Delayed_Pragma (Aitem);
2217 goto Continue;
2218
2219 -- Refined_Post
2220
2221 when Aspect_Refined_Post =>
2222 Make_Aitem_Pragma
2223 (Pragma_Argument_Associations => New_List (
2224 Make_Pragma_Argument_Association (Loc,
2225 Expression => Relocate_Node (Expr))),
2226 Pragma_Name => Name_Refined_Post);
2227
2228 -- Refined_State
2229
2230 when Aspect_Refined_State => Refined_State : declare
2231 Decls : List_Id;
2232
2233 begin
2234 -- The corresponding pragma for Refined_State is inserted in
2235 -- the declarations of the related package body. This action
2236 -- synchronizes both the source and from-aspect versions of
2237 -- the pragma.
2238
2239 if Nkind (N) = N_Package_Body then
2240 Decls := Declarations (N);
2241
2242 Make_Aitem_Pragma
2243 (Pragma_Argument_Associations => New_List (
2244 Make_Pragma_Argument_Association (Loc,
2245 Expression => Relocate_Node (Expr))),
2246 Pragma_Name => Name_Refined_State);
2247 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2248
2249 if No (Decls) then
2250 Decls := New_List;
2251 Set_Declarations (N, Decls);
2252 end if;
2253
2254 Prepend_To (Decls, Aitem);
2255
2256 else
2257 Error_Msg_NE
2258 ("aspect & must apply to a package body", Aspect, Id);
2259 end if;
2260
2261 goto Continue;
2262 end Refined_State;
2263
2264 -- Relative_Deadline
2265
2266 when Aspect_Relative_Deadline =>
2267 Make_Aitem_Pragma
2268 (Pragma_Argument_Associations => New_List (
2269 Make_Pragma_Argument_Association (Loc,
2270 Expression => Relocate_Node (Expr))),
2271 Pragma_Name => Name_Relative_Deadline);
2272
2273 -- If the aspect applies to a task, the corresponding pragma
2274 -- must appear within its declarations, not after.
2275
2276 if Nkind (N) = N_Task_Type_Declaration then
2277 declare
2278 Def : Node_Id;
2279 V : List_Id;
2280
2281 begin
2282 if No (Task_Definition (N)) then
2283 Set_Task_Definition (N,
2284 Make_Task_Definition (Loc,
2285 Visible_Declarations => New_List,
2286 End_Label => Empty));
2287 end if;
2288
2289 Def := Task_Definition (N);
2290 V := Visible_Declarations (Def);
2291 if not Is_Empty_List (V) then
2292 Insert_Before (First (V), Aitem);
2293
2294 else
2295 Set_Visible_Declarations (Def, New_List (Aitem));
2296 end if;
2297
2298 goto Continue;
2299 end;
2300 end if;
2301
2302 -- Case 3 : Aspects that don't correspond to pragma/attribute
2303 -- definition clause.
2304
2305 -- Case 3a: The aspects listed below don't correspond to
2306 -- pragmas/attributes but do require delayed analysis.
2307
2308 -- Default_Value, Default_Component_Value
2309
2310 when Aspect_Default_Value |
2311 Aspect_Default_Component_Value =>
2312 Aitem := Empty;
2313
2314 -- Case 3b: The aspects listed below don't correspond to
2315 -- pragmas/attributes and don't need delayed analysis.
2316
2317 -- Implicit_Dereference
2318
2319 -- For Implicit_Dereference, External_Name and Link_Name, only
2320 -- the legality checks are done during the analysis, thus no
2321 -- delay is required.
2322
2323 when Aspect_Implicit_Dereference =>
2324 Analyze_Aspect_Implicit_Dereference;
2325 goto Continue;
2326
2327 -- External_Name, Link_Name
2328
2329 when Aspect_External_Name |
2330 Aspect_Link_Name =>
2331 Analyze_Aspect_External_Or_Link_Name;
2332 goto Continue;
2333
2334 -- Dimension
2335
2336 when Aspect_Dimension =>
2337 Analyze_Aspect_Dimension (N, Id, Expr);
2338 goto Continue;
2339
2340 -- Dimension_System
2341
2342 when Aspect_Dimension_System =>
2343 Analyze_Aspect_Dimension_System (N, Id, Expr);
2344 goto Continue;
2345
2346 -- Case 4: Aspects requiring special handling
2347
2348 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2349 -- pragmas take care of the delay.
2350
2351 -- Pre/Post
2352
2353 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2354 -- with a first argument that is the expression, and a second
2355 -- argument that is an informative message if the test fails.
2356 -- This is inserted right after the declaration, to get the
2357 -- required pragma placement. The processing for the pragmas
2358 -- takes care of the required delay.
2359
2360 when Pre_Post_Aspects => Pre_Post : declare
2361 Pname : Name_Id;
2362
2363 begin
2364 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2365 Pname := Name_Precondition;
2366 else
2367 Pname := Name_Postcondition;
2368 end if;
2369
2370 -- If the expressions is of the form A and then B, then
2371 -- we generate separate Pre/Post aspects for the separate
2372 -- clauses. Since we allow multiple pragmas, there is no
2373 -- problem in allowing multiple Pre/Post aspects internally.
2374 -- These should be treated in reverse order (B first and
2375 -- A second) since they are later inserted just after N in
2376 -- the order they are treated. This way, the pragma for A
2377 -- ends up preceding the pragma for B, which may have an
2378 -- importance for the error raised (either constraint error
2379 -- or precondition error).
2380
2381 -- We do not do this for Pre'Class, since we have to put
2382 -- these conditions together in a complex OR expression
2383
2384 -- We do not do this in ASIS mode, as ASIS relies on the
2385 -- original node representing the complete expression, when
2386 -- retrieving it through the source aspect table.
2387
2388 if not ASIS_Mode
2389 and then (Pname = Name_Postcondition
2390 or else not Class_Present (Aspect))
2391 then
2392 while Nkind (Expr) = N_And_Then loop
2393 Insert_After (Aspect,
2394 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2395 Identifier => Identifier (Aspect),
2396 Expression => Relocate_Node (Left_Opnd (Expr)),
2397 Class_Present => Class_Present (Aspect),
2398 Split_PPC => True));
2399 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2400 Eloc := Sloc (Expr);
2401 end loop;
2402 end if;
2403
2404 -- Build the precondition/postcondition pragma
2405
2406 -- Add note about why we do NOT need Copy_Tree here ???
2407
2408 Make_Aitem_Pragma
2409 (Pragma_Argument_Associations => New_List (
2410 Make_Pragma_Argument_Association (Eloc,
2411 Chars => Name_Check,
2412 Expression => Relocate_Node (Expr))),
2413 Pragma_Name => Pname);
2414
2415 -- Add message unless exception messages are suppressed
2416
2417 if not Opt.Exception_Locations_Suppressed then
2418 Append_To (Pragma_Argument_Associations (Aitem),
2419 Make_Pragma_Argument_Association (Eloc,
2420 Chars => Name_Message,
2421 Expression =>
2422 Make_String_Literal (Eloc,
2423 Strval => "failed "
2424 & Get_Name_String (Pname)
2425 & " from "
2426 & Build_Location_String (Eloc))));
2427 end if;
2428
2429 Set_Is_Delayed_Aspect (Aspect);
2430
2431 -- For Pre/Post cases, insert immediately after the entity
2432 -- declaration, since that is the required pragma placement.
2433 -- Note that for these aspects, we do not have to worry
2434 -- about delay issues, since the pragmas themselves deal
2435 -- with delay of visibility for the expression analysis.
2436
2437 Insert_Delayed_Pragma (Aitem);
2438 goto Continue;
2439 end Pre_Post;
2440
2441 -- Test_Case
2442
2443 when Aspect_Test_Case => Test_Case : declare
2444 Args : List_Id;
2445 Comp_Expr : Node_Id;
2446 Comp_Assn : Node_Id;
2447 New_Expr : Node_Id;
2448
2449 begin
2450 Args := New_List;
2451
2452 if Nkind (Parent (N)) = N_Compilation_Unit then
2453 Error_Msg_Name_1 := Nam;
2454 Error_Msg_N ("incorrect placement of aspect `%`", E);
2455 goto Continue;
2456 end if;
2457
2458 if Nkind (Expr) /= N_Aggregate then
2459 Error_Msg_Name_1 := Nam;
2460 Error_Msg_NE
2461 ("wrong syntax for aspect `%` for &", Id, E);
2462 goto Continue;
2463 end if;
2464
2465 -- Make pragma expressions refer to the original aspect
2466 -- expressions through the Original_Node link. This is
2467 -- used in semantic analysis for ASIS mode, so that the
2468 -- original expression also gets analyzed.
2469
2470 Comp_Expr := First (Expressions (Expr));
2471 while Present (Comp_Expr) loop
2472 New_Expr := Relocate_Node (Comp_Expr);
2473 Set_Original_Node (New_Expr, Comp_Expr);
2474 Append_To (Args,
2475 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2476 Expression => New_Expr));
2477 Next (Comp_Expr);
2478 end loop;
2479
2480 Comp_Assn := First (Component_Associations (Expr));
2481 while Present (Comp_Assn) loop
2482 if List_Length (Choices (Comp_Assn)) /= 1
2483 or else
2484 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2485 then
2486 Error_Msg_Name_1 := Nam;
2487 Error_Msg_NE
2488 ("wrong syntax for aspect `%` for &", Id, E);
2489 goto Continue;
2490 end if;
2491
2492 New_Expr := Relocate_Node (Expression (Comp_Assn));
2493 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2494 Append_To (Args,
2495 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2496 Chars => Chars (First (Choices (Comp_Assn))),
2497 Expression => New_Expr));
2498 Next (Comp_Assn);
2499 end loop;
2500
2501 -- Build the test-case pragma
2502
2503 Make_Aitem_Pragma
2504 (Pragma_Argument_Associations => Args,
2505 Pragma_Name => Nam);
2506 end Test_Case;
2507
2508 -- Contract_Cases
2509
2510 when Aspect_Contract_Cases =>
2511 Make_Aitem_Pragma
2512 (Pragma_Argument_Associations => New_List (
2513 Make_Pragma_Argument_Association (Loc,
2514 Expression => Relocate_Node (Expr))),
2515 Pragma_Name => Nam);
2516
2517 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2518 Insert_Delayed_Pragma (Aitem);
2519 goto Continue;
2520
2521 -- Case 5: Special handling for aspects with an optional
2522 -- boolean argument.
2523
2524 -- In the general case, the corresponding pragma cannot be
2525 -- generated yet because the evaluation of the boolean needs
2526 -- to be delayed till the freeze point.
2527
2528 when Boolean_Aspects |
2529 Library_Unit_Aspects =>
2530
2531 Set_Is_Boolean_Aspect (Aspect);
2532
2533 -- Lock_Free aspect only apply to protected objects
2534
2535 if A_Id = Aspect_Lock_Free then
2536 if Ekind (E) /= E_Protected_Type then
2537 Error_Msg_Name_1 := Nam;
2538 Error_Msg_N
2539 ("aspect % only applies to a protected object",
2540 Aspect);
2541
2542 else
2543 -- Set the Uses_Lock_Free flag to True if there is no
2544 -- expression or if the expression is True. The
2545 -- evaluation of this aspect should be delayed to the
2546 -- freeze point (why???)
2547
2548 if No (Expr)
2549 or else Is_True (Static_Boolean (Expr))
2550 then
2551 Set_Uses_Lock_Free (E);
2552 end if;
2553
2554 Record_Rep_Item (E, Aspect);
2555 end if;
2556
2557 goto Continue;
2558
2559 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2560
2561 -- Verify that there is an aspect Convention that will
2562 -- incorporate the Import/Export aspect, and eventual
2563 -- Link/External names.
2564
2565 declare
2566 A : Node_Id;
2567
2568 begin
2569 A := First (L);
2570 while Present (A) loop
2571 exit when Chars (Identifier (A)) = Name_Convention;
2572 Next (A);
2573 end loop;
2574
2575 -- It is legal to specify Import for a variable, in
2576 -- order to suppress initialization for it, without
2577 -- specifying explicitly its convention. However this
2578 -- is only legal if the convention of the object type
2579 -- is Ada or similar.
2580
2581 if No (A) then
2582 if Ekind (E) = E_Variable
2583 and then A_Id = Aspect_Import
2584 then
2585 declare
2586 C : constant Convention_Id :=
2587 Convention (Etype (E));
2588 begin
2589 if C = Convention_Ada or else
2590 C = Convention_Ada_Pass_By_Copy or else
2591 C = Convention_Ada_Pass_By_Reference
2592 then
2593 goto Continue;
2594 end if;
2595 end;
2596 end if;
2597
2598 -- Otherwise, Convention must be specified
2599
2600 Error_Msg_N
2601 ("missing Convention aspect for Export/Import",
2602 Aspect);
2603 end if;
2604 end;
2605
2606 goto Continue;
2607 end if;
2608
2609 -- Library unit aspects require special handling in the case
2610 -- of a package declaration, the pragma needs to be inserted
2611 -- in the list of declarations for the associated package.
2612 -- There is no issue of visibility delay for these aspects.
2613
2614 if A_Id in Library_Unit_Aspects
2615 and then
2616 Nkind_In (N, N_Package_Declaration,
2617 N_Generic_Package_Declaration)
2618 and then Nkind (Parent (N)) /= N_Compilation_Unit
2619 then
2620 Error_Msg_N
2621 ("incorrect context for library unit aspect&", Id);
2622 goto Continue;
2623 end if;
2624
2625 -- Cases where we do not delay, includes all cases where
2626 -- the expression is missing other than the above cases.
2627
2628 if not Delay_Required or else No (Expr) then
2629 Make_Aitem_Pragma
2630 (Pragma_Argument_Associations => New_List (
2631 Make_Pragma_Argument_Association (Sloc (Ent),
2632 Expression => Ent)),
2633 Pragma_Name => Chars (Id));
2634 Delay_Required := False;
2635
2636 -- In general cases, the corresponding pragma/attribute
2637 -- definition clause will be inserted later at the freezing
2638 -- point, and we do not need to build it now
2639
2640 else
2641 Aitem := Empty;
2642 end if;
2643
2644 -- Storage_Size
2645
2646 -- This is special because for access types we need to generate
2647 -- an attribute definition clause. This also works for single
2648 -- task declarations, but it does not work for task type
2649 -- declarations, because we have the case where the expression
2650 -- references a discriminant of the task type. That can't use
2651 -- an attribute definition clause because we would not have
2652 -- visibility on the discriminant. For that case we must
2653 -- generate a pragma in the task definition.
2654
2655 when Aspect_Storage_Size =>
2656
2657 -- Task type case
2658
2659 if Ekind (E) = E_Task_Type then
2660 declare
2661 Decl : constant Node_Id := Declaration_Node (E);
2662
2663 begin
2664 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
2665
2666 -- If no task definition, create one
2667
2668 if No (Task_Definition (Decl)) then
2669 Set_Task_Definition (Decl,
2670 Make_Task_Definition (Loc,
2671 Visible_Declarations => Empty_List,
2672 End_Label => Empty));
2673 end if;
2674
2675 -- Create a pragma and put it at the start of the
2676 -- task definition for the task type declaration.
2677
2678 Make_Aitem_Pragma
2679 (Pragma_Argument_Associations => New_List (
2680 Make_Pragma_Argument_Association (Loc,
2681 Expression => Relocate_Node (Expr))),
2682 Pragma_Name => Name_Storage_Size);
2683
2684 Prepend
2685 (Aitem,
2686 Visible_Declarations (Task_Definition (Decl)));
2687 goto Continue;
2688 end;
2689
2690 -- All other cases, generate attribute definition
2691
2692 else
2693 Aitem :=
2694 Make_Attribute_Definition_Clause (Loc,
2695 Name => Ent,
2696 Chars => Chars (Id),
2697 Expression => Relocate_Node (Expr));
2698 end if;
2699 end case;
2700
2701 -- Attach the corresponding pragma/attribute definition clause to
2702 -- the aspect specification node.
2703
2704 if Present (Aitem) then
2705 Set_From_Aspect_Specification (Aitem, True);
2706 end if;
2707
2708 -- In the context of a compilation unit, we directly put the
2709 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
2710 -- node (no delay is required here) except for aspects on a
2711 -- subprogram body (see below) and a generic package, for which
2712 -- we need to introduce the pragma before building the generic
2713 -- copy (see sem_ch12), and for package instantiations, where
2714 -- the library unit pragmas are better handled early.
2715
2716 if Nkind (Parent (N)) = N_Compilation_Unit
2717 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
2718 then
2719 declare
2720 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2721
2722 begin
2723 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2724
2725 -- For a Boolean aspect, create the corresponding pragma if
2726 -- no expression or if the value is True.
2727
2728 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2729 if Is_True (Static_Boolean (Expr)) then
2730 Make_Aitem_Pragma
2731 (Pragma_Argument_Associations => New_List (
2732 Make_Pragma_Argument_Association (Sloc (Ent),
2733 Expression => Ent)),
2734 Pragma_Name => Chars (Id));
2735
2736 Set_From_Aspect_Specification (Aitem, True);
2737 Set_Corresponding_Aspect (Aitem, Aspect);
2738
2739 else
2740 goto Continue;
2741 end if;
2742 end if;
2743
2744 -- If the aspect is on a subprogram body (relevant aspect
2745 -- is Inline), add the pragma in front of the declarations.
2746
2747 if Nkind (N) = N_Subprogram_Body then
2748 if No (Declarations (N)) then
2749 Set_Declarations (N, New_List);
2750 end if;
2751
2752 Prepend (Aitem, Declarations (N));
2753
2754 elsif Nkind (N) = N_Generic_Package_Declaration then
2755 if No (Visible_Declarations (Specification (N))) then
2756 Set_Visible_Declarations (Specification (N), New_List);
2757 end if;
2758
2759 Prepend (Aitem,
2760 Visible_Declarations (Specification (N)));
2761
2762 elsif Nkind (N) = N_Package_Instantiation then
2763 declare
2764 Spec : constant Node_Id :=
2765 Specification (Instance_Spec (N));
2766 begin
2767 if No (Visible_Declarations (Spec)) then
2768 Set_Visible_Declarations (Spec, New_List);
2769 end if;
2770
2771 Prepend (Aitem, Visible_Declarations (Spec));
2772 end;
2773
2774 else
2775 if No (Pragmas_After (Aux)) then
2776 Set_Pragmas_After (Aux, New_List);
2777 end if;
2778
2779 Append (Aitem, Pragmas_After (Aux));
2780 end if;
2781
2782 goto Continue;
2783 end;
2784 end if;
2785
2786 -- The evaluation of the aspect is delayed to the freezing point.
2787 -- The pragma or attribute clause if there is one is then attached
2788 -- to the aspect specification which is put in the rep item list.
2789
2790 if Delay_Required then
2791 if Present (Aitem) then
2792 Set_Is_Delayed_Aspect (Aitem);
2793 Set_Aspect_Rep_Item (Aspect, Aitem);
2794 Set_Parent (Aitem, Aspect);
2795 end if;
2796
2797 Set_Is_Delayed_Aspect (Aspect);
2798
2799 -- In the case of Default_Value, link the aspect to base type
2800 -- as well, even though it appears on a first subtype. This is
2801 -- mandated by the semantics of the aspect. Do not establish
2802 -- the link when processing the base type itself as this leads
2803 -- to a rep item circularity. Verify that we are dealing with
2804 -- a scalar type to prevent cascaded errors.
2805
2806 if A_Id = Aspect_Default_Value
2807 and then Is_Scalar_Type (E)
2808 and then Base_Type (E) /= E
2809 then
2810 Set_Has_Delayed_Aspects (Base_Type (E));
2811 Record_Rep_Item (Base_Type (E), Aspect);
2812 end if;
2813
2814 Set_Has_Delayed_Aspects (E);
2815 Record_Rep_Item (E, Aspect);
2816
2817 -- When delay is not required and the context is a package or a
2818 -- subprogram body, insert the pragma in the body declarations.
2819
2820 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
2821 if No (Declarations (N)) then
2822 Set_Declarations (N, New_List);
2823 end if;
2824
2825 -- The pragma is added before source declarations
2826
2827 Prepend_To (Declarations (N), Aitem);
2828
2829 -- When delay is not required and the context is not a compilation
2830 -- unit, we simply insert the pragma/attribute definition clause
2831 -- in sequence.
2832
2833 else
2834 Insert_After (Ins_Node, Aitem);
2835 Ins_Node := Aitem;
2836 end if;
2837 end Analyze_One_Aspect;
2838
2839 <<Continue>>
2840 Next (Aspect);
2841 end loop Aspect_Loop;
2842
2843 if Has_Delayed_Aspects (E) then
2844 Ensure_Freeze_Node (E);
2845 end if;
2846 end Analyze_Aspect_Specifications;
2847
2848 -----------------------
2849 -- Analyze_At_Clause --
2850 -----------------------
2851
2852 -- An at clause is replaced by the corresponding Address attribute
2853 -- definition clause that is the preferred approach in Ada 95.
2854
2855 procedure Analyze_At_Clause (N : Node_Id) is
2856 CS : constant Boolean := Comes_From_Source (N);
2857
2858 begin
2859 -- This is an obsolescent feature
2860
2861 Check_Restriction (No_Obsolescent_Features, N);
2862
2863 if Warn_On_Obsolescent_Feature then
2864 Error_Msg_N
2865 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
2866 Error_Msg_N
2867 ("\?j?use address attribute definition clause instead", N);
2868 end if;
2869
2870 -- Rewrite as address clause
2871
2872 Rewrite (N,
2873 Make_Attribute_Definition_Clause (Sloc (N),
2874 Name => Identifier (N),
2875 Chars => Name_Address,
2876 Expression => Expression (N)));
2877
2878 -- We preserve Comes_From_Source, since logically the clause still comes
2879 -- from the source program even though it is changed in form.
2880
2881 Set_Comes_From_Source (N, CS);
2882
2883 -- Analyze rewritten clause
2884
2885 Analyze_Attribute_Definition_Clause (N);
2886 end Analyze_At_Clause;
2887
2888 -----------------------------------------
2889 -- Analyze_Attribute_Definition_Clause --
2890 -----------------------------------------
2891
2892 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2893 Loc : constant Source_Ptr := Sloc (N);
2894 Nam : constant Node_Id := Name (N);
2895 Attr : constant Name_Id := Chars (N);
2896 Expr : constant Node_Id := Expression (N);
2897 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2898
2899 Ent : Entity_Id;
2900 -- The entity of Nam after it is analyzed. In the case of an incomplete
2901 -- type, this is the underlying type.
2902
2903 U_Ent : Entity_Id;
2904 -- The underlying entity to which the attribute applies. Generally this
2905 -- is the Underlying_Type of Ent, except in the case where the clause
2906 -- applies to full view of incomplete type or private type in which case
2907 -- U_Ent is just a copy of Ent.
2908
2909 FOnly : Boolean := False;
2910 -- Reset to True for subtype specific attribute (Alignment, Size)
2911 -- and for stream attributes, i.e. those cases where in the call
2912 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2913 -- rules are checked. Note that the case of stream attributes is not
2914 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2915 -- disallow Storage_Size for derived task types, but that is also
2916 -- clearly unintentional.
2917
2918 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2919 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2920 -- definition clauses.
2921
2922 function Duplicate_Clause return Boolean;
2923 -- This routine checks if the aspect for U_Ent being given by attribute
2924 -- definition clause N is for an aspect that has already been specified,
2925 -- and if so gives an error message. If there is a duplicate, True is
2926 -- returned, otherwise if there is no error, False is returned.
2927
2928 procedure Check_Indexing_Functions;
2929 -- Check that the function in Constant_Indexing or Variable_Indexing
2930 -- attribute has the proper type structure. If the name is overloaded,
2931 -- check that some interpretation is legal.
2932
2933 procedure Check_Iterator_Functions;
2934 -- Check that there is a single function in Default_Iterator attribute
2935 -- has the proper type structure.
2936
2937 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2938 -- Common legality check for the previous two
2939
2940 -----------------------------------
2941 -- Analyze_Stream_TSS_Definition --
2942 -----------------------------------
2943
2944 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2945 Subp : Entity_Id := Empty;
2946 I : Interp_Index;
2947 It : Interp;
2948 Pnam : Entity_Id;
2949
2950 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2951 -- True for Read attribute, false for other attributes
2952
2953 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2954 -- Return true if the entity is a subprogram with an appropriate
2955 -- profile for the attribute being defined.
2956
2957 ----------------------
2958 -- Has_Good_Profile --
2959 ----------------------
2960
2961 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2962 F : Entity_Id;
2963 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2964 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2965 (False => E_Procedure, True => E_Function);
2966 Typ : Entity_Id;
2967
2968 begin
2969 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2970 return False;
2971 end if;
2972
2973 F := First_Formal (Subp);
2974
2975 if No (F)
2976 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2977 or else Designated_Type (Etype (F)) /=
2978 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2979 then
2980 return False;
2981 end if;
2982
2983 if not Is_Function then
2984 Next_Formal (F);
2985
2986 declare
2987 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2988 (False => E_In_Parameter,
2989 True => E_Out_Parameter);
2990 begin
2991 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2992 return False;
2993 end if;
2994 end;
2995
2996 Typ := Etype (F);
2997
2998 else
2999 Typ := Etype (Subp);
3000 end if;
3001
3002 return Base_Type (Typ) = Base_Type (Ent)
3003 and then No (Next_Formal (F));
3004 end Has_Good_Profile;
3005
3006 -- Start of processing for Analyze_Stream_TSS_Definition
3007
3008 begin
3009 FOnly := True;
3010
3011 if not Is_Type (U_Ent) then
3012 Error_Msg_N ("local name must be a subtype", Nam);
3013 return;
3014 end if;
3015
3016 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3017
3018 -- If Pnam is present, it can be either inherited from an ancestor
3019 -- type (in which case it is legal to redefine it for this type), or
3020 -- be a previous definition of the attribute for the same type (in
3021 -- which case it is illegal).
3022
3023 -- In the first case, it will have been analyzed already, and we
3024 -- can check that its profile does not match the expected profile
3025 -- for a stream attribute of U_Ent. In the second case, either Pnam
3026 -- has been analyzed (and has the expected profile), or it has not
3027 -- been analyzed yet (case of a type that has not been frozen yet
3028 -- and for which the stream attribute has been set using Set_TSS).
3029
3030 if Present (Pnam)
3031 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3032 then
3033 Error_Msg_Sloc := Sloc (Pnam);
3034 Error_Msg_Name_1 := Attr;
3035 Error_Msg_N ("% attribute already defined #", Nam);
3036 return;
3037 end if;
3038
3039 Analyze (Expr);
3040
3041 if Is_Entity_Name (Expr) then
3042 if not Is_Overloaded (Expr) then
3043 if Has_Good_Profile (Entity (Expr)) then
3044 Subp := Entity (Expr);
3045 end if;
3046
3047 else
3048 Get_First_Interp (Expr, I, It);
3049 while Present (It.Nam) loop
3050 if Has_Good_Profile (It.Nam) then
3051 Subp := It.Nam;
3052 exit;
3053 end if;
3054
3055 Get_Next_Interp (I, It);
3056 end loop;
3057 end if;
3058 end if;
3059
3060 if Present (Subp) then
3061 if Is_Abstract_Subprogram (Subp) then
3062 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3063 return;
3064 end if;
3065
3066 Set_Entity (Expr, Subp);
3067 Set_Etype (Expr, Etype (Subp));
3068
3069 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3070
3071 else
3072 Error_Msg_Name_1 := Attr;
3073 Error_Msg_N ("incorrect expression for% attribute", Expr);
3074 end if;
3075 end Analyze_Stream_TSS_Definition;
3076
3077 ------------------------------
3078 -- Check_Indexing_Functions --
3079 ------------------------------
3080
3081 procedure Check_Indexing_Functions is
3082 Indexing_Found : Boolean;
3083
3084 procedure Check_One_Function (Subp : Entity_Id);
3085 -- Check one possible interpretation. Sets Indexing_Found True if an
3086 -- indexing function is found.
3087
3088 ------------------------
3089 -- Check_One_Function --
3090 ------------------------
3091
3092 procedure Check_One_Function (Subp : Entity_Id) is
3093 Default_Element : constant Node_Id :=
3094 Find_Value_Of_Aspect
3095 (Etype (First_Formal (Subp)),
3096 Aspect_Iterator_Element);
3097
3098 begin
3099 if not Check_Primitive_Function (Subp)
3100 and then not Is_Overloaded (Expr)
3101 then
3102 Error_Msg_NE
3103 ("aspect Indexing requires a function that applies to type&",
3104 Subp, Ent);
3105 end if;
3106
3107 -- An indexing function must return either the default element of
3108 -- the container, or a reference type. For variable indexing it
3109 -- must be the latter.
3110
3111 if Present (Default_Element) then
3112 Analyze (Default_Element);
3113
3114 if Is_Entity_Name (Default_Element)
3115 and then Covers (Entity (Default_Element), Etype (Subp))
3116 then
3117 Indexing_Found := True;
3118 return;
3119 end if;
3120 end if;
3121
3122 -- For variable_indexing the return type must be a reference type
3123
3124 if Attr = Name_Variable_Indexing
3125 and then not Has_Implicit_Dereference (Etype (Subp))
3126 then
3127 Error_Msg_N
3128 ("function for indexing must return a reference type", Subp);
3129
3130 else
3131 Indexing_Found := True;
3132 end if;
3133 end Check_One_Function;
3134
3135 -- Start of processing for Check_Indexing_Functions
3136
3137 begin
3138 if In_Instance then
3139 return;
3140 end if;
3141
3142 Analyze (Expr);
3143
3144 if not Is_Overloaded (Expr) then
3145 Check_One_Function (Entity (Expr));
3146
3147 else
3148 declare
3149 I : Interp_Index;
3150 It : Interp;
3151
3152 begin
3153 Indexing_Found := False;
3154 Get_First_Interp (Expr, I, It);
3155 while Present (It.Nam) loop
3156
3157 -- Note that analysis will have added the interpretation
3158 -- that corresponds to the dereference. We only check the
3159 -- subprogram itself.
3160
3161 if Is_Overloadable (It.Nam) then
3162 Check_One_Function (It.Nam);
3163 end if;
3164
3165 Get_Next_Interp (I, It);
3166 end loop;
3167
3168 if not Indexing_Found then
3169 Error_Msg_NE
3170 ("aspect Indexing requires a function that "
3171 & "applies to type&", Expr, Ent);
3172 end if;
3173 end;
3174 end if;
3175 end Check_Indexing_Functions;
3176
3177 ------------------------------
3178 -- Check_Iterator_Functions --
3179 ------------------------------
3180
3181 procedure Check_Iterator_Functions is
3182 Default : Entity_Id;
3183
3184 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3185 -- Check one possible interpretation for validity
3186
3187 ----------------------------
3188 -- Valid_Default_Iterator --
3189 ----------------------------
3190
3191 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3192 Formal : Entity_Id;
3193
3194 begin
3195 if not Check_Primitive_Function (Subp) then
3196 return False;
3197 else
3198 Formal := First_Formal (Subp);
3199 end if;
3200
3201 -- False if any subsequent formal has no default expression
3202
3203 Formal := Next_Formal (Formal);
3204 while Present (Formal) loop
3205 if No (Expression (Parent (Formal))) then
3206 return False;
3207 end if;
3208
3209 Next_Formal (Formal);
3210 end loop;
3211
3212 -- True if all subsequent formals have default expressions
3213
3214 return True;
3215 end Valid_Default_Iterator;
3216
3217 -- Start of processing for Check_Iterator_Functions
3218
3219 begin
3220 Analyze (Expr);
3221
3222 if not Is_Entity_Name (Expr) then
3223 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3224 end if;
3225
3226 if not Is_Overloaded (Expr) then
3227 if not Check_Primitive_Function (Entity (Expr)) then
3228 Error_Msg_NE
3229 ("aspect Indexing requires a function that applies to type&",
3230 Entity (Expr), Ent);
3231 end if;
3232
3233 if not Valid_Default_Iterator (Entity (Expr)) then
3234 Error_Msg_N ("improper function for default iterator", Expr);
3235 end if;
3236
3237 else
3238 Default := Empty;
3239 declare
3240 I : Interp_Index;
3241 It : Interp;
3242
3243 begin
3244 Get_First_Interp (Expr, I, It);
3245 while Present (It.Nam) loop
3246 if not Check_Primitive_Function (It.Nam)
3247 or else not Valid_Default_Iterator (It.Nam)
3248 then
3249 Remove_Interp (I);
3250
3251 elsif Present (Default) then
3252 Error_Msg_N ("default iterator must be unique", Expr);
3253
3254 else
3255 Default := It.Nam;
3256 end if;
3257
3258 Get_Next_Interp (I, It);
3259 end loop;
3260 end;
3261
3262 if Present (Default) then
3263 Set_Entity (Expr, Default);
3264 Set_Is_Overloaded (Expr, False);
3265 end if;
3266 end if;
3267 end Check_Iterator_Functions;
3268
3269 -------------------------------
3270 -- Check_Primitive_Function --
3271 -------------------------------
3272
3273 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3274 Ctrl : Entity_Id;
3275
3276 begin
3277 if Ekind (Subp) /= E_Function then
3278 return False;
3279 end if;
3280
3281 if No (First_Formal (Subp)) then
3282 return False;
3283 else
3284 Ctrl := Etype (First_Formal (Subp));
3285 end if;
3286
3287 if Ctrl = Ent
3288 or else Ctrl = Class_Wide_Type (Ent)
3289 or else
3290 (Ekind (Ctrl) = E_Anonymous_Access_Type
3291 and then
3292 (Designated_Type (Ctrl) = Ent
3293 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3294 then
3295 null;
3296
3297 else
3298 return False;
3299 end if;
3300
3301 return True;
3302 end Check_Primitive_Function;
3303
3304 ----------------------
3305 -- Duplicate_Clause --
3306 ----------------------
3307
3308 function Duplicate_Clause return Boolean is
3309 A : Node_Id;
3310
3311 begin
3312 -- Nothing to do if this attribute definition clause comes from
3313 -- an aspect specification, since we could not be duplicating an
3314 -- explicit clause, and we dealt with the case of duplicated aspects
3315 -- in Analyze_Aspect_Specifications.
3316
3317 if From_Aspect_Specification (N) then
3318 return False;
3319 end if;
3320
3321 -- Otherwise current clause may duplicate previous clause, or a
3322 -- previously given pragma or aspect specification for the same
3323 -- aspect.
3324
3325 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3326
3327 if Present (A) then
3328 Error_Msg_Name_1 := Chars (N);
3329 Error_Msg_Sloc := Sloc (A);
3330
3331 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3332 return True;
3333 end if;
3334
3335 return False;
3336 end Duplicate_Clause;
3337
3338 -- Start of processing for Analyze_Attribute_Definition_Clause
3339
3340 begin
3341 -- The following code is a defense against recursion. Not clear that
3342 -- this can happen legitimately, but perhaps some error situations
3343 -- can cause it, and we did see this recursion during testing.
3344
3345 if Analyzed (N) then
3346 return;
3347 else
3348 Set_Analyzed (N, True);
3349 end if;
3350
3351 -- Ignore some selected attributes in CodePeer mode since they are not
3352 -- relevant in this context.
3353
3354 if CodePeer_Mode then
3355 case Id is
3356
3357 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3358 -- internal representation of types by implicitly packing them.
3359
3360 when Attribute_Component_Size =>
3361 Rewrite (N, Make_Null_Statement (Sloc (N)));
3362 return;
3363
3364 when others =>
3365 null;
3366 end case;
3367 end if;
3368
3369 -- Process Ignore_Rep_Clauses option
3370
3371 if Ignore_Rep_Clauses then
3372 case Id is
3373
3374 -- The following should be ignored. They do not affect legality
3375 -- and may be target dependent. The basic idea of -gnatI is to
3376 -- ignore any rep clauses that may be target dependent but do not
3377 -- affect legality (except possibly to be rejected because they
3378 -- are incompatible with the compilation target).
3379
3380 when Attribute_Alignment |
3381 Attribute_Bit_Order |
3382 Attribute_Component_Size |
3383 Attribute_Machine_Radix |
3384 Attribute_Object_Size |
3385 Attribute_Size |
3386 Attribute_Stream_Size |
3387 Attribute_Value_Size =>
3388 Rewrite (N, Make_Null_Statement (Sloc (N)));
3389 return;
3390
3391 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
3392
3393 when Attribute_Small =>
3394 if Ignore_Rep_Clauses then
3395 Rewrite (N, Make_Null_Statement (Sloc (N)));
3396 return;
3397 end if;
3398
3399 -- The following should not be ignored, because in the first place
3400 -- they are reasonably portable, and should not cause problems in
3401 -- compiling code from another target, and also they do affect
3402 -- legality, e.g. failing to provide a stream attribute for a
3403 -- type may make a program illegal.
3404
3405 when Attribute_External_Tag |
3406 Attribute_Input |
3407 Attribute_Output |
3408 Attribute_Read |
3409 Attribute_Simple_Storage_Pool |
3410 Attribute_Storage_Pool |
3411 Attribute_Storage_Size |
3412 Attribute_Write =>
3413 null;
3414
3415 -- Other cases are errors ("attribute& cannot be set with
3416 -- definition clause"), which will be caught below.
3417
3418 when others =>
3419 null;
3420 end case;
3421 end if;
3422
3423 Analyze (Nam);
3424 Ent := Entity (Nam);
3425
3426 if Rep_Item_Too_Early (Ent, N) then
3427 return;
3428 end if;
3429
3430 -- Rep clause applies to full view of incomplete type or private type if
3431 -- we have one (if not, this is a premature use of the type). However,
3432 -- certain semantic checks need to be done on the specified entity (i.e.
3433 -- the private view), so we save it in Ent.
3434
3435 if Is_Private_Type (Ent)
3436 and then Is_Derived_Type (Ent)
3437 and then not Is_Tagged_Type (Ent)
3438 and then No (Full_View (Ent))
3439 then
3440 -- If this is a private type whose completion is a derivation from
3441 -- another private type, there is no full view, and the attribute
3442 -- belongs to the type itself, not its underlying parent.
3443
3444 U_Ent := Ent;
3445
3446 elsif Ekind (Ent) = E_Incomplete_Type then
3447
3448 -- The attribute applies to the full view, set the entity of the
3449 -- attribute definition accordingly.
3450
3451 Ent := Underlying_Type (Ent);
3452 U_Ent := Ent;
3453 Set_Entity (Nam, Ent);
3454
3455 else
3456 U_Ent := Underlying_Type (Ent);
3457 end if;
3458
3459 -- Avoid cascaded error
3460
3461 if Etype (Nam) = Any_Type then
3462 return;
3463
3464 -- Must be declared in current scope or in case of an aspect
3465 -- specification, must be visible in current scope.
3466
3467 elsif Scope (Ent) /= Current_Scope
3468 and then
3469 not (From_Aspect_Specification (N)
3470 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
3471 then
3472 Error_Msg_N ("entity must be declared in this scope", Nam);
3473 return;
3474
3475 -- Must not be a source renaming (we do have some cases where the
3476 -- expander generates a renaming, and those cases are OK, in such
3477 -- cases any attribute applies to the renamed object as well).
3478
3479 elsif Is_Object (Ent)
3480 and then Present (Renamed_Object (Ent))
3481 then
3482 -- Case of renamed object from source, this is an error
3483
3484 if Comes_From_Source (Renamed_Object (Ent)) then
3485 Get_Name_String (Chars (N));
3486 Error_Msg_Strlen := Name_Len;
3487 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3488 Error_Msg_N
3489 ("~ clause not allowed for a renaming declaration "
3490 & "(RM 13.1(6))", Nam);
3491 return;
3492
3493 -- For the case of a compiler generated renaming, the attribute
3494 -- definition clause applies to the renamed object created by the
3495 -- expander. The easiest general way to handle this is to create a
3496 -- copy of the attribute definition clause for this object.
3497
3498 else
3499 Insert_Action (N,
3500 Make_Attribute_Definition_Clause (Loc,
3501 Name =>
3502 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
3503 Chars => Chars (N),
3504 Expression => Duplicate_Subexpr (Expression (N))));
3505 end if;
3506
3507 -- If no underlying entity, use entity itself, applies to some
3508 -- previously detected error cases ???
3509
3510 elsif No (U_Ent) then
3511 U_Ent := Ent;
3512
3513 -- Cannot specify for a subtype (exception Object/Value_Size)
3514
3515 elsif Is_Type (U_Ent)
3516 and then not Is_First_Subtype (U_Ent)
3517 and then Id /= Attribute_Object_Size
3518 and then Id /= Attribute_Value_Size
3519 and then not From_At_Mod (N)
3520 then
3521 Error_Msg_N ("cannot specify attribute for subtype", Nam);
3522 return;
3523 end if;
3524
3525 Set_Entity (N, U_Ent);
3526 Check_Restriction_No_Use_Of_Attribute (N);
3527
3528 -- Switch on particular attribute
3529
3530 case Id is
3531
3532 -------------
3533 -- Address --
3534 -------------
3535
3536 -- Address attribute definition clause
3537
3538 when Attribute_Address => Address : begin
3539
3540 -- A little error check, catch for X'Address use X'Address;
3541
3542 if Nkind (Nam) = N_Identifier
3543 and then Nkind (Expr) = N_Attribute_Reference
3544 and then Attribute_Name (Expr) = Name_Address
3545 and then Nkind (Prefix (Expr)) = N_Identifier
3546 and then Chars (Nam) = Chars (Prefix (Expr))
3547 then
3548 Error_Msg_NE
3549 ("address for & is self-referencing", Prefix (Expr), Ent);
3550 return;
3551 end if;
3552
3553 -- Not that special case, carry on with analysis of expression
3554
3555 Analyze_And_Resolve (Expr, RTE (RE_Address));
3556
3557 -- Even when ignoring rep clauses we need to indicate that the
3558 -- entity has an address clause and thus it is legal to declare
3559 -- it imported.
3560
3561 if Ignore_Rep_Clauses then
3562 if Ekind_In (U_Ent, E_Variable, E_Constant) then
3563 Record_Rep_Item (U_Ent, N);
3564 end if;
3565
3566 return;
3567 end if;
3568
3569 if Duplicate_Clause then
3570 null;
3571
3572 -- Case of address clause for subprogram
3573
3574 elsif Is_Subprogram (U_Ent) then
3575 if Has_Homonym (U_Ent) then
3576 Error_Msg_N
3577 ("address clause cannot be given " &
3578 "for overloaded subprogram",
3579 Nam);
3580 return;
3581 end if;
3582
3583 -- For subprograms, all address clauses are permitted, and we
3584 -- mark the subprogram as having a deferred freeze so that Gigi
3585 -- will not elaborate it too soon.
3586
3587 -- Above needs more comments, what is too soon about???
3588
3589 Set_Has_Delayed_Freeze (U_Ent);
3590
3591 -- Case of address clause for entry
3592
3593 elsif Ekind (U_Ent) = E_Entry then
3594 if Nkind (Parent (N)) = N_Task_Body then
3595 Error_Msg_N
3596 ("entry address must be specified in task spec", Nam);
3597 return;
3598 end if;
3599
3600 -- For entries, we require a constant address
3601
3602 Check_Constant_Address_Clause (Expr, U_Ent);
3603
3604 -- Special checks for task types
3605
3606 if Is_Task_Type (Scope (U_Ent))
3607 and then Comes_From_Source (Scope (U_Ent))
3608 then
3609 Error_Msg_N
3610 ("??entry address declared for entry in task type", N);
3611 Error_Msg_N
3612 ("\??only one task can be declared of this type", N);
3613 end if;
3614
3615 -- Entry address clauses are obsolescent
3616
3617 Check_Restriction (No_Obsolescent_Features, N);
3618
3619 if Warn_On_Obsolescent_Feature then
3620 Error_Msg_N
3621 ("?j?attaching interrupt to task entry is an " &
3622 "obsolescent feature (RM J.7.1)", N);
3623 Error_Msg_N
3624 ("\?j?use interrupt procedure instead", N);
3625 end if;
3626
3627 -- Case of an address clause for a controlled object which we
3628 -- consider to be erroneous.
3629
3630 elsif Is_Controlled (Etype (U_Ent))
3631 or else Has_Controlled_Component (Etype (U_Ent))
3632 then
3633 Error_Msg_NE
3634 ("??controlled object& must not be overlaid", Nam, U_Ent);
3635 Error_Msg_N
3636 ("\??Program_Error will be raised at run time", Nam);
3637 Insert_Action (Declaration_Node (U_Ent),
3638 Make_Raise_Program_Error (Loc,
3639 Reason => PE_Overlaid_Controlled_Object));
3640 return;
3641
3642 -- Case of address clause for a (non-controlled) object
3643
3644 elsif
3645 Ekind (U_Ent) = E_Variable
3646 or else
3647 Ekind (U_Ent) = E_Constant
3648 then
3649 declare
3650 Expr : constant Node_Id := Expression (N);
3651 O_Ent : Entity_Id;
3652 Off : Boolean;
3653
3654 begin
3655 -- Exported variables cannot have an address clause, because
3656 -- this cancels the effect of the pragma Export.
3657
3658 if Is_Exported (U_Ent) then
3659 Error_Msg_N
3660 ("cannot export object with address clause", Nam);
3661 return;
3662 end if;
3663
3664 Find_Overlaid_Entity (N, O_Ent, Off);
3665
3666 -- Overlaying controlled objects is erroneous
3667
3668 if Present (O_Ent)
3669 and then (Has_Controlled_Component (Etype (O_Ent))
3670 or else Is_Controlled (Etype (O_Ent)))
3671 then
3672 Error_Msg_N
3673 ("??cannot overlay with controlled object", Expr);
3674 Error_Msg_N
3675 ("\??Program_Error will be raised at run time", Expr);
3676 Insert_Action (Declaration_Node (U_Ent),
3677 Make_Raise_Program_Error (Loc,
3678 Reason => PE_Overlaid_Controlled_Object));
3679 return;
3680
3681 elsif Present (O_Ent)
3682 and then Ekind (U_Ent) = E_Constant
3683 and then not Is_Constant_Object (O_Ent)
3684 then
3685 Error_Msg_N ("??constant overlays a variable", Expr);
3686
3687 -- Imported variables can have an address clause, but then
3688 -- the import is pretty meaningless except to suppress
3689 -- initializations, so we do not need such variables to
3690 -- be statically allocated (and in fact it causes trouble
3691 -- if the address clause is a local value).
3692
3693 elsif Is_Imported (U_Ent) then
3694 Set_Is_Statically_Allocated (U_Ent, False);
3695 end if;
3696
3697 -- We mark a possible modification of a variable with an
3698 -- address clause, since it is likely aliasing is occurring.
3699
3700 Note_Possible_Modification (Nam, Sure => False);
3701
3702 -- Here we are checking for explicit overlap of one variable
3703 -- by another, and if we find this then mark the overlapped
3704 -- variable as also being volatile to prevent unwanted
3705 -- optimizations. This is a significant pessimization so
3706 -- avoid it when there is an offset, i.e. when the object
3707 -- is composite; they cannot be optimized easily anyway.
3708
3709 if Present (O_Ent)
3710 and then Is_Object (O_Ent)
3711 and then not Off
3712
3713 -- The following test is an expedient solution to what
3714 -- is really a problem in CodePeer. Suppressing the
3715 -- Set_Treat_As_Volatile call here prevents later
3716 -- generation (in some cases) of trees that CodePeer
3717 -- should, but currently does not, handle correctly.
3718 -- This test should probably be removed when CodePeer
3719 -- is improved, just because we want the tree CodePeer
3720 -- analyzes to match the tree for which we generate code
3721 -- as closely as is practical. ???
3722
3723 and then not CodePeer_Mode
3724 then
3725 -- ??? O_Ent might not be in current unit
3726
3727 Set_Treat_As_Volatile (O_Ent);
3728 end if;
3729
3730 -- Legality checks on the address clause for initialized
3731 -- objects is deferred until the freeze point, because
3732 -- a subsequent pragma might indicate that the object
3733 -- is imported and thus not initialized. Also, the address
3734 -- clause might involve entities that have yet to be
3735 -- elaborated.
3736
3737 Set_Has_Delayed_Freeze (U_Ent);
3738
3739 -- If an initialization call has been generated for this
3740 -- object, it needs to be deferred to after the freeze node
3741 -- we have just now added, otherwise GIGI will see a
3742 -- reference to the variable (as actual to the IP call)
3743 -- before its definition.
3744
3745 declare
3746 Init_Call : constant Node_Id :=
3747 Remove_Init_Call (U_Ent, N);
3748
3749 begin
3750 if Present (Init_Call) then
3751
3752 -- If the init call is an expression with actions with
3753 -- null expression, just extract the actions.
3754
3755 if Nkind (Init_Call) = N_Expression_With_Actions
3756 and then
3757 Nkind (Expression (Init_Call)) = N_Null_Statement
3758 then
3759 Append_Freeze_Actions (U_Ent, Actions (Init_Call));
3760
3761 -- General case: move Init_Call to freeze actions
3762
3763 else
3764 Append_Freeze_Action (U_Ent, Init_Call);
3765 end if;
3766 end if;
3767 end;
3768
3769 if Is_Exported (U_Ent) then
3770 Error_Msg_N
3771 ("& cannot be exported if an address clause is given",
3772 Nam);
3773 Error_Msg_N
3774 ("\define and export a variable "
3775 & "that holds its address instead", Nam);
3776 end if;
3777
3778 -- Entity has delayed freeze, so we will generate an
3779 -- alignment check at the freeze point unless suppressed.
3780
3781 if not Range_Checks_Suppressed (U_Ent)
3782 and then not Alignment_Checks_Suppressed (U_Ent)
3783 then
3784 Set_Check_Address_Alignment (N);
3785 end if;
3786
3787 -- Kill the size check code, since we are not allocating
3788 -- the variable, it is somewhere else.
3789
3790 Kill_Size_Check_Code (U_Ent);
3791
3792 -- If the address clause is of the form:
3793
3794 -- for Y'Address use X'Address
3795
3796 -- or
3797
3798 -- Const : constant Address := X'Address;
3799 -- ...
3800 -- for Y'Address use Const;
3801
3802 -- then we make an entry in the table for checking the size
3803 -- and alignment of the overlaying variable. We defer this
3804 -- check till after code generation to take full advantage
3805 -- of the annotation done by the back end.
3806
3807 -- If the entity has a generic type, the check will be
3808 -- performed in the instance if the actual type justifies
3809 -- it, and we do not insert the clause in the table to
3810 -- prevent spurious warnings.
3811
3812 -- Note: we used to test Comes_From_Source and only give
3813 -- this warning for source entities, but we have removed
3814 -- this test. It really seems bogus to generate overlays
3815 -- that would trigger this warning in generated code.
3816 -- Furthermore, by removing the test, we handle the
3817 -- aspect case properly.
3818
3819 if Address_Clause_Overlay_Warnings
3820 and then Present (O_Ent)
3821 and then Is_Object (O_Ent)
3822 then
3823 if not Is_Generic_Type (Etype (U_Ent)) then
3824 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3825 end if;
3826
3827 -- If variable overlays a constant view, and we are
3828 -- warning on overlays, then mark the variable as
3829 -- overlaying a constant (we will give warnings later
3830 -- if this variable is assigned).
3831
3832 if Is_Constant_Object (O_Ent)
3833 and then Ekind (U_Ent) = E_Variable
3834 then
3835 Set_Overlays_Constant (U_Ent);
3836 end if;
3837 end if;
3838 end;
3839
3840 -- Not a valid entity for an address clause
3841
3842 else
3843 Error_Msg_N ("address cannot be given for &", Nam);
3844 end if;
3845 end Address;
3846
3847 ---------------
3848 -- Alignment --
3849 ---------------
3850
3851 -- Alignment attribute definition clause
3852
3853 when Attribute_Alignment => Alignment : declare
3854 Align : constant Uint := Get_Alignment_Value (Expr);
3855 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3856
3857 begin
3858 FOnly := True;
3859
3860 if not Is_Type (U_Ent)
3861 and then Ekind (U_Ent) /= E_Variable
3862 and then Ekind (U_Ent) /= E_Constant
3863 then
3864 Error_Msg_N ("alignment cannot be given for &", Nam);
3865
3866 elsif Duplicate_Clause then
3867 null;
3868
3869 elsif Align /= No_Uint then
3870 Set_Has_Alignment_Clause (U_Ent);
3871
3872 -- Tagged type case, check for attempt to set alignment to a
3873 -- value greater than Max_Align, and reset if so.
3874
3875 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3876 Error_Msg_N
3877 ("alignment for & set to Maximum_Aligment??", Nam);
3878 Set_Alignment (U_Ent, Max_Align);
3879
3880 -- All other cases
3881
3882 else
3883 Set_Alignment (U_Ent, Align);
3884 end if;
3885
3886 -- For an array type, U_Ent is the first subtype. In that case,
3887 -- also set the alignment of the anonymous base type so that
3888 -- other subtypes (such as the itypes for aggregates of the
3889 -- type) also receive the expected alignment.
3890
3891 if Is_Array_Type (U_Ent) then
3892 Set_Alignment (Base_Type (U_Ent), Align);
3893 end if;
3894 end if;
3895 end Alignment;
3896
3897 ---------------
3898 -- Bit_Order --
3899 ---------------
3900
3901 -- Bit_Order attribute definition clause
3902
3903 when Attribute_Bit_Order => Bit_Order : declare
3904 begin
3905 if not Is_Record_Type (U_Ent) then
3906 Error_Msg_N
3907 ("Bit_Order can only be defined for record type", Nam);
3908
3909 elsif Duplicate_Clause then
3910 null;
3911
3912 else
3913 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3914
3915 if Etype (Expr) = Any_Type then
3916 return;
3917
3918 elsif not Is_Static_Expression (Expr) then
3919 Flag_Non_Static_Expr
3920 ("Bit_Order requires static expression!", Expr);
3921
3922 else
3923 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3924 Set_Reverse_Bit_Order (U_Ent, True);
3925 end if;
3926 end if;
3927 end if;
3928 end Bit_Order;
3929
3930 --------------------
3931 -- Component_Size --
3932 --------------------
3933
3934 -- Component_Size attribute definition clause
3935
3936 when Attribute_Component_Size => Component_Size_Case : declare
3937 Csize : constant Uint := Static_Integer (Expr);
3938 Ctyp : Entity_Id;
3939 Btype : Entity_Id;
3940 Biased : Boolean;
3941 New_Ctyp : Entity_Id;
3942 Decl : Node_Id;
3943
3944 begin
3945 if not Is_Array_Type (U_Ent) then
3946 Error_Msg_N ("component size requires array type", Nam);
3947 return;
3948 end if;
3949
3950 Btype := Base_Type (U_Ent);
3951 Ctyp := Component_Type (Btype);
3952
3953 if Duplicate_Clause then
3954 null;
3955
3956 elsif Rep_Item_Too_Early (Btype, N) then
3957 null;
3958
3959 elsif Csize /= No_Uint then
3960 Check_Size (Expr, Ctyp, Csize, Biased);
3961
3962 -- For the biased case, build a declaration for a subtype that
3963 -- will be used to represent the biased subtype that reflects
3964 -- the biased representation of components. We need the subtype
3965 -- to get proper conversions on referencing elements of the
3966 -- array. Note: component size clauses are ignored in VM mode.
3967
3968 if VM_Target = No_VM then
3969 if Biased then
3970 New_Ctyp :=
3971 Make_Defining_Identifier (Loc,
3972 Chars =>
3973 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3974
3975 Decl :=
3976 Make_Subtype_Declaration (Loc,
3977 Defining_Identifier => New_Ctyp,
3978 Subtype_Indication =>
3979 New_Occurrence_Of (Component_Type (Btype), Loc));
3980
3981 Set_Parent (Decl, N);
3982 Analyze (Decl, Suppress => All_Checks);
3983
3984 Set_Has_Delayed_Freeze (New_Ctyp, False);
3985 Set_Esize (New_Ctyp, Csize);
3986 Set_RM_Size (New_Ctyp, Csize);
3987 Init_Alignment (New_Ctyp);
3988 Set_Is_Itype (New_Ctyp, True);
3989 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3990
3991 Set_Component_Type (Btype, New_Ctyp);
3992 Set_Biased (New_Ctyp, N, "component size clause");
3993 end if;
3994
3995 Set_Component_Size (Btype, Csize);
3996
3997 -- For VM case, we ignore component size clauses
3998
3999 else
4000 -- Give a warning unless we are in GNAT mode, in which case
4001 -- the warning is suppressed since it is not useful.
4002
4003 if not GNAT_Mode then
4004 Error_Msg_N
4005 ("component size ignored in this configuration??", N);
4006 end if;
4007 end if;
4008
4009 -- Deal with warning on overridden size
4010
4011 if Warn_On_Overridden_Size
4012 and then Has_Size_Clause (Ctyp)
4013 and then RM_Size (Ctyp) /= Csize
4014 then
4015 Error_Msg_NE
4016 ("component size overrides size clause for&?S?", N, Ctyp);
4017 end if;
4018
4019 Set_Has_Component_Size_Clause (Btype, True);
4020 Set_Has_Non_Standard_Rep (Btype, True);
4021 end if;
4022 end Component_Size_Case;
4023
4024 -----------------------
4025 -- Constant_Indexing --
4026 -----------------------
4027
4028 when Attribute_Constant_Indexing =>
4029 Check_Indexing_Functions;
4030
4031 ---------
4032 -- CPU --
4033 ---------
4034
4035 when Attribute_CPU => CPU :
4036 begin
4037 -- CPU attribute definition clause not allowed except from aspect
4038 -- specification.
4039
4040 if From_Aspect_Specification (N) then
4041 if not Is_Task_Type (U_Ent) then
4042 Error_Msg_N ("CPU can only be defined for task", Nam);
4043
4044 elsif Duplicate_Clause then
4045 null;
4046
4047 else
4048 -- The expression must be analyzed in the special manner
4049 -- described in "Handling of Default and Per-Object
4050 -- Expressions" in sem.ads.
4051
4052 -- The visibility to the discriminants must be restored
4053
4054 Push_Scope_And_Install_Discriminants (U_Ent);
4055 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4056 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4057
4058 if not Is_Static_Expression (Expr) then
4059 Check_Restriction (Static_Priorities, Expr);
4060 end if;
4061 end if;
4062
4063 else
4064 Error_Msg_N
4065 ("attribute& cannot be set with definition clause", N);
4066 end if;
4067 end CPU;
4068
4069 ----------------------
4070 -- Default_Iterator --
4071 ----------------------
4072
4073 when Attribute_Default_Iterator => Default_Iterator : declare
4074 Func : Entity_Id;
4075
4076 begin
4077 if not Is_Tagged_Type (U_Ent) then
4078 Error_Msg_N
4079 ("aspect Default_Iterator applies to tagged type", Nam);
4080 end if;
4081
4082 Check_Iterator_Functions;
4083
4084 Analyze (Expr);
4085
4086 if not Is_Entity_Name (Expr)
4087 or else Ekind (Entity (Expr)) /= E_Function
4088 then
4089 Error_Msg_N ("aspect Iterator must be a function", Expr);
4090 else
4091 Func := Entity (Expr);
4092 end if;
4093
4094 if No (First_Formal (Func))
4095 or else Etype (First_Formal (Func)) /= U_Ent
4096 then
4097 Error_Msg_NE
4098 ("Default Iterator must be a primitive of&", Func, U_Ent);
4099 end if;
4100 end Default_Iterator;
4101
4102 ------------------------
4103 -- Dispatching_Domain --
4104 ------------------------
4105
4106 when Attribute_Dispatching_Domain => Dispatching_Domain :
4107 begin
4108 -- Dispatching_Domain attribute definition clause not allowed
4109 -- except from aspect specification.
4110
4111 if From_Aspect_Specification (N) then
4112 if not Is_Task_Type (U_Ent) then
4113 Error_Msg_N ("Dispatching_Domain can only be defined" &
4114 "for task",
4115 Nam);
4116
4117 elsif Duplicate_Clause then
4118 null;
4119
4120 else
4121 -- The expression must be analyzed in the special manner
4122 -- described in "Handling of Default and Per-Object
4123 -- Expressions" in sem.ads.
4124
4125 -- The visibility to the discriminants must be restored
4126
4127 Push_Scope_And_Install_Discriminants (U_Ent);
4128
4129 Preanalyze_Spec_Expression
4130 (Expr, RTE (RE_Dispatching_Domain));
4131
4132 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4133 end if;
4134
4135 else
4136 Error_Msg_N
4137 ("attribute& cannot be set with definition clause", N);
4138 end if;
4139 end Dispatching_Domain;
4140
4141 ------------------
4142 -- External_Tag --
4143 ------------------
4144
4145 when Attribute_External_Tag => External_Tag :
4146 begin
4147 if not Is_Tagged_Type (U_Ent) then
4148 Error_Msg_N ("should be a tagged type", Nam);
4149 end if;
4150
4151 if Duplicate_Clause then
4152 null;
4153
4154 else
4155 Analyze_And_Resolve (Expr, Standard_String);
4156
4157 if not Is_Static_Expression (Expr) then
4158 Flag_Non_Static_Expr
4159 ("static string required for tag name!", Nam);
4160 end if;
4161
4162 if VM_Target = No_VM then
4163 Set_Has_External_Tag_Rep_Clause (U_Ent);
4164 else
4165 Error_Msg_Name_1 := Attr;
4166 Error_Msg_N
4167 ("% attribute unsupported in this configuration", Nam);
4168 end if;
4169
4170 if not Is_Library_Level_Entity (U_Ent) then
4171 Error_Msg_NE
4172 ("??non-unique external tag supplied for &", N, U_Ent);
4173 Error_Msg_N
4174 ("\??same external tag applies to all "
4175 & "subprogram calls", N);
4176 Error_Msg_N
4177 ("\??corresponding internal tag cannot be obtained", N);
4178 end if;
4179 end if;
4180 end External_Tag;
4181
4182 --------------------------
4183 -- Implicit_Dereference --
4184 --------------------------
4185
4186 when Attribute_Implicit_Dereference =>
4187
4188 -- Legality checks already performed at the point of the type
4189 -- declaration, aspect is not delayed.
4190
4191 null;
4192
4193 -----------
4194 -- Input --
4195 -----------
4196
4197 when Attribute_Input =>
4198 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4199 Set_Has_Specified_Stream_Input (Ent);
4200
4201 ------------------------
4202 -- Interrupt_Priority --
4203 ------------------------
4204
4205 when Attribute_Interrupt_Priority => Interrupt_Priority :
4206 begin
4207 -- Interrupt_Priority attribute definition clause not allowed
4208 -- except from aspect specification.
4209
4210 if From_Aspect_Specification (N) then
4211 if not (Is_Protected_Type (U_Ent)
4212 or else Is_Task_Type (U_Ent))
4213 then
4214 Error_Msg_N
4215 ("Interrupt_Priority can only be defined for task" &
4216 "and protected object",
4217 Nam);
4218
4219 elsif Duplicate_Clause then
4220 null;
4221
4222 else
4223 -- The expression must be analyzed in the special manner
4224 -- described in "Handling of Default and Per-Object
4225 -- Expressions" in sem.ads.
4226
4227 -- The visibility to the discriminants must be restored
4228
4229 Push_Scope_And_Install_Discriminants (U_Ent);
4230
4231 Preanalyze_Spec_Expression
4232 (Expr, RTE (RE_Interrupt_Priority));
4233
4234 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4235 end if;
4236
4237 else
4238 Error_Msg_N
4239 ("attribute& cannot be set with definition clause", N);
4240 end if;
4241 end Interrupt_Priority;
4242
4243 ----------------------
4244 -- Iterator_Element --
4245 ----------------------
4246
4247 when Attribute_Iterator_Element =>
4248 Analyze (Expr);
4249
4250 if not Is_Entity_Name (Expr)
4251 or else not Is_Type (Entity (Expr))
4252 then
4253 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4254 end if;
4255
4256 -------------------
4257 -- Machine_Radix --
4258 -------------------
4259
4260 -- Machine radix attribute definition clause
4261
4262 when Attribute_Machine_Radix => Machine_Radix : declare
4263 Radix : constant Uint := Static_Integer (Expr);
4264
4265 begin
4266 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4267 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4268
4269 elsif Duplicate_Clause then
4270 null;
4271
4272 elsif Radix /= No_Uint then
4273 Set_Has_Machine_Radix_Clause (U_Ent);
4274 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4275
4276 if Radix = 2 then
4277 null;
4278 elsif Radix = 10 then
4279 Set_Machine_Radix_10 (U_Ent);
4280 else
4281 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4282 end if;
4283 end if;
4284 end Machine_Radix;
4285
4286 -----------------
4287 -- Object_Size --
4288 -----------------
4289
4290 -- Object_Size attribute definition clause
4291
4292 when Attribute_Object_Size => Object_Size : declare
4293 Size : constant Uint := Static_Integer (Expr);
4294
4295 Biased : Boolean;
4296 pragma Warnings (Off, Biased);
4297
4298 begin
4299 if not Is_Type (U_Ent) then
4300 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4301
4302 elsif Duplicate_Clause then
4303 null;
4304
4305 else
4306 Check_Size (Expr, U_Ent, Size, Biased);
4307
4308 if Size /= 8
4309 and then
4310 Size /= 16
4311 and then
4312 Size /= 32
4313 and then
4314 UI_Mod (Size, 64) /= 0
4315 then
4316 Error_Msg_N
4317 ("Object_Size must be 8, 16, 32, or multiple of 64",
4318 Expr);
4319 end if;
4320
4321 Set_Esize (U_Ent, Size);
4322 Set_Has_Object_Size_Clause (U_Ent);
4323 Alignment_Check_For_Size_Change (U_Ent, Size);
4324 end if;
4325 end Object_Size;
4326
4327 ------------
4328 -- Output --
4329 ------------
4330
4331 when Attribute_Output =>
4332 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4333 Set_Has_Specified_Stream_Output (Ent);
4334
4335 --------------
4336 -- Priority --
4337 --------------
4338
4339 when Attribute_Priority => Priority :
4340 begin
4341 -- Priority attribute definition clause not allowed except from
4342 -- aspect specification.
4343
4344 if From_Aspect_Specification (N) then
4345 if not (Is_Protected_Type (U_Ent)
4346 or else Is_Task_Type (U_Ent)
4347 or else Ekind (U_Ent) = E_Procedure)
4348 then
4349 Error_Msg_N
4350 ("Priority can only be defined for task and protected " &
4351 "object",
4352 Nam);
4353
4354 elsif Duplicate_Clause then
4355 null;
4356
4357 else
4358 -- The expression must be analyzed in the special manner
4359 -- described in "Handling of Default and Per-Object
4360 -- Expressions" in sem.ads.
4361
4362 -- The visibility to the discriminants must be restored
4363
4364 Push_Scope_And_Install_Discriminants (U_Ent);
4365 Preanalyze_Spec_Expression (Expr, Standard_Integer);
4366 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4367
4368 if not Is_Static_Expression (Expr) then
4369 Check_Restriction (Static_Priorities, Expr);
4370 end if;
4371 end if;
4372
4373 else
4374 Error_Msg_N
4375 ("attribute& cannot be set with definition clause", N);
4376 end if;
4377 end Priority;
4378
4379 ----------
4380 -- Read --
4381 ----------
4382
4383 when Attribute_Read =>
4384 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
4385 Set_Has_Specified_Stream_Read (Ent);
4386
4387 --------------------------
4388 -- Scalar_Storage_Order --
4389 --------------------------
4390
4391 -- Scalar_Storage_Order attribute definition clause
4392
4393 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
4394 begin
4395 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
4396 Error_Msg_N
4397 ("Scalar_Storage_Order can only be defined for "
4398 & "record or array type", Nam);
4399
4400 elsif Duplicate_Clause then
4401 null;
4402
4403 else
4404 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4405
4406 if Etype (Expr) = Any_Type then
4407 return;
4408
4409 elsif not Is_Static_Expression (Expr) then
4410 Flag_Non_Static_Expr
4411 ("Scalar_Storage_Order requires static expression!", Expr);
4412
4413 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4414
4415 -- Here for the case of a non-default (i.e. non-confirming)
4416 -- Scalar_Storage_Order attribute definition.
4417
4418 if Support_Nondefault_SSO_On_Target then
4419 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
4420 else
4421 Error_Msg_N
4422 ("non-default Scalar_Storage_Order "
4423 & "not supported on target", Expr);
4424 end if;
4425 end if;
4426 end if;
4427 end Scalar_Storage_Order;
4428
4429 ----------
4430 -- Size --
4431 ----------
4432
4433 -- Size attribute definition clause
4434
4435 when Attribute_Size => Size : declare
4436 Size : constant Uint := Static_Integer (Expr);
4437 Etyp : Entity_Id;
4438 Biased : Boolean;
4439
4440 begin
4441 FOnly := True;
4442
4443 if Duplicate_Clause then
4444 null;
4445
4446 elsif not Is_Type (U_Ent)
4447 and then Ekind (U_Ent) /= E_Variable
4448 and then Ekind (U_Ent) /= E_Constant
4449 then
4450 Error_Msg_N ("size cannot be given for &", Nam);
4451
4452 elsif Is_Array_Type (U_Ent)
4453 and then not Is_Constrained (U_Ent)
4454 then
4455 Error_Msg_N
4456 ("size cannot be given for unconstrained array", Nam);
4457
4458 elsif Size /= No_Uint then
4459 if VM_Target /= No_VM and then not GNAT_Mode then
4460
4461 -- Size clause is not handled properly on VM targets.
4462 -- Display a warning unless we are in GNAT mode, in which
4463 -- case this is useless.
4464
4465 Error_Msg_N
4466 ("size clauses are ignored in this configuration??", N);
4467 end if;
4468
4469 if Is_Type (U_Ent) then
4470 Etyp := U_Ent;
4471 else
4472 Etyp := Etype (U_Ent);
4473 end if;
4474
4475 -- Check size, note that Gigi is in charge of checking that the
4476 -- size of an array or record type is OK. Also we do not check
4477 -- the size in the ordinary fixed-point case, since it is too
4478 -- early to do so (there may be subsequent small clause that
4479 -- affects the size). We can check the size if a small clause
4480 -- has already been given.
4481
4482 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
4483 or else Has_Small_Clause (U_Ent)
4484 then
4485 Check_Size (Expr, Etyp, Size, Biased);
4486 Set_Biased (U_Ent, N, "size clause", Biased);
4487 end if;
4488
4489 -- For types set RM_Size and Esize if possible
4490
4491 if Is_Type (U_Ent) then
4492 Set_RM_Size (U_Ent, Size);
4493
4494 -- For elementary types, increase Object_Size to power of 2,
4495 -- but not less than a storage unit in any case (normally
4496 -- this means it will be byte addressable).
4497
4498 -- For all other types, nothing else to do, we leave Esize
4499 -- (object size) unset, the back end will set it from the
4500 -- size and alignment in an appropriate manner.
4501
4502 -- In both cases, we check whether the alignment must be
4503 -- reset in the wake of the size change.
4504
4505 if Is_Elementary_Type (U_Ent) then
4506 if Size <= System_Storage_Unit then
4507 Init_Esize (U_Ent, System_Storage_Unit);
4508 elsif Size <= 16 then
4509 Init_Esize (U_Ent, 16);
4510 elsif Size <= 32 then
4511 Init_Esize (U_Ent, 32);
4512 else
4513 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
4514 end if;
4515
4516 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
4517 else
4518 Alignment_Check_For_Size_Change (U_Ent, Size);
4519 end if;
4520
4521 -- For objects, set Esize only
4522
4523 else
4524 if Is_Elementary_Type (Etyp) then
4525 if Size /= System_Storage_Unit
4526 and then
4527 Size /= System_Storage_Unit * 2
4528 and then
4529 Size /= System_Storage_Unit * 4
4530 and then
4531 Size /= System_Storage_Unit * 8
4532 then
4533 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4534 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
4535 Error_Msg_N
4536 ("size for primitive object must be a power of 2"
4537 & " in the range ^-^", N);
4538 end if;
4539 end if;
4540
4541 Set_Esize (U_Ent, Size);
4542 end if;
4543
4544 Set_Has_Size_Clause (U_Ent);
4545 end if;
4546 end Size;
4547
4548 -----------
4549 -- Small --
4550 -----------
4551
4552 -- Small attribute definition clause
4553
4554 when Attribute_Small => Small : declare
4555 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
4556 Small : Ureal;
4557
4558 begin
4559 Analyze_And_Resolve (Expr, Any_Real);
4560
4561 if Etype (Expr) = Any_Type then
4562 return;
4563
4564 elsif not Is_Static_Expression (Expr) then
4565 Flag_Non_Static_Expr
4566 ("small requires static expression!", Expr);
4567 return;
4568
4569 else
4570 Small := Expr_Value_R (Expr);
4571
4572 if Small <= Ureal_0 then
4573 Error_Msg_N ("small value must be greater than zero", Expr);
4574 return;
4575 end if;
4576
4577 end if;
4578
4579 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
4580 Error_Msg_N
4581 ("small requires an ordinary fixed point type", Nam);
4582
4583 elsif Has_Small_Clause (U_Ent) then
4584 Error_Msg_N ("small already given for &", Nam);
4585
4586 elsif Small > Delta_Value (U_Ent) then
4587 Error_Msg_N
4588 ("small value must not be greater than delta value", Nam);
4589
4590 else
4591 Set_Small_Value (U_Ent, Small);
4592 Set_Small_Value (Implicit_Base, Small);
4593 Set_Has_Small_Clause (U_Ent);
4594 Set_Has_Small_Clause (Implicit_Base);
4595 Set_Has_Non_Standard_Rep (Implicit_Base);
4596 end if;
4597 end Small;
4598
4599 ------------------
4600 -- Storage_Pool --
4601 ------------------
4602
4603 -- Storage_Pool attribute definition clause
4604
4605 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
4606 Pool : Entity_Id;
4607 T : Entity_Id;
4608
4609 begin
4610 if Ekind (U_Ent) = E_Access_Subprogram_Type then
4611 Error_Msg_N
4612 ("storage pool cannot be given for access-to-subprogram type",
4613 Nam);
4614 return;
4615
4616 elsif not
4617 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
4618 then
4619 Error_Msg_N
4620 ("storage pool can only be given for access types", Nam);
4621 return;
4622
4623 elsif Is_Derived_Type (U_Ent) then
4624 Error_Msg_N
4625 ("storage pool cannot be given for a derived access type",
4626 Nam);
4627
4628 elsif Duplicate_Clause then
4629 return;
4630
4631 elsif Present (Associated_Storage_Pool (U_Ent)) then
4632 Error_Msg_N ("storage pool already given for &", Nam);
4633 return;
4634 end if;
4635
4636 -- Check for Storage_Size previously given
4637
4638 declare
4639 SS : constant Node_Id :=
4640 Get_Attribute_Definition_Clause
4641 (U_Ent, Attribute_Storage_Size);
4642 begin
4643 if Present (SS) then
4644 Check_Pool_Size_Clash (U_Ent, N, SS);
4645 end if;
4646 end;
4647
4648 -- Storage_Pool case
4649
4650 if Id = Attribute_Storage_Pool then
4651 Analyze_And_Resolve
4652 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4653
4654 -- In the Simple_Storage_Pool case, we allow a variable of any
4655 -- simple storage pool type, so we Resolve without imposing an
4656 -- expected type.
4657
4658 else
4659 Analyze_And_Resolve (Expr);
4660
4661 if not Present (Get_Rep_Pragma
4662 (Etype (Expr), Name_Simple_Storage_Pool_Type))
4663 then
4664 Error_Msg_N
4665 ("expression must be of a simple storage pool type", Expr);
4666 end if;
4667 end if;
4668
4669 if not Denotes_Variable (Expr) then
4670 Error_Msg_N ("storage pool must be a variable", Expr);
4671 return;
4672 end if;
4673
4674 if Nkind (Expr) = N_Type_Conversion then
4675 T := Etype (Expression (Expr));
4676 else
4677 T := Etype (Expr);
4678 end if;
4679
4680 -- The Stack_Bounded_Pool is used internally for implementing
4681 -- access types with a Storage_Size. Since it only work properly
4682 -- when used on one specific type, we need to check that it is not
4683 -- hijacked improperly:
4684
4685 -- type T is access Integer;
4686 -- for T'Storage_Size use n;
4687 -- type Q is access Float;
4688 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4689
4690 if RTE_Available (RE_Stack_Bounded_Pool)
4691 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4692 then
4693 Error_Msg_N ("non-shareable internal Pool", Expr);
4694 return;
4695 end if;
4696
4697 -- If the argument is a name that is not an entity name, then
4698 -- we construct a renaming operation to define an entity of
4699 -- type storage pool.
4700
4701 if not Is_Entity_Name (Expr)
4702 and then Is_Object_Reference (Expr)
4703 then
4704 Pool := Make_Temporary (Loc, 'P', Expr);
4705
4706 declare
4707 Rnode : constant Node_Id :=
4708 Make_Object_Renaming_Declaration (Loc,
4709 Defining_Identifier => Pool,
4710 Subtype_Mark =>
4711 New_Occurrence_Of (Etype (Expr), Loc),
4712 Name => Expr);
4713
4714 begin
4715 -- If the attribute definition clause comes from an aspect
4716 -- clause, then insert the renaming before the associated
4717 -- entity's declaration, since the attribute clause has
4718 -- not yet been appended to the declaration list.
4719
4720 if From_Aspect_Specification (N) then
4721 Insert_Before (Parent (Entity (N)), Rnode);
4722 else
4723 Insert_Before (N, Rnode);
4724 end if;
4725
4726 Analyze (Rnode);
4727 Set_Associated_Storage_Pool (U_Ent, Pool);
4728 end;
4729
4730 elsif Is_Entity_Name (Expr) then
4731 Pool := Entity (Expr);
4732
4733 -- If pool is a renamed object, get original one. This can
4734 -- happen with an explicit renaming, and within instances.
4735
4736 while Present (Renamed_Object (Pool))
4737 and then Is_Entity_Name (Renamed_Object (Pool))
4738 loop
4739 Pool := Entity (Renamed_Object (Pool));
4740 end loop;
4741
4742 if Present (Renamed_Object (Pool))
4743 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
4744 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
4745 then
4746 Pool := Entity (Expression (Renamed_Object (Pool)));
4747 end if;
4748
4749 Set_Associated_Storage_Pool (U_Ent, Pool);
4750
4751 elsif Nkind (Expr) = N_Type_Conversion
4752 and then Is_Entity_Name (Expression (Expr))
4753 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
4754 then
4755 Pool := Entity (Expression (Expr));
4756 Set_Associated_Storage_Pool (U_Ent, Pool);
4757
4758 else
4759 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
4760 return;
4761 end if;
4762 end;
4763
4764 ------------------
4765 -- Storage_Size --
4766 ------------------
4767
4768 -- Storage_Size attribute definition clause
4769
4770 when Attribute_Storage_Size => Storage_Size : declare
4771 Btype : constant Entity_Id := Base_Type (U_Ent);
4772
4773 begin
4774 if Is_Task_Type (U_Ent) then
4775
4776 -- Check obsolescent (but never obsolescent if from aspect!)
4777
4778 if not From_Aspect_Specification (N) then
4779 Check_Restriction (No_Obsolescent_Features, N);
4780
4781 if Warn_On_Obsolescent_Feature then
4782 Error_Msg_N
4783 ("?j?storage size clause for task is an " &
4784 "obsolescent feature (RM J.9)", N);
4785 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4786 end if;
4787 end if;
4788
4789 FOnly := True;
4790 end if;
4791
4792 if not Is_Access_Type (U_Ent)
4793 and then Ekind (U_Ent) /= E_Task_Type
4794 then
4795 Error_Msg_N ("storage size cannot be given for &", Nam);
4796
4797 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4798 Error_Msg_N
4799 ("storage size cannot be given for a derived access type",
4800 Nam);
4801
4802 elsif Duplicate_Clause then
4803 null;
4804
4805 else
4806 Analyze_And_Resolve (Expr, Any_Integer);
4807
4808 if Is_Access_Type (U_Ent) then
4809
4810 -- Check for Storage_Pool previously given
4811
4812 declare
4813 SP : constant Node_Id :=
4814 Get_Attribute_Definition_Clause
4815 (U_Ent, Attribute_Storage_Pool);
4816
4817 begin
4818 if Present (SP) then
4819 Check_Pool_Size_Clash (U_Ent, SP, N);
4820 end if;
4821 end;
4822
4823 -- Special case of for x'Storage_Size use 0
4824
4825 if Is_OK_Static_Expression (Expr)
4826 and then Expr_Value (Expr) = 0
4827 then
4828 Set_No_Pool_Assigned (Btype);
4829 end if;
4830 end if;
4831
4832 Set_Has_Storage_Size_Clause (Btype);
4833 end if;
4834 end Storage_Size;
4835
4836 -----------------
4837 -- Stream_Size --
4838 -----------------
4839
4840 when Attribute_Stream_Size => Stream_Size : declare
4841 Size : constant Uint := Static_Integer (Expr);
4842
4843 begin
4844 if Ada_Version <= Ada_95 then
4845 Check_Restriction (No_Implementation_Attributes, N);
4846 end if;
4847
4848 if Duplicate_Clause then
4849 null;
4850
4851 elsif Is_Elementary_Type (U_Ent) then
4852 if Size /= System_Storage_Unit
4853 and then
4854 Size /= System_Storage_Unit * 2
4855 and then
4856 Size /= System_Storage_Unit * 4
4857 and then
4858 Size /= System_Storage_Unit * 8
4859 then
4860 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4861 Error_Msg_N
4862 ("stream size for elementary type must be a"
4863 & " power of 2 and at least ^", N);
4864
4865 elsif RM_Size (U_Ent) > Size then
4866 Error_Msg_Uint_1 := RM_Size (U_Ent);
4867 Error_Msg_N
4868 ("stream size for elementary type must be a"
4869 & " power of 2 and at least ^", N);
4870 end if;
4871
4872 Set_Has_Stream_Size_Clause (U_Ent);
4873
4874 else
4875 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
4876 end if;
4877 end Stream_Size;
4878
4879 ----------------
4880 -- Value_Size --
4881 ----------------
4882
4883 -- Value_Size attribute definition clause
4884
4885 when Attribute_Value_Size => Value_Size : declare
4886 Size : constant Uint := Static_Integer (Expr);
4887 Biased : Boolean;
4888
4889 begin
4890 if not Is_Type (U_Ent) then
4891 Error_Msg_N ("Value_Size cannot be given for &", Nam);
4892
4893 elsif Duplicate_Clause then
4894 null;
4895
4896 elsif Is_Array_Type (U_Ent)
4897 and then not Is_Constrained (U_Ent)
4898 then
4899 Error_Msg_N
4900 ("Value_Size cannot be given for unconstrained array", Nam);
4901
4902 else
4903 if Is_Elementary_Type (U_Ent) then
4904 Check_Size (Expr, U_Ent, Size, Biased);
4905 Set_Biased (U_Ent, N, "value size clause", Biased);
4906 end if;
4907
4908 Set_RM_Size (U_Ent, Size);
4909 end if;
4910 end Value_Size;
4911
4912 -----------------------
4913 -- Variable_Indexing --
4914 -----------------------
4915
4916 when Attribute_Variable_Indexing =>
4917 Check_Indexing_Functions;
4918
4919 -----------
4920 -- Write --
4921 -----------
4922
4923 when Attribute_Write =>
4924 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4925 Set_Has_Specified_Stream_Write (Ent);
4926
4927 -- All other attributes cannot be set
4928
4929 when others =>
4930 Error_Msg_N
4931 ("attribute& cannot be set with definition clause", N);
4932 end case;
4933
4934 -- The test for the type being frozen must be performed after any
4935 -- expression the clause has been analyzed since the expression itself
4936 -- might cause freezing that makes the clause illegal.
4937
4938 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4939 return;
4940 end if;
4941 end Analyze_Attribute_Definition_Clause;
4942
4943 ----------------------------
4944 -- Analyze_Code_Statement --
4945 ----------------------------
4946
4947 procedure Analyze_Code_Statement (N : Node_Id) is
4948 HSS : constant Node_Id := Parent (N);
4949 SBody : constant Node_Id := Parent (HSS);
4950 Subp : constant Entity_Id := Current_Scope;
4951 Stmt : Node_Id;
4952 Decl : Node_Id;
4953 StmtO : Node_Id;
4954 DeclO : Node_Id;
4955
4956 begin
4957 -- Analyze and check we get right type, note that this implements the
4958 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4959 -- is the only way that Asm_Insn could possibly be visible.
4960
4961 Analyze_And_Resolve (Expression (N));
4962
4963 if Etype (Expression (N)) = Any_Type then
4964 return;
4965 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4966 Error_Msg_N ("incorrect type for code statement", N);
4967 return;
4968 end if;
4969
4970 Check_Code_Statement (N);
4971
4972 -- Make sure we appear in the handled statement sequence of a
4973 -- subprogram (RM 13.8(3)).
4974
4975 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4976 or else Nkind (SBody) /= N_Subprogram_Body
4977 then
4978 Error_Msg_N
4979 ("code statement can only appear in body of subprogram", N);
4980 return;
4981 end if;
4982
4983 -- Do remaining checks (RM 13.8(3)) if not already done
4984
4985 if not Is_Machine_Code_Subprogram (Subp) then
4986 Set_Is_Machine_Code_Subprogram (Subp);
4987
4988 -- No exception handlers allowed
4989
4990 if Present (Exception_Handlers (HSS)) then
4991 Error_Msg_N
4992 ("exception handlers not permitted in machine code subprogram",
4993 First (Exception_Handlers (HSS)));
4994 end if;
4995
4996 -- No declarations other than use clauses and pragmas (we allow
4997 -- certain internally generated declarations as well).
4998
4999 Decl := First (Declarations (SBody));
5000 while Present (Decl) loop
5001 DeclO := Original_Node (Decl);
5002 if Comes_From_Source (DeclO)
5003 and not Nkind_In (DeclO, N_Pragma,
5004 N_Use_Package_Clause,
5005 N_Use_Type_Clause,
5006 N_Implicit_Label_Declaration)
5007 then
5008 Error_Msg_N
5009 ("this declaration not allowed in machine code subprogram",
5010 DeclO);
5011 end if;
5012
5013 Next (Decl);
5014 end loop;
5015
5016 -- No statements other than code statements, pragmas, and labels.
5017 -- Again we allow certain internally generated statements.
5018
5019 -- In Ada 2012, qualified expressions are names, and the code
5020 -- statement is initially parsed as a procedure call.
5021
5022 Stmt := First (Statements (HSS));
5023 while Present (Stmt) loop
5024 StmtO := Original_Node (Stmt);
5025
5026 -- A procedure call transformed into a code statement is OK.
5027
5028 if Ada_Version >= Ada_2012
5029 and then Nkind (StmtO) = N_Procedure_Call_Statement
5030 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5031 then
5032 null;
5033
5034 elsif Comes_From_Source (StmtO)
5035 and then not Nkind_In (StmtO, N_Pragma,
5036 N_Label,
5037 N_Code_Statement)
5038 then
5039 Error_Msg_N
5040 ("this statement is not allowed in machine code subprogram",
5041 StmtO);
5042 end if;
5043
5044 Next (Stmt);
5045 end loop;
5046 end if;
5047 end Analyze_Code_Statement;
5048
5049 -----------------------------------------------
5050 -- Analyze_Enumeration_Representation_Clause --
5051 -----------------------------------------------
5052
5053 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5054 Ident : constant Node_Id := Identifier (N);
5055 Aggr : constant Node_Id := Array_Aggregate (N);
5056 Enumtype : Entity_Id;
5057 Elit : Entity_Id;
5058 Expr : Node_Id;
5059 Assoc : Node_Id;
5060 Choice : Node_Id;
5061 Val : Uint;
5062
5063 Err : Boolean := False;
5064 -- Set True to avoid cascade errors and crashes on incorrect source code
5065
5066 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5067 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5068 -- Allowed range of universal integer (= allowed range of enum lit vals)
5069
5070 Min : Uint;
5071 Max : Uint;
5072 -- Minimum and maximum values of entries
5073
5074 Max_Node : Node_Id;
5075 -- Pointer to node for literal providing max value
5076
5077 begin
5078 if Ignore_Rep_Clauses then
5079 return;
5080 end if;
5081
5082 -- Ignore enumeration rep clauses by default in CodePeer mode,
5083 -- unless -gnatd.I is specified, as a work around for potential false
5084 -- positive messages.
5085
5086 if CodePeer_Mode and not Debug_Flag_Dot_II then
5087 return;
5088 end if;
5089
5090 -- First some basic error checks
5091
5092 Find_Type (Ident);
5093 Enumtype := Entity (Ident);
5094
5095 if Enumtype = Any_Type
5096 or else Rep_Item_Too_Early (Enumtype, N)
5097 then
5098 return;
5099 else
5100 Enumtype := Underlying_Type (Enumtype);
5101 end if;
5102
5103 if not Is_Enumeration_Type (Enumtype) then
5104 Error_Msg_NE
5105 ("enumeration type required, found}",
5106 Ident, First_Subtype (Enumtype));
5107 return;
5108 end if;
5109
5110 -- Ignore rep clause on generic actual type. This will already have
5111 -- been flagged on the template as an error, and this is the safest
5112 -- way to ensure we don't get a junk cascaded message in the instance.
5113
5114 if Is_Generic_Actual_Type (Enumtype) then
5115 return;
5116
5117 -- Type must be in current scope
5118
5119 elsif Scope (Enumtype) /= Current_Scope then
5120 Error_Msg_N ("type must be declared in this scope", Ident);
5121 return;
5122
5123 -- Type must be a first subtype
5124
5125 elsif not Is_First_Subtype (Enumtype) then
5126 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5127 return;
5128
5129 -- Ignore duplicate rep clause
5130
5131 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5132 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5133 return;
5134
5135 -- Don't allow rep clause for standard [wide_[wide_]]character
5136
5137 elsif Is_Standard_Character_Type (Enumtype) then
5138 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5139 return;
5140
5141 -- Check that the expression is a proper aggregate (no parentheses)
5142
5143 elsif Paren_Count (Aggr) /= 0 then
5144 Error_Msg
5145 ("extra parentheses surrounding aggregate not allowed",
5146 First_Sloc (Aggr));
5147 return;
5148
5149 -- All tests passed, so set rep clause in place
5150
5151 else
5152 Set_Has_Enumeration_Rep_Clause (Enumtype);
5153 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5154 end if;
5155
5156 -- Now we process the aggregate. Note that we don't use the normal
5157 -- aggregate code for this purpose, because we don't want any of the
5158 -- normal expansion activities, and a number of special semantic
5159 -- rules apply (including the component type being any integer type)
5160
5161 Elit := First_Literal (Enumtype);
5162
5163 -- First the positional entries if any
5164
5165 if Present (Expressions (Aggr)) then
5166 Expr := First (Expressions (Aggr));
5167 while Present (Expr) loop
5168 if No (Elit) then
5169 Error_Msg_N ("too many entries in aggregate", Expr);
5170 return;
5171 end if;
5172
5173 Val := Static_Integer (Expr);
5174
5175 -- Err signals that we found some incorrect entries processing
5176 -- the list. The final checks for completeness and ordering are
5177 -- skipped in this case.
5178
5179 if Val = No_Uint then
5180 Err := True;
5181 elsif Val < Lo or else Hi < Val then
5182 Error_Msg_N ("value outside permitted range", Expr);
5183 Err := True;
5184 end if;
5185
5186 Set_Enumeration_Rep (Elit, Val);
5187 Set_Enumeration_Rep_Expr (Elit, Expr);
5188 Next (Expr);
5189 Next (Elit);
5190 end loop;
5191 end if;
5192
5193 -- Now process the named entries if present
5194
5195 if Present (Component_Associations (Aggr)) then
5196 Assoc := First (Component_Associations (Aggr));
5197 while Present (Assoc) loop
5198 Choice := First (Choices (Assoc));
5199
5200 if Present (Next (Choice)) then
5201 Error_Msg_N
5202 ("multiple choice not allowed here", Next (Choice));
5203 Err := True;
5204 end if;
5205
5206 if Nkind (Choice) = N_Others_Choice then
5207 Error_Msg_N ("others choice not allowed here", Choice);
5208 Err := True;
5209
5210 elsif Nkind (Choice) = N_Range then
5211
5212 -- ??? should allow zero/one element range here
5213
5214 Error_Msg_N ("range not allowed here", Choice);
5215 Err := True;
5216
5217 else
5218 Analyze_And_Resolve (Choice, Enumtype);
5219
5220 if Error_Posted (Choice) then
5221 Err := True;
5222 end if;
5223
5224 if not Err then
5225 if Is_Entity_Name (Choice)
5226 and then Is_Type (Entity (Choice))
5227 then
5228 Error_Msg_N ("subtype name not allowed here", Choice);
5229 Err := True;
5230
5231 -- ??? should allow static subtype with zero/one entry
5232
5233 elsif Etype (Choice) = Base_Type (Enumtype) then
5234 if not Is_Static_Expression (Choice) then
5235 Flag_Non_Static_Expr
5236 ("non-static expression used for choice!", Choice);
5237 Err := True;
5238
5239 else
5240 Elit := Expr_Value_E (Choice);
5241
5242 if Present (Enumeration_Rep_Expr (Elit)) then
5243 Error_Msg_Sloc :=
5244 Sloc (Enumeration_Rep_Expr (Elit));
5245 Error_Msg_NE
5246 ("representation for& previously given#",
5247 Choice, Elit);
5248 Err := True;
5249 end if;
5250
5251 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5252
5253 Expr := Expression (Assoc);
5254 Val := Static_Integer (Expr);
5255
5256 if Val = No_Uint then
5257 Err := True;
5258
5259 elsif Val < Lo or else Hi < Val then
5260 Error_Msg_N ("value outside permitted range", Expr);
5261 Err := True;
5262 end if;
5263
5264 Set_Enumeration_Rep (Elit, Val);
5265 end if;
5266 end if;
5267 end if;
5268 end if;
5269
5270 Next (Assoc);
5271 end loop;
5272 end if;
5273
5274 -- Aggregate is fully processed. Now we check that a full set of
5275 -- representations was given, and that they are in range and in order.
5276 -- These checks are only done if no other errors occurred.
5277
5278 if not Err then
5279 Min := No_Uint;
5280 Max := No_Uint;
5281
5282 Elit := First_Literal (Enumtype);
5283 while Present (Elit) loop
5284 if No (Enumeration_Rep_Expr (Elit)) then
5285 Error_Msg_NE ("missing representation for&!", N, Elit);
5286
5287 else
5288 Val := Enumeration_Rep (Elit);
5289
5290 if Min = No_Uint then
5291 Min := Val;
5292 end if;
5293
5294 if Val /= No_Uint then
5295 if Max /= No_Uint and then Val <= Max then
5296 Error_Msg_NE
5297 ("enumeration value for& not ordered!",
5298 Enumeration_Rep_Expr (Elit), Elit);
5299 end if;
5300
5301 Max_Node := Enumeration_Rep_Expr (Elit);
5302 Max := Val;
5303 end if;
5304
5305 -- If there is at least one literal whose representation is not
5306 -- equal to the Pos value, then note that this enumeration type
5307 -- has a non-standard representation.
5308
5309 if Val /= Enumeration_Pos (Elit) then
5310 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5311 end if;
5312 end if;
5313
5314 Next (Elit);
5315 end loop;
5316
5317 -- Now set proper size information
5318
5319 declare
5320 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5321
5322 begin
5323 if Has_Size_Clause (Enumtype) then
5324
5325 -- All OK, if size is OK now
5326
5327 if RM_Size (Enumtype) >= Minsize then
5328 null;
5329
5330 else
5331 -- Try if we can get by with biasing
5332
5333 Minsize :=
5334 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5335
5336 -- Error message if even biasing does not work
5337
5338 if RM_Size (Enumtype) < Minsize then
5339 Error_Msg_Uint_1 := RM_Size (Enumtype);
5340 Error_Msg_Uint_2 := Max;
5341 Error_Msg_N
5342 ("previously given size (^) is too small "
5343 & "for this value (^)", Max_Node);
5344
5345 -- If biasing worked, indicate that we now have biased rep
5346
5347 else
5348 Set_Biased
5349 (Enumtype, Size_Clause (Enumtype), "size clause");
5350 end if;
5351 end if;
5352
5353 else
5354 Set_RM_Size (Enumtype, Minsize);
5355 Set_Enum_Esize (Enumtype);
5356 end if;
5357
5358 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
5359 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
5360 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
5361 end;
5362 end if;
5363
5364 -- We repeat the too late test in case it froze itself!
5365
5366 if Rep_Item_Too_Late (Enumtype, N) then
5367 null;
5368 end if;
5369 end Analyze_Enumeration_Representation_Clause;
5370
5371 ----------------------------
5372 -- Analyze_Free_Statement --
5373 ----------------------------
5374
5375 procedure Analyze_Free_Statement (N : Node_Id) is
5376 begin
5377 Analyze (Expression (N));
5378 end Analyze_Free_Statement;
5379
5380 ---------------------------
5381 -- Analyze_Freeze_Entity --
5382 ---------------------------
5383
5384 procedure Analyze_Freeze_Entity (N : Node_Id) is
5385 begin
5386 Freeze_Entity_Checks (N);
5387 end Analyze_Freeze_Entity;
5388
5389 -----------------------------------
5390 -- Analyze_Freeze_Generic_Entity --
5391 -----------------------------------
5392
5393 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
5394 begin
5395 Freeze_Entity_Checks (N);
5396 end Analyze_Freeze_Generic_Entity;
5397
5398 ------------------------------------------
5399 -- Analyze_Record_Representation_Clause --
5400 ------------------------------------------
5401
5402 -- Note: we check as much as we can here, but we can't do any checks
5403 -- based on the position values (e.g. overlap checks) until freeze time
5404 -- because especially in Ada 2005 (machine scalar mode), the processing
5405 -- for non-standard bit order can substantially change the positions.
5406 -- See procedure Check_Record_Representation_Clause (called from Freeze)
5407 -- for the remainder of this processing.
5408
5409 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
5410 Ident : constant Node_Id := Identifier (N);
5411 Biased : Boolean;
5412 CC : Node_Id;
5413 Comp : Entity_Id;
5414 Fbit : Uint;
5415 Hbit : Uint := Uint_0;
5416 Lbit : Uint;
5417 Ocomp : Entity_Id;
5418 Posit : Uint;
5419 Rectype : Entity_Id;
5420 Recdef : Node_Id;
5421
5422 function Is_Inherited (Comp : Entity_Id) return Boolean;
5423 -- True if Comp is an inherited component in a record extension
5424
5425 ------------------
5426 -- Is_Inherited --
5427 ------------------
5428
5429 function Is_Inherited (Comp : Entity_Id) return Boolean is
5430 Comp_Base : Entity_Id;
5431
5432 begin
5433 if Ekind (Rectype) = E_Record_Subtype then
5434 Comp_Base := Original_Record_Component (Comp);
5435 else
5436 Comp_Base := Comp;
5437 end if;
5438
5439 return Comp_Base /= Original_Record_Component (Comp_Base);
5440 end Is_Inherited;
5441
5442 -- Local variables
5443
5444 Is_Record_Extension : Boolean;
5445 -- True if Rectype is a record extension
5446
5447 CR_Pragma : Node_Id := Empty;
5448 -- Points to N_Pragma node if Complete_Representation pragma present
5449
5450 -- Start of processing for Analyze_Record_Representation_Clause
5451
5452 begin
5453 if Ignore_Rep_Clauses then
5454 return;
5455 end if;
5456
5457 Find_Type (Ident);
5458 Rectype := Entity (Ident);
5459
5460 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
5461 return;
5462 else
5463 Rectype := Underlying_Type (Rectype);
5464 end if;
5465
5466 -- First some basic error checks
5467
5468 if not Is_Record_Type (Rectype) then
5469 Error_Msg_NE
5470 ("record type required, found}", Ident, First_Subtype (Rectype));
5471 return;
5472
5473 elsif Scope (Rectype) /= Current_Scope then
5474 Error_Msg_N ("type must be declared in this scope", N);
5475 return;
5476
5477 elsif not Is_First_Subtype (Rectype) then
5478 Error_Msg_N ("cannot give record rep clause for subtype", N);
5479 return;
5480
5481 elsif Has_Record_Rep_Clause (Rectype) then
5482 Error_Msg_N ("duplicate record rep clause ignored", N);
5483 return;
5484
5485 elsif Rep_Item_Too_Late (Rectype, N) then
5486 return;
5487 end if;
5488
5489 -- We know we have a first subtype, now possibly go the the anonymous
5490 -- base type to determine whether Rectype is a record extension.
5491
5492 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
5493 Is_Record_Extension :=
5494 Nkind (Recdef) = N_Derived_Type_Definition
5495 and then Present (Record_Extension_Part (Recdef));
5496
5497 if Present (Mod_Clause (N)) then
5498 declare
5499 Loc : constant Source_Ptr := Sloc (N);
5500 M : constant Node_Id := Mod_Clause (N);
5501 P : constant List_Id := Pragmas_Before (M);
5502 AtM_Nod : Node_Id;
5503
5504 Mod_Val : Uint;
5505 pragma Warnings (Off, Mod_Val);
5506
5507 begin
5508 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
5509
5510 if Warn_On_Obsolescent_Feature then
5511 Error_Msg_N
5512 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
5513 Error_Msg_N
5514 ("\?j?use alignment attribute definition clause instead", N);
5515 end if;
5516
5517 if Present (P) then
5518 Analyze_List (P);
5519 end if;
5520
5521 -- In ASIS_Mode mode, expansion is disabled, but we must convert
5522 -- the Mod clause into an alignment clause anyway, so that the
5523 -- back-end can compute and back-annotate properly the size and
5524 -- alignment of types that may include this record.
5525
5526 -- This seems dubious, this destroys the source tree in a manner
5527 -- not detectable by ASIS ???
5528
5529 if Operating_Mode = Check_Semantics and then ASIS_Mode then
5530 AtM_Nod :=
5531 Make_Attribute_Definition_Clause (Loc,
5532 Name => New_Reference_To (Base_Type (Rectype), Loc),
5533 Chars => Name_Alignment,
5534 Expression => Relocate_Node (Expression (M)));
5535
5536 Set_From_At_Mod (AtM_Nod);
5537 Insert_After (N, AtM_Nod);
5538 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5539 Set_Mod_Clause (N, Empty);
5540
5541 else
5542 -- Get the alignment value to perform error checking
5543
5544 Mod_Val := Get_Alignment_Value (Expression (M));
5545 end if;
5546 end;
5547 end if;
5548
5549 -- For untagged types, clear any existing component clauses for the
5550 -- type. If the type is derived, this is what allows us to override
5551 -- a rep clause for the parent. For type extensions, the representation
5552 -- of the inherited components is inherited, so we want to keep previous
5553 -- component clauses for completeness.
5554
5555 if not Is_Tagged_Type (Rectype) then
5556 Comp := First_Component_Or_Discriminant (Rectype);
5557 while Present (Comp) loop
5558 Set_Component_Clause (Comp, Empty);
5559 Next_Component_Or_Discriminant (Comp);
5560 end loop;
5561 end if;
5562
5563 -- All done if no component clauses
5564
5565 CC := First (Component_Clauses (N));
5566
5567 if No (CC) then
5568 return;
5569 end if;
5570
5571 -- A representation like this applies to the base type
5572
5573 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5574 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5575 Set_Has_Specified_Layout (Base_Type (Rectype));
5576
5577 -- Process the component clauses
5578
5579 while Present (CC) loop
5580
5581 -- Pragma
5582
5583 if Nkind (CC) = N_Pragma then
5584 Analyze (CC);
5585
5586 -- The only pragma of interest is Complete_Representation
5587
5588 if Pragma_Name (CC) = Name_Complete_Representation then
5589 CR_Pragma := CC;
5590 end if;
5591
5592 -- Processing for real component clause
5593
5594 else
5595 Posit := Static_Integer (Position (CC));
5596 Fbit := Static_Integer (First_Bit (CC));
5597 Lbit := Static_Integer (Last_Bit (CC));
5598
5599 if Posit /= No_Uint
5600 and then Fbit /= No_Uint
5601 and then Lbit /= No_Uint
5602 then
5603 if Posit < 0 then
5604 Error_Msg_N
5605 ("position cannot be negative", Position (CC));
5606
5607 elsif Fbit < 0 then
5608 Error_Msg_N
5609 ("first bit cannot be negative", First_Bit (CC));
5610
5611 -- The Last_Bit specified in a component clause must not be
5612 -- less than the First_Bit minus one (RM-13.5.1(10)).
5613
5614 elsif Lbit < Fbit - 1 then
5615 Error_Msg_N
5616 ("last bit cannot be less than first bit minus one",
5617 Last_Bit (CC));
5618
5619 -- Values look OK, so find the corresponding record component
5620 -- Even though the syntax allows an attribute reference for
5621 -- implementation-defined components, GNAT does not allow the
5622 -- tag to get an explicit position.
5623
5624 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5625 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5626 Error_Msg_N ("position of tag cannot be specified", CC);
5627 else
5628 Error_Msg_N ("illegal component name", CC);
5629 end if;
5630
5631 else
5632 Comp := First_Entity (Rectype);
5633 while Present (Comp) loop
5634 exit when Chars (Comp) = Chars (Component_Name (CC));
5635 Next_Entity (Comp);
5636 end loop;
5637
5638 if No (Comp) then
5639
5640 -- Maybe component of base type that is absent from
5641 -- statically constrained first subtype.
5642
5643 Comp := First_Entity (Base_Type (Rectype));
5644 while Present (Comp) loop
5645 exit when Chars (Comp) = Chars (Component_Name (CC));
5646 Next_Entity (Comp);
5647 end loop;
5648 end if;
5649
5650 if No (Comp) then
5651 Error_Msg_N
5652 ("component clause is for non-existent field", CC);
5653
5654 -- Ada 2012 (AI05-0026): Any name that denotes a
5655 -- discriminant of an object of an unchecked union type
5656 -- shall not occur within a record_representation_clause.
5657
5658 -- The general restriction of using record rep clauses on
5659 -- Unchecked_Union types has now been lifted. Since it is
5660 -- possible to introduce a record rep clause which mentions
5661 -- the discriminant of an Unchecked_Union in non-Ada 2012
5662 -- code, this check is applied to all versions of the
5663 -- language.
5664
5665 elsif Ekind (Comp) = E_Discriminant
5666 and then Is_Unchecked_Union (Rectype)
5667 then
5668 Error_Msg_N
5669 ("cannot reference discriminant of unchecked union",
5670 Component_Name (CC));
5671
5672 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5673 Error_Msg_NE
5674 ("component clause not allowed for inherited "
5675 & "component&", CC, Comp);
5676
5677 elsif Present (Component_Clause (Comp)) then
5678
5679 -- Diagnose duplicate rep clause, or check consistency
5680 -- if this is an inherited component. In a double fault,
5681 -- there may be a duplicate inconsistent clause for an
5682 -- inherited component.
5683
5684 if Scope (Original_Record_Component (Comp)) = Rectype
5685 or else Parent (Component_Clause (Comp)) = N
5686 then
5687 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5688 Error_Msg_N ("component clause previously given#", CC);
5689
5690 else
5691 declare
5692 Rep1 : constant Node_Id := Component_Clause (Comp);
5693 begin
5694 if Intval (Position (Rep1)) /=
5695 Intval (Position (CC))
5696 or else Intval (First_Bit (Rep1)) /=
5697 Intval (First_Bit (CC))
5698 or else Intval (Last_Bit (Rep1)) /=
5699 Intval (Last_Bit (CC))
5700 then
5701 Error_Msg_N
5702 ("component clause inconsistent "
5703 & "with representation of ancestor", CC);
5704
5705 elsif Warn_On_Redundant_Constructs then
5706 Error_Msg_N
5707 ("?r?redundant confirming component clause "
5708 & "for component!", CC);
5709 end if;
5710 end;
5711 end if;
5712
5713 -- Normal case where this is the first component clause we
5714 -- have seen for this entity, so set it up properly.
5715
5716 else
5717 -- Make reference for field in record rep clause and set
5718 -- appropriate entity field in the field identifier.
5719
5720 Generate_Reference
5721 (Comp, Component_Name (CC), Set_Ref => False);
5722 Set_Entity (Component_Name (CC), Comp);
5723
5724 -- Update Fbit and Lbit to the actual bit number
5725
5726 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5727 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5728
5729 if Has_Size_Clause (Rectype)
5730 and then RM_Size (Rectype) <= Lbit
5731 then
5732 Error_Msg_N
5733 ("bit number out of range of specified size",
5734 Last_Bit (CC));
5735 else
5736 Set_Component_Clause (Comp, CC);
5737 Set_Component_Bit_Offset (Comp, Fbit);
5738 Set_Esize (Comp, 1 + (Lbit - Fbit));
5739 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5740 Set_Normalized_Position (Comp, Fbit / SSU);
5741
5742 if Warn_On_Overridden_Size
5743 and then Has_Size_Clause (Etype (Comp))
5744 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5745 then
5746 Error_Msg_NE
5747 ("?S?component size overrides size clause for&",
5748 Component_Name (CC), Etype (Comp));
5749 end if;
5750
5751 -- This information is also set in the corresponding
5752 -- component of the base type, found by accessing the
5753 -- Original_Record_Component link if it is present.
5754
5755 Ocomp := Original_Record_Component (Comp);
5756
5757 if Hbit < Lbit then
5758 Hbit := Lbit;
5759 end if;
5760
5761 Check_Size
5762 (Component_Name (CC),
5763 Etype (Comp),
5764 Esize (Comp),
5765 Biased);
5766
5767 Set_Biased
5768 (Comp, First_Node (CC), "component clause", Biased);
5769
5770 if Present (Ocomp) then
5771 Set_Component_Clause (Ocomp, CC);
5772 Set_Component_Bit_Offset (Ocomp, Fbit);
5773 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5774 Set_Normalized_Position (Ocomp, Fbit / SSU);
5775 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5776
5777 Set_Normalized_Position_Max
5778 (Ocomp, Normalized_Position (Ocomp));
5779
5780 -- Note: we don't use Set_Biased here, because we
5781 -- already gave a warning above if needed, and we
5782 -- would get a duplicate for the same name here.
5783
5784 Set_Has_Biased_Representation
5785 (Ocomp, Has_Biased_Representation (Comp));
5786 end if;
5787
5788 if Esize (Comp) < 0 then
5789 Error_Msg_N ("component size is negative", CC);
5790 end if;
5791 end if;
5792 end if;
5793 end if;
5794 end if;
5795 end if;
5796
5797 Next (CC);
5798 end loop;
5799
5800 -- Check missing components if Complete_Representation pragma appeared
5801
5802 if Present (CR_Pragma) then
5803 Comp := First_Component_Or_Discriminant (Rectype);
5804 while Present (Comp) loop
5805 if No (Component_Clause (Comp)) then
5806 Error_Msg_NE
5807 ("missing component clause for &", CR_Pragma, Comp);
5808 end if;
5809
5810 Next_Component_Or_Discriminant (Comp);
5811 end loop;
5812
5813 -- Give missing components warning if required
5814
5815 elsif Warn_On_Unrepped_Components then
5816 declare
5817 Num_Repped_Components : Nat := 0;
5818 Num_Unrepped_Components : Nat := 0;
5819
5820 begin
5821 -- First count number of repped and unrepped components
5822
5823 Comp := First_Component_Or_Discriminant (Rectype);
5824 while Present (Comp) loop
5825 if Present (Component_Clause (Comp)) then
5826 Num_Repped_Components := Num_Repped_Components + 1;
5827 else
5828 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5829 end if;
5830
5831 Next_Component_Or_Discriminant (Comp);
5832 end loop;
5833
5834 -- We are only interested in the case where there is at least one
5835 -- unrepped component, and at least half the components have rep
5836 -- clauses. We figure that if less than half have them, then the
5837 -- partial rep clause is really intentional. If the component
5838 -- type has no underlying type set at this point (as for a generic
5839 -- formal type), we don't know enough to give a warning on the
5840 -- component.
5841
5842 if Num_Unrepped_Components > 0
5843 and then Num_Unrepped_Components < Num_Repped_Components
5844 then
5845 Comp := First_Component_Or_Discriminant (Rectype);
5846 while Present (Comp) loop
5847 if No (Component_Clause (Comp))
5848 and then Comes_From_Source (Comp)
5849 and then Present (Underlying_Type (Etype (Comp)))
5850 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5851 or else Size_Known_At_Compile_Time
5852 (Underlying_Type (Etype (Comp))))
5853 and then not Has_Warnings_Off (Rectype)
5854 then
5855 Error_Msg_Sloc := Sloc (Comp);
5856 Error_Msg_NE
5857 ("?C?no component clause given for & declared #",
5858 N, Comp);
5859 end if;
5860
5861 Next_Component_Or_Discriminant (Comp);
5862 end loop;
5863 end if;
5864 end;
5865 end if;
5866 end Analyze_Record_Representation_Clause;
5867
5868 -------------------------------------------
5869 -- Build_Invariant_Procedure_Declaration --
5870 -------------------------------------------
5871
5872 function Build_Invariant_Procedure_Declaration
5873 (Typ : Entity_Id) return Node_Id
5874 is
5875 Loc : constant Source_Ptr := Sloc (Typ);
5876 Object_Entity : constant Entity_Id :=
5877 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5878 Spec : Node_Id;
5879 SId : Entity_Id;
5880
5881 begin
5882 Set_Etype (Object_Entity, Typ);
5883
5884 -- Check for duplicate definiations.
5885
5886 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
5887 return Empty;
5888 end if;
5889
5890 SId :=
5891 Make_Defining_Identifier (Loc,
5892 Chars => New_External_Name (Chars (Typ), "Invariant"));
5893 Set_Has_Invariants (Typ);
5894 Set_Ekind (SId, E_Procedure);
5895 Set_Is_Invariant_Procedure (SId);
5896 Set_Invariant_Procedure (Typ, SId);
5897
5898 Spec :=
5899 Make_Procedure_Specification (Loc,
5900 Defining_Unit_Name => SId,
5901 Parameter_Specifications => New_List (
5902 Make_Parameter_Specification (Loc,
5903 Defining_Identifier => Object_Entity,
5904 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5905
5906 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5907 end Build_Invariant_Procedure_Declaration;
5908
5909 -------------------------------
5910 -- Build_Invariant_Procedure --
5911 -------------------------------
5912
5913 -- The procedure that is constructed here has the form
5914
5915 -- procedure typInvariant (Ixxx : typ) is
5916 -- begin
5917 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5918 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5919 -- ...
5920 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5921 -- ...
5922 -- end typInvariant;
5923
5924 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5925 Loc : constant Source_Ptr := Sloc (Typ);
5926 Stmts : List_Id;
5927 Spec : Node_Id;
5928 SId : Entity_Id;
5929 PDecl : Node_Id;
5930 PBody : Node_Id;
5931
5932 Visible_Decls : constant List_Id := Visible_Declarations (N);
5933 Private_Decls : constant List_Id := Private_Declarations (N);
5934
5935 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5936 -- Appends statements to Stmts for any invariants in the rep item chain
5937 -- of the given type. If Inherit is False, then we only process entries
5938 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5939 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5940 -- "inherited" to the exception message and generating an informational
5941 -- message about the inheritance of an invariant.
5942
5943 Object_Name : Name_Id;
5944 -- Name for argument of invariant procedure
5945
5946 Object_Entity : Node_Id;
5947 -- The entity of the formal for the procedure
5948
5949 --------------------
5950 -- Add_Invariants --
5951 --------------------
5952
5953 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5954 Ritem : Node_Id;
5955 Arg1 : Node_Id;
5956 Arg2 : Node_Id;
5957 Arg3 : Node_Id;
5958 Exp : Node_Id;
5959 Loc : Source_Ptr;
5960 Assoc : List_Id;
5961 Str : String_Id;
5962
5963 procedure Replace_Type_Reference (N : Node_Id);
5964 -- Replace a single occurrence N of the subtype name with a reference
5965 -- to the formal of the predicate function. N can be an identifier
5966 -- referencing the subtype, or a selected component, representing an
5967 -- appropriately qualified occurrence of the subtype name.
5968
5969 procedure Replace_Type_References is
5970 new Replace_Type_References_Generic (Replace_Type_Reference);
5971 -- Traverse an expression replacing all occurrences of the subtype
5972 -- name with appropriate references to the object that is the formal
5973 -- parameter of the predicate function. Note that we must ensure
5974 -- that the type and entity information is properly set in the
5975 -- replacement node, since we will do a Preanalyze call of this
5976 -- expression without proper visibility of the procedure argument.
5977
5978 ----------------------------
5979 -- Replace_Type_Reference --
5980 ----------------------------
5981
5982 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5983 -- regarding handling of Sloc and Comes_From_Source.
5984
5985 procedure Replace_Type_Reference (N : Node_Id) is
5986 begin
5987
5988 -- Add semantic information to node to be rewritten, for ASIS
5989 -- navigation needs.
5990
5991 if Nkind (N) = N_Identifier then
5992 Set_Entity (N, T);
5993 Set_Etype (N, T);
5994
5995 elsif Nkind (N) = N_Selected_Component then
5996 Analyze (Prefix (N));
5997 Set_Entity (Selector_Name (N), T);
5998 Set_Etype (Selector_Name (N), T);
5999 end if;
6000
6001 -- Invariant'Class, replace with T'Class (obj)
6002
6003 if Class_Present (Ritem) then
6004 Rewrite (N,
6005 Make_Type_Conversion (Sloc (N),
6006 Subtype_Mark =>
6007 Make_Attribute_Reference (Sloc (N),
6008 Prefix => New_Occurrence_Of (T, Sloc (N)),
6009 Attribute_Name => Name_Class),
6010 Expression => Make_Identifier (Sloc (N), Object_Name)));
6011
6012 Set_Entity (Expression (N), Object_Entity);
6013 Set_Etype (Expression (N), Typ);
6014
6015 -- Invariant, replace with obj
6016
6017 else
6018 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6019 Set_Entity (N, Object_Entity);
6020 Set_Etype (N, Typ);
6021 end if;
6022
6023 Set_Comes_From_Source (N, True);
6024 end Replace_Type_Reference;
6025
6026 -- Start of processing for Add_Invariants
6027
6028 begin
6029 Ritem := First_Rep_Item (T);
6030 while Present (Ritem) loop
6031 if Nkind (Ritem) = N_Pragma
6032 and then Pragma_Name (Ritem) = Name_Invariant
6033 then
6034 Arg1 := First (Pragma_Argument_Associations (Ritem));
6035 Arg2 := Next (Arg1);
6036 Arg3 := Next (Arg2);
6037
6038 Arg1 := Get_Pragma_Arg (Arg1);
6039 Arg2 := Get_Pragma_Arg (Arg2);
6040
6041 -- For Inherit case, ignore Invariant, process only Class case
6042
6043 if Inherit then
6044 if not Class_Present (Ritem) then
6045 goto Continue;
6046 end if;
6047
6048 -- For Inherit false, process only item for right type
6049
6050 else
6051 if Entity (Arg1) /= Typ then
6052 goto Continue;
6053 end if;
6054 end if;
6055
6056 if No (Stmts) then
6057 Stmts := Empty_List;
6058 end if;
6059
6060 Exp := New_Copy_Tree (Arg2);
6061
6062 -- Preserve sloc of original pragma Invariant
6063
6064 Loc := Sloc (Ritem);
6065
6066 -- We need to replace any occurrences of the name of the type
6067 -- with references to the object, converted to type'Class in
6068 -- the case of Invariant'Class aspects.
6069
6070 Replace_Type_References (Exp, Chars (T));
6071
6072 -- If this invariant comes from an aspect, find the aspect
6073 -- specification, and replace the saved expression because
6074 -- we need the subtype references replaced for the calls to
6075 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6076 -- and Check_Aspect_At_End_Of_Declarations.
6077
6078 if From_Aspect_Specification (Ritem) then
6079 declare
6080 Aitem : Node_Id;
6081
6082 begin
6083 -- Loop to find corresponding aspect, note that this
6084 -- must be present given the pragma is marked delayed.
6085
6086 Aitem := Next_Rep_Item (Ritem);
6087 while Present (Aitem) loop
6088 if Nkind (Aitem) = N_Aspect_Specification
6089 and then Aspect_Rep_Item (Aitem) = Ritem
6090 then
6091 Set_Entity
6092 (Identifier (Aitem), New_Copy_Tree (Exp));
6093 exit;
6094 end if;
6095
6096 Aitem := Next_Rep_Item (Aitem);
6097 end loop;
6098 end;
6099 end if;
6100
6101 -- Now we need to preanalyze the expression to properly capture
6102 -- the visibility in the visible part. The expression will not
6103 -- be analyzed for real until the body is analyzed, but that is
6104 -- at the end of the private part and has the wrong visibility.
6105
6106 Set_Parent (Exp, N);
6107 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
6108
6109 -- In ASIS mode, even if assertions are not enabled, we must
6110 -- analyze the original expression in the aspect specification
6111 -- because it is part of the original tree.
6112
6113 if ASIS_Mode then
6114 declare
6115 Inv : constant Node_Id :=
6116 Expression (Corresponding_Aspect (Ritem));
6117 begin
6118 Replace_Type_References (Inv, Chars (T));
6119 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
6120 end;
6121 end if;
6122
6123 -- Build first two arguments for Check pragma
6124
6125 Assoc := New_List (
6126 Make_Pragma_Argument_Association (Loc,
6127 Expression => Make_Identifier (Loc, Name_Invariant)),
6128 Make_Pragma_Argument_Association (Loc,
6129 Expression => Exp));
6130
6131 -- Add message if present in Invariant pragma
6132
6133 if Present (Arg3) then
6134 Str := Strval (Get_Pragma_Arg (Arg3));
6135
6136 -- If inherited case, and message starts "failed invariant",
6137 -- change it to be "failed inherited invariant".
6138
6139 if Inherit then
6140 String_To_Name_Buffer (Str);
6141
6142 if Name_Buffer (1 .. 16) = "failed invariant" then
6143 Insert_Str_In_Name_Buffer ("inherited ", 8);
6144 Str := String_From_Name_Buffer;
6145 end if;
6146 end if;
6147
6148 Append_To (Assoc,
6149 Make_Pragma_Argument_Association (Loc,
6150 Expression => Make_String_Literal (Loc, Str)));
6151 end if;
6152
6153 -- Add Check pragma to list of statements
6154
6155 Append_To (Stmts,
6156 Make_Pragma (Loc,
6157 Pragma_Identifier =>
6158 Make_Identifier (Loc, Name_Check),
6159 Pragma_Argument_Associations => Assoc));
6160
6161 -- If Inherited case and option enabled, output info msg. Note
6162 -- that we know this is a case of Invariant'Class.
6163
6164 if Inherit and Opt.List_Inherited_Aspects then
6165 Error_Msg_Sloc := Sloc (Ritem);
6166 Error_Msg_N
6167 ("?L?info: & inherits `Invariant''Class` aspect from #",
6168 Typ);
6169 end if;
6170 end if;
6171
6172 <<Continue>>
6173 Next_Rep_Item (Ritem);
6174 end loop;
6175 end Add_Invariants;
6176
6177 -- Start of processing for Build_Invariant_Procedure
6178
6179 begin
6180 Stmts := No_List;
6181 PDecl := Empty;
6182 PBody := Empty;
6183 SId := Empty;
6184
6185 -- If the aspect specification exists for some view of the type, the
6186 -- declaration for the procedure has been created.
6187
6188 if Has_Invariants (Typ) then
6189 SId := Invariant_Procedure (Typ);
6190 end if;
6191
6192 if Present (SId) then
6193 PDecl := Unit_Declaration_Node (SId);
6194 else
6195 PDecl := Build_Invariant_Procedure_Declaration (Typ);
6196 end if;
6197
6198 -- Recover formal of procedure, for use in the calls to invariant
6199 -- functions (including inherited ones).
6200
6201 Object_Entity :=
6202 Defining_Identifier
6203 (First (Parameter_Specifications (Specification (PDecl))));
6204 Object_Name := Chars (Object_Entity);
6205
6206 -- Add invariants for the current type
6207
6208 Add_Invariants (Typ, Inherit => False);
6209
6210 -- Add invariants for parent types
6211
6212 declare
6213 Current_Typ : Entity_Id;
6214 Parent_Typ : Entity_Id;
6215
6216 begin
6217 Current_Typ := Typ;
6218 loop
6219 Parent_Typ := Etype (Current_Typ);
6220
6221 if Is_Private_Type (Parent_Typ)
6222 and then Present (Full_View (Base_Type (Parent_Typ)))
6223 then
6224 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6225 end if;
6226
6227 exit when Parent_Typ = Current_Typ;
6228
6229 Current_Typ := Parent_Typ;
6230 Add_Invariants (Current_Typ, Inherit => True);
6231 end loop;
6232 end;
6233
6234 -- Build the procedure if we generated at least one Check pragma
6235
6236 if Stmts /= No_List then
6237 Spec := Copy_Separate_Tree (Specification (PDecl));
6238
6239 PBody :=
6240 Make_Subprogram_Body (Loc,
6241 Specification => Spec,
6242 Declarations => Empty_List,
6243 Handled_Statement_Sequence =>
6244 Make_Handled_Sequence_Of_Statements (Loc,
6245 Statements => Stmts));
6246
6247 -- Insert procedure declaration and spec at the appropriate points.
6248 -- If declaration is already analyzed, it was processed by the
6249 -- generated pragma.
6250
6251 if Present (Private_Decls) then
6252
6253 -- The spec goes at the end of visible declarations, but they have
6254 -- already been analyzed, so we need to explicitly do the analyze.
6255
6256 if not Analyzed (PDecl) then
6257 Append_To (Visible_Decls, PDecl);
6258 Analyze (PDecl);
6259 end if;
6260
6261 -- The body goes at the end of the private declarations, which we
6262 -- have not analyzed yet, so we do not need to perform an explicit
6263 -- analyze call. We skip this if there are no private declarations
6264 -- (this is an error that will be caught elsewhere);
6265
6266 Append_To (Private_Decls, PBody);
6267
6268 -- If the invariant appears on the full view of a type, the
6269 -- analysis of the private part is complete, and we must
6270 -- analyze the new body explicitly.
6271
6272 if In_Private_Part (Current_Scope) then
6273 Analyze (PBody);
6274 end if;
6275
6276 -- If there are no private declarations this may be an error that
6277 -- will be diagnosed elsewhere. However, if this is a non-private
6278 -- type that inherits invariants, it needs no completion and there
6279 -- may be no private part. In this case insert invariant procedure
6280 -- at end of current declarative list, and analyze at once, given
6281 -- that the type is about to be frozen.
6282
6283 elsif not Is_Private_Type (Typ) then
6284 Append_To (Visible_Decls, PDecl);
6285 Append_To (Visible_Decls, PBody);
6286 Analyze (PDecl);
6287 Analyze (PBody);
6288 end if;
6289 end if;
6290 end Build_Invariant_Procedure;
6291
6292 -------------------------------
6293 -- Build_Predicate_Functions --
6294 -------------------------------
6295
6296 -- The procedures that are constructed here have the form:
6297
6298 -- function typPredicate (Ixxx : typ) return Boolean is
6299 -- begin
6300 -- return
6301 -- exp1 and then exp2 and then ...
6302 -- and then typ1Predicate (typ1 (Ixxx))
6303 -- and then typ2Predicate (typ2 (Ixxx))
6304 -- and then ...;
6305 -- end typPredicate;
6306
6307 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
6308 -- this is the point at which these expressions get analyzed, providing the
6309 -- required delay, and typ1, typ2, are entities from which predicates are
6310 -- inherited. Note that we do NOT generate Check pragmas, that's because we
6311 -- use this function even if checks are off, e.g. for membership tests.
6312
6313 -- If the expression has at least one Raise_Expression, then we also build
6314 -- the typPredicateM version of the function, in which any occurrence of a
6315 -- Raise_Expression is converted to "return False".
6316
6317 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
6318 Loc : constant Source_Ptr := Sloc (Typ);
6319
6320 Expr : Node_Id;
6321 -- This is the expression for the result of the function. It is
6322 -- is build by connecting the component predicates with AND THEN.
6323
6324 Expr_M : Node_Id;
6325 -- This is the corresponding return expression for the Predicate_M
6326 -- function. It differs in that raise expressions are marked for
6327 -- special expansion (see Process_REs).
6328
6329 Object_Name : constant Name_Id := New_Internal_Name ('I');
6330 -- Name for argument of Predicate procedure. Note that we use the same
6331 -- name for both predicate procedure. That way the reference within the
6332 -- predicate expression is the same in both functions.
6333
6334 Object_Entity : constant Entity_Id :=
6335 Make_Defining_Identifier (Loc, Chars => Object_Name);
6336 -- Entity for argument of Predicate procedure
6337
6338 Object_Entity_M : constant Entity_Id :=
6339 Make_Defining_Identifier (Loc, Chars => Object_Name);
6340 -- Entity for argument of Predicate_M procedure
6341
6342 Raise_Expression_Present : Boolean := False;
6343 -- Set True if Expr has at least one Raise_Expression
6344
6345 Static_Predic : Node_Id := Empty;
6346 -- Set to N_Pragma node for a static predicate if one is encountered
6347
6348 procedure Add_Call (T : Entity_Id);
6349 -- Includes a call to the predicate function for type T in Expr if T
6350 -- has predicates and Predicate_Function (T) is non-empty.
6351
6352 procedure Add_Predicates;
6353 -- Appends expressions for any Predicate pragmas in the rep item chain
6354 -- Typ to Expr. Note that we look only at items for this exact entity.
6355 -- Inheritance of predicates for the parent type is done by calling the
6356 -- Predicate_Function of the parent type, using Add_Call above.
6357
6358 function Test_RE (N : Node_Id) return Traverse_Result;
6359 -- Used in Test_REs, tests one node for being a raise expression, and if
6360 -- so sets Raise_Expression_Present True.
6361
6362 procedure Test_REs is new Traverse_Proc (Test_RE);
6363 -- Tests to see if Expr contains any raise expressions
6364
6365 function Process_RE (N : Node_Id) return Traverse_Result;
6366 -- Used in Process REs, tests if node N is a raise expression, and if
6367 -- so, marks it to be converted to return False.
6368
6369 procedure Process_REs is new Traverse_Proc (Process_RE);
6370 -- Marks any raise expressions in Expr_M to return False
6371
6372 --------------
6373 -- Add_Call --
6374 --------------
6375
6376 procedure Add_Call (T : Entity_Id) is
6377 Exp : Node_Id;
6378
6379 begin
6380 if Present (T) and then Present (Predicate_Function (T)) then
6381 Set_Has_Predicates (Typ);
6382
6383 -- Build the call to the predicate function of T
6384
6385 Exp :=
6386 Make_Predicate_Call
6387 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
6388
6389 -- Add call to evolving expression, using AND THEN if needed
6390
6391 if No (Expr) then
6392 Expr := Exp;
6393 else
6394 Expr :=
6395 Make_And_Then (Loc,
6396 Left_Opnd => Relocate_Node (Expr),
6397 Right_Opnd => Exp);
6398 end if;
6399
6400 -- Output info message on inheritance if required. Note we do not
6401 -- give this information for generic actual types, since it is
6402 -- unwelcome noise in that case in instantiations. We also
6403 -- generally suppress the message in instantiations, and also
6404 -- if it involves internal names.
6405
6406 if Opt.List_Inherited_Aspects
6407 and then not Is_Generic_Actual_Type (Typ)
6408 and then Instantiation_Depth (Sloc (Typ)) = 0
6409 and then not Is_Internal_Name (Chars (T))
6410 and then not Is_Internal_Name (Chars (Typ))
6411 then
6412 Error_Msg_Sloc := Sloc (Predicate_Function (T));
6413 Error_Msg_Node_2 := T;
6414 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
6415 end if;
6416 end if;
6417 end Add_Call;
6418
6419 --------------------
6420 -- Add_Predicates --
6421 --------------------
6422
6423 procedure Add_Predicates is
6424 Ritem : Node_Id;
6425 Arg1 : Node_Id;
6426 Arg2 : Node_Id;
6427
6428 procedure Replace_Type_Reference (N : Node_Id);
6429 -- Replace a single occurrence N of the subtype name with a reference
6430 -- to the formal of the predicate function. N can be an identifier
6431 -- referencing the subtype, or a selected component, representing an
6432 -- appropriately qualified occurrence of the subtype name.
6433
6434 procedure Replace_Type_References is
6435 new Replace_Type_References_Generic (Replace_Type_Reference);
6436 -- Traverse an expression changing every occurrence of an identifier
6437 -- whose name matches the name of the subtype with a reference to
6438 -- the formal parameter of the predicate function.
6439
6440 ----------------------------
6441 -- Replace_Type_Reference --
6442 ----------------------------
6443
6444 procedure Replace_Type_Reference (N : Node_Id) is
6445 begin
6446 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6447 -- Use the Sloc of the usage name, not the defining name
6448
6449 Set_Etype (N, Typ);
6450 Set_Entity (N, Object_Entity);
6451
6452 -- We want to treat the node as if it comes from source, so that
6453 -- ASIS will not ignore it
6454
6455 Set_Comes_From_Source (N, True);
6456 end Replace_Type_Reference;
6457
6458 -- Start of processing for Add_Predicates
6459
6460 begin
6461 Ritem := First_Rep_Item (Typ);
6462 while Present (Ritem) loop
6463 if Nkind (Ritem) = N_Pragma
6464 and then Pragma_Name (Ritem) = Name_Predicate
6465 then
6466 -- Save the static predicate of the type for diagnostics and
6467 -- error reporting purposes.
6468
6469 if Present (Corresponding_Aspect (Ritem))
6470 and then Chars (Identifier (Corresponding_Aspect (Ritem))) =
6471 Name_Static_Predicate
6472 then
6473 Static_Predic := Ritem;
6474 end if;
6475
6476 -- Acquire arguments
6477
6478 Arg1 := First (Pragma_Argument_Associations (Ritem));
6479 Arg2 := Next (Arg1);
6480
6481 Arg1 := Get_Pragma_Arg (Arg1);
6482 Arg2 := Get_Pragma_Arg (Arg2);
6483
6484 -- See if this predicate pragma is for the current type or for
6485 -- its full view. A predicate on a private completion is placed
6486 -- on the partial view beause this is the visible entity that
6487 -- is frozen.
6488
6489 if Entity (Arg1) = Typ
6490 or else Full_View (Entity (Arg1)) = Typ
6491 then
6492 -- We have a match, this entry is for our subtype
6493
6494 -- We need to replace any occurrences of the name of the
6495 -- type with references to the object.
6496
6497 Replace_Type_References (Arg2, Chars (Typ));
6498
6499 -- If this predicate comes from an aspect, find the aspect
6500 -- specification, and replace the saved expression because
6501 -- we need the subtype references replaced for the calls to
6502 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6503 -- and Check_Aspect_At_End_Of_Declarations.
6504
6505 if From_Aspect_Specification (Ritem) then
6506 declare
6507 Aitem : Node_Id;
6508
6509 begin
6510 -- Loop to find corresponding aspect, note that this
6511 -- must be present given the pragma is marked delayed.
6512
6513 Aitem := Next_Rep_Item (Ritem);
6514 loop
6515 if Nkind (Aitem) = N_Aspect_Specification
6516 and then Aspect_Rep_Item (Aitem) = Ritem
6517 then
6518 Set_Entity
6519 (Identifier (Aitem), New_Copy_Tree (Arg2));
6520 exit;
6521 end if;
6522
6523 Aitem := Next_Rep_Item (Aitem);
6524 end loop;
6525 end;
6526 end if;
6527
6528 -- Now we can add the expression
6529
6530 if No (Expr) then
6531 Expr := Relocate_Node (Arg2);
6532
6533 -- There already was a predicate, so add to it
6534
6535 else
6536 Expr :=
6537 Make_And_Then (Loc,
6538 Left_Opnd => Relocate_Node (Expr),
6539 Right_Opnd => Relocate_Node (Arg2));
6540 end if;
6541 end if;
6542 end if;
6543
6544 Next_Rep_Item (Ritem);
6545 end loop;
6546 end Add_Predicates;
6547
6548 ----------------
6549 -- Process_RE --
6550 ----------------
6551
6552 function Process_RE (N : Node_Id) return Traverse_Result is
6553 begin
6554 if Nkind (N) = N_Raise_Expression then
6555 Set_Convert_To_Return_False (N);
6556 return Skip;
6557 else
6558 return OK;
6559 end if;
6560 end Process_RE;
6561
6562 -------------
6563 -- Test_RE --
6564 -------------
6565
6566 function Test_RE (N : Node_Id) return Traverse_Result is
6567 begin
6568 if Nkind (N) = N_Raise_Expression then
6569 Raise_Expression_Present := True;
6570 return Abandon;
6571 else
6572 return OK;
6573 end if;
6574 end Test_RE;
6575
6576 -- Start of processing for Build_Predicate_Functions
6577
6578 begin
6579 -- Return if already built or if type does not have predicates
6580
6581 if not Has_Predicates (Typ)
6582 or else Present (Predicate_Function (Typ))
6583 then
6584 return;
6585 end if;
6586
6587 -- Prepare to construct predicate expression
6588
6589 Expr := Empty;
6590
6591 -- Add Predicates for the current type
6592
6593 Add_Predicates;
6594
6595 -- Add predicates for ancestor if present
6596
6597 declare
6598 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
6599 begin
6600 if Present (Atyp) then
6601 Add_Call (Atyp);
6602 end if;
6603 end;
6604
6605 -- Case where predicates are present
6606
6607 if Present (Expr) then
6608
6609 -- Test for raise expression present
6610
6611 Test_REs (Expr);
6612
6613 -- If raise expression is present, capture a copy of Expr for use
6614 -- in building the predicateM function version later on. For this
6615 -- copy we replace references to Object_Entity by Object_Entity_M.
6616
6617 if Raise_Expression_Present then
6618 declare
6619 Map : constant Elist_Id := New_Elmt_List;
6620 begin
6621 Append_Elmt (Object_Entity, Map);
6622 Append_Elmt (Object_Entity_M, Map);
6623 Expr_M := New_Copy_Tree (Expr, Map => Map);
6624 end;
6625 end if;
6626
6627 -- Build the main predicate function
6628
6629 declare
6630 SId : constant Entity_Id :=
6631 Make_Defining_Identifier (Loc,
6632 Chars => New_External_Name (Chars (Typ), "Predicate"));
6633 -- The entity for the the function spec
6634
6635 SIdB : constant Entity_Id :=
6636 Make_Defining_Identifier (Loc,
6637 Chars => New_External_Name (Chars (Typ), "Predicate"));
6638 -- The entity for the function body
6639
6640 Spec : Node_Id;
6641 FDecl : Node_Id;
6642 FBody : Node_Id;
6643
6644 begin
6645 -- Build function declaration
6646
6647 Set_Ekind (SId, E_Function);
6648 Set_Is_Predicate_Function (SId);
6649 Set_Predicate_Function (Typ, SId);
6650
6651 -- The predicate function is shared between views of a type
6652
6653 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6654 Set_Predicate_Function (Full_View (Typ), SId);
6655 end if;
6656
6657 Spec :=
6658 Make_Function_Specification (Loc,
6659 Defining_Unit_Name => SId,
6660 Parameter_Specifications => New_List (
6661 Make_Parameter_Specification (Loc,
6662 Defining_Identifier => Object_Entity,
6663 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6664 Result_Definition =>
6665 New_Occurrence_Of (Standard_Boolean, Loc));
6666
6667 FDecl :=
6668 Make_Subprogram_Declaration (Loc,
6669 Specification => Spec);
6670
6671 -- Build function body
6672
6673 Spec :=
6674 Make_Function_Specification (Loc,
6675 Defining_Unit_Name => SIdB,
6676 Parameter_Specifications => New_List (
6677 Make_Parameter_Specification (Loc,
6678 Defining_Identifier =>
6679 Make_Defining_Identifier (Loc, Object_Name),
6680 Parameter_Type =>
6681 New_Occurrence_Of (Typ, Loc))),
6682 Result_Definition =>
6683 New_Occurrence_Of (Standard_Boolean, Loc));
6684
6685 FBody :=
6686 Make_Subprogram_Body (Loc,
6687 Specification => Spec,
6688 Declarations => Empty_List,
6689 Handled_Statement_Sequence =>
6690 Make_Handled_Sequence_Of_Statements (Loc,
6691 Statements => New_List (
6692 Make_Simple_Return_Statement (Loc,
6693 Expression => Expr))));
6694
6695 -- Insert declaration before freeze node and body after
6696
6697 Insert_Before_And_Analyze (N, FDecl);
6698 Insert_After_And_Analyze (N, FBody);
6699 end;
6700
6701 -- Test for raise expressions present and if so build M version
6702
6703 if Raise_Expression_Present then
6704 declare
6705 SId : constant Entity_Id :=
6706 Make_Defining_Identifier (Loc,
6707 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6708 -- The entity for the the function spec
6709
6710 SIdB : constant Entity_Id :=
6711 Make_Defining_Identifier (Loc,
6712 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6713 -- The entity for the function body
6714
6715 Spec : Node_Id;
6716 FDecl : Node_Id;
6717 FBody : Node_Id;
6718 BTemp : Entity_Id;
6719
6720 begin
6721 -- Mark any raise expressions for special expansion
6722
6723 Process_REs (Expr_M);
6724
6725 -- Build function declaration
6726
6727 Set_Ekind (SId, E_Function);
6728 Set_Is_Predicate_Function_M (SId);
6729 Set_Predicate_Function_M (Typ, SId);
6730
6731 -- The predicate function is shared between views of a type
6732
6733 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6734 Set_Predicate_Function_M (Full_View (Typ), SId);
6735 end if;
6736
6737 Spec :=
6738 Make_Function_Specification (Loc,
6739 Defining_Unit_Name => SId,
6740 Parameter_Specifications => New_List (
6741 Make_Parameter_Specification (Loc,
6742 Defining_Identifier => Object_Entity_M,
6743 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6744 Result_Definition =>
6745 New_Occurrence_Of (Standard_Boolean, Loc));
6746
6747 FDecl :=
6748 Make_Subprogram_Declaration (Loc,
6749 Specification => Spec);
6750
6751 -- Build function body
6752
6753 Spec :=
6754 Make_Function_Specification (Loc,
6755 Defining_Unit_Name => SIdB,
6756 Parameter_Specifications => New_List (
6757 Make_Parameter_Specification (Loc,
6758 Defining_Identifier =>
6759 Make_Defining_Identifier (Loc, Object_Name),
6760 Parameter_Type =>
6761 New_Occurrence_Of (Typ, Loc))),
6762 Result_Definition =>
6763 New_Occurrence_Of (Standard_Boolean, Loc));
6764
6765 -- Build the body, we declare the boolean expression before
6766 -- doing the return, because we are not really confident of
6767 -- what happens if a return appears within a return!
6768
6769 BTemp :=
6770 Make_Defining_Identifier (Loc,
6771 Chars => New_Internal_Name ('B'));
6772
6773 FBody :=
6774 Make_Subprogram_Body (Loc,
6775 Specification => Spec,
6776
6777 Declarations => New_List (
6778 Make_Object_Declaration (Loc,
6779 Defining_Identifier => BTemp,
6780 Constant_Present => True,
6781 Object_Definition =>
6782 New_Reference_To (Standard_Boolean, Loc),
6783 Expression => Expr_M)),
6784
6785 Handled_Statement_Sequence =>
6786 Make_Handled_Sequence_Of_Statements (Loc,
6787 Statements => New_List (
6788 Make_Simple_Return_Statement (Loc,
6789 Expression => New_Reference_To (BTemp, Loc)))));
6790
6791 -- Insert declaration before freeze node and body after
6792
6793 Insert_Before_And_Analyze (N, FDecl);
6794 Insert_After_And_Analyze (N, FBody);
6795 end;
6796 end if;
6797
6798 if Is_Scalar_Type (Typ) then
6799
6800 -- Attempt to build a static predicate for a discrete or a real
6801 -- subtype. This action may fail because the actual expression may
6802 -- not be static. Note that the presence of an inherited or
6803 -- explicitly declared dynamic predicate is orthogonal to this
6804 -- check because we are only interested in the static predicate.
6805
6806 if Ekind_In (Typ, E_Decimal_Fixed_Point_Subtype,
6807 E_Enumeration_Subtype,
6808 E_Floating_Point_Subtype,
6809 E_Modular_Integer_Subtype,
6810 E_Ordinary_Fixed_Point_Subtype,
6811 E_Signed_Integer_Subtype)
6812 then
6813 Build_Static_Predicate (Typ, Expr, Object_Name);
6814
6815 -- Emit an error when the predicate is categorized as static
6816 -- but its expression is dynamic.
6817
6818 if Present (Static_Predic)
6819 and then No (Static_Predicate (Typ))
6820 then
6821 Error_Msg_F
6822 ("expression does not have required form for "
6823 & "static predicate",
6824 Next (First (Pragma_Argument_Associations
6825 (Static_Predic))));
6826 end if;
6827 end if;
6828
6829 -- If a static predicate applies on other types, that's an error:
6830 -- either the type is scalar but non-static, or it's not even a
6831 -- scalar type. We do not issue an error on generated types, as
6832 -- these may be duplicates of the same error on a source type.
6833
6834 elsif Present (Static_Predic) and then Comes_From_Source (Typ) then
6835 if Is_Scalar_Type (Typ) then
6836 Error_Msg_FE
6837 ("static predicate not allowed for non-static type&",
6838 Typ, Typ);
6839 else
6840 Error_Msg_FE
6841 ("static predicate not allowed for non-scalar type&",
6842 Typ, Typ);
6843 end if;
6844 end if;
6845 end if;
6846 end Build_Predicate_Functions;
6847
6848 ----------------------------
6849 -- Build_Static_Predicate --
6850 ----------------------------
6851
6852 procedure Build_Static_Predicate
6853 (Typ : Entity_Id;
6854 Expr : Node_Id;
6855 Nam : Name_Id)
6856 is
6857 Loc : constant Source_Ptr := Sloc (Expr);
6858
6859 Non_Static : exception;
6860 -- Raised if something non-static is found
6861
6862 Btyp : constant Entity_Id := Base_Type (Typ);
6863
6864 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6865 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6866 -- Low bound and high bound value of base type of Typ
6867
6868 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6869 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6870 -- Low bound and high bound values of static subtype Typ
6871
6872 type REnt is record
6873 Lo, Hi : Uint;
6874 end record;
6875 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6876 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6877 -- value.
6878
6879 type RList is array (Nat range <>) of REnt;
6880 -- A list of ranges. The ranges are sorted in increasing order, and are
6881 -- disjoint (there is a gap of at least one value between each range in
6882 -- the table). A value is in the set of ranges in Rlist if it lies
6883 -- within one of these ranges.
6884
6885 False_Range : constant RList :=
6886 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6887 -- An empty set of ranges represents a range list that can never be
6888 -- satisfied, since there are no ranges in which the value could lie,
6889 -- so it does not lie in any of them. False_Range is a canonical value
6890 -- for this empty set, but general processing should test for an Rlist
6891 -- with length zero (see Is_False predicate), since other null ranges
6892 -- may appear which must be treated as False.
6893
6894 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6895 -- Range representing True, value must be in the base range
6896
6897 function "and" (Left : RList; Right : RList) return RList;
6898 -- And's together two range lists, returning a range list. This is a set
6899 -- intersection operation.
6900
6901 function "or" (Left : RList; Right : RList) return RList;
6902 -- Or's together two range lists, returning a range list. This is a set
6903 -- union operation.
6904
6905 function "not" (Right : RList) return RList;
6906 -- Returns complement of a given range list, i.e. a range list
6907 -- representing all the values in TLo .. THi that are not in the input
6908 -- operand Right.
6909
6910 function Build_Val (V : Uint) return Node_Id;
6911 -- Return an analyzed N_Identifier node referencing this value, suitable
6912 -- for use as an entry in the Static_Predicate list. This node is typed
6913 -- with the base type.
6914
6915 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6916 -- Return an analyzed N_Range node referencing this range, suitable for
6917 -- use as an entry in the Static_Predicate list. This node is typed with
6918 -- the base type.
6919
6920 function Get_RList (Exp : Node_Id) return RList;
6921 -- This is a recursive routine that converts the given expression into a
6922 -- list of ranges, suitable for use in building the static predicate.
6923
6924 function Is_False (R : RList) return Boolean;
6925 pragma Inline (Is_False);
6926 -- Returns True if the given range list is empty, and thus represents a
6927 -- False list of ranges that can never be satisfied.
6928
6929 function Is_True (R : RList) return Boolean;
6930 -- Returns True if R trivially represents the True predicate by having a
6931 -- single range from BLo to BHi.
6932
6933 function Is_Type_Ref (N : Node_Id) return Boolean;
6934 pragma Inline (Is_Type_Ref);
6935 -- Returns if True if N is a reference to the type for the predicate in
6936 -- the expression (i.e. if it is an identifier whose Chars field matches
6937 -- the Nam given in the call).
6938
6939 function Lo_Val (N : Node_Id) return Uint;
6940 -- Given static expression or static range from a Static_Predicate list,
6941 -- gets expression value or low bound of range.
6942
6943 function Hi_Val (N : Node_Id) return Uint;
6944 -- Given static expression or static range from a Static_Predicate list,
6945 -- gets expression value of high bound of range.
6946
6947 function Membership_Entry (N : Node_Id) return RList;
6948 -- Given a single membership entry (range, value, or subtype), returns
6949 -- the corresponding range list. Raises Static_Error if not static.
6950
6951 function Membership_Entries (N : Node_Id) return RList;
6952 -- Given an element on an alternatives list of a membership operation,
6953 -- returns the range list corresponding to this entry and all following
6954 -- entries (i.e. returns the "or" of this list of values).
6955
6956 function Stat_Pred (Typ : Entity_Id) return RList;
6957 -- Given a type, if it has a static predicate, then return the predicate
6958 -- as a range list, otherwise raise Non_Static.
6959
6960 -----------
6961 -- "and" --
6962 -----------
6963
6964 function "and" (Left : RList; Right : RList) return RList is
6965 FEnt : REnt;
6966 -- First range of result
6967
6968 SLeft : Nat := Left'First;
6969 -- Start of rest of left entries
6970
6971 SRight : Nat := Right'First;
6972 -- Start of rest of right entries
6973
6974 begin
6975 -- If either range is True, return the other
6976
6977 if Is_True (Left) then
6978 return Right;
6979 elsif Is_True (Right) then
6980 return Left;
6981 end if;
6982
6983 -- If either range is False, return False
6984
6985 if Is_False (Left) or else Is_False (Right) then
6986 return False_Range;
6987 end if;
6988
6989 -- Loop to remove entries at start that are disjoint, and thus just
6990 -- get discarded from the result entirely.
6991
6992 loop
6993 -- If no operands left in either operand, result is false
6994
6995 if SLeft > Left'Last or else SRight > Right'Last then
6996 return False_Range;
6997
6998 -- Discard first left operand entry if disjoint with right
6999
7000 elsif Left (SLeft).Hi < Right (SRight).Lo then
7001 SLeft := SLeft + 1;
7002
7003 -- Discard first right operand entry if disjoint with left
7004
7005 elsif Right (SRight).Hi < Left (SLeft).Lo then
7006 SRight := SRight + 1;
7007
7008 -- Otherwise we have an overlapping entry
7009
7010 else
7011 exit;
7012 end if;
7013 end loop;
7014
7015 -- Now we have two non-null operands, and first entries overlap. The
7016 -- first entry in the result will be the overlapping part of these
7017 -- two entries.
7018
7019 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7020 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7021
7022 -- Now we can remove the entry that ended at a lower value, since its
7023 -- contribution is entirely contained in Fent.
7024
7025 if Left (SLeft).Hi <= Right (SRight).Hi then
7026 SLeft := SLeft + 1;
7027 else
7028 SRight := SRight + 1;
7029 end if;
7030
7031 -- Compute result by concatenating this first entry with the "and" of
7032 -- the remaining parts of the left and right operands. Note that if
7033 -- either of these is empty, "and" will yield empty, so that we will
7034 -- end up with just Fent, which is what we want in that case.
7035
7036 return
7037 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7038 end "and";
7039
7040 -----------
7041 -- "not" --
7042 -----------
7043
7044 function "not" (Right : RList) return RList is
7045 begin
7046 -- Return True if False range
7047
7048 if Is_False (Right) then
7049 return True_Range;
7050 end if;
7051
7052 -- Return False if True range
7053
7054 if Is_True (Right) then
7055 return False_Range;
7056 end if;
7057
7058 -- Here if not trivial case
7059
7060 declare
7061 Result : RList (1 .. Right'Length + 1);
7062 -- May need one more entry for gap at beginning and end
7063
7064 Count : Nat := 0;
7065 -- Number of entries stored in Result
7066
7067 begin
7068 -- Gap at start
7069
7070 if Right (Right'First).Lo > TLo then
7071 Count := Count + 1;
7072 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7073 end if;
7074
7075 -- Gaps between ranges
7076
7077 for J in Right'First .. Right'Last - 1 loop
7078 Count := Count + 1;
7079 Result (Count) :=
7080 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7081 end loop;
7082
7083 -- Gap at end
7084
7085 if Right (Right'Last).Hi < THi then
7086 Count := Count + 1;
7087 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7088 end if;
7089
7090 return Result (1 .. Count);
7091 end;
7092 end "not";
7093
7094 ----------
7095 -- "or" --
7096 ----------
7097
7098 function "or" (Left : RList; Right : RList) return RList is
7099 FEnt : REnt;
7100 -- First range of result
7101
7102 SLeft : Nat := Left'First;
7103 -- Start of rest of left entries
7104
7105 SRight : Nat := Right'First;
7106 -- Start of rest of right entries
7107
7108 begin
7109 -- If either range is True, return True
7110
7111 if Is_True (Left) or else Is_True (Right) then
7112 return True_Range;
7113 end if;
7114
7115 -- If either range is False (empty), return the other
7116
7117 if Is_False (Left) then
7118 return Right;
7119 elsif Is_False (Right) then
7120 return Left;
7121 end if;
7122
7123 -- Initialize result first entry from left or right operand depending
7124 -- on which starts with the lower range.
7125
7126 if Left (SLeft).Lo < Right (SRight).Lo then
7127 FEnt := Left (SLeft);
7128 SLeft := SLeft + 1;
7129 else
7130 FEnt := Right (SRight);
7131 SRight := SRight + 1;
7132 end if;
7133
7134 -- This loop eats ranges from left and right operands that are
7135 -- contiguous with the first range we are gathering.
7136
7137 loop
7138 -- Eat first entry in left operand if contiguous or overlapped by
7139 -- gathered first operand of result.
7140
7141 if SLeft <= Left'Last
7142 and then Left (SLeft).Lo <= FEnt.Hi + 1
7143 then
7144 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7145 SLeft := SLeft + 1;
7146
7147 -- Eat first entry in right operand if contiguous or overlapped by
7148 -- gathered right operand of result.
7149
7150 elsif SRight <= Right'Last
7151 and then Right (SRight).Lo <= FEnt.Hi + 1
7152 then
7153 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7154 SRight := SRight + 1;
7155
7156 -- All done if no more entries to eat
7157
7158 else
7159 exit;
7160 end if;
7161 end loop;
7162
7163 -- Obtain result as the first entry we just computed, concatenated
7164 -- to the "or" of the remaining results (if one operand is empty,
7165 -- this will just concatenate with the other
7166
7167 return
7168 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7169 end "or";
7170
7171 -----------------
7172 -- Build_Range --
7173 -----------------
7174
7175 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7176 Result : Node_Id;
7177
7178 begin
7179 Result :=
7180 Make_Range (Loc,
7181 Low_Bound => Build_Val (Lo),
7182 High_Bound => Build_Val (Hi));
7183 Set_Etype (Result, Btyp);
7184 Set_Analyzed (Result);
7185
7186 return Result;
7187 end Build_Range;
7188
7189 ---------------
7190 -- Build_Val --
7191 ---------------
7192
7193 function Build_Val (V : Uint) return Node_Id is
7194 Result : Node_Id;
7195
7196 begin
7197 if Is_Enumeration_Type (Typ) then
7198 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7199 else
7200 Result := Make_Integer_Literal (Loc, V);
7201 end if;
7202
7203 Set_Etype (Result, Btyp);
7204 Set_Is_Static_Expression (Result);
7205 Set_Analyzed (Result);
7206 return Result;
7207 end Build_Val;
7208
7209 ---------------
7210 -- Get_RList --
7211 ---------------
7212
7213 function Get_RList (Exp : Node_Id) return RList is
7214 Op : Node_Kind;
7215 Val : Uint;
7216
7217 begin
7218 -- Static expression can only be true or false
7219
7220 if Is_OK_Static_Expression (Exp) then
7221
7222 -- For False
7223
7224 if Expr_Value (Exp) = 0 then
7225 return False_Range;
7226 else
7227 return True_Range;
7228 end if;
7229 end if;
7230
7231 -- Otherwise test node type
7232
7233 Op := Nkind (Exp);
7234
7235 case Op is
7236
7237 -- And
7238
7239 when N_Op_And | N_And_Then =>
7240 return Get_RList (Left_Opnd (Exp))
7241 and
7242 Get_RList (Right_Opnd (Exp));
7243
7244 -- Or
7245
7246 when N_Op_Or | N_Or_Else =>
7247 return Get_RList (Left_Opnd (Exp))
7248 or
7249 Get_RList (Right_Opnd (Exp));
7250
7251 -- Not
7252
7253 when N_Op_Not =>
7254 return not Get_RList (Right_Opnd (Exp));
7255
7256 -- Comparisons of type with static value
7257
7258 when N_Op_Compare =>
7259
7260 -- Type is left operand
7261
7262 if Is_Type_Ref (Left_Opnd (Exp))
7263 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7264 then
7265 Val := Expr_Value (Right_Opnd (Exp));
7266
7267 -- Typ is right operand
7268
7269 elsif Is_Type_Ref (Right_Opnd (Exp))
7270 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7271 then
7272 Val := Expr_Value (Left_Opnd (Exp));
7273
7274 -- Invert sense of comparison
7275
7276 case Op is
7277 when N_Op_Gt => Op := N_Op_Lt;
7278 when N_Op_Lt => Op := N_Op_Gt;
7279 when N_Op_Ge => Op := N_Op_Le;
7280 when N_Op_Le => Op := N_Op_Ge;
7281 when others => null;
7282 end case;
7283
7284 -- Other cases are non-static
7285
7286 else
7287 raise Non_Static;
7288 end if;
7289
7290 -- Construct range according to comparison operation
7291
7292 case Op is
7293 when N_Op_Eq =>
7294 return RList'(1 => REnt'(Val, Val));
7295
7296 when N_Op_Ge =>
7297 return RList'(1 => REnt'(Val, BHi));
7298
7299 when N_Op_Gt =>
7300 return RList'(1 => REnt'(Val + 1, BHi));
7301
7302 when N_Op_Le =>
7303 return RList'(1 => REnt'(BLo, Val));
7304
7305 when N_Op_Lt =>
7306 return RList'(1 => REnt'(BLo, Val - 1));
7307
7308 when N_Op_Ne =>
7309 return RList'(REnt'(BLo, Val - 1),
7310 REnt'(Val + 1, BHi));
7311
7312 when others =>
7313 raise Program_Error;
7314 end case;
7315
7316 -- Membership (IN)
7317
7318 when N_In =>
7319 if not Is_Type_Ref (Left_Opnd (Exp)) then
7320 raise Non_Static;
7321 end if;
7322
7323 if Present (Right_Opnd (Exp)) then
7324 return Membership_Entry (Right_Opnd (Exp));
7325 else
7326 return Membership_Entries (First (Alternatives (Exp)));
7327 end if;
7328
7329 -- Negative membership (NOT IN)
7330
7331 when N_Not_In =>
7332 if not Is_Type_Ref (Left_Opnd (Exp)) then
7333 raise Non_Static;
7334 end if;
7335
7336 if Present (Right_Opnd (Exp)) then
7337 return not Membership_Entry (Right_Opnd (Exp));
7338 else
7339 return not Membership_Entries (First (Alternatives (Exp)));
7340 end if;
7341
7342 -- Function call, may be call to static predicate
7343
7344 when N_Function_Call =>
7345 if Is_Entity_Name (Name (Exp)) then
7346 declare
7347 Ent : constant Entity_Id := Entity (Name (Exp));
7348 begin
7349 if Is_Predicate_Function (Ent)
7350 or else
7351 Is_Predicate_Function_M (Ent)
7352 then
7353 return Stat_Pred (Etype (First_Formal (Ent)));
7354 end if;
7355 end;
7356 end if;
7357
7358 -- Other function call cases are non-static
7359
7360 raise Non_Static;
7361
7362 -- Qualified expression, dig out the expression
7363
7364 when N_Qualified_Expression =>
7365 return Get_RList (Expression (Exp));
7366
7367 -- Expression with actions: if no actions, dig out expression
7368
7369 when N_Expression_With_Actions =>
7370 if Is_Empty_List (Actions (Exp)) then
7371 return Get_RList (Expression (Exp));
7372
7373 else
7374 raise Non_Static;
7375 end if;
7376
7377 -- Xor operator
7378
7379 when N_Op_Xor =>
7380 return (Get_RList (Left_Opnd (Exp))
7381 and not Get_RList (Right_Opnd (Exp)))
7382 or (Get_RList (Right_Opnd (Exp))
7383 and not Get_RList (Left_Opnd (Exp)));
7384
7385 -- Any other node type is non-static
7386
7387 when others =>
7388 raise Non_Static;
7389 end case;
7390 end Get_RList;
7391
7392 ------------
7393 -- Hi_Val --
7394 ------------
7395
7396 function Hi_Val (N : Node_Id) return Uint is
7397 begin
7398 if Is_Static_Expression (N) then
7399 return Expr_Value (N);
7400 else
7401 pragma Assert (Nkind (N) = N_Range);
7402 return Expr_Value (High_Bound (N));
7403 end if;
7404 end Hi_Val;
7405
7406 --------------
7407 -- Is_False --
7408 --------------
7409
7410 function Is_False (R : RList) return Boolean is
7411 begin
7412 return R'Length = 0;
7413 end Is_False;
7414
7415 -------------
7416 -- Is_True --
7417 -------------
7418
7419 function Is_True (R : RList) return Boolean is
7420 begin
7421 return R'Length = 1
7422 and then R (R'First).Lo = BLo
7423 and then R (R'First).Hi = BHi;
7424 end Is_True;
7425
7426 -----------------
7427 -- Is_Type_Ref --
7428 -----------------
7429
7430 function Is_Type_Ref (N : Node_Id) return Boolean is
7431 begin
7432 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
7433 end Is_Type_Ref;
7434
7435 ------------
7436 -- Lo_Val --
7437 ------------
7438
7439 function Lo_Val (N : Node_Id) return Uint is
7440 begin
7441 if Is_Static_Expression (N) then
7442 return Expr_Value (N);
7443 else
7444 pragma Assert (Nkind (N) = N_Range);
7445 return Expr_Value (Low_Bound (N));
7446 end if;
7447 end Lo_Val;
7448
7449 ------------------------
7450 -- Membership_Entries --
7451 ------------------------
7452
7453 function Membership_Entries (N : Node_Id) return RList is
7454 begin
7455 if No (Next (N)) then
7456 return Membership_Entry (N);
7457 else
7458 return Membership_Entry (N) or Membership_Entries (Next (N));
7459 end if;
7460 end Membership_Entries;
7461
7462 ----------------------
7463 -- Membership_Entry --
7464 ----------------------
7465
7466 function Membership_Entry (N : Node_Id) return RList is
7467 Val : Uint;
7468 SLo : Uint;
7469 SHi : Uint;
7470
7471 begin
7472 -- Range case
7473
7474 if Nkind (N) = N_Range then
7475 if not Is_Static_Expression (Low_Bound (N))
7476 or else
7477 not Is_Static_Expression (High_Bound (N))
7478 then
7479 raise Non_Static;
7480 else
7481 SLo := Expr_Value (Low_Bound (N));
7482 SHi := Expr_Value (High_Bound (N));
7483 return RList'(1 => REnt'(SLo, SHi));
7484 end if;
7485
7486 -- Static expression case
7487
7488 elsif Is_Static_Expression (N) then
7489 Val := Expr_Value (N);
7490 return RList'(1 => REnt'(Val, Val));
7491
7492 -- Identifier (other than static expression) case
7493
7494 else pragma Assert (Nkind (N) = N_Identifier);
7495
7496 -- Type case
7497
7498 if Is_Type (Entity (N)) then
7499
7500 -- If type has predicates, process them
7501
7502 if Has_Predicates (Entity (N)) then
7503 return Stat_Pred (Entity (N));
7504
7505 -- For static subtype without predicates, get range
7506
7507 elsif Is_Static_Subtype (Entity (N)) then
7508 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7509 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7510 return RList'(1 => REnt'(SLo, SHi));
7511
7512 -- Any other type makes us non-static
7513
7514 else
7515 raise Non_Static;
7516 end if;
7517
7518 -- Any other kind of identifier in predicate (e.g. a non-static
7519 -- expression value) means this is not a static predicate.
7520
7521 else
7522 raise Non_Static;
7523 end if;
7524 end if;
7525 end Membership_Entry;
7526
7527 ---------------
7528 -- Stat_Pred --
7529 ---------------
7530
7531 function Stat_Pred (Typ : Entity_Id) return RList is
7532 begin
7533 -- Not static if type does not have static predicates
7534
7535 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
7536 raise Non_Static;
7537 end if;
7538
7539 -- Otherwise we convert the predicate list to a range list
7540
7541 declare
7542 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
7543 P : Node_Id;
7544
7545 begin
7546 P := First (Static_Predicate (Typ));
7547 for J in Result'Range loop
7548 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7549 Next (P);
7550 end loop;
7551
7552 return Result;
7553 end;
7554 end Stat_Pred;
7555
7556 -- Start of processing for Build_Static_Predicate
7557
7558 begin
7559 -- Now analyze the expression to see if it is a static predicate
7560
7561 declare
7562 Ranges : constant RList := Get_RList (Expr);
7563 -- Range list from expression if it is static
7564
7565 Plist : List_Id;
7566
7567 begin
7568 -- Convert range list into a form for the static predicate. In the
7569 -- Ranges array, we just have raw ranges, these must be converted
7570 -- to properly typed and analyzed static expressions or range nodes.
7571
7572 -- Note: here we limit ranges to the ranges of the subtype, so that
7573 -- a predicate is always false for values outside the subtype. That
7574 -- seems fine, such values are invalid anyway, and considering them
7575 -- to fail the predicate seems allowed and friendly, and furthermore
7576 -- simplifies processing for case statements and loops.
7577
7578 Plist := New_List;
7579
7580 for J in Ranges'Range loop
7581 declare
7582 Lo : Uint := Ranges (J).Lo;
7583 Hi : Uint := Ranges (J).Hi;
7584
7585 begin
7586 -- Ignore completely out of range entry
7587
7588 if Hi < TLo or else Lo > THi then
7589 null;
7590
7591 -- Otherwise process entry
7592
7593 else
7594 -- Adjust out of range value to subtype range
7595
7596 if Lo < TLo then
7597 Lo := TLo;
7598 end if;
7599
7600 if Hi > THi then
7601 Hi := THi;
7602 end if;
7603
7604 -- Convert range into required form
7605
7606 Append_To (Plist, Build_Range (Lo, Hi));
7607 end if;
7608 end;
7609 end loop;
7610
7611 -- Processing was successful and all entries were static, so now we
7612 -- can store the result as the predicate list.
7613
7614 Set_Static_Predicate (Typ, Plist);
7615
7616 -- The processing for static predicates put the expression into
7617 -- canonical form as a series of ranges. It also eliminated
7618 -- duplicates and collapsed and combined ranges. We might as well
7619 -- replace the alternatives list of the right operand of the
7620 -- membership test with the static predicate list, which will
7621 -- usually be more efficient.
7622
7623 declare
7624 New_Alts : constant List_Id := New_List;
7625 Old_Node : Node_Id;
7626 New_Node : Node_Id;
7627
7628 begin
7629 Old_Node := First (Plist);
7630 while Present (Old_Node) loop
7631 New_Node := New_Copy (Old_Node);
7632
7633 if Nkind (New_Node) = N_Range then
7634 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7635 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7636 end if;
7637
7638 Append_To (New_Alts, New_Node);
7639 Next (Old_Node);
7640 end loop;
7641
7642 -- If empty list, replace by False
7643
7644 if Is_Empty_List (New_Alts) then
7645 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7646
7647 -- Else replace by set membership test
7648
7649 else
7650 Rewrite (Expr,
7651 Make_In (Loc,
7652 Left_Opnd => Make_Identifier (Loc, Nam),
7653 Right_Opnd => Empty,
7654 Alternatives => New_Alts));
7655
7656 -- Resolve new expression in function context
7657
7658 Install_Formals (Predicate_Function (Typ));
7659 Push_Scope (Predicate_Function (Typ));
7660 Analyze_And_Resolve (Expr, Standard_Boolean);
7661 Pop_Scope;
7662 end if;
7663 end;
7664 end;
7665
7666 -- If non-static, return doing nothing
7667
7668 exception
7669 when Non_Static =>
7670 return;
7671 end Build_Static_Predicate;
7672
7673 -----------------------------------------
7674 -- Check_Aspect_At_End_Of_Declarations --
7675 -----------------------------------------
7676
7677 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
7678 Ent : constant Entity_Id := Entity (ASN);
7679 Ident : constant Node_Id := Identifier (ASN);
7680 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7681
7682 End_Decl_Expr : constant Node_Id := Entity (Ident);
7683 -- Expression to be analyzed at end of declarations
7684
7685 Freeze_Expr : constant Node_Id := Expression (ASN);
7686 -- Expression from call to Check_Aspect_At_Freeze_Point
7687
7688 T : constant Entity_Id := Etype (Freeze_Expr);
7689 -- Type required for preanalyze call
7690
7691 Err : Boolean;
7692 -- Set False if error
7693
7694 -- On entry to this procedure, Entity (Ident) contains a copy of the
7695 -- original expression from the aspect, saved for this purpose, and
7696 -- but Expression (Ident) is a preanalyzed copy of the expression,
7697 -- preanalyzed just after the freeze point.
7698
7699 procedure Check_Overloaded_Name;
7700 -- For aspects whose expression is simply a name, this routine checks if
7701 -- the name is overloaded or not. If so, it verifies there is an
7702 -- interpretation that matches the entity obtained at the freeze point,
7703 -- otherwise the compiler complains.
7704
7705 ---------------------------
7706 -- Check_Overloaded_Name --
7707 ---------------------------
7708
7709 procedure Check_Overloaded_Name is
7710 begin
7711 if not Is_Overloaded (End_Decl_Expr) then
7712 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7713
7714 else
7715 Err := True;
7716
7717 declare
7718 Index : Interp_Index;
7719 It : Interp;
7720
7721 begin
7722 Get_First_Interp (End_Decl_Expr, Index, It);
7723 while Present (It.Typ) loop
7724 if It.Nam = Entity (Freeze_Expr) then
7725 Err := False;
7726 exit;
7727 end if;
7728
7729 Get_Next_Interp (Index, It);
7730 end loop;
7731 end;
7732 end if;
7733 end Check_Overloaded_Name;
7734
7735 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7736
7737 begin
7738 -- Case of aspects Dimension, Dimension_System and Synchronization
7739
7740 if A_Id = Aspect_Synchronization then
7741 return;
7742
7743 -- Case of stream attributes, just have to compare entities. However,
7744 -- the expression is just a name (possibly overloaded), and there may
7745 -- be stream operations declared for unrelated types, so we just need
7746 -- to verify that one of these interpretations is the one available at
7747 -- at the freeze point.
7748
7749 elsif A_Id = Aspect_Input or else
7750 A_Id = Aspect_Output or else
7751 A_Id = Aspect_Read or else
7752 A_Id = Aspect_Write
7753 then
7754 Analyze (End_Decl_Expr);
7755 Check_Overloaded_Name;
7756
7757 elsif A_Id = Aspect_Variable_Indexing or else
7758 A_Id = Aspect_Constant_Indexing or else
7759 A_Id = Aspect_Default_Iterator or else
7760 A_Id = Aspect_Iterator_Element
7761 then
7762 -- Make type unfrozen before analysis, to prevent spurious errors
7763 -- about late attributes.
7764
7765 Set_Is_Frozen (Ent, False);
7766 Analyze (End_Decl_Expr);
7767 Set_Is_Frozen (Ent, True);
7768
7769 -- If the end of declarations comes before any other freeze
7770 -- point, the Freeze_Expr is not analyzed: no check needed.
7771
7772 if Analyzed (Freeze_Expr) and then not In_Instance then
7773 Check_Overloaded_Name;
7774 else
7775 Err := False;
7776 end if;
7777
7778 -- All other cases
7779
7780 else
7781 -- In a generic context the aspect expressions have not been
7782 -- preanalyzed, so do it now. There are no conformance checks
7783 -- to perform in this case.
7784
7785 if No (T) then
7786 Check_Aspect_At_Freeze_Point (ASN);
7787 return;
7788
7789 -- The default values attributes may be defined in the private part,
7790 -- and the analysis of the expression may take place when only the
7791 -- partial view is visible. The expression must be scalar, so use
7792 -- the full view to resolve.
7793
7794 elsif (A_Id = Aspect_Default_Value
7795 or else
7796 A_Id = Aspect_Default_Component_Value)
7797 and then Is_Private_Type (T)
7798 then
7799 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7800 else
7801 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7802 end if;
7803
7804 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7805 end if;
7806
7807 -- Output error message if error
7808
7809 if Err then
7810 Error_Msg_NE
7811 ("visibility of aspect for& changes after freeze point",
7812 ASN, Ent);
7813 Error_Msg_NE
7814 ("info: & is frozen here, aspects evaluated at this point??",
7815 Freeze_Node (Ent), Ent);
7816 end if;
7817 end Check_Aspect_At_End_Of_Declarations;
7818
7819 ----------------------------------
7820 -- Check_Aspect_At_Freeze_Point --
7821 ----------------------------------
7822
7823 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7824 Ident : constant Node_Id := Identifier (ASN);
7825 -- Identifier (use Entity field to save expression)
7826
7827 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7828
7829 T : Entity_Id := Empty;
7830 -- Type required for preanalyze call
7831
7832 begin
7833 -- On entry to this procedure, Entity (Ident) contains a copy of the
7834 -- original expression from the aspect, saved for this purpose.
7835
7836 -- On exit from this procedure Entity (Ident) is unchanged, still
7837 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7838 -- of the expression, preanalyzed just after the freeze point.
7839
7840 -- Make a copy of the expression to be preanalyzed
7841
7842 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
7843
7844 -- Find type for preanalyze call
7845
7846 case A_Id is
7847
7848 -- No_Aspect should be impossible
7849
7850 when No_Aspect =>
7851 raise Program_Error;
7852
7853 -- Aspects taking an optional boolean argument
7854
7855 when Boolean_Aspects |
7856 Library_Unit_Aspects =>
7857
7858 T := Standard_Boolean;
7859
7860 -- Aspects corresponding to attribute definition clauses
7861
7862 when Aspect_Address =>
7863 T := RTE (RE_Address);
7864
7865 when Aspect_Attach_Handler =>
7866 T := RTE (RE_Interrupt_ID);
7867
7868 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
7869 T := RTE (RE_Bit_Order);
7870
7871 when Aspect_Convention =>
7872 return;
7873
7874 when Aspect_CPU =>
7875 T := RTE (RE_CPU_Range);
7876
7877 -- Default_Component_Value is resolved with the component type
7878
7879 when Aspect_Default_Component_Value =>
7880 T := Component_Type (Entity (ASN));
7881
7882 -- Default_Value is resolved with the type entity in question
7883
7884 when Aspect_Default_Value =>
7885 T := Entity (ASN);
7886
7887 -- Depends is a delayed aspect because it mentiones names first
7888 -- introduced by aspect Global which is already delayed. There is
7889 -- no action to be taken with respect to the aspect itself as the
7890 -- analysis is done by the corresponding pragma.
7891
7892 when Aspect_Depends =>
7893 return;
7894
7895 when Aspect_Dispatching_Domain =>
7896 T := RTE (RE_Dispatching_Domain);
7897
7898 when Aspect_External_Tag =>
7899 T := Standard_String;
7900
7901 when Aspect_External_Name =>
7902 T := Standard_String;
7903
7904 -- Global is a delayed aspect because it may reference names that
7905 -- have not been declared yet. There is no action to be taken with
7906 -- respect to the aspect itself as the reference checking is done
7907 -- on the corresponding pragma.
7908
7909 when Aspect_Global =>
7910 return;
7911
7912 when Aspect_Link_Name =>
7913 T := Standard_String;
7914
7915 when Aspect_Priority | Aspect_Interrupt_Priority =>
7916 T := Standard_Integer;
7917
7918 when Aspect_Relative_Deadline =>
7919 T := RTE (RE_Time_Span);
7920
7921 when Aspect_Small =>
7922 T := Universal_Real;
7923
7924 -- For a simple storage pool, we have to retrieve the type of the
7925 -- pool object associated with the aspect's corresponding attribute
7926 -- definition clause.
7927
7928 when Aspect_Simple_Storage_Pool =>
7929 T := Etype (Expression (Aspect_Rep_Item (ASN)));
7930
7931 when Aspect_Storage_Pool =>
7932 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
7933
7934 when Aspect_Alignment |
7935 Aspect_Component_Size |
7936 Aspect_Machine_Radix |
7937 Aspect_Object_Size |
7938 Aspect_Size |
7939 Aspect_Storage_Size |
7940 Aspect_Stream_Size |
7941 Aspect_Value_Size =>
7942 T := Any_Integer;
7943
7944 when Aspect_Synchronization =>
7945 return;
7946
7947 -- Special case, the expression of these aspects is just an entity
7948 -- that does not need any resolution, so just analyze.
7949
7950 when Aspect_Input |
7951 Aspect_Output |
7952 Aspect_Read |
7953 Aspect_Suppress |
7954 Aspect_Unsuppress |
7955 Aspect_Warnings |
7956 Aspect_Write =>
7957 Analyze (Expression (ASN));
7958 return;
7959
7960 -- Same for Iterator aspects, where the expression is a function
7961 -- name. Legality rules are checked separately.
7962
7963 when Aspect_Constant_Indexing |
7964 Aspect_Default_Iterator |
7965 Aspect_Iterator_Element |
7966 Aspect_Variable_Indexing =>
7967 Analyze (Expression (ASN));
7968 return;
7969
7970 -- Invariant/Predicate take boolean expressions
7971
7972 when Aspect_Dynamic_Predicate |
7973 Aspect_Invariant |
7974 Aspect_Predicate |
7975 Aspect_Static_Predicate |
7976 Aspect_Type_Invariant =>
7977 T := Standard_Boolean;
7978
7979 -- Here is the list of aspects that don't require delay analysis
7980
7981 when Aspect_Abstract_State |
7982 Aspect_Contract_Cases |
7983 Aspect_Dimension |
7984 Aspect_Dimension_System |
7985 Aspect_Implicit_Dereference |
7986 Aspect_Initial_Condition |
7987 Aspect_Initializes |
7988 Aspect_Post |
7989 Aspect_Postcondition |
7990 Aspect_Pre |
7991 Aspect_Precondition |
7992 Aspect_Refined_Depends |
7993 Aspect_Refined_Global |
7994 Aspect_Refined_Post |
7995 Aspect_Refined_State |
7996 Aspect_SPARK_Mode |
7997 Aspect_Test_Case =>
7998 raise Program_Error;
7999
8000 end case;
8001
8002 -- Do the preanalyze call
8003
8004 Preanalyze_Spec_Expression (Expression (ASN), T);
8005 end Check_Aspect_At_Freeze_Point;
8006
8007 -----------------------------------
8008 -- Check_Constant_Address_Clause --
8009 -----------------------------------
8010
8011 procedure Check_Constant_Address_Clause
8012 (Expr : Node_Id;
8013 U_Ent : Entity_Id)
8014 is
8015 procedure Check_At_Constant_Address (Nod : Node_Id);
8016 -- Checks that the given node N represents a name whose 'Address is
8017 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8018 -- address value is the same at the point of declaration of U_Ent and at
8019 -- the time of elaboration of the address clause.
8020
8021 procedure Check_Expr_Constants (Nod : Node_Id);
8022 -- Checks that Nod meets the requirements for a constant address clause
8023 -- in the sense of the enclosing procedure.
8024
8025 procedure Check_List_Constants (Lst : List_Id);
8026 -- Check that all elements of list Lst meet the requirements for a
8027 -- constant address clause in the sense of the enclosing procedure.
8028
8029 -------------------------------
8030 -- Check_At_Constant_Address --
8031 -------------------------------
8032
8033 procedure Check_At_Constant_Address (Nod : Node_Id) is
8034 begin
8035 if Is_Entity_Name (Nod) then
8036 if Present (Address_Clause (Entity ((Nod)))) then
8037 Error_Msg_NE
8038 ("invalid address clause for initialized object &!",
8039 Nod, U_Ent);
8040 Error_Msg_NE
8041 ("address for& cannot" &
8042 " depend on another address clause! (RM 13.1(22))!",
8043 Nod, U_Ent);
8044
8045 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8046 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8047 then
8048 Error_Msg_NE
8049 ("invalid address clause for initialized object &!",
8050 Nod, U_Ent);
8051 Error_Msg_Node_2 := U_Ent;
8052 Error_Msg_NE
8053 ("\& must be defined before & (RM 13.1(22))!",
8054 Nod, Entity (Nod));
8055 end if;
8056
8057 elsif Nkind (Nod) = N_Selected_Component then
8058 declare
8059 T : constant Entity_Id := Etype (Prefix (Nod));
8060
8061 begin
8062 if (Is_Record_Type (T)
8063 and then Has_Discriminants (T))
8064 or else
8065 (Is_Access_Type (T)
8066 and then Is_Record_Type (Designated_Type (T))
8067 and then Has_Discriminants (Designated_Type (T)))
8068 then
8069 Error_Msg_NE
8070 ("invalid address clause for initialized object &!",
8071 Nod, U_Ent);
8072 Error_Msg_N
8073 ("\address cannot depend on component" &
8074 " of discriminated record (RM 13.1(22))!",
8075 Nod);
8076 else
8077 Check_At_Constant_Address (Prefix (Nod));
8078 end if;
8079 end;
8080
8081 elsif Nkind (Nod) = N_Indexed_Component then
8082 Check_At_Constant_Address (Prefix (Nod));
8083 Check_List_Constants (Expressions (Nod));
8084
8085 else
8086 Check_Expr_Constants (Nod);
8087 end if;
8088 end Check_At_Constant_Address;
8089
8090 --------------------------
8091 -- Check_Expr_Constants --
8092 --------------------------
8093
8094 procedure Check_Expr_Constants (Nod : Node_Id) is
8095 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8096 Ent : Entity_Id := Empty;
8097
8098 begin
8099 if Nkind (Nod) in N_Has_Etype
8100 and then Etype (Nod) = Any_Type
8101 then
8102 return;
8103 end if;
8104
8105 case Nkind (Nod) is
8106 when N_Empty | N_Error =>
8107 return;
8108
8109 when N_Identifier | N_Expanded_Name =>
8110 Ent := Entity (Nod);
8111
8112 -- We need to look at the original node if it is different
8113 -- from the node, since we may have rewritten things and
8114 -- substituted an identifier representing the rewrite.
8115
8116 if Original_Node (Nod) /= Nod then
8117 Check_Expr_Constants (Original_Node (Nod));
8118
8119 -- If the node is an object declaration without initial
8120 -- value, some code has been expanded, and the expression
8121 -- is not constant, even if the constituents might be
8122 -- acceptable, as in A'Address + offset.
8123
8124 if Ekind (Ent) = E_Variable
8125 and then
8126 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8127 and then
8128 No (Expression (Declaration_Node (Ent)))
8129 then
8130 Error_Msg_NE
8131 ("invalid address clause for initialized object &!",
8132 Nod, U_Ent);
8133
8134 -- If entity is constant, it may be the result of expanding
8135 -- a check. We must verify that its declaration appears
8136 -- before the object in question, else we also reject the
8137 -- address clause.
8138
8139 elsif Ekind (Ent) = E_Constant
8140 and then In_Same_Source_Unit (Ent, U_Ent)
8141 and then Sloc (Ent) > Loc_U_Ent
8142 then
8143 Error_Msg_NE
8144 ("invalid address clause for initialized object &!",
8145 Nod, U_Ent);
8146 end if;
8147
8148 return;
8149 end if;
8150
8151 -- Otherwise look at the identifier and see if it is OK
8152
8153 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8154 or else Is_Type (Ent)
8155 then
8156 return;
8157
8158 elsif
8159 Ekind (Ent) = E_Constant
8160 or else
8161 Ekind (Ent) = E_In_Parameter
8162 then
8163 -- This is the case where we must have Ent defined before
8164 -- U_Ent. Clearly if they are in different units this
8165 -- requirement is met since the unit containing Ent is
8166 -- already processed.
8167
8168 if not In_Same_Source_Unit (Ent, U_Ent) then
8169 return;
8170
8171 -- Otherwise location of Ent must be before the location
8172 -- of U_Ent, that's what prior defined means.
8173
8174 elsif Sloc (Ent) < Loc_U_Ent then
8175 return;
8176
8177 else
8178 Error_Msg_NE
8179 ("invalid address clause for initialized object &!",
8180 Nod, U_Ent);
8181 Error_Msg_Node_2 := U_Ent;
8182 Error_Msg_NE
8183 ("\& must be defined before & (RM 13.1(22))!",
8184 Nod, Ent);
8185 end if;
8186
8187 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
8188 Check_Expr_Constants (Original_Node (Nod));
8189
8190 else
8191 Error_Msg_NE
8192 ("invalid address clause for initialized object &!",
8193 Nod, U_Ent);
8194
8195 if Comes_From_Source (Ent) then
8196 Error_Msg_NE
8197 ("\reference to variable& not allowed"
8198 & " (RM 13.1(22))!", Nod, Ent);
8199 else
8200 Error_Msg_N
8201 ("non-static expression not allowed"
8202 & " (RM 13.1(22))!", Nod);
8203 end if;
8204 end if;
8205
8206 when N_Integer_Literal =>
8207
8208 -- If this is a rewritten unchecked conversion, in a system
8209 -- where Address is an integer type, always use the base type
8210 -- for a literal value. This is user-friendly and prevents
8211 -- order-of-elaboration issues with instances of unchecked
8212 -- conversion.
8213
8214 if Nkind (Original_Node (Nod)) = N_Function_Call then
8215 Set_Etype (Nod, Base_Type (Etype (Nod)));
8216 end if;
8217
8218 when N_Real_Literal |
8219 N_String_Literal |
8220 N_Character_Literal =>
8221 return;
8222
8223 when N_Range =>
8224 Check_Expr_Constants (Low_Bound (Nod));
8225 Check_Expr_Constants (High_Bound (Nod));
8226
8227 when N_Explicit_Dereference =>
8228 Check_Expr_Constants (Prefix (Nod));
8229
8230 when N_Indexed_Component =>
8231 Check_Expr_Constants (Prefix (Nod));
8232 Check_List_Constants (Expressions (Nod));
8233
8234 when N_Slice =>
8235 Check_Expr_Constants (Prefix (Nod));
8236 Check_Expr_Constants (Discrete_Range (Nod));
8237
8238 when N_Selected_Component =>
8239 Check_Expr_Constants (Prefix (Nod));
8240
8241 when N_Attribute_Reference =>
8242 if Nam_In (Attribute_Name (Nod), Name_Address,
8243 Name_Access,
8244 Name_Unchecked_Access,
8245 Name_Unrestricted_Access)
8246 then
8247 Check_At_Constant_Address (Prefix (Nod));
8248
8249 else
8250 Check_Expr_Constants (Prefix (Nod));
8251 Check_List_Constants (Expressions (Nod));
8252 end if;
8253
8254 when N_Aggregate =>
8255 Check_List_Constants (Component_Associations (Nod));
8256 Check_List_Constants (Expressions (Nod));
8257
8258 when N_Component_Association =>
8259 Check_Expr_Constants (Expression (Nod));
8260
8261 when N_Extension_Aggregate =>
8262 Check_Expr_Constants (Ancestor_Part (Nod));
8263 Check_List_Constants (Component_Associations (Nod));
8264 Check_List_Constants (Expressions (Nod));
8265
8266 when N_Null =>
8267 return;
8268
8269 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
8270 Check_Expr_Constants (Left_Opnd (Nod));
8271 Check_Expr_Constants (Right_Opnd (Nod));
8272
8273 when N_Unary_Op =>
8274 Check_Expr_Constants (Right_Opnd (Nod));
8275
8276 when N_Type_Conversion |
8277 N_Qualified_Expression |
8278 N_Allocator |
8279 N_Unchecked_Type_Conversion =>
8280 Check_Expr_Constants (Expression (Nod));
8281
8282 when N_Function_Call =>
8283 if not Is_Pure (Entity (Name (Nod))) then
8284 Error_Msg_NE
8285 ("invalid address clause for initialized object &!",
8286 Nod, U_Ent);
8287
8288 Error_Msg_NE
8289 ("\function & is not pure (RM 13.1(22))!",
8290 Nod, Entity (Name (Nod)));
8291
8292 else
8293 Check_List_Constants (Parameter_Associations (Nod));
8294 end if;
8295
8296 when N_Parameter_Association =>
8297 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
8298
8299 when others =>
8300 Error_Msg_NE
8301 ("invalid address clause for initialized object &!",
8302 Nod, U_Ent);
8303 Error_Msg_NE
8304 ("\must be constant defined before& (RM 13.1(22))!",
8305 Nod, U_Ent);
8306 end case;
8307 end Check_Expr_Constants;
8308
8309 --------------------------
8310 -- Check_List_Constants --
8311 --------------------------
8312
8313 procedure Check_List_Constants (Lst : List_Id) is
8314 Nod1 : Node_Id;
8315
8316 begin
8317 if Present (Lst) then
8318 Nod1 := First (Lst);
8319 while Present (Nod1) loop
8320 Check_Expr_Constants (Nod1);
8321 Next (Nod1);
8322 end loop;
8323 end if;
8324 end Check_List_Constants;
8325
8326 -- Start of processing for Check_Constant_Address_Clause
8327
8328 begin
8329 -- If rep_clauses are to be ignored, no need for legality checks. In
8330 -- particular, no need to pester user about rep clauses that violate
8331 -- the rule on constant addresses, given that these clauses will be
8332 -- removed by Freeze before they reach the back end.
8333
8334 if not Ignore_Rep_Clauses then
8335 Check_Expr_Constants (Expr);
8336 end if;
8337 end Check_Constant_Address_Clause;
8338
8339 ---------------------------
8340 -- Check_Pool_Size_Clash --
8341 ---------------------------
8342
8343 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
8344 Post : Node_Id;
8345
8346 begin
8347 -- We need to find out which one came first. Note that in the case of
8348 -- aspects mixed with pragmas there are cases where the processing order
8349 -- is reversed, which is why we do the check here.
8350
8351 if Sloc (SP) < Sloc (SS) then
8352 Error_Msg_Sloc := Sloc (SP);
8353 Post := SS;
8354 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
8355
8356 else
8357 Error_Msg_Sloc := Sloc (SS);
8358 Post := SP;
8359 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
8360 end if;
8361
8362 Error_Msg_N
8363 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
8364 end Check_Pool_Size_Clash;
8365
8366 ----------------------------------------
8367 -- Check_Record_Representation_Clause --
8368 ----------------------------------------
8369
8370 procedure Check_Record_Representation_Clause (N : Node_Id) is
8371 Loc : constant Source_Ptr := Sloc (N);
8372 Ident : constant Node_Id := Identifier (N);
8373 Rectype : Entity_Id;
8374 Fent : Entity_Id;
8375 CC : Node_Id;
8376 Fbit : Uint;
8377 Lbit : Uint;
8378 Hbit : Uint := Uint_0;
8379 Comp : Entity_Id;
8380 Pcomp : Entity_Id;
8381
8382 Max_Bit_So_Far : Uint;
8383 -- Records the maximum bit position so far. If all field positions
8384 -- are monotonically increasing, then we can skip the circuit for
8385 -- checking for overlap, since no overlap is possible.
8386
8387 Tagged_Parent : Entity_Id := Empty;
8388 -- This is set in the case of a derived tagged type for which we have
8389 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
8390 -- positioned by record representation clauses). In this case we must
8391 -- check for overlap between components of this tagged type, and the
8392 -- components of its parent. Tagged_Parent will point to this parent
8393 -- type. For all other cases Tagged_Parent is left set to Empty.
8394
8395 Parent_Last_Bit : Uint;
8396 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
8397 -- last bit position for any field in the parent type. We only need to
8398 -- check overlap for fields starting below this point.
8399
8400 Overlap_Check_Required : Boolean;
8401 -- Used to keep track of whether or not an overlap check is required
8402
8403 Overlap_Detected : Boolean := False;
8404 -- Set True if an overlap is detected
8405
8406 Ccount : Natural := 0;
8407 -- Number of component clauses in record rep clause
8408
8409 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
8410 -- Given two entities for record components or discriminants, checks
8411 -- if they have overlapping component clauses and issues errors if so.
8412
8413 procedure Find_Component;
8414 -- Finds component entity corresponding to current component clause (in
8415 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
8416 -- start/stop bits for the field. If there is no matching component or
8417 -- if the matching component does not have a component clause, then
8418 -- that's an error and Comp is set to Empty, but no error message is
8419 -- issued, since the message was already given. Comp is also set to
8420 -- Empty if the current "component clause" is in fact a pragma.
8421
8422 -----------------------------
8423 -- Check_Component_Overlap --
8424 -----------------------------
8425
8426 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
8427 CC1 : constant Node_Id := Component_Clause (C1_Ent);
8428 CC2 : constant Node_Id := Component_Clause (C2_Ent);
8429
8430 begin
8431 if Present (CC1) and then Present (CC2) then
8432
8433 -- Exclude odd case where we have two tag components in the same
8434 -- record, both at location zero. This seems a bit strange, but
8435 -- it seems to happen in some circumstances, perhaps on an error.
8436
8437 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
8438 return;
8439 end if;
8440
8441 -- Here we check if the two fields overlap
8442
8443 declare
8444 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
8445 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
8446 E1 : constant Uint := S1 + Esize (C1_Ent);
8447 E2 : constant Uint := S2 + Esize (C2_Ent);
8448
8449 begin
8450 if E2 <= S1 or else E1 <= S2 then
8451 null;
8452 else
8453 Error_Msg_Node_2 := Component_Name (CC2);
8454 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
8455 Error_Msg_Node_1 := Component_Name (CC1);
8456 Error_Msg_N
8457 ("component& overlaps & #", Component_Name (CC1));
8458 Overlap_Detected := True;
8459 end if;
8460 end;
8461 end if;
8462 end Check_Component_Overlap;
8463
8464 --------------------
8465 -- Find_Component --
8466 --------------------
8467
8468 procedure Find_Component is
8469
8470 procedure Search_Component (R : Entity_Id);
8471 -- Search components of R for a match. If found, Comp is set
8472
8473 ----------------------
8474 -- Search_Component --
8475 ----------------------
8476
8477 procedure Search_Component (R : Entity_Id) is
8478 begin
8479 Comp := First_Component_Or_Discriminant (R);
8480 while Present (Comp) loop
8481
8482 -- Ignore error of attribute name for component name (we
8483 -- already gave an error message for this, so no need to
8484 -- complain here)
8485
8486 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
8487 null;
8488 else
8489 exit when Chars (Comp) = Chars (Component_Name (CC));
8490 end if;
8491
8492 Next_Component_Or_Discriminant (Comp);
8493 end loop;
8494 end Search_Component;
8495
8496 -- Start of processing for Find_Component
8497
8498 begin
8499 -- Return with Comp set to Empty if we have a pragma
8500
8501 if Nkind (CC) = N_Pragma then
8502 Comp := Empty;
8503 return;
8504 end if;
8505
8506 -- Search current record for matching component
8507
8508 Search_Component (Rectype);
8509
8510 -- If not found, maybe component of base type discriminant that is
8511 -- absent from statically constrained first subtype.
8512
8513 if No (Comp) then
8514 Search_Component (Base_Type (Rectype));
8515 end if;
8516
8517 -- If no component, or the component does not reference the component
8518 -- clause in question, then there was some previous error for which
8519 -- we already gave a message, so just return with Comp Empty.
8520
8521 if No (Comp) or else Component_Clause (Comp) /= CC then
8522 Check_Error_Detected;
8523 Comp := Empty;
8524
8525 -- Normal case where we have a component clause
8526
8527 else
8528 Fbit := Component_Bit_Offset (Comp);
8529 Lbit := Fbit + Esize (Comp) - 1;
8530 end if;
8531 end Find_Component;
8532
8533 -- Start of processing for Check_Record_Representation_Clause
8534
8535 begin
8536 Find_Type (Ident);
8537 Rectype := Entity (Ident);
8538
8539 if Rectype = Any_Type then
8540 return;
8541 else
8542 Rectype := Underlying_Type (Rectype);
8543 end if;
8544
8545 -- See if we have a fully repped derived tagged type
8546
8547 declare
8548 PS : constant Entity_Id := Parent_Subtype (Rectype);
8549
8550 begin
8551 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
8552 Tagged_Parent := PS;
8553
8554 -- Find maximum bit of any component of the parent type
8555
8556 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
8557 Pcomp := First_Entity (Tagged_Parent);
8558 while Present (Pcomp) loop
8559 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
8560 if Component_Bit_Offset (Pcomp) /= No_Uint
8561 and then Known_Static_Esize (Pcomp)
8562 then
8563 Parent_Last_Bit :=
8564 UI_Max
8565 (Parent_Last_Bit,
8566 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
8567 end if;
8568
8569 Next_Entity (Pcomp);
8570 end if;
8571 end loop;
8572 end if;
8573 end;
8574
8575 -- All done if no component clauses
8576
8577 CC := First (Component_Clauses (N));
8578
8579 if No (CC) then
8580 return;
8581 end if;
8582
8583 -- If a tag is present, then create a component clause that places it
8584 -- at the start of the record (otherwise gigi may place it after other
8585 -- fields that have rep clauses).
8586
8587 Fent := First_Entity (Rectype);
8588
8589 if Nkind (Fent) = N_Defining_Identifier
8590 and then Chars (Fent) = Name_uTag
8591 then
8592 Set_Component_Bit_Offset (Fent, Uint_0);
8593 Set_Normalized_Position (Fent, Uint_0);
8594 Set_Normalized_First_Bit (Fent, Uint_0);
8595 Set_Normalized_Position_Max (Fent, Uint_0);
8596 Init_Esize (Fent, System_Address_Size);
8597
8598 Set_Component_Clause (Fent,
8599 Make_Component_Clause (Loc,
8600 Component_Name => Make_Identifier (Loc, Name_uTag),
8601
8602 Position => Make_Integer_Literal (Loc, Uint_0),
8603 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8604 Last_Bit =>
8605 Make_Integer_Literal (Loc,
8606 UI_From_Int (System_Address_Size))));
8607
8608 Ccount := Ccount + 1;
8609 end if;
8610
8611 Max_Bit_So_Far := Uint_Minus_1;
8612 Overlap_Check_Required := False;
8613
8614 -- Process the component clauses
8615
8616 while Present (CC) loop
8617 Find_Component;
8618
8619 if Present (Comp) then
8620 Ccount := Ccount + 1;
8621
8622 -- We need a full overlap check if record positions non-monotonic
8623
8624 if Fbit <= Max_Bit_So_Far then
8625 Overlap_Check_Required := True;
8626 end if;
8627
8628 Max_Bit_So_Far := Lbit;
8629
8630 -- Check bit position out of range of specified size
8631
8632 if Has_Size_Clause (Rectype)
8633 and then RM_Size (Rectype) <= Lbit
8634 then
8635 Error_Msg_N
8636 ("bit number out of range of specified size",
8637 Last_Bit (CC));
8638
8639 -- Check for overlap with tag component
8640
8641 else
8642 if Is_Tagged_Type (Rectype)
8643 and then Fbit < System_Address_Size
8644 then
8645 Error_Msg_NE
8646 ("component overlaps tag field of&",
8647 Component_Name (CC), Rectype);
8648 Overlap_Detected := True;
8649 end if;
8650
8651 if Hbit < Lbit then
8652 Hbit := Lbit;
8653 end if;
8654 end if;
8655
8656 -- Check parent overlap if component might overlap parent field
8657
8658 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
8659 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
8660 while Present (Pcomp) loop
8661 if not Is_Tag (Pcomp)
8662 and then Chars (Pcomp) /= Name_uParent
8663 then
8664 Check_Component_Overlap (Comp, Pcomp);
8665 end if;
8666
8667 Next_Component_Or_Discriminant (Pcomp);
8668 end loop;
8669 end if;
8670 end if;
8671
8672 Next (CC);
8673 end loop;
8674
8675 -- Now that we have processed all the component clauses, check for
8676 -- overlap. We have to leave this till last, since the components can
8677 -- appear in any arbitrary order in the representation clause.
8678
8679 -- We do not need this check if all specified ranges were monotonic,
8680 -- as recorded by Overlap_Check_Required being False at this stage.
8681
8682 -- This first section checks if there are any overlapping entries at
8683 -- all. It does this by sorting all entries and then seeing if there are
8684 -- any overlaps. If there are none, then that is decisive, but if there
8685 -- are overlaps, they may still be OK (they may result from fields in
8686 -- different variants).
8687
8688 if Overlap_Check_Required then
8689 Overlap_Check1 : declare
8690
8691 OC_Fbit : array (0 .. Ccount) of Uint;
8692 -- First-bit values for component clauses, the value is the offset
8693 -- of the first bit of the field from start of record. The zero
8694 -- entry is for use in sorting.
8695
8696 OC_Lbit : array (0 .. Ccount) of Uint;
8697 -- Last-bit values for component clauses, the value is the offset
8698 -- of the last bit of the field from start of record. The zero
8699 -- entry is for use in sorting.
8700
8701 OC_Count : Natural := 0;
8702 -- Count of entries in OC_Fbit and OC_Lbit
8703
8704 function OC_Lt (Op1, Op2 : Natural) return Boolean;
8705 -- Compare routine for Sort
8706
8707 procedure OC_Move (From : Natural; To : Natural);
8708 -- Move routine for Sort
8709
8710 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8711
8712 -----------
8713 -- OC_Lt --
8714 -----------
8715
8716 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8717 begin
8718 return OC_Fbit (Op1) < OC_Fbit (Op2);
8719 end OC_Lt;
8720
8721 -------------
8722 -- OC_Move --
8723 -------------
8724
8725 procedure OC_Move (From : Natural; To : Natural) is
8726 begin
8727 OC_Fbit (To) := OC_Fbit (From);
8728 OC_Lbit (To) := OC_Lbit (From);
8729 end OC_Move;
8730
8731 -- Start of processing for Overlap_Check
8732
8733 begin
8734 CC := First (Component_Clauses (N));
8735 while Present (CC) loop
8736
8737 -- Exclude component clause already marked in error
8738
8739 if not Error_Posted (CC) then
8740 Find_Component;
8741
8742 if Present (Comp) then
8743 OC_Count := OC_Count + 1;
8744 OC_Fbit (OC_Count) := Fbit;
8745 OC_Lbit (OC_Count) := Lbit;
8746 end if;
8747 end if;
8748
8749 Next (CC);
8750 end loop;
8751
8752 Sorting.Sort (OC_Count);
8753
8754 Overlap_Check_Required := False;
8755 for J in 1 .. OC_Count - 1 loop
8756 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8757 Overlap_Check_Required := True;
8758 exit;
8759 end if;
8760 end loop;
8761 end Overlap_Check1;
8762 end if;
8763
8764 -- If Overlap_Check_Required is still True, then we have to do the full
8765 -- scale overlap check, since we have at least two fields that do
8766 -- overlap, and we need to know if that is OK since they are in
8767 -- different variant, or whether we have a definite problem.
8768
8769 if Overlap_Check_Required then
8770 Overlap_Check2 : declare
8771 C1_Ent, C2_Ent : Entity_Id;
8772 -- Entities of components being checked for overlap
8773
8774 Clist : Node_Id;
8775 -- Component_List node whose Component_Items are being checked
8776
8777 Citem : Node_Id;
8778 -- Component declaration for component being checked
8779
8780 begin
8781 C1_Ent := First_Entity (Base_Type (Rectype));
8782
8783 -- Loop through all components in record. For each component check
8784 -- for overlap with any of the preceding elements on the component
8785 -- list containing the component and also, if the component is in
8786 -- a variant, check against components outside the case structure.
8787 -- This latter test is repeated recursively up the variant tree.
8788
8789 Main_Component_Loop : while Present (C1_Ent) loop
8790 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8791 goto Continue_Main_Component_Loop;
8792 end if;
8793
8794 -- Skip overlap check if entity has no declaration node. This
8795 -- happens with discriminants in constrained derived types.
8796 -- Possibly we are missing some checks as a result, but that
8797 -- does not seem terribly serious.
8798
8799 if No (Declaration_Node (C1_Ent)) then
8800 goto Continue_Main_Component_Loop;
8801 end if;
8802
8803 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8804
8805 -- Loop through component lists that need checking. Check the
8806 -- current component list and all lists in variants above us.
8807
8808 Component_List_Loop : loop
8809
8810 -- If derived type definition, go to full declaration
8811 -- If at outer level, check discriminants if there are any.
8812
8813 if Nkind (Clist) = N_Derived_Type_Definition then
8814 Clist := Parent (Clist);
8815 end if;
8816
8817 -- Outer level of record definition, check discriminants
8818
8819 if Nkind_In (Clist, N_Full_Type_Declaration,
8820 N_Private_Type_Declaration)
8821 then
8822 if Has_Discriminants (Defining_Identifier (Clist)) then
8823 C2_Ent :=
8824 First_Discriminant (Defining_Identifier (Clist));
8825 while Present (C2_Ent) loop
8826 exit when C1_Ent = C2_Ent;
8827 Check_Component_Overlap (C1_Ent, C2_Ent);
8828 Next_Discriminant (C2_Ent);
8829 end loop;
8830 end if;
8831
8832 -- Record extension case
8833
8834 elsif Nkind (Clist) = N_Derived_Type_Definition then
8835 Clist := Empty;
8836
8837 -- Otherwise check one component list
8838
8839 else
8840 Citem := First (Component_Items (Clist));
8841 while Present (Citem) loop
8842 if Nkind (Citem) = N_Component_Declaration then
8843 C2_Ent := Defining_Identifier (Citem);
8844 exit when C1_Ent = C2_Ent;
8845 Check_Component_Overlap (C1_Ent, C2_Ent);
8846 end if;
8847
8848 Next (Citem);
8849 end loop;
8850 end if;
8851
8852 -- Check for variants above us (the parent of the Clist can
8853 -- be a variant, in which case its parent is a variant part,
8854 -- and the parent of the variant part is a component list
8855 -- whose components must all be checked against the current
8856 -- component for overlap).
8857
8858 if Nkind (Parent (Clist)) = N_Variant then
8859 Clist := Parent (Parent (Parent (Clist)));
8860
8861 -- Check for possible discriminant part in record, this
8862 -- is treated essentially as another level in the
8863 -- recursion. For this case the parent of the component
8864 -- list is the record definition, and its parent is the
8865 -- full type declaration containing the discriminant
8866 -- specifications.
8867
8868 elsif Nkind (Parent (Clist)) = N_Record_Definition then
8869 Clist := Parent (Parent ((Clist)));
8870
8871 -- If neither of these two cases, we are at the top of
8872 -- the tree.
8873
8874 else
8875 exit Component_List_Loop;
8876 end if;
8877 end loop Component_List_Loop;
8878
8879 <<Continue_Main_Component_Loop>>
8880 Next_Entity (C1_Ent);
8881
8882 end loop Main_Component_Loop;
8883 end Overlap_Check2;
8884 end if;
8885
8886 -- The following circuit deals with warning on record holes (gaps). We
8887 -- skip this check if overlap was detected, since it makes sense for the
8888 -- programmer to fix this illegality before worrying about warnings.
8889
8890 if not Overlap_Detected and Warn_On_Record_Holes then
8891 Record_Hole_Check : declare
8892 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
8893 -- Full declaration of record type
8894
8895 procedure Check_Component_List
8896 (CL : Node_Id;
8897 Sbit : Uint;
8898 DS : List_Id);
8899 -- Check component list CL for holes. The starting bit should be
8900 -- Sbit. which is zero for the main record component list and set
8901 -- appropriately for recursive calls for variants. DS is set to
8902 -- a list of discriminant specifications to be included in the
8903 -- consideration of components. It is No_List if none to consider.
8904
8905 --------------------------
8906 -- Check_Component_List --
8907 --------------------------
8908
8909 procedure Check_Component_List
8910 (CL : Node_Id;
8911 Sbit : Uint;
8912 DS : List_Id)
8913 is
8914 Compl : Integer;
8915
8916 begin
8917 Compl := Integer (List_Length (Component_Items (CL)));
8918
8919 if DS /= No_List then
8920 Compl := Compl + Integer (List_Length (DS));
8921 end if;
8922
8923 declare
8924 Comps : array (Natural range 0 .. Compl) of Entity_Id;
8925 -- Gather components (zero entry is for sort routine)
8926
8927 Ncomps : Natural := 0;
8928 -- Number of entries stored in Comps (starting at Comps (1))
8929
8930 Citem : Node_Id;
8931 -- One component item or discriminant specification
8932
8933 Nbit : Uint;
8934 -- Starting bit for next component
8935
8936 CEnt : Entity_Id;
8937 -- Component entity
8938
8939 Variant : Node_Id;
8940 -- One variant
8941
8942 function Lt (Op1, Op2 : Natural) return Boolean;
8943 -- Compare routine for Sort
8944
8945 procedure Move (From : Natural; To : Natural);
8946 -- Move routine for Sort
8947
8948 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8949
8950 --------
8951 -- Lt --
8952 --------
8953
8954 function Lt (Op1, Op2 : Natural) return Boolean is
8955 begin
8956 return Component_Bit_Offset (Comps (Op1))
8957 <
8958 Component_Bit_Offset (Comps (Op2));
8959 end Lt;
8960
8961 ----------
8962 -- Move --
8963 ----------
8964
8965 procedure Move (From : Natural; To : Natural) is
8966 begin
8967 Comps (To) := Comps (From);
8968 end Move;
8969
8970 begin
8971 -- Gather discriminants into Comp
8972
8973 if DS /= No_List then
8974 Citem := First (DS);
8975 while Present (Citem) loop
8976 if Nkind (Citem) = N_Discriminant_Specification then
8977 declare
8978 Ent : constant Entity_Id :=
8979 Defining_Identifier (Citem);
8980 begin
8981 if Ekind (Ent) = E_Discriminant then
8982 Ncomps := Ncomps + 1;
8983 Comps (Ncomps) := Ent;
8984 end if;
8985 end;
8986 end if;
8987
8988 Next (Citem);
8989 end loop;
8990 end if;
8991
8992 -- Gather component entities into Comp
8993
8994 Citem := First (Component_Items (CL));
8995 while Present (Citem) loop
8996 if Nkind (Citem) = N_Component_Declaration then
8997 Ncomps := Ncomps + 1;
8998 Comps (Ncomps) := Defining_Identifier (Citem);
8999 end if;
9000
9001 Next (Citem);
9002 end loop;
9003
9004 -- Now sort the component entities based on the first bit.
9005 -- Note we already know there are no overlapping components.
9006
9007 Sorting.Sort (Ncomps);
9008
9009 -- Loop through entries checking for holes
9010
9011 Nbit := Sbit;
9012 for J in 1 .. Ncomps loop
9013 CEnt := Comps (J);
9014 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9015
9016 if Error_Msg_Uint_1 > 0 then
9017 Error_Msg_NE
9018 ("?H?^-bit gap before component&",
9019 Component_Name (Component_Clause (CEnt)), CEnt);
9020 end if;
9021
9022 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9023 end loop;
9024
9025 -- Process variant parts recursively if present
9026
9027 if Present (Variant_Part (CL)) then
9028 Variant := First (Variants (Variant_Part (CL)));
9029 while Present (Variant) loop
9030 Check_Component_List
9031 (Component_List (Variant), Nbit, No_List);
9032 Next (Variant);
9033 end loop;
9034 end if;
9035 end;
9036 end Check_Component_List;
9037
9038 -- Start of processing for Record_Hole_Check
9039
9040 begin
9041 declare
9042 Sbit : Uint;
9043
9044 begin
9045 if Is_Tagged_Type (Rectype) then
9046 Sbit := UI_From_Int (System_Address_Size);
9047 else
9048 Sbit := Uint_0;
9049 end if;
9050
9051 if Nkind (Decl) = N_Full_Type_Declaration
9052 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9053 then
9054 Check_Component_List
9055 (Component_List (Type_Definition (Decl)),
9056 Sbit,
9057 Discriminant_Specifications (Decl));
9058 end if;
9059 end;
9060 end Record_Hole_Check;
9061 end if;
9062
9063 -- For records that have component clauses for all components, and whose
9064 -- size is less than or equal to 32, we need to know the size in the
9065 -- front end to activate possible packed array processing where the
9066 -- component type is a record.
9067
9068 -- At this stage Hbit + 1 represents the first unused bit from all the
9069 -- component clauses processed, so if the component clauses are
9070 -- complete, then this is the length of the record.
9071
9072 -- For records longer than System.Storage_Unit, and for those where not
9073 -- all components have component clauses, the back end determines the
9074 -- length (it may for example be appropriate to round up the size
9075 -- to some convenient boundary, based on alignment considerations, etc).
9076
9077 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9078
9079 -- Nothing to do if at least one component has no component clause
9080
9081 Comp := First_Component_Or_Discriminant (Rectype);
9082 while Present (Comp) loop
9083 exit when No (Component_Clause (Comp));
9084 Next_Component_Or_Discriminant (Comp);
9085 end loop;
9086
9087 -- If we fall out of loop, all components have component clauses
9088 -- and so we can set the size to the maximum value.
9089
9090 if No (Comp) then
9091 Set_RM_Size (Rectype, Hbit + 1);
9092 end if;
9093 end if;
9094 end Check_Record_Representation_Clause;
9095
9096 ----------------
9097 -- Check_Size --
9098 ----------------
9099
9100 procedure Check_Size
9101 (N : Node_Id;
9102 T : Entity_Id;
9103 Siz : Uint;
9104 Biased : out Boolean)
9105 is
9106 UT : constant Entity_Id := Underlying_Type (T);
9107 M : Uint;
9108
9109 begin
9110 Biased := False;
9111
9112 -- Reject patently improper size values.
9113
9114 if Is_Elementary_Type (T)
9115 and then Siz > UI_From_Int (Int'Last)
9116 then
9117 Error_Msg_N ("Size value too large for elementary type", N);
9118
9119 if Nkind (Original_Node (N)) = N_Op_Expon then
9120 Error_Msg_N
9121 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9122 end if;
9123 end if;
9124
9125 -- Dismiss generic types
9126
9127 if Is_Generic_Type (T)
9128 or else
9129 Is_Generic_Type (UT)
9130 or else
9131 Is_Generic_Type (Root_Type (UT))
9132 then
9133 return;
9134
9135 -- Guard against previous errors
9136
9137 elsif No (UT) or else UT = Any_Type then
9138 Check_Error_Detected;
9139 return;
9140
9141 -- Check case of bit packed array
9142
9143 elsif Is_Array_Type (UT)
9144 and then Known_Static_Component_Size (UT)
9145 and then Is_Bit_Packed_Array (UT)
9146 then
9147 declare
9148 Asiz : Uint;
9149 Indx : Node_Id;
9150 Ityp : Entity_Id;
9151
9152 begin
9153 Asiz := Component_Size (UT);
9154 Indx := First_Index (UT);
9155 loop
9156 Ityp := Etype (Indx);
9157
9158 -- If non-static bound, then we are not in the business of
9159 -- trying to check the length, and indeed an error will be
9160 -- issued elsewhere, since sizes of non-static array types
9161 -- cannot be set implicitly or explicitly.
9162
9163 if not Is_Static_Subtype (Ityp) then
9164 return;
9165 end if;
9166
9167 -- Otherwise accumulate next dimension
9168
9169 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9170 Expr_Value (Type_Low_Bound (Ityp)) +
9171 Uint_1);
9172
9173 Next_Index (Indx);
9174 exit when No (Indx);
9175 end loop;
9176
9177 if Asiz <= Siz then
9178 return;
9179
9180 else
9181 Error_Msg_Uint_1 := Asiz;
9182 Error_Msg_NE
9183 ("size for& too small, minimum allowed is ^", N, T);
9184 Set_Esize (T, Asiz);
9185 Set_RM_Size (T, Asiz);
9186 end if;
9187 end;
9188
9189 -- All other composite types are ignored
9190
9191 elsif Is_Composite_Type (UT) then
9192 return;
9193
9194 -- For fixed-point types, don't check minimum if type is not frozen,
9195 -- since we don't know all the characteristics of the type that can
9196 -- affect the size (e.g. a specified small) till freeze time.
9197
9198 elsif Is_Fixed_Point_Type (UT)
9199 and then not Is_Frozen (UT)
9200 then
9201 null;
9202
9203 -- Cases for which a minimum check is required
9204
9205 else
9206 -- Ignore if specified size is correct for the type
9207
9208 if Known_Esize (UT) and then Siz = Esize (UT) then
9209 return;
9210 end if;
9211
9212 -- Otherwise get minimum size
9213
9214 M := UI_From_Int (Minimum_Size (UT));
9215
9216 if Siz < M then
9217
9218 -- Size is less than minimum size, but one possibility remains
9219 -- that we can manage with the new size if we bias the type.
9220
9221 M := UI_From_Int (Minimum_Size (UT, Biased => True));
9222
9223 if Siz < M then
9224 Error_Msg_Uint_1 := M;
9225 Error_Msg_NE
9226 ("size for& too small, minimum allowed is ^", N, T);
9227 Set_Esize (T, M);
9228 Set_RM_Size (T, M);
9229 else
9230 Biased := True;
9231 end if;
9232 end if;
9233 end if;
9234 end Check_Size;
9235
9236 --------------------------
9237 -- Freeze_Entity_Checks --
9238 --------------------------
9239
9240 procedure Freeze_Entity_Checks (N : Node_Id) is
9241 E : constant Entity_Id := Entity (N);
9242
9243 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
9244 -- True in non-generic case. Some of the processing here is skipped
9245 -- for the generic case since it is not needed. Basically in the
9246 -- generic case, we only need to do stuff that might generate error
9247 -- messages or warnings.
9248 begin
9249 -- Remember that we are processing a freezing entity. Required to
9250 -- ensure correct decoration of internal entities associated with
9251 -- interfaces (see New_Overloaded_Entity).
9252
9253 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
9254
9255 -- For tagged types covering interfaces add internal entities that link
9256 -- the primitives of the interfaces with the primitives that cover them.
9257 -- Note: These entities were originally generated only when generating
9258 -- code because their main purpose was to provide support to initialize
9259 -- the secondary dispatch tables. They are now generated also when
9260 -- compiling with no code generation to provide ASIS the relationship
9261 -- between interface primitives and tagged type primitives. They are
9262 -- also used to locate primitives covering interfaces when processing
9263 -- generics (see Derive_Subprograms).
9264
9265 -- This is not needed in the generic case
9266
9267 if Ada_Version >= Ada_2005
9268 and then Non_Generic_Case
9269 and then Ekind (E) = E_Record_Type
9270 and then Is_Tagged_Type (E)
9271 and then not Is_Interface (E)
9272 and then Has_Interfaces (E)
9273 then
9274 -- This would be a good common place to call the routine that checks
9275 -- overriding of interface primitives (and thus factorize calls to
9276 -- Check_Abstract_Overriding located at different contexts in the
9277 -- compiler). However, this is not possible because it causes
9278 -- spurious errors in case of late overriding.
9279
9280 Add_Internal_Interface_Entities (E);
9281 end if;
9282
9283 -- Check CPP types
9284
9285 if Ekind (E) = E_Record_Type
9286 and then Is_CPP_Class (E)
9287 and then Is_Tagged_Type (E)
9288 and then Tagged_Type_Expansion
9289 then
9290 if CPP_Num_Prims (E) = 0 then
9291
9292 -- If the CPP type has user defined components then it must import
9293 -- primitives from C++. This is required because if the C++ class
9294 -- has no primitives then the C++ compiler does not added the _tag
9295 -- component to the type.
9296
9297 if First_Entity (E) /= Last_Entity (E) then
9298 Error_Msg_N
9299 ("'C'P'P type must import at least one primitive from C++??",
9300 E);
9301 end if;
9302 end if;
9303
9304 -- Check that all its primitives are abstract or imported from C++.
9305 -- Check also availability of the C++ constructor.
9306
9307 declare
9308 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
9309 Elmt : Elmt_Id;
9310 Error_Reported : Boolean := False;
9311 Prim : Node_Id;
9312
9313 begin
9314 Elmt := First_Elmt (Primitive_Operations (E));
9315 while Present (Elmt) loop
9316 Prim := Node (Elmt);
9317
9318 if Comes_From_Source (Prim) then
9319 if Is_Abstract_Subprogram (Prim) then
9320 null;
9321
9322 elsif not Is_Imported (Prim)
9323 or else Convention (Prim) /= Convention_CPP
9324 then
9325 Error_Msg_N
9326 ("primitives of 'C'P'P types must be imported from C++ "
9327 & "or abstract??", Prim);
9328
9329 elsif not Has_Constructors
9330 and then not Error_Reported
9331 then
9332 Error_Msg_Name_1 := Chars (E);
9333 Error_Msg_N
9334 ("??'C'P'P constructor required for type %", Prim);
9335 Error_Reported := True;
9336 end if;
9337 end if;
9338
9339 Next_Elmt (Elmt);
9340 end loop;
9341 end;
9342 end if;
9343
9344 -- Check Ada derivation of CPP type
9345
9346 if Expander_Active -- why? losing errors in -gnatc mode???
9347 and then Tagged_Type_Expansion
9348 and then Ekind (E) = E_Record_Type
9349 and then Etype (E) /= E
9350 and then Is_CPP_Class (Etype (E))
9351 and then CPP_Num_Prims (Etype (E)) > 0
9352 and then not Is_CPP_Class (E)
9353 and then not Has_CPP_Constructors (Etype (E))
9354 then
9355 -- If the parent has C++ primitives but it has no constructor then
9356 -- check that all the primitives are overridden in this derivation;
9357 -- otherwise the constructor of the parent is needed to build the
9358 -- dispatch table.
9359
9360 declare
9361 Elmt : Elmt_Id;
9362 Prim : Node_Id;
9363
9364 begin
9365 Elmt := First_Elmt (Primitive_Operations (E));
9366 while Present (Elmt) loop
9367 Prim := Node (Elmt);
9368
9369 if not Is_Abstract_Subprogram (Prim)
9370 and then No (Interface_Alias (Prim))
9371 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
9372 then
9373 Error_Msg_Name_1 := Chars (Etype (E));
9374 Error_Msg_N
9375 ("'C'P'P constructor required for parent type %", E);
9376 exit;
9377 end if;
9378
9379 Next_Elmt (Elmt);
9380 end loop;
9381 end;
9382 end if;
9383
9384 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
9385
9386 -- If we have a type with predicates, build predicate function. This
9387 -- is not needed in the generic casee
9388
9389 if Non_Generic_Case and then Is_Type (E) and then Has_Predicates (E) then
9390 Build_Predicate_Functions (E, N);
9391 end if;
9392
9393 -- If type has delayed aspects, this is where we do the preanalysis at
9394 -- the freeze point, as part of the consistent visibility check. Note
9395 -- that this must be done after calling Build_Predicate_Functions or
9396 -- Build_Invariant_Procedure since these subprograms fix occurrences of
9397 -- the subtype name in the saved expression so that they will not cause
9398 -- trouble in the preanalysis.
9399
9400 -- This is also not needed in the generic case
9401
9402 if Non_Generic_Case
9403 and then Has_Delayed_Aspects (E)
9404 and then Scope (E) = Current_Scope
9405 then
9406 -- Retrieve the visibility to the discriminants in order to properly
9407 -- analyze the aspects.
9408
9409 Push_Scope_And_Install_Discriminants (E);
9410
9411 declare
9412 Ritem : Node_Id;
9413
9414 begin
9415 -- Look for aspect specification entries for this entity
9416
9417 Ritem := First_Rep_Item (E);
9418 while Present (Ritem) loop
9419 if Nkind (Ritem) = N_Aspect_Specification
9420 and then Entity (Ritem) = E
9421 and then Is_Delayed_Aspect (Ritem)
9422 then
9423 Check_Aspect_At_Freeze_Point (Ritem);
9424 end if;
9425
9426 Next_Rep_Item (Ritem);
9427 end loop;
9428 end;
9429
9430 Uninstall_Discriminants_And_Pop_Scope (E);
9431 end if;
9432
9433 -- For a record type, deal with variant parts. This has to be delayed
9434 -- to this point, because of the issue of statically precicated
9435 -- subtypes, which we have to ensure are frozen before checking
9436 -- choices, since we need to have the static choice list set.
9437
9438 if Is_Record_Type (E) then
9439 Check_Variant_Part : declare
9440 D : constant Node_Id := Declaration_Node (E);
9441 T : Node_Id;
9442 C : Node_Id;
9443 VP : Node_Id;
9444
9445 Others_Present : Boolean;
9446 pragma Warnings (Off, Others_Present);
9447 -- Indicates others present, not used in this case
9448
9449 procedure Non_Static_Choice_Error (Choice : Node_Id);
9450 -- Error routine invoked by the generic instantiation below when
9451 -- the variant part has a non static choice.
9452
9453 procedure Process_Declarations (Variant : Node_Id);
9454 -- Processes declarations associated with a variant. We analyzed
9455 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
9456 -- but we still need the recursive call to Check_Choices for any
9457 -- nested variant to get its choices properly processed. This is
9458 -- also where we expand out the choices if expansion is active.
9459
9460 package Variant_Choices_Processing is new
9461 Generic_Check_Choices
9462 (Process_Empty_Choice => No_OP,
9463 Process_Non_Static_Choice => Non_Static_Choice_Error,
9464 Process_Associated_Node => Process_Declarations);
9465 use Variant_Choices_Processing;
9466
9467 -----------------------------
9468 -- Non_Static_Choice_Error --
9469 -----------------------------
9470
9471 procedure Non_Static_Choice_Error (Choice : Node_Id) is
9472 begin
9473 Flag_Non_Static_Expr
9474 ("choice given in variant part is not static!", Choice);
9475 end Non_Static_Choice_Error;
9476
9477 --------------------------
9478 -- Process_Declarations --
9479 --------------------------
9480
9481 procedure Process_Declarations (Variant : Node_Id) is
9482 CL : constant Node_Id := Component_List (Variant);
9483 VP : Node_Id;
9484
9485 begin
9486 -- Check for static predicate present in this variant
9487
9488 if Has_SP_Choice (Variant) then
9489
9490 -- Here we expand. You might expect to find this call in
9491 -- Expand_N_Variant_Part, but that is called when we first
9492 -- see the variant part, and we cannot do this expansion
9493 -- earlier than the freeze point, since for statically
9494 -- predicated subtypes, the predicate is not known till
9495 -- the freeze point.
9496
9497 -- Furthermore, we do this expansion even if the expander
9498 -- is not active, because other semantic processing, e.g.
9499 -- for aggregates, requires the expanded list of choices.
9500
9501 -- If the expander is not active, then we can't just clobber
9502 -- the list since it would invalidate the ASIS -gnatct tree.
9503 -- So we have to rewrite the variant part with a Rewrite
9504 -- call that replaces it with a copy and clobber the copy.
9505
9506 if not Expander_Active then
9507 declare
9508 NewV : constant Node_Id := New_Copy (Variant);
9509 begin
9510 Set_Discrete_Choices
9511 (NewV, New_Copy_List (Discrete_Choices (Variant)));
9512 Rewrite (Variant, NewV);
9513 end;
9514 end if;
9515
9516 Expand_Static_Predicates_In_Choices (Variant);
9517 end if;
9518
9519 -- We don't need to worry about the declarations in the variant
9520 -- (since they were analyzed by Analyze_Choices when we first
9521 -- encountered the variant), but we do need to take care of
9522 -- expansion of any nested variants.
9523
9524 if not Null_Present (CL) then
9525 VP := Variant_Part (CL);
9526
9527 if Present (VP) then
9528 Check_Choices
9529 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9530 end if;
9531 end if;
9532 end Process_Declarations;
9533
9534 -- Start of processing for Check_Variant_Part
9535
9536 begin
9537 -- Find component list
9538
9539 C := Empty;
9540
9541 if Nkind (D) = N_Full_Type_Declaration then
9542 T := Type_Definition (D);
9543
9544 if Nkind (T) = N_Record_Definition then
9545 C := Component_List (T);
9546
9547 elsif Nkind (T) = N_Derived_Type_Definition
9548 and then Present (Record_Extension_Part (T))
9549 then
9550 C := Component_List (Record_Extension_Part (T));
9551 end if;
9552 end if;
9553
9554 -- Case of variant part present
9555
9556 if Present (C) and then Present (Variant_Part (C)) then
9557 VP := Variant_Part (C);
9558
9559 -- Check choices
9560
9561 Check_Choices
9562 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9563
9564 -- If the last variant does not contain the Others choice,
9565 -- replace it with an N_Others_Choice node since Gigi always
9566 -- wants an Others. Note that we do not bother to call Analyze
9567 -- on the modified variant part, since its only effect would be
9568 -- to compute the Others_Discrete_Choices node laboriously, and
9569 -- of course we already know the list of choices corresponding
9570 -- to the others choice (it's the list we're replacing!)
9571
9572 -- We only want to do this if the expander is active, since
9573 -- we do not want to clobber the ASIS tree!
9574
9575 if Expander_Active then
9576 declare
9577 Last_Var : constant Node_Id :=
9578 Last_Non_Pragma (Variants (VP));
9579
9580 Others_Node : Node_Id;
9581
9582 begin
9583 if Nkind (First (Discrete_Choices (Last_Var))) /=
9584 N_Others_Choice
9585 then
9586 Others_Node := Make_Others_Choice (Sloc (Last_Var));
9587 Set_Others_Discrete_Choices
9588 (Others_Node, Discrete_Choices (Last_Var));
9589 Set_Discrete_Choices
9590 (Last_Var, New_List (Others_Node));
9591 end if;
9592 end;
9593 end if;
9594 end if;
9595 end Check_Variant_Part;
9596 end if;
9597 end Freeze_Entity_Checks;
9598
9599 -------------------------
9600 -- Get_Alignment_Value --
9601 -------------------------
9602
9603 function Get_Alignment_Value (Expr : Node_Id) return Uint is
9604 Align : constant Uint := Static_Integer (Expr);
9605
9606 begin
9607 if Align = No_Uint then
9608 return No_Uint;
9609
9610 elsif Align <= 0 then
9611 Error_Msg_N ("alignment value must be positive", Expr);
9612 return No_Uint;
9613
9614 else
9615 for J in Int range 0 .. 64 loop
9616 declare
9617 M : constant Uint := Uint_2 ** J;
9618
9619 begin
9620 exit when M = Align;
9621
9622 if M > Align then
9623 Error_Msg_N
9624 ("alignment value must be power of 2", Expr);
9625 return No_Uint;
9626 end if;
9627 end;
9628 end loop;
9629
9630 return Align;
9631 end if;
9632 end Get_Alignment_Value;
9633
9634 -------------------------------------
9635 -- Inherit_Aspects_At_Freeze_Point --
9636 -------------------------------------
9637
9638 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
9639 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9640 (Rep_Item : Node_Id) return Boolean;
9641 -- This routine checks if Rep_Item is either a pragma or an aspect
9642 -- specification node whose correponding pragma (if any) is present in
9643 -- the Rep Item chain of the entity it has been specified to.
9644
9645 --------------------------------------------------
9646 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
9647 --------------------------------------------------
9648
9649 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9650 (Rep_Item : Node_Id) return Boolean
9651 is
9652 begin
9653 return Nkind (Rep_Item) = N_Pragma
9654 or else Present_In_Rep_Item
9655 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
9656 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
9657
9658 -- Start of processing for Inherit_Aspects_At_Freeze_Point
9659
9660 begin
9661 -- A representation item is either subtype-specific (Size and Alignment
9662 -- clauses) or type-related (all others). Subtype-specific aspects may
9663 -- differ for different subtypes of the same type (RM 13.1.8).
9664
9665 -- A derived type inherits each type-related representation aspect of
9666 -- its parent type that was directly specified before the declaration of
9667 -- the derived type (RM 13.1.15).
9668
9669 -- A derived subtype inherits each subtype-specific representation
9670 -- aspect of its parent subtype that was directly specified before the
9671 -- declaration of the derived type (RM 13.1.15).
9672
9673 -- The general processing involves inheriting a representation aspect
9674 -- from a parent type whenever the first rep item (aspect specification,
9675 -- attribute definition clause, pragma) corresponding to the given
9676 -- representation aspect in the rep item chain of Typ, if any, isn't
9677 -- directly specified to Typ but to one of its parents.
9678
9679 -- ??? Note that, for now, just a limited number of representation
9680 -- aspects have been inherited here so far. Many of them are
9681 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
9682 -- a non- exhaustive list of aspects that likely also need to
9683 -- be moved to this routine: Alignment, Component_Alignment,
9684 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
9685 -- Preelaborable_Initialization, RM_Size and Small.
9686
9687 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
9688 return;
9689 end if;
9690
9691 -- Ada_05/Ada_2005
9692
9693 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
9694 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
9695 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9696 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
9697 then
9698 Set_Is_Ada_2005_Only (Typ);
9699 end if;
9700
9701 -- Ada_12/Ada_2012
9702
9703 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
9704 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
9705 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9706 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
9707 then
9708 Set_Is_Ada_2012_Only (Typ);
9709 end if;
9710
9711 -- Atomic/Shared
9712
9713 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
9714 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
9715 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9716 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
9717 then
9718 Set_Is_Atomic (Typ);
9719 Set_Treat_As_Volatile (Typ);
9720 Set_Is_Volatile (Typ);
9721 end if;
9722
9723 -- Default_Component_Value
9724
9725 if Is_Array_Type (Typ)
9726 and then Is_Base_Type (Typ)
9727 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
9728 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
9729 then
9730 Set_Default_Aspect_Component_Value (Typ,
9731 Default_Aspect_Component_Value
9732 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
9733 end if;
9734
9735 -- Default_Value
9736
9737 if Is_Scalar_Type (Typ)
9738 and then Is_Base_Type (Typ)
9739 and then Has_Rep_Item (Typ, Name_Default_Value, False)
9740 and then Has_Rep_Item (Typ, Name_Default_Value)
9741 then
9742 Set_Default_Aspect_Value (Typ,
9743 Default_Aspect_Value
9744 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
9745 end if;
9746
9747 -- Discard_Names
9748
9749 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
9750 and then Has_Rep_Item (Typ, Name_Discard_Names)
9751 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9752 (Get_Rep_Item (Typ, Name_Discard_Names))
9753 then
9754 Set_Discard_Names (Typ);
9755 end if;
9756
9757 -- Invariants
9758
9759 if not Has_Rep_Item (Typ, Name_Invariant, False)
9760 and then Has_Rep_Item (Typ, Name_Invariant)
9761 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9762 (Get_Rep_Item (Typ, Name_Invariant))
9763 then
9764 Set_Has_Invariants (Typ);
9765
9766 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
9767 Set_Has_Inheritable_Invariants (Typ);
9768 end if;
9769 end if;
9770
9771 -- Volatile
9772
9773 if not Has_Rep_Item (Typ, Name_Volatile, False)
9774 and then Has_Rep_Item (Typ, Name_Volatile)
9775 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9776 (Get_Rep_Item (Typ, Name_Volatile))
9777 then
9778 Set_Treat_As_Volatile (Typ);
9779 Set_Is_Volatile (Typ);
9780 end if;
9781
9782 -- Inheritance for derived types only
9783
9784 if Is_Derived_Type (Typ) then
9785 declare
9786 Bas_Typ : constant Entity_Id := Base_Type (Typ);
9787 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
9788
9789 begin
9790 -- Atomic_Components
9791
9792 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
9793 and then Has_Rep_Item (Typ, Name_Atomic_Components)
9794 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9795 (Get_Rep_Item (Typ, Name_Atomic_Components))
9796 then
9797 Set_Has_Atomic_Components (Imp_Bas_Typ);
9798 end if;
9799
9800 -- Volatile_Components
9801
9802 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
9803 and then Has_Rep_Item (Typ, Name_Volatile_Components)
9804 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9805 (Get_Rep_Item (Typ, Name_Volatile_Components))
9806 then
9807 Set_Has_Volatile_Components (Imp_Bas_Typ);
9808 end if;
9809
9810 -- Finalize_Storage_Only.
9811
9812 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
9813 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
9814 then
9815 Set_Finalize_Storage_Only (Bas_Typ);
9816 end if;
9817
9818 -- Universal_Aliasing
9819
9820 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
9821 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
9822 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9823 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
9824 then
9825 Set_Universal_Aliasing (Imp_Bas_Typ);
9826 end if;
9827
9828 -- Record type specific aspects
9829
9830 if Is_Record_Type (Typ) then
9831
9832 -- Bit_Order
9833
9834 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
9835 and then Has_Rep_Item (Typ, Name_Bit_Order)
9836 then
9837 Set_Reverse_Bit_Order (Bas_Typ,
9838 Reverse_Bit_Order (Entity (Name
9839 (Get_Rep_Item (Typ, Name_Bit_Order)))));
9840 end if;
9841
9842 -- Scalar_Storage_Order
9843
9844 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
9845 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
9846 then
9847 Set_Reverse_Storage_Order (Bas_Typ,
9848 Reverse_Storage_Order (Entity (Name
9849 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
9850 end if;
9851 end if;
9852 end;
9853 end if;
9854 end Inherit_Aspects_At_Freeze_Point;
9855
9856 ----------------
9857 -- Initialize --
9858 ----------------
9859
9860 procedure Initialize is
9861 begin
9862 Address_Clause_Checks.Init;
9863 Independence_Checks.Init;
9864 Unchecked_Conversions.Init;
9865 end Initialize;
9866
9867 -------------------------
9868 -- Is_Operational_Item --
9869 -------------------------
9870
9871 function Is_Operational_Item (N : Node_Id) return Boolean is
9872 begin
9873 if Nkind (N) /= N_Attribute_Definition_Clause then
9874 return False;
9875
9876 else
9877 declare
9878 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
9879 begin
9880 return Id = Attribute_Input
9881 or else Id = Attribute_Output
9882 or else Id = Attribute_Read
9883 or else Id = Attribute_Write
9884 or else Id = Attribute_External_Tag;
9885 end;
9886 end if;
9887 end Is_Operational_Item;
9888
9889 ------------------
9890 -- Minimum_Size --
9891 ------------------
9892
9893 function Minimum_Size
9894 (T : Entity_Id;
9895 Biased : Boolean := False) return Nat
9896 is
9897 Lo : Uint := No_Uint;
9898 Hi : Uint := No_Uint;
9899 LoR : Ureal := No_Ureal;
9900 HiR : Ureal := No_Ureal;
9901 LoSet : Boolean := False;
9902 HiSet : Boolean := False;
9903 B : Uint;
9904 S : Nat;
9905 Ancest : Entity_Id;
9906 R_Typ : constant Entity_Id := Root_Type (T);
9907
9908 begin
9909 -- If bad type, return 0
9910
9911 if T = Any_Type then
9912 return 0;
9913
9914 -- For generic types, just return zero. There cannot be any legitimate
9915 -- need to know such a size, but this routine may be called with a
9916 -- generic type as part of normal processing.
9917
9918 elsif Is_Generic_Type (R_Typ)
9919 or else R_Typ = Any_Type
9920 then
9921 return 0;
9922
9923 -- Access types. Normally an access type cannot have a size smaller
9924 -- than the size of System.Address. The exception is on VMS, where
9925 -- we have short and long addresses, and it is possible for an access
9926 -- type to have a short address size (and thus be less than the size
9927 -- of System.Address itself). We simply skip the check for VMS, and
9928 -- leave it to the back end to do the check.
9929
9930 elsif Is_Access_Type (T) then
9931 if OpenVMS_On_Target then
9932 return 0;
9933 else
9934 return System_Address_Size;
9935 end if;
9936
9937 -- Floating-point types
9938
9939 elsif Is_Floating_Point_Type (T) then
9940 return UI_To_Int (Esize (R_Typ));
9941
9942 -- Discrete types
9943
9944 elsif Is_Discrete_Type (T) then
9945
9946 -- The following loop is looking for the nearest compile time known
9947 -- bounds following the ancestor subtype chain. The idea is to find
9948 -- the most restrictive known bounds information.
9949
9950 Ancest := T;
9951 loop
9952 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9953 return 0;
9954 end if;
9955
9956 if not LoSet then
9957 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
9958 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
9959 LoSet := True;
9960 exit when HiSet;
9961 end if;
9962 end if;
9963
9964 if not HiSet then
9965 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
9966 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
9967 HiSet := True;
9968 exit when LoSet;
9969 end if;
9970 end if;
9971
9972 Ancest := Ancestor_Subtype (Ancest);
9973
9974 if No (Ancest) then
9975 Ancest := Base_Type (T);
9976
9977 if Is_Generic_Type (Ancest) then
9978 return 0;
9979 end if;
9980 end if;
9981 end loop;
9982
9983 -- Fixed-point types. We can't simply use Expr_Value to get the
9984 -- Corresponding_Integer_Value values of the bounds, since these do not
9985 -- get set till the type is frozen, and this routine can be called
9986 -- before the type is frozen. Similarly the test for bounds being static
9987 -- needs to include the case where we have unanalyzed real literals for
9988 -- the same reason.
9989
9990 elsif Is_Fixed_Point_Type (T) then
9991
9992 -- The following loop is looking for the nearest compile time known
9993 -- bounds following the ancestor subtype chain. The idea is to find
9994 -- the most restrictive known bounds information.
9995
9996 Ancest := T;
9997 loop
9998 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9999 return 0;
10000 end if;
10001
10002 -- Note: In the following two tests for LoSet and HiSet, it may
10003 -- seem redundant to test for N_Real_Literal here since normally
10004 -- one would assume that the test for the value being known at
10005 -- compile time includes this case. However, there is a glitch.
10006 -- If the real literal comes from folding a non-static expression,
10007 -- then we don't consider any non- static expression to be known
10008 -- at compile time if we are in configurable run time mode (needed
10009 -- in some cases to give a clearer definition of what is and what
10010 -- is not accepted). So the test is indeed needed. Without it, we
10011 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
10012
10013 if not LoSet then
10014 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
10015 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
10016 then
10017 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
10018 LoSet := True;
10019 exit when HiSet;
10020 end if;
10021 end if;
10022
10023 if not HiSet then
10024 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
10025 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
10026 then
10027 HiR := Expr_Value_R (Type_High_Bound (Ancest));
10028 HiSet := True;
10029 exit when LoSet;
10030 end if;
10031 end if;
10032
10033 Ancest := Ancestor_Subtype (Ancest);
10034
10035 if No (Ancest) then
10036 Ancest := Base_Type (T);
10037
10038 if Is_Generic_Type (Ancest) then
10039 return 0;
10040 end if;
10041 end if;
10042 end loop;
10043
10044 Lo := UR_To_Uint (LoR / Small_Value (T));
10045 Hi := UR_To_Uint (HiR / Small_Value (T));
10046
10047 -- No other types allowed
10048
10049 else
10050 raise Program_Error;
10051 end if;
10052
10053 -- Fall through with Hi and Lo set. Deal with biased case
10054
10055 if (Biased
10056 and then not Is_Fixed_Point_Type (T)
10057 and then not (Is_Enumeration_Type (T)
10058 and then Has_Non_Standard_Rep (T)))
10059 or else Has_Biased_Representation (T)
10060 then
10061 Hi := Hi - Lo;
10062 Lo := Uint_0;
10063 end if;
10064
10065 -- Signed case. Note that we consider types like range 1 .. -1 to be
10066 -- signed for the purpose of computing the size, since the bounds have
10067 -- to be accommodated in the base type.
10068
10069 if Lo < 0 or else Hi < 0 then
10070 S := 1;
10071 B := Uint_1;
10072
10073 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
10074 -- Note that we accommodate the case where the bounds cross. This
10075 -- can happen either because of the way the bounds are declared
10076 -- or because of the algorithm in Freeze_Fixed_Point_Type.
10077
10078 while Lo < -B
10079 or else Hi < -B
10080 or else Lo >= B
10081 or else Hi >= B
10082 loop
10083 B := Uint_2 ** S;
10084 S := S + 1;
10085 end loop;
10086
10087 -- Unsigned case
10088
10089 else
10090 -- If both bounds are positive, make sure that both are represen-
10091 -- table in the case where the bounds are crossed. This can happen
10092 -- either because of the way the bounds are declared, or because of
10093 -- the algorithm in Freeze_Fixed_Point_Type.
10094
10095 if Lo > Hi then
10096 Hi := Lo;
10097 end if;
10098
10099 -- S = size, (can accommodate 0 .. (2**size - 1))
10100
10101 S := 0;
10102 while Hi >= Uint_2 ** S loop
10103 S := S + 1;
10104 end loop;
10105 end if;
10106
10107 return S;
10108 end Minimum_Size;
10109
10110 ---------------------------
10111 -- New_Stream_Subprogram --
10112 ---------------------------
10113
10114 procedure New_Stream_Subprogram
10115 (N : Node_Id;
10116 Ent : Entity_Id;
10117 Subp : Entity_Id;
10118 Nam : TSS_Name_Type)
10119 is
10120 Loc : constant Source_Ptr := Sloc (N);
10121 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
10122 Subp_Id : Entity_Id;
10123 Subp_Decl : Node_Id;
10124 F : Entity_Id;
10125 Etyp : Entity_Id;
10126
10127 Defer_Declaration : constant Boolean :=
10128 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
10129 -- For a tagged type, there is a declaration for each stream attribute
10130 -- at the freeze point, and we must generate only a completion of this
10131 -- declaration. We do the same for private types, because the full view
10132 -- might be tagged. Otherwise we generate a declaration at the point of
10133 -- the attribute definition clause.
10134
10135 function Build_Spec return Node_Id;
10136 -- Used for declaration and renaming declaration, so that this is
10137 -- treated as a renaming_as_body.
10138
10139 ----------------
10140 -- Build_Spec --
10141 ----------------
10142
10143 function Build_Spec return Node_Id is
10144 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
10145 Formals : List_Id;
10146 Spec : Node_Id;
10147 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
10148
10149 begin
10150 Subp_Id := Make_Defining_Identifier (Loc, Sname);
10151
10152 -- S : access Root_Stream_Type'Class
10153
10154 Formals := New_List (
10155 Make_Parameter_Specification (Loc,
10156 Defining_Identifier =>
10157 Make_Defining_Identifier (Loc, Name_S),
10158 Parameter_Type =>
10159 Make_Access_Definition (Loc,
10160 Subtype_Mark =>
10161 New_Reference_To (
10162 Designated_Type (Etype (F)), Loc))));
10163
10164 if Nam = TSS_Stream_Input then
10165 Spec :=
10166 Make_Function_Specification (Loc,
10167 Defining_Unit_Name => Subp_Id,
10168 Parameter_Specifications => Formals,
10169 Result_Definition => T_Ref);
10170 else
10171 -- V : [out] T
10172
10173 Append_To (Formals,
10174 Make_Parameter_Specification (Loc,
10175 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
10176 Out_Present => Out_P,
10177 Parameter_Type => T_Ref));
10178
10179 Spec :=
10180 Make_Procedure_Specification (Loc,
10181 Defining_Unit_Name => Subp_Id,
10182 Parameter_Specifications => Formals);
10183 end if;
10184
10185 return Spec;
10186 end Build_Spec;
10187
10188 -- Start of processing for New_Stream_Subprogram
10189
10190 begin
10191 F := First_Formal (Subp);
10192
10193 if Ekind (Subp) = E_Procedure then
10194 Etyp := Etype (Next_Formal (F));
10195 else
10196 Etyp := Etype (Subp);
10197 end if;
10198
10199 -- Prepare subprogram declaration and insert it as an action on the
10200 -- clause node. The visibility for this entity is used to test for
10201 -- visibility of the attribute definition clause (in the sense of
10202 -- 8.3(23) as amended by AI-195).
10203
10204 if not Defer_Declaration then
10205 Subp_Decl :=
10206 Make_Subprogram_Declaration (Loc,
10207 Specification => Build_Spec);
10208
10209 -- For a tagged type, there is always a visible declaration for each
10210 -- stream TSS (it is a predefined primitive operation), and the
10211 -- completion of this declaration occurs at the freeze point, which is
10212 -- not always visible at places where the attribute definition clause is
10213 -- visible. So, we create a dummy entity here for the purpose of
10214 -- tracking the visibility of the attribute definition clause itself.
10215
10216 else
10217 Subp_Id :=
10218 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
10219 Subp_Decl :=
10220 Make_Object_Declaration (Loc,
10221 Defining_Identifier => Subp_Id,
10222 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
10223 end if;
10224
10225 Insert_Action (N, Subp_Decl);
10226 Set_Entity (N, Subp_Id);
10227
10228 Subp_Decl :=
10229 Make_Subprogram_Renaming_Declaration (Loc,
10230 Specification => Build_Spec,
10231 Name => New_Reference_To (Subp, Loc));
10232
10233 if Defer_Declaration then
10234 Set_TSS (Base_Type (Ent), Subp_Id);
10235 else
10236 Insert_Action (N, Subp_Decl);
10237 Copy_TSS (Subp_Id, Base_Type (Ent));
10238 end if;
10239 end New_Stream_Subprogram;
10240
10241 ------------------------
10242 -- Rep_Item_Too_Early --
10243 ------------------------
10244
10245 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
10246 begin
10247 -- Cannot apply non-operational rep items to generic types
10248
10249 if Is_Operational_Item (N) then
10250 return False;
10251
10252 elsif Is_Type (T)
10253 and then Is_Generic_Type (Root_Type (T))
10254 then
10255 Error_Msg_N ("representation item not allowed for generic type", N);
10256 return True;
10257 end if;
10258
10259 -- Otherwise check for incomplete type
10260
10261 if Is_Incomplete_Or_Private_Type (T)
10262 and then No (Underlying_Type (T))
10263 and then
10264 (Nkind (N) /= N_Pragma
10265 or else Get_Pragma_Id (N) /= Pragma_Import)
10266 then
10267 Error_Msg_N
10268 ("representation item must be after full type declaration", N);
10269 return True;
10270
10271 -- If the type has incomplete components, a representation clause is
10272 -- illegal but stream attributes and Convention pragmas are correct.
10273
10274 elsif Has_Private_Component (T) then
10275 if Nkind (N) = N_Pragma then
10276 return False;
10277
10278 else
10279 Error_Msg_N
10280 ("representation item must appear after type is fully defined",
10281 N);
10282 return True;
10283 end if;
10284 else
10285 return False;
10286 end if;
10287 end Rep_Item_Too_Early;
10288
10289 -----------------------
10290 -- Rep_Item_Too_Late --
10291 -----------------------
10292
10293 function Rep_Item_Too_Late
10294 (T : Entity_Id;
10295 N : Node_Id;
10296 FOnly : Boolean := False) return Boolean
10297 is
10298 S : Entity_Id;
10299 Parent_Type : Entity_Id;
10300
10301 procedure Too_Late;
10302 -- Output the too late message. Note that this is not considered a
10303 -- serious error, since the effect is simply that we ignore the
10304 -- representation clause in this case.
10305
10306 --------------
10307 -- Too_Late --
10308 --------------
10309
10310 procedure Too_Late is
10311 begin
10312 -- Other compilers seem more relaxed about rep items appearing too
10313 -- late. Since analysis tools typically don't care about rep items
10314 -- anyway, no reason to be too strict about this.
10315
10316 if not Relaxed_RM_Semantics then
10317 Error_Msg_N ("|representation item appears too late!", N);
10318 end if;
10319 end Too_Late;
10320
10321 -- Start of processing for Rep_Item_Too_Late
10322
10323 begin
10324 -- First make sure entity is not frozen (RM 13.1(9))
10325
10326 if Is_Frozen (T)
10327
10328 -- Exclude imported types, which may be frozen if they appear in a
10329 -- representation clause for a local type.
10330
10331 and then not From_Limited_With (T)
10332
10333 -- Exclude generated entities (not coming from source). The common
10334 -- case is when we generate a renaming which prematurely freezes the
10335 -- renamed internal entity, but we still want to be able to set copies
10336 -- of attribute values such as Size/Alignment.
10337
10338 and then Comes_From_Source (T)
10339 then
10340 Too_Late;
10341 S := First_Subtype (T);
10342
10343 if Present (Freeze_Node (S)) then
10344 Error_Msg_NE
10345 ("??no more representation items for }", Freeze_Node (S), S);
10346 end if;
10347
10348 return True;
10349
10350 -- Check for case of non-tagged derived type whose parent either has
10351 -- primitive operations, or is a by reference type (RM 13.1(10)).
10352
10353 elsif Is_Type (T)
10354 and then not FOnly
10355 and then Is_Derived_Type (T)
10356 and then not Is_Tagged_Type (T)
10357 then
10358 Parent_Type := Etype (Base_Type (T));
10359
10360 if Has_Primitive_Operations (Parent_Type) then
10361 Too_Late;
10362 Error_Msg_NE
10363 ("primitive operations already defined for&!", N, Parent_Type);
10364 return True;
10365
10366 elsif Is_By_Reference_Type (Parent_Type) then
10367 Too_Late;
10368 Error_Msg_NE
10369 ("parent type & is a by reference type!", N, Parent_Type);
10370 return True;
10371 end if;
10372 end if;
10373
10374 -- No error, link item into head of chain of rep items for the entity,
10375 -- but avoid chaining if we have an overloadable entity, and the pragma
10376 -- is one that can apply to multiple overloaded entities.
10377
10378 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
10379 declare
10380 Pname : constant Name_Id := Pragma_Name (N);
10381 begin
10382 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
10383 Name_External, Name_Interface)
10384 then
10385 return False;
10386 end if;
10387 end;
10388 end if;
10389
10390 Record_Rep_Item (T, N);
10391 return False;
10392 end Rep_Item_Too_Late;
10393
10394 -------------------------------------
10395 -- Replace_Type_References_Generic --
10396 -------------------------------------
10397
10398 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
10399
10400 function Replace_Node (N : Node_Id) return Traverse_Result;
10401 -- Processes a single node in the traversal procedure below, checking
10402 -- if node N should be replaced, and if so, doing the replacement.
10403
10404 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
10405 -- This instantiation provides the body of Replace_Type_References
10406
10407 ------------------
10408 -- Replace_Node --
10409 ------------------
10410
10411 function Replace_Node (N : Node_Id) return Traverse_Result is
10412 S : Entity_Id;
10413 P : Node_Id;
10414
10415 begin
10416 -- Case of identifier
10417
10418 if Nkind (N) = N_Identifier then
10419
10420 -- If not the type name, all done with this node
10421
10422 if Chars (N) /= TName then
10423 return Skip;
10424
10425 -- Otherwise do the replacement and we are done with this node
10426
10427 else
10428 Replace_Type_Reference (N);
10429 return Skip;
10430 end if;
10431
10432 -- Case of selected component (which is what a qualification
10433 -- looks like in the unanalyzed tree, which is what we have.
10434
10435 elsif Nkind (N) = N_Selected_Component then
10436
10437 -- If selector name is not our type, keeping going (we might
10438 -- still have an occurrence of the type in the prefix).
10439
10440 if Nkind (Selector_Name (N)) /= N_Identifier
10441 or else Chars (Selector_Name (N)) /= TName
10442 then
10443 return OK;
10444
10445 -- Selector name is our type, check qualification
10446
10447 else
10448 -- Loop through scopes and prefixes, doing comparison
10449
10450 S := Current_Scope;
10451 P := Prefix (N);
10452 loop
10453 -- Continue if no more scopes or scope with no name
10454
10455 if No (S) or else Nkind (S) not in N_Has_Chars then
10456 return OK;
10457 end if;
10458
10459 -- Do replace if prefix is an identifier matching the
10460 -- scope that we are currently looking at.
10461
10462 if Nkind (P) = N_Identifier
10463 and then Chars (P) = Chars (S)
10464 then
10465 Replace_Type_Reference (N);
10466 return Skip;
10467 end if;
10468
10469 -- Go check scope above us if prefix is itself of the
10470 -- form of a selected component, whose selector matches
10471 -- the scope we are currently looking at.
10472
10473 if Nkind (P) = N_Selected_Component
10474 and then Nkind (Selector_Name (P)) = N_Identifier
10475 and then Chars (Selector_Name (P)) = Chars (S)
10476 then
10477 S := Scope (S);
10478 P := Prefix (P);
10479
10480 -- For anything else, we don't have a match, so keep on
10481 -- going, there are still some weird cases where we may
10482 -- still have a replacement within the prefix.
10483
10484 else
10485 return OK;
10486 end if;
10487 end loop;
10488 end if;
10489
10490 -- Continue for any other node kind
10491
10492 else
10493 return OK;
10494 end if;
10495 end Replace_Node;
10496
10497 begin
10498 Replace_Type_Refs (N);
10499 end Replace_Type_References_Generic;
10500
10501 -------------------------
10502 -- Same_Representation --
10503 -------------------------
10504
10505 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
10506 T1 : constant Entity_Id := Underlying_Type (Typ1);
10507 T2 : constant Entity_Id := Underlying_Type (Typ2);
10508
10509 begin
10510 -- A quick check, if base types are the same, then we definitely have
10511 -- the same representation, because the subtype specific representation
10512 -- attributes (Size and Alignment) do not affect representation from
10513 -- the point of view of this test.
10514
10515 if Base_Type (T1) = Base_Type (T2) then
10516 return True;
10517
10518 elsif Is_Private_Type (Base_Type (T2))
10519 and then Base_Type (T1) = Full_View (Base_Type (T2))
10520 then
10521 return True;
10522 end if;
10523
10524 -- Tagged types never have differing representations
10525
10526 if Is_Tagged_Type (T1) then
10527 return True;
10528 end if;
10529
10530 -- Representations are definitely different if conventions differ
10531
10532 if Convention (T1) /= Convention (T2) then
10533 return False;
10534 end if;
10535
10536 -- Representations are different if component alignments or scalar
10537 -- storage orders differ.
10538
10539 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
10540 and then
10541 (Is_Record_Type (T2) or else Is_Array_Type (T2))
10542 and then
10543 (Component_Alignment (T1) /= Component_Alignment (T2)
10544 or else
10545 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
10546 then
10547 return False;
10548 end if;
10549
10550 -- For arrays, the only real issue is component size. If we know the
10551 -- component size for both arrays, and it is the same, then that's
10552 -- good enough to know we don't have a change of representation.
10553
10554 if Is_Array_Type (T1) then
10555 if Known_Component_Size (T1)
10556 and then Known_Component_Size (T2)
10557 and then Component_Size (T1) = Component_Size (T2)
10558 then
10559 if VM_Target = No_VM then
10560 return True;
10561
10562 -- In VM targets the representation of arrays with aliased
10563 -- components differs from arrays with non-aliased components
10564
10565 else
10566 return Has_Aliased_Components (Base_Type (T1))
10567 =
10568 Has_Aliased_Components (Base_Type (T2));
10569 end if;
10570 end if;
10571 end if;
10572
10573 -- Types definitely have same representation if neither has non-standard
10574 -- representation since default representations are always consistent.
10575 -- If only one has non-standard representation, and the other does not,
10576 -- then we consider that they do not have the same representation. They
10577 -- might, but there is no way of telling early enough.
10578
10579 if Has_Non_Standard_Rep (T1) then
10580 if not Has_Non_Standard_Rep (T2) then
10581 return False;
10582 end if;
10583 else
10584 return not Has_Non_Standard_Rep (T2);
10585 end if;
10586
10587 -- Here the two types both have non-standard representation, and we need
10588 -- to determine if they have the same non-standard representation.
10589
10590 -- For arrays, we simply need to test if the component sizes are the
10591 -- same. Pragma Pack is reflected in modified component sizes, so this
10592 -- check also deals with pragma Pack.
10593
10594 if Is_Array_Type (T1) then
10595 return Component_Size (T1) = Component_Size (T2);
10596
10597 -- Tagged types always have the same representation, because it is not
10598 -- possible to specify different representations for common fields.
10599
10600 elsif Is_Tagged_Type (T1) then
10601 return True;
10602
10603 -- Case of record types
10604
10605 elsif Is_Record_Type (T1) then
10606
10607 -- Packed status must conform
10608
10609 if Is_Packed (T1) /= Is_Packed (T2) then
10610 return False;
10611
10612 -- Otherwise we must check components. Typ2 maybe a constrained
10613 -- subtype with fewer components, so we compare the components
10614 -- of the base types.
10615
10616 else
10617 Record_Case : declare
10618 CD1, CD2 : Entity_Id;
10619
10620 function Same_Rep return Boolean;
10621 -- CD1 and CD2 are either components or discriminants. This
10622 -- function tests whether they have the same representation.
10623
10624 --------------
10625 -- Same_Rep --
10626 --------------
10627
10628 function Same_Rep return Boolean is
10629 begin
10630 if No (Component_Clause (CD1)) then
10631 return No (Component_Clause (CD2));
10632 else
10633 -- Note: at this point, component clauses have been
10634 -- normalized to the default bit order, so that the
10635 -- comparison of Component_Bit_Offsets is meaningful.
10636
10637 return
10638 Present (Component_Clause (CD2))
10639 and then
10640 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
10641 and then
10642 Esize (CD1) = Esize (CD2);
10643 end if;
10644 end Same_Rep;
10645
10646 -- Start of processing for Record_Case
10647
10648 begin
10649 if Has_Discriminants (T1) then
10650
10651 -- The number of discriminants may be different if the
10652 -- derived type has fewer (constrained by values). The
10653 -- invisible discriminants retain the representation of
10654 -- the original, so the discrepancy does not per se
10655 -- indicate a different representation.
10656
10657 CD1 := First_Discriminant (T1);
10658 CD2 := First_Discriminant (T2);
10659 while Present (CD1) and then Present (CD2) loop
10660 if not Same_Rep then
10661 return False;
10662 else
10663 Next_Discriminant (CD1);
10664 Next_Discriminant (CD2);
10665 end if;
10666 end loop;
10667 end if;
10668
10669 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
10670 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
10671 while Present (CD1) loop
10672 if not Same_Rep then
10673 return False;
10674 else
10675 Next_Component (CD1);
10676 Next_Component (CD2);
10677 end if;
10678 end loop;
10679
10680 return True;
10681 end Record_Case;
10682 end if;
10683
10684 -- For enumeration types, we must check each literal to see if the
10685 -- representation is the same. Note that we do not permit enumeration
10686 -- representation clauses for Character and Wide_Character, so these
10687 -- cases were already dealt with.
10688
10689 elsif Is_Enumeration_Type (T1) then
10690 Enumeration_Case : declare
10691 L1, L2 : Entity_Id;
10692
10693 begin
10694 L1 := First_Literal (T1);
10695 L2 := First_Literal (T2);
10696 while Present (L1) loop
10697 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
10698 return False;
10699 else
10700 Next_Literal (L1);
10701 Next_Literal (L2);
10702 end if;
10703 end loop;
10704
10705 return True;
10706 end Enumeration_Case;
10707
10708 -- Any other types have the same representation for these purposes
10709
10710 else
10711 return True;
10712 end if;
10713 end Same_Representation;
10714
10715 ----------------
10716 -- Set_Biased --
10717 ----------------
10718
10719 procedure Set_Biased
10720 (E : Entity_Id;
10721 N : Node_Id;
10722 Msg : String;
10723 Biased : Boolean := True)
10724 is
10725 begin
10726 if Biased then
10727 Set_Has_Biased_Representation (E);
10728
10729 if Warn_On_Biased_Representation then
10730 Error_Msg_NE
10731 ("?B?" & Msg & " forces biased representation for&", N, E);
10732 end if;
10733 end if;
10734 end Set_Biased;
10735
10736 --------------------
10737 -- Set_Enum_Esize --
10738 --------------------
10739
10740 procedure Set_Enum_Esize (T : Entity_Id) is
10741 Lo : Uint;
10742 Hi : Uint;
10743 Sz : Nat;
10744
10745 begin
10746 Init_Alignment (T);
10747
10748 -- Find the minimum standard size (8,16,32,64) that fits
10749
10750 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
10751 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
10752
10753 if Lo < 0 then
10754 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
10755 Sz := Standard_Character_Size; -- May be > 8 on some targets
10756
10757 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
10758 Sz := 16;
10759
10760 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
10761 Sz := 32;
10762
10763 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
10764 Sz := 64;
10765 end if;
10766
10767 else
10768 if Hi < Uint_2**08 then
10769 Sz := Standard_Character_Size; -- May be > 8 on some targets
10770
10771 elsif Hi < Uint_2**16 then
10772 Sz := 16;
10773
10774 elsif Hi < Uint_2**32 then
10775 Sz := 32;
10776
10777 else pragma Assert (Hi < Uint_2**63);
10778 Sz := 64;
10779 end if;
10780 end if;
10781
10782 -- That minimum is the proper size unless we have a foreign convention
10783 -- and the size required is 32 or less, in which case we bump the size
10784 -- up to 32. This is required for C and C++ and seems reasonable for
10785 -- all other foreign conventions.
10786
10787 if Has_Foreign_Convention (T)
10788 and then Esize (T) < Standard_Integer_Size
10789 then
10790 Init_Esize (T, Standard_Integer_Size);
10791 else
10792 Init_Esize (T, Sz);
10793 end if;
10794 end Set_Enum_Esize;
10795
10796 ------------------------------
10797 -- Validate_Address_Clauses --
10798 ------------------------------
10799
10800 procedure Validate_Address_Clauses is
10801 begin
10802 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
10803 declare
10804 ACCR : Address_Clause_Check_Record
10805 renames Address_Clause_Checks.Table (J);
10806
10807 Expr : Node_Id;
10808
10809 X_Alignment : Uint;
10810 Y_Alignment : Uint;
10811
10812 X_Size : Uint;
10813 Y_Size : Uint;
10814
10815 begin
10816 -- Skip processing of this entry if warning already posted
10817
10818 if not Address_Warning_Posted (ACCR.N) then
10819 Expr := Original_Node (Expression (ACCR.N));
10820
10821 -- Get alignments
10822
10823 X_Alignment := Alignment (ACCR.X);
10824 Y_Alignment := Alignment (ACCR.Y);
10825
10826 -- Similarly obtain sizes
10827
10828 X_Size := Esize (ACCR.X);
10829 Y_Size := Esize (ACCR.Y);
10830
10831 -- Check for large object overlaying smaller one
10832
10833 if Y_Size > Uint_0
10834 and then X_Size > Uint_0
10835 and then X_Size > Y_Size
10836 then
10837 Error_Msg_NE
10838 ("?& overlays smaller object", ACCR.N, ACCR.X);
10839 Error_Msg_N
10840 ("\??program execution may be erroneous", ACCR.N);
10841 Error_Msg_Uint_1 := X_Size;
10842 Error_Msg_NE
10843 ("\??size of & is ^", ACCR.N, ACCR.X);
10844 Error_Msg_Uint_1 := Y_Size;
10845 Error_Msg_NE
10846 ("\??size of & is ^", ACCR.N, ACCR.Y);
10847
10848 -- Check for inadequate alignment, both of the base object
10849 -- and of the offset, if any.
10850
10851 -- Note: we do not check the alignment if we gave a size
10852 -- warning, since it would likely be redundant.
10853
10854 elsif Y_Alignment /= Uint_0
10855 and then (Y_Alignment < X_Alignment
10856 or else (ACCR.Off
10857 and then
10858 Nkind (Expr) = N_Attribute_Reference
10859 and then
10860 Attribute_Name (Expr) = Name_Address
10861 and then
10862 Has_Compatible_Alignment
10863 (ACCR.X, Prefix (Expr))
10864 /= Known_Compatible))
10865 then
10866 Error_Msg_NE
10867 ("??specified address for& may be inconsistent "
10868 & "with alignment", ACCR.N, ACCR.X);
10869 Error_Msg_N
10870 ("\??program execution may be erroneous (RM 13.3(27))",
10871 ACCR.N);
10872 Error_Msg_Uint_1 := X_Alignment;
10873 Error_Msg_NE
10874 ("\??alignment of & is ^", ACCR.N, ACCR.X);
10875 Error_Msg_Uint_1 := Y_Alignment;
10876 Error_Msg_NE
10877 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
10878 if Y_Alignment >= X_Alignment then
10879 Error_Msg_N
10880 ("\??but offset is not multiple of alignment", ACCR.N);
10881 end if;
10882 end if;
10883 end if;
10884 end;
10885 end loop;
10886 end Validate_Address_Clauses;
10887
10888 ---------------------------
10889 -- Validate_Independence --
10890 ---------------------------
10891
10892 procedure Validate_Independence is
10893 SU : constant Uint := UI_From_Int (System_Storage_Unit);
10894 N : Node_Id;
10895 E : Entity_Id;
10896 IC : Boolean;
10897 Comp : Entity_Id;
10898 Addr : Node_Id;
10899 P : Node_Id;
10900
10901 procedure Check_Array_Type (Atyp : Entity_Id);
10902 -- Checks if the array type Atyp has independent components, and
10903 -- if not, outputs an appropriate set of error messages.
10904
10905 procedure No_Independence;
10906 -- Output message that independence cannot be guaranteed
10907
10908 function OK_Component (C : Entity_Id) return Boolean;
10909 -- Checks one component to see if it is independently accessible, and
10910 -- if so yields True, otherwise yields False if independent access
10911 -- cannot be guaranteed. This is a conservative routine, it only
10912 -- returns True if it knows for sure, it returns False if it knows
10913 -- there is a problem, or it cannot be sure there is no problem.
10914
10915 procedure Reason_Bad_Component (C : Entity_Id);
10916 -- Outputs continuation message if a reason can be determined for
10917 -- the component C being bad.
10918
10919 ----------------------
10920 -- Check_Array_Type --
10921 ----------------------
10922
10923 procedure Check_Array_Type (Atyp : Entity_Id) is
10924 Ctyp : constant Entity_Id := Component_Type (Atyp);
10925
10926 begin
10927 -- OK if no alignment clause, no pack, and no component size
10928
10929 if not Has_Component_Size_Clause (Atyp)
10930 and then not Has_Alignment_Clause (Atyp)
10931 and then not Is_Packed (Atyp)
10932 then
10933 return;
10934 end if;
10935
10936 -- Check actual component size
10937
10938 if not Known_Component_Size (Atyp)
10939 or else not (Addressable (Component_Size (Atyp))
10940 and then Component_Size (Atyp) < 64)
10941 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
10942 then
10943 No_Independence;
10944
10945 -- Bad component size, check reason
10946
10947 if Has_Component_Size_Clause (Atyp) then
10948 P := Get_Attribute_Definition_Clause
10949 (Atyp, Attribute_Component_Size);
10950
10951 if Present (P) then
10952 Error_Msg_Sloc := Sloc (P);
10953 Error_Msg_N ("\because of Component_Size clause#", N);
10954 return;
10955 end if;
10956 end if;
10957
10958 if Is_Packed (Atyp) then
10959 P := Get_Rep_Pragma (Atyp, Name_Pack);
10960
10961 if Present (P) then
10962 Error_Msg_Sloc := Sloc (P);
10963 Error_Msg_N ("\because of pragma Pack#", N);
10964 return;
10965 end if;
10966 end if;
10967
10968 -- No reason found, just return
10969
10970 return;
10971 end if;
10972
10973 -- Array type is OK independence-wise
10974
10975 return;
10976 end Check_Array_Type;
10977
10978 ---------------------
10979 -- No_Independence --
10980 ---------------------
10981
10982 procedure No_Independence is
10983 begin
10984 if Pragma_Name (N) = Name_Independent then
10985 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
10986 else
10987 Error_Msg_NE
10988 ("independent components cannot be guaranteed for&", N, E);
10989 end if;
10990 end No_Independence;
10991
10992 ------------------
10993 -- OK_Component --
10994 ------------------
10995
10996 function OK_Component (C : Entity_Id) return Boolean is
10997 Rec : constant Entity_Id := Scope (C);
10998 Ctyp : constant Entity_Id := Etype (C);
10999
11000 begin
11001 -- OK if no component clause, no Pack, and no alignment clause
11002
11003 if No (Component_Clause (C))
11004 and then not Is_Packed (Rec)
11005 and then not Has_Alignment_Clause (Rec)
11006 then
11007 return True;
11008 end if;
11009
11010 -- Here we look at the actual component layout. A component is
11011 -- addressable if its size is a multiple of the Esize of the
11012 -- component type, and its starting position in the record has
11013 -- appropriate alignment, and the record itself has appropriate
11014 -- alignment to guarantee the component alignment.
11015
11016 -- Make sure sizes are static, always assume the worst for any
11017 -- cases where we cannot check static values.
11018
11019 if not (Known_Static_Esize (C)
11020 and then
11021 Known_Static_Esize (Ctyp))
11022 then
11023 return False;
11024 end if;
11025
11026 -- Size of component must be addressable or greater than 64 bits
11027 -- and a multiple of bytes.
11028
11029 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
11030 return False;
11031 end if;
11032
11033 -- Check size is proper multiple
11034
11035 if Esize (C) mod Esize (Ctyp) /= 0 then
11036 return False;
11037 end if;
11038
11039 -- Check alignment of component is OK
11040
11041 if not Known_Component_Bit_Offset (C)
11042 or else Component_Bit_Offset (C) < Uint_0
11043 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
11044 then
11045 return False;
11046 end if;
11047
11048 -- Check alignment of record type is OK
11049
11050 if not Known_Alignment (Rec)
11051 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11052 then
11053 return False;
11054 end if;
11055
11056 -- All tests passed, component is addressable
11057
11058 return True;
11059 end OK_Component;
11060
11061 --------------------------
11062 -- Reason_Bad_Component --
11063 --------------------------
11064
11065 procedure Reason_Bad_Component (C : Entity_Id) is
11066 Rec : constant Entity_Id := Scope (C);
11067 Ctyp : constant Entity_Id := Etype (C);
11068
11069 begin
11070 -- If component clause present assume that's the problem
11071
11072 if Present (Component_Clause (C)) then
11073 Error_Msg_Sloc := Sloc (Component_Clause (C));
11074 Error_Msg_N ("\because of Component_Clause#", N);
11075 return;
11076 end if;
11077
11078 -- If pragma Pack clause present, assume that's the problem
11079
11080 if Is_Packed (Rec) then
11081 P := Get_Rep_Pragma (Rec, Name_Pack);
11082
11083 if Present (P) then
11084 Error_Msg_Sloc := Sloc (P);
11085 Error_Msg_N ("\because of pragma Pack#", N);
11086 return;
11087 end if;
11088 end if;
11089
11090 -- See if record has bad alignment clause
11091
11092 if Has_Alignment_Clause (Rec)
11093 and then Known_Alignment (Rec)
11094 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11095 then
11096 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
11097
11098 if Present (P) then
11099 Error_Msg_Sloc := Sloc (P);
11100 Error_Msg_N ("\because of Alignment clause#", N);
11101 end if;
11102 end if;
11103
11104 -- Couldn't find a reason, so return without a message
11105
11106 return;
11107 end Reason_Bad_Component;
11108
11109 -- Start of processing for Validate_Independence
11110
11111 begin
11112 for J in Independence_Checks.First .. Independence_Checks.Last loop
11113 N := Independence_Checks.Table (J).N;
11114 E := Independence_Checks.Table (J).E;
11115 IC := Pragma_Name (N) = Name_Independent_Components;
11116
11117 -- Deal with component case
11118
11119 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
11120 if not OK_Component (E) then
11121 No_Independence;
11122 Reason_Bad_Component (E);
11123 goto Continue;
11124 end if;
11125 end if;
11126
11127 -- Deal with record with Independent_Components
11128
11129 if IC and then Is_Record_Type (E) then
11130 Comp := First_Component_Or_Discriminant (E);
11131 while Present (Comp) loop
11132 if not OK_Component (Comp) then
11133 No_Independence;
11134 Reason_Bad_Component (Comp);
11135 goto Continue;
11136 end if;
11137
11138 Next_Component_Or_Discriminant (Comp);
11139 end loop;
11140 end if;
11141
11142 -- Deal with address clause case
11143
11144 if Is_Object (E) then
11145 Addr := Address_Clause (E);
11146
11147 if Present (Addr) then
11148 No_Independence;
11149 Error_Msg_Sloc := Sloc (Addr);
11150 Error_Msg_N ("\because of Address clause#", N);
11151 goto Continue;
11152 end if;
11153 end if;
11154
11155 -- Deal with independent components for array type
11156
11157 if IC and then Is_Array_Type (E) then
11158 Check_Array_Type (E);
11159 end if;
11160
11161 -- Deal with independent components for array object
11162
11163 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
11164 Check_Array_Type (Etype (E));
11165 end if;
11166
11167 <<Continue>> null;
11168 end loop;
11169 end Validate_Independence;
11170
11171 -----------------------------------
11172 -- Validate_Unchecked_Conversion --
11173 -----------------------------------
11174
11175 procedure Validate_Unchecked_Conversion
11176 (N : Node_Id;
11177 Act_Unit : Entity_Id)
11178 is
11179 Source : Entity_Id;
11180 Target : Entity_Id;
11181 Vnode : Node_Id;
11182
11183 begin
11184 -- Obtain source and target types. Note that we call Ancestor_Subtype
11185 -- here because the processing for generic instantiation always makes
11186 -- subtypes, and we want the original frozen actual types.
11187
11188 -- If we are dealing with private types, then do the check on their
11189 -- fully declared counterparts if the full declarations have been
11190 -- encountered (they don't have to be visible, but they must exist!)
11191
11192 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
11193
11194 if Is_Private_Type (Source)
11195 and then Present (Underlying_Type (Source))
11196 then
11197 Source := Underlying_Type (Source);
11198 end if;
11199
11200 Target := Ancestor_Subtype (Etype (Act_Unit));
11201
11202 -- If either type is generic, the instantiation happens within a generic
11203 -- unit, and there is nothing to check. The proper check will happen
11204 -- when the enclosing generic is instantiated.
11205
11206 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
11207 return;
11208 end if;
11209
11210 if Is_Private_Type (Target)
11211 and then Present (Underlying_Type (Target))
11212 then
11213 Target := Underlying_Type (Target);
11214 end if;
11215
11216 -- Source may be unconstrained array, but not target
11217
11218 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
11219 Error_Msg_N
11220 ("unchecked conversion to unconstrained array not allowed", N);
11221 return;
11222 end if;
11223
11224 -- Warn if conversion between two different convention pointers
11225
11226 if Is_Access_Type (Target)
11227 and then Is_Access_Type (Source)
11228 and then Convention (Target) /= Convention (Source)
11229 and then Warn_On_Unchecked_Conversion
11230 then
11231 -- Give warnings for subprogram pointers only on most targets. The
11232 -- exception is VMS, where data pointers can have different lengths
11233 -- depending on the pointer convention.
11234
11235 if Is_Access_Subprogram_Type (Target)
11236 or else Is_Access_Subprogram_Type (Source)
11237 or else OpenVMS_On_Target
11238 then
11239 Error_Msg_N
11240 ("?z?conversion between pointers with different conventions!",
11241 N);
11242 end if;
11243 end if;
11244
11245 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
11246 -- warning when compiling GNAT-related sources.
11247
11248 if Warn_On_Unchecked_Conversion
11249 and then not In_Predefined_Unit (N)
11250 and then RTU_Loaded (Ada_Calendar)
11251 and then
11252 (Chars (Source) = Name_Time
11253 or else
11254 Chars (Target) = Name_Time)
11255 then
11256 -- If Ada.Calendar is loaded and the name of one of the operands is
11257 -- Time, there is a good chance that this is Ada.Calendar.Time.
11258
11259 declare
11260 Calendar_Time : constant Entity_Id :=
11261 Full_View (RTE (RO_CA_Time));
11262 begin
11263 pragma Assert (Present (Calendar_Time));
11264
11265 if Source = Calendar_Time or else Target = Calendar_Time then
11266 Error_Msg_N
11267 ("?z?representation of 'Time values may change between " &
11268 "'G'N'A'T versions", N);
11269 end if;
11270 end;
11271 end if;
11272
11273 -- Make entry in unchecked conversion table for later processing by
11274 -- Validate_Unchecked_Conversions, which will check sizes and alignments
11275 -- (using values set by the back-end where possible). This is only done
11276 -- if the appropriate warning is active.
11277
11278 if Warn_On_Unchecked_Conversion then
11279 Unchecked_Conversions.Append
11280 (New_Val => UC_Entry'(Eloc => Sloc (N),
11281 Source => Source,
11282 Target => Target));
11283
11284 -- If both sizes are known statically now, then back end annotation
11285 -- is not required to do a proper check but if either size is not
11286 -- known statically, then we need the annotation.
11287
11288 if Known_Static_RM_Size (Source)
11289 and then
11290 Known_Static_RM_Size (Target)
11291 then
11292 null;
11293 else
11294 Back_Annotate_Rep_Info := True;
11295 end if;
11296 end if;
11297
11298 -- If unchecked conversion to access type, and access type is declared
11299 -- in the same unit as the unchecked conversion, then set the flag
11300 -- No_Strict_Aliasing (no strict aliasing is implicit here)
11301
11302 if Is_Access_Type (Target) and then
11303 In_Same_Source_Unit (Target, N)
11304 then
11305 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
11306 end if;
11307
11308 -- Generate N_Validate_Unchecked_Conversion node for back end in case
11309 -- the back end needs to perform special validation checks.
11310
11311 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
11312 -- have full expansion and the back end is called ???
11313
11314 Vnode :=
11315 Make_Validate_Unchecked_Conversion (Sloc (N));
11316 Set_Source_Type (Vnode, Source);
11317 Set_Target_Type (Vnode, Target);
11318
11319 -- If the unchecked conversion node is in a list, just insert before it.
11320 -- If not we have some strange case, not worth bothering about.
11321
11322 if Is_List_Member (N) then
11323 Insert_After (N, Vnode);
11324 end if;
11325 end Validate_Unchecked_Conversion;
11326
11327 ------------------------------------
11328 -- Validate_Unchecked_Conversions --
11329 ------------------------------------
11330
11331 procedure Validate_Unchecked_Conversions is
11332 begin
11333 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
11334 declare
11335 T : UC_Entry renames Unchecked_Conversions.Table (N);
11336
11337 Eloc : constant Source_Ptr := T.Eloc;
11338 Source : constant Entity_Id := T.Source;
11339 Target : constant Entity_Id := T.Target;
11340
11341 Source_Siz : Uint;
11342 Target_Siz : Uint;
11343
11344 begin
11345 -- This validation check, which warns if we have unequal sizes for
11346 -- unchecked conversion, and thus potentially implementation
11347 -- dependent semantics, is one of the few occasions on which we
11348 -- use the official RM size instead of Esize. See description in
11349 -- Einfo "Handling of Type'Size Values" for details.
11350
11351 if Serious_Errors_Detected = 0
11352 and then Known_Static_RM_Size (Source)
11353 and then Known_Static_RM_Size (Target)
11354
11355 -- Don't do the check if warnings off for either type, note the
11356 -- deliberate use of OR here instead of OR ELSE to get the flag
11357 -- Warnings_Off_Used set for both types if appropriate.
11358
11359 and then not (Has_Warnings_Off (Source)
11360 or
11361 Has_Warnings_Off (Target))
11362 then
11363 Source_Siz := RM_Size (Source);
11364 Target_Siz := RM_Size (Target);
11365
11366 if Source_Siz /= Target_Siz then
11367 Error_Msg
11368 ("?z?types for unchecked conversion have different sizes!",
11369 Eloc);
11370
11371 if All_Errors_Mode then
11372 Error_Msg_Name_1 := Chars (Source);
11373 Error_Msg_Uint_1 := Source_Siz;
11374 Error_Msg_Name_2 := Chars (Target);
11375 Error_Msg_Uint_2 := Target_Siz;
11376 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
11377
11378 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
11379
11380 if Is_Discrete_Type (Source)
11381 and then
11382 Is_Discrete_Type (Target)
11383 then
11384 if Source_Siz > Target_Siz then
11385 Error_Msg
11386 ("\?z?^ high order bits of source will "
11387 & "be ignored!", Eloc);
11388
11389 elsif Is_Unsigned_Type (Source) then
11390 Error_Msg
11391 ("\?z?source will be extended with ^ high order "
11392 & "zero bits?!", Eloc);
11393
11394 else
11395 Error_Msg
11396 ("\?z?source will be extended with ^ high order "
11397 & "sign bits!", Eloc);
11398 end if;
11399
11400 elsif Source_Siz < Target_Siz then
11401 if Is_Discrete_Type (Target) then
11402 if Bytes_Big_Endian then
11403 Error_Msg
11404 ("\?z?target value will include ^ undefined "
11405 & "low order bits!", Eloc);
11406 else
11407 Error_Msg
11408 ("\?z?target value will include ^ undefined "
11409 & "high order bits!", Eloc);
11410 end if;
11411
11412 else
11413 Error_Msg
11414 ("\?z?^ trailing bits of target value will be "
11415 & "undefined!", Eloc);
11416 end if;
11417
11418 else pragma Assert (Source_Siz > Target_Siz);
11419 Error_Msg
11420 ("\?z?^ trailing bits of source will be ignored!",
11421 Eloc);
11422 end if;
11423 end if;
11424 end if;
11425 end if;
11426
11427 -- If both types are access types, we need to check the alignment.
11428 -- If the alignment of both is specified, we can do it here.
11429
11430 if Serious_Errors_Detected = 0
11431 and then Ekind (Source) in Access_Kind
11432 and then Ekind (Target) in Access_Kind
11433 and then Target_Strict_Alignment
11434 and then Present (Designated_Type (Source))
11435 and then Present (Designated_Type (Target))
11436 then
11437 declare
11438 D_Source : constant Entity_Id := Designated_Type (Source);
11439 D_Target : constant Entity_Id := Designated_Type (Target);
11440
11441 begin
11442 if Known_Alignment (D_Source)
11443 and then
11444 Known_Alignment (D_Target)
11445 then
11446 declare
11447 Source_Align : constant Uint := Alignment (D_Source);
11448 Target_Align : constant Uint := Alignment (D_Target);
11449
11450 begin
11451 if Source_Align < Target_Align
11452 and then not Is_Tagged_Type (D_Source)
11453
11454 -- Suppress warning if warnings suppressed on either
11455 -- type or either designated type. Note the use of
11456 -- OR here instead of OR ELSE. That is intentional,
11457 -- we would like to set flag Warnings_Off_Used in
11458 -- all types for which warnings are suppressed.
11459
11460 and then not (Has_Warnings_Off (D_Source)
11461 or
11462 Has_Warnings_Off (D_Target)
11463 or
11464 Has_Warnings_Off (Source)
11465 or
11466 Has_Warnings_Off (Target))
11467 then
11468 Error_Msg_Uint_1 := Target_Align;
11469 Error_Msg_Uint_2 := Source_Align;
11470 Error_Msg_Node_1 := D_Target;
11471 Error_Msg_Node_2 := D_Source;
11472 Error_Msg
11473 ("?z?alignment of & (^) is stricter than "
11474 & "alignment of & (^)!", Eloc);
11475 Error_Msg
11476 ("\?z?resulting access value may have invalid "
11477 & "alignment!", Eloc);
11478 end if;
11479 end;
11480 end if;
11481 end;
11482 end if;
11483 end;
11484 end loop;
11485 end Validate_Unchecked_Conversions;
11486
11487 end Sem_Ch13;