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