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