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