Make vect_model_store_cost take a vec_load_store_type
[official-gcc.git] / gcc / ada / sem_ch13.adb
blobd2533b01f7eb7e2116353abc589983783d3bf7ae
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-2017, 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 -- pre-analyzed 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_Secondary_Stack_Size
2214 | Aspect_Simple_Storage_Pool
2215 | Aspect_Size
2216 | Aspect_Small
2217 | Aspect_Storage_Pool
2218 | Aspect_Stream_Size
2219 | Aspect_Value_Size
2220 | Aspect_Variable_Indexing
2221 | Aspect_Write
2223 -- Indexing aspects apply only to tagged type
2225 if (A_Id = Aspect_Constant_Indexing
2226 or else
2227 A_Id = Aspect_Variable_Indexing)
2228 and then not (Is_Type (E)
2229 and then Is_Tagged_Type (E))
2230 then
2231 Error_Msg_N
2232 ("indexing aspect can only apply to a tagged type",
2233 Aspect);
2234 goto Continue;
2235 end if;
2237 -- For the case of aspect Address, we don't consider that we
2238 -- know the entity is never set in the source, since it is
2239 -- is likely aliasing is occurring.
2241 -- Note: one might think that the analysis of the resulting
2242 -- attribute definition clause would take care of that, but
2243 -- that's not the case since it won't be from source.
2245 if A_Id = Aspect_Address then
2246 Set_Never_Set_In_Source (E, False);
2247 end if;
2249 -- Correctness of the profile of a stream operation is
2250 -- verified at the freeze point, but we must detect the
2251 -- illegal specification of this aspect for a subtype now,
2252 -- to prevent malformed rep_item chains.
2254 if A_Id = Aspect_Input or else
2255 A_Id = Aspect_Output or else
2256 A_Id = Aspect_Read or else
2257 A_Id = Aspect_Write
2258 then
2259 if not Is_First_Subtype (E) then
2260 Error_Msg_N
2261 ("local name must be a first subtype", Aspect);
2262 goto Continue;
2264 -- If stream aspect applies to the class-wide type,
2265 -- the generated attribute definition applies to the
2266 -- class-wide type as well.
2268 elsif Class_Present (Aspect) then
2269 Ent :=
2270 Make_Attribute_Reference (Loc,
2271 Prefix => Ent,
2272 Attribute_Name => Name_Class);
2273 end if;
2274 end if;
2276 -- Construct the attribute_definition_clause. The expression
2277 -- in the aspect specification is simply shared with the
2278 -- constructed attribute, because it will be fully analyzed
2279 -- when the attribute is processed. However, in ASIS mode
2280 -- the aspect expression itself is preanalyzed and resolved
2281 -- to catch visibility errors that are otherwise caught
2282 -- later, and we create a separate copy of the expression
2283 -- to prevent analysis of a malformed tree (e.g. a function
2284 -- call with parameter associations).
2286 if ASIS_Mode then
2287 Aitem :=
2288 Make_Attribute_Definition_Clause (Loc,
2289 Name => Ent,
2290 Chars => Chars (Id),
2291 Expression => New_Copy_Tree (Expr));
2292 else
2293 Aitem :=
2294 Make_Attribute_Definition_Clause (Loc,
2295 Name => Ent,
2296 Chars => Chars (Id),
2297 Expression => Relocate_Node (Expr));
2298 end if;
2300 -- If the address is specified, then we treat the entity as
2301 -- referenced, to avoid spurious warnings. This is analogous
2302 -- to what is done with an attribute definition clause, but
2303 -- here we don't want to generate a reference because this
2304 -- is the point of definition of the entity.
2306 if A_Id = Aspect_Address then
2307 Set_Referenced (E);
2308 end if;
2310 -- Case 2: Aspects corresponding to pragmas
2312 -- Case 2a: Aspects corresponding to pragmas with two
2313 -- arguments, where the first argument is a local name
2314 -- referring to the entity, and the second argument is the
2315 -- aspect definition expression.
2317 -- Linker_Section/Suppress/Unsuppress
2319 when Aspect_Linker_Section
2320 | Aspect_Suppress
2321 | Aspect_Unsuppress
2323 Make_Aitem_Pragma
2324 (Pragma_Argument_Associations => New_List (
2325 Make_Pragma_Argument_Association (Loc,
2326 Expression => New_Occurrence_Of (E, Loc)),
2327 Make_Pragma_Argument_Association (Sloc (Expr),
2328 Expression => Relocate_Node (Expr))),
2329 Pragma_Name => Chars (Id));
2331 -- Linker_Section does not need delaying, as its argument
2332 -- must be a static string. Furthermore, if applied to
2333 -- an object with an explicit initialization, the object
2334 -- must be frozen in order to elaborate the initialization
2335 -- code. (This is already done for types with implicit
2336 -- initialization, such as protected types.)
2338 if A_Id = Aspect_Linker_Section
2339 and then Nkind (N) = N_Object_Declaration
2340 and then Has_Init_Expression (N)
2341 then
2342 Delay_Required := False;
2343 end if;
2345 -- Synchronization
2347 -- Corresponds to pragma Implemented, construct the pragma
2349 when Aspect_Synchronization =>
2350 Make_Aitem_Pragma
2351 (Pragma_Argument_Associations => New_List (
2352 Make_Pragma_Argument_Association (Loc,
2353 Expression => New_Occurrence_Of (E, Loc)),
2354 Make_Pragma_Argument_Association (Sloc (Expr),
2355 Expression => Relocate_Node (Expr))),
2356 Pragma_Name => Name_Implemented);
2358 -- Attach_Handler
2360 when Aspect_Attach_Handler =>
2361 Make_Aitem_Pragma
2362 (Pragma_Argument_Associations => New_List (
2363 Make_Pragma_Argument_Association (Sloc (Ent),
2364 Expression => Ent),
2365 Make_Pragma_Argument_Association (Sloc (Expr),
2366 Expression => Relocate_Node (Expr))),
2367 Pragma_Name => Name_Attach_Handler);
2369 -- We need to insert this pragma into the tree to get proper
2370 -- processing and to look valid from a placement viewpoint.
2372 Insert_Pragma (Aitem);
2373 goto Continue;
2375 -- Dynamic_Predicate, Predicate, Static_Predicate
2377 when Aspect_Dynamic_Predicate
2378 | Aspect_Predicate
2379 | Aspect_Static_Predicate
2381 -- These aspects apply only to subtypes
2383 if not Is_Type (E) then
2384 Error_Msg_N
2385 ("predicate can only be specified for a subtype",
2386 Aspect);
2387 goto Continue;
2389 elsif Is_Incomplete_Type (E) then
2390 Error_Msg_N
2391 ("predicate cannot apply to incomplete view", Aspect);
2393 elsif Is_Generic_Type (E) then
2394 Error_Msg_N
2395 ("predicate cannot apply to formal type", Aspect);
2396 goto Continue;
2397 end if;
2399 -- Construct the pragma (always a pragma Predicate, with
2400 -- flags recording whether it is static/dynamic). We also
2401 -- set flags recording this in the type itself.
2403 Make_Aitem_Pragma
2404 (Pragma_Argument_Associations => New_List (
2405 Make_Pragma_Argument_Association (Sloc (Ent),
2406 Expression => Ent),
2407 Make_Pragma_Argument_Association (Sloc (Expr),
2408 Expression => Relocate_Node (Expr))),
2409 Pragma_Name => Name_Predicate);
2411 -- Mark type has predicates, and remember what kind of
2412 -- aspect lead to this predicate (we need this to access
2413 -- the right set of check policies later on).
2415 Set_Has_Predicates (E);
2417 if A_Id = Aspect_Dynamic_Predicate then
2418 Set_Has_Dynamic_Predicate_Aspect (E);
2420 -- If the entity has a dynamic predicate, any inherited
2421 -- static predicate becomes dynamic as well, and the
2422 -- predicate function includes the conjunction of both.
2424 Set_Has_Static_Predicate_Aspect (E, False);
2426 elsif A_Id = Aspect_Static_Predicate then
2427 Set_Has_Static_Predicate_Aspect (E);
2428 end if;
2430 -- If the type is private, indicate that its completion
2431 -- has a freeze node, because that is the one that will
2432 -- be visible at freeze time.
2434 if Is_Private_Type (E) and then Present (Full_View (E)) then
2435 Set_Has_Predicates (Full_View (E));
2437 if A_Id = Aspect_Dynamic_Predicate then
2438 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2439 elsif A_Id = Aspect_Static_Predicate then
2440 Set_Has_Static_Predicate_Aspect (Full_View (E));
2441 end if;
2443 Set_Has_Delayed_Aspects (Full_View (E));
2444 Ensure_Freeze_Node (Full_View (E));
2445 end if;
2447 -- Predicate_Failure
2449 when Aspect_Predicate_Failure =>
2451 -- This aspect applies only to subtypes
2453 if not Is_Type (E) then
2454 Error_Msg_N
2455 ("predicate can only be specified for a subtype",
2456 Aspect);
2457 goto Continue;
2459 elsif Is_Incomplete_Type (E) then
2460 Error_Msg_N
2461 ("predicate cannot apply to incomplete view", Aspect);
2462 goto Continue;
2463 end if;
2465 -- Construct the pragma
2467 Make_Aitem_Pragma
2468 (Pragma_Argument_Associations => New_List (
2469 Make_Pragma_Argument_Association (Sloc (Ent),
2470 Expression => Ent),
2471 Make_Pragma_Argument_Association (Sloc (Expr),
2472 Expression => Relocate_Node (Expr))),
2473 Pragma_Name => Name_Predicate_Failure);
2475 Set_Has_Predicates (E);
2477 -- If the type is private, indicate that its completion
2478 -- has a freeze node, because that is the one that will
2479 -- be visible at freeze time.
2481 if Is_Private_Type (E) and then Present (Full_View (E)) then
2482 Set_Has_Predicates (Full_View (E));
2483 Set_Has_Delayed_Aspects (Full_View (E));
2484 Ensure_Freeze_Node (Full_View (E));
2485 end if;
2487 -- Case 2b: Aspects corresponding to pragmas with two
2488 -- arguments, where the second argument is a local name
2489 -- referring to the entity, and the first argument is the
2490 -- aspect definition expression.
2492 -- Convention
2494 when Aspect_Convention =>
2495 Analyze_Aspect_Convention;
2496 goto Continue;
2498 -- External_Name, Link_Name
2500 when Aspect_External_Name
2501 | Aspect_Link_Name
2503 Analyze_Aspect_External_Link_Name;
2504 goto Continue;
2506 -- CPU, Interrupt_Priority, Priority
2508 -- These three aspects can be specified for a subprogram spec
2509 -- or body, in which case we analyze the expression and export
2510 -- the value of the aspect.
2512 -- Previously, we generated an equivalent pragma for bodies
2513 -- (note that the specs cannot contain these pragmas). The
2514 -- pragma was inserted ahead of local declarations, rather than
2515 -- after the body. This leads to a certain duplication between
2516 -- the processing performed for the aspect and the pragma, but
2517 -- given the straightforward handling required it is simpler
2518 -- to duplicate than to translate the aspect in the spec into
2519 -- a pragma in the declarative part of the body.
2521 when Aspect_CPU
2522 | Aspect_Interrupt_Priority
2523 | Aspect_Priority
2525 if Nkind_In (N, N_Subprogram_Body,
2526 N_Subprogram_Declaration)
2527 then
2528 -- Analyze the aspect expression
2530 Analyze_And_Resolve (Expr, Standard_Integer);
2532 -- Interrupt_Priority aspect not allowed for main
2533 -- subprograms. RM D.1 does not forbid this explicitly,
2534 -- but RM J.15.11(6/3) does not permit pragma
2535 -- Interrupt_Priority for subprograms.
2537 if A_Id = Aspect_Interrupt_Priority then
2538 Error_Msg_N
2539 ("Interrupt_Priority aspect cannot apply to "
2540 & "subprogram", Expr);
2542 -- The expression must be static
2544 elsif not Is_OK_Static_Expression (Expr) then
2545 Flag_Non_Static_Expr
2546 ("aspect requires static expression!", Expr);
2548 -- Check whether this is the main subprogram. Issue a
2549 -- warning only if it is obviously not a main program
2550 -- (when it has parameters or when the subprogram is
2551 -- within a package).
2553 elsif Present (Parameter_Specifications
2554 (Specification (N)))
2555 or else not Is_Compilation_Unit (Defining_Entity (N))
2556 then
2557 -- See RM D.1(14/3) and D.16(12/3)
2559 Error_Msg_N
2560 ("aspect applied to subprogram other than the "
2561 & "main subprogram has no effect??", Expr);
2563 -- Otherwise check in range and export the value
2565 -- For the CPU aspect
2567 elsif A_Id = Aspect_CPU then
2568 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2570 -- Value is correct so we export the value to make
2571 -- it available at execution time.
2573 Set_Main_CPU
2574 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2576 else
2577 Error_Msg_N
2578 ("main subprogram CPU is out of range", Expr);
2579 end if;
2581 -- For the Priority aspect
2583 elsif A_Id = Aspect_Priority then
2584 if Is_In_Range (Expr, RTE (RE_Priority)) then
2586 -- Value is correct so we export the value to make
2587 -- it available at execution time.
2589 Set_Main_Priority
2590 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2592 -- Ignore pragma if Relaxed_RM_Semantics to support
2593 -- other targets/non GNAT compilers.
2595 elsif not Relaxed_RM_Semantics then
2596 Error_Msg_N
2597 ("main subprogram priority is out of range",
2598 Expr);
2599 end if;
2600 end if;
2602 -- Load an arbitrary entity from System.Tasking.Stages
2603 -- or System.Tasking.Restricted.Stages (depending on
2604 -- the supported profile) to make sure that one of these
2605 -- packages is implicitly with'ed, since we need to have
2606 -- the tasking run time active for the pragma Priority to
2607 -- have any effect. Previously we with'ed the package
2608 -- System.Tasking, but this package does not trigger the
2609 -- required initialization of the run-time library.
2611 declare
2612 Discard : Entity_Id;
2613 begin
2614 if Restricted_Profile then
2615 Discard := RTE (RE_Activate_Restricted_Tasks);
2616 else
2617 Discard := RTE (RE_Activate_Tasks);
2618 end if;
2619 end;
2621 -- Handling for these aspects in subprograms is complete
2623 goto Continue;
2625 -- For task and protected types pass the aspect as an
2626 -- attribute.
2628 else
2629 Aitem :=
2630 Make_Attribute_Definition_Clause (Loc,
2631 Name => Ent,
2632 Chars => Chars (Id),
2633 Expression => Relocate_Node (Expr));
2634 end if;
2636 -- Warnings
2638 when Aspect_Warnings =>
2639 Make_Aitem_Pragma
2640 (Pragma_Argument_Associations => New_List (
2641 Make_Pragma_Argument_Association (Sloc (Expr),
2642 Expression => Relocate_Node (Expr)),
2643 Make_Pragma_Argument_Association (Loc,
2644 Expression => New_Occurrence_Of (E, Loc))),
2645 Pragma_Name => Chars (Id));
2647 Decorate (Aspect, Aitem);
2648 Insert_Pragma (Aitem);
2649 goto Continue;
2651 -- Case 2c: Aspects corresponding to pragmas with three
2652 -- arguments.
2654 -- Invariant aspects have a first argument that references the
2655 -- entity, a second argument that is the expression and a third
2656 -- argument that is an appropriate message.
2658 -- Invariant, Type_Invariant
2660 when Aspect_Invariant
2661 | Aspect_Type_Invariant
2663 -- Analysis of the pragma will verify placement legality:
2664 -- an invariant must apply to a private type, or appear in
2665 -- the private part of a spec and apply to a completion.
2667 Make_Aitem_Pragma
2668 (Pragma_Argument_Associations => New_List (
2669 Make_Pragma_Argument_Association (Sloc (Ent),
2670 Expression => Ent),
2671 Make_Pragma_Argument_Association (Sloc (Expr),
2672 Expression => Relocate_Node (Expr))),
2673 Pragma_Name => Name_Invariant);
2675 -- Add message unless exception messages are suppressed
2677 if not Opt.Exception_Locations_Suppressed then
2678 Append_To (Pragma_Argument_Associations (Aitem),
2679 Make_Pragma_Argument_Association (Eloc,
2680 Chars => Name_Message,
2681 Expression =>
2682 Make_String_Literal (Eloc,
2683 Strval => "failed invariant from "
2684 & Build_Location_String (Eloc))));
2685 end if;
2687 -- For Invariant case, insert immediately after the entity
2688 -- declaration. We do not have to worry about delay issues
2689 -- since the pragma processing takes care of this.
2691 Delay_Required := False;
2693 -- Case 2d : Aspects that correspond to a pragma with one
2694 -- argument.
2696 -- Abstract_State
2698 -- Aspect Abstract_State introduces implicit declarations for
2699 -- all state abstraction entities it defines. To emulate this
2700 -- behavior, insert the pragma at the beginning of the visible
2701 -- declarations of the related package so that it is analyzed
2702 -- immediately.
2704 when Aspect_Abstract_State => Abstract_State : declare
2705 Context : Node_Id := N;
2707 begin
2708 -- When aspect Abstract_State appears on a generic package,
2709 -- it is propageted to the package instance. The context in
2710 -- this case is the instance spec.
2712 if Nkind (Context) = N_Package_Instantiation then
2713 Context := Instance_Spec (Context);
2714 end if;
2716 if Nkind_In (Context, N_Generic_Package_Declaration,
2717 N_Package_Declaration)
2718 then
2719 Make_Aitem_Pragma
2720 (Pragma_Argument_Associations => New_List (
2721 Make_Pragma_Argument_Association (Loc,
2722 Expression => Relocate_Node (Expr))),
2723 Pragma_Name => Name_Abstract_State);
2725 Decorate (Aspect, Aitem);
2726 Insert_Pragma
2727 (Prag => Aitem,
2728 Is_Instance =>
2729 Is_Generic_Instance (Defining_Entity (Context)));
2731 else
2732 Error_Msg_NE
2733 ("aspect & must apply to a package declaration",
2734 Aspect, Id);
2735 end if;
2737 goto Continue;
2738 end Abstract_State;
2740 -- Aspect Async_Readers is never delayed because it is
2741 -- equivalent to a source pragma which appears after the
2742 -- related object declaration.
2744 when Aspect_Async_Readers =>
2745 Make_Aitem_Pragma
2746 (Pragma_Argument_Associations => New_List (
2747 Make_Pragma_Argument_Association (Loc,
2748 Expression => Relocate_Node (Expr))),
2749 Pragma_Name => Name_Async_Readers);
2751 Decorate (Aspect, Aitem);
2752 Insert_Pragma (Aitem);
2753 goto Continue;
2755 -- Aspect Async_Writers is never delayed because it is
2756 -- equivalent to a source pragma which appears after the
2757 -- related object declaration.
2759 when Aspect_Async_Writers =>
2760 Make_Aitem_Pragma
2761 (Pragma_Argument_Associations => New_List (
2762 Make_Pragma_Argument_Association (Loc,
2763 Expression => Relocate_Node (Expr))),
2764 Pragma_Name => Name_Async_Writers);
2766 Decorate (Aspect, Aitem);
2767 Insert_Pragma (Aitem);
2768 goto Continue;
2770 -- Aspect Constant_After_Elaboration is never delayed because
2771 -- it is equivalent to a source pragma which appears after the
2772 -- related object declaration.
2774 when Aspect_Constant_After_Elaboration =>
2775 Make_Aitem_Pragma
2776 (Pragma_Argument_Associations => New_List (
2777 Make_Pragma_Argument_Association (Loc,
2778 Expression => Relocate_Node (Expr))),
2779 Pragma_Name =>
2780 Name_Constant_After_Elaboration);
2782 Decorate (Aspect, Aitem);
2783 Insert_Pragma (Aitem);
2784 goto Continue;
2786 -- Aspect Default_Internal_Condition is never delayed because
2787 -- it is equivalent to a source pragma which appears after the
2788 -- related private type. To deal with forward references, the
2789 -- generated pragma is stored in the rep chain of the related
2790 -- private type as types do not carry contracts. The pragma is
2791 -- wrapped inside of a procedure at the freeze point of the
2792 -- private type's full view.
2794 when Aspect_Default_Initial_Condition =>
2795 Make_Aitem_Pragma
2796 (Pragma_Argument_Associations => New_List (
2797 Make_Pragma_Argument_Association (Loc,
2798 Expression => Relocate_Node (Expr))),
2799 Pragma_Name =>
2800 Name_Default_Initial_Condition);
2802 Decorate (Aspect, Aitem);
2803 Insert_Pragma (Aitem);
2804 goto Continue;
2806 -- Default_Storage_Pool
2808 when Aspect_Default_Storage_Pool =>
2809 Make_Aitem_Pragma
2810 (Pragma_Argument_Associations => New_List (
2811 Make_Pragma_Argument_Association (Loc,
2812 Expression => Relocate_Node (Expr))),
2813 Pragma_Name =>
2814 Name_Default_Storage_Pool);
2816 Decorate (Aspect, Aitem);
2817 Insert_Pragma (Aitem);
2818 goto Continue;
2820 -- Depends
2822 -- Aspect Depends is never delayed because it is equivalent to
2823 -- a source pragma which appears after the related subprogram.
2824 -- To deal with forward references, the generated pragma is
2825 -- stored in the contract of the related subprogram and later
2826 -- analyzed at the end of the declarative region. See routine
2827 -- Analyze_Depends_In_Decl_Part for details.
2829 when Aspect_Depends =>
2830 Make_Aitem_Pragma
2831 (Pragma_Argument_Associations => New_List (
2832 Make_Pragma_Argument_Association (Loc,
2833 Expression => Relocate_Node (Expr))),
2834 Pragma_Name => Name_Depends);
2836 Decorate (Aspect, Aitem);
2837 Insert_Pragma (Aitem);
2838 goto Continue;
2840 -- Aspect Effecitve_Reads is never delayed because it is
2841 -- equivalent to a source pragma which appears after the
2842 -- related object declaration.
2844 when Aspect_Effective_Reads =>
2845 Make_Aitem_Pragma
2846 (Pragma_Argument_Associations => New_List (
2847 Make_Pragma_Argument_Association (Loc,
2848 Expression => Relocate_Node (Expr))),
2849 Pragma_Name => Name_Effective_Reads);
2851 Decorate (Aspect, Aitem);
2852 Insert_Pragma (Aitem);
2853 goto Continue;
2855 -- Aspect Effective_Writes is never delayed because it is
2856 -- equivalent to a source pragma which appears after the
2857 -- related object declaration.
2859 when Aspect_Effective_Writes =>
2860 Make_Aitem_Pragma
2861 (Pragma_Argument_Associations => New_List (
2862 Make_Pragma_Argument_Association (Loc,
2863 Expression => Relocate_Node (Expr))),
2864 Pragma_Name => Name_Effective_Writes);
2866 Decorate (Aspect, Aitem);
2867 Insert_Pragma (Aitem);
2868 goto Continue;
2870 -- Aspect Extensions_Visible is never delayed because it is
2871 -- equivalent to a source pragma which appears after the
2872 -- related subprogram.
2874 when Aspect_Extensions_Visible =>
2875 Make_Aitem_Pragma
2876 (Pragma_Argument_Associations => New_List (
2877 Make_Pragma_Argument_Association (Loc,
2878 Expression => Relocate_Node (Expr))),
2879 Pragma_Name => Name_Extensions_Visible);
2881 Decorate (Aspect, Aitem);
2882 Insert_Pragma (Aitem);
2883 goto Continue;
2885 -- Aspect Ghost is never delayed because it is equivalent to a
2886 -- source pragma which appears at the top of [generic] package
2887 -- declarations or after an object, a [generic] subprogram, or
2888 -- a type declaration.
2890 when Aspect_Ghost =>
2891 Make_Aitem_Pragma
2892 (Pragma_Argument_Associations => New_List (
2893 Make_Pragma_Argument_Association (Loc,
2894 Expression => Relocate_Node (Expr))),
2895 Pragma_Name => Name_Ghost);
2897 Decorate (Aspect, Aitem);
2898 Insert_Pragma (Aitem);
2899 goto Continue;
2901 -- Global
2903 -- Aspect Global is never delayed because it is equivalent to
2904 -- a source pragma which appears after the related subprogram.
2905 -- To deal with forward references, the generated pragma is
2906 -- stored in the contract of the related subprogram and later
2907 -- analyzed at the end of the declarative region. See routine
2908 -- Analyze_Global_In_Decl_Part for details.
2910 when Aspect_Global =>
2911 Make_Aitem_Pragma
2912 (Pragma_Argument_Associations => New_List (
2913 Make_Pragma_Argument_Association (Loc,
2914 Expression => Relocate_Node (Expr))),
2915 Pragma_Name => Name_Global);
2917 Decorate (Aspect, Aitem);
2918 Insert_Pragma (Aitem);
2919 goto Continue;
2921 -- Initial_Condition
2923 -- Aspect Initial_Condition is never delayed because it is
2924 -- equivalent to a source pragma which appears after the
2925 -- related package. To deal with forward references, the
2926 -- generated pragma is stored in the contract of the related
2927 -- package and later analyzed at the end of the declarative
2928 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2929 -- for details.
2931 when Aspect_Initial_Condition => Initial_Condition : declare
2932 Context : Node_Id := N;
2934 begin
2935 -- When aspect Initial_Condition appears on a generic
2936 -- package, it is propageted to the package instance. The
2937 -- context in this case is the instance spec.
2939 if Nkind (Context) = N_Package_Instantiation then
2940 Context := Instance_Spec (Context);
2941 end if;
2943 if Nkind_In (Context, N_Generic_Package_Declaration,
2944 N_Package_Declaration)
2945 then
2946 Make_Aitem_Pragma
2947 (Pragma_Argument_Associations => New_List (
2948 Make_Pragma_Argument_Association (Loc,
2949 Expression => Relocate_Node (Expr))),
2950 Pragma_Name =>
2951 Name_Initial_Condition);
2953 Decorate (Aspect, Aitem);
2954 Insert_Pragma
2955 (Prag => Aitem,
2956 Is_Instance =>
2957 Is_Generic_Instance (Defining_Entity (Context)));
2959 -- Otherwise the context is illegal
2961 else
2962 Error_Msg_NE
2963 ("aspect & must apply to a package declaration",
2964 Aspect, Id);
2965 end if;
2967 goto Continue;
2968 end Initial_Condition;
2970 -- Initializes
2972 -- Aspect Initializes is never delayed because it is equivalent
2973 -- to a source pragma appearing after the related package. To
2974 -- deal with forward references, the generated pragma is stored
2975 -- in the contract of the related package and later analyzed at
2976 -- the end of the declarative region. For details, see routine
2977 -- Analyze_Initializes_In_Decl_Part.
2979 when Aspect_Initializes => Initializes : declare
2980 Context : Node_Id := N;
2982 begin
2983 -- When aspect Initializes appears on a generic package,
2984 -- it is propageted to the package instance. The context
2985 -- in this case is the instance spec.
2987 if Nkind (Context) = N_Package_Instantiation then
2988 Context := Instance_Spec (Context);
2989 end if;
2991 if Nkind_In (Context, N_Generic_Package_Declaration,
2992 N_Package_Declaration)
2993 then
2994 Make_Aitem_Pragma
2995 (Pragma_Argument_Associations => New_List (
2996 Make_Pragma_Argument_Association (Loc,
2997 Expression => Relocate_Node (Expr))),
2998 Pragma_Name => Name_Initializes);
3000 Decorate (Aspect, Aitem);
3001 Insert_Pragma
3002 (Prag => Aitem,
3003 Is_Instance =>
3004 Is_Generic_Instance (Defining_Entity (Context)));
3006 -- Otherwise the context is illegal
3008 else
3009 Error_Msg_NE
3010 ("aspect & must apply to a package declaration",
3011 Aspect, Id);
3012 end if;
3014 goto Continue;
3015 end Initializes;
3017 -- Max_Queue_Length
3019 when Aspect_Max_Queue_Length =>
3020 Make_Aitem_Pragma
3021 (Pragma_Argument_Associations => New_List (
3022 Make_Pragma_Argument_Association (Loc,
3023 Expression => Relocate_Node (Expr))),
3024 Pragma_Name => Name_Max_Queue_Length);
3026 Decorate (Aspect, Aitem);
3027 Insert_Pragma (Aitem);
3028 goto Continue;
3030 -- Obsolescent
3032 when Aspect_Obsolescent => declare
3033 Args : List_Id;
3035 begin
3036 if No (Expr) then
3037 Args := No_List;
3038 else
3039 Args := New_List (
3040 Make_Pragma_Argument_Association (Sloc (Expr),
3041 Expression => Relocate_Node (Expr)));
3042 end if;
3044 Make_Aitem_Pragma
3045 (Pragma_Argument_Associations => Args,
3046 Pragma_Name => Chars (Id));
3047 end;
3049 -- Part_Of
3051 when Aspect_Part_Of =>
3052 if Nkind_In (N, N_Object_Declaration,
3053 N_Package_Instantiation)
3054 or else Is_Single_Concurrent_Type_Declaration (N)
3055 then
3056 Make_Aitem_Pragma
3057 (Pragma_Argument_Associations => New_List (
3058 Make_Pragma_Argument_Association (Loc,
3059 Expression => Relocate_Node (Expr))),
3060 Pragma_Name => Name_Part_Of);
3062 Decorate (Aspect, Aitem);
3063 Insert_Pragma (Aitem);
3065 else
3066 Error_Msg_NE
3067 ("aspect & must apply to package instantiation, "
3068 & "object, single protected type or single task type",
3069 Aspect, Id);
3070 end if;
3072 goto Continue;
3074 -- SPARK_Mode
3076 when Aspect_SPARK_Mode =>
3077 Make_Aitem_Pragma
3078 (Pragma_Argument_Associations => New_List (
3079 Make_Pragma_Argument_Association (Loc,
3080 Expression => Relocate_Node (Expr))),
3081 Pragma_Name => Name_SPARK_Mode);
3083 Decorate (Aspect, Aitem);
3084 Insert_Pragma (Aitem);
3085 goto Continue;
3087 -- Refined_Depends
3089 -- Aspect Refined_Depends is never delayed because it is
3090 -- equivalent to a source pragma which appears in the
3091 -- declarations of the related subprogram body. To deal with
3092 -- forward references, the generated pragma is stored in the
3093 -- contract of the related subprogram body and later analyzed
3094 -- at the end of the declarative region. For details, see
3095 -- routine Analyze_Refined_Depends_In_Decl_Part.
3097 when Aspect_Refined_Depends =>
3098 Make_Aitem_Pragma
3099 (Pragma_Argument_Associations => New_List (
3100 Make_Pragma_Argument_Association (Loc,
3101 Expression => Relocate_Node (Expr))),
3102 Pragma_Name => Name_Refined_Depends);
3104 Decorate (Aspect, Aitem);
3105 Insert_Pragma (Aitem);
3106 goto Continue;
3108 -- Refined_Global
3110 -- Aspect Refined_Global is never delayed because it is
3111 -- equivalent to a source pragma which appears in the
3112 -- declarations of the related subprogram body. To deal with
3113 -- forward references, the generated pragma is stored in the
3114 -- contract of the related subprogram body and later analyzed
3115 -- at the end of the declarative region. For details, see
3116 -- routine Analyze_Refined_Global_In_Decl_Part.
3118 when Aspect_Refined_Global =>
3119 Make_Aitem_Pragma
3120 (Pragma_Argument_Associations => New_List (
3121 Make_Pragma_Argument_Association (Loc,
3122 Expression => Relocate_Node (Expr))),
3123 Pragma_Name => Name_Refined_Global);
3125 Decorate (Aspect, Aitem);
3126 Insert_Pragma (Aitem);
3127 goto Continue;
3129 -- Refined_Post
3131 when Aspect_Refined_Post =>
3132 Make_Aitem_Pragma
3133 (Pragma_Argument_Associations => New_List (
3134 Make_Pragma_Argument_Association (Loc,
3135 Expression => Relocate_Node (Expr))),
3136 Pragma_Name => Name_Refined_Post);
3138 Decorate (Aspect, Aitem);
3139 Insert_Pragma (Aitem);
3140 goto Continue;
3142 -- Refined_State
3144 when Aspect_Refined_State =>
3146 -- The corresponding pragma for Refined_State is inserted in
3147 -- the declarations of the related package body. This action
3148 -- synchronizes both the source and from-aspect versions of
3149 -- the pragma.
3151 if Nkind (N) = N_Package_Body then
3152 Make_Aitem_Pragma
3153 (Pragma_Argument_Associations => New_List (
3154 Make_Pragma_Argument_Association (Loc,
3155 Expression => Relocate_Node (Expr))),
3156 Pragma_Name => Name_Refined_State);
3158 Decorate (Aspect, Aitem);
3159 Insert_Pragma (Aitem);
3161 -- Otherwise the context is illegal
3163 else
3164 Error_Msg_NE
3165 ("aspect & must apply to a package body", Aspect, Id);
3166 end if;
3168 goto Continue;
3170 -- Relative_Deadline
3172 when Aspect_Relative_Deadline =>
3173 Make_Aitem_Pragma
3174 (Pragma_Argument_Associations => New_List (
3175 Make_Pragma_Argument_Association (Loc,
3176 Expression => Relocate_Node (Expr))),
3177 Pragma_Name => Name_Relative_Deadline);
3179 -- If the aspect applies to a task, the corresponding pragma
3180 -- must appear within its declarations, not after.
3182 if Nkind (N) = N_Task_Type_Declaration then
3183 declare
3184 Def : Node_Id;
3185 V : List_Id;
3187 begin
3188 if No (Task_Definition (N)) then
3189 Set_Task_Definition (N,
3190 Make_Task_Definition (Loc,
3191 Visible_Declarations => New_List,
3192 End_Label => Empty));
3193 end if;
3195 Def := Task_Definition (N);
3196 V := Visible_Declarations (Def);
3197 if not Is_Empty_List (V) then
3198 Insert_Before (First (V), Aitem);
3200 else
3201 Set_Visible_Declarations (Def, New_List (Aitem));
3202 end if;
3204 goto Continue;
3205 end;
3206 end if;
3208 -- Aspect Volatile_Function is never delayed because it is
3209 -- equivalent to a source pragma which appears after the
3210 -- related subprogram.
3212 when Aspect_Volatile_Function =>
3213 Make_Aitem_Pragma
3214 (Pragma_Argument_Associations => New_List (
3215 Make_Pragma_Argument_Association (Loc,
3216 Expression => Relocate_Node (Expr))),
3217 Pragma_Name => Name_Volatile_Function);
3219 Decorate (Aspect, Aitem);
3220 Insert_Pragma (Aitem);
3221 goto Continue;
3223 -- Case 2e: Annotate aspect
3225 when Aspect_Annotate =>
3226 declare
3227 Args : List_Id;
3228 Pargs : List_Id;
3229 Arg : Node_Id;
3231 begin
3232 -- The argument can be a single identifier
3234 if Nkind (Expr) = N_Identifier then
3236 -- One level of parens is allowed
3238 if Paren_Count (Expr) > 1 then
3239 Error_Msg_F ("extra parentheses ignored", Expr);
3240 end if;
3242 Set_Paren_Count (Expr, 0);
3244 -- Add the single item to the list
3246 Args := New_List (Expr);
3248 -- Otherwise we must have an aggregate
3250 elsif Nkind (Expr) = N_Aggregate then
3252 -- Must be positional
3254 if Present (Component_Associations (Expr)) then
3255 Error_Msg_F
3256 ("purely positional aggregate required", Expr);
3257 goto Continue;
3258 end if;
3260 -- Must not be parenthesized
3262 if Paren_Count (Expr) /= 0 then
3263 Error_Msg_F ("extra parentheses ignored", Expr);
3264 end if;
3266 -- List of arguments is list of aggregate expressions
3268 Args := Expressions (Expr);
3270 -- Anything else is illegal
3272 else
3273 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3274 goto Continue;
3275 end if;
3277 -- Prepare pragma arguments
3279 Pargs := New_List;
3280 Arg := First (Args);
3281 while Present (Arg) loop
3282 Append_To (Pargs,
3283 Make_Pragma_Argument_Association (Sloc (Arg),
3284 Expression => Relocate_Node (Arg)));
3285 Next (Arg);
3286 end loop;
3288 Append_To (Pargs,
3289 Make_Pragma_Argument_Association (Sloc (Ent),
3290 Chars => Name_Entity,
3291 Expression => Ent));
3293 Make_Aitem_Pragma
3294 (Pragma_Argument_Associations => Pargs,
3295 Pragma_Name => Name_Annotate);
3296 end;
3298 -- Case 3 : Aspects that don't correspond to pragma/attribute
3299 -- definition clause.
3301 -- Case 3a: The aspects listed below don't correspond to
3302 -- pragmas/attributes but do require delayed analysis.
3304 -- Default_Value can only apply to a scalar type
3306 when Aspect_Default_Value =>
3307 if not Is_Scalar_Type (E) then
3308 Error_Msg_N
3309 ("aspect Default_Value must apply to a scalar type", N);
3310 end if;
3312 Aitem := Empty;
3314 -- Default_Component_Value can only apply to an array type
3315 -- with scalar components.
3317 when Aspect_Default_Component_Value =>
3318 if not (Is_Array_Type (E)
3319 and then Is_Scalar_Type (Component_Type (E)))
3320 then
3321 Error_Msg_N
3322 ("aspect Default_Component_Value can only apply to an "
3323 & "array of scalar components", N);
3324 end if;
3326 Aitem := Empty;
3328 -- Case 3b: The aspects listed below don't correspond to
3329 -- pragmas/attributes and don't need delayed analysis.
3331 -- Implicit_Dereference
3333 -- For Implicit_Dereference, External_Name and Link_Name, only
3334 -- the legality checks are done during the analysis, thus no
3335 -- delay is required.
3337 when Aspect_Implicit_Dereference =>
3338 Analyze_Aspect_Implicit_Dereference;
3339 goto Continue;
3341 -- Dimension
3343 when Aspect_Dimension =>
3344 Analyze_Aspect_Dimension (N, Id, Expr);
3345 goto Continue;
3347 -- Dimension_System
3349 when Aspect_Dimension_System =>
3350 Analyze_Aspect_Dimension_System (N, Id, Expr);
3351 goto Continue;
3353 -- Case 4: Aspects requiring special handling
3355 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3356 -- pragmas take care of the delay.
3358 -- Pre/Post
3360 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3361 -- with a first argument that is the expression, and a second
3362 -- argument that is an informative message if the test fails.
3363 -- This is inserted right after the declaration, to get the
3364 -- required pragma placement. The processing for the pragmas
3365 -- takes care of the required delay.
3367 when Pre_Post_Aspects => Pre_Post : declare
3368 Pname : Name_Id;
3370 begin
3371 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
3372 Pname := Name_Precondition;
3373 else
3374 Pname := Name_Postcondition;
3375 end if;
3377 -- Check that the class-wide predicate cannot be applied to
3378 -- an operation of a synchronized type. AI12-0182 forbids
3379 -- these altogether, while earlier language semantics made
3380 -- them legal on tagged synchronized types.
3382 -- Other legality checks are performed when analyzing the
3383 -- contract of the operation.
3385 if Class_Present (Aspect)
3386 and then Is_Concurrent_Type (Current_Scope)
3387 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3388 then
3389 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3390 Error_Msg_N
3391 ("aspect % can only be specified for a primitive "
3392 & "operation of a tagged type", Aspect);
3394 goto Continue;
3395 end if;
3397 -- If the expressions is of the form A and then B, then
3398 -- we generate separate Pre/Post aspects for the separate
3399 -- clauses. Since we allow multiple pragmas, there is no
3400 -- problem in allowing multiple Pre/Post aspects internally.
3401 -- These should be treated in reverse order (B first and
3402 -- A second) since they are later inserted just after N in
3403 -- the order they are treated. This way, the pragma for A
3404 -- ends up preceding the pragma for B, which may have an
3405 -- importance for the error raised (either constraint error
3406 -- or precondition error).
3408 -- We do not do this for Pre'Class, since we have to put
3409 -- these conditions together in a complex OR expression.
3411 -- We do not do this in ASIS mode, as ASIS relies on the
3412 -- original node representing the complete expression, when
3413 -- retrieving it through the source aspect table.
3415 if not ASIS_Mode
3416 and then (Pname = Name_Postcondition
3417 or else not Class_Present (Aspect))
3418 then
3419 while Nkind (Expr) = N_And_Then loop
3420 Insert_After (Aspect,
3421 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
3422 Identifier => Identifier (Aspect),
3423 Expression => Relocate_Node (Left_Opnd (Expr)),
3424 Class_Present => Class_Present (Aspect),
3425 Split_PPC => True));
3426 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
3427 Eloc := Sloc (Expr);
3428 end loop;
3429 end if;
3431 -- Build the precondition/postcondition pragma
3433 -- Add note about why we do NOT need Copy_Tree here???
3435 Make_Aitem_Pragma
3436 (Pragma_Argument_Associations => New_List (
3437 Make_Pragma_Argument_Association (Eloc,
3438 Chars => Name_Check,
3439 Expression => Relocate_Node (Expr))),
3440 Pragma_Name => Pname);
3442 -- Add message unless exception messages are suppressed
3444 if not Opt.Exception_Locations_Suppressed then
3445 Append_To (Pragma_Argument_Associations (Aitem),
3446 Make_Pragma_Argument_Association (Eloc,
3447 Chars => Name_Message,
3448 Expression =>
3449 Make_String_Literal (Eloc,
3450 Strval => "failed "
3451 & Get_Name_String (Pname)
3452 & " from "
3453 & Build_Location_String (Eloc))));
3454 end if;
3456 Set_Is_Delayed_Aspect (Aspect);
3458 -- For Pre/Post cases, insert immediately after the entity
3459 -- declaration, since that is the required pragma placement.
3460 -- Note that for these aspects, we do not have to worry
3461 -- about delay issues, since the pragmas themselves deal
3462 -- with delay of visibility for the expression analysis.
3464 Insert_Pragma (Aitem);
3466 goto Continue;
3467 end Pre_Post;
3469 -- Test_Case
3471 when Aspect_Test_Case => Test_Case : declare
3472 Args : List_Id;
3473 Comp_Expr : Node_Id;
3474 Comp_Assn : Node_Id;
3475 New_Expr : Node_Id;
3477 begin
3478 Args := New_List;
3480 if Nkind (Parent (N)) = N_Compilation_Unit then
3481 Error_Msg_Name_1 := Nam;
3482 Error_Msg_N ("incorrect placement of aspect `%`", E);
3483 goto Continue;
3484 end if;
3486 if Nkind (Expr) /= N_Aggregate then
3487 Error_Msg_Name_1 := Nam;
3488 Error_Msg_NE
3489 ("wrong syntax for aspect `%` for &", Id, E);
3490 goto Continue;
3491 end if;
3493 -- Make pragma expressions refer to the original aspect
3494 -- expressions through the Original_Node link. This is used
3495 -- in semantic analysis for ASIS mode, so that the original
3496 -- expression also gets analyzed.
3498 Comp_Expr := First (Expressions (Expr));
3499 while Present (Comp_Expr) loop
3500 New_Expr := Relocate_Node (Comp_Expr);
3501 Append_To (Args,
3502 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3503 Expression => New_Expr));
3504 Next (Comp_Expr);
3505 end loop;
3507 Comp_Assn := First (Component_Associations (Expr));
3508 while Present (Comp_Assn) loop
3509 if List_Length (Choices (Comp_Assn)) /= 1
3510 or else
3511 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3512 then
3513 Error_Msg_Name_1 := Nam;
3514 Error_Msg_NE
3515 ("wrong syntax for aspect `%` for &", Id, E);
3516 goto Continue;
3517 end if;
3519 Append_To (Args,
3520 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
3521 Chars => Chars (First (Choices (Comp_Assn))),
3522 Expression =>
3523 Relocate_Node (Expression (Comp_Assn))));
3524 Next (Comp_Assn);
3525 end loop;
3527 -- Build the test-case pragma
3529 Make_Aitem_Pragma
3530 (Pragma_Argument_Associations => Args,
3531 Pragma_Name => Nam);
3532 end Test_Case;
3534 -- Contract_Cases
3536 when Aspect_Contract_Cases =>
3537 Make_Aitem_Pragma
3538 (Pragma_Argument_Associations => New_List (
3539 Make_Pragma_Argument_Association (Loc,
3540 Expression => Relocate_Node (Expr))),
3541 Pragma_Name => Nam);
3543 Decorate (Aspect, Aitem);
3544 Insert_Pragma (Aitem);
3545 goto Continue;
3547 -- Case 5: Special handling for aspects with an optional
3548 -- boolean argument.
3550 -- In the delayed case, the corresponding pragma cannot be
3551 -- generated yet because the evaluation of the boolean needs
3552 -- to be delayed till the freeze point.
3554 when Boolean_Aspects
3555 | Library_Unit_Aspects
3557 Set_Is_Boolean_Aspect (Aspect);
3559 -- Lock_Free aspect only apply to protected objects
3561 if A_Id = Aspect_Lock_Free then
3562 if Ekind (E) /= E_Protected_Type then
3563 Error_Msg_Name_1 := Nam;
3564 Error_Msg_N
3565 ("aspect % only applies to a protected object",
3566 Aspect);
3568 else
3569 -- Set the Uses_Lock_Free flag to True if there is no
3570 -- expression or if the expression is True. The
3571 -- evaluation of this aspect should be delayed to the
3572 -- freeze point (why???)
3574 if No (Expr)
3575 or else Is_True (Static_Boolean (Expr))
3576 then
3577 Set_Uses_Lock_Free (E);
3578 end if;
3580 Record_Rep_Item (E, Aspect);
3581 end if;
3583 goto Continue;
3585 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3586 Analyze_Aspect_Export_Import;
3588 -- Disable_Controlled
3590 elsif A_Id = Aspect_Disable_Controlled then
3591 Analyze_Aspect_Disable_Controlled;
3592 goto Continue;
3593 end if;
3595 -- Library unit aspects require special handling in the case
3596 -- of a package declaration, the pragma needs to be inserted
3597 -- in the list of declarations for the associated package.
3598 -- There is no issue of visibility delay for these aspects.
3600 if A_Id in Library_Unit_Aspects
3601 and then
3602 Nkind_In (N, N_Package_Declaration,
3603 N_Generic_Package_Declaration)
3604 and then Nkind (Parent (N)) /= N_Compilation_Unit
3606 -- Aspect is legal on a local instantiation of a library-
3607 -- level generic unit.
3609 and then not Is_Generic_Instance (Defining_Entity (N))
3610 then
3611 Error_Msg_N
3612 ("incorrect context for library unit aspect&", Id);
3613 goto Continue;
3614 end if;
3616 -- Cases where we do not delay, includes all cases where the
3617 -- expression is missing other than the above cases.
3619 if not Delay_Required or else No (Expr) then
3621 -- Exclude aspects Export and Import because their pragma
3622 -- syntax does not map directly to a Boolean aspect.
3624 if A_Id /= Aspect_Export
3625 and then A_Id /= Aspect_Import
3626 then
3627 Make_Aitem_Pragma
3628 (Pragma_Argument_Associations => New_List (
3629 Make_Pragma_Argument_Association (Sloc (Ent),
3630 Expression => Ent)),
3631 Pragma_Name => Chars (Id));
3632 end if;
3634 Delay_Required := False;
3636 -- In general cases, the corresponding pragma/attribute
3637 -- definition clause will be inserted later at the freezing
3638 -- point, and we do not need to build it now.
3640 else
3641 Aitem := Empty;
3642 end if;
3644 -- Storage_Size
3646 -- This is special because for access types we need to generate
3647 -- an attribute definition clause. This also works for single
3648 -- task declarations, but it does not work for task type
3649 -- declarations, because we have the case where the expression
3650 -- references a discriminant of the task type. That can't use
3651 -- an attribute definition clause because we would not have
3652 -- visibility on the discriminant. For that case we must
3653 -- generate a pragma in the task definition.
3655 when Aspect_Storage_Size =>
3657 -- Task type case
3659 if Ekind (E) = E_Task_Type then
3660 declare
3661 Decl : constant Node_Id := Declaration_Node (E);
3663 begin
3664 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3666 -- If no task definition, create one
3668 if No (Task_Definition (Decl)) then
3669 Set_Task_Definition (Decl,
3670 Make_Task_Definition (Loc,
3671 Visible_Declarations => Empty_List,
3672 End_Label => Empty));
3673 end if;
3675 -- Create a pragma and put it at the start of the task
3676 -- definition for the task type declaration.
3678 Make_Aitem_Pragma
3679 (Pragma_Argument_Associations => New_List (
3680 Make_Pragma_Argument_Association (Loc,
3681 Expression => Relocate_Node (Expr))),
3682 Pragma_Name => Name_Storage_Size);
3684 Prepend
3685 (Aitem,
3686 Visible_Declarations (Task_Definition (Decl)));
3687 goto Continue;
3688 end;
3690 -- All other cases, generate attribute definition
3692 else
3693 Aitem :=
3694 Make_Attribute_Definition_Clause (Loc,
3695 Name => Ent,
3696 Chars => Chars (Id),
3697 Expression => Relocate_Node (Expr));
3698 end if;
3699 end case;
3701 -- Attach the corresponding pragma/attribute definition clause to
3702 -- the aspect specification node.
3704 if Present (Aitem) then
3705 Set_From_Aspect_Specification (Aitem);
3706 end if;
3708 -- In the context of a compilation unit, we directly put the
3709 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3710 -- node (no delay is required here) except for aspects on a
3711 -- subprogram body (see below) and a generic package, for which we
3712 -- need to introduce the pragma before building the generic copy
3713 -- (see sem_ch12), and for package instantiations, where the
3714 -- library unit pragmas are better handled early.
3716 if Nkind (Parent (N)) = N_Compilation_Unit
3717 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3718 then
3719 declare
3720 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
3722 begin
3723 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
3725 -- For a Boolean aspect, create the corresponding pragma if
3726 -- no expression or if the value is True.
3728 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
3729 if Is_True (Static_Boolean (Expr)) then
3730 Make_Aitem_Pragma
3731 (Pragma_Argument_Associations => New_List (
3732 Make_Pragma_Argument_Association (Sloc (Ent),
3733 Expression => Ent)),
3734 Pragma_Name => Chars (Id));
3736 Set_From_Aspect_Specification (Aitem, True);
3737 Set_Corresponding_Aspect (Aitem, Aspect);
3739 else
3740 goto Continue;
3741 end if;
3742 end if;
3744 -- If the aspect is on a subprogram body (relevant aspect
3745 -- is Inline), add the pragma in front of the declarations.
3747 if Nkind (N) = N_Subprogram_Body then
3748 if No (Declarations (N)) then
3749 Set_Declarations (N, New_List);
3750 end if;
3752 Prepend (Aitem, Declarations (N));
3754 elsif Nkind (N) = N_Generic_Package_Declaration then
3755 if No (Visible_Declarations (Specification (N))) then
3756 Set_Visible_Declarations (Specification (N), New_List);
3757 end if;
3759 Prepend (Aitem,
3760 Visible_Declarations (Specification (N)));
3762 elsif Nkind (N) = N_Package_Instantiation then
3763 declare
3764 Spec : constant Node_Id :=
3765 Specification (Instance_Spec (N));
3766 begin
3767 if No (Visible_Declarations (Spec)) then
3768 Set_Visible_Declarations (Spec, New_List);
3769 end if;
3771 Prepend (Aitem, Visible_Declarations (Spec));
3772 end;
3774 else
3775 if No (Pragmas_After (Aux)) then
3776 Set_Pragmas_After (Aux, New_List);
3777 end if;
3779 Append (Aitem, Pragmas_After (Aux));
3780 end if;
3782 goto Continue;
3783 end;
3784 end if;
3786 -- The evaluation of the aspect is delayed to the freezing point.
3787 -- The pragma or attribute clause if there is one is then attached
3788 -- to the aspect specification which is put in the rep item list.
3790 if Delay_Required then
3791 if Present (Aitem) then
3792 Set_Is_Delayed_Aspect (Aitem);
3793 Set_Aspect_Rep_Item (Aspect, Aitem);
3794 Set_Parent (Aitem, Aspect);
3795 end if;
3797 Set_Is_Delayed_Aspect (Aspect);
3799 -- In the case of Default_Value, link the aspect to base type
3800 -- as well, even though it appears on a first subtype. This is
3801 -- mandated by the semantics of the aspect. Do not establish
3802 -- the link when processing the base type itself as this leads
3803 -- to a rep item circularity. Verify that we are dealing with
3804 -- a scalar type to prevent cascaded errors.
3806 if A_Id = Aspect_Default_Value
3807 and then Is_Scalar_Type (E)
3808 and then Base_Type (E) /= E
3809 then
3810 Set_Has_Delayed_Aspects (Base_Type (E));
3811 Record_Rep_Item (Base_Type (E), Aspect);
3812 end if;
3814 Set_Has_Delayed_Aspects (E);
3815 Record_Rep_Item (E, Aspect);
3817 -- When delay is not required and the context is a package or a
3818 -- subprogram body, insert the pragma in the body declarations.
3820 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3821 if No (Declarations (N)) then
3822 Set_Declarations (N, New_List);
3823 end if;
3825 -- The pragma is added before source declarations
3827 Prepend_To (Declarations (N), Aitem);
3829 -- When delay is not required and the context is not a compilation
3830 -- unit, we simply insert the pragma/attribute definition clause
3831 -- in sequence.
3833 elsif Present (Aitem) then
3834 Insert_After (Ins_Node, Aitem);
3835 Ins_Node := Aitem;
3836 end if;
3837 end Analyze_One_Aspect;
3839 <<Continue>>
3840 Next (Aspect);
3841 end loop Aspect_Loop;
3843 if Has_Delayed_Aspects (E) then
3844 Ensure_Freeze_Node (E);
3845 end if;
3846 end Analyze_Aspect_Specifications;
3848 ---------------------------------------------------
3849 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3850 ---------------------------------------------------
3852 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3853 Body_Id : constant Entity_Id := Defining_Entity (N);
3855 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3856 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3857 -- error message depending on the aspects involved. Spec_Id denotes the
3858 -- entity of the corresponding spec.
3860 --------------------------------
3861 -- Diagnose_Misplaced_Aspects --
3862 --------------------------------
3864 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3865 procedure Misplaced_Aspect_Error
3866 (Asp : Node_Id;
3867 Ref_Nam : Name_Id);
3868 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3869 -- the name of the refined version of the aspect.
3871 ----------------------------
3872 -- Misplaced_Aspect_Error --
3873 ----------------------------
3875 procedure Misplaced_Aspect_Error
3876 (Asp : Node_Id;
3877 Ref_Nam : Name_Id)
3879 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3880 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3882 begin
3883 -- The corresponding spec already contains the aspect in question
3884 -- and the one appearing on the body must be the refined form:
3886 -- procedure P with Global ...;
3887 -- procedure P with Global ... is ... end P;
3888 -- ^
3889 -- Refined_Global
3891 if Has_Aspect (Spec_Id, Asp_Id) then
3892 Error_Msg_Name_1 := Asp_Nam;
3894 -- Subunits cannot carry aspects that apply to a subprogram
3895 -- declaration.
3897 if Nkind (Parent (N)) = N_Subunit then
3898 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3900 -- Otherwise suggest the refined form
3902 else
3903 Error_Msg_Name_2 := Ref_Nam;
3904 Error_Msg_N ("aspect % should be %", Asp);
3905 end if;
3907 -- Otherwise the aspect must appear on the spec, not on the body
3909 -- procedure P;
3910 -- procedure P with Global ... is ... end P;
3912 else
3913 Error_Msg_N
3914 ("aspect specification must appear on initial declaration",
3915 Asp);
3916 end if;
3917 end Misplaced_Aspect_Error;
3919 -- Local variables
3921 Asp : Node_Id;
3922 Asp_Nam : Name_Id;
3924 -- Start of processing for Diagnose_Misplaced_Aspects
3926 begin
3927 -- Iterate over the aspect specifications and emit specific errors
3928 -- where applicable.
3930 Asp := First (Aspect_Specifications (N));
3931 while Present (Asp) loop
3932 Asp_Nam := Chars (Identifier (Asp));
3934 -- Do not emit errors on aspects that can appear on a subprogram
3935 -- body. This scenario occurs when the aspect specification list
3936 -- contains both misplaced and properly placed aspects.
3938 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3939 null;
3941 -- Special diagnostics for SPARK aspects
3943 elsif Asp_Nam = Name_Depends then
3944 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3946 elsif Asp_Nam = Name_Global then
3947 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3949 elsif Asp_Nam = Name_Post then
3950 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3952 -- Otherwise a language-defined aspect is misplaced
3954 else
3955 Error_Msg_N
3956 ("aspect specification must appear on initial declaration",
3957 Asp);
3958 end if;
3960 Next (Asp);
3961 end loop;
3962 end Diagnose_Misplaced_Aspects;
3964 -- Local variables
3966 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
3968 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3970 begin
3971 -- Language-defined aspects cannot be associated with a subprogram body
3972 -- [stub] if the subprogram has a spec. Certain implementation defined
3973 -- aspects are allowed to break this rule (for all applicable cases, see
3974 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3976 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
3977 Diagnose_Misplaced_Aspects (Spec_Id);
3978 else
3979 Analyze_Aspect_Specifications (N, Body_Id);
3980 end if;
3981 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3983 -----------------------
3984 -- Analyze_At_Clause --
3985 -----------------------
3987 -- An at clause is replaced by the corresponding Address attribute
3988 -- definition clause that is the preferred approach in Ada 95.
3990 procedure Analyze_At_Clause (N : Node_Id) is
3991 CS : constant Boolean := Comes_From_Source (N);
3993 begin
3994 -- This is an obsolescent feature
3996 Check_Restriction (No_Obsolescent_Features, N);
3998 if Warn_On_Obsolescent_Feature then
3999 Error_Msg_N
4000 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
4001 Error_Msg_N
4002 ("\?j?use address attribute definition clause instead", N);
4003 end if;
4005 -- Rewrite as address clause
4007 Rewrite (N,
4008 Make_Attribute_Definition_Clause (Sloc (N),
4009 Name => Identifier (N),
4010 Chars => Name_Address,
4011 Expression => Expression (N)));
4013 -- We preserve Comes_From_Source, since logically the clause still comes
4014 -- from the source program even though it is changed in form.
4016 Set_Comes_From_Source (N, CS);
4018 -- Analyze rewritten clause
4020 Analyze_Attribute_Definition_Clause (N);
4021 end Analyze_At_Clause;
4023 -----------------------------------------
4024 -- Analyze_Attribute_Definition_Clause --
4025 -----------------------------------------
4027 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
4028 Loc : constant Source_Ptr := Sloc (N);
4029 Nam : constant Node_Id := Name (N);
4030 Attr : constant Name_Id := Chars (N);
4031 Expr : constant Node_Id := Expression (N);
4032 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
4034 Ent : Entity_Id;
4035 -- The entity of Nam after it is analyzed. In the case of an incomplete
4036 -- type, this is the underlying type.
4038 U_Ent : Entity_Id;
4039 -- The underlying entity to which the attribute applies. Generally this
4040 -- is the Underlying_Type of Ent, except in the case where the clause
4041 -- applies to the full view of an incomplete or private type, in which
4042 -- case U_Ent is just a copy of Ent.
4044 FOnly : Boolean := False;
4045 -- Reset to True for subtype specific attribute (Alignment, Size)
4046 -- and for stream attributes, i.e. those cases where in the call to
4047 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
4048 -- are checked. Note that the case of stream attributes is not clear
4049 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
4050 -- Storage_Size for derived task types, but that is also clearly
4051 -- unintentional.
4053 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
4054 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
4055 -- definition clauses.
4057 function Duplicate_Clause return Boolean;
4058 -- This routine checks if the aspect for U_Ent being given by attribute
4059 -- definition clause N is for an aspect that has already been specified,
4060 -- and if so gives an error message. If there is a duplicate, True is
4061 -- returned, otherwise if there is no error, False is returned.
4063 procedure Check_Indexing_Functions;
4064 -- Check that the function in Constant_Indexing or Variable_Indexing
4065 -- attribute has the proper type structure. If the name is overloaded,
4066 -- check that some interpretation is legal.
4068 procedure Check_Iterator_Functions;
4069 -- Check that there is a single function in Default_Iterator attribute
4070 -- that has the proper type structure.
4072 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
4073 -- Common legality check for the previous two
4075 -----------------------------------
4076 -- Analyze_Stream_TSS_Definition --
4077 -----------------------------------
4079 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
4080 Subp : Entity_Id := Empty;
4081 I : Interp_Index;
4082 It : Interp;
4083 Pnam : Entity_Id;
4085 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
4086 -- True for Read attribute, False for other attributes
4088 function Has_Good_Profile
4089 (Subp : Entity_Id;
4090 Report : Boolean := False) return Boolean;
4091 -- Return true if the entity is a subprogram with an appropriate
4092 -- profile for the attribute being defined. If result is False and
4093 -- Report is True, function emits appropriate error.
4095 ----------------------
4096 -- Has_Good_Profile --
4097 ----------------------
4099 function Has_Good_Profile
4100 (Subp : Entity_Id;
4101 Report : Boolean := False) return Boolean
4103 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
4104 (False => E_Procedure, True => E_Function);
4105 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
4106 F : Entity_Id;
4107 Typ : Entity_Id;
4109 begin
4110 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
4111 return False;
4112 end if;
4114 F := First_Formal (Subp);
4116 if No (F)
4117 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
4118 or else Designated_Type (Etype (F)) /=
4119 Class_Wide_Type (RTE (RE_Root_Stream_Type))
4120 then
4121 return False;
4122 end if;
4124 if not Is_Function then
4125 Next_Formal (F);
4127 declare
4128 Expected_Mode : constant array (Boolean) of Entity_Kind :=
4129 (False => E_In_Parameter,
4130 True => E_Out_Parameter);
4131 begin
4132 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
4133 return False;
4134 end if;
4135 end;
4137 Typ := Etype (F);
4139 -- If the attribute specification comes from an aspect
4140 -- specification for a class-wide stream, the parameter must be
4141 -- a class-wide type of the entity to which the aspect applies.
4143 if From_Aspect_Specification (N)
4144 and then Class_Present (Parent (N))
4145 and then Is_Class_Wide_Type (Typ)
4146 then
4147 Typ := Etype (Typ);
4148 end if;
4150 else
4151 Typ := Etype (Subp);
4152 end if;
4154 -- Verify that the prefix of the attribute and the local name for
4155 -- the type of the formal match, or one is the class-wide of the
4156 -- other, in the case of a class-wide stream operation.
4158 if Base_Type (Typ) = Base_Type (Ent)
4159 or else (Is_Class_Wide_Type (Typ)
4160 and then Typ = Class_Wide_Type (Base_Type (Ent)))
4161 or else (Is_Class_Wide_Type (Ent)
4162 and then Ent = Class_Wide_Type (Base_Type (Typ)))
4163 then
4164 null;
4165 else
4166 return False;
4167 end if;
4169 if Present (Next_Formal (F)) then
4170 return False;
4172 elsif not Is_Scalar_Type (Typ)
4173 and then not Is_First_Subtype (Typ)
4174 and then not Is_Class_Wide_Type (Typ)
4175 then
4176 if Report and not Is_First_Subtype (Typ) then
4177 Error_Msg_N
4178 ("subtype of formal in stream operation must be a first "
4179 & "subtype", Parameter_Type (Parent (F)));
4180 end if;
4182 return False;
4184 else
4185 return True;
4186 end if;
4187 end Has_Good_Profile;
4189 -- Start of processing for Analyze_Stream_TSS_Definition
4191 begin
4192 FOnly := True;
4194 if not Is_Type (U_Ent) then
4195 Error_Msg_N ("local name must be a subtype", Nam);
4196 return;
4198 elsif not Is_First_Subtype (U_Ent) then
4199 Error_Msg_N ("local name must be a first subtype", Nam);
4200 return;
4201 end if;
4203 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
4205 -- If Pnam is present, it can be either inherited from an ancestor
4206 -- type (in which case it is legal to redefine it for this type), or
4207 -- be a previous definition of the attribute for the same type (in
4208 -- which case it is illegal).
4210 -- In the first case, it will have been analyzed already, and we
4211 -- can check that its profile does not match the expected profile
4212 -- for a stream attribute of U_Ent. In the second case, either Pnam
4213 -- has been analyzed (and has the expected profile), or it has not
4214 -- been analyzed yet (case of a type that has not been frozen yet
4215 -- and for which the stream attribute has been set using Set_TSS).
4217 if Present (Pnam)
4218 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4219 then
4220 Error_Msg_Sloc := Sloc (Pnam);
4221 Error_Msg_Name_1 := Attr;
4222 Error_Msg_N ("% attribute already defined #", Nam);
4223 return;
4224 end if;
4226 Analyze (Expr);
4228 if Is_Entity_Name (Expr) then
4229 if not Is_Overloaded (Expr) then
4230 if Has_Good_Profile (Entity (Expr), Report => True) then
4231 Subp := Entity (Expr);
4232 end if;
4234 else
4235 Get_First_Interp (Expr, I, It);
4236 while Present (It.Nam) loop
4237 if Has_Good_Profile (It.Nam) then
4238 Subp := It.Nam;
4239 exit;
4240 end if;
4242 Get_Next_Interp (I, It);
4243 end loop;
4244 end if;
4245 end if;
4247 if Present (Subp) then
4248 if Is_Abstract_Subprogram (Subp) then
4249 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4250 return;
4252 -- A stream subprogram for an interface type must be a null
4253 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4254 -- of an interface is not an interface type (3.9.4 (6.b/2)).
4256 elsif Is_Interface (U_Ent)
4257 and then not Is_Class_Wide_Type (U_Ent)
4258 and then not Inside_A_Generic
4259 and then
4260 (Ekind (Subp) = E_Function
4261 or else
4262 not Null_Present
4263 (Specification
4264 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
4265 then
4266 Error_Msg_N
4267 ("stream subprogram for interface type must be null "
4268 & "procedure", Expr);
4269 end if;
4271 Set_Entity (Expr, Subp);
4272 Set_Etype (Expr, Etype (Subp));
4274 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
4276 else
4277 Error_Msg_Name_1 := Attr;
4278 Error_Msg_N ("incorrect expression for% attribute", Expr);
4279 end if;
4280 end Analyze_Stream_TSS_Definition;
4282 ------------------------------
4283 -- Check_Indexing_Functions --
4284 ------------------------------
4286 procedure Check_Indexing_Functions is
4287 Indexing_Found : Boolean := False;
4289 procedure Check_Inherited_Indexing;
4290 -- For a derived type, check that no indexing aspect is specified
4291 -- for the type if it is also inherited
4293 procedure Check_One_Function (Subp : Entity_Id);
4294 -- Check one possible interpretation. Sets Indexing_Found True if a
4295 -- legal indexing function is found.
4297 procedure Illegal_Indexing (Msg : String);
4298 -- Diagnose illegal indexing function if not overloaded. In the
4299 -- overloaded case indicate that no legal interpretation exists.
4301 ------------------------------
4302 -- Check_Inherited_Indexing --
4303 ------------------------------
4305 procedure Check_Inherited_Indexing is
4306 Inherited : Node_Id;
4308 begin
4309 if Attr = Name_Constant_Indexing then
4310 Inherited :=
4311 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4312 else pragma Assert (Attr = Name_Variable_Indexing);
4313 Inherited :=
4314 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4315 end if;
4317 if Present (Inherited) then
4318 if Debug_Flag_Dot_XX then
4319 null;
4321 -- OK if current attribute_definition_clause is expansion of
4322 -- inherited aspect.
4324 elsif Aspect_Rep_Item (Inherited) = N then
4325 null;
4327 -- Indicate the operation that must be overridden, rather than
4328 -- redefining the indexing aspect.
4330 else
4331 Illegal_Indexing
4332 ("indexing function already inherited from parent type");
4333 Error_Msg_NE
4334 ("!override & instead",
4335 N, Entity (Expression (Inherited)));
4336 end if;
4337 end if;
4338 end Check_Inherited_Indexing;
4340 ------------------------
4341 -- Check_One_Function --
4342 ------------------------
4344 procedure Check_One_Function (Subp : Entity_Id) is
4345 Default_Element : Node_Id;
4346 Ret_Type : constant Entity_Id := Etype (Subp);
4348 begin
4349 if not Is_Overloadable (Subp) then
4350 Illegal_Indexing ("illegal indexing function for type&");
4351 return;
4353 elsif Scope (Subp) /= Scope (Ent) then
4354 if Nkind (Expr) = N_Expanded_Name then
4356 -- Indexing function can't be declared elsewhere
4358 Illegal_Indexing
4359 ("indexing function must be declared in scope of type&");
4360 end if;
4362 return;
4364 elsif No (First_Formal (Subp)) then
4365 Illegal_Indexing
4366 ("Indexing requires a function that applies to type&");
4367 return;
4369 elsif No (Next_Formal (First_Formal (Subp))) then
4370 Illegal_Indexing
4371 ("indexing function must have at least two parameters");
4372 return;
4374 elsif Is_Derived_Type (Ent) then
4375 Check_Inherited_Indexing;
4376 end if;
4378 if not Check_Primitive_Function (Subp) then
4379 Illegal_Indexing
4380 ("Indexing aspect requires a function that applies to type&");
4381 return;
4382 end if;
4384 -- If partial declaration exists, verify that it is not tagged.
4386 if Ekind (Current_Scope) = E_Package
4387 and then Has_Private_Declaration (Ent)
4388 and then From_Aspect_Specification (N)
4389 and then
4390 List_Containing (Parent (Ent)) =
4391 Private_Declarations
4392 (Specification (Unit_Declaration_Node (Current_Scope)))
4393 and then Nkind (N) = N_Attribute_Definition_Clause
4394 then
4395 declare
4396 Decl : Node_Id;
4398 begin
4399 Decl :=
4400 First (Visible_Declarations
4401 (Specification
4402 (Unit_Declaration_Node (Current_Scope))));
4404 while Present (Decl) loop
4405 if Nkind (Decl) = N_Private_Type_Declaration
4406 and then Ent = Full_View (Defining_Identifier (Decl))
4407 and then Tagged_Present (Decl)
4408 and then No (Aspect_Specifications (Decl))
4409 then
4410 Illegal_Indexing
4411 ("Indexing aspect cannot be specified on full view "
4412 & "if partial view is tagged");
4413 return;
4414 end if;
4416 Next (Decl);
4417 end loop;
4418 end;
4419 end if;
4421 -- An indexing function must return either the default element of
4422 -- the container, or a reference type. For variable indexing it
4423 -- must be the latter.
4425 Default_Element :=
4426 Find_Value_Of_Aspect
4427 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4429 if Present (Default_Element) then
4430 Analyze (Default_Element);
4431 end if;
4433 -- For variable_indexing the return type must be a reference type
4435 if Attr = Name_Variable_Indexing then
4436 if not Has_Implicit_Dereference (Ret_Type) then
4437 Illegal_Indexing
4438 ("variable indexing must return a reference type");
4439 return;
4441 elsif Is_Access_Constant
4442 (Etype (First_Discriminant (Ret_Type)))
4443 then
4444 Illegal_Indexing
4445 ("variable indexing must return an access to variable");
4446 return;
4447 end if;
4449 else
4450 if Has_Implicit_Dereference (Ret_Type)
4451 and then not
4452 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4453 then
4454 Illegal_Indexing
4455 ("constant indexing must return an access to constant");
4456 return;
4458 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4459 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4460 then
4461 Illegal_Indexing
4462 ("constant indexing must apply to an access to constant");
4463 return;
4464 end if;
4465 end if;
4467 -- All checks succeeded.
4469 Indexing_Found := True;
4470 end Check_One_Function;
4472 -----------------------
4473 -- Illegal_Indexing --
4474 -----------------------
4476 procedure Illegal_Indexing (Msg : String) is
4477 begin
4478 Error_Msg_NE (Msg, N, Ent);
4479 end Illegal_Indexing;
4481 -- Start of processing for Check_Indexing_Functions
4483 begin
4484 if In_Instance then
4485 Check_Inherited_Indexing;
4486 end if;
4488 Analyze (Expr);
4490 if not Is_Overloaded (Expr) then
4491 Check_One_Function (Entity (Expr));
4493 else
4494 declare
4495 I : Interp_Index;
4496 It : Interp;
4498 begin
4499 Indexing_Found := False;
4500 Get_First_Interp (Expr, I, It);
4501 while Present (It.Nam) loop
4503 -- Note that analysis will have added the interpretation
4504 -- that corresponds to the dereference. We only check the
4505 -- subprogram itself. Ignore homonyms that may come from
4506 -- derived types in the context.
4508 if Is_Overloadable (It.Nam)
4509 and then Comes_From_Source (It.Nam)
4510 then
4511 Check_One_Function (It.Nam);
4512 end if;
4514 Get_Next_Interp (I, It);
4515 end loop;
4516 end;
4517 end if;
4519 if not Indexing_Found and then not Error_Posted (N) then
4520 Error_Msg_NE
4521 ("aspect Indexing requires a local function that applies to "
4522 & "type&", Expr, Ent);
4523 end if;
4524 end Check_Indexing_Functions;
4526 ------------------------------
4527 -- Check_Iterator_Functions --
4528 ------------------------------
4530 procedure Check_Iterator_Functions is
4531 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
4532 -- Check one possible interpretation for validity
4534 ----------------------------
4535 -- Valid_Default_Iterator --
4536 ----------------------------
4538 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4539 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
4540 Formal : Entity_Id;
4542 begin
4543 if not Check_Primitive_Function (Subp) then
4544 return False;
4546 -- The return type must be derived from a type in an instance
4547 -- of Iterator.Interfaces, and thus its root type must have a
4548 -- predefined name.
4550 elsif Chars (Root_T) /= Name_Forward_Iterator
4551 and then Chars (Root_T) /= Name_Reversible_Iterator
4552 then
4553 return False;
4555 else
4556 Formal := First_Formal (Subp);
4557 end if;
4559 -- False if any subsequent formal has no default expression
4561 Formal := Next_Formal (Formal);
4562 while Present (Formal) loop
4563 if No (Expression (Parent (Formal))) then
4564 return False;
4565 end if;
4567 Next_Formal (Formal);
4568 end loop;
4570 -- True if all subsequent formals have default expressions
4572 return True;
4573 end Valid_Default_Iterator;
4575 -- Start of processing for Check_Iterator_Functions
4577 begin
4578 Analyze (Expr);
4580 if not Is_Entity_Name (Expr) then
4581 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4582 end if;
4584 if not Is_Overloaded (Expr) then
4585 if not Check_Primitive_Function (Entity (Expr)) then
4586 Error_Msg_NE
4587 ("aspect Indexing requires a function that applies to type&",
4588 Entity (Expr), Ent);
4589 end if;
4591 -- Flag the default_iterator as well as the denoted function.
4593 if not Valid_Default_Iterator (Entity (Expr)) then
4594 Error_Msg_N ("improper function for default iterator!", Expr);
4595 end if;
4597 else
4598 declare
4599 Default : Entity_Id := Empty;
4600 I : Interp_Index;
4601 It : Interp;
4603 begin
4604 Get_First_Interp (Expr, I, It);
4605 while Present (It.Nam) loop
4606 if not Check_Primitive_Function (It.Nam)
4607 or else not Valid_Default_Iterator (It.Nam)
4608 then
4609 Remove_Interp (I);
4611 elsif Present (Default) then
4613 -- An explicit one should override an implicit one
4615 if Comes_From_Source (Default) =
4616 Comes_From_Source (It.Nam)
4617 then
4618 Error_Msg_N ("default iterator must be unique", Expr);
4619 Error_Msg_Sloc := Sloc (Default);
4620 Error_Msg_N ("\\possible interpretation#", Expr);
4621 Error_Msg_Sloc := Sloc (It.Nam);
4622 Error_Msg_N ("\\possible interpretation#", Expr);
4624 elsif Comes_From_Source (It.Nam) then
4625 Default := It.Nam;
4626 end if;
4627 else
4628 Default := It.Nam;
4629 end if;
4631 Get_Next_Interp (I, It);
4632 end loop;
4634 if Present (Default) then
4635 Set_Entity (Expr, Default);
4636 Set_Is_Overloaded (Expr, False);
4637 else
4638 Error_Msg_N
4639 ("no interpretation is a valid default iterator!", Expr);
4640 end if;
4641 end;
4642 end if;
4643 end Check_Iterator_Functions;
4645 -------------------------------
4646 -- Check_Primitive_Function --
4647 -------------------------------
4649 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4650 Ctrl : Entity_Id;
4652 begin
4653 if Ekind (Subp) /= E_Function then
4654 return False;
4655 end if;
4657 if No (First_Formal (Subp)) then
4658 return False;
4659 else
4660 Ctrl := Etype (First_Formal (Subp));
4661 end if;
4663 -- To be a primitive operation subprogram has to be in same scope.
4665 if Scope (Ctrl) /= Scope (Subp) then
4666 return False;
4667 end if;
4669 -- Type of formal may be the class-wide type, an access to such,
4670 -- or an incomplete view.
4672 if Ctrl = Ent
4673 or else Ctrl = Class_Wide_Type (Ent)
4674 or else
4675 (Ekind (Ctrl) = E_Anonymous_Access_Type
4676 and then (Designated_Type (Ctrl) = Ent
4677 or else
4678 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
4679 or else
4680 (Ekind (Ctrl) = E_Incomplete_Type
4681 and then Full_View (Ctrl) = Ent)
4682 then
4683 null;
4684 else
4685 return False;
4686 end if;
4688 return True;
4689 end Check_Primitive_Function;
4691 ----------------------
4692 -- Duplicate_Clause --
4693 ----------------------
4695 function Duplicate_Clause return Boolean is
4696 A : Node_Id;
4698 begin
4699 -- Nothing to do if this attribute definition clause comes from
4700 -- an aspect specification, since we could not be duplicating an
4701 -- explicit clause, and we dealt with the case of duplicated aspects
4702 -- in Analyze_Aspect_Specifications.
4704 if From_Aspect_Specification (N) then
4705 return False;
4706 end if;
4708 -- Otherwise current clause may duplicate previous clause, or a
4709 -- previously given pragma or aspect specification for the same
4710 -- aspect.
4712 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
4714 if Present (A) then
4715 Error_Msg_Name_1 := Chars (N);
4716 Error_Msg_Sloc := Sloc (A);
4718 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
4719 return True;
4720 end if;
4722 return False;
4723 end Duplicate_Clause;
4725 -- Start of processing for Analyze_Attribute_Definition_Clause
4727 begin
4728 -- The following code is a defense against recursion. Not clear that
4729 -- this can happen legitimately, but perhaps some error situations can
4730 -- cause it, and we did see this recursion during testing.
4732 if Analyzed (N) then
4733 return;
4734 else
4735 Set_Analyzed (N, True);
4736 end if;
4738 Check_Restriction_No_Use_Of_Attribute (N);
4740 -- Ignore some selected attributes in CodePeer mode since they are not
4741 -- relevant in this context.
4743 if CodePeer_Mode then
4744 case Id is
4746 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4747 -- internal representation of types by implicitly packing them.
4749 when Attribute_Component_Size =>
4750 Rewrite (N, Make_Null_Statement (Sloc (N)));
4751 return;
4753 when others =>
4754 null;
4755 end case;
4756 end if;
4758 -- Process Ignore_Rep_Clauses option
4760 if Ignore_Rep_Clauses then
4761 case Id is
4763 -- The following should be ignored. They do not affect legality
4764 -- and may be target dependent. The basic idea of -gnatI is to
4765 -- ignore any rep clauses that may be target dependent but do not
4766 -- affect legality (except possibly to be rejected because they
4767 -- are incompatible with the compilation target).
4769 when Attribute_Alignment
4770 | Attribute_Bit_Order
4771 | Attribute_Component_Size
4772 | Attribute_Default_Scalar_Storage_Order
4773 | Attribute_Machine_Radix
4774 | Attribute_Object_Size
4775 | Attribute_Scalar_Storage_Order
4776 | Attribute_Size
4777 | Attribute_Small
4778 | Attribute_Stream_Size
4779 | Attribute_Value_Size
4781 Kill_Rep_Clause (N);
4782 return;
4784 -- The following should not be ignored, because in the first place
4785 -- they are reasonably portable, and should not cause problems
4786 -- in compiling code from another target, and also they do affect
4787 -- legality, e.g. failing to provide a stream attribute for a type
4788 -- may make a program illegal.
4790 when Attribute_External_Tag
4791 | Attribute_Input
4792 | Attribute_Output
4793 | Attribute_Read
4794 | Attribute_Simple_Storage_Pool
4795 | Attribute_Storage_Pool
4796 | Attribute_Storage_Size
4797 | Attribute_Write
4799 null;
4801 -- We do not do anything here with address clauses, they will be
4802 -- removed by Freeze later on, but for now, it works better to
4803 -- keep them in the tree.
4805 when Attribute_Address =>
4806 null;
4808 -- Other cases are errors ("attribute& cannot be set with
4809 -- definition clause"), which will be caught below.
4811 when others =>
4812 null;
4813 end case;
4814 end if;
4816 Analyze (Nam);
4817 Ent := Entity (Nam);
4819 if Rep_Item_Too_Early (Ent, N) then
4820 return;
4821 end if;
4823 -- Rep clause applies to full view of incomplete type or private type if
4824 -- we have one (if not, this is a premature use of the type). However,
4825 -- certain semantic checks need to be done on the specified entity (i.e.
4826 -- the private view), so we save it in Ent.
4828 if Is_Private_Type (Ent)
4829 and then Is_Derived_Type (Ent)
4830 and then not Is_Tagged_Type (Ent)
4831 and then No (Full_View (Ent))
4832 then
4833 -- If this is a private type whose completion is a derivation from
4834 -- another private type, there is no full view, and the attribute
4835 -- belongs to the type itself, not its underlying parent.
4837 U_Ent := Ent;
4839 elsif Ekind (Ent) = E_Incomplete_Type then
4841 -- The attribute applies to the full view, set the entity of the
4842 -- attribute definition accordingly.
4844 Ent := Underlying_Type (Ent);
4845 U_Ent := Ent;
4846 Set_Entity (Nam, Ent);
4848 else
4849 U_Ent := Underlying_Type (Ent);
4850 end if;
4852 -- Avoid cascaded error
4854 if Etype (Nam) = Any_Type then
4855 return;
4857 -- Must be declared in current scope or in case of an aspect
4858 -- specification, must be visible in current scope.
4860 elsif Scope (Ent) /= Current_Scope
4861 and then
4862 not (From_Aspect_Specification (N)
4863 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
4864 then
4865 Error_Msg_N ("entity must be declared in this scope", Nam);
4866 return;
4868 -- Must not be a source renaming (we do have some cases where the
4869 -- expander generates a renaming, and those cases are OK, in such
4870 -- cases any attribute applies to the renamed object as well).
4872 elsif Is_Object (Ent)
4873 and then Present (Renamed_Object (Ent))
4874 then
4875 -- Case of renamed object from source, this is an error
4877 if Comes_From_Source (Renamed_Object (Ent)) then
4878 Get_Name_String (Chars (N));
4879 Error_Msg_Strlen := Name_Len;
4880 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4881 Error_Msg_N
4882 ("~ clause not allowed for a renaming declaration "
4883 & "(RM 13.1(6))", Nam);
4884 return;
4886 -- For the case of a compiler generated renaming, the attribute
4887 -- definition clause applies to the renamed object created by the
4888 -- expander. The easiest general way to handle this is to create a
4889 -- copy of the attribute definition clause for this object.
4891 elsif Is_Entity_Name (Renamed_Object (Ent)) then
4892 Insert_Action (N,
4893 Make_Attribute_Definition_Clause (Loc,
4894 Name =>
4895 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4896 Chars => Chars (N),
4897 Expression => Duplicate_Subexpr (Expression (N))));
4899 -- If the renamed object is not an entity, it must be a dereference
4900 -- of an unconstrained function call, and we must introduce a new
4901 -- declaration to capture the expression. This is needed in the case
4902 -- of 'Alignment, where the original declaration must be rewritten.
4904 else
4905 pragma Assert
4906 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4907 null;
4908 end if;
4910 -- If no underlying entity, use entity itself, applies to some
4911 -- previously detected error cases ???
4913 elsif No (U_Ent) then
4914 U_Ent := Ent;
4916 -- Cannot specify for a subtype (exception Object/Value_Size)
4918 elsif Is_Type (U_Ent)
4919 and then not Is_First_Subtype (U_Ent)
4920 and then Id /= Attribute_Object_Size
4921 and then Id /= Attribute_Value_Size
4922 and then not From_At_Mod (N)
4923 then
4924 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4925 return;
4926 end if;
4928 Set_Entity (N, U_Ent);
4930 -- Switch on particular attribute
4932 case Id is
4934 -------------
4935 -- Address --
4936 -------------
4938 -- Address attribute definition clause
4940 when Attribute_Address => Address : begin
4942 -- A little error check, catch for X'Address use X'Address;
4944 if Nkind (Nam) = N_Identifier
4945 and then Nkind (Expr) = N_Attribute_Reference
4946 and then Attribute_Name (Expr) = Name_Address
4947 and then Nkind (Prefix (Expr)) = N_Identifier
4948 and then Chars (Nam) = Chars (Prefix (Expr))
4949 then
4950 Error_Msg_NE
4951 ("address for & is self-referencing", Prefix (Expr), Ent);
4952 return;
4953 end if;
4955 -- Not that special case, carry on with analysis of expression
4957 Analyze_And_Resolve (Expr, RTE (RE_Address));
4959 -- Even when ignoring rep clauses we need to indicate that the
4960 -- entity has an address clause and thus it is legal to declare
4961 -- it imported. Freeze will get rid of the address clause later.
4962 -- Also call Set_Address_Taken to indicate that an address clause
4963 -- was present, even if we are about to remove it.
4965 if Ignore_Rep_Clauses then
4966 Set_Address_Taken (U_Ent);
4968 if Ekind_In (U_Ent, E_Variable, E_Constant) then
4969 Record_Rep_Item (U_Ent, N);
4970 end if;
4972 return;
4973 end if;
4975 if Duplicate_Clause then
4976 null;
4978 -- Case of address clause for subprogram
4980 elsif Is_Subprogram (U_Ent) then
4981 if Has_Homonym (U_Ent) then
4982 Error_Msg_N
4983 ("address clause cannot be given for overloaded "
4984 & "subprogram", Nam);
4985 return;
4986 end if;
4988 -- For subprograms, all address clauses are permitted, and we
4989 -- mark the subprogram as having a deferred freeze so that Gigi
4990 -- will not elaborate it too soon.
4992 -- Above needs more comments, what is too soon about???
4994 Set_Has_Delayed_Freeze (U_Ent);
4996 -- Case of address clause for entry
4998 elsif Ekind (U_Ent) = E_Entry then
4999 if Nkind (Parent (N)) = N_Task_Body then
5000 Error_Msg_N
5001 ("entry address must be specified in task spec", Nam);
5002 return;
5003 end if;
5005 -- For entries, we require a constant address
5007 Check_Constant_Address_Clause (Expr, U_Ent);
5009 -- Special checks for task types
5011 if Is_Task_Type (Scope (U_Ent))
5012 and then Comes_From_Source (Scope (U_Ent))
5013 then
5014 Error_Msg_N
5015 ("??entry address declared for entry in task type", N);
5016 Error_Msg_N
5017 ("\??only one task can be declared of this type", N);
5018 end if;
5020 -- Entry address clauses are obsolescent
5022 Check_Restriction (No_Obsolescent_Features, N);
5024 if Warn_On_Obsolescent_Feature then
5025 Error_Msg_N
5026 ("?j?attaching interrupt to task entry is an obsolescent "
5027 & "feature (RM J.7.1)", N);
5028 Error_Msg_N
5029 ("\?j?use interrupt procedure instead", N);
5030 end if;
5032 -- Case of an address clause for a class-wide object, which is
5033 -- considered erroneous.
5035 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
5036 Error_Msg_NE
5037 ("??class-wide object & must not be overlaid", Nam, U_Ent);
5038 Error_Msg_N
5039 ("\??Program_Error will be raised at run time", Nam);
5040 Insert_Action (Declaration_Node (U_Ent),
5041 Make_Raise_Program_Error (Loc,
5042 Reason => PE_Overlaid_Controlled_Object));
5043 return;
5045 -- Case of address clause for an object
5047 elsif Ekind_In (U_Ent, E_Constant, E_Variable) then
5048 declare
5049 Expr : constant Node_Id := Expression (N);
5050 O_Ent : Entity_Id;
5051 Off : Boolean;
5053 begin
5054 -- Exported variables cannot have an address clause, because
5055 -- this cancels the effect of the pragma Export.
5057 if Is_Exported (U_Ent) then
5058 Error_Msg_N
5059 ("cannot export object with address clause", Nam);
5060 return;
5061 end if;
5063 Find_Overlaid_Entity (N, O_Ent, Off);
5065 if Present (O_Ent) then
5067 -- If the object overlays a constant object, mark it so
5069 if Is_Constant_Object (O_Ent) then
5070 Set_Overlays_Constant (U_Ent);
5071 end if;
5073 -- If the address clause is of the form:
5075 -- for X'Address use Y'Address;
5077 -- or
5079 -- C : constant Address := Y'Address;
5080 -- ...
5081 -- for X'Address use C;
5083 -- then we make an entry in the table to check the size
5084 -- and alignment of the overlaying variable. But we defer
5085 -- this check till after code generation to take full
5086 -- advantage of the annotation done by the back end.
5088 -- If the entity has a generic type, the check will be
5089 -- performed in the instance if the actual type justifies
5090 -- it, and we do not insert the clause in the table to
5091 -- prevent spurious warnings.
5093 -- Note: we used to test Comes_From_Source and only give
5094 -- this warning for source entities, but we have removed
5095 -- this test. It really seems bogus to generate overlays
5096 -- that would trigger this warning in generated code.
5097 -- Furthermore, by removing the test, we handle the
5098 -- aspect case properly.
5100 if Is_Object (O_Ent)
5101 and then not Is_Generic_Type (Etype (U_Ent))
5102 and then Address_Clause_Overlay_Warnings
5103 then
5104 Register_Address_Clause_Check
5105 (N, U_Ent, No_Uint, O_Ent, Off);
5106 end if;
5108 -- If the overlay changes the storage order, mark the
5109 -- entity as being volatile to block any optimization
5110 -- for it since the construct is not really supported
5111 -- by the back end.
5113 if (Is_Record_Type (Etype (U_Ent))
5114 or else Is_Array_Type (Etype (U_Ent)))
5115 and then (Is_Record_Type (Etype (O_Ent))
5116 or else Is_Array_Type (Etype (O_Ent)))
5117 and then Reverse_Storage_Order (Etype (U_Ent)) /=
5118 Reverse_Storage_Order (Etype (O_Ent))
5119 then
5120 Set_Treat_As_Volatile (U_Ent);
5121 end if;
5123 else
5124 -- If this is not an overlay, mark a variable as being
5125 -- volatile to prevent unwanted optimizations. It's a
5126 -- conservative interpretation of RM 13.3(19) for the
5127 -- cases where the compiler cannot detect potential
5128 -- aliasing issues easily and it also covers the case
5129 -- of an absolute address where the volatile aspect is
5130 -- kind of implicit.
5132 if Ekind (U_Ent) = E_Variable then
5133 Set_Treat_As_Volatile (U_Ent);
5134 end if;
5136 -- Make an entry in the table for an absolute address as
5137 -- above to check that the value is compatible with the
5138 -- alignment of the object.
5140 declare
5141 Addr : constant Node_Id := Address_Value (Expr);
5142 begin
5143 if Compile_Time_Known_Value (Addr)
5144 and then Address_Clause_Overlay_Warnings
5145 then
5146 Register_Address_Clause_Check
5147 (N, U_Ent, Expr_Value (Addr), Empty, False);
5148 end if;
5149 end;
5150 end if;
5152 -- Issue an unconditional warning for a constant overlaying
5153 -- a variable. For the reverse case, we will issue it only
5154 -- if the variable is modified.
5156 if Ekind (U_Ent) = E_Constant
5157 and then Present (O_Ent)
5158 and then not Overlays_Constant (U_Ent)
5159 and then Address_Clause_Overlay_Warnings
5160 then
5161 Error_Msg_N ("??constant overlays a variable", Expr);
5163 -- Imported variables can have an address clause, but then
5164 -- the import is pretty meaningless except to suppress
5165 -- initializations, so we do not need such variables to
5166 -- be statically allocated (and in fact it causes trouble
5167 -- if the address clause is a local value).
5169 elsif Is_Imported (U_Ent) then
5170 Set_Is_Statically_Allocated (U_Ent, False);
5171 end if;
5173 -- We mark a possible modification of a variable with an
5174 -- address clause, since it is likely aliasing is occurring.
5176 Note_Possible_Modification (Nam, Sure => False);
5178 -- Legality checks on the address clause for initialized
5179 -- objects is deferred until the freeze point, because
5180 -- a subsequent pragma might indicate that the object
5181 -- is imported and thus not initialized. Also, the address
5182 -- clause might involve entities that have yet to be
5183 -- elaborated.
5185 Set_Has_Delayed_Freeze (U_Ent);
5187 -- If an initialization call has been generated for this
5188 -- object, it needs to be deferred to after the freeze node
5189 -- we have just now added, otherwise GIGI will see a
5190 -- reference to the variable (as actual to the IP call)
5191 -- before its definition.
5193 declare
5194 Init_Call : constant Node_Id :=
5195 Remove_Init_Call (U_Ent, N);
5197 begin
5198 if Present (Init_Call) then
5199 Append_Freeze_Action (U_Ent, Init_Call);
5201 -- Reset Initialization_Statements pointer so that
5202 -- if there is a pragma Import further down, it can
5203 -- clear any default initialization.
5205 Set_Initialization_Statements (U_Ent, Init_Call);
5206 end if;
5207 end;
5209 -- Entity has delayed freeze, so we will generate an
5210 -- alignment check at the freeze point unless suppressed.
5212 if not Range_Checks_Suppressed (U_Ent)
5213 and then not Alignment_Checks_Suppressed (U_Ent)
5214 then
5215 Set_Check_Address_Alignment (N);
5216 end if;
5218 -- Kill the size check code, since we are not allocating
5219 -- the variable, it is somewhere else.
5221 Kill_Size_Check_Code (U_Ent);
5222 end;
5224 -- Not a valid entity for an address clause
5226 else
5227 Error_Msg_N ("address cannot be given for &", Nam);
5228 end if;
5229 end Address;
5231 ---------------
5232 -- Alignment --
5233 ---------------
5235 -- Alignment attribute definition clause
5237 when Attribute_Alignment => Alignment : declare
5238 Align : constant Uint := Get_Alignment_Value (Expr);
5239 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
5241 begin
5242 FOnly := True;
5244 if not Is_Type (U_Ent)
5245 and then Ekind (U_Ent) /= E_Variable
5246 and then Ekind (U_Ent) /= E_Constant
5247 then
5248 Error_Msg_N ("alignment cannot be given for &", Nam);
5250 elsif Duplicate_Clause then
5251 null;
5253 elsif Align /= No_Uint then
5254 Set_Has_Alignment_Clause (U_Ent);
5256 -- Tagged type case, check for attempt to set alignment to a
5257 -- value greater than Max_Align, and reset if so. This error
5258 -- is suppressed in ASIS mode to allow for different ASIS
5259 -- back ends or ASIS-based tools to query the illegal clause.
5261 if Is_Tagged_Type (U_Ent)
5262 and then Align > Max_Align
5263 and then not ASIS_Mode
5264 then
5265 Error_Msg_N
5266 ("alignment for & set to Maximum_Aligment??", Nam);
5267 Set_Alignment (U_Ent, Max_Align);
5269 -- All other cases
5271 else
5272 Set_Alignment (U_Ent, Align);
5273 end if;
5275 -- For an array type, U_Ent is the first subtype. In that case,
5276 -- also set the alignment of the anonymous base type so that
5277 -- other subtypes (such as the itypes for aggregates of the
5278 -- type) also receive the expected alignment.
5280 if Is_Array_Type (U_Ent) then
5281 Set_Alignment (Base_Type (U_Ent), Align);
5282 end if;
5283 end if;
5284 end Alignment;
5286 ---------------
5287 -- Bit_Order --
5288 ---------------
5290 -- Bit_Order attribute definition clause
5292 when Attribute_Bit_Order =>
5293 if not Is_Record_Type (U_Ent) then
5294 Error_Msg_N
5295 ("Bit_Order can only be defined for record type", Nam);
5297 elsif Duplicate_Clause then
5298 null;
5300 else
5301 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5303 if Etype (Expr) = Any_Type then
5304 return;
5306 elsif not Is_OK_Static_Expression (Expr) then
5307 Flag_Non_Static_Expr
5308 ("Bit_Order requires static expression!", Expr);
5310 else
5311 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5312 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
5313 end if;
5314 end if;
5315 end if;
5317 --------------------
5318 -- Component_Size --
5319 --------------------
5321 -- Component_Size attribute definition clause
5323 when Attribute_Component_Size => Component_Size_Case : declare
5324 Csize : constant Uint := Static_Integer (Expr);
5325 Ctyp : Entity_Id;
5326 Btype : Entity_Id;
5327 Biased : Boolean;
5328 New_Ctyp : Entity_Id;
5329 Decl : Node_Id;
5331 begin
5332 if not Is_Array_Type (U_Ent) then
5333 Error_Msg_N ("component size requires array type", Nam);
5334 return;
5335 end if;
5337 Btype := Base_Type (U_Ent);
5338 Ctyp := Component_Type (Btype);
5340 if Duplicate_Clause then
5341 null;
5343 elsif Rep_Item_Too_Early (Btype, N) then
5344 null;
5346 elsif Csize /= No_Uint then
5347 Check_Size (Expr, Ctyp, Csize, Biased);
5349 -- For the biased case, build a declaration for a subtype that
5350 -- will be used to represent the biased subtype that reflects
5351 -- the biased representation of components. We need the subtype
5352 -- to get proper conversions on referencing elements of the
5353 -- array.
5355 if Biased then
5356 New_Ctyp :=
5357 Make_Defining_Identifier (Loc,
5358 Chars =>
5359 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
5361 Decl :=
5362 Make_Subtype_Declaration (Loc,
5363 Defining_Identifier => New_Ctyp,
5364 Subtype_Indication =>
5365 New_Occurrence_Of (Component_Type (Btype), Loc));
5367 Set_Parent (Decl, N);
5368 Analyze (Decl, Suppress => All_Checks);
5370 Set_Has_Delayed_Freeze (New_Ctyp, False);
5371 Set_Esize (New_Ctyp, Csize);
5372 Set_RM_Size (New_Ctyp, Csize);
5373 Init_Alignment (New_Ctyp);
5374 Set_Is_Itype (New_Ctyp, True);
5375 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5377 Set_Component_Type (Btype, New_Ctyp);
5378 Set_Biased (New_Ctyp, N, "component size clause");
5379 end if;
5381 Set_Component_Size (Btype, Csize);
5383 -- Deal with warning on overridden size
5385 if Warn_On_Overridden_Size
5386 and then Has_Size_Clause (Ctyp)
5387 and then RM_Size (Ctyp) /= Csize
5388 then
5389 Error_Msg_NE
5390 ("component size overrides size clause for&?S?", N, Ctyp);
5391 end if;
5393 Set_Has_Component_Size_Clause (Btype, True);
5394 Set_Has_Non_Standard_Rep (Btype, True);
5395 end if;
5396 end Component_Size_Case;
5398 -----------------------
5399 -- Constant_Indexing --
5400 -----------------------
5402 when Attribute_Constant_Indexing =>
5403 Check_Indexing_Functions;
5405 ---------
5406 -- CPU --
5407 ---------
5409 when Attribute_CPU =>
5411 -- CPU attribute definition clause not allowed except from aspect
5412 -- specification.
5414 if From_Aspect_Specification (N) then
5415 if not Is_Task_Type (U_Ent) then
5416 Error_Msg_N ("CPU can only be defined for task", Nam);
5418 elsif Duplicate_Clause then
5419 null;
5421 else
5422 -- The expression must be analyzed in the special manner
5423 -- described in "Handling of Default and Per-Object
5424 -- Expressions" in sem.ads.
5426 -- The visibility to the discriminants must be restored
5428 Push_Scope_And_Install_Discriminants (U_Ent);
5429 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5430 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5432 if not Is_OK_Static_Expression (Expr) then
5433 Check_Restriction (Static_Priorities, Expr);
5434 end if;
5435 end if;
5437 else
5438 Error_Msg_N
5439 ("attribute& cannot be set with definition clause", N);
5440 end if;
5442 ----------------------
5443 -- Default_Iterator --
5444 ----------------------
5446 when Attribute_Default_Iterator => Default_Iterator : declare
5447 Func : Entity_Id;
5448 Typ : Entity_Id;
5450 begin
5451 -- If target type is untagged, further checks are irrelevant
5453 if not Is_Tagged_Type (U_Ent) then
5454 Error_Msg_N
5455 ("aspect Default_Iterator applies to tagged type", Nam);
5456 return;
5457 end if;
5459 Check_Iterator_Functions;
5461 Analyze (Expr);
5463 if not Is_Entity_Name (Expr)
5464 or else Ekind (Entity (Expr)) /= E_Function
5465 then
5466 Error_Msg_N ("aspect Iterator must be a function", Expr);
5467 return;
5468 else
5469 Func := Entity (Expr);
5470 end if;
5472 -- The type of the first parameter must be T, T'class, or a
5473 -- corresponding access type (5.5.1 (8/3). If function is
5474 -- parameterless label type accordingly.
5476 if No (First_Formal (Func)) then
5477 Typ := Any_Type;
5478 else
5479 Typ := Etype (First_Formal (Func));
5480 end if;
5482 if Typ = U_Ent
5483 or else Typ = Class_Wide_Type (U_Ent)
5484 or else (Is_Access_Type (Typ)
5485 and then Designated_Type (Typ) = U_Ent)
5486 or else (Is_Access_Type (Typ)
5487 and then Designated_Type (Typ) =
5488 Class_Wide_Type (U_Ent))
5489 then
5490 null;
5492 else
5493 Error_Msg_NE
5494 ("Default Iterator must be a primitive of&", Func, U_Ent);
5495 end if;
5496 end Default_Iterator;
5498 ------------------------
5499 -- Dispatching_Domain --
5500 ------------------------
5502 when Attribute_Dispatching_Domain =>
5504 -- Dispatching_Domain attribute definition clause not allowed
5505 -- except from aspect specification.
5507 if From_Aspect_Specification (N) then
5508 if not Is_Task_Type (U_Ent) then
5509 Error_Msg_N
5510 ("Dispatching_Domain can only be defined for task", Nam);
5512 elsif Duplicate_Clause then
5513 null;
5515 else
5516 -- The expression must be analyzed in the special manner
5517 -- described in "Handling of Default and Per-Object
5518 -- Expressions" in sem.ads.
5520 -- The visibility to the discriminants must be restored
5522 Push_Scope_And_Install_Discriminants (U_Ent);
5524 Preanalyze_Spec_Expression
5525 (Expr, RTE (RE_Dispatching_Domain));
5527 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5528 end if;
5530 else
5531 Error_Msg_N
5532 ("attribute& cannot be set with definition clause", N);
5533 end if;
5535 ------------------
5536 -- External_Tag --
5537 ------------------
5539 when Attribute_External_Tag =>
5540 if not Is_Tagged_Type (U_Ent) then
5541 Error_Msg_N ("should be a tagged type", Nam);
5542 end if;
5544 if Duplicate_Clause then
5545 null;
5547 else
5548 Analyze_And_Resolve (Expr, Standard_String);
5550 if not Is_OK_Static_Expression (Expr) then
5551 Flag_Non_Static_Expr
5552 ("static string required for tag name!", Nam);
5553 end if;
5555 if not Is_Library_Level_Entity (U_Ent) then
5556 Error_Msg_NE
5557 ("??non-unique external tag supplied for &", N, U_Ent);
5558 Error_Msg_N
5559 ("\??same external tag applies to all subprogram calls",
5561 Error_Msg_N
5562 ("\??corresponding internal tag cannot be obtained", N);
5563 end if;
5564 end if;
5566 --------------------------
5567 -- Implicit_Dereference --
5568 --------------------------
5570 when Attribute_Implicit_Dereference =>
5572 -- Legality checks already performed at the point of the type
5573 -- declaration, aspect is not delayed.
5575 null;
5577 -----------
5578 -- Input --
5579 -----------
5581 when Attribute_Input =>
5582 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5583 Set_Has_Specified_Stream_Input (Ent);
5585 ------------------------
5586 -- Interrupt_Priority --
5587 ------------------------
5589 when Attribute_Interrupt_Priority =>
5591 -- Interrupt_Priority attribute definition clause not allowed
5592 -- except from aspect specification.
5594 if From_Aspect_Specification (N) then
5595 if not Is_Concurrent_Type (U_Ent) then
5596 Error_Msg_N
5597 ("Interrupt_Priority can only be defined for task and "
5598 & "protected object", Nam);
5600 elsif Duplicate_Clause then
5601 null;
5603 else
5604 -- The expression must be analyzed in the special manner
5605 -- described in "Handling of Default and Per-Object
5606 -- Expressions" in sem.ads.
5608 -- The visibility to the discriminants must be restored
5610 Push_Scope_And_Install_Discriminants (U_Ent);
5612 Preanalyze_Spec_Expression
5613 (Expr, RTE (RE_Interrupt_Priority));
5615 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5617 -- Check the No_Task_At_Interrupt_Priority restriction
5619 if Is_Task_Type (U_Ent) then
5620 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5621 end if;
5622 end if;
5624 else
5625 Error_Msg_N
5626 ("attribute& cannot be set with definition clause", N);
5627 end if;
5629 --------------
5630 -- Iterable --
5631 --------------
5633 when Attribute_Iterable =>
5634 Analyze (Expr);
5636 if Nkind (Expr) /= N_Aggregate then
5637 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5638 end if;
5640 declare
5641 Assoc : Node_Id;
5643 begin
5644 Assoc := First (Component_Associations (Expr));
5645 while Present (Assoc) loop
5646 if not Is_Entity_Name (Expression (Assoc)) then
5647 Error_Msg_N ("value must be a function", Assoc);
5648 end if;
5650 Next (Assoc);
5651 end loop;
5652 end;
5654 ----------------------
5655 -- Iterator_Element --
5656 ----------------------
5658 when Attribute_Iterator_Element =>
5659 Analyze (Expr);
5661 if not Is_Entity_Name (Expr)
5662 or else not Is_Type (Entity (Expr))
5663 then
5664 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5665 end if;
5667 -------------------
5668 -- Machine_Radix --
5669 -------------------
5671 -- Machine radix attribute definition clause
5673 when Attribute_Machine_Radix => Machine_Radix : declare
5674 Radix : constant Uint := Static_Integer (Expr);
5676 begin
5677 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5678 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5680 elsif Duplicate_Clause then
5681 null;
5683 elsif Radix /= No_Uint then
5684 Set_Has_Machine_Radix_Clause (U_Ent);
5685 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5687 if Radix = 2 then
5688 null;
5690 elsif Radix = 10 then
5691 Set_Machine_Radix_10 (U_Ent);
5693 -- The following error is suppressed in ASIS mode to allow for
5694 -- different ASIS back ends or ASIS-based tools to query the
5695 -- illegal clause.
5697 elsif not ASIS_Mode then
5698 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5699 end if;
5700 end if;
5701 end Machine_Radix;
5703 -----------------
5704 -- Object_Size --
5705 -----------------
5707 -- Object_Size attribute definition clause
5709 when Attribute_Object_Size => Object_Size : declare
5710 Size : constant Uint := Static_Integer (Expr);
5712 Biased : Boolean;
5713 pragma Warnings (Off, Biased);
5715 begin
5716 if not Is_Type (U_Ent) then
5717 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5719 elsif Duplicate_Clause then
5720 null;
5722 else
5723 Check_Size (Expr, U_Ent, Size, Biased);
5725 -- The following errors are suppressed in ASIS mode to allow
5726 -- for different ASIS back ends or ASIS-based tools to query
5727 -- the illegal clause.
5729 if ASIS_Mode then
5730 null;
5732 elsif Is_Scalar_Type (U_Ent) then
5733 if Size /= 8 and then Size /= 16 and then Size /= 32
5734 and then UI_Mod (Size, 64) /= 0
5735 then
5736 Error_Msg_N
5737 ("Object_Size must be 8, 16, 32, or multiple of 64",
5738 Expr);
5739 end if;
5741 elsif Size mod 8 /= 0 then
5742 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
5743 end if;
5745 Set_Esize (U_Ent, Size);
5746 Set_Has_Object_Size_Clause (U_Ent);
5747 Alignment_Check_For_Size_Change (U_Ent, Size);
5748 end if;
5749 end Object_Size;
5751 ------------
5752 -- Output --
5753 ------------
5755 when Attribute_Output =>
5756 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5757 Set_Has_Specified_Stream_Output (Ent);
5759 --------------
5760 -- Priority --
5761 --------------
5763 when Attribute_Priority =>
5765 -- Priority attribute definition clause not allowed except from
5766 -- aspect specification.
5768 if From_Aspect_Specification (N) then
5769 if not (Is_Concurrent_Type (U_Ent)
5770 or else Ekind (U_Ent) = E_Procedure)
5771 then
5772 Error_Msg_N
5773 ("Priority can only be defined for task and protected "
5774 & "object", Nam);
5776 elsif Duplicate_Clause then
5777 null;
5779 else
5780 -- The expression must be analyzed in the special manner
5781 -- described in "Handling of Default and Per-Object
5782 -- Expressions" in sem.ads.
5784 -- The visibility to the discriminants must be restored
5786 Push_Scope_And_Install_Discriminants (U_Ent);
5787 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5788 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5790 if not Is_OK_Static_Expression (Expr) then
5791 Check_Restriction (Static_Priorities, Expr);
5792 end if;
5793 end if;
5795 else
5796 Error_Msg_N
5797 ("attribute& cannot be set with definition clause", N);
5798 end if;
5800 ----------
5801 -- Read --
5802 ----------
5804 when Attribute_Read =>
5805 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5806 Set_Has_Specified_Stream_Read (Ent);
5808 --------------------------
5809 -- Scalar_Storage_Order --
5810 --------------------------
5812 -- Scalar_Storage_Order attribute definition clause
5814 when Attribute_Scalar_Storage_Order =>
5815 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
5816 Error_Msg_N
5817 ("Scalar_Storage_Order can only be defined for record or "
5818 & "array type", Nam);
5820 elsif Duplicate_Clause then
5821 null;
5823 else
5824 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5826 if Etype (Expr) = Any_Type then
5827 return;
5829 elsif not Is_OK_Static_Expression (Expr) then
5830 Flag_Non_Static_Expr
5831 ("Scalar_Storage_Order requires static expression!", Expr);
5833 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5835 -- Here for the case of a non-default (i.e. non-confirming)
5836 -- Scalar_Storage_Order attribute definition.
5838 if Support_Nondefault_SSO_On_Target then
5839 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
5840 else
5841 Error_Msg_N
5842 ("non-default Scalar_Storage_Order not supported on "
5843 & "target", Expr);
5844 end if;
5845 end if;
5847 -- Clear SSO default indications since explicit setting of the
5848 -- order overrides the defaults.
5850 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5851 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
5852 end if;
5854 --------------------------
5855 -- Secondary_Stack_Size --
5856 --------------------------
5858 when Attribute_Secondary_Stack_Size =>
5860 -- Secondary_Stack_Size attribute definition clause not allowed
5861 -- except from aspect specification.
5863 if From_Aspect_Specification (N) then
5864 if not Is_Task_Type (U_Ent) then
5865 Error_Msg_N
5866 ("Secondary Stack Size can only be defined for task", Nam);
5868 elsif Duplicate_Clause then
5869 null;
5871 else
5872 Check_Restriction (No_Secondary_Stack, Expr);
5874 -- The expression must be analyzed in the special manner
5875 -- described in "Handling of Default and Per-Object
5876 -- Expressions" in sem.ads.
5878 -- The visibility to the discriminants must be restored
5880 Push_Scope_And_Install_Discriminants (U_Ent);
5881 Preanalyze_Spec_Expression (Expr, Any_Integer);
5882 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5884 if not Is_OK_Static_Expression (Expr) then
5885 Check_Restriction (Static_Storage_Size, Expr);
5886 end if;
5887 end if;
5889 else
5890 Error_Msg_N
5891 ("attribute& cannot be set with definition clause", N);
5892 end if;
5894 ----------
5895 -- Size --
5896 ----------
5898 -- Size attribute definition clause
5900 when Attribute_Size => Size : declare
5901 Size : constant Uint := Static_Integer (Expr);
5902 Etyp : Entity_Id;
5903 Biased : Boolean;
5905 begin
5906 FOnly := True;
5908 if Duplicate_Clause then
5909 null;
5911 elsif not Is_Type (U_Ent)
5912 and then Ekind (U_Ent) /= E_Variable
5913 and then Ekind (U_Ent) /= E_Constant
5914 then
5915 Error_Msg_N ("size cannot be given for &", Nam);
5917 elsif Is_Array_Type (U_Ent)
5918 and then not Is_Constrained (U_Ent)
5919 then
5920 Error_Msg_N
5921 ("size cannot be given for unconstrained array", Nam);
5923 elsif Size /= No_Uint then
5924 if Is_Type (U_Ent) then
5925 Etyp := U_Ent;
5926 else
5927 Etyp := Etype (U_Ent);
5928 end if;
5930 -- Check size, note that Gigi is in charge of checking that the
5931 -- size of an array or record type is OK. Also we do not check
5932 -- the size in the ordinary fixed-point case, since it is too
5933 -- early to do so (there may be subsequent small clause that
5934 -- affects the size). We can check the size if a small clause
5935 -- has already been given.
5937 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5938 or else Has_Small_Clause (U_Ent)
5939 then
5940 Check_Size (Expr, Etyp, Size, Biased);
5941 Set_Biased (U_Ent, N, "size clause", Biased);
5942 end if;
5944 -- For types set RM_Size and Esize if possible
5946 if Is_Type (U_Ent) then
5947 Set_RM_Size (U_Ent, Size);
5949 -- For elementary types, increase Object_Size to power of 2,
5950 -- but not less than a storage unit in any case (normally
5951 -- this means it will be byte addressable).
5953 -- For all other types, nothing else to do, we leave Esize
5954 -- (object size) unset, the back end will set it from the
5955 -- size and alignment in an appropriate manner.
5957 -- In both cases, we check whether the alignment must be
5958 -- reset in the wake of the size change.
5960 if Is_Elementary_Type (U_Ent) then
5961 if Size <= System_Storage_Unit then
5962 Init_Esize (U_Ent, System_Storage_Unit);
5963 elsif Size <= 16 then
5964 Init_Esize (U_Ent, 16);
5965 elsif Size <= 32 then
5966 Init_Esize (U_Ent, 32);
5967 else
5968 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5969 end if;
5971 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5972 else
5973 Alignment_Check_For_Size_Change (U_Ent, Size);
5974 end if;
5976 -- For objects, set Esize only
5978 else
5979 -- The following error is suppressed in ASIS mode to allow
5980 -- for different ASIS back ends or ASIS-based tools to query
5981 -- the illegal clause.
5983 if Is_Elementary_Type (Etyp)
5984 and then Size /= System_Storage_Unit
5985 and then Size /= System_Storage_Unit * 2
5986 and then Size /= System_Storage_Unit * 4
5987 and then Size /= System_Storage_Unit * 8
5988 and then not ASIS_Mode
5989 then
5990 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5991 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5992 Error_Msg_N
5993 ("size for primitive object must be a power of 2 in "
5994 & "the range ^-^", N);
5995 end if;
5997 Set_Esize (U_Ent, Size);
5998 end if;
6000 Set_Has_Size_Clause (U_Ent);
6001 end if;
6002 end Size;
6004 -----------
6005 -- Small --
6006 -----------
6008 -- Small attribute definition clause
6010 when Attribute_Small => Small : declare
6011 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
6012 Small : Ureal;
6014 begin
6015 Analyze_And_Resolve (Expr, Any_Real);
6017 if Etype (Expr) = Any_Type then
6018 return;
6020 elsif not Is_OK_Static_Expression (Expr) then
6021 Flag_Non_Static_Expr
6022 ("small requires static expression!", Expr);
6023 return;
6025 else
6026 Small := Expr_Value_R (Expr);
6028 if Small <= Ureal_0 then
6029 Error_Msg_N ("small value must be greater than zero", Expr);
6030 return;
6031 end if;
6033 end if;
6035 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
6036 Error_Msg_N
6037 ("small requires an ordinary fixed point type", Nam);
6039 elsif Has_Small_Clause (U_Ent) then
6040 Error_Msg_N ("small already given for &", Nam);
6042 elsif Small > Delta_Value (U_Ent) then
6043 Error_Msg_N
6044 ("small value must not be greater than delta value", Nam);
6046 else
6047 Set_Small_Value (U_Ent, Small);
6048 Set_Small_Value (Implicit_Base, Small);
6049 Set_Has_Small_Clause (U_Ent);
6050 Set_Has_Small_Clause (Implicit_Base);
6051 Set_Has_Non_Standard_Rep (Implicit_Base);
6052 end if;
6053 end Small;
6055 ------------------
6056 -- Storage_Pool --
6057 ------------------
6059 -- Storage_Pool attribute definition clause
6061 when Attribute_Simple_Storage_Pool
6062 | Attribute_Storage_Pool
6064 Storage_Pool : declare
6065 Pool : Entity_Id;
6066 T : Entity_Id;
6068 begin
6069 if Ekind (U_Ent) = E_Access_Subprogram_Type then
6070 Error_Msg_N
6071 ("storage pool cannot be given for access-to-subprogram type",
6072 Nam);
6073 return;
6075 elsif not Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
6076 then
6077 Error_Msg_N
6078 ("storage pool can only be given for access types", Nam);
6079 return;
6081 elsif Is_Derived_Type (U_Ent) then
6082 Error_Msg_N
6083 ("storage pool cannot be given for a derived access type",
6084 Nam);
6086 elsif Duplicate_Clause then
6087 return;
6089 elsif Present (Associated_Storage_Pool (U_Ent)) then
6090 Error_Msg_N ("storage pool already given for &", Nam);
6091 return;
6092 end if;
6094 -- Check for Storage_Size previously given
6096 declare
6097 SS : constant Node_Id :=
6098 Get_Attribute_Definition_Clause
6099 (U_Ent, Attribute_Storage_Size);
6100 begin
6101 if Present (SS) then
6102 Check_Pool_Size_Clash (U_Ent, N, SS);
6103 end if;
6104 end;
6106 -- Storage_Pool case
6108 if Id = Attribute_Storage_Pool then
6109 Analyze_And_Resolve
6110 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6112 -- In the Simple_Storage_Pool case, we allow a variable of any
6113 -- simple storage pool type, so we Resolve without imposing an
6114 -- expected type.
6116 else
6117 Analyze_And_Resolve (Expr);
6119 if not Present (Get_Rep_Pragma
6120 (Etype (Expr), Name_Simple_Storage_Pool_Type))
6121 then
6122 Error_Msg_N
6123 ("expression must be of a simple storage pool type", Expr);
6124 end if;
6125 end if;
6127 if not Denotes_Variable (Expr) then
6128 Error_Msg_N ("storage pool must be a variable", Expr);
6129 return;
6130 end if;
6132 if Nkind (Expr) = N_Type_Conversion then
6133 T := Etype (Expression (Expr));
6134 else
6135 T := Etype (Expr);
6136 end if;
6138 -- The Stack_Bounded_Pool is used internally for implementing
6139 -- access types with a Storage_Size. Since it only work properly
6140 -- when used on one specific type, we need to check that it is not
6141 -- hijacked improperly:
6143 -- type T is access Integer;
6144 -- for T'Storage_Size use n;
6145 -- type Q is access Float;
6146 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
6148 if RTE_Available (RE_Stack_Bounded_Pool)
6149 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
6150 then
6151 Error_Msg_N ("non-shareable internal Pool", Expr);
6152 return;
6153 end if;
6155 -- If the argument is a name that is not an entity name, then
6156 -- we construct a renaming operation to define an entity of
6157 -- type storage pool.
6159 if not Is_Entity_Name (Expr)
6160 and then Is_Object_Reference (Expr)
6161 then
6162 Pool := Make_Temporary (Loc, 'P', Expr);
6164 declare
6165 Rnode : constant Node_Id :=
6166 Make_Object_Renaming_Declaration (Loc,
6167 Defining_Identifier => Pool,
6168 Subtype_Mark =>
6169 New_Occurrence_Of (Etype (Expr), Loc),
6170 Name => Expr);
6172 begin
6173 -- If the attribute definition clause comes from an aspect
6174 -- clause, then insert the renaming before the associated
6175 -- entity's declaration, since the attribute clause has
6176 -- not yet been appended to the declaration list.
6178 if From_Aspect_Specification (N) then
6179 Insert_Before (Parent (Entity (N)), Rnode);
6180 else
6181 Insert_Before (N, Rnode);
6182 end if;
6184 Analyze (Rnode);
6185 Set_Associated_Storage_Pool (U_Ent, Pool);
6186 end;
6188 elsif Is_Entity_Name (Expr) then
6189 Pool := Entity (Expr);
6191 -- If pool is a renamed object, get original one. This can
6192 -- happen with an explicit renaming, and within instances.
6194 while Present (Renamed_Object (Pool))
6195 and then Is_Entity_Name (Renamed_Object (Pool))
6196 loop
6197 Pool := Entity (Renamed_Object (Pool));
6198 end loop;
6200 if Present (Renamed_Object (Pool))
6201 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
6202 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
6203 then
6204 Pool := Entity (Expression (Renamed_Object (Pool)));
6205 end if;
6207 Set_Associated_Storage_Pool (U_Ent, Pool);
6209 elsif Nkind (Expr) = N_Type_Conversion
6210 and then Is_Entity_Name (Expression (Expr))
6211 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
6212 then
6213 Pool := Entity (Expression (Expr));
6214 Set_Associated_Storage_Pool (U_Ent, Pool);
6216 else
6217 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
6218 return;
6219 end if;
6220 end Storage_Pool;
6222 ------------------
6223 -- Storage_Size --
6224 ------------------
6226 -- Storage_Size attribute definition clause
6228 when Attribute_Storage_Size => Storage_Size : declare
6229 Btype : constant Entity_Id := Base_Type (U_Ent);
6231 begin
6232 if Is_Task_Type (U_Ent) then
6234 -- Check obsolescent (but never obsolescent if from aspect)
6236 if not From_Aspect_Specification (N) then
6237 Check_Restriction (No_Obsolescent_Features, N);
6239 if Warn_On_Obsolescent_Feature then
6240 Error_Msg_N
6241 ("?j?storage size clause for task is an obsolescent "
6242 & "feature (RM J.9)", N);
6243 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
6244 end if;
6245 end if;
6247 FOnly := True;
6248 end if;
6250 if not Is_Access_Type (U_Ent)
6251 and then Ekind (U_Ent) /= E_Task_Type
6252 then
6253 Error_Msg_N ("storage size cannot be given for &", Nam);
6255 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6256 Error_Msg_N
6257 ("storage size cannot be given for a derived access type",
6258 Nam);
6260 elsif Duplicate_Clause then
6261 null;
6263 else
6264 Analyze_And_Resolve (Expr, Any_Integer);
6266 if Is_Access_Type (U_Ent) then
6268 -- Check for Storage_Pool previously given
6270 declare
6271 SP : constant Node_Id :=
6272 Get_Attribute_Definition_Clause
6273 (U_Ent, Attribute_Storage_Pool);
6275 begin
6276 if Present (SP) then
6277 Check_Pool_Size_Clash (U_Ent, SP, N);
6278 end if;
6279 end;
6281 -- Special case of for x'Storage_Size use 0
6283 if Is_OK_Static_Expression (Expr)
6284 and then Expr_Value (Expr) = 0
6285 then
6286 Set_No_Pool_Assigned (Btype);
6287 end if;
6288 end if;
6290 Set_Has_Storage_Size_Clause (Btype);
6291 end if;
6292 end Storage_Size;
6294 -----------------
6295 -- Stream_Size --
6296 -----------------
6298 when Attribute_Stream_Size => Stream_Size : declare
6299 Size : constant Uint := Static_Integer (Expr);
6301 begin
6302 if Ada_Version <= Ada_95 then
6303 Check_Restriction (No_Implementation_Attributes, N);
6304 end if;
6306 if Duplicate_Clause then
6307 null;
6309 elsif Is_Elementary_Type (U_Ent) then
6311 -- The following errors are suppressed in ASIS mode to allow
6312 -- for different ASIS back ends or ASIS-based tools to query
6313 -- the illegal clause.
6315 if ASIS_Mode then
6316 null;
6318 elsif Size /= System_Storage_Unit
6319 and then Size /= System_Storage_Unit * 2
6320 and then Size /= System_Storage_Unit * 4
6321 and then Size /= System_Storage_Unit * 8
6322 then
6323 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6324 Error_Msg_N
6325 ("stream size for elementary type must be a power of 2 "
6326 & "and at least ^", N);
6328 elsif RM_Size (U_Ent) > Size then
6329 Error_Msg_Uint_1 := RM_Size (U_Ent);
6330 Error_Msg_N
6331 ("stream size for elementary type must be a power of 2 "
6332 & "and at least ^", N);
6333 end if;
6335 Set_Has_Stream_Size_Clause (U_Ent);
6337 else
6338 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6339 end if;
6340 end Stream_Size;
6342 ----------------
6343 -- Value_Size --
6344 ----------------
6346 -- Value_Size attribute definition clause
6348 when Attribute_Value_Size => Value_Size : declare
6349 Size : constant Uint := Static_Integer (Expr);
6350 Biased : Boolean;
6352 begin
6353 if not Is_Type (U_Ent) then
6354 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6356 elsif Duplicate_Clause then
6357 null;
6359 elsif Is_Array_Type (U_Ent)
6360 and then not Is_Constrained (U_Ent)
6361 then
6362 Error_Msg_N
6363 ("Value_Size cannot be given for unconstrained array", Nam);
6365 else
6366 if Is_Elementary_Type (U_Ent) then
6367 Check_Size (Expr, U_Ent, Size, Biased);
6368 Set_Biased (U_Ent, N, "value size clause", Biased);
6369 end if;
6371 Set_RM_Size (U_Ent, Size);
6372 end if;
6373 end Value_Size;
6375 -----------------------
6376 -- Variable_Indexing --
6377 -----------------------
6379 when Attribute_Variable_Indexing =>
6380 Check_Indexing_Functions;
6382 -----------
6383 -- Write --
6384 -----------
6386 when Attribute_Write =>
6387 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6388 Set_Has_Specified_Stream_Write (Ent);
6390 -- All other attributes cannot be set
6392 when others =>
6393 Error_Msg_N
6394 ("attribute& cannot be set with definition clause", N);
6395 end case;
6397 -- The test for the type being frozen must be performed after any
6398 -- expression the clause has been analyzed since the expression itself
6399 -- might cause freezing that makes the clause illegal.
6401 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6402 return;
6403 end if;
6404 end Analyze_Attribute_Definition_Clause;
6406 ----------------------------
6407 -- Analyze_Code_Statement --
6408 ----------------------------
6410 procedure Analyze_Code_Statement (N : Node_Id) is
6411 HSS : constant Node_Id := Parent (N);
6412 SBody : constant Node_Id := Parent (HSS);
6413 Subp : constant Entity_Id := Current_Scope;
6414 Stmt : Node_Id;
6415 Decl : Node_Id;
6416 StmtO : Node_Id;
6417 DeclO : Node_Id;
6419 begin
6420 -- Accept foreign code statements for CodePeer. The analysis is skipped
6421 -- to avoid rejecting unrecognized constructs.
6423 if CodePeer_Mode then
6424 Set_Analyzed (N);
6425 return;
6426 end if;
6428 -- Analyze and check we get right type, note that this implements the
6429 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6430 -- the only way that Asm_Insn could possibly be visible.
6432 Analyze_And_Resolve (Expression (N));
6434 if Etype (Expression (N)) = Any_Type then
6435 return;
6436 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6437 Error_Msg_N ("incorrect type for code statement", N);
6438 return;
6439 end if;
6441 Check_Code_Statement (N);
6443 -- Make sure we appear in the handled statement sequence of a subprogram
6444 -- (RM 13.8(3)).
6446 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6447 or else Nkind (SBody) /= N_Subprogram_Body
6448 then
6449 Error_Msg_N
6450 ("code statement can only appear in body of subprogram", N);
6451 return;
6452 end if;
6454 -- Do remaining checks (RM 13.8(3)) if not already done
6456 if not Is_Machine_Code_Subprogram (Subp) then
6457 Set_Is_Machine_Code_Subprogram (Subp);
6459 -- No exception handlers allowed
6461 if Present (Exception_Handlers (HSS)) then
6462 Error_Msg_N
6463 ("exception handlers not permitted in machine code subprogram",
6464 First (Exception_Handlers (HSS)));
6465 end if;
6467 -- No declarations other than use clauses and pragmas (we allow
6468 -- certain internally generated declarations as well).
6470 Decl := First (Declarations (SBody));
6471 while Present (Decl) loop
6472 DeclO := Original_Node (Decl);
6473 if Comes_From_Source (DeclO)
6474 and not Nkind_In (DeclO, N_Pragma,
6475 N_Use_Package_Clause,
6476 N_Use_Type_Clause,
6477 N_Implicit_Label_Declaration)
6478 then
6479 Error_Msg_N
6480 ("this declaration not allowed in machine code subprogram",
6481 DeclO);
6482 end if;
6484 Next (Decl);
6485 end loop;
6487 -- No statements other than code statements, pragmas, and labels.
6488 -- Again we allow certain internally generated statements.
6490 -- In Ada 2012, qualified expressions are names, and the code
6491 -- statement is initially parsed as a procedure call.
6493 Stmt := First (Statements (HSS));
6494 while Present (Stmt) loop
6495 StmtO := Original_Node (Stmt);
6497 -- A procedure call transformed into a code statement is OK
6499 if Ada_Version >= Ada_2012
6500 and then Nkind (StmtO) = N_Procedure_Call_Statement
6501 and then Nkind (Name (StmtO)) = N_Qualified_Expression
6502 then
6503 null;
6505 elsif Comes_From_Source (StmtO)
6506 and then not Nkind_In (StmtO, N_Pragma,
6507 N_Label,
6508 N_Code_Statement)
6509 then
6510 Error_Msg_N
6511 ("this statement is not allowed in machine code subprogram",
6512 StmtO);
6513 end if;
6515 Next (Stmt);
6516 end loop;
6517 end if;
6518 end Analyze_Code_Statement;
6520 -----------------------------------------------
6521 -- Analyze_Enumeration_Representation_Clause --
6522 -----------------------------------------------
6524 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6525 Ident : constant Node_Id := Identifier (N);
6526 Aggr : constant Node_Id := Array_Aggregate (N);
6527 Enumtype : Entity_Id;
6528 Elit : Entity_Id;
6529 Expr : Node_Id;
6530 Assoc : Node_Id;
6531 Choice : Node_Id;
6532 Val : Uint;
6534 Err : Boolean := False;
6535 -- Set True to avoid cascade errors and crashes on incorrect source code
6537 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6538 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6539 -- Allowed range of universal integer (= allowed range of enum lit vals)
6541 Min : Uint;
6542 Max : Uint;
6543 -- Minimum and maximum values of entries
6545 Max_Node : Node_Id := Empty; -- init to avoid warning
6546 -- Pointer to node for literal providing max value
6548 begin
6549 if Ignore_Rep_Clauses then
6550 Kill_Rep_Clause (N);
6551 return;
6552 end if;
6554 -- Ignore enumeration rep clauses by default in CodePeer mode,
6555 -- unless -gnatd.I is specified, as a work around for potential false
6556 -- positive messages.
6558 if CodePeer_Mode and not Debug_Flag_Dot_II then
6559 return;
6560 end if;
6562 -- First some basic error checks
6564 Find_Type (Ident);
6565 Enumtype := Entity (Ident);
6567 if Enumtype = Any_Type
6568 or else Rep_Item_Too_Early (Enumtype, N)
6569 then
6570 return;
6571 else
6572 Enumtype := Underlying_Type (Enumtype);
6573 end if;
6575 if not Is_Enumeration_Type (Enumtype) then
6576 Error_Msg_NE
6577 ("enumeration type required, found}",
6578 Ident, First_Subtype (Enumtype));
6579 return;
6580 end if;
6582 -- Ignore rep clause on generic actual type. This will already have
6583 -- been flagged on the template as an error, and this is the safest
6584 -- way to ensure we don't get a junk cascaded message in the instance.
6586 if Is_Generic_Actual_Type (Enumtype) then
6587 return;
6589 -- Type must be in current scope
6591 elsif Scope (Enumtype) /= Current_Scope then
6592 Error_Msg_N ("type must be declared in this scope", Ident);
6593 return;
6595 -- Type must be a first subtype
6597 elsif not Is_First_Subtype (Enumtype) then
6598 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6599 return;
6601 -- Ignore duplicate rep clause
6603 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6604 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6605 return;
6607 -- Don't allow rep clause for standard [wide_[wide_]]character
6609 elsif Is_Standard_Character_Type (Enumtype) then
6610 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
6611 return;
6613 -- Check that the expression is a proper aggregate (no parentheses)
6615 elsif Paren_Count (Aggr) /= 0 then
6616 Error_Msg
6617 ("extra parentheses surrounding aggregate not allowed",
6618 First_Sloc (Aggr));
6619 return;
6621 -- All tests passed, so set rep clause in place
6623 else
6624 Set_Has_Enumeration_Rep_Clause (Enumtype);
6625 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6626 end if;
6628 -- Now we process the aggregate. Note that we don't use the normal
6629 -- aggregate code for this purpose, because we don't want any of the
6630 -- normal expansion activities, and a number of special semantic
6631 -- rules apply (including the component type being any integer type)
6633 Elit := First_Literal (Enumtype);
6635 -- First the positional entries if any
6637 if Present (Expressions (Aggr)) then
6638 Expr := First (Expressions (Aggr));
6639 while Present (Expr) loop
6640 if No (Elit) then
6641 Error_Msg_N ("too many entries in aggregate", Expr);
6642 return;
6643 end if;
6645 Val := Static_Integer (Expr);
6647 -- Err signals that we found some incorrect entries processing
6648 -- the list. The final checks for completeness and ordering are
6649 -- skipped in this case.
6651 if Val = No_Uint then
6652 Err := True;
6654 elsif Val < Lo or else Hi < Val then
6655 Error_Msg_N ("value outside permitted range", Expr);
6656 Err := True;
6657 end if;
6659 Set_Enumeration_Rep (Elit, Val);
6660 Set_Enumeration_Rep_Expr (Elit, Expr);
6661 Next (Expr);
6662 Next (Elit);
6663 end loop;
6664 end if;
6666 -- Now process the named entries if present
6668 if Present (Component_Associations (Aggr)) then
6669 Assoc := First (Component_Associations (Aggr));
6670 while Present (Assoc) loop
6671 Choice := First (Choices (Assoc));
6673 if Present (Next (Choice)) then
6674 Error_Msg_N
6675 ("multiple choice not allowed here", Next (Choice));
6676 Err := True;
6677 end if;
6679 if Nkind (Choice) = N_Others_Choice then
6680 Error_Msg_N ("others choice not allowed here", Choice);
6681 Err := True;
6683 elsif Nkind (Choice) = N_Range then
6685 -- ??? should allow zero/one element range here
6687 Error_Msg_N ("range not allowed here", Choice);
6688 Err := True;
6690 else
6691 Analyze_And_Resolve (Choice, Enumtype);
6693 if Error_Posted (Choice) then
6694 Err := True;
6695 end if;
6697 if not Err then
6698 if Is_Entity_Name (Choice)
6699 and then Is_Type (Entity (Choice))
6700 then
6701 Error_Msg_N ("subtype name not allowed here", Choice);
6702 Err := True;
6704 -- ??? should allow static subtype with zero/one entry
6706 elsif Etype (Choice) = Base_Type (Enumtype) then
6707 if not Is_OK_Static_Expression (Choice) then
6708 Flag_Non_Static_Expr
6709 ("non-static expression used for choice!", Choice);
6710 Err := True;
6712 else
6713 Elit := Expr_Value_E (Choice);
6715 if Present (Enumeration_Rep_Expr (Elit)) then
6716 Error_Msg_Sloc :=
6717 Sloc (Enumeration_Rep_Expr (Elit));
6718 Error_Msg_NE
6719 ("representation for& previously given#",
6720 Choice, Elit);
6721 Err := True;
6722 end if;
6724 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
6726 Expr := Expression (Assoc);
6727 Val := Static_Integer (Expr);
6729 if Val = No_Uint then
6730 Err := True;
6732 elsif Val < Lo or else Hi < Val then
6733 Error_Msg_N ("value outside permitted range", Expr);
6734 Err := True;
6735 end if;
6737 Set_Enumeration_Rep (Elit, Val);
6738 end if;
6739 end if;
6740 end if;
6741 end if;
6743 Next (Assoc);
6744 end loop;
6745 end if;
6747 -- Aggregate is fully processed. Now we check that a full set of
6748 -- representations was given, and that they are in range and in order.
6749 -- These checks are only done if no other errors occurred.
6751 if not Err then
6752 Min := No_Uint;
6753 Max := No_Uint;
6755 Elit := First_Literal (Enumtype);
6756 while Present (Elit) loop
6757 if No (Enumeration_Rep_Expr (Elit)) then
6758 Error_Msg_NE ("missing representation for&!", N, Elit);
6760 else
6761 Val := Enumeration_Rep (Elit);
6763 if Min = No_Uint then
6764 Min := Val;
6765 end if;
6767 if Val /= No_Uint then
6768 if Max /= No_Uint and then Val <= Max then
6769 Error_Msg_NE
6770 ("enumeration value for& not ordered!",
6771 Enumeration_Rep_Expr (Elit), Elit);
6772 end if;
6774 Max_Node := Enumeration_Rep_Expr (Elit);
6775 Max := Val;
6776 end if;
6778 -- If there is at least one literal whose representation is not
6779 -- equal to the Pos value, then note that this enumeration type
6780 -- has a non-standard representation.
6782 if Val /= Enumeration_Pos (Elit) then
6783 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6784 end if;
6785 end if;
6787 Next (Elit);
6788 end loop;
6790 -- Now set proper size information
6792 declare
6793 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6795 begin
6796 if Has_Size_Clause (Enumtype) then
6798 -- All OK, if size is OK now
6800 if RM_Size (Enumtype) >= Minsize then
6801 null;
6803 else
6804 -- Try if we can get by with biasing
6806 Minsize :=
6807 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6809 -- Error message if even biasing does not work
6811 if RM_Size (Enumtype) < Minsize then
6812 Error_Msg_Uint_1 := RM_Size (Enumtype);
6813 Error_Msg_Uint_2 := Max;
6814 Error_Msg_N
6815 ("previously given size (^) is too small "
6816 & "for this value (^)", Max_Node);
6818 -- If biasing worked, indicate that we now have biased rep
6820 else
6821 Set_Biased
6822 (Enumtype, Size_Clause (Enumtype), "size clause");
6823 end if;
6824 end if;
6826 else
6827 Set_RM_Size (Enumtype, Minsize);
6828 Set_Enum_Esize (Enumtype);
6829 end if;
6831 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6832 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6833 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6834 end;
6835 end if;
6837 -- We repeat the too late test in case it froze itself
6839 if Rep_Item_Too_Late (Enumtype, N) then
6840 null;
6841 end if;
6842 end Analyze_Enumeration_Representation_Clause;
6844 ----------------------------
6845 -- Analyze_Free_Statement --
6846 ----------------------------
6848 procedure Analyze_Free_Statement (N : Node_Id) is
6849 begin
6850 Analyze (Expression (N));
6851 end Analyze_Free_Statement;
6853 ---------------------------
6854 -- Analyze_Freeze_Entity --
6855 ---------------------------
6857 procedure Analyze_Freeze_Entity (N : Node_Id) is
6858 begin
6859 Freeze_Entity_Checks (N);
6860 end Analyze_Freeze_Entity;
6862 -----------------------------------
6863 -- Analyze_Freeze_Generic_Entity --
6864 -----------------------------------
6866 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6867 E : constant Entity_Id := Entity (N);
6869 begin
6870 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
6871 Analyze_Aspects_At_Freeze_Point (E);
6872 end if;
6874 Freeze_Entity_Checks (N);
6875 end Analyze_Freeze_Generic_Entity;
6877 ------------------------------------------
6878 -- Analyze_Record_Representation_Clause --
6879 ------------------------------------------
6881 -- Note: we check as much as we can here, but we can't do any checks
6882 -- based on the position values (e.g. overlap checks) until freeze time
6883 -- because especially in Ada 2005 (machine scalar mode), the processing
6884 -- for non-standard bit order can substantially change the positions.
6885 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6886 -- for the remainder of this processing.
6888 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6889 Ident : constant Node_Id := Identifier (N);
6890 Biased : Boolean;
6891 CC : Node_Id;
6892 Comp : Entity_Id;
6893 Fbit : Uint;
6894 Hbit : Uint := Uint_0;
6895 Lbit : Uint;
6896 Ocomp : Entity_Id;
6897 Posit : Uint;
6898 Rectype : Entity_Id;
6899 Recdef : Node_Id;
6901 function Is_Inherited (Comp : Entity_Id) return Boolean;
6902 -- True if Comp is an inherited component in a record extension
6904 ------------------
6905 -- Is_Inherited --
6906 ------------------
6908 function Is_Inherited (Comp : Entity_Id) return Boolean is
6909 Comp_Base : Entity_Id;
6911 begin
6912 if Ekind (Rectype) = E_Record_Subtype then
6913 Comp_Base := Original_Record_Component (Comp);
6914 else
6915 Comp_Base := Comp;
6916 end if;
6918 return Comp_Base /= Original_Record_Component (Comp_Base);
6919 end Is_Inherited;
6921 -- Local variables
6923 Is_Record_Extension : Boolean;
6924 -- True if Rectype is a record extension
6926 CR_Pragma : Node_Id := Empty;
6927 -- Points to N_Pragma node if Complete_Representation pragma present
6929 -- Start of processing for Analyze_Record_Representation_Clause
6931 begin
6932 if Ignore_Rep_Clauses then
6933 Kill_Rep_Clause (N);
6934 return;
6935 end if;
6937 Find_Type (Ident);
6938 Rectype := Entity (Ident);
6940 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6941 return;
6942 else
6943 Rectype := Underlying_Type (Rectype);
6944 end if;
6946 -- First some basic error checks
6948 if not Is_Record_Type (Rectype) then
6949 Error_Msg_NE
6950 ("record type required, found}", Ident, First_Subtype (Rectype));
6951 return;
6953 elsif Scope (Rectype) /= Current_Scope then
6954 Error_Msg_N ("type must be declared in this scope", N);
6955 return;
6957 elsif not Is_First_Subtype (Rectype) then
6958 Error_Msg_N ("cannot give record rep clause for subtype", N);
6959 return;
6961 elsif Has_Record_Rep_Clause (Rectype) then
6962 Error_Msg_N ("duplicate record rep clause ignored", N);
6963 return;
6965 elsif Rep_Item_Too_Late (Rectype, N) then
6966 return;
6967 end if;
6969 -- We know we have a first subtype, now possibly go to the anonymous
6970 -- base type to determine whether Rectype is a record extension.
6972 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6973 Is_Record_Extension :=
6974 Nkind (Recdef) = N_Derived_Type_Definition
6975 and then Present (Record_Extension_Part (Recdef));
6977 if Present (Mod_Clause (N)) then
6978 declare
6979 Loc : constant Source_Ptr := Sloc (N);
6980 M : constant Node_Id := Mod_Clause (N);
6981 P : constant List_Id := Pragmas_Before (M);
6982 AtM_Nod : Node_Id;
6984 Mod_Val : Uint;
6985 pragma Warnings (Off, Mod_Val);
6987 begin
6988 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
6990 if Warn_On_Obsolescent_Feature then
6991 Error_Msg_N
6992 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6993 Error_Msg_N
6994 ("\?j?use alignment attribute definition clause instead", N);
6995 end if;
6997 if Present (P) then
6998 Analyze_List (P);
6999 end if;
7001 -- In ASIS_Mode mode, expansion is disabled, but we must convert
7002 -- the Mod clause into an alignment clause anyway, so that the
7003 -- back end can compute and back-annotate properly the size and
7004 -- alignment of types that may include this record.
7006 -- This seems dubious, this destroys the source tree in a manner
7007 -- not detectable by ASIS ???
7009 if Operating_Mode = Check_Semantics and then ASIS_Mode then
7010 AtM_Nod :=
7011 Make_Attribute_Definition_Clause (Loc,
7012 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
7013 Chars => Name_Alignment,
7014 Expression => Relocate_Node (Expression (M)));
7016 Set_From_At_Mod (AtM_Nod);
7017 Insert_After (N, AtM_Nod);
7018 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
7019 Set_Mod_Clause (N, Empty);
7021 else
7022 -- Get the alignment value to perform error checking
7024 Mod_Val := Get_Alignment_Value (Expression (M));
7025 end if;
7026 end;
7027 end if;
7029 -- For untagged types, clear any existing component clauses for the
7030 -- type. If the type is derived, this is what allows us to override
7031 -- a rep clause for the parent. For type extensions, the representation
7032 -- of the inherited components is inherited, so we want to keep previous
7033 -- component clauses for completeness.
7035 if not Is_Tagged_Type (Rectype) then
7036 Comp := First_Component_Or_Discriminant (Rectype);
7037 while Present (Comp) loop
7038 Set_Component_Clause (Comp, Empty);
7039 Next_Component_Or_Discriminant (Comp);
7040 end loop;
7041 end if;
7043 -- All done if no component clauses
7045 CC := First (Component_Clauses (N));
7047 if No (CC) then
7048 return;
7049 end if;
7051 -- A representation like this applies to the base type
7053 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
7054 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
7055 Set_Has_Specified_Layout (Base_Type (Rectype));
7057 -- Process the component clauses
7059 while Present (CC) loop
7061 -- Pragma
7063 if Nkind (CC) = N_Pragma then
7064 Analyze (CC);
7066 -- The only pragma of interest is Complete_Representation
7068 if Pragma_Name (CC) = Name_Complete_Representation then
7069 CR_Pragma := CC;
7070 end if;
7072 -- Processing for real component clause
7074 else
7075 Posit := Static_Integer (Position (CC));
7076 Fbit := Static_Integer (First_Bit (CC));
7077 Lbit := Static_Integer (Last_Bit (CC));
7079 if Posit /= No_Uint
7080 and then Fbit /= No_Uint
7081 and then Lbit /= No_Uint
7082 then
7083 if Posit < 0 then
7084 Error_Msg_N ("position cannot be negative", Position (CC));
7086 elsif Fbit < 0 then
7087 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
7089 -- The Last_Bit specified in a component clause must not be
7090 -- less than the First_Bit minus one (RM-13.5.1(10)).
7092 elsif Lbit < Fbit - 1 then
7093 Error_Msg_N
7094 ("last bit cannot be less than first bit minus one",
7095 Last_Bit (CC));
7097 -- Values look OK, so find the corresponding record component
7098 -- Even though the syntax allows an attribute reference for
7099 -- implementation-defined components, GNAT does not allow the
7100 -- tag to get an explicit position.
7102 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
7103 if Attribute_Name (Component_Name (CC)) = Name_Tag then
7104 Error_Msg_N ("position of tag cannot be specified", CC);
7105 else
7106 Error_Msg_N ("illegal component name", CC);
7107 end if;
7109 else
7110 Comp := First_Entity (Rectype);
7111 while Present (Comp) loop
7112 exit when Chars (Comp) = Chars (Component_Name (CC));
7113 Next_Entity (Comp);
7114 end loop;
7116 if No (Comp) then
7118 -- Maybe component of base type that is absent from
7119 -- statically constrained first subtype.
7121 Comp := First_Entity (Base_Type (Rectype));
7122 while Present (Comp) loop
7123 exit when Chars (Comp) = Chars (Component_Name (CC));
7124 Next_Entity (Comp);
7125 end loop;
7126 end if;
7128 if No (Comp) then
7129 Error_Msg_N
7130 ("component clause is for non-existent field", CC);
7132 -- Ada 2012 (AI05-0026): Any name that denotes a
7133 -- discriminant of an object of an unchecked union type
7134 -- shall not occur within a record_representation_clause.
7136 -- The general restriction of using record rep clauses on
7137 -- Unchecked_Union types has now been lifted. Since it is
7138 -- possible to introduce a record rep clause which mentions
7139 -- the discriminant of an Unchecked_Union in non-Ada 2012
7140 -- code, this check is applied to all versions of the
7141 -- language.
7143 elsif Ekind (Comp) = E_Discriminant
7144 and then Is_Unchecked_Union (Rectype)
7145 then
7146 Error_Msg_N
7147 ("cannot reference discriminant of unchecked union",
7148 Component_Name (CC));
7150 elsif Is_Record_Extension and then Is_Inherited (Comp) then
7151 Error_Msg_NE
7152 ("component clause not allowed for inherited "
7153 & "component&", CC, Comp);
7155 elsif Present (Component_Clause (Comp)) then
7157 -- Diagnose duplicate rep clause, or check consistency
7158 -- if this is an inherited component. In a double fault,
7159 -- there may be a duplicate inconsistent clause for an
7160 -- inherited component.
7162 if Scope (Original_Record_Component (Comp)) = Rectype
7163 or else Parent (Component_Clause (Comp)) = N
7164 then
7165 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
7166 Error_Msg_N ("component clause previously given#", CC);
7168 else
7169 declare
7170 Rep1 : constant Node_Id := Component_Clause (Comp);
7171 begin
7172 if Intval (Position (Rep1)) /=
7173 Intval (Position (CC))
7174 or else Intval (First_Bit (Rep1)) /=
7175 Intval (First_Bit (CC))
7176 or else Intval (Last_Bit (Rep1)) /=
7177 Intval (Last_Bit (CC))
7178 then
7179 Error_Msg_N
7180 ("component clause inconsistent with "
7181 & "representation of ancestor", CC);
7183 elsif Warn_On_Redundant_Constructs then
7184 Error_Msg_N
7185 ("?r?redundant confirming component clause "
7186 & "for component!", CC);
7187 end if;
7188 end;
7189 end if;
7191 -- Normal case where this is the first component clause we
7192 -- have seen for this entity, so set it up properly.
7194 else
7195 -- Make reference for field in record rep clause and set
7196 -- appropriate entity field in the field identifier.
7198 Generate_Reference
7199 (Comp, Component_Name (CC), Set_Ref => False);
7200 Set_Entity (Component_Name (CC), Comp);
7202 -- Update Fbit and Lbit to the actual bit number
7204 Fbit := Fbit + UI_From_Int (SSU) * Posit;
7205 Lbit := Lbit + UI_From_Int (SSU) * Posit;
7207 if Has_Size_Clause (Rectype)
7208 and then RM_Size (Rectype) <= Lbit
7209 then
7210 Error_Msg_N
7211 ("bit number out of range of specified size",
7212 Last_Bit (CC));
7213 else
7214 Set_Component_Clause (Comp, CC);
7215 Set_Component_Bit_Offset (Comp, Fbit);
7216 Set_Esize (Comp, 1 + (Lbit - Fbit));
7217 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
7218 Set_Normalized_Position (Comp, Fbit / SSU);
7220 if Warn_On_Overridden_Size
7221 and then Has_Size_Clause (Etype (Comp))
7222 and then RM_Size (Etype (Comp)) /= Esize (Comp)
7223 then
7224 Error_Msg_NE
7225 ("?S?component size overrides size clause for&",
7226 Component_Name (CC), Etype (Comp));
7227 end if;
7229 -- This information is also set in the corresponding
7230 -- component of the base type, found by accessing the
7231 -- Original_Record_Component link if it is present.
7233 Ocomp := Original_Record_Component (Comp);
7235 if Hbit < Lbit then
7236 Hbit := Lbit;
7237 end if;
7239 Check_Size
7240 (Component_Name (CC),
7241 Etype (Comp),
7242 Esize (Comp),
7243 Biased);
7245 Set_Biased
7246 (Comp, First_Node (CC), "component clause", Biased);
7248 if Present (Ocomp) then
7249 Set_Component_Clause (Ocomp, CC);
7250 Set_Component_Bit_Offset (Ocomp, Fbit);
7251 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
7252 Set_Normalized_Position (Ocomp, Fbit / SSU);
7253 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7255 Set_Normalized_Position_Max
7256 (Ocomp, Normalized_Position (Ocomp));
7258 -- Note: we don't use Set_Biased here, because we
7259 -- already gave a warning above if needed, and we
7260 -- would get a duplicate for the same name here.
7262 Set_Has_Biased_Representation
7263 (Ocomp, Has_Biased_Representation (Comp));
7264 end if;
7266 if Esize (Comp) < 0 then
7267 Error_Msg_N ("component size is negative", CC);
7268 end if;
7269 end if;
7270 end if;
7271 end if;
7272 end if;
7273 end if;
7275 Next (CC);
7276 end loop;
7278 -- Check missing components if Complete_Representation pragma appeared
7280 if Present (CR_Pragma) then
7281 Comp := First_Component_Or_Discriminant (Rectype);
7282 while Present (Comp) loop
7283 if No (Component_Clause (Comp)) then
7284 Error_Msg_NE
7285 ("missing component clause for &", CR_Pragma, Comp);
7286 end if;
7288 Next_Component_Or_Discriminant (Comp);
7289 end loop;
7291 -- Give missing components warning if required
7293 elsif Warn_On_Unrepped_Components then
7294 declare
7295 Num_Repped_Components : Nat := 0;
7296 Num_Unrepped_Components : Nat := 0;
7298 begin
7299 -- First count number of repped and unrepped components
7301 Comp := First_Component_Or_Discriminant (Rectype);
7302 while Present (Comp) loop
7303 if Present (Component_Clause (Comp)) then
7304 Num_Repped_Components := Num_Repped_Components + 1;
7305 else
7306 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7307 end if;
7309 Next_Component_Or_Discriminant (Comp);
7310 end loop;
7312 -- We are only interested in the case where there is at least one
7313 -- unrepped component, and at least half the components have rep
7314 -- clauses. We figure that if less than half have them, then the
7315 -- partial rep clause is really intentional. If the component
7316 -- type has no underlying type set at this point (as for a generic
7317 -- formal type), we don't know enough to give a warning on the
7318 -- component.
7320 if Num_Unrepped_Components > 0
7321 and then Num_Unrepped_Components < Num_Repped_Components
7322 then
7323 Comp := First_Component_Or_Discriminant (Rectype);
7324 while Present (Comp) loop
7325 if No (Component_Clause (Comp))
7326 and then Comes_From_Source (Comp)
7327 and then Present (Underlying_Type (Etype (Comp)))
7328 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
7329 or else Size_Known_At_Compile_Time
7330 (Underlying_Type (Etype (Comp))))
7331 and then not Has_Warnings_Off (Rectype)
7333 -- Ignore discriminant in unchecked union, since it is
7334 -- not there, and cannot have a component clause.
7336 and then (not Is_Unchecked_Union (Rectype)
7337 or else Ekind (Comp) /= E_Discriminant)
7338 then
7339 Error_Msg_Sloc := Sloc (Comp);
7340 Error_Msg_NE
7341 ("?C?no component clause given for & declared #",
7342 N, Comp);
7343 end if;
7345 Next_Component_Or_Discriminant (Comp);
7346 end loop;
7347 end if;
7348 end;
7349 end if;
7350 end Analyze_Record_Representation_Clause;
7352 -------------------------------------
7353 -- Build_Discrete_Static_Predicate --
7354 -------------------------------------
7356 procedure Build_Discrete_Static_Predicate
7357 (Typ : Entity_Id;
7358 Expr : Node_Id;
7359 Nam : Name_Id)
7361 Loc : constant Source_Ptr := Sloc (Expr);
7363 Non_Static : exception;
7364 -- Raised if something non-static is found
7366 Btyp : constant Entity_Id := Base_Type (Typ);
7368 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7369 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7370 -- Low bound and high bound value of base type of Typ
7372 TLo : Uint;
7373 THi : Uint;
7374 -- Bounds for constructing the static predicate. We use the bound of the
7375 -- subtype if it is static, otherwise the corresponding base type bound.
7376 -- Note: a non-static subtype can have a static predicate.
7378 type REnt is record
7379 Lo, Hi : Uint;
7380 end record;
7381 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7382 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7383 -- value.
7385 type RList is array (Nat range <>) of REnt;
7386 -- A list of ranges. The ranges are sorted in increasing order, and are
7387 -- disjoint (there is a gap of at least one value between each range in
7388 -- the table). A value is in the set of ranges in Rlist if it lies
7389 -- within one of these ranges.
7391 False_Range : constant RList :=
7392 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7393 -- An empty set of ranges represents a range list that can never be
7394 -- satisfied, since there are no ranges in which the value could lie,
7395 -- so it does not lie in any of them. False_Range is a canonical value
7396 -- for this empty set, but general processing should test for an Rlist
7397 -- with length zero (see Is_False predicate), since other null ranges
7398 -- may appear which must be treated as False.
7400 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7401 -- Range representing True, value must be in the base range
7403 function "and" (Left : RList; Right : RList) return RList;
7404 -- And's together two range lists, returning a range list. This is a set
7405 -- intersection operation.
7407 function "or" (Left : RList; Right : RList) return RList;
7408 -- Or's together two range lists, returning a range list. This is a set
7409 -- union operation.
7411 function "not" (Right : RList) return RList;
7412 -- Returns complement of a given range list, i.e. a range list
7413 -- representing all the values in TLo .. THi that are not in the input
7414 -- operand Right.
7416 function Build_Val (V : Uint) return Node_Id;
7417 -- Return an analyzed N_Identifier node referencing this value, suitable
7418 -- for use as an entry in the Static_Discrte_Predicate list. This node
7419 -- is typed with the base type.
7421 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7422 -- Return an analyzed N_Range node referencing this range, suitable for
7423 -- use as an entry in the Static_Discrete_Predicate list. This node is
7424 -- typed with the base type.
7426 function Get_RList (Exp : Node_Id) return RList;
7427 -- This is a recursive routine that converts the given expression into a
7428 -- list of ranges, suitable for use in building the static predicate.
7430 function Is_False (R : RList) return Boolean;
7431 pragma Inline (Is_False);
7432 -- Returns True if the given range list is empty, and thus represents a
7433 -- False list of ranges that can never be satisfied.
7435 function Is_True (R : RList) return Boolean;
7436 -- Returns True if R trivially represents the True predicate by having a
7437 -- single range from BLo to BHi.
7439 function Is_Type_Ref (N : Node_Id) return Boolean;
7440 pragma Inline (Is_Type_Ref);
7441 -- Returns if True if N is a reference to the type for the predicate in
7442 -- the expression (i.e. if it is an identifier whose Chars field matches
7443 -- the Nam given in the call). N must not be parenthesized, if the type
7444 -- name appears in parens, this routine will return False.
7446 function Lo_Val (N : Node_Id) return Uint;
7447 -- Given an entry from a Static_Discrete_Predicate list that is either
7448 -- a static expression or static range, gets either the expression value
7449 -- or the low bound of the range.
7451 function Hi_Val (N : Node_Id) return Uint;
7452 -- Given an entry from a Static_Discrete_Predicate list that is either
7453 -- a static expression or static range, gets either the expression value
7454 -- or the high bound of the range.
7456 function Membership_Entry (N : Node_Id) return RList;
7457 -- Given a single membership entry (range, value, or subtype), returns
7458 -- the corresponding range list. Raises Static_Error if not static.
7460 function Membership_Entries (N : Node_Id) return RList;
7461 -- Given an element on an alternatives list of a membership operation,
7462 -- returns the range list corresponding to this entry and all following
7463 -- entries (i.e. returns the "or" of this list of values).
7465 function Stat_Pred (Typ : Entity_Id) return RList;
7466 -- Given a type, if it has a static predicate, then return the predicate
7467 -- as a range list, otherwise raise Non_Static.
7469 -----------
7470 -- "and" --
7471 -----------
7473 function "and" (Left : RList; Right : RList) return RList is
7474 FEnt : REnt;
7475 -- First range of result
7477 SLeft : Nat := Left'First;
7478 -- Start of rest of left entries
7480 SRight : Nat := Right'First;
7481 -- Start of rest of right entries
7483 begin
7484 -- If either range is True, return the other
7486 if Is_True (Left) then
7487 return Right;
7488 elsif Is_True (Right) then
7489 return Left;
7490 end if;
7492 -- If either range is False, return False
7494 if Is_False (Left) or else Is_False (Right) then
7495 return False_Range;
7496 end if;
7498 -- Loop to remove entries at start that are disjoint, and thus just
7499 -- get discarded from the result entirely.
7501 loop
7502 -- If no operands left in either operand, result is false
7504 if SLeft > Left'Last or else SRight > Right'Last then
7505 return False_Range;
7507 -- Discard first left operand entry if disjoint with right
7509 elsif Left (SLeft).Hi < Right (SRight).Lo then
7510 SLeft := SLeft + 1;
7512 -- Discard first right operand entry if disjoint with left
7514 elsif Right (SRight).Hi < Left (SLeft).Lo then
7515 SRight := SRight + 1;
7517 -- Otherwise we have an overlapping entry
7519 else
7520 exit;
7521 end if;
7522 end loop;
7524 -- Now we have two non-null operands, and first entries overlap. The
7525 -- first entry in the result will be the overlapping part of these
7526 -- two entries.
7528 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7529 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
7531 -- Now we can remove the entry that ended at a lower value, since its
7532 -- contribution is entirely contained in Fent.
7534 if Left (SLeft).Hi <= Right (SRight).Hi then
7535 SLeft := SLeft + 1;
7536 else
7537 SRight := SRight + 1;
7538 end if;
7540 -- Compute result by concatenating this first entry with the "and" of
7541 -- the remaining parts of the left and right operands. Note that if
7542 -- either of these is empty, "and" will yield empty, so that we will
7543 -- end up with just Fent, which is what we want in that case.
7545 return
7546 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7547 end "and";
7549 -----------
7550 -- "not" --
7551 -----------
7553 function "not" (Right : RList) return RList is
7554 begin
7555 -- Return True if False range
7557 if Is_False (Right) then
7558 return True_Range;
7559 end if;
7561 -- Return False if True range
7563 if Is_True (Right) then
7564 return False_Range;
7565 end if;
7567 -- Here if not trivial case
7569 declare
7570 Result : RList (1 .. Right'Length + 1);
7571 -- May need one more entry for gap at beginning and end
7573 Count : Nat := 0;
7574 -- Number of entries stored in Result
7576 begin
7577 -- Gap at start
7579 if Right (Right'First).Lo > TLo then
7580 Count := Count + 1;
7581 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7582 end if;
7584 -- Gaps between ranges
7586 for J in Right'First .. Right'Last - 1 loop
7587 Count := Count + 1;
7588 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7589 end loop;
7591 -- Gap at end
7593 if Right (Right'Last).Hi < THi then
7594 Count := Count + 1;
7595 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7596 end if;
7598 return Result (1 .. Count);
7599 end;
7600 end "not";
7602 ----------
7603 -- "or" --
7604 ----------
7606 function "or" (Left : RList; Right : RList) return RList is
7607 FEnt : REnt;
7608 -- First range of result
7610 SLeft : Nat := Left'First;
7611 -- Start of rest of left entries
7613 SRight : Nat := Right'First;
7614 -- Start of rest of right entries
7616 begin
7617 -- If either range is True, return True
7619 if Is_True (Left) or else Is_True (Right) then
7620 return True_Range;
7621 end if;
7623 -- If either range is False (empty), return the other
7625 if Is_False (Left) then
7626 return Right;
7627 elsif Is_False (Right) then
7628 return Left;
7629 end if;
7631 -- Initialize result first entry from left or right operand depending
7632 -- on which starts with the lower range.
7634 if Left (SLeft).Lo < Right (SRight).Lo then
7635 FEnt := Left (SLeft);
7636 SLeft := SLeft + 1;
7637 else
7638 FEnt := Right (SRight);
7639 SRight := SRight + 1;
7640 end if;
7642 -- This loop eats ranges from left and right operands that are
7643 -- contiguous with the first range we are gathering.
7645 loop
7646 -- Eat first entry in left operand if contiguous or overlapped by
7647 -- gathered first operand of result.
7649 if SLeft <= Left'Last
7650 and then Left (SLeft).Lo <= FEnt.Hi + 1
7651 then
7652 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7653 SLeft := SLeft + 1;
7655 -- Eat first entry in right operand if contiguous or overlapped by
7656 -- gathered right operand of result.
7658 elsif SRight <= Right'Last
7659 and then Right (SRight).Lo <= FEnt.Hi + 1
7660 then
7661 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7662 SRight := SRight + 1;
7664 -- All done if no more entries to eat
7666 else
7667 exit;
7668 end if;
7669 end loop;
7671 -- Obtain result as the first entry we just computed, concatenated
7672 -- to the "or" of the remaining results (if one operand is empty,
7673 -- this will just concatenate with the other
7675 return
7676 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7677 end "or";
7679 -----------------
7680 -- Build_Range --
7681 -----------------
7683 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7684 Result : Node_Id;
7685 begin
7686 Result :=
7687 Make_Range (Loc,
7688 Low_Bound => Build_Val (Lo),
7689 High_Bound => Build_Val (Hi));
7690 Set_Etype (Result, Btyp);
7691 Set_Analyzed (Result);
7692 return Result;
7693 end Build_Range;
7695 ---------------
7696 -- Build_Val --
7697 ---------------
7699 function Build_Val (V : Uint) return Node_Id is
7700 Result : Node_Id;
7702 begin
7703 if Is_Enumeration_Type (Typ) then
7704 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7705 else
7706 Result := Make_Integer_Literal (Loc, V);
7707 end if;
7709 Set_Etype (Result, Btyp);
7710 Set_Is_Static_Expression (Result);
7711 Set_Analyzed (Result);
7712 return Result;
7713 end Build_Val;
7715 ---------------
7716 -- Get_RList --
7717 ---------------
7719 function Get_RList (Exp : Node_Id) return RList is
7720 Op : Node_Kind;
7721 Val : Uint;
7723 begin
7724 -- Static expression can only be true or false
7726 if Is_OK_Static_Expression (Exp) then
7727 if Expr_Value (Exp) = 0 then
7728 return False_Range;
7729 else
7730 return True_Range;
7731 end if;
7732 end if;
7734 -- Otherwise test node type
7736 Op := Nkind (Exp);
7738 case Op is
7740 -- And
7742 when N_And_Then
7743 | N_Op_And
7745 return Get_RList (Left_Opnd (Exp))
7747 Get_RList (Right_Opnd (Exp));
7749 -- Or
7751 when N_Op_Or
7752 | N_Or_Else
7754 return Get_RList (Left_Opnd (Exp))
7756 Get_RList (Right_Opnd (Exp));
7758 -- Not
7760 when N_Op_Not =>
7761 return not Get_RList (Right_Opnd (Exp));
7763 -- Comparisons of type with static value
7765 when N_Op_Compare =>
7767 -- Type is left operand
7769 if Is_Type_Ref (Left_Opnd (Exp))
7770 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7771 then
7772 Val := Expr_Value (Right_Opnd (Exp));
7774 -- Typ is right operand
7776 elsif Is_Type_Ref (Right_Opnd (Exp))
7777 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7778 then
7779 Val := Expr_Value (Left_Opnd (Exp));
7781 -- Invert sense of comparison
7783 case Op is
7784 when N_Op_Gt => Op := N_Op_Lt;
7785 when N_Op_Lt => Op := N_Op_Gt;
7786 when N_Op_Ge => Op := N_Op_Le;
7787 when N_Op_Le => Op := N_Op_Ge;
7788 when others => null;
7789 end case;
7791 -- Other cases are non-static
7793 else
7794 raise Non_Static;
7795 end if;
7797 -- Construct range according to comparison operation
7799 case Op is
7800 when N_Op_Eq =>
7801 return RList'(1 => REnt'(Val, Val));
7803 when N_Op_Ge =>
7804 return RList'(1 => REnt'(Val, BHi));
7806 when N_Op_Gt =>
7807 return RList'(1 => REnt'(Val + 1, BHi));
7809 when N_Op_Le =>
7810 return RList'(1 => REnt'(BLo, Val));
7812 when N_Op_Lt =>
7813 return RList'(1 => REnt'(BLo, Val - 1));
7815 when N_Op_Ne =>
7816 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
7818 when others =>
7819 raise Program_Error;
7820 end case;
7822 -- Membership (IN)
7824 when N_In =>
7825 if not Is_Type_Ref (Left_Opnd (Exp)) then
7826 raise Non_Static;
7827 end if;
7829 if Present (Right_Opnd (Exp)) then
7830 return Membership_Entry (Right_Opnd (Exp));
7831 else
7832 return Membership_Entries (First (Alternatives (Exp)));
7833 end if;
7835 -- Negative membership (NOT IN)
7837 when N_Not_In =>
7838 if not Is_Type_Ref (Left_Opnd (Exp)) then
7839 raise Non_Static;
7840 end if;
7842 if Present (Right_Opnd (Exp)) then
7843 return not Membership_Entry (Right_Opnd (Exp));
7844 else
7845 return not Membership_Entries (First (Alternatives (Exp)));
7846 end if;
7848 -- Function call, may be call to static predicate
7850 when N_Function_Call =>
7851 if Is_Entity_Name (Name (Exp)) then
7852 declare
7853 Ent : constant Entity_Id := Entity (Name (Exp));
7854 begin
7855 if Is_Predicate_Function (Ent)
7856 or else
7857 Is_Predicate_Function_M (Ent)
7858 then
7859 return Stat_Pred (Etype (First_Formal (Ent)));
7860 end if;
7861 end;
7862 end if;
7864 -- Other function call cases are non-static
7866 raise Non_Static;
7868 -- Qualified expression, dig out the expression
7870 when N_Qualified_Expression =>
7871 return Get_RList (Expression (Exp));
7873 when N_Case_Expression =>
7874 declare
7875 Alt : Node_Id;
7876 Choices : List_Id;
7877 Dep : Node_Id;
7879 begin
7880 if not Is_Entity_Name (Expression (Expr))
7881 or else Etype (Expression (Expr)) /= Typ
7882 then
7883 Error_Msg_N
7884 ("expression must denaote subtype", Expression (Expr));
7885 return False_Range;
7886 end if;
7888 -- Collect discrete choices in all True alternatives
7890 Choices := New_List;
7891 Alt := First (Alternatives (Exp));
7892 while Present (Alt) loop
7893 Dep := Expression (Alt);
7895 if not Is_OK_Static_Expression (Dep) then
7896 raise Non_Static;
7898 elsif Is_True (Expr_Value (Dep)) then
7899 Append_List_To (Choices,
7900 New_Copy_List (Discrete_Choices (Alt)));
7901 end if;
7903 Next (Alt);
7904 end loop;
7906 return Membership_Entries (First (Choices));
7907 end;
7909 -- Expression with actions: if no actions, dig out expression
7911 when N_Expression_With_Actions =>
7912 if Is_Empty_List (Actions (Exp)) then
7913 return Get_RList (Expression (Exp));
7914 else
7915 raise Non_Static;
7916 end if;
7918 -- Xor operator
7920 when N_Op_Xor =>
7921 return (Get_RList (Left_Opnd (Exp))
7922 and not Get_RList (Right_Opnd (Exp)))
7923 or (Get_RList (Right_Opnd (Exp))
7924 and not Get_RList (Left_Opnd (Exp)));
7926 -- Any other node type is non-static
7928 when others =>
7929 raise Non_Static;
7930 end case;
7931 end Get_RList;
7933 ------------
7934 -- Hi_Val --
7935 ------------
7937 function Hi_Val (N : Node_Id) return Uint is
7938 begin
7939 if Is_OK_Static_Expression (N) then
7940 return Expr_Value (N);
7941 else
7942 pragma Assert (Nkind (N) = N_Range);
7943 return Expr_Value (High_Bound (N));
7944 end if;
7945 end Hi_Val;
7947 --------------
7948 -- Is_False --
7949 --------------
7951 function Is_False (R : RList) return Boolean is
7952 begin
7953 return R'Length = 0;
7954 end Is_False;
7956 -------------
7957 -- Is_True --
7958 -------------
7960 function Is_True (R : RList) return Boolean is
7961 begin
7962 return R'Length = 1
7963 and then R (R'First).Lo = BLo
7964 and then R (R'First).Hi = BHi;
7965 end Is_True;
7967 -----------------
7968 -- Is_Type_Ref --
7969 -----------------
7971 function Is_Type_Ref (N : Node_Id) return Boolean is
7972 begin
7973 return Nkind (N) = N_Identifier
7974 and then Chars (N) = Nam
7975 and then Paren_Count (N) = 0;
7976 end Is_Type_Ref;
7978 ------------
7979 -- Lo_Val --
7980 ------------
7982 function Lo_Val (N : Node_Id) return Uint is
7983 begin
7984 if Is_OK_Static_Expression (N) then
7985 return Expr_Value (N);
7986 else
7987 pragma Assert (Nkind (N) = N_Range);
7988 return Expr_Value (Low_Bound (N));
7989 end if;
7990 end Lo_Val;
7992 ------------------------
7993 -- Membership_Entries --
7994 ------------------------
7996 function Membership_Entries (N : Node_Id) return RList is
7997 begin
7998 if No (Next (N)) then
7999 return Membership_Entry (N);
8000 else
8001 return Membership_Entry (N) or Membership_Entries (Next (N));
8002 end if;
8003 end Membership_Entries;
8005 ----------------------
8006 -- Membership_Entry --
8007 ----------------------
8009 function Membership_Entry (N : Node_Id) return RList is
8010 Val : Uint;
8011 SLo : Uint;
8012 SHi : Uint;
8014 begin
8015 -- Range case
8017 if Nkind (N) = N_Range then
8018 if not Is_OK_Static_Expression (Low_Bound (N))
8019 or else
8020 not Is_OK_Static_Expression (High_Bound (N))
8021 then
8022 raise Non_Static;
8023 else
8024 SLo := Expr_Value (Low_Bound (N));
8025 SHi := Expr_Value (High_Bound (N));
8026 return RList'(1 => REnt'(SLo, SHi));
8027 end if;
8029 -- Static expression case
8031 elsif Is_OK_Static_Expression (N) then
8032 Val := Expr_Value (N);
8033 return RList'(1 => REnt'(Val, Val));
8035 -- Identifier (other than static expression) case
8037 else pragma Assert (Nkind (N) = N_Identifier);
8039 -- Type case
8041 if Is_Type (Entity (N)) then
8043 -- If type has predicates, process them
8045 if Has_Predicates (Entity (N)) then
8046 return Stat_Pred (Entity (N));
8048 -- For static subtype without predicates, get range
8050 elsif Is_OK_Static_Subtype (Entity (N)) then
8051 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
8052 SHi := Expr_Value (Type_High_Bound (Entity (N)));
8053 return RList'(1 => REnt'(SLo, SHi));
8055 -- Any other type makes us non-static
8057 else
8058 raise Non_Static;
8059 end if;
8061 -- Any other kind of identifier in predicate (e.g. a non-static
8062 -- expression value) means this is not a static predicate.
8064 else
8065 raise Non_Static;
8066 end if;
8067 end if;
8068 end Membership_Entry;
8070 ---------------
8071 -- Stat_Pred --
8072 ---------------
8074 function Stat_Pred (Typ : Entity_Id) return RList is
8075 begin
8076 -- Not static if type does not have static predicates
8078 if not Has_Static_Predicate (Typ) then
8079 raise Non_Static;
8080 end if;
8082 -- Otherwise we convert the predicate list to a range list
8084 declare
8085 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
8086 Result : RList (1 .. List_Length (Spred));
8087 P : Node_Id;
8089 begin
8090 P := First (Static_Discrete_Predicate (Typ));
8091 for J in Result'Range loop
8092 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
8093 Next (P);
8094 end loop;
8096 return Result;
8097 end;
8098 end Stat_Pred;
8100 -- Start of processing for Build_Discrete_Static_Predicate
8102 begin
8103 -- Establish bounds for the predicate
8105 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8106 TLo := Expr_Value (Type_Low_Bound (Typ));
8107 else
8108 TLo := BLo;
8109 end if;
8111 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8112 THi := Expr_Value (Type_High_Bound (Typ));
8113 else
8114 THi := BHi;
8115 end if;
8117 -- Analyze the expression to see if it is a static predicate
8119 declare
8120 Ranges : constant RList := Get_RList (Expr);
8121 -- Range list from expression if it is static
8123 Plist : List_Id;
8125 begin
8126 -- Convert range list into a form for the static predicate. In the
8127 -- Ranges array, we just have raw ranges, these must be converted
8128 -- to properly typed and analyzed static expressions or range nodes.
8130 -- Note: here we limit ranges to the ranges of the subtype, so that
8131 -- a predicate is always false for values outside the subtype. That
8132 -- seems fine, such values are invalid anyway, and considering them
8133 -- to fail the predicate seems allowed and friendly, and furthermore
8134 -- simplifies processing for case statements and loops.
8136 Plist := New_List;
8138 for J in Ranges'Range loop
8139 declare
8140 Lo : Uint := Ranges (J).Lo;
8141 Hi : Uint := Ranges (J).Hi;
8143 begin
8144 -- Ignore completely out of range entry
8146 if Hi < TLo or else Lo > THi then
8147 null;
8149 -- Otherwise process entry
8151 else
8152 -- Adjust out of range value to subtype range
8154 if Lo < TLo then
8155 Lo := TLo;
8156 end if;
8158 if Hi > THi then
8159 Hi := THi;
8160 end if;
8162 -- Convert range into required form
8164 Append_To (Plist, Build_Range (Lo, Hi));
8165 end if;
8166 end;
8167 end loop;
8169 -- Processing was successful and all entries were static, so now we
8170 -- can store the result as the predicate list.
8172 Set_Static_Discrete_Predicate (Typ, Plist);
8174 -- The processing for static predicates put the expression into
8175 -- canonical form as a series of ranges. It also eliminated
8176 -- duplicates and collapsed and combined ranges. We might as well
8177 -- replace the alternatives list of the right operand of the
8178 -- membership test with the static predicate list, which will
8179 -- usually be more efficient.
8181 declare
8182 New_Alts : constant List_Id := New_List;
8183 Old_Node : Node_Id;
8184 New_Node : Node_Id;
8186 begin
8187 Old_Node := First (Plist);
8188 while Present (Old_Node) loop
8189 New_Node := New_Copy (Old_Node);
8191 if Nkind (New_Node) = N_Range then
8192 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
8193 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
8194 end if;
8196 Append_To (New_Alts, New_Node);
8197 Next (Old_Node);
8198 end loop;
8200 -- If empty list, replace by False
8202 if Is_Empty_List (New_Alts) then
8203 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
8205 -- Else replace by set membership test
8207 else
8208 Rewrite (Expr,
8209 Make_In (Loc,
8210 Left_Opnd => Make_Identifier (Loc, Nam),
8211 Right_Opnd => Empty,
8212 Alternatives => New_Alts));
8214 -- Resolve new expression in function context
8216 Install_Formals (Predicate_Function (Typ));
8217 Push_Scope (Predicate_Function (Typ));
8218 Analyze_And_Resolve (Expr, Standard_Boolean);
8219 Pop_Scope;
8220 end if;
8221 end;
8222 end;
8224 -- If non-static, return doing nothing
8226 exception
8227 when Non_Static =>
8228 return;
8229 end Build_Discrete_Static_Predicate;
8231 --------------------------------
8232 -- Build_Export_Import_Pragma --
8233 --------------------------------
8235 function Build_Export_Import_Pragma
8236 (Asp : Node_Id;
8237 Id : Entity_Id) return Node_Id
8239 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
8240 Expr : constant Node_Id := Expression (Asp);
8241 Loc : constant Source_Ptr := Sloc (Asp);
8243 Args : List_Id;
8244 Conv : Node_Id;
8245 Conv_Arg : Node_Id;
8246 Dummy_1 : Node_Id;
8247 Dummy_2 : Node_Id;
8248 EN : Node_Id;
8249 LN : Node_Id;
8250 Prag : Node_Id;
8252 Create_Pragma : Boolean := False;
8253 -- This flag is set when the aspect form is such that it warrants the
8254 -- creation of a corresponding pragma.
8256 begin
8257 if Present (Expr) then
8258 if Error_Posted (Expr) then
8259 null;
8261 elsif Is_True (Expr_Value (Expr)) then
8262 Create_Pragma := True;
8263 end if;
8265 -- Otherwise the aspect defaults to True
8267 else
8268 Create_Pragma := True;
8269 end if;
8271 -- Nothing to do when the expression is False or is erroneous
8273 if not Create_Pragma then
8274 return Empty;
8275 end if;
8277 -- Obtain all interfacing aspects that apply to the related entity
8279 Get_Interfacing_Aspects
8280 (Iface_Asp => Asp,
8281 Conv_Asp => Conv,
8282 EN_Asp => EN,
8283 Expo_Asp => Dummy_1,
8284 Imp_Asp => Dummy_2,
8285 LN_Asp => LN);
8287 Args := New_List;
8289 -- Handle the convention argument
8291 if Present (Conv) then
8292 Conv_Arg := New_Copy_Tree (Expression (Conv));
8294 -- Assume convention "Ada' when aspect Convention is missing
8296 else
8297 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8298 end if;
8300 Append_To (Args,
8301 Make_Pragma_Argument_Association (Loc,
8302 Chars => Name_Convention,
8303 Expression => Conv_Arg));
8305 -- Handle the entity argument
8307 Append_To (Args,
8308 Make_Pragma_Argument_Association (Loc,
8309 Chars => Name_Entity,
8310 Expression => New_Occurrence_Of (Id, Loc)));
8312 -- Handle the External_Name argument
8314 if Present (EN) then
8315 Append_To (Args,
8316 Make_Pragma_Argument_Association (Loc,
8317 Chars => Name_External_Name,
8318 Expression => New_Copy_Tree (Expression (EN))));
8319 end if;
8321 -- Handle the Link_Name argument
8323 if Present (LN) then
8324 Append_To (Args,
8325 Make_Pragma_Argument_Association (Loc,
8326 Chars => Name_Link_Name,
8327 Expression => New_Copy_Tree (Expression (LN))));
8328 end if;
8330 -- Generate:
8331 -- pragma Export/Import
8332 -- (Convention => <Conv>/Ada,
8333 -- Entity => <Id>,
8334 -- [External_Name => <EN>,]
8335 -- [Link_Name => <LN>]);
8337 Prag :=
8338 Make_Pragma (Loc,
8339 Pragma_Identifier =>
8340 Make_Identifier (Loc, Chars (Identifier (Asp))),
8341 Pragma_Argument_Associations => Args);
8343 -- Decorate the relevant aspect and the pragma
8345 Set_Aspect_Rep_Item (Asp, Prag);
8347 Set_Corresponding_Aspect (Prag, Asp);
8348 Set_From_Aspect_Specification (Prag);
8349 Set_Parent (Prag, Asp);
8351 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8352 Set_Import_Pragma (Id, Prag);
8353 end if;
8355 return Prag;
8356 end Build_Export_Import_Pragma;
8358 -------------------------------
8359 -- Build_Predicate_Functions --
8360 -------------------------------
8362 -- The procedures that are constructed here have the form:
8364 -- function typPredicate (Ixxx : typ) return Boolean is
8365 -- begin
8366 -- return
8367 -- typ1Predicate (typ1 (Ixxx))
8368 -- and then typ2Predicate (typ2 (Ixxx))
8369 -- and then ...;
8370 -- exp1 and then exp2 and then ...
8371 -- end typPredicate;
8373 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8374 -- this is the point at which these expressions get analyzed, providing the
8375 -- required delay, and typ1, typ2, are entities from which predicates are
8376 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8377 -- use this function even if checks are off, e.g. for membership tests.
8379 -- Note that the inherited predicates are evaluated first, as required by
8380 -- AI12-0071-1.
8382 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8383 -- the form of this return expression.
8385 -- If the expression has at least one Raise_Expression, then we also build
8386 -- the typPredicateM version of the function, in which any occurrence of a
8387 -- Raise_Expression is converted to "return False".
8389 -- WARNING: This routine manages Ghost regions. Return statements must be
8390 -- replaced by gotos which jump to the end of the routine and restore the
8391 -- Ghost mode.
8393 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8394 Loc : constant Source_Ptr := Sloc (Typ);
8396 Expr : Node_Id;
8397 -- This is the expression for the result of the function. It is
8398 -- is build by connecting the component predicates with AND THEN.
8400 Expr_M : Node_Id := Empty; -- init to avoid warning
8401 -- This is the corresponding return expression for the Predicate_M
8402 -- function. It differs in that raise expressions are marked for
8403 -- special expansion (see Process_REs).
8405 Object_Name : Name_Id;
8406 -- Name for argument of Predicate procedure. Note that we use the same
8407 -- name for both predicate functions. That way the reference within the
8408 -- predicate expression is the same in both functions.
8410 Object_Entity : Entity_Id;
8411 -- Entity for argument of Predicate procedure
8413 Object_Entity_M : Entity_Id;
8414 -- Entity for argument of separate Predicate procedure when exceptions
8415 -- are present in expression.
8417 FDecl : Node_Id;
8418 -- The function declaration
8420 SId : Entity_Id;
8421 -- Its entity
8423 Raise_Expression_Present : Boolean := False;
8424 -- Set True if Expr has at least one Raise_Expression
8426 procedure Add_Condition (Cond : Node_Id);
8427 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8428 -- Expr is empty).
8430 procedure Add_Predicates;
8431 -- Appends expressions for any Predicate pragmas in the rep item chain
8432 -- Typ to Expr. Note that we look only at items for this exact entity.
8433 -- Inheritance of predicates for the parent type is done by calling the
8434 -- Predicate_Function of the parent type, using Add_Call above.
8436 procedure Add_Call (T : Entity_Id);
8437 -- Includes a call to the predicate function for type T in Expr if T
8438 -- has predicates and Predicate_Function (T) is non-empty.
8440 function Process_RE (N : Node_Id) return Traverse_Result;
8441 -- Used in Process REs, tests if node N is a raise expression, and if
8442 -- so, marks it to be converted to return False.
8444 procedure Process_REs is new Traverse_Proc (Process_RE);
8445 -- Marks any raise expressions in Expr_M to return False
8447 function Test_RE (N : Node_Id) return Traverse_Result;
8448 -- Used in Test_REs, tests one node for being a raise expression, and if
8449 -- so sets Raise_Expression_Present True.
8451 procedure Test_REs is new Traverse_Proc (Test_RE);
8452 -- Tests to see if Expr contains any raise expressions
8454 --------------
8455 -- Add_Call --
8456 --------------
8458 procedure Add_Call (T : Entity_Id) is
8459 Exp : Node_Id;
8461 begin
8462 if Present (T) and then Present (Predicate_Function (T)) then
8463 Set_Has_Predicates (Typ);
8465 -- Build the call to the predicate function of T. The type may be
8466 -- derived, so use an unchecked conversion for the actual.
8468 Exp :=
8469 Make_Predicate_Call
8470 (Typ => T,
8471 Expr =>
8472 Unchecked_Convert_To (T,
8473 Make_Identifier (Loc, Object_Name)));
8475 -- "and"-in the call to evolving expression
8477 Add_Condition (Exp);
8479 -- Output info message on inheritance if required. Note we do not
8480 -- give this information for generic actual types, since it is
8481 -- unwelcome noise in that case in instantiations. We also
8482 -- generally suppress the message in instantiations, and also
8483 -- if it involves internal names.
8485 if Opt.List_Inherited_Aspects
8486 and then not Is_Generic_Actual_Type (Typ)
8487 and then Instantiation_Depth (Sloc (Typ)) = 0
8488 and then not Is_Internal_Name (Chars (T))
8489 and then not Is_Internal_Name (Chars (Typ))
8490 then
8491 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8492 Error_Msg_Node_2 := T;
8493 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8494 end if;
8495 end if;
8496 end Add_Call;
8498 -------------------
8499 -- Add_Condition --
8500 -------------------
8502 procedure Add_Condition (Cond : Node_Id) is
8503 begin
8504 -- This is the first predicate expression
8506 if No (Expr) then
8507 Expr := Cond;
8509 -- Otherwise concatenate to the existing predicate expressions by
8510 -- using "and then".
8512 else
8513 Expr :=
8514 Make_And_Then (Loc,
8515 Left_Opnd => Relocate_Node (Expr),
8516 Right_Opnd => Cond);
8517 end if;
8518 end Add_Condition;
8520 --------------------
8521 -- Add_Predicates --
8522 --------------------
8524 procedure Add_Predicates is
8525 procedure Add_Predicate (Prag : Node_Id);
8526 -- Concatenate the expression of predicate pragma Prag to Expr by
8527 -- using a short circuit "and then" operator.
8529 -------------------
8530 -- Add_Predicate --
8531 -------------------
8533 procedure Add_Predicate (Prag : Node_Id) is
8534 procedure Replace_Type_Reference (N : Node_Id);
8535 -- Replace a single occurrence N of the subtype name with a
8536 -- reference to the formal of the predicate function. N can be an
8537 -- identifier referencing the subtype, or a selected component,
8538 -- representing an appropriately qualified occurrence of the
8539 -- subtype name.
8541 procedure Replace_Type_References is
8542 new Replace_Type_References_Generic (Replace_Type_Reference);
8543 -- Traverse an expression changing every occurrence of an
8544 -- identifier whose name matches the name of the subtype with a
8545 -- reference to the formal parameter of the predicate function.
8547 ----------------------------
8548 -- Replace_Type_Reference --
8549 ----------------------------
8551 procedure Replace_Type_Reference (N : Node_Id) is
8552 begin
8553 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8554 -- Use the Sloc of the usage name, not the defining name
8556 Set_Etype (N, Typ);
8557 Set_Entity (N, Object_Entity);
8559 -- We want to treat the node as if it comes from source, so
8560 -- that ASIS will not ignore it.
8562 Set_Comes_From_Source (N, True);
8563 end Replace_Type_Reference;
8565 -- Local variables
8567 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8568 Arg1 : Node_Id;
8569 Arg2 : Node_Id;
8571 -- Start of processing for Add_Predicate
8573 begin
8574 -- Mark corresponding SCO as enabled
8576 Set_SCO_Pragma_Enabled (Sloc (Prag));
8578 -- Extract the arguments of the pragma. The expression itself
8579 -- is copied for use in the predicate function, to preserve the
8580 -- original version for ASIS use.
8582 Arg1 := First (Pragma_Argument_Associations (Prag));
8583 Arg2 := Next (Arg1);
8585 Arg1 := Get_Pragma_Arg (Arg1);
8586 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
8588 -- When the predicate pragma applies to the current type or its
8589 -- full view, replace all occurrences of the subtype name with
8590 -- references to the formal parameter of the predicate function.
8592 if Entity (Arg1) = Typ
8593 or else Full_View (Entity (Arg1)) = Typ
8594 then
8595 Replace_Type_References (Arg2, Typ);
8597 -- If the predicate pragma comes from an aspect, replace the
8598 -- saved expression because we need the subtype references
8599 -- replaced for the calls to Preanalyze_Spec_Expression in
8600 -- Check_Aspect_At_xxx routines.
8602 if Present (Asp) then
8603 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8604 end if;
8606 -- "and"-in the Arg2 condition to evolving expression
8608 Add_Condition (Relocate_Node (Arg2));
8609 end if;
8610 end Add_Predicate;
8612 -- Local variables
8614 Ritem : Node_Id;
8616 -- Start of processing for Add_Predicates
8618 begin
8619 Ritem := First_Rep_Item (Typ);
8621 -- If the type is private, check whether full view has inherited
8622 -- predicates.
8624 if Is_Private_Type (Typ) and then No (Ritem) then
8625 Ritem := First_Rep_Item (Full_View (Typ));
8626 end if;
8628 while Present (Ritem) loop
8629 if Nkind (Ritem) = N_Pragma
8630 and then Pragma_Name (Ritem) = Name_Predicate
8631 then
8632 Add_Predicate (Ritem);
8634 -- If the type is declared in an inner package it may be frozen
8635 -- outside of the package, and the generated pragma has not been
8636 -- analyzed yet, so capture the expression for the predicate
8637 -- function at this point.
8639 elsif Nkind (Ritem) = N_Aspect_Specification
8640 and then Present (Aspect_Rep_Item (Ritem))
8641 and then Scope (Typ) /= Current_Scope
8642 then
8643 declare
8644 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8646 begin
8647 if Nkind (Prag) = N_Pragma
8648 and then Pragma_Name (Prag) = Name_Predicate
8649 then
8650 Add_Predicate (Prag);
8651 end if;
8652 end;
8653 end if;
8655 Next_Rep_Item (Ritem);
8656 end loop;
8657 end Add_Predicates;
8659 ----------------
8660 -- Process_RE --
8661 ----------------
8663 function Process_RE (N : Node_Id) return Traverse_Result is
8664 begin
8665 if Nkind (N) = N_Raise_Expression then
8666 Set_Convert_To_Return_False (N);
8667 return Skip;
8668 else
8669 return OK;
8670 end if;
8671 end Process_RE;
8673 -------------
8674 -- Test_RE --
8675 -------------
8677 function Test_RE (N : Node_Id) return Traverse_Result is
8678 begin
8679 if Nkind (N) = N_Raise_Expression then
8680 Raise_Expression_Present := True;
8681 return Abandon;
8682 else
8683 return OK;
8684 end if;
8685 end Test_RE;
8687 -- Local variables
8689 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
8690 -- Save the Ghost mode to restore on exit
8692 -- Start of processing for Build_Predicate_Functions
8694 begin
8695 -- Return if already built or if type does not have predicates
8697 SId := Predicate_Function (Typ);
8698 if not Has_Predicates (Typ)
8699 or else (Present (SId) and then Has_Completion (SId))
8700 then
8701 return;
8702 end if;
8704 -- The related type may be subject to pragma Ghost. Set the mode now to
8705 -- ensure that the predicate functions are properly marked as Ghost.
8707 Set_Ghost_Mode (Typ);
8709 -- Prepare to construct predicate expression
8711 Expr := Empty;
8713 if Present (SId) then
8714 FDecl := Unit_Declaration_Node (SId);
8716 else
8717 FDecl := Build_Predicate_Function_Declaration (Typ);
8718 SId := Defining_Entity (FDecl);
8719 end if;
8721 -- Recover name of formal parameter of function that replaces references
8722 -- to the type in predicate expressions.
8724 Object_Entity :=
8725 Defining_Identifier
8726 (First (Parameter_Specifications (Specification (FDecl))));
8728 Object_Name := Chars (Object_Entity);
8729 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8731 -- Add predicates for ancestor if present. These must come before the
8732 -- ones for the current type, as required by AI12-0071-1.
8734 declare
8735 Atyp : Entity_Id;
8736 begin
8737 Atyp := Nearest_Ancestor (Typ);
8739 -- The type may be private but the full view may inherit predicates
8741 if No (Atyp) and then Is_Private_Type (Typ) then
8742 Atyp := Nearest_Ancestor (Full_View (Typ));
8743 end if;
8745 if Present (Atyp) then
8746 Add_Call (Atyp);
8747 end if;
8748 end;
8750 -- Add Predicates for the current type
8752 Add_Predicates;
8754 -- Case where predicates are present
8756 if Present (Expr) then
8758 -- Test for raise expression present
8760 Test_REs (Expr);
8762 -- If raise expression is present, capture a copy of Expr for use
8763 -- in building the predicateM function version later on. For this
8764 -- copy we replace references to Object_Entity by Object_Entity_M.
8766 if Raise_Expression_Present then
8767 declare
8768 Map : constant Elist_Id := New_Elmt_List;
8769 New_V : Entity_Id := Empty;
8771 -- The unanalyzed expression will be copied and appear in
8772 -- both functions. Normally expressions do not declare new
8773 -- entities, but quantified expressions do, so we need to
8774 -- create new entities for their bound variables, to prevent
8775 -- multiple definitions in gigi.
8777 function Reset_Loop_Variable (N : Node_Id)
8778 return Traverse_Result;
8780 procedure Collect_Loop_Variables is
8781 new Traverse_Proc (Reset_Loop_Variable);
8783 ------------------------
8784 -- Reset_Loop_Variable --
8785 ------------------------
8787 function Reset_Loop_Variable (N : Node_Id)
8788 return Traverse_Result
8790 begin
8791 if Nkind (N) = N_Iterator_Specification then
8792 New_V := Make_Defining_Identifier
8793 (Sloc (N), Chars (Defining_Identifier (N)));
8795 Set_Defining_Identifier (N, New_V);
8796 end if;
8798 return OK;
8799 end Reset_Loop_Variable;
8801 begin
8802 Append_Elmt (Object_Entity, Map);
8803 Append_Elmt (Object_Entity_M, Map);
8804 Expr_M := New_Copy_Tree (Expr, Map => Map);
8805 Collect_Loop_Variables (Expr_M);
8806 end;
8807 end if;
8809 -- Build the main predicate function
8811 declare
8812 SIdB : constant Entity_Id :=
8813 Make_Defining_Identifier (Loc,
8814 Chars => New_External_Name (Chars (Typ), "Predicate"));
8815 -- The entity for the function body
8817 Spec : Node_Id;
8818 FBody : Node_Id;
8820 begin
8821 Set_Ekind (SIdB, E_Function);
8822 Set_Is_Predicate_Function (SIdB);
8824 -- The predicate function is shared between views of a type
8826 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8827 Set_Predicate_Function (Full_View (Typ), SId);
8828 end if;
8830 -- Build function body
8832 Spec :=
8833 Make_Function_Specification (Loc,
8834 Defining_Unit_Name => SIdB,
8835 Parameter_Specifications => New_List (
8836 Make_Parameter_Specification (Loc,
8837 Defining_Identifier =>
8838 Make_Defining_Identifier (Loc, Object_Name),
8839 Parameter_Type =>
8840 New_Occurrence_Of (Typ, Loc))),
8841 Result_Definition =>
8842 New_Occurrence_Of (Standard_Boolean, Loc));
8844 FBody :=
8845 Make_Subprogram_Body (Loc,
8846 Specification => Spec,
8847 Declarations => Empty_List,
8848 Handled_Statement_Sequence =>
8849 Make_Handled_Sequence_Of_Statements (Loc,
8850 Statements => New_List (
8851 Make_Simple_Return_Statement (Loc,
8852 Expression => Expr))));
8854 -- If declaration has not been analyzed yet, Insert declaration
8855 -- before freeze node. Insert body itself after freeze node.
8857 if not Analyzed (FDecl) then
8858 Insert_Before_And_Analyze (N, FDecl);
8859 end if;
8861 Insert_After_And_Analyze (N, FBody);
8863 -- Static predicate functions are always side-effect free, and
8864 -- in most cases dynamic predicate functions are as well. Mark
8865 -- them as such whenever possible, so redundant predicate checks
8866 -- can be optimized. If there is a variable reference within the
8867 -- expression, the function is not pure.
8869 if Expander_Active then
8870 Set_Is_Pure (SId,
8871 Side_Effect_Free (Expr, Variable_Ref => True));
8872 Set_Is_Inlined (SId);
8873 end if;
8874 end;
8876 -- Test for raise expressions present and if so build M version
8878 if Raise_Expression_Present then
8879 declare
8880 SId : constant Entity_Id :=
8881 Make_Defining_Identifier (Loc,
8882 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8883 -- The entity for the function spec
8885 SIdB : constant Entity_Id :=
8886 Make_Defining_Identifier (Loc,
8887 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8888 -- The entity for the function body
8890 Spec : Node_Id;
8891 FBody : Node_Id;
8892 FDecl : Node_Id;
8893 BTemp : Entity_Id;
8895 begin
8896 -- Mark any raise expressions for special expansion
8898 Process_REs (Expr_M);
8900 -- Build function declaration
8902 Set_Ekind (SId, E_Function);
8903 Set_Is_Predicate_Function_M (SId);
8904 Set_Predicate_Function_M (Typ, SId);
8906 -- The predicate function is shared between views of a type
8908 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8909 Set_Predicate_Function_M (Full_View (Typ), SId);
8910 end if;
8912 Spec :=
8913 Make_Function_Specification (Loc,
8914 Defining_Unit_Name => SId,
8915 Parameter_Specifications => New_List (
8916 Make_Parameter_Specification (Loc,
8917 Defining_Identifier => Object_Entity_M,
8918 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8919 Result_Definition =>
8920 New_Occurrence_Of (Standard_Boolean, Loc));
8922 FDecl :=
8923 Make_Subprogram_Declaration (Loc,
8924 Specification => Spec);
8926 -- Build function body
8928 Spec :=
8929 Make_Function_Specification (Loc,
8930 Defining_Unit_Name => SIdB,
8931 Parameter_Specifications => New_List (
8932 Make_Parameter_Specification (Loc,
8933 Defining_Identifier =>
8934 Make_Defining_Identifier (Loc, Object_Name),
8935 Parameter_Type =>
8936 New_Occurrence_Of (Typ, Loc))),
8937 Result_Definition =>
8938 New_Occurrence_Of (Standard_Boolean, Loc));
8940 -- Build the body, we declare the boolean expression before
8941 -- doing the return, because we are not really confident of
8942 -- what happens if a return appears within a return.
8944 BTemp :=
8945 Make_Defining_Identifier (Loc,
8946 Chars => New_Internal_Name ('B'));
8948 FBody :=
8949 Make_Subprogram_Body (Loc,
8950 Specification => Spec,
8952 Declarations => New_List (
8953 Make_Object_Declaration (Loc,
8954 Defining_Identifier => BTemp,
8955 Constant_Present => True,
8956 Object_Definition =>
8957 New_Occurrence_Of (Standard_Boolean, Loc),
8958 Expression => Expr_M)),
8960 Handled_Statement_Sequence =>
8961 Make_Handled_Sequence_Of_Statements (Loc,
8962 Statements => New_List (
8963 Make_Simple_Return_Statement (Loc,
8964 Expression => New_Occurrence_Of (BTemp, Loc)))));
8966 -- Insert declaration before freeze node and body after
8968 Insert_Before_And_Analyze (N, FDecl);
8969 Insert_After_And_Analyze (N, FBody);
8970 end;
8971 end if;
8973 -- See if we have a static predicate. Note that the answer may be
8974 -- yes even if we have an explicit Dynamic_Predicate present.
8976 declare
8977 PS : Boolean;
8978 EN : Node_Id;
8980 begin
8981 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8982 PS := False;
8983 else
8984 PS := Is_Predicate_Static (Expr, Object_Name);
8985 end if;
8987 -- Case where we have a predicate-static aspect
8989 if PS then
8991 -- We don't set Has_Static_Predicate_Aspect, since we can have
8992 -- any of the three cases (Predicate, Dynamic_Predicate, or
8993 -- Static_Predicate) generating a predicate with an expression
8994 -- that is predicate-static. We just indicate that we have a
8995 -- predicate that can be treated as static.
8997 Set_Has_Static_Predicate (Typ);
8999 -- For discrete subtype, build the static predicate list
9001 if Is_Discrete_Type (Typ) then
9002 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
9004 -- If we don't get a static predicate list, it means that we
9005 -- have a case where this is not possible, most typically in
9006 -- the case where we inherit a dynamic predicate. We do not
9007 -- consider this an error, we just leave the predicate as
9008 -- dynamic. But if we do succeed in building the list, then
9009 -- we mark the predicate as static.
9011 if No (Static_Discrete_Predicate (Typ)) then
9012 Set_Has_Static_Predicate (Typ, False);
9013 end if;
9015 -- For real or string subtype, save predicate expression
9017 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9018 Set_Static_Real_Or_String_Predicate (Typ, Expr);
9019 end if;
9021 -- Case of dynamic predicate (expression is not predicate-static)
9023 else
9024 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
9025 -- is only set if we have an explicit Dynamic_Predicate aspect
9026 -- given. Here we may simply have a Predicate aspect where the
9027 -- expression happens not to be predicate-static.
9029 -- Emit an error when the predicate is categorized as static
9030 -- but its expression is not predicate-static.
9032 -- First a little fiddling to get a nice location for the
9033 -- message. If the expression is of the form (A and then B),
9034 -- where A is an inherited predicate, then use the right
9035 -- operand for the Sloc. This avoids getting confused by a call
9036 -- to an inherited predicate with a less convenient source
9037 -- location.
9039 EN := Expr;
9040 while Nkind (EN) = N_And_Then
9041 and then Nkind (Left_Opnd (EN)) = N_Function_Call
9042 and then Is_Predicate_Function
9043 (Entity (Name (Left_Opnd (EN))))
9044 loop
9045 EN := Right_Opnd (EN);
9046 end loop;
9048 -- Now post appropriate message
9050 if Has_Static_Predicate_Aspect (Typ) then
9051 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
9052 Error_Msg_F
9053 ("expression is not predicate-static (RM 3.2.4(16-22))",
9054 EN);
9055 else
9056 Error_Msg_F
9057 ("static predicate requires scalar or string type", EN);
9058 end if;
9059 end if;
9060 end if;
9061 end;
9062 end if;
9064 Restore_Ghost_Mode (Saved_GM);
9065 end Build_Predicate_Functions;
9067 ------------------------------------------
9068 -- Build_Predicate_Function_Declaration --
9069 ------------------------------------------
9071 -- WARNING: This routine manages Ghost regions. Return statements must be
9072 -- replaced by gotos which jump to the end of the routine and restore the
9073 -- Ghost mode.
9075 function Build_Predicate_Function_Declaration
9076 (Typ : Entity_Id) return Node_Id
9078 Loc : constant Source_Ptr := Sloc (Typ);
9080 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9081 -- Save the Ghost mode to restore on exit
9083 Func_Decl : Node_Id;
9084 Func_Id : Entity_Id;
9085 Spec : Node_Id;
9087 begin
9088 -- The related type may be subject to pragma Ghost. Set the mode now to
9089 -- ensure that the predicate functions are properly marked as Ghost.
9091 Set_Ghost_Mode (Typ);
9093 Func_Id :=
9094 Make_Defining_Identifier (Loc,
9095 Chars => New_External_Name (Chars (Typ), "Predicate"));
9097 -- The predicate function requires debug info when the predicates are
9098 -- subject to Source Coverage Obligations.
9100 if Opt.Generate_SCO then
9101 Set_Debug_Info_Needed (Func_Id);
9102 end if;
9104 Spec :=
9105 Make_Function_Specification (Loc,
9106 Defining_Unit_Name => Func_Id,
9107 Parameter_Specifications => New_List (
9108 Make_Parameter_Specification (Loc,
9109 Defining_Identifier => Make_Temporary (Loc, 'I'),
9110 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9111 Result_Definition =>
9112 New_Occurrence_Of (Standard_Boolean, Loc));
9114 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
9116 Set_Ekind (Func_Id, E_Function);
9117 Set_Etype (Func_Id, Standard_Boolean);
9118 Set_Is_Internal (Func_Id);
9119 Set_Is_Predicate_Function (Func_Id);
9120 Set_Predicate_Function (Typ, Func_Id);
9122 Insert_After (Parent (Typ), Func_Decl);
9123 Analyze (Func_Decl);
9125 Restore_Ghost_Mode (Saved_GM);
9127 return Func_Decl;
9128 end Build_Predicate_Function_Declaration;
9130 -----------------------------------------
9131 -- Check_Aspect_At_End_Of_Declarations --
9132 -----------------------------------------
9134 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9135 Ent : constant Entity_Id := Entity (ASN);
9136 Ident : constant Node_Id := Identifier (ASN);
9137 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9139 End_Decl_Expr : constant Node_Id := Entity (Ident);
9140 -- Expression to be analyzed at end of declarations
9142 Freeze_Expr : constant Node_Id := Expression (ASN);
9143 -- Expression from call to Check_Aspect_At_Freeze_Point.
9145 T : constant Entity_Id := Etype (Original_Node (Freeze_Expr));
9146 -- Type required for preanalyze call. We use the original expression to
9147 -- get the proper type, to prevent cascaded errors when the expression
9148 -- is constant-folded.
9150 Err : Boolean;
9151 -- Set False if error
9153 -- On entry to this procedure, Entity (Ident) contains a copy of the
9154 -- original expression from the aspect, saved for this purpose, and
9155 -- but Expression (Ident) is a preanalyzed copy of the expression,
9156 -- preanalyzed just after the freeze point.
9158 procedure Check_Overloaded_Name;
9159 -- For aspects whose expression is simply a name, this routine checks if
9160 -- the name is overloaded or not. If so, it verifies there is an
9161 -- interpretation that matches the entity obtained at the freeze point,
9162 -- otherwise the compiler complains.
9164 ---------------------------
9165 -- Check_Overloaded_Name --
9166 ---------------------------
9168 procedure Check_Overloaded_Name is
9169 begin
9170 if not Is_Overloaded (End_Decl_Expr) then
9171 Err := not Is_Entity_Name (End_Decl_Expr)
9172 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
9174 else
9175 Err := True;
9177 declare
9178 Index : Interp_Index;
9179 It : Interp;
9181 begin
9182 Get_First_Interp (End_Decl_Expr, Index, It);
9183 while Present (It.Typ) loop
9184 if It.Nam = Entity (Freeze_Expr) then
9185 Err := False;
9186 exit;
9187 end if;
9189 Get_Next_Interp (Index, It);
9190 end loop;
9191 end;
9192 end if;
9193 end Check_Overloaded_Name;
9195 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9197 begin
9198 -- In an instance we do not perform the consistency check between freeze
9199 -- point and end of declarations, because it was done already in the
9200 -- analysis of the generic. Furthermore, the delayed analysis of an
9201 -- aspect of the instance may produce spurious errors when the generic
9202 -- is a child unit that references entities in the parent (which might
9203 -- not be in scope at the freeze point of the instance).
9205 if In_Instance then
9206 return;
9208 -- The enclosing scope may have been rewritten during expansion (.e.g. a
9209 -- task body is rewritten as a procedure) after this conformance check
9210 -- has been performed, so do not perform it again (it may not easily be
9211 -- done if full visibility of local entities is not available).
9213 elsif not Comes_From_Source (Current_Scope) then
9214 return;
9216 -- Case of aspects Dimension, Dimension_System and Synchronization
9218 elsif A_Id = Aspect_Synchronization then
9219 return;
9221 -- Case of stream attributes, just have to compare entities. However,
9222 -- the expression is just a name (possibly overloaded), and there may
9223 -- be stream operations declared for unrelated types, so we just need
9224 -- to verify that one of these interpretations is the one available at
9225 -- at the freeze point.
9227 elsif A_Id = Aspect_Input or else
9228 A_Id = Aspect_Output or else
9229 A_Id = Aspect_Read or else
9230 A_Id = Aspect_Write
9231 then
9232 Analyze (End_Decl_Expr);
9233 Check_Overloaded_Name;
9235 elsif A_Id = Aspect_Variable_Indexing or else
9236 A_Id = Aspect_Constant_Indexing or else
9237 A_Id = Aspect_Default_Iterator or else
9238 A_Id = Aspect_Iterator_Element
9239 then
9240 -- Make type unfrozen before analysis, to prevent spurious errors
9241 -- about late attributes.
9243 Set_Is_Frozen (Ent, False);
9244 Analyze (End_Decl_Expr);
9245 Set_Is_Frozen (Ent, True);
9247 -- If the end of declarations comes before any other freeze
9248 -- point, the Freeze_Expr is not analyzed: no check needed.
9250 if Analyzed (Freeze_Expr) and then not In_Instance then
9251 Check_Overloaded_Name;
9252 else
9253 Err := False;
9254 end if;
9256 -- All other cases
9258 else
9259 -- Indicate that the expression comes from an aspect specification,
9260 -- which is used in subsequent analysis even if expansion is off.
9262 Set_Parent (End_Decl_Expr, ASN);
9264 -- In a generic context the aspect expressions have not been
9265 -- preanalyzed, so do it now. There are no conformance checks
9266 -- to perform in this case.
9268 if No (T) then
9269 Check_Aspect_At_Freeze_Point (ASN);
9270 return;
9272 -- The default values attributes may be defined in the private part,
9273 -- and the analysis of the expression may take place when only the
9274 -- partial view is visible. The expression must be scalar, so use
9275 -- the full view to resolve.
9277 elsif (A_Id = Aspect_Default_Value
9278 or else
9279 A_Id = Aspect_Default_Component_Value)
9280 and then Is_Private_Type (T)
9281 then
9282 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
9284 else
9285 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9286 end if;
9288 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9289 end if;
9291 -- Output error message if error. Force error on aspect specification
9292 -- even if there is an error on the expression itself.
9294 if Err then
9295 Error_Msg_NE
9296 ("!visibility of aspect for& changes after freeze point",
9297 ASN, Ent);
9298 Error_Msg_NE
9299 ("info: & is frozen here, aspects evaluated at this point??",
9300 Freeze_Node (Ent), Ent);
9301 end if;
9302 end Check_Aspect_At_End_Of_Declarations;
9304 ----------------------------------
9305 -- Check_Aspect_At_Freeze_Point --
9306 ----------------------------------
9308 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9309 Ident : constant Node_Id := Identifier (ASN);
9310 -- Identifier (use Entity field to save expression)
9312 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9314 T : Entity_Id := Empty;
9315 -- Type required for preanalyze call
9317 begin
9318 -- On entry to this procedure, Entity (Ident) contains a copy of the
9319 -- original expression from the aspect, saved for this purpose.
9321 -- On exit from this procedure Entity (Ident) is unchanged, still
9322 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9323 -- of the expression, preanalyzed just after the freeze point.
9325 -- Make a copy of the expression to be preanalyzed
9327 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
9329 -- Find type for preanalyze call
9331 case A_Id is
9333 -- No_Aspect should be impossible
9335 when No_Aspect =>
9336 raise Program_Error;
9338 -- Aspects taking an optional boolean argument
9340 when Boolean_Aspects
9341 | Library_Unit_Aspects
9343 T := Standard_Boolean;
9345 -- Aspects corresponding to attribute definition clauses
9347 when Aspect_Address =>
9348 T := RTE (RE_Address);
9350 when Aspect_Attach_Handler =>
9351 T := RTE (RE_Interrupt_ID);
9353 when Aspect_Bit_Order
9354 | Aspect_Scalar_Storage_Order
9356 T := RTE (RE_Bit_Order);
9358 when Aspect_Convention =>
9359 return;
9361 when Aspect_CPU =>
9362 T := RTE (RE_CPU_Range);
9364 -- Default_Component_Value is resolved with the component type
9366 when Aspect_Default_Component_Value =>
9367 T := Component_Type (Entity (ASN));
9369 when Aspect_Default_Storage_Pool =>
9370 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9372 -- Default_Value is resolved with the type entity in question
9374 when Aspect_Default_Value =>
9375 T := Entity (ASN);
9377 when Aspect_Dispatching_Domain =>
9378 T := RTE (RE_Dispatching_Domain);
9380 when Aspect_External_Tag =>
9381 T := Standard_String;
9383 when Aspect_External_Name =>
9384 T := Standard_String;
9386 when Aspect_Link_Name =>
9387 T := Standard_String;
9389 when Aspect_Interrupt_Priority
9390 | Aspect_Priority
9392 T := Standard_Integer;
9394 when Aspect_Relative_Deadline =>
9395 T := RTE (RE_Time_Span);
9397 when Aspect_Secondary_Stack_Size =>
9398 T := Standard_Integer;
9400 when Aspect_Small =>
9402 -- Note that the expression can be of any real type (not just a
9403 -- real universal literal) as long as it is a static constant.
9405 T := Any_Real;
9407 -- For a simple storage pool, we have to retrieve the type of the
9408 -- pool object associated with the aspect's corresponding attribute
9409 -- definition clause.
9411 when Aspect_Simple_Storage_Pool =>
9412 T := Etype (Expression (Aspect_Rep_Item (ASN)));
9414 when Aspect_Storage_Pool =>
9415 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9417 when Aspect_Alignment
9418 | Aspect_Component_Size
9419 | Aspect_Machine_Radix
9420 | Aspect_Object_Size
9421 | Aspect_Size
9422 | Aspect_Storage_Size
9423 | Aspect_Stream_Size
9424 | Aspect_Value_Size
9426 T := Any_Integer;
9428 when Aspect_Linker_Section =>
9429 T := Standard_String;
9431 when Aspect_Synchronization =>
9432 return;
9434 -- Special case, the expression of these aspects is just an entity
9435 -- that does not need any resolution, so just analyze.
9437 when Aspect_Input
9438 | Aspect_Output
9439 | Aspect_Read
9440 | Aspect_Suppress
9441 | Aspect_Unsuppress
9442 | Aspect_Warnings
9443 | Aspect_Write
9445 Analyze (Expression (ASN));
9446 return;
9448 -- Same for Iterator aspects, where the expression is a function
9449 -- name. Legality rules are checked separately.
9451 when Aspect_Constant_Indexing
9452 | Aspect_Default_Iterator
9453 | Aspect_Iterator_Element
9454 | Aspect_Variable_Indexing
9456 Analyze (Expression (ASN));
9457 return;
9459 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9461 when Aspect_Iterable =>
9462 T := Entity (ASN);
9464 declare
9465 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
9466 Assoc : Node_Id;
9467 Expr : Node_Id;
9469 begin
9470 if Cursor = Any_Type then
9471 return;
9472 end if;
9474 Assoc := First (Component_Associations (Expression (ASN)));
9475 while Present (Assoc) loop
9476 Expr := Expression (Assoc);
9477 Analyze (Expr);
9479 if not Error_Posted (Expr) then
9480 Resolve_Iterable_Operation
9481 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9482 end if;
9484 Next (Assoc);
9485 end loop;
9486 end;
9488 return;
9490 -- Invariant/Predicate take boolean expressions
9492 when Aspect_Dynamic_Predicate
9493 | Aspect_Invariant
9494 | Aspect_Predicate
9495 | Aspect_Static_Predicate
9496 | Aspect_Type_Invariant
9498 T := Standard_Boolean;
9500 when Aspect_Predicate_Failure =>
9501 T := Standard_String;
9503 -- Here is the list of aspects that don't require delay analysis
9505 when Aspect_Abstract_State
9506 | Aspect_Annotate
9507 | Aspect_Async_Readers
9508 | Aspect_Async_Writers
9509 | Aspect_Constant_After_Elaboration
9510 | Aspect_Contract_Cases
9511 | Aspect_Default_Initial_Condition
9512 | Aspect_Depends
9513 | Aspect_Dimension
9514 | Aspect_Dimension_System
9515 | Aspect_Effective_Reads
9516 | Aspect_Effective_Writes
9517 | Aspect_Extensions_Visible
9518 | Aspect_Ghost
9519 | Aspect_Global
9520 | Aspect_Implicit_Dereference
9521 | Aspect_Initial_Condition
9522 | Aspect_Initializes
9523 | Aspect_Max_Queue_Length
9524 | Aspect_Obsolescent
9525 | Aspect_Part_Of
9526 | Aspect_Post
9527 | Aspect_Postcondition
9528 | Aspect_Pre
9529 | Aspect_Precondition
9530 | Aspect_Refined_Depends
9531 | Aspect_Refined_Global
9532 | Aspect_Refined_Post
9533 | Aspect_Refined_State
9534 | Aspect_SPARK_Mode
9535 | Aspect_Test_Case
9536 | Aspect_Unimplemented
9537 | Aspect_Volatile_Function
9539 raise Program_Error;
9541 end case;
9543 -- Do the preanalyze call
9545 Preanalyze_Spec_Expression (Expression (ASN), T);
9546 end Check_Aspect_At_Freeze_Point;
9548 -----------------------------------
9549 -- Check_Constant_Address_Clause --
9550 -----------------------------------
9552 procedure Check_Constant_Address_Clause
9553 (Expr : Node_Id;
9554 U_Ent : Entity_Id)
9556 procedure Check_At_Constant_Address (Nod : Node_Id);
9557 -- Checks that the given node N represents a name whose 'Address is
9558 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9559 -- address value is the same at the point of declaration of U_Ent and at
9560 -- the time of elaboration of the address clause.
9562 procedure Check_Expr_Constants (Nod : Node_Id);
9563 -- Checks that Nod meets the requirements for a constant address clause
9564 -- in the sense of the enclosing procedure.
9566 procedure Check_List_Constants (Lst : List_Id);
9567 -- Check that all elements of list Lst meet the requirements for a
9568 -- constant address clause in the sense of the enclosing procedure.
9570 -------------------------------
9571 -- Check_At_Constant_Address --
9572 -------------------------------
9574 procedure Check_At_Constant_Address (Nod : Node_Id) is
9575 begin
9576 if Is_Entity_Name (Nod) then
9577 if Present (Address_Clause (Entity ((Nod)))) then
9578 Error_Msg_NE
9579 ("invalid address clause for initialized object &!",
9580 Nod, U_Ent);
9581 Error_Msg_NE
9582 ("address for& cannot depend on another address clause! "
9583 & "(RM 13.1(22))!", Nod, U_Ent);
9585 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9586 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9587 then
9588 Error_Msg_NE
9589 ("invalid address clause for initialized object &!",
9590 Nod, U_Ent);
9591 Error_Msg_Node_2 := U_Ent;
9592 Error_Msg_NE
9593 ("\& must be defined before & (RM 13.1(22))!",
9594 Nod, Entity (Nod));
9595 end if;
9597 elsif Nkind (Nod) = N_Selected_Component then
9598 declare
9599 T : constant Entity_Id := Etype (Prefix (Nod));
9601 begin
9602 if (Is_Record_Type (T)
9603 and then Has_Discriminants (T))
9604 or else
9605 (Is_Access_Type (T)
9606 and then Is_Record_Type (Designated_Type (T))
9607 and then Has_Discriminants (Designated_Type (T)))
9608 then
9609 Error_Msg_NE
9610 ("invalid address clause for initialized object &!",
9611 Nod, U_Ent);
9612 Error_Msg_N
9613 ("\address cannot depend on component of discriminated "
9614 & "record (RM 13.1(22))!", Nod);
9615 else
9616 Check_At_Constant_Address (Prefix (Nod));
9617 end if;
9618 end;
9620 elsif Nkind (Nod) = N_Indexed_Component then
9621 Check_At_Constant_Address (Prefix (Nod));
9622 Check_List_Constants (Expressions (Nod));
9624 else
9625 Check_Expr_Constants (Nod);
9626 end if;
9627 end Check_At_Constant_Address;
9629 --------------------------
9630 -- Check_Expr_Constants --
9631 --------------------------
9633 procedure Check_Expr_Constants (Nod : Node_Id) is
9634 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9635 Ent : Entity_Id := Empty;
9637 begin
9638 if Nkind (Nod) in N_Has_Etype
9639 and then Etype (Nod) = Any_Type
9640 then
9641 return;
9642 end if;
9644 case Nkind (Nod) is
9645 when N_Empty
9646 | N_Error
9648 return;
9650 when N_Expanded_Name
9651 | N_Identifier
9653 Ent := Entity (Nod);
9655 -- We need to look at the original node if it is different
9656 -- from the node, since we may have rewritten things and
9657 -- substituted an identifier representing the rewrite.
9659 if Original_Node (Nod) /= Nod then
9660 Check_Expr_Constants (Original_Node (Nod));
9662 -- If the node is an object declaration without initial
9663 -- value, some code has been expanded, and the expression
9664 -- is not constant, even if the constituents might be
9665 -- acceptable, as in A'Address + offset.
9667 if Ekind (Ent) = E_Variable
9668 and then
9669 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9670 and then
9671 No (Expression (Declaration_Node (Ent)))
9672 then
9673 Error_Msg_NE
9674 ("invalid address clause for initialized object &!",
9675 Nod, U_Ent);
9677 -- If entity is constant, it may be the result of expanding
9678 -- a check. We must verify that its declaration appears
9679 -- before the object in question, else we also reject the
9680 -- address clause.
9682 elsif Ekind (Ent) = E_Constant
9683 and then In_Same_Source_Unit (Ent, U_Ent)
9684 and then Sloc (Ent) > Loc_U_Ent
9685 then
9686 Error_Msg_NE
9687 ("invalid address clause for initialized object &!",
9688 Nod, U_Ent);
9689 end if;
9691 return;
9692 end if;
9694 -- Otherwise look at the identifier and see if it is OK
9696 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9697 or else Is_Type (Ent)
9698 then
9699 return;
9701 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9703 -- This is the case where we must have Ent defined before
9704 -- U_Ent. Clearly if they are in different units this
9705 -- requirement is met since the unit containing Ent is
9706 -- already processed.
9708 if not In_Same_Source_Unit (Ent, U_Ent) then
9709 return;
9711 -- Otherwise location of Ent must be before the location
9712 -- of U_Ent, that's what prior defined means.
9714 elsif Sloc (Ent) < Loc_U_Ent then
9715 return;
9717 else
9718 Error_Msg_NE
9719 ("invalid address clause for initialized object &!",
9720 Nod, U_Ent);
9721 Error_Msg_Node_2 := U_Ent;
9722 Error_Msg_NE
9723 ("\& must be defined before & (RM 13.1(22))!",
9724 Nod, Ent);
9725 end if;
9727 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9728 Check_Expr_Constants (Original_Node (Nod));
9730 else
9731 Error_Msg_NE
9732 ("invalid address clause for initialized object &!",
9733 Nod, U_Ent);
9735 if Comes_From_Source (Ent) then
9736 Error_Msg_NE
9737 ("\reference to variable& not allowed"
9738 & " (RM 13.1(22))!", Nod, Ent);
9739 else
9740 Error_Msg_N
9741 ("non-static expression not allowed"
9742 & " (RM 13.1(22))!", Nod);
9743 end if;
9744 end if;
9746 when N_Integer_Literal =>
9748 -- If this is a rewritten unchecked conversion, in a system
9749 -- where Address is an integer type, always use the base type
9750 -- for a literal value. This is user-friendly and prevents
9751 -- order-of-elaboration issues with instances of unchecked
9752 -- conversion.
9754 if Nkind (Original_Node (Nod)) = N_Function_Call then
9755 Set_Etype (Nod, Base_Type (Etype (Nod)));
9756 end if;
9758 when N_Character_Literal
9759 | N_Real_Literal
9760 | N_String_Literal
9762 return;
9764 when N_Range =>
9765 Check_Expr_Constants (Low_Bound (Nod));
9766 Check_Expr_Constants (High_Bound (Nod));
9768 when N_Explicit_Dereference =>
9769 Check_Expr_Constants (Prefix (Nod));
9771 when N_Indexed_Component =>
9772 Check_Expr_Constants (Prefix (Nod));
9773 Check_List_Constants (Expressions (Nod));
9775 when N_Slice =>
9776 Check_Expr_Constants (Prefix (Nod));
9777 Check_Expr_Constants (Discrete_Range (Nod));
9779 when N_Selected_Component =>
9780 Check_Expr_Constants (Prefix (Nod));
9782 when N_Attribute_Reference =>
9783 if Nam_In (Attribute_Name (Nod), Name_Address,
9784 Name_Access,
9785 Name_Unchecked_Access,
9786 Name_Unrestricted_Access)
9787 then
9788 Check_At_Constant_Address (Prefix (Nod));
9790 -- Normally, System'To_Address will have been transformed into
9791 -- an Unchecked_Conversion, but in -gnatc mode, it will not,
9792 -- and we don't want to give an error, because the whole point
9793 -- of 'To_Address is that it is static.
9795 elsif Attribute_Name (Nod) = Name_To_Address then
9796 pragma Assert (Operating_Mode = Check_Semantics);
9797 null;
9799 else
9800 Check_Expr_Constants (Prefix (Nod));
9801 Check_List_Constants (Expressions (Nod));
9802 end if;
9804 when N_Aggregate =>
9805 Check_List_Constants (Component_Associations (Nod));
9806 Check_List_Constants (Expressions (Nod));
9808 when N_Component_Association =>
9809 Check_Expr_Constants (Expression (Nod));
9811 when N_Extension_Aggregate =>
9812 Check_Expr_Constants (Ancestor_Part (Nod));
9813 Check_List_Constants (Component_Associations (Nod));
9814 Check_List_Constants (Expressions (Nod));
9816 when N_Null =>
9817 return;
9819 when N_Binary_Op
9820 | N_Membership_Test
9821 | N_Short_Circuit
9823 Check_Expr_Constants (Left_Opnd (Nod));
9824 Check_Expr_Constants (Right_Opnd (Nod));
9826 when N_Unary_Op =>
9827 Check_Expr_Constants (Right_Opnd (Nod));
9829 when N_Allocator
9830 | N_Qualified_Expression
9831 | N_Type_Conversion
9832 | N_Unchecked_Type_Conversion
9834 Check_Expr_Constants (Expression (Nod));
9836 when N_Function_Call =>
9837 if not Is_Pure (Entity (Name (Nod))) then
9838 Error_Msg_NE
9839 ("invalid address clause for initialized object &!",
9840 Nod, U_Ent);
9842 Error_Msg_NE
9843 ("\function & is not pure (RM 13.1(22))!",
9844 Nod, Entity (Name (Nod)));
9846 else
9847 Check_List_Constants (Parameter_Associations (Nod));
9848 end if;
9850 when N_Parameter_Association =>
9851 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
9853 when others =>
9854 Error_Msg_NE
9855 ("invalid address clause for initialized object &!",
9856 Nod, U_Ent);
9857 Error_Msg_NE
9858 ("\must be constant defined before& (RM 13.1(22))!",
9859 Nod, U_Ent);
9860 end case;
9861 end Check_Expr_Constants;
9863 --------------------------
9864 -- Check_List_Constants --
9865 --------------------------
9867 procedure Check_List_Constants (Lst : List_Id) is
9868 Nod1 : Node_Id;
9870 begin
9871 if Present (Lst) then
9872 Nod1 := First (Lst);
9873 while Present (Nod1) loop
9874 Check_Expr_Constants (Nod1);
9875 Next (Nod1);
9876 end loop;
9877 end if;
9878 end Check_List_Constants;
9880 -- Start of processing for Check_Constant_Address_Clause
9882 begin
9883 -- If rep_clauses are to be ignored, no need for legality checks. In
9884 -- particular, no need to pester user about rep clauses that violate the
9885 -- rule on constant addresses, given that these clauses will be removed
9886 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9887 -- we want to relax these checks.
9889 if not Ignore_Rep_Clauses and not CodePeer_Mode then
9890 Check_Expr_Constants (Expr);
9891 end if;
9892 end Check_Constant_Address_Clause;
9894 ---------------------------
9895 -- Check_Pool_Size_Clash --
9896 ---------------------------
9898 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9899 Post : Node_Id;
9901 begin
9902 -- We need to find out which one came first. Note that in the case of
9903 -- aspects mixed with pragmas there are cases where the processing order
9904 -- is reversed, which is why we do the check here.
9906 if Sloc (SP) < Sloc (SS) then
9907 Error_Msg_Sloc := Sloc (SP);
9908 Post := SS;
9909 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9911 else
9912 Error_Msg_Sloc := Sloc (SS);
9913 Post := SP;
9914 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9915 end if;
9917 Error_Msg_N
9918 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9919 end Check_Pool_Size_Clash;
9921 ----------------------------------------
9922 -- Check_Record_Representation_Clause --
9923 ----------------------------------------
9925 procedure Check_Record_Representation_Clause (N : Node_Id) is
9926 Loc : constant Source_Ptr := Sloc (N);
9927 Ident : constant Node_Id := Identifier (N);
9928 Rectype : Entity_Id;
9929 Fent : Entity_Id;
9930 CC : Node_Id;
9931 Fbit : Uint;
9932 Lbit : Uint;
9933 Hbit : Uint := Uint_0;
9934 Comp : Entity_Id;
9935 Pcomp : Entity_Id;
9937 Max_Bit_So_Far : Uint;
9938 -- Records the maximum bit position so far. If all field positions
9939 -- are monotonically increasing, then we can skip the circuit for
9940 -- checking for overlap, since no overlap is possible.
9942 Tagged_Parent : Entity_Id := Empty;
9943 -- This is set in the case of an extension for which we have either a
9944 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
9945 -- components are positioned by record representation clauses) on the
9946 -- parent type. In this case we check for overlap between components of
9947 -- this tagged type and the parent component. Tagged_Parent will point
9948 -- to this parent type. For all other cases, Tagged_Parent is Empty.
9950 Parent_Last_Bit : Uint := No_Uint; -- init to avoid warning
9951 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9952 -- last bit position for any field in the parent type. We only need to
9953 -- check overlap for fields starting below this point.
9955 Overlap_Check_Required : Boolean;
9956 -- Used to keep track of whether or not an overlap check is required
9958 Overlap_Detected : Boolean := False;
9959 -- Set True if an overlap is detected
9961 Ccount : Natural := 0;
9962 -- Number of component clauses in record rep clause
9964 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9965 -- Given two entities for record components or discriminants, checks
9966 -- if they have overlapping component clauses and issues errors if so.
9968 procedure Find_Component;
9969 -- Finds component entity corresponding to current component clause (in
9970 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9971 -- start/stop bits for the field. If there is no matching component or
9972 -- if the matching component does not have a component clause, then
9973 -- that's an error and Comp is set to Empty, but no error message is
9974 -- issued, since the message was already given. Comp is also set to
9975 -- Empty if the current "component clause" is in fact a pragma.
9977 -----------------------------
9978 -- Check_Component_Overlap --
9979 -----------------------------
9981 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9982 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9983 CC2 : constant Node_Id := Component_Clause (C2_Ent);
9985 begin
9986 if Present (CC1) and then Present (CC2) then
9988 -- Exclude odd case where we have two tag components in the same
9989 -- record, both at location zero. This seems a bit strange, but
9990 -- it seems to happen in some circumstances, perhaps on an error.
9992 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9993 return;
9994 end if;
9996 -- Here we check if the two fields overlap
9998 declare
9999 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
10000 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
10001 E1 : constant Uint := S1 + Esize (C1_Ent);
10002 E2 : constant Uint := S2 + Esize (C2_Ent);
10004 begin
10005 if E2 <= S1 or else E1 <= S2 then
10006 null;
10007 else
10008 Error_Msg_Node_2 := Component_Name (CC2);
10009 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
10010 Error_Msg_Node_1 := Component_Name (CC1);
10011 Error_Msg_N
10012 ("component& overlaps & #", Component_Name (CC1));
10013 Overlap_Detected := True;
10014 end if;
10015 end;
10016 end if;
10017 end Check_Component_Overlap;
10019 --------------------
10020 -- Find_Component --
10021 --------------------
10023 procedure Find_Component is
10025 procedure Search_Component (R : Entity_Id);
10026 -- Search components of R for a match. If found, Comp is set
10028 ----------------------
10029 -- Search_Component --
10030 ----------------------
10032 procedure Search_Component (R : Entity_Id) is
10033 begin
10034 Comp := First_Component_Or_Discriminant (R);
10035 while Present (Comp) loop
10037 -- Ignore error of attribute name for component name (we
10038 -- already gave an error message for this, so no need to
10039 -- complain here)
10041 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
10042 null;
10043 else
10044 exit when Chars (Comp) = Chars (Component_Name (CC));
10045 end if;
10047 Next_Component_Or_Discriminant (Comp);
10048 end loop;
10049 end Search_Component;
10051 -- Start of processing for Find_Component
10053 begin
10054 -- Return with Comp set to Empty if we have a pragma
10056 if Nkind (CC) = N_Pragma then
10057 Comp := Empty;
10058 return;
10059 end if;
10061 -- Search current record for matching component
10063 Search_Component (Rectype);
10065 -- If not found, maybe component of base type discriminant that is
10066 -- absent from statically constrained first subtype.
10068 if No (Comp) then
10069 Search_Component (Base_Type (Rectype));
10070 end if;
10072 -- If no component, or the component does not reference the component
10073 -- clause in question, then there was some previous error for which
10074 -- we already gave a message, so just return with Comp Empty.
10076 if No (Comp) or else Component_Clause (Comp) /= CC then
10077 Check_Error_Detected;
10078 Comp := Empty;
10080 -- Normal case where we have a component clause
10082 else
10083 Fbit := Component_Bit_Offset (Comp);
10084 Lbit := Fbit + Esize (Comp) - 1;
10085 end if;
10086 end Find_Component;
10088 -- Start of processing for Check_Record_Representation_Clause
10090 begin
10091 Find_Type (Ident);
10092 Rectype := Entity (Ident);
10094 if Rectype = Any_Type then
10095 return;
10096 end if;
10098 Rectype := Underlying_Type (Rectype);
10100 -- See if we have a fully repped derived tagged type
10102 declare
10103 PS : constant Entity_Id := Parent_Subtype (Rectype);
10105 begin
10106 if Present (PS) and then Known_Static_RM_Size (PS) then
10107 Tagged_Parent := PS;
10108 Parent_Last_Bit := RM_Size (PS) - 1;
10110 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
10111 Tagged_Parent := PS;
10113 -- Find maximum bit of any component of the parent type
10115 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
10116 Pcomp := First_Entity (Tagged_Parent);
10117 while Present (Pcomp) loop
10118 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
10119 if Component_Bit_Offset (Pcomp) /= No_Uint
10120 and then Known_Static_Esize (Pcomp)
10121 then
10122 Parent_Last_Bit :=
10123 UI_Max
10124 (Parent_Last_Bit,
10125 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
10126 end if;
10127 else
10129 -- Skip anonymous types generated for constrained array
10130 -- or record components.
10132 null;
10133 end if;
10135 Next_Entity (Pcomp);
10136 end loop;
10137 end if;
10138 end;
10140 -- All done if no component clauses
10142 CC := First (Component_Clauses (N));
10144 if No (CC) then
10145 return;
10146 end if;
10148 -- If a tag is present, then create a component clause that places it
10149 -- at the start of the record (otherwise gigi may place it after other
10150 -- fields that have rep clauses).
10152 Fent := First_Entity (Rectype);
10154 if Nkind (Fent) = N_Defining_Identifier
10155 and then Chars (Fent) = Name_uTag
10156 then
10157 Set_Component_Bit_Offset (Fent, Uint_0);
10158 Set_Normalized_Position (Fent, Uint_0);
10159 Set_Normalized_First_Bit (Fent, Uint_0);
10160 Set_Normalized_Position_Max (Fent, Uint_0);
10161 Init_Esize (Fent, System_Address_Size);
10163 Set_Component_Clause (Fent,
10164 Make_Component_Clause (Loc,
10165 Component_Name => Make_Identifier (Loc, Name_uTag),
10167 Position => Make_Integer_Literal (Loc, Uint_0),
10168 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10169 Last_Bit =>
10170 Make_Integer_Literal (Loc,
10171 UI_From_Int (System_Address_Size))));
10173 Ccount := Ccount + 1;
10174 end if;
10176 Max_Bit_So_Far := Uint_Minus_1;
10177 Overlap_Check_Required := False;
10179 -- Process the component clauses
10181 while Present (CC) loop
10182 Find_Component;
10184 if Present (Comp) then
10185 Ccount := Ccount + 1;
10187 -- We need a full overlap check if record positions non-monotonic
10189 if Fbit <= Max_Bit_So_Far then
10190 Overlap_Check_Required := True;
10191 end if;
10193 Max_Bit_So_Far := Lbit;
10195 -- Check bit position out of range of specified size
10197 if Has_Size_Clause (Rectype)
10198 and then RM_Size (Rectype) <= Lbit
10199 then
10200 Error_Msg_N
10201 ("bit number out of range of specified size",
10202 Last_Bit (CC));
10204 -- Check for overlap with tag or parent component
10206 else
10207 if Is_Tagged_Type (Rectype)
10208 and then Fbit < System_Address_Size
10209 then
10210 Error_Msg_NE
10211 ("component overlaps tag field of&",
10212 Component_Name (CC), Rectype);
10213 Overlap_Detected := True;
10215 elsif Present (Tagged_Parent)
10216 and then Fbit <= Parent_Last_Bit
10217 then
10218 Error_Msg_NE
10219 ("component overlaps parent field of&",
10220 Component_Name (CC), Rectype);
10221 Overlap_Detected := True;
10222 end if;
10224 if Hbit < Lbit then
10225 Hbit := Lbit;
10226 end if;
10227 end if;
10228 end if;
10230 Next (CC);
10231 end loop;
10233 -- Now that we have processed all the component clauses, check for
10234 -- overlap. We have to leave this till last, since the components can
10235 -- appear in any arbitrary order in the representation clause.
10237 -- We do not need this check if all specified ranges were monotonic,
10238 -- as recorded by Overlap_Check_Required being False at this stage.
10240 -- This first section checks if there are any overlapping entries at
10241 -- all. It does this by sorting all entries and then seeing if there are
10242 -- any overlaps. If there are none, then that is decisive, but if there
10243 -- are overlaps, they may still be OK (they may result from fields in
10244 -- different variants).
10246 if Overlap_Check_Required then
10247 Overlap_Check1 : declare
10249 OC_Fbit : array (0 .. Ccount) of Uint;
10250 -- First-bit values for component clauses, the value is the offset
10251 -- of the first bit of the field from start of record. The zero
10252 -- entry is for use in sorting.
10254 OC_Lbit : array (0 .. Ccount) of Uint;
10255 -- Last-bit values for component clauses, the value is the offset
10256 -- of the last bit of the field from start of record. The zero
10257 -- entry is for use in sorting.
10259 OC_Count : Natural := 0;
10260 -- Count of entries in OC_Fbit and OC_Lbit
10262 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10263 -- Compare routine for Sort
10265 procedure OC_Move (From : Natural; To : Natural);
10266 -- Move routine for Sort
10268 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
10270 -----------
10271 -- OC_Lt --
10272 -----------
10274 function OC_Lt (Op1, Op2 : Natural) return Boolean is
10275 begin
10276 return OC_Fbit (Op1) < OC_Fbit (Op2);
10277 end OC_Lt;
10279 -------------
10280 -- OC_Move --
10281 -------------
10283 procedure OC_Move (From : Natural; To : Natural) is
10284 begin
10285 OC_Fbit (To) := OC_Fbit (From);
10286 OC_Lbit (To) := OC_Lbit (From);
10287 end OC_Move;
10289 -- Start of processing for Overlap_Check
10291 begin
10292 CC := First (Component_Clauses (N));
10293 while Present (CC) loop
10295 -- Exclude component clause already marked in error
10297 if not Error_Posted (CC) then
10298 Find_Component;
10300 if Present (Comp) then
10301 OC_Count := OC_Count + 1;
10302 OC_Fbit (OC_Count) := Fbit;
10303 OC_Lbit (OC_Count) := Lbit;
10304 end if;
10305 end if;
10307 Next (CC);
10308 end loop;
10310 Sorting.Sort (OC_Count);
10312 Overlap_Check_Required := False;
10313 for J in 1 .. OC_Count - 1 loop
10314 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10315 Overlap_Check_Required := True;
10316 exit;
10317 end if;
10318 end loop;
10319 end Overlap_Check1;
10320 end if;
10322 -- If Overlap_Check_Required is still True, then we have to do the full
10323 -- scale overlap check, since we have at least two fields that do
10324 -- overlap, and we need to know if that is OK since they are in
10325 -- different variant, or whether we have a definite problem.
10327 if Overlap_Check_Required then
10328 Overlap_Check2 : declare
10329 C1_Ent, C2_Ent : Entity_Id;
10330 -- Entities of components being checked for overlap
10332 Clist : Node_Id;
10333 -- Component_List node whose Component_Items are being checked
10335 Citem : Node_Id;
10336 -- Component declaration for component being checked
10338 begin
10339 C1_Ent := First_Entity (Base_Type (Rectype));
10341 -- Loop through all components in record. For each component check
10342 -- for overlap with any of the preceding elements on the component
10343 -- list containing the component and also, if the component is in
10344 -- a variant, check against components outside the case structure.
10345 -- This latter test is repeated recursively up the variant tree.
10347 Main_Component_Loop : while Present (C1_Ent) loop
10348 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10349 goto Continue_Main_Component_Loop;
10350 end if;
10352 -- Skip overlap check if entity has no declaration node. This
10353 -- happens with discriminants in constrained derived types.
10354 -- Possibly we are missing some checks as a result, but that
10355 -- does not seem terribly serious.
10357 if No (Declaration_Node (C1_Ent)) then
10358 goto Continue_Main_Component_Loop;
10359 end if;
10361 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
10363 -- Loop through component lists that need checking. Check the
10364 -- current component list and all lists in variants above us.
10366 Component_List_Loop : loop
10368 -- If derived type definition, go to full declaration
10369 -- If at outer level, check discriminants if there are any.
10371 if Nkind (Clist) = N_Derived_Type_Definition then
10372 Clist := Parent (Clist);
10373 end if;
10375 -- Outer level of record definition, check discriminants
10377 if Nkind_In (Clist, N_Full_Type_Declaration,
10378 N_Private_Type_Declaration)
10379 then
10380 if Has_Discriminants (Defining_Identifier (Clist)) then
10381 C2_Ent :=
10382 First_Discriminant (Defining_Identifier (Clist));
10383 while Present (C2_Ent) loop
10384 exit when C1_Ent = C2_Ent;
10385 Check_Component_Overlap (C1_Ent, C2_Ent);
10386 Next_Discriminant (C2_Ent);
10387 end loop;
10388 end if;
10390 -- Record extension case
10392 elsif Nkind (Clist) = N_Derived_Type_Definition then
10393 Clist := Empty;
10395 -- Otherwise check one component list
10397 else
10398 Citem := First (Component_Items (Clist));
10399 while Present (Citem) loop
10400 if Nkind (Citem) = N_Component_Declaration then
10401 C2_Ent := Defining_Identifier (Citem);
10402 exit when C1_Ent = C2_Ent;
10403 Check_Component_Overlap (C1_Ent, C2_Ent);
10404 end if;
10406 Next (Citem);
10407 end loop;
10408 end if;
10410 -- Check for variants above us (the parent of the Clist can
10411 -- be a variant, in which case its parent is a variant part,
10412 -- and the parent of the variant part is a component list
10413 -- whose components must all be checked against the current
10414 -- component for overlap).
10416 if Nkind (Parent (Clist)) = N_Variant then
10417 Clist := Parent (Parent (Parent (Clist)));
10419 -- Check for possible discriminant part in record, this
10420 -- is treated essentially as another level in the
10421 -- recursion. For this case the parent of the component
10422 -- list is the record definition, and its parent is the
10423 -- full type declaration containing the discriminant
10424 -- specifications.
10426 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10427 Clist := Parent (Parent ((Clist)));
10429 -- If neither of these two cases, we are at the top of
10430 -- the tree.
10432 else
10433 exit Component_List_Loop;
10434 end if;
10435 end loop Component_List_Loop;
10437 <<Continue_Main_Component_Loop>>
10438 Next_Entity (C1_Ent);
10440 end loop Main_Component_Loop;
10441 end Overlap_Check2;
10442 end if;
10444 -- The following circuit deals with warning on record holes (gaps). We
10445 -- skip this check if overlap was detected, since it makes sense for the
10446 -- programmer to fix this illegality before worrying about warnings.
10448 if not Overlap_Detected and Warn_On_Record_Holes then
10449 Record_Hole_Check : declare
10450 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10451 -- Full declaration of record type
10453 procedure Check_Component_List
10454 (CL : Node_Id;
10455 Sbit : Uint;
10456 DS : List_Id);
10457 -- Check component list CL for holes. The starting bit should be
10458 -- Sbit. which is zero for the main record component list and set
10459 -- appropriately for recursive calls for variants. DS is set to
10460 -- a list of discriminant specifications to be included in the
10461 -- consideration of components. It is No_List if none to consider.
10463 --------------------------
10464 -- Check_Component_List --
10465 --------------------------
10467 procedure Check_Component_List
10468 (CL : Node_Id;
10469 Sbit : Uint;
10470 DS : List_Id)
10472 Compl : Integer;
10474 begin
10475 Compl := Integer (List_Length (Component_Items (CL)));
10477 if DS /= No_List then
10478 Compl := Compl + Integer (List_Length (DS));
10479 end if;
10481 declare
10482 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10483 -- Gather components (zero entry is for sort routine)
10485 Ncomps : Natural := 0;
10486 -- Number of entries stored in Comps (starting at Comps (1))
10488 Citem : Node_Id;
10489 -- One component item or discriminant specification
10491 Nbit : Uint;
10492 -- Starting bit for next component
10494 CEnt : Entity_Id;
10495 -- Component entity
10497 Variant : Node_Id;
10498 -- One variant
10500 function Lt (Op1, Op2 : Natural) return Boolean;
10501 -- Compare routine for Sort
10503 procedure Move (From : Natural; To : Natural);
10504 -- Move routine for Sort
10506 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
10508 --------
10509 -- Lt --
10510 --------
10512 function Lt (Op1, Op2 : Natural) return Boolean is
10513 begin
10514 return Component_Bit_Offset (Comps (Op1))
10516 Component_Bit_Offset (Comps (Op2));
10517 end Lt;
10519 ----------
10520 -- Move --
10521 ----------
10523 procedure Move (From : Natural; To : Natural) is
10524 begin
10525 Comps (To) := Comps (From);
10526 end Move;
10528 begin
10529 -- Gather discriminants into Comp
10531 if DS /= No_List then
10532 Citem := First (DS);
10533 while Present (Citem) loop
10534 if Nkind (Citem) = N_Discriminant_Specification then
10535 declare
10536 Ent : constant Entity_Id :=
10537 Defining_Identifier (Citem);
10538 begin
10539 if Ekind (Ent) = E_Discriminant then
10540 Ncomps := Ncomps + 1;
10541 Comps (Ncomps) := Ent;
10542 end if;
10543 end;
10544 end if;
10546 Next (Citem);
10547 end loop;
10548 end if;
10550 -- Gather component entities into Comp
10552 Citem := First (Component_Items (CL));
10553 while Present (Citem) loop
10554 if Nkind (Citem) = N_Component_Declaration then
10555 Ncomps := Ncomps + 1;
10556 Comps (Ncomps) := Defining_Identifier (Citem);
10557 end if;
10559 Next (Citem);
10560 end loop;
10562 -- Now sort the component entities based on the first bit.
10563 -- Note we already know there are no overlapping components.
10565 Sorting.Sort (Ncomps);
10567 -- Loop through entries checking for holes
10569 Nbit := Sbit;
10570 for J in 1 .. Ncomps loop
10571 CEnt := Comps (J);
10573 declare
10574 CBO : constant Uint := Component_Bit_Offset (CEnt);
10576 begin
10577 -- Skip components with unknown offsets
10579 if CBO /= No_Uint and then CBO >= 0 then
10580 Error_Msg_Uint_1 := CBO - Nbit;
10582 if Error_Msg_Uint_1 > 0 then
10583 Error_Msg_NE
10584 ("?H?^-bit gap before component&",
10585 Component_Name (Component_Clause (CEnt)),
10586 CEnt);
10587 end if;
10589 Nbit := CBO + Esize (CEnt);
10590 end if;
10591 end;
10592 end loop;
10594 -- Process variant parts recursively if present
10596 if Present (Variant_Part (CL)) then
10597 Variant := First (Variants (Variant_Part (CL)));
10598 while Present (Variant) loop
10599 Check_Component_List
10600 (Component_List (Variant), Nbit, No_List);
10601 Next (Variant);
10602 end loop;
10603 end if;
10604 end;
10605 end Check_Component_List;
10607 -- Start of processing for Record_Hole_Check
10609 begin
10610 declare
10611 Sbit : Uint;
10613 begin
10614 if Is_Tagged_Type (Rectype) then
10615 Sbit := UI_From_Int (System_Address_Size);
10616 else
10617 Sbit := Uint_0;
10618 end if;
10620 if Nkind (Decl) = N_Full_Type_Declaration
10621 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10622 then
10623 Check_Component_List
10624 (Component_List (Type_Definition (Decl)),
10625 Sbit,
10626 Discriminant_Specifications (Decl));
10627 end if;
10628 end;
10629 end Record_Hole_Check;
10630 end if;
10632 -- For records that have component clauses for all components, and whose
10633 -- size is less than or equal to 32, we need to know the size in the
10634 -- front end to activate possible packed array processing where the
10635 -- component type is a record.
10637 -- At this stage Hbit + 1 represents the first unused bit from all the
10638 -- component clauses processed, so if the component clauses are
10639 -- complete, then this is the length of the record.
10641 -- For records longer than System.Storage_Unit, and for those where not
10642 -- all components have component clauses, the back end determines the
10643 -- length (it may for example be appropriate to round up the size
10644 -- to some convenient boundary, based on alignment considerations, etc).
10646 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
10648 -- Nothing to do if at least one component has no component clause
10650 Comp := First_Component_Or_Discriminant (Rectype);
10651 while Present (Comp) loop
10652 exit when No (Component_Clause (Comp));
10653 Next_Component_Or_Discriminant (Comp);
10654 end loop;
10656 -- If we fall out of loop, all components have component clauses
10657 -- and so we can set the size to the maximum value.
10659 if No (Comp) then
10660 Set_RM_Size (Rectype, Hbit + 1);
10661 end if;
10662 end if;
10663 end Check_Record_Representation_Clause;
10665 ----------------
10666 -- Check_Size --
10667 ----------------
10669 procedure Check_Size
10670 (N : Node_Id;
10671 T : Entity_Id;
10672 Siz : Uint;
10673 Biased : out Boolean)
10675 procedure Size_Too_Small_Error (Min_Siz : Uint);
10676 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10677 -- minimum size.
10679 --------------------------
10680 -- Size_Too_Small_Error --
10681 --------------------------
10683 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10684 begin
10685 -- This error is suppressed in ASIS mode to allow for different ASIS
10686 -- back ends or ASIS-based tools to query the illegal clause.
10688 if not ASIS_Mode then
10689 Error_Msg_Uint_1 := Min_Siz;
10690 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
10691 end if;
10692 end Size_Too_Small_Error;
10694 -- Local variables
10696 UT : constant Entity_Id := Underlying_Type (T);
10697 M : Uint;
10699 -- Start of processing for Check_Size
10701 begin
10702 Biased := False;
10704 -- Reject patently improper size values
10706 if Is_Elementary_Type (T)
10707 and then Siz > UI_From_Int (Int'Last)
10708 then
10709 Error_Msg_N ("Size value too large for elementary type", N);
10711 if Nkind (Original_Node (N)) = N_Op_Expon then
10712 Error_Msg_N
10713 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10714 end if;
10715 end if;
10717 -- Dismiss generic types
10719 if Is_Generic_Type (T)
10720 or else
10721 Is_Generic_Type (UT)
10722 or else
10723 Is_Generic_Type (Root_Type (UT))
10724 then
10725 return;
10727 -- Guard against previous errors
10729 elsif No (UT) or else UT = Any_Type then
10730 Check_Error_Detected;
10731 return;
10733 -- Check case of bit packed array
10735 elsif Is_Array_Type (UT)
10736 and then Known_Static_Component_Size (UT)
10737 and then Is_Bit_Packed_Array (UT)
10738 then
10739 declare
10740 Asiz : Uint;
10741 Indx : Node_Id;
10742 Ityp : Entity_Id;
10744 begin
10745 Asiz := Component_Size (UT);
10746 Indx := First_Index (UT);
10747 loop
10748 Ityp := Etype (Indx);
10750 -- If non-static bound, then we are not in the business of
10751 -- trying to check the length, and indeed an error will be
10752 -- issued elsewhere, since sizes of non-static array types
10753 -- cannot be set implicitly or explicitly.
10755 if not Is_OK_Static_Subtype (Ityp) then
10756 return;
10757 end if;
10759 -- Otherwise accumulate next dimension
10761 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10762 Expr_Value (Type_Low_Bound (Ityp)) +
10763 Uint_1);
10765 Next_Index (Indx);
10766 exit when No (Indx);
10767 end loop;
10769 if Asiz <= Siz then
10770 return;
10772 else
10773 Size_Too_Small_Error (Asiz);
10774 Set_Esize (T, Asiz);
10775 Set_RM_Size (T, Asiz);
10776 end if;
10777 end;
10779 -- All other composite types are ignored
10781 elsif Is_Composite_Type (UT) then
10782 return;
10784 -- For fixed-point types, don't check minimum if type is not frozen,
10785 -- since we don't know all the characteristics of the type that can
10786 -- affect the size (e.g. a specified small) till freeze time.
10788 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
10789 null;
10791 -- Cases for which a minimum check is required
10793 else
10794 -- Ignore if specified size is correct for the type
10796 if Known_Esize (UT) and then Siz = Esize (UT) then
10797 return;
10798 end if;
10800 -- Otherwise get minimum size
10802 M := UI_From_Int (Minimum_Size (UT));
10804 if Siz < M then
10806 -- Size is less than minimum size, but one possibility remains
10807 -- that we can manage with the new size if we bias the type.
10809 M := UI_From_Int (Minimum_Size (UT, Biased => True));
10811 if Siz < M then
10812 Size_Too_Small_Error (M);
10813 Set_Esize (T, M);
10814 Set_RM_Size (T, M);
10815 else
10816 Biased := True;
10817 end if;
10818 end if;
10819 end if;
10820 end Check_Size;
10822 --------------------------
10823 -- Freeze_Entity_Checks --
10824 --------------------------
10826 procedure Freeze_Entity_Checks (N : Node_Id) is
10827 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10828 -- Inspect the primitive operations of type Typ and hide all pairs of
10829 -- implicitly declared non-overridden non-fully conformant homographs
10830 -- (Ada RM 8.3 12.3/2).
10832 -------------------------------------
10833 -- Hide_Non_Overridden_Subprograms --
10834 -------------------------------------
10836 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10837 procedure Hide_Matching_Homographs
10838 (Subp_Id : Entity_Id;
10839 Start_Elmt : Elmt_Id);
10840 -- Inspect a list of primitive operations starting with Start_Elmt
10841 -- and find matching implicitly declared non-overridden non-fully
10842 -- conformant homographs of Subp_Id. If found, all matches along
10843 -- with Subp_Id are hidden from all visibility.
10845 function Is_Non_Overridden_Or_Null_Procedure
10846 (Subp_Id : Entity_Id) return Boolean;
10847 -- Determine whether subprogram Subp_Id is implicitly declared non-
10848 -- overridden subprogram or an implicitly declared null procedure.
10850 ------------------------------
10851 -- Hide_Matching_Homographs --
10852 ------------------------------
10854 procedure Hide_Matching_Homographs
10855 (Subp_Id : Entity_Id;
10856 Start_Elmt : Elmt_Id)
10858 Prim : Entity_Id;
10859 Prim_Elmt : Elmt_Id;
10861 begin
10862 Prim_Elmt := Start_Elmt;
10863 while Present (Prim_Elmt) loop
10864 Prim := Node (Prim_Elmt);
10866 -- The current primitive is implicitly declared non-overridden
10867 -- non-fully conformant homograph of Subp_Id. Both subprograms
10868 -- must be hidden from visibility.
10870 if Chars (Prim) = Chars (Subp_Id)
10871 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
10872 and then not Fully_Conformant (Prim, Subp_Id)
10873 then
10874 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10875 Set_Is_Immediately_Visible (Prim, False);
10876 Set_Is_Potentially_Use_Visible (Prim, False);
10878 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10879 Set_Is_Immediately_Visible (Subp_Id, False);
10880 Set_Is_Potentially_Use_Visible (Subp_Id, False);
10881 end if;
10883 Next_Elmt (Prim_Elmt);
10884 end loop;
10885 end Hide_Matching_Homographs;
10887 -----------------------------------------
10888 -- Is_Non_Overridden_Or_Null_Procedure --
10889 -----------------------------------------
10891 function Is_Non_Overridden_Or_Null_Procedure
10892 (Subp_Id : Entity_Id) return Boolean
10894 Alias_Id : Entity_Id;
10896 begin
10897 -- The subprogram is inherited (implicitly declared), it does not
10898 -- override and does not cover a primitive of an interface.
10900 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10901 and then Present (Alias (Subp_Id))
10902 and then No (Interface_Alias (Subp_Id))
10903 and then No (Overridden_Operation (Subp_Id))
10904 then
10905 Alias_Id := Alias (Subp_Id);
10907 if Requires_Overriding (Alias_Id) then
10908 return True;
10910 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10911 and then Null_Present (Parent (Alias_Id))
10912 then
10913 return True;
10914 end if;
10915 end if;
10917 return False;
10918 end Is_Non_Overridden_Or_Null_Procedure;
10920 -- Local variables
10922 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10923 Prim : Entity_Id;
10924 Prim_Elmt : Elmt_Id;
10926 -- Start of processing for Hide_Non_Overridden_Subprograms
10928 begin
10929 -- Inspect the list of primitives looking for non-overridden
10930 -- subprograms.
10932 if Present (Prim_Ops) then
10933 Prim_Elmt := First_Elmt (Prim_Ops);
10934 while Present (Prim_Elmt) loop
10935 Prim := Node (Prim_Elmt);
10936 Next_Elmt (Prim_Elmt);
10938 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10939 Hide_Matching_Homographs
10940 (Subp_Id => Prim,
10941 Start_Elmt => Prim_Elmt);
10942 end if;
10943 end loop;
10944 end if;
10945 end Hide_Non_Overridden_Subprograms;
10947 -- Local variables
10949 E : constant Entity_Id := Entity (N);
10951 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10952 -- True in nongeneric case. Some of the processing here is skipped
10953 -- for the generic case since it is not needed. Basically in the
10954 -- generic case, we only need to do stuff that might generate error
10955 -- messages or warnings.
10957 -- Start of processing for Freeze_Entity_Checks
10959 begin
10960 -- Remember that we are processing a freezing entity. Required to
10961 -- ensure correct decoration of internal entities associated with
10962 -- interfaces (see New_Overloaded_Entity).
10964 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
10966 -- For tagged types covering interfaces add internal entities that link
10967 -- the primitives of the interfaces with the primitives that cover them.
10968 -- Note: These entities were originally generated only when generating
10969 -- code because their main purpose was to provide support to initialize
10970 -- the secondary dispatch tables. They are now generated also when
10971 -- compiling with no code generation to provide ASIS the relationship
10972 -- between interface primitives and tagged type primitives. They are
10973 -- also used to locate primitives covering interfaces when processing
10974 -- generics (see Derive_Subprograms).
10976 -- This is not needed in the generic case
10978 if Ada_Version >= Ada_2005
10979 and then Nongeneric_Case
10980 and then Ekind (E) = E_Record_Type
10981 and then Is_Tagged_Type (E)
10982 and then not Is_Interface (E)
10983 and then Has_Interfaces (E)
10984 then
10985 -- This would be a good common place to call the routine that checks
10986 -- overriding of interface primitives (and thus factorize calls to
10987 -- Check_Abstract_Overriding located at different contexts in the
10988 -- compiler). However, this is not possible because it causes
10989 -- spurious errors in case of late overriding.
10991 Add_Internal_Interface_Entities (E);
10992 end if;
10994 -- After all forms of overriding have been resolved, a tagged type may
10995 -- be left with a set of implicitly declared and possibly erroneous
10996 -- abstract subprograms, null procedures and subprograms that require
10997 -- overriding. If this set contains fully conformant homographs, then
10998 -- one is chosen arbitrarily (already done during resolution), otherwise
10999 -- all remaining non-fully conformant homographs are hidden from
11000 -- visibility (Ada RM 8.3 12.3/2).
11002 if Is_Tagged_Type (E) then
11003 Hide_Non_Overridden_Subprograms (E);
11004 end if;
11006 -- Check CPP types
11008 if Ekind (E) = E_Record_Type
11009 and then Is_CPP_Class (E)
11010 and then Is_Tagged_Type (E)
11011 and then Tagged_Type_Expansion
11012 then
11013 if CPP_Num_Prims (E) = 0 then
11015 -- If the CPP type has user defined components then it must import
11016 -- primitives from C++. This is required because if the C++ class
11017 -- has no primitives then the C++ compiler does not added the _tag
11018 -- component to the type.
11020 if First_Entity (E) /= Last_Entity (E) then
11021 Error_Msg_N
11022 ("'C'P'P type must import at least one primitive from C++??",
11024 end if;
11025 end if;
11027 -- Check that all its primitives are abstract or imported from C++.
11028 -- Check also availability of the C++ constructor.
11030 declare
11031 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
11032 Elmt : Elmt_Id;
11033 Error_Reported : Boolean := False;
11034 Prim : Node_Id;
11036 begin
11037 Elmt := First_Elmt (Primitive_Operations (E));
11038 while Present (Elmt) loop
11039 Prim := Node (Elmt);
11041 if Comes_From_Source (Prim) then
11042 if Is_Abstract_Subprogram (Prim) then
11043 null;
11045 elsif not Is_Imported (Prim)
11046 or else Convention (Prim) /= Convention_CPP
11047 then
11048 Error_Msg_N
11049 ("primitives of 'C'P'P types must be imported from C++ "
11050 & "or abstract??", Prim);
11052 elsif not Has_Constructors
11053 and then not Error_Reported
11054 then
11055 Error_Msg_Name_1 := Chars (E);
11056 Error_Msg_N
11057 ("??'C'P'P constructor required for type %", Prim);
11058 Error_Reported := True;
11059 end if;
11060 end if;
11062 Next_Elmt (Elmt);
11063 end loop;
11064 end;
11065 end if;
11067 -- Check Ada derivation of CPP type
11069 if Expander_Active -- why? losing errors in -gnatc mode???
11070 and then Present (Etype (E)) -- defend against errors
11071 and then Tagged_Type_Expansion
11072 and then Ekind (E) = E_Record_Type
11073 and then Etype (E) /= E
11074 and then Is_CPP_Class (Etype (E))
11075 and then CPP_Num_Prims (Etype (E)) > 0
11076 and then not Is_CPP_Class (E)
11077 and then not Has_CPP_Constructors (Etype (E))
11078 then
11079 -- If the parent has C++ primitives but it has no constructor then
11080 -- check that all the primitives are overridden in this derivation;
11081 -- otherwise the constructor of the parent is needed to build the
11082 -- dispatch table.
11084 declare
11085 Elmt : Elmt_Id;
11086 Prim : Node_Id;
11088 begin
11089 Elmt := First_Elmt (Primitive_Operations (E));
11090 while Present (Elmt) loop
11091 Prim := Node (Elmt);
11093 if not Is_Abstract_Subprogram (Prim)
11094 and then No (Interface_Alias (Prim))
11095 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
11096 then
11097 Error_Msg_Name_1 := Chars (Etype (E));
11098 Error_Msg_N
11099 ("'C'P'P constructor required for parent type %", E);
11100 exit;
11101 end if;
11103 Next_Elmt (Elmt);
11104 end loop;
11105 end;
11106 end if;
11108 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
11110 -- If we have a type with predicates, build predicate function. This is
11111 -- not needed in the generic case, nor within TSS subprograms and other
11112 -- predefined primitives.
11114 if Is_Type (E)
11115 and then Nongeneric_Case
11116 and then not Within_Internal_Subprogram
11117 and then Has_Predicates (E)
11118 then
11119 Build_Predicate_Functions (E, N);
11120 end if;
11122 -- If type has delayed aspects, this is where we do the preanalysis at
11123 -- the freeze point, as part of the consistent visibility check. Note
11124 -- that this must be done after calling Build_Predicate_Functions or
11125 -- Build_Invariant_Procedure since these subprograms fix occurrences of
11126 -- the subtype name in the saved expression so that they will not cause
11127 -- trouble in the preanalysis.
11129 -- This is also not needed in the generic case
11131 if Nongeneric_Case
11132 and then Has_Delayed_Aspects (E)
11133 and then Scope (E) = Current_Scope
11134 then
11135 -- Retrieve the visibility to the discriminants in order to properly
11136 -- analyze the aspects.
11138 Push_Scope_And_Install_Discriminants (E);
11140 declare
11141 Ritem : Node_Id;
11143 begin
11144 -- Look for aspect specification entries for this entity
11146 Ritem := First_Rep_Item (E);
11147 while Present (Ritem) loop
11148 if Nkind (Ritem) = N_Aspect_Specification
11149 and then Entity (Ritem) = E
11150 and then Is_Delayed_Aspect (Ritem)
11151 then
11152 Check_Aspect_At_Freeze_Point (Ritem);
11153 end if;
11155 Next_Rep_Item (Ritem);
11156 end loop;
11157 end;
11159 Uninstall_Discriminants_And_Pop_Scope (E);
11160 end if;
11162 -- For a record type, deal with variant parts. This has to be delayed
11163 -- to this point, because of the issue of statically predicated
11164 -- subtypes, which we have to ensure are frozen before checking
11165 -- choices, since we need to have the static choice list set.
11167 if Is_Record_Type (E) then
11168 Check_Variant_Part : declare
11169 D : constant Node_Id := Declaration_Node (E);
11170 T : Node_Id;
11171 C : Node_Id;
11172 VP : Node_Id;
11174 Others_Present : Boolean;
11175 pragma Warnings (Off, Others_Present);
11176 -- Indicates others present, not used in this case
11178 procedure Non_Static_Choice_Error (Choice : Node_Id);
11179 -- Error routine invoked by the generic instantiation below when
11180 -- the variant part has a non static choice.
11182 procedure Process_Declarations (Variant : Node_Id);
11183 -- Processes declarations associated with a variant. We analyzed
11184 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11185 -- but we still need the recursive call to Check_Choices for any
11186 -- nested variant to get its choices properly processed. This is
11187 -- also where we expand out the choices if expansion is active.
11189 package Variant_Choices_Processing is new
11190 Generic_Check_Choices
11191 (Process_Empty_Choice => No_OP,
11192 Process_Non_Static_Choice => Non_Static_Choice_Error,
11193 Process_Associated_Node => Process_Declarations);
11194 use Variant_Choices_Processing;
11196 -----------------------------
11197 -- Non_Static_Choice_Error --
11198 -----------------------------
11200 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11201 begin
11202 Flag_Non_Static_Expr
11203 ("choice given in variant part is not static!", Choice);
11204 end Non_Static_Choice_Error;
11206 --------------------------
11207 -- Process_Declarations --
11208 --------------------------
11210 procedure Process_Declarations (Variant : Node_Id) is
11211 CL : constant Node_Id := Component_List (Variant);
11212 VP : Node_Id;
11214 begin
11215 -- Check for static predicate present in this variant
11217 if Has_SP_Choice (Variant) then
11219 -- Here we expand. You might expect to find this call in
11220 -- Expand_N_Variant_Part, but that is called when we first
11221 -- see the variant part, and we cannot do this expansion
11222 -- earlier than the freeze point, since for statically
11223 -- predicated subtypes, the predicate is not known till
11224 -- the freeze point.
11226 -- Furthermore, we do this expansion even if the expander
11227 -- is not active, because other semantic processing, e.g.
11228 -- for aggregates, requires the expanded list of choices.
11230 -- If the expander is not active, then we can't just clobber
11231 -- the list since it would invalidate the ASIS -gnatct tree.
11232 -- So we have to rewrite the variant part with a Rewrite
11233 -- call that replaces it with a copy and clobber the copy.
11235 if not Expander_Active then
11236 declare
11237 NewV : constant Node_Id := New_Copy (Variant);
11238 begin
11239 Set_Discrete_Choices
11240 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11241 Rewrite (Variant, NewV);
11242 end;
11243 end if;
11245 Expand_Static_Predicates_In_Choices (Variant);
11246 end if;
11248 -- We don't need to worry about the declarations in the variant
11249 -- (since they were analyzed by Analyze_Choices when we first
11250 -- encountered the variant), but we do need to take care of
11251 -- expansion of any nested variants.
11253 if not Null_Present (CL) then
11254 VP := Variant_Part (CL);
11256 if Present (VP) then
11257 Check_Choices
11258 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11259 end if;
11260 end if;
11261 end Process_Declarations;
11263 -- Start of processing for Check_Variant_Part
11265 begin
11266 -- Find component list
11268 C := Empty;
11270 if Nkind (D) = N_Full_Type_Declaration then
11271 T := Type_Definition (D);
11273 if Nkind (T) = N_Record_Definition then
11274 C := Component_List (T);
11276 elsif Nkind (T) = N_Derived_Type_Definition
11277 and then Present (Record_Extension_Part (T))
11278 then
11279 C := Component_List (Record_Extension_Part (T));
11280 end if;
11281 end if;
11283 -- Case of variant part present
11285 if Present (C) and then Present (Variant_Part (C)) then
11286 VP := Variant_Part (C);
11288 -- Check choices
11290 Check_Choices
11291 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11293 -- If the last variant does not contain the Others choice,
11294 -- replace it with an N_Others_Choice node since Gigi always
11295 -- wants an Others. Note that we do not bother to call Analyze
11296 -- on the modified variant part, since its only effect would be
11297 -- to compute the Others_Discrete_Choices node laboriously, and
11298 -- of course we already know the list of choices corresponding
11299 -- to the others choice (it's the list we're replacing).
11301 -- We only want to do this if the expander is active, since
11302 -- we do not want to clobber the ASIS tree.
11304 if Expander_Active then
11305 declare
11306 Last_Var : constant Node_Id :=
11307 Last_Non_Pragma (Variants (VP));
11309 Others_Node : Node_Id;
11311 begin
11312 if Nkind (First (Discrete_Choices (Last_Var))) /=
11313 N_Others_Choice
11314 then
11315 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11316 Set_Others_Discrete_Choices
11317 (Others_Node, Discrete_Choices (Last_Var));
11318 Set_Discrete_Choices
11319 (Last_Var, New_List (Others_Node));
11320 end if;
11321 end;
11322 end if;
11323 end if;
11324 end Check_Variant_Part;
11325 end if;
11326 end Freeze_Entity_Checks;
11328 -------------------------
11329 -- Get_Alignment_Value --
11330 -------------------------
11332 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11333 Align : constant Uint := Static_Integer (Expr);
11335 begin
11336 if Align = No_Uint then
11337 return No_Uint;
11339 elsif Align <= 0 then
11341 -- This error is suppressed in ASIS mode to allow for different ASIS
11342 -- back ends or ASIS-based tools to query the illegal clause.
11344 if not ASIS_Mode then
11345 Error_Msg_N ("alignment value must be positive", Expr);
11346 end if;
11348 return No_Uint;
11350 else
11351 for J in Int range 0 .. 64 loop
11352 declare
11353 M : constant Uint := Uint_2 ** J;
11355 begin
11356 exit when M = Align;
11358 if M > Align then
11360 -- This error is suppressed in ASIS mode to allow for
11361 -- different ASIS back ends or ASIS-based tools to query the
11362 -- illegal clause.
11364 if not ASIS_Mode then
11365 Error_Msg_N ("alignment value must be power of 2", Expr);
11366 end if;
11368 return No_Uint;
11369 end if;
11370 end;
11371 end loop;
11373 return Align;
11374 end if;
11375 end Get_Alignment_Value;
11377 -------------------------------------
11378 -- Inherit_Aspects_At_Freeze_Point --
11379 -------------------------------------
11381 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11382 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11383 (Rep_Item : Node_Id) return Boolean;
11384 -- This routine checks if Rep_Item is either a pragma or an aspect
11385 -- specification node whose correponding pragma (if any) is present in
11386 -- the Rep Item chain of the entity it has been specified to.
11388 --------------------------------------------------
11389 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11390 --------------------------------------------------
11392 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11393 (Rep_Item : Node_Id) return Boolean
11395 begin
11396 return
11397 Nkind (Rep_Item) = N_Pragma
11398 or else Present_In_Rep_Item
11399 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
11400 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11402 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11404 begin
11405 -- A representation item is either subtype-specific (Size and Alignment
11406 -- clauses) or type-related (all others). Subtype-specific aspects may
11407 -- differ for different subtypes of the same type (RM 13.1.8).
11409 -- A derived type inherits each type-related representation aspect of
11410 -- its parent type that was directly specified before the declaration of
11411 -- the derived type (RM 13.1.15).
11413 -- A derived subtype inherits each subtype-specific representation
11414 -- aspect of its parent subtype that was directly specified before the
11415 -- declaration of the derived type (RM 13.1.15).
11417 -- The general processing involves inheriting a representation aspect
11418 -- from a parent type whenever the first rep item (aspect specification,
11419 -- attribute definition clause, pragma) corresponding to the given
11420 -- representation aspect in the rep item chain of Typ, if any, isn't
11421 -- directly specified to Typ but to one of its parents.
11423 -- ??? Note that, for now, just a limited number of representation
11424 -- aspects have been inherited here so far. Many of them are
11425 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11426 -- a non- exhaustive list of aspects that likely also need to
11427 -- be moved to this routine: Alignment, Component_Alignment,
11428 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
11429 -- Preelaborable_Initialization, RM_Size and Small.
11431 -- In addition, Convention must be propagated from base type to subtype,
11432 -- because the subtype may have been declared on an incomplete view.
11434 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11435 return;
11436 end if;
11438 -- Ada_05/Ada_2005
11440 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11441 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11442 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11443 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11444 then
11445 Set_Is_Ada_2005_Only (Typ);
11446 end if;
11448 -- Ada_12/Ada_2012
11450 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11451 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11452 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11453 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11454 then
11455 Set_Is_Ada_2012_Only (Typ);
11456 end if;
11458 -- Atomic/Shared
11460 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11461 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11462 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11463 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11464 then
11465 Set_Is_Atomic (Typ);
11466 Set_Is_Volatile (Typ);
11467 Set_Treat_As_Volatile (Typ);
11468 end if;
11470 -- Convention
11472 if Is_Record_Type (Typ)
11473 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11474 then
11475 Set_Convention (Typ, Convention (Base_Type (Typ)));
11476 end if;
11478 -- Default_Component_Value
11480 -- Verify that there is no rep_item declared for the type, and there
11481 -- is one coming from an ancestor.
11483 if Is_Array_Type (Typ)
11484 and then Is_Base_Type (Typ)
11485 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
11486 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11487 then
11488 Set_Default_Aspect_Component_Value (Typ,
11489 Default_Aspect_Component_Value
11490 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11491 end if;
11493 -- Default_Value
11495 if Is_Scalar_Type (Typ)
11496 and then Is_Base_Type (Typ)
11497 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
11498 and then Has_Rep_Item (Typ, Name_Default_Value)
11499 then
11500 Set_Has_Default_Aspect (Typ);
11501 Set_Default_Aspect_Value (Typ,
11502 Default_Aspect_Value
11503 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11504 end if;
11506 -- Discard_Names
11508 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11509 and then Has_Rep_Item (Typ, Name_Discard_Names)
11510 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11511 (Get_Rep_Item (Typ, Name_Discard_Names))
11512 then
11513 Set_Discard_Names (Typ);
11514 end if;
11516 -- Volatile
11518 if not Has_Rep_Item (Typ, Name_Volatile, False)
11519 and then Has_Rep_Item (Typ, Name_Volatile)
11520 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11521 (Get_Rep_Item (Typ, Name_Volatile))
11522 then
11523 Set_Is_Volatile (Typ);
11524 Set_Treat_As_Volatile (Typ);
11525 end if;
11527 -- Volatile_Full_Access
11529 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11530 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11531 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11532 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11533 then
11534 Set_Is_Volatile_Full_Access (Typ);
11535 Set_Is_Volatile (Typ);
11536 Set_Treat_As_Volatile (Typ);
11537 end if;
11539 -- Inheritance for derived types only
11541 if Is_Derived_Type (Typ) then
11542 declare
11543 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11544 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11546 begin
11547 -- Atomic_Components
11549 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11550 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11551 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11552 (Get_Rep_Item (Typ, Name_Atomic_Components))
11553 then
11554 Set_Has_Atomic_Components (Imp_Bas_Typ);
11555 end if;
11557 -- Volatile_Components
11559 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11560 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11561 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11562 (Get_Rep_Item (Typ, Name_Volatile_Components))
11563 then
11564 Set_Has_Volatile_Components (Imp_Bas_Typ);
11565 end if;
11567 -- Finalize_Storage_Only
11569 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11570 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11571 then
11572 Set_Finalize_Storage_Only (Bas_Typ);
11573 end if;
11575 -- Universal_Aliasing
11577 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11578 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11579 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11580 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11581 then
11582 Set_Universal_Aliasing (Imp_Bas_Typ);
11583 end if;
11585 -- Bit_Order
11587 if Is_Record_Type (Typ) then
11588 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11589 and then Has_Rep_Item (Typ, Name_Bit_Order)
11590 then
11591 Set_Reverse_Bit_Order (Bas_Typ,
11592 Reverse_Bit_Order (Entity (Name
11593 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11594 end if;
11595 end if;
11597 -- Scalar_Storage_Order
11599 -- Note: the aspect is specified on a first subtype, but recorded
11600 -- in a flag of the base type!
11602 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
11603 and then Typ = Bas_Typ
11604 then
11605 -- For a type extension, always inherit from parent; otherwise
11606 -- inherit if no default applies. Note: we do not check for
11607 -- an explicit rep item on the parent type when inheriting,
11608 -- because the parent SSO may itself have been set by default.
11610 if not Has_Rep_Item (First_Subtype (Typ),
11611 Name_Scalar_Storage_Order, False)
11612 and then (Is_Tagged_Type (Bas_Typ)
11613 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11614 or else
11615 SSO_Set_High_By_Default (Bas_Typ)))
11616 then
11617 Set_Reverse_Storage_Order (Bas_Typ,
11618 Reverse_Storage_Order
11619 (Implementation_Base_Type (Etype (Bas_Typ))));
11621 -- Clear default SSO indications, since the inherited aspect
11622 -- which was set explicitly overrides the default.
11624 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11625 Set_SSO_Set_High_By_Default (Bas_Typ, False);
11626 end if;
11627 end if;
11628 end;
11629 end if;
11630 end Inherit_Aspects_At_Freeze_Point;
11632 ----------------
11633 -- Initialize --
11634 ----------------
11636 procedure Initialize is
11637 begin
11638 Address_Clause_Checks.Init;
11639 Compile_Time_Warnings_Errors.Init;
11640 Unchecked_Conversions.Init;
11642 -- ??? Might be needed in the future for some non GCC back-ends
11643 -- if AAMP_On_Target then
11644 -- Independence_Checks.Init;
11645 -- end if;
11646 end Initialize;
11648 ---------------------------
11649 -- Install_Discriminants --
11650 ---------------------------
11652 procedure Install_Discriminants (E : Entity_Id) is
11653 Disc : Entity_Id;
11654 Prev : Entity_Id;
11655 begin
11656 Disc := First_Discriminant (E);
11657 while Present (Disc) loop
11658 Prev := Current_Entity (Disc);
11659 Set_Current_Entity (Disc);
11660 Set_Is_Immediately_Visible (Disc);
11661 Set_Homonym (Disc, Prev);
11662 Next_Discriminant (Disc);
11663 end loop;
11664 end Install_Discriminants;
11666 -------------------------
11667 -- Is_Operational_Item --
11668 -------------------------
11670 function Is_Operational_Item (N : Node_Id) return Boolean is
11671 begin
11672 if Nkind (N) /= N_Attribute_Definition_Clause then
11673 return False;
11675 else
11676 declare
11677 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
11678 begin
11680 -- List of operational items is given in AARM 13.1(8.mm/1).
11681 -- It is clearly incomplete, as it does not include iterator
11682 -- aspects, among others.
11684 return Id = Attribute_Constant_Indexing
11685 or else Id = Attribute_Default_Iterator
11686 or else Id = Attribute_Implicit_Dereference
11687 or else Id = Attribute_Input
11688 or else Id = Attribute_Iterator_Element
11689 or else Id = Attribute_Iterable
11690 or else Id = Attribute_Output
11691 or else Id = Attribute_Read
11692 or else Id = Attribute_Variable_Indexing
11693 or else Id = Attribute_Write
11694 or else Id = Attribute_External_Tag;
11695 end;
11696 end if;
11697 end Is_Operational_Item;
11699 -------------------------
11700 -- Is_Predicate_Static --
11701 -------------------------
11703 -- Note: the basic legality of the expression has already been checked, so
11704 -- we don't need to worry about cases or ranges on strings for example.
11706 function Is_Predicate_Static
11707 (Expr : Node_Id;
11708 Nam : Name_Id) return Boolean
11710 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
11711 -- Given a list of case expression alternatives, returns True if all
11712 -- the alternatives are static (have all static choices, and a static
11713 -- expression).
11715 function All_Static_Choices (L : List_Id) return Boolean;
11716 -- Returns true if all elements of the list are OK static choices
11717 -- as defined below for Is_Static_Choice. Used for case expression
11718 -- alternatives and for the right operand of a membership test. An
11719 -- others_choice is static if the corresponding expression is static.
11720 -- The staticness of the bounds is checked separately.
11722 function Is_Static_Choice (N : Node_Id) return Boolean;
11723 -- Returns True if N represents a static choice (static subtype, or
11724 -- static subtype indication, or static expression, or static range).
11726 -- Note that this is a bit more inclusive than we actually need
11727 -- (in particular membership tests do not allow the use of subtype
11728 -- indications). But that doesn't matter, we have already checked
11729 -- that the construct is legal to get this far.
11731 function Is_Type_Ref (N : Node_Id) return Boolean;
11732 pragma Inline (Is_Type_Ref);
11733 -- Returns True if N is a reference to the type for the predicate in the
11734 -- expression (i.e. if it is an identifier whose Chars field matches the
11735 -- Nam given in the call). N must not be parenthesized, if the type name
11736 -- appears in parens, this routine will return False.
11738 -- The routine also returns True for function calls generated during the
11739 -- expansion of comparison operators on strings, which are intended to
11740 -- be legal in static predicates, and are converted into calls to array
11741 -- comparison routines in the body of the corresponding predicate
11742 -- function.
11744 ----------------------------------
11745 -- All_Static_Case_Alternatives --
11746 ----------------------------------
11748 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11749 N : Node_Id;
11751 begin
11752 N := First (L);
11753 while Present (N) loop
11754 if not (All_Static_Choices (Discrete_Choices (N))
11755 and then Is_OK_Static_Expression (Expression (N)))
11756 then
11757 return False;
11758 end if;
11760 Next (N);
11761 end loop;
11763 return True;
11764 end All_Static_Case_Alternatives;
11766 ------------------------
11767 -- All_Static_Choices --
11768 ------------------------
11770 function All_Static_Choices (L : List_Id) return Boolean is
11771 N : Node_Id;
11773 begin
11774 N := First (L);
11775 while Present (N) loop
11776 if not Is_Static_Choice (N) then
11777 return False;
11778 end if;
11780 Next (N);
11781 end loop;
11783 return True;
11784 end All_Static_Choices;
11786 ----------------------
11787 -- Is_Static_Choice --
11788 ----------------------
11790 function Is_Static_Choice (N : Node_Id) return Boolean is
11791 begin
11792 return Nkind (N) = N_Others_Choice
11793 or else Is_OK_Static_Expression (N)
11794 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11795 and then Is_OK_Static_Subtype (Entity (N)))
11796 or else (Nkind (N) = N_Subtype_Indication
11797 and then Is_OK_Static_Subtype (Entity (N)))
11798 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11799 end Is_Static_Choice;
11801 -----------------
11802 -- Is_Type_Ref --
11803 -----------------
11805 function Is_Type_Ref (N : Node_Id) return Boolean is
11806 begin
11807 return (Nkind (N) = N_Identifier
11808 and then Chars (N) = Nam
11809 and then Paren_Count (N) = 0)
11810 or else Nkind (N) = N_Function_Call;
11811 end Is_Type_Ref;
11813 -- Start of processing for Is_Predicate_Static
11815 begin
11816 -- Predicate_Static means one of the following holds. Numbers are the
11817 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11819 -- 16: A static expression
11821 if Is_OK_Static_Expression (Expr) then
11822 return True;
11824 -- 17: A membership test whose simple_expression is the current
11825 -- instance, and whose membership_choice_list meets the requirements
11826 -- for a static membership test.
11828 elsif Nkind (Expr) in N_Membership_Test
11829 and then ((Present (Right_Opnd (Expr))
11830 and then Is_Static_Choice (Right_Opnd (Expr)))
11831 or else
11832 (Present (Alternatives (Expr))
11833 and then All_Static_Choices (Alternatives (Expr))))
11834 then
11835 return True;
11837 -- 18. A case_expression whose selecting_expression is the current
11838 -- instance, and whose dependent expressions are static expressions.
11840 elsif Nkind (Expr) = N_Case_Expression
11841 and then Is_Type_Ref (Expression (Expr))
11842 and then All_Static_Case_Alternatives (Alternatives (Expr))
11843 then
11844 return True;
11846 -- 19. A call to a predefined equality or ordering operator, where one
11847 -- operand is the current instance, and the other is a static
11848 -- expression.
11850 -- Note: the RM is clearly wrong here in not excluding string types.
11851 -- Without this exclusion, we would allow expressions like X > "ABC"
11852 -- to be considered as predicate-static, which is clearly not intended,
11853 -- since the idea is for predicate-static to be a subset of normal
11854 -- static expressions (and "DEF" > "ABC" is not a static expression).
11856 -- However, we do allow internally generated (not from source) equality
11857 -- and inequality operations to be valid on strings (this helps deal
11858 -- with cases where we transform A in "ABC" to A = "ABC).
11860 -- In fact, it appears that the intent of the ARG is to extend static
11861 -- predicates to strings, and that the extension should probably apply
11862 -- to static expressions themselves. The code below accepts comparison
11863 -- operators that apply to static strings.
11865 elsif Nkind (Expr) in N_Op_Compare
11866 and then ((Is_Type_Ref (Left_Opnd (Expr))
11867 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11868 or else
11869 (Is_Type_Ref (Right_Opnd (Expr))
11870 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11871 then
11872 return True;
11874 -- 20. A call to a predefined boolean logical operator, where each
11875 -- operand is predicate-static.
11877 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11878 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11879 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11880 or else
11881 (Nkind (Expr) = N_Op_Not
11882 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11883 then
11884 return True;
11886 -- 21. A short-circuit control form where both operands are
11887 -- predicate-static.
11889 elsif Nkind (Expr) in N_Short_Circuit
11890 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11891 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11892 then
11893 return True;
11895 -- 22. A parenthesized predicate-static expression. This does not
11896 -- require any special test, since we just ignore paren levels in
11897 -- all the cases above.
11899 -- One more test that is an implementation artifact caused by the fact
11900 -- that we are analyzing not the original expression, but the generated
11901 -- expression in the body of the predicate function. This can include
11902 -- references to inherited predicates, so that the expression we are
11903 -- processing looks like:
11905 -- xxPredicate (typ (Inns)) and then expression
11907 -- Where the call is to a Predicate function for an inherited predicate.
11908 -- We simply ignore such a call, which could be to either a dynamic or
11909 -- a static predicate. Note that if the parent predicate is dynamic then
11910 -- eventually this type will be marked as dynamic, but you are allowed
11911 -- to specify a static predicate for a subtype which is inheriting a
11912 -- dynamic predicate, so the static predicate validation here ignores
11913 -- the inherited predicate even if it is dynamic.
11914 -- In all cases, a static predicate can only apply to a scalar type.
11916 elsif Nkind (Expr) = N_Function_Call
11917 and then Is_Predicate_Function (Entity (Name (Expr)))
11918 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
11919 then
11920 return True;
11922 elsif Is_Entity_Name (Expr)
11923 and then Entity (Expr) = Standard_True
11924 then
11925 Error_Msg_N ("predicate is redundant (always True)?", Expr);
11926 return True;
11928 -- That's an exhaustive list of tests, all other cases are not
11929 -- predicate-static, so we return False.
11931 else
11932 return False;
11933 end if;
11934 end Is_Predicate_Static;
11936 ---------------------
11937 -- Kill_Rep_Clause --
11938 ---------------------
11940 procedure Kill_Rep_Clause (N : Node_Id) is
11941 begin
11942 pragma Assert (Ignore_Rep_Clauses);
11944 -- Note: we use Replace rather than Rewrite, because we don't want
11945 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11946 -- rep clause that is being replaced.
11948 Replace (N, Make_Null_Statement (Sloc (N)));
11950 -- The null statement must be marked as not coming from source. This is
11951 -- so that ASIS ignores it, and also the back end does not expect bogus
11952 -- "from source" null statements in weird places (e.g. in declarative
11953 -- regions where such null statements are not allowed).
11955 Set_Comes_From_Source (N, False);
11956 end Kill_Rep_Clause;
11958 ------------------
11959 -- Minimum_Size --
11960 ------------------
11962 function Minimum_Size
11963 (T : Entity_Id;
11964 Biased : Boolean := False) return Nat
11966 Lo : Uint := No_Uint;
11967 Hi : Uint := No_Uint;
11968 LoR : Ureal := No_Ureal;
11969 HiR : Ureal := No_Ureal;
11970 LoSet : Boolean := False;
11971 HiSet : Boolean := False;
11972 B : Uint;
11973 S : Nat;
11974 Ancest : Entity_Id;
11975 R_Typ : constant Entity_Id := Root_Type (T);
11977 begin
11978 -- If bad type, return 0
11980 if T = Any_Type then
11981 return 0;
11983 -- For generic types, just return zero. There cannot be any legitimate
11984 -- need to know such a size, but this routine may be called with a
11985 -- generic type as part of normal processing.
11987 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
11988 return 0;
11990 -- Access types (cannot have size smaller than System.Address)
11992 elsif Is_Access_Type (T) then
11993 return System_Address_Size;
11995 -- Floating-point types
11997 elsif Is_Floating_Point_Type (T) then
11998 return UI_To_Int (Esize (R_Typ));
12000 -- Discrete types
12002 elsif Is_Discrete_Type (T) then
12004 -- The following loop is looking for the nearest compile time known
12005 -- bounds following the ancestor subtype chain. The idea is to find
12006 -- the most restrictive known bounds information.
12008 Ancest := T;
12009 loop
12010 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12011 return 0;
12012 end if;
12014 if not LoSet then
12015 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
12016 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
12017 LoSet := True;
12018 exit when HiSet;
12019 end if;
12020 end if;
12022 if not HiSet then
12023 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
12024 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
12025 HiSet := True;
12026 exit when LoSet;
12027 end if;
12028 end if;
12030 Ancest := Ancestor_Subtype (Ancest);
12032 if No (Ancest) then
12033 Ancest := Base_Type (T);
12035 if Is_Generic_Type (Ancest) then
12036 return 0;
12037 end if;
12038 end if;
12039 end loop;
12041 -- Fixed-point types. We can't simply use Expr_Value to get the
12042 -- Corresponding_Integer_Value values of the bounds, since these do not
12043 -- get set till the type is frozen, and this routine can be called
12044 -- before the type is frozen. Similarly the test for bounds being static
12045 -- needs to include the case where we have unanalyzed real literals for
12046 -- the same reason.
12048 elsif Is_Fixed_Point_Type (T) then
12050 -- The following loop is looking for the nearest compile time known
12051 -- bounds following the ancestor subtype chain. The idea is to find
12052 -- the most restrictive known bounds information.
12054 Ancest := T;
12055 loop
12056 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12057 return 0;
12058 end if;
12060 -- Note: In the following two tests for LoSet and HiSet, it may
12061 -- seem redundant to test for N_Real_Literal here since normally
12062 -- one would assume that the test for the value being known at
12063 -- compile time includes this case. However, there is a glitch.
12064 -- If the real literal comes from folding a non-static expression,
12065 -- then we don't consider any non- static expression to be known
12066 -- at compile time if we are in configurable run time mode (needed
12067 -- in some cases to give a clearer definition of what is and what
12068 -- is not accepted). So the test is indeed needed. Without it, we
12069 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
12071 if not LoSet then
12072 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
12073 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
12074 then
12075 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
12076 LoSet := True;
12077 exit when HiSet;
12078 end if;
12079 end if;
12081 if not HiSet then
12082 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
12083 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
12084 then
12085 HiR := Expr_Value_R (Type_High_Bound (Ancest));
12086 HiSet := True;
12087 exit when LoSet;
12088 end if;
12089 end if;
12091 Ancest := Ancestor_Subtype (Ancest);
12093 if No (Ancest) then
12094 Ancest := Base_Type (T);
12096 if Is_Generic_Type (Ancest) then
12097 return 0;
12098 end if;
12099 end if;
12100 end loop;
12102 Lo := UR_To_Uint (LoR / Small_Value (T));
12103 Hi := UR_To_Uint (HiR / Small_Value (T));
12105 -- No other types allowed
12107 else
12108 raise Program_Error;
12109 end if;
12111 -- Fall through with Hi and Lo set. Deal with biased case
12113 if (Biased
12114 and then not Is_Fixed_Point_Type (T)
12115 and then not (Is_Enumeration_Type (T)
12116 and then Has_Non_Standard_Rep (T)))
12117 or else Has_Biased_Representation (T)
12118 then
12119 Hi := Hi - Lo;
12120 Lo := Uint_0;
12121 end if;
12123 -- Null range case, size is always zero. We only do this in the discrete
12124 -- type case, since that's the odd case that came up. Probably we should
12125 -- also do this in the fixed-point case, but doing so causes peculiar
12126 -- gigi failures, and it is not worth worrying about this incredibly
12127 -- marginal case (explicit null-range fixed-point type declarations)???
12129 if Lo > Hi and then Is_Discrete_Type (T) then
12130 S := 0;
12132 -- Signed case. Note that we consider types like range 1 .. -1 to be
12133 -- signed for the purpose of computing the size, since the bounds have
12134 -- to be accommodated in the base type.
12136 elsif Lo < 0 or else Hi < 0 then
12137 S := 1;
12138 B := Uint_1;
12140 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12141 -- Note that we accommodate the case where the bounds cross. This
12142 -- can happen either because of the way the bounds are declared
12143 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12145 while Lo < -B
12146 or else Hi < -B
12147 or else Lo >= B
12148 or else Hi >= B
12149 loop
12150 B := Uint_2 ** S;
12151 S := S + 1;
12152 end loop;
12154 -- Unsigned case
12156 else
12157 -- If both bounds are positive, make sure that both are represen-
12158 -- table in the case where the bounds are crossed. This can happen
12159 -- either because of the way the bounds are declared, or because of
12160 -- the algorithm in Freeze_Fixed_Point_Type.
12162 if Lo > Hi then
12163 Hi := Lo;
12164 end if;
12166 -- S = size, (can accommodate 0 .. (2**size - 1))
12168 S := 0;
12169 while Hi >= Uint_2 ** S loop
12170 S := S + 1;
12171 end loop;
12172 end if;
12174 return S;
12175 end Minimum_Size;
12177 ---------------------------
12178 -- New_Stream_Subprogram --
12179 ---------------------------
12181 procedure New_Stream_Subprogram
12182 (N : Node_Id;
12183 Ent : Entity_Id;
12184 Subp : Entity_Id;
12185 Nam : TSS_Name_Type)
12187 Loc : constant Source_Ptr := Sloc (N);
12188 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
12189 Subp_Id : Entity_Id;
12190 Subp_Decl : Node_Id;
12191 F : Entity_Id;
12192 Etyp : Entity_Id;
12194 Defer_Declaration : constant Boolean :=
12195 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12196 -- For a tagged type, there is a declaration for each stream attribute
12197 -- at the freeze point, and we must generate only a completion of this
12198 -- declaration. We do the same for private types, because the full view
12199 -- might be tagged. Otherwise we generate a declaration at the point of
12200 -- the attribute definition clause. If the attribute definition comes
12201 -- from an aspect specification the declaration is part of the freeze
12202 -- actions of the type.
12204 function Build_Spec return Node_Id;
12205 -- Used for declaration and renaming declaration, so that this is
12206 -- treated as a renaming_as_body.
12208 ----------------
12209 -- Build_Spec --
12210 ----------------
12212 function Build_Spec return Node_Id is
12213 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12214 Formals : List_Id;
12215 Spec : Node_Id;
12216 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
12218 begin
12219 Subp_Id := Make_Defining_Identifier (Loc, Sname);
12221 -- S : access Root_Stream_Type'Class
12223 Formals := New_List (
12224 Make_Parameter_Specification (Loc,
12225 Defining_Identifier =>
12226 Make_Defining_Identifier (Loc, Name_S),
12227 Parameter_Type =>
12228 Make_Access_Definition (Loc,
12229 Subtype_Mark =>
12230 New_Occurrence_Of (
12231 Designated_Type (Etype (F)), Loc))));
12233 if Nam = TSS_Stream_Input then
12234 Spec :=
12235 Make_Function_Specification (Loc,
12236 Defining_Unit_Name => Subp_Id,
12237 Parameter_Specifications => Formals,
12238 Result_Definition => T_Ref);
12239 else
12240 -- V : [out] T
12242 Append_To (Formals,
12243 Make_Parameter_Specification (Loc,
12244 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12245 Out_Present => Out_P,
12246 Parameter_Type => T_Ref));
12248 Spec :=
12249 Make_Procedure_Specification (Loc,
12250 Defining_Unit_Name => Subp_Id,
12251 Parameter_Specifications => Formals);
12252 end if;
12254 return Spec;
12255 end Build_Spec;
12257 -- Start of processing for New_Stream_Subprogram
12259 begin
12260 F := First_Formal (Subp);
12262 if Ekind (Subp) = E_Procedure then
12263 Etyp := Etype (Next_Formal (F));
12264 else
12265 Etyp := Etype (Subp);
12266 end if;
12268 -- Prepare subprogram declaration and insert it as an action on the
12269 -- clause node. The visibility for this entity is used to test for
12270 -- visibility of the attribute definition clause (in the sense of
12271 -- 8.3(23) as amended by AI-195).
12273 if not Defer_Declaration then
12274 Subp_Decl :=
12275 Make_Subprogram_Declaration (Loc,
12276 Specification => Build_Spec);
12278 -- For a tagged type, there is always a visible declaration for each
12279 -- stream TSS (it is a predefined primitive operation), and the
12280 -- completion of this declaration occurs at the freeze point, which is
12281 -- not always visible at places where the attribute definition clause is
12282 -- visible. So, we create a dummy entity here for the purpose of
12283 -- tracking the visibility of the attribute definition clause itself.
12285 else
12286 Subp_Id :=
12287 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
12288 Subp_Decl :=
12289 Make_Object_Declaration (Loc,
12290 Defining_Identifier => Subp_Id,
12291 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
12292 end if;
12294 if not Defer_Declaration
12295 and then From_Aspect_Specification (N)
12296 and then Has_Delayed_Freeze (Ent)
12297 then
12298 Append_Freeze_Action (Ent, Subp_Decl);
12300 else
12301 Insert_Action (N, Subp_Decl);
12302 Set_Entity (N, Subp_Id);
12303 end if;
12305 Subp_Decl :=
12306 Make_Subprogram_Renaming_Declaration (Loc,
12307 Specification => Build_Spec,
12308 Name => New_Occurrence_Of (Subp, Loc));
12310 if Defer_Declaration then
12311 Set_TSS (Base_Type (Ent), Subp_Id);
12313 else
12314 if From_Aspect_Specification (N) then
12315 Append_Freeze_Action (Ent, Subp_Decl);
12316 else
12317 Insert_Action (N, Subp_Decl);
12318 end if;
12320 Copy_TSS (Subp_Id, Base_Type (Ent));
12321 end if;
12322 end New_Stream_Subprogram;
12324 ------------------------------------------
12325 -- Push_Scope_And_Install_Discriminants --
12326 ------------------------------------------
12328 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12329 begin
12330 if Has_Discriminants (E) then
12331 Push_Scope (E);
12333 -- Make the discriminants visible for type declarations and protected
12334 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12336 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12337 Install_Discriminants (E);
12338 end if;
12339 end if;
12340 end Push_Scope_And_Install_Discriminants;
12342 -----------------------------------
12343 -- Register_Address_Clause_Check --
12344 -----------------------------------
12346 procedure Register_Address_Clause_Check
12347 (N : Node_Id;
12348 X : Entity_Id;
12349 A : Uint;
12350 Y : Entity_Id;
12351 Off : Boolean)
12353 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
12354 begin
12355 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
12356 end Register_Address_Clause_Check;
12358 ------------------------
12359 -- Rep_Item_Too_Early --
12360 ------------------------
12362 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
12363 begin
12364 -- Cannot apply non-operational rep items to generic types
12366 if Is_Operational_Item (N) then
12367 return False;
12369 elsif Is_Type (T)
12370 and then Is_Generic_Type (Root_Type (T))
12371 and then (Nkind (N) /= N_Pragma
12372 or else Get_Pragma_Id (N) /= Pragma_Convention)
12373 then
12374 Error_Msg_N ("representation item not allowed for generic type", N);
12375 return True;
12376 end if;
12378 -- Otherwise check for incomplete type
12380 if Is_Incomplete_Or_Private_Type (T)
12381 and then No (Underlying_Type (T))
12382 and then
12383 (Nkind (N) /= N_Pragma
12384 or else Get_Pragma_Id (N) /= Pragma_Import)
12385 then
12386 Error_Msg_N
12387 ("representation item must be after full type declaration", N);
12388 return True;
12390 -- If the type has incomplete components, a representation clause is
12391 -- illegal but stream attributes and Convention pragmas are correct.
12393 elsif Has_Private_Component (T) then
12394 if Nkind (N) = N_Pragma then
12395 return False;
12397 else
12398 Error_Msg_N
12399 ("representation item must appear after type is fully defined",
12401 return True;
12402 end if;
12403 else
12404 return False;
12405 end if;
12406 end Rep_Item_Too_Early;
12408 -----------------------
12409 -- Rep_Item_Too_Late --
12410 -----------------------
12412 function Rep_Item_Too_Late
12413 (T : Entity_Id;
12414 N : Node_Id;
12415 FOnly : Boolean := False) return Boolean
12417 S : Entity_Id;
12418 Parent_Type : Entity_Id;
12420 procedure No_Type_Rep_Item;
12421 -- Output message indicating that no type-related aspects can be
12422 -- specified due to some property of the parent type.
12424 procedure Too_Late;
12425 -- Output message for an aspect being specified too late
12427 -- Note that neither of the above errors is considered a serious one,
12428 -- since the effect is simply that we ignore the representation clause
12429 -- in these cases.
12430 -- Is this really true? In any case if we make this change we must
12431 -- document the requirement in the spec of Rep_Item_Too_Late that
12432 -- if True is returned, then the rep item must be completely ignored???
12434 ----------------------
12435 -- No_Type_Rep_Item --
12436 ----------------------
12438 procedure No_Type_Rep_Item is
12439 begin
12440 Error_Msg_N ("|type-related representation item not permitted!", N);
12441 end No_Type_Rep_Item;
12443 --------------
12444 -- Too_Late --
12445 --------------
12447 procedure Too_Late is
12448 begin
12449 -- Other compilers seem more relaxed about rep items appearing too
12450 -- late. Since analysis tools typically don't care about rep items
12451 -- anyway, no reason to be too strict about this.
12453 if not Relaxed_RM_Semantics then
12454 Error_Msg_N ("|representation item appears too late!", N);
12455 end if;
12456 end Too_Late;
12458 -- Start of processing for Rep_Item_Too_Late
12460 begin
12461 -- First make sure entity is not frozen (RM 13.1(9))
12463 if Is_Frozen (T)
12465 -- Exclude imported types, which may be frozen if they appear in a
12466 -- representation clause for a local type.
12468 and then not From_Limited_With (T)
12470 -- Exclude generated entities (not coming from source). The common
12471 -- case is when we generate a renaming which prematurely freezes the
12472 -- renamed internal entity, but we still want to be able to set copies
12473 -- of attribute values such as Size/Alignment.
12475 and then Comes_From_Source (T)
12476 then
12477 -- A self-referential aspect is illegal if it forces freezing the
12478 -- entity before the corresponding pragma has been analyzed.
12480 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12481 and then From_Aspect_Specification (N)
12482 then
12483 Error_Msg_NE
12484 ("aspect specification causes premature freezing of&", N, T);
12485 Set_Has_Delayed_Freeze (T, False);
12486 return True;
12487 end if;
12489 Too_Late;
12490 S := First_Subtype (T);
12492 if Present (Freeze_Node (S)) then
12493 if not Relaxed_RM_Semantics then
12494 Error_Msg_NE
12495 ("??no more representation items for }", Freeze_Node (S), S);
12496 end if;
12497 end if;
12499 return True;
12501 -- Check for case of untagged derived type whose parent either has
12502 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12503 -- this case we do not output a Too_Late message, since there is no
12504 -- earlier point where the rep item could be placed to make it legal.
12506 elsif Is_Type (T)
12507 and then not FOnly
12508 and then Is_Derived_Type (T)
12509 and then not Is_Tagged_Type (T)
12510 then
12511 Parent_Type := Etype (Base_Type (T));
12513 if Has_Primitive_Operations (Parent_Type) then
12514 No_Type_Rep_Item;
12516 if not Relaxed_RM_Semantics then
12517 Error_Msg_NE
12518 ("\parent type & has primitive operations!", N, Parent_Type);
12519 end if;
12521 return True;
12523 elsif Is_By_Reference_Type (Parent_Type) then
12524 No_Type_Rep_Item;
12526 if not Relaxed_RM_Semantics then
12527 Error_Msg_NE
12528 ("\parent type & is a by reference type!", N, Parent_Type);
12529 end if;
12531 return True;
12532 end if;
12533 end if;
12535 -- No error, but one more warning to consider. The RM (surprisingly)
12536 -- allows this pattern:
12538 -- type S is ...
12539 -- primitive operations for S
12540 -- type R is new S;
12541 -- rep clause for S
12543 -- Meaning that calls on the primitive operations of S for values of
12544 -- type R may require possibly expensive implicit conversion operations.
12545 -- This is not an error, but is worth a warning.
12547 if not Relaxed_RM_Semantics and then Is_Type (T) then
12548 declare
12549 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12551 begin
12552 if Present (DTL)
12553 and then Has_Primitive_Operations (Base_Type (T))
12555 -- For now, do not generate this warning for the case of aspect
12556 -- specification using Ada 2012 syntax, since we get wrong
12557 -- messages we do not understand. The whole business of derived
12558 -- types and rep items seems a bit confused when aspects are
12559 -- used, since the aspects are not evaluated till freeze time.
12561 and then not From_Aspect_Specification (N)
12562 then
12563 Error_Msg_Sloc := Sloc (DTL);
12564 Error_Msg_N
12565 ("representation item for& appears after derived type "
12566 & "declaration#??", N);
12567 Error_Msg_NE
12568 ("\may result in implicit conversions for primitive "
12569 & "operations of&??", N, T);
12570 Error_Msg_NE
12571 ("\to change representations when called with arguments "
12572 & "of type&??", N, DTL);
12573 end if;
12574 end;
12575 end if;
12577 -- No error, link item into head of chain of rep items for the entity,
12578 -- but avoid chaining if we have an overloadable entity, and the pragma
12579 -- is one that can apply to multiple overloaded entities.
12581 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
12582 declare
12583 Pname : constant Name_Id := Pragma_Name (N);
12584 begin
12585 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12586 Name_External, Name_Interface)
12587 then
12588 return False;
12589 end if;
12590 end;
12591 end if;
12593 Record_Rep_Item (T, N);
12594 return False;
12595 end Rep_Item_Too_Late;
12597 -------------------------------------
12598 -- Replace_Type_References_Generic --
12599 -------------------------------------
12601 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12602 TName : constant Name_Id := Chars (T);
12604 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
12605 -- Processes a single node in the traversal procedure below, checking
12606 -- if node N should be replaced, and if so, doing the replacement.
12608 function Visible_Component (Comp : Name_Id) return Entity_Id;
12609 -- Given an identifier in the expression, check whether there is a
12610 -- discriminant or component of the type that is directy visible, and
12611 -- rewrite it as the corresponding selected component of the formal of
12612 -- the subprogram. The entity is located by a sequential search, which
12613 -- seems acceptable given the typical size of component lists and check
12614 -- expressions. Possible optimization ???
12616 ----------------------
12617 -- Replace_Type_Ref --
12618 ----------------------
12620 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
12621 Loc : constant Source_Ptr := Sloc (N);
12623 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
12624 -- Add the proper prefix to a reference to a component of the type
12625 -- when it is not already a selected component.
12627 ----------------
12628 -- Add_Prefix --
12629 ----------------
12631 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12632 begin
12633 Rewrite (Ref,
12634 Make_Selected_Component (Loc,
12635 Prefix => New_Occurrence_Of (T, Loc),
12636 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12637 Replace_Type_Reference (Prefix (Ref));
12638 end Add_Prefix;
12640 -- Local variables
12642 Comp : Entity_Id;
12643 Pref : Node_Id;
12644 Scop : Entity_Id;
12646 -- Start of processing for Replace_Type_Ref
12648 begin
12649 if Nkind (N) = N_Identifier then
12651 -- If not the type name, check whether it is a reference to some
12652 -- other type, which must be frozen before the predicate function
12653 -- is analyzed, i.e. before the freeze node of the type to which
12654 -- the predicate applies.
12656 if Chars (N) /= TName then
12657 if Present (Current_Entity (N))
12658 and then Is_Type (Current_Entity (N))
12659 then
12660 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12661 end if;
12663 -- The components of the type are directly visible and can
12664 -- be referenced without a prefix.
12666 if Nkind (Parent (N)) = N_Selected_Component then
12667 null;
12669 -- In expression C (I), C may be a directly visible function
12670 -- or a visible component that has an array type. Disambiguate
12671 -- by examining the component type.
12673 elsif Nkind (Parent (N)) = N_Indexed_Component
12674 and then N = Prefix (Parent (N))
12675 then
12676 Comp := Visible_Component (Chars (N));
12678 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
12679 Add_Prefix (N, Comp);
12680 end if;
12682 else
12683 Comp := Visible_Component (Chars (N));
12685 if Present (Comp) then
12686 Add_Prefix (N, Comp);
12687 end if;
12688 end if;
12690 return Skip;
12692 -- Otherwise do the replacement if this is not a qualified
12693 -- reference to a homograph of the type itself. Note that the
12694 -- current instance could not appear in such a context, e.g.
12695 -- the prefix of a type conversion.
12697 else
12698 if Nkind (Parent (N)) /= N_Selected_Component
12699 or else N /= Selector_Name (Parent (N))
12700 then
12701 Replace_Type_Reference (N);
12702 end if;
12704 return Skip;
12705 end if;
12707 -- Case of selected component (which is what a qualification looks
12708 -- like in the unanalyzed tree, which is what we have.
12710 elsif Nkind (N) = N_Selected_Component then
12712 -- If selector name is not our type, keep going (we might still
12713 -- have an occurrence of the type in the prefix).
12715 if Nkind (Selector_Name (N)) /= N_Identifier
12716 or else Chars (Selector_Name (N)) /= TName
12717 then
12718 return OK;
12720 -- Selector name is our type, check qualification
12722 else
12723 -- Loop through scopes and prefixes, doing comparison
12725 Scop := Current_Scope;
12726 Pref := Prefix (N);
12727 loop
12728 -- Continue if no more scopes or scope with no name
12730 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
12731 return OK;
12732 end if;
12734 -- Do replace if prefix is an identifier matching the scope
12735 -- that we are currently looking at.
12737 if Nkind (Pref) = N_Identifier
12738 and then Chars (Pref) = Chars (Scop)
12739 then
12740 Replace_Type_Reference (N);
12741 return Skip;
12742 end if;
12744 -- Go check scope above us if prefix is itself of the form
12745 -- of a selected component, whose selector matches the scope
12746 -- we are currently looking at.
12748 if Nkind (Pref) = N_Selected_Component
12749 and then Nkind (Selector_Name (Pref)) = N_Identifier
12750 and then Chars (Selector_Name (Pref)) = Chars (Scop)
12751 then
12752 Scop := Scope (Scop);
12753 Pref := Prefix (Pref);
12755 -- For anything else, we don't have a match, so keep on
12756 -- going, there are still some weird cases where we may
12757 -- still have a replacement within the prefix.
12759 else
12760 return OK;
12761 end if;
12762 end loop;
12763 end if;
12765 -- Continue for any other node kind
12767 else
12768 return OK;
12769 end if;
12770 end Replace_Type_Ref;
12772 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
12774 -----------------------
12775 -- Visible_Component --
12776 -----------------------
12778 function Visible_Component (Comp : Name_Id) return Entity_Id is
12779 E : Entity_Id;
12781 begin
12782 -- Types with nameable components are records and discriminated
12783 -- private types.
12785 if Ekind (T) = E_Record_Type
12786 or else (Is_Private_Type (T) and then Has_Discriminants (T))
12787 then
12788 E := First_Entity (T);
12789 while Present (E) loop
12790 if Comes_From_Source (E) and then Chars (E) = Comp then
12791 return E;
12792 end if;
12794 Next_Entity (E);
12795 end loop;
12796 end if;
12798 -- Nothing by that name, or the type has no components
12800 return Empty;
12801 end Visible_Component;
12803 -- Start of processing for Replace_Type_References_Generic
12805 begin
12806 Replace_Type_Refs (N);
12807 end Replace_Type_References_Generic;
12809 --------------------------------
12810 -- Resolve_Aspect_Expressions --
12811 --------------------------------
12813 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12814 function Resolve_Name (N : Node_Id) return Traverse_Result;
12815 -- Verify that all identifiers in the expression, with the exception
12816 -- of references to the current entity, denote visible entities. This
12817 -- is done only to detect visibility errors, as the expression will be
12818 -- properly analyzed/expanded during analysis of the predicate function
12819 -- body. We omit quantified expressions from this test, given that they
12820 -- introduce a local identifier that would require proper expansion to
12821 -- handle properly.
12823 -- In ASIS_Mode we preserve the entity in the source because there is
12824 -- no subsequent expansion to decorate the tree.
12826 ------------------
12827 -- Resolve_Name --
12828 ------------------
12830 function Resolve_Name (N : Node_Id) return Traverse_Result is
12831 Dummy : Traverse_Result;
12833 begin
12834 if Nkind (N) = N_Selected_Component then
12835 if Nkind (Prefix (N)) = N_Identifier
12836 and then Chars (Prefix (N)) /= Chars (E)
12837 then
12838 Find_Selected_Component (N);
12839 end if;
12841 return Skip;
12843 -- Resolve identifiers that are not selectors in parameter
12844 -- associations (these are never resolved by visibility).
12846 elsif Nkind (N) = N_Identifier
12847 and then Chars (N) /= Chars (E)
12848 and then (Nkind (Parent (N)) /= N_Parameter_Association
12849 or else N /= Selector_Name (Parent (N)))
12850 then
12851 Find_Direct_Name (N);
12853 -- In ASIS mode we must analyze overloaded identifiers to ensure
12854 -- their correct decoration because expansion is disabled (and
12855 -- the expansion of freeze nodes takes care of resolving aspect
12856 -- expressions).
12858 if ASIS_Mode then
12859 if Is_Overloaded (N) then
12860 Analyze (Parent (N));
12861 end if;
12862 else
12863 Set_Entity (N, Empty);
12864 end if;
12866 -- The name is component association needs no resolution.
12868 elsif Nkind (N) = N_Component_Association then
12869 Dummy := Resolve_Name (Expression (N));
12870 return Skip;
12872 elsif Nkind (N) = N_Quantified_Expression then
12873 return Skip;
12874 end if;
12876 return OK;
12877 end Resolve_Name;
12879 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12881 -- Local variables
12883 ASN : Node_Id := First_Rep_Item (E);
12885 -- Start of processing for Resolve_Aspect_Expressions
12887 begin
12888 -- Need to make sure discriminants, if any, are directly visible
12890 Push_Scope_And_Install_Discriminants (E);
12892 while Present (ASN) loop
12893 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12894 declare
12895 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
12896 Expr : constant Node_Id := Expression (ASN);
12898 begin
12899 case A_Id is
12901 -- For now we only deal with aspects that do not generate
12902 -- subprograms, or that may mention current instances of
12903 -- types. These will require special handling (???TBD).
12905 when Aspect_Invariant
12906 | Aspect_Predicate
12907 | Aspect_Predicate_Failure
12909 null;
12911 when Aspect_Dynamic_Predicate
12912 | Aspect_Static_Predicate
12914 -- Build predicate function specification and preanalyze
12915 -- expression after type replacement. The function
12916 -- declaration must be analyzed in the scope of the
12917 -- type, but the expression must see components.
12919 if No (Predicate_Function (E)) then
12920 Uninstall_Discriminants_And_Pop_Scope (E);
12921 declare
12922 FDecl : constant Node_Id :=
12923 Build_Predicate_Function_Declaration (E);
12924 pragma Unreferenced (FDecl);
12926 begin
12927 Push_Scope_And_Install_Discriminants (E);
12928 Resolve_Aspect_Expression (Expr);
12929 end;
12930 end if;
12932 when Pre_Post_Aspects =>
12933 null;
12935 when Aspect_Iterable =>
12936 if Nkind (Expr) = N_Aggregate then
12937 declare
12938 Assoc : Node_Id;
12940 begin
12941 Assoc := First (Component_Associations (Expr));
12942 while Present (Assoc) loop
12943 Find_Direct_Name (Expression (Assoc));
12944 Next (Assoc);
12945 end loop;
12946 end;
12947 end if;
12949 -- The expression for Default_Value is a static expression
12950 -- of the type, but this expression does not freeze the
12951 -- type, so it can still appear in a representation clause
12952 -- before the actual freeze point.
12954 when Aspect_Default_Value =>
12955 Set_Must_Not_Freeze (Expr);
12956 Preanalyze_Spec_Expression (Expr, E);
12958 -- Ditto for Storage_Size. Any other aspects that carry
12959 -- expressions that should not freeze ??? This is only
12960 -- relevant to the misuse of deferred constants.
12962 when Aspect_Storage_Size =>
12963 Set_Must_Not_Freeze (Expr);
12964 Preanalyze_Spec_Expression (Expr, Any_Integer);
12966 when others =>
12967 if Present (Expr) then
12968 case Aspect_Argument (A_Id) is
12969 when Expression
12970 | Optional_Expression
12972 Analyze_And_Resolve (Expr);
12974 when Name
12975 | Optional_Name
12977 if Nkind (Expr) = N_Identifier then
12978 Find_Direct_Name (Expr);
12980 elsif Nkind (Expr) = N_Selected_Component then
12981 Find_Selected_Component (Expr);
12982 end if;
12983 end case;
12984 end if;
12985 end case;
12986 end;
12987 end if;
12989 ASN := Next_Rep_Item (ASN);
12990 end loop;
12992 Uninstall_Discriminants_And_Pop_Scope (E);
12993 end Resolve_Aspect_Expressions;
12995 -------------------------
12996 -- Same_Representation --
12997 -------------------------
12999 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
13000 T1 : constant Entity_Id := Underlying_Type (Typ1);
13001 T2 : constant Entity_Id := Underlying_Type (Typ2);
13003 begin
13004 -- A quick check, if base types are the same, then we definitely have
13005 -- the same representation, because the subtype specific representation
13006 -- attributes (Size and Alignment) do not affect representation from
13007 -- the point of view of this test.
13009 if Base_Type (T1) = Base_Type (T2) then
13010 return True;
13012 elsif Is_Private_Type (Base_Type (T2))
13013 and then Base_Type (T1) = Full_View (Base_Type (T2))
13014 then
13015 return True;
13016 end if;
13018 -- Tagged types always have the same representation, because it is not
13019 -- possible to specify different representations for common fields.
13021 if Is_Tagged_Type (T1) then
13022 return True;
13023 end if;
13025 -- Representations are definitely different if conventions differ
13027 if Convention (T1) /= Convention (T2) then
13028 return False;
13029 end if;
13031 -- Representations are different if component alignments or scalar
13032 -- storage orders differ.
13034 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
13035 and then
13036 (Is_Record_Type (T2) or else Is_Array_Type (T2))
13037 and then
13038 (Component_Alignment (T1) /= Component_Alignment (T2)
13039 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
13040 then
13041 return False;
13042 end if;
13044 -- For arrays, the only real issue is component size. If we know the
13045 -- component size for both arrays, and it is the same, then that's
13046 -- good enough to know we don't have a change of representation.
13048 if Is_Array_Type (T1) then
13049 if Known_Component_Size (T1)
13050 and then Known_Component_Size (T2)
13051 and then Component_Size (T1) = Component_Size (T2)
13052 then
13053 return True;
13054 end if;
13055 end if;
13057 -- For records, representations are different if reorderings differ
13059 if Is_Record_Type (T1)
13060 and then Is_Record_Type (T2)
13061 and then No_Reordering (T1) /= No_Reordering (T2)
13062 then
13063 return False;
13064 end if;
13066 -- Types definitely have same representation if neither has non-standard
13067 -- representation since default representations are always consistent.
13068 -- If only one has non-standard representation, and the other does not,
13069 -- then we consider that they do not have the same representation. They
13070 -- might, but there is no way of telling early enough.
13072 if Has_Non_Standard_Rep (T1) then
13073 if not Has_Non_Standard_Rep (T2) then
13074 return False;
13075 end if;
13076 else
13077 return not Has_Non_Standard_Rep (T2);
13078 end if;
13080 -- Here the two types both have non-standard representation, and we need
13081 -- to determine if they have the same non-standard representation.
13083 -- For arrays, we simply need to test if the component sizes are the
13084 -- same. Pragma Pack is reflected in modified component sizes, so this
13085 -- check also deals with pragma Pack.
13087 if Is_Array_Type (T1) then
13088 return Component_Size (T1) = Component_Size (T2);
13090 -- Case of record types
13092 elsif Is_Record_Type (T1) then
13094 -- Packed status must conform
13096 if Is_Packed (T1) /= Is_Packed (T2) then
13097 return False;
13099 -- Otherwise we must check components. Typ2 maybe a constrained
13100 -- subtype with fewer components, so we compare the components
13101 -- of the base types.
13103 else
13104 Record_Case : declare
13105 CD1, CD2 : Entity_Id;
13107 function Same_Rep return Boolean;
13108 -- CD1 and CD2 are either components or discriminants. This
13109 -- function tests whether they have the same representation.
13111 --------------
13112 -- Same_Rep --
13113 --------------
13115 function Same_Rep return Boolean is
13116 begin
13117 if No (Component_Clause (CD1)) then
13118 return No (Component_Clause (CD2));
13119 else
13120 -- Note: at this point, component clauses have been
13121 -- normalized to the default bit order, so that the
13122 -- comparison of Component_Bit_Offsets is meaningful.
13124 return
13125 Present (Component_Clause (CD2))
13126 and then
13127 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13128 and then
13129 Esize (CD1) = Esize (CD2);
13130 end if;
13131 end Same_Rep;
13133 -- Start of processing for Record_Case
13135 begin
13136 if Has_Discriminants (T1) then
13138 -- The number of discriminants may be different if the
13139 -- derived type has fewer (constrained by values). The
13140 -- invisible discriminants retain the representation of
13141 -- the original, so the discrepancy does not per se
13142 -- indicate a different representation.
13144 CD1 := First_Discriminant (T1);
13145 CD2 := First_Discriminant (T2);
13146 while Present (CD1) and then Present (CD2) loop
13147 if not Same_Rep then
13148 return False;
13149 else
13150 Next_Discriminant (CD1);
13151 Next_Discriminant (CD2);
13152 end if;
13153 end loop;
13154 end if;
13156 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13157 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
13158 while Present (CD1) loop
13159 if not Same_Rep then
13160 return False;
13161 else
13162 Next_Component (CD1);
13163 Next_Component (CD2);
13164 end if;
13165 end loop;
13167 return True;
13168 end Record_Case;
13169 end if;
13171 -- For enumeration types, we must check each literal to see if the
13172 -- representation is the same. Note that we do not permit enumeration
13173 -- representation clauses for Character and Wide_Character, so these
13174 -- cases were already dealt with.
13176 elsif Is_Enumeration_Type (T1) then
13177 Enumeration_Case : declare
13178 L1, L2 : Entity_Id;
13180 begin
13181 L1 := First_Literal (T1);
13182 L2 := First_Literal (T2);
13183 while Present (L1) loop
13184 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13185 return False;
13186 else
13187 Next_Literal (L1);
13188 Next_Literal (L2);
13189 end if;
13190 end loop;
13192 return True;
13193 end Enumeration_Case;
13195 -- Any other types have the same representation for these purposes
13197 else
13198 return True;
13199 end if;
13200 end Same_Representation;
13202 --------------------------------
13203 -- Resolve_Iterable_Operation --
13204 --------------------------------
13206 procedure Resolve_Iterable_Operation
13207 (N : Node_Id;
13208 Cursor : Entity_Id;
13209 Typ : Entity_Id;
13210 Nam : Name_Id)
13212 Ent : Entity_Id;
13213 F1 : Entity_Id;
13214 F2 : Entity_Id;
13216 begin
13217 if not Is_Overloaded (N) then
13218 if not Is_Entity_Name (N)
13219 or else Ekind (Entity (N)) /= E_Function
13220 or else Scope (Entity (N)) /= Scope (Typ)
13221 or else No (First_Formal (Entity (N)))
13222 or else Etype (First_Formal (Entity (N))) /= Typ
13223 then
13224 Error_Msg_N
13225 ("iterable primitive must be local function name whose first "
13226 & "formal is an iterable type", N);
13227 return;
13228 end if;
13230 Ent := Entity (N);
13231 F1 := First_Formal (Ent);
13233 if Nam = Name_First or else Nam = Name_Last then
13235 -- First or Last (Container) => Cursor
13237 if Etype (Ent) /= Cursor then
13238 Error_Msg_N ("primitive for First must yield a curosr", N);
13239 end if;
13241 elsif Nam = Name_Next then
13243 -- Next (Container, Cursor) => Cursor
13245 F2 := Next_Formal (F1);
13247 if Etype (F2) /= Cursor
13248 or else Etype (Ent) /= Cursor
13249 or else Present (Next_Formal (F2))
13250 then
13251 Error_Msg_N ("no match for Next iterable primitive", N);
13252 end if;
13254 elsif Nam = Name_Previous then
13256 -- Previous (Container, Cursor) => Cursor
13258 F2 := Next_Formal (F1);
13260 if Etype (F2) /= Cursor
13261 or else Etype (Ent) /= Cursor
13262 or else Present (Next_Formal (F2))
13263 then
13264 Error_Msg_N ("no match for Previous iterable primitive", N);
13265 end if;
13267 elsif Nam = Name_Has_Element then
13269 -- Has_Element (Container, Cursor) => Boolean
13271 F2 := Next_Formal (F1);
13273 if Etype (F2) /= Cursor
13274 or else Etype (Ent) /= Standard_Boolean
13275 or else Present (Next_Formal (F2))
13276 then
13277 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13278 end if;
13280 elsif Nam = Name_Element then
13281 F2 := Next_Formal (F1);
13283 if No (F2)
13284 or else Etype (F2) /= Cursor
13285 or else Present (Next_Formal (F2))
13286 then
13287 Error_Msg_N ("no match for Element iterable primitive", N);
13288 end if;
13290 else
13291 raise Program_Error;
13292 end if;
13294 else
13295 -- Overloaded case: find subprogram with proper signature. Caller
13296 -- will report error if no match is found.
13298 declare
13299 I : Interp_Index;
13300 It : Interp;
13302 begin
13303 Get_First_Interp (N, I, It);
13304 while Present (It.Typ) loop
13305 if Ekind (It.Nam) = E_Function
13306 and then Scope (It.Nam) = Scope (Typ)
13307 and then Etype (First_Formal (It.Nam)) = Typ
13308 then
13309 F1 := First_Formal (It.Nam);
13311 if Nam = Name_First then
13312 if Etype (It.Nam) = Cursor
13313 and then No (Next_Formal (F1))
13314 then
13315 Set_Entity (N, It.Nam);
13316 exit;
13317 end if;
13319 elsif Nam = Name_Next then
13320 F2 := Next_Formal (F1);
13322 if Present (F2)
13323 and then No (Next_Formal (F2))
13324 and then Etype (F2) = Cursor
13325 and then Etype (It.Nam) = Cursor
13326 then
13327 Set_Entity (N, It.Nam);
13328 exit;
13329 end if;
13331 elsif Nam = Name_Has_Element then
13332 F2 := Next_Formal (F1);
13334 if Present (F2)
13335 and then No (Next_Formal (F2))
13336 and then Etype (F2) = Cursor
13337 and then Etype (It.Nam) = Standard_Boolean
13338 then
13339 Set_Entity (N, It.Nam);
13340 F2 := Next_Formal (F1);
13341 exit;
13342 end if;
13344 elsif Nam = Name_Element then
13345 F2 := Next_Formal (F1);
13347 if Present (F2)
13348 and then No (Next_Formal (F2))
13349 and then Etype (F2) = Cursor
13350 then
13351 Set_Entity (N, It.Nam);
13352 exit;
13353 end if;
13354 end if;
13355 end if;
13357 Get_Next_Interp (I, It);
13358 end loop;
13359 end;
13360 end if;
13361 end Resolve_Iterable_Operation;
13363 ----------------
13364 -- Set_Biased --
13365 ----------------
13367 procedure Set_Biased
13368 (E : Entity_Id;
13369 N : Node_Id;
13370 Msg : String;
13371 Biased : Boolean := True)
13373 begin
13374 if Biased then
13375 Set_Has_Biased_Representation (E);
13377 if Warn_On_Biased_Representation then
13378 Error_Msg_NE
13379 ("?B?" & Msg & " forces biased representation for&", N, E);
13380 end if;
13381 end if;
13382 end Set_Biased;
13384 --------------------
13385 -- Set_Enum_Esize --
13386 --------------------
13388 procedure Set_Enum_Esize (T : Entity_Id) is
13389 Lo : Uint;
13390 Hi : Uint;
13391 Sz : Nat;
13393 begin
13394 Init_Alignment (T);
13396 -- Find the minimum standard size (8,16,32,64) that fits
13398 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13399 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13401 if Lo < 0 then
13402 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
13403 Sz := Standard_Character_Size; -- May be > 8 on some targets
13405 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13406 Sz := 16;
13408 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13409 Sz := 32;
13411 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13412 Sz := 64;
13413 end if;
13415 else
13416 if Hi < Uint_2**08 then
13417 Sz := Standard_Character_Size; -- May be > 8 on some targets
13419 elsif Hi < Uint_2**16 then
13420 Sz := 16;
13422 elsif Hi < Uint_2**32 then
13423 Sz := 32;
13425 else pragma Assert (Hi < Uint_2**63);
13426 Sz := 64;
13427 end if;
13428 end if;
13430 -- That minimum is the proper size unless we have a foreign convention
13431 -- and the size required is 32 or less, in which case we bump the size
13432 -- up to 32. This is required for C and C++ and seems reasonable for
13433 -- all other foreign conventions.
13435 if Has_Foreign_Convention (T)
13436 and then Esize (T) < Standard_Integer_Size
13438 -- Don't do this if Short_Enums on target
13440 and then not Target_Short_Enums
13441 then
13442 Init_Esize (T, Standard_Integer_Size);
13443 else
13444 Init_Esize (T, Sz);
13445 end if;
13446 end Set_Enum_Esize;
13448 -----------------------------
13449 -- Uninstall_Discriminants --
13450 -----------------------------
13452 procedure Uninstall_Discriminants (E : Entity_Id) is
13453 Disc : Entity_Id;
13454 Prev : Entity_Id;
13455 Outer : Entity_Id;
13457 begin
13458 -- Discriminants have been made visible for type declarations and
13459 -- protected type declarations, not for subtype declarations.
13461 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13462 Disc := First_Discriminant (E);
13463 while Present (Disc) loop
13464 if Disc /= Current_Entity (Disc) then
13465 Prev := Current_Entity (Disc);
13466 while Present (Prev)
13467 and then Present (Homonym (Prev))
13468 and then Homonym (Prev) /= Disc
13469 loop
13470 Prev := Homonym (Prev);
13471 end loop;
13472 else
13473 Prev := Empty;
13474 end if;
13476 Set_Is_Immediately_Visible (Disc, False);
13478 Outer := Homonym (Disc);
13479 while Present (Outer) and then Scope (Outer) = E loop
13480 Outer := Homonym (Outer);
13481 end loop;
13483 -- Reset homonym link of other entities, but do not modify link
13484 -- between entities in current scope, so that the back end can
13485 -- have a proper count of local overloadings.
13487 if No (Prev) then
13488 Set_Name_Entity_Id (Chars (Disc), Outer);
13490 elsif Scope (Prev) /= Scope (Disc) then
13491 Set_Homonym (Prev, Outer);
13492 end if;
13494 Next_Discriminant (Disc);
13495 end loop;
13496 end if;
13497 end Uninstall_Discriminants;
13499 -------------------------------------------
13500 -- Uninstall_Discriminants_And_Pop_Scope --
13501 -------------------------------------------
13503 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13504 begin
13505 if Has_Discriminants (E) then
13506 Uninstall_Discriminants (E);
13507 Pop_Scope;
13508 end if;
13509 end Uninstall_Discriminants_And_Pop_Scope;
13511 ------------------------------
13512 -- Validate_Address_Clauses --
13513 ------------------------------
13515 procedure Validate_Address_Clauses is
13516 function Offset_Value (Expr : Node_Id) return Uint;
13517 -- Given an Address attribute reference, return the value in bits of its
13518 -- offset from the first bit of the underlying entity, or 0 if it is not
13519 -- known at compile time.
13521 ------------------
13522 -- Offset_Value --
13523 ------------------
13525 function Offset_Value (Expr : Node_Id) return Uint is
13526 N : Node_Id := Prefix (Expr);
13527 Off : Uint;
13528 Val : Uint := Uint_0;
13530 begin
13531 -- Climb the prefix chain and compute the cumulative offset
13533 loop
13534 if Is_Entity_Name (N) then
13535 return Val;
13537 elsif Nkind (N) = N_Selected_Component then
13538 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13539 if Off /= No_Uint and then Off >= Uint_0 then
13540 Val := Val + Off;
13541 N := Prefix (N);
13542 else
13543 return Uint_0;
13544 end if;
13546 elsif Nkind (N) = N_Indexed_Component then
13547 Off := Indexed_Component_Bit_Offset (N);
13548 if Off /= No_Uint then
13549 Val := Val + Off;
13550 N := Prefix (N);
13551 else
13552 return Uint_0;
13553 end if;
13555 else
13556 return Uint_0;
13557 end if;
13558 end loop;
13559 end Offset_Value;
13561 -- Start of processing for Validate_Address_Clauses
13563 begin
13564 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13565 declare
13566 ACCR : Address_Clause_Check_Record
13567 renames Address_Clause_Checks.Table (J);
13569 Expr : Node_Id;
13571 X_Alignment : Uint;
13572 Y_Alignment : Uint := Uint_0;
13574 X_Size : Uint;
13575 Y_Size : Uint := Uint_0;
13577 X_Offs : Uint;
13579 begin
13580 -- Skip processing of this entry if warning already posted
13582 if not Address_Warning_Posted (ACCR.N) then
13583 Expr := Original_Node (Expression (ACCR.N));
13585 -- Get alignments, sizes and offset, if any
13587 X_Alignment := Alignment (ACCR.X);
13588 X_Size := Esize (ACCR.X);
13590 if Present (ACCR.Y) then
13591 Y_Alignment := Alignment (ACCR.Y);
13592 Y_Size := Esize (ACCR.Y);
13593 end if;
13595 if ACCR.Off
13596 and then Nkind (Expr) = N_Attribute_Reference
13597 and then Attribute_Name (Expr) = Name_Address
13598 then
13599 X_Offs := Offset_Value (Expr);
13600 else
13601 X_Offs := Uint_0;
13602 end if;
13604 -- Check for known value not multiple of alignment
13606 if No (ACCR.Y) then
13607 if not Alignment_Checks_Suppressed (ACCR)
13608 and then X_Alignment /= 0
13609 and then ACCR.A mod X_Alignment /= 0
13610 then
13611 Error_Msg_NE
13612 ("??specified address for& is inconsistent with "
13613 & "alignment", ACCR.N, ACCR.X);
13614 Error_Msg_N
13615 ("\??program execution may be erroneous (RM 13.3(27))",
13616 ACCR.N);
13618 Error_Msg_Uint_1 := X_Alignment;
13619 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13620 end if;
13622 -- Check for large object overlaying smaller one
13624 elsif Y_Size > Uint_0
13625 and then X_Size > Uint_0
13626 and then X_Offs + X_Size > Y_Size
13627 then
13628 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
13629 Error_Msg_N
13630 ("\??program execution may be erroneous", ACCR.N);
13632 Error_Msg_Uint_1 := X_Size;
13633 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13635 Error_Msg_Uint_1 := Y_Size;
13636 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
13638 if Y_Size >= X_Size then
13639 Error_Msg_Uint_1 := X_Offs;
13640 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
13641 end if;
13643 -- Check for inadequate alignment, both of the base object
13644 -- and of the offset, if any. We only do this check if the
13645 -- run-time Alignment_Check is active. No point in warning
13646 -- if this check has been suppressed (or is suppressed by
13647 -- default in the non-strict alignment machine case).
13649 -- Note: we do not check the alignment if we gave a size
13650 -- warning, since it would likely be redundant.
13652 elsif not Alignment_Checks_Suppressed (ACCR)
13653 and then Y_Alignment /= Uint_0
13654 and then
13655 (Y_Alignment < X_Alignment
13656 or else
13657 (ACCR.Off
13658 and then Nkind (Expr) = N_Attribute_Reference
13659 and then Attribute_Name (Expr) = Name_Address
13660 and then Has_Compatible_Alignment
13661 (ACCR.X, Prefix (Expr), True) /=
13662 Known_Compatible))
13663 then
13664 Error_Msg_NE
13665 ("??specified address for& may be inconsistent with "
13666 & "alignment", ACCR.N, ACCR.X);
13667 Error_Msg_N
13668 ("\??program execution may be erroneous (RM 13.3(27))",
13669 ACCR.N);
13671 Error_Msg_Uint_1 := X_Alignment;
13672 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13674 Error_Msg_Uint_1 := Y_Alignment;
13675 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13677 if Y_Alignment >= X_Alignment then
13678 Error_Msg_N
13679 ("\??but offset is not multiple of alignment", ACCR.N);
13680 end if;
13681 end if;
13682 end if;
13683 end;
13684 end loop;
13685 end Validate_Address_Clauses;
13687 -----------------------------------------
13688 -- Validate_Compile_Time_Warning_Error --
13689 -----------------------------------------
13691 procedure Validate_Compile_Time_Warning_Error (N : Node_Id) is
13692 begin
13693 Compile_Time_Warnings_Errors.Append
13694 (New_Val => CTWE_Entry'(Eloc => Sloc (N),
13695 Scope => Current_Scope,
13696 Prag => N));
13697 end Validate_Compile_Time_Warning_Error;
13699 ------------------------------------------
13700 -- Validate_Compile_Time_Warning_Errors --
13701 ------------------------------------------
13703 procedure Validate_Compile_Time_Warning_Errors is
13704 procedure Set_Scope (S : Entity_Id);
13705 -- Install all enclosing scopes of S along with S itself
13707 procedure Unset_Scope (S : Entity_Id);
13708 -- Uninstall all enclosing scopes of S along with S itself
13710 ---------------
13711 -- Set_Scope --
13712 ---------------
13714 procedure Set_Scope (S : Entity_Id) is
13715 begin
13716 if S /= Standard_Standard then
13717 Set_Scope (Scope (S));
13718 end if;
13720 Push_Scope (S);
13721 end Set_Scope;
13723 -----------------
13724 -- Unset_Scope --
13725 -----------------
13727 procedure Unset_Scope (S : Entity_Id) is
13728 begin
13729 if S /= Standard_Standard then
13730 Unset_Scope (Scope (S));
13731 end if;
13733 Pop_Scope;
13734 end Unset_Scope;
13736 -- Start of processing for Validate_Compile_Time_Warning_Errors
13738 begin
13739 Expander_Mode_Save_And_Set (False);
13740 In_Compile_Time_Warning_Or_Error := True;
13742 for N in Compile_Time_Warnings_Errors.First ..
13743 Compile_Time_Warnings_Errors.Last
13744 loop
13745 declare
13746 T : CTWE_Entry renames Compile_Time_Warnings_Errors.Table (N);
13748 begin
13749 Set_Scope (T.Scope);
13750 Reset_Analyzed_Flags (T.Prag);
13751 Process_Compile_Time_Warning_Or_Error (T.Prag, T.Eloc);
13752 Unset_Scope (T.Scope);
13753 end;
13754 end loop;
13756 In_Compile_Time_Warning_Or_Error := False;
13757 Expander_Mode_Restore;
13758 end Validate_Compile_Time_Warning_Errors;
13760 ---------------------------
13761 -- Validate_Independence --
13762 ---------------------------
13764 procedure Validate_Independence is
13765 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13766 N : Node_Id;
13767 E : Entity_Id;
13768 IC : Boolean;
13769 Comp : Entity_Id;
13770 Addr : Node_Id;
13771 P : Node_Id;
13773 procedure Check_Array_Type (Atyp : Entity_Id);
13774 -- Checks if the array type Atyp has independent components, and
13775 -- if not, outputs an appropriate set of error messages.
13777 procedure No_Independence;
13778 -- Output message that independence cannot be guaranteed
13780 function OK_Component (C : Entity_Id) return Boolean;
13781 -- Checks one component to see if it is independently accessible, and
13782 -- if so yields True, otherwise yields False if independent access
13783 -- cannot be guaranteed. This is a conservative routine, it only
13784 -- returns True if it knows for sure, it returns False if it knows
13785 -- there is a problem, or it cannot be sure there is no problem.
13787 procedure Reason_Bad_Component (C : Entity_Id);
13788 -- Outputs continuation message if a reason can be determined for
13789 -- the component C being bad.
13791 ----------------------
13792 -- Check_Array_Type --
13793 ----------------------
13795 procedure Check_Array_Type (Atyp : Entity_Id) is
13796 Ctyp : constant Entity_Id := Component_Type (Atyp);
13798 begin
13799 -- OK if no alignment clause, no pack, and no component size
13801 if not Has_Component_Size_Clause (Atyp)
13802 and then not Has_Alignment_Clause (Atyp)
13803 and then not Is_Packed (Atyp)
13804 then
13805 return;
13806 end if;
13808 -- Case of component size is greater than or equal to 64 and the
13809 -- alignment of the array is at least as large as the alignment
13810 -- of the component. We are definitely OK in this situation.
13812 if Known_Component_Size (Atyp)
13813 and then Component_Size (Atyp) >= 64
13814 and then Known_Alignment (Atyp)
13815 and then Known_Alignment (Ctyp)
13816 and then Alignment (Atyp) >= Alignment (Ctyp)
13817 then
13818 return;
13819 end if;
13821 -- Check actual component size
13823 if not Known_Component_Size (Atyp)
13824 or else not (Addressable (Component_Size (Atyp))
13825 and then Component_Size (Atyp) < 64)
13826 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13827 then
13828 No_Independence;
13830 -- Bad component size, check reason
13832 if Has_Component_Size_Clause (Atyp) then
13833 P := Get_Attribute_Definition_Clause
13834 (Atyp, Attribute_Component_Size);
13836 if Present (P) then
13837 Error_Msg_Sloc := Sloc (P);
13838 Error_Msg_N ("\because of Component_Size clause#", N);
13839 return;
13840 end if;
13841 end if;
13843 if Is_Packed (Atyp) then
13844 P := Get_Rep_Pragma (Atyp, Name_Pack);
13846 if Present (P) then
13847 Error_Msg_Sloc := Sloc (P);
13848 Error_Msg_N ("\because of pragma Pack#", N);
13849 return;
13850 end if;
13851 end if;
13853 -- No reason found, just return
13855 return;
13856 end if;
13858 -- Array type is OK independence-wise
13860 return;
13861 end Check_Array_Type;
13863 ---------------------
13864 -- No_Independence --
13865 ---------------------
13867 procedure No_Independence is
13868 begin
13869 if Pragma_Name (N) = Name_Independent then
13870 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
13871 else
13872 Error_Msg_NE
13873 ("independent components cannot be guaranteed for&", N, E);
13874 end if;
13875 end No_Independence;
13877 ------------------
13878 -- OK_Component --
13879 ------------------
13881 function OK_Component (C : Entity_Id) return Boolean is
13882 Rec : constant Entity_Id := Scope (C);
13883 Ctyp : constant Entity_Id := Etype (C);
13885 begin
13886 -- OK if no component clause, no Pack, and no alignment clause
13888 if No (Component_Clause (C))
13889 and then not Is_Packed (Rec)
13890 and then not Has_Alignment_Clause (Rec)
13891 then
13892 return True;
13893 end if;
13895 -- Here we look at the actual component layout. A component is
13896 -- addressable if its size is a multiple of the Esize of the
13897 -- component type, and its starting position in the record has
13898 -- appropriate alignment, and the record itself has appropriate
13899 -- alignment to guarantee the component alignment.
13901 -- Make sure sizes are static, always assume the worst for any
13902 -- cases where we cannot check static values.
13904 if not (Known_Static_Esize (C)
13905 and then
13906 Known_Static_Esize (Ctyp))
13907 then
13908 return False;
13909 end if;
13911 -- Size of component must be addressable or greater than 64 bits
13912 -- and a multiple of bytes.
13914 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
13915 return False;
13916 end if;
13918 -- Check size is proper multiple
13920 if Esize (C) mod Esize (Ctyp) /= 0 then
13921 return False;
13922 end if;
13924 -- Check alignment of component is OK
13926 if not Known_Component_Bit_Offset (C)
13927 or else Component_Bit_Offset (C) < Uint_0
13928 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13929 then
13930 return False;
13931 end if;
13933 -- Check alignment of record type is OK
13935 if not Known_Alignment (Rec)
13936 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13937 then
13938 return False;
13939 end if;
13941 -- All tests passed, component is addressable
13943 return True;
13944 end OK_Component;
13946 --------------------------
13947 -- Reason_Bad_Component --
13948 --------------------------
13950 procedure Reason_Bad_Component (C : Entity_Id) is
13951 Rec : constant Entity_Id := Scope (C);
13952 Ctyp : constant Entity_Id := Etype (C);
13954 begin
13955 -- If component clause present assume that's the problem
13957 if Present (Component_Clause (C)) then
13958 Error_Msg_Sloc := Sloc (Component_Clause (C));
13959 Error_Msg_N ("\because of Component_Clause#", N);
13960 return;
13961 end if;
13963 -- If pragma Pack clause present, assume that's the problem
13965 if Is_Packed (Rec) then
13966 P := Get_Rep_Pragma (Rec, Name_Pack);
13968 if Present (P) then
13969 Error_Msg_Sloc := Sloc (P);
13970 Error_Msg_N ("\because of pragma Pack#", N);
13971 return;
13972 end if;
13973 end if;
13975 -- See if record has bad alignment clause
13977 if Has_Alignment_Clause (Rec)
13978 and then Known_Alignment (Rec)
13979 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13980 then
13981 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13983 if Present (P) then
13984 Error_Msg_Sloc := Sloc (P);
13985 Error_Msg_N ("\because of Alignment clause#", N);
13986 end if;
13987 end if;
13989 -- Couldn't find a reason, so return without a message
13991 return;
13992 end Reason_Bad_Component;
13994 -- Start of processing for Validate_Independence
13996 begin
13997 for J in Independence_Checks.First .. Independence_Checks.Last loop
13998 N := Independence_Checks.Table (J).N;
13999 E := Independence_Checks.Table (J).E;
14000 IC := Pragma_Name (N) = Name_Independent_Components;
14002 -- Deal with component case
14004 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
14005 if not OK_Component (E) then
14006 No_Independence;
14007 Reason_Bad_Component (E);
14008 goto Continue;
14009 end if;
14010 end if;
14012 -- Deal with record with Independent_Components
14014 if IC and then Is_Record_Type (E) then
14015 Comp := First_Component_Or_Discriminant (E);
14016 while Present (Comp) loop
14017 if not OK_Component (Comp) then
14018 No_Independence;
14019 Reason_Bad_Component (Comp);
14020 goto Continue;
14021 end if;
14023 Next_Component_Or_Discriminant (Comp);
14024 end loop;
14025 end if;
14027 -- Deal with address clause case
14029 if Is_Object (E) then
14030 Addr := Address_Clause (E);
14032 if Present (Addr) then
14033 No_Independence;
14034 Error_Msg_Sloc := Sloc (Addr);
14035 Error_Msg_N ("\because of Address clause#", N);
14036 goto Continue;
14037 end if;
14038 end if;
14040 -- Deal with independent components for array type
14042 if IC and then Is_Array_Type (E) then
14043 Check_Array_Type (E);
14044 end if;
14046 -- Deal with independent components for array object
14048 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
14049 Check_Array_Type (Etype (E));
14050 end if;
14052 <<Continue>> null;
14053 end loop;
14054 end Validate_Independence;
14056 ------------------------------
14057 -- Validate_Iterable_Aspect --
14058 ------------------------------
14060 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
14061 Assoc : Node_Id;
14062 Expr : Node_Id;
14064 Prim : Node_Id;
14065 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
14067 First_Id : Entity_Id;
14068 Last_Id : Entity_Id;
14069 Next_Id : Entity_Id;
14070 Has_Element_Id : Entity_Id;
14071 Element_Id : Entity_Id;
14073 begin
14074 -- If previous error aspect is unusable
14076 if Cursor = Any_Type then
14077 return;
14078 end if;
14080 First_Id := Empty;
14081 Last_Id := Empty;
14082 Next_Id := Empty;
14083 Has_Element_Id := Empty;
14084 Element_Id := Empty;
14086 -- Each expression must resolve to a function with the proper signature
14088 Assoc := First (Component_Associations (Expression (ASN)));
14089 while Present (Assoc) loop
14090 Expr := Expression (Assoc);
14091 Analyze (Expr);
14093 Prim := First (Choices (Assoc));
14095 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
14096 Error_Msg_N ("illegal name in association", Prim);
14098 elsif Chars (Prim) = Name_First then
14099 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
14100 First_Id := Entity (Expr);
14102 elsif Chars (Prim) = Name_Last then
14103 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Last);
14104 Last_Id := Entity (Expr);
14106 elsif Chars (Prim) = Name_Previous then
14107 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Previous);
14108 Last_Id := Entity (Expr);
14110 elsif Chars (Prim) = Name_Next then
14111 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
14112 Next_Id := Entity (Expr);
14114 elsif Chars (Prim) = Name_Has_Element then
14115 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
14116 Has_Element_Id := Entity (Expr);
14118 elsif Chars (Prim) = Name_Element then
14119 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
14120 Element_Id := Entity (Expr);
14122 else
14123 Error_Msg_N ("invalid name for iterable function", Prim);
14124 end if;
14126 Next (Assoc);
14127 end loop;
14129 if No (First_Id) then
14130 Error_Msg_N ("match for First primitive not found", ASN);
14132 elsif No (Next_Id) then
14133 Error_Msg_N ("match for Next primitive not found", ASN);
14135 elsif No (Has_Element_Id) then
14136 Error_Msg_N ("match for Has_Element primitive not found", ASN);
14138 elsif No (Element_Id) or else No (Last_Id) then
14139 null; -- optional
14140 end if;
14141 end Validate_Iterable_Aspect;
14143 -----------------------------------
14144 -- Validate_Unchecked_Conversion --
14145 -----------------------------------
14147 procedure Validate_Unchecked_Conversion
14148 (N : Node_Id;
14149 Act_Unit : Entity_Id)
14151 Source : Entity_Id;
14152 Target : Entity_Id;
14153 Vnode : Node_Id;
14155 begin
14156 -- Obtain source and target types. Note that we call Ancestor_Subtype
14157 -- here because the processing for generic instantiation always makes
14158 -- subtypes, and we want the original frozen actual types.
14160 -- If we are dealing with private types, then do the check on their
14161 -- fully declared counterparts if the full declarations have been
14162 -- encountered (they don't have to be visible, but they must exist).
14164 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
14166 if Is_Private_Type (Source)
14167 and then Present (Underlying_Type (Source))
14168 then
14169 Source := Underlying_Type (Source);
14170 end if;
14172 Target := Ancestor_Subtype (Etype (Act_Unit));
14174 -- If either type is generic, the instantiation happens within a generic
14175 -- unit, and there is nothing to check. The proper check will happen
14176 -- when the enclosing generic is instantiated.
14178 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
14179 return;
14180 end if;
14182 if Is_Private_Type (Target)
14183 and then Present (Underlying_Type (Target))
14184 then
14185 Target := Underlying_Type (Target);
14186 end if;
14188 -- Source may be unconstrained array, but not target, except in relaxed
14189 -- semantics mode.
14191 if Is_Array_Type (Target)
14192 and then not Is_Constrained (Target)
14193 and then not Relaxed_RM_Semantics
14194 then
14195 Error_Msg_N
14196 ("unchecked conversion to unconstrained array not allowed", N);
14197 return;
14198 end if;
14200 -- Warn if conversion between two different convention pointers
14202 if Is_Access_Type (Target)
14203 and then Is_Access_Type (Source)
14204 and then Convention (Target) /= Convention (Source)
14205 and then Warn_On_Unchecked_Conversion
14206 then
14207 -- Give warnings for subprogram pointers only on most targets
14209 if Is_Access_Subprogram_Type (Target)
14210 or else Is_Access_Subprogram_Type (Source)
14211 then
14212 Error_Msg_N
14213 ("?z?conversion between pointers with different conventions!",
14215 end if;
14216 end if;
14218 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
14219 -- warning when compiling GNAT-related sources.
14221 if Warn_On_Unchecked_Conversion
14222 and then not In_Predefined_Unit (N)
14223 and then RTU_Loaded (Ada_Calendar)
14224 and then (Chars (Source) = Name_Time
14225 or else
14226 Chars (Target) = Name_Time)
14227 then
14228 -- If Ada.Calendar is loaded and the name of one of the operands is
14229 -- Time, there is a good chance that this is Ada.Calendar.Time.
14231 declare
14232 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
14233 begin
14234 pragma Assert (Present (Calendar_Time));
14236 if Source = Calendar_Time or else Target = Calendar_Time then
14237 Error_Msg_N
14238 ("?z?representation of 'Time values may change between "
14239 & "'G'N'A'T versions", N);
14240 end if;
14241 end;
14242 end if;
14244 -- Make entry in unchecked conversion table for later processing by
14245 -- Validate_Unchecked_Conversions, which will check sizes and alignments
14246 -- (using values set by the back end where possible). This is only done
14247 -- if the appropriate warning is active.
14249 if Warn_On_Unchecked_Conversion then
14250 Unchecked_Conversions.Append
14251 (New_Val => UC_Entry'(Eloc => Sloc (N),
14252 Source => Source,
14253 Target => Target,
14254 Act_Unit => Act_Unit));
14256 -- If both sizes are known statically now, then back-end annotation
14257 -- is not required to do a proper check but if either size is not
14258 -- known statically, then we need the annotation.
14260 if Known_Static_RM_Size (Source)
14261 and then
14262 Known_Static_RM_Size (Target)
14263 then
14264 null;
14265 else
14266 Back_Annotate_Rep_Info := True;
14267 end if;
14268 end if;
14270 -- If unchecked conversion to access type, and access type is declared
14271 -- in the same unit as the unchecked conversion, then set the flag
14272 -- No_Strict_Aliasing (no strict aliasing is implicit here)
14274 if Is_Access_Type (Target) and then
14275 In_Same_Source_Unit (Target, N)
14276 then
14277 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14278 end if;
14280 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14281 -- the back end needs to perform special validation checks.
14283 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14284 -- have full expansion and the back end is called ???
14286 Vnode :=
14287 Make_Validate_Unchecked_Conversion (Sloc (N));
14288 Set_Source_Type (Vnode, Source);
14289 Set_Target_Type (Vnode, Target);
14291 -- If the unchecked conversion node is in a list, just insert before it.
14292 -- If not we have some strange case, not worth bothering about.
14294 if Is_List_Member (N) then
14295 Insert_After (N, Vnode);
14296 end if;
14297 end Validate_Unchecked_Conversion;
14299 ------------------------------------
14300 -- Validate_Unchecked_Conversions --
14301 ------------------------------------
14303 procedure Validate_Unchecked_Conversions is
14304 begin
14305 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14306 declare
14307 T : UC_Entry renames Unchecked_Conversions.Table (N);
14309 Act_Unit : constant Entity_Id := T.Act_Unit;
14310 Eloc : constant Source_Ptr := T.Eloc;
14311 Source : constant Entity_Id := T.Source;
14312 Target : constant Entity_Id := T.Target;
14314 Source_Siz : Uint;
14315 Target_Siz : Uint;
14317 begin
14318 -- Skip if function marked as warnings off
14320 if Warnings_Off (Act_Unit) then
14321 goto Continue;
14322 end if;
14324 -- This validation check, which warns if we have unequal sizes for
14325 -- unchecked conversion, and thus potentially implementation
14326 -- dependent semantics, is one of the few occasions on which we
14327 -- use the official RM size instead of Esize. See description in
14328 -- Einfo "Handling of Type'Size Values" for details.
14330 if Serious_Errors_Detected = 0
14331 and then Known_Static_RM_Size (Source)
14332 and then Known_Static_RM_Size (Target)
14334 -- Don't do the check if warnings off for either type, note the
14335 -- deliberate use of OR here instead of OR ELSE to get the flag
14336 -- Warnings_Off_Used set for both types if appropriate.
14338 and then not (Has_Warnings_Off (Source)
14340 Has_Warnings_Off (Target))
14341 then
14342 Source_Siz := RM_Size (Source);
14343 Target_Siz := RM_Size (Target);
14345 if Source_Siz /= Target_Siz then
14346 Error_Msg
14347 ("?z?types for unchecked conversion have different sizes!",
14348 Eloc, Act_Unit);
14350 if All_Errors_Mode then
14351 Error_Msg_Name_1 := Chars (Source);
14352 Error_Msg_Uint_1 := Source_Siz;
14353 Error_Msg_Name_2 := Chars (Target);
14354 Error_Msg_Uint_2 := Target_Siz;
14355 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
14357 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14359 if Is_Discrete_Type (Source)
14360 and then
14361 Is_Discrete_Type (Target)
14362 then
14363 if Source_Siz > Target_Siz then
14364 Error_Msg
14365 ("\?z?^ high order bits of source will "
14366 & "be ignored!", Eloc);
14368 elsif Is_Unsigned_Type (Source) then
14369 Error_Msg
14370 ("\?z?source will be extended with ^ high order "
14371 & "zero bits!", Eloc);
14373 else
14374 Error_Msg
14375 ("\?z?source will be extended with ^ high order "
14376 & "sign bits!", Eloc);
14377 end if;
14379 elsif Source_Siz < Target_Siz then
14380 if Is_Discrete_Type (Target) then
14381 if Bytes_Big_Endian then
14382 Error_Msg
14383 ("\?z?target value will include ^ undefined "
14384 & "low order bits!", Eloc, Act_Unit);
14385 else
14386 Error_Msg
14387 ("\?z?target value will include ^ undefined "
14388 & "high order bits!", Eloc, Act_Unit);
14389 end if;
14391 else
14392 Error_Msg
14393 ("\?z?^ trailing bits of target value will be "
14394 & "undefined!", Eloc, Act_Unit);
14395 end if;
14397 else pragma Assert (Source_Siz > Target_Siz);
14398 if Is_Discrete_Type (Source) then
14399 if Bytes_Big_Endian then
14400 Error_Msg
14401 ("\?z?^ low order bits of source will be "
14402 & "ignored!", Eloc, Act_Unit);
14403 else
14404 Error_Msg
14405 ("\?z?^ high order bits of source will be "
14406 & "ignored!", Eloc, Act_Unit);
14407 end if;
14409 else
14410 Error_Msg
14411 ("\?z?^ trailing bits of source will be "
14412 & "ignored!", Eloc, Act_Unit);
14413 end if;
14414 end if;
14415 end if;
14416 end if;
14417 end if;
14419 -- If both types are access types, we need to check the alignment.
14420 -- If the alignment of both is specified, we can do it here.
14422 if Serious_Errors_Detected = 0
14423 and then Is_Access_Type (Source)
14424 and then Is_Access_Type (Target)
14425 and then Target_Strict_Alignment
14426 and then Present (Designated_Type (Source))
14427 and then Present (Designated_Type (Target))
14428 then
14429 declare
14430 D_Source : constant Entity_Id := Designated_Type (Source);
14431 D_Target : constant Entity_Id := Designated_Type (Target);
14433 begin
14434 if Known_Alignment (D_Source)
14435 and then
14436 Known_Alignment (D_Target)
14437 then
14438 declare
14439 Source_Align : constant Uint := Alignment (D_Source);
14440 Target_Align : constant Uint := Alignment (D_Target);
14442 begin
14443 if Source_Align < Target_Align
14444 and then not Is_Tagged_Type (D_Source)
14446 -- Suppress warning if warnings suppressed on either
14447 -- type or either designated type. Note the use of
14448 -- OR here instead of OR ELSE. That is intentional,
14449 -- we would like to set flag Warnings_Off_Used in
14450 -- all types for which warnings are suppressed.
14452 and then not (Has_Warnings_Off (D_Source)
14454 Has_Warnings_Off (D_Target)
14456 Has_Warnings_Off (Source)
14458 Has_Warnings_Off (Target))
14459 then
14460 Error_Msg_Uint_1 := Target_Align;
14461 Error_Msg_Uint_2 := Source_Align;
14462 Error_Msg_Node_1 := D_Target;
14463 Error_Msg_Node_2 := D_Source;
14464 Error_Msg
14465 ("?z?alignment of & (^) is stricter than "
14466 & "alignment of & (^)!", Eloc, Act_Unit);
14467 Error_Msg
14468 ("\?z?resulting access value may have invalid "
14469 & "alignment!", Eloc, Act_Unit);
14470 end if;
14471 end;
14472 end if;
14473 end;
14474 end if;
14475 end;
14477 <<Continue>>
14478 null;
14479 end loop;
14480 end Validate_Unchecked_Conversions;
14482 end Sem_Ch13;