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