* gcc.dg/Wtrampolines.c: XFAIL AIX.
[official-gcc.git] / gcc / ada / sem_ch13.adb
blobbff49e6430b09cece77766a776c09099b68744a7
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 ------------------------------------------------------------------------------
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 Sinput; use Sinput;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
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;
72 with GNAT.Heap_Sort_G;
74 package body Sem_Ch13 is
76 SSU : constant Pos := System_Storage_Unit;
77 -- Convenient short hand for commonly used constant
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
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.
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.
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.
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.
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.
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.
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).
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.
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:
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
164 -- When flag Do_Checks is set, this routine will flag duplicate uses of
165 -- aspects.
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.
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.
184 -- Only scalar types can have static predicates, so False is always
185 -- returned for non-scalar types.
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:
191 -- "ABC" < "DEF"
193 -- is not a static expression. So even though the clearly faulty RM wording
194 -- allows the following:
196 -- subtype S is String with Static_Predicate => S < "DEF"
198 -- We can't allow this, otherwise we have predicate-static applying to a
199 -- larger class than static expressions, which was never intended.
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.
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.
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.
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.
239 ---------------------------------------------------
240 -- Table for Validate_Compile_Time_Warning_Error --
241 ---------------------------------------------------
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.
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.
255 type CTWE_Entry is record
256 Eloc : Source_Ptr;
257 -- Source location used in warnings and error messages
259 Prag : Node_Id;
260 -- Pragma Compile_Time_Error or Compile_Time_Warning
262 Scope : Node_Id;
263 -- The scope which encloses the pragma
264 end record;
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");
274 ----------------------------------------------
275 -- Table for Validate_Unchecked_Conversions --
276 ----------------------------------------------
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.
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.
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;
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");
304 ----------------------------------------
305 -- Table for Validate_Address_Clauses --
306 ----------------------------------------
308 -- If an address clause has the form
310 -- for X'Address use Expr
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.
321 type Address_Clause_Check_Record is record
322 N : Node_Id;
323 -- The address clause
325 X : Entity_Id;
326 -- The entity of the object subject to the address clause
328 A : Uint;
329 -- The value of the address in the first case
331 Y : Entity_Id;
332 -- The entity of the object being overlaid in the second case
334 Off : Boolean;
335 -- Whether the address is offset within Y in the second case
336 end record;
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");
346 -----------------------------------------
347 -- Adjust_Record_For_Reverse_Bit_Order --
348 -----------------------------------------
350 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
351 Comp : Node_Id;
352 CC : Node_Id;
354 begin
355 -- Processing depends on version of Ada
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.
361 if Ada_Version < Ada_2005 then
362 Comp := First_Component_Or_Discriminant (R);
363 while Present (Comp) loop
364 CC := Component_Clause (Comp);
366 -- If component clause is present, then deal with the non-default
367 -- bit order case for Ada 95 mode.
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.
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);
381 Storage_Unit_Offset : constant Uint :=
382 CFB / System_Storage_Unit;
384 Start_Bit : constant Uint :=
385 CFB mod System_Storage_Unit;
387 begin
388 -- Cases where field goes over storage unit boundary
390 if Start_Bit + CSZ > System_Storage_Unit then
392 -- Allow multi-byte field but generate warning
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);
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;
411 -- Do not allow non-contiguous field
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;
425 -- Case where field fits in one storage unit
427 else
428 -- Give warning if suspicious component clause
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;
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:
448 -- First_Bit .. Last_Bit Component_Bit_Offset
449 -- old new old new
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
456 -- 1 .. 1 6 .. 6 1 6
457 -- 1 .. 4 3 .. 6 1 3
458 -- 4 .. 7 0 .. 3 4 0
460 -- The rule is that the first bit is is obtained by
461 -- subtracting the old ending bit from storage_unit - 1.
463 Set_Component_Bit_Offset
464 (Comp,
465 (Storage_Unit_Offset * System_Storage_Unit) +
466 (System_Storage_Unit - 1) -
467 (Start_Bit + CSZ - 1));
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;
477 Next_Component_Or_Discriminant (Comp);
478 end loop;
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.
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
492 Num_CC : Natural;
493 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
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.
503 Num_CC := 0;
504 Comp := First_Component_Or_Discriminant (R);
505 while Present (Comp) loop
506 CC := Component_Clause (Comp);
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));
513 begin
514 -- Case of component with last bit >= max machine scalar
516 if Lbit >= Max_Machine_Scalar_Size then
518 -- This is allowed only if first bit is zero, and
519 -- last bit + 1 is a multiple of storage unit size.
521 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
523 -- This is the case to give a warning if enabled
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);
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;
541 -- Give error message for RM 13.5.1(10) violation
543 else
544 Error_Msg_FE
545 ("machine scalar rules not followed for&",
546 First_Bit (CC), Comp);
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));
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));
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;
571 -- OK case of machine scalar related component clause,
572 -- For now, just count them.
574 else
575 Num_CC := Num_CC + 1;
576 end if;
577 end;
578 end if;
580 Next_Component_Or_Discriminant (Comp);
581 end loop;
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.
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.
594 function CP_Lt (Op1, Op2 : Natural) return Boolean;
595 -- Compare routine for Sort
597 procedure CP_Move (From : Natural; To : Natural);
598 -- Move routine for Sort
600 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
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).
608 MaxL : Uint;
609 -- Maximum last bit value of any component in this set
611 MSS : Uint;
612 -- Corresponding machine scalar size
614 -----------
615 -- CP_Lt --
616 -----------
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;
624 -------------
625 -- CP_Move --
626 -------------
628 procedure CP_Move (From : Natural; To : Natural) is
629 begin
630 Comps (To) := Comps (From);
631 end CP_Move;
633 -- Start of processing for Sort_CC
635 begin
636 -- Collect the machine scalar relevant component clauses
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);
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.
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;
662 Next_Component_Or_Discriminant (Comp);
663 end loop;
665 -- Sort by ascending position number
667 Sorting.Sort (Num_CC);
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.
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;
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.
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.
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;
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:
721 -- First_Bit .. Last_Bit Component_Bit_Offset
722 -- old new old new
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
729 -- 1 .. 1 6 .. 6 1 6
730 -- 1 .. 4 3 .. 6 1 3
731 -- 4 .. 7 0 .. 3 4 0
733 -- The rule is that the first bit is obtained by subtracting
734 -- the old ending bit from machine scalar size - 1.
736 for C in Start .. Stop loop
737 declare
738 Comp : constant Entity_Id := Comps (C);
739 CC : constant Node_Id := Component_Clause (Comp);
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));
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;
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;
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;
776 -------------------------------------
777 -- Alignment_Check_For_Size_Change --
778 -------------------------------------
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.
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;
795 -------------------------------------
796 -- Analyze_Aspects_At_Freeze_Point --
797 -------------------------------------
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.
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:
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;
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.
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.
832 ----------------------------------
833 -- Analyze_Aspect_Default_Value --
834 ----------------------------------
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);
842 begin
843 Error_Msg_Name_1 := Chars (Id);
845 if not Is_Type (Ent) then
846 Error_Msg_N ("aspect% can only apply to a type", Id);
847 return;
849 elsif not Is_First_Subtype (Ent) then
850 Error_Msg_N ("aspect% cannot apply to subtype", Id);
851 return;
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;
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;
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;
870 Set_Has_Default_Aspect (Base_Type (Ent));
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;
879 ---------------------------------
880 -- Inherit_Delayed_Rep_Aspects --
881 ---------------------------------
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
888 N : Node_Id;
889 -- Item from Rep_Item chain
891 A : Aspect_Id;
893 begin
894 -- Loop through delayed aspects for the parent type
896 N := ASN;
897 while Present (N) loop
898 if Nkind (N) = N_Aspect_Specification then
899 exit when Entity (N) /= P;
901 if Is_Delayed_Aspect (N) then
902 A := Get_Aspect_Id (Chars (Identifier (N)));
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.
911 case A is
913 -- Alignment
915 when Aspect_Alignment =>
916 if not Has_Alignment_Clause (E) then
917 Set_Alignment (E, Alignment (P));
918 end if;
920 -- Atomic
922 when Aspect_Atomic =>
923 if Is_Atomic (P) then
924 Set_Is_Atomic (E);
925 end if;
927 -- Atomic_Components
929 when Aspect_Atomic_Components =>
930 if Has_Atomic_Components (P) then
931 Set_Has_Atomic_Components (Base_Type (E));
932 end if;
934 -- Bit_Order
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;
945 -- Component_Size
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;
955 -- Machine_Radix
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;
964 -- Object_Size (also Size which also sets Object_Size)
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;
975 -- Pack
977 when Aspect_Pack =>
978 if not Is_Packed (E) then
979 Set_Is_Packed (Base_Type (E));
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;
988 -- Scalar_Storage_Order
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));
998 -- Clear default SSO indications, since the aspect
999 -- overrides the default.
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;
1005 -- Small
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;
1014 -- Storage_Size
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;
1024 -- Value_Size
1026 when Aspect_Value_Size =>
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.
1032 null;
1034 -- Volatile
1036 when Aspect_Volatile =>
1037 if Is_Volatile (P) then
1038 Set_Is_Volatile (E);
1039 end if;
1041 -- Volatile_Full_Access
1043 when Aspect_Volatile_Full_Access =>
1044 if Is_Volatile_Full_Access (P) then
1045 Set_Is_Volatile_Full_Access (E);
1046 end if;
1048 -- Volatile_Components
1050 when Aspect_Volatile_Components =>
1051 if Has_Volatile_Components (P) then
1052 Set_Has_Volatile_Components (Base_Type (E));
1053 end if;
1055 -- That should be all the Rep Aspects
1057 when others =>
1058 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1059 null;
1061 end case;
1062 end if;
1063 end if;
1065 N := Next_Rep_Item (N);
1066 end loop;
1067 end Inherit_Delayed_Rep_Aspects;
1069 -------------------------------------
1070 -- Make_Pragma_From_Boolean_Aspect --
1071 -------------------------------------
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);
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.
1086 -----------------------------------------
1087 -- Check_False_Aspect_For_Derived_Type --
1088 -----------------------------------------
1090 procedure Check_False_Aspect_For_Derived_Type is
1091 Par : Node_Id;
1093 begin
1094 -- We are only checking derived types
1096 if not Is_Derived_Type (E) then
1097 return;
1098 end if;
1100 Par := Nearest_Ancestor (E);
1102 case A_Id is
1103 when Aspect_Atomic | Aspect_Shared =>
1104 if not Is_Atomic (Par) then
1105 return;
1106 end if;
1108 when Aspect_Atomic_Components =>
1109 if not Has_Atomic_Components (Par) then
1110 return;
1111 end if;
1113 when Aspect_Discard_Names =>
1114 if not Discard_Names (Par) then
1115 return;
1116 end if;
1118 when Aspect_Pack =>
1119 if not Is_Packed (Par) then
1120 return;
1121 end if;
1123 when Aspect_Unchecked_Union =>
1124 if not Is_Unchecked_Union (Par) then
1125 return;
1126 end if;
1128 when Aspect_Volatile =>
1129 if not Is_Volatile (Par) then
1130 return;
1131 end if;
1133 when Aspect_Volatile_Components =>
1134 if not Has_Volatile_Components (Par) then
1135 return;
1136 end if;
1138 when Aspect_Volatile_Full_Access =>
1139 if not Is_Volatile_Full_Access (Par) then
1140 return;
1141 end if;
1143 when others =>
1144 return;
1145 end case;
1147 -- Fall through means we are canceling an inherited aspect
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;
1154 -- Local variables
1156 Prag : Node_Id;
1158 -- Start of processing for Make_Pragma_From_Boolean_Aspect
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.
1165 if Is_False (Static_Boolean (Expr)) then
1166 Check_False_Aspect_For_Derived_Type;
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)))));
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;
1185 -- Local variables
1187 A_Id : Aspect_Id;
1188 ASN : Node_Id;
1189 Ritem : Node_Id;
1191 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1193 begin
1194 -- Must be visible in current scope
1196 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1197 return;
1198 end if;
1200 -- Look for aspect specification entries for this entity
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;
1207 if Is_Delayed_Aspect (ASN) then
1208 A_Id := Get_Aspect_Id (ASN);
1210 case A_Id is
1212 -- For aspects whose expression is an optional Boolean, make
1213 -- the corresponding pragma at the freeze point.
1215 when Boolean_Aspects |
1216 Library_Unit_Aspects =>
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.
1228 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1229 null;
1231 -- Otherwise create a corresponding pragma
1233 else
1234 Make_Pragma_From_Boolean_Aspect (ASN);
1235 end if;
1237 -- Special handling for aspects that don't correspond to
1238 -- pragmas/attributes.
1240 when Aspect_Default_Value |
1241 Aspect_Default_Component_Value =>
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.
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;
1257 -- Ditto for iterator aspects, because the corresponding
1258 -- attributes may not have been analyzed yet.
1260 when Aspect_Constant_Indexing |
1261 Aspect_Variable_Indexing |
1262 Aspect_Default_Iterator |
1263 Aspect_Iterator_Element =>
1264 Analyze (Expression (ASN));
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;
1272 when Aspect_Iterable =>
1273 Validate_Iterable_Aspect (E, ASN);
1275 when others =>
1276 null;
1277 end case;
1279 Ritem := Aspect_Rep_Item (ASN);
1281 if Present (Ritem) then
1282 Analyze (Ritem);
1283 end if;
1284 end if;
1285 end if;
1287 Next_Rep_Item (ASN);
1288 end loop;
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.
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;
1300 -----------------------------------
1301 -- Analyze_Aspect_Specifications --
1302 -----------------------------------
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
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.
1331 --------------
1332 -- Decorate --
1333 --------------
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;
1343 -------------------
1344 -- Insert_Pragma --
1345 -------------------
1347 procedure Insert_Pragma
1348 (Prag : Node_Id;
1349 Is_Instance : Boolean := False)
1351 Aux : Node_Id;
1352 Decl : Node_Id;
1353 Decls : List_Id;
1354 Def : Node_Id;
1355 Inserted : Boolean := False;
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.
1363 -- package body Pack with Aspect is
1365 -- package body Pack is
1366 -- pragma Prag;
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);
1376 if No (Decls) then
1377 Decls := New_List;
1378 Set_Declarations (N, Decls);
1379 end if;
1381 Prepend_To (Decls, Prag);
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.
1387 -- package Pack with Aspect is
1389 -- package Pack is
1390 -- pragma Prag;
1392 elsif Nkind_In (N, N_Generic_Package_Declaration,
1393 N_Package_Declaration)
1394 then
1395 Decls := Visible_Declarations (Specification (N));
1397 if No (Decls) then
1398 Decls := New_List;
1399 Set_Visible_Declarations (Specification (N), Decls);
1400 end if;
1402 -- The visible declarations of a generic instance have the
1403 -- following structure:
1405 -- <renamings of generic formals>
1406 -- <renamings of internally-generated spec and body>
1407 -- <first source declaration>
1409 -- Insert the pragma before the first source declaration by
1410 -- skipping the instance "header" to ensure proper visibility of
1411 -- all formals.
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;
1425 -- The pragma is placed after the instance "header"
1427 if not Inserted then
1428 Append_To (Decls, Prag);
1429 end if;
1431 -- Otherwise this is not a generic instance
1433 else
1434 Prepend_To (Decls, Prag);
1435 end if;
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.
1441 -- protected [type] Prot with Aspect is
1443 -- protected [type] Prot is
1444 -- pragma Prag;
1446 elsif Nkind (N) = N_Protected_Type_Declaration then
1447 Def := Protected_Definition (N);
1449 if No (Def) then
1450 Def :=
1451 Make_Protected_Definition (Sloc (N),
1452 Visible_Declarations => New_List,
1453 End_Label => Empty);
1455 Set_Protected_Definition (N, Def);
1456 end if;
1458 Decls := Visible_Declarations (Def);
1460 if No (Decls) then
1461 Decls := New_List;
1462 Set_Visible_Declarations (Def, Decls);
1463 end if;
1465 Prepend_To (Decls, Prag);
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.
1471 -- task [type] Prot with Aspect is
1473 -- task [type] Prot is
1474 -- pragma Prag;
1476 elsif Nkind (N) = N_Task_Type_Declaration then
1477 Def := Task_Definition (N);
1479 if No (Def) then
1480 Def :=
1481 Make_Task_Definition (Sloc (N),
1482 Visible_Declarations => New_List,
1483 End_Label => Empty);
1485 Set_Task_Definition (N, Def);
1486 end if;
1488 Decls := Visible_Declarations (Def);
1490 if No (Decls) then
1491 Decls := New_List;
1492 Set_Visible_Declarations (Def, Decls);
1493 end if;
1495 Prepend_To (Decls, Prag);
1497 -- When the context is a library unit, the pragma is added to the
1498 -- Pragmas_After list.
1500 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1501 Aux := Aux_Decls_Node (Parent (N));
1503 if No (Pragmas_After (Aux)) then
1504 Set_Pragmas_After (Aux, New_List);
1505 end if;
1507 Prepend (Prag, Pragmas_After (Aux));
1509 -- Default, the pragma is inserted after the context
1511 else
1512 Insert_After (N, Prag);
1513 end if;
1514 end Insert_Pragma;
1516 -- Local variables
1518 Aspect : Node_Id;
1519 Aitem : Node_Id;
1520 Ent : Node_Id;
1522 L : constant List_Id := Aspect_Specifications (N);
1524 Ins_Node : Node_Id := N;
1525 -- Insert pragmas/attribute definition clause after this node when no
1526 -- delayed analysis is required.
1528 -- Start of processing for Analyze_Aspect_Specifications
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.
1541 -- Some special cases don't require delay analysis, thus the aspect is
1542 -- analyzed right now.
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.
1550 pragma Assert (Present (L));
1552 -- Loop through aspects
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;
1564 Delay_Required : Boolean;
1565 -- Set False if delay is not required
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.
1571 procedure Analyze_Aspect_Convention;
1572 -- Perform analysis of aspect Convention
1574 procedure Analyze_Aspect_Export_Import;
1575 -- Perform analysis of aspects Export or Import
1577 procedure Analyze_Aspect_External_Link_Name;
1578 -- Perform analysis of aspects External_Name or Link_Name
1580 procedure Analyze_Aspect_Implicit_Dereference;
1581 -- Perform analysis of the Implicit_Dereference aspects
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.
1595 -------------------------------
1596 -- Analyze_Aspect_Convention --
1597 -------------------------------
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;
1607 begin
1608 -- Obtain all interfacing aspects that apply to the related
1609 -- entity.
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);
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.
1624 if Present (Expo) or else Present (Imp) then
1625 return;
1627 -- Otherwise Convention appears by itself
1629 else
1630 -- The aspect specifies a particular convention
1632 if Present (Expr) then
1633 Conv := New_Copy_Tree (Expr);
1635 -- Otherwise assume convention Ada
1637 else
1638 Conv := Make_Identifier (Loc, Name_Ada);
1639 end if;
1641 -- Generate:
1642 -- pragma Convention (<Conv>, <E>);
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))));
1652 Decorate (Aspect, Aitem);
1653 Insert_Pragma (Aitem);
1654 end if;
1655 end Analyze_Aspect_Convention;
1657 ----------------------------------
1658 -- Analyze_Aspect_Export_Import --
1659 ----------------------------------
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;
1668 begin
1669 -- Obtain all interfacing aspects that apply to the related
1670 -- entity.
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);
1681 -- The related entity cannot be subject to both aspects Export
1682 -- and Import.
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;
1693 -- A variable is most likely modified from the outside. Take
1694 -- Take the optimistic approach to avoid spurious errors.
1696 if Ekind (E) = E_Variable then
1697 Set_Never_Set_In_Source (E, False);
1698 end if;
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.
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);
1714 -- An imported object cannot be explicitly initialized
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;
1724 else
1725 pragma Assert (A_Id = Aspect_Export);
1726 Set_Is_Exported (E);
1727 end if;
1729 -- Create the proper form of pragma Export or Import taking
1730 -- into account Conversion, External_Name, and Link_Name.
1732 Aitem := Build_Export_Import_Pragma (Aspect, E);
1734 -- Otherwise the expression is either False or erroneous. There
1735 -- is no corresponding pragma.
1737 else
1738 Aitem := Empty;
1739 end if;
1740 end Analyze_Aspect_Export_Import;
1742 ---------------------------------------
1743 -- Analyze_Aspect_External_Link_Name --
1744 ---------------------------------------
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;
1753 begin
1754 -- Obtain all interfacing aspects that apply to the related
1755 -- entity.
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);
1766 -- Ensure that aspect External_Name applies to aspect Export or
1767 -- Import.
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;
1776 -- Otherwise ensure that aspect Link_Name applies to aspect
1777 -- Export or Import.
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;
1789 -----------------------------------------
1790 -- Analyze_Aspect_Implicit_Dereference --
1791 -----------------------------------------
1793 procedure Analyze_Aspect_Implicit_Dereference is
1794 Disc : Entity_Id;
1795 Parent_Disc : Entity_Id;
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);
1802 elsif not Is_Entity_Name (Expr) then
1803 Error_Msg_N
1804 ("aspect must name a discriminant of current type", Expr);
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;
1818 Next_Discriminant (Disc);
1819 end loop;
1821 -- Error if no proper access discriminant
1823 if No (Disc) then
1824 Error_Msg_NE ("not an access discriminant of&", Expr, E);
1825 return;
1826 end if;
1827 end if;
1829 -- For a type extension, check whether parent has a
1830 -- reference discriminant, to verify that use is proper.
1832 if Is_Derived_Type (E)
1833 and then Has_Discriminants (Etype (E))
1834 then
1835 Parent_Disc := Get_Reference_Discriminant (Etype (E));
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;
1847 -----------------------
1848 -- Make_Aitem_Pragma --
1849 -----------------------
1851 procedure Make_Aitem_Pragma
1852 (Pragma_Argument_Associations : List_Id;
1853 Pragma_Name : Name_Id)
1855 Args : List_Id := Pragma_Argument_Associations;
1857 begin
1858 -- We should never get here if aspect was disabled
1860 pragma Assert (not Is_Disabled (Aspect));
1862 -- Certain aspects allow for an optional name or expression. Do
1863 -- not generate a pragma with empty argument association list.
1865 if No (Args) or else No (Expression (First (Args))) then
1866 Args := No_List;
1867 end if;
1869 -- Build the pragma
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));
1879 -- Set additional semantic fields
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;
1887 Set_Corresponding_Aspect (Aitem, Aspect);
1888 Set_From_Aspect_Specification (Aitem);
1889 end Make_Aitem_Pragma;
1891 -- Start of processing for Analyze_Aspect_Specifications
1893 begin
1894 -- Skip aspect if already analyzed, to avoid looping in some cases
1896 if Analyzed (Aspect) then
1897 goto Continue;
1898 end if;
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.
1904 Check_Applicable_Policy (Aspect);
1906 if Is_Disabled (Aspect) then
1907 goto Continue;
1908 end if;
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.
1918 if Present (Expr) then
1919 Eloc := Sloc (First_Node (Expr));
1920 end if;
1922 -- Check restriction No_Implementation_Aspect_Specifications
1924 if Implementation_Defined_Aspect (A_Id) then
1925 Check_Restriction
1926 (No_Implementation_Aspect_Specifications, Aspect);
1927 end if;
1929 -- Check restriction No_Specification_Of_Aspect
1931 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1933 -- Mark aspect analyzed (actual analysis is delayed till later)
1935 Set_Analyzed (Aspect);
1936 Set_Entity (Aspect, E);
1937 Ent := New_Occurrence_Of (E, Sloc (Id));
1939 -- Check for duplicate aspect. Note that the Comes_From_Source
1940 -- test allows duplicate Pre/Post's that we generate internally
1941 -- to escape being flagged here.
1943 if No_Duplicates_Allowed (A_Id) then
1944 Anod := First (L);
1945 while Anod /= Aspect loop
1946 if Comes_From_Source (Aspect)
1947 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1948 then
1949 Error_Msg_Name_1 := Nam;
1950 Error_Msg_Sloc := Sloc (Anod);
1952 -- Case of same aspect specified twice
1954 if Class_Present (Anod) = Class_Present (Aspect) then
1955 if not Class_Present (Anod) then
1956 Error_Msg_NE
1957 ("aspect% for & previously given#",
1958 Id, E);
1959 else
1960 Error_Msg_NE
1961 ("aspect `%''Class` for & previously given#",
1962 Id, E);
1963 end if;
1964 end if;
1965 end if;
1967 Next (Anod);
1968 end loop;
1969 end if;
1971 -- Check some general restrictions on language defined aspects
1973 if not Implementation_Defined_Aspect (A_Id) then
1974 Error_Msg_Name_1 := Nam;
1976 -- Not allowed for renaming declarations. Examine the original
1977 -- node because a subprogram renaming may have been rewritten
1978 -- as a body.
1980 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
1981 Error_Msg_N
1982 ("aspect % not allowed for renaming declaration",
1983 Aspect);
1984 end if;
1986 -- Not allowed for formal type declarations
1988 if Nkind (N) = N_Formal_Type_Declaration then
1989 Error_Msg_N
1990 ("aspect % not allowed for formal type declaration",
1991 Aspect);
1992 end if;
1993 end if;
1995 -- Copy expression for later processing by the procedures
1996 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1998 Set_Entity (Id, New_Copy_Tree (Expr));
2000 -- Set Delay_Required as appropriate to aspect
2002 case Aspect_Delay (A_Id) is
2003 when Always_Delay =>
2004 Delay_Required := True;
2006 when Never_Delay =>
2007 Delay_Required := False;
2009 when Rep_Aspect =>
2011 -- If expression has the form of an integer literal, then
2012 -- do not delay, since we know the value cannot change.
2013 -- This optimization catches most rep clause cases.
2015 -- For Boolean aspects, don't delay if no expression
2017 if A_Id in Boolean_Aspects and then No (Expr) then
2018 Delay_Required := False;
2020 -- For non-Boolean aspects, don't delay if integer literal
2022 elsif A_Id not in Boolean_Aspects
2023 and then Present (Expr)
2024 and then Nkind (Expr) = N_Integer_Literal
2025 then
2026 Delay_Required := False;
2028 -- All other cases are delayed
2030 else
2031 Delay_Required := True;
2032 Set_Has_Delayed_Rep_Aspects (E);
2033 end if;
2034 end case;
2036 -- Processing based on specific aspect
2038 case A_Id is
2039 when Aspect_Unimplemented =>
2040 null; -- ??? temp for now
2042 -- No_Aspect should be impossible
2044 when No_Aspect =>
2045 raise Program_Error;
2047 -- Case 1: Aspects corresponding to attribute definition
2048 -- clauses.
2050 when Aspect_Address |
2051 Aspect_Alignment |
2052 Aspect_Bit_Order |
2053 Aspect_Component_Size |
2054 Aspect_Constant_Indexing |
2055 Aspect_Default_Iterator |
2056 Aspect_Dispatching_Domain |
2057 Aspect_External_Tag |
2058 Aspect_Input |
2059 Aspect_Iterable |
2060 Aspect_Iterator_Element |
2061 Aspect_Machine_Radix |
2062 Aspect_Object_Size |
2063 Aspect_Output |
2064 Aspect_Read |
2065 Aspect_Scalar_Storage_Order |
2066 Aspect_Size |
2067 Aspect_Small |
2068 Aspect_Simple_Storage_Pool |
2069 Aspect_Storage_Pool |
2070 Aspect_Stream_Size |
2071 Aspect_Value_Size |
2072 Aspect_Variable_Indexing |
2073 Aspect_Write =>
2075 -- Indexing aspects apply only to tagged type
2077 if (A_Id = Aspect_Constant_Indexing
2078 or else
2079 A_Id = Aspect_Variable_Indexing)
2080 and then not (Is_Type (E)
2081 and then Is_Tagged_Type (E))
2082 then
2083 Error_Msg_N
2084 ("indexing aspect can only apply to a tagged type",
2085 Aspect);
2086 goto Continue;
2087 end if;
2089 -- For the case of aspect Address, we don't consider that we
2090 -- know the entity is never set in the source, since it is
2091 -- is likely aliasing is occurring.
2093 -- Note: one might think that the analysis of the resulting
2094 -- attribute definition clause would take care of that, but
2095 -- that's not the case since it won't be from source.
2097 if A_Id = Aspect_Address then
2098 Set_Never_Set_In_Source (E, False);
2099 end if;
2101 -- Correctness of the profile of a stream operation is
2102 -- verified at the freeze point, but we must detect the
2103 -- illegal specification of this aspect for a subtype now,
2104 -- to prevent malformed rep_item chains.
2106 if A_Id = Aspect_Input or else
2107 A_Id = Aspect_Output or else
2108 A_Id = Aspect_Read or else
2109 A_Id = Aspect_Write
2110 then
2111 if not Is_First_Subtype (E) then
2112 Error_Msg_N
2113 ("local name must be a first subtype", Aspect);
2114 goto Continue;
2116 -- If stream aspect applies to the class-wide type,
2117 -- the generated attribute definition applies to the
2118 -- class-wide type as well.
2120 elsif Class_Present (Aspect) then
2121 Ent :=
2122 Make_Attribute_Reference (Loc,
2123 Prefix => Ent,
2124 Attribute_Name => Name_Class);
2125 end if;
2126 end if;
2128 -- Construct the attribute definition clause
2130 Aitem :=
2131 Make_Attribute_Definition_Clause (Loc,
2132 Name => Ent,
2133 Chars => Chars (Id),
2134 Expression => Relocate_Node (Expr));
2136 -- If the address is specified, then we treat the entity as
2137 -- referenced, to avoid spurious warnings. This is analogous
2138 -- to what is done with an attribute definition clause, but
2139 -- here we don't want to generate a reference because this
2140 -- is the point of definition of the entity.
2142 if A_Id = Aspect_Address then
2143 Set_Referenced (E);
2144 end if;
2146 -- Case 2: Aspects corresponding to pragmas
2148 -- Case 2a: Aspects corresponding to pragmas with two
2149 -- arguments, where the first argument is a local name
2150 -- referring to the entity, and the second argument is the
2151 -- aspect definition expression.
2153 -- Linker_Section/Suppress/Unsuppress
2155 when Aspect_Linker_Section |
2156 Aspect_Suppress |
2157 Aspect_Unsuppress =>
2159 Make_Aitem_Pragma
2160 (Pragma_Argument_Associations => New_List (
2161 Make_Pragma_Argument_Association (Loc,
2162 Expression => New_Occurrence_Of (E, Loc)),
2163 Make_Pragma_Argument_Association (Sloc (Expr),
2164 Expression => Relocate_Node (Expr))),
2165 Pragma_Name => Chars (Id));
2167 -- Synchronization
2169 -- Corresponds to pragma Implemented, construct the pragma
2171 when Aspect_Synchronization =>
2172 Make_Aitem_Pragma
2173 (Pragma_Argument_Associations => New_List (
2174 Make_Pragma_Argument_Association (Loc,
2175 Expression => New_Occurrence_Of (E, Loc)),
2176 Make_Pragma_Argument_Association (Sloc (Expr),
2177 Expression => Relocate_Node (Expr))),
2178 Pragma_Name => Name_Implemented);
2180 -- Attach_Handler
2182 when Aspect_Attach_Handler =>
2183 Make_Aitem_Pragma
2184 (Pragma_Argument_Associations => New_List (
2185 Make_Pragma_Argument_Association (Sloc (Ent),
2186 Expression => Ent),
2187 Make_Pragma_Argument_Association (Sloc (Expr),
2188 Expression => Relocate_Node (Expr))),
2189 Pragma_Name => Name_Attach_Handler);
2191 -- We need to insert this pragma into the tree to get proper
2192 -- processing and to look valid from a placement viewpoint.
2194 Insert_Pragma (Aitem);
2195 goto Continue;
2197 -- Dynamic_Predicate, Predicate, Static_Predicate
2199 when Aspect_Dynamic_Predicate |
2200 Aspect_Predicate |
2201 Aspect_Static_Predicate =>
2203 -- These aspects apply only to subtypes
2205 if not Is_Type (E) then
2206 Error_Msg_N
2207 ("predicate can only be specified for a subtype",
2208 Aspect);
2209 goto Continue;
2211 elsif Is_Incomplete_Type (E) then
2212 Error_Msg_N
2213 ("predicate cannot apply to incomplete view", Aspect);
2214 goto Continue;
2215 end if;
2217 -- Construct the pragma (always a pragma Predicate, with
2218 -- flags recording whether it is static/dynamic). We also
2219 -- set flags recording this in the type itself.
2221 Make_Aitem_Pragma
2222 (Pragma_Argument_Associations => New_List (
2223 Make_Pragma_Argument_Association (Sloc (Ent),
2224 Expression => Ent),
2225 Make_Pragma_Argument_Association (Sloc (Expr),
2226 Expression => Relocate_Node (Expr))),
2227 Pragma_Name => Name_Predicate);
2229 -- Mark type has predicates, and remember what kind of
2230 -- aspect lead to this predicate (we need this to access
2231 -- the right set of check policies later on).
2233 Set_Has_Predicates (E);
2235 if A_Id = Aspect_Dynamic_Predicate then
2236 Set_Has_Dynamic_Predicate_Aspect (E);
2237 elsif A_Id = Aspect_Static_Predicate then
2238 Set_Has_Static_Predicate_Aspect (E);
2239 end if;
2241 -- If the type is private, indicate that its completion
2242 -- has a freeze node, because that is the one that will
2243 -- be visible at freeze time.
2245 if Is_Private_Type (E) and then Present (Full_View (E)) then
2246 Set_Has_Predicates (Full_View (E));
2248 if A_Id = Aspect_Dynamic_Predicate then
2249 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2250 elsif A_Id = Aspect_Static_Predicate then
2251 Set_Has_Static_Predicate_Aspect (Full_View (E));
2252 end if;
2254 Set_Has_Delayed_Aspects (Full_View (E));
2255 Ensure_Freeze_Node (Full_View (E));
2256 end if;
2258 -- Predicate_Failure
2260 when Aspect_Predicate_Failure =>
2262 -- This aspect applies only to subtypes
2264 if not Is_Type (E) then
2265 Error_Msg_N
2266 ("predicate can only be specified for a subtype",
2267 Aspect);
2268 goto Continue;
2270 elsif Is_Incomplete_Type (E) then
2271 Error_Msg_N
2272 ("predicate cannot apply to incomplete view", Aspect);
2273 goto Continue;
2274 end if;
2276 -- Construct the pragma
2278 Make_Aitem_Pragma
2279 (Pragma_Argument_Associations => New_List (
2280 Make_Pragma_Argument_Association (Sloc (Ent),
2281 Expression => Ent),
2282 Make_Pragma_Argument_Association (Sloc (Expr),
2283 Expression => Relocate_Node (Expr))),
2284 Pragma_Name => Name_Predicate_Failure);
2286 Set_Has_Predicates (E);
2288 -- If the type is private, indicate that its completion
2289 -- has a freeze node, because that is the one that will
2290 -- be visible at freeze time.
2292 if Is_Private_Type (E) and then Present (Full_View (E)) then
2293 Set_Has_Predicates (Full_View (E));
2294 Set_Has_Delayed_Aspects (Full_View (E));
2295 Ensure_Freeze_Node (Full_View (E));
2296 end if;
2298 -- Case 2b: Aspects corresponding to pragmas with two
2299 -- arguments, where the second argument is a local name
2300 -- referring to the entity, and the first argument is the
2301 -- aspect definition expression.
2303 -- Convention
2305 when Aspect_Convention =>
2306 Analyze_Aspect_Convention;
2307 goto Continue;
2309 -- External_Name, Link_Name
2311 when Aspect_External_Name |
2312 Aspect_Link_Name =>
2313 Analyze_Aspect_External_Link_Name;
2314 goto Continue;
2316 -- CPU, Interrupt_Priority, Priority
2318 -- These three aspects can be specified for a subprogram spec
2319 -- or body, in which case we analyze the expression and export
2320 -- the value of the aspect.
2322 -- Previously, we generated an equivalent pragma for bodies
2323 -- (note that the specs cannot contain these pragmas). The
2324 -- pragma was inserted ahead of local declarations, rather than
2325 -- after the body. This leads to a certain duplication between
2326 -- the processing performed for the aspect and the pragma, but
2327 -- given the straightforward handling required it is simpler
2328 -- to duplicate than to translate the aspect in the spec into
2329 -- a pragma in the declarative part of the body.
2331 when Aspect_CPU |
2332 Aspect_Interrupt_Priority |
2333 Aspect_Priority =>
2335 if Nkind_In (N, N_Subprogram_Body,
2336 N_Subprogram_Declaration)
2337 then
2338 -- Analyze the aspect expression
2340 Analyze_And_Resolve (Expr, Standard_Integer);
2342 -- Interrupt_Priority aspect not allowed for main
2343 -- subprograms. RM D.1 does not forbid this explicitly,
2344 -- but RM J.15.11(6/3) does not permit pragma
2345 -- Interrupt_Priority for subprograms.
2347 if A_Id = Aspect_Interrupt_Priority then
2348 Error_Msg_N
2349 ("Interrupt_Priority aspect cannot apply to "
2350 & "subprogram", Expr);
2352 -- The expression must be static
2354 elsif not Is_OK_Static_Expression (Expr) then
2355 Flag_Non_Static_Expr
2356 ("aspect requires static expression!", Expr);
2358 -- Check whether this is the main subprogram. Issue a
2359 -- warning only if it is obviously not a main program
2360 -- (when it has parameters or when the subprogram is
2361 -- within a package).
2363 elsif Present (Parameter_Specifications
2364 (Specification (N)))
2365 or else not Is_Compilation_Unit (Defining_Entity (N))
2366 then
2367 -- See RM D.1(14/3) and D.16(12/3)
2369 Error_Msg_N
2370 ("aspect applied to subprogram other than the "
2371 & "main subprogram has no effect??", Expr);
2373 -- Otherwise check in range and export the value
2375 -- For the CPU aspect
2377 elsif A_Id = Aspect_CPU then
2378 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2380 -- Value is correct so we export the value to make
2381 -- it available at execution time.
2383 Set_Main_CPU
2384 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2386 else
2387 Error_Msg_N
2388 ("main subprogram CPU is out of range", Expr);
2389 end if;
2391 -- For the Priority aspect
2393 elsif A_Id = Aspect_Priority then
2394 if Is_In_Range (Expr, RTE (RE_Priority)) then
2396 -- Value is correct so we export the value to make
2397 -- it available at execution time.
2399 Set_Main_Priority
2400 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2402 -- Ignore pragma if Relaxed_RM_Semantics to support
2403 -- other targets/non GNAT compilers.
2405 elsif not Relaxed_RM_Semantics then
2406 Error_Msg_N
2407 ("main subprogram priority is out of range",
2408 Expr);
2409 end if;
2410 end if;
2412 -- Load an arbitrary entity from System.Tasking.Stages
2413 -- or System.Tasking.Restricted.Stages (depending on
2414 -- the supported profile) to make sure that one of these
2415 -- packages is implicitly with'ed, since we need to have
2416 -- the tasking run time active for the pragma Priority to
2417 -- have any effect. Previously we with'ed the package
2418 -- System.Tasking, but this package does not trigger the
2419 -- required initialization of the run-time library.
2421 declare
2422 Discard : Entity_Id;
2423 begin
2424 if Restricted_Profile then
2425 Discard := RTE (RE_Activate_Restricted_Tasks);
2426 else
2427 Discard := RTE (RE_Activate_Tasks);
2428 end if;
2429 end;
2431 -- Handling for these Aspects in subprograms is complete
2433 goto Continue;
2435 -- For tasks pass the aspect as an attribute
2437 else
2438 Aitem :=
2439 Make_Attribute_Definition_Clause (Loc,
2440 Name => Ent,
2441 Chars => Chars (Id),
2442 Expression => Relocate_Node (Expr));
2443 end if;
2445 -- Warnings
2447 when Aspect_Warnings =>
2448 Make_Aitem_Pragma
2449 (Pragma_Argument_Associations => New_List (
2450 Make_Pragma_Argument_Association (Sloc (Expr),
2451 Expression => Relocate_Node (Expr)),
2452 Make_Pragma_Argument_Association (Loc,
2453 Expression => New_Occurrence_Of (E, Loc))),
2454 Pragma_Name => Chars (Id));
2456 Decorate (Aspect, Aitem);
2457 Insert_Pragma (Aitem);
2458 goto Continue;
2460 -- Case 2c: Aspects corresponding to pragmas with three
2461 -- arguments.
2463 -- Invariant aspects have a first argument that references the
2464 -- entity, a second argument that is the expression and a third
2465 -- argument that is an appropriate message.
2467 -- Invariant, Type_Invariant
2469 when Aspect_Invariant |
2470 Aspect_Type_Invariant =>
2472 -- Analysis of the pragma will verify placement legality:
2473 -- an invariant must apply to a private type, or appear in
2474 -- the private part of a spec and apply to a completion.
2476 Make_Aitem_Pragma
2477 (Pragma_Argument_Associations => New_List (
2478 Make_Pragma_Argument_Association (Sloc (Ent),
2479 Expression => Ent),
2480 Make_Pragma_Argument_Association (Sloc (Expr),
2481 Expression => Relocate_Node (Expr))),
2482 Pragma_Name => Name_Invariant);
2484 -- Add message unless exception messages are suppressed
2486 if not Opt.Exception_Locations_Suppressed then
2487 Append_To (Pragma_Argument_Associations (Aitem),
2488 Make_Pragma_Argument_Association (Eloc,
2489 Chars => Name_Message,
2490 Expression =>
2491 Make_String_Literal (Eloc,
2492 Strval => "failed invariant from "
2493 & Build_Location_String (Eloc))));
2494 end if;
2496 -- For Invariant case, insert immediately after the entity
2497 -- declaration. We do not have to worry about delay issues
2498 -- since the pragma processing takes care of this.
2500 Delay_Required := False;
2502 -- Case 2d : Aspects that correspond to a pragma with one
2503 -- argument.
2505 -- Abstract_State
2507 -- Aspect Abstract_State introduces implicit declarations for
2508 -- all state abstraction entities it defines. To emulate this
2509 -- behavior, insert the pragma at the beginning of the visible
2510 -- declarations of the related package so that it is analyzed
2511 -- immediately.
2513 when Aspect_Abstract_State => Abstract_State : declare
2514 Context : Node_Id := N;
2516 begin
2517 -- When aspect Abstract_State appears on a generic package,
2518 -- it is propageted to the package instance. The context in
2519 -- this case is the instance spec.
2521 if Nkind (Context) = N_Package_Instantiation then
2522 Context := Instance_Spec (Context);
2523 end if;
2525 if Nkind_In (Context, N_Generic_Package_Declaration,
2526 N_Package_Declaration)
2527 then
2528 Make_Aitem_Pragma
2529 (Pragma_Argument_Associations => New_List (
2530 Make_Pragma_Argument_Association (Loc,
2531 Expression => Relocate_Node (Expr))),
2532 Pragma_Name => Name_Abstract_State);
2534 Decorate (Aspect, Aitem);
2535 Insert_Pragma
2536 (Prag => Aitem,
2537 Is_Instance =>
2538 Is_Generic_Instance (Defining_Entity (Context)));
2540 else
2541 Error_Msg_NE
2542 ("aspect & must apply to a package declaration",
2543 Aspect, Id);
2544 end if;
2546 goto Continue;
2547 end Abstract_State;
2549 -- Aspect Async_Readers is never delayed because it is
2550 -- equivalent to a source pragma which appears after the
2551 -- related object declaration.
2553 when Aspect_Async_Readers =>
2554 Make_Aitem_Pragma
2555 (Pragma_Argument_Associations => New_List (
2556 Make_Pragma_Argument_Association (Loc,
2557 Expression => Relocate_Node (Expr))),
2558 Pragma_Name => Name_Async_Readers);
2560 Decorate (Aspect, Aitem);
2561 Insert_Pragma (Aitem);
2562 goto Continue;
2564 -- Aspect Async_Writers is never delayed because it is
2565 -- equivalent to a source pragma which appears after the
2566 -- related object declaration.
2568 when Aspect_Async_Writers =>
2569 Make_Aitem_Pragma
2570 (Pragma_Argument_Associations => New_List (
2571 Make_Pragma_Argument_Association (Loc,
2572 Expression => Relocate_Node (Expr))),
2573 Pragma_Name => Name_Async_Writers);
2575 Decorate (Aspect, Aitem);
2576 Insert_Pragma (Aitem);
2577 goto Continue;
2579 -- Aspect Constant_After_Elaboration is never delayed because
2580 -- it is equivalent to a source pragma which appears after the
2581 -- related object declaration.
2583 when Aspect_Constant_After_Elaboration =>
2584 Make_Aitem_Pragma
2585 (Pragma_Argument_Associations => New_List (
2586 Make_Pragma_Argument_Association (Loc,
2587 Expression => Relocate_Node (Expr))),
2588 Pragma_Name =>
2589 Name_Constant_After_Elaboration);
2591 Decorate (Aspect, Aitem);
2592 Insert_Pragma (Aitem);
2593 goto Continue;
2595 -- Aspect Default_Internal_Condition is never delayed because
2596 -- it is equivalent to a source pragma which appears after the
2597 -- related private type. To deal with forward references, the
2598 -- generated pragma is stored in the rep chain of the related
2599 -- private type as types do not carry contracts. The pragma is
2600 -- wrapped inside of a procedure at the freeze point of the
2601 -- private type's full view.
2603 when Aspect_Default_Initial_Condition =>
2604 Make_Aitem_Pragma
2605 (Pragma_Argument_Associations => New_List (
2606 Make_Pragma_Argument_Association (Loc,
2607 Expression => Relocate_Node (Expr))),
2608 Pragma_Name =>
2609 Name_Default_Initial_Condition);
2611 Decorate (Aspect, Aitem);
2612 Insert_Pragma (Aitem);
2613 goto Continue;
2615 -- Default_Storage_Pool
2617 when Aspect_Default_Storage_Pool =>
2618 Make_Aitem_Pragma
2619 (Pragma_Argument_Associations => New_List (
2620 Make_Pragma_Argument_Association (Loc,
2621 Expression => Relocate_Node (Expr))),
2622 Pragma_Name =>
2623 Name_Default_Storage_Pool);
2625 Decorate (Aspect, Aitem);
2626 Insert_Pragma (Aitem);
2627 goto Continue;
2629 -- Depends
2631 -- Aspect Depends is never delayed because it is equivalent to
2632 -- a source pragma which appears after the related subprogram.
2633 -- To deal with forward references, the generated pragma is
2634 -- stored in the contract of the related subprogram and later
2635 -- analyzed at the end of the declarative region. See routine
2636 -- Analyze_Depends_In_Decl_Part for details.
2638 when Aspect_Depends =>
2639 Make_Aitem_Pragma
2640 (Pragma_Argument_Associations => New_List (
2641 Make_Pragma_Argument_Association (Loc,
2642 Expression => Relocate_Node (Expr))),
2643 Pragma_Name => Name_Depends);
2645 Decorate (Aspect, Aitem);
2646 Insert_Pragma (Aitem);
2647 goto Continue;
2649 -- Aspect Effecitve_Reads is never delayed because it is
2650 -- equivalent to a source pragma which appears after the
2651 -- related object declaration.
2653 when Aspect_Effective_Reads =>
2654 Make_Aitem_Pragma
2655 (Pragma_Argument_Associations => New_List (
2656 Make_Pragma_Argument_Association (Loc,
2657 Expression => Relocate_Node (Expr))),
2658 Pragma_Name => Name_Effective_Reads);
2660 Decorate (Aspect, Aitem);
2661 Insert_Pragma (Aitem);
2662 goto Continue;
2664 -- Aspect Effective_Writes is never delayed because it is
2665 -- equivalent to a source pragma which appears after the
2666 -- related object declaration.
2668 when Aspect_Effective_Writes =>
2669 Make_Aitem_Pragma
2670 (Pragma_Argument_Associations => New_List (
2671 Make_Pragma_Argument_Association (Loc,
2672 Expression => Relocate_Node (Expr))),
2673 Pragma_Name => Name_Effective_Writes);
2675 Decorate (Aspect, Aitem);
2676 Insert_Pragma (Aitem);
2677 goto Continue;
2679 -- Aspect Extensions_Visible is never delayed because it is
2680 -- equivalent to a source pragma which appears after the
2681 -- related subprogram.
2683 when Aspect_Extensions_Visible =>
2684 Make_Aitem_Pragma
2685 (Pragma_Argument_Associations => New_List (
2686 Make_Pragma_Argument_Association (Loc,
2687 Expression => Relocate_Node (Expr))),
2688 Pragma_Name => Name_Extensions_Visible);
2690 Decorate (Aspect, Aitem);
2691 Insert_Pragma (Aitem);
2692 goto Continue;
2694 -- Aspect Ghost is never delayed because it is equivalent to a
2695 -- source pragma which appears at the top of [generic] package
2696 -- declarations or after an object, a [generic] subprogram, or
2697 -- a type declaration.
2699 when Aspect_Ghost =>
2700 Make_Aitem_Pragma
2701 (Pragma_Argument_Associations => New_List (
2702 Make_Pragma_Argument_Association (Loc,
2703 Expression => Relocate_Node (Expr))),
2704 Pragma_Name => Name_Ghost);
2706 Decorate (Aspect, Aitem);
2707 Insert_Pragma (Aitem);
2708 goto Continue;
2710 -- Global
2712 -- Aspect Global is never delayed because it is equivalent to
2713 -- a source pragma which appears after the related subprogram.
2714 -- To deal with forward references, the generated pragma is
2715 -- stored in the contract of the related subprogram and later
2716 -- analyzed at the end of the declarative region. See routine
2717 -- Analyze_Global_In_Decl_Part for details.
2719 when Aspect_Global =>
2720 Make_Aitem_Pragma
2721 (Pragma_Argument_Associations => New_List (
2722 Make_Pragma_Argument_Association (Loc,
2723 Expression => Relocate_Node (Expr))),
2724 Pragma_Name => Name_Global);
2726 Decorate (Aspect, Aitem);
2727 Insert_Pragma (Aitem);
2728 goto Continue;
2730 -- Initial_Condition
2732 -- Aspect Initial_Condition is never delayed because it is
2733 -- equivalent to a source pragma which appears after the
2734 -- related package. To deal with forward references, the
2735 -- generated pragma is stored in the contract of the related
2736 -- package and later analyzed at the end of the declarative
2737 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2738 -- for details.
2740 when Aspect_Initial_Condition => Initial_Condition : declare
2741 Context : Node_Id := N;
2743 begin
2744 -- When aspect Initial_Condition appears on a generic
2745 -- package, it is propageted to the package instance. The
2746 -- context in this case is the instance spec.
2748 if Nkind (Context) = N_Package_Instantiation then
2749 Context := Instance_Spec (Context);
2750 end if;
2752 if Nkind_In (Context, N_Generic_Package_Declaration,
2753 N_Package_Declaration)
2754 then
2755 Make_Aitem_Pragma
2756 (Pragma_Argument_Associations => New_List (
2757 Make_Pragma_Argument_Association (Loc,
2758 Expression => Relocate_Node (Expr))),
2759 Pragma_Name =>
2760 Name_Initial_Condition);
2762 Decorate (Aspect, Aitem);
2763 Insert_Pragma
2764 (Prag => Aitem,
2765 Is_Instance =>
2766 Is_Generic_Instance (Defining_Entity (Context)));
2768 -- Otherwise the context is illegal
2770 else
2771 Error_Msg_NE
2772 ("aspect & must apply to a package declaration",
2773 Aspect, Id);
2774 end if;
2776 goto Continue;
2777 end Initial_Condition;
2779 -- Initializes
2781 -- Aspect Initializes is never delayed because it is equivalent
2782 -- to a source pragma appearing after the related package. To
2783 -- deal with forward references, the generated pragma is stored
2784 -- in the contract of the related package and later analyzed at
2785 -- the end of the declarative region. For details, see routine
2786 -- Analyze_Initializes_In_Decl_Part.
2788 when Aspect_Initializes => Initializes : declare
2789 Context : Node_Id := N;
2791 begin
2792 -- When aspect Initializes appears on a generic package,
2793 -- it is propageted to the package instance. The context
2794 -- in this case is the instance spec.
2796 if Nkind (Context) = N_Package_Instantiation then
2797 Context := Instance_Spec (Context);
2798 end if;
2800 if Nkind_In (Context, N_Generic_Package_Declaration,
2801 N_Package_Declaration)
2802 then
2803 Make_Aitem_Pragma
2804 (Pragma_Argument_Associations => New_List (
2805 Make_Pragma_Argument_Association (Loc,
2806 Expression => Relocate_Node (Expr))),
2807 Pragma_Name => Name_Initializes);
2809 Decorate (Aspect, Aitem);
2810 Insert_Pragma
2811 (Prag => Aitem,
2812 Is_Instance =>
2813 Is_Generic_Instance (Defining_Entity (Context)));
2815 -- Otherwise the context is illegal
2817 else
2818 Error_Msg_NE
2819 ("aspect & must apply to a package declaration",
2820 Aspect, Id);
2821 end if;
2823 goto Continue;
2824 end Initializes;
2826 -- Obsolescent
2828 when Aspect_Obsolescent => declare
2829 Args : List_Id;
2831 begin
2832 if No (Expr) then
2833 Args := No_List;
2834 else
2835 Args := New_List (
2836 Make_Pragma_Argument_Association (Sloc (Expr),
2837 Expression => Relocate_Node (Expr)));
2838 end if;
2840 Make_Aitem_Pragma
2841 (Pragma_Argument_Associations => Args,
2842 Pragma_Name => Chars (Id));
2843 end;
2845 -- Part_Of
2847 when Aspect_Part_Of =>
2848 if Nkind_In (N, N_Object_Declaration,
2849 N_Package_Instantiation)
2850 or else Is_Single_Concurrent_Type_Declaration (N)
2851 then
2852 Make_Aitem_Pragma
2853 (Pragma_Argument_Associations => New_List (
2854 Make_Pragma_Argument_Association (Loc,
2855 Expression => Relocate_Node (Expr))),
2856 Pragma_Name => Name_Part_Of);
2858 Decorate (Aspect, Aitem);
2859 Insert_Pragma (Aitem);
2861 else
2862 Error_Msg_NE
2863 ("aspect & must apply to package instantiation, "
2864 & "object, single protected type or single task type",
2865 Aspect, Id);
2866 end if;
2868 goto Continue;
2870 -- SPARK_Mode
2872 when Aspect_SPARK_Mode =>
2873 Make_Aitem_Pragma
2874 (Pragma_Argument_Associations => New_List (
2875 Make_Pragma_Argument_Association (Loc,
2876 Expression => Relocate_Node (Expr))),
2877 Pragma_Name => Name_SPARK_Mode);
2879 Decorate (Aspect, Aitem);
2880 Insert_Pragma (Aitem);
2881 goto Continue;
2883 -- Refined_Depends
2885 -- Aspect Refined_Depends is never delayed because it is
2886 -- equivalent to a source pragma which appears in the
2887 -- declarations of the related subprogram body. To deal with
2888 -- forward references, the generated pragma is stored in the
2889 -- contract of the related subprogram body and later analyzed
2890 -- at the end of the declarative region. For details, see
2891 -- routine Analyze_Refined_Depends_In_Decl_Part.
2893 when Aspect_Refined_Depends =>
2894 Make_Aitem_Pragma
2895 (Pragma_Argument_Associations => New_List (
2896 Make_Pragma_Argument_Association (Loc,
2897 Expression => Relocate_Node (Expr))),
2898 Pragma_Name => Name_Refined_Depends);
2900 Decorate (Aspect, Aitem);
2901 Insert_Pragma (Aitem);
2902 goto Continue;
2904 -- Refined_Global
2906 -- Aspect Refined_Global is never delayed because it is
2907 -- equivalent to a source pragma which appears in the
2908 -- declarations of the related subprogram body. To deal with
2909 -- forward references, the generated pragma is stored in the
2910 -- contract of the related subprogram body and later analyzed
2911 -- at the end of the declarative region. For details, see
2912 -- routine Analyze_Refined_Global_In_Decl_Part.
2914 when Aspect_Refined_Global =>
2915 Make_Aitem_Pragma
2916 (Pragma_Argument_Associations => New_List (
2917 Make_Pragma_Argument_Association (Loc,
2918 Expression => Relocate_Node (Expr))),
2919 Pragma_Name => Name_Refined_Global);
2921 Decorate (Aspect, Aitem);
2922 Insert_Pragma (Aitem);
2923 goto Continue;
2925 -- Refined_Post
2927 when Aspect_Refined_Post =>
2928 Make_Aitem_Pragma
2929 (Pragma_Argument_Associations => New_List (
2930 Make_Pragma_Argument_Association (Loc,
2931 Expression => Relocate_Node (Expr))),
2932 Pragma_Name => Name_Refined_Post);
2934 Decorate (Aspect, Aitem);
2935 Insert_Pragma (Aitem);
2936 goto Continue;
2938 -- Refined_State
2940 when Aspect_Refined_State =>
2942 -- The corresponding pragma for Refined_State is inserted in
2943 -- the declarations of the related package body. This action
2944 -- synchronizes both the source and from-aspect versions of
2945 -- the pragma.
2947 if Nkind (N) = N_Package_Body then
2948 Make_Aitem_Pragma
2949 (Pragma_Argument_Associations => New_List (
2950 Make_Pragma_Argument_Association (Loc,
2951 Expression => Relocate_Node (Expr))),
2952 Pragma_Name => Name_Refined_State);
2954 Decorate (Aspect, Aitem);
2955 Insert_Pragma (Aitem);
2957 -- Otherwise the context is illegal
2959 else
2960 Error_Msg_NE
2961 ("aspect & must apply to a package body", Aspect, Id);
2962 end if;
2964 goto Continue;
2966 -- Relative_Deadline
2968 when Aspect_Relative_Deadline =>
2969 Make_Aitem_Pragma
2970 (Pragma_Argument_Associations => New_List (
2971 Make_Pragma_Argument_Association (Loc,
2972 Expression => Relocate_Node (Expr))),
2973 Pragma_Name => Name_Relative_Deadline);
2975 -- If the aspect applies to a task, the corresponding pragma
2976 -- must appear within its declarations, not after.
2978 if Nkind (N) = N_Task_Type_Declaration then
2979 declare
2980 Def : Node_Id;
2981 V : List_Id;
2983 begin
2984 if No (Task_Definition (N)) then
2985 Set_Task_Definition (N,
2986 Make_Task_Definition (Loc,
2987 Visible_Declarations => New_List,
2988 End_Label => Empty));
2989 end if;
2991 Def := Task_Definition (N);
2992 V := Visible_Declarations (Def);
2993 if not Is_Empty_List (V) then
2994 Insert_Before (First (V), Aitem);
2996 else
2997 Set_Visible_Declarations (Def, New_List (Aitem));
2998 end if;
3000 goto Continue;
3001 end;
3002 end if;
3004 -- Aspect Volatile_Function is never delayed because it is
3005 -- equivalent to a source pragma which appears after the
3006 -- related subprogram.
3008 when Aspect_Volatile_Function =>
3009 Make_Aitem_Pragma
3010 (Pragma_Argument_Associations => New_List (
3011 Make_Pragma_Argument_Association (Loc,
3012 Expression => Relocate_Node (Expr))),
3013 Pragma_Name => Name_Volatile_Function);
3015 Decorate (Aspect, Aitem);
3016 Insert_Pragma (Aitem);
3017 goto Continue;
3019 -- Case 2e: Annotate aspect
3021 when Aspect_Annotate =>
3022 declare
3023 Args : List_Id;
3024 Pargs : List_Id;
3025 Arg : Node_Id;
3027 begin
3028 -- The argument can be a single identifier
3030 if Nkind (Expr) = N_Identifier then
3032 -- One level of parens is allowed
3034 if Paren_Count (Expr) > 1 then
3035 Error_Msg_F ("extra parentheses ignored", Expr);
3036 end if;
3038 Set_Paren_Count (Expr, 0);
3040 -- Add the single item to the list
3042 Args := New_List (Expr);
3044 -- Otherwise we must have an aggregate
3046 elsif Nkind (Expr) = N_Aggregate then
3048 -- Must be positional
3050 if Present (Component_Associations (Expr)) then
3051 Error_Msg_F
3052 ("purely positional aggregate required", Expr);
3053 goto Continue;
3054 end if;
3056 -- Must not be parenthesized
3058 if Paren_Count (Expr) /= 0 then
3059 Error_Msg_F ("extra parentheses ignored", Expr);
3060 end if;
3062 -- List of arguments is list of aggregate expressions
3064 Args := Expressions (Expr);
3066 -- Anything else is illegal
3068 else
3069 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3070 goto Continue;
3071 end if;
3073 -- Prepare pragma arguments
3075 Pargs := New_List;
3076 Arg := First (Args);
3077 while Present (Arg) loop
3078 Append_To (Pargs,
3079 Make_Pragma_Argument_Association (Sloc (Arg),
3080 Expression => Relocate_Node (Arg)));
3081 Next (Arg);
3082 end loop;
3084 Append_To (Pargs,
3085 Make_Pragma_Argument_Association (Sloc (Ent),
3086 Chars => Name_Entity,
3087 Expression => Ent));
3089 Make_Aitem_Pragma
3090 (Pragma_Argument_Associations => Pargs,
3091 Pragma_Name => Name_Annotate);
3092 end;
3094 -- Case 3 : Aspects that don't correspond to pragma/attribute
3095 -- definition clause.
3097 -- Case 3a: The aspects listed below don't correspond to
3098 -- pragmas/attributes but do require delayed analysis.
3100 -- Default_Value can only apply to a scalar type
3102 when Aspect_Default_Value =>
3103 if not Is_Scalar_Type (E) then
3104 Error_Msg_N
3105 ("aspect Default_Value must apply to a scalar type", N);
3106 end if;
3108 Aitem := Empty;
3110 -- Default_Component_Value can only apply to an array type
3111 -- with scalar components.
3113 when Aspect_Default_Component_Value =>
3114 if not (Is_Array_Type (E)
3115 and then Is_Scalar_Type (Component_Type (E)))
3116 then
3117 Error_Msg_N
3118 ("aspect Default_Component_Value can only apply to an "
3119 & "array of scalar components", N);
3120 end if;
3122 Aitem := Empty;
3124 -- Case 3b: The aspects listed below don't correspond to
3125 -- pragmas/attributes and don't need delayed analysis.
3127 -- Implicit_Dereference
3129 -- For Implicit_Dereference, External_Name and Link_Name, only
3130 -- the legality checks are done during the analysis, thus no
3131 -- delay is required.
3133 when Aspect_Implicit_Dereference =>
3134 Analyze_Aspect_Implicit_Dereference;
3135 goto Continue;
3137 -- Dimension
3139 when Aspect_Dimension =>
3140 Analyze_Aspect_Dimension (N, Id, Expr);
3141 goto Continue;
3143 -- Dimension_System
3145 when Aspect_Dimension_System =>
3146 Analyze_Aspect_Dimension_System (N, Id, Expr);
3147 goto Continue;
3149 -- Case 4: Aspects requiring special handling
3151 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3152 -- pragmas take care of the delay.
3154 -- Pre/Post
3156 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3157 -- with a first argument that is the expression, and a second
3158 -- argument that is an informative message if the test fails.
3159 -- This is inserted right after the declaration, to get the
3160 -- required pragma placement. The processing for the pragmas
3161 -- takes care of the required delay.
3163 when Pre_Post_Aspects => Pre_Post : declare
3164 Pname : Name_Id;
3166 begin
3167 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
3168 Pname := Name_Precondition;
3169 else
3170 Pname := Name_Postcondition;
3171 end if;
3173 -- Check that the class-wide predicate cannot be applied to
3174 -- an operation of a synchronized type that is not a tagged
3175 -- type. Other legality checks are performed when analyzing
3176 -- the contract of the operation.
3178 if Class_Present (Aspect)
3179 and then Is_Concurrent_Type (Current_Scope)
3180 and then not Is_Tagged_Type (Current_Scope)
3181 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3182 then
3183 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3184 Error_Msg_N
3185 ("aspect % can only be specified for a primitive "
3186 & "operation of a tagged type", Aspect);
3188 goto Continue;
3189 end if;
3191 -- If the expressions is of the form A and then B, then
3192 -- we generate separate Pre/Post aspects for the separate
3193 -- clauses. Since we allow multiple pragmas, there is no
3194 -- problem in allowing multiple Pre/Post aspects internally.
3195 -- These should be treated in reverse order (B first and
3196 -- A second) since they are later inserted just after N in
3197 -- the order they are treated. This way, the pragma for A
3198 -- ends up preceding the pragma for B, which may have an
3199 -- importance for the error raised (either constraint error
3200 -- or precondition error).
3202 -- We do not do this for Pre'Class, since we have to put
3203 -- these conditions together in a complex OR expression.
3205 -- We do not do this in ASIS mode, as ASIS relies on the
3206 -- original node representing the complete expression, when
3207 -- retrieving it through the source aspect table.
3209 if not ASIS_Mode
3210 and then (Pname = Name_Postcondition
3211 or else not Class_Present (Aspect))
3212 then
3213 while Nkind (Expr) = N_And_Then loop
3214 Insert_After (Aspect,
3215 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
3216 Identifier => Identifier (Aspect),
3217 Expression => Relocate_Node (Left_Opnd (Expr)),
3218 Class_Present => Class_Present (Aspect),
3219 Split_PPC => True));
3220 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
3221 Eloc := Sloc (Expr);
3222 end loop;
3223 end if;
3225 -- Build the precondition/postcondition pragma
3227 -- Add note about why we do NOT need Copy_Tree here???
3229 Make_Aitem_Pragma
3230 (Pragma_Argument_Associations => New_List (
3231 Make_Pragma_Argument_Association (Eloc,
3232 Chars => Name_Check,
3233 Expression => Relocate_Node (Expr))),
3234 Pragma_Name => Pname);
3236 -- Add message unless exception messages are suppressed
3238 if not Opt.Exception_Locations_Suppressed then
3239 Append_To (Pragma_Argument_Associations (Aitem),
3240 Make_Pragma_Argument_Association (Eloc,
3241 Chars => Name_Message,
3242 Expression =>
3243 Make_String_Literal (Eloc,
3244 Strval => "failed "
3245 & Get_Name_String (Pname)
3246 & " from "
3247 & Build_Location_String (Eloc))));
3248 end if;
3250 Set_Is_Delayed_Aspect (Aspect);
3252 -- For Pre/Post cases, insert immediately after the entity
3253 -- declaration, since that is the required pragma placement.
3254 -- Note that for these aspects, we do not have to worry
3255 -- about delay issues, since the pragmas themselves deal
3256 -- with delay of visibility for the expression analysis.
3258 Insert_Pragma (Aitem);
3260 goto Continue;
3261 end Pre_Post;
3263 -- Test_Case
3265 when Aspect_Test_Case => Test_Case : declare
3266 Args : List_Id;
3267 Comp_Expr : Node_Id;
3268 Comp_Assn : Node_Id;
3269 New_Expr : Node_Id;
3271 begin
3272 Args := New_List;
3274 if Nkind (Parent (N)) = N_Compilation_Unit then
3275 Error_Msg_Name_1 := Nam;
3276 Error_Msg_N ("incorrect placement of aspect `%`", E);
3277 goto Continue;
3278 end if;
3280 if Nkind (Expr) /= N_Aggregate then
3281 Error_Msg_Name_1 := Nam;
3282 Error_Msg_NE
3283 ("wrong syntax for aspect `%` for &", Id, E);
3284 goto Continue;
3285 end if;
3287 -- Make pragma expressions refer to the original aspect
3288 -- expressions through the Original_Node link. This is used
3289 -- in semantic analysis for ASIS mode, so that the original
3290 -- expression also gets analyzed.
3292 Comp_Expr := First (Expressions (Expr));
3293 while Present (Comp_Expr) loop
3294 New_Expr := Relocate_Node (Comp_Expr);
3295 Append_To (Args,
3296 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3297 Expression => New_Expr));
3298 Next (Comp_Expr);
3299 end loop;
3301 Comp_Assn := First (Component_Associations (Expr));
3302 while Present (Comp_Assn) loop
3303 if List_Length (Choices (Comp_Assn)) /= 1
3304 or else
3305 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3306 then
3307 Error_Msg_Name_1 := Nam;
3308 Error_Msg_NE
3309 ("wrong syntax for aspect `%` for &", Id, E);
3310 goto Continue;
3311 end if;
3313 Append_To (Args,
3314 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3315 Chars => Chars (First (Choices (Comp_Assn))),
3316 Expression =>
3317 Relocate_Node (Expression (Comp_Assn))));
3318 Next (Comp_Assn);
3319 end loop;
3321 -- Build the test-case pragma
3323 Make_Aitem_Pragma
3324 (Pragma_Argument_Associations => Args,
3325 Pragma_Name => Nam);
3326 end Test_Case;
3328 -- Contract_Cases
3330 when Aspect_Contract_Cases =>
3331 Make_Aitem_Pragma
3332 (Pragma_Argument_Associations => New_List (
3333 Make_Pragma_Argument_Association (Loc,
3334 Expression => Relocate_Node (Expr))),
3335 Pragma_Name => Nam);
3337 Decorate (Aspect, Aitem);
3338 Insert_Pragma (Aitem);
3339 goto Continue;
3341 -- Case 5: Special handling for aspects with an optional
3342 -- boolean argument.
3344 -- In the delayed case, the corresponding pragma cannot be
3345 -- generated yet because the evaluation of the boolean needs
3346 -- to be delayed till the freeze point.
3348 when Boolean_Aspects |
3349 Library_Unit_Aspects =>
3351 Set_Is_Boolean_Aspect (Aspect);
3353 -- Lock_Free aspect only apply to protected objects
3355 if A_Id = Aspect_Lock_Free then
3356 if Ekind (E) /= E_Protected_Type then
3357 Error_Msg_Name_1 := Nam;
3358 Error_Msg_N
3359 ("aspect % only applies to a protected object",
3360 Aspect);
3362 else
3363 -- Set the Uses_Lock_Free flag to True if there is no
3364 -- expression or if the expression is True. The
3365 -- evaluation of this aspect should be delayed to the
3366 -- freeze point (why???)
3368 if No (Expr)
3369 or else Is_True (Static_Boolean (Expr))
3370 then
3371 Set_Uses_Lock_Free (E);
3372 end if;
3374 Record_Rep_Item (E, Aspect);
3375 end if;
3377 goto Continue;
3379 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3380 Analyze_Aspect_Export_Import;
3382 -- Disable_Controlled
3384 elsif A_Id = Aspect_Disable_Controlled then
3385 if Ekind (E) /= E_Record_Type
3386 or else not Is_Controlled (E)
3387 then
3388 Error_Msg_N
3389 ("aspect % requires controlled record type", Aspect);
3390 goto Continue;
3391 end if;
3393 -- If we're in a generic template, we don't want to try
3394 -- to disable controlled types, because typical usage is
3395 -- "Disable_Controlled => not <some_check>'Enabled", and
3396 -- the value of Enabled is not known until we see a
3397 -- particular instance. In such a context, we just need
3398 -- to preanalyze the expression for legality.
3400 if Expander_Active then
3401 Analyze_And_Resolve (Expr, Standard_Boolean);
3403 if not Present (Expr)
3404 or else Is_True (Static_Boolean (Expr))
3405 then
3406 Set_Disable_Controlled (E);
3407 end if;
3409 elsif Serious_Errors_Detected = 0 then
3410 Preanalyze_And_Resolve (Expr, Standard_Boolean);
3411 end if;
3413 goto Continue;
3414 end if;
3416 -- Library unit aspects require special handling in the case
3417 -- of a package declaration, the pragma needs to be inserted
3418 -- in the list of declarations for the associated package.
3419 -- There is no issue of visibility delay for these aspects.
3421 if A_Id in Library_Unit_Aspects
3422 and then
3423 Nkind_In (N, N_Package_Declaration,
3424 N_Generic_Package_Declaration)
3425 and then Nkind (Parent (N)) /= N_Compilation_Unit
3427 -- Aspect is legal on a local instantiation of a library-
3428 -- level generic unit.
3430 and then not Is_Generic_Instance (Defining_Entity (N))
3431 then
3432 Error_Msg_N
3433 ("incorrect context for library unit aspect&", Id);
3434 goto Continue;
3435 end if;
3437 -- Cases where we do not delay, includes all cases where the
3438 -- expression is missing other than the above cases.
3440 if not Delay_Required or else No (Expr) then
3442 -- Exclude aspects Export and Import because their pragma
3443 -- syntax does not map directly to a Boolean aspect.
3445 if A_Id /= Aspect_Export
3446 and then A_Id /= Aspect_Import
3447 then
3448 Make_Aitem_Pragma
3449 (Pragma_Argument_Associations => New_List (
3450 Make_Pragma_Argument_Association (Sloc (Ent),
3451 Expression => Ent)),
3452 Pragma_Name => Chars (Id));
3453 end if;
3455 Delay_Required := False;
3457 -- In general cases, the corresponding pragma/attribute
3458 -- definition clause will be inserted later at the freezing
3459 -- point, and we do not need to build it now.
3461 else
3462 Aitem := Empty;
3463 end if;
3465 -- Storage_Size
3467 -- This is special because for access types we need to generate
3468 -- an attribute definition clause. This also works for single
3469 -- task declarations, but it does not work for task type
3470 -- declarations, because we have the case where the expression
3471 -- references a discriminant of the task type. That can't use
3472 -- an attribute definition clause because we would not have
3473 -- visibility on the discriminant. For that case we must
3474 -- generate a pragma in the task definition.
3476 when Aspect_Storage_Size =>
3478 -- Task type case
3480 if Ekind (E) = E_Task_Type then
3481 declare
3482 Decl : constant Node_Id := Declaration_Node (E);
3484 begin
3485 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3487 -- If no task definition, create one
3489 if No (Task_Definition (Decl)) then
3490 Set_Task_Definition (Decl,
3491 Make_Task_Definition (Loc,
3492 Visible_Declarations => Empty_List,
3493 End_Label => Empty));
3494 end if;
3496 -- Create a pragma and put it at the start of the task
3497 -- definition for the task type declaration.
3499 Make_Aitem_Pragma
3500 (Pragma_Argument_Associations => New_List (
3501 Make_Pragma_Argument_Association (Loc,
3502 Expression => Relocate_Node (Expr))),
3503 Pragma_Name => Name_Storage_Size);
3505 Prepend
3506 (Aitem,
3507 Visible_Declarations (Task_Definition (Decl)));
3508 goto Continue;
3509 end;
3511 -- All other cases, generate attribute definition
3513 else
3514 Aitem :=
3515 Make_Attribute_Definition_Clause (Loc,
3516 Name => Ent,
3517 Chars => Chars (Id),
3518 Expression => Relocate_Node (Expr));
3519 end if;
3520 end case;
3522 -- Attach the corresponding pragma/attribute definition clause to
3523 -- the aspect specification node.
3525 if Present (Aitem) then
3526 Set_From_Aspect_Specification (Aitem);
3527 end if;
3529 -- In the context of a compilation unit, we directly put the
3530 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3531 -- node (no delay is required here) except for aspects on a
3532 -- subprogram body (see below) and a generic package, for which we
3533 -- need to introduce the pragma before building the generic copy
3534 -- (see sem_ch12), and for package instantiations, where the
3535 -- library unit pragmas are better handled early.
3537 if Nkind (Parent (N)) = N_Compilation_Unit
3538 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3539 then
3540 declare
3541 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3543 begin
3544 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3546 -- For a Boolean aspect, create the corresponding pragma if
3547 -- no expression or if the value is True.
3549 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3550 if Is_True (Static_Boolean (Expr)) then
3551 Make_Aitem_Pragma
3552 (Pragma_Argument_Associations => New_List (
3553 Make_Pragma_Argument_Association (Sloc (Ent),
3554 Expression => Ent)),
3555 Pragma_Name => Chars (Id));
3557 Set_From_Aspect_Specification (Aitem, True);
3558 Set_Corresponding_Aspect (Aitem, Aspect);
3560 else
3561 goto Continue;
3562 end if;
3563 end if;
3565 -- If the aspect is on a subprogram body (relevant aspect
3566 -- is Inline), add the pragma in front of the declarations.
3568 if Nkind (N) = N_Subprogram_Body then
3569 if No (Declarations (N)) then
3570 Set_Declarations (N, New_List);
3571 end if;
3573 Prepend (Aitem, Declarations (N));
3575 elsif Nkind (N) = N_Generic_Package_Declaration then
3576 if No (Visible_Declarations (Specification (N))) then
3577 Set_Visible_Declarations (Specification (N), New_List);
3578 end if;
3580 Prepend (Aitem,
3581 Visible_Declarations (Specification (N)));
3583 elsif Nkind (N) = N_Package_Instantiation then
3584 declare
3585 Spec : constant Node_Id :=
3586 Specification (Instance_Spec (N));
3587 begin
3588 if No (Visible_Declarations (Spec)) then
3589 Set_Visible_Declarations (Spec, New_List);
3590 end if;
3592 Prepend (Aitem, Visible_Declarations (Spec));
3593 end;
3595 else
3596 if No (Pragmas_After (Aux)) then
3597 Set_Pragmas_After (Aux, New_List);
3598 end if;
3600 Append (Aitem, Pragmas_After (Aux));
3601 end if;
3603 goto Continue;
3604 end;
3605 end if;
3607 -- The evaluation of the aspect is delayed to the freezing point.
3608 -- The pragma or attribute clause if there is one is then attached
3609 -- to the aspect specification which is put in the rep item list.
3611 if Delay_Required then
3612 if Present (Aitem) then
3613 Set_Is_Delayed_Aspect (Aitem);
3614 Set_Aspect_Rep_Item (Aspect, Aitem);
3615 Set_Parent (Aitem, Aspect);
3616 end if;
3618 Set_Is_Delayed_Aspect (Aspect);
3620 -- In the case of Default_Value, link the aspect to base type
3621 -- as well, even though it appears on a first subtype. This is
3622 -- mandated by the semantics of the aspect. Do not establish
3623 -- the link when processing the base type itself as this leads
3624 -- to a rep item circularity. Verify that we are dealing with
3625 -- a scalar type to prevent cascaded errors.
3627 if A_Id = Aspect_Default_Value
3628 and then Is_Scalar_Type (E)
3629 and then Base_Type (E) /= E
3630 then
3631 Set_Has_Delayed_Aspects (Base_Type (E));
3632 Record_Rep_Item (Base_Type (E), Aspect);
3633 end if;
3635 Set_Has_Delayed_Aspects (E);
3636 Record_Rep_Item (E, Aspect);
3638 -- When delay is not required and the context is a package or a
3639 -- subprogram body, insert the pragma in the body declarations.
3641 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3642 if No (Declarations (N)) then
3643 Set_Declarations (N, New_List);
3644 end if;
3646 -- The pragma is added before source declarations
3648 Prepend_To (Declarations (N), Aitem);
3650 -- When delay is not required and the context is not a compilation
3651 -- unit, we simply insert the pragma/attribute definition clause
3652 -- in sequence.
3654 elsif Present (Aitem) then
3655 Insert_After (Ins_Node, Aitem);
3656 Ins_Node := Aitem;
3657 end if;
3658 end Analyze_One_Aspect;
3660 <<Continue>>
3661 Next (Aspect);
3662 end loop Aspect_Loop;
3664 if Has_Delayed_Aspects (E) then
3665 Ensure_Freeze_Node (E);
3666 end if;
3667 end Analyze_Aspect_Specifications;
3669 ---------------------------------------------------
3670 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3671 ---------------------------------------------------
3673 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3674 Body_Id : constant Entity_Id := Defining_Entity (N);
3676 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3677 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3678 -- error message depending on the aspects involved. Spec_Id denotes the
3679 -- entity of the corresponding spec.
3681 --------------------------------
3682 -- Diagnose_Misplaced_Aspects --
3683 --------------------------------
3685 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3686 procedure Misplaced_Aspect_Error
3687 (Asp : Node_Id;
3688 Ref_Nam : Name_Id);
3689 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3690 -- the name of the refined version of the aspect.
3692 ----------------------------
3693 -- Misplaced_Aspect_Error --
3694 ----------------------------
3696 procedure Misplaced_Aspect_Error
3697 (Asp : Node_Id;
3698 Ref_Nam : Name_Id)
3700 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3701 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3703 begin
3704 -- The corresponding spec already contains the aspect in question
3705 -- and the one appearing on the body must be the refined form:
3707 -- procedure P with Global ...;
3708 -- procedure P with Global ... is ... end P;
3709 -- ^
3710 -- Refined_Global
3712 if Has_Aspect (Spec_Id, Asp_Id) then
3713 Error_Msg_Name_1 := Asp_Nam;
3715 -- Subunits cannot carry aspects that apply to a subprogram
3716 -- declaration.
3718 if Nkind (Parent (N)) = N_Subunit then
3719 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3721 -- Otherwise suggest the refined form
3723 else
3724 Error_Msg_Name_2 := Ref_Nam;
3725 Error_Msg_N ("aspect % should be %", Asp);
3726 end if;
3728 -- Otherwise the aspect must appear on the spec, not on the body
3730 -- procedure P;
3731 -- procedure P with Global ... is ... end P;
3733 else
3734 Error_Msg_N
3735 ("aspect specification must appear on initial declaration",
3736 Asp);
3737 end if;
3738 end Misplaced_Aspect_Error;
3740 -- Local variables
3742 Asp : Node_Id;
3743 Asp_Nam : Name_Id;
3745 -- Start of processing for Diagnose_Misplaced_Aspects
3747 begin
3748 -- Iterate over the aspect specifications and emit specific errors
3749 -- where applicable.
3751 Asp := First (Aspect_Specifications (N));
3752 while Present (Asp) loop
3753 Asp_Nam := Chars (Identifier (Asp));
3755 -- Do not emit errors on aspects that can appear on a subprogram
3756 -- body. This scenario occurs when the aspect specification list
3757 -- contains both misplaced and properly placed aspects.
3759 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3760 null;
3762 -- Special diagnostics for SPARK aspects
3764 elsif Asp_Nam = Name_Depends then
3765 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3767 elsif Asp_Nam = Name_Global then
3768 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3770 elsif Asp_Nam = Name_Post then
3771 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3773 -- Otherwise a language-defined aspect is misplaced
3775 else
3776 Error_Msg_N
3777 ("aspect specification must appear on initial declaration",
3778 Asp);
3779 end if;
3781 Next (Asp);
3782 end loop;
3783 end Diagnose_Misplaced_Aspects;
3785 -- Local variables
3787 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
3789 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3791 begin
3792 -- Language-defined aspects cannot be associated with a subprogram body
3793 -- [stub] if the subprogram has a spec. Certain implementation defined
3794 -- aspects are allowed to break this rule (for all applicable cases, see
3795 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3797 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
3798 Diagnose_Misplaced_Aspects (Spec_Id);
3799 else
3800 Analyze_Aspect_Specifications (N, Body_Id);
3801 end if;
3802 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3804 -----------------------
3805 -- Analyze_At_Clause --
3806 -----------------------
3808 -- An at clause is replaced by the corresponding Address attribute
3809 -- definition clause that is the preferred approach in Ada 95.
3811 procedure Analyze_At_Clause (N : Node_Id) is
3812 CS : constant Boolean := Comes_From_Source (N);
3814 begin
3815 -- This is an obsolescent feature
3817 Check_Restriction (No_Obsolescent_Features, N);
3819 if Warn_On_Obsolescent_Feature then
3820 Error_Msg_N
3821 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3822 Error_Msg_N
3823 ("\?j?use address attribute definition clause instead", N);
3824 end if;
3826 -- Rewrite as address clause
3828 Rewrite (N,
3829 Make_Attribute_Definition_Clause (Sloc (N),
3830 Name => Identifier (N),
3831 Chars => Name_Address,
3832 Expression => Expression (N)));
3834 -- We preserve Comes_From_Source, since logically the clause still comes
3835 -- from the source program even though it is changed in form.
3837 Set_Comes_From_Source (N, CS);
3839 -- Analyze rewritten clause
3841 Analyze_Attribute_Definition_Clause (N);
3842 end Analyze_At_Clause;
3844 -----------------------------------------
3845 -- Analyze_Attribute_Definition_Clause --
3846 -----------------------------------------
3848 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3849 Loc : constant Source_Ptr := Sloc (N);
3850 Nam : constant Node_Id := Name (N);
3851 Attr : constant Name_Id := Chars (N);
3852 Expr : constant Node_Id := Expression (N);
3853 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3855 Ent : Entity_Id;
3856 -- The entity of Nam after it is analyzed. In the case of an incomplete
3857 -- type, this is the underlying type.
3859 U_Ent : Entity_Id;
3860 -- The underlying entity to which the attribute applies. Generally this
3861 -- is the Underlying_Type of Ent, except in the case where the clause
3862 -- applies to the full view of an incomplete or private type, in which
3863 -- case U_Ent is just a copy of Ent.
3865 FOnly : Boolean := False;
3866 -- Reset to True for subtype specific attribute (Alignment, Size)
3867 -- and for stream attributes, i.e. those cases where in the call to
3868 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3869 -- are checked. Note that the case of stream attributes is not clear
3870 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3871 -- Storage_Size for derived task types, but that is also clearly
3872 -- unintentional.
3874 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3875 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3876 -- definition clauses.
3878 function Duplicate_Clause return Boolean;
3879 -- This routine checks if the aspect for U_Ent being given by attribute
3880 -- definition clause N is for an aspect that has already been specified,
3881 -- and if so gives an error message. If there is a duplicate, True is
3882 -- returned, otherwise if there is no error, False is returned.
3884 procedure Check_Indexing_Functions;
3885 -- Check that the function in Constant_Indexing or Variable_Indexing
3886 -- attribute has the proper type structure. If the name is overloaded,
3887 -- check that some interpretation is legal.
3889 procedure Check_Iterator_Functions;
3890 -- Check that there is a single function in Default_Iterator attribute
3891 -- has the proper type structure.
3893 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3894 -- Common legality check for the previous two
3896 -----------------------------------
3897 -- Analyze_Stream_TSS_Definition --
3898 -----------------------------------
3900 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3901 Subp : Entity_Id := Empty;
3902 I : Interp_Index;
3903 It : Interp;
3904 Pnam : Entity_Id;
3906 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3907 -- True for Read attribute, False for other attributes
3909 function Has_Good_Profile
3910 (Subp : Entity_Id;
3911 Report : Boolean := False) return Boolean;
3912 -- Return true if the entity is a subprogram with an appropriate
3913 -- profile for the attribute being defined. If result is False and
3914 -- Report is True, function emits appropriate error.
3916 ----------------------
3917 -- Has_Good_Profile --
3918 ----------------------
3920 function Has_Good_Profile
3921 (Subp : Entity_Id;
3922 Report : Boolean := False) return Boolean
3924 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3925 (False => E_Procedure, True => E_Function);
3926 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3927 F : Entity_Id;
3928 Typ : Entity_Id;
3930 begin
3931 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3932 return False;
3933 end if;
3935 F := First_Formal (Subp);
3937 if No (F)
3938 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3939 or else Designated_Type (Etype (F)) /=
3940 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3941 then
3942 return False;
3943 end if;
3945 if not Is_Function then
3946 Next_Formal (F);
3948 declare
3949 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3950 (False => E_In_Parameter,
3951 True => E_Out_Parameter);
3952 begin
3953 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3954 return False;
3955 end if;
3956 end;
3958 Typ := Etype (F);
3960 -- If the attribute specification comes from an aspect
3961 -- specification for a class-wide stream, the parameter must be
3962 -- a class-wide type of the entity to which the aspect applies.
3964 if From_Aspect_Specification (N)
3965 and then Class_Present (Parent (N))
3966 and then Is_Class_Wide_Type (Typ)
3967 then
3968 Typ := Etype (Typ);
3969 end if;
3971 else
3972 Typ := Etype (Subp);
3973 end if;
3975 -- Verify that the prefix of the attribute and the local name for
3976 -- the type of the formal match, or one is the class-wide of the
3977 -- other, in the case of a class-wide stream operation.
3979 if Base_Type (Typ) = Base_Type (Ent)
3980 or else (Is_Class_Wide_Type (Typ)
3981 and then Typ = Class_Wide_Type (Base_Type (Ent)))
3982 or else (Is_Class_Wide_Type (Ent)
3983 and then Ent = Class_Wide_Type (Base_Type (Typ)))
3984 then
3985 null;
3986 else
3987 return False;
3988 end if;
3990 if Present (Next_Formal (F)) then
3991 return False;
3993 elsif not Is_Scalar_Type (Typ)
3994 and then not Is_First_Subtype (Typ)
3995 and then not Is_Class_Wide_Type (Typ)
3996 then
3997 if Report and not Is_First_Subtype (Typ) then
3998 Error_Msg_N
3999 ("subtype of formal in stream operation must be a first "
4000 & "subtype", Parameter_Type (Parent (F)));
4001 end if;
4003 return False;
4005 else
4006 return True;
4007 end if;
4008 end Has_Good_Profile;
4010 -- Start of processing for Analyze_Stream_TSS_Definition
4012 begin
4013 FOnly := True;
4015 if not Is_Type (U_Ent) then
4016 Error_Msg_N ("local name must be a subtype", Nam);
4017 return;
4019 elsif not Is_First_Subtype (U_Ent) then
4020 Error_Msg_N ("local name must be a first subtype", Nam);
4021 return;
4022 end if;
4024 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
4026 -- If Pnam is present, it can be either inherited from an ancestor
4027 -- type (in which case it is legal to redefine it for this type), or
4028 -- be a previous definition of the attribute for the same type (in
4029 -- which case it is illegal).
4031 -- In the first case, it will have been analyzed already, and we
4032 -- can check that its profile does not match the expected profile
4033 -- for a stream attribute of U_Ent. In the second case, either Pnam
4034 -- has been analyzed (and has the expected profile), or it has not
4035 -- been analyzed yet (case of a type that has not been frozen yet
4036 -- and for which the stream attribute has been set using Set_TSS).
4038 if Present (Pnam)
4039 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4040 then
4041 Error_Msg_Sloc := Sloc (Pnam);
4042 Error_Msg_Name_1 := Attr;
4043 Error_Msg_N ("% attribute already defined #", Nam);
4044 return;
4045 end if;
4047 Analyze (Expr);
4049 if Is_Entity_Name (Expr) then
4050 if not Is_Overloaded (Expr) then
4051 if Has_Good_Profile (Entity (Expr), Report => True) then
4052 Subp := Entity (Expr);
4053 end if;
4055 else
4056 Get_First_Interp (Expr, I, It);
4057 while Present (It.Nam) loop
4058 if Has_Good_Profile (It.Nam) then
4059 Subp := It.Nam;
4060 exit;
4061 end if;
4063 Get_Next_Interp (I, It);
4064 end loop;
4065 end if;
4066 end if;
4068 if Present (Subp) then
4069 if Is_Abstract_Subprogram (Subp) then
4070 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4071 return;
4073 -- A stream subprogram for an interface type must be a null
4074 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4075 -- of an interface is not an interface type (3.9.4 (6.b/2)).
4077 elsif Is_Interface (U_Ent)
4078 and then not Is_Class_Wide_Type (U_Ent)
4079 and then not Inside_A_Generic
4080 and then
4081 (Ekind (Subp) = E_Function
4082 or else
4083 not Null_Present
4084 (Specification
4085 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
4086 then
4087 Error_Msg_N
4088 ("stream subprogram for interface type must be null "
4089 & "procedure", Expr);
4090 end if;
4092 Set_Entity (Expr, Subp);
4093 Set_Etype (Expr, Etype (Subp));
4095 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
4097 else
4098 Error_Msg_Name_1 := Attr;
4099 Error_Msg_N ("incorrect expression for% attribute", Expr);
4100 end if;
4101 end Analyze_Stream_TSS_Definition;
4103 ------------------------------
4104 -- Check_Indexing_Functions --
4105 ------------------------------
4107 procedure Check_Indexing_Functions is
4108 Indexing_Found : Boolean := False;
4110 procedure Check_Inherited_Indexing;
4111 -- For a derived type, check that no indexing aspect is specified
4112 -- for the type if it is also inherited
4114 procedure Check_One_Function (Subp : Entity_Id);
4115 -- Check one possible interpretation. Sets Indexing_Found True if a
4116 -- legal indexing function is found.
4118 procedure Illegal_Indexing (Msg : String);
4119 -- Diagnose illegal indexing function if not overloaded. In the
4120 -- overloaded case indicate that no legal interpretation exists.
4122 ------------------------------
4123 -- Check_Inherited_Indexing --
4124 ------------------------------
4126 procedure Check_Inherited_Indexing is
4127 Inherited : Node_Id;
4129 begin
4130 if Attr = Name_Constant_Indexing then
4131 Inherited :=
4132 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4133 else pragma Assert (Attr = Name_Variable_Indexing);
4134 Inherited :=
4135 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4136 end if;
4138 if Present (Inherited) then
4139 if Debug_Flag_Dot_XX then
4140 null;
4142 -- OK if current attribute_definition_clause is expansion of
4143 -- inherited aspect.
4145 elsif Aspect_Rep_Item (Inherited) = N then
4146 null;
4148 -- Indicate the operation that must be overridden, rather than
4149 -- redefining the indexing aspect.
4151 else
4152 Illegal_Indexing
4153 ("indexing function already inherited from parent type");
4154 Error_Msg_NE
4155 ("!override & instead",
4156 N, Entity (Expression (Inherited)));
4157 end if;
4158 end if;
4159 end Check_Inherited_Indexing;
4161 ------------------------
4162 -- Check_One_Function --
4163 ------------------------
4165 procedure Check_One_Function (Subp : Entity_Id) is
4166 Default_Element : Node_Id;
4167 Ret_Type : constant Entity_Id := Etype (Subp);
4169 begin
4170 if not Is_Overloadable (Subp) then
4171 Illegal_Indexing ("illegal indexing function for type&");
4172 return;
4174 elsif Scope (Subp) /= Scope (Ent) then
4175 if Nkind (Expr) = N_Expanded_Name then
4177 -- Indexing function can't be declared elsewhere
4179 Illegal_Indexing
4180 ("indexing function must be declared in scope of type&");
4181 end if;
4183 return;
4185 elsif No (First_Formal (Subp)) then
4186 Illegal_Indexing
4187 ("Indexing requires a function that applies to type&");
4188 return;
4190 elsif No (Next_Formal (First_Formal (Subp))) then
4191 Illegal_Indexing
4192 ("indexing function must have at least two parameters");
4193 return;
4195 elsif Is_Derived_Type (Ent) then
4196 Check_Inherited_Indexing;
4197 end if;
4199 if not Check_Primitive_Function (Subp) then
4200 Illegal_Indexing
4201 ("Indexing aspect requires a function that applies to type&");
4202 return;
4203 end if;
4205 -- If partial declaration exists, verify that it is not tagged.
4207 if Ekind (Current_Scope) = E_Package
4208 and then Has_Private_Declaration (Ent)
4209 and then From_Aspect_Specification (N)
4210 and then
4211 List_Containing (Parent (Ent)) =
4212 Private_Declarations
4213 (Specification (Unit_Declaration_Node (Current_Scope)))
4214 and then Nkind (N) = N_Attribute_Definition_Clause
4215 then
4216 declare
4217 Decl : Node_Id;
4219 begin
4220 Decl :=
4221 First (Visible_Declarations
4222 (Specification
4223 (Unit_Declaration_Node (Current_Scope))));
4225 while Present (Decl) loop
4226 if Nkind (Decl) = N_Private_Type_Declaration
4227 and then Ent = Full_View (Defining_Identifier (Decl))
4228 and then Tagged_Present (Decl)
4229 and then No (Aspect_Specifications (Decl))
4230 then
4231 Illegal_Indexing
4232 ("Indexing aspect cannot be specified on full view "
4233 & "if partial view is tagged");
4234 return;
4235 end if;
4237 Next (Decl);
4238 end loop;
4239 end;
4240 end if;
4242 -- An indexing function must return either the default element of
4243 -- the container, or a reference type. For variable indexing it
4244 -- must be the latter.
4246 Default_Element :=
4247 Find_Value_Of_Aspect
4248 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4250 if Present (Default_Element) then
4251 Analyze (Default_Element);
4253 if Is_Entity_Name (Default_Element)
4254 and then not Covers (Entity (Default_Element), Ret_Type)
4255 and then False
4256 then
4257 Illegal_Indexing
4258 ("wrong return type for indexing function");
4259 return;
4260 end if;
4261 end if;
4263 -- For variable_indexing the return type must be a reference type
4265 if Attr = Name_Variable_Indexing then
4266 if not Has_Implicit_Dereference (Ret_Type) then
4267 Illegal_Indexing
4268 ("variable indexing must return a reference type");
4269 return;
4271 elsif Is_Access_Constant
4272 (Etype (First_Discriminant (Ret_Type)))
4273 then
4274 Illegal_Indexing
4275 ("variable indexing must return an access to variable");
4276 return;
4277 end if;
4279 else
4280 if Has_Implicit_Dereference (Ret_Type)
4281 and then not
4282 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4283 then
4284 Illegal_Indexing
4285 ("constant indexing must return an access to constant");
4286 return;
4288 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4289 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4290 then
4291 Illegal_Indexing
4292 ("constant indexing must apply to an access to constant");
4293 return;
4294 end if;
4295 end if;
4297 -- All checks succeeded.
4299 Indexing_Found := True;
4300 end Check_One_Function;
4302 -----------------------
4303 -- Illegal_Indexing --
4304 -----------------------
4306 procedure Illegal_Indexing (Msg : String) is
4307 begin
4308 Error_Msg_NE (Msg, N, Ent);
4309 end Illegal_Indexing;
4311 -- Start of processing for Check_Indexing_Functions
4313 begin
4314 if In_Instance then
4315 Check_Inherited_Indexing;
4316 end if;
4318 Analyze (Expr);
4320 if not Is_Overloaded (Expr) then
4321 Check_One_Function (Entity (Expr));
4323 else
4324 declare
4325 I : Interp_Index;
4326 It : Interp;
4328 begin
4329 Indexing_Found := False;
4330 Get_First_Interp (Expr, I, It);
4331 while Present (It.Nam) loop
4333 -- Note that analysis will have added the interpretation
4334 -- that corresponds to the dereference. We only check the
4335 -- subprogram itself.
4337 if Is_Overloadable (It.Nam) then
4338 Check_One_Function (It.Nam);
4339 end if;
4341 Get_Next_Interp (I, It);
4342 end loop;
4343 end;
4344 end if;
4346 if not Indexing_Found and then not Error_Posted (N) then
4347 Error_Msg_NE
4348 ("aspect Indexing requires a local function that "
4349 & "applies to type&", Expr, Ent);
4350 end if;
4351 end Check_Indexing_Functions;
4353 ------------------------------
4354 -- Check_Iterator_Functions --
4355 ------------------------------
4357 procedure Check_Iterator_Functions is
4358 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4359 -- Check one possible interpretation for validity
4361 ----------------------------
4362 -- Valid_Default_Iterator --
4363 ----------------------------
4365 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4366 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
4367 Formal : Entity_Id;
4369 begin
4370 if not Check_Primitive_Function (Subp) then
4371 return False;
4373 -- The return type must be derived from a type in an instance
4374 -- of Iterator.Interfaces, and thus its root type must have a
4375 -- predefined name.
4377 elsif Chars (Root_T) /= Name_Forward_Iterator
4378 and then Chars (Root_T) /= Name_Reversible_Iterator
4379 then
4380 return False;
4382 else
4383 Formal := First_Formal (Subp);
4384 end if;
4386 -- False if any subsequent formal has no default expression
4388 Formal := Next_Formal (Formal);
4389 while Present (Formal) loop
4390 if No (Expression (Parent (Formal))) then
4391 return False;
4392 end if;
4394 Next_Formal (Formal);
4395 end loop;
4397 -- True if all subsequent formals have default expressions
4399 return True;
4400 end Valid_Default_Iterator;
4402 -- Start of processing for Check_Iterator_Functions
4404 begin
4405 Analyze (Expr);
4407 if not Is_Entity_Name (Expr) then
4408 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4409 end if;
4411 if not Is_Overloaded (Expr) then
4412 if not Check_Primitive_Function (Entity (Expr)) then
4413 Error_Msg_NE
4414 ("aspect Indexing requires a function that applies to type&",
4415 Entity (Expr), Ent);
4416 end if;
4418 -- Flag the default_iterator as well as the denoted function.
4420 if not Valid_Default_Iterator (Entity (Expr)) then
4421 Error_Msg_N ("improper function for default iterator!", Expr);
4422 end if;
4424 else
4425 declare
4426 Default : Entity_Id := Empty;
4427 I : Interp_Index;
4428 It : Interp;
4430 begin
4431 Get_First_Interp (Expr, I, It);
4432 while Present (It.Nam) loop
4433 if not Check_Primitive_Function (It.Nam)
4434 or else not Valid_Default_Iterator (It.Nam)
4435 then
4436 Remove_Interp (I);
4438 elsif Present (Default) then
4440 -- An explicit one should override an implicit one
4442 if Comes_From_Source (Default) =
4443 Comes_From_Source (It.Nam)
4444 then
4445 Error_Msg_N ("default iterator must be unique", Expr);
4446 Error_Msg_Sloc := Sloc (Default);
4447 Error_Msg_N ("\\possible interpretation#", Expr);
4448 Error_Msg_Sloc := Sloc (It.Nam);
4449 Error_Msg_N ("\\possible interpretation#", Expr);
4451 elsif Comes_From_Source (It.Nam) then
4452 Default := It.Nam;
4453 end if;
4454 else
4455 Default := It.Nam;
4456 end if;
4458 Get_Next_Interp (I, It);
4459 end loop;
4461 if Present (Default) then
4462 Set_Entity (Expr, Default);
4463 Set_Is_Overloaded (Expr, False);
4464 else
4465 Error_Msg_N
4466 ("no interpretation is a valid default iterator!", Expr);
4467 end if;
4468 end;
4469 end if;
4470 end Check_Iterator_Functions;
4472 -------------------------------
4473 -- Check_Primitive_Function --
4474 -------------------------------
4476 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4477 Ctrl : Entity_Id;
4479 begin
4480 if Ekind (Subp) /= E_Function then
4481 return False;
4482 end if;
4484 if No (First_Formal (Subp)) then
4485 return False;
4486 else
4487 Ctrl := Etype (First_Formal (Subp));
4488 end if;
4490 -- To be a primitive operation subprogram has to be in same scope.
4492 if Scope (Ctrl) /= Scope (Subp) then
4493 return False;
4494 end if;
4496 -- Type of formal may be the class-wide type, an access to such,
4497 -- or an incomplete view.
4499 if Ctrl = Ent
4500 or else Ctrl = Class_Wide_Type (Ent)
4501 or else
4502 (Ekind (Ctrl) = E_Anonymous_Access_Type
4503 and then (Designated_Type (Ctrl) = Ent
4504 or else
4505 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4506 or else
4507 (Ekind (Ctrl) = E_Incomplete_Type
4508 and then Full_View (Ctrl) = Ent)
4509 then
4510 null;
4511 else
4512 return False;
4513 end if;
4515 return True;
4516 end Check_Primitive_Function;
4518 ----------------------
4519 -- Duplicate_Clause --
4520 ----------------------
4522 function Duplicate_Clause return Boolean is
4523 A : Node_Id;
4525 begin
4526 -- Nothing to do if this attribute definition clause comes from
4527 -- an aspect specification, since we could not be duplicating an
4528 -- explicit clause, and we dealt with the case of duplicated aspects
4529 -- in Analyze_Aspect_Specifications.
4531 if From_Aspect_Specification (N) then
4532 return False;
4533 end if;
4535 -- Otherwise current clause may duplicate previous clause, or a
4536 -- previously given pragma or aspect specification for the same
4537 -- aspect.
4539 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4541 if Present (A) then
4542 Error_Msg_Name_1 := Chars (N);
4543 Error_Msg_Sloc := Sloc (A);
4545 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4546 return True;
4547 end if;
4549 return False;
4550 end Duplicate_Clause;
4552 -- Start of processing for Analyze_Attribute_Definition_Clause
4554 begin
4555 -- The following code is a defense against recursion. Not clear that
4556 -- this can happen legitimately, but perhaps some error situations can
4557 -- cause it, and we did see this recursion during testing.
4559 if Analyzed (N) then
4560 return;
4561 else
4562 Set_Analyzed (N, True);
4563 end if;
4565 Check_Restriction_No_Use_Of_Attribute (N);
4567 -- Ignore some selected attributes in CodePeer mode since they are not
4568 -- relevant in this context.
4570 if CodePeer_Mode then
4571 case Id is
4573 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4574 -- internal representation of types by implicitly packing them.
4576 when Attribute_Component_Size =>
4577 Rewrite (N, Make_Null_Statement (Sloc (N)));
4578 return;
4580 when others =>
4581 null;
4582 end case;
4583 end if;
4585 -- Process Ignore_Rep_Clauses option
4587 if Ignore_Rep_Clauses then
4588 case Id is
4590 -- The following should be ignored. They do not affect legality
4591 -- and may be target dependent. The basic idea of -gnatI is to
4592 -- ignore any rep clauses that may be target dependent but do not
4593 -- affect legality (except possibly to be rejected because they
4594 -- are incompatible with the compilation target).
4596 when Attribute_Alignment |
4597 Attribute_Bit_Order |
4598 Attribute_Component_Size |
4599 Attribute_Machine_Radix |
4600 Attribute_Object_Size |
4601 Attribute_Size |
4602 Attribute_Small |
4603 Attribute_Stream_Size |
4604 Attribute_Value_Size =>
4605 Kill_Rep_Clause (N);
4606 return;
4608 -- The following should not be ignored, because in the first place
4609 -- they are reasonably portable, and should not cause problems
4610 -- in compiling code from another target, and also they do affect
4611 -- legality, e.g. failing to provide a stream attribute for a type
4612 -- may make a program illegal.
4614 when Attribute_External_Tag |
4615 Attribute_Input |
4616 Attribute_Output |
4617 Attribute_Read |
4618 Attribute_Simple_Storage_Pool |
4619 Attribute_Storage_Pool |
4620 Attribute_Storage_Size |
4621 Attribute_Write =>
4622 null;
4624 -- We do not do anything here with address clauses, they will be
4625 -- removed by Freeze later on, but for now, it works better to
4626 -- keep then in the tree.
4628 when Attribute_Address =>
4629 null;
4631 -- Other cases are errors ("attribute& cannot be set with
4632 -- definition clause"), which will be caught below.
4634 when others =>
4635 null;
4636 end case;
4637 end if;
4639 Analyze (Nam);
4640 Ent := Entity (Nam);
4642 if Rep_Item_Too_Early (Ent, N) then
4643 return;
4644 end if;
4646 -- Rep clause applies to full view of incomplete type or private type if
4647 -- we have one (if not, this is a premature use of the type). However,
4648 -- certain semantic checks need to be done on the specified entity (i.e.
4649 -- the private view), so we save it in Ent.
4651 if Is_Private_Type (Ent)
4652 and then Is_Derived_Type (Ent)
4653 and then not Is_Tagged_Type (Ent)
4654 and then No (Full_View (Ent))
4655 then
4656 -- If this is a private type whose completion is a derivation from
4657 -- another private type, there is no full view, and the attribute
4658 -- belongs to the type itself, not its underlying parent.
4660 U_Ent := Ent;
4662 elsif Ekind (Ent) = E_Incomplete_Type then
4664 -- The attribute applies to the full view, set the entity of the
4665 -- attribute definition accordingly.
4667 Ent := Underlying_Type (Ent);
4668 U_Ent := Ent;
4669 Set_Entity (Nam, Ent);
4671 else
4672 U_Ent := Underlying_Type (Ent);
4673 end if;
4675 -- Avoid cascaded error
4677 if Etype (Nam) = Any_Type then
4678 return;
4680 -- Must be declared in current scope or in case of an aspect
4681 -- specification, must be visible in current scope.
4683 elsif Scope (Ent) /= Current_Scope
4684 and then
4685 not (From_Aspect_Specification (N)
4686 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4687 then
4688 Error_Msg_N ("entity must be declared in this scope", Nam);
4689 return;
4691 -- Must not be a source renaming (we do have some cases where the
4692 -- expander generates a renaming, and those cases are OK, in such
4693 -- cases any attribute applies to the renamed object as well).
4695 elsif Is_Object (Ent)
4696 and then Present (Renamed_Object (Ent))
4697 then
4698 -- Case of renamed object from source, this is an error
4700 if Comes_From_Source (Renamed_Object (Ent)) then
4701 Get_Name_String (Chars (N));
4702 Error_Msg_Strlen := Name_Len;
4703 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4704 Error_Msg_N
4705 ("~ clause not allowed for a renaming declaration "
4706 & "(RM 13.1(6))", Nam);
4707 return;
4709 -- For the case of a compiler generated renaming, the attribute
4710 -- definition clause applies to the renamed object created by the
4711 -- expander. The easiest general way to handle this is to create a
4712 -- copy of the attribute definition clause for this object.
4714 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4715 Insert_Action (N,
4716 Make_Attribute_Definition_Clause (Loc,
4717 Name =>
4718 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4719 Chars => Chars (N),
4720 Expression => Duplicate_Subexpr (Expression (N))));
4722 -- If the renamed object is not an entity, it must be a dereference
4723 -- of an unconstrained function call, and we must introduce a new
4724 -- declaration to capture the expression. This is needed in the case
4725 -- of 'Alignment, where the original declaration must be rewritten.
4727 else
4728 pragma Assert
4729 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4730 null;
4731 end if;
4733 -- If no underlying entity, use entity itself, applies to some
4734 -- previously detected error cases ???
4736 elsif No (U_Ent) then
4737 U_Ent := Ent;
4739 -- Cannot specify for a subtype (exception Object/Value_Size)
4741 elsif Is_Type (U_Ent)
4742 and then not Is_First_Subtype (U_Ent)
4743 and then Id /= Attribute_Object_Size
4744 and then Id /= Attribute_Value_Size
4745 and then not From_At_Mod (N)
4746 then
4747 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4748 return;
4749 end if;
4751 Set_Entity (N, U_Ent);
4753 -- Switch on particular attribute
4755 case Id is
4757 -------------
4758 -- Address --
4759 -------------
4761 -- Address attribute definition clause
4763 when Attribute_Address => Address : begin
4765 -- A little error check, catch for X'Address use X'Address;
4767 if Nkind (Nam) = N_Identifier
4768 and then Nkind (Expr) = N_Attribute_Reference
4769 and then Attribute_Name (Expr) = Name_Address
4770 and then Nkind (Prefix (Expr)) = N_Identifier
4771 and then Chars (Nam) = Chars (Prefix (Expr))
4772 then
4773 Error_Msg_NE
4774 ("address for & is self-referencing", Prefix (Expr), Ent);
4775 return;
4776 end if;
4778 -- Not that special case, carry on with analysis of expression
4780 Analyze_And_Resolve (Expr, RTE (RE_Address));
4782 -- Even when ignoring rep clauses we need to indicate that the
4783 -- entity has an address clause and thus it is legal to declare
4784 -- it imported. Freeze will get rid of the address clause later.
4786 if Ignore_Rep_Clauses then
4787 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4788 Record_Rep_Item (U_Ent, N);
4789 end if;
4791 return;
4792 end if;
4794 if Duplicate_Clause then
4795 null;
4797 -- Case of address clause for subprogram
4799 elsif Is_Subprogram (U_Ent) then
4800 if Has_Homonym (U_Ent) then
4801 Error_Msg_N
4802 ("address clause cannot be given for overloaded "
4803 & "subprogram", Nam);
4804 return;
4805 end if;
4807 -- For subprograms, all address clauses are permitted, and we
4808 -- mark the subprogram as having a deferred freeze so that Gigi
4809 -- will not elaborate it too soon.
4811 -- Above needs more comments, what is too soon about???
4813 Set_Has_Delayed_Freeze (U_Ent);
4815 -- Case of address clause for entry
4817 elsif Ekind (U_Ent) = E_Entry then
4818 if Nkind (Parent (N)) = N_Task_Body then
4819 Error_Msg_N
4820 ("entry address must be specified in task spec", Nam);
4821 return;
4822 end if;
4824 -- For entries, we require a constant address
4826 Check_Constant_Address_Clause (Expr, U_Ent);
4828 -- Special checks for task types
4830 if Is_Task_Type (Scope (U_Ent))
4831 and then Comes_From_Source (Scope (U_Ent))
4832 then
4833 Error_Msg_N
4834 ("??entry address declared for entry in task type", N);
4835 Error_Msg_N
4836 ("\??only one task can be declared of this type", N);
4837 end if;
4839 -- Entry address clauses are obsolescent
4841 Check_Restriction (No_Obsolescent_Features, N);
4843 if Warn_On_Obsolescent_Feature then
4844 Error_Msg_N
4845 ("?j?attaching interrupt to task entry is an obsolescent "
4846 & "feature (RM J.7.1)", N);
4847 Error_Msg_N
4848 ("\?j?use interrupt procedure instead", N);
4849 end if;
4851 -- Case of an address clause for a controlled object which we
4852 -- consider to be erroneous.
4854 elsif Is_Controlled (Etype (U_Ent))
4855 or else Has_Controlled_Component (Etype (U_Ent))
4856 then
4857 Error_Msg_NE
4858 ("??controlled object& must not be overlaid", Nam, U_Ent);
4859 Error_Msg_N
4860 ("\??Program_Error will be raised at run time", Nam);
4861 Insert_Action (Declaration_Node (U_Ent),
4862 Make_Raise_Program_Error (Loc,
4863 Reason => PE_Overlaid_Controlled_Object));
4864 return;
4866 -- Case of address clause for a (non-controlled) object
4868 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4869 declare
4870 Expr : constant Node_Id := Expression (N);
4871 O_Ent : Entity_Id;
4872 Off : Boolean;
4874 begin
4875 -- Exported variables cannot have an address clause, because
4876 -- this cancels the effect of the pragma Export.
4878 if Is_Exported (U_Ent) then
4879 Error_Msg_N
4880 ("cannot export object with address clause", Nam);
4881 return;
4882 end if;
4884 Find_Overlaid_Entity (N, O_Ent, Off);
4886 if Present (O_Ent) then
4888 -- If the object overlays a constant object, mark it so
4890 if Is_Constant_Object (O_Ent) then
4891 Set_Overlays_Constant (U_Ent);
4892 end if;
4894 -- If the address clause is of the form:
4896 -- for X'Address use Y'Address;
4898 -- or
4900 -- C : constant Address := Y'Address;
4901 -- ...
4902 -- for X'Address use C;
4904 -- then we make an entry in the table to check the size
4905 -- and alignment of the overlaying variable. But we defer
4906 -- this check till after code generation to take full
4907 -- advantage of the annotation done by the back end.
4909 -- If the entity has a generic type, the check will be
4910 -- performed in the instance if the actual type justifies
4911 -- it, and we do not insert the clause in the table to
4912 -- prevent spurious warnings.
4914 -- Note: we used to test Comes_From_Source and only give
4915 -- this warning for source entities, but we have removed
4916 -- this test. It really seems bogus to generate overlays
4917 -- that would trigger this warning in generated code.
4918 -- Furthermore, by removing the test, we handle the
4919 -- aspect case properly.
4921 if Is_Object (O_Ent)
4922 and then not Is_Generic_Type (Etype (U_Ent))
4923 and then Address_Clause_Overlay_Warnings
4924 then
4925 Address_Clause_Checks.Append
4926 ((N, U_Ent, No_Uint, O_Ent, Off));
4927 end if;
4928 else
4929 -- If this is not an overlay, mark a variable as being
4930 -- volatile to prevent unwanted optimizations. It's a
4931 -- conservative interpretation of RM 13.3(19) for the
4932 -- cases where the compiler cannot detect potential
4933 -- aliasing issues easily and it also covers the case
4934 -- of an absolute address where the volatile aspect is
4935 -- kind of implicit.
4937 if Ekind (U_Ent) = E_Variable then
4938 Set_Treat_As_Volatile (U_Ent);
4939 end if;
4941 -- Make an entry in the table for an absolute address as
4942 -- above to check that the value is compatible with the
4943 -- alignment of the object.
4945 declare
4946 Addr : constant Node_Id := Address_Value (Expr);
4947 begin
4948 if Compile_Time_Known_Value (Addr)
4949 and then Address_Clause_Overlay_Warnings
4950 then
4951 Address_Clause_Checks.Append
4952 ((N, U_Ent, Expr_Value (Addr), Empty, False));
4953 end if;
4954 end;
4955 end if;
4957 -- Overlaying controlled objects is erroneous. Emit warning
4958 -- but continue analysis because program is itself legal,
4959 -- and back end must see address clause.
4961 if Present (O_Ent)
4962 and then (Has_Controlled_Component (Etype (O_Ent))
4963 or else Is_Controlled (Etype (O_Ent)))
4964 and then not Inside_A_Generic
4965 then
4966 Error_Msg_N
4967 ("??cannot use overlays with controlled objects", Expr);
4968 Error_Msg_N
4969 ("\??Program_Error will be raised at run time", Expr);
4970 Insert_Action (Declaration_Node (U_Ent),
4971 Make_Raise_Program_Error (Loc,
4972 Reason => PE_Overlaid_Controlled_Object));
4974 -- Issue an unconditional warning for a constant overlaying
4975 -- a variable. For the reverse case, we will issue it only
4976 -- if the variable is modified.
4978 elsif Ekind (U_Ent) = E_Constant
4979 and then Present (O_Ent)
4980 and then not Overlays_Constant (U_Ent)
4981 and then Address_Clause_Overlay_Warnings
4982 then
4983 Error_Msg_N ("??constant overlays a variable", Expr);
4985 -- Imported variables can have an address clause, but then
4986 -- the import is pretty meaningless except to suppress
4987 -- initializations, so we do not need such variables to
4988 -- be statically allocated (and in fact it causes trouble
4989 -- if the address clause is a local value).
4991 elsif Is_Imported (U_Ent) then
4992 Set_Is_Statically_Allocated (U_Ent, False);
4993 end if;
4995 -- We mark a possible modification of a variable with an
4996 -- address clause, since it is likely aliasing is occurring.
4998 Note_Possible_Modification (Nam, Sure => False);
5000 -- Legality checks on the address clause for initialized
5001 -- objects is deferred until the freeze point, because
5002 -- a subsequent pragma might indicate that the object
5003 -- is imported and thus not initialized. Also, the address
5004 -- clause might involve entities that have yet to be
5005 -- elaborated.
5007 Set_Has_Delayed_Freeze (U_Ent);
5009 -- If an initialization call has been generated for this
5010 -- object, it needs to be deferred to after the freeze node
5011 -- we have just now added, otherwise GIGI will see a
5012 -- reference to the variable (as actual to the IP call)
5013 -- before its definition.
5015 declare
5016 Init_Call : constant Node_Id :=
5017 Remove_Init_Call (U_Ent, N);
5019 begin
5020 if Present (Init_Call) then
5021 Append_Freeze_Action (U_Ent, Init_Call);
5023 -- Reset Initialization_Statements pointer so that
5024 -- if there is a pragma Import further down, it can
5025 -- clear any default initialization.
5027 Set_Initialization_Statements (U_Ent, Init_Call);
5028 end if;
5029 end;
5031 -- Entity has delayed freeze, so we will generate an
5032 -- alignment check at the freeze point unless suppressed.
5034 if not Range_Checks_Suppressed (U_Ent)
5035 and then not Alignment_Checks_Suppressed (U_Ent)
5036 then
5037 Set_Check_Address_Alignment (N);
5038 end if;
5040 -- Kill the size check code, since we are not allocating
5041 -- the variable, it is somewhere else.
5043 Kill_Size_Check_Code (U_Ent);
5044 end;
5046 -- Not a valid entity for an address clause
5048 else
5049 Error_Msg_N ("address cannot be given for &", Nam);
5050 end if;
5051 end Address;
5053 ---------------
5054 -- Alignment --
5055 ---------------
5057 -- Alignment attribute definition clause
5059 when Attribute_Alignment => Alignment : declare
5060 Align : constant Uint := Get_Alignment_Value (Expr);
5061 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
5063 begin
5064 FOnly := True;
5066 if not Is_Type (U_Ent)
5067 and then Ekind (U_Ent) /= E_Variable
5068 and then Ekind (U_Ent) /= E_Constant
5069 then
5070 Error_Msg_N ("alignment cannot be given for &", Nam);
5072 elsif Duplicate_Clause then
5073 null;
5075 elsif Align /= No_Uint then
5076 Set_Has_Alignment_Clause (U_Ent);
5078 -- Tagged type case, check for attempt to set alignment to a
5079 -- value greater than Max_Align, and reset if so. This error
5080 -- is suppressed in ASIS mode to allow for different ASIS
5081 -- back ends or ASIS-based tools to query the illegal clause.
5083 if Is_Tagged_Type (U_Ent)
5084 and then Align > Max_Align
5085 and then not ASIS_Mode
5086 then
5087 Error_Msg_N
5088 ("alignment for & set to Maximum_Aligment??", Nam);
5089 Set_Alignment (U_Ent, Max_Align);
5091 -- All other cases
5093 else
5094 Set_Alignment (U_Ent, Align);
5095 end if;
5097 -- For an array type, U_Ent is the first subtype. In that case,
5098 -- also set the alignment of the anonymous base type so that
5099 -- other subtypes (such as the itypes for aggregates of the
5100 -- type) also receive the expected alignment.
5102 if Is_Array_Type (U_Ent) then
5103 Set_Alignment (Base_Type (U_Ent), Align);
5104 end if;
5105 end if;
5106 end Alignment;
5108 ---------------
5109 -- Bit_Order --
5110 ---------------
5112 -- Bit_Order attribute definition clause
5114 when Attribute_Bit_Order => Bit_Order : declare
5115 begin
5116 if not Is_Record_Type (U_Ent) then
5117 Error_Msg_N
5118 ("Bit_Order can only be defined for record type", Nam);
5120 elsif Duplicate_Clause then
5121 null;
5123 else
5124 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5126 if Etype (Expr) = Any_Type then
5127 return;
5129 elsif not Is_OK_Static_Expression (Expr) then
5130 Flag_Non_Static_Expr
5131 ("Bit_Order requires static expression!", Expr);
5133 else
5134 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5135 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
5136 end if;
5137 end if;
5138 end if;
5139 end Bit_Order;
5141 --------------------
5142 -- Component_Size --
5143 --------------------
5145 -- Component_Size attribute definition clause
5147 when Attribute_Component_Size => Component_Size_Case : declare
5148 Csize : constant Uint := Static_Integer (Expr);
5149 Ctyp : Entity_Id;
5150 Btype : Entity_Id;
5151 Biased : Boolean;
5152 New_Ctyp : Entity_Id;
5153 Decl : Node_Id;
5155 begin
5156 if not Is_Array_Type (U_Ent) then
5157 Error_Msg_N ("component size requires array type", Nam);
5158 return;
5159 end if;
5161 Btype := Base_Type (U_Ent);
5162 Ctyp := Component_Type (Btype);
5164 if Duplicate_Clause then
5165 null;
5167 elsif Rep_Item_Too_Early (Btype, N) then
5168 null;
5170 elsif Csize /= No_Uint then
5171 Check_Size (Expr, Ctyp, Csize, Biased);
5173 -- For the biased case, build a declaration for a subtype that
5174 -- will be used to represent the biased subtype that reflects
5175 -- the biased representation of components. We need the subtype
5176 -- to get proper conversions on referencing elements of the
5177 -- array.
5179 if Biased then
5180 New_Ctyp :=
5181 Make_Defining_Identifier (Loc,
5182 Chars =>
5183 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
5185 Decl :=
5186 Make_Subtype_Declaration (Loc,
5187 Defining_Identifier => New_Ctyp,
5188 Subtype_Indication =>
5189 New_Occurrence_Of (Component_Type (Btype), Loc));
5191 Set_Parent (Decl, N);
5192 Analyze (Decl, Suppress => All_Checks);
5194 Set_Has_Delayed_Freeze (New_Ctyp, False);
5195 Set_Esize (New_Ctyp, Csize);
5196 Set_RM_Size (New_Ctyp, Csize);
5197 Init_Alignment (New_Ctyp);
5198 Set_Is_Itype (New_Ctyp, True);
5199 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5201 Set_Component_Type (Btype, New_Ctyp);
5202 Set_Biased (New_Ctyp, N, "component size clause");
5203 end if;
5205 Set_Component_Size (Btype, Csize);
5207 -- Deal with warning on overridden size
5209 if Warn_On_Overridden_Size
5210 and then Has_Size_Clause (Ctyp)
5211 and then RM_Size (Ctyp) /= Csize
5212 then
5213 Error_Msg_NE
5214 ("component size overrides size clause for&?S?", N, Ctyp);
5215 end if;
5217 Set_Has_Component_Size_Clause (Btype, True);
5218 Set_Has_Non_Standard_Rep (Btype, True);
5219 end if;
5220 end Component_Size_Case;
5222 -----------------------
5223 -- Constant_Indexing --
5224 -----------------------
5226 when Attribute_Constant_Indexing =>
5227 Check_Indexing_Functions;
5229 ---------
5230 -- CPU --
5231 ---------
5233 when Attribute_CPU => CPU :
5234 begin
5235 -- CPU attribute definition clause not allowed except from aspect
5236 -- specification.
5238 if From_Aspect_Specification (N) then
5239 if not Is_Task_Type (U_Ent) then
5240 Error_Msg_N ("CPU can only be defined for task", Nam);
5242 elsif Duplicate_Clause then
5243 null;
5245 else
5246 -- The expression must be analyzed in the special manner
5247 -- described in "Handling of Default and Per-Object
5248 -- Expressions" in sem.ads.
5250 -- The visibility to the discriminants must be restored
5252 Push_Scope_And_Install_Discriminants (U_Ent);
5253 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5254 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5256 if not Is_OK_Static_Expression (Expr) then
5257 Check_Restriction (Static_Priorities, Expr);
5258 end if;
5259 end if;
5261 else
5262 Error_Msg_N
5263 ("attribute& cannot be set with definition clause", N);
5264 end if;
5265 end CPU;
5267 ----------------------
5268 -- Default_Iterator --
5269 ----------------------
5271 when Attribute_Default_Iterator => Default_Iterator : declare
5272 Func : Entity_Id;
5273 Typ : Entity_Id;
5275 begin
5276 -- If target type is untagged, further checks are irrelevant
5278 if not Is_Tagged_Type (U_Ent) then
5279 Error_Msg_N
5280 ("aspect Default_Iterator applies to tagged type", Nam);
5281 return;
5282 end if;
5284 Check_Iterator_Functions;
5286 Analyze (Expr);
5288 if not Is_Entity_Name (Expr)
5289 or else Ekind (Entity (Expr)) /= E_Function
5290 then
5291 Error_Msg_N ("aspect Iterator must be a function", Expr);
5292 return;
5293 else
5294 Func := Entity (Expr);
5295 end if;
5297 -- The type of the first parameter must be T, T'class, or a
5298 -- corresponding access type (5.5.1 (8/3). If function is
5299 -- parameterless label type accordingly.
5301 if No (First_Formal (Func)) then
5302 Typ := Any_Type;
5303 else
5304 Typ := Etype (First_Formal (Func));
5305 end if;
5307 if Typ = U_Ent
5308 or else Typ = Class_Wide_Type (U_Ent)
5309 or else (Is_Access_Type (Typ)
5310 and then Designated_Type (Typ) = U_Ent)
5311 or else (Is_Access_Type (Typ)
5312 and then Designated_Type (Typ) =
5313 Class_Wide_Type (U_Ent))
5314 then
5315 null;
5317 else
5318 Error_Msg_NE
5319 ("Default Iterator must be a primitive of&", Func, U_Ent);
5320 end if;
5321 end Default_Iterator;
5323 ------------------------
5324 -- Dispatching_Domain --
5325 ------------------------
5327 when Attribute_Dispatching_Domain => Dispatching_Domain :
5328 begin
5329 -- Dispatching_Domain attribute definition clause not allowed
5330 -- except from aspect specification.
5332 if From_Aspect_Specification (N) then
5333 if not Is_Task_Type (U_Ent) then
5334 Error_Msg_N
5335 ("Dispatching_Domain can only be defined for task", Nam);
5337 elsif Duplicate_Clause then
5338 null;
5340 else
5341 -- The expression must be analyzed in the special manner
5342 -- described in "Handling of Default and Per-Object
5343 -- Expressions" in sem.ads.
5345 -- The visibility to the discriminants must be restored
5347 Push_Scope_And_Install_Discriminants (U_Ent);
5349 Preanalyze_Spec_Expression
5350 (Expr, RTE (RE_Dispatching_Domain));
5352 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5353 end if;
5355 else
5356 Error_Msg_N
5357 ("attribute& cannot be set with definition clause", N);
5358 end if;
5359 end Dispatching_Domain;
5361 ------------------
5362 -- External_Tag --
5363 ------------------
5365 when Attribute_External_Tag => External_Tag :
5366 begin
5367 if not Is_Tagged_Type (U_Ent) then
5368 Error_Msg_N ("should be a tagged type", Nam);
5369 end if;
5371 if Duplicate_Clause then
5372 null;
5374 else
5375 Analyze_And_Resolve (Expr, Standard_String);
5377 if not Is_OK_Static_Expression (Expr) then
5378 Flag_Non_Static_Expr
5379 ("static string required for tag name!", Nam);
5380 end if;
5382 if not Is_Library_Level_Entity (U_Ent) then
5383 Error_Msg_NE
5384 ("??non-unique external tag supplied for &", N, U_Ent);
5385 Error_Msg_N
5386 ("\??same external tag applies to all subprogram calls",
5388 Error_Msg_N
5389 ("\??corresponding internal tag cannot be obtained", N);
5390 end if;
5391 end if;
5392 end External_Tag;
5394 --------------------------
5395 -- Implicit_Dereference --
5396 --------------------------
5398 when Attribute_Implicit_Dereference =>
5400 -- Legality checks already performed at the point of the type
5401 -- declaration, aspect is not delayed.
5403 null;
5405 -----------
5406 -- Input --
5407 -----------
5409 when Attribute_Input =>
5410 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5411 Set_Has_Specified_Stream_Input (Ent);
5413 ------------------------
5414 -- Interrupt_Priority --
5415 ------------------------
5417 when Attribute_Interrupt_Priority => Interrupt_Priority :
5418 begin
5419 -- Interrupt_Priority attribute definition clause not allowed
5420 -- except from aspect specification.
5422 if From_Aspect_Specification (N) then
5423 if not Is_Concurrent_Type (U_Ent) then
5424 Error_Msg_N
5425 ("Interrupt_Priority can only be defined for task and "
5426 & "protected object", Nam);
5428 elsif Duplicate_Clause then
5429 null;
5431 else
5432 -- The expression must be analyzed in the special manner
5433 -- described in "Handling of Default and Per-Object
5434 -- Expressions" in sem.ads.
5436 -- The visibility to the discriminants must be restored
5438 Push_Scope_And_Install_Discriminants (U_Ent);
5440 Preanalyze_Spec_Expression
5441 (Expr, RTE (RE_Interrupt_Priority));
5443 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5445 -- Check the No_Task_At_Interrupt_Priority restriction
5447 if Is_Task_Type (U_Ent) then
5448 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5449 end if;
5450 end if;
5452 else
5453 Error_Msg_N
5454 ("attribute& cannot be set with definition clause", N);
5455 end if;
5456 end Interrupt_Priority;
5458 --------------
5459 -- Iterable --
5460 --------------
5462 when Attribute_Iterable =>
5463 Analyze (Expr);
5465 if Nkind (Expr) /= N_Aggregate then
5466 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5467 end if;
5469 declare
5470 Assoc : Node_Id;
5472 begin
5473 Assoc := First (Component_Associations (Expr));
5474 while Present (Assoc) loop
5475 if not Is_Entity_Name (Expression (Assoc)) then
5476 Error_Msg_N ("value must be a function", Assoc);
5477 end if;
5479 Next (Assoc);
5480 end loop;
5481 end;
5483 ----------------------
5484 -- Iterator_Element --
5485 ----------------------
5487 when Attribute_Iterator_Element =>
5488 Analyze (Expr);
5490 if not Is_Entity_Name (Expr)
5491 or else not Is_Type (Entity (Expr))
5492 then
5493 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5494 end if;
5496 -------------------
5497 -- Machine_Radix --
5498 -------------------
5500 -- Machine radix attribute definition clause
5502 when Attribute_Machine_Radix => Machine_Radix : declare
5503 Radix : constant Uint := Static_Integer (Expr);
5505 begin
5506 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5507 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5509 elsif Duplicate_Clause then
5510 null;
5512 elsif Radix /= No_Uint then
5513 Set_Has_Machine_Radix_Clause (U_Ent);
5514 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5516 if Radix = 2 then
5517 null;
5519 elsif Radix = 10 then
5520 Set_Machine_Radix_10 (U_Ent);
5522 -- The following error is suppressed in ASIS mode to allow for
5523 -- different ASIS back ends or ASIS-based tools to query the
5524 -- illegal clause.
5526 elsif not ASIS_Mode then
5527 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5528 end if;
5529 end if;
5530 end Machine_Radix;
5532 -----------------
5533 -- Object_Size --
5534 -----------------
5536 -- Object_Size attribute definition clause
5538 when Attribute_Object_Size => Object_Size : declare
5539 Size : constant Uint := Static_Integer (Expr);
5541 Biased : Boolean;
5542 pragma Warnings (Off, Biased);
5544 begin
5545 if not Is_Type (U_Ent) then
5546 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5548 elsif Duplicate_Clause then
5549 null;
5551 else
5552 Check_Size (Expr, U_Ent, Size, Biased);
5554 -- The following errors are suppressed in ASIS mode to allow
5555 -- for different ASIS back ends or ASIS-based tools to query
5556 -- the illegal clause.
5558 if ASIS_Mode then
5559 null;
5561 elsif Is_Scalar_Type (U_Ent) then
5562 if Size /= 8 and then Size /= 16 and then Size /= 32
5563 and then UI_Mod (Size, 64) /= 0
5564 then
5565 Error_Msg_N
5566 ("Object_Size must be 8, 16, 32, or multiple of 64",
5567 Expr);
5568 end if;
5570 elsif Size mod 8 /= 0 then
5571 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5572 end if;
5574 Set_Esize (U_Ent, Size);
5575 Set_Has_Object_Size_Clause (U_Ent);
5576 Alignment_Check_For_Size_Change (U_Ent, Size);
5577 end if;
5578 end Object_Size;
5580 ------------
5581 -- Output --
5582 ------------
5584 when Attribute_Output =>
5585 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5586 Set_Has_Specified_Stream_Output (Ent);
5588 --------------
5589 -- Priority --
5590 --------------
5592 when Attribute_Priority => Priority :
5593 begin
5594 -- Priority attribute definition clause not allowed except from
5595 -- aspect specification.
5597 if From_Aspect_Specification (N) then
5598 if not (Is_Concurrent_Type (U_Ent)
5599 or else Ekind (U_Ent) = E_Procedure)
5600 then
5601 Error_Msg_N
5602 ("Priority can only be defined for task and protected "
5603 & "object", Nam);
5605 elsif Duplicate_Clause then
5606 null;
5608 else
5609 -- The expression must be analyzed in the special manner
5610 -- described in "Handling of Default and Per-Object
5611 -- Expressions" in sem.ads.
5613 -- The visibility to the discriminants must be restored
5615 Push_Scope_And_Install_Discriminants (U_Ent);
5616 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5617 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5619 if not Is_OK_Static_Expression (Expr) then
5620 Check_Restriction (Static_Priorities, Expr);
5621 end if;
5622 end if;
5624 else
5625 Error_Msg_N
5626 ("attribute& cannot be set with definition clause", N);
5627 end if;
5628 end Priority;
5630 ----------
5631 -- Read --
5632 ----------
5634 when Attribute_Read =>
5635 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5636 Set_Has_Specified_Stream_Read (Ent);
5638 --------------------------
5639 -- Scalar_Storage_Order --
5640 --------------------------
5642 -- Scalar_Storage_Order attribute definition clause
5644 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5645 begin
5646 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5647 Error_Msg_N
5648 ("Scalar_Storage_Order can only be defined for record or "
5649 & "array type", Nam);
5651 elsif Duplicate_Clause then
5652 null;
5654 else
5655 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5657 if Etype (Expr) = Any_Type then
5658 return;
5660 elsif not Is_OK_Static_Expression (Expr) then
5661 Flag_Non_Static_Expr
5662 ("Scalar_Storage_Order requires static expression!", Expr);
5664 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5666 -- Here for the case of a non-default (i.e. non-confirming)
5667 -- Scalar_Storage_Order attribute definition.
5669 if Support_Nondefault_SSO_On_Target then
5670 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5671 else
5672 Error_Msg_N
5673 ("non-default Scalar_Storage_Order not supported on "
5674 & "target", Expr);
5675 end if;
5676 end if;
5678 -- Clear SSO default indications since explicit setting of the
5679 -- order overrides the defaults.
5681 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5682 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5683 end if;
5684 end Scalar_Storage_Order;
5686 ----------
5687 -- Size --
5688 ----------
5690 -- Size attribute definition clause
5692 when Attribute_Size => Size : declare
5693 Size : constant Uint := Static_Integer (Expr);
5694 Etyp : Entity_Id;
5695 Biased : Boolean;
5697 begin
5698 FOnly := True;
5700 if Duplicate_Clause then
5701 null;
5703 elsif not Is_Type (U_Ent)
5704 and then Ekind (U_Ent) /= E_Variable
5705 and then Ekind (U_Ent) /= E_Constant
5706 then
5707 Error_Msg_N ("size cannot be given for &", Nam);
5709 elsif Is_Array_Type (U_Ent)
5710 and then not Is_Constrained (U_Ent)
5711 then
5712 Error_Msg_N
5713 ("size cannot be given for unconstrained array", Nam);
5715 elsif Size /= No_Uint then
5716 if Is_Type (U_Ent) then
5717 Etyp := U_Ent;
5718 else
5719 Etyp := Etype (U_Ent);
5720 end if;
5722 -- Check size, note that Gigi is in charge of checking that the
5723 -- size of an array or record type is OK. Also we do not check
5724 -- the size in the ordinary fixed-point case, since it is too
5725 -- early to do so (there may be subsequent small clause that
5726 -- affects the size). We can check the size if a small clause
5727 -- has already been given.
5729 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5730 or else Has_Small_Clause (U_Ent)
5731 then
5732 Check_Size (Expr, Etyp, Size, Biased);
5733 Set_Biased (U_Ent, N, "size clause", Biased);
5734 end if;
5736 -- For types set RM_Size and Esize if possible
5738 if Is_Type (U_Ent) then
5739 Set_RM_Size (U_Ent, Size);
5741 -- For elementary types, increase Object_Size to power of 2,
5742 -- but not less than a storage unit in any case (normally
5743 -- this means it will be byte addressable).
5745 -- For all other types, nothing else to do, we leave Esize
5746 -- (object size) unset, the back end will set it from the
5747 -- size and alignment in an appropriate manner.
5749 -- In both cases, we check whether the alignment must be
5750 -- reset in the wake of the size change.
5752 if Is_Elementary_Type (U_Ent) then
5753 if Size <= System_Storage_Unit then
5754 Init_Esize (U_Ent, System_Storage_Unit);
5755 elsif Size <= 16 then
5756 Init_Esize (U_Ent, 16);
5757 elsif Size <= 32 then
5758 Init_Esize (U_Ent, 32);
5759 else
5760 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5761 end if;
5763 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5764 else
5765 Alignment_Check_For_Size_Change (U_Ent, Size);
5766 end if;
5768 -- For objects, set Esize only
5770 else
5771 -- The following error is suppressed in ASIS mode to allow
5772 -- for different ASIS back ends or ASIS-based tools to query
5773 -- the illegal clause.
5775 if Is_Elementary_Type (Etyp)
5776 and then Size /= System_Storage_Unit
5777 and then Size /= System_Storage_Unit * 2
5778 and then Size /= System_Storage_Unit * 4
5779 and then Size /= System_Storage_Unit * 8
5780 and then not ASIS_Mode
5781 then
5782 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5783 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5784 Error_Msg_N
5785 ("size for primitive object must be a power of 2 in "
5786 & "the range ^-^", N);
5787 end if;
5789 Set_Esize (U_Ent, Size);
5790 end if;
5792 Set_Has_Size_Clause (U_Ent);
5793 end if;
5794 end Size;
5796 -----------
5797 -- Small --
5798 -----------
5800 -- Small attribute definition clause
5802 when Attribute_Small => Small : declare
5803 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5804 Small : Ureal;
5806 begin
5807 Analyze_And_Resolve (Expr, Any_Real);
5809 if Etype (Expr) = Any_Type then
5810 return;
5812 elsif not Is_OK_Static_Expression (Expr) then
5813 Flag_Non_Static_Expr
5814 ("small requires static expression!", Expr);
5815 return;
5817 else
5818 Small := Expr_Value_R (Expr);
5820 if Small <= Ureal_0 then
5821 Error_Msg_N ("small value must be greater than zero", Expr);
5822 return;
5823 end if;
5825 end if;
5827 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5828 Error_Msg_N
5829 ("small requires an ordinary fixed point type", Nam);
5831 elsif Has_Small_Clause (U_Ent) then
5832 Error_Msg_N ("small already given for &", Nam);
5834 elsif Small > Delta_Value (U_Ent) then
5835 Error_Msg_N
5836 ("small value must not be greater than delta value", Nam);
5838 else
5839 Set_Small_Value (U_Ent, Small);
5840 Set_Small_Value (Implicit_Base, Small);
5841 Set_Has_Small_Clause (U_Ent);
5842 Set_Has_Small_Clause (Implicit_Base);
5843 Set_Has_Non_Standard_Rep (Implicit_Base);
5844 end if;
5845 end Small;
5847 ------------------
5848 -- Storage_Pool --
5849 ------------------
5851 -- Storage_Pool attribute definition clause
5853 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5854 Pool : Entity_Id;
5855 T : Entity_Id;
5857 begin
5858 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5859 Error_Msg_N
5860 ("storage pool cannot be given for access-to-subprogram type",
5861 Nam);
5862 return;
5864 elsif not
5865 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5866 then
5867 Error_Msg_N
5868 ("storage pool can only be given for access types", Nam);
5869 return;
5871 elsif Is_Derived_Type (U_Ent) then
5872 Error_Msg_N
5873 ("storage pool cannot be given for a derived access type",
5874 Nam);
5876 elsif Duplicate_Clause then
5877 return;
5879 elsif Present (Associated_Storage_Pool (U_Ent)) then
5880 Error_Msg_N ("storage pool already given for &", Nam);
5881 return;
5882 end if;
5884 -- Check for Storage_Size previously given
5886 declare
5887 SS : constant Node_Id :=
5888 Get_Attribute_Definition_Clause
5889 (U_Ent, Attribute_Storage_Size);
5890 begin
5891 if Present (SS) then
5892 Check_Pool_Size_Clash (U_Ent, N, SS);
5893 end if;
5894 end;
5896 -- Storage_Pool case
5898 if Id = Attribute_Storage_Pool then
5899 Analyze_And_Resolve
5900 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5902 -- In the Simple_Storage_Pool case, we allow a variable of any
5903 -- simple storage pool type, so we Resolve without imposing an
5904 -- expected type.
5906 else
5907 Analyze_And_Resolve (Expr);
5909 if not Present (Get_Rep_Pragma
5910 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5911 then
5912 Error_Msg_N
5913 ("expression must be of a simple storage pool type", Expr);
5914 end if;
5915 end if;
5917 if not Denotes_Variable (Expr) then
5918 Error_Msg_N ("storage pool must be a variable", Expr);
5919 return;
5920 end if;
5922 if Nkind (Expr) = N_Type_Conversion then
5923 T := Etype (Expression (Expr));
5924 else
5925 T := Etype (Expr);
5926 end if;
5928 -- The Stack_Bounded_Pool is used internally for implementing
5929 -- access types with a Storage_Size. Since it only work properly
5930 -- when used on one specific type, we need to check that it is not
5931 -- hijacked improperly:
5933 -- type T is access Integer;
5934 -- for T'Storage_Size use n;
5935 -- type Q is access Float;
5936 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5938 if RTE_Available (RE_Stack_Bounded_Pool)
5939 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5940 then
5941 Error_Msg_N ("non-shareable internal Pool", Expr);
5942 return;
5943 end if;
5945 -- If the argument is a name that is not an entity name, then
5946 -- we construct a renaming operation to define an entity of
5947 -- type storage pool.
5949 if not Is_Entity_Name (Expr)
5950 and then Is_Object_Reference (Expr)
5951 then
5952 Pool := Make_Temporary (Loc, 'P', Expr);
5954 declare
5955 Rnode : constant Node_Id :=
5956 Make_Object_Renaming_Declaration (Loc,
5957 Defining_Identifier => Pool,
5958 Subtype_Mark =>
5959 New_Occurrence_Of (Etype (Expr), Loc),
5960 Name => Expr);
5962 begin
5963 -- If the attribute definition clause comes from an aspect
5964 -- clause, then insert the renaming before the associated
5965 -- entity's declaration, since the attribute clause has
5966 -- not yet been appended to the declaration list.
5968 if From_Aspect_Specification (N) then
5969 Insert_Before (Parent (Entity (N)), Rnode);
5970 else
5971 Insert_Before (N, Rnode);
5972 end if;
5974 Analyze (Rnode);
5975 Set_Associated_Storage_Pool (U_Ent, Pool);
5976 end;
5978 elsif Is_Entity_Name (Expr) then
5979 Pool := Entity (Expr);
5981 -- If pool is a renamed object, get original one. This can
5982 -- happen with an explicit renaming, and within instances.
5984 while Present (Renamed_Object (Pool))
5985 and then Is_Entity_Name (Renamed_Object (Pool))
5986 loop
5987 Pool := Entity (Renamed_Object (Pool));
5988 end loop;
5990 if Present (Renamed_Object (Pool))
5991 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5992 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5993 then
5994 Pool := Entity (Expression (Renamed_Object (Pool)));
5995 end if;
5997 Set_Associated_Storage_Pool (U_Ent, Pool);
5999 elsif Nkind (Expr) = N_Type_Conversion
6000 and then Is_Entity_Name (Expression (Expr))
6001 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
6002 then
6003 Pool := Entity (Expression (Expr));
6004 Set_Associated_Storage_Pool (U_Ent, Pool);
6006 else
6007 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
6008 return;
6009 end if;
6010 end;
6012 ------------------
6013 -- Storage_Size --
6014 ------------------
6016 -- Storage_Size attribute definition clause
6018 when Attribute_Storage_Size => Storage_Size : declare
6019 Btype : constant Entity_Id := Base_Type (U_Ent);
6021 begin
6022 if Is_Task_Type (U_Ent) then
6024 -- Check obsolescent (but never obsolescent if from aspect)
6026 if not From_Aspect_Specification (N) then
6027 Check_Restriction (No_Obsolescent_Features, N);
6029 if Warn_On_Obsolescent_Feature then
6030 Error_Msg_N
6031 ("?j?storage size clause for task is an obsolescent "
6032 & "feature (RM J.9)", N);
6033 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
6034 end if;
6035 end if;
6037 FOnly := True;
6038 end if;
6040 if not Is_Access_Type (U_Ent)
6041 and then Ekind (U_Ent) /= E_Task_Type
6042 then
6043 Error_Msg_N ("storage size cannot be given for &", Nam);
6045 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6046 Error_Msg_N
6047 ("storage size cannot be given for a derived access type",
6048 Nam);
6050 elsif Duplicate_Clause then
6051 null;
6053 else
6054 Analyze_And_Resolve (Expr, Any_Integer);
6056 if Is_Access_Type (U_Ent) then
6058 -- Check for Storage_Pool previously given
6060 declare
6061 SP : constant Node_Id :=
6062 Get_Attribute_Definition_Clause
6063 (U_Ent, Attribute_Storage_Pool);
6065 begin
6066 if Present (SP) then
6067 Check_Pool_Size_Clash (U_Ent, SP, N);
6068 end if;
6069 end;
6071 -- Special case of for x'Storage_Size use 0
6073 if Is_OK_Static_Expression (Expr)
6074 and then Expr_Value (Expr) = 0
6075 then
6076 Set_No_Pool_Assigned (Btype);
6077 end if;
6078 end if;
6080 Set_Has_Storage_Size_Clause (Btype);
6081 end if;
6082 end Storage_Size;
6084 -----------------
6085 -- Stream_Size --
6086 -----------------
6088 when Attribute_Stream_Size => Stream_Size : declare
6089 Size : constant Uint := Static_Integer (Expr);
6091 begin
6092 if Ada_Version <= Ada_95 then
6093 Check_Restriction (No_Implementation_Attributes, N);
6094 end if;
6096 if Duplicate_Clause then
6097 null;
6099 elsif Is_Elementary_Type (U_Ent) then
6101 -- The following errors are suppressed in ASIS mode to allow
6102 -- for different ASIS back ends or ASIS-based tools to query
6103 -- the illegal clause.
6105 if ASIS_Mode then
6106 null;
6108 elsif Size /= System_Storage_Unit
6109 and then Size /= System_Storage_Unit * 2
6110 and then Size /= System_Storage_Unit * 4
6111 and then Size /= System_Storage_Unit * 8
6112 then
6113 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6114 Error_Msg_N
6115 ("stream size for elementary type must be a power of 2 "
6116 & "and at least ^", N);
6118 elsif RM_Size (U_Ent) > Size then
6119 Error_Msg_Uint_1 := RM_Size (U_Ent);
6120 Error_Msg_N
6121 ("stream size for elementary type must be a power of 2 "
6122 & "and at least ^", N);
6123 end if;
6125 Set_Has_Stream_Size_Clause (U_Ent);
6127 else
6128 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6129 end if;
6130 end Stream_Size;
6132 ----------------
6133 -- Value_Size --
6134 ----------------
6136 -- Value_Size attribute definition clause
6138 when Attribute_Value_Size => Value_Size : declare
6139 Size : constant Uint := Static_Integer (Expr);
6140 Biased : Boolean;
6142 begin
6143 if not Is_Type (U_Ent) then
6144 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6146 elsif Duplicate_Clause then
6147 null;
6149 elsif Is_Array_Type (U_Ent)
6150 and then not Is_Constrained (U_Ent)
6151 then
6152 Error_Msg_N
6153 ("Value_Size cannot be given for unconstrained array", Nam);
6155 else
6156 if Is_Elementary_Type (U_Ent) then
6157 Check_Size (Expr, U_Ent, Size, Biased);
6158 Set_Biased (U_Ent, N, "value size clause", Biased);
6159 end if;
6161 Set_RM_Size (U_Ent, Size);
6162 end if;
6163 end Value_Size;
6165 -----------------------
6166 -- Variable_Indexing --
6167 -----------------------
6169 when Attribute_Variable_Indexing =>
6170 Check_Indexing_Functions;
6172 -----------
6173 -- Write --
6174 -----------
6176 when Attribute_Write =>
6177 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6178 Set_Has_Specified_Stream_Write (Ent);
6180 -- All other attributes cannot be set
6182 when others =>
6183 Error_Msg_N
6184 ("attribute& cannot be set with definition clause", N);
6185 end case;
6187 -- The test for the type being frozen must be performed after any
6188 -- expression the clause has been analyzed since the expression itself
6189 -- might cause freezing that makes the clause illegal.
6191 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6192 return;
6193 end if;
6194 end Analyze_Attribute_Definition_Clause;
6196 ----------------------------
6197 -- Analyze_Code_Statement --
6198 ----------------------------
6200 procedure Analyze_Code_Statement (N : Node_Id) is
6201 HSS : constant Node_Id := Parent (N);
6202 SBody : constant Node_Id := Parent (HSS);
6203 Subp : constant Entity_Id := Current_Scope;
6204 Stmt : Node_Id;
6205 Decl : Node_Id;
6206 StmtO : Node_Id;
6207 DeclO : Node_Id;
6209 begin
6210 -- Accept foreign code statements for CodePeer. The analysis is skipped
6211 -- to avoid rejecting unrecognized constructs.
6213 if CodePeer_Mode then
6214 Set_Analyzed (N);
6215 return;
6216 end if;
6218 -- Analyze and check we get right type, note that this implements the
6219 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6220 -- the only way that Asm_Insn could possibly be visible.
6222 Analyze_And_Resolve (Expression (N));
6224 if Etype (Expression (N)) = Any_Type then
6225 return;
6226 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6227 Error_Msg_N ("incorrect type for code statement", N);
6228 return;
6229 end if;
6231 Check_Code_Statement (N);
6233 -- Make sure we appear in the handled statement sequence of a subprogram
6234 -- (RM 13.8(3)).
6236 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6237 or else Nkind (SBody) /= N_Subprogram_Body
6238 then
6239 Error_Msg_N
6240 ("code statement can only appear in body of subprogram", N);
6241 return;
6242 end if;
6244 -- Do remaining checks (RM 13.8(3)) if not already done
6246 if not Is_Machine_Code_Subprogram (Subp) then
6247 Set_Is_Machine_Code_Subprogram (Subp);
6249 -- No exception handlers allowed
6251 if Present (Exception_Handlers (HSS)) then
6252 Error_Msg_N
6253 ("exception handlers not permitted in machine code subprogram",
6254 First (Exception_Handlers (HSS)));
6255 end if;
6257 -- No declarations other than use clauses and pragmas (we allow
6258 -- certain internally generated declarations as well).
6260 Decl := First (Declarations (SBody));
6261 while Present (Decl) loop
6262 DeclO := Original_Node (Decl);
6263 if Comes_From_Source (DeclO)
6264 and not Nkind_In (DeclO, N_Pragma,
6265 N_Use_Package_Clause,
6266 N_Use_Type_Clause,
6267 N_Implicit_Label_Declaration)
6268 then
6269 Error_Msg_N
6270 ("this declaration not allowed in machine code subprogram",
6271 DeclO);
6272 end if;
6274 Next (Decl);
6275 end loop;
6277 -- No statements other than code statements, pragmas, and labels.
6278 -- Again we allow certain internally generated statements.
6280 -- In Ada 2012, qualified expressions are names, and the code
6281 -- statement is initially parsed as a procedure call.
6283 Stmt := First (Statements (HSS));
6284 while Present (Stmt) loop
6285 StmtO := Original_Node (Stmt);
6287 -- A procedure call transformed into a code statement is OK
6289 if Ada_Version >= Ada_2012
6290 and then Nkind (StmtO) = N_Procedure_Call_Statement
6291 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6292 then
6293 null;
6295 elsif Comes_From_Source (StmtO)
6296 and then not Nkind_In (StmtO, N_Pragma,
6297 N_Label,
6298 N_Code_Statement)
6299 then
6300 Error_Msg_N
6301 ("this statement is not allowed in machine code subprogram",
6302 StmtO);
6303 end if;
6305 Next (Stmt);
6306 end loop;
6307 end if;
6308 end Analyze_Code_Statement;
6310 -----------------------------------------------
6311 -- Analyze_Enumeration_Representation_Clause --
6312 -----------------------------------------------
6314 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6315 Ident : constant Node_Id := Identifier (N);
6316 Aggr : constant Node_Id := Array_Aggregate (N);
6317 Enumtype : Entity_Id;
6318 Elit : Entity_Id;
6319 Expr : Node_Id;
6320 Assoc : Node_Id;
6321 Choice : Node_Id;
6322 Val : Uint;
6324 Err : Boolean := False;
6325 -- Set True to avoid cascade errors and crashes on incorrect source code
6327 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6328 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6329 -- Allowed range of universal integer (= allowed range of enum lit vals)
6331 Min : Uint;
6332 Max : Uint;
6333 -- Minimum and maximum values of entries
6335 Max_Node : Node_Id;
6336 -- Pointer to node for literal providing max value
6338 begin
6339 if Ignore_Rep_Clauses then
6340 Kill_Rep_Clause (N);
6341 return;
6342 end if;
6344 -- Ignore enumeration rep clauses by default in CodePeer mode,
6345 -- unless -gnatd.I is specified, as a work around for potential false
6346 -- positive messages.
6348 if CodePeer_Mode and not Debug_Flag_Dot_II then
6349 return;
6350 end if;
6352 -- First some basic error checks
6354 Find_Type (Ident);
6355 Enumtype := Entity (Ident);
6357 if Enumtype = Any_Type
6358 or else Rep_Item_Too_Early (Enumtype, N)
6359 then
6360 return;
6361 else
6362 Enumtype := Underlying_Type (Enumtype);
6363 end if;
6365 if not Is_Enumeration_Type (Enumtype) then
6366 Error_Msg_NE
6367 ("enumeration type required, found}",
6368 Ident, First_Subtype (Enumtype));
6369 return;
6370 end if;
6372 -- Ignore rep clause on generic actual type. This will already have
6373 -- been flagged on the template as an error, and this is the safest
6374 -- way to ensure we don't get a junk cascaded message in the instance.
6376 if Is_Generic_Actual_Type (Enumtype) then
6377 return;
6379 -- Type must be in current scope
6381 elsif Scope (Enumtype) /= Current_Scope then
6382 Error_Msg_N ("type must be declared in this scope", Ident);
6383 return;
6385 -- Type must be a first subtype
6387 elsif not Is_First_Subtype (Enumtype) then
6388 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6389 return;
6391 -- Ignore duplicate rep clause
6393 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6394 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6395 return;
6397 -- Don't allow rep clause for standard [wide_[wide_]]character
6399 elsif Is_Standard_Character_Type (Enumtype) then
6400 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6401 return;
6403 -- Check that the expression is a proper aggregate (no parentheses)
6405 elsif Paren_Count (Aggr) /= 0 then
6406 Error_Msg
6407 ("extra parentheses surrounding aggregate not allowed",
6408 First_Sloc (Aggr));
6409 return;
6411 -- All tests passed, so set rep clause in place
6413 else
6414 Set_Has_Enumeration_Rep_Clause (Enumtype);
6415 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6416 end if;
6418 -- Now we process the aggregate. Note that we don't use the normal
6419 -- aggregate code for this purpose, because we don't want any of the
6420 -- normal expansion activities, and a number of special semantic
6421 -- rules apply (including the component type being any integer type)
6423 Elit := First_Literal (Enumtype);
6425 -- First the positional entries if any
6427 if Present (Expressions (Aggr)) then
6428 Expr := First (Expressions (Aggr));
6429 while Present (Expr) loop
6430 if No (Elit) then
6431 Error_Msg_N ("too many entries in aggregate", Expr);
6432 return;
6433 end if;
6435 Val := Static_Integer (Expr);
6437 -- Err signals that we found some incorrect entries processing
6438 -- the list. The final checks for completeness and ordering are
6439 -- skipped in this case.
6441 if Val = No_Uint then
6442 Err := True;
6444 elsif Val < Lo or else Hi < Val then
6445 Error_Msg_N ("value outside permitted range", Expr);
6446 Err := True;
6447 end if;
6449 Set_Enumeration_Rep (Elit, Val);
6450 Set_Enumeration_Rep_Expr (Elit, Expr);
6451 Next (Expr);
6452 Next (Elit);
6453 end loop;
6454 end if;
6456 -- Now process the named entries if present
6458 if Present (Component_Associations (Aggr)) then
6459 Assoc := First (Component_Associations (Aggr));
6460 while Present (Assoc) loop
6461 Choice := First (Choices (Assoc));
6463 if Present (Next (Choice)) then
6464 Error_Msg_N
6465 ("multiple choice not allowed here", Next (Choice));
6466 Err := True;
6467 end if;
6469 if Nkind (Choice) = N_Others_Choice then
6470 Error_Msg_N ("others choice not allowed here", Choice);
6471 Err := True;
6473 elsif Nkind (Choice) = N_Range then
6475 -- ??? should allow zero/one element range here
6477 Error_Msg_N ("range not allowed here", Choice);
6478 Err := True;
6480 else
6481 Analyze_And_Resolve (Choice, Enumtype);
6483 if Error_Posted (Choice) then
6484 Err := True;
6485 end if;
6487 if not Err then
6488 if Is_Entity_Name (Choice)
6489 and then Is_Type (Entity (Choice))
6490 then
6491 Error_Msg_N ("subtype name not allowed here", Choice);
6492 Err := True;
6494 -- ??? should allow static subtype with zero/one entry
6496 elsif Etype (Choice) = Base_Type (Enumtype) then
6497 if not Is_OK_Static_Expression (Choice) then
6498 Flag_Non_Static_Expr
6499 ("non-static expression used for choice!", Choice);
6500 Err := True;
6502 else
6503 Elit := Expr_Value_E (Choice);
6505 if Present (Enumeration_Rep_Expr (Elit)) then
6506 Error_Msg_Sloc :=
6507 Sloc (Enumeration_Rep_Expr (Elit));
6508 Error_Msg_NE
6509 ("representation for& previously given#",
6510 Choice, Elit);
6511 Err := True;
6512 end if;
6514 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6516 Expr := Expression (Assoc);
6517 Val := Static_Integer (Expr);
6519 if Val = No_Uint then
6520 Err := True;
6522 elsif Val < Lo or else Hi < Val then
6523 Error_Msg_N ("value outside permitted range", Expr);
6524 Err := True;
6525 end if;
6527 Set_Enumeration_Rep (Elit, Val);
6528 end if;
6529 end if;
6530 end if;
6531 end if;
6533 Next (Assoc);
6534 end loop;
6535 end if;
6537 -- Aggregate is fully processed. Now we check that a full set of
6538 -- representations was given, and that they are in range and in order.
6539 -- These checks are only done if no other errors occurred.
6541 if not Err then
6542 Min := No_Uint;
6543 Max := No_Uint;
6545 Elit := First_Literal (Enumtype);
6546 while Present (Elit) loop
6547 if No (Enumeration_Rep_Expr (Elit)) then
6548 Error_Msg_NE ("missing representation for&!", N, Elit);
6550 else
6551 Val := Enumeration_Rep (Elit);
6553 if Min = No_Uint then
6554 Min := Val;
6555 end if;
6557 if Val /= No_Uint then
6558 if Max /= No_Uint and then Val <= Max then
6559 Error_Msg_NE
6560 ("enumeration value for& not ordered!",
6561 Enumeration_Rep_Expr (Elit), Elit);
6562 end if;
6564 Max_Node := Enumeration_Rep_Expr (Elit);
6565 Max := Val;
6566 end if;
6568 -- If there is at least one literal whose representation is not
6569 -- equal to the Pos value, then note that this enumeration type
6570 -- has a non-standard representation.
6572 if Val /= Enumeration_Pos (Elit) then
6573 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6574 end if;
6575 end if;
6577 Next (Elit);
6578 end loop;
6580 -- Now set proper size information
6582 declare
6583 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6585 begin
6586 if Has_Size_Clause (Enumtype) then
6588 -- All OK, if size is OK now
6590 if RM_Size (Enumtype) >= Minsize then
6591 null;
6593 else
6594 -- Try if we can get by with biasing
6596 Minsize :=
6597 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6599 -- Error message if even biasing does not work
6601 if RM_Size (Enumtype) < Minsize then
6602 Error_Msg_Uint_1 := RM_Size (Enumtype);
6603 Error_Msg_Uint_2 := Max;
6604 Error_Msg_N
6605 ("previously given size (^) is too small "
6606 & "for this value (^)", Max_Node);
6608 -- If biasing worked, indicate that we now have biased rep
6610 else
6611 Set_Biased
6612 (Enumtype, Size_Clause (Enumtype), "size clause");
6613 end if;
6614 end if;
6616 else
6617 Set_RM_Size (Enumtype, Minsize);
6618 Set_Enum_Esize (Enumtype);
6619 end if;
6621 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6622 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6623 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6624 end;
6625 end if;
6627 -- We repeat the too late test in case it froze itself
6629 if Rep_Item_Too_Late (Enumtype, N) then
6630 null;
6631 end if;
6632 end Analyze_Enumeration_Representation_Clause;
6634 ----------------------------
6635 -- Analyze_Free_Statement --
6636 ----------------------------
6638 procedure Analyze_Free_Statement (N : Node_Id) is
6639 begin
6640 Analyze (Expression (N));
6641 end Analyze_Free_Statement;
6643 ---------------------------
6644 -- Analyze_Freeze_Entity --
6645 ---------------------------
6647 procedure Analyze_Freeze_Entity (N : Node_Id) is
6648 begin
6649 Freeze_Entity_Checks (N);
6650 end Analyze_Freeze_Entity;
6652 -----------------------------------
6653 -- Analyze_Freeze_Generic_Entity --
6654 -----------------------------------
6656 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6657 E : constant Entity_Id := Entity (N);
6659 begin
6660 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
6661 Analyze_Aspects_At_Freeze_Point (E);
6662 end if;
6664 Freeze_Entity_Checks (N);
6665 end Analyze_Freeze_Generic_Entity;
6667 ------------------------------------------
6668 -- Analyze_Record_Representation_Clause --
6669 ------------------------------------------
6671 -- Note: we check as much as we can here, but we can't do any checks
6672 -- based on the position values (e.g. overlap checks) until freeze time
6673 -- because especially in Ada 2005 (machine scalar mode), the processing
6674 -- for non-standard bit order can substantially change the positions.
6675 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6676 -- for the remainder of this processing.
6678 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6679 Ident : constant Node_Id := Identifier (N);
6680 Biased : Boolean;
6681 CC : Node_Id;
6682 Comp : Entity_Id;
6683 Fbit : Uint;
6684 Hbit : Uint := Uint_0;
6685 Lbit : Uint;
6686 Ocomp : Entity_Id;
6687 Posit : Uint;
6688 Rectype : Entity_Id;
6689 Recdef : Node_Id;
6691 function Is_Inherited (Comp : Entity_Id) return Boolean;
6692 -- True if Comp is an inherited component in a record extension
6694 ------------------
6695 -- Is_Inherited --
6696 ------------------
6698 function Is_Inherited (Comp : Entity_Id) return Boolean is
6699 Comp_Base : Entity_Id;
6701 begin
6702 if Ekind (Rectype) = E_Record_Subtype then
6703 Comp_Base := Original_Record_Component (Comp);
6704 else
6705 Comp_Base := Comp;
6706 end if;
6708 return Comp_Base /= Original_Record_Component (Comp_Base);
6709 end Is_Inherited;
6711 -- Local variables
6713 Is_Record_Extension : Boolean;
6714 -- True if Rectype is a record extension
6716 CR_Pragma : Node_Id := Empty;
6717 -- Points to N_Pragma node if Complete_Representation pragma present
6719 -- Start of processing for Analyze_Record_Representation_Clause
6721 begin
6722 if Ignore_Rep_Clauses then
6723 Kill_Rep_Clause (N);
6724 return;
6725 end if;
6727 Find_Type (Ident);
6728 Rectype := Entity (Ident);
6730 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6731 return;
6732 else
6733 Rectype := Underlying_Type (Rectype);
6734 end if;
6736 -- First some basic error checks
6738 if not Is_Record_Type (Rectype) then
6739 Error_Msg_NE
6740 ("record type required, found}", Ident, First_Subtype (Rectype));
6741 return;
6743 elsif Scope (Rectype) /= Current_Scope then
6744 Error_Msg_N ("type must be declared in this scope", N);
6745 return;
6747 elsif not Is_First_Subtype (Rectype) then
6748 Error_Msg_N ("cannot give record rep clause for subtype", N);
6749 return;
6751 elsif Has_Record_Rep_Clause (Rectype) then
6752 Error_Msg_N ("duplicate record rep clause ignored", N);
6753 return;
6755 elsif Rep_Item_Too_Late (Rectype, N) then
6756 return;
6757 end if;
6759 -- We know we have a first subtype, now possibly go to the anonymous
6760 -- base type to determine whether Rectype is a record extension.
6762 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6763 Is_Record_Extension :=
6764 Nkind (Recdef) = N_Derived_Type_Definition
6765 and then Present (Record_Extension_Part (Recdef));
6767 if Present (Mod_Clause (N)) then
6768 declare
6769 Loc : constant Source_Ptr := Sloc (N);
6770 M : constant Node_Id := Mod_Clause (N);
6771 P : constant List_Id := Pragmas_Before (M);
6772 AtM_Nod : Node_Id;
6774 Mod_Val : Uint;
6775 pragma Warnings (Off, Mod_Val);
6777 begin
6778 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6780 if Warn_On_Obsolescent_Feature then
6781 Error_Msg_N
6782 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6783 Error_Msg_N
6784 ("\?j?use alignment attribute definition clause instead", N);
6785 end if;
6787 if Present (P) then
6788 Analyze_List (P);
6789 end if;
6791 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6792 -- the Mod clause into an alignment clause anyway, so that the
6793 -- back end can compute and back-annotate properly the size and
6794 -- alignment of types that may include this record.
6796 -- This seems dubious, this destroys the source tree in a manner
6797 -- not detectable by ASIS ???
6799 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6800 AtM_Nod :=
6801 Make_Attribute_Definition_Clause (Loc,
6802 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6803 Chars => Name_Alignment,
6804 Expression => Relocate_Node (Expression (M)));
6806 Set_From_At_Mod (AtM_Nod);
6807 Insert_After (N, AtM_Nod);
6808 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6809 Set_Mod_Clause (N, Empty);
6811 else
6812 -- Get the alignment value to perform error checking
6814 Mod_Val := Get_Alignment_Value (Expression (M));
6815 end if;
6816 end;
6817 end if;
6819 -- For untagged types, clear any existing component clauses for the
6820 -- type. If the type is derived, this is what allows us to override
6821 -- a rep clause for the parent. For type extensions, the representation
6822 -- of the inherited components is inherited, so we want to keep previous
6823 -- component clauses for completeness.
6825 if not Is_Tagged_Type (Rectype) then
6826 Comp := First_Component_Or_Discriminant (Rectype);
6827 while Present (Comp) loop
6828 Set_Component_Clause (Comp, Empty);
6829 Next_Component_Or_Discriminant (Comp);
6830 end loop;
6831 end if;
6833 -- All done if no component clauses
6835 CC := First (Component_Clauses (N));
6837 if No (CC) then
6838 return;
6839 end if;
6841 -- A representation like this applies to the base type
6843 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6844 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6845 Set_Has_Specified_Layout (Base_Type (Rectype));
6847 -- Process the component clauses
6849 while Present (CC) loop
6851 -- Pragma
6853 if Nkind (CC) = N_Pragma then
6854 Analyze (CC);
6856 -- The only pragma of interest is Complete_Representation
6858 if Pragma_Name (CC) = Name_Complete_Representation then
6859 CR_Pragma := CC;
6860 end if;
6862 -- Processing for real component clause
6864 else
6865 Posit := Static_Integer (Position (CC));
6866 Fbit := Static_Integer (First_Bit (CC));
6867 Lbit := Static_Integer (Last_Bit (CC));
6869 if Posit /= No_Uint
6870 and then Fbit /= No_Uint
6871 and then Lbit /= No_Uint
6872 then
6873 if Posit < 0 then
6874 Error_Msg_N ("position cannot be negative", Position (CC));
6876 elsif Fbit < 0 then
6877 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
6879 -- The Last_Bit specified in a component clause must not be
6880 -- less than the First_Bit minus one (RM-13.5.1(10)).
6882 elsif Lbit < Fbit - 1 then
6883 Error_Msg_N
6884 ("last bit cannot be less than first bit minus one",
6885 Last_Bit (CC));
6887 -- Values look OK, so find the corresponding record component
6888 -- Even though the syntax allows an attribute reference for
6889 -- implementation-defined components, GNAT does not allow the
6890 -- tag to get an explicit position.
6892 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6893 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6894 Error_Msg_N ("position of tag cannot be specified", CC);
6895 else
6896 Error_Msg_N ("illegal component name", CC);
6897 end if;
6899 else
6900 Comp := First_Entity (Rectype);
6901 while Present (Comp) loop
6902 exit when Chars (Comp) = Chars (Component_Name (CC));
6903 Next_Entity (Comp);
6904 end loop;
6906 if No (Comp) then
6908 -- Maybe component of base type that is absent from
6909 -- statically constrained first subtype.
6911 Comp := First_Entity (Base_Type (Rectype));
6912 while Present (Comp) loop
6913 exit when Chars (Comp) = Chars (Component_Name (CC));
6914 Next_Entity (Comp);
6915 end loop;
6916 end if;
6918 if No (Comp) then
6919 Error_Msg_N
6920 ("component clause is for non-existent field", CC);
6922 -- Ada 2012 (AI05-0026): Any name that denotes a
6923 -- discriminant of an object of an unchecked union type
6924 -- shall not occur within a record_representation_clause.
6926 -- The general restriction of using record rep clauses on
6927 -- Unchecked_Union types has now been lifted. Since it is
6928 -- possible to introduce a record rep clause which mentions
6929 -- the discriminant of an Unchecked_Union in non-Ada 2012
6930 -- code, this check is applied to all versions of the
6931 -- language.
6933 elsif Ekind (Comp) = E_Discriminant
6934 and then Is_Unchecked_Union (Rectype)
6935 then
6936 Error_Msg_N
6937 ("cannot reference discriminant of unchecked union",
6938 Component_Name (CC));
6940 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6941 Error_Msg_NE
6942 ("component clause not allowed for inherited "
6943 & "component&", CC, Comp);
6945 elsif Present (Component_Clause (Comp)) then
6947 -- Diagnose duplicate rep clause, or check consistency
6948 -- if this is an inherited component. In a double fault,
6949 -- there may be a duplicate inconsistent clause for an
6950 -- inherited component.
6952 if Scope (Original_Record_Component (Comp)) = Rectype
6953 or else Parent (Component_Clause (Comp)) = N
6954 then
6955 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6956 Error_Msg_N ("component clause previously given#", CC);
6958 else
6959 declare
6960 Rep1 : constant Node_Id := Component_Clause (Comp);
6961 begin
6962 if Intval (Position (Rep1)) /=
6963 Intval (Position (CC))
6964 or else Intval (First_Bit (Rep1)) /=
6965 Intval (First_Bit (CC))
6966 or else Intval (Last_Bit (Rep1)) /=
6967 Intval (Last_Bit (CC))
6968 then
6969 Error_Msg_N
6970 ("component clause inconsistent with "
6971 & "representation of ancestor", CC);
6973 elsif Warn_On_Redundant_Constructs then
6974 Error_Msg_N
6975 ("?r?redundant confirming component clause "
6976 & "for component!", CC);
6977 end if;
6978 end;
6979 end if;
6981 -- Normal case where this is the first component clause we
6982 -- have seen for this entity, so set it up properly.
6984 else
6985 -- Make reference for field in record rep clause and set
6986 -- appropriate entity field in the field identifier.
6988 Generate_Reference
6989 (Comp, Component_Name (CC), Set_Ref => False);
6990 Set_Entity (Component_Name (CC), Comp);
6992 -- Update Fbit and Lbit to the actual bit number
6994 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6995 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6997 if Has_Size_Clause (Rectype)
6998 and then RM_Size (Rectype) <= Lbit
6999 then
7000 Error_Msg_N
7001 ("bit number out of range of specified size",
7002 Last_Bit (CC));
7003 else
7004 Set_Component_Clause (Comp, CC);
7005 Set_Component_Bit_Offset (Comp, Fbit);
7006 Set_Esize (Comp, 1 + (Lbit - Fbit));
7007 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
7008 Set_Normalized_Position (Comp, Fbit / SSU);
7010 if Warn_On_Overridden_Size
7011 and then Has_Size_Clause (Etype (Comp))
7012 and then RM_Size (Etype (Comp)) /= Esize (Comp)
7013 then
7014 Error_Msg_NE
7015 ("?S?component size overrides size clause for&",
7016 Component_Name (CC), Etype (Comp));
7017 end if;
7019 -- This information is also set in the corresponding
7020 -- component of the base type, found by accessing the
7021 -- Original_Record_Component link if it is present.
7023 Ocomp := Original_Record_Component (Comp);
7025 if Hbit < Lbit then
7026 Hbit := Lbit;
7027 end if;
7029 Check_Size
7030 (Component_Name (CC),
7031 Etype (Comp),
7032 Esize (Comp),
7033 Biased);
7035 Set_Biased
7036 (Comp, First_Node (CC), "component clause", Biased);
7038 if Present (Ocomp) then
7039 Set_Component_Clause (Ocomp, CC);
7040 Set_Component_Bit_Offset (Ocomp, Fbit);
7041 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
7042 Set_Normalized_Position (Ocomp, Fbit / SSU);
7043 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7045 Set_Normalized_Position_Max
7046 (Ocomp, Normalized_Position (Ocomp));
7048 -- Note: we don't use Set_Biased here, because we
7049 -- already gave a warning above if needed, and we
7050 -- would get a duplicate for the same name here.
7052 Set_Has_Biased_Representation
7053 (Ocomp, Has_Biased_Representation (Comp));
7054 end if;
7056 if Esize (Comp) < 0 then
7057 Error_Msg_N ("component size is negative", CC);
7058 end if;
7059 end if;
7060 end if;
7061 end if;
7062 end if;
7063 end if;
7065 Next (CC);
7066 end loop;
7068 -- Check missing components if Complete_Representation pragma appeared
7070 if Present (CR_Pragma) then
7071 Comp := First_Component_Or_Discriminant (Rectype);
7072 while Present (Comp) loop
7073 if No (Component_Clause (Comp)) then
7074 Error_Msg_NE
7075 ("missing component clause for &", CR_Pragma, Comp);
7076 end if;
7078 Next_Component_Or_Discriminant (Comp);
7079 end loop;
7081 -- Give missing components warning if required
7083 elsif Warn_On_Unrepped_Components then
7084 declare
7085 Num_Repped_Components : Nat := 0;
7086 Num_Unrepped_Components : Nat := 0;
7088 begin
7089 -- First count number of repped and unrepped components
7091 Comp := First_Component_Or_Discriminant (Rectype);
7092 while Present (Comp) loop
7093 if Present (Component_Clause (Comp)) then
7094 Num_Repped_Components := Num_Repped_Components + 1;
7095 else
7096 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7097 end if;
7099 Next_Component_Or_Discriminant (Comp);
7100 end loop;
7102 -- We are only interested in the case where there is at least one
7103 -- unrepped component, and at least half the components have rep
7104 -- clauses. We figure that if less than half have them, then the
7105 -- partial rep clause is really intentional. If the component
7106 -- type has no underlying type set at this point (as for a generic
7107 -- formal type), we don't know enough to give a warning on the
7108 -- component.
7110 if Num_Unrepped_Components > 0
7111 and then Num_Unrepped_Components < Num_Repped_Components
7112 then
7113 Comp := First_Component_Or_Discriminant (Rectype);
7114 while Present (Comp) loop
7115 if No (Component_Clause (Comp))
7116 and then Comes_From_Source (Comp)
7117 and then Present (Underlying_Type (Etype (Comp)))
7118 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
7119 or else Size_Known_At_Compile_Time
7120 (Underlying_Type (Etype (Comp))))
7121 and then not Has_Warnings_Off (Rectype)
7123 -- Ignore discriminant in unchecked union, since it is
7124 -- not there, and cannot have a component clause.
7126 and then (not Is_Unchecked_Union (Rectype)
7127 or else Ekind (Comp) /= E_Discriminant)
7128 then
7129 Error_Msg_Sloc := Sloc (Comp);
7130 Error_Msg_NE
7131 ("?C?no component clause given for & declared #",
7132 N, Comp);
7133 end if;
7135 Next_Component_Or_Discriminant (Comp);
7136 end loop;
7137 end if;
7138 end;
7139 end if;
7140 end Analyze_Record_Representation_Clause;
7142 -------------------------------------
7143 -- Build_Discrete_Static_Predicate --
7144 -------------------------------------
7146 procedure Build_Discrete_Static_Predicate
7147 (Typ : Entity_Id;
7148 Expr : Node_Id;
7149 Nam : Name_Id)
7151 Loc : constant Source_Ptr := Sloc (Expr);
7153 Non_Static : exception;
7154 -- Raised if something non-static is found
7156 Btyp : constant Entity_Id := Base_Type (Typ);
7158 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7159 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7160 -- Low bound and high bound value of base type of Typ
7162 TLo : Uint;
7163 THi : Uint;
7164 -- Bounds for constructing the static predicate. We use the bound of the
7165 -- subtype if it is static, otherwise the corresponding base type bound.
7166 -- Note: a non-static subtype can have a static predicate.
7168 type REnt is record
7169 Lo, Hi : Uint;
7170 end record;
7171 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7172 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7173 -- value.
7175 type RList is array (Nat range <>) of REnt;
7176 -- A list of ranges. The ranges are sorted in increasing order, and are
7177 -- disjoint (there is a gap of at least one value between each range in
7178 -- the table). A value is in the set of ranges in Rlist if it lies
7179 -- within one of these ranges.
7181 False_Range : constant RList :=
7182 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7183 -- An empty set of ranges represents a range list that can never be
7184 -- satisfied, since there are no ranges in which the value could lie,
7185 -- so it does not lie in any of them. False_Range is a canonical value
7186 -- for this empty set, but general processing should test for an Rlist
7187 -- with length zero (see Is_False predicate), since other null ranges
7188 -- may appear which must be treated as False.
7190 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7191 -- Range representing True, value must be in the base range
7193 function "and" (Left : RList; Right : RList) return RList;
7194 -- And's together two range lists, returning a range list. This is a set
7195 -- intersection operation.
7197 function "or" (Left : RList; Right : RList) return RList;
7198 -- Or's together two range lists, returning a range list. This is a set
7199 -- union operation.
7201 function "not" (Right : RList) return RList;
7202 -- Returns complement of a given range list, i.e. a range list
7203 -- representing all the values in TLo .. THi that are not in the input
7204 -- operand Right.
7206 function Build_Val (V : Uint) return Node_Id;
7207 -- Return an analyzed N_Identifier node referencing this value, suitable
7208 -- for use as an entry in the Static_Discrte_Predicate list. This node
7209 -- is typed with the base type.
7211 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7212 -- Return an analyzed N_Range node referencing this range, suitable for
7213 -- use as an entry in the Static_Discrete_Predicate list. This node is
7214 -- typed with the base type.
7216 function Get_RList (Exp : Node_Id) return RList;
7217 -- This is a recursive routine that converts the given expression into a
7218 -- list of ranges, suitable for use in building the static predicate.
7220 function Is_False (R : RList) return Boolean;
7221 pragma Inline (Is_False);
7222 -- Returns True if the given range list is empty, and thus represents a
7223 -- False list of ranges that can never be satisfied.
7225 function Is_True (R : RList) return Boolean;
7226 -- Returns True if R trivially represents the True predicate by having a
7227 -- single range from BLo to BHi.
7229 function Is_Type_Ref (N : Node_Id) return Boolean;
7230 pragma Inline (Is_Type_Ref);
7231 -- Returns if True if N is a reference to the type for the predicate in
7232 -- the expression (i.e. if it is an identifier whose Chars field matches
7233 -- the Nam given in the call). N must not be parenthesized, if the type
7234 -- name appears in parens, this routine will return False.
7236 function Lo_Val (N : Node_Id) return Uint;
7237 -- Given an entry from a Static_Discrete_Predicate list that is either
7238 -- a static expression or static range, gets either the expression value
7239 -- or the low bound of the range.
7241 function Hi_Val (N : Node_Id) return Uint;
7242 -- Given an entry from a Static_Discrete_Predicate list that is either
7243 -- a static expression or static range, gets either the expression value
7244 -- or the high bound of the range.
7246 function Membership_Entry (N : Node_Id) return RList;
7247 -- Given a single membership entry (range, value, or subtype), returns
7248 -- the corresponding range list. Raises Static_Error if not static.
7250 function Membership_Entries (N : Node_Id) return RList;
7251 -- Given an element on an alternatives list of a membership operation,
7252 -- returns the range list corresponding to this entry and all following
7253 -- entries (i.e. returns the "or" of this list of values).
7255 function Stat_Pred (Typ : Entity_Id) return RList;
7256 -- Given a type, if it has a static predicate, then return the predicate
7257 -- as a range list, otherwise raise Non_Static.
7259 -----------
7260 -- "and" --
7261 -----------
7263 function "and" (Left : RList; Right : RList) return RList is
7264 FEnt : REnt;
7265 -- First range of result
7267 SLeft : Nat := Left'First;
7268 -- Start of rest of left entries
7270 SRight : Nat := Right'First;
7271 -- Start of rest of right entries
7273 begin
7274 -- If either range is True, return the other
7276 if Is_True (Left) then
7277 return Right;
7278 elsif Is_True (Right) then
7279 return Left;
7280 end if;
7282 -- If either range is False, return False
7284 if Is_False (Left) or else Is_False (Right) then
7285 return False_Range;
7286 end if;
7288 -- Loop to remove entries at start that are disjoint, and thus just
7289 -- get discarded from the result entirely.
7291 loop
7292 -- If no operands left in either operand, result is false
7294 if SLeft > Left'Last or else SRight > Right'Last then
7295 return False_Range;
7297 -- Discard first left operand entry if disjoint with right
7299 elsif Left (SLeft).Hi < Right (SRight).Lo then
7300 SLeft := SLeft + 1;
7302 -- Discard first right operand entry if disjoint with left
7304 elsif Right (SRight).Hi < Left (SLeft).Lo then
7305 SRight := SRight + 1;
7307 -- Otherwise we have an overlapping entry
7309 else
7310 exit;
7311 end if;
7312 end loop;
7314 -- Now we have two non-null operands, and first entries overlap. The
7315 -- first entry in the result will be the overlapping part of these
7316 -- two entries.
7318 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7319 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7321 -- Now we can remove the entry that ended at a lower value, since its
7322 -- contribution is entirely contained in Fent.
7324 if Left (SLeft).Hi <= Right (SRight).Hi then
7325 SLeft := SLeft + 1;
7326 else
7327 SRight := SRight + 1;
7328 end if;
7330 -- Compute result by concatenating this first entry with the "and" of
7331 -- the remaining parts of the left and right operands. Note that if
7332 -- either of these is empty, "and" will yield empty, so that we will
7333 -- end up with just Fent, which is what we want in that case.
7335 return
7336 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7337 end "and";
7339 -----------
7340 -- "not" --
7341 -----------
7343 function "not" (Right : RList) return RList is
7344 begin
7345 -- Return True if False range
7347 if Is_False (Right) then
7348 return True_Range;
7349 end if;
7351 -- Return False if True range
7353 if Is_True (Right) then
7354 return False_Range;
7355 end if;
7357 -- Here if not trivial case
7359 declare
7360 Result : RList (1 .. Right'Length + 1);
7361 -- May need one more entry for gap at beginning and end
7363 Count : Nat := 0;
7364 -- Number of entries stored in Result
7366 begin
7367 -- Gap at start
7369 if Right (Right'First).Lo > TLo then
7370 Count := Count + 1;
7371 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7372 end if;
7374 -- Gaps between ranges
7376 for J in Right'First .. Right'Last - 1 loop
7377 Count := Count + 1;
7378 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7379 end loop;
7381 -- Gap at end
7383 if Right (Right'Last).Hi < THi then
7384 Count := Count + 1;
7385 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7386 end if;
7388 return Result (1 .. Count);
7389 end;
7390 end "not";
7392 ----------
7393 -- "or" --
7394 ----------
7396 function "or" (Left : RList; Right : RList) return RList is
7397 FEnt : REnt;
7398 -- First range of result
7400 SLeft : Nat := Left'First;
7401 -- Start of rest of left entries
7403 SRight : Nat := Right'First;
7404 -- Start of rest of right entries
7406 begin
7407 -- If either range is True, return True
7409 if Is_True (Left) or else Is_True (Right) then
7410 return True_Range;
7411 end if;
7413 -- If either range is False (empty), return the other
7415 if Is_False (Left) then
7416 return Right;
7417 elsif Is_False (Right) then
7418 return Left;
7419 end if;
7421 -- Initialize result first entry from left or right operand depending
7422 -- on which starts with the lower range.
7424 if Left (SLeft).Lo < Right (SRight).Lo then
7425 FEnt := Left (SLeft);
7426 SLeft := SLeft + 1;
7427 else
7428 FEnt := Right (SRight);
7429 SRight := SRight + 1;
7430 end if;
7432 -- This loop eats ranges from left and right operands that are
7433 -- contiguous with the first range we are gathering.
7435 loop
7436 -- Eat first entry in left operand if contiguous or overlapped by
7437 -- gathered first operand of result.
7439 if SLeft <= Left'Last
7440 and then Left (SLeft).Lo <= FEnt.Hi + 1
7441 then
7442 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7443 SLeft := SLeft + 1;
7445 -- Eat first entry in right operand if contiguous or overlapped by
7446 -- gathered right operand of result.
7448 elsif SRight <= Right'Last
7449 and then Right (SRight).Lo <= FEnt.Hi + 1
7450 then
7451 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7452 SRight := SRight + 1;
7454 -- All done if no more entries to eat
7456 else
7457 exit;
7458 end if;
7459 end loop;
7461 -- Obtain result as the first entry we just computed, concatenated
7462 -- to the "or" of the remaining results (if one operand is empty,
7463 -- this will just concatenate with the other
7465 return
7466 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7467 end "or";
7469 -----------------
7470 -- Build_Range --
7471 -----------------
7473 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7474 Result : Node_Id;
7475 begin
7476 Result :=
7477 Make_Range (Loc,
7478 Low_Bound => Build_Val (Lo),
7479 High_Bound => Build_Val (Hi));
7480 Set_Etype (Result, Btyp);
7481 Set_Analyzed (Result);
7482 return Result;
7483 end Build_Range;
7485 ---------------
7486 -- Build_Val --
7487 ---------------
7489 function Build_Val (V : Uint) return Node_Id is
7490 Result : Node_Id;
7492 begin
7493 if Is_Enumeration_Type (Typ) then
7494 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7495 else
7496 Result := Make_Integer_Literal (Loc, V);
7497 end if;
7499 Set_Etype (Result, Btyp);
7500 Set_Is_Static_Expression (Result);
7501 Set_Analyzed (Result);
7502 return Result;
7503 end Build_Val;
7505 ---------------
7506 -- Get_RList --
7507 ---------------
7509 function Get_RList (Exp : Node_Id) return RList is
7510 Op : Node_Kind;
7511 Val : Uint;
7513 begin
7514 -- Static expression can only be true or false
7516 if Is_OK_Static_Expression (Exp) then
7517 if Expr_Value (Exp) = 0 then
7518 return False_Range;
7519 else
7520 return True_Range;
7521 end if;
7522 end if;
7524 -- Otherwise test node type
7526 Op := Nkind (Exp);
7528 case Op is
7530 -- And
7532 when N_Op_And | N_And_Then =>
7533 return Get_RList (Left_Opnd (Exp))
7535 Get_RList (Right_Opnd (Exp));
7537 -- Or
7539 when N_Op_Or | N_Or_Else =>
7540 return Get_RList (Left_Opnd (Exp))
7542 Get_RList (Right_Opnd (Exp));
7544 -- Not
7546 when N_Op_Not =>
7547 return not Get_RList (Right_Opnd (Exp));
7549 -- Comparisons of type with static value
7551 when N_Op_Compare =>
7553 -- Type is left operand
7555 if Is_Type_Ref (Left_Opnd (Exp))
7556 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7557 then
7558 Val := Expr_Value (Right_Opnd (Exp));
7560 -- Typ is right operand
7562 elsif Is_Type_Ref (Right_Opnd (Exp))
7563 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7564 then
7565 Val := Expr_Value (Left_Opnd (Exp));
7567 -- Invert sense of comparison
7569 case Op is
7570 when N_Op_Gt => Op := N_Op_Lt;
7571 when N_Op_Lt => Op := N_Op_Gt;
7572 when N_Op_Ge => Op := N_Op_Le;
7573 when N_Op_Le => Op := N_Op_Ge;
7574 when others => null;
7575 end case;
7577 -- Other cases are non-static
7579 else
7580 raise Non_Static;
7581 end if;
7583 -- Construct range according to comparison operation
7585 case Op is
7586 when N_Op_Eq =>
7587 return RList'(1 => REnt'(Val, Val));
7589 when N_Op_Ge =>
7590 return RList'(1 => REnt'(Val, BHi));
7592 when N_Op_Gt =>
7593 return RList'(1 => REnt'(Val + 1, BHi));
7595 when N_Op_Le =>
7596 return RList'(1 => REnt'(BLo, Val));
7598 when N_Op_Lt =>
7599 return RList'(1 => REnt'(BLo, Val - 1));
7601 when N_Op_Ne =>
7602 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7604 when others =>
7605 raise Program_Error;
7606 end case;
7608 -- Membership (IN)
7610 when N_In =>
7611 if not Is_Type_Ref (Left_Opnd (Exp)) then
7612 raise Non_Static;
7613 end if;
7615 if Present (Right_Opnd (Exp)) then
7616 return Membership_Entry (Right_Opnd (Exp));
7617 else
7618 return Membership_Entries (First (Alternatives (Exp)));
7619 end if;
7621 -- Negative membership (NOT IN)
7623 when N_Not_In =>
7624 if not Is_Type_Ref (Left_Opnd (Exp)) then
7625 raise Non_Static;
7626 end if;
7628 if Present (Right_Opnd (Exp)) then
7629 return not Membership_Entry (Right_Opnd (Exp));
7630 else
7631 return not Membership_Entries (First (Alternatives (Exp)));
7632 end if;
7634 -- Function call, may be call to static predicate
7636 when N_Function_Call =>
7637 if Is_Entity_Name (Name (Exp)) then
7638 declare
7639 Ent : constant Entity_Id := Entity (Name (Exp));
7640 begin
7641 if Is_Predicate_Function (Ent)
7642 or else
7643 Is_Predicate_Function_M (Ent)
7644 then
7645 return Stat_Pred (Etype (First_Formal (Ent)));
7646 end if;
7647 end;
7648 end if;
7650 -- Other function call cases are non-static
7652 raise Non_Static;
7654 -- Qualified expression, dig out the expression
7656 when N_Qualified_Expression =>
7657 return Get_RList (Expression (Exp));
7659 when N_Case_Expression =>
7660 declare
7661 Alt : Node_Id;
7662 Choices : List_Id;
7663 Dep : Node_Id;
7665 begin
7666 if not Is_Entity_Name (Expression (Expr))
7667 or else Etype (Expression (Expr)) /= Typ
7668 then
7669 Error_Msg_N
7670 ("expression must denaote subtype", Expression (Expr));
7671 return False_Range;
7672 end if;
7674 -- Collect discrete choices in all True alternatives
7676 Choices := New_List;
7677 Alt := First (Alternatives (Exp));
7678 while Present (Alt) loop
7679 Dep := Expression (Alt);
7681 if not Is_OK_Static_Expression (Dep) then
7682 raise Non_Static;
7684 elsif Is_True (Expr_Value (Dep)) then
7685 Append_List_To (Choices,
7686 New_Copy_List (Discrete_Choices (Alt)));
7687 end if;
7689 Next (Alt);
7690 end loop;
7692 return Membership_Entries (First (Choices));
7693 end;
7695 -- Expression with actions: if no actions, dig out expression
7697 when N_Expression_With_Actions =>
7698 if Is_Empty_List (Actions (Exp)) then
7699 return Get_RList (Expression (Exp));
7700 else
7701 raise Non_Static;
7702 end if;
7704 -- Xor operator
7706 when N_Op_Xor =>
7707 return (Get_RList (Left_Opnd (Exp))
7708 and not Get_RList (Right_Opnd (Exp)))
7709 or (Get_RList (Right_Opnd (Exp))
7710 and not Get_RList (Left_Opnd (Exp)));
7712 -- Any other node type is non-static
7714 when others =>
7715 raise Non_Static;
7716 end case;
7717 end Get_RList;
7719 ------------
7720 -- Hi_Val --
7721 ------------
7723 function Hi_Val (N : Node_Id) return Uint is
7724 begin
7725 if Is_OK_Static_Expression (N) then
7726 return Expr_Value (N);
7727 else
7728 pragma Assert (Nkind (N) = N_Range);
7729 return Expr_Value (High_Bound (N));
7730 end if;
7731 end Hi_Val;
7733 --------------
7734 -- Is_False --
7735 --------------
7737 function Is_False (R : RList) return Boolean is
7738 begin
7739 return R'Length = 0;
7740 end Is_False;
7742 -------------
7743 -- Is_True --
7744 -------------
7746 function Is_True (R : RList) return Boolean is
7747 begin
7748 return R'Length = 1
7749 and then R (R'First).Lo = BLo
7750 and then R (R'First).Hi = BHi;
7751 end Is_True;
7753 -----------------
7754 -- Is_Type_Ref --
7755 -----------------
7757 function Is_Type_Ref (N : Node_Id) return Boolean is
7758 begin
7759 return Nkind (N) = N_Identifier
7760 and then Chars (N) = Nam
7761 and then Paren_Count (N) = 0;
7762 end Is_Type_Ref;
7764 ------------
7765 -- Lo_Val --
7766 ------------
7768 function Lo_Val (N : Node_Id) return Uint is
7769 begin
7770 if Is_OK_Static_Expression (N) then
7771 return Expr_Value (N);
7772 else
7773 pragma Assert (Nkind (N) = N_Range);
7774 return Expr_Value (Low_Bound (N));
7775 end if;
7776 end Lo_Val;
7778 ------------------------
7779 -- Membership_Entries --
7780 ------------------------
7782 function Membership_Entries (N : Node_Id) return RList is
7783 begin
7784 if No (Next (N)) then
7785 return Membership_Entry (N);
7786 else
7787 return Membership_Entry (N) or Membership_Entries (Next (N));
7788 end if;
7789 end Membership_Entries;
7791 ----------------------
7792 -- Membership_Entry --
7793 ----------------------
7795 function Membership_Entry (N : Node_Id) return RList is
7796 Val : Uint;
7797 SLo : Uint;
7798 SHi : Uint;
7800 begin
7801 -- Range case
7803 if Nkind (N) = N_Range then
7804 if not Is_OK_Static_Expression (Low_Bound (N))
7805 or else
7806 not Is_OK_Static_Expression (High_Bound (N))
7807 then
7808 raise Non_Static;
7809 else
7810 SLo := Expr_Value (Low_Bound (N));
7811 SHi := Expr_Value (High_Bound (N));
7812 return RList'(1 => REnt'(SLo, SHi));
7813 end if;
7815 -- Static expression case
7817 elsif Is_OK_Static_Expression (N) then
7818 Val := Expr_Value (N);
7819 return RList'(1 => REnt'(Val, Val));
7821 -- Identifier (other than static expression) case
7823 else pragma Assert (Nkind (N) = N_Identifier);
7825 -- Type case
7827 if Is_Type (Entity (N)) then
7829 -- If type has predicates, process them
7831 if Has_Predicates (Entity (N)) then
7832 return Stat_Pred (Entity (N));
7834 -- For static subtype without predicates, get range
7836 elsif Is_OK_Static_Subtype (Entity (N)) then
7837 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7838 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7839 return RList'(1 => REnt'(SLo, SHi));
7841 -- Any other type makes us non-static
7843 else
7844 raise Non_Static;
7845 end if;
7847 -- Any other kind of identifier in predicate (e.g. a non-static
7848 -- expression value) means this is not a static predicate.
7850 else
7851 raise Non_Static;
7852 end if;
7853 end if;
7854 end Membership_Entry;
7856 ---------------
7857 -- Stat_Pred --
7858 ---------------
7860 function Stat_Pred (Typ : Entity_Id) return RList is
7861 begin
7862 -- Not static if type does not have static predicates
7864 if not Has_Static_Predicate (Typ) then
7865 raise Non_Static;
7866 end if;
7868 -- Otherwise we convert the predicate list to a range list
7870 declare
7871 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7872 Result : RList (1 .. List_Length (Spred));
7873 P : Node_Id;
7875 begin
7876 P := First (Static_Discrete_Predicate (Typ));
7877 for J in Result'Range loop
7878 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7879 Next (P);
7880 end loop;
7882 return Result;
7883 end;
7884 end Stat_Pred;
7886 -- Start of processing for Build_Discrete_Static_Predicate
7888 begin
7889 -- Establish bounds for the predicate
7891 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7892 TLo := Expr_Value (Type_Low_Bound (Typ));
7893 else
7894 TLo := BLo;
7895 end if;
7897 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7898 THi := Expr_Value (Type_High_Bound (Typ));
7899 else
7900 THi := BHi;
7901 end if;
7903 -- Analyze the expression to see if it is a static predicate
7905 declare
7906 Ranges : constant RList := Get_RList (Expr);
7907 -- Range list from expression if it is static
7909 Plist : List_Id;
7911 begin
7912 -- Convert range list into a form for the static predicate. In the
7913 -- Ranges array, we just have raw ranges, these must be converted
7914 -- to properly typed and analyzed static expressions or range nodes.
7916 -- Note: here we limit ranges to the ranges of the subtype, so that
7917 -- a predicate is always false for values outside the subtype. That
7918 -- seems fine, such values are invalid anyway, and considering them
7919 -- to fail the predicate seems allowed and friendly, and furthermore
7920 -- simplifies processing for case statements and loops.
7922 Plist := New_List;
7924 for J in Ranges'Range loop
7925 declare
7926 Lo : Uint := Ranges (J).Lo;
7927 Hi : Uint := Ranges (J).Hi;
7929 begin
7930 -- Ignore completely out of range entry
7932 if Hi < TLo or else Lo > THi then
7933 null;
7935 -- Otherwise process entry
7937 else
7938 -- Adjust out of range value to subtype range
7940 if Lo < TLo then
7941 Lo := TLo;
7942 end if;
7944 if Hi > THi then
7945 Hi := THi;
7946 end if;
7948 -- Convert range into required form
7950 Append_To (Plist, Build_Range (Lo, Hi));
7951 end if;
7952 end;
7953 end loop;
7955 -- Processing was successful and all entries were static, so now we
7956 -- can store the result as the predicate list.
7958 Set_Static_Discrete_Predicate (Typ, Plist);
7960 -- The processing for static predicates put the expression into
7961 -- canonical form as a series of ranges. It also eliminated
7962 -- duplicates and collapsed and combined ranges. We might as well
7963 -- replace the alternatives list of the right operand of the
7964 -- membership test with the static predicate list, which will
7965 -- usually be more efficient.
7967 declare
7968 New_Alts : constant List_Id := New_List;
7969 Old_Node : Node_Id;
7970 New_Node : Node_Id;
7972 begin
7973 Old_Node := First (Plist);
7974 while Present (Old_Node) loop
7975 New_Node := New_Copy (Old_Node);
7977 if Nkind (New_Node) = N_Range then
7978 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7979 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7980 end if;
7982 Append_To (New_Alts, New_Node);
7983 Next (Old_Node);
7984 end loop;
7986 -- If empty list, replace by False
7988 if Is_Empty_List (New_Alts) then
7989 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7991 -- Else replace by set membership test
7993 else
7994 Rewrite (Expr,
7995 Make_In (Loc,
7996 Left_Opnd => Make_Identifier (Loc, Nam),
7997 Right_Opnd => Empty,
7998 Alternatives => New_Alts));
8000 -- Resolve new expression in function context
8002 Install_Formals (Predicate_Function (Typ));
8003 Push_Scope (Predicate_Function (Typ));
8004 Analyze_And_Resolve (Expr, Standard_Boolean);
8005 Pop_Scope;
8006 end if;
8007 end;
8008 end;
8010 -- If non-static, return doing nothing
8012 exception
8013 when Non_Static =>
8014 return;
8015 end Build_Discrete_Static_Predicate;
8017 --------------------------------
8018 -- Build_Export_Import_Pragma --
8019 --------------------------------
8021 function Build_Export_Import_Pragma
8022 (Asp : Node_Id;
8023 Id : Entity_Id) return Node_Id
8025 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
8026 Expr : constant Node_Id := Expression (Asp);
8027 Loc : constant Source_Ptr := Sloc (Asp);
8029 Args : List_Id;
8030 Conv : Node_Id;
8031 Conv_Arg : Node_Id;
8032 Dummy_1 : Node_Id;
8033 Dummy_2 : Node_Id;
8034 EN : Node_Id;
8035 LN : Node_Id;
8036 Prag : Node_Id;
8038 Create_Pragma : Boolean := False;
8039 -- This flag is set when the aspect form is such that it warrants the
8040 -- creation of a corresponding pragma.
8042 begin
8043 if Present (Expr) then
8044 if Error_Posted (Expr) then
8045 null;
8047 elsif Is_True (Expr_Value (Expr)) then
8048 Create_Pragma := True;
8049 end if;
8051 -- Otherwise the aspect defaults to True
8053 else
8054 Create_Pragma := True;
8055 end if;
8057 -- Nothing to do when the expression is False or is erroneous
8059 if not Create_Pragma then
8060 return Empty;
8061 end if;
8063 -- Obtain all interfacing aspects that apply to the related entity
8065 Get_Interfacing_Aspects
8066 (Iface_Asp => Asp,
8067 Conv_Asp => Conv,
8068 EN_Asp => EN,
8069 Expo_Asp => Dummy_1,
8070 Imp_Asp => Dummy_2,
8071 LN_Asp => LN);
8073 Args := New_List;
8075 -- Handle the convention argument
8077 if Present (Conv) then
8078 Conv_Arg := New_Copy_Tree (Expression (Conv));
8080 -- Assume convention "Ada' when aspect Convention is missing
8082 else
8083 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8084 end if;
8086 Append_To (Args,
8087 Make_Pragma_Argument_Association (Loc,
8088 Chars => Name_Convention,
8089 Expression => Conv_Arg));
8091 -- Handle the entity argument
8093 Append_To (Args,
8094 Make_Pragma_Argument_Association (Loc,
8095 Chars => Name_Entity,
8096 Expression => New_Occurrence_Of (Id, Loc)));
8098 -- Handle the External_Name argument
8100 if Present (EN) then
8101 Append_To (Args,
8102 Make_Pragma_Argument_Association (Loc,
8103 Chars => Name_External_Name,
8104 Expression => New_Copy_Tree (Expression (EN))));
8105 end if;
8107 -- Handle the Link_Name argument
8109 if Present (LN) then
8110 Append_To (Args,
8111 Make_Pragma_Argument_Association (Loc,
8112 Chars => Name_Link_Name,
8113 Expression => New_Copy_Tree (Expression (LN))));
8114 end if;
8116 -- Generate:
8117 -- pragma Export/Import
8118 -- (Convention => <Conv>/Ada,
8119 -- Entity => <Id>,
8120 -- [External_Name => <EN>,]
8121 -- [Link_Name => <LN>]);
8123 Prag :=
8124 Make_Pragma (Loc,
8125 Pragma_Identifier =>
8126 Make_Identifier (Loc, Chars (Identifier (Asp))),
8127 Pragma_Argument_Associations => Args);
8129 -- Decorate the relevant aspect and the pragma
8131 Set_Aspect_Rep_Item (Asp, Prag);
8133 Set_Corresponding_Aspect (Prag, Asp);
8134 Set_From_Aspect_Specification (Prag);
8135 Set_Parent (Prag, Asp);
8137 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8138 Set_Import_Pragma (Id, Prag);
8139 end if;
8141 return Prag;
8142 end Build_Export_Import_Pragma;
8144 -------------------------------
8145 -- Build_Predicate_Functions --
8146 -------------------------------
8148 -- The procedures that are constructed here have the form:
8150 -- function typPredicate (Ixxx : typ) return Boolean is
8151 -- begin
8152 -- return
8153 -- typ1Predicate (typ1 (Ixxx))
8154 -- and then typ2Predicate (typ2 (Ixxx))
8155 -- and then ...;
8156 -- exp1 and then exp2 and then ...
8157 -- end typPredicate;
8159 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8160 -- this is the point at which these expressions get analyzed, providing the
8161 -- required delay, and typ1, typ2, are entities from which predicates are
8162 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8163 -- use this function even if checks are off, e.g. for membership tests.
8165 -- Note that the inherited predicates are evaluated first, as required by
8166 -- AI12-0071-1.
8168 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8169 -- the form of this return expression.
8171 -- If the expression has at least one Raise_Expression, then we also build
8172 -- the typPredicateM version of the function, in which any occurrence of a
8173 -- Raise_Expression is converted to "return False".
8175 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8176 Loc : constant Source_Ptr := Sloc (Typ);
8178 Expr : Node_Id;
8179 -- This is the expression for the result of the function. It is
8180 -- is build by connecting the component predicates with AND THEN.
8182 Expr_M : Node_Id;
8183 -- This is the corresponding return expression for the Predicate_M
8184 -- function. It differs in that raise expressions are marked for
8185 -- special expansion (see Process_REs).
8187 Object_Name : Name_Id;
8188 -- Name for argument of Predicate procedure. Note that we use the same
8189 -- name for both predicate functions. That way the reference within the
8190 -- predicate expression is the same in both functions.
8192 Object_Entity : Entity_Id;
8193 -- Entity for argument of Predicate procedure
8195 Object_Entity_M : Entity_Id;
8196 -- Entity for argument of separate Predicate procedure when exceptions
8197 -- are present in expression.
8199 FDecl : Node_Id;
8200 -- The function declaration
8202 SId : Entity_Id;
8203 -- Its entity
8205 Raise_Expression_Present : Boolean := False;
8206 -- Set True if Expr has at least one Raise_Expression
8208 procedure Add_Condition (Cond : Node_Id);
8209 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8210 -- Expr is empty).
8212 procedure Add_Predicates;
8213 -- Appends expressions for any Predicate pragmas in the rep item chain
8214 -- Typ to Expr. Note that we look only at items for this exact entity.
8215 -- Inheritance of predicates for the parent type is done by calling the
8216 -- Predicate_Function of the parent type, using Add_Call above.
8218 procedure Add_Call (T : Entity_Id);
8219 -- Includes a call to the predicate function for type T in Expr if T
8220 -- has predicates and Predicate_Function (T) is non-empty.
8222 function Process_RE (N : Node_Id) return Traverse_Result;
8223 -- Used in Process REs, tests if node N is a raise expression, and if
8224 -- so, marks it to be converted to return False.
8226 procedure Process_REs is new Traverse_Proc (Process_RE);
8227 -- Marks any raise expressions in Expr_M to return False
8229 function Test_RE (N : Node_Id) return Traverse_Result;
8230 -- Used in Test_REs, tests one node for being a raise expression, and if
8231 -- so sets Raise_Expression_Present True.
8233 procedure Test_REs is new Traverse_Proc (Test_RE);
8234 -- Tests to see if Expr contains any raise expressions
8236 --------------
8237 -- Add_Call --
8238 --------------
8240 procedure Add_Call (T : Entity_Id) is
8241 Exp : Node_Id;
8243 begin
8244 if Present (T) and then Present (Predicate_Function (T)) then
8245 Set_Has_Predicates (Typ);
8247 -- Build the call to the predicate function of T
8249 Exp :=
8250 Make_Predicate_Call
8251 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8253 -- "and"-in the call to evolving expression
8255 Add_Condition (Exp);
8257 -- Output info message on inheritance if required. Note we do not
8258 -- give this information for generic actual types, since it is
8259 -- unwelcome noise in that case in instantiations. We also
8260 -- generally suppress the message in instantiations, and also
8261 -- if it involves internal names.
8263 if Opt.List_Inherited_Aspects
8264 and then not Is_Generic_Actual_Type (Typ)
8265 and then Instantiation_Depth (Sloc (Typ)) = 0
8266 and then not Is_Internal_Name (Chars (T))
8267 and then not Is_Internal_Name (Chars (Typ))
8268 then
8269 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8270 Error_Msg_Node_2 := T;
8271 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8272 end if;
8273 end if;
8274 end Add_Call;
8276 -------------------
8277 -- Add_Condition --
8278 -------------------
8280 procedure Add_Condition (Cond : Node_Id) is
8281 begin
8282 -- This is the first predicate expression
8284 if No (Expr) then
8285 Expr := Cond;
8287 -- Otherwise concatenate to the existing predicate expressions by
8288 -- using "and then".
8290 else
8291 Expr :=
8292 Make_And_Then (Loc,
8293 Left_Opnd => Relocate_Node (Expr),
8294 Right_Opnd => Cond);
8295 end if;
8296 end Add_Condition;
8298 --------------------
8299 -- Add_Predicates --
8300 --------------------
8302 procedure Add_Predicates is
8303 procedure Add_Predicate (Prag : Node_Id);
8304 -- Concatenate the expression of predicate pragma Prag to Expr by
8305 -- using a short circuit "and then" operator.
8307 -------------------
8308 -- Add_Predicate --
8309 -------------------
8311 procedure Add_Predicate (Prag : Node_Id) is
8312 procedure Replace_Type_Reference (N : Node_Id);
8313 -- Replace a single occurrence N of the subtype name with a
8314 -- reference to the formal of the predicate function. N can be an
8315 -- identifier referencing the subtype, or a selected component,
8316 -- representing an appropriately qualified occurrence of the
8317 -- subtype name.
8319 procedure Replace_Type_References is
8320 new Replace_Type_References_Generic (Replace_Type_Reference);
8321 -- Traverse an expression changing every occurrence of an
8322 -- identifier whose name matches the name of the subtype with a
8323 -- reference to the formal parameter of the predicate function.
8325 ----------------------------
8326 -- Replace_Type_Reference --
8327 ----------------------------
8329 procedure Replace_Type_Reference (N : Node_Id) is
8330 begin
8331 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8332 -- Use the Sloc of the usage name, not the defining name
8334 Set_Etype (N, Typ);
8335 Set_Entity (N, Object_Entity);
8337 -- We want to treat the node as if it comes from source, so
8338 -- that ASIS will not ignore it.
8340 Set_Comes_From_Source (N, True);
8341 end Replace_Type_Reference;
8343 -- Local variables
8345 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8346 Arg1 : Node_Id;
8347 Arg2 : Node_Id;
8349 -- Start of processing for Add_Predicate
8351 begin
8352 -- Extract the arguments of the pragma. The expression itself
8353 -- is copied for use in the predicate function, to preserve the
8354 -- original version for ASIS use.
8356 Arg1 := First (Pragma_Argument_Associations (Prag));
8357 Arg2 := Next (Arg1);
8359 Arg1 := Get_Pragma_Arg (Arg1);
8360 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8362 -- When the predicate pragma applies to the current type or its
8363 -- full view, replace all occurrences of the subtype name with
8364 -- references to the formal parameter of the predicate function.
8366 if Entity (Arg1) = Typ
8367 or else Full_View (Entity (Arg1)) = Typ
8368 then
8369 Replace_Type_References (Arg2, Typ);
8371 -- If the predicate pragma comes from an aspect, replace the
8372 -- saved expression because we need the subtype references
8373 -- replaced for the calls to Preanalyze_Spec_Expression in
8374 -- Check_Aspect_At_xxx routines.
8376 if Present (Asp) then
8377 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8378 end if;
8380 -- "and"-in the Arg2 condition to evolving expression
8382 Add_Condition (Relocate_Node (Arg2));
8383 end if;
8384 end Add_Predicate;
8386 -- Local variables
8388 Ritem : Node_Id;
8390 -- Start of processing for Add_Predicates
8392 begin
8393 Ritem := First_Rep_Item (Typ);
8394 while Present (Ritem) loop
8395 if Nkind (Ritem) = N_Pragma
8396 and then Pragma_Name (Ritem) = Name_Predicate
8397 then
8398 Add_Predicate (Ritem);
8400 -- If the type is declared in an inner package it may be frozen
8401 -- outside of the package, and the generated pragma has not been
8402 -- analyzed yet, so capture the expression for the predicate
8403 -- function at this point.
8405 elsif Nkind (Ritem) = N_Aspect_Specification
8406 and then Present (Aspect_Rep_Item (Ritem))
8407 and then Scope (Typ) /= Current_Scope
8408 then
8409 declare
8410 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8412 begin
8413 if Nkind (Prag) = N_Pragma
8414 and then Pragma_Name (Prag) = Name_Predicate
8415 then
8416 Add_Predicate (Prag);
8417 end if;
8418 end;
8419 end if;
8421 Next_Rep_Item (Ritem);
8422 end loop;
8423 end Add_Predicates;
8425 ----------------
8426 -- Process_RE --
8427 ----------------
8429 function Process_RE (N : Node_Id) return Traverse_Result is
8430 begin
8431 if Nkind (N) = N_Raise_Expression then
8432 Set_Convert_To_Return_False (N);
8433 return Skip;
8434 else
8435 return OK;
8436 end if;
8437 end Process_RE;
8439 -------------
8440 -- Test_RE --
8441 -------------
8443 function Test_RE (N : Node_Id) return Traverse_Result is
8444 begin
8445 if Nkind (N) = N_Raise_Expression then
8446 Raise_Expression_Present := True;
8447 return Abandon;
8448 else
8449 return OK;
8450 end if;
8451 end Test_RE;
8453 -- Local variables
8455 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8457 -- Start of processing for Build_Predicate_Functions
8459 begin
8460 -- Return if already built or if type does not have predicates
8462 SId := Predicate_Function (Typ);
8463 if not Has_Predicates (Typ)
8464 or else (Present (SId) and then Has_Completion (SId))
8465 then
8466 return;
8467 end if;
8469 -- The related type may be subject to pragma Ghost. Set the mode now to
8470 -- ensure that the predicate functions are properly marked as Ghost.
8472 Set_Ghost_Mode_From_Entity (Typ);
8474 -- Prepare to construct predicate expression
8476 Expr := Empty;
8478 if Present (SId) then
8479 FDecl := Unit_Declaration_Node (SId);
8481 else
8482 FDecl := Build_Predicate_Function_Declaration (Typ);
8483 SId := Defining_Entity (FDecl);
8484 end if;
8486 -- Recover name of formal parameter of function that replaces references
8487 -- to the type in predicate expressions.
8489 Object_Entity :=
8490 Defining_Identifier
8491 (First (Parameter_Specifications (Specification (FDecl))));
8493 Object_Name := Chars (Object_Entity);
8494 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8496 -- Add predicates for ancestor if present. These must come before the
8497 -- ones for the current type, as required by AI12-0071-1.
8499 declare
8500 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8501 begin
8502 if Present (Atyp) then
8503 Add_Call (Atyp);
8504 end if;
8505 end;
8507 -- Add Predicates for the current type
8509 Add_Predicates;
8511 -- Case where predicates are present
8513 if Present (Expr) then
8515 -- Test for raise expression present
8517 Test_REs (Expr);
8519 -- If raise expression is present, capture a copy of Expr for use
8520 -- in building the predicateM function version later on. For this
8521 -- copy we replace references to Object_Entity by Object_Entity_M.
8523 if Raise_Expression_Present then
8524 declare
8525 Map : constant Elist_Id := New_Elmt_List;
8526 New_V : Entity_Id := Empty;
8528 -- The unanalyzed expression will be copied and appear in
8529 -- both functions. Normally expressions do not declare new
8530 -- entities, but quantified expressions do, so we need to
8531 -- create new entities for their bound variables, to prevent
8532 -- multiple definitions in gigi.
8534 function Reset_Loop_Variable (N : Node_Id)
8535 return Traverse_Result;
8537 procedure Collect_Loop_Variables is
8538 new Traverse_Proc (Reset_Loop_Variable);
8540 ------------------------
8541 -- Reset_Loop_Variable --
8542 ------------------------
8544 function Reset_Loop_Variable (N : Node_Id)
8545 return Traverse_Result
8547 begin
8548 if Nkind (N) = N_Iterator_Specification then
8549 New_V := Make_Defining_Identifier
8550 (Sloc (N), Chars (Defining_Identifier (N)));
8552 Set_Defining_Identifier (N, New_V);
8553 end if;
8555 return OK;
8556 end Reset_Loop_Variable;
8558 begin
8559 Append_Elmt (Object_Entity, Map);
8560 Append_Elmt (Object_Entity_M, Map);
8561 Expr_M := New_Copy_Tree (Expr, Map => Map);
8562 Collect_Loop_Variables (Expr_M);
8563 end;
8564 end if;
8566 -- Build the main predicate function
8568 declare
8569 SIdB : constant Entity_Id :=
8570 Make_Defining_Identifier (Loc,
8571 Chars => New_External_Name (Chars (Typ), "Predicate"));
8572 -- The entity for the function body
8574 Spec : Node_Id;
8575 FBody : Node_Id;
8577 begin
8579 -- The predicate function is shared between views of a type
8581 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8582 Set_Predicate_Function (Full_View (Typ), SId);
8583 end if;
8585 -- Mark the predicate function explicitly as Ghost because it does
8586 -- not come from source.
8588 if Ghost_Mode > None then
8589 Set_Is_Ghost_Entity (SId);
8590 end if;
8592 -- Build function body
8594 Spec :=
8595 Make_Function_Specification (Loc,
8596 Defining_Unit_Name => SIdB,
8597 Parameter_Specifications => New_List (
8598 Make_Parameter_Specification (Loc,
8599 Defining_Identifier =>
8600 Make_Defining_Identifier (Loc, Object_Name),
8601 Parameter_Type =>
8602 New_Occurrence_Of (Typ, Loc))),
8603 Result_Definition =>
8604 New_Occurrence_Of (Standard_Boolean, Loc));
8606 FBody :=
8607 Make_Subprogram_Body (Loc,
8608 Specification => Spec,
8609 Declarations => Empty_List,
8610 Handled_Statement_Sequence =>
8611 Make_Handled_Sequence_Of_Statements (Loc,
8612 Statements => New_List (
8613 Make_Simple_Return_Statement (Loc,
8614 Expression => Expr))));
8616 -- If declaration has not been analyzed yet, Insert declaration
8617 -- before freeze node. Insert body itself after freeze node.
8619 if not Analyzed (FDecl) then
8620 Insert_Before_And_Analyze (N, FDecl);
8621 end if;
8623 Insert_After_And_Analyze (N, FBody);
8625 -- Static predicate functions are always side-effect free, and
8626 -- in most cases dynamic predicate functions are as well. Mark
8627 -- them as such whenever possible, so redundant predicate checks
8628 -- can be optimized. If there is a variable reference within the
8629 -- expression, the function is not pure.
8631 if Expander_Active then
8632 Set_Is_Pure (SId,
8633 Side_Effect_Free (Expr, Variable_Ref => True));
8634 Set_Is_Inlined (SId);
8635 end if;
8636 end;
8638 -- Test for raise expressions present and if so build M version
8640 if Raise_Expression_Present then
8641 declare
8642 SId : constant Entity_Id :=
8643 Make_Defining_Identifier (Loc,
8644 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8645 -- The entity for the function spec
8647 SIdB : constant Entity_Id :=
8648 Make_Defining_Identifier (Loc,
8649 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8650 -- The entity for the function body
8652 Spec : Node_Id;
8653 FBody : Node_Id;
8654 FDecl : Node_Id;
8655 BTemp : Entity_Id;
8657 begin
8658 -- Mark any raise expressions for special expansion
8660 Process_REs (Expr_M);
8662 -- Build function declaration
8664 Set_Ekind (SId, E_Function);
8665 Set_Is_Predicate_Function_M (SId);
8666 Set_Predicate_Function_M (Typ, SId);
8668 -- The predicate function is shared between views of a type
8670 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8671 Set_Predicate_Function_M (Full_View (Typ), SId);
8672 end if;
8674 -- Mark the predicate function explicitly as Ghost because it
8675 -- does not come from source.
8677 if Ghost_Mode > None then
8678 Set_Is_Ghost_Entity (SId);
8679 end if;
8681 Spec :=
8682 Make_Function_Specification (Loc,
8683 Defining_Unit_Name => SId,
8684 Parameter_Specifications => New_List (
8685 Make_Parameter_Specification (Loc,
8686 Defining_Identifier => Object_Entity_M,
8687 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8688 Result_Definition =>
8689 New_Occurrence_Of (Standard_Boolean, Loc));
8691 FDecl :=
8692 Make_Subprogram_Declaration (Loc,
8693 Specification => Spec);
8695 -- Build function body
8697 Spec :=
8698 Make_Function_Specification (Loc,
8699 Defining_Unit_Name => SIdB,
8700 Parameter_Specifications => New_List (
8701 Make_Parameter_Specification (Loc,
8702 Defining_Identifier =>
8703 Make_Defining_Identifier (Loc, Object_Name),
8704 Parameter_Type =>
8705 New_Occurrence_Of (Typ, Loc))),
8706 Result_Definition =>
8707 New_Occurrence_Of (Standard_Boolean, Loc));
8709 -- Build the body, we declare the boolean expression before
8710 -- doing the return, because we are not really confident of
8711 -- what happens if a return appears within a return.
8713 BTemp :=
8714 Make_Defining_Identifier (Loc,
8715 Chars => New_Internal_Name ('B'));
8717 FBody :=
8718 Make_Subprogram_Body (Loc,
8719 Specification => Spec,
8721 Declarations => New_List (
8722 Make_Object_Declaration (Loc,
8723 Defining_Identifier => BTemp,
8724 Constant_Present => True,
8725 Object_Definition =>
8726 New_Occurrence_Of (Standard_Boolean, Loc),
8727 Expression => Expr_M)),
8729 Handled_Statement_Sequence =>
8730 Make_Handled_Sequence_Of_Statements (Loc,
8731 Statements => New_List (
8732 Make_Simple_Return_Statement (Loc,
8733 Expression => New_Occurrence_Of (BTemp, Loc)))));
8735 -- Insert declaration before freeze node and body after
8737 Insert_Before_And_Analyze (N, FDecl);
8738 Insert_After_And_Analyze (N, FBody);
8739 end;
8740 end if;
8742 -- See if we have a static predicate. Note that the answer may be
8743 -- yes even if we have an explicit Dynamic_Predicate present.
8745 declare
8746 PS : Boolean;
8747 EN : Node_Id;
8749 begin
8750 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8751 PS := False;
8752 else
8753 PS := Is_Predicate_Static (Expr, Object_Name);
8754 end if;
8756 -- Case where we have a predicate-static aspect
8758 if PS then
8760 -- We don't set Has_Static_Predicate_Aspect, since we can have
8761 -- any of the three cases (Predicate, Dynamic_Predicate, or
8762 -- Static_Predicate) generating a predicate with an expression
8763 -- that is predicate-static. We just indicate that we have a
8764 -- predicate that can be treated as static.
8766 Set_Has_Static_Predicate (Typ);
8768 -- For discrete subtype, build the static predicate list
8770 if Is_Discrete_Type (Typ) then
8771 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8773 -- If we don't get a static predicate list, it means that we
8774 -- have a case where this is not possible, most typically in
8775 -- the case where we inherit a dynamic predicate. We do not
8776 -- consider this an error, we just leave the predicate as
8777 -- dynamic. But if we do succeed in building the list, then
8778 -- we mark the predicate as static.
8780 if No (Static_Discrete_Predicate (Typ)) then
8781 Set_Has_Static_Predicate (Typ, False);
8782 end if;
8784 -- For real or string subtype, save predicate expression
8786 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8787 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8788 end if;
8790 -- Case of dynamic predicate (expression is not predicate-static)
8792 else
8793 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8794 -- is only set if we have an explicit Dynamic_Predicate aspect
8795 -- given. Here we may simply have a Predicate aspect where the
8796 -- expression happens not to be predicate-static.
8798 -- Emit an error when the predicate is categorized as static
8799 -- but its expression is not predicate-static.
8801 -- First a little fiddling to get a nice location for the
8802 -- message. If the expression is of the form (A and then B),
8803 -- where A is an inherited predicate, then use the right
8804 -- operand for the Sloc. This avoids getting confused by a call
8805 -- to an inherited predicate with a less convenient source
8806 -- location.
8808 EN := Expr;
8809 while Nkind (EN) = N_And_Then
8810 and then Nkind (Left_Opnd (EN)) = N_Function_Call
8811 and then Is_Predicate_Function
8812 (Entity (Name (Left_Opnd (EN))))
8813 loop
8814 EN := Right_Opnd (EN);
8815 end loop;
8817 -- Now post appropriate message
8819 if Has_Static_Predicate_Aspect (Typ) then
8820 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8821 Error_Msg_F
8822 ("expression is not predicate-static (RM 3.2.4(16-22))",
8823 EN);
8824 else
8825 Error_Msg_F
8826 ("static predicate requires scalar or string type", EN);
8827 end if;
8828 end if;
8829 end if;
8830 end;
8831 end if;
8833 Ghost_Mode := Save_Ghost_Mode;
8834 end Build_Predicate_Functions;
8836 ------------------------------------------
8837 -- Build_Predicate_Function_Declaration --
8838 ------------------------------------------
8840 function Build_Predicate_Function_Declaration
8841 (Typ : Entity_Id) return Node_Id
8843 Loc : constant Source_Ptr := Sloc (Typ);
8845 Object_Entity : constant Entity_Id :=
8846 Make_Defining_Identifier (Loc,
8847 Chars => New_Internal_Name ('I'));
8849 -- The formal parameter of the function
8851 SId : constant Entity_Id :=
8852 Make_Defining_Identifier (Loc,
8853 Chars => New_External_Name (Chars (Typ), "Predicate"));
8855 -- The entity for the function spec
8857 FDecl : Node_Id;
8858 Spec : Node_Id;
8860 begin
8861 Spec :=
8862 Make_Function_Specification (Loc,
8863 Defining_Unit_Name => SId,
8864 Parameter_Specifications => New_List (
8865 Make_Parameter_Specification (Loc,
8866 Defining_Identifier => Object_Entity,
8867 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8868 Result_Definition =>
8869 New_Occurrence_Of (Standard_Boolean, Loc));
8871 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
8873 Set_Ekind (SId, E_Function);
8874 Set_Etype (SId, Standard_Boolean);
8875 Set_Is_Internal (SId);
8876 Set_Is_Predicate_Function (SId);
8877 Set_Predicate_Function (Typ, SId);
8879 Insert_After (Parent (Typ), FDecl);
8881 Analyze (FDecl);
8883 return FDecl;
8884 end Build_Predicate_Function_Declaration;
8886 -----------------------------------------
8887 -- Check_Aspect_At_End_Of_Declarations --
8888 -----------------------------------------
8890 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8891 Ent : constant Entity_Id := Entity (ASN);
8892 Ident : constant Node_Id := Identifier (ASN);
8893 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8895 End_Decl_Expr : constant Node_Id := Entity (Ident);
8896 -- Expression to be analyzed at end of declarations
8898 Freeze_Expr : constant Node_Id := Expression (ASN);
8899 -- Expression from call to Check_Aspect_At_Freeze_Point
8901 T : constant Entity_Id := Etype (Freeze_Expr);
8902 -- Type required for preanalyze call
8904 Err : Boolean;
8905 -- Set False if error
8907 -- On entry to this procedure, Entity (Ident) contains a copy of the
8908 -- original expression from the aspect, saved for this purpose, and
8909 -- but Expression (Ident) is a preanalyzed copy of the expression,
8910 -- preanalyzed just after the freeze point.
8912 procedure Check_Overloaded_Name;
8913 -- For aspects whose expression is simply a name, this routine checks if
8914 -- the name is overloaded or not. If so, it verifies there is an
8915 -- interpretation that matches the entity obtained at the freeze point,
8916 -- otherwise the compiler complains.
8918 ---------------------------
8919 -- Check_Overloaded_Name --
8920 ---------------------------
8922 procedure Check_Overloaded_Name is
8923 begin
8924 if not Is_Overloaded (End_Decl_Expr) then
8925 Err := not Is_Entity_Name (End_Decl_Expr)
8926 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8928 else
8929 Err := True;
8931 declare
8932 Index : Interp_Index;
8933 It : Interp;
8935 begin
8936 Get_First_Interp (End_Decl_Expr, Index, It);
8937 while Present (It.Typ) loop
8938 if It.Nam = Entity (Freeze_Expr) then
8939 Err := False;
8940 exit;
8941 end if;
8943 Get_Next_Interp (Index, It);
8944 end loop;
8945 end;
8946 end if;
8947 end Check_Overloaded_Name;
8949 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8951 begin
8952 -- In an instance we do not perform the consistency check between freeze
8953 -- point and end of declarations, because it was done already in the
8954 -- analysis of the generic. Furthermore, the delayed analysis of an
8955 -- aspect of the instance may produce spurious errors when the generic
8956 -- is a child unit that references entities in the parent (which might
8957 -- not be in scope at the freeze point of the instance).
8959 if In_Instance then
8960 return;
8962 -- Case of aspects Dimension, Dimension_System and Synchronization
8964 elsif A_Id = Aspect_Synchronization then
8965 return;
8967 -- Case of stream attributes, just have to compare entities. However,
8968 -- the expression is just a name (possibly overloaded), and there may
8969 -- be stream operations declared for unrelated types, so we just need
8970 -- to verify that one of these interpretations is the one available at
8971 -- at the freeze point.
8973 elsif A_Id = Aspect_Input or else
8974 A_Id = Aspect_Output or else
8975 A_Id = Aspect_Read or else
8976 A_Id = Aspect_Write
8977 then
8978 Analyze (End_Decl_Expr);
8979 Check_Overloaded_Name;
8981 elsif A_Id = Aspect_Variable_Indexing or else
8982 A_Id = Aspect_Constant_Indexing or else
8983 A_Id = Aspect_Default_Iterator or else
8984 A_Id = Aspect_Iterator_Element
8985 then
8986 -- Make type unfrozen before analysis, to prevent spurious errors
8987 -- about late attributes.
8989 Set_Is_Frozen (Ent, False);
8990 Analyze (End_Decl_Expr);
8991 Set_Is_Frozen (Ent, True);
8993 -- If the end of declarations comes before any other freeze
8994 -- point, the Freeze_Expr is not analyzed: no check needed.
8996 if Analyzed (Freeze_Expr) and then not In_Instance then
8997 Check_Overloaded_Name;
8998 else
8999 Err := False;
9000 end if;
9002 -- All other cases
9004 else
9005 -- Indicate that the expression comes from an aspect specification,
9006 -- which is used in subsequent analysis even if expansion is off.
9008 Set_Parent (End_Decl_Expr, ASN);
9010 -- In a generic context the aspect expressions have not been
9011 -- preanalyzed, so do it now. There are no conformance checks
9012 -- to perform in this case.
9014 if No (T) then
9015 Check_Aspect_At_Freeze_Point (ASN);
9016 return;
9018 -- The default values attributes may be defined in the private part,
9019 -- and the analysis of the expression may take place when only the
9020 -- partial view is visible. The expression must be scalar, so use
9021 -- the full view to resolve.
9023 elsif (A_Id = Aspect_Default_Value
9024 or else
9025 A_Id = Aspect_Default_Component_Value)
9026 and then Is_Private_Type (T)
9027 then
9028 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9030 else
9031 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9032 end if;
9034 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9035 end if;
9037 -- Output error message if error. Force error on aspect specification
9038 -- even if there is an error on the expression itself.
9040 if Err then
9041 Error_Msg_NE
9042 ("!visibility of aspect for& changes after freeze point",
9043 ASN, Ent);
9044 Error_Msg_NE
9045 ("info: & is frozen here, aspects evaluated at this point??",
9046 Freeze_Node (Ent), Ent);
9047 end if;
9048 end Check_Aspect_At_End_Of_Declarations;
9050 ----------------------------------
9051 -- Check_Aspect_At_Freeze_Point --
9052 ----------------------------------
9054 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9055 Ident : constant Node_Id := Identifier (ASN);
9056 -- Identifier (use Entity field to save expression)
9058 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9060 T : Entity_Id := Empty;
9061 -- Type required for preanalyze call
9063 begin
9064 -- On entry to this procedure, Entity (Ident) contains a copy of the
9065 -- original expression from the aspect, saved for this purpose.
9067 -- On exit from this procedure Entity (Ident) is unchanged, still
9068 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9069 -- of the expression, preanalyzed just after the freeze point.
9071 -- Make a copy of the expression to be preanalyzed
9073 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9075 -- Find type for preanalyze call
9077 case A_Id is
9079 -- No_Aspect should be impossible
9081 when No_Aspect =>
9082 raise Program_Error;
9084 -- Aspects taking an optional boolean argument
9086 when Boolean_Aspects |
9087 Library_Unit_Aspects =>
9089 T := Standard_Boolean;
9091 -- Aspects corresponding to attribute definition clauses
9093 when Aspect_Address =>
9094 T := RTE (RE_Address);
9096 when Aspect_Attach_Handler =>
9097 T := RTE (RE_Interrupt_ID);
9099 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9100 T := RTE (RE_Bit_Order);
9102 when Aspect_Convention =>
9103 return;
9105 when Aspect_CPU =>
9106 T := RTE (RE_CPU_Range);
9108 -- Default_Component_Value is resolved with the component type
9110 when Aspect_Default_Component_Value =>
9111 T := Component_Type (Entity (ASN));
9113 when Aspect_Default_Storage_Pool =>
9114 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9116 -- Default_Value is resolved with the type entity in question
9118 when Aspect_Default_Value =>
9119 T := Entity (ASN);
9121 when Aspect_Dispatching_Domain =>
9122 T := RTE (RE_Dispatching_Domain);
9124 when Aspect_External_Tag =>
9125 T := Standard_String;
9127 when Aspect_External_Name =>
9128 T := Standard_String;
9130 when Aspect_Link_Name =>
9131 T := Standard_String;
9133 when Aspect_Priority | Aspect_Interrupt_Priority =>
9134 T := Standard_Integer;
9136 when Aspect_Relative_Deadline =>
9137 T := RTE (RE_Time_Span);
9139 when Aspect_Small =>
9140 T := Universal_Real;
9142 -- For a simple storage pool, we have to retrieve the type of the
9143 -- pool object associated with the aspect's corresponding attribute
9144 -- definition clause.
9146 when Aspect_Simple_Storage_Pool =>
9147 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9149 when Aspect_Storage_Pool =>
9150 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9152 when Aspect_Alignment |
9153 Aspect_Component_Size |
9154 Aspect_Machine_Radix |
9155 Aspect_Object_Size |
9156 Aspect_Size |
9157 Aspect_Storage_Size |
9158 Aspect_Stream_Size |
9159 Aspect_Value_Size =>
9160 T := Any_Integer;
9162 when Aspect_Linker_Section =>
9163 T := Standard_String;
9165 when Aspect_Synchronization =>
9166 return;
9168 -- Special case, the expression of these aspects is just an entity
9169 -- that does not need any resolution, so just analyze.
9171 when Aspect_Input |
9172 Aspect_Output |
9173 Aspect_Read |
9174 Aspect_Suppress |
9175 Aspect_Unsuppress |
9176 Aspect_Warnings |
9177 Aspect_Write =>
9178 Analyze (Expression (ASN));
9179 return;
9181 -- Same for Iterator aspects, where the expression is a function
9182 -- name. Legality rules are checked separately.
9184 when Aspect_Constant_Indexing |
9185 Aspect_Default_Iterator |
9186 Aspect_Iterator_Element |
9187 Aspect_Variable_Indexing =>
9188 Analyze (Expression (ASN));
9189 return;
9191 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9193 when Aspect_Iterable =>
9194 T := Entity (ASN);
9196 declare
9197 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9198 Assoc : Node_Id;
9199 Expr : Node_Id;
9201 begin
9202 if Cursor = Any_Type then
9203 return;
9204 end if;
9206 Assoc := First (Component_Associations (Expression (ASN)));
9207 while Present (Assoc) loop
9208 Expr := Expression (Assoc);
9209 Analyze (Expr);
9211 if not Error_Posted (Expr) then
9212 Resolve_Iterable_Operation
9213 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9214 end if;
9216 Next (Assoc);
9217 end loop;
9218 end;
9220 return;
9222 -- Invariant/Predicate take boolean expressions
9224 when Aspect_Dynamic_Predicate |
9225 Aspect_Invariant |
9226 Aspect_Predicate |
9227 Aspect_Static_Predicate |
9228 Aspect_Type_Invariant =>
9229 T := Standard_Boolean;
9231 when Aspect_Predicate_Failure =>
9232 T := Standard_String;
9234 -- Here is the list of aspects that don't require delay analysis
9236 when Aspect_Abstract_State |
9237 Aspect_Annotate |
9238 Aspect_Async_Readers |
9239 Aspect_Async_Writers |
9240 Aspect_Constant_After_Elaboration |
9241 Aspect_Contract_Cases |
9242 Aspect_Default_Initial_Condition |
9243 Aspect_Depends |
9244 Aspect_Dimension |
9245 Aspect_Dimension_System |
9246 Aspect_Effective_Reads |
9247 Aspect_Effective_Writes |
9248 Aspect_Extensions_Visible |
9249 Aspect_Ghost |
9250 Aspect_Global |
9251 Aspect_Implicit_Dereference |
9252 Aspect_Initial_Condition |
9253 Aspect_Initializes |
9254 Aspect_Obsolescent |
9255 Aspect_Part_Of |
9256 Aspect_Post |
9257 Aspect_Postcondition |
9258 Aspect_Pre |
9259 Aspect_Precondition |
9260 Aspect_Refined_Depends |
9261 Aspect_Refined_Global |
9262 Aspect_Refined_Post |
9263 Aspect_Refined_State |
9264 Aspect_SPARK_Mode |
9265 Aspect_Test_Case |
9266 Aspect_Unimplemented |
9267 Aspect_Volatile_Function =>
9268 raise Program_Error;
9270 end case;
9272 -- Do the preanalyze call
9274 Preanalyze_Spec_Expression (Expression (ASN), T);
9275 end Check_Aspect_At_Freeze_Point;
9277 -----------------------------------
9278 -- Check_Constant_Address_Clause --
9279 -----------------------------------
9281 procedure Check_Constant_Address_Clause
9282 (Expr : Node_Id;
9283 U_Ent : Entity_Id)
9285 procedure Check_At_Constant_Address (Nod : Node_Id);
9286 -- Checks that the given node N represents a name whose 'Address is
9287 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9288 -- address value is the same at the point of declaration of U_Ent and at
9289 -- the time of elaboration of the address clause.
9291 procedure Check_Expr_Constants (Nod : Node_Id);
9292 -- Checks that Nod meets the requirements for a constant address clause
9293 -- in the sense of the enclosing procedure.
9295 procedure Check_List_Constants (Lst : List_Id);
9296 -- Check that all elements of list Lst meet the requirements for a
9297 -- constant address clause in the sense of the enclosing procedure.
9299 -------------------------------
9300 -- Check_At_Constant_Address --
9301 -------------------------------
9303 procedure Check_At_Constant_Address (Nod : Node_Id) is
9304 begin
9305 if Is_Entity_Name (Nod) then
9306 if Present (Address_Clause (Entity ((Nod)))) then
9307 Error_Msg_NE
9308 ("invalid address clause for initialized object &!",
9309 Nod, U_Ent);
9310 Error_Msg_NE
9311 ("address for& cannot" &
9312 " depend on another address clause! (RM 13.1(22))!",
9313 Nod, U_Ent);
9315 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9316 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9317 then
9318 Error_Msg_NE
9319 ("invalid address clause for initialized object &!",
9320 Nod, U_Ent);
9321 Error_Msg_Node_2 := U_Ent;
9322 Error_Msg_NE
9323 ("\& must be defined before & (RM 13.1(22))!",
9324 Nod, Entity (Nod));
9325 end if;
9327 elsif Nkind (Nod) = N_Selected_Component then
9328 declare
9329 T : constant Entity_Id := Etype (Prefix (Nod));
9331 begin
9332 if (Is_Record_Type (T)
9333 and then Has_Discriminants (T))
9334 or else
9335 (Is_Access_Type (T)
9336 and then Is_Record_Type (Designated_Type (T))
9337 and then Has_Discriminants (Designated_Type (T)))
9338 then
9339 Error_Msg_NE
9340 ("invalid address clause for initialized object &!",
9341 Nod, U_Ent);
9342 Error_Msg_N
9343 ("\address cannot depend on component" &
9344 " of discriminated record (RM 13.1(22))!",
9345 Nod);
9346 else
9347 Check_At_Constant_Address (Prefix (Nod));
9348 end if;
9349 end;
9351 elsif Nkind (Nod) = N_Indexed_Component then
9352 Check_At_Constant_Address (Prefix (Nod));
9353 Check_List_Constants (Expressions (Nod));
9355 else
9356 Check_Expr_Constants (Nod);
9357 end if;
9358 end Check_At_Constant_Address;
9360 --------------------------
9361 -- Check_Expr_Constants --
9362 --------------------------
9364 procedure Check_Expr_Constants (Nod : Node_Id) is
9365 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9366 Ent : Entity_Id := Empty;
9368 begin
9369 if Nkind (Nod) in N_Has_Etype
9370 and then Etype (Nod) = Any_Type
9371 then
9372 return;
9373 end if;
9375 case Nkind (Nod) is
9376 when N_Empty | N_Error =>
9377 return;
9379 when N_Identifier | N_Expanded_Name =>
9380 Ent := Entity (Nod);
9382 -- We need to look at the original node if it is different
9383 -- from the node, since we may have rewritten things and
9384 -- substituted an identifier representing the rewrite.
9386 if Original_Node (Nod) /= Nod then
9387 Check_Expr_Constants (Original_Node (Nod));
9389 -- If the node is an object declaration without initial
9390 -- value, some code has been expanded, and the expression
9391 -- is not constant, even if the constituents might be
9392 -- acceptable, as in A'Address + offset.
9394 if Ekind (Ent) = E_Variable
9395 and then
9396 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9397 and then
9398 No (Expression (Declaration_Node (Ent)))
9399 then
9400 Error_Msg_NE
9401 ("invalid address clause for initialized object &!",
9402 Nod, U_Ent);
9404 -- If entity is constant, it may be the result of expanding
9405 -- a check. We must verify that its declaration appears
9406 -- before the object in question, else we also reject the
9407 -- address clause.
9409 elsif Ekind (Ent) = E_Constant
9410 and then In_Same_Source_Unit (Ent, U_Ent)
9411 and then Sloc (Ent) > Loc_U_Ent
9412 then
9413 Error_Msg_NE
9414 ("invalid address clause for initialized object &!",
9415 Nod, U_Ent);
9416 end if;
9418 return;
9419 end if;
9421 -- Otherwise look at the identifier and see if it is OK
9423 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9424 or else Is_Type (Ent)
9425 then
9426 return;
9428 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9430 -- This is the case where we must have Ent defined before
9431 -- U_Ent. Clearly if they are in different units this
9432 -- requirement is met since the unit containing Ent is
9433 -- already processed.
9435 if not In_Same_Source_Unit (Ent, U_Ent) then
9436 return;
9438 -- Otherwise location of Ent must be before the location
9439 -- of U_Ent, that's what prior defined means.
9441 elsif Sloc (Ent) < Loc_U_Ent then
9442 return;
9444 else
9445 Error_Msg_NE
9446 ("invalid address clause for initialized object &!",
9447 Nod, U_Ent);
9448 Error_Msg_Node_2 := U_Ent;
9449 Error_Msg_NE
9450 ("\& must be defined before & (RM 13.1(22))!",
9451 Nod, Ent);
9452 end if;
9454 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9455 Check_Expr_Constants (Original_Node (Nod));
9457 else
9458 Error_Msg_NE
9459 ("invalid address clause for initialized object &!",
9460 Nod, U_Ent);
9462 if Comes_From_Source (Ent) then
9463 Error_Msg_NE
9464 ("\reference to variable& not allowed"
9465 & " (RM 13.1(22))!", Nod, Ent);
9466 else
9467 Error_Msg_N
9468 ("non-static expression not allowed"
9469 & " (RM 13.1(22))!", Nod);
9470 end if;
9471 end if;
9473 when N_Integer_Literal =>
9475 -- If this is a rewritten unchecked conversion, in a system
9476 -- where Address is an integer type, always use the base type
9477 -- for a literal value. This is user-friendly and prevents
9478 -- order-of-elaboration issues with instances of unchecked
9479 -- conversion.
9481 if Nkind (Original_Node (Nod)) = N_Function_Call then
9482 Set_Etype (Nod, Base_Type (Etype (Nod)));
9483 end if;
9485 when N_Real_Literal |
9486 N_String_Literal |
9487 N_Character_Literal =>
9488 return;
9490 when N_Range =>
9491 Check_Expr_Constants (Low_Bound (Nod));
9492 Check_Expr_Constants (High_Bound (Nod));
9494 when N_Explicit_Dereference =>
9495 Check_Expr_Constants (Prefix (Nod));
9497 when N_Indexed_Component =>
9498 Check_Expr_Constants (Prefix (Nod));
9499 Check_List_Constants (Expressions (Nod));
9501 when N_Slice =>
9502 Check_Expr_Constants (Prefix (Nod));
9503 Check_Expr_Constants (Discrete_Range (Nod));
9505 when N_Selected_Component =>
9506 Check_Expr_Constants (Prefix (Nod));
9508 when N_Attribute_Reference =>
9509 if Nam_In (Attribute_Name (Nod), Name_Address,
9510 Name_Access,
9511 Name_Unchecked_Access,
9512 Name_Unrestricted_Access)
9513 then
9514 Check_At_Constant_Address (Prefix (Nod));
9516 else
9517 Check_Expr_Constants (Prefix (Nod));
9518 Check_List_Constants (Expressions (Nod));
9519 end if;
9521 when N_Aggregate =>
9522 Check_List_Constants (Component_Associations (Nod));
9523 Check_List_Constants (Expressions (Nod));
9525 when N_Component_Association =>
9526 Check_Expr_Constants (Expression (Nod));
9528 when N_Extension_Aggregate =>
9529 Check_Expr_Constants (Ancestor_Part (Nod));
9530 Check_List_Constants (Component_Associations (Nod));
9531 Check_List_Constants (Expressions (Nod));
9533 when N_Null =>
9534 return;
9536 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9537 Check_Expr_Constants (Left_Opnd (Nod));
9538 Check_Expr_Constants (Right_Opnd (Nod));
9540 when N_Unary_Op =>
9541 Check_Expr_Constants (Right_Opnd (Nod));
9543 when N_Type_Conversion |
9544 N_Qualified_Expression |
9545 N_Allocator |
9546 N_Unchecked_Type_Conversion =>
9547 Check_Expr_Constants (Expression (Nod));
9549 when N_Function_Call =>
9550 if not Is_Pure (Entity (Name (Nod))) then
9551 Error_Msg_NE
9552 ("invalid address clause for initialized object &!",
9553 Nod, U_Ent);
9555 Error_Msg_NE
9556 ("\function & is not pure (RM 13.1(22))!",
9557 Nod, Entity (Name (Nod)));
9559 else
9560 Check_List_Constants (Parameter_Associations (Nod));
9561 end if;
9563 when N_Parameter_Association =>
9564 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9566 when others =>
9567 Error_Msg_NE
9568 ("invalid address clause for initialized object &!",
9569 Nod, U_Ent);
9570 Error_Msg_NE
9571 ("\must be constant defined before& (RM 13.1(22))!",
9572 Nod, U_Ent);
9573 end case;
9574 end Check_Expr_Constants;
9576 --------------------------
9577 -- Check_List_Constants --
9578 --------------------------
9580 procedure Check_List_Constants (Lst : List_Id) is
9581 Nod1 : Node_Id;
9583 begin
9584 if Present (Lst) then
9585 Nod1 := First (Lst);
9586 while Present (Nod1) loop
9587 Check_Expr_Constants (Nod1);
9588 Next (Nod1);
9589 end loop;
9590 end if;
9591 end Check_List_Constants;
9593 -- Start of processing for Check_Constant_Address_Clause
9595 begin
9596 -- If rep_clauses are to be ignored, no need for legality checks. In
9597 -- particular, no need to pester user about rep clauses that violate the
9598 -- rule on constant addresses, given that these clauses will be removed
9599 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9600 -- we want to relax these checks.
9602 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9603 Check_Expr_Constants (Expr);
9604 end if;
9605 end Check_Constant_Address_Clause;
9607 ---------------------------
9608 -- Check_Pool_Size_Clash --
9609 ---------------------------
9611 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9612 Post : Node_Id;
9614 begin
9615 -- We need to find out which one came first. Note that in the case of
9616 -- aspects mixed with pragmas there are cases where the processing order
9617 -- is reversed, which is why we do the check here.
9619 if Sloc (SP) < Sloc (SS) then
9620 Error_Msg_Sloc := Sloc (SP);
9621 Post := SS;
9622 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9624 else
9625 Error_Msg_Sloc := Sloc (SS);
9626 Post := SP;
9627 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9628 end if;
9630 Error_Msg_N
9631 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9632 end Check_Pool_Size_Clash;
9634 ----------------------------------------
9635 -- Check_Record_Representation_Clause --
9636 ----------------------------------------
9638 procedure Check_Record_Representation_Clause (N : Node_Id) is
9639 Loc : constant Source_Ptr := Sloc (N);
9640 Ident : constant Node_Id := Identifier (N);
9641 Rectype : Entity_Id;
9642 Fent : Entity_Id;
9643 CC : Node_Id;
9644 Fbit : Uint;
9645 Lbit : Uint;
9646 Hbit : Uint := Uint_0;
9647 Comp : Entity_Id;
9648 Pcomp : Entity_Id;
9650 Max_Bit_So_Far : Uint;
9651 -- Records the maximum bit position so far. If all field positions
9652 -- are monotonically increasing, then we can skip the circuit for
9653 -- checking for overlap, since no overlap is possible.
9655 Tagged_Parent : Entity_Id := Empty;
9656 -- This is set in the case of a derived tagged type for which we have
9657 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9658 -- positioned by record representation clauses). In this case we must
9659 -- check for overlap between components of this tagged type, and the
9660 -- components of its parent. Tagged_Parent will point to this parent
9661 -- type. For all other cases Tagged_Parent is left set to Empty.
9663 Parent_Last_Bit : Uint;
9664 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9665 -- last bit position for any field in the parent type. We only need to
9666 -- check overlap for fields starting below this point.
9668 Overlap_Check_Required : Boolean;
9669 -- Used to keep track of whether or not an overlap check is required
9671 Overlap_Detected : Boolean := False;
9672 -- Set True if an overlap is detected
9674 Ccount : Natural := 0;
9675 -- Number of component clauses in record rep clause
9677 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9678 -- Given two entities for record components or discriminants, checks
9679 -- if they have overlapping component clauses and issues errors if so.
9681 procedure Find_Component;
9682 -- Finds component entity corresponding to current component clause (in
9683 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9684 -- start/stop bits for the field. If there is no matching component or
9685 -- if the matching component does not have a component clause, then
9686 -- that's an error and Comp is set to Empty, but no error message is
9687 -- issued, since the message was already given. Comp is also set to
9688 -- Empty if the current "component clause" is in fact a pragma.
9690 -----------------------------
9691 -- Check_Component_Overlap --
9692 -----------------------------
9694 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9695 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9696 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9698 begin
9699 if Present (CC1) and then Present (CC2) then
9701 -- Exclude odd case where we have two tag components in the same
9702 -- record, both at location zero. This seems a bit strange, but
9703 -- it seems to happen in some circumstances, perhaps on an error.
9705 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9706 return;
9707 end if;
9709 -- Here we check if the two fields overlap
9711 declare
9712 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9713 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9714 E1 : constant Uint := S1 + Esize (C1_Ent);
9715 E2 : constant Uint := S2 + Esize (C2_Ent);
9717 begin
9718 if E2 <= S1 or else E1 <= S2 then
9719 null;
9720 else
9721 Error_Msg_Node_2 := Component_Name (CC2);
9722 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9723 Error_Msg_Node_1 := Component_Name (CC1);
9724 Error_Msg_N
9725 ("component& overlaps & #", Component_Name (CC1));
9726 Overlap_Detected := True;
9727 end if;
9728 end;
9729 end if;
9730 end Check_Component_Overlap;
9732 --------------------
9733 -- Find_Component --
9734 --------------------
9736 procedure Find_Component is
9738 procedure Search_Component (R : Entity_Id);
9739 -- Search components of R for a match. If found, Comp is set
9741 ----------------------
9742 -- Search_Component --
9743 ----------------------
9745 procedure Search_Component (R : Entity_Id) is
9746 begin
9747 Comp := First_Component_Or_Discriminant (R);
9748 while Present (Comp) loop
9750 -- Ignore error of attribute name for component name (we
9751 -- already gave an error message for this, so no need to
9752 -- complain here)
9754 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9755 null;
9756 else
9757 exit when Chars (Comp) = Chars (Component_Name (CC));
9758 end if;
9760 Next_Component_Or_Discriminant (Comp);
9761 end loop;
9762 end Search_Component;
9764 -- Start of processing for Find_Component
9766 begin
9767 -- Return with Comp set to Empty if we have a pragma
9769 if Nkind (CC) = N_Pragma then
9770 Comp := Empty;
9771 return;
9772 end if;
9774 -- Search current record for matching component
9776 Search_Component (Rectype);
9778 -- If not found, maybe component of base type discriminant that is
9779 -- absent from statically constrained first subtype.
9781 if No (Comp) then
9782 Search_Component (Base_Type (Rectype));
9783 end if;
9785 -- If no component, or the component does not reference the component
9786 -- clause in question, then there was some previous error for which
9787 -- we already gave a message, so just return with Comp Empty.
9789 if No (Comp) or else Component_Clause (Comp) /= CC then
9790 Check_Error_Detected;
9791 Comp := Empty;
9793 -- Normal case where we have a component clause
9795 else
9796 Fbit := Component_Bit_Offset (Comp);
9797 Lbit := Fbit + Esize (Comp) - 1;
9798 end if;
9799 end Find_Component;
9801 -- Start of processing for Check_Record_Representation_Clause
9803 begin
9804 Find_Type (Ident);
9805 Rectype := Entity (Ident);
9807 if Rectype = Any_Type then
9808 return;
9809 else
9810 Rectype := Underlying_Type (Rectype);
9811 end if;
9813 -- See if we have a fully repped derived tagged type
9815 declare
9816 PS : constant Entity_Id := Parent_Subtype (Rectype);
9818 begin
9819 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9820 Tagged_Parent := PS;
9822 -- Find maximum bit of any component of the parent type
9824 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9825 Pcomp := First_Entity (Tagged_Parent);
9826 while Present (Pcomp) loop
9827 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9828 if Component_Bit_Offset (Pcomp) /= No_Uint
9829 and then Known_Static_Esize (Pcomp)
9830 then
9831 Parent_Last_Bit :=
9832 UI_Max
9833 (Parent_Last_Bit,
9834 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9835 end if;
9836 else
9838 -- Skip anonymous types generated for constrained array
9839 -- or record components.
9841 null;
9842 end if;
9844 Next_Entity (Pcomp);
9845 end loop;
9846 end if;
9847 end;
9849 -- All done if no component clauses
9851 CC := First (Component_Clauses (N));
9853 if No (CC) then
9854 return;
9855 end if;
9857 -- If a tag is present, then create a component clause that places it
9858 -- at the start of the record (otherwise gigi may place it after other
9859 -- fields that have rep clauses).
9861 Fent := First_Entity (Rectype);
9863 if Nkind (Fent) = N_Defining_Identifier
9864 and then Chars (Fent) = Name_uTag
9865 then
9866 Set_Component_Bit_Offset (Fent, Uint_0);
9867 Set_Normalized_Position (Fent, Uint_0);
9868 Set_Normalized_First_Bit (Fent, Uint_0);
9869 Set_Normalized_Position_Max (Fent, Uint_0);
9870 Init_Esize (Fent, System_Address_Size);
9872 Set_Component_Clause (Fent,
9873 Make_Component_Clause (Loc,
9874 Component_Name => Make_Identifier (Loc, Name_uTag),
9876 Position => Make_Integer_Literal (Loc, Uint_0),
9877 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9878 Last_Bit =>
9879 Make_Integer_Literal (Loc,
9880 UI_From_Int (System_Address_Size))));
9882 Ccount := Ccount + 1;
9883 end if;
9885 Max_Bit_So_Far := Uint_Minus_1;
9886 Overlap_Check_Required := False;
9888 -- Process the component clauses
9890 while Present (CC) loop
9891 Find_Component;
9893 if Present (Comp) then
9894 Ccount := Ccount + 1;
9896 -- We need a full overlap check if record positions non-monotonic
9898 if Fbit <= Max_Bit_So_Far then
9899 Overlap_Check_Required := True;
9900 end if;
9902 Max_Bit_So_Far := Lbit;
9904 -- Check bit position out of range of specified size
9906 if Has_Size_Clause (Rectype)
9907 and then RM_Size (Rectype) <= Lbit
9908 then
9909 Error_Msg_N
9910 ("bit number out of range of specified size",
9911 Last_Bit (CC));
9913 -- Check for overlap with tag component
9915 else
9916 if Is_Tagged_Type (Rectype)
9917 and then Fbit < System_Address_Size
9918 then
9919 Error_Msg_NE
9920 ("component overlaps tag field of&",
9921 Component_Name (CC), Rectype);
9922 Overlap_Detected := True;
9923 end if;
9925 if Hbit < Lbit then
9926 Hbit := Lbit;
9927 end if;
9928 end if;
9930 -- Check parent overlap if component might overlap parent field
9932 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9933 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9934 while Present (Pcomp) loop
9935 if not Is_Tag (Pcomp)
9936 and then Chars (Pcomp) /= Name_uParent
9937 then
9938 Check_Component_Overlap (Comp, Pcomp);
9939 end if;
9941 Next_Component_Or_Discriminant (Pcomp);
9942 end loop;
9943 end if;
9944 end if;
9946 Next (CC);
9947 end loop;
9949 -- Now that we have processed all the component clauses, check for
9950 -- overlap. We have to leave this till last, since the components can
9951 -- appear in any arbitrary order in the representation clause.
9953 -- We do not need this check if all specified ranges were monotonic,
9954 -- as recorded by Overlap_Check_Required being False at this stage.
9956 -- This first section checks if there are any overlapping entries at
9957 -- all. It does this by sorting all entries and then seeing if there are
9958 -- any overlaps. If there are none, then that is decisive, but if there
9959 -- are overlaps, they may still be OK (they may result from fields in
9960 -- different variants).
9962 if Overlap_Check_Required then
9963 Overlap_Check1 : declare
9965 OC_Fbit : array (0 .. Ccount) of Uint;
9966 -- First-bit values for component clauses, the value is the offset
9967 -- of the first bit of the field from start of record. The zero
9968 -- entry is for use in sorting.
9970 OC_Lbit : array (0 .. Ccount) of Uint;
9971 -- Last-bit values for component clauses, the value is the offset
9972 -- of the last bit of the field from start of record. The zero
9973 -- entry is for use in sorting.
9975 OC_Count : Natural := 0;
9976 -- Count of entries in OC_Fbit and OC_Lbit
9978 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9979 -- Compare routine for Sort
9981 procedure OC_Move (From : Natural; To : Natural);
9982 -- Move routine for Sort
9984 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9986 -----------
9987 -- OC_Lt --
9988 -----------
9990 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9991 begin
9992 return OC_Fbit (Op1) < OC_Fbit (Op2);
9993 end OC_Lt;
9995 -------------
9996 -- OC_Move --
9997 -------------
9999 procedure OC_Move (From : Natural; To : Natural) is
10000 begin
10001 OC_Fbit (To) := OC_Fbit (From);
10002 OC_Lbit (To) := OC_Lbit (From);
10003 end OC_Move;
10005 -- Start of processing for Overlap_Check
10007 begin
10008 CC := First (Component_Clauses (N));
10009 while Present (CC) loop
10011 -- Exclude component clause already marked in error
10013 if not Error_Posted (CC) then
10014 Find_Component;
10016 if Present (Comp) then
10017 OC_Count := OC_Count + 1;
10018 OC_Fbit (OC_Count) := Fbit;
10019 OC_Lbit (OC_Count) := Lbit;
10020 end if;
10021 end if;
10023 Next (CC);
10024 end loop;
10026 Sorting.Sort (OC_Count);
10028 Overlap_Check_Required := False;
10029 for J in 1 .. OC_Count - 1 loop
10030 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10031 Overlap_Check_Required := True;
10032 exit;
10033 end if;
10034 end loop;
10035 end Overlap_Check1;
10036 end if;
10038 -- If Overlap_Check_Required is still True, then we have to do the full
10039 -- scale overlap check, since we have at least two fields that do
10040 -- overlap, and we need to know if that is OK since they are in
10041 -- different variant, or whether we have a definite problem.
10043 if Overlap_Check_Required then
10044 Overlap_Check2 : declare
10045 C1_Ent, C2_Ent : Entity_Id;
10046 -- Entities of components being checked for overlap
10048 Clist : Node_Id;
10049 -- Component_List node whose Component_Items are being checked
10051 Citem : Node_Id;
10052 -- Component declaration for component being checked
10054 begin
10055 C1_Ent := First_Entity (Base_Type (Rectype));
10057 -- Loop through all components in record. For each component check
10058 -- for overlap with any of the preceding elements on the component
10059 -- list containing the component and also, if the component is in
10060 -- a variant, check against components outside the case structure.
10061 -- This latter test is repeated recursively up the variant tree.
10063 Main_Component_Loop : while Present (C1_Ent) loop
10064 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10065 goto Continue_Main_Component_Loop;
10066 end if;
10068 -- Skip overlap check if entity has no declaration node. This
10069 -- happens with discriminants in constrained derived types.
10070 -- Possibly we are missing some checks as a result, but that
10071 -- does not seem terribly serious.
10073 if No (Declaration_Node (C1_Ent)) then
10074 goto Continue_Main_Component_Loop;
10075 end if;
10077 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10079 -- Loop through component lists that need checking. Check the
10080 -- current component list and all lists in variants above us.
10082 Component_List_Loop : loop
10084 -- If derived type definition, go to full declaration
10085 -- If at outer level, check discriminants if there are any.
10087 if Nkind (Clist) = N_Derived_Type_Definition then
10088 Clist := Parent (Clist);
10089 end if;
10091 -- Outer level of record definition, check discriminants
10093 if Nkind_In (Clist, N_Full_Type_Declaration,
10094 N_Private_Type_Declaration)
10095 then
10096 if Has_Discriminants (Defining_Identifier (Clist)) then
10097 C2_Ent :=
10098 First_Discriminant (Defining_Identifier (Clist));
10099 while Present (C2_Ent) loop
10100 exit when C1_Ent = C2_Ent;
10101 Check_Component_Overlap (C1_Ent, C2_Ent);
10102 Next_Discriminant (C2_Ent);
10103 end loop;
10104 end if;
10106 -- Record extension case
10108 elsif Nkind (Clist) = N_Derived_Type_Definition then
10109 Clist := Empty;
10111 -- Otherwise check one component list
10113 else
10114 Citem := First (Component_Items (Clist));
10115 while Present (Citem) loop
10116 if Nkind (Citem) = N_Component_Declaration then
10117 C2_Ent := Defining_Identifier (Citem);
10118 exit when C1_Ent = C2_Ent;
10119 Check_Component_Overlap (C1_Ent, C2_Ent);
10120 end if;
10122 Next (Citem);
10123 end loop;
10124 end if;
10126 -- Check for variants above us (the parent of the Clist can
10127 -- be a variant, in which case its parent is a variant part,
10128 -- and the parent of the variant part is a component list
10129 -- whose components must all be checked against the current
10130 -- component for overlap).
10132 if Nkind (Parent (Clist)) = N_Variant then
10133 Clist := Parent (Parent (Parent (Clist)));
10135 -- Check for possible discriminant part in record, this
10136 -- is treated essentially as another level in the
10137 -- recursion. For this case the parent of the component
10138 -- list is the record definition, and its parent is the
10139 -- full type declaration containing the discriminant
10140 -- specifications.
10142 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10143 Clist := Parent (Parent ((Clist)));
10145 -- If neither of these two cases, we are at the top of
10146 -- the tree.
10148 else
10149 exit Component_List_Loop;
10150 end if;
10151 end loop Component_List_Loop;
10153 <<Continue_Main_Component_Loop>>
10154 Next_Entity (C1_Ent);
10156 end loop Main_Component_Loop;
10157 end Overlap_Check2;
10158 end if;
10160 -- The following circuit deals with warning on record holes (gaps). We
10161 -- skip this check if overlap was detected, since it makes sense for the
10162 -- programmer to fix this illegality before worrying about warnings.
10164 if not Overlap_Detected and Warn_On_Record_Holes then
10165 Record_Hole_Check : declare
10166 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10167 -- Full declaration of record type
10169 procedure Check_Component_List
10170 (CL : Node_Id;
10171 Sbit : Uint;
10172 DS : List_Id);
10173 -- Check component list CL for holes. The starting bit should be
10174 -- Sbit. which is zero for the main record component list and set
10175 -- appropriately for recursive calls for variants. DS is set to
10176 -- a list of discriminant specifications to be included in the
10177 -- consideration of components. It is No_List if none to consider.
10179 --------------------------
10180 -- Check_Component_List --
10181 --------------------------
10183 procedure Check_Component_List
10184 (CL : Node_Id;
10185 Sbit : Uint;
10186 DS : List_Id)
10188 Compl : Integer;
10190 begin
10191 Compl := Integer (List_Length (Component_Items (CL)));
10193 if DS /= No_List then
10194 Compl := Compl + Integer (List_Length (DS));
10195 end if;
10197 declare
10198 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10199 -- Gather components (zero entry is for sort routine)
10201 Ncomps : Natural := 0;
10202 -- Number of entries stored in Comps (starting at Comps (1))
10204 Citem : Node_Id;
10205 -- One component item or discriminant specification
10207 Nbit : Uint;
10208 -- Starting bit for next component
10210 CEnt : Entity_Id;
10211 -- Component entity
10213 Variant : Node_Id;
10214 -- One variant
10216 function Lt (Op1, Op2 : Natural) return Boolean;
10217 -- Compare routine for Sort
10219 procedure Move (From : Natural; To : Natural);
10220 -- Move routine for Sort
10222 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10224 --------
10225 -- Lt --
10226 --------
10228 function Lt (Op1, Op2 : Natural) return Boolean is
10229 begin
10230 return Component_Bit_Offset (Comps (Op1))
10232 Component_Bit_Offset (Comps (Op2));
10233 end Lt;
10235 ----------
10236 -- Move --
10237 ----------
10239 procedure Move (From : Natural; To : Natural) is
10240 begin
10241 Comps (To) := Comps (From);
10242 end Move;
10244 begin
10245 -- Gather discriminants into Comp
10247 if DS /= No_List then
10248 Citem := First (DS);
10249 while Present (Citem) loop
10250 if Nkind (Citem) = N_Discriminant_Specification then
10251 declare
10252 Ent : constant Entity_Id :=
10253 Defining_Identifier (Citem);
10254 begin
10255 if Ekind (Ent) = E_Discriminant then
10256 Ncomps := Ncomps + 1;
10257 Comps (Ncomps) := Ent;
10258 end if;
10259 end;
10260 end if;
10262 Next (Citem);
10263 end loop;
10264 end if;
10266 -- Gather component entities into Comp
10268 Citem := First (Component_Items (CL));
10269 while Present (Citem) loop
10270 if Nkind (Citem) = N_Component_Declaration then
10271 Ncomps := Ncomps + 1;
10272 Comps (Ncomps) := Defining_Identifier (Citem);
10273 end if;
10275 Next (Citem);
10276 end loop;
10278 -- Now sort the component entities based on the first bit.
10279 -- Note we already know there are no overlapping components.
10281 Sorting.Sort (Ncomps);
10283 -- Loop through entries checking for holes
10285 Nbit := Sbit;
10286 for J in 1 .. Ncomps loop
10287 CEnt := Comps (J);
10288 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10290 if Error_Msg_Uint_1 > 0 then
10291 Error_Msg_NE
10292 ("?H?^-bit gap before component&",
10293 Component_Name (Component_Clause (CEnt)), CEnt);
10294 end if;
10296 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10297 end loop;
10299 -- Process variant parts recursively if present
10301 if Present (Variant_Part (CL)) then
10302 Variant := First (Variants (Variant_Part (CL)));
10303 while Present (Variant) loop
10304 Check_Component_List
10305 (Component_List (Variant), Nbit, No_List);
10306 Next (Variant);
10307 end loop;
10308 end if;
10309 end;
10310 end Check_Component_List;
10312 -- Start of processing for Record_Hole_Check
10314 begin
10315 declare
10316 Sbit : Uint;
10318 begin
10319 if Is_Tagged_Type (Rectype) then
10320 Sbit := UI_From_Int (System_Address_Size);
10321 else
10322 Sbit := Uint_0;
10323 end if;
10325 if Nkind (Decl) = N_Full_Type_Declaration
10326 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10327 then
10328 Check_Component_List
10329 (Component_List (Type_Definition (Decl)),
10330 Sbit,
10331 Discriminant_Specifications (Decl));
10332 end if;
10333 end;
10334 end Record_Hole_Check;
10335 end if;
10337 -- For records that have component clauses for all components, and whose
10338 -- size is less than or equal to 32, we need to know the size in the
10339 -- front end to activate possible packed array processing where the
10340 -- component type is a record.
10342 -- At this stage Hbit + 1 represents the first unused bit from all the
10343 -- component clauses processed, so if the component clauses are
10344 -- complete, then this is the length of the record.
10346 -- For records longer than System.Storage_Unit, and for those where not
10347 -- all components have component clauses, the back end determines the
10348 -- length (it may for example be appropriate to round up the size
10349 -- to some convenient boundary, based on alignment considerations, etc).
10351 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10353 -- Nothing to do if at least one component has no component clause
10355 Comp := First_Component_Or_Discriminant (Rectype);
10356 while Present (Comp) loop
10357 exit when No (Component_Clause (Comp));
10358 Next_Component_Or_Discriminant (Comp);
10359 end loop;
10361 -- If we fall out of loop, all components have component clauses
10362 -- and so we can set the size to the maximum value.
10364 if No (Comp) then
10365 Set_RM_Size (Rectype, Hbit + 1);
10366 end if;
10367 end if;
10368 end Check_Record_Representation_Clause;
10370 ----------------
10371 -- Check_Size --
10372 ----------------
10374 procedure Check_Size
10375 (N : Node_Id;
10376 T : Entity_Id;
10377 Siz : Uint;
10378 Biased : out Boolean)
10380 procedure Size_Too_Small_Error (Min_Siz : Uint);
10381 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10382 -- minimum size.
10384 --------------------------
10385 -- Size_Too_Small_Error --
10386 --------------------------
10388 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10389 begin
10390 -- This error is suppressed in ASIS mode to allow for different ASIS
10391 -- back ends or ASIS-based tools to query the illegal clause.
10393 if not ASIS_Mode then
10394 Error_Msg_Uint_1 := Min_Siz;
10395 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
10396 end if;
10397 end Size_Too_Small_Error;
10399 -- Local variables
10401 UT : constant Entity_Id := Underlying_Type (T);
10402 M : Uint;
10404 -- Start of processing for Check_Size
10406 begin
10407 Biased := False;
10409 -- Reject patently improper size values
10411 if Is_Elementary_Type (T)
10412 and then Siz > UI_From_Int (Int'Last)
10413 then
10414 Error_Msg_N ("Size value too large for elementary type", N);
10416 if Nkind (Original_Node (N)) = N_Op_Expon then
10417 Error_Msg_N
10418 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10419 end if;
10420 end if;
10422 -- Dismiss generic types
10424 if Is_Generic_Type (T)
10425 or else
10426 Is_Generic_Type (UT)
10427 or else
10428 Is_Generic_Type (Root_Type (UT))
10429 then
10430 return;
10432 -- Guard against previous errors
10434 elsif No (UT) or else UT = Any_Type then
10435 Check_Error_Detected;
10436 return;
10438 -- Check case of bit packed array
10440 elsif Is_Array_Type (UT)
10441 and then Known_Static_Component_Size (UT)
10442 and then Is_Bit_Packed_Array (UT)
10443 then
10444 declare
10445 Asiz : Uint;
10446 Indx : Node_Id;
10447 Ityp : Entity_Id;
10449 begin
10450 Asiz := Component_Size (UT);
10451 Indx := First_Index (UT);
10452 loop
10453 Ityp := Etype (Indx);
10455 -- If non-static bound, then we are not in the business of
10456 -- trying to check the length, and indeed an error will be
10457 -- issued elsewhere, since sizes of non-static array types
10458 -- cannot be set implicitly or explicitly.
10460 if not Is_OK_Static_Subtype (Ityp) then
10461 return;
10462 end if;
10464 -- Otherwise accumulate next dimension
10466 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10467 Expr_Value (Type_Low_Bound (Ityp)) +
10468 Uint_1);
10470 Next_Index (Indx);
10471 exit when No (Indx);
10472 end loop;
10474 if Asiz <= Siz then
10475 return;
10477 else
10478 Size_Too_Small_Error (Asiz);
10479 Set_Esize (T, Asiz);
10480 Set_RM_Size (T, Asiz);
10481 end if;
10482 end;
10484 -- All other composite types are ignored
10486 elsif Is_Composite_Type (UT) then
10487 return;
10489 -- For fixed-point types, don't check minimum if type is not frozen,
10490 -- since we don't know all the characteristics of the type that can
10491 -- affect the size (e.g. a specified small) till freeze time.
10493 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
10494 null;
10496 -- Cases for which a minimum check is required
10498 else
10499 -- Ignore if specified size is correct for the type
10501 if Known_Esize (UT) and then Siz = Esize (UT) then
10502 return;
10503 end if;
10505 -- Otherwise get minimum size
10507 M := UI_From_Int (Minimum_Size (UT));
10509 if Siz < M then
10511 -- Size is less than minimum size, but one possibility remains
10512 -- that we can manage with the new size if we bias the type.
10514 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10516 if Siz < M then
10517 Size_Too_Small_Error (M);
10518 Set_Esize (T, M);
10519 Set_RM_Size (T, M);
10520 else
10521 Biased := True;
10522 end if;
10523 end if;
10524 end if;
10525 end Check_Size;
10527 --------------------------
10528 -- Freeze_Entity_Checks --
10529 --------------------------
10531 procedure Freeze_Entity_Checks (N : Node_Id) is
10532 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10533 -- Inspect the primitive operations of type Typ and hide all pairs of
10534 -- implicitly declared non-overridden non-fully conformant homographs
10535 -- (Ada RM 8.3 12.3/2).
10537 -------------------------------------
10538 -- Hide_Non_Overridden_Subprograms --
10539 -------------------------------------
10541 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10542 procedure Hide_Matching_Homographs
10543 (Subp_Id : Entity_Id;
10544 Start_Elmt : Elmt_Id);
10545 -- Inspect a list of primitive operations starting with Start_Elmt
10546 -- and find matching implicitly declared non-overridden non-fully
10547 -- conformant homographs of Subp_Id. If found, all matches along
10548 -- with Subp_Id are hidden from all visibility.
10550 function Is_Non_Overridden_Or_Null_Procedure
10551 (Subp_Id : Entity_Id) return Boolean;
10552 -- Determine whether subprogram Subp_Id is implicitly declared non-
10553 -- overridden subprogram or an implicitly declared null procedure.
10555 ------------------------------
10556 -- Hide_Matching_Homographs --
10557 ------------------------------
10559 procedure Hide_Matching_Homographs
10560 (Subp_Id : Entity_Id;
10561 Start_Elmt : Elmt_Id)
10563 Prim : Entity_Id;
10564 Prim_Elmt : Elmt_Id;
10566 begin
10567 Prim_Elmt := Start_Elmt;
10568 while Present (Prim_Elmt) loop
10569 Prim := Node (Prim_Elmt);
10571 -- The current primitive is implicitly declared non-overridden
10572 -- non-fully conformant homograph of Subp_Id. Both subprograms
10573 -- must be hidden from visibility.
10575 if Chars (Prim) = Chars (Subp_Id)
10576 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10577 and then not Fully_Conformant (Prim, Subp_Id)
10578 then
10579 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10580 Set_Is_Immediately_Visible (Prim, False);
10581 Set_Is_Potentially_Use_Visible (Prim, False);
10583 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10584 Set_Is_Immediately_Visible (Subp_Id, False);
10585 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10586 end if;
10588 Next_Elmt (Prim_Elmt);
10589 end loop;
10590 end Hide_Matching_Homographs;
10592 -----------------------------------------
10593 -- Is_Non_Overridden_Or_Null_Procedure --
10594 -----------------------------------------
10596 function Is_Non_Overridden_Or_Null_Procedure
10597 (Subp_Id : Entity_Id) return Boolean
10599 Alias_Id : Entity_Id;
10601 begin
10602 -- The subprogram is inherited (implicitly declared), it does not
10603 -- override and does not cover a primitive of an interface.
10605 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10606 and then Present (Alias (Subp_Id))
10607 and then No (Interface_Alias (Subp_Id))
10608 and then No (Overridden_Operation (Subp_Id))
10609 then
10610 Alias_Id := Alias (Subp_Id);
10612 if Requires_Overriding (Alias_Id) then
10613 return True;
10615 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10616 and then Null_Present (Parent (Alias_Id))
10617 then
10618 return True;
10619 end if;
10620 end if;
10622 return False;
10623 end Is_Non_Overridden_Or_Null_Procedure;
10625 -- Local variables
10627 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10628 Prim : Entity_Id;
10629 Prim_Elmt : Elmt_Id;
10631 -- Start of processing for Hide_Non_Overridden_Subprograms
10633 begin
10634 -- Inspect the list of primitives looking for non-overridden
10635 -- subprograms.
10637 if Present (Prim_Ops) then
10638 Prim_Elmt := First_Elmt (Prim_Ops);
10639 while Present (Prim_Elmt) loop
10640 Prim := Node (Prim_Elmt);
10641 Next_Elmt (Prim_Elmt);
10643 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10644 Hide_Matching_Homographs
10645 (Subp_Id => Prim,
10646 Start_Elmt => Prim_Elmt);
10647 end if;
10648 end loop;
10649 end if;
10650 end Hide_Non_Overridden_Subprograms;
10652 -- Local variables
10654 E : constant Entity_Id := Entity (N);
10656 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10657 -- True in non-generic case. Some of the processing here is skipped
10658 -- for the generic case since it is not needed. Basically in the
10659 -- generic case, we only need to do stuff that might generate error
10660 -- messages or warnings.
10662 -- Start of processing for Freeze_Entity_Checks
10664 begin
10665 -- Remember that we are processing a freezing entity. Required to
10666 -- ensure correct decoration of internal entities associated with
10667 -- interfaces (see New_Overloaded_Entity).
10669 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10671 -- For tagged types covering interfaces add internal entities that link
10672 -- the primitives of the interfaces with the primitives that cover them.
10673 -- Note: These entities were originally generated only when generating
10674 -- code because their main purpose was to provide support to initialize
10675 -- the secondary dispatch tables. They are now generated also when
10676 -- compiling with no code generation to provide ASIS the relationship
10677 -- between interface primitives and tagged type primitives. They are
10678 -- also used to locate primitives covering interfaces when processing
10679 -- generics (see Derive_Subprograms).
10681 -- This is not needed in the generic case
10683 if Ada_Version >= Ada_2005
10684 and then Non_Generic_Case
10685 and then Ekind (E) = E_Record_Type
10686 and then Is_Tagged_Type (E)
10687 and then not Is_Interface (E)
10688 and then Has_Interfaces (E)
10689 then
10690 -- This would be a good common place to call the routine that checks
10691 -- overriding of interface primitives (and thus factorize calls to
10692 -- Check_Abstract_Overriding located at different contexts in the
10693 -- compiler). However, this is not possible because it causes
10694 -- spurious errors in case of late overriding.
10696 Add_Internal_Interface_Entities (E);
10697 end if;
10699 -- After all forms of overriding have been resolved, a tagged type may
10700 -- be left with a set of implicitly declared and possibly erroneous
10701 -- abstract subprograms, null procedures and subprograms that require
10702 -- overriding. If this set contains fully conformant homographs, then
10703 -- one is chosen arbitrarily (already done during resolution), otherwise
10704 -- all remaining non-fully conformant homographs are hidden from
10705 -- visibility (Ada RM 8.3 12.3/2).
10707 if Is_Tagged_Type (E) then
10708 Hide_Non_Overridden_Subprograms (E);
10709 end if;
10711 -- Check CPP types
10713 if Ekind (E) = E_Record_Type
10714 and then Is_CPP_Class (E)
10715 and then Is_Tagged_Type (E)
10716 and then Tagged_Type_Expansion
10717 then
10718 if CPP_Num_Prims (E) = 0 then
10720 -- If the CPP type has user defined components then it must import
10721 -- primitives from C++. This is required because if the C++ class
10722 -- has no primitives then the C++ compiler does not added the _tag
10723 -- component to the type.
10725 if First_Entity (E) /= Last_Entity (E) then
10726 Error_Msg_N
10727 ("'C'P'P type must import at least one primitive from C++??",
10729 end if;
10730 end if;
10732 -- Check that all its primitives are abstract or imported from C++.
10733 -- Check also availability of the C++ constructor.
10735 declare
10736 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10737 Elmt : Elmt_Id;
10738 Error_Reported : Boolean := False;
10739 Prim : Node_Id;
10741 begin
10742 Elmt := First_Elmt (Primitive_Operations (E));
10743 while Present (Elmt) loop
10744 Prim := Node (Elmt);
10746 if Comes_From_Source (Prim) then
10747 if Is_Abstract_Subprogram (Prim) then
10748 null;
10750 elsif not Is_Imported (Prim)
10751 or else Convention (Prim) /= Convention_CPP
10752 then
10753 Error_Msg_N
10754 ("primitives of 'C'P'P types must be imported from C++ "
10755 & "or abstract??", Prim);
10757 elsif not Has_Constructors
10758 and then not Error_Reported
10759 then
10760 Error_Msg_Name_1 := Chars (E);
10761 Error_Msg_N
10762 ("??'C'P'P constructor required for type %", Prim);
10763 Error_Reported := True;
10764 end if;
10765 end if;
10767 Next_Elmt (Elmt);
10768 end loop;
10769 end;
10770 end if;
10772 -- Check Ada derivation of CPP type
10774 if Expander_Active -- why? losing errors in -gnatc mode???
10775 and then Present (Etype (E)) -- defend against errors
10776 and then Tagged_Type_Expansion
10777 and then Ekind (E) = E_Record_Type
10778 and then Etype (E) /= E
10779 and then Is_CPP_Class (Etype (E))
10780 and then CPP_Num_Prims (Etype (E)) > 0
10781 and then not Is_CPP_Class (E)
10782 and then not Has_CPP_Constructors (Etype (E))
10783 then
10784 -- If the parent has C++ primitives but it has no constructor then
10785 -- check that all the primitives are overridden in this derivation;
10786 -- otherwise the constructor of the parent is needed to build the
10787 -- dispatch table.
10789 declare
10790 Elmt : Elmt_Id;
10791 Prim : Node_Id;
10793 begin
10794 Elmt := First_Elmt (Primitive_Operations (E));
10795 while Present (Elmt) loop
10796 Prim := Node (Elmt);
10798 if not Is_Abstract_Subprogram (Prim)
10799 and then No (Interface_Alias (Prim))
10800 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10801 then
10802 Error_Msg_Name_1 := Chars (Etype (E));
10803 Error_Msg_N
10804 ("'C'P'P constructor required for parent type %", E);
10805 exit;
10806 end if;
10808 Next_Elmt (Elmt);
10809 end loop;
10810 end;
10811 end if;
10813 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10815 -- If we have a type with predicates, build predicate function. This is
10816 -- not needed in the generic case, nor within TSS subprograms and other
10817 -- predefined primitives.
10819 if Is_Type (E)
10820 and then Non_Generic_Case
10821 and then not Within_Internal_Subprogram
10822 and then Has_Predicates (E)
10823 then
10824 Build_Predicate_Functions (E, N);
10825 end if;
10827 -- If type has delayed aspects, this is where we do the preanalysis at
10828 -- the freeze point, as part of the consistent visibility check. Note
10829 -- that this must be done after calling Build_Predicate_Functions or
10830 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10831 -- the subtype name in the saved expression so that they will not cause
10832 -- trouble in the preanalysis.
10834 -- This is also not needed in the generic case
10836 if Non_Generic_Case
10837 and then Has_Delayed_Aspects (E)
10838 and then Scope (E) = Current_Scope
10839 then
10840 -- Retrieve the visibility to the discriminants in order to properly
10841 -- analyze the aspects.
10843 Push_Scope_And_Install_Discriminants (E);
10845 declare
10846 Ritem : Node_Id;
10848 begin
10849 -- Look for aspect specification entries for this entity
10851 Ritem := First_Rep_Item (E);
10852 while Present (Ritem) loop
10853 if Nkind (Ritem) = N_Aspect_Specification
10854 and then Entity (Ritem) = E
10855 and then Is_Delayed_Aspect (Ritem)
10856 then
10857 Check_Aspect_At_Freeze_Point (Ritem);
10858 end if;
10860 Next_Rep_Item (Ritem);
10861 end loop;
10862 end;
10864 Uninstall_Discriminants_And_Pop_Scope (E);
10865 end if;
10867 -- For a record type, deal with variant parts. This has to be delayed
10868 -- to this point, because of the issue of statically predicated
10869 -- subtypes, which we have to ensure are frozen before checking
10870 -- choices, since we need to have the static choice list set.
10872 if Is_Record_Type (E) then
10873 Check_Variant_Part : declare
10874 D : constant Node_Id := Declaration_Node (E);
10875 T : Node_Id;
10876 C : Node_Id;
10877 VP : Node_Id;
10879 Others_Present : Boolean;
10880 pragma Warnings (Off, Others_Present);
10881 -- Indicates others present, not used in this case
10883 procedure Non_Static_Choice_Error (Choice : Node_Id);
10884 -- Error routine invoked by the generic instantiation below when
10885 -- the variant part has a non static choice.
10887 procedure Process_Declarations (Variant : Node_Id);
10888 -- Processes declarations associated with a variant. We analyzed
10889 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10890 -- but we still need the recursive call to Check_Choices for any
10891 -- nested variant to get its choices properly processed. This is
10892 -- also where we expand out the choices if expansion is active.
10894 package Variant_Choices_Processing is new
10895 Generic_Check_Choices
10896 (Process_Empty_Choice => No_OP,
10897 Process_Non_Static_Choice => Non_Static_Choice_Error,
10898 Process_Associated_Node => Process_Declarations);
10899 use Variant_Choices_Processing;
10901 -----------------------------
10902 -- Non_Static_Choice_Error --
10903 -----------------------------
10905 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10906 begin
10907 Flag_Non_Static_Expr
10908 ("choice given in variant part is not static!", Choice);
10909 end Non_Static_Choice_Error;
10911 --------------------------
10912 -- Process_Declarations --
10913 --------------------------
10915 procedure Process_Declarations (Variant : Node_Id) is
10916 CL : constant Node_Id := Component_List (Variant);
10917 VP : Node_Id;
10919 begin
10920 -- Check for static predicate present in this variant
10922 if Has_SP_Choice (Variant) then
10924 -- Here we expand. You might expect to find this call in
10925 -- Expand_N_Variant_Part, but that is called when we first
10926 -- see the variant part, and we cannot do this expansion
10927 -- earlier than the freeze point, since for statically
10928 -- predicated subtypes, the predicate is not known till
10929 -- the freeze point.
10931 -- Furthermore, we do this expansion even if the expander
10932 -- is not active, because other semantic processing, e.g.
10933 -- for aggregates, requires the expanded list of choices.
10935 -- If the expander is not active, then we can't just clobber
10936 -- the list since it would invalidate the ASIS -gnatct tree.
10937 -- So we have to rewrite the variant part with a Rewrite
10938 -- call that replaces it with a copy and clobber the copy.
10940 if not Expander_Active then
10941 declare
10942 NewV : constant Node_Id := New_Copy (Variant);
10943 begin
10944 Set_Discrete_Choices
10945 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10946 Rewrite (Variant, NewV);
10947 end;
10948 end if;
10950 Expand_Static_Predicates_In_Choices (Variant);
10951 end if;
10953 -- We don't need to worry about the declarations in the variant
10954 -- (since they were analyzed by Analyze_Choices when we first
10955 -- encountered the variant), but we do need to take care of
10956 -- expansion of any nested variants.
10958 if not Null_Present (CL) then
10959 VP := Variant_Part (CL);
10961 if Present (VP) then
10962 Check_Choices
10963 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10964 end if;
10965 end if;
10966 end Process_Declarations;
10968 -- Start of processing for Check_Variant_Part
10970 begin
10971 -- Find component list
10973 C := Empty;
10975 if Nkind (D) = N_Full_Type_Declaration then
10976 T := Type_Definition (D);
10978 if Nkind (T) = N_Record_Definition then
10979 C := Component_List (T);
10981 elsif Nkind (T) = N_Derived_Type_Definition
10982 and then Present (Record_Extension_Part (T))
10983 then
10984 C := Component_List (Record_Extension_Part (T));
10985 end if;
10986 end if;
10988 -- Case of variant part present
10990 if Present (C) and then Present (Variant_Part (C)) then
10991 VP := Variant_Part (C);
10993 -- Check choices
10995 Check_Choices
10996 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10998 -- If the last variant does not contain the Others choice,
10999 -- replace it with an N_Others_Choice node since Gigi always
11000 -- wants an Others. Note that we do not bother to call Analyze
11001 -- on the modified variant part, since its only effect would be
11002 -- to compute the Others_Discrete_Choices node laboriously, and
11003 -- of course we already know the list of choices corresponding
11004 -- to the others choice (it's the list we're replacing).
11006 -- We only want to do this if the expander is active, since
11007 -- we do not want to clobber the ASIS tree.
11009 if Expander_Active then
11010 declare
11011 Last_Var : constant Node_Id :=
11012 Last_Non_Pragma (Variants (VP));
11014 Others_Node : Node_Id;
11016 begin
11017 if Nkind (First (Discrete_Choices (Last_Var))) /=
11018 N_Others_Choice
11019 then
11020 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11021 Set_Others_Discrete_Choices
11022 (Others_Node, Discrete_Choices (Last_Var));
11023 Set_Discrete_Choices
11024 (Last_Var, New_List (Others_Node));
11025 end if;
11026 end;
11027 end if;
11028 end if;
11029 end Check_Variant_Part;
11030 end if;
11031 end Freeze_Entity_Checks;
11033 -------------------------
11034 -- Get_Alignment_Value --
11035 -------------------------
11037 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11038 Align : constant Uint := Static_Integer (Expr);
11040 begin
11041 if Align = No_Uint then
11042 return No_Uint;
11044 elsif Align <= 0 then
11046 -- This error is suppressed in ASIS mode to allow for different ASIS
11047 -- back ends or ASIS-based tools to query the illegal clause.
11049 if not ASIS_Mode then
11050 Error_Msg_N ("alignment value must be positive", Expr);
11051 end if;
11053 return No_Uint;
11055 else
11056 for J in Int range 0 .. 64 loop
11057 declare
11058 M : constant Uint := Uint_2 ** J;
11060 begin
11061 exit when M = Align;
11063 if M > Align then
11065 -- This error is suppressed in ASIS mode to allow for
11066 -- different ASIS back ends or ASIS-based tools to query the
11067 -- illegal clause.
11069 if not ASIS_Mode then
11070 Error_Msg_N ("alignment value must be power of 2", Expr);
11071 end if;
11073 return No_Uint;
11074 end if;
11075 end;
11076 end loop;
11078 return Align;
11079 end if;
11080 end Get_Alignment_Value;
11082 -----------------------------
11083 -- Get_Interfacing_Aspects --
11084 -----------------------------
11086 procedure Get_Interfacing_Aspects
11087 (Iface_Asp : Node_Id;
11088 Conv_Asp : out Node_Id;
11089 EN_Asp : out Node_Id;
11090 Expo_Asp : out Node_Id;
11091 Imp_Asp : out Node_Id;
11092 LN_Asp : out Node_Id;
11093 Do_Checks : Boolean := False)
11095 procedure Save_Or_Duplication_Error
11096 (Asp : Node_Id;
11097 To : in out Node_Id);
11098 -- Save the value of aspect Asp in node To. If To already has a value,
11099 -- then this is considered a duplicate use of aspect. Emit an error if
11100 -- flag Do_Checks is set.
11102 -------------------------------
11103 -- Save_Or_Duplication_Error --
11104 -------------------------------
11106 procedure Save_Or_Duplication_Error
11107 (Asp : Node_Id;
11108 To : in out Node_Id)
11110 begin
11111 -- Detect an extra aspect and issue an error
11113 if Present (To) then
11114 if Do_Checks then
11115 Error_Msg_Name_1 := Chars (Identifier (Asp));
11116 Error_Msg_Sloc := Sloc (To);
11117 Error_Msg_N ("aspect % previously given #", Asp);
11118 end if;
11120 -- Otherwise capture the aspect
11122 else
11123 To := Asp;
11124 end if;
11125 end Save_Or_Duplication_Error;
11127 -- Local variables
11129 Asp : Node_Id;
11130 Asp_Id : Aspect_Id;
11132 -- The following variables capture each individual aspect
11134 Conv : Node_Id := Empty;
11135 EN : Node_Id := Empty;
11136 Expo : Node_Id := Empty;
11137 Imp : Node_Id := Empty;
11138 LN : Node_Id := Empty;
11140 -- Start of processing for Get_Interfacing_Aspects
11142 begin
11143 -- The input interfacing aspect should reside in an aspect specification
11144 -- list.
11146 pragma Assert (Is_List_Member (Iface_Asp));
11148 -- Examine the aspect specifications of the related entity. Find and
11149 -- capture all interfacing aspects. Detect duplicates and emit errors
11150 -- if applicable.
11152 Asp := First (List_Containing (Iface_Asp));
11153 while Present (Asp) loop
11154 Asp_Id := Get_Aspect_Id (Asp);
11156 if Asp_Id = Aspect_Convention then
11157 Save_Or_Duplication_Error (Asp, Conv);
11159 elsif Asp_Id = Aspect_External_Name then
11160 Save_Or_Duplication_Error (Asp, EN);
11162 elsif Asp_Id = Aspect_Export then
11163 Save_Or_Duplication_Error (Asp, Expo);
11165 elsif Asp_Id = Aspect_Import then
11166 Save_Or_Duplication_Error (Asp, Imp);
11168 elsif Asp_Id = Aspect_Link_Name then
11169 Save_Or_Duplication_Error (Asp, LN);
11170 end if;
11172 Next (Asp);
11173 end loop;
11175 Conv_Asp := Conv;
11176 EN_Asp := EN;
11177 Expo_Asp := Expo;
11178 Imp_Asp := Imp;
11179 LN_Asp := LN;
11180 end Get_Interfacing_Aspects;
11182 -------------------------------------
11183 -- Inherit_Aspects_At_Freeze_Point --
11184 -------------------------------------
11186 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11187 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11188 (Rep_Item : Node_Id) return Boolean;
11189 -- This routine checks if Rep_Item is either a pragma or an aspect
11190 -- specification node whose correponding pragma (if any) is present in
11191 -- the Rep Item chain of the entity it has been specified to.
11193 --------------------------------------------------
11194 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11195 --------------------------------------------------
11197 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11198 (Rep_Item : Node_Id) return Boolean
11200 begin
11201 return
11202 Nkind (Rep_Item) = N_Pragma
11203 or else Present_In_Rep_Item
11204 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11205 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11207 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11209 begin
11210 -- A representation item is either subtype-specific (Size and Alignment
11211 -- clauses) or type-related (all others). Subtype-specific aspects may
11212 -- differ for different subtypes of the same type (RM 13.1.8).
11214 -- A derived type inherits each type-related representation aspect of
11215 -- its parent type that was directly specified before the declaration of
11216 -- the derived type (RM 13.1.15).
11218 -- A derived subtype inherits each subtype-specific representation
11219 -- aspect of its parent subtype that was directly specified before the
11220 -- declaration of the derived type (RM 13.1.15).
11222 -- The general processing involves inheriting a representation aspect
11223 -- from a parent type whenever the first rep item (aspect specification,
11224 -- attribute definition clause, pragma) corresponding to the given
11225 -- representation aspect in the rep item chain of Typ, if any, isn't
11226 -- directly specified to Typ but to one of its parents.
11228 -- ??? Note that, for now, just a limited number of representation
11229 -- aspects have been inherited here so far. Many of them are
11230 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11231 -- a non- exhaustive list of aspects that likely also need to
11232 -- be moved to this routine: Alignment, Component_Alignment,
11233 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11234 -- Preelaborable_Initialization, RM_Size and Small.
11236 -- In addition, Convention must be propagated from base type to subtype,
11237 -- because the subtype may have been declared on an incomplete view.
11239 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11240 return;
11241 end if;
11243 -- Ada_05/Ada_2005
11245 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11246 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11247 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11248 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11249 then
11250 Set_Is_Ada_2005_Only (Typ);
11251 end if;
11253 -- Ada_12/Ada_2012
11255 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11256 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11257 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11258 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11259 then
11260 Set_Is_Ada_2012_Only (Typ);
11261 end if;
11263 -- Atomic/Shared
11265 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11266 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11267 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11268 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11269 then
11270 Set_Is_Atomic (Typ);
11271 Set_Is_Volatile (Typ);
11272 Set_Treat_As_Volatile (Typ);
11273 end if;
11275 -- Convention
11277 if Is_Record_Type (Typ)
11278 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11279 then
11280 Set_Convention (Typ, Convention (Base_Type (Typ)));
11281 end if;
11283 -- Default_Component_Value
11285 -- Verify that there is no rep_item declared for the type, and there
11286 -- is one coming from an ancestor.
11288 if Is_Array_Type (Typ)
11289 and then Is_Base_Type (Typ)
11290 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11291 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11292 then
11293 Set_Default_Aspect_Component_Value (Typ,
11294 Default_Aspect_Component_Value
11295 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11296 end if;
11298 -- Default_Value
11300 if Is_Scalar_Type (Typ)
11301 and then Is_Base_Type (Typ)
11302 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11303 and then Has_Rep_Item (Typ, Name_Default_Value)
11304 then
11305 Set_Has_Default_Aspect (Typ);
11306 Set_Default_Aspect_Value (Typ,
11307 Default_Aspect_Value
11308 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11309 end if;
11311 -- Discard_Names
11313 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11314 and then Has_Rep_Item (Typ, Name_Discard_Names)
11315 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11316 (Get_Rep_Item (Typ, Name_Discard_Names))
11317 then
11318 Set_Discard_Names (Typ);
11319 end if;
11321 -- Volatile
11323 if not Has_Rep_Item (Typ, Name_Volatile, False)
11324 and then Has_Rep_Item (Typ, Name_Volatile)
11325 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11326 (Get_Rep_Item (Typ, Name_Volatile))
11327 then
11328 Set_Is_Volatile (Typ);
11329 Set_Treat_As_Volatile (Typ);
11330 end if;
11332 -- Volatile_Full_Access
11334 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11335 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11336 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11337 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11338 then
11339 Set_Is_Volatile_Full_Access (Typ);
11340 Set_Is_Volatile (Typ);
11341 Set_Treat_As_Volatile (Typ);
11342 end if;
11344 -- Inheritance for derived types only
11346 if Is_Derived_Type (Typ) then
11347 declare
11348 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11349 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11351 begin
11352 -- Atomic_Components
11354 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11355 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11356 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11357 (Get_Rep_Item (Typ, Name_Atomic_Components))
11358 then
11359 Set_Has_Atomic_Components (Imp_Bas_Typ);
11360 end if;
11362 -- Volatile_Components
11364 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11365 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11366 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11367 (Get_Rep_Item (Typ, Name_Volatile_Components))
11368 then
11369 Set_Has_Volatile_Components (Imp_Bas_Typ);
11370 end if;
11372 -- Finalize_Storage_Only
11374 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11375 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11376 then
11377 Set_Finalize_Storage_Only (Bas_Typ);
11378 end if;
11380 -- Universal_Aliasing
11382 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11383 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11384 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11385 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11386 then
11387 Set_Universal_Aliasing (Imp_Bas_Typ);
11388 end if;
11390 -- Bit_Order
11392 if Is_Record_Type (Typ) then
11393 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11394 and then Has_Rep_Item (Typ, Name_Bit_Order)
11395 then
11396 Set_Reverse_Bit_Order (Bas_Typ,
11397 Reverse_Bit_Order (Entity (Name
11398 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11399 end if;
11400 end if;
11402 -- Scalar_Storage_Order
11404 -- Note: the aspect is specified on a first subtype, but recorded
11405 -- in a flag of the base type!
11407 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11408 and then Typ = Bas_Typ
11409 then
11410 -- For a type extension, always inherit from parent; otherwise
11411 -- inherit if no default applies. Note: we do not check for
11412 -- an explicit rep item on the parent type when inheriting,
11413 -- because the parent SSO may itself have been set by default.
11415 if not Has_Rep_Item (First_Subtype (Typ),
11416 Name_Scalar_Storage_Order, False)
11417 and then (Is_Tagged_Type (Bas_Typ)
11418 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11419 or else
11420 SSO_Set_High_By_Default (Bas_Typ)))
11421 then
11422 Set_Reverse_Storage_Order (Bas_Typ,
11423 Reverse_Storage_Order
11424 (Implementation_Base_Type (Etype (Bas_Typ))));
11426 -- Clear default SSO indications, since the inherited aspect
11427 -- which was set explicitly overrides the default.
11429 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11430 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11431 end if;
11432 end if;
11433 end;
11434 end if;
11435 end Inherit_Aspects_At_Freeze_Point;
11437 ----------------
11438 -- Initialize --
11439 ----------------
11441 procedure Initialize is
11442 begin
11443 Address_Clause_Checks.Init;
11444 Compile_Time_Warnings_Errors.Init;
11445 Unchecked_Conversions.Init;
11447 if AAMP_On_Target then
11448 Independence_Checks.Init;
11449 end if;
11450 end Initialize;
11452 ---------------------------
11453 -- Install_Discriminants --
11454 ---------------------------
11456 procedure Install_Discriminants (E : Entity_Id) is
11457 Disc : Entity_Id;
11458 Prev : Entity_Id;
11459 begin
11460 Disc := First_Discriminant (E);
11461 while Present (Disc) loop
11462 Prev := Current_Entity (Disc);
11463 Set_Current_Entity (Disc);
11464 Set_Is_Immediately_Visible (Disc);
11465 Set_Homonym (Disc, Prev);
11466 Next_Discriminant (Disc);
11467 end loop;
11468 end Install_Discriminants;
11470 -------------------------
11471 -- Is_Operational_Item --
11472 -------------------------
11474 function Is_Operational_Item (N : Node_Id) return Boolean is
11475 begin
11476 if Nkind (N) /= N_Attribute_Definition_Clause then
11477 return False;
11479 else
11480 declare
11481 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11482 begin
11484 -- List of operational items is given in AARM 13.1(8.mm/1).
11485 -- It is clearly incomplete, as it does not include iterator
11486 -- aspects, among others.
11488 return Id = Attribute_Constant_Indexing
11489 or else Id = Attribute_Default_Iterator
11490 or else Id = Attribute_Implicit_Dereference
11491 or else Id = Attribute_Input
11492 or else Id = Attribute_Iterator_Element
11493 or else Id = Attribute_Iterable
11494 or else Id = Attribute_Output
11495 or else Id = Attribute_Read
11496 or else Id = Attribute_Variable_Indexing
11497 or else Id = Attribute_Write
11498 or else Id = Attribute_External_Tag;
11499 end;
11500 end if;
11501 end Is_Operational_Item;
11503 -------------------------
11504 -- Is_Predicate_Static --
11505 -------------------------
11507 -- Note: the basic legality of the expression has already been checked, so
11508 -- we don't need to worry about cases or ranges on strings for example.
11510 function Is_Predicate_Static
11511 (Expr : Node_Id;
11512 Nam : Name_Id) return Boolean
11514 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11515 -- Given a list of case expression alternatives, returns True if all
11516 -- the alternatives are static (have all static choices, and a static
11517 -- expression).
11519 function All_Static_Choices (L : List_Id) return Boolean;
11520 -- Returns true if all elements of the list are OK static choices
11521 -- as defined below for Is_Static_Choice. Used for case expression
11522 -- alternatives and for the right operand of a membership test. An
11523 -- others_choice is static if the corresponding expression is static.
11524 -- The staticness of the bounds is checked separately.
11526 function Is_Static_Choice (N : Node_Id) return Boolean;
11527 -- Returns True if N represents a static choice (static subtype, or
11528 -- static subtype indication, or static expression, or static range).
11530 -- Note that this is a bit more inclusive than we actually need
11531 -- (in particular membership tests do not allow the use of subtype
11532 -- indications). But that doesn't matter, we have already checked
11533 -- that the construct is legal to get this far.
11535 function Is_Type_Ref (N : Node_Id) return Boolean;
11536 pragma Inline (Is_Type_Ref);
11537 -- Returns True if N is a reference to the type for the predicate in the
11538 -- expression (i.e. if it is an identifier whose Chars field matches the
11539 -- Nam given in the call). N must not be parenthesized, if the type name
11540 -- appears in parens, this routine will return False.
11542 ----------------------------------
11543 -- All_Static_Case_Alternatives --
11544 ----------------------------------
11546 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11547 N : Node_Id;
11549 begin
11550 N := First (L);
11551 while Present (N) loop
11552 if not (All_Static_Choices (Discrete_Choices (N))
11553 and then Is_OK_Static_Expression (Expression (N)))
11554 then
11555 return False;
11556 end if;
11558 Next (N);
11559 end loop;
11561 return True;
11562 end All_Static_Case_Alternatives;
11564 ------------------------
11565 -- All_Static_Choices --
11566 ------------------------
11568 function All_Static_Choices (L : List_Id) return Boolean is
11569 N : Node_Id;
11571 begin
11572 N := First (L);
11573 while Present (N) loop
11574 if not Is_Static_Choice (N) then
11575 return False;
11576 end if;
11578 Next (N);
11579 end loop;
11581 return True;
11582 end All_Static_Choices;
11584 ----------------------
11585 -- Is_Static_Choice --
11586 ----------------------
11588 function Is_Static_Choice (N : Node_Id) return Boolean is
11589 begin
11590 return Nkind (N) = N_Others_Choice
11591 or else Is_OK_Static_Expression (N)
11592 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11593 and then Is_OK_Static_Subtype (Entity (N)))
11594 or else (Nkind (N) = N_Subtype_Indication
11595 and then Is_OK_Static_Subtype (Entity (N)))
11596 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11597 end Is_Static_Choice;
11599 -----------------
11600 -- Is_Type_Ref --
11601 -----------------
11603 function Is_Type_Ref (N : Node_Id) return Boolean is
11604 begin
11605 return Nkind (N) = N_Identifier
11606 and then Chars (N) = Nam
11607 and then Paren_Count (N) = 0;
11608 end Is_Type_Ref;
11610 -- Start of processing for Is_Predicate_Static
11612 begin
11613 -- Predicate_Static means one of the following holds. Numbers are the
11614 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11616 -- 16: A static expression
11618 if Is_OK_Static_Expression (Expr) then
11619 return True;
11621 -- 17: A membership test whose simple_expression is the current
11622 -- instance, and whose membership_choice_list meets the requirements
11623 -- for a static membership test.
11625 elsif Nkind (Expr) in N_Membership_Test
11626 and then ((Present (Right_Opnd (Expr))
11627 and then Is_Static_Choice (Right_Opnd (Expr)))
11628 or else
11629 (Present (Alternatives (Expr))
11630 and then All_Static_Choices (Alternatives (Expr))))
11631 then
11632 return True;
11634 -- 18. A case_expression whose selecting_expression is the current
11635 -- instance, and whose dependent expressions are static expressions.
11637 elsif Nkind (Expr) = N_Case_Expression
11638 and then Is_Type_Ref (Expression (Expr))
11639 and then All_Static_Case_Alternatives (Alternatives (Expr))
11640 then
11641 return True;
11643 -- 19. A call to a predefined equality or ordering operator, where one
11644 -- operand is the current instance, and the other is a static
11645 -- expression.
11647 -- Note: the RM is clearly wrong here in not excluding string types.
11648 -- Without this exclusion, we would allow expressions like X > "ABC"
11649 -- to be considered as predicate-static, which is clearly not intended,
11650 -- since the idea is for predicate-static to be a subset of normal
11651 -- static expressions (and "DEF" > "ABC" is not a static expression).
11653 -- However, we do allow internally generated (not from source) equality
11654 -- and inequality operations to be valid on strings (this helps deal
11655 -- with cases where we transform A in "ABC" to A = "ABC).
11657 elsif Nkind (Expr) in N_Op_Compare
11658 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11659 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11660 and then not Comes_From_Source (Expr)))
11661 and then ((Is_Type_Ref (Left_Opnd (Expr))
11662 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11663 or else
11664 (Is_Type_Ref (Right_Opnd (Expr))
11665 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11666 then
11667 return True;
11669 -- 20. A call to a predefined boolean logical operator, where each
11670 -- operand is predicate-static.
11672 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11673 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11674 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11675 or else
11676 (Nkind (Expr) = N_Op_Not
11677 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11678 then
11679 return True;
11681 -- 21. A short-circuit control form where both operands are
11682 -- predicate-static.
11684 elsif Nkind (Expr) in N_Short_Circuit
11685 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11686 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11687 then
11688 return True;
11690 -- 22. A parenthesized predicate-static expression. This does not
11691 -- require any special test, since we just ignore paren levels in
11692 -- all the cases above.
11694 -- One more test that is an implementation artifact caused by the fact
11695 -- that we are analyzing not the original expression, but the generated
11696 -- expression in the body of the predicate function. This can include
11697 -- references to inherited predicates, so that the expression we are
11698 -- processing looks like:
11700 -- xxPredicate (typ (Inns)) and then expression
11702 -- Where the call is to a Predicate function for an inherited predicate.
11703 -- We simply ignore such a call, which could be to either a dynamic or
11704 -- a static predicate. Note that if the parent predicate is dynamic then
11705 -- eventually this type will be marked as dynamic, but you are allowed
11706 -- to specify a static predicate for a subtype which is inheriting a
11707 -- dynamic predicate, so the static predicate validation here ignores
11708 -- the inherited predicate even if it is dynamic.
11709 -- In all cases, a static predicate can only apply to a scalar type.
11711 elsif Nkind (Expr) = N_Function_Call
11712 and then Is_Predicate_Function (Entity (Name (Expr)))
11713 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
11714 then
11715 return True;
11717 -- That's an exhaustive list of tests, all other cases are not
11718 -- predicate-static, so we return False.
11720 else
11721 return False;
11722 end if;
11723 end Is_Predicate_Static;
11725 ---------------------
11726 -- Kill_Rep_Clause --
11727 ---------------------
11729 procedure Kill_Rep_Clause (N : Node_Id) is
11730 begin
11731 pragma Assert (Ignore_Rep_Clauses);
11733 -- Note: we use Replace rather than Rewrite, because we don't want
11734 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11735 -- rep clause that is being replaced.
11737 Replace (N, Make_Null_Statement (Sloc (N)));
11739 -- The null statement must be marked as not coming from source. This is
11740 -- so that ASIS ignores it, and also the back end does not expect bogus
11741 -- "from source" null statements in weird places (e.g. in declarative
11742 -- regions where such null statements are not allowed).
11744 Set_Comes_From_Source (N, False);
11745 end Kill_Rep_Clause;
11747 ------------------
11748 -- Minimum_Size --
11749 ------------------
11751 function Minimum_Size
11752 (T : Entity_Id;
11753 Biased : Boolean := False) return Nat
11755 Lo : Uint := No_Uint;
11756 Hi : Uint := No_Uint;
11757 LoR : Ureal := No_Ureal;
11758 HiR : Ureal := No_Ureal;
11759 LoSet : Boolean := False;
11760 HiSet : Boolean := False;
11761 B : Uint;
11762 S : Nat;
11763 Ancest : Entity_Id;
11764 R_Typ : constant Entity_Id := Root_Type (T);
11766 begin
11767 -- If bad type, return 0
11769 if T = Any_Type then
11770 return 0;
11772 -- For generic types, just return zero. There cannot be any legitimate
11773 -- need to know such a size, but this routine may be called with a
11774 -- generic type as part of normal processing.
11776 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11777 return 0;
11779 -- Access types (cannot have size smaller than System.Address)
11781 elsif Is_Access_Type (T) then
11782 return System_Address_Size;
11784 -- Floating-point types
11786 elsif Is_Floating_Point_Type (T) then
11787 return UI_To_Int (Esize (R_Typ));
11789 -- Discrete types
11791 elsif Is_Discrete_Type (T) then
11793 -- The following loop is looking for the nearest compile time known
11794 -- bounds following the ancestor subtype chain. The idea is to find
11795 -- the most restrictive known bounds information.
11797 Ancest := T;
11798 loop
11799 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11800 return 0;
11801 end if;
11803 if not LoSet then
11804 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11805 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11806 LoSet := True;
11807 exit when HiSet;
11808 end if;
11809 end if;
11811 if not HiSet then
11812 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11813 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11814 HiSet := True;
11815 exit when LoSet;
11816 end if;
11817 end if;
11819 Ancest := Ancestor_Subtype (Ancest);
11821 if No (Ancest) then
11822 Ancest := Base_Type (T);
11824 if Is_Generic_Type (Ancest) then
11825 return 0;
11826 end if;
11827 end if;
11828 end loop;
11830 -- Fixed-point types. We can't simply use Expr_Value to get the
11831 -- Corresponding_Integer_Value values of the bounds, since these do not
11832 -- get set till the type is frozen, and this routine can be called
11833 -- before the type is frozen. Similarly the test for bounds being static
11834 -- needs to include the case where we have unanalyzed real literals for
11835 -- the same reason.
11837 elsif Is_Fixed_Point_Type (T) then
11839 -- The following loop is looking for the nearest compile time known
11840 -- bounds following the ancestor subtype chain. The idea is to find
11841 -- the most restrictive known bounds information.
11843 Ancest := T;
11844 loop
11845 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11846 return 0;
11847 end if;
11849 -- Note: In the following two tests for LoSet and HiSet, it may
11850 -- seem redundant to test for N_Real_Literal here since normally
11851 -- one would assume that the test for the value being known at
11852 -- compile time includes this case. However, there is a glitch.
11853 -- If the real literal comes from folding a non-static expression,
11854 -- then we don't consider any non- static expression to be known
11855 -- at compile time if we are in configurable run time mode (needed
11856 -- in some cases to give a clearer definition of what is and what
11857 -- is not accepted). So the test is indeed needed. Without it, we
11858 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11860 if not LoSet then
11861 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11862 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11863 then
11864 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11865 LoSet := True;
11866 exit when HiSet;
11867 end if;
11868 end if;
11870 if not HiSet then
11871 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11872 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11873 then
11874 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11875 HiSet := True;
11876 exit when LoSet;
11877 end if;
11878 end if;
11880 Ancest := Ancestor_Subtype (Ancest);
11882 if No (Ancest) then
11883 Ancest := Base_Type (T);
11885 if Is_Generic_Type (Ancest) then
11886 return 0;
11887 end if;
11888 end if;
11889 end loop;
11891 Lo := UR_To_Uint (LoR / Small_Value (T));
11892 Hi := UR_To_Uint (HiR / Small_Value (T));
11894 -- No other types allowed
11896 else
11897 raise Program_Error;
11898 end if;
11900 -- Fall through with Hi and Lo set. Deal with biased case
11902 if (Biased
11903 and then not Is_Fixed_Point_Type (T)
11904 and then not (Is_Enumeration_Type (T)
11905 and then Has_Non_Standard_Rep (T)))
11906 or else Has_Biased_Representation (T)
11907 then
11908 Hi := Hi - Lo;
11909 Lo := Uint_0;
11910 end if;
11912 -- Null range case, size is always zero. We only do this in the discrete
11913 -- type case, since that's the odd case that came up. Probably we should
11914 -- also do this in the fixed-point case, but doing so causes peculiar
11915 -- gigi failures, and it is not worth worrying about this incredibly
11916 -- marginal case (explicit null-range fixed-point type declarations)???
11918 if Lo > Hi and then Is_Discrete_Type (T) then
11919 S := 0;
11921 -- Signed case. Note that we consider types like range 1 .. -1 to be
11922 -- signed for the purpose of computing the size, since the bounds have
11923 -- to be accommodated in the base type.
11925 elsif Lo < 0 or else Hi < 0 then
11926 S := 1;
11927 B := Uint_1;
11929 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11930 -- Note that we accommodate the case where the bounds cross. This
11931 -- can happen either because of the way the bounds are declared
11932 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11934 while Lo < -B
11935 or else Hi < -B
11936 or else Lo >= B
11937 or else Hi >= B
11938 loop
11939 B := Uint_2 ** S;
11940 S := S + 1;
11941 end loop;
11943 -- Unsigned case
11945 else
11946 -- If both bounds are positive, make sure that both are represen-
11947 -- table in the case where the bounds are crossed. This can happen
11948 -- either because of the way the bounds are declared, or because of
11949 -- the algorithm in Freeze_Fixed_Point_Type.
11951 if Lo > Hi then
11952 Hi := Lo;
11953 end if;
11955 -- S = size, (can accommodate 0 .. (2**size - 1))
11957 S := 0;
11958 while Hi >= Uint_2 ** S loop
11959 S := S + 1;
11960 end loop;
11961 end if;
11963 return S;
11964 end Minimum_Size;
11966 ---------------------------
11967 -- New_Stream_Subprogram --
11968 ---------------------------
11970 procedure New_Stream_Subprogram
11971 (N : Node_Id;
11972 Ent : Entity_Id;
11973 Subp : Entity_Id;
11974 Nam : TSS_Name_Type)
11976 Loc : constant Source_Ptr := Sloc (N);
11977 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
11978 Subp_Id : Entity_Id;
11979 Subp_Decl : Node_Id;
11980 F : Entity_Id;
11981 Etyp : Entity_Id;
11983 Defer_Declaration : constant Boolean :=
11984 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11985 -- For a tagged type, there is a declaration for each stream attribute
11986 -- at the freeze point, and we must generate only a completion of this
11987 -- declaration. We do the same for private types, because the full view
11988 -- might be tagged. Otherwise we generate a declaration at the point of
11989 -- the attribute definition clause. If the attribute definition comes
11990 -- from an aspect specification the declaration is part of the freeze
11991 -- actions of the type.
11993 function Build_Spec return Node_Id;
11994 -- Used for declaration and renaming declaration, so that this is
11995 -- treated as a renaming_as_body.
11997 ----------------
11998 -- Build_Spec --
11999 ----------------
12001 function Build_Spec return Node_Id is
12002 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12003 Formals : List_Id;
12004 Spec : Node_Id;
12005 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
12007 begin
12008 Subp_Id := Make_Defining_Identifier (Loc, Sname);
12010 -- S : access Root_Stream_Type'Class
12012 Formals := New_List (
12013 Make_Parameter_Specification (Loc,
12014 Defining_Identifier =>
12015 Make_Defining_Identifier (Loc, Name_S),
12016 Parameter_Type =>
12017 Make_Access_Definition (Loc,
12018 Subtype_Mark =>
12019 New_Occurrence_Of (
12020 Designated_Type (Etype (F)), Loc))));
12022 if Nam = TSS_Stream_Input then
12023 Spec :=
12024 Make_Function_Specification (Loc,
12025 Defining_Unit_Name => Subp_Id,
12026 Parameter_Specifications => Formals,
12027 Result_Definition => T_Ref);
12028 else
12029 -- V : [out] T
12031 Append_To (Formals,
12032 Make_Parameter_Specification (Loc,
12033 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12034 Out_Present => Out_P,
12035 Parameter_Type => T_Ref));
12037 Spec :=
12038 Make_Procedure_Specification (Loc,
12039 Defining_Unit_Name => Subp_Id,
12040 Parameter_Specifications => Formals);
12041 end if;
12043 return Spec;
12044 end Build_Spec;
12046 -- Start of processing for New_Stream_Subprogram
12048 begin
12049 F := First_Formal (Subp);
12051 if Ekind (Subp) = E_Procedure then
12052 Etyp := Etype (Next_Formal (F));
12053 else
12054 Etyp := Etype (Subp);
12055 end if;
12057 -- Prepare subprogram declaration and insert it as an action on the
12058 -- clause node. The visibility for this entity is used to test for
12059 -- visibility of the attribute definition clause (in the sense of
12060 -- 8.3(23) as amended by AI-195).
12062 if not Defer_Declaration then
12063 Subp_Decl :=
12064 Make_Subprogram_Declaration (Loc,
12065 Specification => Build_Spec);
12067 -- For a tagged type, there is always a visible declaration for each
12068 -- stream TSS (it is a predefined primitive operation), and the
12069 -- completion of this declaration occurs at the freeze point, which is
12070 -- not always visible at places where the attribute definition clause is
12071 -- visible. So, we create a dummy entity here for the purpose of
12072 -- tracking the visibility of the attribute definition clause itself.
12074 else
12075 Subp_Id :=
12076 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
12077 Subp_Decl :=
12078 Make_Object_Declaration (Loc,
12079 Defining_Identifier => Subp_Id,
12080 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
12081 end if;
12083 if not Defer_Declaration
12084 and then From_Aspect_Specification (N)
12085 and then Has_Delayed_Freeze (Ent)
12086 then
12087 Append_Freeze_Action (Ent, Subp_Decl);
12089 else
12090 Insert_Action (N, Subp_Decl);
12091 Set_Entity (N, Subp_Id);
12092 end if;
12094 Subp_Decl :=
12095 Make_Subprogram_Renaming_Declaration (Loc,
12096 Specification => Build_Spec,
12097 Name => New_Occurrence_Of (Subp, Loc));
12099 if Defer_Declaration then
12100 Set_TSS (Base_Type (Ent), Subp_Id);
12102 else
12103 if From_Aspect_Specification (N) then
12104 Append_Freeze_Action (Ent, Subp_Decl);
12105 else
12106 Insert_Action (N, Subp_Decl);
12107 end if;
12109 Copy_TSS (Subp_Id, Base_Type (Ent));
12110 end if;
12111 end New_Stream_Subprogram;
12113 ------------------------------------------
12114 -- Push_Scope_And_Install_Discriminants --
12115 ------------------------------------------
12117 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12118 begin
12119 if Has_Discriminants (E) then
12120 Push_Scope (E);
12122 -- Make the discriminants visible for type declarations and protected
12123 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12125 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12126 Install_Discriminants (E);
12127 end if;
12128 end if;
12129 end Push_Scope_And_Install_Discriminants;
12131 ------------------------
12132 -- Rep_Item_Too_Early --
12133 ------------------------
12135 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12136 begin
12137 -- Cannot apply non-operational rep items to generic types
12139 if Is_Operational_Item (N) then
12140 return False;
12142 elsif Is_Type (T)
12143 and then Is_Generic_Type (Root_Type (T))
12144 and then (Nkind (N) /= N_Pragma
12145 or else Get_Pragma_Id (N) /= Pragma_Convention)
12146 then
12147 Error_Msg_N ("representation item not allowed for generic type", N);
12148 return True;
12149 end if;
12151 -- Otherwise check for incomplete type
12153 if Is_Incomplete_Or_Private_Type (T)
12154 and then No (Underlying_Type (T))
12155 and then
12156 (Nkind (N) /= N_Pragma
12157 or else Get_Pragma_Id (N) /= Pragma_Import)
12158 then
12159 Error_Msg_N
12160 ("representation item must be after full type declaration", N);
12161 return True;
12163 -- If the type has incomplete components, a representation clause is
12164 -- illegal but stream attributes and Convention pragmas are correct.
12166 elsif Has_Private_Component (T) then
12167 if Nkind (N) = N_Pragma then
12168 return False;
12170 else
12171 Error_Msg_N
12172 ("representation item must appear after type is fully defined",
12174 return True;
12175 end if;
12176 else
12177 return False;
12178 end if;
12179 end Rep_Item_Too_Early;
12181 -----------------------
12182 -- Rep_Item_Too_Late --
12183 -----------------------
12185 function Rep_Item_Too_Late
12186 (T : Entity_Id;
12187 N : Node_Id;
12188 FOnly : Boolean := False) return Boolean
12190 S : Entity_Id;
12191 Parent_Type : Entity_Id;
12193 procedure No_Type_Rep_Item;
12194 -- Output message indicating that no type-related aspects can be
12195 -- specified due to some property of the parent type.
12197 procedure Too_Late;
12198 -- Output message for an aspect being specified too late
12200 -- Note that neither of the above errors is considered a serious one,
12201 -- since the effect is simply that we ignore the representation clause
12202 -- in these cases.
12203 -- Is this really true? In any case if we make this change we must
12204 -- document the requirement in the spec of Rep_Item_Too_Late that
12205 -- if True is returned, then the rep item must be completely ignored???
12207 ----------------------
12208 -- No_Type_Rep_Item --
12209 ----------------------
12211 procedure No_Type_Rep_Item is
12212 begin
12213 Error_Msg_N ("|type-related representation item not permitted!", N);
12214 end No_Type_Rep_Item;
12216 --------------
12217 -- Too_Late --
12218 --------------
12220 procedure Too_Late is
12221 begin
12222 -- Other compilers seem more relaxed about rep items appearing too
12223 -- late. Since analysis tools typically don't care about rep items
12224 -- anyway, no reason to be too strict about this.
12226 if not Relaxed_RM_Semantics then
12227 Error_Msg_N ("|representation item appears too late!", N);
12228 end if;
12229 end Too_Late;
12231 -- Start of processing for Rep_Item_Too_Late
12233 begin
12234 -- First make sure entity is not frozen (RM 13.1(9))
12236 if Is_Frozen (T)
12238 -- Exclude imported types, which may be frozen if they appear in a
12239 -- representation clause for a local type.
12241 and then not From_Limited_With (T)
12243 -- Exclude generated entities (not coming from source). The common
12244 -- case is when we generate a renaming which prematurely freezes the
12245 -- renamed internal entity, but we still want to be able to set copies
12246 -- of attribute values such as Size/Alignment.
12248 and then Comes_From_Source (T)
12249 then
12250 -- A self-referential aspect is illegal if it forces freezing the
12251 -- entity before the corresponding pragma has been analyzed.
12253 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12254 and then From_Aspect_Specification (N)
12255 then
12256 Error_Msg_NE
12257 ("aspect specification causes premature freezing of&", T, N);
12258 Set_Has_Delayed_Freeze (T, False);
12259 return True;
12260 end if;
12262 Too_Late;
12263 S := First_Subtype (T);
12265 if Present (Freeze_Node (S)) then
12266 if not Relaxed_RM_Semantics then
12267 Error_Msg_NE
12268 ("??no more representation items for }", Freeze_Node (S), S);
12269 end if;
12270 end if;
12272 return True;
12274 -- Check for case of untagged derived type whose parent either has
12275 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12276 -- this case we do not output a Too_Late message, since there is no
12277 -- earlier point where the rep item could be placed to make it legal.
12279 elsif Is_Type (T)
12280 and then not FOnly
12281 and then Is_Derived_Type (T)
12282 and then not Is_Tagged_Type (T)
12283 then
12284 Parent_Type := Etype (Base_Type (T));
12286 if Has_Primitive_Operations (Parent_Type) then
12287 No_Type_Rep_Item;
12289 if not Relaxed_RM_Semantics then
12290 Error_Msg_NE
12291 ("\parent type & has primitive operations!", N, Parent_Type);
12292 end if;
12294 return True;
12296 elsif Is_By_Reference_Type (Parent_Type) then
12297 No_Type_Rep_Item;
12299 if not Relaxed_RM_Semantics then
12300 Error_Msg_NE
12301 ("\parent type & is a by reference type!", N, Parent_Type);
12302 end if;
12304 return True;
12305 end if;
12306 end if;
12308 -- No error, but one more warning to consider. The RM (surprisingly)
12309 -- allows this pattern:
12311 -- type S is ...
12312 -- primitive operations for S
12313 -- type R is new S;
12314 -- rep clause for S
12316 -- Meaning that calls on the primitive operations of S for values of
12317 -- type R may require possibly expensive implicit conversion operations.
12318 -- This is not an error, but is worth a warning.
12320 if not Relaxed_RM_Semantics and then Is_Type (T) then
12321 declare
12322 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12324 begin
12325 if Present (DTL)
12326 and then Has_Primitive_Operations (Base_Type (T))
12328 -- For now, do not generate this warning for the case of aspect
12329 -- specification using Ada 2012 syntax, since we get wrong
12330 -- messages we do not understand. The whole business of derived
12331 -- types and rep items seems a bit confused when aspects are
12332 -- used, since the aspects are not evaluated till freeze time.
12334 and then not From_Aspect_Specification (N)
12335 then
12336 Error_Msg_Sloc := Sloc (DTL);
12337 Error_Msg_N
12338 ("representation item for& appears after derived type "
12339 & "declaration#??", N);
12340 Error_Msg_NE
12341 ("\may result in implicit conversions for primitive "
12342 & "operations of&??", N, T);
12343 Error_Msg_NE
12344 ("\to change representations when called with arguments "
12345 & "of type&??", N, DTL);
12346 end if;
12347 end;
12348 end if;
12350 -- No error, link item into head of chain of rep items for the entity,
12351 -- but avoid chaining if we have an overloadable entity, and the pragma
12352 -- is one that can apply to multiple overloaded entities.
12354 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12355 declare
12356 Pname : constant Name_Id := Pragma_Name (N);
12357 begin
12358 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12359 Name_External, Name_Interface)
12360 then
12361 return False;
12362 end if;
12363 end;
12364 end if;
12366 Record_Rep_Item (T, N);
12367 return False;
12368 end Rep_Item_Too_Late;
12370 -------------------------------------
12371 -- Replace_Type_References_Generic --
12372 -------------------------------------
12374 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12375 TName : constant Name_Id := Chars (T);
12377 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
12378 -- Processes a single node in the traversal procedure below, checking
12379 -- if node N should be replaced, and if so, doing the replacement.
12381 function Visible_Component (Comp : Name_Id) return Entity_Id;
12382 -- Given an identifier in the expression, check whether there is a
12383 -- discriminant or component of the type that is directy visible, and
12384 -- rewrite it as the corresponding selected component of the formal of
12385 -- the subprogram. The entity is located by a sequential search, which
12386 -- seems acceptable given the typical size of component lists and check
12387 -- expressions. Possible optimization ???
12389 ----------------------
12390 -- Replace_Type_Ref --
12391 ----------------------
12393 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
12394 Loc : constant Source_Ptr := Sloc (N);
12396 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
12397 -- Add the proper prefix to a reference to a component of the type
12398 -- when it is not already a selected component.
12400 ----------------
12401 -- Add_Prefix --
12402 ----------------
12404 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12405 begin
12406 Rewrite (Ref,
12407 Make_Selected_Component (Loc,
12408 Prefix => New_Occurrence_Of (T, Loc),
12409 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12410 Replace_Type_Reference (Prefix (Ref));
12411 end Add_Prefix;
12413 -- Local variables
12415 Comp : Entity_Id;
12416 Pref : Node_Id;
12417 Scop : Entity_Id;
12419 -- Start of processing for Replace_Type_Ref
12421 begin
12422 if Nkind (N) = N_Identifier then
12424 -- If not the type name, check whether it is a reference to some
12425 -- other type, which must be frozen before the predicate function
12426 -- is analyzed, i.e. before the freeze node of the type to which
12427 -- the predicate applies.
12429 if Chars (N) /= TName then
12430 if Present (Current_Entity (N))
12431 and then Is_Type (Current_Entity (N))
12432 then
12433 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12434 end if;
12436 -- The components of the type are directly visible and can
12437 -- be referenced without a prefix.
12439 if Nkind (Parent (N)) = N_Selected_Component then
12440 null;
12442 -- In expression C (I), C may be a directly visible function
12443 -- or a visible component that has an array type. Disambiguate
12444 -- by examining the component type.
12446 elsif Nkind (Parent (N)) = N_Indexed_Component
12447 and then N = Prefix (Parent (N))
12448 then
12449 Comp := Visible_Component (Chars (N));
12451 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
12452 Add_Prefix (N, Comp);
12453 end if;
12455 else
12456 Comp := Visible_Component (Chars (N));
12458 if Present (Comp) then
12459 Add_Prefix (N, Comp);
12460 end if;
12461 end if;
12463 return Skip;
12465 -- Otherwise do the replacement and we are done with this node
12467 else
12468 Replace_Type_Reference (N);
12469 return Skip;
12470 end if;
12472 -- Case of selected component (which is what a qualification looks
12473 -- like in the unanalyzed tree, which is what we have.
12475 elsif Nkind (N) = N_Selected_Component then
12477 -- If selector name is not our type, keeping going (we might still
12478 -- have an occurrence of the type in the prefix).
12480 if Nkind (Selector_Name (N)) /= N_Identifier
12481 or else Chars (Selector_Name (N)) /= TName
12482 then
12483 return OK;
12485 -- Selector name is our type, check qualification
12487 else
12488 -- Loop through scopes and prefixes, doing comparison
12490 Scop := Current_Scope;
12491 Pref := Prefix (N);
12492 loop
12493 -- Continue if no more scopes or scope with no name
12495 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
12496 return OK;
12497 end if;
12499 -- Do replace if prefix is an identifier matching the scope
12500 -- that we are currently looking at.
12502 if Nkind (Pref) = N_Identifier
12503 and then Chars (Pref) = Chars (Scop)
12504 then
12505 Replace_Type_Reference (N);
12506 return Skip;
12507 end if;
12509 -- Go check scope above us if prefix is itself of the form
12510 -- of a selected component, whose selector matches the scope
12511 -- we are currently looking at.
12513 if Nkind (Pref) = N_Selected_Component
12514 and then Nkind (Selector_Name (Pref)) = N_Identifier
12515 and then Chars (Selector_Name (Pref)) = Chars (Scop)
12516 then
12517 Scop := Scope (Scop);
12518 Pref := Prefix (Pref);
12520 -- For anything else, we don't have a match, so keep on
12521 -- going, there are still some weird cases where we may
12522 -- still have a replacement within the prefix.
12524 else
12525 return OK;
12526 end if;
12527 end loop;
12528 end if;
12530 -- Continue for any other node kind
12532 else
12533 return OK;
12534 end if;
12535 end Replace_Type_Ref;
12537 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
12539 -----------------------
12540 -- Visible_Component --
12541 -----------------------
12543 function Visible_Component (Comp : Name_Id) return Entity_Id is
12544 E : Entity_Id;
12546 begin
12547 if Ekind (T) /= E_Record_Type then
12548 return Empty;
12550 else
12551 E := First_Entity (T);
12552 while Present (E) loop
12553 if Comes_From_Source (E) and then Chars (E) = Comp then
12554 return E;
12555 end if;
12557 Next_Entity (E);
12558 end loop;
12560 return Empty;
12561 end if;
12562 end Visible_Component;
12564 -- Start of processing for Replace_Type_References_Generic
12566 begin
12567 Replace_Type_Refs (N);
12568 end Replace_Type_References_Generic;
12570 --------------------------------
12571 -- Resolve_Aspect_Expressions --
12572 --------------------------------
12574 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12575 ASN : Node_Id;
12576 A_Id : Aspect_Id;
12577 Expr : Node_Id;
12579 function Resolve_Name (N : Node_Id) return Traverse_Result;
12580 -- Verify that all identifiers in the expression, with the exception
12581 -- of references to the current entity, denote visible entities. This
12582 -- is done only to detect visibility errors, as the expression will be
12583 -- properly analyzed/expanded during analysis of the predicate function
12584 -- body. We omit quantified expressions from this test, given that they
12585 -- introduce a local identifier that would require proper expansion to
12586 -- handle properly.
12588 ------------------
12589 -- Resolve_Name --
12590 ------------------
12592 function Resolve_Name (N : Node_Id) return Traverse_Result is
12593 begin
12594 if Nkind (N) = N_Selected_Component then
12595 if Nkind (Prefix (N)) = N_Identifier
12596 and then Chars (Prefix (N)) /= Chars (E)
12597 then
12598 Find_Selected_Component (N);
12599 end if;
12601 return Skip;
12603 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
12604 Find_Direct_Name (N);
12605 Set_Entity (N, Empty);
12607 elsif Nkind (N) = N_Quantified_Expression then
12608 return Skip;
12609 end if;
12611 return OK;
12612 end Resolve_Name;
12614 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12616 -- Start of processing for Resolve_Aspect_Expressions
12618 begin
12619 ASN := First_Rep_Item (E);
12620 while Present (ASN) loop
12621 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12622 A_Id := Get_Aspect_Id (ASN);
12623 Expr := Expression (ASN);
12625 case A_Id is
12627 -- For now we only deal with aspects that do not generate
12628 -- subprograms, or that may mention current instances of
12629 -- types. These will require special handling (???TBD).
12631 when Aspect_Predicate |
12632 Aspect_Predicate_Failure |
12633 Aspect_Invariant =>
12634 null;
12636 when Aspect_Dynamic_Predicate |
12637 Aspect_Static_Predicate =>
12639 -- Build predicate function specification and preanalyze
12640 -- expression after type replacement.
12642 if No (Predicate_Function (E)) then
12643 declare
12644 FDecl : constant Node_Id :=
12645 Build_Predicate_Function_Declaration (E);
12646 pragma Unreferenced (FDecl);
12647 begin
12648 Resolve_Aspect_Expression (Expr);
12649 end;
12650 end if;
12652 when Pre_Post_Aspects =>
12653 null;
12655 when Aspect_Iterable =>
12656 if Nkind (Expr) = N_Aggregate then
12657 declare
12658 Assoc : Node_Id;
12660 begin
12661 Assoc := First (Component_Associations (Expr));
12662 while Present (Assoc) loop
12663 Find_Direct_Name (Expression (Assoc));
12664 Next (Assoc);
12665 end loop;
12666 end;
12667 end if;
12669 when others =>
12670 if Present (Expr) then
12671 case Aspect_Argument (A_Id) is
12672 when Expression | Optional_Expression =>
12673 Analyze_And_Resolve (Expression (ASN));
12675 when Name | Optional_Name =>
12676 if Nkind (Expr) = N_Identifier then
12677 Find_Direct_Name (Expr);
12679 elsif Nkind (Expr) = N_Selected_Component then
12680 Find_Selected_Component (Expr);
12682 else
12683 null;
12684 end if;
12685 end case;
12686 end if;
12687 end case;
12688 end if;
12690 ASN := Next_Rep_Item (ASN);
12691 end loop;
12692 end Resolve_Aspect_Expressions;
12694 -------------------------
12695 -- Same_Representation --
12696 -------------------------
12698 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12699 T1 : constant Entity_Id := Underlying_Type (Typ1);
12700 T2 : constant Entity_Id := Underlying_Type (Typ2);
12702 begin
12703 -- A quick check, if base types are the same, then we definitely have
12704 -- the same representation, because the subtype specific representation
12705 -- attributes (Size and Alignment) do not affect representation from
12706 -- the point of view of this test.
12708 if Base_Type (T1) = Base_Type (T2) then
12709 return True;
12711 elsif Is_Private_Type (Base_Type (T2))
12712 and then Base_Type (T1) = Full_View (Base_Type (T2))
12713 then
12714 return True;
12715 end if;
12717 -- Tagged types never have differing representations
12719 if Is_Tagged_Type (T1) then
12720 return True;
12721 end if;
12723 -- Representations are definitely different if conventions differ
12725 if Convention (T1) /= Convention (T2) then
12726 return False;
12727 end if;
12729 -- Representations are different if component alignments or scalar
12730 -- storage orders differ.
12732 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12733 and then
12734 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12735 and then
12736 (Component_Alignment (T1) /= Component_Alignment (T2)
12737 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12738 then
12739 return False;
12740 end if;
12742 -- For arrays, the only real issue is component size. If we know the
12743 -- component size for both arrays, and it is the same, then that's
12744 -- good enough to know we don't have a change of representation.
12746 if Is_Array_Type (T1) then
12747 if Known_Component_Size (T1)
12748 and then Known_Component_Size (T2)
12749 and then Component_Size (T1) = Component_Size (T2)
12750 then
12751 return True;
12752 end if;
12753 end if;
12755 -- Types definitely have same representation if neither has non-standard
12756 -- representation since default representations are always consistent.
12757 -- If only one has non-standard representation, and the other does not,
12758 -- then we consider that they do not have the same representation. They
12759 -- might, but there is no way of telling early enough.
12761 if Has_Non_Standard_Rep (T1) then
12762 if not Has_Non_Standard_Rep (T2) then
12763 return False;
12764 end if;
12765 else
12766 return not Has_Non_Standard_Rep (T2);
12767 end if;
12769 -- Here the two types both have non-standard representation, and we need
12770 -- to determine if they have the same non-standard representation.
12772 -- For arrays, we simply need to test if the component sizes are the
12773 -- same. Pragma Pack is reflected in modified component sizes, so this
12774 -- check also deals with pragma Pack.
12776 if Is_Array_Type (T1) then
12777 return Component_Size (T1) = Component_Size (T2);
12779 -- Tagged types always have the same representation, because it is not
12780 -- possible to specify different representations for common fields.
12782 elsif Is_Tagged_Type (T1) then
12783 return True;
12785 -- Case of record types
12787 elsif Is_Record_Type (T1) then
12789 -- Packed status must conform
12791 if Is_Packed (T1) /= Is_Packed (T2) then
12792 return False;
12794 -- Otherwise we must check components. Typ2 maybe a constrained
12795 -- subtype with fewer components, so we compare the components
12796 -- of the base types.
12798 else
12799 Record_Case : declare
12800 CD1, CD2 : Entity_Id;
12802 function Same_Rep return Boolean;
12803 -- CD1 and CD2 are either components or discriminants. This
12804 -- function tests whether they have the same representation.
12806 --------------
12807 -- Same_Rep --
12808 --------------
12810 function Same_Rep return Boolean is
12811 begin
12812 if No (Component_Clause (CD1)) then
12813 return No (Component_Clause (CD2));
12814 else
12815 -- Note: at this point, component clauses have been
12816 -- normalized to the default bit order, so that the
12817 -- comparison of Component_Bit_Offsets is meaningful.
12819 return
12820 Present (Component_Clause (CD2))
12821 and then
12822 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12823 and then
12824 Esize (CD1) = Esize (CD2);
12825 end if;
12826 end Same_Rep;
12828 -- Start of processing for Record_Case
12830 begin
12831 if Has_Discriminants (T1) then
12833 -- The number of discriminants may be different if the
12834 -- derived type has fewer (constrained by values). The
12835 -- invisible discriminants retain the representation of
12836 -- the original, so the discrepancy does not per se
12837 -- indicate a different representation.
12839 CD1 := First_Discriminant (T1);
12840 CD2 := First_Discriminant (T2);
12841 while Present (CD1) and then Present (CD2) loop
12842 if not Same_Rep then
12843 return False;
12844 else
12845 Next_Discriminant (CD1);
12846 Next_Discriminant (CD2);
12847 end if;
12848 end loop;
12849 end if;
12851 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12852 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12853 while Present (CD1) loop
12854 if not Same_Rep then
12855 return False;
12856 else
12857 Next_Component (CD1);
12858 Next_Component (CD2);
12859 end if;
12860 end loop;
12862 return True;
12863 end Record_Case;
12864 end if;
12866 -- For enumeration types, we must check each literal to see if the
12867 -- representation is the same. Note that we do not permit enumeration
12868 -- representation clauses for Character and Wide_Character, so these
12869 -- cases were already dealt with.
12871 elsif Is_Enumeration_Type (T1) then
12872 Enumeration_Case : declare
12873 L1, L2 : Entity_Id;
12875 begin
12876 L1 := First_Literal (T1);
12877 L2 := First_Literal (T2);
12878 while Present (L1) loop
12879 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12880 return False;
12881 else
12882 Next_Literal (L1);
12883 Next_Literal (L2);
12884 end if;
12885 end loop;
12887 return True;
12888 end Enumeration_Case;
12890 -- Any other types have the same representation for these purposes
12892 else
12893 return True;
12894 end if;
12895 end Same_Representation;
12897 --------------------------------
12898 -- Resolve_Iterable_Operation --
12899 --------------------------------
12901 procedure Resolve_Iterable_Operation
12902 (N : Node_Id;
12903 Cursor : Entity_Id;
12904 Typ : Entity_Id;
12905 Nam : Name_Id)
12907 Ent : Entity_Id;
12908 F1 : Entity_Id;
12909 F2 : Entity_Id;
12911 begin
12912 if not Is_Overloaded (N) then
12913 if not Is_Entity_Name (N)
12914 or else Ekind (Entity (N)) /= E_Function
12915 or else Scope (Entity (N)) /= Scope (Typ)
12916 or else No (First_Formal (Entity (N)))
12917 or else Etype (First_Formal (Entity (N))) /= Typ
12918 then
12919 Error_Msg_N ("iterable primitive must be local function name "
12920 & "whose first formal is an iterable type", N);
12921 return;
12922 end if;
12924 Ent := Entity (N);
12925 F1 := First_Formal (Ent);
12926 if Nam = Name_First then
12928 -- First (Container) => Cursor
12930 if Etype (Ent) /= Cursor then
12931 Error_Msg_N ("primitive for First must yield a curosr", N);
12932 end if;
12934 elsif Nam = Name_Next then
12936 -- Next (Container, Cursor) => Cursor
12938 F2 := Next_Formal (F1);
12940 if Etype (F2) /= Cursor
12941 or else Etype (Ent) /= Cursor
12942 or else Present (Next_Formal (F2))
12943 then
12944 Error_Msg_N ("no match for Next iterable primitive", N);
12945 end if;
12947 elsif Nam = Name_Has_Element then
12949 -- Has_Element (Container, Cursor) => Boolean
12951 F2 := Next_Formal (F1);
12952 if Etype (F2) /= Cursor
12953 or else Etype (Ent) /= Standard_Boolean
12954 or else Present (Next_Formal (F2))
12955 then
12956 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12957 end if;
12959 elsif Nam = Name_Element then
12960 F2 := Next_Formal (F1);
12962 if No (F2)
12963 or else Etype (F2) /= Cursor
12964 or else Present (Next_Formal (F2))
12965 then
12966 Error_Msg_N ("no match for Element iterable primitive", N);
12967 end if;
12968 null;
12970 else
12971 raise Program_Error;
12972 end if;
12974 else
12975 -- Overloaded case: find subprogram with proper signature.
12976 -- Caller will report error if no match is found.
12978 declare
12979 I : Interp_Index;
12980 It : Interp;
12982 begin
12983 Get_First_Interp (N, I, It);
12984 while Present (It.Typ) loop
12985 if Ekind (It.Nam) = E_Function
12986 and then Scope (It.Nam) = Scope (Typ)
12987 and then Etype (First_Formal (It.Nam)) = Typ
12988 then
12989 F1 := First_Formal (It.Nam);
12991 if Nam = Name_First then
12992 if Etype (It.Nam) = Cursor
12993 and then No (Next_Formal (F1))
12994 then
12995 Set_Entity (N, It.Nam);
12996 exit;
12997 end if;
12999 elsif Nam = Name_Next then
13000 F2 := Next_Formal (F1);
13002 if Present (F2)
13003 and then No (Next_Formal (F2))
13004 and then Etype (F2) = Cursor
13005 and then Etype (It.Nam) = Cursor
13006 then
13007 Set_Entity (N, It.Nam);
13008 exit;
13009 end if;
13011 elsif Nam = Name_Has_Element then
13012 F2 := Next_Formal (F1);
13014 if Present (F2)
13015 and then No (Next_Formal (F2))
13016 and then Etype (F2) = Cursor
13017 and then Etype (It.Nam) = Standard_Boolean
13018 then
13019 Set_Entity (N, It.Nam);
13020 F2 := Next_Formal (F1);
13021 exit;
13022 end if;
13024 elsif Nam = Name_Element then
13025 F2 := Next_Formal (F1);
13027 if Present (F2)
13028 and then No (Next_Formal (F2))
13029 and then Etype (F2) = Cursor
13030 then
13031 Set_Entity (N, It.Nam);
13032 exit;
13033 end if;
13034 end if;
13035 end if;
13037 Get_Next_Interp (I, It);
13038 end loop;
13039 end;
13040 end if;
13041 end Resolve_Iterable_Operation;
13043 ----------------
13044 -- Set_Biased --
13045 ----------------
13047 procedure Set_Biased
13048 (E : Entity_Id;
13049 N : Node_Id;
13050 Msg : String;
13051 Biased : Boolean := True)
13053 begin
13054 if Biased then
13055 Set_Has_Biased_Representation (E);
13057 if Warn_On_Biased_Representation then
13058 Error_Msg_NE
13059 ("?B?" & Msg & " forces biased representation for&", N, E);
13060 end if;
13061 end if;
13062 end Set_Biased;
13064 --------------------
13065 -- Set_Enum_Esize --
13066 --------------------
13068 procedure Set_Enum_Esize (T : Entity_Id) is
13069 Lo : Uint;
13070 Hi : Uint;
13071 Sz : Nat;
13073 begin
13074 Init_Alignment (T);
13076 -- Find the minimum standard size (8,16,32,64) that fits
13078 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13079 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13081 if Lo < 0 then
13082 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
13083 Sz := Standard_Character_Size; -- May be > 8 on some targets
13085 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13086 Sz := 16;
13088 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13089 Sz := 32;
13091 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13092 Sz := 64;
13093 end if;
13095 else
13096 if Hi < Uint_2**08 then
13097 Sz := Standard_Character_Size; -- May be > 8 on some targets
13099 elsif Hi < Uint_2**16 then
13100 Sz := 16;
13102 elsif Hi < Uint_2**32 then
13103 Sz := 32;
13105 else pragma Assert (Hi < Uint_2**63);
13106 Sz := 64;
13107 end if;
13108 end if;
13110 -- That minimum is the proper size unless we have a foreign convention
13111 -- and the size required is 32 or less, in which case we bump the size
13112 -- up to 32. This is required for C and C++ and seems reasonable for
13113 -- all other foreign conventions.
13115 if Has_Foreign_Convention (T)
13116 and then Esize (T) < Standard_Integer_Size
13118 -- Don't do this if Short_Enums on target
13120 and then not Target_Short_Enums
13121 then
13122 Init_Esize (T, Standard_Integer_Size);
13123 else
13124 Init_Esize (T, Sz);
13125 end if;
13126 end Set_Enum_Esize;
13128 -----------------------------
13129 -- Uninstall_Discriminants --
13130 -----------------------------
13132 procedure Uninstall_Discriminants (E : Entity_Id) is
13133 Disc : Entity_Id;
13134 Prev : Entity_Id;
13135 Outer : Entity_Id;
13137 begin
13138 -- Discriminants have been made visible for type declarations and
13139 -- protected type declarations, not for subtype declarations.
13141 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13142 Disc := First_Discriminant (E);
13143 while Present (Disc) loop
13144 if Disc /= Current_Entity (Disc) then
13145 Prev := Current_Entity (Disc);
13146 while Present (Prev)
13147 and then Present (Homonym (Prev))
13148 and then Homonym (Prev) /= Disc
13149 loop
13150 Prev := Homonym (Prev);
13151 end loop;
13152 else
13153 Prev := Empty;
13154 end if;
13156 Set_Is_Immediately_Visible (Disc, False);
13158 Outer := Homonym (Disc);
13159 while Present (Outer) and then Scope (Outer) = E loop
13160 Outer := Homonym (Outer);
13161 end loop;
13163 -- Reset homonym link of other entities, but do not modify link
13164 -- between entities in current scope, so that the back end can
13165 -- have a proper count of local overloadings.
13167 if No (Prev) then
13168 Set_Name_Entity_Id (Chars (Disc), Outer);
13170 elsif Scope (Prev) /= Scope (Disc) then
13171 Set_Homonym (Prev, Outer);
13172 end if;
13174 Next_Discriminant (Disc);
13175 end loop;
13176 end if;
13177 end Uninstall_Discriminants;
13179 -------------------------------------------
13180 -- Uninstall_Discriminants_And_Pop_Scope --
13181 -------------------------------------------
13183 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13184 begin
13185 if Has_Discriminants (E) then
13186 Uninstall_Discriminants (E);
13187 Pop_Scope;
13188 end if;
13189 end Uninstall_Discriminants_And_Pop_Scope;
13191 ------------------------------
13192 -- Validate_Address_Clauses --
13193 ------------------------------
13195 procedure Validate_Address_Clauses is
13196 function Offset_Value (Expr : Node_Id) return Uint;
13197 -- Given an Address attribute reference, return the value in bits of its
13198 -- offset from the first bit of the underlying entity, or 0 if it is not
13199 -- known at compile time.
13201 ------------------
13202 -- Offset_Value --
13203 ------------------
13205 function Offset_Value (Expr : Node_Id) return Uint is
13206 N : Node_Id := Prefix (Expr);
13207 Off : Uint;
13208 Val : Uint := Uint_0;
13210 begin
13211 -- Climb the prefix chain and compute the cumulative offset
13213 loop
13214 if Is_Entity_Name (N) then
13215 return Val;
13217 elsif Nkind (N) = N_Selected_Component then
13218 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13219 if Off /= No_Uint and then Off >= Uint_0 then
13220 Val := Val + Off;
13221 N := Prefix (N);
13222 else
13223 return Uint_0;
13224 end if;
13226 elsif Nkind (N) = N_Indexed_Component then
13227 Off := Indexed_Component_Bit_Offset (N);
13228 if Off /= No_Uint then
13229 Val := Val + Off;
13230 N := Prefix (N);
13231 else
13232 return Uint_0;
13233 end if;
13235 else
13236 return Uint_0;
13237 end if;
13238 end loop;
13239 end Offset_Value;
13241 -- Start of processing for Validate_Address_Clauses
13243 begin
13244 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13245 declare
13246 ACCR : Address_Clause_Check_Record
13247 renames Address_Clause_Checks.Table (J);
13249 Expr : Node_Id;
13251 X_Alignment : Uint;
13252 Y_Alignment : Uint;
13254 X_Size : Uint;
13255 Y_Size : Uint;
13257 X_Offs : Uint;
13259 begin
13260 -- Skip processing of this entry if warning already posted
13262 if not Address_Warning_Posted (ACCR.N) then
13263 Expr := Original_Node (Expression (ACCR.N));
13265 -- Get alignments, sizes and offset, if any
13267 X_Alignment := Alignment (ACCR.X);
13268 X_Size := Esize (ACCR.X);
13270 if Present (ACCR.Y) then
13271 Y_Alignment := Alignment (ACCR.Y);
13272 Y_Size := Esize (ACCR.Y);
13273 end if;
13275 if ACCR.Off
13276 and then Nkind (Expr) = N_Attribute_Reference
13277 and then Attribute_Name (Expr) = Name_Address
13278 then
13279 X_Offs := Offset_Value (Expr);
13280 else
13281 X_Offs := Uint_0;
13282 end if;
13284 -- Check for known value not multiple of alignment
13286 if No (ACCR.Y) then
13287 if not Alignment_Checks_Suppressed (ACCR.X)
13288 and then X_Alignment /= 0
13289 and then ACCR.A mod X_Alignment /= 0
13290 then
13291 Error_Msg_NE
13292 ("??specified address for& is inconsistent with "
13293 & "alignment", ACCR.N, ACCR.X);
13294 Error_Msg_N
13295 ("\??program execution may be erroneous (RM 13.3(27))",
13296 ACCR.N);
13298 Error_Msg_Uint_1 := X_Alignment;
13299 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13300 end if;
13302 -- Check for large object overlaying smaller one
13304 elsif Y_Size > Uint_0
13305 and then X_Size > Uint_0
13306 and then X_Offs + X_Size > Y_Size
13307 then
13308 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
13309 Error_Msg_N
13310 ("\??program execution may be erroneous", ACCR.N);
13312 Error_Msg_Uint_1 := X_Size;
13313 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13315 Error_Msg_Uint_1 := Y_Size;
13316 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
13318 if Y_Size >= X_Size then
13319 Error_Msg_Uint_1 := X_Offs;
13320 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
13321 end if;
13323 -- Check for inadequate alignment, both of the base object
13324 -- and of the offset, if any. We only do this check if the
13325 -- run-time Alignment_Check is active. No point in warning
13326 -- if this check has been suppressed (or is suppressed by
13327 -- default in the non-strict alignment machine case).
13329 -- Note: we do not check the alignment if we gave a size
13330 -- warning, since it would likely be redundant.
13332 elsif not Alignment_Checks_Suppressed (ACCR.X)
13333 and then Y_Alignment /= Uint_0
13334 and then
13335 (Y_Alignment < X_Alignment
13336 or else
13337 (ACCR.Off
13338 and then Nkind (Expr) = N_Attribute_Reference
13339 and then Attribute_Name (Expr) = Name_Address
13340 and then Has_Compatible_Alignment
13341 (ACCR.X, Prefix (Expr), True) /=
13342 Known_Compatible))
13343 then
13344 Error_Msg_NE
13345 ("??specified address for& may be inconsistent with "
13346 & "alignment", ACCR.N, ACCR.X);
13347 Error_Msg_N
13348 ("\??program execution may be erroneous (RM 13.3(27))",
13349 ACCR.N);
13351 Error_Msg_Uint_1 := X_Alignment;
13352 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13354 Error_Msg_Uint_1 := Y_Alignment;
13355 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13357 if Y_Alignment >= X_Alignment then
13358 Error_Msg_N
13359 ("\??but offset is not multiple of alignment", ACCR.N);
13360 end if;
13361 end if;
13362 end if;
13363 end;
13364 end loop;
13365 end Validate_Address_Clauses;
13367 -----------------------------------------
13368 -- Validate_Compile_Time_Warning_Error --
13369 -----------------------------------------
13371 procedure Validate_Compile_Time_Warning_Error (N : Node_Id) is
13372 begin
13373 Compile_Time_Warnings_Errors.Append
13374 (New_Val => CTWE_Entry'(Eloc => Sloc (N),
13375 Scope => Current_Scope,
13376 Prag => N));
13377 end Validate_Compile_Time_Warning_Error;
13379 ------------------------------------------
13380 -- Validate_Compile_Time_Warning_Errors --
13381 ------------------------------------------
13383 procedure Validate_Compile_Time_Warning_Errors is
13384 procedure Set_Scope (S : Entity_Id);
13385 -- Install all enclosing scopes of S along with S itself
13387 procedure Unset_Scope (S : Entity_Id);
13388 -- Uninstall all enclosing scopes of S along with S itself
13390 ---------------
13391 -- Set_Scope --
13392 ---------------
13394 procedure Set_Scope (S : Entity_Id) is
13395 begin
13396 if S /= Standard_Standard then
13397 Set_Scope (Scope (S));
13398 end if;
13400 Push_Scope (S);
13401 end Set_Scope;
13403 -----------------
13404 -- Unset_Scope --
13405 -----------------
13407 procedure Unset_Scope (S : Entity_Id) is
13408 begin
13409 if S /= Standard_Standard then
13410 Unset_Scope (Scope (S));
13411 end if;
13413 Pop_Scope;
13414 end Unset_Scope;
13416 -- Start of processing for Validate_Compile_Time_Warning_Errors
13418 begin
13419 Expander_Mode_Save_And_Set (False);
13420 In_Compile_Time_Warning_Or_Error := True;
13422 for N in Compile_Time_Warnings_Errors.First ..
13423 Compile_Time_Warnings_Errors.Last
13424 loop
13425 declare
13426 T : CTWE_Entry renames Compile_Time_Warnings_Errors.Table (N);
13428 begin
13429 Set_Scope (T.Scope);
13430 Reset_Analyzed_Flags (T.Prag);
13431 Process_Compile_Time_Warning_Or_Error (T.Prag, T.Eloc);
13432 Unset_Scope (T.Scope);
13433 end;
13434 end loop;
13436 In_Compile_Time_Warning_Or_Error := False;
13437 Expander_Mode_Restore;
13438 end Validate_Compile_Time_Warning_Errors;
13440 ---------------------------
13441 -- Validate_Independence --
13442 ---------------------------
13444 procedure Validate_Independence is
13445 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13446 N : Node_Id;
13447 E : Entity_Id;
13448 IC : Boolean;
13449 Comp : Entity_Id;
13450 Addr : Node_Id;
13451 P : Node_Id;
13453 procedure Check_Array_Type (Atyp : Entity_Id);
13454 -- Checks if the array type Atyp has independent components, and
13455 -- if not, outputs an appropriate set of error messages.
13457 procedure No_Independence;
13458 -- Output message that independence cannot be guaranteed
13460 function OK_Component (C : Entity_Id) return Boolean;
13461 -- Checks one component to see if it is independently accessible, and
13462 -- if so yields True, otherwise yields False if independent access
13463 -- cannot be guaranteed. This is a conservative routine, it only
13464 -- returns True if it knows for sure, it returns False if it knows
13465 -- there is a problem, or it cannot be sure there is no problem.
13467 procedure Reason_Bad_Component (C : Entity_Id);
13468 -- Outputs continuation message if a reason can be determined for
13469 -- the component C being bad.
13471 ----------------------
13472 -- Check_Array_Type --
13473 ----------------------
13475 procedure Check_Array_Type (Atyp : Entity_Id) is
13476 Ctyp : constant Entity_Id := Component_Type (Atyp);
13478 begin
13479 -- OK if no alignment clause, no pack, and no component size
13481 if not Has_Component_Size_Clause (Atyp)
13482 and then not Has_Alignment_Clause (Atyp)
13483 and then not Is_Packed (Atyp)
13484 then
13485 return;
13486 end if;
13488 -- Case of component size is greater than or equal to 64 and the
13489 -- alignment of the array is at least as large as the alignment
13490 -- of the component. We are definitely OK in this situation.
13492 if Known_Component_Size (Atyp)
13493 and then Component_Size (Atyp) >= 64
13494 and then Known_Alignment (Atyp)
13495 and then Known_Alignment (Ctyp)
13496 and then Alignment (Atyp) >= Alignment (Ctyp)
13497 then
13498 return;
13499 end if;
13501 -- Check actual component size
13503 if not Known_Component_Size (Atyp)
13504 or else not (Addressable (Component_Size (Atyp))
13505 and then Component_Size (Atyp) < 64)
13506 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13507 then
13508 No_Independence;
13510 -- Bad component size, check reason
13512 if Has_Component_Size_Clause (Atyp) then
13513 P := Get_Attribute_Definition_Clause
13514 (Atyp, Attribute_Component_Size);
13516 if Present (P) then
13517 Error_Msg_Sloc := Sloc (P);
13518 Error_Msg_N ("\because of Component_Size clause#", N);
13519 return;
13520 end if;
13521 end if;
13523 if Is_Packed (Atyp) then
13524 P := Get_Rep_Pragma (Atyp, Name_Pack);
13526 if Present (P) then
13527 Error_Msg_Sloc := Sloc (P);
13528 Error_Msg_N ("\because of pragma Pack#", N);
13529 return;
13530 end if;
13531 end if;
13533 -- No reason found, just return
13535 return;
13536 end if;
13538 -- Array type is OK independence-wise
13540 return;
13541 end Check_Array_Type;
13543 ---------------------
13544 -- No_Independence --
13545 ---------------------
13547 procedure No_Independence is
13548 begin
13549 if Pragma_Name (N) = Name_Independent then
13550 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
13551 else
13552 Error_Msg_NE
13553 ("independent components cannot be guaranteed for&", N, E);
13554 end if;
13555 end No_Independence;
13557 ------------------
13558 -- OK_Component --
13559 ------------------
13561 function OK_Component (C : Entity_Id) return Boolean is
13562 Rec : constant Entity_Id := Scope (C);
13563 Ctyp : constant Entity_Id := Etype (C);
13565 begin
13566 -- OK if no component clause, no Pack, and no alignment clause
13568 if No (Component_Clause (C))
13569 and then not Is_Packed (Rec)
13570 and then not Has_Alignment_Clause (Rec)
13571 then
13572 return True;
13573 end if;
13575 -- Here we look at the actual component layout. A component is
13576 -- addressable if its size is a multiple of the Esize of the
13577 -- component type, and its starting position in the record has
13578 -- appropriate alignment, and the record itself has appropriate
13579 -- alignment to guarantee the component alignment.
13581 -- Make sure sizes are static, always assume the worst for any
13582 -- cases where we cannot check static values.
13584 if not (Known_Static_Esize (C)
13585 and then
13586 Known_Static_Esize (Ctyp))
13587 then
13588 return False;
13589 end if;
13591 -- Size of component must be addressable or greater than 64 bits
13592 -- and a multiple of bytes.
13594 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
13595 return False;
13596 end if;
13598 -- Check size is proper multiple
13600 if Esize (C) mod Esize (Ctyp) /= 0 then
13601 return False;
13602 end if;
13604 -- Check alignment of component is OK
13606 if not Known_Component_Bit_Offset (C)
13607 or else Component_Bit_Offset (C) < Uint_0
13608 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13609 then
13610 return False;
13611 end if;
13613 -- Check alignment of record type is OK
13615 if not Known_Alignment (Rec)
13616 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13617 then
13618 return False;
13619 end if;
13621 -- All tests passed, component is addressable
13623 return True;
13624 end OK_Component;
13626 --------------------------
13627 -- Reason_Bad_Component --
13628 --------------------------
13630 procedure Reason_Bad_Component (C : Entity_Id) is
13631 Rec : constant Entity_Id := Scope (C);
13632 Ctyp : constant Entity_Id := Etype (C);
13634 begin
13635 -- If component clause present assume that's the problem
13637 if Present (Component_Clause (C)) then
13638 Error_Msg_Sloc := Sloc (Component_Clause (C));
13639 Error_Msg_N ("\because of Component_Clause#", N);
13640 return;
13641 end if;
13643 -- If pragma Pack clause present, assume that's the problem
13645 if Is_Packed (Rec) then
13646 P := Get_Rep_Pragma (Rec, Name_Pack);
13648 if Present (P) then
13649 Error_Msg_Sloc := Sloc (P);
13650 Error_Msg_N ("\because of pragma Pack#", N);
13651 return;
13652 end if;
13653 end if;
13655 -- See if record has bad alignment clause
13657 if Has_Alignment_Clause (Rec)
13658 and then Known_Alignment (Rec)
13659 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13660 then
13661 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13663 if Present (P) then
13664 Error_Msg_Sloc := Sloc (P);
13665 Error_Msg_N ("\because of Alignment clause#", N);
13666 end if;
13667 end if;
13669 -- Couldn't find a reason, so return without a message
13671 return;
13672 end Reason_Bad_Component;
13674 -- Start of processing for Validate_Independence
13676 begin
13677 for J in Independence_Checks.First .. Independence_Checks.Last loop
13678 N := Independence_Checks.Table (J).N;
13679 E := Independence_Checks.Table (J).E;
13680 IC := Pragma_Name (N) = Name_Independent_Components;
13682 -- Deal with component case
13684 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13685 if not OK_Component (E) then
13686 No_Independence;
13687 Reason_Bad_Component (E);
13688 goto Continue;
13689 end if;
13690 end if;
13692 -- Deal with record with Independent_Components
13694 if IC and then Is_Record_Type (E) then
13695 Comp := First_Component_Or_Discriminant (E);
13696 while Present (Comp) loop
13697 if not OK_Component (Comp) then
13698 No_Independence;
13699 Reason_Bad_Component (Comp);
13700 goto Continue;
13701 end if;
13703 Next_Component_Or_Discriminant (Comp);
13704 end loop;
13705 end if;
13707 -- Deal with address clause case
13709 if Is_Object (E) then
13710 Addr := Address_Clause (E);
13712 if Present (Addr) then
13713 No_Independence;
13714 Error_Msg_Sloc := Sloc (Addr);
13715 Error_Msg_N ("\because of Address clause#", N);
13716 goto Continue;
13717 end if;
13718 end if;
13720 -- Deal with independent components for array type
13722 if IC and then Is_Array_Type (E) then
13723 Check_Array_Type (E);
13724 end if;
13726 -- Deal with independent components for array object
13728 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13729 Check_Array_Type (Etype (E));
13730 end if;
13732 <<Continue>> null;
13733 end loop;
13734 end Validate_Independence;
13736 ------------------------------
13737 -- Validate_Iterable_Aspect --
13738 ------------------------------
13740 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
13741 Assoc : Node_Id;
13742 Expr : Node_Id;
13744 Prim : Node_Id;
13745 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
13747 First_Id : Entity_Id;
13748 Next_Id : Entity_Id;
13749 Has_Element_Id : Entity_Id;
13750 Element_Id : Entity_Id;
13752 begin
13753 -- If previous error aspect is unusable
13755 if Cursor = Any_Type then
13756 return;
13757 end if;
13759 First_Id := Empty;
13760 Next_Id := Empty;
13761 Has_Element_Id := Empty;
13762 Element_Id := Empty;
13764 -- Each expression must resolve to a function with the proper signature
13766 Assoc := First (Component_Associations (Expression (ASN)));
13767 while Present (Assoc) loop
13768 Expr := Expression (Assoc);
13769 Analyze (Expr);
13771 Prim := First (Choices (Assoc));
13773 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
13774 Error_Msg_N ("illegal name in association", Prim);
13776 elsif Chars (Prim) = Name_First then
13777 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
13778 First_Id := Entity (Expr);
13780 elsif Chars (Prim) = Name_Next then
13781 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
13782 Next_Id := Entity (Expr);
13784 elsif Chars (Prim) = Name_Has_Element then
13785 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
13786 Has_Element_Id := Entity (Expr);
13788 elsif Chars (Prim) = Name_Element then
13789 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
13790 Element_Id := Entity (Expr);
13792 else
13793 Error_Msg_N ("invalid name for iterable function", Prim);
13794 end if;
13796 Next (Assoc);
13797 end loop;
13799 if No (First_Id) then
13800 Error_Msg_N ("match for First primitive not found", ASN);
13802 elsif No (Next_Id) then
13803 Error_Msg_N ("match for Next primitive not found", ASN);
13805 elsif No (Has_Element_Id) then
13806 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13808 elsif No (Element_Id) then
13809 null; -- Optional.
13810 end if;
13811 end Validate_Iterable_Aspect;
13813 -----------------------------------
13814 -- Validate_Unchecked_Conversion --
13815 -----------------------------------
13817 procedure Validate_Unchecked_Conversion
13818 (N : Node_Id;
13819 Act_Unit : Entity_Id)
13821 Source : Entity_Id;
13822 Target : Entity_Id;
13823 Vnode : Node_Id;
13825 begin
13826 -- Obtain source and target types. Note that we call Ancestor_Subtype
13827 -- here because the processing for generic instantiation always makes
13828 -- subtypes, and we want the original frozen actual types.
13830 -- If we are dealing with private types, then do the check on their
13831 -- fully declared counterparts if the full declarations have been
13832 -- encountered (they don't have to be visible, but they must exist).
13834 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13836 if Is_Private_Type (Source)
13837 and then Present (Underlying_Type (Source))
13838 then
13839 Source := Underlying_Type (Source);
13840 end if;
13842 Target := Ancestor_Subtype (Etype (Act_Unit));
13844 -- If either type is generic, the instantiation happens within a generic
13845 -- unit, and there is nothing to check. The proper check will happen
13846 -- when the enclosing generic is instantiated.
13848 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13849 return;
13850 end if;
13852 if Is_Private_Type (Target)
13853 and then Present (Underlying_Type (Target))
13854 then
13855 Target := Underlying_Type (Target);
13856 end if;
13858 -- Source may be unconstrained array, but not target, except in relaxed
13859 -- semantics mode.
13861 if Is_Array_Type (Target)
13862 and then not Is_Constrained (Target)
13863 and then not Relaxed_RM_Semantics
13864 then
13865 Error_Msg_N
13866 ("unchecked conversion to unconstrained array not allowed", N);
13867 return;
13868 end if;
13870 -- Warn if conversion between two different convention pointers
13872 if Is_Access_Type (Target)
13873 and then Is_Access_Type (Source)
13874 and then Convention (Target) /= Convention (Source)
13875 and then Warn_On_Unchecked_Conversion
13876 then
13877 -- Give warnings for subprogram pointers only on most targets
13879 if Is_Access_Subprogram_Type (Target)
13880 or else Is_Access_Subprogram_Type (Source)
13881 then
13882 Error_Msg_N
13883 ("?z?conversion between pointers with different conventions!",
13885 end if;
13886 end if;
13888 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13889 -- warning when compiling GNAT-related sources.
13891 if Warn_On_Unchecked_Conversion
13892 and then not In_Predefined_Unit (N)
13893 and then RTU_Loaded (Ada_Calendar)
13894 and then (Chars (Source) = Name_Time
13895 or else
13896 Chars (Target) = Name_Time)
13897 then
13898 -- If Ada.Calendar is loaded and the name of one of the operands is
13899 -- Time, there is a good chance that this is Ada.Calendar.Time.
13901 declare
13902 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13903 begin
13904 pragma Assert (Present (Calendar_Time));
13906 if Source = Calendar_Time or else Target = Calendar_Time then
13907 Error_Msg_N
13908 ("?z?representation of 'Time values may change between "
13909 & "'G'N'A'T versions", N);
13910 end if;
13911 end;
13912 end if;
13914 -- Make entry in unchecked conversion table for later processing by
13915 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13916 -- (using values set by the back end where possible). This is only done
13917 -- if the appropriate warning is active.
13919 if Warn_On_Unchecked_Conversion then
13920 Unchecked_Conversions.Append
13921 (New_Val => UC_Entry'(Eloc => Sloc (N),
13922 Source => Source,
13923 Target => Target,
13924 Act_Unit => Act_Unit));
13926 -- If both sizes are known statically now, then back-end annotation
13927 -- is not required to do a proper check but if either size is not
13928 -- known statically, then we need the annotation.
13930 if Known_Static_RM_Size (Source)
13931 and then
13932 Known_Static_RM_Size (Target)
13933 then
13934 null;
13935 else
13936 Back_Annotate_Rep_Info := True;
13937 end if;
13938 end if;
13940 -- If unchecked conversion to access type, and access type is declared
13941 -- in the same unit as the unchecked conversion, then set the flag
13942 -- No_Strict_Aliasing (no strict aliasing is implicit here)
13944 if Is_Access_Type (Target) and then
13945 In_Same_Source_Unit (Target, N)
13946 then
13947 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13948 end if;
13950 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13951 -- the back end needs to perform special validation checks.
13953 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13954 -- have full expansion and the back end is called ???
13956 Vnode :=
13957 Make_Validate_Unchecked_Conversion (Sloc (N));
13958 Set_Source_Type (Vnode, Source);
13959 Set_Target_Type (Vnode, Target);
13961 -- If the unchecked conversion node is in a list, just insert before it.
13962 -- If not we have some strange case, not worth bothering about.
13964 if Is_List_Member (N) then
13965 Insert_After (N, Vnode);
13966 end if;
13967 end Validate_Unchecked_Conversion;
13969 ------------------------------------
13970 -- Validate_Unchecked_Conversions --
13971 ------------------------------------
13973 procedure Validate_Unchecked_Conversions is
13974 begin
13975 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13976 declare
13977 T : UC_Entry renames Unchecked_Conversions.Table (N);
13979 Act_Unit : constant Entity_Id := T.Act_Unit;
13980 Eloc : constant Source_Ptr := T.Eloc;
13981 Source : constant Entity_Id := T.Source;
13982 Target : constant Entity_Id := T.Target;
13984 Source_Siz : Uint;
13985 Target_Siz : Uint;
13987 begin
13988 -- Skip if function marked as warnings off
13990 if Warnings_Off (Act_Unit) then
13991 goto Continue;
13992 end if;
13994 -- This validation check, which warns if we have unequal sizes for
13995 -- unchecked conversion, and thus potentially implementation
13996 -- dependent semantics, is one of the few occasions on which we
13997 -- use the official RM size instead of Esize. See description in
13998 -- Einfo "Handling of Type'Size Values" for details.
14000 if Serious_Errors_Detected = 0
14001 and then Known_Static_RM_Size (Source)
14002 and then Known_Static_RM_Size (Target)
14004 -- Don't do the check if warnings off for either type, note the
14005 -- deliberate use of OR here instead of OR ELSE to get the flag
14006 -- Warnings_Off_Used set for both types if appropriate.
14008 and then not (Has_Warnings_Off (Source)
14010 Has_Warnings_Off (Target))
14011 then
14012 Source_Siz := RM_Size (Source);
14013 Target_Siz := RM_Size (Target);
14015 if Source_Siz /= Target_Siz then
14016 Error_Msg
14017 ("?z?types for unchecked conversion have different sizes!",
14018 Eloc);
14020 if All_Errors_Mode then
14021 Error_Msg_Name_1 := Chars (Source);
14022 Error_Msg_Uint_1 := Source_Siz;
14023 Error_Msg_Name_2 := Chars (Target);
14024 Error_Msg_Uint_2 := Target_Siz;
14025 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
14027 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14029 if Is_Discrete_Type (Source)
14030 and then
14031 Is_Discrete_Type (Target)
14032 then
14033 if Source_Siz > Target_Siz then
14034 Error_Msg
14035 ("\?z?^ high order bits of source will "
14036 & "be ignored!", Eloc);
14038 elsif Is_Unsigned_Type (Source) then
14039 Error_Msg
14040 ("\?z?source will be extended with ^ high order "
14041 & "zero bits!", Eloc);
14043 else
14044 Error_Msg
14045 ("\?z?source will be extended with ^ high order "
14046 & "sign bits!", Eloc);
14047 end if;
14049 elsif Source_Siz < Target_Siz then
14050 if Is_Discrete_Type (Target) then
14051 if Bytes_Big_Endian then
14052 Error_Msg
14053 ("\?z?target value will include ^ undefined "
14054 & "low order bits!", Eloc);
14055 else
14056 Error_Msg
14057 ("\?z?target value will include ^ undefined "
14058 & "high order bits!", Eloc);
14059 end if;
14061 else
14062 Error_Msg
14063 ("\?z?^ trailing bits of target value will be "
14064 & "undefined!", Eloc);
14065 end if;
14067 else pragma Assert (Source_Siz > Target_Siz);
14068 if Is_Discrete_Type (Source) then
14069 if Bytes_Big_Endian then
14070 Error_Msg
14071 ("\?z?^ low order bits of source will be "
14072 & "ignored!", Eloc);
14073 else
14074 Error_Msg
14075 ("\?z?^ high order bits of source will be "
14076 & "ignored!", Eloc);
14077 end if;
14079 else
14080 Error_Msg
14081 ("\?z?^ trailing bits of source will be "
14082 & "ignored!", Eloc);
14083 end if;
14084 end if;
14085 end if;
14086 end if;
14087 end if;
14089 -- If both types are access types, we need to check the alignment.
14090 -- If the alignment of both is specified, we can do it here.
14092 if Serious_Errors_Detected = 0
14093 and then Is_Access_Type (Source)
14094 and then Is_Access_Type (Target)
14095 and then Target_Strict_Alignment
14096 and then Present (Designated_Type (Source))
14097 and then Present (Designated_Type (Target))
14098 then
14099 declare
14100 D_Source : constant Entity_Id := Designated_Type (Source);
14101 D_Target : constant Entity_Id := Designated_Type (Target);
14103 begin
14104 if Known_Alignment (D_Source)
14105 and then
14106 Known_Alignment (D_Target)
14107 then
14108 declare
14109 Source_Align : constant Uint := Alignment (D_Source);
14110 Target_Align : constant Uint := Alignment (D_Target);
14112 begin
14113 if Source_Align < Target_Align
14114 and then not Is_Tagged_Type (D_Source)
14116 -- Suppress warning if warnings suppressed on either
14117 -- type or either designated type. Note the use of
14118 -- OR here instead of OR ELSE. That is intentional,
14119 -- we would like to set flag Warnings_Off_Used in
14120 -- all types for which warnings are suppressed.
14122 and then not (Has_Warnings_Off (D_Source)
14124 Has_Warnings_Off (D_Target)
14126 Has_Warnings_Off (Source)
14128 Has_Warnings_Off (Target))
14129 then
14130 Error_Msg_Uint_1 := Target_Align;
14131 Error_Msg_Uint_2 := Source_Align;
14132 Error_Msg_Node_1 := D_Target;
14133 Error_Msg_Node_2 := D_Source;
14134 Error_Msg
14135 ("?z?alignment of & (^) is stricter than "
14136 & "alignment of & (^)!", Eloc);
14137 Error_Msg
14138 ("\?z?resulting access value may have invalid "
14139 & "alignment!", Eloc);
14140 end if;
14141 end;
14142 end if;
14143 end;
14144 end if;
14145 end;
14147 <<Continue>>
14148 null;
14149 end loop;
14150 end Validate_Unchecked_Conversions;
14152 end Sem_Ch13;