1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Case; use Sem_Case;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Dim; use Sem_Dim;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Prag; use Sem_Prag;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Sinput; use Sinput;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Targparm; use Targparm;
67 with Ttypes; use Ttypes;
68 with Tbuild; use Tbuild;
69 with Urealp; use Urealp;
70 with Warnsw; use Warnsw;
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
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
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
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
173 function Is_Predicate_Static
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:
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
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
221 procedure Resolve_Iterable_Operation
226 -- If the name of a primitive operation for an Iterable aspect is
227 -- overloaded, resolve according to required signature.
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
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
257 -- Source location used in warnings and error messages
260 -- Pragma Compile_Time_Error or Compile_Time_Warning
263 -- The scope which encloses the pragma
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,
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
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
296 package Unchecked_Conversions is new Table.Table (
297 Table_Component_Type => UC_Entry,
298 Table_Index_Type => Int,
299 Table_Low_Bound => 1,
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
323 -- The address clause
326 -- The entity of the object subject to the address clause
329 -- The value of the address in the first case
332 -- The entity of the object being overlaid in the second case
335 -- Whether the address is offset within Y in the second case
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,
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
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
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;
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
398 ("info: multi-byte field specified with "
399 & "non-standard Bit_Order?V?", CLC);
401 if Bytes_Big_Endian then
403 ("\bytes are not reversed "
404 & "(component is big-endian)?V?", CLC);
407 ("\bytes are not reversed "
408 & "(component is little-endian)?V?", CLC);
411 -- Do not allow non-contiguous field
415 ("attempt to specify non-contiguous field "
416 & "not permitted", CLC);
418 ("\caused by non-standard Bit_Order "
421 ("\consider possibility of using "
422 & "Ada 2005 mode here", CLC);
425 -- Case where field fits in one storage unit
428 -- Give warning if suspicious component clause
430 if Intval (FB) >= System_Storage_Unit
431 and then Warn_On_Reverse_Bit_Order
434 ("info: Bit_Order clause does not affect " &
435 "byte ordering?V?", Pos);
437 Intval (Pos) + Intval (FB) /
440 ("info: position normalized to ^ before bit " &
441 "order interpreted?V?", Pos);
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
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
465 (Storage_Unit_Offset * System_Storage_Unit) +
466 (System_Storage_Unit - 1) -
467 (Start_Bit + CSZ - 1));
469 Set_Normalized_First_Bit
471 Component_Bit_Offset (Comp) mod
472 System_Storage_Unit);
477 Next_Component_Or_Discriminant (Comp);
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.
487 Max_Machine_Scalar_Size : constant Uint :=
489 (Standard_Long_Long_Integer_Size);
490 -- We use this as the maximum machine scalar size
493 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
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.
504 Comp := First_Component_Or_Discriminant (R);
505 while Present (Comp) loop
506 CC := Component_Clause (Comp);
510 Fbit : constant Uint := Static_Integer (First_Bit (CC));
511 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
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
527 ("info: multi-byte field specified with "
528 & "non-standard Bit_Order?V?", CC);
530 if Bytes_Big_Endian then
532 ("\bytes are not reversed "
533 & "(component is big-endian)?V?", CC);
536 ("\bytes are not reversed "
537 & "(component is little-endian)?V?", CC);
541 -- Give error message for RM 13.5.1(10) violation
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;
551 ("\last bit + 1 (^) exceeds maximum machine "
555 if (Lbit + 1) mod SSU /= 0 then
556 Error_Msg_Uint_1 := SSU;
558 ("\and is not a multiple of Storage_Unit (^) "
563 Error_Msg_Uint_1 := Fbit;
565 ("\and first bit (^) is non-zero "
571 -- OK case of machine scalar related component clause,
572 -- For now, just count them.
575 Num_CC := Num_CC + 1;
580 Next_Component_Or_Discriminant (Comp);
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
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
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);
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).
609 -- Maximum last bit value of any component in this set
612 -- Corresponding machine scalar size
618 function CP_Lt (Op1, Op2 : Natural) return Boolean is
620 return Position (Component_Clause (Comps (Op1))) <
621 Position (Component_Clause (Comps (Op2)));
628 procedure CP_Move (From : Natural; To : Natural) is
630 Comps (To) := Comps (From);
633 -- Start of processing for Sort_CC
636 -- Collect the machine scalar relevant component clauses
639 Comp := First_Component_Or_Discriminant (R);
640 while Present (Comp) loop
642 CC : constant Node_Id := Component_Clause (Comp);
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.
653 and then not Error_Posted (Last_Bit (CC))
654 and then Static_Integer (Last_Bit (CC)) <
655 Max_Machine_Scalar_Size
657 Num_CC := Num_CC + 1;
658 Comps (Num_CC) := Comp;
662 Next_Component_Or_Discriminant (Comp);
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.
675 while Stop < Num_CC loop
680 (Last_Bit (Component_Clause (Comps (Start))));
681 while Stop < Num_CC loop
683 (Position (Component_Clause (Comps (Stop + 1)))) =
685 (Position (Component_Clause (Comps (Stop))))
693 (Component_Clause (Comps (Stop)))));
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;
710 and then (MSS / 2) >= SSU
711 and then (MSS / 2) > MaxL
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
721 -- First_Bit .. Last_Bit Component_Bit_Offset
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
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));
747 if Warn_On_Reverse_Bit_Order then
748 Error_Msg_Uint_1 := MSS;
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
757 ("\big-endian range for component "
758 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
761 ("\little-endian range for component"
762 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
766 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
767 Set_Normalized_First_Bit (Comp, NFB mod SSU);
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
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
791 Init_Alignment (Typ);
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;
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);
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);
849 elsif not Is_First_Subtype (Ent) then
850 Error_Msg_N ("aspect% cannot apply to subtype", Id);
853 elsif A_Id = Aspect_Default_Value
854 and then not Is_Scalar_Type (Ent)
856 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
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);
864 elsif not Is_Scalar_Type (Component_Type (Ent)) then
865 Error_Msg_N ("aspect% requires scalar components", Id);
870 Set_Has_Default_Aspect (Base_Type (Ent));
872 if Is_Scalar_Type (Ent) then
873 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
875 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
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
889 -- Item from Rep_Item chain
894 -- Loop through delayed aspects for the parent type
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.
915 when Aspect_Alignment =>
916 if not Has_Alignment_Clause (E) then
917 Set_Alignment (E, Alignment (P));
922 when Aspect_Atomic =>
923 if Is_Atomic (P) then
929 when Aspect_Atomic_Components =>
930 if Has_Atomic_Components (P) then
931 Set_Has_Atomic_Components (Base_Type (E));
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)
942 Set_Reverse_Bit_Order (Base_Type (E));
947 when Aspect_Component_Size =>
949 and then not Has_Component_Size_Clause (E)
952 (Base_Type (E), Component_Size (P));
957 when Aspect_Machine_Radix =>
958 if Is_Decimal_Fixed_Point_Type (E)
959 and then not Has_Machine_Radix_Clause (E)
961 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
964 -- Object_Size (also Size which also sets Object_Size)
966 when Aspect_Object_Size | Aspect_Size =>
967 if not Has_Size_Clause (E)
969 No (Get_Attribute_Definition_Clause
970 (E, Attribute_Object_Size))
972 Set_Esize (E, Esize (P));
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));
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)
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);
1007 when Aspect_Small =>
1008 if Is_Fixed_Point_Type (E)
1009 and then not Has_Small_Clause (E)
1011 Set_Small_Value (E, Small_Value (P));
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)
1020 Set_Storage_Size_Variable
1021 (Base_Type (E), Storage_Size_Variable (P));
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.
1036 when Aspect_Volatile =>
1037 if Is_Volatile (P) then
1038 Set_Is_Volatile (E);
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);
1048 -- Volatile_Components
1050 when Aspect_Volatile_Components =>
1051 if Has_Volatile_Components (P) then
1052 Set_Has_Volatile_Components (Base_Type (E));
1055 -- That should be all the Rep Aspects
1058 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1065 N := Next_Rep_Item (N);
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
1086 -----------------------------------------
1087 -- Check_False_Aspect_For_Derived_Type --
1088 -----------------------------------------
1090 procedure Check_False_Aspect_For_Derived_Type is
1094 -- We are only checking derived types
1096 if not Is_Derived_Type (E) then
1100 Par := Nearest_Ancestor (E);
1103 when Aspect_Atomic | Aspect_Shared =>
1104 if not Is_Atomic (Par) then
1108 when Aspect_Atomic_Components =>
1109 if not Has_Atomic_Components (Par) then
1113 when Aspect_Discard_Names =>
1114 if not Discard_Names (Par) then
1119 if not Is_Packed (Par) then
1123 when Aspect_Unchecked_Union =>
1124 if not Is_Unchecked_Union (Par) then
1128 when Aspect_Volatile =>
1129 if not Is_Volatile (Par) then
1133 when Aspect_Volatile_Components =>
1134 if not Has_Volatile_Components (Par) then
1138 when Aspect_Volatile_Full_Access =>
1139 if not Is_Volatile_Full_Access (Par) then
1147 -- Fall through means we are canceling an inherited aspect
1149 Error_Msg_Name_1 := A_Name;
1151 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1152 end Check_False_Aspect_For_Derived_Type;
1158 -- Start of processing for Make_Pragma_From_Boolean_Aspect
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;
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);
1183 end Make_Pragma_From_Boolean_Aspect;
1191 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1194 -- Must be visible in current scope
1196 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
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);
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
1231 -- Otherwise create a corresponding pragma
1234 Make_Pragma_From_Boolean_Aspect (ASN);
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)
1254 Analyze_Aspect_Default_Value (ASN);
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
1268 ("\aspect must be fully defined before & is frozen",
1272 when Aspect_Iterable =>
1273 Validate_Iterable_Aspect (E, ASN);
1279 Ritem := Aspect_Rep_Item (ASN);
1281 if Present (Ritem) then
1287 Next_Rep_Item (ASN);
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);
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
1310 Is_Instance : Boolean := False);
1311 -- Subsidiary to the analysis of aspects
1318 -- Initial_Condition
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.
1335 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
1337 Set_Aspect_Rep_Item (Asp, Prag);
1338 Set_Corresponding_Aspect (Prag, Asp);
1339 Set_From_Aspect_Specification (Prag);
1340 Set_Parent (Prag, Asp);
1347 procedure Insert_Pragma
1349 Is_Instance : Boolean := False)
1355 Inserted : Boolean := False;
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
1363 -- package body Pack with Aspect is
1365 -- package body Pack is
1368 if Nkind_In (N, N_Entry_Body,
1374 Decls := Declarations (N);
1378 Set_Declarations (N, Decls);
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
1392 elsif Nkind_In (N, N_Generic_Package_Declaration,
1393 N_Package_Declaration)
1395 Decls := Visible_Declarations (Specification (N));
1399 Set_Visible_Declarations (Specification (N), Decls);
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
1414 Decl := First (Decls);
1415 while Present (Decl) loop
1416 if Comes_From_Source (Decl) then
1417 Insert_Before (Decl, Prag);
1425 -- The pragma is placed after the instance "header"
1427 if not Inserted then
1428 Append_To (Decls, Prag);
1431 -- Otherwise this is not a generic instance
1434 Prepend_To (Decls, Prag);
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
1446 elsif Nkind (N) = N_Protected_Type_Declaration then
1447 Def := Protected_Definition (N);
1451 Make_Protected_Definition (Sloc (N),
1452 Visible_Declarations => New_List,
1453 End_Label => Empty);
1455 Set_Protected_Definition (N, Def);
1458 Decls := Visible_Declarations (Def);
1462 Set_Visible_Declarations (Def, Decls);
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
1476 elsif Nkind (N) = N_Task_Type_Declaration then
1477 Def := Task_Definition (N);
1481 Make_Task_Definition (Sloc (N),
1482 Visible_Declarations => New_List,
1483 End_Label => Empty);
1485 Set_Task_Definition (N, Def);
1488 Decls := Visible_Declarations (Def);
1492 Set_Visible_Declarations (Def, Decls);
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);
1507 Prepend (Prag, Pragmas_After (Aux));
1509 -- Default, the pragma is inserted after the context
1512 Insert_After (N, Prag);
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
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);
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
1608 -- Obtain all interfacing aspects that apply to the related
1611 Get_Interfacing_Aspects
1612 (Iface_Asp => Aspect,
1613 Conv_Asp => Dummy_1,
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
1627 -- Otherwise Convention appears by itself
1630 -- The aspect specifies a particular convention
1632 if Present (Expr) then
1633 Conv := New_Copy_Tree (Expr);
1635 -- Otherwise assume convention Ada
1638 Conv := Make_Identifier (Loc, Name_Ada);
1642 -- pragma Convention (<Conv>, <E>);
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);
1655 end Analyze_Aspect_Convention;
1657 ----------------------------------
1658 -- Analyze_Aspect_Export_Import --
1659 ----------------------------------
1661 procedure Analyze_Aspect_Export_Import is
1669 -- Obtain all interfacing aspects that apply to the related
1672 Get_Interfacing_Aspects
1673 (Iface_Asp => Aspect,
1674 Conv_Asp => Dummy_1,
1681 -- The related entity cannot be subject to both aspects Export
1684 if Present (Expo) and then Present (Imp) then
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);
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);
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))
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))
1720 ("imported entities cannot be initialized "
1721 & "(RM B.1(24))", Expression (N));
1725 pragma Assert (A_Id = Aspect_Export);
1726 Set_Is_Exported (E);
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.
1740 end Analyze_Aspect_Export_Import;
1742 ---------------------------------------
1743 -- Analyze_Aspect_External_Link_Name --
1744 ---------------------------------------
1746 procedure Analyze_Aspect_External_Link_Name is
1754 -- Obtain all interfacing aspects that apply to the related
1757 Get_Interfacing_Aspects
1758 (Iface_Asp => Aspect,
1759 Conv_Asp => Dummy_1,
1766 -- Ensure that aspect External_Name applies to aspect Export or
1769 if A_Id = Aspect_External_Name then
1770 if No (Expo) and then No (Imp) then
1772 ("aspect `External_Name` requires aspect `Import` or "
1773 & "`Export`", Aspect);
1776 -- Otherwise ensure that aspect Link_Name applies to aspect
1777 -- Export or Import.
1780 pragma Assert (A_Id = Aspect_Link_Name);
1781 if No (Expo) and then No (Imp) then
1783 ("aspect `Link_Name` requires aspect `Import` or "
1784 & "`Export`", Aspect);
1787 end Analyze_Aspect_External_Link_Name;
1789 -----------------------------------------
1790 -- Analyze_Aspect_Implicit_Dereference --
1791 -----------------------------------------
1793 procedure Analyze_Aspect_Implicit_Dereference is
1795 Parent_Disc : Entity_Id;
1798 if not Is_Type (E) or else not Has_Discriminants (E) then
1800 ("aspect must apply to a type with discriminants", Expr);
1802 elsif not Is_Entity_Name (Expr) then
1804 ("aspect must name a discriminant of current type", Expr);
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
1813 Set_Has_Implicit_Dereference (E);
1814 Set_Has_Implicit_Dereference (Disc);
1818 Next_Discriminant (Disc);
1821 -- Error if no proper access discriminant
1824 Error_Msg_NE ("not an access discriminant of&", Expr, E);
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))
1835 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1837 if Present (Parent_Disc)
1838 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1841 ("reference discriminant does not match discriminant "
1842 & "of parent type", Expr);
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;
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
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);
1887 Set_Corresponding_Aspect (Aitem, Aspect);
1888 Set_From_Aspect_Specification (Aitem);
1889 end Make_Aitem_Pragma;
1891 -- Start of processing for Analyze_One_Aspect
1894 -- Skip aspect if already analyzed, to avoid looping in some cases
1896 if Analyzed (Aspect) then
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
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));
1922 -- Check restriction No_Implementation_Aspect_Specifications
1924 if Implementation_Defined_Aspect (A_Id) then
1926 (No_Implementation_Aspect_Specifications, Aspect);
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);
1938 -- Build the reference to E that will be used in the built pragmas
1940 Ent := New_Occurrence_Of (E, Sloc (Id));
1942 if A_Id = Aspect_Attach_Handler
1943 or else A_Id = Aspect_Interrupt_Handler
1945 -- Decorate the reference as comming from the sources and force
1946 -- its reanalysis to generate the reference to E; required to
1947 -- avoid reporting spurious warning on E as unreferenced entity
1948 -- (because aspects are not fully analyzed).
1950 Set_Comes_From_Source (Ent, Comes_From_Source (Id));
1951 Set_Entity (Ent, Empty);
1956 -- Check for duplicate aspect. Note that the Comes_From_Source
1957 -- test allows duplicate Pre/Post's that we generate internally
1958 -- to escape being flagged here.
1960 if No_Duplicates_Allowed (A_Id) then
1962 while Anod /= Aspect loop
1963 if Comes_From_Source (Aspect)
1964 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1966 Error_Msg_Name_1 := Nam;
1967 Error_Msg_Sloc := Sloc (Anod);
1969 -- Case of same aspect specified twice
1971 if Class_Present (Anod) = Class_Present (Aspect) then
1972 if not Class_Present (Anod) then
1974 ("aspect% for & previously given#",
1978 ("aspect `%''Class` for & previously given#",
1988 -- Check some general restrictions on language defined aspects
1990 if not Implementation_Defined_Aspect (A_Id) then
1991 Error_Msg_Name_1 := Nam;
1993 -- Not allowed for renaming declarations. Examine the original
1994 -- node because a subprogram renaming may have been rewritten
1997 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
1999 ("aspect % not allowed for renaming declaration",
2003 -- Not allowed for formal type declarations
2005 if Nkind (N) = N_Formal_Type_Declaration then
2007 ("aspect % not allowed for formal type declaration",
2012 -- Copy expression for later processing by the procedures
2013 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2015 Set_Entity (Id, New_Copy_Tree (Expr));
2017 -- Set Delay_Required as appropriate to aspect
2019 case Aspect_Delay (A_Id) is
2020 when Always_Delay =>
2021 Delay_Required := True;
2024 Delay_Required := False;
2028 -- If expression has the form of an integer literal, then
2029 -- do not delay, since we know the value cannot change.
2030 -- This optimization catches most rep clause cases.
2032 -- For Boolean aspects, don't delay if no expression
2034 if A_Id in Boolean_Aspects and then No (Expr) then
2035 Delay_Required := False;
2037 -- For non-Boolean aspects, don't delay if integer literal
2039 elsif A_Id not in Boolean_Aspects
2040 and then Present (Expr)
2041 and then Nkind (Expr) = N_Integer_Literal
2043 Delay_Required := False;
2045 -- All other cases are delayed
2048 Delay_Required := True;
2049 Set_Has_Delayed_Rep_Aspects (E);
2053 -- Processing based on specific aspect
2056 when Aspect_Unimplemented =>
2057 null; -- ??? temp for now
2059 -- No_Aspect should be impossible
2062 raise Program_Error;
2064 -- Case 1: Aspects corresponding to attribute definition
2067 when Aspect_Address |
2070 Aspect_Component_Size |
2071 Aspect_Constant_Indexing |
2072 Aspect_Default_Iterator |
2073 Aspect_Dispatching_Domain |
2074 Aspect_External_Tag |
2077 Aspect_Iterator_Element |
2078 Aspect_Machine_Radix |
2079 Aspect_Object_Size |
2082 Aspect_Scalar_Storage_Order |
2083 Aspect_Secondary_Stack_Size |
2084 Aspect_Simple_Storage_Pool |
2087 Aspect_Storage_Pool |
2088 Aspect_Stream_Size |
2090 Aspect_Variable_Indexing |
2093 -- Indexing aspects apply only to tagged type
2095 if (A_Id = Aspect_Constant_Indexing
2097 A_Id = Aspect_Variable_Indexing)
2098 and then not (Is_Type (E)
2099 and then Is_Tagged_Type (E))
2102 ("indexing aspect can only apply to a tagged type",
2107 -- For the case of aspect Address, we don't consider that we
2108 -- know the entity is never set in the source, since it is
2109 -- is likely aliasing is occurring.
2111 -- Note: one might think that the analysis of the resulting
2112 -- attribute definition clause would take care of that, but
2113 -- that's not the case since it won't be from source.
2115 if A_Id = Aspect_Address then
2116 Set_Never_Set_In_Source (E, False);
2119 -- Correctness of the profile of a stream operation is
2120 -- verified at the freeze point, but we must detect the
2121 -- illegal specification of this aspect for a subtype now,
2122 -- to prevent malformed rep_item chains.
2124 if A_Id = Aspect_Input or else
2125 A_Id = Aspect_Output or else
2126 A_Id = Aspect_Read or else
2129 if not Is_First_Subtype (E) then
2131 ("local name must be a first subtype", Aspect);
2134 -- If stream aspect applies to the class-wide type,
2135 -- the generated attribute definition applies to the
2136 -- class-wide type as well.
2138 elsif Class_Present (Aspect) then
2140 Make_Attribute_Reference (Loc,
2142 Attribute_Name => Name_Class);
2146 -- Construct the attribute definition clause
2149 Make_Attribute_Definition_Clause (Loc,
2151 Chars => Chars (Id),
2152 Expression => Relocate_Node (Expr));
2154 -- If the address is specified, then we treat the entity as
2155 -- referenced, to avoid spurious warnings. This is analogous
2156 -- to what is done with an attribute definition clause, but
2157 -- here we don't want to generate a reference because this
2158 -- is the point of definition of the entity.
2160 if A_Id = Aspect_Address then
2164 -- Case 2: Aspects corresponding to pragmas
2166 -- Case 2a: Aspects corresponding to pragmas with two
2167 -- arguments, where the first argument is a local name
2168 -- referring to the entity, and the second argument is the
2169 -- aspect definition expression.
2171 -- Linker_Section/Suppress/Unsuppress
2173 when Aspect_Linker_Section |
2175 Aspect_Unsuppress =>
2178 (Pragma_Argument_Associations => New_List (
2179 Make_Pragma_Argument_Association (Loc,
2180 Expression => New_Occurrence_Of (E, Loc)),
2181 Make_Pragma_Argument_Association (Sloc (Expr),
2182 Expression => Relocate_Node (Expr))),
2183 Pragma_Name => Chars (Id));
2187 -- Corresponds to pragma Implemented, construct the pragma
2189 when Aspect_Synchronization =>
2191 (Pragma_Argument_Associations => New_List (
2192 Make_Pragma_Argument_Association (Loc,
2193 Expression => New_Occurrence_Of (E, Loc)),
2194 Make_Pragma_Argument_Association (Sloc (Expr),
2195 Expression => Relocate_Node (Expr))),
2196 Pragma_Name => Name_Implemented);
2200 when Aspect_Attach_Handler =>
2202 (Pragma_Argument_Associations => New_List (
2203 Make_Pragma_Argument_Association (Sloc (Ent),
2205 Make_Pragma_Argument_Association (Sloc (Expr),
2206 Expression => Relocate_Node (Expr))),
2207 Pragma_Name => Name_Attach_Handler);
2209 -- We need to insert this pragma into the tree to get proper
2210 -- processing and to look valid from a placement viewpoint.
2212 Insert_Pragma (Aitem);
2215 -- Dynamic_Predicate, Predicate, Static_Predicate
2217 when Aspect_Dynamic_Predicate |
2219 Aspect_Static_Predicate =>
2221 -- These aspects apply only to subtypes
2223 if not Is_Type (E) then
2225 ("predicate can only be specified for a subtype",
2229 elsif Is_Incomplete_Type (E) then
2231 ("predicate cannot apply to incomplete view", Aspect);
2235 -- Construct the pragma (always a pragma Predicate, with
2236 -- flags recording whether it is static/dynamic). We also
2237 -- set flags recording this in the type itself.
2240 (Pragma_Argument_Associations => New_List (
2241 Make_Pragma_Argument_Association (Sloc (Ent),
2243 Make_Pragma_Argument_Association (Sloc (Expr),
2244 Expression => Relocate_Node (Expr))),
2245 Pragma_Name => Name_Predicate);
2247 -- Mark type has predicates, and remember what kind of
2248 -- aspect lead to this predicate (we need this to access
2249 -- the right set of check policies later on).
2251 Set_Has_Predicates (E);
2253 if A_Id = Aspect_Dynamic_Predicate then
2254 Set_Has_Dynamic_Predicate_Aspect (E);
2255 elsif A_Id = Aspect_Static_Predicate then
2256 Set_Has_Static_Predicate_Aspect (E);
2259 -- If the type is private, indicate that its completion
2260 -- has a freeze node, because that is the one that will
2261 -- be visible at freeze time.
2263 if Is_Private_Type (E) and then Present (Full_View (E)) then
2264 Set_Has_Predicates (Full_View (E));
2266 if A_Id = Aspect_Dynamic_Predicate then
2267 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2268 elsif A_Id = Aspect_Static_Predicate then
2269 Set_Has_Static_Predicate_Aspect (Full_View (E));
2272 Set_Has_Delayed_Aspects (Full_View (E));
2273 Ensure_Freeze_Node (Full_View (E));
2276 -- Predicate_Failure
2278 when Aspect_Predicate_Failure =>
2280 -- This aspect applies only to subtypes
2282 if not Is_Type (E) then
2284 ("predicate can only be specified for a subtype",
2288 elsif Is_Incomplete_Type (E) then
2290 ("predicate cannot apply to incomplete view", Aspect);
2294 -- Construct the pragma
2297 (Pragma_Argument_Associations => New_List (
2298 Make_Pragma_Argument_Association (Sloc (Ent),
2300 Make_Pragma_Argument_Association (Sloc (Expr),
2301 Expression => Relocate_Node (Expr))),
2302 Pragma_Name => Name_Predicate_Failure);
2304 Set_Has_Predicates (E);
2306 -- If the type is private, indicate that its completion
2307 -- has a freeze node, because that is the one that will
2308 -- be visible at freeze time.
2310 if Is_Private_Type (E) and then Present (Full_View (E)) then
2311 Set_Has_Predicates (Full_View (E));
2312 Set_Has_Delayed_Aspects (Full_View (E));
2313 Ensure_Freeze_Node (Full_View (E));
2316 -- Case 2b: Aspects corresponding to pragmas with two
2317 -- arguments, where the second argument is a local name
2318 -- referring to the entity, and the first argument is the
2319 -- aspect definition expression.
2323 when Aspect_Convention =>
2324 Analyze_Aspect_Convention;
2327 -- External_Name, Link_Name
2329 when Aspect_External_Name |
2331 Analyze_Aspect_External_Link_Name;
2334 -- CPU, Interrupt_Priority, Priority
2336 -- These three aspects can be specified for a subprogram spec
2337 -- or body, in which case we analyze the expression and export
2338 -- the value of the aspect.
2340 -- Previously, we generated an equivalent pragma for bodies
2341 -- (note that the specs cannot contain these pragmas). The
2342 -- pragma was inserted ahead of local declarations, rather than
2343 -- after the body. This leads to a certain duplication between
2344 -- the processing performed for the aspect and the pragma, but
2345 -- given the straightforward handling required it is simpler
2346 -- to duplicate than to translate the aspect in the spec into
2347 -- a pragma in the declarative part of the body.
2350 Aspect_Interrupt_Priority |
2353 if Nkind_In (N, N_Subprogram_Body,
2354 N_Subprogram_Declaration)
2356 -- Analyze the aspect expression
2358 Analyze_And_Resolve (Expr, Standard_Integer);
2360 -- Interrupt_Priority aspect not allowed for main
2361 -- subprograms. RM D.1 does not forbid this explicitly,
2362 -- but RM J.15.11(6/3) does not permit pragma
2363 -- Interrupt_Priority for subprograms.
2365 if A_Id = Aspect_Interrupt_Priority then
2367 ("Interrupt_Priority aspect cannot apply to "
2368 & "subprogram", Expr);
2370 -- The expression must be static
2372 elsif not Is_OK_Static_Expression (Expr) then
2373 Flag_Non_Static_Expr
2374 ("aspect requires static expression!", Expr);
2376 -- Check whether this is the main subprogram. Issue a
2377 -- warning only if it is obviously not a main program
2378 -- (when it has parameters or when the subprogram is
2379 -- within a package).
2381 elsif Present (Parameter_Specifications
2382 (Specification (N)))
2383 or else not Is_Compilation_Unit (Defining_Entity (N))
2385 -- See RM D.1(14/3) and D.16(12/3)
2388 ("aspect applied to subprogram other than the "
2389 & "main subprogram has no effect??", Expr);
2391 -- Otherwise check in range and export the value
2393 -- For the CPU aspect
2395 elsif A_Id = Aspect_CPU then
2396 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2398 -- Value is correct so we export the value to make
2399 -- it available at execution time.
2402 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2406 ("main subprogram CPU is out of range", Expr);
2409 -- For the Priority aspect
2411 elsif A_Id = Aspect_Priority then
2412 if Is_In_Range (Expr, RTE (RE_Priority)) then
2414 -- Value is correct so we export the value to make
2415 -- it available at execution time.
2418 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2420 -- Ignore pragma if Relaxed_RM_Semantics to support
2421 -- other targets/non GNAT compilers.
2423 elsif not Relaxed_RM_Semantics then
2425 ("main subprogram priority is out of range",
2430 -- Load an arbitrary entity from System.Tasking.Stages
2431 -- or System.Tasking.Restricted.Stages (depending on
2432 -- the supported profile) to make sure that one of these
2433 -- packages is implicitly with'ed, since we need to have
2434 -- the tasking run time active for the pragma Priority to
2435 -- have any effect. Previously we with'ed the package
2436 -- System.Tasking, but this package does not trigger the
2437 -- required initialization of the run-time library.
2440 Discard : Entity_Id;
2442 if Restricted_Profile then
2443 Discard := RTE (RE_Activate_Restricted_Tasks);
2445 Discard := RTE (RE_Activate_Tasks);
2449 -- Handling for these aspects in subprograms is complete
2453 -- For tasks pass the aspect as an attribute
2457 Make_Attribute_Definition_Clause (Loc,
2459 Chars => Chars (Id),
2460 Expression => Relocate_Node (Expr));
2465 when Aspect_Warnings =>
2467 (Pragma_Argument_Associations => New_List (
2468 Make_Pragma_Argument_Association (Sloc (Expr),
2469 Expression => Relocate_Node (Expr)),
2470 Make_Pragma_Argument_Association (Loc,
2471 Expression => New_Occurrence_Of (E, Loc))),
2472 Pragma_Name => Chars (Id));
2474 Decorate (Aspect, Aitem);
2475 Insert_Pragma (Aitem);
2478 -- Case 2c: Aspects corresponding to pragmas with three
2481 -- Invariant aspects have a first argument that references the
2482 -- entity, a second argument that is the expression and a third
2483 -- argument that is an appropriate message.
2485 -- Invariant, Type_Invariant
2487 when Aspect_Invariant |
2488 Aspect_Type_Invariant =>
2490 -- Analysis of the pragma will verify placement legality:
2491 -- an invariant must apply to a private type, or appear in
2492 -- the private part of a spec and apply to a completion.
2495 (Pragma_Argument_Associations => New_List (
2496 Make_Pragma_Argument_Association (Sloc (Ent),
2498 Make_Pragma_Argument_Association (Sloc (Expr),
2499 Expression => Relocate_Node (Expr))),
2500 Pragma_Name => Name_Invariant);
2502 -- Add message unless exception messages are suppressed
2504 if not Opt.Exception_Locations_Suppressed then
2505 Append_To (Pragma_Argument_Associations (Aitem),
2506 Make_Pragma_Argument_Association (Eloc,
2507 Chars => Name_Message,
2509 Make_String_Literal (Eloc,
2510 Strval => "failed invariant from "
2511 & Build_Location_String (Eloc))));
2514 -- For Invariant case, insert immediately after the entity
2515 -- declaration. We do not have to worry about delay issues
2516 -- since the pragma processing takes care of this.
2518 Delay_Required := False;
2520 -- Case 2d : Aspects that correspond to a pragma with one
2525 -- Aspect Abstract_State introduces implicit declarations for
2526 -- all state abstraction entities it defines. To emulate this
2527 -- behavior, insert the pragma at the beginning of the visible
2528 -- declarations of the related package so that it is analyzed
2531 when Aspect_Abstract_State => Abstract_State : declare
2532 Context : Node_Id := N;
2535 -- When aspect Abstract_State appears on a generic package,
2536 -- it is propageted to the package instance. The context in
2537 -- this case is the instance spec.
2539 if Nkind (Context) = N_Package_Instantiation then
2540 Context := Instance_Spec (Context);
2543 if Nkind_In (Context, N_Generic_Package_Declaration,
2544 N_Package_Declaration)
2547 (Pragma_Argument_Associations => New_List (
2548 Make_Pragma_Argument_Association (Loc,
2549 Expression => Relocate_Node (Expr))),
2550 Pragma_Name => Name_Abstract_State);
2552 Decorate (Aspect, Aitem);
2556 Is_Generic_Instance (Defining_Entity (Context)));
2560 ("aspect & must apply to a package declaration",
2567 -- Aspect Async_Readers is never delayed because it is
2568 -- equivalent to a source pragma which appears after the
2569 -- related object declaration.
2571 when Aspect_Async_Readers =>
2573 (Pragma_Argument_Associations => New_List (
2574 Make_Pragma_Argument_Association (Loc,
2575 Expression => Relocate_Node (Expr))),
2576 Pragma_Name => Name_Async_Readers);
2578 Decorate (Aspect, Aitem);
2579 Insert_Pragma (Aitem);
2582 -- Aspect Async_Writers is never delayed because it is
2583 -- equivalent to a source pragma which appears after the
2584 -- related object declaration.
2586 when Aspect_Async_Writers =>
2588 (Pragma_Argument_Associations => New_List (
2589 Make_Pragma_Argument_Association (Loc,
2590 Expression => Relocate_Node (Expr))),
2591 Pragma_Name => Name_Async_Writers);
2593 Decorate (Aspect, Aitem);
2594 Insert_Pragma (Aitem);
2597 -- Aspect Constant_After_Elaboration is never delayed because
2598 -- it is equivalent to a source pragma which appears after the
2599 -- related object declaration.
2601 when Aspect_Constant_After_Elaboration =>
2603 (Pragma_Argument_Associations => New_List (
2604 Make_Pragma_Argument_Association (Loc,
2605 Expression => Relocate_Node (Expr))),
2607 Name_Constant_After_Elaboration);
2609 Decorate (Aspect, Aitem);
2610 Insert_Pragma (Aitem);
2613 -- Aspect Default_Internal_Condition is never delayed because
2614 -- it is equivalent to a source pragma which appears after the
2615 -- related private type. To deal with forward references, the
2616 -- generated pragma is stored in the rep chain of the related
2617 -- private type as types do not carry contracts. The pragma is
2618 -- wrapped inside of a procedure at the freeze point of the
2619 -- private type's full view.
2621 when Aspect_Default_Initial_Condition =>
2623 (Pragma_Argument_Associations => New_List (
2624 Make_Pragma_Argument_Association (Loc,
2625 Expression => Relocate_Node (Expr))),
2627 Name_Default_Initial_Condition);
2629 Decorate (Aspect, Aitem);
2630 Insert_Pragma (Aitem);
2633 -- Default_Storage_Pool
2635 when Aspect_Default_Storage_Pool =>
2637 (Pragma_Argument_Associations => New_List (
2638 Make_Pragma_Argument_Association (Loc,
2639 Expression => Relocate_Node (Expr))),
2641 Name_Default_Storage_Pool);
2643 Decorate (Aspect, Aitem);
2644 Insert_Pragma (Aitem);
2649 -- Aspect Depends is never delayed because it is equivalent to
2650 -- a source pragma which appears after the related subprogram.
2651 -- To deal with forward references, the generated pragma is
2652 -- stored in the contract of the related subprogram and later
2653 -- analyzed at the end of the declarative region. See routine
2654 -- Analyze_Depends_In_Decl_Part for details.
2656 when Aspect_Depends =>
2658 (Pragma_Argument_Associations => New_List (
2659 Make_Pragma_Argument_Association (Loc,
2660 Expression => Relocate_Node (Expr))),
2661 Pragma_Name => Name_Depends);
2663 Decorate (Aspect, Aitem);
2664 Insert_Pragma (Aitem);
2667 -- Aspect Effecitve_Reads is never delayed because it is
2668 -- equivalent to a source pragma which appears after the
2669 -- related object declaration.
2671 when Aspect_Effective_Reads =>
2673 (Pragma_Argument_Associations => New_List (
2674 Make_Pragma_Argument_Association (Loc,
2675 Expression => Relocate_Node (Expr))),
2676 Pragma_Name => Name_Effective_Reads);
2678 Decorate (Aspect, Aitem);
2679 Insert_Pragma (Aitem);
2682 -- Aspect Effective_Writes is never delayed because it is
2683 -- equivalent to a source pragma which appears after the
2684 -- related object declaration.
2686 when Aspect_Effective_Writes =>
2688 (Pragma_Argument_Associations => New_List (
2689 Make_Pragma_Argument_Association (Loc,
2690 Expression => Relocate_Node (Expr))),
2691 Pragma_Name => Name_Effective_Writes);
2693 Decorate (Aspect, Aitem);
2694 Insert_Pragma (Aitem);
2697 -- Aspect Extensions_Visible is never delayed because it is
2698 -- equivalent to a source pragma which appears after the
2699 -- related subprogram.
2701 when Aspect_Extensions_Visible =>
2703 (Pragma_Argument_Associations => New_List (
2704 Make_Pragma_Argument_Association (Loc,
2705 Expression => Relocate_Node (Expr))),
2706 Pragma_Name => Name_Extensions_Visible);
2708 Decorate (Aspect, Aitem);
2709 Insert_Pragma (Aitem);
2712 -- Aspect Ghost is never delayed because it is equivalent to a
2713 -- source pragma which appears at the top of [generic] package
2714 -- declarations or after an object, a [generic] subprogram, or
2715 -- a type declaration.
2717 when Aspect_Ghost =>
2719 (Pragma_Argument_Associations => New_List (
2720 Make_Pragma_Argument_Association (Loc,
2721 Expression => Relocate_Node (Expr))),
2722 Pragma_Name => Name_Ghost);
2724 Decorate (Aspect, Aitem);
2725 Insert_Pragma (Aitem);
2730 -- Aspect Global is never delayed because it is equivalent to
2731 -- a source pragma which appears after the related subprogram.
2732 -- To deal with forward references, the generated pragma is
2733 -- stored in the contract of the related subprogram and later
2734 -- analyzed at the end of the declarative region. See routine
2735 -- Analyze_Global_In_Decl_Part for details.
2737 when Aspect_Global =>
2739 (Pragma_Argument_Associations => New_List (
2740 Make_Pragma_Argument_Association (Loc,
2741 Expression => Relocate_Node (Expr))),
2742 Pragma_Name => Name_Global);
2744 Decorate (Aspect, Aitem);
2745 Insert_Pragma (Aitem);
2748 -- Initial_Condition
2750 -- Aspect Initial_Condition is never delayed because it is
2751 -- equivalent to a source pragma which appears after the
2752 -- related package. To deal with forward references, the
2753 -- generated pragma is stored in the contract of the related
2754 -- package and later analyzed at the end of the declarative
2755 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2758 when Aspect_Initial_Condition => Initial_Condition : declare
2759 Context : Node_Id := N;
2762 -- When aspect Initial_Condition appears on a generic
2763 -- package, it is propageted to the package instance. The
2764 -- context in this case is the instance spec.
2766 if Nkind (Context) = N_Package_Instantiation then
2767 Context := Instance_Spec (Context);
2770 if Nkind_In (Context, N_Generic_Package_Declaration,
2771 N_Package_Declaration)
2774 (Pragma_Argument_Associations => New_List (
2775 Make_Pragma_Argument_Association (Loc,
2776 Expression => Relocate_Node (Expr))),
2778 Name_Initial_Condition);
2780 Decorate (Aspect, Aitem);
2784 Is_Generic_Instance (Defining_Entity (Context)));
2786 -- Otherwise the context is illegal
2790 ("aspect & must apply to a package declaration",
2795 end Initial_Condition;
2799 -- Aspect Initializes is never delayed because it is equivalent
2800 -- to a source pragma appearing after the related package. To
2801 -- deal with forward references, the generated pragma is stored
2802 -- in the contract of the related package and later analyzed at
2803 -- the end of the declarative region. For details, see routine
2804 -- Analyze_Initializes_In_Decl_Part.
2806 when Aspect_Initializes => Initializes : declare
2807 Context : Node_Id := N;
2810 -- When aspect Initializes appears on a generic package,
2811 -- it is propageted to the package instance. The context
2812 -- in this case is the instance spec.
2814 if Nkind (Context) = N_Package_Instantiation then
2815 Context := Instance_Spec (Context);
2818 if Nkind_In (Context, N_Generic_Package_Declaration,
2819 N_Package_Declaration)
2822 (Pragma_Argument_Associations => New_List (
2823 Make_Pragma_Argument_Association (Loc,
2824 Expression => Relocate_Node (Expr))),
2825 Pragma_Name => Name_Initializes);
2827 Decorate (Aspect, Aitem);
2831 Is_Generic_Instance (Defining_Entity (Context)));
2833 -- Otherwise the context is illegal
2837 ("aspect & must apply to a package declaration",
2846 when Aspect_Max_Queue_Length =>
2848 (Pragma_Argument_Associations => New_List (
2849 Make_Pragma_Argument_Association (Loc,
2850 Expression => Relocate_Node (Expr))),
2851 Pragma_Name => Name_Max_Queue_Length);
2853 Decorate (Aspect, Aitem);
2854 Insert_Pragma (Aitem);
2859 when Aspect_Obsolescent => declare
2867 Make_Pragma_Argument_Association (Sloc (Expr),
2868 Expression => Relocate_Node (Expr)));
2872 (Pragma_Argument_Associations => Args,
2873 Pragma_Name => Chars (Id));
2878 when Aspect_Part_Of =>
2879 if Nkind_In (N, N_Object_Declaration,
2880 N_Package_Instantiation)
2881 or else Is_Single_Concurrent_Type_Declaration (N)
2884 (Pragma_Argument_Associations => New_List (
2885 Make_Pragma_Argument_Association (Loc,
2886 Expression => Relocate_Node (Expr))),
2887 Pragma_Name => Name_Part_Of);
2889 Decorate (Aspect, Aitem);
2890 Insert_Pragma (Aitem);
2894 ("aspect & must apply to package instantiation, "
2895 & "object, single protected type or single task type",
2903 when Aspect_SPARK_Mode =>
2905 (Pragma_Argument_Associations => New_List (
2906 Make_Pragma_Argument_Association (Loc,
2907 Expression => Relocate_Node (Expr))),
2908 Pragma_Name => Name_SPARK_Mode);
2910 Decorate (Aspect, Aitem);
2911 Insert_Pragma (Aitem);
2916 -- Aspect Refined_Depends is never delayed because it is
2917 -- equivalent to a source pragma which appears in the
2918 -- declarations of the related subprogram body. To deal with
2919 -- forward references, the generated pragma is stored in the
2920 -- contract of the related subprogram body and later analyzed
2921 -- at the end of the declarative region. For details, see
2922 -- routine Analyze_Refined_Depends_In_Decl_Part.
2924 when Aspect_Refined_Depends =>
2926 (Pragma_Argument_Associations => New_List (
2927 Make_Pragma_Argument_Association (Loc,
2928 Expression => Relocate_Node (Expr))),
2929 Pragma_Name => Name_Refined_Depends);
2931 Decorate (Aspect, Aitem);
2932 Insert_Pragma (Aitem);
2937 -- Aspect Refined_Global is never delayed because it is
2938 -- equivalent to a source pragma which appears in the
2939 -- declarations of the related subprogram body. To deal with
2940 -- forward references, the generated pragma is stored in the
2941 -- contract of the related subprogram body and later analyzed
2942 -- at the end of the declarative region. For details, see
2943 -- routine Analyze_Refined_Global_In_Decl_Part.
2945 when Aspect_Refined_Global =>
2947 (Pragma_Argument_Associations => New_List (
2948 Make_Pragma_Argument_Association (Loc,
2949 Expression => Relocate_Node (Expr))),
2950 Pragma_Name => Name_Refined_Global);
2952 Decorate (Aspect, Aitem);
2953 Insert_Pragma (Aitem);
2958 when Aspect_Refined_Post =>
2960 (Pragma_Argument_Associations => New_List (
2961 Make_Pragma_Argument_Association (Loc,
2962 Expression => Relocate_Node (Expr))),
2963 Pragma_Name => Name_Refined_Post);
2965 Decorate (Aspect, Aitem);
2966 Insert_Pragma (Aitem);
2971 when Aspect_Refined_State =>
2973 -- The corresponding pragma for Refined_State is inserted in
2974 -- the declarations of the related package body. This action
2975 -- synchronizes both the source and from-aspect versions of
2978 if Nkind (N) = N_Package_Body then
2980 (Pragma_Argument_Associations => New_List (
2981 Make_Pragma_Argument_Association (Loc,
2982 Expression => Relocate_Node (Expr))),
2983 Pragma_Name => Name_Refined_State);
2985 Decorate (Aspect, Aitem);
2986 Insert_Pragma (Aitem);
2988 -- Otherwise the context is illegal
2992 ("aspect & must apply to a package body", Aspect, Id);
2997 -- Relative_Deadline
2999 when Aspect_Relative_Deadline =>
3001 (Pragma_Argument_Associations => New_List (
3002 Make_Pragma_Argument_Association (Loc,
3003 Expression => Relocate_Node (Expr))),
3004 Pragma_Name => Name_Relative_Deadline);
3006 -- If the aspect applies to a task, the corresponding pragma
3007 -- must appear within its declarations, not after.
3009 if Nkind (N) = N_Task_Type_Declaration then
3015 if No (Task_Definition (N)) then
3016 Set_Task_Definition (N,
3017 Make_Task_Definition (Loc,
3018 Visible_Declarations => New_List,
3019 End_Label => Empty));
3022 Def := Task_Definition (N);
3023 V := Visible_Declarations (Def);
3024 if not Is_Empty_List (V) then
3025 Insert_Before (First (V), Aitem);
3028 Set_Visible_Declarations (Def, New_List (Aitem));
3035 -- Aspect Volatile_Function is never delayed because it is
3036 -- equivalent to a source pragma which appears after the
3037 -- related subprogram.
3039 when Aspect_Volatile_Function =>
3041 (Pragma_Argument_Associations => New_List (
3042 Make_Pragma_Argument_Association (Loc,
3043 Expression => Relocate_Node (Expr))),
3044 Pragma_Name => Name_Volatile_Function);
3046 Decorate (Aspect, Aitem);
3047 Insert_Pragma (Aitem);
3050 -- Case 2e: Annotate aspect
3052 when Aspect_Annotate =>
3059 -- The argument can be a single identifier
3061 if Nkind (Expr) = N_Identifier then
3063 -- One level of parens is allowed
3065 if Paren_Count (Expr) > 1 then
3066 Error_Msg_F ("extra parentheses ignored", Expr);
3069 Set_Paren_Count (Expr, 0);
3071 -- Add the single item to the list
3073 Args := New_List (Expr);
3075 -- Otherwise we must have an aggregate
3077 elsif Nkind (Expr) = N_Aggregate then
3079 -- Must be positional
3081 if Present (Component_Associations (Expr)) then
3083 ("purely positional aggregate required", Expr);
3087 -- Must not be parenthesized
3089 if Paren_Count (Expr) /= 0 then
3090 Error_Msg_F ("extra parentheses ignored", Expr);
3093 -- List of arguments is list of aggregate expressions
3095 Args := Expressions (Expr);
3097 -- Anything else is illegal
3100 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3104 -- Prepare pragma arguments
3107 Arg := First (Args);
3108 while Present (Arg) loop
3110 Make_Pragma_Argument_Association (Sloc (Arg),
3111 Expression => Relocate_Node (Arg)));
3116 Make_Pragma_Argument_Association (Sloc (Ent),
3117 Chars => Name_Entity,
3118 Expression => Ent));
3121 (Pragma_Argument_Associations => Pargs,
3122 Pragma_Name => Name_Annotate);
3125 -- Case 3 : Aspects that don't correspond to pragma/attribute
3126 -- definition clause.
3128 -- Case 3a: The aspects listed below don't correspond to
3129 -- pragmas/attributes but do require delayed analysis.
3131 -- Default_Value can only apply to a scalar type
3133 when Aspect_Default_Value =>
3134 if not Is_Scalar_Type (E) then
3136 ("aspect Default_Value must apply to a scalar type", N);
3141 -- Default_Component_Value can only apply to an array type
3142 -- with scalar components.
3144 when Aspect_Default_Component_Value =>
3145 if not (Is_Array_Type (E)
3146 and then Is_Scalar_Type (Component_Type (E)))
3149 ("aspect Default_Component_Value can only apply to an "
3150 & "array of scalar components", N);
3155 -- Case 3b: The aspects listed below don't correspond to
3156 -- pragmas/attributes and don't need delayed analysis.
3158 -- Implicit_Dereference
3160 -- For Implicit_Dereference, External_Name and Link_Name, only
3161 -- the legality checks are done during the analysis, thus no
3162 -- delay is required.
3164 when Aspect_Implicit_Dereference =>
3165 Analyze_Aspect_Implicit_Dereference;
3170 when Aspect_Dimension =>
3171 Analyze_Aspect_Dimension (N, Id, Expr);
3176 when Aspect_Dimension_System =>
3177 Analyze_Aspect_Dimension_System (N, Id, Expr);
3180 -- Case 4: Aspects requiring special handling
3182 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3183 -- pragmas take care of the delay.
3187 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3188 -- with a first argument that is the expression, and a second
3189 -- argument that is an informative message if the test fails.
3190 -- This is inserted right after the declaration, to get the
3191 -- required pragma placement. The processing for the pragmas
3192 -- takes care of the required delay.
3194 when Pre_Post_Aspects => Pre_Post : declare
3198 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
3199 Pname := Name_Precondition;
3201 Pname := Name_Postcondition;
3204 -- Check that the class-wide predicate cannot be applied to
3205 -- an operation of a synchronized type that is not a tagged
3206 -- type. Other legality checks are performed when analyzing
3207 -- the contract of the operation.
3209 if Class_Present (Aspect)
3210 and then Is_Concurrent_Type (Current_Scope)
3211 and then not Is_Tagged_Type (Current_Scope)
3212 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3214 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3216 ("aspect % can only be specified for a primitive "
3217 & "operation of a tagged type", Aspect);
3222 -- If the expressions is of the form A and then B, then
3223 -- we generate separate Pre/Post aspects for the separate
3224 -- clauses. Since we allow multiple pragmas, there is no
3225 -- problem in allowing multiple Pre/Post aspects internally.
3226 -- These should be treated in reverse order (B first and
3227 -- A second) since they are later inserted just after N in
3228 -- the order they are treated. This way, the pragma for A
3229 -- ends up preceding the pragma for B, which may have an
3230 -- importance for the error raised (either constraint error
3231 -- or precondition error).
3233 -- We do not do this for Pre'Class, since we have to put
3234 -- these conditions together in a complex OR expression.
3236 -- We do not do this in ASIS mode, as ASIS relies on the
3237 -- original node representing the complete expression, when
3238 -- retrieving it through the source aspect table.
3241 and then (Pname = Name_Postcondition
3242 or else not Class_Present (Aspect))
3244 while Nkind (Expr) = N_And_Then loop
3245 Insert_After (Aspect,
3246 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
3247 Identifier => Identifier (Aspect),
3248 Expression => Relocate_Node (Left_Opnd (Expr)),
3249 Class_Present => Class_Present (Aspect),
3250 Split_PPC => True));
3251 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
3252 Eloc := Sloc (Expr);
3256 -- Build the precondition/postcondition pragma
3258 -- Add note about why we do NOT need Copy_Tree here???
3261 (Pragma_Argument_Associations => New_List (
3262 Make_Pragma_Argument_Association (Eloc,
3263 Chars => Name_Check,
3264 Expression => Relocate_Node (Expr))),
3265 Pragma_Name => Pname);
3267 -- Add message unless exception messages are suppressed
3269 if not Opt.Exception_Locations_Suppressed then
3270 Append_To (Pragma_Argument_Associations (Aitem),
3271 Make_Pragma_Argument_Association (Eloc,
3272 Chars => Name_Message,
3274 Make_String_Literal (Eloc,
3276 & Get_Name_String (Pname)
3278 & Build_Location_String (Eloc))));
3281 Set_Is_Delayed_Aspect (Aspect);
3283 -- For Pre/Post cases, insert immediately after the entity
3284 -- declaration, since that is the required pragma placement.
3285 -- Note that for these aspects, we do not have to worry
3286 -- about delay issues, since the pragmas themselves deal
3287 -- with delay of visibility for the expression analysis.
3289 Insert_Pragma (Aitem);
3296 when Aspect_Test_Case => Test_Case : declare
3298 Comp_Expr : Node_Id;
3299 Comp_Assn : Node_Id;
3305 if Nkind (Parent (N)) = N_Compilation_Unit then
3306 Error_Msg_Name_1 := Nam;
3307 Error_Msg_N ("incorrect placement of aspect `%`", E);
3311 if Nkind (Expr) /= N_Aggregate then
3312 Error_Msg_Name_1 := Nam;
3314 ("wrong syntax for aspect `%` for &", Id, E);
3318 -- Make pragma expressions refer to the original aspect
3319 -- expressions through the Original_Node link. This is used
3320 -- in semantic analysis for ASIS mode, so that the original
3321 -- expression also gets analyzed.
3323 Comp_Expr := First (Expressions (Expr));
3324 while Present (Comp_Expr) loop
3325 New_Expr := Relocate_Node (Comp_Expr);
3327 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3328 Expression => New_Expr));
3332 Comp_Assn := First (Component_Associations (Expr));
3333 while Present (Comp_Assn) loop
3334 if List_Length (Choices (Comp_Assn)) /= 1
3336 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3338 Error_Msg_Name_1 := Nam;
3340 ("wrong syntax for aspect `%` for &", Id, E);
3345 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3346 Chars => Chars (First (Choices (Comp_Assn))),
3348 Relocate_Node (Expression (Comp_Assn))));
3352 -- Build the test-case pragma
3355 (Pragma_Argument_Associations => Args,
3356 Pragma_Name => Nam);
3361 when Aspect_Contract_Cases =>
3363 (Pragma_Argument_Associations => New_List (
3364 Make_Pragma_Argument_Association (Loc,
3365 Expression => Relocate_Node (Expr))),
3366 Pragma_Name => Nam);
3368 Decorate (Aspect, Aitem);
3369 Insert_Pragma (Aitem);
3372 -- Case 5: Special handling for aspects with an optional
3373 -- boolean argument.
3375 -- In the delayed case, the corresponding pragma cannot be
3376 -- generated yet because the evaluation of the boolean needs
3377 -- to be delayed till the freeze point.
3379 when Boolean_Aspects |
3380 Library_Unit_Aspects =>
3382 Set_Is_Boolean_Aspect (Aspect);
3384 -- Lock_Free aspect only apply to protected objects
3386 if A_Id = Aspect_Lock_Free then
3387 if Ekind (E) /= E_Protected_Type then
3388 Error_Msg_Name_1 := Nam;
3390 ("aspect % only applies to a protected object",
3394 -- Set the Uses_Lock_Free flag to True if there is no
3395 -- expression or if the expression is True. The
3396 -- evaluation of this aspect should be delayed to the
3397 -- freeze point (why???)
3400 or else Is_True (Static_Boolean (Expr))
3402 Set_Uses_Lock_Free (E);
3405 Record_Rep_Item (E, Aspect);
3410 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3411 Analyze_Aspect_Export_Import;
3413 -- Disable_Controlled
3415 elsif A_Id = Aspect_Disable_Controlled then
3416 if Ekind (E) /= E_Record_Type
3417 or else not Is_Controlled (E)
3420 ("aspect % requires controlled record type", Aspect);
3424 -- If we're in a generic template, we don't want to try
3425 -- to disable controlled types, because typical usage is
3426 -- "Disable_Controlled => not <some_check>'Enabled", and
3427 -- the value of Enabled is not known until we see a
3428 -- particular instance. In such a context, we just need
3429 -- to preanalyze the expression for legality.
3431 if Expander_Active then
3432 Analyze_And_Resolve (Expr, Standard_Boolean);
3434 if not Present (Expr)
3435 or else Is_True (Static_Boolean (Expr))
3437 Set_Disable_Controlled (E);
3440 elsif Serious_Errors_Detected = 0 then
3441 Preanalyze_And_Resolve (Expr, Standard_Boolean);
3447 -- Library unit aspects require special handling in the case
3448 -- of a package declaration, the pragma needs to be inserted
3449 -- in the list of declarations for the associated package.
3450 -- There is no issue of visibility delay for these aspects.
3452 if A_Id in Library_Unit_Aspects
3454 Nkind_In (N, N_Package_Declaration,
3455 N_Generic_Package_Declaration)
3456 and then Nkind (Parent (N)) /= N_Compilation_Unit
3458 -- Aspect is legal on a local instantiation of a library-
3459 -- level generic unit.
3461 and then not Is_Generic_Instance (Defining_Entity (N))
3464 ("incorrect context for library unit aspect&", Id);
3468 -- Cases where we do not delay, includes all cases where the
3469 -- expression is missing other than the above cases.
3471 if not Delay_Required or else No (Expr) then
3473 -- Exclude aspects Export and Import because their pragma
3474 -- syntax does not map directly to a Boolean aspect.
3476 if A_Id /= Aspect_Export
3477 and then A_Id /= Aspect_Import
3480 (Pragma_Argument_Associations => New_List (
3481 Make_Pragma_Argument_Association (Sloc (Ent),
3482 Expression => Ent)),
3483 Pragma_Name => Chars (Id));
3486 Delay_Required := False;
3488 -- In general cases, the corresponding pragma/attribute
3489 -- definition clause will be inserted later at the freezing
3490 -- point, and we do not need to build it now.
3498 -- This is special because for access types we need to generate
3499 -- an attribute definition clause. This also works for single
3500 -- task declarations, but it does not work for task type
3501 -- declarations, because we have the case where the expression
3502 -- references a discriminant of the task type. That can't use
3503 -- an attribute definition clause because we would not have
3504 -- visibility on the discriminant. For that case we must
3505 -- generate a pragma in the task definition.
3507 when Aspect_Storage_Size =>
3511 if Ekind (E) = E_Task_Type then
3513 Decl : constant Node_Id := Declaration_Node (E);
3516 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3518 -- If no task definition, create one
3520 if No (Task_Definition (Decl)) then
3521 Set_Task_Definition (Decl,
3522 Make_Task_Definition (Loc,
3523 Visible_Declarations => Empty_List,
3524 End_Label => Empty));
3527 -- Create a pragma and put it at the start of the task
3528 -- definition for the task type declaration.
3531 (Pragma_Argument_Associations => New_List (
3532 Make_Pragma_Argument_Association (Loc,
3533 Expression => Relocate_Node (Expr))),
3534 Pragma_Name => Name_Storage_Size);
3538 Visible_Declarations (Task_Definition (Decl)));
3542 -- All other cases, generate attribute definition
3546 Make_Attribute_Definition_Clause (Loc,
3548 Chars => Chars (Id),
3549 Expression => Relocate_Node (Expr));
3553 -- Attach the corresponding pragma/attribute definition clause to
3554 -- the aspect specification node.
3556 if Present (Aitem) then
3557 Set_From_Aspect_Specification (Aitem);
3560 -- In the context of a compilation unit, we directly put the
3561 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3562 -- node (no delay is required here) except for aspects on a
3563 -- subprogram body (see below) and a generic package, for which we
3564 -- need to introduce the pragma before building the generic copy
3565 -- (see sem_ch12), and for package instantiations, where the
3566 -- library unit pragmas are better handled early.
3568 if Nkind (Parent (N)) = N_Compilation_Unit
3569 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3572 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3575 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3577 -- For a Boolean aspect, create the corresponding pragma if
3578 -- no expression or if the value is True.
3580 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3581 if Is_True (Static_Boolean (Expr)) then
3583 (Pragma_Argument_Associations => New_List (
3584 Make_Pragma_Argument_Association (Sloc (Ent),
3585 Expression => Ent)),
3586 Pragma_Name => Chars (Id));
3588 Set_From_Aspect_Specification (Aitem, True);
3589 Set_Corresponding_Aspect (Aitem, Aspect);
3596 -- If the aspect is on a subprogram body (relevant aspect
3597 -- is Inline), add the pragma in front of the declarations.
3599 if Nkind (N) = N_Subprogram_Body then
3600 if No (Declarations (N)) then
3601 Set_Declarations (N, New_List);
3604 Prepend (Aitem, Declarations (N));
3606 elsif Nkind (N) = N_Generic_Package_Declaration then
3607 if No (Visible_Declarations (Specification (N))) then
3608 Set_Visible_Declarations (Specification (N), New_List);
3612 Visible_Declarations (Specification (N)));
3614 elsif Nkind (N) = N_Package_Instantiation then
3616 Spec : constant Node_Id :=
3617 Specification (Instance_Spec (N));
3619 if No (Visible_Declarations (Spec)) then
3620 Set_Visible_Declarations (Spec, New_List);
3623 Prepend (Aitem, Visible_Declarations (Spec));
3627 if No (Pragmas_After (Aux)) then
3628 Set_Pragmas_After (Aux, New_List);
3631 Append (Aitem, Pragmas_After (Aux));
3638 -- The evaluation of the aspect is delayed to the freezing point.
3639 -- The pragma or attribute clause if there is one is then attached
3640 -- to the aspect specification which is put in the rep item list.
3642 if Delay_Required then
3643 if Present (Aitem) then
3644 Set_Is_Delayed_Aspect (Aitem);
3645 Set_Aspect_Rep_Item (Aspect, Aitem);
3646 Set_Parent (Aitem, Aspect);
3649 Set_Is_Delayed_Aspect (Aspect);
3651 -- In the case of Default_Value, link the aspect to base type
3652 -- as well, even though it appears on a first subtype. This is
3653 -- mandated by the semantics of the aspect. Do not establish
3654 -- the link when processing the base type itself as this leads
3655 -- to a rep item circularity. Verify that we are dealing with
3656 -- a scalar type to prevent cascaded errors.
3658 if A_Id = Aspect_Default_Value
3659 and then Is_Scalar_Type (E)
3660 and then Base_Type (E) /= E
3662 Set_Has_Delayed_Aspects (Base_Type (E));
3663 Record_Rep_Item (Base_Type (E), Aspect);
3666 Set_Has_Delayed_Aspects (E);
3667 Record_Rep_Item (E, Aspect);
3669 -- When delay is not required and the context is a package or a
3670 -- subprogram body, insert the pragma in the body declarations.
3672 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3673 if No (Declarations (N)) then
3674 Set_Declarations (N, New_List);
3677 -- The pragma is added before source declarations
3679 Prepend_To (Declarations (N), Aitem);
3681 -- When delay is not required and the context is not a compilation
3682 -- unit, we simply insert the pragma/attribute definition clause
3685 elsif Present (Aitem) then
3686 Insert_After (Ins_Node, Aitem);
3689 end Analyze_One_Aspect;
3693 end loop Aspect_Loop;
3695 if Has_Delayed_Aspects (E) then
3696 Ensure_Freeze_Node (E);
3698 end Analyze_Aspect_Specifications;
3700 ---------------------------------------------------
3701 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3702 ---------------------------------------------------
3704 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3705 Body_Id : constant Entity_Id := Defining_Entity (N);
3707 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3708 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3709 -- error message depending on the aspects involved. Spec_Id denotes the
3710 -- entity of the corresponding spec.
3712 --------------------------------
3713 -- Diagnose_Misplaced_Aspects --
3714 --------------------------------
3716 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3717 procedure Misplaced_Aspect_Error
3720 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3721 -- the name of the refined version of the aspect.
3723 ----------------------------
3724 -- Misplaced_Aspect_Error --
3725 ----------------------------
3727 procedure Misplaced_Aspect_Error
3731 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3732 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3735 -- The corresponding spec already contains the aspect in question
3736 -- and the one appearing on the body must be the refined form:
3738 -- procedure P with Global ...;
3739 -- procedure P with Global ... is ... end P;
3743 if Has_Aspect (Spec_Id, Asp_Id) then
3744 Error_Msg_Name_1 := Asp_Nam;
3746 -- Subunits cannot carry aspects that apply to a subprogram
3749 if Nkind (Parent (N)) = N_Subunit then
3750 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3752 -- Otherwise suggest the refined form
3755 Error_Msg_Name_2 := Ref_Nam;
3756 Error_Msg_N ("aspect % should be %", Asp);
3759 -- Otherwise the aspect must appear on the spec, not on the body
3762 -- procedure P with Global ... is ... end P;
3766 ("aspect specification must appear on initial declaration",
3769 end Misplaced_Aspect_Error;
3776 -- Start of processing for Diagnose_Misplaced_Aspects
3779 -- Iterate over the aspect specifications and emit specific errors
3780 -- where applicable.
3782 Asp := First (Aspect_Specifications (N));
3783 while Present (Asp) loop
3784 Asp_Nam := Chars (Identifier (Asp));
3786 -- Do not emit errors on aspects that can appear on a subprogram
3787 -- body. This scenario occurs when the aspect specification list
3788 -- contains both misplaced and properly placed aspects.
3790 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3793 -- Special diagnostics for SPARK aspects
3795 elsif Asp_Nam = Name_Depends then
3796 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3798 elsif Asp_Nam = Name_Global then
3799 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3801 elsif Asp_Nam = Name_Post then
3802 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3804 -- Otherwise a language-defined aspect is misplaced
3808 ("aspect specification must appear on initial declaration",
3814 end Diagnose_Misplaced_Aspects;
3818 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
3820 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3823 -- Language-defined aspects cannot be associated with a subprogram body
3824 -- [stub] if the subprogram has a spec. Certain implementation defined
3825 -- aspects are allowed to break this rule (for all applicable cases, see
3826 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3828 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
3829 Diagnose_Misplaced_Aspects (Spec_Id);
3831 Analyze_Aspect_Specifications (N, Body_Id);
3833 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3835 -----------------------
3836 -- Analyze_At_Clause --
3837 -----------------------
3839 -- An at clause is replaced by the corresponding Address attribute
3840 -- definition clause that is the preferred approach in Ada 95.
3842 procedure Analyze_At_Clause (N : Node_Id) is
3843 CS : constant Boolean := Comes_From_Source (N);
3846 -- This is an obsolescent feature
3848 Check_Restriction (No_Obsolescent_Features, N);
3850 if Warn_On_Obsolescent_Feature then
3852 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3854 ("\?j?use address attribute definition clause instead", N);
3857 -- Rewrite as address clause
3860 Make_Attribute_Definition_Clause (Sloc (N),
3861 Name => Identifier (N),
3862 Chars => Name_Address,
3863 Expression => Expression (N)));
3865 -- We preserve Comes_From_Source, since logically the clause still comes
3866 -- from the source program even though it is changed in form.
3868 Set_Comes_From_Source (N, CS);
3870 -- Analyze rewritten clause
3872 Analyze_Attribute_Definition_Clause (N);
3873 end Analyze_At_Clause;
3875 -----------------------------------------
3876 -- Analyze_Attribute_Definition_Clause --
3877 -----------------------------------------
3879 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3880 Loc : constant Source_Ptr := Sloc (N);
3881 Nam : constant Node_Id := Name (N);
3882 Attr : constant Name_Id := Chars (N);
3883 Expr : constant Node_Id := Expression (N);
3884 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3887 -- The entity of Nam after it is analyzed. In the case of an incomplete
3888 -- type, this is the underlying type.
3891 -- The underlying entity to which the attribute applies. Generally this
3892 -- is the Underlying_Type of Ent, except in the case where the clause
3893 -- applies to the full view of an incomplete or private type, in which
3894 -- case U_Ent is just a copy of Ent.
3896 FOnly : Boolean := False;
3897 -- Reset to True for subtype specific attribute (Alignment, Size)
3898 -- and for stream attributes, i.e. those cases where in the call to
3899 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3900 -- are checked. Note that the case of stream attributes is not clear
3901 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3902 -- Storage_Size for derived task types, but that is also clearly
3905 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3906 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3907 -- definition clauses.
3909 function Duplicate_Clause return Boolean;
3910 -- This routine checks if the aspect for U_Ent being given by attribute
3911 -- definition clause N is for an aspect that has already been specified,
3912 -- and if so gives an error message. If there is a duplicate, True is
3913 -- returned, otherwise if there is no error, False is returned.
3915 procedure Check_Indexing_Functions;
3916 -- Check that the function in Constant_Indexing or Variable_Indexing
3917 -- attribute has the proper type structure. If the name is overloaded,
3918 -- check that some interpretation is legal.
3920 procedure Check_Iterator_Functions;
3921 -- Check that there is a single function in Default_Iterator attribute
3922 -- has the proper type structure.
3924 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3925 -- Common legality check for the previous two
3927 -----------------------------------
3928 -- Analyze_Stream_TSS_Definition --
3929 -----------------------------------
3931 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3932 Subp : Entity_Id := Empty;
3937 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3938 -- True for Read attribute, False for other attributes
3940 function Has_Good_Profile
3942 Report : Boolean := False) return Boolean;
3943 -- Return true if the entity is a subprogram with an appropriate
3944 -- profile for the attribute being defined. If result is False and
3945 -- Report is True, function emits appropriate error.
3947 ----------------------
3948 -- Has_Good_Profile --
3949 ----------------------
3951 function Has_Good_Profile
3953 Report : Boolean := False) return Boolean
3955 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3956 (False => E_Procedure, True => E_Function);
3957 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3962 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3966 F := First_Formal (Subp);
3969 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3970 or else Designated_Type (Etype (F)) /=
3971 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3976 if not Is_Function then
3980 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3981 (False => E_In_Parameter,
3982 True => E_Out_Parameter);
3984 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3991 -- If the attribute specification comes from an aspect
3992 -- specification for a class-wide stream, the parameter must be
3993 -- a class-wide type of the entity to which the aspect applies.
3995 if From_Aspect_Specification (N)
3996 and then Class_Present (Parent (N))
3997 and then Is_Class_Wide_Type (Typ)
4003 Typ := Etype (Subp);
4006 -- Verify that the prefix of the attribute and the local name for
4007 -- the type of the formal match, or one is the class-wide of the
4008 -- other, in the case of a class-wide stream operation.
4010 if Base_Type (Typ) = Base_Type (Ent)
4011 or else (Is_Class_Wide_Type (Typ)
4012 and then Typ = Class_Wide_Type (Base_Type (Ent)))
4013 or else (Is_Class_Wide_Type (Ent)
4014 and then Ent = Class_Wide_Type (Base_Type (Typ)))
4021 if Present (Next_Formal (F)) then
4024 elsif not Is_Scalar_Type (Typ)
4025 and then not Is_First_Subtype (Typ)
4026 and then not Is_Class_Wide_Type (Typ)
4028 if Report and not Is_First_Subtype (Typ) then
4030 ("subtype of formal in stream operation must be a first "
4031 & "subtype", Parameter_Type (Parent (F)));
4039 end Has_Good_Profile;
4041 -- Start of processing for Analyze_Stream_TSS_Definition
4046 if not Is_Type (U_Ent) then
4047 Error_Msg_N ("local name must be a subtype", Nam);
4050 elsif not Is_First_Subtype (U_Ent) then
4051 Error_Msg_N ("local name must be a first subtype", Nam);
4055 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
4057 -- If Pnam is present, it can be either inherited from an ancestor
4058 -- type (in which case it is legal to redefine it for this type), or
4059 -- be a previous definition of the attribute for the same type (in
4060 -- which case it is illegal).
4062 -- In the first case, it will have been analyzed already, and we
4063 -- can check that its profile does not match the expected profile
4064 -- for a stream attribute of U_Ent. In the second case, either Pnam
4065 -- has been analyzed (and has the expected profile), or it has not
4066 -- been analyzed yet (case of a type that has not been frozen yet
4067 -- and for which the stream attribute has been set using Set_TSS).
4070 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4072 Error_Msg_Sloc := Sloc (Pnam);
4073 Error_Msg_Name_1 := Attr;
4074 Error_Msg_N ("% attribute already defined #", Nam);
4080 if Is_Entity_Name (Expr) then
4081 if not Is_Overloaded (Expr) then
4082 if Has_Good_Profile (Entity (Expr), Report => True) then
4083 Subp := Entity (Expr);
4087 Get_First_Interp (Expr, I, It);
4088 while Present (It.Nam) loop
4089 if Has_Good_Profile (It.Nam) then
4094 Get_Next_Interp (I, It);
4099 if Present (Subp) then
4100 if Is_Abstract_Subprogram (Subp) then
4101 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4104 -- A stream subprogram for an interface type must be a null
4105 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4106 -- of an interface is not an interface type (3.9.4 (6.b/2)).
4108 elsif Is_Interface (U_Ent)
4109 and then not Is_Class_Wide_Type (U_Ent)
4110 and then not Inside_A_Generic
4112 (Ekind (Subp) = E_Function
4116 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
4119 ("stream subprogram for interface type must be null "
4120 & "procedure", Expr);
4123 Set_Entity (Expr, Subp);
4124 Set_Etype (Expr, Etype (Subp));
4126 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
4129 Error_Msg_Name_1 := Attr;
4130 Error_Msg_N ("incorrect expression for% attribute", Expr);
4132 end Analyze_Stream_TSS_Definition;
4134 ------------------------------
4135 -- Check_Indexing_Functions --
4136 ------------------------------
4138 procedure Check_Indexing_Functions is
4139 Indexing_Found : Boolean := False;
4141 procedure Check_Inherited_Indexing;
4142 -- For a derived type, check that no indexing aspect is specified
4143 -- for the type if it is also inherited
4145 procedure Check_One_Function (Subp : Entity_Id);
4146 -- Check one possible interpretation. Sets Indexing_Found True if a
4147 -- legal indexing function is found.
4149 procedure Illegal_Indexing (Msg : String);
4150 -- Diagnose illegal indexing function if not overloaded. In the
4151 -- overloaded case indicate that no legal interpretation exists.
4153 ------------------------------
4154 -- Check_Inherited_Indexing --
4155 ------------------------------
4157 procedure Check_Inherited_Indexing is
4158 Inherited : Node_Id;
4161 if Attr = Name_Constant_Indexing then
4163 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4164 else pragma Assert (Attr = Name_Variable_Indexing);
4166 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4169 if Present (Inherited) then
4170 if Debug_Flag_Dot_XX then
4173 -- OK if current attribute_definition_clause is expansion of
4174 -- inherited aspect.
4176 elsif Aspect_Rep_Item (Inherited) = N then
4179 -- Indicate the operation that must be overridden, rather than
4180 -- redefining the indexing aspect.
4184 ("indexing function already inherited from parent type");
4186 ("!override & instead",
4187 N, Entity (Expression (Inherited)));
4190 end Check_Inherited_Indexing;
4192 ------------------------
4193 -- Check_One_Function --
4194 ------------------------
4196 procedure Check_One_Function (Subp : Entity_Id) is
4197 Default_Element : Node_Id;
4198 Ret_Type : constant Entity_Id := Etype (Subp);
4201 if not Is_Overloadable (Subp) then
4202 Illegal_Indexing ("illegal indexing function for type&");
4205 elsif Scope (Subp) /= Scope (Ent) then
4206 if Nkind (Expr) = N_Expanded_Name then
4208 -- Indexing function can't be declared elsewhere
4211 ("indexing function must be declared in scope of type&");
4216 elsif No (First_Formal (Subp)) then
4218 ("Indexing requires a function that applies to type&");
4221 elsif No (Next_Formal (First_Formal (Subp))) then
4223 ("indexing function must have at least two parameters");
4226 elsif Is_Derived_Type (Ent) then
4227 Check_Inherited_Indexing;
4230 if not Check_Primitive_Function (Subp) then
4232 ("Indexing aspect requires a function that applies to type&");
4236 -- If partial declaration exists, verify that it is not tagged.
4238 if Ekind (Current_Scope) = E_Package
4239 and then Has_Private_Declaration (Ent)
4240 and then From_Aspect_Specification (N)
4242 List_Containing (Parent (Ent)) =
4243 Private_Declarations
4244 (Specification (Unit_Declaration_Node (Current_Scope)))
4245 and then Nkind (N) = N_Attribute_Definition_Clause
4252 First (Visible_Declarations
4254 (Unit_Declaration_Node (Current_Scope))));
4256 while Present (Decl) loop
4257 if Nkind (Decl) = N_Private_Type_Declaration
4258 and then Ent = Full_View (Defining_Identifier (Decl))
4259 and then Tagged_Present (Decl)
4260 and then No (Aspect_Specifications (Decl))
4263 ("Indexing aspect cannot be specified on full view "
4264 & "if partial view is tagged");
4273 -- An indexing function must return either the default element of
4274 -- the container, or a reference type. For variable indexing it
4275 -- must be the latter.
4278 Find_Value_Of_Aspect
4279 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4281 if Present (Default_Element) then
4282 Analyze (Default_Element);
4284 if Is_Entity_Name (Default_Element)
4285 and then not Covers (Entity (Default_Element), Ret_Type)
4289 ("wrong return type for indexing function");
4294 -- For variable_indexing the return type must be a reference type
4296 if Attr = Name_Variable_Indexing then
4297 if not Has_Implicit_Dereference (Ret_Type) then
4299 ("variable indexing must return a reference type");
4302 elsif Is_Access_Constant
4303 (Etype (First_Discriminant (Ret_Type)))
4306 ("variable indexing must return an access to variable");
4311 if Has_Implicit_Dereference (Ret_Type)
4313 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4316 ("constant indexing must return an access to constant");
4319 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4320 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4323 ("constant indexing must apply to an access to constant");
4328 -- All checks succeeded.
4330 Indexing_Found := True;
4331 end Check_One_Function;
4333 -----------------------
4334 -- Illegal_Indexing --
4335 -----------------------
4337 procedure Illegal_Indexing (Msg : String) is
4339 Error_Msg_NE (Msg, N, Ent);
4340 end Illegal_Indexing;
4342 -- Start of processing for Check_Indexing_Functions
4346 Check_Inherited_Indexing;
4351 if not Is_Overloaded (Expr) then
4352 Check_One_Function (Entity (Expr));
4360 Indexing_Found := False;
4361 Get_First_Interp (Expr, I, It);
4362 while Present (It.Nam) loop
4364 -- Note that analysis will have added the interpretation
4365 -- that corresponds to the dereference. We only check the
4366 -- subprogram itself.
4368 if Is_Overloadable (It.Nam) then
4369 Check_One_Function (It.Nam);
4372 Get_Next_Interp (I, It);
4377 if not Indexing_Found and then not Error_Posted (N) then
4379 ("aspect Indexing requires a local function that "
4380 & "applies to type&", Expr, Ent);
4382 end Check_Indexing_Functions;
4384 ------------------------------
4385 -- Check_Iterator_Functions --
4386 ------------------------------
4388 procedure Check_Iterator_Functions is
4389 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4390 -- Check one possible interpretation for validity
4392 ----------------------------
4393 -- Valid_Default_Iterator --
4394 ----------------------------
4396 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4397 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
4401 if not Check_Primitive_Function (Subp) then
4404 -- The return type must be derived from a type in an instance
4405 -- of Iterator.Interfaces, and thus its root type must have a
4408 elsif Chars (Root_T) /= Name_Forward_Iterator
4409 and then Chars (Root_T) /= Name_Reversible_Iterator
4414 Formal := First_Formal (Subp);
4417 -- False if any subsequent formal has no default expression
4419 Formal := Next_Formal (Formal);
4420 while Present (Formal) loop
4421 if No (Expression (Parent (Formal))) then
4425 Next_Formal (Formal);
4428 -- True if all subsequent formals have default expressions
4431 end Valid_Default_Iterator;
4433 -- Start of processing for Check_Iterator_Functions
4438 if not Is_Entity_Name (Expr) then
4439 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4442 if not Is_Overloaded (Expr) then
4443 if not Check_Primitive_Function (Entity (Expr)) then
4445 ("aspect Indexing requires a function that applies to type&",
4446 Entity (Expr), Ent);
4449 -- Flag the default_iterator as well as the denoted function.
4451 if not Valid_Default_Iterator (Entity (Expr)) then
4452 Error_Msg_N ("improper function for default iterator!", Expr);
4457 Default : Entity_Id := Empty;
4462 Get_First_Interp (Expr, I, It);
4463 while Present (It.Nam) loop
4464 if not Check_Primitive_Function (It.Nam)
4465 or else not Valid_Default_Iterator (It.Nam)
4469 elsif Present (Default) then
4471 -- An explicit one should override an implicit one
4473 if Comes_From_Source (Default) =
4474 Comes_From_Source (It.Nam)
4476 Error_Msg_N ("default iterator must be unique", Expr);
4477 Error_Msg_Sloc := Sloc (Default);
4478 Error_Msg_N ("\\possible interpretation#", Expr);
4479 Error_Msg_Sloc := Sloc (It.Nam);
4480 Error_Msg_N ("\\possible interpretation#", Expr);
4482 elsif Comes_From_Source (It.Nam) then
4489 Get_Next_Interp (I, It);
4492 if Present (Default) then
4493 Set_Entity (Expr, Default);
4494 Set_Is_Overloaded (Expr, False);
4497 ("no interpretation is a valid default iterator!", Expr);
4501 end Check_Iterator_Functions;
4503 -------------------------------
4504 -- Check_Primitive_Function --
4505 -------------------------------
4507 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4511 if Ekind (Subp) /= E_Function then
4515 if No (First_Formal (Subp)) then
4518 Ctrl := Etype (First_Formal (Subp));
4521 -- To be a primitive operation subprogram has to be in same scope.
4523 if Scope (Ctrl) /= Scope (Subp) then
4527 -- Type of formal may be the class-wide type, an access to such,
4528 -- or an incomplete view.
4531 or else Ctrl = Class_Wide_Type (Ent)
4533 (Ekind (Ctrl) = E_Anonymous_Access_Type
4534 and then (Designated_Type (Ctrl) = Ent
4536 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4538 (Ekind (Ctrl) = E_Incomplete_Type
4539 and then Full_View (Ctrl) = Ent)
4547 end Check_Primitive_Function;
4549 ----------------------
4550 -- Duplicate_Clause --
4551 ----------------------
4553 function Duplicate_Clause return Boolean is
4557 -- Nothing to do if this attribute definition clause comes from
4558 -- an aspect specification, since we could not be duplicating an
4559 -- explicit clause, and we dealt with the case of duplicated aspects
4560 -- in Analyze_Aspect_Specifications.
4562 if From_Aspect_Specification (N) then
4566 -- Otherwise current clause may duplicate previous clause, or a
4567 -- previously given pragma or aspect specification for the same
4570 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4573 Error_Msg_Name_1 := Chars (N);
4574 Error_Msg_Sloc := Sloc (A);
4576 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4581 end Duplicate_Clause;
4583 -- Start of processing for Analyze_Attribute_Definition_Clause
4586 -- The following code is a defense against recursion. Not clear that
4587 -- this can happen legitimately, but perhaps some error situations can
4588 -- cause it, and we did see this recursion during testing.
4590 if Analyzed (N) then
4593 Set_Analyzed (N, True);
4596 Check_Restriction_No_Use_Of_Attribute (N);
4598 -- Ignore some selected attributes in CodePeer mode since they are not
4599 -- relevant in this context.
4601 if CodePeer_Mode then
4604 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4605 -- internal representation of types by implicitly packing them.
4607 when Attribute_Component_Size =>
4608 Rewrite (N, Make_Null_Statement (Sloc (N)));
4616 -- Process Ignore_Rep_Clauses option
4618 if Ignore_Rep_Clauses then
4621 -- The following should be ignored. They do not affect legality
4622 -- and may be target dependent. The basic idea of -gnatI is to
4623 -- ignore any rep clauses that may be target dependent but do not
4624 -- affect legality (except possibly to be rejected because they
4625 -- are incompatible with the compilation target).
4627 when Attribute_Alignment |
4628 Attribute_Bit_Order |
4629 Attribute_Component_Size |
4630 Attribute_Machine_Radix |
4631 Attribute_Object_Size |
4634 Attribute_Stream_Size |
4635 Attribute_Value_Size =>
4636 Kill_Rep_Clause (N);
4639 -- The following should not be ignored, because in the first place
4640 -- they are reasonably portable, and should not cause problems
4641 -- in compiling code from another target, and also they do affect
4642 -- legality, e.g. failing to provide a stream attribute for a type
4643 -- may make a program illegal.
4645 when Attribute_External_Tag |
4649 Attribute_Simple_Storage_Pool |
4650 Attribute_Storage_Pool |
4651 Attribute_Storage_Size |
4655 -- We do not do anything here with address clauses, they will be
4656 -- removed by Freeze later on, but for now, it works better to
4657 -- keep then in the tree.
4659 when Attribute_Address =>
4662 -- Other cases are errors ("attribute& cannot be set with
4663 -- definition clause"), which will be caught below.
4671 Ent := Entity (Nam);
4673 if Rep_Item_Too_Early (Ent, N) then
4677 -- Rep clause applies to full view of incomplete type or private type if
4678 -- we have one (if not, this is a premature use of the type). However,
4679 -- certain semantic checks need to be done on the specified entity (i.e.
4680 -- the private view), so we save it in Ent.
4682 if Is_Private_Type (Ent)
4683 and then Is_Derived_Type (Ent)
4684 and then not Is_Tagged_Type (Ent)
4685 and then No (Full_View (Ent))
4687 -- If this is a private type whose completion is a derivation from
4688 -- another private type, there is no full view, and the attribute
4689 -- belongs to the type itself, not its underlying parent.
4693 elsif Ekind (Ent) = E_Incomplete_Type then
4695 -- The attribute applies to the full view, set the entity of the
4696 -- attribute definition accordingly.
4698 Ent := Underlying_Type (Ent);
4700 Set_Entity (Nam, Ent);
4703 U_Ent := Underlying_Type (Ent);
4706 -- Avoid cascaded error
4708 if Etype (Nam) = Any_Type then
4711 -- Must be declared in current scope or in case of an aspect
4712 -- specification, must be visible in current scope.
4714 elsif Scope (Ent) /= Current_Scope
4716 not (From_Aspect_Specification (N)
4717 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4719 Error_Msg_N ("entity must be declared in this scope", Nam);
4722 -- Must not be a source renaming (we do have some cases where the
4723 -- expander generates a renaming, and those cases are OK, in such
4724 -- cases any attribute applies to the renamed object as well).
4726 elsif Is_Object (Ent)
4727 and then Present (Renamed_Object (Ent))
4729 -- Case of renamed object from source, this is an error
4731 if Comes_From_Source (Renamed_Object (Ent)) then
4732 Get_Name_String (Chars (N));
4733 Error_Msg_Strlen := Name_Len;
4734 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4736 ("~ clause not allowed for a renaming declaration "
4737 & "(RM 13.1(6))", Nam);
4740 -- For the case of a compiler generated renaming, the attribute
4741 -- definition clause applies to the renamed object created by the
4742 -- expander. The easiest general way to handle this is to create a
4743 -- copy of the attribute definition clause for this object.
4745 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4747 Make_Attribute_Definition_Clause (Loc,
4749 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4751 Expression => Duplicate_Subexpr (Expression (N))));
4753 -- If the renamed object is not an entity, it must be a dereference
4754 -- of an unconstrained function call, and we must introduce a new
4755 -- declaration to capture the expression. This is needed in the case
4756 -- of 'Alignment, where the original declaration must be rewritten.
4760 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4764 -- If no underlying entity, use entity itself, applies to some
4765 -- previously detected error cases ???
4767 elsif No (U_Ent) then
4770 -- Cannot specify for a subtype (exception Object/Value_Size)
4772 elsif Is_Type (U_Ent)
4773 and then not Is_First_Subtype (U_Ent)
4774 and then Id /= Attribute_Object_Size
4775 and then Id /= Attribute_Value_Size
4776 and then not From_At_Mod (N)
4778 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4782 Set_Entity (N, U_Ent);
4784 -- Switch on particular attribute
4792 -- Address attribute definition clause
4794 when Attribute_Address => Address : begin
4796 -- A little error check, catch for X'Address use X'Address;
4798 if Nkind (Nam) = N_Identifier
4799 and then Nkind (Expr) = N_Attribute_Reference
4800 and then Attribute_Name (Expr) = Name_Address
4801 and then Nkind (Prefix (Expr)) = N_Identifier
4802 and then Chars (Nam) = Chars (Prefix (Expr))
4805 ("address for & is self-referencing", Prefix (Expr), Ent);
4809 -- Not that special case, carry on with analysis of expression
4811 Analyze_And_Resolve (Expr, RTE (RE_Address));
4813 -- Even when ignoring rep clauses we need to indicate that the
4814 -- entity has an address clause and thus it is legal to declare
4815 -- it imported. Freeze will get rid of the address clause later.
4817 if Ignore_Rep_Clauses then
4818 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4819 Record_Rep_Item (U_Ent, N);
4825 if Duplicate_Clause then
4828 -- Case of address clause for subprogram
4830 elsif Is_Subprogram (U_Ent) then
4831 if Has_Homonym (U_Ent) then
4833 ("address clause cannot be given for overloaded "
4834 & "subprogram", Nam);
4838 -- For subprograms, all address clauses are permitted, and we
4839 -- mark the subprogram as having a deferred freeze so that Gigi
4840 -- will not elaborate it too soon.
4842 -- Above needs more comments, what is too soon about???
4844 Set_Has_Delayed_Freeze (U_Ent);
4846 -- Case of address clause for entry
4848 elsif Ekind (U_Ent) = E_Entry then
4849 if Nkind (Parent (N)) = N_Task_Body then
4851 ("entry address must be specified in task spec", Nam);
4855 -- For entries, we require a constant address
4857 Check_Constant_Address_Clause (Expr, U_Ent);
4859 -- Special checks for task types
4861 if Is_Task_Type (Scope (U_Ent))
4862 and then Comes_From_Source (Scope (U_Ent))
4865 ("??entry address declared for entry in task type", N);
4867 ("\??only one task can be declared of this type", N);
4870 -- Entry address clauses are obsolescent
4872 Check_Restriction (No_Obsolescent_Features, N);
4874 if Warn_On_Obsolescent_Feature then
4876 ("?j?attaching interrupt to task entry is an obsolescent "
4877 & "feature (RM J.7.1)", N);
4879 ("\?j?use interrupt procedure instead", N);
4882 -- Case of an address clause for a controlled object which we
4883 -- consider to be erroneous.
4885 elsif Is_Controlled (Etype (U_Ent))
4886 or else Has_Controlled_Component (Etype (U_Ent))
4889 ("??controlled object& must not be overlaid", Nam, U_Ent);
4891 ("\??Program_Error will be raised at run time", Nam);
4892 Insert_Action (Declaration_Node (U_Ent),
4893 Make_Raise_Program_Error (Loc,
4894 Reason => PE_Overlaid_Controlled_Object));
4897 -- Case of address clause for a (non-controlled) object
4899 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
4901 Expr : constant Node_Id := Expression (N);
4906 -- Exported variables cannot have an address clause, because
4907 -- this cancels the effect of the pragma Export.
4909 if Is_Exported (U_Ent) then
4911 ("cannot export object with address clause", Nam);
4915 Find_Overlaid_Entity (N, O_Ent, Off);
4917 if Present (O_Ent) then
4919 -- If the object overlays a constant object, mark it so
4921 if Is_Constant_Object (O_Ent) then
4922 Set_Overlays_Constant (U_Ent);
4925 -- If the address clause is of the form:
4927 -- for X'Address use Y'Address;
4931 -- C : constant Address := Y'Address;
4933 -- for X'Address use C;
4935 -- then we make an entry in the table to check the size
4936 -- and alignment of the overlaying variable. But we defer
4937 -- this check till after code generation to take full
4938 -- advantage of the annotation done by the back end.
4940 -- If the entity has a generic type, the check will be
4941 -- performed in the instance if the actual type justifies
4942 -- it, and we do not insert the clause in the table to
4943 -- prevent spurious warnings.
4945 -- Note: we used to test Comes_From_Source and only give
4946 -- this warning for source entities, but we have removed
4947 -- this test. It really seems bogus to generate overlays
4948 -- that would trigger this warning in generated code.
4949 -- Furthermore, by removing the test, we handle the
4950 -- aspect case properly.
4952 if Is_Object (O_Ent)
4953 and then not Is_Generic_Type (Etype (U_Ent))
4954 and then Address_Clause_Overlay_Warnings
4956 Address_Clause_Checks.Append
4957 ((N, U_Ent, No_Uint, O_Ent, Off));
4960 -- If this is not an overlay, mark a variable as being
4961 -- volatile to prevent unwanted optimizations. It's a
4962 -- conservative interpretation of RM 13.3(19) for the
4963 -- cases where the compiler cannot detect potential
4964 -- aliasing issues easily and it also covers the case
4965 -- of an absolute address where the volatile aspect is
4966 -- kind of implicit.
4968 if Ekind (U_Ent) = E_Variable then
4969 Set_Treat_As_Volatile (U_Ent);
4972 -- Make an entry in the table for an absolute address as
4973 -- above to check that the value is compatible with the
4974 -- alignment of the object.
4977 Addr : constant Node_Id := Address_Value (Expr);
4979 if Compile_Time_Known_Value (Addr)
4980 and then Address_Clause_Overlay_Warnings
4982 Address_Clause_Checks.Append
4983 ((N, U_Ent, Expr_Value (Addr), Empty, False));
4988 -- Overlaying controlled objects is erroneous. Emit warning
4989 -- but continue analysis because program is itself legal,
4990 -- and back end must see address clause.
4993 and then (Has_Controlled_Component (Etype (O_Ent))
4994 or else Is_Controlled (Etype (O_Ent)))
4995 and then not Inside_A_Generic
4998 ("??cannot use overlays with controlled objects", Expr);
5000 ("\??Program_Error will be raised at run time", Expr);
5001 Insert_Action (Declaration_Node (U_Ent),
5002 Make_Raise_Program_Error (Loc,
5003 Reason => PE_Overlaid_Controlled_Object));
5005 -- Issue an unconditional warning for a constant overlaying
5006 -- a variable. For the reverse case, we will issue it only
5007 -- if the variable is modified.
5009 elsif Ekind (U_Ent) = E_Constant
5010 and then Present (O_Ent)
5011 and then not Overlays_Constant (U_Ent)
5012 and then Address_Clause_Overlay_Warnings
5014 Error_Msg_N ("??constant overlays a variable", Expr);
5016 -- Imported variables can have an address clause, but then
5017 -- the import is pretty meaningless except to suppress
5018 -- initializations, so we do not need such variables to
5019 -- be statically allocated (and in fact it causes trouble
5020 -- if the address clause is a local value).
5022 elsif Is_Imported (U_Ent) then
5023 Set_Is_Statically_Allocated (U_Ent, False);
5026 -- We mark a possible modification of a variable with an
5027 -- address clause, since it is likely aliasing is occurring.
5029 Note_Possible_Modification (Nam, Sure => False);
5031 -- Legality checks on the address clause for initialized
5032 -- objects is deferred until the freeze point, because
5033 -- a subsequent pragma might indicate that the object
5034 -- is imported and thus not initialized. Also, the address
5035 -- clause might involve entities that have yet to be
5038 Set_Has_Delayed_Freeze (U_Ent);
5040 -- If an initialization call has been generated for this
5041 -- object, it needs to be deferred to after the freeze node
5042 -- we have just now added, otherwise GIGI will see a
5043 -- reference to the variable (as actual to the IP call)
5044 -- before its definition.
5047 Init_Call : constant Node_Id :=
5048 Remove_Init_Call (U_Ent, N);
5051 if Present (Init_Call) then
5052 Append_Freeze_Action (U_Ent, Init_Call);
5054 -- Reset Initialization_Statements pointer so that
5055 -- if there is a pragma Import further down, it can
5056 -- clear any default initialization.
5058 Set_Initialization_Statements (U_Ent, Init_Call);
5062 -- Entity has delayed freeze, so we will generate an
5063 -- alignment check at the freeze point unless suppressed.
5065 if not Range_Checks_Suppressed (U_Ent)
5066 and then not Alignment_Checks_Suppressed (U_Ent)
5068 Set_Check_Address_Alignment (N);
5071 -- Kill the size check code, since we are not allocating
5072 -- the variable, it is somewhere else.
5074 Kill_Size_Check_Code (U_Ent);
5077 -- Not a valid entity for an address clause
5080 Error_Msg_N ("address cannot be given for &", Nam);
5088 -- Alignment attribute definition clause
5090 when Attribute_Alignment => Alignment : declare
5091 Align : constant Uint := Get_Alignment_Value (Expr);
5092 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
5097 if not Is_Type (U_Ent)
5098 and then Ekind (U_Ent) /= E_Variable
5099 and then Ekind (U_Ent) /= E_Constant
5101 Error_Msg_N ("alignment cannot be given for &", Nam);
5103 elsif Duplicate_Clause then
5106 elsif Align /= No_Uint then
5107 Set_Has_Alignment_Clause (U_Ent);
5109 -- Tagged type case, check for attempt to set alignment to a
5110 -- value greater than Max_Align, and reset if so. This error
5111 -- is suppressed in ASIS mode to allow for different ASIS
5112 -- back ends or ASIS-based tools to query the illegal clause.
5114 if Is_Tagged_Type (U_Ent)
5115 and then Align > Max_Align
5116 and then not ASIS_Mode
5119 ("alignment for & set to Maximum_Aligment??", Nam);
5120 Set_Alignment (U_Ent, Max_Align);
5125 Set_Alignment (U_Ent, Align);
5128 -- For an array type, U_Ent is the first subtype. In that case,
5129 -- also set the alignment of the anonymous base type so that
5130 -- other subtypes (such as the itypes for aggregates of the
5131 -- type) also receive the expected alignment.
5133 if Is_Array_Type (U_Ent) then
5134 Set_Alignment (Base_Type (U_Ent), Align);
5143 -- Bit_Order attribute definition clause
5145 when Attribute_Bit_Order => Bit_Order : declare
5147 if not Is_Record_Type (U_Ent) then
5149 ("Bit_Order can only be defined for record type", Nam);
5151 elsif Duplicate_Clause then
5155 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5157 if Etype (Expr) = Any_Type then
5160 elsif not Is_OK_Static_Expression (Expr) then
5161 Flag_Non_Static_Expr
5162 ("Bit_Order requires static expression!", Expr);
5165 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5166 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
5172 --------------------
5173 -- Component_Size --
5174 --------------------
5176 -- Component_Size attribute definition clause
5178 when Attribute_Component_Size => Component_Size_Case : declare
5179 Csize : constant Uint := Static_Integer (Expr);
5183 New_Ctyp : Entity_Id;
5187 if not Is_Array_Type (U_Ent) then
5188 Error_Msg_N ("component size requires array type", Nam);
5192 Btype := Base_Type (U_Ent);
5193 Ctyp := Component_Type (Btype);
5195 if Duplicate_Clause then
5198 elsif Rep_Item_Too_Early (Btype, N) then
5201 elsif Csize /= No_Uint then
5202 Check_Size (Expr, Ctyp, Csize, Biased);
5204 -- For the biased case, build a declaration for a subtype that
5205 -- will be used to represent the biased subtype that reflects
5206 -- the biased representation of components. We need the subtype
5207 -- to get proper conversions on referencing elements of the
5212 Make_Defining_Identifier (Loc,
5214 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
5217 Make_Subtype_Declaration (Loc,
5218 Defining_Identifier => New_Ctyp,
5219 Subtype_Indication =>
5220 New_Occurrence_Of (Component_Type (Btype), Loc));
5222 Set_Parent (Decl, N);
5223 Analyze (Decl, Suppress => All_Checks);
5225 Set_Has_Delayed_Freeze (New_Ctyp, False);
5226 Set_Esize (New_Ctyp, Csize);
5227 Set_RM_Size (New_Ctyp, Csize);
5228 Init_Alignment (New_Ctyp);
5229 Set_Is_Itype (New_Ctyp, True);
5230 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5232 Set_Component_Type (Btype, New_Ctyp);
5233 Set_Biased (New_Ctyp, N, "component size clause");
5236 Set_Component_Size (Btype, Csize);
5238 -- Deal with warning on overridden size
5240 if Warn_On_Overridden_Size
5241 and then Has_Size_Clause (Ctyp)
5242 and then RM_Size (Ctyp) /= Csize
5245 ("component size overrides size clause for&?S?", N, Ctyp);
5248 Set_Has_Component_Size_Clause (Btype, True);
5249 Set_Has_Non_Standard_Rep (Btype, True);
5251 end Component_Size_Case;
5253 -----------------------
5254 -- Constant_Indexing --
5255 -----------------------
5257 when Attribute_Constant_Indexing =>
5258 Check_Indexing_Functions;
5264 when Attribute_CPU => CPU :
5266 -- CPU attribute definition clause not allowed except from aspect
5269 if From_Aspect_Specification (N) then
5270 if not Is_Task_Type (U_Ent) then
5271 Error_Msg_N ("CPU can only be defined for task", Nam);
5273 elsif Duplicate_Clause then
5277 -- The expression must be analyzed in the special manner
5278 -- described in "Handling of Default and Per-Object
5279 -- Expressions" in sem.ads.
5281 -- The visibility to the discriminants must be restored
5283 Push_Scope_And_Install_Discriminants (U_Ent);
5284 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5285 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5287 if not Is_OK_Static_Expression (Expr) then
5288 Check_Restriction (Static_Priorities, Expr);
5294 ("attribute& cannot be set with definition clause", N);
5298 ----------------------
5299 -- Default_Iterator --
5300 ----------------------
5302 when Attribute_Default_Iterator => Default_Iterator : declare
5307 -- If target type is untagged, further checks are irrelevant
5309 if not Is_Tagged_Type (U_Ent) then
5311 ("aspect Default_Iterator applies to tagged type", Nam);
5315 Check_Iterator_Functions;
5319 if not Is_Entity_Name (Expr)
5320 or else Ekind (Entity (Expr)) /= E_Function
5322 Error_Msg_N ("aspect Iterator must be a function", Expr);
5325 Func := Entity (Expr);
5328 -- The type of the first parameter must be T, T'class, or a
5329 -- corresponding access type (5.5.1 (8/3). If function is
5330 -- parameterless label type accordingly.
5332 if No (First_Formal (Func)) then
5335 Typ := Etype (First_Formal (Func));
5339 or else Typ = Class_Wide_Type (U_Ent)
5340 or else (Is_Access_Type (Typ)
5341 and then Designated_Type (Typ) = U_Ent)
5342 or else (Is_Access_Type (Typ)
5343 and then Designated_Type (Typ) =
5344 Class_Wide_Type (U_Ent))
5350 ("Default Iterator must be a primitive of&", Func, U_Ent);
5352 end Default_Iterator;
5354 ------------------------
5355 -- Dispatching_Domain --
5356 ------------------------
5358 when Attribute_Dispatching_Domain => Dispatching_Domain :
5360 -- Dispatching_Domain attribute definition clause not allowed
5361 -- except from aspect specification.
5363 if From_Aspect_Specification (N) then
5364 if not Is_Task_Type (U_Ent) then
5366 ("Dispatching_Domain can only be defined for task", Nam);
5368 elsif Duplicate_Clause then
5372 -- The expression must be analyzed in the special manner
5373 -- described in "Handling of Default and Per-Object
5374 -- Expressions" in sem.ads.
5376 -- The visibility to the discriminants must be restored
5378 Push_Scope_And_Install_Discriminants (U_Ent);
5380 Preanalyze_Spec_Expression
5381 (Expr, RTE (RE_Dispatching_Domain));
5383 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5388 ("attribute& cannot be set with definition clause", N);
5390 end Dispatching_Domain;
5396 when Attribute_External_Tag => External_Tag :
5398 if not Is_Tagged_Type (U_Ent) then
5399 Error_Msg_N ("should be a tagged type", Nam);
5402 if Duplicate_Clause then
5406 Analyze_And_Resolve (Expr, Standard_String);
5408 if not Is_OK_Static_Expression (Expr) then
5409 Flag_Non_Static_Expr
5410 ("static string required for tag name!", Nam);
5413 if not Is_Library_Level_Entity (U_Ent) then
5415 ("??non-unique external tag supplied for &", N, U_Ent);
5417 ("\??same external tag applies to all subprogram calls",
5420 ("\??corresponding internal tag cannot be obtained", N);
5425 --------------------------
5426 -- Implicit_Dereference --
5427 --------------------------
5429 when Attribute_Implicit_Dereference =>
5431 -- Legality checks already performed at the point of the type
5432 -- declaration, aspect is not delayed.
5440 when Attribute_Input =>
5441 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5442 Set_Has_Specified_Stream_Input (Ent);
5444 ------------------------
5445 -- Interrupt_Priority --
5446 ------------------------
5448 when Attribute_Interrupt_Priority => Interrupt_Priority :
5450 -- Interrupt_Priority attribute definition clause not allowed
5451 -- except from aspect specification.
5453 if From_Aspect_Specification (N) then
5454 if not Is_Concurrent_Type (U_Ent) then
5456 ("Interrupt_Priority can only be defined for task and "
5457 & "protected object", Nam);
5459 elsif Duplicate_Clause then
5463 -- The expression must be analyzed in the special manner
5464 -- described in "Handling of Default and Per-Object
5465 -- Expressions" in sem.ads.
5467 -- The visibility to the discriminants must be restored
5469 Push_Scope_And_Install_Discriminants (U_Ent);
5471 Preanalyze_Spec_Expression
5472 (Expr, RTE (RE_Interrupt_Priority));
5474 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5476 -- Check the No_Task_At_Interrupt_Priority restriction
5478 if Is_Task_Type (U_Ent) then
5479 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5485 ("attribute& cannot be set with definition clause", N);
5487 end Interrupt_Priority;
5493 when Attribute_Iterable =>
5496 if Nkind (Expr) /= N_Aggregate then
5497 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5504 Assoc := First (Component_Associations (Expr));
5505 while Present (Assoc) loop
5506 if not Is_Entity_Name (Expression (Assoc)) then
5507 Error_Msg_N ("value must be a function", Assoc);
5514 ----------------------
5515 -- Iterator_Element --
5516 ----------------------
5518 when Attribute_Iterator_Element =>
5521 if not Is_Entity_Name (Expr)
5522 or else not Is_Type (Entity (Expr))
5524 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5531 -- Machine radix attribute definition clause
5533 when Attribute_Machine_Radix => Machine_Radix : declare
5534 Radix : constant Uint := Static_Integer (Expr);
5537 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5538 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5540 elsif Duplicate_Clause then
5543 elsif Radix /= No_Uint then
5544 Set_Has_Machine_Radix_Clause (U_Ent);
5545 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5550 elsif Radix = 10 then
5551 Set_Machine_Radix_10 (U_Ent);
5553 -- The following error is suppressed in ASIS mode to allow for
5554 -- different ASIS back ends or ASIS-based tools to query the
5557 elsif not ASIS_Mode then
5558 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5567 -- Object_Size attribute definition clause
5569 when Attribute_Object_Size => Object_Size : declare
5570 Size : constant Uint := Static_Integer (Expr);
5573 pragma Warnings (Off, Biased);
5576 if not Is_Type (U_Ent) then
5577 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5579 elsif Duplicate_Clause then
5583 Check_Size (Expr, U_Ent, Size, Biased);
5585 -- The following errors are suppressed in ASIS mode to allow
5586 -- for different ASIS back ends or ASIS-based tools to query
5587 -- the illegal clause.
5592 elsif Is_Scalar_Type (U_Ent) then
5593 if Size /= 8 and then Size /= 16 and then Size /= 32
5594 and then UI_Mod (Size, 64) /= 0
5597 ("Object_Size must be 8, 16, 32, or multiple of 64",
5601 elsif Size mod 8 /= 0 then
5602 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5605 Set_Esize (U_Ent, Size);
5606 Set_Has_Object_Size_Clause (U_Ent);
5607 Alignment_Check_For_Size_Change (U_Ent, Size);
5615 when Attribute_Output =>
5616 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5617 Set_Has_Specified_Stream_Output (Ent);
5623 when Attribute_Priority => Priority :
5625 -- Priority attribute definition clause not allowed except from
5626 -- aspect specification.
5628 if From_Aspect_Specification (N) then
5629 if not (Is_Concurrent_Type (U_Ent)
5630 or else Ekind (U_Ent) = E_Procedure)
5633 ("Priority can only be defined for task and protected "
5636 elsif Duplicate_Clause then
5640 -- The expression must be analyzed in the special manner
5641 -- described in "Handling of Default and Per-Object
5642 -- Expressions" in sem.ads.
5644 -- The visibility to the discriminants must be restored
5646 Push_Scope_And_Install_Discriminants (U_Ent);
5647 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5648 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5650 if not Is_OK_Static_Expression (Expr) then
5651 Check_Restriction (Static_Priorities, Expr);
5657 ("attribute& cannot be set with definition clause", N);
5665 when Attribute_Read =>
5666 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5667 Set_Has_Specified_Stream_Read (Ent);
5669 --------------------------
5670 -- Scalar_Storage_Order --
5671 --------------------------
5673 -- Scalar_Storage_Order attribute definition clause
5675 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5677 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5679 ("Scalar_Storage_Order can only be defined for record or "
5680 & "array type", Nam);
5682 elsif Duplicate_Clause then
5686 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5688 if Etype (Expr) = Any_Type then
5691 elsif not Is_OK_Static_Expression (Expr) then
5692 Flag_Non_Static_Expr
5693 ("Scalar_Storage_Order requires static expression!", Expr);
5695 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5697 -- Here for the case of a non-default (i.e. non-confirming)
5698 -- Scalar_Storage_Order attribute definition.
5700 if Support_Nondefault_SSO_On_Target then
5701 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5704 ("non-default Scalar_Storage_Order not supported on "
5709 -- Clear SSO default indications since explicit setting of the
5710 -- order overrides the defaults.
5712 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5713 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5715 end Scalar_Storage_Order;
5717 --------------------------
5718 -- Secondary_Stack_Size --
5719 --------------------------
5721 when Attribute_Secondary_Stack_Size => Secondary_Stack_Size :
5723 -- Secondary_Stack_Size attribute definition clause not allowed
5724 -- except from aspect specification.
5726 if From_Aspect_Specification (N) then
5727 if not Is_Task_Type (U_Ent) then
5729 ("Secondary Stack Size can only be defined for task", Nam);
5731 elsif Duplicate_Clause then
5735 Check_Restriction (No_Secondary_Stack, Expr);
5737 -- The expression must be analyzed in the special manner
5738 -- described in "Handling of Default and Per-Object
5739 -- Expressions" in sem.ads.
5741 -- The visibility to the discriminants must be restored
5743 Push_Scope_And_Install_Discriminants (U_Ent);
5744 Preanalyze_Spec_Expression (Expr, Any_Integer);
5745 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5747 if not Is_OK_Static_Expression (Expr) then
5748 Check_Restriction (Static_Storage_Size, Expr);
5754 ("attribute& cannot be set with definition clause", N);
5756 end Secondary_Stack_Size;
5762 -- Size attribute definition clause
5764 when Attribute_Size => Size : declare
5765 Size : constant Uint := Static_Integer (Expr);
5772 if Duplicate_Clause then
5775 elsif not Is_Type (U_Ent)
5776 and then Ekind (U_Ent) /= E_Variable
5777 and then Ekind (U_Ent) /= E_Constant
5779 Error_Msg_N ("size cannot be given for &", Nam);
5781 elsif Is_Array_Type (U_Ent)
5782 and then not Is_Constrained (U_Ent)
5785 ("size cannot be given for unconstrained array", Nam);
5787 elsif Size /= No_Uint then
5788 if Is_Type (U_Ent) then
5791 Etyp := Etype (U_Ent);
5794 -- Check size, note that Gigi is in charge of checking that the
5795 -- size of an array or record type is OK. Also we do not check
5796 -- the size in the ordinary fixed-point case, since it is too
5797 -- early to do so (there may be subsequent small clause that
5798 -- affects the size). We can check the size if a small clause
5799 -- has already been given.
5801 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5802 or else Has_Small_Clause (U_Ent)
5804 Check_Size (Expr, Etyp, Size, Biased);
5805 Set_Biased (U_Ent, N, "size clause", Biased);
5808 -- For types set RM_Size and Esize if possible
5810 if Is_Type (U_Ent) then
5811 Set_RM_Size (U_Ent, Size);
5813 -- For elementary types, increase Object_Size to power of 2,
5814 -- but not less than a storage unit in any case (normally
5815 -- this means it will be byte addressable).
5817 -- For all other types, nothing else to do, we leave Esize
5818 -- (object size) unset, the back end will set it from the
5819 -- size and alignment in an appropriate manner.
5821 -- In both cases, we check whether the alignment must be
5822 -- reset in the wake of the size change.
5824 if Is_Elementary_Type (U_Ent) then
5825 if Size <= System_Storage_Unit then
5826 Init_Esize (U_Ent, System_Storage_Unit);
5827 elsif Size <= 16 then
5828 Init_Esize (U_Ent, 16);
5829 elsif Size <= 32 then
5830 Init_Esize (U_Ent, 32);
5832 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5835 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5837 Alignment_Check_For_Size_Change (U_Ent, Size);
5840 -- For objects, set Esize only
5843 -- The following error is suppressed in ASIS mode to allow
5844 -- for different ASIS back ends or ASIS-based tools to query
5845 -- the illegal clause.
5847 if Is_Elementary_Type (Etyp)
5848 and then Size /= System_Storage_Unit
5849 and then Size /= System_Storage_Unit * 2
5850 and then Size /= System_Storage_Unit * 4
5851 and then Size /= System_Storage_Unit * 8
5852 and then not ASIS_Mode
5854 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5855 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5857 ("size for primitive object must be a power of 2 in "
5858 & "the range ^-^", N);
5861 Set_Esize (U_Ent, Size);
5864 Set_Has_Size_Clause (U_Ent);
5872 -- Small attribute definition clause
5874 when Attribute_Small => Small : declare
5875 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5879 Analyze_And_Resolve (Expr, Any_Real);
5881 if Etype (Expr) = Any_Type then
5884 elsif not Is_OK_Static_Expression (Expr) then
5885 Flag_Non_Static_Expr
5886 ("small requires static expression!", Expr);
5890 Small := Expr_Value_R (Expr);
5892 if Small <= Ureal_0 then
5893 Error_Msg_N ("small value must be greater than zero", Expr);
5899 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5901 ("small requires an ordinary fixed point type", Nam);
5903 elsif Has_Small_Clause (U_Ent) then
5904 Error_Msg_N ("small already given for &", Nam);
5906 elsif Small > Delta_Value (U_Ent) then
5908 ("small value must not be greater than delta value", Nam);
5911 Set_Small_Value (U_Ent, Small);
5912 Set_Small_Value (Implicit_Base, Small);
5913 Set_Has_Small_Clause (U_Ent);
5914 Set_Has_Small_Clause (Implicit_Base);
5915 Set_Has_Non_Standard_Rep (Implicit_Base);
5923 -- Storage_Pool attribute definition clause
5925 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
5930 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5932 ("storage pool cannot be given for access-to-subprogram type",
5937 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
5940 ("storage pool can only be given for access types", Nam);
5943 elsif Is_Derived_Type (U_Ent) then
5945 ("storage pool cannot be given for a derived access type",
5948 elsif Duplicate_Clause then
5951 elsif Present (Associated_Storage_Pool (U_Ent)) then
5952 Error_Msg_N ("storage pool already given for &", Nam);
5956 -- Check for Storage_Size previously given
5959 SS : constant Node_Id :=
5960 Get_Attribute_Definition_Clause
5961 (U_Ent, Attribute_Storage_Size);
5963 if Present (SS) then
5964 Check_Pool_Size_Clash (U_Ent, N, SS);
5968 -- Storage_Pool case
5970 if Id = Attribute_Storage_Pool then
5972 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5974 -- In the Simple_Storage_Pool case, we allow a variable of any
5975 -- simple storage pool type, so we Resolve without imposing an
5979 Analyze_And_Resolve (Expr);
5981 if not Present (Get_Rep_Pragma
5982 (Etype (Expr), Name_Simple_Storage_Pool_Type))
5985 ("expression must be of a simple storage pool type", Expr);
5989 if not Denotes_Variable (Expr) then
5990 Error_Msg_N ("storage pool must be a variable", Expr);
5994 if Nkind (Expr) = N_Type_Conversion then
5995 T := Etype (Expression (Expr));
6000 -- The Stack_Bounded_Pool is used internally for implementing
6001 -- access types with a Storage_Size. Since it only work properly
6002 -- when used on one specific type, we need to check that it is not
6003 -- hijacked improperly:
6005 -- type T is access Integer;
6006 -- for T'Storage_Size use n;
6007 -- type Q is access Float;
6008 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
6010 if RTE_Available (RE_Stack_Bounded_Pool)
6011 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
6013 Error_Msg_N ("non-shareable internal Pool", Expr);
6017 -- If the argument is a name that is not an entity name, then
6018 -- we construct a renaming operation to define an entity of
6019 -- type storage pool.
6021 if not Is_Entity_Name (Expr)
6022 and then Is_Object_Reference (Expr)
6024 Pool := Make_Temporary (Loc, 'P', Expr);
6027 Rnode : constant Node_Id :=
6028 Make_Object_Renaming_Declaration (Loc,
6029 Defining_Identifier => Pool,
6031 New_Occurrence_Of (Etype (Expr), Loc),
6035 -- If the attribute definition clause comes from an aspect
6036 -- clause, then insert the renaming before the associated
6037 -- entity's declaration, since the attribute clause has
6038 -- not yet been appended to the declaration list.
6040 if From_Aspect_Specification (N) then
6041 Insert_Before (Parent (Entity (N)), Rnode);
6043 Insert_Before (N, Rnode);
6047 Set_Associated_Storage_Pool (U_Ent, Pool);
6050 elsif Is_Entity_Name (Expr) then
6051 Pool := Entity (Expr);
6053 -- If pool is a renamed object, get original one. This can
6054 -- happen with an explicit renaming, and within instances.
6056 while Present (Renamed_Object (Pool))
6057 and then Is_Entity_Name (Renamed_Object (Pool))
6059 Pool := Entity (Renamed_Object (Pool));
6062 if Present (Renamed_Object (Pool))
6063 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
6064 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
6066 Pool := Entity (Expression (Renamed_Object (Pool)));
6069 Set_Associated_Storage_Pool (U_Ent, Pool);
6071 elsif Nkind (Expr) = N_Type_Conversion
6072 and then Is_Entity_Name (Expression (Expr))
6073 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
6075 Pool := Entity (Expression (Expr));
6076 Set_Associated_Storage_Pool (U_Ent, Pool);
6079 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
6088 -- Storage_Size attribute definition clause
6090 when Attribute_Storage_Size => Storage_Size : declare
6091 Btype : constant Entity_Id := Base_Type (U_Ent);
6094 if Is_Task_Type (U_Ent) then
6096 -- Check obsolescent (but never obsolescent if from aspect)
6098 if not From_Aspect_Specification (N) then
6099 Check_Restriction (No_Obsolescent_Features, N);
6101 if Warn_On_Obsolescent_Feature then
6103 ("?j?storage size clause for task is an obsolescent "
6104 & "feature (RM J.9)", N);
6105 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
6112 if not Is_Access_Type (U_Ent)
6113 and then Ekind (U_Ent) /= E_Task_Type
6115 Error_Msg_N ("storage size cannot be given for &", Nam);
6117 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6119 ("storage size cannot be given for a derived access type",
6122 elsif Duplicate_Clause then
6126 Analyze_And_Resolve (Expr, Any_Integer);
6128 if Is_Access_Type (U_Ent) then
6130 -- Check for Storage_Pool previously given
6133 SP : constant Node_Id :=
6134 Get_Attribute_Definition_Clause
6135 (U_Ent, Attribute_Storage_Pool);
6138 if Present (SP) then
6139 Check_Pool_Size_Clash (U_Ent, SP, N);
6143 -- Special case of for x'Storage_Size use 0
6145 if Is_OK_Static_Expression (Expr)
6146 and then Expr_Value (Expr) = 0
6148 Set_No_Pool_Assigned (Btype);
6152 Set_Has_Storage_Size_Clause (Btype);
6160 when Attribute_Stream_Size => Stream_Size : declare
6161 Size : constant Uint := Static_Integer (Expr);
6164 if Ada_Version <= Ada_95 then
6165 Check_Restriction (No_Implementation_Attributes, N);
6168 if Duplicate_Clause then
6171 elsif Is_Elementary_Type (U_Ent) then
6173 -- The following errors are suppressed in ASIS mode to allow
6174 -- for different ASIS back ends or ASIS-based tools to query
6175 -- the illegal clause.
6180 elsif Size /= System_Storage_Unit
6181 and then Size /= System_Storage_Unit * 2
6182 and then Size /= System_Storage_Unit * 4
6183 and then Size /= System_Storage_Unit * 8
6185 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6187 ("stream size for elementary type must be a power of 2 "
6188 & "and at least ^", N);
6190 elsif RM_Size (U_Ent) > Size then
6191 Error_Msg_Uint_1 := RM_Size (U_Ent);
6193 ("stream size for elementary type must be a power of 2 "
6194 & "and at least ^", N);
6197 Set_Has_Stream_Size_Clause (U_Ent);
6200 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6208 -- Value_Size attribute definition clause
6210 when Attribute_Value_Size => Value_Size : declare
6211 Size : constant Uint := Static_Integer (Expr);
6215 if not Is_Type (U_Ent) then
6216 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6218 elsif Duplicate_Clause then
6221 elsif Is_Array_Type (U_Ent)
6222 and then not Is_Constrained (U_Ent)
6225 ("Value_Size cannot be given for unconstrained array", Nam);
6228 if Is_Elementary_Type (U_Ent) then
6229 Check_Size (Expr, U_Ent, Size, Biased);
6230 Set_Biased (U_Ent, N, "value size clause", Biased);
6233 Set_RM_Size (U_Ent, Size);
6237 -----------------------
6238 -- Variable_Indexing --
6239 -----------------------
6241 when Attribute_Variable_Indexing =>
6242 Check_Indexing_Functions;
6248 when Attribute_Write =>
6249 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6250 Set_Has_Specified_Stream_Write (Ent);
6252 -- All other attributes cannot be set
6256 ("attribute& cannot be set with definition clause", N);
6259 -- The test for the type being frozen must be performed after any
6260 -- expression the clause has been analyzed since the expression itself
6261 -- might cause freezing that makes the clause illegal.
6263 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6266 end Analyze_Attribute_Definition_Clause;
6268 ----------------------------
6269 -- Analyze_Code_Statement --
6270 ----------------------------
6272 procedure Analyze_Code_Statement (N : Node_Id) is
6273 HSS : constant Node_Id := Parent (N);
6274 SBody : constant Node_Id := Parent (HSS);
6275 Subp : constant Entity_Id := Current_Scope;
6282 -- Accept foreign code statements for CodePeer. The analysis is skipped
6283 -- to avoid rejecting unrecognized constructs.
6285 if CodePeer_Mode then
6290 -- Analyze and check we get right type, note that this implements the
6291 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6292 -- the only way that Asm_Insn could possibly be visible.
6294 Analyze_And_Resolve (Expression (N));
6296 if Etype (Expression (N)) = Any_Type then
6298 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6299 Error_Msg_N ("incorrect type for code statement", N);
6303 Check_Code_Statement (N);
6305 -- Make sure we appear in the handled statement sequence of a subprogram
6308 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6309 or else Nkind (SBody) /= N_Subprogram_Body
6312 ("code statement can only appear in body of subprogram", N);
6316 -- Do remaining checks (RM 13.8(3)) if not already done
6318 if not Is_Machine_Code_Subprogram (Subp) then
6319 Set_Is_Machine_Code_Subprogram (Subp);
6321 -- No exception handlers allowed
6323 if Present (Exception_Handlers (HSS)) then
6325 ("exception handlers not permitted in machine code subprogram",
6326 First (Exception_Handlers (HSS)));
6329 -- No declarations other than use clauses and pragmas (we allow
6330 -- certain internally generated declarations as well).
6332 Decl := First (Declarations (SBody));
6333 while Present (Decl) loop
6334 DeclO := Original_Node (Decl);
6335 if Comes_From_Source (DeclO)
6336 and not Nkind_In (DeclO, N_Pragma,
6337 N_Use_Package_Clause,
6339 N_Implicit_Label_Declaration)
6342 ("this declaration not allowed in machine code subprogram",
6349 -- No statements other than code statements, pragmas, and labels.
6350 -- Again we allow certain internally generated statements.
6352 -- In Ada 2012, qualified expressions are names, and the code
6353 -- statement is initially parsed as a procedure call.
6355 Stmt := First (Statements (HSS));
6356 while Present (Stmt) loop
6357 StmtO := Original_Node (Stmt);
6359 -- A procedure call transformed into a code statement is OK
6361 if Ada_Version >= Ada_2012
6362 and then Nkind (StmtO) = N_Procedure_Call_Statement
6363 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6367 elsif Comes_From_Source (StmtO)
6368 and then not Nkind_In (StmtO, N_Pragma,
6373 ("this statement is not allowed in machine code subprogram",
6380 end Analyze_Code_Statement;
6382 -----------------------------------------------
6383 -- Analyze_Enumeration_Representation_Clause --
6384 -----------------------------------------------
6386 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6387 Ident : constant Node_Id := Identifier (N);
6388 Aggr : constant Node_Id := Array_Aggregate (N);
6389 Enumtype : Entity_Id;
6396 Err : Boolean := False;
6397 -- Set True to avoid cascade errors and crashes on incorrect source code
6399 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6400 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6401 -- Allowed range of universal integer (= allowed range of enum lit vals)
6405 -- Minimum and maximum values of entries
6408 -- Pointer to node for literal providing max value
6411 if Ignore_Rep_Clauses then
6412 Kill_Rep_Clause (N);
6416 -- Ignore enumeration rep clauses by default in CodePeer mode,
6417 -- unless -gnatd.I is specified, as a work around for potential false
6418 -- positive messages.
6420 if CodePeer_Mode and not Debug_Flag_Dot_II then
6424 -- First some basic error checks
6427 Enumtype := Entity (Ident);
6429 if Enumtype = Any_Type
6430 or else Rep_Item_Too_Early (Enumtype, N)
6434 Enumtype := Underlying_Type (Enumtype);
6437 if not Is_Enumeration_Type (Enumtype) then
6439 ("enumeration type required, found}",
6440 Ident, First_Subtype (Enumtype));
6444 -- Ignore rep clause on generic actual type. This will already have
6445 -- been flagged on the template as an error, and this is the safest
6446 -- way to ensure we don't get a junk cascaded message in the instance.
6448 if Is_Generic_Actual_Type (Enumtype) then
6451 -- Type must be in current scope
6453 elsif Scope (Enumtype) /= Current_Scope then
6454 Error_Msg_N ("type must be declared in this scope", Ident);
6457 -- Type must be a first subtype
6459 elsif not Is_First_Subtype (Enumtype) then
6460 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6463 -- Ignore duplicate rep clause
6465 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6466 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6469 -- Don't allow rep clause for standard [wide_[wide_]]character
6471 elsif Is_Standard_Character_Type (Enumtype) then
6472 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6475 -- Check that the expression is a proper aggregate (no parentheses)
6477 elsif Paren_Count (Aggr) /= 0 then
6479 ("extra parentheses surrounding aggregate not allowed",
6483 -- All tests passed, so set rep clause in place
6486 Set_Has_Enumeration_Rep_Clause (Enumtype);
6487 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6490 -- Now we process the aggregate. Note that we don't use the normal
6491 -- aggregate code for this purpose, because we don't want any of the
6492 -- normal expansion activities, and a number of special semantic
6493 -- rules apply (including the component type being any integer type)
6495 Elit := First_Literal (Enumtype);
6497 -- First the positional entries if any
6499 if Present (Expressions (Aggr)) then
6500 Expr := First (Expressions (Aggr));
6501 while Present (Expr) loop
6503 Error_Msg_N ("too many entries in aggregate", Expr);
6507 Val := Static_Integer (Expr);
6509 -- Err signals that we found some incorrect entries processing
6510 -- the list. The final checks for completeness and ordering are
6511 -- skipped in this case.
6513 if Val = No_Uint then
6516 elsif Val < Lo or else Hi < Val then
6517 Error_Msg_N ("value outside permitted range", Expr);
6521 Set_Enumeration_Rep (Elit, Val);
6522 Set_Enumeration_Rep_Expr (Elit, Expr);
6528 -- Now process the named entries if present
6530 if Present (Component_Associations (Aggr)) then
6531 Assoc := First (Component_Associations (Aggr));
6532 while Present (Assoc) loop
6533 Choice := First (Choices (Assoc));
6535 if Present (Next (Choice)) then
6537 ("multiple choice not allowed here", Next (Choice));
6541 if Nkind (Choice) = N_Others_Choice then
6542 Error_Msg_N ("others choice not allowed here", Choice);
6545 elsif Nkind (Choice) = N_Range then
6547 -- ??? should allow zero/one element range here
6549 Error_Msg_N ("range not allowed here", Choice);
6553 Analyze_And_Resolve (Choice, Enumtype);
6555 if Error_Posted (Choice) then
6560 if Is_Entity_Name (Choice)
6561 and then Is_Type (Entity (Choice))
6563 Error_Msg_N ("subtype name not allowed here", Choice);
6566 -- ??? should allow static subtype with zero/one entry
6568 elsif Etype (Choice) = Base_Type (Enumtype) then
6569 if not Is_OK_Static_Expression (Choice) then
6570 Flag_Non_Static_Expr
6571 ("non-static expression used for choice!", Choice);
6575 Elit := Expr_Value_E (Choice);
6577 if Present (Enumeration_Rep_Expr (Elit)) then
6579 Sloc (Enumeration_Rep_Expr (Elit));
6581 ("representation for& previously given#",
6586 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6588 Expr := Expression (Assoc);
6589 Val := Static_Integer (Expr);
6591 if Val = No_Uint then
6594 elsif Val < Lo or else Hi < Val then
6595 Error_Msg_N ("value outside permitted range", Expr);
6599 Set_Enumeration_Rep (Elit, Val);
6609 -- Aggregate is fully processed. Now we check that a full set of
6610 -- representations was given, and that they are in range and in order.
6611 -- These checks are only done if no other errors occurred.
6617 Elit := First_Literal (Enumtype);
6618 while Present (Elit) loop
6619 if No (Enumeration_Rep_Expr (Elit)) then
6620 Error_Msg_NE ("missing representation for&!", N, Elit);
6623 Val := Enumeration_Rep (Elit);
6625 if Min = No_Uint then
6629 if Val /= No_Uint then
6630 if Max /= No_Uint and then Val <= Max then
6632 ("enumeration value for& not ordered!",
6633 Enumeration_Rep_Expr (Elit), Elit);
6636 Max_Node := Enumeration_Rep_Expr (Elit);
6640 -- If there is at least one literal whose representation is not
6641 -- equal to the Pos value, then note that this enumeration type
6642 -- has a non-standard representation.
6644 if Val /= Enumeration_Pos (Elit) then
6645 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6652 -- Now set proper size information
6655 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6658 if Has_Size_Clause (Enumtype) then
6660 -- All OK, if size is OK now
6662 if RM_Size (Enumtype) >= Minsize then
6666 -- Try if we can get by with biasing
6669 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6671 -- Error message if even biasing does not work
6673 if RM_Size (Enumtype) < Minsize then
6674 Error_Msg_Uint_1 := RM_Size (Enumtype);
6675 Error_Msg_Uint_2 := Max;
6677 ("previously given size (^) is too small "
6678 & "for this value (^)", Max_Node);
6680 -- If biasing worked, indicate that we now have biased rep
6684 (Enumtype, Size_Clause (Enumtype), "size clause");
6689 Set_RM_Size (Enumtype, Minsize);
6690 Set_Enum_Esize (Enumtype);
6693 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6694 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6695 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6699 -- We repeat the too late test in case it froze itself
6701 if Rep_Item_Too_Late (Enumtype, N) then
6704 end Analyze_Enumeration_Representation_Clause;
6706 ----------------------------
6707 -- Analyze_Free_Statement --
6708 ----------------------------
6710 procedure Analyze_Free_Statement (N : Node_Id) is
6712 Analyze (Expression (N));
6713 end Analyze_Free_Statement;
6715 ---------------------------
6716 -- Analyze_Freeze_Entity --
6717 ---------------------------
6719 procedure Analyze_Freeze_Entity (N : Node_Id) is
6721 Freeze_Entity_Checks (N);
6722 end Analyze_Freeze_Entity;
6724 -----------------------------------
6725 -- Analyze_Freeze_Generic_Entity --
6726 -----------------------------------
6728 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6729 E : constant Entity_Id := Entity (N);
6732 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
6733 Analyze_Aspects_At_Freeze_Point (E);
6736 Freeze_Entity_Checks (N);
6737 end Analyze_Freeze_Generic_Entity;
6739 ------------------------------------------
6740 -- Analyze_Record_Representation_Clause --
6741 ------------------------------------------
6743 -- Note: we check as much as we can here, but we can't do any checks
6744 -- based on the position values (e.g. overlap checks) until freeze time
6745 -- because especially in Ada 2005 (machine scalar mode), the processing
6746 -- for non-standard bit order can substantially change the positions.
6747 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6748 -- for the remainder of this processing.
6750 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6751 Ident : constant Node_Id := Identifier (N);
6756 Hbit : Uint := Uint_0;
6760 Rectype : Entity_Id;
6763 function Is_Inherited (Comp : Entity_Id) return Boolean;
6764 -- True if Comp is an inherited component in a record extension
6770 function Is_Inherited (Comp : Entity_Id) return Boolean is
6771 Comp_Base : Entity_Id;
6774 if Ekind (Rectype) = E_Record_Subtype then
6775 Comp_Base := Original_Record_Component (Comp);
6780 return Comp_Base /= Original_Record_Component (Comp_Base);
6785 Is_Record_Extension : Boolean;
6786 -- True if Rectype is a record extension
6788 CR_Pragma : Node_Id := Empty;
6789 -- Points to N_Pragma node if Complete_Representation pragma present
6791 -- Start of processing for Analyze_Record_Representation_Clause
6794 if Ignore_Rep_Clauses then
6795 Kill_Rep_Clause (N);
6800 Rectype := Entity (Ident);
6802 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6805 Rectype := Underlying_Type (Rectype);
6808 -- First some basic error checks
6810 if not Is_Record_Type (Rectype) then
6812 ("record type required, found}", Ident, First_Subtype (Rectype));
6815 elsif Scope (Rectype) /= Current_Scope then
6816 Error_Msg_N ("type must be declared in this scope", N);
6819 elsif not Is_First_Subtype (Rectype) then
6820 Error_Msg_N ("cannot give record rep clause for subtype", N);
6823 elsif Has_Record_Rep_Clause (Rectype) then
6824 Error_Msg_N ("duplicate record rep clause ignored", N);
6827 elsif Rep_Item_Too_Late (Rectype, N) then
6831 -- We know we have a first subtype, now possibly go to the anonymous
6832 -- base type to determine whether Rectype is a record extension.
6834 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6835 Is_Record_Extension :=
6836 Nkind (Recdef) = N_Derived_Type_Definition
6837 and then Present (Record_Extension_Part (Recdef));
6839 if Present (Mod_Clause (N)) then
6841 Loc : constant Source_Ptr := Sloc (N);
6842 M : constant Node_Id := Mod_Clause (N);
6843 P : constant List_Id := Pragmas_Before (M);
6847 pragma Warnings (Off, Mod_Val);
6850 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6852 if Warn_On_Obsolescent_Feature then
6854 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6856 ("\?j?use alignment attribute definition clause instead", N);
6863 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6864 -- the Mod clause into an alignment clause anyway, so that the
6865 -- back end can compute and back-annotate properly the size and
6866 -- alignment of types that may include this record.
6868 -- This seems dubious, this destroys the source tree in a manner
6869 -- not detectable by ASIS ???
6871 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6873 Make_Attribute_Definition_Clause (Loc,
6874 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
6875 Chars => Name_Alignment,
6876 Expression => Relocate_Node (Expression (M)));
6878 Set_From_At_Mod (AtM_Nod);
6879 Insert_After (N, AtM_Nod);
6880 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6881 Set_Mod_Clause (N, Empty);
6884 -- Get the alignment value to perform error checking
6886 Mod_Val := Get_Alignment_Value (Expression (M));
6891 -- For untagged types, clear any existing component clauses for the
6892 -- type. If the type is derived, this is what allows us to override
6893 -- a rep clause for the parent. For type extensions, the representation
6894 -- of the inherited components is inherited, so we want to keep previous
6895 -- component clauses for completeness.
6897 if not Is_Tagged_Type (Rectype) then
6898 Comp := First_Component_Or_Discriminant (Rectype);
6899 while Present (Comp) loop
6900 Set_Component_Clause (Comp, Empty);
6901 Next_Component_Or_Discriminant (Comp);
6905 -- All done if no component clauses
6907 CC := First (Component_Clauses (N));
6913 -- A representation like this applies to the base type
6915 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6916 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6917 Set_Has_Specified_Layout (Base_Type (Rectype));
6919 -- Process the component clauses
6921 while Present (CC) loop
6925 if Nkind (CC) = N_Pragma then
6928 -- The only pragma of interest is Complete_Representation
6930 if Pragma_Name (CC) = Name_Complete_Representation then
6934 -- Processing for real component clause
6937 Posit := Static_Integer (Position (CC));
6938 Fbit := Static_Integer (First_Bit (CC));
6939 Lbit := Static_Integer (Last_Bit (CC));
6942 and then Fbit /= No_Uint
6943 and then Lbit /= No_Uint
6946 Error_Msg_N ("position cannot be negative", Position (CC));
6949 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
6951 -- The Last_Bit specified in a component clause must not be
6952 -- less than the First_Bit minus one (RM-13.5.1(10)).
6954 elsif Lbit < Fbit - 1 then
6956 ("last bit cannot be less than first bit minus one",
6959 -- Values look OK, so find the corresponding record component
6960 -- Even though the syntax allows an attribute reference for
6961 -- implementation-defined components, GNAT does not allow the
6962 -- tag to get an explicit position.
6964 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6965 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6966 Error_Msg_N ("position of tag cannot be specified", CC);
6968 Error_Msg_N ("illegal component name", CC);
6972 Comp := First_Entity (Rectype);
6973 while Present (Comp) loop
6974 exit when Chars (Comp) = Chars (Component_Name (CC));
6980 -- Maybe component of base type that is absent from
6981 -- statically constrained first subtype.
6983 Comp := First_Entity (Base_Type (Rectype));
6984 while Present (Comp) loop
6985 exit when Chars (Comp) = Chars (Component_Name (CC));
6992 ("component clause is for non-existent field", CC);
6994 -- Ada 2012 (AI05-0026): Any name that denotes a
6995 -- discriminant of an object of an unchecked union type
6996 -- shall not occur within a record_representation_clause.
6998 -- The general restriction of using record rep clauses on
6999 -- Unchecked_Union types has now been lifted. Since it is
7000 -- possible to introduce a record rep clause which mentions
7001 -- the discriminant of an Unchecked_Union in non-Ada 2012
7002 -- code, this check is applied to all versions of the
7005 elsif Ekind (Comp) = E_Discriminant
7006 and then Is_Unchecked_Union (Rectype)
7009 ("cannot reference discriminant of unchecked union",
7010 Component_Name (CC));
7012 elsif Is_Record_Extension and then Is_Inherited (Comp) then
7014 ("component clause not allowed for inherited "
7015 & "component&", CC, Comp);
7017 elsif Present (Component_Clause (Comp)) then
7019 -- Diagnose duplicate rep clause, or check consistency
7020 -- if this is an inherited component. In a double fault,
7021 -- there may be a duplicate inconsistent clause for an
7022 -- inherited component.
7024 if Scope (Original_Record_Component (Comp)) = Rectype
7025 or else Parent (Component_Clause (Comp)) = N
7027 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
7028 Error_Msg_N ("component clause previously given#", CC);
7032 Rep1 : constant Node_Id := Component_Clause (Comp);
7034 if Intval (Position (Rep1)) /=
7035 Intval (Position (CC))
7036 or else Intval (First_Bit (Rep1)) /=
7037 Intval (First_Bit (CC))
7038 or else Intval (Last_Bit (Rep1)) /=
7039 Intval (Last_Bit (CC))
7042 ("component clause inconsistent with "
7043 & "representation of ancestor", CC);
7045 elsif Warn_On_Redundant_Constructs then
7047 ("?r?redundant confirming component clause "
7048 & "for component!", CC);
7053 -- Normal case where this is the first component clause we
7054 -- have seen for this entity, so set it up properly.
7057 -- Make reference for field in record rep clause and set
7058 -- appropriate entity field in the field identifier.
7061 (Comp, Component_Name (CC), Set_Ref => False);
7062 Set_Entity (Component_Name (CC), Comp);
7064 -- Update Fbit and Lbit to the actual bit number
7066 Fbit := Fbit + UI_From_Int (SSU) * Posit;
7067 Lbit := Lbit + UI_From_Int (SSU) * Posit;
7069 if Has_Size_Clause (Rectype)
7070 and then RM_Size (Rectype) <= Lbit
7073 ("bit number out of range of specified size",
7076 Set_Component_Clause (Comp, CC);
7077 Set_Component_Bit_Offset (Comp, Fbit);
7078 Set_Esize (Comp, 1 + (Lbit - Fbit));
7079 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
7080 Set_Normalized_Position (Comp, Fbit / SSU);
7082 if Warn_On_Overridden_Size
7083 and then Has_Size_Clause (Etype (Comp))
7084 and then RM_Size (Etype (Comp)) /= Esize (Comp)
7087 ("?S?component size overrides size clause for&",
7088 Component_Name (CC), Etype (Comp));
7091 -- This information is also set in the corresponding
7092 -- component of the base type, found by accessing the
7093 -- Original_Record_Component link if it is present.
7095 Ocomp := Original_Record_Component (Comp);
7102 (Component_Name (CC),
7108 (Comp, First_Node (CC), "component clause", Biased);
7110 if Present (Ocomp) then
7111 Set_Component_Clause (Ocomp, CC);
7112 Set_Component_Bit_Offset (Ocomp, Fbit);
7113 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
7114 Set_Normalized_Position (Ocomp, Fbit / SSU);
7115 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7117 Set_Normalized_Position_Max
7118 (Ocomp, Normalized_Position (Ocomp));
7120 -- Note: we don't use Set_Biased here, because we
7121 -- already gave a warning above if needed, and we
7122 -- would get a duplicate for the same name here.
7124 Set_Has_Biased_Representation
7125 (Ocomp, Has_Biased_Representation (Comp));
7128 if Esize (Comp) < 0 then
7129 Error_Msg_N ("component size is negative", CC);
7140 -- Check missing components if Complete_Representation pragma appeared
7142 if Present (CR_Pragma) then
7143 Comp := First_Component_Or_Discriminant (Rectype);
7144 while Present (Comp) loop
7145 if No (Component_Clause (Comp)) then
7147 ("missing component clause for &", CR_Pragma, Comp);
7150 Next_Component_Or_Discriminant (Comp);
7153 -- Give missing components warning if required
7155 elsif Warn_On_Unrepped_Components then
7157 Num_Repped_Components : Nat := 0;
7158 Num_Unrepped_Components : Nat := 0;
7161 -- First count number of repped and unrepped components
7163 Comp := First_Component_Or_Discriminant (Rectype);
7164 while Present (Comp) loop
7165 if Present (Component_Clause (Comp)) then
7166 Num_Repped_Components := Num_Repped_Components + 1;
7168 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7171 Next_Component_Or_Discriminant (Comp);
7174 -- We are only interested in the case where there is at least one
7175 -- unrepped component, and at least half the components have rep
7176 -- clauses. We figure that if less than half have them, then the
7177 -- partial rep clause is really intentional. If the component
7178 -- type has no underlying type set at this point (as for a generic
7179 -- formal type), we don't know enough to give a warning on the
7182 if Num_Unrepped_Components > 0
7183 and then Num_Unrepped_Components < Num_Repped_Components
7185 Comp := First_Component_Or_Discriminant (Rectype);
7186 while Present (Comp) loop
7187 if No (Component_Clause (Comp))
7188 and then Comes_From_Source (Comp)
7189 and then Present (Underlying_Type (Etype (Comp)))
7190 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
7191 or else Size_Known_At_Compile_Time
7192 (Underlying_Type (Etype (Comp))))
7193 and then not Has_Warnings_Off (Rectype)
7195 -- Ignore discriminant in unchecked union, since it is
7196 -- not there, and cannot have a component clause.
7198 and then (not Is_Unchecked_Union (Rectype)
7199 or else Ekind (Comp) /= E_Discriminant)
7201 Error_Msg_Sloc := Sloc (Comp);
7203 ("?C?no component clause given for & declared #",
7207 Next_Component_Or_Discriminant (Comp);
7212 end Analyze_Record_Representation_Clause;
7214 -------------------------------------
7215 -- Build_Discrete_Static_Predicate --
7216 -------------------------------------
7218 procedure Build_Discrete_Static_Predicate
7223 Loc : constant Source_Ptr := Sloc (Expr);
7225 Non_Static : exception;
7226 -- Raised if something non-static is found
7228 Btyp : constant Entity_Id := Base_Type (Typ);
7230 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7231 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7232 -- Low bound and high bound value of base type of Typ
7236 -- Bounds for constructing the static predicate. We use the bound of the
7237 -- subtype if it is static, otherwise the corresponding base type bound.
7238 -- Note: a non-static subtype can have a static predicate.
7243 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7244 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7247 type RList is array (Nat range <>) of REnt;
7248 -- A list of ranges. The ranges are sorted in increasing order, and are
7249 -- disjoint (there is a gap of at least one value between each range in
7250 -- the table). A value is in the set of ranges in Rlist if it lies
7251 -- within one of these ranges.
7253 False_Range : constant RList :=
7254 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7255 -- An empty set of ranges represents a range list that can never be
7256 -- satisfied, since there are no ranges in which the value could lie,
7257 -- so it does not lie in any of them. False_Range is a canonical value
7258 -- for this empty set, but general processing should test for an Rlist
7259 -- with length zero (see Is_False predicate), since other null ranges
7260 -- may appear which must be treated as False.
7262 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7263 -- Range representing True, value must be in the base range
7265 function "and" (Left : RList; Right : RList) return RList;
7266 -- And's together two range lists, returning a range list. This is a set
7267 -- intersection operation.
7269 function "or" (Left : RList; Right : RList) return RList;
7270 -- Or's together two range lists, returning a range list. This is a set
7273 function "not" (Right : RList) return RList;
7274 -- Returns complement of a given range list, i.e. a range list
7275 -- representing all the values in TLo .. THi that are not in the input
7278 function Build_Val (V : Uint) return Node_Id;
7279 -- Return an analyzed N_Identifier node referencing this value, suitable
7280 -- for use as an entry in the Static_Discrte_Predicate list. This node
7281 -- is typed with the base type.
7283 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7284 -- Return an analyzed N_Range node referencing this range, suitable for
7285 -- use as an entry in the Static_Discrete_Predicate list. This node is
7286 -- typed with the base type.
7288 function Get_RList (Exp : Node_Id) return RList;
7289 -- This is a recursive routine that converts the given expression into a
7290 -- list of ranges, suitable for use in building the static predicate.
7292 function Is_False (R : RList) return Boolean;
7293 pragma Inline (Is_False);
7294 -- Returns True if the given range list is empty, and thus represents a
7295 -- False list of ranges that can never be satisfied.
7297 function Is_True (R : RList) return Boolean;
7298 -- Returns True if R trivially represents the True predicate by having a
7299 -- single range from BLo to BHi.
7301 function Is_Type_Ref (N : Node_Id) return Boolean;
7302 pragma Inline (Is_Type_Ref);
7303 -- Returns if True if N is a reference to the type for the predicate in
7304 -- the expression (i.e. if it is an identifier whose Chars field matches
7305 -- the Nam given in the call). N must not be parenthesized, if the type
7306 -- name appears in parens, this routine will return False.
7308 function Lo_Val (N : Node_Id) return Uint;
7309 -- Given an entry from a Static_Discrete_Predicate list that is either
7310 -- a static expression or static range, gets either the expression value
7311 -- or the low bound of the range.
7313 function Hi_Val (N : Node_Id) return Uint;
7314 -- Given an entry from a Static_Discrete_Predicate list that is either
7315 -- a static expression or static range, gets either the expression value
7316 -- or the high bound of the range.
7318 function Membership_Entry (N : Node_Id) return RList;
7319 -- Given a single membership entry (range, value, or subtype), returns
7320 -- the corresponding range list. Raises Static_Error if not static.
7322 function Membership_Entries (N : Node_Id) return RList;
7323 -- Given an element on an alternatives list of a membership operation,
7324 -- returns the range list corresponding to this entry and all following
7325 -- entries (i.e. returns the "or" of this list of values).
7327 function Stat_Pred (Typ : Entity_Id) return RList;
7328 -- Given a type, if it has a static predicate, then return the predicate
7329 -- as a range list, otherwise raise Non_Static.
7335 function "and" (Left : RList; Right : RList) return RList is
7337 -- First range of result
7339 SLeft : Nat := Left'First;
7340 -- Start of rest of left entries
7342 SRight : Nat := Right'First;
7343 -- Start of rest of right entries
7346 -- If either range is True, return the other
7348 if Is_True (Left) then
7350 elsif Is_True (Right) then
7354 -- If either range is False, return False
7356 if Is_False (Left) or else Is_False (Right) then
7360 -- Loop to remove entries at start that are disjoint, and thus just
7361 -- get discarded from the result entirely.
7364 -- If no operands left in either operand, result is false
7366 if SLeft > Left'Last or else SRight > Right'Last then
7369 -- Discard first left operand entry if disjoint with right
7371 elsif Left (SLeft).Hi < Right (SRight).Lo then
7374 -- Discard first right operand entry if disjoint with left
7376 elsif Right (SRight).Hi < Left (SLeft).Lo then
7377 SRight := SRight + 1;
7379 -- Otherwise we have an overlapping entry
7386 -- Now we have two non-null operands, and first entries overlap. The
7387 -- first entry in the result will be the overlapping part of these
7390 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7391 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7393 -- Now we can remove the entry that ended at a lower value, since its
7394 -- contribution is entirely contained in Fent.
7396 if Left (SLeft).Hi <= Right (SRight).Hi then
7399 SRight := SRight + 1;
7402 -- Compute result by concatenating this first entry with the "and" of
7403 -- the remaining parts of the left and right operands. Note that if
7404 -- either of these is empty, "and" will yield empty, so that we will
7405 -- end up with just Fent, which is what we want in that case.
7408 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7415 function "not" (Right : RList) return RList is
7417 -- Return True if False range
7419 if Is_False (Right) then
7423 -- Return False if True range
7425 if Is_True (Right) then
7429 -- Here if not trivial case
7432 Result : RList (1 .. Right'Length + 1);
7433 -- May need one more entry for gap at beginning and end
7436 -- Number of entries stored in Result
7441 if Right (Right'First).Lo > TLo then
7443 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7446 -- Gaps between ranges
7448 for J in Right'First .. Right'Last - 1 loop
7450 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7455 if Right (Right'Last).Hi < THi then
7457 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7460 return Result (1 .. Count);
7468 function "or" (Left : RList; Right : RList) return RList is
7470 -- First range of result
7472 SLeft : Nat := Left'First;
7473 -- Start of rest of left entries
7475 SRight : Nat := Right'First;
7476 -- Start of rest of right entries
7479 -- If either range is True, return True
7481 if Is_True (Left) or else Is_True (Right) then
7485 -- If either range is False (empty), return the other
7487 if Is_False (Left) then
7489 elsif Is_False (Right) then
7493 -- Initialize result first entry from left or right operand depending
7494 -- on which starts with the lower range.
7496 if Left (SLeft).Lo < Right (SRight).Lo then
7497 FEnt := Left (SLeft);
7500 FEnt := Right (SRight);
7501 SRight := SRight + 1;
7504 -- This loop eats ranges from left and right operands that are
7505 -- contiguous with the first range we are gathering.
7508 -- Eat first entry in left operand if contiguous or overlapped by
7509 -- gathered first operand of result.
7511 if SLeft <= Left'Last
7512 and then Left (SLeft).Lo <= FEnt.Hi + 1
7514 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7517 -- Eat first entry in right operand if contiguous or overlapped by
7518 -- gathered right operand of result.
7520 elsif SRight <= Right'Last
7521 and then Right (SRight).Lo <= FEnt.Hi + 1
7523 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7524 SRight := SRight + 1;
7526 -- All done if no more entries to eat
7533 -- Obtain result as the first entry we just computed, concatenated
7534 -- to the "or" of the remaining results (if one operand is empty,
7535 -- this will just concatenate with the other
7538 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7545 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7550 Low_Bound => Build_Val (Lo),
7551 High_Bound => Build_Val (Hi));
7552 Set_Etype (Result, Btyp);
7553 Set_Analyzed (Result);
7561 function Build_Val (V : Uint) return Node_Id is
7565 if Is_Enumeration_Type (Typ) then
7566 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7568 Result := Make_Integer_Literal (Loc, V);
7571 Set_Etype (Result, Btyp);
7572 Set_Is_Static_Expression (Result);
7573 Set_Analyzed (Result);
7581 function Get_RList (Exp : Node_Id) return RList is
7586 -- Static expression can only be true or false
7588 if Is_OK_Static_Expression (Exp) then
7589 if Expr_Value (Exp) = 0 then
7596 -- Otherwise test node type
7604 when N_Op_And | N_And_Then =>
7605 return Get_RList (Left_Opnd (Exp))
7607 Get_RList (Right_Opnd (Exp));
7611 when N_Op_Or | N_Or_Else =>
7612 return Get_RList (Left_Opnd (Exp))
7614 Get_RList (Right_Opnd (Exp));
7619 return not Get_RList (Right_Opnd (Exp));
7621 -- Comparisons of type with static value
7623 when N_Op_Compare =>
7625 -- Type is left operand
7627 if Is_Type_Ref (Left_Opnd (Exp))
7628 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7630 Val := Expr_Value (Right_Opnd (Exp));
7632 -- Typ is right operand
7634 elsif Is_Type_Ref (Right_Opnd (Exp))
7635 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7637 Val := Expr_Value (Left_Opnd (Exp));
7639 -- Invert sense of comparison
7642 when N_Op_Gt => Op := N_Op_Lt;
7643 when N_Op_Lt => Op := N_Op_Gt;
7644 when N_Op_Ge => Op := N_Op_Le;
7645 when N_Op_Le => Op := N_Op_Ge;
7646 when others => null;
7649 -- Other cases are non-static
7655 -- Construct range according to comparison operation
7659 return RList'(1 => REnt'(Val, Val));
7662 return RList'(1 => REnt'(Val, BHi));
7665 return RList'(1 => REnt'(Val + 1, BHi));
7668 return RList'(1 => REnt'(BLo, Val));
7671 return RList'(1 => REnt'(BLo, Val - 1));
7674 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7677 raise Program_Error;
7683 if not Is_Type_Ref (Left_Opnd (Exp)) then
7687 if Present (Right_Opnd (Exp)) then
7688 return Membership_Entry (Right_Opnd (Exp));
7690 return Membership_Entries (First (Alternatives (Exp)));
7693 -- Negative membership (NOT IN)
7696 if not Is_Type_Ref (Left_Opnd (Exp)) then
7700 if Present (Right_Opnd (Exp)) then
7701 return not Membership_Entry (Right_Opnd (Exp));
7703 return not Membership_Entries (First (Alternatives (Exp)));
7706 -- Function call, may be call to static predicate
7708 when N_Function_Call =>
7709 if Is_Entity_Name (Name (Exp)) then
7711 Ent : constant Entity_Id := Entity (Name (Exp));
7713 if Is_Predicate_Function (Ent)
7715 Is_Predicate_Function_M (Ent)
7717 return Stat_Pred (Etype (First_Formal (Ent)));
7722 -- Other function call cases are non-static
7726 -- Qualified expression, dig out the expression
7728 when N_Qualified_Expression =>
7729 return Get_RList (Expression (Exp));
7731 when N_Case_Expression =>
7738 if not Is_Entity_Name (Expression (Expr))
7739 or else Etype (Expression (Expr)) /= Typ
7742 ("expression must denaote subtype", Expression (Expr));
7746 -- Collect discrete choices in all True alternatives
7748 Choices := New_List;
7749 Alt := First (Alternatives (Exp));
7750 while Present (Alt) loop
7751 Dep := Expression (Alt);
7753 if not Is_OK_Static_Expression (Dep) then
7756 elsif Is_True (Expr_Value (Dep)) then
7757 Append_List_To (Choices,
7758 New_Copy_List (Discrete_Choices (Alt)));
7764 return Membership_Entries (First (Choices));
7767 -- Expression with actions: if no actions, dig out expression
7769 when N_Expression_With_Actions =>
7770 if Is_Empty_List (Actions (Exp)) then
7771 return Get_RList (Expression (Exp));
7779 return (Get_RList (Left_Opnd (Exp))
7780 and not Get_RList (Right_Opnd (Exp)))
7781 or (Get_RList (Right_Opnd (Exp))
7782 and not Get_RList (Left_Opnd (Exp)));
7784 -- Any other node type is non-static
7795 function Hi_Val (N : Node_Id) return Uint is
7797 if Is_OK_Static_Expression (N) then
7798 return Expr_Value (N);
7800 pragma Assert (Nkind (N) = N_Range);
7801 return Expr_Value (High_Bound (N));
7809 function Is_False (R : RList) return Boolean is
7811 return R'Length = 0;
7818 function Is_True (R : RList) return Boolean is
7821 and then R (R'First).Lo = BLo
7822 and then R (R'First).Hi = BHi;
7829 function Is_Type_Ref (N : Node_Id) return Boolean is
7831 return Nkind (N) = N_Identifier
7832 and then Chars (N) = Nam
7833 and then Paren_Count (N) = 0;
7840 function Lo_Val (N : Node_Id) return Uint is
7842 if Is_OK_Static_Expression (N) then
7843 return Expr_Value (N);
7845 pragma Assert (Nkind (N) = N_Range);
7846 return Expr_Value (Low_Bound (N));
7850 ------------------------
7851 -- Membership_Entries --
7852 ------------------------
7854 function Membership_Entries (N : Node_Id) return RList is
7856 if No (Next (N)) then
7857 return Membership_Entry (N);
7859 return Membership_Entry (N) or Membership_Entries (Next (N));
7861 end Membership_Entries;
7863 ----------------------
7864 -- Membership_Entry --
7865 ----------------------
7867 function Membership_Entry (N : Node_Id) return RList is
7875 if Nkind (N) = N_Range then
7876 if not Is_OK_Static_Expression (Low_Bound (N))
7878 not Is_OK_Static_Expression (High_Bound (N))
7882 SLo := Expr_Value (Low_Bound (N));
7883 SHi := Expr_Value (High_Bound (N));
7884 return RList'(1 => REnt'(SLo, SHi));
7887 -- Static expression case
7889 elsif Is_OK_Static_Expression (N) then
7890 Val := Expr_Value (N);
7891 return RList'(1 => REnt'(Val, Val));
7893 -- Identifier (other than static expression) case
7895 else pragma Assert (Nkind (N) = N_Identifier);
7899 if Is_Type (Entity (N)) then
7901 -- If type has predicates, process them
7903 if Has_Predicates (Entity (N)) then
7904 return Stat_Pred (Entity (N));
7906 -- For static subtype without predicates, get range
7908 elsif Is_OK_Static_Subtype (Entity (N)) then
7909 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7910 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7911 return RList'(1 => REnt'(SLo, SHi));
7913 -- Any other type makes us non-static
7919 -- Any other kind of identifier in predicate (e.g. a non-static
7920 -- expression value) means this is not a static predicate.
7926 end Membership_Entry;
7932 function Stat_Pred (Typ : Entity_Id) return RList is
7934 -- Not static if type does not have static predicates
7936 if not Has_Static_Predicate (Typ) then
7940 -- Otherwise we convert the predicate list to a range list
7943 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7944 Result : RList (1 .. List_Length (Spred));
7948 P := First (Static_Discrete_Predicate (Typ));
7949 for J in Result'Range loop
7950 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7958 -- Start of processing for Build_Discrete_Static_Predicate
7961 -- Establish bounds for the predicate
7963 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7964 TLo := Expr_Value (Type_Low_Bound (Typ));
7969 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7970 THi := Expr_Value (Type_High_Bound (Typ));
7975 -- Analyze the expression to see if it is a static predicate
7978 Ranges : constant RList := Get_RList (Expr);
7979 -- Range list from expression if it is static
7984 -- Convert range list into a form for the static predicate. In the
7985 -- Ranges array, we just have raw ranges, these must be converted
7986 -- to properly typed and analyzed static expressions or range nodes.
7988 -- Note: here we limit ranges to the ranges of the subtype, so that
7989 -- a predicate is always false for values outside the subtype. That
7990 -- seems fine, such values are invalid anyway, and considering them
7991 -- to fail the predicate seems allowed and friendly, and furthermore
7992 -- simplifies processing for case statements and loops.
7996 for J in Ranges'Range loop
7998 Lo : Uint := Ranges (J).Lo;
7999 Hi : Uint := Ranges (J).Hi;
8002 -- Ignore completely out of range entry
8004 if Hi < TLo or else Lo > THi then
8007 -- Otherwise process entry
8010 -- Adjust out of range value to subtype range
8020 -- Convert range into required form
8022 Append_To (Plist, Build_Range (Lo, Hi));
8027 -- Processing was successful and all entries were static, so now we
8028 -- can store the result as the predicate list.
8030 Set_Static_Discrete_Predicate (Typ, Plist);
8032 -- The processing for static predicates put the expression into
8033 -- canonical form as a series of ranges. It also eliminated
8034 -- duplicates and collapsed and combined ranges. We might as well
8035 -- replace the alternatives list of the right operand of the
8036 -- membership test with the static predicate list, which will
8037 -- usually be more efficient.
8040 New_Alts : constant List_Id := New_List;
8045 Old_Node := First (Plist);
8046 while Present (Old_Node) loop
8047 New_Node := New_Copy (Old_Node);
8049 if Nkind (New_Node) = N_Range then
8050 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
8051 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
8054 Append_To (New_Alts, New_Node);
8058 -- If empty list, replace by False
8060 if Is_Empty_List (New_Alts) then
8061 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
8063 -- Else replace by set membership test
8068 Left_Opnd => Make_Identifier (Loc, Nam),
8069 Right_Opnd => Empty,
8070 Alternatives => New_Alts));
8072 -- Resolve new expression in function context
8074 Install_Formals (Predicate_Function (Typ));
8075 Push_Scope (Predicate_Function (Typ));
8076 Analyze_And_Resolve (Expr, Standard_Boolean);
8082 -- If non-static, return doing nothing
8087 end Build_Discrete_Static_Predicate;
8089 --------------------------------
8090 -- Build_Export_Import_Pragma --
8091 --------------------------------
8093 function Build_Export_Import_Pragma
8095 Id : Entity_Id) return Node_Id
8097 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
8098 Expr : constant Node_Id := Expression (Asp);
8099 Loc : constant Source_Ptr := Sloc (Asp);
8110 Create_Pragma : Boolean := False;
8111 -- This flag is set when the aspect form is such that it warrants the
8112 -- creation of a corresponding pragma.
8115 if Present (Expr) then
8116 if Error_Posted (Expr) then
8119 elsif Is_True (Expr_Value (Expr)) then
8120 Create_Pragma := True;
8123 -- Otherwise the aspect defaults to True
8126 Create_Pragma := True;
8129 -- Nothing to do when the expression is False or is erroneous
8131 if not Create_Pragma then
8135 -- Obtain all interfacing aspects that apply to the related entity
8137 Get_Interfacing_Aspects
8141 Expo_Asp => Dummy_1,
8147 -- Handle the convention argument
8149 if Present (Conv) then
8150 Conv_Arg := New_Copy_Tree (Expression (Conv));
8152 -- Assume convention "Ada' when aspect Convention is missing
8155 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8159 Make_Pragma_Argument_Association (Loc,
8160 Chars => Name_Convention,
8161 Expression => Conv_Arg));
8163 -- Handle the entity argument
8166 Make_Pragma_Argument_Association (Loc,
8167 Chars => Name_Entity,
8168 Expression => New_Occurrence_Of (Id, Loc)));
8170 -- Handle the External_Name argument
8172 if Present (EN) then
8174 Make_Pragma_Argument_Association (Loc,
8175 Chars => Name_External_Name,
8176 Expression => New_Copy_Tree (Expression (EN))));
8179 -- Handle the Link_Name argument
8181 if Present (LN) then
8183 Make_Pragma_Argument_Association (Loc,
8184 Chars => Name_Link_Name,
8185 Expression => New_Copy_Tree (Expression (LN))));
8189 -- pragma Export/Import
8190 -- (Convention => <Conv>/Ada,
8192 -- [External_Name => <EN>,]
8193 -- [Link_Name => <LN>]);
8197 Pragma_Identifier =>
8198 Make_Identifier (Loc, Chars (Identifier (Asp))),
8199 Pragma_Argument_Associations => Args);
8201 -- Decorate the relevant aspect and the pragma
8203 Set_Aspect_Rep_Item (Asp, Prag);
8205 Set_Corresponding_Aspect (Prag, Asp);
8206 Set_From_Aspect_Specification (Prag);
8207 Set_Parent (Prag, Asp);
8209 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8210 Set_Import_Pragma (Id, Prag);
8214 end Build_Export_Import_Pragma;
8216 -------------------------------
8217 -- Build_Predicate_Functions --
8218 -------------------------------
8220 -- The procedures that are constructed here have the form:
8222 -- function typPredicate (Ixxx : typ) return Boolean is
8225 -- typ1Predicate (typ1 (Ixxx))
8226 -- and then typ2Predicate (typ2 (Ixxx))
8228 -- exp1 and then exp2 and then ...
8229 -- end typPredicate;
8231 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8232 -- this is the point at which these expressions get analyzed, providing the
8233 -- required delay, and typ1, typ2, are entities from which predicates are
8234 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8235 -- use this function even if checks are off, e.g. for membership tests.
8237 -- Note that the inherited predicates are evaluated first, as required by
8240 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8241 -- the form of this return expression.
8243 -- If the expression has at least one Raise_Expression, then we also build
8244 -- the typPredicateM version of the function, in which any occurrence of a
8245 -- Raise_Expression is converted to "return False".
8247 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8248 Loc : constant Source_Ptr := Sloc (Typ);
8251 -- This is the expression for the result of the function. It is
8252 -- is build by connecting the component predicates with AND THEN.
8255 -- This is the corresponding return expression for the Predicate_M
8256 -- function. It differs in that raise expressions are marked for
8257 -- special expansion (see Process_REs).
8259 Object_Name : Name_Id;
8260 -- Name for argument of Predicate procedure. Note that we use the same
8261 -- name for both predicate functions. That way the reference within the
8262 -- predicate expression is the same in both functions.
8264 Object_Entity : Entity_Id;
8265 -- Entity for argument of Predicate procedure
8267 Object_Entity_M : Entity_Id;
8268 -- Entity for argument of separate Predicate procedure when exceptions
8269 -- are present in expression.
8272 -- The function declaration
8277 Raise_Expression_Present : Boolean := False;
8278 -- Set True if Expr has at least one Raise_Expression
8280 procedure Add_Condition (Cond : Node_Id);
8281 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8284 procedure Add_Predicates;
8285 -- Appends expressions for any Predicate pragmas in the rep item chain
8286 -- Typ to Expr. Note that we look only at items for this exact entity.
8287 -- Inheritance of predicates for the parent type is done by calling the
8288 -- Predicate_Function of the parent type, using Add_Call above.
8290 procedure Add_Call (T : Entity_Id);
8291 -- Includes a call to the predicate function for type T in Expr if T
8292 -- has predicates and Predicate_Function (T) is non-empty.
8294 function Process_RE (N : Node_Id) return Traverse_Result;
8295 -- Used in Process REs, tests if node N is a raise expression, and if
8296 -- so, marks it to be converted to return False.
8298 procedure Process_REs is new Traverse_Proc (Process_RE);
8299 -- Marks any raise expressions in Expr_M to return False
8301 function Test_RE (N : Node_Id) return Traverse_Result;
8302 -- Used in Test_REs, tests one node for being a raise expression, and if
8303 -- so sets Raise_Expression_Present True.
8305 procedure Test_REs is new Traverse_Proc (Test_RE);
8306 -- Tests to see if Expr contains any raise expressions
8312 procedure Add_Call (T : Entity_Id) is
8316 if Present (T) and then Present (Predicate_Function (T)) then
8317 Set_Has_Predicates (Typ);
8319 -- Build the call to the predicate function of T
8323 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
8325 -- "and"-in the call to evolving expression
8327 Add_Condition (Exp);
8329 -- Output info message on inheritance if required. Note we do not
8330 -- give this information for generic actual types, since it is
8331 -- unwelcome noise in that case in instantiations. We also
8332 -- generally suppress the message in instantiations, and also
8333 -- if it involves internal names.
8335 if Opt.List_Inherited_Aspects
8336 and then not Is_Generic_Actual_Type (Typ)
8337 and then Instantiation_Depth (Sloc (Typ)) = 0
8338 and then not Is_Internal_Name (Chars (T))
8339 and then not Is_Internal_Name (Chars (Typ))
8341 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8342 Error_Msg_Node_2 := T;
8343 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8352 procedure Add_Condition (Cond : Node_Id) is
8354 -- This is the first predicate expression
8359 -- Otherwise concatenate to the existing predicate expressions by
8360 -- using "and then".
8365 Left_Opnd => Relocate_Node (Expr),
8366 Right_Opnd => Cond);
8370 --------------------
8371 -- Add_Predicates --
8372 --------------------
8374 procedure Add_Predicates is
8375 procedure Add_Predicate (Prag : Node_Id);
8376 -- Concatenate the expression of predicate pragma Prag to Expr by
8377 -- using a short circuit "and then" operator.
8383 procedure Add_Predicate (Prag : Node_Id) is
8384 procedure Replace_Type_Reference (N : Node_Id);
8385 -- Replace a single occurrence N of the subtype name with a
8386 -- reference to the formal of the predicate function. N can be an
8387 -- identifier referencing the subtype, or a selected component,
8388 -- representing an appropriately qualified occurrence of the
8391 procedure Replace_Type_References is
8392 new Replace_Type_References_Generic (Replace_Type_Reference);
8393 -- Traverse an expression changing every occurrence of an
8394 -- identifier whose name matches the name of the subtype with a
8395 -- reference to the formal parameter of the predicate function.
8397 ----------------------------
8398 -- Replace_Type_Reference --
8399 ----------------------------
8401 procedure Replace_Type_Reference (N : Node_Id) is
8403 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8404 -- Use the Sloc of the usage name, not the defining name
8407 Set_Entity (N, Object_Entity);
8409 -- We want to treat the node as if it comes from source, so
8410 -- that ASIS will not ignore it.
8412 Set_Comes_From_Source (N, True);
8413 end Replace_Type_Reference;
8417 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8421 -- Start of processing for Add_Predicate
8424 -- Extract the arguments of the pragma. The expression itself
8425 -- is copied for use in the predicate function, to preserve the
8426 -- original version for ASIS use.
8428 Arg1 := First (Pragma_Argument_Associations (Prag));
8429 Arg2 := Next (Arg1);
8431 Arg1 := Get_Pragma_Arg (Arg1);
8432 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8434 -- When the predicate pragma applies to the current type or its
8435 -- full view, replace all occurrences of the subtype name with
8436 -- references to the formal parameter of the predicate function.
8438 if Entity (Arg1) = Typ
8439 or else Full_View (Entity (Arg1)) = Typ
8441 Replace_Type_References (Arg2, Typ);
8443 -- If the predicate pragma comes from an aspect, replace the
8444 -- saved expression because we need the subtype references
8445 -- replaced for the calls to Preanalyze_Spec_Expression in
8446 -- Check_Aspect_At_xxx routines.
8448 if Present (Asp) then
8449 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8452 -- "and"-in the Arg2 condition to evolving expression
8454 Add_Condition (Relocate_Node (Arg2));
8462 -- Start of processing for Add_Predicates
8465 Ritem := First_Rep_Item (Typ);
8466 while Present (Ritem) loop
8467 if Nkind (Ritem) = N_Pragma
8468 and then Pragma_Name (Ritem) = Name_Predicate
8470 Add_Predicate (Ritem);
8472 -- If the type is declared in an inner package it may be frozen
8473 -- outside of the package, and the generated pragma has not been
8474 -- analyzed yet, so capture the expression for the predicate
8475 -- function at this point.
8477 elsif Nkind (Ritem) = N_Aspect_Specification
8478 and then Present (Aspect_Rep_Item (Ritem))
8479 and then Scope (Typ) /= Current_Scope
8482 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8485 if Nkind (Prag) = N_Pragma
8486 and then Pragma_Name (Prag) = Name_Predicate
8488 Add_Predicate (Prag);
8493 Next_Rep_Item (Ritem);
8501 function Process_RE (N : Node_Id) return Traverse_Result is
8503 if Nkind (N) = N_Raise_Expression then
8504 Set_Convert_To_Return_False (N);
8515 function Test_RE (N : Node_Id) return Traverse_Result is
8517 if Nkind (N) = N_Raise_Expression then
8518 Raise_Expression_Present := True;
8527 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8529 -- Start of processing for Build_Predicate_Functions
8532 -- Return if already built or if type does not have predicates
8534 SId := Predicate_Function (Typ);
8535 if not Has_Predicates (Typ)
8536 or else (Present (SId) and then Has_Completion (SId))
8541 -- The related type may be subject to pragma Ghost. Set the mode now to
8542 -- ensure that the predicate functions are properly marked as Ghost.
8544 Set_Ghost_Mode_From_Entity (Typ);
8546 -- Prepare to construct predicate expression
8550 if Present (SId) then
8551 FDecl := Unit_Declaration_Node (SId);
8554 FDecl := Build_Predicate_Function_Declaration (Typ);
8555 SId := Defining_Entity (FDecl);
8558 -- Recover name of formal parameter of function that replaces references
8559 -- to the type in predicate expressions.
8563 (First (Parameter_Specifications (Specification (FDecl))));
8565 Object_Name := Chars (Object_Entity);
8566 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8568 -- Add predicates for ancestor if present. These must come before the
8569 -- ones for the current type, as required by AI12-0071-1.
8572 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
8574 if Present (Atyp) then
8579 -- Add Predicates for the current type
8583 -- Case where predicates are present
8585 if Present (Expr) then
8587 -- Test for raise expression present
8591 -- If raise expression is present, capture a copy of Expr for use
8592 -- in building the predicateM function version later on. For this
8593 -- copy we replace references to Object_Entity by Object_Entity_M.
8595 if Raise_Expression_Present then
8597 Map : constant Elist_Id := New_Elmt_List;
8598 New_V : Entity_Id := Empty;
8600 -- The unanalyzed expression will be copied and appear in
8601 -- both functions. Normally expressions do not declare new
8602 -- entities, but quantified expressions do, so we need to
8603 -- create new entities for their bound variables, to prevent
8604 -- multiple definitions in gigi.
8606 function Reset_Loop_Variable (N : Node_Id)
8607 return Traverse_Result;
8609 procedure Collect_Loop_Variables is
8610 new Traverse_Proc (Reset_Loop_Variable);
8612 ------------------------
8613 -- Reset_Loop_Variable --
8614 ------------------------
8616 function Reset_Loop_Variable (N : Node_Id)
8617 return Traverse_Result
8620 if Nkind (N) = N_Iterator_Specification then
8621 New_V := Make_Defining_Identifier
8622 (Sloc (N), Chars (Defining_Identifier (N)));
8624 Set_Defining_Identifier (N, New_V);
8628 end Reset_Loop_Variable;
8631 Append_Elmt (Object_Entity, Map);
8632 Append_Elmt (Object_Entity_M, Map);
8633 Expr_M := New_Copy_Tree (Expr, Map => Map);
8634 Collect_Loop_Variables (Expr_M);
8638 -- Build the main predicate function
8641 SIdB : constant Entity_Id :=
8642 Make_Defining_Identifier (Loc,
8643 Chars => New_External_Name (Chars (Typ), "Predicate"));
8644 -- The entity for the function body
8651 -- The predicate function is shared between views of a type
8653 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8654 Set_Predicate_Function (Full_View (Typ), SId);
8657 -- Mark the predicate function explicitly as Ghost because it does
8658 -- not come from source.
8660 if Ghost_Mode > None then
8661 Set_Is_Ghost_Entity (SId);
8664 -- Build function body
8667 Make_Function_Specification (Loc,
8668 Defining_Unit_Name => SIdB,
8669 Parameter_Specifications => New_List (
8670 Make_Parameter_Specification (Loc,
8671 Defining_Identifier =>
8672 Make_Defining_Identifier (Loc, Object_Name),
8674 New_Occurrence_Of (Typ, Loc))),
8675 Result_Definition =>
8676 New_Occurrence_Of (Standard_Boolean, Loc));
8679 Make_Subprogram_Body (Loc,
8680 Specification => Spec,
8681 Declarations => Empty_List,
8682 Handled_Statement_Sequence =>
8683 Make_Handled_Sequence_Of_Statements (Loc,
8684 Statements => New_List (
8685 Make_Simple_Return_Statement (Loc,
8686 Expression => Expr))));
8688 -- If declaration has not been analyzed yet, Insert declaration
8689 -- before freeze node. Insert body itself after freeze node.
8691 if not Analyzed (FDecl) then
8692 Insert_Before_And_Analyze (N, FDecl);
8695 Insert_After_And_Analyze (N, FBody);
8697 -- Static predicate functions are always side-effect free, and
8698 -- in most cases dynamic predicate functions are as well. Mark
8699 -- them as such whenever possible, so redundant predicate checks
8700 -- can be optimized. If there is a variable reference within the
8701 -- expression, the function is not pure.
8703 if Expander_Active then
8705 Side_Effect_Free (Expr, Variable_Ref => True));
8706 Set_Is_Inlined (SId);
8710 -- Test for raise expressions present and if so build M version
8712 if Raise_Expression_Present then
8714 SId : constant Entity_Id :=
8715 Make_Defining_Identifier (Loc,
8716 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8717 -- The entity for the function spec
8719 SIdB : constant Entity_Id :=
8720 Make_Defining_Identifier (Loc,
8721 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8722 -- The entity for the function body
8730 -- Mark any raise expressions for special expansion
8732 Process_REs (Expr_M);
8734 -- Build function declaration
8736 Set_Ekind (SId, E_Function);
8737 Set_Is_Predicate_Function_M (SId);
8738 Set_Predicate_Function_M (Typ, SId);
8740 -- The predicate function is shared between views of a type
8742 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8743 Set_Predicate_Function_M (Full_View (Typ), SId);
8746 -- Mark the predicate function explicitly as Ghost because it
8747 -- does not come from source.
8749 if Ghost_Mode > None then
8750 Set_Is_Ghost_Entity (SId);
8754 Make_Function_Specification (Loc,
8755 Defining_Unit_Name => SId,
8756 Parameter_Specifications => New_List (
8757 Make_Parameter_Specification (Loc,
8758 Defining_Identifier => Object_Entity_M,
8759 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8760 Result_Definition =>
8761 New_Occurrence_Of (Standard_Boolean, Loc));
8764 Make_Subprogram_Declaration (Loc,
8765 Specification => Spec);
8767 -- Build function body
8770 Make_Function_Specification (Loc,
8771 Defining_Unit_Name => SIdB,
8772 Parameter_Specifications => New_List (
8773 Make_Parameter_Specification (Loc,
8774 Defining_Identifier =>
8775 Make_Defining_Identifier (Loc, Object_Name),
8777 New_Occurrence_Of (Typ, Loc))),
8778 Result_Definition =>
8779 New_Occurrence_Of (Standard_Boolean, Loc));
8781 -- Build the body, we declare the boolean expression before
8782 -- doing the return, because we are not really confident of
8783 -- what happens if a return appears within a return.
8786 Make_Defining_Identifier (Loc,
8787 Chars => New_Internal_Name ('B'));
8790 Make_Subprogram_Body (Loc,
8791 Specification => Spec,
8793 Declarations => New_List (
8794 Make_Object_Declaration (Loc,
8795 Defining_Identifier => BTemp,
8796 Constant_Present => True,
8797 Object_Definition =>
8798 New_Occurrence_Of (Standard_Boolean, Loc),
8799 Expression => Expr_M)),
8801 Handled_Statement_Sequence =>
8802 Make_Handled_Sequence_Of_Statements (Loc,
8803 Statements => New_List (
8804 Make_Simple_Return_Statement (Loc,
8805 Expression => New_Occurrence_Of (BTemp, Loc)))));
8807 -- Insert declaration before freeze node and body after
8809 Insert_Before_And_Analyze (N, FDecl);
8810 Insert_After_And_Analyze (N, FBody);
8814 -- See if we have a static predicate. Note that the answer may be
8815 -- yes even if we have an explicit Dynamic_Predicate present.
8822 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8825 PS := Is_Predicate_Static (Expr, Object_Name);
8828 -- Case where we have a predicate-static aspect
8832 -- We don't set Has_Static_Predicate_Aspect, since we can have
8833 -- any of the three cases (Predicate, Dynamic_Predicate, or
8834 -- Static_Predicate) generating a predicate with an expression
8835 -- that is predicate-static. We just indicate that we have a
8836 -- predicate that can be treated as static.
8838 Set_Has_Static_Predicate (Typ);
8840 -- For discrete subtype, build the static predicate list
8842 if Is_Discrete_Type (Typ) then
8843 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8845 -- If we don't get a static predicate list, it means that we
8846 -- have a case where this is not possible, most typically in
8847 -- the case where we inherit a dynamic predicate. We do not
8848 -- consider this an error, we just leave the predicate as
8849 -- dynamic. But if we do succeed in building the list, then
8850 -- we mark the predicate as static.
8852 if No (Static_Discrete_Predicate (Typ)) then
8853 Set_Has_Static_Predicate (Typ, False);
8856 -- For real or string subtype, save predicate expression
8858 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8859 Set_Static_Real_Or_String_Predicate (Typ, Expr);
8862 -- Case of dynamic predicate (expression is not predicate-static)
8865 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8866 -- is only set if we have an explicit Dynamic_Predicate aspect
8867 -- given. Here we may simply have a Predicate aspect where the
8868 -- expression happens not to be predicate-static.
8870 -- Emit an error when the predicate is categorized as static
8871 -- but its expression is not predicate-static.
8873 -- First a little fiddling to get a nice location for the
8874 -- message. If the expression is of the form (A and then B),
8875 -- where A is an inherited predicate, then use the right
8876 -- operand for the Sloc. This avoids getting confused by a call
8877 -- to an inherited predicate with a less convenient source
8881 while Nkind (EN) = N_And_Then
8882 and then Nkind (Left_Opnd (EN)) = N_Function_Call
8883 and then Is_Predicate_Function
8884 (Entity (Name (Left_Opnd (EN))))
8886 EN := Right_Opnd (EN);
8889 -- Now post appropriate message
8891 if Has_Static_Predicate_Aspect (Typ) then
8892 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
8894 ("expression is not predicate-static (RM 3.2.4(16-22))",
8898 ("static predicate requires scalar or string type", EN);
8905 Ghost_Mode := Save_Ghost_Mode;
8906 end Build_Predicate_Functions;
8908 ------------------------------------------
8909 -- Build_Predicate_Function_Declaration --
8910 ------------------------------------------
8912 function Build_Predicate_Function_Declaration
8913 (Typ : Entity_Id) return Node_Id
8915 Loc : constant Source_Ptr := Sloc (Typ);
8917 Object_Entity : constant Entity_Id :=
8918 Make_Defining_Identifier (Loc,
8919 Chars => New_Internal_Name ('I'));
8921 -- The formal parameter of the function
8923 SId : constant Entity_Id :=
8924 Make_Defining_Identifier (Loc,
8925 Chars => New_External_Name (Chars (Typ), "Predicate"));
8927 -- The entity for the function spec
8934 Make_Function_Specification (Loc,
8935 Defining_Unit_Name => SId,
8936 Parameter_Specifications => New_List (
8937 Make_Parameter_Specification (Loc,
8938 Defining_Identifier => Object_Entity,
8939 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8940 Result_Definition =>
8941 New_Occurrence_Of (Standard_Boolean, Loc));
8943 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
8945 Set_Ekind (SId, E_Function);
8946 Set_Etype (SId, Standard_Boolean);
8947 Set_Is_Internal (SId);
8948 Set_Is_Predicate_Function (SId);
8949 Set_Predicate_Function (Typ, SId);
8951 Insert_After (Parent (Typ), FDecl);
8956 end Build_Predicate_Function_Declaration;
8958 -----------------------------------------
8959 -- Check_Aspect_At_End_Of_Declarations --
8960 -----------------------------------------
8962 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8963 Ent : constant Entity_Id := Entity (ASN);
8964 Ident : constant Node_Id := Identifier (ASN);
8965 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8967 End_Decl_Expr : constant Node_Id := Entity (Ident);
8968 -- Expression to be analyzed at end of declarations
8970 Freeze_Expr : constant Node_Id := Expression (ASN);
8971 -- Expression from call to Check_Aspect_At_Freeze_Point
8973 T : constant Entity_Id := Etype (Freeze_Expr);
8974 -- Type required for preanalyze call
8977 -- Set False if error
8979 -- On entry to this procedure, Entity (Ident) contains a copy of the
8980 -- original expression from the aspect, saved for this purpose, and
8981 -- but Expression (Ident) is a preanalyzed copy of the expression,
8982 -- preanalyzed just after the freeze point.
8984 procedure Check_Overloaded_Name;
8985 -- For aspects whose expression is simply a name, this routine checks if
8986 -- the name is overloaded or not. If so, it verifies there is an
8987 -- interpretation that matches the entity obtained at the freeze point,
8988 -- otherwise the compiler complains.
8990 ---------------------------
8991 -- Check_Overloaded_Name --
8992 ---------------------------
8994 procedure Check_Overloaded_Name is
8996 if not Is_Overloaded (End_Decl_Expr) then
8997 Err := not Is_Entity_Name (End_Decl_Expr)
8998 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
9004 Index : Interp_Index;
9008 Get_First_Interp (End_Decl_Expr, Index, It);
9009 while Present (It.Typ) loop
9010 if It.Nam = Entity (Freeze_Expr) then
9015 Get_Next_Interp (Index, It);
9019 end Check_Overloaded_Name;
9021 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9024 -- In an instance we do not perform the consistency check between freeze
9025 -- point and end of declarations, because it was done already in the
9026 -- analysis of the generic. Furthermore, the delayed analysis of an
9027 -- aspect of the instance may produce spurious errors when the generic
9028 -- is a child unit that references entities in the parent (which might
9029 -- not be in scope at the freeze point of the instance).
9034 -- Case of aspects Dimension, Dimension_System and Synchronization
9036 elsif A_Id = Aspect_Synchronization then
9039 -- Case of stream attributes, just have to compare entities. However,
9040 -- the expression is just a name (possibly overloaded), and there may
9041 -- be stream operations declared for unrelated types, so we just need
9042 -- to verify that one of these interpretations is the one available at
9043 -- at the freeze point.
9045 elsif A_Id = Aspect_Input or else
9046 A_Id = Aspect_Output or else
9047 A_Id = Aspect_Read or else
9050 Analyze (End_Decl_Expr);
9051 Check_Overloaded_Name;
9053 elsif A_Id = Aspect_Variable_Indexing or else
9054 A_Id = Aspect_Constant_Indexing or else
9055 A_Id = Aspect_Default_Iterator or else
9056 A_Id = Aspect_Iterator_Element
9058 -- Make type unfrozen before analysis, to prevent spurious errors
9059 -- about late attributes.
9061 Set_Is_Frozen (Ent, False);
9062 Analyze (End_Decl_Expr);
9063 Set_Is_Frozen (Ent, True);
9065 -- If the end of declarations comes before any other freeze
9066 -- point, the Freeze_Expr is not analyzed: no check needed.
9068 if Analyzed (Freeze_Expr) and then not In_Instance then
9069 Check_Overloaded_Name;
9077 -- Indicate that the expression comes from an aspect specification,
9078 -- which is used in subsequent analysis even if expansion is off.
9080 Set_Parent (End_Decl_Expr, ASN);
9082 -- In a generic context the aspect expressions have not been
9083 -- preanalyzed, so do it now. There are no conformance checks
9084 -- to perform in this case.
9087 Check_Aspect_At_Freeze_Point (ASN);
9090 -- The default values attributes may be defined in the private part,
9091 -- and the analysis of the expression may take place when only the
9092 -- partial view is visible. The expression must be scalar, so use
9093 -- the full view to resolve.
9095 elsif (A_Id = Aspect_Default_Value
9097 A_Id = Aspect_Default_Component_Value)
9098 and then Is_Private_Type (T)
9100 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9103 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9106 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9109 -- Output error message if error. Force error on aspect specification
9110 -- even if there is an error on the expression itself.
9114 ("!visibility of aspect for& changes after freeze point",
9117 ("info: & is frozen here, aspects evaluated at this point??",
9118 Freeze_Node (Ent), Ent);
9120 end Check_Aspect_At_End_Of_Declarations;
9122 ----------------------------------
9123 -- Check_Aspect_At_Freeze_Point --
9124 ----------------------------------
9126 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9127 Ident : constant Node_Id := Identifier (ASN);
9128 -- Identifier (use Entity field to save expression)
9130 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9132 T : Entity_Id := Empty;
9133 -- Type required for preanalyze call
9136 -- On entry to this procedure, Entity (Ident) contains a copy of the
9137 -- original expression from the aspect, saved for this purpose.
9139 -- On exit from this procedure Entity (Ident) is unchanged, still
9140 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9141 -- of the expression, preanalyzed just after the freeze point.
9143 -- Make a copy of the expression to be preanalyzed
9145 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9147 -- Find type for preanalyze call
9151 -- No_Aspect should be impossible
9154 raise Program_Error;
9156 -- Aspects taking an optional boolean argument
9158 when Boolean_Aspects |
9159 Library_Unit_Aspects =>
9161 T := Standard_Boolean;
9163 -- Aspects corresponding to attribute definition clauses
9165 when Aspect_Address =>
9166 T := RTE (RE_Address);
9168 when Aspect_Attach_Handler =>
9169 T := RTE (RE_Interrupt_ID);
9171 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9172 T := RTE (RE_Bit_Order);
9174 when Aspect_Convention =>
9178 T := RTE (RE_CPU_Range);
9180 -- Default_Component_Value is resolved with the component type
9182 when Aspect_Default_Component_Value =>
9183 T := Component_Type (Entity (ASN));
9185 when Aspect_Default_Storage_Pool =>
9186 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9188 -- Default_Value is resolved with the type entity in question
9190 when Aspect_Default_Value =>
9193 when Aspect_Dispatching_Domain =>
9194 T := RTE (RE_Dispatching_Domain);
9196 when Aspect_External_Tag =>
9197 T := Standard_String;
9199 when Aspect_External_Name =>
9200 T := Standard_String;
9202 when Aspect_Link_Name =>
9203 T := Standard_String;
9205 when Aspect_Priority | Aspect_Interrupt_Priority =>
9206 T := Standard_Integer;
9208 when Aspect_Relative_Deadline =>
9209 T := RTE (RE_Time_Span);
9211 when Aspect_Secondary_Stack_Size =>
9212 T := Standard_Integer;
9214 when Aspect_Small =>
9215 T := Universal_Real;
9217 -- For a simple storage pool, we have to retrieve the type of the
9218 -- pool object associated with the aspect's corresponding attribute
9219 -- definition clause.
9221 when Aspect_Simple_Storage_Pool =>
9222 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9224 when Aspect_Storage_Pool =>
9225 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9227 when Aspect_Alignment |
9228 Aspect_Component_Size |
9229 Aspect_Machine_Radix |
9230 Aspect_Object_Size |
9232 Aspect_Storage_Size |
9233 Aspect_Stream_Size |
9234 Aspect_Value_Size =>
9237 when Aspect_Linker_Section =>
9238 T := Standard_String;
9240 when Aspect_Synchronization =>
9243 -- Special case, the expression of these aspects is just an entity
9244 -- that does not need any resolution, so just analyze.
9253 Analyze (Expression (ASN));
9256 -- Same for Iterator aspects, where the expression is a function
9257 -- name. Legality rules are checked separately.
9259 when Aspect_Constant_Indexing |
9260 Aspect_Default_Iterator |
9261 Aspect_Iterator_Element |
9262 Aspect_Variable_Indexing =>
9263 Analyze (Expression (ASN));
9266 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9268 when Aspect_Iterable =>
9272 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9277 if Cursor = Any_Type then
9281 Assoc := First (Component_Associations (Expression (ASN)));
9282 while Present (Assoc) loop
9283 Expr := Expression (Assoc);
9286 if not Error_Posted (Expr) then
9287 Resolve_Iterable_Operation
9288 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9297 -- Invariant/Predicate take boolean expressions
9299 when Aspect_Dynamic_Predicate |
9302 Aspect_Static_Predicate |
9303 Aspect_Type_Invariant =>
9304 T := Standard_Boolean;
9306 when Aspect_Predicate_Failure =>
9307 T := Standard_String;
9309 -- Here is the list of aspects that don't require delay analysis
9311 when Aspect_Abstract_State |
9313 Aspect_Async_Readers |
9314 Aspect_Async_Writers |
9315 Aspect_Constant_After_Elaboration |
9316 Aspect_Contract_Cases |
9317 Aspect_Default_Initial_Condition |
9320 Aspect_Dimension_System |
9321 Aspect_Effective_Reads |
9322 Aspect_Effective_Writes |
9323 Aspect_Extensions_Visible |
9326 Aspect_Implicit_Dereference |
9327 Aspect_Initial_Condition |
9328 Aspect_Initializes |
9329 Aspect_Max_Queue_Length |
9330 Aspect_Obsolescent |
9333 Aspect_Postcondition |
9335 Aspect_Precondition |
9336 Aspect_Refined_Depends |
9337 Aspect_Refined_Global |
9338 Aspect_Refined_Post |
9339 Aspect_Refined_State |
9342 Aspect_Unimplemented |
9343 Aspect_Volatile_Function =>
9344 raise Program_Error;
9348 -- Do the preanalyze call
9350 Preanalyze_Spec_Expression (Expression (ASN), T);
9351 end Check_Aspect_At_Freeze_Point;
9353 -----------------------------------
9354 -- Check_Constant_Address_Clause --
9355 -----------------------------------
9357 procedure Check_Constant_Address_Clause
9361 procedure Check_At_Constant_Address (Nod : Node_Id);
9362 -- Checks that the given node N represents a name whose 'Address is
9363 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9364 -- address value is the same at the point of declaration of U_Ent and at
9365 -- the time of elaboration of the address clause.
9367 procedure Check_Expr_Constants (Nod : Node_Id);
9368 -- Checks that Nod meets the requirements for a constant address clause
9369 -- in the sense of the enclosing procedure.
9371 procedure Check_List_Constants (Lst : List_Id);
9372 -- Check that all elements of list Lst meet the requirements for a
9373 -- constant address clause in the sense of the enclosing procedure.
9375 -------------------------------
9376 -- Check_At_Constant_Address --
9377 -------------------------------
9379 procedure Check_At_Constant_Address (Nod : Node_Id) is
9381 if Is_Entity_Name (Nod) then
9382 if Present (Address_Clause (Entity ((Nod)))) then
9384 ("invalid address clause for initialized object &!",
9387 ("address for& cannot" &
9388 " depend on another address clause! (RM 13.1(22))!",
9391 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9392 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9395 ("invalid address clause for initialized object &!",
9397 Error_Msg_Node_2 := U_Ent;
9399 ("\& must be defined before & (RM 13.1(22))!",
9403 elsif Nkind (Nod) = N_Selected_Component then
9405 T : constant Entity_Id := Etype (Prefix (Nod));
9408 if (Is_Record_Type (T)
9409 and then Has_Discriminants (T))
9412 and then Is_Record_Type (Designated_Type (T))
9413 and then Has_Discriminants (Designated_Type (T)))
9416 ("invalid address clause for initialized object &!",
9419 ("\address cannot depend on component" &
9420 " of discriminated record (RM 13.1(22))!",
9423 Check_At_Constant_Address (Prefix (Nod));
9427 elsif Nkind (Nod) = N_Indexed_Component then
9428 Check_At_Constant_Address (Prefix (Nod));
9429 Check_List_Constants (Expressions (Nod));
9432 Check_Expr_Constants (Nod);
9434 end Check_At_Constant_Address;
9436 --------------------------
9437 -- Check_Expr_Constants --
9438 --------------------------
9440 procedure Check_Expr_Constants (Nod : Node_Id) is
9441 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9442 Ent : Entity_Id := Empty;
9445 if Nkind (Nod) in N_Has_Etype
9446 and then Etype (Nod) = Any_Type
9452 when N_Empty | N_Error =>
9455 when N_Identifier | N_Expanded_Name =>
9456 Ent := Entity (Nod);
9458 -- We need to look at the original node if it is different
9459 -- from the node, since we may have rewritten things and
9460 -- substituted an identifier representing the rewrite.
9462 if Original_Node (Nod) /= Nod then
9463 Check_Expr_Constants (Original_Node (Nod));
9465 -- If the node is an object declaration without initial
9466 -- value, some code has been expanded, and the expression
9467 -- is not constant, even if the constituents might be
9468 -- acceptable, as in A'Address + offset.
9470 if Ekind (Ent) = E_Variable
9472 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9474 No (Expression (Declaration_Node (Ent)))
9477 ("invalid address clause for initialized object &!",
9480 -- If entity is constant, it may be the result of expanding
9481 -- a check. We must verify that its declaration appears
9482 -- before the object in question, else we also reject the
9485 elsif Ekind (Ent) = E_Constant
9486 and then In_Same_Source_Unit (Ent, U_Ent)
9487 and then Sloc (Ent) > Loc_U_Ent
9490 ("invalid address clause for initialized object &!",
9497 -- Otherwise look at the identifier and see if it is OK
9499 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9500 or else Is_Type (Ent)
9504 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9506 -- This is the case where we must have Ent defined before
9507 -- U_Ent. Clearly if they are in different units this
9508 -- requirement is met since the unit containing Ent is
9509 -- already processed.
9511 if not In_Same_Source_Unit (Ent, U_Ent) then
9514 -- Otherwise location of Ent must be before the location
9515 -- of U_Ent, that's what prior defined means.
9517 elsif Sloc (Ent) < Loc_U_Ent then
9522 ("invalid address clause for initialized object &!",
9524 Error_Msg_Node_2 := U_Ent;
9526 ("\& must be defined before & (RM 13.1(22))!",
9530 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9531 Check_Expr_Constants (Original_Node (Nod));
9535 ("invalid address clause for initialized object &!",
9538 if Comes_From_Source (Ent) then
9540 ("\reference to variable& not allowed"
9541 & " (RM 13.1(22))!", Nod, Ent);
9544 ("non-static expression not allowed"
9545 & " (RM 13.1(22))!", Nod);
9549 when N_Integer_Literal =>
9551 -- If this is a rewritten unchecked conversion, in a system
9552 -- where Address is an integer type, always use the base type
9553 -- for a literal value. This is user-friendly and prevents
9554 -- order-of-elaboration issues with instances of unchecked
9557 if Nkind (Original_Node (Nod)) = N_Function_Call then
9558 Set_Etype (Nod, Base_Type (Etype (Nod)));
9561 when N_Real_Literal |
9563 N_Character_Literal =>
9567 Check_Expr_Constants (Low_Bound (Nod));
9568 Check_Expr_Constants (High_Bound (Nod));
9570 when N_Explicit_Dereference =>
9571 Check_Expr_Constants (Prefix (Nod));
9573 when N_Indexed_Component =>
9574 Check_Expr_Constants (Prefix (Nod));
9575 Check_List_Constants (Expressions (Nod));
9578 Check_Expr_Constants (Prefix (Nod));
9579 Check_Expr_Constants (Discrete_Range (Nod));
9581 when N_Selected_Component =>
9582 Check_Expr_Constants (Prefix (Nod));
9584 when N_Attribute_Reference =>
9585 if Nam_In (Attribute_Name (Nod), Name_Address,
9587 Name_Unchecked_Access,
9588 Name_Unrestricted_Access)
9590 Check_At_Constant_Address (Prefix (Nod));
9593 Check_Expr_Constants (Prefix (Nod));
9594 Check_List_Constants (Expressions (Nod));
9598 Check_List_Constants (Component_Associations (Nod));
9599 Check_List_Constants (Expressions (Nod));
9601 when N_Component_Association =>
9602 Check_Expr_Constants (Expression (Nod));
9604 when N_Extension_Aggregate =>
9605 Check_Expr_Constants (Ancestor_Part (Nod));
9606 Check_List_Constants (Component_Associations (Nod));
9607 Check_List_Constants (Expressions (Nod));
9612 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9613 Check_Expr_Constants (Left_Opnd (Nod));
9614 Check_Expr_Constants (Right_Opnd (Nod));
9617 Check_Expr_Constants (Right_Opnd (Nod));
9619 when N_Type_Conversion |
9620 N_Qualified_Expression |
9622 N_Unchecked_Type_Conversion =>
9623 Check_Expr_Constants (Expression (Nod));
9625 when N_Function_Call =>
9626 if not Is_Pure (Entity (Name (Nod))) then
9628 ("invalid address clause for initialized object &!",
9632 ("\function & is not pure (RM 13.1(22))!",
9633 Nod, Entity (Name (Nod)));
9636 Check_List_Constants (Parameter_Associations (Nod));
9639 when N_Parameter_Association =>
9640 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9644 ("invalid address clause for initialized object &!",
9647 ("\must be constant defined before& (RM 13.1(22))!",
9650 end Check_Expr_Constants;
9652 --------------------------
9653 -- Check_List_Constants --
9654 --------------------------
9656 procedure Check_List_Constants (Lst : List_Id) is
9660 if Present (Lst) then
9661 Nod1 := First (Lst);
9662 while Present (Nod1) loop
9663 Check_Expr_Constants (Nod1);
9667 end Check_List_Constants;
9669 -- Start of processing for Check_Constant_Address_Clause
9672 -- If rep_clauses are to be ignored, no need for legality checks. In
9673 -- particular, no need to pester user about rep clauses that violate the
9674 -- rule on constant addresses, given that these clauses will be removed
9675 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9676 -- we want to relax these checks.
9678 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9679 Check_Expr_Constants (Expr);
9681 end Check_Constant_Address_Clause;
9683 ---------------------------
9684 -- Check_Pool_Size_Clash --
9685 ---------------------------
9687 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9691 -- We need to find out which one came first. Note that in the case of
9692 -- aspects mixed with pragmas there are cases where the processing order
9693 -- is reversed, which is why we do the check here.
9695 if Sloc (SP) < Sloc (SS) then
9696 Error_Msg_Sloc := Sloc (SP);
9698 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9701 Error_Msg_Sloc := Sloc (SS);
9703 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9707 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9708 end Check_Pool_Size_Clash;
9710 ----------------------------------------
9711 -- Check_Record_Representation_Clause --
9712 ----------------------------------------
9714 procedure Check_Record_Representation_Clause (N : Node_Id) is
9715 Loc : constant Source_Ptr := Sloc (N);
9716 Ident : constant Node_Id := Identifier (N);
9717 Rectype : Entity_Id;
9722 Hbit : Uint := Uint_0;
9726 Max_Bit_So_Far : Uint;
9727 -- Records the maximum bit position so far. If all field positions
9728 -- are monotonically increasing, then we can skip the circuit for
9729 -- checking for overlap, since no overlap is possible.
9731 Tagged_Parent : Entity_Id := Empty;
9732 -- This is set in the case of a derived tagged type for which we have
9733 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9734 -- positioned by record representation clauses). In this case we must
9735 -- check for overlap between components of this tagged type, and the
9736 -- components of its parent. Tagged_Parent will point to this parent
9737 -- type. For all other cases Tagged_Parent is left set to Empty.
9739 Parent_Last_Bit : Uint;
9740 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9741 -- last bit position for any field in the parent type. We only need to
9742 -- check overlap for fields starting below this point.
9744 Overlap_Check_Required : Boolean;
9745 -- Used to keep track of whether or not an overlap check is required
9747 Overlap_Detected : Boolean := False;
9748 -- Set True if an overlap is detected
9750 Ccount : Natural := 0;
9751 -- Number of component clauses in record rep clause
9753 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9754 -- Given two entities for record components or discriminants, checks
9755 -- if they have overlapping component clauses and issues errors if so.
9757 procedure Find_Component;
9758 -- Finds component entity corresponding to current component clause (in
9759 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9760 -- start/stop bits for the field. If there is no matching component or
9761 -- if the matching component does not have a component clause, then
9762 -- that's an error and Comp is set to Empty, but no error message is
9763 -- issued, since the message was already given. Comp is also set to
9764 -- Empty if the current "component clause" is in fact a pragma.
9766 -----------------------------
9767 -- Check_Component_Overlap --
9768 -----------------------------
9770 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9771 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9772 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9775 if Present (CC1) and then Present (CC2) then
9777 -- Exclude odd case where we have two tag components in the same
9778 -- record, both at location zero. This seems a bit strange, but
9779 -- it seems to happen in some circumstances, perhaps on an error.
9781 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9785 -- Here we check if the two fields overlap
9788 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9789 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9790 E1 : constant Uint := S1 + Esize (C1_Ent);
9791 E2 : constant Uint := S2 + Esize (C2_Ent);
9794 if E2 <= S1 or else E1 <= S2 then
9797 Error_Msg_Node_2 := Component_Name (CC2);
9798 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9799 Error_Msg_Node_1 := Component_Name (CC1);
9801 ("component& overlaps & #", Component_Name (CC1));
9802 Overlap_Detected := True;
9806 end Check_Component_Overlap;
9808 --------------------
9809 -- Find_Component --
9810 --------------------
9812 procedure Find_Component is
9814 procedure Search_Component (R : Entity_Id);
9815 -- Search components of R for a match. If found, Comp is set
9817 ----------------------
9818 -- Search_Component --
9819 ----------------------
9821 procedure Search_Component (R : Entity_Id) is
9823 Comp := First_Component_Or_Discriminant (R);
9824 while Present (Comp) loop
9826 -- Ignore error of attribute name for component name (we
9827 -- already gave an error message for this, so no need to
9830 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9833 exit when Chars (Comp) = Chars (Component_Name (CC));
9836 Next_Component_Or_Discriminant (Comp);
9838 end Search_Component;
9840 -- Start of processing for Find_Component
9843 -- Return with Comp set to Empty if we have a pragma
9845 if Nkind (CC) = N_Pragma then
9850 -- Search current record for matching component
9852 Search_Component (Rectype);
9854 -- If not found, maybe component of base type discriminant that is
9855 -- absent from statically constrained first subtype.
9858 Search_Component (Base_Type (Rectype));
9861 -- If no component, or the component does not reference the component
9862 -- clause in question, then there was some previous error for which
9863 -- we already gave a message, so just return with Comp Empty.
9865 if No (Comp) or else Component_Clause (Comp) /= CC then
9866 Check_Error_Detected;
9869 -- Normal case where we have a component clause
9872 Fbit := Component_Bit_Offset (Comp);
9873 Lbit := Fbit + Esize (Comp) - 1;
9877 -- Start of processing for Check_Record_Representation_Clause
9881 Rectype := Entity (Ident);
9883 if Rectype = Any_Type then
9886 Rectype := Underlying_Type (Rectype);
9889 -- See if we have a fully repped derived tagged type
9892 PS : constant Entity_Id := Parent_Subtype (Rectype);
9895 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9896 Tagged_Parent := PS;
9898 -- Find maximum bit of any component of the parent type
9900 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9901 Pcomp := First_Entity (Tagged_Parent);
9902 while Present (Pcomp) loop
9903 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9904 if Component_Bit_Offset (Pcomp) /= No_Uint
9905 and then Known_Static_Esize (Pcomp)
9910 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9914 -- Skip anonymous types generated for constrained array
9915 -- or record components.
9920 Next_Entity (Pcomp);
9925 -- All done if no component clauses
9927 CC := First (Component_Clauses (N));
9933 -- If a tag is present, then create a component clause that places it
9934 -- at the start of the record (otherwise gigi may place it after other
9935 -- fields that have rep clauses).
9937 Fent := First_Entity (Rectype);
9939 if Nkind (Fent) = N_Defining_Identifier
9940 and then Chars (Fent) = Name_uTag
9942 Set_Component_Bit_Offset (Fent, Uint_0);
9943 Set_Normalized_Position (Fent, Uint_0);
9944 Set_Normalized_First_Bit (Fent, Uint_0);
9945 Set_Normalized_Position_Max (Fent, Uint_0);
9946 Init_Esize (Fent, System_Address_Size);
9948 Set_Component_Clause (Fent,
9949 Make_Component_Clause (Loc,
9950 Component_Name => Make_Identifier (Loc, Name_uTag),
9952 Position => Make_Integer_Literal (Loc, Uint_0),
9953 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9955 Make_Integer_Literal (Loc,
9956 UI_From_Int (System_Address_Size))));
9958 Ccount := Ccount + 1;
9961 Max_Bit_So_Far := Uint_Minus_1;
9962 Overlap_Check_Required := False;
9964 -- Process the component clauses
9966 while Present (CC) loop
9969 if Present (Comp) then
9970 Ccount := Ccount + 1;
9972 -- We need a full overlap check if record positions non-monotonic
9974 if Fbit <= Max_Bit_So_Far then
9975 Overlap_Check_Required := True;
9978 Max_Bit_So_Far := Lbit;
9980 -- Check bit position out of range of specified size
9982 if Has_Size_Clause (Rectype)
9983 and then RM_Size (Rectype) <= Lbit
9986 ("bit number out of range of specified size",
9989 -- Check for overlap with tag component
9992 if Is_Tagged_Type (Rectype)
9993 and then Fbit < System_Address_Size
9996 ("component overlaps tag field of&",
9997 Component_Name (CC), Rectype);
9998 Overlap_Detected := True;
10001 if Hbit < Lbit then
10006 -- Check parent overlap if component might overlap parent field
10008 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
10009 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
10010 while Present (Pcomp) loop
10011 if not Is_Tag (Pcomp)
10012 and then Chars (Pcomp) /= Name_uParent
10014 Check_Component_Overlap (Comp, Pcomp);
10017 Next_Component_Or_Discriminant (Pcomp);
10025 -- Now that we have processed all the component clauses, check for
10026 -- overlap. We have to leave this till last, since the components can
10027 -- appear in any arbitrary order in the representation clause.
10029 -- We do not need this check if all specified ranges were monotonic,
10030 -- as recorded by Overlap_Check_Required being False at this stage.
10032 -- This first section checks if there are any overlapping entries at
10033 -- all. It does this by sorting all entries and then seeing if there are
10034 -- any overlaps. If there are none, then that is decisive, but if there
10035 -- are overlaps, they may still be OK (they may result from fields in
10036 -- different variants).
10038 if Overlap_Check_Required then
10039 Overlap_Check1 : declare
10041 OC_Fbit : array (0 .. Ccount) of Uint;
10042 -- First-bit values for component clauses, the value is the offset
10043 -- of the first bit of the field from start of record. The zero
10044 -- entry is for use in sorting.
10046 OC_Lbit : array (0 .. Ccount) of Uint;
10047 -- Last-bit values for component clauses, the value is the offset
10048 -- of the last bit of the field from start of record. The zero
10049 -- entry is for use in sorting.
10051 OC_Count : Natural := 0;
10052 -- Count of entries in OC_Fbit and OC_Lbit
10054 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10055 -- Compare routine for Sort
10057 procedure OC_Move (From : Natural; To : Natural);
10058 -- Move routine for Sort
10060 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
10066 function OC_Lt (Op1, Op2 : Natural) return Boolean is
10068 return OC_Fbit (Op1) < OC_Fbit (Op2);
10075 procedure OC_Move (From : Natural; To : Natural) is
10077 OC_Fbit (To) := OC_Fbit (From);
10078 OC_Lbit (To) := OC_Lbit (From);
10081 -- Start of processing for Overlap_Check
10084 CC := First (Component_Clauses (N));
10085 while Present (CC) loop
10087 -- Exclude component clause already marked in error
10089 if not Error_Posted (CC) then
10092 if Present (Comp) then
10093 OC_Count := OC_Count + 1;
10094 OC_Fbit (OC_Count) := Fbit;
10095 OC_Lbit (OC_Count) := Lbit;
10102 Sorting.Sort (OC_Count);
10104 Overlap_Check_Required := False;
10105 for J in 1 .. OC_Count - 1 loop
10106 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10107 Overlap_Check_Required := True;
10111 end Overlap_Check1;
10114 -- If Overlap_Check_Required is still True, then we have to do the full
10115 -- scale overlap check, since we have at least two fields that do
10116 -- overlap, and we need to know if that is OK since they are in
10117 -- different variant, or whether we have a definite problem.
10119 if Overlap_Check_Required then
10120 Overlap_Check2 : declare
10121 C1_Ent, C2_Ent : Entity_Id;
10122 -- Entities of components being checked for overlap
10125 -- Component_List node whose Component_Items are being checked
10128 -- Component declaration for component being checked
10131 C1_Ent := First_Entity (Base_Type (Rectype));
10133 -- Loop through all components in record. For each component check
10134 -- for overlap with any of the preceding elements on the component
10135 -- list containing the component and also, if the component is in
10136 -- a variant, check against components outside the case structure.
10137 -- This latter test is repeated recursively up the variant tree.
10139 Main_Component_Loop : while Present (C1_Ent) loop
10140 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10141 goto Continue_Main_Component_Loop;
10144 -- Skip overlap check if entity has no declaration node. This
10145 -- happens with discriminants in constrained derived types.
10146 -- Possibly we are missing some checks as a result, but that
10147 -- does not seem terribly serious.
10149 if No (Declaration_Node (C1_Ent)) then
10150 goto Continue_Main_Component_Loop;
10153 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10155 -- Loop through component lists that need checking. Check the
10156 -- current component list and all lists in variants above us.
10158 Component_List_Loop : loop
10160 -- If derived type definition, go to full declaration
10161 -- If at outer level, check discriminants if there are any.
10163 if Nkind (Clist) = N_Derived_Type_Definition then
10164 Clist := Parent (Clist);
10167 -- Outer level of record definition, check discriminants
10169 if Nkind_In (Clist, N_Full_Type_Declaration,
10170 N_Private_Type_Declaration)
10172 if Has_Discriminants (Defining_Identifier (Clist)) then
10174 First_Discriminant (Defining_Identifier (Clist));
10175 while Present (C2_Ent) loop
10176 exit when C1_Ent = C2_Ent;
10177 Check_Component_Overlap (C1_Ent, C2_Ent);
10178 Next_Discriminant (C2_Ent);
10182 -- Record extension case
10184 elsif Nkind (Clist) = N_Derived_Type_Definition then
10187 -- Otherwise check one component list
10190 Citem := First (Component_Items (Clist));
10191 while Present (Citem) loop
10192 if Nkind (Citem) = N_Component_Declaration then
10193 C2_Ent := Defining_Identifier (Citem);
10194 exit when C1_Ent = C2_Ent;
10195 Check_Component_Overlap (C1_Ent, C2_Ent);
10202 -- Check for variants above us (the parent of the Clist can
10203 -- be a variant, in which case its parent is a variant part,
10204 -- and the parent of the variant part is a component list
10205 -- whose components must all be checked against the current
10206 -- component for overlap).
10208 if Nkind (Parent (Clist)) = N_Variant then
10209 Clist := Parent (Parent (Parent (Clist)));
10211 -- Check for possible discriminant part in record, this
10212 -- is treated essentially as another level in the
10213 -- recursion. For this case the parent of the component
10214 -- list is the record definition, and its parent is the
10215 -- full type declaration containing the discriminant
10218 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10219 Clist := Parent (Parent ((Clist)));
10221 -- If neither of these two cases, we are at the top of
10225 exit Component_List_Loop;
10227 end loop Component_List_Loop;
10229 <<Continue_Main_Component_Loop>>
10230 Next_Entity (C1_Ent);
10232 end loop Main_Component_Loop;
10233 end Overlap_Check2;
10236 -- The following circuit deals with warning on record holes (gaps). We
10237 -- skip this check if overlap was detected, since it makes sense for the
10238 -- programmer to fix this illegality before worrying about warnings.
10240 if not Overlap_Detected and Warn_On_Record_Holes then
10241 Record_Hole_Check : declare
10242 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10243 -- Full declaration of record type
10245 procedure Check_Component_List
10249 -- Check component list CL for holes. The starting bit should be
10250 -- Sbit. which is zero for the main record component list and set
10251 -- appropriately for recursive calls for variants. DS is set to
10252 -- a list of discriminant specifications to be included in the
10253 -- consideration of components. It is No_List if none to consider.
10255 --------------------------
10256 -- Check_Component_List --
10257 --------------------------
10259 procedure Check_Component_List
10267 Compl := Integer (List_Length (Component_Items (CL)));
10269 if DS /= No_List then
10270 Compl := Compl + Integer (List_Length (DS));
10274 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10275 -- Gather components (zero entry is for sort routine)
10277 Ncomps : Natural := 0;
10278 -- Number of entries stored in Comps (starting at Comps (1))
10281 -- One component item or discriminant specification
10284 -- Starting bit for next component
10287 -- Component entity
10292 function Lt (Op1, Op2 : Natural) return Boolean;
10293 -- Compare routine for Sort
10295 procedure Move (From : Natural; To : Natural);
10296 -- Move routine for Sort
10298 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10304 function Lt (Op1, Op2 : Natural) return Boolean is
10306 return Component_Bit_Offset (Comps (Op1))
10308 Component_Bit_Offset (Comps (Op2));
10315 procedure Move (From : Natural; To : Natural) is
10317 Comps (To) := Comps (From);
10321 -- Gather discriminants into Comp
10323 if DS /= No_List then
10324 Citem := First (DS);
10325 while Present (Citem) loop
10326 if Nkind (Citem) = N_Discriminant_Specification then
10328 Ent : constant Entity_Id :=
10329 Defining_Identifier (Citem);
10331 if Ekind (Ent) = E_Discriminant then
10332 Ncomps := Ncomps + 1;
10333 Comps (Ncomps) := Ent;
10342 -- Gather component entities into Comp
10344 Citem := First (Component_Items (CL));
10345 while Present (Citem) loop
10346 if Nkind (Citem) = N_Component_Declaration then
10347 Ncomps := Ncomps + 1;
10348 Comps (Ncomps) := Defining_Identifier (Citem);
10354 -- Now sort the component entities based on the first bit.
10355 -- Note we already know there are no overlapping components.
10357 Sorting.Sort (Ncomps);
10359 -- Loop through entries checking for holes
10362 for J in 1 .. Ncomps loop
10364 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
10366 if Error_Msg_Uint_1 > 0 then
10368 ("?H?^-bit gap before component&",
10369 Component_Name (Component_Clause (CEnt)), CEnt);
10372 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10375 -- Process variant parts recursively if present
10377 if Present (Variant_Part (CL)) then
10378 Variant := First (Variants (Variant_Part (CL)));
10379 while Present (Variant) loop
10380 Check_Component_List
10381 (Component_List (Variant), Nbit, No_List);
10386 end Check_Component_List;
10388 -- Start of processing for Record_Hole_Check
10395 if Is_Tagged_Type (Rectype) then
10396 Sbit := UI_From_Int (System_Address_Size);
10401 if Nkind (Decl) = N_Full_Type_Declaration
10402 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10404 Check_Component_List
10405 (Component_List (Type_Definition (Decl)),
10407 Discriminant_Specifications (Decl));
10410 end Record_Hole_Check;
10413 -- For records that have component clauses for all components, and whose
10414 -- size is less than or equal to 32, we need to know the size in the
10415 -- front end to activate possible packed array processing where the
10416 -- component type is a record.
10418 -- At this stage Hbit + 1 represents the first unused bit from all the
10419 -- component clauses processed, so if the component clauses are
10420 -- complete, then this is the length of the record.
10422 -- For records longer than System.Storage_Unit, and for those where not
10423 -- all components have component clauses, the back end determines the
10424 -- length (it may for example be appropriate to round up the size
10425 -- to some convenient boundary, based on alignment considerations, etc).
10427 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10429 -- Nothing to do if at least one component has no component clause
10431 Comp := First_Component_Or_Discriminant (Rectype);
10432 while Present (Comp) loop
10433 exit when No (Component_Clause (Comp));
10434 Next_Component_Or_Discriminant (Comp);
10437 -- If we fall out of loop, all components have component clauses
10438 -- and so we can set the size to the maximum value.
10441 Set_RM_Size (Rectype, Hbit + 1);
10444 end Check_Record_Representation_Clause;
10450 procedure Check_Size
10454 Biased : out Boolean)
10456 procedure Size_Too_Small_Error (Min_Siz : Uint);
10457 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10460 --------------------------
10461 -- Size_Too_Small_Error --
10462 --------------------------
10464 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10466 -- This error is suppressed in ASIS mode to allow for different ASIS
10467 -- back ends or ASIS-based tools to query the illegal clause.
10469 if not ASIS_Mode then
10470 Error_Msg_Uint_1 := Min_Siz;
10471 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
10473 end Size_Too_Small_Error;
10477 UT : constant Entity_Id := Underlying_Type (T);
10480 -- Start of processing for Check_Size
10485 -- Reject patently improper size values
10487 if Is_Elementary_Type (T)
10488 and then Siz > UI_From_Int (Int'Last)
10490 Error_Msg_N ("Size value too large for elementary type", N);
10492 if Nkind (Original_Node (N)) = N_Op_Expon then
10494 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10498 -- Dismiss generic types
10500 if Is_Generic_Type (T)
10502 Is_Generic_Type (UT)
10504 Is_Generic_Type (Root_Type (UT))
10508 -- Guard against previous errors
10510 elsif No (UT) or else UT = Any_Type then
10511 Check_Error_Detected;
10514 -- Check case of bit packed array
10516 elsif Is_Array_Type (UT)
10517 and then Known_Static_Component_Size (UT)
10518 and then Is_Bit_Packed_Array (UT)
10526 Asiz := Component_Size (UT);
10527 Indx := First_Index (UT);
10529 Ityp := Etype (Indx);
10531 -- If non-static bound, then we are not in the business of
10532 -- trying to check the length, and indeed an error will be
10533 -- issued elsewhere, since sizes of non-static array types
10534 -- cannot be set implicitly or explicitly.
10536 if not Is_OK_Static_Subtype (Ityp) then
10540 -- Otherwise accumulate next dimension
10542 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10543 Expr_Value (Type_Low_Bound (Ityp)) +
10547 exit when No (Indx);
10550 if Asiz <= Siz then
10554 Size_Too_Small_Error (Asiz);
10555 Set_Esize (T, Asiz);
10556 Set_RM_Size (T, Asiz);
10560 -- All other composite types are ignored
10562 elsif Is_Composite_Type (UT) then
10565 -- For fixed-point types, don't check minimum if type is not frozen,
10566 -- since we don't know all the characteristics of the type that can
10567 -- affect the size (e.g. a specified small) till freeze time.
10569 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
10572 -- Cases for which a minimum check is required
10575 -- Ignore if specified size is correct for the type
10577 if Known_Esize (UT) and then Siz = Esize (UT) then
10581 -- Otherwise get minimum size
10583 M := UI_From_Int (Minimum_Size (UT));
10587 -- Size is less than minimum size, but one possibility remains
10588 -- that we can manage with the new size if we bias the type.
10590 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10593 Size_Too_Small_Error (M);
10595 Set_RM_Size (T, M);
10603 --------------------------
10604 -- Freeze_Entity_Checks --
10605 --------------------------
10607 procedure Freeze_Entity_Checks (N : Node_Id) is
10608 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10609 -- Inspect the primitive operations of type Typ and hide all pairs of
10610 -- implicitly declared non-overridden non-fully conformant homographs
10611 -- (Ada RM 8.3 12.3/2).
10613 -------------------------------------
10614 -- Hide_Non_Overridden_Subprograms --
10615 -------------------------------------
10617 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10618 procedure Hide_Matching_Homographs
10619 (Subp_Id : Entity_Id;
10620 Start_Elmt : Elmt_Id);
10621 -- Inspect a list of primitive operations starting with Start_Elmt
10622 -- and find matching implicitly declared non-overridden non-fully
10623 -- conformant homographs of Subp_Id. If found, all matches along
10624 -- with Subp_Id are hidden from all visibility.
10626 function Is_Non_Overridden_Or_Null_Procedure
10627 (Subp_Id : Entity_Id) return Boolean;
10628 -- Determine whether subprogram Subp_Id is implicitly declared non-
10629 -- overridden subprogram or an implicitly declared null procedure.
10631 ------------------------------
10632 -- Hide_Matching_Homographs --
10633 ------------------------------
10635 procedure Hide_Matching_Homographs
10636 (Subp_Id : Entity_Id;
10637 Start_Elmt : Elmt_Id)
10640 Prim_Elmt : Elmt_Id;
10643 Prim_Elmt := Start_Elmt;
10644 while Present (Prim_Elmt) loop
10645 Prim := Node (Prim_Elmt);
10647 -- The current primitive is implicitly declared non-overridden
10648 -- non-fully conformant homograph of Subp_Id. Both subprograms
10649 -- must be hidden from visibility.
10651 if Chars (Prim) = Chars (Subp_Id)
10652 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10653 and then not Fully_Conformant (Prim, Subp_Id)
10655 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10656 Set_Is_Immediately_Visible (Prim, False);
10657 Set_Is_Potentially_Use_Visible (Prim, False);
10659 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10660 Set_Is_Immediately_Visible (Subp_Id, False);
10661 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10664 Next_Elmt (Prim_Elmt);
10666 end Hide_Matching_Homographs;
10668 -----------------------------------------
10669 -- Is_Non_Overridden_Or_Null_Procedure --
10670 -----------------------------------------
10672 function Is_Non_Overridden_Or_Null_Procedure
10673 (Subp_Id : Entity_Id) return Boolean
10675 Alias_Id : Entity_Id;
10678 -- The subprogram is inherited (implicitly declared), it does not
10679 -- override and does not cover a primitive of an interface.
10681 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10682 and then Present (Alias (Subp_Id))
10683 and then No (Interface_Alias (Subp_Id))
10684 and then No (Overridden_Operation (Subp_Id))
10686 Alias_Id := Alias (Subp_Id);
10688 if Requires_Overriding (Alias_Id) then
10691 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10692 and then Null_Present (Parent (Alias_Id))
10699 end Is_Non_Overridden_Or_Null_Procedure;
10703 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10705 Prim_Elmt : Elmt_Id;
10707 -- Start of processing for Hide_Non_Overridden_Subprograms
10710 -- Inspect the list of primitives looking for non-overridden
10713 if Present (Prim_Ops) then
10714 Prim_Elmt := First_Elmt (Prim_Ops);
10715 while Present (Prim_Elmt) loop
10716 Prim := Node (Prim_Elmt);
10717 Next_Elmt (Prim_Elmt);
10719 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10720 Hide_Matching_Homographs
10722 Start_Elmt => Prim_Elmt);
10726 end Hide_Non_Overridden_Subprograms;
10730 E : constant Entity_Id := Entity (N);
10732 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10733 -- True in non-generic case. Some of the processing here is skipped
10734 -- for the generic case since it is not needed. Basically in the
10735 -- generic case, we only need to do stuff that might generate error
10736 -- messages or warnings.
10738 -- Start of processing for Freeze_Entity_Checks
10741 -- Remember that we are processing a freezing entity. Required to
10742 -- ensure correct decoration of internal entities associated with
10743 -- interfaces (see New_Overloaded_Entity).
10745 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10747 -- For tagged types covering interfaces add internal entities that link
10748 -- the primitives of the interfaces with the primitives that cover them.
10749 -- Note: These entities were originally generated only when generating
10750 -- code because their main purpose was to provide support to initialize
10751 -- the secondary dispatch tables. They are now generated also when
10752 -- compiling with no code generation to provide ASIS the relationship
10753 -- between interface primitives and tagged type primitives. They are
10754 -- also used to locate primitives covering interfaces when processing
10755 -- generics (see Derive_Subprograms).
10757 -- This is not needed in the generic case
10759 if Ada_Version >= Ada_2005
10760 and then Non_Generic_Case
10761 and then Ekind (E) = E_Record_Type
10762 and then Is_Tagged_Type (E)
10763 and then not Is_Interface (E)
10764 and then Has_Interfaces (E)
10766 -- This would be a good common place to call the routine that checks
10767 -- overriding of interface primitives (and thus factorize calls to
10768 -- Check_Abstract_Overriding located at different contexts in the
10769 -- compiler). However, this is not possible because it causes
10770 -- spurious errors in case of late overriding.
10772 Add_Internal_Interface_Entities (E);
10775 -- After all forms of overriding have been resolved, a tagged type may
10776 -- be left with a set of implicitly declared and possibly erroneous
10777 -- abstract subprograms, null procedures and subprograms that require
10778 -- overriding. If this set contains fully conformant homographs, then
10779 -- one is chosen arbitrarily (already done during resolution), otherwise
10780 -- all remaining non-fully conformant homographs are hidden from
10781 -- visibility (Ada RM 8.3 12.3/2).
10783 if Is_Tagged_Type (E) then
10784 Hide_Non_Overridden_Subprograms (E);
10789 if Ekind (E) = E_Record_Type
10790 and then Is_CPP_Class (E)
10791 and then Is_Tagged_Type (E)
10792 and then Tagged_Type_Expansion
10794 if CPP_Num_Prims (E) = 0 then
10796 -- If the CPP type has user defined components then it must import
10797 -- primitives from C++. This is required because if the C++ class
10798 -- has no primitives then the C++ compiler does not added the _tag
10799 -- component to the type.
10801 if First_Entity (E) /= Last_Entity (E) then
10803 ("'C'P'P type must import at least one primitive from C++??",
10808 -- Check that all its primitives are abstract or imported from C++.
10809 -- Check also availability of the C++ constructor.
10812 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10814 Error_Reported : Boolean := False;
10818 Elmt := First_Elmt (Primitive_Operations (E));
10819 while Present (Elmt) loop
10820 Prim := Node (Elmt);
10822 if Comes_From_Source (Prim) then
10823 if Is_Abstract_Subprogram (Prim) then
10826 elsif not Is_Imported (Prim)
10827 or else Convention (Prim) /= Convention_CPP
10830 ("primitives of 'C'P'P types must be imported from C++ "
10831 & "or abstract??", Prim);
10833 elsif not Has_Constructors
10834 and then not Error_Reported
10836 Error_Msg_Name_1 := Chars (E);
10838 ("??'C'P'P constructor required for type %", Prim);
10839 Error_Reported := True;
10848 -- Check Ada derivation of CPP type
10850 if Expander_Active -- why? losing errors in -gnatc mode???
10851 and then Present (Etype (E)) -- defend against errors
10852 and then Tagged_Type_Expansion
10853 and then Ekind (E) = E_Record_Type
10854 and then Etype (E) /= E
10855 and then Is_CPP_Class (Etype (E))
10856 and then CPP_Num_Prims (Etype (E)) > 0
10857 and then not Is_CPP_Class (E)
10858 and then not Has_CPP_Constructors (Etype (E))
10860 -- If the parent has C++ primitives but it has no constructor then
10861 -- check that all the primitives are overridden in this derivation;
10862 -- otherwise the constructor of the parent is needed to build the
10870 Elmt := First_Elmt (Primitive_Operations (E));
10871 while Present (Elmt) loop
10872 Prim := Node (Elmt);
10874 if not Is_Abstract_Subprogram (Prim)
10875 and then No (Interface_Alias (Prim))
10876 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
10878 Error_Msg_Name_1 := Chars (Etype (E));
10880 ("'C'P'P constructor required for parent type %", E);
10889 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
10891 -- If we have a type with predicates, build predicate function. This is
10892 -- not needed in the generic case, nor within TSS subprograms and other
10893 -- predefined primitives.
10896 and then Non_Generic_Case
10897 and then not Within_Internal_Subprogram
10898 and then Has_Predicates (E)
10900 Build_Predicate_Functions (E, N);
10903 -- If type has delayed aspects, this is where we do the preanalysis at
10904 -- the freeze point, as part of the consistent visibility check. Note
10905 -- that this must be done after calling Build_Predicate_Functions or
10906 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10907 -- the subtype name in the saved expression so that they will not cause
10908 -- trouble in the preanalysis.
10910 -- This is also not needed in the generic case
10912 if Non_Generic_Case
10913 and then Has_Delayed_Aspects (E)
10914 and then Scope (E) = Current_Scope
10916 -- Retrieve the visibility to the discriminants in order to properly
10917 -- analyze the aspects.
10919 Push_Scope_And_Install_Discriminants (E);
10925 -- Look for aspect specification entries for this entity
10927 Ritem := First_Rep_Item (E);
10928 while Present (Ritem) loop
10929 if Nkind (Ritem) = N_Aspect_Specification
10930 and then Entity (Ritem) = E
10931 and then Is_Delayed_Aspect (Ritem)
10933 Check_Aspect_At_Freeze_Point (Ritem);
10936 Next_Rep_Item (Ritem);
10940 Uninstall_Discriminants_And_Pop_Scope (E);
10943 -- For a record type, deal with variant parts. This has to be delayed
10944 -- to this point, because of the issue of statically predicated
10945 -- subtypes, which we have to ensure are frozen before checking
10946 -- choices, since we need to have the static choice list set.
10948 if Is_Record_Type (E) then
10949 Check_Variant_Part : declare
10950 D : constant Node_Id := Declaration_Node (E);
10955 Others_Present : Boolean;
10956 pragma Warnings (Off, Others_Present);
10957 -- Indicates others present, not used in this case
10959 procedure Non_Static_Choice_Error (Choice : Node_Id);
10960 -- Error routine invoked by the generic instantiation below when
10961 -- the variant part has a non static choice.
10963 procedure Process_Declarations (Variant : Node_Id);
10964 -- Processes declarations associated with a variant. We analyzed
10965 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10966 -- but we still need the recursive call to Check_Choices for any
10967 -- nested variant to get its choices properly processed. This is
10968 -- also where we expand out the choices if expansion is active.
10970 package Variant_Choices_Processing is new
10971 Generic_Check_Choices
10972 (Process_Empty_Choice => No_OP,
10973 Process_Non_Static_Choice => Non_Static_Choice_Error,
10974 Process_Associated_Node => Process_Declarations);
10975 use Variant_Choices_Processing;
10977 -----------------------------
10978 -- Non_Static_Choice_Error --
10979 -----------------------------
10981 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10983 Flag_Non_Static_Expr
10984 ("choice given in variant part is not static!", Choice);
10985 end Non_Static_Choice_Error;
10987 --------------------------
10988 -- Process_Declarations --
10989 --------------------------
10991 procedure Process_Declarations (Variant : Node_Id) is
10992 CL : constant Node_Id := Component_List (Variant);
10996 -- Check for static predicate present in this variant
10998 if Has_SP_Choice (Variant) then
11000 -- Here we expand. You might expect to find this call in
11001 -- Expand_N_Variant_Part, but that is called when we first
11002 -- see the variant part, and we cannot do this expansion
11003 -- earlier than the freeze point, since for statically
11004 -- predicated subtypes, the predicate is not known till
11005 -- the freeze point.
11007 -- Furthermore, we do this expansion even if the expander
11008 -- is not active, because other semantic processing, e.g.
11009 -- for aggregates, requires the expanded list of choices.
11011 -- If the expander is not active, then we can't just clobber
11012 -- the list since it would invalidate the ASIS -gnatct tree.
11013 -- So we have to rewrite the variant part with a Rewrite
11014 -- call that replaces it with a copy and clobber the copy.
11016 if not Expander_Active then
11018 NewV : constant Node_Id := New_Copy (Variant);
11020 Set_Discrete_Choices
11021 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11022 Rewrite (Variant, NewV);
11026 Expand_Static_Predicates_In_Choices (Variant);
11029 -- We don't need to worry about the declarations in the variant
11030 -- (since they were analyzed by Analyze_Choices when we first
11031 -- encountered the variant), but we do need to take care of
11032 -- expansion of any nested variants.
11034 if not Null_Present (CL) then
11035 VP := Variant_Part (CL);
11037 if Present (VP) then
11039 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11042 end Process_Declarations;
11044 -- Start of processing for Check_Variant_Part
11047 -- Find component list
11051 if Nkind (D) = N_Full_Type_Declaration then
11052 T := Type_Definition (D);
11054 if Nkind (T) = N_Record_Definition then
11055 C := Component_List (T);
11057 elsif Nkind (T) = N_Derived_Type_Definition
11058 and then Present (Record_Extension_Part (T))
11060 C := Component_List (Record_Extension_Part (T));
11064 -- Case of variant part present
11066 if Present (C) and then Present (Variant_Part (C)) then
11067 VP := Variant_Part (C);
11072 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11074 -- If the last variant does not contain the Others choice,
11075 -- replace it with an N_Others_Choice node since Gigi always
11076 -- wants an Others. Note that we do not bother to call Analyze
11077 -- on the modified variant part, since its only effect would be
11078 -- to compute the Others_Discrete_Choices node laboriously, and
11079 -- of course we already know the list of choices corresponding
11080 -- to the others choice (it's the list we're replacing).
11082 -- We only want to do this if the expander is active, since
11083 -- we do not want to clobber the ASIS tree.
11085 if Expander_Active then
11087 Last_Var : constant Node_Id :=
11088 Last_Non_Pragma (Variants (VP));
11090 Others_Node : Node_Id;
11093 if Nkind (First (Discrete_Choices (Last_Var))) /=
11096 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11097 Set_Others_Discrete_Choices
11098 (Others_Node, Discrete_Choices (Last_Var));
11099 Set_Discrete_Choices
11100 (Last_Var, New_List (Others_Node));
11105 end Check_Variant_Part;
11107 end Freeze_Entity_Checks;
11109 -------------------------
11110 -- Get_Alignment_Value --
11111 -------------------------
11113 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11114 Align : constant Uint := Static_Integer (Expr);
11117 if Align = No_Uint then
11120 elsif Align <= 0 then
11122 -- This error is suppressed in ASIS mode to allow for different ASIS
11123 -- back ends or ASIS-based tools to query the illegal clause.
11125 if not ASIS_Mode then
11126 Error_Msg_N ("alignment value must be positive", Expr);
11132 for J in Int range 0 .. 64 loop
11134 M : constant Uint := Uint_2 ** J;
11137 exit when M = Align;
11141 -- This error is suppressed in ASIS mode to allow for
11142 -- different ASIS back ends or ASIS-based tools to query the
11145 if not ASIS_Mode then
11146 Error_Msg_N ("alignment value must be power of 2", Expr);
11156 end Get_Alignment_Value;
11158 -----------------------------
11159 -- Get_Interfacing_Aspects --
11160 -----------------------------
11162 procedure Get_Interfacing_Aspects
11163 (Iface_Asp : Node_Id;
11164 Conv_Asp : out Node_Id;
11165 EN_Asp : out Node_Id;
11166 Expo_Asp : out Node_Id;
11167 Imp_Asp : out Node_Id;
11168 LN_Asp : out Node_Id;
11169 Do_Checks : Boolean := False)
11171 procedure Save_Or_Duplication_Error
11173 To : in out Node_Id);
11174 -- Save the value of aspect Asp in node To. If To already has a value,
11175 -- then this is considered a duplicate use of aspect. Emit an error if
11176 -- flag Do_Checks is set.
11178 -------------------------------
11179 -- Save_Or_Duplication_Error --
11180 -------------------------------
11182 procedure Save_Or_Duplication_Error
11184 To : in out Node_Id)
11187 -- Detect an extra aspect and issue an error
11189 if Present (To) then
11191 Error_Msg_Name_1 := Chars (Identifier (Asp));
11192 Error_Msg_Sloc := Sloc (To);
11193 Error_Msg_N ("aspect % previously given #", Asp);
11196 -- Otherwise capture the aspect
11201 end Save_Or_Duplication_Error;
11206 Asp_Id : Aspect_Id;
11208 -- The following variables capture each individual aspect
11210 Conv : Node_Id := Empty;
11211 EN : Node_Id := Empty;
11212 Expo : Node_Id := Empty;
11213 Imp : Node_Id := Empty;
11214 LN : Node_Id := Empty;
11216 -- Start of processing for Get_Interfacing_Aspects
11219 -- The input interfacing aspect should reside in an aspect specification
11222 pragma Assert (Is_List_Member (Iface_Asp));
11224 -- Examine the aspect specifications of the related entity. Find and
11225 -- capture all interfacing aspects. Detect duplicates and emit errors
11228 Asp := First (List_Containing (Iface_Asp));
11229 while Present (Asp) loop
11230 Asp_Id := Get_Aspect_Id (Asp);
11232 if Asp_Id = Aspect_Convention then
11233 Save_Or_Duplication_Error (Asp, Conv);
11235 elsif Asp_Id = Aspect_External_Name then
11236 Save_Or_Duplication_Error (Asp, EN);
11238 elsif Asp_Id = Aspect_Export then
11239 Save_Or_Duplication_Error (Asp, Expo);
11241 elsif Asp_Id = Aspect_Import then
11242 Save_Or_Duplication_Error (Asp, Imp);
11244 elsif Asp_Id = Aspect_Link_Name then
11245 Save_Or_Duplication_Error (Asp, LN);
11256 end Get_Interfacing_Aspects;
11258 -------------------------------------
11259 -- Inherit_Aspects_At_Freeze_Point --
11260 -------------------------------------
11262 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11263 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11264 (Rep_Item : Node_Id) return Boolean;
11265 -- This routine checks if Rep_Item is either a pragma or an aspect
11266 -- specification node whose correponding pragma (if any) is present in
11267 -- the Rep Item chain of the entity it has been specified to.
11269 --------------------------------------------------
11270 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11271 --------------------------------------------------
11273 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11274 (Rep_Item : Node_Id) return Boolean
11278 Nkind (Rep_Item) = N_Pragma
11279 or else Present_In_Rep_Item
11280 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11281 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11283 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11286 -- A representation item is either subtype-specific (Size and Alignment
11287 -- clauses) or type-related (all others). Subtype-specific aspects may
11288 -- differ for different subtypes of the same type (RM 13.1.8).
11290 -- A derived type inherits each type-related representation aspect of
11291 -- its parent type that was directly specified before the declaration of
11292 -- the derived type (RM 13.1.15).
11294 -- A derived subtype inherits each subtype-specific representation
11295 -- aspect of its parent subtype that was directly specified before the
11296 -- declaration of the derived type (RM 13.1.15).
11298 -- The general processing involves inheriting a representation aspect
11299 -- from a parent type whenever the first rep item (aspect specification,
11300 -- attribute definition clause, pragma) corresponding to the given
11301 -- representation aspect in the rep item chain of Typ, if any, isn't
11302 -- directly specified to Typ but to one of its parents.
11304 -- ??? Note that, for now, just a limited number of representation
11305 -- aspects have been inherited here so far. Many of them are
11306 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11307 -- a non- exhaustive list of aspects that likely also need to
11308 -- be moved to this routine: Alignment, Component_Alignment,
11309 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11310 -- Preelaborable_Initialization, RM_Size and Small.
11312 -- In addition, Convention must be propagated from base type to subtype,
11313 -- because the subtype may have been declared on an incomplete view.
11315 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11321 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11322 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11323 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11324 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11326 Set_Is_Ada_2005_Only (Typ);
11331 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11332 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11333 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11334 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11336 Set_Is_Ada_2012_Only (Typ);
11341 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11342 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11343 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11344 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11346 Set_Is_Atomic (Typ);
11347 Set_Is_Volatile (Typ);
11348 Set_Treat_As_Volatile (Typ);
11353 if Is_Record_Type (Typ)
11354 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11356 Set_Convention (Typ, Convention (Base_Type (Typ)));
11359 -- Default_Component_Value
11361 -- Verify that there is no rep_item declared for the type, and there
11362 -- is one coming from an ancestor.
11364 if Is_Array_Type (Typ)
11365 and then Is_Base_Type (Typ)
11366 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11367 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11369 Set_Default_Aspect_Component_Value (Typ,
11370 Default_Aspect_Component_Value
11371 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11376 if Is_Scalar_Type (Typ)
11377 and then Is_Base_Type (Typ)
11378 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11379 and then Has_Rep_Item (Typ, Name_Default_Value)
11381 Set_Has_Default_Aspect (Typ);
11382 Set_Default_Aspect_Value (Typ,
11383 Default_Aspect_Value
11384 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11389 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11390 and then Has_Rep_Item (Typ, Name_Discard_Names)
11391 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11392 (Get_Rep_Item (Typ, Name_Discard_Names))
11394 Set_Discard_Names (Typ);
11399 if not Has_Rep_Item (Typ, Name_Volatile, False)
11400 and then Has_Rep_Item (Typ, Name_Volatile)
11401 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11402 (Get_Rep_Item (Typ, Name_Volatile))
11404 Set_Is_Volatile (Typ);
11405 Set_Treat_As_Volatile (Typ);
11408 -- Volatile_Full_Access
11410 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11411 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11412 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11413 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11415 Set_Is_Volatile_Full_Access (Typ);
11416 Set_Is_Volatile (Typ);
11417 Set_Treat_As_Volatile (Typ);
11420 -- Inheritance for derived types only
11422 if Is_Derived_Type (Typ) then
11424 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11425 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11428 -- Atomic_Components
11430 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11431 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11432 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11433 (Get_Rep_Item (Typ, Name_Atomic_Components))
11435 Set_Has_Atomic_Components (Imp_Bas_Typ);
11438 -- Volatile_Components
11440 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11441 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11442 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11443 (Get_Rep_Item (Typ, Name_Volatile_Components))
11445 Set_Has_Volatile_Components (Imp_Bas_Typ);
11448 -- Finalize_Storage_Only
11450 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11451 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11453 Set_Finalize_Storage_Only (Bas_Typ);
11456 -- Universal_Aliasing
11458 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11459 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11460 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11461 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11463 Set_Universal_Aliasing (Imp_Bas_Typ);
11468 if Is_Record_Type (Typ) then
11469 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11470 and then Has_Rep_Item (Typ, Name_Bit_Order)
11472 Set_Reverse_Bit_Order (Bas_Typ,
11473 Reverse_Bit_Order (Entity (Name
11474 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11478 -- Scalar_Storage_Order
11480 -- Note: the aspect is specified on a first subtype, but recorded
11481 -- in a flag of the base type!
11483 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11484 and then Typ = Bas_Typ
11486 -- For a type extension, always inherit from parent; otherwise
11487 -- inherit if no default applies. Note: we do not check for
11488 -- an explicit rep item on the parent type when inheriting,
11489 -- because the parent SSO may itself have been set by default.
11491 if not Has_Rep_Item (First_Subtype (Typ),
11492 Name_Scalar_Storage_Order, False)
11493 and then (Is_Tagged_Type (Bas_Typ)
11494 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11496 SSO_Set_High_By_Default (Bas_Typ)))
11498 Set_Reverse_Storage_Order (Bas_Typ,
11499 Reverse_Storage_Order
11500 (Implementation_Base_Type (Etype (Bas_Typ))));
11502 -- Clear default SSO indications, since the inherited aspect
11503 -- which was set explicitly overrides the default.
11505 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11506 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11511 end Inherit_Aspects_At_Freeze_Point;
11517 procedure Initialize is
11519 Address_Clause_Checks.Init;
11520 Compile_Time_Warnings_Errors.Init;
11521 Unchecked_Conversions.Init;
11523 if AAMP_On_Target then
11524 Independence_Checks.Init;
11528 ---------------------------
11529 -- Install_Discriminants --
11530 ---------------------------
11532 procedure Install_Discriminants (E : Entity_Id) is
11536 Disc := First_Discriminant (E);
11537 while Present (Disc) loop
11538 Prev := Current_Entity (Disc);
11539 Set_Current_Entity (Disc);
11540 Set_Is_Immediately_Visible (Disc);
11541 Set_Homonym (Disc, Prev);
11542 Next_Discriminant (Disc);
11544 end Install_Discriminants;
11546 -------------------------
11547 -- Is_Operational_Item --
11548 -------------------------
11550 function Is_Operational_Item (N : Node_Id) return Boolean is
11552 if Nkind (N) /= N_Attribute_Definition_Clause then
11557 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11560 -- List of operational items is given in AARM 13.1(8.mm/1).
11561 -- It is clearly incomplete, as it does not include iterator
11562 -- aspects, among others.
11564 return Id = Attribute_Constant_Indexing
11565 or else Id = Attribute_Default_Iterator
11566 or else Id = Attribute_Implicit_Dereference
11567 or else Id = Attribute_Input
11568 or else Id = Attribute_Iterator_Element
11569 or else Id = Attribute_Iterable
11570 or else Id = Attribute_Output
11571 or else Id = Attribute_Read
11572 or else Id = Attribute_Variable_Indexing
11573 or else Id = Attribute_Write
11574 or else Id = Attribute_External_Tag;
11577 end Is_Operational_Item;
11579 -------------------------
11580 -- Is_Predicate_Static --
11581 -------------------------
11583 -- Note: the basic legality of the expression has already been checked, so
11584 -- we don't need to worry about cases or ranges on strings for example.
11586 function Is_Predicate_Static
11588 Nam : Name_Id) return Boolean
11590 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11591 -- Given a list of case expression alternatives, returns True if all
11592 -- the alternatives are static (have all static choices, and a static
11595 function All_Static_Choices (L : List_Id) return Boolean;
11596 -- Returns true if all elements of the list are OK static choices
11597 -- as defined below for Is_Static_Choice. Used for case expression
11598 -- alternatives and for the right operand of a membership test. An
11599 -- others_choice is static if the corresponding expression is static.
11600 -- The staticness of the bounds is checked separately.
11602 function Is_Static_Choice (N : Node_Id) return Boolean;
11603 -- Returns True if N represents a static choice (static subtype, or
11604 -- static subtype indication, or static expression, or static range).
11606 -- Note that this is a bit more inclusive than we actually need
11607 -- (in particular membership tests do not allow the use of subtype
11608 -- indications). But that doesn't matter, we have already checked
11609 -- that the construct is legal to get this far.
11611 function Is_Type_Ref (N : Node_Id) return Boolean;
11612 pragma Inline (Is_Type_Ref);
11613 -- Returns True if N is a reference to the type for the predicate in the
11614 -- expression (i.e. if it is an identifier whose Chars field matches the
11615 -- Nam given in the call). N must not be parenthesized, if the type name
11616 -- appears in parens, this routine will return False.
11618 ----------------------------------
11619 -- All_Static_Case_Alternatives --
11620 ----------------------------------
11622 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11627 while Present (N) loop
11628 if not (All_Static_Choices (Discrete_Choices (N))
11629 and then Is_OK_Static_Expression (Expression (N)))
11638 end All_Static_Case_Alternatives;
11640 ------------------------
11641 -- All_Static_Choices --
11642 ------------------------
11644 function All_Static_Choices (L : List_Id) return Boolean is
11649 while Present (N) loop
11650 if not Is_Static_Choice (N) then
11658 end All_Static_Choices;
11660 ----------------------
11661 -- Is_Static_Choice --
11662 ----------------------
11664 function Is_Static_Choice (N : Node_Id) return Boolean is
11666 return Nkind (N) = N_Others_Choice
11667 or else Is_OK_Static_Expression (N)
11668 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11669 and then Is_OK_Static_Subtype (Entity (N)))
11670 or else (Nkind (N) = N_Subtype_Indication
11671 and then Is_OK_Static_Subtype (Entity (N)))
11672 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11673 end Is_Static_Choice;
11679 function Is_Type_Ref (N : Node_Id) return Boolean is
11681 return Nkind (N) = N_Identifier
11682 and then Chars (N) = Nam
11683 and then Paren_Count (N) = 0;
11686 -- Start of processing for Is_Predicate_Static
11689 -- Predicate_Static means one of the following holds. Numbers are the
11690 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11692 -- 16: A static expression
11694 if Is_OK_Static_Expression (Expr) then
11697 -- 17: A membership test whose simple_expression is the current
11698 -- instance, and whose membership_choice_list meets the requirements
11699 -- for a static membership test.
11701 elsif Nkind (Expr) in N_Membership_Test
11702 and then ((Present (Right_Opnd (Expr))
11703 and then Is_Static_Choice (Right_Opnd (Expr)))
11705 (Present (Alternatives (Expr))
11706 and then All_Static_Choices (Alternatives (Expr))))
11710 -- 18. A case_expression whose selecting_expression is the current
11711 -- instance, and whose dependent expressions are static expressions.
11713 elsif Nkind (Expr) = N_Case_Expression
11714 and then Is_Type_Ref (Expression (Expr))
11715 and then All_Static_Case_Alternatives (Alternatives (Expr))
11719 -- 19. A call to a predefined equality or ordering operator, where one
11720 -- operand is the current instance, and the other is a static
11723 -- Note: the RM is clearly wrong here in not excluding string types.
11724 -- Without this exclusion, we would allow expressions like X > "ABC"
11725 -- to be considered as predicate-static, which is clearly not intended,
11726 -- since the idea is for predicate-static to be a subset of normal
11727 -- static expressions (and "DEF" > "ABC" is not a static expression).
11729 -- However, we do allow internally generated (not from source) equality
11730 -- and inequality operations to be valid on strings (this helps deal
11731 -- with cases where we transform A in "ABC" to A = "ABC).
11733 elsif Nkind (Expr) in N_Op_Compare
11734 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11735 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11736 and then not Comes_From_Source (Expr)))
11737 and then ((Is_Type_Ref (Left_Opnd (Expr))
11738 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11740 (Is_Type_Ref (Right_Opnd (Expr))
11741 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11745 -- 20. A call to a predefined boolean logical operator, where each
11746 -- operand is predicate-static.
11748 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11749 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11750 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11752 (Nkind (Expr) = N_Op_Not
11753 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11757 -- 21. A short-circuit control form where both operands are
11758 -- predicate-static.
11760 elsif Nkind (Expr) in N_Short_Circuit
11761 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11762 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11766 -- 22. A parenthesized predicate-static expression. This does not
11767 -- require any special test, since we just ignore paren levels in
11768 -- all the cases above.
11770 -- One more test that is an implementation artifact caused by the fact
11771 -- that we are analyzing not the original expression, but the generated
11772 -- expression in the body of the predicate function. This can include
11773 -- references to inherited predicates, so that the expression we are
11774 -- processing looks like:
11776 -- xxPredicate (typ (Inns)) and then expression
11778 -- Where the call is to a Predicate function for an inherited predicate.
11779 -- We simply ignore such a call, which could be to either a dynamic or
11780 -- a static predicate. Note that if the parent predicate is dynamic then
11781 -- eventually this type will be marked as dynamic, but you are allowed
11782 -- to specify a static predicate for a subtype which is inheriting a
11783 -- dynamic predicate, so the static predicate validation here ignores
11784 -- the inherited predicate even if it is dynamic.
11785 -- In all cases, a static predicate can only apply to a scalar type.
11787 elsif Nkind (Expr) = N_Function_Call
11788 and then Is_Predicate_Function (Entity (Name (Expr)))
11789 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
11793 -- That's an exhaustive list of tests, all other cases are not
11794 -- predicate-static, so we return False.
11799 end Is_Predicate_Static;
11801 ---------------------
11802 -- Kill_Rep_Clause --
11803 ---------------------
11805 procedure Kill_Rep_Clause (N : Node_Id) is
11807 pragma Assert (Ignore_Rep_Clauses);
11809 -- Note: we use Replace rather than Rewrite, because we don't want
11810 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11811 -- rep clause that is being replaced.
11813 Replace (N, Make_Null_Statement (Sloc (N)));
11815 -- The null statement must be marked as not coming from source. This is
11816 -- so that ASIS ignores it, and also the back end does not expect bogus
11817 -- "from source" null statements in weird places (e.g. in declarative
11818 -- regions where such null statements are not allowed).
11820 Set_Comes_From_Source (N, False);
11821 end Kill_Rep_Clause;
11827 function Minimum_Size
11829 Biased : Boolean := False) return Nat
11831 Lo : Uint := No_Uint;
11832 Hi : Uint := No_Uint;
11833 LoR : Ureal := No_Ureal;
11834 HiR : Ureal := No_Ureal;
11835 LoSet : Boolean := False;
11836 HiSet : Boolean := False;
11839 Ancest : Entity_Id;
11840 R_Typ : constant Entity_Id := Root_Type (T);
11843 -- If bad type, return 0
11845 if T = Any_Type then
11848 -- For generic types, just return zero. There cannot be any legitimate
11849 -- need to know such a size, but this routine may be called with a
11850 -- generic type as part of normal processing.
11852 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11855 -- Access types (cannot have size smaller than System.Address)
11857 elsif Is_Access_Type (T) then
11858 return System_Address_Size;
11860 -- Floating-point types
11862 elsif Is_Floating_Point_Type (T) then
11863 return UI_To_Int (Esize (R_Typ));
11867 elsif Is_Discrete_Type (T) then
11869 -- The following loop is looking for the nearest compile time known
11870 -- bounds following the ancestor subtype chain. The idea is to find
11871 -- the most restrictive known bounds information.
11875 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11880 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11881 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11888 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11889 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11895 Ancest := Ancestor_Subtype (Ancest);
11897 if No (Ancest) then
11898 Ancest := Base_Type (T);
11900 if Is_Generic_Type (Ancest) then
11906 -- Fixed-point types. We can't simply use Expr_Value to get the
11907 -- Corresponding_Integer_Value values of the bounds, since these do not
11908 -- get set till the type is frozen, and this routine can be called
11909 -- before the type is frozen. Similarly the test for bounds being static
11910 -- needs to include the case where we have unanalyzed real literals for
11911 -- the same reason.
11913 elsif Is_Fixed_Point_Type (T) then
11915 -- The following loop is looking for the nearest compile time known
11916 -- bounds following the ancestor subtype chain. The idea is to find
11917 -- the most restrictive known bounds information.
11921 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11925 -- Note: In the following two tests for LoSet and HiSet, it may
11926 -- seem redundant to test for N_Real_Literal here since normally
11927 -- one would assume that the test for the value being known at
11928 -- compile time includes this case. However, there is a glitch.
11929 -- If the real literal comes from folding a non-static expression,
11930 -- then we don't consider any non- static expression to be known
11931 -- at compile time if we are in configurable run time mode (needed
11932 -- in some cases to give a clearer definition of what is and what
11933 -- is not accepted). So the test is indeed needed. Without it, we
11934 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11937 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11938 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11940 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11947 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11948 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11950 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11956 Ancest := Ancestor_Subtype (Ancest);
11958 if No (Ancest) then
11959 Ancest := Base_Type (T);
11961 if Is_Generic_Type (Ancest) then
11967 Lo := UR_To_Uint (LoR / Small_Value (T));
11968 Hi := UR_To_Uint (HiR / Small_Value (T));
11970 -- No other types allowed
11973 raise Program_Error;
11976 -- Fall through with Hi and Lo set. Deal with biased case
11979 and then not Is_Fixed_Point_Type (T)
11980 and then not (Is_Enumeration_Type (T)
11981 and then Has_Non_Standard_Rep (T)))
11982 or else Has_Biased_Representation (T)
11988 -- Null range case, size is always zero. We only do this in the discrete
11989 -- type case, since that's the odd case that came up. Probably we should
11990 -- also do this in the fixed-point case, but doing so causes peculiar
11991 -- gigi failures, and it is not worth worrying about this incredibly
11992 -- marginal case (explicit null-range fixed-point type declarations)???
11994 if Lo > Hi and then Is_Discrete_Type (T) then
11997 -- Signed case. Note that we consider types like range 1 .. -1 to be
11998 -- signed for the purpose of computing the size, since the bounds have
11999 -- to be accommodated in the base type.
12001 elsif Lo < 0 or else Hi < 0 then
12005 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12006 -- Note that we accommodate the case where the bounds cross. This
12007 -- can happen either because of the way the bounds are declared
12008 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12022 -- If both bounds are positive, make sure that both are represen-
12023 -- table in the case where the bounds are crossed. This can happen
12024 -- either because of the way the bounds are declared, or because of
12025 -- the algorithm in Freeze_Fixed_Point_Type.
12031 -- S = size, (can accommodate 0 .. (2**size - 1))
12034 while Hi >= Uint_2 ** S loop
12042 ---------------------------
12043 -- New_Stream_Subprogram --
12044 ---------------------------
12046 procedure New_Stream_Subprogram
12050 Nam : TSS_Name_Type)
12052 Loc : constant Source_Ptr := Sloc (N);
12053 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
12054 Subp_Id : Entity_Id;
12055 Subp_Decl : Node_Id;
12059 Defer_Declaration : constant Boolean :=
12060 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12061 -- For a tagged type, there is a declaration for each stream attribute
12062 -- at the freeze point, and we must generate only a completion of this
12063 -- declaration. We do the same for private types, because the full view
12064 -- might be tagged. Otherwise we generate a declaration at the point of
12065 -- the attribute definition clause. If the attribute definition comes
12066 -- from an aspect specification the declaration is part of the freeze
12067 -- actions of the type.
12069 function Build_Spec return Node_Id;
12070 -- Used for declaration and renaming declaration, so that this is
12071 -- treated as a renaming_as_body.
12077 function Build_Spec return Node_Id is
12078 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12081 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
12084 Subp_Id := Make_Defining_Identifier (Loc, Sname);
12086 -- S : access Root_Stream_Type'Class
12088 Formals := New_List (
12089 Make_Parameter_Specification (Loc,
12090 Defining_Identifier =>
12091 Make_Defining_Identifier (Loc, Name_S),
12093 Make_Access_Definition (Loc,
12095 New_Occurrence_Of (
12096 Designated_Type (Etype (F)), Loc))));
12098 if Nam = TSS_Stream_Input then
12100 Make_Function_Specification (Loc,
12101 Defining_Unit_Name => Subp_Id,
12102 Parameter_Specifications => Formals,
12103 Result_Definition => T_Ref);
12107 Append_To (Formals,
12108 Make_Parameter_Specification (Loc,
12109 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12110 Out_Present => Out_P,
12111 Parameter_Type => T_Ref));
12114 Make_Procedure_Specification (Loc,
12115 Defining_Unit_Name => Subp_Id,
12116 Parameter_Specifications => Formals);
12122 -- Start of processing for New_Stream_Subprogram
12125 F := First_Formal (Subp);
12127 if Ekind (Subp) = E_Procedure then
12128 Etyp := Etype (Next_Formal (F));
12130 Etyp := Etype (Subp);
12133 -- Prepare subprogram declaration and insert it as an action on the
12134 -- clause node. The visibility for this entity is used to test for
12135 -- visibility of the attribute definition clause (in the sense of
12136 -- 8.3(23) as amended by AI-195).
12138 if not Defer_Declaration then
12140 Make_Subprogram_Declaration (Loc,
12141 Specification => Build_Spec);
12143 -- For a tagged type, there is always a visible declaration for each
12144 -- stream TSS (it is a predefined primitive operation), and the
12145 -- completion of this declaration occurs at the freeze point, which is
12146 -- not always visible at places where the attribute definition clause is
12147 -- visible. So, we create a dummy entity here for the purpose of
12148 -- tracking the visibility of the attribute definition clause itself.
12152 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
12154 Make_Object_Declaration (Loc,
12155 Defining_Identifier => Subp_Id,
12156 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
12159 if not Defer_Declaration
12160 and then From_Aspect_Specification (N)
12161 and then Has_Delayed_Freeze (Ent)
12163 Append_Freeze_Action (Ent, Subp_Decl);
12166 Insert_Action (N, Subp_Decl);
12167 Set_Entity (N, Subp_Id);
12171 Make_Subprogram_Renaming_Declaration (Loc,
12172 Specification => Build_Spec,
12173 Name => New_Occurrence_Of (Subp, Loc));
12175 if Defer_Declaration then
12176 Set_TSS (Base_Type (Ent), Subp_Id);
12179 if From_Aspect_Specification (N) then
12180 Append_Freeze_Action (Ent, Subp_Decl);
12182 Insert_Action (N, Subp_Decl);
12185 Copy_TSS (Subp_Id, Base_Type (Ent));
12187 end New_Stream_Subprogram;
12189 ------------------------------------------
12190 -- Push_Scope_And_Install_Discriminants --
12191 ------------------------------------------
12193 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12195 if Has_Discriminants (E) then
12198 -- Make the discriminants visible for type declarations and protected
12199 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12201 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12202 Install_Discriminants (E);
12205 end Push_Scope_And_Install_Discriminants;
12207 ------------------------
12208 -- Rep_Item_Too_Early --
12209 ------------------------
12211 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12213 -- Cannot apply non-operational rep items to generic types
12215 if Is_Operational_Item (N) then
12219 and then Is_Generic_Type (Root_Type (T))
12220 and then (Nkind (N) /= N_Pragma
12221 or else Get_Pragma_Id (N) /= Pragma_Convention)
12223 Error_Msg_N ("representation item not allowed for generic type", N);
12227 -- Otherwise check for incomplete type
12229 if Is_Incomplete_Or_Private_Type (T)
12230 and then No (Underlying_Type (T))
12232 (Nkind (N) /= N_Pragma
12233 or else Get_Pragma_Id (N) /= Pragma_Import)
12236 ("representation item must be after full type declaration", N);
12239 -- If the type has incomplete components, a representation clause is
12240 -- illegal but stream attributes and Convention pragmas are correct.
12242 elsif Has_Private_Component (T) then
12243 if Nkind (N) = N_Pragma then
12248 ("representation item must appear after type is fully defined",
12255 end Rep_Item_Too_Early;
12257 -----------------------
12258 -- Rep_Item_Too_Late --
12259 -----------------------
12261 function Rep_Item_Too_Late
12264 FOnly : Boolean := False) return Boolean
12267 Parent_Type : Entity_Id;
12269 procedure No_Type_Rep_Item;
12270 -- Output message indicating that no type-related aspects can be
12271 -- specified due to some property of the parent type.
12273 procedure Too_Late;
12274 -- Output message for an aspect being specified too late
12276 -- Note that neither of the above errors is considered a serious one,
12277 -- since the effect is simply that we ignore the representation clause
12279 -- Is this really true? In any case if we make this change we must
12280 -- document the requirement in the spec of Rep_Item_Too_Late that
12281 -- if True is returned, then the rep item must be completely ignored???
12283 ----------------------
12284 -- No_Type_Rep_Item --
12285 ----------------------
12287 procedure No_Type_Rep_Item is
12289 Error_Msg_N ("|type-related representation item not permitted!", N);
12290 end No_Type_Rep_Item;
12296 procedure Too_Late is
12298 -- Other compilers seem more relaxed about rep items appearing too
12299 -- late. Since analysis tools typically don't care about rep items
12300 -- anyway, no reason to be too strict about this.
12302 if not Relaxed_RM_Semantics then
12303 Error_Msg_N ("|representation item appears too late!", N);
12307 -- Start of processing for Rep_Item_Too_Late
12310 -- First make sure entity is not frozen (RM 13.1(9))
12314 -- Exclude imported types, which may be frozen if they appear in a
12315 -- representation clause for a local type.
12317 and then not From_Limited_With (T)
12319 -- Exclude generated entities (not coming from source). The common
12320 -- case is when we generate a renaming which prematurely freezes the
12321 -- renamed internal entity, but we still want to be able to set copies
12322 -- of attribute values such as Size/Alignment.
12324 and then Comes_From_Source (T)
12326 -- A self-referential aspect is illegal if it forces freezing the
12327 -- entity before the corresponding pragma has been analyzed.
12329 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12330 and then From_Aspect_Specification (N)
12333 ("aspect specification causes premature freezing of&", T, N);
12334 Set_Has_Delayed_Freeze (T, False);
12339 S := First_Subtype (T);
12341 if Present (Freeze_Node (S)) then
12342 if not Relaxed_RM_Semantics then
12344 ("??no more representation items for }", Freeze_Node (S), S);
12350 -- Check for case of untagged derived type whose parent either has
12351 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12352 -- this case we do not output a Too_Late message, since there is no
12353 -- earlier point where the rep item could be placed to make it legal.
12357 and then Is_Derived_Type (T)
12358 and then not Is_Tagged_Type (T)
12360 Parent_Type := Etype (Base_Type (T));
12362 if Has_Primitive_Operations (Parent_Type) then
12365 if not Relaxed_RM_Semantics then
12367 ("\parent type & has primitive operations!", N, Parent_Type);
12372 elsif Is_By_Reference_Type (Parent_Type) then
12375 if not Relaxed_RM_Semantics then
12377 ("\parent type & is a by reference type!", N, Parent_Type);
12384 -- No error, but one more warning to consider. The RM (surprisingly)
12385 -- allows this pattern:
12388 -- primitive operations for S
12389 -- type R is new S;
12390 -- rep clause for S
12392 -- Meaning that calls on the primitive operations of S for values of
12393 -- type R may require possibly expensive implicit conversion operations.
12394 -- This is not an error, but is worth a warning.
12396 if not Relaxed_RM_Semantics and then Is_Type (T) then
12398 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12402 and then Has_Primitive_Operations (Base_Type (T))
12404 -- For now, do not generate this warning for the case of aspect
12405 -- specification using Ada 2012 syntax, since we get wrong
12406 -- messages we do not understand. The whole business of derived
12407 -- types and rep items seems a bit confused when aspects are
12408 -- used, since the aspects are not evaluated till freeze time.
12410 and then not From_Aspect_Specification (N)
12412 Error_Msg_Sloc := Sloc (DTL);
12414 ("representation item for& appears after derived type "
12415 & "declaration#??", N);
12417 ("\may result in implicit conversions for primitive "
12418 & "operations of&??", N, T);
12420 ("\to change representations when called with arguments "
12421 & "of type&??", N, DTL);
12426 -- No error, link item into head of chain of rep items for the entity,
12427 -- but avoid chaining if we have an overloadable entity, and the pragma
12428 -- is one that can apply to multiple overloaded entities.
12430 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12432 Pname : constant Name_Id := Pragma_Name (N);
12434 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12435 Name_External, Name_Interface)
12442 Record_Rep_Item (T, N);
12444 end Rep_Item_Too_Late;
12446 -------------------------------------
12447 -- Replace_Type_References_Generic --
12448 -------------------------------------
12450 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12451 TName : constant Name_Id := Chars (T);
12453 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
12454 -- Processes a single node in the traversal procedure below, checking
12455 -- if node N should be replaced, and if so, doing the replacement.
12457 function Visible_Component (Comp : Name_Id) return Entity_Id;
12458 -- Given an identifier in the expression, check whether there is a
12459 -- discriminant or component of the type that is directy visible, and
12460 -- rewrite it as the corresponding selected component of the formal of
12461 -- the subprogram. The entity is located by a sequential search, which
12462 -- seems acceptable given the typical size of component lists and check
12463 -- expressions. Possible optimization ???
12465 ----------------------
12466 -- Replace_Type_Ref --
12467 ----------------------
12469 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
12470 Loc : constant Source_Ptr := Sloc (N);
12472 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
12473 -- Add the proper prefix to a reference to a component of the type
12474 -- when it is not already a selected component.
12480 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12483 Make_Selected_Component (Loc,
12484 Prefix => New_Occurrence_Of (T, Loc),
12485 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12486 Replace_Type_Reference (Prefix (Ref));
12495 -- Start of processing for Replace_Type_Ref
12498 if Nkind (N) = N_Identifier then
12500 -- If not the type name, check whether it is a reference to some
12501 -- other type, which must be frozen before the predicate function
12502 -- is analyzed, i.e. before the freeze node of the type to which
12503 -- the predicate applies.
12505 if Chars (N) /= TName then
12506 if Present (Current_Entity (N))
12507 and then Is_Type (Current_Entity (N))
12509 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12512 -- The components of the type are directly visible and can
12513 -- be referenced without a prefix.
12515 if Nkind (Parent (N)) = N_Selected_Component then
12518 -- In expression C (I), C may be a directly visible function
12519 -- or a visible component that has an array type. Disambiguate
12520 -- by examining the component type.
12522 elsif Nkind (Parent (N)) = N_Indexed_Component
12523 and then N = Prefix (Parent (N))
12525 Comp := Visible_Component (Chars (N));
12527 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
12528 Add_Prefix (N, Comp);
12532 Comp := Visible_Component (Chars (N));
12534 if Present (Comp) then
12535 Add_Prefix (N, Comp);
12541 -- Otherwise do the replacement and we are done with this node
12544 Replace_Type_Reference (N);
12548 -- Case of selected component (which is what a qualification looks
12549 -- like in the unanalyzed tree, which is what we have.
12551 elsif Nkind (N) = N_Selected_Component then
12553 -- If selector name is not our type, keeping going (we might still
12554 -- have an occurrence of the type in the prefix).
12556 if Nkind (Selector_Name (N)) /= N_Identifier
12557 or else Chars (Selector_Name (N)) /= TName
12561 -- Selector name is our type, check qualification
12564 -- Loop through scopes and prefixes, doing comparison
12566 Scop := Current_Scope;
12567 Pref := Prefix (N);
12569 -- Continue if no more scopes or scope with no name
12571 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
12575 -- Do replace if prefix is an identifier matching the scope
12576 -- that we are currently looking at.
12578 if Nkind (Pref) = N_Identifier
12579 and then Chars (Pref) = Chars (Scop)
12581 Replace_Type_Reference (N);
12585 -- Go check scope above us if prefix is itself of the form
12586 -- of a selected component, whose selector matches the scope
12587 -- we are currently looking at.
12589 if Nkind (Pref) = N_Selected_Component
12590 and then Nkind (Selector_Name (Pref)) = N_Identifier
12591 and then Chars (Selector_Name (Pref)) = Chars (Scop)
12593 Scop := Scope (Scop);
12594 Pref := Prefix (Pref);
12596 -- For anything else, we don't have a match, so keep on
12597 -- going, there are still some weird cases where we may
12598 -- still have a replacement within the prefix.
12606 -- Continue for any other node kind
12611 end Replace_Type_Ref;
12613 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
12615 -----------------------
12616 -- Visible_Component --
12617 -----------------------
12619 function Visible_Component (Comp : Name_Id) return Entity_Id is
12623 if Ekind (T) /= E_Record_Type then
12627 E := First_Entity (T);
12628 while Present (E) loop
12629 if Comes_From_Source (E) and then Chars (E) = Comp then
12638 end Visible_Component;
12640 -- Start of processing for Replace_Type_References_Generic
12643 Replace_Type_Refs (N);
12644 end Replace_Type_References_Generic;
12646 --------------------------------
12647 -- Resolve_Aspect_Expressions --
12648 --------------------------------
12650 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12655 function Resolve_Name (N : Node_Id) return Traverse_Result;
12656 -- Verify that all identifiers in the expression, with the exception
12657 -- of references to the current entity, denote visible entities. This
12658 -- is done only to detect visibility errors, as the expression will be
12659 -- properly analyzed/expanded during analysis of the predicate function
12660 -- body. We omit quantified expressions from this test, given that they
12661 -- introduce a local identifier that would require proper expansion to
12662 -- handle properly.
12668 function Resolve_Name (N : Node_Id) return Traverse_Result is
12670 if Nkind (N) = N_Selected_Component then
12671 if Nkind (Prefix (N)) = N_Identifier
12672 and then Chars (Prefix (N)) /= Chars (E)
12674 Find_Selected_Component (N);
12679 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
12680 Find_Direct_Name (N);
12681 Set_Entity (N, Empty);
12683 elsif Nkind (N) = N_Quantified_Expression then
12690 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12692 -- Start of processing for Resolve_Aspect_Expressions
12695 ASN := First_Rep_Item (E);
12696 while Present (ASN) loop
12697 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12698 A_Id := Get_Aspect_Id (ASN);
12699 Expr := Expression (ASN);
12703 -- For now we only deal with aspects that do not generate
12704 -- subprograms, or that may mention current instances of
12705 -- types. These will require special handling (???TBD).
12707 when Aspect_Predicate |
12708 Aspect_Predicate_Failure |
12709 Aspect_Invariant =>
12712 when Aspect_Dynamic_Predicate |
12713 Aspect_Static_Predicate =>
12715 -- Build predicate function specification and preanalyze
12716 -- expression after type replacement.
12718 if No (Predicate_Function (E)) then
12720 FDecl : constant Node_Id :=
12721 Build_Predicate_Function_Declaration (E);
12722 pragma Unreferenced (FDecl);
12724 Resolve_Aspect_Expression (Expr);
12728 when Pre_Post_Aspects =>
12731 when Aspect_Iterable =>
12732 if Nkind (Expr) = N_Aggregate then
12737 Assoc := First (Component_Associations (Expr));
12738 while Present (Assoc) loop
12739 Find_Direct_Name (Expression (Assoc));
12746 if Present (Expr) then
12747 case Aspect_Argument (A_Id) is
12748 when Expression | Optional_Expression =>
12749 Analyze_And_Resolve (Expression (ASN));
12751 when Name | Optional_Name =>
12752 if Nkind (Expr) = N_Identifier then
12753 Find_Direct_Name (Expr);
12755 elsif Nkind (Expr) = N_Selected_Component then
12756 Find_Selected_Component (Expr);
12766 ASN := Next_Rep_Item (ASN);
12768 end Resolve_Aspect_Expressions;
12770 -------------------------
12771 -- Same_Representation --
12772 -------------------------
12774 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12775 T1 : constant Entity_Id := Underlying_Type (Typ1);
12776 T2 : constant Entity_Id := Underlying_Type (Typ2);
12779 -- A quick check, if base types are the same, then we definitely have
12780 -- the same representation, because the subtype specific representation
12781 -- attributes (Size and Alignment) do not affect representation from
12782 -- the point of view of this test.
12784 if Base_Type (T1) = Base_Type (T2) then
12787 elsif Is_Private_Type (Base_Type (T2))
12788 and then Base_Type (T1) = Full_View (Base_Type (T2))
12793 -- Tagged types never have differing representations
12795 if Is_Tagged_Type (T1) then
12799 -- Representations are definitely different if conventions differ
12801 if Convention (T1) /= Convention (T2) then
12805 -- Representations are different if component alignments or scalar
12806 -- storage orders differ.
12808 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
12810 (Is_Record_Type (T2) or else Is_Array_Type (T2))
12812 (Component_Alignment (T1) /= Component_Alignment (T2)
12813 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
12818 -- For arrays, the only real issue is component size. If we know the
12819 -- component size for both arrays, and it is the same, then that's
12820 -- good enough to know we don't have a change of representation.
12822 if Is_Array_Type (T1) then
12823 if Known_Component_Size (T1)
12824 and then Known_Component_Size (T2)
12825 and then Component_Size (T1) = Component_Size (T2)
12831 -- Types definitely have same representation if neither has non-standard
12832 -- representation since default representations are always consistent.
12833 -- If only one has non-standard representation, and the other does not,
12834 -- then we consider that they do not have the same representation. They
12835 -- might, but there is no way of telling early enough.
12837 if Has_Non_Standard_Rep (T1) then
12838 if not Has_Non_Standard_Rep (T2) then
12842 return not Has_Non_Standard_Rep (T2);
12845 -- Here the two types both have non-standard representation, and we need
12846 -- to determine if they have the same non-standard representation.
12848 -- For arrays, we simply need to test if the component sizes are the
12849 -- same. Pragma Pack is reflected in modified component sizes, so this
12850 -- check also deals with pragma Pack.
12852 if Is_Array_Type (T1) then
12853 return Component_Size (T1) = Component_Size (T2);
12855 -- Tagged types always have the same representation, because it is not
12856 -- possible to specify different representations for common fields.
12858 elsif Is_Tagged_Type (T1) then
12861 -- Case of record types
12863 elsif Is_Record_Type (T1) then
12865 -- Packed status must conform
12867 if Is_Packed (T1) /= Is_Packed (T2) then
12870 -- Otherwise we must check components. Typ2 maybe a constrained
12871 -- subtype with fewer components, so we compare the components
12872 -- of the base types.
12875 Record_Case : declare
12876 CD1, CD2 : Entity_Id;
12878 function Same_Rep return Boolean;
12879 -- CD1 and CD2 are either components or discriminants. This
12880 -- function tests whether they have the same representation.
12886 function Same_Rep return Boolean is
12888 if No (Component_Clause (CD1)) then
12889 return No (Component_Clause (CD2));
12891 -- Note: at this point, component clauses have been
12892 -- normalized to the default bit order, so that the
12893 -- comparison of Component_Bit_Offsets is meaningful.
12896 Present (Component_Clause (CD2))
12898 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12900 Esize (CD1) = Esize (CD2);
12904 -- Start of processing for Record_Case
12907 if Has_Discriminants (T1) then
12909 -- The number of discriminants may be different if the
12910 -- derived type has fewer (constrained by values). The
12911 -- invisible discriminants retain the representation of
12912 -- the original, so the discrepancy does not per se
12913 -- indicate a different representation.
12915 CD1 := First_Discriminant (T1);
12916 CD2 := First_Discriminant (T2);
12917 while Present (CD1) and then Present (CD2) loop
12918 if not Same_Rep then
12921 Next_Discriminant (CD1);
12922 Next_Discriminant (CD2);
12927 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12928 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
12929 while Present (CD1) loop
12930 if not Same_Rep then
12933 Next_Component (CD1);
12934 Next_Component (CD2);
12942 -- For enumeration types, we must check each literal to see if the
12943 -- representation is the same. Note that we do not permit enumeration
12944 -- representation clauses for Character and Wide_Character, so these
12945 -- cases were already dealt with.
12947 elsif Is_Enumeration_Type (T1) then
12948 Enumeration_Case : declare
12949 L1, L2 : Entity_Id;
12952 L1 := First_Literal (T1);
12953 L2 := First_Literal (T2);
12954 while Present (L1) loop
12955 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12964 end Enumeration_Case;
12966 -- Any other types have the same representation for these purposes
12971 end Same_Representation;
12973 --------------------------------
12974 -- Resolve_Iterable_Operation --
12975 --------------------------------
12977 procedure Resolve_Iterable_Operation
12979 Cursor : Entity_Id;
12988 if not Is_Overloaded (N) then
12989 if not Is_Entity_Name (N)
12990 or else Ekind (Entity (N)) /= E_Function
12991 or else Scope (Entity (N)) /= Scope (Typ)
12992 or else No (First_Formal (Entity (N)))
12993 or else Etype (First_Formal (Entity (N))) /= Typ
12995 Error_Msg_N ("iterable primitive must be local function name "
12996 & "whose first formal is an iterable type", N);
13001 F1 := First_Formal (Ent);
13002 if Nam = Name_First then
13004 -- First (Container) => Cursor
13006 if Etype (Ent) /= Cursor then
13007 Error_Msg_N ("primitive for First must yield a curosr", N);
13010 elsif Nam = Name_Next then
13012 -- Next (Container, Cursor) => Cursor
13014 F2 := Next_Formal (F1);
13016 if Etype (F2) /= Cursor
13017 or else Etype (Ent) /= Cursor
13018 or else Present (Next_Formal (F2))
13020 Error_Msg_N ("no match for Next iterable primitive", N);
13023 elsif Nam = Name_Has_Element then
13025 -- Has_Element (Container, Cursor) => Boolean
13027 F2 := Next_Formal (F1);
13028 if Etype (F2) /= Cursor
13029 or else Etype (Ent) /= Standard_Boolean
13030 or else Present (Next_Formal (F2))
13032 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13035 elsif Nam = Name_Element then
13036 F2 := Next_Formal (F1);
13039 or else Etype (F2) /= Cursor
13040 or else Present (Next_Formal (F2))
13042 Error_Msg_N ("no match for Element iterable primitive", N);
13047 raise Program_Error;
13051 -- Overloaded case: find subprogram with proper signature.
13052 -- Caller will report error if no match is found.
13059 Get_First_Interp (N, I, It);
13060 while Present (It.Typ) loop
13061 if Ekind (It.Nam) = E_Function
13062 and then Scope (It.Nam) = Scope (Typ)
13063 and then Etype (First_Formal (It.Nam)) = Typ
13065 F1 := First_Formal (It.Nam);
13067 if Nam = Name_First then
13068 if Etype (It.Nam) = Cursor
13069 and then No (Next_Formal (F1))
13071 Set_Entity (N, It.Nam);
13075 elsif Nam = Name_Next then
13076 F2 := Next_Formal (F1);
13079 and then No (Next_Formal (F2))
13080 and then Etype (F2) = Cursor
13081 and then Etype (It.Nam) = Cursor
13083 Set_Entity (N, It.Nam);
13087 elsif Nam = Name_Has_Element then
13088 F2 := Next_Formal (F1);
13091 and then No (Next_Formal (F2))
13092 and then Etype (F2) = Cursor
13093 and then Etype (It.Nam) = Standard_Boolean
13095 Set_Entity (N, It.Nam);
13096 F2 := Next_Formal (F1);
13100 elsif Nam = Name_Element then
13101 F2 := Next_Formal (F1);
13104 and then No (Next_Formal (F2))
13105 and then Etype (F2) = Cursor
13107 Set_Entity (N, It.Nam);
13113 Get_Next_Interp (I, It);
13117 end Resolve_Iterable_Operation;
13123 procedure Set_Biased
13127 Biased : Boolean := True)
13131 Set_Has_Biased_Representation (E);
13133 if Warn_On_Biased_Representation then
13135 ("?B?" & Msg & " forces biased representation for&", N, E);
13140 --------------------
13141 -- Set_Enum_Esize --
13142 --------------------
13144 procedure Set_Enum_Esize (T : Entity_Id) is
13150 Init_Alignment (T);
13152 -- Find the minimum standard size (8,16,32,64) that fits
13154 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13155 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13158 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
13159 Sz := Standard_Character_Size; -- May be > 8 on some targets
13161 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13164 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13167 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13172 if Hi < Uint_2**08 then
13173 Sz := Standard_Character_Size; -- May be > 8 on some targets
13175 elsif Hi < Uint_2**16 then
13178 elsif Hi < Uint_2**32 then
13181 else pragma Assert (Hi < Uint_2**63);
13186 -- That minimum is the proper size unless we have a foreign convention
13187 -- and the size required is 32 or less, in which case we bump the size
13188 -- up to 32. This is required for C and C++ and seems reasonable for
13189 -- all other foreign conventions.
13191 if Has_Foreign_Convention (T)
13192 and then Esize (T) < Standard_Integer_Size
13194 -- Don't do this if Short_Enums on target
13196 and then not Target_Short_Enums
13198 Init_Esize (T, Standard_Integer_Size);
13200 Init_Esize (T, Sz);
13202 end Set_Enum_Esize;
13204 -----------------------------
13205 -- Uninstall_Discriminants --
13206 -----------------------------
13208 procedure Uninstall_Discriminants (E : Entity_Id) is
13214 -- Discriminants have been made visible for type declarations and
13215 -- protected type declarations, not for subtype declarations.
13217 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13218 Disc := First_Discriminant (E);
13219 while Present (Disc) loop
13220 if Disc /= Current_Entity (Disc) then
13221 Prev := Current_Entity (Disc);
13222 while Present (Prev)
13223 and then Present (Homonym (Prev))
13224 and then Homonym (Prev) /= Disc
13226 Prev := Homonym (Prev);
13232 Set_Is_Immediately_Visible (Disc, False);
13234 Outer := Homonym (Disc);
13235 while Present (Outer) and then Scope (Outer) = E loop
13236 Outer := Homonym (Outer);
13239 -- Reset homonym link of other entities, but do not modify link
13240 -- between entities in current scope, so that the back end can
13241 -- have a proper count of local overloadings.
13244 Set_Name_Entity_Id (Chars (Disc), Outer);
13246 elsif Scope (Prev) /= Scope (Disc) then
13247 Set_Homonym (Prev, Outer);
13250 Next_Discriminant (Disc);
13253 end Uninstall_Discriminants;
13255 -------------------------------------------
13256 -- Uninstall_Discriminants_And_Pop_Scope --
13257 -------------------------------------------
13259 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13261 if Has_Discriminants (E) then
13262 Uninstall_Discriminants (E);
13265 end Uninstall_Discriminants_And_Pop_Scope;
13267 ------------------------------
13268 -- Validate_Address_Clauses --
13269 ------------------------------
13271 procedure Validate_Address_Clauses is
13272 function Offset_Value (Expr : Node_Id) return Uint;
13273 -- Given an Address attribute reference, return the value in bits of its
13274 -- offset from the first bit of the underlying entity, or 0 if it is not
13275 -- known at compile time.
13281 function Offset_Value (Expr : Node_Id) return Uint is
13282 N : Node_Id := Prefix (Expr);
13284 Val : Uint := Uint_0;
13287 -- Climb the prefix chain and compute the cumulative offset
13290 if Is_Entity_Name (N) then
13293 elsif Nkind (N) = N_Selected_Component then
13294 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13295 if Off /= No_Uint and then Off >= Uint_0 then
13302 elsif Nkind (N) = N_Indexed_Component then
13303 Off := Indexed_Component_Bit_Offset (N);
13304 if Off /= No_Uint then
13317 -- Start of processing for Validate_Address_Clauses
13320 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13322 ACCR : Address_Clause_Check_Record
13323 renames Address_Clause_Checks.Table (J);
13327 X_Alignment : Uint;
13328 Y_Alignment : Uint;
13336 -- Skip processing of this entry if warning already posted
13338 if not Address_Warning_Posted (ACCR.N) then
13339 Expr := Original_Node (Expression (ACCR.N));
13341 -- Get alignments, sizes and offset, if any
13343 X_Alignment := Alignment (ACCR.X);
13344 X_Size := Esize (ACCR.X);
13346 if Present (ACCR.Y) then
13347 Y_Alignment := Alignment (ACCR.Y);
13348 Y_Size := Esize (ACCR.Y);
13352 and then Nkind (Expr) = N_Attribute_Reference
13353 and then Attribute_Name (Expr) = Name_Address
13355 X_Offs := Offset_Value (Expr);
13360 -- Check for known value not multiple of alignment
13362 if No (ACCR.Y) then
13363 if not Alignment_Checks_Suppressed (ACCR.X)
13364 and then X_Alignment /= 0
13365 and then ACCR.A mod X_Alignment /= 0
13368 ("??specified address for& is inconsistent with "
13369 & "alignment", ACCR.N, ACCR.X);
13371 ("\??program execution may be erroneous (RM 13.3(27))",
13374 Error_Msg_Uint_1 := X_Alignment;
13375 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13378 -- Check for large object overlaying smaller one
13380 elsif Y_Size > Uint_0
13381 and then X_Size > Uint_0
13382 and then X_Offs + X_Size > Y_Size
13384 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
13386 ("\??program execution may be erroneous", ACCR.N);
13388 Error_Msg_Uint_1 := X_Size;
13389 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13391 Error_Msg_Uint_1 := Y_Size;
13392 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
13394 if Y_Size >= X_Size then
13395 Error_Msg_Uint_1 := X_Offs;
13396 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
13399 -- Check for inadequate alignment, both of the base object
13400 -- and of the offset, if any. We only do this check if the
13401 -- run-time Alignment_Check is active. No point in warning
13402 -- if this check has been suppressed (or is suppressed by
13403 -- default in the non-strict alignment machine case).
13405 -- Note: we do not check the alignment if we gave a size
13406 -- warning, since it would likely be redundant.
13408 elsif not Alignment_Checks_Suppressed (ACCR.X)
13409 and then Y_Alignment /= Uint_0
13411 (Y_Alignment < X_Alignment
13414 and then Nkind (Expr) = N_Attribute_Reference
13415 and then Attribute_Name (Expr) = Name_Address
13416 and then Has_Compatible_Alignment
13417 (ACCR.X, Prefix (Expr), True) /=
13421 ("??specified address for& may be inconsistent with "
13422 & "alignment", ACCR.N, ACCR.X);
13424 ("\??program execution may be erroneous (RM 13.3(27))",
13427 Error_Msg_Uint_1 := X_Alignment;
13428 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13430 Error_Msg_Uint_1 := Y_Alignment;
13431 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13433 if Y_Alignment >= X_Alignment then
13435 ("\??but offset is not multiple of alignment", ACCR.N);
13441 end Validate_Address_Clauses;
13443 -----------------------------------------
13444 -- Validate_Compile_Time_Warning_Error --
13445 -----------------------------------------
13447 procedure Validate_Compile_Time_Warning_Error (N : Node_Id) is
13449 Compile_Time_Warnings_Errors.Append
13450 (New_Val => CTWE_Entry'(Eloc => Sloc (N),
13451 Scope => Current_Scope,
13453 end Validate_Compile_Time_Warning_Error;
13455 ------------------------------------------
13456 -- Validate_Compile_Time_Warning_Errors --
13457 ------------------------------------------
13459 procedure Validate_Compile_Time_Warning_Errors is
13460 procedure Set_Scope (S : Entity_Id);
13461 -- Install all enclosing scopes of S along with S itself
13463 procedure Unset_Scope (S : Entity_Id);
13464 -- Uninstall all enclosing scopes of S along with S itself
13470 procedure Set_Scope (S : Entity_Id) is
13472 if S /= Standard_Standard then
13473 Set_Scope (Scope (S));
13483 procedure Unset_Scope (S : Entity_Id) is
13485 if S /= Standard_Standard then
13486 Unset_Scope (Scope (S));
13492 -- Start of processing for Validate_Compile_Time_Warning_Errors
13495 Expander_Mode_Save_And_Set (False);
13496 In_Compile_Time_Warning_Or_Error := True;
13498 for N in Compile_Time_Warnings_Errors.First ..
13499 Compile_Time_Warnings_Errors.Last
13502 T : CTWE_Entry renames Compile_Time_Warnings_Errors.Table (N);
13505 Set_Scope (T.Scope);
13506 Reset_Analyzed_Flags (T.Prag);
13507 Process_Compile_Time_Warning_Or_Error (T.Prag, T.Eloc);
13508 Unset_Scope (T.Scope);
13512 In_Compile_Time_Warning_Or_Error := False;
13513 Expander_Mode_Restore;
13514 end Validate_Compile_Time_Warning_Errors;
13516 ---------------------------
13517 -- Validate_Independence --
13518 ---------------------------
13520 procedure Validate_Independence is
13521 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13529 procedure Check_Array_Type (Atyp : Entity_Id);
13530 -- Checks if the array type Atyp has independent components, and
13531 -- if not, outputs an appropriate set of error messages.
13533 procedure No_Independence;
13534 -- Output message that independence cannot be guaranteed
13536 function OK_Component (C : Entity_Id) return Boolean;
13537 -- Checks one component to see if it is independently accessible, and
13538 -- if so yields True, otherwise yields False if independent access
13539 -- cannot be guaranteed. This is a conservative routine, it only
13540 -- returns True if it knows for sure, it returns False if it knows
13541 -- there is a problem, or it cannot be sure there is no problem.
13543 procedure Reason_Bad_Component (C : Entity_Id);
13544 -- Outputs continuation message if a reason can be determined for
13545 -- the component C being bad.
13547 ----------------------
13548 -- Check_Array_Type --
13549 ----------------------
13551 procedure Check_Array_Type (Atyp : Entity_Id) is
13552 Ctyp : constant Entity_Id := Component_Type (Atyp);
13555 -- OK if no alignment clause, no pack, and no component size
13557 if not Has_Component_Size_Clause (Atyp)
13558 and then not Has_Alignment_Clause (Atyp)
13559 and then not Is_Packed (Atyp)
13564 -- Case of component size is greater than or equal to 64 and the
13565 -- alignment of the array is at least as large as the alignment
13566 -- of the component. We are definitely OK in this situation.
13568 if Known_Component_Size (Atyp)
13569 and then Component_Size (Atyp) >= 64
13570 and then Known_Alignment (Atyp)
13571 and then Known_Alignment (Ctyp)
13572 and then Alignment (Atyp) >= Alignment (Ctyp)
13577 -- Check actual component size
13579 if not Known_Component_Size (Atyp)
13580 or else not (Addressable (Component_Size (Atyp))
13581 and then Component_Size (Atyp) < 64)
13582 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13586 -- Bad component size, check reason
13588 if Has_Component_Size_Clause (Atyp) then
13589 P := Get_Attribute_Definition_Clause
13590 (Atyp, Attribute_Component_Size);
13592 if Present (P) then
13593 Error_Msg_Sloc := Sloc (P);
13594 Error_Msg_N ("\because of Component_Size clause#", N);
13599 if Is_Packed (Atyp) then
13600 P := Get_Rep_Pragma (Atyp, Name_Pack);
13602 if Present (P) then
13603 Error_Msg_Sloc := Sloc (P);
13604 Error_Msg_N ("\because of pragma Pack#", N);
13609 -- No reason found, just return
13614 -- Array type is OK independence-wise
13617 end Check_Array_Type;
13619 ---------------------
13620 -- No_Independence --
13621 ---------------------
13623 procedure No_Independence is
13625 if Pragma_Name (N) = Name_Independent then
13626 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
13629 ("independent components cannot be guaranteed for&", N, E);
13631 end No_Independence;
13637 function OK_Component (C : Entity_Id) return Boolean is
13638 Rec : constant Entity_Id := Scope (C);
13639 Ctyp : constant Entity_Id := Etype (C);
13642 -- OK if no component clause, no Pack, and no alignment clause
13644 if No (Component_Clause (C))
13645 and then not Is_Packed (Rec)
13646 and then not Has_Alignment_Clause (Rec)
13651 -- Here we look at the actual component layout. A component is
13652 -- addressable if its size is a multiple of the Esize of the
13653 -- component type, and its starting position in the record has
13654 -- appropriate alignment, and the record itself has appropriate
13655 -- alignment to guarantee the component alignment.
13657 -- Make sure sizes are static, always assume the worst for any
13658 -- cases where we cannot check static values.
13660 if not (Known_Static_Esize (C)
13662 Known_Static_Esize (Ctyp))
13667 -- Size of component must be addressable or greater than 64 bits
13668 -- and a multiple of bytes.
13670 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
13674 -- Check size is proper multiple
13676 if Esize (C) mod Esize (Ctyp) /= 0 then
13680 -- Check alignment of component is OK
13682 if not Known_Component_Bit_Offset (C)
13683 or else Component_Bit_Offset (C) < Uint_0
13684 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13689 -- Check alignment of record type is OK
13691 if not Known_Alignment (Rec)
13692 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13697 -- All tests passed, component is addressable
13702 --------------------------
13703 -- Reason_Bad_Component --
13704 --------------------------
13706 procedure Reason_Bad_Component (C : Entity_Id) is
13707 Rec : constant Entity_Id := Scope (C);
13708 Ctyp : constant Entity_Id := Etype (C);
13711 -- If component clause present assume that's the problem
13713 if Present (Component_Clause (C)) then
13714 Error_Msg_Sloc := Sloc (Component_Clause (C));
13715 Error_Msg_N ("\because of Component_Clause#", N);
13719 -- If pragma Pack clause present, assume that's the problem
13721 if Is_Packed (Rec) then
13722 P := Get_Rep_Pragma (Rec, Name_Pack);
13724 if Present (P) then
13725 Error_Msg_Sloc := Sloc (P);
13726 Error_Msg_N ("\because of pragma Pack#", N);
13731 -- See if record has bad alignment clause
13733 if Has_Alignment_Clause (Rec)
13734 and then Known_Alignment (Rec)
13735 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13737 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13739 if Present (P) then
13740 Error_Msg_Sloc := Sloc (P);
13741 Error_Msg_N ("\because of Alignment clause#", N);
13745 -- Couldn't find a reason, so return without a message
13748 end Reason_Bad_Component;
13750 -- Start of processing for Validate_Independence
13753 for J in Independence_Checks.First .. Independence_Checks.Last loop
13754 N := Independence_Checks.Table (J).N;
13755 E := Independence_Checks.Table (J).E;
13756 IC := Pragma_Name (N) = Name_Independent_Components;
13758 -- Deal with component case
13760 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13761 if not OK_Component (E) then
13763 Reason_Bad_Component (E);
13768 -- Deal with record with Independent_Components
13770 if IC and then Is_Record_Type (E) then
13771 Comp := First_Component_Or_Discriminant (E);
13772 while Present (Comp) loop
13773 if not OK_Component (Comp) then
13775 Reason_Bad_Component (Comp);
13779 Next_Component_Or_Discriminant (Comp);
13783 -- Deal with address clause case
13785 if Is_Object (E) then
13786 Addr := Address_Clause (E);
13788 if Present (Addr) then
13790 Error_Msg_Sloc := Sloc (Addr);
13791 Error_Msg_N ("\because of Address clause#", N);
13796 -- Deal with independent components for array type
13798 if IC and then Is_Array_Type (E) then
13799 Check_Array_Type (E);
13802 -- Deal with independent components for array object
13804 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13805 Check_Array_Type (Etype (E));
13810 end Validate_Independence;
13812 ------------------------------
13813 -- Validate_Iterable_Aspect --
13814 ------------------------------
13816 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
13821 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
13823 First_Id : Entity_Id;
13824 Next_Id : Entity_Id;
13825 Has_Element_Id : Entity_Id;
13826 Element_Id : Entity_Id;
13829 -- If previous error aspect is unusable
13831 if Cursor = Any_Type then
13837 Has_Element_Id := Empty;
13838 Element_Id := Empty;
13840 -- Each expression must resolve to a function with the proper signature
13842 Assoc := First (Component_Associations (Expression (ASN)));
13843 while Present (Assoc) loop
13844 Expr := Expression (Assoc);
13847 Prim := First (Choices (Assoc));
13849 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
13850 Error_Msg_N ("illegal name in association", Prim);
13852 elsif Chars (Prim) = Name_First then
13853 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
13854 First_Id := Entity (Expr);
13856 elsif Chars (Prim) = Name_Next then
13857 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
13858 Next_Id := Entity (Expr);
13860 elsif Chars (Prim) = Name_Has_Element then
13861 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
13862 Has_Element_Id := Entity (Expr);
13864 elsif Chars (Prim) = Name_Element then
13865 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
13866 Element_Id := Entity (Expr);
13869 Error_Msg_N ("invalid name for iterable function", Prim);
13875 if No (First_Id) then
13876 Error_Msg_N ("match for First primitive not found", ASN);
13878 elsif No (Next_Id) then
13879 Error_Msg_N ("match for Next primitive not found", ASN);
13881 elsif No (Has_Element_Id) then
13882 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13884 elsif No (Element_Id) then
13887 end Validate_Iterable_Aspect;
13889 -----------------------------------
13890 -- Validate_Unchecked_Conversion --
13891 -----------------------------------
13893 procedure Validate_Unchecked_Conversion
13895 Act_Unit : Entity_Id)
13897 Source : Entity_Id;
13898 Target : Entity_Id;
13902 -- Obtain source and target types. Note that we call Ancestor_Subtype
13903 -- here because the processing for generic instantiation always makes
13904 -- subtypes, and we want the original frozen actual types.
13906 -- If we are dealing with private types, then do the check on their
13907 -- fully declared counterparts if the full declarations have been
13908 -- encountered (they don't have to be visible, but they must exist).
13910 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13912 if Is_Private_Type (Source)
13913 and then Present (Underlying_Type (Source))
13915 Source := Underlying_Type (Source);
13918 Target := Ancestor_Subtype (Etype (Act_Unit));
13920 -- If either type is generic, the instantiation happens within a generic
13921 -- unit, and there is nothing to check. The proper check will happen
13922 -- when the enclosing generic is instantiated.
13924 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13928 if Is_Private_Type (Target)
13929 and then Present (Underlying_Type (Target))
13931 Target := Underlying_Type (Target);
13934 -- Source may be unconstrained array, but not target, except in relaxed
13937 if Is_Array_Type (Target)
13938 and then not Is_Constrained (Target)
13939 and then not Relaxed_RM_Semantics
13942 ("unchecked conversion to unconstrained array not allowed", N);
13946 -- Warn if conversion between two different convention pointers
13948 if Is_Access_Type (Target)
13949 and then Is_Access_Type (Source)
13950 and then Convention (Target) /= Convention (Source)
13951 and then Warn_On_Unchecked_Conversion
13953 -- Give warnings for subprogram pointers only on most targets
13955 if Is_Access_Subprogram_Type (Target)
13956 or else Is_Access_Subprogram_Type (Source)
13959 ("?z?conversion between pointers with different conventions!",
13964 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13965 -- warning when compiling GNAT-related sources.
13967 if Warn_On_Unchecked_Conversion
13968 and then not In_Predefined_Unit (N)
13969 and then RTU_Loaded (Ada_Calendar)
13970 and then (Chars (Source) = Name_Time
13972 Chars (Target) = Name_Time)
13974 -- If Ada.Calendar is loaded and the name of one of the operands is
13975 -- Time, there is a good chance that this is Ada.Calendar.Time.
13978 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
13980 pragma Assert (Present (Calendar_Time));
13982 if Source = Calendar_Time or else Target = Calendar_Time then
13984 ("?z?representation of 'Time values may change between "
13985 & "'G'N'A'T versions", N);
13990 -- Make entry in unchecked conversion table for later processing by
13991 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13992 -- (using values set by the back end where possible). This is only done
13993 -- if the appropriate warning is active.
13995 if Warn_On_Unchecked_Conversion then
13996 Unchecked_Conversions.Append
13997 (New_Val => UC_Entry'(Eloc => Sloc (N),
14000 Act_Unit => Act_Unit));
14002 -- If both sizes are known statically now, then back-end annotation
14003 -- is not required to do a proper check but if either size is not
14004 -- known statically, then we need the annotation.
14006 if Known_Static_RM_Size (Source)
14008 Known_Static_RM_Size (Target)
14012 Back_Annotate_Rep_Info := True;
14016 -- If unchecked conversion to access type, and access type is declared
14017 -- in the same unit as the unchecked conversion, then set the flag
14018 -- No_Strict_Aliasing (no strict aliasing is implicit here)
14020 if Is_Access_Type (Target) and then
14021 In_Same_Source_Unit (Target, N)
14023 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14026 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14027 -- the back end needs to perform special validation checks.
14029 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14030 -- have full expansion and the back end is called ???
14033 Make_Validate_Unchecked_Conversion (Sloc (N));
14034 Set_Source_Type (Vnode, Source);
14035 Set_Target_Type (Vnode, Target);
14037 -- If the unchecked conversion node is in a list, just insert before it.
14038 -- If not we have some strange case, not worth bothering about.
14040 if Is_List_Member (N) then
14041 Insert_After (N, Vnode);
14043 end Validate_Unchecked_Conversion;
14045 ------------------------------------
14046 -- Validate_Unchecked_Conversions --
14047 ------------------------------------
14049 procedure Validate_Unchecked_Conversions is
14051 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14053 T : UC_Entry renames Unchecked_Conversions.Table (N);
14055 Act_Unit : constant Entity_Id := T.Act_Unit;
14056 Eloc : constant Source_Ptr := T.Eloc;
14057 Source : constant Entity_Id := T.Source;
14058 Target : constant Entity_Id := T.Target;
14064 -- Skip if function marked as warnings off
14066 if Warnings_Off (Act_Unit) then
14070 -- This validation check, which warns if we have unequal sizes for
14071 -- unchecked conversion, and thus potentially implementation
14072 -- dependent semantics, is one of the few occasions on which we
14073 -- use the official RM size instead of Esize. See description in
14074 -- Einfo "Handling of Type'Size Values" for details.
14076 if Serious_Errors_Detected = 0
14077 and then Known_Static_RM_Size (Source)
14078 and then Known_Static_RM_Size (Target)
14080 -- Don't do the check if warnings off for either type, note the
14081 -- deliberate use of OR here instead of OR ELSE to get the flag
14082 -- Warnings_Off_Used set for both types if appropriate.
14084 and then not (Has_Warnings_Off (Source)
14086 Has_Warnings_Off (Target))
14088 Source_Siz := RM_Size (Source);
14089 Target_Siz := RM_Size (Target);
14091 if Source_Siz /= Target_Siz then
14093 ("?z?types for unchecked conversion have different sizes!",
14096 if All_Errors_Mode then
14097 Error_Msg_Name_1 := Chars (Source);
14098 Error_Msg_Uint_1 := Source_Siz;
14099 Error_Msg_Name_2 := Chars (Target);
14100 Error_Msg_Uint_2 := Target_Siz;
14101 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
14103 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14105 if Is_Discrete_Type (Source)
14107 Is_Discrete_Type (Target)
14109 if Source_Siz > Target_Siz then
14111 ("\?z?^ high order bits of source will "
14112 & "be ignored!", Eloc);
14114 elsif Is_Unsigned_Type (Source) then
14116 ("\?z?source will be extended with ^ high order "
14117 & "zero bits!", Eloc);
14121 ("\?z?source will be extended with ^ high order "
14122 & "sign bits!", Eloc);
14125 elsif Source_Siz < Target_Siz then
14126 if Is_Discrete_Type (Target) then
14127 if Bytes_Big_Endian then
14129 ("\?z?target value will include ^ undefined "
14130 & "low order bits!", Eloc);
14133 ("\?z?target value will include ^ undefined "
14134 & "high order bits!", Eloc);
14139 ("\?z?^ trailing bits of target value will be "
14140 & "undefined!", Eloc);
14143 else pragma Assert (Source_Siz > Target_Siz);
14144 if Is_Discrete_Type (Source) then
14145 if Bytes_Big_Endian then
14147 ("\?z?^ low order bits of source will be "
14148 & "ignored!", Eloc);
14151 ("\?z?^ high order bits of source will be "
14152 & "ignored!", Eloc);
14157 ("\?z?^ trailing bits of source will be "
14158 & "ignored!", Eloc);
14165 -- If both types are access types, we need to check the alignment.
14166 -- If the alignment of both is specified, we can do it here.
14168 if Serious_Errors_Detected = 0
14169 and then Is_Access_Type (Source)
14170 and then Is_Access_Type (Target)
14171 and then Target_Strict_Alignment
14172 and then Present (Designated_Type (Source))
14173 and then Present (Designated_Type (Target))
14176 D_Source : constant Entity_Id := Designated_Type (Source);
14177 D_Target : constant Entity_Id := Designated_Type (Target);
14180 if Known_Alignment (D_Source)
14182 Known_Alignment (D_Target)
14185 Source_Align : constant Uint := Alignment (D_Source);
14186 Target_Align : constant Uint := Alignment (D_Target);
14189 if Source_Align < Target_Align
14190 and then not Is_Tagged_Type (D_Source)
14192 -- Suppress warning if warnings suppressed on either
14193 -- type or either designated type. Note the use of
14194 -- OR here instead of OR ELSE. That is intentional,
14195 -- we would like to set flag Warnings_Off_Used in
14196 -- all types for which warnings are suppressed.
14198 and then not (Has_Warnings_Off (D_Source)
14200 Has_Warnings_Off (D_Target)
14202 Has_Warnings_Off (Source)
14204 Has_Warnings_Off (Target))
14206 Error_Msg_Uint_1 := Target_Align;
14207 Error_Msg_Uint_2 := Source_Align;
14208 Error_Msg_Node_1 := D_Target;
14209 Error_Msg_Node_2 := D_Source;
14211 ("?z?alignment of & (^) is stricter than "
14212 & "alignment of & (^)!", Eloc);
14214 ("\?z?resulting access value may have invalid "
14215 & "alignment!", Eloc);
14226 end Validate_Unchecked_Conversions;