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