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