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