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