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