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