]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch13.adb
28ccf5666b5a6dd909f425eb15addd33a05579ae
[thirdparty/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-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Case; use Sem_Case;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Dim; use Sem_Dim;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Sinput; use Sinput;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Stringt; use Stringt;
66 with Targparm; use Targparm;
67 with Ttypes; use Ttypes;
68 with Tbuild; use Tbuild;
69 with Urealp; use Urealp;
70 with Warnsw; use Warnsw;
71
72 with GNAT.Heap_Sort_G;
73
74 package body Sem_Ch13 is
75
76 SSU : constant Pos := System_Storage_Unit;
77 -- Convenient short hand for commonly used constant
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
83 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
84 -- This routine is called after setting one of the sizes of type entity
85 -- Typ to Size. The purpose is to deal with the situation of a derived
86 -- type whose inherited alignment is no longer appropriate for the new
87 -- size value. In this case, we reset the Alignment to unknown.
88
89 procedure Build_Discrete_Static_Predicate
90 (Typ : Entity_Id;
91 Expr : Node_Id;
92 Nam : Name_Id);
93 -- Given a predicated type Typ, where Typ is a discrete static subtype,
94 -- whose predicate expression is Expr, tests if Expr is a static predicate,
95 -- and if so, builds the predicate range list. Nam is the name of the one
96 -- argument to the predicate function. Occurrences of the type name in the
97 -- predicate expression have been replaced by identifier references to this
98 -- name, which is unique, so any identifier with Chars matching Nam must be
99 -- a reference to the type. If the predicate is non-static, this procedure
100 -- returns doing nothing. If the predicate is static, then the predicate
101 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
102 -- rewritten as a canonicalized membership operation.
103
104 function Build_Export_Import_Pragma
105 (Asp : Node_Id;
106 Id : Entity_Id) return Node_Id;
107 -- Create the corresponding pragma for aspect Export or Import denoted by
108 -- Asp. Id is the related entity subject to the aspect. Return Empty when
109 -- the expression of aspect Asp evaluates to False or is erroneous.
110
111 function Build_Predicate_Function_Declaration
112 (Typ : Entity_Id) return Node_Id;
113 -- Build the declaration for a predicate function. The declaration is built
114 -- at the end of the declarative part containing the type definition, which
115 -- may be before the freeze point of the type. The predicate expression is
116 -- pre-analyzed at this point, to catch visibility errors.
117
118 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
119 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
120 -- then either there are pragma Predicate entries on the rep chain for the
121 -- type (note that Predicate aspects are converted to pragma Predicate), or
122 -- there are inherited aspects from a parent type, or ancestor subtypes.
123 -- This procedure builds body for the Predicate function that tests these
124 -- predicates. N is the freeze node for the type. The spec of the function
125 -- is inserted before the freeze node, and the body of the function is
126 -- inserted after the freeze node. If the predicate expression has a least
127 -- one Raise_Expression, then this procedure also builds the M version of
128 -- the predicate function for use in membership tests.
129
130 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
131 -- Called if both Storage_Pool and Storage_Size attribute definition
132 -- clauses (SP and SS) are present for entity Ent. Issue error message.
133
134 procedure Freeze_Entity_Checks (N : Node_Id);
135 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
136 -- to generate appropriate semantic checks that are delayed until this
137 -- point (they had to be delayed this long for cases of delayed aspects,
138 -- e.g. analysis of statically predicated subtypes in choices, for which
139 -- we have to be sure the subtypes in question are frozen before checking).
140
141 function Get_Alignment_Value (Expr : Node_Id) return Uint;
142 -- Given the expression for an alignment value, returns the corresponding
143 -- Uint value. If the value is inappropriate, then error messages are
144 -- posted as required, and a value of No_Uint is returned.
145
146 procedure Get_Interfacing_Aspects
147 (Iface_Asp : Node_Id;
148 Conv_Asp : out Node_Id;
149 EN_Asp : out Node_Id;
150 Expo_Asp : out Node_Id;
151 Imp_Asp : out Node_Id;
152 LN_Asp : out Node_Id;
153 Do_Checks : Boolean := False);
154 -- Given a single interfacing aspect Iface_Asp, retrieve other interfacing
155 -- aspects that apply to the same related entity. The aspects considered by
156 -- this routine are as follows:
157 --
158 -- Conv_Asp - aspect Convention
159 -- EN_Asp - aspect External_Name
160 -- Expo_Asp - aspect Export
161 -- Imp_Asp - aspect Import
162 -- LN_Asp - aspect Link_Name
163 --
164 -- When flag Do_Checks is set, this routine will flag duplicate uses of
165 -- aspects.
166
167 function Is_Operational_Item (N : Node_Id) return Boolean;
168 -- A specification for a stream attribute is allowed before the full type
169 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
170 -- that do not specify a representation characteristic are operational
171 -- attributes.
172
173 function Is_Predicate_Static
174 (Expr : Node_Id;
175 Nam : Name_Id) return Boolean;
176 -- Given predicate expression Expr, tests if Expr is predicate-static in
177 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
178 -- name in the predicate expression have been replaced by references to
179 -- an identifier whose Chars field is Nam. This name is unique, so any
180 -- identifier with Chars matching Nam must be a reference to the type.
181 -- Returns True if the expression is predicate-static and False otherwise,
182 -- but is not in the business of setting flags or issuing error messages.
183 --
184 -- Only scalar types can have static predicates, so False is always
185 -- returned for non-scalar types.
186 --
187 -- Note: the RM seems to suggest that string types can also have static
188 -- predicates. But that really makes lttle sense as very few useful
189 -- predicates can be constructed for strings. Remember that:
190 --
191 -- "ABC" < "DEF"
192 --
193 -- is not a static expression. So even though the clearly faulty RM wording
194 -- allows the following:
195 --
196 -- subtype S is String with Static_Predicate => S < "DEF"
197 --
198 -- We can't allow this, otherwise we have predicate-static applying to a
199 -- larger class than static expressions, which was never intended.
200
201 procedure New_Stream_Subprogram
202 (N : Node_Id;
203 Ent : Entity_Id;
204 Subp : Entity_Id;
205 Nam : TSS_Name_Type);
206 -- Create a subprogram renaming of a given stream attribute to the
207 -- designated subprogram and then in the tagged case, provide this as a
208 -- primitive operation, or in the untagged case make an appropriate TSS
209 -- entry. This is more properly an expansion activity than just semantics,
210 -- but the presence of user-defined stream functions for limited types
211 -- is a legality check, which is why this takes place here rather than in
212 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
213 -- function to be generated.
214 --
215 -- To avoid elaboration anomalies with freeze nodes, for untagged types
216 -- we generate both a subprogram declaration and a subprogram renaming
217 -- declaration, so that the attribute specification is handled as a
218 -- renaming_as_body. For tagged types, the specification is one of the
219 -- primitive specs.
220
221 procedure Resolve_Iterable_Operation
222 (N : Node_Id;
223 Cursor : Entity_Id;
224 Typ : Entity_Id;
225 Nam : Name_Id);
226 -- If the name of a primitive operation for an Iterable aspect is
227 -- overloaded, resolve according to required signature.
228
229 procedure Set_Biased
230 (E : Entity_Id;
231 N : Node_Id;
232 Msg : String;
233 Biased : Boolean := True);
234 -- If Biased is True, sets Has_Biased_Representation flag for E, and
235 -- outputs a warning message at node N if Warn_On_Biased_Representation is
236 -- is True. This warning inserts the string Msg to describe the construct
237 -- causing biasing.
238
239 ----------------------------------------------
240 -- Table for Validate_Unchecked_Conversions --
241 ----------------------------------------------
242
243 -- The following table collects unchecked conversions for validation.
244 -- Entries are made by Validate_Unchecked_Conversion and then the call
245 -- to Validate_Unchecked_Conversions does the actual error checking and
246 -- posting of warnings. The reason for this delayed processing is to take
247 -- advantage of back-annotations of size and alignment values performed by
248 -- the back end.
249
250 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
251 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
252 -- already have modified all Sloc values if the -gnatD option is set.
253
254 type UC_Entry is record
255 Eloc : Source_Ptr; -- node used for posting warnings
256 Source : Entity_Id; -- source type for unchecked conversion
257 Target : Entity_Id; -- target type for unchecked conversion
258 Act_Unit : Entity_Id; -- actual function instantiated
259 end record;
260
261 package Unchecked_Conversions is new Table.Table (
262 Table_Component_Type => UC_Entry,
263 Table_Index_Type => Int,
264 Table_Low_Bound => 1,
265 Table_Initial => 50,
266 Table_Increment => 200,
267 Table_Name => "Unchecked_Conversions");
268
269 ----------------------------------------
270 -- Table for Validate_Address_Clauses --
271 ----------------------------------------
272
273 -- If an address clause has the form
274
275 -- for X'Address use Expr
276
277 -- where Expr is of the form Y'Address or recursively is a reference to a
278 -- constant of either of these forms, and X and Y are entities of objects,
279 -- then if Y has a smaller alignment than X, that merits a warning about
280 -- possible bad alignment. The following table collects address clauses of
281 -- this kind. We put these in a table so that they can be checked after the
282 -- back end has completed annotation of the alignments of objects, since we
283 -- can catch more cases that way.
284
285 type Address_Clause_Check_Record is record
286 N : Node_Id;
287 -- The address clause
288
289 X : Entity_Id;
290 -- The entity of the object overlaying Y
291
292 Y : Entity_Id;
293 -- The entity of the object being overlaid
294
295 Off : Boolean;
296 -- Whether the address is offset within Y
297 end record;
298
299 package Address_Clause_Checks is new Table.Table (
300 Table_Component_Type => Address_Clause_Check_Record,
301 Table_Index_Type => Int,
302 Table_Low_Bound => 1,
303 Table_Initial => 20,
304 Table_Increment => 200,
305 Table_Name => "Address_Clause_Checks");
306
307 -----------------------------------------
308 -- Adjust_Record_For_Reverse_Bit_Order --
309 -----------------------------------------
310
311 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
312 Comp : Node_Id;
313 CC : Node_Id;
314
315 begin
316 -- Processing depends on version of Ada
317
318 -- For Ada 95, we just renumber bits within a storage unit. We do the
319 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
320 -- Ada 83, and are free to add this extension.
321
322 if Ada_Version < Ada_2005 then
323 Comp := First_Component_Or_Discriminant (R);
324 while Present (Comp) loop
325 CC := Component_Clause (Comp);
326
327 -- If component clause is present, then deal with the non-default
328 -- bit order case for Ada 95 mode.
329
330 -- We only do this processing for the base type, and in fact that
331 -- is important, since otherwise if there are record subtypes, we
332 -- could reverse the bits once for each subtype, which is wrong.
333
334 if Present (CC) and then Ekind (R) = E_Record_Type then
335 declare
336 CFB : constant Uint := Component_Bit_Offset (Comp);
337 CSZ : constant Uint := Esize (Comp);
338 CLC : constant Node_Id := Component_Clause (Comp);
339 Pos : constant Node_Id := Position (CLC);
340 FB : constant Node_Id := First_Bit (CLC);
341
342 Storage_Unit_Offset : constant Uint :=
343 CFB / System_Storage_Unit;
344
345 Start_Bit : constant Uint :=
346 CFB mod System_Storage_Unit;
347
348 begin
349 -- Cases where field goes over storage unit boundary
350
351 if Start_Bit + CSZ > System_Storage_Unit then
352
353 -- Allow multi-byte field but generate warning
354
355 if Start_Bit mod System_Storage_Unit = 0
356 and then CSZ mod System_Storage_Unit = 0
357 then
358 Error_Msg_N
359 ("info: multi-byte field specified with "
360 & "non-standard Bit_Order?V?", CLC);
361
362 if Bytes_Big_Endian then
363 Error_Msg_N
364 ("\bytes are not reversed "
365 & "(component is big-endian)?V?", CLC);
366 else
367 Error_Msg_N
368 ("\bytes are not reversed "
369 & "(component is little-endian)?V?", CLC);
370 end if;
371
372 -- Do not allow non-contiguous field
373
374 else
375 Error_Msg_N
376 ("attempt to specify non-contiguous field "
377 & "not permitted", CLC);
378 Error_Msg_N
379 ("\caused by non-standard Bit_Order "
380 & "specified", CLC);
381 Error_Msg_N
382 ("\consider possibility of using "
383 & "Ada 2005 mode here", CLC);
384 end if;
385
386 -- Case where field fits in one storage unit
387
388 else
389 -- Give warning if suspicious component clause
390
391 if Intval (FB) >= System_Storage_Unit
392 and then Warn_On_Reverse_Bit_Order
393 then
394 Error_Msg_N
395 ("info: Bit_Order clause does not affect " &
396 "byte ordering?V?", Pos);
397 Error_Msg_Uint_1 :=
398 Intval (Pos) + Intval (FB) /
399 System_Storage_Unit;
400 Error_Msg_N
401 ("info: position normalized to ^ before bit " &
402 "order interpreted?V?", Pos);
403 end if;
404
405 -- Here is where we fix up the Component_Bit_Offset value
406 -- to account for the reverse bit order. Some examples of
407 -- what needs to be done are:
408
409 -- First_Bit .. Last_Bit Component_Bit_Offset
410 -- old new old new
411
412 -- 0 .. 0 7 .. 7 0 7
413 -- 0 .. 1 6 .. 7 0 6
414 -- 0 .. 2 5 .. 7 0 5
415 -- 0 .. 7 0 .. 7 0 4
416
417 -- 1 .. 1 6 .. 6 1 6
418 -- 1 .. 4 3 .. 6 1 3
419 -- 4 .. 7 0 .. 3 4 0
420
421 -- The rule is that the first bit is is obtained by
422 -- subtracting the old ending bit from storage_unit - 1.
423
424 Set_Component_Bit_Offset
425 (Comp,
426 (Storage_Unit_Offset * System_Storage_Unit) +
427 (System_Storage_Unit - 1) -
428 (Start_Bit + CSZ - 1));
429
430 Set_Normalized_First_Bit
431 (Comp,
432 Component_Bit_Offset (Comp) mod
433 System_Storage_Unit);
434 end if;
435 end;
436 end if;
437
438 Next_Component_Or_Discriminant (Comp);
439 end loop;
440
441 -- For Ada 2005, we do machine scalar processing, as fully described In
442 -- AI-133. This involves gathering all components which start at the
443 -- same byte offset and processing them together. Same approach is still
444 -- valid in later versions including Ada 2012.
445
446 else
447 declare
448 Max_Machine_Scalar_Size : constant Uint :=
449 UI_From_Int
450 (Standard_Long_Long_Integer_Size);
451 -- We use this as the maximum machine scalar size
452
453 Num_CC : Natural;
454 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
455
456 begin
457 -- This first loop through components does two things. First it
458 -- deals with the case of components with component clauses whose
459 -- length is greater than the maximum machine scalar size (either
460 -- accepting them or rejecting as needed). Second, it counts the
461 -- number of components with component clauses whose length does
462 -- not exceed this maximum for later processing.
463
464 Num_CC := 0;
465 Comp := First_Component_Or_Discriminant (R);
466 while Present (Comp) loop
467 CC := Component_Clause (Comp);
468
469 if Present (CC) then
470 declare
471 Fbit : constant Uint := Static_Integer (First_Bit (CC));
472 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
473
474 begin
475 -- Case of component with last bit >= max machine scalar
476
477 if Lbit >= Max_Machine_Scalar_Size then
478
479 -- This is allowed only if first bit is zero, and
480 -- last bit + 1 is a multiple of storage unit size.
481
482 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
483
484 -- This is the case to give a warning if enabled
485
486 if Warn_On_Reverse_Bit_Order then
487 Error_Msg_N
488 ("info: multi-byte field specified with "
489 & "non-standard Bit_Order?V?", CC);
490
491 if Bytes_Big_Endian then
492 Error_Msg_N
493 ("\bytes are not reversed "
494 & "(component is big-endian)?V?", CC);
495 else
496 Error_Msg_N
497 ("\bytes are not reversed "
498 & "(component is little-endian)?V?", CC);
499 end if;
500 end if;
501
502 -- Give error message for RM 13.5.1(10) violation
503
504 else
505 Error_Msg_FE
506 ("machine scalar rules not followed for&",
507 First_Bit (CC), Comp);
508
509 Error_Msg_Uint_1 := Lbit + 1;
510 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
511 Error_Msg_F
512 ("\last bit + 1 (^) exceeds maximum machine "
513 & "scalar size (^)",
514 First_Bit (CC));
515
516 if (Lbit + 1) mod SSU /= 0 then
517 Error_Msg_Uint_1 := SSU;
518 Error_Msg_F
519 ("\and is not a multiple of Storage_Unit (^) "
520 & "(RM 13.5.1(10))",
521 First_Bit (CC));
522
523 else
524 Error_Msg_Uint_1 := Fbit;
525 Error_Msg_F
526 ("\and first bit (^) is non-zero "
527 & "(RM 13.4.1(10))",
528 First_Bit (CC));
529 end if;
530 end if;
531
532 -- OK case of machine scalar related component clause,
533 -- For now, just count them.
534
535 else
536 Num_CC := Num_CC + 1;
537 end if;
538 end;
539 end if;
540
541 Next_Component_Or_Discriminant (Comp);
542 end loop;
543
544 -- We need to sort the component clauses on the basis of the
545 -- Position values in the clause, so we can group clauses with
546 -- the same Position together to determine the relevant machine
547 -- scalar size.
548
549 Sort_CC : declare
550 Comps : array (0 .. Num_CC) of Entity_Id;
551 -- Array to collect component and discriminant entities. The
552 -- data starts at index 1, the 0'th entry is for the sort
553 -- routine.
554
555 function CP_Lt (Op1, Op2 : Natural) return Boolean;
556 -- Compare routine for Sort
557
558 procedure CP_Move (From : Natural; To : Natural);
559 -- Move routine for Sort
560
561 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
562
563 Start : Natural;
564 Stop : Natural;
565 -- Start and stop positions in the component list of the set of
566 -- components with the same starting position (that constitute
567 -- components in a single machine scalar).
568
569 MaxL : Uint;
570 -- Maximum last bit value of any component in this set
571
572 MSS : Uint;
573 -- Corresponding machine scalar size
574
575 -----------
576 -- CP_Lt --
577 -----------
578
579 function CP_Lt (Op1, Op2 : Natural) return Boolean is
580 begin
581 return Position (Component_Clause (Comps (Op1))) <
582 Position (Component_Clause (Comps (Op2)));
583 end CP_Lt;
584
585 -------------
586 -- CP_Move --
587 -------------
588
589 procedure CP_Move (From : Natural; To : Natural) is
590 begin
591 Comps (To) := Comps (From);
592 end CP_Move;
593
594 -- Start of processing for Sort_CC
595
596 begin
597 -- Collect the machine scalar relevant component clauses
598
599 Num_CC := 0;
600 Comp := First_Component_Or_Discriminant (R);
601 while Present (Comp) loop
602 declare
603 CC : constant Node_Id := Component_Clause (Comp);
604
605 begin
606 -- Collect only component clauses whose last bit is less
607 -- than machine scalar size. Any component clause whose
608 -- last bit exceeds this value does not take part in
609 -- machine scalar layout considerations. The test for
610 -- Error_Posted makes sure we exclude component clauses
611 -- for which we already posted an error.
612
613 if Present (CC)
614 and then not Error_Posted (Last_Bit (CC))
615 and then Static_Integer (Last_Bit (CC)) <
616 Max_Machine_Scalar_Size
617 then
618 Num_CC := Num_CC + 1;
619 Comps (Num_CC) := Comp;
620 end if;
621 end;
622
623 Next_Component_Or_Discriminant (Comp);
624 end loop;
625
626 -- Sort by ascending position number
627
628 Sorting.Sort (Num_CC);
629
630 -- We now have all the components whose size does not exceed
631 -- the max machine scalar value, sorted by starting position.
632 -- In this loop we gather groups of clauses starting at the
633 -- same position, to process them in accordance with AI-133.
634
635 Stop := 0;
636 while Stop < Num_CC loop
637 Start := Stop + 1;
638 Stop := Start;
639 MaxL :=
640 Static_Integer
641 (Last_Bit (Component_Clause (Comps (Start))));
642 while Stop < Num_CC loop
643 if Static_Integer
644 (Position (Component_Clause (Comps (Stop + 1)))) =
645 Static_Integer
646 (Position (Component_Clause (Comps (Stop))))
647 then
648 Stop := Stop + 1;
649 MaxL :=
650 UI_Max
651 (MaxL,
652 Static_Integer
653 (Last_Bit
654 (Component_Clause (Comps (Stop)))));
655 else
656 exit;
657 end if;
658 end loop;
659
660 -- Now we have a group of component clauses from Start to
661 -- Stop whose positions are identical, and MaxL is the
662 -- maximum last bit value of any of these components.
663
664 -- We need to determine the corresponding machine scalar
665 -- size. This loop assumes that machine scalar sizes are
666 -- even, and that each possible machine scalar has twice
667 -- as many bits as the next smaller one.
668
669 MSS := Max_Machine_Scalar_Size;
670 while MSS mod 2 = 0
671 and then (MSS / 2) >= SSU
672 and then (MSS / 2) > MaxL
673 loop
674 MSS := MSS / 2;
675 end loop;
676
677 -- Here is where we fix up the Component_Bit_Offset value
678 -- to account for the reverse bit order. Some examples of
679 -- what needs to be done for the case of a machine scalar
680 -- size of 8 are:
681
682 -- First_Bit .. Last_Bit Component_Bit_Offset
683 -- old new old new
684
685 -- 0 .. 0 7 .. 7 0 7
686 -- 0 .. 1 6 .. 7 0 6
687 -- 0 .. 2 5 .. 7 0 5
688 -- 0 .. 7 0 .. 7 0 4
689
690 -- 1 .. 1 6 .. 6 1 6
691 -- 1 .. 4 3 .. 6 1 3
692 -- 4 .. 7 0 .. 3 4 0
693
694 -- The rule is that the first bit is obtained by subtracting
695 -- the old ending bit from machine scalar size - 1.
696
697 for C in Start .. Stop loop
698 declare
699 Comp : constant Entity_Id := Comps (C);
700 CC : constant Node_Id := Component_Clause (Comp);
701
702 LB : constant Uint := Static_Integer (Last_Bit (CC));
703 NFB : constant Uint := MSS - Uint_1 - LB;
704 NLB : constant Uint := NFB + Esize (Comp) - 1;
705 Pos : constant Uint := Static_Integer (Position (CC));
706
707 begin
708 if Warn_On_Reverse_Bit_Order then
709 Error_Msg_Uint_1 := MSS;
710 Error_Msg_N
711 ("info: reverse bit order in machine " &
712 "scalar of length^?V?", First_Bit (CC));
713 Error_Msg_Uint_1 := NFB;
714 Error_Msg_Uint_2 := NLB;
715
716 if Bytes_Big_Endian then
717 Error_Msg_NE
718 ("\big-endian range for component "
719 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
720 else
721 Error_Msg_NE
722 ("\little-endian range for component"
723 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
724 end if;
725 end if;
726
727 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
728 Set_Normalized_First_Bit (Comp, NFB mod SSU);
729 end;
730 end loop;
731 end loop;
732 end Sort_CC;
733 end;
734 end if;
735 end Adjust_Record_For_Reverse_Bit_Order;
736
737 -------------------------------------
738 -- Alignment_Check_For_Size_Change --
739 -------------------------------------
740
741 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
742 begin
743 -- If the alignment is known, and not set by a rep clause, and is
744 -- inconsistent with the size being set, then reset it to unknown,
745 -- we assume in this case that the size overrides the inherited
746 -- alignment, and that the alignment must be recomputed.
747
748 if Known_Alignment (Typ)
749 and then not Has_Alignment_Clause (Typ)
750 and then Size mod (Alignment (Typ) * SSU) /= 0
751 then
752 Init_Alignment (Typ);
753 end if;
754 end Alignment_Check_For_Size_Change;
755
756 -------------------------------------
757 -- Analyze_Aspects_At_Freeze_Point --
758 -------------------------------------
759
760 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
761 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
762 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
763 -- the aspect specification node ASN.
764
765 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
766 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
767 -- a derived type can inherit aspects from its parent which have been
768 -- specified at the time of the derivation using an aspect, as in:
769 --
770 -- type A is range 1 .. 10
771 -- with Size => Not_Defined_Yet;
772 -- ..
773 -- type B is new A;
774 -- ..
775 -- Not_Defined_Yet : constant := 64;
776 --
777 -- In this example, the Size of A is considered to be specified prior
778 -- to the derivation, and thus inherited, even though the value is not
779 -- known at the time of derivation. To deal with this, we use two entity
780 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
781 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
782 -- the derived type (B here). If this flag is set when the derived type
783 -- is frozen, then this procedure is called to ensure proper inheritance
784 -- of all delayed aspects from the parent type. The derived type is E,
785 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
786 -- aspect specification node in the Rep_Item chain for the parent type.
787
788 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
789 -- Given an aspect specification node ASN whose expression is an
790 -- optional Boolean, this routines creates the corresponding pragma
791 -- at the freezing point.
792
793 ----------------------------------
794 -- Analyze_Aspect_Default_Value --
795 ----------------------------------
796
797 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
798 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
799 Ent : constant Entity_Id := Entity (ASN);
800 Expr : constant Node_Id := Expression (ASN);
801 Id : constant Node_Id := Identifier (ASN);
802
803 begin
804 Error_Msg_Name_1 := Chars (Id);
805
806 if not Is_Type (Ent) then
807 Error_Msg_N ("aspect% can only apply to a type", Id);
808 return;
809
810 elsif not Is_First_Subtype (Ent) then
811 Error_Msg_N ("aspect% cannot apply to subtype", Id);
812 return;
813
814 elsif A_Id = Aspect_Default_Value
815 and then not Is_Scalar_Type (Ent)
816 then
817 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
818 return;
819
820 elsif A_Id = Aspect_Default_Component_Value then
821 if not Is_Array_Type (Ent) then
822 Error_Msg_N ("aspect% can only be applied to array type", Id);
823 return;
824
825 elsif not Is_Scalar_Type (Component_Type (Ent)) then
826 Error_Msg_N ("aspect% requires scalar components", Id);
827 return;
828 end if;
829 end if;
830
831 Set_Has_Default_Aspect (Base_Type (Ent));
832
833 if Is_Scalar_Type (Ent) then
834 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
835 else
836 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
837 end if;
838 end Analyze_Aspect_Default_Value;
839
840 ---------------------------------
841 -- Inherit_Delayed_Rep_Aspects --
842 ---------------------------------
843
844 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
845 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
846 P : constant Entity_Id := Entity (ASN);
847 -- Entithy for parent type
848
849 N : Node_Id;
850 -- Item from Rep_Item chain
851
852 A : Aspect_Id;
853
854 begin
855 -- Loop through delayed aspects for the parent type
856
857 N := ASN;
858 while Present (N) loop
859 if Nkind (N) = N_Aspect_Specification then
860 exit when Entity (N) /= P;
861
862 if Is_Delayed_Aspect (N) then
863 A := Get_Aspect_Id (Chars (Identifier (N)));
864
865 -- Process delayed rep aspect. For Boolean attributes it is
866 -- not possible to cancel an attribute once set (the attempt
867 -- to use an aspect with xxx => False is an error) for a
868 -- derived type. So for those cases, we do not have to check
869 -- if a clause has been given for the derived type, since it
870 -- is harmless to set it again if it is already set.
871
872 case A is
873
874 -- Alignment
875
876 when Aspect_Alignment =>
877 if not Has_Alignment_Clause (E) then
878 Set_Alignment (E, Alignment (P));
879 end if;
880
881 -- Atomic
882
883 when Aspect_Atomic =>
884 if Is_Atomic (P) then
885 Set_Is_Atomic (E);
886 end if;
887
888 -- Atomic_Components
889
890 when Aspect_Atomic_Components =>
891 if Has_Atomic_Components (P) then
892 Set_Has_Atomic_Components (Base_Type (E));
893 end if;
894
895 -- Bit_Order
896
897 when Aspect_Bit_Order =>
898 if Is_Record_Type (E)
899 and then No (Get_Attribute_Definition_Clause
900 (E, Attribute_Bit_Order))
901 and then Reverse_Bit_Order (P)
902 then
903 Set_Reverse_Bit_Order (Base_Type (E));
904 end if;
905
906 -- Component_Size
907
908 when Aspect_Component_Size =>
909 if Is_Array_Type (E)
910 and then not Has_Component_Size_Clause (E)
911 then
912 Set_Component_Size
913 (Base_Type (E), Component_Size (P));
914 end if;
915
916 -- Machine_Radix
917
918 when Aspect_Machine_Radix =>
919 if Is_Decimal_Fixed_Point_Type (E)
920 and then not Has_Machine_Radix_Clause (E)
921 then
922 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
923 end if;
924
925 -- Object_Size (also Size which also sets Object_Size)
926
927 when Aspect_Object_Size | Aspect_Size =>
928 if not Has_Size_Clause (E)
929 and then
930 No (Get_Attribute_Definition_Clause
931 (E, Attribute_Object_Size))
932 then
933 Set_Esize (E, Esize (P));
934 end if;
935
936 -- Pack
937
938 when Aspect_Pack =>
939 if not Is_Packed (E) then
940 Set_Is_Packed (Base_Type (E));
941
942 if Is_Bit_Packed_Array (P) then
943 Set_Is_Bit_Packed_Array (Base_Type (E));
944 Set_Packed_Array_Impl_Type
945 (E, Packed_Array_Impl_Type (P));
946 end if;
947 end if;
948
949 -- Scalar_Storage_Order
950
951 when Aspect_Scalar_Storage_Order =>
952 if (Is_Record_Type (E) or else Is_Array_Type (E))
953 and then No (Get_Attribute_Definition_Clause
954 (E, Attribute_Scalar_Storage_Order))
955 and then Reverse_Storage_Order (P)
956 then
957 Set_Reverse_Storage_Order (Base_Type (E));
958
959 -- Clear default SSO indications, since the aspect
960 -- overrides the default.
961
962 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
963 Set_SSO_Set_High_By_Default (Base_Type (E), False);
964 end if;
965
966 -- Small
967
968 when Aspect_Small =>
969 if Is_Fixed_Point_Type (E)
970 and then not Has_Small_Clause (E)
971 then
972 Set_Small_Value (E, Small_Value (P));
973 end if;
974
975 -- Storage_Size
976
977 when Aspect_Storage_Size =>
978 if (Is_Access_Type (E) or else Is_Task_Type (E))
979 and then not Has_Storage_Size_Clause (E)
980 then
981 Set_Storage_Size_Variable
982 (Base_Type (E), Storage_Size_Variable (P));
983 end if;
984
985 -- Value_Size
986
987 when Aspect_Value_Size =>
988
989 -- Value_Size is never inherited, it is either set by
990 -- default, or it is explicitly set for the derived
991 -- type. So nothing to do here.
992
993 null;
994
995 -- Volatile
996
997 when Aspect_Volatile =>
998 if Is_Volatile (P) then
999 Set_Is_Volatile (E);
1000 end if;
1001
1002 -- Volatile_Full_Access
1003
1004 when Aspect_Volatile_Full_Access =>
1005 if Is_Volatile_Full_Access (P) then
1006 Set_Is_Volatile_Full_Access (E);
1007 end if;
1008
1009 -- Volatile_Components
1010
1011 when Aspect_Volatile_Components =>
1012 if Has_Volatile_Components (P) then
1013 Set_Has_Volatile_Components (Base_Type (E));
1014 end if;
1015
1016 -- That should be all the Rep Aspects
1017
1018 when others =>
1019 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1020 null;
1021
1022 end case;
1023 end if;
1024 end if;
1025
1026 N := Next_Rep_Item (N);
1027 end loop;
1028 end Inherit_Delayed_Rep_Aspects;
1029
1030 -------------------------------------
1031 -- Make_Pragma_From_Boolean_Aspect --
1032 -------------------------------------
1033
1034 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1035 Ident : constant Node_Id := Identifier (ASN);
1036 A_Name : constant Name_Id := Chars (Ident);
1037 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1038 Ent : constant Entity_Id := Entity (ASN);
1039 Expr : constant Node_Id := Expression (ASN);
1040 Loc : constant Source_Ptr := Sloc (ASN);
1041
1042 procedure Check_False_Aspect_For_Derived_Type;
1043 -- This procedure checks for the case of a false aspect for a derived
1044 -- type, which improperly tries to cancel an aspect inherited from
1045 -- the parent.
1046
1047 -----------------------------------------
1048 -- Check_False_Aspect_For_Derived_Type --
1049 -----------------------------------------
1050
1051 procedure Check_False_Aspect_For_Derived_Type is
1052 Par : Node_Id;
1053
1054 begin
1055 -- We are only checking derived types
1056
1057 if not Is_Derived_Type (E) then
1058 return;
1059 end if;
1060
1061 Par := Nearest_Ancestor (E);
1062
1063 case A_Id is
1064 when Aspect_Atomic | Aspect_Shared =>
1065 if not Is_Atomic (Par) then
1066 return;
1067 end if;
1068
1069 when Aspect_Atomic_Components =>
1070 if not Has_Atomic_Components (Par) then
1071 return;
1072 end if;
1073
1074 when Aspect_Discard_Names =>
1075 if not Discard_Names (Par) then
1076 return;
1077 end if;
1078
1079 when Aspect_Pack =>
1080 if not Is_Packed (Par) then
1081 return;
1082 end if;
1083
1084 when Aspect_Unchecked_Union =>
1085 if not Is_Unchecked_Union (Par) then
1086 return;
1087 end if;
1088
1089 when Aspect_Volatile =>
1090 if not Is_Volatile (Par) then
1091 return;
1092 end if;
1093
1094 when Aspect_Volatile_Components =>
1095 if not Has_Volatile_Components (Par) then
1096 return;
1097 end if;
1098
1099 when Aspect_Volatile_Full_Access =>
1100 if not Is_Volatile_Full_Access (Par) then
1101 return;
1102 end if;
1103
1104 when others =>
1105 return;
1106 end case;
1107
1108 -- Fall through means we are canceling an inherited aspect
1109
1110 Error_Msg_Name_1 := A_Name;
1111 Error_Msg_NE
1112 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1113 end Check_False_Aspect_For_Derived_Type;
1114
1115 -- Local variables
1116
1117 Prag : Node_Id;
1118
1119 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1120
1121 begin
1122 -- Note that we know Expr is present, because for a missing Expr
1123 -- argument, we knew it was True and did not need to delay the
1124 -- evaluation to the freeze point.
1125
1126 if Is_False (Static_Boolean (Expr)) then
1127 Check_False_Aspect_For_Derived_Type;
1128
1129 else
1130 Prag :=
1131 Make_Pragma (Loc,
1132 Pragma_Identifier =>
1133 Make_Identifier (Sloc (Ident), Chars (Ident)),
1134 Pragma_Argument_Associations => New_List (
1135 Make_Pragma_Argument_Association (Sloc (Ident),
1136 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
1137
1138 Set_From_Aspect_Specification (Prag, True);
1139 Set_Corresponding_Aspect (Prag, ASN);
1140 Set_Aspect_Rep_Item (ASN, Prag);
1141 Set_Is_Delayed_Aspect (Prag);
1142 Set_Parent (Prag, ASN);
1143 end if;
1144 end Make_Pragma_From_Boolean_Aspect;
1145
1146 -- Local variables
1147
1148 A_Id : Aspect_Id;
1149 ASN : Node_Id;
1150 Ritem : Node_Id;
1151
1152 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1153
1154 begin
1155 -- Must be visible in current scope
1156
1157 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1158 return;
1159 end if;
1160
1161 -- Look for aspect specification entries for this entity
1162
1163 ASN := First_Rep_Item (E);
1164 while Present (ASN) loop
1165 if Nkind (ASN) = N_Aspect_Specification then
1166 exit when Entity (ASN) /= E;
1167
1168 if Is_Delayed_Aspect (ASN) then
1169 A_Id := Get_Aspect_Id (ASN);
1170
1171 case A_Id is
1172
1173 -- For aspects whose expression is an optional Boolean, make
1174 -- the corresponding pragma at the freeze point.
1175
1176 when Boolean_Aspects |
1177 Library_Unit_Aspects =>
1178
1179 -- Aspects Export and Import require special handling.
1180 -- Both are by definition Boolean and may benefit from
1181 -- forward references, however their expressions are
1182 -- treated as static. In addition, the syntax of their
1183 -- corresponding pragmas requires extra "pieces" which
1184 -- may also contain forward references. To account for
1185 -- all of this, the corresponding pragma is created by
1186 -- Analyze_Aspect_Export_Import, but is not analyzed as
1187 -- the complete analysis must happen now.
1188
1189 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1190 null;
1191
1192 -- Otherwise create a corresponding pragma
1193
1194 else
1195 Make_Pragma_From_Boolean_Aspect (ASN);
1196 end if;
1197
1198 -- Special handling for aspects that don't correspond to
1199 -- pragmas/attributes.
1200
1201 when Aspect_Default_Value |
1202 Aspect_Default_Component_Value =>
1203
1204 -- Do not inherit aspect for anonymous base type of a
1205 -- scalar or array type, because they apply to the first
1206 -- subtype of the type, and will be processed when that
1207 -- first subtype is frozen.
1208
1209 if Is_Derived_Type (E)
1210 and then not Comes_From_Source (E)
1211 and then E /= First_Subtype (E)
1212 then
1213 null;
1214 else
1215 Analyze_Aspect_Default_Value (ASN);
1216 end if;
1217
1218 -- Ditto for iterator aspects, because the corresponding
1219 -- attributes may not have been analyzed yet.
1220
1221 when Aspect_Constant_Indexing |
1222 Aspect_Variable_Indexing |
1223 Aspect_Default_Iterator |
1224 Aspect_Iterator_Element =>
1225 Analyze (Expression (ASN));
1226
1227 if Etype (Expression (ASN)) = Any_Type then
1228 Error_Msg_NE
1229 ("\aspect must be fully defined before & is frozen",
1230 ASN, E);
1231 end if;
1232
1233 when Aspect_Iterable =>
1234 Validate_Iterable_Aspect (E, ASN);
1235
1236 when others =>
1237 null;
1238 end case;
1239
1240 Ritem := Aspect_Rep_Item (ASN);
1241
1242 if Present (Ritem) then
1243 Analyze (Ritem);
1244 end if;
1245 end if;
1246 end if;
1247
1248 Next_Rep_Item (ASN);
1249 end loop;
1250
1251 -- This is where we inherit delayed rep aspects from our parent. Note
1252 -- that if we fell out of the above loop with ASN non-empty, it means
1253 -- we hit an aspect for an entity other than E, and it must be the
1254 -- type from which we were derived.
1255
1256 if May_Inherit_Delayed_Rep_Aspects (E) then
1257 Inherit_Delayed_Rep_Aspects (ASN);
1258 end if;
1259 end Analyze_Aspects_At_Freeze_Point;
1260
1261 -----------------------------------
1262 -- Analyze_Aspect_Specifications --
1263 -----------------------------------
1264
1265 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1266 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
1267 -- Establish linkages between an aspect and its corresponding pragma
1268
1269 procedure Insert_Pragma
1270 (Prag : Node_Id;
1271 Is_Instance : Boolean := False);
1272 -- Subsidiary to the analysis of aspects
1273 -- Abstract_State
1274 -- Attach_Handler
1275 -- Contract_Cases
1276 -- Depends
1277 -- Ghost
1278 -- Global
1279 -- Initial_Condition
1280 -- Initializes
1281 -- Post
1282 -- Pre
1283 -- Refined_Depends
1284 -- Refined_Global
1285 -- Refined_State
1286 -- SPARK_Mode
1287 -- Warnings
1288 -- Insert pragma Prag such that it mimics the placement of a source
1289 -- pragma of the same kind. Flag Is_Generic should be set when the
1290 -- context denotes a generic instance.
1291
1292 --------------
1293 -- Decorate --
1294 --------------
1295
1296 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1297 begin
1298 Set_Aspect_Rep_Item (Asp, Prag);
1299 Set_Corresponding_Aspect (Prag, Asp);
1300 Set_From_Aspect_Specification (Prag);
1301 Set_Parent (Prag, Asp);
1302 end Decorate;
1303
1304 -------------------
1305 -- Insert_Pragma --
1306 -------------------
1307
1308 procedure Insert_Pragma
1309 (Prag : Node_Id;
1310 Is_Instance : Boolean := False)
1311 is
1312 Aux : Node_Id;
1313 Decl : Node_Id;
1314 Decls : List_Id;
1315 Def : Node_Id;
1316 Inserted : Boolean := False;
1317
1318 begin
1319 -- When the aspect appears on an entry, package, protected unit,
1320 -- subprogram, or task unit body, insert the generated pragma at the
1321 -- top of the body declarations to emulate the behavior of a source
1322 -- pragma.
1323
1324 -- package body Pack with Aspect is
1325
1326 -- package body Pack is
1327 -- pragma Prag;
1328
1329 if Nkind_In (N, N_Entry_Body,
1330 N_Package_Body,
1331 N_Protected_Body,
1332 N_Subprogram_Body,
1333 N_Task_Body)
1334 then
1335 Decls := Declarations (N);
1336
1337 if No (Decls) then
1338 Decls := New_List;
1339 Set_Declarations (N, Decls);
1340 end if;
1341
1342 Prepend_To (Decls, Prag);
1343
1344 -- When the aspect is associated with a [generic] package declaration
1345 -- insert the generated pragma at the top of the visible declarations
1346 -- to emulate the behavior of a source pragma.
1347
1348 -- package Pack with Aspect is
1349
1350 -- package Pack is
1351 -- pragma Prag;
1352
1353 elsif Nkind_In (N, N_Generic_Package_Declaration,
1354 N_Package_Declaration)
1355 then
1356 Decls := Visible_Declarations (Specification (N));
1357
1358 if No (Decls) then
1359 Decls := New_List;
1360 Set_Visible_Declarations (Specification (N), Decls);
1361 end if;
1362
1363 -- The visible declarations of a generic instance have the
1364 -- following structure:
1365
1366 -- <renamings of generic formals>
1367 -- <renamings of internally-generated spec and body>
1368 -- <first source declaration>
1369
1370 -- Insert the pragma before the first source declaration by
1371 -- skipping the instance "header" to ensure proper visibility of
1372 -- all formals.
1373
1374 if Is_Instance then
1375 Decl := First (Decls);
1376 while Present (Decl) loop
1377 if Comes_From_Source (Decl) then
1378 Insert_Before (Decl, Prag);
1379 Inserted := True;
1380 exit;
1381 else
1382 Next (Decl);
1383 end if;
1384 end loop;
1385
1386 -- The pragma is placed after the instance "header"
1387
1388 if not Inserted then
1389 Append_To (Decls, Prag);
1390 end if;
1391
1392 -- Otherwise this is not a generic instance
1393
1394 else
1395 Prepend_To (Decls, Prag);
1396 end if;
1397
1398 -- When the aspect is associated with a protected unit declaration,
1399 -- insert the generated pragma at the top of the visible declarations
1400 -- the emulate the behavior of a source pragma.
1401
1402 -- protected [type] Prot with Aspect is
1403
1404 -- protected [type] Prot is
1405 -- pragma Prag;
1406
1407 elsif Nkind (N) = N_Protected_Type_Declaration then
1408 Def := Protected_Definition (N);
1409
1410 if No (Def) then
1411 Def :=
1412 Make_Protected_Definition (Sloc (N),
1413 Visible_Declarations => New_List,
1414 End_Label => Empty);
1415
1416 Set_Protected_Definition (N, Def);
1417 end if;
1418
1419 Decls := Visible_Declarations (Def);
1420
1421 if No (Decls) then
1422 Decls := New_List;
1423 Set_Visible_Declarations (Def, Decls);
1424 end if;
1425
1426 Prepend_To (Decls, Prag);
1427
1428 -- When the aspect is associated with a task unit declaration, insert
1429 -- insert the generated pragma at the top of the visible declarations
1430 -- the emulate the behavior of a source pragma.
1431
1432 -- task [type] Prot with Aspect is
1433
1434 -- task [type] Prot is
1435 -- pragma Prag;
1436
1437 elsif Nkind (N) = N_Task_Type_Declaration then
1438 Def := Task_Definition (N);
1439
1440 if No (Def) then
1441 Def :=
1442 Make_Task_Definition (Sloc (N),
1443 Visible_Declarations => New_List,
1444 End_Label => Empty);
1445
1446 Set_Task_Definition (N, Def);
1447 end if;
1448
1449 Decls := Visible_Declarations (Def);
1450
1451 if No (Decls) then
1452 Decls := New_List;
1453 Set_Visible_Declarations (Def, Decls);
1454 end if;
1455
1456 Prepend_To (Decls, Prag);
1457
1458 -- When the context is a library unit, the pragma is added to the
1459 -- Pragmas_After list.
1460
1461 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1462 Aux := Aux_Decls_Node (Parent (N));
1463
1464 if No (Pragmas_After (Aux)) then
1465 Set_Pragmas_After (Aux, New_List);
1466 end if;
1467
1468 Prepend (Prag, Pragmas_After (Aux));
1469
1470 -- Default, the pragma is inserted after the context
1471
1472 else
1473 Insert_After (N, Prag);
1474 end if;
1475 end Insert_Pragma;
1476
1477 -- Local variables
1478
1479 Aspect : Node_Id;
1480 Aitem : Node_Id;
1481 Ent : Node_Id;
1482
1483 L : constant List_Id := Aspect_Specifications (N);
1484
1485 Ins_Node : Node_Id := N;
1486 -- Insert pragmas/attribute definition clause after this node when no
1487 -- delayed analysis is required.
1488
1489 -- Start of processing for Analyze_Aspect_Specifications
1490
1491 begin
1492 -- The general processing involves building an attribute definition
1493 -- clause or a pragma node that corresponds to the aspect. Then in order
1494 -- to delay the evaluation of this aspect to the freeze point, we attach
1495 -- the corresponding pragma/attribute definition clause to the aspect
1496 -- specification node, which is then placed in the Rep Item chain. In
1497 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1498 -- and we evaluate the rep item at the freeze point. When the aspect
1499 -- doesn't have a corresponding pragma/attribute definition clause, then
1500 -- its analysis is simply delayed at the freeze point.
1501
1502 -- Some special cases don't require delay analysis, thus the aspect is
1503 -- analyzed right now.
1504
1505 -- Note that there is a special handling for Pre, Post, Test_Case,
1506 -- Contract_Cases aspects. In these cases, we do not have to worry
1507 -- about delay issues, since the pragmas themselves deal with delay
1508 -- of visibility for the expression analysis. Thus, we just insert
1509 -- the pragma after the node N.
1510
1511 pragma Assert (Present (L));
1512
1513 -- Loop through aspects
1514
1515 Aspect := First (L);
1516 Aspect_Loop : while Present (Aspect) loop
1517 Analyze_One_Aspect : declare
1518 Expr : constant Node_Id := Expression (Aspect);
1519 Id : constant Node_Id := Identifier (Aspect);
1520 Loc : constant Source_Ptr := Sloc (Aspect);
1521 Nam : constant Name_Id := Chars (Id);
1522 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1523 Anod : Node_Id;
1524
1525 Delay_Required : Boolean;
1526 -- Set False if delay is not required
1527
1528 Eloc : Source_Ptr := No_Location;
1529 -- Source location of expression, modified when we split PPC's. It
1530 -- is set below when Expr is present.
1531
1532 procedure Analyze_Aspect_Convention;
1533 -- Perform analysis of aspect Convention
1534
1535 procedure Analyze_Aspect_Export_Import;
1536 -- Perform analysis of aspects Export or Import
1537
1538 procedure Analyze_Aspect_External_Link_Name;
1539 -- Perform analysis of aspects External_Name or Link_Name
1540
1541 procedure Analyze_Aspect_Implicit_Dereference;
1542 -- Perform analysis of the Implicit_Dereference aspects
1543
1544 procedure Make_Aitem_Pragma
1545 (Pragma_Argument_Associations : List_Id;
1546 Pragma_Name : Name_Id);
1547 -- This is a wrapper for Make_Pragma used for converting aspects
1548 -- to pragmas. It takes care of Sloc (set from Loc) and building
1549 -- the pragma identifier from the given name. In addition the
1550 -- flags Class_Present and Split_PPC are set from the aspect
1551 -- node, as well as Is_Ignored. This routine also sets the
1552 -- From_Aspect_Specification in the resulting pragma node to
1553 -- True, and sets Corresponding_Aspect to point to the aspect.
1554 -- The resulting pragma is assigned to Aitem.
1555
1556 -------------------------------
1557 -- Analyze_Aspect_Convention --
1558 -------------------------------
1559
1560 procedure Analyze_Aspect_Convention is
1561 Conv : Node_Id;
1562 Dummy_1 : Node_Id;
1563 Dummy_2 : Node_Id;
1564 Dummy_3 : Node_Id;
1565 Expo : Node_Id;
1566 Imp : Node_Id;
1567
1568 begin
1569 -- Obtain all interfacing aspects that apply to the related
1570 -- entity.
1571
1572 Get_Interfacing_Aspects
1573 (Iface_Asp => Aspect,
1574 Conv_Asp => Dummy_1,
1575 EN_Asp => Dummy_2,
1576 Expo_Asp => Expo,
1577 Imp_Asp => Imp,
1578 LN_Asp => Dummy_3,
1579 Do_Checks => True);
1580
1581 -- The related entity is subject to aspect Export or Import.
1582 -- Do not process Convention now because it must be analysed
1583 -- as part of Export or Import.
1584
1585 if Present (Expo) or else Present (Imp) then
1586 return;
1587
1588 -- Otherwise Convention appears by itself
1589
1590 else
1591 -- The aspect specifies a particular convention
1592
1593 if Present (Expr) then
1594 Conv := New_Copy_Tree (Expr);
1595
1596 -- Otherwise assume convention Ada
1597
1598 else
1599 Conv := Make_Identifier (Loc, Name_Ada);
1600 end if;
1601
1602 -- Generate:
1603 -- pragma Convention (<Conv>, <E>);
1604
1605 Make_Aitem_Pragma
1606 (Pragma_Name => Name_Convention,
1607 Pragma_Argument_Associations => New_List (
1608 Make_Pragma_Argument_Association (Loc,
1609 Expression => Conv),
1610 Make_Pragma_Argument_Association (Loc,
1611 Expression => New_Occurrence_Of (E, Loc))));
1612
1613 Decorate (Aspect, Aitem);
1614 Insert_Pragma (Aitem);
1615 end if;
1616 end Analyze_Aspect_Convention;
1617
1618 ----------------------------------
1619 -- Analyze_Aspect_Export_Import --
1620 ----------------------------------
1621
1622 procedure Analyze_Aspect_Export_Import is
1623 Dummy_1 : Node_Id;
1624 Dummy_2 : Node_Id;
1625 Dummy_3 : Node_Id;
1626 Expo : Node_Id;
1627 Imp : Node_Id;
1628
1629 begin
1630 -- Obtain all interfacing aspects that apply to the related
1631 -- entity.
1632
1633 Get_Interfacing_Aspects
1634 (Iface_Asp => Aspect,
1635 Conv_Asp => Dummy_1,
1636 EN_Asp => Dummy_2,
1637 Expo_Asp => Expo,
1638 Imp_Asp => Imp,
1639 LN_Asp => Dummy_3,
1640 Do_Checks => True);
1641
1642 -- The related entity cannot be subject to both aspects Export
1643 -- and Import.
1644
1645 if Present (Expo) and then Present (Imp) then
1646 Error_Msg_N
1647 ("incompatible interfacing aspects given for &", E);
1648 Error_Msg_Sloc := Sloc (Expo);
1649 Error_Msg_N ("\aspect `Export` #", E);
1650 Error_Msg_Sloc := Sloc (Imp);
1651 Error_Msg_N ("\aspect `Import` #", E);
1652 end if;
1653
1654 -- A variable is most likely modified from the outside. Take
1655 -- Take the optimistic approach to avoid spurious errors.
1656
1657 if Ekind (E) = E_Variable then
1658 Set_Never_Set_In_Source (E, False);
1659 end if;
1660
1661 -- Resolve the expression of an Import or Export here, and
1662 -- require it to be of type Boolean and static. This is not
1663 -- quite right, because in general this should be delayed,
1664 -- but that seems tricky for these, because normally Boolean
1665 -- aspects are replaced with pragmas at the freeze point in
1666 -- Make_Pragma_From_Boolean_Aspect.
1667
1668 if not Present (Expr)
1669 or else Is_True (Static_Boolean (Expr))
1670 then
1671 if A_Id = Aspect_Import then
1672 Set_Has_Completion (E);
1673 Set_Is_Imported (E);
1674
1675 -- An imported object cannot be explicitly initialized
1676
1677 if Nkind (N) = N_Object_Declaration
1678 and then Present (Expression (N))
1679 then
1680 Error_Msg_N
1681 ("imported entities cannot be initialized "
1682 & "(RM B.1(24))", Expression (N));
1683 end if;
1684
1685 else
1686 pragma Assert (A_Id = Aspect_Export);
1687 Set_Is_Exported (E);
1688 end if;
1689
1690 -- Create the proper form of pragma Export or Import taking
1691 -- into account Conversion, External_Name, and Link_Name.
1692
1693 Aitem := Build_Export_Import_Pragma (Aspect, E);
1694
1695 -- Otherwise the expression is either False or erroneous. There
1696 -- is no corresponding pragma.
1697
1698 else
1699 Aitem := Empty;
1700 end if;
1701 end Analyze_Aspect_Export_Import;
1702
1703 ---------------------------------------
1704 -- Analyze_Aspect_External_Link_Name --
1705 ---------------------------------------
1706
1707 procedure Analyze_Aspect_External_Link_Name is
1708 Dummy_1 : Node_Id;
1709 Dummy_2 : Node_Id;
1710 Dummy_3 : Node_Id;
1711 Expo : Node_Id;
1712 Imp : Node_Id;
1713
1714 begin
1715 -- Obtain all interfacing aspects that apply to the related
1716 -- entity.
1717
1718 Get_Interfacing_Aspects
1719 (Iface_Asp => Aspect,
1720 Conv_Asp => Dummy_1,
1721 EN_Asp => Dummy_2,
1722 Expo_Asp => Expo,
1723 Imp_Asp => Imp,
1724 LN_Asp => Dummy_3,
1725 Do_Checks => True);
1726
1727 -- Ensure that aspect External_Name applies to aspect Export or
1728 -- Import.
1729
1730 if A_Id = Aspect_External_Name then
1731 if No (Expo) and then No (Imp) then
1732 Error_Msg_N
1733 ("aspect `External_Name` requires aspect `Import` or "
1734 & "`Export`", Aspect);
1735 end if;
1736
1737 -- Otherwise ensure that aspect Link_Name applies to aspect
1738 -- Export or Import.
1739
1740 else
1741 pragma Assert (A_Id = Aspect_Link_Name);
1742 if No (Expo) and then No (Imp) then
1743 Error_Msg_N
1744 ("aspect `Link_Name` requires aspect `Import` or "
1745 & "`Export`", Aspect);
1746 end if;
1747 end if;
1748 end Analyze_Aspect_External_Link_Name;
1749
1750 -----------------------------------------
1751 -- Analyze_Aspect_Implicit_Dereference --
1752 -----------------------------------------
1753
1754 procedure Analyze_Aspect_Implicit_Dereference is
1755 Disc : Entity_Id;
1756 Parent_Disc : Entity_Id;
1757
1758 begin
1759 if not Is_Type (E) or else not Has_Discriminants (E) then
1760 Error_Msg_N
1761 ("aspect must apply to a type with discriminants", Expr);
1762
1763 elsif not Is_Entity_Name (Expr) then
1764 Error_Msg_N
1765 ("aspect must name a discriminant of current type", Expr);
1766
1767 else
1768 Disc := First_Discriminant (E);
1769 while Present (Disc) loop
1770 if Chars (Expr) = Chars (Disc)
1771 and then Ekind (Etype (Disc)) =
1772 E_Anonymous_Access_Type
1773 then
1774 Set_Has_Implicit_Dereference (E);
1775 Set_Has_Implicit_Dereference (Disc);
1776 exit;
1777 end if;
1778
1779 Next_Discriminant (Disc);
1780 end loop;
1781
1782 -- Error if no proper access discriminant
1783
1784 if No (Disc) then
1785 Error_Msg_NE ("not an access discriminant of&", Expr, E);
1786 return;
1787 end if;
1788 end if;
1789
1790 -- For a type extension, check whether parent has a
1791 -- reference discriminant, to verify that use is proper.
1792
1793 if Is_Derived_Type (E)
1794 and then Has_Discriminants (Etype (E))
1795 then
1796 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1797
1798 if Present (Parent_Disc)
1799 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1800 then
1801 Error_Msg_N
1802 ("reference discriminant does not match discriminant "
1803 & "of parent type", Expr);
1804 end if;
1805 end if;
1806 end Analyze_Aspect_Implicit_Dereference;
1807
1808 -----------------------
1809 -- Make_Aitem_Pragma --
1810 -----------------------
1811
1812 procedure Make_Aitem_Pragma
1813 (Pragma_Argument_Associations : List_Id;
1814 Pragma_Name : Name_Id)
1815 is
1816 Args : List_Id := Pragma_Argument_Associations;
1817
1818 begin
1819 -- We should never get here if aspect was disabled
1820
1821 pragma Assert (not Is_Disabled (Aspect));
1822
1823 -- Certain aspects allow for an optional name or expression. Do
1824 -- not generate a pragma with empty argument association list.
1825
1826 if No (Args) or else No (Expression (First (Args))) then
1827 Args := No_List;
1828 end if;
1829
1830 -- Build the pragma
1831
1832 Aitem :=
1833 Make_Pragma (Loc,
1834 Pragma_Argument_Associations => Args,
1835 Pragma_Identifier =>
1836 Make_Identifier (Sloc (Id), Pragma_Name),
1837 Class_Present => Class_Present (Aspect),
1838 Split_PPC => Split_PPC (Aspect));
1839
1840 -- Set additional semantic fields
1841
1842 if Is_Ignored (Aspect) then
1843 Set_Is_Ignored (Aitem);
1844 elsif Is_Checked (Aspect) then
1845 Set_Is_Checked (Aitem);
1846 end if;
1847
1848 Set_Corresponding_Aspect (Aitem, Aspect);
1849 Set_From_Aspect_Specification (Aitem);
1850 end Make_Aitem_Pragma;
1851
1852 -- Start of processing for Analyze_One_Aspect
1853
1854 begin
1855 -- Skip aspect if already analyzed, to avoid looping in some cases
1856
1857 if Analyzed (Aspect) then
1858 goto Continue;
1859 end if;
1860
1861 -- Skip looking at aspect if it is totally disabled. Just mark it
1862 -- as such for later reference in the tree. This also sets the
1863 -- Is_Ignored and Is_Checked flags appropriately.
1864
1865 Check_Applicable_Policy (Aspect);
1866
1867 if Is_Disabled (Aspect) then
1868 goto Continue;
1869 end if;
1870
1871 -- Set the source location of expression, used in the case of
1872 -- a failed precondition/postcondition or invariant. Note that
1873 -- the source location of the expression is not usually the best
1874 -- choice here. For example, it gets located on the last AND
1875 -- keyword in a chain of boolean expressiond AND'ed together.
1876 -- It is best to put the message on the first character of the
1877 -- assertion, which is the effect of the First_Node call here.
1878
1879 if Present (Expr) then
1880 Eloc := Sloc (First_Node (Expr));
1881 end if;
1882
1883 -- Check restriction No_Implementation_Aspect_Specifications
1884
1885 if Implementation_Defined_Aspect (A_Id) then
1886 Check_Restriction
1887 (No_Implementation_Aspect_Specifications, Aspect);
1888 end if;
1889
1890 -- Check restriction No_Specification_Of_Aspect
1891
1892 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1893
1894 -- Mark aspect analyzed (actual analysis is delayed till later)
1895
1896 Set_Analyzed (Aspect);
1897 Set_Entity (Aspect, E);
1898 Ent := New_Occurrence_Of (E, Sloc (Id));
1899
1900 -- Check for duplicate aspect. Note that the Comes_From_Source
1901 -- test allows duplicate Pre/Post's that we generate internally
1902 -- to escape being flagged here.
1903
1904 if No_Duplicates_Allowed (A_Id) then
1905 Anod := First (L);
1906 while Anod /= Aspect loop
1907 if Comes_From_Source (Aspect)
1908 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1909 then
1910 Error_Msg_Name_1 := Nam;
1911 Error_Msg_Sloc := Sloc (Anod);
1912
1913 -- Case of same aspect specified twice
1914
1915 if Class_Present (Anod) = Class_Present (Aspect) then
1916 if not Class_Present (Anod) then
1917 Error_Msg_NE
1918 ("aspect% for & previously given#",
1919 Id, E);
1920 else
1921 Error_Msg_NE
1922 ("aspect `%''Class` for & previously given#",
1923 Id, E);
1924 end if;
1925 end if;
1926 end if;
1927
1928 Next (Anod);
1929 end loop;
1930 end if;
1931
1932 -- Check some general restrictions on language defined aspects
1933
1934 if not Implementation_Defined_Aspect (A_Id) then
1935 Error_Msg_Name_1 := Nam;
1936
1937 -- Not allowed for renaming declarations
1938
1939 if Nkind (N) in N_Renaming_Declaration then
1940 Error_Msg_N
1941 ("aspect % not allowed for renaming declaration",
1942 Aspect);
1943 end if;
1944
1945 -- Not allowed for formal type declarations
1946
1947 if Nkind (N) = N_Formal_Type_Declaration then
1948 Error_Msg_N
1949 ("aspect % not allowed for formal type declaration",
1950 Aspect);
1951 end if;
1952 end if;
1953
1954 -- Copy expression for later processing by the procedures
1955 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1956
1957 Set_Entity (Id, New_Copy_Tree (Expr));
1958
1959 -- Set Delay_Required as appropriate to aspect
1960
1961 case Aspect_Delay (A_Id) is
1962 when Always_Delay =>
1963 Delay_Required := True;
1964
1965 when Never_Delay =>
1966 Delay_Required := False;
1967
1968 when Rep_Aspect =>
1969
1970 -- If expression has the form of an integer literal, then
1971 -- do not delay, since we know the value cannot change.
1972 -- This optimization catches most rep clause cases.
1973
1974 -- For Boolean aspects, don't delay if no expression
1975
1976 if A_Id in Boolean_Aspects and then No (Expr) then
1977 Delay_Required := False;
1978
1979 -- For non-Boolean aspects, don't delay if integer literal
1980
1981 elsif A_Id not in Boolean_Aspects
1982 and then Present (Expr)
1983 and then Nkind (Expr) = N_Integer_Literal
1984 then
1985 Delay_Required := False;
1986
1987 -- All other cases are delayed
1988
1989 else
1990 Delay_Required := True;
1991 Set_Has_Delayed_Rep_Aspects (E);
1992 end if;
1993 end case;
1994
1995 -- Processing based on specific aspect
1996
1997 case A_Id is
1998 when Aspect_Unimplemented =>
1999 null; -- ??? temp for now
2000
2001 -- No_Aspect should be impossible
2002
2003 when No_Aspect =>
2004 raise Program_Error;
2005
2006 -- Case 1: Aspects corresponding to attribute definition
2007 -- clauses.
2008
2009 when Aspect_Address |
2010 Aspect_Alignment |
2011 Aspect_Bit_Order |
2012 Aspect_Component_Size |
2013 Aspect_Constant_Indexing |
2014 Aspect_Default_Iterator |
2015 Aspect_Dispatching_Domain |
2016 Aspect_External_Tag |
2017 Aspect_Input |
2018 Aspect_Iterable |
2019 Aspect_Iterator_Element |
2020 Aspect_Machine_Radix |
2021 Aspect_Object_Size |
2022 Aspect_Output |
2023 Aspect_Read |
2024 Aspect_Scalar_Storage_Order |
2025 Aspect_Size |
2026 Aspect_Small |
2027 Aspect_Simple_Storage_Pool |
2028 Aspect_Storage_Pool |
2029 Aspect_Stream_Size |
2030 Aspect_Value_Size |
2031 Aspect_Variable_Indexing |
2032 Aspect_Write =>
2033
2034 -- Indexing aspects apply only to tagged type
2035
2036 if (A_Id = Aspect_Constant_Indexing
2037 or else
2038 A_Id = Aspect_Variable_Indexing)
2039 and then not (Is_Type (E)
2040 and then Is_Tagged_Type (E))
2041 then
2042 Error_Msg_N
2043 ("indexing aspect can only apply to a tagged type",
2044 Aspect);
2045 goto Continue;
2046 end if;
2047
2048 -- For the case of aspect Address, we don't consider that we
2049 -- know the entity is never set in the source, since it is
2050 -- is likely aliasing is occurring.
2051
2052 -- Note: one might think that the analysis of the resulting
2053 -- attribute definition clause would take care of that, but
2054 -- that's not the case since it won't be from source.
2055
2056 if A_Id = Aspect_Address then
2057 Set_Never_Set_In_Source (E, False);
2058 end if;
2059
2060 -- Correctness of the profile of a stream operation is
2061 -- verified at the freeze point, but we must detect the
2062 -- illegal specification of this aspect for a subtype now,
2063 -- to prevent malformed rep_item chains.
2064
2065 if A_Id = Aspect_Input or else
2066 A_Id = Aspect_Output or else
2067 A_Id = Aspect_Read or else
2068 A_Id = Aspect_Write
2069 then
2070 if not Is_First_Subtype (E) then
2071 Error_Msg_N
2072 ("local name must be a first subtype", Aspect);
2073 goto Continue;
2074
2075 -- If stream aspect applies to the class-wide type,
2076 -- the generated attribute definition applies to the
2077 -- class-wide type as well.
2078
2079 elsif Class_Present (Aspect) then
2080 Ent :=
2081 Make_Attribute_Reference (Loc,
2082 Prefix => Ent,
2083 Attribute_Name => Name_Class);
2084 end if;
2085 end if;
2086
2087 -- Construct the attribute definition clause
2088
2089 Aitem :=
2090 Make_Attribute_Definition_Clause (Loc,
2091 Name => Ent,
2092 Chars => Chars (Id),
2093 Expression => Relocate_Node (Expr));
2094
2095 -- If the address is specified, then we treat the entity as
2096 -- referenced, to avoid spurious warnings. This is analogous
2097 -- to what is done with an attribute definition clause, but
2098 -- here we don't want to generate a reference because this
2099 -- is the point of definition of the entity.
2100
2101 if A_Id = Aspect_Address then
2102 Set_Referenced (E);
2103 end if;
2104
2105 -- Case 2: Aspects corresponding to pragmas
2106
2107 -- Case 2a: Aspects corresponding to pragmas with two
2108 -- arguments, where the first argument is a local name
2109 -- referring to the entity, and the second argument is the
2110 -- aspect definition expression.
2111
2112 -- Linker_Section/Suppress/Unsuppress
2113
2114 when Aspect_Linker_Section |
2115 Aspect_Suppress |
2116 Aspect_Unsuppress =>
2117
2118 Make_Aitem_Pragma
2119 (Pragma_Argument_Associations => New_List (
2120 Make_Pragma_Argument_Association (Loc,
2121 Expression => New_Occurrence_Of (E, Loc)),
2122 Make_Pragma_Argument_Association (Sloc (Expr),
2123 Expression => Relocate_Node (Expr))),
2124 Pragma_Name => Chars (Id));
2125
2126 -- Synchronization
2127
2128 -- Corresponds to pragma Implemented, construct the pragma
2129
2130 when Aspect_Synchronization =>
2131 Make_Aitem_Pragma
2132 (Pragma_Argument_Associations => New_List (
2133 Make_Pragma_Argument_Association (Loc,
2134 Expression => New_Occurrence_Of (E, Loc)),
2135 Make_Pragma_Argument_Association (Sloc (Expr),
2136 Expression => Relocate_Node (Expr))),
2137 Pragma_Name => Name_Implemented);
2138
2139 -- Attach_Handler
2140
2141 when Aspect_Attach_Handler =>
2142 Make_Aitem_Pragma
2143 (Pragma_Argument_Associations => New_List (
2144 Make_Pragma_Argument_Association (Sloc (Ent),
2145 Expression => Ent),
2146 Make_Pragma_Argument_Association (Sloc (Expr),
2147 Expression => Relocate_Node (Expr))),
2148 Pragma_Name => Name_Attach_Handler);
2149
2150 -- We need to insert this pragma into the tree to get proper
2151 -- processing and to look valid from a placement viewpoint.
2152
2153 Insert_Pragma (Aitem);
2154 goto Continue;
2155
2156 -- Dynamic_Predicate, Predicate, Static_Predicate
2157
2158 when Aspect_Dynamic_Predicate |
2159 Aspect_Predicate |
2160 Aspect_Static_Predicate =>
2161
2162 -- These aspects apply only to subtypes
2163
2164 if not Is_Type (E) then
2165 Error_Msg_N
2166 ("predicate can only be specified for a subtype",
2167 Aspect);
2168 goto Continue;
2169
2170 elsif Is_Incomplete_Type (E) then
2171 Error_Msg_N
2172 ("predicate cannot apply to incomplete view", Aspect);
2173 goto Continue;
2174 end if;
2175
2176 -- Construct the pragma (always a pragma Predicate, with
2177 -- flags recording whether it is static/dynamic). We also
2178 -- set flags recording this in the type itself.
2179
2180 Make_Aitem_Pragma
2181 (Pragma_Argument_Associations => New_List (
2182 Make_Pragma_Argument_Association (Sloc (Ent),
2183 Expression => Ent),
2184 Make_Pragma_Argument_Association (Sloc (Expr),
2185 Expression => Relocate_Node (Expr))),
2186 Pragma_Name => Name_Predicate);
2187
2188 -- Mark type has predicates, and remember what kind of
2189 -- aspect lead to this predicate (we need this to access
2190 -- the right set of check policies later on).
2191
2192 Set_Has_Predicates (E);
2193
2194 if A_Id = Aspect_Dynamic_Predicate then
2195 Set_Has_Dynamic_Predicate_Aspect (E);
2196 elsif A_Id = Aspect_Static_Predicate then
2197 Set_Has_Static_Predicate_Aspect (E);
2198 end if;
2199
2200 -- If the type is private, indicate that its completion
2201 -- has a freeze node, because that is the one that will
2202 -- be visible at freeze time.
2203
2204 if Is_Private_Type (E) and then Present (Full_View (E)) then
2205 Set_Has_Predicates (Full_View (E));
2206
2207 if A_Id = Aspect_Dynamic_Predicate then
2208 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2209 elsif A_Id = Aspect_Static_Predicate then
2210 Set_Has_Static_Predicate_Aspect (Full_View (E));
2211 end if;
2212
2213 Set_Has_Delayed_Aspects (Full_View (E));
2214 Ensure_Freeze_Node (Full_View (E));
2215 end if;
2216
2217 -- Predicate_Failure
2218
2219 when Aspect_Predicate_Failure =>
2220
2221 -- This aspect applies only to subtypes
2222
2223 if not Is_Type (E) then
2224 Error_Msg_N
2225 ("predicate can only be specified for a subtype",
2226 Aspect);
2227 goto Continue;
2228
2229 elsif Is_Incomplete_Type (E) then
2230 Error_Msg_N
2231 ("predicate cannot apply to incomplete view", Aspect);
2232 goto Continue;
2233 end if;
2234
2235 -- Construct the pragma
2236
2237 Make_Aitem_Pragma
2238 (Pragma_Argument_Associations => New_List (
2239 Make_Pragma_Argument_Association (Sloc (Ent),
2240 Expression => Ent),
2241 Make_Pragma_Argument_Association (Sloc (Expr),
2242 Expression => Relocate_Node (Expr))),
2243 Pragma_Name => Name_Predicate_Failure);
2244
2245 Set_Has_Predicates (E);
2246
2247 -- If the type is private, indicate that its completion
2248 -- has a freeze node, because that is the one that will
2249 -- be visible at freeze time.
2250
2251 if Is_Private_Type (E) and then Present (Full_View (E)) then
2252 Set_Has_Predicates (Full_View (E));
2253 Set_Has_Delayed_Aspects (Full_View (E));
2254 Ensure_Freeze_Node (Full_View (E));
2255 end if;
2256
2257 -- Case 2b: Aspects corresponding to pragmas with two
2258 -- arguments, where the second argument is a local name
2259 -- referring to the entity, and the first argument is the
2260 -- aspect definition expression.
2261
2262 -- Convention
2263
2264 when Aspect_Convention =>
2265 Analyze_Aspect_Convention;
2266 goto Continue;
2267
2268 -- External_Name, Link_Name
2269
2270 when Aspect_External_Name |
2271 Aspect_Link_Name =>
2272 Analyze_Aspect_External_Link_Name;
2273 goto Continue;
2274
2275 -- CPU, Interrupt_Priority, Priority
2276
2277 -- These three aspects can be specified for a subprogram spec
2278 -- or body, in which case we analyze the expression and export
2279 -- the value of the aspect.
2280
2281 -- Previously, we generated an equivalent pragma for bodies
2282 -- (note that the specs cannot contain these pragmas). The
2283 -- pragma was inserted ahead of local declarations, rather than
2284 -- after the body. This leads to a certain duplication between
2285 -- the processing performed for the aspect and the pragma, but
2286 -- given the straightforward handling required it is simpler
2287 -- to duplicate than to translate the aspect in the spec into
2288 -- a pragma in the declarative part of the body.
2289
2290 when Aspect_CPU |
2291 Aspect_Interrupt_Priority |
2292 Aspect_Priority =>
2293
2294 if Nkind_In (N, N_Subprogram_Body,
2295 N_Subprogram_Declaration)
2296 then
2297 -- Analyze the aspect expression
2298
2299 Analyze_And_Resolve (Expr, Standard_Integer);
2300
2301 -- Interrupt_Priority aspect not allowed for main
2302 -- subprograms. RM D.1 does not forbid this explicitly,
2303 -- but RM J.15.11(6/3) does not permit pragma
2304 -- Interrupt_Priority for subprograms.
2305
2306 if A_Id = Aspect_Interrupt_Priority then
2307 Error_Msg_N
2308 ("Interrupt_Priority aspect cannot apply to "
2309 & "subprogram", Expr);
2310
2311 -- The expression must be static
2312
2313 elsif not Is_OK_Static_Expression (Expr) then
2314 Flag_Non_Static_Expr
2315 ("aspect requires static expression!", Expr);
2316
2317 -- Check whether this is the main subprogram. Issue a
2318 -- warning only if it is obviously not a main program
2319 -- (when it has parameters or when the subprogram is
2320 -- within a package).
2321
2322 elsif Present (Parameter_Specifications
2323 (Specification (N)))
2324 or else not Is_Compilation_Unit (Defining_Entity (N))
2325 then
2326 -- See RM D.1(14/3) and D.16(12/3)
2327
2328 Error_Msg_N
2329 ("aspect applied to subprogram other than the "
2330 & "main subprogram has no effect??", Expr);
2331
2332 -- Otherwise check in range and export the value
2333
2334 -- For the CPU aspect
2335
2336 elsif A_Id = Aspect_CPU then
2337 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2338
2339 -- Value is correct so we export the value to make
2340 -- it available at execution time.
2341
2342 Set_Main_CPU
2343 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2344
2345 else
2346 Error_Msg_N
2347 ("main subprogram CPU is out of range", Expr);
2348 end if;
2349
2350 -- For the Priority aspect
2351
2352 elsif A_Id = Aspect_Priority then
2353 if Is_In_Range (Expr, RTE (RE_Priority)) then
2354
2355 -- Value is correct so we export the value to make
2356 -- it available at execution time.
2357
2358 Set_Main_Priority
2359 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2360
2361 -- Ignore pragma if Relaxed_RM_Semantics to support
2362 -- other targets/non GNAT compilers.
2363
2364 elsif not Relaxed_RM_Semantics then
2365 Error_Msg_N
2366 ("main subprogram priority is out of range",
2367 Expr);
2368 end if;
2369 end if;
2370
2371 -- Load an arbitrary entity from System.Tasking.Stages
2372 -- or System.Tasking.Restricted.Stages (depending on
2373 -- the supported profile) to make sure that one of these
2374 -- packages is implicitly with'ed, since we need to have
2375 -- the tasking run time active for the pragma Priority to
2376 -- have any effect. Previously we with'ed the package
2377 -- System.Tasking, but this package does not trigger the
2378 -- required initialization of the run-time library.
2379
2380 declare
2381 Discard : Entity_Id;
2382 begin
2383 if Restricted_Profile then
2384 Discard := RTE (RE_Activate_Restricted_Tasks);
2385 else
2386 Discard := RTE (RE_Activate_Tasks);
2387 end if;
2388 end;
2389
2390 -- Handling for these Aspects in subprograms is complete
2391
2392 goto Continue;
2393
2394 -- For tasks pass the aspect as an attribute
2395
2396 else
2397 Aitem :=
2398 Make_Attribute_Definition_Clause (Loc,
2399 Name => Ent,
2400 Chars => Chars (Id),
2401 Expression => Relocate_Node (Expr));
2402 end if;
2403
2404 -- Warnings
2405
2406 when Aspect_Warnings =>
2407 Make_Aitem_Pragma
2408 (Pragma_Argument_Associations => New_List (
2409 Make_Pragma_Argument_Association (Sloc (Expr),
2410 Expression => Relocate_Node (Expr)),
2411 Make_Pragma_Argument_Association (Loc,
2412 Expression => New_Occurrence_Of (E, Loc))),
2413 Pragma_Name => Chars (Id));
2414
2415 Decorate (Aspect, Aitem);
2416 Insert_Pragma (Aitem);
2417 goto Continue;
2418
2419 -- Case 2c: Aspects corresponding to pragmas with three
2420 -- arguments.
2421
2422 -- Invariant aspects have a first argument that references the
2423 -- entity, a second argument that is the expression and a third
2424 -- argument that is an appropriate message.
2425
2426 -- Invariant, Type_Invariant
2427
2428 when Aspect_Invariant |
2429 Aspect_Type_Invariant =>
2430
2431 -- Analysis of the pragma will verify placement legality:
2432 -- an invariant must apply to a private type, or appear in
2433 -- the private part of a spec and apply to a completion.
2434
2435 Make_Aitem_Pragma
2436 (Pragma_Argument_Associations => New_List (
2437 Make_Pragma_Argument_Association (Sloc (Ent),
2438 Expression => Ent),
2439 Make_Pragma_Argument_Association (Sloc (Expr),
2440 Expression => Relocate_Node (Expr))),
2441 Pragma_Name => Name_Invariant);
2442
2443 -- Add message unless exception messages are suppressed
2444
2445 if not Opt.Exception_Locations_Suppressed then
2446 Append_To (Pragma_Argument_Associations (Aitem),
2447 Make_Pragma_Argument_Association (Eloc,
2448 Chars => Name_Message,
2449 Expression =>
2450 Make_String_Literal (Eloc,
2451 Strval => "failed invariant from "
2452 & Build_Location_String (Eloc))));
2453 end if;
2454
2455 -- For Invariant case, insert immediately after the entity
2456 -- declaration. We do not have to worry about delay issues
2457 -- since the pragma processing takes care of this.
2458
2459 Delay_Required := False;
2460
2461 -- Case 2d : Aspects that correspond to a pragma with one
2462 -- argument.
2463
2464 -- Abstract_State
2465
2466 -- Aspect Abstract_State introduces implicit declarations for
2467 -- all state abstraction entities it defines. To emulate this
2468 -- behavior, insert the pragma at the beginning of the visible
2469 -- declarations of the related package so that it is analyzed
2470 -- immediately.
2471
2472 when Aspect_Abstract_State => Abstract_State : declare
2473 Context : Node_Id := N;
2474
2475 begin
2476 -- When aspect Abstract_State appears on a generic package,
2477 -- it is propageted to the package instance. The context in
2478 -- this case is the instance spec.
2479
2480 if Nkind (Context) = N_Package_Instantiation then
2481 Context := Instance_Spec (Context);
2482 end if;
2483
2484 if Nkind_In (Context, N_Generic_Package_Declaration,
2485 N_Package_Declaration)
2486 then
2487 Make_Aitem_Pragma
2488 (Pragma_Argument_Associations => New_List (
2489 Make_Pragma_Argument_Association (Loc,
2490 Expression => Relocate_Node (Expr))),
2491 Pragma_Name => Name_Abstract_State);
2492
2493 Decorate (Aspect, Aitem);
2494 Insert_Pragma
2495 (Prag => Aitem,
2496 Is_Instance =>
2497 Is_Generic_Instance (Defining_Entity (Context)));
2498
2499 else
2500 Error_Msg_NE
2501 ("aspect & must apply to a package declaration",
2502 Aspect, Id);
2503 end if;
2504
2505 goto Continue;
2506 end Abstract_State;
2507
2508 -- Aspect Async_Readers is never delayed because it is
2509 -- equivalent to a source pragma which appears after the
2510 -- related object declaration.
2511
2512 when Aspect_Async_Readers =>
2513 Make_Aitem_Pragma
2514 (Pragma_Argument_Associations => New_List (
2515 Make_Pragma_Argument_Association (Loc,
2516 Expression => Relocate_Node (Expr))),
2517 Pragma_Name => Name_Async_Readers);
2518
2519 Decorate (Aspect, Aitem);
2520 Insert_Pragma (Aitem);
2521 goto Continue;
2522
2523 -- Aspect Async_Writers is never delayed because it is
2524 -- equivalent to a source pragma which appears after the
2525 -- related object declaration.
2526
2527 when Aspect_Async_Writers =>
2528 Make_Aitem_Pragma
2529 (Pragma_Argument_Associations => New_List (
2530 Make_Pragma_Argument_Association (Loc,
2531 Expression => Relocate_Node (Expr))),
2532 Pragma_Name => Name_Async_Writers);
2533
2534 Decorate (Aspect, Aitem);
2535 Insert_Pragma (Aitem);
2536 goto Continue;
2537
2538 -- Aspect Constant_After_Elaboration is never delayed because
2539 -- it is equivalent to a source pragma which appears after the
2540 -- related object declaration.
2541
2542 when Aspect_Constant_After_Elaboration =>
2543 Make_Aitem_Pragma
2544 (Pragma_Argument_Associations => New_List (
2545 Make_Pragma_Argument_Association (Loc,
2546 Expression => Relocate_Node (Expr))),
2547 Pragma_Name =>
2548 Name_Constant_After_Elaboration);
2549
2550 Decorate (Aspect, Aitem);
2551 Insert_Pragma (Aitem);
2552 goto Continue;
2553
2554 -- Aspect Default_Internal_Condition is never delayed because
2555 -- it is equivalent to a source pragma which appears after the
2556 -- related private type. To deal with forward references, the
2557 -- generated pragma is stored in the rep chain of the related
2558 -- private type as types do not carry contracts. The pragma is
2559 -- wrapped inside of a procedure at the freeze point of the
2560 -- private type's full view.
2561
2562 when Aspect_Default_Initial_Condition =>
2563 Make_Aitem_Pragma
2564 (Pragma_Argument_Associations => New_List (
2565 Make_Pragma_Argument_Association (Loc,
2566 Expression => Relocate_Node (Expr))),
2567 Pragma_Name =>
2568 Name_Default_Initial_Condition);
2569
2570 Decorate (Aspect, Aitem);
2571 Insert_Pragma (Aitem);
2572 goto Continue;
2573
2574 -- Default_Storage_Pool
2575
2576 when Aspect_Default_Storage_Pool =>
2577 Make_Aitem_Pragma
2578 (Pragma_Argument_Associations => New_List (
2579 Make_Pragma_Argument_Association (Loc,
2580 Expression => Relocate_Node (Expr))),
2581 Pragma_Name =>
2582 Name_Default_Storage_Pool);
2583
2584 Decorate (Aspect, Aitem);
2585 Insert_Pragma (Aitem);
2586 goto Continue;
2587
2588 -- Depends
2589
2590 -- Aspect Depends is never delayed because it is equivalent to
2591 -- a source pragma which appears after the related subprogram.
2592 -- To deal with forward references, the generated pragma is
2593 -- stored in the contract of the related subprogram and later
2594 -- analyzed at the end of the declarative region. See routine
2595 -- Analyze_Depends_In_Decl_Part for details.
2596
2597 when Aspect_Depends =>
2598 Make_Aitem_Pragma
2599 (Pragma_Argument_Associations => New_List (
2600 Make_Pragma_Argument_Association (Loc,
2601 Expression => Relocate_Node (Expr))),
2602 Pragma_Name => Name_Depends);
2603
2604 Decorate (Aspect, Aitem);
2605 Insert_Pragma (Aitem);
2606 goto Continue;
2607
2608 -- Aspect Effecitve_Reads is never delayed because it is
2609 -- equivalent to a source pragma which appears after the
2610 -- related object declaration.
2611
2612 when Aspect_Effective_Reads =>
2613 Make_Aitem_Pragma
2614 (Pragma_Argument_Associations => New_List (
2615 Make_Pragma_Argument_Association (Loc,
2616 Expression => Relocate_Node (Expr))),
2617 Pragma_Name => Name_Effective_Reads);
2618
2619 Decorate (Aspect, Aitem);
2620 Insert_Pragma (Aitem);
2621 goto Continue;
2622
2623 -- Aspect Effective_Writes is never delayed because it is
2624 -- equivalent to a source pragma which appears after the
2625 -- related object declaration.
2626
2627 when Aspect_Effective_Writes =>
2628 Make_Aitem_Pragma
2629 (Pragma_Argument_Associations => New_List (
2630 Make_Pragma_Argument_Association (Loc,
2631 Expression => Relocate_Node (Expr))),
2632 Pragma_Name => Name_Effective_Writes);
2633
2634 Decorate (Aspect, Aitem);
2635 Insert_Pragma (Aitem);
2636 goto Continue;
2637
2638 -- Aspect Extensions_Visible is never delayed because it is
2639 -- equivalent to a source pragma which appears after the
2640 -- related subprogram.
2641
2642 when Aspect_Extensions_Visible =>
2643 Make_Aitem_Pragma
2644 (Pragma_Argument_Associations => New_List (
2645 Make_Pragma_Argument_Association (Loc,
2646 Expression => Relocate_Node (Expr))),
2647 Pragma_Name => Name_Extensions_Visible);
2648
2649 Decorate (Aspect, Aitem);
2650 Insert_Pragma (Aitem);
2651 goto Continue;
2652
2653 -- Aspect Ghost is never delayed because it is equivalent to a
2654 -- source pragma which appears at the top of [generic] package
2655 -- declarations or after an object, a [generic] subprogram, or
2656 -- a type declaration.
2657
2658 when Aspect_Ghost =>
2659 Make_Aitem_Pragma
2660 (Pragma_Argument_Associations => New_List (
2661 Make_Pragma_Argument_Association (Loc,
2662 Expression => Relocate_Node (Expr))),
2663 Pragma_Name => Name_Ghost);
2664
2665 Decorate (Aspect, Aitem);
2666 Insert_Pragma (Aitem);
2667 goto Continue;
2668
2669 -- Global
2670
2671 -- Aspect Global is never delayed because it is equivalent to
2672 -- a source pragma which appears after the related subprogram.
2673 -- To deal with forward references, the generated pragma is
2674 -- stored in the contract of the related subprogram and later
2675 -- analyzed at the end of the declarative region. See routine
2676 -- Analyze_Global_In_Decl_Part for details.
2677
2678 when Aspect_Global =>
2679 Make_Aitem_Pragma
2680 (Pragma_Argument_Associations => New_List (
2681 Make_Pragma_Argument_Association (Loc,
2682 Expression => Relocate_Node (Expr))),
2683 Pragma_Name => Name_Global);
2684
2685 Decorate (Aspect, Aitem);
2686 Insert_Pragma (Aitem);
2687 goto Continue;
2688
2689 -- Initial_Condition
2690
2691 -- Aspect Initial_Condition is never delayed because it is
2692 -- equivalent to a source pragma which appears after the
2693 -- related package. To deal with forward references, the
2694 -- generated pragma is stored in the contract of the related
2695 -- package and later analyzed at the end of the declarative
2696 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2697 -- for details.
2698
2699 when Aspect_Initial_Condition => Initial_Condition : declare
2700 Context : Node_Id := N;
2701
2702 begin
2703 -- When aspect Initial_Condition appears on a generic
2704 -- package, it is propageted to the package instance. The
2705 -- context in this case is the instance spec.
2706
2707 if Nkind (Context) = N_Package_Instantiation then
2708 Context := Instance_Spec (Context);
2709 end if;
2710
2711 if Nkind_In (Context, N_Generic_Package_Declaration,
2712 N_Package_Declaration)
2713 then
2714 Make_Aitem_Pragma
2715 (Pragma_Argument_Associations => New_List (
2716 Make_Pragma_Argument_Association (Loc,
2717 Expression => Relocate_Node (Expr))),
2718 Pragma_Name =>
2719 Name_Initial_Condition);
2720
2721 Decorate (Aspect, Aitem);
2722 Insert_Pragma
2723 (Prag => Aitem,
2724 Is_Instance =>
2725 Is_Generic_Instance (Defining_Entity (Context)));
2726
2727 -- Otherwise the context is illegal
2728
2729 else
2730 Error_Msg_NE
2731 ("aspect & must apply to a package declaration",
2732 Aspect, Id);
2733 end if;
2734
2735 goto Continue;
2736 end Initial_Condition;
2737
2738 -- Initializes
2739
2740 -- Aspect Initializes is never delayed because it is equivalent
2741 -- to a source pragma appearing after the related package. To
2742 -- deal with forward references, the generated pragma is stored
2743 -- in the contract of the related package and later analyzed at
2744 -- the end of the declarative region. For details, see routine
2745 -- Analyze_Initializes_In_Decl_Part.
2746
2747 when Aspect_Initializes => Initializes : declare
2748 Context : Node_Id := N;
2749
2750 begin
2751 -- When aspect Initializes appears on a generic package,
2752 -- it is propageted to the package instance. The context
2753 -- in this case is the instance spec.
2754
2755 if Nkind (Context) = N_Package_Instantiation then
2756 Context := Instance_Spec (Context);
2757 end if;
2758
2759 if Nkind_In (Context, N_Generic_Package_Declaration,
2760 N_Package_Declaration)
2761 then
2762 Make_Aitem_Pragma
2763 (Pragma_Argument_Associations => New_List (
2764 Make_Pragma_Argument_Association (Loc,
2765 Expression => Relocate_Node (Expr))),
2766 Pragma_Name => Name_Initializes);
2767
2768 Decorate (Aspect, Aitem);
2769 Insert_Pragma
2770 (Prag => Aitem,
2771 Is_Instance =>
2772 Is_Generic_Instance (Defining_Entity (Context)));
2773
2774 -- Otherwise the context is illegal
2775
2776 else
2777 Error_Msg_NE
2778 ("aspect & must apply to a package declaration",
2779 Aspect, Id);
2780 end if;
2781
2782 goto Continue;
2783 end Initializes;
2784
2785 -- Obsolescent
2786
2787 when Aspect_Obsolescent => declare
2788 Args : List_Id;
2789
2790 begin
2791 if No (Expr) then
2792 Args := No_List;
2793 else
2794 Args := New_List (
2795 Make_Pragma_Argument_Association (Sloc (Expr),
2796 Expression => Relocate_Node (Expr)));
2797 end if;
2798
2799 Make_Aitem_Pragma
2800 (Pragma_Argument_Associations => Args,
2801 Pragma_Name => Chars (Id));
2802 end;
2803
2804 -- Part_Of
2805
2806 when Aspect_Part_Of =>
2807 if Nkind_In (N, N_Object_Declaration,
2808 N_Package_Instantiation)
2809 or else Is_Single_Concurrent_Type_Declaration (N)
2810 then
2811 Make_Aitem_Pragma
2812 (Pragma_Argument_Associations => New_List (
2813 Make_Pragma_Argument_Association (Loc,
2814 Expression => Relocate_Node (Expr))),
2815 Pragma_Name => Name_Part_Of);
2816
2817 Decorate (Aspect, Aitem);
2818 Insert_Pragma (Aitem);
2819
2820 else
2821 Error_Msg_NE
2822 ("aspect & must apply to package instantiation, "
2823 & "object, single protected type or single task type",
2824 Aspect, Id);
2825 end if;
2826
2827 goto Continue;
2828
2829 -- SPARK_Mode
2830
2831 when Aspect_SPARK_Mode =>
2832 Make_Aitem_Pragma
2833 (Pragma_Argument_Associations => New_List (
2834 Make_Pragma_Argument_Association (Loc,
2835 Expression => Relocate_Node (Expr))),
2836 Pragma_Name => Name_SPARK_Mode);
2837
2838 Decorate (Aspect, Aitem);
2839 Insert_Pragma (Aitem);
2840 goto Continue;
2841
2842 -- Refined_Depends
2843
2844 -- Aspect Refined_Depends is never delayed because it is
2845 -- equivalent to a source pragma which appears in the
2846 -- declarations of the related subprogram body. To deal with
2847 -- forward references, the generated pragma is stored in the
2848 -- contract of the related subprogram body and later analyzed
2849 -- at the end of the declarative region. For details, see
2850 -- routine Analyze_Refined_Depends_In_Decl_Part.
2851
2852 when Aspect_Refined_Depends =>
2853 Make_Aitem_Pragma
2854 (Pragma_Argument_Associations => New_List (
2855 Make_Pragma_Argument_Association (Loc,
2856 Expression => Relocate_Node (Expr))),
2857 Pragma_Name => Name_Refined_Depends);
2858
2859 Decorate (Aspect, Aitem);
2860 Insert_Pragma (Aitem);
2861 goto Continue;
2862
2863 -- Refined_Global
2864
2865 -- Aspect Refined_Global is never delayed because it is
2866 -- equivalent to a source pragma which appears in the
2867 -- declarations of the related subprogram body. To deal with
2868 -- forward references, the generated pragma is stored in the
2869 -- contract of the related subprogram body and later analyzed
2870 -- at the end of the declarative region. For details, see
2871 -- routine Analyze_Refined_Global_In_Decl_Part.
2872
2873 when Aspect_Refined_Global =>
2874 Make_Aitem_Pragma
2875 (Pragma_Argument_Associations => New_List (
2876 Make_Pragma_Argument_Association (Loc,
2877 Expression => Relocate_Node (Expr))),
2878 Pragma_Name => Name_Refined_Global);
2879
2880 Decorate (Aspect, Aitem);
2881 Insert_Pragma (Aitem);
2882 goto Continue;
2883
2884 -- Refined_Post
2885
2886 when Aspect_Refined_Post =>
2887 Make_Aitem_Pragma
2888 (Pragma_Argument_Associations => New_List (
2889 Make_Pragma_Argument_Association (Loc,
2890 Expression => Relocate_Node (Expr))),
2891 Pragma_Name => Name_Refined_Post);
2892
2893 Decorate (Aspect, Aitem);
2894 Insert_Pragma (Aitem);
2895 goto Continue;
2896
2897 -- Refined_State
2898
2899 when Aspect_Refined_State =>
2900
2901 -- The corresponding pragma for Refined_State is inserted in
2902 -- the declarations of the related package body. This action
2903 -- synchronizes both the source and from-aspect versions of
2904 -- the pragma.
2905
2906 if Nkind (N) = N_Package_Body then
2907 Make_Aitem_Pragma
2908 (Pragma_Argument_Associations => New_List (
2909 Make_Pragma_Argument_Association (Loc,
2910 Expression => Relocate_Node (Expr))),
2911 Pragma_Name => Name_Refined_State);
2912
2913 Decorate (Aspect, Aitem);
2914 Insert_Pragma (Aitem);
2915
2916 -- Otherwise the context is illegal
2917
2918 else
2919 Error_Msg_NE
2920 ("aspect & must apply to a package body", Aspect, Id);
2921 end if;
2922
2923 goto Continue;
2924
2925 -- Relative_Deadline
2926
2927 when Aspect_Relative_Deadline =>
2928 Make_Aitem_Pragma
2929 (Pragma_Argument_Associations => New_List (
2930 Make_Pragma_Argument_Association (Loc,
2931 Expression => Relocate_Node (Expr))),
2932 Pragma_Name => Name_Relative_Deadline);
2933
2934 -- If the aspect applies to a task, the corresponding pragma
2935 -- must appear within its declarations, not after.
2936
2937 if Nkind (N) = N_Task_Type_Declaration then
2938 declare
2939 Def : Node_Id;
2940 V : List_Id;
2941
2942 begin
2943 if No (Task_Definition (N)) then
2944 Set_Task_Definition (N,
2945 Make_Task_Definition (Loc,
2946 Visible_Declarations => New_List,
2947 End_Label => Empty));
2948 end if;
2949
2950 Def := Task_Definition (N);
2951 V := Visible_Declarations (Def);
2952 if not Is_Empty_List (V) then
2953 Insert_Before (First (V), Aitem);
2954
2955 else
2956 Set_Visible_Declarations (Def, New_List (Aitem));
2957 end if;
2958
2959 goto Continue;
2960 end;
2961 end if;
2962
2963 -- Aspect Volatile_Function is never delayed because it is
2964 -- equivalent to a source pragma which appears after the
2965 -- related subprogram.
2966
2967 when Aspect_Volatile_Function =>
2968 Make_Aitem_Pragma
2969 (Pragma_Argument_Associations => New_List (
2970 Make_Pragma_Argument_Association (Loc,
2971 Expression => Relocate_Node (Expr))),
2972 Pragma_Name => Name_Volatile_Function);
2973
2974 Decorate (Aspect, Aitem);
2975 Insert_Pragma (Aitem);
2976 goto Continue;
2977
2978 -- Case 2e: Annotate aspect
2979
2980 when Aspect_Annotate =>
2981 declare
2982 Args : List_Id;
2983 Pargs : List_Id;
2984 Arg : Node_Id;
2985
2986 begin
2987 -- The argument can be a single identifier
2988
2989 if Nkind (Expr) = N_Identifier then
2990
2991 -- One level of parens is allowed
2992
2993 if Paren_Count (Expr) > 1 then
2994 Error_Msg_F ("extra parentheses ignored", Expr);
2995 end if;
2996
2997 Set_Paren_Count (Expr, 0);
2998
2999 -- Add the single item to the list
3000
3001 Args := New_List (Expr);
3002
3003 -- Otherwise we must have an aggregate
3004
3005 elsif Nkind (Expr) = N_Aggregate then
3006
3007 -- Must be positional
3008
3009 if Present (Component_Associations (Expr)) then
3010 Error_Msg_F
3011 ("purely positional aggregate required", Expr);
3012 goto Continue;
3013 end if;
3014
3015 -- Must not be parenthesized
3016
3017 if Paren_Count (Expr) /= 0 then
3018 Error_Msg_F ("extra parentheses ignored", Expr);
3019 end if;
3020
3021 -- List of arguments is list of aggregate expressions
3022
3023 Args := Expressions (Expr);
3024
3025 -- Anything else is illegal
3026
3027 else
3028 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3029 goto Continue;
3030 end if;
3031
3032 -- Prepare pragma arguments
3033
3034 Pargs := New_List;
3035 Arg := First (Args);
3036 while Present (Arg) loop
3037 Append_To (Pargs,
3038 Make_Pragma_Argument_Association (Sloc (Arg),
3039 Expression => Relocate_Node (Arg)));
3040 Next (Arg);
3041 end loop;
3042
3043 Append_To (Pargs,
3044 Make_Pragma_Argument_Association (Sloc (Ent),
3045 Chars => Name_Entity,
3046 Expression => Ent));
3047
3048 Make_Aitem_Pragma
3049 (Pragma_Argument_Associations => Pargs,
3050 Pragma_Name => Name_Annotate);
3051 end;
3052
3053 -- Case 3 : Aspects that don't correspond to pragma/attribute
3054 -- definition clause.
3055
3056 -- Case 3a: The aspects listed below don't correspond to
3057 -- pragmas/attributes but do require delayed analysis.
3058
3059 -- Default_Value can only apply to a scalar type
3060
3061 when Aspect_Default_Value =>
3062 if not Is_Scalar_Type (E) then
3063 Error_Msg_N
3064 ("aspect Default_Value must apply to a scalar type", N);
3065 end if;
3066
3067 Aitem := Empty;
3068
3069 -- Default_Component_Value can only apply to an array type
3070 -- with scalar components.
3071
3072 when Aspect_Default_Component_Value =>
3073 if not (Is_Array_Type (E)
3074 and then Is_Scalar_Type (Component_Type (E)))
3075 then
3076 Error_Msg_N
3077 ("aspect Default_Component_Value can only apply to an "
3078 & "array of scalar components", N);
3079 end if;
3080
3081 Aitem := Empty;
3082
3083 -- Case 3b: The aspects listed below don't correspond to
3084 -- pragmas/attributes and don't need delayed analysis.
3085
3086 -- Implicit_Dereference
3087
3088 -- For Implicit_Dereference, External_Name and Link_Name, only
3089 -- the legality checks are done during the analysis, thus no
3090 -- delay is required.
3091
3092 when Aspect_Implicit_Dereference =>
3093 Analyze_Aspect_Implicit_Dereference;
3094 goto Continue;
3095
3096 -- Dimension
3097
3098 when Aspect_Dimension =>
3099 Analyze_Aspect_Dimension (N, Id, Expr);
3100 goto Continue;
3101
3102 -- Dimension_System
3103
3104 when Aspect_Dimension_System =>
3105 Analyze_Aspect_Dimension_System (N, Id, Expr);
3106 goto Continue;
3107
3108 -- Case 4: Aspects requiring special handling
3109
3110 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3111 -- pragmas take care of the delay.
3112
3113 -- Pre/Post
3114
3115 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3116 -- with a first argument that is the expression, and a second
3117 -- argument that is an informative message if the test fails.
3118 -- This is inserted right after the declaration, to get the
3119 -- required pragma placement. The processing for the pragmas
3120 -- takes care of the required delay.
3121
3122 when Pre_Post_Aspects => Pre_Post : declare
3123 Pname : Name_Id;
3124
3125 begin
3126 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
3127 Pname := Name_Precondition;
3128 else
3129 Pname := Name_Postcondition;
3130 end if;
3131
3132 -- Check that the class-wide predicate cannot be applied to
3133 -- an operation of a synchronized type that is not a tagged
3134 -- type. Other legality checks are performed when analyzing
3135 -- the contract of the operation.
3136
3137 if Class_Present (Aspect)
3138 and then Is_Concurrent_Type (Current_Scope)
3139 and then not Is_Tagged_Type (Current_Scope)
3140 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3141 then
3142 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3143 Error_Msg_N
3144 ("aspect % can only be specified for a primitive "
3145 & "operation of a tagged type", Aspect);
3146
3147 goto Continue;
3148 end if;
3149
3150 -- If the expressions is of the form A and then B, then
3151 -- we generate separate Pre/Post aspects for the separate
3152 -- clauses. Since we allow multiple pragmas, there is no
3153 -- problem in allowing multiple Pre/Post aspects internally.
3154 -- These should be treated in reverse order (B first and
3155 -- A second) since they are later inserted just after N in
3156 -- the order they are treated. This way, the pragma for A
3157 -- ends up preceding the pragma for B, which may have an
3158 -- importance for the error raised (either constraint error
3159 -- or precondition error).
3160
3161 -- We do not do this for Pre'Class, since we have to put
3162 -- these conditions together in a complex OR expression.
3163
3164 -- We do not do this in ASIS mode, as ASIS relies on the
3165 -- original node representing the complete expression, when
3166 -- retrieving it through the source aspect table.
3167
3168 if not ASIS_Mode
3169 and then (Pname = Name_Postcondition
3170 or else not Class_Present (Aspect))
3171 then
3172 while Nkind (Expr) = N_And_Then loop
3173 Insert_After (Aspect,
3174 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
3175 Identifier => Identifier (Aspect),
3176 Expression => Relocate_Node (Left_Opnd (Expr)),
3177 Class_Present => Class_Present (Aspect),
3178 Split_PPC => True));
3179 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
3180 Eloc := Sloc (Expr);
3181 end loop;
3182 end if;
3183
3184 -- Build the precondition/postcondition pragma
3185
3186 -- Add note about why we do NOT need Copy_Tree here???
3187
3188 Make_Aitem_Pragma
3189 (Pragma_Argument_Associations => New_List (
3190 Make_Pragma_Argument_Association (Eloc,
3191 Chars => Name_Check,
3192 Expression => Relocate_Node (Expr))),
3193 Pragma_Name => Pname);
3194
3195 -- Add message unless exception messages are suppressed
3196
3197 if not Opt.Exception_Locations_Suppressed then
3198 Append_To (Pragma_Argument_Associations (Aitem),
3199 Make_Pragma_Argument_Association (Eloc,
3200 Chars => Name_Message,
3201 Expression =>
3202 Make_String_Literal (Eloc,
3203 Strval => "failed "
3204 & Get_Name_String (Pname)
3205 & " from "
3206 & Build_Location_String (Eloc))));
3207 end if;
3208
3209 Set_Is_Delayed_Aspect (Aspect);
3210
3211 -- For Pre/Post cases, insert immediately after the entity
3212 -- declaration, since that is the required pragma placement.
3213 -- Note that for these aspects, we do not have to worry
3214 -- about delay issues, since the pragmas themselves deal
3215 -- with delay of visibility for the expression analysis.
3216
3217 Insert_Pragma (Aitem);
3218
3219 goto Continue;
3220 end Pre_Post;
3221
3222 -- Test_Case
3223
3224 when Aspect_Test_Case => Test_Case : declare
3225 Args : List_Id;
3226 Comp_Expr : Node_Id;
3227 Comp_Assn : Node_Id;
3228 New_Expr : Node_Id;
3229
3230 begin
3231 Args := New_List;
3232
3233 if Nkind (Parent (N)) = N_Compilation_Unit then
3234 Error_Msg_Name_1 := Nam;
3235 Error_Msg_N ("incorrect placement of aspect `%`", E);
3236 goto Continue;
3237 end if;
3238
3239 if Nkind (Expr) /= N_Aggregate then
3240 Error_Msg_Name_1 := Nam;
3241 Error_Msg_NE
3242 ("wrong syntax for aspect `%` for &", Id, E);
3243 goto Continue;
3244 end if;
3245
3246 -- Make pragma expressions refer to the original aspect
3247 -- expressions through the Original_Node link. This is used
3248 -- in semantic analysis for ASIS mode, so that the original
3249 -- expression also gets analyzed.
3250
3251 Comp_Expr := First (Expressions (Expr));
3252 while Present (Comp_Expr) loop
3253 New_Expr := Relocate_Node (Comp_Expr);
3254 Append_To (Args,
3255 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3256 Expression => New_Expr));
3257 Next (Comp_Expr);
3258 end loop;
3259
3260 Comp_Assn := First (Component_Associations (Expr));
3261 while Present (Comp_Assn) loop
3262 if List_Length (Choices (Comp_Assn)) /= 1
3263 or else
3264 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3265 then
3266 Error_Msg_Name_1 := Nam;
3267 Error_Msg_NE
3268 ("wrong syntax for aspect `%` for &", Id, E);
3269 goto Continue;
3270 end if;
3271
3272 Append_To (Args,
3273 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3274 Chars => Chars (First (Choices (Comp_Assn))),
3275 Expression =>
3276 Relocate_Node (Expression (Comp_Assn))));
3277 Next (Comp_Assn);
3278 end loop;
3279
3280 -- Build the test-case pragma
3281
3282 Make_Aitem_Pragma
3283 (Pragma_Argument_Associations => Args,
3284 Pragma_Name => Nam);
3285 end Test_Case;
3286
3287 -- Contract_Cases
3288
3289 when Aspect_Contract_Cases =>
3290 Make_Aitem_Pragma
3291 (Pragma_Argument_Associations => New_List (
3292 Make_Pragma_Argument_Association (Loc,
3293 Expression => Relocate_Node (Expr))),
3294 Pragma_Name => Nam);
3295
3296 Decorate (Aspect, Aitem);
3297 Insert_Pragma (Aitem);
3298 goto Continue;
3299
3300 -- Case 5: Special handling for aspects with an optional
3301 -- boolean argument.
3302
3303 -- In the delayed case, the corresponding pragma cannot be
3304 -- generated yet because the evaluation of the boolean needs
3305 -- to be delayed till the freeze point.
3306
3307 when Boolean_Aspects |
3308 Library_Unit_Aspects =>
3309
3310 Set_Is_Boolean_Aspect (Aspect);
3311
3312 -- Lock_Free aspect only apply to protected objects
3313
3314 if A_Id = Aspect_Lock_Free then
3315 if Ekind (E) /= E_Protected_Type then
3316 Error_Msg_Name_1 := Nam;
3317 Error_Msg_N
3318 ("aspect % only applies to a protected object",
3319 Aspect);
3320
3321 else
3322 -- Set the Uses_Lock_Free flag to True if there is no
3323 -- expression or if the expression is True. The
3324 -- evaluation of this aspect should be delayed to the
3325 -- freeze point (why???)
3326
3327 if No (Expr)
3328 or else Is_True (Static_Boolean (Expr))
3329 then
3330 Set_Uses_Lock_Free (E);
3331 end if;
3332
3333 Record_Rep_Item (E, Aspect);
3334 end if;
3335
3336 goto Continue;
3337
3338 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3339 Analyze_Aspect_Export_Import;
3340
3341 -- Disable_Controlled
3342
3343 elsif A_Id = Aspect_Disable_Controlled then
3344 if Ekind (E) /= E_Record_Type
3345 or else not Is_Controlled (E)
3346 then
3347 Error_Msg_N
3348 ("aspect % requires controlled record type", Aspect);
3349 goto Continue;
3350 end if;
3351
3352 -- If we're in a generic template, we don't want to try
3353 -- to disable controlled types, because typical usage is
3354 -- "Disable_Controlled => not <some_check>'Enabled", and
3355 -- the value of Enabled is not known until we see a
3356 -- particular instance. In such a context, we just need
3357 -- to preanalyze the expression for legality.
3358
3359 if Expander_Active then
3360 Analyze_And_Resolve (Expr, Standard_Boolean);
3361
3362 if not Present (Expr)
3363 or else Is_True (Static_Boolean (Expr))
3364 then
3365 Set_Disable_Controlled (E);
3366 end if;
3367
3368 elsif Serious_Errors_Detected = 0 then
3369 Preanalyze_And_Resolve (Expr, Standard_Boolean);
3370 end if;
3371
3372 goto Continue;
3373 end if;
3374
3375 -- Library unit aspects require special handling in the case
3376 -- of a package declaration, the pragma needs to be inserted
3377 -- in the list of declarations for the associated package.
3378 -- There is no issue of visibility delay for these aspects.
3379
3380 if A_Id in Library_Unit_Aspects
3381 and then
3382 Nkind_In (N, N_Package_Declaration,
3383 N_Generic_Package_Declaration)
3384 and then Nkind (Parent (N)) /= N_Compilation_Unit
3385
3386 -- Aspect is legal on a local instantiation of a library-
3387 -- level generic unit.
3388
3389 and then not Is_Generic_Instance (Defining_Entity (N))
3390 then
3391 Error_Msg_N
3392 ("incorrect context for library unit aspect&", Id);
3393 goto Continue;
3394 end if;
3395
3396 -- Cases where we do not delay, includes all cases where the
3397 -- expression is missing other than the above cases.
3398
3399 if not Delay_Required or else No (Expr) then
3400
3401 -- Exclude aspects Export and Import because their pragma
3402 -- syntax does not map directly to a Boolean aspect.
3403
3404 if A_Id /= Aspect_Export
3405 and then A_Id /= Aspect_Import
3406 then
3407 Make_Aitem_Pragma
3408 (Pragma_Argument_Associations => New_List (
3409 Make_Pragma_Argument_Association (Sloc (Ent),
3410 Expression => Ent)),
3411 Pragma_Name => Chars (Id));
3412 end if;
3413
3414 Delay_Required := False;
3415
3416 -- In general cases, the corresponding pragma/attribute
3417 -- definition clause will be inserted later at the freezing
3418 -- point, and we do not need to build it now.
3419
3420 else
3421 Aitem := Empty;
3422 end if;
3423
3424 -- Storage_Size
3425
3426 -- This is special because for access types we need to generate
3427 -- an attribute definition clause. This also works for single
3428 -- task declarations, but it does not work for task type
3429 -- declarations, because we have the case where the expression
3430 -- references a discriminant of the task type. That can't use
3431 -- an attribute definition clause because we would not have
3432 -- visibility on the discriminant. For that case we must
3433 -- generate a pragma in the task definition.
3434
3435 when Aspect_Storage_Size =>
3436
3437 -- Task type case
3438
3439 if Ekind (E) = E_Task_Type then
3440 declare
3441 Decl : constant Node_Id := Declaration_Node (E);
3442
3443 begin
3444 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3445
3446 -- If no task definition, create one
3447
3448 if No (Task_Definition (Decl)) then
3449 Set_Task_Definition (Decl,
3450 Make_Task_Definition (Loc,
3451 Visible_Declarations => Empty_List,
3452 End_Label => Empty));
3453 end if;
3454
3455 -- Create a pragma and put it at the start of the task
3456 -- definition for the task type declaration.
3457
3458 Make_Aitem_Pragma
3459 (Pragma_Argument_Associations => New_List (
3460 Make_Pragma_Argument_Association (Loc,
3461 Expression => Relocate_Node (Expr))),
3462 Pragma_Name => Name_Storage_Size);
3463
3464 Prepend
3465 (Aitem,
3466 Visible_Declarations (Task_Definition (Decl)));
3467 goto Continue;
3468 end;
3469
3470 -- All other cases, generate attribute definition
3471
3472 else
3473 Aitem :=
3474 Make_Attribute_Definition_Clause (Loc,
3475 Name => Ent,
3476 Chars => Chars (Id),
3477 Expression => Relocate_Node (Expr));
3478 end if;
3479 end case;
3480
3481 -- Attach the corresponding pragma/attribute definition clause to
3482 -- the aspect specification node.
3483
3484 if Present (Aitem) then
3485 Set_From_Aspect_Specification (Aitem);
3486 end if;
3487
3488 -- In the context of a compilation unit, we directly put the
3489 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3490 -- node (no delay is required here) except for aspects on a
3491 -- subprogram body (see below) and a generic package, for which we
3492 -- need to introduce the pragma before building the generic copy
3493 -- (see sem_ch12), and for package instantiations, where the
3494 -- library unit pragmas are better handled early.
3495
3496 if Nkind (Parent (N)) = N_Compilation_Unit
3497 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3498 then
3499 declare
3500 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3501
3502 begin
3503 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3504
3505 -- For a Boolean aspect, create the corresponding pragma if
3506 -- no expression or if the value is True.
3507
3508 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3509 if Is_True (Static_Boolean (Expr)) then
3510 Make_Aitem_Pragma
3511 (Pragma_Argument_Associations => New_List (
3512 Make_Pragma_Argument_Association (Sloc (Ent),
3513 Expression => Ent)),
3514 Pragma_Name => Chars (Id));
3515
3516 Set_From_Aspect_Specification (Aitem, True);
3517 Set_Corresponding_Aspect (Aitem, Aspect);
3518
3519 else
3520 goto Continue;
3521 end if;
3522 end if;
3523
3524 -- If the aspect is on a subprogram body (relevant aspect
3525 -- is Inline), add the pragma in front of the declarations.
3526
3527 if Nkind (N) = N_Subprogram_Body then
3528 if No (Declarations (N)) then
3529 Set_Declarations (N, New_List);
3530 end if;
3531
3532 Prepend (Aitem, Declarations (N));
3533
3534 elsif Nkind (N) = N_Generic_Package_Declaration then
3535 if No (Visible_Declarations (Specification (N))) then
3536 Set_Visible_Declarations (Specification (N), New_List);
3537 end if;
3538
3539 Prepend (Aitem,
3540 Visible_Declarations (Specification (N)));
3541
3542 elsif Nkind (N) = N_Package_Instantiation then
3543 declare
3544 Spec : constant Node_Id :=
3545 Specification (Instance_Spec (N));
3546 begin
3547 if No (Visible_Declarations (Spec)) then
3548 Set_Visible_Declarations (Spec, New_List);
3549 end if;
3550
3551 Prepend (Aitem, Visible_Declarations (Spec));
3552 end;
3553
3554 else
3555 if No (Pragmas_After (Aux)) then
3556 Set_Pragmas_After (Aux, New_List);
3557 end if;
3558
3559 Append (Aitem, Pragmas_After (Aux));
3560 end if;
3561
3562 goto Continue;
3563 end;
3564 end if;
3565
3566 -- The evaluation of the aspect is delayed to the freezing point.
3567 -- The pragma or attribute clause if there is one is then attached
3568 -- to the aspect specification which is put in the rep item list.
3569
3570 if Delay_Required then
3571 if Present (Aitem) then
3572 Set_Is_Delayed_Aspect (Aitem);
3573 Set_Aspect_Rep_Item (Aspect, Aitem);
3574 Set_Parent (Aitem, Aspect);
3575 end if;
3576
3577 Set_Is_Delayed_Aspect (Aspect);
3578
3579 -- In the case of Default_Value, link the aspect to base type
3580 -- as well, even though it appears on a first subtype. This is
3581 -- mandated by the semantics of the aspect. Do not establish
3582 -- the link when processing the base type itself as this leads
3583 -- to a rep item circularity. Verify that we are dealing with
3584 -- a scalar type to prevent cascaded errors.
3585
3586 if A_Id = Aspect_Default_Value
3587 and then Is_Scalar_Type (E)
3588 and then Base_Type (E) /= E
3589 then
3590 Set_Has_Delayed_Aspects (Base_Type (E));
3591 Record_Rep_Item (Base_Type (E), Aspect);
3592 end if;
3593
3594 Set_Has_Delayed_Aspects (E);
3595 Record_Rep_Item (E, Aspect);
3596
3597 -- When delay is not required and the context is a package or a
3598 -- subprogram body, insert the pragma in the body declarations.
3599
3600 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3601 if No (Declarations (N)) then
3602 Set_Declarations (N, New_List);
3603 end if;
3604
3605 -- The pragma is added before source declarations
3606
3607 Prepend_To (Declarations (N), Aitem);
3608
3609 -- When delay is not required and the context is not a compilation
3610 -- unit, we simply insert the pragma/attribute definition clause
3611 -- in sequence.
3612
3613 elsif Present (Aitem) then
3614 Insert_After (Ins_Node, Aitem);
3615 Ins_Node := Aitem;
3616 end if;
3617 end Analyze_One_Aspect;
3618
3619 <<Continue>>
3620 Next (Aspect);
3621 end loop Aspect_Loop;
3622
3623 if Has_Delayed_Aspects (E) then
3624 Ensure_Freeze_Node (E);
3625 end if;
3626 end Analyze_Aspect_Specifications;
3627
3628 ---------------------------------------------------
3629 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3630 ---------------------------------------------------
3631
3632 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3633 Body_Id : constant Entity_Id := Defining_Entity (N);
3634
3635 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3636 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3637 -- error message depending on the aspects involved. Spec_Id denotes the
3638 -- entity of the corresponding spec.
3639
3640 --------------------------------
3641 -- Diagnose_Misplaced_Aspects --
3642 --------------------------------
3643
3644 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3645 procedure Misplaced_Aspect_Error
3646 (Asp : Node_Id;
3647 Ref_Nam : Name_Id);
3648 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3649 -- the name of the refined version of the aspect.
3650
3651 ----------------------------
3652 -- Misplaced_Aspect_Error --
3653 ----------------------------
3654
3655 procedure Misplaced_Aspect_Error
3656 (Asp : Node_Id;
3657 Ref_Nam : Name_Id)
3658 is
3659 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3660 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3661
3662 begin
3663 -- The corresponding spec already contains the aspect in question
3664 -- and the one appearing on the body must be the refined form:
3665
3666 -- procedure P with Global ...;
3667 -- procedure P with Global ... is ... end P;
3668 -- ^
3669 -- Refined_Global
3670
3671 if Has_Aspect (Spec_Id, Asp_Id) then
3672 Error_Msg_Name_1 := Asp_Nam;
3673
3674 -- Subunits cannot carry aspects that apply to a subprogram
3675 -- declaration.
3676
3677 if Nkind (Parent (N)) = N_Subunit then
3678 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3679
3680 -- Otherwise suggest the refined form
3681
3682 else
3683 Error_Msg_Name_2 := Ref_Nam;
3684 Error_Msg_N ("aspect % should be %", Asp);
3685 end if;
3686
3687 -- Otherwise the aspect must appear on the spec, not on the body
3688
3689 -- procedure P;
3690 -- procedure P with Global ... is ... end P;
3691
3692 else
3693 Error_Msg_N
3694 ("aspect specification must appear on initial declaration",
3695 Asp);
3696 end if;
3697 end Misplaced_Aspect_Error;
3698
3699 -- Local variables
3700
3701 Asp : Node_Id;
3702 Asp_Nam : Name_Id;
3703
3704 -- Start of processing for Diagnose_Misplaced_Aspects
3705
3706 begin
3707 -- Iterate over the aspect specifications and emit specific errors
3708 -- where applicable.
3709
3710 Asp := First (Aspect_Specifications (N));
3711 while Present (Asp) loop
3712 Asp_Nam := Chars (Identifier (Asp));
3713
3714 -- Do not emit errors on aspects that can appear on a subprogram
3715 -- body. This scenario occurs when the aspect specification list
3716 -- contains both misplaced and properly placed aspects.
3717
3718 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3719 null;
3720
3721 -- Special diagnostics for SPARK aspects
3722
3723 elsif Asp_Nam = Name_Depends then
3724 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3725
3726 elsif Asp_Nam = Name_Global then
3727 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3728
3729 elsif Asp_Nam = Name_Post then
3730 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3731
3732 -- Otherwise a language-defined aspect is misplaced
3733
3734 else
3735 Error_Msg_N
3736 ("aspect specification must appear on initial declaration",
3737 Asp);
3738 end if;
3739
3740 Next (Asp);
3741 end loop;
3742 end Diagnose_Misplaced_Aspects;
3743
3744 -- Local variables
3745
3746 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
3747
3748 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3749
3750 begin
3751 -- Language-defined aspects cannot be associated with a subprogram body
3752 -- [stub] if the subprogram has a spec. Certain implementation defined
3753 -- aspects are allowed to break this rule (for all applicable cases, see
3754 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3755
3756 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
3757 Diagnose_Misplaced_Aspects (Spec_Id);
3758 else
3759 Analyze_Aspect_Specifications (N, Body_Id);
3760 end if;
3761 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3762
3763 -----------------------
3764 -- Analyze_At_Clause --
3765 -----------------------
3766
3767 -- An at clause is replaced by the corresponding Address attribute
3768 -- definition clause that is the preferred approach in Ada 95.
3769
3770 procedure Analyze_At_Clause (N : Node_Id) is
3771 CS : constant Boolean := Comes_From_Source (N);
3772
3773 begin
3774 -- This is an obsolescent feature
3775
3776 Check_Restriction (No_Obsolescent_Features, N);
3777
3778 if Warn_On_Obsolescent_Feature then
3779 Error_Msg_N
3780 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3781 Error_Msg_N
3782 ("\?j?use address attribute definition clause instead", N);
3783 end if;
3784
3785 -- Rewrite as address clause
3786
3787 Rewrite (N,
3788 Make_Attribute_Definition_Clause (Sloc (N),
3789 Name => Identifier (N),
3790 Chars => Name_Address,
3791 Expression => Expression (N)));
3792
3793 -- We preserve Comes_From_Source, since logically the clause still comes
3794 -- from the source program even though it is changed in form.
3795
3796 Set_Comes_From_Source (N, CS);
3797
3798 -- Analyze rewritten clause
3799
3800 Analyze_Attribute_Definition_Clause (N);
3801 end Analyze_At_Clause;
3802
3803 -----------------------------------------
3804 -- Analyze_Attribute_Definition_Clause --
3805 -----------------------------------------
3806
3807 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3808 Loc : constant Source_Ptr := Sloc (N);
3809 Nam : constant Node_Id := Name (N);
3810 Attr : constant Name_Id := Chars (N);
3811 Expr : constant Node_Id := Expression (N);
3812 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3813
3814 Ent : Entity_Id;
3815 -- The entity of Nam after it is analyzed. In the case of an incomplete
3816 -- type, this is the underlying type.
3817
3818 U_Ent : Entity_Id;
3819 -- The underlying entity to which the attribute applies. Generally this
3820 -- is the Underlying_Type of Ent, except in the case where the clause
3821 -- applies to full view of incomplete type or private type in which case
3822 -- U_Ent is just a copy of Ent.
3823
3824 FOnly : Boolean := False;
3825 -- Reset to True for subtype specific attribute (Alignment, Size)
3826 -- and for stream attributes, i.e. those cases where in the call to
3827 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3828 -- are checked. Note that the case of stream attributes is not clear
3829 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3830 -- Storage_Size for derived task types, but that is also clearly
3831 -- unintentional.
3832
3833 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3834 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3835 -- definition clauses.
3836
3837 function Duplicate_Clause return Boolean;
3838 -- This routine checks if the aspect for U_Ent being given by attribute
3839 -- definition clause N is for an aspect that has already been specified,
3840 -- and if so gives an error message. If there is a duplicate, True is
3841 -- returned, otherwise if there is no error, False is returned.
3842
3843 procedure Check_Indexing_Functions;
3844 -- Check that the function in Constant_Indexing or Variable_Indexing
3845 -- attribute has the proper type structure. If the name is overloaded,
3846 -- check that some interpretation is legal.
3847
3848 procedure Check_Iterator_Functions;
3849 -- Check that there is a single function in Default_Iterator attribute
3850 -- has the proper type structure.
3851
3852 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3853 -- Common legality check for the previous two
3854
3855 -----------------------------------
3856 -- Analyze_Stream_TSS_Definition --
3857 -----------------------------------
3858
3859 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3860 Subp : Entity_Id := Empty;
3861 I : Interp_Index;
3862 It : Interp;
3863 Pnam : Entity_Id;
3864
3865 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3866 -- True for Read attribute, False for other attributes
3867
3868 function Has_Good_Profile
3869 (Subp : Entity_Id;
3870 Report : Boolean := False) return Boolean;
3871 -- Return true if the entity is a subprogram with an appropriate
3872 -- profile for the attribute being defined. If result is False and
3873 -- Report is True, function emits appropriate error.
3874
3875 ----------------------
3876 -- Has_Good_Profile --
3877 ----------------------
3878
3879 function Has_Good_Profile
3880 (Subp : Entity_Id;
3881 Report : Boolean := False) return Boolean
3882 is
3883 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3884 (False => E_Procedure, True => E_Function);
3885 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3886 F : Entity_Id;
3887 Typ : Entity_Id;
3888
3889 begin
3890 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3891 return False;
3892 end if;
3893
3894 F := First_Formal (Subp);
3895
3896 if No (F)
3897 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3898 or else Designated_Type (Etype (F)) /=
3899 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3900 then
3901 return False;
3902 end if;
3903
3904 if not Is_Function then
3905 Next_Formal (F);
3906
3907 declare
3908 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3909 (False => E_In_Parameter,
3910 True => E_Out_Parameter);
3911 begin
3912 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3913 return False;
3914 end if;
3915 end;
3916
3917 Typ := Etype (F);
3918
3919 -- If the attribute specification comes from an aspect
3920 -- specification for a class-wide stream, the parameter must be
3921 -- a class-wide type of the entity to which the aspect applies.
3922
3923 if From_Aspect_Specification (N)
3924 and then Class_Present (Parent (N))
3925 and then Is_Class_Wide_Type (Typ)
3926 then
3927 Typ := Etype (Typ);
3928 end if;
3929
3930 else
3931 Typ := Etype (Subp);
3932 end if;
3933
3934 -- Verify that the prefix of the attribute and the local name for
3935 -- the type of the formal match, or one is the class-wide of the
3936 -- other, in the case of a class-wide stream operation.
3937
3938 if Base_Type (Typ) = Base_Type (Ent)
3939 or else (Is_Class_Wide_Type (Typ)
3940 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3941 or else (Is_Class_Wide_Type (Ent)
3942 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3943 then
3944 null;
3945 else
3946 return False;
3947 end if;
3948
3949 if Present (Next_Formal (F)) then
3950 return False;
3951
3952 elsif not Is_Scalar_Type (Typ)
3953 and then not Is_First_Subtype (Typ)
3954 and then not Is_Class_Wide_Type (Typ)
3955 then
3956 if Report and not Is_First_Subtype (Typ) then
3957 Error_Msg_N
3958 ("subtype of formal in stream operation must be a first "
3959 & "subtype", Parameter_Type (Parent (F)));
3960 end if;
3961
3962 return False;
3963
3964 else
3965 return True;
3966 end if;
3967 end Has_Good_Profile;
3968
3969 -- Start of processing for Analyze_Stream_TSS_Definition
3970
3971 begin
3972 FOnly := True;
3973
3974 if not Is_Type (U_Ent) then
3975 Error_Msg_N ("local name must be a subtype", Nam);
3976 return;
3977
3978 elsif not Is_First_Subtype (U_Ent) then
3979 Error_Msg_N ("local name must be a first subtype", Nam);
3980 return;
3981 end if;
3982
3983 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3984
3985 -- If Pnam is present, it can be either inherited from an ancestor
3986 -- type (in which case it is legal to redefine it for this type), or
3987 -- be a previous definition of the attribute for the same type (in
3988 -- which case it is illegal).
3989
3990 -- In the first case, it will have been analyzed already, and we
3991 -- can check that its profile does not match the expected profile
3992 -- for a stream attribute of U_Ent. In the second case, either Pnam
3993 -- has been analyzed (and has the expected profile), or it has not
3994 -- been analyzed yet (case of a type that has not been frozen yet
3995 -- and for which the stream attribute has been set using Set_TSS).
3996
3997 if Present (Pnam)
3998 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3999 then
4000 Error_Msg_Sloc := Sloc (Pnam);
4001 Error_Msg_Name_1 := Attr;
4002 Error_Msg_N ("% attribute already defined #", Nam);
4003 return;
4004 end if;
4005
4006 Analyze (Expr);
4007
4008 if Is_Entity_Name (Expr) then
4009 if not Is_Overloaded (Expr) then
4010 if Has_Good_Profile (Entity (Expr), Report => True) then
4011 Subp := Entity (Expr);
4012 end if;
4013
4014 else
4015 Get_First_Interp (Expr, I, It);
4016 while Present (It.Nam) loop
4017 if Has_Good_Profile (It.Nam) then
4018 Subp := It.Nam;
4019 exit;
4020 end if;
4021
4022 Get_Next_Interp (I, It);
4023 end loop;
4024 end if;
4025 end if;
4026
4027 if Present (Subp) then
4028 if Is_Abstract_Subprogram (Subp) then
4029 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4030 return;
4031
4032 -- A stream subprogram for an interface type must be a null
4033 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4034 -- of an interface is not an interface type (3.9.4 (6.b/2)).
4035
4036 elsif Is_Interface (U_Ent)
4037 and then not Is_Class_Wide_Type (U_Ent)
4038 and then not Inside_A_Generic
4039 and then
4040 (Ekind (Subp) = E_Function
4041 or else
4042 not Null_Present
4043 (Specification
4044 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
4045 then
4046 Error_Msg_N
4047 ("stream subprogram for interface type must be null "
4048 & "procedure", Expr);
4049 end if;
4050
4051 Set_Entity (Expr, Subp);
4052 Set_Etype (Expr, Etype (Subp));
4053
4054 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
4055
4056 else
4057 Error_Msg_Name_1 := Attr;
4058 Error_Msg_N ("incorrect expression for% attribute", Expr);
4059 end if;
4060 end Analyze_Stream_TSS_Definition;
4061
4062 ------------------------------
4063 -- Check_Indexing_Functions --
4064 ------------------------------
4065
4066 procedure Check_Indexing_Functions is
4067 Indexing_Found : Boolean := False;
4068
4069 procedure Check_Inherited_Indexing;
4070 -- For a derived type, check that no indexing aspect is specified
4071 -- for the type if it is also inherited
4072
4073 procedure Check_One_Function (Subp : Entity_Id);
4074 -- Check one possible interpretation. Sets Indexing_Found True if a
4075 -- legal indexing function is found.
4076
4077 procedure Illegal_Indexing (Msg : String);
4078 -- Diagnose illegal indexing function if not overloaded. In the
4079 -- overloaded case indicate that no legal interpretation exists.
4080
4081 ------------------------------
4082 -- Check_Inherited_Indexing --
4083 ------------------------------
4084
4085 procedure Check_Inherited_Indexing is
4086 Inherited : Node_Id;
4087
4088 begin
4089 if Attr = Name_Constant_Indexing then
4090 Inherited :=
4091 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4092 else pragma Assert (Attr = Name_Variable_Indexing);
4093 Inherited :=
4094 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4095 end if;
4096
4097 if Present (Inherited) then
4098 if Debug_Flag_Dot_XX then
4099 null;
4100
4101 -- OK if current attribute_definition_clause is expansion of
4102 -- inherited aspect.
4103
4104 elsif Aspect_Rep_Item (Inherited) = N then
4105 null;
4106
4107 -- Indicate the operation that must be overridden, rather than
4108 -- redefining the indexing aspect.
4109
4110 else
4111 Illegal_Indexing
4112 ("indexing function already inherited from parent type");
4113 Error_Msg_NE
4114 ("!override & instead",
4115 N, Entity (Expression (Inherited)));
4116 end if;
4117 end if;
4118 end Check_Inherited_Indexing;
4119
4120 ------------------------
4121 -- Check_One_Function --
4122 ------------------------
4123
4124 procedure Check_One_Function (Subp : Entity_Id) is
4125 Default_Element : Node_Id;
4126 Ret_Type : constant Entity_Id := Etype (Subp);
4127
4128 begin
4129 if not Is_Overloadable (Subp) then
4130 Illegal_Indexing ("illegal indexing function for type&");
4131 return;
4132
4133 elsif Scope (Subp) /= Scope (Ent) then
4134 if Nkind (Expr) = N_Expanded_Name then
4135
4136 -- Indexing function can't be declared elsewhere
4137
4138 Illegal_Indexing
4139 ("indexing function must be declared in scope of type&");
4140 end if;
4141
4142 return;
4143
4144 elsif No (First_Formal (Subp)) then
4145 Illegal_Indexing
4146 ("Indexing requires a function that applies to type&");
4147 return;
4148
4149 elsif No (Next_Formal (First_Formal (Subp))) then
4150 Illegal_Indexing
4151 ("indexing function must have at least two parameters");
4152 return;
4153
4154 elsif Is_Derived_Type (Ent) then
4155 Check_Inherited_Indexing;
4156 end if;
4157
4158 if not Check_Primitive_Function (Subp) then
4159 Illegal_Indexing
4160 ("Indexing aspect requires a function that applies to type&");
4161 return;
4162 end if;
4163
4164 -- If partial declaration exists, verify that it is not tagged.
4165
4166 if Ekind (Current_Scope) = E_Package
4167 and then Has_Private_Declaration (Ent)
4168 and then From_Aspect_Specification (N)
4169 and then
4170 List_Containing (Parent (Ent)) =
4171 Private_Declarations
4172 (Specification (Unit_Declaration_Node (Current_Scope)))
4173 and then Nkind (N) = N_Attribute_Definition_Clause
4174 then
4175 declare
4176 Decl : Node_Id;
4177
4178 begin
4179 Decl :=
4180 First (Visible_Declarations
4181 (Specification
4182 (Unit_Declaration_Node (Current_Scope))));
4183
4184 while Present (Decl) loop
4185 if Nkind (Decl) = N_Private_Type_Declaration
4186 and then Ent = Full_View (Defining_Identifier (Decl))
4187 and then Tagged_Present (Decl)
4188 and then No (Aspect_Specifications (Decl))
4189 then
4190 Illegal_Indexing
4191 ("Indexing aspect cannot be specified on full view "
4192 & "if partial view is tagged");
4193 return;
4194 end if;
4195
4196 Next (Decl);
4197 end loop;
4198 end;
4199 end if;
4200
4201 -- An indexing function must return either the default element of
4202 -- the container, or a reference type. For variable indexing it
4203 -- must be the latter.
4204
4205 Default_Element :=
4206 Find_Value_Of_Aspect
4207 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4208
4209 if Present (Default_Element) then
4210 Analyze (Default_Element);
4211
4212 if Is_Entity_Name (Default_Element)
4213 and then not Covers (Entity (Default_Element), Ret_Type)
4214 and then False
4215 then
4216 Illegal_Indexing
4217 ("wrong return type for indexing function");
4218 return;
4219 end if;
4220 end if;
4221
4222 -- For variable_indexing the return type must be a reference type
4223
4224 if Attr = Name_Variable_Indexing then
4225 if not Has_Implicit_Dereference (Ret_Type) then
4226 Illegal_Indexing
4227 ("variable indexing must return a reference type");
4228 return;
4229
4230 elsif Is_Access_Constant
4231 (Etype (First_Discriminant (Ret_Type)))
4232 then
4233 Illegal_Indexing
4234 ("variable indexing must return an access to variable");
4235 return;
4236 end if;
4237
4238 else
4239 if Has_Implicit_Dereference (Ret_Type)
4240 and then not
4241 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4242 then
4243 Illegal_Indexing
4244 ("constant indexing must return an access to constant");
4245 return;
4246
4247 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4248 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4249 then
4250 Illegal_Indexing
4251 ("constant indexing must apply to an access to constant");
4252 return;
4253 end if;
4254 end if;
4255
4256 -- All checks succeeded.
4257
4258 Indexing_Found := True;
4259 end Check_One_Function;
4260
4261 -----------------------
4262 -- Illegal_Indexing --
4263 -----------------------
4264
4265 procedure Illegal_Indexing (Msg : String) is
4266 begin
4267 Error_Msg_NE (Msg, N, Ent);
4268 end Illegal_Indexing;
4269
4270 -- Start of processing for Check_Indexing_Functions
4271
4272 begin
4273 if In_Instance then
4274 Check_Inherited_Indexing;
4275 end if;
4276
4277 Analyze (Expr);
4278
4279 if not Is_Overloaded (Expr) then
4280 Check_One_Function (Entity (Expr));
4281
4282 else
4283 declare
4284 I : Interp_Index;
4285 It : Interp;
4286
4287 begin
4288 Indexing_Found := False;
4289 Get_First_Interp (Expr, I, It);
4290 while Present (It.Nam) loop
4291
4292 -- Note that analysis will have added the interpretation
4293 -- that corresponds to the dereference. We only check the
4294 -- subprogram itself.
4295
4296 if Is_Overloadable (It.Nam) then
4297 Check_One_Function (It.Nam);
4298 end if;
4299
4300 Get_Next_Interp (I, It);
4301 end loop;
4302 end;
4303 end if;
4304
4305 if not Indexing_Found and then not Error_Posted (N) then
4306 Error_Msg_NE
4307 ("aspect Indexing requires a local function that "
4308 & "applies to type&", Expr, Ent);
4309 end if;
4310 end Check_Indexing_Functions;
4311
4312 ------------------------------
4313 -- Check_Iterator_Functions --
4314 ------------------------------
4315
4316 procedure Check_Iterator_Functions is
4317 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4318 -- Check one possible interpretation for validity
4319
4320 ----------------------------
4321 -- Valid_Default_Iterator --
4322 ----------------------------
4323
4324 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4325 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
4326 Formal : Entity_Id;
4327
4328 begin
4329 if not Check_Primitive_Function (Subp) then
4330 return False;
4331
4332 -- The return type must be derived from a type in an instance
4333 -- of Iterator.Interfaces, and thus its root type must have a
4334 -- predefined name.
4335
4336 elsif Chars (Root_T) /= Name_Forward_Iterator
4337 and then Chars (Root_T) /= Name_Reversible_Iterator
4338 then
4339 return False;
4340
4341 else
4342 Formal := First_Formal (Subp);
4343 end if;
4344
4345 -- False if any subsequent formal has no default expression
4346
4347 Formal := Next_Formal (Formal);
4348 while Present (Formal) loop
4349 if No (Expression (Parent (Formal))) then
4350 return False;
4351 end if;
4352
4353 Next_Formal (Formal);
4354 end loop;
4355
4356 -- True if all subsequent formals have default expressions
4357
4358 return True;
4359 end Valid_Default_Iterator;
4360
4361 -- Start of processing for Check_Iterator_Functions
4362
4363 begin
4364 Analyze (Expr);
4365
4366 if not Is_Entity_Name (Expr) then
4367 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4368 end if;
4369
4370 if not Is_Overloaded (Expr) then
4371 if not Check_Primitive_Function (Entity (Expr)) then
4372 Error_Msg_NE
4373 ("aspect Indexing requires a function that applies to type&",
4374 Entity (Expr), Ent);
4375 end if;
4376
4377 -- Flag the default_iterator as well as the denoted function.
4378
4379 if not Valid_Default_Iterator (Entity (Expr)) then
4380 Error_Msg_N ("improper function for default iterator!", Expr);
4381 end if;
4382
4383 else
4384 declare
4385 Default : Entity_Id := Empty;
4386 I : Interp_Index;
4387 It : Interp;
4388
4389 begin
4390 Get_First_Interp (Expr, I, It);
4391 while Present (It.Nam) loop
4392 if not Check_Primitive_Function (It.Nam)
4393 or else not Valid_Default_Iterator (It.Nam)
4394 then
4395 Remove_Interp (I);
4396
4397 elsif Present (Default) then
4398
4399 -- An explicit one should override an implicit one
4400
4401 if Comes_From_Source (Default) =
4402 Comes_From_Source (It.Nam)
4403 then
4404 Error_Msg_N ("default iterator must be unique", Expr);
4405 Error_Msg_Sloc := Sloc (Default);
4406 Error_Msg_N ("\\possible interpretation#", Expr);
4407 Error_Msg_Sloc := Sloc (It.Nam);
4408 Error_Msg_N ("\\possible interpretation#", Expr);
4409
4410 elsif Comes_From_Source (It.Nam) then
4411 Default := It.Nam;
4412 end if;
4413 else
4414 Default := It.Nam;
4415 end if;
4416
4417 Get_Next_Interp (I, It);
4418 end loop;
4419
4420 if Present (Default) then
4421 Set_Entity (Expr, Default);
4422 Set_Is_Overloaded (Expr, False);
4423 else
4424 Error_Msg_N
4425 ("no interpretation is a valid default iterator!", Expr);
4426 end if;
4427 end;
4428 end if;
4429 end Check_Iterator_Functions;
4430
4431 -------------------------------
4432 -- Check_Primitive_Function --
4433 -------------------------------
4434
4435 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4436 Ctrl : Entity_Id;
4437
4438 begin
4439 if Ekind (Subp) /= E_Function then
4440 return False;
4441 end if;
4442
4443 if No (First_Formal (Subp)) then
4444 return False;
4445 else
4446 Ctrl := Etype (First_Formal (Subp));
4447 end if;
4448
4449 -- To be a primitive operation subprogram has to be in same scope.
4450
4451 if Scope (Ctrl) /= Scope (Subp) then
4452 return False;
4453 end if;
4454
4455 -- Type of formal may be the class-wide type, an access to such,
4456 -- or an incomplete view.
4457
4458 if Ctrl = Ent
4459 or else Ctrl = Class_Wide_Type (Ent)
4460 or else
4461 (Ekind (Ctrl) = E_Anonymous_Access_Type
4462 and then (Designated_Type (Ctrl) = Ent
4463 or else
4464 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4465 or else
4466 (Ekind (Ctrl) = E_Incomplete_Type
4467 and then Full_View (Ctrl) = Ent)
4468 then
4469 null;
4470 else
4471 return False;
4472 end if;
4473
4474 return True;
4475 end Check_Primitive_Function;
4476
4477 ----------------------
4478 -- Duplicate_Clause --
4479 ----------------------
4480
4481 function Duplicate_Clause return Boolean is
4482 A : Node_Id;
4483
4484 begin
4485 -- Nothing to do if this attribute definition clause comes from
4486 -- an aspect specification, since we could not be duplicating an
4487 -- explicit clause, and we dealt with the case of duplicated aspects
4488 -- in Analyze_Aspect_Specifications.
4489
4490 if From_Aspect_Specification (N) then
4491 return False;
4492 end if;
4493
4494 -- Otherwise current clause may duplicate previous clause, or a
4495 -- previously given pragma or aspect specification for the same
4496 -- aspect.
4497
4498 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4499
4500 if Present (A) then
4501 Error_Msg_Name_1 := Chars (N);
4502 Error_Msg_Sloc := Sloc (A);
4503
4504 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4505 return True;
4506 end if;
4507
4508 return False;
4509 end Duplicate_Clause;
4510
4511 -- Start of processing for Analyze_Attribute_Definition_Clause
4512
4513 begin
4514 -- The following code is a defense against recursion. Not clear that
4515 -- this can happen legitimately, but perhaps some error situations can
4516 -- cause it, and we did see this recursion during testing.
4517
4518 if Analyzed (N) then
4519 return;
4520 else
4521 Set_Analyzed (N, True);
4522 end if;
4523
4524 Check_Restriction_No_Use_Of_Attribute (N);
4525
4526 -- Ignore some selected attributes in CodePeer mode since they are not
4527 -- relevant in this context.
4528
4529 if CodePeer_Mode then
4530 case Id is
4531
4532 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4533 -- internal representation of types by implicitly packing them.
4534
4535 when Attribute_Component_Size =>
4536 Rewrite (N, Make_Null_Statement (Sloc (N)));
4537 return;
4538
4539 when others =>
4540 null;
4541 end case;
4542 end if;
4543
4544 -- Process Ignore_Rep_Clauses option
4545
4546 if Ignore_Rep_Clauses then
4547 case Id is
4548
4549 -- The following should be ignored. They do not affect legality
4550 -- and may be target dependent. The basic idea of -gnatI is to
4551 -- ignore any rep clauses that may be target dependent but do not
4552 -- affect legality (except possibly to be rejected because they
4553 -- are incompatible with the compilation target).
4554
4555 when Attribute_Alignment |
4556 Attribute_Bit_Order |
4557 Attribute_Component_Size |
4558 Attribute_Machine_Radix |
4559 Attribute_Object_Size |
4560 Attribute_Size |
4561 Attribute_Small |
4562 Attribute_Stream_Size |
4563 Attribute_Value_Size =>
4564 Kill_Rep_Clause (N);
4565 return;
4566
4567 -- The following should not be ignored, because in the first place
4568 -- they are reasonably portable, and should not cause problems
4569 -- in compiling code from another target, and also they do affect
4570 -- legality, e.g. failing to provide a stream attribute for a type
4571 -- may make a program illegal.
4572
4573 when Attribute_External_Tag |
4574 Attribute_Input |
4575 Attribute_Output |
4576 Attribute_Read |
4577 Attribute_Simple_Storage_Pool |
4578 Attribute_Storage_Pool |
4579 Attribute_Storage_Size |
4580 Attribute_Write =>
4581 null;
4582
4583 -- We do not do anything here with address clauses, they will be
4584 -- removed by Freeze later on, but for now, it works better to
4585 -- keep then in the tree.
4586
4587 when Attribute_Address =>
4588 null;
4589
4590 -- Other cases are errors ("attribute& cannot be set with
4591 -- definition clause"), which will be caught below.
4592
4593 when others =>
4594 null;
4595 end case;
4596 end if;
4597
4598 Analyze (Nam);
4599 Ent := Entity (Nam);
4600
4601 if Rep_Item_Too_Early (Ent, N) then
4602 return;
4603 end if;
4604
4605 -- Rep clause applies to full view of incomplete type or private type if
4606 -- we have one (if not, this is a premature use of the type). However,
4607 -- certain semantic checks need to be done on the specified entity (i.e.
4608 -- the private view), so we save it in Ent.
4609
4610 if Is_Private_Type (Ent)
4611 and then Is_Derived_Type (Ent)
4612 and then not Is_Tagged_Type (Ent)
4613 and then No (Full_View (Ent))
4614 then
4615 -- If this is a private type whose completion is a derivation from
4616 -- another private type, there is no full view, and the attribute
4617 -- belongs to the type itself, not its underlying parent.
4618
4619 U_Ent := Ent;
4620
4621 elsif Ekind (Ent) = E_Incomplete_Type then
4622
4623 -- The attribute applies to the full view, set the entity of the
4624 -- attribute definition accordingly.
4625
4626 Ent := Underlying_Type (Ent);
4627 U_Ent := Ent;
4628 Set_Entity (Nam, Ent);
4629
4630 else
4631 U_Ent := Underlying_Type (Ent);
4632 end if;
4633
4634 -- Avoid cascaded error
4635
4636 if Etype (Nam) = Any_Type then
4637 return;
4638
4639 -- Must be declared in current scope or in case of an aspect
4640 -- specification, must be visible in current scope.
4641
4642 elsif Scope (Ent) /= Current_Scope
4643 and then
4644 not (From_Aspect_Specification (N)
4645 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4646 then
4647 Error_Msg_N ("entity must be declared in this scope", Nam);
4648 return;
4649
4650 -- Must not be a source renaming (we do have some cases where the
4651 -- expander generates a renaming, and those cases are OK, in such
4652 -- cases any attribute applies to the renamed object as well).
4653
4654 elsif Is_Object (Ent)
4655 and then Present (Renamed_Object (Ent))
4656 then
4657 -- Case of renamed object from source, this is an error
4658
4659 if Comes_From_Source (Renamed_Object (Ent)) then
4660 Get_Name_String (Chars (N));
4661 Error_Msg_Strlen := Name_Len;
4662 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4663 Error_Msg_N
4664 ("~ clause not allowed for a renaming declaration "
4665 & "(RM 13.1(6))", Nam);
4666 return;
4667
4668 -- For the case of a compiler generated renaming, the attribute
4669 -- definition clause applies to the renamed object created by the
4670 -- expander. The easiest general way to handle this is to create a
4671 -- copy of the attribute definition clause for this object.
4672
4673 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4674 Insert_Action (N,
4675 Make_Attribute_Definition_Clause (Loc,
4676 Name =>
4677 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4678 Chars => Chars (N),
4679 Expression => Duplicate_Subexpr (Expression (N))));
4680
4681 -- If the renamed object is not an entity, it must be a dereference
4682 -- of an unconstrained function call, and we must introduce a new
4683 -- declaration to capture the expression. This is needed in the case
4684 -- of 'Alignment, where the original declaration must be rewritten.
4685
4686 else
4687 pragma Assert
4688 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4689 null;
4690 end if;
4691
4692 -- If no underlying entity, use entity itself, applies to some
4693 -- previously detected error cases ???
4694
4695 elsif No (U_Ent) then
4696 U_Ent := Ent;
4697
4698 -- Cannot specify for a subtype (exception Object/Value_Size)
4699
4700 elsif Is_Type (U_Ent)
4701 and then not Is_First_Subtype (U_Ent)
4702 and then Id /= Attribute_Object_Size
4703 and then Id /= Attribute_Value_Size
4704 and then not From_At_Mod (N)
4705 then
4706 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4707 return;
4708 end if;
4709
4710 Set_Entity (N, U_Ent);
4711
4712 -- Switch on particular attribute
4713
4714 case Id is
4715
4716 -------------
4717 -- Address --
4718 -------------
4719
4720 -- Address attribute definition clause
4721
4722 when Attribute_Address => Address : begin
4723
4724 -- A little error check, catch for X'Address use X'Address;
4725
4726 if Nkind (Nam) = N_Identifier
4727 and then Nkind (Expr) = N_Attribute_Reference
4728 and then Attribute_Name (Expr) = Name_Address
4729 and then Nkind (Prefix (Expr)) = N_Identifier
4730 and then Chars (Nam) = Chars (Prefix (Expr))
4731 then
4732 Error_Msg_NE
4733 ("address for & is self-referencing", Prefix (Expr), Ent);
4734 return;
4735 end if;
4736
4737 -- Not that special case, carry on with analysis of expression
4738
4739 Analyze_And_Resolve (Expr, RTE (RE_Address));
4740
4741 -- Even when ignoring rep clauses we need to indicate that the
4742 -- entity has an address clause and thus it is legal to declare
4743 -- it imported. Freeze will get rid of the address clause later.
4744
4745 if Ignore_Rep_Clauses then
4746 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4747 Record_Rep_Item (U_Ent, N);
4748 end if;
4749
4750 return;
4751 end if;
4752
4753 if Duplicate_Clause then
4754 null;
4755
4756 -- Case of address clause for subprogram
4757
4758 elsif Is_Subprogram (U_Ent) then
4759 if Has_Homonym (U_Ent) then
4760 Error_Msg_N
4761 ("address clause cannot be given for overloaded "
4762 & "subprogram", Nam);
4763 return;
4764 end if;
4765
4766 -- For subprograms, all address clauses are permitted, and we
4767 -- mark the subprogram as having a deferred freeze so that Gigi
4768 -- will not elaborate it too soon.
4769
4770 -- Above needs more comments, what is too soon about???
4771
4772 Set_Has_Delayed_Freeze (U_Ent);
4773
4774 -- Case of address clause for entry
4775
4776 elsif Ekind (U_Ent) = E_Entry then
4777 if Nkind (Parent (N)) = N_Task_Body then
4778 Error_Msg_N
4779 ("entry address must be specified in task spec", Nam);
4780 return;
4781 end if;
4782
4783 -- For entries, we require a constant address
4784
4785 Check_Constant_Address_Clause (Expr, U_Ent);
4786
4787 -- Special checks for task types
4788
4789 if Is_Task_Type (Scope (U_Ent))
4790 and then Comes_From_Source (Scope (U_Ent))
4791 then
4792 Error_Msg_N
4793 ("??entry address declared for entry in task type", N);
4794 Error_Msg_N
4795 ("\??only one task can be declared of this type", N);
4796 end if;
4797
4798 -- Entry address clauses are obsolescent
4799
4800 Check_Restriction (No_Obsolescent_Features, N);
4801
4802 if Warn_On_Obsolescent_Feature then
4803 Error_Msg_N
4804 ("?j?attaching interrupt to task entry is an obsolescent "
4805 & "feature (RM J.7.1)", N);
4806 Error_Msg_N
4807 ("\?j?use interrupt procedure instead", N);
4808 end if;
4809
4810 -- Case of an address clause for a controlled object which we
4811 -- consider to be erroneous.
4812
4813 elsif Is_Controlled (Etype (U_Ent))
4814 or else Has_Controlled_Component (Etype (U_Ent))
4815 then
4816 Error_Msg_NE
4817 ("??controlled object& must not be overlaid", Nam, U_Ent);
4818 Error_Msg_N
4819 ("\??Program_Error will be raised at run time", Nam);
4820 Insert_Action (Declaration_Node (U_Ent),
4821 Make_Raise_Program_Error (Loc,
4822 Reason => PE_Overlaid_Controlled_Object));
4823 return;
4824
4825 -- Case of address clause for a (non-controlled) object
4826
4827 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4828 declare
4829 Expr : constant Node_Id := Expression (N);
4830 O_Ent : Entity_Id;
4831 Off : Boolean;
4832
4833 begin
4834 -- Exported variables cannot have an address clause, because
4835 -- this cancels the effect of the pragma Export.
4836
4837 if Is_Exported (U_Ent) then
4838 Error_Msg_N
4839 ("cannot export object with address clause", Nam);
4840 return;
4841 end if;
4842
4843 Find_Overlaid_Entity (N, O_Ent, Off);
4844
4845 if Present (O_Ent) then
4846
4847 -- If the object overlays a constant object, mark it so
4848
4849 if Is_Constant_Object (O_Ent) then
4850 Set_Overlays_Constant (U_Ent);
4851 end if;
4852
4853 else
4854 -- If this is not an overlay, mark a variable as being
4855 -- volatile to prevent unwanted optimizations. It's a
4856 -- conservative interpretation of RM 13.3(19) for the
4857 -- cases where the compiler cannot detect potential
4858 -- aliasing issues easily and it also covers the case
4859 -- of an absolute address where the volatile aspect is
4860 -- kind of implicit.
4861
4862 if Ekind (U_Ent) = E_Variable then
4863 Set_Treat_As_Volatile (U_Ent);
4864 end if;
4865 end if;
4866
4867 -- Overlaying controlled objects is erroneous. Emit warning
4868 -- but continue analysis because program is itself legal,
4869 -- and back end must see address clause.
4870
4871 if Present (O_Ent)
4872 and then (Has_Controlled_Component (Etype (O_Ent))
4873 or else Is_Controlled (Etype (O_Ent)))
4874 and then not Inside_A_Generic
4875 then
4876 Error_Msg_N
4877 ("??cannot use overlays with controlled objects", Expr);
4878 Error_Msg_N
4879 ("\??Program_Error will be raised at run time", Expr);
4880 Insert_Action (Declaration_Node (U_Ent),
4881 Make_Raise_Program_Error (Loc,
4882 Reason => PE_Overlaid_Controlled_Object));
4883
4884 -- Issue an unconditional warning for a constant overlaying
4885 -- a variable. For the reverse case, we will issue it only
4886 -- if the variable is modified.
4887
4888 elsif Ekind (U_Ent) = E_Constant
4889 and then Present (O_Ent)
4890 and then not Overlays_Constant (U_Ent)
4891 and then Address_Clause_Overlay_Warnings
4892 then
4893 Error_Msg_N ("??constant overlays a variable", Expr);
4894
4895 -- Imported variables can have an address clause, but then
4896 -- the import is pretty meaningless except to suppress
4897 -- initializations, so we do not need such variables to
4898 -- be statically allocated (and in fact it causes trouble
4899 -- if the address clause is a local value).
4900
4901 elsif Is_Imported (U_Ent) then
4902 Set_Is_Statically_Allocated (U_Ent, False);
4903 end if;
4904
4905 -- We mark a possible modification of a variable with an
4906 -- address clause, since it is likely aliasing is occurring.
4907
4908 Note_Possible_Modification (Nam, Sure => False);
4909
4910 -- Legality checks on the address clause for initialized
4911 -- objects is deferred until the freeze point, because
4912 -- a subsequent pragma might indicate that the object
4913 -- is imported and thus not initialized. Also, the address
4914 -- clause might involve entities that have yet to be
4915 -- elaborated.
4916
4917 Set_Has_Delayed_Freeze (U_Ent);
4918
4919 -- If an initialization call has been generated for this
4920 -- object, it needs to be deferred to after the freeze node
4921 -- we have just now added, otherwise GIGI will see a
4922 -- reference to the variable (as actual to the IP call)
4923 -- before its definition.
4924
4925 declare
4926 Init_Call : constant Node_Id :=
4927 Remove_Init_Call (U_Ent, N);
4928
4929 begin
4930 if Present (Init_Call) then
4931 Append_Freeze_Action (U_Ent, Init_Call);
4932
4933 -- Reset Initialization_Statements pointer so that
4934 -- if there is a pragma Import further down, it can
4935 -- clear any default initialization.
4936
4937 Set_Initialization_Statements (U_Ent, Init_Call);
4938 end if;
4939 end;
4940
4941 -- Entity has delayed freeze, so we will generate an
4942 -- alignment check at the freeze point unless suppressed.
4943
4944 if not Range_Checks_Suppressed (U_Ent)
4945 and then not Alignment_Checks_Suppressed (U_Ent)
4946 then
4947 Set_Check_Address_Alignment (N);
4948 end if;
4949
4950 -- Kill the size check code, since we are not allocating
4951 -- the variable, it is somewhere else.
4952
4953 Kill_Size_Check_Code (U_Ent);
4954
4955 -- If the address clause is of the form:
4956
4957 -- for Y'Address use X'Address
4958
4959 -- or
4960
4961 -- Const : constant Address := X'Address;
4962 -- ...
4963 -- for Y'Address use Const;
4964
4965 -- then we make an entry in the table for checking the size
4966 -- and alignment of the overlaying variable. We defer this
4967 -- check till after code generation to take full advantage
4968 -- of the annotation done by the back end.
4969
4970 -- If the entity has a generic type, the check will be
4971 -- performed in the instance if the actual type justifies
4972 -- it, and we do not insert the clause in the table to
4973 -- prevent spurious warnings.
4974
4975 -- Note: we used to test Comes_From_Source and only give
4976 -- this warning for source entities, but we have removed
4977 -- this test. It really seems bogus to generate overlays
4978 -- that would trigger this warning in generated code.
4979 -- Furthermore, by removing the test, we handle the
4980 -- aspect case properly.
4981
4982 if Present (O_Ent)
4983 and then Is_Object (O_Ent)
4984 and then not Is_Generic_Type (Etype (U_Ent))
4985 and then Address_Clause_Overlay_Warnings
4986 then
4987 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4988 end if;
4989 end;
4990
4991 -- Not a valid entity for an address clause
4992
4993 else
4994 Error_Msg_N ("address cannot be given for &", Nam);
4995 end if;
4996 end Address;
4997
4998 ---------------
4999 -- Alignment --
5000 ---------------
5001
5002 -- Alignment attribute definition clause
5003
5004 when Attribute_Alignment => Alignment : declare
5005 Align : constant Uint := Get_Alignment_Value (Expr);
5006 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
5007
5008 begin
5009 FOnly := True;
5010
5011 if not Is_Type (U_Ent)
5012 and then Ekind (U_Ent) /= E_Variable
5013 and then Ekind (U_Ent) /= E_Constant
5014 then
5015 Error_Msg_N ("alignment cannot be given for &", Nam);
5016
5017 elsif Duplicate_Clause then
5018 null;
5019
5020 elsif Align /= No_Uint then
5021 Set_Has_Alignment_Clause (U_Ent);
5022
5023 -- Tagged type case, check for attempt to set alignment to a
5024 -- value greater than Max_Align, and reset if so. This error
5025 -- is suppressed in ASIS mode to allow for different ASIS
5026 -- back ends or ASIS-based tools to query the illegal clause.
5027
5028 if Is_Tagged_Type (U_Ent)
5029 and then Align > Max_Align
5030 and then not ASIS_Mode
5031 then
5032 Error_Msg_N
5033 ("alignment for & set to Maximum_Aligment??", Nam);
5034 Set_Alignment (U_Ent, Max_Align);
5035
5036 -- All other cases
5037
5038 else
5039 Set_Alignment (U_Ent, Align);
5040 end if;
5041
5042 -- For an array type, U_Ent is the first subtype. In that case,
5043 -- also set the alignment of the anonymous base type so that
5044 -- other subtypes (such as the itypes for aggregates of the
5045 -- type) also receive the expected alignment.
5046
5047 if Is_Array_Type (U_Ent) then
5048 Set_Alignment (Base_Type (U_Ent), Align);
5049 end if;
5050 end if;
5051 end Alignment;
5052
5053 ---------------
5054 -- Bit_Order --
5055 ---------------
5056
5057 -- Bit_Order attribute definition clause
5058
5059 when Attribute_Bit_Order => Bit_Order : declare
5060 begin
5061 if not Is_Record_Type (U_Ent) then
5062 Error_Msg_N
5063 ("Bit_Order can only be defined for record type", Nam);
5064
5065 elsif Duplicate_Clause then
5066 null;
5067
5068 else
5069 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5070
5071 if Etype (Expr) = Any_Type then
5072 return;
5073
5074 elsif not Is_OK_Static_Expression (Expr) then
5075 Flag_Non_Static_Expr
5076 ("Bit_Order requires static expression!", Expr);
5077
5078 else
5079 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5080 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
5081 end if;
5082 end if;
5083 end if;
5084 end Bit_Order;
5085
5086 --------------------
5087 -- Component_Size --
5088 --------------------
5089
5090 -- Component_Size attribute definition clause
5091
5092 when Attribute_Component_Size => Component_Size_Case : declare
5093 Csize : constant Uint := Static_Integer (Expr);
5094 Ctyp : Entity_Id;
5095 Btype : Entity_Id;
5096 Biased : Boolean;
5097 New_Ctyp : Entity_Id;
5098 Decl : Node_Id;
5099
5100 begin
5101 if not Is_Array_Type (U_Ent) then
5102 Error_Msg_N ("component size requires array type", Nam);
5103 return;
5104 end if;
5105
5106 Btype := Base_Type (U_Ent);
5107 Ctyp := Component_Type (Btype);
5108
5109 if Duplicate_Clause then
5110 null;
5111
5112 elsif Rep_Item_Too_Early (Btype, N) then
5113 null;
5114
5115 elsif Csize /= No_Uint then
5116 Check_Size (Expr, Ctyp, Csize, Biased);
5117
5118 -- For the biased case, build a declaration for a subtype that
5119 -- will be used to represent the biased subtype that reflects
5120 -- the biased representation of components. We need the subtype
5121 -- to get proper conversions on referencing elements of the
5122 -- array.
5123
5124 if Biased then
5125 New_Ctyp :=
5126 Make_Defining_Identifier (Loc,
5127 Chars =>
5128 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
5129
5130 Decl :=
5131 Make_Subtype_Declaration (Loc,
5132 Defining_Identifier => New_Ctyp,
5133 Subtype_Indication =>
5134 New_Occurrence_Of (Component_Type (Btype), Loc));
5135
5136 Set_Parent (Decl, N);
5137 Analyze (Decl, Suppress => All_Checks);
5138
5139 Set_Has_Delayed_Freeze (New_Ctyp, False);
5140 Set_Esize (New_Ctyp, Csize);
5141 Set_RM_Size (New_Ctyp, Csize);
5142 Init_Alignment (New_Ctyp);
5143 Set_Is_Itype (New_Ctyp, True);
5144 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5145
5146 Set_Component_Type (Btype, New_Ctyp);
5147 Set_Biased (New_Ctyp, N, "component size clause");
5148 end if;
5149
5150 Set_Component_Size (Btype, Csize);
5151
5152 -- Deal with warning on overridden size
5153
5154 if Warn_On_Overridden_Size
5155 and then Has_Size_Clause (Ctyp)
5156 and then RM_Size (Ctyp) /= Csize
5157 then
5158 Error_Msg_NE
5159 ("component size overrides size clause for&?S?", N, Ctyp);
5160 end if;
5161
5162 Set_Has_Component_Size_Clause (Btype, True);
5163 Set_Has_Non_Standard_Rep (Btype, True);
5164 end if;
5165 end Component_Size_Case;
5166
5167 -----------------------
5168 -- Constant_Indexing --
5169 -----------------------
5170
5171 when Attribute_Constant_Indexing =>
5172 Check_Indexing_Functions;
5173
5174 ---------
5175 -- CPU --
5176 ---------
5177
5178 when Attribute_CPU => CPU :
5179 begin
5180 -- CPU attribute definition clause not allowed except from aspect
5181 -- specification.
5182
5183 if From_Aspect_Specification (N) then
5184 if not Is_Task_Type (U_Ent) then
5185 Error_Msg_N ("CPU can only be defined for task", Nam);
5186
5187 elsif Duplicate_Clause then
5188 null;
5189
5190 else
5191 -- The expression must be analyzed in the special manner
5192 -- described in "Handling of Default and Per-Object
5193 -- Expressions" in sem.ads.
5194
5195 -- The visibility to the discriminants must be restored
5196
5197 Push_Scope_And_Install_Discriminants (U_Ent);
5198 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5199 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5200
5201 if not Is_OK_Static_Expression (Expr) then
5202 Check_Restriction (Static_Priorities, Expr);
5203 end if;
5204 end if;
5205
5206 else
5207 Error_Msg_N
5208 ("attribute& cannot be set with definition clause", N);
5209 end if;
5210 end CPU;
5211
5212 ----------------------
5213 -- Default_Iterator --
5214 ----------------------
5215
5216 when Attribute_Default_Iterator => Default_Iterator : declare
5217 Func : Entity_Id;
5218 Typ : Entity_Id;
5219
5220 begin
5221 -- If target type is untagged, further checks are irrelevant
5222
5223 if not Is_Tagged_Type (U_Ent) then
5224 Error_Msg_N
5225 ("aspect Default_Iterator applies to tagged type", Nam);
5226 return;
5227 end if;
5228
5229 Check_Iterator_Functions;
5230
5231 Analyze (Expr);
5232
5233 if not Is_Entity_Name (Expr)
5234 or else Ekind (Entity (Expr)) /= E_Function
5235 then
5236 Error_Msg_N ("aspect Iterator must be a function", Expr);
5237 return;
5238 else
5239 Func := Entity (Expr);
5240 end if;
5241
5242 -- The type of the first parameter must be T, T'class, or a
5243 -- corresponding access type (5.5.1 (8/3). If function is
5244 -- parameterless label type accordingly.
5245
5246 if No (First_Formal (Func)) then
5247 Typ := Any_Type;
5248 else
5249 Typ := Etype (First_Formal (Func));
5250 end if;
5251
5252 if Typ = U_Ent
5253 or else Typ = Class_Wide_Type (U_Ent)
5254 or else (Is_Access_Type (Typ)
5255 and then Designated_Type (Typ) = U_Ent)
5256 or else (Is_Access_Type (Typ)
5257 and then Designated_Type (Typ) =
5258 Class_Wide_Type (U_Ent))
5259 then
5260 null;
5261
5262 else
5263 Error_Msg_NE
5264 ("Default Iterator must be a primitive of&", Func, U_Ent);
5265 end if;
5266 end Default_Iterator;
5267
5268 ------------------------
5269 -- Dispatching_Domain --
5270 ------------------------
5271
5272 when Attribute_Dispatching_Domain => Dispatching_Domain :
5273 begin
5274 -- Dispatching_Domain attribute definition clause not allowed
5275 -- except from aspect specification.
5276
5277 if From_Aspect_Specification (N) then
5278 if not Is_Task_Type (U_Ent) then
5279 Error_Msg_N
5280 ("Dispatching_Domain can only be defined for task", Nam);
5281
5282 elsif Duplicate_Clause then
5283 null;
5284
5285 else
5286 -- The expression must be analyzed in the special manner
5287 -- described in "Handling of Default and Per-Object
5288 -- Expressions" in sem.ads.
5289
5290 -- The visibility to the discriminants must be restored
5291
5292 Push_Scope_And_Install_Discriminants (U_Ent);
5293
5294 Preanalyze_Spec_Expression
5295 (Expr, RTE (RE_Dispatching_Domain));
5296
5297 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5298 end if;
5299
5300 else
5301 Error_Msg_N
5302 ("attribute& cannot be set with definition clause", N);
5303 end if;
5304 end Dispatching_Domain;
5305
5306 ------------------
5307 -- External_Tag --
5308 ------------------
5309
5310 when Attribute_External_Tag => External_Tag :
5311 begin
5312 if not Is_Tagged_Type (U_Ent) then
5313 Error_Msg_N ("should be a tagged type", Nam);
5314 end if;
5315
5316 if Duplicate_Clause then
5317 null;
5318
5319 else
5320 Analyze_And_Resolve (Expr, Standard_String);
5321
5322 if not Is_OK_Static_Expression (Expr) then
5323 Flag_Non_Static_Expr
5324 ("static string required for tag name!", Nam);
5325 end if;
5326
5327 if not Is_Library_Level_Entity (U_Ent) then
5328 Error_Msg_NE
5329 ("??non-unique external tag supplied for &", N, U_Ent);
5330 Error_Msg_N
5331 ("\??same external tag applies to all subprogram calls",
5332 N);
5333 Error_Msg_N
5334 ("\??corresponding internal tag cannot be obtained", N);
5335 end if;
5336 end if;
5337 end External_Tag;
5338
5339 --------------------------
5340 -- Implicit_Dereference --
5341 --------------------------
5342
5343 when Attribute_Implicit_Dereference =>
5344
5345 -- Legality checks already performed at the point of the type
5346 -- declaration, aspect is not delayed.
5347
5348 null;
5349
5350 -----------
5351 -- Input --
5352 -----------
5353
5354 when Attribute_Input =>
5355 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5356 Set_Has_Specified_Stream_Input (Ent);
5357
5358 ------------------------
5359 -- Interrupt_Priority --
5360 ------------------------
5361
5362 when Attribute_Interrupt_Priority => Interrupt_Priority :
5363 begin
5364 -- Interrupt_Priority attribute definition clause not allowed
5365 -- except from aspect specification.
5366
5367 if From_Aspect_Specification (N) then
5368 if not Is_Concurrent_Type (U_Ent) then
5369 Error_Msg_N
5370 ("Interrupt_Priority can only be defined for task and "
5371 & "protected object", Nam);
5372
5373 elsif Duplicate_Clause then
5374 null;
5375
5376 else
5377 -- The expression must be analyzed in the special manner
5378 -- described in "Handling of Default and Per-Object
5379 -- Expressions" in sem.ads.
5380
5381 -- The visibility to the discriminants must be restored
5382
5383 Push_Scope_And_Install_Discriminants (U_Ent);
5384
5385 Preanalyze_Spec_Expression
5386 (Expr, RTE (RE_Interrupt_Priority));
5387
5388 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5389
5390 -- Check the No_Task_At_Interrupt_Priority restriction
5391
5392 if Is_Task_Type (U_Ent) then
5393 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5394 end if;
5395 end if;
5396
5397 else
5398 Error_Msg_N
5399 ("attribute& cannot be set with definition clause", N);
5400 end if;
5401 end Interrupt_Priority;
5402
5403 --------------
5404 -- Iterable --
5405 --------------
5406
5407 when Attribute_Iterable =>
5408 Analyze (Expr);
5409
5410 if Nkind (Expr) /= N_Aggregate then
5411 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5412 end if;
5413
5414 declare
5415 Assoc : Node_Id;
5416
5417 begin
5418 Assoc := First (Component_Associations (Expr));
5419 while Present (Assoc) loop
5420 if not Is_Entity_Name (Expression (Assoc)) then
5421 Error_Msg_N ("value must be a function", Assoc);
5422 end if;
5423
5424 Next (Assoc);
5425 end loop;
5426 end;
5427
5428 ----------------------
5429 -- Iterator_Element --
5430 ----------------------
5431
5432 when Attribute_Iterator_Element =>
5433 Analyze (Expr);
5434
5435 if not Is_Entity_Name (Expr)
5436 or else not Is_Type (Entity (Expr))
5437 then
5438 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5439 end if;
5440
5441 -------------------
5442 -- Machine_Radix --
5443 -------------------
5444
5445 -- Machine radix attribute definition clause
5446
5447 when Attribute_Machine_Radix => Machine_Radix : declare
5448 Radix : constant Uint := Static_Integer (Expr);
5449
5450 begin
5451 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5452 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5453
5454 elsif Duplicate_Clause then
5455 null;
5456
5457 elsif Radix /= No_Uint then
5458 Set_Has_Machine_Radix_Clause (U_Ent);
5459 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5460
5461 if Radix = 2 then
5462 null;
5463
5464 elsif Radix = 10 then
5465 Set_Machine_Radix_10 (U_Ent);
5466
5467 -- The following error is suppressed in ASIS mode to allow for
5468 -- different ASIS back ends or ASIS-based tools to query the
5469 -- illegal clause.
5470
5471 elsif not ASIS_Mode then
5472 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5473 end if;
5474 end if;
5475 end Machine_Radix;
5476
5477 -----------------
5478 -- Object_Size --
5479 -----------------
5480
5481 -- Object_Size attribute definition clause
5482
5483 when Attribute_Object_Size => Object_Size : declare
5484 Size : constant Uint := Static_Integer (Expr);
5485
5486 Biased : Boolean;
5487 pragma Warnings (Off, Biased);
5488
5489 begin
5490 if not Is_Type (U_Ent) then
5491 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5492
5493 elsif Duplicate_Clause then
5494 null;
5495
5496 else
5497 Check_Size (Expr, U_Ent, Size, Biased);
5498
5499 -- The following errors are suppressed in ASIS mode to allow
5500 -- for different ASIS back ends or ASIS-based tools to query
5501 -- the illegal clause.
5502
5503 if ASIS_Mode then
5504 null;
5505
5506 elsif Is_Scalar_Type (U_Ent) then
5507 if Size /= 8 and then Size /= 16 and then Size /= 32
5508 and then UI_Mod (Size, 64) /= 0
5509 then
5510 Error_Msg_N
5511 ("Object_Size must be 8, 16, 32, or multiple of 64",
5512 Expr);
5513 end if;
5514
5515 elsif Size mod 8 /= 0 then
5516 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5517 end if;
5518
5519 Set_Esize (U_Ent, Size);
5520 Set_Has_Object_Size_Clause (U_Ent);
5521 Alignment_Check_For_Size_Change (U_Ent, Size);
5522 end if;
5523 end Object_Size;
5524
5525 ------------
5526 -- Output --
5527 ------------
5528
5529 when Attribute_Output =>
5530 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5531 Set_Has_Specified_Stream_Output (Ent);
5532
5533 --------------
5534 -- Priority --
5535 --------------
5536
5537 when Attribute_Priority => Priority :
5538 begin
5539 -- Priority attribute definition clause not allowed except from
5540 -- aspect specification.
5541
5542 if From_Aspect_Specification (N) then
5543 if not (Is_Concurrent_Type (U_Ent)
5544 or else Ekind (U_Ent) = E_Procedure)
5545 then
5546 Error_Msg_N
5547 ("Priority can only be defined for task and protected "
5548 & "object", Nam);
5549
5550 elsif Duplicate_Clause then
5551 null;
5552
5553 else
5554 -- The expression must be analyzed in the special manner
5555 -- described in "Handling of Default and Per-Object
5556 -- Expressions" in sem.ads.
5557
5558 -- The visibility to the discriminants must be restored
5559
5560 Push_Scope_And_Install_Discriminants (U_Ent);
5561 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5562 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5563
5564 if not Is_OK_Static_Expression (Expr) then
5565 Check_Restriction (Static_Priorities, Expr);
5566 end if;
5567 end if;
5568
5569 else
5570 Error_Msg_N
5571 ("attribute& cannot be set with definition clause", N);
5572 end if;
5573 end Priority;
5574
5575 ----------
5576 -- Read --
5577 ----------
5578
5579 when Attribute_Read =>
5580 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5581 Set_Has_Specified_Stream_Read (Ent);
5582
5583 --------------------------
5584 -- Scalar_Storage_Order --
5585 --------------------------
5586
5587 -- Scalar_Storage_Order attribute definition clause
5588
5589 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5590 begin
5591 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5592 Error_Msg_N
5593 ("Scalar_Storage_Order can only be defined for record or "
5594 & "array type", Nam);
5595
5596 elsif Duplicate_Clause then
5597 null;
5598
5599 else
5600 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5601
5602 if Etype (Expr) = Any_Type then
5603 return;
5604
5605 elsif not Is_OK_Static_Expression (Expr) then
5606 Flag_Non_Static_Expr
5607 ("Scalar_Storage_Order requires static expression!", Expr);
5608
5609 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5610
5611 -- Here for the case of a non-default (i.e. non-confirming)
5612 -- Scalar_Storage_Order attribute definition.
5613
5614 if Support_Nondefault_SSO_On_Target then
5615 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5616 else
5617 Error_Msg_N
5618 ("non-default Scalar_Storage_Order not supported on "
5619 & "target", Expr);
5620 end if;
5621 end if;
5622
5623 -- Clear SSO default indications since explicit setting of the
5624 -- order overrides the defaults.
5625
5626 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5627 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5628 end if;
5629 end Scalar_Storage_Order;
5630
5631 ----------
5632 -- Size --
5633 ----------
5634
5635 -- Size attribute definition clause
5636
5637 when Attribute_Size => Size : declare
5638 Size : constant Uint := Static_Integer (Expr);
5639 Etyp : Entity_Id;
5640 Biased : Boolean;
5641
5642 begin
5643 FOnly := True;
5644
5645 if Duplicate_Clause then
5646 null;
5647
5648 elsif not Is_Type (U_Ent)
5649 and then Ekind (U_Ent) /= E_Variable
5650 and then Ekind (U_Ent) /= E_Constant
5651 then
5652 Error_Msg_N ("size cannot be given for &", Nam);
5653
5654 elsif Is_Array_Type (U_Ent)
5655 and then not Is_Constrained (U_Ent)
5656 then
5657 Error_Msg_N
5658 ("size cannot be given for unconstrained array", Nam);
5659
5660 elsif Size /= No_Uint then
5661 if Is_Type (U_Ent) then
5662 Etyp := U_Ent;
5663 else
5664 Etyp := Etype (U_Ent);
5665 end if;
5666
5667 -- Check size, note that Gigi is in charge of checking that the
5668 -- size of an array or record type is OK. Also we do not check
5669 -- the size in the ordinary fixed-point case, since it is too
5670 -- early to do so (there may be subsequent small clause that
5671 -- affects the size). We can check the size if a small clause
5672 -- has already been given.
5673
5674 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5675 or else Has_Small_Clause (U_Ent)
5676 then
5677 Check_Size (Expr, Etyp, Size, Biased);
5678 Set_Biased (U_Ent, N, "size clause", Biased);
5679 end if;
5680
5681 -- For types set RM_Size and Esize if possible
5682
5683 if Is_Type (U_Ent) then
5684 Set_RM_Size (U_Ent, Size);
5685
5686 -- For elementary types, increase Object_Size to power of 2,
5687 -- but not less than a storage unit in any case (normally
5688 -- this means it will be byte addressable).
5689
5690 -- For all other types, nothing else to do, we leave Esize
5691 -- (object size) unset, the back end will set it from the
5692 -- size and alignment in an appropriate manner.
5693
5694 -- In both cases, we check whether the alignment must be
5695 -- reset in the wake of the size change.
5696
5697 if Is_Elementary_Type (U_Ent) then
5698 if Size <= System_Storage_Unit then
5699 Init_Esize (U_Ent, System_Storage_Unit);
5700 elsif Size <= 16 then
5701 Init_Esize (U_Ent, 16);
5702 elsif Size <= 32 then
5703 Init_Esize (U_Ent, 32);
5704 else
5705 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5706 end if;
5707
5708 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5709 else
5710 Alignment_Check_For_Size_Change (U_Ent, Size);
5711 end if;
5712
5713 -- For objects, set Esize only
5714
5715 else
5716 -- The following error is suppressed in ASIS mode to allow
5717 -- for different ASIS back ends or ASIS-based tools to query
5718 -- the illegal clause.
5719
5720 if Is_Elementary_Type (Etyp)
5721 and then Size /= System_Storage_Unit
5722 and then Size /= System_Storage_Unit * 2
5723 and then Size /= System_Storage_Unit * 4
5724 and then Size /= System_Storage_Unit * 8
5725 and then not ASIS_Mode
5726 then
5727 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5728 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5729 Error_Msg_N
5730 ("size for primitive object must be a power of 2 in "
5731 & "the range ^-^", N);
5732 end if;
5733
5734 Set_Esize (U_Ent, Size);
5735 end if;
5736
5737 Set_Has_Size_Clause (U_Ent);
5738 end if;
5739 end Size;
5740
5741 -----------
5742 -- Small --
5743 -----------
5744
5745 -- Small attribute definition clause
5746
5747 when Attribute_Small => Small : declare
5748 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5749 Small : Ureal;
5750
5751 begin
5752 Analyze_And_Resolve (Expr, Any_Real);
5753
5754 if Etype (Expr) = Any_Type then
5755 return;
5756
5757 elsif not Is_OK_Static_Expression (Expr) then
5758 Flag_Non_Static_Expr
5759 ("small requires static expression!", Expr);
5760 return;
5761
5762 else
5763 Small := Expr_Value_R (Expr);
5764
5765 if Small <= Ureal_0 then
5766 Error_Msg_N ("small value must be greater than zero", Expr);
5767 return;
5768 end if;
5769
5770 end if;
5771
5772 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5773 Error_Msg_N
5774 ("small requires an ordinary fixed point type", Nam);
5775
5776 elsif Has_Small_Clause (U_Ent) then
5777 Error_Msg_N ("small already given for &", Nam);
5778
5779 elsif Small > Delta_Value (U_Ent) then
5780 Error_Msg_N
5781 ("small value must not be greater than delta value", Nam);
5782
5783 else
5784 Set_Small_Value (U_Ent, Small);
5785 Set_Small_Value (Implicit_Base, Small);
5786 Set_Has_Small_Clause (U_Ent);
5787 Set_Has_Small_Clause (Implicit_Base);
5788 Set_Has_Non_Standard_Rep (Implicit_Base);
5789 end if;
5790 end Small;
5791
5792 ------------------
5793 -- Storage_Pool --
5794 ------------------
5795
5796 -- Storage_Pool attribute definition clause
5797
5798 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5799 Pool : Entity_Id;
5800 T : Entity_Id;
5801
5802 begin
5803 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5804 Error_Msg_N
5805 ("storage pool cannot be given for access-to-subprogram type",
5806 Nam);
5807 return;
5808
5809 elsif not
5810 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5811 then
5812 Error_Msg_N
5813 ("storage pool can only be given for access types", Nam);
5814 return;
5815
5816 elsif Is_Derived_Type (U_Ent) then
5817 Error_Msg_N
5818 ("storage pool cannot be given for a derived access type",
5819 Nam);
5820
5821 elsif Duplicate_Clause then
5822 return;
5823
5824 elsif Present (Associated_Storage_Pool (U_Ent)) then
5825 Error_Msg_N ("storage pool already given for &", Nam);
5826 return;
5827 end if;
5828
5829 -- Check for Storage_Size previously given
5830
5831 declare
5832 SS : constant Node_Id :=
5833 Get_Attribute_Definition_Clause
5834 (U_Ent, Attribute_Storage_Size);
5835 begin
5836 if Present (SS) then
5837 Check_Pool_Size_Clash (U_Ent, N, SS);
5838 end if;
5839 end;
5840
5841 -- Storage_Pool case
5842
5843 if Id = Attribute_Storage_Pool then
5844 Analyze_And_Resolve
5845 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5846
5847 -- In the Simple_Storage_Pool case, we allow a variable of any
5848 -- simple storage pool type, so we Resolve without imposing an
5849 -- expected type.
5850
5851 else
5852 Analyze_And_Resolve (Expr);
5853
5854 if not Present (Get_Rep_Pragma
5855 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5856 then
5857 Error_Msg_N
5858 ("expression must be of a simple storage pool type", Expr);
5859 end if;
5860 end if;
5861
5862 if not Denotes_Variable (Expr) then
5863 Error_Msg_N ("storage pool must be a variable", Expr);
5864 return;
5865 end if;
5866
5867 if Nkind (Expr) = N_Type_Conversion then
5868 T := Etype (Expression (Expr));
5869 else
5870 T := Etype (Expr);
5871 end if;
5872
5873 -- The Stack_Bounded_Pool is used internally for implementing
5874 -- access types with a Storage_Size. Since it only work properly
5875 -- when used on one specific type, we need to check that it is not
5876 -- hijacked improperly:
5877
5878 -- type T is access Integer;
5879 -- for T'Storage_Size use n;
5880 -- type Q is access Float;
5881 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5882
5883 if RTE_Available (RE_Stack_Bounded_Pool)
5884 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5885 then
5886 Error_Msg_N ("non-shareable internal Pool", Expr);
5887 return;
5888 end if;
5889
5890 -- If the argument is a name that is not an entity name, then
5891 -- we construct a renaming operation to define an entity of
5892 -- type storage pool.
5893
5894 if not Is_Entity_Name (Expr)
5895 and then Is_Object_Reference (Expr)
5896 then
5897 Pool := Make_Temporary (Loc, 'P', Expr);
5898
5899 declare
5900 Rnode : constant Node_Id :=
5901 Make_Object_Renaming_Declaration (Loc,
5902 Defining_Identifier => Pool,
5903 Subtype_Mark =>
5904 New_Occurrence_Of (Etype (Expr), Loc),
5905 Name => Expr);
5906
5907 begin
5908 -- If the attribute definition clause comes from an aspect
5909 -- clause, then insert the renaming before the associated
5910 -- entity's declaration, since the attribute clause has
5911 -- not yet been appended to the declaration list.
5912
5913 if From_Aspect_Specification (N) then
5914 Insert_Before (Parent (Entity (N)), Rnode);
5915 else
5916 Insert_Before (N, Rnode);
5917 end if;
5918
5919 Analyze (Rnode);
5920 Set_Associated_Storage_Pool (U_Ent, Pool);
5921 end;
5922
5923 elsif Is_Entity_Name (Expr) then
5924 Pool := Entity (Expr);
5925
5926 -- If pool is a renamed object, get original one. This can
5927 -- happen with an explicit renaming, and within instances.
5928
5929 while Present (Renamed_Object (Pool))
5930 and then Is_Entity_Name (Renamed_Object (Pool))
5931 loop
5932 Pool := Entity (Renamed_Object (Pool));
5933 end loop;
5934
5935 if Present (Renamed_Object (Pool))
5936 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5937 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5938 then
5939 Pool := Entity (Expression (Renamed_Object (Pool)));
5940 end if;
5941
5942 Set_Associated_Storage_Pool (U_Ent, Pool);
5943
5944 elsif Nkind (Expr) = N_Type_Conversion
5945 and then Is_Entity_Name (Expression (Expr))
5946 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5947 then
5948 Pool := Entity (Expression (Expr));
5949 Set_Associated_Storage_Pool (U_Ent, Pool);
5950
5951 else
5952 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5953 return;
5954 end if;
5955 end;
5956
5957 ------------------
5958 -- Storage_Size --
5959 ------------------
5960
5961 -- Storage_Size attribute definition clause
5962
5963 when Attribute_Storage_Size => Storage_Size : declare
5964 Btype : constant Entity_Id := Base_Type (U_Ent);
5965
5966 begin
5967 if Is_Task_Type (U_Ent) then
5968
5969 -- Check obsolescent (but never obsolescent if from aspect)
5970
5971 if not From_Aspect_Specification (N) then
5972 Check_Restriction (No_Obsolescent_Features, N);
5973
5974 if Warn_On_Obsolescent_Feature then
5975 Error_Msg_N
5976 ("?j?storage size clause for task is an obsolescent "
5977 & "feature (RM J.9)", N);
5978 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5979 end if;
5980 end if;
5981
5982 FOnly := True;
5983 end if;
5984
5985 if not Is_Access_Type (U_Ent)
5986 and then Ekind (U_Ent) /= E_Task_Type
5987 then
5988 Error_Msg_N ("storage size cannot be given for &", Nam);
5989
5990 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5991 Error_Msg_N
5992 ("storage size cannot be given for a derived access type",
5993 Nam);
5994
5995 elsif Duplicate_Clause then
5996 null;
5997
5998 else
5999 Analyze_And_Resolve (Expr, Any_Integer);
6000
6001 if Is_Access_Type (U_Ent) then
6002
6003 -- Check for Storage_Pool previously given
6004
6005 declare
6006 SP : constant Node_Id :=
6007 Get_Attribute_Definition_Clause
6008 (U_Ent, Attribute_Storage_Pool);
6009
6010 begin
6011 if Present (SP) then
6012 Check_Pool_Size_Clash (U_Ent, SP, N);
6013 end if;
6014 end;
6015
6016 -- Special case of for x'Storage_Size use 0
6017
6018 if Is_OK_Static_Expression (Expr)
6019 and then Expr_Value (Expr) = 0
6020 then
6021 Set_No_Pool_Assigned (Btype);
6022 end if;
6023 end if;
6024
6025 Set_Has_Storage_Size_Clause (Btype);
6026 end if;
6027 end Storage_Size;
6028
6029 -----------------
6030 -- Stream_Size --
6031 -----------------
6032
6033 when Attribute_Stream_Size => Stream_Size : declare
6034 Size : constant Uint := Static_Integer (Expr);
6035
6036 begin
6037 if Ada_Version <= Ada_95 then
6038 Check_Restriction (No_Implementation_Attributes, N);
6039 end if;
6040
6041 if Duplicate_Clause then
6042 null;
6043
6044 elsif Is_Elementary_Type (U_Ent) then
6045
6046 -- The following errors are suppressed in ASIS mode to allow
6047 -- for different ASIS back ends or ASIS-based tools to query
6048 -- the illegal clause.
6049
6050 if ASIS_Mode then
6051 null;
6052
6053 elsif Size /= System_Storage_Unit
6054 and then Size /= System_Storage_Unit * 2
6055 and then Size /= System_Storage_Unit * 4
6056 and then Size /= System_Storage_Unit * 8
6057 then
6058 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6059 Error_Msg_N
6060 ("stream size for elementary type must be a power of 2 "
6061 & "and at least ^", N);
6062
6063 elsif RM_Size (U_Ent) > Size then
6064 Error_Msg_Uint_1 := RM_Size (U_Ent);
6065 Error_Msg_N
6066 ("stream size for elementary type must be a power of 2 "
6067 & "and at least ^", N);
6068 end if;
6069
6070 Set_Has_Stream_Size_Clause (U_Ent);
6071
6072 else
6073 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6074 end if;
6075 end Stream_Size;
6076
6077 ----------------
6078 -- Value_Size --
6079 ----------------
6080
6081 -- Value_Size attribute definition clause
6082
6083 when Attribute_Value_Size => Value_Size : declare
6084 Size : constant Uint := Static_Integer (Expr);
6085 Biased : Boolean;
6086
6087 begin
6088 if not Is_Type (U_Ent) then
6089 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6090
6091 elsif Duplicate_Clause then
6092 null;
6093
6094 elsif Is_Array_Type (U_Ent)
6095 and then not Is_Constrained (U_Ent)
6096 then
6097 Error_Msg_N
6098 ("Value_Size cannot be given for unconstrained array", Nam);
6099
6100 else
6101 if Is_Elementary_Type (U_Ent) then
6102 Check_Size (Expr, U_Ent, Size, Biased);
6103 Set_Biased (U_Ent, N, "value size clause", Biased);
6104 end if;
6105
6106 Set_RM_Size (U_Ent, Size);
6107 end if;
6108 end Value_Size;
6109
6110 -----------------------
6111 -- Variable_Indexing --
6112 -----------------------
6113
6114 when Attribute_Variable_Indexing =>
6115 Check_Indexing_Functions;
6116
6117 -----------
6118 -- Write --
6119 -----------
6120
6121 when Attribute_Write =>
6122 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6123 Set_Has_Specified_Stream_Write (Ent);
6124
6125 -- All other attributes cannot be set
6126
6127 when others =>
6128 Error_Msg_N
6129 ("attribute& cannot be set with definition clause", N);
6130 end case;
6131
6132 -- The test for the type being frozen must be performed after any
6133 -- expression the clause has been analyzed since the expression itself
6134 -- might cause freezing that makes the clause illegal.
6135
6136 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6137 return;
6138 end if;
6139 end Analyze_Attribute_Definition_Clause;
6140
6141 ----------------------------
6142 -- Analyze_Code_Statement --
6143 ----------------------------
6144
6145 procedure Analyze_Code_Statement (N : Node_Id) is
6146 HSS : constant Node_Id := Parent (N);
6147 SBody : constant Node_Id := Parent (HSS);
6148 Subp : constant Entity_Id := Current_Scope;
6149 Stmt : Node_Id;
6150 Decl : Node_Id;
6151 StmtO : Node_Id;
6152 DeclO : Node_Id;
6153
6154 begin
6155 -- Accept foreign code statements for CodePeer. The analysis is skipped
6156 -- to avoid rejecting unrecognized constructs.
6157
6158 if CodePeer_Mode then
6159 Set_Analyzed (N);
6160 return;
6161 end if;
6162
6163 -- Analyze and check we get right type, note that this implements the
6164 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6165 -- the only way that Asm_Insn could possibly be visible.
6166
6167 Analyze_And_Resolve (Expression (N));
6168
6169 if Etype (Expression (N)) = Any_Type then
6170 return;
6171 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6172 Error_Msg_N ("incorrect type for code statement", N);
6173 return;
6174 end if;
6175
6176 Check_Code_Statement (N);
6177
6178 -- Make sure we appear in the handled statement sequence of a subprogram
6179 -- (RM 13.8(3)).
6180
6181 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6182 or else Nkind (SBody) /= N_Subprogram_Body
6183 then
6184 Error_Msg_N
6185 ("code statement can only appear in body of subprogram", N);
6186 return;
6187 end if;
6188
6189 -- Do remaining checks (RM 13.8(3)) if not already done
6190
6191 if not Is_Machine_Code_Subprogram (Subp) then
6192 Set_Is_Machine_Code_Subprogram (Subp);
6193
6194 -- No exception handlers allowed
6195
6196 if Present (Exception_Handlers (HSS)) then
6197 Error_Msg_N
6198 ("exception handlers not permitted in machine code subprogram",
6199 First (Exception_Handlers (HSS)));
6200 end if;
6201
6202 -- No declarations other than use clauses and pragmas (we allow
6203 -- certain internally generated declarations as well).
6204
6205 Decl := First (Declarations (SBody));
6206 while Present (Decl) loop
6207 DeclO := Original_Node (Decl);
6208 if Comes_From_Source (DeclO)
6209 and not Nkind_In (DeclO, N_Pragma,
6210 N_Use_Package_Clause,
6211 N_Use_Type_Clause,
6212 N_Implicit_Label_Declaration)
6213 then
6214 Error_Msg_N
6215 ("this declaration not allowed in machine code subprogram",
6216 DeclO);
6217 end if;
6218
6219 Next (Decl);
6220 end loop;
6221
6222 -- No statements other than code statements, pragmas, and labels.
6223 -- Again we allow certain internally generated statements.
6224
6225 -- In Ada 2012, qualified expressions are names, and the code
6226 -- statement is initially parsed as a procedure call.
6227
6228 Stmt := First (Statements (HSS));
6229 while Present (Stmt) loop
6230 StmtO := Original_Node (Stmt);
6231
6232 -- A procedure call transformed into a code statement is OK
6233
6234 if Ada_Version >= Ada_2012
6235 and then Nkind (StmtO) = N_Procedure_Call_Statement
6236 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6237 then
6238 null;
6239
6240 elsif Comes_From_Source (StmtO)
6241 and then not Nkind_In (StmtO, N_Pragma,
6242 N_Label,
6243 N_Code_Statement)
6244 then
6245 Error_Msg_N
6246 ("this statement is not allowed in machine code subprogram",
6247 StmtO);
6248 end if;
6249
6250 Next (Stmt);
6251 end loop;
6252 end if;
6253 end Analyze_Code_Statement;
6254
6255 -----------------------------------------------
6256 -- Analyze_Enumeration_Representation_Clause --
6257 -----------------------------------------------
6258
6259 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6260 Ident : constant Node_Id := Identifier (N);
6261 Aggr : constant Node_Id := Array_Aggregate (N);
6262 Enumtype : Entity_Id;
6263 Elit : Entity_Id;
6264 Expr : Node_Id;
6265 Assoc : Node_Id;
6266 Choice : Node_Id;
6267 Val : Uint;
6268
6269 Err : Boolean := False;
6270 -- Set True to avoid cascade errors and crashes on incorrect source code
6271
6272 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6273 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6274 -- Allowed range of universal integer (= allowed range of enum lit vals)
6275
6276 Min : Uint;
6277 Max : Uint;
6278 -- Minimum and maximum values of entries
6279
6280 Max_Node : Node_Id;
6281 -- Pointer to node for literal providing max value
6282
6283 begin
6284 if Ignore_Rep_Clauses then
6285 Kill_Rep_Clause (N);
6286 return;
6287 end if;
6288
6289 -- Ignore enumeration rep clauses by default in CodePeer mode,
6290 -- unless -gnatd.I is specified, as a work around for potential false
6291 -- positive messages.
6292
6293 if CodePeer_Mode and not Debug_Flag_Dot_II then
6294 return;
6295 end if;
6296
6297 -- First some basic error checks
6298
6299 Find_Type (Ident);
6300 Enumtype := Entity (Ident);
6301
6302 if Enumtype = Any_Type
6303 or else Rep_Item_Too_Early (Enumtype, N)
6304 then
6305 return;
6306 else
6307 Enumtype := Underlying_Type (Enumtype);
6308 end if;
6309
6310 if not Is_Enumeration_Type (Enumtype) then
6311 Error_Msg_NE
6312 ("enumeration type required, found}",
6313 Ident, First_Subtype (Enumtype));
6314 return;
6315 end if;
6316
6317 -- Ignore rep clause on generic actual type. This will already have
6318 -- been flagged on the template as an error, and this is the safest
6319 -- way to ensure we don't get a junk cascaded message in the instance.
6320
6321 if Is_Generic_Actual_Type (Enumtype) then
6322 return;
6323
6324 -- Type must be in current scope
6325
6326 elsif Scope (Enumtype) /= Current_Scope then
6327 Error_Msg_N ("type must be declared in this scope", Ident);
6328 return;
6329
6330 -- Type must be a first subtype
6331
6332 elsif not Is_First_Subtype (Enumtype) then
6333 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6334 return;
6335
6336 -- Ignore duplicate rep clause
6337
6338 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6339 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6340 return;
6341
6342 -- Don't allow rep clause for standard [wide_[wide_]]character
6343
6344 elsif Is_Standard_Character_Type (Enumtype) then
6345 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6346 return;
6347
6348 -- Check that the expression is a proper aggregate (no parentheses)
6349
6350 elsif Paren_Count (Aggr) /= 0 then
6351 Error_Msg
6352 ("extra parentheses surrounding aggregate not allowed",
6353 First_Sloc (Aggr));
6354 return;
6355
6356 -- All tests passed, so set rep clause in place
6357
6358 else
6359 Set_Has_Enumeration_Rep_Clause (Enumtype);
6360 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6361 end if;
6362
6363 -- Now we process the aggregate. Note that we don't use the normal
6364 -- aggregate code for this purpose, because we don't want any of the
6365 -- normal expansion activities, and a number of special semantic
6366 -- rules apply (including the component type being any integer type)
6367
6368 Elit := First_Literal (Enumtype);
6369
6370 -- First the positional entries if any
6371
6372 if Present (Expressions (Aggr)) then
6373 Expr := First (Expressions (Aggr));
6374 while Present (Expr) loop
6375 if No (Elit) then
6376 Error_Msg_N ("too many entries in aggregate", Expr);
6377 return;
6378 end if;
6379
6380 Val := Static_Integer (Expr);
6381
6382 -- Err signals that we found some incorrect entries processing
6383 -- the list. The final checks for completeness and ordering are
6384 -- skipped in this case.
6385
6386 if Val = No_Uint then
6387 Err := True;
6388
6389 elsif Val < Lo or else Hi < Val then
6390 Error_Msg_N ("value outside permitted range", Expr);
6391 Err := True;
6392 end if;
6393
6394 Set_Enumeration_Rep (Elit, Val);
6395 Set_Enumeration_Rep_Expr (Elit, Expr);
6396 Next (Expr);
6397 Next (Elit);
6398 end loop;
6399 end if;
6400
6401 -- Now process the named entries if present
6402
6403 if Present (Component_Associations (Aggr)) then
6404 Assoc := First (Component_Associations (Aggr));
6405 while Present (Assoc) loop
6406 Choice := First (Choices (Assoc));
6407
6408 if Present (Next (Choice)) then
6409 Error_Msg_N
6410 ("multiple choice not allowed here", Next (Choice));
6411 Err := True;
6412 end if;
6413
6414 if Nkind (Choice) = N_Others_Choice then
6415 Error_Msg_N ("others choice not allowed here", Choice);
6416 Err := True;
6417
6418 elsif Nkind (Choice) = N_Range then
6419
6420 -- ??? should allow zero/one element range here
6421
6422 Error_Msg_N ("range not allowed here", Choice);
6423 Err := True;
6424
6425 else
6426 Analyze_And_Resolve (Choice, Enumtype);
6427
6428 if Error_Posted (Choice) then
6429 Err := True;
6430 end if;
6431
6432 if not Err then
6433 if Is_Entity_Name (Choice)
6434 and then Is_Type (Entity (Choice))
6435 then
6436 Error_Msg_N ("subtype name not allowed here", Choice);
6437 Err := True;
6438
6439 -- ??? should allow static subtype with zero/one entry
6440
6441 elsif Etype (Choice) = Base_Type (Enumtype) then
6442 if not Is_OK_Static_Expression (Choice) then
6443 Flag_Non_Static_Expr
6444 ("non-static expression used for choice!", Choice);
6445 Err := True;
6446
6447 else
6448 Elit := Expr_Value_E (Choice);
6449
6450 if Present (Enumeration_Rep_Expr (Elit)) then
6451 Error_Msg_Sloc :=
6452 Sloc (Enumeration_Rep_Expr (Elit));
6453 Error_Msg_NE
6454 ("representation for& previously given#",
6455 Choice, Elit);
6456 Err := True;
6457 end if;
6458
6459 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6460
6461 Expr := Expression (Assoc);
6462 Val := Static_Integer (Expr);
6463
6464 if Val = No_Uint then
6465 Err := True;
6466
6467 elsif Val < Lo or else Hi < Val then
6468 Error_Msg_N ("value outside permitted range", Expr);
6469 Err := True;
6470 end if;
6471
6472 Set_Enumeration_Rep (Elit, Val);
6473 end if;
6474 end if;
6475 end if;
6476 end if;
6477
6478 Next (Assoc);
6479 end loop;
6480 end if;
6481
6482 -- Aggregate is fully processed. Now we check that a full set of
6483 -- representations was given, and that they are in range and in order.
6484 -- These checks are only done if no other errors occurred.
6485
6486 if not Err then
6487 Min := No_Uint;
6488 Max := No_Uint;
6489
6490 Elit := First_Literal (Enumtype);
6491 while Present (Elit) loop
6492 if No (Enumeration_Rep_Expr (Elit)) then
6493 Error_Msg_NE ("missing representation for&!", N, Elit);
6494
6495 else
6496 Val := Enumeration_Rep (Elit);
6497
6498 if Min = No_Uint then
6499 Min := Val;
6500 end if;
6501
6502 if Val /= No_Uint then
6503 if Max /= No_Uint and then Val <= Max then
6504 Error_Msg_NE
6505 ("enumeration value for& not ordered!",
6506 Enumeration_Rep_Expr (Elit), Elit);
6507 end if;
6508
6509 Max_Node := Enumeration_Rep_Expr (Elit);
6510 Max := Val;
6511 end if;
6512
6513 -- If there is at least one literal whose representation is not
6514 -- equal to the Pos value, then note that this enumeration type
6515 -- has a non-standard representation.
6516
6517 if Val /= Enumeration_Pos (Elit) then
6518 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6519 end if;
6520 end if;
6521
6522 Next (Elit);
6523 end loop;
6524
6525 -- Now set proper size information
6526
6527 declare
6528 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6529
6530 begin
6531 if Has_Size_Clause (Enumtype) then
6532
6533 -- All OK, if size is OK now
6534
6535 if RM_Size (Enumtype) >= Minsize then
6536 null;
6537
6538 else
6539 -- Try if we can get by with biasing
6540
6541 Minsize :=
6542 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6543
6544 -- Error message if even biasing does not work
6545
6546 if RM_Size (Enumtype) < Minsize then
6547 Error_Msg_Uint_1 := RM_Size (Enumtype);
6548 Error_Msg_Uint_2 := Max;
6549 Error_Msg_N
6550 ("previously given size (^) is too small "
6551 & "for this value (^)", Max_Node);
6552
6553 -- If biasing worked, indicate that we now have biased rep
6554
6555 else
6556 Set_Biased
6557 (Enumtype, Size_Clause (Enumtype), "size clause");
6558 end if;
6559 end if;
6560
6561 else
6562 Set_RM_Size (Enumtype, Minsize);
6563 Set_Enum_Esize (Enumtype);
6564 end if;
6565
6566 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6567 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6568 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6569 end;
6570 end if;
6571
6572 -- We repeat the too late test in case it froze itself
6573
6574 if Rep_Item_Too_Late (Enumtype, N) then
6575 null;
6576 end if;
6577 end Analyze_Enumeration_Representation_Clause;
6578
6579 ----------------------------
6580 -- Analyze_Free_Statement --
6581 ----------------------------
6582
6583 procedure Analyze_Free_Statement (N : Node_Id) is
6584 begin
6585 Analyze (Expression (N));
6586 end Analyze_Free_Statement;
6587
6588 ---------------------------
6589 -- Analyze_Freeze_Entity --
6590 ---------------------------
6591
6592 procedure Analyze_Freeze_Entity (N : Node_Id) is
6593 begin
6594 Freeze_Entity_Checks (N);
6595 end Analyze_Freeze_Entity;
6596
6597 -----------------------------------
6598 -- Analyze_Freeze_Generic_Entity --
6599 -----------------------------------
6600
6601 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6602 begin
6603 Freeze_Entity_Checks (N);
6604 end Analyze_Freeze_Generic_Entity;
6605
6606 ------------------------------------------
6607 -- Analyze_Record_Representation_Clause --
6608 ------------------------------------------
6609
6610 -- Note: we check as much as we can here, but we can't do any checks
6611 -- based on the position values (e.g. overlap checks) until freeze time
6612 -- because especially in Ada 2005 (machine scalar mode), the processing
6613 -- for non-standard bit order can substantially change the positions.
6614 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6615 -- for the remainder of this processing.
6616
6617 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6618 Ident : constant Node_Id := Identifier (N);
6619 Biased : Boolean;
6620 CC : Node_Id;
6621 Comp : Entity_Id;
6622 Fbit : Uint;
6623 Hbit : Uint := Uint_0;
6624 Lbit : Uint;
6625 Ocomp : Entity_Id;
6626 Posit : Uint;
6627 Rectype : Entity_Id;
6628 Recdef : Node_Id;
6629
6630 function Is_Inherited (Comp : Entity_Id) return Boolean;
6631 -- True if Comp is an inherited component in a record extension
6632
6633 ------------------
6634 -- Is_Inherited --
6635 ------------------
6636
6637 function Is_Inherited (Comp : Entity_Id) return Boolean is
6638 Comp_Base : Entity_Id;
6639
6640 begin
6641 if Ekind (Rectype) = E_Record_Subtype then
6642 Comp_Base := Original_Record_Component (Comp);
6643 else
6644 Comp_Base := Comp;
6645 end if;
6646
6647 return Comp_Base /= Original_Record_Component (Comp_Base);
6648 end Is_Inherited;
6649
6650 -- Local variables
6651
6652 Is_Record_Extension : Boolean;
6653 -- True if Rectype is a record extension
6654
6655 CR_Pragma : Node_Id := Empty;
6656 -- Points to N_Pragma node if Complete_Representation pragma present
6657
6658 -- Start of processing for Analyze_Record_Representation_Clause
6659
6660 begin
6661 if Ignore_Rep_Clauses then
6662 Kill_Rep_Clause (N);
6663 return;
6664 end if;
6665
6666 Find_Type (Ident);
6667 Rectype := Entity (Ident);
6668
6669 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6670 return;
6671 else
6672 Rectype := Underlying_Type (Rectype);
6673 end if;
6674
6675 -- First some basic error checks
6676
6677 if not Is_Record_Type (Rectype) then
6678 Error_Msg_NE
6679 ("record type required, found}", Ident, First_Subtype (Rectype));
6680 return;
6681
6682 elsif Scope (Rectype) /= Current_Scope then
6683 Error_Msg_N ("type must be declared in this scope", N);
6684 return;
6685
6686 elsif not Is_First_Subtype (Rectype) then
6687 Error_Msg_N ("cannot give record rep clause for subtype", N);
6688 return;
6689
6690 elsif Has_Record_Rep_Clause (Rectype) then
6691 Error_Msg_N ("duplicate record rep clause ignored", N);
6692 return;
6693
6694 elsif Rep_Item_Too_Late (Rectype, N) then
6695 return;
6696 end if;
6697
6698 -- We know we have a first subtype, now possibly go to the anonymous
6699 -- base type to determine whether Rectype is a record extension.
6700
6701 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6702 Is_Record_Extension :=
6703 Nkind (Recdef) = N_Derived_Type_Definition
6704 and then Present (Record_Extension_Part (Recdef));
6705
6706 if Present (Mod_Clause (N)) then
6707 declare
6708 Loc : constant Source_Ptr := Sloc (N);
6709 M : constant Node_Id := Mod_Clause (N);
6710 P : constant List_Id := Pragmas_Before (M);
6711 AtM_Nod : Node_Id;
6712
6713 Mod_Val : Uint;
6714 pragma Warnings (Off, Mod_Val);
6715
6716 begin
6717 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6718
6719 if Warn_On_Obsolescent_Feature then
6720 Error_Msg_N
6721 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6722 Error_Msg_N
6723 ("\?j?use alignment attribute definition clause instead", N);
6724 end if;
6725
6726 if Present (P) then
6727 Analyze_List (P);
6728 end if;
6729
6730 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6731 -- the Mod clause into an alignment clause anyway, so that the
6732 -- back end can compute and back-annotate properly the size and
6733 -- alignment of types that may include this record.
6734
6735 -- This seems dubious, this destroys the source tree in a manner
6736 -- not detectable by ASIS ???
6737
6738 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6739 AtM_Nod :=
6740 Make_Attribute_Definition_Clause (Loc,
6741 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6742 Chars => Name_Alignment,
6743 Expression => Relocate_Node (Expression (M)));
6744
6745 Set_From_At_Mod (AtM_Nod);
6746 Insert_After (N, AtM_Nod);
6747 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6748 Set_Mod_Clause (N, Empty);
6749
6750 else
6751 -- Get the alignment value to perform error checking
6752
6753 Mod_Val := Get_Alignment_Value (Expression (M));
6754 end if;
6755 end;
6756 end if;
6757
6758 -- For untagged types, clear any existing component clauses for the
6759 -- type. If the type is derived, this is what allows us to override
6760 -- a rep clause for the parent. For type extensions, the representation
6761 -- of the inherited components is inherited, so we want to keep previous
6762 -- component clauses for completeness.
6763
6764 if not Is_Tagged_Type (Rectype) then
6765 Comp := First_Component_Or_Discriminant (Rectype);
6766 while Present (Comp) loop
6767 Set_Component_Clause (Comp, Empty);
6768 Next_Component_Or_Discriminant (Comp);
6769 end loop;
6770 end if;
6771
6772 -- All done if no component clauses
6773
6774 CC := First (Component_Clauses (N));
6775
6776 if No (CC) then
6777 return;
6778 end if;
6779
6780 -- A representation like this applies to the base type
6781
6782 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6783 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6784 Set_Has_Specified_Layout (Base_Type (Rectype));
6785
6786 -- Process the component clauses
6787
6788 while Present (CC) loop
6789
6790 -- Pragma
6791
6792 if Nkind (CC) = N_Pragma then
6793 Analyze (CC);
6794
6795 -- The only pragma of interest is Complete_Representation
6796
6797 if Pragma_Name (CC) = Name_Complete_Representation then
6798 CR_Pragma := CC;
6799 end if;
6800
6801 -- Processing for real component clause
6802
6803 else
6804 Posit := Static_Integer (Position (CC));
6805 Fbit := Static_Integer (First_Bit (CC));
6806 Lbit := Static_Integer (Last_Bit (CC));
6807
6808 if Posit /= No_Uint
6809 and then Fbit /= No_Uint
6810 and then Lbit /= No_Uint
6811 then
6812 if Posit < 0 then
6813 Error_Msg_N ("position cannot be negative", Position (CC));
6814
6815 elsif Fbit < 0 then
6816 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
6817
6818 -- The Last_Bit specified in a component clause must not be
6819 -- less than the First_Bit minus one (RM-13.5.1(10)).
6820
6821 elsif Lbit < Fbit - 1 then
6822 Error_Msg_N
6823 ("last bit cannot be less than first bit minus one",
6824 Last_Bit (CC));
6825
6826 -- Values look OK, so find the corresponding record component
6827 -- Even though the syntax allows an attribute reference for
6828 -- implementation-defined components, GNAT does not allow the
6829 -- tag to get an explicit position.
6830
6831 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6832 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6833 Error_Msg_N ("position of tag cannot be specified", CC);
6834 else
6835 Error_Msg_N ("illegal component name", CC);
6836 end if;
6837
6838 else
6839 Comp := First_Entity (Rectype);
6840 while Present (Comp) loop
6841 exit when Chars (Comp) = Chars (Component_Name (CC));
6842 Next_Entity (Comp);
6843 end loop;
6844
6845 if No (Comp) then
6846
6847 -- Maybe component of base type that is absent from
6848 -- statically constrained first subtype.
6849
6850 Comp := First_Entity (Base_Type (Rectype));
6851 while Present (Comp) loop
6852 exit when Chars (Comp) = Chars (Component_Name (CC));
6853 Next_Entity (Comp);
6854 end loop;
6855 end if;
6856
6857 if No (Comp) then
6858 Error_Msg_N
6859 ("component clause is for non-existent field", CC);
6860
6861 -- Ada 2012 (AI05-0026): Any name that denotes a
6862 -- discriminant of an object of an unchecked union type
6863 -- shall not occur within a record_representation_clause.
6864
6865 -- The general restriction of using record rep clauses on
6866 -- Unchecked_Union types has now been lifted. Since it is
6867 -- possible to introduce a record rep clause which mentions
6868 -- the discriminant of an Unchecked_Union in non-Ada 2012
6869 -- code, this check is applied to all versions of the
6870 -- language.
6871
6872 elsif Ekind (Comp) = E_Discriminant
6873 and then Is_Unchecked_Union (Rectype)
6874 then
6875 Error_Msg_N
6876 ("cannot reference discriminant of unchecked union",
6877 Component_Name (CC));
6878
6879 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6880 Error_Msg_NE
6881 ("component clause not allowed for inherited "
6882 & "component&", CC, Comp);
6883
6884 elsif Present (Component_Clause (Comp)) then
6885
6886 -- Diagnose duplicate rep clause, or check consistency
6887 -- if this is an inherited component. In a double fault,
6888 -- there may be a duplicate inconsistent clause for an
6889 -- inherited component.
6890
6891 if Scope (Original_Record_Component (Comp)) = Rectype
6892 or else Parent (Component_Clause (Comp)) = N
6893 then
6894 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6895 Error_Msg_N ("component clause previously given#", CC);
6896
6897 else
6898 declare
6899 Rep1 : constant Node_Id := Component_Clause (Comp);
6900 begin
6901 if Intval (Position (Rep1)) /=
6902 Intval (Position (CC))
6903 or else Intval (First_Bit (Rep1)) /=
6904 Intval (First_Bit (CC))
6905 or else Intval (Last_Bit (Rep1)) /=
6906 Intval (Last_Bit (CC))
6907 then
6908 Error_Msg_N
6909 ("component clause inconsistent with "
6910 & "representation of ancestor", CC);
6911
6912 elsif Warn_On_Redundant_Constructs then
6913 Error_Msg_N
6914 ("?r?redundant confirming component clause "
6915 & "for component!", CC);
6916 end if;
6917 end;
6918 end if;
6919
6920 -- Normal case where this is the first component clause we
6921 -- have seen for this entity, so set it up properly.
6922
6923 else
6924 -- Make reference for field in record rep clause and set
6925 -- appropriate entity field in the field identifier.
6926
6927 Generate_Reference
6928 (Comp, Component_Name (CC), Set_Ref => False);
6929 Set_Entity (Component_Name (CC), Comp);
6930
6931 -- Update Fbit and Lbit to the actual bit number
6932
6933 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6934 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6935
6936 if Has_Size_Clause (Rectype)
6937 and then RM_Size (Rectype) <= Lbit
6938 then
6939 Error_Msg_N
6940 ("bit number out of range of specified size",
6941 Last_Bit (CC));
6942 else
6943 Set_Component_Clause (Comp, CC);
6944 Set_Component_Bit_Offset (Comp, Fbit);
6945 Set_Esize (Comp, 1 + (Lbit - Fbit));
6946 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6947 Set_Normalized_Position (Comp, Fbit / SSU);
6948
6949 if Warn_On_Overridden_Size
6950 and then Has_Size_Clause (Etype (Comp))
6951 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6952 then
6953 Error_Msg_NE
6954 ("?S?component size overrides size clause for&",
6955 Component_Name (CC), Etype (Comp));
6956 end if;
6957
6958 -- This information is also set in the corresponding
6959 -- component of the base type, found by accessing the
6960 -- Original_Record_Component link if it is present.
6961
6962 Ocomp := Original_Record_Component (Comp);
6963
6964 if Hbit < Lbit then
6965 Hbit := Lbit;
6966 end if;
6967
6968 Check_Size
6969 (Component_Name (CC),
6970 Etype (Comp),
6971 Esize (Comp),
6972 Biased);
6973
6974 Set_Biased
6975 (Comp, First_Node (CC), "component clause", Biased);
6976
6977 if Present (Ocomp) then
6978 Set_Component_Clause (Ocomp, CC);
6979 Set_Component_Bit_Offset (Ocomp, Fbit);
6980 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6981 Set_Normalized_Position (Ocomp, Fbit / SSU);
6982 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6983
6984 Set_Normalized_Position_Max
6985 (Ocomp, Normalized_Position (Ocomp));
6986
6987 -- Note: we don't use Set_Biased here, because we
6988 -- already gave a warning above if needed, and we
6989 -- would get a duplicate for the same name here.
6990
6991 Set_Has_Biased_Representation
6992 (Ocomp, Has_Biased_Representation (Comp));
6993 end if;
6994
6995 if Esize (Comp) < 0 then
6996 Error_Msg_N ("component size is negative", CC);
6997 end if;
6998 end if;
6999 end if;
7000 end if;
7001 end if;
7002 end if;
7003
7004 Next (CC);
7005 end loop;
7006
7007 -- Check missing components if Complete_Representation pragma appeared
7008
7009 if Present (CR_Pragma) then
7010 Comp := First_Component_Or_Discriminant (Rectype);
7011 while Present (Comp) loop
7012 if No (Component_Clause (Comp)) then
7013 Error_Msg_NE
7014 ("missing component clause for &", CR_Pragma, Comp);
7015 end if;
7016
7017 Next_Component_Or_Discriminant (Comp);
7018 end loop;
7019
7020 -- Give missing components warning if required
7021
7022 elsif Warn_On_Unrepped_Components then
7023 declare
7024 Num_Repped_Components : Nat := 0;
7025 Num_Unrepped_Components : Nat := 0;
7026
7027 begin
7028 -- First count number of repped and unrepped components
7029
7030 Comp := First_Component_Or_Discriminant (Rectype);
7031 while Present (Comp) loop
7032 if Present (Component_Clause (Comp)) then
7033 Num_Repped_Components := Num_Repped_Components + 1;
7034 else
7035 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7036 end if;
7037
7038 Next_Component_Or_Discriminant (Comp);
7039 end loop;
7040
7041 -- We are only interested in the case where there is at least one
7042 -- unrepped component, and at least half the components have rep
7043 -- clauses. We figure that if less than half have them, then the
7044 -- partial rep clause is really intentional. If the component
7045 -- type has no underlying type set at this point (as for a generic
7046 -- formal type), we don't know enough to give a warning on the
7047 -- component.
7048
7049 if Num_Unrepped_Components > 0
7050 and then Num_Unrepped_Components < Num_Repped_Components
7051 then
7052 Comp := First_Component_Or_Discriminant (Rectype);
7053 while Present (Comp) loop
7054 if No (Component_Clause (Comp))
7055 and then Comes_From_Source (Comp)
7056 and then Present (Underlying_Type (Etype (Comp)))
7057 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
7058 or else Size_Known_At_Compile_Time
7059 (Underlying_Type (Etype (Comp))))
7060 and then not Has_Warnings_Off (Rectype)
7061
7062 -- Ignore discriminant in unchecked union, since it is
7063 -- not there, and cannot have a component clause.
7064
7065 and then (not Is_Unchecked_Union (Rectype)
7066 or else Ekind (Comp) /= E_Discriminant)
7067 then
7068 Error_Msg_Sloc := Sloc (Comp);
7069 Error_Msg_NE
7070 ("?C?no component clause given for & declared #",
7071 N, Comp);
7072 end if;
7073
7074 Next_Component_Or_Discriminant (Comp);
7075 end loop;
7076 end if;
7077 end;
7078 end if;
7079 end Analyze_Record_Representation_Clause;
7080
7081 -------------------------------------
7082 -- Build_Discrete_Static_Predicate --
7083 -------------------------------------
7084
7085 procedure Build_Discrete_Static_Predicate
7086 (Typ : Entity_Id;
7087 Expr : Node_Id;
7088 Nam : Name_Id)
7089 is
7090 Loc : constant Source_Ptr := Sloc (Expr);
7091
7092 Non_Static : exception;
7093 -- Raised if something non-static is found
7094
7095 Btyp : constant Entity_Id := Base_Type (Typ);
7096
7097 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7098 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7099 -- Low bound and high bound value of base type of Typ
7100
7101 TLo : Uint;
7102 THi : Uint;
7103 -- Bounds for constructing the static predicate. We use the bound of the
7104 -- subtype if it is static, otherwise the corresponding base type bound.
7105 -- Note: a non-static subtype can have a static predicate.
7106
7107 type REnt is record
7108 Lo, Hi : Uint;
7109 end record;
7110 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7111 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7112 -- value.
7113
7114 type RList is array (Nat range <>) of REnt;
7115 -- A list of ranges. The ranges are sorted in increasing order, and are
7116 -- disjoint (there is a gap of at least one value between each range in
7117 -- the table). A value is in the set of ranges in Rlist if it lies
7118 -- within one of these ranges.
7119
7120 False_Range : constant RList :=
7121 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7122 -- An empty set of ranges represents a range list that can never be
7123 -- satisfied, since there are no ranges in which the value could lie,
7124 -- so it does not lie in any of them. False_Range is a canonical value
7125 -- for this empty set, but general processing should test for an Rlist
7126 -- with length zero (see Is_False predicate), since other null ranges
7127 -- may appear which must be treated as False.
7128
7129 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7130 -- Range representing True, value must be in the base range
7131
7132 function "and" (Left : RList; Right : RList) return RList;
7133 -- And's together two range lists, returning a range list. This is a set
7134 -- intersection operation.
7135
7136 function "or" (Left : RList; Right : RList) return RList;
7137 -- Or's together two range lists, returning a range list. This is a set
7138 -- union operation.
7139
7140 function "not" (Right : RList) return RList;
7141 -- Returns complement of a given range list, i.e. a range list
7142 -- representing all the values in TLo .. THi that are not in the input
7143 -- operand Right.
7144
7145 function Build_Val (V : Uint) return Node_Id;
7146 -- Return an analyzed N_Identifier node referencing this value, suitable
7147 -- for use as an entry in the Static_Discrte_Predicate list. This node
7148 -- is typed with the base type.
7149
7150 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7151 -- Return an analyzed N_Range node referencing this range, suitable for
7152 -- use as an entry in the Static_Discrete_Predicate list. This node is
7153 -- typed with the base type.
7154
7155 function Get_RList (Exp : Node_Id) return RList;
7156 -- This is a recursive routine that converts the given expression into a
7157 -- list of ranges, suitable for use in building the static predicate.
7158
7159 function Is_False (R : RList) return Boolean;
7160 pragma Inline (Is_False);
7161 -- Returns True if the given range list is empty, and thus represents a
7162 -- False list of ranges that can never be satisfied.
7163
7164 function Is_True (R : RList) return Boolean;
7165 -- Returns True if R trivially represents the True predicate by having a
7166 -- single range from BLo to BHi.
7167
7168 function Is_Type_Ref (N : Node_Id) return Boolean;
7169 pragma Inline (Is_Type_Ref);
7170 -- Returns if True if N is a reference to the type for the predicate in
7171 -- the expression (i.e. if it is an identifier whose Chars field matches
7172 -- the Nam given in the call). N must not be parenthesized, if the type
7173 -- name appears in parens, this routine will return False.
7174
7175 function Lo_Val (N : Node_Id) return Uint;
7176 -- Given an entry from a Static_Discrete_Predicate list that is either
7177 -- a static expression or static range, gets either the expression value
7178 -- or the low bound of the range.
7179
7180 function Hi_Val (N : Node_Id) return Uint;
7181 -- Given an entry from a Static_Discrete_Predicate list that is either
7182 -- a static expression or static range, gets either the expression value
7183 -- or the high bound of the range.
7184
7185 function Membership_Entry (N : Node_Id) return RList;
7186 -- Given a single membership entry (range, value, or subtype), returns
7187 -- the corresponding range list. Raises Static_Error if not static.
7188
7189 function Membership_Entries (N : Node_Id) return RList;
7190 -- Given an element on an alternatives list of a membership operation,
7191 -- returns the range list corresponding to this entry and all following
7192 -- entries (i.e. returns the "or" of this list of values).
7193
7194 function Stat_Pred (Typ : Entity_Id) return RList;
7195 -- Given a type, if it has a static predicate, then return the predicate
7196 -- as a range list, otherwise raise Non_Static.
7197
7198 -----------
7199 -- "and" --
7200 -----------
7201
7202 function "and" (Left : RList; Right : RList) return RList is
7203 FEnt : REnt;
7204 -- First range of result
7205
7206 SLeft : Nat := Left'First;
7207 -- Start of rest of left entries
7208
7209 SRight : Nat := Right'First;
7210 -- Start of rest of right entries
7211
7212 begin
7213 -- If either range is True, return the other
7214
7215 if Is_True (Left) then
7216 return Right;
7217 elsif Is_True (Right) then
7218 return Left;
7219 end if;
7220
7221 -- If either range is False, return False
7222
7223 if Is_False (Left) or else Is_False (Right) then
7224 return False_Range;
7225 end if;
7226
7227 -- Loop to remove entries at start that are disjoint, and thus just
7228 -- get discarded from the result entirely.
7229
7230 loop
7231 -- If no operands left in either operand, result is false
7232
7233 if SLeft > Left'Last or else SRight > Right'Last then
7234 return False_Range;
7235
7236 -- Discard first left operand entry if disjoint with right
7237
7238 elsif Left (SLeft).Hi < Right (SRight).Lo then
7239 SLeft := SLeft + 1;
7240
7241 -- Discard first right operand entry if disjoint with left
7242
7243 elsif Right (SRight).Hi < Left (SLeft).Lo then
7244 SRight := SRight + 1;
7245
7246 -- Otherwise we have an overlapping entry
7247
7248 else
7249 exit;
7250 end if;
7251 end loop;
7252
7253 -- Now we have two non-null operands, and first entries overlap. The
7254 -- first entry in the result will be the overlapping part of these
7255 -- two entries.
7256
7257 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7258 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7259
7260 -- Now we can remove the entry that ended at a lower value, since its
7261 -- contribution is entirely contained in Fent.
7262
7263 if Left (SLeft).Hi <= Right (SRight).Hi then
7264 SLeft := SLeft + 1;
7265 else
7266 SRight := SRight + 1;
7267 end if;
7268
7269 -- Compute result by concatenating this first entry with the "and" of
7270 -- the remaining parts of the left and right operands. Note that if
7271 -- either of these is empty, "and" will yield empty, so that we will
7272 -- end up with just Fent, which is what we want in that case.
7273
7274 return
7275 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7276 end "and";
7277
7278 -----------
7279 -- "not" --
7280 -----------
7281
7282 function "not" (Right : RList) return RList is
7283 begin
7284 -- Return True if False range
7285
7286 if Is_False (Right) then
7287 return True_Range;
7288 end if;
7289
7290 -- Return False if True range
7291
7292 if Is_True (Right) then
7293 return False_Range;
7294 end if;
7295
7296 -- Here if not trivial case
7297
7298 declare
7299 Result : RList (1 .. Right'Length + 1);
7300 -- May need one more entry for gap at beginning and end
7301
7302 Count : Nat := 0;
7303 -- Number of entries stored in Result
7304
7305 begin
7306 -- Gap at start
7307
7308 if Right (Right'First).Lo > TLo then
7309 Count := Count + 1;
7310 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7311 end if;
7312
7313 -- Gaps between ranges
7314
7315 for J in Right'First .. Right'Last - 1 loop
7316 Count := Count + 1;
7317 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7318 end loop;
7319
7320 -- Gap at end
7321
7322 if Right (Right'Last).Hi < THi then
7323 Count := Count + 1;
7324 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7325 end if;
7326
7327 return Result (1 .. Count);
7328 end;
7329 end "not";
7330
7331 ----------
7332 -- "or" --
7333 ----------
7334
7335 function "or" (Left : RList; Right : RList) return RList is
7336 FEnt : REnt;
7337 -- First range of result
7338
7339 SLeft : Nat := Left'First;
7340 -- Start of rest of left entries
7341
7342 SRight : Nat := Right'First;
7343 -- Start of rest of right entries
7344
7345 begin
7346 -- If either range is True, return True
7347
7348 if Is_True (Left) or else Is_True (Right) then
7349 return True_Range;
7350 end if;
7351
7352 -- If either range is False (empty), return the other
7353
7354 if Is_False (Left) then
7355 return Right;
7356 elsif Is_False (Right) then
7357 return Left;
7358 end if;
7359
7360 -- Initialize result first entry from left or right operand depending
7361 -- on which starts with the lower range.
7362
7363 if Left (SLeft).Lo < Right (SRight).Lo then
7364 FEnt := Left (SLeft);
7365 SLeft := SLeft + 1;
7366 else
7367 FEnt := Right (SRight);
7368 SRight := SRight + 1;
7369 end if;
7370
7371 -- This loop eats ranges from left and right operands that are
7372 -- contiguous with the first range we are gathering.
7373
7374 loop
7375 -- Eat first entry in left operand if contiguous or overlapped by
7376 -- gathered first operand of result.
7377
7378 if SLeft <= Left'Last
7379 and then Left (SLeft).Lo <= FEnt.Hi + 1
7380 then
7381 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7382 SLeft := SLeft + 1;
7383
7384 -- Eat first entry in right operand if contiguous or overlapped by
7385 -- gathered right operand of result.
7386
7387 elsif SRight <= Right'Last
7388 and then Right (SRight).Lo <= FEnt.Hi + 1
7389 then
7390 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7391 SRight := SRight + 1;
7392
7393 -- All done if no more entries to eat
7394
7395 else
7396 exit;
7397 end if;
7398 end loop;
7399
7400 -- Obtain result as the first entry we just computed, concatenated
7401 -- to the "or" of the remaining results (if one operand is empty,
7402 -- this will just concatenate with the other
7403
7404 return
7405 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7406 end "or";
7407
7408 -----------------
7409 -- Build_Range --
7410 -----------------
7411
7412 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7413 Result : Node_Id;
7414 begin
7415 Result :=
7416 Make_Range (Loc,
7417 Low_Bound => Build_Val (Lo),
7418 High_Bound => Build_Val (Hi));
7419 Set_Etype (Result, Btyp);
7420 Set_Analyzed (Result);
7421 return Result;
7422 end Build_Range;
7423
7424 ---------------
7425 -- Build_Val --
7426 ---------------
7427
7428 function Build_Val (V : Uint) return Node_Id is
7429 Result : Node_Id;
7430
7431 begin
7432 if Is_Enumeration_Type (Typ) then
7433 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7434 else
7435 Result := Make_Integer_Literal (Loc, V);
7436 end if;
7437
7438 Set_Etype (Result, Btyp);
7439 Set_Is_Static_Expression (Result);
7440 Set_Analyzed (Result);
7441 return Result;
7442 end Build_Val;
7443
7444 ---------------
7445 -- Get_RList --
7446 ---------------
7447
7448 function Get_RList (Exp : Node_Id) return RList is
7449 Op : Node_Kind;
7450 Val : Uint;
7451
7452 begin
7453 -- Static expression can only be true or false
7454
7455 if Is_OK_Static_Expression (Exp) then
7456 if Expr_Value (Exp) = 0 then
7457 return False_Range;
7458 else
7459 return True_Range;
7460 end if;
7461 end if;
7462
7463 -- Otherwise test node type
7464
7465 Op := Nkind (Exp);
7466
7467 case Op is
7468
7469 -- And
7470
7471 when N_Op_And | N_And_Then =>
7472 return Get_RList (Left_Opnd (Exp))
7473 and
7474 Get_RList (Right_Opnd (Exp));
7475
7476 -- Or
7477
7478 when N_Op_Or | N_Or_Else =>
7479 return Get_RList (Left_Opnd (Exp))
7480 or
7481 Get_RList (Right_Opnd (Exp));
7482
7483 -- Not
7484
7485 when N_Op_Not =>
7486 return not Get_RList (Right_Opnd (Exp));
7487
7488 -- Comparisons of type with static value
7489
7490 when N_Op_Compare =>
7491
7492 -- Type is left operand
7493
7494 if Is_Type_Ref (Left_Opnd (Exp))
7495 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7496 then
7497 Val := Expr_Value (Right_Opnd (Exp));
7498
7499 -- Typ is right operand
7500
7501 elsif Is_Type_Ref (Right_Opnd (Exp))
7502 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7503 then
7504 Val := Expr_Value (Left_Opnd (Exp));
7505
7506 -- Invert sense of comparison
7507
7508 case Op is
7509 when N_Op_Gt => Op := N_Op_Lt;
7510 when N_Op_Lt => Op := N_Op_Gt;
7511 when N_Op_Ge => Op := N_Op_Le;
7512 when N_Op_Le => Op := N_Op_Ge;
7513 when others => null;
7514 end case;
7515
7516 -- Other cases are non-static
7517
7518 else
7519 raise Non_Static;
7520 end if;
7521
7522 -- Construct range according to comparison operation
7523
7524 case Op is
7525 when N_Op_Eq =>
7526 return RList'(1 => REnt'(Val, Val));
7527
7528 when N_Op_Ge =>
7529 return RList'(1 => REnt'(Val, BHi));
7530
7531 when N_Op_Gt =>
7532 return RList'(1 => REnt'(Val + 1, BHi));
7533
7534 when N_Op_Le =>
7535 return RList'(1 => REnt'(BLo, Val));
7536
7537 when N_Op_Lt =>
7538 return RList'(1 => REnt'(BLo, Val - 1));
7539
7540 when N_Op_Ne =>
7541 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7542
7543 when others =>
7544 raise Program_Error;
7545 end case;
7546
7547 -- Membership (IN)
7548
7549 when N_In =>
7550 if not Is_Type_Ref (Left_Opnd (Exp)) then
7551 raise Non_Static;
7552 end if;
7553
7554 if Present (Right_Opnd (Exp)) then
7555 return Membership_Entry (Right_Opnd (Exp));
7556 else
7557 return Membership_Entries (First (Alternatives (Exp)));
7558 end if;
7559
7560 -- Negative membership (NOT IN)
7561
7562 when N_Not_In =>
7563 if not Is_Type_Ref (Left_Opnd (Exp)) then
7564 raise Non_Static;
7565 end if;
7566
7567 if Present (Right_Opnd (Exp)) then
7568 return not Membership_Entry (Right_Opnd (Exp));
7569 else
7570 return not Membership_Entries (First (Alternatives (Exp)));
7571 end if;
7572
7573 -- Function call, may be call to static predicate
7574
7575 when N_Function_Call =>
7576 if Is_Entity_Name (Name (Exp)) then
7577 declare
7578 Ent : constant Entity_Id := Entity (Name (Exp));
7579 begin
7580 if Is_Predicate_Function (Ent)
7581 or else
7582 Is_Predicate_Function_M (Ent)
7583 then
7584 return Stat_Pred (Etype (First_Formal (Ent)));
7585 end if;
7586 end;
7587 end if;
7588
7589 -- Other function call cases are non-static
7590
7591 raise Non_Static;
7592
7593 -- Qualified expression, dig out the expression
7594
7595 when N_Qualified_Expression =>
7596 return Get_RList (Expression (Exp));
7597
7598 when N_Case_Expression =>
7599 declare
7600 Alt : Node_Id;
7601 Choices : List_Id;
7602 Dep : Node_Id;
7603
7604 begin
7605 if not Is_Entity_Name (Expression (Expr))
7606 or else Etype (Expression (Expr)) /= Typ
7607 then
7608 Error_Msg_N
7609 ("expression must denaote subtype", Expression (Expr));
7610 return False_Range;
7611 end if;
7612
7613 -- Collect discrete choices in all True alternatives
7614
7615 Choices := New_List;
7616 Alt := First (Alternatives (Exp));
7617 while Present (Alt) loop
7618 Dep := Expression (Alt);
7619
7620 if not Is_OK_Static_Expression (Dep) then
7621 raise Non_Static;
7622
7623 elsif Is_True (Expr_Value (Dep)) then
7624 Append_List_To (Choices,
7625 New_Copy_List (Discrete_Choices (Alt)));
7626 end if;
7627
7628 Next (Alt);
7629 end loop;
7630
7631 return Membership_Entries (First (Choices));
7632 end;
7633
7634 -- Expression with actions: if no actions, dig out expression
7635
7636 when N_Expression_With_Actions =>
7637 if Is_Empty_List (Actions (Exp)) then
7638 return Get_RList (Expression (Exp));
7639 else
7640 raise Non_Static;
7641 end if;
7642
7643 -- Xor operator
7644
7645 when N_Op_Xor =>
7646 return (Get_RList (Left_Opnd (Exp))
7647 and not Get_RList (Right_Opnd (Exp)))
7648 or (Get_RList (Right_Opnd (Exp))
7649 and not Get_RList (Left_Opnd (Exp)));
7650
7651 -- Any other node type is non-static
7652
7653 when others =>
7654 raise Non_Static;
7655 end case;
7656 end Get_RList;
7657
7658 ------------
7659 -- Hi_Val --
7660 ------------
7661
7662 function Hi_Val (N : Node_Id) return Uint is
7663 begin
7664 if Is_OK_Static_Expression (N) then
7665 return Expr_Value (N);
7666 else
7667 pragma Assert (Nkind (N) = N_Range);
7668 return Expr_Value (High_Bound (N));
7669 end if;
7670 end Hi_Val;
7671
7672 --------------
7673 -- Is_False --
7674 --------------
7675
7676 function Is_False (R : RList) return Boolean is
7677 begin
7678 return R'Length = 0;
7679 end Is_False;
7680
7681 -------------
7682 -- Is_True --
7683 -------------
7684
7685 function Is_True (R : RList) return Boolean is
7686 begin
7687 return R'Length = 1
7688 and then R (R'First).Lo = BLo
7689 and then R (R'First).Hi = BHi;
7690 end Is_True;
7691
7692 -----------------
7693 -- Is_Type_Ref --
7694 -----------------
7695
7696 function Is_Type_Ref (N : Node_Id) return Boolean is
7697 begin
7698 return Nkind (N) = N_Identifier
7699 and then Chars (N) = Nam
7700 and then Paren_Count (N) = 0;
7701 end Is_Type_Ref;
7702
7703 ------------
7704 -- Lo_Val --
7705 ------------
7706
7707 function Lo_Val (N : Node_Id) return Uint is
7708 begin
7709 if Is_OK_Static_Expression (N) then
7710 return Expr_Value (N);
7711 else
7712 pragma Assert (Nkind (N) = N_Range);
7713 return Expr_Value (Low_Bound (N));
7714 end if;
7715 end Lo_Val;
7716
7717 ------------------------
7718 -- Membership_Entries --
7719 ------------------------
7720
7721 function Membership_Entries (N : Node_Id) return RList is
7722 begin
7723 if No (Next (N)) then
7724 return Membership_Entry (N);
7725 else
7726 return Membership_Entry (N) or Membership_Entries (Next (N));
7727 end if;
7728 end Membership_Entries;
7729
7730 ----------------------
7731 -- Membership_Entry --
7732 ----------------------
7733
7734 function Membership_Entry (N : Node_Id) return RList is
7735 Val : Uint;
7736 SLo : Uint;
7737 SHi : Uint;
7738
7739 begin
7740 -- Range case
7741
7742 if Nkind (N) = N_Range then
7743 if not Is_OK_Static_Expression (Low_Bound (N))
7744 or else
7745 not Is_OK_Static_Expression (High_Bound (N))
7746 then
7747 raise Non_Static;
7748 else
7749 SLo := Expr_Value (Low_Bound (N));
7750 SHi := Expr_Value (High_Bound (N));
7751 return RList'(1 => REnt'(SLo, SHi));
7752 end if;
7753
7754 -- Static expression case
7755
7756 elsif Is_OK_Static_Expression (N) then
7757 Val := Expr_Value (N);
7758 return RList'(1 => REnt'(Val, Val));
7759
7760 -- Identifier (other than static expression) case
7761
7762 else pragma Assert (Nkind (N) = N_Identifier);
7763
7764 -- Type case
7765
7766 if Is_Type (Entity (N)) then
7767
7768 -- If type has predicates, process them
7769
7770 if Has_Predicates (Entity (N)) then
7771 return Stat_Pred (Entity (N));
7772
7773 -- For static subtype without predicates, get range
7774
7775 elsif Is_OK_Static_Subtype (Entity (N)) then
7776 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7777 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7778 return RList'(1 => REnt'(SLo, SHi));
7779
7780 -- Any other type makes us non-static
7781
7782 else
7783 raise Non_Static;
7784 end if;
7785
7786 -- Any other kind of identifier in predicate (e.g. a non-static
7787 -- expression value) means this is not a static predicate.
7788
7789 else
7790 raise Non_Static;
7791 end if;
7792 end if;
7793 end Membership_Entry;
7794
7795 ---------------
7796 -- Stat_Pred --
7797 ---------------
7798
7799 function Stat_Pred (Typ : Entity_Id) return RList is
7800 begin
7801 -- Not static if type does not have static predicates
7802
7803 if not Has_Static_Predicate (Typ) then
7804 raise Non_Static;
7805 end if;
7806
7807 -- Otherwise we convert the predicate list to a range list
7808
7809 declare
7810 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7811 Result : RList (1 .. List_Length (Spred));
7812 P : Node_Id;
7813
7814 begin
7815 P := First (Static_Discrete_Predicate (Typ));
7816 for J in Result'Range loop
7817 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7818 Next (P);
7819 end loop;
7820
7821 return Result;
7822 end;
7823 end Stat_Pred;
7824
7825 -- Start of processing for Build_Discrete_Static_Predicate
7826
7827 begin
7828 -- Establish bounds for the predicate
7829
7830 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7831 TLo := Expr_Value (Type_Low_Bound (Typ));
7832 else
7833 TLo := BLo;
7834 end if;
7835
7836 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7837 THi := Expr_Value (Type_High_Bound (Typ));
7838 else
7839 THi := BHi;
7840 end if;
7841
7842 -- Analyze the expression to see if it is a static predicate
7843
7844 declare
7845 Ranges : constant RList := Get_RList (Expr);
7846 -- Range list from expression if it is static
7847
7848 Plist : List_Id;
7849
7850 begin
7851 -- Convert range list into a form for the static predicate. In the
7852 -- Ranges array, we just have raw ranges, these must be converted
7853 -- to properly typed and analyzed static expressions or range nodes.
7854
7855 -- Note: here we limit ranges to the ranges of the subtype, so that
7856 -- a predicate is always false for values outside the subtype. That
7857 -- seems fine, such values are invalid anyway, and considering them
7858 -- to fail the predicate seems allowed and friendly, and furthermore
7859 -- simplifies processing for case statements and loops.
7860
7861 Plist := New_List;
7862
7863 for J in Ranges'Range loop
7864 declare
7865 Lo : Uint := Ranges (J).Lo;
7866 Hi : Uint := Ranges (J).Hi;
7867
7868 begin
7869 -- Ignore completely out of range entry
7870
7871 if Hi < TLo or else Lo > THi then
7872 null;
7873
7874 -- Otherwise process entry
7875
7876 else
7877 -- Adjust out of range value to subtype range
7878
7879 if Lo < TLo then
7880 Lo := TLo;
7881 end if;
7882
7883 if Hi > THi then
7884 Hi := THi;
7885 end if;
7886
7887 -- Convert range into required form
7888
7889 Append_To (Plist, Build_Range (Lo, Hi));
7890 end if;
7891 end;
7892 end loop;
7893
7894 -- Processing was successful and all entries were static, so now we
7895 -- can store the result as the predicate list.
7896
7897 Set_Static_Discrete_Predicate (Typ, Plist);
7898
7899 -- The processing for static predicates put the expression into
7900 -- canonical form as a series of ranges. It also eliminated
7901 -- duplicates and collapsed and combined ranges. We might as well
7902 -- replace the alternatives list of the right operand of the
7903 -- membership test with the static predicate list, which will
7904 -- usually be more efficient.
7905
7906 declare
7907 New_Alts : constant List_Id := New_List;
7908 Old_Node : Node_Id;
7909 New_Node : Node_Id;
7910
7911 begin
7912 Old_Node := First (Plist);
7913 while Present (Old_Node) loop
7914 New_Node := New_Copy (Old_Node);
7915
7916 if Nkind (New_Node) = N_Range then
7917 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7918 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7919 end if;
7920
7921 Append_To (New_Alts, New_Node);
7922 Next (Old_Node);
7923 end loop;
7924
7925 -- If empty list, replace by False
7926
7927 if Is_Empty_List (New_Alts) then
7928 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7929
7930 -- Else replace by set membership test
7931
7932 else
7933 Rewrite (Expr,
7934 Make_In (Loc,
7935 Left_Opnd => Make_Identifier (Loc, Nam),
7936 Right_Opnd => Empty,
7937 Alternatives => New_Alts));
7938
7939 -- Resolve new expression in function context
7940
7941 Install_Formals (Predicate_Function (Typ));
7942 Push_Scope (Predicate_Function (Typ));
7943 Analyze_And_Resolve (Expr, Standard_Boolean);
7944 Pop_Scope;
7945 end if;
7946 end;
7947 end;
7948
7949 -- If non-static, return doing nothing
7950
7951 exception
7952 when Non_Static =>
7953 return;
7954 end Build_Discrete_Static_Predicate;
7955
7956 --------------------------------
7957 -- Build_Export_Import_Pragma --
7958 --------------------------------
7959
7960 function Build_Export_Import_Pragma
7961 (Asp : Node_Id;
7962 Id : Entity_Id) return Node_Id
7963 is
7964 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
7965 Expr : constant Node_Id := Expression (Asp);
7966 Loc : constant Source_Ptr := Sloc (Asp);
7967
7968 Args : List_Id;
7969 Conv : Node_Id;
7970 Conv_Arg : Node_Id;
7971 Dummy_1 : Node_Id;
7972 Dummy_2 : Node_Id;
7973 EN : Node_Id;
7974 LN : Node_Id;
7975 Prag : Node_Id;
7976
7977 Create_Pragma : Boolean := False;
7978 -- This flag is set when the aspect form is such that it warrants the
7979 -- creation of a corresponding pragma.
7980
7981 begin
7982 if Present (Expr) then
7983 if Error_Posted (Expr) then
7984 null;
7985
7986 elsif Is_True (Expr_Value (Expr)) then
7987 Create_Pragma := True;
7988 end if;
7989
7990 -- Otherwise the aspect defaults to True
7991
7992 else
7993 Create_Pragma := True;
7994 end if;
7995
7996 -- Nothing to do when the expression is False or is erroneous
7997
7998 if not Create_Pragma then
7999 return Empty;
8000 end if;
8001
8002 -- Obtain all interfacing aspects that apply to the related entity
8003
8004 Get_Interfacing_Aspects
8005 (Iface_Asp => Asp,
8006 Conv_Asp => Conv,
8007 EN_Asp => EN,
8008 Expo_Asp => Dummy_1,
8009 Imp_Asp => Dummy_2,
8010 LN_Asp => LN);
8011
8012 Args := New_List;
8013
8014 -- Handle the convention argument
8015
8016 if Present (Conv) then
8017 Conv_Arg := New_Copy_Tree (Expression (Conv));
8018
8019 -- Assume convention "Ada' when aspect Convention is missing
8020
8021 else
8022 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8023 end if;
8024
8025 Append_To (Args,
8026 Make_Pragma_Argument_Association (Loc,
8027 Chars => Name_Convention,
8028 Expression => Conv_Arg));
8029
8030 -- Handle the entity argument
8031
8032 Append_To (Args,
8033 Make_Pragma_Argument_Association (Loc,
8034 Chars => Name_Entity,
8035 Expression => New_Occurrence_Of (Id, Loc)));
8036
8037 -- Handle the External_Name argument
8038
8039 if Present (EN) then
8040 Append_To (Args,
8041 Make_Pragma_Argument_Association (Loc,
8042 Chars => Name_External_Name,
8043 Expression => New_Copy_Tree (Expression (EN))));
8044 end if;
8045
8046 -- Handle the Link_Name argument
8047
8048 if Present (LN) then
8049 Append_To (Args,
8050 Make_Pragma_Argument_Association (Loc,
8051 Chars => Name_Link_Name,
8052 Expression => New_Copy_Tree (Expression (LN))));
8053 end if;
8054
8055 -- Generate:
8056 -- pragma Export/Import
8057 -- (Convention => <Conv>/Ada,
8058 -- Entity => <Id>,
8059 -- [External_Name => <EN>,]
8060 -- [Link_Name => <LN>]);
8061
8062 Prag :=
8063 Make_Pragma (Loc,
8064 Pragma_Identifier =>
8065 Make_Identifier (Loc, Chars (Identifier (Asp))),
8066 Pragma_Argument_Associations => Args);
8067
8068 -- Decorate the relevant aspect and the pragma
8069
8070 Set_Aspect_Rep_Item (Asp, Prag);
8071
8072 Set_Corresponding_Aspect (Prag, Asp);
8073 Set_From_Aspect_Specification (Prag);
8074 Set_Parent (Prag, Asp);
8075
8076 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8077 Set_Import_Pragma (Id, Prag);
8078 end if;
8079
8080 return Prag;
8081 end Build_Export_Import_Pragma;
8082
8083 -------------------------------------------
8084 -- Build_Invariant_Procedure_Declaration --
8085 -------------------------------------------
8086
8087 function Build_Invariant_Procedure_Declaration
8088 (Typ : Entity_Id) return Node_Id
8089 is
8090 Loc : constant Source_Ptr := Sloc (Typ);
8091 Decl : Node_Id;
8092 Obj_Id : Entity_Id;
8093 SId : Entity_Id;
8094
8095 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8096
8097 begin
8098 -- Check for duplicate definitions
8099
8100 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
8101 return Empty;
8102 end if;
8103
8104 -- The related type may be subject to pragma Ghost. Set the mode now to
8105 -- ensure that the invariant procedure is properly marked as Ghost.
8106
8107 Set_Ghost_Mode_From_Entity (Typ);
8108
8109 SId :=
8110 Make_Defining_Identifier (Loc,
8111 Chars => New_External_Name (Chars (Typ), "Invariant"));
8112 Set_Has_Invariants (Typ);
8113 Set_Ekind (SId, E_Procedure);
8114 Set_Etype (SId, Standard_Void_Type);
8115 Set_Is_Invariant_Procedure (SId);
8116 Set_Invariant_Procedure (Typ, SId);
8117
8118 -- Source Coverage Obligations might be attached to the invariant
8119 -- expression this procedure evaluates, and we need debug info to be
8120 -- able to assess the coverage achieved by evaluations.
8121
8122 if Opt.Generate_SCO then
8123 Set_Needs_Debug_Info (SId);
8124 end if;
8125
8126 -- Mark the invariant procedure explicitly as Ghost because it does not
8127 -- come from source.
8128
8129 if Ghost_Mode > None then
8130 Set_Is_Ghost_Entity (SId);
8131 end if;
8132
8133 Obj_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
8134 Set_Etype (Obj_Id, Typ);
8135
8136 Decl :=
8137 Make_Subprogram_Declaration (Loc,
8138 Make_Procedure_Specification (Loc,
8139 Defining_Unit_Name => SId,
8140 Parameter_Specifications => New_List (
8141 Make_Parameter_Specification (Loc,
8142 Defining_Identifier => Obj_Id,
8143 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
8144
8145 Ghost_Mode := Save_Ghost_Mode;
8146
8147 return Decl;
8148 end Build_Invariant_Procedure_Declaration;
8149
8150 -------------------------------
8151 -- Build_Invariant_Procedure --
8152 -------------------------------
8153
8154 -- The procedure that is constructed here has the form
8155
8156 -- procedure typInvariant (Ixxx : typ) is
8157 -- begin
8158 -- pragma Check (Invariant, exp, "failed invariant from xxx");
8159 -- pragma Check (Invariant, exp, "failed invariant from xxx");
8160 -- ...
8161 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
8162 -- ...
8163 -- end typInvariant;
8164
8165 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
8166 procedure Add_Invariants
8167 (T : Entity_Id;
8168 Obj_Id : Entity_Id;
8169 Stmts : in out List_Id;
8170 Inherit : Boolean);
8171 -- Appends statements to Stmts for any invariants in the rep item chain
8172 -- of the given type. If Inherit is False, then we only process entries
8173 -- on the chain for the type Typ. If Inherit is True, then we ignore any
8174 -- Invariant aspects, but we process all Invariant'Class aspects, adding
8175 -- "inherited" to the exception message and generating an informational
8176 -- message about the inheritance of an invariant.
8177
8178 --------------------
8179 -- Add_Invariants --
8180 --------------------
8181
8182 procedure Add_Invariants
8183 (T : Entity_Id;
8184 Obj_Id : Entity_Id;
8185 Stmts : in out List_Id;
8186 Inherit : Boolean)
8187 is
8188 procedure Add_Invariant (Prag : Node_Id);
8189 -- Create a runtime check to verify the exression of invariant pragma
8190 -- Prag. All generated code is added to list Stmts.
8191
8192 -------------------
8193 -- Add_Invariant --
8194 -------------------
8195
8196 procedure Add_Invariant (Prag : Node_Id) is
8197 procedure Replace_Type_Reference (N : Node_Id);
8198 -- Replace a single occurrence N of the subtype name with a
8199 -- reference to the formal of the predicate function. N can be an
8200 -- identifier referencing the subtype, or a selected component,
8201 -- representing an appropriately qualified occurrence of the
8202 -- subtype name.
8203
8204 procedure Replace_Type_References is
8205 new Replace_Type_References_Generic (Replace_Type_Reference);
8206 -- Traverse an expression replacing all occurrences of the subtype
8207 -- name with appropriate references to the formal of the predicate
8208 -- function. Note that we must ensure that the type and entity
8209 -- information is properly set in the replacement node, since we
8210 -- will do a Preanalyze call of this expression without proper
8211 -- visibility of the procedure argument.
8212
8213 ----------------------------
8214 -- Replace_Type_Reference --
8215 ----------------------------
8216
8217 -- Note: See comments in Add_Predicates.Replace_Type_Reference
8218 -- regarding handling of Sloc and Comes_From_Source.
8219
8220 procedure Replace_Type_Reference (N : Node_Id) is
8221 Nloc : constant Source_Ptr := Sloc (N);
8222
8223 begin
8224 -- Add semantic information to node to be rewritten, for ASIS
8225 -- navigation needs.
8226
8227 if Nkind (N) = N_Identifier then
8228 Set_Entity (N, T);
8229 Set_Etype (N, T);
8230
8231 elsif Nkind (N) = N_Selected_Component then
8232 Analyze (Prefix (N));
8233 Set_Entity (Selector_Name (N), T);
8234 Set_Etype (Selector_Name (N), T);
8235 end if;
8236
8237 -- Invariant'Class, replace with T'Class (obj)
8238
8239 if Class_Present (Prag) then
8240
8241 -- In ASIS mode, an inherited item is already analyzed,
8242 -- and the replacement has been done, so do not repeat
8243 -- the transformation to prevent a malformed tree.
8244
8245 if ASIS_Mode
8246 and then Nkind (Parent (N)) = N_Attribute_Reference
8247 and then Attribute_Name (Parent (N)) = Name_Class
8248 then
8249 null;
8250
8251 else
8252 Rewrite (N,
8253 Make_Type_Conversion (Nloc,
8254 Subtype_Mark =>
8255 Make_Attribute_Reference (Nloc,
8256 Prefix => New_Occurrence_Of (T, Nloc),
8257 Attribute_Name => Name_Class),
8258 Expression =>
8259 Make_Identifier (Nloc, Chars (Obj_Id))));
8260
8261 Set_Entity (Expression (N), Obj_Id);
8262 Set_Etype (Expression (N), Typ);
8263 end if;
8264
8265 -- Invariant, replace with obj
8266
8267 else
8268 Rewrite (N, Make_Identifier (Nloc, Chars (Obj_Id)));
8269 Set_Entity (N, Obj_Id);
8270 Set_Etype (N, Typ);
8271 end if;
8272
8273 Set_Comes_From_Source (N, True);
8274 end Replace_Type_Reference;
8275
8276 -- Local variables
8277
8278 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8279 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
8280 Ploc : constant Source_Ptr := Sloc (Prag);
8281 Arg1 : Node_Id;
8282 Arg2 : Node_Id;
8283 Arg3 : Node_Id;
8284 Assoc : List_Id;
8285 Expr : Node_Id;
8286 Str : String_Id;
8287
8288 -- Start of processing for Add_Invariant
8289
8290 begin
8291 -- Extract the arguments of the invariant pragma
8292
8293 Arg1 := First (Pragma_Argument_Associations (Prag));
8294 Arg2 := Next (Arg1);
8295 Arg3 := Next (Arg2);
8296
8297 Arg1 := Get_Pragma_Arg (Arg1);
8298 Arg2 := Get_Pragma_Arg (Arg2);
8299
8300 -- The caller requests processing of all Invariant'Class pragmas,
8301 -- but the current pragma does not fall in this category. Return
8302 -- as there is nothing left to do.
8303
8304 if Inherit then
8305 if not Class_Present (Prag) then
8306 return;
8307 end if;
8308
8309 -- Otherwise the pragma must apply to the current type
8310
8311 elsif Entity (Arg1) /= T then
8312 return;
8313 end if;
8314
8315 Expr := New_Copy_Tree (Arg2);
8316
8317 -- Replace all occurrences of the type's name with references to
8318 -- the formal parameter of the invariant procedure.
8319
8320 Replace_Type_References (Expr, T);
8321
8322 -- If the invariant pragma comes from an aspect, replace the saved
8323 -- expression because we need the subtype references replaced for
8324 -- the calls to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
8325 -- routines. This is not done for interited class-wide invariants
8326 -- because the original pragma of the parent type must remain
8327 -- unchanged.
8328
8329 if not Inherit and then Present (Asp) then
8330 Set_Entity (Identifier (Asp), New_Copy_Tree (Expr));
8331 end if;
8332
8333 -- Preanalyze the invariant expression to capture the visibility
8334 -- of the proper package part. In general the expression is not
8335 -- fully analyzed until the body of the invariant procedure is
8336 -- analyzed at the end of the private part, but that yields the
8337 -- wrong visibility.
8338
8339 -- Historical note: we used to set N as the parent, but a package
8340 -- specification as the parent of an expression is bizarre.
8341
8342 Set_Parent (Expr, Parent (Arg2));
8343 Preanalyze_Assert_Expression (Expr, Any_Boolean);
8344
8345 -- Both modifications performed below are not done for inherited
8346 -- class-wide invariants because the origial aspect/pragma of the
8347 -- parent type must remain unchanged.
8348
8349 if not Inherit then
8350
8351 -- A class-wide invariant may be inherited in a separate unit,
8352 -- where the corresponding expression cannot be resolved by
8353 -- visibility, because it refers to a local function. Propagate
8354 -- semantic information to the original representation item, to
8355 -- be used when an invariant procedure for a derived type is
8356 -- constructed.
8357
8358 -- ??? Unclear how to handle class-wide invariants that are not
8359 -- function calls.
8360
8361 if Class_Present (Prag)
8362 and then Nkind (Expr) = N_Function_Call
8363 and then Nkind (Arg2) = N_Indexed_Component
8364 then
8365 Rewrite (Arg2,
8366 Make_Function_Call (Ploc,
8367 Name =>
8368 New_Occurrence_Of (Entity (Name (Expr)), Ploc),
8369 Parameter_Associations => Expressions (Arg2)));
8370 end if;
8371
8372 -- In ASIS mode, even if assertions are not enabled, we must
8373 -- analyze the original expression in the aspect specification
8374 -- because it is part of the original tree.
8375
8376 if ASIS_Mode and then Present (Asp) then
8377 declare
8378 Asp_Expr : constant Node_Id := Expression (Asp);
8379
8380 begin
8381 Replace_Type_References (Asp_Expr, T);
8382 Preanalyze_Assert_Expression (Asp_Expr, Any_Boolean);
8383 end;
8384 end if;
8385 end if;
8386
8387 -- An ignored invariant must not generate a runtime check. Add a
8388 -- null statement to ensure that the invariant procedure does get
8389 -- a completing body.
8390
8391 if No (Stmts) then
8392 Stmts := Empty_List;
8393 end if;
8394
8395 if Is_Ignored (Prag) then
8396 Append_To (Stmts, Make_Null_Statement (Ploc));
8397
8398 -- Otherwise the invariant is checked. Build a Check pragma to
8399 -- verify the expression at runtime.
8400
8401 else
8402 Assoc := New_List (
8403 Make_Pragma_Argument_Association (Ploc,
8404 Expression => Make_Identifier (Ploc, Nam)),
8405 Make_Pragma_Argument_Association (Ploc,
8406 Expression => Expr));
8407
8408 -- Handle the String argument (if any)
8409
8410 if Present (Arg3) then
8411 Str := Strval (Get_Pragma_Arg (Arg3));
8412
8413 -- When inheriting an invariant, modify the message from
8414 -- "failed invariant" to "failed inherited invariant".
8415
8416 if Inherit then
8417 String_To_Name_Buffer (Str);
8418
8419 if Name_Buffer (1 .. 16) = "failed invariant" then
8420 Insert_Str_In_Name_Buffer ("inherited ", 8);
8421 Str := String_From_Name_Buffer;
8422 end if;
8423 end if;
8424
8425 Append_To (Assoc,
8426 Make_Pragma_Argument_Association (Ploc,
8427 Expression => Make_String_Literal (Ploc, Str)));
8428 end if;
8429
8430 -- Generate:
8431 -- pragma Check (Nam, Expr, Str);
8432
8433 Append_To (Stmts,
8434 Make_Pragma (Ploc,
8435 Pragma_Identifier =>
8436 Make_Identifier (Ploc, Name_Check),
8437 Pragma_Argument_Associations => Assoc));
8438 end if;
8439
8440 -- Output an info message when inheriting an invariant and the
8441 -- listing option is enabled.
8442
8443 if Inherit and Opt.List_Inherited_Aspects then
8444 Error_Msg_Sloc := Sloc (Prag);
8445 Error_Msg_N
8446 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
8447 end if;
8448 end Add_Invariant;
8449
8450 -- Local variables
8451
8452 Ritem : Node_Id;
8453
8454 -- Start of processing for Add_Invariants
8455
8456 begin
8457 Ritem := First_Rep_Item (T);
8458 while Present (Ritem) loop
8459 if Nkind (Ritem) = N_Pragma
8460 and then Pragma_Name (Ritem) = Name_Invariant
8461 then
8462 Add_Invariant (Ritem);
8463 end if;
8464
8465 Next_Rep_Item (Ritem);
8466 end loop;
8467 end Add_Invariants;
8468
8469 -- Local variables
8470
8471 Loc : constant Source_Ptr := Sloc (Typ);
8472 Priv_Decls : constant List_Id := Private_Declarations (N);
8473 Vis_Decls : constant List_Id := Visible_Declarations (N);
8474
8475 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8476
8477 PBody : Node_Id;
8478 PDecl : Node_Id;
8479 SId : Entity_Id;
8480 Spec : Node_Id;
8481 Stmts : List_Id;
8482
8483 Obj_Id : Node_Id;
8484 -- The entity of the formal for the procedure
8485
8486 -- Start of processing for Build_Invariant_Procedure
8487
8488 begin
8489 -- The related type may be subject to pragma Ghost. Set the mode now to
8490 -- ensure that the invariant procedure is properly marked as Ghost.
8491
8492 Set_Ghost_Mode_From_Entity (Typ);
8493
8494 Stmts := No_List;
8495 PDecl := Empty;
8496 PBody := Empty;
8497 SId := Empty;
8498
8499 -- If the aspect specification exists for some view of the type, the
8500 -- declaration for the procedure has been created.
8501
8502 if Has_Invariants (Typ) then
8503 SId := Invariant_Procedure (Typ);
8504 end if;
8505
8506 -- If the body is already present, nothing to do. This will occur when
8507 -- the type is already frozen, which is the case when the invariant
8508 -- appears in a private part, and the freezing takes place before the
8509 -- final pass over full declarations.
8510
8511 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
8512
8513 if Present (SId) then
8514 PDecl := Unit_Declaration_Node (SId);
8515
8516 if Present (PDecl)
8517 and then Nkind (PDecl) = N_Subprogram_Declaration
8518 and then Present (Corresponding_Body (PDecl))
8519 then
8520 Ghost_Mode := Save_Ghost_Mode;
8521 return;
8522 end if;
8523
8524 else
8525 PDecl := Build_Invariant_Procedure_Declaration (Typ);
8526 end if;
8527
8528 -- Recover formal of procedure, for use in the calls to invariant
8529 -- functions (including inherited ones).
8530
8531 Obj_Id :=
8532 Defining_Identifier
8533 (First (Parameter_Specifications (Specification (PDecl))));
8534
8535 -- Add invariants for the current type
8536
8537 Add_Invariants
8538 (T => Typ,
8539 Obj_Id => Obj_Id,
8540 Stmts => Stmts,
8541 Inherit => False);
8542
8543 -- Add invariants for parent types
8544
8545 declare
8546 Current_Typ : Entity_Id;
8547 Parent_Typ : Entity_Id;
8548
8549 begin
8550 Current_Typ := Typ;
8551 loop
8552 Parent_Typ := Etype (Current_Typ);
8553
8554 if Is_Private_Type (Parent_Typ)
8555 and then Present (Full_View (Base_Type (Parent_Typ)))
8556 then
8557 Parent_Typ := Full_View (Base_Type (Parent_Typ));
8558 end if;
8559
8560 exit when Parent_Typ = Current_Typ;
8561
8562 Current_Typ := Parent_Typ;
8563 Add_Invariants
8564 (T => Current_Typ,
8565 Obj_Id => Obj_Id,
8566 Stmts => Stmts,
8567 Inherit => True);
8568 end loop;
8569 end;
8570
8571 -- Add invariants of progenitors
8572
8573 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
8574 declare
8575 Ifaces_List : Elist_Id;
8576 AI : Elmt_Id;
8577 Iface : Entity_Id;
8578
8579 begin
8580 Collect_Interfaces (Typ, Ifaces_List);
8581
8582 AI := First_Elmt (Ifaces_List);
8583 while Present (AI) loop
8584 Iface := Node (AI);
8585
8586 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8587 Add_Invariants
8588 (T => Iface,
8589 Obj_Id => Obj_Id,
8590 Stmts => Stmts,
8591 Inherit => True);
8592 end if;
8593
8594 Next_Elmt (AI);
8595 end loop;
8596 end;
8597 end if;
8598
8599 -- Build the procedure if we generated at least one Check pragma
8600
8601 if Stmts /= No_List then
8602 Spec := Copy_Separate_Tree (Specification (PDecl));
8603
8604 PBody :=
8605 Make_Subprogram_Body (Loc,
8606 Specification => Spec,
8607 Declarations => Empty_List,
8608 Handled_Statement_Sequence =>
8609 Make_Handled_Sequence_Of_Statements (Loc,
8610 Statements => Stmts));
8611
8612 -- The processing of an invariant pragma immediately generates the
8613 -- invariant procedure spec, inserts it into the tree, and analyzes
8614 -- it. If the spec has not been analyzed, then the invariant pragma
8615 -- is being inherited and requires manual insertion and analysis.
8616
8617 if not Analyzed (PDecl) then
8618 Append_To (Vis_Decls, PDecl);
8619 Analyze (PDecl);
8620 end if;
8621
8622 -- The invariant procedure body is inserted at the end of the private
8623 -- declarations.
8624
8625 if Present (Priv_Decls) then
8626 Append_To (Priv_Decls, PBody);
8627
8628 -- If the invariant appears on the full view of a private type,
8629 -- then the analysis of the private part is already completed.
8630 -- Manually analyze the new body in this case, otherwise wait
8631 -- for the analysis of the private declarations to process the
8632 -- body.
8633
8634 if In_Private_Part (Current_Scope) then
8635 Analyze (PBody);
8636 end if;
8637
8638 -- Otherwise there are no private declarations. This is either an
8639 -- error or the related type is a private extension, in which case
8640 -- it does not need a completion in a private part. Insert the body
8641 -- at the end of the visible declarations and analyze immediately
8642 -- because the related type is about to be frozen.
8643
8644 else
8645 Append_To (Vis_Decls, PBody);
8646 Analyze (PBody);
8647 end if;
8648 end if;
8649
8650 Ghost_Mode := Save_Ghost_Mode;
8651 end Build_Invariant_Procedure;
8652
8653 -------------------------------
8654 -- Build_Predicate_Functions --
8655 -------------------------------
8656
8657 -- The procedures that are constructed here have the form:
8658
8659 -- function typPredicate (Ixxx : typ) return Boolean is
8660 -- begin
8661 -- return
8662 -- typ1Predicate (typ1 (Ixxx))
8663 -- and then typ2Predicate (typ2 (Ixxx))
8664 -- and then ...;
8665 -- exp1 and then exp2 and then ...
8666 -- end typPredicate;
8667
8668 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8669 -- this is the point at which these expressions get analyzed, providing the
8670 -- required delay, and typ1, typ2, are entities from which predicates are
8671 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8672 -- use this function even if checks are off, e.g. for membership tests.
8673
8674 -- Note that the inherited predicates are evaluated first, as required by
8675 -- AI12-0071-1.
8676
8677 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8678 -- the form of this return expression.
8679
8680 -- If the expression has at least one Raise_Expression, then we also build
8681 -- the typPredicateM version of the function, in which any occurrence of a
8682 -- Raise_Expression is converted to "return False".
8683
8684 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8685 Loc : constant Source_Ptr := Sloc (Typ);
8686
8687 Expr : Node_Id;
8688 -- This is the expression for the result of the function. It is
8689 -- is build by connecting the component predicates with AND THEN.
8690
8691 Expr_M : Node_Id;
8692 -- This is the corresponding return expression for the Predicate_M
8693 -- function. It differs in that raise expressions are marked for
8694 -- special expansion (see Process_REs).
8695
8696 Object_Name : Name_Id;
8697 -- Name for argument of Predicate procedure. Note that we use the same
8698 -- name for both predicate functions. That way the reference within the
8699 -- predicate expression is the same in both functions.
8700
8701 Object_Entity : Entity_Id;
8702 -- Entity for argument of Predicate procedure
8703
8704 Object_Entity_M : Entity_Id;
8705 -- Entity for argument of separate Predicate procedure when exceptions
8706 -- are present in expression.
8707
8708 FDecl : Node_Id;
8709 -- The function declaration
8710
8711 SId : Entity_Id;
8712 -- Its entity
8713
8714 Raise_Expression_Present : Boolean := False;
8715 -- Set True if Expr has at least one Raise_Expression
8716
8717 procedure Add_Condition (Cond : Node_Id);
8718 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8719 -- Expr is empty).
8720
8721 procedure Add_Predicates;
8722 -- Appends expressions for any Predicate pragmas in the rep item chain
8723 -- Typ to Expr. Note that we look only at items for this exact entity.
8724 -- Inheritance of predicates for the parent type is done by calling the
8725 -- Predicate_Function of the parent type, using Add_Call above.
8726
8727 procedure Add_Call (T : Entity_Id);
8728 -- Includes a call to the predicate function for type T in Expr if T
8729 -- has predicates and Predicate_Function (T) is non-empty.
8730
8731 function Process_RE (N : Node_Id) return Traverse_Result;
8732 -- Used in Process REs, tests if node N is a raise expression, and if
8733 -- so, marks it to be converted to return False.
8734
8735 procedure Process_REs is new Traverse_Proc (Process_RE);
8736 -- Marks any raise expressions in Expr_M to return False
8737
8738 function Test_RE (N : Node_Id) return Traverse_Result;
8739 -- Used in Test_REs, tests one node for being a raise expression, and if
8740 -- so sets Raise_Expression_Present True.
8741
8742 procedure Test_REs is new Traverse_Proc (Test_RE);
8743 -- Tests to see if Expr contains any raise expressions
8744
8745 --------------
8746 -- Add_Call --
8747 --------------
8748
8749 procedure Add_Call (T : Entity_Id) is
8750 Exp : Node_Id;
8751
8752 begin
8753 if Present (T) and then Present (Predicate_Function (T)) then
8754 Set_Has_Predicates (Typ);
8755
8756 -- Build the call to the predicate function of T
8757
8758 Exp :=
8759 Make_Predicate_Call
8760 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8761
8762 -- "and"-in the call to evolving expression
8763
8764 Add_Condition (Exp);
8765
8766 -- Output info message on inheritance if required. Note we do not
8767 -- give this information for generic actual types, since it is
8768 -- unwelcome noise in that case in instantiations. We also
8769 -- generally suppress the message in instantiations, and also
8770 -- if it involves internal names.
8771
8772 if Opt.List_Inherited_Aspects
8773 and then not Is_Generic_Actual_Type (Typ)
8774 and then Instantiation_Depth (Sloc (Typ)) = 0
8775 and then not Is_Internal_Name (Chars (T))
8776 and then not Is_Internal_Name (Chars (Typ))
8777 then
8778 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8779 Error_Msg_Node_2 := T;
8780 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8781 end if;
8782 end if;
8783 end Add_Call;
8784
8785 -------------------
8786 -- Add_Condition --
8787 -------------------
8788
8789 procedure Add_Condition (Cond : Node_Id) is
8790 begin
8791 -- This is the first predicate expression
8792
8793 if No (Expr) then
8794 Expr := Cond;
8795
8796 -- Otherwise concatenate to the existing predicate expressions by
8797 -- using "and then".
8798
8799 else
8800 Expr :=
8801 Make_And_Then (Loc,
8802 Left_Opnd => Relocate_Node (Expr),
8803 Right_Opnd => Cond);
8804 end if;
8805 end Add_Condition;
8806
8807 --------------------
8808 -- Add_Predicates --
8809 --------------------
8810
8811 procedure Add_Predicates is
8812 procedure Add_Predicate (Prag : Node_Id);
8813 -- Concatenate the expression of predicate pragma Prag to Expr by
8814 -- using a short circuit "and then" operator.
8815
8816 -------------------
8817 -- Add_Predicate --
8818 -------------------
8819
8820 procedure Add_Predicate (Prag : Node_Id) is
8821 procedure Replace_Type_Reference (N : Node_Id);
8822 -- Replace a single occurrence N of the subtype name with a
8823 -- reference to the formal of the predicate function. N can be an
8824 -- identifier referencing the subtype, or a selected component,
8825 -- representing an appropriately qualified occurrence of the
8826 -- subtype name.
8827
8828 procedure Replace_Type_References is
8829 new Replace_Type_References_Generic (Replace_Type_Reference);
8830 -- Traverse an expression changing every occurrence of an
8831 -- identifier whose name matches the name of the subtype with a
8832 -- reference to the formal parameter of the predicate function.
8833
8834 ----------------------------
8835 -- Replace_Type_Reference --
8836 ----------------------------
8837
8838 procedure Replace_Type_Reference (N : Node_Id) is
8839 begin
8840 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8841 -- Use the Sloc of the usage name, not the defining name
8842
8843 Set_Etype (N, Typ);
8844 Set_Entity (N, Object_Entity);
8845
8846 -- We want to treat the node as if it comes from source, so
8847 -- that ASIS will not ignore it.
8848
8849 Set_Comes_From_Source (N, True);
8850 end Replace_Type_Reference;
8851
8852 -- Local variables
8853
8854 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8855 Arg1 : Node_Id;
8856 Arg2 : Node_Id;
8857
8858 -- Start of processing for Add_Predicate
8859
8860 begin
8861 -- Extract the arguments of the pragma. The expression itself
8862 -- is copied for use in the predicate function, to preserve the
8863 -- original version for ASIS use.
8864
8865 Arg1 := First (Pragma_Argument_Associations (Prag));
8866 Arg2 := Next (Arg1);
8867
8868 Arg1 := Get_Pragma_Arg (Arg1);
8869 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8870
8871 -- When the predicate pragma applies to the current type or its
8872 -- full view, replace all occurrences of the subtype name with
8873 -- references to the formal parameter of the predicate function.
8874
8875 if Entity (Arg1) = Typ
8876 or else Full_View (Entity (Arg1)) = Typ
8877 then
8878 Replace_Type_References (Arg2, Typ);
8879
8880 -- If the predicate pragma comes from an aspect, replace the
8881 -- saved expression because we need the subtype references
8882 -- replaced for the calls to Preanalyze_Spec_Expression in
8883 -- Check_Aspect_At_xxx routines.
8884
8885 if Present (Asp) then
8886 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8887 end if;
8888
8889 -- "and"-in the Arg2 condition to evolving expression
8890
8891 Add_Condition (Relocate_Node (Arg2));
8892 end if;
8893 end Add_Predicate;
8894
8895 -- Local variables
8896
8897 Ritem : Node_Id;
8898
8899 -- Start of processing for Add_Predicates
8900
8901 begin
8902 Ritem := First_Rep_Item (Typ);
8903 while Present (Ritem) loop
8904 if Nkind (Ritem) = N_Pragma
8905 and then Pragma_Name (Ritem) = Name_Predicate
8906 then
8907 Add_Predicate (Ritem);
8908
8909 -- If the type is declared in an inner package it may be frozen
8910 -- outside of the package, and the generated pragma has not been
8911 -- analyzed yet, so capture the expression for the predicate
8912 -- function at this point.
8913
8914 elsif Nkind (Ritem) = N_Aspect_Specification
8915 and then Present (Aspect_Rep_Item (Ritem))
8916 and then Scope (Typ) /= Current_Scope
8917 then
8918 declare
8919 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8920
8921 begin
8922 if Nkind (Prag) = N_Pragma
8923 and then Pragma_Name (Prag) = Name_Predicate
8924 then
8925 Add_Predicate (Prag);
8926 end if;
8927 end;
8928 end if;
8929
8930 Next_Rep_Item (Ritem);
8931 end loop;
8932 end Add_Predicates;
8933
8934 ----------------
8935 -- Process_RE --
8936 ----------------
8937
8938 function Process_RE (N : Node_Id) return Traverse_Result is
8939 begin
8940 if Nkind (N) = N_Raise_Expression then
8941 Set_Convert_To_Return_False (N);
8942 return Skip;
8943 else
8944 return OK;
8945 end if;
8946 end Process_RE;
8947
8948 -------------
8949 -- Test_RE --
8950 -------------
8951
8952 function Test_RE (N : Node_Id) return Traverse_Result is
8953 begin
8954 if Nkind (N) = N_Raise_Expression then
8955 Raise_Expression_Present := True;
8956 return Abandon;
8957 else
8958 return OK;
8959 end if;
8960 end Test_RE;
8961
8962 -- Local variables
8963
8964 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8965
8966 -- Start of processing for Build_Predicate_Functions
8967
8968 begin
8969 -- Return if already built or if type does not have predicates
8970
8971 SId := Predicate_Function (Typ);
8972 if not Has_Predicates (Typ)
8973 or else (Present (SId) and then Has_Completion (SId))
8974 then
8975 return;
8976 end if;
8977
8978 -- The related type may be subject to pragma Ghost. Set the mode now to
8979 -- ensure that the predicate functions are properly marked as Ghost.
8980
8981 Set_Ghost_Mode_From_Entity (Typ);
8982
8983 -- Prepare to construct predicate expression
8984
8985 Expr := Empty;
8986
8987 if Present (SId) then
8988 FDecl := Unit_Declaration_Node (SId);
8989
8990 else
8991 FDecl := Build_Predicate_Function_Declaration (Typ);
8992 SId := Defining_Entity (FDecl);
8993 end if;
8994
8995 -- Recover name of formal parameter of function that replaces references
8996 -- to the type in predicate expressions.
8997
8998 Object_Entity :=
8999 Defining_Identifier
9000 (First (Parameter_Specifications (Specification (FDecl))));
9001
9002 Object_Name := Chars (Object_Entity);
9003 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
9004
9005 -- Add predicates for ancestor if present. These must come before the
9006 -- ones for the current type, as required by AI12-0071-1.
9007
9008 declare
9009 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
9010 begin
9011 if Present (Atyp) then
9012 Add_Call (Atyp);
9013 end if;
9014 end;
9015
9016 -- Add Predicates for the current type
9017
9018 Add_Predicates;
9019
9020 -- Case where predicates are present
9021
9022 if Present (Expr) then
9023
9024 -- Test for raise expression present
9025
9026 Test_REs (Expr);
9027
9028 -- If raise expression is present, capture a copy of Expr for use
9029 -- in building the predicateM function version later on. For this
9030 -- copy we replace references to Object_Entity by Object_Entity_M.
9031
9032 if Raise_Expression_Present then
9033 declare
9034 Map : constant Elist_Id := New_Elmt_List;
9035 New_V : Entity_Id := Empty;
9036
9037 -- The unanalyzed expression will be copied and appear in
9038 -- both functions. Normally expressions do not declare new
9039 -- entities, but quantified expressions do, so we need to
9040 -- create new entities for their bound variables, to prevent
9041 -- multiple definitions in gigi.
9042
9043 function Reset_Loop_Variable (N : Node_Id)
9044 return Traverse_Result;
9045
9046 procedure Collect_Loop_Variables is
9047 new Traverse_Proc (Reset_Loop_Variable);
9048
9049 ------------------------
9050 -- Reset_Loop_Variable --
9051 ------------------------
9052
9053 function Reset_Loop_Variable (N : Node_Id)
9054 return Traverse_Result
9055 is
9056 begin
9057 if Nkind (N) = N_Iterator_Specification then
9058 New_V := Make_Defining_Identifier
9059 (Sloc (N), Chars (Defining_Identifier (N)));
9060
9061 Set_Defining_Identifier (N, New_V);
9062 end if;
9063
9064 return OK;
9065 end Reset_Loop_Variable;
9066
9067 begin
9068 Append_Elmt (Object_Entity, Map);
9069 Append_Elmt (Object_Entity_M, Map);
9070 Expr_M := New_Copy_Tree (Expr, Map => Map);
9071 Collect_Loop_Variables (Expr_M);
9072 end;
9073 end if;
9074
9075 -- Build the main predicate function
9076
9077 declare
9078 SIdB : constant Entity_Id :=
9079 Make_Defining_Identifier (Loc,
9080 Chars => New_External_Name (Chars (Typ), "Predicate"));
9081 -- The entity for the function body
9082
9083 Spec : Node_Id;
9084 FBody : Node_Id;
9085
9086 begin
9087
9088 -- The predicate function is shared between views of a type
9089
9090 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
9091 Set_Predicate_Function (Full_View (Typ), SId);
9092 end if;
9093
9094 -- Mark the predicate function explicitly as Ghost because it does
9095 -- not come from source.
9096
9097 if Ghost_Mode > None then
9098 Set_Is_Ghost_Entity (SId);
9099 end if;
9100
9101 -- Build function body
9102
9103 Spec :=
9104 Make_Function_Specification (Loc,
9105 Defining_Unit_Name => SIdB,
9106 Parameter_Specifications => New_List (
9107 Make_Parameter_Specification (Loc,
9108 Defining_Identifier =>
9109 Make_Defining_Identifier (Loc, Object_Name),
9110 Parameter_Type =>
9111 New_Occurrence_Of (Typ, Loc))),
9112 Result_Definition =>
9113 New_Occurrence_Of (Standard_Boolean, Loc));
9114
9115 FBody :=
9116 Make_Subprogram_Body (Loc,
9117 Specification => Spec,
9118 Declarations => Empty_List,
9119 Handled_Statement_Sequence =>
9120 Make_Handled_Sequence_Of_Statements (Loc,
9121 Statements => New_List (
9122 Make_Simple_Return_Statement (Loc,
9123 Expression => Expr))));
9124
9125 -- If declaration has not been analyzed yet, Insert declaration
9126 -- before freeze node.
9127 -- Insert body after freeze node.
9128
9129 if not Analyzed (FDecl) then
9130 Insert_Before_And_Analyze (N, FDecl);
9131 end if;
9132
9133 Insert_After_And_Analyze (N, FBody);
9134
9135 -- Static predicate functions are always side-effect free, and
9136 -- in most cases dynamic predicate functions are as well. Mark
9137 -- them as such whenever possible, so redundant predicate checks
9138 -- can be optimized. If there is a variable reference within the
9139 -- expression, the function is not pure.
9140
9141 if Expander_Active then
9142 Set_Is_Pure (SId,
9143 Side_Effect_Free (Expr, Variable_Ref => True));
9144 Set_Is_Inlined (SId);
9145 end if;
9146 end;
9147
9148 -- Test for raise expressions present and if so build M version
9149
9150 if Raise_Expression_Present then
9151 declare
9152 SId : constant Entity_Id :=
9153 Make_Defining_Identifier (Loc,
9154 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9155 -- The entity for the function spec
9156
9157 SIdB : constant Entity_Id :=
9158 Make_Defining_Identifier (Loc,
9159 Chars => New_External_Name (Chars (Typ), "PredicateM"));
9160 -- The entity for the function body
9161
9162 Spec : Node_Id;
9163 FBody : Node_Id;
9164 FDecl : Node_Id;
9165 BTemp : Entity_Id;
9166
9167 begin
9168 -- Mark any raise expressions for special expansion
9169
9170 Process_REs (Expr_M);
9171
9172 -- Build function declaration
9173
9174 Set_Ekind (SId, E_Function);
9175 Set_Is_Predicate_Function_M (SId);
9176 Set_Predicate_Function_M (Typ, SId);
9177
9178 -- The predicate function is shared between views of a type
9179
9180 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
9181 Set_Predicate_Function_M (Full_View (Typ), SId);
9182 end if;
9183
9184 -- Mark the predicate function explicitly as Ghost because it
9185 -- does not come from source.
9186
9187 if Ghost_Mode > None then
9188 Set_Is_Ghost_Entity (SId);
9189 end if;
9190
9191 Spec :=
9192 Make_Function_Specification (Loc,
9193 Defining_Unit_Name => SId,
9194 Parameter_Specifications => New_List (
9195 Make_Parameter_Specification (Loc,
9196 Defining_Identifier => Object_Entity_M,
9197 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9198 Result_Definition =>
9199 New_Occurrence_Of (Standard_Boolean, Loc));
9200
9201 FDecl :=
9202 Make_Subprogram_Declaration (Loc,
9203 Specification => Spec);
9204
9205 -- Build function body
9206
9207 Spec :=
9208 Make_Function_Specification (Loc,
9209 Defining_Unit_Name => SIdB,
9210 Parameter_Specifications => New_List (
9211 Make_Parameter_Specification (Loc,
9212 Defining_Identifier =>
9213 Make_Defining_Identifier (Loc, Object_Name),
9214 Parameter_Type =>
9215 New_Occurrence_Of (Typ, Loc))),
9216 Result_Definition =>
9217 New_Occurrence_Of (Standard_Boolean, Loc));
9218
9219 -- Build the body, we declare the boolean expression before
9220 -- doing the return, because we are not really confident of
9221 -- what happens if a return appears within a return.
9222
9223 BTemp :=
9224 Make_Defining_Identifier (Loc,
9225 Chars => New_Internal_Name ('B'));
9226
9227 FBody :=
9228 Make_Subprogram_Body (Loc,
9229 Specification => Spec,
9230
9231 Declarations => New_List (
9232 Make_Object_Declaration (Loc,
9233 Defining_Identifier => BTemp,
9234 Constant_Present => True,
9235 Object_Definition =>
9236 New_Occurrence_Of (Standard_Boolean, Loc),
9237 Expression => Expr_M)),
9238
9239 Handled_Statement_Sequence =>
9240 Make_Handled_Sequence_Of_Statements (Loc,
9241 Statements => New_List (
9242 Make_Simple_Return_Statement (Loc,
9243 Expression => New_Occurrence_Of (BTemp, Loc)))));
9244
9245 -- Insert declaration before freeze node and body after
9246
9247 Insert_Before_And_Analyze (N, FDecl);
9248 Insert_After_And_Analyze (N, FBody);
9249 end;
9250 end if;
9251
9252 -- See if we have a static predicate. Note that the answer may be
9253 -- yes even if we have an explicit Dynamic_Predicate present.
9254
9255 declare
9256 PS : Boolean;
9257 EN : Node_Id;
9258
9259 begin
9260 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
9261 PS := False;
9262 else
9263 PS := Is_Predicate_Static (Expr, Object_Name);
9264 end if;
9265
9266 -- Case where we have a predicate-static aspect
9267
9268 if PS then
9269
9270 -- We don't set Has_Static_Predicate_Aspect, since we can have
9271 -- any of the three cases (Predicate, Dynamic_Predicate, or
9272 -- Static_Predicate) generating a predicate with an expression
9273 -- that is predicate-static. We just indicate that we have a
9274 -- predicate that can be treated as static.
9275
9276 Set_Has_Static_Predicate (Typ);
9277
9278 -- For discrete subtype, build the static predicate list
9279
9280 if Is_Discrete_Type (Typ) then
9281 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
9282
9283 -- If we don't get a static predicate list, it means that we
9284 -- have a case where this is not possible, most typically in
9285 -- the case where we inherit a dynamic predicate. We do not
9286 -- consider this an error, we just leave the predicate as
9287 -- dynamic. But if we do succeed in building the list, then
9288 -- we mark the predicate as static.
9289
9290 if No (Static_Discrete_Predicate (Typ)) then
9291 Set_Has_Static_Predicate (Typ, False);
9292 end if;
9293
9294 -- For real or string subtype, save predicate expression
9295
9296 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9297 Set_Static_Real_Or_String_Predicate (Typ, Expr);
9298 end if;
9299
9300 -- Case of dynamic predicate (expression is not predicate-static)
9301
9302 else
9303 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
9304 -- is only set if we have an explicit Dynamic_Predicate aspect
9305 -- given. Here we may simply have a Predicate aspect where the
9306 -- expression happens not to be predicate-static.
9307
9308 -- Emit an error when the predicate is categorized as static
9309 -- but its expression is not predicate-static.
9310
9311 -- First a little fiddling to get a nice location for the
9312 -- message. If the expression is of the form (A and then B),
9313 -- where A is an inherited predicate, then use the right
9314 -- operand for the Sloc. This avoids getting confused by a call
9315 -- to an inherited predicate with a less convenient source
9316 -- location.
9317
9318 EN := Expr;
9319 while Nkind (EN) = N_And_Then
9320 and then Nkind (Left_Opnd (EN)) = N_Function_Call
9321 and then Is_Predicate_Function
9322 (Entity (Name (Left_Opnd (EN))))
9323 loop
9324 EN := Right_Opnd (EN);
9325 end loop;
9326
9327 -- Now post appropriate message
9328
9329 if Has_Static_Predicate_Aspect (Typ) then
9330 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
9331 Error_Msg_F
9332 ("expression is not predicate-static (RM 3.2.4(16-22))",
9333 EN);
9334 else
9335 Error_Msg_F
9336 ("static predicate requires scalar or string type", EN);
9337 end if;
9338 end if;
9339 end if;
9340 end;
9341 end if;
9342
9343 Ghost_Mode := Save_Ghost_Mode;
9344 end Build_Predicate_Functions;
9345
9346 ------------------------------------------
9347 -- Build_Predicate_Function_Declaration --
9348 ------------------------------------------
9349
9350 function Build_Predicate_Function_Declaration
9351 (Typ : Entity_Id) return Node_Id
9352 is
9353 Loc : constant Source_Ptr := Sloc (Typ);
9354
9355 Object_Entity : constant Entity_Id :=
9356 Make_Defining_Identifier (Loc,
9357 Chars => New_Internal_Name ('I'));
9358
9359 -- The formal parameter of the function
9360
9361 SId : constant Entity_Id :=
9362 Make_Defining_Identifier (Loc,
9363 Chars => New_External_Name (Chars (Typ), "Predicate"));
9364
9365 -- The entity for the function spec
9366
9367 FDecl : Node_Id;
9368 Spec : Node_Id;
9369
9370 begin
9371 Spec :=
9372 Make_Function_Specification (Loc,
9373 Defining_Unit_Name => SId,
9374 Parameter_Specifications => New_List (
9375 Make_Parameter_Specification (Loc,
9376 Defining_Identifier => Object_Entity,
9377 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9378 Result_Definition =>
9379 New_Occurrence_Of (Standard_Boolean, Loc));
9380
9381 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
9382
9383 Set_Ekind (SId, E_Function);
9384 Set_Etype (SId, Standard_Boolean);
9385 Set_Is_Internal (SId);
9386 Set_Is_Predicate_Function (SId);
9387 Set_Predicate_Function (Typ, SId);
9388
9389 Insert_After (Parent (Typ), FDecl);
9390
9391 Analyze (FDecl);
9392
9393 return FDecl;
9394 end Build_Predicate_Function_Declaration;
9395
9396 -----------------------------------------
9397 -- Check_Aspect_At_End_Of_Declarations --
9398 -----------------------------------------
9399
9400 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9401 Ent : constant Entity_Id := Entity (ASN);
9402 Ident : constant Node_Id := Identifier (ASN);
9403 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9404
9405 End_Decl_Expr : constant Node_Id := Entity (Ident);
9406 -- Expression to be analyzed at end of declarations
9407
9408 Freeze_Expr : constant Node_Id := Expression (ASN);
9409 -- Expression from call to Check_Aspect_At_Freeze_Point
9410
9411 T : constant Entity_Id := Etype (Freeze_Expr);
9412 -- Type required for preanalyze call
9413
9414 Err : Boolean;
9415 -- Set False if error
9416
9417 -- On entry to this procedure, Entity (Ident) contains a copy of the
9418 -- original expression from the aspect, saved for this purpose, and
9419 -- but Expression (Ident) is a preanalyzed copy of the expression,
9420 -- preanalyzed just after the freeze point.
9421
9422 procedure Check_Overloaded_Name;
9423 -- For aspects whose expression is simply a name, this routine checks if
9424 -- the name is overloaded or not. If so, it verifies there is an
9425 -- interpretation that matches the entity obtained at the freeze point,
9426 -- otherwise the compiler complains.
9427
9428 ---------------------------
9429 -- Check_Overloaded_Name --
9430 ---------------------------
9431
9432 procedure Check_Overloaded_Name is
9433 begin
9434 if not Is_Overloaded (End_Decl_Expr) then
9435 Err := not Is_Entity_Name (End_Decl_Expr)
9436 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
9437
9438 else
9439 Err := True;
9440
9441 declare
9442 Index : Interp_Index;
9443 It : Interp;
9444
9445 begin
9446 Get_First_Interp (End_Decl_Expr, Index, It);
9447 while Present (It.Typ) loop
9448 if It.Nam = Entity (Freeze_Expr) then
9449 Err := False;
9450 exit;
9451 end if;
9452
9453 Get_Next_Interp (Index, It);
9454 end loop;
9455 end;
9456 end if;
9457 end Check_Overloaded_Name;
9458
9459 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9460
9461 begin
9462 -- In an instance we do not perform the consistency check between freeze
9463 -- point and end of declarations, because it was done already in the
9464 -- analysis of the generic. Furthermore, the delayed analysis of an
9465 -- aspect of the instance may produce spurious errors when the generic
9466 -- is a child unit that references entities in the parent (which might
9467 -- not be in scope at the freeze point of the instance).
9468
9469 if In_Instance then
9470 return;
9471
9472 -- Case of aspects Dimension, Dimension_System and Synchronization
9473
9474 elsif A_Id = Aspect_Synchronization then
9475 return;
9476
9477 -- Case of stream attributes, just have to compare entities. However,
9478 -- the expression is just a name (possibly overloaded), and there may
9479 -- be stream operations declared for unrelated types, so we just need
9480 -- to verify that one of these interpretations is the one available at
9481 -- at the freeze point.
9482
9483 elsif A_Id = Aspect_Input or else
9484 A_Id = Aspect_Output or else
9485 A_Id = Aspect_Read or else
9486 A_Id = Aspect_Write
9487 then
9488 Analyze (End_Decl_Expr);
9489 Check_Overloaded_Name;
9490
9491 elsif A_Id = Aspect_Variable_Indexing or else
9492 A_Id = Aspect_Constant_Indexing or else
9493 A_Id = Aspect_Default_Iterator or else
9494 A_Id = Aspect_Iterator_Element
9495 then
9496 -- Make type unfrozen before analysis, to prevent spurious errors
9497 -- about late attributes.
9498
9499 Set_Is_Frozen (Ent, False);
9500 Analyze (End_Decl_Expr);
9501 Set_Is_Frozen (Ent, True);
9502
9503 -- If the end of declarations comes before any other freeze
9504 -- point, the Freeze_Expr is not analyzed: no check needed.
9505
9506 if Analyzed (Freeze_Expr) and then not In_Instance then
9507 Check_Overloaded_Name;
9508 else
9509 Err := False;
9510 end if;
9511
9512 -- All other cases
9513
9514 else
9515 -- Indicate that the expression comes from an aspect specification,
9516 -- which is used in subsequent analysis even if expansion is off.
9517
9518 Set_Parent (End_Decl_Expr, ASN);
9519
9520 -- In a generic context the aspect expressions have not been
9521 -- preanalyzed, so do it now. There are no conformance checks
9522 -- to perform in this case.
9523
9524 if No (T) then
9525 Check_Aspect_At_Freeze_Point (ASN);
9526 return;
9527
9528 -- The default values attributes may be defined in the private part,
9529 -- and the analysis of the expression may take place when only the
9530 -- partial view is visible. The expression must be scalar, so use
9531 -- the full view to resolve.
9532
9533 elsif (A_Id = Aspect_Default_Value
9534 or else
9535 A_Id = Aspect_Default_Component_Value)
9536 and then Is_Private_Type (T)
9537 then
9538 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9539
9540 else
9541 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9542 end if;
9543
9544 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9545 end if;
9546
9547 -- Output error message if error. Force error on aspect specification
9548 -- even if there is an error on the expression itself.
9549
9550 if Err then
9551 Error_Msg_NE
9552 ("!visibility of aspect for& changes after freeze point",
9553 ASN, Ent);
9554 Error_Msg_NE
9555 ("info: & is frozen here, aspects evaluated at this point??",
9556 Freeze_Node (Ent), Ent);
9557 end if;
9558 end Check_Aspect_At_End_Of_Declarations;
9559
9560 ----------------------------------
9561 -- Check_Aspect_At_Freeze_Point --
9562 ----------------------------------
9563
9564 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9565 Ident : constant Node_Id := Identifier (ASN);
9566 -- Identifier (use Entity field to save expression)
9567
9568 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9569
9570 T : Entity_Id := Empty;
9571 -- Type required for preanalyze call
9572
9573 begin
9574 -- On entry to this procedure, Entity (Ident) contains a copy of the
9575 -- original expression from the aspect, saved for this purpose.
9576
9577 -- On exit from this procedure Entity (Ident) is unchanged, still
9578 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9579 -- of the expression, preanalyzed just after the freeze point.
9580
9581 -- Make a copy of the expression to be preanalyzed
9582
9583 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9584
9585 -- Find type for preanalyze call
9586
9587 case A_Id is
9588
9589 -- No_Aspect should be impossible
9590
9591 when No_Aspect =>
9592 raise Program_Error;
9593
9594 -- Aspects taking an optional boolean argument
9595
9596 when Boolean_Aspects |
9597 Library_Unit_Aspects =>
9598
9599 T := Standard_Boolean;
9600
9601 -- Aspects corresponding to attribute definition clauses
9602
9603 when Aspect_Address =>
9604 T := RTE (RE_Address);
9605
9606 when Aspect_Attach_Handler =>
9607 T := RTE (RE_Interrupt_ID);
9608
9609 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9610 T := RTE (RE_Bit_Order);
9611
9612 when Aspect_Convention =>
9613 return;
9614
9615 when Aspect_CPU =>
9616 T := RTE (RE_CPU_Range);
9617
9618 -- Default_Component_Value is resolved with the component type
9619
9620 when Aspect_Default_Component_Value =>
9621 T := Component_Type (Entity (ASN));
9622
9623 when Aspect_Default_Storage_Pool =>
9624 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9625
9626 -- Default_Value is resolved with the type entity in question
9627
9628 when Aspect_Default_Value =>
9629 T := Entity (ASN);
9630
9631 when Aspect_Dispatching_Domain =>
9632 T := RTE (RE_Dispatching_Domain);
9633
9634 when Aspect_External_Tag =>
9635 T := Standard_String;
9636
9637 when Aspect_External_Name =>
9638 T := Standard_String;
9639
9640 when Aspect_Link_Name =>
9641 T := Standard_String;
9642
9643 when Aspect_Priority | Aspect_Interrupt_Priority =>
9644 T := Standard_Integer;
9645
9646 when Aspect_Relative_Deadline =>
9647 T := RTE (RE_Time_Span);
9648
9649 when Aspect_Small =>
9650 T := Universal_Real;
9651
9652 -- For a simple storage pool, we have to retrieve the type of the
9653 -- pool object associated with the aspect's corresponding attribute
9654 -- definition clause.
9655
9656 when Aspect_Simple_Storage_Pool =>
9657 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9658
9659 when Aspect_Storage_Pool =>
9660 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9661
9662 when Aspect_Alignment |
9663 Aspect_Component_Size |
9664 Aspect_Machine_Radix |
9665 Aspect_Object_Size |
9666 Aspect_Size |
9667 Aspect_Storage_Size |
9668 Aspect_Stream_Size |
9669 Aspect_Value_Size =>
9670 T := Any_Integer;
9671
9672 when Aspect_Linker_Section =>
9673 T := Standard_String;
9674
9675 when Aspect_Synchronization =>
9676 return;
9677
9678 -- Special case, the expression of these aspects is just an entity
9679 -- that does not need any resolution, so just analyze.
9680
9681 when Aspect_Input |
9682 Aspect_Output |
9683 Aspect_Read |
9684 Aspect_Suppress |
9685 Aspect_Unsuppress |
9686 Aspect_Warnings |
9687 Aspect_Write =>
9688 Analyze (Expression (ASN));
9689 return;
9690
9691 -- Same for Iterator aspects, where the expression is a function
9692 -- name. Legality rules are checked separately.
9693
9694 when Aspect_Constant_Indexing |
9695 Aspect_Default_Iterator |
9696 Aspect_Iterator_Element |
9697 Aspect_Variable_Indexing =>
9698 Analyze (Expression (ASN));
9699 return;
9700
9701 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9702
9703 when Aspect_Iterable =>
9704 T := Entity (ASN);
9705
9706 declare
9707 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9708 Assoc : Node_Id;
9709 Expr : Node_Id;
9710
9711 begin
9712 if Cursor = Any_Type then
9713 return;
9714 end if;
9715
9716 Assoc := First (Component_Associations (Expression (ASN)));
9717 while Present (Assoc) loop
9718 Expr := Expression (Assoc);
9719 Analyze (Expr);
9720
9721 if not Error_Posted (Expr) then
9722 Resolve_Iterable_Operation
9723 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9724 end if;
9725
9726 Next (Assoc);
9727 end loop;
9728 end;
9729
9730 return;
9731
9732 -- Invariant/Predicate take boolean expressions
9733
9734 when Aspect_Dynamic_Predicate |
9735 Aspect_Invariant |
9736 Aspect_Predicate |
9737 Aspect_Static_Predicate |
9738 Aspect_Type_Invariant =>
9739 T := Standard_Boolean;
9740
9741 when Aspect_Predicate_Failure =>
9742 T := Standard_String;
9743
9744 -- Here is the list of aspects that don't require delay analysis
9745
9746 when Aspect_Abstract_State |
9747 Aspect_Annotate |
9748 Aspect_Async_Readers |
9749 Aspect_Async_Writers |
9750 Aspect_Constant_After_Elaboration |
9751 Aspect_Contract_Cases |
9752 Aspect_Default_Initial_Condition |
9753 Aspect_Depends |
9754 Aspect_Dimension |
9755 Aspect_Dimension_System |
9756 Aspect_Effective_Reads |
9757 Aspect_Effective_Writes |
9758 Aspect_Extensions_Visible |
9759 Aspect_Ghost |
9760 Aspect_Global |
9761 Aspect_Implicit_Dereference |
9762 Aspect_Initial_Condition |
9763 Aspect_Initializes |
9764 Aspect_Obsolescent |
9765 Aspect_Part_Of |
9766 Aspect_Post |
9767 Aspect_Postcondition |
9768 Aspect_Pre |
9769 Aspect_Precondition |
9770 Aspect_Refined_Depends |
9771 Aspect_Refined_Global |
9772 Aspect_Refined_Post |
9773 Aspect_Refined_State |
9774 Aspect_SPARK_Mode |
9775 Aspect_Test_Case |
9776 Aspect_Unimplemented |
9777 Aspect_Volatile_Function =>
9778 raise Program_Error;
9779
9780 end case;
9781
9782 -- Do the preanalyze call
9783
9784 Preanalyze_Spec_Expression (Expression (ASN), T);
9785 end Check_Aspect_At_Freeze_Point;
9786
9787 -----------------------------------
9788 -- Check_Constant_Address_Clause --
9789 -----------------------------------
9790
9791 procedure Check_Constant_Address_Clause
9792 (Expr : Node_Id;
9793 U_Ent : Entity_Id)
9794 is
9795 procedure Check_At_Constant_Address (Nod : Node_Id);
9796 -- Checks that the given node N represents a name whose 'Address is
9797 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9798 -- address value is the same at the point of declaration of U_Ent and at
9799 -- the time of elaboration of the address clause.
9800
9801 procedure Check_Expr_Constants (Nod : Node_Id);
9802 -- Checks that Nod meets the requirements for a constant address clause
9803 -- in the sense of the enclosing procedure.
9804
9805 procedure Check_List_Constants (Lst : List_Id);
9806 -- Check that all elements of list Lst meet the requirements for a
9807 -- constant address clause in the sense of the enclosing procedure.
9808
9809 -------------------------------
9810 -- Check_At_Constant_Address --
9811 -------------------------------
9812
9813 procedure Check_At_Constant_Address (Nod : Node_Id) is
9814 begin
9815 if Is_Entity_Name (Nod) then
9816 if Present (Address_Clause (Entity ((Nod)))) then
9817 Error_Msg_NE
9818 ("invalid address clause for initialized object &!",
9819 Nod, U_Ent);
9820 Error_Msg_NE
9821 ("address for& cannot" &
9822 " depend on another address clause! (RM 13.1(22))!",
9823 Nod, U_Ent);
9824
9825 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9826 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9827 then
9828 Error_Msg_NE
9829 ("invalid address clause for initialized object &!",
9830 Nod, U_Ent);
9831 Error_Msg_Node_2 := U_Ent;
9832 Error_Msg_NE
9833 ("\& must be defined before & (RM 13.1(22))!",
9834 Nod, Entity (Nod));
9835 end if;
9836
9837 elsif Nkind (Nod) = N_Selected_Component then
9838 declare
9839 T : constant Entity_Id := Etype (Prefix (Nod));
9840
9841 begin
9842 if (Is_Record_Type (T)
9843 and then Has_Discriminants (T))
9844 or else
9845 (Is_Access_Type (T)
9846 and then Is_Record_Type (Designated_Type (T))
9847 and then Has_Discriminants (Designated_Type (T)))
9848 then
9849 Error_Msg_NE
9850 ("invalid address clause for initialized object &!",
9851 Nod, U_Ent);
9852 Error_Msg_N
9853 ("\address cannot depend on component" &
9854 " of discriminated record (RM 13.1(22))!",
9855 Nod);
9856 else
9857 Check_At_Constant_Address (Prefix (Nod));
9858 end if;
9859 end;
9860
9861 elsif Nkind (Nod) = N_Indexed_Component then
9862 Check_At_Constant_Address (Prefix (Nod));
9863 Check_List_Constants (Expressions (Nod));
9864
9865 else
9866 Check_Expr_Constants (Nod);
9867 end if;
9868 end Check_At_Constant_Address;
9869
9870 --------------------------
9871 -- Check_Expr_Constants --
9872 --------------------------
9873
9874 procedure Check_Expr_Constants (Nod : Node_Id) is
9875 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9876 Ent : Entity_Id := Empty;
9877
9878 begin
9879 if Nkind (Nod) in N_Has_Etype
9880 and then Etype (Nod) = Any_Type
9881 then
9882 return;
9883 end if;
9884
9885 case Nkind (Nod) is
9886 when N_Empty | N_Error =>
9887 return;
9888
9889 when N_Identifier | N_Expanded_Name =>
9890 Ent := Entity (Nod);
9891
9892 -- We need to look at the original node if it is different
9893 -- from the node, since we may have rewritten things and
9894 -- substituted an identifier representing the rewrite.
9895
9896 if Original_Node (Nod) /= Nod then
9897 Check_Expr_Constants (Original_Node (Nod));
9898
9899 -- If the node is an object declaration without initial
9900 -- value, some code has been expanded, and the expression
9901 -- is not constant, even if the constituents might be
9902 -- acceptable, as in A'Address + offset.
9903
9904 if Ekind (Ent) = E_Variable
9905 and then
9906 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9907 and then
9908 No (Expression (Declaration_Node (Ent)))
9909 then
9910 Error_Msg_NE
9911 ("invalid address clause for initialized object &!",
9912 Nod, U_Ent);
9913
9914 -- If entity is constant, it may be the result of expanding
9915 -- a check. We must verify that its declaration appears
9916 -- before the object in question, else we also reject the
9917 -- address clause.
9918
9919 elsif Ekind (Ent) = E_Constant
9920 and then In_Same_Source_Unit (Ent, U_Ent)
9921 and then Sloc (Ent) > Loc_U_Ent
9922 then
9923 Error_Msg_NE
9924 ("invalid address clause for initialized object &!",
9925 Nod, U_Ent);
9926 end if;
9927
9928 return;
9929 end if;
9930
9931 -- Otherwise look at the identifier and see if it is OK
9932
9933 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9934 or else Is_Type (Ent)
9935 then
9936 return;
9937
9938 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9939
9940 -- This is the case where we must have Ent defined before
9941 -- U_Ent. Clearly if they are in different units this
9942 -- requirement is met since the unit containing Ent is
9943 -- already processed.
9944
9945 if not In_Same_Source_Unit (Ent, U_Ent) then
9946 return;
9947
9948 -- Otherwise location of Ent must be before the location
9949 -- of U_Ent, that's what prior defined means.
9950
9951 elsif Sloc (Ent) < Loc_U_Ent then
9952 return;
9953
9954 else
9955 Error_Msg_NE
9956 ("invalid address clause for initialized object &!",
9957 Nod, U_Ent);
9958 Error_Msg_Node_2 := U_Ent;
9959 Error_Msg_NE
9960 ("\& must be defined before & (RM 13.1(22))!",
9961 Nod, Ent);
9962 end if;
9963
9964 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9965 Check_Expr_Constants (Original_Node (Nod));
9966
9967 else
9968 Error_Msg_NE
9969 ("invalid address clause for initialized object &!",
9970 Nod, U_Ent);
9971
9972 if Comes_From_Source (Ent) then
9973 Error_Msg_NE
9974 ("\reference to variable& not allowed"
9975 & " (RM 13.1(22))!", Nod, Ent);
9976 else
9977 Error_Msg_N
9978 ("non-static expression not allowed"
9979 & " (RM 13.1(22))!", Nod);
9980 end if;
9981 end if;
9982
9983 when N_Integer_Literal =>
9984
9985 -- If this is a rewritten unchecked conversion, in a system
9986 -- where Address is an integer type, always use the base type
9987 -- for a literal value. This is user-friendly and prevents
9988 -- order-of-elaboration issues with instances of unchecked
9989 -- conversion.
9990
9991 if Nkind (Original_Node (Nod)) = N_Function_Call then
9992 Set_Etype (Nod, Base_Type (Etype (Nod)));
9993 end if;
9994
9995 when N_Real_Literal |
9996 N_String_Literal |
9997 N_Character_Literal =>
9998 return;
9999
10000 when N_Range =>
10001 Check_Expr_Constants (Low_Bound (Nod));
10002 Check_Expr_Constants (High_Bound (Nod));
10003
10004 when N_Explicit_Dereference =>
10005 Check_Expr_Constants (Prefix (Nod));
10006
10007 when N_Indexed_Component =>
10008 Check_Expr_Constants (Prefix (Nod));
10009 Check_List_Constants (Expressions (Nod));
10010
10011 when N_Slice =>
10012 Check_Expr_Constants (Prefix (Nod));
10013 Check_Expr_Constants (Discrete_Range (Nod));
10014
10015 when N_Selected_Component =>
10016 Check_Expr_Constants (Prefix (Nod));
10017
10018 when N_Attribute_Reference =>
10019 if Nam_In (Attribute_Name (Nod), Name_Address,
10020 Name_Access,
10021 Name_Unchecked_Access,
10022 Name_Unrestricted_Access)
10023 then
10024 Check_At_Constant_Address (Prefix (Nod));
10025
10026 else
10027 Check_Expr_Constants (Prefix (Nod));
10028 Check_List_Constants (Expressions (Nod));
10029 end if;
10030
10031 when N_Aggregate =>
10032 Check_List_Constants (Component_Associations (Nod));
10033 Check_List_Constants (Expressions (Nod));
10034
10035 when N_Component_Association =>
10036 Check_Expr_Constants (Expression (Nod));
10037
10038 when N_Extension_Aggregate =>
10039 Check_Expr_Constants (Ancestor_Part (Nod));
10040 Check_List_Constants (Component_Associations (Nod));
10041 Check_List_Constants (Expressions (Nod));
10042
10043 when N_Null =>
10044 return;
10045
10046 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
10047 Check_Expr_Constants (Left_Opnd (Nod));
10048 Check_Expr_Constants (Right_Opnd (Nod));
10049
10050 when N_Unary_Op =>
10051 Check_Expr_Constants (Right_Opnd (Nod));
10052
10053 when N_Type_Conversion |
10054 N_Qualified_Expression |
10055 N_Allocator |
10056 N_Unchecked_Type_Conversion =>
10057 Check_Expr_Constants (Expression (Nod));
10058
10059 when N_Function_Call =>
10060 if not Is_Pure (Entity (Name (Nod))) then
10061 Error_Msg_NE
10062 ("invalid address clause for initialized object &!",
10063 Nod, U_Ent);
10064
10065 Error_Msg_NE
10066 ("\function & is not pure (RM 13.1(22))!",
10067 Nod, Entity (Name (Nod)));
10068
10069 else
10070 Check_List_Constants (Parameter_Associations (Nod));
10071 end if;
10072
10073 when N_Parameter_Association =>
10074 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
10075
10076 when others =>
10077 Error_Msg_NE
10078 ("invalid address clause for initialized object &!",
10079 Nod, U_Ent);
10080 Error_Msg_NE
10081 ("\must be constant defined before& (RM 13.1(22))!",
10082 Nod, U_Ent);
10083 end case;
10084 end Check_Expr_Constants;
10085
10086 --------------------------
10087 -- Check_List_Constants --
10088 --------------------------
10089
10090 procedure Check_List_Constants (Lst : List_Id) is
10091 Nod1 : Node_Id;
10092
10093 begin
10094 if Present (Lst) then
10095 Nod1 := First (Lst);
10096 while Present (Nod1) loop
10097 Check_Expr_Constants (Nod1);
10098 Next (Nod1);
10099 end loop;
10100 end if;
10101 end Check_List_Constants;
10102
10103 -- Start of processing for Check_Constant_Address_Clause
10104
10105 begin
10106 -- If rep_clauses are to be ignored, no need for legality checks. In
10107 -- particular, no need to pester user about rep clauses that violate the
10108 -- rule on constant addresses, given that these clauses will be removed
10109 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
10110 -- we want to relax these checks.
10111
10112 if not Ignore_Rep_Clauses and not CodePeer_Mode then
10113 Check_Expr_Constants (Expr);
10114 end if;
10115 end Check_Constant_Address_Clause;
10116
10117 ---------------------------
10118 -- Check_Pool_Size_Clash --
10119 ---------------------------
10120
10121 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
10122 Post : Node_Id;
10123
10124 begin
10125 -- We need to find out which one came first. Note that in the case of
10126 -- aspects mixed with pragmas there are cases where the processing order
10127 -- is reversed, which is why we do the check here.
10128
10129 if Sloc (SP) < Sloc (SS) then
10130 Error_Msg_Sloc := Sloc (SP);
10131 Post := SS;
10132 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
10133
10134 else
10135 Error_Msg_Sloc := Sloc (SS);
10136 Post := SP;
10137 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
10138 end if;
10139
10140 Error_Msg_N
10141 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
10142 end Check_Pool_Size_Clash;
10143
10144 ----------------------------------------
10145 -- Check_Record_Representation_Clause --
10146 ----------------------------------------
10147
10148 procedure Check_Record_Representation_Clause (N : Node_Id) is
10149 Loc : constant Source_Ptr := Sloc (N);
10150 Ident : constant Node_Id := Identifier (N);
10151 Rectype : Entity_Id;
10152 Fent : Entity_Id;
10153 CC : Node_Id;
10154 Fbit : Uint;
10155 Lbit : Uint;
10156 Hbit : Uint := Uint_0;
10157 Comp : Entity_Id;
10158 Pcomp : Entity_Id;
10159
10160 Max_Bit_So_Far : Uint;
10161 -- Records the maximum bit position so far. If all field positions
10162 -- are monotonically increasing, then we can skip the circuit for
10163 -- checking for overlap, since no overlap is possible.
10164
10165 Tagged_Parent : Entity_Id := Empty;
10166 -- This is set in the case of a derived tagged type for which we have
10167 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
10168 -- positioned by record representation clauses). In this case we must
10169 -- check for overlap between components of this tagged type, and the
10170 -- components of its parent. Tagged_Parent will point to this parent
10171 -- type. For all other cases Tagged_Parent is left set to Empty.
10172
10173 Parent_Last_Bit : Uint;
10174 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
10175 -- last bit position for any field in the parent type. We only need to
10176 -- check overlap for fields starting below this point.
10177
10178 Overlap_Check_Required : Boolean;
10179 -- Used to keep track of whether or not an overlap check is required
10180
10181 Overlap_Detected : Boolean := False;
10182 -- Set True if an overlap is detected
10183
10184 Ccount : Natural := 0;
10185 -- Number of component clauses in record rep clause
10186
10187 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
10188 -- Given two entities for record components or discriminants, checks
10189 -- if they have overlapping component clauses and issues errors if so.
10190
10191 procedure Find_Component;
10192 -- Finds component entity corresponding to current component clause (in
10193 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
10194 -- start/stop bits for the field. If there is no matching component or
10195 -- if the matching component does not have a component clause, then
10196 -- that's an error and Comp is set to Empty, but no error message is
10197 -- issued, since the message was already given. Comp is also set to
10198 -- Empty if the current "component clause" is in fact a pragma.
10199
10200 -----------------------------
10201 -- Check_Component_Overlap --
10202 -----------------------------
10203
10204 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
10205 CC1 : constant Node_Id := Component_Clause (C1_Ent);
10206 CC2 : constant Node_Id := Component_Clause (C2_Ent);
10207
10208 begin
10209 if Present (CC1) and then Present (CC2) then
10210
10211 -- Exclude odd case where we have two tag components in the same
10212 -- record, both at location zero. This seems a bit strange, but
10213 -- it seems to happen in some circumstances, perhaps on an error.
10214
10215 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
10216 return;
10217 end if;
10218
10219 -- Here we check if the two fields overlap
10220
10221 declare
10222 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
10223 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
10224 E1 : constant Uint := S1 + Esize (C1_Ent);
10225 E2 : constant Uint := S2 + Esize (C2_Ent);
10226
10227 begin
10228 if E2 <= S1 or else E1 <= S2 then
10229 null;
10230 else
10231 Error_Msg_Node_2 := Component_Name (CC2);
10232 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
10233 Error_Msg_Node_1 := Component_Name (CC1);
10234 Error_Msg_N
10235 ("component& overlaps & #", Component_Name (CC1));
10236 Overlap_Detected := True;
10237 end if;
10238 end;
10239 end if;
10240 end Check_Component_Overlap;
10241
10242 --------------------
10243 -- Find_Component --
10244 --------------------
10245
10246 procedure Find_Component is
10247
10248 procedure Search_Component (R : Entity_Id);
10249 -- Search components of R for a match. If found, Comp is set
10250
10251 ----------------------
10252 -- Search_Component --
10253 ----------------------
10254
10255 procedure Search_Component (R : Entity_Id) is
10256 begin
10257 Comp := First_Component_Or_Discriminant (R);
10258 while Present (Comp) loop
10259
10260 -- Ignore error of attribute name for component name (we
10261 -- already gave an error message for this, so no need to
10262 -- complain here)
10263
10264 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
10265 null;
10266 else
10267 exit when Chars (Comp) = Chars (Component_Name (CC));
10268 end if;
10269
10270 Next_Component_Or_Discriminant (Comp);
10271 end loop;
10272 end Search_Component;
10273
10274 -- Start of processing for Find_Component
10275
10276 begin
10277 -- Return with Comp set to Empty if we have a pragma
10278
10279 if Nkind (CC) = N_Pragma then
10280 Comp := Empty;
10281 return;
10282 end if;
10283
10284 -- Search current record for matching component
10285
10286 Search_Component (Rectype);
10287
10288 -- If not found, maybe component of base type discriminant that is
10289 -- absent from statically constrained first subtype.
10290
10291 if No (Comp) then
10292 Search_Component (Base_Type (Rectype));
10293 end if;
10294
10295 -- If no component, or the component does not reference the component
10296 -- clause in question, then there was some previous error for which
10297 -- we already gave a message, so just return with Comp Empty.
10298
10299 if No (Comp) or else Component_Clause (Comp) /= CC then
10300 Check_Error_Detected;
10301 Comp := Empty;
10302
10303 -- Normal case where we have a component clause
10304
10305 else
10306 Fbit := Component_Bit_Offset (Comp);
10307 Lbit := Fbit + Esize (Comp) - 1;
10308 end if;
10309 end Find_Component;
10310
10311 -- Start of processing for Check_Record_Representation_Clause
10312
10313 begin
10314 Find_Type (Ident);
10315 Rectype := Entity (Ident);
10316
10317 if Rectype = Any_Type then
10318 return;
10319 else
10320 Rectype := Underlying_Type (Rectype);
10321 end if;
10322
10323 -- See if we have a fully repped derived tagged type
10324
10325 declare
10326 PS : constant Entity_Id := Parent_Subtype (Rectype);
10327
10328 begin
10329 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
10330 Tagged_Parent := PS;
10331
10332 -- Find maximum bit of any component of the parent type
10333
10334 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
10335 Pcomp := First_Entity (Tagged_Parent);
10336 while Present (Pcomp) loop
10337 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
10338 if Component_Bit_Offset (Pcomp) /= No_Uint
10339 and then Known_Static_Esize (Pcomp)
10340 then
10341 Parent_Last_Bit :=
10342 UI_Max
10343 (Parent_Last_Bit,
10344 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
10345 end if;
10346 else
10347
10348 -- Skip anonymous types generated for constrained array
10349 -- or record components.
10350
10351 null;
10352 end if;
10353
10354 Next_Entity (Pcomp);
10355 end loop;
10356 end if;
10357 end;
10358
10359 -- All done if no component clauses
10360
10361 CC := First (Component_Clauses (N));
10362
10363 if No (CC) then
10364 return;
10365 end if;
10366
10367 -- If a tag is present, then create a component clause that places it
10368 -- at the start of the record (otherwise gigi may place it after other
10369 -- fields that have rep clauses).
10370
10371 Fent := First_Entity (Rectype);
10372
10373 if Nkind (Fent) = N_Defining_Identifier
10374 and then Chars (Fent) = Name_uTag
10375 then
10376 Set_Component_Bit_Offset (Fent, Uint_0);
10377 Set_Normalized_Position (Fent, Uint_0);
10378 Set_Normalized_First_Bit (Fent, Uint_0);
10379 Set_Normalized_Position_Max (Fent, Uint_0);
10380 Init_Esize (Fent, System_Address_Size);
10381
10382 Set_Component_Clause (Fent,
10383 Make_Component_Clause (Loc,
10384 Component_Name => Make_Identifier (Loc, Name_uTag),
10385
10386 Position => Make_Integer_Literal (Loc, Uint_0),
10387 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10388 Last_Bit =>
10389 Make_Integer_Literal (Loc,
10390 UI_From_Int (System_Address_Size))));
10391
10392 Ccount := Ccount + 1;
10393 end if;
10394
10395 Max_Bit_So_Far := Uint_Minus_1;
10396 Overlap_Check_Required := False;
10397
10398 -- Process the component clauses
10399
10400 while Present (CC) loop
10401 Find_Component;
10402
10403 if Present (Comp) then
10404 Ccount := Ccount + 1;
10405
10406 -- We need a full overlap check if record positions non-monotonic
10407
10408 if Fbit <= Max_Bit_So_Far then
10409 Overlap_Check_Required := True;
10410 end if;
10411
10412 Max_Bit_So_Far := Lbit;
10413
10414 -- Check bit position out of range of specified size
10415
10416 if Has_Size_Clause (Rectype)
10417 and then RM_Size (Rectype) <= Lbit
10418 then
10419 Error_Msg_N
10420 ("bit number out of range of specified size",
10421 Last_Bit (CC));
10422
10423 -- Check for overlap with tag component
10424
10425 else
10426 if Is_Tagged_Type (Rectype)
10427 and then Fbit < System_Address_Size
10428 then
10429 Error_Msg_NE
10430 ("component overlaps tag field of&",
10431 Component_Name (CC), Rectype);
10432 Overlap_Detected := True;
10433 end if;
10434
10435 if Hbit < Lbit then
10436 Hbit := Lbit;
10437 end if;
10438 end if;
10439
10440 -- Check parent overlap if component might overlap parent field
10441
10442 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
10443 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
10444 while Present (Pcomp) loop
10445 if not Is_Tag (Pcomp)
10446 and then Chars (Pcomp) /= Name_uParent
10447 then
10448 Check_Component_Overlap (Comp, Pcomp);
10449 end if;
10450
10451 Next_Component_Or_Discriminant (Pcomp);
10452 end loop;
10453 end if;
10454 end if;
10455
10456 Next (CC);
10457 end loop;
10458
10459 -- Now that we have processed all the component clauses, check for
10460 -- overlap. We have to leave this till last, since the components can
10461 -- appear in any arbitrary order in the representation clause.
10462
10463 -- We do not need this check if all specified ranges were monotonic,
10464 -- as recorded by Overlap_Check_Required being False at this stage.
10465
10466 -- This first section checks if there are any overlapping entries at
10467 -- all. It does this by sorting all entries and then seeing if there are
10468 -- any overlaps. If there are none, then that is decisive, but if there
10469 -- are overlaps, they may still be OK (they may result from fields in
10470 -- different variants).
10471
10472 if Overlap_Check_Required then
10473 Overlap_Check1 : declare
10474
10475 OC_Fbit : array (0 .. Ccount) of Uint;
10476 -- First-bit values for component clauses, the value is the offset
10477 -- of the first bit of the field from start of record. The zero
10478 -- entry is for use in sorting.
10479
10480 OC_Lbit : array (0 .. Ccount) of Uint;
10481 -- Last-bit values for component clauses, the value is the offset
10482 -- of the last bit of the field from start of record. The zero
10483 -- entry is for use in sorting.
10484
10485 OC_Count : Natural := 0;
10486 -- Count of entries in OC_Fbit and OC_Lbit
10487
10488 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10489 -- Compare routine for Sort
10490
10491 procedure OC_Move (From : Natural; To : Natural);
10492 -- Move routine for Sort
10493
10494 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
10495
10496 -----------
10497 -- OC_Lt --
10498 -----------
10499
10500 function OC_Lt (Op1, Op2 : Natural) return Boolean is
10501 begin
10502 return OC_Fbit (Op1) < OC_Fbit (Op2);
10503 end OC_Lt;
10504
10505 -------------
10506 -- OC_Move --
10507 -------------
10508
10509 procedure OC_Move (From : Natural; To : Natural) is
10510 begin
10511 OC_Fbit (To) := OC_Fbit (From);
10512 OC_Lbit (To) := OC_Lbit (From);
10513 end OC_Move;
10514
10515 -- Start of processing for Overlap_Check
10516
10517 begin
10518 CC := First (Component_Clauses (N));
10519 while Present (CC) loop
10520
10521 -- Exclude component clause already marked in error
10522
10523 if not Error_Posted (CC) then
10524 Find_Component;
10525
10526 if Present (Comp) then
10527 OC_Count := OC_Count + 1;
10528 OC_Fbit (OC_Count) := Fbit;
10529 OC_Lbit (OC_Count) := Lbit;
10530 end if;
10531 end if;
10532
10533 Next (CC);
10534 end loop;
10535
10536 Sorting.Sort (OC_Count);
10537
10538 Overlap_Check_Required := False;
10539 for J in 1 .. OC_Count - 1 loop
10540 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10541 Overlap_Check_Required := True;
10542 exit;
10543 end if;
10544 end loop;
10545 end Overlap_Check1;
10546 end if;
10547
10548 -- If Overlap_Check_Required is still True, then we have to do the full
10549 -- scale overlap check, since we have at least two fields that do
10550 -- overlap, and we need to know if that is OK since they are in
10551 -- different variant, or whether we have a definite problem.
10552
10553 if Overlap_Check_Required then
10554 Overlap_Check2 : declare
10555 C1_Ent, C2_Ent : Entity_Id;
10556 -- Entities of components being checked for overlap
10557
10558 Clist : Node_Id;
10559 -- Component_List node whose Component_Items are being checked
10560
10561 Citem : Node_Id;
10562 -- Component declaration for component being checked
10563
10564 begin
10565 C1_Ent := First_Entity (Base_Type (Rectype));
10566
10567 -- Loop through all components in record. For each component check
10568 -- for overlap with any of the preceding elements on the component
10569 -- list containing the component and also, if the component is in
10570 -- a variant, check against components outside the case structure.
10571 -- This latter test is repeated recursively up the variant tree.
10572
10573 Main_Component_Loop : while Present (C1_Ent) loop
10574 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10575 goto Continue_Main_Component_Loop;
10576 end if;
10577
10578 -- Skip overlap check if entity has no declaration node. This
10579 -- happens with discriminants in constrained derived types.
10580 -- Possibly we are missing some checks as a result, but that
10581 -- does not seem terribly serious.
10582
10583 if No (Declaration_Node (C1_Ent)) then
10584 goto Continue_Main_Component_Loop;
10585 end if;
10586
10587 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10588
10589 -- Loop through component lists that need checking. Check the
10590 -- current component list and all lists in variants above us.
10591
10592 Component_List_Loop : loop
10593
10594 -- If derived type definition, go to full declaration
10595 -- If at outer level, check discriminants if there are any.
10596
10597 if Nkind (Clist) = N_Derived_Type_Definition then
10598 Clist := Parent (Clist);
10599 end if;
10600
10601 -- Outer level of record definition, check discriminants
10602
10603 if Nkind_In (Clist, N_Full_Type_Declaration,
10604 N_Private_Type_Declaration)
10605 then
10606 if Has_Discriminants (Defining_Identifier (Clist)) then
10607 C2_Ent :=
10608 First_Discriminant (Defining_Identifier (Clist));
10609 while Present (C2_Ent) loop
10610 exit when C1_Ent = C2_Ent;
10611 Check_Component_Overlap (C1_Ent, C2_Ent);
10612 Next_Discriminant (C2_Ent);
10613 end loop;
10614 end if;
10615
10616 -- Record extension case
10617
10618 elsif Nkind (Clist) = N_Derived_Type_Definition then
10619 Clist := Empty;
10620
10621 -- Otherwise check one component list
10622
10623 else
10624 Citem := First (Component_Items (Clist));
10625 while Present (Citem) loop
10626 if Nkind (Citem) = N_Component_Declaration then
10627 C2_Ent := Defining_Identifier (Citem);
10628 exit when C1_Ent = C2_Ent;
10629 Check_Component_Overlap (C1_Ent, C2_Ent);
10630 end if;
10631
10632 Next (Citem);
10633 end loop;
10634 end if;
10635
10636 -- Check for variants above us (the parent of the Clist can
10637 -- be a variant, in which case its parent is a variant part,
10638 -- and the parent of the variant part is a component list
10639 -- whose components must all be checked against the current
10640 -- component for overlap).
10641
10642 if Nkind (Parent (Clist)) = N_Variant then
10643 Clist := Parent (Parent (Parent (Clist)));
10644
10645 -- Check for possible discriminant part in record, this
10646 -- is treated essentially as another level in the
10647 -- recursion. For this case the parent of the component
10648 -- list is the record definition, and its parent is the
10649 -- full type declaration containing the discriminant
10650 -- specifications.
10651
10652 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10653 Clist := Parent (Parent ((Clist)));
10654
10655 -- If neither of these two cases, we are at the top of
10656 -- the tree.
10657
10658 else
10659 exit Component_List_Loop;
10660 end if;
10661 end loop Component_List_Loop;
10662
10663 <<Continue_Main_Component_Loop>>
10664 Next_Entity (C1_Ent);
10665
10666 end loop Main_Component_Loop;
10667 end Overlap_Check2;
10668 end if;
10669
10670 -- The following circuit deals with warning on record holes (gaps). We
10671 -- skip this check if overlap was detected, since it makes sense for the
10672 -- programmer to fix this illegality before worrying about warnings.
10673
10674 if not Overlap_Detected and Warn_On_Record_Holes then
10675 Record_Hole_Check : declare
10676 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10677 -- Full declaration of record type
10678
10679 procedure Check_Component_List
10680 (CL : Node_Id;
10681 Sbit : Uint;
10682 DS : List_Id);
10683 -- Check component list CL for holes. The starting bit should be
10684 -- Sbit. which is zero for the main record component list and set
10685 -- appropriately for recursive calls for variants. DS is set to
10686 -- a list of discriminant specifications to be included in the
10687 -- consideration of components. It is No_List if none to consider.
10688
10689 --------------------------
10690 -- Check_Component_List --
10691 --------------------------
10692
10693 procedure Check_Component_List
10694 (CL : Node_Id;
10695 Sbit : Uint;
10696 DS : List_Id)
10697 is
10698 Compl : Integer;
10699
10700 begin
10701 Compl := Integer (List_Length (Component_Items (CL)));
10702
10703 if DS /= No_List then
10704 Compl := Compl + Integer (List_Length (DS));
10705 end if;
10706
10707 declare
10708 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10709 -- Gather components (zero entry is for sort routine)
10710
10711 Ncomps : Natural := 0;
10712 -- Number of entries stored in Comps (starting at Comps (1))
10713
10714 Citem : Node_Id;
10715 -- One component item or discriminant specification
10716
10717 Nbit : Uint;
10718 -- Starting bit for next component
10719
10720 CEnt : Entity_Id;
10721 -- Component entity
10722
10723 Variant : Node_Id;
10724 -- One variant
10725
10726 function Lt (Op1, Op2 : Natural) return Boolean;
10727 -- Compare routine for Sort
10728
10729 procedure Move (From : Natural; To : Natural);
10730 -- Move routine for Sort
10731
10732 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10733
10734 --------
10735 -- Lt --
10736 --------
10737
10738 function Lt (Op1, Op2 : Natural) return Boolean is
10739 begin
10740 return Component_Bit_Offset (Comps (Op1))
10741 <
10742 Component_Bit_Offset (Comps (Op2));
10743 end Lt;
10744
10745 ----------
10746 -- Move --
10747 ----------
10748
10749 procedure Move (From : Natural; To : Natural) is
10750 begin
10751 Comps (To) := Comps (From);
10752 end Move;
10753
10754 begin
10755 -- Gather discriminants into Comp
10756
10757 if DS /= No_List then
10758 Citem := First (DS);
10759 while Present (Citem) loop
10760 if Nkind (Citem) = N_Discriminant_Specification then
10761 declare
10762 Ent : constant Entity_Id :=
10763 Defining_Identifier (Citem);
10764 begin
10765 if Ekind (Ent) = E_Discriminant then
10766 Ncomps := Ncomps + 1;
10767 Comps (Ncomps) := Ent;
10768 end if;
10769 end;
10770 end if;
10771
10772 Next (Citem);
10773 end loop;
10774 end if;
10775
10776 -- Gather component entities into Comp
10777
10778 Citem := First (Component_Items (CL));
10779 while Present (Citem) loop
10780 if Nkind (Citem) = N_Component_Declaration then
10781 Ncomps := Ncomps + 1;
10782 Comps (Ncomps) := Defining_Identifier (Citem);
10783 end if;
10784
10785 Next (Citem);
10786 end loop;
10787
10788 -- Now sort the component entities based on the first bit.
10789 -- Note we already know there are no overlapping components.
10790
10791 Sorting.Sort (Ncomps);
10792
10793 -- Loop through entries checking for holes
10794
10795 Nbit := Sbit;
10796 for J in 1 .. Ncomps loop
10797 CEnt := Comps (J);
10798 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10799
10800 if Error_Msg_Uint_1 > 0 then
10801 Error_Msg_NE
10802 ("?H?^-bit gap before component&",
10803 Component_Name (Component_Clause (CEnt)), CEnt);
10804 end if;
10805
10806 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10807 end loop;
10808
10809 -- Process variant parts recursively if present
10810
10811 if Present (Variant_Part (CL)) then
10812 Variant := First (Variants (Variant_Part (CL)));
10813 while Present (Variant) loop
10814 Check_Component_List
10815 (Component_List (Variant), Nbit, No_List);
10816 Next (Variant);
10817 end loop;
10818 end if;
10819 end;
10820 end Check_Component_List;
10821
10822 -- Start of processing for Record_Hole_Check
10823
10824 begin
10825 declare
10826 Sbit : Uint;
10827
10828 begin
10829 if Is_Tagged_Type (Rectype) then
10830 Sbit := UI_From_Int (System_Address_Size);
10831 else
10832 Sbit := Uint_0;
10833 end if;
10834
10835 if Nkind (Decl) = N_Full_Type_Declaration
10836 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10837 then
10838 Check_Component_List
10839 (Component_List (Type_Definition (Decl)),
10840 Sbit,
10841 Discriminant_Specifications (Decl));
10842 end if;
10843 end;
10844 end Record_Hole_Check;
10845 end if;
10846
10847 -- For records that have component clauses for all components, and whose
10848 -- size is less than or equal to 32, we need to know the size in the
10849 -- front end to activate possible packed array processing where the
10850 -- component type is a record.
10851
10852 -- At this stage Hbit + 1 represents the first unused bit from all the
10853 -- component clauses processed, so if the component clauses are
10854 -- complete, then this is the length of the record.
10855
10856 -- For records longer than System.Storage_Unit, and for those where not
10857 -- all components have component clauses, the back end determines the
10858 -- length (it may for example be appropriate to round up the size
10859 -- to some convenient boundary, based on alignment considerations, etc).
10860
10861 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10862
10863 -- Nothing to do if at least one component has no component clause
10864
10865 Comp := First_Component_Or_Discriminant (Rectype);
10866 while Present (Comp) loop
10867 exit when No (Component_Clause (Comp));
10868 Next_Component_Or_Discriminant (Comp);
10869 end loop;
10870
10871 -- If we fall out of loop, all components have component clauses
10872 -- and so we can set the size to the maximum value.
10873
10874 if No (Comp) then
10875 Set_RM_Size (Rectype, Hbit + 1);
10876 end if;
10877 end if;
10878 end Check_Record_Representation_Clause;
10879
10880 ----------------
10881 -- Check_Size --
10882 ----------------
10883
10884 procedure Check_Size
10885 (N : Node_Id;
10886 T : Entity_Id;
10887 Siz : Uint;
10888 Biased : out Boolean)
10889 is
10890 procedure Size_Too_Small_Error (Min_Siz : Uint);
10891 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10892 -- minimum size.
10893
10894 --------------------------
10895 -- Size_Too_Small_Error --
10896 --------------------------
10897
10898 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10899 begin
10900 -- This error is suppressed in ASIS mode to allow for different ASIS
10901 -- back ends or ASIS-based tools to query the illegal clause.
10902
10903 if not ASIS_Mode then
10904 Error_Msg_Uint_1 := Min_Siz;
10905 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
10906 end if;
10907 end Size_Too_Small_Error;
10908
10909 -- Local variables
10910
10911 UT : constant Entity_Id := Underlying_Type (T);
10912 M : Uint;
10913
10914 -- Start of processing for Check_Size
10915
10916 begin
10917 Biased := False;
10918
10919 -- Reject patently improper size values
10920
10921 if Is_Elementary_Type (T)
10922 and then Siz > UI_From_Int (Int'Last)
10923 then
10924 Error_Msg_N ("Size value too large for elementary type", N);
10925
10926 if Nkind (Original_Node (N)) = N_Op_Expon then
10927 Error_Msg_N
10928 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10929 end if;
10930 end if;
10931
10932 -- Dismiss generic types
10933
10934 if Is_Generic_Type (T)
10935 or else
10936 Is_Generic_Type (UT)
10937 or else
10938 Is_Generic_Type (Root_Type (UT))
10939 then
10940 return;
10941
10942 -- Guard against previous errors
10943
10944 elsif No (UT) or else UT = Any_Type then
10945 Check_Error_Detected;
10946 return;
10947
10948 -- Check case of bit packed array
10949
10950 elsif Is_Array_Type (UT)
10951 and then Known_Static_Component_Size (UT)
10952 and then Is_Bit_Packed_Array (UT)
10953 then
10954 declare
10955 Asiz : Uint;
10956 Indx : Node_Id;
10957 Ityp : Entity_Id;
10958
10959 begin
10960 Asiz := Component_Size (UT);
10961 Indx := First_Index (UT);
10962 loop
10963 Ityp := Etype (Indx);
10964
10965 -- If non-static bound, then we are not in the business of
10966 -- trying to check the length, and indeed an error will be
10967 -- issued elsewhere, since sizes of non-static array types
10968 -- cannot be set implicitly or explicitly.
10969
10970 if not Is_OK_Static_Subtype (Ityp) then
10971 return;
10972 end if;
10973
10974 -- Otherwise accumulate next dimension
10975
10976 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10977 Expr_Value (Type_Low_Bound (Ityp)) +
10978 Uint_1);
10979
10980 Next_Index (Indx);
10981 exit when No (Indx);
10982 end loop;
10983
10984 if Asiz <= Siz then
10985 return;
10986
10987 else
10988 Size_Too_Small_Error (Asiz);
10989 Set_Esize (T, Asiz);
10990 Set_RM_Size (T, Asiz);
10991 end if;
10992 end;
10993
10994 -- All other composite types are ignored
10995
10996 elsif Is_Composite_Type (UT) then
10997 return;
10998
10999 -- For fixed-point types, don't check minimum if type is not frozen,
11000 -- since we don't know all the characteristics of the type that can
11001 -- affect the size (e.g. a specified small) till freeze time.
11002
11003 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
11004 null;
11005
11006 -- Cases for which a minimum check is required
11007
11008 else
11009 -- Ignore if specified size is correct for the type
11010
11011 if Known_Esize (UT) and then Siz = Esize (UT) then
11012 return;
11013 end if;
11014
11015 -- Otherwise get minimum size
11016
11017 M := UI_From_Int (Minimum_Size (UT));
11018
11019 if Siz < M then
11020
11021 -- Size is less than minimum size, but one possibility remains
11022 -- that we can manage with the new size if we bias the type.
11023
11024 M := UI_From_Int (Minimum_Size (UT, Biased => True));
11025
11026 if Siz < M then
11027 Size_Too_Small_Error (M);
11028 Set_Esize (T, M);
11029 Set_RM_Size (T, M);
11030 else
11031 Biased := True;
11032 end if;
11033 end if;
11034 end if;
11035 end Check_Size;
11036
11037 --------------------------
11038 -- Freeze_Entity_Checks --
11039 --------------------------
11040
11041 procedure Freeze_Entity_Checks (N : Node_Id) is
11042 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
11043 -- Inspect the primitive operations of type Typ and hide all pairs of
11044 -- implicitly declared non-overridden non-fully conformant homographs
11045 -- (Ada RM 8.3 12.3/2).
11046
11047 -------------------------------------
11048 -- Hide_Non_Overridden_Subprograms --
11049 -------------------------------------
11050
11051 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
11052 procedure Hide_Matching_Homographs
11053 (Subp_Id : Entity_Id;
11054 Start_Elmt : Elmt_Id);
11055 -- Inspect a list of primitive operations starting with Start_Elmt
11056 -- and find matching implicitly declared non-overridden non-fully
11057 -- conformant homographs of Subp_Id. If found, all matches along
11058 -- with Subp_Id are hidden from all visibility.
11059
11060 function Is_Non_Overridden_Or_Null_Procedure
11061 (Subp_Id : Entity_Id) return Boolean;
11062 -- Determine whether subprogram Subp_Id is implicitly declared non-
11063 -- overridden subprogram or an implicitly declared null procedure.
11064
11065 ------------------------------
11066 -- Hide_Matching_Homographs --
11067 ------------------------------
11068
11069 procedure Hide_Matching_Homographs
11070 (Subp_Id : Entity_Id;
11071 Start_Elmt : Elmt_Id)
11072 is
11073 Prim : Entity_Id;
11074 Prim_Elmt : Elmt_Id;
11075
11076 begin
11077 Prim_Elmt := Start_Elmt;
11078 while Present (Prim_Elmt) loop
11079 Prim := Node (Prim_Elmt);
11080
11081 -- The current primitive is implicitly declared non-overridden
11082 -- non-fully conformant homograph of Subp_Id. Both subprograms
11083 -- must be hidden from visibility.
11084
11085 if Chars (Prim) = Chars (Subp_Id)
11086 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
11087 and then not Fully_Conformant (Prim, Subp_Id)
11088 then
11089 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
11090 Set_Is_Immediately_Visible (Prim, False);
11091 Set_Is_Potentially_Use_Visible (Prim, False);
11092
11093 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
11094 Set_Is_Immediately_Visible (Subp_Id, False);
11095 Set_Is_Potentially_Use_Visible (Subp_Id, False);
11096 end if;
11097
11098 Next_Elmt (Prim_Elmt);
11099 end loop;
11100 end Hide_Matching_Homographs;
11101
11102 -----------------------------------------
11103 -- Is_Non_Overridden_Or_Null_Procedure --
11104 -----------------------------------------
11105
11106 function Is_Non_Overridden_Or_Null_Procedure
11107 (Subp_Id : Entity_Id) return Boolean
11108 is
11109 Alias_Id : Entity_Id;
11110
11111 begin
11112 -- The subprogram is inherited (implicitly declared), it does not
11113 -- override and does not cover a primitive of an interface.
11114
11115 if Ekind_In (Subp_Id, E_Function, E_Procedure)
11116 and then Present (Alias (Subp_Id))
11117 and then No (Interface_Alias (Subp_Id))
11118 and then No (Overridden_Operation (Subp_Id))
11119 then
11120 Alias_Id := Alias (Subp_Id);
11121
11122 if Requires_Overriding (Alias_Id) then
11123 return True;
11124
11125 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
11126 and then Null_Present (Parent (Alias_Id))
11127 then
11128 return True;
11129 end if;
11130 end if;
11131
11132 return False;
11133 end Is_Non_Overridden_Or_Null_Procedure;
11134
11135 -- Local variables
11136
11137 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
11138 Prim : Entity_Id;
11139 Prim_Elmt : Elmt_Id;
11140
11141 -- Start of processing for Hide_Non_Overridden_Subprograms
11142
11143 begin
11144 -- Inspect the list of primitives looking for non-overridden
11145 -- subprograms.
11146
11147 if Present (Prim_Ops) then
11148 Prim_Elmt := First_Elmt (Prim_Ops);
11149 while Present (Prim_Elmt) loop
11150 Prim := Node (Prim_Elmt);
11151 Next_Elmt (Prim_Elmt);
11152
11153 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
11154 Hide_Matching_Homographs
11155 (Subp_Id => Prim,
11156 Start_Elmt => Prim_Elmt);
11157 end if;
11158 end loop;
11159 end if;
11160 end Hide_Non_Overridden_Subprograms;
11161
11162 ---------------------
11163 -- Local variables --
11164 ---------------------
11165
11166 E : constant Entity_Id := Entity (N);
11167
11168 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
11169 -- True in non-generic case. Some of the processing here is skipped
11170 -- for the generic case since it is not needed. Basically in the
11171 -- generic case, we only need to do stuff that might generate error
11172 -- messages or warnings.
11173
11174 -- Start of processing for Freeze_Entity_Checks
11175
11176 begin
11177 -- Remember that we are processing a freezing entity. Required to
11178 -- ensure correct decoration of internal entities associated with
11179 -- interfaces (see New_Overloaded_Entity).
11180
11181 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
11182
11183 -- For tagged types covering interfaces add internal entities that link
11184 -- the primitives of the interfaces with the primitives that cover them.
11185 -- Note: These entities were originally generated only when generating
11186 -- code because their main purpose was to provide support to initialize
11187 -- the secondary dispatch tables. They are now generated also when
11188 -- compiling with no code generation to provide ASIS the relationship
11189 -- between interface primitives and tagged type primitives. They are
11190 -- also used to locate primitives covering interfaces when processing
11191 -- generics (see Derive_Subprograms).
11192
11193 -- This is not needed in the generic case
11194
11195 if Ada_Version >= Ada_2005
11196 and then Non_Generic_Case
11197 and then Ekind (E) = E_Record_Type
11198 and then Is_Tagged_Type (E)
11199 and then not Is_Interface (E)
11200 and then Has_Interfaces (E)
11201 then
11202 -- This would be a good common place to call the routine that checks
11203 -- overriding of interface primitives (and thus factorize calls to
11204 -- Check_Abstract_Overriding located at different contexts in the
11205 -- compiler). However, this is not possible because it causes
11206 -- spurious errors in case of late overriding.
11207
11208 Add_Internal_Interface_Entities (E);
11209 end if;
11210
11211 -- After all forms of overriding have been resolved, a tagged type may
11212 -- be left with a set of implicitly declared and possibly erroneous
11213 -- abstract subprograms, null procedures and subprograms that require
11214 -- overriding. If this set contains fully conformant homographs, then
11215 -- one is chosen arbitrarily (already done during resolution), otherwise
11216 -- all remaining non-fully conformant homographs are hidden from
11217 -- visibility (Ada RM 8.3 12.3/2).
11218
11219 if Is_Tagged_Type (E) then
11220 Hide_Non_Overridden_Subprograms (E);
11221 end if;
11222
11223 -- Check CPP types
11224
11225 if Ekind (E) = E_Record_Type
11226 and then Is_CPP_Class (E)
11227 and then Is_Tagged_Type (E)
11228 and then Tagged_Type_Expansion
11229 then
11230 if CPP_Num_Prims (E) = 0 then
11231
11232 -- If the CPP type has user defined components then it must import
11233 -- primitives from C++. This is required because if the C++ class
11234 -- has no primitives then the C++ compiler does not added the _tag
11235 -- component to the type.
11236
11237 if First_Entity (E) /= Last_Entity (E) then
11238 Error_Msg_N
11239 ("'C'P'P type must import at least one primitive from C++??",
11240 E);
11241 end if;
11242 end if;
11243
11244 -- Check that all its primitives are abstract or imported from C++.
11245 -- Check also availability of the C++ constructor.
11246
11247 declare
11248 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
11249 Elmt : Elmt_Id;
11250 Error_Reported : Boolean := False;
11251 Prim : Node_Id;
11252
11253 begin
11254 Elmt := First_Elmt (Primitive_Operations (E));
11255 while Present (Elmt) loop
11256 Prim := Node (Elmt);
11257
11258 if Comes_From_Source (Prim) then
11259 if Is_Abstract_Subprogram (Prim) then
11260 null;
11261
11262 elsif not Is_Imported (Prim)
11263 or else Convention (Prim) /= Convention_CPP
11264 then
11265 Error_Msg_N
11266 ("primitives of 'C'P'P types must be imported from C++ "
11267 & "or abstract??", Prim);
11268
11269 elsif not Has_Constructors
11270 and then not Error_Reported
11271 then
11272 Error_Msg_Name_1 := Chars (E);
11273 Error_Msg_N
11274 ("??'C'P'P constructor required for type %", Prim);
11275 Error_Reported := True;
11276 end if;
11277 end if;
11278
11279 Next_Elmt (Elmt);
11280 end loop;
11281 end;
11282 end if;
11283
11284 -- Check Ada derivation of CPP type
11285
11286 if Expander_Active -- why? losing errors in -gnatc mode???
11287 and then Present (Etype (E)) -- defend against errors
11288 and then Tagged_Type_Expansion
11289 and then Ekind (E) = E_Record_Type
11290 and then Etype (E) /= E
11291 and then Is_CPP_Class (Etype (E))
11292 and then CPP_Num_Prims (Etype (E)) > 0
11293 and then not Is_CPP_Class (E)
11294 and then not Has_CPP_Constructors (Etype (E))
11295 then
11296 -- If the parent has C++ primitives but it has no constructor then
11297 -- check that all the primitives are overridden in this derivation;
11298 -- otherwise the constructor of the parent is needed to build the
11299 -- dispatch table.
11300
11301 declare
11302 Elmt : Elmt_Id;
11303 Prim : Node_Id;
11304
11305 begin
11306 Elmt := First_Elmt (Primitive_Operations (E));
11307 while Present (Elmt) loop
11308 Prim := Node (Elmt);
11309
11310 if not Is_Abstract_Subprogram (Prim)
11311 and then No (Interface_Alias (Prim))
11312 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
11313 then
11314 Error_Msg_Name_1 := Chars (Etype (E));
11315 Error_Msg_N
11316 ("'C'P'P constructor required for parent type %", E);
11317 exit;
11318 end if;
11319
11320 Next_Elmt (Elmt);
11321 end loop;
11322 end;
11323 end if;
11324
11325 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
11326
11327 -- If we have a type with predicates, build predicate function. This
11328 -- is not needed in the generic case, and is not needed within TSS
11329 -- subprograms and other predefined primitives.
11330
11331 if Non_Generic_Case
11332 and then Is_Type (E)
11333 and then Has_Predicates (E)
11334 and then not Within_Internal_Subprogram
11335 then
11336 Build_Predicate_Functions (E, N);
11337 end if;
11338
11339 -- If type has delayed aspects, this is where we do the preanalysis at
11340 -- the freeze point, as part of the consistent visibility check. Note
11341 -- that this must be done after calling Build_Predicate_Functions or
11342 -- Build_Invariant_Procedure since these subprograms fix occurrences of
11343 -- the subtype name in the saved expression so that they will not cause
11344 -- trouble in the preanalysis.
11345
11346 -- This is also not needed in the generic case
11347
11348 if Non_Generic_Case
11349 and then Has_Delayed_Aspects (E)
11350 and then Scope (E) = Current_Scope
11351 then
11352 -- Retrieve the visibility to the discriminants in order to properly
11353 -- analyze the aspects.
11354
11355 Push_Scope_And_Install_Discriminants (E);
11356
11357 declare
11358 Ritem : Node_Id;
11359
11360 begin
11361 -- Look for aspect specification entries for this entity
11362
11363 Ritem := First_Rep_Item (E);
11364 while Present (Ritem) loop
11365 if Nkind (Ritem) = N_Aspect_Specification
11366 and then Entity (Ritem) = E
11367 and then Is_Delayed_Aspect (Ritem)
11368 then
11369 Check_Aspect_At_Freeze_Point (Ritem);
11370 end if;
11371
11372 Next_Rep_Item (Ritem);
11373 end loop;
11374 end;
11375
11376 Uninstall_Discriminants_And_Pop_Scope (E);
11377 end if;
11378
11379 -- For a record type, deal with variant parts. This has to be delayed
11380 -- to this point, because of the issue of statically predicated
11381 -- subtypes, which we have to ensure are frozen before checking
11382 -- choices, since we need to have the static choice list set.
11383
11384 if Is_Record_Type (E) then
11385 Check_Variant_Part : declare
11386 D : constant Node_Id := Declaration_Node (E);
11387 T : Node_Id;
11388 C : Node_Id;
11389 VP : Node_Id;
11390
11391 Others_Present : Boolean;
11392 pragma Warnings (Off, Others_Present);
11393 -- Indicates others present, not used in this case
11394
11395 procedure Non_Static_Choice_Error (Choice : Node_Id);
11396 -- Error routine invoked by the generic instantiation below when
11397 -- the variant part has a non static choice.
11398
11399 procedure Process_Declarations (Variant : Node_Id);
11400 -- Processes declarations associated with a variant. We analyzed
11401 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11402 -- but we still need the recursive call to Check_Choices for any
11403 -- nested variant to get its choices properly processed. This is
11404 -- also where we expand out the choices if expansion is active.
11405
11406 package Variant_Choices_Processing is new
11407 Generic_Check_Choices
11408 (Process_Empty_Choice => No_OP,
11409 Process_Non_Static_Choice => Non_Static_Choice_Error,
11410 Process_Associated_Node => Process_Declarations);
11411 use Variant_Choices_Processing;
11412
11413 -----------------------------
11414 -- Non_Static_Choice_Error --
11415 -----------------------------
11416
11417 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11418 begin
11419 Flag_Non_Static_Expr
11420 ("choice given in variant part is not static!", Choice);
11421 end Non_Static_Choice_Error;
11422
11423 --------------------------
11424 -- Process_Declarations --
11425 --------------------------
11426
11427 procedure Process_Declarations (Variant : Node_Id) is
11428 CL : constant Node_Id := Component_List (Variant);
11429 VP : Node_Id;
11430
11431 begin
11432 -- Check for static predicate present in this variant
11433
11434 if Has_SP_Choice (Variant) then
11435
11436 -- Here we expand. You might expect to find this call in
11437 -- Expand_N_Variant_Part, but that is called when we first
11438 -- see the variant part, and we cannot do this expansion
11439 -- earlier than the freeze point, since for statically
11440 -- predicated subtypes, the predicate is not known till
11441 -- the freeze point.
11442
11443 -- Furthermore, we do this expansion even if the expander
11444 -- is not active, because other semantic processing, e.g.
11445 -- for aggregates, requires the expanded list of choices.
11446
11447 -- If the expander is not active, then we can't just clobber
11448 -- the list since it would invalidate the ASIS -gnatct tree.
11449 -- So we have to rewrite the variant part with a Rewrite
11450 -- call that replaces it with a copy and clobber the copy.
11451
11452 if not Expander_Active then
11453 declare
11454 NewV : constant Node_Id := New_Copy (Variant);
11455 begin
11456 Set_Discrete_Choices
11457 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11458 Rewrite (Variant, NewV);
11459 end;
11460 end if;
11461
11462 Expand_Static_Predicates_In_Choices (Variant);
11463 end if;
11464
11465 -- We don't need to worry about the declarations in the variant
11466 -- (since they were analyzed by Analyze_Choices when we first
11467 -- encountered the variant), but we do need to take care of
11468 -- expansion of any nested variants.
11469
11470 if not Null_Present (CL) then
11471 VP := Variant_Part (CL);
11472
11473 if Present (VP) then
11474 Check_Choices
11475 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11476 end if;
11477 end if;
11478 end Process_Declarations;
11479
11480 -- Start of processing for Check_Variant_Part
11481
11482 begin
11483 -- Find component list
11484
11485 C := Empty;
11486
11487 if Nkind (D) = N_Full_Type_Declaration then
11488 T := Type_Definition (D);
11489
11490 if Nkind (T) = N_Record_Definition then
11491 C := Component_List (T);
11492
11493 elsif Nkind (T) = N_Derived_Type_Definition
11494 and then Present (Record_Extension_Part (T))
11495 then
11496 C := Component_List (Record_Extension_Part (T));
11497 end if;
11498 end if;
11499
11500 -- Case of variant part present
11501
11502 if Present (C) and then Present (Variant_Part (C)) then
11503 VP := Variant_Part (C);
11504
11505 -- Check choices
11506
11507 Check_Choices
11508 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11509
11510 -- If the last variant does not contain the Others choice,
11511 -- replace it with an N_Others_Choice node since Gigi always
11512 -- wants an Others. Note that we do not bother to call Analyze
11513 -- on the modified variant part, since its only effect would be
11514 -- to compute the Others_Discrete_Choices node laboriously, and
11515 -- of course we already know the list of choices corresponding
11516 -- to the others choice (it's the list we're replacing).
11517
11518 -- We only want to do this if the expander is active, since
11519 -- we do not want to clobber the ASIS tree.
11520
11521 if Expander_Active then
11522 declare
11523 Last_Var : constant Node_Id :=
11524 Last_Non_Pragma (Variants (VP));
11525
11526 Others_Node : Node_Id;
11527
11528 begin
11529 if Nkind (First (Discrete_Choices (Last_Var))) /=
11530 N_Others_Choice
11531 then
11532 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11533 Set_Others_Discrete_Choices
11534 (Others_Node, Discrete_Choices (Last_Var));
11535 Set_Discrete_Choices
11536 (Last_Var, New_List (Others_Node));
11537 end if;
11538 end;
11539 end if;
11540 end if;
11541 end Check_Variant_Part;
11542 end if;
11543 end Freeze_Entity_Checks;
11544
11545 -------------------------
11546 -- Get_Alignment_Value --
11547 -------------------------
11548
11549 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11550 Align : constant Uint := Static_Integer (Expr);
11551
11552 begin
11553 if Align = No_Uint then
11554 return No_Uint;
11555
11556 elsif Align <= 0 then
11557
11558 -- This error is suppressed in ASIS mode to allow for different ASIS
11559 -- back ends or ASIS-based tools to query the illegal clause.
11560
11561 if not ASIS_Mode then
11562 Error_Msg_N ("alignment value must be positive", Expr);
11563 end if;
11564
11565 return No_Uint;
11566
11567 else
11568 for J in Int range 0 .. 64 loop
11569 declare
11570 M : constant Uint := Uint_2 ** J;
11571
11572 begin
11573 exit when M = Align;
11574
11575 if M > Align then
11576
11577 -- This error is suppressed in ASIS mode to allow for
11578 -- different ASIS back ends or ASIS-based tools to query the
11579 -- illegal clause.
11580
11581 if not ASIS_Mode then
11582 Error_Msg_N ("alignment value must be power of 2", Expr);
11583 end if;
11584
11585 return No_Uint;
11586 end if;
11587 end;
11588 end loop;
11589
11590 return Align;
11591 end if;
11592 end Get_Alignment_Value;
11593
11594 -----------------------------
11595 -- Get_Interfacing_Aspects --
11596 -----------------------------
11597
11598 procedure Get_Interfacing_Aspects
11599 (Iface_Asp : Node_Id;
11600 Conv_Asp : out Node_Id;
11601 EN_Asp : out Node_Id;
11602 Expo_Asp : out Node_Id;
11603 Imp_Asp : out Node_Id;
11604 LN_Asp : out Node_Id;
11605 Do_Checks : Boolean := False)
11606 is
11607 procedure Save_Or_Duplication_Error
11608 (Asp : Node_Id;
11609 To : in out Node_Id);
11610 -- Save the value of aspect Asp in node To. If To already has a value,
11611 -- then this is considered a duplicate use of aspect. Emit an error if
11612 -- flag Do_Checks is set.
11613
11614 -------------------------------
11615 -- Save_Or_Duplication_Error --
11616 -------------------------------
11617
11618 procedure Save_Or_Duplication_Error
11619 (Asp : Node_Id;
11620 To : in out Node_Id)
11621 is
11622 begin
11623 -- Detect an extra aspect and issue an error
11624
11625 if Present (To) then
11626 if Do_Checks then
11627 Error_Msg_Name_1 := Chars (Identifier (Asp));
11628 Error_Msg_Sloc := Sloc (To);
11629 Error_Msg_N ("aspect % previously given #", Asp);
11630 end if;
11631
11632 -- Otherwise capture the aspect
11633
11634 else
11635 To := Asp;
11636 end if;
11637 end Save_Or_Duplication_Error;
11638
11639 -- Local variables
11640
11641 Asp : Node_Id;
11642 Asp_Id : Aspect_Id;
11643
11644 -- The following variables capture each individual aspect
11645
11646 Conv : Node_Id := Empty;
11647 EN : Node_Id := Empty;
11648 Expo : Node_Id := Empty;
11649 Imp : Node_Id := Empty;
11650 LN : Node_Id := Empty;
11651
11652 -- Start of processing for Get_Interfacing_Aspects
11653
11654 begin
11655 -- The input interfacing aspect should reside in an aspect specification
11656 -- list.
11657
11658 pragma Assert (Is_List_Member (Iface_Asp));
11659
11660 -- Examine the aspect specifications of the related entity. Find and
11661 -- capture all interfacing aspects. Detect duplicates and emit errors
11662 -- if applicable.
11663
11664 Asp := First (List_Containing (Iface_Asp));
11665 while Present (Asp) loop
11666 Asp_Id := Get_Aspect_Id (Asp);
11667
11668 if Asp_Id = Aspect_Convention then
11669 Save_Or_Duplication_Error (Asp, Conv);
11670
11671 elsif Asp_Id = Aspect_External_Name then
11672 Save_Or_Duplication_Error (Asp, EN);
11673
11674 elsif Asp_Id = Aspect_Export then
11675 Save_Or_Duplication_Error (Asp, Expo);
11676
11677 elsif Asp_Id = Aspect_Import then
11678 Save_Or_Duplication_Error (Asp, Imp);
11679
11680 elsif Asp_Id = Aspect_Link_Name then
11681 Save_Or_Duplication_Error (Asp, LN);
11682 end if;
11683
11684 Next (Asp);
11685 end loop;
11686
11687 Conv_Asp := Conv;
11688 EN_Asp := EN;
11689 Expo_Asp := Expo;
11690 Imp_Asp := Imp;
11691 LN_Asp := LN;
11692 end Get_Interfacing_Aspects;
11693
11694 -------------------------------------
11695 -- Inherit_Aspects_At_Freeze_Point --
11696 -------------------------------------
11697
11698 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11699 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11700 (Rep_Item : Node_Id) return Boolean;
11701 -- This routine checks if Rep_Item is either a pragma or an aspect
11702 -- specification node whose correponding pragma (if any) is present in
11703 -- the Rep Item chain of the entity it has been specified to.
11704
11705 --------------------------------------------------
11706 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11707 --------------------------------------------------
11708
11709 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11710 (Rep_Item : Node_Id) return Boolean
11711 is
11712 begin
11713 return
11714 Nkind (Rep_Item) = N_Pragma
11715 or else Present_In_Rep_Item
11716 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11717 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11718
11719 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11720
11721 begin
11722 -- A representation item is either subtype-specific (Size and Alignment
11723 -- clauses) or type-related (all others). Subtype-specific aspects may
11724 -- differ for different subtypes of the same type (RM 13.1.8).
11725
11726 -- A derived type inherits each type-related representation aspect of
11727 -- its parent type that was directly specified before the declaration of
11728 -- the derived type (RM 13.1.15).
11729
11730 -- A derived subtype inherits each subtype-specific representation
11731 -- aspect of its parent subtype that was directly specified before the
11732 -- declaration of the derived type (RM 13.1.15).
11733
11734 -- The general processing involves inheriting a representation aspect
11735 -- from a parent type whenever the first rep item (aspect specification,
11736 -- attribute definition clause, pragma) corresponding to the given
11737 -- representation aspect in the rep item chain of Typ, if any, isn't
11738 -- directly specified to Typ but to one of its parents.
11739
11740 -- ??? Note that, for now, just a limited number of representation
11741 -- aspects have been inherited here so far. Many of them are
11742 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11743 -- a non- exhaustive list of aspects that likely also need to
11744 -- be moved to this routine: Alignment, Component_Alignment,
11745 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11746 -- Preelaborable_Initialization, RM_Size and Small.
11747
11748 -- In addition, Convention must be propagated from base type to subtype,
11749 -- because the subtype may have been declared on an incomplete view.
11750
11751 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11752 return;
11753 end if;
11754
11755 -- Ada_05/Ada_2005
11756
11757 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11758 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11759 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11760 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11761 then
11762 Set_Is_Ada_2005_Only (Typ);
11763 end if;
11764
11765 -- Ada_12/Ada_2012
11766
11767 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11768 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11769 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11770 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11771 then
11772 Set_Is_Ada_2012_Only (Typ);
11773 end if;
11774
11775 -- Atomic/Shared
11776
11777 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11778 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11779 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11780 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11781 then
11782 Set_Is_Atomic (Typ);
11783 Set_Is_Volatile (Typ);
11784 Set_Treat_As_Volatile (Typ);
11785 end if;
11786
11787 -- Convention
11788
11789 if Is_Record_Type (Typ)
11790 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11791 then
11792 Set_Convention (Typ, Convention (Base_Type (Typ)));
11793 end if;
11794
11795 -- Default_Component_Value
11796
11797 -- Verify that there is no rep_item declared for the type, and there
11798 -- is one coming from an ancestor.
11799
11800 if Is_Array_Type (Typ)
11801 and then Is_Base_Type (Typ)
11802 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11803 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11804 then
11805 Set_Default_Aspect_Component_Value (Typ,
11806 Default_Aspect_Component_Value
11807 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11808 end if;
11809
11810 -- Default_Value
11811
11812 if Is_Scalar_Type (Typ)
11813 and then Is_Base_Type (Typ)
11814 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11815 and then Has_Rep_Item (Typ, Name_Default_Value)
11816 then
11817 Set_Has_Default_Aspect (Typ);
11818 Set_Default_Aspect_Value (Typ,
11819 Default_Aspect_Value
11820 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11821 end if;
11822
11823 -- Discard_Names
11824
11825 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11826 and then Has_Rep_Item (Typ, Name_Discard_Names)
11827 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11828 (Get_Rep_Item (Typ, Name_Discard_Names))
11829 then
11830 Set_Discard_Names (Typ);
11831 end if;
11832
11833 -- Invariants
11834
11835 if not Has_Rep_Item (Typ, Name_Invariant, False)
11836 and then Has_Rep_Item (Typ, Name_Invariant)
11837 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11838 (Get_Rep_Item (Typ, Name_Invariant))
11839 then
11840 Set_Has_Invariants (Typ);
11841
11842 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
11843 Set_Has_Inheritable_Invariants (Typ);
11844 end if;
11845
11846 -- If we have a subtype with invariants, whose base type does not have
11847 -- invariants, copy these invariants to the base type. This happens for
11848 -- the case of implicit base types created for scalar and array types.
11849
11850 elsif Has_Invariants (Typ)
11851 and then not Has_Invariants (Base_Type (Typ))
11852 then
11853 Set_Has_Invariants (Base_Type (Typ));
11854 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
11855 end if;
11856
11857 -- Volatile
11858
11859 if not Has_Rep_Item (Typ, Name_Volatile, False)
11860 and then Has_Rep_Item (Typ, Name_Volatile)
11861 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11862 (Get_Rep_Item (Typ, Name_Volatile))
11863 then
11864 Set_Is_Volatile (Typ);
11865 Set_Treat_As_Volatile (Typ);
11866 end if;
11867
11868 -- Volatile_Full_Access
11869
11870 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11871 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11872 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11873 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11874 then
11875 Set_Is_Volatile_Full_Access (Typ);
11876 Set_Is_Volatile (Typ);
11877 Set_Treat_As_Volatile (Typ);
11878 end if;
11879
11880 -- Inheritance for derived types only
11881
11882 if Is_Derived_Type (Typ) then
11883 declare
11884 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11885 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11886
11887 begin
11888 -- Atomic_Components
11889
11890 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11891 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11892 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11893 (Get_Rep_Item (Typ, Name_Atomic_Components))
11894 then
11895 Set_Has_Atomic_Components (Imp_Bas_Typ);
11896 end if;
11897
11898 -- Volatile_Components
11899
11900 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11901 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11902 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11903 (Get_Rep_Item (Typ, Name_Volatile_Components))
11904 then
11905 Set_Has_Volatile_Components (Imp_Bas_Typ);
11906 end if;
11907
11908 -- Finalize_Storage_Only
11909
11910 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11911 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11912 then
11913 Set_Finalize_Storage_Only (Bas_Typ);
11914 end if;
11915
11916 -- Universal_Aliasing
11917
11918 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11919 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11920 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11921 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11922 then
11923 Set_Universal_Aliasing (Imp_Bas_Typ);
11924 end if;
11925
11926 -- Bit_Order
11927
11928 if Is_Record_Type (Typ) then
11929 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11930 and then Has_Rep_Item (Typ, Name_Bit_Order)
11931 then
11932 Set_Reverse_Bit_Order (Bas_Typ,
11933 Reverse_Bit_Order (Entity (Name
11934 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11935 end if;
11936 end if;
11937
11938 -- Scalar_Storage_Order
11939
11940 -- Note: the aspect is specified on a first subtype, but recorded
11941 -- in a flag of the base type!
11942
11943 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11944 and then Typ = Bas_Typ
11945 then
11946 -- For a type extension, always inherit from parent; otherwise
11947 -- inherit if no default applies. Note: we do not check for
11948 -- an explicit rep item on the parent type when inheriting,
11949 -- because the parent SSO may itself have been set by default.
11950
11951 if not Has_Rep_Item (First_Subtype (Typ),
11952 Name_Scalar_Storage_Order, False)
11953 and then (Is_Tagged_Type (Bas_Typ)
11954 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11955 or else
11956 SSO_Set_High_By_Default (Bas_Typ)))
11957 then
11958 Set_Reverse_Storage_Order (Bas_Typ,
11959 Reverse_Storage_Order
11960 (Implementation_Base_Type (Etype (Bas_Typ))));
11961
11962 -- Clear default SSO indications, since the inherited aspect
11963 -- which was set explicitly overrides the default.
11964
11965 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11966 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11967 end if;
11968 end if;
11969 end;
11970 end if;
11971 end Inherit_Aspects_At_Freeze_Point;
11972
11973 ----------------
11974 -- Initialize --
11975 ----------------
11976
11977 procedure Initialize is
11978 begin
11979 Address_Clause_Checks.Init;
11980 Unchecked_Conversions.Init;
11981
11982 if AAMP_On_Target then
11983 Independence_Checks.Init;
11984 end if;
11985 end Initialize;
11986
11987 ---------------------------
11988 -- Install_Discriminants --
11989 ---------------------------
11990
11991 procedure Install_Discriminants (E : Entity_Id) is
11992 Disc : Entity_Id;
11993 Prev : Entity_Id;
11994 begin
11995 Disc := First_Discriminant (E);
11996 while Present (Disc) loop
11997 Prev := Current_Entity (Disc);
11998 Set_Current_Entity (Disc);
11999 Set_Is_Immediately_Visible (Disc);
12000 Set_Homonym (Disc, Prev);
12001 Next_Discriminant (Disc);
12002 end loop;
12003 end Install_Discriminants;
12004
12005 -------------------------
12006 -- Is_Operational_Item --
12007 -------------------------
12008
12009 function Is_Operational_Item (N : Node_Id) return Boolean is
12010 begin
12011 if Nkind (N) /= N_Attribute_Definition_Clause then
12012 return False;
12013
12014 else
12015 declare
12016 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
12017 begin
12018
12019 -- List of operational items is given in AARM 13.1(8.mm/1).
12020 -- It is clearly incomplete, as it does not include iterator
12021 -- aspects, among others.
12022
12023 return Id = Attribute_Constant_Indexing
12024 or else Id = Attribute_Default_Iterator
12025 or else Id = Attribute_Implicit_Dereference
12026 or else Id = Attribute_Input
12027 or else Id = Attribute_Iterator_Element
12028 or else Id = Attribute_Iterable
12029 or else Id = Attribute_Output
12030 or else Id = Attribute_Read
12031 or else Id = Attribute_Variable_Indexing
12032 or else Id = Attribute_Write
12033 or else Id = Attribute_External_Tag;
12034 end;
12035 end if;
12036 end Is_Operational_Item;
12037
12038 -------------------------
12039 -- Is_Predicate_Static --
12040 -------------------------
12041
12042 -- Note: the basic legality of the expression has already been checked, so
12043 -- we don't need to worry about cases or ranges on strings for example.
12044
12045 function Is_Predicate_Static
12046 (Expr : Node_Id;
12047 Nam : Name_Id) return Boolean
12048 is
12049 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
12050 -- Given a list of case expression alternatives, returns True if all
12051 -- the alternatives are static (have all static choices, and a static
12052 -- expression).
12053
12054 function All_Static_Choices (L : List_Id) return Boolean;
12055 -- Returns true if all elements of the list are OK static choices
12056 -- as defined below for Is_Static_Choice. Used for case expression
12057 -- alternatives and for the right operand of a membership test. An
12058 -- others_choice is static if the corresponding expression is static.
12059 -- The staticness of the bounds is checked separately.
12060
12061 function Is_Static_Choice (N : Node_Id) return Boolean;
12062 -- Returns True if N represents a static choice (static subtype, or
12063 -- static subtype indication, or static expression, or static range).
12064 --
12065 -- Note that this is a bit more inclusive than we actually need
12066 -- (in particular membership tests do not allow the use of subtype
12067 -- indications). But that doesn't matter, we have already checked
12068 -- that the construct is legal to get this far.
12069
12070 function Is_Type_Ref (N : Node_Id) return Boolean;
12071 pragma Inline (Is_Type_Ref);
12072 -- Returns True if N is a reference to the type for the predicate in the
12073 -- expression (i.e. if it is an identifier whose Chars field matches the
12074 -- Nam given in the call). N must not be parenthesized, if the type name
12075 -- appears in parens, this routine will return False.
12076
12077 ----------------------------------
12078 -- All_Static_Case_Alternatives --
12079 ----------------------------------
12080
12081 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
12082 N : Node_Id;
12083
12084 begin
12085 N := First (L);
12086 while Present (N) loop
12087 if not (All_Static_Choices (Discrete_Choices (N))
12088 and then Is_OK_Static_Expression (Expression (N)))
12089 then
12090 return False;
12091 end if;
12092
12093 Next (N);
12094 end loop;
12095
12096 return True;
12097 end All_Static_Case_Alternatives;
12098
12099 ------------------------
12100 -- All_Static_Choices --
12101 ------------------------
12102
12103 function All_Static_Choices (L : List_Id) return Boolean is
12104 N : Node_Id;
12105
12106 begin
12107 N := First (L);
12108 while Present (N) loop
12109 if not Is_Static_Choice (N) then
12110 return False;
12111 end if;
12112
12113 Next (N);
12114 end loop;
12115
12116 return True;
12117 end All_Static_Choices;
12118
12119 ----------------------
12120 -- Is_Static_Choice --
12121 ----------------------
12122
12123 function Is_Static_Choice (N : Node_Id) return Boolean is
12124 begin
12125 return Nkind (N) = N_Others_Choice
12126 or else Is_OK_Static_Expression (N)
12127 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
12128 and then Is_OK_Static_Subtype (Entity (N)))
12129 or else (Nkind (N) = N_Subtype_Indication
12130 and then Is_OK_Static_Subtype (Entity (N)))
12131 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
12132 end Is_Static_Choice;
12133
12134 -----------------
12135 -- Is_Type_Ref --
12136 -----------------
12137
12138 function Is_Type_Ref (N : Node_Id) return Boolean is
12139 begin
12140 return Nkind (N) = N_Identifier
12141 and then Chars (N) = Nam
12142 and then Paren_Count (N) = 0;
12143 end Is_Type_Ref;
12144
12145 -- Start of processing for Is_Predicate_Static
12146
12147 begin
12148 -- Predicate_Static means one of the following holds. Numbers are the
12149 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
12150
12151 -- 16: A static expression
12152
12153 if Is_OK_Static_Expression (Expr) then
12154 return True;
12155
12156 -- 17: A membership test whose simple_expression is the current
12157 -- instance, and whose membership_choice_list meets the requirements
12158 -- for a static membership test.
12159
12160 elsif Nkind (Expr) in N_Membership_Test
12161 and then ((Present (Right_Opnd (Expr))
12162 and then Is_Static_Choice (Right_Opnd (Expr)))
12163 or else
12164 (Present (Alternatives (Expr))
12165 and then All_Static_Choices (Alternatives (Expr))))
12166 then
12167 return True;
12168
12169 -- 18. A case_expression whose selecting_expression is the current
12170 -- instance, and whose dependent expressions are static expressions.
12171
12172 elsif Nkind (Expr) = N_Case_Expression
12173 and then Is_Type_Ref (Expression (Expr))
12174 and then All_Static_Case_Alternatives (Alternatives (Expr))
12175 then
12176 return True;
12177
12178 -- 19. A call to a predefined equality or ordering operator, where one
12179 -- operand is the current instance, and the other is a static
12180 -- expression.
12181
12182 -- Note: the RM is clearly wrong here in not excluding string types.
12183 -- Without this exclusion, we would allow expressions like X > "ABC"
12184 -- to be considered as predicate-static, which is clearly not intended,
12185 -- since the idea is for predicate-static to be a subset of normal
12186 -- static expressions (and "DEF" > "ABC" is not a static expression).
12187
12188 -- However, we do allow internally generated (not from source) equality
12189 -- and inequality operations to be valid on strings (this helps deal
12190 -- with cases where we transform A in "ABC" to A = "ABC).
12191
12192 elsif Nkind (Expr) in N_Op_Compare
12193 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
12194 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
12195 and then not Comes_From_Source (Expr)))
12196 and then ((Is_Type_Ref (Left_Opnd (Expr))
12197 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
12198 or else
12199 (Is_Type_Ref (Right_Opnd (Expr))
12200 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
12201 then
12202 return True;
12203
12204 -- 20. A call to a predefined boolean logical operator, where each
12205 -- operand is predicate-static.
12206
12207 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
12208 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
12209 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
12210 or else
12211 (Nkind (Expr) = N_Op_Not
12212 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
12213 then
12214 return True;
12215
12216 -- 21. A short-circuit control form where both operands are
12217 -- predicate-static.
12218
12219 elsif Nkind (Expr) in N_Short_Circuit
12220 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
12221 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
12222 then
12223 return True;
12224
12225 -- 22. A parenthesized predicate-static expression. This does not
12226 -- require any special test, since we just ignore paren levels in
12227 -- all the cases above.
12228
12229 -- One more test that is an implementation artifact caused by the fact
12230 -- that we are analyzing not the original expression, but the generated
12231 -- expression in the body of the predicate function. This can include
12232 -- references to inherited predicates, so that the expression we are
12233 -- processing looks like:
12234
12235 -- xxPredicate (typ (Inns)) and then expression
12236
12237 -- Where the call is to a Predicate function for an inherited predicate.
12238 -- We simply ignore such a call, which could be to either a dynamic or
12239 -- a static predicate. Note that if the parent predicate is dynamic then
12240 -- eventually this type will be marked as dynamic, but you are allowed
12241 -- to specify a static predicate for a subtype which is inheriting a
12242 -- dynamic predicate, so the static predicate validation here ignores
12243 -- the inherited predicate even if it is dynamic.
12244
12245 elsif Nkind (Expr) = N_Function_Call
12246 and then Is_Predicate_Function (Entity (Name (Expr)))
12247 then
12248 return True;
12249
12250 -- That's an exhaustive list of tests, all other cases are not
12251 -- predicate-static, so we return False.
12252
12253 else
12254 return False;
12255 end if;
12256 end Is_Predicate_Static;
12257
12258 ---------------------
12259 -- Kill_Rep_Clause --
12260 ---------------------
12261
12262 procedure Kill_Rep_Clause (N : Node_Id) is
12263 begin
12264 pragma Assert (Ignore_Rep_Clauses);
12265
12266 -- Note: we use Replace rather than Rewrite, because we don't want
12267 -- ASIS to be able to use Original_Node to dig out the (undecorated)
12268 -- rep clause that is being replaced.
12269
12270 Replace (N, Make_Null_Statement (Sloc (N)));
12271
12272 -- The null statement must be marked as not coming from source. This is
12273 -- so that ASIS ignores it, and also the back end does not expect bogus
12274 -- "from source" null statements in weird places (e.g. in declarative
12275 -- regions where such null statements are not allowed).
12276
12277 Set_Comes_From_Source (N, False);
12278 end Kill_Rep_Clause;
12279
12280 ------------------
12281 -- Minimum_Size --
12282 ------------------
12283
12284 function Minimum_Size
12285 (T : Entity_Id;
12286 Biased : Boolean := False) return Nat
12287 is
12288 Lo : Uint := No_Uint;
12289 Hi : Uint := No_Uint;
12290 LoR : Ureal := No_Ureal;
12291 HiR : Ureal := No_Ureal;
12292 LoSet : Boolean := False;
12293 HiSet : Boolean := False;
12294 B : Uint;
12295 S : Nat;
12296 Ancest : Entity_Id;
12297 R_Typ : constant Entity_Id := Root_Type (T);
12298
12299 begin
12300 -- If bad type, return 0
12301
12302 if T = Any_Type then
12303 return 0;
12304
12305 -- For generic types, just return zero. There cannot be any legitimate
12306 -- need to know such a size, but this routine may be called with a
12307 -- generic type as part of normal processing.
12308
12309 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
12310 return 0;
12311
12312 -- Access types (cannot have size smaller than System.Address)
12313
12314 elsif Is_Access_Type (T) then
12315 return System_Address_Size;
12316
12317 -- Floating-point types
12318
12319 elsif Is_Floating_Point_Type (T) then
12320 return UI_To_Int (Esize (R_Typ));
12321
12322 -- Discrete types
12323
12324 elsif Is_Discrete_Type (T) then
12325
12326 -- The following loop is looking for the nearest compile time known
12327 -- bounds following the ancestor subtype chain. The idea is to find
12328 -- the most restrictive known bounds information.
12329
12330 Ancest := T;
12331 loop
12332 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12333 return 0;
12334 end if;
12335
12336 if not LoSet then
12337 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
12338 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
12339 LoSet := True;
12340 exit when HiSet;
12341 end if;
12342 end if;
12343
12344 if not HiSet then
12345 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
12346 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
12347 HiSet := True;
12348 exit when LoSet;
12349 end if;
12350 end if;
12351
12352 Ancest := Ancestor_Subtype (Ancest);
12353
12354 if No (Ancest) then
12355 Ancest := Base_Type (T);
12356
12357 if Is_Generic_Type (Ancest) then
12358 return 0;
12359 end if;
12360 end if;
12361 end loop;
12362
12363 -- Fixed-point types. We can't simply use Expr_Value to get the
12364 -- Corresponding_Integer_Value values of the bounds, since these do not
12365 -- get set till the type is frozen, and this routine can be called
12366 -- before the type is frozen. Similarly the test for bounds being static
12367 -- needs to include the case where we have unanalyzed real literals for
12368 -- the same reason.
12369
12370 elsif Is_Fixed_Point_Type (T) then
12371
12372 -- The following loop is looking for the nearest compile time known
12373 -- bounds following the ancestor subtype chain. The idea is to find
12374 -- the most restrictive known bounds information.
12375
12376 Ancest := T;
12377 loop
12378 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12379 return 0;
12380 end if;
12381
12382 -- Note: In the following two tests for LoSet and HiSet, it may
12383 -- seem redundant to test for N_Real_Literal here since normally
12384 -- one would assume that the test for the value being known at
12385 -- compile time includes this case. However, there is a glitch.
12386 -- If the real literal comes from folding a non-static expression,
12387 -- then we don't consider any non- static expression to be known
12388 -- at compile time if we are in configurable run time mode (needed
12389 -- in some cases to give a clearer definition of what is and what
12390 -- is not accepted). So the test is indeed needed. Without it, we
12391 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
12392
12393 if not LoSet then
12394 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
12395 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
12396 then
12397 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
12398 LoSet := True;
12399 exit when HiSet;
12400 end if;
12401 end if;
12402
12403 if not HiSet then
12404 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
12405 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
12406 then
12407 HiR := Expr_Value_R (Type_High_Bound (Ancest));
12408 HiSet := True;
12409 exit when LoSet;
12410 end if;
12411 end if;
12412
12413 Ancest := Ancestor_Subtype (Ancest);
12414
12415 if No (Ancest) then
12416 Ancest := Base_Type (T);
12417
12418 if Is_Generic_Type (Ancest) then
12419 return 0;
12420 end if;
12421 end if;
12422 end loop;
12423
12424 Lo := UR_To_Uint (LoR / Small_Value (T));
12425 Hi := UR_To_Uint (HiR / Small_Value (T));
12426
12427 -- No other types allowed
12428
12429 else
12430 raise Program_Error;
12431 end if;
12432
12433 -- Fall through with Hi and Lo set. Deal with biased case
12434
12435 if (Biased
12436 and then not Is_Fixed_Point_Type (T)
12437 and then not (Is_Enumeration_Type (T)
12438 and then Has_Non_Standard_Rep (T)))
12439 or else Has_Biased_Representation (T)
12440 then
12441 Hi := Hi - Lo;
12442 Lo := Uint_0;
12443 end if;
12444
12445 -- Null range case, size is always zero. We only do this in the discrete
12446 -- type case, since that's the odd case that came up. Probably we should
12447 -- also do this in the fixed-point case, but doing so causes peculiar
12448 -- gigi failures, and it is not worth worrying about this incredibly
12449 -- marginal case (explicit null-range fixed-point type declarations)???
12450
12451 if Lo > Hi and then Is_Discrete_Type (T) then
12452 S := 0;
12453
12454 -- Signed case. Note that we consider types like range 1 .. -1 to be
12455 -- signed for the purpose of computing the size, since the bounds have
12456 -- to be accommodated in the base type.
12457
12458 elsif Lo < 0 or else Hi < 0 then
12459 S := 1;
12460 B := Uint_1;
12461
12462 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12463 -- Note that we accommodate the case where the bounds cross. This
12464 -- can happen either because of the way the bounds are declared
12465 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12466
12467 while Lo < -B
12468 or else Hi < -B
12469 or else Lo >= B
12470 or else Hi >= B
12471 loop
12472 B := Uint_2 ** S;
12473 S := S + 1;
12474 end loop;
12475
12476 -- Unsigned case
12477
12478 else
12479 -- If both bounds are positive, make sure that both are represen-
12480 -- table in the case where the bounds are crossed. This can happen
12481 -- either because of the way the bounds are declared, or because of
12482 -- the algorithm in Freeze_Fixed_Point_Type.
12483
12484 if Lo > Hi then
12485 Hi := Lo;
12486 end if;
12487
12488 -- S = size, (can accommodate 0 .. (2**size - 1))
12489
12490 S := 0;
12491 while Hi >= Uint_2 ** S loop
12492 S := S + 1;
12493 end loop;
12494 end if;
12495
12496 return S;
12497 end Minimum_Size;
12498
12499 ---------------------------
12500 -- New_Stream_Subprogram --
12501 ---------------------------
12502
12503 procedure New_Stream_Subprogram
12504 (N : Node_Id;
12505 Ent : Entity_Id;
12506 Subp : Entity_Id;
12507 Nam : TSS_Name_Type)
12508 is
12509 Loc : constant Source_Ptr := Sloc (N);
12510 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
12511 Subp_Id : Entity_Id;
12512 Subp_Decl : Node_Id;
12513 F : Entity_Id;
12514 Etyp : Entity_Id;
12515
12516 Defer_Declaration : constant Boolean :=
12517 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12518 -- For a tagged type, there is a declaration for each stream attribute
12519 -- at the freeze point, and we must generate only a completion of this
12520 -- declaration. We do the same for private types, because the full view
12521 -- might be tagged. Otherwise we generate a declaration at the point of
12522 -- the attribute definition clause.
12523
12524 function Build_Spec return Node_Id;
12525 -- Used for declaration and renaming declaration, so that this is
12526 -- treated as a renaming_as_body.
12527
12528 ----------------
12529 -- Build_Spec --
12530 ----------------
12531
12532 function Build_Spec return Node_Id is
12533 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12534 Formals : List_Id;
12535 Spec : Node_Id;
12536 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
12537
12538 begin
12539 Subp_Id := Make_Defining_Identifier (Loc, Sname);
12540
12541 -- S : access Root_Stream_Type'Class
12542
12543 Formals := New_List (
12544 Make_Parameter_Specification (Loc,
12545 Defining_Identifier =>
12546 Make_Defining_Identifier (Loc, Name_S),
12547 Parameter_Type =>
12548 Make_Access_Definition (Loc,
12549 Subtype_Mark =>
12550 New_Occurrence_Of (
12551 Designated_Type (Etype (F)), Loc))));
12552
12553 if Nam = TSS_Stream_Input then
12554 Spec :=
12555 Make_Function_Specification (Loc,
12556 Defining_Unit_Name => Subp_Id,
12557 Parameter_Specifications => Formals,
12558 Result_Definition => T_Ref);
12559 else
12560 -- V : [out] T
12561
12562 Append_To (Formals,
12563 Make_Parameter_Specification (Loc,
12564 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12565 Out_Present => Out_P,
12566 Parameter_Type => T_Ref));
12567
12568 Spec :=
12569 Make_Procedure_Specification (Loc,
12570 Defining_Unit_Name => Subp_Id,
12571 Parameter_Specifications => Formals);
12572 end if;
12573
12574 return Spec;
12575 end Build_Spec;
12576
12577 -- Start of processing for New_Stream_Subprogram
12578
12579 begin
12580 F := First_Formal (Subp);
12581
12582 if Ekind (Subp) = E_Procedure then
12583 Etyp := Etype (Next_Formal (F));
12584 else
12585 Etyp := Etype (Subp);
12586 end if;
12587
12588 -- Prepare subprogram declaration and insert it as an action on the
12589 -- clause node. The visibility for this entity is used to test for
12590 -- visibility of the attribute definition clause (in the sense of
12591 -- 8.3(23) as amended by AI-195).
12592
12593 if not Defer_Declaration then
12594 Subp_Decl :=
12595 Make_Subprogram_Declaration (Loc,
12596 Specification => Build_Spec);
12597
12598 -- For a tagged type, there is always a visible declaration for each
12599 -- stream TSS (it is a predefined primitive operation), and the
12600 -- completion of this declaration occurs at the freeze point, which is
12601 -- not always visible at places where the attribute definition clause is
12602 -- visible. So, we create a dummy entity here for the purpose of
12603 -- tracking the visibility of the attribute definition clause itself.
12604
12605 else
12606 Subp_Id :=
12607 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
12608 Subp_Decl :=
12609 Make_Object_Declaration (Loc,
12610 Defining_Identifier => Subp_Id,
12611 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
12612 end if;
12613
12614 Insert_Action (N, Subp_Decl);
12615 Set_Entity (N, Subp_Id);
12616
12617 Subp_Decl :=
12618 Make_Subprogram_Renaming_Declaration (Loc,
12619 Specification => Build_Spec,
12620 Name => New_Occurrence_Of (Subp, Loc));
12621
12622 if Defer_Declaration then
12623 Set_TSS (Base_Type (Ent), Subp_Id);
12624 else
12625 Insert_Action (N, Subp_Decl);
12626 Copy_TSS (Subp_Id, Base_Type (Ent));
12627 end if;
12628 end New_Stream_Subprogram;
12629
12630 ------------------------------------------
12631 -- Push_Scope_And_Install_Discriminants --
12632 ------------------------------------------
12633
12634 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12635 begin
12636 if Has_Discriminants (E) then
12637 Push_Scope (E);
12638
12639 -- Make discriminants visible for type declarations and protected
12640 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12641
12642 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12643 Install_Discriminants (E);
12644 end if;
12645 end if;
12646 end Push_Scope_And_Install_Discriminants;
12647
12648 ------------------------
12649 -- Rep_Item_Too_Early --
12650 ------------------------
12651
12652 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12653 begin
12654 -- Cannot apply non-operational rep items to generic types
12655
12656 if Is_Operational_Item (N) then
12657 return False;
12658
12659 elsif Is_Type (T)
12660 and then Is_Generic_Type (Root_Type (T))
12661 and then (Nkind (N) /= N_Pragma
12662 or else Get_Pragma_Id (N) /= Pragma_Convention)
12663 then
12664 Error_Msg_N ("representation item not allowed for generic type", N);
12665 return True;
12666 end if;
12667
12668 -- Otherwise check for incomplete type
12669
12670 if Is_Incomplete_Or_Private_Type (T)
12671 and then No (Underlying_Type (T))
12672 and then
12673 (Nkind (N) /= N_Pragma
12674 or else Get_Pragma_Id (N) /= Pragma_Import)
12675 then
12676 Error_Msg_N
12677 ("representation item must be after full type declaration", N);
12678 return True;
12679
12680 -- If the type has incomplete components, a representation clause is
12681 -- illegal but stream attributes and Convention pragmas are correct.
12682
12683 elsif Has_Private_Component (T) then
12684 if Nkind (N) = N_Pragma then
12685 return False;
12686
12687 else
12688 Error_Msg_N
12689 ("representation item must appear after type is fully defined",
12690 N);
12691 return True;
12692 end if;
12693 else
12694 return False;
12695 end if;
12696 end Rep_Item_Too_Early;
12697
12698 -----------------------
12699 -- Rep_Item_Too_Late --
12700 -----------------------
12701
12702 function Rep_Item_Too_Late
12703 (T : Entity_Id;
12704 N : Node_Id;
12705 FOnly : Boolean := False) return Boolean
12706 is
12707 S : Entity_Id;
12708 Parent_Type : Entity_Id;
12709
12710 procedure No_Type_Rep_Item;
12711 -- Output message indicating that no type-related aspects can be
12712 -- specified due to some property of the parent type.
12713
12714 procedure Too_Late;
12715 -- Output message for an aspect being specified too late
12716
12717 -- Note that neither of the above errors is considered a serious one,
12718 -- since the effect is simply that we ignore the representation clause
12719 -- in these cases.
12720 -- Is this really true? In any case if we make this change we must
12721 -- document the requirement in the spec of Rep_Item_Too_Late that
12722 -- if True is returned, then the rep item must be completely ignored???
12723
12724 ----------------------
12725 -- No_Type_Rep_Item --
12726 ----------------------
12727
12728 procedure No_Type_Rep_Item is
12729 begin
12730 Error_Msg_N ("|type-related representation item not permitted!", N);
12731 end No_Type_Rep_Item;
12732
12733 --------------
12734 -- Too_Late --
12735 --------------
12736
12737 procedure Too_Late is
12738 begin
12739 -- Other compilers seem more relaxed about rep items appearing too
12740 -- late. Since analysis tools typically don't care about rep items
12741 -- anyway, no reason to be too strict about this.
12742
12743 if not Relaxed_RM_Semantics then
12744 Error_Msg_N ("|representation item appears too late!", N);
12745 end if;
12746 end Too_Late;
12747
12748 -- Start of processing for Rep_Item_Too_Late
12749
12750 begin
12751 -- First make sure entity is not frozen (RM 13.1(9))
12752
12753 if Is_Frozen (T)
12754
12755 -- Exclude imported types, which may be frozen if they appear in a
12756 -- representation clause for a local type.
12757
12758 and then not From_Limited_With (T)
12759
12760 -- Exclude generated entities (not coming from source). The common
12761 -- case is when we generate a renaming which prematurely freezes the
12762 -- renamed internal entity, but we still want to be able to set copies
12763 -- of attribute values such as Size/Alignment.
12764
12765 and then Comes_From_Source (T)
12766 then
12767 -- A self-referential aspect is illegal if it forces freezing the
12768 -- entity before the corresponding pragma has been analyzed.
12769
12770 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12771 and then From_Aspect_Specification (N)
12772 then
12773 Error_Msg_NE
12774 ("aspect specification causes premature freezing of&", T, N);
12775 Set_Has_Delayed_Freeze (T, False);
12776 return True;
12777 end if;
12778
12779 Too_Late;
12780 S := First_Subtype (T);
12781
12782 if Present (Freeze_Node (S)) then
12783 if not Relaxed_RM_Semantics then
12784 Error_Msg_NE
12785 ("??no more representation items for }", Freeze_Node (S), S);
12786 end if;
12787 end if;
12788
12789 return True;
12790
12791 -- Check for case of untagged derived type whose parent either has
12792 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12793 -- this case we do not output a Too_Late message, since there is no
12794 -- earlier point where the rep item could be placed to make it legal.
12795
12796 elsif Is_Type (T)
12797 and then not FOnly
12798 and then Is_Derived_Type (T)
12799 and then not Is_Tagged_Type (T)
12800 then
12801 Parent_Type := Etype (Base_Type (T));
12802
12803 if Has_Primitive_Operations (Parent_Type) then
12804 No_Type_Rep_Item;
12805
12806 if not Relaxed_RM_Semantics then
12807 Error_Msg_NE
12808 ("\parent type & has primitive operations!", N, Parent_Type);
12809 end if;
12810
12811 return True;
12812
12813 elsif Is_By_Reference_Type (Parent_Type) then
12814 No_Type_Rep_Item;
12815
12816 if not Relaxed_RM_Semantics then
12817 Error_Msg_NE
12818 ("\parent type & is a by reference type!", N, Parent_Type);
12819 end if;
12820
12821 return True;
12822 end if;
12823 end if;
12824
12825 -- No error, but one more warning to consider. The RM (surprisingly)
12826 -- allows this pattern:
12827
12828 -- type S is ...
12829 -- primitive operations for S
12830 -- type R is new S;
12831 -- rep clause for S
12832
12833 -- Meaning that calls on the primitive operations of S for values of
12834 -- type R may require possibly expensive implicit conversion operations.
12835 -- This is not an error, but is worth a warning.
12836
12837 if not Relaxed_RM_Semantics and then Is_Type (T) then
12838 declare
12839 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12840
12841 begin
12842 if Present (DTL)
12843 and then Has_Primitive_Operations (Base_Type (T))
12844
12845 -- For now, do not generate this warning for the case of aspect
12846 -- specification using Ada 2012 syntax, since we get wrong
12847 -- messages we do not understand. The whole business of derived
12848 -- types and rep items seems a bit confused when aspects are
12849 -- used, since the aspects are not evaluated till freeze time.
12850
12851 and then not From_Aspect_Specification (N)
12852 then
12853 Error_Msg_Sloc := Sloc (DTL);
12854 Error_Msg_N
12855 ("representation item for& appears after derived type "
12856 & "declaration#??", N);
12857 Error_Msg_NE
12858 ("\may result in implicit conversions for primitive "
12859 & "operations of&??", N, T);
12860 Error_Msg_NE
12861 ("\to change representations when called with arguments "
12862 & "of type&??", N, DTL);
12863 end if;
12864 end;
12865 end if;
12866
12867 -- No error, link item into head of chain of rep items for the entity,
12868 -- but avoid chaining if we have an overloadable entity, and the pragma
12869 -- is one that can apply to multiple overloaded entities.
12870
12871 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12872 declare
12873 Pname : constant Name_Id := Pragma_Name (N);
12874 begin
12875 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12876 Name_External, Name_Interface)
12877 then
12878 return False;
12879 end if;
12880 end;
12881 end if;
12882
12883 Record_Rep_Item (T, N);
12884 return False;
12885 end Rep_Item_Too_Late;
12886
12887 -------------------------------------
12888 -- Replace_Type_References_Generic --
12889 -------------------------------------
12890
12891 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12892 TName : constant Name_Id := Chars (T);
12893
12894 function Replace_Node (N : Node_Id) return Traverse_Result;
12895 -- Processes a single node in the traversal procedure below, checking
12896 -- if node N should be replaced, and if so, doing the replacement.
12897
12898 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
12899 -- This instantiation provides the body of Replace_Type_References
12900
12901 ------------------
12902 -- Replace_Node --
12903 ------------------
12904
12905 function Replace_Node (N : Node_Id) return Traverse_Result is
12906 S : Entity_Id;
12907 P : Node_Id;
12908
12909 begin
12910 -- Case of identifier
12911
12912 if Nkind (N) = N_Identifier then
12913
12914 -- If not the type name, check whether it is a reference to
12915 -- some other type, which must be frozen before the predicate
12916 -- function is analyzed, i.e. before the freeze node of the
12917 -- type to which the predicate applies.
12918
12919 if Chars (N) /= TName then
12920 if Present (Current_Entity (N))
12921 and then Is_Type (Current_Entity (N))
12922 then
12923 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12924 end if;
12925
12926 return Skip;
12927
12928 -- Otherwise do the replacement and we are done with this node
12929
12930 else
12931 Replace_Type_Reference (N);
12932 return Skip;
12933 end if;
12934
12935 -- Case of selected component (which is what a qualification
12936 -- looks like in the unanalyzed tree, which is what we have.
12937
12938 elsif Nkind (N) = N_Selected_Component then
12939
12940 -- If selector name is not our type, keeping going (we might
12941 -- still have an occurrence of the type in the prefix).
12942
12943 if Nkind (Selector_Name (N)) /= N_Identifier
12944 or else Chars (Selector_Name (N)) /= TName
12945 then
12946 return OK;
12947
12948 -- Selector name is our type, check qualification
12949
12950 else
12951 -- Loop through scopes and prefixes, doing comparison
12952
12953 S := Current_Scope;
12954 P := Prefix (N);
12955 loop
12956 -- Continue if no more scopes or scope with no name
12957
12958 if No (S) or else Nkind (S) not in N_Has_Chars then
12959 return OK;
12960 end if;
12961
12962 -- Do replace if prefix is an identifier matching the
12963 -- scope that we are currently looking at.
12964
12965 if Nkind (P) = N_Identifier
12966 and then Chars (P) = Chars (S)
12967 then
12968 Replace_Type_Reference (N);
12969 return Skip;
12970 end if;
12971
12972 -- Go check scope above us if prefix is itself of the
12973 -- form of a selected component, whose selector matches
12974 -- the scope we are currently looking at.
12975
12976 if Nkind (P) = N_Selected_Component
12977 and then Nkind (Selector_Name (P)) = N_Identifier
12978 and then Chars (Selector_Name (P)) = Chars (S)
12979 then
12980 S := Scope (S);
12981 P := Prefix (P);
12982
12983 -- For anything else, we don't have a match, so keep on
12984 -- going, there are still some weird cases where we may
12985 -- still have a replacement within the prefix.
12986
12987 else
12988 return OK;
12989 end if;
12990 end loop;
12991 end if;
12992
12993 -- Continue for any other node kind
12994
12995 else
12996 return OK;
12997 end if;
12998 end Replace_Node;
12999
13000 begin
13001 Replace_Type_Refs (N);
13002 end Replace_Type_References_Generic;
13003
13004 --------------------------------
13005 -- Resolve_Aspect_Expressions --
13006 --------------------------------
13007
13008 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
13009 ASN : Node_Id;
13010 A_Id : Aspect_Id;
13011 Expr : Node_Id;
13012
13013 function Resolve_Name (N : Node_Id) return Traverse_Result;
13014 -- Verify that all identifiers in the expression, with the exception
13015 -- of references to the current entity, denote visible entities. This
13016 -- is done only to detect visibility errors, as the expression will be
13017 -- properly analyzed/expanded during analysis of the predicate function
13018 -- body. We omit quantified expressions from this test, given that they
13019 -- introduce a local identifier that would require proper expansion to
13020 -- handle properly.
13021
13022 ------------------
13023 -- Resolve_Name --
13024 ------------------
13025
13026 function Resolve_Name (N : Node_Id) return Traverse_Result is
13027 begin
13028 if Nkind (N) = N_Selected_Component then
13029 if Nkind (Prefix (N)) = N_Identifier
13030 and then Chars (Prefix (N)) /= Chars (E)
13031 then
13032 Find_Selected_Component (N);
13033 end if;
13034
13035 return Skip;
13036
13037 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
13038 Find_Direct_Name (N);
13039 Set_Entity (N, Empty);
13040
13041 elsif Nkind (N) = N_Quantified_Expression then
13042 return Skip;
13043 end if;
13044
13045 return OK;
13046 end Resolve_Name;
13047
13048 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
13049
13050 -- Start of processing for Resolve_Aspect_Expressions
13051
13052 begin
13053 ASN := First_Rep_Item (E);
13054 while Present (ASN) loop
13055 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
13056 A_Id := Get_Aspect_Id (ASN);
13057 Expr := Expression (ASN);
13058
13059 case A_Id is
13060 -- For now we only deal with aspects that do not generate
13061 -- subprograms, or that may mention current instances of
13062 -- types. These will require special handling (???TBD).
13063
13064 when Aspect_Predicate |
13065 Aspect_Predicate_Failure |
13066 Aspect_Invariant =>
13067 null;
13068
13069 when Aspect_Static_Predicate |
13070 Aspect_Dynamic_Predicate =>
13071
13072 -- Build predicate function specification and preanalyze
13073 -- expression after type replacement.
13074
13075 if No (Predicate_Function (E)) then
13076 declare
13077 FDecl : constant Node_Id :=
13078 Build_Predicate_Function_Declaration (E);
13079 pragma Unreferenced (FDecl);
13080 begin
13081 Resolve_Aspect_Expression (Expr);
13082 end;
13083 end if;
13084
13085 when Pre_Post_Aspects =>
13086 null;
13087
13088 when Aspect_Iterable =>
13089 if Nkind (Expr) = N_Aggregate then
13090 declare
13091 Assoc : Node_Id;
13092
13093 begin
13094 Assoc := First (Component_Associations (Expr));
13095 while Present (Assoc) loop
13096 Find_Direct_Name (Expression (Assoc));
13097 Next (Assoc);
13098 end loop;
13099 end;
13100 end if;
13101
13102 when others =>
13103 if Present (Expr) then
13104 case Aspect_Argument (A_Id) is
13105 when Expression | Optional_Expression =>
13106 Analyze_And_Resolve (Expression (ASN));
13107
13108 when Name | Optional_Name =>
13109 if Nkind (Expr) = N_Identifier then
13110 Find_Direct_Name (Expr);
13111
13112 elsif Nkind (Expr) = N_Selected_Component then
13113 Find_Selected_Component (Expr);
13114
13115 else
13116 null;
13117 end if;
13118 end case;
13119 end if;
13120 end case;
13121 end if;
13122
13123 ASN := Next_Rep_Item (ASN);
13124 end loop;
13125 end Resolve_Aspect_Expressions;
13126
13127 -------------------------
13128 -- Same_Representation --
13129 -------------------------
13130
13131 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
13132 T1 : constant Entity_Id := Underlying_Type (Typ1);
13133 T2 : constant Entity_Id := Underlying_Type (Typ2);
13134
13135 begin
13136 -- A quick check, if base types are the same, then we definitely have
13137 -- the same representation, because the subtype specific representation
13138 -- attributes (Size and Alignment) do not affect representation from
13139 -- the point of view of this test.
13140
13141 if Base_Type (T1) = Base_Type (T2) then
13142 return True;
13143
13144 elsif Is_Private_Type (Base_Type (T2))
13145 and then Base_Type (T1) = Full_View (Base_Type (T2))
13146 then
13147 return True;
13148 end if;
13149
13150 -- Tagged types never have differing representations
13151
13152 if Is_Tagged_Type (T1) then
13153 return True;
13154 end if;
13155
13156 -- Representations are definitely different if conventions differ
13157
13158 if Convention (T1) /= Convention (T2) then
13159 return False;
13160 end if;
13161
13162 -- Representations are different if component alignments or scalar
13163 -- storage orders differ.
13164
13165 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
13166 and then
13167 (Is_Record_Type (T2) or else Is_Array_Type (T2))
13168 and then
13169 (Component_Alignment (T1) /= Component_Alignment (T2)
13170 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13171 then
13172 return False;
13173 end if;
13174
13175 -- For arrays, the only real issue is component size. If we know the
13176 -- component size for both arrays, and it is the same, then that's
13177 -- good enough to know we don't have a change of representation.
13178
13179 if Is_Array_Type (T1) then
13180 if Known_Component_Size (T1)
13181 and then Known_Component_Size (T2)
13182 and then Component_Size (T1) = Component_Size (T2)
13183 then
13184 return True;
13185 end if;
13186 end if;
13187
13188 -- Types definitely have same representation if neither has non-standard
13189 -- representation since default representations are always consistent.
13190 -- If only one has non-standard representation, and the other does not,
13191 -- then we consider that they do not have the same representation. They
13192 -- might, but there is no way of telling early enough.
13193
13194 if Has_Non_Standard_Rep (T1) then
13195 if not Has_Non_Standard_Rep (T2) then
13196 return False;
13197 end if;
13198 else
13199 return not Has_Non_Standard_Rep (T2);
13200 end if;
13201
13202 -- Here the two types both have non-standard representation, and we need
13203 -- to determine if they have the same non-standard representation.
13204
13205 -- For arrays, we simply need to test if the component sizes are the
13206 -- same. Pragma Pack is reflected in modified component sizes, so this
13207 -- check also deals with pragma Pack.
13208
13209 if Is_Array_Type (T1) then
13210 return Component_Size (T1) = Component_Size (T2);
13211
13212 -- Tagged types always have the same representation, because it is not
13213 -- possible to specify different representations for common fields.
13214
13215 elsif Is_Tagged_Type (T1) then
13216 return True;
13217
13218 -- Case of record types
13219
13220 elsif Is_Record_Type (T1) then
13221
13222 -- Packed status must conform
13223
13224 if Is_Packed (T1) /= Is_Packed (T2) then
13225 return False;
13226
13227 -- Otherwise we must check components. Typ2 maybe a constrained
13228 -- subtype with fewer components, so we compare the components
13229 -- of the base types.
13230
13231 else
13232 Record_Case : declare
13233 CD1, CD2 : Entity_Id;
13234
13235 function Same_Rep return Boolean;
13236 -- CD1 and CD2 are either components or discriminants. This
13237 -- function tests whether they have the same representation.
13238
13239 --------------
13240 -- Same_Rep --
13241 --------------
13242
13243 function Same_Rep return Boolean is
13244 begin
13245 if No (Component_Clause (CD1)) then
13246 return No (Component_Clause (CD2));
13247 else
13248 -- Note: at this point, component clauses have been
13249 -- normalized to the default bit order, so that the
13250 -- comparison of Component_Bit_Offsets is meaningful.
13251
13252 return
13253 Present (Component_Clause (CD2))
13254 and then
13255 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13256 and then
13257 Esize (CD1) = Esize (CD2);
13258 end if;
13259 end Same_Rep;
13260
13261 -- Start of processing for Record_Case
13262
13263 begin
13264 if Has_Discriminants (T1) then
13265
13266 -- The number of discriminants may be different if the
13267 -- derived type has fewer (constrained by values). The
13268 -- invisible discriminants retain the representation of
13269 -- the original, so the discrepancy does not per se
13270 -- indicate a different representation.
13271
13272 CD1 := First_Discriminant (T1);
13273 CD2 := First_Discriminant (T2);
13274 while Present (CD1) and then Present (CD2) loop
13275 if not Same_Rep then
13276 return False;
13277 else
13278 Next_Discriminant (CD1);
13279 Next_Discriminant (CD2);
13280 end if;
13281 end loop;
13282 end if;
13283
13284 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13285 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13286 while Present (CD1) loop
13287 if not Same_Rep then
13288 return False;
13289 else
13290 Next_Component (CD1);
13291 Next_Component (CD2);
13292 end if;
13293 end loop;
13294
13295 return True;
13296 end Record_Case;
13297 end if;
13298
13299 -- For enumeration types, we must check each literal to see if the
13300 -- representation is the same. Note that we do not permit enumeration
13301 -- representation clauses for Character and Wide_Character, so these
13302 -- cases were already dealt with.
13303
13304 elsif Is_Enumeration_Type (T1) then
13305 Enumeration_Case : declare
13306 L1, L2 : Entity_Id;
13307
13308 begin
13309 L1 := First_Literal (T1);
13310 L2 := First_Literal (T2);
13311 while Present (L1) loop
13312 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13313 return False;
13314 else
13315 Next_Literal (L1);
13316 Next_Literal (L2);
13317 end if;
13318 end loop;
13319
13320 return True;
13321 end Enumeration_Case;
13322
13323 -- Any other types have the same representation for these purposes
13324
13325 else
13326 return True;
13327 end if;
13328 end Same_Representation;
13329
13330 --------------------------------
13331 -- Resolve_Iterable_Operation --
13332 --------------------------------
13333
13334 procedure Resolve_Iterable_Operation
13335 (N : Node_Id;
13336 Cursor : Entity_Id;
13337 Typ : Entity_Id;
13338 Nam : Name_Id)
13339 is
13340 Ent : Entity_Id;
13341 F1 : Entity_Id;
13342 F2 : Entity_Id;
13343
13344 begin
13345 if not Is_Overloaded (N) then
13346 if not Is_Entity_Name (N)
13347 or else Ekind (Entity (N)) /= E_Function
13348 or else Scope (Entity (N)) /= Scope (Typ)
13349 or else No (First_Formal (Entity (N)))
13350 or else Etype (First_Formal (Entity (N))) /= Typ
13351 then
13352 Error_Msg_N ("iterable primitive must be local function name "
13353 & "whose first formal is an iterable type", N);
13354 return;
13355 end if;
13356
13357 Ent := Entity (N);
13358 F1 := First_Formal (Ent);
13359 if Nam = Name_First then
13360
13361 -- First (Container) => Cursor
13362
13363 if Etype (Ent) /= Cursor then
13364 Error_Msg_N ("primitive for First must yield a curosr", N);
13365 end if;
13366
13367 elsif Nam = Name_Next then
13368
13369 -- Next (Container, Cursor) => Cursor
13370
13371 F2 := Next_Formal (F1);
13372
13373 if Etype (F2) /= Cursor
13374 or else Etype (Ent) /= Cursor
13375 or else Present (Next_Formal (F2))
13376 then
13377 Error_Msg_N ("no match for Next iterable primitive", N);
13378 end if;
13379
13380 elsif Nam = Name_Has_Element then
13381
13382 -- Has_Element (Container, Cursor) => Boolean
13383
13384 F2 := Next_Formal (F1);
13385 if Etype (F2) /= Cursor
13386 or else Etype (Ent) /= Standard_Boolean
13387 or else Present (Next_Formal (F2))
13388 then
13389 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13390 end if;
13391
13392 elsif Nam = Name_Element then
13393 F2 := Next_Formal (F1);
13394
13395 if No (F2)
13396 or else Etype (F2) /= Cursor
13397 or else Present (Next_Formal (F2))
13398 then
13399 Error_Msg_N ("no match for Element iterable primitive", N);
13400 end if;
13401 null;
13402
13403 else
13404 raise Program_Error;
13405 end if;
13406
13407 else
13408 -- Overloaded case: find subprogram with proper signature.
13409 -- Caller will report error if no match is found.
13410
13411 declare
13412 I : Interp_Index;
13413 It : Interp;
13414
13415 begin
13416 Get_First_Interp (N, I, It);
13417 while Present (It.Typ) loop
13418 if Ekind (It.Nam) = E_Function
13419 and then Scope (It.Nam) = Scope (Typ)
13420 and then Etype (First_Formal (It.Nam)) = Typ
13421 then
13422 F1 := First_Formal (It.Nam);
13423
13424 if Nam = Name_First then
13425 if Etype (It.Nam) = Cursor
13426 and then No (Next_Formal (F1))
13427 then
13428 Set_Entity (N, It.Nam);
13429 exit;
13430 end if;
13431
13432 elsif Nam = Name_Next then
13433 F2 := Next_Formal (F1);
13434
13435 if Present (F2)
13436 and then No (Next_Formal (F2))
13437 and then Etype (F2) = Cursor
13438 and then Etype (It.Nam) = Cursor
13439 then
13440 Set_Entity (N, It.Nam);
13441 exit;
13442 end if;
13443
13444 elsif Nam = Name_Has_Element then
13445 F2 := Next_Formal (F1);
13446
13447 if Present (F2)
13448 and then No (Next_Formal (F2))
13449 and then Etype (F2) = Cursor
13450 and then Etype (It.Nam) = Standard_Boolean
13451 then
13452 Set_Entity (N, It.Nam);
13453 F2 := Next_Formal (F1);
13454 exit;
13455 end if;
13456
13457 elsif Nam = Name_Element then
13458 F2 := Next_Formal (F1);
13459
13460 if Present (F2)
13461 and then No (Next_Formal (F2))
13462 and then Etype (F2) = Cursor
13463 then
13464 Set_Entity (N, It.Nam);
13465 exit;
13466 end if;
13467 end if;
13468 end if;
13469
13470 Get_Next_Interp (I, It);
13471 end loop;
13472 end;
13473 end if;
13474 end Resolve_Iterable_Operation;
13475
13476 ----------------
13477 -- Set_Biased --
13478 ----------------
13479
13480 procedure Set_Biased
13481 (E : Entity_Id;
13482 N : Node_Id;
13483 Msg : String;
13484 Biased : Boolean := True)
13485 is
13486 begin
13487 if Biased then
13488 Set_Has_Biased_Representation (E);
13489
13490 if Warn_On_Biased_Representation then
13491 Error_Msg_NE
13492 ("?B?" & Msg & " forces biased representation for&", N, E);
13493 end if;
13494 end if;
13495 end Set_Biased;
13496
13497 --------------------
13498 -- Set_Enum_Esize --
13499 --------------------
13500
13501 procedure Set_Enum_Esize (T : Entity_Id) is
13502 Lo : Uint;
13503 Hi : Uint;
13504 Sz : Nat;
13505
13506 begin
13507 Init_Alignment (T);
13508
13509 -- Find the minimum standard size (8,16,32,64) that fits
13510
13511 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13512 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13513
13514 if Lo < 0 then
13515 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
13516 Sz := Standard_Character_Size; -- May be > 8 on some targets
13517
13518 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13519 Sz := 16;
13520
13521 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13522 Sz := 32;
13523
13524 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13525 Sz := 64;
13526 end if;
13527
13528 else
13529 if Hi < Uint_2**08 then
13530 Sz := Standard_Character_Size; -- May be > 8 on some targets
13531
13532 elsif Hi < Uint_2**16 then
13533 Sz := 16;
13534
13535 elsif Hi < Uint_2**32 then
13536 Sz := 32;
13537
13538 else pragma Assert (Hi < Uint_2**63);
13539 Sz := 64;
13540 end if;
13541 end if;
13542
13543 -- That minimum is the proper size unless we have a foreign convention
13544 -- and the size required is 32 or less, in which case we bump the size
13545 -- up to 32. This is required for C and C++ and seems reasonable for
13546 -- all other foreign conventions.
13547
13548 if Has_Foreign_Convention (T)
13549 and then Esize (T) < Standard_Integer_Size
13550
13551 -- Don't do this if Short_Enums on target
13552
13553 and then not Target_Short_Enums
13554 then
13555 Init_Esize (T, Standard_Integer_Size);
13556 else
13557 Init_Esize (T, Sz);
13558 end if;
13559 end Set_Enum_Esize;
13560
13561 -----------------------------
13562 -- Uninstall_Discriminants --
13563 -----------------------------
13564
13565 procedure Uninstall_Discriminants (E : Entity_Id) is
13566 Disc : Entity_Id;
13567 Prev : Entity_Id;
13568 Outer : Entity_Id;
13569
13570 begin
13571 -- Discriminants have been made visible for type declarations and
13572 -- protected type declarations, not for subtype declarations.
13573
13574 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13575 Disc := First_Discriminant (E);
13576 while Present (Disc) loop
13577 if Disc /= Current_Entity (Disc) then
13578 Prev := Current_Entity (Disc);
13579 while Present (Prev)
13580 and then Present (Homonym (Prev))
13581 and then Homonym (Prev) /= Disc
13582 loop
13583 Prev := Homonym (Prev);
13584 end loop;
13585 else
13586 Prev := Empty;
13587 end if;
13588
13589 Set_Is_Immediately_Visible (Disc, False);
13590
13591 Outer := Homonym (Disc);
13592 while Present (Outer) and then Scope (Outer) = E loop
13593 Outer := Homonym (Outer);
13594 end loop;
13595
13596 -- Reset homonym link of other entities, but do not modify link
13597 -- between entities in current scope, so that the back end can
13598 -- have a proper count of local overloadings.
13599
13600 if No (Prev) then
13601 Set_Name_Entity_Id (Chars (Disc), Outer);
13602
13603 elsif Scope (Prev) /= Scope (Disc) then
13604 Set_Homonym (Prev, Outer);
13605 end if;
13606
13607 Next_Discriminant (Disc);
13608 end loop;
13609 end if;
13610 end Uninstall_Discriminants;
13611
13612 -------------------------------------------
13613 -- Uninstall_Discriminants_And_Pop_Scope --
13614 -------------------------------------------
13615
13616 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13617 begin
13618 if Has_Discriminants (E) then
13619 Uninstall_Discriminants (E);
13620 Pop_Scope;
13621 end if;
13622 end Uninstall_Discriminants_And_Pop_Scope;
13623
13624 ------------------------------
13625 -- Validate_Address_Clauses --
13626 ------------------------------
13627
13628 procedure Validate_Address_Clauses is
13629 function Offset_Value (Expr : Node_Id) return Uint;
13630 -- Given an Address attribute reference, return the value in bits of its
13631 -- offset from the first bit of the underlying entity, or 0 if it is not
13632 -- known at compile time.
13633
13634 ------------------
13635 -- Offset_Value --
13636 ------------------
13637
13638 function Offset_Value (Expr : Node_Id) return Uint is
13639 N : Node_Id := Prefix (Expr);
13640 Off : Uint;
13641 Val : Uint := Uint_0;
13642
13643 begin
13644 -- Climb the prefix chain and compute the cumulative offset
13645
13646 loop
13647 if Is_Entity_Name (N) then
13648 return Val;
13649
13650 elsif Nkind (N) = N_Selected_Component then
13651 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13652 if Off /= No_Uint and then Off >= Uint_0 then
13653 Val := Val + Off;
13654 N := Prefix (N);
13655 else
13656 return Uint_0;
13657 end if;
13658
13659 elsif Nkind (N) = N_Indexed_Component then
13660 Off := Indexed_Component_Bit_Offset (N);
13661 if Off /= No_Uint then
13662 Val := Val + Off;
13663 N := Prefix (N);
13664 else
13665 return Uint_0;
13666 end if;
13667
13668 else
13669 return Uint_0;
13670 end if;
13671 end loop;
13672 end Offset_Value;
13673
13674 -- Start of processing for Validate_Address_Clauses
13675
13676 begin
13677 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13678 declare
13679 ACCR : Address_Clause_Check_Record
13680 renames Address_Clause_Checks.Table (J);
13681
13682 Expr : Node_Id;
13683
13684 X_Alignment : Uint;
13685 Y_Alignment : Uint;
13686
13687 X_Size : Uint;
13688 Y_Size : Uint;
13689
13690 X_Offs : Uint;
13691
13692 begin
13693 -- Skip processing of this entry if warning already posted
13694
13695 if not Address_Warning_Posted (ACCR.N) then
13696 Expr := Original_Node (Expression (ACCR.N));
13697
13698 -- Get alignments
13699
13700 X_Alignment := Alignment (ACCR.X);
13701 Y_Alignment := Alignment (ACCR.Y);
13702
13703 -- Similarly obtain sizes and offset
13704
13705 X_Size := Esize (ACCR.X);
13706 Y_Size := Esize (ACCR.Y);
13707
13708 if ACCR.Off
13709 and then Nkind (Expr) = N_Attribute_Reference
13710 and then Attribute_Name (Expr) = Name_Address
13711 then
13712 X_Offs := Offset_Value (Expr);
13713 else
13714 X_Offs := Uint_0;
13715 end if;
13716
13717 -- Check for large object overlaying smaller one
13718
13719 if Y_Size > Uint_0
13720 and then X_Size > Uint_0
13721 and then X_Offs + X_Size > Y_Size
13722 then
13723 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
13724 Error_Msg_N
13725 ("\??program execution may be erroneous", ACCR.N);
13726
13727 Error_Msg_Uint_1 := X_Size;
13728 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13729
13730 Error_Msg_Uint_1 := Y_Size;
13731 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
13732
13733 if Y_Size >= X_Size then
13734 Error_Msg_Uint_1 := X_Offs;
13735 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
13736 end if;
13737
13738 -- Check for inadequate alignment, both of the base object
13739 -- and of the offset, if any. We only do this check if the
13740 -- run-time Alignment_Check is active. No point in warning
13741 -- if this check has been suppressed (or is suppressed by
13742 -- default in the non-strict alignment machine case).
13743
13744 -- Note: we do not check the alignment if we gave a size
13745 -- warning, since it would likely be redundant.
13746
13747 elsif not Alignment_Checks_Suppressed (ACCR.Y)
13748 and then Y_Alignment /= Uint_0
13749 and then
13750 (Y_Alignment < X_Alignment
13751 or else
13752 (ACCR.Off
13753 and then Nkind (Expr) = N_Attribute_Reference
13754 and then Attribute_Name (Expr) = Name_Address
13755 and then Has_Compatible_Alignment
13756 (ACCR.X, Prefix (Expr), True) /=
13757 Known_Compatible))
13758 then
13759 Error_Msg_NE
13760 ("??specified address for& may be inconsistent with "
13761 & "alignment", ACCR.N, ACCR.X);
13762 Error_Msg_N
13763 ("\??program execution may be erroneous (RM 13.3(27))",
13764 ACCR.N);
13765
13766 Error_Msg_Uint_1 := X_Alignment;
13767 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13768
13769 Error_Msg_Uint_1 := Y_Alignment;
13770 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13771
13772 if Y_Alignment >= X_Alignment then
13773 Error_Msg_N
13774 ("\??but offset is not multiple of alignment", ACCR.N);
13775 end if;
13776 end if;
13777 end if;
13778 end;
13779 end loop;
13780 end Validate_Address_Clauses;
13781
13782 ---------------------------
13783 -- Validate_Independence --
13784 ---------------------------
13785
13786 procedure Validate_Independence is
13787 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13788 N : Node_Id;
13789 E : Entity_Id;
13790 IC : Boolean;
13791 Comp : Entity_Id;
13792 Addr : Node_Id;
13793 P : Node_Id;
13794
13795 procedure Check_Array_Type (Atyp : Entity_Id);
13796 -- Checks if the array type Atyp has independent components, and
13797 -- if not, outputs an appropriate set of error messages.
13798
13799 procedure No_Independence;
13800 -- Output message that independence cannot be guaranteed
13801
13802 function OK_Component (C : Entity_Id) return Boolean;
13803 -- Checks one component to see if it is independently accessible, and
13804 -- if so yields True, otherwise yields False if independent access
13805 -- cannot be guaranteed. This is a conservative routine, it only
13806 -- returns True if it knows for sure, it returns False if it knows
13807 -- there is a problem, or it cannot be sure there is no problem.
13808
13809 procedure Reason_Bad_Component (C : Entity_Id);
13810 -- Outputs continuation message if a reason can be determined for
13811 -- the component C being bad.
13812
13813 ----------------------
13814 -- Check_Array_Type --
13815 ----------------------
13816
13817 procedure Check_Array_Type (Atyp : Entity_Id) is
13818 Ctyp : constant Entity_Id := Component_Type (Atyp);
13819
13820 begin
13821 -- OK if no alignment clause, no pack, and no component size
13822
13823 if not Has_Component_Size_Clause (Atyp)
13824 and then not Has_Alignment_Clause (Atyp)
13825 and then not Is_Packed (Atyp)
13826 then
13827 return;
13828 end if;
13829
13830 -- Case of component size is greater than or equal to 64 and the
13831 -- alignment of the array is at least as large as the alignment
13832 -- of the component. We are definitely OK in this situation.
13833
13834 if Known_Component_Size (Atyp)
13835 and then Component_Size (Atyp) >= 64
13836 and then Known_Alignment (Atyp)
13837 and then Known_Alignment (Ctyp)
13838 and then Alignment (Atyp) >= Alignment (Ctyp)
13839 then
13840 return;
13841 end if;
13842
13843 -- Check actual component size
13844
13845 if not Known_Component_Size (Atyp)
13846 or else not (Addressable (Component_Size (Atyp))
13847 and then Component_Size (Atyp) < 64)
13848 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13849 then
13850 No_Independence;
13851
13852 -- Bad component size, check reason
13853
13854 if Has_Component_Size_Clause (Atyp) then
13855 P := Get_Attribute_Definition_Clause
13856 (Atyp, Attribute_Component_Size);
13857
13858 if Present (P) then
13859 Error_Msg_Sloc := Sloc (P);
13860 Error_Msg_N ("\because of Component_Size clause#", N);
13861 return;
13862 end if;
13863 end if;
13864
13865 if Is_Packed (Atyp) then
13866 P := Get_Rep_Pragma (Atyp, Name_Pack);
13867
13868 if Present (P) then
13869 Error_Msg_Sloc := Sloc (P);
13870 Error_Msg_N ("\because of pragma Pack#", N);
13871 return;
13872 end if;
13873 end if;
13874
13875 -- No reason found, just return
13876
13877 return;
13878 end if;
13879
13880 -- Array type is OK independence-wise
13881
13882 return;
13883 end Check_Array_Type;
13884
13885 ---------------------
13886 -- No_Independence --
13887 ---------------------
13888
13889 procedure No_Independence is
13890 begin
13891 if Pragma_Name (N) = Name_Independent then
13892 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
13893 else
13894 Error_Msg_NE
13895 ("independent components cannot be guaranteed for&", N, E);
13896 end if;
13897 end No_Independence;
13898
13899 ------------------
13900 -- OK_Component --
13901 ------------------
13902
13903 function OK_Component (C : Entity_Id) return Boolean is
13904 Rec : constant Entity_Id := Scope (C);
13905 Ctyp : constant Entity_Id := Etype (C);
13906
13907 begin
13908 -- OK if no component clause, no Pack, and no alignment clause
13909
13910 if No (Component_Clause (C))
13911 and then not Is_Packed (Rec)
13912 and then not Has_Alignment_Clause (Rec)
13913 then
13914 return True;
13915 end if;
13916
13917 -- Here we look at the actual component layout. A component is
13918 -- addressable if its size is a multiple of the Esize of the
13919 -- component type, and its starting position in the record has
13920 -- appropriate alignment, and the record itself has appropriate
13921 -- alignment to guarantee the component alignment.
13922
13923 -- Make sure sizes are static, always assume the worst for any
13924 -- cases where we cannot check static values.
13925
13926 if not (Known_Static_Esize (C)
13927 and then
13928 Known_Static_Esize (Ctyp))
13929 then
13930 return False;
13931 end if;
13932
13933 -- Size of component must be addressable or greater than 64 bits
13934 -- and a multiple of bytes.
13935
13936 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
13937 return False;
13938 end if;
13939
13940 -- Check size is proper multiple
13941
13942 if Esize (C) mod Esize (Ctyp) /= 0 then
13943 return False;
13944 end if;
13945
13946 -- Check alignment of component is OK
13947
13948 if not Known_Component_Bit_Offset (C)
13949 or else Component_Bit_Offset (C) < Uint_0
13950 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13951 then
13952 return False;
13953 end if;
13954
13955 -- Check alignment of record type is OK
13956
13957 if not Known_Alignment (Rec)
13958 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13959 then
13960 return False;
13961 end if;
13962
13963 -- All tests passed, component is addressable
13964
13965 return True;
13966 end OK_Component;
13967
13968 --------------------------
13969 -- Reason_Bad_Component --
13970 --------------------------
13971
13972 procedure Reason_Bad_Component (C : Entity_Id) is
13973 Rec : constant Entity_Id := Scope (C);
13974 Ctyp : constant Entity_Id := Etype (C);
13975
13976 begin
13977 -- If component clause present assume that's the problem
13978
13979 if Present (Component_Clause (C)) then
13980 Error_Msg_Sloc := Sloc (Component_Clause (C));
13981 Error_Msg_N ("\because of Component_Clause#", N);
13982 return;
13983 end if;
13984
13985 -- If pragma Pack clause present, assume that's the problem
13986
13987 if Is_Packed (Rec) then
13988 P := Get_Rep_Pragma (Rec, Name_Pack);
13989
13990 if Present (P) then
13991 Error_Msg_Sloc := Sloc (P);
13992 Error_Msg_N ("\because of pragma Pack#", N);
13993 return;
13994 end if;
13995 end if;
13996
13997 -- See if record has bad alignment clause
13998
13999 if Has_Alignment_Clause (Rec)
14000 and then Known_Alignment (Rec)
14001 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
14002 then
14003 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
14004
14005 if Present (P) then
14006 Error_Msg_Sloc := Sloc (P);
14007 Error_Msg_N ("\because of Alignment clause#", N);
14008 end if;
14009 end if;
14010
14011 -- Couldn't find a reason, so return without a message
14012
14013 return;
14014 end Reason_Bad_Component;
14015
14016 -- Start of processing for Validate_Independence
14017
14018 begin
14019 for J in Independence_Checks.First .. Independence_Checks.Last loop
14020 N := Independence_Checks.Table (J).N;
14021 E := Independence_Checks.Table (J).E;
14022 IC := Pragma_Name (N) = Name_Independent_Components;
14023
14024 -- Deal with component case
14025
14026 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
14027 if not OK_Component (E) then
14028 No_Independence;
14029 Reason_Bad_Component (E);
14030 goto Continue;
14031 end if;
14032 end if;
14033
14034 -- Deal with record with Independent_Components
14035
14036 if IC and then Is_Record_Type (E) then
14037 Comp := First_Component_Or_Discriminant (E);
14038 while Present (Comp) loop
14039 if not OK_Component (Comp) then
14040 No_Independence;
14041 Reason_Bad_Component (Comp);
14042 goto Continue;
14043 end if;
14044
14045 Next_Component_Or_Discriminant (Comp);
14046 end loop;
14047 end if;
14048
14049 -- Deal with address clause case
14050
14051 if Is_Object (E) then
14052 Addr := Address_Clause (E);
14053
14054 if Present (Addr) then
14055 No_Independence;
14056 Error_Msg_Sloc := Sloc (Addr);
14057 Error_Msg_N ("\because of Address clause#", N);
14058 goto Continue;
14059 end if;
14060 end if;
14061
14062 -- Deal with independent components for array type
14063
14064 if IC and then Is_Array_Type (E) then
14065 Check_Array_Type (E);
14066 end if;
14067
14068 -- Deal with independent components for array object
14069
14070 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
14071 Check_Array_Type (Etype (E));
14072 end if;
14073
14074 <<Continue>> null;
14075 end loop;
14076 end Validate_Independence;
14077
14078 ------------------------------
14079 -- Validate_Iterable_Aspect --
14080 ------------------------------
14081
14082 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
14083 Assoc : Node_Id;
14084 Expr : Node_Id;
14085
14086 Prim : Node_Id;
14087 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
14088
14089 First_Id : Entity_Id;
14090 Next_Id : Entity_Id;
14091 Has_Element_Id : Entity_Id;
14092 Element_Id : Entity_Id;
14093
14094 begin
14095 -- If previous error aspect is unusable
14096
14097 if Cursor = Any_Type then
14098 return;
14099 end if;
14100
14101 First_Id := Empty;
14102 Next_Id := Empty;
14103 Has_Element_Id := Empty;
14104 Element_Id := Empty;
14105
14106 -- Each expression must resolve to a function with the proper signature
14107
14108 Assoc := First (Component_Associations (Expression (ASN)));
14109 while Present (Assoc) loop
14110 Expr := Expression (Assoc);
14111 Analyze (Expr);
14112
14113 Prim := First (Choices (Assoc));
14114
14115 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
14116 Error_Msg_N ("illegal name in association", Prim);
14117
14118 elsif Chars (Prim) = Name_First then
14119 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
14120 First_Id := Entity (Expr);
14121
14122 elsif Chars (Prim) = Name_Next then
14123 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
14124 Next_Id := Entity (Expr);
14125
14126 elsif Chars (Prim) = Name_Has_Element then
14127 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
14128 Has_Element_Id := Entity (Expr);
14129
14130 elsif Chars (Prim) = Name_Element then
14131 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
14132 Element_Id := Entity (Expr);
14133
14134 else
14135 Error_Msg_N ("invalid name for iterable function", Prim);
14136 end if;
14137
14138 Next (Assoc);
14139 end loop;
14140
14141 if No (First_Id) then
14142 Error_Msg_N ("match for First primitive not found", ASN);
14143
14144 elsif No (Next_Id) then
14145 Error_Msg_N ("match for Next primitive not found", ASN);
14146
14147 elsif No (Has_Element_Id) then
14148 Error_Msg_N ("match for Has_Element primitive not found", ASN);
14149
14150 elsif No (Element_Id) then
14151 null; -- Optional.
14152 end if;
14153 end Validate_Iterable_Aspect;
14154
14155 -----------------------------------
14156 -- Validate_Unchecked_Conversion --
14157 -----------------------------------
14158
14159 procedure Validate_Unchecked_Conversion
14160 (N : Node_Id;
14161 Act_Unit : Entity_Id)
14162 is
14163 Source : Entity_Id;
14164 Target : Entity_Id;
14165 Vnode : Node_Id;
14166
14167 begin
14168 -- Obtain source and target types. Note that we call Ancestor_Subtype
14169 -- here because the processing for generic instantiation always makes
14170 -- subtypes, and we want the original frozen actual types.
14171
14172 -- If we are dealing with private types, then do the check on their
14173 -- fully declared counterparts if the full declarations have been
14174 -- encountered (they don't have to be visible, but they must exist).
14175
14176 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
14177
14178 if Is_Private_Type (Source)
14179 and then Present (Underlying_Type (Source))
14180 then
14181 Source := Underlying_Type (Source);
14182 end if;
14183
14184 Target := Ancestor_Subtype (Etype (Act_Unit));
14185
14186 -- If either type is generic, the instantiation happens within a generic
14187 -- unit, and there is nothing to check. The proper check will happen
14188 -- when the enclosing generic is instantiated.
14189
14190 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
14191 return;
14192 end if;
14193
14194 if Is_Private_Type (Target)
14195 and then Present (Underlying_Type (Target))
14196 then
14197 Target := Underlying_Type (Target);
14198 end if;
14199
14200 -- Source may be unconstrained array, but not target, except in relaxed
14201 -- semantics mode.
14202
14203 if Is_Array_Type (Target)
14204 and then not Is_Constrained (Target)
14205 and then not Relaxed_RM_Semantics
14206 then
14207 Error_Msg_N
14208 ("unchecked conversion to unconstrained array not allowed", N);
14209 return;
14210 end if;
14211
14212 -- Warn if conversion between two different convention pointers
14213
14214 if Is_Access_Type (Target)
14215 and then Is_Access_Type (Source)
14216 and then Convention (Target) /= Convention (Source)
14217 and then Warn_On_Unchecked_Conversion
14218 then
14219 -- Give warnings for subprogram pointers only on most targets
14220
14221 if Is_Access_Subprogram_Type (Target)
14222 or else Is_Access_Subprogram_Type (Source)
14223 then
14224 Error_Msg_N
14225 ("?z?conversion between pointers with different conventions!",
14226 N);
14227 end if;
14228 end if;
14229
14230 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
14231 -- warning when compiling GNAT-related sources.
14232
14233 if Warn_On_Unchecked_Conversion
14234 and then not In_Predefined_Unit (N)
14235 and then RTU_Loaded (Ada_Calendar)
14236 and then (Chars (Source) = Name_Time
14237 or else
14238 Chars (Target) = Name_Time)
14239 then
14240 -- If Ada.Calendar is loaded and the name of one of the operands is
14241 -- Time, there is a good chance that this is Ada.Calendar.Time.
14242
14243 declare
14244 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
14245 begin
14246 pragma Assert (Present (Calendar_Time));
14247
14248 if Source = Calendar_Time or else Target = Calendar_Time then
14249 Error_Msg_N
14250 ("?z?representation of 'Time values may change between "
14251 & "'G'N'A'T versions", N);
14252 end if;
14253 end;
14254 end if;
14255
14256 -- Make entry in unchecked conversion table for later processing by
14257 -- Validate_Unchecked_Conversions, which will check sizes and alignments
14258 -- (using values set by the back end where possible). This is only done
14259 -- if the appropriate warning is active.
14260
14261 if Warn_On_Unchecked_Conversion then
14262 Unchecked_Conversions.Append
14263 (New_Val => UC_Entry'(Eloc => Sloc (N),
14264 Source => Source,
14265 Target => Target,
14266 Act_Unit => Act_Unit));
14267
14268 -- If both sizes are known statically now, then back-end annotation
14269 -- is not required to do a proper check but if either size is not
14270 -- known statically, then we need the annotation.
14271
14272 if Known_Static_RM_Size (Source)
14273 and then
14274 Known_Static_RM_Size (Target)
14275 then
14276 null;
14277 else
14278 Back_Annotate_Rep_Info := True;
14279 end if;
14280 end if;
14281
14282 -- If unchecked conversion to access type, and access type is declared
14283 -- in the same unit as the unchecked conversion, then set the flag
14284 -- No_Strict_Aliasing (no strict aliasing is implicit here)
14285
14286 if Is_Access_Type (Target) and then
14287 In_Same_Source_Unit (Target, N)
14288 then
14289 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14290 end if;
14291
14292 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14293 -- the back end needs to perform special validation checks.
14294
14295 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14296 -- have full expansion and the back end is called ???
14297
14298 Vnode :=
14299 Make_Validate_Unchecked_Conversion (Sloc (N));
14300 Set_Source_Type (Vnode, Source);
14301 Set_Target_Type (Vnode, Target);
14302
14303 -- If the unchecked conversion node is in a list, just insert before it.
14304 -- If not we have some strange case, not worth bothering about.
14305
14306 if Is_List_Member (N) then
14307 Insert_After (N, Vnode);
14308 end if;
14309 end Validate_Unchecked_Conversion;
14310
14311 ------------------------------------
14312 -- Validate_Unchecked_Conversions --
14313 ------------------------------------
14314
14315 procedure Validate_Unchecked_Conversions is
14316 begin
14317 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14318 declare
14319 T : UC_Entry renames Unchecked_Conversions.Table (N);
14320
14321 Eloc : constant Source_Ptr := T.Eloc;
14322 Source : constant Entity_Id := T.Source;
14323 Target : constant Entity_Id := T.Target;
14324 Act_Unit : constant Entity_Id := T.Act_Unit;
14325
14326 Source_Siz : Uint;
14327 Target_Siz : Uint;
14328
14329 begin
14330 -- Skip if function marked as warnings off
14331
14332 if Warnings_Off (Act_Unit) then
14333 goto Continue;
14334 end if;
14335
14336 -- This validation check, which warns if we have unequal sizes for
14337 -- unchecked conversion, and thus potentially implementation
14338 -- dependent semantics, is one of the few occasions on which we
14339 -- use the official RM size instead of Esize. See description in
14340 -- Einfo "Handling of Type'Size Values" for details.
14341
14342 if Serious_Errors_Detected = 0
14343 and then Known_Static_RM_Size (Source)
14344 and then Known_Static_RM_Size (Target)
14345
14346 -- Don't do the check if warnings off for either type, note the
14347 -- deliberate use of OR here instead of OR ELSE to get the flag
14348 -- Warnings_Off_Used set for both types if appropriate.
14349
14350 and then not (Has_Warnings_Off (Source)
14351 or
14352 Has_Warnings_Off (Target))
14353 then
14354 Source_Siz := RM_Size (Source);
14355 Target_Siz := RM_Size (Target);
14356
14357 if Source_Siz /= Target_Siz then
14358 Error_Msg
14359 ("?z?types for unchecked conversion have different sizes!",
14360 Eloc);
14361
14362 if All_Errors_Mode then
14363 Error_Msg_Name_1 := Chars (Source);
14364 Error_Msg_Uint_1 := Source_Siz;
14365 Error_Msg_Name_2 := Chars (Target);
14366 Error_Msg_Uint_2 := Target_Siz;
14367 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
14368
14369 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14370
14371 if Is_Discrete_Type (Source)
14372 and then
14373 Is_Discrete_Type (Target)
14374 then
14375 if Source_Siz > Target_Siz then
14376 Error_Msg
14377 ("\?z?^ high order bits of source will "
14378 & "be ignored!", Eloc);
14379
14380 elsif Is_Unsigned_Type (Source) then
14381 Error_Msg
14382 ("\?z?source will be extended with ^ high order "
14383 & "zero bits!", Eloc);
14384
14385 else
14386 Error_Msg
14387 ("\?z?source will be extended with ^ high order "
14388 & "sign bits!", Eloc);
14389 end if;
14390
14391 elsif Source_Siz < Target_Siz then
14392 if Is_Discrete_Type (Target) then
14393 if Bytes_Big_Endian then
14394 Error_Msg
14395 ("\?z?target value will include ^ undefined "
14396 & "low order bits!", Eloc);
14397 else
14398 Error_Msg
14399 ("\?z?target value will include ^ undefined "
14400 & "high order bits!", Eloc);
14401 end if;
14402
14403 else
14404 Error_Msg
14405 ("\?z?^ trailing bits of target value will be "
14406 & "undefined!", Eloc);
14407 end if;
14408
14409 else pragma Assert (Source_Siz > Target_Siz);
14410 if Is_Discrete_Type (Source) then
14411 if Bytes_Big_Endian then
14412 Error_Msg
14413 ("\?z?^ low order bits of source will be "
14414 & "ignored!", Eloc);
14415 else
14416 Error_Msg
14417 ("\?z?^ high order bits of source will be "
14418 & "ignored!", Eloc);
14419 end if;
14420
14421 else
14422 Error_Msg
14423 ("\?z?^ trailing bits of source will be "
14424 & "ignored!", Eloc);
14425 end if;
14426 end if;
14427 end if;
14428 end if;
14429 end if;
14430
14431 -- If both types are access types, we need to check the alignment.
14432 -- If the alignment of both is specified, we can do it here.
14433
14434 if Serious_Errors_Detected = 0
14435 and then Is_Access_Type (Source)
14436 and then Is_Access_Type (Target)
14437 and then Target_Strict_Alignment
14438 and then Present (Designated_Type (Source))
14439 and then Present (Designated_Type (Target))
14440 then
14441 declare
14442 D_Source : constant Entity_Id := Designated_Type (Source);
14443 D_Target : constant Entity_Id := Designated_Type (Target);
14444
14445 begin
14446 if Known_Alignment (D_Source)
14447 and then
14448 Known_Alignment (D_Target)
14449 then
14450 declare
14451 Source_Align : constant Uint := Alignment (D_Source);
14452 Target_Align : constant Uint := Alignment (D_Target);
14453
14454 begin
14455 if Source_Align < Target_Align
14456 and then not Is_Tagged_Type (D_Source)
14457
14458 -- Suppress warning if warnings suppressed on either
14459 -- type or either designated type. Note the use of
14460 -- OR here instead of OR ELSE. That is intentional,
14461 -- we would like to set flag Warnings_Off_Used in
14462 -- all types for which warnings are suppressed.
14463
14464 and then not (Has_Warnings_Off (D_Source)
14465 or
14466 Has_Warnings_Off (D_Target)
14467 or
14468 Has_Warnings_Off (Source)
14469 or
14470 Has_Warnings_Off (Target))
14471 then
14472 Error_Msg_Uint_1 := Target_Align;
14473 Error_Msg_Uint_2 := Source_Align;
14474 Error_Msg_Node_1 := D_Target;
14475 Error_Msg_Node_2 := D_Source;
14476 Error_Msg
14477 ("?z?alignment of & (^) is stricter than "
14478 & "alignment of & (^)!", Eloc);
14479 Error_Msg
14480 ("\?z?resulting access value may have invalid "
14481 & "alignment!", Eloc);
14482 end if;
14483 end;
14484 end if;
14485 end;
14486 end if;
14487 end;
14488
14489 <<Continue>>
14490 null;
14491 end loop;
14492 end Validate_Unchecked_Conversions;
14493
14494 end Sem_Ch13;