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