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