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