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