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