]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch13.adb
Add targetm.insn_cost hook
[thirdparty/gcc.git] / gcc / ada / sem_ch13.adb
CommitLineData
d6f39728 1------------------------------------------------------------------------------
7189d17f 2-- --
d6f39728 3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 1 3 --
6-- --
7-- B o d y --
8-- --
74d7e7f5 9-- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
d6f39728 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- --
80df182a 13-- ware Foundation; either version 3, or (at your option) any later ver- --
d6f39728 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 --
80df182a 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. --
d6f39728 20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
e78e8c8e 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
d6f39728 23-- --
24------------------------------------------------------------------------------
25
ae888dbd 26with Aspects; use Aspects;
d6f39728 27with Atree; use Atree;
713c00d6 28with Checks; use Checks;
175a6969 29with Debug; use Debug;
d6f39728 30with Einfo; use Einfo;
d00681a7 31with Elists; use Elists;
d6f39728 32with Errout; use Errout;
76a6b7c7 33with Expander; use Expander;
d00681a7 34with Exp_Disp; use Exp_Disp;
d6f39728 35with Exp_Tss; use Exp_Tss;
36with Exp_Util; use Exp_Util;
37c6552c 37with Freeze; use Freeze;
f9e26ff7 38with Ghost; use Ghost;
d6f39728 39with Lib; use Lib;
83f8f0a6 40with Lib.Xref; use Lib.Xref;
15ebb600 41with Namet; use Namet;
d6f39728 42with Nlists; use Nlists;
43with Nmake; use Nmake;
44with Opt; use Opt;
42fb9d35 45with Par_SCO; use Par_SCO;
e0521a36 46with Restrict; use Restrict;
47with Rident; use Rident;
d6f39728 48with Rtsfind; use Rtsfind;
49with Sem; use Sem;
d60c9ff7 50with Sem_Aux; use Sem_Aux;
be9124d0 51with Sem_Case; use Sem_Case;
40ca69b9 52with Sem_Ch3; use Sem_Ch3;
490beba6 53with Sem_Ch6; use Sem_Ch6;
81083222 54with Sem_Ch7; use Sem_Ch7;
d6f39728 55with Sem_Ch8; use Sem_Ch8;
85696508 56with Sem_Dim; use Sem_Dim;
85377c9b 57with Sem_Disp; use Sem_Disp;
d6f39728 58with Sem_Eval; use Sem_Eval;
51ea9c94 59with Sem_Prag; use Sem_Prag;
d6f39728 60with Sem_Res; use Sem_Res;
61with Sem_Type; use Sem_Type;
62with Sem_Util; use Sem_Util;
44e4341e 63with Sem_Warn; use Sem_Warn;
738ec25b 64with Sinfo; use Sinfo;
1e3c4ae6 65with Sinput; use Sinput;
9dfe12ae 66with Snames; use Snames;
d6f39728 67with Stand; use Stand;
93735cb8 68with Targparm; use Targparm;
d6f39728 69with Ttypes; use Ttypes;
70with Tbuild; use Tbuild;
71with Urealp; use Urealp;
f42f24d7 72with Warnsw; use Warnsw;
d6f39728 73
bfa5a9d9 74with GNAT.Heap_Sort_G;
d6f39728 75
76package body Sem_Ch13 is
77
78 SSU : constant Pos := System_Storage_Unit;
79 -- Convenient short hand for commonly used constant
80
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
84
d95b8c89 85 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id);
2d9fff4f 86 -- Helper routine providing the original (pre-AI95-0133) behavior for
d95b8c89 87 -- Adjust_Record_For_Reverse_Bit_Order.
88
1d366b32 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.
d6f39728 94
eb66e842 95 procedure Build_Discrete_Static_Predicate
d97beb2f 96 (Typ : Entity_Id;
97 Expr : Node_Id;
98 Nam : Name_Id);
d7c2851f 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
6fb3c314 103 -- predicate expression have been replaced by identifier references to this
d7c2851f 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
5c6a5792 107 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
108 -- rewritten as a canonicalized membership operation.
d97beb2f 109
ee2b7923 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.
116
9c20237a 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.
123
eb66e842 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.
9c20237a 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.
eb66e842 135
6653b695 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.
139
d9f6a4ee 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
5f067114 145 -- we have to be sure the subtypes in question are frozen before checking).
d9f6a4ee 146
d6f39728 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.
151
152 function Is_Operational_Item (N : Node_Id) return Boolean;
1e3c4ae6 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.
d6f39728 157
3b23aaa0 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.
168 --
169 -- Only scalar types can have static predicates, so False is always
170 -- returned for non-scalar types.
171 --
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:
175 --
176 -- "ABC" < "DEF"
177 --
178 -- is not a static expression. So even though the clearly faulty RM wording
179 -- allows the following:
180 --
181 -- subtype S is String with Static_Predicate => S < "DEF"
182 --
183 -- We can't allow this, otherwise we have predicate-static applying to a
184 -- larger class than static expressions, which was never intended.
185
44e4341e 186 procedure New_Stream_Subprogram
d6f39728 187 (N : Node_Id;
188 Ent : Entity_Id;
189 Subp : Entity_Id;
9dfe12ae 190 Nam : TSS_Name_Type);
44e4341e 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
d1a2e31b 193 -- primitive operation, or in the untagged case make an appropriate TSS
44e4341e 194 -- entry. This is more properly an expansion activity than just semantics,
d1a2e31b 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
44e4341e 197 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
198 -- function to be generated.
9dfe12ae 199 --
f15731c4 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.
205
d10a1b95 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.
214
3061ffde 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.
222
b77e4501 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.
232
76a6b7c7 233 ---------------------------------------------------
234 -- Table for Validate_Compile_Time_Warning_Error --
235 ---------------------------------------------------
236
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.
244
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.
248
249 type CTWE_Entry is record
250 Eloc : Source_Ptr;
251 -- Source location used in warnings and error messages
252
253 Prag : Node_Id;
254 -- Pragma Compile_Time_Error or Compile_Time_Warning
255
256 Scope : Node_Id;
257 -- The scope which encloses the pragma
258 end record;
259
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");
267
d6f39728 268 ----------------------------------------------
269 -- Table for Validate_Unchecked_Conversions --
270 ----------------------------------------------
271
272 -- The following table collects unchecked conversions for validation.
95deda50 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.
d6f39728 278
95deda50 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.
299480f9 282
d6f39728 283 type UC_Entry is record
86d32751 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
d6f39728 288 end record;
289
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");
297
83f8f0a6 298 ----------------------------------------
299 -- Table for Validate_Address_Clauses --
300 ----------------------------------------
301
302 -- If an address clause has the form
303
304 -- for X'Address use Expr
305
514a5555 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
95deda50 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.
83f8f0a6 314
315 type Address_Clause_Check_Record is record
316 N : Node_Id;
317 -- The address clause
318
319 X : Entity_Id;
514a5555 320 -- The entity of the object subject to the address clause
321
322 A : Uint;
323 -- The value of the address in the first case
83f8f0a6 324
325 Y : Entity_Id;
514a5555 326 -- The entity of the object being overlaid in the second case
d6da7448 327
328 Off : Boolean;
514a5555 329 -- Whether the address is offset within Y in the second case
d10a1b95 330
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.
83f8f0a6 335 end record;
336
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");
344
d10a1b95 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.
349
350 ---------------------------------
351 -- Alignment_Checks_Suppressed --
352 ---------------------------------
353
354 function Alignment_Checks_Suppressed
355 (ACCR : Address_Clause_Check_Record) return Boolean
356 is
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;
364
59ac57b5 365 -----------------------------------------
366 -- Adjust_Record_For_Reverse_Bit_Order --
367 -----------------------------------------
368
369 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
d95b8c89 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
59ac57b5 374
7748ccb2 375 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
376
377 CC : Node_Id;
378 Comp : Node_Id;
d95b8c89 379 Num_CC : Natural;
6797073f 380
d95b8c89 381 begin
2d9fff4f 382 -- Processing here used to depend on Ada version: the behavior was
d95b8c89 383 -- changed by AI95-0133. However this AI is a Binding interpretation,
2d9fff4f 384 -- so we now implement it even in Ada 95 mode. The original behavior
d95b8c89 385 -- from unamended Ada 95 is still available for compatibility under
386 -- debugging switch -gnatd.
387
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;
392
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.
6797073f 397
7748ccb2 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.
6797073f 404
d95b8c89 405 Num_CC := 0;
406 Comp := First_Component_Or_Discriminant (R);
407 while Present (Comp) loop
408 CC := Component_Clause (Comp);
6797073f 409
d95b8c89 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));
6797073f 414
d95b8c89 415 begin
416 -- Case of component with last bit >= max machine scalar
6797073f 417
d95b8c89 418 if Lbit >= Max_Machine_Scalar_Size then
59ac57b5 419
7748ccb2 420 -- This is allowed only if first bit is zero, and last bit
421 -- + 1 is a multiple of storage unit size.
59ac57b5 422
d95b8c89 423 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
59ac57b5 424
d95b8c89 425 -- This is the case to give a warning if enabled
59ac57b5 426
d95b8c89 427 if Warn_On_Reverse_Bit_Order then
6797073f 428 Error_Msg_N
7a41db5b 429 ("info: multi-byte field specified with "
d95b8c89 430 & "non-standard Bit_Order?V?", CC);
31486bc0 431
6797073f 432 if Bytes_Big_Endian then
31486bc0 433 Error_Msg_N
7a41db5b 434 ("\bytes are not reversed "
d95b8c89 435 & "(component is big-endian)?V?", CC);
31486bc0 436 else
437 Error_Msg_N
7a41db5b 438 ("\bytes are not reversed "
d95b8c89 439 & "(component is little-endian)?V?", CC);
31486bc0 440 end if;
d95b8c89 441 end if;
59ac57b5 442
d95b8c89 443 -- Give error message for RM 13.5.1(10) violation
444
445 else
446 Error_Msg_FE
447 ("machine scalar rules not followed for&",
448 First_Bit (CC), Comp);
449
450 Error_Msg_Uint_1 := Lbit + 1;
451 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
452 Error_Msg_F
7748ccb2 453 ("\last bit + 1 (^) exceeds maximum machine scalar "
454 & "size (^)", First_Bit (CC));
d95b8c89 455
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 (^) "
7748ccb2 460 & "(RM 13.5.1(10))", First_Bit (CC));
59ac57b5 461
67278d60 462 else
d95b8c89 463 Error_Msg_Uint_1 := Fbit;
464 Error_Msg_F
465 ("\and first bit (^) is non-zero "
7748ccb2 466 & "(RM 13.4.1(10))", First_Bit (CC));
6797073f 467 end if;
d95b8c89 468 end if;
59ac57b5 469
7748ccb2 470 -- OK case of machine scalar related component clause. For now,
471 -- just count them.
59ac57b5 472
d95b8c89 473 else
474 Num_CC := Num_CC + 1;
475 end if;
476 end;
477 end if;
59ac57b5 478
d95b8c89 479 Next_Component_Or_Discriminant (Comp);
480 end loop;
59ac57b5 481
7748ccb2 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.
bfa5a9d9 485
d95b8c89 486 Sort_CC : declare
487 Comps : array (0 .. Num_CC) of Entity_Id;
7748ccb2 488 -- Array to collect component and discriminant entities. The data
489 -- starts at index 1, the 0'th entry is for the sort routine.
59ac57b5 490
d95b8c89 491 function CP_Lt (Op1, Op2 : Natural) return Boolean;
492 -- Compare routine for Sort
59ac57b5 493
d95b8c89 494 procedure CP_Move (From : Natural; To : Natural);
495 -- Move routine for Sort
59ac57b5 496
d95b8c89 497 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
59ac57b5 498
7748ccb2 499 MaxL : Uint;
500 -- Maximum last bit value of any component in this set
501
502 MSS : Uint;
503 -- Corresponding machine scalar size
504
d95b8c89 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).
59ac57b5 510
d95b8c89 511 -----------
512 -- CP_Lt --
513 -----------
6797073f 514
d95b8c89 515 function CP_Lt (Op1, Op2 : Natural) return Boolean is
516 begin
7748ccb2 517 return
518 Position (Component_Clause (Comps (Op1))) <
d95b8c89 519 Position (Component_Clause (Comps (Op2)));
520 end CP_Lt;
59ac57b5 521
d95b8c89 522 -------------
523 -- CP_Move --
524 -------------
59ac57b5 525
d95b8c89 526 procedure CP_Move (From : Natural; To : Natural) is
6797073f 527 begin
d95b8c89 528 Comps (To) := Comps (From);
529 end CP_Move;
530
531 -- Start of processing for Sort_CC
532
533 begin
534 -- Collect the machine scalar relevant component clauses
67278d60 535
d95b8c89 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);
67278d60 541
d95b8c89 542 begin
7748ccb2 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.
d95b8c89 549
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;
67278d60 559
d95b8c89 560 Next_Component_Or_Discriminant (Comp);
561 end loop;
67278d60 562
d95b8c89 563 -- Sort by ascending position number
564
565 Sorting.Sort (Num_CC);
566
7748ccb2 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.
d95b8c89 571
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;
67278d60 596
7748ccb2 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.
d95b8c89 600
7748ccb2 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.
d95b8c89 605
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;
67278d60 613
7748ccb2 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:
67278d60 617
d95b8c89 618 -- First_Bit .. Last_Bit Component_Bit_Offset
619 -- old new old new
67278d60 620
d95b8c89 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
b38e4131 625
d95b8c89 626 -- 1 .. 1 6 .. 6 1 6
627 -- 1 .. 4 3 .. 6 1 3
628 -- 4 .. 7 0 .. 3 4 0
67278d60 629
7748ccb2 630 -- The rule is that the first bit is obtained by subtracting the
631 -- old ending bit from machine scalar size - 1.
67278d60 632
d95b8c89 633 for C in Start .. Stop loop
634 declare
635 Comp : constant Entity_Id := Comps (C);
636 CC : constant Node_Id := Component_Clause (Comp);
59ac57b5 637
d95b8c89 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));
59ac57b5 642
d95b8c89 643 begin
644 if Warn_On_Reverse_Bit_Order then
645 Error_Msg_Uint_1 := MSS;
646 Error_Msg_N
7748ccb2 647 ("info: reverse bit order in machine scalar of "
648 & "length^?V?", First_Bit (CC));
d95b8c89 649 Error_Msg_Uint_1 := NFB;
650 Error_Msg_Uint_2 := NLB;
651
652 if Bytes_Big_Endian then
653 Error_Msg_NE
7748ccb2 654 ("\big-endian range for component & is ^ .. ^?V?",
655 First_Bit (CC), Comp);
6797073f 656 else
d95b8c89 657 Error_Msg_NE
7748ccb2 658 ("\little-endian range for component & is ^ .. ^?V?",
659 First_Bit (CC), Comp);
6797073f 660 end if;
d95b8c89 661 end if;
59ac57b5 662
d95b8c89 663 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
ed7f78d7 664 Set_Normalized_Position (Comp, Pos + NFB / SSU);
d95b8c89 665 Set_Normalized_First_Bit (Comp, NFB mod SSU);
666 end;
6797073f 667 end loop;
d95b8c89 668 end loop;
669 end Sort_CC;
670 end Adjust_Record_For_Reverse_Bit_Order;
59ac57b5 671
d95b8c89 672 ------------------------------------------------
673 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
674 ------------------------------------------------
59ac57b5 675
d95b8c89 676 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
d95b8c89 677 CC : Node_Id;
7748ccb2 678 Comp : Node_Id;
59ac57b5 679
d95b8c89 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.
59ac57b5 684
d95b8c89 685 Comp := First_Component_Or_Discriminant (R);
686 while Present (Comp) loop
687 CC := Component_Clause (Comp);
59ac57b5 688
d95b8c89 689 -- If component clause is present, then deal with the non-default
690 -- bit order case for Ada 95 mode.
59ac57b5 691
d95b8c89 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.
59ac57b5 695
d95b8c89 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);
59ac57b5 703
d95b8c89 704 Storage_Unit_Offset : constant Uint :=
705 CFB / System_Storage_Unit;
67278d60 706
d95b8c89 707 Start_Bit : constant Uint :=
708 CFB mod System_Storage_Unit;
67278d60 709
d95b8c89 710 begin
711 -- Cases where field goes over storage unit boundary
67278d60 712
d95b8c89 713 if Start_Bit + CSZ > System_Storage_Unit then
67278d60 714
d95b8c89 715 -- Allow multi-byte field but generate warning
67278d60 716
d95b8c89 717 if Start_Bit mod System_Storage_Unit = 0
718 and then CSZ mod System_Storage_Unit = 0
719 then
720 Error_Msg_N
7748ccb2 721 ("info: multi-byte field specified with non-standard "
722 & "Bit_Order?V?", CLC);
59ac57b5 723
d95b8c89 724 if Bytes_Big_Endian then
725 Error_Msg_N
726 ("\bytes are not reversed "
727 & "(component is big-endian)?V?", CLC);
6797073f 728 else
d95b8c89 729 Error_Msg_N
730 ("\bytes are not reversed "
731 & "(component is little-endian)?V?", CLC);
6797073f 732 end if;
67278d60 733
d95b8c89 734 -- Do not allow non-contiguous field
6797073f 735
d95b8c89 736 else
737 Error_Msg_N
7748ccb2 738 ("attempt to specify non-contiguous field not "
739 & "permitted", CLC);
d95b8c89 740 Error_Msg_N
7748ccb2 741 ("\caused by non-standard Bit_Order specified in "
742 & "legacy Ada 95 mode", CLC);
d95b8c89 743 end if;
6797073f 744
d95b8c89 745 -- Case where field fits in one storage unit
746
747 else
748 -- Give warning if suspicious component clause
749
750 if Intval (FB) >= System_Storage_Unit
751 and then Warn_On_Reverse_Bit_Order
752 then
753 Error_Msg_N
7748ccb2 754 ("info: Bit_Order clause does not affect byte "
755 & "ordering?V?", Pos);
d95b8c89 756 Error_Msg_Uint_1 :=
757 Intval (Pos) + Intval (FB) /
758 System_Storage_Unit;
759 Error_Msg_N
7748ccb2 760 ("info: position normalized to ^ before bit order "
761 & "interpreted?V?", Pos);
d95b8c89 762 end if;
67278d60 763
6797073f 764 -- Here is where we fix up the Component_Bit_Offset value
765 -- to account for the reverse bit order. Some examples of
d95b8c89 766 -- what needs to be done are:
67278d60 767
6797073f 768 -- First_Bit .. Last_Bit Component_Bit_Offset
769 -- old new old new
67278d60 770
6797073f 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
67278d60 775
6797073f 776 -- 1 .. 1 6 .. 6 1 6
777 -- 1 .. 4 3 .. 6 1 3
778 -- 4 .. 7 0 .. 3 4 0
67278d60 779
d95b8c89 780 -- The rule is that the first bit is is obtained by
781 -- subtracting the old ending bit from storage_unit - 1.
67278d60 782
7748ccb2 783 Set_Component_Bit_Offset (Comp,
784 (Storage_Unit_Offset * System_Storage_Unit) +
785 (System_Storage_Unit - 1) -
786 (Start_Bit + CSZ - 1));
b9e61b2a 787
ed7f78d7 788 Set_Normalized_Position (Comp,
789 Component_Bit_Offset (Comp) / System_Storage_Unit);
790
7748ccb2 791 Set_Normalized_First_Bit (Comp,
792 Component_Bit_Offset (Comp) mod System_Storage_Unit);
d95b8c89 793 end if;
794 end;
795 end if;
67278d60 796
d95b8c89 797 Next_Component_Or_Discriminant (Comp);
798 end loop;
799 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
59ac57b5 800
1d366b32 801 -------------------------------------
802 -- Alignment_Check_For_Size_Change --
803 -------------------------------------
d6f39728 804
1d366b32 805 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
d6f39728 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.
811
812 if Known_Alignment (Typ)
813 and then not Has_Alignment_Clause (Typ)
1d366b32 814 and then Size mod (Alignment (Typ) * SSU) /= 0
d6f39728 815 then
816 Init_Alignment (Typ);
817 end if;
1d366b32 818 end Alignment_Check_For_Size_Change;
d6f39728 819
06ef5f86 820 -------------------------------------
821 -- Analyze_Aspects_At_Freeze_Point --
822 -------------------------------------
823
824 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
06ef5f86 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.
828
37c6e44c 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:
833 --
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;
840 --
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
b21edad9 848 -- of all delayed aspects from the parent type. The derived type is E,
37c6e44c 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.
851
06ef5f86 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.
856
857 ----------------------------------
858 -- Analyze_Aspect_Default_Value --
859 ----------------------------------
860
861 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
ee2b7923 862 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
06ef5f86 863 Ent : constant Entity_Id := Entity (ASN);
864 Expr : constant Node_Id := Expression (ASN);
865 Id : constant Node_Id := Identifier (ASN);
866
867 begin
868 Error_Msg_Name_1 := Chars (Id);
869
870 if not Is_Type (Ent) then
871 Error_Msg_N ("aspect% can only apply to a type", Id);
872 return;
873
874 elsif not Is_First_Subtype (Ent) then
875 Error_Msg_N ("aspect% cannot apply to subtype", Id);
876 return;
877
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;
883
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;
888
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;
894
895 Set_Has_Default_Aspect (Base_Type (Ent));
896
897 if Is_Scalar_Type (Ent) then
9f36e3fb 898 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
06ef5f86 899 else
f3d70f08 900 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
06ef5f86 901 end if;
902 end Analyze_Aspect_Default_Value;
903
37c6e44c 904 ---------------------------------
905 -- Inherit_Delayed_Rep_Aspects --
906 ---------------------------------
907
908 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
ee2b7923 909 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
910 P : constant Entity_Id := Entity (ASN);
37c6e44c 911 -- Entithy for parent type
912
913 N : Node_Id;
914 -- Item from Rep_Item chain
915
916 A : Aspect_Id;
917
918 begin
919 -- Loop through delayed aspects for the parent type
920
921 N := ASN;
922 while Present (N) loop
923 if Nkind (N) = N_Aspect_Specification then
924 exit when Entity (N) /= P;
925
926 if Is_Delayed_Aspect (N) then
927 A := Get_Aspect_Id (Chars (Identifier (N)));
928
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.
935
936 case A is
937
938 -- Alignment
939
940 when Aspect_Alignment =>
941 if not Has_Alignment_Clause (E) then
942 Set_Alignment (E, Alignment (P));
943 end if;
944
945 -- Atomic
946
947 when Aspect_Atomic =>
948 if Is_Atomic (P) then
949 Set_Is_Atomic (E);
950 end if;
951
952 -- Atomic_Components
953
954 when Aspect_Atomic_Components =>
955 if Has_Atomic_Components (P) then
956 Set_Has_Atomic_Components (Base_Type (E));
957 end if;
958
959 -- Bit_Order
960
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;
969
970 -- Component_Size
971
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;
979
980 -- Machine_Radix
981
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;
988
989 -- Object_Size (also Size which also sets Object_Size)
990
99378362 991 when Aspect_Object_Size
992 | Aspect_Size
993 =>
37c6e44c 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;
1001
1002 -- Pack
1003
1004 when Aspect_Pack =>
1005 if not Is_Packed (E) then
1006 Set_Is_Packed (Base_Type (E));
1007
1008 if Is_Bit_Packed_Array (P) then
1009 Set_Is_Bit_Packed_Array (Base_Type (E));
a88a5773 1010 Set_Packed_Array_Impl_Type
1011 (E, Packed_Array_Impl_Type (P));
37c6e44c 1012 end if;
1013 end if;
1014
1015 -- Scalar_Storage_Order
1016
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
e163cac8 1020 (E, Attribute_Scalar_Storage_Order))
37c6e44c 1021 and then Reverse_Storage_Order (P)
1022 then
1023 Set_Reverse_Storage_Order (Base_Type (E));
b64082f2 1024
1025 -- Clear default SSO indications, since the aspect
1026 -- overrides the default.
1027
1028 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
1029 Set_SSO_Set_High_By_Default (Base_Type (E), False);
37c6e44c 1030 end if;
1031
1032 -- Small
1033
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;
1040
1041 -- Storage_Size
1042
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;
1050
1051 -- Value_Size
1052
1053 when Aspect_Value_Size =>
1054
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.
1058
1059 null;
1060
1061 -- Volatile
1062
1063 when Aspect_Volatile =>
1064 if Is_Volatile (P) then
1065 Set_Is_Volatile (E);
1066 end if;
1067
2fe893b9 1068 -- Volatile_Full_Access
1069
1070 when Aspect_Volatile_Full_Access =>
4bf2acc9 1071 if Is_Volatile_Full_Access (P) then
1072 Set_Is_Volatile_Full_Access (E);
2fe893b9 1073 end if;
1074
37c6e44c 1075 -- Volatile_Components
1076
1077 when Aspect_Volatile_Components =>
1078 if Has_Volatile_Components (P) then
1079 Set_Has_Volatile_Components (Base_Type (E));
1080 end if;
1081
1082 -- That should be all the Rep Aspects
1083
1084 when others =>
1085 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1086 null;
37c6e44c 1087 end case;
1088 end if;
1089 end if;
1090
1091 N := Next_Rep_Item (N);
1092 end loop;
1093 end Inherit_Delayed_Rep_Aspects;
1094
06ef5f86 1095 -------------------------------------
1096 -- Make_Pragma_From_Boolean_Aspect --
1097 -------------------------------------
1098
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);
1106
06ef5f86 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.
1111
1112 -----------------------------------------
1113 -- Check_False_Aspect_For_Derived_Type --
1114 -----------------------------------------
1115
1116 procedure Check_False_Aspect_For_Derived_Type is
1117 Par : Node_Id;
1118
1119 begin
1120 -- We are only checking derived types
1121
1122 if not Is_Derived_Type (E) then
1123 return;
1124 end if;
1125
1126 Par := Nearest_Ancestor (E);
1127
1128 case A_Id is
99378362 1129 when Aspect_Atomic
1130 | Aspect_Shared
1131 =>
06ef5f86 1132 if not Is_Atomic (Par) then
1133 return;
1134 end if;
1135
1136 when Aspect_Atomic_Components =>
1137 if not Has_Atomic_Components (Par) then
1138 return;
1139 end if;
1140
1141 when Aspect_Discard_Names =>
1142 if not Discard_Names (Par) then
1143 return;
1144 end if;
1145
1146 when Aspect_Pack =>
1147 if not Is_Packed (Par) then
1148 return;
1149 end if;
1150
1151 when Aspect_Unchecked_Union =>
1152 if not Is_Unchecked_Union (Par) then
1153 return;
1154 end if;
1155
1156 when Aspect_Volatile =>
1157 if not Is_Volatile (Par) then
1158 return;
1159 end if;
1160
1161 when Aspect_Volatile_Components =>
1162 if not Has_Volatile_Components (Par) then
1163 return;
1164 end if;
1165
2fe893b9 1166 when Aspect_Volatile_Full_Access =>
4bf2acc9 1167 if not Is_Volatile_Full_Access (Par) then
2fe893b9 1168 return;
1169 end if;
1170
06ef5f86 1171 when others =>
1172 return;
1173 end case;
1174
1175 -- Fall through means we are canceling an inherited aspect
1176
1177 Error_Msg_Name_1 := A_Name;
37c6e44c 1178 Error_Msg_NE
1179 ("derived type& inherits aspect%, cannot cancel", Expr, E);
06ef5f86 1180 end Check_False_Aspect_For_Derived_Type;
1181
ee2b7923 1182 -- Local variables
1183
1184 Prag : Node_Id;
1185
06ef5f86 1186 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1187
1188 begin
37c6e44c 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.
1192
06ef5f86 1193 if Is_False (Static_Boolean (Expr)) then
1194 Check_False_Aspect_For_Derived_Type;
1195
1196 else
1197 Prag :=
1198 Make_Pragma (Loc,
ee2b7923 1199 Pragma_Identifier =>
1200 Make_Identifier (Sloc (Ident), Chars (Ident)),
06ef5f86 1201 Pragma_Argument_Associations => New_List (
57cd943b 1202 Make_Pragma_Argument_Association (Sloc (Ident),
ee2b7923 1203 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
06ef5f86 1204
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;
06ef5f86 1211 end Make_Pragma_From_Boolean_Aspect;
1212
ee2b7923 1213 -- Local variables
1214
1215 A_Id : Aspect_Id;
1216 ASN : Node_Id;
1217 Ritem : Node_Id;
1218
06ef5f86 1219 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1220
1221 begin
5e67c1f8 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.
06ef5f86 1227
ace3389d 1228 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
3051730b 1229 if Is_Type (E) and then From_Nested_Package (E) then
81083222 1230 declare
1231 Pack : constant Entity_Id := Scope (E);
1232
1233 begin
1234 Push_Scope (Pack);
1235 Install_Visible_Declarations (Pack);
1236 Install_Private_Declarations (Pack);
1237 Analyze_Aspects_At_Freeze_Point (E);
1238
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;
1244
1245 End_Package_Scope (Pack);
3051730b 1246 return;
81083222 1247 end;
1248
5e67c1f8 1249 -- Aspects from other entities in different contexts are analyzed
1250 -- elsewhere.
81083222 1251
5e67c1f8 1252 else
81083222 1253 return;
1254 end if;
06ef5f86 1255 end if;
1256
1257 -- Look for aspect specification entries for this entity
1258
1259 ASN := First_Rep_Item (E);
06ef5f86 1260 while Present (ASN) loop
37c6e44c 1261 if Nkind (ASN) = N_Aspect_Specification then
1262 exit when Entity (ASN) /= E;
06ef5f86 1263
37c6e44c 1264 if Is_Delayed_Aspect (ASN) then
1265 A_Id := Get_Aspect_Id (ASN);
1266
1267 case A_Id is
e4c87fa5 1268
37c6e44c 1269 -- For aspects whose expression is an optional Boolean, make
7d6fb253 1270 -- the corresponding pragma at the freeze point.
06ef5f86 1271
99378362 1272 when Boolean_Aspects
1273 | Library_Unit_Aspects
1274 =>
ee2b7923 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.
1284
1285 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1286 null;
1287
1288 -- Otherwise create a corresponding pragma
1289
1290 else
1291 Make_Pragma_From_Boolean_Aspect (ASN);
1292 end if;
06ef5f86 1293
37c6e44c 1294 -- Special handling for aspects that don't correspond to
1295 -- pragmas/attributes.
06ef5f86 1296
99378362 1297 when Aspect_Default_Value
1298 | Aspect_Default_Component_Value
1299 =>
81c2bc19 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.
1304
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;
06ef5f86 1313
37c6e44c 1314 -- Ditto for iterator aspects, because the corresponding
1315 -- attributes may not have been analyzed yet.
af9fed8f 1316
99378362 1317 when Aspect_Constant_Indexing
1318 | Aspect_Default_Iterator
1319 | Aspect_Iterator_Element
1320 | Aspect_Variable_Indexing
1321 =>
7d6fb253 1322 Analyze (Expression (ASN));
af9fed8f 1323
7d6fb253 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;
b3f8228a 1329
7d6fb253 1330 when Aspect_Iterable =>
1331 Validate_Iterable_Aspect (E, ASN);
1332
1333 when others =>
1334 null;
37c6e44c 1335 end case;
06ef5f86 1336
37c6e44c 1337 Ritem := Aspect_Rep_Item (ASN);
06ef5f86 1338
37c6e44c 1339 if Present (Ritem) then
1340 Analyze (Ritem);
1341 end if;
06ef5f86 1342 end if;
1343 end if;
1344
1345 Next_Rep_Item (ASN);
1346 end loop;
37c6e44c 1347
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.
1352
1353 if May_Inherit_Delayed_Rep_Aspects (E) then
1354 Inherit_Delayed_Rep_Aspects (ASN);
1355 end if;
06ef5f86 1356 end Analyze_Aspects_At_Freeze_Point;
1357
ae888dbd 1358 -----------------------------------
1359 -- Analyze_Aspect_Specifications --
1360 -----------------------------------
1361
21ea3a4f 1362 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
e2bf777d 1363 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
6c5793cd 1364 -- Establish linkages between an aspect and its corresponding pragma
5ddd846b 1365
5655be8a 1366 procedure Insert_Pragma
1367 (Prag : Node_Id;
1368 Is_Instance : Boolean := False);
2f06c88a 1369 -- Subsidiary to the analysis of aspects
1370 -- Abstract_State
2f06c88a 1371 -- Attach_Handler
1372 -- Contract_Cases
1373 -- Depends
5655be8a 1374 -- Ghost
2f06c88a 1375 -- Global
5655be8a 1376 -- Initial_Condition
1377 -- Initializes
2f06c88a 1378 -- Post
1379 -- Pre
1380 -- Refined_Depends
1381 -- Refined_Global
5655be8a 1382 -- Refined_State
2f06c88a 1383 -- SPARK_Mode
1384 -- Warnings
e2bf777d 1385 -- Insert pragma Prag such that it mimics the placement of a source
5655be8a 1386 -- pragma of the same kind. Flag Is_Generic should be set when the
1387 -- context denotes a generic instance.
e2bf777d 1388
1389 --------------
1390 -- Decorate --
1391 --------------
1392
1393 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
5ddd846b 1394 begin
6c5793cd 1395 Set_Aspect_Rep_Item (Asp, Prag);
5ddd846b 1396 Set_Corresponding_Aspect (Prag, Asp);
1397 Set_From_Aspect_Specification (Prag);
5ddd846b 1398 Set_Parent (Prag, Asp);
e2bf777d 1399 end Decorate;
f0813d71 1400
e2bf777d 1401 -------------------
1402 -- Insert_Pragma --
1403 -------------------
c1006d6d 1404
5655be8a 1405 procedure Insert_Pragma
1406 (Prag : Node_Id;
1407 Is_Instance : Boolean := False)
1408 is
3ff5e35d 1409 Aux : Node_Id;
1410 Decl : Node_Id;
1411 Decls : List_Id;
1412 Def : Node_Id;
1413 Inserted : Boolean := False;
c1006d6d 1414
1415 begin
3ff5e35d 1416 -- When the aspect appears on an entry, package, protected unit,
1417 -- subprogram, or task unit body, insert the generated pragma at the
1418 -- top of the body declarations to emulate the behavior of a source
1419 -- pragma.
2f06c88a 1420
1421 -- package body Pack with Aspect is
1422
1423 -- package body Pack is
1424 -- pragma Prag;
1425
3ff5e35d 1426 if Nkind_In (N, N_Entry_Body,
1427 N_Package_Body,
2f06c88a 1428 N_Protected_Body,
1429 N_Subprogram_Body,
1430 N_Task_Body)
1431 then
1432 Decls := Declarations (N);
1433
1434 if No (Decls) then
1435 Decls := New_List;
1436 Set_Declarations (N, Decls);
1437 end if;
e2bf777d 1438
3ff5e35d 1439 Prepend_To (Decls, Prag);
2f06c88a 1440
1441 -- When the aspect is associated with a [generic] package declaration
1442 -- insert the generated pragma at the top of the visible declarations
1443 -- to emulate the behavior of a source pragma.
1444
1445 -- package Pack with Aspect is
1446
1447 -- package Pack is
1448 -- pragma Prag;
1449
1450 elsif Nkind_In (N, N_Generic_Package_Declaration,
1451 N_Package_Declaration)
1452 then
1453 Decls := Visible_Declarations (Specification (N));
1454
1455 if No (Decls) then
1456 Decls := New_List;
1457 Set_Visible_Declarations (Specification (N), Decls);
1458 end if;
1459
5655be8a 1460 -- The visible declarations of a generic instance have the
1461 -- following structure:
1462
1463 -- <renamings of generic formals>
1464 -- <renamings of internally-generated spec and body>
1465 -- <first source declaration>
1466
1467 -- Insert the pragma before the first source declaration by
3ff5e35d 1468 -- skipping the instance "header" to ensure proper visibility of
1469 -- all formals.
5655be8a 1470
1471 if Is_Instance then
1472 Decl := First (Decls);
3ff5e35d 1473 while Present (Decl) loop
1474 if Comes_From_Source (Decl) then
1475 Insert_Before (Decl, Prag);
1476 Inserted := True;
1477 exit;
1478 else
1479 Next (Decl);
1480 end if;
5655be8a 1481 end loop;
1482
3ff5e35d 1483 -- The pragma is placed after the instance "header"
5655be8a 1484
3ff5e35d 1485 if not Inserted then
5655be8a 1486 Append_To (Decls, Prag);
1487 end if;
1488
1489 -- Otherwise this is not a generic instance
1490
1491 else
1492 Prepend_To (Decls, Prag);
1493 end if;
2f06c88a 1494
1495 -- When the aspect is associated with a protected unit declaration,
1496 -- insert the generated pragma at the top of the visible declarations
1497 -- the emulate the behavior of a source pragma.
1498
1499 -- protected [type] Prot with Aspect is
1500
1501 -- protected [type] Prot is
1502 -- pragma Prag;
1503
1504 elsif Nkind (N) = N_Protected_Type_Declaration then
736b80cc 1505 Def := Protected_Definition (N);
1506
1507 if No (Def) then
1508 Def :=
1509 Make_Protected_Definition (Sloc (N),
1510 Visible_Declarations => New_List,
1511 End_Label => Empty);
1512
1513 Set_Protected_Definition (N, Def);
1514 end if;
1515
1516 Decls := Visible_Declarations (Def);
2f06c88a 1517
1518 if No (Decls) then
1519 Decls := New_List;
736b80cc 1520 Set_Visible_Declarations (Def, Decls);
2f06c88a 1521 end if;
1522
1523 Prepend_To (Decls, Prag);
1524
736b80cc 1525 -- When the aspect is associated with a task unit declaration, insert
1526 -- insert the generated pragma at the top of the visible declarations
1527 -- the emulate the behavior of a source pragma.
2f06c88a 1528
1529 -- task [type] Prot with Aspect is
1530
1531 -- task [type] Prot is
1532 -- pragma Prag;
1533
736b80cc 1534 elsif Nkind (N) = N_Task_Type_Declaration then
1535 Def := Task_Definition (N);
1536
1537 if No (Def) then
1538 Def :=
1539 Make_Task_Definition (Sloc (N),
1540 Visible_Declarations => New_List,
1541 End_Label => Empty);
1542
1543 Set_Task_Definition (N, Def);
1544 end if;
1545
1546 Decls := Visible_Declarations (Def);
2f06c88a 1547
1548 if No (Decls) then
1549 Decls := New_List;
736b80cc 1550 Set_Visible_Declarations (Def, Decls);
d324c418 1551 end if;
c1006d6d 1552
2f06c88a 1553 Prepend_To (Decls, Prag);
1554
ed695684 1555 -- When the context is a library unit, the pragma is added to the
1556 -- Pragmas_After list.
1557
1558 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1559 Aux := Aux_Decls_Node (Parent (N));
1560
1561 if No (Pragmas_After (Aux)) then
1562 Set_Pragmas_After (Aux, New_List);
1563 end if;
1564
1565 Prepend (Prag, Pragmas_After (Aux));
1566
2f06c88a 1567 -- Default, the pragma is inserted after the context
c1006d6d 1568
1569 else
1570 Insert_After (N, Prag);
c1006d6d 1571 end if;
e2bf777d 1572 end Insert_Pragma;
c1006d6d 1573
1574 -- Local variables
1575
ae888dbd 1576 Aspect : Node_Id;
d74fc39a 1577 Aitem : Node_Id;
ae888dbd 1578 Ent : Node_Id;
ae888dbd 1579
21ea3a4f 1580 L : constant List_Id := Aspect_Specifications (N);
1581
ae888dbd 1582 Ins_Node : Node_Id := N;
89f1e35c 1583 -- Insert pragmas/attribute definition clause after this node when no
1584 -- delayed analysis is required.
d74fc39a 1585
ee2b7923 1586 -- Start of processing for Analyze_Aspect_Specifications
f0813d71 1587
ee2b7923 1588 begin
d74fc39a 1589 -- The general processing involves building an attribute definition
89f1e35c 1590 -- clause or a pragma node that corresponds to the aspect. Then in order
1591 -- to delay the evaluation of this aspect to the freeze point, we attach
1592 -- the corresponding pragma/attribute definition clause to the aspect
1593 -- specification node, which is then placed in the Rep Item chain. In
1594 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1595 -- and we evaluate the rep item at the freeze point. When the aspect
1596 -- doesn't have a corresponding pragma/attribute definition clause, then
1597 -- its analysis is simply delayed at the freeze point.
1598
1599 -- Some special cases don't require delay analysis, thus the aspect is
1600 -- analyzed right now.
1601
51ea9c94 1602 -- Note that there is a special handling for Pre, Post, Test_Case,
e66f4e2a 1603 -- Contract_Cases aspects. In these cases, we do not have to worry
51ea9c94 1604 -- about delay issues, since the pragmas themselves deal with delay
1605 -- of visibility for the expression analysis. Thus, we just insert
1606 -- the pragma after the node N.
ae888dbd 1607
21ea3a4f 1608 pragma Assert (Present (L));
1609
6fb3c314 1610 -- Loop through aspects
f93e7257 1611
ae888dbd 1612 Aspect := First (L);
21ea3a4f 1613 Aspect_Loop : while Present (Aspect) loop
0fd13d32 1614 Analyze_One_Aspect : declare
94153a42 1615 Expr : constant Node_Id := Expression (Aspect);
89f1e35c 1616 Id : constant Node_Id := Identifier (Aspect);
1617 Loc : constant Source_Ptr := Sloc (Aspect);
94153a42 1618 Nam : constant Name_Id := Chars (Id);
1619 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
ae888dbd 1620 Anod : Node_Id;
1621
37c6e44c 1622 Delay_Required : Boolean;
89f1e35c 1623 -- Set False if delay is not required
1624
c0793fff 1625 Eloc : Source_Ptr := No_Location;
1626 -- Source location of expression, modified when we split PPC's. It
1627 -- is set below when Expr is present.
39e1f22f 1628
ee2b7923 1629 procedure Analyze_Aspect_Convention;
1630 -- Perform analysis of aspect Convention
1631
0b10029c 1632 procedure Analyze_Aspect_Disable_Controlled;
1633 -- Perform analysis of aspect Disable_Controlled
1634
ee2b7923 1635 procedure Analyze_Aspect_Export_Import;
1636 -- Perform analysis of aspects Export or Import
1637
1638 procedure Analyze_Aspect_External_Link_Name;
1639 -- Perform analysis of aspects External_Name or Link_Name
21ea3a4f 1640
89f1e35c 1641 procedure Analyze_Aspect_Implicit_Dereference;
9ab32fe9 1642 -- Perform analysis of the Implicit_Dereference aspects
0fd13d32 1643
1644 procedure Make_Aitem_Pragma
1645 (Pragma_Argument_Associations : List_Id;
1646 Pragma_Name : Name_Id);
1647 -- This is a wrapper for Make_Pragma used for converting aspects
1648 -- to pragmas. It takes care of Sloc (set from Loc) and building
1649 -- the pragma identifier from the given name. In addition the
1650 -- flags Class_Present and Split_PPC are set from the aspect
1651 -- node, as well as Is_Ignored. This routine also sets the
1652 -- From_Aspect_Specification in the resulting pragma node to
1653 -- True, and sets Corresponding_Aspect to point to the aspect.
1654 -- The resulting pragma is assigned to Aitem.
21ea3a4f 1655
ee2b7923 1656 -------------------------------
1657 -- Analyze_Aspect_Convention --
1658 -------------------------------
1659
1660 procedure Analyze_Aspect_Convention is
1661 Conv : Node_Id;
1662 Dummy_1 : Node_Id;
1663 Dummy_2 : Node_Id;
1664 Dummy_3 : Node_Id;
1665 Expo : Node_Id;
1666 Imp : Node_Id;
89f1e35c 1667
21ea3a4f 1668 begin
ee2b7923 1669 -- Obtain all interfacing aspects that apply to the related
1670 -- entity.
1671
1672 Get_Interfacing_Aspects
1673 (Iface_Asp => Aspect,
1674 Conv_Asp => Dummy_1,
1675 EN_Asp => Dummy_2,
1676 Expo_Asp => Expo,
1677 Imp_Asp => Imp,
1678 LN_Asp => Dummy_3,
1679 Do_Checks => True);
1680
1681 -- The related entity is subject to aspect Export or Import.
1682 -- Do not process Convention now because it must be analysed
1683 -- as part of Export or Import.
1684
1685 if Present (Expo) or else Present (Imp) then
1686 return;
21ea3a4f 1687
ee2b7923 1688 -- Otherwise Convention appears by itself
21ea3a4f 1689
ee2b7923 1690 else
1691 -- The aspect specifies a particular convention
1692
1693 if Present (Expr) then
1694 Conv := New_Copy_Tree (Expr);
1695
1696 -- Otherwise assume convention Ada
1697
1698 else
1699 Conv := Make_Identifier (Loc, Name_Ada);
1700 end if;
1701
1702 -- Generate:
1703 -- pragma Convention (<Conv>, <E>);
1704
1705 Make_Aitem_Pragma
1706 (Pragma_Name => Name_Convention,
1707 Pragma_Argument_Associations => New_List (
1708 Make_Pragma_Argument_Association (Loc,
1709 Expression => Conv),
1710 Make_Pragma_Argument_Association (Loc,
1711 Expression => New_Occurrence_Of (E, Loc))));
1712
1713 Decorate (Aspect, Aitem);
1714 Insert_Pragma (Aitem);
1715 end if;
1716 end Analyze_Aspect_Convention;
1717
0b10029c 1718 ---------------------------------------
1719 -- Analyze_Aspect_Disable_Controlled --
1720 ---------------------------------------
1721
1722 procedure Analyze_Aspect_Disable_Controlled is
1723 begin
1724 -- The aspect applies only to controlled records
1725
1726 if not (Ekind (E) = E_Record_Type
1727 and then Is_Controlled_Active (E))
1728 then
1729 Error_Msg_N
1730 ("aspect % requires controlled record type", Aspect);
1731 return;
1732 end if;
1733
1734 -- Preanalyze the expression (if any) when the aspect resides
1735 -- in a generic unit.
1736
1737 if Inside_A_Generic then
1738 if Present (Expr) then
1739 Preanalyze_And_Resolve (Expr, Any_Boolean);
1740 end if;
1741
1742 -- Otherwise the aspect resides in a nongeneric context
1743
1744 else
1745 -- A controlled record type loses its controlled semantics
1746 -- when the expression statically evaluates to True.
1747
1748 if Present (Expr) then
1749 Analyze_And_Resolve (Expr, Any_Boolean);
1750
1751 if Is_OK_Static_Expression (Expr) then
1752 if Is_True (Static_Boolean (Expr)) then
1753 Set_Disable_Controlled (E);
1754 end if;
1755
1756 -- Otherwise the expression is not static
1757
1758 else
1759 Error_Msg_N
1760 ("expression of aspect % must be static", Aspect);
1761 end if;
1762
1763 -- Otherwise the aspect appears without an expression and
1764 -- defaults to True.
1765
1766 else
1767 Set_Disable_Controlled (E);
1768 end if;
1769 end if;
1770 end Analyze_Aspect_Disable_Controlled;
1771
ee2b7923 1772 ----------------------------------
1773 -- Analyze_Aspect_Export_Import --
1774 ----------------------------------
21ea3a4f 1775
ee2b7923 1776 procedure Analyze_Aspect_Export_Import is
1777 Dummy_1 : Node_Id;
1778 Dummy_2 : Node_Id;
1779 Dummy_3 : Node_Id;
1780 Expo : Node_Id;
1781 Imp : Node_Id;
1782
1783 begin
1784 -- Obtain all interfacing aspects that apply to the related
1785 -- entity.
1786
1787 Get_Interfacing_Aspects
1788 (Iface_Asp => Aspect,
1789 Conv_Asp => Dummy_1,
1790 EN_Asp => Dummy_2,
1791 Expo_Asp => Expo,
1792 Imp_Asp => Imp,
1793 LN_Asp => Dummy_3,
1794 Do_Checks => True);
1795
1796 -- The related entity cannot be subject to both aspects Export
1797 -- and Import.
1798
1799 if Present (Expo) and then Present (Imp) then
1800 Error_Msg_N
1801 ("incompatible interfacing aspects given for &", E);
1802 Error_Msg_Sloc := Sloc (Expo);
1803 Error_Msg_N ("\aspect `Export` #", E);
1804 Error_Msg_Sloc := Sloc (Imp);
1805 Error_Msg_N ("\aspect `Import` #", E);
1806 end if;
1807
1808 -- A variable is most likely modified from the outside. Take
051826ee 1809 -- the optimistic approach to avoid spurious errors.
ee2b7923 1810
1811 if Ekind (E) = E_Variable then
1812 Set_Never_Set_In_Source (E, False);
1813 end if;
1814
1815 -- Resolve the expression of an Import or Export here, and
1816 -- require it to be of type Boolean and static. This is not
1817 -- quite right, because in general this should be delayed,
1818 -- but that seems tricky for these, because normally Boolean
1819 -- aspects are replaced with pragmas at the freeze point in
1820 -- Make_Pragma_From_Boolean_Aspect.
1821
1822 if not Present (Expr)
1823 or else Is_True (Static_Boolean (Expr))
1824 then
1825 if A_Id = Aspect_Import then
1826 Set_Has_Completion (E);
1827 Set_Is_Imported (E);
1828
1829 -- An imported object cannot be explicitly initialized
1830
1831 if Nkind (N) = N_Object_Declaration
1832 and then Present (Expression (N))
1833 then
1834 Error_Msg_N
1835 ("imported entities cannot be initialized "
1836 & "(RM B.1(24))", Expression (N));
1837 end if;
1838
1839 else
1840 pragma Assert (A_Id = Aspect_Export);
1841 Set_Is_Exported (E);
1842 end if;
1843
1844 -- Create the proper form of pragma Export or Import taking
1845 -- into account Conversion, External_Name, and Link_Name.
1846
1847 Aitem := Build_Export_Import_Pragma (Aspect, E);
d8e539ae 1848
1849 -- Otherwise the expression is either False or erroneous. There
1850 -- is no corresponding pragma.
1851
1852 else
1853 Aitem := Empty;
ee2b7923 1854 end if;
1855 end Analyze_Aspect_Export_Import;
1856
1857 ---------------------------------------
1858 -- Analyze_Aspect_External_Link_Name --
1859 ---------------------------------------
1860
1861 procedure Analyze_Aspect_External_Link_Name is
1862 Dummy_1 : Node_Id;
1863 Dummy_2 : Node_Id;
1864 Dummy_3 : Node_Id;
1865 Expo : Node_Id;
1866 Imp : Node_Id;
1867
1868 begin
1869 -- Obtain all interfacing aspects that apply to the related
1870 -- entity.
1871
1872 Get_Interfacing_Aspects
1873 (Iface_Asp => Aspect,
1874 Conv_Asp => Dummy_1,
1875 EN_Asp => Dummy_2,
1876 Expo_Asp => Expo,
1877 Imp_Asp => Imp,
1878 LN_Asp => Dummy_3,
1879 Do_Checks => True);
1880
1881 -- Ensure that aspect External_Name applies to aspect Export or
1882 -- Import.
1883
1884 if A_Id = Aspect_External_Name then
1885 if No (Expo) and then No (Imp) then
89f1e35c 1886 Error_Msg_N
ee2b7923 1887 ("aspect `External_Name` requires aspect `Import` or "
1888 & "`Export`", Aspect);
89f1e35c 1889 end if;
ee2b7923 1890
1891 -- Otherwise ensure that aspect Link_Name applies to aspect
1892 -- Export or Import.
1893
1894 else
1895 pragma Assert (A_Id = Aspect_Link_Name);
1896 if No (Expo) and then No (Imp) then
1897 Error_Msg_N
1898 ("aspect `Link_Name` requires aspect `Import` or "
1899 & "`Export`", Aspect);
1900 end if;
1901 end if;
1902 end Analyze_Aspect_External_Link_Name;
21ea3a4f 1903
89f1e35c 1904 -----------------------------------------
1905 -- Analyze_Aspect_Implicit_Dereference --
1906 -----------------------------------------
21ea3a4f 1907
89f1e35c 1908 procedure Analyze_Aspect_Implicit_Dereference is
1ff43c00 1909 Disc : Entity_Id;
1910 Parent_Disc : Entity_Id;
1911
89f1e35c 1912 begin
b9e61b2a 1913 if not Is_Type (E) or else not Has_Discriminants (E) then
89f1e35c 1914 Error_Msg_N
1ff43c00 1915 ("aspect must apply to a type with discriminants", Expr);
21ea3a4f 1916
1ff43c00 1917 elsif not Is_Entity_Name (Expr) then
1918 Error_Msg_N
1919 ("aspect must name a discriminant of current type", Expr);
21ea3a4f 1920
1ff43c00 1921 else
f021ee0f 1922 -- Discriminant type be an anonymous access type or an
1923 -- anonymous access to subprogram.
0d0a4e9b 1924
f021ee0f 1925 -- Missing synchronized types???
1926
1ff43c00 1927 Disc := First_Discriminant (E);
1928 while Present (Disc) loop
1929 if Chars (Expr) = Chars (Disc)
f021ee0f 1930 and then Ekind_In (Etype (Disc),
0d0a4e9b 1931 E_Anonymous_Access_Subprogram_Type,
1932 E_Anonymous_Access_Type)
1ff43c00 1933 then
1934 Set_Has_Implicit_Dereference (E);
1935 Set_Has_Implicit_Dereference (Disc);
1936 exit;
1937 end if;
21ea3a4f 1938
1ff43c00 1939 Next_Discriminant (Disc);
1940 end loop;
21ea3a4f 1941
9b5b11fb 1942 -- Error if no proper access discriminant
21ea3a4f 1943
1ff43c00 1944 if No (Disc) then
ee2b7923 1945 Error_Msg_NE ("not an access discriminant of&", Expr, E);
1ff43c00 1946 return;
1947 end if;
1948 end if;
1949
9b5b11fb 1950 -- For a type extension, check whether parent has a
1951 -- reference discriminant, to verify that use is proper.
1952
1ff43c00 1953 if Is_Derived_Type (E)
1954 and then Has_Discriminants (Etype (E))
1955 then
1956 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1957
1958 if Present (Parent_Disc)
1959 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1960 then
ee2b7923 1961 Error_Msg_N
1962 ("reference discriminant does not match discriminant "
1963 & "of parent type", Expr);
1ff43c00 1964 end if;
89f1e35c 1965 end if;
1966 end Analyze_Aspect_Implicit_Dereference;
21ea3a4f 1967
0fd13d32 1968 -----------------------
1969 -- Make_Aitem_Pragma --
1970 -----------------------
1971
1972 procedure Make_Aitem_Pragma
1973 (Pragma_Argument_Associations : List_Id;
1974 Pragma_Name : Name_Id)
1975 is
b855559d 1976 Args : List_Id := Pragma_Argument_Associations;
1977
0fd13d32 1978 begin
1979 -- We should never get here if aspect was disabled
1980
1981 pragma Assert (not Is_Disabled (Aspect));
1982
056dc987 1983 -- Certain aspects allow for an optional name or expression. Do
1984 -- not generate a pragma with empty argument association list.
b855559d 1985
1986 if No (Args) or else No (Expression (First (Args))) then
1987 Args := No_List;
1988 end if;
1989
0fd13d32 1990 -- Build the pragma
1991
1992 Aitem :=
1993 Make_Pragma (Loc,
b855559d 1994 Pragma_Argument_Associations => Args,
0fd13d32 1995 Pragma_Identifier =>
1996 Make_Identifier (Sloc (Id), Pragma_Name),
9ab32fe9 1997 Class_Present => Class_Present (Aspect),
1998 Split_PPC => Split_PPC (Aspect));
0fd13d32 1999
2000 -- Set additional semantic fields
2001
2002 if Is_Ignored (Aspect) then
2003 Set_Is_Ignored (Aitem);
57d8d1f3 2004 elsif Is_Checked (Aspect) then
a5109493 2005 Set_Is_Checked (Aitem);
0fd13d32 2006 end if;
2007
2008 Set_Corresponding_Aspect (Aitem, Aspect);
fdec445e 2009 Set_From_Aspect_Specification (Aitem);
0fd13d32 2010 end Make_Aitem_Pragma;
2011
738ec25b 2012 -- Start of processing for Analyze_One_Aspect
0fd13d32 2013
ae888dbd 2014 begin
2d1acfa7 2015 -- Skip aspect if already analyzed, to avoid looping in some cases
fb7f2fc4 2016
2017 if Analyzed (Aspect) then
2018 goto Continue;
2019 end if;
2020
ef957022 2021 -- Skip looking at aspect if it is totally disabled. Just mark it
2022 -- as such for later reference in the tree. This also sets the
2023 -- Is_Ignored and Is_Checked flags appropriately.
51ea9c94 2024
2025 Check_Applicable_Policy (Aspect);
2026
2027 if Is_Disabled (Aspect) then
2028 goto Continue;
2029 end if;
2030
c0793fff 2031 -- Set the source location of expression, used in the case of
2032 -- a failed precondition/postcondition or invariant. Note that
2033 -- the source location of the expression is not usually the best
2034 -- choice here. For example, it gets located on the last AND
2035 -- keyword in a chain of boolean expressiond AND'ed together.
2036 -- It is best to put the message on the first character of the
2037 -- assertion, which is the effect of the First_Node call here.
2038
2039 if Present (Expr) then
2040 Eloc := Sloc (First_Node (Expr));
2041 end if;
2042
d7ed83a2 2043 -- Check restriction No_Implementation_Aspect_Specifications
2044
c171e1be 2045 if Implementation_Defined_Aspect (A_Id) then
d7ed83a2 2046 Check_Restriction
2047 (No_Implementation_Aspect_Specifications, Aspect);
2048 end if;
2049
2050 -- Check restriction No_Specification_Of_Aspect
2051
2052 Check_Restriction_No_Specification_Of_Aspect (Aspect);
2053
f67ed4f5 2054 -- Mark aspect analyzed (actual analysis is delayed till later)
d7ed83a2 2055
fb7f2fc4 2056 Set_Analyzed (Aspect);
d74fc39a 2057 Set_Entity (Aspect, E);
738ec25b 2058
2059 -- Build the reference to E that will be used in the built pragmas
2060
d74fc39a 2061 Ent := New_Occurrence_Of (E, Sloc (Id));
2062
738ec25b 2063 if A_Id = Aspect_Attach_Handler
2064 or else A_Id = Aspect_Interrupt_Handler
2065 then
738ec25b 2066
f0e731f2 2067 -- Treat the specification as a reference to the protected
2068 -- operation, which might otherwise appear unreferenced and
2069 -- generate spurious warnings.
738ec25b 2070
f0e731f2 2071 Generate_Reference (E, Id);
738ec25b 2072 end if;
2073
1e3c4ae6 2074 -- Check for duplicate aspect. Note that the Comes_From_Source
2075 -- test allows duplicate Pre/Post's that we generate internally
2076 -- to escape being flagged here.
ae888dbd 2077
6c545057 2078 if No_Duplicates_Allowed (A_Id) then
2079 Anod := First (L);
2080 while Anod /= Aspect loop
c171e1be 2081 if Comes_From_Source (Aspect)
2082 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
6c545057 2083 then
2084 Error_Msg_Name_1 := Nam;
2085 Error_Msg_Sloc := Sloc (Anod);
39e1f22f 2086
6c545057 2087 -- Case of same aspect specified twice
39e1f22f 2088
6c545057 2089 if Class_Present (Anod) = Class_Present (Aspect) then
2090 if not Class_Present (Anod) then
2091 Error_Msg_NE
2092 ("aspect% for & previously given#",
2093 Id, E);
2094 else
2095 Error_Msg_NE
2096 ("aspect `%''Class` for & previously given#",
2097 Id, E);
2098 end if;
39e1f22f 2099 end if;
6c545057 2100 end if;
ae888dbd 2101
6c545057 2102 Next (Anod);
2103 end loop;
2104 end if;
ae888dbd 2105
4db325e6 2106 -- Check some general restrictions on language defined aspects
2107
c171e1be 2108 if not Implementation_Defined_Aspect (A_Id) then
4db325e6 2109 Error_Msg_Name_1 := Nam;
2110
d1edd78e 2111 -- Not allowed for renaming declarations. Examine the original
da1b7592 2112 -- node because a subprogram renaming may have been rewritten
2113 -- as a body.
4db325e6 2114
da1b7592 2115 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
4db325e6 2116 Error_Msg_N
2117 ("aspect % not allowed for renaming declaration",
2118 Aspect);
2119 end if;
2120
2121 -- Not allowed for formal type declarations
2122
2123 if Nkind (N) = N_Formal_Type_Declaration then
2124 Error_Msg_N
2125 ("aspect % not allowed for formal type declaration",
2126 Aspect);
2127 end if;
2128 end if;
2129
7d20685d 2130 -- Copy expression for later processing by the procedures
2131 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2132
2133 Set_Entity (Id, New_Copy_Tree (Expr));
2134
37c6e44c 2135 -- Set Delay_Required as appropriate to aspect
2136
2137 case Aspect_Delay (A_Id) is
2138 when Always_Delay =>
2139 Delay_Required := True;
2140
2141 when Never_Delay =>
2142 Delay_Required := False;
2143
2144 when Rep_Aspect =>
2145
2146 -- If expression has the form of an integer literal, then
2147 -- do not delay, since we know the value cannot change.
2148 -- This optimization catches most rep clause cases.
2149
e43fc5c5 2150 -- For Boolean aspects, don't delay if no expression
2151
2152 if A_Id in Boolean_Aspects and then No (Expr) then
2153 Delay_Required := False;
2154
c5c6a638 2155 -- For non-Boolean aspects, don't delay if integer literal,
2156 -- unless the aspect is Alignment, which affects the
2157 -- freezing of an initialized object.
e43fc5c5 2158
2159 elsif A_Id not in Boolean_Aspects
c5c6a638 2160 and then A_Id /= Aspect_Alignment
e43fc5c5 2161 and then Present (Expr)
2162 and then Nkind (Expr) = N_Integer_Literal
2163 then
2164 Delay_Required := False;
2165
2166 -- All other cases are delayed
2167
2168 else
2169 Delay_Required := True;
2170 Set_Has_Delayed_Rep_Aspects (E);
2171 end if;
37c6e44c 2172 end case;
2173
ae888dbd 2174 -- Processing based on specific aspect
2175
d74fc39a 2176 case A_Id is
aa2f48d2 2177 when Aspect_Unimplemented =>
2178 null; -- ??? temp for now
ae888dbd 2179
2180 -- No_Aspect should be impossible
2181
2182 when No_Aspect =>
2183 raise Program_Error;
2184
89f1e35c 2185 -- Case 1: Aspects corresponding to attribute definition
2186 -- clauses.
ae888dbd 2187
99378362 2188 when Aspect_Address
2189 | Aspect_Alignment
2190 | Aspect_Bit_Order
2191 | Aspect_Component_Size
2192 | Aspect_Constant_Indexing
2193 | Aspect_Default_Iterator
2194 | Aspect_Dispatching_Domain
2195 | Aspect_External_Tag
2196 | Aspect_Input
2197 | Aspect_Iterable
2198 | Aspect_Iterator_Element
2199 | Aspect_Machine_Radix
2200 | Aspect_Object_Size
2201 | Aspect_Output
2202 | Aspect_Read
2203 | Aspect_Scalar_Storage_Order
2204 | Aspect_Secondary_Stack_Size
2205 | Aspect_Simple_Storage_Pool
2206 | Aspect_Size
2207 | Aspect_Small
2208 | Aspect_Storage_Pool
2209 | Aspect_Stream_Size
2210 | Aspect_Value_Size
2211 | Aspect_Variable_Indexing
2212 | Aspect_Write
2213 =>
89f1e35c 2214 -- Indexing aspects apply only to tagged type
2215
2216 if (A_Id = Aspect_Constant_Indexing
37c6e44c 2217 or else
2218 A_Id = Aspect_Variable_Indexing)
89f1e35c 2219 and then not (Is_Type (E)
2220 and then Is_Tagged_Type (E))
2221 then
05987af3 2222 Error_Msg_N
2223 ("indexing aspect can only apply to a tagged type",
3f4c9ffc 2224 Aspect);
89f1e35c 2225 goto Continue;
2226 end if;
2227
39616053 2228 -- For the case of aspect Address, we don't consider that we
588e7f97 2229 -- know the entity is never set in the source, since it is
2230 -- is likely aliasing is occurring.
2231
2232 -- Note: one might think that the analysis of the resulting
2233 -- attribute definition clause would take care of that, but
2234 -- that's not the case since it won't be from source.
2235
2236 if A_Id = Aspect_Address then
2237 Set_Never_Set_In_Source (E, False);
2238 end if;
2239
5ac76cee 2240 -- Correctness of the profile of a stream operation is
2241 -- verified at the freeze point, but we must detect the
2242 -- illegal specification of this aspect for a subtype now,
2243 -- to prevent malformed rep_item chains.
2244
fbf4d6ef 2245 if A_Id = Aspect_Input or else
2246 A_Id = Aspect_Output or else
2247 A_Id = Aspect_Read or else
2248 A_Id = Aspect_Write
5ac76cee 2249 then
fbf4d6ef 2250 if not Is_First_Subtype (E) then
2251 Error_Msg_N
2252 ("local name must be a first subtype", Aspect);
2253 goto Continue;
2254
2255 -- If stream aspect applies to the class-wide type,
2256 -- the generated attribute definition applies to the
2257 -- class-wide type as well.
2258
2259 elsif Class_Present (Aspect) then
2260 Ent :=
2261 Make_Attribute_Reference (Loc,
2262 Prefix => Ent,
2263 Attribute_Name => Name_Class);
2264 end if;
5ac76cee 2265 end if;
2266
842e7c6b 2267 -- Construct the attribute_definition_clause. The expression
2268 -- in the aspect specification is simply shared with the
2269 -- constructed attribute, because it will be fully analyzed
2270 -- when the attribute is processed. However, in ASIS mode
2271 -- the aspect expression itself is preanalyzed and resolved
2272 -- to catch visibility errors that are otherwise caught
2273 -- later, and we create a separate copy of the expression
2274 -- to prevent analysis of a malformed tree (e.g. a function
2275 -- call with parameter associations).
2276
2277 if ASIS_Mode then
2278 Aitem :=
2279 Make_Attribute_Definition_Clause (Loc,
2280 Name => Ent,
2281 Chars => Chars (Id),
2282 Expression => New_Copy_Tree (Expr));
2283 else
2284 Aitem :=
2285 Make_Attribute_Definition_Clause (Loc,
2286 Name => Ent,
2287 Chars => Chars (Id),
2288 Expression => Relocate_Node (Expr));
2289 end if;
ae888dbd 2290
af9a0cc3 2291 -- If the address is specified, then we treat the entity as
41f06abf 2292 -- referenced, to avoid spurious warnings. This is analogous
2293 -- to what is done with an attribute definition clause, but
2294 -- here we don't want to generate a reference because this
2295 -- is the point of definition of the entity.
2296
2297 if A_Id = Aspect_Address then
2298 Set_Referenced (E);
2299 end if;
2300
51ea9c94 2301 -- Case 2: Aspects corresponding to pragmas
d74fc39a 2302
89f1e35c 2303 -- Case 2a: Aspects corresponding to pragmas with two
2304 -- arguments, where the first argument is a local name
2305 -- referring to the entity, and the second argument is the
2306 -- aspect definition expression.
ae888dbd 2307
04ae062f 2308 -- Linker_Section/Suppress/Unsuppress
0fd13d32 2309
99378362 2310 when Aspect_Linker_Section
2311 | Aspect_Suppress
2312 | Aspect_Unsuppress
2313 =>
0fd13d32 2314 Make_Aitem_Pragma
2315 (Pragma_Argument_Associations => New_List (
2316 Make_Pragma_Argument_Association (Loc,
2317 Expression => New_Occurrence_Of (E, Loc)),
2318 Make_Pragma_Argument_Association (Sloc (Expr),
2319 Expression => Relocate_Node (Expr))),
2320 Pragma_Name => Chars (Id));
57cd943b 2321
33dde36e 2322 -- Linker_Section does not need delaying, as its argument
2323 -- must be a static string. Furthermore, if applied to
2324 -- an object with an explicit initialization, the object
2325 -- must be frozen in order to elaborate the initialization
2326 -- code. (This is already done for types with implicit
2327 -- initialization, such as protected types.)
2328
2329 if A_Id = Aspect_Linker_Section
2330 and then Nkind (N) = N_Object_Declaration
2331 and then Has_Init_Expression (N)
2332 then
2333 Delay_Required := False;
2334 end if;
2335
0fd13d32 2336 -- Synchronization
d74fc39a 2337
0fd13d32 2338 -- Corresponds to pragma Implemented, construct the pragma
49213728 2339
5bbfbad2 2340 when Aspect_Synchronization =>
0fd13d32 2341 Make_Aitem_Pragma
2342 (Pragma_Argument_Associations => New_List (
2343 Make_Pragma_Argument_Association (Loc,
2344 Expression => New_Occurrence_Of (E, Loc)),
2345 Make_Pragma_Argument_Association (Sloc (Expr),
2346 Expression => Relocate_Node (Expr))),
2347 Pragma_Name => Name_Implemented);
49213728 2348
e2bf777d 2349 -- Attach_Handler
0fd13d32 2350
89f1e35c 2351 when Aspect_Attach_Handler =>
0fd13d32 2352 Make_Aitem_Pragma
2353 (Pragma_Argument_Associations => New_List (
2354 Make_Pragma_Argument_Association (Sloc (Ent),
2355 Expression => Ent),
2356 Make_Pragma_Argument_Association (Sloc (Expr),
2357 Expression => Relocate_Node (Expr))),
2358 Pragma_Name => Name_Attach_Handler);
2359
f67ed4f5 2360 -- We need to insert this pragma into the tree to get proper
2361 -- processing and to look valid from a placement viewpoint.
2362
e2bf777d 2363 Insert_Pragma (Aitem);
f67ed4f5 2364 goto Continue;
2365
0fd13d32 2366 -- Dynamic_Predicate, Predicate, Static_Predicate
89f1e35c 2367
99378362 2368 when Aspect_Dynamic_Predicate
2369 | Aspect_Predicate
2370 | Aspect_Static_Predicate
2371 =>
a47ce82d 2372 -- These aspects apply only to subtypes
2373
2374 if not Is_Type (E) then
2375 Error_Msg_N
2376 ("predicate can only be specified for a subtype",
2377 Aspect);
2378 goto Continue;
7c0c95b8 2379
2380 elsif Is_Incomplete_Type (E) then
2381 Error_Msg_N
2382 ("predicate cannot apply to incomplete view", Aspect);
2383 goto Continue;
a47ce82d 2384 end if;
2385
89f1e35c 2386 -- Construct the pragma (always a pragma Predicate, with
51ea9c94 2387 -- flags recording whether it is static/dynamic). We also
2388 -- set flags recording this in the type itself.
89f1e35c 2389
0fd13d32 2390 Make_Aitem_Pragma
2391 (Pragma_Argument_Associations => New_List (
2392 Make_Pragma_Argument_Association (Sloc (Ent),
2393 Expression => Ent),
2394 Make_Pragma_Argument_Association (Sloc (Expr),
2395 Expression => Relocate_Node (Expr))),
fdec445e 2396 Pragma_Name => Name_Predicate);
89f1e35c 2397
51ea9c94 2398 -- Mark type has predicates, and remember what kind of
2399 -- aspect lead to this predicate (we need this to access
2400 -- the right set of check policies later on).
2401
2402 Set_Has_Predicates (E);
2403
2404 if A_Id = Aspect_Dynamic_Predicate then
2405 Set_Has_Dynamic_Predicate_Aspect (E);
0ec8f3e0 2406
2407 -- If the entity has a dynamic predicate, any inherited
2408 -- static predicate becomes dynamic as well, and the
2409 -- predicate function includes the conjunction of both.
2410
2411 Set_Has_Static_Predicate_Aspect (E, False);
2412
51ea9c94 2413 elsif A_Id = Aspect_Static_Predicate then
2414 Set_Has_Static_Predicate_Aspect (E);
2415 end if;
2416
89f1e35c 2417 -- If the type is private, indicate that its completion
6653b695 2418 -- has a freeze node, because that is the one that will
2419 -- be visible at freeze time.
89f1e35c 2420
0fd13d32 2421 if Is_Private_Type (E) and then Present (Full_View (E)) then
89f1e35c 2422 Set_Has_Predicates (Full_View (E));
51ea9c94 2423
2424 if A_Id = Aspect_Dynamic_Predicate then
2425 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2426 elsif A_Id = Aspect_Static_Predicate then
2427 Set_Has_Static_Predicate_Aspect (Full_View (E));
2428 end if;
2429
89f1e35c 2430 Set_Has_Delayed_Aspects (Full_View (E));
2431 Ensure_Freeze_Node (Full_View (E));
2432 end if;
2433
fdec445e 2434 -- Predicate_Failure
2435
2436 when Aspect_Predicate_Failure =>
2437
2438 -- This aspect applies only to subtypes
2439
2440 if not Is_Type (E) then
2441 Error_Msg_N
2442 ("predicate can only be specified for a subtype",
2443 Aspect);
2444 goto Continue;
2445
2446 elsif Is_Incomplete_Type (E) then
2447 Error_Msg_N
2448 ("predicate cannot apply to incomplete view", Aspect);
2449 goto Continue;
2450 end if;
2451
2452 -- Construct the pragma
2453
2454 Make_Aitem_Pragma
2455 (Pragma_Argument_Associations => New_List (
2456 Make_Pragma_Argument_Association (Sloc (Ent),
2457 Expression => Ent),
2458 Make_Pragma_Argument_Association (Sloc (Expr),
2459 Expression => Relocate_Node (Expr))),
2460 Pragma_Name => Name_Predicate_Failure);
2461
2462 Set_Has_Predicates (E);
2463
2464 -- If the type is private, indicate that its completion
2465 -- has a freeze node, because that is the one that will
2466 -- be visible at freeze time.
2467
2468 if Is_Private_Type (E) and then Present (Full_View (E)) then
2469 Set_Has_Predicates (Full_View (E));
2470 Set_Has_Delayed_Aspects (Full_View (E));
2471 Ensure_Freeze_Node (Full_View (E));
2472 end if;
2473
89f1e35c 2474 -- Case 2b: Aspects corresponding to pragmas with two
2475 -- arguments, where the second argument is a local name
2476 -- referring to the entity, and the first argument is the
2477 -- aspect definition expression.
ae888dbd 2478
0fd13d32 2479 -- Convention
2480
ee2b7923 2481 when Aspect_Convention =>
2482 Analyze_Aspect_Convention;
2483 goto Continue;
97bf66e6 2484
ee2b7923 2485 -- External_Name, Link_Name
97bf66e6 2486
99378362 2487 when Aspect_External_Name
2488 | Aspect_Link_Name
2489 =>
ee2b7923 2490 Analyze_Aspect_External_Link_Name;
2491 goto Continue;
e1cedbae 2492
0fd13d32 2493 -- CPU, Interrupt_Priority, Priority
2494
d6814978 2495 -- These three aspects can be specified for a subprogram spec
2496 -- or body, in which case we analyze the expression and export
2497 -- the value of the aspect.
2498
2499 -- Previously, we generated an equivalent pragma for bodies
2500 -- (note that the specs cannot contain these pragmas). The
2501 -- pragma was inserted ahead of local declarations, rather than
2502 -- after the body. This leads to a certain duplication between
2503 -- the processing performed for the aspect and the pragma, but
2504 -- given the straightforward handling required it is simpler
2505 -- to duplicate than to translate the aspect in the spec into
2506 -- a pragma in the declarative part of the body.
3a72f9c3 2507
99378362 2508 when Aspect_CPU
2509 | Aspect_Interrupt_Priority
2510 | Aspect_Priority
2511 =>
d6814978 2512 if Nkind_In (N, N_Subprogram_Body,
2513 N_Subprogram_Declaration)
2514 then
2515 -- Analyze the aspect expression
2516
2517 Analyze_And_Resolve (Expr, Standard_Integer);
2518
2519 -- Interrupt_Priority aspect not allowed for main
078a74b8 2520 -- subprograms. RM D.1 does not forbid this explicitly,
2521 -- but RM J.15.11(6/3) does not permit pragma
d6814978 2522 -- Interrupt_Priority for subprograms.
2523
2524 if A_Id = Aspect_Interrupt_Priority then
2525 Error_Msg_N
2526 ("Interrupt_Priority aspect cannot apply to "
2527 & "subprogram", Expr);
2528
2529 -- The expression must be static
2530
cda40848 2531 elsif not Is_OK_Static_Expression (Expr) then
d6814978 2532 Flag_Non_Static_Expr
2533 ("aspect requires static expression!", Expr);
2534
24d7b9d6 2535 -- Check whether this is the main subprogram. Issue a
2536 -- warning only if it is obviously not a main program
2537 -- (when it has parameters or when the subprogram is
2538 -- within a package).
2539
2540 elsif Present (Parameter_Specifications
2541 (Specification (N)))
2542 or else not Is_Compilation_Unit (Defining_Entity (N))
d6814978 2543 then
078a74b8 2544 -- See RM D.1(14/3) and D.16(12/3)
d6814978 2545
2546 Error_Msg_N
2547 ("aspect applied to subprogram other than the "
2548 & "main subprogram has no effect??", Expr);
2549
2550 -- Otherwise check in range and export the value
2551
2552 -- For the CPU aspect
2553
2554 elsif A_Id = Aspect_CPU then
2555 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2556
2557 -- Value is correct so we export the value to make
2558 -- it available at execution time.
2559
2560 Set_Main_CPU
2561 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2562
2563 else
2564 Error_Msg_N
2565 ("main subprogram CPU is out of range", Expr);
2566 end if;
2567
2568 -- For the Priority aspect
2569
2570 elsif A_Id = Aspect_Priority then
2571 if Is_In_Range (Expr, RTE (RE_Priority)) then
2572
2573 -- Value is correct so we export the value to make
2574 -- it available at execution time.
2575
2576 Set_Main_Priority
2577 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2578
32572384 2579 -- Ignore pragma if Relaxed_RM_Semantics to support
2580 -- other targets/non GNAT compilers.
2581
2582 elsif not Relaxed_RM_Semantics then
d6814978 2583 Error_Msg_N
2584 ("main subprogram priority is out of range",
2585 Expr);
2586 end if;
2587 end if;
2588
2589 -- Load an arbitrary entity from System.Tasking.Stages
2590 -- or System.Tasking.Restricted.Stages (depending on
2591 -- the supported profile) to make sure that one of these
2592 -- packages is implicitly with'ed, since we need to have
2593 -- the tasking run time active for the pragma Priority to
a0c3eeb9 2594 -- have any effect. Previously we with'ed the package
d6814978 2595 -- System.Tasking, but this package does not trigger the
2596 -- required initialization of the run-time library.
2597
2598 declare
2599 Discard : Entity_Id;
d6814978 2600 begin
2601 if Restricted_Profile then
2602 Discard := RTE (RE_Activate_Restricted_Tasks);
2603 else
2604 Discard := RTE (RE_Activate_Tasks);
2605 end if;
2606 end;
2607
e6ce0468 2608 -- Handling for these aspects in subprograms is complete
d6814978 2609
2610 goto Continue;
2611
fa65ad5e 2612 -- For task and protected types pass the aspect as an
2613 -- attribute.
0fd13d32 2614
3a72f9c3 2615 else
2616 Aitem :=
2617 Make_Attribute_Definition_Clause (Loc,
2618 Name => Ent,
2619 Chars => Chars (Id),
2620 Expression => Relocate_Node (Expr));
2621 end if;
2622
0fd13d32 2623 -- Warnings
2624
ae888dbd 2625 when Aspect_Warnings =>
0fd13d32 2626 Make_Aitem_Pragma
2627 (Pragma_Argument_Associations => New_List (
2628 Make_Pragma_Argument_Association (Sloc (Expr),
2629 Expression => Relocate_Node (Expr)),
2630 Make_Pragma_Argument_Association (Loc,
2631 Expression => New_Occurrence_Of (E, Loc))),
2632 Pragma_Name => Chars (Id));
ae888dbd 2633
2f06c88a 2634 Decorate (Aspect, Aitem);
2635 Insert_Pragma (Aitem);
2636 goto Continue;
2637
89f1e35c 2638 -- Case 2c: Aspects corresponding to pragmas with three
2639 -- arguments.
d64221a7 2640
89f1e35c 2641 -- Invariant aspects have a first argument that references the
2642 -- entity, a second argument that is the expression and a third
2643 -- argument that is an appropriate message.
d64221a7 2644
0fd13d32 2645 -- Invariant, Type_Invariant
2646
99378362 2647 when Aspect_Invariant
2648 | Aspect_Type_Invariant
2649 =>
89f1e35c 2650 -- Analysis of the pragma will verify placement legality:
2651 -- an invariant must apply to a private type, or appear in
2652 -- the private part of a spec and apply to a completion.
d64221a7 2653
0fd13d32 2654 Make_Aitem_Pragma
2655 (Pragma_Argument_Associations => New_List (
2656 Make_Pragma_Argument_Association (Sloc (Ent),
2657 Expression => Ent),
2658 Make_Pragma_Argument_Association (Sloc (Expr),
2659 Expression => Relocate_Node (Expr))),
2660 Pragma_Name => Name_Invariant);
89f1e35c 2661
2662 -- Add message unless exception messages are suppressed
2663
2664 if not Opt.Exception_Locations_Suppressed then
2665 Append_To (Pragma_Argument_Associations (Aitem),
2666 Make_Pragma_Argument_Association (Eloc,
2667 Chars => Name_Message,
2668 Expression =>
2669 Make_String_Literal (Eloc,
2670 Strval => "failed invariant from "
2671 & Build_Location_String (Eloc))));
d64221a7 2672 end if;
2673
89f1e35c 2674 -- For Invariant case, insert immediately after the entity
2675 -- declaration. We do not have to worry about delay issues
2676 -- since the pragma processing takes care of this.
2677
89f1e35c 2678 Delay_Required := False;
d64221a7 2679
47a46747 2680 -- Case 2d : Aspects that correspond to a pragma with one
2681 -- argument.
2682
0fd13d32 2683 -- Abstract_State
115f7b08 2684
d4e369ad 2685 -- Aspect Abstract_State introduces implicit declarations for
2686 -- all state abstraction entities it defines. To emulate this
2687 -- behavior, insert the pragma at the beginning of the visible
2688 -- declarations of the related package so that it is analyzed
2689 -- immediately.
2690
9129c28f 2691 when Aspect_Abstract_State => Abstract_State : declare
eb4f7efa 2692 Context : Node_Id := N;
9129c28f 2693
2694 begin
eb4f7efa 2695 -- When aspect Abstract_State appears on a generic package,
2696 -- it is propageted to the package instance. The context in
2697 -- this case is the instance spec.
2698
2699 if Nkind (Context) = N_Package_Instantiation then
2700 Context := Instance_Spec (Context);
2701 end if;
2702
2703 if Nkind_In (Context, N_Generic_Package_Declaration,
2704 N_Package_Declaration)
9129c28f 2705 then
9129c28f 2706 Make_Aitem_Pragma
2707 (Pragma_Argument_Associations => New_List (
2708 Make_Pragma_Argument_Association (Loc,
2709 Expression => Relocate_Node (Expr))),
2710 Pragma_Name => Name_Abstract_State);
630b6d55 2711
5655be8a 2712 Decorate (Aspect, Aitem);
2713 Insert_Pragma
2714 (Prag => Aitem,
2715 Is_Instance =>
2716 Is_Generic_Instance (Defining_Entity (Context)));
9129c28f 2717
2718 else
2719 Error_Msg_NE
2720 ("aspect & must apply to a package declaration",
2721 Aspect, Id);
2722 end if;
2723
2724 goto Continue;
2725 end Abstract_State;
115f7b08 2726
85ee12c0 2727 -- Aspect Async_Readers is never delayed because it is
2728 -- equivalent to a source pragma which appears after the
2729 -- related object declaration.
2730
2731 when Aspect_Async_Readers =>
2732 Make_Aitem_Pragma
2733 (Pragma_Argument_Associations => New_List (
2734 Make_Pragma_Argument_Association (Loc,
2735 Expression => Relocate_Node (Expr))),
2736 Pragma_Name => Name_Async_Readers);
2737
2738 Decorate (Aspect, Aitem);
2739 Insert_Pragma (Aitem);
2740 goto Continue;
2741
2742 -- Aspect Async_Writers is never delayed because it is
2743 -- equivalent to a source pragma which appears after the
2744 -- related object declaration.
2745
2746 when Aspect_Async_Writers =>
2747 Make_Aitem_Pragma
2748 (Pragma_Argument_Associations => New_List (
2749 Make_Pragma_Argument_Association (Loc,
2750 Expression => Relocate_Node (Expr))),
2751 Pragma_Name => Name_Async_Writers);
2752
2753 Decorate (Aspect, Aitem);
2754 Insert_Pragma (Aitem);
2755 goto Continue;
2756
d0849c23 2757 -- Aspect Constant_After_Elaboration is never delayed because
2758 -- it is equivalent to a source pragma which appears after the
2759 -- related object declaration.
2760
2761 when Aspect_Constant_After_Elaboration =>
2762 Make_Aitem_Pragma
2763 (Pragma_Argument_Associations => New_List (
2764 Make_Pragma_Argument_Association (Loc,
2765 Expression => Relocate_Node (Expr))),
2766 Pragma_Name =>
2767 Name_Constant_After_Elaboration);
2768
2769 Decorate (Aspect, Aitem);
2770 Insert_Pragma (Aitem);
2771 goto Continue;
2772
ec6f6da5 2773 -- Aspect Default_Internal_Condition is never delayed because
2774 -- it is equivalent to a source pragma which appears after the
2775 -- related private type. To deal with forward references, the
2776 -- generated pragma is stored in the rep chain of the related
2777 -- private type as types do not carry contracts. The pragma is
2778 -- wrapped inside of a procedure at the freeze point of the
2779 -- private type's full view.
2780
2781 when Aspect_Default_Initial_Condition =>
2782 Make_Aitem_Pragma
2783 (Pragma_Argument_Associations => New_List (
2784 Make_Pragma_Argument_Association (Loc,
2785 Expression => Relocate_Node (Expr))),
2786 Pragma_Name =>
2787 Name_Default_Initial_Condition);
2788
2789 Decorate (Aspect, Aitem);
2790 Insert_Pragma (Aitem);
2791 goto Continue;
2792
647fab54 2793 -- Default_Storage_Pool
2794
2795 when Aspect_Default_Storage_Pool =>
2796 Make_Aitem_Pragma
2797 (Pragma_Argument_Associations => New_List (
2798 Make_Pragma_Argument_Association (Loc,
2799 Expression => Relocate_Node (Expr))),
2800 Pragma_Name =>
2801 Name_Default_Storage_Pool);
2802
2803 Decorate (Aspect, Aitem);
2804 Insert_Pragma (Aitem);
2805 goto Continue;
2806
0fd13d32 2807 -- Depends
2808
e2bf777d 2809 -- Aspect Depends is never delayed because it is equivalent to
2810 -- a source pragma which appears after the related subprogram.
2811 -- To deal with forward references, the generated pragma is
2812 -- stored in the contract of the related subprogram and later
2813 -- analyzed at the end of the declarative region. See routine
2814 -- Analyze_Depends_In_Decl_Part for details.
6144c105 2815
12334c57 2816 when Aspect_Depends =>
0fd13d32 2817 Make_Aitem_Pragma
2818 (Pragma_Argument_Associations => New_List (
2819 Make_Pragma_Argument_Association (Loc,
2820 Expression => Relocate_Node (Expr))),
2821 Pragma_Name => Name_Depends);
2822
e2bf777d 2823 Decorate (Aspect, Aitem);
2824 Insert_Pragma (Aitem);
c1006d6d 2825 goto Continue;
2826
85ee12c0 2827 -- Aspect Effecitve_Reads is never delayed because it is
2828 -- equivalent to a source pragma which appears after the
2829 -- related object declaration.
2830
2831 when Aspect_Effective_Reads =>
2832 Make_Aitem_Pragma
2833 (Pragma_Argument_Associations => New_List (
2834 Make_Pragma_Argument_Association (Loc,
2835 Expression => Relocate_Node (Expr))),
2836 Pragma_Name => Name_Effective_Reads);
2837
2838 Decorate (Aspect, Aitem);
2839 Insert_Pragma (Aitem);
2840 goto Continue;
2841
2842 -- Aspect Effective_Writes is never delayed because it is
2843 -- equivalent to a source pragma which appears after the
2844 -- related object declaration.
2845
2846 when Aspect_Effective_Writes =>
2847 Make_Aitem_Pragma
2848 (Pragma_Argument_Associations => New_List (
2849 Make_Pragma_Argument_Association (Loc,
2850 Expression => Relocate_Node (Expr))),
2851 Pragma_Name => Name_Effective_Writes);
2852
2853 Decorate (Aspect, Aitem);
2854 Insert_Pragma (Aitem);
2855 goto Continue;
2856
cab27d2a 2857 -- Aspect Extensions_Visible is never delayed because it is
2858 -- equivalent to a source pragma which appears after the
2859 -- related subprogram.
2860
2861 when Aspect_Extensions_Visible =>
2862 Make_Aitem_Pragma
2863 (Pragma_Argument_Associations => New_List (
2864 Make_Pragma_Argument_Association (Loc,
2865 Expression => Relocate_Node (Expr))),
2866 Pragma_Name => Name_Extensions_Visible);
2867
2868 Decorate (Aspect, Aitem);
2869 Insert_Pragma (Aitem);
2870 goto Continue;
2871
3dbe7a69 2872 -- Aspect Ghost is never delayed because it is equivalent to a
2873 -- source pragma which appears at the top of [generic] package
2874 -- declarations or after an object, a [generic] subprogram, or
2875 -- a type declaration.
2876
5655be8a 2877 when Aspect_Ghost =>
3dbe7a69 2878 Make_Aitem_Pragma
2879 (Pragma_Argument_Associations => New_List (
2880 Make_Pragma_Argument_Association (Loc,
2881 Expression => Relocate_Node (Expr))),
2882 Pragma_Name => Name_Ghost);
2883
2884 Decorate (Aspect, Aitem);
5655be8a 2885 Insert_Pragma (Aitem);
3dbe7a69 2886 goto Continue;
3dbe7a69 2887
0fd13d32 2888 -- Global
12334c57 2889
e2bf777d 2890 -- Aspect Global is never delayed because it is equivalent to
2891 -- a source pragma which appears after the related subprogram.
2892 -- To deal with forward references, the generated pragma is
2893 -- stored in the contract of the related subprogram and later
2894 -- analyzed at the end of the declarative region. See routine
2895 -- Analyze_Global_In_Decl_Part for details.
3cdbaa5a 2896
2897 when Aspect_Global =>
0fd13d32 2898 Make_Aitem_Pragma
2899 (Pragma_Argument_Associations => New_List (
2900 Make_Pragma_Argument_Association (Loc,
2901 Expression => Relocate_Node (Expr))),
2902 Pragma_Name => Name_Global);
2903
e2bf777d 2904 Decorate (Aspect, Aitem);
2905 Insert_Pragma (Aitem);
c1006d6d 2906 goto Continue;
2907
9c138530 2908 -- Initial_Condition
2909
e2bf777d 2910 -- Aspect Initial_Condition is never delayed because it is
2911 -- equivalent to a source pragma which appears after the
2912 -- related package. To deal with forward references, the
2913 -- generated pragma is stored in the contract of the related
2914 -- package and later analyzed at the end of the declarative
2915 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2916 -- for details.
9c138530 2917
2918 when Aspect_Initial_Condition => Initial_Condition : declare
eb4f7efa 2919 Context : Node_Id := N;
9c138530 2920
2921 begin
e2bf777d 2922 -- When aspect Initial_Condition appears on a generic
2923 -- package, it is propageted to the package instance. The
2924 -- context in this case is the instance spec.
eb4f7efa 2925
2926 if Nkind (Context) = N_Package_Instantiation then
2927 Context := Instance_Spec (Context);
2928 end if;
2929
2930 if Nkind_In (Context, N_Generic_Package_Declaration,
2931 N_Package_Declaration)
9c138530 2932 then
9c138530 2933 Make_Aitem_Pragma
2934 (Pragma_Argument_Associations => New_List (
2935 Make_Pragma_Argument_Association (Loc,
2936 Expression => Relocate_Node (Expr))),
2937 Pragma_Name =>
2938 Name_Initial_Condition);
9c138530 2939
5655be8a 2940 Decorate (Aspect, Aitem);
2941 Insert_Pragma
2942 (Prag => Aitem,
2943 Is_Instance =>
2944 Is_Generic_Instance (Defining_Entity (Context)));
50e44732 2945
5655be8a 2946 -- Otherwise the context is illegal
9c138530 2947
2948 else
2949 Error_Msg_NE
2950 ("aspect & must apply to a package declaration",
2951 Aspect, Id);
2952 end if;
2953
2954 goto Continue;
2955 end Initial_Condition;
2956
d4e369ad 2957 -- Initializes
2958
e2bf777d 2959 -- Aspect Initializes is never delayed because it is equivalent
2960 -- to a source pragma appearing after the related package. To
2961 -- deal with forward references, the generated pragma is stored
2962 -- in the contract of the related package and later analyzed at
2963 -- the end of the declarative region. For details, see routine
2964 -- Analyze_Initializes_In_Decl_Part.
d4e369ad 2965
2966 when Aspect_Initializes => Initializes : declare
eb4f7efa 2967 Context : Node_Id := N;
d4e369ad 2968
2969 begin
50e44732 2970 -- When aspect Initializes appears on a generic package,
2971 -- it is propageted to the package instance. The context
2972 -- in this case is the instance spec.
eb4f7efa 2973
2974 if Nkind (Context) = N_Package_Instantiation then
2975 Context := Instance_Spec (Context);
2976 end if;
2977
2978 if Nkind_In (Context, N_Generic_Package_Declaration,
2979 N_Package_Declaration)
d4e369ad 2980 then
d4e369ad 2981 Make_Aitem_Pragma
2982 (Pragma_Argument_Associations => New_List (
2983 Make_Pragma_Argument_Association (Loc,
2984 Expression => Relocate_Node (Expr))),
2985 Pragma_Name => Name_Initializes);
d4e369ad 2986
5655be8a 2987 Decorate (Aspect, Aitem);
2988 Insert_Pragma
2989 (Prag => Aitem,
2990 Is_Instance =>
2991 Is_Generic_Instance (Defining_Entity (Context)));
50e44732 2992
5655be8a 2993 -- Otherwise the context is illegal
d4e369ad 2994
2995 else
2996 Error_Msg_NE
2997 ("aspect & must apply to a package declaration",
2998 Aspect, Id);
2999 end if;
3000
3001 goto Continue;
3002 end Initializes;
3003
cbd45084 3004 -- Max_Queue_Length
3005
3006 when Aspect_Max_Queue_Length =>
3007 Make_Aitem_Pragma
3008 (Pragma_Argument_Associations => New_List (
3009 Make_Pragma_Argument_Association (Loc,
3010 Expression => Relocate_Node (Expr))),
3011 Pragma_Name => Name_Max_Queue_Length);
3012
3013 Decorate (Aspect, Aitem);
3014 Insert_Pragma (Aitem);
3015 goto Continue;
3016
1fd4313f 3017 -- Obsolescent
3018
3019 when Aspect_Obsolescent => declare
3020 Args : List_Id;
3021
3022 begin
3023 if No (Expr) then
3024 Args := No_List;
3025 else
3026 Args := New_List (
3027 Make_Pragma_Argument_Association (Sloc (Expr),
3028 Expression => Relocate_Node (Expr)));
3029 end if;
3030
3031 Make_Aitem_Pragma
3032 (Pragma_Argument_Associations => Args,
3033 Pragma_Name => Chars (Id));
3034 end;
3035
5cc6f0cf 3036 -- Part_Of
3037
3038 when Aspect_Part_Of =>
3039 if Nkind_In (N, N_Object_Declaration,
3040 N_Package_Instantiation)
736b80cc 3041 or else Is_Single_Concurrent_Type_Declaration (N)
5cc6f0cf 3042 then
3043 Make_Aitem_Pragma
3044 (Pragma_Argument_Associations => New_List (
3045 Make_Pragma_Argument_Association (Loc,
3046 Expression => Relocate_Node (Expr))),
3047 Pragma_Name => Name_Part_Of);
3048
736b80cc 3049 Decorate (Aspect, Aitem);
3050 Insert_Pragma (Aitem);
736b80cc 3051
5cc6f0cf 3052 else
3053 Error_Msg_NE
736b80cc 3054 ("aspect & must apply to package instantiation, "
3055 & "object, single protected type or single task type",
3056 Aspect, Id);
5cc6f0cf 3057 end if;
3058
d5c65b80 3059 goto Continue;
3060
5dd93a61 3061 -- SPARK_Mode
3062
2f06c88a 3063 when Aspect_SPARK_Mode =>
5dd93a61 3064 Make_Aitem_Pragma
3065 (Pragma_Argument_Associations => New_List (
3066 Make_Pragma_Argument_Association (Loc,
3067 Expression => Relocate_Node (Expr))),
3068 Pragma_Name => Name_SPARK_Mode);
5dd93a61 3069
2f06c88a 3070 Decorate (Aspect, Aitem);
3071 Insert_Pragma (Aitem);
3072 goto Continue;
778ebf56 3073
4befb1a0 3074 -- Refined_Depends
3075
e2bf777d 3076 -- Aspect Refined_Depends is never delayed because it is
3077 -- equivalent to a source pragma which appears in the
3078 -- declarations of the related subprogram body. To deal with
3079 -- forward references, the generated pragma is stored in the
3080 -- contract of the related subprogram body and later analyzed
3081 -- at the end of the declarative region. For details, see
3082 -- routine Analyze_Refined_Depends_In_Decl_Part.
4befb1a0 3083
3084 when Aspect_Refined_Depends =>
422073ed 3085 Make_Aitem_Pragma
3086 (Pragma_Argument_Associations => New_List (
3087 Make_Pragma_Argument_Association (Loc,
3088 Expression => Relocate_Node (Expr))),
3089 Pragma_Name => Name_Refined_Depends);
3090
e2bf777d 3091 Decorate (Aspect, Aitem);
3092 Insert_Pragma (Aitem);
422073ed 3093 goto Continue;
4befb1a0 3094
3095 -- Refined_Global
3096
e2bf777d 3097 -- Aspect Refined_Global is never delayed because it is
3098 -- equivalent to a source pragma which appears in the
3099 -- declarations of the related subprogram body. To deal with
3100 -- forward references, the generated pragma is stored in the
3101 -- contract of the related subprogram body and later analyzed
3102 -- at the end of the declarative region. For details, see
3103 -- routine Analyze_Refined_Global_In_Decl_Part.
4befb1a0 3104
3105 when Aspect_Refined_Global =>
28ff117f 3106 Make_Aitem_Pragma
3107 (Pragma_Argument_Associations => New_List (
3108 Make_Pragma_Argument_Association (Loc,
3109 Expression => Relocate_Node (Expr))),
3110 Pragma_Name => Name_Refined_Global);
3111
e2bf777d 3112 Decorate (Aspect, Aitem);
3113 Insert_Pragma (Aitem);
28ff117f 3114 goto Continue;
4befb1a0 3115
63b65b2d 3116 -- Refined_Post
3117
3118 when Aspect_Refined_Post =>
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_Post);
3124
3ff5e35d 3125 Decorate (Aspect, Aitem);
3126 Insert_Pragma (Aitem);
3127 goto Continue;
3128
9129c28f 3129 -- Refined_State
3130
5655be8a 3131 when Aspect_Refined_State =>
9129c28f 3132
9129c28f 3133 -- The corresponding pragma for Refined_State is inserted in
3134 -- the declarations of the related package body. This action
3135 -- synchronizes both the source and from-aspect versions of
3136 -- the pragma.
3137
3138 if Nkind (N) = N_Package_Body then
9129c28f 3139 Make_Aitem_Pragma
3140 (Pragma_Argument_Associations => New_List (
3141 Make_Pragma_Argument_Association (Loc,
3142 Expression => Relocate_Node (Expr))),
3143 Pragma_Name => Name_Refined_State);
b9b2d6e5 3144
5655be8a 3145 Decorate (Aspect, Aitem);
3146 Insert_Pragma (Aitem);
b9b2d6e5 3147
5655be8a 3148 -- Otherwise the context is illegal
9129c28f 3149
3150 else
3151 Error_Msg_NE
3152 ("aspect & must apply to a package body", Aspect, Id);
3153 end if;
3154
3155 goto Continue;
9129c28f 3156
0fd13d32 3157 -- Relative_Deadline
3cdbaa5a 3158
3159 when Aspect_Relative_Deadline =>
0fd13d32 3160 Make_Aitem_Pragma
3161 (Pragma_Argument_Associations => New_List (
3162 Make_Pragma_Argument_Association (Loc,
3163 Expression => Relocate_Node (Expr))),
3164 Pragma_Name => Name_Relative_Deadline);
47a46747 3165
3166 -- If the aspect applies to a task, the corresponding pragma
3167 -- must appear within its declarations, not after.
3168
3169 if Nkind (N) = N_Task_Type_Declaration then
3170 declare
3171 Def : Node_Id;
3172 V : List_Id;
3173
3174 begin
3175 if No (Task_Definition (N)) then
3176 Set_Task_Definition (N,
3177 Make_Task_Definition (Loc,
3178 Visible_Declarations => New_List,
3179 End_Label => Empty));
3180 end if;
3181
3182 Def := Task_Definition (N);
3183 V := Visible_Declarations (Def);
3184 if not Is_Empty_List (V) then
3185 Insert_Before (First (V), Aitem);
3186
3187 else
3188 Set_Visible_Declarations (Def, New_List (Aitem));
3189 end if;
3190
3191 goto Continue;
3192 end;
3193 end if;
3194
85ee12c0 3195 -- Aspect Volatile_Function is never delayed because it is
3196 -- equivalent to a source pragma which appears after the
3197 -- related subprogram.
3198
3199 when Aspect_Volatile_Function =>
3200 Make_Aitem_Pragma
3201 (Pragma_Argument_Associations => New_List (
3202 Make_Pragma_Argument_Association (Loc,
3203 Expression => Relocate_Node (Expr))),
3204 Pragma_Name => Name_Volatile_Function);
3205
3206 Decorate (Aspect, Aitem);
3207 Insert_Pragma (Aitem);
3208 goto Continue;
3209
956ffaf4 3210 -- Case 2e: Annotate aspect
3211
3212 when Aspect_Annotate =>
3213 declare
3214 Args : List_Id;
3215 Pargs : List_Id;
3216 Arg : Node_Id;
3217
3218 begin
3219 -- The argument can be a single identifier
3220
3221 if Nkind (Expr) = N_Identifier then
3222
3223 -- One level of parens is allowed
3224
3225 if Paren_Count (Expr) > 1 then
3226 Error_Msg_F ("extra parentheses ignored", Expr);
3227 end if;
3228
3229 Set_Paren_Count (Expr, 0);
3230
3231 -- Add the single item to the list
3232
3233 Args := New_List (Expr);
3234
3235 -- Otherwise we must have an aggregate
3236
3237 elsif Nkind (Expr) = N_Aggregate then
3238
3239 -- Must be positional
3240
3241 if Present (Component_Associations (Expr)) then
3242 Error_Msg_F
3243 ("purely positional aggregate required", Expr);
3244 goto Continue;
3245 end if;
3246
3247 -- Must not be parenthesized
3248
3249 if Paren_Count (Expr) /= 0 then
3250 Error_Msg_F ("extra parentheses ignored", Expr);
3251 end if;
3252
3253 -- List of arguments is list of aggregate expressions
3254
3255 Args := Expressions (Expr);
3256
3257 -- Anything else is illegal
3258
3259 else
3260 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3261 goto Continue;
3262 end if;
3263
3264 -- Prepare pragma arguments
3265
3266 Pargs := New_List;
3267 Arg := First (Args);
3268 while Present (Arg) loop
3269 Append_To (Pargs,
3270 Make_Pragma_Argument_Association (Sloc (Arg),
3271 Expression => Relocate_Node (Arg)));
3272 Next (Arg);
3273 end loop;
3274
3275 Append_To (Pargs,
3276 Make_Pragma_Argument_Association (Sloc (Ent),
3277 Chars => Name_Entity,
3278 Expression => Ent));
3279
3280 Make_Aitem_Pragma
3281 (Pragma_Argument_Associations => Pargs,
3282 Pragma_Name => Name_Annotate);
3283 end;
3284
89f1e35c 3285 -- Case 3 : Aspects that don't correspond to pragma/attribute
3286 -- definition clause.
7b9b2f05 3287
89f1e35c 3288 -- Case 3a: The aspects listed below don't correspond to
3289 -- pragmas/attributes but do require delayed analysis.
7f694ca2 3290
51fa2a45 3291 -- Default_Value can only apply to a scalar type
3292
3293 when Aspect_Default_Value =>
3294 if not Is_Scalar_Type (E) then
3295 Error_Msg_N
1089ff19 3296 ("aspect Default_Value must apply to a scalar type", N);
51fa2a45 3297 end if;
3298
3299 Aitem := Empty;
3300
3301 -- Default_Component_Value can only apply to an array type
3302 -- with scalar components.
3303
3304 when Aspect_Default_Component_Value =>
3305 if not (Is_Array_Type (E)
3f4c9ffc 3306 and then Is_Scalar_Type (Component_Type (E)))
51fa2a45 3307 then
ee2b7923 3308 Error_Msg_N
3309 ("aspect Default_Component_Value can only apply to an "
3310 & "array of scalar components", N);
51fa2a45 3311 end if;
0fd13d32 3312
89f1e35c 3313 Aitem := Empty;
7f694ca2 3314
89f1e35c 3315 -- Case 3b: The aspects listed below don't correspond to
3316 -- pragmas/attributes and don't need delayed analysis.
95bc75fa 3317
0fd13d32 3318 -- Implicit_Dereference
3319
89f1e35c 3320 -- For Implicit_Dereference, External_Name and Link_Name, only
3321 -- the legality checks are done during the analysis, thus no
3322 -- delay is required.
a8e38e1d 3323
89f1e35c 3324 when Aspect_Implicit_Dereference =>
3325 Analyze_Aspect_Implicit_Dereference;
3326 goto Continue;
7f694ca2 3327
0fd13d32 3328 -- Dimension
3329
89f1e35c 3330 when Aspect_Dimension =>
3331 Analyze_Aspect_Dimension (N, Id, Expr);
3332 goto Continue;
cb4c311d 3333
0fd13d32 3334 -- Dimension_System
3335
89f1e35c 3336 when Aspect_Dimension_System =>
3337 Analyze_Aspect_Dimension_System (N, Id, Expr);
3338 goto Continue;
7f694ca2 3339
ceec4f7c 3340 -- Case 4: Aspects requiring special handling
51ea9c94 3341
e66f4e2a 3342 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3343 -- pragmas take care of the delay.
7f694ca2 3344
0fd13d32 3345 -- Pre/Post
3346
1e3c4ae6 3347 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3348 -- with a first argument that is the expression, and a second
3349 -- argument that is an informative message if the test fails.
3350 -- This is inserted right after the declaration, to get the
5b5df4a9 3351 -- required pragma placement. The processing for the pragmas
3352 -- takes care of the required delay.
ae888dbd 3353
5ddd846b 3354 when Pre_Post_Aspects => Pre_Post : declare
1e3c4ae6 3355 Pname : Name_Id;
ae888dbd 3356
1e3c4ae6 3357 begin
77ae6789 3358 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1e3c4ae6 3359 Pname := Name_Precondition;
3360 else
3361 Pname := Name_Postcondition;
3362 end if;
d74fc39a 3363
26062729 3364 -- Check that the class-wide predicate cannot be applied to
051826ee 3365 -- an operation of a synchronized type. AI12-0182 forbids
3366 -- these altogether, while earlier language semantics made
3367 -- them legal on tagged synchronized types.
3368
3369 -- Other legality checks are performed when analyzing the
3370 -- contract of the operation.
26062729 3371
3372 if Class_Present (Aspect)
3373 and then Is_Concurrent_Type (Current_Scope)
26062729 3374 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3375 then
3376 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3377 Error_Msg_N
3378 ("aspect % can only be specified for a primitive "
3379 & "operation of a tagged type", Aspect);
3380
3381 goto Continue;
3382 end if;
3383
1e3c4ae6 3384 -- If the expressions is of the form A and then B, then
3385 -- we generate separate Pre/Post aspects for the separate
3386 -- clauses. Since we allow multiple pragmas, there is no
3387 -- problem in allowing multiple Pre/Post aspects internally.
a273015d 3388 -- These should be treated in reverse order (B first and
3389 -- A second) since they are later inserted just after N in
3390 -- the order they are treated. This way, the pragma for A
3391 -- ends up preceding the pragma for B, which may have an
3392 -- importance for the error raised (either constraint error
3393 -- or precondition error).
1e3c4ae6 3394
39e1f22f 3395 -- We do not do this for Pre'Class, since we have to put
51fa2a45 3396 -- these conditions together in a complex OR expression.
ae888dbd 3397
4282d342 3398 -- We do not do this in ASIS mode, as ASIS relies on the
3399 -- original node representing the complete expression, when
3400 -- retrieving it through the source aspect table.
3401
3402 if not ASIS_Mode
3403 and then (Pname = Name_Postcondition
3404 or else not Class_Present (Aspect))
39e1f22f 3405 then
3406 while Nkind (Expr) = N_And_Then loop
3407 Insert_After (Aspect,
a273015d 3408 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
39e1f22f 3409 Identifier => Identifier (Aspect),
a273015d 3410 Expression => Relocate_Node (Left_Opnd (Expr)),
39e1f22f 3411 Class_Present => Class_Present (Aspect),
3412 Split_PPC => True));
a273015d 3413 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
39e1f22f 3414 Eloc := Sloc (Expr);
3415 end loop;
3416 end if;
ae888dbd 3417
48d6f069 3418 -- Build the precondition/postcondition pragma
3419
51fa2a45 3420 -- Add note about why we do NOT need Copy_Tree here???
d74fc39a 3421
0fd13d32 3422 Make_Aitem_Pragma
3423 (Pragma_Argument_Associations => New_List (
3424 Make_Pragma_Argument_Association (Eloc,
3425 Chars => Name_Check,
a19e1763 3426 Expression => Relocate_Node (Expr))),
0fd13d32 3427 Pragma_Name => Pname);
39e1f22f 3428
3429 -- Add message unless exception messages are suppressed
3430
3431 if not Opt.Exception_Locations_Suppressed then
3432 Append_To (Pragma_Argument_Associations (Aitem),
3433 Make_Pragma_Argument_Association (Eloc,
ed695684 3434 Chars => Name_Message,
39e1f22f 3435 Expression =>
3436 Make_String_Literal (Eloc,
3437 Strval => "failed "
3438 & Get_Name_String (Pname)
3439 & " from "
3440 & Build_Location_String (Eloc))));
3441 end if;
d74fc39a 3442
7d20685d 3443 Set_Is_Delayed_Aspect (Aspect);
d74fc39a 3444
1e3c4ae6 3445 -- For Pre/Post cases, insert immediately after the entity
3446 -- declaration, since that is the required pragma placement.
3447 -- Note that for these aspects, we do not have to worry
3448 -- about delay issues, since the pragmas themselves deal
3449 -- with delay of visibility for the expression analysis.
3450
e2bf777d 3451 Insert_Pragma (Aitem);
299b347e 3452
1e3c4ae6 3453 goto Continue;
5ddd846b 3454 end Pre_Post;
ae888dbd 3455
0fd13d32 3456 -- Test_Case
3457
e66f4e2a 3458 when Aspect_Test_Case => Test_Case : declare
3459 Args : List_Id;
3460 Comp_Expr : Node_Id;
3461 Comp_Assn : Node_Id;
3462 New_Expr : Node_Id;
57cd943b 3463
e66f4e2a 3464 begin
3465 Args := New_List;
b0bc40fd 3466
e66f4e2a 3467 if Nkind (Parent (N)) = N_Compilation_Unit then
3468 Error_Msg_Name_1 := Nam;
3469 Error_Msg_N ("incorrect placement of aspect `%`", E);
3470 goto Continue;
3471 end if;
6c545057 3472
e66f4e2a 3473 if Nkind (Expr) /= N_Aggregate then
3474 Error_Msg_Name_1 := Nam;
3475 Error_Msg_NE
3476 ("wrong syntax for aspect `%` for &", Id, E);
3477 goto Continue;
3478 end if;
6c545057 3479
e66f4e2a 3480 -- Make pragma expressions refer to the original aspect
51fa2a45 3481 -- expressions through the Original_Node link. This is used
3482 -- in semantic analysis for ASIS mode, so that the original
3483 -- expression also gets analyzed.
e66f4e2a 3484
3485 Comp_Expr := First (Expressions (Expr));
3486 while Present (Comp_Expr) loop
3487 New_Expr := Relocate_Node (Comp_Expr);
e66f4e2a 3488 Append_To (Args,
3489 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3490 Expression => New_Expr));
3491 Next (Comp_Expr);
3492 end loop;
3493
3494 Comp_Assn := First (Component_Associations (Expr));
3495 while Present (Comp_Assn) loop
3496 if List_Length (Choices (Comp_Assn)) /= 1
3497 or else
3498 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3499 then
fad014fe 3500 Error_Msg_Name_1 := Nam;
6c545057 3501 Error_Msg_NE
fad014fe 3502 ("wrong syntax for aspect `%` for &", Id, E);
6c545057 3503 goto Continue;
3504 end if;
3505
e66f4e2a 3506 Append_To (Args,
3507 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
ed695684 3508 Chars => Chars (First (Choices (Comp_Assn))),
3509 Expression =>
3510 Relocate_Node (Expression (Comp_Assn))));
e66f4e2a 3511 Next (Comp_Assn);
3512 end loop;
6c545057 3513
e66f4e2a 3514 -- Build the test-case pragma
6c545057 3515
0fd13d32 3516 Make_Aitem_Pragma
3517 (Pragma_Argument_Associations => Args,
3518 Pragma_Name => Nam);
e66f4e2a 3519 end Test_Case;
85696508 3520
0fd13d32 3521 -- Contract_Cases
3522
5ddd846b 3523 when Aspect_Contract_Cases =>
0fd13d32 3524 Make_Aitem_Pragma
3525 (Pragma_Argument_Associations => New_List (
3526 Make_Pragma_Argument_Association (Loc,
3527 Expression => Relocate_Node (Expr))),
3528 Pragma_Name => Nam);
3a128918 3529
e2bf777d 3530 Decorate (Aspect, Aitem);
3531 Insert_Pragma (Aitem);
5ddd846b 3532 goto Continue;
3a128918 3533
89f1e35c 3534 -- Case 5: Special handling for aspects with an optional
3535 -- boolean argument.
85696508 3536
6c5793cd 3537 -- In the delayed case, the corresponding pragma cannot be
0fd13d32 3538 -- generated yet because the evaluation of the boolean needs
3539 -- to be delayed till the freeze point.
3540
99378362 3541 when Boolean_Aspects
3542 | Library_Unit_Aspects
3543 =>
89f1e35c 3544 Set_Is_Boolean_Aspect (Aspect);
a5a64273 3545
89f1e35c 3546 -- Lock_Free aspect only apply to protected objects
e1cedbae 3547
89f1e35c 3548 if A_Id = Aspect_Lock_Free then
3549 if Ekind (E) /= E_Protected_Type then
99a2d5bd 3550 Error_Msg_Name_1 := Nam;
a5a64273 3551 Error_Msg_N
89f1e35c 3552 ("aspect % only applies to a protected object",
3553 Aspect);
3554
3555 else
3556 -- Set the Uses_Lock_Free flag to True if there is no
37c6e44c 3557 -- expression or if the expression is True. The
89f1e35c 3558 -- evaluation of this aspect should be delayed to the
37c6e44c 3559 -- freeze point (why???)
89f1e35c 3560
e81df51c 3561 if No (Expr)
3562 or else Is_True (Static_Boolean (Expr))
89f1e35c 3563 then
3564 Set_Uses_Lock_Free (E);
3565 end if;
caf125ce 3566
3567 Record_Rep_Item (E, Aspect);
a5a64273 3568 end if;
e1cedbae 3569
89f1e35c 3570 goto Continue;
ae888dbd 3571
ee2b7923 3572 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3573 Analyze_Aspect_Export_Import;
6c5793cd 3574
3575 -- Disable_Controlled
3576
3577 elsif A_Id = Aspect_Disable_Controlled then
0b10029c 3578 Analyze_Aspect_Disable_Controlled;
89f1e35c 3579 goto Continue;
3580 end if;
d74fc39a 3581
37c6e44c 3582 -- Library unit aspects require special handling in the case
3583 -- of a package declaration, the pragma needs to be inserted
3584 -- in the list of declarations for the associated package.
3585 -- There is no issue of visibility delay for these aspects.
d64221a7 3586
89f1e35c 3587 if A_Id in Library_Unit_Aspects
178fec9b 3588 and then
3589 Nkind_In (N, N_Package_Declaration,
3590 N_Generic_Package_Declaration)
89f1e35c 3591 and then Nkind (Parent (N)) /= N_Compilation_Unit
3ad60f63 3592
3593 -- Aspect is legal on a local instantiation of a library-
3594 -- level generic unit.
3595
b94a633e 3596 and then not Is_Generic_Instance (Defining_Entity (N))
89f1e35c 3597 then
3598 Error_Msg_N
dd4c44af 3599 ("incorrect context for library unit aspect&", Id);
89f1e35c 3600 goto Continue;
3601 end if;
cce84b09 3602
51fa2a45 3603 -- Cases where we do not delay, includes all cases where the
3604 -- expression is missing other than the above cases.
d74fc39a 3605
85ee12c0 3606 if not Delay_Required or else No (Expr) then
ee2b7923 3607
3608 -- Exclude aspects Export and Import because their pragma
3609 -- syntax does not map directly to a Boolean aspect.
3610
3611 if A_Id /= Aspect_Export
3612 and then A_Id /= Aspect_Import
3613 then
3614 Make_Aitem_Pragma
3615 (Pragma_Argument_Associations => New_List (
3616 Make_Pragma_Argument_Association (Sloc (Ent),
3617 Expression => Ent)),
3618 Pragma_Name => Chars (Id));
3619 end if;
3620
89f1e35c 3621 Delay_Required := False;
ddf1337b 3622
89f1e35c 3623 -- In general cases, the corresponding pragma/attribute
3624 -- definition clause will be inserted later at the freezing
294709fa 3625 -- point, and we do not need to build it now.
ddf1337b 3626
89f1e35c 3627 else
3628 Aitem := Empty;
3629 end if;
ceec4f7c 3630
3631 -- Storage_Size
3632
3633 -- This is special because for access types we need to generate
3634 -- an attribute definition clause. This also works for single
3635 -- task declarations, but it does not work for task type
3636 -- declarations, because we have the case where the expression
3637 -- references a discriminant of the task type. That can't use
3638 -- an attribute definition clause because we would not have
3639 -- visibility on the discriminant. For that case we must
3640 -- generate a pragma in the task definition.
3641
3642 when Aspect_Storage_Size =>
3643
3644 -- Task type case
3645
3646 if Ekind (E) = E_Task_Type then
3647 declare
3648 Decl : constant Node_Id := Declaration_Node (E);
3649
3650 begin
3651 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3652
3653 -- If no task definition, create one
3654
3655 if No (Task_Definition (Decl)) then
3656 Set_Task_Definition (Decl,
3657 Make_Task_Definition (Loc,
3658 Visible_Declarations => Empty_List,
3659 End_Label => Empty));
3660 end if;
3661
51fa2a45 3662 -- Create a pragma and put it at the start of the task
3663 -- definition for the task type declaration.
ceec4f7c 3664
3665 Make_Aitem_Pragma
3666 (Pragma_Argument_Associations => New_List (
3667 Make_Pragma_Argument_Association (Loc,
3668 Expression => Relocate_Node (Expr))),
3669 Pragma_Name => Name_Storage_Size);
3670
3671 Prepend
3672 (Aitem,
3673 Visible_Declarations (Task_Definition (Decl)));
3674 goto Continue;
3675 end;
3676
3677 -- All other cases, generate attribute definition
3678
3679 else
3680 Aitem :=
3681 Make_Attribute_Definition_Clause (Loc,
3682 Name => Ent,
3683 Chars => Chars (Id),
3684 Expression => Relocate_Node (Expr));
3685 end if;
89f1e35c 3686 end case;
ddf1337b 3687
89f1e35c 3688 -- Attach the corresponding pragma/attribute definition clause to
3689 -- the aspect specification node.
d74fc39a 3690
89f1e35c 3691 if Present (Aitem) then
e2bf777d 3692 Set_From_Aspect_Specification (Aitem);
89f1e35c 3693 end if;
53c179ea 3694
89f1e35c 3695 -- In the context of a compilation unit, we directly put the
0fd13d32 3696 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3697 -- node (no delay is required here) except for aspects on a
51fa2a45 3698 -- subprogram body (see below) and a generic package, for which we
3699 -- need to introduce the pragma before building the generic copy
3700 -- (see sem_ch12), and for package instantiations, where the
3701 -- library unit pragmas are better handled early.
ddf1337b 3702
9129c28f 3703 if Nkind (Parent (N)) = N_Compilation_Unit
89f1e35c 3704 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3705 then
3706 declare
3707 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
7f694ca2 3708
89f1e35c 3709 begin
3710 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
7f694ca2 3711
89f1e35c 3712 -- For a Boolean aspect, create the corresponding pragma if
3713 -- no expression or if the value is True.
7f694ca2 3714
b9e61b2a 3715 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
89f1e35c 3716 if Is_True (Static_Boolean (Expr)) then
0fd13d32 3717 Make_Aitem_Pragma
3718 (Pragma_Argument_Associations => New_List (
3719 Make_Pragma_Argument_Association (Sloc (Ent),
3720 Expression => Ent)),
3721 Pragma_Name => Chars (Id));
7f694ca2 3722
89f1e35c 3723 Set_From_Aspect_Specification (Aitem, True);
3724 Set_Corresponding_Aspect (Aitem, Aspect);
3725
3726 else
3727 goto Continue;
3728 end if;
3729 end if;
7f694ca2 3730
d6814978 3731 -- If the aspect is on a subprogram body (relevant aspect
3732 -- is Inline), add the pragma in front of the declarations.
3a72f9c3 3733
3734 if Nkind (N) = N_Subprogram_Body then
3735 if No (Declarations (N)) then
3736 Set_Declarations (N, New_List);
3737 end if;
3738
3739 Prepend (Aitem, Declarations (N));
3740
178fec9b 3741 elsif Nkind (N) = N_Generic_Package_Declaration then
3742 if No (Visible_Declarations (Specification (N))) then
3743 Set_Visible_Declarations (Specification (N), New_List);
3744 end if;
3745
3746 Prepend (Aitem,
3747 Visible_Declarations (Specification (N)));
3748
c39cce40 3749 elsif Nkind (N) = N_Package_Instantiation then
df8b0dae 3750 declare
3751 Spec : constant Node_Id :=
3752 Specification (Instance_Spec (N));
3753 begin
3754 if No (Visible_Declarations (Spec)) then
3755 Set_Visible_Declarations (Spec, New_List);
3756 end if;
3757
3758 Prepend (Aitem, Visible_Declarations (Spec));
3759 end;
3760
3a72f9c3 3761 else
3762 if No (Pragmas_After (Aux)) then
d4596fbe 3763 Set_Pragmas_After (Aux, New_List);
3a72f9c3 3764 end if;
3765
3766 Append (Aitem, Pragmas_After (Aux));
89f1e35c 3767 end if;
7f694ca2 3768
89f1e35c 3769 goto Continue;
3770 end;
3771 end if;
7f694ca2 3772
89f1e35c 3773 -- The evaluation of the aspect is delayed to the freezing point.
3774 -- The pragma or attribute clause if there is one is then attached
37c6e44c 3775 -- to the aspect specification which is put in the rep item list.
1a814552 3776
89f1e35c 3777 if Delay_Required then
3778 if Present (Aitem) then
3779 Set_Is_Delayed_Aspect (Aitem);
3780 Set_Aspect_Rep_Item (Aspect, Aitem);
3781 Set_Parent (Aitem, Aspect);
3782 end if;
1a814552 3783
89f1e35c 3784 Set_Is_Delayed_Aspect (Aspect);
9f36e3fb 3785
cba2ae82 3786 -- In the case of Default_Value, link the aspect to base type
3787 -- as well, even though it appears on a first subtype. This is
3788 -- mandated by the semantics of the aspect. Do not establish
3789 -- the link when processing the base type itself as this leads
3790 -- to a rep item circularity. Verify that we are dealing with
3791 -- a scalar type to prevent cascaded errors.
3792
3793 if A_Id = Aspect_Default_Value
3794 and then Is_Scalar_Type (E)
3795 and then Base_Type (E) /= E
3796 then
9f36e3fb 3797 Set_Has_Delayed_Aspects (Base_Type (E));
3798 Record_Rep_Item (Base_Type (E), Aspect);
3799 end if;
3800
89f1e35c 3801 Set_Has_Delayed_Aspects (E);
3802 Record_Rep_Item (E, Aspect);
ddf1337b 3803
b855559d 3804 -- When delay is not required and the context is a package or a
3805 -- subprogram body, insert the pragma in the body declarations.
f55ce169 3806
b855559d 3807 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
f55ce169 3808 if No (Declarations (N)) then
3809 Set_Declarations (N, New_List);
3810 end if;
3811
3812 -- The pragma is added before source declarations
3813
3814 Prepend_To (Declarations (N), Aitem);
3815
89f1e35c 3816 -- When delay is not required and the context is not a compilation
3817 -- unit, we simply insert the pragma/attribute definition clause
3818 -- in sequence.
ddf1337b 3819
ee2b7923 3820 elsif Present (Aitem) then
89f1e35c 3821 Insert_After (Ins_Node, Aitem);
3822 Ins_Node := Aitem;
d74fc39a 3823 end if;
0fd13d32 3824 end Analyze_One_Aspect;
ae888dbd 3825
d64221a7 3826 <<Continue>>
3827 Next (Aspect);
21ea3a4f 3828 end loop Aspect_Loop;
89f1e35c 3829
3830 if Has_Delayed_Aspects (E) then
3831 Ensure_Freeze_Node (E);
3832 end if;
21ea3a4f 3833 end Analyze_Aspect_Specifications;
ae888dbd 3834
eb8aeefc 3835 ---------------------------------------------------
3836 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3837 ---------------------------------------------------
3838
3839 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3840 Body_Id : constant Entity_Id := Defining_Entity (N);
3841
3842 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
c02dccca 3843 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3844 -- error message depending on the aspects involved. Spec_Id denotes the
3845 -- entity of the corresponding spec.
eb8aeefc 3846
3847 --------------------------------
3848 -- Diagnose_Misplaced_Aspects --
3849 --------------------------------
3850
3851 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3852 procedure Misplaced_Aspect_Error
3853 (Asp : Node_Id;
3854 Ref_Nam : Name_Id);
3855 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3856 -- the name of the refined version of the aspect.
3857
3858 ----------------------------
3859 -- Misplaced_Aspect_Error --
3860 ----------------------------
3861
3862 procedure Misplaced_Aspect_Error
3863 (Asp : Node_Id;
3864 Ref_Nam : Name_Id)
3865 is
3866 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3867 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3868
3869 begin
3870 -- The corresponding spec already contains the aspect in question
3871 -- and the one appearing on the body must be the refined form:
3872
3873 -- procedure P with Global ...;
3874 -- procedure P with Global ... is ... end P;
3875 -- ^
3876 -- Refined_Global
3877
3878 if Has_Aspect (Spec_Id, Asp_Id) then
3879 Error_Msg_Name_1 := Asp_Nam;
3880
3881 -- Subunits cannot carry aspects that apply to a subprogram
3882 -- declaration.
3883
3884 if Nkind (Parent (N)) = N_Subunit then
3885 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3886
3887 -- Otherwise suggest the refined form
3888
3889 else
3890 Error_Msg_Name_2 := Ref_Nam;
3891 Error_Msg_N ("aspect % should be %", Asp);
3892 end if;
3893
3894 -- Otherwise the aspect must appear on the spec, not on the body
3895
3896 -- procedure P;
3897 -- procedure P with Global ... is ... end P;
3898
3899 else
3900 Error_Msg_N
c02dccca 3901 ("aspect specification must appear on initial declaration",
eb8aeefc 3902 Asp);
3903 end if;
3904 end Misplaced_Aspect_Error;
3905
3906 -- Local variables
3907
3908 Asp : Node_Id;
3909 Asp_Nam : Name_Id;
3910
3911 -- Start of processing for Diagnose_Misplaced_Aspects
3912
3913 begin
3914 -- Iterate over the aspect specifications and emit specific errors
3915 -- where applicable.
3916
3917 Asp := First (Aspect_Specifications (N));
3918 while Present (Asp) loop
3919 Asp_Nam := Chars (Identifier (Asp));
3920
3921 -- Do not emit errors on aspects that can appear on a subprogram
3922 -- body. This scenario occurs when the aspect specification list
3923 -- contains both misplaced and properly placed aspects.
3924
3925 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3926 null;
3927
3928 -- Special diagnostics for SPARK aspects
3929
3930 elsif Asp_Nam = Name_Depends then
3931 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3932
3933 elsif Asp_Nam = Name_Global then
3934 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3935
3936 elsif Asp_Nam = Name_Post then
3937 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3938
3939 -- Otherwise a language-defined aspect is misplaced
3940
3941 else
3942 Error_Msg_N
c02dccca 3943 ("aspect specification must appear on initial declaration",
eb8aeefc 3944 Asp);
3945 end if;
3946
3947 Next (Asp);
3948 end loop;
3949 end Diagnose_Misplaced_Aspects;
3950
3951 -- Local variables
3952
c02dccca 3953 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
eb8aeefc 3954
3955 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3956
3957 begin
eb8aeefc 3958 -- Language-defined aspects cannot be associated with a subprogram body
3959 -- [stub] if the subprogram has a spec. Certain implementation defined
3960 -- aspects are allowed to break this rule (for all applicable cases, see
3961 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3962
c02dccca 3963 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
eb8aeefc 3964 Diagnose_Misplaced_Aspects (Spec_Id);
3965 else
3966 Analyze_Aspect_Specifications (N, Body_Id);
3967 end if;
3968 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3969
d6f39728 3970 -----------------------
3971 -- Analyze_At_Clause --
3972 -----------------------
3973
3974 -- An at clause is replaced by the corresponding Address attribute
3975 -- definition clause that is the preferred approach in Ada 95.
3976
3977 procedure Analyze_At_Clause (N : Node_Id) is
177675a7 3978 CS : constant Boolean := Comes_From_Source (N);
3979
d6f39728 3980 begin
177675a7 3981 -- This is an obsolescent feature
3982
e0521a36 3983 Check_Restriction (No_Obsolescent_Features, N);
3984
9dfe12ae 3985 if Warn_On_Obsolescent_Feature then
3986 Error_Msg_N
b174444e 3987 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
9dfe12ae 3988 Error_Msg_N
b174444e 3989 ("\?j?use address attribute definition clause instead", N);
9dfe12ae 3990 end if;
3991
177675a7 3992 -- Rewrite as address clause
3993
d6f39728 3994 Rewrite (N,
3995 Make_Attribute_Definition_Clause (Sloc (N),
935e86e0 3996 Name => Identifier (N),
3997 Chars => Name_Address,
d6f39728 3998 Expression => Expression (N)));
177675a7 3999
2beb22b1 4000 -- We preserve Comes_From_Source, since logically the clause still comes
4001 -- from the source program even though it is changed in form.
177675a7 4002
4003 Set_Comes_From_Source (N, CS);
4004
4005 -- Analyze rewritten clause
4006
d6f39728 4007 Analyze_Attribute_Definition_Clause (N);
4008 end Analyze_At_Clause;
4009
4010 -----------------------------------------
4011 -- Analyze_Attribute_Definition_Clause --
4012 -----------------------------------------
4013
4014 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
4015 Loc : constant Source_Ptr := Sloc (N);
4016 Nam : constant Node_Id := Name (N);
4017 Attr : constant Name_Id := Chars (N);
4018 Expr : constant Node_Id := Expression (N);
4019 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
d64221a7 4020
4021 Ent : Entity_Id;
4022 -- The entity of Nam after it is analyzed. In the case of an incomplete
4023 -- type, this is the underlying type.
4024
d6f39728 4025 U_Ent : Entity_Id;
d64221a7 4026 -- The underlying entity to which the attribute applies. Generally this
4027 -- is the Underlying_Type of Ent, except in the case where the clause
69069c76 4028 -- applies to the full view of an incomplete or private type, in which
4029 -- case U_Ent is just a copy of Ent.
d6f39728 4030
4031 FOnly : Boolean := False;
4032 -- Reset to True for subtype specific attribute (Alignment, Size)
51fa2a45 4033 -- and for stream attributes, i.e. those cases where in the call to
4034 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
4035 -- are checked. Note that the case of stream attributes is not clear
4036 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
4037 -- Storage_Size for derived task types, but that is also clearly
4038 -- unintentional.
d6f39728 4039
9f373bb8 4040 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
4041 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
4042 -- definition clauses.
4043
ae888dbd 4044 function Duplicate_Clause return Boolean;
4045 -- This routine checks if the aspect for U_Ent being given by attribute
4046 -- definition clause N is for an aspect that has already been specified,
4047 -- and if so gives an error message. If there is a duplicate, True is
4048 -- returned, otherwise if there is no error, False is returned.
4049
81b424ac 4050 procedure Check_Indexing_Functions;
4051 -- Check that the function in Constant_Indexing or Variable_Indexing
4052 -- attribute has the proper type structure. If the name is overloaded,
cac18f71 4053 -- check that some interpretation is legal.
81b424ac 4054
89cc7147 4055 procedure Check_Iterator_Functions;
4056 -- Check that there is a single function in Default_Iterator attribute
58a61b0f 4057 -- that has the proper type structure.
89cc7147 4058
4059 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
d03bfaa1 4060 -- Common legality check for the previous two
89cc7147 4061
177675a7 4062 -----------------------------------
4063 -- Analyze_Stream_TSS_Definition --
4064 -----------------------------------
4065
9f373bb8 4066 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
4067 Subp : Entity_Id := Empty;
4068 I : Interp_Index;
4069 It : Interp;
4070 Pnam : Entity_Id;
4071
4072 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
ba662f09 4073 -- True for Read attribute, False for other attributes
9f373bb8 4074
c41e404d 4075 function Has_Good_Profile
4076 (Subp : Entity_Id;
4077 Report : Boolean := False) return Boolean;
9f373bb8 4078 -- Return true if the entity is a subprogram with an appropriate
ba662f09 4079 -- profile for the attribute being defined. If result is False and
4080 -- Report is True, function emits appropriate error.
9f373bb8 4081
4082 ----------------------
4083 -- Has_Good_Profile --
4084 ----------------------
4085
c41e404d 4086 function Has_Good_Profile
4087 (Subp : Entity_Id;
4088 Report : Boolean := False) return Boolean
4089 is
9f373bb8 4090 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
4091 (False => E_Procedure, True => E_Function);
4a83cc35 4092 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
4093 F : Entity_Id;
9f373bb8 4094 Typ : Entity_Id;
4095
4096 begin
4097 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
4098 return False;
4099 end if;
4100
4101 F := First_Formal (Subp);
4102
4103 if No (F)
4104 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
4105 or else Designated_Type (Etype (F)) /=
4a83cc35 4106 Class_Wide_Type (RTE (RE_Root_Stream_Type))
9f373bb8 4107 then
4108 return False;
4109 end if;
4110
4111 if not Is_Function then
4112 Next_Formal (F);
4113
4114 declare
4115 Expected_Mode : constant array (Boolean) of Entity_Kind :=
4116 (False => E_In_Parameter,
4117 True => E_Out_Parameter);
4118 begin
4119 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
4120 return False;
4121 end if;
4122 end;
4123
4124 Typ := Etype (F);
4125
b64082f2 4126 -- If the attribute specification comes from an aspect
51fa2a45 4127 -- specification for a class-wide stream, the parameter must be
4128 -- a class-wide type of the entity to which the aspect applies.
b64082f2 4129
4130 if From_Aspect_Specification (N)
4131 and then Class_Present (Parent (N))
4132 and then Is_Class_Wide_Type (Typ)
4133 then
4134 Typ := Etype (Typ);
4135 end if;
4136
9f373bb8 4137 else
4138 Typ := Etype (Subp);
4139 end if;
4140
51fa2a45 4141 -- Verify that the prefix of the attribute and the local name for
5a8fe506 4142 -- the type of the formal match, or one is the class-wide of the
4143 -- other, in the case of a class-wide stream operation.
48680a09 4144
b8eacb12 4145 if Base_Type (Typ) = Base_Type (Ent)
5a8fe506 4146 or else (Is_Class_Wide_Type (Typ)
2be1f7d7 4147 and then Typ = Class_Wide_Type (Base_Type (Ent)))
fbf4d6ef 4148 or else (Is_Class_Wide_Type (Ent)
4149 and then Ent = Class_Wide_Type (Base_Type (Typ)))
5a8fe506 4150 then
4151 null;
4152 else
4153 return False;
4154 end if;
4155
4a83cc35 4156 if Present (Next_Formal (F)) then
48680a09 4157 return False;
4158
4159 elsif not Is_Scalar_Type (Typ)
4160 and then not Is_First_Subtype (Typ)
4161 and then not Is_Class_Wide_Type (Typ)
4162 then
c41e404d 4163 if Report and not Is_First_Subtype (Typ) then
4164 Error_Msg_N
ba662f09 4165 ("subtype of formal in stream operation must be a first "
4166 & "subtype", Parameter_Type (Parent (F)));
c41e404d 4167 end if;
4168
48680a09 4169 return False;
4170
4171 else
4172 return True;
4173 end if;
9f373bb8 4174 end Has_Good_Profile;
4175
4176 -- Start of processing for Analyze_Stream_TSS_Definition
4177
4178 begin
4179 FOnly := True;
4180
4181 if not Is_Type (U_Ent) then
4182 Error_Msg_N ("local name must be a subtype", Nam);
4183 return;
48680a09 4184
4185 elsif not Is_First_Subtype (U_Ent) then
4186 Error_Msg_N ("local name must be a first subtype", Nam);
4187 return;
9f373bb8 4188 end if;
4189
4190 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
4191
44e4341e 4192 -- If Pnam is present, it can be either inherited from an ancestor
4193 -- type (in which case it is legal to redefine it for this type), or
4194 -- be a previous definition of the attribute for the same type (in
4195 -- which case it is illegal).
4196
4197 -- In the first case, it will have been analyzed already, and we
4198 -- can check that its profile does not match the expected profile
4199 -- for a stream attribute of U_Ent. In the second case, either Pnam
4200 -- has been analyzed (and has the expected profile), or it has not
4201 -- been analyzed yet (case of a type that has not been frozen yet
4202 -- and for which the stream attribute has been set using Set_TSS).
4203
4204 if Present (Pnam)
4205 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4206 then
9f373bb8 4207 Error_Msg_Sloc := Sloc (Pnam);
4208 Error_Msg_Name_1 := Attr;
4209 Error_Msg_N ("% attribute already defined #", Nam);
4210 return;
4211 end if;
4212
4213 Analyze (Expr);
4214
4215 if Is_Entity_Name (Expr) then
4216 if not Is_Overloaded (Expr) then
c41e404d 4217 if Has_Good_Profile (Entity (Expr), Report => True) then
9f373bb8 4218 Subp := Entity (Expr);
4219 end if;
4220
4221 else
4222 Get_First_Interp (Expr, I, It);
9f373bb8 4223 while Present (It.Nam) loop
4224 if Has_Good_Profile (It.Nam) then
4225 Subp := It.Nam;
4226 exit;
4227 end if;
4228
4229 Get_Next_Interp (I, It);
4230 end loop;
4231 end if;
4232 end if;
4233
4234 if Present (Subp) then
59ac57b5 4235 if Is_Abstract_Subprogram (Subp) then
9f373bb8 4236 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4237 return;
e12b2502 4238
299b347e 4239 -- A stream subprogram for an interface type must be a null
bfbd9cf4 4240 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4241 -- of an interface is not an interface type (3.9.4 (6.b/2)).
e12b2502 4242
4243 elsif Is_Interface (U_Ent)
5a8fe506 4244 and then not Is_Class_Wide_Type (U_Ent)
e12b2502 4245 and then not Inside_A_Generic
e12b2502 4246 and then
5a8fe506 4247 (Ekind (Subp) = E_Function
4248 or else
4249 not Null_Present
2be1f7d7 4250 (Specification
4251 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
e12b2502 4252 then
4253 Error_Msg_N
4a83cc35 4254 ("stream subprogram for interface type must be null "
4255 & "procedure", Expr);
9f373bb8 4256 end if;
4257
4258 Set_Entity (Expr, Subp);
4259 Set_Etype (Expr, Etype (Subp));
4260
44e4341e 4261 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
9f373bb8 4262
4263 else
4264 Error_Msg_Name_1 := Attr;
4265 Error_Msg_N ("incorrect expression for% attribute", Expr);
4266 end if;
4267 end Analyze_Stream_TSS_Definition;
4268
81b424ac 4269 ------------------------------
4270 -- Check_Indexing_Functions --
4271 ------------------------------
4272
4273 procedure Check_Indexing_Functions is
c8a2d809 4274 Indexing_Found : Boolean := False;
8df4f2a5 4275
44d567c8 4276 procedure Check_Inherited_Indexing;
4277 -- For a derived type, check that no indexing aspect is specified
4278 -- for the type if it is also inherited
4279
81b424ac 4280 procedure Check_One_Function (Subp : Entity_Id);
7796365f 4281 -- Check one possible interpretation. Sets Indexing_Found True if a
4282 -- legal indexing function is found.
81b424ac 4283
05987af3 4284 procedure Illegal_Indexing (Msg : String);
4285 -- Diagnose illegal indexing function if not overloaded. In the
4286 -- overloaded case indicate that no legal interpretation exists.
4287
44d567c8 4288 ------------------------------
4289 -- Check_Inherited_Indexing --
4290 ------------------------------
4291
4292 procedure Check_Inherited_Indexing is
4293 Inherited : Node_Id;
4294
4295 begin
4296 if Attr = Name_Constant_Indexing then
4297 Inherited :=
4298 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4299 else pragma Assert (Attr = Name_Variable_Indexing);
4300 Inherited :=
4301 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4302 end if;
4303
4304 if Present (Inherited) then
4305 if Debug_Flag_Dot_XX then
4306 null;
4307
83d39cd3 4308 -- OK if current attribute_definition_clause is expansion of
4309 -- inherited aspect.
44d567c8 4310
4311 elsif Aspect_Rep_Item (Inherited) = N then
4312 null;
4313
83d39cd3 4314 -- Indicate the operation that must be overridden, rather than
4315 -- redefining the indexing aspect.
44d567c8 4316
4317 else
4318 Illegal_Indexing
f2837ceb 4319 ("indexing function already inherited from parent type");
44d567c8 4320 Error_Msg_NE
4321 ("!override & instead",
4322 N, Entity (Expression (Inherited)));
4323 end if;
4324 end if;
4325 end Check_Inherited_Indexing;
4326
81b424ac 4327 ------------------------
4328 -- Check_One_Function --
4329 ------------------------
4330
4331 procedure Check_One_Function (Subp : Entity_Id) is
05987af3 4332 Default_Element : Node_Id;
4333 Ret_Type : constant Entity_Id := Etype (Subp);
1b7510f9 4334
81b424ac 4335 begin
05987af3 4336 if not Is_Overloadable (Subp) then
4337 Illegal_Indexing ("illegal indexing function for type&");
4338 return;
4339
7796365f 4340 elsif Scope (Subp) /= Scope (Ent) then
4341 if Nkind (Expr) = N_Expanded_Name then
4342
4343 -- Indexing function can't be declared elsewhere
4344
4345 Illegal_Indexing
4346 ("indexing function must be declared in scope of type&");
4347 end if;
4348
05987af3 4349 return;
4350
4351 elsif No (First_Formal (Subp)) then
4352 Illegal_Indexing
4353 ("Indexing requires a function that applies to type&");
4354 return;
4355
4356 elsif No (Next_Formal (First_Formal (Subp))) then
4357 Illegal_Indexing
2eb0ff42 4358 ("indexing function must have at least two parameters");
05987af3 4359 return;
4360
4361 elsif Is_Derived_Type (Ent) then
44d567c8 4362 Check_Inherited_Indexing;
05987af3 4363 end if;
4364
e81df51c 4365 if not Check_Primitive_Function (Subp) then
05987af3 4366 Illegal_Indexing
4367 ("Indexing aspect requires a function that applies to type&");
4368 return;
81b424ac 4369 end if;
4370
7796365f 4371 -- If partial declaration exists, verify that it is not tagged.
4372
4373 if Ekind (Current_Scope) = E_Package
4374 and then Has_Private_Declaration (Ent)
4375 and then From_Aspect_Specification (N)
7c0c95b8 4376 and then
4377 List_Containing (Parent (Ent)) =
4378 Private_Declarations
7796365f 4379 (Specification (Unit_Declaration_Node (Current_Scope)))
4380 and then Nkind (N) = N_Attribute_Definition_Clause
4381 then
4382 declare
4383 Decl : Node_Id;
4384
4385 begin
4386 Decl :=
4387 First (Visible_Declarations
7c0c95b8 4388 (Specification
4389 (Unit_Declaration_Node (Current_Scope))));
7796365f 4390
4391 while Present (Decl) loop
4392 if Nkind (Decl) = N_Private_Type_Declaration
4393 and then Ent = Full_View (Defining_Identifier (Decl))
4394 and then Tagged_Present (Decl)
4395 and then No (Aspect_Specifications (Decl))
4396 then
4397 Illegal_Indexing
4398 ("Indexing aspect cannot be specified on full view "
7c0c95b8 4399 & "if partial view is tagged");
7796365f 4400 return;
4401 end if;
4402
4403 Next (Decl);
4404 end loop;
4405 end;
4406 end if;
4407
1b7510f9 4408 -- An indexing function must return either the default element of
cac18f71 4409 -- the container, or a reference type. For variable indexing it
a45d946f 4410 -- must be the latter.
1b7510f9 4411
05987af3 4412 Default_Element :=
4413 Find_Value_Of_Aspect
4414 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4415
1b7510f9 4416 if Present (Default_Element) then
4417 Analyze (Default_Element);
a45d946f 4418
1b7510f9 4419 if Is_Entity_Name (Default_Element)
05987af3 4420 and then not Covers (Entity (Default_Element), Ret_Type)
4421 and then False
1b7510f9 4422 then
05987af3 4423 Illegal_Indexing
4424 ("wrong return type for indexing function");
1b7510f9 4425 return;
4426 end if;
4427 end if;
4428
a45d946f 4429 -- For variable_indexing the return type must be a reference type
1b7510f9 4430
05987af3 4431 if Attr = Name_Variable_Indexing then
4432 if not Has_Implicit_Dereference (Ret_Type) then
4433 Illegal_Indexing
4434 ("variable indexing must return a reference type");
4435 return;
4436
423b89fd 4437 elsif Is_Access_Constant
4438 (Etype (First_Discriminant (Ret_Type)))
05987af3 4439 then
4440 Illegal_Indexing
4441 ("variable indexing must return an access to variable");
4442 return;
4443 end if;
cac18f71 4444
4445 else
05987af3 4446 if Has_Implicit_Dereference (Ret_Type)
4447 and then not
4448 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4449 then
4450 Illegal_Indexing
4451 ("constant indexing must return an access to constant");
4452 return;
4453
4454 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4455 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4456 then
4457 Illegal_Indexing
4458 ("constant indexing must apply to an access to constant");
4459 return;
4460 end if;
81b424ac 4461 end if;
05987af3 4462
4463 -- All checks succeeded.
4464
4465 Indexing_Found := True;
81b424ac 4466 end Check_One_Function;
4467
05987af3 4468 -----------------------
4469 -- Illegal_Indexing --
4470 -----------------------
4471
4472 procedure Illegal_Indexing (Msg : String) is
4473 begin
7796365f 4474 Error_Msg_NE (Msg, N, Ent);
05987af3 4475 end Illegal_Indexing;
4476
81b424ac 4477 -- Start of processing for Check_Indexing_Functions
4478
4479 begin
89cc7147 4480 if In_Instance then
44d567c8 4481 Check_Inherited_Indexing;
89cc7147 4482 end if;
4483
81b424ac 4484 Analyze (Expr);
4485
4486 if not Is_Overloaded (Expr) then
4487 Check_One_Function (Entity (Expr));
4488
4489 else
4490 declare
2c5754de 4491 I : Interp_Index;
81b424ac 4492 It : Interp;
4493
4494 begin
cac18f71 4495 Indexing_Found := False;
81b424ac 4496 Get_First_Interp (Expr, I, It);
4497 while Present (It.Nam) loop
4498
4499 -- Note that analysis will have added the interpretation
4500 -- that corresponds to the dereference. We only check the
1ef2e6ef 4501 -- subprogram itself. Ignore homonyms that may come from
4502 -- derived types in the context.
81b424ac 4503
1ef2e6ef 4504 if Is_Overloadable (It.Nam)
4505 and then Comes_From_Source (It.Nam)
4506 then
4507 Check_One_Function (It.Nam);
81b424ac 4508 end if;
4509
4510 Get_Next_Interp (I, It);
4511 end loop;
4512 end;
4513 end if;
7796365f 4514
7c0c95b8 4515 if not Indexing_Found and then not Error_Posted (N) then
7796365f 4516 Error_Msg_NE
1ef2e6ef 4517 ("aspect Indexing requires a local function that applies to "
4518 & "type&", Expr, Ent);
7796365f 4519 end if;
81b424ac 4520 end Check_Indexing_Functions;
4521
89cc7147 4522 ------------------------------
4523 -- Check_Iterator_Functions --
4524 ------------------------------
4525
4526 procedure Check_Iterator_Functions is
89cc7147 4527 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
8df4f2a5 4528 -- Check one possible interpretation for validity
89cc7147 4529
4530 ----------------------------
4531 -- Valid_Default_Iterator --
4532 ----------------------------
4533
4534 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
8b8be176 4535 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
7f5dd8d8 4536 Formal : Entity_Id;
89cc7147 4537
4538 begin
4539 if not Check_Primitive_Function (Subp) then
4540 return False;
8b8be176 4541
4542 -- The return type must be derived from a type in an instance
4543 -- of Iterator.Interfaces, and thus its root type must have a
4544 -- predefined name.
4545
4546 elsif Chars (Root_T) /= Name_Forward_Iterator
4547 and then Chars (Root_T) /= Name_Reversible_Iterator
4548 then
4549 return False;
4550
89cc7147 4551 else
4552 Formal := First_Formal (Subp);
4553 end if;
4554
8df4f2a5 4555 -- False if any subsequent formal has no default expression
89cc7147 4556
8df4f2a5 4557 Formal := Next_Formal (Formal);
4558 while Present (Formal) loop
4559 if No (Expression (Parent (Formal))) then
4560 return False;
4561 end if;
89cc7147 4562
8df4f2a5 4563 Next_Formal (Formal);
4564 end loop;
89cc7147 4565
8df4f2a5 4566 -- True if all subsequent formals have default expressions
89cc7147 4567
4568 return True;
4569 end Valid_Default_Iterator;
4570
4571 -- Start of processing for Check_Iterator_Functions
4572
4573 begin
4574 Analyze (Expr);
4575
4576 if not Is_Entity_Name (Expr) then
4577 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4578 end if;
4579
4580 if not Is_Overloaded (Expr) then
4581 if not Check_Primitive_Function (Entity (Expr)) then
4582 Error_Msg_NE
4583 ("aspect Indexing requires a function that applies to type&",
4584 Entity (Expr), Ent);
4585 end if;
4586
05f6f999 4587 -- Flag the default_iterator as well as the denoted function.
4588
89cc7147 4589 if not Valid_Default_Iterator (Entity (Expr)) then
05f6f999 4590 Error_Msg_N ("improper function for default iterator!", Expr);
89cc7147 4591 end if;
4592
4593 else
89cc7147 4594 declare
270ee9c5 4595 Default : Entity_Id := Empty;
8be33fbe 4596 I : Interp_Index;
4597 It : Interp;
89cc7147 4598
4599 begin
4600 Get_First_Interp (Expr, I, It);
4601 while Present (It.Nam) loop
4602 if not Check_Primitive_Function (It.Nam)
59f3e675 4603 or else not Valid_Default_Iterator (It.Nam)
89cc7147 4604 then
4605 Remove_Interp (I);
4606
4607 elsif Present (Default) then
89cc7147 4608
8be33fbe 4609 -- An explicit one should override an implicit one
4610
4611 if Comes_From_Source (Default) =
4612 Comes_From_Source (It.Nam)
4613 then
4614 Error_Msg_N ("default iterator must be unique", Expr);
4615 Error_Msg_Sloc := Sloc (Default);
4616 Error_Msg_N ("\\possible interpretation#", Expr);
4617 Error_Msg_Sloc := Sloc (It.Nam);
4618 Error_Msg_N ("\\possible interpretation#", Expr);
4619
4620 elsif Comes_From_Source (It.Nam) then
4621 Default := It.Nam;
4622 end if;
89cc7147 4623 else
4624 Default := It.Nam;
4625 end if;
4626
4627 Get_Next_Interp (I, It);
4628 end loop;
89cc7147 4629
270ee9c5 4630 if Present (Default) then
4631 Set_Entity (Expr, Default);
4632 Set_Is_Overloaded (Expr, False);
8b8be176 4633 else
4634 Error_Msg_N
7f5dd8d8 4635 ("no interpretation is a valid default iterator!", Expr);
270ee9c5 4636 end if;
4637 end;
89cc7147 4638 end if;
4639 end Check_Iterator_Functions;
4640
4641 -------------------------------
4642 -- Check_Primitive_Function --
4643 -------------------------------
4644
4645 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4646 Ctrl : Entity_Id;
4647
4648 begin
4649 if Ekind (Subp) /= E_Function then
4650 return False;
4651 end if;
4652
4653 if No (First_Formal (Subp)) then
4654 return False;
4655 else
4656 Ctrl := Etype (First_Formal (Subp));
4657 end if;
4658
05f6f999 4659 -- To be a primitive operation subprogram has to be in same scope.
4660
4661 if Scope (Ctrl) /= Scope (Subp) then
4662 return False;
4663 end if;
4664
7d6fb253 4665 -- Type of formal may be the class-wide type, an access to such,
4666 -- or an incomplete view.
4667
89cc7147 4668 if Ctrl = Ent
4669 or else Ctrl = Class_Wide_Type (Ent)
4670 or else
4671 (Ekind (Ctrl) = E_Anonymous_Access_Type
b85d62ec 4672 and then (Designated_Type (Ctrl) = Ent
4673 or else
4674 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
7d6fb253 4675 or else
4676 (Ekind (Ctrl) = E_Incomplete_Type
4677 and then Full_View (Ctrl) = Ent)
89cc7147 4678 then
4679 null;
89cc7147 4680 else
4681 return False;
4682 end if;
4683
4684 return True;
4685 end Check_Primitive_Function;
4686
ae888dbd 4687 ----------------------
4688 -- Duplicate_Clause --
4689 ----------------------
4690
4691 function Duplicate_Clause return Boolean is
d74fc39a 4692 A : Node_Id;
ae888dbd 4693
4694 begin
c8969ba6 4695 -- Nothing to do if this attribute definition clause comes from
4696 -- an aspect specification, since we could not be duplicating an
ae888dbd 4697 -- explicit clause, and we dealt with the case of duplicated aspects
4698 -- in Analyze_Aspect_Specifications.
4699
4700 if From_Aspect_Specification (N) then
4701 return False;
4702 end if;
4703
89f1e35c 4704 -- Otherwise current clause may duplicate previous clause, or a
4705 -- previously given pragma or aspect specification for the same
4706 -- aspect.
d74fc39a 4707
89b3b365 4708 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
ae888dbd 4709
4710 if Present (A) then
89f1e35c 4711 Error_Msg_Name_1 := Chars (N);
4712 Error_Msg_Sloc := Sloc (A);
4713
89b3b365 4714 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
89f1e35c 4715 return True;
ae888dbd 4716 end if;
4717
4718 return False;
4719 end Duplicate_Clause;
4720
9f373bb8 4721 -- Start of processing for Analyze_Attribute_Definition_Clause
4722
d6f39728 4723 begin
d64221a7 4724 -- The following code is a defense against recursion. Not clear that
51fa2a45 4725 -- this can happen legitimately, but perhaps some error situations can
4726 -- cause it, and we did see this recursion during testing.
d64221a7 4727
4728 if Analyzed (N) then
4729 return;
4730 else
4731 Set_Analyzed (N, True);
4732 end if;
4733
2609e4d0 4734 Check_Restriction_No_Use_Of_Attribute (N);
4735
a29bc1d9 4736 -- Ignore some selected attributes in CodePeer mode since they are not
4737 -- relevant in this context.
4738
4739 if CodePeer_Mode then
4740 case Id is
4741
4742 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4743 -- internal representation of types by implicitly packing them.
4744
4745 when Attribute_Component_Size =>
4746 Rewrite (N, Make_Null_Statement (Sloc (N)));
4747 return;
4748
4749 when others =>
4750 null;
4751 end case;
4752 end if;
4753
d8ba53a8 4754 -- Process Ignore_Rep_Clauses option
eef1ca1e 4755
d8ba53a8 4756 if Ignore_Rep_Clauses then
9d627c41 4757 case Id is
4758
eef1ca1e 4759 -- The following should be ignored. They do not affect legality
4760 -- and may be target dependent. The basic idea of -gnatI is to
4761 -- ignore any rep clauses that may be target dependent but do not
4762 -- affect legality (except possibly to be rejected because they
4763 -- are incompatible with the compilation target).
9d627c41 4764
99378362 4765 when Attribute_Alignment
4766 | Attribute_Bit_Order
4767 | Attribute_Component_Size
5bcff344 4768 | Attribute_Default_Scalar_Storage_Order
99378362 4769 | Attribute_Machine_Radix
4770 | Attribute_Object_Size
5bcff344 4771 | Attribute_Scalar_Storage_Order
99378362 4772 | Attribute_Size
4773 | Attribute_Small
4774 | Attribute_Stream_Size
4775 | Attribute_Value_Size
4776 =>
2ff55065 4777 Kill_Rep_Clause (N);
9d627c41 4778 return;
4779
eef1ca1e 4780 -- The following should not be ignored, because in the first place
51fa2a45 4781 -- they are reasonably portable, and should not cause problems
4782 -- in compiling code from another target, and also they do affect
4783 -- legality, e.g. failing to provide a stream attribute for a type
4784 -- may make a program illegal.
9d627c41 4785
99378362 4786 when Attribute_External_Tag
4787 | Attribute_Input
4788 | Attribute_Output
4789 | Attribute_Read
4790 | Attribute_Simple_Storage_Pool
4791 | Attribute_Storage_Pool
4792 | Attribute_Storage_Size
4793 | Attribute_Write
4794 =>
9d627c41 4795 null;
4796
2ff55065 4797 -- We do not do anything here with address clauses, they will be
4798 -- removed by Freeze later on, but for now, it works better to
c07717de 4799 -- keep them in the tree.
2ff55065 4800
4801 when Attribute_Address =>
4802 null;
4803
b593a52c 4804 -- Other cases are errors ("attribute& cannot be set with
4805 -- definition clause"), which will be caught below.
9d627c41 4806
4807 when others =>
4808 null;
4809 end case;
fbc67f84 4810 end if;
4811
d6f39728 4812 Analyze (Nam);
4813 Ent := Entity (Nam);
4814
4815 if Rep_Item_Too_Early (Ent, N) then
4816 return;
4817 end if;
4818
9f373bb8 4819 -- Rep clause applies to full view of incomplete type or private type if
4820 -- we have one (if not, this is a premature use of the type). However,
4821 -- certain semantic checks need to be done on the specified entity (i.e.
4822 -- the private view), so we save it in Ent.
d6f39728 4823
4824 if Is_Private_Type (Ent)
4825 and then Is_Derived_Type (Ent)
4826 and then not Is_Tagged_Type (Ent)
4827 and then No (Full_View (Ent))
4828 then
9f373bb8 4829 -- If this is a private type whose completion is a derivation from
4830 -- another private type, there is no full view, and the attribute
4831 -- belongs to the type itself, not its underlying parent.
d6f39728 4832
4833 U_Ent := Ent;
4834
4835 elsif Ekind (Ent) = E_Incomplete_Type then
d5b349fa 4836
9f373bb8 4837 -- The attribute applies to the full view, set the entity of the
4838 -- attribute definition accordingly.
d5b349fa 4839
d6f39728 4840 Ent := Underlying_Type (Ent);
4841 U_Ent := Ent;
d5b349fa 4842 Set_Entity (Nam, Ent);
4843
d6f39728 4844 else
4845 U_Ent := Underlying_Type (Ent);
4846 end if;
4847
44705307 4848 -- Avoid cascaded error
d6f39728 4849
4850 if Etype (Nam) = Any_Type then
4851 return;
4852
89f1e35c 4853 -- Must be declared in current scope or in case of an aspect
ace3389d 4854 -- specification, must be visible in current scope.
44705307 4855
89f1e35c 4856 elsif Scope (Ent) /= Current_Scope
ace3389d 4857 and then
4858 not (From_Aspect_Specification (N)
4859 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
89f1e35c 4860 then
d6f39728 4861 Error_Msg_N ("entity must be declared in this scope", Nam);
4862 return;
4863
44705307 4864 -- Must not be a source renaming (we do have some cases where the
4865 -- expander generates a renaming, and those cases are OK, in such
a3248fc4 4866 -- cases any attribute applies to the renamed object as well).
44705307 4867
4868 elsif Is_Object (Ent)
4869 and then Present (Renamed_Object (Ent))
44705307 4870 then
a3248fc4 4871 -- Case of renamed object from source, this is an error
4872
4873 if Comes_From_Source (Renamed_Object (Ent)) then
4874 Get_Name_String (Chars (N));
4875 Error_Msg_Strlen := Name_Len;
4876 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4877 Error_Msg_N
4878 ("~ clause not allowed for a renaming declaration "
4879 & "(RM 13.1(6))", Nam);
4880 return;
4881
4882 -- For the case of a compiler generated renaming, the attribute
4883 -- definition clause applies to the renamed object created by the
4884 -- expander. The easiest general way to handle this is to create a
4885 -- copy of the attribute definition clause for this object.
4886
9a48fc56 4887 elsif Is_Entity_Name (Renamed_Object (Ent)) then
a3248fc4 4888 Insert_Action (N,
4889 Make_Attribute_Definition_Clause (Loc,
4890 Name =>
4891 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4892 Chars => Chars (N),
4893 Expression => Duplicate_Subexpr (Expression (N))));
9a48fc56 4894
4895 -- If the renamed object is not an entity, it must be a dereference
4896 -- of an unconstrained function call, and we must introduce a new
4897 -- declaration to capture the expression. This is needed in the case
4898 -- of 'Alignment, where the original declaration must be rewritten.
4899
4900 else
4901 pragma Assert
4902 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4903 null;
a3248fc4 4904 end if;
44705307 4905
4906 -- If no underlying entity, use entity itself, applies to some
4907 -- previously detected error cases ???
4908
f15731c4 4909 elsif No (U_Ent) then
4910 U_Ent := Ent;
4911
44705307 4912 -- Cannot specify for a subtype (exception Object/Value_Size)
4913
d6f39728 4914 elsif Is_Type (U_Ent)
4915 and then not Is_First_Subtype (U_Ent)
4916 and then Id /= Attribute_Object_Size
4917 and then Id /= Attribute_Value_Size
4918 and then not From_At_Mod (N)
4919 then
4920 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4921 return;
d6f39728 4922 end if;
4923
ae888dbd 4924 Set_Entity (N, U_Ent);
4925
d6f39728 4926 -- Switch on particular attribute
4927
4928 case Id is
4929
4930 -------------
4931 -- Address --
4932 -------------
4933
4934 -- Address attribute definition clause
4935
4936 when Attribute_Address => Address : begin
177675a7 4937
4938 -- A little error check, catch for X'Address use X'Address;
4939
4940 if Nkind (Nam) = N_Identifier
4941 and then Nkind (Expr) = N_Attribute_Reference
4942 and then Attribute_Name (Expr) = Name_Address
4943 and then Nkind (Prefix (Expr)) = N_Identifier
4944 and then Chars (Nam) = Chars (Prefix (Expr))
4945 then
4946 Error_Msg_NE
4947 ("address for & is self-referencing", Prefix (Expr), Ent);
4948 return;
4949 end if;
4950
4951 -- Not that special case, carry on with analysis of expression
4952
d6f39728 4953 Analyze_And_Resolve (Expr, RTE (RE_Address));
4954
2f1aac99 4955 -- Even when ignoring rep clauses we need to indicate that the
4956 -- entity has an address clause and thus it is legal to declare
2ff55065 4957 -- it imported. Freeze will get rid of the address clause later.
c07717de 4958 -- Also call Set_Address_Taken to indicate that an address clause
4959 -- was present, even if we are about to remove it.
2f1aac99 4960
4961 if Ignore_Rep_Clauses then
c07717de 4962 Set_Address_Taken (U_Ent);
4963
d3ef794c 4964 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2f1aac99 4965 Record_Rep_Item (U_Ent, N);
4966 end if;
4967
4968 return;
4969 end if;
4970
ae888dbd 4971 if Duplicate_Clause then
4972 null;
d6f39728 4973
4974 -- Case of address clause for subprogram
4975
4976 elsif Is_Subprogram (U_Ent) then
d6f39728 4977 if Has_Homonym (U_Ent) then
4978 Error_Msg_N
f74a102b 4979 ("address clause cannot be given for overloaded "
4980 & "subprogram", Nam);
83f8f0a6 4981 return;
d6f39728 4982 end if;
4983
83f8f0a6 4984 -- For subprograms, all address clauses are permitted, and we
4985 -- mark the subprogram as having a deferred freeze so that Gigi
4986 -- will not elaborate it too soon.
d6f39728 4987
4988 -- Above needs more comments, what is too soon about???
4989
4990 Set_Has_Delayed_Freeze (U_Ent);
4991
4992 -- Case of address clause for entry
4993
4994 elsif Ekind (U_Ent) = E_Entry then
d6f39728 4995 if Nkind (Parent (N)) = N_Task_Body then
4996 Error_Msg_N
4997 ("entry address must be specified in task spec", Nam);
83f8f0a6 4998 return;
d6f39728 4999 end if;
5000
5001 -- For entries, we require a constant address
5002
5003 Check_Constant_Address_Clause (Expr, U_Ent);
5004
83f8f0a6 5005 -- Special checks for task types
5006
f15731c4 5007 if Is_Task_Type (Scope (U_Ent))
5008 and then Comes_From_Source (Scope (U_Ent))
5009 then
5010 Error_Msg_N
1e3532e7 5011 ("??entry address declared for entry in task type", N);
f15731c4 5012 Error_Msg_N
1e3532e7 5013 ("\??only one task can be declared of this type", N);
f15731c4 5014 end if;
5015
83f8f0a6 5016 -- Entry address clauses are obsolescent
5017
e0521a36 5018 Check_Restriction (No_Obsolescent_Features, N);
5019
9dfe12ae 5020 if Warn_On_Obsolescent_Feature then
5021 Error_Msg_N
f74a102b 5022 ("?j?attaching interrupt to task entry is an obsolescent "
5023 & "feature (RM J.7.1)", N);
9dfe12ae 5024 Error_Msg_N
1e3532e7 5025 ("\?j?use interrupt procedure instead", N);
9dfe12ae 5026 end if;
5027
8c252f6f 5028 -- Case of an address clause for a class-wide object, which is
5110559b 5029 -- considered erroneous.
5030
5031 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
5032 Error_Msg_NE
5033 ("??class-wide object & must not be overlaid", Nam, U_Ent);
9dfe12ae 5034 Error_Msg_N
1e3532e7 5035 ("\??Program_Error will be raised at run time", Nam);
9dfe12ae 5036 Insert_Action (Declaration_Node (U_Ent),
5037 Make_Raise_Program_Error (Loc,
5038 Reason => PE_Overlaid_Controlled_Object));
83f8f0a6 5039 return;
9dfe12ae 5040
76be83f9 5041 -- Case of address clause for an object
d6f39728 5042
76be83f9 5043 elsif Ekind_In (U_Ent, E_Constant, E_Variable) then
d6f39728 5044 declare
d6da7448 5045 Expr : constant Node_Id := Expression (N);
5046 O_Ent : Entity_Id;
5047 Off : Boolean;
d6f39728 5048
5049 begin
7ee315cc 5050 -- Exported variables cannot have an address clause, because
5051 -- this cancels the effect of the pragma Export.
d6f39728 5052
5053 if Is_Exported (U_Ent) then
5054 Error_Msg_N
5055 ("cannot export object with address clause", Nam);
83f8f0a6 5056 return;
d6da7448 5057 end if;
5058
5059 Find_Overlaid_Entity (N, O_Ent, Off);
d6f39728 5060
a9dd889b 5061 if Present (O_Ent) then
798dec73 5062
a9dd889b 5063 -- If the object overlays a constant object, mark it so
b2d32174 5064
a9dd889b 5065 if Is_Constant_Object (O_Ent) then
5066 Set_Overlays_Constant (U_Ent);
5067 end if;
798dec73 5068
514a5555 5069 -- If the address clause is of the form:
5070
5071 -- for X'Address use Y'Address;
5072
5073 -- or
5074
5075 -- C : constant Address := Y'Address;
5076 -- ...
5077 -- for X'Address use C;
5078
5079 -- then we make an entry in the table to check the size
5080 -- and alignment of the overlaying variable. But we defer
5081 -- this check till after code generation to take full
5082 -- advantage of the annotation done by the back end.
5083
5084 -- If the entity has a generic type, the check will be
5085 -- performed in the instance if the actual type justifies
5086 -- it, and we do not insert the clause in the table to
5087 -- prevent spurious warnings.
5088
5089 -- Note: we used to test Comes_From_Source and only give
5090 -- this warning for source entities, but we have removed
5091 -- this test. It really seems bogus to generate overlays
5092 -- that would trigger this warning in generated code.
5093 -- Furthermore, by removing the test, we handle the
5094 -- aspect case properly.
5095
5096 if Is_Object (O_Ent)
5097 and then not Is_Generic_Type (Etype (U_Ent))
5098 and then Address_Clause_Overlay_Warnings
5099 then
d10a1b95 5100 Register_Address_Clause_Check
5101 (N, U_Ent, No_Uint, O_Ent, Off);
514a5555 5102 end if;
9ab70407 5103
5104 -- If the overlay changes the storage order, mark the
5105 -- entity as being volatile to block any optimization
5106 -- for it since the construct is not really supported
5107 -- by the back end.
5108
5109 if (Is_Record_Type (Etype (U_Ent))
5110 or else Is_Array_Type (Etype (U_Ent)))
5111 and then (Is_Record_Type (Etype (O_Ent))
5112 or else Is_Array_Type (Etype (O_Ent)))
88d1247a 5113 and then Reverse_Storage_Order (Etype (U_Ent)) /=
5114 Reverse_Storage_Order (Etype (O_Ent))
9ab70407 5115 then
5116 Set_Treat_As_Volatile (U_Ent);
5117 end if;
5118
a9dd889b 5119 else
5120 -- If this is not an overlay, mark a variable as being
5121 -- volatile to prevent unwanted optimizations. It's a
5122 -- conservative interpretation of RM 13.3(19) for the
5123 -- cases where the compiler cannot detect potential
5124 -- aliasing issues easily and it also covers the case
5125 -- of an absolute address where the volatile aspect is
5126 -- kind of implicit.
5127
5128 if Ekind (U_Ent) = E_Variable then
5129 Set_Treat_As_Volatile (U_Ent);
5130 end if;
514a5555 5131
5132 -- Make an entry in the table for an absolute address as
5133 -- above to check that the value is compatible with the
5134 -- alignment of the object.
5135
5136 declare
5137 Addr : constant Node_Id := Address_Value (Expr);
5138 begin
5139 if Compile_Time_Known_Value (Addr)
5140 and then Address_Clause_Overlay_Warnings
5141 then
d10a1b95 5142 Register_Address_Clause_Check
5143 (N, U_Ent, Expr_Value (Addr), Empty, False);
514a5555 5144 end if;
5145 end;
b2d32174 5146 end if;
5147
95009d64 5148 -- Issue an unconditional warning for a constant overlaying
5149 -- a variable. For the reverse case, we will issue it only
b2d32174 5150 -- if the variable is modified.
95009d64 5151
76be83f9 5152 if Ekind (U_Ent) = E_Constant
95009d64 5153 and then Present (O_Ent)
b2d32174 5154 and then not Overlays_Constant (U_Ent)
5155 and then Address_Clause_Overlay_Warnings
9dfe12ae 5156 then
1e3532e7 5157 Error_Msg_N ("??constant overlays a variable", Expr);
9dfe12ae 5158
d6f39728 5159 -- Imported variables can have an address clause, but then
5160 -- the import is pretty meaningless except to suppress
5161 -- initializations, so we do not need such variables to
5162 -- be statically allocated (and in fact it causes trouble
5163 -- if the address clause is a local value).
5164
5165 elsif Is_Imported (U_Ent) then
5166 Set_Is_Statically_Allocated (U_Ent, False);
5167 end if;
5168
5169 -- We mark a possible modification of a variable with an
5170 -- address clause, since it is likely aliasing is occurring.
5171
177675a7 5172 Note_Possible_Modification (Nam, Sure => False);
d6f39728 5173
9dfe12ae 5174 -- Legality checks on the address clause for initialized
5175 -- objects is deferred until the freeze point, because
2beb22b1 5176 -- a subsequent pragma might indicate that the object
42e09e36 5177 -- is imported and thus not initialized. Also, the address
5178 -- clause might involve entities that have yet to be
5179 -- elaborated.
9dfe12ae 5180
5181 Set_Has_Delayed_Freeze (U_Ent);
5182
51ad5ad2 5183 -- If an initialization call has been generated for this
5184 -- object, it needs to be deferred to after the freeze node
5185 -- we have just now added, otherwise GIGI will see a
5186 -- reference to the variable (as actual to the IP call)
5187 -- before its definition.
5188
5189 declare
df9fba45 5190 Init_Call : constant Node_Id :=
5191 Remove_Init_Call (U_Ent, N);
4bba0a8d 5192
51ad5ad2 5193 begin
5194 if Present (Init_Call) then
28a4283c 5195 Append_Freeze_Action (U_Ent, Init_Call);
df9fba45 5196
28a4283c 5197 -- Reset Initialization_Statements pointer so that
5198 -- if there is a pragma Import further down, it can
5199 -- clear any default initialization.
df9fba45 5200
28a4283c 5201 Set_Initialization_Statements (U_Ent, Init_Call);
51ad5ad2 5202 end if;
5203 end;
5204
44e4341e 5205 -- Entity has delayed freeze, so we will generate an
5206 -- alignment check at the freeze point unless suppressed.
d6f39728 5207
44e4341e 5208 if not Range_Checks_Suppressed (U_Ent)
5209 and then not Alignment_Checks_Suppressed (U_Ent)
5210 then
5211 Set_Check_Address_Alignment (N);
5212 end if;
d6f39728 5213
5214 -- Kill the size check code, since we are not allocating
5215 -- the variable, it is somewhere else.
5216
5217 Kill_Size_Check_Code (U_Ent);
d6da7448 5218 end;
83f8f0a6 5219
d6f39728 5220 -- Not a valid entity for an address clause
5221
5222 else
5223 Error_Msg_N ("address cannot be given for &", Nam);
5224 end if;
5225 end Address;
5226
5227 ---------------
5228 -- Alignment --
5229 ---------------
5230
5231 -- Alignment attribute definition clause
5232
b47769f0 5233 when Attribute_Alignment => Alignment : declare
208fd589 5234 Align : constant Uint := Get_Alignment_Value (Expr);
5235 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
41331dcf 5236
d6f39728 5237 begin
5238 FOnly := True;
5239
5240 if not Is_Type (U_Ent)
5241 and then Ekind (U_Ent) /= E_Variable
5242 and then Ekind (U_Ent) /= E_Constant
5243 then
5244 Error_Msg_N ("alignment cannot be given for &", Nam);
5245
ae888dbd 5246 elsif Duplicate_Clause then
5247 null;
d6f39728 5248
5249 elsif Align /= No_Uint then
5250 Set_Has_Alignment_Clause (U_Ent);
208fd589 5251
44705307 5252 -- Tagged type case, check for attempt to set alignment to a
f74a102b 5253 -- value greater than Max_Align, and reset if so. This error
5254 -- is suppressed in ASIS mode to allow for different ASIS
f9906591 5255 -- back ends or ASIS-based tools to query the illegal clause.
44705307 5256
f74a102b 5257 if Is_Tagged_Type (U_Ent)
5258 and then Align > Max_Align
5259 and then not ASIS_Mode
5260 then
208fd589 5261 Error_Msg_N
1e3532e7 5262 ("alignment for & set to Maximum_Aligment??", Nam);
f74a102b 5263 Set_Alignment (U_Ent, Max_Align);
44705307 5264
5265 -- All other cases
5266
208fd589 5267 else
5268 Set_Alignment (U_Ent, Align);
5269 end if;
b47769f0 5270
5271 -- For an array type, U_Ent is the first subtype. In that case,
5272 -- also set the alignment of the anonymous base type so that
5273 -- other subtypes (such as the itypes for aggregates of the
5274 -- type) also receive the expected alignment.
5275
5276 if Is_Array_Type (U_Ent) then
5277 Set_Alignment (Base_Type (U_Ent), Align);
5278 end if;
d6f39728 5279 end if;
b47769f0 5280 end Alignment;
d6f39728 5281
5282 ---------------
5283 -- Bit_Order --
5284 ---------------
5285
5286 -- Bit_Order attribute definition clause
5287
99378362 5288 when Attribute_Bit_Order =>
d6f39728 5289 if not Is_Record_Type (U_Ent) then
5290 Error_Msg_N
5291 ("Bit_Order can only be defined for record type", Nam);
5292
ae888dbd 5293 elsif Duplicate_Clause then
5294 null;
5295
d6f39728 5296 else
5297 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5298
5299 if Etype (Expr) = Any_Type then
5300 return;
5301
cda40848 5302 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5303 Flag_Non_Static_Expr
5304 ("Bit_Order requires static expression!", Expr);
d6f39728 5305
5306 else
5307 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
fae4ea1f 5308 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
d6f39728 5309 end if;
5310 end if;
5311 end if;
d6f39728 5312
5313 --------------------
5314 -- Component_Size --
5315 --------------------
5316
5317 -- Component_Size attribute definition clause
5318
5319 when Attribute_Component_Size => Component_Size_Case : declare
5320 Csize : constant Uint := Static_Integer (Expr);
a0fc8c5b 5321 Ctyp : Entity_Id;
d6f39728 5322 Btype : Entity_Id;
5323 Biased : Boolean;
5324 New_Ctyp : Entity_Id;
5325 Decl : Node_Id;
5326
5327 begin
5328 if not Is_Array_Type (U_Ent) then
5329 Error_Msg_N ("component size requires array type", Nam);
5330 return;
5331 end if;
5332
5333 Btype := Base_Type (U_Ent);
f74a102b 5334 Ctyp := Component_Type (Btype);
d6f39728 5335
ae888dbd 5336 if Duplicate_Clause then
5337 null;
d6f39728 5338
f3e4db96 5339 elsif Rep_Item_Too_Early (Btype, N) then
5340 null;
5341
d6f39728 5342 elsif Csize /= No_Uint then
a0fc8c5b 5343 Check_Size (Expr, Ctyp, Csize, Biased);
d6f39728 5344
d74fc39a 5345 -- For the biased case, build a declaration for a subtype that
5346 -- will be used to represent the biased subtype that reflects
5347 -- the biased representation of components. We need the subtype
5348 -- to get proper conversions on referencing elements of the
36ac5fbb 5349 -- array.
3062c401 5350
36ac5fbb 5351 if Biased then
5352 New_Ctyp :=
5353 Make_Defining_Identifier (Loc,
5354 Chars =>
5355 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3062c401 5356
36ac5fbb 5357 Decl :=
5358 Make_Subtype_Declaration (Loc,
5359 Defining_Identifier => New_Ctyp,
5360 Subtype_Indication =>
5361 New_Occurrence_Of (Component_Type (Btype), Loc));
5362
5363 Set_Parent (Decl, N);
5364 Analyze (Decl, Suppress => All_Checks);
5365
5366 Set_Has_Delayed_Freeze (New_Ctyp, False);
5367 Set_Esize (New_Ctyp, Csize);
5368 Set_RM_Size (New_Ctyp, Csize);
5369 Init_Alignment (New_Ctyp);
5370 Set_Is_Itype (New_Ctyp, True);
5371 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5372
5373 Set_Component_Type (Btype, New_Ctyp);
5374 Set_Biased (New_Ctyp, N, "component size clause");
d6f39728 5375 end if;
5376
36ac5fbb 5377 Set_Component_Size (Btype, Csize);
5378
a0fc8c5b 5379 -- Deal with warning on overridden size
5380
5381 if Warn_On_Overridden_Size
5382 and then Has_Size_Clause (Ctyp)
5383 and then RM_Size (Ctyp) /= Csize
5384 then
5385 Error_Msg_NE
1e3532e7 5386 ("component size overrides size clause for&?S?", N, Ctyp);
a0fc8c5b 5387 end if;
5388
d6f39728 5389 Set_Has_Component_Size_Clause (Btype, True);
f3e4db96 5390 Set_Has_Non_Standard_Rep (Btype, True);
d6f39728 5391 end if;
5392 end Component_Size_Case;
5393
81b424ac 5394 -----------------------
5395 -- Constant_Indexing --
5396 -----------------------
5397
5398 when Attribute_Constant_Indexing =>
5399 Check_Indexing_Functions;
5400
89f1e35c 5401 ---------
5402 -- CPU --
5403 ---------
5404
99378362 5405 when Attribute_CPU =>
5406
89f1e35c 5407 -- CPU attribute definition clause not allowed except from aspect
5408 -- specification.
5409
5410 if From_Aspect_Specification (N) then
5411 if not Is_Task_Type (U_Ent) then
5412 Error_Msg_N ("CPU can only be defined for task", Nam);
5413
5414 elsif Duplicate_Clause then
5415 null;
5416
5417 else
5418 -- The expression must be analyzed in the special manner
5419 -- described in "Handling of Default and Per-Object
5420 -- Expressions" in sem.ads.
5421
5422 -- The visibility to the discriminants must be restored
5423
5424 Push_Scope_And_Install_Discriminants (U_Ent);
5425 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5426 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5427
cda40848 5428 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5429 Check_Restriction (Static_Priorities, Expr);
5430 end if;
5431 end if;
5432
5433 else
5434 Error_Msg_N
5435 ("attribute& cannot be set with definition clause", N);
5436 end if;
89f1e35c 5437
89cc7147 5438 ----------------------
5439 -- Default_Iterator --
5440 ----------------------
5441
5442 when Attribute_Default_Iterator => Default_Iterator : declare
5443 Func : Entity_Id;
fbf4d6ef 5444 Typ : Entity_Id;
89cc7147 5445
5446 begin
05f6f999 5447 -- If target type is untagged, further checks are irrelevant
5448
89cc7147 5449 if not Is_Tagged_Type (U_Ent) then
5450 Error_Msg_N
05f6f999 5451 ("aspect Default_Iterator applies to tagged type", Nam);
5452 return;
89cc7147 5453 end if;
5454
5455 Check_Iterator_Functions;
5456
5457 Analyze (Expr);
5458
5459 if not Is_Entity_Name (Expr)
5460 or else Ekind (Entity (Expr)) /= E_Function
5461 then
5462 Error_Msg_N ("aspect Iterator must be a function", Expr);
05f6f999 5463 return;
89cc7147 5464 else
5465 Func := Entity (Expr);
5466 end if;
5467
fbf4d6ef 5468 -- The type of the first parameter must be T, T'class, or a
05f6f999 5469 -- corresponding access type (5.5.1 (8/3). If function is
5470 -- parameterless label type accordingly.
fbf4d6ef 5471
5472 if No (First_Formal (Func)) then
05f6f999 5473 Typ := Any_Type;
fbf4d6ef 5474 else
5475 Typ := Etype (First_Formal (Func));
5476 end if;
5477
5478 if Typ = U_Ent
5479 or else Typ = Class_Wide_Type (U_Ent)
5480 or else (Is_Access_Type (Typ)
5481 and then Designated_Type (Typ) = U_Ent)
5482 or else (Is_Access_Type (Typ)
5483 and then Designated_Type (Typ) =
5484 Class_Wide_Type (U_Ent))
89cc7147 5485 then
fbf4d6ef 5486 null;
5487
5488 else
89cc7147 5489 Error_Msg_NE
5490 ("Default Iterator must be a primitive of&", Func, U_Ent);
5491 end if;
5492 end Default_Iterator;
5493
89f1e35c 5494 ------------------------
5495 -- Dispatching_Domain --
5496 ------------------------
5497
99378362 5498 when Attribute_Dispatching_Domain =>
5499
89f1e35c 5500 -- Dispatching_Domain attribute definition clause not allowed
5501 -- except from aspect specification.
5502
5503 if From_Aspect_Specification (N) then
5504 if not Is_Task_Type (U_Ent) then
fbf4d6ef 5505 Error_Msg_N
5506 ("Dispatching_Domain can only be defined for task", Nam);
89f1e35c 5507
5508 elsif Duplicate_Clause then
5509 null;
5510
5511 else
5512 -- The expression must be analyzed in the special manner
5513 -- described in "Handling of Default and Per-Object
5514 -- Expressions" in sem.ads.
5515
5516 -- The visibility to the discriminants must be restored
5517
5518 Push_Scope_And_Install_Discriminants (U_Ent);
5519
5520 Preanalyze_Spec_Expression
5521 (Expr, RTE (RE_Dispatching_Domain));
5522
5523 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5524 end if;
5525
5526 else
5527 Error_Msg_N
5528 ("attribute& cannot be set with definition clause", N);
5529 end if;
89f1e35c 5530
d6f39728 5531 ------------------
5532 -- External_Tag --
5533 ------------------
5534
99378362 5535 when Attribute_External_Tag =>
d6f39728 5536 if not Is_Tagged_Type (U_Ent) then
5537 Error_Msg_N ("should be a tagged type", Nam);
5538 end if;
5539
ae888dbd 5540 if Duplicate_Clause then
5541 null;
d6f39728 5542
9af0ddc7 5543 else
ae888dbd 5544 Analyze_And_Resolve (Expr, Standard_String);
fbc67f84 5545
cda40848 5546 if not Is_OK_Static_Expression (Expr) then
ae888dbd 5547 Flag_Non_Static_Expr
5548 ("static string required for tag name!", Nam);
5549 end if;
5550
ae888dbd 5551 if not Is_Library_Level_Entity (U_Ent) then
5552 Error_Msg_NE
1e3532e7 5553 ("??non-unique external tag supplied for &", N, U_Ent);
ae888dbd 5554 Error_Msg_N
f74a102b 5555 ("\??same external tag applies to all subprogram calls",
5556 N);
ae888dbd 5557 Error_Msg_N
1e3532e7 5558 ("\??corresponding internal tag cannot be obtained", N);
ae888dbd 5559 end if;
fbc67f84 5560 end if;
d6f39728 5561
b57530b8 5562 --------------------------
5563 -- Implicit_Dereference --
5564 --------------------------
7947a439 5565
b57530b8 5566 when Attribute_Implicit_Dereference =>
7947a439 5567
2beb22b1 5568 -- Legality checks already performed at the point of the type
5569 -- declaration, aspect is not delayed.
7947a439 5570
89cc7147 5571 null;
b57530b8 5572
d6f39728 5573 -----------
5574 -- Input --
5575 -----------
5576
9f373bb8 5577 when Attribute_Input =>
5578 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5579 Set_Has_Specified_Stream_Input (Ent);
d6f39728 5580
89f1e35c 5581 ------------------------
5582 -- Interrupt_Priority --
5583 ------------------------
5584
99378362 5585 when Attribute_Interrupt_Priority =>
5586
89f1e35c 5587 -- Interrupt_Priority attribute definition clause not allowed
5588 -- except from aspect specification.
5589
5590 if From_Aspect_Specification (N) then
f02a9a9a 5591 if not Is_Concurrent_Type (U_Ent) then
89f1e35c 5592 Error_Msg_N
f74a102b 5593 ("Interrupt_Priority can only be defined for task and "
5594 & "protected object", Nam);
89f1e35c 5595
5596 elsif Duplicate_Clause then
5597 null;
5598
5599 else
5600 -- The expression must be analyzed in the special manner
5601 -- described in "Handling of Default and Per-Object
5602 -- Expressions" in sem.ads.
5603
5604 -- The visibility to the discriminants must be restored
5605
5606 Push_Scope_And_Install_Discriminants (U_Ent);
5607
5608 Preanalyze_Spec_Expression
5609 (Expr, RTE (RE_Interrupt_Priority));
5610
5611 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
d4e1acfa 5612
5613 -- Check the No_Task_At_Interrupt_Priority restriction
5614
5615 if Is_Task_Type (U_Ent) then
5616 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5617 end if;
89f1e35c 5618 end if;
5619
5620 else
5621 Error_Msg_N
5622 ("attribute& cannot be set with definition clause", N);
5623 end if;
89f1e35c 5624
b3f8228a 5625 --------------
5626 -- Iterable --
5627 --------------
5628
5629 when Attribute_Iterable =>
5630 Analyze (Expr);
bde03454 5631
b3f8228a 5632 if Nkind (Expr) /= N_Aggregate then
5633 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5634 end if;
5635
5636 declare
5637 Assoc : Node_Id;
5638
5639 begin
5640 Assoc := First (Component_Associations (Expr));
5641 while Present (Assoc) loop
5642 if not Is_Entity_Name (Expression (Assoc)) then
5643 Error_Msg_N ("value must be a function", Assoc);
5644 end if;
bde03454 5645
b3f8228a 5646 Next (Assoc);
5647 end loop;
5648 end;
5649
89cc7147 5650 ----------------------
5651 -- Iterator_Element --
5652 ----------------------
5653
5654 when Attribute_Iterator_Element =>
5655 Analyze (Expr);
5656
5657 if not Is_Entity_Name (Expr)
5658 or else not Is_Type (Entity (Expr))
5659 then
5660 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5661 end if;
5662
d6f39728 5663 -------------------
5664 -- Machine_Radix --
5665 -------------------
5666
5667 -- Machine radix attribute definition clause
5668
5669 when Attribute_Machine_Radix => Machine_Radix : declare
5670 Radix : constant Uint := Static_Integer (Expr);
5671
5672 begin
5673 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5674 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5675
ae888dbd 5676 elsif Duplicate_Clause then
5677 null;
d6f39728 5678
5679 elsif Radix /= No_Uint then
5680 Set_Has_Machine_Radix_Clause (U_Ent);
5681 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5682
5683 if Radix = 2 then
5684 null;
f74a102b 5685
d6f39728 5686 elsif Radix = 10 then
5687 Set_Machine_Radix_10 (U_Ent);
f74a102b 5688
5689 -- The following error is suppressed in ASIS mode to allow for
f9906591 5690 -- different ASIS back ends or ASIS-based tools to query the
f74a102b 5691 -- illegal clause.
5692
5693 elsif not ASIS_Mode then
d6f39728 5694 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5695 end if;
5696 end if;
5697 end Machine_Radix;
5698
5699 -----------------
5700 -- Object_Size --
5701 -----------------
5702
5703 -- Object_Size attribute definition clause
5704
5705 when Attribute_Object_Size => Object_Size : declare
bfa5a9d9 5706 Size : constant Uint := Static_Integer (Expr);
5707
d6f39728 5708 Biased : Boolean;
bfa5a9d9 5709 pragma Warnings (Off, Biased);
d6f39728 5710
5711 begin
5712 if not Is_Type (U_Ent) then
5713 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5714
ae888dbd 5715 elsif Duplicate_Clause then
5716 null;
d6f39728 5717
5718 else
5719 Check_Size (Expr, U_Ent, Size, Biased);
5720
f74a102b 5721 -- The following errors are suppressed in ASIS mode to allow
f9906591 5722 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 5723 -- the illegal clause.
5724
5725 if ASIS_Mode then
5726 null;
5727
5728 elsif Is_Scalar_Type (U_Ent) then
829cd457 5729 if Size /= 8 and then Size /= 16 and then Size /= 32
5730 and then UI_Mod (Size, 64) /= 0
5731 then
5732 Error_Msg_N
5733 ("Object_Size must be 8, 16, 32, or multiple of 64",
5734 Expr);
5735 end if;
5736
5737 elsif Size mod 8 /= 0 then
5738 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
d6f39728 5739 end if;
5740
5741 Set_Esize (U_Ent, Size);
5742 Set_Has_Object_Size_Clause (U_Ent);
1d366b32 5743 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5744 end if;
5745 end Object_Size;
5746
5747 ------------
5748 -- Output --
5749 ------------
5750
9f373bb8 5751 when Attribute_Output =>
5752 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5753 Set_Has_Specified_Stream_Output (Ent);
d6f39728 5754
89f1e35c 5755 --------------
5756 -- Priority --
5757 --------------
5758
99378362 5759 when Attribute_Priority =>
5760
89f1e35c 5761 -- Priority attribute definition clause not allowed except from
5762 -- aspect specification.
5763
5764 if From_Aspect_Specification (N) then
f02a9a9a 5765 if not (Is_Concurrent_Type (U_Ent)
3a72f9c3 5766 or else Ekind (U_Ent) = E_Procedure)
89f1e35c 5767 then
5768 Error_Msg_N
f02a9a9a 5769 ("Priority can only be defined for task and protected "
5770 & "object", Nam);
89f1e35c 5771
5772 elsif Duplicate_Clause then
5773 null;
5774
5775 else
5776 -- The expression must be analyzed in the special manner
5777 -- described in "Handling of Default and Per-Object
5778 -- Expressions" in sem.ads.
5779
5780 -- The visibility to the discriminants must be restored
5781
5782 Push_Scope_And_Install_Discriminants (U_Ent);
5783 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5784 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5785
cda40848 5786 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5787 Check_Restriction (Static_Priorities, Expr);
5788 end if;
5789 end if;
5790
5791 else
5792 Error_Msg_N
5793 ("attribute& cannot be set with definition clause", N);
5794 end if;
89f1e35c 5795
d6f39728 5796 ----------
5797 -- Read --
5798 ----------
5799
9f373bb8 5800 when Attribute_Read =>
5801 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5802 Set_Has_Specified_Stream_Read (Ent);
d6f39728 5803
b7b74740 5804 --------------------------
5805 -- Scalar_Storage_Order --
5806 --------------------------
5807
5808 -- Scalar_Storage_Order attribute definition clause
5809
99378362 5810 when Attribute_Scalar_Storage_Order =>
b43a5770 5811 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
b7b74740 5812 Error_Msg_N
f74a102b 5813 ("Scalar_Storage_Order can only be defined for record or "
5814 & "array type", Nam);
b7b74740 5815
5816 elsif Duplicate_Clause then
5817 null;
5818
5819 else
5820 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5821
5822 if Etype (Expr) = Any_Type then
5823 return;
5824
cda40848 5825 elsif not Is_OK_Static_Expression (Expr) then
b7b74740 5826 Flag_Non_Static_Expr
5827 ("Scalar_Storage_Order requires static expression!", Expr);
5828
c0912570 5829 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5830
5831 -- Here for the case of a non-default (i.e. non-confirming)
5832 -- Scalar_Storage_Order attribute definition.
5833
5834 if Support_Nondefault_SSO_On_Target then
d0a9ea3b 5835 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
c0912570 5836 else
5837 Error_Msg_N
f74a102b 5838 ("non-default Scalar_Storage_Order not supported on "
5839 & "target", Expr);
b7b74740 5840 end if;
5841 end if;
b64082f2 5842
5843 -- Clear SSO default indications since explicit setting of the
5844 -- order overrides the defaults.
5845
5846 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5847 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
b7b74740 5848 end if;
b7b74740 5849
e6ce0468 5850 --------------------------
5851 -- Secondary_Stack_Size --
5852 --------------------------
5853
99378362 5854 when Attribute_Secondary_Stack_Size =>
5855
e6ce0468 5856 -- Secondary_Stack_Size attribute definition clause not allowed
5857 -- except from aspect specification.
5858
5859 if From_Aspect_Specification (N) then
5860 if not Is_Task_Type (U_Ent) then
fe696bd7 5861 Error_Msg_N
5862 ("Secondary Stack Size can only be defined for task", Nam);
e6ce0468 5863
5864 elsif Duplicate_Clause then
5865 null;
5866
5867 else
5868 Check_Restriction (No_Secondary_Stack, Expr);
5869
5870 -- The expression must be analyzed in the special manner
5871 -- described in "Handling of Default and Per-Object
5872 -- Expressions" in sem.ads.
5873
5874 -- The visibility to the discriminants must be restored
5875
5876 Push_Scope_And_Install_Discriminants (U_Ent);
5877 Preanalyze_Spec_Expression (Expr, Any_Integer);
5878 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5879
5880 if not Is_OK_Static_Expression (Expr) then
5881 Check_Restriction (Static_Storage_Size, Expr);
5882 end if;
5883 end if;
5884
5885 else
5886 Error_Msg_N
5887 ("attribute& cannot be set with definition clause", N);
5888 end if;
e6ce0468 5889
d6f39728 5890 ----------
5891 -- Size --
5892 ----------
5893
5894 -- Size attribute definition clause
5895
5896 when Attribute_Size => Size : declare
5897 Size : constant Uint := Static_Integer (Expr);
5898 Etyp : Entity_Id;
5899 Biased : Boolean;
5900
5901 begin
5902 FOnly := True;
5903
ae888dbd 5904 if Duplicate_Clause then
5905 null;
d6f39728 5906
5907 elsif not Is_Type (U_Ent)
5908 and then Ekind (U_Ent) /= E_Variable
5909 and then Ekind (U_Ent) /= E_Constant
5910 then
5911 Error_Msg_N ("size cannot be given for &", Nam);
5912
5913 elsif Is_Array_Type (U_Ent)
5914 and then not Is_Constrained (U_Ent)
5915 then
5916 Error_Msg_N
5917 ("size cannot be given for unconstrained array", Nam);
5918
c2b89d6e 5919 elsif Size /= No_Uint then
d6f39728 5920 if Is_Type (U_Ent) then
5921 Etyp := U_Ent;
5922 else
5923 Etyp := Etype (U_Ent);
5924 end if;
5925
59ac57b5 5926 -- Check size, note that Gigi is in charge of checking that the
5927 -- size of an array or record type is OK. Also we do not check
5928 -- the size in the ordinary fixed-point case, since it is too
5929 -- early to do so (there may be subsequent small clause that
5930 -- affects the size). We can check the size if a small clause
5931 -- has already been given.
d6f39728 5932
5933 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5934 or else Has_Small_Clause (U_Ent)
5935 then
5936 Check_Size (Expr, Etyp, Size, Biased);
b77e4501 5937 Set_Biased (U_Ent, N, "size clause", Biased);
d6f39728 5938 end if;
5939
5940 -- For types set RM_Size and Esize if possible
5941
5942 if Is_Type (U_Ent) then
5943 Set_RM_Size (U_Ent, Size);
5944
ada34def 5945 -- For elementary types, increase Object_Size to power of 2,
5946 -- but not less than a storage unit in any case (normally
59ac57b5 5947 -- this means it will be byte addressable).
d6f39728 5948
ada34def 5949 -- For all other types, nothing else to do, we leave Esize
5950 -- (object size) unset, the back end will set it from the
5951 -- size and alignment in an appropriate manner.
5952
1d366b32 5953 -- In both cases, we check whether the alignment must be
5954 -- reset in the wake of the size change.
5955
ada34def 5956 if Is_Elementary_Type (U_Ent) then
f15731c4 5957 if Size <= System_Storage_Unit then
5958 Init_Esize (U_Ent, System_Storage_Unit);
d6f39728 5959 elsif Size <= 16 then
5960 Init_Esize (U_Ent, 16);
5961 elsif Size <= 32 then
5962 Init_Esize (U_Ent, 32);
5963 else
5964 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5965 end if;
5966
1d366b32 5967 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5968 else
5969 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5970 end if;
5971
d6f39728 5972 -- For objects, set Esize only
5973
5974 else
f74a102b 5975 -- The following error is suppressed in ASIS mode to allow
f9906591 5976 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 5977 -- the illegal clause.
5978
5979 if Is_Elementary_Type (Etyp)
5980 and then Size /= System_Storage_Unit
5981 and then Size /= System_Storage_Unit * 2
5982 and then Size /= System_Storage_Unit * 4
5983 and then Size /= System_Storage_Unit * 8
5984 and then not ASIS_Mode
5985 then
5986 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5987 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5988 Error_Msg_N
5989 ("size for primitive object must be a power of 2 in "
5990 & "the range ^-^", N);
9dfe12ae 5991 end if;
5992
d6f39728 5993 Set_Esize (U_Ent, Size);
5994 end if;
5995
5996 Set_Has_Size_Clause (U_Ent);
5997 end if;
5998 end Size;
5999
6000 -----------
6001 -- Small --
6002 -----------
6003
6004 -- Small attribute definition clause
6005
6006 when Attribute_Small => Small : declare
6007 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
6008 Small : Ureal;
6009
6010 begin
6011 Analyze_And_Resolve (Expr, Any_Real);
6012
6013 if Etype (Expr) = Any_Type then
6014 return;
6015
cda40848 6016 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 6017 Flag_Non_Static_Expr
6018 ("small requires static expression!", Expr);
d6f39728 6019 return;
6020
6021 else
6022 Small := Expr_Value_R (Expr);
6023
6024 if Small <= Ureal_0 then
6025 Error_Msg_N ("small value must be greater than zero", Expr);
6026 return;
6027 end if;
6028
6029 end if;
6030
6031 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
6032 Error_Msg_N
6033 ("small requires an ordinary fixed point type", Nam);
6034
6035 elsif Has_Small_Clause (U_Ent) then
6036 Error_Msg_N ("small already given for &", Nam);
6037
6038 elsif Small > Delta_Value (U_Ent) then
6039 Error_Msg_N
ce3e25d6 6040 ("small value must not be greater than delta value", Nam);
d6f39728 6041
6042 else
6043 Set_Small_Value (U_Ent, Small);
6044 Set_Small_Value (Implicit_Base, Small);
6045 Set_Has_Small_Clause (U_Ent);
6046 Set_Has_Small_Clause (Implicit_Base);
6047 Set_Has_Non_Standard_Rep (Implicit_Base);
6048 end if;
6049 end Small;
6050
d6f39728 6051 ------------------
6052 -- Storage_Pool --
6053 ------------------
6054
6055 -- Storage_Pool attribute definition clause
6056
99378362 6057 when Attribute_Simple_Storage_Pool
6058 | Attribute_Storage_Pool
6059 =>
6060 Storage_Pool : declare
d6f39728 6061 Pool : Entity_Id;
6b567c71 6062 T : Entity_Id;
d6f39728 6063
6064 begin
44e4341e 6065 if Ekind (U_Ent) = E_Access_Subprogram_Type then
6066 Error_Msg_N
6067 ("storage pool cannot be given for access-to-subprogram type",
6068 Nam);
6069 return;
6070
99378362 6071 elsif not Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
d6f39728 6072 then
44e4341e 6073 Error_Msg_N
6074 ("storage pool can only be given for access types", Nam);
d6f39728 6075 return;
6076
6077 elsif Is_Derived_Type (U_Ent) then
6078 Error_Msg_N
6079 ("storage pool cannot be given for a derived access type",
6080 Nam);
6081
ae888dbd 6082 elsif Duplicate_Clause then
d6f39728 6083 return;
6084
6085 elsif Present (Associated_Storage_Pool (U_Ent)) then
6086 Error_Msg_N ("storage pool already given for &", Nam);
6087 return;
6088 end if;
6089
6653b695 6090 -- Check for Storage_Size previously given
6091
6092 declare
6093 SS : constant Node_Id :=
6094 Get_Attribute_Definition_Clause
6095 (U_Ent, Attribute_Storage_Size);
6096 begin
6097 if Present (SS) then
6098 Check_Pool_Size_Clash (U_Ent, N, SS);
6099 end if;
6100 end;
6101
6102 -- Storage_Pool case
6103
b55f7641 6104 if Id = Attribute_Storage_Pool then
6105 Analyze_And_Resolve
6106 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
6107
6108 -- In the Simple_Storage_Pool case, we allow a variable of any
b15003c3 6109 -- simple storage pool type, so we Resolve without imposing an
b55f7641 6110 -- expected type.
6111
6112 else
6113 Analyze_And_Resolve (Expr);
6114
6115 if not Present (Get_Rep_Pragma
b15003c3 6116 (Etype (Expr), Name_Simple_Storage_Pool_Type))
b55f7641 6117 then
6118 Error_Msg_N
6119 ("expression must be of a simple storage pool type", Expr);
6120 end if;
6121 end if;
d6f39728 6122
8c5c7277 6123 if not Denotes_Variable (Expr) then
6124 Error_Msg_N ("storage pool must be a variable", Expr);
6125 return;
6126 end if;
6127
6b567c71 6128 if Nkind (Expr) = N_Type_Conversion then
6129 T := Etype (Expression (Expr));
6130 else
6131 T := Etype (Expr);
6132 end if;
6133
6134 -- The Stack_Bounded_Pool is used internally for implementing
d64221a7 6135 -- access types with a Storage_Size. Since it only work properly
6136 -- when used on one specific type, we need to check that it is not
6137 -- hijacked improperly:
6138
6b567c71 6139 -- type T is access Integer;
6140 -- for T'Storage_Size use n;
6141 -- type Q is access Float;
6142 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
6143
15ebb600 6144 if RTE_Available (RE_Stack_Bounded_Pool)
6145 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
6146 then
6147 Error_Msg_N ("non-shareable internal Pool", Expr);
6b567c71 6148 return;
6149 end if;
6150
d6f39728 6151 -- If the argument is a name that is not an entity name, then
6152 -- we construct a renaming operation to define an entity of
6153 -- type storage pool.
6154
6155 if not Is_Entity_Name (Expr)
6156 and then Is_Object_Reference (Expr)
6157 then
11deeeb6 6158 Pool := Make_Temporary (Loc, 'P', Expr);
d6f39728 6159
6160 declare
6161 Rnode : constant Node_Id :=
6162 Make_Object_Renaming_Declaration (Loc,
6163 Defining_Identifier => Pool,
6164 Subtype_Mark =>
6165 New_Occurrence_Of (Etype (Expr), Loc),
11deeeb6 6166 Name => Expr);
d6f39728 6167
6168 begin
f65f7fdf 6169 -- If the attribute definition clause comes from an aspect
6170 -- clause, then insert the renaming before the associated
6171 -- entity's declaration, since the attribute clause has
6172 -- not yet been appended to the declaration list.
6173
6174 if From_Aspect_Specification (N) then
6175 Insert_Before (Parent (Entity (N)), Rnode);
6176 else
6177 Insert_Before (N, Rnode);
6178 end if;
6179
d6f39728 6180 Analyze (Rnode);
6181 Set_Associated_Storage_Pool (U_Ent, Pool);
6182 end;
6183
6184 elsif Is_Entity_Name (Expr) then
6185 Pool := Entity (Expr);
6186
6187 -- If pool is a renamed object, get original one. This can
6188 -- happen with an explicit renaming, and within instances.
6189
6190 while Present (Renamed_Object (Pool))
6191 and then Is_Entity_Name (Renamed_Object (Pool))
6192 loop
6193 Pool := Entity (Renamed_Object (Pool));
6194 end loop;
6195
6196 if Present (Renamed_Object (Pool))
6197 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
6198 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
6199 then
6200 Pool := Entity (Expression (Renamed_Object (Pool)));
6201 end if;
6202
6b567c71 6203 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 6204
6205 elsif Nkind (Expr) = N_Type_Conversion
6206 and then Is_Entity_Name (Expression (Expr))
6207 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
6208 then
6209 Pool := Entity (Expression (Expr));
6b567c71 6210 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 6211
6212 else
6213 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
6214 return;
6215 end if;
99378362 6216 end Storage_Pool;
d6f39728 6217
44e4341e 6218 ------------------
6219 -- Storage_Size --
6220 ------------------
6221
6222 -- Storage_Size attribute definition clause
6223
6224 when Attribute_Storage_Size => Storage_Size : declare
6225 Btype : constant Entity_Id := Base_Type (U_Ent);
44e4341e 6226
6227 begin
6228 if Is_Task_Type (U_Ent) then
44e4341e 6229
39a0c1d3 6230 -- Check obsolescent (but never obsolescent if from aspect)
ceec4f7c 6231
6232 if not From_Aspect_Specification (N) then
6233 Check_Restriction (No_Obsolescent_Features, N);
6234
6235 if Warn_On_Obsolescent_Feature then
6236 Error_Msg_N
f74a102b 6237 ("?j?storage size clause for task is an obsolescent "
6238 & "feature (RM J.9)", N);
ceec4f7c 6239 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
6240 end if;
44e4341e 6241 end if;
6242
6243 FOnly := True;
6244 end if;
6245
6246 if not Is_Access_Type (U_Ent)
6247 and then Ekind (U_Ent) /= E_Task_Type
6248 then
6249 Error_Msg_N ("storage size cannot be given for &", Nam);
6250
6251 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6252 Error_Msg_N
6253 ("storage size cannot be given for a derived access type",
6254 Nam);
6255
ae888dbd 6256 elsif Duplicate_Clause then
6257 null;
44e4341e 6258
6259 else
6260 Analyze_And_Resolve (Expr, Any_Integer);
6261
6262 if Is_Access_Type (U_Ent) then
6653b695 6263
6264 -- Check for Storage_Pool previously given
6265
6266 declare
6267 SP : constant Node_Id :=
6268 Get_Attribute_Definition_Clause
6269 (U_Ent, Attribute_Storage_Pool);
6270
6271 begin
6272 if Present (SP) then
6273 Check_Pool_Size_Clash (U_Ent, SP, N);
6274 end if;
6275 end;
6276
6277 -- Special case of for x'Storage_Size use 0
44e4341e 6278
5941a4e9 6279 if Is_OK_Static_Expression (Expr)
44e4341e 6280 and then Expr_Value (Expr) = 0
6281 then
6282 Set_No_Pool_Assigned (Btype);
6283 end if;
44e4341e 6284 end if;
6285
6286 Set_Has_Storage_Size_Clause (Btype);
6287 end if;
6288 end Storage_Size;
6289
7189d17f 6290 -----------------
6291 -- Stream_Size --
6292 -----------------
6293
6294 when Attribute_Stream_Size => Stream_Size : declare
6295 Size : constant Uint := Static_Integer (Expr);
6296
6297 begin
15ebb600 6298 if Ada_Version <= Ada_95 then
6299 Check_Restriction (No_Implementation_Attributes, N);
6300 end if;
6301
ae888dbd 6302 if Duplicate_Clause then
6303 null;
7189d17f 6304
6305 elsif Is_Elementary_Type (U_Ent) then
f74a102b 6306
6307 -- The following errors are suppressed in ASIS mode to allow
f9906591 6308 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 6309 -- the illegal clause.
6310
6311 if ASIS_Mode then
6312 null;
6313
6314 elsif Size /= System_Storage_Unit
6315 and then Size /= System_Storage_Unit * 2
6316 and then Size /= System_Storage_Unit * 4
6317 and then Size /= System_Storage_Unit * 8
7189d17f 6318 then
6319 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6320 Error_Msg_N
f74a102b 6321 ("stream size for elementary type must be a power of 2 "
6322 & "and at least ^", N);
7189d17f 6323
6324 elsif RM_Size (U_Ent) > Size then
6325 Error_Msg_Uint_1 := RM_Size (U_Ent);
6326 Error_Msg_N
f74a102b 6327 ("stream size for elementary type must be a power of 2 "
6328 & "and at least ^", N);
7189d17f 6329 end if;
6330
6331 Set_Has_Stream_Size_Clause (U_Ent);
6332
6333 else
6334 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6335 end if;
6336 end Stream_Size;
6337
d6f39728 6338 ----------------
6339 -- Value_Size --
6340 ----------------
6341
6342 -- Value_Size attribute definition clause
6343
6344 when Attribute_Value_Size => Value_Size : declare
6345 Size : constant Uint := Static_Integer (Expr);
6346 Biased : Boolean;
6347
6348 begin
6349 if not Is_Type (U_Ent) then
6350 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6351
ae888dbd 6352 elsif Duplicate_Clause then
6353 null;
d6f39728 6354
59ac57b5 6355 elsif Is_Array_Type (U_Ent)
6356 and then not Is_Constrained (U_Ent)
6357 then
6358 Error_Msg_N
6359 ("Value_Size cannot be given for unconstrained array", Nam);
6360
d6f39728 6361 else
6362 if Is_Elementary_Type (U_Ent) then
6363 Check_Size (Expr, U_Ent, Size, Biased);
b77e4501 6364 Set_Biased (U_Ent, N, "value size clause", Biased);
d6f39728 6365 end if;
6366
6367 Set_RM_Size (U_Ent, Size);
6368 end if;
6369 end Value_Size;
6370
81b424ac 6371 -----------------------
6372 -- Variable_Indexing --
6373 -----------------------
6374
6375 when Attribute_Variable_Indexing =>
6376 Check_Indexing_Functions;
6377
d6f39728 6378 -----------
6379 -- Write --
6380 -----------
6381
9f373bb8 6382 when Attribute_Write =>
6383 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6384 Set_Has_Specified_Stream_Write (Ent);
d6f39728 6385
6386 -- All other attributes cannot be set
6387
6388 when others =>
6389 Error_Msg_N
6390 ("attribute& cannot be set with definition clause", N);
d6f39728 6391 end case;
6392
d64221a7 6393 -- The test for the type being frozen must be performed after any
6394 -- expression the clause has been analyzed since the expression itself
6395 -- might cause freezing that makes the clause illegal.
d6f39728 6396
6397 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6398 return;
6399 end if;
6400 end Analyze_Attribute_Definition_Clause;
6401
6402 ----------------------------
6403 -- Analyze_Code_Statement --
6404 ----------------------------
6405
6406 procedure Analyze_Code_Statement (N : Node_Id) is
6407 HSS : constant Node_Id := Parent (N);
6408 SBody : constant Node_Id := Parent (HSS);
6409 Subp : constant Entity_Id := Current_Scope;
6410 Stmt : Node_Id;
6411 Decl : Node_Id;
6412 StmtO : Node_Id;
6413 DeclO : Node_Id;
6414
6415 begin
1d3f0c6b 6416 -- Accept foreign code statements for CodePeer. The analysis is skipped
6417 -- to avoid rejecting unrecognized constructs.
6418
6419 if CodePeer_Mode then
6420 Set_Analyzed (N);
6421 return;
6422 end if;
6423
d6f39728 6424 -- Analyze and check we get right type, note that this implements the
1d3f0c6b 6425 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6426 -- the only way that Asm_Insn could possibly be visible.
d6f39728 6427
6428 Analyze_And_Resolve (Expression (N));
6429
6430 if Etype (Expression (N)) = Any_Type then
6431 return;
6432 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6433 Error_Msg_N ("incorrect type for code statement", N);
6434 return;
6435 end if;
6436
44e4341e 6437 Check_Code_Statement (N);
6438
1d3f0c6b 6439 -- Make sure we appear in the handled statement sequence of a subprogram
6440 -- (RM 13.8(3)).
d6f39728 6441
6442 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6443 or else Nkind (SBody) /= N_Subprogram_Body
6444 then
6445 Error_Msg_N
6446 ("code statement can only appear in body of subprogram", N);
6447 return;
6448 end if;
6449
6450 -- Do remaining checks (RM 13.8(3)) if not already done
6451
6452 if not Is_Machine_Code_Subprogram (Subp) then
6453 Set_Is_Machine_Code_Subprogram (Subp);
6454
6455 -- No exception handlers allowed
6456
6457 if Present (Exception_Handlers (HSS)) then
6458 Error_Msg_N
6459 ("exception handlers not permitted in machine code subprogram",
6460 First (Exception_Handlers (HSS)));
6461 end if;
6462
6463 -- No declarations other than use clauses and pragmas (we allow
6464 -- certain internally generated declarations as well).
6465
6466 Decl := First (Declarations (SBody));
6467 while Present (Decl) loop
6468 DeclO := Original_Node (Decl);
6469 if Comes_From_Source (DeclO)
fdd294d1 6470 and not Nkind_In (DeclO, N_Pragma,
6471 N_Use_Package_Clause,
6472 N_Use_Type_Clause,
6473 N_Implicit_Label_Declaration)
d6f39728 6474 then
6475 Error_Msg_N
6476 ("this declaration not allowed in machine code subprogram",
6477 DeclO);
6478 end if;
6479
6480 Next (Decl);
6481 end loop;
6482
6483 -- No statements other than code statements, pragmas, and labels.
6484 -- Again we allow certain internally generated statements.
3ab42ff7 6485
c3107527 6486 -- In Ada 2012, qualified expressions are names, and the code
6487 -- statement is initially parsed as a procedure call.
d6f39728 6488
6489 Stmt := First (Statements (HSS));
6490 while Present (Stmt) loop
6491 StmtO := Original_Node (Stmt);
c3107527 6492
1d3f0c6b 6493 -- A procedure call transformed into a code statement is OK
59f2fcab 6494
c3107527 6495 if Ada_Version >= Ada_2012
6496 and then Nkind (StmtO) = N_Procedure_Call_Statement
59f2fcab 6497 and then Nkind (Name (StmtO)) = N_Qualified_Expression
c3107527 6498 then
6499 null;
6500
6501 elsif Comes_From_Source (StmtO)
fdd294d1 6502 and then not Nkind_In (StmtO, N_Pragma,
6503 N_Label,
6504 N_Code_Statement)
d6f39728 6505 then
6506 Error_Msg_N
6507 ("this statement is not allowed in machine code subprogram",
6508 StmtO);
6509 end if;
6510
6511 Next (Stmt);
6512 end loop;
6513 end if;
d6f39728 6514 end Analyze_Code_Statement;
6515
6516 -----------------------------------------------
6517 -- Analyze_Enumeration_Representation_Clause --
6518 -----------------------------------------------
6519
6520 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
21647c2d 6521 Ident : constant Node_Id := Identifier (N);
6522 Aggr : constant Node_Id := Array_Aggregate (N);
d6f39728 6523 Enumtype : Entity_Id;
6524 Elit : Entity_Id;
6525 Expr : Node_Id;
6526 Assoc : Node_Id;
6527 Choice : Node_Id;
6528 Val : Uint;
b3190af0 6529
6530 Err : Boolean := False;
098d3082 6531 -- Set True to avoid cascade errors and crashes on incorrect source code
d6f39728 6532
e30c7d84 6533 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6534 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6535 -- Allowed range of universal integer (= allowed range of enum lit vals)
6536
d6f39728 6537 Min : Uint;
6538 Max : Uint;
e30c7d84 6539 -- Minimum and maximum values of entries
6540
6541 Max_Node : Node_Id;
6542 -- Pointer to node for literal providing max value
d6f39728 6543
6544 begin
ca301e17 6545 if Ignore_Rep_Clauses then
2ff55065 6546 Kill_Rep_Clause (N);
fbc67f84 6547 return;
6548 end if;
6549
175a6969 6550 -- Ignore enumeration rep clauses by default in CodePeer mode,
6551 -- unless -gnatd.I is specified, as a work around for potential false
6552 -- positive messages.
6553
6554 if CodePeer_Mode and not Debug_Flag_Dot_II then
6555 return;
6556 end if;
6557
d6f39728 6558 -- First some basic error checks
6559
6560 Find_Type (Ident);
6561 Enumtype := Entity (Ident);
6562
6563 if Enumtype = Any_Type
6564 or else Rep_Item_Too_Early (Enumtype, N)
6565 then
6566 return;
6567 else
6568 Enumtype := Underlying_Type (Enumtype);
6569 end if;
6570
6571 if not Is_Enumeration_Type (Enumtype) then
6572 Error_Msg_NE
6573 ("enumeration type required, found}",
6574 Ident, First_Subtype (Enumtype));
6575 return;
6576 end if;
6577
9dfe12ae 6578 -- Ignore rep clause on generic actual type. This will already have
6579 -- been flagged on the template as an error, and this is the safest
6580 -- way to ensure we don't get a junk cascaded message in the instance.
6581
6582 if Is_Generic_Actual_Type (Enumtype) then
6583 return;
6584
6585 -- Type must be in current scope
6586
6587 elsif Scope (Enumtype) /= Current_Scope then
d6f39728 6588 Error_Msg_N ("type must be declared in this scope", Ident);
6589 return;
6590
9dfe12ae 6591 -- Type must be a first subtype
6592
d6f39728 6593 elsif not Is_First_Subtype (Enumtype) then
6594 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6595 return;
6596
9dfe12ae 6597 -- Ignore duplicate rep clause
6598
d6f39728 6599 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6600 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6601 return;
6602
7189d17f 6603 -- Don't allow rep clause for standard [wide_[wide_]]character
9dfe12ae 6604
177675a7 6605 elsif Is_Standard_Character_Type (Enumtype) then
d6f39728 6606 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
9dfe12ae 6607 return;
6608
d9125581 6609 -- Check that the expression is a proper aggregate (no parentheses)
6610
6611 elsif Paren_Count (Aggr) /= 0 then
6612 Error_Msg
6613 ("extra parentheses surrounding aggregate not allowed",
6614 First_Sloc (Aggr));
6615 return;
6616
9dfe12ae 6617 -- All tests passed, so set rep clause in place
d6f39728 6618
6619 else
6620 Set_Has_Enumeration_Rep_Clause (Enumtype);
6621 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6622 end if;
6623
6624 -- Now we process the aggregate. Note that we don't use the normal
6625 -- aggregate code for this purpose, because we don't want any of the
6626 -- normal expansion activities, and a number of special semantic
6627 -- rules apply (including the component type being any integer type)
6628
d6f39728 6629 Elit := First_Literal (Enumtype);
6630
6631 -- First the positional entries if any
6632
6633 if Present (Expressions (Aggr)) then
6634 Expr := First (Expressions (Aggr));
6635 while Present (Expr) loop
6636 if No (Elit) then
6637 Error_Msg_N ("too many entries in aggregate", Expr);
6638 return;
6639 end if;
6640
6641 Val := Static_Integer (Expr);
6642
d9125581 6643 -- Err signals that we found some incorrect entries processing
6644 -- the list. The final checks for completeness and ordering are
6645 -- skipped in this case.
6646
d6f39728 6647 if Val = No_Uint then
6648 Err := True;
f02a9a9a 6649
d6f39728 6650 elsif Val < Lo or else Hi < Val then
6651 Error_Msg_N ("value outside permitted range", Expr);
6652 Err := True;
6653 end if;
6654
6655 Set_Enumeration_Rep (Elit, Val);
6656 Set_Enumeration_Rep_Expr (Elit, Expr);
6657 Next (Expr);
6658 Next (Elit);
6659 end loop;
6660 end if;
6661
6662 -- Now process the named entries if present
6663
6664 if Present (Component_Associations (Aggr)) then
6665 Assoc := First (Component_Associations (Aggr));
6666 while Present (Assoc) loop
6667 Choice := First (Choices (Assoc));
6668
6669 if Present (Next (Choice)) then
6670 Error_Msg_N
6671 ("multiple choice not allowed here", Next (Choice));
6672 Err := True;
6673 end if;
6674
6675 if Nkind (Choice) = N_Others_Choice then
6676 Error_Msg_N ("others choice not allowed here", Choice);
6677 Err := True;
6678
6679 elsif Nkind (Choice) = N_Range then
b3190af0 6680
d6f39728 6681 -- ??? should allow zero/one element range here
b3190af0 6682
d6f39728 6683 Error_Msg_N ("range not allowed here", Choice);
6684 Err := True;
6685
6686 else
6687 Analyze_And_Resolve (Choice, Enumtype);
b3190af0 6688
098d3082 6689 if Error_Posted (Choice) then
d6f39728 6690 Err := True;
098d3082 6691 end if;
d6f39728 6692
098d3082 6693 if not Err then
6694 if Is_Entity_Name (Choice)
6695 and then Is_Type (Entity (Choice))
6696 then
6697 Error_Msg_N ("subtype name not allowed here", Choice);
d6f39728 6698 Err := True;
b3190af0 6699
098d3082 6700 -- ??? should allow static subtype with zero/one entry
d6f39728 6701
098d3082 6702 elsif Etype (Choice) = Base_Type (Enumtype) then
cda40848 6703 if not Is_OK_Static_Expression (Choice) then
098d3082 6704 Flag_Non_Static_Expr
6705 ("non-static expression used for choice!", Choice);
d6f39728 6706 Err := True;
d6f39728 6707
098d3082 6708 else
6709 Elit := Expr_Value_E (Choice);
6710
6711 if Present (Enumeration_Rep_Expr (Elit)) then
6712 Error_Msg_Sloc :=
6713 Sloc (Enumeration_Rep_Expr (Elit));
6714 Error_Msg_NE
6715 ("representation for& previously given#",
6716 Choice, Elit);
6717 Err := True;
6718 end if;
d6f39728 6719
098d3082 6720 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
d6f39728 6721
098d3082 6722 Expr := Expression (Assoc);
6723 Val := Static_Integer (Expr);
d6f39728 6724
098d3082 6725 if Val = No_Uint then
6726 Err := True;
6727
6728 elsif Val < Lo or else Hi < Val then
6729 Error_Msg_N ("value outside permitted range", Expr);
6730 Err := True;
6731 end if;
d6f39728 6732
098d3082 6733 Set_Enumeration_Rep (Elit, Val);
6734 end if;
d6f39728 6735 end if;
6736 end if;
6737 end if;
6738
6739 Next (Assoc);
6740 end loop;
6741 end if;
6742
6743 -- Aggregate is fully processed. Now we check that a full set of
6744 -- representations was given, and that they are in range and in order.
6745 -- These checks are only done if no other errors occurred.
6746
6747 if not Err then
6748 Min := No_Uint;
6749 Max := No_Uint;
6750
6751 Elit := First_Literal (Enumtype);
6752 while Present (Elit) loop
6753 if No (Enumeration_Rep_Expr (Elit)) then
6754 Error_Msg_NE ("missing representation for&!", N, Elit);
6755
6756 else
6757 Val := Enumeration_Rep (Elit);
6758
6759 if Min = No_Uint then
6760 Min := Val;
6761 end if;
6762
6763 if Val /= No_Uint then
6764 if Max /= No_Uint and then Val <= Max then
6765 Error_Msg_NE
6766 ("enumeration value for& not ordered!",
e30c7d84 6767 Enumeration_Rep_Expr (Elit), Elit);
d6f39728 6768 end if;
6769
e30c7d84 6770 Max_Node := Enumeration_Rep_Expr (Elit);
d6f39728 6771 Max := Val;
6772 end if;
6773
e30c7d84 6774 -- If there is at least one literal whose representation is not
6775 -- equal to the Pos value, then note that this enumeration type
6776 -- has a non-standard representation.
d6f39728 6777
6778 if Val /= Enumeration_Pos (Elit) then
6779 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6780 end if;
6781 end if;
6782
6783 Next (Elit);
6784 end loop;
6785
6786 -- Now set proper size information
6787
6788 declare
6789 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6790
6791 begin
6792 if Has_Size_Clause (Enumtype) then
e30c7d84 6793
6794 -- All OK, if size is OK now
6795
6796 if RM_Size (Enumtype) >= Minsize then
d6f39728 6797 null;
6798
6799 else
e30c7d84 6800 -- Try if we can get by with biasing
6801
d6f39728 6802 Minsize :=
6803 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6804
e30c7d84 6805 -- Error message if even biasing does not work
6806
6807 if RM_Size (Enumtype) < Minsize then
6808 Error_Msg_Uint_1 := RM_Size (Enumtype);
6809 Error_Msg_Uint_2 := Max;
6810 Error_Msg_N
6811 ("previously given size (^) is too small "
6812 & "for this value (^)", Max_Node);
6813
6814 -- If biasing worked, indicate that we now have biased rep
d6f39728 6815
6816 else
b77e4501 6817 Set_Biased
6818 (Enumtype, Size_Clause (Enumtype), "size clause");
d6f39728 6819 end if;
6820 end if;
6821
6822 else
6823 Set_RM_Size (Enumtype, Minsize);
6824 Set_Enum_Esize (Enumtype);
6825 end if;
6826
6827 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6828 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6829 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6830 end;
6831 end if;
6832
39a0c1d3 6833 -- We repeat the too late test in case it froze itself
d6f39728 6834
6835 if Rep_Item_Too_Late (Enumtype, N) then
6836 null;
6837 end if;
d6f39728 6838 end Analyze_Enumeration_Representation_Clause;
6839
6840 ----------------------------
6841 -- Analyze_Free_Statement --
6842 ----------------------------
6843
6844 procedure Analyze_Free_Statement (N : Node_Id) is
6845 begin
6846 Analyze (Expression (N));
6847 end Analyze_Free_Statement;
6848
40ca69b9 6849 ---------------------------
6850 -- Analyze_Freeze_Entity --
6851 ---------------------------
6852
6853 procedure Analyze_Freeze_Entity (N : Node_Id) is
40ca69b9 6854 begin
d9f6a4ee 6855 Freeze_Entity_Checks (N);
6856 end Analyze_Freeze_Entity;
98f7db28 6857
d9f6a4ee 6858 -----------------------------------
6859 -- Analyze_Freeze_Generic_Entity --
6860 -----------------------------------
98f7db28 6861
d9f6a4ee 6862 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
61989dbb 6863 E : constant Entity_Id := Entity (N);
6864
d9f6a4ee 6865 begin
61989dbb 6866 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
6867 Analyze_Aspects_At_Freeze_Point (E);
6868 end if;
6869
d9f6a4ee 6870 Freeze_Entity_Checks (N);
6871 end Analyze_Freeze_Generic_Entity;
40ca69b9 6872
d9f6a4ee 6873 ------------------------------------------
6874 -- Analyze_Record_Representation_Clause --
6875 ------------------------------------------
c8da6114 6876
d9f6a4ee 6877 -- Note: we check as much as we can here, but we can't do any checks
6878 -- based on the position values (e.g. overlap checks) until freeze time
6879 -- because especially in Ada 2005 (machine scalar mode), the processing
6880 -- for non-standard bit order can substantially change the positions.
6881 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6882 -- for the remainder of this processing.
d00681a7 6883
d9f6a4ee 6884 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6885 Ident : constant Node_Id := Identifier (N);
6886 Biased : Boolean;
6887 CC : Node_Id;
6888 Comp : Entity_Id;
6889 Fbit : Uint;
6890 Hbit : Uint := Uint_0;
6891 Lbit : Uint;
6892 Ocomp : Entity_Id;
6893 Posit : Uint;
6894 Rectype : Entity_Id;
6895 Recdef : Node_Id;
d00681a7 6896
d9f6a4ee 6897 function Is_Inherited (Comp : Entity_Id) return Boolean;
6898 -- True if Comp is an inherited component in a record extension
d00681a7 6899
d9f6a4ee 6900 ------------------
6901 -- Is_Inherited --
6902 ------------------
d00681a7 6903
d9f6a4ee 6904 function Is_Inherited (Comp : Entity_Id) return Boolean is
6905 Comp_Base : Entity_Id;
d00681a7 6906
d9f6a4ee 6907 begin
6908 if Ekind (Rectype) = E_Record_Subtype then
6909 Comp_Base := Original_Record_Component (Comp);
6910 else
6911 Comp_Base := Comp;
d00681a7 6912 end if;
6913
d9f6a4ee 6914 return Comp_Base /= Original_Record_Component (Comp_Base);
6915 end Is_Inherited;
d00681a7 6916
d9f6a4ee 6917 -- Local variables
d00681a7 6918
d9f6a4ee 6919 Is_Record_Extension : Boolean;
6920 -- True if Rectype is a record extension
d00681a7 6921
d9f6a4ee 6922 CR_Pragma : Node_Id := Empty;
6923 -- Points to N_Pragma node if Complete_Representation pragma present
d00681a7 6924
d9f6a4ee 6925 -- Start of processing for Analyze_Record_Representation_Clause
d00681a7 6926
d9f6a4ee 6927 begin
6928 if Ignore_Rep_Clauses then
2ff55065 6929 Kill_Rep_Clause (N);
d9f6a4ee 6930 return;
d00681a7 6931 end if;
98f7db28 6932
d9f6a4ee 6933 Find_Type (Ident);
6934 Rectype := Entity (Ident);
85377c9b 6935
d9f6a4ee 6936 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6937 return;
6938 else
6939 Rectype := Underlying_Type (Rectype);
6940 end if;
85377c9b 6941
d9f6a4ee 6942 -- First some basic error checks
85377c9b 6943
d9f6a4ee 6944 if not Is_Record_Type (Rectype) then
6945 Error_Msg_NE
6946 ("record type required, found}", Ident, First_Subtype (Rectype));
6947 return;
85377c9b 6948
d9f6a4ee 6949 elsif Scope (Rectype) /= Current_Scope then
6950 Error_Msg_N ("type must be declared in this scope", N);
6951 return;
85377c9b 6952
d9f6a4ee 6953 elsif not Is_First_Subtype (Rectype) then
6954 Error_Msg_N ("cannot give record rep clause for subtype", N);
6955 return;
9dc88aea 6956
d9f6a4ee 6957 elsif Has_Record_Rep_Clause (Rectype) then
6958 Error_Msg_N ("duplicate record rep clause ignored", N);
6959 return;
9dc88aea 6960
d9f6a4ee 6961 elsif Rep_Item_Too_Late (Rectype, N) then
6962 return;
9dc88aea 6963 end if;
fb7f2fc4 6964
2ced3742 6965 -- We know we have a first subtype, now possibly go to the anonymous
d9f6a4ee 6966 -- base type to determine whether Rectype is a record extension.
89f1e35c 6967
d9f6a4ee 6968 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6969 Is_Record_Extension :=
6970 Nkind (Recdef) = N_Derived_Type_Definition
6971 and then Present (Record_Extension_Part (Recdef));
89f1e35c 6972
d9f6a4ee 6973 if Present (Mod_Clause (N)) then
fb7f2fc4 6974 declare
d9f6a4ee 6975 Loc : constant Source_Ptr := Sloc (N);
6976 M : constant Node_Id := Mod_Clause (N);
6977 P : constant List_Id := Pragmas_Before (M);
6978 AtM_Nod : Node_Id;
6979
6980 Mod_Val : Uint;
6981 pragma Warnings (Off, Mod_Val);
fb7f2fc4 6982
6983 begin
d9f6a4ee 6984 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
fb7f2fc4 6985
d9f6a4ee 6986 if Warn_On_Obsolescent_Feature then
6987 Error_Msg_N
6988 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6989 Error_Msg_N
6990 ("\?j?use alignment attribute definition clause instead", N);
6991 end if;
fb7f2fc4 6992
d9f6a4ee 6993 if Present (P) then
6994 Analyze_List (P);
6995 end if;
89f1e35c 6996
d9f6a4ee 6997 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6998 -- the Mod clause into an alignment clause anyway, so that the
3ff5e35d 6999 -- back end can compute and back-annotate properly the size and
d9f6a4ee 7000 -- alignment of types that may include this record.
be9124d0 7001
d9f6a4ee 7002 -- This seems dubious, this destroys the source tree in a manner
7003 -- not detectable by ASIS ???
be9124d0 7004
d9f6a4ee 7005 if Operating_Mode = Check_Semantics and then ASIS_Mode then
7006 AtM_Nod :=
7007 Make_Attribute_Definition_Clause (Loc,
83c6c069 7008 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
d9f6a4ee 7009 Chars => Name_Alignment,
7010 Expression => Relocate_Node (Expression (M)));
be9124d0 7011
d9f6a4ee 7012 Set_From_At_Mod (AtM_Nod);
7013 Insert_After (N, AtM_Nod);
7014 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
7015 Set_Mod_Clause (N, Empty);
be9124d0 7016
d9f6a4ee 7017 else
7018 -- Get the alignment value to perform error checking
be9124d0 7019
d9f6a4ee 7020 Mod_Val := Get_Alignment_Value (Expression (M));
7021 end if;
7022 end;
7023 end if;
be9124d0 7024
d9f6a4ee 7025 -- For untagged types, clear any existing component clauses for the
7026 -- type. If the type is derived, this is what allows us to override
7027 -- a rep clause for the parent. For type extensions, the representation
7028 -- of the inherited components is inherited, so we want to keep previous
7029 -- component clauses for completeness.
be9124d0 7030
d9f6a4ee 7031 if not Is_Tagged_Type (Rectype) then
7032 Comp := First_Component_Or_Discriminant (Rectype);
7033 while Present (Comp) loop
7034 Set_Component_Clause (Comp, Empty);
7035 Next_Component_Or_Discriminant (Comp);
7036 end loop;
7037 end if;
be9124d0 7038
d9f6a4ee 7039 -- All done if no component clauses
be9124d0 7040
d9f6a4ee 7041 CC := First (Component_Clauses (N));
be9124d0 7042
d9f6a4ee 7043 if No (CC) then
7044 return;
7045 end if;
be9124d0 7046
d9f6a4ee 7047 -- A representation like this applies to the base type
be9124d0 7048
d9f6a4ee 7049 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
7050 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
7051 Set_Has_Specified_Layout (Base_Type (Rectype));
be9124d0 7052
d9f6a4ee 7053 -- Process the component clauses
be9124d0 7054
d9f6a4ee 7055 while Present (CC) loop
be9124d0 7056
d9f6a4ee 7057 -- Pragma
be9124d0 7058
d9f6a4ee 7059 if Nkind (CC) = N_Pragma then
7060 Analyze (CC);
be9124d0 7061
d9f6a4ee 7062 -- The only pragma of interest is Complete_Representation
be9124d0 7063
ddccc924 7064 if Pragma_Name (CC) = Name_Complete_Representation then
d9f6a4ee 7065 CR_Pragma := CC;
7066 end if;
be9124d0 7067
d9f6a4ee 7068 -- Processing for real component clause
be9124d0 7069
d9f6a4ee 7070 else
7071 Posit := Static_Integer (Position (CC));
7072 Fbit := Static_Integer (First_Bit (CC));
7073 Lbit := Static_Integer (Last_Bit (CC));
be9124d0 7074
d9f6a4ee 7075 if Posit /= No_Uint
7076 and then Fbit /= No_Uint
7077 and then Lbit /= No_Uint
7078 then
7079 if Posit < 0 then
f74a102b 7080 Error_Msg_N ("position cannot be negative", Position (CC));
be9124d0 7081
d9f6a4ee 7082 elsif Fbit < 0 then
f74a102b 7083 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
be9124d0 7084
d9f6a4ee 7085 -- The Last_Bit specified in a component clause must not be
7086 -- less than the First_Bit minus one (RM-13.5.1(10)).
be9124d0 7087
d9f6a4ee 7088 elsif Lbit < Fbit - 1 then
7089 Error_Msg_N
7090 ("last bit cannot be less than first bit minus one",
7091 Last_Bit (CC));
be9124d0 7092
d9f6a4ee 7093 -- Values look OK, so find the corresponding record component
7094 -- Even though the syntax allows an attribute reference for
7095 -- implementation-defined components, GNAT does not allow the
7096 -- tag to get an explicit position.
be9124d0 7097
d9f6a4ee 7098 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
7099 if Attribute_Name (Component_Name (CC)) = Name_Tag then
7100 Error_Msg_N ("position of tag cannot be specified", CC);
7101 else
7102 Error_Msg_N ("illegal component name", CC);
7103 end if;
be9124d0 7104
d9f6a4ee 7105 else
7106 Comp := First_Entity (Rectype);
7107 while Present (Comp) loop
7108 exit when Chars (Comp) = Chars (Component_Name (CC));
7109 Next_Entity (Comp);
7110 end loop;
be9124d0 7111
d9f6a4ee 7112 if No (Comp) then
be9124d0 7113
d9f6a4ee 7114 -- Maybe component of base type that is absent from
7115 -- statically constrained first subtype.
be9124d0 7116
d9f6a4ee 7117 Comp := First_Entity (Base_Type (Rectype));
7118 while Present (Comp) loop
7119 exit when Chars (Comp) = Chars (Component_Name (CC));
7120 Next_Entity (Comp);
7121 end loop;
7122 end if;
be9124d0 7123
d9f6a4ee 7124 if No (Comp) then
7125 Error_Msg_N
7126 ("component clause is for non-existent field", CC);
be9124d0 7127
d9f6a4ee 7128 -- Ada 2012 (AI05-0026): Any name that denotes a
7129 -- discriminant of an object of an unchecked union type
7130 -- shall not occur within a record_representation_clause.
be9124d0 7131
d9f6a4ee 7132 -- The general restriction of using record rep clauses on
7133 -- Unchecked_Union types has now been lifted. Since it is
7134 -- possible to introduce a record rep clause which mentions
7135 -- the discriminant of an Unchecked_Union in non-Ada 2012
7136 -- code, this check is applied to all versions of the
7137 -- language.
be9124d0 7138
d9f6a4ee 7139 elsif Ekind (Comp) = E_Discriminant
7140 and then Is_Unchecked_Union (Rectype)
7141 then
7142 Error_Msg_N
7143 ("cannot reference discriminant of unchecked union",
7144 Component_Name (CC));
be9124d0 7145
d9f6a4ee 7146 elsif Is_Record_Extension and then Is_Inherited (Comp) then
7147 Error_Msg_NE
7148 ("component clause not allowed for inherited "
7149 & "component&", CC, Comp);
40ca69b9 7150
d9f6a4ee 7151 elsif Present (Component_Clause (Comp)) then
462a079f 7152
d9f6a4ee 7153 -- Diagnose duplicate rep clause, or check consistency
7154 -- if this is an inherited component. In a double fault,
7155 -- there may be a duplicate inconsistent clause for an
7156 -- inherited component.
462a079f 7157
d9f6a4ee 7158 if Scope (Original_Record_Component (Comp)) = Rectype
7159 or else Parent (Component_Clause (Comp)) = N
7160 then
7161 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
7162 Error_Msg_N ("component clause previously given#", CC);
3062c401 7163
7164 else
7165 declare
7166 Rep1 : constant Node_Id := Component_Clause (Comp);
3062c401 7167 begin
7168 if Intval (Position (Rep1)) /=
7169 Intval (Position (CC))
7170 or else Intval (First_Bit (Rep1)) /=
7171 Intval (First_Bit (CC))
7172 or else Intval (Last_Bit (Rep1)) /=
7173 Intval (Last_Bit (CC))
7174 then
b9e61b2a 7175 Error_Msg_N
f74a102b 7176 ("component clause inconsistent with "
7177 & "representation of ancestor", CC);
6a06584c 7178
3062c401 7179 elsif Warn_On_Redundant_Constructs then
b9e61b2a 7180 Error_Msg_N
6a06584c 7181 ("?r?redundant confirming component clause "
7182 & "for component!", CC);
3062c401 7183 end if;
7184 end;
7185 end if;
d6f39728 7186
d2b860b4 7187 -- Normal case where this is the first component clause we
7188 -- have seen for this entity, so set it up properly.
7189
d6f39728 7190 else
83f8f0a6 7191 -- Make reference for field in record rep clause and set
7192 -- appropriate entity field in the field identifier.
7193
7194 Generate_Reference
7195 (Comp, Component_Name (CC), Set_Ref => False);
7196 Set_Entity (Component_Name (CC), Comp);
7197
2866d595 7198 -- Update Fbit and Lbit to the actual bit number
d6f39728 7199
7200 Fbit := Fbit + UI_From_Int (SSU) * Posit;
7201 Lbit := Lbit + UI_From_Int (SSU) * Posit;
7202
d6f39728 7203 if Has_Size_Clause (Rectype)
ada34def 7204 and then RM_Size (Rectype) <= Lbit
d6f39728 7205 then
7206 Error_Msg_N
7207 ("bit number out of range of specified size",
7208 Last_Bit (CC));
7209 else
7210 Set_Component_Clause (Comp, CC);
7211 Set_Component_Bit_Offset (Comp, Fbit);
7212 Set_Esize (Comp, 1 + (Lbit - Fbit));
7213 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
7214 Set_Normalized_Position (Comp, Fbit / SSU);
7215
a0fc8c5b 7216 if Warn_On_Overridden_Size
7217 and then Has_Size_Clause (Etype (Comp))
7218 and then RM_Size (Etype (Comp)) /= Esize (Comp)
7219 then
7220 Error_Msg_NE
1e3532e7 7221 ("?S?component size overrides size clause for&",
a0fc8c5b 7222 Component_Name (CC), Etype (Comp));
7223 end if;
7224
ea61a7ea 7225 -- This information is also set in the corresponding
7226 -- component of the base type, found by accessing the
7227 -- Original_Record_Component link if it is present.
d6f39728 7228
7229 Ocomp := Original_Record_Component (Comp);
7230
7231 if Hbit < Lbit then
7232 Hbit := Lbit;
7233 end if;
7234
7235 Check_Size
7236 (Component_Name (CC),
7237 Etype (Comp),
7238 Esize (Comp),
7239 Biased);
7240
b77e4501 7241 Set_Biased
7242 (Comp, First_Node (CC), "component clause", Biased);
cc46ff4b 7243
d6f39728 7244 if Present (Ocomp) then
7245 Set_Component_Clause (Ocomp, CC);
7246 Set_Component_Bit_Offset (Ocomp, Fbit);
7247 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
7248 Set_Normalized_Position (Ocomp, Fbit / SSU);
7249 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7250
7251 Set_Normalized_Position_Max
7252 (Ocomp, Normalized_Position (Ocomp));
7253
b77e4501 7254 -- Note: we don't use Set_Biased here, because we
7255 -- already gave a warning above if needed, and we
7256 -- would get a duplicate for the same name here.
7257
d6f39728 7258 Set_Has_Biased_Representation
7259 (Ocomp, Has_Biased_Representation (Comp));
7260 end if;
7261
7262 if Esize (Comp) < 0 then
7263 Error_Msg_N ("component size is negative", CC);
7264 end if;
7265 end if;
7266 end if;
7267 end if;
7268 end if;
7269 end if;
7270
7271 Next (CC);
7272 end loop;
7273
67278d60 7274 -- Check missing components if Complete_Representation pragma appeared
d6f39728 7275
67278d60 7276 if Present (CR_Pragma) then
7277 Comp := First_Component_Or_Discriminant (Rectype);
7278 while Present (Comp) loop
7279 if No (Component_Clause (Comp)) then
7280 Error_Msg_NE
7281 ("missing component clause for &", CR_Pragma, Comp);
7282 end if;
d6f39728 7283
67278d60 7284 Next_Component_Or_Discriminant (Comp);
7285 end loop;
d6f39728 7286
1e3532e7 7287 -- Give missing components warning if required
15ebb600 7288
fdd294d1 7289 elsif Warn_On_Unrepped_Components then
15ebb600 7290 declare
7291 Num_Repped_Components : Nat := 0;
7292 Num_Unrepped_Components : Nat := 0;
7293
7294 begin
7295 -- First count number of repped and unrepped components
7296
7297 Comp := First_Component_Or_Discriminant (Rectype);
7298 while Present (Comp) loop
7299 if Present (Component_Clause (Comp)) then
7300 Num_Repped_Components := Num_Repped_Components + 1;
7301 else
7302 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7303 end if;
7304
7305 Next_Component_Or_Discriminant (Comp);
7306 end loop;
7307
7308 -- We are only interested in the case where there is at least one
7309 -- unrepped component, and at least half the components have rep
7310 -- clauses. We figure that if less than half have them, then the
87f9eef5 7311 -- partial rep clause is really intentional. If the component
7312 -- type has no underlying type set at this point (as for a generic
7313 -- formal type), we don't know enough to give a warning on the
7314 -- component.
15ebb600 7315
7316 if Num_Unrepped_Components > 0
7317 and then Num_Unrepped_Components < Num_Repped_Components
7318 then
7319 Comp := First_Component_Or_Discriminant (Rectype);
7320 while Present (Comp) loop
83f8f0a6 7321 if No (Component_Clause (Comp))
3062c401 7322 and then Comes_From_Source (Comp)
87f9eef5 7323 and then Present (Underlying_Type (Etype (Comp)))
83f8f0a6 7324 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
67278d60 7325 or else Size_Known_At_Compile_Time
7326 (Underlying_Type (Etype (Comp))))
fdd294d1 7327 and then not Has_Warnings_Off (Rectype)
2be1f7d7 7328
7329 -- Ignore discriminant in unchecked union, since it is
7330 -- not there, and cannot have a component clause.
7331
7332 and then (not Is_Unchecked_Union (Rectype)
7333 or else Ekind (Comp) /= E_Discriminant)
83f8f0a6 7334 then
15ebb600 7335 Error_Msg_Sloc := Sloc (Comp);
7336 Error_Msg_NE
1e3532e7 7337 ("?C?no component clause given for & declared #",
15ebb600 7338 N, Comp);
7339 end if;
7340
7341 Next_Component_Or_Discriminant (Comp);
7342 end loop;
7343 end if;
7344 end;
d6f39728 7345 end if;
d6f39728 7346 end Analyze_Record_Representation_Clause;
7347
eb66e842 7348 -------------------------------------
7349 -- Build_Discrete_Static_Predicate --
7350 -------------------------------------
9ea61fdd 7351
eb66e842 7352 procedure Build_Discrete_Static_Predicate
7353 (Typ : Entity_Id;
7354 Expr : Node_Id;
7355 Nam : Name_Id)
9ea61fdd 7356 is
eb66e842 7357 Loc : constant Source_Ptr := Sloc (Expr);
9ea61fdd 7358
eb66e842 7359 Non_Static : exception;
7360 -- Raised if something non-static is found
9ea61fdd 7361
eb66e842 7362 Btyp : constant Entity_Id := Base_Type (Typ);
9ea61fdd 7363
eb66e842 7364 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7365 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7366 -- Low bound and high bound value of base type of Typ
9ea61fdd 7367
afc229da 7368 TLo : Uint;
7369 THi : Uint;
7370 -- Bounds for constructing the static predicate. We use the bound of the
7371 -- subtype if it is static, otherwise the corresponding base type bound.
7372 -- Note: a non-static subtype can have a static predicate.
9ea61fdd 7373
eb66e842 7374 type REnt is record
7375 Lo, Hi : Uint;
7376 end record;
7377 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7378 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7379 -- value.
9ea61fdd 7380
eb66e842 7381 type RList is array (Nat range <>) of REnt;
7382 -- A list of ranges. The ranges are sorted in increasing order, and are
7383 -- disjoint (there is a gap of at least one value between each range in
7384 -- the table). A value is in the set of ranges in Rlist if it lies
7385 -- within one of these ranges.
9ea61fdd 7386
eb66e842 7387 False_Range : constant RList :=
7388 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7389 -- An empty set of ranges represents a range list that can never be
7390 -- satisfied, since there are no ranges in which the value could lie,
7391 -- so it does not lie in any of them. False_Range is a canonical value
7392 -- for this empty set, but general processing should test for an Rlist
7393 -- with length zero (see Is_False predicate), since other null ranges
7394 -- may appear which must be treated as False.
5b5df4a9 7395
eb66e842 7396 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7397 -- Range representing True, value must be in the base range
5b5df4a9 7398
eb66e842 7399 function "and" (Left : RList; Right : RList) return RList;
7400 -- And's together two range lists, returning a range list. This is a set
7401 -- intersection operation.
5b5df4a9 7402
eb66e842 7403 function "or" (Left : RList; Right : RList) return RList;
7404 -- Or's together two range lists, returning a range list. This is a set
7405 -- union operation.
87f3d5d3 7406
eb66e842 7407 function "not" (Right : RList) return RList;
7408 -- Returns complement of a given range list, i.e. a range list
7409 -- representing all the values in TLo .. THi that are not in the input
7410 -- operand Right.
ed4adc99 7411
eb66e842 7412 function Build_Val (V : Uint) return Node_Id;
7413 -- Return an analyzed N_Identifier node referencing this value, suitable
5c6a5792 7414 -- for use as an entry in the Static_Discrte_Predicate list. This node
7415 -- is typed with the base type.
5b5df4a9 7416
eb66e842 7417 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7418 -- Return an analyzed N_Range node referencing this range, suitable for
5c6a5792 7419 -- use as an entry in the Static_Discrete_Predicate list. This node is
7420 -- typed with the base type.
5b5df4a9 7421
eb66e842 7422 function Get_RList (Exp : Node_Id) return RList;
7423 -- This is a recursive routine that converts the given expression into a
7424 -- list of ranges, suitable for use in building the static predicate.
5b5df4a9 7425
eb66e842 7426 function Is_False (R : RList) return Boolean;
7427 pragma Inline (Is_False);
7428 -- Returns True if the given range list is empty, and thus represents a
7429 -- False list of ranges that can never be satisfied.
87f3d5d3 7430
eb66e842 7431 function Is_True (R : RList) return Boolean;
7432 -- Returns True if R trivially represents the True predicate by having a
7433 -- single range from BLo to BHi.
5b5df4a9 7434
eb66e842 7435 function Is_Type_Ref (N : Node_Id) return Boolean;
7436 pragma Inline (Is_Type_Ref);
7437 -- Returns if True if N is a reference to the type for the predicate in
7438 -- the expression (i.e. if it is an identifier whose Chars field matches
7de4cba3 7439 -- the Nam given in the call). N must not be parenthesized, if the type
7440 -- name appears in parens, this routine will return False.
5b5df4a9 7441
eb66e842 7442 function Lo_Val (N : Node_Id) return Uint;
5c6a5792 7443 -- Given an entry from a Static_Discrete_Predicate list that is either
7444 -- a static expression or static range, gets either the expression value
7445 -- or the low bound of the range.
5b5df4a9 7446
eb66e842 7447 function Hi_Val (N : Node_Id) return Uint;
5c6a5792 7448 -- Given an entry from a Static_Discrete_Predicate list that is either
7449 -- a static expression or static range, gets either the expression value
7450 -- or the high bound of the range.
5b5df4a9 7451
eb66e842 7452 function Membership_Entry (N : Node_Id) return RList;
7453 -- Given a single membership entry (range, value, or subtype), returns
7454 -- the corresponding range list. Raises Static_Error if not static.
5b5df4a9 7455
eb66e842 7456 function Membership_Entries (N : Node_Id) return RList;
7457 -- Given an element on an alternatives list of a membership operation,
7458 -- returns the range list corresponding to this entry and all following
7459 -- entries (i.e. returns the "or" of this list of values).
b9e61b2a 7460
eb66e842 7461 function Stat_Pred (Typ : Entity_Id) return RList;
7462 -- Given a type, if it has a static predicate, then return the predicate
7463 -- as a range list, otherwise raise Non_Static.
c4968aa2 7464
eb66e842 7465 -----------
7466 -- "and" --
7467 -----------
c4968aa2 7468
eb66e842 7469 function "and" (Left : RList; Right : RList) return RList is
7470 FEnt : REnt;
7471 -- First range of result
c4968aa2 7472
eb66e842 7473 SLeft : Nat := Left'First;
7474 -- Start of rest of left entries
c4968aa2 7475
eb66e842 7476 SRight : Nat := Right'First;
7477 -- Start of rest of right entries
2072eaa9 7478
eb66e842 7479 begin
7480 -- If either range is True, return the other
5b5df4a9 7481
eb66e842 7482 if Is_True (Left) then
7483 return Right;
7484 elsif Is_True (Right) then
7485 return Left;
7486 end if;
87f3d5d3 7487
eb66e842 7488 -- If either range is False, return False
5b5df4a9 7489
eb66e842 7490 if Is_False (Left) or else Is_False (Right) then
7491 return False_Range;
7492 end if;
4c1fd062 7493
eb66e842 7494 -- Loop to remove entries at start that are disjoint, and thus just
7495 -- get discarded from the result entirely.
5b5df4a9 7496
eb66e842 7497 loop
7498 -- If no operands left in either operand, result is false
5b5df4a9 7499
eb66e842 7500 if SLeft > Left'Last or else SRight > Right'Last then
7501 return False_Range;
5b5df4a9 7502
eb66e842 7503 -- Discard first left operand entry if disjoint with right
5b5df4a9 7504
eb66e842 7505 elsif Left (SLeft).Hi < Right (SRight).Lo then
7506 SLeft := SLeft + 1;
5b5df4a9 7507
eb66e842 7508 -- Discard first right operand entry if disjoint with left
5b5df4a9 7509
eb66e842 7510 elsif Right (SRight).Hi < Left (SLeft).Lo then
7511 SRight := SRight + 1;
5b5df4a9 7512
eb66e842 7513 -- Otherwise we have an overlapping entry
5b5df4a9 7514
eb66e842 7515 else
7516 exit;
7517 end if;
7518 end loop;
5b5df4a9 7519
eb66e842 7520 -- Now we have two non-null operands, and first entries overlap. The
7521 -- first entry in the result will be the overlapping part of these
7522 -- two entries.
47a46747 7523
eb66e842 7524 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7525 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
47a46747 7526
eb66e842 7527 -- Now we can remove the entry that ended at a lower value, since its
7528 -- contribution is entirely contained in Fent.
5b5df4a9 7529
eb66e842 7530 if Left (SLeft).Hi <= Right (SRight).Hi then
7531 SLeft := SLeft + 1;
7532 else
7533 SRight := SRight + 1;
7534 end if;
5b5df4a9 7535
eb66e842 7536 -- Compute result by concatenating this first entry with the "and" of
7537 -- the remaining parts of the left and right operands. Note that if
7538 -- either of these is empty, "and" will yield empty, so that we will
7539 -- end up with just Fent, which is what we want in that case.
5b5df4a9 7540
eb66e842 7541 return
7542 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7543 end "and";
fb7f2fc4 7544
eb66e842 7545 -----------
7546 -- "not" --
7547 -----------
fb7f2fc4 7548
eb66e842 7549 function "not" (Right : RList) return RList is
7550 begin
7551 -- Return True if False range
fb7f2fc4 7552
eb66e842 7553 if Is_False (Right) then
7554 return True_Range;
7555 end if;
ed4adc99 7556
eb66e842 7557 -- Return False if True range
fb7f2fc4 7558
eb66e842 7559 if Is_True (Right) then
7560 return False_Range;
7561 end if;
fb7f2fc4 7562
eb66e842 7563 -- Here if not trivial case
87f3d5d3 7564
eb66e842 7565 declare
7566 Result : RList (1 .. Right'Length + 1);
7567 -- May need one more entry for gap at beginning and end
87f3d5d3 7568
eb66e842 7569 Count : Nat := 0;
7570 -- Number of entries stored in Result
4098232e 7571
eb66e842 7572 begin
7573 -- Gap at start
4098232e 7574
eb66e842 7575 if Right (Right'First).Lo > TLo then
7576 Count := Count + 1;
7577 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7578 end if;
ed4adc99 7579
eb66e842 7580 -- Gaps between ranges
ed4adc99 7581
eb66e842 7582 for J in Right'First .. Right'Last - 1 loop
7583 Count := Count + 1;
7584 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7585 end loop;
5b5df4a9 7586
eb66e842 7587 -- Gap at end
5b5df4a9 7588
eb66e842 7589 if Right (Right'Last).Hi < THi then
7590 Count := Count + 1;
7591 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7592 end if;
5b5df4a9 7593
eb66e842 7594 return Result (1 .. Count);
7595 end;
7596 end "not";
5b5df4a9 7597
eb66e842 7598 ----------
7599 -- "or" --
7600 ----------
5b5df4a9 7601
eb66e842 7602 function "or" (Left : RList; Right : RList) return RList is
7603 FEnt : REnt;
7604 -- First range of result
5b5df4a9 7605
eb66e842 7606 SLeft : Nat := Left'First;
7607 -- Start of rest of left entries
5b5df4a9 7608
eb66e842 7609 SRight : Nat := Right'First;
7610 -- Start of rest of right entries
5b5df4a9 7611
eb66e842 7612 begin
7613 -- If either range is True, return True
5b5df4a9 7614
eb66e842 7615 if Is_True (Left) or else Is_True (Right) then
7616 return True_Range;
7617 end if;
5b5df4a9 7618
eb66e842 7619 -- If either range is False (empty), return the other
5b5df4a9 7620
eb66e842 7621 if Is_False (Left) then
7622 return Right;
7623 elsif Is_False (Right) then
7624 return Left;
7625 end if;
5b5df4a9 7626
eb66e842 7627 -- Initialize result first entry from left or right operand depending
7628 -- on which starts with the lower range.
5b5df4a9 7629
eb66e842 7630 if Left (SLeft).Lo < Right (SRight).Lo then
7631 FEnt := Left (SLeft);
7632 SLeft := SLeft + 1;
7633 else
7634 FEnt := Right (SRight);
7635 SRight := SRight + 1;
7636 end if;
5b5df4a9 7637
eb66e842 7638 -- This loop eats ranges from left and right operands that are
7639 -- contiguous with the first range we are gathering.
9ea61fdd 7640
eb66e842 7641 loop
7642 -- Eat first entry in left operand if contiguous or overlapped by
7643 -- gathered first operand of result.
9ea61fdd 7644
eb66e842 7645 if SLeft <= Left'Last
7646 and then Left (SLeft).Lo <= FEnt.Hi + 1
7647 then
7648 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7649 SLeft := SLeft + 1;
9ea61fdd 7650
eb66e842 7651 -- Eat first entry in right operand if contiguous or overlapped by
7652 -- gathered right operand of result.
9ea61fdd 7653
eb66e842 7654 elsif SRight <= Right'Last
7655 and then Right (SRight).Lo <= FEnt.Hi + 1
7656 then
7657 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7658 SRight := SRight + 1;
9ea61fdd 7659
eb66e842 7660 -- All done if no more entries to eat
5b5df4a9 7661
eb66e842 7662 else
7663 exit;
7664 end if;
7665 end loop;
5b5df4a9 7666
eb66e842 7667 -- Obtain result as the first entry we just computed, concatenated
7668 -- to the "or" of the remaining results (if one operand is empty,
7669 -- this will just concatenate with the other
5b5df4a9 7670
eb66e842 7671 return
7672 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7673 end "or";
5b5df4a9 7674
eb66e842 7675 -----------------
7676 -- Build_Range --
7677 -----------------
5b5df4a9 7678
eb66e842 7679 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7680 Result : Node_Id;
5b5df4a9 7681 begin
eb66e842 7682 Result :=
7683 Make_Range (Loc,
7684 Low_Bound => Build_Val (Lo),
7685 High_Bound => Build_Val (Hi));
7686 Set_Etype (Result, Btyp);
7687 Set_Analyzed (Result);
7688 return Result;
7689 end Build_Range;
5b5df4a9 7690
eb66e842 7691 ---------------
7692 -- Build_Val --
7693 ---------------
5b5df4a9 7694
eb66e842 7695 function Build_Val (V : Uint) return Node_Id is
7696 Result : Node_Id;
5b5df4a9 7697
eb66e842 7698 begin
7699 if Is_Enumeration_Type (Typ) then
7700 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7701 else
7702 Result := Make_Integer_Literal (Loc, V);
7703 end if;
5b5df4a9 7704
eb66e842 7705 Set_Etype (Result, Btyp);
7706 Set_Is_Static_Expression (Result);
7707 Set_Analyzed (Result);
7708 return Result;
7709 end Build_Val;
87f3d5d3 7710
eb66e842 7711 ---------------
7712 -- Get_RList --
7713 ---------------
87f3d5d3 7714
eb66e842 7715 function Get_RList (Exp : Node_Id) return RList is
7716 Op : Node_Kind;
7717 Val : Uint;
87f3d5d3 7718
eb66e842 7719 begin
7720 -- Static expression can only be true or false
87f3d5d3 7721
eb66e842 7722 if Is_OK_Static_Expression (Exp) then
7723 if Expr_Value (Exp) = 0 then
7724 return False_Range;
7725 else
7726 return True_Range;
9ea61fdd 7727 end if;
eb66e842 7728 end if;
87f3d5d3 7729
eb66e842 7730 -- Otherwise test node type
192b8dab 7731
eb66e842 7732 Op := Nkind (Exp);
192b8dab 7733
eb66e842 7734 case Op is
5d3fb947 7735
eb66e842 7736 -- And
5d3fb947 7737
99378362 7738 when N_And_Then
7739 | N_Op_And
7740 =>
eb66e842 7741 return Get_RList (Left_Opnd (Exp))
7742 and
7743 Get_RList (Right_Opnd (Exp));
5b5df4a9 7744
eb66e842 7745 -- Or
9dc88aea 7746
99378362 7747 when N_Op_Or
7748 | N_Or_Else
7749 =>
eb66e842 7750 return Get_RList (Left_Opnd (Exp))
7751 or
7752 Get_RList (Right_Opnd (Exp));
7c443ae8 7753
eb66e842 7754 -- Not
9dc88aea 7755
eb66e842 7756 when N_Op_Not =>
7757 return not Get_RList (Right_Opnd (Exp));
9dc88aea 7758
eb66e842 7759 -- Comparisons of type with static value
84c8f0b8 7760
eb66e842 7761 when N_Op_Compare =>
490beba6 7762
eb66e842 7763 -- Type is left operand
9dc88aea 7764
eb66e842 7765 if Is_Type_Ref (Left_Opnd (Exp))
7766 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7767 then
7768 Val := Expr_Value (Right_Opnd (Exp));
84c8f0b8 7769
eb66e842 7770 -- Typ is right operand
84c8f0b8 7771
eb66e842 7772 elsif Is_Type_Ref (Right_Opnd (Exp))
7773 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7774 then
7775 Val := Expr_Value (Left_Opnd (Exp));
84c8f0b8 7776
eb66e842 7777 -- Invert sense of comparison
84c8f0b8 7778
eb66e842 7779 case Op is
7780 when N_Op_Gt => Op := N_Op_Lt;
7781 when N_Op_Lt => Op := N_Op_Gt;
7782 when N_Op_Ge => Op := N_Op_Le;
7783 when N_Op_Le => Op := N_Op_Ge;
7784 when others => null;
7785 end case;
84c8f0b8 7786
eb66e842 7787 -- Other cases are non-static
34d045d3 7788
eb66e842 7789 else
7790 raise Non_Static;
7791 end if;
9dc88aea 7792
eb66e842 7793 -- Construct range according to comparison operation
9dc88aea 7794
eb66e842 7795 case Op is
7796 when N_Op_Eq =>
7797 return RList'(1 => REnt'(Val, Val));
9dc88aea 7798
eb66e842 7799 when N_Op_Ge =>
7800 return RList'(1 => REnt'(Val, BHi));
84c8f0b8 7801
eb66e842 7802 when N_Op_Gt =>
7803 return RList'(1 => REnt'(Val + 1, BHi));
84c8f0b8 7804
eb66e842 7805 when N_Op_Le =>
7806 return RList'(1 => REnt'(BLo, Val));
fb7f2fc4 7807
eb66e842 7808 when N_Op_Lt =>
7809 return RList'(1 => REnt'(BLo, Val - 1));
9dc88aea 7810
eb66e842 7811 when N_Op_Ne =>
7812 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9dc88aea 7813
eb66e842 7814 when others =>
7815 raise Program_Error;
7816 end case;
9dc88aea 7817
eb66e842 7818 -- Membership (IN)
9dc88aea 7819
eb66e842 7820 when N_In =>
7821 if not Is_Type_Ref (Left_Opnd (Exp)) then
7822 raise Non_Static;
7823 end if;
9dc88aea 7824
eb66e842 7825 if Present (Right_Opnd (Exp)) then
7826 return Membership_Entry (Right_Opnd (Exp));
7827 else
7828 return Membership_Entries (First (Alternatives (Exp)));
7829 end if;
9dc88aea 7830
eb66e842 7831 -- Negative membership (NOT IN)
9dc88aea 7832
eb66e842 7833 when N_Not_In =>
7834 if not Is_Type_Ref (Left_Opnd (Exp)) then
7835 raise Non_Static;
7836 end if;
9dc88aea 7837
eb66e842 7838 if Present (Right_Opnd (Exp)) then
7839 return not Membership_Entry (Right_Opnd (Exp));
7840 else
7841 return not Membership_Entries (First (Alternatives (Exp)));
7842 end if;
9dc88aea 7843
eb66e842 7844 -- Function call, may be call to static predicate
9dc88aea 7845
eb66e842 7846 when N_Function_Call =>
7847 if Is_Entity_Name (Name (Exp)) then
7848 declare
7849 Ent : constant Entity_Id := Entity (Name (Exp));
7850 begin
7851 if Is_Predicate_Function (Ent)
7852 or else
7853 Is_Predicate_Function_M (Ent)
7854 then
7855 return Stat_Pred (Etype (First_Formal (Ent)));
7856 end if;
7857 end;
7858 end if;
9dc88aea 7859
eb66e842 7860 -- Other function call cases are non-static
9dc88aea 7861
eb66e842 7862 raise Non_Static;
490beba6 7863
eb66e842 7864 -- Qualified expression, dig out the expression
c92e878b 7865
eb66e842 7866 when N_Qualified_Expression =>
7867 return Get_RList (Expression (Exp));
4c1fd062 7868
eb66e842 7869 when N_Case_Expression =>
7870 declare
7871 Alt : Node_Id;
7872 Choices : List_Id;
7873 Dep : Node_Id;
4c1fd062 7874
eb66e842 7875 begin
7876 if not Is_Entity_Name (Expression (Expr))
7877 or else Etype (Expression (Expr)) /= Typ
7878 then
7879 Error_Msg_N
7880 ("expression must denaote subtype", Expression (Expr));
7881 return False_Range;
7882 end if;
9dc88aea 7883
eb66e842 7884 -- Collect discrete choices in all True alternatives
9dc88aea 7885
eb66e842 7886 Choices := New_List;
7887 Alt := First (Alternatives (Exp));
7888 while Present (Alt) loop
7889 Dep := Expression (Alt);
34d045d3 7890
cda40848 7891 if not Is_OK_Static_Expression (Dep) then
eb66e842 7892 raise Non_Static;
ebbab42d 7893
eb66e842 7894 elsif Is_True (Expr_Value (Dep)) then
7895 Append_List_To (Choices,
7896 New_Copy_List (Discrete_Choices (Alt)));
7897 end if;
fb7f2fc4 7898
eb66e842 7899 Next (Alt);
7900 end loop;
9dc88aea 7901
eb66e842 7902 return Membership_Entries (First (Choices));
7903 end;
9dc88aea 7904
eb66e842 7905 -- Expression with actions: if no actions, dig out expression
9dc88aea 7906
eb66e842 7907 when N_Expression_With_Actions =>
7908 if Is_Empty_List (Actions (Exp)) then
7909 return Get_RList (Expression (Exp));
7910 else
7911 raise Non_Static;
7912 end if;
9dc88aea 7913
eb66e842 7914 -- Xor operator
490beba6 7915
eb66e842 7916 when N_Op_Xor =>
7917 return (Get_RList (Left_Opnd (Exp))
7918 and not Get_RList (Right_Opnd (Exp)))
7919 or (Get_RList (Right_Opnd (Exp))
7920 and not Get_RList (Left_Opnd (Exp)));
9dc88aea 7921
eb66e842 7922 -- Any other node type is non-static
fb7f2fc4 7923
eb66e842 7924 when others =>
7925 raise Non_Static;
7926 end case;
7927 end Get_RList;
fb7f2fc4 7928
eb66e842 7929 ------------
7930 -- Hi_Val --
7931 ------------
fb7f2fc4 7932
eb66e842 7933 function Hi_Val (N : Node_Id) return Uint is
7934 begin
cda40848 7935 if Is_OK_Static_Expression (N) then
eb66e842 7936 return Expr_Value (N);
7937 else
7938 pragma Assert (Nkind (N) = N_Range);
7939 return Expr_Value (High_Bound (N));
7940 end if;
7941 end Hi_Val;
fb7f2fc4 7942
eb66e842 7943 --------------
7944 -- Is_False --
7945 --------------
fb7f2fc4 7946
eb66e842 7947 function Is_False (R : RList) return Boolean is
7948 begin
7949 return R'Length = 0;
7950 end Is_False;
9dc88aea 7951
eb66e842 7952 -------------
7953 -- Is_True --
7954 -------------
9dc88aea 7955
eb66e842 7956 function Is_True (R : RList) return Boolean is
7957 begin
7958 return R'Length = 1
7959 and then R (R'First).Lo = BLo
7960 and then R (R'First).Hi = BHi;
7961 end Is_True;
9dc88aea 7962
eb66e842 7963 -----------------
7964 -- Is_Type_Ref --
7965 -----------------
9dc88aea 7966
eb66e842 7967 function Is_Type_Ref (N : Node_Id) return Boolean is
7968 begin
7de4cba3 7969 return Nkind (N) = N_Identifier
7970 and then Chars (N) = Nam
7971 and then Paren_Count (N) = 0;
eb66e842 7972 end Is_Type_Ref;
9dc88aea 7973
eb66e842 7974 ------------
7975 -- Lo_Val --
7976 ------------
9dc88aea 7977
eb66e842 7978 function Lo_Val (N : Node_Id) return Uint is
84c8f0b8 7979 begin
cda40848 7980 if Is_OK_Static_Expression (N) then
eb66e842 7981 return Expr_Value (N);
84c8f0b8 7982 else
eb66e842 7983 pragma Assert (Nkind (N) = N_Range);
7984 return Expr_Value (Low_Bound (N));
84c8f0b8 7985 end if;
eb66e842 7986 end Lo_Val;
d97beb2f 7987
eb66e842 7988 ------------------------
7989 -- Membership_Entries --
7990 ------------------------
d97beb2f 7991
eb66e842 7992 function Membership_Entries (N : Node_Id) return RList is
84c8f0b8 7993 begin
eb66e842 7994 if No (Next (N)) then
7995 return Membership_Entry (N);
84c8f0b8 7996 else
eb66e842 7997 return Membership_Entry (N) or Membership_Entries (Next (N));
84c8f0b8 7998 end if;
eb66e842 7999 end Membership_Entries;
84c8f0b8 8000
eb66e842 8001 ----------------------
8002 -- Membership_Entry --
8003 ----------------------
84c8f0b8 8004
eb66e842 8005 function Membership_Entry (N : Node_Id) return RList is
8006 Val : Uint;
8007 SLo : Uint;
8008 SHi : Uint;
d97beb2f 8009
eb66e842 8010 begin
8011 -- Range case
d97beb2f 8012
eb66e842 8013 if Nkind (N) = N_Range then
cda40848 8014 if not Is_OK_Static_Expression (Low_Bound (N))
eb66e842 8015 or else
cda40848 8016 not Is_OK_Static_Expression (High_Bound (N))
eb66e842 8017 then
8018 raise Non_Static;
8019 else
8020 SLo := Expr_Value (Low_Bound (N));
8021 SHi := Expr_Value (High_Bound (N));
8022 return RList'(1 => REnt'(SLo, SHi));
8023 end if;
84c8f0b8 8024
eb66e842 8025 -- Static expression case
84c8f0b8 8026
cda40848 8027 elsif Is_OK_Static_Expression (N) then
eb66e842 8028 Val := Expr_Value (N);
8029 return RList'(1 => REnt'(Val, Val));
d97beb2f 8030
eb66e842 8031 -- Identifier (other than static expression) case
d97beb2f 8032
eb66e842 8033 else pragma Assert (Nkind (N) = N_Identifier);
d97beb2f 8034
eb66e842 8035 -- Type case
d97beb2f 8036
eb66e842 8037 if Is_Type (Entity (N)) then
d97beb2f 8038
eb66e842 8039 -- If type has predicates, process them
d97beb2f 8040
eb66e842 8041 if Has_Predicates (Entity (N)) then
8042 return Stat_Pred (Entity (N));
d97beb2f 8043
eb66e842 8044 -- For static subtype without predicates, get range
9dc88aea 8045
cda40848 8046 elsif Is_OK_Static_Subtype (Entity (N)) then
eb66e842 8047 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
8048 SHi := Expr_Value (Type_High_Bound (Entity (N)));
8049 return RList'(1 => REnt'(SLo, SHi));
9f269bd8 8050
eb66e842 8051 -- Any other type makes us non-static
9f269bd8 8052
eb66e842 8053 else
8054 raise Non_Static;
8055 end if;
84c8f0b8 8056
eb66e842 8057 -- Any other kind of identifier in predicate (e.g. a non-static
8058 -- expression value) means this is not a static predicate.
84c8f0b8 8059
eb66e842 8060 else
8061 raise Non_Static;
8062 end if;
8063 end if;
8064 end Membership_Entry;
84c8f0b8 8065
eb66e842 8066 ---------------
8067 -- Stat_Pred --
8068 ---------------
84c8f0b8 8069
eb66e842 8070 function Stat_Pred (Typ : Entity_Id) return RList is
8071 begin
8072 -- Not static if type does not have static predicates
84c8f0b8 8073
5c6a5792 8074 if not Has_Static_Predicate (Typ) then
eb66e842 8075 raise Non_Static;
8076 end if;
84c8f0b8 8077
eb66e842 8078 -- Otherwise we convert the predicate list to a range list
84c8f0b8 8079
eb66e842 8080 declare
5c6a5792 8081 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
8082 Result : RList (1 .. List_Length (Spred));
eb66e842 8083 P : Node_Id;
84c8f0b8 8084
eb66e842 8085 begin
5c6a5792 8086 P := First (Static_Discrete_Predicate (Typ));
eb66e842 8087 for J in Result'Range loop
8088 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
8089 Next (P);
8090 end loop;
84c8f0b8 8091
eb66e842 8092 return Result;
8093 end;
8094 end Stat_Pred;
84c8f0b8 8095
eb66e842 8096 -- Start of processing for Build_Discrete_Static_Predicate
84c8f0b8 8097
eb66e842 8098 begin
fdec445e 8099 -- Establish bounds for the predicate
afc229da 8100
8101 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8102 TLo := Expr_Value (Type_Low_Bound (Typ));
8103 else
8104 TLo := BLo;
8105 end if;
8106
8107 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8108 THi := Expr_Value (Type_High_Bound (Typ));
8109 else
8110 THi := BHi;
8111 end if;
8112
eb66e842 8113 -- Analyze the expression to see if it is a static predicate
84c8f0b8 8114
eb66e842 8115 declare
8116 Ranges : constant RList := Get_RList (Expr);
8117 -- Range list from expression if it is static
84c8f0b8 8118
eb66e842 8119 Plist : List_Id;
84c8f0b8 8120
eb66e842 8121 begin
8122 -- Convert range list into a form for the static predicate. In the
8123 -- Ranges array, we just have raw ranges, these must be converted
8124 -- to properly typed and analyzed static expressions or range nodes.
84c8f0b8 8125
eb66e842 8126 -- Note: here we limit ranges to the ranges of the subtype, so that
8127 -- a predicate is always false for values outside the subtype. That
8128 -- seems fine, such values are invalid anyway, and considering them
8129 -- to fail the predicate seems allowed and friendly, and furthermore
8130 -- simplifies processing for case statements and loops.
84c8f0b8 8131
eb66e842 8132 Plist := New_List;
8133
8134 for J in Ranges'Range loop
84c8f0b8 8135 declare
eb66e842 8136 Lo : Uint := Ranges (J).Lo;
8137 Hi : Uint := Ranges (J).Hi;
84c8f0b8 8138
eb66e842 8139 begin
8140 -- Ignore completely out of range entry
84c8f0b8 8141
eb66e842 8142 if Hi < TLo or else Lo > THi then
8143 null;
84c8f0b8 8144
eb66e842 8145 -- Otherwise process entry
84c8f0b8 8146
eb66e842 8147 else
8148 -- Adjust out of range value to subtype range
490beba6 8149
eb66e842 8150 if Lo < TLo then
8151 Lo := TLo;
8152 end if;
490beba6 8153
eb66e842 8154 if Hi > THi then
8155 Hi := THi;
8156 end if;
84c8f0b8 8157
eb66e842 8158 -- Convert range into required form
84c8f0b8 8159
eb66e842 8160 Append_To (Plist, Build_Range (Lo, Hi));
84c8f0b8 8161 end if;
eb66e842 8162 end;
8163 end loop;
84c8f0b8 8164
eb66e842 8165 -- Processing was successful and all entries were static, so now we
8166 -- can store the result as the predicate list.
84c8f0b8 8167
5c6a5792 8168 Set_Static_Discrete_Predicate (Typ, Plist);
84c8f0b8 8169
eb66e842 8170 -- The processing for static predicates put the expression into
8171 -- canonical form as a series of ranges. It also eliminated
8172 -- duplicates and collapsed and combined ranges. We might as well
8173 -- replace the alternatives list of the right operand of the
8174 -- membership test with the static predicate list, which will
8175 -- usually be more efficient.
84c8f0b8 8176
eb66e842 8177 declare
8178 New_Alts : constant List_Id := New_List;
8179 Old_Node : Node_Id;
8180 New_Node : Node_Id;
84c8f0b8 8181
eb66e842 8182 begin
8183 Old_Node := First (Plist);
8184 while Present (Old_Node) loop
8185 New_Node := New_Copy (Old_Node);
84c8f0b8 8186
eb66e842 8187 if Nkind (New_Node) = N_Range then
8188 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
8189 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
8190 end if;
84c8f0b8 8191
eb66e842 8192 Append_To (New_Alts, New_Node);
8193 Next (Old_Node);
8194 end loop;
84c8f0b8 8195
eb66e842 8196 -- If empty list, replace by False
84c8f0b8 8197
eb66e842 8198 if Is_Empty_List (New_Alts) then
8199 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
84c8f0b8 8200
eb66e842 8201 -- Else replace by set membership test
84c8f0b8 8202
eb66e842 8203 else
8204 Rewrite (Expr,
8205 Make_In (Loc,
8206 Left_Opnd => Make_Identifier (Loc, Nam),
8207 Right_Opnd => Empty,
8208 Alternatives => New_Alts));
490beba6 8209
eb66e842 8210 -- Resolve new expression in function context
490beba6 8211
eb66e842 8212 Install_Formals (Predicate_Function (Typ));
8213 Push_Scope (Predicate_Function (Typ));
8214 Analyze_And_Resolve (Expr, Standard_Boolean);
8215 Pop_Scope;
8216 end if;
8217 end;
8218 end;
9ab32fe9 8219
eb66e842 8220 -- If non-static, return doing nothing
9ab32fe9 8221
eb66e842 8222 exception
8223 when Non_Static =>
8224 return;
8225 end Build_Discrete_Static_Predicate;
64cc9e5d 8226
ee2b7923 8227 --------------------------------
8228 -- Build_Export_Import_Pragma --
8229 --------------------------------
8230
8231 function Build_Export_Import_Pragma
8232 (Asp : Node_Id;
8233 Id : Entity_Id) return Node_Id
8234 is
8235 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
8236 Expr : constant Node_Id := Expression (Asp);
8237 Loc : constant Source_Ptr := Sloc (Asp);
8238
8239 Args : List_Id;
8240 Conv : Node_Id;
8241 Conv_Arg : Node_Id;
8242 Dummy_1 : Node_Id;
8243 Dummy_2 : Node_Id;
8244 EN : Node_Id;
8245 LN : Node_Id;
8246 Prag : Node_Id;
8247
8248 Create_Pragma : Boolean := False;
8249 -- This flag is set when the aspect form is such that it warrants the
8250 -- creation of a corresponding pragma.
8251
8252 begin
8253 if Present (Expr) then
8254 if Error_Posted (Expr) then
8255 null;
8256
8257 elsif Is_True (Expr_Value (Expr)) then
8258 Create_Pragma := True;
8259 end if;
8260
8261 -- Otherwise the aspect defaults to True
8262
8263 else
8264 Create_Pragma := True;
8265 end if;
8266
8267 -- Nothing to do when the expression is False or is erroneous
8268
8269 if not Create_Pragma then
8270 return Empty;
8271 end if;
8272
8273 -- Obtain all interfacing aspects that apply to the related entity
8274
8275 Get_Interfacing_Aspects
8276 (Iface_Asp => Asp,
8277 Conv_Asp => Conv,
8278 EN_Asp => EN,
8279 Expo_Asp => Dummy_1,
8280 Imp_Asp => Dummy_2,
8281 LN_Asp => LN);
8282
8283 Args := New_List;
8284
8285 -- Handle the convention argument
8286
8287 if Present (Conv) then
8288 Conv_Arg := New_Copy_Tree (Expression (Conv));
8289
8290 -- Assume convention "Ada' when aspect Convention is missing
8291
8292 else
8293 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8294 end if;
8295
8296 Append_To (Args,
8297 Make_Pragma_Argument_Association (Loc,
8298 Chars => Name_Convention,
8299 Expression => Conv_Arg));
8300
8301 -- Handle the entity argument
8302
8303 Append_To (Args,
8304 Make_Pragma_Argument_Association (Loc,
8305 Chars => Name_Entity,
8306 Expression => New_Occurrence_Of (Id, Loc)));
8307
8308 -- Handle the External_Name argument
8309
8310 if Present (EN) then
8311 Append_To (Args,
8312 Make_Pragma_Argument_Association (Loc,
8313 Chars => Name_External_Name,
8314 Expression => New_Copy_Tree (Expression (EN))));
8315 end if;
8316
8317 -- Handle the Link_Name argument
8318
8319 if Present (LN) then
8320 Append_To (Args,
8321 Make_Pragma_Argument_Association (Loc,
8322 Chars => Name_Link_Name,
8323 Expression => New_Copy_Tree (Expression (LN))));
8324 end if;
8325
8326 -- Generate:
8327 -- pragma Export/Import
8328 -- (Convention => <Conv>/Ada,
8329 -- Entity => <Id>,
8330 -- [External_Name => <EN>,]
8331 -- [Link_Name => <LN>]);
8332
8333 Prag :=
8334 Make_Pragma (Loc,
8335 Pragma_Identifier =>
8336 Make_Identifier (Loc, Chars (Identifier (Asp))),
8337 Pragma_Argument_Associations => Args);
8338
8339 -- Decorate the relevant aspect and the pragma
8340
8341 Set_Aspect_Rep_Item (Asp, Prag);
8342
8343 Set_Corresponding_Aspect (Prag, Asp);
8344 Set_From_Aspect_Specification (Prag);
8345 Set_Parent (Prag, Asp);
8346
8347 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8348 Set_Import_Pragma (Id, Prag);
8349 end if;
8350
8351 return Prag;
8352 end Build_Export_Import_Pragma;
8353
eb66e842 8354 -------------------------------
8355 -- Build_Predicate_Functions --
8356 -------------------------------
d9f6a4ee 8357
eb66e842 8358 -- The procedures that are constructed here have the form:
d9f6a4ee 8359
eb66e842 8360 -- function typPredicate (Ixxx : typ) return Boolean is
8361 -- begin
8362 -- return
75491446 8363 -- typ1Predicate (typ1 (Ixxx))
eb66e842 8364 -- and then typ2Predicate (typ2 (Ixxx))
8365 -- and then ...;
75491446 8366 -- exp1 and then exp2 and then ...
eb66e842 8367 -- end typPredicate;
d9f6a4ee 8368
eb66e842 8369 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8370 -- this is the point at which these expressions get analyzed, providing the
8371 -- required delay, and typ1, typ2, are entities from which predicates are
8372 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8373 -- use this function even if checks are off, e.g. for membership tests.
d9f6a4ee 8374
75491446 8375 -- Note that the inherited predicates are evaluated first, as required by
8376 -- AI12-0071-1.
8377
8378 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8379 -- the form of this return expression.
8380
eb66e842 8381 -- If the expression has at least one Raise_Expression, then we also build
8382 -- the typPredicateM version of the function, in which any occurrence of a
8383 -- Raise_Expression is converted to "return False".
d9f6a4ee 8384
1ecdfe4b 8385 -- WARNING: This routine manages Ghost regions. Return statements must be
8386 -- replaced by gotos which jump to the end of the routine and restore the
8387 -- Ghost mode.
8388
eb66e842 8389 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8390 Loc : constant Source_Ptr := Sloc (Typ);
d9f6a4ee 8391
eb66e842 8392 Expr : Node_Id;
8393 -- This is the expression for the result of the function. It is
8394 -- is build by connecting the component predicates with AND THEN.
d9f6a4ee 8395
eb66e842 8396 Expr_M : Node_Id;
8397 -- This is the corresponding return expression for the Predicate_M
8398 -- function. It differs in that raise expressions are marked for
8399 -- special expansion (see Process_REs).
d9f6a4ee 8400
9c20237a 8401 Object_Name : Name_Id;
eb66e842 8402 -- Name for argument of Predicate procedure. Note that we use the same
499918a7 8403 -- name for both predicate functions. That way the reference within the
eb66e842 8404 -- predicate expression is the same in both functions.
d9f6a4ee 8405
9c20237a 8406 Object_Entity : Entity_Id;
eb66e842 8407 -- Entity for argument of Predicate procedure
d9f6a4ee 8408
9c20237a 8409 Object_Entity_M : Entity_Id;
8410 -- Entity for argument of separate Predicate procedure when exceptions
8411 -- are present in expression.
8412
02e5d0d0 8413 FDecl : Node_Id;
8414 -- The function declaration
9c20237a 8415
02e5d0d0 8416 SId : Entity_Id;
8417 -- Its entity
d9f6a4ee 8418
eb66e842 8419 Raise_Expression_Present : Boolean := False;
8420 -- Set True if Expr has at least one Raise_Expression
d9f6a4ee 8421
75491446 8422 procedure Add_Condition (Cond : Node_Id);
8423 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8424 -- Expr is empty).
d9f6a4ee 8425
eb66e842 8426 procedure Add_Predicates;
8427 -- Appends expressions for any Predicate pragmas in the rep item chain
8428 -- Typ to Expr. Note that we look only at items for this exact entity.
8429 -- Inheritance of predicates for the parent type is done by calling the
8430 -- Predicate_Function of the parent type, using Add_Call above.
d9f6a4ee 8431
75491446 8432 procedure Add_Call (T : Entity_Id);
8433 -- Includes a call to the predicate function for type T in Expr if T
8434 -- has predicates and Predicate_Function (T) is non-empty.
8435
eb66e842 8436 function Process_RE (N : Node_Id) return Traverse_Result;
8437 -- Used in Process REs, tests if node N is a raise expression, and if
8438 -- so, marks it to be converted to return False.
d9f6a4ee 8439
eb66e842 8440 procedure Process_REs is new Traverse_Proc (Process_RE);
8441 -- Marks any raise expressions in Expr_M to return False
d9f6a4ee 8442
f9e26ff7 8443 function Test_RE (N : Node_Id) return Traverse_Result;
8444 -- Used in Test_REs, tests one node for being a raise expression, and if
8445 -- so sets Raise_Expression_Present True.
8446
8447 procedure Test_REs is new Traverse_Proc (Test_RE);
8448 -- Tests to see if Expr contains any raise expressions
8449
eb66e842 8450 --------------
8451 -- Add_Call --
8452 --------------
d9f6a4ee 8453
eb66e842 8454 procedure Add_Call (T : Entity_Id) is
8455 Exp : Node_Id;
d9f6a4ee 8456
eb66e842 8457 begin
8458 if Present (T) and then Present (Predicate_Function (T)) then
8459 Set_Has_Predicates (Typ);
d9f6a4ee 8460
74d7e7f5 8461 -- Build the call to the predicate function of T. The type may be
8462 -- derived, so use an unchecked conversion for the actual.
d9f6a4ee 8463
eb66e842 8464 Exp :=
8465 Make_Predicate_Call
74d7e7f5 8466 (Typ => T,
8467 Expr =>
8468 Unchecked_Convert_To (T,
8469 Make_Identifier (Loc, Object_Name)));
d9f6a4ee 8470
75491446 8471 -- "and"-in the call to evolving expression
d9f6a4ee 8472
75491446 8473 Add_Condition (Exp);
d9f6a4ee 8474
eb66e842 8475 -- Output info message on inheritance if required. Note we do not
8476 -- give this information for generic actual types, since it is
8477 -- unwelcome noise in that case in instantiations. We also
8478 -- generally suppress the message in instantiations, and also
8479 -- if it involves internal names.
d9f6a4ee 8480
eb66e842 8481 if Opt.List_Inherited_Aspects
8482 and then not Is_Generic_Actual_Type (Typ)
8483 and then Instantiation_Depth (Sloc (Typ)) = 0
8484 and then not Is_Internal_Name (Chars (T))
8485 and then not Is_Internal_Name (Chars (Typ))
8486 then
8487 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8488 Error_Msg_Node_2 := T;
8489 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8490 end if;
8491 end if;
8492 end Add_Call;
d9f6a4ee 8493
75491446 8494 -------------------
8495 -- Add_Condition --
8496 -------------------
8497
8498 procedure Add_Condition (Cond : Node_Id) is
8499 begin
8500 -- This is the first predicate expression
8501
8502 if No (Expr) then
8503 Expr := Cond;
8504
8505 -- Otherwise concatenate to the existing predicate expressions by
8506 -- using "and then".
8507
8508 else
8509 Expr :=
8510 Make_And_Then (Loc,
8511 Left_Opnd => Relocate_Node (Expr),
8512 Right_Opnd => Cond);
8513 end if;
8514 end Add_Condition;
8515
eb66e842 8516 --------------------
8517 -- Add_Predicates --
8518 --------------------
d9f6a4ee 8519
eb66e842 8520 procedure Add_Predicates is
f9e26ff7 8521 procedure Add_Predicate (Prag : Node_Id);
8522 -- Concatenate the expression of predicate pragma Prag to Expr by
8523 -- using a short circuit "and then" operator.
d9f6a4ee 8524
f9e26ff7 8525 -------------------
8526 -- Add_Predicate --
8527 -------------------
d9f6a4ee 8528
f9e26ff7 8529 procedure Add_Predicate (Prag : Node_Id) is
8530 procedure Replace_Type_Reference (N : Node_Id);
8531 -- Replace a single occurrence N of the subtype name with a
8532 -- reference to the formal of the predicate function. N can be an
8533 -- identifier referencing the subtype, or a selected component,
8534 -- representing an appropriately qualified occurrence of the
8535 -- subtype name.
8536
8537 procedure Replace_Type_References is
8538 new Replace_Type_References_Generic (Replace_Type_Reference);
8539 -- Traverse an expression changing every occurrence of an
8540 -- identifier whose name matches the name of the subtype with a
8541 -- reference to the formal parameter of the predicate function.
8542
8543 ----------------------------
8544 -- Replace_Type_Reference --
8545 ----------------------------
8546
8547 procedure Replace_Type_Reference (N : Node_Id) is
8548 begin
8549 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8550 -- Use the Sloc of the usage name, not the defining name
d9f6a4ee 8551
f9e26ff7 8552 Set_Etype (N, Typ);
8553 Set_Entity (N, Object_Entity);
d97beb2f 8554
f9e26ff7 8555 -- We want to treat the node as if it comes from source, so
8556 -- that ASIS will not ignore it.
d97beb2f 8557
f9e26ff7 8558 Set_Comes_From_Source (N, True);
8559 end Replace_Type_Reference;
d97beb2f 8560
f9e26ff7 8561 -- Local variables
d97beb2f 8562
f9e26ff7 8563 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8564 Arg1 : Node_Id;
8565 Arg2 : Node_Id;
d97beb2f 8566
f9e26ff7 8567 -- Start of processing for Add_Predicate
24c8d764 8568
f9e26ff7 8569 begin
42fb9d35 8570 -- Mark corresponding SCO as enabled
8571
8572 Set_SCO_Pragma_Enabled (Sloc (Prag));
8573
f9e26ff7 8574 -- Extract the arguments of the pragma. The expression itself
8575 -- is copied for use in the predicate function, to preserve the
8576 -- original version for ASIS use.
d97beb2f 8577
f9e26ff7 8578 Arg1 := First (Pragma_Argument_Associations (Prag));
8579 Arg2 := Next (Arg1);
d97beb2f 8580
f9e26ff7 8581 Arg1 := Get_Pragma_Arg (Arg1);
8582 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
d97beb2f 8583
f9e26ff7 8584 -- When the predicate pragma applies to the current type or its
8585 -- full view, replace all occurrences of the subtype name with
8586 -- references to the formal parameter of the predicate function.
639c3741 8587
f9e26ff7 8588 if Entity (Arg1) = Typ
8589 or else Full_View (Entity (Arg1)) = Typ
8590 then
8591 Replace_Type_References (Arg2, Typ);
639c3741 8592
f9e26ff7 8593 -- If the predicate pragma comes from an aspect, replace the
8594 -- saved expression because we need the subtype references
8595 -- replaced for the calls to Preanalyze_Spec_Expression in
8596 -- Check_Aspect_At_xxx routines.
639c3741 8597
f9e26ff7 8598 if Present (Asp) then
f9e26ff7 8599 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8600 end if;
24c8d764 8601
75491446 8602 -- "and"-in the Arg2 condition to evolving expression
639c3741 8603
75491446 8604 Add_Condition (Relocate_Node (Arg2));
f9e26ff7 8605 end if;
8606 end Add_Predicate;
737e8460 8607
f9e26ff7 8608 -- Local variables
737e8460 8609
f9e26ff7 8610 Ritem : Node_Id;
d97beb2f 8611
f9e26ff7 8612 -- Start of processing for Add_Predicates
d97beb2f 8613
f9e26ff7 8614 begin
8615 Ritem := First_Rep_Item (Typ);
74d7e7f5 8616
8617 -- If the type is private, check whether full view has inherited
8618 -- predicates.
8619
8620 if Is_Private_Type (Typ) and then No (Ritem) then
8621 Ritem := First_Rep_Item (Full_View (Typ));
8622 end if;
8623
f9e26ff7 8624 while Present (Ritem) loop
8625 if Nkind (Ritem) = N_Pragma
ddccc924 8626 and then Pragma_Name (Ritem) = Name_Predicate
f9e26ff7 8627 then
8628 Add_Predicate (Ritem);
0ea02224 8629
8630 -- If the type is declared in an inner package it may be frozen
8631 -- outside of the package, and the generated pragma has not been
8632 -- analyzed yet, so capture the expression for the predicate
8633 -- function at this point.
8634
8635 elsif Nkind (Ritem) = N_Aspect_Specification
238921ae 8636 and then Present (Aspect_Rep_Item (Ritem))
8637 and then Scope (Typ) /= Current_Scope
0ea02224 8638 then
8639 declare
8640 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8641
8642 begin
8643 if Nkind (Prag) = N_Pragma
ddccc924 8644 and then Pragma_Name (Prag) = Name_Predicate
0ea02224 8645 then
8646 Add_Predicate (Prag);
8647 end if;
8648 end;
eb66e842 8649 end if;
d97beb2f 8650
eb66e842 8651 Next_Rep_Item (Ritem);
8652 end loop;
8653 end Add_Predicates;
d97beb2f 8654
eb66e842 8655 ----------------
8656 -- Process_RE --
8657 ----------------
d97beb2f 8658
eb66e842 8659 function Process_RE (N : Node_Id) return Traverse_Result is
d9f6a4ee 8660 begin
eb66e842 8661 if Nkind (N) = N_Raise_Expression then
8662 Set_Convert_To_Return_False (N);
8663 return Skip;
d9f6a4ee 8664 else
eb66e842 8665 return OK;
d9f6a4ee 8666 end if;
eb66e842 8667 end Process_RE;
d7c2851f 8668
d9f6a4ee 8669 -------------
eb66e842 8670 -- Test_RE --
d9f6a4ee 8671 -------------
d7c2851f 8672
eb66e842 8673 function Test_RE (N : Node_Id) return Traverse_Result is
d97beb2f 8674 begin
eb66e842 8675 if Nkind (N) = N_Raise_Expression then
8676 Raise_Expression_Present := True;
8677 return Abandon;
8678 else
8679 return OK;
8680 end if;
8681 end Test_RE;
d97beb2f 8682
f9e26ff7 8683 -- Local variables
8684
e02e4129 8685 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
8686 -- Save the Ghost mode to restore on exit
f9e26ff7 8687
eb66e842 8688 -- Start of processing for Build_Predicate_Functions
d97beb2f 8689
eb66e842 8690 begin
8691 -- Return if already built or if type does not have predicates
9dc88aea 8692
9c20237a 8693 SId := Predicate_Function (Typ);
eb66e842 8694 if not Has_Predicates (Typ)
9c20237a 8695 or else (Present (SId) and then Has_Completion (SId))
eb66e842 8696 then
8697 return;
8698 end if;
d9f6a4ee 8699
30f8d103 8700 -- The related type may be subject to pragma Ghost. Set the mode now to
8701 -- ensure that the predicate functions are properly marked as Ghost.
f9e26ff7 8702
e02e4129 8703 Set_Ghost_Mode (Typ);
f9e26ff7 8704
eb66e842 8705 -- Prepare to construct predicate expression
d97beb2f 8706
eb66e842 8707 Expr := Empty;
d97beb2f 8708
9c20237a 8709 if Present (SId) then
8710 FDecl := Unit_Declaration_Node (SId);
8711
8712 else
8713 FDecl := Build_Predicate_Function_Declaration (Typ);
8714 SId := Defining_Entity (FDecl);
8715 end if;
8716
8717 -- Recover name of formal parameter of function that replaces references
8718 -- to the type in predicate expressions.
8719
8720 Object_Entity :=
8721 Defining_Identifier
8722 (First (Parameter_Specifications (Specification (FDecl))));
8723
8724 Object_Name := Chars (Object_Entity);
8725 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8726
75491446 8727 -- Add predicates for ancestor if present. These must come before the
8728 -- ones for the current type, as required by AI12-0071-1.
d97beb2f 8729
eb66e842 8730 declare
74d7e7f5 8731 Atyp : Entity_Id;
d9f6a4ee 8732 begin
74d7e7f5 8733 Atyp := Nearest_Ancestor (Typ);
8734
8735 -- The type may be private but the full view may inherit predicates
8736
8737 if No (Atyp) and then Is_Private_Type (Typ) then
8738 Atyp := Nearest_Ancestor (Full_View (Typ));
8739 end if;
8740
eb66e842 8741 if Present (Atyp) then
8742 Add_Call (Atyp);
8743 end if;
8744 end;
02e5d0d0 8745
75491446 8746 -- Add Predicates for the current type
8747
8748 Add_Predicates;
8749
eb66e842 8750 -- Case where predicates are present
9dc88aea 8751
eb66e842 8752 if Present (Expr) then
726fd56a 8753
eb66e842 8754 -- Test for raise expression present
726fd56a 8755
eb66e842 8756 Test_REs (Expr);
9dc88aea 8757
eb66e842 8758 -- If raise expression is present, capture a copy of Expr for use
8759 -- in building the predicateM function version later on. For this
8760 -- copy we replace references to Object_Entity by Object_Entity_M.
9dc88aea 8761
eb66e842 8762 if Raise_Expression_Present then
8763 declare
299b347e 8764 Map : constant Elist_Id := New_Elmt_List;
8765 New_V : Entity_Id := Empty;
8766
8767 -- The unanalyzed expression will be copied and appear in
8768 -- both functions. Normally expressions do not declare new
8769 -- entities, but quantified expressions do, so we need to
8770 -- create new entities for their bound variables, to prevent
8771 -- multiple definitions in gigi.
8772
8773 function Reset_Loop_Variable (N : Node_Id)
8774 return Traverse_Result;
8775
8776 procedure Collect_Loop_Variables is
8777 new Traverse_Proc (Reset_Loop_Variable);
8778
8779 ------------------------
8780 -- Reset_Loop_Variable --
8781 ------------------------
8782
8783 function Reset_Loop_Variable (N : Node_Id)
8784 return Traverse_Result
8785 is
8786 begin
8787 if Nkind (N) = N_Iterator_Specification then
8788 New_V := Make_Defining_Identifier
8789 (Sloc (N), Chars (Defining_Identifier (N)));
8790
8791 Set_Defining_Identifier (N, New_V);
8792 end if;
8793
8794 return OK;
8795 end Reset_Loop_Variable;
8796
eb66e842 8797 begin
8798 Append_Elmt (Object_Entity, Map);
8799 Append_Elmt (Object_Entity_M, Map);
8800 Expr_M := New_Copy_Tree (Expr, Map => Map);
299b347e 8801 Collect_Loop_Variables (Expr_M);
eb66e842 8802 end;
8803 end if;
d97beb2f 8804
eb66e842 8805 -- Build the main predicate function
9dc88aea 8806
eb66e842 8807 declare
eb66e842 8808 SIdB : constant Entity_Id :=
8809 Make_Defining_Identifier (Loc,
8810 Chars => New_External_Name (Chars (Typ), "Predicate"));
8811 -- The entity for the function body
9dc88aea 8812
eb66e842 8813 Spec : Node_Id;
eb66e842 8814 FBody : Node_Id;
9dc88aea 8815
eb66e842 8816 begin
37066559 8817 Set_Ekind (SIdB, E_Function);
8818 Set_Is_Predicate_Function (SIdB);
8819
eb66e842 8820 -- The predicate function is shared between views of a type
d97beb2f 8821
eb66e842 8822 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8823 Set_Predicate_Function (Full_View (Typ), SId);
d97beb2f 8824 end if;
d97beb2f 8825
eb66e842 8826 -- Build function body
d97beb2f 8827
eb66e842 8828 Spec :=
8829 Make_Function_Specification (Loc,
8830 Defining_Unit_Name => SIdB,
8831 Parameter_Specifications => New_List (
8832 Make_Parameter_Specification (Loc,
8833 Defining_Identifier =>
8834 Make_Defining_Identifier (Loc, Object_Name),
8835 Parameter_Type =>
8836 New_Occurrence_Of (Typ, Loc))),
8837 Result_Definition =>
8838 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8839
eb66e842 8840 FBody :=
8841 Make_Subprogram_Body (Loc,
8842 Specification => Spec,
8843 Declarations => Empty_List,
8844 Handled_Statement_Sequence =>
8845 Make_Handled_Sequence_Of_Statements (Loc,
8846 Statements => New_List (
8847 Make_Simple_Return_Statement (Loc,
8848 Expression => Expr))));
9dc88aea 8849
9c20237a 8850 -- If declaration has not been analyzed yet, Insert declaration
f021ee0f 8851 -- before freeze node. Insert body itself after freeze node.
9c20237a 8852
8853 if not Analyzed (FDecl) then
8854 Insert_Before_And_Analyze (N, FDecl);
8855 end if;
d97beb2f 8856
02e5d0d0 8857 Insert_After_And_Analyze (N, FBody);
6958c62c 8858
8859 -- Static predicate functions are always side-effect free, and
8860 -- in most cases dynamic predicate functions are as well. Mark
8861 -- them as such whenever possible, so redundant predicate checks
7dd0b9b3 8862 -- can be optimized. If there is a variable reference within the
8863 -- expression, the function is not pure.
b2e821de 8864
6958c62c 8865 if Expander_Active then
7dd0b9b3 8866 Set_Is_Pure (SId,
8867 Side_Effect_Free (Expr, Variable_Ref => True));
6958c62c 8868 Set_Is_Inlined (SId);
8869 end if;
d9f6a4ee 8870 end;
d97beb2f 8871
eb66e842 8872 -- Test for raise expressions present and if so build M version
d97beb2f 8873
eb66e842 8874 if Raise_Expression_Present then
8875 declare
8876 SId : constant Entity_Id :=
8877 Make_Defining_Identifier (Loc,
8878 Chars => New_External_Name (Chars (Typ), "PredicateM"));
c96806b2 8879 -- The entity for the function spec
d97beb2f 8880
eb66e842 8881 SIdB : constant Entity_Id :=
8882 Make_Defining_Identifier (Loc,
8883 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8884 -- The entity for the function body
b9e61b2a 8885
eb66e842 8886 Spec : Node_Id;
eb66e842 8887 FBody : Node_Id;
9c20237a 8888 FDecl : Node_Id;
eb66e842 8889 BTemp : Entity_Id;
d97beb2f 8890
eb66e842 8891 begin
8892 -- Mark any raise expressions for special expansion
d97beb2f 8893
eb66e842 8894 Process_REs (Expr_M);
d97beb2f 8895
eb66e842 8896 -- Build function declaration
d97beb2f 8897
eb66e842 8898 Set_Ekind (SId, E_Function);
8899 Set_Is_Predicate_Function_M (SId);
8900 Set_Predicate_Function_M (Typ, SId);
d97beb2f 8901
eb66e842 8902 -- The predicate function is shared between views of a type
d97beb2f 8903
eb66e842 8904 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8905 Set_Predicate_Function_M (Full_View (Typ), SId);
8906 end if;
9dc88aea 8907
eb66e842 8908 Spec :=
8909 Make_Function_Specification (Loc,
8910 Defining_Unit_Name => SId,
8911 Parameter_Specifications => New_List (
8912 Make_Parameter_Specification (Loc,
8913 Defining_Identifier => Object_Entity_M,
8914 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8915 Result_Definition =>
8916 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8917
eb66e842 8918 FDecl :=
8919 Make_Subprogram_Declaration (Loc,
8920 Specification => Spec);
9dc88aea 8921
eb66e842 8922 -- Build function body
9dc88aea 8923
eb66e842 8924 Spec :=
8925 Make_Function_Specification (Loc,
8926 Defining_Unit_Name => SIdB,
8927 Parameter_Specifications => New_List (
8928 Make_Parameter_Specification (Loc,
8929 Defining_Identifier =>
8930 Make_Defining_Identifier (Loc, Object_Name),
8931 Parameter_Type =>
8932 New_Occurrence_Of (Typ, Loc))),
8933 Result_Definition =>
8934 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8935
eb66e842 8936 -- Build the body, we declare the boolean expression before
8937 -- doing the return, because we are not really confident of
8938 -- what happens if a return appears within a return.
9dc88aea 8939
eb66e842 8940 BTemp :=
8941 Make_Defining_Identifier (Loc,
8942 Chars => New_Internal_Name ('B'));
9dc88aea 8943
eb66e842 8944 FBody :=
8945 Make_Subprogram_Body (Loc,
8946 Specification => Spec,
9dc88aea 8947
eb66e842 8948 Declarations => New_List (
8949 Make_Object_Declaration (Loc,
8950 Defining_Identifier => BTemp,
8951 Constant_Present => True,
8952 Object_Definition =>
8953 New_Occurrence_Of (Standard_Boolean, Loc),
8954 Expression => Expr_M)),
d97beb2f 8955
eb66e842 8956 Handled_Statement_Sequence =>
8957 Make_Handled_Sequence_Of_Statements (Loc,
8958 Statements => New_List (
8959 Make_Simple_Return_Statement (Loc,
8960 Expression => New_Occurrence_Of (BTemp, Loc)))));
d97beb2f 8961
eb66e842 8962 -- Insert declaration before freeze node and body after
d97beb2f 8963
eb66e842 8964 Insert_Before_And_Analyze (N, FDecl);
8965 Insert_After_And_Analyze (N, FBody);
8966 end;
8967 end if;
9dc88aea 8968
3b23aaa0 8969 -- See if we have a static predicate. Note that the answer may be
8970 -- yes even if we have an explicit Dynamic_Predicate present.
9dc88aea 8971
3b23aaa0 8972 declare
94d896aa 8973 PS : Boolean;
3b23aaa0 8974 EN : Node_Id;
9dc88aea 8975
3b23aaa0 8976 begin
94d896aa 8977 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8978 PS := False;
8979 else
8980 PS := Is_Predicate_Static (Expr, Object_Name);
8981 end if;
8982
a360a0f7 8983 -- Case where we have a predicate-static aspect
9dc88aea 8984
3b23aaa0 8985 if PS then
9dc88aea 8986
3b23aaa0 8987 -- We don't set Has_Static_Predicate_Aspect, since we can have
8988 -- any of the three cases (Predicate, Dynamic_Predicate, or
8989 -- Static_Predicate) generating a predicate with an expression
a360a0f7 8990 -- that is predicate-static. We just indicate that we have a
3b23aaa0 8991 -- predicate that can be treated as static.
d7c2851f 8992
3b23aaa0 8993 Set_Has_Static_Predicate (Typ);
d7c2851f 8994
3b23aaa0 8995 -- For discrete subtype, build the static predicate list
9dc88aea 8996
3b23aaa0 8997 if Is_Discrete_Type (Typ) then
8998 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8999
9000 -- If we don't get a static predicate list, it means that we
9001 -- have a case where this is not possible, most typically in
9002 -- the case where we inherit a dynamic predicate. We do not
9003 -- consider this an error, we just leave the predicate as
9004 -- dynamic. But if we do succeed in building the list, then
9005 -- we mark the predicate as static.
9006
5c6a5792 9007 if No (Static_Discrete_Predicate (Typ)) then
3b23aaa0 9008 Set_Has_Static_Predicate (Typ, False);
9009 end if;
94d896aa 9010
9011 -- For real or string subtype, save predicate expression
9012
9013 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9014 Set_Static_Real_Or_String_Predicate (Typ, Expr);
3b23aaa0 9015 end if;
9016
9017 -- Case of dynamic predicate (expression is not predicate-static)
9dc88aea 9018
eb66e842 9019 else
3b23aaa0 9020 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
9021 -- is only set if we have an explicit Dynamic_Predicate aspect
9022 -- given. Here we may simply have a Predicate aspect where the
9023 -- expression happens not to be predicate-static.
9024
9025 -- Emit an error when the predicate is categorized as static
9026 -- but its expression is not predicate-static.
9027
9028 -- First a little fiddling to get a nice location for the
9029 -- message. If the expression is of the form (A and then B),
75491446 9030 -- where A is an inherited predicate, then use the right
9031 -- operand for the Sloc. This avoids getting confused by a call
9032 -- to an inherited predicate with a less convenient source
9033 -- location.
3b23aaa0 9034
9035 EN := Expr;
75491446 9036 while Nkind (EN) = N_And_Then
9037 and then Nkind (Left_Opnd (EN)) = N_Function_Call
9038 and then Is_Predicate_Function
9039 (Entity (Name (Left_Opnd (EN))))
9040 loop
9041 EN := Right_Opnd (EN);
3b23aaa0 9042 end loop;
9043
9044 -- Now post appropriate message
9045
9046 if Has_Static_Predicate_Aspect (Typ) then
94d896aa 9047 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
3b23aaa0 9048 Error_Msg_F
26279d91 9049 ("expression is not predicate-static (RM 3.2.4(16-22))",
3b23aaa0 9050 EN);
9051 else
94d896aa 9052 Error_Msg_F
9053 ("static predicate requires scalar or string type", EN);
3b23aaa0 9054 end if;
9055 end if;
eb66e842 9056 end if;
3b23aaa0 9057 end;
eb66e842 9058 end if;
f9e26ff7 9059
e02e4129 9060 Restore_Ghost_Mode (Saved_GM);
eb66e842 9061 end Build_Predicate_Functions;
9dc88aea 9062
9c20237a 9063 ------------------------------------------
9064 -- Build_Predicate_Function_Declaration --
9065 ------------------------------------------
9066
1ecdfe4b 9067 -- WARNING: This routine manages Ghost regions. Return statements must be
9068 -- replaced by gotos which jump to the end of the routine and restore the
9069 -- Ghost mode.
9070
9c20237a 9071 function Build_Predicate_Function_Declaration
9072 (Typ : Entity_Id) return Node_Id
9073 is
9074 Loc : constant Source_Ptr := Sloc (Typ);
9075
e02e4129 9076 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
9077 -- Save the Ghost mode to restore on exit
9078
72a98436 9079 Func_Decl : Node_Id;
9080 Func_Id : Entity_Id;
72a98436 9081 Spec : Node_Id;
9c20237a 9082
72a98436 9083 begin
9084 -- The related type may be subject to pragma Ghost. Set the mode now to
9085 -- ensure that the predicate functions are properly marked as Ghost.
9c20237a 9086
e02e4129 9087 Set_Ghost_Mode (Typ);
9c20237a 9088
72a98436 9089 Func_Id :=
9090 Make_Defining_Identifier (Loc,
9091 Chars => New_External_Name (Chars (Typ), "Predicate"));
9c20237a 9092
3db675d2 9093 -- The predicate function requires debug info when the predicates are
9094 -- subject to Source Coverage Obligations.
9095
9096 if Opt.Generate_SCO then
9097 Set_Debug_Info_Needed (Func_Id);
9098 end if;
9099
9c20237a 9100 Spec :=
9101 Make_Function_Specification (Loc,
72a98436 9102 Defining_Unit_Name => Func_Id,
9c20237a 9103 Parameter_Specifications => New_List (
9104 Make_Parameter_Specification (Loc,
72a98436 9105 Defining_Identifier => Make_Temporary (Loc, 'I'),
9c20237a 9106 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9107 Result_Definition =>
9108 New_Occurrence_Of (Standard_Boolean, Loc));
9109
72a98436 9110 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
9c20237a 9111
72a98436 9112 Set_Ekind (Func_Id, E_Function);
9113 Set_Etype (Func_Id, Standard_Boolean);
9114 Set_Is_Internal (Func_Id);
9115 Set_Is_Predicate_Function (Func_Id);
9116 Set_Predicate_Function (Typ, Func_Id);
9c20237a 9117
72a98436 9118 Insert_After (Parent (Typ), Func_Decl);
9119 Analyze (Func_Decl);
9c20237a 9120
e02e4129 9121 Restore_Ghost_Mode (Saved_GM);
9c20237a 9122
72a98436 9123 return Func_Decl;
9c20237a 9124 end Build_Predicate_Function_Declaration;
9125
d9f6a4ee 9126 -----------------------------------------
9127 -- Check_Aspect_At_End_Of_Declarations --
9128 -----------------------------------------
9dc88aea 9129
d9f6a4ee 9130 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9131 Ent : constant Entity_Id := Entity (ASN);
9132 Ident : constant Node_Id := Identifier (ASN);
9133 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
d7c2851f 9134
d9f6a4ee 9135 End_Decl_Expr : constant Node_Id := Entity (Ident);
9136 -- Expression to be analyzed at end of declarations
d7c2851f 9137
d9f6a4ee 9138 Freeze_Expr : constant Node_Id := Expression (ASN);
6da581c1 9139 -- Expression from call to Check_Aspect_At_Freeze_Point.
d7c2851f 9140
25e4fa47 9141 T : constant Entity_Id := Etype (Original_Node (Freeze_Expr));
6da581c1 9142 -- Type required for preanalyze call. We use the original expression to
9143 -- get the proper type, to prevent cascaded errors when the expression
9144 -- is constant-folded.
d7c2851f 9145
d9f6a4ee 9146 Err : Boolean;
9147 -- Set False if error
9dc88aea 9148
d9f6a4ee 9149 -- On entry to this procedure, Entity (Ident) contains a copy of the
9150 -- original expression from the aspect, saved for this purpose, and
9151 -- but Expression (Ident) is a preanalyzed copy of the expression,
9152 -- preanalyzed just after the freeze point.
9dc88aea 9153
d9f6a4ee 9154 procedure Check_Overloaded_Name;
9155 -- For aspects whose expression is simply a name, this routine checks if
9156 -- the name is overloaded or not. If so, it verifies there is an
9157 -- interpretation that matches the entity obtained at the freeze point,
9158 -- otherwise the compiler complains.
9dc88aea 9159
d9f6a4ee 9160 ---------------------------
9161 -- Check_Overloaded_Name --
9162 ---------------------------
9163
9164 procedure Check_Overloaded_Name is
d97beb2f 9165 begin
d9f6a4ee 9166 if not Is_Overloaded (End_Decl_Expr) then
5ac76cee 9167 Err := not Is_Entity_Name (End_Decl_Expr)
9168 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
d9f6a4ee 9169
d97beb2f 9170 else
d9f6a4ee 9171 Err := True;
9dc88aea 9172
d9f6a4ee 9173 declare
9174 Index : Interp_Index;
9175 It : Interp;
9dc88aea 9176
d9f6a4ee 9177 begin
9178 Get_First_Interp (End_Decl_Expr, Index, It);
9179 while Present (It.Typ) loop
9180 if It.Nam = Entity (Freeze_Expr) then
9181 Err := False;
9182 exit;
9183 end if;
9184
9185 Get_Next_Interp (Index, It);
9186 end loop;
9187 end;
9dc88aea 9188 end if;
d9f6a4ee 9189 end Check_Overloaded_Name;
9dc88aea 9190
d9f6a4ee 9191 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9dc88aea 9192
d9f6a4ee 9193 begin
da3cad01 9194 -- In an instance we do not perform the consistency check between freeze
9195 -- point and end of declarations, because it was done already in the
9196 -- analysis of the generic. Furthermore, the delayed analysis of an
9197 -- aspect of the instance may produce spurious errors when the generic
9198 -- is a child unit that references entities in the parent (which might
9199 -- not be in scope at the freeze point of the instance).
9200
9201 if In_Instance then
9202 return;
9203
1c164d44 9204 -- The enclosing scope may have been rewritten during expansion (.e.g. a
9205 -- task body is rewritten as a procedure) after this conformance check
9206 -- has been performed, so do not perform it again (it may not easily be
9207 -- done if full visibility of local entities is not available).
ce450a94 9208
9209 elsif not Comes_From_Source (Current_Scope) then
9210 return;
9211
d9f6a4ee 9212 -- Case of aspects Dimension, Dimension_System and Synchronization
9dc88aea 9213
da3cad01 9214 elsif A_Id = Aspect_Synchronization then
d9f6a4ee 9215 return;
d97beb2f 9216
d9f6a4ee 9217 -- Case of stream attributes, just have to compare entities. However,
9218 -- the expression is just a name (possibly overloaded), and there may
9219 -- be stream operations declared for unrelated types, so we just need
9220 -- to verify that one of these interpretations is the one available at
9221 -- at the freeze point.
9dc88aea 9222
d9f6a4ee 9223 elsif A_Id = Aspect_Input or else
f02a9a9a 9224 A_Id = Aspect_Output or else
9225 A_Id = Aspect_Read or else
9226 A_Id = Aspect_Write
d9f6a4ee 9227 then
9228 Analyze (End_Decl_Expr);
9229 Check_Overloaded_Name;
9dc88aea 9230
d9f6a4ee 9231 elsif A_Id = Aspect_Variable_Indexing or else
9232 A_Id = Aspect_Constant_Indexing or else
9233 A_Id = Aspect_Default_Iterator or else
9234 A_Id = Aspect_Iterator_Element
9235 then
9236 -- Make type unfrozen before analysis, to prevent spurious errors
9237 -- about late attributes.
9dc88aea 9238
d9f6a4ee 9239 Set_Is_Frozen (Ent, False);
9240 Analyze (End_Decl_Expr);
9241 Set_Is_Frozen (Ent, True);
9dc88aea 9242
d9f6a4ee 9243 -- If the end of declarations comes before any other freeze
9244 -- point, the Freeze_Expr is not analyzed: no check needed.
9dc88aea 9245
d9f6a4ee 9246 if Analyzed (Freeze_Expr) and then not In_Instance then
9247 Check_Overloaded_Name;
9248 else
9249 Err := False;
9250 end if;
55e8372b 9251
d9f6a4ee 9252 -- All other cases
55e8372b 9253
d9f6a4ee 9254 else
c1efebf9 9255 -- Indicate that the expression comes from an aspect specification,
9256 -- which is used in subsequent analysis even if expansion is off.
9257
9258 Set_Parent (End_Decl_Expr, ASN);
9259
d9f6a4ee 9260 -- In a generic context the aspect expressions have not been
9261 -- preanalyzed, so do it now. There are no conformance checks
9262 -- to perform in this case.
55e8372b 9263
d9f6a4ee 9264 if No (T) then
9265 Check_Aspect_At_Freeze_Point (ASN);
9266 return;
55e8372b 9267
d9f6a4ee 9268 -- The default values attributes may be defined in the private part,
9269 -- and the analysis of the expression may take place when only the
9270 -- partial view is visible. The expression must be scalar, so use
9271 -- the full view to resolve.
55e8372b 9272
d9f6a4ee 9273 elsif (A_Id = Aspect_Default_Value
9274 or else
9275 A_Id = Aspect_Default_Component_Value)
9276 and then Is_Private_Type (T)
9277 then
9278 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
c1efebf9 9279
d9f6a4ee 9280 else
9281 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9282 end if;
d97beb2f 9283
d9f6a4ee 9284 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9285 end if;
55e8372b 9286
c1efebf9 9287 -- Output error message if error. Force error on aspect specification
9288 -- even if there is an error on the expression itself.
55e8372b 9289
d9f6a4ee 9290 if Err then
9291 Error_Msg_NE
c1efebf9 9292 ("!visibility of aspect for& changes after freeze point",
d9f6a4ee 9293 ASN, Ent);
9294 Error_Msg_NE
9295 ("info: & is frozen here, aspects evaluated at this point??",
9296 Freeze_Node (Ent), Ent);
9297 end if;
9298 end Check_Aspect_At_End_Of_Declarations;
55e8372b 9299
d9f6a4ee 9300 ----------------------------------
9301 -- Check_Aspect_At_Freeze_Point --
9302 ----------------------------------
9dc88aea 9303
d9f6a4ee 9304 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9305 Ident : constant Node_Id := Identifier (ASN);
9306 -- Identifier (use Entity field to save expression)
9dc88aea 9307
d9f6a4ee 9308 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9dc88aea 9309
d9f6a4ee 9310 T : Entity_Id := Empty;
9311 -- Type required for preanalyze call
9dc88aea 9312
d9f6a4ee 9313 begin
9314 -- On entry to this procedure, Entity (Ident) contains a copy of the
9315 -- original expression from the aspect, saved for this purpose.
9dc88aea 9316
d9f6a4ee 9317 -- On exit from this procedure Entity (Ident) is unchanged, still
9318 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9319 -- of the expression, preanalyzed just after the freeze point.
d97beb2f 9320
d9f6a4ee 9321 -- Make a copy of the expression to be preanalyzed
d97beb2f 9322
d9f6a4ee 9323 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
d97beb2f 9324
d9f6a4ee 9325 -- Find type for preanalyze call
d97beb2f 9326
d9f6a4ee 9327 case A_Id is
9dc88aea 9328
d9f6a4ee 9329 -- No_Aspect should be impossible
d97beb2f 9330
d9f6a4ee 9331 when No_Aspect =>
9332 raise Program_Error;
9333
9334 -- Aspects taking an optional boolean argument
d97beb2f 9335
99378362 9336 when Boolean_Aspects
9337 | Library_Unit_Aspects
9338 =>
d9f6a4ee 9339 T := Standard_Boolean;
d7c2851f 9340
d9f6a4ee 9341 -- Aspects corresponding to attribute definition clauses
9dc88aea 9342
d9f6a4ee 9343 when Aspect_Address =>
9344 T := RTE (RE_Address);
9dc88aea 9345
d9f6a4ee 9346 when Aspect_Attach_Handler =>
9347 T := RTE (RE_Interrupt_ID);
d7c2851f 9348
99378362 9349 when Aspect_Bit_Order
9350 | Aspect_Scalar_Storage_Order
9351 =>
d9f6a4ee 9352 T := RTE (RE_Bit_Order);
d7c2851f 9353
d9f6a4ee 9354 when Aspect_Convention =>
9355 return;
d7c2851f 9356
d9f6a4ee 9357 when Aspect_CPU =>
9358 T := RTE (RE_CPU_Range);
d7c2851f 9359
d9f6a4ee 9360 -- Default_Component_Value is resolved with the component type
d7c2851f 9361
d9f6a4ee 9362 when Aspect_Default_Component_Value =>
9363 T := Component_Type (Entity (ASN));
d7c2851f 9364
647fab54 9365 when Aspect_Default_Storage_Pool =>
9366 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9367
d9f6a4ee 9368 -- Default_Value is resolved with the type entity in question
d7c2851f 9369
d9f6a4ee 9370 when Aspect_Default_Value =>
9371 T := Entity (ASN);
9dc88aea 9372
d9f6a4ee 9373 when Aspect_Dispatching_Domain =>
9374 T := RTE (RE_Dispatching_Domain);
9dc88aea 9375
d9f6a4ee 9376 when Aspect_External_Tag =>
9377 T := Standard_String;
9dc88aea 9378
d9f6a4ee 9379 when Aspect_External_Name =>
9380 T := Standard_String;
9dc88aea 9381
d9f6a4ee 9382 when Aspect_Link_Name =>
9383 T := Standard_String;
9dc88aea 9384
99378362 9385 when Aspect_Interrupt_Priority
9386 | Aspect_Priority
9387 =>
d9f6a4ee 9388 T := Standard_Integer;
d97beb2f 9389
d9f6a4ee 9390 when Aspect_Relative_Deadline =>
9391 T := RTE (RE_Time_Span);
d97beb2f 9392
e6ce0468 9393 when Aspect_Secondary_Stack_Size =>
9394 T := Standard_Integer;
9395
d9f6a4ee 9396 when Aspect_Small =>
edfb7dbc 9397
9398 -- Note that the expression can be of any real type (not just a
9399 -- real universal literal) as long as it is a static constant.
eba9690d 9400
9401 T := Any_Real;
490beba6 9402
d9f6a4ee 9403 -- For a simple storage pool, we have to retrieve the type of the
9404 -- pool object associated with the aspect's corresponding attribute
9405 -- definition clause.
490beba6 9406
d9f6a4ee 9407 when Aspect_Simple_Storage_Pool =>
9408 T := Etype (Expression (Aspect_Rep_Item (ASN)));
d97beb2f 9409
d9f6a4ee 9410 when Aspect_Storage_Pool =>
9411 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
d97beb2f 9412
99378362 9413 when Aspect_Alignment
9414 | Aspect_Component_Size
9415 | Aspect_Machine_Radix
9416 | Aspect_Object_Size
9417 | Aspect_Size
9418 | Aspect_Storage_Size
9419 | Aspect_Stream_Size
9420 | Aspect_Value_Size
9421 =>
d9f6a4ee 9422 T := Any_Integer;
9dc88aea 9423
04ae062f 9424 when Aspect_Linker_Section =>
9425 T := Standard_String;
9426
d9f6a4ee 9427 when Aspect_Synchronization =>
9428 return;
7d20685d 9429
d9f6a4ee 9430 -- Special case, the expression of these aspects is just an entity
9431 -- that does not need any resolution, so just analyze.
7d20685d 9432
99378362 9433 when Aspect_Input
9434 | Aspect_Output
9435 | Aspect_Read
9436 | Aspect_Suppress
9437 | Aspect_Unsuppress
9438 | Aspect_Warnings
9439 | Aspect_Write
9440 =>
d9f6a4ee 9441 Analyze (Expression (ASN));
9442 return;
7d20685d 9443
d9f6a4ee 9444 -- Same for Iterator aspects, where the expression is a function
9445 -- name. Legality rules are checked separately.
89f1e35c 9446
99378362 9447 when Aspect_Constant_Indexing
9448 | Aspect_Default_Iterator
9449 | Aspect_Iterator_Element
9450 | Aspect_Variable_Indexing
9451 =>
d9f6a4ee 9452 Analyze (Expression (ASN));
9453 return;
7d20685d 9454
b3f8228a 9455 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9456
9457 when Aspect_Iterable =>
3061ffde 9458 T := Entity (ASN);
9459
b3f8228a 9460 declare
a9f5fea7 9461 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
3061ffde 9462 Assoc : Node_Id;
9463 Expr : Node_Id;
a9f5fea7 9464
b3f8228a 9465 begin
a9f5fea7 9466 if Cursor = Any_Type then
9467 return;
9468 end if;
9469
b3f8228a 9470 Assoc := First (Component_Associations (Expression (ASN)));
9471 while Present (Assoc) loop
3061ffde 9472 Expr := Expression (Assoc);
9473 Analyze (Expr);
a9f5fea7 9474
9475 if not Error_Posted (Expr) then
9476 Resolve_Iterable_Operation
9477 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9478 end if;
9479
b3f8228a 9480 Next (Assoc);
9481 end loop;
9482 end;
3061ffde 9483
b3f8228a 9484 return;
9485
d9f6a4ee 9486 -- Invariant/Predicate take boolean expressions
7d20685d 9487
99378362 9488 when Aspect_Dynamic_Predicate
9489 | Aspect_Invariant
9490 | Aspect_Predicate
9491 | Aspect_Static_Predicate
9492 | Aspect_Type_Invariant
9493 =>
d9f6a4ee 9494 T := Standard_Boolean;
7d20685d 9495
fdec445e 9496 when Aspect_Predicate_Failure =>
9497 T := Standard_String;
9498
d9f6a4ee 9499 -- Here is the list of aspects that don't require delay analysis
89f1e35c 9500
99378362 9501 when Aspect_Abstract_State
9502 | Aspect_Annotate
9503 | Aspect_Async_Readers
9504 | Aspect_Async_Writers
9505 | Aspect_Constant_After_Elaboration
9506 | Aspect_Contract_Cases
9507 | Aspect_Default_Initial_Condition
9508 | Aspect_Depends
9509 | Aspect_Dimension
9510 | Aspect_Dimension_System
9511 | Aspect_Effective_Reads
9512 | Aspect_Effective_Writes
9513 | Aspect_Extensions_Visible
9514 | Aspect_Ghost
9515 | Aspect_Global
9516 | Aspect_Implicit_Dereference
9517 | Aspect_Initial_Condition
9518 | Aspect_Initializes
9519 | Aspect_Max_Queue_Length
9520 | Aspect_Obsolescent
9521 | Aspect_Part_Of
9522 | Aspect_Post
9523 | Aspect_Postcondition
9524 | Aspect_Pre
9525 | Aspect_Precondition
9526 | Aspect_Refined_Depends
9527 | Aspect_Refined_Global
9528 | Aspect_Refined_Post
9529 | Aspect_Refined_State
9530 | Aspect_SPARK_Mode
9531 | Aspect_Test_Case
9532 | Aspect_Unimplemented
9533 | Aspect_Volatile_Function
9534 =>
d9f6a4ee 9535 raise Program_Error;
2b184b2f 9536
d9f6a4ee 9537 end case;
2b184b2f 9538
d9f6a4ee 9539 -- Do the preanalyze call
2b184b2f 9540
d9f6a4ee 9541 Preanalyze_Spec_Expression (Expression (ASN), T);
9542 end Check_Aspect_At_Freeze_Point;
2b184b2f 9543
d9f6a4ee 9544 -----------------------------------
9545 -- Check_Constant_Address_Clause --
9546 -----------------------------------
2b184b2f 9547
d9f6a4ee 9548 procedure Check_Constant_Address_Clause
9549 (Expr : Node_Id;
9550 U_Ent : Entity_Id)
9551 is
9552 procedure Check_At_Constant_Address (Nod : Node_Id);
9553 -- Checks that the given node N represents a name whose 'Address is
9554 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9555 -- address value is the same at the point of declaration of U_Ent and at
9556 -- the time of elaboration of the address clause.
84ed7523 9557
d9f6a4ee 9558 procedure Check_Expr_Constants (Nod : Node_Id);
9559 -- Checks that Nod meets the requirements for a constant address clause
9560 -- in the sense of the enclosing procedure.
84ed7523 9561
d9f6a4ee 9562 procedure Check_List_Constants (Lst : List_Id);
9563 -- Check that all elements of list Lst meet the requirements for a
9564 -- constant address clause in the sense of the enclosing procedure.
84ed7523 9565
d9f6a4ee 9566 -------------------------------
9567 -- Check_At_Constant_Address --
9568 -------------------------------
84ed7523 9569
d9f6a4ee 9570 procedure Check_At_Constant_Address (Nod : Node_Id) is
9571 begin
9572 if Is_Entity_Name (Nod) then
9573 if Present (Address_Clause (Entity ((Nod)))) then
9574 Error_Msg_NE
9575 ("invalid address clause for initialized object &!",
d9f6a4ee 9576 Nod, U_Ent);
99378362 9577 Error_Msg_NE
9578 ("address for& cannot depend on another address clause! "
9579 & "(RM 13.1(22))!", Nod, U_Ent);
84ed7523 9580
d9f6a4ee 9581 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9582 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9583 then
9584 Error_Msg_NE
9585 ("invalid address clause for initialized object &!",
9586 Nod, U_Ent);
9587 Error_Msg_Node_2 := U_Ent;
9588 Error_Msg_NE
9589 ("\& must be defined before & (RM 13.1(22))!",
9590 Nod, Entity (Nod));
9591 end if;
7d20685d 9592
d9f6a4ee 9593 elsif Nkind (Nod) = N_Selected_Component then
9594 declare
9595 T : constant Entity_Id := Etype (Prefix (Nod));
59f3e675 9596
d9f6a4ee 9597 begin
9598 if (Is_Record_Type (T)
9599 and then Has_Discriminants (T))
9600 or else
9601 (Is_Access_Type (T)
f02a9a9a 9602 and then Is_Record_Type (Designated_Type (T))
9603 and then Has_Discriminants (Designated_Type (T)))
d9f6a4ee 9604 then
9605 Error_Msg_NE
9606 ("invalid address clause for initialized object &!",
9607 Nod, U_Ent);
9608 Error_Msg_N
99378362 9609 ("\address cannot depend on component of discriminated "
9610 & "record (RM 13.1(22))!", Nod);
d9f6a4ee 9611 else
9612 Check_At_Constant_Address (Prefix (Nod));
9613 end if;
9614 end;
89cc7147 9615
d9f6a4ee 9616 elsif Nkind (Nod) = N_Indexed_Component then
9617 Check_At_Constant_Address (Prefix (Nod));
9618 Check_List_Constants (Expressions (Nod));
89cc7147 9619
84ed7523 9620 else
d9f6a4ee 9621 Check_Expr_Constants (Nod);
84ed7523 9622 end if;
d9f6a4ee 9623 end Check_At_Constant_Address;
81b424ac 9624
d9f6a4ee 9625 --------------------------
9626 -- Check_Expr_Constants --
9627 --------------------------
7b9b2f05 9628
d9f6a4ee 9629 procedure Check_Expr_Constants (Nod : Node_Id) is
9630 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9631 Ent : Entity_Id := Empty;
7b9b2f05 9632
d9f6a4ee 9633 begin
9634 if Nkind (Nod) in N_Has_Etype
9635 and then Etype (Nod) = Any_Type
7b9b2f05 9636 then
d9f6a4ee 9637 return;
309c3053 9638 end if;
9639
d9f6a4ee 9640 case Nkind (Nod) is
99378362 9641 when N_Empty
9642 | N_Error
9643 =>
d9f6a4ee 9644 return;
7d20685d 9645
99378362 9646 when N_Expanded_Name
9647 | N_Identifier
9648 =>
d9f6a4ee 9649 Ent := Entity (Nod);
7d20685d 9650
d9f6a4ee 9651 -- We need to look at the original node if it is different
9652 -- from the node, since we may have rewritten things and
9653 -- substituted an identifier representing the rewrite.
7d20685d 9654
d9f6a4ee 9655 if Original_Node (Nod) /= Nod then
9656 Check_Expr_Constants (Original_Node (Nod));
7d20685d 9657
d9f6a4ee 9658 -- If the node is an object declaration without initial
9659 -- value, some code has been expanded, and the expression
9660 -- is not constant, even if the constituents might be
9661 -- acceptable, as in A'Address + offset.
7d20685d 9662
d9f6a4ee 9663 if Ekind (Ent) = E_Variable
9664 and then
9665 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9666 and then
9667 No (Expression (Declaration_Node (Ent)))
9668 then
9669 Error_Msg_NE
9670 ("invalid address clause for initialized object &!",
9671 Nod, U_Ent);
89f1e35c 9672
d9f6a4ee 9673 -- If entity is constant, it may be the result of expanding
9674 -- a check. We must verify that its declaration appears
9675 -- before the object in question, else we also reject the
9676 -- address clause.
7d20685d 9677
d9f6a4ee 9678 elsif Ekind (Ent) = E_Constant
9679 and then In_Same_Source_Unit (Ent, U_Ent)
9680 and then Sloc (Ent) > Loc_U_Ent
9681 then
9682 Error_Msg_NE
9683 ("invalid address clause for initialized object &!",
9684 Nod, U_Ent);
9685 end if;
7d20685d 9686
d9f6a4ee 9687 return;
9688 end if;
7d20685d 9689
d9f6a4ee 9690 -- Otherwise look at the identifier and see if it is OK
7d20685d 9691
d9f6a4ee 9692 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9693 or else Is_Type (Ent)
9694 then
9695 return;
7d20685d 9696
f02a9a9a 9697 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9698
d9f6a4ee 9699 -- This is the case where we must have Ent defined before
9700 -- U_Ent. Clearly if they are in different units this
9701 -- requirement is met since the unit containing Ent is
9702 -- already processed.
7d20685d 9703
d9f6a4ee 9704 if not In_Same_Source_Unit (Ent, U_Ent) then
9705 return;
7d20685d 9706
d9f6a4ee 9707 -- Otherwise location of Ent must be before the location
9708 -- of U_Ent, that's what prior defined means.
7d20685d 9709
d9f6a4ee 9710 elsif Sloc (Ent) < Loc_U_Ent then
9711 return;
6c545057 9712
d9f6a4ee 9713 else
9714 Error_Msg_NE
9715 ("invalid address clause for initialized object &!",
9716 Nod, U_Ent);
9717 Error_Msg_Node_2 := U_Ent;
9718 Error_Msg_NE
9719 ("\& must be defined before & (RM 13.1(22))!",
9720 Nod, Ent);
9721 end if;
37c6e44c 9722
d9f6a4ee 9723 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9724 Check_Expr_Constants (Original_Node (Nod));
6c545057 9725
d9f6a4ee 9726 else
9727 Error_Msg_NE
9728 ("invalid address clause for initialized object &!",
9729 Nod, U_Ent);
3cdbaa5a 9730
d9f6a4ee 9731 if Comes_From_Source (Ent) then
9732 Error_Msg_NE
9733 ("\reference to variable& not allowed"
9734 & " (RM 13.1(22))!", Nod, Ent);
9735 else
9736 Error_Msg_N
9737 ("non-static expression not allowed"
9738 & " (RM 13.1(22))!", Nod);
9739 end if;
9740 end if;
3cdbaa5a 9741
d9f6a4ee 9742 when N_Integer_Literal =>
7f694ca2 9743
d9f6a4ee 9744 -- If this is a rewritten unchecked conversion, in a system
9745 -- where Address is an integer type, always use the base type
9746 -- for a literal value. This is user-friendly and prevents
9747 -- order-of-elaboration issues with instances of unchecked
9748 -- conversion.
3cdbaa5a 9749
d9f6a4ee 9750 if Nkind (Original_Node (Nod)) = N_Function_Call then
9751 Set_Etype (Nod, Base_Type (Etype (Nod)));
9752 end if;
e1cedbae 9753
99378362 9754 when N_Character_Literal
9755 | N_Real_Literal
9756 | N_String_Literal
9757 =>
d9f6a4ee 9758 return;
7d20685d 9759
d9f6a4ee 9760 when N_Range =>
9761 Check_Expr_Constants (Low_Bound (Nod));
9762 Check_Expr_Constants (High_Bound (Nod));
231eb581 9763
d9f6a4ee 9764 when N_Explicit_Dereference =>
9765 Check_Expr_Constants (Prefix (Nod));
231eb581 9766
d9f6a4ee 9767 when N_Indexed_Component =>
9768 Check_Expr_Constants (Prefix (Nod));
9769 Check_List_Constants (Expressions (Nod));
7d20685d 9770
d9f6a4ee 9771 when N_Slice =>
9772 Check_Expr_Constants (Prefix (Nod));
9773 Check_Expr_Constants (Discrete_Range (Nod));
cb4c311d 9774
d9f6a4ee 9775 when N_Selected_Component =>
9776 Check_Expr_Constants (Prefix (Nod));
6144c105 9777
d9f6a4ee 9778 when N_Attribute_Reference =>
9779 if Nam_In (Attribute_Name (Nod), Name_Address,
9780 Name_Access,
9781 Name_Unchecked_Access,
9782 Name_Unrestricted_Access)
9783 then
9784 Check_At_Constant_Address (Prefix (Nod));
6144c105 9785
d9f6a4ee 9786 else
9787 Check_Expr_Constants (Prefix (Nod));
9788 Check_List_Constants (Expressions (Nod));
9789 end if;
a7a4a7c2 9790
d9f6a4ee 9791 when N_Aggregate =>
9792 Check_List_Constants (Component_Associations (Nod));
9793 Check_List_Constants (Expressions (Nod));
7d20685d 9794
d9f6a4ee 9795 when N_Component_Association =>
9796 Check_Expr_Constants (Expression (Nod));
e1cedbae 9797
d9f6a4ee 9798 when N_Extension_Aggregate =>
9799 Check_Expr_Constants (Ancestor_Part (Nod));
9800 Check_List_Constants (Component_Associations (Nod));
9801 Check_List_Constants (Expressions (Nod));
3cdbaa5a 9802
d9f6a4ee 9803 when N_Null =>
9804 return;
3cdbaa5a 9805
99378362 9806 when N_Binary_Op
9807 | N_Membership_Test
9808 | N_Short_Circuit
9809 =>
d9f6a4ee 9810 Check_Expr_Constants (Left_Opnd (Nod));
9811 Check_Expr_Constants (Right_Opnd (Nod));
e1cedbae 9812
d9f6a4ee 9813 when N_Unary_Op =>
9814 Check_Expr_Constants (Right_Opnd (Nod));
7f694ca2 9815
99378362 9816 when N_Allocator
9817 | N_Qualified_Expression
9818 | N_Type_Conversion
9819 | N_Unchecked_Type_Conversion
9820 =>
d9f6a4ee 9821 Check_Expr_Constants (Expression (Nod));
47a46747 9822
d9f6a4ee 9823 when N_Function_Call =>
9824 if not Is_Pure (Entity (Name (Nod))) then
9825 Error_Msg_NE
9826 ("invalid address clause for initialized object &!",
9827 Nod, U_Ent);
7f694ca2 9828
d9f6a4ee 9829 Error_Msg_NE
9830 ("\function & is not pure (RM 13.1(22))!",
9831 Nod, Entity (Name (Nod)));
b55f7641 9832
d9f6a4ee 9833 else
9834 Check_List_Constants (Parameter_Associations (Nod));
9835 end if;
b55f7641 9836
d9f6a4ee 9837 when N_Parameter_Association =>
9838 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7d20685d 9839
d9f6a4ee 9840 when others =>
9841 Error_Msg_NE
9842 ("invalid address clause for initialized object &!",
9843 Nod, U_Ent);
9844 Error_Msg_NE
9845 ("\must be constant defined before& (RM 13.1(22))!",
9846 Nod, U_Ent);
9847 end case;
9848 end Check_Expr_Constants;
7d20685d 9849
d9f6a4ee 9850 --------------------------
9851 -- Check_List_Constants --
9852 --------------------------
89f1e35c 9853
d9f6a4ee 9854 procedure Check_List_Constants (Lst : List_Id) is
9855 Nod1 : Node_Id;
7d20685d 9856
d9f6a4ee 9857 begin
9858 if Present (Lst) then
9859 Nod1 := First (Lst);
9860 while Present (Nod1) loop
9861 Check_Expr_Constants (Nod1);
9862 Next (Nod1);
9863 end loop;
9864 end if;
9865 end Check_List_Constants;
81b424ac 9866
d9f6a4ee 9867 -- Start of processing for Check_Constant_Address_Clause
81b424ac 9868
d9f6a4ee 9869 begin
9870 -- If rep_clauses are to be ignored, no need for legality checks. In
9c7948d7 9871 -- particular, no need to pester user about rep clauses that violate the
9872 -- rule on constant addresses, given that these clauses will be removed
9873 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9874 -- we want to relax these checks.
7d20685d 9875
f1a9be43 9876 if not Ignore_Rep_Clauses and not CodePeer_Mode then
d9f6a4ee 9877 Check_Expr_Constants (Expr);
9878 end if;
9879 end Check_Constant_Address_Clause;
7d20685d 9880
6653b695 9881 ---------------------------
9882 -- Check_Pool_Size_Clash --
9883 ---------------------------
9884
9885 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9886 Post : Node_Id;
9887
9888 begin
9889 -- We need to find out which one came first. Note that in the case of
9890 -- aspects mixed with pragmas there are cases where the processing order
9891 -- is reversed, which is why we do the check here.
9892
9893 if Sloc (SP) < Sloc (SS) then
9894 Error_Msg_Sloc := Sloc (SP);
9895 Post := SS;
9896 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9897
9898 else
9899 Error_Msg_Sloc := Sloc (SS);
9900 Post := SP;
9901 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9902 end if;
9903
9904 Error_Msg_N
9905 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9906 end Check_Pool_Size_Clash;
9907
d9f6a4ee 9908 ----------------------------------------
9909 -- Check_Record_Representation_Clause --
9910 ----------------------------------------
85696508 9911
d9f6a4ee 9912 procedure Check_Record_Representation_Clause (N : Node_Id) is
9913 Loc : constant Source_Ptr := Sloc (N);
9914 Ident : constant Node_Id := Identifier (N);
9915 Rectype : Entity_Id;
9916 Fent : Entity_Id;
9917 CC : Node_Id;
9918 Fbit : Uint;
9919 Lbit : Uint;
9920 Hbit : Uint := Uint_0;
9921 Comp : Entity_Id;
9922 Pcomp : Entity_Id;
89f1e35c 9923
d9f6a4ee 9924 Max_Bit_So_Far : Uint;
9925 -- Records the maximum bit position so far. If all field positions
9926 -- are monotonically increasing, then we can skip the circuit for
9927 -- checking for overlap, since no overlap is possible.
85696508 9928
d9f6a4ee 9929 Tagged_Parent : Entity_Id := Empty;
53b51b7a 9930 -- This is set in the case of an extension for which we have either a
9931 -- size clause or Is_Fully_Repped_Tagged_Type True (indicating that all
9932 -- components are positioned by record representation clauses) on the
9933 -- parent type. In this case we check for overlap between components of
9934 -- this tagged type and the parent component. Tagged_Parent will point
9935 -- to this parent type. For all other cases, Tagged_Parent is Empty.
7d20685d 9936
d9f6a4ee 9937 Parent_Last_Bit : Uint;
9938 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9939 -- last bit position for any field in the parent type. We only need to
9940 -- check overlap for fields starting below this point.
7d20685d 9941
d9f6a4ee 9942 Overlap_Check_Required : Boolean;
9943 -- Used to keep track of whether or not an overlap check is required
7d20685d 9944
d9f6a4ee 9945 Overlap_Detected : Boolean := False;
9946 -- Set True if an overlap is detected
d6f39728 9947
d9f6a4ee 9948 Ccount : Natural := 0;
9949 -- Number of component clauses in record rep clause
d6f39728 9950
d9f6a4ee 9951 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9952 -- Given two entities for record components or discriminants, checks
9953 -- if they have overlapping component clauses and issues errors if so.
d6f39728 9954
d9f6a4ee 9955 procedure Find_Component;
9956 -- Finds component entity corresponding to current component clause (in
9957 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9958 -- start/stop bits for the field. If there is no matching component or
9959 -- if the matching component does not have a component clause, then
9960 -- that's an error and Comp is set to Empty, but no error message is
9961 -- issued, since the message was already given. Comp is also set to
9962 -- Empty if the current "component clause" is in fact a pragma.
d6f39728 9963
d9f6a4ee 9964 -----------------------------
9965 -- Check_Component_Overlap --
9966 -----------------------------
9967
9968 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9969 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9970 CC2 : constant Node_Id := Component_Clause (C2_Ent);
d6f39728 9971
d6f39728 9972 begin
d9f6a4ee 9973 if Present (CC1) and then Present (CC2) then
d6f39728 9974
d9f6a4ee 9975 -- Exclude odd case where we have two tag components in the same
9976 -- record, both at location zero. This seems a bit strange, but
9977 -- it seems to happen in some circumstances, perhaps on an error.
9978
9979 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9980 return;
d6f39728 9981 end if;
9982
d9f6a4ee 9983 -- Here we check if the two fields overlap
9984
d6f39728 9985 declare
d9f6a4ee 9986 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9987 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9988 E1 : constant Uint := S1 + Esize (C1_Ent);
9989 E2 : constant Uint := S2 + Esize (C2_Ent);
d6f39728 9990
9991 begin
d9f6a4ee 9992 if E2 <= S1 or else E1 <= S2 then
9993 null;
d6f39728 9994 else
d9f6a4ee 9995 Error_Msg_Node_2 := Component_Name (CC2);
9996 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9997 Error_Msg_Node_1 := Component_Name (CC1);
9998 Error_Msg_N
9999 ("component& overlaps & #", Component_Name (CC1));
10000 Overlap_Detected := True;
d6f39728 10001 end if;
10002 end;
d6f39728 10003 end if;
d9f6a4ee 10004 end Check_Component_Overlap;
d6f39728 10005
d9f6a4ee 10006 --------------------
10007 -- Find_Component --
10008 --------------------
9dfe12ae 10009
d9f6a4ee 10010 procedure Find_Component is
9dfe12ae 10011
d9f6a4ee 10012 procedure Search_Component (R : Entity_Id);
10013 -- Search components of R for a match. If found, Comp is set
9dfe12ae 10014
d9f6a4ee 10015 ----------------------
10016 -- Search_Component --
10017 ----------------------
e7b2d6bc 10018
d9f6a4ee 10019 procedure Search_Component (R : Entity_Id) is
10020 begin
10021 Comp := First_Component_Or_Discriminant (R);
10022 while Present (Comp) loop
e7b2d6bc 10023
d9f6a4ee 10024 -- Ignore error of attribute name for component name (we
10025 -- already gave an error message for this, so no need to
10026 -- complain here)
e7b2d6bc 10027
d9f6a4ee 10028 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
10029 null;
10030 else
10031 exit when Chars (Comp) = Chars (Component_Name (CC));
9dfe12ae 10032 end if;
10033
d9f6a4ee 10034 Next_Component_Or_Discriminant (Comp);
10035 end loop;
10036 end Search_Component;
d6f39728 10037
d9f6a4ee 10038 -- Start of processing for Find_Component
d6f39728 10039
d9f6a4ee 10040 begin
10041 -- Return with Comp set to Empty if we have a pragma
d6f39728 10042
d9f6a4ee 10043 if Nkind (CC) = N_Pragma then
10044 Comp := Empty;
10045 return;
10046 end if;
d6f39728 10047
d9f6a4ee 10048 -- Search current record for matching component
d6f39728 10049
d9f6a4ee 10050 Search_Component (Rectype);
9dfe12ae 10051
d9f6a4ee 10052 -- If not found, maybe component of base type discriminant that is
10053 -- absent from statically constrained first subtype.
e7b2d6bc 10054
d9f6a4ee 10055 if No (Comp) then
10056 Search_Component (Base_Type (Rectype));
10057 end if;
e7b2d6bc 10058
d9f6a4ee 10059 -- If no component, or the component does not reference the component
10060 -- clause in question, then there was some previous error for which
10061 -- we already gave a message, so just return with Comp Empty.
d6f39728 10062
d9f6a4ee 10063 if No (Comp) or else Component_Clause (Comp) /= CC then
10064 Check_Error_Detected;
10065 Comp := Empty;
93735cb8 10066
d9f6a4ee 10067 -- Normal case where we have a component clause
93735cb8 10068
d9f6a4ee 10069 else
10070 Fbit := Component_Bit_Offset (Comp);
10071 Lbit := Fbit + Esize (Comp) - 1;
10072 end if;
10073 end Find_Component;
93735cb8 10074
d9f6a4ee 10075 -- Start of processing for Check_Record_Representation_Clause
d6f39728 10076
d9f6a4ee 10077 begin
10078 Find_Type (Ident);
10079 Rectype := Entity (Ident);
d6f39728 10080
d9f6a4ee 10081 if Rectype = Any_Type then
10082 return;
d9f6a4ee 10083 end if;
d6f39728 10084
53b51b7a 10085 Rectype := Underlying_Type (Rectype);
10086
d9f6a4ee 10087 -- See if we have a fully repped derived tagged type
d6f39728 10088
d9f6a4ee 10089 declare
10090 PS : constant Entity_Id := Parent_Subtype (Rectype);
d6f39728 10091
d9f6a4ee 10092 begin
53b51b7a 10093 if Present (PS) and then Known_Static_RM_Size (PS) then
10094 Tagged_Parent := PS;
10095 Parent_Last_Bit := RM_Size (PS) - 1;
10096
10097 elsif Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
d9f6a4ee 10098 Tagged_Parent := PS;
d6f39728 10099
d9f6a4ee 10100 -- Find maximum bit of any component of the parent type
d6f39728 10101
d9f6a4ee 10102 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
10103 Pcomp := First_Entity (Tagged_Parent);
10104 while Present (Pcomp) loop
10105 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
10106 if Component_Bit_Offset (Pcomp) /= No_Uint
10107 and then Known_Static_Esize (Pcomp)
10108 then
10109 Parent_Last_Bit :=
10110 UI_Max
10111 (Parent_Last_Bit,
10112 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
10113 end if;
b7df4cda 10114 else
10115
10116 -- Skip anonymous types generated for constrained array
10117 -- or record components.
d9f6a4ee 10118
b7df4cda 10119 null;
d6f39728 10120 end if;
b7df4cda 10121
10122 Next_Entity (Pcomp);
d9f6a4ee 10123 end loop;
10124 end if;
10125 end;
d6f39728 10126
d9f6a4ee 10127 -- All done if no component clauses
d6f39728 10128
d9f6a4ee 10129 CC := First (Component_Clauses (N));
d6f39728 10130
d9f6a4ee 10131 if No (CC) then
10132 return;
10133 end if;
d6f39728 10134
d9f6a4ee 10135 -- If a tag is present, then create a component clause that places it
10136 -- at the start of the record (otherwise gigi may place it after other
10137 -- fields that have rep clauses).
d6f39728 10138
d9f6a4ee 10139 Fent := First_Entity (Rectype);
d6f39728 10140
d9f6a4ee 10141 if Nkind (Fent) = N_Defining_Identifier
10142 and then Chars (Fent) = Name_uTag
10143 then
10144 Set_Component_Bit_Offset (Fent, Uint_0);
10145 Set_Normalized_Position (Fent, Uint_0);
10146 Set_Normalized_First_Bit (Fent, Uint_0);
10147 Set_Normalized_Position_Max (Fent, Uint_0);
10148 Init_Esize (Fent, System_Address_Size);
d6f39728 10149
d9f6a4ee 10150 Set_Component_Clause (Fent,
10151 Make_Component_Clause (Loc,
10152 Component_Name => Make_Identifier (Loc, Name_uTag),
d6f39728 10153
d9f6a4ee 10154 Position => Make_Integer_Literal (Loc, Uint_0),
10155 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10156 Last_Bit =>
10157 Make_Integer_Literal (Loc,
10158 UI_From_Int (System_Address_Size))));
d6f39728 10159
d9f6a4ee 10160 Ccount := Ccount + 1;
10161 end if;
d6f39728 10162
d9f6a4ee 10163 Max_Bit_So_Far := Uint_Minus_1;
10164 Overlap_Check_Required := False;
d6f39728 10165
d9f6a4ee 10166 -- Process the component clauses
d6f39728 10167
d9f6a4ee 10168 while Present (CC) loop
10169 Find_Component;
d6f39728 10170
d9f6a4ee 10171 if Present (Comp) then
10172 Ccount := Ccount + 1;
d6f39728 10173
d9f6a4ee 10174 -- We need a full overlap check if record positions non-monotonic
d6f39728 10175
d9f6a4ee 10176 if Fbit <= Max_Bit_So_Far then
10177 Overlap_Check_Required := True;
10178 end if;
d6f39728 10179
d9f6a4ee 10180 Max_Bit_So_Far := Lbit;
d6f39728 10181
d9f6a4ee 10182 -- Check bit position out of range of specified size
01cb2726 10183
d9f6a4ee 10184 if Has_Size_Clause (Rectype)
10185 and then RM_Size (Rectype) <= Lbit
10186 then
10187 Error_Msg_N
10188 ("bit number out of range of specified size",
10189 Last_Bit (CC));
d6f39728 10190
53b51b7a 10191 -- Check for overlap with tag or parent component
67278d60 10192
d9f6a4ee 10193 else
10194 if Is_Tagged_Type (Rectype)
10195 and then Fbit < System_Address_Size
10196 then
10197 Error_Msg_NE
10198 ("component overlaps tag field of&",
10199 Component_Name (CC), Rectype);
10200 Overlap_Detected := True;
53b51b7a 10201
10202 elsif Present (Tagged_Parent)
10203 and then Fbit <= Parent_Last_Bit
10204 then
10205 Error_Msg_NE
10206 ("component overlaps parent field of&",
10207 Component_Name (CC), Rectype);
10208 Overlap_Detected := True;
d9f6a4ee 10209 end if;
67278d60 10210
d9f6a4ee 10211 if Hbit < Lbit then
10212 Hbit := Lbit;
10213 end if;
10214 end if;
d9f6a4ee 10215 end if;
67278d60 10216
d9f6a4ee 10217 Next (CC);
10218 end loop;
47495553 10219
d9f6a4ee 10220 -- Now that we have processed all the component clauses, check for
10221 -- overlap. We have to leave this till last, since the components can
10222 -- appear in any arbitrary order in the representation clause.
67278d60 10223
d9f6a4ee 10224 -- We do not need this check if all specified ranges were monotonic,
10225 -- as recorded by Overlap_Check_Required being False at this stage.
67278d60 10226
d9f6a4ee 10227 -- This first section checks if there are any overlapping entries at
10228 -- all. It does this by sorting all entries and then seeing if there are
10229 -- any overlaps. If there are none, then that is decisive, but if there
10230 -- are overlaps, they may still be OK (they may result from fields in
10231 -- different variants).
67278d60 10232
d9f6a4ee 10233 if Overlap_Check_Required then
10234 Overlap_Check1 : declare
67278d60 10235
d9f6a4ee 10236 OC_Fbit : array (0 .. Ccount) of Uint;
10237 -- First-bit values for component clauses, the value is the offset
10238 -- of the first bit of the field from start of record. The zero
10239 -- entry is for use in sorting.
47495553 10240
d9f6a4ee 10241 OC_Lbit : array (0 .. Ccount) of Uint;
10242 -- Last-bit values for component clauses, the value is the offset
10243 -- of the last bit of the field from start of record. The zero
10244 -- entry is for use in sorting.
10245
10246 OC_Count : Natural := 0;
10247 -- Count of entries in OC_Fbit and OC_Lbit
67278d60 10248
d9f6a4ee 10249 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10250 -- Compare routine for Sort
67278d60 10251
d9f6a4ee 10252 procedure OC_Move (From : Natural; To : Natural);
10253 -- Move routine for Sort
67278d60 10254
d9f6a4ee 10255 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
67278d60 10256
d9f6a4ee 10257 -----------
10258 -- OC_Lt --
10259 -----------
67278d60 10260
d9f6a4ee 10261 function OC_Lt (Op1, Op2 : Natural) return Boolean is
67278d60 10262 begin
d9f6a4ee 10263 return OC_Fbit (Op1) < OC_Fbit (Op2);
10264 end OC_Lt;
67278d60 10265
d9f6a4ee 10266 -------------
10267 -- OC_Move --
10268 -------------
67278d60 10269
d9f6a4ee 10270 procedure OC_Move (From : Natural; To : Natural) is
10271 begin
10272 OC_Fbit (To) := OC_Fbit (From);
10273 OC_Lbit (To) := OC_Lbit (From);
10274 end OC_Move;
67278d60 10275
d9f6a4ee 10276 -- Start of processing for Overlap_Check
67278d60 10277
67278d60 10278 begin
d9f6a4ee 10279 CC := First (Component_Clauses (N));
10280 while Present (CC) loop
67278d60 10281
d9f6a4ee 10282 -- Exclude component clause already marked in error
67278d60 10283
d9f6a4ee 10284 if not Error_Posted (CC) then
10285 Find_Component;
10286
10287 if Present (Comp) then
10288 OC_Count := OC_Count + 1;
10289 OC_Fbit (OC_Count) := Fbit;
10290 OC_Lbit (OC_Count) := Lbit;
10291 end if;
67278d60 10292 end if;
10293
d9f6a4ee 10294 Next (CC);
67278d60 10295 end loop;
67278d60 10296
d9f6a4ee 10297 Sorting.Sort (OC_Count);
67278d60 10298
d9f6a4ee 10299 Overlap_Check_Required := False;
10300 for J in 1 .. OC_Count - 1 loop
10301 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10302 Overlap_Check_Required := True;
10303 exit;
10304 end if;
10305 end loop;
10306 end Overlap_Check1;
10307 end if;
67278d60 10308
d9f6a4ee 10309 -- If Overlap_Check_Required is still True, then we have to do the full
10310 -- scale overlap check, since we have at least two fields that do
10311 -- overlap, and we need to know if that is OK since they are in
10312 -- different variant, or whether we have a definite problem.
67278d60 10313
d9f6a4ee 10314 if Overlap_Check_Required then
10315 Overlap_Check2 : declare
10316 C1_Ent, C2_Ent : Entity_Id;
10317 -- Entities of components being checked for overlap
67278d60 10318
d9f6a4ee 10319 Clist : Node_Id;
10320 -- Component_List node whose Component_Items are being checked
67278d60 10321
d9f6a4ee 10322 Citem : Node_Id;
10323 -- Component declaration for component being checked
67278d60 10324
d9f6a4ee 10325 begin
10326 C1_Ent := First_Entity (Base_Type (Rectype));
67278d60 10327
d9f6a4ee 10328 -- Loop through all components in record. For each component check
10329 -- for overlap with any of the preceding elements on the component
10330 -- list containing the component and also, if the component is in
10331 -- a variant, check against components outside the case structure.
10332 -- This latter test is repeated recursively up the variant tree.
67278d60 10333
d9f6a4ee 10334 Main_Component_Loop : while Present (C1_Ent) loop
10335 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10336 goto Continue_Main_Component_Loop;
10337 end if;
67278d60 10338
d9f6a4ee 10339 -- Skip overlap check if entity has no declaration node. This
10340 -- happens with discriminants in constrained derived types.
10341 -- Possibly we are missing some checks as a result, but that
10342 -- does not seem terribly serious.
67278d60 10343
d9f6a4ee 10344 if No (Declaration_Node (C1_Ent)) then
10345 goto Continue_Main_Component_Loop;
10346 end if;
67278d60 10347
d9f6a4ee 10348 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
67278d60 10349
d9f6a4ee 10350 -- Loop through component lists that need checking. Check the
10351 -- current component list and all lists in variants above us.
67278d60 10352
d9f6a4ee 10353 Component_List_Loop : loop
67278d60 10354
d9f6a4ee 10355 -- If derived type definition, go to full declaration
10356 -- If at outer level, check discriminants if there are any.
67278d60 10357
d9f6a4ee 10358 if Nkind (Clist) = N_Derived_Type_Definition then
10359 Clist := Parent (Clist);
10360 end if;
67278d60 10361
d9f6a4ee 10362 -- Outer level of record definition, check discriminants
67278d60 10363
d9f6a4ee 10364 if Nkind_In (Clist, N_Full_Type_Declaration,
10365 N_Private_Type_Declaration)
67278d60 10366 then
d9f6a4ee 10367 if Has_Discriminants (Defining_Identifier (Clist)) then
10368 C2_Ent :=
10369 First_Discriminant (Defining_Identifier (Clist));
10370 while Present (C2_Ent) loop
10371 exit when C1_Ent = C2_Ent;
10372 Check_Component_Overlap (C1_Ent, C2_Ent);
10373 Next_Discriminant (C2_Ent);
10374 end loop;
10375 end if;
67278d60 10376
d9f6a4ee 10377 -- Record extension case
67278d60 10378
d9f6a4ee 10379 elsif Nkind (Clist) = N_Derived_Type_Definition then
10380 Clist := Empty;
67278d60 10381
d9f6a4ee 10382 -- Otherwise check one component list
67278d60 10383
d9f6a4ee 10384 else
10385 Citem := First (Component_Items (Clist));
10386 while Present (Citem) loop
10387 if Nkind (Citem) = N_Component_Declaration then
10388 C2_Ent := Defining_Identifier (Citem);
10389 exit when C1_Ent = C2_Ent;
10390 Check_Component_Overlap (C1_Ent, C2_Ent);
10391 end if;
67278d60 10392
d9f6a4ee 10393 Next (Citem);
10394 end loop;
10395 end if;
67278d60 10396
d9f6a4ee 10397 -- Check for variants above us (the parent of the Clist can
10398 -- be a variant, in which case its parent is a variant part,
10399 -- and the parent of the variant part is a component list
10400 -- whose components must all be checked against the current
10401 -- component for overlap).
67278d60 10402
d9f6a4ee 10403 if Nkind (Parent (Clist)) = N_Variant then
10404 Clist := Parent (Parent (Parent (Clist)));
67278d60 10405
d9f6a4ee 10406 -- Check for possible discriminant part in record, this
10407 -- is treated essentially as another level in the
10408 -- recursion. For this case the parent of the component
10409 -- list is the record definition, and its parent is the
10410 -- full type declaration containing the discriminant
10411 -- specifications.
10412
10413 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10414 Clist := Parent (Parent ((Clist)));
10415
10416 -- If neither of these two cases, we are at the top of
10417 -- the tree.
10418
10419 else
10420 exit Component_List_Loop;
10421 end if;
10422 end loop Component_List_Loop;
67278d60 10423
d9f6a4ee 10424 <<Continue_Main_Component_Loop>>
10425 Next_Entity (C1_Ent);
67278d60 10426
d9f6a4ee 10427 end loop Main_Component_Loop;
10428 end Overlap_Check2;
67278d60 10429 end if;
10430
d9f6a4ee 10431 -- The following circuit deals with warning on record holes (gaps). We
10432 -- skip this check if overlap was detected, since it makes sense for the
10433 -- programmer to fix this illegality before worrying about warnings.
67278d60 10434
d9f6a4ee 10435 if not Overlap_Detected and Warn_On_Record_Holes then
10436 Record_Hole_Check : declare
10437 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10438 -- Full declaration of record type
67278d60 10439
d9f6a4ee 10440 procedure Check_Component_List
10441 (CL : Node_Id;
10442 Sbit : Uint;
10443 DS : List_Id);
10444 -- Check component list CL for holes. The starting bit should be
10445 -- Sbit. which is zero for the main record component list and set
10446 -- appropriately for recursive calls for variants. DS is set to
10447 -- a list of discriminant specifications to be included in the
10448 -- consideration of components. It is No_List if none to consider.
67278d60 10449
d9f6a4ee 10450 --------------------------
10451 -- Check_Component_List --
10452 --------------------------
47495553 10453
d9f6a4ee 10454 procedure Check_Component_List
10455 (CL : Node_Id;
10456 Sbit : Uint;
10457 DS : List_Id)
10458 is
10459 Compl : Integer;
67278d60 10460
d9f6a4ee 10461 begin
10462 Compl := Integer (List_Length (Component_Items (CL)));
47495553 10463
d9f6a4ee 10464 if DS /= No_List then
10465 Compl := Compl + Integer (List_Length (DS));
10466 end if;
67278d60 10467
d9f6a4ee 10468 declare
10469 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10470 -- Gather components (zero entry is for sort routine)
67278d60 10471
d9f6a4ee 10472 Ncomps : Natural := 0;
10473 -- Number of entries stored in Comps (starting at Comps (1))
67278d60 10474
d9f6a4ee 10475 Citem : Node_Id;
10476 -- One component item or discriminant specification
67278d60 10477
d9f6a4ee 10478 Nbit : Uint;
10479 -- Starting bit for next component
67278d60 10480
d9f6a4ee 10481 CEnt : Entity_Id;
10482 -- Component entity
67278d60 10483
d9f6a4ee 10484 Variant : Node_Id;
10485 -- One variant
67278d60 10486
d9f6a4ee 10487 function Lt (Op1, Op2 : Natural) return Boolean;
10488 -- Compare routine for Sort
67278d60 10489
d9f6a4ee 10490 procedure Move (From : Natural; To : Natural);
10491 -- Move routine for Sort
67278d60 10492
d9f6a4ee 10493 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
67278d60 10494
d9f6a4ee 10495 --------
10496 -- Lt --
10497 --------
67278d60 10498
d9f6a4ee 10499 function Lt (Op1, Op2 : Natural) return Boolean is
10500 begin
10501 return Component_Bit_Offset (Comps (Op1))
10502 <
10503 Component_Bit_Offset (Comps (Op2));
10504 end Lt;
67278d60 10505
d9f6a4ee 10506 ----------
10507 -- Move --
10508 ----------
67278d60 10509
d9f6a4ee 10510 procedure Move (From : Natural; To : Natural) is
10511 begin
10512 Comps (To) := Comps (From);
10513 end Move;
67278d60 10514
d9f6a4ee 10515 begin
10516 -- Gather discriminants into Comp
67278d60 10517
d9f6a4ee 10518 if DS /= No_List then
10519 Citem := First (DS);
10520 while Present (Citem) loop
10521 if Nkind (Citem) = N_Discriminant_Specification then
10522 declare
10523 Ent : constant Entity_Id :=
10524 Defining_Identifier (Citem);
10525 begin
10526 if Ekind (Ent) = E_Discriminant then
10527 Ncomps := Ncomps + 1;
10528 Comps (Ncomps) := Ent;
10529 end if;
10530 end;
10531 end if;
67278d60 10532
d9f6a4ee 10533 Next (Citem);
10534 end loop;
10535 end if;
67278d60 10536
d9f6a4ee 10537 -- Gather component entities into Comp
67278d60 10538
d9f6a4ee 10539 Citem := First (Component_Items (CL));
10540 while Present (Citem) loop
10541 if Nkind (Citem) = N_Component_Declaration then
10542 Ncomps := Ncomps + 1;
10543 Comps (Ncomps) := Defining_Identifier (Citem);
10544 end if;
67278d60 10545
d9f6a4ee 10546 Next (Citem);
10547 end loop;
67278d60 10548
d9f6a4ee 10549 -- Now sort the component entities based on the first bit.
10550 -- Note we already know there are no overlapping components.
67278d60 10551
d9f6a4ee 10552 Sorting.Sort (Ncomps);
67278d60 10553
d9f6a4ee 10554 -- Loop through entries checking for holes
67278d60 10555
d9f6a4ee 10556 Nbit := Sbit;
10557 for J in 1 .. Ncomps loop
10558 CEnt := Comps (J);
67278d60 10559
f55a6472 10560 declare
10561 CBO : constant Uint := Component_Bit_Offset (CEnt);
10562
10563 begin
10564 -- Skip components with unknown offsets
10565
10566 if CBO /= No_Uint and then CBO >= 0 then
10567 Error_Msg_Uint_1 := CBO - Nbit;
67278d60 10568
f55a6472 10569 if Error_Msg_Uint_1 > 0 then
10570 Error_Msg_NE
10571 ("?H?^-bit gap before component&",
10572 Component_Name (Component_Clause (CEnt)),
10573 CEnt);
10574 end if;
10575
10576 Nbit := CBO + Esize (CEnt);
10577 end if;
10578 end;
d9f6a4ee 10579 end loop;
67278d60 10580
d9f6a4ee 10581 -- Process variant parts recursively if present
67278d60 10582
d9f6a4ee 10583 if Present (Variant_Part (CL)) then
10584 Variant := First (Variants (Variant_Part (CL)));
10585 while Present (Variant) loop
10586 Check_Component_List
10587 (Component_List (Variant), Nbit, No_List);
10588 Next (Variant);
10589 end loop;
67278d60 10590 end if;
d9f6a4ee 10591 end;
10592 end Check_Component_List;
67278d60 10593
d9f6a4ee 10594 -- Start of processing for Record_Hole_Check
67278d60 10595
d9f6a4ee 10596 begin
10597 declare
10598 Sbit : Uint;
67278d60 10599
d9f6a4ee 10600 begin
10601 if Is_Tagged_Type (Rectype) then
10602 Sbit := UI_From_Int (System_Address_Size);
10603 else
10604 Sbit := Uint_0;
10605 end if;
10606
10607 if Nkind (Decl) = N_Full_Type_Declaration
10608 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10609 then
10610 Check_Component_List
10611 (Component_List (Type_Definition (Decl)),
10612 Sbit,
10613 Discriminant_Specifications (Decl));
67278d60 10614 end if;
d9f6a4ee 10615 end;
10616 end Record_Hole_Check;
67278d60 10617 end if;
10618
d9f6a4ee 10619 -- For records that have component clauses for all components, and whose
10620 -- size is less than or equal to 32, we need to know the size in the
10621 -- front end to activate possible packed array processing where the
10622 -- component type is a record.
67278d60 10623
d9f6a4ee 10624 -- At this stage Hbit + 1 represents the first unused bit from all the
10625 -- component clauses processed, so if the component clauses are
10626 -- complete, then this is the length of the record.
67278d60 10627
d9f6a4ee 10628 -- For records longer than System.Storage_Unit, and for those where not
10629 -- all components have component clauses, the back end determines the
10630 -- length (it may for example be appropriate to round up the size
10631 -- to some convenient boundary, based on alignment considerations, etc).
67278d60 10632
d9f6a4ee 10633 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
67278d60 10634
d9f6a4ee 10635 -- Nothing to do if at least one component has no component clause
67278d60 10636
d9f6a4ee 10637 Comp := First_Component_Or_Discriminant (Rectype);
10638 while Present (Comp) loop
10639 exit when No (Component_Clause (Comp));
10640 Next_Component_Or_Discriminant (Comp);
10641 end loop;
67278d60 10642
d9f6a4ee 10643 -- If we fall out of loop, all components have component clauses
10644 -- and so we can set the size to the maximum value.
67278d60 10645
d9f6a4ee 10646 if No (Comp) then
10647 Set_RM_Size (Rectype, Hbit + 1);
10648 end if;
10649 end if;
10650 end Check_Record_Representation_Clause;
67278d60 10651
d9f6a4ee 10652 ----------------
10653 -- Check_Size --
10654 ----------------
67278d60 10655
d9f6a4ee 10656 procedure Check_Size
10657 (N : Node_Id;
10658 T : Entity_Id;
10659 Siz : Uint;
10660 Biased : out Boolean)
10661 is
f74a102b 10662 procedure Size_Too_Small_Error (Min_Siz : Uint);
10663 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10664 -- minimum size.
10665
10666 --------------------------
10667 -- Size_Too_Small_Error --
10668 --------------------------
10669
10670 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10671 begin
10672 -- This error is suppressed in ASIS mode to allow for different ASIS
f9906591 10673 -- back ends or ASIS-based tools to query the illegal clause.
f74a102b 10674
10675 if not ASIS_Mode then
10676 Error_Msg_Uint_1 := Min_Siz;
6d22398d 10677 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
f74a102b 10678 end if;
10679 end Size_Too_Small_Error;
10680
10681 -- Local variables
10682
d9f6a4ee 10683 UT : constant Entity_Id := Underlying_Type (T);
10684 M : Uint;
67278d60 10685
f74a102b 10686 -- Start of processing for Check_Size
10687
d9f6a4ee 10688 begin
10689 Biased := False;
67278d60 10690
f74a102b 10691 -- Reject patently improper size values
67278d60 10692
d9f6a4ee 10693 if Is_Elementary_Type (T)
10694 and then Siz > UI_From_Int (Int'Last)
10695 then
10696 Error_Msg_N ("Size value too large for elementary type", N);
67278d60 10697
d9f6a4ee 10698 if Nkind (Original_Node (N)) = N_Op_Expon then
10699 Error_Msg_N
10700 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10701 end if;
10702 end if;
67278d60 10703
d9f6a4ee 10704 -- Dismiss generic types
67278d60 10705
d9f6a4ee 10706 if Is_Generic_Type (T)
10707 or else
10708 Is_Generic_Type (UT)
10709 or else
10710 Is_Generic_Type (Root_Type (UT))
10711 then
10712 return;
67278d60 10713
d9f6a4ee 10714 -- Guard against previous errors
67278d60 10715
d9f6a4ee 10716 elsif No (UT) or else UT = Any_Type then
10717 Check_Error_Detected;
10718 return;
67278d60 10719
d9f6a4ee 10720 -- Check case of bit packed array
67278d60 10721
d9f6a4ee 10722 elsif Is_Array_Type (UT)
10723 and then Known_Static_Component_Size (UT)
10724 and then Is_Bit_Packed_Array (UT)
10725 then
10726 declare
10727 Asiz : Uint;
10728 Indx : Node_Id;
10729 Ityp : Entity_Id;
67278d60 10730
d9f6a4ee 10731 begin
10732 Asiz := Component_Size (UT);
10733 Indx := First_Index (UT);
10734 loop
10735 Ityp := Etype (Indx);
67278d60 10736
d9f6a4ee 10737 -- If non-static bound, then we are not in the business of
10738 -- trying to check the length, and indeed an error will be
10739 -- issued elsewhere, since sizes of non-static array types
10740 -- cannot be set implicitly or explicitly.
67278d60 10741
cda40848 10742 if not Is_OK_Static_Subtype (Ityp) then
d9f6a4ee 10743 return;
10744 end if;
67278d60 10745
d9f6a4ee 10746 -- Otherwise accumulate next dimension
67278d60 10747
d9f6a4ee 10748 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10749 Expr_Value (Type_Low_Bound (Ityp)) +
10750 Uint_1);
67278d60 10751
d9f6a4ee 10752 Next_Index (Indx);
10753 exit when No (Indx);
10754 end loop;
67278d60 10755
d9f6a4ee 10756 if Asiz <= Siz then
10757 return;
67278d60 10758
d9f6a4ee 10759 else
f74a102b 10760 Size_Too_Small_Error (Asiz);
d9f6a4ee 10761 Set_Esize (T, Asiz);
10762 Set_RM_Size (T, Asiz);
10763 end if;
10764 end;
67278d60 10765
d9f6a4ee 10766 -- All other composite types are ignored
67278d60 10767
d9f6a4ee 10768 elsif Is_Composite_Type (UT) then
10769 return;
47495553 10770
d9f6a4ee 10771 -- For fixed-point types, don't check minimum if type is not frozen,
10772 -- since we don't know all the characteristics of the type that can
10773 -- affect the size (e.g. a specified small) till freeze time.
47495553 10774
f74a102b 10775 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
d9f6a4ee 10776 null;
47495553 10777
d9f6a4ee 10778 -- Cases for which a minimum check is required
47495553 10779
d9f6a4ee 10780 else
10781 -- Ignore if specified size is correct for the type
47495553 10782
d9f6a4ee 10783 if Known_Esize (UT) and then Siz = Esize (UT) then
10784 return;
10785 end if;
47495553 10786
d9f6a4ee 10787 -- Otherwise get minimum size
47495553 10788
d9f6a4ee 10789 M := UI_From_Int (Minimum_Size (UT));
47495553 10790
d9f6a4ee 10791 if Siz < M then
47495553 10792
d9f6a4ee 10793 -- Size is less than minimum size, but one possibility remains
10794 -- that we can manage with the new size if we bias the type.
47495553 10795
d9f6a4ee 10796 M := UI_From_Int (Minimum_Size (UT, Biased => True));
47495553 10797
d9f6a4ee 10798 if Siz < M then
f74a102b 10799 Size_Too_Small_Error (M);
10800 Set_Esize (T, M);
d9f6a4ee 10801 Set_RM_Size (T, M);
10802 else
10803 Biased := True;
10804 end if;
10805 end if;
10806 end if;
10807 end Check_Size;
47495553 10808
d9f6a4ee 10809 --------------------------
10810 -- Freeze_Entity_Checks --
10811 --------------------------
47495553 10812
d9f6a4ee 10813 procedure Freeze_Entity_Checks (N : Node_Id) is
8cf481c9 10814 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10815 -- Inspect the primitive operations of type Typ and hide all pairs of
3118058b 10816 -- implicitly declared non-overridden non-fully conformant homographs
10817 -- (Ada RM 8.3 12.3/2).
8cf481c9 10818
10819 -------------------------------------
10820 -- Hide_Non_Overridden_Subprograms --
10821 -------------------------------------
10822
10823 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10824 procedure Hide_Matching_Homographs
10825 (Subp_Id : Entity_Id;
10826 Start_Elmt : Elmt_Id);
10827 -- Inspect a list of primitive operations starting with Start_Elmt
3118058b 10828 -- and find matching implicitly declared non-overridden non-fully
10829 -- conformant homographs of Subp_Id. If found, all matches along
10830 -- with Subp_Id are hidden from all visibility.
8cf481c9 10831
10832 function Is_Non_Overridden_Or_Null_Procedure
10833 (Subp_Id : Entity_Id) return Boolean;
10834 -- Determine whether subprogram Subp_Id is implicitly declared non-
10835 -- overridden subprogram or an implicitly declared null procedure.
10836
10837 ------------------------------
10838 -- Hide_Matching_Homographs --
10839 ------------------------------
10840
10841 procedure Hide_Matching_Homographs
10842 (Subp_Id : Entity_Id;
10843 Start_Elmt : Elmt_Id)
10844 is
10845 Prim : Entity_Id;
10846 Prim_Elmt : Elmt_Id;
10847
10848 begin
10849 Prim_Elmt := Start_Elmt;
10850 while Present (Prim_Elmt) loop
10851 Prim := Node (Prim_Elmt);
10852
10853 -- The current primitive is implicitly declared non-overridden
3118058b 10854 -- non-fully conformant homograph of Subp_Id. Both subprograms
10855 -- must be hidden from visibility.
8cf481c9 10856
10857 if Chars (Prim) = Chars (Subp_Id)
8cf481c9 10858 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
3118058b 10859 and then not Fully_Conformant (Prim, Subp_Id)
8cf481c9 10860 then
8c7ee4ac 10861 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10862 Set_Is_Immediately_Visible (Prim, False);
10863 Set_Is_Potentially_Use_Visible (Prim, False);
8cf481c9 10864
8c7ee4ac 10865 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10866 Set_Is_Immediately_Visible (Subp_Id, False);
10867 Set_Is_Potentially_Use_Visible (Subp_Id, False);
8cf481c9 10868 end if;
10869
10870 Next_Elmt (Prim_Elmt);
10871 end loop;
10872 end Hide_Matching_Homographs;
10873
10874 -----------------------------------------
10875 -- Is_Non_Overridden_Or_Null_Procedure --
10876 -----------------------------------------
10877
10878 function Is_Non_Overridden_Or_Null_Procedure
10879 (Subp_Id : Entity_Id) return Boolean
10880 is
10881 Alias_Id : Entity_Id;
10882
10883 begin
10884 -- The subprogram is inherited (implicitly declared), it does not
10885 -- override and does not cover a primitive of an interface.
10886
10887 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10888 and then Present (Alias (Subp_Id))
10889 and then No (Interface_Alias (Subp_Id))
10890 and then No (Overridden_Operation (Subp_Id))
10891 then
10892 Alias_Id := Alias (Subp_Id);
10893
10894 if Requires_Overriding (Alias_Id) then
10895 return True;
10896
10897 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10898 and then Null_Present (Parent (Alias_Id))
10899 then
10900 return True;
10901 end if;
10902 end if;
10903
10904 return False;
10905 end Is_Non_Overridden_Or_Null_Procedure;
10906
10907 -- Local variables
10908
10909 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10910 Prim : Entity_Id;
10911 Prim_Elmt : Elmt_Id;
10912
10913 -- Start of processing for Hide_Non_Overridden_Subprograms
10914
10915 begin
3118058b 10916 -- Inspect the list of primitives looking for non-overridden
10917 -- subprograms.
8cf481c9 10918
10919 if Present (Prim_Ops) then
10920 Prim_Elmt := First_Elmt (Prim_Ops);
10921 while Present (Prim_Elmt) loop
10922 Prim := Node (Prim_Elmt);
10923 Next_Elmt (Prim_Elmt);
10924
10925 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10926 Hide_Matching_Homographs
10927 (Subp_Id => Prim,
10928 Start_Elmt => Prim_Elmt);
10929 end if;
10930 end loop;
10931 end if;
10932 end Hide_Non_Overridden_Subprograms;
10933
97c23bbe 10934 -- Local variables
8cf481c9 10935
d9f6a4ee 10936 E : constant Entity_Id := Entity (N);
47495553 10937
0b10029c 10938 Nongeneric_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10939 -- True in nongeneric case. Some of the processing here is skipped
d9f6a4ee 10940 -- for the generic case since it is not needed. Basically in the
10941 -- generic case, we only need to do stuff that might generate error
10942 -- messages or warnings.
8cf481c9 10943
10944 -- Start of processing for Freeze_Entity_Checks
10945
d9f6a4ee 10946 begin
10947 -- Remember that we are processing a freezing entity. Required to
10948 -- ensure correct decoration of internal entities associated with
10949 -- interfaces (see New_Overloaded_Entity).
47495553 10950
d9f6a4ee 10951 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
47495553 10952
d9f6a4ee 10953 -- For tagged types covering interfaces add internal entities that link
10954 -- the primitives of the interfaces with the primitives that cover them.
10955 -- Note: These entities were originally generated only when generating
10956 -- code because their main purpose was to provide support to initialize
10957 -- the secondary dispatch tables. They are now generated also when
10958 -- compiling with no code generation to provide ASIS the relationship
10959 -- between interface primitives and tagged type primitives. They are
10960 -- also used to locate primitives covering interfaces when processing
10961 -- generics (see Derive_Subprograms).
47495553 10962
d9f6a4ee 10963 -- This is not needed in the generic case
47495553 10964
d9f6a4ee 10965 if Ada_Version >= Ada_2005
0b10029c 10966 and then Nongeneric_Case
d9f6a4ee 10967 and then Ekind (E) = E_Record_Type
10968 and then Is_Tagged_Type (E)
10969 and then not Is_Interface (E)
10970 and then Has_Interfaces (E)
10971 then
10972 -- This would be a good common place to call the routine that checks
10973 -- overriding of interface primitives (and thus factorize calls to
10974 -- Check_Abstract_Overriding located at different contexts in the
10975 -- compiler). However, this is not possible because it causes
10976 -- spurious errors in case of late overriding.
47495553 10977
d9f6a4ee 10978 Add_Internal_Interface_Entities (E);
10979 end if;
47495553 10980
8cf481c9 10981 -- After all forms of overriding have been resolved, a tagged type may
10982 -- be left with a set of implicitly declared and possibly erroneous
10983 -- abstract subprograms, null procedures and subprograms that require
0c4e0575 10984 -- overriding. If this set contains fully conformant homographs, then
10985 -- one is chosen arbitrarily (already done during resolution), otherwise
10986 -- all remaining non-fully conformant homographs are hidden from
10987 -- visibility (Ada RM 8.3 12.3/2).
8cf481c9 10988
10989 if Is_Tagged_Type (E) then
10990 Hide_Non_Overridden_Subprograms (E);
10991 end if;
10992
d9f6a4ee 10993 -- Check CPP types
47495553 10994
d9f6a4ee 10995 if Ekind (E) = E_Record_Type
10996 and then Is_CPP_Class (E)
10997 and then Is_Tagged_Type (E)
10998 and then Tagged_Type_Expansion
d9f6a4ee 10999 then
11000 if CPP_Num_Prims (E) = 0 then
47495553 11001
d9f6a4ee 11002 -- If the CPP type has user defined components then it must import
11003 -- primitives from C++. This is required because if the C++ class
11004 -- has no primitives then the C++ compiler does not added the _tag
11005 -- component to the type.
47495553 11006
d9f6a4ee 11007 if First_Entity (E) /= Last_Entity (E) then
11008 Error_Msg_N
11009 ("'C'P'P type must import at least one primitive from C++??",
11010 E);
11011 end if;
11012 end if;
47495553 11013
d9f6a4ee 11014 -- Check that all its primitives are abstract or imported from C++.
11015 -- Check also availability of the C++ constructor.
47495553 11016
d9f6a4ee 11017 declare
11018 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
11019 Elmt : Elmt_Id;
11020 Error_Reported : Boolean := False;
11021 Prim : Node_Id;
47495553 11022
d9f6a4ee 11023 begin
11024 Elmt := First_Elmt (Primitive_Operations (E));
11025 while Present (Elmt) loop
11026 Prim := Node (Elmt);
47495553 11027
d9f6a4ee 11028 if Comes_From_Source (Prim) then
11029 if Is_Abstract_Subprogram (Prim) then
11030 null;
47495553 11031
d9f6a4ee 11032 elsif not Is_Imported (Prim)
11033 or else Convention (Prim) /= Convention_CPP
11034 then
11035 Error_Msg_N
11036 ("primitives of 'C'P'P types must be imported from C++ "
11037 & "or abstract??", Prim);
47495553 11038
d9f6a4ee 11039 elsif not Has_Constructors
11040 and then not Error_Reported
11041 then
11042 Error_Msg_Name_1 := Chars (E);
11043 Error_Msg_N
11044 ("??'C'P'P constructor required for type %", Prim);
11045 Error_Reported := True;
11046 end if;
11047 end if;
47495553 11048
d9f6a4ee 11049 Next_Elmt (Elmt);
11050 end loop;
11051 end;
11052 end if;
47495553 11053
d9f6a4ee 11054 -- Check Ada derivation of CPP type
47495553 11055
30ab103b 11056 if Expander_Active -- why? losing errors in -gnatc mode???
11057 and then Present (Etype (E)) -- defend against errors
d9f6a4ee 11058 and then Tagged_Type_Expansion
11059 and then Ekind (E) = E_Record_Type
11060 and then Etype (E) /= E
11061 and then Is_CPP_Class (Etype (E))
11062 and then CPP_Num_Prims (Etype (E)) > 0
11063 and then not Is_CPP_Class (E)
11064 and then not Has_CPP_Constructors (Etype (E))
11065 then
11066 -- If the parent has C++ primitives but it has no constructor then
11067 -- check that all the primitives are overridden in this derivation;
11068 -- otherwise the constructor of the parent is needed to build the
11069 -- dispatch table.
47495553 11070
d9f6a4ee 11071 declare
11072 Elmt : Elmt_Id;
11073 Prim : Node_Id;
47495553 11074
11075 begin
d9f6a4ee 11076 Elmt := First_Elmt (Primitive_Operations (E));
11077 while Present (Elmt) loop
11078 Prim := Node (Elmt);
47495553 11079
d9f6a4ee 11080 if not Is_Abstract_Subprogram (Prim)
11081 and then No (Interface_Alias (Prim))
11082 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
47495553 11083 then
d9f6a4ee 11084 Error_Msg_Name_1 := Chars (Etype (E));
11085 Error_Msg_N
11086 ("'C'P'P constructor required for parent type %", E);
11087 exit;
47495553 11088 end if;
d9f6a4ee 11089
11090 Next_Elmt (Elmt);
11091 end loop;
11092 end;
47495553 11093 end if;
11094
d9f6a4ee 11095 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
67278d60 11096
97c23bbe 11097 -- If we have a type with predicates, build predicate function. This is
11098 -- not needed in the generic case, nor within TSS subprograms and other
11099 -- predefined primitives.
67278d60 11100
97c23bbe 11101 if Is_Type (E)
0b10029c 11102 and then Nongeneric_Case
ea822fd4 11103 and then not Within_Internal_Subprogram
97c23bbe 11104 and then Has_Predicates (E)
ea822fd4 11105 then
d9f6a4ee 11106 Build_Predicate_Functions (E, N);
11107 end if;
67278d60 11108
d9f6a4ee 11109 -- If type has delayed aspects, this is where we do the preanalysis at
11110 -- the freeze point, as part of the consistent visibility check. Note
11111 -- that this must be done after calling Build_Predicate_Functions or
11112 -- Build_Invariant_Procedure since these subprograms fix occurrences of
11113 -- the subtype name in the saved expression so that they will not cause
11114 -- trouble in the preanalysis.
67278d60 11115
61989dbb 11116 -- This is also not needed in the generic case
d9f6a4ee 11117
0b10029c 11118 if Nongeneric_Case
61989dbb 11119 and then Has_Delayed_Aspects (E)
d9f6a4ee 11120 and then Scope (E) = Current_Scope
11121 then
11122 -- Retrieve the visibility to the discriminants in order to properly
11123 -- analyze the aspects.
11124
11125 Push_Scope_And_Install_Discriminants (E);
11126
11127 declare
11128 Ritem : Node_Id;
11129
11130 begin
11131 -- Look for aspect specification entries for this entity
67278d60 11132
d9f6a4ee 11133 Ritem := First_Rep_Item (E);
11134 while Present (Ritem) loop
11135 if Nkind (Ritem) = N_Aspect_Specification
11136 and then Entity (Ritem) = E
11137 and then Is_Delayed_Aspect (Ritem)
11138 then
11139 Check_Aspect_At_Freeze_Point (Ritem);
11140 end if;
67278d60 11141
d9f6a4ee 11142 Next_Rep_Item (Ritem);
11143 end loop;
11144 end;
67278d60 11145
d9f6a4ee 11146 Uninstall_Discriminants_And_Pop_Scope (E);
67278d60 11147 end if;
67278d60 11148
d9f6a4ee 11149 -- For a record type, deal with variant parts. This has to be delayed
d0988351 11150 -- to this point, because of the issue of statically predicated
d9f6a4ee 11151 -- subtypes, which we have to ensure are frozen before checking
11152 -- choices, since we need to have the static choice list set.
d6f39728 11153
d9f6a4ee 11154 if Is_Record_Type (E) then
11155 Check_Variant_Part : declare
11156 D : constant Node_Id := Declaration_Node (E);
11157 T : Node_Id;
11158 C : Node_Id;
11159 VP : Node_Id;
d6f39728 11160
d9f6a4ee 11161 Others_Present : Boolean;
11162 pragma Warnings (Off, Others_Present);
11163 -- Indicates others present, not used in this case
d6f39728 11164
d9f6a4ee 11165 procedure Non_Static_Choice_Error (Choice : Node_Id);
11166 -- Error routine invoked by the generic instantiation below when
11167 -- the variant part has a non static choice.
f117057b 11168
d9f6a4ee 11169 procedure Process_Declarations (Variant : Node_Id);
11170 -- Processes declarations associated with a variant. We analyzed
11171 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11172 -- but we still need the recursive call to Check_Choices for any
11173 -- nested variant to get its choices properly processed. This is
11174 -- also where we expand out the choices if expansion is active.
1f526845 11175
d9f6a4ee 11176 package Variant_Choices_Processing is new
11177 Generic_Check_Choices
11178 (Process_Empty_Choice => No_OP,
11179 Process_Non_Static_Choice => Non_Static_Choice_Error,
11180 Process_Associated_Node => Process_Declarations);
11181 use Variant_Choices_Processing;
f117057b 11182
d9f6a4ee 11183 -----------------------------
11184 -- Non_Static_Choice_Error --
11185 -----------------------------
d6f39728 11186
d9f6a4ee 11187 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11188 begin
11189 Flag_Non_Static_Expr
11190 ("choice given in variant part is not static!", Choice);
11191 end Non_Static_Choice_Error;
d6f39728 11192
d9f6a4ee 11193 --------------------------
11194 -- Process_Declarations --
11195 --------------------------
dba36b60 11196
d9f6a4ee 11197 procedure Process_Declarations (Variant : Node_Id) is
11198 CL : constant Node_Id := Component_List (Variant);
11199 VP : Node_Id;
dba36b60 11200
d9f6a4ee 11201 begin
11202 -- Check for static predicate present in this variant
ea61a7ea 11203
d9f6a4ee 11204 if Has_SP_Choice (Variant) then
ea61a7ea 11205
d9f6a4ee 11206 -- Here we expand. You might expect to find this call in
11207 -- Expand_N_Variant_Part, but that is called when we first
11208 -- see the variant part, and we cannot do this expansion
11209 -- earlier than the freeze point, since for statically
11210 -- predicated subtypes, the predicate is not known till
11211 -- the freeze point.
ea61a7ea 11212
d9f6a4ee 11213 -- Furthermore, we do this expansion even if the expander
11214 -- is not active, because other semantic processing, e.g.
11215 -- for aggregates, requires the expanded list of choices.
ea61a7ea 11216
d9f6a4ee 11217 -- If the expander is not active, then we can't just clobber
11218 -- the list since it would invalidate the ASIS -gnatct tree.
11219 -- So we have to rewrite the variant part with a Rewrite
11220 -- call that replaces it with a copy and clobber the copy.
11221
11222 if not Expander_Active then
11223 declare
11224 NewV : constant Node_Id := New_Copy (Variant);
11225 begin
11226 Set_Discrete_Choices
11227 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11228 Rewrite (Variant, NewV);
11229 end;
11230 end if;
11231
11232 Expand_Static_Predicates_In_Choices (Variant);
ea61a7ea 11233 end if;
11234
d9f6a4ee 11235 -- We don't need to worry about the declarations in the variant
11236 -- (since they were analyzed by Analyze_Choices when we first
11237 -- encountered the variant), but we do need to take care of
11238 -- expansion of any nested variants.
ea61a7ea 11239
d9f6a4ee 11240 if not Null_Present (CL) then
11241 VP := Variant_Part (CL);
ea61a7ea 11242
d9f6a4ee 11243 if Present (VP) then
11244 Check_Choices
11245 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11246 end if;
11247 end if;
11248 end Process_Declarations;
ea61a7ea 11249
d9f6a4ee 11250 -- Start of processing for Check_Variant_Part
b9e61b2a 11251
d9f6a4ee 11252 begin
11253 -- Find component list
ea61a7ea 11254
d9f6a4ee 11255 C := Empty;
ea61a7ea 11256
d9f6a4ee 11257 if Nkind (D) = N_Full_Type_Declaration then
11258 T := Type_Definition (D);
ea61a7ea 11259
d9f6a4ee 11260 if Nkind (T) = N_Record_Definition then
11261 C := Component_List (T);
d6f39728 11262
d9f6a4ee 11263 elsif Nkind (T) = N_Derived_Type_Definition
11264 and then Present (Record_Extension_Part (T))
11265 then
11266 C := Component_List (Record_Extension_Part (T));
11267 end if;
11268 end if;
d6f39728 11269
d9f6a4ee 11270 -- Case of variant part present
d6f39728 11271
d9f6a4ee 11272 if Present (C) and then Present (Variant_Part (C)) then
11273 VP := Variant_Part (C);
ea61a7ea 11274
d9f6a4ee 11275 -- Check choices
ea61a7ea 11276
d9f6a4ee 11277 Check_Choices
11278 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
ea61a7ea 11279
d9f6a4ee 11280 -- If the last variant does not contain the Others choice,
11281 -- replace it with an N_Others_Choice node since Gigi always
11282 -- wants an Others. Note that we do not bother to call Analyze
11283 -- on the modified variant part, since its only effect would be
11284 -- to compute the Others_Discrete_Choices node laboriously, and
11285 -- of course we already know the list of choices corresponding
39a0c1d3 11286 -- to the others choice (it's the list we're replacing).
d6f39728 11287
d9f6a4ee 11288 -- We only want to do this if the expander is active, since
39a0c1d3 11289 -- we do not want to clobber the ASIS tree.
d6f39728 11290
d9f6a4ee 11291 if Expander_Active then
11292 declare
11293 Last_Var : constant Node_Id :=
11294 Last_Non_Pragma (Variants (VP));
d6f39728 11295
d9f6a4ee 11296 Others_Node : Node_Id;
d6f39728 11297
d9f6a4ee 11298 begin
11299 if Nkind (First (Discrete_Choices (Last_Var))) /=
11300 N_Others_Choice
11301 then
11302 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11303 Set_Others_Discrete_Choices
11304 (Others_Node, Discrete_Choices (Last_Var));
11305 Set_Discrete_Choices
11306 (Last_Var, New_List (Others_Node));
11307 end if;
11308 end;
11309 end if;
d6f39728 11310 end if;
d9f6a4ee 11311 end Check_Variant_Part;
d6f39728 11312 end if;
d9f6a4ee 11313 end Freeze_Entity_Checks;
d6f39728 11314
11315 -------------------------
11316 -- Get_Alignment_Value --
11317 -------------------------
11318
11319 function Get_Alignment_Value (Expr : Node_Id) return Uint is
f5d97bf5 11320 Align : constant Uint := Static_Integer (Expr);
f74a102b 11321
f5d97bf5 11322 begin
11323 if Align = No_Uint then
11324 return No_Uint;
11325
11326 elsif Align <= 0 then
f74a102b 11327
f74a102b 11328 -- This error is suppressed in ASIS mode to allow for different ASIS
f9906591 11329 -- back ends or ASIS-based tools to query the illegal clause.
f74a102b 11330
11331 if not ASIS_Mode then
11332 Error_Msg_N ("alignment value must be positive", Expr);
11333 end if;
f74a102b 11334
d6f39728 11335 return No_Uint;
11336
11337 else
11338 for J in Int range 0 .. 64 loop
11339 declare
11340 M : constant Uint := Uint_2 ** J;
11341
11342 begin
11343 exit when M = Align;
11344
11345 if M > Align then
f5d97bf5 11346
11347 -- This error is suppressed in ASIS mode to allow for
f9906591 11348 -- different ASIS back ends or ASIS-based tools to query the
f5d97bf5 11349 -- illegal clause.
11350
11351 if not ASIS_Mode then
11352 Error_Msg_N ("alignment value must be power of 2", Expr);
11353 end if;
11354
d6f39728 11355 return No_Uint;
11356 end if;
11357 end;
11358 end loop;
11359
11360 return Align;
11361 end if;
11362 end Get_Alignment_Value;
11363
99a2d5bd 11364 -------------------------------------
11365 -- Inherit_Aspects_At_Freeze_Point --
11366 -------------------------------------
11367
11368 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11369 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11370 (Rep_Item : Node_Id) return Boolean;
11371 -- This routine checks if Rep_Item is either a pragma or an aspect
11372 -- specification node whose correponding pragma (if any) is present in
11373 -- the Rep Item chain of the entity it has been specified to.
11374
11375 --------------------------------------------------
11376 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11377 --------------------------------------------------
11378
11379 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11380 (Rep_Item : Node_Id) return Boolean
11381 is
11382 begin
ec6f6da5 11383 return
11384 Nkind (Rep_Item) = N_Pragma
11385 or else Present_In_Rep_Item
11386 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
99a2d5bd 11387 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11388
29a9d4be 11389 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11390
99a2d5bd 11391 begin
11392 -- A representation item is either subtype-specific (Size and Alignment
11393 -- clauses) or type-related (all others). Subtype-specific aspects may
29a9d4be 11394 -- differ for different subtypes of the same type (RM 13.1.8).
99a2d5bd 11395
11396 -- A derived type inherits each type-related representation aspect of
11397 -- its parent type that was directly specified before the declaration of
29a9d4be 11398 -- the derived type (RM 13.1.15).
99a2d5bd 11399
11400 -- A derived subtype inherits each subtype-specific representation
11401 -- aspect of its parent subtype that was directly specified before the
29a9d4be 11402 -- declaration of the derived type (RM 13.1.15).
99a2d5bd 11403
11404 -- The general processing involves inheriting a representation aspect
11405 -- from a parent type whenever the first rep item (aspect specification,
11406 -- attribute definition clause, pragma) corresponding to the given
11407 -- representation aspect in the rep item chain of Typ, if any, isn't
11408 -- directly specified to Typ but to one of its parents.
11409
11410 -- ??? Note that, for now, just a limited number of representation
29a9d4be 11411 -- aspects have been inherited here so far. Many of them are
11412 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11413 -- a non- exhaustive list of aspects that likely also need to
11414 -- be moved to this routine: Alignment, Component_Alignment,
11415 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
99a2d5bd 11416 -- Preelaborable_Initialization, RM_Size and Small.
11417
8b6e9bf2 11418 -- In addition, Convention must be propagated from base type to subtype,
11419 -- because the subtype may have been declared on an incomplete view.
11420
99a2d5bd 11421 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11422 return;
11423 end if;
11424
11425 -- Ada_05/Ada_2005
11426
11427 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11428 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11429 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11430 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11431 then
11432 Set_Is_Ada_2005_Only (Typ);
11433 end if;
11434
11435 -- Ada_12/Ada_2012
11436
11437 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11438 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11439 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11440 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11441 then
11442 Set_Is_Ada_2012_Only (Typ);
11443 end if;
11444
11445 -- Atomic/Shared
11446
11447 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11448 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11449 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11450 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11451 then
11452 Set_Is_Atomic (Typ);
99a2d5bd 11453 Set_Is_Volatile (Typ);
4bf2acc9 11454 Set_Treat_As_Volatile (Typ);
99a2d5bd 11455 end if;
11456
8b6e9bf2 11457 -- Convention
11458
7ac4254e 11459 if Is_Record_Type (Typ)
11460 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11461 then
8b6e9bf2 11462 Set_Convention (Typ, Convention (Base_Type (Typ)));
11463 end if;
11464
29a9d4be 11465 -- Default_Component_Value
99a2d5bd 11466
81c2bc19 11467 -- Verify that there is no rep_item declared for the type, and there
11468 -- is one coming from an ancestor.
11469
99a2d5bd 11470 if Is_Array_Type (Typ)
f3d70f08 11471 and then Is_Base_Type (Typ)
81c2bc19 11472 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
99a2d5bd 11473 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11474 then
11475 Set_Default_Aspect_Component_Value (Typ,
11476 Default_Aspect_Component_Value
11477 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11478 end if;
11479
29a9d4be 11480 -- Default_Value
99a2d5bd 11481
11482 if Is_Scalar_Type (Typ)
f3d70f08 11483 and then Is_Base_Type (Typ)
81c2bc19 11484 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
99a2d5bd 11485 and then Has_Rep_Item (Typ, Name_Default_Value)
11486 then
81c2bc19 11487 Set_Has_Default_Aspect (Typ);
99a2d5bd 11488 Set_Default_Aspect_Value (Typ,
11489 Default_Aspect_Value
11490 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11491 end if;
11492
11493 -- Discard_Names
11494
11495 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11496 and then Has_Rep_Item (Typ, Name_Discard_Names)
11497 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11498 (Get_Rep_Item (Typ, Name_Discard_Names))
11499 then
11500 Set_Discard_Names (Typ);
11501 end if;
11502
99a2d5bd 11503 -- Volatile
11504
11505 if not Has_Rep_Item (Typ, Name_Volatile, False)
11506 and then Has_Rep_Item (Typ, Name_Volatile)
11507 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11508 (Get_Rep_Item (Typ, Name_Volatile))
11509 then
99a2d5bd 11510 Set_Is_Volatile (Typ);
4bf2acc9 11511 Set_Treat_As_Volatile (Typ);
99a2d5bd 11512 end if;
11513
2fe893b9 11514 -- Volatile_Full_Access
11515
11516 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11517 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11518 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11519 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11520 then
4bf2acc9 11521 Set_Is_Volatile_Full_Access (Typ);
2fe893b9 11522 Set_Is_Volatile (Typ);
4bf2acc9 11523 Set_Treat_As_Volatile (Typ);
2fe893b9 11524 end if;
11525
99a2d5bd 11526 -- Inheritance for derived types only
11527
11528 if Is_Derived_Type (Typ) then
11529 declare
11530 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11531 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11532
11533 begin
11534 -- Atomic_Components
11535
11536 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11537 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11538 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11539 (Get_Rep_Item (Typ, Name_Atomic_Components))
11540 then
11541 Set_Has_Atomic_Components (Imp_Bas_Typ);
11542 end if;
11543
11544 -- Volatile_Components
11545
11546 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11547 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11548 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11549 (Get_Rep_Item (Typ, Name_Volatile_Components))
11550 then
11551 Set_Has_Volatile_Components (Imp_Bas_Typ);
11552 end if;
11553
e81df51c 11554 -- Finalize_Storage_Only
99a2d5bd 11555
11556 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11557 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11558 then
11559 Set_Finalize_Storage_Only (Bas_Typ);
11560 end if;
11561
11562 -- Universal_Aliasing
11563
11564 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11565 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11566 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11567 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11568 then
11569 Set_Universal_Aliasing (Imp_Bas_Typ);
11570 end if;
11571
e81df51c 11572 -- Bit_Order
99a2d5bd 11573
11574 if Is_Record_Type (Typ) then
99a2d5bd 11575 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11576 and then Has_Rep_Item (Typ, Name_Bit_Order)
11577 then
11578 Set_Reverse_Bit_Order (Bas_Typ,
11579 Reverse_Bit_Order (Entity (Name
11580 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11581 end if;
e81df51c 11582 end if;
11583
e9218716 11584 -- Scalar_Storage_Order
11585
11586 -- Note: the aspect is specified on a first subtype, but recorded
11587 -- in a flag of the base type!
e81df51c 11588
11589 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
29b91bc7 11590 and then Typ = Bas_Typ
e81df51c 11591 then
e81df51c 11592 -- For a type extension, always inherit from parent; otherwise
11593 -- inherit if no default applies. Note: we do not check for
11594 -- an explicit rep item on the parent type when inheriting,
11595 -- because the parent SSO may itself have been set by default.
99a2d5bd 11596
e9218716 11597 if not Has_Rep_Item (First_Subtype (Typ),
11598 Name_Scalar_Storage_Order, False)
e81df51c 11599 and then (Is_Tagged_Type (Bas_Typ)
29b91bc7 11600 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11601 or else
11602 SSO_Set_High_By_Default (Bas_Typ)))
99a2d5bd 11603 then
11604 Set_Reverse_Storage_Order (Bas_Typ,
423b89fd 11605 Reverse_Storage_Order
11606 (Implementation_Base_Type (Etype (Bas_Typ))));
b64082f2 11607
11608 -- Clear default SSO indications, since the inherited aspect
11609 -- which was set explicitly overrides the default.
11610
11611 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11612 Set_SSO_Set_High_By_Default (Bas_Typ, False);
99a2d5bd 11613 end if;
11614 end if;
11615 end;
11616 end if;
11617 end Inherit_Aspects_At_Freeze_Point;
11618
d6f39728 11619 ----------------
11620 -- Initialize --
11621 ----------------
11622
11623 procedure Initialize is
11624 begin
7717ea00 11625 Address_Clause_Checks.Init;
76a6b7c7 11626 Compile_Time_Warnings_Errors.Init;
d6f39728 11627 Unchecked_Conversions.Init;
dba38d2f 11628
3a7fe2f3 11629 -- ??? Might be needed in the future for some non GCC back-ends
11630 -- if AAMP_On_Target then
11631 -- Independence_Checks.Init;
11632 -- end if;
d6f39728 11633 end Initialize;
11634
2625eb01 11635 ---------------------------
11636 -- Install_Discriminants --
11637 ---------------------------
11638
11639 procedure Install_Discriminants (E : Entity_Id) is
11640 Disc : Entity_Id;
11641 Prev : Entity_Id;
11642 begin
11643 Disc := First_Discriminant (E);
11644 while Present (Disc) loop
11645 Prev := Current_Entity (Disc);
11646 Set_Current_Entity (Disc);
11647 Set_Is_Immediately_Visible (Disc);
11648 Set_Homonym (Disc, Prev);
11649 Next_Discriminant (Disc);
11650 end loop;
11651 end Install_Discriminants;
11652
d6f39728 11653 -------------------------
11654 -- Is_Operational_Item --
11655 -------------------------
11656
11657 function Is_Operational_Item (N : Node_Id) return Boolean is
11658 begin
11659 if Nkind (N) /= N_Attribute_Definition_Clause then
11660 return False;
b9e61b2a 11661
d6f39728 11662 else
11663 declare
b9e61b2a 11664 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
d6f39728 11665 begin
078a74b8 11666
55ab5265 11667 -- List of operational items is given in AARM 13.1(8.mm/1).
078a74b8 11668 -- It is clearly incomplete, as it does not include iterator
11669 -- aspects, among others.
11670
11671 return Id = Attribute_Constant_Indexing
11672 or else Id = Attribute_Default_Iterator
11673 or else Id = Attribute_Implicit_Dereference
11674 or else Id = Attribute_Input
11675 or else Id = Attribute_Iterator_Element
11676 or else Id = Attribute_Iterable
d6f39728 11677 or else Id = Attribute_Output
11678 or else Id = Attribute_Read
078a74b8 11679 or else Id = Attribute_Variable_Indexing
f15731c4 11680 or else Id = Attribute_Write
11681 or else Id = Attribute_External_Tag;
d6f39728 11682 end;
11683 end if;
11684 end Is_Operational_Item;
11685
3b23aaa0 11686 -------------------------
11687 -- Is_Predicate_Static --
11688 -------------------------
11689
94d896aa 11690 -- Note: the basic legality of the expression has already been checked, so
11691 -- we don't need to worry about cases or ranges on strings for example.
11692
3b23aaa0 11693 function Is_Predicate_Static
11694 (Expr : Node_Id;
11695 Nam : Name_Id) return Boolean
11696 is
11697 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
973c2fba 11698 -- Given a list of case expression alternatives, returns True if all
11699 -- the alternatives are static (have all static choices, and a static
11700 -- expression).
3b23aaa0 11701
11702 function All_Static_Choices (L : List_Id) return Boolean;
a360a0f7 11703 -- Returns true if all elements of the list are OK static choices
3b23aaa0 11704 -- as defined below for Is_Static_Choice. Used for case expression
973c2fba 11705 -- alternatives and for the right operand of a membership test. An
11706 -- others_choice is static if the corresponding expression is static.
7c0c95b8 11707 -- The staticness of the bounds is checked separately.
3b23aaa0 11708
11709 function Is_Static_Choice (N : Node_Id) return Boolean;
11710 -- Returns True if N represents a static choice (static subtype, or
a360a0f7 11711 -- static subtype indication, or static expression, or static range).
3b23aaa0 11712 --
11713 -- Note that this is a bit more inclusive than we actually need
11714 -- (in particular membership tests do not allow the use of subtype
a360a0f7 11715 -- indications). But that doesn't matter, we have already checked
3b23aaa0 11716 -- that the construct is legal to get this far.
11717
11718 function Is_Type_Ref (N : Node_Id) return Boolean;
11719 pragma Inline (Is_Type_Ref);
973c2fba 11720 -- Returns True if N is a reference to the type for the predicate in the
11721 -- expression (i.e. if it is an identifier whose Chars field matches the
11722 -- Nam given in the call). N must not be parenthesized, if the type name
11723 -- appears in parens, this routine will return False.
10f62e3a 11724 --
ea90be0f 11725 -- The routine also returns True for function calls generated during the
11726 -- expansion of comparison operators on strings, which are intended to
11727 -- be legal in static predicates, and are converted into calls to array
11728 -- comparison routines in the body of the corresponding predicate
11729 -- function.
11730
3b23aaa0 11731 ----------------------------------
11732 -- All_Static_Case_Alternatives --
11733 ----------------------------------
11734
11735 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11736 N : Node_Id;
11737
11738 begin
11739 N := First (L);
11740 while Present (N) loop
11741 if not (All_Static_Choices (Discrete_Choices (N))
11742 and then Is_OK_Static_Expression (Expression (N)))
11743 then
11744 return False;
11745 end if;
11746
11747 Next (N);
11748 end loop;
11749
11750 return True;
11751 end All_Static_Case_Alternatives;
11752
11753 ------------------------
11754 -- All_Static_Choices --
11755 ------------------------
11756
11757 function All_Static_Choices (L : List_Id) return Boolean is
11758 N : Node_Id;
11759
11760 begin
11761 N := First (L);
11762 while Present (N) loop
11763 if not Is_Static_Choice (N) then
11764 return False;
11765 end if;
11766
11767 Next (N);
11768 end loop;
11769
11770 return True;
11771 end All_Static_Choices;
11772
11773 ----------------------
11774 -- Is_Static_Choice --
11775 ----------------------
11776
11777 function Is_Static_Choice (N : Node_Id) return Boolean is
11778 begin
7c0c95b8 11779 return Nkind (N) = N_Others_Choice
11780 or else Is_OK_Static_Expression (N)
3b23aaa0 11781 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11782 and then Is_OK_Static_Subtype (Entity (N)))
11783 or else (Nkind (N) = N_Subtype_Indication
11784 and then Is_OK_Static_Subtype (Entity (N)))
11785 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11786 end Is_Static_Choice;
11787
11788 -----------------
11789 -- Is_Type_Ref --
11790 -----------------
11791
11792 function Is_Type_Ref (N : Node_Id) return Boolean is
11793 begin
ea90be0f 11794 return (Nkind (N) = N_Identifier
11795 and then Chars (N) = Nam
11796 and then Paren_Count (N) = 0)
11797 or else Nkind (N) = N_Function_Call;
3b23aaa0 11798 end Is_Type_Ref;
11799
11800 -- Start of processing for Is_Predicate_Static
11801
11802 begin
3b23aaa0 11803 -- Predicate_Static means one of the following holds. Numbers are the
11804 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11805
11806 -- 16: A static expression
11807
11808 if Is_OK_Static_Expression (Expr) then
11809 return True;
11810
11811 -- 17: A membership test whose simple_expression is the current
11812 -- instance, and whose membership_choice_list meets the requirements
11813 -- for a static membership test.
11814
11815 elsif Nkind (Expr) in N_Membership_Test
11816 and then ((Present (Right_Opnd (Expr))
11817 and then Is_Static_Choice (Right_Opnd (Expr)))
11818 or else
11819 (Present (Alternatives (Expr))
11820 and then All_Static_Choices (Alternatives (Expr))))
11821 then
11822 return True;
11823
11824 -- 18. A case_expression whose selecting_expression is the current
11825 -- instance, and whose dependent expressions are static expressions.
11826
11827 elsif Nkind (Expr) = N_Case_Expression
11828 and then Is_Type_Ref (Expression (Expr))
11829 and then All_Static_Case_Alternatives (Alternatives (Expr))
11830 then
11831 return True;
11832
11833 -- 19. A call to a predefined equality or ordering operator, where one
11834 -- operand is the current instance, and the other is a static
11835 -- expression.
11836
94d896aa 11837 -- Note: the RM is clearly wrong here in not excluding string types.
11838 -- Without this exclusion, we would allow expressions like X > "ABC"
11839 -- to be considered as predicate-static, which is clearly not intended,
11840 -- since the idea is for predicate-static to be a subset of normal
11841 -- static expressions (and "DEF" > "ABC" is not a static expression).
11842
11843 -- However, we do allow internally generated (not from source) equality
11844 -- and inequality operations to be valid on strings (this helps deal
11845 -- with cases where we transform A in "ABC" to A = "ABC).
11846
ea90be0f 11847 -- In fact, it appears that the intent of the ARG is to extend static
11848 -- predicates to strings, and that the extension should probably apply
11849 -- to static expressions themselves. The code below accepts comparison
11850 -- operators that apply to static strings.
11851
3b23aaa0 11852 elsif Nkind (Expr) in N_Op_Compare
11853 and then ((Is_Type_Ref (Left_Opnd (Expr))
11854 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11855 or else
11856 (Is_Type_Ref (Right_Opnd (Expr))
11857 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11858 then
11859 return True;
11860
11861 -- 20. A call to a predefined boolean logical operator, where each
11862 -- operand is predicate-static.
11863
11864 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11865 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11866 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11867 or else
11868 (Nkind (Expr) = N_Op_Not
11869 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11870 then
11871 return True;
11872
11873 -- 21. A short-circuit control form where both operands are
11874 -- predicate-static.
11875
11876 elsif Nkind (Expr) in N_Short_Circuit
11877 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11878 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11879 then
11880 return True;
11881
11882 -- 22. A parenthesized predicate-static expression. This does not
11883 -- require any special test, since we just ignore paren levels in
11884 -- all the cases above.
11885
11886 -- One more test that is an implementation artifact caused by the fact
499918a7 11887 -- that we are analyzing not the original expression, but the generated
3b23aaa0 11888 -- expression in the body of the predicate function. This can include
a360a0f7 11889 -- references to inherited predicates, so that the expression we are
3b23aaa0 11890 -- processing looks like:
11891
75491446 11892 -- xxPredicate (typ (Inns)) and then expression
3b23aaa0 11893
11894 -- Where the call is to a Predicate function for an inherited predicate.
60a4a5af 11895 -- We simply ignore such a call, which could be to either a dynamic or
11896 -- a static predicate. Note that if the parent predicate is dynamic then
11897 -- eventually this type will be marked as dynamic, but you are allowed
11898 -- to specify a static predicate for a subtype which is inheriting a
11899 -- dynamic predicate, so the static predicate validation here ignores
11900 -- the inherited predicate even if it is dynamic.
7db33803 11901 -- In all cases, a static predicate can only apply to a scalar type.
3b23aaa0 11902
11903 elsif Nkind (Expr) = N_Function_Call
11904 and then Is_Predicate_Function (Entity (Name (Expr)))
7db33803 11905 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
3b23aaa0 11906 then
11907 return True;
11908
11909 -- That's an exhaustive list of tests, all other cases are not
a360a0f7 11910 -- predicate-static, so we return False.
3b23aaa0 11911
11912 else
11913 return False;
11914 end if;
11915 end Is_Predicate_Static;
11916
2ff55065 11917 ---------------------
11918 -- Kill_Rep_Clause --
11919 ---------------------
11920
11921 procedure Kill_Rep_Clause (N : Node_Id) is
11922 begin
11923 pragma Assert (Ignore_Rep_Clauses);
360f426f 11924
11925 -- Note: we use Replace rather than Rewrite, because we don't want
11926 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11927 -- rep clause that is being replaced.
11928
4949ddd5 11929 Replace (N, Make_Null_Statement (Sloc (N)));
360f426f 11930
11931 -- The null statement must be marked as not coming from source. This is
37c6552c 11932 -- so that ASIS ignores it, and also the back end does not expect bogus
360f426f 11933 -- "from source" null statements in weird places (e.g. in declarative
11934 -- regions where such null statements are not allowed).
11935
11936 Set_Comes_From_Source (N, False);
2ff55065 11937 end Kill_Rep_Clause;
11938
d6f39728 11939 ------------------
11940 -- Minimum_Size --
11941 ------------------
11942
11943 function Minimum_Size
11944 (T : Entity_Id;
d5b349fa 11945 Biased : Boolean := False) return Nat
d6f39728 11946 is
11947 Lo : Uint := No_Uint;
11948 Hi : Uint := No_Uint;
11949 LoR : Ureal := No_Ureal;
11950 HiR : Ureal := No_Ureal;
11951 LoSet : Boolean := False;
11952 HiSet : Boolean := False;
11953 B : Uint;
11954 S : Nat;
11955 Ancest : Entity_Id;
f15731c4 11956 R_Typ : constant Entity_Id := Root_Type (T);
d6f39728 11957
11958 begin
11959 -- If bad type, return 0
11960
11961 if T = Any_Type then
11962 return 0;
11963
11964 -- For generic types, just return zero. There cannot be any legitimate
11965 -- need to know such a size, but this routine may be called with a
11966 -- generic type as part of normal processing.
11967
f02a9a9a 11968 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
d6f39728 11969 return 0;
11970
74c7ae52 11971 -- Access types (cannot have size smaller than System.Address)
d6f39728 11972
11973 elsif Is_Access_Type (T) then
74c7ae52 11974 return System_Address_Size;
d6f39728 11975
11976 -- Floating-point types
11977
11978 elsif Is_Floating_Point_Type (T) then
f15731c4 11979 return UI_To_Int (Esize (R_Typ));
d6f39728 11980
11981 -- Discrete types
11982
11983 elsif Is_Discrete_Type (T) then
11984
fdd294d1 11985 -- The following loop is looking for the nearest compile time known
11986 -- bounds following the ancestor subtype chain. The idea is to find
11987 -- the most restrictive known bounds information.
d6f39728 11988
11989 Ancest := T;
11990 loop
11991 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11992 return 0;
11993 end if;
11994
11995 if not LoSet then
11996 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11997 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11998 LoSet := True;
11999 exit when HiSet;
12000 end if;
12001 end if;
12002
12003 if not HiSet then
12004 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
12005 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
12006 HiSet := True;
12007 exit when LoSet;
12008 end if;
12009 end if;
12010
12011 Ancest := Ancestor_Subtype (Ancest);
12012
12013 if No (Ancest) then
12014 Ancest := Base_Type (T);
12015
12016 if Is_Generic_Type (Ancest) then
12017 return 0;
12018 end if;
12019 end if;
12020 end loop;
12021
12022 -- Fixed-point types. We can't simply use Expr_Value to get the
fdd294d1 12023 -- Corresponding_Integer_Value values of the bounds, since these do not
12024 -- get set till the type is frozen, and this routine can be called
12025 -- before the type is frozen. Similarly the test for bounds being static
12026 -- needs to include the case where we have unanalyzed real literals for
12027 -- the same reason.
d6f39728 12028
12029 elsif Is_Fixed_Point_Type (T) then
12030
fdd294d1 12031 -- The following loop is looking for the nearest compile time known
12032 -- bounds following the ancestor subtype chain. The idea is to find
12033 -- the most restrictive known bounds information.
d6f39728 12034
12035 Ancest := T;
12036 loop
12037 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
12038 return 0;
12039 end if;
12040
3062c401 12041 -- Note: In the following two tests for LoSet and HiSet, it may
12042 -- seem redundant to test for N_Real_Literal here since normally
12043 -- one would assume that the test for the value being known at
12044 -- compile time includes this case. However, there is a glitch.
12045 -- If the real literal comes from folding a non-static expression,
12046 -- then we don't consider any non- static expression to be known
12047 -- at compile time if we are in configurable run time mode (needed
12048 -- in some cases to give a clearer definition of what is and what
12049 -- is not accepted). So the test is indeed needed. Without it, we
12050 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
12051
d6f39728 12052 if not LoSet then
12053 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
12054 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
12055 then
12056 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
12057 LoSet := True;
12058 exit when HiSet;
12059 end if;
12060 end if;
12061
12062 if not HiSet then
12063 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
12064 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
12065 then
12066 HiR := Expr_Value_R (Type_High_Bound (Ancest));
12067 HiSet := True;
12068 exit when LoSet;
12069 end if;
12070 end if;
12071
12072 Ancest := Ancestor_Subtype (Ancest);
12073
12074 if No (Ancest) then
12075 Ancest := Base_Type (T);
12076
12077 if Is_Generic_Type (Ancest) then
12078 return 0;
12079 end if;
12080 end if;
12081 end loop;
12082
12083 Lo := UR_To_Uint (LoR / Small_Value (T));
12084 Hi := UR_To_Uint (HiR / Small_Value (T));
12085
12086 -- No other types allowed
12087
12088 else
12089 raise Program_Error;
12090 end if;
12091
2866d595 12092 -- Fall through with Hi and Lo set. Deal with biased case
d6f39728 12093
cc46ff4b 12094 if (Biased
12095 and then not Is_Fixed_Point_Type (T)
12096 and then not (Is_Enumeration_Type (T)
12097 and then Has_Non_Standard_Rep (T)))
d6f39728 12098 or else Has_Biased_Representation (T)
12099 then
12100 Hi := Hi - Lo;
12101 Lo := Uint_0;
12102 end if;
12103
005366f7 12104 -- Null range case, size is always zero. We only do this in the discrete
12105 -- type case, since that's the odd case that came up. Probably we should
12106 -- also do this in the fixed-point case, but doing so causes peculiar
12107 -- gigi failures, and it is not worth worrying about this incredibly
12108 -- marginal case (explicit null-range fixed-point type declarations)???
12109
12110 if Lo > Hi and then Is_Discrete_Type (T) then
12111 S := 0;
12112
d6f39728 12113 -- Signed case. Note that we consider types like range 1 .. -1 to be
fdd294d1 12114 -- signed for the purpose of computing the size, since the bounds have
1a34e48c 12115 -- to be accommodated in the base type.
d6f39728 12116
005366f7 12117 elsif Lo < 0 or else Hi < 0 then
d6f39728 12118 S := 1;
12119 B := Uint_1;
12120
da253936 12121 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12122 -- Note that we accommodate the case where the bounds cross. This
d6f39728 12123 -- can happen either because of the way the bounds are declared
12124 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12125
12126 while Lo < -B
12127 or else Hi < -B
12128 or else Lo >= B
12129 or else Hi >= B
12130 loop
12131 B := Uint_2 ** S;
12132 S := S + 1;
12133 end loop;
12134
12135 -- Unsigned case
12136
12137 else
12138 -- If both bounds are positive, make sure that both are represen-
12139 -- table in the case where the bounds are crossed. This can happen
12140 -- either because of the way the bounds are declared, or because of
12141 -- the algorithm in Freeze_Fixed_Point_Type.
12142
12143 if Lo > Hi then
12144 Hi := Lo;
12145 end if;
12146
da253936 12147 -- S = size, (can accommodate 0 .. (2**size - 1))
d6f39728 12148
12149 S := 0;
12150 while Hi >= Uint_2 ** S loop
12151 S := S + 1;
12152 end loop;
12153 end if;
12154
12155 return S;
12156 end Minimum_Size;
12157
44e4341e 12158 ---------------------------
12159 -- New_Stream_Subprogram --
12160 ---------------------------
d6f39728 12161
44e4341e 12162 procedure New_Stream_Subprogram
12163 (N : Node_Id;
12164 Ent : Entity_Id;
12165 Subp : Entity_Id;
12166 Nam : TSS_Name_Type)
d6f39728 12167 is
12168 Loc : constant Source_Ptr := Sloc (N);
9dfe12ae 12169 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
f15731c4 12170 Subp_Id : Entity_Id;
d6f39728 12171 Subp_Decl : Node_Id;
12172 F : Entity_Id;
12173 Etyp : Entity_Id;
12174
44e4341e 12175 Defer_Declaration : constant Boolean :=
12176 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12177 -- For a tagged type, there is a declaration for each stream attribute
12178 -- at the freeze point, and we must generate only a completion of this
12179 -- declaration. We do the same for private types, because the full view
12180 -- might be tagged. Otherwise we generate a declaration at the point of
449c4f58 12181 -- the attribute definition clause. If the attribute definition comes
12182 -- from an aspect specification the declaration is part of the freeze
12183 -- actions of the type.
44e4341e 12184
f15731c4 12185 function Build_Spec return Node_Id;
12186 -- Used for declaration and renaming declaration, so that this is
12187 -- treated as a renaming_as_body.
12188
12189 ----------------
12190 -- Build_Spec --
12191 ----------------
12192
d5b349fa 12193 function Build_Spec return Node_Id is
44e4341e 12194 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12195 Formals : List_Id;
12196 Spec : Node_Id;
83c6c069 12197 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
44e4341e 12198
f15731c4 12199 begin
9dfe12ae 12200 Subp_Id := Make_Defining_Identifier (Loc, Sname);
f15731c4 12201
44e4341e 12202 -- S : access Root_Stream_Type'Class
12203
12204 Formals := New_List (
12205 Make_Parameter_Specification (Loc,
12206 Defining_Identifier =>
12207 Make_Defining_Identifier (Loc, Name_S),
12208 Parameter_Type =>
12209 Make_Access_Definition (Loc,
12210 Subtype_Mark =>
83c6c069 12211 New_Occurrence_Of (
44e4341e 12212 Designated_Type (Etype (F)), Loc))));
12213
12214 if Nam = TSS_Stream_Input then
4bba0a8d 12215 Spec :=
12216 Make_Function_Specification (Loc,
12217 Defining_Unit_Name => Subp_Id,
12218 Parameter_Specifications => Formals,
12219 Result_Definition => T_Ref);
44e4341e 12220 else
12221 -- V : [out] T
f15731c4 12222
44e4341e 12223 Append_To (Formals,
12224 Make_Parameter_Specification (Loc,
12225 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12226 Out_Present => Out_P,
12227 Parameter_Type => T_Ref));
f15731c4 12228
d3ef794c 12229 Spec :=
12230 Make_Procedure_Specification (Loc,
12231 Defining_Unit_Name => Subp_Id,
12232 Parameter_Specifications => Formals);
44e4341e 12233 end if;
f15731c4 12234
44e4341e 12235 return Spec;
12236 end Build_Spec;
d6f39728 12237
44e4341e 12238 -- Start of processing for New_Stream_Subprogram
d6f39728 12239
44e4341e 12240 begin
12241 F := First_Formal (Subp);
12242
12243 if Ekind (Subp) = E_Procedure then
12244 Etyp := Etype (Next_Formal (F));
d6f39728 12245 else
44e4341e 12246 Etyp := Etype (Subp);
d6f39728 12247 end if;
f15731c4 12248
44e4341e 12249 -- Prepare subprogram declaration and insert it as an action on the
12250 -- clause node. The visibility for this entity is used to test for
12251 -- visibility of the attribute definition clause (in the sense of
12252 -- 8.3(23) as amended by AI-195).
9dfe12ae 12253
44e4341e 12254 if not Defer_Declaration then
f15731c4 12255 Subp_Decl :=
12256 Make_Subprogram_Declaration (Loc,
12257 Specification => Build_Spec);
44e4341e 12258
12259 -- For a tagged type, there is always a visible declaration for each
15ebb600 12260 -- stream TSS (it is a predefined primitive operation), and the
44e4341e 12261 -- completion of this declaration occurs at the freeze point, which is
12262 -- not always visible at places where the attribute definition clause is
12263 -- visible. So, we create a dummy entity here for the purpose of
12264 -- tracking the visibility of the attribute definition clause itself.
12265
12266 else
12267 Subp_Id :=
55868293 12268 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
44e4341e 12269 Subp_Decl :=
12270 Make_Object_Declaration (Loc,
12271 Defining_Identifier => Subp_Id,
12272 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
f15731c4 12273 end if;
12274
449c4f58 12275 if not Defer_Declaration
12276 and then From_Aspect_Specification (N)
12277 and then Has_Delayed_Freeze (Ent)
12278 then
12279 Append_Freeze_Action (Ent, Subp_Decl);
12280
12281 else
12282 Insert_Action (N, Subp_Decl);
12283 Set_Entity (N, Subp_Id);
12284 end if;
44e4341e 12285
d6f39728 12286 Subp_Decl :=
12287 Make_Subprogram_Renaming_Declaration (Loc,
f15731c4 12288 Specification => Build_Spec,
8acb75b4 12289 Name => New_Occurrence_Of (Subp, Loc));
d6f39728 12290
44e4341e 12291 if Defer_Declaration then
d6f39728 12292 Set_TSS (Base_Type (Ent), Subp_Id);
449c4f58 12293
d6f39728 12294 else
449c4f58 12295 if From_Aspect_Specification (N) then
12296 Append_Freeze_Action (Ent, Subp_Decl);
449c4f58 12297 else
12298 Insert_Action (N, Subp_Decl);
12299 end if;
12300
d6f39728 12301 Copy_TSS (Subp_Id, Base_Type (Ent));
12302 end if;
44e4341e 12303 end New_Stream_Subprogram;
d6f39728 12304
2625eb01 12305 ------------------------------------------
12306 -- Push_Scope_And_Install_Discriminants --
12307 ------------------------------------------
12308
12309 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12310 begin
12311 if Has_Discriminants (E) then
12312 Push_Scope (E);
12313
97c23bbe 12314 -- Make the discriminants visible for type declarations and protected
2625eb01 12315 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12316
12317 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12318 Install_Discriminants (E);
12319 end if;
12320 end if;
12321 end Push_Scope_And_Install_Discriminants;
12322
d10a1b95 12323 -----------------------------------
12324 -- Register_Address_Clause_Check --
12325 -----------------------------------
12326
12327 procedure Register_Address_Clause_Check
12328 (N : Node_Id;
12329 X : Entity_Id;
12330 A : Uint;
12331 Y : Entity_Id;
12332 Off : Boolean)
12333 is
12334 ACS : constant Boolean := Scope_Suppress.Suppress (Alignment_Check);
12335 begin
12336 Address_Clause_Checks.Append ((N, X, A, Y, Off, ACS));
12337 end Register_Address_Clause_Check;
12338
d6f39728 12339 ------------------------
12340 -- Rep_Item_Too_Early --
12341 ------------------------
12342
80d4fec4 12343 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
d6f39728 12344 begin
44e4341e 12345 -- Cannot apply non-operational rep items to generic types
d6f39728 12346
f15731c4 12347 if Is_Operational_Item (N) then
12348 return False;
12349
12350 elsif Is_Type (T)
d6f39728 12351 and then Is_Generic_Type (Root_Type (T))
e17c5076 12352 and then (Nkind (N) /= N_Pragma
12353 or else Get_Pragma_Id (N) /= Pragma_Convention)
d6f39728 12354 then
503f7fd3 12355 Error_Msg_N ("representation item not allowed for generic type", N);
d6f39728 12356 return True;
12357 end if;
12358
fdd294d1 12359 -- Otherwise check for incomplete type
d6f39728 12360
12361 if Is_Incomplete_Or_Private_Type (T)
12362 and then No (Underlying_Type (T))
d64221a7 12363 and then
12364 (Nkind (N) /= N_Pragma
60014bc9 12365 or else Get_Pragma_Id (N) /= Pragma_Import)
d6f39728 12366 then
12367 Error_Msg_N
12368 ("representation item must be after full type declaration", N);
12369 return True;
12370
1a34e48c 12371 -- If the type has incomplete components, a representation clause is
d6f39728 12372 -- illegal but stream attributes and Convention pragmas are correct.
12373
12374 elsif Has_Private_Component (T) then
f15731c4 12375 if Nkind (N) = N_Pragma then
d6f39728 12376 return False;
b9e61b2a 12377
d6f39728 12378 else
12379 Error_Msg_N
12380 ("representation item must appear after type is fully defined",
12381 N);
12382 return True;
12383 end if;
12384 else
12385 return False;
12386 end if;
12387 end Rep_Item_Too_Early;
12388
12389 -----------------------
12390 -- Rep_Item_Too_Late --
12391 -----------------------
12392
12393 function Rep_Item_Too_Late
12394 (T : Entity_Id;
12395 N : Node_Id;
d5b349fa 12396 FOnly : Boolean := False) return Boolean
d6f39728 12397 is
12398 S : Entity_Id;
12399 Parent_Type : Entity_Id;
12400
4d0944e9 12401 procedure No_Type_Rep_Item;
12402 -- Output message indicating that no type-related aspects can be
12403 -- specified due to some property of the parent type.
12404
d6f39728 12405 procedure Too_Late;
4d0944e9 12406 -- Output message for an aspect being specified too late
12407
12408 -- Note that neither of the above errors is considered a serious one,
12409 -- since the effect is simply that we ignore the representation clause
12410 -- in these cases.
04d38ee4 12411 -- Is this really true? In any case if we make this change we must
12412 -- document the requirement in the spec of Rep_Item_Too_Late that
12413 -- if True is returned, then the rep item must be completely ignored???
4d0944e9 12414
12415 ----------------------
12416 -- No_Type_Rep_Item --
12417 ----------------------
12418
12419 procedure No_Type_Rep_Item is
12420 begin
12421 Error_Msg_N ("|type-related representation item not permitted!", N);
12422 end No_Type_Rep_Item;
d53a018a 12423
12424 --------------
12425 -- Too_Late --
12426 --------------
d6f39728 12427
12428 procedure Too_Late is
12429 begin
ce4da1ed 12430 -- Other compilers seem more relaxed about rep items appearing too
12431 -- late. Since analysis tools typically don't care about rep items
12432 -- anyway, no reason to be too strict about this.
12433
a9cd517c 12434 if not Relaxed_RM_Semantics then
12435 Error_Msg_N ("|representation item appears too late!", N);
12436 end if;
d6f39728 12437 end Too_Late;
12438
12439 -- Start of processing for Rep_Item_Too_Late
12440
12441 begin
a3248fc4 12442 -- First make sure entity is not frozen (RM 13.1(9))
d6f39728 12443
12444 if Is_Frozen (T)
a3248fc4 12445
12446 -- Exclude imported types, which may be frozen if they appear in a
12447 -- representation clause for a local type.
12448
4aa270d8 12449 and then not From_Limited_With (T)
a3248fc4 12450
a9cd517c 12451 -- Exclude generated entities (not coming from source). The common
a3248fc4 12452 -- case is when we generate a renaming which prematurely freezes the
12453 -- renamed internal entity, but we still want to be able to set copies
12454 -- of attribute values such as Size/Alignment.
12455
12456 and then Comes_From_Source (T)
d6f39728 12457 then
58e133a6 12458 -- A self-referential aspect is illegal if it forces freezing the
12459 -- entity before the corresponding pragma has been analyzed.
12460
12461 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12462 and then From_Aspect_Specification (N)
12463 then
12464 Error_Msg_NE
ea90be0f 12465 ("aspect specification causes premature freezing of&", N, T);
58e133a6 12466 Set_Has_Delayed_Freeze (T, False);
12467 return True;
12468 end if;
12469
d6f39728 12470 Too_Late;
12471 S := First_Subtype (T);
12472
12473 if Present (Freeze_Node (S)) then
04d38ee4 12474 if not Relaxed_RM_Semantics then
12475 Error_Msg_NE
12476 ("??no more representation items for }", Freeze_Node (S), S);
12477 end if;
d6f39728 12478 end if;
12479
12480 return True;
12481
d1a2e31b 12482 -- Check for case of untagged derived type whose parent either has
4d0944e9 12483 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12484 -- this case we do not output a Too_Late message, since there is no
12485 -- earlier point where the rep item could be placed to make it legal.
d6f39728 12486
12487 elsif Is_Type (T)
12488 and then not FOnly
12489 and then Is_Derived_Type (T)
12490 and then not Is_Tagged_Type (T)
12491 then
12492 Parent_Type := Etype (Base_Type (T));
12493
12494 if Has_Primitive_Operations (Parent_Type) then
4d0944e9 12495 No_Type_Rep_Item;
04d38ee4 12496
12497 if not Relaxed_RM_Semantics then
12498 Error_Msg_NE
12499 ("\parent type & has primitive operations!", N, Parent_Type);
12500 end if;
12501
d6f39728 12502 return True;
12503
12504 elsif Is_By_Reference_Type (Parent_Type) then
4d0944e9 12505 No_Type_Rep_Item;
04d38ee4 12506
12507 if not Relaxed_RM_Semantics then
12508 Error_Msg_NE
12509 ("\parent type & is a by reference type!", N, Parent_Type);
12510 end if;
12511
d6f39728 12512 return True;
12513 end if;
12514 end if;
12515
04d38ee4 12516 -- No error, but one more warning to consider. The RM (surprisingly)
12517 -- allows this pattern:
12518
12519 -- type S is ...
12520 -- primitive operations for S
12521 -- type R is new S;
12522 -- rep clause for S
12523
12524 -- Meaning that calls on the primitive operations of S for values of
12525 -- type R may require possibly expensive implicit conversion operations.
12526 -- This is not an error, but is worth a warning.
12527
12528 if not Relaxed_RM_Semantics and then Is_Type (T) then
12529 declare
12530 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12531
12532 begin
12533 if Present (DTL)
12534 and then Has_Primitive_Operations (Base_Type (T))
12535
12536 -- For now, do not generate this warning for the case of aspect
12537 -- specification using Ada 2012 syntax, since we get wrong
12538 -- messages we do not understand. The whole business of derived
12539 -- types and rep items seems a bit confused when aspects are
12540 -- used, since the aspects are not evaluated till freeze time.
12541
12542 and then not From_Aspect_Specification (N)
12543 then
12544 Error_Msg_Sloc := Sloc (DTL);
12545 Error_Msg_N
12546 ("representation item for& appears after derived type "
12547 & "declaration#??", N);
12548 Error_Msg_NE
12549 ("\may result in implicit conversions for primitive "
12550 & "operations of&??", N, T);
12551 Error_Msg_NE
12552 ("\to change representations when called with arguments "
12553 & "of type&??", N, DTL);
12554 end if;
12555 end;
12556 end if;
12557
3062c401 12558 -- No error, link item into head of chain of rep items for the entity,
12559 -- but avoid chaining if we have an overloadable entity, and the pragma
12560 -- is one that can apply to multiple overloaded entities.
12561
b9e61b2a 12562 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
fdd294d1 12563 declare
ddccc924 12564 Pname : constant Name_Id := Pragma_Name (N);
fdd294d1 12565 begin
18393965 12566 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12567 Name_External, Name_Interface)
fdd294d1 12568 then
12569 return False;
12570 end if;
12571 end;
3062c401 12572 end if;
12573
fdd294d1 12574 Record_Rep_Item (T, N);
d6f39728 12575 return False;
12576 end Rep_Item_Too_Late;
12577
2072eaa9 12578 -------------------------------------
12579 -- Replace_Type_References_Generic --
12580 -------------------------------------
12581
37c6552c 12582 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12583 TName : constant Name_Id := Chars (T);
2072eaa9 12584
97c23bbe 12585 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
2072eaa9 12586 -- Processes a single node in the traversal procedure below, checking
12587 -- if node N should be replaced, and if so, doing the replacement.
12588
d0931270 12589 function Visible_Component (Comp : Name_Id) return Entity_Id;
12590 -- Given an identifier in the expression, check whether there is a
12591 -- discriminant or component of the type that is directy visible, and
12592 -- rewrite it as the corresponding selected component of the formal of
12593 -- the subprogram. The entity is located by a sequential search, which
12594 -- seems acceptable given the typical size of component lists and check
12595 -- expressions. Possible optimization ???
12596
97c23bbe 12597 ----------------------
12598 -- Replace_Type_Ref --
12599 ----------------------
2072eaa9 12600
97c23bbe 12601 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
d0931270 12602 Loc : constant Source_Ptr := Sloc (N);
2072eaa9 12603
d0931270 12604 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
77fd9c7a 12605 -- Add the proper prefix to a reference to a component of the type
12606 -- when it is not already a selected component.
d0931270 12607
12608 ----------------
12609 -- Add_Prefix --
12610 ----------------
2072eaa9 12611
d0931270 12612 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12613 begin
12614 Rewrite (Ref,
12615 Make_Selected_Component (Loc,
77fd9c7a 12616 Prefix => New_Occurrence_Of (T, Loc),
d0931270 12617 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12618 Replace_Type_Reference (Prefix (Ref));
12619 end Add_Prefix;
12620
77fd9c7a 12621 -- Local variables
12622
12623 Comp : Entity_Id;
12624 Pref : Node_Id;
12625 Scop : Entity_Id;
12626
d0931270 12627 -- Start of processing for Replace_Type_Ref
12628
12629 begin
2072eaa9 12630 if Nkind (N) = N_Identifier then
12631
97c23bbe 12632 -- If not the type name, check whether it is a reference to some
12633 -- other type, which must be frozen before the predicate function
12634 -- is analyzed, i.e. before the freeze node of the type to which
12635 -- the predicate applies.
2072eaa9 12636
12637 if Chars (N) /= TName then
37c6552c 12638 if Present (Current_Entity (N))
46532462 12639 and then Is_Type (Current_Entity (N))
37c6552c 12640 then
12641 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12642 end if;
12643
d0931270 12644 -- The components of the type are directly visible and can
12645 -- be referenced without a prefix.
12646
12647 if Nkind (Parent (N)) = N_Selected_Component then
12648 null;
12649
12650 -- In expression C (I), C may be a directly visible function
12651 -- or a visible component that has an array type. Disambiguate
12652 -- by examining the component type.
12653
12654 elsif Nkind (Parent (N)) = N_Indexed_Component
12655 and then N = Prefix (Parent (N))
12656 then
77fd9c7a 12657 Comp := Visible_Component (Chars (N));
d0931270 12658
77fd9c7a 12659 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
12660 Add_Prefix (N, Comp);
d0931270 12661 end if;
12662
12663 else
77fd9c7a 12664 Comp := Visible_Component (Chars (N));
d0931270 12665
77fd9c7a 12666 if Present (Comp) then
12667 Add_Prefix (N, Comp);
d0931270 12668 end if;
12669 end if;
12670
2072eaa9 12671 return Skip;
12672
12673 -- Otherwise do the replacement and we are done with this node
12674
12675 else
12676 Replace_Type_Reference (N);
12677 return Skip;
12678 end if;
12679
97c23bbe 12680 -- Case of selected component (which is what a qualification looks
12681 -- like in the unanalyzed tree, which is what we have.
2072eaa9 12682
12683 elsif Nkind (N) = N_Selected_Component then
12684
97c23bbe 12685 -- If selector name is not our type, keeping going (we might still
12686 -- have an occurrence of the type in the prefix).
2072eaa9 12687
12688 if Nkind (Selector_Name (N)) /= N_Identifier
12689 or else Chars (Selector_Name (N)) /= TName
12690 then
12691 return OK;
12692
12693 -- Selector name is our type, check qualification
12694
12695 else
12696 -- Loop through scopes and prefixes, doing comparison
12697
77fd9c7a 12698 Scop := Current_Scope;
12699 Pref := Prefix (N);
2072eaa9 12700 loop
12701 -- Continue if no more scopes or scope with no name
12702
77fd9c7a 12703 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
2072eaa9 12704 return OK;
12705 end if;
12706
97c23bbe 12707 -- Do replace if prefix is an identifier matching the scope
12708 -- that we are currently looking at.
2072eaa9 12709
77fd9c7a 12710 if Nkind (Pref) = N_Identifier
12711 and then Chars (Pref) = Chars (Scop)
2072eaa9 12712 then
12713 Replace_Type_Reference (N);
12714 return Skip;
12715 end if;
12716
97c23bbe 12717 -- Go check scope above us if prefix is itself of the form
12718 -- of a selected component, whose selector matches the scope
12719 -- we are currently looking at.
2072eaa9 12720
77fd9c7a 12721 if Nkind (Pref) = N_Selected_Component
12722 and then Nkind (Selector_Name (Pref)) = N_Identifier
12723 and then Chars (Selector_Name (Pref)) = Chars (Scop)
2072eaa9 12724 then
77fd9c7a 12725 Scop := Scope (Scop);
12726 Pref := Prefix (Pref);
2072eaa9 12727
12728 -- For anything else, we don't have a match, so keep on
12729 -- going, there are still some weird cases where we may
12730 -- still have a replacement within the prefix.
12731
12732 else
12733 return OK;
12734 end if;
12735 end loop;
12736 end if;
12737
ec6f6da5 12738 -- Continue for any other node kind
2072eaa9 12739
12740 else
12741 return OK;
12742 end if;
97c23bbe 12743 end Replace_Type_Ref;
12744
77fd9c7a 12745 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
12746
d0931270 12747 -----------------------
12748 -- Visible_Component --
12749 -----------------------
12750
12751 function Visible_Component (Comp : Name_Id) return Entity_Id is
12752 E : Entity_Id;
77fd9c7a 12753
d0931270 12754 begin
1a5b3a27 12755 -- Types with nameable components are records and discriminated
12756 -- private types.
12757
12758 if Ekind (T) = E_Record_Type
12759 or else (Is_Private_Type (T) and then Has_Discriminants (T))
12760 then
d0931270 12761 E := First_Entity (T);
12762 while Present (E) loop
77fd9c7a 12763 if Comes_From_Source (E) and then Chars (E) = Comp then
d0931270 12764 return E;
12765 end if;
12766
12767 Next_Entity (E);
12768 end loop;
d0931270 12769 end if;
1a5b3a27 12770
b58a7126 12771 -- Nothing by that name, or the type has no components
1a5b3a27 12772
12773 return Empty;
d0931270 12774 end Visible_Component;
12775
77fd9c7a 12776 -- Start of processing for Replace_Type_References_Generic
2072eaa9 12777
12778 begin
12779 Replace_Type_Refs (N);
12780 end Replace_Type_References_Generic;
12781
81bd1c0d 12782 --------------------------------
12783 -- Resolve_Aspect_Expressions --
12784 --------------------------------
12785
12786 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
9c20237a 12787 function Resolve_Name (N : Node_Id) return Traverse_Result;
12788 -- Verify that all identifiers in the expression, with the exception
12789 -- of references to the current entity, denote visible entities. This
12790 -- is done only to detect visibility errors, as the expression will be
12791 -- properly analyzed/expanded during analysis of the predicate function
c098acfb 12792 -- body. We omit quantified expressions from this test, given that they
12793 -- introduce a local identifier that would require proper expansion to
12794 -- handle properly.
9c20237a 12795
25e4fa47 12796 -- In ASIS_Mode we preserve the entity in the source because there is
12797 -- no subsequent expansion to decorate the tree.
12798
9c20237a 12799 ------------------
12800 -- Resolve_Name --
12801 ------------------
12802
12803 function Resolve_Name (N : Node_Id) return Traverse_Result is
37066559 12804 Dummy : Traverse_Result;
85bbb15a 12805
9c20237a 12806 begin
12807 if Nkind (N) = N_Selected_Component then
12808 if Nkind (Prefix (N)) = N_Identifier
12809 and then Chars (Prefix (N)) /= Chars (E)
12810 then
f4e18891 12811 Find_Selected_Component (N);
9c20237a 12812 end if;
02e5d0d0 12813
9c20237a 12814 return Skip;
12815
2a6c14a6 12816 -- Resolve identifiers that are not selectors in parameter
12817 -- associations (these are never resolved by visibility).
12818
12819 elsif Nkind (N) = N_Identifier
12820 and then Chars (N) /= Chars (E)
12821 and then (Nkind (Parent (N)) /= N_Parameter_Association
12822 or else N /= Selector_Name (Parent (N)))
12823 then
9c20237a 12824 Find_Direct_Name (N);
25e4fa47 12825
156588cb 12826 -- In ASIS mode we must analyze overloaded identifiers to ensure
12827 -- their correct decoration because expansion is disabled (and
12828 -- the expansion of freeze nodes takes care of resolving aspect
12829 -- expressions).
12830
12831 if ASIS_Mode then
12832 if Is_Overloaded (N) then
12833 Analyze (Parent (N));
12834 end if;
12835 else
25e4fa47 12836 Set_Entity (N, Empty);
12837 end if;
c098acfb 12838
37066559 12839 -- The name is component association needs no resolution.
12840
12841 elsif Nkind (N) = N_Component_Association then
12842 Dummy := Resolve_Name (Expression (N));
12843 return Skip;
12844
c098acfb 12845 elsif Nkind (N) = N_Quantified_Expression then
12846 return Skip;
9c20237a 12847 end if;
12848
12849 return OK;
12850 end Resolve_Name;
12851
12852 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12853
85bbb15a 12854 -- Local variables
12855
bfed3e04 12856 ASN : Node_Id := First_Rep_Item (E);
12857
02e5d0d0 12858 -- Start of processing for Resolve_Aspect_Expressions
12859
81bd1c0d 12860 begin
bfed3e04 12861 -- Need to make sure discriminants, if any, are directly visible
81bd1c0d 12862
bfed3e04 12863 Push_Scope_And_Install_Discriminants (E);
97c23bbe 12864
bfed3e04 12865 while Present (ASN) loop
12866 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12867 declare
12868 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
12869 Expr : constant Node_Id := Expression (ASN);
5535eed4 12870
bfed3e04 12871 begin
12872 case A_Id is
5535eed4 12873
bfed3e04 12874 -- For now we only deal with aspects that do not generate
12875 -- subprograms, or that may mention current instances of
12876 -- types. These will require special handling (???TBD).
81bd1c0d 12877
bfed3e04 12878 when Aspect_Invariant
12879 | Aspect_Predicate
12880 | Aspect_Predicate_Failure
12881 =>
12882 null;
81bd1c0d 12883
bfed3e04 12884 when Aspect_Dynamic_Predicate
12885 | Aspect_Static_Predicate
12886 =>
12887 -- Build predicate function specification and preanalyze
37066559 12888 -- expression after type replacement. The function
12889 -- declaration must be analyzed in the scope of the
12890 -- type, but the expression must see components.
9c20237a 12891
bfed3e04 12892 if No (Predicate_Function (E)) then
37066559 12893 Uninstall_Discriminants_And_Pop_Scope (E);
bfed3e04 12894 declare
12895 FDecl : constant Node_Id :=
12896 Build_Predicate_Function_Declaration (E);
12897 pragma Unreferenced (FDecl);
37066559 12898
bfed3e04 12899 begin
37066559 12900 Push_Scope_And_Install_Discriminants (E);
bfed3e04 12901 Resolve_Aspect_Expression (Expr);
12902 end;
12903 end if;
9c20237a 12904
bfed3e04 12905 when Pre_Post_Aspects =>
12906 null;
81bd1c0d 12907
bfed3e04 12908 when Aspect_Iterable =>
12909 if Nkind (Expr) = N_Aggregate then
12910 declare
12911 Assoc : Node_Id;
81bd1c0d 12912
bfed3e04 12913 begin
12914 Assoc := First (Component_Associations (Expr));
12915 while Present (Assoc) loop
12916 Find_Direct_Name (Expression (Assoc));
12917 Next (Assoc);
12918 end loop;
12919 end;
12920 end if;
81bd1c0d 12921
4cb8adff 12922 -- The expression for Default_Value is a static expression
12923 -- of the type, but this expression does not freeze the
12924 -- type, so it can still appear in a representation clause
12925 -- before the actual freeze point.
12926
12927 when Aspect_Default_Value =>
12928 Set_Must_Not_Freeze (Expr);
12929 Preanalyze_Spec_Expression (Expr, E);
12930
1728e3b3 12931 -- Ditto for Storage_Size. Any other aspects that carry
12932 -- expressions that should not freeze ??? This is only
12933 -- relevant to the misuse of deferred constants.
12934
12935 when Aspect_Storage_Size =>
12936 Set_Must_Not_Freeze (Expr);
12937 Preanalyze_Spec_Expression (Expr, Any_Integer);
12938
bfed3e04 12939 when others =>
12940 if Present (Expr) then
12941 case Aspect_Argument (A_Id) is
12942 when Expression
12943 | Optional_Expression
12944 =>
12945 Analyze_And_Resolve (Expr);
12946
12947 when Name
12948 | Optional_Name
12949 =>
12950 if Nkind (Expr) = N_Identifier then
12951 Find_Direct_Name (Expr);
12952
12953 elsif Nkind (Expr) = N_Selected_Component then
12954 Find_Selected_Component (Expr);
12955 end if;
12956 end case;
12957 end if;
12958 end case;
12959 end;
81bd1c0d 12960 end if;
12961
a738763e 12962 ASN := Next_Rep_Item (ASN);
81bd1c0d 12963 end loop;
bfed3e04 12964
12965 Uninstall_Discriminants_And_Pop_Scope (E);
81bd1c0d 12966 end Resolve_Aspect_Expressions;
12967
d6f39728 12968 -------------------------
12969 -- Same_Representation --
12970 -------------------------
12971
12972 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12973 T1 : constant Entity_Id := Underlying_Type (Typ1);
12974 T2 : constant Entity_Id := Underlying_Type (Typ2);
12975
12976 begin
12977 -- A quick check, if base types are the same, then we definitely have
12978 -- the same representation, because the subtype specific representation
12979 -- attributes (Size and Alignment) do not affect representation from
12980 -- the point of view of this test.
12981
12982 if Base_Type (T1) = Base_Type (T2) then
12983 return True;
12984
12985 elsif Is_Private_Type (Base_Type (T2))
12986 and then Base_Type (T1) = Full_View (Base_Type (T2))
12987 then
12988 return True;
12989 end if;
12990
3645e9c5 12991 -- Tagged types always have the same representation, because it is not
12992 -- possible to specify different representations for common fields.
d6f39728 12993
12994 if Is_Tagged_Type (T1) then
12995 return True;
12996 end if;
12997
12998 -- Representations are definitely different if conventions differ
12999
13000 if Convention (T1) /= Convention (T2) then
13001 return False;
13002 end if;
13003
ef0772bc 13004 -- Representations are different if component alignments or scalar
13005 -- storage orders differ.
d6f39728 13006
13007 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
726fd56a 13008 and then
d6f39728 13009 (Is_Record_Type (T2) or else Is_Array_Type (T2))
ef0772bc 13010 and then
13011 (Component_Alignment (T1) /= Component_Alignment (T2)
f02a9a9a 13012 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
d6f39728 13013 then
13014 return False;
13015 end if;
13016
13017 -- For arrays, the only real issue is component size. If we know the
13018 -- component size for both arrays, and it is the same, then that's
13019 -- good enough to know we don't have a change of representation.
13020
13021 if Is_Array_Type (T1) then
13022 if Known_Component_Size (T1)
13023 and then Known_Component_Size (T2)
13024 and then Component_Size (T1) = Component_Size (T2)
13025 then
36ac5fbb 13026 return True;
d6f39728 13027 end if;
13028 end if;
13029
3645e9c5 13030 -- For records, representations are different if reorderings differ
13031
13032 if Is_Record_Type (T1)
13033 and then Is_Record_Type (T2)
13034 and then No_Reordering (T1) /= No_Reordering (T2)
13035 then
13036 return False;
13037 end if;
13038
d6f39728 13039 -- Types definitely have same representation if neither has non-standard
13040 -- representation since default representations are always consistent.
13041 -- If only one has non-standard representation, and the other does not,
13042 -- then we consider that they do not have the same representation. They
13043 -- might, but there is no way of telling early enough.
13044
13045 if Has_Non_Standard_Rep (T1) then
13046 if not Has_Non_Standard_Rep (T2) then
13047 return False;
13048 end if;
13049 else
13050 return not Has_Non_Standard_Rep (T2);
13051 end if;
13052
fdd294d1 13053 -- Here the two types both have non-standard representation, and we need
13054 -- to determine if they have the same non-standard representation.
d6f39728 13055
13056 -- For arrays, we simply need to test if the component sizes are the
13057 -- same. Pragma Pack is reflected in modified component sizes, so this
13058 -- check also deals with pragma Pack.
13059
13060 if Is_Array_Type (T1) then
13061 return Component_Size (T1) = Component_Size (T2);
13062
d6f39728 13063 -- Case of record types
13064
13065 elsif Is_Record_Type (T1) then
13066
13067 -- Packed status must conform
13068
13069 if Is_Packed (T1) /= Is_Packed (T2) then
13070 return False;
13071
13072 -- Otherwise we must check components. Typ2 maybe a constrained
13073 -- subtype with fewer components, so we compare the components
13074 -- of the base types.
13075
13076 else
13077 Record_Case : declare
13078 CD1, CD2 : Entity_Id;
13079
13080 function Same_Rep return Boolean;
13081 -- CD1 and CD2 are either components or discriminants. This
ef0772bc 13082 -- function tests whether they have the same representation.
d6f39728 13083
80d4fec4 13084 --------------
13085 -- Same_Rep --
13086 --------------
13087
d6f39728 13088 function Same_Rep return Boolean is
13089 begin
13090 if No (Component_Clause (CD1)) then
13091 return No (Component_Clause (CD2));
d6f39728 13092 else
ef0772bc 13093 -- Note: at this point, component clauses have been
13094 -- normalized to the default bit order, so that the
13095 -- comparison of Component_Bit_Offsets is meaningful.
13096
d6f39728 13097 return
13098 Present (Component_Clause (CD2))
13099 and then
13100 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
13101 and then
13102 Esize (CD1) = Esize (CD2);
13103 end if;
13104 end Same_Rep;
13105
1e35409d 13106 -- Start of processing for Record_Case
d6f39728 13107
13108 begin
13109 if Has_Discriminants (T1) then
d6f39728 13110
9dfe12ae 13111 -- The number of discriminants may be different if the
13112 -- derived type has fewer (constrained by values). The
13113 -- invisible discriminants retain the representation of
13114 -- the original, so the discrepancy does not per se
13115 -- indicate a different representation.
13116
b9e61b2a 13117 CD1 := First_Discriminant (T1);
13118 CD2 := First_Discriminant (T2);
13119 while Present (CD1) and then Present (CD2) loop
d6f39728 13120 if not Same_Rep then
13121 return False;
13122 else
13123 Next_Discriminant (CD1);
13124 Next_Discriminant (CD2);
13125 end if;
13126 end loop;
13127 end if;
13128
13129 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
13130 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
d6f39728 13131 while Present (CD1) loop
13132 if not Same_Rep then
13133 return False;
13134 else
13135 Next_Component (CD1);
13136 Next_Component (CD2);
13137 end if;
13138 end loop;
13139
13140 return True;
13141 end Record_Case;
13142 end if;
13143
13144 -- For enumeration types, we must check each literal to see if the
13145 -- representation is the same. Note that we do not permit enumeration
1a34e48c 13146 -- representation clauses for Character and Wide_Character, so these
d6f39728 13147 -- cases were already dealt with.
13148
13149 elsif Is_Enumeration_Type (T1) then
d6f39728 13150 Enumeration_Case : declare
13151 L1, L2 : Entity_Id;
13152
13153 begin
13154 L1 := First_Literal (T1);
13155 L2 := First_Literal (T2);
d6f39728 13156 while Present (L1) loop
13157 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
13158 return False;
13159 else
13160 Next_Literal (L1);
13161 Next_Literal (L2);
13162 end if;
13163 end loop;
13164
13165 return True;
d6f39728 13166 end Enumeration_Case;
13167
13168 -- Any other types have the same representation for these purposes
13169
13170 else
13171 return True;
13172 end if;
d6f39728 13173 end Same_Representation;
13174
3061ffde 13175 --------------------------------
13176 -- Resolve_Iterable_Operation --
13177 --------------------------------
13178
13179 procedure Resolve_Iterable_Operation
13180 (N : Node_Id;
13181 Cursor : Entity_Id;
13182 Typ : Entity_Id;
13183 Nam : Name_Id)
13184 is
13185 Ent : Entity_Id;
13186 F1 : Entity_Id;
13187 F2 : Entity_Id;
13188
13189 begin
13190 if not Is_Overloaded (N) then
13191 if not Is_Entity_Name (N)
13192 or else Ekind (Entity (N)) /= E_Function
13193 or else Scope (Entity (N)) /= Scope (Typ)
13194 or else No (First_Formal (Entity (N)))
13195 or else Etype (First_Formal (Entity (N))) /= Typ
13196 then
13197 Error_Msg_N ("iterable primitive must be local function name "
13198 & "whose first formal is an iterable type", N);
a9f5fea7 13199 return;
3061ffde 13200 end if;
13201
13202 Ent := Entity (N);
13203 F1 := First_Formal (Ent);
13204 if Nam = Name_First then
13205
13206 -- First (Container) => Cursor
13207
13208 if Etype (Ent) /= Cursor then
13209 Error_Msg_N ("primitive for First must yield a curosr", N);
13210 end if;
13211
13212 elsif Nam = Name_Next then
13213
13214 -- Next (Container, Cursor) => Cursor
13215
13216 F2 := Next_Formal (F1);
13217
13218 if Etype (F2) /= Cursor
13219 or else Etype (Ent) /= Cursor
13220 or else Present (Next_Formal (F2))
13221 then
13222 Error_Msg_N ("no match for Next iterable primitive", N);
13223 end if;
13224
13225 elsif Nam = Name_Has_Element then
13226
13227 -- Has_Element (Container, Cursor) => Boolean
13228
13229 F2 := Next_Formal (F1);
13230 if Etype (F2) /= Cursor
13231 or else Etype (Ent) /= Standard_Boolean
13232 or else Present (Next_Formal (F2))
13233 then
13234 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13235 end if;
13236
13237 elsif Nam = Name_Element then
b9b03799 13238 F2 := Next_Formal (F1);
13239
13240 if No (F2)
13241 or else Etype (F2) /= Cursor
13242 or else Present (Next_Formal (F2))
13243 then
13244 Error_Msg_N ("no match for Element iterable primitive", N);
13245 end if;
3061ffde 13246 null;
13247
13248 else
13249 raise Program_Error;
13250 end if;
13251
13252 else
13253 -- Overloaded case: find subprogram with proper signature.
13254 -- Caller will report error if no match is found.
13255
13256 declare
13257 I : Interp_Index;
13258 It : Interp;
13259
13260 begin
13261 Get_First_Interp (N, I, It);
13262 while Present (It.Typ) loop
13263 if Ekind (It.Nam) = E_Function
b9b03799 13264 and then Scope (It.Nam) = Scope (Typ)
3061ffde 13265 and then Etype (First_Formal (It.Nam)) = Typ
13266 then
13267 F1 := First_Formal (It.Nam);
13268
13269 if Nam = Name_First then
13270 if Etype (It.Nam) = Cursor
13271 and then No (Next_Formal (F1))
13272 then
13273 Set_Entity (N, It.Nam);
13274 exit;
13275 end if;
13276
13277 elsif Nam = Name_Next then
13278 F2 := Next_Formal (F1);
13279
13280 if Present (F2)
13281 and then No (Next_Formal (F2))
13282 and then Etype (F2) = Cursor
13283 and then Etype (It.Nam) = Cursor
13284 then
13285 Set_Entity (N, It.Nam);
13286 exit;
13287 end if;
13288
13289 elsif Nam = Name_Has_Element then
13290 F2 := Next_Formal (F1);
13291
13292 if Present (F2)
13293 and then No (Next_Formal (F2))
13294 and then Etype (F2) = Cursor
13295 and then Etype (It.Nam) = Standard_Boolean
13296 then
13297 Set_Entity (N, It.Nam);
13298 F2 := Next_Formal (F1);
13299 exit;
13300 end if;
13301
13302 elsif Nam = Name_Element then
b9b03799 13303 F2 := Next_Formal (F1);
13304
3061ffde 13305 if Present (F2)
13306 and then No (Next_Formal (F2))
13307 and then Etype (F2) = Cursor
13308 then
13309 Set_Entity (N, It.Nam);
13310 exit;
13311 end if;
13312 end if;
13313 end if;
13314
13315 Get_Next_Interp (I, It);
13316 end loop;
13317 end;
13318 end if;
13319 end Resolve_Iterable_Operation;
13320
b77e4501 13321 ----------------
13322 -- Set_Biased --
13323 ----------------
13324
13325 procedure Set_Biased
13326 (E : Entity_Id;
13327 N : Node_Id;
13328 Msg : String;
13329 Biased : Boolean := True)
13330 is
13331 begin
13332 if Biased then
13333 Set_Has_Biased_Representation (E);
13334
13335 if Warn_On_Biased_Representation then
13336 Error_Msg_NE
1e3532e7 13337 ("?B?" & Msg & " forces biased representation for&", N, E);
b77e4501 13338 end if;
13339 end if;
13340 end Set_Biased;
13341
d6f39728 13342 --------------------
13343 -- Set_Enum_Esize --
13344 --------------------
13345
13346 procedure Set_Enum_Esize (T : Entity_Id) is
13347 Lo : Uint;
13348 Hi : Uint;
13349 Sz : Nat;
13350
13351 begin
13352 Init_Alignment (T);
13353
13354 -- Find the minimum standard size (8,16,32,64) that fits
13355
13356 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13357 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13358
13359 if Lo < 0 then
13360 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
f15731c4 13361 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 13362
13363 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13364 Sz := 16;
13365
13366 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13367 Sz := 32;
13368
13369 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13370 Sz := 64;
13371 end if;
13372
13373 else
13374 if Hi < Uint_2**08 then
f15731c4 13375 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 13376
13377 elsif Hi < Uint_2**16 then
13378 Sz := 16;
13379
13380 elsif Hi < Uint_2**32 then
13381 Sz := 32;
13382
13383 else pragma Assert (Hi < Uint_2**63);
13384 Sz := 64;
13385 end if;
13386 end if;
13387
13388 -- That minimum is the proper size unless we have a foreign convention
13389 -- and the size required is 32 or less, in which case we bump the size
13390 -- up to 32. This is required for C and C++ and seems reasonable for
13391 -- all other foreign conventions.
13392
13393 if Has_Foreign_Convention (T)
13394 and then Esize (T) < Standard_Integer_Size
db1eed69 13395
13396 -- Don't do this if Short_Enums on target
13397
e9185b9d 13398 and then not Target_Short_Enums
d6f39728 13399 then
13400 Init_Esize (T, Standard_Integer_Size);
d6f39728 13401 else
13402 Init_Esize (T, Sz);
13403 end if;
d6f39728 13404 end Set_Enum_Esize;
13405
2625eb01 13406 -----------------------------
13407 -- Uninstall_Discriminants --
13408 -----------------------------
13409
13410 procedure Uninstall_Discriminants (E : Entity_Id) is
13411 Disc : Entity_Id;
13412 Prev : Entity_Id;
13413 Outer : Entity_Id;
13414
13415 begin
13416 -- Discriminants have been made visible for type declarations and
13417 -- protected type declarations, not for subtype declarations.
13418
13419 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13420 Disc := First_Discriminant (E);
13421 while Present (Disc) loop
13422 if Disc /= Current_Entity (Disc) then
13423 Prev := Current_Entity (Disc);
13424 while Present (Prev)
13425 and then Present (Homonym (Prev))
13426 and then Homonym (Prev) /= Disc
13427 loop
13428 Prev := Homonym (Prev);
13429 end loop;
13430 else
13431 Prev := Empty;
13432 end if;
13433
13434 Set_Is_Immediately_Visible (Disc, False);
13435
13436 Outer := Homonym (Disc);
13437 while Present (Outer) and then Scope (Outer) = E loop
13438 Outer := Homonym (Outer);
13439 end loop;
13440
13441 -- Reset homonym link of other entities, but do not modify link
3ff5e35d 13442 -- between entities in current scope, so that the back end can
2625eb01 13443 -- have a proper count of local overloadings.
13444
13445 if No (Prev) then
13446 Set_Name_Entity_Id (Chars (Disc), Outer);
13447
13448 elsif Scope (Prev) /= Scope (Disc) then
13449 Set_Homonym (Prev, Outer);
13450 end if;
13451
13452 Next_Discriminant (Disc);
13453 end loop;
13454 end if;
13455 end Uninstall_Discriminants;
13456
13457 -------------------------------------------
13458 -- Uninstall_Discriminants_And_Pop_Scope --
13459 -------------------------------------------
13460
13461 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13462 begin
13463 if Has_Discriminants (E) then
13464 Uninstall_Discriminants (E);
13465 Pop_Scope;
13466 end if;
13467 end Uninstall_Discriminants_And_Pop_Scope;
13468
83f8f0a6 13469 ------------------------------
13470 -- Validate_Address_Clauses --
13471 ------------------------------
13472
13473 procedure Validate_Address_Clauses is
c7a1569a 13474 function Offset_Value (Expr : Node_Id) return Uint;
13475 -- Given an Address attribute reference, return the value in bits of its
13476 -- offset from the first bit of the underlying entity, or 0 if it is not
13477 -- known at compile time.
13478
13479 ------------------
13480 -- Offset_Value --
13481 ------------------
13482
13483 function Offset_Value (Expr : Node_Id) return Uint is
13484 N : Node_Id := Prefix (Expr);
13485 Off : Uint;
13486 Val : Uint := Uint_0;
13487
13488 begin
13489 -- Climb the prefix chain and compute the cumulative offset
13490
13491 loop
13492 if Is_Entity_Name (N) then
13493 return Val;
13494
13495 elsif Nkind (N) = N_Selected_Component then
13496 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13497 if Off /= No_Uint and then Off >= Uint_0 then
13498 Val := Val + Off;
13499 N := Prefix (N);
13500 else
13501 return Uint_0;
13502 end if;
13503
13504 elsif Nkind (N) = N_Indexed_Component then
13505 Off := Indexed_Component_Bit_Offset (N);
13506 if Off /= No_Uint then
13507 Val := Val + Off;
13508 N := Prefix (N);
13509 else
13510 return Uint_0;
13511 end if;
13512
13513 else
13514 return Uint_0;
13515 end if;
13516 end loop;
13517 end Offset_Value;
13518
13519 -- Start of processing for Validate_Address_Clauses
13520
83f8f0a6 13521 begin
13522 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13523 declare
13524 ACCR : Address_Clause_Check_Record
13525 renames Address_Clause_Checks.Table (J);
13526
d6da7448 13527 Expr : Node_Id;
13528
83f8f0a6 13529 X_Alignment : Uint;
f907cb13 13530 Y_Alignment : Uint := Uint_0;
83f8f0a6 13531
13532 X_Size : Uint;
f907cb13 13533 Y_Size : Uint := Uint_0;
83f8f0a6 13534
c7a1569a 13535 X_Offs : Uint;
13536
83f8f0a6 13537 begin
13538 -- Skip processing of this entry if warning already posted
13539
13540 if not Address_Warning_Posted (ACCR.N) then
d6da7448 13541 Expr := Original_Node (Expression (ACCR.N));
83f8f0a6 13542
514a5555 13543 -- Get alignments, sizes and offset, if any
83f8f0a6 13544
d6da7448 13545 X_Alignment := Alignment (ACCR.X);
8650387e 13546 X_Size := Esize (ACCR.X);
514a5555 13547
13548 if Present (ACCR.Y) then
13549 Y_Alignment := Alignment (ACCR.Y);
8650387e 13550 Y_Size := Esize (ACCR.Y);
514a5555 13551 end if;
83f8f0a6 13552
c7a1569a 13553 if ACCR.Off
13554 and then Nkind (Expr) = N_Attribute_Reference
13555 and then Attribute_Name (Expr) = Name_Address
13556 then
13557 X_Offs := Offset_Value (Expr);
13558 else
13559 X_Offs := Uint_0;
13560 end if;
13561
514a5555 13562 -- Check for known value not multiple of alignment
13563
13564 if No (ACCR.Y) then
d10a1b95 13565 if not Alignment_Checks_Suppressed (ACCR)
514a5555 13566 and then X_Alignment /= 0
13567 and then ACCR.A mod X_Alignment /= 0
13568 then
13569 Error_Msg_NE
13570 ("??specified address for& is inconsistent with "
13571 & "alignment", ACCR.N, ACCR.X);
13572 Error_Msg_N
13573 ("\??program execution may be erroneous (RM 13.3(27))",
13574 ACCR.N);
13575
13576 Error_Msg_Uint_1 := X_Alignment;
13577 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13578 end if;
13579
83f8f0a6 13580 -- Check for large object overlaying smaller one
13581
514a5555 13582 elsif Y_Size > Uint_0
83f8f0a6 13583 and then X_Size > Uint_0
c7a1569a 13584 and then X_Offs + X_Size > Y_Size
83f8f0a6 13585 then
7161e166 13586 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
83f8f0a6 13587 Error_Msg_N
1e3532e7 13588 ("\??program execution may be erroneous", ACCR.N);
7161e166 13589
83f8f0a6 13590 Error_Msg_Uint_1 := X_Size;
7161e166 13591 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13592
83f8f0a6 13593 Error_Msg_Uint_1 := Y_Size;
7161e166 13594 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
83f8f0a6 13595
f5cc2579 13596 if Y_Size >= X_Size then
c7a1569a 13597 Error_Msg_Uint_1 := X_Offs;
f5cc2579 13598 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
c7a1569a 13599 end if;
13600
d6da7448 13601 -- Check for inadequate alignment, both of the base object
e556831e 13602 -- and of the offset, if any. We only do this check if the
13603 -- run-time Alignment_Check is active. No point in warning
13604 -- if this check has been suppressed (or is suppressed by
13605 -- default in the non-strict alignment machine case).
83f8f0a6 13606
d6da7448 13607 -- Note: we do not check the alignment if we gave a size
13608 -- warning, since it would likely be redundant.
83f8f0a6 13609
d10a1b95 13610 elsif not Alignment_Checks_Suppressed (ACCR)
e556831e 13611 and then Y_Alignment /= Uint_0
7161e166 13612 and then
13613 (Y_Alignment < X_Alignment
13614 or else
13615 (ACCR.Off
13616 and then Nkind (Expr) = N_Attribute_Reference
13617 and then Attribute_Name (Expr) = Name_Address
13618 and then Has_Compatible_Alignment
13619 (ACCR.X, Prefix (Expr), True) /=
13620 Known_Compatible))
83f8f0a6 13621 then
13622 Error_Msg_NE
7161e166 13623 ("??specified address for& may be inconsistent with "
13624 & "alignment", ACCR.N, ACCR.X);
83f8f0a6 13625 Error_Msg_N
1e3532e7 13626 ("\??program execution may be erroneous (RM 13.3(27))",
83f8f0a6 13627 ACCR.N);
7161e166 13628
83f8f0a6 13629 Error_Msg_Uint_1 := X_Alignment;
7161e166 13630 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13631
83f8f0a6 13632 Error_Msg_Uint_1 := Y_Alignment;
7161e166 13633 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13634
d6da7448 13635 if Y_Alignment >= X_Alignment then
13636 Error_Msg_N
7161e166 13637 ("\??but offset is not multiple of alignment", ACCR.N);
d6da7448 13638 end if;
83f8f0a6 13639 end if;
13640 end if;
13641 end;
13642 end loop;
13643 end Validate_Address_Clauses;
13644
76a6b7c7 13645 -----------------------------------------
13646 -- Validate_Compile_Time_Warning_Error --
13647 -----------------------------------------
13648
13649 procedure Validate_Compile_Time_Warning_Error (N : Node_Id) is
13650 begin
13651 Compile_Time_Warnings_Errors.Append
13652 (New_Val => CTWE_Entry'(Eloc => Sloc (N),
13653 Scope => Current_Scope,
13654 Prag => N));
13655 end Validate_Compile_Time_Warning_Error;
13656
13657 ------------------------------------------
13658 -- Validate_Compile_Time_Warning_Errors --
13659 ------------------------------------------
13660
13661 procedure Validate_Compile_Time_Warning_Errors is
13662 procedure Set_Scope (S : Entity_Id);
13663 -- Install all enclosing scopes of S along with S itself
13664
13665 procedure Unset_Scope (S : Entity_Id);
13666 -- Uninstall all enclosing scopes of S along with S itself
13667
13668 ---------------
13669 -- Set_Scope --
13670 ---------------
13671
13672 procedure Set_Scope (S : Entity_Id) is
13673 begin
13674 if S /= Standard_Standard then
13675 Set_Scope (Scope (S));
13676 end if;
13677
13678 Push_Scope (S);
13679 end Set_Scope;
13680
13681 -----------------
13682 -- Unset_Scope --
13683 -----------------
13684
13685 procedure Unset_Scope (S : Entity_Id) is
13686 begin
13687 if S /= Standard_Standard then
13688 Unset_Scope (Scope (S));
13689 end if;
13690
13691 Pop_Scope;
13692 end Unset_Scope;
13693
13694 -- Start of processing for Validate_Compile_Time_Warning_Errors
13695
13696 begin
13697 Expander_Mode_Save_And_Set (False);
13698 In_Compile_Time_Warning_Or_Error := True;
13699
13700 for N in Compile_Time_Warnings_Errors.First ..
13701 Compile_Time_Warnings_Errors.Last
13702 loop
13703 declare
13704 T : CTWE_Entry renames Compile_Time_Warnings_Errors.Table (N);
13705
13706 begin
13707 Set_Scope (T.Scope);
13708 Reset_Analyzed_Flags (T.Prag);
13709 Process_Compile_Time_Warning_Or_Error (T.Prag, T.Eloc);
13710 Unset_Scope (T.Scope);
13711 end;
13712 end loop;
13713
13714 In_Compile_Time_Warning_Or_Error := False;
13715 Expander_Mode_Restore;
13716 end Validate_Compile_Time_Warning_Errors;
13717
7717ea00 13718 ---------------------------
13719 -- Validate_Independence --
13720 ---------------------------
13721
13722 procedure Validate_Independence is
13723 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13724 N : Node_Id;
13725 E : Entity_Id;
13726 IC : Boolean;
13727 Comp : Entity_Id;
13728 Addr : Node_Id;
13729 P : Node_Id;
13730
13731 procedure Check_Array_Type (Atyp : Entity_Id);
13732 -- Checks if the array type Atyp has independent components, and
13733 -- if not, outputs an appropriate set of error messages.
13734
13735 procedure No_Independence;
13736 -- Output message that independence cannot be guaranteed
13737
13738 function OK_Component (C : Entity_Id) return Boolean;
13739 -- Checks one component to see if it is independently accessible, and
13740 -- if so yields True, otherwise yields False if independent access
13741 -- cannot be guaranteed. This is a conservative routine, it only
13742 -- returns True if it knows for sure, it returns False if it knows
13743 -- there is a problem, or it cannot be sure there is no problem.
13744
13745 procedure Reason_Bad_Component (C : Entity_Id);
13746 -- Outputs continuation message if a reason can be determined for
13747 -- the component C being bad.
13748
13749 ----------------------
13750 -- Check_Array_Type --
13751 ----------------------
13752
13753 procedure Check_Array_Type (Atyp : Entity_Id) is
13754 Ctyp : constant Entity_Id := Component_Type (Atyp);
13755
13756 begin
13757 -- OK if no alignment clause, no pack, and no component size
13758
13759 if not Has_Component_Size_Clause (Atyp)
13760 and then not Has_Alignment_Clause (Atyp)
13761 and then not Is_Packed (Atyp)
13762 then
13763 return;
13764 end if;
13765
aa0a69ab 13766 -- Case of component size is greater than or equal to 64 and the
13767 -- alignment of the array is at least as large as the alignment
13768 -- of the component. We are definitely OK in this situation.
13769
13770 if Known_Component_Size (Atyp)
13771 and then Component_Size (Atyp) >= 64
13772 and then Known_Alignment (Atyp)
13773 and then Known_Alignment (Ctyp)
13774 and then Alignment (Atyp) >= Alignment (Ctyp)
13775 then
13776 return;
13777 end if;
13778
7717ea00 13779 -- Check actual component size
13780
13781 if not Known_Component_Size (Atyp)
13782 or else not (Addressable (Component_Size (Atyp))
aa0a69ab 13783 and then Component_Size (Atyp) < 64)
7717ea00 13784 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13785 then
13786 No_Independence;
13787
13788 -- Bad component size, check reason
13789
13790 if Has_Component_Size_Clause (Atyp) then
b9e61b2a 13791 P := Get_Attribute_Definition_Clause
13792 (Atyp, Attribute_Component_Size);
7717ea00 13793
13794 if Present (P) then
13795 Error_Msg_Sloc := Sloc (P);
13796 Error_Msg_N ("\because of Component_Size clause#", N);
13797 return;
13798 end if;
13799 end if;
13800
13801 if Is_Packed (Atyp) then
13802 P := Get_Rep_Pragma (Atyp, Name_Pack);
13803
13804 if Present (P) then
13805 Error_Msg_Sloc := Sloc (P);
13806 Error_Msg_N ("\because of pragma Pack#", N);
13807 return;
13808 end if;
13809 end if;
13810
13811 -- No reason found, just return
13812
13813 return;
13814 end if;
13815
13816 -- Array type is OK independence-wise
13817
13818 return;
13819 end Check_Array_Type;
13820
13821 ---------------------
13822 -- No_Independence --
13823 ---------------------
13824
13825 procedure No_Independence is
13826 begin
ddccc924 13827 if Pragma_Name (N) = Name_Independent then
18393965 13828 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
7717ea00 13829 else
13830 Error_Msg_NE
13831 ("independent components cannot be guaranteed for&", N, E);
13832 end if;
13833 end No_Independence;
13834
13835 ------------------
13836 -- OK_Component --
13837 ------------------
13838
13839 function OK_Component (C : Entity_Id) return Boolean is
13840 Rec : constant Entity_Id := Scope (C);
13841 Ctyp : constant Entity_Id := Etype (C);
13842
13843 begin
13844 -- OK if no component clause, no Pack, and no alignment clause
13845
13846 if No (Component_Clause (C))
13847 and then not Is_Packed (Rec)
13848 and then not Has_Alignment_Clause (Rec)
13849 then
13850 return True;
13851 end if;
13852
13853 -- Here we look at the actual component layout. A component is
13854 -- addressable if its size is a multiple of the Esize of the
13855 -- component type, and its starting position in the record has
13856 -- appropriate alignment, and the record itself has appropriate
13857 -- alignment to guarantee the component alignment.
13858
13859 -- Make sure sizes are static, always assume the worst for any
13860 -- cases where we cannot check static values.
13861
13862 if not (Known_Static_Esize (C)
b9e61b2a 13863 and then
13864 Known_Static_Esize (Ctyp))
7717ea00 13865 then
13866 return False;
13867 end if;
13868
13869 -- Size of component must be addressable or greater than 64 bits
13870 -- and a multiple of bytes.
13871
b9e61b2a 13872 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
7717ea00 13873 return False;
13874 end if;
13875
13876 -- Check size is proper multiple
13877
13878 if Esize (C) mod Esize (Ctyp) /= 0 then
13879 return False;
13880 end if;
13881
13882 -- Check alignment of component is OK
13883
13884 if not Known_Component_Bit_Offset (C)
13885 or else Component_Bit_Offset (C) < Uint_0
13886 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13887 then
13888 return False;
13889 end if;
13890
13891 -- Check alignment of record type is OK
13892
13893 if not Known_Alignment (Rec)
13894 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13895 then
13896 return False;
13897 end if;
13898
13899 -- All tests passed, component is addressable
13900
13901 return True;
13902 end OK_Component;
13903
13904 --------------------------
13905 -- Reason_Bad_Component --
13906 --------------------------
13907
13908 procedure Reason_Bad_Component (C : Entity_Id) is
13909 Rec : constant Entity_Id := Scope (C);
13910 Ctyp : constant Entity_Id := Etype (C);
13911
13912 begin
13913 -- If component clause present assume that's the problem
13914
13915 if Present (Component_Clause (C)) then
13916 Error_Msg_Sloc := Sloc (Component_Clause (C));
13917 Error_Msg_N ("\because of Component_Clause#", N);
13918 return;
13919 end if;
13920
13921 -- If pragma Pack clause present, assume that's the problem
13922
13923 if Is_Packed (Rec) then
13924 P := Get_Rep_Pragma (Rec, Name_Pack);
13925
13926 if Present (P) then
13927 Error_Msg_Sloc := Sloc (P);
13928 Error_Msg_N ("\because of pragma Pack#", N);
13929 return;
13930 end if;
13931 end if;
13932
13933 -- See if record has bad alignment clause
13934
13935 if Has_Alignment_Clause (Rec)
13936 and then Known_Alignment (Rec)
13937 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13938 then
13939 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13940
13941 if Present (P) then
13942 Error_Msg_Sloc := Sloc (P);
13943 Error_Msg_N ("\because of Alignment clause#", N);
13944 end if;
13945 end if;
13946
13947 -- Couldn't find a reason, so return without a message
13948
13949 return;
13950 end Reason_Bad_Component;
13951
13952 -- Start of processing for Validate_Independence
13953
13954 begin
13955 for J in Independence_Checks.First .. Independence_Checks.Last loop
13956 N := Independence_Checks.Table (J).N;
13957 E := Independence_Checks.Table (J).E;
ddccc924 13958 IC := Pragma_Name (N) = Name_Independent_Components;
7717ea00 13959
13960 -- Deal with component case
13961
13962 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13963 if not OK_Component (E) then
13964 No_Independence;
13965 Reason_Bad_Component (E);
13966 goto Continue;
13967 end if;
13968 end if;
13969
13970 -- Deal with record with Independent_Components
13971
13972 if IC and then Is_Record_Type (E) then
13973 Comp := First_Component_Or_Discriminant (E);
13974 while Present (Comp) loop
13975 if not OK_Component (Comp) then
13976 No_Independence;
13977 Reason_Bad_Component (Comp);
13978 goto Continue;
13979 end if;
13980
13981 Next_Component_Or_Discriminant (Comp);
13982 end loop;
13983 end if;
13984
13985 -- Deal with address clause case
13986
13987 if Is_Object (E) then
13988 Addr := Address_Clause (E);
13989
13990 if Present (Addr) then
13991 No_Independence;
13992 Error_Msg_Sloc := Sloc (Addr);
13993 Error_Msg_N ("\because of Address clause#", N);
13994 goto Continue;
13995 end if;
13996 end if;
13997
13998 -- Deal with independent components for array type
13999
14000 if IC and then Is_Array_Type (E) then
14001 Check_Array_Type (E);
14002 end if;
14003
14004 -- Deal with independent components for array object
14005
14006 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
14007 Check_Array_Type (Etype (E));
14008 end if;
14009
14010 <<Continue>> null;
14011 end loop;
14012 end Validate_Independence;
14013
b3f8228a 14014 ------------------------------
14015 -- Validate_Iterable_Aspect --
14016 ------------------------------
14017
14018 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
3061ffde 14019 Assoc : Node_Id;
14020 Expr : Node_Id;
b3f8228a 14021
bde03454 14022 Prim : Node_Id;
a9f5fea7 14023 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
b3f8228a 14024
14025 First_Id : Entity_Id;
14026 Next_Id : Entity_Id;
14027 Has_Element_Id : Entity_Id;
14028 Element_Id : Entity_Id;
14029
b3f8228a 14030 begin
9698629c 14031 -- If previous error aspect is unusable
a9f5fea7 14032
14033 if Cursor = Any_Type then
3061ffde 14034 return;
14035 end if;
b3f8228a 14036
14037 First_Id := Empty;
14038 Next_Id := Empty;
14039 Has_Element_Id := Empty;
32de816b 14040 Element_Id := Empty;
b3f8228a 14041
14042 -- Each expression must resolve to a function with the proper signature
14043
14044 Assoc := First (Component_Associations (Expression (ASN)));
14045 while Present (Assoc) loop
14046 Expr := Expression (Assoc);
14047 Analyze (Expr);
14048
b3f8228a 14049 Prim := First (Choices (Assoc));
bde03454 14050
f02a9a9a 14051 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
b3f8228a 14052 Error_Msg_N ("illegal name in association", Prim);
14053
14054 elsif Chars (Prim) = Name_First then
3061ffde 14055 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
b3f8228a 14056 First_Id := Entity (Expr);
b3f8228a 14057
14058 elsif Chars (Prim) = Name_Next then
3061ffde 14059 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
b3f8228a 14060 Next_Id := Entity (Expr);
b3f8228a 14061
14062 elsif Chars (Prim) = Name_Has_Element then
3061ffde 14063 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
b3f8228a 14064 Has_Element_Id := Entity (Expr);
bde03454 14065
b3f8228a 14066 elsif Chars (Prim) = Name_Element then
3061ffde 14067 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
b3f8228a 14068 Element_Id := Entity (Expr);
b3f8228a 14069
14070 else
14071 Error_Msg_N ("invalid name for iterable function", Prim);
14072 end if;
14073
14074 Next (Assoc);
14075 end loop;
14076
14077 if No (First_Id) then
3061ffde 14078 Error_Msg_N ("match for First primitive not found", ASN);
b3f8228a 14079
14080 elsif No (Next_Id) then
3061ffde 14081 Error_Msg_N ("match for Next primitive not found", ASN);
b3f8228a 14082
14083 elsif No (Has_Element_Id) then
3061ffde 14084 Error_Msg_N ("match for Has_Element primitive not found", ASN);
14085
14086 elsif No (Element_Id) then
14087 null; -- Optional.
b3f8228a 14088 end if;
14089 end Validate_Iterable_Aspect;
14090
d6f39728 14091 -----------------------------------
14092 -- Validate_Unchecked_Conversion --
14093 -----------------------------------
14094
14095 procedure Validate_Unchecked_Conversion
14096 (N : Node_Id;
14097 Act_Unit : Entity_Id)
14098 is
14099 Source : Entity_Id;
14100 Target : Entity_Id;
14101 Vnode : Node_Id;
14102
14103 begin
14104 -- Obtain source and target types. Note that we call Ancestor_Subtype
14105 -- here because the processing for generic instantiation always makes
14106 -- subtypes, and we want the original frozen actual types.
14107
14108 -- If we are dealing with private types, then do the check on their
14109 -- fully declared counterparts if the full declarations have been
39a0c1d3 14110 -- encountered (they don't have to be visible, but they must exist).
d6f39728 14111
14112 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
14113
14114 if Is_Private_Type (Source)
14115 and then Present (Underlying_Type (Source))
14116 then
14117 Source := Underlying_Type (Source);
14118 end if;
14119
14120 Target := Ancestor_Subtype (Etype (Act_Unit));
14121
fdd294d1 14122 -- If either type is generic, the instantiation happens within a generic
95deda50 14123 -- unit, and there is nothing to check. The proper check will happen
14124 -- when the enclosing generic is instantiated.
d6f39728 14125
14126 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
14127 return;
14128 end if;
14129
14130 if Is_Private_Type (Target)
14131 and then Present (Underlying_Type (Target))
14132 then
14133 Target := Underlying_Type (Target);
14134 end if;
14135
0924014e 14136 -- Source may be unconstrained array, but not target, except in relaxed
14137 -- semantics mode.
d6f39728 14138
0924014e 14139 if Is_Array_Type (Target)
14140 and then not Is_Constrained (Target)
14141 and then not Relaxed_RM_Semantics
14142 then
d6f39728 14143 Error_Msg_N
14144 ("unchecked conversion to unconstrained array not allowed", N);
14145 return;
14146 end if;
14147
fbc67f84 14148 -- Warn if conversion between two different convention pointers
14149
14150 if Is_Access_Type (Target)
14151 and then Is_Access_Type (Source)
14152 and then Convention (Target) /= Convention (Source)
14153 and then Warn_On_Unchecked_Conversion
14154 then
74c7ae52 14155 -- Give warnings for subprogram pointers only on most targets
fdd294d1 14156
14157 if Is_Access_Subprogram_Type (Target)
14158 or else Is_Access_Subprogram_Type (Source)
fdd294d1 14159 then
14160 Error_Msg_N
cb97ae5c 14161 ("?z?conversion between pointers with different conventions!",
1e3532e7 14162 N);
fdd294d1 14163 end if;
fbc67f84 14164 end if;
14165
3062c401 14166 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
14167 -- warning when compiling GNAT-related sources.
14168
14169 if Warn_On_Unchecked_Conversion
14170 and then not In_Predefined_Unit (N)
14171 and then RTU_Loaded (Ada_Calendar)
f02a9a9a 14172 and then (Chars (Source) = Name_Time
14173 or else
14174 Chars (Target) = Name_Time)
3062c401 14175 then
14176 -- If Ada.Calendar is loaded and the name of one of the operands is
14177 -- Time, there is a good chance that this is Ada.Calendar.Time.
14178
14179 declare
f02a9a9a 14180 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
3062c401 14181 begin
14182 pragma Assert (Present (Calendar_Time));
14183
b9e61b2a 14184 if Source = Calendar_Time or else Target = Calendar_Time then
3062c401 14185 Error_Msg_N
f02a9a9a 14186 ("?z?representation of 'Time values may change between "
14187 & "'G'N'A'T versions", N);
3062c401 14188 end if;
14189 end;
14190 end if;
14191
fdd294d1 14192 -- Make entry in unchecked conversion table for later processing by
14193 -- Validate_Unchecked_Conversions, which will check sizes and alignments
3ff5e35d 14194 -- (using values set by the back end where possible). This is only done
fdd294d1 14195 -- if the appropriate warning is active.
d6f39728 14196
9dfe12ae 14197 if Warn_On_Unchecked_Conversion then
14198 Unchecked_Conversions.Append
86d32751 14199 (New_Val => UC_Entry'(Eloc => Sloc (N),
14200 Source => Source,
14201 Target => Target,
14202 Act_Unit => Act_Unit));
9dfe12ae 14203
f9906591 14204 -- If both sizes are known statically now, then back-end annotation
9dfe12ae 14205 -- is not required to do a proper check but if either size is not
14206 -- known statically, then we need the annotation.
14207
14208 if Known_Static_RM_Size (Source)
1e3532e7 14209 and then
14210 Known_Static_RM_Size (Target)
9dfe12ae 14211 then
14212 null;
14213 else
14214 Back_Annotate_Rep_Info := True;
14215 end if;
14216 end if;
d6f39728 14217
fdd294d1 14218 -- If unchecked conversion to access type, and access type is declared
95deda50 14219 -- in the same unit as the unchecked conversion, then set the flag
14220 -- No_Strict_Aliasing (no strict aliasing is implicit here)
28ed91d4 14221
14222 if Is_Access_Type (Target) and then
14223 In_Same_Source_Unit (Target, N)
14224 then
14225 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14226 end if;
3d875462 14227
95deda50 14228 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14229 -- the back end needs to perform special validation checks.
3d875462 14230
95deda50 14231 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14232 -- have full expansion and the back end is called ???
3d875462 14233
14234 Vnode :=
14235 Make_Validate_Unchecked_Conversion (Sloc (N));
14236 Set_Source_Type (Vnode, Source);
14237 Set_Target_Type (Vnode, Target);
14238
fdd294d1 14239 -- If the unchecked conversion node is in a list, just insert before it.
14240 -- If not we have some strange case, not worth bothering about.
3d875462 14241
14242 if Is_List_Member (N) then
d6f39728 14243 Insert_After (N, Vnode);
14244 end if;
14245 end Validate_Unchecked_Conversion;
14246
14247 ------------------------------------
14248 -- Validate_Unchecked_Conversions --
14249 ------------------------------------
14250
14251 procedure Validate_Unchecked_Conversions is
14252 begin
14253 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14254 declare
14255 T : UC_Entry renames Unchecked_Conversions.Table (N);
14256
e13b1635 14257 Act_Unit : constant Entity_Id := T.Act_Unit;
86d32751 14258 Eloc : constant Source_Ptr := T.Eloc;
14259 Source : constant Entity_Id := T.Source;
14260 Target : constant Entity_Id := T.Target;
d6f39728 14261
44705307 14262 Source_Siz : Uint;
14263 Target_Siz : Uint;
d6f39728 14264
14265 begin
86d32751 14266 -- Skip if function marked as warnings off
14267
14268 if Warnings_Off (Act_Unit) then
14269 goto Continue;
14270 end if;
14271
fdd294d1 14272 -- This validation check, which warns if we have unequal sizes for
14273 -- unchecked conversion, and thus potentially implementation
d6f39728 14274 -- dependent semantics, is one of the few occasions on which we
fdd294d1 14275 -- use the official RM size instead of Esize. See description in
14276 -- Einfo "Handling of Type'Size Values" for details.
d6f39728 14277
f15731c4 14278 if Serious_Errors_Detected = 0
d6f39728 14279 and then Known_Static_RM_Size (Source)
14280 and then Known_Static_RM_Size (Target)
f25f4252 14281
14282 -- Don't do the check if warnings off for either type, note the
14283 -- deliberate use of OR here instead of OR ELSE to get the flag
14284 -- Warnings_Off_Used set for both types if appropriate.
14285
14286 and then not (Has_Warnings_Off (Source)
14287 or
14288 Has_Warnings_Off (Target))
d6f39728 14289 then
14290 Source_Siz := RM_Size (Source);
14291 Target_Siz := RM_Size (Target);
14292
14293 if Source_Siz /= Target_Siz then
299480f9 14294 Error_Msg
cb97ae5c 14295 ("?z?types for unchecked conversion have different sizes!",
299480f9 14296 Eloc);
d6f39728 14297
14298 if All_Errors_Mode then
14299 Error_Msg_Name_1 := Chars (Source);
14300 Error_Msg_Uint_1 := Source_Siz;
14301 Error_Msg_Name_2 := Chars (Target);
14302 Error_Msg_Uint_2 := Target_Siz;
cb97ae5c 14303 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
d6f39728 14304
14305 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14306
14307 if Is_Discrete_Type (Source)
b9e61b2a 14308 and then
14309 Is_Discrete_Type (Target)
d6f39728 14310 then
14311 if Source_Siz > Target_Siz then
299480f9 14312 Error_Msg
cb97ae5c 14313 ("\?z?^ high order bits of source will "
1e3532e7 14314 & "be ignored!", Eloc);
d6f39728 14315
9dfe12ae 14316 elsif Is_Unsigned_Type (Source) then
299480f9 14317 Error_Msg
cb97ae5c 14318 ("\?z?source will be extended with ^ high order "
1581f2d7 14319 & "zero bits!", Eloc);
d6f39728 14320
14321 else
299480f9 14322 Error_Msg
cb97ae5c 14323 ("\?z?source will be extended with ^ high order "
1e3532e7 14324 & "sign bits!", Eloc);
d6f39728 14325 end if;
14326
14327 elsif Source_Siz < Target_Siz then
14328 if Is_Discrete_Type (Target) then
14329 if Bytes_Big_Endian then
299480f9 14330 Error_Msg
cb97ae5c 14331 ("\?z?target value will include ^ undefined "
1e3532e7 14332 & "low order bits!", Eloc);
d6f39728 14333 else
299480f9 14334 Error_Msg
cb97ae5c 14335 ("\?z?target value will include ^ undefined "
1e3532e7 14336 & "high order bits!", Eloc);
d6f39728 14337 end if;
14338
14339 else
299480f9 14340 Error_Msg
cb97ae5c 14341 ("\?z?^ trailing bits of target value will be "
1e3532e7 14342 & "undefined!", Eloc);
d6f39728 14343 end if;
14344
14345 else pragma Assert (Source_Siz > Target_Siz);
0388e54e 14346 if Is_Discrete_Type (Source) then
14347 if Bytes_Big_Endian then
14348 Error_Msg
14349 ("\?z?^ low order bits of source will be "
14350 & "ignored!", Eloc);
14351 else
14352 Error_Msg
14353 ("\?z?^ high order bits of source will be "
14354 & "ignored!", Eloc);
14355 end if;
14356
14357 else
14358 Error_Msg
14359 ("\?z?^ trailing bits of source will be "
14360 & "ignored!", Eloc);
14361 end if;
d6f39728 14362 end if;
14363 end if;
d6f39728 14364 end if;
14365 end if;
14366
14367 -- If both types are access types, we need to check the alignment.
14368 -- If the alignment of both is specified, we can do it here.
14369
f15731c4 14370 if Serious_Errors_Detected = 0
2a10e737 14371 and then Is_Access_Type (Source)
14372 and then Is_Access_Type (Target)
d6f39728 14373 and then Target_Strict_Alignment
14374 and then Present (Designated_Type (Source))
14375 and then Present (Designated_Type (Target))
14376 then
14377 declare
14378 D_Source : constant Entity_Id := Designated_Type (Source);
14379 D_Target : constant Entity_Id := Designated_Type (Target);
14380
14381 begin
14382 if Known_Alignment (D_Source)
b9e61b2a 14383 and then
14384 Known_Alignment (D_Target)
d6f39728 14385 then
14386 declare
14387 Source_Align : constant Uint := Alignment (D_Source);
14388 Target_Align : constant Uint := Alignment (D_Target);
14389
14390 begin
14391 if Source_Align < Target_Align
14392 and then not Is_Tagged_Type (D_Source)
f25f4252 14393
14394 -- Suppress warning if warnings suppressed on either
14395 -- type or either designated type. Note the use of
14396 -- OR here instead of OR ELSE. That is intentional,
14397 -- we would like to set flag Warnings_Off_Used in
14398 -- all types for which warnings are suppressed.
14399
14400 and then not (Has_Warnings_Off (D_Source)
14401 or
14402 Has_Warnings_Off (D_Target)
14403 or
14404 Has_Warnings_Off (Source)
14405 or
14406 Has_Warnings_Off (Target))
d6f39728 14407 then
d6f39728 14408 Error_Msg_Uint_1 := Target_Align;
14409 Error_Msg_Uint_2 := Source_Align;
299480f9 14410 Error_Msg_Node_1 := D_Target;
d6f39728 14411 Error_Msg_Node_2 := D_Source;
299480f9 14412 Error_Msg
cb97ae5c 14413 ("?z?alignment of & (^) is stricter than "
1e3532e7 14414 & "alignment of & (^)!", Eloc);
f25f4252 14415 Error_Msg
cb97ae5c 14416 ("\?z?resulting access value may have invalid "
1e3532e7 14417 & "alignment!", Eloc);
d6f39728 14418 end if;
14419 end;
14420 end if;
14421 end;
14422 end if;
14423 end;
86d32751 14424
14425 <<Continue>>
14426 null;
d6f39728 14427 end loop;
14428 end Validate_Unchecked_Conversions;
14429
d6f39728 14430end Sem_Ch13;