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