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