585a6d6492f996e59317fbf8aa83a4cbd16569e3
[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 return Base_Type (Typ) = Base_Type (Ent)
3136 and then No (Next_Formal (F));
3137 end Has_Good_Profile;
3138
3139 -- Start of processing for Analyze_Stream_TSS_Definition
3140
3141 begin
3142 FOnly := True;
3143
3144 if not Is_Type (U_Ent) then
3145 Error_Msg_N ("local name must be a subtype", Nam);
3146 return;
3147 end if;
3148
3149 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3150
3151 -- If Pnam is present, it can be either inherited from an ancestor
3152 -- type (in which case it is legal to redefine it for this type), or
3153 -- be a previous definition of the attribute for the same type (in
3154 -- which case it is illegal).
3155
3156 -- In the first case, it will have been analyzed already, and we
3157 -- can check that its profile does not match the expected profile
3158 -- for a stream attribute of U_Ent. In the second case, either Pnam
3159 -- has been analyzed (and has the expected profile), or it has not
3160 -- been analyzed yet (case of a type that has not been frozen yet
3161 -- and for which the stream attribute has been set using Set_TSS).
3162
3163 if Present (Pnam)
3164 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3165 then
3166 Error_Msg_Sloc := Sloc (Pnam);
3167 Error_Msg_Name_1 := Attr;
3168 Error_Msg_N ("% attribute already defined #", Nam);
3169 return;
3170 end if;
3171
3172 Analyze (Expr);
3173
3174 if Is_Entity_Name (Expr) then
3175 if not Is_Overloaded (Expr) then
3176 if Has_Good_Profile (Entity (Expr)) then
3177 Subp := Entity (Expr);
3178 end if;
3179
3180 else
3181 Get_First_Interp (Expr, I, It);
3182 while Present (It.Nam) loop
3183 if Has_Good_Profile (It.Nam) then
3184 Subp := It.Nam;
3185 exit;
3186 end if;
3187
3188 Get_Next_Interp (I, It);
3189 end loop;
3190 end if;
3191 end if;
3192
3193 if Present (Subp) then
3194 if Is_Abstract_Subprogram (Subp) then
3195 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3196 return;
3197 end if;
3198
3199 Set_Entity (Expr, Subp);
3200 Set_Etype (Expr, Etype (Subp));
3201
3202 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3203
3204 else
3205 Error_Msg_Name_1 := Attr;
3206 Error_Msg_N ("incorrect expression for% attribute", Expr);
3207 end if;
3208 end Analyze_Stream_TSS_Definition;
3209
3210 ------------------------------
3211 -- Check_Indexing_Functions --
3212 ------------------------------
3213
3214 procedure Check_Indexing_Functions is
3215 Indexing_Found : Boolean;
3216
3217 procedure Check_One_Function (Subp : Entity_Id);
3218 -- Check one possible interpretation. Sets Indexing_Found True if an
3219 -- indexing function is found.
3220
3221 ------------------------
3222 -- Check_One_Function --
3223 ------------------------
3224
3225 procedure Check_One_Function (Subp : Entity_Id) is
3226 Default_Element : constant Node_Id :=
3227 Find_Value_Of_Aspect
3228 (Etype (First_Formal (Subp)),
3229 Aspect_Iterator_Element);
3230
3231 begin
3232 if not Check_Primitive_Function (Subp)
3233 and then not Is_Overloaded (Expr)
3234 then
3235 Error_Msg_NE
3236 ("aspect Indexing requires a function that applies to type&",
3237 Subp, Ent);
3238 end if;
3239
3240 -- An indexing function must return either the default element of
3241 -- the container, or a reference type. For variable indexing it
3242 -- must be the latter.
3243
3244 if Present (Default_Element) then
3245 Analyze (Default_Element);
3246
3247 if Is_Entity_Name (Default_Element)
3248 and then Covers (Entity (Default_Element), Etype (Subp))
3249 then
3250 Indexing_Found := True;
3251 return;
3252 end if;
3253 end if;
3254
3255 -- For variable_indexing the return type must be a reference type
3256
3257 if Attr = Name_Variable_Indexing
3258 and then not Has_Implicit_Dereference (Etype (Subp))
3259 then
3260 Error_Msg_N
3261 ("function for indexing must return a reference type", Subp);
3262
3263 else
3264 Indexing_Found := True;
3265 end if;
3266 end Check_One_Function;
3267
3268 -- Start of processing for Check_Indexing_Functions
3269
3270 begin
3271 if In_Instance then
3272 return;
3273 end if;
3274
3275 Analyze (Expr);
3276
3277 if not Is_Overloaded (Expr) then
3278 Check_One_Function (Entity (Expr));
3279
3280 else
3281 declare
3282 I : Interp_Index;
3283 It : Interp;
3284
3285 begin
3286 Indexing_Found := False;
3287 Get_First_Interp (Expr, I, It);
3288 while Present (It.Nam) loop
3289
3290 -- Note that analysis will have added the interpretation
3291 -- that corresponds to the dereference. We only check the
3292 -- subprogram itself.
3293
3294 if Is_Overloadable (It.Nam) then
3295 Check_One_Function (It.Nam);
3296 end if;
3297
3298 Get_Next_Interp (I, It);
3299 end loop;
3300
3301 if not Indexing_Found then
3302 Error_Msg_NE
3303 ("aspect Indexing requires a function that "
3304 & "applies to type&", Expr, Ent);
3305 end if;
3306 end;
3307 end if;
3308 end Check_Indexing_Functions;
3309
3310 ------------------------------
3311 -- Check_Iterator_Functions --
3312 ------------------------------
3313
3314 procedure Check_Iterator_Functions is
3315 Default : Entity_Id;
3316
3317 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3318 -- Check one possible interpretation for validity
3319
3320 ----------------------------
3321 -- Valid_Default_Iterator --
3322 ----------------------------
3323
3324 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3325 Formal : Entity_Id;
3326
3327 begin
3328 if not Check_Primitive_Function (Subp) then
3329 return False;
3330 else
3331 Formal := First_Formal (Subp);
3332 end if;
3333
3334 -- False if any subsequent formal has no default expression
3335
3336 Formal := Next_Formal (Formal);
3337 while Present (Formal) loop
3338 if No (Expression (Parent (Formal))) then
3339 return False;
3340 end if;
3341
3342 Next_Formal (Formal);
3343 end loop;
3344
3345 -- True if all subsequent formals have default expressions
3346
3347 return True;
3348 end Valid_Default_Iterator;
3349
3350 -- Start of processing for Check_Iterator_Functions
3351
3352 begin
3353 Analyze (Expr);
3354
3355 if not Is_Entity_Name (Expr) then
3356 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3357 end if;
3358
3359 if not Is_Overloaded (Expr) then
3360 if not Check_Primitive_Function (Entity (Expr)) then
3361 Error_Msg_NE
3362 ("aspect Indexing requires a function that applies to type&",
3363 Entity (Expr), Ent);
3364 end if;
3365
3366 if not Valid_Default_Iterator (Entity (Expr)) then
3367 Error_Msg_N ("improper function for default iterator", Expr);
3368 end if;
3369
3370 else
3371 Default := Empty;
3372 declare
3373 I : Interp_Index;
3374 It : Interp;
3375
3376 begin
3377 Get_First_Interp (Expr, I, It);
3378 while Present (It.Nam) loop
3379 if not Check_Primitive_Function (It.Nam)
3380 or else not Valid_Default_Iterator (It.Nam)
3381 then
3382 Remove_Interp (I);
3383
3384 elsif Present (Default) then
3385 Error_Msg_N ("default iterator must be unique", Expr);
3386
3387 else
3388 Default := It.Nam;
3389 end if;
3390
3391 Get_Next_Interp (I, It);
3392 end loop;
3393 end;
3394
3395 if Present (Default) then
3396 Set_Entity (Expr, Default);
3397 Set_Is_Overloaded (Expr, False);
3398 end if;
3399 end if;
3400 end Check_Iterator_Functions;
3401
3402 -------------------------------
3403 -- Check_Primitive_Function --
3404 -------------------------------
3405
3406 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3407 Ctrl : Entity_Id;
3408
3409 begin
3410 if Ekind (Subp) /= E_Function then
3411 return False;
3412 end if;
3413
3414 if No (First_Formal (Subp)) then
3415 return False;
3416 else
3417 Ctrl := Etype (First_Formal (Subp));
3418 end if;
3419
3420 if Ctrl = Ent
3421 or else Ctrl = Class_Wide_Type (Ent)
3422 or else
3423 (Ekind (Ctrl) = E_Anonymous_Access_Type
3424 and then
3425 (Designated_Type (Ctrl) = Ent
3426 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3427 then
3428 null;
3429
3430 else
3431 return False;
3432 end if;
3433
3434 return True;
3435 end Check_Primitive_Function;
3436
3437 ----------------------
3438 -- Duplicate_Clause --
3439 ----------------------
3440
3441 function Duplicate_Clause return Boolean is
3442 A : Node_Id;
3443
3444 begin
3445 -- Nothing to do if this attribute definition clause comes from
3446 -- an aspect specification, since we could not be duplicating an
3447 -- explicit clause, and we dealt with the case of duplicated aspects
3448 -- in Analyze_Aspect_Specifications.
3449
3450 if From_Aspect_Specification (N) then
3451 return False;
3452 end if;
3453
3454 -- Otherwise current clause may duplicate previous clause, or a
3455 -- previously given pragma or aspect specification for the same
3456 -- aspect.
3457
3458 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3459
3460 if Present (A) then
3461 Error_Msg_Name_1 := Chars (N);
3462 Error_Msg_Sloc := Sloc (A);
3463
3464 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3465 return True;
3466 end if;
3467
3468 return False;
3469 end Duplicate_Clause;
3470
3471 -- Start of processing for Analyze_Attribute_Definition_Clause
3472
3473 begin
3474 -- The following code is a defense against recursion. Not clear that
3475 -- this can happen legitimately, but perhaps some error situations
3476 -- can cause it, and we did see this recursion during testing.
3477
3478 if Analyzed (N) then
3479 return;
3480 else
3481 Set_Analyzed (N, True);
3482 end if;
3483
3484 -- Ignore some selected attributes in CodePeer mode since they are not
3485 -- relevant in this context.
3486
3487 if CodePeer_Mode then
3488 case Id is
3489
3490 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3491 -- internal representation of types by implicitly packing them.
3492
3493 when Attribute_Component_Size =>
3494 Rewrite (N, Make_Null_Statement (Sloc (N)));
3495 return;
3496
3497 when others =>
3498 null;
3499 end case;
3500 end if;
3501
3502 -- Process Ignore_Rep_Clauses option
3503
3504 if Ignore_Rep_Clauses then
3505 case Id is
3506
3507 -- The following should be ignored. They do not affect legality
3508 -- and may be target dependent. The basic idea of -gnatI is to
3509 -- ignore any rep clauses that may be target dependent but do not
3510 -- affect legality (except possibly to be rejected because they
3511 -- are incompatible with the compilation target).
3512
3513 when Attribute_Alignment |
3514 Attribute_Bit_Order |
3515 Attribute_Component_Size |
3516 Attribute_Machine_Radix |
3517 Attribute_Object_Size |
3518 Attribute_Size |
3519 Attribute_Stream_Size |
3520 Attribute_Value_Size =>
3521 Rewrite (N, Make_Null_Statement (Sloc (N)));
3522 return;
3523
3524 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
3525
3526 when Attribute_Small =>
3527 if Ignore_Rep_Clauses then
3528 Rewrite (N, Make_Null_Statement (Sloc (N)));
3529 return;
3530 end if;
3531
3532 -- The following should not be ignored, because in the first place
3533 -- they are reasonably portable, and should not cause problems in
3534 -- compiling code from another target, and also they do affect
3535 -- legality, e.g. failing to provide a stream attribute for a
3536 -- type may make a program illegal.
3537
3538 when Attribute_External_Tag |
3539 Attribute_Input |
3540 Attribute_Output |
3541 Attribute_Read |
3542 Attribute_Simple_Storage_Pool |
3543 Attribute_Storage_Pool |
3544 Attribute_Storage_Size |
3545 Attribute_Write =>
3546 null;
3547
3548 -- Other cases are errors ("attribute& cannot be set with
3549 -- definition clause"), which will be caught below.
3550
3551 when others =>
3552 null;
3553 end case;
3554 end if;
3555
3556 Analyze (Nam);
3557 Ent := Entity (Nam);
3558
3559 if Rep_Item_Too_Early (Ent, N) then
3560 return;
3561 end if;
3562
3563 -- Rep clause applies to full view of incomplete type or private type if
3564 -- we have one (if not, this is a premature use of the type). However,
3565 -- certain semantic checks need to be done on the specified entity (i.e.
3566 -- the private view), so we save it in Ent.
3567
3568 if Is_Private_Type (Ent)
3569 and then Is_Derived_Type (Ent)
3570 and then not Is_Tagged_Type (Ent)
3571 and then No (Full_View (Ent))
3572 then
3573 -- If this is a private type whose completion is a derivation from
3574 -- another private type, there is no full view, and the attribute
3575 -- belongs to the type itself, not its underlying parent.
3576
3577 U_Ent := Ent;
3578
3579 elsif Ekind (Ent) = E_Incomplete_Type then
3580
3581 -- The attribute applies to the full view, set the entity of the
3582 -- attribute definition accordingly.
3583
3584 Ent := Underlying_Type (Ent);
3585 U_Ent := Ent;
3586 Set_Entity (Nam, Ent);
3587
3588 else
3589 U_Ent := Underlying_Type (Ent);
3590 end if;
3591
3592 -- Avoid cascaded error
3593
3594 if Etype (Nam) = Any_Type then
3595 return;
3596
3597 -- Must be declared in current scope or in case of an aspect
3598 -- specification, must be visible in current scope.
3599
3600 elsif Scope (Ent) /= Current_Scope
3601 and then
3602 not (From_Aspect_Specification (N)
3603 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
3604 then
3605 Error_Msg_N ("entity must be declared in this scope", Nam);
3606 return;
3607
3608 -- Must not be a source renaming (we do have some cases where the
3609 -- expander generates a renaming, and those cases are OK, in such
3610 -- cases any attribute applies to the renamed object as well).
3611
3612 elsif Is_Object (Ent)
3613 and then Present (Renamed_Object (Ent))
3614 then
3615 -- Case of renamed object from source, this is an error
3616
3617 if Comes_From_Source (Renamed_Object (Ent)) then
3618 Get_Name_String (Chars (N));
3619 Error_Msg_Strlen := Name_Len;
3620 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3621 Error_Msg_N
3622 ("~ clause not allowed for a renaming declaration "
3623 & "(RM 13.1(6))", Nam);
3624 return;
3625
3626 -- For the case of a compiler generated renaming, the attribute
3627 -- definition clause applies to the renamed object created by the
3628 -- expander. The easiest general way to handle this is to create a
3629 -- copy of the attribute definition clause for this object.
3630
3631 elsif Is_Entity_Name (Renamed_Object (Ent)) then
3632 Insert_Action (N,
3633 Make_Attribute_Definition_Clause (Loc,
3634 Name =>
3635 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
3636 Chars => Chars (N),
3637 Expression => Duplicate_Subexpr (Expression (N))));
3638
3639 -- If the renamed object is not an entity, it must be a dereference
3640 -- of an unconstrained function call, and we must introduce a new
3641 -- declaration to capture the expression. This is needed in the case
3642 -- of 'Alignment, where the original declaration must be rewritten.
3643
3644 else
3645 pragma Assert
3646 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
3647 null;
3648 end if;
3649
3650 -- If no underlying entity, use entity itself, applies to some
3651 -- previously detected error cases ???
3652
3653 elsif No (U_Ent) then
3654 U_Ent := Ent;
3655
3656 -- Cannot specify for a subtype (exception Object/Value_Size)
3657
3658 elsif Is_Type (U_Ent)
3659 and then not Is_First_Subtype (U_Ent)
3660 and then Id /= Attribute_Object_Size
3661 and then Id /= Attribute_Value_Size
3662 and then not From_At_Mod (N)
3663 then
3664 Error_Msg_N ("cannot specify attribute for subtype", Nam);
3665 return;
3666 end if;
3667
3668 Set_Entity (N, U_Ent);
3669 Check_Restriction_No_Use_Of_Attribute (N);
3670
3671 -- Switch on particular attribute
3672
3673 case Id is
3674
3675 -------------
3676 -- Address --
3677 -------------
3678
3679 -- Address attribute definition clause
3680
3681 when Attribute_Address => Address : begin
3682
3683 -- A little error check, catch for X'Address use X'Address;
3684
3685 if Nkind (Nam) = N_Identifier
3686 and then Nkind (Expr) = N_Attribute_Reference
3687 and then Attribute_Name (Expr) = Name_Address
3688 and then Nkind (Prefix (Expr)) = N_Identifier
3689 and then Chars (Nam) = Chars (Prefix (Expr))
3690 then
3691 Error_Msg_NE
3692 ("address for & is self-referencing", Prefix (Expr), Ent);
3693 return;
3694 end if;
3695
3696 -- Not that special case, carry on with analysis of expression
3697
3698 Analyze_And_Resolve (Expr, RTE (RE_Address));
3699
3700 -- Even when ignoring rep clauses we need to indicate that the
3701 -- entity has an address clause and thus it is legal to declare
3702 -- it imported.
3703
3704 if Ignore_Rep_Clauses then
3705 if Ekind_In (U_Ent, E_Variable, E_Constant) then
3706 Record_Rep_Item (U_Ent, N);
3707 end if;
3708
3709 return;
3710 end if;
3711
3712 if Duplicate_Clause then
3713 null;
3714
3715 -- Case of address clause for subprogram
3716
3717 elsif Is_Subprogram (U_Ent) then
3718 if Has_Homonym (U_Ent) then
3719 Error_Msg_N
3720 ("address clause cannot be given " &
3721 "for overloaded subprogram",
3722 Nam);
3723 return;
3724 end if;
3725
3726 -- For subprograms, all address clauses are permitted, and we
3727 -- mark the subprogram as having a deferred freeze so that Gigi
3728 -- will not elaborate it too soon.
3729
3730 -- Above needs more comments, what is too soon about???
3731
3732 Set_Has_Delayed_Freeze (U_Ent);
3733
3734 -- Case of address clause for entry
3735
3736 elsif Ekind (U_Ent) = E_Entry then
3737 if Nkind (Parent (N)) = N_Task_Body then
3738 Error_Msg_N
3739 ("entry address must be specified in task spec", Nam);
3740 return;
3741 end if;
3742
3743 -- For entries, we require a constant address
3744
3745 Check_Constant_Address_Clause (Expr, U_Ent);
3746
3747 -- Special checks for task types
3748
3749 if Is_Task_Type (Scope (U_Ent))
3750 and then Comes_From_Source (Scope (U_Ent))
3751 then
3752 Error_Msg_N
3753 ("??entry address declared for entry in task type", N);
3754 Error_Msg_N
3755 ("\??only one task can be declared of this type", N);
3756 end if;
3757
3758 -- Entry address clauses are obsolescent
3759
3760 Check_Restriction (No_Obsolescent_Features, N);
3761
3762 if Warn_On_Obsolescent_Feature then
3763 Error_Msg_N
3764 ("?j?attaching interrupt to task entry is an " &
3765 "obsolescent feature (RM J.7.1)", N);
3766 Error_Msg_N
3767 ("\?j?use interrupt procedure instead", N);
3768 end if;
3769
3770 -- Case of an address clause for a controlled object which we
3771 -- consider to be erroneous.
3772
3773 elsif Is_Controlled (Etype (U_Ent))
3774 or else Has_Controlled_Component (Etype (U_Ent))
3775 then
3776 Error_Msg_NE
3777 ("??controlled object& must not be overlaid", Nam, U_Ent);
3778 Error_Msg_N
3779 ("\??Program_Error will be raised at run time", Nam);
3780 Insert_Action (Declaration_Node (U_Ent),
3781 Make_Raise_Program_Error (Loc,
3782 Reason => PE_Overlaid_Controlled_Object));
3783 return;
3784
3785 -- Case of address clause for a (non-controlled) object
3786
3787 elsif
3788 Ekind (U_Ent) = E_Variable
3789 or else
3790 Ekind (U_Ent) = E_Constant
3791 then
3792 declare
3793 Expr : constant Node_Id := Expression (N);
3794 O_Ent : Entity_Id;
3795 Off : Boolean;
3796
3797 begin
3798 -- Exported variables cannot have an address clause, because
3799 -- this cancels the effect of the pragma Export.
3800
3801 if Is_Exported (U_Ent) then
3802 Error_Msg_N
3803 ("cannot export object with address clause", Nam);
3804 return;
3805 end if;
3806
3807 Find_Overlaid_Entity (N, O_Ent, Off);
3808
3809 -- Overlaying controlled objects is erroneous
3810
3811 if Present (O_Ent)
3812 and then (Has_Controlled_Component (Etype (O_Ent))
3813 or else Is_Controlled (Etype (O_Ent)))
3814 then
3815 Error_Msg_N
3816 ("??cannot overlay with controlled object", Expr);
3817 Error_Msg_N
3818 ("\??Program_Error will be raised at run time", Expr);
3819 Insert_Action (Declaration_Node (U_Ent),
3820 Make_Raise_Program_Error (Loc,
3821 Reason => PE_Overlaid_Controlled_Object));
3822 return;
3823
3824 elsif Present (O_Ent)
3825 and then Ekind (U_Ent) = E_Constant
3826 and then not Is_Constant_Object (O_Ent)
3827 then
3828 Error_Msg_N ("??constant overlays a variable", Expr);
3829
3830 -- Imported variables can have an address clause, but then
3831 -- the import is pretty meaningless except to suppress
3832 -- initializations, so we do not need such variables to
3833 -- be statically allocated (and in fact it causes trouble
3834 -- if the address clause is a local value).
3835
3836 elsif Is_Imported (U_Ent) then
3837 Set_Is_Statically_Allocated (U_Ent, False);
3838 end if;
3839
3840 -- We mark a possible modification of a variable with an
3841 -- address clause, since it is likely aliasing is occurring.
3842
3843 Note_Possible_Modification (Nam, Sure => False);
3844
3845 -- Here we are checking for explicit overlap of one variable
3846 -- by another, and if we find this then mark the overlapped
3847 -- variable as also being volatile to prevent unwanted
3848 -- optimizations. This is a significant pessimization so
3849 -- avoid it when there is an offset, i.e. when the object
3850 -- is composite; they cannot be optimized easily anyway.
3851
3852 if Present (O_Ent)
3853 and then Is_Object (O_Ent)
3854 and then not Off
3855
3856 -- The following test is an expedient solution to what
3857 -- is really a problem in CodePeer. Suppressing the
3858 -- Set_Treat_As_Volatile call here prevents later
3859 -- generation (in some cases) of trees that CodePeer
3860 -- should, but currently does not, handle correctly.
3861 -- This test should probably be removed when CodePeer
3862 -- is improved, just because we want the tree CodePeer
3863 -- analyzes to match the tree for which we generate code
3864 -- as closely as is practical. ???
3865
3866 and then not CodePeer_Mode
3867 then
3868 -- ??? O_Ent might not be in current unit
3869
3870 Set_Treat_As_Volatile (O_Ent);
3871 end if;
3872
3873 -- Legality checks on the address clause for initialized
3874 -- objects is deferred until the freeze point, because
3875 -- a subsequent pragma might indicate that the object
3876 -- is imported and thus not initialized. Also, the address
3877 -- clause might involve entities that have yet to be
3878 -- elaborated.
3879
3880 Set_Has_Delayed_Freeze (U_Ent);
3881
3882 -- If an initialization call has been generated for this
3883 -- object, it needs to be deferred to after the freeze node
3884 -- we have just now added, otherwise GIGI will see a
3885 -- reference to the variable (as actual to the IP call)
3886 -- before its definition.
3887
3888 declare
3889 Init_Call : constant Node_Id :=
3890 Remove_Init_Call (U_Ent, N);
3891
3892 begin
3893 if Present (Init_Call) then
3894 Append_Freeze_Action (U_Ent, Init_Call);
3895
3896 -- Reset Initialization_Statements pointer so that
3897 -- if there is a pragma Import further down, it can
3898 -- clear any default initialization.
3899
3900 Set_Initialization_Statements (U_Ent, Init_Call);
3901 end if;
3902 end;
3903
3904 if Is_Exported (U_Ent) then
3905 Error_Msg_N
3906 ("& cannot be exported if an address clause is given",
3907 Nam);
3908 Error_Msg_N
3909 ("\define and export a variable "
3910 & "that holds its address instead", Nam);
3911 end if;
3912
3913 -- Entity has delayed freeze, so we will generate an
3914 -- alignment check at the freeze point unless suppressed.
3915
3916 if not Range_Checks_Suppressed (U_Ent)
3917 and then not Alignment_Checks_Suppressed (U_Ent)
3918 then
3919 Set_Check_Address_Alignment (N);
3920 end if;
3921
3922 -- Kill the size check code, since we are not allocating
3923 -- the variable, it is somewhere else.
3924
3925 Kill_Size_Check_Code (U_Ent);
3926
3927 -- If the address clause is of the form:
3928
3929 -- for Y'Address use X'Address
3930
3931 -- or
3932
3933 -- Const : constant Address := X'Address;
3934 -- ...
3935 -- for Y'Address use Const;
3936
3937 -- then we make an entry in the table for checking the size
3938 -- and alignment of the overlaying variable. We defer this
3939 -- check till after code generation to take full advantage
3940 -- of the annotation done by the back end.
3941
3942 -- If the entity has a generic type, the check will be
3943 -- performed in the instance if the actual type justifies
3944 -- it, and we do not insert the clause in the table to
3945 -- prevent spurious warnings.
3946
3947 -- Note: we used to test Comes_From_Source and only give
3948 -- this warning for source entities, but we have removed
3949 -- this test. It really seems bogus to generate overlays
3950 -- that would trigger this warning in generated code.
3951 -- Furthermore, by removing the test, we handle the
3952 -- aspect case properly.
3953
3954 if Address_Clause_Overlay_Warnings
3955 and then Present (O_Ent)
3956 and then Is_Object (O_Ent)
3957 then
3958 if not Is_Generic_Type (Etype (U_Ent)) then
3959 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3960 end if;
3961
3962 -- If variable overlays a constant view, and we are
3963 -- warning on overlays, then mark the variable as
3964 -- overlaying a constant (we will give warnings later
3965 -- if this variable is assigned).
3966
3967 if Is_Constant_Object (O_Ent)
3968 and then Ekind (U_Ent) = E_Variable
3969 then
3970 Set_Overlays_Constant (U_Ent);
3971 end if;
3972 end if;
3973 end;
3974
3975 -- Not a valid entity for an address clause
3976
3977 else
3978 Error_Msg_N ("address cannot be given for &", Nam);
3979 end if;
3980 end Address;
3981
3982 ---------------
3983 -- Alignment --
3984 ---------------
3985
3986 -- Alignment attribute definition clause
3987
3988 when Attribute_Alignment => Alignment : declare
3989 Align : constant Uint := Get_Alignment_Value (Expr);
3990 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3991
3992 begin
3993 FOnly := True;
3994
3995 if not Is_Type (U_Ent)
3996 and then Ekind (U_Ent) /= E_Variable
3997 and then Ekind (U_Ent) /= E_Constant
3998 then
3999 Error_Msg_N ("alignment cannot be given for &", Nam);
4000
4001 elsif Duplicate_Clause then
4002 null;
4003
4004 elsif Align /= No_Uint then
4005 Set_Has_Alignment_Clause (U_Ent);
4006
4007 -- Tagged type case, check for attempt to set alignment to a
4008 -- value greater than Max_Align, and reset if so.
4009
4010 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4011 Error_Msg_N
4012 ("alignment for & set to Maximum_Aligment??", Nam);
4013 Set_Alignment (U_Ent, Max_Align);
4014
4015 -- All other cases
4016
4017 else
4018 Set_Alignment (U_Ent, Align);
4019 end if;
4020
4021 -- For an array type, U_Ent is the first subtype. In that case,
4022 -- also set the alignment of the anonymous base type so that
4023 -- other subtypes (such as the itypes for aggregates of the
4024 -- type) also receive the expected alignment.
4025
4026 if Is_Array_Type (U_Ent) then
4027 Set_Alignment (Base_Type (U_Ent), Align);
4028 end if;
4029 end if;
4030 end Alignment;
4031
4032 ---------------
4033 -- Bit_Order --
4034 ---------------
4035
4036 -- Bit_Order attribute definition clause
4037
4038 when Attribute_Bit_Order => Bit_Order : declare
4039 begin
4040 if not Is_Record_Type (U_Ent) then
4041 Error_Msg_N
4042 ("Bit_Order can only be defined for record type", Nam);
4043
4044 elsif Duplicate_Clause then
4045 null;
4046
4047 else
4048 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4049
4050 if Etype (Expr) = Any_Type then
4051 return;
4052
4053 elsif not Is_Static_Expression (Expr) then
4054 Flag_Non_Static_Expr
4055 ("Bit_Order requires static expression!", Expr);
4056
4057 else
4058 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4059 Set_Reverse_Bit_Order (U_Ent, True);
4060 end if;
4061 end if;
4062 end if;
4063 end Bit_Order;
4064
4065 --------------------
4066 -- Component_Size --
4067 --------------------
4068
4069 -- Component_Size attribute definition clause
4070
4071 when Attribute_Component_Size => Component_Size_Case : declare
4072 Csize : constant Uint := Static_Integer (Expr);
4073 Ctyp : Entity_Id;
4074 Btype : Entity_Id;
4075 Biased : Boolean;
4076 New_Ctyp : Entity_Id;
4077 Decl : Node_Id;
4078
4079 begin
4080 if not Is_Array_Type (U_Ent) then
4081 Error_Msg_N ("component size requires array type", Nam);
4082 return;
4083 end if;
4084
4085 Btype := Base_Type (U_Ent);
4086 Ctyp := Component_Type (Btype);
4087
4088 if Duplicate_Clause then
4089 null;
4090
4091 elsif Rep_Item_Too_Early (Btype, N) then
4092 null;
4093
4094 elsif Csize /= No_Uint then
4095 Check_Size (Expr, Ctyp, Csize, Biased);
4096
4097 -- For the biased case, build a declaration for a subtype that
4098 -- will be used to represent the biased subtype that reflects
4099 -- the biased representation of components. We need the subtype
4100 -- to get proper conversions on referencing elements of the
4101 -- array. Note: component size clauses are ignored in VM mode.
4102
4103 if VM_Target = No_VM then
4104 if Biased then
4105 New_Ctyp :=
4106 Make_Defining_Identifier (Loc,
4107 Chars =>
4108 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4109
4110 Decl :=
4111 Make_Subtype_Declaration (Loc,
4112 Defining_Identifier => New_Ctyp,
4113 Subtype_Indication =>
4114 New_Occurrence_Of (Component_Type (Btype), Loc));
4115
4116 Set_Parent (Decl, N);
4117 Analyze (Decl, Suppress => All_Checks);
4118
4119 Set_Has_Delayed_Freeze (New_Ctyp, False);
4120 Set_Esize (New_Ctyp, Csize);
4121 Set_RM_Size (New_Ctyp, Csize);
4122 Init_Alignment (New_Ctyp);
4123 Set_Is_Itype (New_Ctyp, True);
4124 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4125
4126 Set_Component_Type (Btype, New_Ctyp);
4127 Set_Biased (New_Ctyp, N, "component size clause");
4128 end if;
4129
4130 Set_Component_Size (Btype, Csize);
4131
4132 -- For VM case, we ignore component size clauses
4133
4134 else
4135 -- Give a warning unless we are in GNAT mode, in which case
4136 -- the warning is suppressed since it is not useful.
4137
4138 if not GNAT_Mode then
4139 Error_Msg_N
4140 ("component size ignored in this configuration??", N);
4141 end if;
4142 end if;
4143
4144 -- Deal with warning on overridden size
4145
4146 if Warn_On_Overridden_Size
4147 and then Has_Size_Clause (Ctyp)
4148 and then RM_Size (Ctyp) /= Csize
4149 then
4150 Error_Msg_NE
4151 ("component size overrides size clause for&?S?", N, Ctyp);
4152 end if;
4153
4154 Set_Has_Component_Size_Clause (Btype, True);
4155 Set_Has_Non_Standard_Rep (Btype, True);
4156 end if;
4157 end Component_Size_Case;
4158
4159 -----------------------
4160 -- Constant_Indexing --
4161 -----------------------
4162
4163 when Attribute_Constant_Indexing =>
4164 Check_Indexing_Functions;
4165
4166 ---------
4167 -- CPU --
4168 ---------
4169
4170 when Attribute_CPU => CPU :
4171 begin
4172 -- CPU attribute definition clause not allowed except from aspect
4173 -- specification.
4174
4175 if From_Aspect_Specification (N) then
4176 if not Is_Task_Type (U_Ent) then
4177 Error_Msg_N ("CPU can only be defined for task", Nam);
4178
4179 elsif Duplicate_Clause then
4180 null;
4181
4182 else
4183 -- The expression must be analyzed in the special manner
4184 -- described in "Handling of Default and Per-Object
4185 -- Expressions" in sem.ads.
4186
4187 -- The visibility to the discriminants must be restored
4188
4189 Push_Scope_And_Install_Discriminants (U_Ent);
4190 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4191 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4192
4193 if not Is_Static_Expression (Expr) then
4194 Check_Restriction (Static_Priorities, Expr);
4195 end if;
4196 end if;
4197
4198 else
4199 Error_Msg_N
4200 ("attribute& cannot be set with definition clause", N);
4201 end if;
4202 end CPU;
4203
4204 ----------------------
4205 -- Default_Iterator --
4206 ----------------------
4207
4208 when Attribute_Default_Iterator => Default_Iterator : declare
4209 Func : Entity_Id;
4210
4211 begin
4212 if not Is_Tagged_Type (U_Ent) then
4213 Error_Msg_N
4214 ("aspect Default_Iterator applies to tagged type", Nam);
4215 end if;
4216
4217 Check_Iterator_Functions;
4218
4219 Analyze (Expr);
4220
4221 if not Is_Entity_Name (Expr)
4222 or else Ekind (Entity (Expr)) /= E_Function
4223 then
4224 Error_Msg_N ("aspect Iterator must be a function", Expr);
4225 else
4226 Func := Entity (Expr);
4227 end if;
4228
4229 if No (First_Formal (Func))
4230 or else Etype (First_Formal (Func)) /= U_Ent
4231 then
4232 Error_Msg_NE
4233 ("Default Iterator must be a primitive of&", Func, U_Ent);
4234 end if;
4235 end Default_Iterator;
4236
4237 ------------------------
4238 -- Dispatching_Domain --
4239 ------------------------
4240
4241 when Attribute_Dispatching_Domain => Dispatching_Domain :
4242 begin
4243 -- Dispatching_Domain attribute definition clause not allowed
4244 -- except from aspect specification.
4245
4246 if From_Aspect_Specification (N) then
4247 if not Is_Task_Type (U_Ent) then
4248 Error_Msg_N ("Dispatching_Domain can only be defined" &
4249 "for task",
4250 Nam);
4251
4252 elsif Duplicate_Clause then
4253 null;
4254
4255 else
4256 -- The expression must be analyzed in the special manner
4257 -- described in "Handling of Default and Per-Object
4258 -- Expressions" in sem.ads.
4259
4260 -- The visibility to the discriminants must be restored
4261
4262 Push_Scope_And_Install_Discriminants (U_Ent);
4263
4264 Preanalyze_Spec_Expression
4265 (Expr, RTE (RE_Dispatching_Domain));
4266
4267 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4268 end if;
4269
4270 else
4271 Error_Msg_N
4272 ("attribute& cannot be set with definition clause", N);
4273 end if;
4274 end Dispatching_Domain;
4275
4276 ------------------
4277 -- External_Tag --
4278 ------------------
4279
4280 when Attribute_External_Tag => External_Tag :
4281 begin
4282 if not Is_Tagged_Type (U_Ent) then
4283 Error_Msg_N ("should be a tagged type", Nam);
4284 end if;
4285
4286 if Duplicate_Clause then
4287 null;
4288
4289 else
4290 Analyze_And_Resolve (Expr, Standard_String);
4291
4292 if not Is_Static_Expression (Expr) then
4293 Flag_Non_Static_Expr
4294 ("static string required for tag name!", Nam);
4295 end if;
4296
4297 if VM_Target = No_VM then
4298 Set_Has_External_Tag_Rep_Clause (U_Ent);
4299 else
4300 Error_Msg_Name_1 := Attr;
4301 Error_Msg_N
4302 ("% attribute unsupported in this configuration", Nam);
4303 end if;
4304
4305 if not Is_Library_Level_Entity (U_Ent) then
4306 Error_Msg_NE
4307 ("??non-unique external tag supplied for &", N, U_Ent);
4308 Error_Msg_N
4309 ("\??same external tag applies to all "
4310 & "subprogram calls", N);
4311 Error_Msg_N
4312 ("\??corresponding internal tag cannot be obtained", N);
4313 end if;
4314 end if;
4315 end External_Tag;
4316
4317 --------------------------
4318 -- Implicit_Dereference --
4319 --------------------------
4320
4321 when Attribute_Implicit_Dereference =>
4322
4323 -- Legality checks already performed at the point of the type
4324 -- declaration, aspect is not delayed.
4325
4326 null;
4327
4328 -----------
4329 -- Input --
4330 -----------
4331
4332 when Attribute_Input =>
4333 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4334 Set_Has_Specified_Stream_Input (Ent);
4335
4336 ------------------------
4337 -- Interrupt_Priority --
4338 ------------------------
4339
4340 when Attribute_Interrupt_Priority => Interrupt_Priority :
4341 begin
4342 -- Interrupt_Priority attribute definition clause not allowed
4343 -- except from aspect specification.
4344
4345 if From_Aspect_Specification (N) then
4346 if not (Is_Protected_Type (U_Ent)
4347 or else Is_Task_Type (U_Ent))
4348 then
4349 Error_Msg_N
4350 ("Interrupt_Priority can only be defined for task" &
4351 "and protected object",
4352 Nam);
4353
4354 elsif Duplicate_Clause then
4355 null;
4356
4357 else
4358 -- The expression must be analyzed in the special manner
4359 -- described in "Handling of Default and Per-Object
4360 -- Expressions" in sem.ads.
4361
4362 -- The visibility to the discriminants must be restored
4363
4364 Push_Scope_And_Install_Discriminants (U_Ent);
4365
4366 Preanalyze_Spec_Expression
4367 (Expr, RTE (RE_Interrupt_Priority));
4368
4369 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4370 end if;
4371
4372 else
4373 Error_Msg_N
4374 ("attribute& cannot be set with definition clause", N);
4375 end if;
4376 end Interrupt_Priority;
4377
4378 --------------
4379 -- Iterable --
4380 --------------
4381
4382 when Attribute_Iterable =>
4383 Analyze (Expr);
4384
4385 if Nkind (Expr) /= N_Aggregate then
4386 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4387 end if;
4388
4389 declare
4390 Assoc : Node_Id;
4391
4392 begin
4393 Assoc := First (Component_Associations (Expr));
4394 while Present (Assoc) loop
4395 if not Is_Entity_Name (Expression (Assoc)) then
4396 Error_Msg_N ("value must be a function", Assoc);
4397 end if;
4398
4399 Next (Assoc);
4400 end loop;
4401 end;
4402
4403 ----------------------
4404 -- Iterator_Element --
4405 ----------------------
4406
4407 when Attribute_Iterator_Element =>
4408 Analyze (Expr);
4409
4410 if not Is_Entity_Name (Expr)
4411 or else not Is_Type (Entity (Expr))
4412 then
4413 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4414 end if;
4415
4416 -------------------
4417 -- Machine_Radix --
4418 -------------------
4419
4420 -- Machine radix attribute definition clause
4421
4422 when Attribute_Machine_Radix => Machine_Radix : declare
4423 Radix : constant Uint := Static_Integer (Expr);
4424
4425 begin
4426 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4427 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4428
4429 elsif Duplicate_Clause then
4430 null;
4431
4432 elsif Radix /= No_Uint then
4433 Set_Has_Machine_Radix_Clause (U_Ent);
4434 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4435
4436 if Radix = 2 then
4437 null;
4438 elsif Radix = 10 then
4439 Set_Machine_Radix_10 (U_Ent);
4440 else
4441 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4442 end if;
4443 end if;
4444 end Machine_Radix;
4445
4446 -----------------
4447 -- Object_Size --
4448 -----------------
4449
4450 -- Object_Size attribute definition clause
4451
4452 when Attribute_Object_Size => Object_Size : declare
4453 Size : constant Uint := Static_Integer (Expr);
4454
4455 Biased : Boolean;
4456 pragma Warnings (Off, Biased);
4457
4458 begin
4459 if not Is_Type (U_Ent) then
4460 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4461
4462 elsif Duplicate_Clause then
4463 null;
4464
4465 else
4466 Check_Size (Expr, U_Ent, Size, Biased);
4467
4468 if Is_Scalar_Type (U_Ent) then
4469 if Size /= 8 and then Size /= 16 and then Size /= 32
4470 and then UI_Mod (Size, 64) /= 0
4471 then
4472 Error_Msg_N
4473 ("Object_Size must be 8, 16, 32, or multiple of 64",
4474 Expr);
4475 end if;
4476
4477 elsif Size mod 8 /= 0 then
4478 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
4479 end if;
4480
4481 Set_Esize (U_Ent, Size);
4482 Set_Has_Object_Size_Clause (U_Ent);
4483 Alignment_Check_For_Size_Change (U_Ent, Size);
4484 end if;
4485 end Object_Size;
4486
4487 ------------
4488 -- Output --
4489 ------------
4490
4491 when Attribute_Output =>
4492 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4493 Set_Has_Specified_Stream_Output (Ent);
4494
4495 --------------
4496 -- Priority --
4497 --------------
4498
4499 when Attribute_Priority => Priority :
4500 begin
4501 -- Priority attribute definition clause not allowed except from
4502 -- aspect specification.
4503
4504 if From_Aspect_Specification (N) then
4505 if not (Is_Protected_Type (U_Ent)
4506 or else Is_Task_Type (U_Ent)
4507 or else Ekind (U_Ent) = E_Procedure)
4508 then
4509 Error_Msg_N
4510 ("Priority can only be defined for task and protected " &
4511 "object",
4512 Nam);
4513
4514 elsif Duplicate_Clause then
4515 null;
4516
4517 else
4518 -- The expression must be analyzed in the special manner
4519 -- described in "Handling of Default and Per-Object
4520 -- Expressions" in sem.ads.
4521
4522 -- The visibility to the discriminants must be restored
4523
4524 Push_Scope_And_Install_Discriminants (U_Ent);
4525 Preanalyze_Spec_Expression (Expr, Standard_Integer);
4526 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4527
4528 if not Is_Static_Expression (Expr) then
4529 Check_Restriction (Static_Priorities, Expr);
4530 end if;
4531 end if;
4532
4533 else
4534 Error_Msg_N
4535 ("attribute& cannot be set with definition clause", N);
4536 end if;
4537 end Priority;
4538
4539 ----------
4540 -- Read --
4541 ----------
4542
4543 when Attribute_Read =>
4544 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
4545 Set_Has_Specified_Stream_Read (Ent);
4546
4547 --------------------------
4548 -- Scalar_Storage_Order --
4549 --------------------------
4550
4551 -- Scalar_Storage_Order attribute definition clause
4552
4553 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
4554 begin
4555 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
4556 Error_Msg_N
4557 ("Scalar_Storage_Order can only be defined for "
4558 & "record or array type", Nam);
4559
4560 elsif Duplicate_Clause then
4561 null;
4562
4563 else
4564 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4565
4566 if Etype (Expr) = Any_Type then
4567 return;
4568
4569 elsif not Is_Static_Expression (Expr) then
4570 Flag_Non_Static_Expr
4571 ("Scalar_Storage_Order requires static expression!", Expr);
4572
4573 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4574
4575 -- Here for the case of a non-default (i.e. non-confirming)
4576 -- Scalar_Storage_Order attribute definition.
4577
4578 if Support_Nondefault_SSO_On_Target then
4579 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
4580 else
4581 Error_Msg_N
4582 ("non-default Scalar_Storage_Order "
4583 & "not supported on target", Expr);
4584 end if;
4585 end if;
4586 end if;
4587 end Scalar_Storage_Order;
4588
4589 ----------
4590 -- Size --
4591 ----------
4592
4593 -- Size attribute definition clause
4594
4595 when Attribute_Size => Size : declare
4596 Size : constant Uint := Static_Integer (Expr);
4597 Etyp : Entity_Id;
4598 Biased : Boolean;
4599
4600 begin
4601 FOnly := True;
4602
4603 if Duplicate_Clause then
4604 null;
4605
4606 elsif not Is_Type (U_Ent)
4607 and then Ekind (U_Ent) /= E_Variable
4608 and then Ekind (U_Ent) /= E_Constant
4609 then
4610 Error_Msg_N ("size cannot be given for &", Nam);
4611
4612 elsif Is_Array_Type (U_Ent)
4613 and then not Is_Constrained (U_Ent)
4614 then
4615 Error_Msg_N
4616 ("size cannot be given for unconstrained array", Nam);
4617
4618 elsif Size /= No_Uint then
4619 if VM_Target /= No_VM and then not GNAT_Mode then
4620
4621 -- Size clause is not handled properly on VM targets.
4622 -- Display a warning unless we are in GNAT mode, in which
4623 -- case this is useless.
4624
4625 Error_Msg_N
4626 ("size clauses are ignored in this configuration??", N);
4627 end if;
4628
4629 if Is_Type (U_Ent) then
4630 Etyp := U_Ent;
4631 else
4632 Etyp := Etype (U_Ent);
4633 end if;
4634
4635 -- Check size, note that Gigi is in charge of checking that the
4636 -- size of an array or record type is OK. Also we do not check
4637 -- the size in the ordinary fixed-point case, since it is too
4638 -- early to do so (there may be subsequent small clause that
4639 -- affects the size). We can check the size if a small clause
4640 -- has already been given.
4641
4642 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
4643 or else Has_Small_Clause (U_Ent)
4644 then
4645 Check_Size (Expr, Etyp, Size, Biased);
4646 Set_Biased (U_Ent, N, "size clause", Biased);
4647 end if;
4648
4649 -- For types set RM_Size and Esize if possible
4650
4651 if Is_Type (U_Ent) then
4652 Set_RM_Size (U_Ent, Size);
4653
4654 -- For elementary types, increase Object_Size to power of 2,
4655 -- but not less than a storage unit in any case (normally
4656 -- this means it will be byte addressable).
4657
4658 -- For all other types, nothing else to do, we leave Esize
4659 -- (object size) unset, the back end will set it from the
4660 -- size and alignment in an appropriate manner.
4661
4662 -- In both cases, we check whether the alignment must be
4663 -- reset in the wake of the size change.
4664
4665 if Is_Elementary_Type (U_Ent) then
4666 if Size <= System_Storage_Unit then
4667 Init_Esize (U_Ent, System_Storage_Unit);
4668 elsif Size <= 16 then
4669 Init_Esize (U_Ent, 16);
4670 elsif Size <= 32 then
4671 Init_Esize (U_Ent, 32);
4672 else
4673 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
4674 end if;
4675
4676 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
4677 else
4678 Alignment_Check_For_Size_Change (U_Ent, Size);
4679 end if;
4680
4681 -- For objects, set Esize only
4682
4683 else
4684 if Is_Elementary_Type (Etyp) then
4685 if Size /= System_Storage_Unit
4686 and then
4687 Size /= System_Storage_Unit * 2
4688 and then
4689 Size /= System_Storage_Unit * 4
4690 and then
4691 Size /= System_Storage_Unit * 8
4692 then
4693 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4694 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
4695 Error_Msg_N
4696 ("size for primitive object must be a power of 2"
4697 & " in the range ^-^", N);
4698 end if;
4699 end if;
4700
4701 Set_Esize (U_Ent, Size);
4702 end if;
4703
4704 Set_Has_Size_Clause (U_Ent);
4705 end if;
4706 end Size;
4707
4708 -----------
4709 -- Small --
4710 -----------
4711
4712 -- Small attribute definition clause
4713
4714 when Attribute_Small => Small : declare
4715 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
4716 Small : Ureal;
4717
4718 begin
4719 Analyze_And_Resolve (Expr, Any_Real);
4720
4721 if Etype (Expr) = Any_Type then
4722 return;
4723
4724 elsif not Is_Static_Expression (Expr) then
4725 Flag_Non_Static_Expr
4726 ("small requires static expression!", Expr);
4727 return;
4728
4729 else
4730 Small := Expr_Value_R (Expr);
4731
4732 if Small <= Ureal_0 then
4733 Error_Msg_N ("small value must be greater than zero", Expr);
4734 return;
4735 end if;
4736
4737 end if;
4738
4739 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
4740 Error_Msg_N
4741 ("small requires an ordinary fixed point type", Nam);
4742
4743 elsif Has_Small_Clause (U_Ent) then
4744 Error_Msg_N ("small already given for &", Nam);
4745
4746 elsif Small > Delta_Value (U_Ent) then
4747 Error_Msg_N
4748 ("small value must not be greater than delta value", Nam);
4749
4750 else
4751 Set_Small_Value (U_Ent, Small);
4752 Set_Small_Value (Implicit_Base, Small);
4753 Set_Has_Small_Clause (U_Ent);
4754 Set_Has_Small_Clause (Implicit_Base);
4755 Set_Has_Non_Standard_Rep (Implicit_Base);
4756 end if;
4757 end Small;
4758
4759 ------------------
4760 -- Storage_Pool --
4761 ------------------
4762
4763 -- Storage_Pool attribute definition clause
4764
4765 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
4766 Pool : Entity_Id;
4767 T : Entity_Id;
4768
4769 begin
4770 if Ekind (U_Ent) = E_Access_Subprogram_Type then
4771 Error_Msg_N
4772 ("storage pool cannot be given for access-to-subprogram type",
4773 Nam);
4774 return;
4775
4776 elsif not
4777 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
4778 then
4779 Error_Msg_N
4780 ("storage pool can only be given for access types", Nam);
4781 return;
4782
4783 elsif Is_Derived_Type (U_Ent) then
4784 Error_Msg_N
4785 ("storage pool cannot be given for a derived access type",
4786 Nam);
4787
4788 elsif Duplicate_Clause then
4789 return;
4790
4791 elsif Present (Associated_Storage_Pool (U_Ent)) then
4792 Error_Msg_N ("storage pool already given for &", Nam);
4793 return;
4794 end if;
4795
4796 -- Check for Storage_Size previously given
4797
4798 declare
4799 SS : constant Node_Id :=
4800 Get_Attribute_Definition_Clause
4801 (U_Ent, Attribute_Storage_Size);
4802 begin
4803 if Present (SS) then
4804 Check_Pool_Size_Clash (U_Ent, N, SS);
4805 end if;
4806 end;
4807
4808 -- Storage_Pool case
4809
4810 if Id = Attribute_Storage_Pool then
4811 Analyze_And_Resolve
4812 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4813
4814 -- In the Simple_Storage_Pool case, we allow a variable of any
4815 -- simple storage pool type, so we Resolve without imposing an
4816 -- expected type.
4817
4818 else
4819 Analyze_And_Resolve (Expr);
4820
4821 if not Present (Get_Rep_Pragma
4822 (Etype (Expr), Name_Simple_Storage_Pool_Type))
4823 then
4824 Error_Msg_N
4825 ("expression must be of a simple storage pool type", Expr);
4826 end if;
4827 end if;
4828
4829 if not Denotes_Variable (Expr) then
4830 Error_Msg_N ("storage pool must be a variable", Expr);
4831 return;
4832 end if;
4833
4834 if Nkind (Expr) = N_Type_Conversion then
4835 T := Etype (Expression (Expr));
4836 else
4837 T := Etype (Expr);
4838 end if;
4839
4840 -- The Stack_Bounded_Pool is used internally for implementing
4841 -- access types with a Storage_Size. Since it only work properly
4842 -- when used on one specific type, we need to check that it is not
4843 -- hijacked improperly:
4844
4845 -- type T is access Integer;
4846 -- for T'Storage_Size use n;
4847 -- type Q is access Float;
4848 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4849
4850 if RTE_Available (RE_Stack_Bounded_Pool)
4851 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4852 then
4853 Error_Msg_N ("non-shareable internal Pool", Expr);
4854 return;
4855 end if;
4856
4857 -- If the argument is a name that is not an entity name, then
4858 -- we construct a renaming operation to define an entity of
4859 -- type storage pool.
4860
4861 if not Is_Entity_Name (Expr)
4862 and then Is_Object_Reference (Expr)
4863 then
4864 Pool := Make_Temporary (Loc, 'P', Expr);
4865
4866 declare
4867 Rnode : constant Node_Id :=
4868 Make_Object_Renaming_Declaration (Loc,
4869 Defining_Identifier => Pool,
4870 Subtype_Mark =>
4871 New_Occurrence_Of (Etype (Expr), Loc),
4872 Name => Expr);
4873
4874 begin
4875 -- If the attribute definition clause comes from an aspect
4876 -- clause, then insert the renaming before the associated
4877 -- entity's declaration, since the attribute clause has
4878 -- not yet been appended to the declaration list.
4879
4880 if From_Aspect_Specification (N) then
4881 Insert_Before (Parent (Entity (N)), Rnode);
4882 else
4883 Insert_Before (N, Rnode);
4884 end if;
4885
4886 Analyze (Rnode);
4887 Set_Associated_Storage_Pool (U_Ent, Pool);
4888 end;
4889
4890 elsif Is_Entity_Name (Expr) then
4891 Pool := Entity (Expr);
4892
4893 -- If pool is a renamed object, get original one. This can
4894 -- happen with an explicit renaming, and within instances.
4895
4896 while Present (Renamed_Object (Pool))
4897 and then Is_Entity_Name (Renamed_Object (Pool))
4898 loop
4899 Pool := Entity (Renamed_Object (Pool));
4900 end loop;
4901
4902 if Present (Renamed_Object (Pool))
4903 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
4904 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
4905 then
4906 Pool := Entity (Expression (Renamed_Object (Pool)));
4907 end if;
4908
4909 Set_Associated_Storage_Pool (U_Ent, Pool);
4910
4911 elsif Nkind (Expr) = N_Type_Conversion
4912 and then Is_Entity_Name (Expression (Expr))
4913 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
4914 then
4915 Pool := Entity (Expression (Expr));
4916 Set_Associated_Storage_Pool (U_Ent, Pool);
4917
4918 else
4919 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
4920 return;
4921 end if;
4922 end;
4923
4924 ------------------
4925 -- Storage_Size --
4926 ------------------
4927
4928 -- Storage_Size attribute definition clause
4929
4930 when Attribute_Storage_Size => Storage_Size : declare
4931 Btype : constant Entity_Id := Base_Type (U_Ent);
4932
4933 begin
4934 if Is_Task_Type (U_Ent) then
4935
4936 -- Check obsolescent (but never obsolescent if from aspect)
4937
4938 if not From_Aspect_Specification (N) then
4939 Check_Restriction (No_Obsolescent_Features, N);
4940
4941 if Warn_On_Obsolescent_Feature then
4942 Error_Msg_N
4943 ("?j?storage size clause for task is an " &
4944 "obsolescent feature (RM J.9)", N);
4945 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4946 end if;
4947 end if;
4948
4949 FOnly := True;
4950 end if;
4951
4952 if not Is_Access_Type (U_Ent)
4953 and then Ekind (U_Ent) /= E_Task_Type
4954 then
4955 Error_Msg_N ("storage size cannot be given for &", Nam);
4956
4957 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4958 Error_Msg_N
4959 ("storage size cannot be given for a derived access type",
4960 Nam);
4961
4962 elsif Duplicate_Clause then
4963 null;
4964
4965 else
4966 Analyze_And_Resolve (Expr, Any_Integer);
4967
4968 if Is_Access_Type (U_Ent) then
4969
4970 -- Check for Storage_Pool previously given
4971
4972 declare
4973 SP : constant Node_Id :=
4974 Get_Attribute_Definition_Clause
4975 (U_Ent, Attribute_Storage_Pool);
4976
4977 begin
4978 if Present (SP) then
4979 Check_Pool_Size_Clash (U_Ent, SP, N);
4980 end if;
4981 end;
4982
4983 -- Special case of for x'Storage_Size use 0
4984
4985 if Is_OK_Static_Expression (Expr)
4986 and then Expr_Value (Expr) = 0
4987 then
4988 Set_No_Pool_Assigned (Btype);
4989 end if;
4990 end if;
4991
4992 Set_Has_Storage_Size_Clause (Btype);
4993 end if;
4994 end Storage_Size;
4995
4996 -----------------
4997 -- Stream_Size --
4998 -----------------
4999
5000 when Attribute_Stream_Size => Stream_Size : declare
5001 Size : constant Uint := Static_Integer (Expr);
5002
5003 begin
5004 if Ada_Version <= Ada_95 then
5005 Check_Restriction (No_Implementation_Attributes, N);
5006 end if;
5007
5008 if Duplicate_Clause then
5009 null;
5010
5011 elsif Is_Elementary_Type (U_Ent) then
5012 if Size /= System_Storage_Unit
5013 and then
5014 Size /= System_Storage_Unit * 2
5015 and then
5016 Size /= System_Storage_Unit * 4
5017 and then
5018 Size /= System_Storage_Unit * 8
5019 then
5020 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5021 Error_Msg_N
5022 ("stream size for elementary type must be a"
5023 & " power of 2 and at least ^", N);
5024
5025 elsif RM_Size (U_Ent) > Size then
5026 Error_Msg_Uint_1 := RM_Size (U_Ent);
5027 Error_Msg_N
5028 ("stream size for elementary type must be a"
5029 & " power of 2 and at least ^", N);
5030 end if;
5031
5032 Set_Has_Stream_Size_Clause (U_Ent);
5033
5034 else
5035 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5036 end if;
5037 end Stream_Size;
5038
5039 ----------------
5040 -- Value_Size --
5041 ----------------
5042
5043 -- Value_Size attribute definition clause
5044
5045 when Attribute_Value_Size => Value_Size : declare
5046 Size : constant Uint := Static_Integer (Expr);
5047 Biased : Boolean;
5048
5049 begin
5050 if not Is_Type (U_Ent) then
5051 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5052
5053 elsif Duplicate_Clause then
5054 null;
5055
5056 elsif Is_Array_Type (U_Ent)
5057 and then not Is_Constrained (U_Ent)
5058 then
5059 Error_Msg_N
5060 ("Value_Size cannot be given for unconstrained array", Nam);
5061
5062 else
5063 if Is_Elementary_Type (U_Ent) then
5064 Check_Size (Expr, U_Ent, Size, Biased);
5065 Set_Biased (U_Ent, N, "value size clause", Biased);
5066 end if;
5067
5068 Set_RM_Size (U_Ent, Size);
5069 end if;
5070 end Value_Size;
5071
5072 -----------------------
5073 -- Variable_Indexing --
5074 -----------------------
5075
5076 when Attribute_Variable_Indexing =>
5077 Check_Indexing_Functions;
5078
5079 -----------
5080 -- Write --
5081 -----------
5082
5083 when Attribute_Write =>
5084 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5085 Set_Has_Specified_Stream_Write (Ent);
5086
5087 -- All other attributes cannot be set
5088
5089 when others =>
5090 Error_Msg_N
5091 ("attribute& cannot be set with definition clause", N);
5092 end case;
5093
5094 -- The test for the type being frozen must be performed after any
5095 -- expression the clause has been analyzed since the expression itself
5096 -- might cause freezing that makes the clause illegal.
5097
5098 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5099 return;
5100 end if;
5101 end Analyze_Attribute_Definition_Clause;
5102
5103 ----------------------------
5104 -- Analyze_Code_Statement --
5105 ----------------------------
5106
5107 procedure Analyze_Code_Statement (N : Node_Id) is
5108 HSS : constant Node_Id := Parent (N);
5109 SBody : constant Node_Id := Parent (HSS);
5110 Subp : constant Entity_Id := Current_Scope;
5111 Stmt : Node_Id;
5112 Decl : Node_Id;
5113 StmtO : Node_Id;
5114 DeclO : Node_Id;
5115
5116 begin
5117 -- Analyze and check we get right type, note that this implements the
5118 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5119 -- is the only way that Asm_Insn could possibly be visible.
5120
5121 Analyze_And_Resolve (Expression (N));
5122
5123 if Etype (Expression (N)) = Any_Type then
5124 return;
5125 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5126 Error_Msg_N ("incorrect type for code statement", N);
5127 return;
5128 end if;
5129
5130 Check_Code_Statement (N);
5131
5132 -- Make sure we appear in the handled statement sequence of a
5133 -- subprogram (RM 13.8(3)).
5134
5135 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5136 or else Nkind (SBody) /= N_Subprogram_Body
5137 then
5138 Error_Msg_N
5139 ("code statement can only appear in body of subprogram", N);
5140 return;
5141 end if;
5142
5143 -- Do remaining checks (RM 13.8(3)) if not already done
5144
5145 if not Is_Machine_Code_Subprogram (Subp) then
5146 Set_Is_Machine_Code_Subprogram (Subp);
5147
5148 -- No exception handlers allowed
5149
5150 if Present (Exception_Handlers (HSS)) then
5151 Error_Msg_N
5152 ("exception handlers not permitted in machine code subprogram",
5153 First (Exception_Handlers (HSS)));
5154 end if;
5155
5156 -- No declarations other than use clauses and pragmas (we allow
5157 -- certain internally generated declarations as well).
5158
5159 Decl := First (Declarations (SBody));
5160 while Present (Decl) loop
5161 DeclO := Original_Node (Decl);
5162 if Comes_From_Source (DeclO)
5163 and not Nkind_In (DeclO, N_Pragma,
5164 N_Use_Package_Clause,
5165 N_Use_Type_Clause,
5166 N_Implicit_Label_Declaration)
5167 then
5168 Error_Msg_N
5169 ("this declaration not allowed in machine code subprogram",
5170 DeclO);
5171 end if;
5172
5173 Next (Decl);
5174 end loop;
5175
5176 -- No statements other than code statements, pragmas, and labels.
5177 -- Again we allow certain internally generated statements.
5178
5179 -- In Ada 2012, qualified expressions are names, and the code
5180 -- statement is initially parsed as a procedure call.
5181
5182 Stmt := First (Statements (HSS));
5183 while Present (Stmt) loop
5184 StmtO := Original_Node (Stmt);
5185
5186 -- A procedure call transformed into a code statement is OK.
5187
5188 if Ada_Version >= Ada_2012
5189 and then Nkind (StmtO) = N_Procedure_Call_Statement
5190 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5191 then
5192 null;
5193
5194 elsif Comes_From_Source (StmtO)
5195 and then not Nkind_In (StmtO, N_Pragma,
5196 N_Label,
5197 N_Code_Statement)
5198 then
5199 Error_Msg_N
5200 ("this statement is not allowed in machine code subprogram",
5201 StmtO);
5202 end if;
5203
5204 Next (Stmt);
5205 end loop;
5206 end if;
5207 end Analyze_Code_Statement;
5208
5209 -----------------------------------------------
5210 -- Analyze_Enumeration_Representation_Clause --
5211 -----------------------------------------------
5212
5213 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5214 Ident : constant Node_Id := Identifier (N);
5215 Aggr : constant Node_Id := Array_Aggregate (N);
5216 Enumtype : Entity_Id;
5217 Elit : Entity_Id;
5218 Expr : Node_Id;
5219 Assoc : Node_Id;
5220 Choice : Node_Id;
5221 Val : Uint;
5222
5223 Err : Boolean := False;
5224 -- Set True to avoid cascade errors and crashes on incorrect source code
5225
5226 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5227 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5228 -- Allowed range of universal integer (= allowed range of enum lit vals)
5229
5230 Min : Uint;
5231 Max : Uint;
5232 -- Minimum and maximum values of entries
5233
5234 Max_Node : Node_Id;
5235 -- Pointer to node for literal providing max value
5236
5237 begin
5238 if Ignore_Rep_Clauses then
5239 return;
5240 end if;
5241
5242 -- Ignore enumeration rep clauses by default in CodePeer mode,
5243 -- unless -gnatd.I is specified, as a work around for potential false
5244 -- positive messages.
5245
5246 if CodePeer_Mode and not Debug_Flag_Dot_II then
5247 return;
5248 end if;
5249
5250 -- First some basic error checks
5251
5252 Find_Type (Ident);
5253 Enumtype := Entity (Ident);
5254
5255 if Enumtype = Any_Type
5256 or else Rep_Item_Too_Early (Enumtype, N)
5257 then
5258 return;
5259 else
5260 Enumtype := Underlying_Type (Enumtype);
5261 end if;
5262
5263 if not Is_Enumeration_Type (Enumtype) then
5264 Error_Msg_NE
5265 ("enumeration type required, found}",
5266 Ident, First_Subtype (Enumtype));
5267 return;
5268 end if;
5269
5270 -- Ignore rep clause on generic actual type. This will already have
5271 -- been flagged on the template as an error, and this is the safest
5272 -- way to ensure we don't get a junk cascaded message in the instance.
5273
5274 if Is_Generic_Actual_Type (Enumtype) then
5275 return;
5276
5277 -- Type must be in current scope
5278
5279 elsif Scope (Enumtype) /= Current_Scope then
5280 Error_Msg_N ("type must be declared in this scope", Ident);
5281 return;
5282
5283 -- Type must be a first subtype
5284
5285 elsif not Is_First_Subtype (Enumtype) then
5286 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5287 return;
5288
5289 -- Ignore duplicate rep clause
5290
5291 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5292 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5293 return;
5294
5295 -- Don't allow rep clause for standard [wide_[wide_]]character
5296
5297 elsif Is_Standard_Character_Type (Enumtype) then
5298 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5299 return;
5300
5301 -- Check that the expression is a proper aggregate (no parentheses)
5302
5303 elsif Paren_Count (Aggr) /= 0 then
5304 Error_Msg
5305 ("extra parentheses surrounding aggregate not allowed",
5306 First_Sloc (Aggr));
5307 return;
5308
5309 -- All tests passed, so set rep clause in place
5310
5311 else
5312 Set_Has_Enumeration_Rep_Clause (Enumtype);
5313 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5314 end if;
5315
5316 -- Now we process the aggregate. Note that we don't use the normal
5317 -- aggregate code for this purpose, because we don't want any of the
5318 -- normal expansion activities, and a number of special semantic
5319 -- rules apply (including the component type being any integer type)
5320
5321 Elit := First_Literal (Enumtype);
5322
5323 -- First the positional entries if any
5324
5325 if Present (Expressions (Aggr)) then
5326 Expr := First (Expressions (Aggr));
5327 while Present (Expr) loop
5328 if No (Elit) then
5329 Error_Msg_N ("too many entries in aggregate", Expr);
5330 return;
5331 end if;
5332
5333 Val := Static_Integer (Expr);
5334
5335 -- Err signals that we found some incorrect entries processing
5336 -- the list. The final checks for completeness and ordering are
5337 -- skipped in this case.
5338
5339 if Val = No_Uint then
5340 Err := True;
5341 elsif Val < Lo or else Hi < Val then
5342 Error_Msg_N ("value outside permitted range", Expr);
5343 Err := True;
5344 end if;
5345
5346 Set_Enumeration_Rep (Elit, Val);
5347 Set_Enumeration_Rep_Expr (Elit, Expr);
5348 Next (Expr);
5349 Next (Elit);
5350 end loop;
5351 end if;
5352
5353 -- Now process the named entries if present
5354
5355 if Present (Component_Associations (Aggr)) then
5356 Assoc := First (Component_Associations (Aggr));
5357 while Present (Assoc) loop
5358 Choice := First (Choices (Assoc));
5359
5360 if Present (Next (Choice)) then
5361 Error_Msg_N
5362 ("multiple choice not allowed here", Next (Choice));
5363 Err := True;
5364 end if;
5365
5366 if Nkind (Choice) = N_Others_Choice then
5367 Error_Msg_N ("others choice not allowed here", Choice);
5368 Err := True;
5369
5370 elsif Nkind (Choice) = N_Range then
5371
5372 -- ??? should allow zero/one element range here
5373
5374 Error_Msg_N ("range not allowed here", Choice);
5375 Err := True;
5376
5377 else
5378 Analyze_And_Resolve (Choice, Enumtype);
5379
5380 if Error_Posted (Choice) then
5381 Err := True;
5382 end if;
5383
5384 if not Err then
5385 if Is_Entity_Name (Choice)
5386 and then Is_Type (Entity (Choice))
5387 then
5388 Error_Msg_N ("subtype name not allowed here", Choice);
5389 Err := True;
5390
5391 -- ??? should allow static subtype with zero/one entry
5392
5393 elsif Etype (Choice) = Base_Type (Enumtype) then
5394 if not Is_Static_Expression (Choice) then
5395 Flag_Non_Static_Expr
5396 ("non-static expression used for choice!", Choice);
5397 Err := True;
5398
5399 else
5400 Elit := Expr_Value_E (Choice);
5401
5402 if Present (Enumeration_Rep_Expr (Elit)) then
5403 Error_Msg_Sloc :=
5404 Sloc (Enumeration_Rep_Expr (Elit));
5405 Error_Msg_NE
5406 ("representation for& previously given#",
5407 Choice, Elit);
5408 Err := True;
5409 end if;
5410
5411 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5412
5413 Expr := Expression (Assoc);
5414 Val := Static_Integer (Expr);
5415
5416 if Val = No_Uint then
5417 Err := True;
5418
5419 elsif Val < Lo or else Hi < Val then
5420 Error_Msg_N ("value outside permitted range", Expr);
5421 Err := True;
5422 end if;
5423
5424 Set_Enumeration_Rep (Elit, Val);
5425 end if;
5426 end if;
5427 end if;
5428 end if;
5429
5430 Next (Assoc);
5431 end loop;
5432 end if;
5433
5434 -- Aggregate is fully processed. Now we check that a full set of
5435 -- representations was given, and that they are in range and in order.
5436 -- These checks are only done if no other errors occurred.
5437
5438 if not Err then
5439 Min := No_Uint;
5440 Max := No_Uint;
5441
5442 Elit := First_Literal (Enumtype);
5443 while Present (Elit) loop
5444 if No (Enumeration_Rep_Expr (Elit)) then
5445 Error_Msg_NE ("missing representation for&!", N, Elit);
5446
5447 else
5448 Val := Enumeration_Rep (Elit);
5449
5450 if Min = No_Uint then
5451 Min := Val;
5452 end if;
5453
5454 if Val /= No_Uint then
5455 if Max /= No_Uint and then Val <= Max then
5456 Error_Msg_NE
5457 ("enumeration value for& not ordered!",
5458 Enumeration_Rep_Expr (Elit), Elit);
5459 end if;
5460
5461 Max_Node := Enumeration_Rep_Expr (Elit);
5462 Max := Val;
5463 end if;
5464
5465 -- If there is at least one literal whose representation is not
5466 -- equal to the Pos value, then note that this enumeration type
5467 -- has a non-standard representation.
5468
5469 if Val /= Enumeration_Pos (Elit) then
5470 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5471 end if;
5472 end if;
5473
5474 Next (Elit);
5475 end loop;
5476
5477 -- Now set proper size information
5478
5479 declare
5480 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5481
5482 begin
5483 if Has_Size_Clause (Enumtype) then
5484
5485 -- All OK, if size is OK now
5486
5487 if RM_Size (Enumtype) >= Minsize then
5488 null;
5489
5490 else
5491 -- Try if we can get by with biasing
5492
5493 Minsize :=
5494 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5495
5496 -- Error message if even biasing does not work
5497
5498 if RM_Size (Enumtype) < Minsize then
5499 Error_Msg_Uint_1 := RM_Size (Enumtype);
5500 Error_Msg_Uint_2 := Max;
5501 Error_Msg_N
5502 ("previously given size (^) is too small "
5503 & "for this value (^)", Max_Node);
5504
5505 -- If biasing worked, indicate that we now have biased rep
5506
5507 else
5508 Set_Biased
5509 (Enumtype, Size_Clause (Enumtype), "size clause");
5510 end if;
5511 end if;
5512
5513 else
5514 Set_RM_Size (Enumtype, Minsize);
5515 Set_Enum_Esize (Enumtype);
5516 end if;
5517
5518 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
5519 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
5520 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
5521 end;
5522 end if;
5523
5524 -- We repeat the too late test in case it froze itself
5525
5526 if Rep_Item_Too_Late (Enumtype, N) then
5527 null;
5528 end if;
5529 end Analyze_Enumeration_Representation_Clause;
5530
5531 ----------------------------
5532 -- Analyze_Free_Statement --
5533 ----------------------------
5534
5535 procedure Analyze_Free_Statement (N : Node_Id) is
5536 begin
5537 Analyze (Expression (N));
5538 end Analyze_Free_Statement;
5539
5540 ---------------------------
5541 -- Analyze_Freeze_Entity --
5542 ---------------------------
5543
5544 procedure Analyze_Freeze_Entity (N : Node_Id) is
5545 begin
5546 Freeze_Entity_Checks (N);
5547 end Analyze_Freeze_Entity;
5548
5549 -----------------------------------
5550 -- Analyze_Freeze_Generic_Entity --
5551 -----------------------------------
5552
5553 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
5554 begin
5555 Freeze_Entity_Checks (N);
5556 end Analyze_Freeze_Generic_Entity;
5557
5558 ------------------------------------------
5559 -- Analyze_Record_Representation_Clause --
5560 ------------------------------------------
5561
5562 -- Note: we check as much as we can here, but we can't do any checks
5563 -- based on the position values (e.g. overlap checks) until freeze time
5564 -- because especially in Ada 2005 (machine scalar mode), the processing
5565 -- for non-standard bit order can substantially change the positions.
5566 -- See procedure Check_Record_Representation_Clause (called from Freeze)
5567 -- for the remainder of this processing.
5568
5569 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
5570 Ident : constant Node_Id := Identifier (N);
5571 Biased : Boolean;
5572 CC : Node_Id;
5573 Comp : Entity_Id;
5574 Fbit : Uint;
5575 Hbit : Uint := Uint_0;
5576 Lbit : Uint;
5577 Ocomp : Entity_Id;
5578 Posit : Uint;
5579 Rectype : Entity_Id;
5580 Recdef : Node_Id;
5581
5582 function Is_Inherited (Comp : Entity_Id) return Boolean;
5583 -- True if Comp is an inherited component in a record extension
5584
5585 ------------------
5586 -- Is_Inherited --
5587 ------------------
5588
5589 function Is_Inherited (Comp : Entity_Id) return Boolean is
5590 Comp_Base : Entity_Id;
5591
5592 begin
5593 if Ekind (Rectype) = E_Record_Subtype then
5594 Comp_Base := Original_Record_Component (Comp);
5595 else
5596 Comp_Base := Comp;
5597 end if;
5598
5599 return Comp_Base /= Original_Record_Component (Comp_Base);
5600 end Is_Inherited;
5601
5602 -- Local variables
5603
5604 Is_Record_Extension : Boolean;
5605 -- True if Rectype is a record extension
5606
5607 CR_Pragma : Node_Id := Empty;
5608 -- Points to N_Pragma node if Complete_Representation pragma present
5609
5610 -- Start of processing for Analyze_Record_Representation_Clause
5611
5612 begin
5613 if Ignore_Rep_Clauses then
5614 return;
5615 end if;
5616
5617 Find_Type (Ident);
5618 Rectype := Entity (Ident);
5619
5620 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
5621 return;
5622 else
5623 Rectype := Underlying_Type (Rectype);
5624 end if;
5625
5626 -- First some basic error checks
5627
5628 if not Is_Record_Type (Rectype) then
5629 Error_Msg_NE
5630 ("record type required, found}", Ident, First_Subtype (Rectype));
5631 return;
5632
5633 elsif Scope (Rectype) /= Current_Scope then
5634 Error_Msg_N ("type must be declared in this scope", N);
5635 return;
5636
5637 elsif not Is_First_Subtype (Rectype) then
5638 Error_Msg_N ("cannot give record rep clause for subtype", N);
5639 return;
5640
5641 elsif Has_Record_Rep_Clause (Rectype) then
5642 Error_Msg_N ("duplicate record rep clause ignored", N);
5643 return;
5644
5645 elsif Rep_Item_Too_Late (Rectype, N) then
5646 return;
5647 end if;
5648
5649 -- We know we have a first subtype, now possibly go the the anonymous
5650 -- base type to determine whether Rectype is a record extension.
5651
5652 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
5653 Is_Record_Extension :=
5654 Nkind (Recdef) = N_Derived_Type_Definition
5655 and then Present (Record_Extension_Part (Recdef));
5656
5657 if Present (Mod_Clause (N)) then
5658 declare
5659 Loc : constant Source_Ptr := Sloc (N);
5660 M : constant Node_Id := Mod_Clause (N);
5661 P : constant List_Id := Pragmas_Before (M);
5662 AtM_Nod : Node_Id;
5663
5664 Mod_Val : Uint;
5665 pragma Warnings (Off, Mod_Val);
5666
5667 begin
5668 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
5669
5670 if Warn_On_Obsolescent_Feature then
5671 Error_Msg_N
5672 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
5673 Error_Msg_N
5674 ("\?j?use alignment attribute definition clause instead", N);
5675 end if;
5676
5677 if Present (P) then
5678 Analyze_List (P);
5679 end if;
5680
5681 -- In ASIS_Mode mode, expansion is disabled, but we must convert
5682 -- the Mod clause into an alignment clause anyway, so that the
5683 -- back-end can compute and back-annotate properly the size and
5684 -- alignment of types that may include this record.
5685
5686 -- This seems dubious, this destroys the source tree in a manner
5687 -- not detectable by ASIS ???
5688
5689 if Operating_Mode = Check_Semantics and then ASIS_Mode then
5690 AtM_Nod :=
5691 Make_Attribute_Definition_Clause (Loc,
5692 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
5693 Chars => Name_Alignment,
5694 Expression => Relocate_Node (Expression (M)));
5695
5696 Set_From_At_Mod (AtM_Nod);
5697 Insert_After (N, AtM_Nod);
5698 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5699 Set_Mod_Clause (N, Empty);
5700
5701 else
5702 -- Get the alignment value to perform error checking
5703
5704 Mod_Val := Get_Alignment_Value (Expression (M));
5705 end if;
5706 end;
5707 end if;
5708
5709 -- For untagged types, clear any existing component clauses for the
5710 -- type. If the type is derived, this is what allows us to override
5711 -- a rep clause for the parent. For type extensions, the representation
5712 -- of the inherited components is inherited, so we want to keep previous
5713 -- component clauses for completeness.
5714
5715 if not Is_Tagged_Type (Rectype) then
5716 Comp := First_Component_Or_Discriminant (Rectype);
5717 while Present (Comp) loop
5718 Set_Component_Clause (Comp, Empty);
5719 Next_Component_Or_Discriminant (Comp);
5720 end loop;
5721 end if;
5722
5723 -- All done if no component clauses
5724
5725 CC := First (Component_Clauses (N));
5726
5727 if No (CC) then
5728 return;
5729 end if;
5730
5731 -- A representation like this applies to the base type
5732
5733 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5734 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5735 Set_Has_Specified_Layout (Base_Type (Rectype));
5736
5737 -- Process the component clauses
5738
5739 while Present (CC) loop
5740
5741 -- Pragma
5742
5743 if Nkind (CC) = N_Pragma then
5744 Analyze (CC);
5745
5746 -- The only pragma of interest is Complete_Representation
5747
5748 if Pragma_Name (CC) = Name_Complete_Representation then
5749 CR_Pragma := CC;
5750 end if;
5751
5752 -- Processing for real component clause
5753
5754 else
5755 Posit := Static_Integer (Position (CC));
5756 Fbit := Static_Integer (First_Bit (CC));
5757 Lbit := Static_Integer (Last_Bit (CC));
5758
5759 if Posit /= No_Uint
5760 and then Fbit /= No_Uint
5761 and then Lbit /= No_Uint
5762 then
5763 if Posit < 0 then
5764 Error_Msg_N
5765 ("position cannot be negative", Position (CC));
5766
5767 elsif Fbit < 0 then
5768 Error_Msg_N
5769 ("first bit cannot be negative", First_Bit (CC));
5770
5771 -- The Last_Bit specified in a component clause must not be
5772 -- less than the First_Bit minus one (RM-13.5.1(10)).
5773
5774 elsif Lbit < Fbit - 1 then
5775 Error_Msg_N
5776 ("last bit cannot be less than first bit minus one",
5777 Last_Bit (CC));
5778
5779 -- Values look OK, so find the corresponding record component
5780 -- Even though the syntax allows an attribute reference for
5781 -- implementation-defined components, GNAT does not allow the
5782 -- tag to get an explicit position.
5783
5784 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5785 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5786 Error_Msg_N ("position of tag cannot be specified", CC);
5787 else
5788 Error_Msg_N ("illegal component name", CC);
5789 end if;
5790
5791 else
5792 Comp := First_Entity (Rectype);
5793 while Present (Comp) loop
5794 exit when Chars (Comp) = Chars (Component_Name (CC));
5795 Next_Entity (Comp);
5796 end loop;
5797
5798 if No (Comp) then
5799
5800 -- Maybe component of base type that is absent from
5801 -- statically constrained first subtype.
5802
5803 Comp := First_Entity (Base_Type (Rectype));
5804 while Present (Comp) loop
5805 exit when Chars (Comp) = Chars (Component_Name (CC));
5806 Next_Entity (Comp);
5807 end loop;
5808 end if;
5809
5810 if No (Comp) then
5811 Error_Msg_N
5812 ("component clause is for non-existent field", CC);
5813
5814 -- Ada 2012 (AI05-0026): Any name that denotes a
5815 -- discriminant of an object of an unchecked union type
5816 -- shall not occur within a record_representation_clause.
5817
5818 -- The general restriction of using record rep clauses on
5819 -- Unchecked_Union types has now been lifted. Since it is
5820 -- possible to introduce a record rep clause which mentions
5821 -- the discriminant of an Unchecked_Union in non-Ada 2012
5822 -- code, this check is applied to all versions of the
5823 -- language.
5824
5825 elsif Ekind (Comp) = E_Discriminant
5826 and then Is_Unchecked_Union (Rectype)
5827 then
5828 Error_Msg_N
5829 ("cannot reference discriminant of unchecked union",
5830 Component_Name (CC));
5831
5832 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5833 Error_Msg_NE
5834 ("component clause not allowed for inherited "
5835 & "component&", CC, Comp);
5836
5837 elsif Present (Component_Clause (Comp)) then
5838
5839 -- Diagnose duplicate rep clause, or check consistency
5840 -- if this is an inherited component. In a double fault,
5841 -- there may be a duplicate inconsistent clause for an
5842 -- inherited component.
5843
5844 if Scope (Original_Record_Component (Comp)) = Rectype
5845 or else Parent (Component_Clause (Comp)) = N
5846 then
5847 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5848 Error_Msg_N ("component clause previously given#", CC);
5849
5850 else
5851 declare
5852 Rep1 : constant Node_Id := Component_Clause (Comp);
5853 begin
5854 if Intval (Position (Rep1)) /=
5855 Intval (Position (CC))
5856 or else Intval (First_Bit (Rep1)) /=
5857 Intval (First_Bit (CC))
5858 or else Intval (Last_Bit (Rep1)) /=
5859 Intval (Last_Bit (CC))
5860 then
5861 Error_Msg_N
5862 ("component clause inconsistent "
5863 & "with representation of ancestor", CC);
5864
5865 elsif Warn_On_Redundant_Constructs then
5866 Error_Msg_N
5867 ("?r?redundant confirming component clause "
5868 & "for component!", CC);
5869 end if;
5870 end;
5871 end if;
5872
5873 -- Normal case where this is the first component clause we
5874 -- have seen for this entity, so set it up properly.
5875
5876 else
5877 -- Make reference for field in record rep clause and set
5878 -- appropriate entity field in the field identifier.
5879
5880 Generate_Reference
5881 (Comp, Component_Name (CC), Set_Ref => False);
5882 Set_Entity (Component_Name (CC), Comp);
5883
5884 -- Update Fbit and Lbit to the actual bit number
5885
5886 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5887 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5888
5889 if Has_Size_Clause (Rectype)
5890 and then RM_Size (Rectype) <= Lbit
5891 then
5892 Error_Msg_N
5893 ("bit number out of range of specified size",
5894 Last_Bit (CC));
5895 else
5896 Set_Component_Clause (Comp, CC);
5897 Set_Component_Bit_Offset (Comp, Fbit);
5898 Set_Esize (Comp, 1 + (Lbit - Fbit));
5899 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5900 Set_Normalized_Position (Comp, Fbit / SSU);
5901
5902 if Warn_On_Overridden_Size
5903 and then Has_Size_Clause (Etype (Comp))
5904 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5905 then
5906 Error_Msg_NE
5907 ("?S?component size overrides size clause for&",
5908 Component_Name (CC), Etype (Comp));
5909 end if;
5910
5911 -- This information is also set in the corresponding
5912 -- component of the base type, found by accessing the
5913 -- Original_Record_Component link if it is present.
5914
5915 Ocomp := Original_Record_Component (Comp);
5916
5917 if Hbit < Lbit then
5918 Hbit := Lbit;
5919 end if;
5920
5921 Check_Size
5922 (Component_Name (CC),
5923 Etype (Comp),
5924 Esize (Comp),
5925 Biased);
5926
5927 Set_Biased
5928 (Comp, First_Node (CC), "component clause", Biased);
5929
5930 if Present (Ocomp) then
5931 Set_Component_Clause (Ocomp, CC);
5932 Set_Component_Bit_Offset (Ocomp, Fbit);
5933 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5934 Set_Normalized_Position (Ocomp, Fbit / SSU);
5935 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5936
5937 Set_Normalized_Position_Max
5938 (Ocomp, Normalized_Position (Ocomp));
5939
5940 -- Note: we don't use Set_Biased here, because we
5941 -- already gave a warning above if needed, and we
5942 -- would get a duplicate for the same name here.
5943
5944 Set_Has_Biased_Representation
5945 (Ocomp, Has_Biased_Representation (Comp));
5946 end if;
5947
5948 if Esize (Comp) < 0 then
5949 Error_Msg_N ("component size is negative", CC);
5950 end if;
5951 end if;
5952 end if;
5953 end if;
5954 end if;
5955 end if;
5956
5957 Next (CC);
5958 end loop;
5959
5960 -- Check missing components if Complete_Representation pragma appeared
5961
5962 if Present (CR_Pragma) then
5963 Comp := First_Component_Or_Discriminant (Rectype);
5964 while Present (Comp) loop
5965 if No (Component_Clause (Comp)) then
5966 Error_Msg_NE
5967 ("missing component clause for &", CR_Pragma, Comp);
5968 end if;
5969
5970 Next_Component_Or_Discriminant (Comp);
5971 end loop;
5972
5973 -- Give missing components warning if required
5974
5975 elsif Warn_On_Unrepped_Components then
5976 declare
5977 Num_Repped_Components : Nat := 0;
5978 Num_Unrepped_Components : Nat := 0;
5979
5980 begin
5981 -- First count number of repped and unrepped components
5982
5983 Comp := First_Component_Or_Discriminant (Rectype);
5984 while Present (Comp) loop
5985 if Present (Component_Clause (Comp)) then
5986 Num_Repped_Components := Num_Repped_Components + 1;
5987 else
5988 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5989 end if;
5990
5991 Next_Component_Or_Discriminant (Comp);
5992 end loop;
5993
5994 -- We are only interested in the case where there is at least one
5995 -- unrepped component, and at least half the components have rep
5996 -- clauses. We figure that if less than half have them, then the
5997 -- partial rep clause is really intentional. If the component
5998 -- type has no underlying type set at this point (as for a generic
5999 -- formal type), we don't know enough to give a warning on the
6000 -- component.
6001
6002 if Num_Unrepped_Components > 0
6003 and then Num_Unrepped_Components < Num_Repped_Components
6004 then
6005 Comp := First_Component_Or_Discriminant (Rectype);
6006 while Present (Comp) loop
6007 if No (Component_Clause (Comp))
6008 and then Comes_From_Source (Comp)
6009 and then Present (Underlying_Type (Etype (Comp)))
6010 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6011 or else Size_Known_At_Compile_Time
6012 (Underlying_Type (Etype (Comp))))
6013 and then not Has_Warnings_Off (Rectype)
6014 then
6015 Error_Msg_Sloc := Sloc (Comp);
6016 Error_Msg_NE
6017 ("?C?no component clause given for & declared #",
6018 N, Comp);
6019 end if;
6020
6021 Next_Component_Or_Discriminant (Comp);
6022 end loop;
6023 end if;
6024 end;
6025 end if;
6026 end Analyze_Record_Representation_Clause;
6027
6028 -------------------------------------------
6029 -- Build_Invariant_Procedure_Declaration --
6030 -------------------------------------------
6031
6032 function Build_Invariant_Procedure_Declaration
6033 (Typ : Entity_Id) return Node_Id
6034 is
6035 Loc : constant Source_Ptr := Sloc (Typ);
6036 Object_Entity : constant Entity_Id :=
6037 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
6038 Spec : Node_Id;
6039 SId : Entity_Id;
6040
6041 begin
6042 Set_Etype (Object_Entity, Typ);
6043
6044 -- Check for duplicate definiations.
6045
6046 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
6047 return Empty;
6048 end if;
6049
6050 SId :=
6051 Make_Defining_Identifier (Loc,
6052 Chars => New_External_Name (Chars (Typ), "Invariant"));
6053 Set_Has_Invariants (Typ);
6054 Set_Ekind (SId, E_Procedure);
6055 Set_Is_Invariant_Procedure (SId);
6056 Set_Invariant_Procedure (Typ, SId);
6057
6058 Spec :=
6059 Make_Procedure_Specification (Loc,
6060 Defining_Unit_Name => SId,
6061 Parameter_Specifications => New_List (
6062 Make_Parameter_Specification (Loc,
6063 Defining_Identifier => Object_Entity,
6064 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
6065
6066 return Make_Subprogram_Declaration (Loc, Specification => Spec);
6067 end Build_Invariant_Procedure_Declaration;
6068
6069 -------------------------------
6070 -- Build_Invariant_Procedure --
6071 -------------------------------
6072
6073 -- The procedure that is constructed here has the form
6074
6075 -- procedure typInvariant (Ixxx : typ) is
6076 -- begin
6077 -- pragma Check (Invariant, exp, "failed invariant from xxx");
6078 -- pragma Check (Invariant, exp, "failed invariant from xxx");
6079 -- ...
6080 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
6081 -- ...
6082 -- end typInvariant;
6083
6084 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
6085 Loc : constant Source_Ptr := Sloc (Typ);
6086 Stmts : List_Id;
6087 Spec : Node_Id;
6088 SId : Entity_Id;
6089 PDecl : Node_Id;
6090 PBody : Node_Id;
6091
6092 Visible_Decls : constant List_Id := Visible_Declarations (N);
6093 Private_Decls : constant List_Id := Private_Declarations (N);
6094
6095 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
6096 -- Appends statements to Stmts for any invariants in the rep item chain
6097 -- of the given type. If Inherit is False, then we only process entries
6098 -- on the chain for the type Typ. If Inherit is True, then we ignore any
6099 -- Invariant aspects, but we process all Invariant'Class aspects, adding
6100 -- "inherited" to the exception message and generating an informational
6101 -- message about the inheritance of an invariant.
6102
6103 Object_Name : Name_Id;
6104 -- Name for argument of invariant procedure
6105
6106 Object_Entity : Node_Id;
6107 -- The entity of the formal for the procedure
6108
6109 --------------------
6110 -- Add_Invariants --
6111 --------------------
6112
6113 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
6114 Ritem : Node_Id;
6115 Arg1 : Node_Id;
6116 Arg2 : Node_Id;
6117 Arg3 : Node_Id;
6118 Exp : Node_Id;
6119 Loc : Source_Ptr;
6120 Assoc : List_Id;
6121 Str : String_Id;
6122
6123 procedure Replace_Type_Reference (N : Node_Id);
6124 -- Replace a single occurrence N of the subtype name with a reference
6125 -- to the formal of the predicate function. N can be an identifier
6126 -- referencing the subtype, or a selected component, representing an
6127 -- appropriately qualified occurrence of the subtype name.
6128
6129 procedure Replace_Type_References is
6130 new Replace_Type_References_Generic (Replace_Type_Reference);
6131 -- Traverse an expression replacing all occurrences of the subtype
6132 -- name with appropriate references to the object that is the formal
6133 -- parameter of the predicate function. Note that we must ensure
6134 -- that the type and entity information is properly set in the
6135 -- replacement node, since we will do a Preanalyze call of this
6136 -- expression without proper visibility of the procedure argument.
6137
6138 ----------------------------
6139 -- Replace_Type_Reference --
6140 ----------------------------
6141
6142 -- Note: See comments in Add_Predicates.Replace_Type_Reference
6143 -- regarding handling of Sloc and Comes_From_Source.
6144
6145 procedure Replace_Type_Reference (N : Node_Id) is
6146 begin
6147
6148 -- Add semantic information to node to be rewritten, for ASIS
6149 -- navigation needs.
6150
6151 if Nkind (N) = N_Identifier then
6152 Set_Entity (N, T);
6153 Set_Etype (N, T);
6154
6155 elsif Nkind (N) = N_Selected_Component then
6156 Analyze (Prefix (N));
6157 Set_Entity (Selector_Name (N), T);
6158 Set_Etype (Selector_Name (N), T);
6159 end if;
6160
6161 -- Invariant'Class, replace with T'Class (obj)
6162
6163 if Class_Present (Ritem) then
6164 Rewrite (N,
6165 Make_Type_Conversion (Sloc (N),
6166 Subtype_Mark =>
6167 Make_Attribute_Reference (Sloc (N),
6168 Prefix => New_Occurrence_Of (T, Sloc (N)),
6169 Attribute_Name => Name_Class),
6170 Expression => Make_Identifier (Sloc (N), Object_Name)));
6171
6172 Set_Entity (Expression (N), Object_Entity);
6173 Set_Etype (Expression (N), Typ);
6174
6175 -- Invariant, replace with obj
6176
6177 else
6178 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6179 Set_Entity (N, Object_Entity);
6180 Set_Etype (N, Typ);
6181 end if;
6182
6183 Set_Comes_From_Source (N, True);
6184 end Replace_Type_Reference;
6185
6186 -- Start of processing for Add_Invariants
6187
6188 begin
6189 Ritem := First_Rep_Item (T);
6190 while Present (Ritem) loop
6191 if Nkind (Ritem) = N_Pragma
6192 and then Pragma_Name (Ritem) = Name_Invariant
6193 then
6194 Arg1 := First (Pragma_Argument_Associations (Ritem));
6195 Arg2 := Next (Arg1);
6196 Arg3 := Next (Arg2);
6197
6198 Arg1 := Get_Pragma_Arg (Arg1);
6199 Arg2 := Get_Pragma_Arg (Arg2);
6200
6201 -- For Inherit case, ignore Invariant, process only Class case
6202
6203 if Inherit then
6204 if not Class_Present (Ritem) then
6205 goto Continue;
6206 end if;
6207
6208 -- For Inherit false, process only item for right type
6209
6210 else
6211 if Entity (Arg1) /= Typ then
6212 goto Continue;
6213 end if;
6214 end if;
6215
6216 if No (Stmts) then
6217 Stmts := Empty_List;
6218 end if;
6219
6220 Exp := New_Copy_Tree (Arg2);
6221
6222 -- Preserve sloc of original pragma Invariant
6223
6224 Loc := Sloc (Ritem);
6225
6226 -- We need to replace any occurrences of the name of the type
6227 -- with references to the object, converted to type'Class in
6228 -- the case of Invariant'Class aspects.
6229
6230 Replace_Type_References (Exp, Chars (T));
6231
6232 -- If this invariant comes from an aspect, find the aspect
6233 -- specification, and replace the saved expression because
6234 -- we need the subtype references replaced for the calls to
6235 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6236 -- and Check_Aspect_At_End_Of_Declarations.
6237
6238 if From_Aspect_Specification (Ritem) then
6239 declare
6240 Aitem : Node_Id;
6241
6242 begin
6243 -- Loop to find corresponding aspect, note that this
6244 -- must be present given the pragma is marked delayed.
6245
6246 Aitem := Next_Rep_Item (Ritem);
6247 while Present (Aitem) loop
6248 if Nkind (Aitem) = N_Aspect_Specification
6249 and then Aspect_Rep_Item (Aitem) = Ritem
6250 then
6251 Set_Entity
6252 (Identifier (Aitem), New_Copy_Tree (Exp));
6253 exit;
6254 end if;
6255
6256 Aitem := Next_Rep_Item (Aitem);
6257 end loop;
6258 end;
6259 end if;
6260
6261 -- Now we need to preanalyze the expression to properly capture
6262 -- the visibility in the visible part. The expression will not
6263 -- be analyzed for real until the body is analyzed, but that is
6264 -- at the end of the private part and has the wrong visibility.
6265
6266 Set_Parent (Exp, N);
6267 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
6268
6269 -- In ASIS mode, even if assertions are not enabled, we must
6270 -- analyze the original expression in the aspect specification
6271 -- because it is part of the original tree.
6272
6273 if ASIS_Mode then
6274 declare
6275 Inv : constant Node_Id :=
6276 Expression (Corresponding_Aspect (Ritem));
6277 begin
6278 Replace_Type_References (Inv, Chars (T));
6279 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
6280 end;
6281 end if;
6282
6283 -- Build first two arguments for Check pragma
6284
6285 Assoc := New_List (
6286 Make_Pragma_Argument_Association (Loc,
6287 Expression => Make_Identifier (Loc, Name_Invariant)),
6288 Make_Pragma_Argument_Association (Loc,
6289 Expression => Exp));
6290
6291 -- Add message if present in Invariant pragma
6292
6293 if Present (Arg3) then
6294 Str := Strval (Get_Pragma_Arg (Arg3));
6295
6296 -- If inherited case, and message starts "failed invariant",
6297 -- change it to be "failed inherited invariant".
6298
6299 if Inherit then
6300 String_To_Name_Buffer (Str);
6301
6302 if Name_Buffer (1 .. 16) = "failed invariant" then
6303 Insert_Str_In_Name_Buffer ("inherited ", 8);
6304 Str := String_From_Name_Buffer;
6305 end if;
6306 end if;
6307
6308 Append_To (Assoc,
6309 Make_Pragma_Argument_Association (Loc,
6310 Expression => Make_String_Literal (Loc, Str)));
6311 end if;
6312
6313 -- Add Check pragma to list of statements
6314
6315 Append_To (Stmts,
6316 Make_Pragma (Loc,
6317 Pragma_Identifier =>
6318 Make_Identifier (Loc, Name_Check),
6319 Pragma_Argument_Associations => Assoc));
6320
6321 -- If Inherited case and option enabled, output info msg. Note
6322 -- that we know this is a case of Invariant'Class.
6323
6324 if Inherit and Opt.List_Inherited_Aspects then
6325 Error_Msg_Sloc := Sloc (Ritem);
6326 Error_Msg_N
6327 ("info: & inherits `Invariant''Class` aspect from #?L?",
6328 Typ);
6329 end if;
6330 end if;
6331
6332 <<Continue>>
6333 Next_Rep_Item (Ritem);
6334 end loop;
6335 end Add_Invariants;
6336
6337 -- Start of processing for Build_Invariant_Procedure
6338
6339 begin
6340 Stmts := No_List;
6341 PDecl := Empty;
6342 PBody := Empty;
6343 SId := Empty;
6344
6345 -- If the aspect specification exists for some view of the type, the
6346 -- declaration for the procedure has been created.
6347
6348 if Has_Invariants (Typ) then
6349 SId := Invariant_Procedure (Typ);
6350 end if;
6351
6352 if Present (SId) then
6353 PDecl := Unit_Declaration_Node (SId);
6354 else
6355 PDecl := Build_Invariant_Procedure_Declaration (Typ);
6356 end if;
6357
6358 -- Recover formal of procedure, for use in the calls to invariant
6359 -- functions (including inherited ones).
6360
6361 Object_Entity :=
6362 Defining_Identifier
6363 (First (Parameter_Specifications (Specification (PDecl))));
6364 Object_Name := Chars (Object_Entity);
6365
6366 -- Add invariants for the current type
6367
6368 Add_Invariants (Typ, Inherit => False);
6369
6370 -- Add invariants for parent types
6371
6372 declare
6373 Current_Typ : Entity_Id;
6374 Parent_Typ : Entity_Id;
6375
6376 begin
6377 Current_Typ := Typ;
6378 loop
6379 Parent_Typ := Etype (Current_Typ);
6380
6381 if Is_Private_Type (Parent_Typ)
6382 and then Present (Full_View (Base_Type (Parent_Typ)))
6383 then
6384 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6385 end if;
6386
6387 exit when Parent_Typ = Current_Typ;
6388
6389 Current_Typ := Parent_Typ;
6390 Add_Invariants (Current_Typ, Inherit => True);
6391 end loop;
6392 end;
6393
6394 -- Build the procedure if we generated at least one Check pragma
6395
6396 if Stmts /= No_List then
6397 Spec := Copy_Separate_Tree (Specification (PDecl));
6398
6399 PBody :=
6400 Make_Subprogram_Body (Loc,
6401 Specification => Spec,
6402 Declarations => Empty_List,
6403 Handled_Statement_Sequence =>
6404 Make_Handled_Sequence_Of_Statements (Loc,
6405 Statements => Stmts));
6406
6407 -- Insert procedure declaration and spec at the appropriate points.
6408 -- If declaration is already analyzed, it was processed by the
6409 -- generated pragma.
6410
6411 if Present (Private_Decls) then
6412
6413 -- The spec goes at the end of visible declarations, but they have
6414 -- already been analyzed, so we need to explicitly do the analyze.
6415
6416 if not Analyzed (PDecl) then
6417 Append_To (Visible_Decls, PDecl);
6418 Analyze (PDecl);
6419 end if;
6420
6421 -- The body goes at the end of the private declarations, which we
6422 -- have not analyzed yet, so we do not need to perform an explicit
6423 -- analyze call. We skip this if there are no private declarations
6424 -- (this is an error that will be caught elsewhere);
6425
6426 Append_To (Private_Decls, PBody);
6427
6428 -- If the invariant appears on the full view of a type, the
6429 -- analysis of the private part is complete, and we must
6430 -- analyze the new body explicitly.
6431
6432 if In_Private_Part (Current_Scope) then
6433 Analyze (PBody);
6434 end if;
6435
6436 -- If there are no private declarations this may be an error that
6437 -- will be diagnosed elsewhere. However, if this is a non-private
6438 -- type that inherits invariants, it needs no completion and there
6439 -- may be no private part. In this case insert invariant procedure
6440 -- at end of current declarative list, and analyze at once, given
6441 -- that the type is about to be frozen.
6442
6443 elsif not Is_Private_Type (Typ) then
6444 Append_To (Visible_Decls, PDecl);
6445 Append_To (Visible_Decls, PBody);
6446 Analyze (PDecl);
6447 Analyze (PBody);
6448 end if;
6449 end if;
6450 end Build_Invariant_Procedure;
6451
6452 -------------------------------
6453 -- Build_Predicate_Functions --
6454 -------------------------------
6455
6456 -- The procedures that are constructed here have the form:
6457
6458 -- function typPredicate (Ixxx : typ) return Boolean is
6459 -- begin
6460 -- return
6461 -- exp1 and then exp2 and then ...
6462 -- and then typ1Predicate (typ1 (Ixxx))
6463 -- and then typ2Predicate (typ2 (Ixxx))
6464 -- and then ...;
6465 -- end typPredicate;
6466
6467 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
6468 -- this is the point at which these expressions get analyzed, providing the
6469 -- required delay, and typ1, typ2, are entities from which predicates are
6470 -- inherited. Note that we do NOT generate Check pragmas, that's because we
6471 -- use this function even if checks are off, e.g. for membership tests.
6472
6473 -- If the expression has at least one Raise_Expression, then we also build
6474 -- the typPredicateM version of the function, in which any occurrence of a
6475 -- Raise_Expression is converted to "return False".
6476
6477 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
6478 Loc : constant Source_Ptr := Sloc (Typ);
6479
6480 Expr : Node_Id;
6481 -- This is the expression for the result of the function. It is
6482 -- is build by connecting the component predicates with AND THEN.
6483
6484 Expr_M : Node_Id;
6485 -- This is the corresponding return expression for the Predicate_M
6486 -- function. It differs in that raise expressions are marked for
6487 -- special expansion (see Process_REs).
6488
6489 Object_Name : constant Name_Id := New_Internal_Name ('I');
6490 -- Name for argument of Predicate procedure. Note that we use the same
6491 -- name for both predicate procedure. That way the reference within the
6492 -- predicate expression is the same in both functions.
6493
6494 Object_Entity : constant Entity_Id :=
6495 Make_Defining_Identifier (Loc, Chars => Object_Name);
6496 -- Entity for argument of Predicate procedure
6497
6498 Object_Entity_M : constant Entity_Id :=
6499 Make_Defining_Identifier (Loc, Chars => Object_Name);
6500 -- Entity for argument of Predicate_M procedure
6501
6502 Raise_Expression_Present : Boolean := False;
6503 -- Set True if Expr has at least one Raise_Expression
6504
6505 Static_Predic : Node_Id := Empty;
6506 -- Set to N_Pragma node for a static predicate if one is encountered
6507
6508 procedure Add_Call (T : Entity_Id);
6509 -- Includes a call to the predicate function for type T in Expr if T
6510 -- has predicates and Predicate_Function (T) is non-empty.
6511
6512 procedure Add_Predicates;
6513 -- Appends expressions for any Predicate pragmas in the rep item chain
6514 -- Typ to Expr. Note that we look only at items for this exact entity.
6515 -- Inheritance of predicates for the parent type is done by calling the
6516 -- Predicate_Function of the parent type, using Add_Call above.
6517
6518 function Test_RE (N : Node_Id) return Traverse_Result;
6519 -- Used in Test_REs, tests one node for being a raise expression, and if
6520 -- so sets Raise_Expression_Present True.
6521
6522 procedure Test_REs is new Traverse_Proc (Test_RE);
6523 -- Tests to see if Expr contains any raise expressions
6524
6525 function Process_RE (N : Node_Id) return Traverse_Result;
6526 -- Used in Process REs, tests if node N is a raise expression, and if
6527 -- so, marks it to be converted to return False.
6528
6529 procedure Process_REs is new Traverse_Proc (Process_RE);
6530 -- Marks any raise expressions in Expr_M to return False
6531
6532 --------------
6533 -- Add_Call --
6534 --------------
6535
6536 procedure Add_Call (T : Entity_Id) is
6537 Exp : Node_Id;
6538
6539 begin
6540 if Present (T) and then Present (Predicate_Function (T)) then
6541 Set_Has_Predicates (Typ);
6542
6543 -- Build the call to the predicate function of T
6544
6545 Exp :=
6546 Make_Predicate_Call
6547 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
6548
6549 -- Add call to evolving expression, using AND THEN if needed
6550
6551 if No (Expr) then
6552 Expr := Exp;
6553 else
6554 Expr :=
6555 Make_And_Then (Loc,
6556 Left_Opnd => Relocate_Node (Expr),
6557 Right_Opnd => Exp);
6558 end if;
6559
6560 -- Output info message on inheritance if required. Note we do not
6561 -- give this information for generic actual types, since it is
6562 -- unwelcome noise in that case in instantiations. We also
6563 -- generally suppress the message in instantiations, and also
6564 -- if it involves internal names.
6565
6566 if Opt.List_Inherited_Aspects
6567 and then not Is_Generic_Actual_Type (Typ)
6568 and then Instantiation_Depth (Sloc (Typ)) = 0
6569 and then not Is_Internal_Name (Chars (T))
6570 and then not Is_Internal_Name (Chars (Typ))
6571 then
6572 Error_Msg_Sloc := Sloc (Predicate_Function (T));
6573 Error_Msg_Node_2 := T;
6574 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
6575 end if;
6576 end if;
6577 end Add_Call;
6578
6579 --------------------
6580 -- Add_Predicates --
6581 --------------------
6582
6583 procedure Add_Predicates is
6584 Ritem : Node_Id;
6585 Arg1 : Node_Id;
6586 Arg2 : Node_Id;
6587
6588 procedure Replace_Type_Reference (N : Node_Id);
6589 -- Replace a single occurrence N of the subtype name with a reference
6590 -- to the formal of the predicate function. N can be an identifier
6591 -- referencing the subtype, or a selected component, representing an
6592 -- appropriately qualified occurrence of the subtype name.
6593
6594 procedure Replace_Type_References is
6595 new Replace_Type_References_Generic (Replace_Type_Reference);
6596 -- Traverse an expression changing every occurrence of an identifier
6597 -- whose name matches the name of the subtype with a reference to
6598 -- the formal parameter of the predicate function.
6599
6600 ----------------------------
6601 -- Replace_Type_Reference --
6602 ----------------------------
6603
6604 procedure Replace_Type_Reference (N : Node_Id) is
6605 begin
6606 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6607 -- Use the Sloc of the usage name, not the defining name
6608
6609 Set_Etype (N, Typ);
6610 Set_Entity (N, Object_Entity);
6611
6612 -- We want to treat the node as if it comes from source, so that
6613 -- ASIS will not ignore it
6614
6615 Set_Comes_From_Source (N, True);
6616 end Replace_Type_Reference;
6617
6618 -- Start of processing for Add_Predicates
6619
6620 begin
6621 Ritem := First_Rep_Item (Typ);
6622 while Present (Ritem) loop
6623 if Nkind (Ritem) = N_Pragma
6624 and then Pragma_Name (Ritem) = Name_Predicate
6625 then
6626 -- Save the static predicate of the type for diagnostics and
6627 -- error reporting purposes.
6628
6629 if Present (Corresponding_Aspect (Ritem))
6630 and then Chars (Identifier (Corresponding_Aspect (Ritem))) =
6631 Name_Static_Predicate
6632 then
6633 Static_Predic := Ritem;
6634 end if;
6635
6636 -- Acquire arguments
6637
6638 Arg1 := First (Pragma_Argument_Associations (Ritem));
6639 Arg2 := Next (Arg1);
6640
6641 Arg1 := Get_Pragma_Arg (Arg1);
6642 Arg2 := Get_Pragma_Arg (Arg2);
6643
6644 -- See if this predicate pragma is for the current type or for
6645 -- its full view. A predicate on a private completion is placed
6646 -- on the partial view beause this is the visible entity that
6647 -- is frozen.
6648
6649 if Entity (Arg1) = Typ
6650 or else Full_View (Entity (Arg1)) = Typ
6651 then
6652 -- We have a match, this entry is for our subtype
6653
6654 -- We need to replace any occurrences of the name of the
6655 -- type with references to the object.
6656
6657 Replace_Type_References (Arg2, Chars (Typ));
6658
6659 -- If this predicate comes from an aspect, find the aspect
6660 -- specification, and replace the saved expression because
6661 -- we need the subtype references replaced for the calls to
6662 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6663 -- and Check_Aspect_At_End_Of_Declarations.
6664
6665 if From_Aspect_Specification (Ritem) then
6666 declare
6667 Aitem : Node_Id;
6668
6669 begin
6670 -- Loop to find corresponding aspect, note that this
6671 -- must be present given the pragma is marked delayed.
6672
6673 Aitem := Next_Rep_Item (Ritem);
6674 loop
6675 if Nkind (Aitem) = N_Aspect_Specification
6676 and then Aspect_Rep_Item (Aitem) = Ritem
6677 then
6678 Set_Entity
6679 (Identifier (Aitem), New_Copy_Tree (Arg2));
6680 exit;
6681 end if;
6682
6683 Aitem := Next_Rep_Item (Aitem);
6684 end loop;
6685 end;
6686 end if;
6687
6688 -- Now we can add the expression
6689
6690 if No (Expr) then
6691 Expr := Relocate_Node (Arg2);
6692
6693 -- There already was a predicate, so add to it
6694
6695 else
6696 Expr :=
6697 Make_And_Then (Loc,
6698 Left_Opnd => Relocate_Node (Expr),
6699 Right_Opnd => Relocate_Node (Arg2));
6700 end if;
6701 end if;
6702 end if;
6703
6704 Next_Rep_Item (Ritem);
6705 end loop;
6706 end Add_Predicates;
6707
6708 ----------------
6709 -- Process_RE --
6710 ----------------
6711
6712 function Process_RE (N : Node_Id) return Traverse_Result is
6713 begin
6714 if Nkind (N) = N_Raise_Expression then
6715 Set_Convert_To_Return_False (N);
6716 return Skip;
6717 else
6718 return OK;
6719 end if;
6720 end Process_RE;
6721
6722 -------------
6723 -- Test_RE --
6724 -------------
6725
6726 function Test_RE (N : Node_Id) return Traverse_Result is
6727 begin
6728 if Nkind (N) = N_Raise_Expression then
6729 Raise_Expression_Present := True;
6730 return Abandon;
6731 else
6732 return OK;
6733 end if;
6734 end Test_RE;
6735
6736 -- Start of processing for Build_Predicate_Functions
6737
6738 begin
6739 -- Return if already built or if type does not have predicates
6740
6741 if not Has_Predicates (Typ)
6742 or else Present (Predicate_Function (Typ))
6743 then
6744 return;
6745 end if;
6746
6747 -- Prepare to construct predicate expression
6748
6749 Expr := Empty;
6750
6751 -- Add Predicates for the current type
6752
6753 Add_Predicates;
6754
6755 -- Add predicates for ancestor if present
6756
6757 declare
6758 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
6759 begin
6760 if Present (Atyp) then
6761 Add_Call (Atyp);
6762 end if;
6763 end;
6764
6765 -- Case where predicates are present
6766
6767 if Present (Expr) then
6768
6769 -- Test for raise expression present
6770
6771 Test_REs (Expr);
6772
6773 -- If raise expression is present, capture a copy of Expr for use
6774 -- in building the predicateM function version later on. For this
6775 -- copy we replace references to Object_Entity by Object_Entity_M.
6776
6777 if Raise_Expression_Present then
6778 declare
6779 Map : constant Elist_Id := New_Elmt_List;
6780 begin
6781 Append_Elmt (Object_Entity, Map);
6782 Append_Elmt (Object_Entity_M, Map);
6783 Expr_M := New_Copy_Tree (Expr, Map => Map);
6784 end;
6785 end if;
6786
6787 -- Build the main predicate function
6788
6789 declare
6790 SId : constant Entity_Id :=
6791 Make_Defining_Identifier (Loc,
6792 Chars => New_External_Name (Chars (Typ), "Predicate"));
6793 -- The entity for the the function spec
6794
6795 SIdB : constant Entity_Id :=
6796 Make_Defining_Identifier (Loc,
6797 Chars => New_External_Name (Chars (Typ), "Predicate"));
6798 -- The entity for the function body
6799
6800 Spec : Node_Id;
6801 FDecl : Node_Id;
6802 FBody : Node_Id;
6803
6804 begin
6805 -- Build function declaration
6806
6807 Set_Ekind (SId, E_Function);
6808 Set_Is_Internal (SId);
6809 Set_Is_Predicate_Function (SId);
6810 Set_Predicate_Function (Typ, SId);
6811
6812 -- The predicate function is shared between views of a type
6813
6814 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6815 Set_Predicate_Function (Full_View (Typ), SId);
6816 end if;
6817
6818 Spec :=
6819 Make_Function_Specification (Loc,
6820 Defining_Unit_Name => SId,
6821 Parameter_Specifications => New_List (
6822 Make_Parameter_Specification (Loc,
6823 Defining_Identifier => Object_Entity,
6824 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6825 Result_Definition =>
6826 New_Occurrence_Of (Standard_Boolean, Loc));
6827
6828 FDecl :=
6829 Make_Subprogram_Declaration (Loc,
6830 Specification => Spec);
6831
6832 -- Build function body
6833
6834 Spec :=
6835 Make_Function_Specification (Loc,
6836 Defining_Unit_Name => SIdB,
6837 Parameter_Specifications => New_List (
6838 Make_Parameter_Specification (Loc,
6839 Defining_Identifier =>
6840 Make_Defining_Identifier (Loc, Object_Name),
6841 Parameter_Type =>
6842 New_Occurrence_Of (Typ, Loc))),
6843 Result_Definition =>
6844 New_Occurrence_Of (Standard_Boolean, Loc));
6845
6846 FBody :=
6847 Make_Subprogram_Body (Loc,
6848 Specification => Spec,
6849 Declarations => Empty_List,
6850 Handled_Statement_Sequence =>
6851 Make_Handled_Sequence_Of_Statements (Loc,
6852 Statements => New_List (
6853 Make_Simple_Return_Statement (Loc,
6854 Expression => Expr))));
6855
6856 -- Insert declaration before freeze node and body after
6857
6858 Insert_Before_And_Analyze (N, FDecl);
6859 Insert_After_And_Analyze (N, FBody);
6860 end;
6861
6862 -- Test for raise expressions present and if so build M version
6863
6864 if Raise_Expression_Present then
6865 declare
6866 SId : constant Entity_Id :=
6867 Make_Defining_Identifier (Loc,
6868 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6869 -- The entity for the the function spec
6870
6871 SIdB : constant Entity_Id :=
6872 Make_Defining_Identifier (Loc,
6873 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6874 -- The entity for the function body
6875
6876 Spec : Node_Id;
6877 FDecl : Node_Id;
6878 FBody : Node_Id;
6879 BTemp : Entity_Id;
6880
6881 begin
6882 -- Mark any raise expressions for special expansion
6883
6884 Process_REs (Expr_M);
6885
6886 -- Build function declaration
6887
6888 Set_Ekind (SId, E_Function);
6889 Set_Is_Predicate_Function_M (SId);
6890 Set_Predicate_Function_M (Typ, SId);
6891
6892 -- The predicate function is shared between views of a type
6893
6894 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6895 Set_Predicate_Function_M (Full_View (Typ), SId);
6896 end if;
6897
6898 Spec :=
6899 Make_Function_Specification (Loc,
6900 Defining_Unit_Name => SId,
6901 Parameter_Specifications => New_List (
6902 Make_Parameter_Specification (Loc,
6903 Defining_Identifier => Object_Entity_M,
6904 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6905 Result_Definition =>
6906 New_Occurrence_Of (Standard_Boolean, Loc));
6907
6908 FDecl :=
6909 Make_Subprogram_Declaration (Loc,
6910 Specification => Spec);
6911
6912 -- Build function body
6913
6914 Spec :=
6915 Make_Function_Specification (Loc,
6916 Defining_Unit_Name => SIdB,
6917 Parameter_Specifications => New_List (
6918 Make_Parameter_Specification (Loc,
6919 Defining_Identifier =>
6920 Make_Defining_Identifier (Loc, Object_Name),
6921 Parameter_Type =>
6922 New_Occurrence_Of (Typ, Loc))),
6923 Result_Definition =>
6924 New_Occurrence_Of (Standard_Boolean, Loc));
6925
6926 -- Build the body, we declare the boolean expression before
6927 -- doing the return, because we are not really confident of
6928 -- what happens if a return appears within a return.
6929
6930 BTemp :=
6931 Make_Defining_Identifier (Loc,
6932 Chars => New_Internal_Name ('B'));
6933
6934 FBody :=
6935 Make_Subprogram_Body (Loc,
6936 Specification => Spec,
6937
6938 Declarations => New_List (
6939 Make_Object_Declaration (Loc,
6940 Defining_Identifier => BTemp,
6941 Constant_Present => True,
6942 Object_Definition =>
6943 New_Occurrence_Of (Standard_Boolean, Loc),
6944 Expression => Expr_M)),
6945
6946 Handled_Statement_Sequence =>
6947 Make_Handled_Sequence_Of_Statements (Loc,
6948 Statements => New_List (
6949 Make_Simple_Return_Statement (Loc,
6950 Expression => New_Occurrence_Of (BTemp, Loc)))));
6951
6952 -- Insert declaration before freeze node and body after
6953
6954 Insert_Before_And_Analyze (N, FDecl);
6955 Insert_After_And_Analyze (N, FBody);
6956 end;
6957 end if;
6958
6959 if Is_Scalar_Type (Typ) then
6960
6961 -- Attempt to build a static predicate for a discrete or a real
6962 -- subtype. This action may fail because the actual expression may
6963 -- not be static. Note that the presence of an inherited or
6964 -- explicitly declared dynamic predicate is orthogonal to this
6965 -- check because we are only interested in the static predicate.
6966
6967 if Ekind_In (Typ, E_Decimal_Fixed_Point_Subtype,
6968 E_Enumeration_Subtype,
6969 E_Floating_Point_Subtype,
6970 E_Modular_Integer_Subtype,
6971 E_Ordinary_Fixed_Point_Subtype,
6972 E_Signed_Integer_Subtype)
6973 then
6974 Build_Static_Predicate (Typ, Expr, Object_Name);
6975
6976 -- Emit an error when the predicate is categorized as static
6977 -- but its expression is dynamic.
6978
6979 if Present (Static_Predic)
6980 and then No (Static_Predicate (Typ))
6981 then
6982 Error_Msg_F
6983 ("expression does not have required form for "
6984 & "static predicate",
6985 Next (First (Pragma_Argument_Associations
6986 (Static_Predic))));
6987 end if;
6988 end if;
6989
6990 -- If a static predicate applies on other types, that's an error:
6991 -- either the type is scalar but non-static, or it's not even a
6992 -- scalar type. We do not issue an error on generated types, as
6993 -- these may be duplicates of the same error on a source type.
6994
6995 elsif Present (Static_Predic) and then Comes_From_Source (Typ) then
6996 if Is_Scalar_Type (Typ) then
6997 Error_Msg_FE
6998 ("static predicate not allowed for non-static type&",
6999 Typ, Typ);
7000 else
7001 Error_Msg_FE
7002 ("static predicate not allowed for non-scalar type&",
7003 Typ, Typ);
7004 end if;
7005 end if;
7006 end if;
7007 end Build_Predicate_Functions;
7008
7009 ----------------------------
7010 -- Build_Static_Predicate --
7011 ----------------------------
7012
7013 procedure Build_Static_Predicate
7014 (Typ : Entity_Id;
7015 Expr : Node_Id;
7016 Nam : Name_Id)
7017 is
7018 Loc : constant Source_Ptr := Sloc (Expr);
7019
7020 Non_Static : exception;
7021 -- Raised if something non-static is found
7022
7023 Btyp : constant Entity_Id := Base_Type (Typ);
7024
7025 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7026 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7027 -- Low bound and high bound value of base type of Typ
7028
7029 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
7030 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
7031 -- Low bound and high bound values of static subtype Typ
7032
7033 type REnt is record
7034 Lo, Hi : Uint;
7035 end record;
7036 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7037 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7038 -- value.
7039
7040 type RList is array (Nat range <>) of REnt;
7041 -- A list of ranges. The ranges are sorted in increasing order, and are
7042 -- disjoint (there is a gap of at least one value between each range in
7043 -- the table). A value is in the set of ranges in Rlist if it lies
7044 -- within one of these ranges.
7045
7046 False_Range : constant RList :=
7047 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7048 -- An empty set of ranges represents a range list that can never be
7049 -- satisfied, since there are no ranges in which the value could lie,
7050 -- so it does not lie in any of them. False_Range is a canonical value
7051 -- for this empty set, but general processing should test for an Rlist
7052 -- with length zero (see Is_False predicate), since other null ranges
7053 -- may appear which must be treated as False.
7054
7055 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7056 -- Range representing True, value must be in the base range
7057
7058 function "and" (Left : RList; Right : RList) return RList;
7059 -- And's together two range lists, returning a range list. This is a set
7060 -- intersection operation.
7061
7062 function "or" (Left : RList; Right : RList) return RList;
7063 -- Or's together two range lists, returning a range list. This is a set
7064 -- union operation.
7065
7066 function "not" (Right : RList) return RList;
7067 -- Returns complement of a given range list, i.e. a range list
7068 -- representing all the values in TLo .. THi that are not in the input
7069 -- operand Right.
7070
7071 function Build_Val (V : Uint) return Node_Id;
7072 -- Return an analyzed N_Identifier node referencing this value, suitable
7073 -- for use as an entry in the Static_Predicate list. This node is typed
7074 -- with the base type.
7075
7076 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7077 -- Return an analyzed N_Range node referencing this range, suitable for
7078 -- use as an entry in the Static_Predicate list. This node is typed with
7079 -- the base type.
7080
7081 function Get_RList (Exp : Node_Id) return RList;
7082 -- This is a recursive routine that converts the given expression into a
7083 -- list of ranges, suitable for use in building the static predicate.
7084
7085 function Is_False (R : RList) return Boolean;
7086 pragma Inline (Is_False);
7087 -- Returns True if the given range list is empty, and thus represents a
7088 -- False list of ranges that can never be satisfied.
7089
7090 function Is_True (R : RList) return Boolean;
7091 -- Returns True if R trivially represents the True predicate by having a
7092 -- single range from BLo to BHi.
7093
7094 function Is_Type_Ref (N : Node_Id) return Boolean;
7095 pragma Inline (Is_Type_Ref);
7096 -- Returns if True if N is a reference to the type for the predicate in
7097 -- the expression (i.e. if it is an identifier whose Chars field matches
7098 -- the Nam given in the call).
7099
7100 function Lo_Val (N : Node_Id) return Uint;
7101 -- Given static expression or static range from a Static_Predicate list,
7102 -- gets expression value or low bound of range.
7103
7104 function Hi_Val (N : Node_Id) return Uint;
7105 -- Given static expression or static range from a Static_Predicate list,
7106 -- gets expression value of high bound of range.
7107
7108 function Membership_Entry (N : Node_Id) return RList;
7109 -- Given a single membership entry (range, value, or subtype), returns
7110 -- the corresponding range list. Raises Static_Error if not static.
7111
7112 function Membership_Entries (N : Node_Id) return RList;
7113 -- Given an element on an alternatives list of a membership operation,
7114 -- returns the range list corresponding to this entry and all following
7115 -- entries (i.e. returns the "or" of this list of values).
7116
7117 function Stat_Pred (Typ : Entity_Id) return RList;
7118 -- Given a type, if it has a static predicate, then return the predicate
7119 -- as a range list, otherwise raise Non_Static.
7120
7121 -----------
7122 -- "and" --
7123 -----------
7124
7125 function "and" (Left : RList; Right : RList) return RList is
7126 FEnt : REnt;
7127 -- First range of result
7128
7129 SLeft : Nat := Left'First;
7130 -- Start of rest of left entries
7131
7132 SRight : Nat := Right'First;
7133 -- Start of rest of right entries
7134
7135 begin
7136 -- If either range is True, return the other
7137
7138 if Is_True (Left) then
7139 return Right;
7140 elsif Is_True (Right) then
7141 return Left;
7142 end if;
7143
7144 -- If either range is False, return False
7145
7146 if Is_False (Left) or else Is_False (Right) then
7147 return False_Range;
7148 end if;
7149
7150 -- Loop to remove entries at start that are disjoint, and thus just
7151 -- get discarded from the result entirely.
7152
7153 loop
7154 -- If no operands left in either operand, result is false
7155
7156 if SLeft > Left'Last or else SRight > Right'Last then
7157 return False_Range;
7158
7159 -- Discard first left operand entry if disjoint with right
7160
7161 elsif Left (SLeft).Hi < Right (SRight).Lo then
7162 SLeft := SLeft + 1;
7163
7164 -- Discard first right operand entry if disjoint with left
7165
7166 elsif Right (SRight).Hi < Left (SLeft).Lo then
7167 SRight := SRight + 1;
7168
7169 -- Otherwise we have an overlapping entry
7170
7171 else
7172 exit;
7173 end if;
7174 end loop;
7175
7176 -- Now we have two non-null operands, and first entries overlap. The
7177 -- first entry in the result will be the overlapping part of these
7178 -- two entries.
7179
7180 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7181 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7182
7183 -- Now we can remove the entry that ended at a lower value, since its
7184 -- contribution is entirely contained in Fent.
7185
7186 if Left (SLeft).Hi <= Right (SRight).Hi then
7187 SLeft := SLeft + 1;
7188 else
7189 SRight := SRight + 1;
7190 end if;
7191
7192 -- Compute result by concatenating this first entry with the "and" of
7193 -- the remaining parts of the left and right operands. Note that if
7194 -- either of these is empty, "and" will yield empty, so that we will
7195 -- end up with just Fent, which is what we want in that case.
7196
7197 return
7198 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7199 end "and";
7200
7201 -----------
7202 -- "not" --
7203 -----------
7204
7205 function "not" (Right : RList) return RList is
7206 begin
7207 -- Return True if False range
7208
7209 if Is_False (Right) then
7210 return True_Range;
7211 end if;
7212
7213 -- Return False if True range
7214
7215 if Is_True (Right) then
7216 return False_Range;
7217 end if;
7218
7219 -- Here if not trivial case
7220
7221 declare
7222 Result : RList (1 .. Right'Length + 1);
7223 -- May need one more entry for gap at beginning and end
7224
7225 Count : Nat := 0;
7226 -- Number of entries stored in Result
7227
7228 begin
7229 -- Gap at start
7230
7231 if Right (Right'First).Lo > TLo then
7232 Count := Count + 1;
7233 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7234 end if;
7235
7236 -- Gaps between ranges
7237
7238 for J in Right'First .. Right'Last - 1 loop
7239 Count := Count + 1;
7240 Result (Count) :=
7241 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7242 end loop;
7243
7244 -- Gap at end
7245
7246 if Right (Right'Last).Hi < THi then
7247 Count := Count + 1;
7248 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7249 end if;
7250
7251 return Result (1 .. Count);
7252 end;
7253 end "not";
7254
7255 ----------
7256 -- "or" --
7257 ----------
7258
7259 function "or" (Left : RList; Right : RList) return RList is
7260 FEnt : REnt;
7261 -- First range of result
7262
7263 SLeft : Nat := Left'First;
7264 -- Start of rest of left entries
7265
7266 SRight : Nat := Right'First;
7267 -- Start of rest of right entries
7268
7269 begin
7270 -- If either range is True, return True
7271
7272 if Is_True (Left) or else Is_True (Right) then
7273 return True_Range;
7274 end if;
7275
7276 -- If either range is False (empty), return the other
7277
7278 if Is_False (Left) then
7279 return Right;
7280 elsif Is_False (Right) then
7281 return Left;
7282 end if;
7283
7284 -- Initialize result first entry from left or right operand depending
7285 -- on which starts with the lower range.
7286
7287 if Left (SLeft).Lo < Right (SRight).Lo then
7288 FEnt := Left (SLeft);
7289 SLeft := SLeft + 1;
7290 else
7291 FEnt := Right (SRight);
7292 SRight := SRight + 1;
7293 end if;
7294
7295 -- This loop eats ranges from left and right operands that are
7296 -- contiguous with the first range we are gathering.
7297
7298 loop
7299 -- Eat first entry in left operand if contiguous or overlapped by
7300 -- gathered first operand of result.
7301
7302 if SLeft <= Left'Last
7303 and then Left (SLeft).Lo <= FEnt.Hi + 1
7304 then
7305 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7306 SLeft := SLeft + 1;
7307
7308 -- Eat first entry in right operand if contiguous or overlapped by
7309 -- gathered right operand of result.
7310
7311 elsif SRight <= Right'Last
7312 and then Right (SRight).Lo <= FEnt.Hi + 1
7313 then
7314 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7315 SRight := SRight + 1;
7316
7317 -- All done if no more entries to eat
7318
7319 else
7320 exit;
7321 end if;
7322 end loop;
7323
7324 -- Obtain result as the first entry we just computed, concatenated
7325 -- to the "or" of the remaining results (if one operand is empty,
7326 -- this will just concatenate with the other
7327
7328 return
7329 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7330 end "or";
7331
7332 -----------------
7333 -- Build_Range --
7334 -----------------
7335
7336 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7337 Result : Node_Id;
7338
7339 begin
7340 Result :=
7341 Make_Range (Loc,
7342 Low_Bound => Build_Val (Lo),
7343 High_Bound => Build_Val (Hi));
7344 Set_Etype (Result, Btyp);
7345 Set_Analyzed (Result);
7346
7347 return Result;
7348 end Build_Range;
7349
7350 ---------------
7351 -- Build_Val --
7352 ---------------
7353
7354 function Build_Val (V : Uint) return Node_Id is
7355 Result : Node_Id;
7356
7357 begin
7358 if Is_Enumeration_Type (Typ) then
7359 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7360 else
7361 Result := Make_Integer_Literal (Loc, V);
7362 end if;
7363
7364 Set_Etype (Result, Btyp);
7365 Set_Is_Static_Expression (Result);
7366 Set_Analyzed (Result);
7367 return Result;
7368 end Build_Val;
7369
7370 ---------------
7371 -- Get_RList --
7372 ---------------
7373
7374 function Get_RList (Exp : Node_Id) return RList is
7375 Op : Node_Kind;
7376 Val : Uint;
7377
7378 begin
7379 -- Static expression can only be true or false
7380
7381 if Is_OK_Static_Expression (Exp) then
7382
7383 -- For False
7384
7385 if Expr_Value (Exp) = 0 then
7386 return False_Range;
7387 else
7388 return True_Range;
7389 end if;
7390 end if;
7391
7392 -- Otherwise test node type
7393
7394 Op := Nkind (Exp);
7395
7396 case Op is
7397
7398 -- And
7399
7400 when N_Op_And | N_And_Then =>
7401 return Get_RList (Left_Opnd (Exp))
7402 and
7403 Get_RList (Right_Opnd (Exp));
7404
7405 -- Or
7406
7407 when N_Op_Or | N_Or_Else =>
7408 return Get_RList (Left_Opnd (Exp))
7409 or
7410 Get_RList (Right_Opnd (Exp));
7411
7412 -- Not
7413
7414 when N_Op_Not =>
7415 return not Get_RList (Right_Opnd (Exp));
7416
7417 -- Comparisons of type with static value
7418
7419 when N_Op_Compare =>
7420
7421 -- Type is left operand
7422
7423 if Is_Type_Ref (Left_Opnd (Exp))
7424 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7425 then
7426 Val := Expr_Value (Right_Opnd (Exp));
7427
7428 -- Typ is right operand
7429
7430 elsif Is_Type_Ref (Right_Opnd (Exp))
7431 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7432 then
7433 Val := Expr_Value (Left_Opnd (Exp));
7434
7435 -- Invert sense of comparison
7436
7437 case Op is
7438 when N_Op_Gt => Op := N_Op_Lt;
7439 when N_Op_Lt => Op := N_Op_Gt;
7440 when N_Op_Ge => Op := N_Op_Le;
7441 when N_Op_Le => Op := N_Op_Ge;
7442 when others => null;
7443 end case;
7444
7445 -- Other cases are non-static
7446
7447 else
7448 raise Non_Static;
7449 end if;
7450
7451 -- Construct range according to comparison operation
7452
7453 case Op is
7454 when N_Op_Eq =>
7455 return RList'(1 => REnt'(Val, Val));
7456
7457 when N_Op_Ge =>
7458 return RList'(1 => REnt'(Val, BHi));
7459
7460 when N_Op_Gt =>
7461 return RList'(1 => REnt'(Val + 1, BHi));
7462
7463 when N_Op_Le =>
7464 return RList'(1 => REnt'(BLo, Val));
7465
7466 when N_Op_Lt =>
7467 return RList'(1 => REnt'(BLo, Val - 1));
7468
7469 when N_Op_Ne =>
7470 return RList'(REnt'(BLo, Val - 1),
7471 REnt'(Val + 1, BHi));
7472
7473 when others =>
7474 raise Program_Error;
7475 end case;
7476
7477 -- Membership (IN)
7478
7479 when N_In =>
7480 if not Is_Type_Ref (Left_Opnd (Exp)) then
7481 raise Non_Static;
7482 end if;
7483
7484 if Present (Right_Opnd (Exp)) then
7485 return Membership_Entry (Right_Opnd (Exp));
7486 else
7487 return Membership_Entries (First (Alternatives (Exp)));
7488 end if;
7489
7490 -- Negative membership (NOT IN)
7491
7492 when N_Not_In =>
7493 if not Is_Type_Ref (Left_Opnd (Exp)) then
7494 raise Non_Static;
7495 end if;
7496
7497 if Present (Right_Opnd (Exp)) then
7498 return not Membership_Entry (Right_Opnd (Exp));
7499 else
7500 return not Membership_Entries (First (Alternatives (Exp)));
7501 end if;
7502
7503 -- Function call, may be call to static predicate
7504
7505 when N_Function_Call =>
7506 if Is_Entity_Name (Name (Exp)) then
7507 declare
7508 Ent : constant Entity_Id := Entity (Name (Exp));
7509 begin
7510 if Is_Predicate_Function (Ent)
7511 or else
7512 Is_Predicate_Function_M (Ent)
7513 then
7514 return Stat_Pred (Etype (First_Formal (Ent)));
7515 end if;
7516 end;
7517 end if;
7518
7519 -- Other function call cases are non-static
7520
7521 raise Non_Static;
7522
7523 -- Qualified expression, dig out the expression
7524
7525 when N_Qualified_Expression =>
7526 return Get_RList (Expression (Exp));
7527
7528 -- Expression with actions: if no actions, dig out expression
7529
7530 when N_Expression_With_Actions =>
7531 if Is_Empty_List (Actions (Exp)) then
7532 return Get_RList (Expression (Exp));
7533
7534 else
7535 raise Non_Static;
7536 end if;
7537
7538 -- Xor operator
7539
7540 when N_Op_Xor =>
7541 return (Get_RList (Left_Opnd (Exp))
7542 and not Get_RList (Right_Opnd (Exp)))
7543 or (Get_RList (Right_Opnd (Exp))
7544 and not Get_RList (Left_Opnd (Exp)));
7545
7546 -- Any other node type is non-static
7547
7548 when others =>
7549 raise Non_Static;
7550 end case;
7551 end Get_RList;
7552
7553 ------------
7554 -- Hi_Val --
7555 ------------
7556
7557 function Hi_Val (N : Node_Id) return Uint is
7558 begin
7559 if Is_Static_Expression (N) then
7560 return Expr_Value (N);
7561 else
7562 pragma Assert (Nkind (N) = N_Range);
7563 return Expr_Value (High_Bound (N));
7564 end if;
7565 end Hi_Val;
7566
7567 --------------
7568 -- Is_False --
7569 --------------
7570
7571 function Is_False (R : RList) return Boolean is
7572 begin
7573 return R'Length = 0;
7574 end Is_False;
7575
7576 -------------
7577 -- Is_True --
7578 -------------
7579
7580 function Is_True (R : RList) return Boolean is
7581 begin
7582 return R'Length = 1
7583 and then R (R'First).Lo = BLo
7584 and then R (R'First).Hi = BHi;
7585 end Is_True;
7586
7587 -----------------
7588 -- Is_Type_Ref --
7589 -----------------
7590
7591 function Is_Type_Ref (N : Node_Id) return Boolean is
7592 begin
7593 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
7594 end Is_Type_Ref;
7595
7596 ------------
7597 -- Lo_Val --
7598 ------------
7599
7600 function Lo_Val (N : Node_Id) return Uint is
7601 begin
7602 if Is_Static_Expression (N) then
7603 return Expr_Value (N);
7604 else
7605 pragma Assert (Nkind (N) = N_Range);
7606 return Expr_Value (Low_Bound (N));
7607 end if;
7608 end Lo_Val;
7609
7610 ------------------------
7611 -- Membership_Entries --
7612 ------------------------
7613
7614 function Membership_Entries (N : Node_Id) return RList is
7615 begin
7616 if No (Next (N)) then
7617 return Membership_Entry (N);
7618 else
7619 return Membership_Entry (N) or Membership_Entries (Next (N));
7620 end if;
7621 end Membership_Entries;
7622
7623 ----------------------
7624 -- Membership_Entry --
7625 ----------------------
7626
7627 function Membership_Entry (N : Node_Id) return RList is
7628 Val : Uint;
7629 SLo : Uint;
7630 SHi : Uint;
7631
7632 begin
7633 -- Range case
7634
7635 if Nkind (N) = N_Range then
7636 if not Is_Static_Expression (Low_Bound (N))
7637 or else
7638 not Is_Static_Expression (High_Bound (N))
7639 then
7640 raise Non_Static;
7641 else
7642 SLo := Expr_Value (Low_Bound (N));
7643 SHi := Expr_Value (High_Bound (N));
7644 return RList'(1 => REnt'(SLo, SHi));
7645 end if;
7646
7647 -- Static expression case
7648
7649 elsif Is_Static_Expression (N) then
7650 Val := Expr_Value (N);
7651 return RList'(1 => REnt'(Val, Val));
7652
7653 -- Identifier (other than static expression) case
7654
7655 else pragma Assert (Nkind (N) = N_Identifier);
7656
7657 -- Type case
7658
7659 if Is_Type (Entity (N)) then
7660
7661 -- If type has predicates, process them
7662
7663 if Has_Predicates (Entity (N)) then
7664 return Stat_Pred (Entity (N));
7665
7666 -- For static subtype without predicates, get range
7667
7668 elsif Is_Static_Subtype (Entity (N)) then
7669 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7670 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7671 return RList'(1 => REnt'(SLo, SHi));
7672
7673 -- Any other type makes us non-static
7674
7675 else
7676 raise Non_Static;
7677 end if;
7678
7679 -- Any other kind of identifier in predicate (e.g. a non-static
7680 -- expression value) means this is not a static predicate.
7681
7682 else
7683 raise Non_Static;
7684 end if;
7685 end if;
7686 end Membership_Entry;
7687
7688 ---------------
7689 -- Stat_Pred --
7690 ---------------
7691
7692 function Stat_Pred (Typ : Entity_Id) return RList is
7693 begin
7694 -- Not static if type does not have static predicates
7695
7696 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
7697 raise Non_Static;
7698 end if;
7699
7700 -- Otherwise we convert the predicate list to a range list
7701
7702 declare
7703 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
7704 P : Node_Id;
7705
7706 begin
7707 P := First (Static_Predicate (Typ));
7708 for J in Result'Range loop
7709 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7710 Next (P);
7711 end loop;
7712
7713 return Result;
7714 end;
7715 end Stat_Pred;
7716
7717 -- Start of processing for Build_Static_Predicate
7718
7719 begin
7720 -- Now analyze the expression to see if it is a static predicate
7721
7722 declare
7723 Ranges : constant RList := Get_RList (Expr);
7724 -- Range list from expression if it is static
7725
7726 Plist : List_Id;
7727
7728 begin
7729 -- Convert range list into a form for the static predicate. In the
7730 -- Ranges array, we just have raw ranges, these must be converted
7731 -- to properly typed and analyzed static expressions or range nodes.
7732
7733 -- Note: here we limit ranges to the ranges of the subtype, so that
7734 -- a predicate is always false for values outside the subtype. That
7735 -- seems fine, such values are invalid anyway, and considering them
7736 -- to fail the predicate seems allowed and friendly, and furthermore
7737 -- simplifies processing for case statements and loops.
7738
7739 Plist := New_List;
7740
7741 for J in Ranges'Range loop
7742 declare
7743 Lo : Uint := Ranges (J).Lo;
7744 Hi : Uint := Ranges (J).Hi;
7745
7746 begin
7747 -- Ignore completely out of range entry
7748
7749 if Hi < TLo or else Lo > THi then
7750 null;
7751
7752 -- Otherwise process entry
7753
7754 else
7755 -- Adjust out of range value to subtype range
7756
7757 if Lo < TLo then
7758 Lo := TLo;
7759 end if;
7760
7761 if Hi > THi then
7762 Hi := THi;
7763 end if;
7764
7765 -- Convert range into required form
7766
7767 Append_To (Plist, Build_Range (Lo, Hi));
7768 end if;
7769 end;
7770 end loop;
7771
7772 -- Processing was successful and all entries were static, so now we
7773 -- can store the result as the predicate list.
7774
7775 Set_Static_Predicate (Typ, Plist);
7776
7777 -- The processing for static predicates put the expression into
7778 -- canonical form as a series of ranges. It also eliminated
7779 -- duplicates and collapsed and combined ranges. We might as well
7780 -- replace the alternatives list of the right operand of the
7781 -- membership test with the static predicate list, which will
7782 -- usually be more efficient.
7783
7784 declare
7785 New_Alts : constant List_Id := New_List;
7786 Old_Node : Node_Id;
7787 New_Node : Node_Id;
7788
7789 begin
7790 Old_Node := First (Plist);
7791 while Present (Old_Node) loop
7792 New_Node := New_Copy (Old_Node);
7793
7794 if Nkind (New_Node) = N_Range then
7795 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7796 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7797 end if;
7798
7799 Append_To (New_Alts, New_Node);
7800 Next (Old_Node);
7801 end loop;
7802
7803 -- If empty list, replace by False
7804
7805 if Is_Empty_List (New_Alts) then
7806 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7807
7808 -- Else replace by set membership test
7809
7810 else
7811 Rewrite (Expr,
7812 Make_In (Loc,
7813 Left_Opnd => Make_Identifier (Loc, Nam),
7814 Right_Opnd => Empty,
7815 Alternatives => New_Alts));
7816
7817 -- Resolve new expression in function context
7818
7819 Install_Formals (Predicate_Function (Typ));
7820 Push_Scope (Predicate_Function (Typ));
7821 Analyze_And_Resolve (Expr, Standard_Boolean);
7822 Pop_Scope;
7823 end if;
7824 end;
7825 end;
7826
7827 -- If non-static, return doing nothing
7828
7829 exception
7830 when Non_Static =>
7831 return;
7832 end Build_Static_Predicate;
7833
7834 -----------------------------------------
7835 -- Check_Aspect_At_End_Of_Declarations --
7836 -----------------------------------------
7837
7838 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
7839 Ent : constant Entity_Id := Entity (ASN);
7840 Ident : constant Node_Id := Identifier (ASN);
7841 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7842
7843 End_Decl_Expr : constant Node_Id := Entity (Ident);
7844 -- Expression to be analyzed at end of declarations
7845
7846 Freeze_Expr : constant Node_Id := Expression (ASN);
7847 -- Expression from call to Check_Aspect_At_Freeze_Point
7848
7849 T : constant Entity_Id := Etype (Freeze_Expr);
7850 -- Type required for preanalyze call
7851
7852 Err : Boolean;
7853 -- Set False if error
7854
7855 -- On entry to this procedure, Entity (Ident) contains a copy of the
7856 -- original expression from the aspect, saved for this purpose, and
7857 -- but Expression (Ident) is a preanalyzed copy of the expression,
7858 -- preanalyzed just after the freeze point.
7859
7860 procedure Check_Overloaded_Name;
7861 -- For aspects whose expression is simply a name, this routine checks if
7862 -- the name is overloaded or not. If so, it verifies there is an
7863 -- interpretation that matches the entity obtained at the freeze point,
7864 -- otherwise the compiler complains.
7865
7866 ---------------------------
7867 -- Check_Overloaded_Name --
7868 ---------------------------
7869
7870 procedure Check_Overloaded_Name is
7871 begin
7872 if not Is_Overloaded (End_Decl_Expr) then
7873 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7874
7875 else
7876 Err := True;
7877
7878 declare
7879 Index : Interp_Index;
7880 It : Interp;
7881
7882 begin
7883 Get_First_Interp (End_Decl_Expr, Index, It);
7884 while Present (It.Typ) loop
7885 if It.Nam = Entity (Freeze_Expr) then
7886 Err := False;
7887 exit;
7888 end if;
7889
7890 Get_Next_Interp (Index, It);
7891 end loop;
7892 end;
7893 end if;
7894 end Check_Overloaded_Name;
7895
7896 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7897
7898 begin
7899 -- Case of aspects Dimension, Dimension_System and Synchronization
7900
7901 if A_Id = Aspect_Synchronization then
7902 return;
7903
7904 -- Case of stream attributes, just have to compare entities. However,
7905 -- the expression is just a name (possibly overloaded), and there may
7906 -- be stream operations declared for unrelated types, so we just need
7907 -- to verify that one of these interpretations is the one available at
7908 -- at the freeze point.
7909
7910 elsif A_Id = Aspect_Input or else
7911 A_Id = Aspect_Output or else
7912 A_Id = Aspect_Read or else
7913 A_Id = Aspect_Write
7914 then
7915 Analyze (End_Decl_Expr);
7916 Check_Overloaded_Name;
7917
7918 elsif A_Id = Aspect_Variable_Indexing or else
7919 A_Id = Aspect_Constant_Indexing or else
7920 A_Id = Aspect_Default_Iterator or else
7921 A_Id = Aspect_Iterator_Element
7922 then
7923 -- Make type unfrozen before analysis, to prevent spurious errors
7924 -- about late attributes.
7925
7926 Set_Is_Frozen (Ent, False);
7927 Analyze (End_Decl_Expr);
7928 Set_Is_Frozen (Ent, True);
7929
7930 -- If the end of declarations comes before any other freeze
7931 -- point, the Freeze_Expr is not analyzed: no check needed.
7932
7933 if Analyzed (Freeze_Expr) and then not In_Instance then
7934 Check_Overloaded_Name;
7935 else
7936 Err := False;
7937 end if;
7938
7939 -- All other cases
7940
7941 else
7942 -- In a generic context the aspect expressions have not been
7943 -- preanalyzed, so do it now. There are no conformance checks
7944 -- to perform in this case.
7945
7946 if No (T) then
7947 Check_Aspect_At_Freeze_Point (ASN);
7948 return;
7949
7950 -- The default values attributes may be defined in the private part,
7951 -- and the analysis of the expression may take place when only the
7952 -- partial view is visible. The expression must be scalar, so use
7953 -- the full view to resolve.
7954
7955 elsif (A_Id = Aspect_Default_Value
7956 or else
7957 A_Id = Aspect_Default_Component_Value)
7958 and then Is_Private_Type (T)
7959 then
7960 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7961 else
7962 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7963 end if;
7964
7965 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7966 end if;
7967
7968 -- Output error message if error
7969
7970 if Err then
7971 Error_Msg_NE
7972 ("visibility of aspect for& changes after freeze point",
7973 ASN, Ent);
7974 Error_Msg_NE
7975 ("info: & is frozen here, aspects evaluated at this point??",
7976 Freeze_Node (Ent), Ent);
7977 end if;
7978 end Check_Aspect_At_End_Of_Declarations;
7979
7980 ----------------------------------
7981 -- Check_Aspect_At_Freeze_Point --
7982 ----------------------------------
7983
7984 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7985 Ident : constant Node_Id := Identifier (ASN);
7986 -- Identifier (use Entity field to save expression)
7987
7988 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7989
7990 T : Entity_Id := Empty;
7991 -- Type required for preanalyze call
7992
7993 begin
7994 -- On entry to this procedure, Entity (Ident) contains a copy of the
7995 -- original expression from the aspect, saved for this purpose.
7996
7997 -- On exit from this procedure Entity (Ident) is unchanged, still
7998 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7999 -- of the expression, preanalyzed just after the freeze point.
8000
8001 -- Make a copy of the expression to be preanalyzed
8002
8003 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8004
8005 -- Find type for preanalyze call
8006
8007 case A_Id is
8008
8009 -- No_Aspect should be impossible
8010
8011 when No_Aspect =>
8012 raise Program_Error;
8013
8014 -- Aspects taking an optional boolean argument
8015
8016 when Boolean_Aspects |
8017 Library_Unit_Aspects =>
8018
8019 T := Standard_Boolean;
8020
8021 -- Aspects corresponding to attribute definition clauses
8022
8023 when Aspect_Address =>
8024 T := RTE (RE_Address);
8025
8026 when Aspect_Attach_Handler =>
8027 T := RTE (RE_Interrupt_ID);
8028
8029 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8030 T := RTE (RE_Bit_Order);
8031
8032 when Aspect_Convention =>
8033 return;
8034
8035 when Aspect_CPU =>
8036 T := RTE (RE_CPU_Range);
8037
8038 -- Default_Component_Value is resolved with the component type
8039
8040 when Aspect_Default_Component_Value =>
8041 T := Component_Type (Entity (ASN));
8042
8043 -- Default_Value is resolved with the type entity in question
8044
8045 when Aspect_Default_Value =>
8046 T := Entity (ASN);
8047
8048 -- Depends is a delayed aspect because it mentiones names first
8049 -- introduced by aspect Global which is already delayed. There is
8050 -- no action to be taken with respect to the aspect itself as the
8051 -- analysis is done by the corresponding pragma.
8052
8053 when Aspect_Depends =>
8054 return;
8055
8056 when Aspect_Dispatching_Domain =>
8057 T := RTE (RE_Dispatching_Domain);
8058
8059 when Aspect_External_Tag =>
8060 T := Standard_String;
8061
8062 when Aspect_External_Name =>
8063 T := Standard_String;
8064
8065 -- Global is a delayed aspect because it may reference names that
8066 -- have not been declared yet. There is no action to be taken with
8067 -- respect to the aspect itself as the reference checking is done
8068 -- on the corresponding pragma.
8069
8070 when Aspect_Global =>
8071 return;
8072
8073 when Aspect_Link_Name =>
8074 T := Standard_String;
8075
8076 when Aspect_Priority | Aspect_Interrupt_Priority =>
8077 T := Standard_Integer;
8078
8079 when Aspect_Relative_Deadline =>
8080 T := RTE (RE_Time_Span);
8081
8082 when Aspect_Small =>
8083 T := Universal_Real;
8084
8085 -- For a simple storage pool, we have to retrieve the type of the
8086 -- pool object associated with the aspect's corresponding attribute
8087 -- definition clause.
8088
8089 when Aspect_Simple_Storage_Pool =>
8090 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8091
8092 when Aspect_Storage_Pool =>
8093 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8094
8095 when Aspect_Alignment |
8096 Aspect_Component_Size |
8097 Aspect_Machine_Radix |
8098 Aspect_Object_Size |
8099 Aspect_Size |
8100 Aspect_Storage_Size |
8101 Aspect_Stream_Size |
8102 Aspect_Value_Size =>
8103 T := Any_Integer;
8104
8105 when Aspect_Linker_Section =>
8106 T := Standard_String;
8107
8108 when Aspect_Synchronization =>
8109 return;
8110
8111 -- Special case, the expression of these aspects is just an entity
8112 -- that does not need any resolution, so just analyze.
8113
8114 when Aspect_Input |
8115 Aspect_Output |
8116 Aspect_Read |
8117 Aspect_Suppress |
8118 Aspect_Unsuppress |
8119 Aspect_Warnings |
8120 Aspect_Write =>
8121 Analyze (Expression (ASN));
8122 return;
8123
8124 -- Same for Iterator aspects, where the expression is a function
8125 -- name. Legality rules are checked separately.
8126
8127 when Aspect_Constant_Indexing |
8128 Aspect_Default_Iterator |
8129 Aspect_Iterator_Element |
8130 Aspect_Variable_Indexing =>
8131 Analyze (Expression (ASN));
8132 return;
8133
8134 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8135
8136 when Aspect_Iterable =>
8137 T := Entity (ASN);
8138
8139 declare
8140 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8141 Assoc : Node_Id;
8142 Expr : Node_Id;
8143
8144 begin
8145 if Cursor = Any_Type then
8146 return;
8147 end if;
8148
8149 Assoc := First (Component_Associations (Expression (ASN)));
8150 while Present (Assoc) loop
8151 Expr := Expression (Assoc);
8152 Analyze (Expr);
8153
8154 if not Error_Posted (Expr) then
8155 Resolve_Iterable_Operation
8156 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8157 end if;
8158
8159 Next (Assoc);
8160 end loop;
8161 end;
8162
8163 return;
8164
8165 -- Invariant/Predicate take boolean expressions
8166
8167 when Aspect_Dynamic_Predicate |
8168 Aspect_Invariant |
8169 Aspect_Predicate |
8170 Aspect_Static_Predicate |
8171 Aspect_Type_Invariant =>
8172 T := Standard_Boolean;
8173
8174 -- Here is the list of aspects that don't require delay analysis
8175
8176 when Aspect_Abstract_State |
8177 Aspect_Contract_Cases |
8178 Aspect_Dimension |
8179 Aspect_Dimension_System |
8180 Aspect_Implicit_Dereference |
8181 Aspect_Initial_Condition |
8182 Aspect_Initializes |
8183 Aspect_Part_Of |
8184 Aspect_Post |
8185 Aspect_Postcondition |
8186 Aspect_Pre |
8187 Aspect_Precondition |
8188 Aspect_Refined_Depends |
8189 Aspect_Refined_Global |
8190 Aspect_Refined_Post |
8191 Aspect_Refined_State |
8192 Aspect_SPARK_Mode |
8193 Aspect_Test_Case =>
8194 raise Program_Error;
8195
8196 end case;
8197
8198 -- Do the preanalyze call
8199
8200 Preanalyze_Spec_Expression (Expression (ASN), T);
8201 end Check_Aspect_At_Freeze_Point;
8202
8203 -----------------------------------
8204 -- Check_Constant_Address_Clause --
8205 -----------------------------------
8206
8207 procedure Check_Constant_Address_Clause
8208 (Expr : Node_Id;
8209 U_Ent : Entity_Id)
8210 is
8211 procedure Check_At_Constant_Address (Nod : Node_Id);
8212 -- Checks that the given node N represents a name whose 'Address is
8213 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8214 -- address value is the same at the point of declaration of U_Ent and at
8215 -- the time of elaboration of the address clause.
8216
8217 procedure Check_Expr_Constants (Nod : Node_Id);
8218 -- Checks that Nod meets the requirements for a constant address clause
8219 -- in the sense of the enclosing procedure.
8220
8221 procedure Check_List_Constants (Lst : List_Id);
8222 -- Check that all elements of list Lst meet the requirements for a
8223 -- constant address clause in the sense of the enclosing procedure.
8224
8225 -------------------------------
8226 -- Check_At_Constant_Address --
8227 -------------------------------
8228
8229 procedure Check_At_Constant_Address (Nod : Node_Id) is
8230 begin
8231 if Is_Entity_Name (Nod) then
8232 if Present (Address_Clause (Entity ((Nod)))) then
8233 Error_Msg_NE
8234 ("invalid address clause for initialized object &!",
8235 Nod, U_Ent);
8236 Error_Msg_NE
8237 ("address for& cannot" &
8238 " depend on another address clause! (RM 13.1(22))!",
8239 Nod, U_Ent);
8240
8241 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8242 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8243 then
8244 Error_Msg_NE
8245 ("invalid address clause for initialized object &!",
8246 Nod, U_Ent);
8247 Error_Msg_Node_2 := U_Ent;
8248 Error_Msg_NE
8249 ("\& must be defined before & (RM 13.1(22))!",
8250 Nod, Entity (Nod));
8251 end if;
8252
8253 elsif Nkind (Nod) = N_Selected_Component then
8254 declare
8255 T : constant Entity_Id := Etype (Prefix (Nod));
8256
8257 begin
8258 if (Is_Record_Type (T)
8259 and then Has_Discriminants (T))
8260 or else
8261 (Is_Access_Type (T)
8262 and then Is_Record_Type (Designated_Type (T))
8263 and then Has_Discriminants (Designated_Type (T)))
8264 then
8265 Error_Msg_NE
8266 ("invalid address clause for initialized object &!",
8267 Nod, U_Ent);
8268 Error_Msg_N
8269 ("\address cannot depend on component" &
8270 " of discriminated record (RM 13.1(22))!",
8271 Nod);
8272 else
8273 Check_At_Constant_Address (Prefix (Nod));
8274 end if;
8275 end;
8276
8277 elsif Nkind (Nod) = N_Indexed_Component then
8278 Check_At_Constant_Address (Prefix (Nod));
8279 Check_List_Constants (Expressions (Nod));
8280
8281 else
8282 Check_Expr_Constants (Nod);
8283 end if;
8284 end Check_At_Constant_Address;
8285
8286 --------------------------
8287 -- Check_Expr_Constants --
8288 --------------------------
8289
8290 procedure Check_Expr_Constants (Nod : Node_Id) is
8291 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8292 Ent : Entity_Id := Empty;
8293
8294 begin
8295 if Nkind (Nod) in N_Has_Etype
8296 and then Etype (Nod) = Any_Type
8297 then
8298 return;
8299 end if;
8300
8301 case Nkind (Nod) is
8302 when N_Empty | N_Error =>
8303 return;
8304
8305 when N_Identifier | N_Expanded_Name =>
8306 Ent := Entity (Nod);
8307
8308 -- We need to look at the original node if it is different
8309 -- from the node, since we may have rewritten things and
8310 -- substituted an identifier representing the rewrite.
8311
8312 if Original_Node (Nod) /= Nod then
8313 Check_Expr_Constants (Original_Node (Nod));
8314
8315 -- If the node is an object declaration without initial
8316 -- value, some code has been expanded, and the expression
8317 -- is not constant, even if the constituents might be
8318 -- acceptable, as in A'Address + offset.
8319
8320 if Ekind (Ent) = E_Variable
8321 and then
8322 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8323 and then
8324 No (Expression (Declaration_Node (Ent)))
8325 then
8326 Error_Msg_NE
8327 ("invalid address clause for initialized object &!",
8328 Nod, U_Ent);
8329
8330 -- If entity is constant, it may be the result of expanding
8331 -- a check. We must verify that its declaration appears
8332 -- before the object in question, else we also reject the
8333 -- address clause.
8334
8335 elsif Ekind (Ent) = E_Constant
8336 and then In_Same_Source_Unit (Ent, U_Ent)
8337 and then Sloc (Ent) > Loc_U_Ent
8338 then
8339 Error_Msg_NE
8340 ("invalid address clause for initialized object &!",
8341 Nod, U_Ent);
8342 end if;
8343
8344 return;
8345 end if;
8346
8347 -- Otherwise look at the identifier and see if it is OK
8348
8349 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8350 or else Is_Type (Ent)
8351 then
8352 return;
8353
8354 elsif
8355 Ekind (Ent) = E_Constant
8356 or else
8357 Ekind (Ent) = E_In_Parameter
8358 then
8359 -- This is the case where we must have Ent defined before
8360 -- U_Ent. Clearly if they are in different units this
8361 -- requirement is met since the unit containing Ent is
8362 -- already processed.
8363
8364 if not In_Same_Source_Unit (Ent, U_Ent) then
8365 return;
8366
8367 -- Otherwise location of Ent must be before the location
8368 -- of U_Ent, that's what prior defined means.
8369
8370 elsif Sloc (Ent) < Loc_U_Ent then
8371 return;
8372
8373 else
8374 Error_Msg_NE
8375 ("invalid address clause for initialized object &!",
8376 Nod, U_Ent);
8377 Error_Msg_Node_2 := U_Ent;
8378 Error_Msg_NE
8379 ("\& must be defined before & (RM 13.1(22))!",
8380 Nod, Ent);
8381 end if;
8382
8383 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
8384 Check_Expr_Constants (Original_Node (Nod));
8385
8386 else
8387 Error_Msg_NE
8388 ("invalid address clause for initialized object &!",
8389 Nod, U_Ent);
8390
8391 if Comes_From_Source (Ent) then
8392 Error_Msg_NE
8393 ("\reference to variable& not allowed"
8394 & " (RM 13.1(22))!", Nod, Ent);
8395 else
8396 Error_Msg_N
8397 ("non-static expression not allowed"
8398 & " (RM 13.1(22))!", Nod);
8399 end if;
8400 end if;
8401
8402 when N_Integer_Literal =>
8403
8404 -- If this is a rewritten unchecked conversion, in a system
8405 -- where Address is an integer type, always use the base type
8406 -- for a literal value. This is user-friendly and prevents
8407 -- order-of-elaboration issues with instances of unchecked
8408 -- conversion.
8409
8410 if Nkind (Original_Node (Nod)) = N_Function_Call then
8411 Set_Etype (Nod, Base_Type (Etype (Nod)));
8412 end if;
8413
8414 when N_Real_Literal |
8415 N_String_Literal |
8416 N_Character_Literal =>
8417 return;
8418
8419 when N_Range =>
8420 Check_Expr_Constants (Low_Bound (Nod));
8421 Check_Expr_Constants (High_Bound (Nod));
8422
8423 when N_Explicit_Dereference =>
8424 Check_Expr_Constants (Prefix (Nod));
8425
8426 when N_Indexed_Component =>
8427 Check_Expr_Constants (Prefix (Nod));
8428 Check_List_Constants (Expressions (Nod));
8429
8430 when N_Slice =>
8431 Check_Expr_Constants (Prefix (Nod));
8432 Check_Expr_Constants (Discrete_Range (Nod));
8433
8434 when N_Selected_Component =>
8435 Check_Expr_Constants (Prefix (Nod));
8436
8437 when N_Attribute_Reference =>
8438 if Nam_In (Attribute_Name (Nod), Name_Address,
8439 Name_Access,
8440 Name_Unchecked_Access,
8441 Name_Unrestricted_Access)
8442 then
8443 Check_At_Constant_Address (Prefix (Nod));
8444
8445 else
8446 Check_Expr_Constants (Prefix (Nod));
8447 Check_List_Constants (Expressions (Nod));
8448 end if;
8449
8450 when N_Aggregate =>
8451 Check_List_Constants (Component_Associations (Nod));
8452 Check_List_Constants (Expressions (Nod));
8453
8454 when N_Component_Association =>
8455 Check_Expr_Constants (Expression (Nod));
8456
8457 when N_Extension_Aggregate =>
8458 Check_Expr_Constants (Ancestor_Part (Nod));
8459 Check_List_Constants (Component_Associations (Nod));
8460 Check_List_Constants (Expressions (Nod));
8461
8462 when N_Null =>
8463 return;
8464
8465 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
8466 Check_Expr_Constants (Left_Opnd (Nod));
8467 Check_Expr_Constants (Right_Opnd (Nod));
8468
8469 when N_Unary_Op =>
8470 Check_Expr_Constants (Right_Opnd (Nod));
8471
8472 when N_Type_Conversion |
8473 N_Qualified_Expression |
8474 N_Allocator |
8475 N_Unchecked_Type_Conversion =>
8476 Check_Expr_Constants (Expression (Nod));
8477
8478 when N_Function_Call =>
8479 if not Is_Pure (Entity (Name (Nod))) then
8480 Error_Msg_NE
8481 ("invalid address clause for initialized object &!",
8482 Nod, U_Ent);
8483
8484 Error_Msg_NE
8485 ("\function & is not pure (RM 13.1(22))!",
8486 Nod, Entity (Name (Nod)));
8487
8488 else
8489 Check_List_Constants (Parameter_Associations (Nod));
8490 end if;
8491
8492 when N_Parameter_Association =>
8493 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
8494
8495 when others =>
8496 Error_Msg_NE
8497 ("invalid address clause for initialized object &!",
8498 Nod, U_Ent);
8499 Error_Msg_NE
8500 ("\must be constant defined before& (RM 13.1(22))!",
8501 Nod, U_Ent);
8502 end case;
8503 end Check_Expr_Constants;
8504
8505 --------------------------
8506 -- Check_List_Constants --
8507 --------------------------
8508
8509 procedure Check_List_Constants (Lst : List_Id) is
8510 Nod1 : Node_Id;
8511
8512 begin
8513 if Present (Lst) then
8514 Nod1 := First (Lst);
8515 while Present (Nod1) loop
8516 Check_Expr_Constants (Nod1);
8517 Next (Nod1);
8518 end loop;
8519 end if;
8520 end Check_List_Constants;
8521
8522 -- Start of processing for Check_Constant_Address_Clause
8523
8524 begin
8525 -- If rep_clauses are to be ignored, no need for legality checks. In
8526 -- particular, no need to pester user about rep clauses that violate
8527 -- the rule on constant addresses, given that these clauses will be
8528 -- removed by Freeze before they reach the back end.
8529
8530 if not Ignore_Rep_Clauses then
8531 Check_Expr_Constants (Expr);
8532 end if;
8533 end Check_Constant_Address_Clause;
8534
8535 ---------------------------
8536 -- Check_Pool_Size_Clash --
8537 ---------------------------
8538
8539 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
8540 Post : Node_Id;
8541
8542 begin
8543 -- We need to find out which one came first. Note that in the case of
8544 -- aspects mixed with pragmas there are cases where the processing order
8545 -- is reversed, which is why we do the check here.
8546
8547 if Sloc (SP) < Sloc (SS) then
8548 Error_Msg_Sloc := Sloc (SP);
8549 Post := SS;
8550 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
8551
8552 else
8553 Error_Msg_Sloc := Sloc (SS);
8554 Post := SP;
8555 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
8556 end if;
8557
8558 Error_Msg_N
8559 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
8560 end Check_Pool_Size_Clash;
8561
8562 ----------------------------------------
8563 -- Check_Record_Representation_Clause --
8564 ----------------------------------------
8565
8566 procedure Check_Record_Representation_Clause (N : Node_Id) is
8567 Loc : constant Source_Ptr := Sloc (N);
8568 Ident : constant Node_Id := Identifier (N);
8569 Rectype : Entity_Id;
8570 Fent : Entity_Id;
8571 CC : Node_Id;
8572 Fbit : Uint;
8573 Lbit : Uint;
8574 Hbit : Uint := Uint_0;
8575 Comp : Entity_Id;
8576 Pcomp : Entity_Id;
8577
8578 Max_Bit_So_Far : Uint;
8579 -- Records the maximum bit position so far. If all field positions
8580 -- are monotonically increasing, then we can skip the circuit for
8581 -- checking for overlap, since no overlap is possible.
8582
8583 Tagged_Parent : Entity_Id := Empty;
8584 -- This is set in the case of a derived tagged type for which we have
8585 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
8586 -- positioned by record representation clauses). In this case we must
8587 -- check for overlap between components of this tagged type, and the
8588 -- components of its parent. Tagged_Parent will point to this parent
8589 -- type. For all other cases Tagged_Parent is left set to Empty.
8590
8591 Parent_Last_Bit : Uint;
8592 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
8593 -- last bit position for any field in the parent type. We only need to
8594 -- check overlap for fields starting below this point.
8595
8596 Overlap_Check_Required : Boolean;
8597 -- Used to keep track of whether or not an overlap check is required
8598
8599 Overlap_Detected : Boolean := False;
8600 -- Set True if an overlap is detected
8601
8602 Ccount : Natural := 0;
8603 -- Number of component clauses in record rep clause
8604
8605 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
8606 -- Given two entities for record components or discriminants, checks
8607 -- if they have overlapping component clauses and issues errors if so.
8608
8609 procedure Find_Component;
8610 -- Finds component entity corresponding to current component clause (in
8611 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
8612 -- start/stop bits for the field. If there is no matching component or
8613 -- if the matching component does not have a component clause, then
8614 -- that's an error and Comp is set to Empty, but no error message is
8615 -- issued, since the message was already given. Comp is also set to
8616 -- Empty if the current "component clause" is in fact a pragma.
8617
8618 -----------------------------
8619 -- Check_Component_Overlap --
8620 -----------------------------
8621
8622 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
8623 CC1 : constant Node_Id := Component_Clause (C1_Ent);
8624 CC2 : constant Node_Id := Component_Clause (C2_Ent);
8625
8626 begin
8627 if Present (CC1) and then Present (CC2) then
8628
8629 -- Exclude odd case where we have two tag components in the same
8630 -- record, both at location zero. This seems a bit strange, but
8631 -- it seems to happen in some circumstances, perhaps on an error.
8632
8633 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
8634 return;
8635 end if;
8636
8637 -- Here we check if the two fields overlap
8638
8639 declare
8640 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
8641 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
8642 E1 : constant Uint := S1 + Esize (C1_Ent);
8643 E2 : constant Uint := S2 + Esize (C2_Ent);
8644
8645 begin
8646 if E2 <= S1 or else E1 <= S2 then
8647 null;
8648 else
8649 Error_Msg_Node_2 := Component_Name (CC2);
8650 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
8651 Error_Msg_Node_1 := Component_Name (CC1);
8652 Error_Msg_N
8653 ("component& overlaps & #", Component_Name (CC1));
8654 Overlap_Detected := True;
8655 end if;
8656 end;
8657 end if;
8658 end Check_Component_Overlap;
8659
8660 --------------------
8661 -- Find_Component --
8662 --------------------
8663
8664 procedure Find_Component is
8665
8666 procedure Search_Component (R : Entity_Id);
8667 -- Search components of R for a match. If found, Comp is set
8668
8669 ----------------------
8670 -- Search_Component --
8671 ----------------------
8672
8673 procedure Search_Component (R : Entity_Id) is
8674 begin
8675 Comp := First_Component_Or_Discriminant (R);
8676 while Present (Comp) loop
8677
8678 -- Ignore error of attribute name for component name (we
8679 -- already gave an error message for this, so no need to
8680 -- complain here)
8681
8682 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
8683 null;
8684 else
8685 exit when Chars (Comp) = Chars (Component_Name (CC));
8686 end if;
8687
8688 Next_Component_Or_Discriminant (Comp);
8689 end loop;
8690 end Search_Component;
8691
8692 -- Start of processing for Find_Component
8693
8694 begin
8695 -- Return with Comp set to Empty if we have a pragma
8696
8697 if Nkind (CC) = N_Pragma then
8698 Comp := Empty;
8699 return;
8700 end if;
8701
8702 -- Search current record for matching component
8703
8704 Search_Component (Rectype);
8705
8706 -- If not found, maybe component of base type discriminant that is
8707 -- absent from statically constrained first subtype.
8708
8709 if No (Comp) then
8710 Search_Component (Base_Type (Rectype));
8711 end if;
8712
8713 -- If no component, or the component does not reference the component
8714 -- clause in question, then there was some previous error for which
8715 -- we already gave a message, so just return with Comp Empty.
8716
8717 if No (Comp) or else Component_Clause (Comp) /= CC then
8718 Check_Error_Detected;
8719 Comp := Empty;
8720
8721 -- Normal case where we have a component clause
8722
8723 else
8724 Fbit := Component_Bit_Offset (Comp);
8725 Lbit := Fbit + Esize (Comp) - 1;
8726 end if;
8727 end Find_Component;
8728
8729 -- Start of processing for Check_Record_Representation_Clause
8730
8731 begin
8732 Find_Type (Ident);
8733 Rectype := Entity (Ident);
8734
8735 if Rectype = Any_Type then
8736 return;
8737 else
8738 Rectype := Underlying_Type (Rectype);
8739 end if;
8740
8741 -- See if we have a fully repped derived tagged type
8742
8743 declare
8744 PS : constant Entity_Id := Parent_Subtype (Rectype);
8745
8746 begin
8747 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
8748 Tagged_Parent := PS;
8749
8750 -- Find maximum bit of any component of the parent type
8751
8752 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
8753 Pcomp := First_Entity (Tagged_Parent);
8754 while Present (Pcomp) loop
8755 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
8756 if Component_Bit_Offset (Pcomp) /= No_Uint
8757 and then Known_Static_Esize (Pcomp)
8758 then
8759 Parent_Last_Bit :=
8760 UI_Max
8761 (Parent_Last_Bit,
8762 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
8763 end if;
8764
8765 Next_Entity (Pcomp);
8766 end if;
8767 end loop;
8768 end if;
8769 end;
8770
8771 -- All done if no component clauses
8772
8773 CC := First (Component_Clauses (N));
8774
8775 if No (CC) then
8776 return;
8777 end if;
8778
8779 -- If a tag is present, then create a component clause that places it
8780 -- at the start of the record (otherwise gigi may place it after other
8781 -- fields that have rep clauses).
8782
8783 Fent := First_Entity (Rectype);
8784
8785 if Nkind (Fent) = N_Defining_Identifier
8786 and then Chars (Fent) = Name_uTag
8787 then
8788 Set_Component_Bit_Offset (Fent, Uint_0);
8789 Set_Normalized_Position (Fent, Uint_0);
8790 Set_Normalized_First_Bit (Fent, Uint_0);
8791 Set_Normalized_Position_Max (Fent, Uint_0);
8792 Init_Esize (Fent, System_Address_Size);
8793
8794 Set_Component_Clause (Fent,
8795 Make_Component_Clause (Loc,
8796 Component_Name => Make_Identifier (Loc, Name_uTag),
8797
8798 Position => Make_Integer_Literal (Loc, Uint_0),
8799 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8800 Last_Bit =>
8801 Make_Integer_Literal (Loc,
8802 UI_From_Int (System_Address_Size))));
8803
8804 Ccount := Ccount + 1;
8805 end if;
8806
8807 Max_Bit_So_Far := Uint_Minus_1;
8808 Overlap_Check_Required := False;
8809
8810 -- Process the component clauses
8811
8812 while Present (CC) loop
8813 Find_Component;
8814
8815 if Present (Comp) then
8816 Ccount := Ccount + 1;
8817
8818 -- We need a full overlap check if record positions non-monotonic
8819
8820 if Fbit <= Max_Bit_So_Far then
8821 Overlap_Check_Required := True;
8822 end if;
8823
8824 Max_Bit_So_Far := Lbit;
8825
8826 -- Check bit position out of range of specified size
8827
8828 if Has_Size_Clause (Rectype)
8829 and then RM_Size (Rectype) <= Lbit
8830 then
8831 Error_Msg_N
8832 ("bit number out of range of specified size",
8833 Last_Bit (CC));
8834
8835 -- Check for overlap with tag component
8836
8837 else
8838 if Is_Tagged_Type (Rectype)
8839 and then Fbit < System_Address_Size
8840 then
8841 Error_Msg_NE
8842 ("component overlaps tag field of&",
8843 Component_Name (CC), Rectype);
8844 Overlap_Detected := True;
8845 end if;
8846
8847 if Hbit < Lbit then
8848 Hbit := Lbit;
8849 end if;
8850 end if;
8851
8852 -- Check parent overlap if component might overlap parent field
8853
8854 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
8855 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
8856 while Present (Pcomp) loop
8857 if not Is_Tag (Pcomp)
8858 and then Chars (Pcomp) /= Name_uParent
8859 then
8860 Check_Component_Overlap (Comp, Pcomp);
8861 end if;
8862
8863 Next_Component_Or_Discriminant (Pcomp);
8864 end loop;
8865 end if;
8866 end if;
8867
8868 Next (CC);
8869 end loop;
8870
8871 -- Now that we have processed all the component clauses, check for
8872 -- overlap. We have to leave this till last, since the components can
8873 -- appear in any arbitrary order in the representation clause.
8874
8875 -- We do not need this check if all specified ranges were monotonic,
8876 -- as recorded by Overlap_Check_Required being False at this stage.
8877
8878 -- This first section checks if there are any overlapping entries at
8879 -- all. It does this by sorting all entries and then seeing if there are
8880 -- any overlaps. If there are none, then that is decisive, but if there
8881 -- are overlaps, they may still be OK (they may result from fields in
8882 -- different variants).
8883
8884 if Overlap_Check_Required then
8885 Overlap_Check1 : declare
8886
8887 OC_Fbit : array (0 .. Ccount) of Uint;
8888 -- First-bit values for component clauses, the value is the offset
8889 -- of the first bit of the field from start of record. The zero
8890 -- entry is for use in sorting.
8891
8892 OC_Lbit : array (0 .. Ccount) of Uint;
8893 -- Last-bit values for component clauses, the value is the offset
8894 -- of the last bit of the field from start of record. The zero
8895 -- entry is for use in sorting.
8896
8897 OC_Count : Natural := 0;
8898 -- Count of entries in OC_Fbit and OC_Lbit
8899
8900 function OC_Lt (Op1, Op2 : Natural) return Boolean;
8901 -- Compare routine for Sort
8902
8903 procedure OC_Move (From : Natural; To : Natural);
8904 -- Move routine for Sort
8905
8906 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8907
8908 -----------
8909 -- OC_Lt --
8910 -----------
8911
8912 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8913 begin
8914 return OC_Fbit (Op1) < OC_Fbit (Op2);
8915 end OC_Lt;
8916
8917 -------------
8918 -- OC_Move --
8919 -------------
8920
8921 procedure OC_Move (From : Natural; To : Natural) is
8922 begin
8923 OC_Fbit (To) := OC_Fbit (From);
8924 OC_Lbit (To) := OC_Lbit (From);
8925 end OC_Move;
8926
8927 -- Start of processing for Overlap_Check
8928
8929 begin
8930 CC := First (Component_Clauses (N));
8931 while Present (CC) loop
8932
8933 -- Exclude component clause already marked in error
8934
8935 if not Error_Posted (CC) then
8936 Find_Component;
8937
8938 if Present (Comp) then
8939 OC_Count := OC_Count + 1;
8940 OC_Fbit (OC_Count) := Fbit;
8941 OC_Lbit (OC_Count) := Lbit;
8942 end if;
8943 end if;
8944
8945 Next (CC);
8946 end loop;
8947
8948 Sorting.Sort (OC_Count);
8949
8950 Overlap_Check_Required := False;
8951 for J in 1 .. OC_Count - 1 loop
8952 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8953 Overlap_Check_Required := True;
8954 exit;
8955 end if;
8956 end loop;
8957 end Overlap_Check1;
8958 end if;
8959
8960 -- If Overlap_Check_Required is still True, then we have to do the full
8961 -- scale overlap check, since we have at least two fields that do
8962 -- overlap, and we need to know if that is OK since they are in
8963 -- different variant, or whether we have a definite problem.
8964
8965 if Overlap_Check_Required then
8966 Overlap_Check2 : declare
8967 C1_Ent, C2_Ent : Entity_Id;
8968 -- Entities of components being checked for overlap
8969
8970 Clist : Node_Id;
8971 -- Component_List node whose Component_Items are being checked
8972
8973 Citem : Node_Id;
8974 -- Component declaration for component being checked
8975
8976 begin
8977 C1_Ent := First_Entity (Base_Type (Rectype));
8978
8979 -- Loop through all components in record. For each component check
8980 -- for overlap with any of the preceding elements on the component
8981 -- list containing the component and also, if the component is in
8982 -- a variant, check against components outside the case structure.
8983 -- This latter test is repeated recursively up the variant tree.
8984
8985 Main_Component_Loop : while Present (C1_Ent) loop
8986 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8987 goto Continue_Main_Component_Loop;
8988 end if;
8989
8990 -- Skip overlap check if entity has no declaration node. This
8991 -- happens with discriminants in constrained derived types.
8992 -- Possibly we are missing some checks as a result, but that
8993 -- does not seem terribly serious.
8994
8995 if No (Declaration_Node (C1_Ent)) then
8996 goto Continue_Main_Component_Loop;
8997 end if;
8998
8999 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9000
9001 -- Loop through component lists that need checking. Check the
9002 -- current component list and all lists in variants above us.
9003
9004 Component_List_Loop : loop
9005
9006 -- If derived type definition, go to full declaration
9007 -- If at outer level, check discriminants if there are any.
9008
9009 if Nkind (Clist) = N_Derived_Type_Definition then
9010 Clist := Parent (Clist);
9011 end if;
9012
9013 -- Outer level of record definition, check discriminants
9014
9015 if Nkind_In (Clist, N_Full_Type_Declaration,
9016 N_Private_Type_Declaration)
9017 then
9018 if Has_Discriminants (Defining_Identifier (Clist)) then
9019 C2_Ent :=
9020 First_Discriminant (Defining_Identifier (Clist));
9021 while Present (C2_Ent) loop
9022 exit when C1_Ent = C2_Ent;
9023 Check_Component_Overlap (C1_Ent, C2_Ent);
9024 Next_Discriminant (C2_Ent);
9025 end loop;
9026 end if;
9027
9028 -- Record extension case
9029
9030 elsif Nkind (Clist) = N_Derived_Type_Definition then
9031 Clist := Empty;
9032
9033 -- Otherwise check one component list
9034
9035 else
9036 Citem := First (Component_Items (Clist));
9037 while Present (Citem) loop
9038 if Nkind (Citem) = N_Component_Declaration then
9039 C2_Ent := Defining_Identifier (Citem);
9040 exit when C1_Ent = C2_Ent;
9041 Check_Component_Overlap (C1_Ent, C2_Ent);
9042 end if;
9043
9044 Next (Citem);
9045 end loop;
9046 end if;
9047
9048 -- Check for variants above us (the parent of the Clist can
9049 -- be a variant, in which case its parent is a variant part,
9050 -- and the parent of the variant part is a component list
9051 -- whose components must all be checked against the current
9052 -- component for overlap).
9053
9054 if Nkind (Parent (Clist)) = N_Variant then
9055 Clist := Parent (Parent (Parent (Clist)));
9056
9057 -- Check for possible discriminant part in record, this
9058 -- is treated essentially as another level in the
9059 -- recursion. For this case the parent of the component
9060 -- list is the record definition, and its parent is the
9061 -- full type declaration containing the discriminant
9062 -- specifications.
9063
9064 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9065 Clist := Parent (Parent ((Clist)));
9066
9067 -- If neither of these two cases, we are at the top of
9068 -- the tree.
9069
9070 else
9071 exit Component_List_Loop;
9072 end if;
9073 end loop Component_List_Loop;
9074
9075 <<Continue_Main_Component_Loop>>
9076 Next_Entity (C1_Ent);
9077
9078 end loop Main_Component_Loop;
9079 end Overlap_Check2;
9080 end if;
9081
9082 -- The following circuit deals with warning on record holes (gaps). We
9083 -- skip this check if overlap was detected, since it makes sense for the
9084 -- programmer to fix this illegality before worrying about warnings.
9085
9086 if not Overlap_Detected and Warn_On_Record_Holes then
9087 Record_Hole_Check : declare
9088 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9089 -- Full declaration of record type
9090
9091 procedure Check_Component_List
9092 (CL : Node_Id;
9093 Sbit : Uint;
9094 DS : List_Id);
9095 -- Check component list CL for holes. The starting bit should be
9096 -- Sbit. which is zero for the main record component list and set
9097 -- appropriately for recursive calls for variants. DS is set to
9098 -- a list of discriminant specifications to be included in the
9099 -- consideration of components. It is No_List if none to consider.
9100
9101 --------------------------
9102 -- Check_Component_List --
9103 --------------------------
9104
9105 procedure Check_Component_List
9106 (CL : Node_Id;
9107 Sbit : Uint;
9108 DS : List_Id)
9109 is
9110 Compl : Integer;
9111
9112 begin
9113 Compl := Integer (List_Length (Component_Items (CL)));
9114
9115 if DS /= No_List then
9116 Compl := Compl + Integer (List_Length (DS));
9117 end if;
9118
9119 declare
9120 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9121 -- Gather components (zero entry is for sort routine)
9122
9123 Ncomps : Natural := 0;
9124 -- Number of entries stored in Comps (starting at Comps (1))
9125
9126 Citem : Node_Id;
9127 -- One component item or discriminant specification
9128
9129 Nbit : Uint;
9130 -- Starting bit for next component
9131
9132 CEnt : Entity_Id;
9133 -- Component entity
9134
9135 Variant : Node_Id;
9136 -- One variant
9137
9138 function Lt (Op1, Op2 : Natural) return Boolean;
9139 -- Compare routine for Sort
9140
9141 procedure Move (From : Natural; To : Natural);
9142 -- Move routine for Sort
9143
9144 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9145
9146 --------
9147 -- Lt --
9148 --------
9149
9150 function Lt (Op1, Op2 : Natural) return Boolean is
9151 begin
9152 return Component_Bit_Offset (Comps (Op1))
9153 <
9154 Component_Bit_Offset (Comps (Op2));
9155 end Lt;
9156
9157 ----------
9158 -- Move --
9159 ----------
9160
9161 procedure Move (From : Natural; To : Natural) is
9162 begin
9163 Comps (To) := Comps (From);
9164 end Move;
9165
9166 begin
9167 -- Gather discriminants into Comp
9168
9169 if DS /= No_List then
9170 Citem := First (DS);
9171 while Present (Citem) loop
9172 if Nkind (Citem) = N_Discriminant_Specification then
9173 declare
9174 Ent : constant Entity_Id :=
9175 Defining_Identifier (Citem);
9176 begin
9177 if Ekind (Ent) = E_Discriminant then
9178 Ncomps := Ncomps + 1;
9179 Comps (Ncomps) := Ent;
9180 end if;
9181 end;
9182 end if;
9183
9184 Next (Citem);
9185 end loop;
9186 end if;
9187
9188 -- Gather component entities into Comp
9189
9190 Citem := First (Component_Items (CL));
9191 while Present (Citem) loop
9192 if Nkind (Citem) = N_Component_Declaration then
9193 Ncomps := Ncomps + 1;
9194 Comps (Ncomps) := Defining_Identifier (Citem);
9195 end if;
9196
9197 Next (Citem);
9198 end loop;
9199
9200 -- Now sort the component entities based on the first bit.
9201 -- Note we already know there are no overlapping components.
9202
9203 Sorting.Sort (Ncomps);
9204
9205 -- Loop through entries checking for holes
9206
9207 Nbit := Sbit;
9208 for J in 1 .. Ncomps loop
9209 CEnt := Comps (J);
9210 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9211
9212 if Error_Msg_Uint_1 > 0 then
9213 Error_Msg_NE
9214 ("?H?^-bit gap before component&",
9215 Component_Name (Component_Clause (CEnt)), CEnt);
9216 end if;
9217
9218 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9219 end loop;
9220
9221 -- Process variant parts recursively if present
9222
9223 if Present (Variant_Part (CL)) then
9224 Variant := First (Variants (Variant_Part (CL)));
9225 while Present (Variant) loop
9226 Check_Component_List
9227 (Component_List (Variant), Nbit, No_List);
9228 Next (Variant);
9229 end loop;
9230 end if;
9231 end;
9232 end Check_Component_List;
9233
9234 -- Start of processing for Record_Hole_Check
9235
9236 begin
9237 declare
9238 Sbit : Uint;
9239
9240 begin
9241 if Is_Tagged_Type (Rectype) then
9242 Sbit := UI_From_Int (System_Address_Size);
9243 else
9244 Sbit := Uint_0;
9245 end if;
9246
9247 if Nkind (Decl) = N_Full_Type_Declaration
9248 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9249 then
9250 Check_Component_List
9251 (Component_List (Type_Definition (Decl)),
9252 Sbit,
9253 Discriminant_Specifications (Decl));
9254 end if;
9255 end;
9256 end Record_Hole_Check;
9257 end if;
9258
9259 -- For records that have component clauses for all components, and whose
9260 -- size is less than or equal to 32, we need to know the size in the
9261 -- front end to activate possible packed array processing where the
9262 -- component type is a record.
9263
9264 -- At this stage Hbit + 1 represents the first unused bit from all the
9265 -- component clauses processed, so if the component clauses are
9266 -- complete, then this is the length of the record.
9267
9268 -- For records longer than System.Storage_Unit, and for those where not
9269 -- all components have component clauses, the back end determines the
9270 -- length (it may for example be appropriate to round up the size
9271 -- to some convenient boundary, based on alignment considerations, etc).
9272
9273 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9274
9275 -- Nothing to do if at least one component has no component clause
9276
9277 Comp := First_Component_Or_Discriminant (Rectype);
9278 while Present (Comp) loop
9279 exit when No (Component_Clause (Comp));
9280 Next_Component_Or_Discriminant (Comp);
9281 end loop;
9282
9283 -- If we fall out of loop, all components have component clauses
9284 -- and so we can set the size to the maximum value.
9285
9286 if No (Comp) then
9287 Set_RM_Size (Rectype, Hbit + 1);
9288 end if;
9289 end if;
9290 end Check_Record_Representation_Clause;
9291
9292 ----------------
9293 -- Check_Size --
9294 ----------------
9295
9296 procedure Check_Size
9297 (N : Node_Id;
9298 T : Entity_Id;
9299 Siz : Uint;
9300 Biased : out Boolean)
9301 is
9302 UT : constant Entity_Id := Underlying_Type (T);
9303 M : Uint;
9304
9305 begin
9306 Biased := False;
9307
9308 -- Reject patently improper size values.
9309
9310 if Is_Elementary_Type (T)
9311 and then Siz > UI_From_Int (Int'Last)
9312 then
9313 Error_Msg_N ("Size value too large for elementary type", N);
9314
9315 if Nkind (Original_Node (N)) = N_Op_Expon then
9316 Error_Msg_N
9317 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9318 end if;
9319 end if;
9320
9321 -- Dismiss generic types
9322
9323 if Is_Generic_Type (T)
9324 or else
9325 Is_Generic_Type (UT)
9326 or else
9327 Is_Generic_Type (Root_Type (UT))
9328 then
9329 return;
9330
9331 -- Guard against previous errors
9332
9333 elsif No (UT) or else UT = Any_Type then
9334 Check_Error_Detected;
9335 return;
9336
9337 -- Check case of bit packed array
9338
9339 elsif Is_Array_Type (UT)
9340 and then Known_Static_Component_Size (UT)
9341 and then Is_Bit_Packed_Array (UT)
9342 then
9343 declare
9344 Asiz : Uint;
9345 Indx : Node_Id;
9346 Ityp : Entity_Id;
9347
9348 begin
9349 Asiz := Component_Size (UT);
9350 Indx := First_Index (UT);
9351 loop
9352 Ityp := Etype (Indx);
9353
9354 -- If non-static bound, then we are not in the business of
9355 -- trying to check the length, and indeed an error will be
9356 -- issued elsewhere, since sizes of non-static array types
9357 -- cannot be set implicitly or explicitly.
9358
9359 if not Is_Static_Subtype (Ityp) then
9360 return;
9361 end if;
9362
9363 -- Otherwise accumulate next dimension
9364
9365 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9366 Expr_Value (Type_Low_Bound (Ityp)) +
9367 Uint_1);
9368
9369 Next_Index (Indx);
9370 exit when No (Indx);
9371 end loop;
9372
9373 if Asiz <= Siz then
9374 return;
9375
9376 else
9377 Error_Msg_Uint_1 := Asiz;
9378 Error_Msg_NE
9379 ("size for& too small, minimum allowed is ^", N, T);
9380 Set_Esize (T, Asiz);
9381 Set_RM_Size (T, Asiz);
9382 end if;
9383 end;
9384
9385 -- All other composite types are ignored
9386
9387 elsif Is_Composite_Type (UT) then
9388 return;
9389
9390 -- For fixed-point types, don't check minimum if type is not frozen,
9391 -- since we don't know all the characteristics of the type that can
9392 -- affect the size (e.g. a specified small) till freeze time.
9393
9394 elsif Is_Fixed_Point_Type (UT)
9395 and then not Is_Frozen (UT)
9396 then
9397 null;
9398
9399 -- Cases for which a minimum check is required
9400
9401 else
9402 -- Ignore if specified size is correct for the type
9403
9404 if Known_Esize (UT) and then Siz = Esize (UT) then
9405 return;
9406 end if;
9407
9408 -- Otherwise get minimum size
9409
9410 M := UI_From_Int (Minimum_Size (UT));
9411
9412 if Siz < M then
9413
9414 -- Size is less than minimum size, but one possibility remains
9415 -- that we can manage with the new size if we bias the type.
9416
9417 M := UI_From_Int (Minimum_Size (UT, Biased => True));
9418
9419 if Siz < M then
9420 Error_Msg_Uint_1 := M;
9421 Error_Msg_NE
9422 ("size for& too small, minimum allowed is ^", N, T);
9423 Set_Esize (T, M);
9424 Set_RM_Size (T, M);
9425 else
9426 Biased := True;
9427 end if;
9428 end if;
9429 end if;
9430 end Check_Size;
9431
9432 --------------------------
9433 -- Freeze_Entity_Checks --
9434 --------------------------
9435
9436 procedure Freeze_Entity_Checks (N : Node_Id) is
9437 E : constant Entity_Id := Entity (N);
9438
9439 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
9440 -- True in non-generic case. Some of the processing here is skipped
9441 -- for the generic case since it is not needed. Basically in the
9442 -- generic case, we only need to do stuff that might generate error
9443 -- messages or warnings.
9444 begin
9445 -- Remember that we are processing a freezing entity. Required to
9446 -- ensure correct decoration of internal entities associated with
9447 -- interfaces (see New_Overloaded_Entity).
9448
9449 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
9450
9451 -- For tagged types covering interfaces add internal entities that link
9452 -- the primitives of the interfaces with the primitives that cover them.
9453 -- Note: These entities were originally generated only when generating
9454 -- code because their main purpose was to provide support to initialize
9455 -- the secondary dispatch tables. They are now generated also when
9456 -- compiling with no code generation to provide ASIS the relationship
9457 -- between interface primitives and tagged type primitives. They are
9458 -- also used to locate primitives covering interfaces when processing
9459 -- generics (see Derive_Subprograms).
9460
9461 -- This is not needed in the generic case
9462
9463 if Ada_Version >= Ada_2005
9464 and then Non_Generic_Case
9465 and then Ekind (E) = E_Record_Type
9466 and then Is_Tagged_Type (E)
9467 and then not Is_Interface (E)
9468 and then Has_Interfaces (E)
9469 then
9470 -- This would be a good common place to call the routine that checks
9471 -- overriding of interface primitives (and thus factorize calls to
9472 -- Check_Abstract_Overriding located at different contexts in the
9473 -- compiler). However, this is not possible because it causes
9474 -- spurious errors in case of late overriding.
9475
9476 Add_Internal_Interface_Entities (E);
9477 end if;
9478
9479 -- Check CPP types
9480
9481 if Ekind (E) = E_Record_Type
9482 and then Is_CPP_Class (E)
9483 and then Is_Tagged_Type (E)
9484 and then Tagged_Type_Expansion
9485 then
9486 if CPP_Num_Prims (E) = 0 then
9487
9488 -- If the CPP type has user defined components then it must import
9489 -- primitives from C++. This is required because if the C++ class
9490 -- has no primitives then the C++ compiler does not added the _tag
9491 -- component to the type.
9492
9493 if First_Entity (E) /= Last_Entity (E) then
9494 Error_Msg_N
9495 ("'C'P'P type must import at least one primitive from C++??",
9496 E);
9497 end if;
9498 end if;
9499
9500 -- Check that all its primitives are abstract or imported from C++.
9501 -- Check also availability of the C++ constructor.
9502
9503 declare
9504 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
9505 Elmt : Elmt_Id;
9506 Error_Reported : Boolean := False;
9507 Prim : Node_Id;
9508
9509 begin
9510 Elmt := First_Elmt (Primitive_Operations (E));
9511 while Present (Elmt) loop
9512 Prim := Node (Elmt);
9513
9514 if Comes_From_Source (Prim) then
9515 if Is_Abstract_Subprogram (Prim) then
9516 null;
9517
9518 elsif not Is_Imported (Prim)
9519 or else Convention (Prim) /= Convention_CPP
9520 then
9521 Error_Msg_N
9522 ("primitives of 'C'P'P types must be imported from C++ "
9523 & "or abstract??", Prim);
9524
9525 elsif not Has_Constructors
9526 and then not Error_Reported
9527 then
9528 Error_Msg_Name_1 := Chars (E);
9529 Error_Msg_N
9530 ("??'C'P'P constructor required for type %", Prim);
9531 Error_Reported := True;
9532 end if;
9533 end if;
9534
9535 Next_Elmt (Elmt);
9536 end loop;
9537 end;
9538 end if;
9539
9540 -- Check Ada derivation of CPP type
9541
9542 if Expander_Active -- why? losing errors in -gnatc mode???
9543 and then Tagged_Type_Expansion
9544 and then Ekind (E) = E_Record_Type
9545 and then Etype (E) /= E
9546 and then Is_CPP_Class (Etype (E))
9547 and then CPP_Num_Prims (Etype (E)) > 0
9548 and then not Is_CPP_Class (E)
9549 and then not Has_CPP_Constructors (Etype (E))
9550 then
9551 -- If the parent has C++ primitives but it has no constructor then
9552 -- check that all the primitives are overridden in this derivation;
9553 -- otherwise the constructor of the parent is needed to build the
9554 -- dispatch table.
9555
9556 declare
9557 Elmt : Elmt_Id;
9558 Prim : Node_Id;
9559
9560 begin
9561 Elmt := First_Elmt (Primitive_Operations (E));
9562 while Present (Elmt) loop
9563 Prim := Node (Elmt);
9564
9565 if not Is_Abstract_Subprogram (Prim)
9566 and then No (Interface_Alias (Prim))
9567 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
9568 then
9569 Error_Msg_Name_1 := Chars (Etype (E));
9570 Error_Msg_N
9571 ("'C'P'P constructor required for parent type %", E);
9572 exit;
9573 end if;
9574
9575 Next_Elmt (Elmt);
9576 end loop;
9577 end;
9578 end if;
9579
9580 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
9581
9582 -- If we have a type with predicates, build predicate function. This
9583 -- is not needed in the generic case, and is not needed within TSS
9584 -- subprograms and other predefined primitives.
9585
9586 if Non_Generic_Case
9587 and then Is_Type (E)
9588 and then Has_Predicates (E)
9589 and then not Within_Internal_Subprogram
9590 then
9591 Build_Predicate_Functions (E, N);
9592 end if;
9593
9594 -- If type has delayed aspects, this is where we do the preanalysis at
9595 -- the freeze point, as part of the consistent visibility check. Note
9596 -- that this must be done after calling Build_Predicate_Functions or
9597 -- Build_Invariant_Procedure since these subprograms fix occurrences of
9598 -- the subtype name in the saved expression so that they will not cause
9599 -- trouble in the preanalysis.
9600
9601 -- This is also not needed in the generic case
9602
9603 if Non_Generic_Case
9604 and then Has_Delayed_Aspects (E)
9605 and then Scope (E) = Current_Scope
9606 then
9607 -- Retrieve the visibility to the discriminants in order to properly
9608 -- analyze the aspects.
9609
9610 Push_Scope_And_Install_Discriminants (E);
9611
9612 declare
9613 Ritem : Node_Id;
9614
9615 begin
9616 -- Look for aspect specification entries for this entity
9617
9618 Ritem := First_Rep_Item (E);
9619 while Present (Ritem) loop
9620 if Nkind (Ritem) = N_Aspect_Specification
9621 and then Entity (Ritem) = E
9622 and then Is_Delayed_Aspect (Ritem)
9623 then
9624 Check_Aspect_At_Freeze_Point (Ritem);
9625 end if;
9626
9627 Next_Rep_Item (Ritem);
9628 end loop;
9629 end;
9630
9631 Uninstall_Discriminants_And_Pop_Scope (E);
9632 end if;
9633
9634 -- For a record type, deal with variant parts. This has to be delayed
9635 -- to this point, because of the issue of statically precicated
9636 -- subtypes, which we have to ensure are frozen before checking
9637 -- choices, since we need to have the static choice list set.
9638
9639 if Is_Record_Type (E) then
9640 Check_Variant_Part : declare
9641 D : constant Node_Id := Declaration_Node (E);
9642 T : Node_Id;
9643 C : Node_Id;
9644 VP : Node_Id;
9645
9646 Others_Present : Boolean;
9647 pragma Warnings (Off, Others_Present);
9648 -- Indicates others present, not used in this case
9649
9650 procedure Non_Static_Choice_Error (Choice : Node_Id);
9651 -- Error routine invoked by the generic instantiation below when
9652 -- the variant part has a non static choice.
9653
9654 procedure Process_Declarations (Variant : Node_Id);
9655 -- Processes declarations associated with a variant. We analyzed
9656 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
9657 -- but we still need the recursive call to Check_Choices for any
9658 -- nested variant to get its choices properly processed. This is
9659 -- also where we expand out the choices if expansion is active.
9660
9661 package Variant_Choices_Processing is new
9662 Generic_Check_Choices
9663 (Process_Empty_Choice => No_OP,
9664 Process_Non_Static_Choice => Non_Static_Choice_Error,
9665 Process_Associated_Node => Process_Declarations);
9666 use Variant_Choices_Processing;
9667
9668 -----------------------------
9669 -- Non_Static_Choice_Error --
9670 -----------------------------
9671
9672 procedure Non_Static_Choice_Error (Choice : Node_Id) is
9673 begin
9674 Flag_Non_Static_Expr
9675 ("choice given in variant part is not static!", Choice);
9676 end Non_Static_Choice_Error;
9677
9678 --------------------------
9679 -- Process_Declarations --
9680 --------------------------
9681
9682 procedure Process_Declarations (Variant : Node_Id) is
9683 CL : constant Node_Id := Component_List (Variant);
9684 VP : Node_Id;
9685
9686 begin
9687 -- Check for static predicate present in this variant
9688
9689 if Has_SP_Choice (Variant) then
9690
9691 -- Here we expand. You might expect to find this call in
9692 -- Expand_N_Variant_Part, but that is called when we first
9693 -- see the variant part, and we cannot do this expansion
9694 -- earlier than the freeze point, since for statically
9695 -- predicated subtypes, the predicate is not known till
9696 -- the freeze point.
9697
9698 -- Furthermore, we do this expansion even if the expander
9699 -- is not active, because other semantic processing, e.g.
9700 -- for aggregates, requires the expanded list of choices.
9701
9702 -- If the expander is not active, then we can't just clobber
9703 -- the list since it would invalidate the ASIS -gnatct tree.
9704 -- So we have to rewrite the variant part with a Rewrite
9705 -- call that replaces it with a copy and clobber the copy.
9706
9707 if not Expander_Active then
9708 declare
9709 NewV : constant Node_Id := New_Copy (Variant);
9710 begin
9711 Set_Discrete_Choices
9712 (NewV, New_Copy_List (Discrete_Choices (Variant)));
9713 Rewrite (Variant, NewV);
9714 end;
9715 end if;
9716
9717 Expand_Static_Predicates_In_Choices (Variant);
9718 end if;
9719
9720 -- We don't need to worry about the declarations in the variant
9721 -- (since they were analyzed by Analyze_Choices when we first
9722 -- encountered the variant), but we do need to take care of
9723 -- expansion of any nested variants.
9724
9725 if not Null_Present (CL) then
9726 VP := Variant_Part (CL);
9727
9728 if Present (VP) then
9729 Check_Choices
9730 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9731 end if;
9732 end if;
9733 end Process_Declarations;
9734
9735 -- Start of processing for Check_Variant_Part
9736
9737 begin
9738 -- Find component list
9739
9740 C := Empty;
9741
9742 if Nkind (D) = N_Full_Type_Declaration then
9743 T := Type_Definition (D);
9744
9745 if Nkind (T) = N_Record_Definition then
9746 C := Component_List (T);
9747
9748 elsif Nkind (T) = N_Derived_Type_Definition
9749 and then Present (Record_Extension_Part (T))
9750 then
9751 C := Component_List (Record_Extension_Part (T));
9752 end if;
9753 end if;
9754
9755 -- Case of variant part present
9756
9757 if Present (C) and then Present (Variant_Part (C)) then
9758 VP := Variant_Part (C);
9759
9760 -- Check choices
9761
9762 Check_Choices
9763 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9764
9765 -- If the last variant does not contain the Others choice,
9766 -- replace it with an N_Others_Choice node since Gigi always
9767 -- wants an Others. Note that we do not bother to call Analyze
9768 -- on the modified variant part, since its only effect would be
9769 -- to compute the Others_Discrete_Choices node laboriously, and
9770 -- of course we already know the list of choices corresponding
9771 -- to the others choice (it's the list we're replacing).
9772
9773 -- We only want to do this if the expander is active, since
9774 -- we do not want to clobber the ASIS tree.
9775
9776 if Expander_Active then
9777 declare
9778 Last_Var : constant Node_Id :=
9779 Last_Non_Pragma (Variants (VP));
9780
9781 Others_Node : Node_Id;
9782
9783 begin
9784 if Nkind (First (Discrete_Choices (Last_Var))) /=
9785 N_Others_Choice
9786 then
9787 Others_Node := Make_Others_Choice (Sloc (Last_Var));
9788 Set_Others_Discrete_Choices
9789 (Others_Node, Discrete_Choices (Last_Var));
9790 Set_Discrete_Choices
9791 (Last_Var, New_List (Others_Node));
9792 end if;
9793 end;
9794 end if;
9795 end if;
9796 end Check_Variant_Part;
9797 end if;
9798 end Freeze_Entity_Checks;
9799
9800 -------------------------
9801 -- Get_Alignment_Value --
9802 -------------------------
9803
9804 function Get_Alignment_Value (Expr : Node_Id) return Uint is
9805 Align : constant Uint := Static_Integer (Expr);
9806
9807 begin
9808 if Align = No_Uint then
9809 return No_Uint;
9810
9811 elsif Align <= 0 then
9812 Error_Msg_N ("alignment value must be positive", Expr);
9813 return No_Uint;
9814
9815 else
9816 for J in Int range 0 .. 64 loop
9817 declare
9818 M : constant Uint := Uint_2 ** J;
9819
9820 begin
9821 exit when M = Align;
9822
9823 if M > Align then
9824 Error_Msg_N
9825 ("alignment value must be power of 2", Expr);
9826 return No_Uint;
9827 end if;
9828 end;
9829 end loop;
9830
9831 return Align;
9832 end if;
9833 end Get_Alignment_Value;
9834
9835 -------------------------------------
9836 -- Inherit_Aspects_At_Freeze_Point --
9837 -------------------------------------
9838
9839 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
9840 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9841 (Rep_Item : Node_Id) return Boolean;
9842 -- This routine checks if Rep_Item is either a pragma or an aspect
9843 -- specification node whose correponding pragma (if any) is present in
9844 -- the Rep Item chain of the entity it has been specified to.
9845
9846 --------------------------------------------------
9847 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
9848 --------------------------------------------------
9849
9850 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9851 (Rep_Item : Node_Id) return Boolean
9852 is
9853 begin
9854 return Nkind (Rep_Item) = N_Pragma
9855 or else Present_In_Rep_Item
9856 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
9857 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
9858
9859 -- Start of processing for Inherit_Aspects_At_Freeze_Point
9860
9861 begin
9862 -- A representation item is either subtype-specific (Size and Alignment
9863 -- clauses) or type-related (all others). Subtype-specific aspects may
9864 -- differ for different subtypes of the same type (RM 13.1.8).
9865
9866 -- A derived type inherits each type-related representation aspect of
9867 -- its parent type that was directly specified before the declaration of
9868 -- the derived type (RM 13.1.15).
9869
9870 -- A derived subtype inherits each subtype-specific representation
9871 -- aspect of its parent subtype that was directly specified before the
9872 -- declaration of the derived type (RM 13.1.15).
9873
9874 -- The general processing involves inheriting a representation aspect
9875 -- from a parent type whenever the first rep item (aspect specification,
9876 -- attribute definition clause, pragma) corresponding to the given
9877 -- representation aspect in the rep item chain of Typ, if any, isn't
9878 -- directly specified to Typ but to one of its parents.
9879
9880 -- ??? Note that, for now, just a limited number of representation
9881 -- aspects have been inherited here so far. Many of them are
9882 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
9883 -- a non- exhaustive list of aspects that likely also need to
9884 -- be moved to this routine: Alignment, Component_Alignment,
9885 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
9886 -- Preelaborable_Initialization, RM_Size and Small.
9887
9888 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
9889 return;
9890 end if;
9891
9892 -- Ada_05/Ada_2005
9893
9894 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
9895 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
9896 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9897 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
9898 then
9899 Set_Is_Ada_2005_Only (Typ);
9900 end if;
9901
9902 -- Ada_12/Ada_2012
9903
9904 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
9905 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
9906 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9907 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
9908 then
9909 Set_Is_Ada_2012_Only (Typ);
9910 end if;
9911
9912 -- Atomic/Shared
9913
9914 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
9915 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
9916 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9917 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
9918 then
9919 Set_Is_Atomic (Typ);
9920 Set_Treat_As_Volatile (Typ);
9921 Set_Is_Volatile (Typ);
9922 end if;
9923
9924 -- Default_Component_Value
9925
9926 if Is_Array_Type (Typ)
9927 and then Is_Base_Type (Typ)
9928 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
9929 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
9930 then
9931 Set_Default_Aspect_Component_Value (Typ,
9932 Default_Aspect_Component_Value
9933 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
9934 end if;
9935
9936 -- Default_Value
9937
9938 if Is_Scalar_Type (Typ)
9939 and then Is_Base_Type (Typ)
9940 and then Has_Rep_Item (Typ, Name_Default_Value, False)
9941 and then Has_Rep_Item (Typ, Name_Default_Value)
9942 then
9943 Set_Default_Aspect_Value (Typ,
9944 Default_Aspect_Value
9945 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
9946 end if;
9947
9948 -- Discard_Names
9949
9950 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
9951 and then Has_Rep_Item (Typ, Name_Discard_Names)
9952 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9953 (Get_Rep_Item (Typ, Name_Discard_Names))
9954 then
9955 Set_Discard_Names (Typ);
9956 end if;
9957
9958 -- Invariants
9959
9960 if not Has_Rep_Item (Typ, Name_Invariant, False)
9961 and then Has_Rep_Item (Typ, Name_Invariant)
9962 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9963 (Get_Rep_Item (Typ, Name_Invariant))
9964 then
9965 Set_Has_Invariants (Typ);
9966
9967 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
9968 Set_Has_Inheritable_Invariants (Typ);
9969 end if;
9970 end if;
9971
9972 -- Volatile
9973
9974 if not Has_Rep_Item (Typ, Name_Volatile, False)
9975 and then Has_Rep_Item (Typ, Name_Volatile)
9976 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9977 (Get_Rep_Item (Typ, Name_Volatile))
9978 then
9979 Set_Treat_As_Volatile (Typ);
9980 Set_Is_Volatile (Typ);
9981 end if;
9982
9983 -- Inheritance for derived types only
9984
9985 if Is_Derived_Type (Typ) then
9986 declare
9987 Bas_Typ : constant Entity_Id := Base_Type (Typ);
9988 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
9989
9990 begin
9991 -- Atomic_Components
9992
9993 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
9994 and then Has_Rep_Item (Typ, Name_Atomic_Components)
9995 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9996 (Get_Rep_Item (Typ, Name_Atomic_Components))
9997 then
9998 Set_Has_Atomic_Components (Imp_Bas_Typ);
9999 end if;
10000
10001 -- Volatile_Components
10002
10003 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10004 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10005 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10006 (Get_Rep_Item (Typ, Name_Volatile_Components))
10007 then
10008 Set_Has_Volatile_Components (Imp_Bas_Typ);
10009 end if;
10010
10011 -- Finalize_Storage_Only.
10012
10013 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10014 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10015 then
10016 Set_Finalize_Storage_Only (Bas_Typ);
10017 end if;
10018
10019 -- Universal_Aliasing
10020
10021 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10022 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10023 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10024 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10025 then
10026 Set_Universal_Aliasing (Imp_Bas_Typ);
10027 end if;
10028
10029 -- Record type specific aspects
10030
10031 if Is_Record_Type (Typ) then
10032
10033 -- Bit_Order
10034
10035 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10036 and then Has_Rep_Item (Typ, Name_Bit_Order)
10037 then
10038 Set_Reverse_Bit_Order (Bas_Typ,
10039 Reverse_Bit_Order (Entity (Name
10040 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10041 end if;
10042
10043 -- Scalar_Storage_Order
10044
10045 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
10046 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
10047 then
10048 Set_Reverse_Storage_Order (Bas_Typ,
10049 Reverse_Storage_Order (Entity (Name
10050 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
10051 end if;
10052 end if;
10053 end;
10054 end if;
10055 end Inherit_Aspects_At_Freeze_Point;
10056
10057 ----------------
10058 -- Initialize --
10059 ----------------
10060
10061 procedure Initialize is
10062 begin
10063 Address_Clause_Checks.Init;
10064 Independence_Checks.Init;
10065 Unchecked_Conversions.Init;
10066 end Initialize;
10067
10068 ---------------------------
10069 -- Install_Discriminants --
10070 ---------------------------
10071
10072 procedure Install_Discriminants (E : Entity_Id) is
10073 Disc : Entity_Id;
10074 Prev : Entity_Id;
10075 begin
10076 Disc := First_Discriminant (E);
10077 while Present (Disc) loop
10078 Prev := Current_Entity (Disc);
10079 Set_Current_Entity (Disc);
10080 Set_Is_Immediately_Visible (Disc);
10081 Set_Homonym (Disc, Prev);
10082 Next_Discriminant (Disc);
10083 end loop;
10084 end Install_Discriminants;
10085
10086 -------------------------
10087 -- Is_Operational_Item --
10088 -------------------------
10089
10090 function Is_Operational_Item (N : Node_Id) return Boolean is
10091 begin
10092 if Nkind (N) /= N_Attribute_Definition_Clause then
10093 return False;
10094
10095 else
10096 declare
10097 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
10098 begin
10099 return Id = Attribute_Input
10100 or else Id = Attribute_Output
10101 or else Id = Attribute_Read
10102 or else Id = Attribute_Write
10103 or else Id = Attribute_External_Tag;
10104 end;
10105 end if;
10106 end Is_Operational_Item;
10107
10108 ------------------
10109 -- Minimum_Size --
10110 ------------------
10111
10112 function Minimum_Size
10113 (T : Entity_Id;
10114 Biased : Boolean := False) return Nat
10115 is
10116 Lo : Uint := No_Uint;
10117 Hi : Uint := No_Uint;
10118 LoR : Ureal := No_Ureal;
10119 HiR : Ureal := No_Ureal;
10120 LoSet : Boolean := False;
10121 HiSet : Boolean := False;
10122 B : Uint;
10123 S : Nat;
10124 Ancest : Entity_Id;
10125 R_Typ : constant Entity_Id := Root_Type (T);
10126
10127 begin
10128 -- If bad type, return 0
10129
10130 if T = Any_Type then
10131 return 0;
10132
10133 -- For generic types, just return zero. There cannot be any legitimate
10134 -- need to know such a size, but this routine may be called with a
10135 -- generic type as part of normal processing.
10136
10137 elsif Is_Generic_Type (R_Typ)
10138 or else R_Typ = Any_Type
10139 then
10140 return 0;
10141
10142 -- Access types. Normally an access type cannot have a size smaller
10143 -- than the size of System.Address. The exception is on VMS, where
10144 -- we have short and long addresses, and it is possible for an access
10145 -- type to have a short address size (and thus be less than the size
10146 -- of System.Address itself). We simply skip the check for VMS, and
10147 -- leave it to the back end to do the check.
10148
10149 elsif Is_Access_Type (T) then
10150 if OpenVMS_On_Target then
10151 return 0;
10152 else
10153 return System_Address_Size;
10154 end if;
10155
10156 -- Floating-point types
10157
10158 elsif Is_Floating_Point_Type (T) then
10159 return UI_To_Int (Esize (R_Typ));
10160
10161 -- Discrete types
10162
10163 elsif Is_Discrete_Type (T) then
10164
10165 -- The following loop is looking for the nearest compile time known
10166 -- bounds following the ancestor subtype chain. The idea is to find
10167 -- the most restrictive known bounds information.
10168
10169 Ancest := T;
10170 loop
10171 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
10172 return 0;
10173 end if;
10174
10175 if not LoSet then
10176 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
10177 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
10178 LoSet := True;
10179 exit when HiSet;
10180 end if;
10181 end if;
10182
10183 if not HiSet then
10184 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
10185 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
10186 HiSet := True;
10187 exit when LoSet;
10188 end if;
10189 end if;
10190
10191 Ancest := Ancestor_Subtype (Ancest);
10192
10193 if No (Ancest) then
10194 Ancest := Base_Type (T);
10195
10196 if Is_Generic_Type (Ancest) then
10197 return 0;
10198 end if;
10199 end if;
10200 end loop;
10201
10202 -- Fixed-point types. We can't simply use Expr_Value to get the
10203 -- Corresponding_Integer_Value values of the bounds, since these do not
10204 -- get set till the type is frozen, and this routine can be called
10205 -- before the type is frozen. Similarly the test for bounds being static
10206 -- needs to include the case where we have unanalyzed real literals for
10207 -- the same reason.
10208
10209 elsif Is_Fixed_Point_Type (T) then
10210
10211 -- The following loop is looking for the nearest compile time known
10212 -- bounds following the ancestor subtype chain. The idea is to find
10213 -- the most restrictive known bounds information.
10214
10215 Ancest := T;
10216 loop
10217 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
10218 return 0;
10219 end if;
10220
10221 -- Note: In the following two tests for LoSet and HiSet, it may
10222 -- seem redundant to test for N_Real_Literal here since normally
10223 -- one would assume that the test for the value being known at
10224 -- compile time includes this case. However, there is a glitch.
10225 -- If the real literal comes from folding a non-static expression,
10226 -- then we don't consider any non- static expression to be known
10227 -- at compile time if we are in configurable run time mode (needed
10228 -- in some cases to give a clearer definition of what is and what
10229 -- is not accepted). So the test is indeed needed. Without it, we
10230 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
10231
10232 if not LoSet then
10233 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
10234 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
10235 then
10236 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
10237 LoSet := True;
10238 exit when HiSet;
10239 end if;
10240 end if;
10241
10242 if not HiSet then
10243 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
10244 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
10245 then
10246 HiR := Expr_Value_R (Type_High_Bound (Ancest));
10247 HiSet := True;
10248 exit when LoSet;
10249 end if;
10250 end if;
10251
10252 Ancest := Ancestor_Subtype (Ancest);
10253
10254 if No (Ancest) then
10255 Ancest := Base_Type (T);
10256
10257 if Is_Generic_Type (Ancest) then
10258 return 0;
10259 end if;
10260 end if;
10261 end loop;
10262
10263 Lo := UR_To_Uint (LoR / Small_Value (T));
10264 Hi := UR_To_Uint (HiR / Small_Value (T));
10265
10266 -- No other types allowed
10267
10268 else
10269 raise Program_Error;
10270 end if;
10271
10272 -- Fall through with Hi and Lo set. Deal with biased case
10273
10274 if (Biased
10275 and then not Is_Fixed_Point_Type (T)
10276 and then not (Is_Enumeration_Type (T)
10277 and then Has_Non_Standard_Rep (T)))
10278 or else Has_Biased_Representation (T)
10279 then
10280 Hi := Hi - Lo;
10281 Lo := Uint_0;
10282 end if;
10283
10284 -- Signed case. Note that we consider types like range 1 .. -1 to be
10285 -- signed for the purpose of computing the size, since the bounds have
10286 -- to be accommodated in the base type.
10287
10288 if Lo < 0 or else Hi < 0 then
10289 S := 1;
10290 B := Uint_1;
10291
10292 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
10293 -- Note that we accommodate the case where the bounds cross. This
10294 -- can happen either because of the way the bounds are declared
10295 -- or because of the algorithm in Freeze_Fixed_Point_Type.
10296
10297 while Lo < -B
10298 or else Hi < -B
10299 or else Lo >= B
10300 or else Hi >= B
10301 loop
10302 B := Uint_2 ** S;
10303 S := S + 1;
10304 end loop;
10305
10306 -- Unsigned case
10307
10308 else
10309 -- If both bounds are positive, make sure that both are represen-
10310 -- table in the case where the bounds are crossed. This can happen
10311 -- either because of the way the bounds are declared, or because of
10312 -- the algorithm in Freeze_Fixed_Point_Type.
10313
10314 if Lo > Hi then
10315 Hi := Lo;
10316 end if;
10317
10318 -- S = size, (can accommodate 0 .. (2**size - 1))
10319
10320 S := 0;
10321 while Hi >= Uint_2 ** S loop
10322 S := S + 1;
10323 end loop;
10324 end if;
10325
10326 return S;
10327 end Minimum_Size;
10328
10329 ---------------------------
10330 -- New_Stream_Subprogram --
10331 ---------------------------
10332
10333 procedure New_Stream_Subprogram
10334 (N : Node_Id;
10335 Ent : Entity_Id;
10336 Subp : Entity_Id;
10337 Nam : TSS_Name_Type)
10338 is
10339 Loc : constant Source_Ptr := Sloc (N);
10340 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
10341 Subp_Id : Entity_Id;
10342 Subp_Decl : Node_Id;
10343 F : Entity_Id;
10344 Etyp : Entity_Id;
10345
10346 Defer_Declaration : constant Boolean :=
10347 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
10348 -- For a tagged type, there is a declaration for each stream attribute
10349 -- at the freeze point, and we must generate only a completion of this
10350 -- declaration. We do the same for private types, because the full view
10351 -- might be tagged. Otherwise we generate a declaration at the point of
10352 -- the attribute definition clause.
10353
10354 function Build_Spec return Node_Id;
10355 -- Used for declaration and renaming declaration, so that this is
10356 -- treated as a renaming_as_body.
10357
10358 ----------------
10359 -- Build_Spec --
10360 ----------------
10361
10362 function Build_Spec return Node_Id is
10363 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
10364 Formals : List_Id;
10365 Spec : Node_Id;
10366 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
10367
10368 begin
10369 Subp_Id := Make_Defining_Identifier (Loc, Sname);
10370
10371 -- S : access Root_Stream_Type'Class
10372
10373 Formals := New_List (
10374 Make_Parameter_Specification (Loc,
10375 Defining_Identifier =>
10376 Make_Defining_Identifier (Loc, Name_S),
10377 Parameter_Type =>
10378 Make_Access_Definition (Loc,
10379 Subtype_Mark =>
10380 New_Occurrence_Of (
10381 Designated_Type (Etype (F)), Loc))));
10382
10383 if Nam = TSS_Stream_Input then
10384 Spec :=
10385 Make_Function_Specification (Loc,
10386 Defining_Unit_Name => Subp_Id,
10387 Parameter_Specifications => Formals,
10388 Result_Definition => T_Ref);
10389 else
10390 -- V : [out] T
10391
10392 Append_To (Formals,
10393 Make_Parameter_Specification (Loc,
10394 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
10395 Out_Present => Out_P,
10396 Parameter_Type => T_Ref));
10397
10398 Spec :=
10399 Make_Procedure_Specification (Loc,
10400 Defining_Unit_Name => Subp_Id,
10401 Parameter_Specifications => Formals);
10402 end if;
10403
10404 return Spec;
10405 end Build_Spec;
10406
10407 -- Start of processing for New_Stream_Subprogram
10408
10409 begin
10410 F := First_Formal (Subp);
10411
10412 if Ekind (Subp) = E_Procedure then
10413 Etyp := Etype (Next_Formal (F));
10414 else
10415 Etyp := Etype (Subp);
10416 end if;
10417
10418 -- Prepare subprogram declaration and insert it as an action on the
10419 -- clause node. The visibility for this entity is used to test for
10420 -- visibility of the attribute definition clause (in the sense of
10421 -- 8.3(23) as amended by AI-195).
10422
10423 if not Defer_Declaration then
10424 Subp_Decl :=
10425 Make_Subprogram_Declaration (Loc,
10426 Specification => Build_Spec);
10427
10428 -- For a tagged type, there is always a visible declaration for each
10429 -- stream TSS (it is a predefined primitive operation), and the
10430 -- completion of this declaration occurs at the freeze point, which is
10431 -- not always visible at places where the attribute definition clause is
10432 -- visible. So, we create a dummy entity here for the purpose of
10433 -- tracking the visibility of the attribute definition clause itself.
10434
10435 else
10436 Subp_Id :=
10437 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
10438 Subp_Decl :=
10439 Make_Object_Declaration (Loc,
10440 Defining_Identifier => Subp_Id,
10441 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
10442 end if;
10443
10444 Insert_Action (N, Subp_Decl);
10445 Set_Entity (N, Subp_Id);
10446
10447 Subp_Decl :=
10448 Make_Subprogram_Renaming_Declaration (Loc,
10449 Specification => Build_Spec,
10450 Name => New_Occurrence_Of (Subp, Loc));
10451
10452 if Defer_Declaration then
10453 Set_TSS (Base_Type (Ent), Subp_Id);
10454 else
10455 Insert_Action (N, Subp_Decl);
10456 Copy_TSS (Subp_Id, Base_Type (Ent));
10457 end if;
10458 end New_Stream_Subprogram;
10459
10460 ------------------------------------------
10461 -- Push_Scope_And_Install_Discriminants --
10462 ------------------------------------------
10463
10464 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
10465 begin
10466 if Has_Discriminants (E) then
10467 Push_Scope (E);
10468
10469 -- Make discriminants visible for type declarations and protected
10470 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
10471
10472 if Nkind (Parent (E)) /= N_Subtype_Declaration then
10473 Install_Discriminants (E);
10474 end if;
10475 end if;
10476 end Push_Scope_And_Install_Discriminants;
10477
10478 ------------------------
10479 -- Rep_Item_Too_Early --
10480 ------------------------
10481
10482 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
10483 begin
10484 -- Cannot apply non-operational rep items to generic types
10485
10486 if Is_Operational_Item (N) then
10487 return False;
10488
10489 elsif Is_Type (T)
10490 and then Is_Generic_Type (Root_Type (T))
10491 then
10492 Error_Msg_N ("representation item not allowed for generic type", N);
10493 return True;
10494 end if;
10495
10496 -- Otherwise check for incomplete type
10497
10498 if Is_Incomplete_Or_Private_Type (T)
10499 and then No (Underlying_Type (T))
10500 and then
10501 (Nkind (N) /= N_Pragma
10502 or else Get_Pragma_Id (N) /= Pragma_Import)
10503 then
10504 Error_Msg_N
10505 ("representation item must be after full type declaration", N);
10506 return True;
10507
10508 -- If the type has incomplete components, a representation clause is
10509 -- illegal but stream attributes and Convention pragmas are correct.
10510
10511 elsif Has_Private_Component (T) then
10512 if Nkind (N) = N_Pragma then
10513 return False;
10514
10515 else
10516 Error_Msg_N
10517 ("representation item must appear after type is fully defined",
10518 N);
10519 return True;
10520 end if;
10521 else
10522 return False;
10523 end if;
10524 end Rep_Item_Too_Early;
10525
10526 -----------------------
10527 -- Rep_Item_Too_Late --
10528 -----------------------
10529
10530 function Rep_Item_Too_Late
10531 (T : Entity_Id;
10532 N : Node_Id;
10533 FOnly : Boolean := False) return Boolean
10534 is
10535 S : Entity_Id;
10536 Parent_Type : Entity_Id;
10537
10538 procedure Too_Late;
10539 -- Output the too late message. Note that this is not considered a
10540 -- serious error, since the effect is simply that we ignore the
10541 -- representation clause in this case.
10542
10543 --------------
10544 -- Too_Late --
10545 --------------
10546
10547 procedure Too_Late is
10548 begin
10549 -- Other compilers seem more relaxed about rep items appearing too
10550 -- late. Since analysis tools typically don't care about rep items
10551 -- anyway, no reason to be too strict about this.
10552
10553 if not Relaxed_RM_Semantics then
10554 Error_Msg_N ("|representation item appears too late!", N);
10555 end if;
10556 end Too_Late;
10557
10558 -- Start of processing for Rep_Item_Too_Late
10559
10560 begin
10561 -- First make sure entity is not frozen (RM 13.1(9))
10562
10563 if Is_Frozen (T)
10564
10565 -- Exclude imported types, which may be frozen if they appear in a
10566 -- representation clause for a local type.
10567
10568 and then not From_Limited_With (T)
10569
10570 -- Exclude generated entities (not coming from source). The common
10571 -- case is when we generate a renaming which prematurely freezes the
10572 -- renamed internal entity, but we still want to be able to set copies
10573 -- of attribute values such as Size/Alignment.
10574
10575 and then Comes_From_Source (T)
10576 then
10577 Too_Late;
10578 S := First_Subtype (T);
10579
10580 if Present (Freeze_Node (S)) then
10581 Error_Msg_NE
10582 ("??no more representation items for }", Freeze_Node (S), S);
10583 end if;
10584
10585 return True;
10586
10587 -- Check for case of non-tagged derived type whose parent either has
10588 -- primitive operations, or is a by reference type (RM 13.1(10)).
10589
10590 elsif Is_Type (T)
10591 and then not FOnly
10592 and then Is_Derived_Type (T)
10593 and then not Is_Tagged_Type (T)
10594 then
10595 Parent_Type := Etype (Base_Type (T));
10596
10597 if Has_Primitive_Operations (Parent_Type) then
10598 Too_Late;
10599 Error_Msg_NE
10600 ("primitive operations already defined for&!", N, Parent_Type);
10601 return True;
10602
10603 elsif Is_By_Reference_Type (Parent_Type) then
10604 Too_Late;
10605 Error_Msg_NE
10606 ("parent type & is a by reference type!", N, Parent_Type);
10607 return True;
10608 end if;
10609 end if;
10610
10611 -- No error, link item into head of chain of rep items for the entity,
10612 -- but avoid chaining if we have an overloadable entity, and the pragma
10613 -- is one that can apply to multiple overloaded entities.
10614
10615 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
10616 declare
10617 Pname : constant Name_Id := Pragma_Name (N);
10618 begin
10619 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
10620 Name_External, Name_Interface)
10621 then
10622 return False;
10623 end if;
10624 end;
10625 end if;
10626
10627 Record_Rep_Item (T, N);
10628 return False;
10629 end Rep_Item_Too_Late;
10630
10631 -------------------------------------
10632 -- Replace_Type_References_Generic --
10633 -------------------------------------
10634
10635 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
10636
10637 function Replace_Node (N : Node_Id) return Traverse_Result;
10638 -- Processes a single node in the traversal procedure below, checking
10639 -- if node N should be replaced, and if so, doing the replacement.
10640
10641 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
10642 -- This instantiation provides the body of Replace_Type_References
10643
10644 ------------------
10645 -- Replace_Node --
10646 ------------------
10647
10648 function Replace_Node (N : Node_Id) return Traverse_Result is
10649 S : Entity_Id;
10650 P : Node_Id;
10651
10652 begin
10653 -- Case of identifier
10654
10655 if Nkind (N) = N_Identifier then
10656
10657 -- If not the type name, all done with this node
10658
10659 if Chars (N) /= TName then
10660 return Skip;
10661
10662 -- Otherwise do the replacement and we are done with this node
10663
10664 else
10665 Replace_Type_Reference (N);
10666 return Skip;
10667 end if;
10668
10669 -- Case of selected component (which is what a qualification
10670 -- looks like in the unanalyzed tree, which is what we have.
10671
10672 elsif Nkind (N) = N_Selected_Component then
10673
10674 -- If selector name is not our type, keeping going (we might
10675 -- still have an occurrence of the type in the prefix).
10676
10677 if Nkind (Selector_Name (N)) /= N_Identifier
10678 or else Chars (Selector_Name (N)) /= TName
10679 then
10680 return OK;
10681
10682 -- Selector name is our type, check qualification
10683
10684 else
10685 -- Loop through scopes and prefixes, doing comparison
10686
10687 S := Current_Scope;
10688 P := Prefix (N);
10689 loop
10690 -- Continue if no more scopes or scope with no name
10691
10692 if No (S) or else Nkind (S) not in N_Has_Chars then
10693 return OK;
10694 end if;
10695
10696 -- Do replace if prefix is an identifier matching the
10697 -- scope that we are currently looking at.
10698
10699 if Nkind (P) = N_Identifier
10700 and then Chars (P) = Chars (S)
10701 then
10702 Replace_Type_Reference (N);
10703 return Skip;
10704 end if;
10705
10706 -- Go check scope above us if prefix is itself of the
10707 -- form of a selected component, whose selector matches
10708 -- the scope we are currently looking at.
10709
10710 if Nkind (P) = N_Selected_Component
10711 and then Nkind (Selector_Name (P)) = N_Identifier
10712 and then Chars (Selector_Name (P)) = Chars (S)
10713 then
10714 S := Scope (S);
10715 P := Prefix (P);
10716
10717 -- For anything else, we don't have a match, so keep on
10718 -- going, there are still some weird cases where we may
10719 -- still have a replacement within the prefix.
10720
10721 else
10722 return OK;
10723 end if;
10724 end loop;
10725 end if;
10726
10727 -- Continue for any other node kind
10728
10729 else
10730 return OK;
10731 end if;
10732 end Replace_Node;
10733
10734 begin
10735 Replace_Type_Refs (N);
10736 end Replace_Type_References_Generic;
10737
10738 -------------------------
10739 -- Same_Representation --
10740 -------------------------
10741
10742 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
10743 T1 : constant Entity_Id := Underlying_Type (Typ1);
10744 T2 : constant Entity_Id := Underlying_Type (Typ2);
10745
10746 begin
10747 -- A quick check, if base types are the same, then we definitely have
10748 -- the same representation, because the subtype specific representation
10749 -- attributes (Size and Alignment) do not affect representation from
10750 -- the point of view of this test.
10751
10752 if Base_Type (T1) = Base_Type (T2) then
10753 return True;
10754
10755 elsif Is_Private_Type (Base_Type (T2))
10756 and then Base_Type (T1) = Full_View (Base_Type (T2))
10757 then
10758 return True;
10759 end if;
10760
10761 -- Tagged types never have differing representations
10762
10763 if Is_Tagged_Type (T1) then
10764 return True;
10765 end if;
10766
10767 -- Representations are definitely different if conventions differ
10768
10769 if Convention (T1) /= Convention (T2) then
10770 return False;
10771 end if;
10772
10773 -- Representations are different if component alignments or scalar
10774 -- storage orders differ.
10775
10776 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
10777 and then
10778 (Is_Record_Type (T2) or else Is_Array_Type (T2))
10779 and then
10780 (Component_Alignment (T1) /= Component_Alignment (T2)
10781 or else
10782 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
10783 then
10784 return False;
10785 end if;
10786
10787 -- For arrays, the only real issue is component size. If we know the
10788 -- component size for both arrays, and it is the same, then that's
10789 -- good enough to know we don't have a change of representation.
10790
10791 if Is_Array_Type (T1) then
10792 if Known_Component_Size (T1)
10793 and then Known_Component_Size (T2)
10794 and then Component_Size (T1) = Component_Size (T2)
10795 then
10796 if VM_Target = No_VM then
10797 return True;
10798
10799 -- In VM targets the representation of arrays with aliased
10800 -- components differs from arrays with non-aliased components
10801
10802 else
10803 return Has_Aliased_Components (Base_Type (T1))
10804 =
10805 Has_Aliased_Components (Base_Type (T2));
10806 end if;
10807 end if;
10808 end if;
10809
10810 -- Types definitely have same representation if neither has non-standard
10811 -- representation since default representations are always consistent.
10812 -- If only one has non-standard representation, and the other does not,
10813 -- then we consider that they do not have the same representation. They
10814 -- might, but there is no way of telling early enough.
10815
10816 if Has_Non_Standard_Rep (T1) then
10817 if not Has_Non_Standard_Rep (T2) then
10818 return False;
10819 end if;
10820 else
10821 return not Has_Non_Standard_Rep (T2);
10822 end if;
10823
10824 -- Here the two types both have non-standard representation, and we need
10825 -- to determine if they have the same non-standard representation.
10826
10827 -- For arrays, we simply need to test if the component sizes are the
10828 -- same. Pragma Pack is reflected in modified component sizes, so this
10829 -- check also deals with pragma Pack.
10830
10831 if Is_Array_Type (T1) then
10832 return Component_Size (T1) = Component_Size (T2);
10833
10834 -- Tagged types always have the same representation, because it is not
10835 -- possible to specify different representations for common fields.
10836
10837 elsif Is_Tagged_Type (T1) then
10838 return True;
10839
10840 -- Case of record types
10841
10842 elsif Is_Record_Type (T1) then
10843
10844 -- Packed status must conform
10845
10846 if Is_Packed (T1) /= Is_Packed (T2) then
10847 return False;
10848
10849 -- Otherwise we must check components. Typ2 maybe a constrained
10850 -- subtype with fewer components, so we compare the components
10851 -- of the base types.
10852
10853 else
10854 Record_Case : declare
10855 CD1, CD2 : Entity_Id;
10856
10857 function Same_Rep return Boolean;
10858 -- CD1 and CD2 are either components or discriminants. This
10859 -- function tests whether they have the same representation.
10860
10861 --------------
10862 -- Same_Rep --
10863 --------------
10864
10865 function Same_Rep return Boolean is
10866 begin
10867 if No (Component_Clause (CD1)) then
10868 return No (Component_Clause (CD2));
10869 else
10870 -- Note: at this point, component clauses have been
10871 -- normalized to the default bit order, so that the
10872 -- comparison of Component_Bit_Offsets is meaningful.
10873
10874 return
10875 Present (Component_Clause (CD2))
10876 and then
10877 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
10878 and then
10879 Esize (CD1) = Esize (CD2);
10880 end if;
10881 end Same_Rep;
10882
10883 -- Start of processing for Record_Case
10884
10885 begin
10886 if Has_Discriminants (T1) then
10887
10888 -- The number of discriminants may be different if the
10889 -- derived type has fewer (constrained by values). The
10890 -- invisible discriminants retain the representation of
10891 -- the original, so the discrepancy does not per se
10892 -- indicate a different representation.
10893
10894 CD1 := First_Discriminant (T1);
10895 CD2 := First_Discriminant (T2);
10896 while Present (CD1) and then Present (CD2) loop
10897 if not Same_Rep then
10898 return False;
10899 else
10900 Next_Discriminant (CD1);
10901 Next_Discriminant (CD2);
10902 end if;
10903 end loop;
10904 end if;
10905
10906 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
10907 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
10908 while Present (CD1) loop
10909 if not Same_Rep then
10910 return False;
10911 else
10912 Next_Component (CD1);
10913 Next_Component (CD2);
10914 end if;
10915 end loop;
10916
10917 return True;
10918 end Record_Case;
10919 end if;
10920
10921 -- For enumeration types, we must check each literal to see if the
10922 -- representation is the same. Note that we do not permit enumeration
10923 -- representation clauses for Character and Wide_Character, so these
10924 -- cases were already dealt with.
10925
10926 elsif Is_Enumeration_Type (T1) then
10927 Enumeration_Case : declare
10928 L1, L2 : Entity_Id;
10929
10930 begin
10931 L1 := First_Literal (T1);
10932 L2 := First_Literal (T2);
10933 while Present (L1) loop
10934 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
10935 return False;
10936 else
10937 Next_Literal (L1);
10938 Next_Literal (L2);
10939 end if;
10940 end loop;
10941
10942 return True;
10943 end Enumeration_Case;
10944
10945 -- Any other types have the same representation for these purposes
10946
10947 else
10948 return True;
10949 end if;
10950 end Same_Representation;
10951
10952 --------------------------------
10953 -- Resolve_Iterable_Operation --
10954 --------------------------------
10955
10956 procedure Resolve_Iterable_Operation
10957 (N : Node_Id;
10958 Cursor : Entity_Id;
10959 Typ : Entity_Id;
10960 Nam : Name_Id)
10961 is
10962 Ent : Entity_Id;
10963 F1 : Entity_Id;
10964 F2 : Entity_Id;
10965
10966 begin
10967 if not Is_Overloaded (N) then
10968 if not Is_Entity_Name (N)
10969 or else Ekind (Entity (N)) /= E_Function
10970 or else Scope (Entity (N)) /= Scope (Typ)
10971 or else No (First_Formal (Entity (N)))
10972 or else Etype (First_Formal (Entity (N))) /= Typ
10973 then
10974 Error_Msg_N ("iterable primitive must be local function name "
10975 & "whose first formal is an iterable type", N);
10976 return;
10977 end if;
10978
10979 Ent := Entity (N);
10980 F1 := First_Formal (Ent);
10981 if Nam = Name_First then
10982
10983 -- First (Container) => Cursor
10984
10985 if Etype (Ent) /= Cursor then
10986 Error_Msg_N ("primitive for First must yield a curosr", N);
10987 end if;
10988
10989 elsif Nam = Name_Next then
10990
10991 -- Next (Container, Cursor) => Cursor
10992
10993 F2 := Next_Formal (F1);
10994
10995 if Etype (F2) /= Cursor
10996 or else Etype (Ent) /= Cursor
10997 or else Present (Next_Formal (F2))
10998 then
10999 Error_Msg_N ("no match for Next iterable primitive", N);
11000 end if;
11001
11002 elsif Nam = Name_Has_Element then
11003
11004 -- Has_Element (Container, Cursor) => Boolean
11005
11006 F2 := Next_Formal (F1);
11007 if Etype (F2) /= Cursor
11008 or else Etype (Ent) /= Standard_Boolean
11009 or else Present (Next_Formal (F2))
11010 then
11011 Error_Msg_N ("no match for Has_Element iterable primitive", N);
11012 end if;
11013
11014 elsif Nam = Name_Element then
11015 F2 := Next_Formal (F1);
11016
11017 if No (F2)
11018 or else Etype (F2) /= Cursor
11019 or else Present (Next_Formal (F2))
11020 then
11021 Error_Msg_N ("no match for Element iterable primitive", N);
11022 end if;
11023 null;
11024
11025 else
11026 raise Program_Error;
11027 end if;
11028
11029 else
11030 -- Overloaded case: find subprogram with proper signature.
11031 -- Caller will report error if no match is found.
11032
11033 declare
11034 I : Interp_Index;
11035 It : Interp;
11036
11037 begin
11038 Get_First_Interp (N, I, It);
11039 while Present (It.Typ) loop
11040 if Ekind (It.Nam) = E_Function
11041 and then Scope (It.Nam) = Scope (Typ)
11042 and then Etype (First_Formal (It.Nam)) = Typ
11043 then
11044 F1 := First_Formal (It.Nam);
11045
11046 if Nam = Name_First then
11047 if Etype (It.Nam) = Cursor
11048 and then No (Next_Formal (F1))
11049 then
11050 Set_Entity (N, It.Nam);
11051 exit;
11052 end if;
11053
11054 elsif Nam = Name_Next then
11055 F2 := Next_Formal (F1);
11056
11057 if Present (F2)
11058 and then No (Next_Formal (F2))
11059 and then Etype (F2) = Cursor
11060 and then Etype (It.Nam) = Cursor
11061 then
11062 Set_Entity (N, It.Nam);
11063 exit;
11064 end if;
11065
11066 elsif Nam = Name_Has_Element then
11067 F2 := Next_Formal (F1);
11068
11069 if Present (F2)
11070 and then No (Next_Formal (F2))
11071 and then Etype (F2) = Cursor
11072 and then Etype (It.Nam) = Standard_Boolean
11073 then
11074 Set_Entity (N, It.Nam);
11075 F2 := Next_Formal (F1);
11076 exit;
11077 end if;
11078
11079 elsif Nam = Name_Element then
11080 F2 := Next_Formal (F1);
11081
11082 if Present (F2)
11083 and then No (Next_Formal (F2))
11084 and then Etype (F2) = Cursor
11085 then
11086 Set_Entity (N, It.Nam);
11087 exit;
11088 end if;
11089 end if;
11090 end if;
11091
11092 Get_Next_Interp (I, It);
11093 end loop;
11094 end;
11095 end if;
11096 end Resolve_Iterable_Operation;
11097
11098 ----------------
11099 -- Set_Biased --
11100 ----------------
11101
11102 procedure Set_Biased
11103 (E : Entity_Id;
11104 N : Node_Id;
11105 Msg : String;
11106 Biased : Boolean := True)
11107 is
11108 begin
11109 if Biased then
11110 Set_Has_Biased_Representation (E);
11111
11112 if Warn_On_Biased_Representation then
11113 Error_Msg_NE
11114 ("?B?" & Msg & " forces biased representation for&", N, E);
11115 end if;
11116 end if;
11117 end Set_Biased;
11118
11119 --------------------
11120 -- Set_Enum_Esize --
11121 --------------------
11122
11123 procedure Set_Enum_Esize (T : Entity_Id) is
11124 Lo : Uint;
11125 Hi : Uint;
11126 Sz : Nat;
11127
11128 begin
11129 Init_Alignment (T);
11130
11131 -- Find the minimum standard size (8,16,32,64) that fits
11132
11133 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
11134 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
11135
11136 if Lo < 0 then
11137 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
11138 Sz := Standard_Character_Size; -- May be > 8 on some targets
11139
11140 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
11141 Sz := 16;
11142
11143 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
11144 Sz := 32;
11145
11146 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
11147 Sz := 64;
11148 end if;
11149
11150 else
11151 if Hi < Uint_2**08 then
11152 Sz := Standard_Character_Size; -- May be > 8 on some targets
11153
11154 elsif Hi < Uint_2**16 then
11155 Sz := 16;
11156
11157 elsif Hi < Uint_2**32 then
11158 Sz := 32;
11159
11160 else pragma Assert (Hi < Uint_2**63);
11161 Sz := 64;
11162 end if;
11163 end if;
11164
11165 -- That minimum is the proper size unless we have a foreign convention
11166 -- and the size required is 32 or less, in which case we bump the size
11167 -- up to 32. This is required for C and C++ and seems reasonable for
11168 -- all other foreign conventions.
11169
11170 if Has_Foreign_Convention (T)
11171 and then Esize (T) < Standard_Integer_Size
11172
11173 -- Don't do this if Short_Enums on target
11174
11175 and then not Target_Short_Enums
11176 then
11177 Init_Esize (T, Standard_Integer_Size);
11178 else
11179 Init_Esize (T, Sz);
11180 end if;
11181 end Set_Enum_Esize;
11182
11183 -----------------------------
11184 -- Uninstall_Discriminants --
11185 -----------------------------
11186
11187 procedure Uninstall_Discriminants (E : Entity_Id) is
11188 Disc : Entity_Id;
11189 Prev : Entity_Id;
11190 Outer : Entity_Id;
11191
11192 begin
11193 -- Discriminants have been made visible for type declarations and
11194 -- protected type declarations, not for subtype declarations.
11195
11196 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11197 Disc := First_Discriminant (E);
11198 while Present (Disc) loop
11199 if Disc /= Current_Entity (Disc) then
11200 Prev := Current_Entity (Disc);
11201 while Present (Prev)
11202 and then Present (Homonym (Prev))
11203 and then Homonym (Prev) /= Disc
11204 loop
11205 Prev := Homonym (Prev);
11206 end loop;
11207 else
11208 Prev := Empty;
11209 end if;
11210
11211 Set_Is_Immediately_Visible (Disc, False);
11212
11213 Outer := Homonym (Disc);
11214 while Present (Outer) and then Scope (Outer) = E loop
11215 Outer := Homonym (Outer);
11216 end loop;
11217
11218 -- Reset homonym link of other entities, but do not modify link
11219 -- between entities in current scope, so that the back-end can
11220 -- have a proper count of local overloadings.
11221
11222 if No (Prev) then
11223 Set_Name_Entity_Id (Chars (Disc), Outer);
11224
11225 elsif Scope (Prev) /= Scope (Disc) then
11226 Set_Homonym (Prev, Outer);
11227 end if;
11228
11229 Next_Discriminant (Disc);
11230 end loop;
11231 end if;
11232 end Uninstall_Discriminants;
11233
11234 -------------------------------------------
11235 -- Uninstall_Discriminants_And_Pop_Scope --
11236 -------------------------------------------
11237
11238 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
11239 begin
11240 if Has_Discriminants (E) then
11241 Uninstall_Discriminants (E);
11242 Pop_Scope;
11243 end if;
11244 end Uninstall_Discriminants_And_Pop_Scope;
11245
11246 ------------------------------
11247 -- Validate_Address_Clauses --
11248 ------------------------------
11249
11250 procedure Validate_Address_Clauses is
11251 begin
11252 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
11253 declare
11254 ACCR : Address_Clause_Check_Record
11255 renames Address_Clause_Checks.Table (J);
11256
11257 Expr : Node_Id;
11258
11259 X_Alignment : Uint;
11260 Y_Alignment : Uint;
11261
11262 X_Size : Uint;
11263 Y_Size : Uint;
11264
11265 begin
11266 -- Skip processing of this entry if warning already posted
11267
11268 if not Address_Warning_Posted (ACCR.N) then
11269 Expr := Original_Node (Expression (ACCR.N));
11270
11271 -- Get alignments
11272
11273 X_Alignment := Alignment (ACCR.X);
11274 Y_Alignment := Alignment (ACCR.Y);
11275
11276 -- Similarly obtain sizes
11277
11278 X_Size := Esize (ACCR.X);
11279 Y_Size := Esize (ACCR.Y);
11280
11281 -- Check for large object overlaying smaller one
11282
11283 if Y_Size > Uint_0
11284 and then X_Size > Uint_0
11285 and then X_Size > Y_Size
11286 then
11287 Error_Msg_NE
11288 ("??& overlays smaller object", ACCR.N, ACCR.X);
11289 Error_Msg_N
11290 ("\??program execution may be erroneous", ACCR.N);
11291 Error_Msg_Uint_1 := X_Size;
11292 Error_Msg_NE
11293 ("\??size of & is ^", ACCR.N, ACCR.X);
11294 Error_Msg_Uint_1 := Y_Size;
11295 Error_Msg_NE
11296 ("\??size of & is ^", ACCR.N, ACCR.Y);
11297
11298 -- Check for inadequate alignment, both of the base object
11299 -- and of the offset, if any.
11300
11301 -- Note: we do not check the alignment if we gave a size
11302 -- warning, since it would likely be redundant.
11303
11304 elsif Y_Alignment /= Uint_0
11305 and then (Y_Alignment < X_Alignment
11306 or else (ACCR.Off
11307 and then
11308 Nkind (Expr) = N_Attribute_Reference
11309 and then
11310 Attribute_Name (Expr) = Name_Address
11311 and then
11312 Has_Compatible_Alignment
11313 (ACCR.X, Prefix (Expr))
11314 /= Known_Compatible))
11315 then
11316 Error_Msg_NE
11317 ("??specified address for& may be inconsistent "
11318 & "with alignment", ACCR.N, ACCR.X);
11319 Error_Msg_N
11320 ("\??program execution may be erroneous (RM 13.3(27))",
11321 ACCR.N);
11322 Error_Msg_Uint_1 := X_Alignment;
11323 Error_Msg_NE
11324 ("\??alignment of & is ^", ACCR.N, ACCR.X);
11325 Error_Msg_Uint_1 := Y_Alignment;
11326 Error_Msg_NE
11327 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
11328 if Y_Alignment >= X_Alignment then
11329 Error_Msg_N
11330 ("\??but offset is not multiple of alignment", ACCR.N);
11331 end if;
11332 end if;
11333 end if;
11334 end;
11335 end loop;
11336 end Validate_Address_Clauses;
11337
11338 ---------------------------
11339 -- Validate_Independence --
11340 ---------------------------
11341
11342 procedure Validate_Independence is
11343 SU : constant Uint := UI_From_Int (System_Storage_Unit);
11344 N : Node_Id;
11345 E : Entity_Id;
11346 IC : Boolean;
11347 Comp : Entity_Id;
11348 Addr : Node_Id;
11349 P : Node_Id;
11350
11351 procedure Check_Array_Type (Atyp : Entity_Id);
11352 -- Checks if the array type Atyp has independent components, and
11353 -- if not, outputs an appropriate set of error messages.
11354
11355 procedure No_Independence;
11356 -- Output message that independence cannot be guaranteed
11357
11358 function OK_Component (C : Entity_Id) return Boolean;
11359 -- Checks one component to see if it is independently accessible, and
11360 -- if so yields True, otherwise yields False if independent access
11361 -- cannot be guaranteed. This is a conservative routine, it only
11362 -- returns True if it knows for sure, it returns False if it knows
11363 -- there is a problem, or it cannot be sure there is no problem.
11364
11365 procedure Reason_Bad_Component (C : Entity_Id);
11366 -- Outputs continuation message if a reason can be determined for
11367 -- the component C being bad.
11368
11369 ----------------------
11370 -- Check_Array_Type --
11371 ----------------------
11372
11373 procedure Check_Array_Type (Atyp : Entity_Id) is
11374 Ctyp : constant Entity_Id := Component_Type (Atyp);
11375
11376 begin
11377 -- OK if no alignment clause, no pack, and no component size
11378
11379 if not Has_Component_Size_Clause (Atyp)
11380 and then not Has_Alignment_Clause (Atyp)
11381 and then not Is_Packed (Atyp)
11382 then
11383 return;
11384 end if;
11385
11386 -- Check actual component size
11387
11388 if not Known_Component_Size (Atyp)
11389 or else not (Addressable (Component_Size (Atyp))
11390 and then Component_Size (Atyp) < 64)
11391 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
11392 then
11393 No_Independence;
11394
11395 -- Bad component size, check reason
11396
11397 if Has_Component_Size_Clause (Atyp) then
11398 P := Get_Attribute_Definition_Clause
11399 (Atyp, Attribute_Component_Size);
11400
11401 if Present (P) then
11402 Error_Msg_Sloc := Sloc (P);
11403 Error_Msg_N ("\because of Component_Size clause#", N);
11404 return;
11405 end if;
11406 end if;
11407
11408 if Is_Packed (Atyp) then
11409 P := Get_Rep_Pragma (Atyp, Name_Pack);
11410
11411 if Present (P) then
11412 Error_Msg_Sloc := Sloc (P);
11413 Error_Msg_N ("\because of pragma Pack#", N);
11414 return;
11415 end if;
11416 end if;
11417
11418 -- No reason found, just return
11419
11420 return;
11421 end if;
11422
11423 -- Array type is OK independence-wise
11424
11425 return;
11426 end Check_Array_Type;
11427
11428 ---------------------
11429 -- No_Independence --
11430 ---------------------
11431
11432 procedure No_Independence is
11433 begin
11434 if Pragma_Name (N) = Name_Independent then
11435 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
11436 else
11437 Error_Msg_NE
11438 ("independent components cannot be guaranteed for&", N, E);
11439 end if;
11440 end No_Independence;
11441
11442 ------------------
11443 -- OK_Component --
11444 ------------------
11445
11446 function OK_Component (C : Entity_Id) return Boolean is
11447 Rec : constant Entity_Id := Scope (C);
11448 Ctyp : constant Entity_Id := Etype (C);
11449
11450 begin
11451 -- OK if no component clause, no Pack, and no alignment clause
11452
11453 if No (Component_Clause (C))
11454 and then not Is_Packed (Rec)
11455 and then not Has_Alignment_Clause (Rec)
11456 then
11457 return True;
11458 end if;
11459
11460 -- Here we look at the actual component layout. A component is
11461 -- addressable if its size is a multiple of the Esize of the
11462 -- component type, and its starting position in the record has
11463 -- appropriate alignment, and the record itself has appropriate
11464 -- alignment to guarantee the component alignment.
11465
11466 -- Make sure sizes are static, always assume the worst for any
11467 -- cases where we cannot check static values.
11468
11469 if not (Known_Static_Esize (C)
11470 and then
11471 Known_Static_Esize (Ctyp))
11472 then
11473 return False;
11474 end if;
11475
11476 -- Size of component must be addressable or greater than 64 bits
11477 -- and a multiple of bytes.
11478
11479 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
11480 return False;
11481 end if;
11482
11483 -- Check size is proper multiple
11484
11485 if Esize (C) mod Esize (Ctyp) /= 0 then
11486 return False;
11487 end if;
11488
11489 -- Check alignment of component is OK
11490
11491 if not Known_Component_Bit_Offset (C)
11492 or else Component_Bit_Offset (C) < Uint_0
11493 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
11494 then
11495 return False;
11496 end if;
11497
11498 -- Check alignment of record type is OK
11499
11500 if not Known_Alignment (Rec)
11501 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11502 then
11503 return False;
11504 end if;
11505
11506 -- All tests passed, component is addressable
11507
11508 return True;
11509 end OK_Component;
11510
11511 --------------------------
11512 -- Reason_Bad_Component --
11513 --------------------------
11514
11515 procedure Reason_Bad_Component (C : Entity_Id) is
11516 Rec : constant Entity_Id := Scope (C);
11517 Ctyp : constant Entity_Id := Etype (C);
11518
11519 begin
11520 -- If component clause present assume that's the problem
11521
11522 if Present (Component_Clause (C)) then
11523 Error_Msg_Sloc := Sloc (Component_Clause (C));
11524 Error_Msg_N ("\because of Component_Clause#", N);
11525 return;
11526 end if;
11527
11528 -- If pragma Pack clause present, assume that's the problem
11529
11530 if Is_Packed (Rec) then
11531 P := Get_Rep_Pragma (Rec, Name_Pack);
11532
11533 if Present (P) then
11534 Error_Msg_Sloc := Sloc (P);
11535 Error_Msg_N ("\because of pragma Pack#", N);
11536 return;
11537 end if;
11538 end if;
11539
11540 -- See if record has bad alignment clause
11541
11542 if Has_Alignment_Clause (Rec)
11543 and then Known_Alignment (Rec)
11544 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11545 then
11546 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
11547
11548 if Present (P) then
11549 Error_Msg_Sloc := Sloc (P);
11550 Error_Msg_N ("\because of Alignment clause#", N);
11551 end if;
11552 end if;
11553
11554 -- Couldn't find a reason, so return without a message
11555
11556 return;
11557 end Reason_Bad_Component;
11558
11559 -- Start of processing for Validate_Independence
11560
11561 begin
11562 for J in Independence_Checks.First .. Independence_Checks.Last loop
11563 N := Independence_Checks.Table (J).N;
11564 E := Independence_Checks.Table (J).E;
11565 IC := Pragma_Name (N) = Name_Independent_Components;
11566
11567 -- Deal with component case
11568
11569 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
11570 if not OK_Component (E) then
11571 No_Independence;
11572 Reason_Bad_Component (E);
11573 goto Continue;
11574 end if;
11575 end if;
11576
11577 -- Deal with record with Independent_Components
11578
11579 if IC and then Is_Record_Type (E) then
11580 Comp := First_Component_Or_Discriminant (E);
11581 while Present (Comp) loop
11582 if not OK_Component (Comp) then
11583 No_Independence;
11584 Reason_Bad_Component (Comp);
11585 goto Continue;
11586 end if;
11587
11588 Next_Component_Or_Discriminant (Comp);
11589 end loop;
11590 end if;
11591
11592 -- Deal with address clause case
11593
11594 if Is_Object (E) then
11595 Addr := Address_Clause (E);
11596
11597 if Present (Addr) then
11598 No_Independence;
11599 Error_Msg_Sloc := Sloc (Addr);
11600 Error_Msg_N ("\because of Address clause#", N);
11601 goto Continue;
11602 end if;
11603 end if;
11604
11605 -- Deal with independent components for array type
11606
11607 if IC and then Is_Array_Type (E) then
11608 Check_Array_Type (E);
11609 end if;
11610
11611 -- Deal with independent components for array object
11612
11613 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
11614 Check_Array_Type (Etype (E));
11615 end if;
11616
11617 <<Continue>> null;
11618 end loop;
11619 end Validate_Independence;
11620
11621 ------------------------------
11622 -- Validate_Iterable_Aspect --
11623 ------------------------------
11624
11625 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
11626 Assoc : Node_Id;
11627 Expr : Node_Id;
11628
11629 Prim : Node_Id;
11630 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
11631
11632 First_Id : Entity_Id;
11633 Next_Id : Entity_Id;
11634 Has_Element_Id : Entity_Id;
11635 Element_Id : Entity_Id;
11636
11637 begin
11638 -- If previous error aspect is unusable
11639
11640 if Cursor = Any_Type then
11641 return;
11642 end if;
11643
11644 First_Id := Empty;
11645 Next_Id := Empty;
11646 Has_Element_Id := Empty;
11647 Element_Id := Empty;
11648
11649 -- Each expression must resolve to a function with the proper signature
11650
11651 Assoc := First (Component_Associations (Expression (ASN)));
11652 while Present (Assoc) loop
11653 Expr := Expression (Assoc);
11654 Analyze (Expr);
11655
11656 Prim := First (Choices (Assoc));
11657
11658 if Nkind (Prim) /= N_Identifier
11659 or else Present (Next (Prim))
11660 then
11661 Error_Msg_N ("illegal name in association", Prim);
11662
11663 elsif Chars (Prim) = Name_First then
11664 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
11665 First_Id := Entity (Expr);
11666
11667 elsif Chars (Prim) = Name_Next then
11668 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
11669 Next_Id := Entity (Expr);
11670
11671 elsif Chars (Prim) = Name_Has_Element then
11672 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
11673 Has_Element_Id := Entity (Expr);
11674
11675 elsif Chars (Prim) = Name_Element then
11676 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
11677 Element_Id := Entity (Expr);
11678
11679 else
11680 Error_Msg_N ("invalid name for iterable function", Prim);
11681 end if;
11682
11683 Next (Assoc);
11684 end loop;
11685
11686 if No (First_Id) then
11687 Error_Msg_N ("match for First primitive not found", ASN);
11688
11689 elsif No (Next_Id) then
11690 Error_Msg_N ("match for Next primitive not found", ASN);
11691
11692 elsif No (Has_Element_Id) then
11693 Error_Msg_N ("match for Has_Element primitive not found", ASN);
11694
11695 elsif No (Element_Id) then
11696 null; -- Optional.
11697 end if;
11698 end Validate_Iterable_Aspect;
11699
11700 -----------------------------------
11701 -- Validate_Unchecked_Conversion --
11702 -----------------------------------
11703
11704 procedure Validate_Unchecked_Conversion
11705 (N : Node_Id;
11706 Act_Unit : Entity_Id)
11707 is
11708 Source : Entity_Id;
11709 Target : Entity_Id;
11710 Vnode : Node_Id;
11711
11712 begin
11713 -- Obtain source and target types. Note that we call Ancestor_Subtype
11714 -- here because the processing for generic instantiation always makes
11715 -- subtypes, and we want the original frozen actual types.
11716
11717 -- If we are dealing with private types, then do the check on their
11718 -- fully declared counterparts if the full declarations have been
11719 -- encountered (they don't have to be visible, but they must exist).
11720
11721 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
11722
11723 if Is_Private_Type (Source)
11724 and then Present (Underlying_Type (Source))
11725 then
11726 Source := Underlying_Type (Source);
11727 end if;
11728
11729 Target := Ancestor_Subtype (Etype (Act_Unit));
11730
11731 -- If either type is generic, the instantiation happens within a generic
11732 -- unit, and there is nothing to check. The proper check will happen
11733 -- when the enclosing generic is instantiated.
11734
11735 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
11736 return;
11737 end if;
11738
11739 if Is_Private_Type (Target)
11740 and then Present (Underlying_Type (Target))
11741 then
11742 Target := Underlying_Type (Target);
11743 end if;
11744
11745 -- Source may be unconstrained array, but not target
11746
11747 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
11748 Error_Msg_N
11749 ("unchecked conversion to unconstrained array not allowed", N);
11750 return;
11751 end if;
11752
11753 -- Warn if conversion between two different convention pointers
11754
11755 if Is_Access_Type (Target)
11756 and then Is_Access_Type (Source)
11757 and then Convention (Target) /= Convention (Source)
11758 and then Warn_On_Unchecked_Conversion
11759 then
11760 -- Give warnings for subprogram pointers only on most targets. The
11761 -- exception is VMS, where data pointers can have different lengths
11762 -- depending on the pointer convention.
11763
11764 if Is_Access_Subprogram_Type (Target)
11765 or else Is_Access_Subprogram_Type (Source)
11766 or else OpenVMS_On_Target
11767 then
11768 Error_Msg_N
11769 ("?z?conversion between pointers with different conventions!",
11770 N);
11771 end if;
11772 end if;
11773
11774 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
11775 -- warning when compiling GNAT-related sources.
11776
11777 if Warn_On_Unchecked_Conversion
11778 and then not In_Predefined_Unit (N)
11779 and then RTU_Loaded (Ada_Calendar)
11780 and then
11781 (Chars (Source) = Name_Time
11782 or else
11783 Chars (Target) = Name_Time)
11784 then
11785 -- If Ada.Calendar is loaded and the name of one of the operands is
11786 -- Time, there is a good chance that this is Ada.Calendar.Time.
11787
11788 declare
11789 Calendar_Time : constant Entity_Id :=
11790 Full_View (RTE (RO_CA_Time));
11791 begin
11792 pragma Assert (Present (Calendar_Time));
11793
11794 if Source = Calendar_Time or else Target = Calendar_Time then
11795 Error_Msg_N
11796 ("?z?representation of 'Time values may change between " &
11797 "'G'N'A'T versions", N);
11798 end if;
11799 end;
11800 end if;
11801
11802 -- Make entry in unchecked conversion table for later processing by
11803 -- Validate_Unchecked_Conversions, which will check sizes and alignments
11804 -- (using values set by the back-end where possible). This is only done
11805 -- if the appropriate warning is active.
11806
11807 if Warn_On_Unchecked_Conversion then
11808 Unchecked_Conversions.Append
11809 (New_Val => UC_Entry'(Eloc => Sloc (N),
11810 Source => Source,
11811 Target => Target,
11812 Act_Unit => Act_Unit));
11813
11814 -- If both sizes are known statically now, then back end annotation
11815 -- is not required to do a proper check but if either size is not
11816 -- known statically, then we need the annotation.
11817
11818 if Known_Static_RM_Size (Source)
11819 and then
11820 Known_Static_RM_Size (Target)
11821 then
11822 null;
11823 else
11824 Back_Annotate_Rep_Info := True;
11825 end if;
11826 end if;
11827
11828 -- If unchecked conversion to access type, and access type is declared
11829 -- in the same unit as the unchecked conversion, then set the flag
11830 -- No_Strict_Aliasing (no strict aliasing is implicit here)
11831
11832 if Is_Access_Type (Target) and then
11833 In_Same_Source_Unit (Target, N)
11834 then
11835 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
11836 end if;
11837
11838 -- Generate N_Validate_Unchecked_Conversion node for back end in case
11839 -- the back end needs to perform special validation checks.
11840
11841 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
11842 -- have full expansion and the back end is called ???
11843
11844 Vnode :=
11845 Make_Validate_Unchecked_Conversion (Sloc (N));
11846 Set_Source_Type (Vnode, Source);
11847 Set_Target_Type (Vnode, Target);
11848
11849 -- If the unchecked conversion node is in a list, just insert before it.
11850 -- If not we have some strange case, not worth bothering about.
11851
11852 if Is_List_Member (N) then
11853 Insert_After (N, Vnode);
11854 end if;
11855 end Validate_Unchecked_Conversion;
11856
11857 ------------------------------------
11858 -- Validate_Unchecked_Conversions --
11859 ------------------------------------
11860
11861 procedure Validate_Unchecked_Conversions is
11862 begin
11863 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
11864 declare
11865 T : UC_Entry renames Unchecked_Conversions.Table (N);
11866
11867 Eloc : constant Source_Ptr := T.Eloc;
11868 Source : constant Entity_Id := T.Source;
11869 Target : constant Entity_Id := T.Target;
11870 Act_Unit : constant Entity_Id := T.Act_Unit;
11871
11872 Source_Siz : Uint;
11873 Target_Siz : Uint;
11874
11875 begin
11876 -- Skip if function marked as warnings off
11877
11878 if Warnings_Off (Act_Unit) then
11879 goto Continue;
11880 end if;
11881
11882 -- This validation check, which warns if we have unequal sizes for
11883 -- unchecked conversion, and thus potentially implementation
11884 -- dependent semantics, is one of the few occasions on which we
11885 -- use the official RM size instead of Esize. See description in
11886 -- Einfo "Handling of Type'Size Values" for details.
11887
11888 if Serious_Errors_Detected = 0
11889 and then Known_Static_RM_Size (Source)
11890 and then Known_Static_RM_Size (Target)
11891
11892 -- Don't do the check if warnings off for either type, note the
11893 -- deliberate use of OR here instead of OR ELSE to get the flag
11894 -- Warnings_Off_Used set for both types if appropriate.
11895
11896 and then not (Has_Warnings_Off (Source)
11897 or
11898 Has_Warnings_Off (Target))
11899 then
11900 Source_Siz := RM_Size (Source);
11901 Target_Siz := RM_Size (Target);
11902
11903 if Source_Siz /= Target_Siz then
11904 Error_Msg
11905 ("?z?types for unchecked conversion have different sizes!",
11906 Eloc);
11907
11908 if All_Errors_Mode then
11909 Error_Msg_Name_1 := Chars (Source);
11910 Error_Msg_Uint_1 := Source_Siz;
11911 Error_Msg_Name_2 := Chars (Target);
11912 Error_Msg_Uint_2 := Target_Siz;
11913 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
11914
11915 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
11916
11917 if Is_Discrete_Type (Source)
11918 and then
11919 Is_Discrete_Type (Target)
11920 then
11921 if Source_Siz > Target_Siz then
11922 Error_Msg
11923 ("\?z?^ high order bits of source will "
11924 & "be ignored!", Eloc);
11925
11926 elsif Is_Unsigned_Type (Source) then
11927 Error_Msg
11928 ("\?z?source will be extended with ^ high order "
11929 & "zero bits!", Eloc);
11930
11931 else
11932 Error_Msg
11933 ("\?z?source will be extended with ^ high order "
11934 & "sign bits!", Eloc);
11935 end if;
11936
11937 elsif Source_Siz < Target_Siz then
11938 if Is_Discrete_Type (Target) then
11939 if Bytes_Big_Endian then
11940 Error_Msg
11941 ("\?z?target value will include ^ undefined "
11942 & "low order bits!", Eloc);
11943 else
11944 Error_Msg
11945 ("\?z?target value will include ^ undefined "
11946 & "high order bits!", Eloc);
11947 end if;
11948
11949 else
11950 Error_Msg
11951 ("\?z?^ trailing bits of target value will be "
11952 & "undefined!", Eloc);
11953 end if;
11954
11955 else pragma Assert (Source_Siz > Target_Siz);
11956 Error_Msg
11957 ("\?z?^ trailing bits of source will be ignored!",
11958 Eloc);
11959 end if;
11960 end if;
11961 end if;
11962 end if;
11963
11964 -- If both types are access types, we need to check the alignment.
11965 -- If the alignment of both is specified, we can do it here.
11966
11967 if Serious_Errors_Detected = 0
11968 and then Ekind (Source) in Access_Kind
11969 and then Ekind (Target) in Access_Kind
11970 and then Target_Strict_Alignment
11971 and then Present (Designated_Type (Source))
11972 and then Present (Designated_Type (Target))
11973 then
11974 declare
11975 D_Source : constant Entity_Id := Designated_Type (Source);
11976 D_Target : constant Entity_Id := Designated_Type (Target);
11977
11978 begin
11979 if Known_Alignment (D_Source)
11980 and then
11981 Known_Alignment (D_Target)
11982 then
11983 declare
11984 Source_Align : constant Uint := Alignment (D_Source);
11985 Target_Align : constant Uint := Alignment (D_Target);
11986
11987 begin
11988 if Source_Align < Target_Align
11989 and then not Is_Tagged_Type (D_Source)
11990
11991 -- Suppress warning if warnings suppressed on either
11992 -- type or either designated type. Note the use of
11993 -- OR here instead of OR ELSE. That is intentional,
11994 -- we would like to set flag Warnings_Off_Used in
11995 -- all types for which warnings are suppressed.
11996
11997 and then not (Has_Warnings_Off (D_Source)
11998 or
11999 Has_Warnings_Off (D_Target)
12000 or
12001 Has_Warnings_Off (Source)
12002 or
12003 Has_Warnings_Off (Target))
12004 then
12005 Error_Msg_Uint_1 := Target_Align;
12006 Error_Msg_Uint_2 := Source_Align;
12007 Error_Msg_Node_1 := D_Target;
12008 Error_Msg_Node_2 := D_Source;
12009 Error_Msg
12010 ("?z?alignment of & (^) is stricter than "
12011 & "alignment of & (^)!", Eloc);
12012 Error_Msg
12013 ("\?z?resulting access value may have invalid "
12014 & "alignment!", Eloc);
12015 end if;
12016 end;
12017 end if;
12018 end;
12019 end if;
12020 end;
12021
12022 <<Continue>>
12023 null;
12024 end loop;
12025 end Validate_Unchecked_Conversions;
12026
12027 end Sem_Ch13;