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