]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch13.adb
2017-09-06 Eric Botcazou <ebotcazou@adacore.com>
[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
3061ffde 206 procedure Resolve_Iterable_Operation
207 (N : Node_Id;
208 Cursor : Entity_Id;
209 Typ : Entity_Id;
210 Nam : Name_Id);
211 -- If the name of a primitive operation for an Iterable aspect is
212 -- overloaded, resolve according to required signature.
213
b77e4501 214 procedure Set_Biased
215 (E : Entity_Id;
216 N : Node_Id;
217 Msg : String;
218 Biased : Boolean := True);
219 -- If Biased is True, sets Has_Biased_Representation flag for E, and
220 -- outputs a warning message at node N if Warn_On_Biased_Representation is
221 -- is True. This warning inserts the string Msg to describe the construct
222 -- causing biasing.
223
76a6b7c7 224 ---------------------------------------------------
225 -- Table for Validate_Compile_Time_Warning_Error --
226 ---------------------------------------------------
227
228 -- The following table collects pragmas Compile_Time_Error and Compile_
229 -- Time_Warning for validation. Entries are made by calls to subprogram
230 -- Validate_Compile_Time_Warning_Error, and the call to the procedure
231 -- Validate_Compile_Time_Warning_Errors does the actual error checking
232 -- and posting of warning and error messages. The reason for this delayed
233 -- processing is to take advantage of back-annotations of attributes size
234 -- and alignment values performed by the back end.
235
236 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
237 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
238 -- already have modified all Sloc values if the -gnatD option is set.
239
240 type CTWE_Entry is record
241 Eloc : Source_Ptr;
242 -- Source location used in warnings and error messages
243
244 Prag : Node_Id;
245 -- Pragma Compile_Time_Error or Compile_Time_Warning
246
247 Scope : Node_Id;
248 -- The scope which encloses the pragma
249 end record;
250
251 package Compile_Time_Warnings_Errors is new Table.Table (
252 Table_Component_Type => CTWE_Entry,
253 Table_Index_Type => Int,
254 Table_Low_Bound => 1,
255 Table_Initial => 50,
256 Table_Increment => 200,
257 Table_Name => "Compile_Time_Warnings_Errors");
258
d6f39728 259 ----------------------------------------------
260 -- Table for Validate_Unchecked_Conversions --
261 ----------------------------------------------
262
263 -- The following table collects unchecked conversions for validation.
95deda50 264 -- Entries are made by Validate_Unchecked_Conversion and then the call
265 -- to Validate_Unchecked_Conversions does the actual error checking and
266 -- posting of warnings. The reason for this delayed processing is to take
267 -- advantage of back-annotations of size and alignment values performed by
268 -- the back end.
d6f39728 269
95deda50 270 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
271 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
272 -- already have modified all Sloc values if the -gnatD option is set.
299480f9 273
d6f39728 274 type UC_Entry is record
86d32751 275 Eloc : Source_Ptr; -- node used for posting warnings
276 Source : Entity_Id; -- source type for unchecked conversion
277 Target : Entity_Id; -- target type for unchecked conversion
278 Act_Unit : Entity_Id; -- actual function instantiated
d6f39728 279 end record;
280
281 package Unchecked_Conversions is new Table.Table (
282 Table_Component_Type => UC_Entry,
283 Table_Index_Type => Int,
284 Table_Low_Bound => 1,
285 Table_Initial => 50,
286 Table_Increment => 200,
287 Table_Name => "Unchecked_Conversions");
288
83f8f0a6 289 ----------------------------------------
290 -- Table for Validate_Address_Clauses --
291 ----------------------------------------
292
293 -- If an address clause has the form
294
295 -- for X'Address use Expr
296
514a5555 297 -- where Expr has a value known at compile time or is of the form Y'Address
298 -- or recursively is a reference to a constant initialized with either of
299 -- these forms, and the value of Expr is not a multiple of X's alignment,
300 -- or if Y has a smaller alignment than X, then that merits a warning about
95deda50 301 -- possible bad alignment. The following table collects address clauses of
302 -- this kind. We put these in a table so that they can be checked after the
303 -- back end has completed annotation of the alignments of objects, since we
304 -- can catch more cases that way.
83f8f0a6 305
306 type Address_Clause_Check_Record is record
307 N : Node_Id;
308 -- The address clause
309
310 X : Entity_Id;
514a5555 311 -- The entity of the object subject to the address clause
312
313 A : Uint;
314 -- The value of the address in the first case
83f8f0a6 315
316 Y : Entity_Id;
514a5555 317 -- The entity of the object being overlaid in the second case
d6da7448 318
319 Off : Boolean;
514a5555 320 -- Whether the address is offset within Y in the second case
83f8f0a6 321 end record;
322
323 package Address_Clause_Checks is new Table.Table (
324 Table_Component_Type => Address_Clause_Check_Record,
325 Table_Index_Type => Int,
326 Table_Low_Bound => 1,
327 Table_Initial => 20,
328 Table_Increment => 200,
329 Table_Name => "Address_Clause_Checks");
330
59ac57b5 331 -----------------------------------------
332 -- Adjust_Record_For_Reverse_Bit_Order --
333 -----------------------------------------
334
335 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
d95b8c89 336 Max_Machine_Scalar_Size : constant Uint :=
337 UI_From_Int
338 (Standard_Long_Long_Integer_Size);
339 -- We use this as the maximum machine scalar size
59ac57b5 340
7748ccb2 341 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
342
343 CC : Node_Id;
344 Comp : Node_Id;
d95b8c89 345 Num_CC : Natural;
6797073f 346
d95b8c89 347 begin
2d9fff4f 348 -- Processing here used to depend on Ada version: the behavior was
d95b8c89 349 -- changed by AI95-0133. However this AI is a Binding interpretation,
2d9fff4f 350 -- so we now implement it even in Ada 95 mode. The original behavior
d95b8c89 351 -- from unamended Ada 95 is still available for compatibility under
352 -- debugging switch -gnatd.
353
354 if Ada_Version < Ada_2005 and then Debug_Flag_Dot_P then
355 Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R);
356 return;
357 end if;
358
359 -- For Ada 2005, we do machine scalar processing, as fully described In
360 -- AI-133. This involves gathering all components which start at the
361 -- same byte offset and processing them together. Same approach is still
362 -- valid in later versions including Ada 2012.
6797073f 363
7748ccb2 364 -- This first loop through components does two things. First it deals
365 -- with the case of components with component clauses whose length is
366 -- greater than the maximum machine scalar size (either accepting them
367 -- or rejecting as needed). Second, it counts the number of components
368 -- with component clauses whose length does not exceed this maximum for
369 -- later processing.
6797073f 370
d95b8c89 371 Num_CC := 0;
372 Comp := First_Component_Or_Discriminant (R);
373 while Present (Comp) loop
374 CC := Component_Clause (Comp);
6797073f 375
d95b8c89 376 if Present (CC) then
377 declare
378 Fbit : constant Uint := Static_Integer (First_Bit (CC));
379 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
6797073f 380
d95b8c89 381 begin
382 -- Case of component with last bit >= max machine scalar
6797073f 383
d95b8c89 384 if Lbit >= Max_Machine_Scalar_Size then
59ac57b5 385
7748ccb2 386 -- This is allowed only if first bit is zero, and last bit
387 -- + 1 is a multiple of storage unit size.
59ac57b5 388
d95b8c89 389 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
59ac57b5 390
d95b8c89 391 -- This is the case to give a warning if enabled
59ac57b5 392
d95b8c89 393 if Warn_On_Reverse_Bit_Order then
6797073f 394 Error_Msg_N
7a41db5b 395 ("info: multi-byte field specified with "
d95b8c89 396 & "non-standard Bit_Order?V?", CC);
31486bc0 397
6797073f 398 if Bytes_Big_Endian then
31486bc0 399 Error_Msg_N
7a41db5b 400 ("\bytes are not reversed "
d95b8c89 401 & "(component is big-endian)?V?", CC);
31486bc0 402 else
403 Error_Msg_N
7a41db5b 404 ("\bytes are not reversed "
d95b8c89 405 & "(component is little-endian)?V?", CC);
31486bc0 406 end if;
d95b8c89 407 end if;
59ac57b5 408
d95b8c89 409 -- Give error message for RM 13.5.1(10) violation
410
411 else
412 Error_Msg_FE
413 ("machine scalar rules not followed for&",
414 First_Bit (CC), Comp);
415
416 Error_Msg_Uint_1 := Lbit + 1;
417 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
418 Error_Msg_F
7748ccb2 419 ("\last bit + 1 (^) exceeds maximum machine scalar "
420 & "size (^)", First_Bit (CC));
d95b8c89 421
422 if (Lbit + 1) mod SSU /= 0 then
423 Error_Msg_Uint_1 := SSU;
424 Error_Msg_F
425 ("\and is not a multiple of Storage_Unit (^) "
7748ccb2 426 & "(RM 13.5.1(10))", First_Bit (CC));
59ac57b5 427
67278d60 428 else
d95b8c89 429 Error_Msg_Uint_1 := Fbit;
430 Error_Msg_F
431 ("\and first bit (^) is non-zero "
7748ccb2 432 & "(RM 13.4.1(10))", First_Bit (CC));
6797073f 433 end if;
d95b8c89 434 end if;
59ac57b5 435
7748ccb2 436 -- OK case of machine scalar related component clause. For now,
437 -- just count them.
59ac57b5 438
d95b8c89 439 else
440 Num_CC := Num_CC + 1;
441 end if;
442 end;
443 end if;
59ac57b5 444
d95b8c89 445 Next_Component_Or_Discriminant (Comp);
446 end loop;
59ac57b5 447
7748ccb2 448 -- We need to sort the component clauses on the basis of the Position
449 -- values in the clause, so we can group clauses with the same Position
450 -- together to determine the relevant machine scalar size.
bfa5a9d9 451
d95b8c89 452 Sort_CC : declare
453 Comps : array (0 .. Num_CC) of Entity_Id;
7748ccb2 454 -- Array to collect component and discriminant entities. The data
455 -- starts at index 1, the 0'th entry is for the sort routine.
59ac57b5 456
d95b8c89 457 function CP_Lt (Op1, Op2 : Natural) return Boolean;
458 -- Compare routine for Sort
59ac57b5 459
d95b8c89 460 procedure CP_Move (From : Natural; To : Natural);
461 -- Move routine for Sort
59ac57b5 462
d95b8c89 463 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
59ac57b5 464
7748ccb2 465 MaxL : Uint;
466 -- Maximum last bit value of any component in this set
467
468 MSS : Uint;
469 -- Corresponding machine scalar size
470
d95b8c89 471 Start : Natural;
472 Stop : Natural;
473 -- Start and stop positions in the component list of the set of
474 -- components with the same starting position (that constitute
475 -- components in a single machine scalar).
59ac57b5 476
d95b8c89 477 -----------
478 -- CP_Lt --
479 -----------
6797073f 480
d95b8c89 481 function CP_Lt (Op1, Op2 : Natural) return Boolean is
482 begin
7748ccb2 483 return
484 Position (Component_Clause (Comps (Op1))) <
d95b8c89 485 Position (Component_Clause (Comps (Op2)));
486 end CP_Lt;
59ac57b5 487
d95b8c89 488 -------------
489 -- CP_Move --
490 -------------
59ac57b5 491
d95b8c89 492 procedure CP_Move (From : Natural; To : Natural) is
6797073f 493 begin
d95b8c89 494 Comps (To) := Comps (From);
495 end CP_Move;
496
497 -- Start of processing for Sort_CC
498
499 begin
500 -- Collect the machine scalar relevant component clauses
67278d60 501
d95b8c89 502 Num_CC := 0;
503 Comp := First_Component_Or_Discriminant (R);
504 while Present (Comp) loop
505 declare
506 CC : constant Node_Id := Component_Clause (Comp);
67278d60 507
d95b8c89 508 begin
7748ccb2 509 -- Collect only component clauses whose last bit is less than
510 -- machine scalar size. Any component clause whose last bit
511 -- exceeds this value does not take part in machine scalar
512 -- layout considerations. The test for Error_Posted makes sure
513 -- we exclude component clauses for which we already posted an
514 -- error.
d95b8c89 515
516 if Present (CC)
517 and then not Error_Posted (Last_Bit (CC))
518 and then Static_Integer (Last_Bit (CC)) <
519 Max_Machine_Scalar_Size
520 then
521 Num_CC := Num_CC + 1;
522 Comps (Num_CC) := Comp;
523 end if;
524 end;
67278d60 525
d95b8c89 526 Next_Component_Or_Discriminant (Comp);
527 end loop;
67278d60 528
d95b8c89 529 -- Sort by ascending position number
530
531 Sorting.Sort (Num_CC);
532
7748ccb2 533 -- We now have all the components whose size does not exceed the max
534 -- machine scalar value, sorted by starting position. In this loop we
535 -- gather groups of clauses starting at the same position, to process
536 -- them in accordance with AI-133.
d95b8c89 537
538 Stop := 0;
539 while Stop < Num_CC loop
540 Start := Stop + 1;
541 Stop := Start;
542 MaxL :=
543 Static_Integer
544 (Last_Bit (Component_Clause (Comps (Start))));
545 while Stop < Num_CC loop
546 if Static_Integer
547 (Position (Component_Clause (Comps (Stop + 1)))) =
548 Static_Integer
549 (Position (Component_Clause (Comps (Stop))))
550 then
551 Stop := Stop + 1;
552 MaxL :=
553 UI_Max
554 (MaxL,
555 Static_Integer
556 (Last_Bit
557 (Component_Clause (Comps (Stop)))));
558 else
559 exit;
560 end if;
561 end loop;
67278d60 562
7748ccb2 563 -- Now we have a group of component clauses from Start to Stop
564 -- whose positions are identical, and MaxL is the maximum last
565 -- bit value of any of these components.
d95b8c89 566
7748ccb2 567 -- We need to determine the corresponding machine scalar size.
568 -- This loop assumes that machine scalar sizes are even, and that
569 -- each possible machine scalar has twice as many bits as the next
570 -- smaller one.
d95b8c89 571
572 MSS := Max_Machine_Scalar_Size;
573 while MSS mod 2 = 0
574 and then (MSS / 2) >= SSU
575 and then (MSS / 2) > MaxL
576 loop
577 MSS := MSS / 2;
578 end loop;
67278d60 579
7748ccb2 580 -- Here is where we fix up the Component_Bit_Offset value to
581 -- account for the reverse bit order. Some examples of what needs
582 -- to be done for the case of a machine scalar size of 8 are:
67278d60 583
d95b8c89 584 -- First_Bit .. Last_Bit Component_Bit_Offset
585 -- old new old new
67278d60 586
d95b8c89 587 -- 0 .. 0 7 .. 7 0 7
588 -- 0 .. 1 6 .. 7 0 6
589 -- 0 .. 2 5 .. 7 0 5
590 -- 0 .. 7 0 .. 7 0 4
b38e4131 591
d95b8c89 592 -- 1 .. 1 6 .. 6 1 6
593 -- 1 .. 4 3 .. 6 1 3
594 -- 4 .. 7 0 .. 3 4 0
67278d60 595
7748ccb2 596 -- The rule is that the first bit is obtained by subtracting the
597 -- old ending bit from machine scalar size - 1.
67278d60 598
d95b8c89 599 for C in Start .. Stop loop
600 declare
601 Comp : constant Entity_Id := Comps (C);
602 CC : constant Node_Id := Component_Clause (Comp);
59ac57b5 603
d95b8c89 604 LB : constant Uint := Static_Integer (Last_Bit (CC));
605 NFB : constant Uint := MSS - Uint_1 - LB;
606 NLB : constant Uint := NFB + Esize (Comp) - 1;
607 Pos : constant Uint := Static_Integer (Position (CC));
59ac57b5 608
d95b8c89 609 begin
610 if Warn_On_Reverse_Bit_Order then
611 Error_Msg_Uint_1 := MSS;
612 Error_Msg_N
7748ccb2 613 ("info: reverse bit order in machine scalar of "
614 & "length^?V?", First_Bit (CC));
d95b8c89 615 Error_Msg_Uint_1 := NFB;
616 Error_Msg_Uint_2 := NLB;
617
618 if Bytes_Big_Endian then
619 Error_Msg_NE
7748ccb2 620 ("\big-endian range for component & is ^ .. ^?V?",
621 First_Bit (CC), Comp);
6797073f 622 else
d95b8c89 623 Error_Msg_NE
7748ccb2 624 ("\little-endian range for component & is ^ .. ^?V?",
625 First_Bit (CC), Comp);
6797073f 626 end if;
d95b8c89 627 end if;
59ac57b5 628
d95b8c89 629 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
630 Set_Normalized_First_Bit (Comp, NFB mod SSU);
631 end;
6797073f 632 end loop;
d95b8c89 633 end loop;
634 end Sort_CC;
635 end Adjust_Record_For_Reverse_Bit_Order;
59ac57b5 636
d95b8c89 637 ------------------------------------------------
638 -- Adjust_Record_For_Reverse_Bit_Order_Ada_95 --
639 ------------------------------------------------
59ac57b5 640
d95b8c89 641 procedure Adjust_Record_For_Reverse_Bit_Order_Ada_95 (R : Entity_Id) is
d95b8c89 642 CC : Node_Id;
7748ccb2 643 Comp : Node_Id;
59ac57b5 644
d95b8c89 645 begin
646 -- For Ada 95, we just renumber bits within a storage unit. We do the
647 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
648 -- Ada 83, and are free to add this extension.
59ac57b5 649
d95b8c89 650 Comp := First_Component_Or_Discriminant (R);
651 while Present (Comp) loop
652 CC := Component_Clause (Comp);
59ac57b5 653
d95b8c89 654 -- If component clause is present, then deal with the non-default
655 -- bit order case for Ada 95 mode.
59ac57b5 656
d95b8c89 657 -- We only do this processing for the base type, and in fact that
658 -- is important, since otherwise if there are record subtypes, we
659 -- could reverse the bits once for each subtype, which is wrong.
59ac57b5 660
d95b8c89 661 if Present (CC) and then Ekind (R) = E_Record_Type then
662 declare
663 CFB : constant Uint := Component_Bit_Offset (Comp);
664 CSZ : constant Uint := Esize (Comp);
665 CLC : constant Node_Id := Component_Clause (Comp);
666 Pos : constant Node_Id := Position (CLC);
667 FB : constant Node_Id := First_Bit (CLC);
59ac57b5 668
d95b8c89 669 Storage_Unit_Offset : constant Uint :=
670 CFB / System_Storage_Unit;
67278d60 671
d95b8c89 672 Start_Bit : constant Uint :=
673 CFB mod System_Storage_Unit;
67278d60 674
d95b8c89 675 begin
676 -- Cases where field goes over storage unit boundary
67278d60 677
d95b8c89 678 if Start_Bit + CSZ > System_Storage_Unit then
67278d60 679
d95b8c89 680 -- Allow multi-byte field but generate warning
67278d60 681
d95b8c89 682 if Start_Bit mod System_Storage_Unit = 0
683 and then CSZ mod System_Storage_Unit = 0
684 then
685 Error_Msg_N
7748ccb2 686 ("info: multi-byte field specified with non-standard "
687 & "Bit_Order?V?", CLC);
59ac57b5 688
d95b8c89 689 if Bytes_Big_Endian then
690 Error_Msg_N
691 ("\bytes are not reversed "
692 & "(component is big-endian)?V?", CLC);
6797073f 693 else
d95b8c89 694 Error_Msg_N
695 ("\bytes are not reversed "
696 & "(component is little-endian)?V?", CLC);
6797073f 697 end if;
67278d60 698
d95b8c89 699 -- Do not allow non-contiguous field
6797073f 700
d95b8c89 701 else
702 Error_Msg_N
7748ccb2 703 ("attempt to specify non-contiguous field not "
704 & "permitted", CLC);
d95b8c89 705 Error_Msg_N
7748ccb2 706 ("\caused by non-standard Bit_Order specified in "
707 & "legacy Ada 95 mode", CLC);
d95b8c89 708 end if;
6797073f 709
d95b8c89 710 -- Case where field fits in one storage unit
711
712 else
713 -- Give warning if suspicious component clause
714
715 if Intval (FB) >= System_Storage_Unit
716 and then Warn_On_Reverse_Bit_Order
717 then
718 Error_Msg_N
7748ccb2 719 ("info: Bit_Order clause does not affect byte "
720 & "ordering?V?", Pos);
d95b8c89 721 Error_Msg_Uint_1 :=
722 Intval (Pos) + Intval (FB) /
723 System_Storage_Unit;
724 Error_Msg_N
7748ccb2 725 ("info: position normalized to ^ before bit order "
726 & "interpreted?V?", Pos);
d95b8c89 727 end if;
67278d60 728
6797073f 729 -- Here is where we fix up the Component_Bit_Offset value
730 -- to account for the reverse bit order. Some examples of
d95b8c89 731 -- what needs to be done are:
67278d60 732
6797073f 733 -- First_Bit .. Last_Bit Component_Bit_Offset
734 -- old new old new
67278d60 735
6797073f 736 -- 0 .. 0 7 .. 7 0 7
737 -- 0 .. 1 6 .. 7 0 6
738 -- 0 .. 2 5 .. 7 0 5
739 -- 0 .. 7 0 .. 7 0 4
67278d60 740
6797073f 741 -- 1 .. 1 6 .. 6 1 6
742 -- 1 .. 4 3 .. 6 1 3
743 -- 4 .. 7 0 .. 3 4 0
67278d60 744
d95b8c89 745 -- The rule is that the first bit is is obtained by
746 -- subtracting the old ending bit from storage_unit - 1.
67278d60 747
7748ccb2 748 Set_Component_Bit_Offset (Comp,
749 (Storage_Unit_Offset * System_Storage_Unit) +
750 (System_Storage_Unit - 1) -
751 (Start_Bit + CSZ - 1));
b9e61b2a 752
7748ccb2 753 Set_Normalized_First_Bit (Comp,
754 Component_Bit_Offset (Comp) mod System_Storage_Unit);
d95b8c89 755 end if;
756 end;
757 end if;
67278d60 758
d95b8c89 759 Next_Component_Or_Discriminant (Comp);
760 end loop;
761 end Adjust_Record_For_Reverse_Bit_Order_Ada_95;
59ac57b5 762
1d366b32 763 -------------------------------------
764 -- Alignment_Check_For_Size_Change --
765 -------------------------------------
d6f39728 766
1d366b32 767 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
d6f39728 768 begin
769 -- If the alignment is known, and not set by a rep clause, and is
770 -- inconsistent with the size being set, then reset it to unknown,
771 -- we assume in this case that the size overrides the inherited
772 -- alignment, and that the alignment must be recomputed.
773
774 if Known_Alignment (Typ)
775 and then not Has_Alignment_Clause (Typ)
1d366b32 776 and then Size mod (Alignment (Typ) * SSU) /= 0
d6f39728 777 then
778 Init_Alignment (Typ);
779 end if;
1d366b32 780 end Alignment_Check_For_Size_Change;
d6f39728 781
06ef5f86 782 -------------------------------------
783 -- Analyze_Aspects_At_Freeze_Point --
784 -------------------------------------
785
786 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
06ef5f86 787 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
788 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
789 -- the aspect specification node ASN.
790
37c6e44c 791 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
792 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
793 -- a derived type can inherit aspects from its parent which have been
794 -- specified at the time of the derivation using an aspect, as in:
795 --
796 -- type A is range 1 .. 10
797 -- with Size => Not_Defined_Yet;
798 -- ..
799 -- type B is new A;
800 -- ..
801 -- Not_Defined_Yet : constant := 64;
802 --
803 -- In this example, the Size of A is considered to be specified prior
804 -- to the derivation, and thus inherited, even though the value is not
805 -- known at the time of derivation. To deal with this, we use two entity
806 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
807 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
808 -- the derived type (B here). If this flag is set when the derived type
809 -- is frozen, then this procedure is called to ensure proper inheritance
b21edad9 810 -- of all delayed aspects from the parent type. The derived type is E,
37c6e44c 811 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
812 -- aspect specification node in the Rep_Item chain for the parent type.
813
06ef5f86 814 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
815 -- Given an aspect specification node ASN whose expression is an
816 -- optional Boolean, this routines creates the corresponding pragma
817 -- at the freezing point.
818
819 ----------------------------------
820 -- Analyze_Aspect_Default_Value --
821 ----------------------------------
822
823 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
ee2b7923 824 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
06ef5f86 825 Ent : constant Entity_Id := Entity (ASN);
826 Expr : constant Node_Id := Expression (ASN);
827 Id : constant Node_Id := Identifier (ASN);
828
829 begin
830 Error_Msg_Name_1 := Chars (Id);
831
832 if not Is_Type (Ent) then
833 Error_Msg_N ("aspect% can only apply to a type", Id);
834 return;
835
836 elsif not Is_First_Subtype (Ent) then
837 Error_Msg_N ("aspect% cannot apply to subtype", Id);
838 return;
839
840 elsif A_Id = Aspect_Default_Value
841 and then not Is_Scalar_Type (Ent)
842 then
843 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
844 return;
845
846 elsif A_Id = Aspect_Default_Component_Value then
847 if not Is_Array_Type (Ent) then
848 Error_Msg_N ("aspect% can only be applied to array type", Id);
849 return;
850
851 elsif not Is_Scalar_Type (Component_Type (Ent)) then
852 Error_Msg_N ("aspect% requires scalar components", Id);
853 return;
854 end if;
855 end if;
856
857 Set_Has_Default_Aspect (Base_Type (Ent));
858
859 if Is_Scalar_Type (Ent) then
9f36e3fb 860 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
06ef5f86 861 else
f3d70f08 862 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
06ef5f86 863 end if;
864 end Analyze_Aspect_Default_Value;
865
37c6e44c 866 ---------------------------------
867 -- Inherit_Delayed_Rep_Aspects --
868 ---------------------------------
869
870 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
ee2b7923 871 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
872 P : constant Entity_Id := Entity (ASN);
37c6e44c 873 -- Entithy for parent type
874
875 N : Node_Id;
876 -- Item from Rep_Item chain
877
878 A : Aspect_Id;
879
880 begin
881 -- Loop through delayed aspects for the parent type
882
883 N := ASN;
884 while Present (N) loop
885 if Nkind (N) = N_Aspect_Specification then
886 exit when Entity (N) /= P;
887
888 if Is_Delayed_Aspect (N) then
889 A := Get_Aspect_Id (Chars (Identifier (N)));
890
891 -- Process delayed rep aspect. For Boolean attributes it is
892 -- not possible to cancel an attribute once set (the attempt
893 -- to use an aspect with xxx => False is an error) for a
894 -- derived type. So for those cases, we do not have to check
895 -- if a clause has been given for the derived type, since it
896 -- is harmless to set it again if it is already set.
897
898 case A is
899
900 -- Alignment
901
902 when Aspect_Alignment =>
903 if not Has_Alignment_Clause (E) then
904 Set_Alignment (E, Alignment (P));
905 end if;
906
907 -- Atomic
908
909 when Aspect_Atomic =>
910 if Is_Atomic (P) then
911 Set_Is_Atomic (E);
912 end if;
913
914 -- Atomic_Components
915
916 when Aspect_Atomic_Components =>
917 if Has_Atomic_Components (P) then
918 Set_Has_Atomic_Components (Base_Type (E));
919 end if;
920
921 -- Bit_Order
922
923 when Aspect_Bit_Order =>
924 if Is_Record_Type (E)
925 and then No (Get_Attribute_Definition_Clause
926 (E, Attribute_Bit_Order))
927 and then Reverse_Bit_Order (P)
928 then
929 Set_Reverse_Bit_Order (Base_Type (E));
930 end if;
931
932 -- Component_Size
933
934 when Aspect_Component_Size =>
935 if Is_Array_Type (E)
936 and then not Has_Component_Size_Clause (E)
937 then
938 Set_Component_Size
939 (Base_Type (E), Component_Size (P));
940 end if;
941
942 -- Machine_Radix
943
944 when Aspect_Machine_Radix =>
945 if Is_Decimal_Fixed_Point_Type (E)
946 and then not Has_Machine_Radix_Clause (E)
947 then
948 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
949 end if;
950
951 -- Object_Size (also Size which also sets Object_Size)
952
99378362 953 when Aspect_Object_Size
954 | Aspect_Size
955 =>
37c6e44c 956 if not Has_Size_Clause (E)
957 and then
958 No (Get_Attribute_Definition_Clause
959 (E, Attribute_Object_Size))
960 then
961 Set_Esize (E, Esize (P));
962 end if;
963
964 -- Pack
965
966 when Aspect_Pack =>
967 if not Is_Packed (E) then
968 Set_Is_Packed (Base_Type (E));
969
970 if Is_Bit_Packed_Array (P) then
971 Set_Is_Bit_Packed_Array (Base_Type (E));
a88a5773 972 Set_Packed_Array_Impl_Type
973 (E, Packed_Array_Impl_Type (P));
37c6e44c 974 end if;
975 end if;
976
977 -- Scalar_Storage_Order
978
979 when Aspect_Scalar_Storage_Order =>
980 if (Is_Record_Type (E) or else Is_Array_Type (E))
981 and then No (Get_Attribute_Definition_Clause
e163cac8 982 (E, Attribute_Scalar_Storage_Order))
37c6e44c 983 and then Reverse_Storage_Order (P)
984 then
985 Set_Reverse_Storage_Order (Base_Type (E));
b64082f2 986
987 -- Clear default SSO indications, since the aspect
988 -- overrides the default.
989
990 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
991 Set_SSO_Set_High_By_Default (Base_Type (E), False);
37c6e44c 992 end if;
993
994 -- Small
995
996 when Aspect_Small =>
997 if Is_Fixed_Point_Type (E)
998 and then not Has_Small_Clause (E)
999 then
1000 Set_Small_Value (E, Small_Value (P));
1001 end if;
1002
1003 -- Storage_Size
1004
1005 when Aspect_Storage_Size =>
1006 if (Is_Access_Type (E) or else Is_Task_Type (E))
1007 and then not Has_Storage_Size_Clause (E)
1008 then
1009 Set_Storage_Size_Variable
1010 (Base_Type (E), Storage_Size_Variable (P));
1011 end if;
1012
1013 -- Value_Size
1014
1015 when Aspect_Value_Size =>
1016
1017 -- Value_Size is never inherited, it is either set by
1018 -- default, or it is explicitly set for the derived
1019 -- type. So nothing to do here.
1020
1021 null;
1022
1023 -- Volatile
1024
1025 when Aspect_Volatile =>
1026 if Is_Volatile (P) then
1027 Set_Is_Volatile (E);
1028 end if;
1029
2fe893b9 1030 -- Volatile_Full_Access
1031
1032 when Aspect_Volatile_Full_Access =>
4bf2acc9 1033 if Is_Volatile_Full_Access (P) then
1034 Set_Is_Volatile_Full_Access (E);
2fe893b9 1035 end if;
1036
37c6e44c 1037 -- Volatile_Components
1038
1039 when Aspect_Volatile_Components =>
1040 if Has_Volatile_Components (P) then
1041 Set_Has_Volatile_Components (Base_Type (E));
1042 end if;
1043
1044 -- That should be all the Rep Aspects
1045
1046 when others =>
1047 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1048 null;
37c6e44c 1049 end case;
1050 end if;
1051 end if;
1052
1053 N := Next_Rep_Item (N);
1054 end loop;
1055 end Inherit_Delayed_Rep_Aspects;
1056
06ef5f86 1057 -------------------------------------
1058 -- Make_Pragma_From_Boolean_Aspect --
1059 -------------------------------------
1060
1061 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1062 Ident : constant Node_Id := Identifier (ASN);
1063 A_Name : constant Name_Id := Chars (Ident);
1064 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1065 Ent : constant Entity_Id := Entity (ASN);
1066 Expr : constant Node_Id := Expression (ASN);
1067 Loc : constant Source_Ptr := Sloc (ASN);
1068
06ef5f86 1069 procedure Check_False_Aspect_For_Derived_Type;
1070 -- This procedure checks for the case of a false aspect for a derived
1071 -- type, which improperly tries to cancel an aspect inherited from
1072 -- the parent.
1073
1074 -----------------------------------------
1075 -- Check_False_Aspect_For_Derived_Type --
1076 -----------------------------------------
1077
1078 procedure Check_False_Aspect_For_Derived_Type is
1079 Par : Node_Id;
1080
1081 begin
1082 -- We are only checking derived types
1083
1084 if not Is_Derived_Type (E) then
1085 return;
1086 end if;
1087
1088 Par := Nearest_Ancestor (E);
1089
1090 case A_Id is
99378362 1091 when Aspect_Atomic
1092 | Aspect_Shared
1093 =>
06ef5f86 1094 if not Is_Atomic (Par) then
1095 return;
1096 end if;
1097
1098 when Aspect_Atomic_Components =>
1099 if not Has_Atomic_Components (Par) then
1100 return;
1101 end if;
1102
1103 when Aspect_Discard_Names =>
1104 if not Discard_Names (Par) then
1105 return;
1106 end if;
1107
1108 when Aspect_Pack =>
1109 if not Is_Packed (Par) then
1110 return;
1111 end if;
1112
1113 when Aspect_Unchecked_Union =>
1114 if not Is_Unchecked_Union (Par) then
1115 return;
1116 end if;
1117
1118 when Aspect_Volatile =>
1119 if not Is_Volatile (Par) then
1120 return;
1121 end if;
1122
1123 when Aspect_Volatile_Components =>
1124 if not Has_Volatile_Components (Par) then
1125 return;
1126 end if;
1127
2fe893b9 1128 when Aspect_Volatile_Full_Access =>
4bf2acc9 1129 if not Is_Volatile_Full_Access (Par) then
2fe893b9 1130 return;
1131 end if;
1132
06ef5f86 1133 when others =>
1134 return;
1135 end case;
1136
1137 -- Fall through means we are canceling an inherited aspect
1138
1139 Error_Msg_Name_1 := A_Name;
37c6e44c 1140 Error_Msg_NE
1141 ("derived type& inherits aspect%, cannot cancel", Expr, E);
06ef5f86 1142 end Check_False_Aspect_For_Derived_Type;
1143
ee2b7923 1144 -- Local variables
1145
1146 Prag : Node_Id;
1147
06ef5f86 1148 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1149
1150 begin
37c6e44c 1151 -- Note that we know Expr is present, because for a missing Expr
1152 -- argument, we knew it was True and did not need to delay the
1153 -- evaluation to the freeze point.
1154
06ef5f86 1155 if Is_False (Static_Boolean (Expr)) then
1156 Check_False_Aspect_For_Derived_Type;
1157
1158 else
1159 Prag :=
1160 Make_Pragma (Loc,
ee2b7923 1161 Pragma_Identifier =>
1162 Make_Identifier (Sloc (Ident), Chars (Ident)),
06ef5f86 1163 Pragma_Argument_Associations => New_List (
57cd943b 1164 Make_Pragma_Argument_Association (Sloc (Ident),
ee2b7923 1165 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
06ef5f86 1166
1167 Set_From_Aspect_Specification (Prag, True);
1168 Set_Corresponding_Aspect (Prag, ASN);
1169 Set_Aspect_Rep_Item (ASN, Prag);
1170 Set_Is_Delayed_Aspect (Prag);
1171 Set_Parent (Prag, ASN);
1172 end if;
06ef5f86 1173 end Make_Pragma_From_Boolean_Aspect;
1174
ee2b7923 1175 -- Local variables
1176
1177 A_Id : Aspect_Id;
1178 ASN : Node_Id;
1179 Ritem : Node_Id;
1180
06ef5f86 1181 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1182
1183 begin
5e67c1f8 1184 -- Must be visible in current scope, but if this is a type from a nested
1185 -- package it may be frozen from an object declaration in the enclosing
1186 -- scope, so install the package declarations to complete the analysis
1187 -- of the aspects, if any. If the package itself is frozen the type will
1188 -- have been frozen as well.
06ef5f86 1189
ace3389d 1190 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
3051730b 1191 if Is_Type (E) and then From_Nested_Package (E) then
81083222 1192 declare
1193 Pack : constant Entity_Id := Scope (E);
1194
1195 begin
1196 Push_Scope (Pack);
1197 Install_Visible_Declarations (Pack);
1198 Install_Private_Declarations (Pack);
1199 Analyze_Aspects_At_Freeze_Point (E);
1200
1201 if Is_Private_Type (E)
1202 and then Present (Full_View (E))
1203 then
1204 Analyze_Aspects_At_Freeze_Point (Full_View (E));
1205 end if;
1206
1207 End_Package_Scope (Pack);
3051730b 1208 return;
81083222 1209 end;
1210
5e67c1f8 1211 -- Aspects from other entities in different contexts are analyzed
1212 -- elsewhere.
81083222 1213
5e67c1f8 1214 else
81083222 1215 return;
1216 end if;
06ef5f86 1217 end if;
1218
1219 -- Look for aspect specification entries for this entity
1220
1221 ASN := First_Rep_Item (E);
06ef5f86 1222 while Present (ASN) loop
37c6e44c 1223 if Nkind (ASN) = N_Aspect_Specification then
1224 exit when Entity (ASN) /= E;
06ef5f86 1225
37c6e44c 1226 if Is_Delayed_Aspect (ASN) then
1227 A_Id := Get_Aspect_Id (ASN);
1228
1229 case A_Id is
e4c87fa5 1230
37c6e44c 1231 -- For aspects whose expression is an optional Boolean, make
7d6fb253 1232 -- the corresponding pragma at the freeze point.
06ef5f86 1233
99378362 1234 when Boolean_Aspects
1235 | Library_Unit_Aspects
1236 =>
ee2b7923 1237 -- Aspects Export and Import require special handling.
1238 -- Both are by definition Boolean and may benefit from
1239 -- forward references, however their expressions are
1240 -- treated as static. In addition, the syntax of their
1241 -- corresponding pragmas requires extra "pieces" which
1242 -- may also contain forward references. To account for
1243 -- all of this, the corresponding pragma is created by
1244 -- Analyze_Aspect_Export_Import, but is not analyzed as
1245 -- the complete analysis must happen now.
1246
1247 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1248 null;
1249
1250 -- Otherwise create a corresponding pragma
1251
1252 else
1253 Make_Pragma_From_Boolean_Aspect (ASN);
1254 end if;
06ef5f86 1255
37c6e44c 1256 -- Special handling for aspects that don't correspond to
1257 -- pragmas/attributes.
06ef5f86 1258
99378362 1259 when Aspect_Default_Value
1260 | Aspect_Default_Component_Value
1261 =>
81c2bc19 1262 -- Do not inherit aspect for anonymous base type of a
1263 -- scalar or array type, because they apply to the first
1264 -- subtype of the type, and will be processed when that
1265 -- first subtype is frozen.
1266
1267 if Is_Derived_Type (E)
1268 and then not Comes_From_Source (E)
1269 and then E /= First_Subtype (E)
1270 then
1271 null;
1272 else
1273 Analyze_Aspect_Default_Value (ASN);
1274 end if;
06ef5f86 1275
37c6e44c 1276 -- Ditto for iterator aspects, because the corresponding
1277 -- attributes may not have been analyzed yet.
af9fed8f 1278
99378362 1279 when Aspect_Constant_Indexing
1280 | Aspect_Default_Iterator
1281 | Aspect_Iterator_Element
1282 | Aspect_Variable_Indexing
1283 =>
7d6fb253 1284 Analyze (Expression (ASN));
af9fed8f 1285
7d6fb253 1286 if Etype (Expression (ASN)) = Any_Type then
1287 Error_Msg_NE
1288 ("\aspect must be fully defined before & is frozen",
1289 ASN, E);
1290 end if;
b3f8228a 1291
7d6fb253 1292 when Aspect_Iterable =>
1293 Validate_Iterable_Aspect (E, ASN);
1294
1295 when others =>
1296 null;
37c6e44c 1297 end case;
06ef5f86 1298
37c6e44c 1299 Ritem := Aspect_Rep_Item (ASN);
06ef5f86 1300
37c6e44c 1301 if Present (Ritem) then
1302 Analyze (Ritem);
1303 end if;
06ef5f86 1304 end if;
1305 end if;
1306
1307 Next_Rep_Item (ASN);
1308 end loop;
37c6e44c 1309
1310 -- This is where we inherit delayed rep aspects from our parent. Note
1311 -- that if we fell out of the above loop with ASN non-empty, it means
1312 -- we hit an aspect for an entity other than E, and it must be the
1313 -- type from which we were derived.
1314
1315 if May_Inherit_Delayed_Rep_Aspects (E) then
1316 Inherit_Delayed_Rep_Aspects (ASN);
1317 end if;
06ef5f86 1318 end Analyze_Aspects_At_Freeze_Point;
1319
ae888dbd 1320 -----------------------------------
1321 -- Analyze_Aspect_Specifications --
1322 -----------------------------------
1323
21ea3a4f 1324 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
e2bf777d 1325 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
6c5793cd 1326 -- Establish linkages between an aspect and its corresponding pragma
5ddd846b 1327
5655be8a 1328 procedure Insert_Pragma
1329 (Prag : Node_Id;
1330 Is_Instance : Boolean := False);
2f06c88a 1331 -- Subsidiary to the analysis of aspects
1332 -- Abstract_State
2f06c88a 1333 -- Attach_Handler
1334 -- Contract_Cases
1335 -- Depends
5655be8a 1336 -- Ghost
2f06c88a 1337 -- Global
5655be8a 1338 -- Initial_Condition
1339 -- Initializes
2f06c88a 1340 -- Post
1341 -- Pre
1342 -- Refined_Depends
1343 -- Refined_Global
5655be8a 1344 -- Refined_State
2f06c88a 1345 -- SPARK_Mode
1346 -- Warnings
e2bf777d 1347 -- Insert pragma Prag such that it mimics the placement of a source
5655be8a 1348 -- pragma of the same kind. Flag Is_Generic should be set when the
1349 -- context denotes a generic instance.
e2bf777d 1350
1351 --------------
1352 -- Decorate --
1353 --------------
1354
1355 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
5ddd846b 1356 begin
6c5793cd 1357 Set_Aspect_Rep_Item (Asp, Prag);
5ddd846b 1358 Set_Corresponding_Aspect (Prag, Asp);
1359 Set_From_Aspect_Specification (Prag);
5ddd846b 1360 Set_Parent (Prag, Asp);
e2bf777d 1361 end Decorate;
f0813d71 1362
e2bf777d 1363 -------------------
1364 -- Insert_Pragma --
1365 -------------------
c1006d6d 1366
5655be8a 1367 procedure Insert_Pragma
1368 (Prag : Node_Id;
1369 Is_Instance : Boolean := False)
1370 is
3ff5e35d 1371 Aux : Node_Id;
1372 Decl : Node_Id;
1373 Decls : List_Id;
1374 Def : Node_Id;
1375 Inserted : Boolean := False;
c1006d6d 1376
1377 begin
3ff5e35d 1378 -- When the aspect appears on an entry, package, protected unit,
1379 -- subprogram, or task unit body, insert the generated pragma at the
1380 -- top of the body declarations to emulate the behavior of a source
1381 -- pragma.
2f06c88a 1382
1383 -- package body Pack with Aspect is
1384
1385 -- package body Pack is
1386 -- pragma Prag;
1387
3ff5e35d 1388 if Nkind_In (N, N_Entry_Body,
1389 N_Package_Body,
2f06c88a 1390 N_Protected_Body,
1391 N_Subprogram_Body,
1392 N_Task_Body)
1393 then
1394 Decls := Declarations (N);
1395
1396 if No (Decls) then
1397 Decls := New_List;
1398 Set_Declarations (N, Decls);
1399 end if;
e2bf777d 1400
3ff5e35d 1401 Prepend_To (Decls, Prag);
2f06c88a 1402
1403 -- When the aspect is associated with a [generic] package declaration
1404 -- insert the generated pragma at the top of the visible declarations
1405 -- to emulate the behavior of a source pragma.
1406
1407 -- package Pack with Aspect is
1408
1409 -- package Pack is
1410 -- pragma Prag;
1411
1412 elsif Nkind_In (N, N_Generic_Package_Declaration,
1413 N_Package_Declaration)
1414 then
1415 Decls := Visible_Declarations (Specification (N));
1416
1417 if No (Decls) then
1418 Decls := New_List;
1419 Set_Visible_Declarations (Specification (N), Decls);
1420 end if;
1421
5655be8a 1422 -- The visible declarations of a generic instance have the
1423 -- following structure:
1424
1425 -- <renamings of generic formals>
1426 -- <renamings of internally-generated spec and body>
1427 -- <first source declaration>
1428
1429 -- Insert the pragma before the first source declaration by
3ff5e35d 1430 -- skipping the instance "header" to ensure proper visibility of
1431 -- all formals.
5655be8a 1432
1433 if Is_Instance then
1434 Decl := First (Decls);
3ff5e35d 1435 while Present (Decl) loop
1436 if Comes_From_Source (Decl) then
1437 Insert_Before (Decl, Prag);
1438 Inserted := True;
1439 exit;
1440 else
1441 Next (Decl);
1442 end if;
5655be8a 1443 end loop;
1444
3ff5e35d 1445 -- The pragma is placed after the instance "header"
5655be8a 1446
3ff5e35d 1447 if not Inserted then
5655be8a 1448 Append_To (Decls, Prag);
1449 end if;
1450
1451 -- Otherwise this is not a generic instance
1452
1453 else
1454 Prepend_To (Decls, Prag);
1455 end if;
2f06c88a 1456
1457 -- When the aspect is associated with a protected unit declaration,
1458 -- insert the generated pragma at the top of the visible declarations
1459 -- the emulate the behavior of a source pragma.
1460
1461 -- protected [type] Prot with Aspect is
1462
1463 -- protected [type] Prot is
1464 -- pragma Prag;
1465
1466 elsif Nkind (N) = N_Protected_Type_Declaration then
736b80cc 1467 Def := Protected_Definition (N);
1468
1469 if No (Def) then
1470 Def :=
1471 Make_Protected_Definition (Sloc (N),
1472 Visible_Declarations => New_List,
1473 End_Label => Empty);
1474
1475 Set_Protected_Definition (N, Def);
1476 end if;
1477
1478 Decls := Visible_Declarations (Def);
2f06c88a 1479
1480 if No (Decls) then
1481 Decls := New_List;
736b80cc 1482 Set_Visible_Declarations (Def, Decls);
2f06c88a 1483 end if;
1484
1485 Prepend_To (Decls, Prag);
1486
736b80cc 1487 -- When the aspect is associated with a task unit declaration, insert
1488 -- insert the generated pragma at the top of the visible declarations
1489 -- the emulate the behavior of a source pragma.
2f06c88a 1490
1491 -- task [type] Prot with Aspect is
1492
1493 -- task [type] Prot is
1494 -- pragma Prag;
1495
736b80cc 1496 elsif Nkind (N) = N_Task_Type_Declaration then
1497 Def := Task_Definition (N);
1498
1499 if No (Def) then
1500 Def :=
1501 Make_Task_Definition (Sloc (N),
1502 Visible_Declarations => New_List,
1503 End_Label => Empty);
1504
1505 Set_Task_Definition (N, Def);
1506 end if;
1507
1508 Decls := Visible_Declarations (Def);
2f06c88a 1509
1510 if No (Decls) then
1511 Decls := New_List;
736b80cc 1512 Set_Visible_Declarations (Def, Decls);
d324c418 1513 end if;
c1006d6d 1514
2f06c88a 1515 Prepend_To (Decls, Prag);
1516
ed695684 1517 -- When the context is a library unit, the pragma is added to the
1518 -- Pragmas_After list.
1519
1520 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1521 Aux := Aux_Decls_Node (Parent (N));
1522
1523 if No (Pragmas_After (Aux)) then
1524 Set_Pragmas_After (Aux, New_List);
1525 end if;
1526
1527 Prepend (Prag, Pragmas_After (Aux));
1528
2f06c88a 1529 -- Default, the pragma is inserted after the context
c1006d6d 1530
1531 else
1532 Insert_After (N, Prag);
c1006d6d 1533 end if;
e2bf777d 1534 end Insert_Pragma;
c1006d6d 1535
1536 -- Local variables
1537
ae888dbd 1538 Aspect : Node_Id;
d74fc39a 1539 Aitem : Node_Id;
ae888dbd 1540 Ent : Node_Id;
ae888dbd 1541
21ea3a4f 1542 L : constant List_Id := Aspect_Specifications (N);
1543
ae888dbd 1544 Ins_Node : Node_Id := N;
89f1e35c 1545 -- Insert pragmas/attribute definition clause after this node when no
1546 -- delayed analysis is required.
d74fc39a 1547
ee2b7923 1548 -- Start of processing for Analyze_Aspect_Specifications
f0813d71 1549
ee2b7923 1550 begin
d74fc39a 1551 -- The general processing involves building an attribute definition
89f1e35c 1552 -- clause or a pragma node that corresponds to the aspect. Then in order
1553 -- to delay the evaluation of this aspect to the freeze point, we attach
1554 -- the corresponding pragma/attribute definition clause to the aspect
1555 -- specification node, which is then placed in the Rep Item chain. In
1556 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1557 -- and we evaluate the rep item at the freeze point. When the aspect
1558 -- doesn't have a corresponding pragma/attribute definition clause, then
1559 -- its analysis is simply delayed at the freeze point.
1560
1561 -- Some special cases don't require delay analysis, thus the aspect is
1562 -- analyzed right now.
1563
51ea9c94 1564 -- Note that there is a special handling for Pre, Post, Test_Case,
e66f4e2a 1565 -- Contract_Cases aspects. In these cases, we do not have to worry
51ea9c94 1566 -- about delay issues, since the pragmas themselves deal with delay
1567 -- of visibility for the expression analysis. Thus, we just insert
1568 -- the pragma after the node N.
ae888dbd 1569
21ea3a4f 1570 pragma Assert (Present (L));
1571
6fb3c314 1572 -- Loop through aspects
f93e7257 1573
ae888dbd 1574 Aspect := First (L);
21ea3a4f 1575 Aspect_Loop : while Present (Aspect) loop
0fd13d32 1576 Analyze_One_Aspect : declare
94153a42 1577 Expr : constant Node_Id := Expression (Aspect);
89f1e35c 1578 Id : constant Node_Id := Identifier (Aspect);
1579 Loc : constant Source_Ptr := Sloc (Aspect);
94153a42 1580 Nam : constant Name_Id := Chars (Id);
1581 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
ae888dbd 1582 Anod : Node_Id;
1583
37c6e44c 1584 Delay_Required : Boolean;
89f1e35c 1585 -- Set False if delay is not required
1586
c0793fff 1587 Eloc : Source_Ptr := No_Location;
1588 -- Source location of expression, modified when we split PPC's. It
1589 -- is set below when Expr is present.
39e1f22f 1590
ee2b7923 1591 procedure Analyze_Aspect_Convention;
1592 -- Perform analysis of aspect Convention
1593
1594 procedure Analyze_Aspect_Export_Import;
1595 -- Perform analysis of aspects Export or Import
1596
1597 procedure Analyze_Aspect_External_Link_Name;
1598 -- Perform analysis of aspects External_Name or Link_Name
21ea3a4f 1599
89f1e35c 1600 procedure Analyze_Aspect_Implicit_Dereference;
9ab32fe9 1601 -- Perform analysis of the Implicit_Dereference aspects
0fd13d32 1602
1603 procedure Make_Aitem_Pragma
1604 (Pragma_Argument_Associations : List_Id;
1605 Pragma_Name : Name_Id);
1606 -- This is a wrapper for Make_Pragma used for converting aspects
1607 -- to pragmas. It takes care of Sloc (set from Loc) and building
1608 -- the pragma identifier from the given name. In addition the
1609 -- flags Class_Present and Split_PPC are set from the aspect
1610 -- node, as well as Is_Ignored. This routine also sets the
1611 -- From_Aspect_Specification in the resulting pragma node to
1612 -- True, and sets Corresponding_Aspect to point to the aspect.
1613 -- The resulting pragma is assigned to Aitem.
21ea3a4f 1614
ee2b7923 1615 -------------------------------
1616 -- Analyze_Aspect_Convention --
1617 -------------------------------
1618
1619 procedure Analyze_Aspect_Convention is
1620 Conv : Node_Id;
1621 Dummy_1 : Node_Id;
1622 Dummy_2 : Node_Id;
1623 Dummy_3 : Node_Id;
1624 Expo : Node_Id;
1625 Imp : Node_Id;
89f1e35c 1626
21ea3a4f 1627 begin
ee2b7923 1628 -- Obtain all interfacing aspects that apply to the related
1629 -- entity.
1630
1631 Get_Interfacing_Aspects
1632 (Iface_Asp => Aspect,
1633 Conv_Asp => Dummy_1,
1634 EN_Asp => Dummy_2,
1635 Expo_Asp => Expo,
1636 Imp_Asp => Imp,
1637 LN_Asp => Dummy_3,
1638 Do_Checks => True);
1639
1640 -- The related entity is subject to aspect Export or Import.
1641 -- Do not process Convention now because it must be analysed
1642 -- as part of Export or Import.
1643
1644 if Present (Expo) or else Present (Imp) then
1645 return;
21ea3a4f 1646
ee2b7923 1647 -- Otherwise Convention appears by itself
21ea3a4f 1648
ee2b7923 1649 else
1650 -- The aspect specifies a particular convention
1651
1652 if Present (Expr) then
1653 Conv := New_Copy_Tree (Expr);
1654
1655 -- Otherwise assume convention Ada
1656
1657 else
1658 Conv := Make_Identifier (Loc, Name_Ada);
1659 end if;
1660
1661 -- Generate:
1662 -- pragma Convention (<Conv>, <E>);
1663
1664 Make_Aitem_Pragma
1665 (Pragma_Name => Name_Convention,
1666 Pragma_Argument_Associations => New_List (
1667 Make_Pragma_Argument_Association (Loc,
1668 Expression => Conv),
1669 Make_Pragma_Argument_Association (Loc,
1670 Expression => New_Occurrence_Of (E, Loc))));
1671
1672 Decorate (Aspect, Aitem);
1673 Insert_Pragma (Aitem);
1674 end if;
1675 end Analyze_Aspect_Convention;
1676
1677 ----------------------------------
1678 -- Analyze_Aspect_Export_Import --
1679 ----------------------------------
21ea3a4f 1680
ee2b7923 1681 procedure Analyze_Aspect_Export_Import is
1682 Dummy_1 : Node_Id;
1683 Dummy_2 : Node_Id;
1684 Dummy_3 : Node_Id;
1685 Expo : Node_Id;
1686 Imp : Node_Id;
1687
1688 begin
1689 -- Obtain all interfacing aspects that apply to the related
1690 -- entity.
1691
1692 Get_Interfacing_Aspects
1693 (Iface_Asp => Aspect,
1694 Conv_Asp => Dummy_1,
1695 EN_Asp => Dummy_2,
1696 Expo_Asp => Expo,
1697 Imp_Asp => Imp,
1698 LN_Asp => Dummy_3,
1699 Do_Checks => True);
1700
1701 -- The related entity cannot be subject to both aspects Export
1702 -- and Import.
1703
1704 if Present (Expo) and then Present (Imp) then
1705 Error_Msg_N
1706 ("incompatible interfacing aspects given for &", E);
1707 Error_Msg_Sloc := Sloc (Expo);
1708 Error_Msg_N ("\aspect `Export` #", E);
1709 Error_Msg_Sloc := Sloc (Imp);
1710 Error_Msg_N ("\aspect `Import` #", E);
1711 end if;
1712
1713 -- A variable is most likely modified from the outside. Take
051826ee 1714 -- the optimistic approach to avoid spurious errors.
ee2b7923 1715
1716 if Ekind (E) = E_Variable then
1717 Set_Never_Set_In_Source (E, False);
1718 end if;
1719
1720 -- Resolve the expression of an Import or Export here, and
1721 -- require it to be of type Boolean and static. This is not
1722 -- quite right, because in general this should be delayed,
1723 -- but that seems tricky for these, because normally Boolean
1724 -- aspects are replaced with pragmas at the freeze point in
1725 -- Make_Pragma_From_Boolean_Aspect.
1726
1727 if not Present (Expr)
1728 or else Is_True (Static_Boolean (Expr))
1729 then
1730 if A_Id = Aspect_Import then
1731 Set_Has_Completion (E);
1732 Set_Is_Imported (E);
1733
1734 -- An imported object cannot be explicitly initialized
1735
1736 if Nkind (N) = N_Object_Declaration
1737 and then Present (Expression (N))
1738 then
1739 Error_Msg_N
1740 ("imported entities cannot be initialized "
1741 & "(RM B.1(24))", Expression (N));
1742 end if;
1743
1744 else
1745 pragma Assert (A_Id = Aspect_Export);
1746 Set_Is_Exported (E);
1747 end if;
1748
1749 -- Create the proper form of pragma Export or Import taking
1750 -- into account Conversion, External_Name, and Link_Name.
1751
1752 Aitem := Build_Export_Import_Pragma (Aspect, E);
d8e539ae 1753
1754 -- Otherwise the expression is either False or erroneous. There
1755 -- is no corresponding pragma.
1756
1757 else
1758 Aitem := Empty;
ee2b7923 1759 end if;
1760 end Analyze_Aspect_Export_Import;
1761
1762 ---------------------------------------
1763 -- Analyze_Aspect_External_Link_Name --
1764 ---------------------------------------
1765
1766 procedure Analyze_Aspect_External_Link_Name is
1767 Dummy_1 : Node_Id;
1768 Dummy_2 : Node_Id;
1769 Dummy_3 : Node_Id;
1770 Expo : Node_Id;
1771 Imp : Node_Id;
1772
1773 begin
1774 -- Obtain all interfacing aspects that apply to the related
1775 -- entity.
1776
1777 Get_Interfacing_Aspects
1778 (Iface_Asp => Aspect,
1779 Conv_Asp => Dummy_1,
1780 EN_Asp => Dummy_2,
1781 Expo_Asp => Expo,
1782 Imp_Asp => Imp,
1783 LN_Asp => Dummy_3,
1784 Do_Checks => True);
1785
1786 -- Ensure that aspect External_Name applies to aspect Export or
1787 -- Import.
1788
1789 if A_Id = Aspect_External_Name then
1790 if No (Expo) and then No (Imp) then
89f1e35c 1791 Error_Msg_N
ee2b7923 1792 ("aspect `External_Name` requires aspect `Import` or "
1793 & "`Export`", Aspect);
89f1e35c 1794 end if;
ee2b7923 1795
1796 -- Otherwise ensure that aspect Link_Name applies to aspect
1797 -- Export or Import.
1798
1799 else
1800 pragma Assert (A_Id = Aspect_Link_Name);
1801 if No (Expo) and then No (Imp) then
1802 Error_Msg_N
1803 ("aspect `Link_Name` requires aspect `Import` or "
1804 & "`Export`", Aspect);
1805 end if;
1806 end if;
1807 end Analyze_Aspect_External_Link_Name;
21ea3a4f 1808
89f1e35c 1809 -----------------------------------------
1810 -- Analyze_Aspect_Implicit_Dereference --
1811 -----------------------------------------
21ea3a4f 1812
89f1e35c 1813 procedure Analyze_Aspect_Implicit_Dereference is
1ff43c00 1814 Disc : Entity_Id;
1815 Parent_Disc : Entity_Id;
1816
89f1e35c 1817 begin
b9e61b2a 1818 if not Is_Type (E) or else not Has_Discriminants (E) then
89f1e35c 1819 Error_Msg_N
1ff43c00 1820 ("aspect must apply to a type with discriminants", Expr);
21ea3a4f 1821
1ff43c00 1822 elsif not Is_Entity_Name (Expr) then
1823 Error_Msg_N
1824 ("aspect must name a discriminant of current type", Expr);
21ea3a4f 1825
1ff43c00 1826 else
f021ee0f 1827 -- Discriminant type be an anonymous access type or an
1828 -- anonymous access to subprogram.
0d0a4e9b 1829
f021ee0f 1830 -- Missing synchronized types???
1831
1ff43c00 1832 Disc := First_Discriminant (E);
1833 while Present (Disc) loop
1834 if Chars (Expr) = Chars (Disc)
f021ee0f 1835 and then Ekind_In (Etype (Disc),
0d0a4e9b 1836 E_Anonymous_Access_Subprogram_Type,
1837 E_Anonymous_Access_Type)
1ff43c00 1838 then
1839 Set_Has_Implicit_Dereference (E);
1840 Set_Has_Implicit_Dereference (Disc);
1841 exit;
1842 end if;
21ea3a4f 1843
1ff43c00 1844 Next_Discriminant (Disc);
1845 end loop;
21ea3a4f 1846
9b5b11fb 1847 -- Error if no proper access discriminant
21ea3a4f 1848
1ff43c00 1849 if No (Disc) then
ee2b7923 1850 Error_Msg_NE ("not an access discriminant of&", Expr, E);
1ff43c00 1851 return;
1852 end if;
1853 end if;
1854
9b5b11fb 1855 -- For a type extension, check whether parent has a
1856 -- reference discriminant, to verify that use is proper.
1857
1ff43c00 1858 if Is_Derived_Type (E)
1859 and then Has_Discriminants (Etype (E))
1860 then
1861 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1862
1863 if Present (Parent_Disc)
1864 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1865 then
ee2b7923 1866 Error_Msg_N
1867 ("reference discriminant does not match discriminant "
1868 & "of parent type", Expr);
1ff43c00 1869 end if;
89f1e35c 1870 end if;
1871 end Analyze_Aspect_Implicit_Dereference;
21ea3a4f 1872
0fd13d32 1873 -----------------------
1874 -- Make_Aitem_Pragma --
1875 -----------------------
1876
1877 procedure Make_Aitem_Pragma
1878 (Pragma_Argument_Associations : List_Id;
1879 Pragma_Name : Name_Id)
1880 is
b855559d 1881 Args : List_Id := Pragma_Argument_Associations;
1882
0fd13d32 1883 begin
1884 -- We should never get here if aspect was disabled
1885
1886 pragma Assert (not Is_Disabled (Aspect));
1887
056dc987 1888 -- Certain aspects allow for an optional name or expression. Do
1889 -- not generate a pragma with empty argument association list.
b855559d 1890
1891 if No (Args) or else No (Expression (First (Args))) then
1892 Args := No_List;
1893 end if;
1894
0fd13d32 1895 -- Build the pragma
1896
1897 Aitem :=
1898 Make_Pragma (Loc,
b855559d 1899 Pragma_Argument_Associations => Args,
0fd13d32 1900 Pragma_Identifier =>
1901 Make_Identifier (Sloc (Id), Pragma_Name),
9ab32fe9 1902 Class_Present => Class_Present (Aspect),
1903 Split_PPC => Split_PPC (Aspect));
0fd13d32 1904
1905 -- Set additional semantic fields
1906
1907 if Is_Ignored (Aspect) then
1908 Set_Is_Ignored (Aitem);
57d8d1f3 1909 elsif Is_Checked (Aspect) then
a5109493 1910 Set_Is_Checked (Aitem);
0fd13d32 1911 end if;
1912
1913 Set_Corresponding_Aspect (Aitem, Aspect);
fdec445e 1914 Set_From_Aspect_Specification (Aitem);
0fd13d32 1915 end Make_Aitem_Pragma;
1916
738ec25b 1917 -- Start of processing for Analyze_One_Aspect
0fd13d32 1918
ae888dbd 1919 begin
2d1acfa7 1920 -- Skip aspect if already analyzed, to avoid looping in some cases
fb7f2fc4 1921
1922 if Analyzed (Aspect) then
1923 goto Continue;
1924 end if;
1925
ef957022 1926 -- Skip looking at aspect if it is totally disabled. Just mark it
1927 -- as such for later reference in the tree. This also sets the
1928 -- Is_Ignored and Is_Checked flags appropriately.
51ea9c94 1929
1930 Check_Applicable_Policy (Aspect);
1931
1932 if Is_Disabled (Aspect) then
1933 goto Continue;
1934 end if;
1935
c0793fff 1936 -- Set the source location of expression, used in the case of
1937 -- a failed precondition/postcondition or invariant. Note that
1938 -- the source location of the expression is not usually the best
1939 -- choice here. For example, it gets located on the last AND
1940 -- keyword in a chain of boolean expressiond AND'ed together.
1941 -- It is best to put the message on the first character of the
1942 -- assertion, which is the effect of the First_Node call here.
1943
1944 if Present (Expr) then
1945 Eloc := Sloc (First_Node (Expr));
1946 end if;
1947
d7ed83a2 1948 -- Check restriction No_Implementation_Aspect_Specifications
1949
c171e1be 1950 if Implementation_Defined_Aspect (A_Id) then
d7ed83a2 1951 Check_Restriction
1952 (No_Implementation_Aspect_Specifications, Aspect);
1953 end if;
1954
1955 -- Check restriction No_Specification_Of_Aspect
1956
1957 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1958
f67ed4f5 1959 -- Mark aspect analyzed (actual analysis is delayed till later)
d7ed83a2 1960
fb7f2fc4 1961 Set_Analyzed (Aspect);
d74fc39a 1962 Set_Entity (Aspect, E);
738ec25b 1963
1964 -- Build the reference to E that will be used in the built pragmas
1965
d74fc39a 1966 Ent := New_Occurrence_Of (E, Sloc (Id));
1967
738ec25b 1968 if A_Id = Aspect_Attach_Handler
1969 or else A_Id = Aspect_Interrupt_Handler
1970 then
738ec25b 1971
f0e731f2 1972 -- Treat the specification as a reference to the protected
1973 -- operation, which might otherwise appear unreferenced and
1974 -- generate spurious warnings.
738ec25b 1975
f0e731f2 1976 Generate_Reference (E, Id);
738ec25b 1977 end if;
1978
1e3c4ae6 1979 -- Check for duplicate aspect. Note that the Comes_From_Source
1980 -- test allows duplicate Pre/Post's that we generate internally
1981 -- to escape being flagged here.
ae888dbd 1982
6c545057 1983 if No_Duplicates_Allowed (A_Id) then
1984 Anod := First (L);
1985 while Anod /= Aspect loop
c171e1be 1986 if Comes_From_Source (Aspect)
1987 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
6c545057 1988 then
1989 Error_Msg_Name_1 := Nam;
1990 Error_Msg_Sloc := Sloc (Anod);
39e1f22f 1991
6c545057 1992 -- Case of same aspect specified twice
39e1f22f 1993
6c545057 1994 if Class_Present (Anod) = Class_Present (Aspect) then
1995 if not Class_Present (Anod) then
1996 Error_Msg_NE
1997 ("aspect% for & previously given#",
1998 Id, E);
1999 else
2000 Error_Msg_NE
2001 ("aspect `%''Class` for & previously given#",
2002 Id, E);
2003 end if;
39e1f22f 2004 end if;
6c545057 2005 end if;
ae888dbd 2006
6c545057 2007 Next (Anod);
2008 end loop;
2009 end if;
ae888dbd 2010
4db325e6 2011 -- Check some general restrictions on language defined aspects
2012
c171e1be 2013 if not Implementation_Defined_Aspect (A_Id) then
4db325e6 2014 Error_Msg_Name_1 := Nam;
2015
d1edd78e 2016 -- Not allowed for renaming declarations. Examine the original
da1b7592 2017 -- node because a subprogram renaming may have been rewritten
2018 -- as a body.
4db325e6 2019
da1b7592 2020 if Nkind (Original_Node (N)) in N_Renaming_Declaration then
4db325e6 2021 Error_Msg_N
2022 ("aspect % not allowed for renaming declaration",
2023 Aspect);
2024 end if;
2025
2026 -- Not allowed for formal type declarations
2027
2028 if Nkind (N) = N_Formal_Type_Declaration then
2029 Error_Msg_N
2030 ("aspect % not allowed for formal type declaration",
2031 Aspect);
2032 end if;
2033 end if;
2034
7d20685d 2035 -- Copy expression for later processing by the procedures
2036 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
2037
2038 Set_Entity (Id, New_Copy_Tree (Expr));
2039
37c6e44c 2040 -- Set Delay_Required as appropriate to aspect
2041
2042 case Aspect_Delay (A_Id) is
2043 when Always_Delay =>
2044 Delay_Required := True;
2045
2046 when Never_Delay =>
2047 Delay_Required := False;
2048
2049 when Rep_Aspect =>
2050
2051 -- If expression has the form of an integer literal, then
2052 -- do not delay, since we know the value cannot change.
2053 -- This optimization catches most rep clause cases.
2054
e43fc5c5 2055 -- For Boolean aspects, don't delay if no expression
2056
2057 if A_Id in Boolean_Aspects and then No (Expr) then
2058 Delay_Required := False;
2059
c5c6a638 2060 -- For non-Boolean aspects, don't delay if integer literal,
2061 -- unless the aspect is Alignment, which affects the
2062 -- freezing of an initialized object.
e43fc5c5 2063
2064 elsif A_Id not in Boolean_Aspects
c5c6a638 2065 and then A_Id /= Aspect_Alignment
e43fc5c5 2066 and then Present (Expr)
2067 and then Nkind (Expr) = N_Integer_Literal
2068 then
2069 Delay_Required := False;
2070
2071 -- All other cases are delayed
2072
2073 else
2074 Delay_Required := True;
2075 Set_Has_Delayed_Rep_Aspects (E);
2076 end if;
37c6e44c 2077 end case;
2078
ae888dbd 2079 -- Processing based on specific aspect
2080
d74fc39a 2081 case A_Id is
aa2f48d2 2082 when Aspect_Unimplemented =>
2083 null; -- ??? temp for now
ae888dbd 2084
2085 -- No_Aspect should be impossible
2086
2087 when No_Aspect =>
2088 raise Program_Error;
2089
89f1e35c 2090 -- Case 1: Aspects corresponding to attribute definition
2091 -- clauses.
ae888dbd 2092
99378362 2093 when Aspect_Address
2094 | Aspect_Alignment
2095 | Aspect_Bit_Order
2096 | Aspect_Component_Size
2097 | Aspect_Constant_Indexing
2098 | Aspect_Default_Iterator
2099 | Aspect_Dispatching_Domain
2100 | Aspect_External_Tag
2101 | Aspect_Input
2102 | Aspect_Iterable
2103 | Aspect_Iterator_Element
2104 | Aspect_Machine_Radix
2105 | Aspect_Object_Size
2106 | Aspect_Output
2107 | Aspect_Read
2108 | Aspect_Scalar_Storage_Order
2109 | Aspect_Secondary_Stack_Size
2110 | Aspect_Simple_Storage_Pool
2111 | Aspect_Size
2112 | Aspect_Small
2113 | Aspect_Storage_Pool
2114 | Aspect_Stream_Size
2115 | Aspect_Value_Size
2116 | Aspect_Variable_Indexing
2117 | Aspect_Write
2118 =>
89f1e35c 2119 -- Indexing aspects apply only to tagged type
2120
2121 if (A_Id = Aspect_Constant_Indexing
37c6e44c 2122 or else
2123 A_Id = Aspect_Variable_Indexing)
89f1e35c 2124 and then not (Is_Type (E)
2125 and then Is_Tagged_Type (E))
2126 then
05987af3 2127 Error_Msg_N
2128 ("indexing aspect can only apply to a tagged type",
3f4c9ffc 2129 Aspect);
89f1e35c 2130 goto Continue;
2131 end if;
2132
39616053 2133 -- For the case of aspect Address, we don't consider that we
588e7f97 2134 -- know the entity is never set in the source, since it is
2135 -- is likely aliasing is occurring.
2136
2137 -- Note: one might think that the analysis of the resulting
2138 -- attribute definition clause would take care of that, but
2139 -- that's not the case since it won't be from source.
2140
2141 if A_Id = Aspect_Address then
2142 Set_Never_Set_In_Source (E, False);
2143 end if;
2144
5ac76cee 2145 -- Correctness of the profile of a stream operation is
2146 -- verified at the freeze point, but we must detect the
2147 -- illegal specification of this aspect for a subtype now,
2148 -- to prevent malformed rep_item chains.
2149
fbf4d6ef 2150 if A_Id = Aspect_Input or else
2151 A_Id = Aspect_Output or else
2152 A_Id = Aspect_Read or else
2153 A_Id = Aspect_Write
5ac76cee 2154 then
fbf4d6ef 2155 if not Is_First_Subtype (E) then
2156 Error_Msg_N
2157 ("local name must be a first subtype", Aspect);
2158 goto Continue;
2159
2160 -- If stream aspect applies to the class-wide type,
2161 -- the generated attribute definition applies to the
2162 -- class-wide type as well.
2163
2164 elsif Class_Present (Aspect) then
2165 Ent :=
2166 Make_Attribute_Reference (Loc,
2167 Prefix => Ent,
2168 Attribute_Name => Name_Class);
2169 end if;
5ac76cee 2170 end if;
2171
d74fc39a 2172 -- Construct the attribute definition clause
2173
2174 Aitem :=
94153a42 2175 Make_Attribute_Definition_Clause (Loc,
d74fc39a 2176 Name => Ent,
ae888dbd 2177 Chars => Chars (Id),
2178 Expression => Relocate_Node (Expr));
2179
af9a0cc3 2180 -- If the address is specified, then we treat the entity as
41f06abf 2181 -- referenced, to avoid spurious warnings. This is analogous
2182 -- to what is done with an attribute definition clause, but
2183 -- here we don't want to generate a reference because this
2184 -- is the point of definition of the entity.
2185
2186 if A_Id = Aspect_Address then
2187 Set_Referenced (E);
2188 end if;
2189
51ea9c94 2190 -- Case 2: Aspects corresponding to pragmas
d74fc39a 2191
89f1e35c 2192 -- Case 2a: Aspects corresponding to pragmas with two
2193 -- arguments, where the first argument is a local name
2194 -- referring to the entity, and the second argument is the
2195 -- aspect definition expression.
ae888dbd 2196
04ae062f 2197 -- Linker_Section/Suppress/Unsuppress
0fd13d32 2198
99378362 2199 when Aspect_Linker_Section
2200 | Aspect_Suppress
2201 | Aspect_Unsuppress
2202 =>
0fd13d32 2203 Make_Aitem_Pragma
2204 (Pragma_Argument_Associations => New_List (
2205 Make_Pragma_Argument_Association (Loc,
2206 Expression => New_Occurrence_Of (E, Loc)),
2207 Make_Pragma_Argument_Association (Sloc (Expr),
2208 Expression => Relocate_Node (Expr))),
2209 Pragma_Name => Chars (Id));
57cd943b 2210
0fd13d32 2211 -- Synchronization
d74fc39a 2212
0fd13d32 2213 -- Corresponds to pragma Implemented, construct the pragma
49213728 2214
5bbfbad2 2215 when Aspect_Synchronization =>
0fd13d32 2216 Make_Aitem_Pragma
2217 (Pragma_Argument_Associations => New_List (
2218 Make_Pragma_Argument_Association (Loc,
2219 Expression => New_Occurrence_Of (E, Loc)),
2220 Make_Pragma_Argument_Association (Sloc (Expr),
2221 Expression => Relocate_Node (Expr))),
2222 Pragma_Name => Name_Implemented);
49213728 2223
e2bf777d 2224 -- Attach_Handler
0fd13d32 2225
89f1e35c 2226 when Aspect_Attach_Handler =>
0fd13d32 2227 Make_Aitem_Pragma
2228 (Pragma_Argument_Associations => New_List (
2229 Make_Pragma_Argument_Association (Sloc (Ent),
2230 Expression => Ent),
2231 Make_Pragma_Argument_Association (Sloc (Expr),
2232 Expression => Relocate_Node (Expr))),
2233 Pragma_Name => Name_Attach_Handler);
2234
f67ed4f5 2235 -- We need to insert this pragma into the tree to get proper
2236 -- processing and to look valid from a placement viewpoint.
2237
e2bf777d 2238 Insert_Pragma (Aitem);
f67ed4f5 2239 goto Continue;
2240
0fd13d32 2241 -- Dynamic_Predicate, Predicate, Static_Predicate
89f1e35c 2242
99378362 2243 when Aspect_Dynamic_Predicate
2244 | Aspect_Predicate
2245 | Aspect_Static_Predicate
2246 =>
a47ce82d 2247 -- These aspects apply only to subtypes
2248
2249 if not Is_Type (E) then
2250 Error_Msg_N
2251 ("predicate can only be specified for a subtype",
2252 Aspect);
2253 goto Continue;
7c0c95b8 2254
2255 elsif Is_Incomplete_Type (E) then
2256 Error_Msg_N
2257 ("predicate cannot apply to incomplete view", Aspect);
2258 goto Continue;
a47ce82d 2259 end if;
2260
89f1e35c 2261 -- Construct the pragma (always a pragma Predicate, with
51ea9c94 2262 -- flags recording whether it is static/dynamic). We also
2263 -- set flags recording this in the type itself.
89f1e35c 2264
0fd13d32 2265 Make_Aitem_Pragma
2266 (Pragma_Argument_Associations => New_List (
2267 Make_Pragma_Argument_Association (Sloc (Ent),
2268 Expression => Ent),
2269 Make_Pragma_Argument_Association (Sloc (Expr),
2270 Expression => Relocate_Node (Expr))),
fdec445e 2271 Pragma_Name => Name_Predicate);
89f1e35c 2272
51ea9c94 2273 -- Mark type has predicates, and remember what kind of
2274 -- aspect lead to this predicate (we need this to access
2275 -- the right set of check policies later on).
2276
2277 Set_Has_Predicates (E);
2278
2279 if A_Id = Aspect_Dynamic_Predicate then
2280 Set_Has_Dynamic_Predicate_Aspect (E);
0ec8f3e0 2281
2282 -- If the entity has a dynamic predicate, any inherited
2283 -- static predicate becomes dynamic as well, and the
2284 -- predicate function includes the conjunction of both.
2285
2286 Set_Has_Static_Predicate_Aspect (E, False);
2287
51ea9c94 2288 elsif A_Id = Aspect_Static_Predicate then
2289 Set_Has_Static_Predicate_Aspect (E);
2290 end if;
2291
89f1e35c 2292 -- If the type is private, indicate that its completion
6653b695 2293 -- has a freeze node, because that is the one that will
2294 -- be visible at freeze time.
89f1e35c 2295
0fd13d32 2296 if Is_Private_Type (E) and then Present (Full_View (E)) then
89f1e35c 2297 Set_Has_Predicates (Full_View (E));
51ea9c94 2298
2299 if A_Id = Aspect_Dynamic_Predicate then
2300 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2301 elsif A_Id = Aspect_Static_Predicate then
2302 Set_Has_Static_Predicate_Aspect (Full_View (E));
2303 end if;
2304
89f1e35c 2305 Set_Has_Delayed_Aspects (Full_View (E));
2306 Ensure_Freeze_Node (Full_View (E));
2307 end if;
2308
fdec445e 2309 -- Predicate_Failure
2310
2311 when Aspect_Predicate_Failure =>
2312
2313 -- This aspect applies only to subtypes
2314
2315 if not Is_Type (E) then
2316 Error_Msg_N
2317 ("predicate can only be specified for a subtype",
2318 Aspect);
2319 goto Continue;
2320
2321 elsif Is_Incomplete_Type (E) then
2322 Error_Msg_N
2323 ("predicate cannot apply to incomplete view", Aspect);
2324 goto Continue;
2325 end if;
2326
2327 -- Construct the pragma
2328
2329 Make_Aitem_Pragma
2330 (Pragma_Argument_Associations => New_List (
2331 Make_Pragma_Argument_Association (Sloc (Ent),
2332 Expression => Ent),
2333 Make_Pragma_Argument_Association (Sloc (Expr),
2334 Expression => Relocate_Node (Expr))),
2335 Pragma_Name => Name_Predicate_Failure);
2336
2337 Set_Has_Predicates (E);
2338
2339 -- If the type is private, indicate that its completion
2340 -- has a freeze node, because that is the one that will
2341 -- be visible at freeze time.
2342
2343 if Is_Private_Type (E) and then Present (Full_View (E)) then
2344 Set_Has_Predicates (Full_View (E));
2345 Set_Has_Delayed_Aspects (Full_View (E));
2346 Ensure_Freeze_Node (Full_View (E));
2347 end if;
2348
89f1e35c 2349 -- Case 2b: Aspects corresponding to pragmas with two
2350 -- arguments, where the second argument is a local name
2351 -- referring to the entity, and the first argument is the
2352 -- aspect definition expression.
ae888dbd 2353
0fd13d32 2354 -- Convention
2355
ee2b7923 2356 when Aspect_Convention =>
2357 Analyze_Aspect_Convention;
2358 goto Continue;
97bf66e6 2359
ee2b7923 2360 -- External_Name, Link_Name
97bf66e6 2361
99378362 2362 when Aspect_External_Name
2363 | Aspect_Link_Name
2364 =>
ee2b7923 2365 Analyze_Aspect_External_Link_Name;
2366 goto Continue;
e1cedbae 2367
0fd13d32 2368 -- CPU, Interrupt_Priority, Priority
2369
d6814978 2370 -- These three aspects can be specified for a subprogram spec
2371 -- or body, in which case we analyze the expression and export
2372 -- the value of the aspect.
2373
2374 -- Previously, we generated an equivalent pragma for bodies
2375 -- (note that the specs cannot contain these pragmas). The
2376 -- pragma was inserted ahead of local declarations, rather than
2377 -- after the body. This leads to a certain duplication between
2378 -- the processing performed for the aspect and the pragma, but
2379 -- given the straightforward handling required it is simpler
2380 -- to duplicate than to translate the aspect in the spec into
2381 -- a pragma in the declarative part of the body.
3a72f9c3 2382
99378362 2383 when Aspect_CPU
2384 | Aspect_Interrupt_Priority
2385 | Aspect_Priority
2386 =>
d6814978 2387 if Nkind_In (N, N_Subprogram_Body,
2388 N_Subprogram_Declaration)
2389 then
2390 -- Analyze the aspect expression
2391
2392 Analyze_And_Resolve (Expr, Standard_Integer);
2393
2394 -- Interrupt_Priority aspect not allowed for main
078a74b8 2395 -- subprograms. RM D.1 does not forbid this explicitly,
2396 -- but RM J.15.11(6/3) does not permit pragma
d6814978 2397 -- Interrupt_Priority for subprograms.
2398
2399 if A_Id = Aspect_Interrupt_Priority then
2400 Error_Msg_N
2401 ("Interrupt_Priority aspect cannot apply to "
2402 & "subprogram", Expr);
2403
2404 -- The expression must be static
2405
cda40848 2406 elsif not Is_OK_Static_Expression (Expr) then
d6814978 2407 Flag_Non_Static_Expr
2408 ("aspect requires static expression!", Expr);
2409
24d7b9d6 2410 -- Check whether this is the main subprogram. Issue a
2411 -- warning only if it is obviously not a main program
2412 -- (when it has parameters or when the subprogram is
2413 -- within a package).
2414
2415 elsif Present (Parameter_Specifications
2416 (Specification (N)))
2417 or else not Is_Compilation_Unit (Defining_Entity (N))
d6814978 2418 then
078a74b8 2419 -- See RM D.1(14/3) and D.16(12/3)
d6814978 2420
2421 Error_Msg_N
2422 ("aspect applied to subprogram other than the "
2423 & "main subprogram has no effect??", Expr);
2424
2425 -- Otherwise check in range and export the value
2426
2427 -- For the CPU aspect
2428
2429 elsif A_Id = Aspect_CPU then
2430 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2431
2432 -- Value is correct so we export the value to make
2433 -- it available at execution time.
2434
2435 Set_Main_CPU
2436 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2437
2438 else
2439 Error_Msg_N
2440 ("main subprogram CPU is out of range", Expr);
2441 end if;
2442
2443 -- For the Priority aspect
2444
2445 elsif A_Id = Aspect_Priority then
2446 if Is_In_Range (Expr, RTE (RE_Priority)) then
2447
2448 -- Value is correct so we export the value to make
2449 -- it available at execution time.
2450
2451 Set_Main_Priority
2452 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2453
32572384 2454 -- Ignore pragma if Relaxed_RM_Semantics to support
2455 -- other targets/non GNAT compilers.
2456
2457 elsif not Relaxed_RM_Semantics then
d6814978 2458 Error_Msg_N
2459 ("main subprogram priority is out of range",
2460 Expr);
2461 end if;
2462 end if;
2463
2464 -- Load an arbitrary entity from System.Tasking.Stages
2465 -- or System.Tasking.Restricted.Stages (depending on
2466 -- the supported profile) to make sure that one of these
2467 -- packages is implicitly with'ed, since we need to have
2468 -- the tasking run time active for the pragma Priority to
a0c3eeb9 2469 -- have any effect. Previously we with'ed the package
d6814978 2470 -- System.Tasking, but this package does not trigger the
2471 -- required initialization of the run-time library.
2472
2473 declare
2474 Discard : Entity_Id;
d6814978 2475 begin
2476 if Restricted_Profile then
2477 Discard := RTE (RE_Activate_Restricted_Tasks);
2478 else
2479 Discard := RTE (RE_Activate_Tasks);
2480 end if;
2481 end;
2482
e6ce0468 2483 -- Handling for these aspects in subprograms is complete
d6814978 2484
2485 goto Continue;
2486
fa65ad5e 2487 -- For task and protected types pass the aspect as an
2488 -- attribute.
0fd13d32 2489
3a72f9c3 2490 else
2491 Aitem :=
2492 Make_Attribute_Definition_Clause (Loc,
2493 Name => Ent,
2494 Chars => Chars (Id),
2495 Expression => Relocate_Node (Expr));
2496 end if;
2497
0fd13d32 2498 -- Warnings
2499
ae888dbd 2500 when Aspect_Warnings =>
0fd13d32 2501 Make_Aitem_Pragma
2502 (Pragma_Argument_Associations => New_List (
2503 Make_Pragma_Argument_Association (Sloc (Expr),
2504 Expression => Relocate_Node (Expr)),
2505 Make_Pragma_Argument_Association (Loc,
2506 Expression => New_Occurrence_Of (E, Loc))),
2507 Pragma_Name => Chars (Id));
ae888dbd 2508
2f06c88a 2509 Decorate (Aspect, Aitem);
2510 Insert_Pragma (Aitem);
2511 goto Continue;
2512
89f1e35c 2513 -- Case 2c: Aspects corresponding to pragmas with three
2514 -- arguments.
d64221a7 2515
89f1e35c 2516 -- Invariant aspects have a first argument that references the
2517 -- entity, a second argument that is the expression and a third
2518 -- argument that is an appropriate message.
d64221a7 2519
0fd13d32 2520 -- Invariant, Type_Invariant
2521
99378362 2522 when Aspect_Invariant
2523 | Aspect_Type_Invariant
2524 =>
89f1e35c 2525 -- Analysis of the pragma will verify placement legality:
2526 -- an invariant must apply to a private type, or appear in
2527 -- the private part of a spec and apply to a completion.
d64221a7 2528
0fd13d32 2529 Make_Aitem_Pragma
2530 (Pragma_Argument_Associations => New_List (
2531 Make_Pragma_Argument_Association (Sloc (Ent),
2532 Expression => Ent),
2533 Make_Pragma_Argument_Association (Sloc (Expr),
2534 Expression => Relocate_Node (Expr))),
2535 Pragma_Name => Name_Invariant);
89f1e35c 2536
2537 -- Add message unless exception messages are suppressed
2538
2539 if not Opt.Exception_Locations_Suppressed then
2540 Append_To (Pragma_Argument_Associations (Aitem),
2541 Make_Pragma_Argument_Association (Eloc,
2542 Chars => Name_Message,
2543 Expression =>
2544 Make_String_Literal (Eloc,
2545 Strval => "failed invariant from "
2546 & Build_Location_String (Eloc))));
d64221a7 2547 end if;
2548
89f1e35c 2549 -- For Invariant case, insert immediately after the entity
2550 -- declaration. We do not have to worry about delay issues
2551 -- since the pragma processing takes care of this.
2552
89f1e35c 2553 Delay_Required := False;
d64221a7 2554
47a46747 2555 -- Case 2d : Aspects that correspond to a pragma with one
2556 -- argument.
2557
0fd13d32 2558 -- Abstract_State
115f7b08 2559
d4e369ad 2560 -- Aspect Abstract_State introduces implicit declarations for
2561 -- all state abstraction entities it defines. To emulate this
2562 -- behavior, insert the pragma at the beginning of the visible
2563 -- declarations of the related package so that it is analyzed
2564 -- immediately.
2565
9129c28f 2566 when Aspect_Abstract_State => Abstract_State : declare
eb4f7efa 2567 Context : Node_Id := N;
9129c28f 2568
2569 begin
eb4f7efa 2570 -- When aspect Abstract_State appears on a generic package,
2571 -- it is propageted to the package instance. The context in
2572 -- this case is the instance spec.
2573
2574 if Nkind (Context) = N_Package_Instantiation then
2575 Context := Instance_Spec (Context);
2576 end if;
2577
2578 if Nkind_In (Context, N_Generic_Package_Declaration,
2579 N_Package_Declaration)
9129c28f 2580 then
9129c28f 2581 Make_Aitem_Pragma
2582 (Pragma_Argument_Associations => New_List (
2583 Make_Pragma_Argument_Association (Loc,
2584 Expression => Relocate_Node (Expr))),
2585 Pragma_Name => Name_Abstract_State);
630b6d55 2586
5655be8a 2587 Decorate (Aspect, Aitem);
2588 Insert_Pragma
2589 (Prag => Aitem,
2590 Is_Instance =>
2591 Is_Generic_Instance (Defining_Entity (Context)));
9129c28f 2592
2593 else
2594 Error_Msg_NE
2595 ("aspect & must apply to a package declaration",
2596 Aspect, Id);
2597 end if;
2598
2599 goto Continue;
2600 end Abstract_State;
115f7b08 2601
85ee12c0 2602 -- Aspect Async_Readers is never delayed because it is
2603 -- equivalent to a source pragma which appears after the
2604 -- related object declaration.
2605
2606 when Aspect_Async_Readers =>
2607 Make_Aitem_Pragma
2608 (Pragma_Argument_Associations => New_List (
2609 Make_Pragma_Argument_Association (Loc,
2610 Expression => Relocate_Node (Expr))),
2611 Pragma_Name => Name_Async_Readers);
2612
2613 Decorate (Aspect, Aitem);
2614 Insert_Pragma (Aitem);
2615 goto Continue;
2616
2617 -- Aspect Async_Writers is never delayed because it is
2618 -- equivalent to a source pragma which appears after the
2619 -- related object declaration.
2620
2621 when Aspect_Async_Writers =>
2622 Make_Aitem_Pragma
2623 (Pragma_Argument_Associations => New_List (
2624 Make_Pragma_Argument_Association (Loc,
2625 Expression => Relocate_Node (Expr))),
2626 Pragma_Name => Name_Async_Writers);
2627
2628 Decorate (Aspect, Aitem);
2629 Insert_Pragma (Aitem);
2630 goto Continue;
2631
d0849c23 2632 -- Aspect Constant_After_Elaboration is never delayed because
2633 -- it is equivalent to a source pragma which appears after the
2634 -- related object declaration.
2635
2636 when Aspect_Constant_After_Elaboration =>
2637 Make_Aitem_Pragma
2638 (Pragma_Argument_Associations => New_List (
2639 Make_Pragma_Argument_Association (Loc,
2640 Expression => Relocate_Node (Expr))),
2641 Pragma_Name =>
2642 Name_Constant_After_Elaboration);
2643
2644 Decorate (Aspect, Aitem);
2645 Insert_Pragma (Aitem);
2646 goto Continue;
2647
ec6f6da5 2648 -- Aspect Default_Internal_Condition is never delayed because
2649 -- it is equivalent to a source pragma which appears after the
2650 -- related private type. To deal with forward references, the
2651 -- generated pragma is stored in the rep chain of the related
2652 -- private type as types do not carry contracts. The pragma is
2653 -- wrapped inside of a procedure at the freeze point of the
2654 -- private type's full view.
2655
2656 when Aspect_Default_Initial_Condition =>
2657 Make_Aitem_Pragma
2658 (Pragma_Argument_Associations => New_List (
2659 Make_Pragma_Argument_Association (Loc,
2660 Expression => Relocate_Node (Expr))),
2661 Pragma_Name =>
2662 Name_Default_Initial_Condition);
2663
2664 Decorate (Aspect, Aitem);
2665 Insert_Pragma (Aitem);
2666 goto Continue;
2667
647fab54 2668 -- Default_Storage_Pool
2669
2670 when Aspect_Default_Storage_Pool =>
2671 Make_Aitem_Pragma
2672 (Pragma_Argument_Associations => New_List (
2673 Make_Pragma_Argument_Association (Loc,
2674 Expression => Relocate_Node (Expr))),
2675 Pragma_Name =>
2676 Name_Default_Storage_Pool);
2677
2678 Decorate (Aspect, Aitem);
2679 Insert_Pragma (Aitem);
2680 goto Continue;
2681
0fd13d32 2682 -- Depends
2683
e2bf777d 2684 -- Aspect Depends is never delayed because it is equivalent to
2685 -- a source pragma which appears after the related subprogram.
2686 -- To deal with forward references, the generated pragma is
2687 -- stored in the contract of the related subprogram and later
2688 -- analyzed at the end of the declarative region. See routine
2689 -- Analyze_Depends_In_Decl_Part for details.
6144c105 2690
12334c57 2691 when Aspect_Depends =>
0fd13d32 2692 Make_Aitem_Pragma
2693 (Pragma_Argument_Associations => New_List (
2694 Make_Pragma_Argument_Association (Loc,
2695 Expression => Relocate_Node (Expr))),
2696 Pragma_Name => Name_Depends);
2697
e2bf777d 2698 Decorate (Aspect, Aitem);
2699 Insert_Pragma (Aitem);
c1006d6d 2700 goto Continue;
2701
85ee12c0 2702 -- Aspect Effecitve_Reads is never delayed because it is
2703 -- equivalent to a source pragma which appears after the
2704 -- related object declaration.
2705
2706 when Aspect_Effective_Reads =>
2707 Make_Aitem_Pragma
2708 (Pragma_Argument_Associations => New_List (
2709 Make_Pragma_Argument_Association (Loc,
2710 Expression => Relocate_Node (Expr))),
2711 Pragma_Name => Name_Effective_Reads);
2712
2713 Decorate (Aspect, Aitem);
2714 Insert_Pragma (Aitem);
2715 goto Continue;
2716
2717 -- Aspect Effective_Writes is never delayed because it is
2718 -- equivalent to a source pragma which appears after the
2719 -- related object declaration.
2720
2721 when Aspect_Effective_Writes =>
2722 Make_Aitem_Pragma
2723 (Pragma_Argument_Associations => New_List (
2724 Make_Pragma_Argument_Association (Loc,
2725 Expression => Relocate_Node (Expr))),
2726 Pragma_Name => Name_Effective_Writes);
2727
2728 Decorate (Aspect, Aitem);
2729 Insert_Pragma (Aitem);
2730 goto Continue;
2731
cab27d2a 2732 -- Aspect Extensions_Visible is never delayed because it is
2733 -- equivalent to a source pragma which appears after the
2734 -- related subprogram.
2735
2736 when Aspect_Extensions_Visible =>
2737 Make_Aitem_Pragma
2738 (Pragma_Argument_Associations => New_List (
2739 Make_Pragma_Argument_Association (Loc,
2740 Expression => Relocate_Node (Expr))),
2741 Pragma_Name => Name_Extensions_Visible);
2742
2743 Decorate (Aspect, Aitem);
2744 Insert_Pragma (Aitem);
2745 goto Continue;
2746
3dbe7a69 2747 -- Aspect Ghost is never delayed because it is equivalent to a
2748 -- source pragma which appears at the top of [generic] package
2749 -- declarations or after an object, a [generic] subprogram, or
2750 -- a type declaration.
2751
5655be8a 2752 when Aspect_Ghost =>
3dbe7a69 2753 Make_Aitem_Pragma
2754 (Pragma_Argument_Associations => New_List (
2755 Make_Pragma_Argument_Association (Loc,
2756 Expression => Relocate_Node (Expr))),
2757 Pragma_Name => Name_Ghost);
2758
2759 Decorate (Aspect, Aitem);
5655be8a 2760 Insert_Pragma (Aitem);
3dbe7a69 2761 goto Continue;
3dbe7a69 2762
0fd13d32 2763 -- Global
12334c57 2764
e2bf777d 2765 -- Aspect Global is never delayed because it is equivalent to
2766 -- a source pragma which appears after the related subprogram.
2767 -- To deal with forward references, the generated pragma is
2768 -- stored in the contract of the related subprogram and later
2769 -- analyzed at the end of the declarative region. See routine
2770 -- Analyze_Global_In_Decl_Part for details.
3cdbaa5a 2771
2772 when Aspect_Global =>
0fd13d32 2773 Make_Aitem_Pragma
2774 (Pragma_Argument_Associations => New_List (
2775 Make_Pragma_Argument_Association (Loc,
2776 Expression => Relocate_Node (Expr))),
2777 Pragma_Name => Name_Global);
2778
e2bf777d 2779 Decorate (Aspect, Aitem);
2780 Insert_Pragma (Aitem);
c1006d6d 2781 goto Continue;
2782
9c138530 2783 -- Initial_Condition
2784
e2bf777d 2785 -- Aspect Initial_Condition is never delayed because it is
2786 -- equivalent to a source pragma which appears after the
2787 -- related package. To deal with forward references, the
2788 -- generated pragma is stored in the contract of the related
2789 -- package and later analyzed at the end of the declarative
2790 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2791 -- for details.
9c138530 2792
2793 when Aspect_Initial_Condition => Initial_Condition : declare
eb4f7efa 2794 Context : Node_Id := N;
9c138530 2795
2796 begin
e2bf777d 2797 -- When aspect Initial_Condition appears on a generic
2798 -- package, it is propageted to the package instance. The
2799 -- context in this case is the instance spec.
eb4f7efa 2800
2801 if Nkind (Context) = N_Package_Instantiation then
2802 Context := Instance_Spec (Context);
2803 end if;
2804
2805 if Nkind_In (Context, N_Generic_Package_Declaration,
2806 N_Package_Declaration)
9c138530 2807 then
9c138530 2808 Make_Aitem_Pragma
2809 (Pragma_Argument_Associations => New_List (
2810 Make_Pragma_Argument_Association (Loc,
2811 Expression => Relocate_Node (Expr))),
2812 Pragma_Name =>
2813 Name_Initial_Condition);
9c138530 2814
5655be8a 2815 Decorate (Aspect, Aitem);
2816 Insert_Pragma
2817 (Prag => Aitem,
2818 Is_Instance =>
2819 Is_Generic_Instance (Defining_Entity (Context)));
50e44732 2820
5655be8a 2821 -- Otherwise the context is illegal
9c138530 2822
2823 else
2824 Error_Msg_NE
2825 ("aspect & must apply to a package declaration",
2826 Aspect, Id);
2827 end if;
2828
2829 goto Continue;
2830 end Initial_Condition;
2831
d4e369ad 2832 -- Initializes
2833
e2bf777d 2834 -- Aspect Initializes is never delayed because it is equivalent
2835 -- to a source pragma appearing after the related package. To
2836 -- deal with forward references, the generated pragma is stored
2837 -- in the contract of the related package and later analyzed at
2838 -- the end of the declarative region. For details, see routine
2839 -- Analyze_Initializes_In_Decl_Part.
d4e369ad 2840
2841 when Aspect_Initializes => Initializes : declare
eb4f7efa 2842 Context : Node_Id := N;
d4e369ad 2843
2844 begin
50e44732 2845 -- When aspect Initializes appears on a generic package,
2846 -- it is propageted to the package instance. The context
2847 -- in this case is the instance spec.
eb4f7efa 2848
2849 if Nkind (Context) = N_Package_Instantiation then
2850 Context := Instance_Spec (Context);
2851 end if;
2852
2853 if Nkind_In (Context, N_Generic_Package_Declaration,
2854 N_Package_Declaration)
d4e369ad 2855 then
d4e369ad 2856 Make_Aitem_Pragma
2857 (Pragma_Argument_Associations => New_List (
2858 Make_Pragma_Argument_Association (Loc,
2859 Expression => Relocate_Node (Expr))),
2860 Pragma_Name => Name_Initializes);
d4e369ad 2861
5655be8a 2862 Decorate (Aspect, Aitem);
2863 Insert_Pragma
2864 (Prag => Aitem,
2865 Is_Instance =>
2866 Is_Generic_Instance (Defining_Entity (Context)));
50e44732 2867
5655be8a 2868 -- Otherwise the context is illegal
d4e369ad 2869
2870 else
2871 Error_Msg_NE
2872 ("aspect & must apply to a package declaration",
2873 Aspect, Id);
2874 end if;
2875
2876 goto Continue;
2877 end Initializes;
2878
cbd45084 2879 -- Max_Queue_Length
2880
2881 when Aspect_Max_Queue_Length =>
2882 Make_Aitem_Pragma
2883 (Pragma_Argument_Associations => New_List (
2884 Make_Pragma_Argument_Association (Loc,
2885 Expression => Relocate_Node (Expr))),
2886 Pragma_Name => Name_Max_Queue_Length);
2887
2888 Decorate (Aspect, Aitem);
2889 Insert_Pragma (Aitem);
2890 goto Continue;
2891
1fd4313f 2892 -- Obsolescent
2893
2894 when Aspect_Obsolescent => declare
2895 Args : List_Id;
2896
2897 begin
2898 if No (Expr) then
2899 Args := No_List;
2900 else
2901 Args := New_List (
2902 Make_Pragma_Argument_Association (Sloc (Expr),
2903 Expression => Relocate_Node (Expr)));
2904 end if;
2905
2906 Make_Aitem_Pragma
2907 (Pragma_Argument_Associations => Args,
2908 Pragma_Name => Chars (Id));
2909 end;
2910
5cc6f0cf 2911 -- Part_Of
2912
2913 when Aspect_Part_Of =>
2914 if Nkind_In (N, N_Object_Declaration,
2915 N_Package_Instantiation)
736b80cc 2916 or else Is_Single_Concurrent_Type_Declaration (N)
5cc6f0cf 2917 then
2918 Make_Aitem_Pragma
2919 (Pragma_Argument_Associations => New_List (
2920 Make_Pragma_Argument_Association (Loc,
2921 Expression => Relocate_Node (Expr))),
2922 Pragma_Name => Name_Part_Of);
2923
736b80cc 2924 Decorate (Aspect, Aitem);
2925 Insert_Pragma (Aitem);
736b80cc 2926
5cc6f0cf 2927 else
2928 Error_Msg_NE
736b80cc 2929 ("aspect & must apply to package instantiation, "
2930 & "object, single protected type or single task type",
2931 Aspect, Id);
5cc6f0cf 2932 end if;
2933
d5c65b80 2934 goto Continue;
2935
5dd93a61 2936 -- SPARK_Mode
2937
2f06c88a 2938 when Aspect_SPARK_Mode =>
5dd93a61 2939 Make_Aitem_Pragma
2940 (Pragma_Argument_Associations => New_List (
2941 Make_Pragma_Argument_Association (Loc,
2942 Expression => Relocate_Node (Expr))),
2943 Pragma_Name => Name_SPARK_Mode);
5dd93a61 2944
2f06c88a 2945 Decorate (Aspect, Aitem);
2946 Insert_Pragma (Aitem);
2947 goto Continue;
778ebf56 2948
4befb1a0 2949 -- Refined_Depends
2950
e2bf777d 2951 -- Aspect Refined_Depends is never delayed because it is
2952 -- equivalent to a source pragma which appears in the
2953 -- declarations of the related subprogram body. To deal with
2954 -- forward references, the generated pragma is stored in the
2955 -- contract of the related subprogram body and later analyzed
2956 -- at the end of the declarative region. For details, see
2957 -- routine Analyze_Refined_Depends_In_Decl_Part.
4befb1a0 2958
2959 when Aspect_Refined_Depends =>
422073ed 2960 Make_Aitem_Pragma
2961 (Pragma_Argument_Associations => New_List (
2962 Make_Pragma_Argument_Association (Loc,
2963 Expression => Relocate_Node (Expr))),
2964 Pragma_Name => Name_Refined_Depends);
2965
e2bf777d 2966 Decorate (Aspect, Aitem);
2967 Insert_Pragma (Aitem);
422073ed 2968 goto Continue;
4befb1a0 2969
2970 -- Refined_Global
2971
e2bf777d 2972 -- Aspect Refined_Global is never delayed because it is
2973 -- equivalent to a source pragma which appears in the
2974 -- declarations of the related subprogram body. To deal with
2975 -- forward references, the generated pragma is stored in the
2976 -- contract of the related subprogram body and later analyzed
2977 -- at the end of the declarative region. For details, see
2978 -- routine Analyze_Refined_Global_In_Decl_Part.
4befb1a0 2979
2980 when Aspect_Refined_Global =>
28ff117f 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_Refined_Global);
2986
e2bf777d 2987 Decorate (Aspect, Aitem);
2988 Insert_Pragma (Aitem);
28ff117f 2989 goto Continue;
4befb1a0 2990
63b65b2d 2991 -- Refined_Post
2992
2993 when Aspect_Refined_Post =>
2994 Make_Aitem_Pragma
2995 (Pragma_Argument_Associations => New_List (
2996 Make_Pragma_Argument_Association (Loc,
2997 Expression => Relocate_Node (Expr))),
2998 Pragma_Name => Name_Refined_Post);
2999
3ff5e35d 3000 Decorate (Aspect, Aitem);
3001 Insert_Pragma (Aitem);
3002 goto Continue;
3003
9129c28f 3004 -- Refined_State
3005
5655be8a 3006 when Aspect_Refined_State =>
9129c28f 3007
9129c28f 3008 -- The corresponding pragma for Refined_State is inserted in
3009 -- the declarations of the related package body. This action
3010 -- synchronizes both the source and from-aspect versions of
3011 -- the pragma.
3012
3013 if Nkind (N) = N_Package_Body then
9129c28f 3014 Make_Aitem_Pragma
3015 (Pragma_Argument_Associations => New_List (
3016 Make_Pragma_Argument_Association (Loc,
3017 Expression => Relocate_Node (Expr))),
3018 Pragma_Name => Name_Refined_State);
b9b2d6e5 3019
5655be8a 3020 Decorate (Aspect, Aitem);
3021 Insert_Pragma (Aitem);
b9b2d6e5 3022
5655be8a 3023 -- Otherwise the context is illegal
9129c28f 3024
3025 else
3026 Error_Msg_NE
3027 ("aspect & must apply to a package body", Aspect, Id);
3028 end if;
3029
3030 goto Continue;
9129c28f 3031
0fd13d32 3032 -- Relative_Deadline
3cdbaa5a 3033
3034 when Aspect_Relative_Deadline =>
0fd13d32 3035 Make_Aitem_Pragma
3036 (Pragma_Argument_Associations => New_List (
3037 Make_Pragma_Argument_Association (Loc,
3038 Expression => Relocate_Node (Expr))),
3039 Pragma_Name => Name_Relative_Deadline);
47a46747 3040
3041 -- If the aspect applies to a task, the corresponding pragma
3042 -- must appear within its declarations, not after.
3043
3044 if Nkind (N) = N_Task_Type_Declaration then
3045 declare
3046 Def : Node_Id;
3047 V : List_Id;
3048
3049 begin
3050 if No (Task_Definition (N)) then
3051 Set_Task_Definition (N,
3052 Make_Task_Definition (Loc,
3053 Visible_Declarations => New_List,
3054 End_Label => Empty));
3055 end if;
3056
3057 Def := Task_Definition (N);
3058 V := Visible_Declarations (Def);
3059 if not Is_Empty_List (V) then
3060 Insert_Before (First (V), Aitem);
3061
3062 else
3063 Set_Visible_Declarations (Def, New_List (Aitem));
3064 end if;
3065
3066 goto Continue;
3067 end;
3068 end if;
3069
85ee12c0 3070 -- Aspect Volatile_Function is never delayed because it is
3071 -- equivalent to a source pragma which appears after the
3072 -- related subprogram.
3073
3074 when Aspect_Volatile_Function =>
3075 Make_Aitem_Pragma
3076 (Pragma_Argument_Associations => New_List (
3077 Make_Pragma_Argument_Association (Loc,
3078 Expression => Relocate_Node (Expr))),
3079 Pragma_Name => Name_Volatile_Function);
3080
3081 Decorate (Aspect, Aitem);
3082 Insert_Pragma (Aitem);
3083 goto Continue;
3084
956ffaf4 3085 -- Case 2e: Annotate aspect
3086
3087 when Aspect_Annotate =>
3088 declare
3089 Args : List_Id;
3090 Pargs : List_Id;
3091 Arg : Node_Id;
3092
3093 begin
3094 -- The argument can be a single identifier
3095
3096 if Nkind (Expr) = N_Identifier then
3097
3098 -- One level of parens is allowed
3099
3100 if Paren_Count (Expr) > 1 then
3101 Error_Msg_F ("extra parentheses ignored", Expr);
3102 end if;
3103
3104 Set_Paren_Count (Expr, 0);
3105
3106 -- Add the single item to the list
3107
3108 Args := New_List (Expr);
3109
3110 -- Otherwise we must have an aggregate
3111
3112 elsif Nkind (Expr) = N_Aggregate then
3113
3114 -- Must be positional
3115
3116 if Present (Component_Associations (Expr)) then
3117 Error_Msg_F
3118 ("purely positional aggregate required", Expr);
3119 goto Continue;
3120 end if;
3121
3122 -- Must not be parenthesized
3123
3124 if Paren_Count (Expr) /= 0 then
3125 Error_Msg_F ("extra parentheses ignored", Expr);
3126 end if;
3127
3128 -- List of arguments is list of aggregate expressions
3129
3130 Args := Expressions (Expr);
3131
3132 -- Anything else is illegal
3133
3134 else
3135 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3136 goto Continue;
3137 end if;
3138
3139 -- Prepare pragma arguments
3140
3141 Pargs := New_List;
3142 Arg := First (Args);
3143 while Present (Arg) loop
3144 Append_To (Pargs,
3145 Make_Pragma_Argument_Association (Sloc (Arg),
3146 Expression => Relocate_Node (Arg)));
3147 Next (Arg);
3148 end loop;
3149
3150 Append_To (Pargs,
3151 Make_Pragma_Argument_Association (Sloc (Ent),
3152 Chars => Name_Entity,
3153 Expression => Ent));
3154
3155 Make_Aitem_Pragma
3156 (Pragma_Argument_Associations => Pargs,
3157 Pragma_Name => Name_Annotate);
3158 end;
3159
89f1e35c 3160 -- Case 3 : Aspects that don't correspond to pragma/attribute
3161 -- definition clause.
7b9b2f05 3162
89f1e35c 3163 -- Case 3a: The aspects listed below don't correspond to
3164 -- pragmas/attributes but do require delayed analysis.
7f694ca2 3165
51fa2a45 3166 -- Default_Value can only apply to a scalar type
3167
3168 when Aspect_Default_Value =>
3169 if not Is_Scalar_Type (E) then
3170 Error_Msg_N
1089ff19 3171 ("aspect Default_Value must apply to a scalar type", N);
51fa2a45 3172 end if;
3173
3174 Aitem := Empty;
3175
3176 -- Default_Component_Value can only apply to an array type
3177 -- with scalar components.
3178
3179 when Aspect_Default_Component_Value =>
3180 if not (Is_Array_Type (E)
3f4c9ffc 3181 and then Is_Scalar_Type (Component_Type (E)))
51fa2a45 3182 then
ee2b7923 3183 Error_Msg_N
3184 ("aspect Default_Component_Value can only apply to an "
3185 & "array of scalar components", N);
51fa2a45 3186 end if;
0fd13d32 3187
89f1e35c 3188 Aitem := Empty;
7f694ca2 3189
89f1e35c 3190 -- Case 3b: The aspects listed below don't correspond to
3191 -- pragmas/attributes and don't need delayed analysis.
95bc75fa 3192
0fd13d32 3193 -- Implicit_Dereference
3194
89f1e35c 3195 -- For Implicit_Dereference, External_Name and Link_Name, only
3196 -- the legality checks are done during the analysis, thus no
3197 -- delay is required.
a8e38e1d 3198
89f1e35c 3199 when Aspect_Implicit_Dereference =>
3200 Analyze_Aspect_Implicit_Dereference;
3201 goto Continue;
7f694ca2 3202
0fd13d32 3203 -- Dimension
3204
89f1e35c 3205 when Aspect_Dimension =>
3206 Analyze_Aspect_Dimension (N, Id, Expr);
3207 goto Continue;
cb4c311d 3208
0fd13d32 3209 -- Dimension_System
3210
89f1e35c 3211 when Aspect_Dimension_System =>
3212 Analyze_Aspect_Dimension_System (N, Id, Expr);
3213 goto Continue;
7f694ca2 3214
ceec4f7c 3215 -- Case 4: Aspects requiring special handling
51ea9c94 3216
e66f4e2a 3217 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3218 -- pragmas take care of the delay.
7f694ca2 3219
0fd13d32 3220 -- Pre/Post
3221
1e3c4ae6 3222 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3223 -- with a first argument that is the expression, and a second
3224 -- argument that is an informative message if the test fails.
3225 -- This is inserted right after the declaration, to get the
5b5df4a9 3226 -- required pragma placement. The processing for the pragmas
3227 -- takes care of the required delay.
ae888dbd 3228
5ddd846b 3229 when Pre_Post_Aspects => Pre_Post : declare
1e3c4ae6 3230 Pname : Name_Id;
ae888dbd 3231
1e3c4ae6 3232 begin
77ae6789 3233 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1e3c4ae6 3234 Pname := Name_Precondition;
3235 else
3236 Pname := Name_Postcondition;
3237 end if;
d74fc39a 3238
26062729 3239 -- Check that the class-wide predicate cannot be applied to
051826ee 3240 -- an operation of a synchronized type. AI12-0182 forbids
3241 -- these altogether, while earlier language semantics made
3242 -- them legal on tagged synchronized types.
3243
3244 -- Other legality checks are performed when analyzing the
3245 -- contract of the operation.
26062729 3246
3247 if Class_Present (Aspect)
3248 and then Is_Concurrent_Type (Current_Scope)
26062729 3249 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3250 then
3251 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3252 Error_Msg_N
3253 ("aspect % can only be specified for a primitive "
3254 & "operation of a tagged type", Aspect);
3255
3256 goto Continue;
3257 end if;
3258
1e3c4ae6 3259 -- If the expressions is of the form A and then B, then
3260 -- we generate separate Pre/Post aspects for the separate
3261 -- clauses. Since we allow multiple pragmas, there is no
3262 -- problem in allowing multiple Pre/Post aspects internally.
a273015d 3263 -- These should be treated in reverse order (B first and
3264 -- A second) since they are later inserted just after N in
3265 -- the order they are treated. This way, the pragma for A
3266 -- ends up preceding the pragma for B, which may have an
3267 -- importance for the error raised (either constraint error
3268 -- or precondition error).
1e3c4ae6 3269
39e1f22f 3270 -- We do not do this for Pre'Class, since we have to put
51fa2a45 3271 -- these conditions together in a complex OR expression.
ae888dbd 3272
4282d342 3273 -- We do not do this in ASIS mode, as ASIS relies on the
3274 -- original node representing the complete expression, when
3275 -- retrieving it through the source aspect table.
3276
3277 if not ASIS_Mode
3278 and then (Pname = Name_Postcondition
3279 or else not Class_Present (Aspect))
39e1f22f 3280 then
3281 while Nkind (Expr) = N_And_Then loop
3282 Insert_After (Aspect,
a273015d 3283 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
39e1f22f 3284 Identifier => Identifier (Aspect),
a273015d 3285 Expression => Relocate_Node (Left_Opnd (Expr)),
39e1f22f 3286 Class_Present => Class_Present (Aspect),
3287 Split_PPC => True));
a273015d 3288 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
39e1f22f 3289 Eloc := Sloc (Expr);
3290 end loop;
3291 end if;
ae888dbd 3292
48d6f069 3293 -- Build the precondition/postcondition pragma
3294
51fa2a45 3295 -- Add note about why we do NOT need Copy_Tree here???
d74fc39a 3296
0fd13d32 3297 Make_Aitem_Pragma
3298 (Pragma_Argument_Associations => New_List (
3299 Make_Pragma_Argument_Association (Eloc,
3300 Chars => Name_Check,
a19e1763 3301 Expression => Relocate_Node (Expr))),
0fd13d32 3302 Pragma_Name => Pname);
39e1f22f 3303
3304 -- Add message unless exception messages are suppressed
3305
3306 if not Opt.Exception_Locations_Suppressed then
3307 Append_To (Pragma_Argument_Associations (Aitem),
3308 Make_Pragma_Argument_Association (Eloc,
ed695684 3309 Chars => Name_Message,
39e1f22f 3310 Expression =>
3311 Make_String_Literal (Eloc,
3312 Strval => "failed "
3313 & Get_Name_String (Pname)
3314 & " from "
3315 & Build_Location_String (Eloc))));
3316 end if;
d74fc39a 3317
7d20685d 3318 Set_Is_Delayed_Aspect (Aspect);
d74fc39a 3319
1e3c4ae6 3320 -- For Pre/Post cases, insert immediately after the entity
3321 -- declaration, since that is the required pragma placement.
3322 -- Note that for these aspects, we do not have to worry
3323 -- about delay issues, since the pragmas themselves deal
3324 -- with delay of visibility for the expression analysis.
3325
e2bf777d 3326 Insert_Pragma (Aitem);
299b347e 3327
1e3c4ae6 3328 goto Continue;
5ddd846b 3329 end Pre_Post;
ae888dbd 3330
0fd13d32 3331 -- Test_Case
3332
e66f4e2a 3333 when Aspect_Test_Case => Test_Case : declare
3334 Args : List_Id;
3335 Comp_Expr : Node_Id;
3336 Comp_Assn : Node_Id;
3337 New_Expr : Node_Id;
57cd943b 3338
e66f4e2a 3339 begin
3340 Args := New_List;
b0bc40fd 3341
e66f4e2a 3342 if Nkind (Parent (N)) = N_Compilation_Unit then
3343 Error_Msg_Name_1 := Nam;
3344 Error_Msg_N ("incorrect placement of aspect `%`", E);
3345 goto Continue;
3346 end if;
6c545057 3347
e66f4e2a 3348 if Nkind (Expr) /= N_Aggregate then
3349 Error_Msg_Name_1 := Nam;
3350 Error_Msg_NE
3351 ("wrong syntax for aspect `%` for &", Id, E);
3352 goto Continue;
3353 end if;
6c545057 3354
e66f4e2a 3355 -- Make pragma expressions refer to the original aspect
51fa2a45 3356 -- expressions through the Original_Node link. This is used
3357 -- in semantic analysis for ASIS mode, so that the original
3358 -- expression also gets analyzed.
e66f4e2a 3359
3360 Comp_Expr := First (Expressions (Expr));
3361 while Present (Comp_Expr) loop
3362 New_Expr := Relocate_Node (Comp_Expr);
e66f4e2a 3363 Append_To (Args,
3364 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3365 Expression => New_Expr));
3366 Next (Comp_Expr);
3367 end loop;
3368
3369 Comp_Assn := First (Component_Associations (Expr));
3370 while Present (Comp_Assn) loop
3371 if List_Length (Choices (Comp_Assn)) /= 1
3372 or else
3373 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3374 then
fad014fe 3375 Error_Msg_Name_1 := Nam;
6c545057 3376 Error_Msg_NE
fad014fe 3377 ("wrong syntax for aspect `%` for &", Id, E);
6c545057 3378 goto Continue;
3379 end if;
3380
e66f4e2a 3381 Append_To (Args,
3382 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
ed695684 3383 Chars => Chars (First (Choices (Comp_Assn))),
3384 Expression =>
3385 Relocate_Node (Expression (Comp_Assn))));
e66f4e2a 3386 Next (Comp_Assn);
3387 end loop;
6c545057 3388
e66f4e2a 3389 -- Build the test-case pragma
6c545057 3390
0fd13d32 3391 Make_Aitem_Pragma
3392 (Pragma_Argument_Associations => Args,
3393 Pragma_Name => Nam);
e66f4e2a 3394 end Test_Case;
85696508 3395
0fd13d32 3396 -- Contract_Cases
3397
5ddd846b 3398 when Aspect_Contract_Cases =>
0fd13d32 3399 Make_Aitem_Pragma
3400 (Pragma_Argument_Associations => New_List (
3401 Make_Pragma_Argument_Association (Loc,
3402 Expression => Relocate_Node (Expr))),
3403 Pragma_Name => Nam);
3a128918 3404
e2bf777d 3405 Decorate (Aspect, Aitem);
3406 Insert_Pragma (Aitem);
5ddd846b 3407 goto Continue;
3a128918 3408
89f1e35c 3409 -- Case 5: Special handling for aspects with an optional
3410 -- boolean argument.
85696508 3411
6c5793cd 3412 -- In the delayed case, the corresponding pragma cannot be
0fd13d32 3413 -- generated yet because the evaluation of the boolean needs
3414 -- to be delayed till the freeze point.
3415
99378362 3416 when Boolean_Aspects
3417 | Library_Unit_Aspects
3418 =>
89f1e35c 3419 Set_Is_Boolean_Aspect (Aspect);
a5a64273 3420
89f1e35c 3421 -- Lock_Free aspect only apply to protected objects
e1cedbae 3422
89f1e35c 3423 if A_Id = Aspect_Lock_Free then
3424 if Ekind (E) /= E_Protected_Type then
99a2d5bd 3425 Error_Msg_Name_1 := Nam;
a5a64273 3426 Error_Msg_N
89f1e35c 3427 ("aspect % only applies to a protected object",
3428 Aspect);
3429
3430 else
3431 -- Set the Uses_Lock_Free flag to True if there is no
37c6e44c 3432 -- expression or if the expression is True. The
89f1e35c 3433 -- evaluation of this aspect should be delayed to the
37c6e44c 3434 -- freeze point (why???)
89f1e35c 3435
e81df51c 3436 if No (Expr)
3437 or else Is_True (Static_Boolean (Expr))
89f1e35c 3438 then
3439 Set_Uses_Lock_Free (E);
3440 end if;
caf125ce 3441
3442 Record_Rep_Item (E, Aspect);
a5a64273 3443 end if;
e1cedbae 3444
89f1e35c 3445 goto Continue;
ae888dbd 3446
ee2b7923 3447 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3448 Analyze_Aspect_Export_Import;
6c5793cd 3449
3450 -- Disable_Controlled
3451
3452 elsif A_Id = Aspect_Disable_Controlled then
3453 if Ekind (E) /= E_Record_Type
3454 or else not Is_Controlled (E)
3455 then
3456 Error_Msg_N
3457 ("aspect % requires controlled record type", Aspect);
3458 goto Continue;
3459 end if;
3460
3f716509 3461 -- If we're in a generic template, we don't want to try
3462 -- to disable controlled types, because typical usage is
3463 -- "Disable_Controlled => not <some_check>'Enabled", and
3464 -- the value of Enabled is not known until we see a
7e2d3667 3465 -- particular instance. In such a context, we just need
3466 -- to preanalyze the expression for legality.
3f716509 3467
3468 if Expander_Active then
aae9bc79 3469 Analyze_And_Resolve (Expr, Standard_Boolean);
3470
3f716509 3471 if not Present (Expr)
3472 or else Is_True (Static_Boolean (Expr))
3473 then
3474 Set_Disable_Controlled (E);
3475 end if;
7e2d3667 3476
3477 elsif Serious_Errors_Detected = 0 then
3478 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6c5793cd 3479 end if;
3480
89f1e35c 3481 goto Continue;
3482 end if;
d74fc39a 3483
37c6e44c 3484 -- Library unit aspects require special handling in the case
3485 -- of a package declaration, the pragma needs to be inserted
3486 -- in the list of declarations for the associated package.
3487 -- There is no issue of visibility delay for these aspects.
d64221a7 3488
89f1e35c 3489 if A_Id in Library_Unit_Aspects
178fec9b 3490 and then
3491 Nkind_In (N, N_Package_Declaration,
3492 N_Generic_Package_Declaration)
89f1e35c 3493 and then Nkind (Parent (N)) /= N_Compilation_Unit
3ad60f63 3494
3495 -- Aspect is legal on a local instantiation of a library-
3496 -- level generic unit.
3497
b94a633e 3498 and then not Is_Generic_Instance (Defining_Entity (N))
89f1e35c 3499 then
3500 Error_Msg_N
dd4c44af 3501 ("incorrect context for library unit aspect&", Id);
89f1e35c 3502 goto Continue;
3503 end if;
cce84b09 3504
51fa2a45 3505 -- Cases where we do not delay, includes all cases where the
3506 -- expression is missing other than the above cases.
d74fc39a 3507
85ee12c0 3508 if not Delay_Required or else No (Expr) then
ee2b7923 3509
3510 -- Exclude aspects Export and Import because their pragma
3511 -- syntax does not map directly to a Boolean aspect.
3512
3513 if A_Id /= Aspect_Export
3514 and then A_Id /= Aspect_Import
3515 then
3516 Make_Aitem_Pragma
3517 (Pragma_Argument_Associations => New_List (
3518 Make_Pragma_Argument_Association (Sloc (Ent),
3519 Expression => Ent)),
3520 Pragma_Name => Chars (Id));
3521 end if;
3522
89f1e35c 3523 Delay_Required := False;
ddf1337b 3524
89f1e35c 3525 -- In general cases, the corresponding pragma/attribute
3526 -- definition clause will be inserted later at the freezing
294709fa 3527 -- point, and we do not need to build it now.
ddf1337b 3528
89f1e35c 3529 else
3530 Aitem := Empty;
3531 end if;
ceec4f7c 3532
3533 -- Storage_Size
3534
3535 -- This is special because for access types we need to generate
3536 -- an attribute definition clause. This also works for single
3537 -- task declarations, but it does not work for task type
3538 -- declarations, because we have the case where the expression
3539 -- references a discriminant of the task type. That can't use
3540 -- an attribute definition clause because we would not have
3541 -- visibility on the discriminant. For that case we must
3542 -- generate a pragma in the task definition.
3543
3544 when Aspect_Storage_Size =>
3545
3546 -- Task type case
3547
3548 if Ekind (E) = E_Task_Type then
3549 declare
3550 Decl : constant Node_Id := Declaration_Node (E);
3551
3552 begin
3553 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3554
3555 -- If no task definition, create one
3556
3557 if No (Task_Definition (Decl)) then
3558 Set_Task_Definition (Decl,
3559 Make_Task_Definition (Loc,
3560 Visible_Declarations => Empty_List,
3561 End_Label => Empty));
3562 end if;
3563
51fa2a45 3564 -- Create a pragma and put it at the start of the task
3565 -- definition for the task type declaration.
ceec4f7c 3566
3567 Make_Aitem_Pragma
3568 (Pragma_Argument_Associations => New_List (
3569 Make_Pragma_Argument_Association (Loc,
3570 Expression => Relocate_Node (Expr))),
3571 Pragma_Name => Name_Storage_Size);
3572
3573 Prepend
3574 (Aitem,
3575 Visible_Declarations (Task_Definition (Decl)));
3576 goto Continue;
3577 end;
3578
3579 -- All other cases, generate attribute definition
3580
3581 else
3582 Aitem :=
3583 Make_Attribute_Definition_Clause (Loc,
3584 Name => Ent,
3585 Chars => Chars (Id),
3586 Expression => Relocate_Node (Expr));
3587 end if;
89f1e35c 3588 end case;
ddf1337b 3589
89f1e35c 3590 -- Attach the corresponding pragma/attribute definition clause to
3591 -- the aspect specification node.
d74fc39a 3592
89f1e35c 3593 if Present (Aitem) then
e2bf777d 3594 Set_From_Aspect_Specification (Aitem);
89f1e35c 3595 end if;
53c179ea 3596
89f1e35c 3597 -- In the context of a compilation unit, we directly put the
0fd13d32 3598 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3599 -- node (no delay is required here) except for aspects on a
51fa2a45 3600 -- subprogram body (see below) and a generic package, for which we
3601 -- need to introduce the pragma before building the generic copy
3602 -- (see sem_ch12), and for package instantiations, where the
3603 -- library unit pragmas are better handled early.
ddf1337b 3604
9129c28f 3605 if Nkind (Parent (N)) = N_Compilation_Unit
89f1e35c 3606 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3607 then
3608 declare
3609 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
7f694ca2 3610
89f1e35c 3611 begin
3612 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
7f694ca2 3613
89f1e35c 3614 -- For a Boolean aspect, create the corresponding pragma if
3615 -- no expression or if the value is True.
7f694ca2 3616
b9e61b2a 3617 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
89f1e35c 3618 if Is_True (Static_Boolean (Expr)) then
0fd13d32 3619 Make_Aitem_Pragma
3620 (Pragma_Argument_Associations => New_List (
3621 Make_Pragma_Argument_Association (Sloc (Ent),
3622 Expression => Ent)),
3623 Pragma_Name => Chars (Id));
7f694ca2 3624
89f1e35c 3625 Set_From_Aspect_Specification (Aitem, True);
3626 Set_Corresponding_Aspect (Aitem, Aspect);
3627
3628 else
3629 goto Continue;
3630 end if;
3631 end if;
7f694ca2 3632
d6814978 3633 -- If the aspect is on a subprogram body (relevant aspect
3634 -- is Inline), add the pragma in front of the declarations.
3a72f9c3 3635
3636 if Nkind (N) = N_Subprogram_Body then
3637 if No (Declarations (N)) then
3638 Set_Declarations (N, New_List);
3639 end if;
3640
3641 Prepend (Aitem, Declarations (N));
3642
178fec9b 3643 elsif Nkind (N) = N_Generic_Package_Declaration then
3644 if No (Visible_Declarations (Specification (N))) then
3645 Set_Visible_Declarations (Specification (N), New_List);
3646 end if;
3647
3648 Prepend (Aitem,
3649 Visible_Declarations (Specification (N)));
3650
c39cce40 3651 elsif Nkind (N) = N_Package_Instantiation then
df8b0dae 3652 declare
3653 Spec : constant Node_Id :=
3654 Specification (Instance_Spec (N));
3655 begin
3656 if No (Visible_Declarations (Spec)) then
3657 Set_Visible_Declarations (Spec, New_List);
3658 end if;
3659
3660 Prepend (Aitem, Visible_Declarations (Spec));
3661 end;
3662
3a72f9c3 3663 else
3664 if No (Pragmas_After (Aux)) then
d4596fbe 3665 Set_Pragmas_After (Aux, New_List);
3a72f9c3 3666 end if;
3667
3668 Append (Aitem, Pragmas_After (Aux));
89f1e35c 3669 end if;
7f694ca2 3670
89f1e35c 3671 goto Continue;
3672 end;
3673 end if;
7f694ca2 3674
89f1e35c 3675 -- The evaluation of the aspect is delayed to the freezing point.
3676 -- The pragma or attribute clause if there is one is then attached
37c6e44c 3677 -- to the aspect specification which is put in the rep item list.
1a814552 3678
89f1e35c 3679 if Delay_Required then
3680 if Present (Aitem) then
3681 Set_Is_Delayed_Aspect (Aitem);
3682 Set_Aspect_Rep_Item (Aspect, Aitem);
3683 Set_Parent (Aitem, Aspect);
3684 end if;
1a814552 3685
89f1e35c 3686 Set_Is_Delayed_Aspect (Aspect);
9f36e3fb 3687
cba2ae82 3688 -- In the case of Default_Value, link the aspect to base type
3689 -- as well, even though it appears on a first subtype. This is
3690 -- mandated by the semantics of the aspect. Do not establish
3691 -- the link when processing the base type itself as this leads
3692 -- to a rep item circularity. Verify that we are dealing with
3693 -- a scalar type to prevent cascaded errors.
3694
3695 if A_Id = Aspect_Default_Value
3696 and then Is_Scalar_Type (E)
3697 and then Base_Type (E) /= E
3698 then
9f36e3fb 3699 Set_Has_Delayed_Aspects (Base_Type (E));
3700 Record_Rep_Item (Base_Type (E), Aspect);
3701 end if;
3702
89f1e35c 3703 Set_Has_Delayed_Aspects (E);
3704 Record_Rep_Item (E, Aspect);
ddf1337b 3705
b855559d 3706 -- When delay is not required and the context is a package or a
3707 -- subprogram body, insert the pragma in the body declarations.
f55ce169 3708
b855559d 3709 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
f55ce169 3710 if No (Declarations (N)) then
3711 Set_Declarations (N, New_List);
3712 end if;
3713
3714 -- The pragma is added before source declarations
3715
3716 Prepend_To (Declarations (N), Aitem);
3717
89f1e35c 3718 -- When delay is not required and the context is not a compilation
3719 -- unit, we simply insert the pragma/attribute definition clause
3720 -- in sequence.
ddf1337b 3721
ee2b7923 3722 elsif Present (Aitem) then
89f1e35c 3723 Insert_After (Ins_Node, Aitem);
3724 Ins_Node := Aitem;
d74fc39a 3725 end if;
0fd13d32 3726 end Analyze_One_Aspect;
ae888dbd 3727
d64221a7 3728 <<Continue>>
3729 Next (Aspect);
21ea3a4f 3730 end loop Aspect_Loop;
89f1e35c 3731
3732 if Has_Delayed_Aspects (E) then
3733 Ensure_Freeze_Node (E);
3734 end if;
21ea3a4f 3735 end Analyze_Aspect_Specifications;
ae888dbd 3736
eb8aeefc 3737 ---------------------------------------------------
3738 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3739 ---------------------------------------------------
3740
3741 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3742 Body_Id : constant Entity_Id := Defining_Entity (N);
3743
3744 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
c02dccca 3745 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3746 -- error message depending on the aspects involved. Spec_Id denotes the
3747 -- entity of the corresponding spec.
eb8aeefc 3748
3749 --------------------------------
3750 -- Diagnose_Misplaced_Aspects --
3751 --------------------------------
3752
3753 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3754 procedure Misplaced_Aspect_Error
3755 (Asp : Node_Id;
3756 Ref_Nam : Name_Id);
3757 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3758 -- the name of the refined version of the aspect.
3759
3760 ----------------------------
3761 -- Misplaced_Aspect_Error --
3762 ----------------------------
3763
3764 procedure Misplaced_Aspect_Error
3765 (Asp : Node_Id;
3766 Ref_Nam : Name_Id)
3767 is
3768 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3769 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3770
3771 begin
3772 -- The corresponding spec already contains the aspect in question
3773 -- and the one appearing on the body must be the refined form:
3774
3775 -- procedure P with Global ...;
3776 -- procedure P with Global ... is ... end P;
3777 -- ^
3778 -- Refined_Global
3779
3780 if Has_Aspect (Spec_Id, Asp_Id) then
3781 Error_Msg_Name_1 := Asp_Nam;
3782
3783 -- Subunits cannot carry aspects that apply to a subprogram
3784 -- declaration.
3785
3786 if Nkind (Parent (N)) = N_Subunit then
3787 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3788
3789 -- Otherwise suggest the refined form
3790
3791 else
3792 Error_Msg_Name_2 := Ref_Nam;
3793 Error_Msg_N ("aspect % should be %", Asp);
3794 end if;
3795
3796 -- Otherwise the aspect must appear on the spec, not on the body
3797
3798 -- procedure P;
3799 -- procedure P with Global ... is ... end P;
3800
3801 else
3802 Error_Msg_N
c02dccca 3803 ("aspect specification must appear on initial declaration",
eb8aeefc 3804 Asp);
3805 end if;
3806 end Misplaced_Aspect_Error;
3807
3808 -- Local variables
3809
3810 Asp : Node_Id;
3811 Asp_Nam : Name_Id;
3812
3813 -- Start of processing for Diagnose_Misplaced_Aspects
3814
3815 begin
3816 -- Iterate over the aspect specifications and emit specific errors
3817 -- where applicable.
3818
3819 Asp := First (Aspect_Specifications (N));
3820 while Present (Asp) loop
3821 Asp_Nam := Chars (Identifier (Asp));
3822
3823 -- Do not emit errors on aspects that can appear on a subprogram
3824 -- body. This scenario occurs when the aspect specification list
3825 -- contains both misplaced and properly placed aspects.
3826
3827 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3828 null;
3829
3830 -- Special diagnostics for SPARK aspects
3831
3832 elsif Asp_Nam = Name_Depends then
3833 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3834
3835 elsif Asp_Nam = Name_Global then
3836 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3837
3838 elsif Asp_Nam = Name_Post then
3839 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3840
3841 -- Otherwise a language-defined aspect is misplaced
3842
3843 else
3844 Error_Msg_N
c02dccca 3845 ("aspect specification must appear on initial declaration",
eb8aeefc 3846 Asp);
3847 end if;
3848
3849 Next (Asp);
3850 end loop;
3851 end Diagnose_Misplaced_Aspects;
3852
3853 -- Local variables
3854
c02dccca 3855 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
eb8aeefc 3856
3857 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3858
3859 begin
eb8aeefc 3860 -- Language-defined aspects cannot be associated with a subprogram body
3861 -- [stub] if the subprogram has a spec. Certain implementation defined
3862 -- aspects are allowed to break this rule (for all applicable cases, see
3863 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3864
c02dccca 3865 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
eb8aeefc 3866 Diagnose_Misplaced_Aspects (Spec_Id);
3867 else
3868 Analyze_Aspect_Specifications (N, Body_Id);
3869 end if;
3870 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3871
d6f39728 3872 -----------------------
3873 -- Analyze_At_Clause --
3874 -----------------------
3875
3876 -- An at clause is replaced by the corresponding Address attribute
3877 -- definition clause that is the preferred approach in Ada 95.
3878
3879 procedure Analyze_At_Clause (N : Node_Id) is
177675a7 3880 CS : constant Boolean := Comes_From_Source (N);
3881
d6f39728 3882 begin
177675a7 3883 -- This is an obsolescent feature
3884
e0521a36 3885 Check_Restriction (No_Obsolescent_Features, N);
3886
9dfe12ae 3887 if Warn_On_Obsolescent_Feature then
3888 Error_Msg_N
b174444e 3889 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
9dfe12ae 3890 Error_Msg_N
b174444e 3891 ("\?j?use address attribute definition clause instead", N);
9dfe12ae 3892 end if;
3893
177675a7 3894 -- Rewrite as address clause
3895
d6f39728 3896 Rewrite (N,
3897 Make_Attribute_Definition_Clause (Sloc (N),
935e86e0 3898 Name => Identifier (N),
3899 Chars => Name_Address,
d6f39728 3900 Expression => Expression (N)));
177675a7 3901
2beb22b1 3902 -- We preserve Comes_From_Source, since logically the clause still comes
3903 -- from the source program even though it is changed in form.
177675a7 3904
3905 Set_Comes_From_Source (N, CS);
3906
3907 -- Analyze rewritten clause
3908
d6f39728 3909 Analyze_Attribute_Definition_Clause (N);
3910 end Analyze_At_Clause;
3911
3912 -----------------------------------------
3913 -- Analyze_Attribute_Definition_Clause --
3914 -----------------------------------------
3915
3916 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3917 Loc : constant Source_Ptr := Sloc (N);
3918 Nam : constant Node_Id := Name (N);
3919 Attr : constant Name_Id := Chars (N);
3920 Expr : constant Node_Id := Expression (N);
3921 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
d64221a7 3922
3923 Ent : Entity_Id;
3924 -- The entity of Nam after it is analyzed. In the case of an incomplete
3925 -- type, this is the underlying type.
3926
d6f39728 3927 U_Ent : Entity_Id;
d64221a7 3928 -- The underlying entity to which the attribute applies. Generally this
3929 -- is the Underlying_Type of Ent, except in the case where the clause
69069c76 3930 -- applies to the full view of an incomplete or private type, in which
3931 -- case U_Ent is just a copy of Ent.
d6f39728 3932
3933 FOnly : Boolean := False;
3934 -- Reset to True for subtype specific attribute (Alignment, Size)
51fa2a45 3935 -- and for stream attributes, i.e. those cases where in the call to
3936 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3937 -- are checked. Note that the case of stream attributes is not clear
3938 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3939 -- Storage_Size for derived task types, but that is also clearly
3940 -- unintentional.
d6f39728 3941
9f373bb8 3942 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3943 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3944 -- definition clauses.
3945
ae888dbd 3946 function Duplicate_Clause return Boolean;
3947 -- This routine checks if the aspect for U_Ent being given by attribute
3948 -- definition clause N is for an aspect that has already been specified,
3949 -- and if so gives an error message. If there is a duplicate, True is
3950 -- returned, otherwise if there is no error, False is returned.
3951
81b424ac 3952 procedure Check_Indexing_Functions;
3953 -- Check that the function in Constant_Indexing or Variable_Indexing
3954 -- attribute has the proper type structure. If the name is overloaded,
cac18f71 3955 -- check that some interpretation is legal.
81b424ac 3956
89cc7147 3957 procedure Check_Iterator_Functions;
3958 -- Check that there is a single function in Default_Iterator attribute
8df4f2a5 3959 -- has the proper type structure.
89cc7147 3960
3961 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
d03bfaa1 3962 -- Common legality check for the previous two
89cc7147 3963
177675a7 3964 -----------------------------------
3965 -- Analyze_Stream_TSS_Definition --
3966 -----------------------------------
3967
9f373bb8 3968 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3969 Subp : Entity_Id := Empty;
3970 I : Interp_Index;
3971 It : Interp;
3972 Pnam : Entity_Id;
3973
3974 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
ba662f09 3975 -- True for Read attribute, False for other attributes
9f373bb8 3976
c41e404d 3977 function Has_Good_Profile
3978 (Subp : Entity_Id;
3979 Report : Boolean := False) return Boolean;
9f373bb8 3980 -- Return true if the entity is a subprogram with an appropriate
ba662f09 3981 -- profile for the attribute being defined. If result is False and
3982 -- Report is True, function emits appropriate error.
9f373bb8 3983
3984 ----------------------
3985 -- Has_Good_Profile --
3986 ----------------------
3987
c41e404d 3988 function Has_Good_Profile
3989 (Subp : Entity_Id;
3990 Report : Boolean := False) return Boolean
3991 is
9f373bb8 3992 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3993 (False => E_Procedure, True => E_Function);
4a83cc35 3994 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3995 F : Entity_Id;
9f373bb8 3996 Typ : Entity_Id;
3997
3998 begin
3999 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
4000 return False;
4001 end if;
4002
4003 F := First_Formal (Subp);
4004
4005 if No (F)
4006 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
4007 or else Designated_Type (Etype (F)) /=
4a83cc35 4008 Class_Wide_Type (RTE (RE_Root_Stream_Type))
9f373bb8 4009 then
4010 return False;
4011 end if;
4012
4013 if not Is_Function then
4014 Next_Formal (F);
4015
4016 declare
4017 Expected_Mode : constant array (Boolean) of Entity_Kind :=
4018 (False => E_In_Parameter,
4019 True => E_Out_Parameter);
4020 begin
4021 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
4022 return False;
4023 end if;
4024 end;
4025
4026 Typ := Etype (F);
4027
b64082f2 4028 -- If the attribute specification comes from an aspect
51fa2a45 4029 -- specification for a class-wide stream, the parameter must be
4030 -- a class-wide type of the entity to which the aspect applies.
b64082f2 4031
4032 if From_Aspect_Specification (N)
4033 and then Class_Present (Parent (N))
4034 and then Is_Class_Wide_Type (Typ)
4035 then
4036 Typ := Etype (Typ);
4037 end if;
4038
9f373bb8 4039 else
4040 Typ := Etype (Subp);
4041 end if;
4042
51fa2a45 4043 -- Verify that the prefix of the attribute and the local name for
5a8fe506 4044 -- the type of the formal match, or one is the class-wide of the
4045 -- other, in the case of a class-wide stream operation.
48680a09 4046
b8eacb12 4047 if Base_Type (Typ) = Base_Type (Ent)
5a8fe506 4048 or else (Is_Class_Wide_Type (Typ)
2be1f7d7 4049 and then Typ = Class_Wide_Type (Base_Type (Ent)))
fbf4d6ef 4050 or else (Is_Class_Wide_Type (Ent)
4051 and then Ent = Class_Wide_Type (Base_Type (Typ)))
5a8fe506 4052 then
4053 null;
4054 else
4055 return False;
4056 end if;
4057
4a83cc35 4058 if Present (Next_Formal (F)) then
48680a09 4059 return False;
4060
4061 elsif not Is_Scalar_Type (Typ)
4062 and then not Is_First_Subtype (Typ)
4063 and then not Is_Class_Wide_Type (Typ)
4064 then
c41e404d 4065 if Report and not Is_First_Subtype (Typ) then
4066 Error_Msg_N
ba662f09 4067 ("subtype of formal in stream operation must be a first "
4068 & "subtype", Parameter_Type (Parent (F)));
c41e404d 4069 end if;
4070
48680a09 4071 return False;
4072
4073 else
4074 return True;
4075 end if;
9f373bb8 4076 end Has_Good_Profile;
4077
4078 -- Start of processing for Analyze_Stream_TSS_Definition
4079
4080 begin
4081 FOnly := True;
4082
4083 if not Is_Type (U_Ent) then
4084 Error_Msg_N ("local name must be a subtype", Nam);
4085 return;
48680a09 4086
4087 elsif not Is_First_Subtype (U_Ent) then
4088 Error_Msg_N ("local name must be a first subtype", Nam);
4089 return;
9f373bb8 4090 end if;
4091
4092 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
4093
44e4341e 4094 -- If Pnam is present, it can be either inherited from an ancestor
4095 -- type (in which case it is legal to redefine it for this type), or
4096 -- be a previous definition of the attribute for the same type (in
4097 -- which case it is illegal).
4098
4099 -- In the first case, it will have been analyzed already, and we
4100 -- can check that its profile does not match the expected profile
4101 -- for a stream attribute of U_Ent. In the second case, either Pnam
4102 -- has been analyzed (and has the expected profile), or it has not
4103 -- been analyzed yet (case of a type that has not been frozen yet
4104 -- and for which the stream attribute has been set using Set_TSS).
4105
4106 if Present (Pnam)
4107 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4108 then
9f373bb8 4109 Error_Msg_Sloc := Sloc (Pnam);
4110 Error_Msg_Name_1 := Attr;
4111 Error_Msg_N ("% attribute already defined #", Nam);
4112 return;
4113 end if;
4114
4115 Analyze (Expr);
4116
4117 if Is_Entity_Name (Expr) then
4118 if not Is_Overloaded (Expr) then
c41e404d 4119 if Has_Good_Profile (Entity (Expr), Report => True) then
9f373bb8 4120 Subp := Entity (Expr);
4121 end if;
4122
4123 else
4124 Get_First_Interp (Expr, I, It);
9f373bb8 4125 while Present (It.Nam) loop
4126 if Has_Good_Profile (It.Nam) then
4127 Subp := It.Nam;
4128 exit;
4129 end if;
4130
4131 Get_Next_Interp (I, It);
4132 end loop;
4133 end if;
4134 end if;
4135
4136 if Present (Subp) then
59ac57b5 4137 if Is_Abstract_Subprogram (Subp) then
9f373bb8 4138 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4139 return;
e12b2502 4140
299b347e 4141 -- A stream subprogram for an interface type must be a null
bfbd9cf4 4142 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4143 -- of an interface is not an interface type (3.9.4 (6.b/2)).
e12b2502 4144
4145 elsif Is_Interface (U_Ent)
5a8fe506 4146 and then not Is_Class_Wide_Type (U_Ent)
e12b2502 4147 and then not Inside_A_Generic
e12b2502 4148 and then
5a8fe506 4149 (Ekind (Subp) = E_Function
4150 or else
4151 not Null_Present
2be1f7d7 4152 (Specification
4153 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
e12b2502 4154 then
4155 Error_Msg_N
4a83cc35 4156 ("stream subprogram for interface type must be null "
4157 & "procedure", Expr);
9f373bb8 4158 end if;
4159
4160 Set_Entity (Expr, Subp);
4161 Set_Etype (Expr, Etype (Subp));
4162
44e4341e 4163 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
9f373bb8 4164
4165 else
4166 Error_Msg_Name_1 := Attr;
4167 Error_Msg_N ("incorrect expression for% attribute", Expr);
4168 end if;
4169 end Analyze_Stream_TSS_Definition;
4170
81b424ac 4171 ------------------------------
4172 -- Check_Indexing_Functions --
4173 ------------------------------
4174
4175 procedure Check_Indexing_Functions is
c8a2d809 4176 Indexing_Found : Boolean := False;
8df4f2a5 4177
44d567c8 4178 procedure Check_Inherited_Indexing;
4179 -- For a derived type, check that no indexing aspect is specified
4180 -- for the type if it is also inherited
4181
81b424ac 4182 procedure Check_One_Function (Subp : Entity_Id);
7796365f 4183 -- Check one possible interpretation. Sets Indexing_Found True if a
4184 -- legal indexing function is found.
81b424ac 4185
05987af3 4186 procedure Illegal_Indexing (Msg : String);
4187 -- Diagnose illegal indexing function if not overloaded. In the
4188 -- overloaded case indicate that no legal interpretation exists.
4189
44d567c8 4190 ------------------------------
4191 -- Check_Inherited_Indexing --
4192 ------------------------------
4193
4194 procedure Check_Inherited_Indexing is
4195 Inherited : Node_Id;
4196
4197 begin
4198 if Attr = Name_Constant_Indexing then
4199 Inherited :=
4200 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4201 else pragma Assert (Attr = Name_Variable_Indexing);
4202 Inherited :=
4203 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4204 end if;
4205
4206 if Present (Inherited) then
4207 if Debug_Flag_Dot_XX then
4208 null;
4209
83d39cd3 4210 -- OK if current attribute_definition_clause is expansion of
4211 -- inherited aspect.
44d567c8 4212
4213 elsif Aspect_Rep_Item (Inherited) = N then
4214 null;
4215
83d39cd3 4216 -- Indicate the operation that must be overridden, rather than
4217 -- redefining the indexing aspect.
44d567c8 4218
4219 else
4220 Illegal_Indexing
f2837ceb 4221 ("indexing function already inherited from parent type");
44d567c8 4222 Error_Msg_NE
4223 ("!override & instead",
4224 N, Entity (Expression (Inherited)));
4225 end if;
4226 end if;
4227 end Check_Inherited_Indexing;
4228
81b424ac 4229 ------------------------
4230 -- Check_One_Function --
4231 ------------------------
4232
4233 procedure Check_One_Function (Subp : Entity_Id) is
05987af3 4234 Default_Element : Node_Id;
4235 Ret_Type : constant Entity_Id := Etype (Subp);
1b7510f9 4236
81b424ac 4237 begin
05987af3 4238 if not Is_Overloadable (Subp) then
4239 Illegal_Indexing ("illegal indexing function for type&");
4240 return;
4241
7796365f 4242 elsif Scope (Subp) /= Scope (Ent) then
4243 if Nkind (Expr) = N_Expanded_Name then
4244
4245 -- Indexing function can't be declared elsewhere
4246
4247 Illegal_Indexing
4248 ("indexing function must be declared in scope of type&");
4249 end if;
4250
05987af3 4251 return;
4252
4253 elsif No (First_Formal (Subp)) then
4254 Illegal_Indexing
4255 ("Indexing requires a function that applies to type&");
4256 return;
4257
4258 elsif No (Next_Formal (First_Formal (Subp))) then
4259 Illegal_Indexing
2eb0ff42 4260 ("indexing function must have at least two parameters");
05987af3 4261 return;
4262
4263 elsif Is_Derived_Type (Ent) then
44d567c8 4264 Check_Inherited_Indexing;
05987af3 4265 end if;
4266
e81df51c 4267 if not Check_Primitive_Function (Subp) then
05987af3 4268 Illegal_Indexing
4269 ("Indexing aspect requires a function that applies to type&");
4270 return;
81b424ac 4271 end if;
4272
7796365f 4273 -- If partial declaration exists, verify that it is not tagged.
4274
4275 if Ekind (Current_Scope) = E_Package
4276 and then Has_Private_Declaration (Ent)
4277 and then From_Aspect_Specification (N)
7c0c95b8 4278 and then
4279 List_Containing (Parent (Ent)) =
4280 Private_Declarations
7796365f 4281 (Specification (Unit_Declaration_Node (Current_Scope)))
4282 and then Nkind (N) = N_Attribute_Definition_Clause
4283 then
4284 declare
4285 Decl : Node_Id;
4286
4287 begin
4288 Decl :=
4289 First (Visible_Declarations
7c0c95b8 4290 (Specification
4291 (Unit_Declaration_Node (Current_Scope))));
7796365f 4292
4293 while Present (Decl) loop
4294 if Nkind (Decl) = N_Private_Type_Declaration
4295 and then Ent = Full_View (Defining_Identifier (Decl))
4296 and then Tagged_Present (Decl)
4297 and then No (Aspect_Specifications (Decl))
4298 then
4299 Illegal_Indexing
4300 ("Indexing aspect cannot be specified on full view "
7c0c95b8 4301 & "if partial view is tagged");
7796365f 4302 return;
4303 end if;
4304
4305 Next (Decl);
4306 end loop;
4307 end;
4308 end if;
4309
1b7510f9 4310 -- An indexing function must return either the default element of
cac18f71 4311 -- the container, or a reference type. For variable indexing it
a45d946f 4312 -- must be the latter.
1b7510f9 4313
05987af3 4314 Default_Element :=
4315 Find_Value_Of_Aspect
4316 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4317
1b7510f9 4318 if Present (Default_Element) then
4319 Analyze (Default_Element);
a45d946f 4320
1b7510f9 4321 if Is_Entity_Name (Default_Element)
05987af3 4322 and then not Covers (Entity (Default_Element), Ret_Type)
4323 and then False
1b7510f9 4324 then
05987af3 4325 Illegal_Indexing
4326 ("wrong return type for indexing function");
1b7510f9 4327 return;
4328 end if;
4329 end if;
4330
a45d946f 4331 -- For variable_indexing the return type must be a reference type
1b7510f9 4332
05987af3 4333 if Attr = Name_Variable_Indexing then
4334 if not Has_Implicit_Dereference (Ret_Type) then
4335 Illegal_Indexing
4336 ("variable indexing must return a reference type");
4337 return;
4338
423b89fd 4339 elsif Is_Access_Constant
4340 (Etype (First_Discriminant (Ret_Type)))
05987af3 4341 then
4342 Illegal_Indexing
4343 ("variable indexing must return an access to variable");
4344 return;
4345 end if;
cac18f71 4346
4347 else
05987af3 4348 if Has_Implicit_Dereference (Ret_Type)
4349 and then not
4350 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4351 then
4352 Illegal_Indexing
4353 ("constant indexing must return an access to constant");
4354 return;
4355
4356 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4357 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4358 then
4359 Illegal_Indexing
4360 ("constant indexing must apply to an access to constant");
4361 return;
4362 end if;
81b424ac 4363 end if;
05987af3 4364
4365 -- All checks succeeded.
4366
4367 Indexing_Found := True;
81b424ac 4368 end Check_One_Function;
4369
05987af3 4370 -----------------------
4371 -- Illegal_Indexing --
4372 -----------------------
4373
4374 procedure Illegal_Indexing (Msg : String) is
4375 begin
7796365f 4376 Error_Msg_NE (Msg, N, Ent);
05987af3 4377 end Illegal_Indexing;
4378
81b424ac 4379 -- Start of processing for Check_Indexing_Functions
4380
4381 begin
89cc7147 4382 if In_Instance then
44d567c8 4383 Check_Inherited_Indexing;
89cc7147 4384 end if;
4385
81b424ac 4386 Analyze (Expr);
4387
4388 if not Is_Overloaded (Expr) then
4389 Check_One_Function (Entity (Expr));
4390
4391 else
4392 declare
2c5754de 4393 I : Interp_Index;
81b424ac 4394 It : Interp;
4395
4396 begin
cac18f71 4397 Indexing_Found := False;
81b424ac 4398 Get_First_Interp (Expr, I, It);
4399 while Present (It.Nam) loop
4400
4401 -- Note that analysis will have added the interpretation
4402 -- that corresponds to the dereference. We only check the
1ef2e6ef 4403 -- subprogram itself. Ignore homonyms that may come from
4404 -- derived types in the context.
81b424ac 4405
1ef2e6ef 4406 if Is_Overloadable (It.Nam)
4407 and then Comes_From_Source (It.Nam)
4408 then
4409 Check_One_Function (It.Nam);
81b424ac 4410 end if;
4411
4412 Get_Next_Interp (I, It);
4413 end loop;
4414 end;
4415 end if;
7796365f 4416
7c0c95b8 4417 if not Indexing_Found and then not Error_Posted (N) then
7796365f 4418 Error_Msg_NE
1ef2e6ef 4419 ("aspect Indexing requires a local function that applies to "
4420 & "type&", Expr, Ent);
7796365f 4421 end if;
81b424ac 4422 end Check_Indexing_Functions;
4423
89cc7147 4424 ------------------------------
4425 -- Check_Iterator_Functions --
4426 ------------------------------
4427
4428 procedure Check_Iterator_Functions is
89cc7147 4429 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
8df4f2a5 4430 -- Check one possible interpretation for validity
89cc7147 4431
4432 ----------------------------
4433 -- Valid_Default_Iterator --
4434 ----------------------------
4435
4436 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
8b8be176 4437 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
7f5dd8d8 4438 Formal : Entity_Id;
89cc7147 4439
4440 begin
4441 if not Check_Primitive_Function (Subp) then
4442 return False;
8b8be176 4443
4444 -- The return type must be derived from a type in an instance
4445 -- of Iterator.Interfaces, and thus its root type must have a
4446 -- predefined name.
4447
4448 elsif Chars (Root_T) /= Name_Forward_Iterator
4449 and then Chars (Root_T) /= Name_Reversible_Iterator
4450 then
4451 return False;
4452
89cc7147 4453 else
4454 Formal := First_Formal (Subp);
4455 end if;
4456
8df4f2a5 4457 -- False if any subsequent formal has no default expression
89cc7147 4458
8df4f2a5 4459 Formal := Next_Formal (Formal);
4460 while Present (Formal) loop
4461 if No (Expression (Parent (Formal))) then
4462 return False;
4463 end if;
89cc7147 4464
8df4f2a5 4465 Next_Formal (Formal);
4466 end loop;
89cc7147 4467
8df4f2a5 4468 -- True if all subsequent formals have default expressions
89cc7147 4469
4470 return True;
4471 end Valid_Default_Iterator;
4472
4473 -- Start of processing for Check_Iterator_Functions
4474
4475 begin
4476 Analyze (Expr);
4477
4478 if not Is_Entity_Name (Expr) then
4479 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4480 end if;
4481
4482 if not Is_Overloaded (Expr) then
4483 if not Check_Primitive_Function (Entity (Expr)) then
4484 Error_Msg_NE
4485 ("aspect Indexing requires a function that applies to type&",
4486 Entity (Expr), Ent);
4487 end if;
4488
05f6f999 4489 -- Flag the default_iterator as well as the denoted function.
4490
89cc7147 4491 if not Valid_Default_Iterator (Entity (Expr)) then
05f6f999 4492 Error_Msg_N ("improper function for default iterator!", Expr);
89cc7147 4493 end if;
4494
4495 else
89cc7147 4496 declare
270ee9c5 4497 Default : Entity_Id := Empty;
8be33fbe 4498 I : Interp_Index;
4499 It : Interp;
89cc7147 4500
4501 begin
4502 Get_First_Interp (Expr, I, It);
4503 while Present (It.Nam) loop
4504 if not Check_Primitive_Function (It.Nam)
59f3e675 4505 or else not Valid_Default_Iterator (It.Nam)
89cc7147 4506 then
4507 Remove_Interp (I);
4508
4509 elsif Present (Default) then
89cc7147 4510
8be33fbe 4511 -- An explicit one should override an implicit one
4512
4513 if Comes_From_Source (Default) =
4514 Comes_From_Source (It.Nam)
4515 then
4516 Error_Msg_N ("default iterator must be unique", Expr);
4517 Error_Msg_Sloc := Sloc (Default);
4518 Error_Msg_N ("\\possible interpretation#", Expr);
4519 Error_Msg_Sloc := Sloc (It.Nam);
4520 Error_Msg_N ("\\possible interpretation#", Expr);
4521
4522 elsif Comes_From_Source (It.Nam) then
4523 Default := It.Nam;
4524 end if;
89cc7147 4525 else
4526 Default := It.Nam;
4527 end if;
4528
4529 Get_Next_Interp (I, It);
4530 end loop;
89cc7147 4531
270ee9c5 4532 if Present (Default) then
4533 Set_Entity (Expr, Default);
4534 Set_Is_Overloaded (Expr, False);
8b8be176 4535 else
4536 Error_Msg_N
7f5dd8d8 4537 ("no interpretation is a valid default iterator!", Expr);
270ee9c5 4538 end if;
4539 end;
89cc7147 4540 end if;
4541 end Check_Iterator_Functions;
4542
4543 -------------------------------
4544 -- Check_Primitive_Function --
4545 -------------------------------
4546
4547 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4548 Ctrl : Entity_Id;
4549
4550 begin
4551 if Ekind (Subp) /= E_Function then
4552 return False;
4553 end if;
4554
4555 if No (First_Formal (Subp)) then
4556 return False;
4557 else
4558 Ctrl := Etype (First_Formal (Subp));
4559 end if;
4560
05f6f999 4561 -- To be a primitive operation subprogram has to be in same scope.
4562
4563 if Scope (Ctrl) /= Scope (Subp) then
4564 return False;
4565 end if;
4566
7d6fb253 4567 -- Type of formal may be the class-wide type, an access to such,
4568 -- or an incomplete view.
4569
89cc7147 4570 if Ctrl = Ent
4571 or else Ctrl = Class_Wide_Type (Ent)
4572 or else
4573 (Ekind (Ctrl) = E_Anonymous_Access_Type
b85d62ec 4574 and then (Designated_Type (Ctrl) = Ent
4575 or else
4576 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
7d6fb253 4577 or else
4578 (Ekind (Ctrl) = E_Incomplete_Type
4579 and then Full_View (Ctrl) = Ent)
89cc7147 4580 then
4581 null;
89cc7147 4582 else
4583 return False;
4584 end if;
4585
4586 return True;
4587 end Check_Primitive_Function;
4588
ae888dbd 4589 ----------------------
4590 -- Duplicate_Clause --
4591 ----------------------
4592
4593 function Duplicate_Clause return Boolean is
d74fc39a 4594 A : Node_Id;
ae888dbd 4595
4596 begin
c8969ba6 4597 -- Nothing to do if this attribute definition clause comes from
4598 -- an aspect specification, since we could not be duplicating an
ae888dbd 4599 -- explicit clause, and we dealt with the case of duplicated aspects
4600 -- in Analyze_Aspect_Specifications.
4601
4602 if From_Aspect_Specification (N) then
4603 return False;
4604 end if;
4605
89f1e35c 4606 -- Otherwise current clause may duplicate previous clause, or a
4607 -- previously given pragma or aspect specification for the same
4608 -- aspect.
d74fc39a 4609
89b3b365 4610 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
ae888dbd 4611
4612 if Present (A) then
89f1e35c 4613 Error_Msg_Name_1 := Chars (N);
4614 Error_Msg_Sloc := Sloc (A);
4615
89b3b365 4616 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
89f1e35c 4617 return True;
ae888dbd 4618 end if;
4619
4620 return False;
4621 end Duplicate_Clause;
4622
9f373bb8 4623 -- Start of processing for Analyze_Attribute_Definition_Clause
4624
d6f39728 4625 begin
d64221a7 4626 -- The following code is a defense against recursion. Not clear that
51fa2a45 4627 -- this can happen legitimately, but perhaps some error situations can
4628 -- cause it, and we did see this recursion during testing.
d64221a7 4629
4630 if Analyzed (N) then
4631 return;
4632 else
4633 Set_Analyzed (N, True);
4634 end if;
4635
2609e4d0 4636 Check_Restriction_No_Use_Of_Attribute (N);
4637
a29bc1d9 4638 -- Ignore some selected attributes in CodePeer mode since they are not
4639 -- relevant in this context.
4640
4641 if CodePeer_Mode then
4642 case Id is
4643
4644 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4645 -- internal representation of types by implicitly packing them.
4646
4647 when Attribute_Component_Size =>
4648 Rewrite (N, Make_Null_Statement (Sloc (N)));
4649 return;
4650
4651 when others =>
4652 null;
4653 end case;
4654 end if;
4655
d8ba53a8 4656 -- Process Ignore_Rep_Clauses option
eef1ca1e 4657
d8ba53a8 4658 if Ignore_Rep_Clauses then
9d627c41 4659 case Id is
4660
eef1ca1e 4661 -- The following should be ignored. They do not affect legality
4662 -- and may be target dependent. The basic idea of -gnatI is to
4663 -- ignore any rep clauses that may be target dependent but do not
4664 -- affect legality (except possibly to be rejected because they
4665 -- are incompatible with the compilation target).
9d627c41 4666
99378362 4667 when Attribute_Alignment
4668 | Attribute_Bit_Order
4669 | Attribute_Component_Size
5bcff344 4670 | Attribute_Default_Scalar_Storage_Order
99378362 4671 | Attribute_Machine_Radix
4672 | Attribute_Object_Size
5bcff344 4673 | Attribute_Scalar_Storage_Order
99378362 4674 | Attribute_Size
4675 | Attribute_Small
4676 | Attribute_Stream_Size
4677 | Attribute_Value_Size
4678 =>
2ff55065 4679 Kill_Rep_Clause (N);
9d627c41 4680 return;
4681
eef1ca1e 4682 -- The following should not be ignored, because in the first place
51fa2a45 4683 -- they are reasonably portable, and should not cause problems
4684 -- in compiling code from another target, and also they do affect
4685 -- legality, e.g. failing to provide a stream attribute for a type
4686 -- may make a program illegal.
9d627c41 4687
99378362 4688 when Attribute_External_Tag
4689 | Attribute_Input
4690 | Attribute_Output
4691 | Attribute_Read
4692 | Attribute_Simple_Storage_Pool
4693 | Attribute_Storage_Pool
4694 | Attribute_Storage_Size
4695 | Attribute_Write
4696 =>
9d627c41 4697 null;
4698
2ff55065 4699 -- We do not do anything here with address clauses, they will be
4700 -- removed by Freeze later on, but for now, it works better to
c07717de 4701 -- keep them in the tree.
2ff55065 4702
4703 when Attribute_Address =>
4704 null;
4705
b593a52c 4706 -- Other cases are errors ("attribute& cannot be set with
4707 -- definition clause"), which will be caught below.
9d627c41 4708
4709 when others =>
4710 null;
4711 end case;
fbc67f84 4712 end if;
4713
d6f39728 4714 Analyze (Nam);
4715 Ent := Entity (Nam);
4716
4717 if Rep_Item_Too_Early (Ent, N) then
4718 return;
4719 end if;
4720
9f373bb8 4721 -- Rep clause applies to full view of incomplete type or private type if
4722 -- we have one (if not, this is a premature use of the type). However,
4723 -- certain semantic checks need to be done on the specified entity (i.e.
4724 -- the private view), so we save it in Ent.
d6f39728 4725
4726 if Is_Private_Type (Ent)
4727 and then Is_Derived_Type (Ent)
4728 and then not Is_Tagged_Type (Ent)
4729 and then No (Full_View (Ent))
4730 then
9f373bb8 4731 -- If this is a private type whose completion is a derivation from
4732 -- another private type, there is no full view, and the attribute
4733 -- belongs to the type itself, not its underlying parent.
d6f39728 4734
4735 U_Ent := Ent;
4736
4737 elsif Ekind (Ent) = E_Incomplete_Type then
d5b349fa 4738
9f373bb8 4739 -- The attribute applies to the full view, set the entity of the
4740 -- attribute definition accordingly.
d5b349fa 4741
d6f39728 4742 Ent := Underlying_Type (Ent);
4743 U_Ent := Ent;
d5b349fa 4744 Set_Entity (Nam, Ent);
4745
d6f39728 4746 else
4747 U_Ent := Underlying_Type (Ent);
4748 end if;
4749
44705307 4750 -- Avoid cascaded error
d6f39728 4751
4752 if Etype (Nam) = Any_Type then
4753 return;
4754
89f1e35c 4755 -- Must be declared in current scope or in case of an aspect
ace3389d 4756 -- specification, must be visible in current scope.
44705307 4757
89f1e35c 4758 elsif Scope (Ent) /= Current_Scope
ace3389d 4759 and then
4760 not (From_Aspect_Specification (N)
4761 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
89f1e35c 4762 then
d6f39728 4763 Error_Msg_N ("entity must be declared in this scope", Nam);
4764 return;
4765
44705307 4766 -- Must not be a source renaming (we do have some cases where the
4767 -- expander generates a renaming, and those cases are OK, in such
a3248fc4 4768 -- cases any attribute applies to the renamed object as well).
44705307 4769
4770 elsif Is_Object (Ent)
4771 and then Present (Renamed_Object (Ent))
44705307 4772 then
a3248fc4 4773 -- Case of renamed object from source, this is an error
4774
4775 if Comes_From_Source (Renamed_Object (Ent)) then
4776 Get_Name_String (Chars (N));
4777 Error_Msg_Strlen := Name_Len;
4778 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4779 Error_Msg_N
4780 ("~ clause not allowed for a renaming declaration "
4781 & "(RM 13.1(6))", Nam);
4782 return;
4783
4784 -- For the case of a compiler generated renaming, the attribute
4785 -- definition clause applies to the renamed object created by the
4786 -- expander. The easiest general way to handle this is to create a
4787 -- copy of the attribute definition clause for this object.
4788
9a48fc56 4789 elsif Is_Entity_Name (Renamed_Object (Ent)) then
a3248fc4 4790 Insert_Action (N,
4791 Make_Attribute_Definition_Clause (Loc,
4792 Name =>
4793 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4794 Chars => Chars (N),
4795 Expression => Duplicate_Subexpr (Expression (N))));
9a48fc56 4796
4797 -- If the renamed object is not an entity, it must be a dereference
4798 -- of an unconstrained function call, and we must introduce a new
4799 -- declaration to capture the expression. This is needed in the case
4800 -- of 'Alignment, where the original declaration must be rewritten.
4801
4802 else
4803 pragma Assert
4804 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4805 null;
a3248fc4 4806 end if;
44705307 4807
4808 -- If no underlying entity, use entity itself, applies to some
4809 -- previously detected error cases ???
4810
f15731c4 4811 elsif No (U_Ent) then
4812 U_Ent := Ent;
4813
44705307 4814 -- Cannot specify for a subtype (exception Object/Value_Size)
4815
d6f39728 4816 elsif Is_Type (U_Ent)
4817 and then not Is_First_Subtype (U_Ent)
4818 and then Id /= Attribute_Object_Size
4819 and then Id /= Attribute_Value_Size
4820 and then not From_At_Mod (N)
4821 then
4822 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4823 return;
d6f39728 4824 end if;
4825
ae888dbd 4826 Set_Entity (N, U_Ent);
4827
d6f39728 4828 -- Switch on particular attribute
4829
4830 case Id is
4831
4832 -------------
4833 -- Address --
4834 -------------
4835
4836 -- Address attribute definition clause
4837
4838 when Attribute_Address => Address : begin
177675a7 4839
4840 -- A little error check, catch for X'Address use X'Address;
4841
4842 if Nkind (Nam) = N_Identifier
4843 and then Nkind (Expr) = N_Attribute_Reference
4844 and then Attribute_Name (Expr) = Name_Address
4845 and then Nkind (Prefix (Expr)) = N_Identifier
4846 and then Chars (Nam) = Chars (Prefix (Expr))
4847 then
4848 Error_Msg_NE
4849 ("address for & is self-referencing", Prefix (Expr), Ent);
4850 return;
4851 end if;
4852
4853 -- Not that special case, carry on with analysis of expression
4854
d6f39728 4855 Analyze_And_Resolve (Expr, RTE (RE_Address));
4856
2f1aac99 4857 -- Even when ignoring rep clauses we need to indicate that the
4858 -- entity has an address clause and thus it is legal to declare
2ff55065 4859 -- it imported. Freeze will get rid of the address clause later.
c07717de 4860 -- Also call Set_Address_Taken to indicate that an address clause
4861 -- was present, even if we are about to remove it.
2f1aac99 4862
4863 if Ignore_Rep_Clauses then
c07717de 4864 Set_Address_Taken (U_Ent);
4865
d3ef794c 4866 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2f1aac99 4867 Record_Rep_Item (U_Ent, N);
4868 end if;
4869
4870 return;
4871 end if;
4872
ae888dbd 4873 if Duplicate_Clause then
4874 null;
d6f39728 4875
4876 -- Case of address clause for subprogram
4877
4878 elsif Is_Subprogram (U_Ent) then
d6f39728 4879 if Has_Homonym (U_Ent) then
4880 Error_Msg_N
f74a102b 4881 ("address clause cannot be given for overloaded "
4882 & "subprogram", Nam);
83f8f0a6 4883 return;
d6f39728 4884 end if;
4885
83f8f0a6 4886 -- For subprograms, all address clauses are permitted, and we
4887 -- mark the subprogram as having a deferred freeze so that Gigi
4888 -- will not elaborate it too soon.
d6f39728 4889
4890 -- Above needs more comments, what is too soon about???
4891
4892 Set_Has_Delayed_Freeze (U_Ent);
4893
4894 -- Case of address clause for entry
4895
4896 elsif Ekind (U_Ent) = E_Entry then
d6f39728 4897 if Nkind (Parent (N)) = N_Task_Body then
4898 Error_Msg_N
4899 ("entry address must be specified in task spec", Nam);
83f8f0a6 4900 return;
d6f39728 4901 end if;
4902
4903 -- For entries, we require a constant address
4904
4905 Check_Constant_Address_Clause (Expr, U_Ent);
4906
83f8f0a6 4907 -- Special checks for task types
4908
f15731c4 4909 if Is_Task_Type (Scope (U_Ent))
4910 and then Comes_From_Source (Scope (U_Ent))
4911 then
4912 Error_Msg_N
1e3532e7 4913 ("??entry address declared for entry in task type", N);
f15731c4 4914 Error_Msg_N
1e3532e7 4915 ("\??only one task can be declared of this type", N);
f15731c4 4916 end if;
4917
83f8f0a6 4918 -- Entry address clauses are obsolescent
4919
e0521a36 4920 Check_Restriction (No_Obsolescent_Features, N);
4921
9dfe12ae 4922 if Warn_On_Obsolescent_Feature then
4923 Error_Msg_N
f74a102b 4924 ("?j?attaching interrupt to task entry is an obsolescent "
4925 & "feature (RM J.7.1)", N);
9dfe12ae 4926 Error_Msg_N
1e3532e7 4927 ("\?j?use interrupt procedure instead", N);
9dfe12ae 4928 end if;
4929
8c252f6f 4930 -- Case of an address clause for a class-wide object, which is
5110559b 4931 -- considered erroneous.
4932
4933 elsif Is_Class_Wide_Type (Etype (U_Ent)) then
4934 Error_Msg_NE
4935 ("??class-wide object & must not be overlaid", Nam, U_Ent);
9dfe12ae 4936 Error_Msg_N
1e3532e7 4937 ("\??Program_Error will be raised at run time", Nam);
9dfe12ae 4938 Insert_Action (Declaration_Node (U_Ent),
4939 Make_Raise_Program_Error (Loc,
4940 Reason => PE_Overlaid_Controlled_Object));
83f8f0a6 4941 return;
9dfe12ae 4942
76be83f9 4943 -- Case of address clause for an object
d6f39728 4944
76be83f9 4945 elsif Ekind_In (U_Ent, E_Constant, E_Variable) then
d6f39728 4946 declare
d6da7448 4947 Expr : constant Node_Id := Expression (N);
4948 O_Ent : Entity_Id;
4949 Off : Boolean;
d6f39728 4950
4951 begin
7ee315cc 4952 -- Exported variables cannot have an address clause, because
4953 -- this cancels the effect of the pragma Export.
d6f39728 4954
4955 if Is_Exported (U_Ent) then
4956 Error_Msg_N
4957 ("cannot export object with address clause", Nam);
83f8f0a6 4958 return;
d6da7448 4959 end if;
4960
4961 Find_Overlaid_Entity (N, O_Ent, Off);
d6f39728 4962
a9dd889b 4963 if Present (O_Ent) then
798dec73 4964
a9dd889b 4965 -- If the object overlays a constant object, mark it so
b2d32174 4966
a9dd889b 4967 if Is_Constant_Object (O_Ent) then
4968 Set_Overlays_Constant (U_Ent);
4969 end if;
798dec73 4970
514a5555 4971 -- If the address clause is of the form:
4972
4973 -- for X'Address use Y'Address;
4974
4975 -- or
4976
4977 -- C : constant Address := Y'Address;
4978 -- ...
4979 -- for X'Address use C;
4980
4981 -- then we make an entry in the table to check the size
4982 -- and alignment of the overlaying variable. But we defer
4983 -- this check till after code generation to take full
4984 -- advantage of the annotation done by the back end.
4985
4986 -- If the entity has a generic type, the check will be
4987 -- performed in the instance if the actual type justifies
4988 -- it, and we do not insert the clause in the table to
4989 -- prevent spurious warnings.
4990
4991 -- Note: we used to test Comes_From_Source and only give
4992 -- this warning for source entities, but we have removed
4993 -- this test. It really seems bogus to generate overlays
4994 -- that would trigger this warning in generated code.
4995 -- Furthermore, by removing the test, we handle the
4996 -- aspect case properly.
4997
4998 if Is_Object (O_Ent)
4999 and then not Is_Generic_Type (Etype (U_Ent))
5000 and then Address_Clause_Overlay_Warnings
5001 then
5002 Address_Clause_Checks.Append
5003 ((N, U_Ent, No_Uint, O_Ent, Off));
5004 end if;
a9dd889b 5005 else
5006 -- If this is not an overlay, mark a variable as being
5007 -- volatile to prevent unwanted optimizations. It's a
5008 -- conservative interpretation of RM 13.3(19) for the
5009 -- cases where the compiler cannot detect potential
5010 -- aliasing issues easily and it also covers the case
5011 -- of an absolute address where the volatile aspect is
5012 -- kind of implicit.
5013
5014 if Ekind (U_Ent) = E_Variable then
5015 Set_Treat_As_Volatile (U_Ent);
5016 end if;
514a5555 5017
5018 -- Make an entry in the table for an absolute address as
5019 -- above to check that the value is compatible with the
5020 -- alignment of the object.
5021
5022 declare
5023 Addr : constant Node_Id := Address_Value (Expr);
5024 begin
5025 if Compile_Time_Known_Value (Addr)
5026 and then Address_Clause_Overlay_Warnings
5027 then
5028 Address_Clause_Checks.Append
5029 ((N, U_Ent, Expr_Value (Addr), Empty, False));
5030 end if;
5031 end;
b2d32174 5032 end if;
5033
95009d64 5034 -- Issue an unconditional warning for a constant overlaying
5035 -- a variable. For the reverse case, we will issue it only
b2d32174 5036 -- if the variable is modified.
95009d64 5037
76be83f9 5038 if Ekind (U_Ent) = E_Constant
95009d64 5039 and then Present (O_Ent)
b2d32174 5040 and then not Overlays_Constant (U_Ent)
5041 and then Address_Clause_Overlay_Warnings
9dfe12ae 5042 then
1e3532e7 5043 Error_Msg_N ("??constant overlays a variable", Expr);
9dfe12ae 5044
d6f39728 5045 -- Imported variables can have an address clause, but then
5046 -- the import is pretty meaningless except to suppress
5047 -- initializations, so we do not need such variables to
5048 -- be statically allocated (and in fact it causes trouble
5049 -- if the address clause is a local value).
5050
5051 elsif Is_Imported (U_Ent) then
5052 Set_Is_Statically_Allocated (U_Ent, False);
5053 end if;
5054
5055 -- We mark a possible modification of a variable with an
5056 -- address clause, since it is likely aliasing is occurring.
5057
177675a7 5058 Note_Possible_Modification (Nam, Sure => False);
d6f39728 5059
9dfe12ae 5060 -- Legality checks on the address clause for initialized
5061 -- objects is deferred until the freeze point, because
2beb22b1 5062 -- a subsequent pragma might indicate that the object
42e09e36 5063 -- is imported and thus not initialized. Also, the address
5064 -- clause might involve entities that have yet to be
5065 -- elaborated.
9dfe12ae 5066
5067 Set_Has_Delayed_Freeze (U_Ent);
5068
51ad5ad2 5069 -- If an initialization call has been generated for this
5070 -- object, it needs to be deferred to after the freeze node
5071 -- we have just now added, otherwise GIGI will see a
5072 -- reference to the variable (as actual to the IP call)
5073 -- before its definition.
5074
5075 declare
df9fba45 5076 Init_Call : constant Node_Id :=
5077 Remove_Init_Call (U_Ent, N);
4bba0a8d 5078
51ad5ad2 5079 begin
5080 if Present (Init_Call) then
28a4283c 5081 Append_Freeze_Action (U_Ent, Init_Call);
df9fba45 5082
28a4283c 5083 -- Reset Initialization_Statements pointer so that
5084 -- if there is a pragma Import further down, it can
5085 -- clear any default initialization.
df9fba45 5086
28a4283c 5087 Set_Initialization_Statements (U_Ent, Init_Call);
51ad5ad2 5088 end if;
5089 end;
5090
44e4341e 5091 -- Entity has delayed freeze, so we will generate an
5092 -- alignment check at the freeze point unless suppressed.
d6f39728 5093
44e4341e 5094 if not Range_Checks_Suppressed (U_Ent)
5095 and then not Alignment_Checks_Suppressed (U_Ent)
5096 then
5097 Set_Check_Address_Alignment (N);
5098 end if;
d6f39728 5099
5100 -- Kill the size check code, since we are not allocating
5101 -- the variable, it is somewhere else.
5102
5103 Kill_Size_Check_Code (U_Ent);
d6da7448 5104 end;
83f8f0a6 5105
d6f39728 5106 -- Not a valid entity for an address clause
5107
5108 else
5109 Error_Msg_N ("address cannot be given for &", Nam);
5110 end if;
5111 end Address;
5112
5113 ---------------
5114 -- Alignment --
5115 ---------------
5116
5117 -- Alignment attribute definition clause
5118
b47769f0 5119 when Attribute_Alignment => Alignment : declare
208fd589 5120 Align : constant Uint := Get_Alignment_Value (Expr);
5121 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
41331dcf 5122
d6f39728 5123 begin
5124 FOnly := True;
5125
5126 if not Is_Type (U_Ent)
5127 and then Ekind (U_Ent) /= E_Variable
5128 and then Ekind (U_Ent) /= E_Constant
5129 then
5130 Error_Msg_N ("alignment cannot be given for &", Nam);
5131
ae888dbd 5132 elsif Duplicate_Clause then
5133 null;
d6f39728 5134
5135 elsif Align /= No_Uint then
5136 Set_Has_Alignment_Clause (U_Ent);
208fd589 5137
44705307 5138 -- Tagged type case, check for attempt to set alignment to a
f74a102b 5139 -- value greater than Max_Align, and reset if so. This error
5140 -- is suppressed in ASIS mode to allow for different ASIS
f9906591 5141 -- back ends or ASIS-based tools to query the illegal clause.
44705307 5142
f74a102b 5143 if Is_Tagged_Type (U_Ent)
5144 and then Align > Max_Align
5145 and then not ASIS_Mode
5146 then
208fd589 5147 Error_Msg_N
1e3532e7 5148 ("alignment for & set to Maximum_Aligment??", Nam);
f74a102b 5149 Set_Alignment (U_Ent, Max_Align);
44705307 5150
5151 -- All other cases
5152
208fd589 5153 else
5154 Set_Alignment (U_Ent, Align);
5155 end if;
b47769f0 5156
5157 -- For an array type, U_Ent is the first subtype. In that case,
5158 -- also set the alignment of the anonymous base type so that
5159 -- other subtypes (such as the itypes for aggregates of the
5160 -- type) also receive the expected alignment.
5161
5162 if Is_Array_Type (U_Ent) then
5163 Set_Alignment (Base_Type (U_Ent), Align);
5164 end if;
d6f39728 5165 end if;
b47769f0 5166 end Alignment;
d6f39728 5167
5168 ---------------
5169 -- Bit_Order --
5170 ---------------
5171
5172 -- Bit_Order attribute definition clause
5173
99378362 5174 when Attribute_Bit_Order =>
d6f39728 5175 if not Is_Record_Type (U_Ent) then
5176 Error_Msg_N
5177 ("Bit_Order can only be defined for record type", Nam);
5178
ae888dbd 5179 elsif Duplicate_Clause then
5180 null;
5181
d6f39728 5182 else
5183 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5184
5185 if Etype (Expr) = Any_Type then
5186 return;
5187
cda40848 5188 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5189 Flag_Non_Static_Expr
5190 ("Bit_Order requires static expression!", Expr);
d6f39728 5191
5192 else
5193 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
fae4ea1f 5194 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
d6f39728 5195 end if;
5196 end if;
5197 end if;
d6f39728 5198
5199 --------------------
5200 -- Component_Size --
5201 --------------------
5202
5203 -- Component_Size attribute definition clause
5204
5205 when Attribute_Component_Size => Component_Size_Case : declare
5206 Csize : constant Uint := Static_Integer (Expr);
a0fc8c5b 5207 Ctyp : Entity_Id;
d6f39728 5208 Btype : Entity_Id;
5209 Biased : Boolean;
5210 New_Ctyp : Entity_Id;
5211 Decl : Node_Id;
5212
5213 begin
5214 if not Is_Array_Type (U_Ent) then
5215 Error_Msg_N ("component size requires array type", Nam);
5216 return;
5217 end if;
5218
5219 Btype := Base_Type (U_Ent);
f74a102b 5220 Ctyp := Component_Type (Btype);
d6f39728 5221
ae888dbd 5222 if Duplicate_Clause then
5223 null;
d6f39728 5224
f3e4db96 5225 elsif Rep_Item_Too_Early (Btype, N) then
5226 null;
5227
d6f39728 5228 elsif Csize /= No_Uint then
a0fc8c5b 5229 Check_Size (Expr, Ctyp, Csize, Biased);
d6f39728 5230
d74fc39a 5231 -- For the biased case, build a declaration for a subtype that
5232 -- will be used to represent the biased subtype that reflects
5233 -- the biased representation of components. We need the subtype
5234 -- to get proper conversions on referencing elements of the
36ac5fbb 5235 -- array.
3062c401 5236
36ac5fbb 5237 if Biased then
5238 New_Ctyp :=
5239 Make_Defining_Identifier (Loc,
5240 Chars =>
5241 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3062c401 5242
36ac5fbb 5243 Decl :=
5244 Make_Subtype_Declaration (Loc,
5245 Defining_Identifier => New_Ctyp,
5246 Subtype_Indication =>
5247 New_Occurrence_Of (Component_Type (Btype), Loc));
5248
5249 Set_Parent (Decl, N);
5250 Analyze (Decl, Suppress => All_Checks);
5251
5252 Set_Has_Delayed_Freeze (New_Ctyp, False);
5253 Set_Esize (New_Ctyp, Csize);
5254 Set_RM_Size (New_Ctyp, Csize);
5255 Init_Alignment (New_Ctyp);
5256 Set_Is_Itype (New_Ctyp, True);
5257 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5258
5259 Set_Component_Type (Btype, New_Ctyp);
5260 Set_Biased (New_Ctyp, N, "component size clause");
d6f39728 5261 end if;
5262
36ac5fbb 5263 Set_Component_Size (Btype, Csize);
5264
a0fc8c5b 5265 -- Deal with warning on overridden size
5266
5267 if Warn_On_Overridden_Size
5268 and then Has_Size_Clause (Ctyp)
5269 and then RM_Size (Ctyp) /= Csize
5270 then
5271 Error_Msg_NE
1e3532e7 5272 ("component size overrides size clause for&?S?", N, Ctyp);
a0fc8c5b 5273 end if;
5274
d6f39728 5275 Set_Has_Component_Size_Clause (Btype, True);
f3e4db96 5276 Set_Has_Non_Standard_Rep (Btype, True);
d6f39728 5277 end if;
5278 end Component_Size_Case;
5279
81b424ac 5280 -----------------------
5281 -- Constant_Indexing --
5282 -----------------------
5283
5284 when Attribute_Constant_Indexing =>
5285 Check_Indexing_Functions;
5286
89f1e35c 5287 ---------
5288 -- CPU --
5289 ---------
5290
99378362 5291 when Attribute_CPU =>
5292
89f1e35c 5293 -- CPU attribute definition clause not allowed except from aspect
5294 -- specification.
5295
5296 if From_Aspect_Specification (N) then
5297 if not Is_Task_Type (U_Ent) then
5298 Error_Msg_N ("CPU can only be defined for task", Nam);
5299
5300 elsif Duplicate_Clause then
5301 null;
5302
5303 else
5304 -- The expression must be analyzed in the special manner
5305 -- described in "Handling of Default and Per-Object
5306 -- Expressions" in sem.ads.
5307
5308 -- The visibility to the discriminants must be restored
5309
5310 Push_Scope_And_Install_Discriminants (U_Ent);
5311 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5312 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5313
cda40848 5314 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5315 Check_Restriction (Static_Priorities, Expr);
5316 end if;
5317 end if;
5318
5319 else
5320 Error_Msg_N
5321 ("attribute& cannot be set with definition clause", N);
5322 end if;
89f1e35c 5323
89cc7147 5324 ----------------------
5325 -- Default_Iterator --
5326 ----------------------
5327
5328 when Attribute_Default_Iterator => Default_Iterator : declare
5329 Func : Entity_Id;
fbf4d6ef 5330 Typ : Entity_Id;
89cc7147 5331
5332 begin
05f6f999 5333 -- If target type is untagged, further checks are irrelevant
5334
89cc7147 5335 if not Is_Tagged_Type (U_Ent) then
5336 Error_Msg_N
05f6f999 5337 ("aspect Default_Iterator applies to tagged type", Nam);
5338 return;
89cc7147 5339 end if;
5340
5341 Check_Iterator_Functions;
5342
5343 Analyze (Expr);
5344
5345 if not Is_Entity_Name (Expr)
5346 or else Ekind (Entity (Expr)) /= E_Function
5347 then
5348 Error_Msg_N ("aspect Iterator must be a function", Expr);
05f6f999 5349 return;
89cc7147 5350 else
5351 Func := Entity (Expr);
5352 end if;
5353
fbf4d6ef 5354 -- The type of the first parameter must be T, T'class, or a
05f6f999 5355 -- corresponding access type (5.5.1 (8/3). If function is
5356 -- parameterless label type accordingly.
fbf4d6ef 5357
5358 if No (First_Formal (Func)) then
05f6f999 5359 Typ := Any_Type;
fbf4d6ef 5360 else
5361 Typ := Etype (First_Formal (Func));
5362 end if;
5363
5364 if Typ = U_Ent
5365 or else Typ = Class_Wide_Type (U_Ent)
5366 or else (Is_Access_Type (Typ)
5367 and then Designated_Type (Typ) = U_Ent)
5368 or else (Is_Access_Type (Typ)
5369 and then Designated_Type (Typ) =
5370 Class_Wide_Type (U_Ent))
89cc7147 5371 then
fbf4d6ef 5372 null;
5373
5374 else
89cc7147 5375 Error_Msg_NE
5376 ("Default Iterator must be a primitive of&", Func, U_Ent);
5377 end if;
5378 end Default_Iterator;
5379
89f1e35c 5380 ------------------------
5381 -- Dispatching_Domain --
5382 ------------------------
5383
99378362 5384 when Attribute_Dispatching_Domain =>
5385
89f1e35c 5386 -- Dispatching_Domain attribute definition clause not allowed
5387 -- except from aspect specification.
5388
5389 if From_Aspect_Specification (N) then
5390 if not Is_Task_Type (U_Ent) then
fbf4d6ef 5391 Error_Msg_N
5392 ("Dispatching_Domain can only be defined for task", Nam);
89f1e35c 5393
5394 elsif Duplicate_Clause then
5395 null;
5396
5397 else
5398 -- The expression must be analyzed in the special manner
5399 -- described in "Handling of Default and Per-Object
5400 -- Expressions" in sem.ads.
5401
5402 -- The visibility to the discriminants must be restored
5403
5404 Push_Scope_And_Install_Discriminants (U_Ent);
5405
5406 Preanalyze_Spec_Expression
5407 (Expr, RTE (RE_Dispatching_Domain));
5408
5409 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5410 end if;
5411
5412 else
5413 Error_Msg_N
5414 ("attribute& cannot be set with definition clause", N);
5415 end if;
89f1e35c 5416
d6f39728 5417 ------------------
5418 -- External_Tag --
5419 ------------------
5420
99378362 5421 when Attribute_External_Tag =>
d6f39728 5422 if not Is_Tagged_Type (U_Ent) then
5423 Error_Msg_N ("should be a tagged type", Nam);
5424 end if;
5425
ae888dbd 5426 if Duplicate_Clause then
5427 null;
d6f39728 5428
9af0ddc7 5429 else
ae888dbd 5430 Analyze_And_Resolve (Expr, Standard_String);
fbc67f84 5431
cda40848 5432 if not Is_OK_Static_Expression (Expr) then
ae888dbd 5433 Flag_Non_Static_Expr
5434 ("static string required for tag name!", Nam);
5435 end if;
5436
ae888dbd 5437 if not Is_Library_Level_Entity (U_Ent) then
5438 Error_Msg_NE
1e3532e7 5439 ("??non-unique external tag supplied for &", N, U_Ent);
ae888dbd 5440 Error_Msg_N
f74a102b 5441 ("\??same external tag applies to all subprogram calls",
5442 N);
ae888dbd 5443 Error_Msg_N
1e3532e7 5444 ("\??corresponding internal tag cannot be obtained", N);
ae888dbd 5445 end if;
fbc67f84 5446 end if;
d6f39728 5447
b57530b8 5448 --------------------------
5449 -- Implicit_Dereference --
5450 --------------------------
7947a439 5451
b57530b8 5452 when Attribute_Implicit_Dereference =>
7947a439 5453
2beb22b1 5454 -- Legality checks already performed at the point of the type
5455 -- declaration, aspect is not delayed.
7947a439 5456
89cc7147 5457 null;
b57530b8 5458
d6f39728 5459 -----------
5460 -- Input --
5461 -----------
5462
9f373bb8 5463 when Attribute_Input =>
5464 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5465 Set_Has_Specified_Stream_Input (Ent);
d6f39728 5466
89f1e35c 5467 ------------------------
5468 -- Interrupt_Priority --
5469 ------------------------
5470
99378362 5471 when Attribute_Interrupt_Priority =>
5472
89f1e35c 5473 -- Interrupt_Priority attribute definition clause not allowed
5474 -- except from aspect specification.
5475
5476 if From_Aspect_Specification (N) then
f02a9a9a 5477 if not Is_Concurrent_Type (U_Ent) then
89f1e35c 5478 Error_Msg_N
f74a102b 5479 ("Interrupt_Priority can only be defined for task and "
5480 & "protected object", Nam);
89f1e35c 5481
5482 elsif Duplicate_Clause then
5483 null;
5484
5485 else
5486 -- The expression must be analyzed in the special manner
5487 -- described in "Handling of Default and Per-Object
5488 -- Expressions" in sem.ads.
5489
5490 -- The visibility to the discriminants must be restored
5491
5492 Push_Scope_And_Install_Discriminants (U_Ent);
5493
5494 Preanalyze_Spec_Expression
5495 (Expr, RTE (RE_Interrupt_Priority));
5496
5497 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
d4e1acfa 5498
5499 -- Check the No_Task_At_Interrupt_Priority restriction
5500
5501 if Is_Task_Type (U_Ent) then
5502 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5503 end if;
89f1e35c 5504 end if;
5505
5506 else
5507 Error_Msg_N
5508 ("attribute& cannot be set with definition clause", N);
5509 end if;
89f1e35c 5510
b3f8228a 5511 --------------
5512 -- Iterable --
5513 --------------
5514
5515 when Attribute_Iterable =>
5516 Analyze (Expr);
bde03454 5517
b3f8228a 5518 if Nkind (Expr) /= N_Aggregate then
5519 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5520 end if;
5521
5522 declare
5523 Assoc : Node_Id;
5524
5525 begin
5526 Assoc := First (Component_Associations (Expr));
5527 while Present (Assoc) loop
5528 if not Is_Entity_Name (Expression (Assoc)) then
5529 Error_Msg_N ("value must be a function", Assoc);
5530 end if;
bde03454 5531
b3f8228a 5532 Next (Assoc);
5533 end loop;
5534 end;
5535
89cc7147 5536 ----------------------
5537 -- Iterator_Element --
5538 ----------------------
5539
5540 when Attribute_Iterator_Element =>
5541 Analyze (Expr);
5542
5543 if not Is_Entity_Name (Expr)
5544 or else not Is_Type (Entity (Expr))
5545 then
5546 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5547 end if;
5548
d6f39728 5549 -------------------
5550 -- Machine_Radix --
5551 -------------------
5552
5553 -- Machine radix attribute definition clause
5554
5555 when Attribute_Machine_Radix => Machine_Radix : declare
5556 Radix : constant Uint := Static_Integer (Expr);
5557
5558 begin
5559 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5560 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5561
ae888dbd 5562 elsif Duplicate_Clause then
5563 null;
d6f39728 5564
5565 elsif Radix /= No_Uint then
5566 Set_Has_Machine_Radix_Clause (U_Ent);
5567 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5568
5569 if Radix = 2 then
5570 null;
f74a102b 5571
d6f39728 5572 elsif Radix = 10 then
5573 Set_Machine_Radix_10 (U_Ent);
f74a102b 5574
5575 -- The following error is suppressed in ASIS mode to allow for
f9906591 5576 -- different ASIS back ends or ASIS-based tools to query the
f74a102b 5577 -- illegal clause.
5578
5579 elsif not ASIS_Mode then
d6f39728 5580 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5581 end if;
5582 end if;
5583 end Machine_Radix;
5584
5585 -----------------
5586 -- Object_Size --
5587 -----------------
5588
5589 -- Object_Size attribute definition clause
5590
5591 when Attribute_Object_Size => Object_Size : declare
bfa5a9d9 5592 Size : constant Uint := Static_Integer (Expr);
5593
d6f39728 5594 Biased : Boolean;
bfa5a9d9 5595 pragma Warnings (Off, Biased);
d6f39728 5596
5597 begin
5598 if not Is_Type (U_Ent) then
5599 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5600
ae888dbd 5601 elsif Duplicate_Clause then
5602 null;
d6f39728 5603
5604 else
5605 Check_Size (Expr, U_Ent, Size, Biased);
5606
f74a102b 5607 -- The following errors are suppressed in ASIS mode to allow
f9906591 5608 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 5609 -- the illegal clause.
5610
5611 if ASIS_Mode then
5612 null;
5613
5614 elsif Is_Scalar_Type (U_Ent) then
829cd457 5615 if Size /= 8 and then Size /= 16 and then Size /= 32
5616 and then UI_Mod (Size, 64) /= 0
5617 then
5618 Error_Msg_N
5619 ("Object_Size must be 8, 16, 32, or multiple of 64",
5620 Expr);
5621 end if;
5622
5623 elsif Size mod 8 /= 0 then
5624 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
d6f39728 5625 end if;
5626
5627 Set_Esize (U_Ent, Size);
5628 Set_Has_Object_Size_Clause (U_Ent);
1d366b32 5629 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5630 end if;
5631 end Object_Size;
5632
5633 ------------
5634 -- Output --
5635 ------------
5636
9f373bb8 5637 when Attribute_Output =>
5638 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5639 Set_Has_Specified_Stream_Output (Ent);
d6f39728 5640
89f1e35c 5641 --------------
5642 -- Priority --
5643 --------------
5644
99378362 5645 when Attribute_Priority =>
5646
89f1e35c 5647 -- Priority attribute definition clause not allowed except from
5648 -- aspect specification.
5649
5650 if From_Aspect_Specification (N) then
f02a9a9a 5651 if not (Is_Concurrent_Type (U_Ent)
3a72f9c3 5652 or else Ekind (U_Ent) = E_Procedure)
89f1e35c 5653 then
5654 Error_Msg_N
f02a9a9a 5655 ("Priority can only be defined for task and protected "
5656 & "object", Nam);
89f1e35c 5657
5658 elsif Duplicate_Clause then
5659 null;
5660
5661 else
5662 -- The expression must be analyzed in the special manner
5663 -- described in "Handling of Default and Per-Object
5664 -- Expressions" in sem.ads.
5665
5666 -- The visibility to the discriminants must be restored
5667
5668 Push_Scope_And_Install_Discriminants (U_Ent);
5669 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5670 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5671
cda40848 5672 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5673 Check_Restriction (Static_Priorities, Expr);
5674 end if;
5675 end if;
5676
5677 else
5678 Error_Msg_N
5679 ("attribute& cannot be set with definition clause", N);
5680 end if;
89f1e35c 5681
d6f39728 5682 ----------
5683 -- Read --
5684 ----------
5685
9f373bb8 5686 when Attribute_Read =>
5687 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5688 Set_Has_Specified_Stream_Read (Ent);
d6f39728 5689
b7b74740 5690 --------------------------
5691 -- Scalar_Storage_Order --
5692 --------------------------
5693
5694 -- Scalar_Storage_Order attribute definition clause
5695
99378362 5696 when Attribute_Scalar_Storage_Order =>
b43a5770 5697 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
b7b74740 5698 Error_Msg_N
f74a102b 5699 ("Scalar_Storage_Order can only be defined for record or "
5700 & "array type", Nam);
b7b74740 5701
5702 elsif Duplicate_Clause then
5703 null;
5704
5705 else
5706 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5707
5708 if Etype (Expr) = Any_Type then
5709 return;
5710
cda40848 5711 elsif not Is_OK_Static_Expression (Expr) then
b7b74740 5712 Flag_Non_Static_Expr
5713 ("Scalar_Storage_Order requires static expression!", Expr);
5714
c0912570 5715 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5716
5717 -- Here for the case of a non-default (i.e. non-confirming)
5718 -- Scalar_Storage_Order attribute definition.
5719
5720 if Support_Nondefault_SSO_On_Target then
d0a9ea3b 5721 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
c0912570 5722 else
5723 Error_Msg_N
f74a102b 5724 ("non-default Scalar_Storage_Order not supported on "
5725 & "target", Expr);
b7b74740 5726 end if;
5727 end if;
b64082f2 5728
5729 -- Clear SSO default indications since explicit setting of the
5730 -- order overrides the defaults.
5731
5732 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5733 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
b7b74740 5734 end if;
b7b74740 5735
e6ce0468 5736 --------------------------
5737 -- Secondary_Stack_Size --
5738 --------------------------
5739
99378362 5740 when Attribute_Secondary_Stack_Size =>
5741
e6ce0468 5742 -- Secondary_Stack_Size attribute definition clause not allowed
5743 -- except from aspect specification.
5744
5745 if From_Aspect_Specification (N) then
5746 if not Is_Task_Type (U_Ent) then
fe696bd7 5747 Error_Msg_N
5748 ("Secondary Stack Size can only be defined for task", Nam);
e6ce0468 5749
5750 elsif Duplicate_Clause then
5751 null;
5752
5753 else
5754 Check_Restriction (No_Secondary_Stack, Expr);
5755
5756 -- The expression must be analyzed in the special manner
5757 -- described in "Handling of Default and Per-Object
5758 -- Expressions" in sem.ads.
5759
5760 -- The visibility to the discriminants must be restored
5761
5762 Push_Scope_And_Install_Discriminants (U_Ent);
5763 Preanalyze_Spec_Expression (Expr, Any_Integer);
5764 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5765
5766 if not Is_OK_Static_Expression (Expr) then
5767 Check_Restriction (Static_Storage_Size, Expr);
5768 end if;
5769 end if;
5770
5771 else
5772 Error_Msg_N
5773 ("attribute& cannot be set with definition clause", N);
5774 end if;
e6ce0468 5775
d6f39728 5776 ----------
5777 -- Size --
5778 ----------
5779
5780 -- Size attribute definition clause
5781
5782 when Attribute_Size => Size : declare
5783 Size : constant Uint := Static_Integer (Expr);
5784 Etyp : Entity_Id;
5785 Biased : Boolean;
5786
5787 begin
5788 FOnly := True;
5789
ae888dbd 5790 if Duplicate_Clause then
5791 null;
d6f39728 5792
5793 elsif not Is_Type (U_Ent)
5794 and then Ekind (U_Ent) /= E_Variable
5795 and then Ekind (U_Ent) /= E_Constant
5796 then
5797 Error_Msg_N ("size cannot be given for &", Nam);
5798
5799 elsif Is_Array_Type (U_Ent)
5800 and then not Is_Constrained (U_Ent)
5801 then
5802 Error_Msg_N
5803 ("size cannot be given for unconstrained array", Nam);
5804
c2b89d6e 5805 elsif Size /= No_Uint then
d6f39728 5806 if Is_Type (U_Ent) then
5807 Etyp := U_Ent;
5808 else
5809 Etyp := Etype (U_Ent);
5810 end if;
5811
59ac57b5 5812 -- Check size, note that Gigi is in charge of checking that the
5813 -- size of an array or record type is OK. Also we do not check
5814 -- the size in the ordinary fixed-point case, since it is too
5815 -- early to do so (there may be subsequent small clause that
5816 -- affects the size). We can check the size if a small clause
5817 -- has already been given.
d6f39728 5818
5819 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5820 or else Has_Small_Clause (U_Ent)
5821 then
5822 Check_Size (Expr, Etyp, Size, Biased);
b77e4501 5823 Set_Biased (U_Ent, N, "size clause", Biased);
d6f39728 5824 end if;
5825
5826 -- For types set RM_Size and Esize if possible
5827
5828 if Is_Type (U_Ent) then
5829 Set_RM_Size (U_Ent, Size);
5830
ada34def 5831 -- For elementary types, increase Object_Size to power of 2,
5832 -- but not less than a storage unit in any case (normally
59ac57b5 5833 -- this means it will be byte addressable).
d6f39728 5834
ada34def 5835 -- For all other types, nothing else to do, we leave Esize
5836 -- (object size) unset, the back end will set it from the
5837 -- size and alignment in an appropriate manner.
5838
1d366b32 5839 -- In both cases, we check whether the alignment must be
5840 -- reset in the wake of the size change.
5841
ada34def 5842 if Is_Elementary_Type (U_Ent) then
f15731c4 5843 if Size <= System_Storage_Unit then
5844 Init_Esize (U_Ent, System_Storage_Unit);
d6f39728 5845 elsif Size <= 16 then
5846 Init_Esize (U_Ent, 16);
5847 elsif Size <= 32 then
5848 Init_Esize (U_Ent, 32);
5849 else
5850 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5851 end if;
5852
1d366b32 5853 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5854 else
5855 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5856 end if;
5857
d6f39728 5858 -- For objects, set Esize only
5859
5860 else
f74a102b 5861 -- The following error is suppressed in ASIS mode to allow
f9906591 5862 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 5863 -- the illegal clause.
5864
5865 if Is_Elementary_Type (Etyp)
5866 and then Size /= System_Storage_Unit
5867 and then Size /= System_Storage_Unit * 2
5868 and then Size /= System_Storage_Unit * 4
5869 and then Size /= System_Storage_Unit * 8
5870 and then not ASIS_Mode
5871 then
5872 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5873 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5874 Error_Msg_N
5875 ("size for primitive object must be a power of 2 in "
5876 & "the range ^-^", N);
9dfe12ae 5877 end if;
5878
d6f39728 5879 Set_Esize (U_Ent, Size);
5880 end if;
5881
5882 Set_Has_Size_Clause (U_Ent);
5883 end if;
5884 end Size;
5885
5886 -----------
5887 -- Small --
5888 -----------
5889
5890 -- Small attribute definition clause
5891
5892 when Attribute_Small => Small : declare
5893 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5894 Small : Ureal;
5895
5896 begin
5897 Analyze_And_Resolve (Expr, Any_Real);
5898
5899 if Etype (Expr) = Any_Type then
5900 return;
5901
cda40848 5902 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5903 Flag_Non_Static_Expr
5904 ("small requires static expression!", Expr);
d6f39728 5905 return;
5906
5907 else
5908 Small := Expr_Value_R (Expr);
5909
5910 if Small <= Ureal_0 then
5911 Error_Msg_N ("small value must be greater than zero", Expr);
5912 return;
5913 end if;
5914
5915 end if;
5916
5917 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5918 Error_Msg_N
5919 ("small requires an ordinary fixed point type", Nam);
5920
5921 elsif Has_Small_Clause (U_Ent) then
5922 Error_Msg_N ("small already given for &", Nam);
5923
5924 elsif Small > Delta_Value (U_Ent) then
5925 Error_Msg_N
ce3e25d6 5926 ("small value must not be greater than delta value", Nam);
d6f39728 5927
5928 else
5929 Set_Small_Value (U_Ent, Small);
5930 Set_Small_Value (Implicit_Base, Small);
5931 Set_Has_Small_Clause (U_Ent);
5932 Set_Has_Small_Clause (Implicit_Base);
5933 Set_Has_Non_Standard_Rep (Implicit_Base);
5934 end if;
5935 end Small;
5936
d6f39728 5937 ------------------
5938 -- Storage_Pool --
5939 ------------------
5940
5941 -- Storage_Pool attribute definition clause
5942
99378362 5943 when Attribute_Simple_Storage_Pool
5944 | Attribute_Storage_Pool
5945 =>
5946 Storage_Pool : declare
d6f39728 5947 Pool : Entity_Id;
6b567c71 5948 T : Entity_Id;
d6f39728 5949
5950 begin
44e4341e 5951 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5952 Error_Msg_N
5953 ("storage pool cannot be given for access-to-subprogram type",
5954 Nam);
5955 return;
5956
99378362 5957 elsif not Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
d6f39728 5958 then
44e4341e 5959 Error_Msg_N
5960 ("storage pool can only be given for access types", Nam);
d6f39728 5961 return;
5962
5963 elsif Is_Derived_Type (U_Ent) then
5964 Error_Msg_N
5965 ("storage pool cannot be given for a derived access type",
5966 Nam);
5967
ae888dbd 5968 elsif Duplicate_Clause then
d6f39728 5969 return;
5970
5971 elsif Present (Associated_Storage_Pool (U_Ent)) then
5972 Error_Msg_N ("storage pool already given for &", Nam);
5973 return;
5974 end if;
5975
6653b695 5976 -- Check for Storage_Size previously given
5977
5978 declare
5979 SS : constant Node_Id :=
5980 Get_Attribute_Definition_Clause
5981 (U_Ent, Attribute_Storage_Size);
5982 begin
5983 if Present (SS) then
5984 Check_Pool_Size_Clash (U_Ent, N, SS);
5985 end if;
5986 end;
5987
5988 -- Storage_Pool case
5989
b55f7641 5990 if Id = Attribute_Storage_Pool then
5991 Analyze_And_Resolve
5992 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5993
5994 -- In the Simple_Storage_Pool case, we allow a variable of any
b15003c3 5995 -- simple storage pool type, so we Resolve without imposing an
b55f7641 5996 -- expected type.
5997
5998 else
5999 Analyze_And_Resolve (Expr);
6000
6001 if not Present (Get_Rep_Pragma
b15003c3 6002 (Etype (Expr), Name_Simple_Storage_Pool_Type))
b55f7641 6003 then
6004 Error_Msg_N
6005 ("expression must be of a simple storage pool type", Expr);
6006 end if;
6007 end if;
d6f39728 6008
8c5c7277 6009 if not Denotes_Variable (Expr) then
6010 Error_Msg_N ("storage pool must be a variable", Expr);
6011 return;
6012 end if;
6013
6b567c71 6014 if Nkind (Expr) = N_Type_Conversion then
6015 T := Etype (Expression (Expr));
6016 else
6017 T := Etype (Expr);
6018 end if;
6019
6020 -- The Stack_Bounded_Pool is used internally for implementing
d64221a7 6021 -- access types with a Storage_Size. Since it only work properly
6022 -- when used on one specific type, we need to check that it is not
6023 -- hijacked improperly:
6024
6b567c71 6025 -- type T is access Integer;
6026 -- for T'Storage_Size use n;
6027 -- type Q is access Float;
6028 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
6029
15ebb600 6030 if RTE_Available (RE_Stack_Bounded_Pool)
6031 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
6032 then
6033 Error_Msg_N ("non-shareable internal Pool", Expr);
6b567c71 6034 return;
6035 end if;
6036
d6f39728 6037 -- If the argument is a name that is not an entity name, then
6038 -- we construct a renaming operation to define an entity of
6039 -- type storage pool.
6040
6041 if not Is_Entity_Name (Expr)
6042 and then Is_Object_Reference (Expr)
6043 then
11deeeb6 6044 Pool := Make_Temporary (Loc, 'P', Expr);
d6f39728 6045
6046 declare
6047 Rnode : constant Node_Id :=
6048 Make_Object_Renaming_Declaration (Loc,
6049 Defining_Identifier => Pool,
6050 Subtype_Mark =>
6051 New_Occurrence_Of (Etype (Expr), Loc),
11deeeb6 6052 Name => Expr);
d6f39728 6053
6054 begin
f65f7fdf 6055 -- If the attribute definition clause comes from an aspect
6056 -- clause, then insert the renaming before the associated
6057 -- entity's declaration, since the attribute clause has
6058 -- not yet been appended to the declaration list.
6059
6060 if From_Aspect_Specification (N) then
6061 Insert_Before (Parent (Entity (N)), Rnode);
6062 else
6063 Insert_Before (N, Rnode);
6064 end if;
6065
d6f39728 6066 Analyze (Rnode);
6067 Set_Associated_Storage_Pool (U_Ent, Pool);
6068 end;
6069
6070 elsif Is_Entity_Name (Expr) then
6071 Pool := Entity (Expr);
6072
6073 -- If pool is a renamed object, get original one. This can
6074 -- happen with an explicit renaming, and within instances.
6075
6076 while Present (Renamed_Object (Pool))
6077 and then Is_Entity_Name (Renamed_Object (Pool))
6078 loop
6079 Pool := Entity (Renamed_Object (Pool));
6080 end loop;
6081
6082 if Present (Renamed_Object (Pool))
6083 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
6084 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
6085 then
6086 Pool := Entity (Expression (Renamed_Object (Pool)));
6087 end if;
6088
6b567c71 6089 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 6090
6091 elsif Nkind (Expr) = N_Type_Conversion
6092 and then Is_Entity_Name (Expression (Expr))
6093 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
6094 then
6095 Pool := Entity (Expression (Expr));
6b567c71 6096 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 6097
6098 else
6099 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
6100 return;
6101 end if;
99378362 6102 end Storage_Pool;
d6f39728 6103
44e4341e 6104 ------------------
6105 -- Storage_Size --
6106 ------------------
6107
6108 -- Storage_Size attribute definition clause
6109
6110 when Attribute_Storage_Size => Storage_Size : declare
6111 Btype : constant Entity_Id := Base_Type (U_Ent);
44e4341e 6112
6113 begin
6114 if Is_Task_Type (U_Ent) then
44e4341e 6115
39a0c1d3 6116 -- Check obsolescent (but never obsolescent if from aspect)
ceec4f7c 6117
6118 if not From_Aspect_Specification (N) then
6119 Check_Restriction (No_Obsolescent_Features, N);
6120
6121 if Warn_On_Obsolescent_Feature then
6122 Error_Msg_N
f74a102b 6123 ("?j?storage size clause for task is an obsolescent "
6124 & "feature (RM J.9)", N);
ceec4f7c 6125 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
6126 end if;
44e4341e 6127 end if;
6128
6129 FOnly := True;
6130 end if;
6131
6132 if not Is_Access_Type (U_Ent)
6133 and then Ekind (U_Ent) /= E_Task_Type
6134 then
6135 Error_Msg_N ("storage size cannot be given for &", Nam);
6136
6137 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6138 Error_Msg_N
6139 ("storage size cannot be given for a derived access type",
6140 Nam);
6141
ae888dbd 6142 elsif Duplicate_Clause then
6143 null;
44e4341e 6144
6145 else
6146 Analyze_And_Resolve (Expr, Any_Integer);
6147
6148 if Is_Access_Type (U_Ent) then
6653b695 6149
6150 -- Check for Storage_Pool previously given
6151
6152 declare
6153 SP : constant Node_Id :=
6154 Get_Attribute_Definition_Clause
6155 (U_Ent, Attribute_Storage_Pool);
6156
6157 begin
6158 if Present (SP) then
6159 Check_Pool_Size_Clash (U_Ent, SP, N);
6160 end if;
6161 end;
6162
6163 -- Special case of for x'Storage_Size use 0
44e4341e 6164
5941a4e9 6165 if Is_OK_Static_Expression (Expr)
44e4341e 6166 and then Expr_Value (Expr) = 0
6167 then
6168 Set_No_Pool_Assigned (Btype);
6169 end if;
44e4341e 6170 end if;
6171
6172 Set_Has_Storage_Size_Clause (Btype);
6173 end if;
6174 end Storage_Size;
6175
7189d17f 6176 -----------------
6177 -- Stream_Size --
6178 -----------------
6179
6180 when Attribute_Stream_Size => Stream_Size : declare
6181 Size : constant Uint := Static_Integer (Expr);
6182
6183 begin
15ebb600 6184 if Ada_Version <= Ada_95 then
6185 Check_Restriction (No_Implementation_Attributes, N);
6186 end if;
6187
ae888dbd 6188 if Duplicate_Clause then
6189 null;
7189d17f 6190
6191 elsif Is_Elementary_Type (U_Ent) then
f74a102b 6192
6193 -- The following errors are suppressed in ASIS mode to allow
f9906591 6194 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 6195 -- the illegal clause.
6196
6197 if ASIS_Mode then
6198 null;
6199
6200 elsif Size /= System_Storage_Unit
6201 and then Size /= System_Storage_Unit * 2
6202 and then Size /= System_Storage_Unit * 4
6203 and then Size /= System_Storage_Unit * 8
7189d17f 6204 then
6205 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6206 Error_Msg_N
f74a102b 6207 ("stream size for elementary type must be a power of 2 "
6208 & "and at least ^", N);
7189d17f 6209
6210 elsif RM_Size (U_Ent) > Size then
6211 Error_Msg_Uint_1 := RM_Size (U_Ent);
6212 Error_Msg_N
f74a102b 6213 ("stream size for elementary type must be a power of 2 "
6214 & "and at least ^", N);
7189d17f 6215 end if;
6216
6217 Set_Has_Stream_Size_Clause (U_Ent);
6218
6219 else
6220 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6221 end if;
6222 end Stream_Size;
6223
d6f39728 6224 ----------------
6225 -- Value_Size --
6226 ----------------
6227
6228 -- Value_Size attribute definition clause
6229
6230 when Attribute_Value_Size => Value_Size : declare
6231 Size : constant Uint := Static_Integer (Expr);
6232 Biased : Boolean;
6233
6234 begin
6235 if not Is_Type (U_Ent) then
6236 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6237
ae888dbd 6238 elsif Duplicate_Clause then
6239 null;
d6f39728 6240
59ac57b5 6241 elsif Is_Array_Type (U_Ent)
6242 and then not Is_Constrained (U_Ent)
6243 then
6244 Error_Msg_N
6245 ("Value_Size cannot be given for unconstrained array", Nam);
6246
d6f39728 6247 else
6248 if Is_Elementary_Type (U_Ent) then
6249 Check_Size (Expr, U_Ent, Size, Biased);
b77e4501 6250 Set_Biased (U_Ent, N, "value size clause", Biased);
d6f39728 6251 end if;
6252
6253 Set_RM_Size (U_Ent, Size);
6254 end if;
6255 end Value_Size;
6256
81b424ac 6257 -----------------------
6258 -- Variable_Indexing --
6259 -----------------------
6260
6261 when Attribute_Variable_Indexing =>
6262 Check_Indexing_Functions;
6263
d6f39728 6264 -----------
6265 -- Write --
6266 -----------
6267
9f373bb8 6268 when Attribute_Write =>
6269 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6270 Set_Has_Specified_Stream_Write (Ent);
d6f39728 6271
6272 -- All other attributes cannot be set
6273
6274 when others =>
6275 Error_Msg_N
6276 ("attribute& cannot be set with definition clause", N);
d6f39728 6277 end case;
6278
d64221a7 6279 -- The test for the type being frozen must be performed after any
6280 -- expression the clause has been analyzed since the expression itself
6281 -- might cause freezing that makes the clause illegal.
d6f39728 6282
6283 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6284 return;
6285 end if;
6286 end Analyze_Attribute_Definition_Clause;
6287
6288 ----------------------------
6289 -- Analyze_Code_Statement --
6290 ----------------------------
6291
6292 procedure Analyze_Code_Statement (N : Node_Id) is
6293 HSS : constant Node_Id := Parent (N);
6294 SBody : constant Node_Id := Parent (HSS);
6295 Subp : constant Entity_Id := Current_Scope;
6296 Stmt : Node_Id;
6297 Decl : Node_Id;
6298 StmtO : Node_Id;
6299 DeclO : Node_Id;
6300
6301 begin
1d3f0c6b 6302 -- Accept foreign code statements for CodePeer. The analysis is skipped
6303 -- to avoid rejecting unrecognized constructs.
6304
6305 if CodePeer_Mode then
6306 Set_Analyzed (N);
6307 return;
6308 end if;
6309
d6f39728 6310 -- Analyze and check we get right type, note that this implements the
1d3f0c6b 6311 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6312 -- the only way that Asm_Insn could possibly be visible.
d6f39728 6313
6314 Analyze_And_Resolve (Expression (N));
6315
6316 if Etype (Expression (N)) = Any_Type then
6317 return;
6318 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6319 Error_Msg_N ("incorrect type for code statement", N);
6320 return;
6321 end if;
6322
44e4341e 6323 Check_Code_Statement (N);
6324
1d3f0c6b 6325 -- Make sure we appear in the handled statement sequence of a subprogram
6326 -- (RM 13.8(3)).
d6f39728 6327
6328 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6329 or else Nkind (SBody) /= N_Subprogram_Body
6330 then
6331 Error_Msg_N
6332 ("code statement can only appear in body of subprogram", N);
6333 return;
6334 end if;
6335
6336 -- Do remaining checks (RM 13.8(3)) if not already done
6337
6338 if not Is_Machine_Code_Subprogram (Subp) then
6339 Set_Is_Machine_Code_Subprogram (Subp);
6340
6341 -- No exception handlers allowed
6342
6343 if Present (Exception_Handlers (HSS)) then
6344 Error_Msg_N
6345 ("exception handlers not permitted in machine code subprogram",
6346 First (Exception_Handlers (HSS)));
6347 end if;
6348
6349 -- No declarations other than use clauses and pragmas (we allow
6350 -- certain internally generated declarations as well).
6351
6352 Decl := First (Declarations (SBody));
6353 while Present (Decl) loop
6354 DeclO := Original_Node (Decl);
6355 if Comes_From_Source (DeclO)
fdd294d1 6356 and not Nkind_In (DeclO, N_Pragma,
6357 N_Use_Package_Clause,
6358 N_Use_Type_Clause,
6359 N_Implicit_Label_Declaration)
d6f39728 6360 then
6361 Error_Msg_N
6362 ("this declaration not allowed in machine code subprogram",
6363 DeclO);
6364 end if;
6365
6366 Next (Decl);
6367 end loop;
6368
6369 -- No statements other than code statements, pragmas, and labels.
6370 -- Again we allow certain internally generated statements.
3ab42ff7 6371
c3107527 6372 -- In Ada 2012, qualified expressions are names, and the code
6373 -- statement is initially parsed as a procedure call.
d6f39728 6374
6375 Stmt := First (Statements (HSS));
6376 while Present (Stmt) loop
6377 StmtO := Original_Node (Stmt);
c3107527 6378
1d3f0c6b 6379 -- A procedure call transformed into a code statement is OK
59f2fcab 6380
c3107527 6381 if Ada_Version >= Ada_2012
6382 and then Nkind (StmtO) = N_Procedure_Call_Statement
59f2fcab 6383 and then Nkind (Name (StmtO)) = N_Qualified_Expression
c3107527 6384 then
6385 null;
6386
6387 elsif Comes_From_Source (StmtO)
fdd294d1 6388 and then not Nkind_In (StmtO, N_Pragma,
6389 N_Label,
6390 N_Code_Statement)
d6f39728 6391 then
6392 Error_Msg_N
6393 ("this statement is not allowed in machine code subprogram",
6394 StmtO);
6395 end if;
6396
6397 Next (Stmt);
6398 end loop;
6399 end if;
d6f39728 6400 end Analyze_Code_Statement;
6401
6402 -----------------------------------------------
6403 -- Analyze_Enumeration_Representation_Clause --
6404 -----------------------------------------------
6405
6406 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
21647c2d 6407 Ident : constant Node_Id := Identifier (N);
6408 Aggr : constant Node_Id := Array_Aggregate (N);
d6f39728 6409 Enumtype : Entity_Id;
6410 Elit : Entity_Id;
6411 Expr : Node_Id;
6412 Assoc : Node_Id;
6413 Choice : Node_Id;
6414 Val : Uint;
b3190af0 6415
6416 Err : Boolean := False;
098d3082 6417 -- Set True to avoid cascade errors and crashes on incorrect source code
d6f39728 6418
e30c7d84 6419 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6420 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6421 -- Allowed range of universal integer (= allowed range of enum lit vals)
6422
d6f39728 6423 Min : Uint;
6424 Max : Uint;
e30c7d84 6425 -- Minimum and maximum values of entries
6426
6427 Max_Node : Node_Id;
6428 -- Pointer to node for literal providing max value
d6f39728 6429
6430 begin
ca301e17 6431 if Ignore_Rep_Clauses then
2ff55065 6432 Kill_Rep_Clause (N);
fbc67f84 6433 return;
6434 end if;
6435
175a6969 6436 -- Ignore enumeration rep clauses by default in CodePeer mode,
6437 -- unless -gnatd.I is specified, as a work around for potential false
6438 -- positive messages.
6439
6440 if CodePeer_Mode and not Debug_Flag_Dot_II then
6441 return;
6442 end if;
6443
d6f39728 6444 -- First some basic error checks
6445
6446 Find_Type (Ident);
6447 Enumtype := Entity (Ident);
6448
6449 if Enumtype = Any_Type
6450 or else Rep_Item_Too_Early (Enumtype, N)
6451 then
6452 return;
6453 else
6454 Enumtype := Underlying_Type (Enumtype);
6455 end if;
6456
6457 if not Is_Enumeration_Type (Enumtype) then
6458 Error_Msg_NE
6459 ("enumeration type required, found}",
6460 Ident, First_Subtype (Enumtype));
6461 return;
6462 end if;
6463
9dfe12ae 6464 -- Ignore rep clause on generic actual type. This will already have
6465 -- been flagged on the template as an error, and this is the safest
6466 -- way to ensure we don't get a junk cascaded message in the instance.
6467
6468 if Is_Generic_Actual_Type (Enumtype) then
6469 return;
6470
6471 -- Type must be in current scope
6472
6473 elsif Scope (Enumtype) /= Current_Scope then
d6f39728 6474 Error_Msg_N ("type must be declared in this scope", Ident);
6475 return;
6476
9dfe12ae 6477 -- Type must be a first subtype
6478
d6f39728 6479 elsif not Is_First_Subtype (Enumtype) then
6480 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6481 return;
6482
9dfe12ae 6483 -- Ignore duplicate rep clause
6484
d6f39728 6485 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6486 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6487 return;
6488
7189d17f 6489 -- Don't allow rep clause for standard [wide_[wide_]]character
9dfe12ae 6490
177675a7 6491 elsif Is_Standard_Character_Type (Enumtype) then
d6f39728 6492 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
9dfe12ae 6493 return;
6494
d9125581 6495 -- Check that the expression is a proper aggregate (no parentheses)
6496
6497 elsif Paren_Count (Aggr) /= 0 then
6498 Error_Msg
6499 ("extra parentheses surrounding aggregate not allowed",
6500 First_Sloc (Aggr));
6501 return;
6502
9dfe12ae 6503 -- All tests passed, so set rep clause in place
d6f39728 6504
6505 else
6506 Set_Has_Enumeration_Rep_Clause (Enumtype);
6507 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6508 end if;
6509
6510 -- Now we process the aggregate. Note that we don't use the normal
6511 -- aggregate code for this purpose, because we don't want any of the
6512 -- normal expansion activities, and a number of special semantic
6513 -- rules apply (including the component type being any integer type)
6514
d6f39728 6515 Elit := First_Literal (Enumtype);
6516
6517 -- First the positional entries if any
6518
6519 if Present (Expressions (Aggr)) then
6520 Expr := First (Expressions (Aggr));
6521 while Present (Expr) loop
6522 if No (Elit) then
6523 Error_Msg_N ("too many entries in aggregate", Expr);
6524 return;
6525 end if;
6526
6527 Val := Static_Integer (Expr);
6528
d9125581 6529 -- Err signals that we found some incorrect entries processing
6530 -- the list. The final checks for completeness and ordering are
6531 -- skipped in this case.
6532
d6f39728 6533 if Val = No_Uint then
6534 Err := True;
f02a9a9a 6535
d6f39728 6536 elsif Val < Lo or else Hi < Val then
6537 Error_Msg_N ("value outside permitted range", Expr);
6538 Err := True;
6539 end if;
6540
6541 Set_Enumeration_Rep (Elit, Val);
6542 Set_Enumeration_Rep_Expr (Elit, Expr);
6543 Next (Expr);
6544 Next (Elit);
6545 end loop;
6546 end if;
6547
6548 -- Now process the named entries if present
6549
6550 if Present (Component_Associations (Aggr)) then
6551 Assoc := First (Component_Associations (Aggr));
6552 while Present (Assoc) loop
6553 Choice := First (Choices (Assoc));
6554
6555 if Present (Next (Choice)) then
6556 Error_Msg_N
6557 ("multiple choice not allowed here", Next (Choice));
6558 Err := True;
6559 end if;
6560
6561 if Nkind (Choice) = N_Others_Choice then
6562 Error_Msg_N ("others choice not allowed here", Choice);
6563 Err := True;
6564
6565 elsif Nkind (Choice) = N_Range then
b3190af0 6566
d6f39728 6567 -- ??? should allow zero/one element range here
b3190af0 6568
d6f39728 6569 Error_Msg_N ("range not allowed here", Choice);
6570 Err := True;
6571
6572 else
6573 Analyze_And_Resolve (Choice, Enumtype);
b3190af0 6574
098d3082 6575 if Error_Posted (Choice) then
d6f39728 6576 Err := True;
098d3082 6577 end if;
d6f39728 6578
098d3082 6579 if not Err then
6580 if Is_Entity_Name (Choice)
6581 and then Is_Type (Entity (Choice))
6582 then
6583 Error_Msg_N ("subtype name not allowed here", Choice);
d6f39728 6584 Err := True;
b3190af0 6585
098d3082 6586 -- ??? should allow static subtype with zero/one entry
d6f39728 6587
098d3082 6588 elsif Etype (Choice) = Base_Type (Enumtype) then
cda40848 6589 if not Is_OK_Static_Expression (Choice) then
098d3082 6590 Flag_Non_Static_Expr
6591 ("non-static expression used for choice!", Choice);
d6f39728 6592 Err := True;
d6f39728 6593
098d3082 6594 else
6595 Elit := Expr_Value_E (Choice);
6596
6597 if Present (Enumeration_Rep_Expr (Elit)) then
6598 Error_Msg_Sloc :=
6599 Sloc (Enumeration_Rep_Expr (Elit));
6600 Error_Msg_NE
6601 ("representation for& previously given#",
6602 Choice, Elit);
6603 Err := True;
6604 end if;
d6f39728 6605
098d3082 6606 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
d6f39728 6607
098d3082 6608 Expr := Expression (Assoc);
6609 Val := Static_Integer (Expr);
d6f39728 6610
098d3082 6611 if Val = No_Uint then
6612 Err := True;
6613
6614 elsif Val < Lo or else Hi < Val then
6615 Error_Msg_N ("value outside permitted range", Expr);
6616 Err := True;
6617 end if;
d6f39728 6618
098d3082 6619 Set_Enumeration_Rep (Elit, Val);
6620 end if;
d6f39728 6621 end if;
6622 end if;
6623 end if;
6624
6625 Next (Assoc);
6626 end loop;
6627 end if;
6628
6629 -- Aggregate is fully processed. Now we check that a full set of
6630 -- representations was given, and that they are in range and in order.
6631 -- These checks are only done if no other errors occurred.
6632
6633 if not Err then
6634 Min := No_Uint;
6635 Max := No_Uint;
6636
6637 Elit := First_Literal (Enumtype);
6638 while Present (Elit) loop
6639 if No (Enumeration_Rep_Expr (Elit)) then
6640 Error_Msg_NE ("missing representation for&!", N, Elit);
6641
6642 else
6643 Val := Enumeration_Rep (Elit);
6644
6645 if Min = No_Uint then
6646 Min := Val;
6647 end if;
6648
6649 if Val /= No_Uint then
6650 if Max /= No_Uint and then Val <= Max then
6651 Error_Msg_NE
6652 ("enumeration value for& not ordered!",
e30c7d84 6653 Enumeration_Rep_Expr (Elit), Elit);
d6f39728 6654 end if;
6655
e30c7d84 6656 Max_Node := Enumeration_Rep_Expr (Elit);
d6f39728 6657 Max := Val;
6658 end if;
6659
e30c7d84 6660 -- If there is at least one literal whose representation is not
6661 -- equal to the Pos value, then note that this enumeration type
6662 -- has a non-standard representation.
d6f39728 6663
6664 if Val /= Enumeration_Pos (Elit) then
6665 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6666 end if;
6667 end if;
6668
6669 Next (Elit);
6670 end loop;
6671
6672 -- Now set proper size information
6673
6674 declare
6675 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6676
6677 begin
6678 if Has_Size_Clause (Enumtype) then
e30c7d84 6679
6680 -- All OK, if size is OK now
6681
6682 if RM_Size (Enumtype) >= Minsize then
d6f39728 6683 null;
6684
6685 else
e30c7d84 6686 -- Try if we can get by with biasing
6687
d6f39728 6688 Minsize :=
6689 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6690
e30c7d84 6691 -- Error message if even biasing does not work
6692
6693 if RM_Size (Enumtype) < Minsize then
6694 Error_Msg_Uint_1 := RM_Size (Enumtype);
6695 Error_Msg_Uint_2 := Max;
6696 Error_Msg_N
6697 ("previously given size (^) is too small "
6698 & "for this value (^)", Max_Node);
6699
6700 -- If biasing worked, indicate that we now have biased rep
d6f39728 6701
6702 else
b77e4501 6703 Set_Biased
6704 (Enumtype, Size_Clause (Enumtype), "size clause");
d6f39728 6705 end if;
6706 end if;
6707
6708 else
6709 Set_RM_Size (Enumtype, Minsize);
6710 Set_Enum_Esize (Enumtype);
6711 end if;
6712
6713 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6714 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6715 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6716 end;
6717 end if;
6718
39a0c1d3 6719 -- We repeat the too late test in case it froze itself
d6f39728 6720
6721 if Rep_Item_Too_Late (Enumtype, N) then
6722 null;
6723 end if;
d6f39728 6724 end Analyze_Enumeration_Representation_Clause;
6725
6726 ----------------------------
6727 -- Analyze_Free_Statement --
6728 ----------------------------
6729
6730 procedure Analyze_Free_Statement (N : Node_Id) is
6731 begin
6732 Analyze (Expression (N));
6733 end Analyze_Free_Statement;
6734
40ca69b9 6735 ---------------------------
6736 -- Analyze_Freeze_Entity --
6737 ---------------------------
6738
6739 procedure Analyze_Freeze_Entity (N : Node_Id) is
40ca69b9 6740 begin
d9f6a4ee 6741 Freeze_Entity_Checks (N);
6742 end Analyze_Freeze_Entity;
98f7db28 6743
d9f6a4ee 6744 -----------------------------------
6745 -- Analyze_Freeze_Generic_Entity --
6746 -----------------------------------
98f7db28 6747
d9f6a4ee 6748 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
61989dbb 6749 E : constant Entity_Id := Entity (N);
6750
d9f6a4ee 6751 begin
61989dbb 6752 if not Is_Frozen (E) and then Has_Delayed_Aspects (E) then
6753 Analyze_Aspects_At_Freeze_Point (E);
6754 end if;
6755
d9f6a4ee 6756 Freeze_Entity_Checks (N);
6757 end Analyze_Freeze_Generic_Entity;
40ca69b9 6758
d9f6a4ee 6759 ------------------------------------------
6760 -- Analyze_Record_Representation_Clause --
6761 ------------------------------------------
c8da6114 6762
d9f6a4ee 6763 -- Note: we check as much as we can here, but we can't do any checks
6764 -- based on the position values (e.g. overlap checks) until freeze time
6765 -- because especially in Ada 2005 (machine scalar mode), the processing
6766 -- for non-standard bit order can substantially change the positions.
6767 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6768 -- for the remainder of this processing.
d00681a7 6769
d9f6a4ee 6770 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6771 Ident : constant Node_Id := Identifier (N);
6772 Biased : Boolean;
6773 CC : Node_Id;
6774 Comp : Entity_Id;
6775 Fbit : Uint;
6776 Hbit : Uint := Uint_0;
6777 Lbit : Uint;
6778 Ocomp : Entity_Id;
6779 Posit : Uint;
6780 Rectype : Entity_Id;
6781 Recdef : Node_Id;
d00681a7 6782
d9f6a4ee 6783 function Is_Inherited (Comp : Entity_Id) return Boolean;
6784 -- True if Comp is an inherited component in a record extension
d00681a7 6785
d9f6a4ee 6786 ------------------
6787 -- Is_Inherited --
6788 ------------------
d00681a7 6789
d9f6a4ee 6790 function Is_Inherited (Comp : Entity_Id) return Boolean is
6791 Comp_Base : Entity_Id;
d00681a7 6792
d9f6a4ee 6793 begin
6794 if Ekind (Rectype) = E_Record_Subtype then
6795 Comp_Base := Original_Record_Component (Comp);
6796 else
6797 Comp_Base := Comp;
d00681a7 6798 end if;
6799
d9f6a4ee 6800 return Comp_Base /= Original_Record_Component (Comp_Base);
6801 end Is_Inherited;
d00681a7 6802
d9f6a4ee 6803 -- Local variables
d00681a7 6804
d9f6a4ee 6805 Is_Record_Extension : Boolean;
6806 -- True if Rectype is a record extension
d00681a7 6807
d9f6a4ee 6808 CR_Pragma : Node_Id := Empty;
6809 -- Points to N_Pragma node if Complete_Representation pragma present
d00681a7 6810
d9f6a4ee 6811 -- Start of processing for Analyze_Record_Representation_Clause
d00681a7 6812
d9f6a4ee 6813 begin
6814 if Ignore_Rep_Clauses then
2ff55065 6815 Kill_Rep_Clause (N);
d9f6a4ee 6816 return;
d00681a7 6817 end if;
98f7db28 6818
d9f6a4ee 6819 Find_Type (Ident);
6820 Rectype := Entity (Ident);
85377c9b 6821
d9f6a4ee 6822 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6823 return;
6824 else
6825 Rectype := Underlying_Type (Rectype);
6826 end if;
85377c9b 6827
d9f6a4ee 6828 -- First some basic error checks
85377c9b 6829
d9f6a4ee 6830 if not Is_Record_Type (Rectype) then
6831 Error_Msg_NE
6832 ("record type required, found}", Ident, First_Subtype (Rectype));
6833 return;
85377c9b 6834
d9f6a4ee 6835 elsif Scope (Rectype) /= Current_Scope then
6836 Error_Msg_N ("type must be declared in this scope", N);
6837 return;
85377c9b 6838
d9f6a4ee 6839 elsif not Is_First_Subtype (Rectype) then
6840 Error_Msg_N ("cannot give record rep clause for subtype", N);
6841 return;
9dc88aea 6842
d9f6a4ee 6843 elsif Has_Record_Rep_Clause (Rectype) then
6844 Error_Msg_N ("duplicate record rep clause ignored", N);
6845 return;
9dc88aea 6846
d9f6a4ee 6847 elsif Rep_Item_Too_Late (Rectype, N) then
6848 return;
9dc88aea 6849 end if;
fb7f2fc4 6850
2ced3742 6851 -- We know we have a first subtype, now possibly go to the anonymous
d9f6a4ee 6852 -- base type to determine whether Rectype is a record extension.
89f1e35c 6853
d9f6a4ee 6854 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6855 Is_Record_Extension :=
6856 Nkind (Recdef) = N_Derived_Type_Definition
6857 and then Present (Record_Extension_Part (Recdef));
89f1e35c 6858
d9f6a4ee 6859 if Present (Mod_Clause (N)) then
fb7f2fc4 6860 declare
d9f6a4ee 6861 Loc : constant Source_Ptr := Sloc (N);
6862 M : constant Node_Id := Mod_Clause (N);
6863 P : constant List_Id := Pragmas_Before (M);
6864 AtM_Nod : Node_Id;
6865
6866 Mod_Val : Uint;
6867 pragma Warnings (Off, Mod_Val);
fb7f2fc4 6868
6869 begin
d9f6a4ee 6870 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
fb7f2fc4 6871
d9f6a4ee 6872 if Warn_On_Obsolescent_Feature then
6873 Error_Msg_N
6874 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6875 Error_Msg_N
6876 ("\?j?use alignment attribute definition clause instead", N);
6877 end if;
fb7f2fc4 6878
d9f6a4ee 6879 if Present (P) then
6880 Analyze_List (P);
6881 end if;
89f1e35c 6882
d9f6a4ee 6883 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6884 -- the Mod clause into an alignment clause anyway, so that the
3ff5e35d 6885 -- back end can compute and back-annotate properly the size and
d9f6a4ee 6886 -- alignment of types that may include this record.
be9124d0 6887
d9f6a4ee 6888 -- This seems dubious, this destroys the source tree in a manner
6889 -- not detectable by ASIS ???
be9124d0 6890
d9f6a4ee 6891 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6892 AtM_Nod :=
6893 Make_Attribute_Definition_Clause (Loc,
83c6c069 6894 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
d9f6a4ee 6895 Chars => Name_Alignment,
6896 Expression => Relocate_Node (Expression (M)));
be9124d0 6897
d9f6a4ee 6898 Set_From_At_Mod (AtM_Nod);
6899 Insert_After (N, AtM_Nod);
6900 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6901 Set_Mod_Clause (N, Empty);
be9124d0 6902
d9f6a4ee 6903 else
6904 -- Get the alignment value to perform error checking
be9124d0 6905
d9f6a4ee 6906 Mod_Val := Get_Alignment_Value (Expression (M));
6907 end if;
6908 end;
6909 end if;
be9124d0 6910
d9f6a4ee 6911 -- For untagged types, clear any existing component clauses for the
6912 -- type. If the type is derived, this is what allows us to override
6913 -- a rep clause for the parent. For type extensions, the representation
6914 -- of the inherited components is inherited, so we want to keep previous
6915 -- component clauses for completeness.
be9124d0 6916
d9f6a4ee 6917 if not Is_Tagged_Type (Rectype) then
6918 Comp := First_Component_Or_Discriminant (Rectype);
6919 while Present (Comp) loop
6920 Set_Component_Clause (Comp, Empty);
6921 Next_Component_Or_Discriminant (Comp);
6922 end loop;
6923 end if;
be9124d0 6924
d9f6a4ee 6925 -- All done if no component clauses
be9124d0 6926
d9f6a4ee 6927 CC := First (Component_Clauses (N));
be9124d0 6928
d9f6a4ee 6929 if No (CC) then
6930 return;
6931 end if;
be9124d0 6932
d9f6a4ee 6933 -- A representation like this applies to the base type
be9124d0 6934
d9f6a4ee 6935 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6936 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6937 Set_Has_Specified_Layout (Base_Type (Rectype));
be9124d0 6938
d9f6a4ee 6939 -- Process the component clauses
be9124d0 6940
d9f6a4ee 6941 while Present (CC) loop
be9124d0 6942
d9f6a4ee 6943 -- Pragma
be9124d0 6944
d9f6a4ee 6945 if Nkind (CC) = N_Pragma then
6946 Analyze (CC);
be9124d0 6947
d9f6a4ee 6948 -- The only pragma of interest is Complete_Representation
be9124d0 6949
ddccc924 6950 if Pragma_Name (CC) = Name_Complete_Representation then
d9f6a4ee 6951 CR_Pragma := CC;
6952 end if;
be9124d0 6953
d9f6a4ee 6954 -- Processing for real component clause
be9124d0 6955
d9f6a4ee 6956 else
6957 Posit := Static_Integer (Position (CC));
6958 Fbit := Static_Integer (First_Bit (CC));
6959 Lbit := Static_Integer (Last_Bit (CC));
be9124d0 6960
d9f6a4ee 6961 if Posit /= No_Uint
6962 and then Fbit /= No_Uint
6963 and then Lbit /= No_Uint
6964 then
6965 if Posit < 0 then
f74a102b 6966 Error_Msg_N ("position cannot be negative", Position (CC));
be9124d0 6967
d9f6a4ee 6968 elsif Fbit < 0 then
f74a102b 6969 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
be9124d0 6970
d9f6a4ee 6971 -- The Last_Bit specified in a component clause must not be
6972 -- less than the First_Bit minus one (RM-13.5.1(10)).
be9124d0 6973
d9f6a4ee 6974 elsif Lbit < Fbit - 1 then
6975 Error_Msg_N
6976 ("last bit cannot be less than first bit minus one",
6977 Last_Bit (CC));
be9124d0 6978
d9f6a4ee 6979 -- Values look OK, so find the corresponding record component
6980 -- Even though the syntax allows an attribute reference for
6981 -- implementation-defined components, GNAT does not allow the
6982 -- tag to get an explicit position.
be9124d0 6983
d9f6a4ee 6984 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6985 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6986 Error_Msg_N ("position of tag cannot be specified", CC);
6987 else
6988 Error_Msg_N ("illegal component name", CC);
6989 end if;
be9124d0 6990
d9f6a4ee 6991 else
6992 Comp := First_Entity (Rectype);
6993 while Present (Comp) loop
6994 exit when Chars (Comp) = Chars (Component_Name (CC));
6995 Next_Entity (Comp);
6996 end loop;
be9124d0 6997
d9f6a4ee 6998 if No (Comp) then
be9124d0 6999
d9f6a4ee 7000 -- Maybe component of base type that is absent from
7001 -- statically constrained first subtype.
be9124d0 7002
d9f6a4ee 7003 Comp := First_Entity (Base_Type (Rectype));
7004 while Present (Comp) loop
7005 exit when Chars (Comp) = Chars (Component_Name (CC));
7006 Next_Entity (Comp);
7007 end loop;
7008 end if;
be9124d0 7009
d9f6a4ee 7010 if No (Comp) then
7011 Error_Msg_N
7012 ("component clause is for non-existent field", CC);
be9124d0 7013
d9f6a4ee 7014 -- Ada 2012 (AI05-0026): Any name that denotes a
7015 -- discriminant of an object of an unchecked union type
7016 -- shall not occur within a record_representation_clause.
be9124d0 7017
d9f6a4ee 7018 -- The general restriction of using record rep clauses on
7019 -- Unchecked_Union types has now been lifted. Since it is
7020 -- possible to introduce a record rep clause which mentions
7021 -- the discriminant of an Unchecked_Union in non-Ada 2012
7022 -- code, this check is applied to all versions of the
7023 -- language.
be9124d0 7024
d9f6a4ee 7025 elsif Ekind (Comp) = E_Discriminant
7026 and then Is_Unchecked_Union (Rectype)
7027 then
7028 Error_Msg_N
7029 ("cannot reference discriminant of unchecked union",
7030 Component_Name (CC));
be9124d0 7031
d9f6a4ee 7032 elsif Is_Record_Extension and then Is_Inherited (Comp) then
7033 Error_Msg_NE
7034 ("component clause not allowed for inherited "
7035 & "component&", CC, Comp);
40ca69b9 7036
d9f6a4ee 7037 elsif Present (Component_Clause (Comp)) then
462a079f 7038
d9f6a4ee 7039 -- Diagnose duplicate rep clause, or check consistency
7040 -- if this is an inherited component. In a double fault,
7041 -- there may be a duplicate inconsistent clause for an
7042 -- inherited component.
462a079f 7043
d9f6a4ee 7044 if Scope (Original_Record_Component (Comp)) = Rectype
7045 or else Parent (Component_Clause (Comp)) = N
7046 then
7047 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
7048 Error_Msg_N ("component clause previously given#", CC);
3062c401 7049
7050 else
7051 declare
7052 Rep1 : constant Node_Id := Component_Clause (Comp);
3062c401 7053 begin
7054 if Intval (Position (Rep1)) /=
7055 Intval (Position (CC))
7056 or else Intval (First_Bit (Rep1)) /=
7057 Intval (First_Bit (CC))
7058 or else Intval (Last_Bit (Rep1)) /=
7059 Intval (Last_Bit (CC))
7060 then
b9e61b2a 7061 Error_Msg_N
f74a102b 7062 ("component clause inconsistent with "
7063 & "representation of ancestor", CC);
6a06584c 7064
3062c401 7065 elsif Warn_On_Redundant_Constructs then
b9e61b2a 7066 Error_Msg_N
6a06584c 7067 ("?r?redundant confirming component clause "
7068 & "for component!", CC);
3062c401 7069 end if;
7070 end;
7071 end if;
d6f39728 7072
d2b860b4 7073 -- Normal case where this is the first component clause we
7074 -- have seen for this entity, so set it up properly.
7075
d6f39728 7076 else
83f8f0a6 7077 -- Make reference for field in record rep clause and set
7078 -- appropriate entity field in the field identifier.
7079
7080 Generate_Reference
7081 (Comp, Component_Name (CC), Set_Ref => False);
7082 Set_Entity (Component_Name (CC), Comp);
7083
2866d595 7084 -- Update Fbit and Lbit to the actual bit number
d6f39728 7085
7086 Fbit := Fbit + UI_From_Int (SSU) * Posit;
7087 Lbit := Lbit + UI_From_Int (SSU) * Posit;
7088
d6f39728 7089 if Has_Size_Clause (Rectype)
ada34def 7090 and then RM_Size (Rectype) <= Lbit
d6f39728 7091 then
7092 Error_Msg_N
7093 ("bit number out of range of specified size",
7094 Last_Bit (CC));
7095 else
7096 Set_Component_Clause (Comp, CC);
7097 Set_Component_Bit_Offset (Comp, Fbit);
7098 Set_Esize (Comp, 1 + (Lbit - Fbit));
7099 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
7100 Set_Normalized_Position (Comp, Fbit / SSU);
7101
a0fc8c5b 7102 if Warn_On_Overridden_Size
7103 and then Has_Size_Clause (Etype (Comp))
7104 and then RM_Size (Etype (Comp)) /= Esize (Comp)
7105 then
7106 Error_Msg_NE
1e3532e7 7107 ("?S?component size overrides size clause for&",
a0fc8c5b 7108 Component_Name (CC), Etype (Comp));
7109 end if;
7110
ea61a7ea 7111 -- This information is also set in the corresponding
7112 -- component of the base type, found by accessing the
7113 -- Original_Record_Component link if it is present.
d6f39728 7114
7115 Ocomp := Original_Record_Component (Comp);
7116
7117 if Hbit < Lbit then
7118 Hbit := Lbit;
7119 end if;
7120
7121 Check_Size
7122 (Component_Name (CC),
7123 Etype (Comp),
7124 Esize (Comp),
7125 Biased);
7126
b77e4501 7127 Set_Biased
7128 (Comp, First_Node (CC), "component clause", Biased);
cc46ff4b 7129
d6f39728 7130 if Present (Ocomp) then
7131 Set_Component_Clause (Ocomp, CC);
7132 Set_Component_Bit_Offset (Ocomp, Fbit);
7133 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
7134 Set_Normalized_Position (Ocomp, Fbit / SSU);
7135 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7136
7137 Set_Normalized_Position_Max
7138 (Ocomp, Normalized_Position (Ocomp));
7139
b77e4501 7140 -- Note: we don't use Set_Biased here, because we
7141 -- already gave a warning above if needed, and we
7142 -- would get a duplicate for the same name here.
7143
d6f39728 7144 Set_Has_Biased_Representation
7145 (Ocomp, Has_Biased_Representation (Comp));
7146 end if;
7147
7148 if Esize (Comp) < 0 then
7149 Error_Msg_N ("component size is negative", CC);
7150 end if;
7151 end if;
7152 end if;
7153 end if;
7154 end if;
7155 end if;
7156
7157 Next (CC);
7158 end loop;
7159
67278d60 7160 -- Check missing components if Complete_Representation pragma appeared
d6f39728 7161
67278d60 7162 if Present (CR_Pragma) then
7163 Comp := First_Component_Or_Discriminant (Rectype);
7164 while Present (Comp) loop
7165 if No (Component_Clause (Comp)) then
7166 Error_Msg_NE
7167 ("missing component clause for &", CR_Pragma, Comp);
7168 end if;
d6f39728 7169
67278d60 7170 Next_Component_Or_Discriminant (Comp);
7171 end loop;
d6f39728 7172
1e3532e7 7173 -- Give missing components warning if required
15ebb600 7174
fdd294d1 7175 elsif Warn_On_Unrepped_Components then
15ebb600 7176 declare
7177 Num_Repped_Components : Nat := 0;
7178 Num_Unrepped_Components : Nat := 0;
7179
7180 begin
7181 -- First count number of repped and unrepped components
7182
7183 Comp := First_Component_Or_Discriminant (Rectype);
7184 while Present (Comp) loop
7185 if Present (Component_Clause (Comp)) then
7186 Num_Repped_Components := Num_Repped_Components + 1;
7187 else
7188 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7189 end if;
7190
7191 Next_Component_Or_Discriminant (Comp);
7192 end loop;
7193
7194 -- We are only interested in the case where there is at least one
7195 -- unrepped component, and at least half the components have rep
7196 -- clauses. We figure that if less than half have them, then the
87f9eef5 7197 -- partial rep clause is really intentional. If the component
7198 -- type has no underlying type set at this point (as for a generic
7199 -- formal type), we don't know enough to give a warning on the
7200 -- component.
15ebb600 7201
7202 if Num_Unrepped_Components > 0
7203 and then Num_Unrepped_Components < Num_Repped_Components
7204 then
7205 Comp := First_Component_Or_Discriminant (Rectype);
7206 while Present (Comp) loop
83f8f0a6 7207 if No (Component_Clause (Comp))
3062c401 7208 and then Comes_From_Source (Comp)
87f9eef5 7209 and then Present (Underlying_Type (Etype (Comp)))
83f8f0a6 7210 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
67278d60 7211 or else Size_Known_At_Compile_Time
7212 (Underlying_Type (Etype (Comp))))
fdd294d1 7213 and then not Has_Warnings_Off (Rectype)
2be1f7d7 7214
7215 -- Ignore discriminant in unchecked union, since it is
7216 -- not there, and cannot have a component clause.
7217
7218 and then (not Is_Unchecked_Union (Rectype)
7219 or else Ekind (Comp) /= E_Discriminant)
83f8f0a6 7220 then
15ebb600 7221 Error_Msg_Sloc := Sloc (Comp);
7222 Error_Msg_NE
1e3532e7 7223 ("?C?no component clause given for & declared #",
15ebb600 7224 N, Comp);
7225 end if;
7226
7227 Next_Component_Or_Discriminant (Comp);
7228 end loop;
7229 end if;
7230 end;
d6f39728 7231 end if;
d6f39728 7232 end Analyze_Record_Representation_Clause;
7233
eb66e842 7234 -------------------------------------
7235 -- Build_Discrete_Static_Predicate --
7236 -------------------------------------
9ea61fdd 7237
eb66e842 7238 procedure Build_Discrete_Static_Predicate
7239 (Typ : Entity_Id;
7240 Expr : Node_Id;
7241 Nam : Name_Id)
9ea61fdd 7242 is
eb66e842 7243 Loc : constant Source_Ptr := Sloc (Expr);
9ea61fdd 7244
eb66e842 7245 Non_Static : exception;
7246 -- Raised if something non-static is found
9ea61fdd 7247
eb66e842 7248 Btyp : constant Entity_Id := Base_Type (Typ);
9ea61fdd 7249
eb66e842 7250 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7251 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7252 -- Low bound and high bound value of base type of Typ
9ea61fdd 7253
afc229da 7254 TLo : Uint;
7255 THi : Uint;
7256 -- Bounds for constructing the static predicate. We use the bound of the
7257 -- subtype if it is static, otherwise the corresponding base type bound.
7258 -- Note: a non-static subtype can have a static predicate.
9ea61fdd 7259
eb66e842 7260 type REnt is record
7261 Lo, Hi : Uint;
7262 end record;
7263 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7264 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7265 -- value.
9ea61fdd 7266
eb66e842 7267 type RList is array (Nat range <>) of REnt;
7268 -- A list of ranges. The ranges are sorted in increasing order, and are
7269 -- disjoint (there is a gap of at least one value between each range in
7270 -- the table). A value is in the set of ranges in Rlist if it lies
7271 -- within one of these ranges.
9ea61fdd 7272
eb66e842 7273 False_Range : constant RList :=
7274 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7275 -- An empty set of ranges represents a range list that can never be
7276 -- satisfied, since there are no ranges in which the value could lie,
7277 -- so it does not lie in any of them. False_Range is a canonical value
7278 -- for this empty set, but general processing should test for an Rlist
7279 -- with length zero (see Is_False predicate), since other null ranges
7280 -- may appear which must be treated as False.
5b5df4a9 7281
eb66e842 7282 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7283 -- Range representing True, value must be in the base range
5b5df4a9 7284
eb66e842 7285 function "and" (Left : RList; Right : RList) return RList;
7286 -- And's together two range lists, returning a range list. This is a set
7287 -- intersection operation.
5b5df4a9 7288
eb66e842 7289 function "or" (Left : RList; Right : RList) return RList;
7290 -- Or's together two range lists, returning a range list. This is a set
7291 -- union operation.
87f3d5d3 7292
eb66e842 7293 function "not" (Right : RList) return RList;
7294 -- Returns complement of a given range list, i.e. a range list
7295 -- representing all the values in TLo .. THi that are not in the input
7296 -- operand Right.
ed4adc99 7297
eb66e842 7298 function Build_Val (V : Uint) return Node_Id;
7299 -- Return an analyzed N_Identifier node referencing this value, suitable
5c6a5792 7300 -- for use as an entry in the Static_Discrte_Predicate list. This node
7301 -- is typed with the base type.
5b5df4a9 7302
eb66e842 7303 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7304 -- Return an analyzed N_Range node referencing this range, suitable for
5c6a5792 7305 -- use as an entry in the Static_Discrete_Predicate list. This node is
7306 -- typed with the base type.
5b5df4a9 7307
eb66e842 7308 function Get_RList (Exp : Node_Id) return RList;
7309 -- This is a recursive routine that converts the given expression into a
7310 -- list of ranges, suitable for use in building the static predicate.
5b5df4a9 7311
eb66e842 7312 function Is_False (R : RList) return Boolean;
7313 pragma Inline (Is_False);
7314 -- Returns True if the given range list is empty, and thus represents a
7315 -- False list of ranges that can never be satisfied.
87f3d5d3 7316
eb66e842 7317 function Is_True (R : RList) return Boolean;
7318 -- Returns True if R trivially represents the True predicate by having a
7319 -- single range from BLo to BHi.
5b5df4a9 7320
eb66e842 7321 function Is_Type_Ref (N : Node_Id) return Boolean;
7322 pragma Inline (Is_Type_Ref);
7323 -- Returns if True if N is a reference to the type for the predicate in
7324 -- the expression (i.e. if it is an identifier whose Chars field matches
7de4cba3 7325 -- the Nam given in the call). N must not be parenthesized, if the type
7326 -- name appears in parens, this routine will return False.
5b5df4a9 7327
eb66e842 7328 function Lo_Val (N : Node_Id) return Uint;
5c6a5792 7329 -- Given an entry from a Static_Discrete_Predicate list that is either
7330 -- a static expression or static range, gets either the expression value
7331 -- or the low bound of the range.
5b5df4a9 7332
eb66e842 7333 function Hi_Val (N : Node_Id) return Uint;
5c6a5792 7334 -- Given an entry from a Static_Discrete_Predicate list that is either
7335 -- a static expression or static range, gets either the expression value
7336 -- or the high bound of the range.
5b5df4a9 7337
eb66e842 7338 function Membership_Entry (N : Node_Id) return RList;
7339 -- Given a single membership entry (range, value, or subtype), returns
7340 -- the corresponding range list. Raises Static_Error if not static.
5b5df4a9 7341
eb66e842 7342 function Membership_Entries (N : Node_Id) return RList;
7343 -- Given an element on an alternatives list of a membership operation,
7344 -- returns the range list corresponding to this entry and all following
7345 -- entries (i.e. returns the "or" of this list of values).
b9e61b2a 7346
eb66e842 7347 function Stat_Pred (Typ : Entity_Id) return RList;
7348 -- Given a type, if it has a static predicate, then return the predicate
7349 -- as a range list, otherwise raise Non_Static.
c4968aa2 7350
eb66e842 7351 -----------
7352 -- "and" --
7353 -----------
c4968aa2 7354
eb66e842 7355 function "and" (Left : RList; Right : RList) return RList is
7356 FEnt : REnt;
7357 -- First range of result
c4968aa2 7358
eb66e842 7359 SLeft : Nat := Left'First;
7360 -- Start of rest of left entries
c4968aa2 7361
eb66e842 7362 SRight : Nat := Right'First;
7363 -- Start of rest of right entries
2072eaa9 7364
eb66e842 7365 begin
7366 -- If either range is True, return the other
5b5df4a9 7367
eb66e842 7368 if Is_True (Left) then
7369 return Right;
7370 elsif Is_True (Right) then
7371 return Left;
7372 end if;
87f3d5d3 7373
eb66e842 7374 -- If either range is False, return False
5b5df4a9 7375
eb66e842 7376 if Is_False (Left) or else Is_False (Right) then
7377 return False_Range;
7378 end if;
4c1fd062 7379
eb66e842 7380 -- Loop to remove entries at start that are disjoint, and thus just
7381 -- get discarded from the result entirely.
5b5df4a9 7382
eb66e842 7383 loop
7384 -- If no operands left in either operand, result is false
5b5df4a9 7385
eb66e842 7386 if SLeft > Left'Last or else SRight > Right'Last then
7387 return False_Range;
5b5df4a9 7388
eb66e842 7389 -- Discard first left operand entry if disjoint with right
5b5df4a9 7390
eb66e842 7391 elsif Left (SLeft).Hi < Right (SRight).Lo then
7392 SLeft := SLeft + 1;
5b5df4a9 7393
eb66e842 7394 -- Discard first right operand entry if disjoint with left
5b5df4a9 7395
eb66e842 7396 elsif Right (SRight).Hi < Left (SLeft).Lo then
7397 SRight := SRight + 1;
5b5df4a9 7398
eb66e842 7399 -- Otherwise we have an overlapping entry
5b5df4a9 7400
eb66e842 7401 else
7402 exit;
7403 end if;
7404 end loop;
5b5df4a9 7405
eb66e842 7406 -- Now we have two non-null operands, and first entries overlap. The
7407 -- first entry in the result will be the overlapping part of these
7408 -- two entries.
47a46747 7409
eb66e842 7410 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7411 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
47a46747 7412
eb66e842 7413 -- Now we can remove the entry that ended at a lower value, since its
7414 -- contribution is entirely contained in Fent.
5b5df4a9 7415
eb66e842 7416 if Left (SLeft).Hi <= Right (SRight).Hi then
7417 SLeft := SLeft + 1;
7418 else
7419 SRight := SRight + 1;
7420 end if;
5b5df4a9 7421
eb66e842 7422 -- Compute result by concatenating this first entry with the "and" of
7423 -- the remaining parts of the left and right operands. Note that if
7424 -- either of these is empty, "and" will yield empty, so that we will
7425 -- end up with just Fent, which is what we want in that case.
5b5df4a9 7426
eb66e842 7427 return
7428 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7429 end "and";
fb7f2fc4 7430
eb66e842 7431 -----------
7432 -- "not" --
7433 -----------
fb7f2fc4 7434
eb66e842 7435 function "not" (Right : RList) return RList is
7436 begin
7437 -- Return True if False range
fb7f2fc4 7438
eb66e842 7439 if Is_False (Right) then
7440 return True_Range;
7441 end if;
ed4adc99 7442
eb66e842 7443 -- Return False if True range
fb7f2fc4 7444
eb66e842 7445 if Is_True (Right) then
7446 return False_Range;
7447 end if;
fb7f2fc4 7448
eb66e842 7449 -- Here if not trivial case
87f3d5d3 7450
eb66e842 7451 declare
7452 Result : RList (1 .. Right'Length + 1);
7453 -- May need one more entry for gap at beginning and end
87f3d5d3 7454
eb66e842 7455 Count : Nat := 0;
7456 -- Number of entries stored in Result
4098232e 7457
eb66e842 7458 begin
7459 -- Gap at start
4098232e 7460
eb66e842 7461 if Right (Right'First).Lo > TLo then
7462 Count := Count + 1;
7463 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7464 end if;
ed4adc99 7465
eb66e842 7466 -- Gaps between ranges
ed4adc99 7467
eb66e842 7468 for J in Right'First .. Right'Last - 1 loop
7469 Count := Count + 1;
7470 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7471 end loop;
5b5df4a9 7472
eb66e842 7473 -- Gap at end
5b5df4a9 7474
eb66e842 7475 if Right (Right'Last).Hi < THi then
7476 Count := Count + 1;
7477 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7478 end if;
5b5df4a9 7479
eb66e842 7480 return Result (1 .. Count);
7481 end;
7482 end "not";
5b5df4a9 7483
eb66e842 7484 ----------
7485 -- "or" --
7486 ----------
5b5df4a9 7487
eb66e842 7488 function "or" (Left : RList; Right : RList) return RList is
7489 FEnt : REnt;
7490 -- First range of result
5b5df4a9 7491
eb66e842 7492 SLeft : Nat := Left'First;
7493 -- Start of rest of left entries
5b5df4a9 7494
eb66e842 7495 SRight : Nat := Right'First;
7496 -- Start of rest of right entries
5b5df4a9 7497
eb66e842 7498 begin
7499 -- If either range is True, return True
5b5df4a9 7500
eb66e842 7501 if Is_True (Left) or else Is_True (Right) then
7502 return True_Range;
7503 end if;
5b5df4a9 7504
eb66e842 7505 -- If either range is False (empty), return the other
5b5df4a9 7506
eb66e842 7507 if Is_False (Left) then
7508 return Right;
7509 elsif Is_False (Right) then
7510 return Left;
7511 end if;
5b5df4a9 7512
eb66e842 7513 -- Initialize result first entry from left or right operand depending
7514 -- on which starts with the lower range.
5b5df4a9 7515
eb66e842 7516 if Left (SLeft).Lo < Right (SRight).Lo then
7517 FEnt := Left (SLeft);
7518 SLeft := SLeft + 1;
7519 else
7520 FEnt := Right (SRight);
7521 SRight := SRight + 1;
7522 end if;
5b5df4a9 7523
eb66e842 7524 -- This loop eats ranges from left and right operands that are
7525 -- contiguous with the first range we are gathering.
9ea61fdd 7526
eb66e842 7527 loop
7528 -- Eat first entry in left operand if contiguous or overlapped by
7529 -- gathered first operand of result.
9ea61fdd 7530
eb66e842 7531 if SLeft <= Left'Last
7532 and then Left (SLeft).Lo <= FEnt.Hi + 1
7533 then
7534 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7535 SLeft := SLeft + 1;
9ea61fdd 7536
eb66e842 7537 -- Eat first entry in right operand if contiguous or overlapped by
7538 -- gathered right operand of result.
9ea61fdd 7539
eb66e842 7540 elsif SRight <= Right'Last
7541 and then Right (SRight).Lo <= FEnt.Hi + 1
7542 then
7543 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7544 SRight := SRight + 1;
9ea61fdd 7545
eb66e842 7546 -- All done if no more entries to eat
5b5df4a9 7547
eb66e842 7548 else
7549 exit;
7550 end if;
7551 end loop;
5b5df4a9 7552
eb66e842 7553 -- Obtain result as the first entry we just computed, concatenated
7554 -- to the "or" of the remaining results (if one operand is empty,
7555 -- this will just concatenate with the other
5b5df4a9 7556
eb66e842 7557 return
7558 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7559 end "or";
5b5df4a9 7560
eb66e842 7561 -----------------
7562 -- Build_Range --
7563 -----------------
5b5df4a9 7564
eb66e842 7565 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7566 Result : Node_Id;
5b5df4a9 7567 begin
eb66e842 7568 Result :=
7569 Make_Range (Loc,
7570 Low_Bound => Build_Val (Lo),
7571 High_Bound => Build_Val (Hi));
7572 Set_Etype (Result, Btyp);
7573 Set_Analyzed (Result);
7574 return Result;
7575 end Build_Range;
5b5df4a9 7576
eb66e842 7577 ---------------
7578 -- Build_Val --
7579 ---------------
5b5df4a9 7580
eb66e842 7581 function Build_Val (V : Uint) return Node_Id is
7582 Result : Node_Id;
5b5df4a9 7583
eb66e842 7584 begin
7585 if Is_Enumeration_Type (Typ) then
7586 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7587 else
7588 Result := Make_Integer_Literal (Loc, V);
7589 end if;
5b5df4a9 7590
eb66e842 7591 Set_Etype (Result, Btyp);
7592 Set_Is_Static_Expression (Result);
7593 Set_Analyzed (Result);
7594 return Result;
7595 end Build_Val;
87f3d5d3 7596
eb66e842 7597 ---------------
7598 -- Get_RList --
7599 ---------------
87f3d5d3 7600
eb66e842 7601 function Get_RList (Exp : Node_Id) return RList is
7602 Op : Node_Kind;
7603 Val : Uint;
87f3d5d3 7604
eb66e842 7605 begin
7606 -- Static expression can only be true or false
87f3d5d3 7607
eb66e842 7608 if Is_OK_Static_Expression (Exp) then
7609 if Expr_Value (Exp) = 0 then
7610 return False_Range;
7611 else
7612 return True_Range;
9ea61fdd 7613 end if;
eb66e842 7614 end if;
87f3d5d3 7615
eb66e842 7616 -- Otherwise test node type
192b8dab 7617
eb66e842 7618 Op := Nkind (Exp);
192b8dab 7619
eb66e842 7620 case Op is
5d3fb947 7621
eb66e842 7622 -- And
5d3fb947 7623
99378362 7624 when N_And_Then
7625 | N_Op_And
7626 =>
eb66e842 7627 return Get_RList (Left_Opnd (Exp))
7628 and
7629 Get_RList (Right_Opnd (Exp));
5b5df4a9 7630
eb66e842 7631 -- Or
9dc88aea 7632
99378362 7633 when N_Op_Or
7634 | N_Or_Else
7635 =>
eb66e842 7636 return Get_RList (Left_Opnd (Exp))
7637 or
7638 Get_RList (Right_Opnd (Exp));
7c443ae8 7639
eb66e842 7640 -- Not
9dc88aea 7641
eb66e842 7642 when N_Op_Not =>
7643 return not Get_RList (Right_Opnd (Exp));
9dc88aea 7644
eb66e842 7645 -- Comparisons of type with static value
84c8f0b8 7646
eb66e842 7647 when N_Op_Compare =>
490beba6 7648
eb66e842 7649 -- Type is left operand
9dc88aea 7650
eb66e842 7651 if Is_Type_Ref (Left_Opnd (Exp))
7652 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7653 then
7654 Val := Expr_Value (Right_Opnd (Exp));
84c8f0b8 7655
eb66e842 7656 -- Typ is right operand
84c8f0b8 7657
eb66e842 7658 elsif Is_Type_Ref (Right_Opnd (Exp))
7659 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7660 then
7661 Val := Expr_Value (Left_Opnd (Exp));
84c8f0b8 7662
eb66e842 7663 -- Invert sense of comparison
84c8f0b8 7664
eb66e842 7665 case Op is
7666 when N_Op_Gt => Op := N_Op_Lt;
7667 when N_Op_Lt => Op := N_Op_Gt;
7668 when N_Op_Ge => Op := N_Op_Le;
7669 when N_Op_Le => Op := N_Op_Ge;
7670 when others => null;
7671 end case;
84c8f0b8 7672
eb66e842 7673 -- Other cases are non-static
34d045d3 7674
eb66e842 7675 else
7676 raise Non_Static;
7677 end if;
9dc88aea 7678
eb66e842 7679 -- Construct range according to comparison operation
9dc88aea 7680
eb66e842 7681 case Op is
7682 when N_Op_Eq =>
7683 return RList'(1 => REnt'(Val, Val));
9dc88aea 7684
eb66e842 7685 when N_Op_Ge =>
7686 return RList'(1 => REnt'(Val, BHi));
84c8f0b8 7687
eb66e842 7688 when N_Op_Gt =>
7689 return RList'(1 => REnt'(Val + 1, BHi));
84c8f0b8 7690
eb66e842 7691 when N_Op_Le =>
7692 return RList'(1 => REnt'(BLo, Val));
fb7f2fc4 7693
eb66e842 7694 when N_Op_Lt =>
7695 return RList'(1 => REnt'(BLo, Val - 1));
9dc88aea 7696
eb66e842 7697 when N_Op_Ne =>
7698 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9dc88aea 7699
eb66e842 7700 when others =>
7701 raise Program_Error;
7702 end case;
9dc88aea 7703
eb66e842 7704 -- Membership (IN)
9dc88aea 7705
eb66e842 7706 when N_In =>
7707 if not Is_Type_Ref (Left_Opnd (Exp)) then
7708 raise Non_Static;
7709 end if;
9dc88aea 7710
eb66e842 7711 if Present (Right_Opnd (Exp)) then
7712 return Membership_Entry (Right_Opnd (Exp));
7713 else
7714 return Membership_Entries (First (Alternatives (Exp)));
7715 end if;
9dc88aea 7716
eb66e842 7717 -- Negative membership (NOT IN)
9dc88aea 7718
eb66e842 7719 when N_Not_In =>
7720 if not Is_Type_Ref (Left_Opnd (Exp)) then
7721 raise Non_Static;
7722 end if;
9dc88aea 7723
eb66e842 7724 if Present (Right_Opnd (Exp)) then
7725 return not Membership_Entry (Right_Opnd (Exp));
7726 else
7727 return not Membership_Entries (First (Alternatives (Exp)));
7728 end if;
9dc88aea 7729
eb66e842 7730 -- Function call, may be call to static predicate
9dc88aea 7731
eb66e842 7732 when N_Function_Call =>
7733 if Is_Entity_Name (Name (Exp)) then
7734 declare
7735 Ent : constant Entity_Id := Entity (Name (Exp));
7736 begin
7737 if Is_Predicate_Function (Ent)
7738 or else
7739 Is_Predicate_Function_M (Ent)
7740 then
7741 return Stat_Pred (Etype (First_Formal (Ent)));
7742 end if;
7743 end;
7744 end if;
9dc88aea 7745
eb66e842 7746 -- Other function call cases are non-static
9dc88aea 7747
eb66e842 7748 raise Non_Static;
490beba6 7749
eb66e842 7750 -- Qualified expression, dig out the expression
c92e878b 7751
eb66e842 7752 when N_Qualified_Expression =>
7753 return Get_RList (Expression (Exp));
4c1fd062 7754
eb66e842 7755 when N_Case_Expression =>
7756 declare
7757 Alt : Node_Id;
7758 Choices : List_Id;
7759 Dep : Node_Id;
4c1fd062 7760
eb66e842 7761 begin
7762 if not Is_Entity_Name (Expression (Expr))
7763 or else Etype (Expression (Expr)) /= Typ
7764 then
7765 Error_Msg_N
7766 ("expression must denaote subtype", Expression (Expr));
7767 return False_Range;
7768 end if;
9dc88aea 7769
eb66e842 7770 -- Collect discrete choices in all True alternatives
9dc88aea 7771
eb66e842 7772 Choices := New_List;
7773 Alt := First (Alternatives (Exp));
7774 while Present (Alt) loop
7775 Dep := Expression (Alt);
34d045d3 7776
cda40848 7777 if not Is_OK_Static_Expression (Dep) then
eb66e842 7778 raise Non_Static;
ebbab42d 7779
eb66e842 7780 elsif Is_True (Expr_Value (Dep)) then
7781 Append_List_To (Choices,
7782 New_Copy_List (Discrete_Choices (Alt)));
7783 end if;
fb7f2fc4 7784
eb66e842 7785 Next (Alt);
7786 end loop;
9dc88aea 7787
eb66e842 7788 return Membership_Entries (First (Choices));
7789 end;
9dc88aea 7790
eb66e842 7791 -- Expression with actions: if no actions, dig out expression
9dc88aea 7792
eb66e842 7793 when N_Expression_With_Actions =>
7794 if Is_Empty_List (Actions (Exp)) then
7795 return Get_RList (Expression (Exp));
7796 else
7797 raise Non_Static;
7798 end if;
9dc88aea 7799
eb66e842 7800 -- Xor operator
490beba6 7801
eb66e842 7802 when N_Op_Xor =>
7803 return (Get_RList (Left_Opnd (Exp))
7804 and not Get_RList (Right_Opnd (Exp)))
7805 or (Get_RList (Right_Opnd (Exp))
7806 and not Get_RList (Left_Opnd (Exp)));
9dc88aea 7807
eb66e842 7808 -- Any other node type is non-static
fb7f2fc4 7809
eb66e842 7810 when others =>
7811 raise Non_Static;
7812 end case;
7813 end Get_RList;
fb7f2fc4 7814
eb66e842 7815 ------------
7816 -- Hi_Val --
7817 ------------
fb7f2fc4 7818
eb66e842 7819 function Hi_Val (N : Node_Id) return Uint is
7820 begin
cda40848 7821 if Is_OK_Static_Expression (N) then
eb66e842 7822 return Expr_Value (N);
7823 else
7824 pragma Assert (Nkind (N) = N_Range);
7825 return Expr_Value (High_Bound (N));
7826 end if;
7827 end Hi_Val;
fb7f2fc4 7828
eb66e842 7829 --------------
7830 -- Is_False --
7831 --------------
fb7f2fc4 7832
eb66e842 7833 function Is_False (R : RList) return Boolean is
7834 begin
7835 return R'Length = 0;
7836 end Is_False;
9dc88aea 7837
eb66e842 7838 -------------
7839 -- Is_True --
7840 -------------
9dc88aea 7841
eb66e842 7842 function Is_True (R : RList) return Boolean is
7843 begin
7844 return R'Length = 1
7845 and then R (R'First).Lo = BLo
7846 and then R (R'First).Hi = BHi;
7847 end Is_True;
9dc88aea 7848
eb66e842 7849 -----------------
7850 -- Is_Type_Ref --
7851 -----------------
9dc88aea 7852
eb66e842 7853 function Is_Type_Ref (N : Node_Id) return Boolean is
7854 begin
7de4cba3 7855 return Nkind (N) = N_Identifier
7856 and then Chars (N) = Nam
7857 and then Paren_Count (N) = 0;
eb66e842 7858 end Is_Type_Ref;
9dc88aea 7859
eb66e842 7860 ------------
7861 -- Lo_Val --
7862 ------------
9dc88aea 7863
eb66e842 7864 function Lo_Val (N : Node_Id) return Uint is
84c8f0b8 7865 begin
cda40848 7866 if Is_OK_Static_Expression (N) then
eb66e842 7867 return Expr_Value (N);
84c8f0b8 7868 else
eb66e842 7869 pragma Assert (Nkind (N) = N_Range);
7870 return Expr_Value (Low_Bound (N));
84c8f0b8 7871 end if;
eb66e842 7872 end Lo_Val;
d97beb2f 7873
eb66e842 7874 ------------------------
7875 -- Membership_Entries --
7876 ------------------------
d97beb2f 7877
eb66e842 7878 function Membership_Entries (N : Node_Id) return RList is
84c8f0b8 7879 begin
eb66e842 7880 if No (Next (N)) then
7881 return Membership_Entry (N);
84c8f0b8 7882 else
eb66e842 7883 return Membership_Entry (N) or Membership_Entries (Next (N));
84c8f0b8 7884 end if;
eb66e842 7885 end Membership_Entries;
84c8f0b8 7886
eb66e842 7887 ----------------------
7888 -- Membership_Entry --
7889 ----------------------
84c8f0b8 7890
eb66e842 7891 function Membership_Entry (N : Node_Id) return RList is
7892 Val : Uint;
7893 SLo : Uint;
7894 SHi : Uint;
d97beb2f 7895
eb66e842 7896 begin
7897 -- Range case
d97beb2f 7898
eb66e842 7899 if Nkind (N) = N_Range then
cda40848 7900 if not Is_OK_Static_Expression (Low_Bound (N))
eb66e842 7901 or else
cda40848 7902 not Is_OK_Static_Expression (High_Bound (N))
eb66e842 7903 then
7904 raise Non_Static;
7905 else
7906 SLo := Expr_Value (Low_Bound (N));
7907 SHi := Expr_Value (High_Bound (N));
7908 return RList'(1 => REnt'(SLo, SHi));
7909 end if;
84c8f0b8 7910
eb66e842 7911 -- Static expression case
84c8f0b8 7912
cda40848 7913 elsif Is_OK_Static_Expression (N) then
eb66e842 7914 Val := Expr_Value (N);
7915 return RList'(1 => REnt'(Val, Val));
d97beb2f 7916
eb66e842 7917 -- Identifier (other than static expression) case
d97beb2f 7918
eb66e842 7919 else pragma Assert (Nkind (N) = N_Identifier);
d97beb2f 7920
eb66e842 7921 -- Type case
d97beb2f 7922
eb66e842 7923 if Is_Type (Entity (N)) then
d97beb2f 7924
eb66e842 7925 -- If type has predicates, process them
d97beb2f 7926
eb66e842 7927 if Has_Predicates (Entity (N)) then
7928 return Stat_Pred (Entity (N));
d97beb2f 7929
eb66e842 7930 -- For static subtype without predicates, get range
9dc88aea 7931
cda40848 7932 elsif Is_OK_Static_Subtype (Entity (N)) then
eb66e842 7933 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7934 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7935 return RList'(1 => REnt'(SLo, SHi));
9f269bd8 7936
eb66e842 7937 -- Any other type makes us non-static
9f269bd8 7938
eb66e842 7939 else
7940 raise Non_Static;
7941 end if;
84c8f0b8 7942
eb66e842 7943 -- Any other kind of identifier in predicate (e.g. a non-static
7944 -- expression value) means this is not a static predicate.
84c8f0b8 7945
eb66e842 7946 else
7947 raise Non_Static;
7948 end if;
7949 end if;
7950 end Membership_Entry;
84c8f0b8 7951
eb66e842 7952 ---------------
7953 -- Stat_Pred --
7954 ---------------
84c8f0b8 7955
eb66e842 7956 function Stat_Pred (Typ : Entity_Id) return RList is
7957 begin
7958 -- Not static if type does not have static predicates
84c8f0b8 7959
5c6a5792 7960 if not Has_Static_Predicate (Typ) then
eb66e842 7961 raise Non_Static;
7962 end if;
84c8f0b8 7963
eb66e842 7964 -- Otherwise we convert the predicate list to a range list
84c8f0b8 7965
eb66e842 7966 declare
5c6a5792 7967 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7968 Result : RList (1 .. List_Length (Spred));
eb66e842 7969 P : Node_Id;
84c8f0b8 7970
eb66e842 7971 begin
5c6a5792 7972 P := First (Static_Discrete_Predicate (Typ));
eb66e842 7973 for J in Result'Range loop
7974 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7975 Next (P);
7976 end loop;
84c8f0b8 7977
eb66e842 7978 return Result;
7979 end;
7980 end Stat_Pred;
84c8f0b8 7981
eb66e842 7982 -- Start of processing for Build_Discrete_Static_Predicate
84c8f0b8 7983
eb66e842 7984 begin
fdec445e 7985 -- Establish bounds for the predicate
afc229da 7986
7987 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7988 TLo := Expr_Value (Type_Low_Bound (Typ));
7989 else
7990 TLo := BLo;
7991 end if;
7992
7993 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7994 THi := Expr_Value (Type_High_Bound (Typ));
7995 else
7996 THi := BHi;
7997 end if;
7998
eb66e842 7999 -- Analyze the expression to see if it is a static predicate
84c8f0b8 8000
eb66e842 8001 declare
8002 Ranges : constant RList := Get_RList (Expr);
8003 -- Range list from expression if it is static
84c8f0b8 8004
eb66e842 8005 Plist : List_Id;
84c8f0b8 8006
eb66e842 8007 begin
8008 -- Convert range list into a form for the static predicate. In the
8009 -- Ranges array, we just have raw ranges, these must be converted
8010 -- to properly typed and analyzed static expressions or range nodes.
84c8f0b8 8011
eb66e842 8012 -- Note: here we limit ranges to the ranges of the subtype, so that
8013 -- a predicate is always false for values outside the subtype. That
8014 -- seems fine, such values are invalid anyway, and considering them
8015 -- to fail the predicate seems allowed and friendly, and furthermore
8016 -- simplifies processing for case statements and loops.
84c8f0b8 8017
eb66e842 8018 Plist := New_List;
8019
8020 for J in Ranges'Range loop
84c8f0b8 8021 declare
eb66e842 8022 Lo : Uint := Ranges (J).Lo;
8023 Hi : Uint := Ranges (J).Hi;
84c8f0b8 8024
eb66e842 8025 begin
8026 -- Ignore completely out of range entry
84c8f0b8 8027
eb66e842 8028 if Hi < TLo or else Lo > THi then
8029 null;
84c8f0b8 8030
eb66e842 8031 -- Otherwise process entry
84c8f0b8 8032
eb66e842 8033 else
8034 -- Adjust out of range value to subtype range
490beba6 8035
eb66e842 8036 if Lo < TLo then
8037 Lo := TLo;
8038 end if;
490beba6 8039
eb66e842 8040 if Hi > THi then
8041 Hi := THi;
8042 end if;
84c8f0b8 8043
eb66e842 8044 -- Convert range into required form
84c8f0b8 8045
eb66e842 8046 Append_To (Plist, Build_Range (Lo, Hi));
84c8f0b8 8047 end if;
eb66e842 8048 end;
8049 end loop;
84c8f0b8 8050
eb66e842 8051 -- Processing was successful and all entries were static, so now we
8052 -- can store the result as the predicate list.
84c8f0b8 8053
5c6a5792 8054 Set_Static_Discrete_Predicate (Typ, Plist);
84c8f0b8 8055
eb66e842 8056 -- The processing for static predicates put the expression into
8057 -- canonical form as a series of ranges. It also eliminated
8058 -- duplicates and collapsed and combined ranges. We might as well
8059 -- replace the alternatives list of the right operand of the
8060 -- membership test with the static predicate list, which will
8061 -- usually be more efficient.
84c8f0b8 8062
eb66e842 8063 declare
8064 New_Alts : constant List_Id := New_List;
8065 Old_Node : Node_Id;
8066 New_Node : Node_Id;
84c8f0b8 8067
eb66e842 8068 begin
8069 Old_Node := First (Plist);
8070 while Present (Old_Node) loop
8071 New_Node := New_Copy (Old_Node);
84c8f0b8 8072
eb66e842 8073 if Nkind (New_Node) = N_Range then
8074 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
8075 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
8076 end if;
84c8f0b8 8077
eb66e842 8078 Append_To (New_Alts, New_Node);
8079 Next (Old_Node);
8080 end loop;
84c8f0b8 8081
eb66e842 8082 -- If empty list, replace by False
84c8f0b8 8083
eb66e842 8084 if Is_Empty_List (New_Alts) then
8085 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
84c8f0b8 8086
eb66e842 8087 -- Else replace by set membership test
84c8f0b8 8088
eb66e842 8089 else
8090 Rewrite (Expr,
8091 Make_In (Loc,
8092 Left_Opnd => Make_Identifier (Loc, Nam),
8093 Right_Opnd => Empty,
8094 Alternatives => New_Alts));
490beba6 8095
eb66e842 8096 -- Resolve new expression in function context
490beba6 8097
eb66e842 8098 Install_Formals (Predicate_Function (Typ));
8099 Push_Scope (Predicate_Function (Typ));
8100 Analyze_And_Resolve (Expr, Standard_Boolean);
8101 Pop_Scope;
8102 end if;
8103 end;
8104 end;
9ab32fe9 8105
eb66e842 8106 -- If non-static, return doing nothing
9ab32fe9 8107
eb66e842 8108 exception
8109 when Non_Static =>
8110 return;
8111 end Build_Discrete_Static_Predicate;
64cc9e5d 8112
ee2b7923 8113 --------------------------------
8114 -- Build_Export_Import_Pragma --
8115 --------------------------------
8116
8117 function Build_Export_Import_Pragma
8118 (Asp : Node_Id;
8119 Id : Entity_Id) return Node_Id
8120 is
8121 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
8122 Expr : constant Node_Id := Expression (Asp);
8123 Loc : constant Source_Ptr := Sloc (Asp);
8124
8125 Args : List_Id;
8126 Conv : Node_Id;
8127 Conv_Arg : Node_Id;
8128 Dummy_1 : Node_Id;
8129 Dummy_2 : Node_Id;
8130 EN : Node_Id;
8131 LN : Node_Id;
8132 Prag : Node_Id;
8133
8134 Create_Pragma : Boolean := False;
8135 -- This flag is set when the aspect form is such that it warrants the
8136 -- creation of a corresponding pragma.
8137
8138 begin
8139 if Present (Expr) then
8140 if Error_Posted (Expr) then
8141 null;
8142
8143 elsif Is_True (Expr_Value (Expr)) then
8144 Create_Pragma := True;
8145 end if;
8146
8147 -- Otherwise the aspect defaults to True
8148
8149 else
8150 Create_Pragma := True;
8151 end if;
8152
8153 -- Nothing to do when the expression is False or is erroneous
8154
8155 if not Create_Pragma then
8156 return Empty;
8157 end if;
8158
8159 -- Obtain all interfacing aspects that apply to the related entity
8160
8161 Get_Interfacing_Aspects
8162 (Iface_Asp => Asp,
8163 Conv_Asp => Conv,
8164 EN_Asp => EN,
8165 Expo_Asp => Dummy_1,
8166 Imp_Asp => Dummy_2,
8167 LN_Asp => LN);
8168
8169 Args := New_List;
8170
8171 -- Handle the convention argument
8172
8173 if Present (Conv) then
8174 Conv_Arg := New_Copy_Tree (Expression (Conv));
8175
8176 -- Assume convention "Ada' when aspect Convention is missing
8177
8178 else
8179 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8180 end if;
8181
8182 Append_To (Args,
8183 Make_Pragma_Argument_Association (Loc,
8184 Chars => Name_Convention,
8185 Expression => Conv_Arg));
8186
8187 -- Handle the entity argument
8188
8189 Append_To (Args,
8190 Make_Pragma_Argument_Association (Loc,
8191 Chars => Name_Entity,
8192 Expression => New_Occurrence_Of (Id, Loc)));
8193
8194 -- Handle the External_Name argument
8195
8196 if Present (EN) then
8197 Append_To (Args,
8198 Make_Pragma_Argument_Association (Loc,
8199 Chars => Name_External_Name,
8200 Expression => New_Copy_Tree (Expression (EN))));
8201 end if;
8202
8203 -- Handle the Link_Name argument
8204
8205 if Present (LN) then
8206 Append_To (Args,
8207 Make_Pragma_Argument_Association (Loc,
8208 Chars => Name_Link_Name,
8209 Expression => New_Copy_Tree (Expression (LN))));
8210 end if;
8211
8212 -- Generate:
8213 -- pragma Export/Import
8214 -- (Convention => <Conv>/Ada,
8215 -- Entity => <Id>,
8216 -- [External_Name => <EN>,]
8217 -- [Link_Name => <LN>]);
8218
8219 Prag :=
8220 Make_Pragma (Loc,
8221 Pragma_Identifier =>
8222 Make_Identifier (Loc, Chars (Identifier (Asp))),
8223 Pragma_Argument_Associations => Args);
8224
8225 -- Decorate the relevant aspect and the pragma
8226
8227 Set_Aspect_Rep_Item (Asp, Prag);
8228
8229 Set_Corresponding_Aspect (Prag, Asp);
8230 Set_From_Aspect_Specification (Prag);
8231 Set_Parent (Prag, Asp);
8232
8233 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8234 Set_Import_Pragma (Id, Prag);
8235 end if;
8236
8237 return Prag;
8238 end Build_Export_Import_Pragma;
8239
eb66e842 8240 -------------------------------
8241 -- Build_Predicate_Functions --
8242 -------------------------------
d9f6a4ee 8243
eb66e842 8244 -- The procedures that are constructed here have the form:
d9f6a4ee 8245
eb66e842 8246 -- function typPredicate (Ixxx : typ) return Boolean is
8247 -- begin
8248 -- return
75491446 8249 -- typ1Predicate (typ1 (Ixxx))
eb66e842 8250 -- and then typ2Predicate (typ2 (Ixxx))
8251 -- and then ...;
75491446 8252 -- exp1 and then exp2 and then ...
eb66e842 8253 -- end typPredicate;
d9f6a4ee 8254
eb66e842 8255 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8256 -- this is the point at which these expressions get analyzed, providing the
8257 -- required delay, and typ1, typ2, are entities from which predicates are
8258 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8259 -- use this function even if checks are off, e.g. for membership tests.
d9f6a4ee 8260
75491446 8261 -- Note that the inherited predicates are evaluated first, as required by
8262 -- AI12-0071-1.
8263
8264 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8265 -- the form of this return expression.
8266
eb66e842 8267 -- If the expression has at least one Raise_Expression, then we also build
8268 -- the typPredicateM version of the function, in which any occurrence of a
8269 -- Raise_Expression is converted to "return False".
d9f6a4ee 8270
1ecdfe4b 8271 -- WARNING: This routine manages Ghost regions. Return statements must be
8272 -- replaced by gotos which jump to the end of the routine and restore the
8273 -- Ghost mode.
8274
eb66e842 8275 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8276 Loc : constant Source_Ptr := Sloc (Typ);
d9f6a4ee 8277
eb66e842 8278 Expr : Node_Id;
8279 -- This is the expression for the result of the function. It is
8280 -- is build by connecting the component predicates with AND THEN.
d9f6a4ee 8281
eb66e842 8282 Expr_M : Node_Id;
8283 -- This is the corresponding return expression for the Predicate_M
8284 -- function. It differs in that raise expressions are marked for
8285 -- special expansion (see Process_REs).
d9f6a4ee 8286
9c20237a 8287 Object_Name : Name_Id;
eb66e842 8288 -- Name for argument of Predicate procedure. Note that we use the same
499918a7 8289 -- name for both predicate functions. That way the reference within the
eb66e842 8290 -- predicate expression is the same in both functions.
d9f6a4ee 8291
9c20237a 8292 Object_Entity : Entity_Id;
eb66e842 8293 -- Entity for argument of Predicate procedure
d9f6a4ee 8294
9c20237a 8295 Object_Entity_M : Entity_Id;
8296 -- Entity for argument of separate Predicate procedure when exceptions
8297 -- are present in expression.
8298
02e5d0d0 8299 FDecl : Node_Id;
8300 -- The function declaration
9c20237a 8301
02e5d0d0 8302 SId : Entity_Id;
8303 -- Its entity
d9f6a4ee 8304
eb66e842 8305 Raise_Expression_Present : Boolean := False;
8306 -- Set True if Expr has at least one Raise_Expression
d9f6a4ee 8307
75491446 8308 procedure Add_Condition (Cond : Node_Id);
8309 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8310 -- Expr is empty).
d9f6a4ee 8311
eb66e842 8312 procedure Add_Predicates;
8313 -- Appends expressions for any Predicate pragmas in the rep item chain
8314 -- Typ to Expr. Note that we look only at items for this exact entity.
8315 -- Inheritance of predicates for the parent type is done by calling the
8316 -- Predicate_Function of the parent type, using Add_Call above.
d9f6a4ee 8317
75491446 8318 procedure Add_Call (T : Entity_Id);
8319 -- Includes a call to the predicate function for type T in Expr if T
8320 -- has predicates and Predicate_Function (T) is non-empty.
8321
eb66e842 8322 function Process_RE (N : Node_Id) return Traverse_Result;
8323 -- Used in Process REs, tests if node N is a raise expression, and if
8324 -- so, marks it to be converted to return False.
d9f6a4ee 8325
eb66e842 8326 procedure Process_REs is new Traverse_Proc (Process_RE);
8327 -- Marks any raise expressions in Expr_M to return False
d9f6a4ee 8328
f9e26ff7 8329 function Test_RE (N : Node_Id) return Traverse_Result;
8330 -- Used in Test_REs, tests one node for being a raise expression, and if
8331 -- so sets Raise_Expression_Present True.
8332
8333 procedure Test_REs is new Traverse_Proc (Test_RE);
8334 -- Tests to see if Expr contains any raise expressions
8335
eb66e842 8336 --------------
8337 -- Add_Call --
8338 --------------
d9f6a4ee 8339
eb66e842 8340 procedure Add_Call (T : Entity_Id) is
8341 Exp : Node_Id;
d9f6a4ee 8342
eb66e842 8343 begin
8344 if Present (T) and then Present (Predicate_Function (T)) then
8345 Set_Has_Predicates (Typ);
d9f6a4ee 8346
74d7e7f5 8347 -- Build the call to the predicate function of T. The type may be
8348 -- derived, so use an unchecked conversion for the actual.
d9f6a4ee 8349
eb66e842 8350 Exp :=
8351 Make_Predicate_Call
74d7e7f5 8352 (Typ => T,
8353 Expr =>
8354 Unchecked_Convert_To (T,
8355 Make_Identifier (Loc, Object_Name)));
d9f6a4ee 8356
75491446 8357 -- "and"-in the call to evolving expression
d9f6a4ee 8358
75491446 8359 Add_Condition (Exp);
d9f6a4ee 8360
eb66e842 8361 -- Output info message on inheritance if required. Note we do not
8362 -- give this information for generic actual types, since it is
8363 -- unwelcome noise in that case in instantiations. We also
8364 -- generally suppress the message in instantiations, and also
8365 -- if it involves internal names.
d9f6a4ee 8366
eb66e842 8367 if Opt.List_Inherited_Aspects
8368 and then not Is_Generic_Actual_Type (Typ)
8369 and then Instantiation_Depth (Sloc (Typ)) = 0
8370 and then not Is_Internal_Name (Chars (T))
8371 and then not Is_Internal_Name (Chars (Typ))
8372 then
8373 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8374 Error_Msg_Node_2 := T;
8375 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8376 end if;
8377 end if;
8378 end Add_Call;
d9f6a4ee 8379
75491446 8380 -------------------
8381 -- Add_Condition --
8382 -------------------
8383
8384 procedure Add_Condition (Cond : Node_Id) is
8385 begin
8386 -- This is the first predicate expression
8387
8388 if No (Expr) then
8389 Expr := Cond;
8390
8391 -- Otherwise concatenate to the existing predicate expressions by
8392 -- using "and then".
8393
8394 else
8395 Expr :=
8396 Make_And_Then (Loc,
8397 Left_Opnd => Relocate_Node (Expr),
8398 Right_Opnd => Cond);
8399 end if;
8400 end Add_Condition;
8401
eb66e842 8402 --------------------
8403 -- Add_Predicates --
8404 --------------------
d9f6a4ee 8405
eb66e842 8406 procedure Add_Predicates is
f9e26ff7 8407 procedure Add_Predicate (Prag : Node_Id);
8408 -- Concatenate the expression of predicate pragma Prag to Expr by
8409 -- using a short circuit "and then" operator.
d9f6a4ee 8410
f9e26ff7 8411 -------------------
8412 -- Add_Predicate --
8413 -------------------
d9f6a4ee 8414
f9e26ff7 8415 procedure Add_Predicate (Prag : Node_Id) is
8416 procedure Replace_Type_Reference (N : Node_Id);
8417 -- Replace a single occurrence N of the subtype name with a
8418 -- reference to the formal of the predicate function. N can be an
8419 -- identifier referencing the subtype, or a selected component,
8420 -- representing an appropriately qualified occurrence of the
8421 -- subtype name.
8422
8423 procedure Replace_Type_References is
8424 new Replace_Type_References_Generic (Replace_Type_Reference);
8425 -- Traverse an expression changing every occurrence of an
8426 -- identifier whose name matches the name of the subtype with a
8427 -- reference to the formal parameter of the predicate function.
8428
8429 ----------------------------
8430 -- Replace_Type_Reference --
8431 ----------------------------
8432
8433 procedure Replace_Type_Reference (N : Node_Id) is
8434 begin
8435 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8436 -- Use the Sloc of the usage name, not the defining name
d9f6a4ee 8437
f9e26ff7 8438 Set_Etype (N, Typ);
8439 Set_Entity (N, Object_Entity);
d97beb2f 8440
f9e26ff7 8441 -- We want to treat the node as if it comes from source, so
8442 -- that ASIS will not ignore it.
d97beb2f 8443
f9e26ff7 8444 Set_Comes_From_Source (N, True);
8445 end Replace_Type_Reference;
d97beb2f 8446
f9e26ff7 8447 -- Local variables
d97beb2f 8448
f9e26ff7 8449 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8450 Arg1 : Node_Id;
8451 Arg2 : Node_Id;
d97beb2f 8452
f9e26ff7 8453 -- Start of processing for Add_Predicate
24c8d764 8454
f9e26ff7 8455 begin
42fb9d35 8456 -- Mark corresponding SCO as enabled
8457
8458 Set_SCO_Pragma_Enabled (Sloc (Prag));
8459
f9e26ff7 8460 -- Extract the arguments of the pragma. The expression itself
8461 -- is copied for use in the predicate function, to preserve the
8462 -- original version for ASIS use.
d97beb2f 8463
f9e26ff7 8464 Arg1 := First (Pragma_Argument_Associations (Prag));
8465 Arg2 := Next (Arg1);
d97beb2f 8466
f9e26ff7 8467 Arg1 := Get_Pragma_Arg (Arg1);
8468 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
d97beb2f 8469
f9e26ff7 8470 -- When the predicate pragma applies to the current type or its
8471 -- full view, replace all occurrences of the subtype name with
8472 -- references to the formal parameter of the predicate function.
639c3741 8473
f9e26ff7 8474 if Entity (Arg1) = Typ
8475 or else Full_View (Entity (Arg1)) = Typ
8476 then
8477 Replace_Type_References (Arg2, Typ);
639c3741 8478
f9e26ff7 8479 -- If the predicate pragma comes from an aspect, replace the
8480 -- saved expression because we need the subtype references
8481 -- replaced for the calls to Preanalyze_Spec_Expression in
8482 -- Check_Aspect_At_xxx routines.
639c3741 8483
f9e26ff7 8484 if Present (Asp) then
f9e26ff7 8485 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8486 end if;
24c8d764 8487
75491446 8488 -- "and"-in the Arg2 condition to evolving expression
639c3741 8489
75491446 8490 Add_Condition (Relocate_Node (Arg2));
f9e26ff7 8491 end if;
8492 end Add_Predicate;
737e8460 8493
f9e26ff7 8494 -- Local variables
737e8460 8495
f9e26ff7 8496 Ritem : Node_Id;
d97beb2f 8497
f9e26ff7 8498 -- Start of processing for Add_Predicates
d97beb2f 8499
f9e26ff7 8500 begin
8501 Ritem := First_Rep_Item (Typ);
74d7e7f5 8502
8503 -- If the type is private, check whether full view has inherited
8504 -- predicates.
8505
8506 if Is_Private_Type (Typ) and then No (Ritem) then
8507 Ritem := First_Rep_Item (Full_View (Typ));
8508 end if;
8509
f9e26ff7 8510 while Present (Ritem) loop
8511 if Nkind (Ritem) = N_Pragma
ddccc924 8512 and then Pragma_Name (Ritem) = Name_Predicate
f9e26ff7 8513 then
8514 Add_Predicate (Ritem);
0ea02224 8515
8516 -- If the type is declared in an inner package it may be frozen
8517 -- outside of the package, and the generated pragma has not been
8518 -- analyzed yet, so capture the expression for the predicate
8519 -- function at this point.
8520
8521 elsif Nkind (Ritem) = N_Aspect_Specification
238921ae 8522 and then Present (Aspect_Rep_Item (Ritem))
8523 and then Scope (Typ) /= Current_Scope
0ea02224 8524 then
8525 declare
8526 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8527
8528 begin
8529 if Nkind (Prag) = N_Pragma
ddccc924 8530 and then Pragma_Name (Prag) = Name_Predicate
0ea02224 8531 then
8532 Add_Predicate (Prag);
8533 end if;
8534 end;
eb66e842 8535 end if;
d97beb2f 8536
eb66e842 8537 Next_Rep_Item (Ritem);
8538 end loop;
8539 end Add_Predicates;
d97beb2f 8540
eb66e842 8541 ----------------
8542 -- Process_RE --
8543 ----------------
d97beb2f 8544
eb66e842 8545 function Process_RE (N : Node_Id) return Traverse_Result is
d9f6a4ee 8546 begin
eb66e842 8547 if Nkind (N) = N_Raise_Expression then
8548 Set_Convert_To_Return_False (N);
8549 return Skip;
d9f6a4ee 8550 else
eb66e842 8551 return OK;
d9f6a4ee 8552 end if;
eb66e842 8553 end Process_RE;
d7c2851f 8554
d9f6a4ee 8555 -------------
eb66e842 8556 -- Test_RE --
d9f6a4ee 8557 -------------
d7c2851f 8558
eb66e842 8559 function Test_RE (N : Node_Id) return Traverse_Result is
d97beb2f 8560 begin
eb66e842 8561 if Nkind (N) = N_Raise_Expression then
8562 Raise_Expression_Present := True;
8563 return Abandon;
8564 else
8565 return OK;
8566 end if;
8567 end Test_RE;
d97beb2f 8568
f9e26ff7 8569 -- Local variables
8570
e02e4129 8571 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
8572 -- Save the Ghost mode to restore on exit
f9e26ff7 8573
eb66e842 8574 -- Start of processing for Build_Predicate_Functions
d97beb2f 8575
eb66e842 8576 begin
8577 -- Return if already built or if type does not have predicates
9dc88aea 8578
9c20237a 8579 SId := Predicate_Function (Typ);
eb66e842 8580 if not Has_Predicates (Typ)
9c20237a 8581 or else (Present (SId) and then Has_Completion (SId))
eb66e842 8582 then
8583 return;
8584 end if;
d9f6a4ee 8585
30f8d103 8586 -- The related type may be subject to pragma Ghost. Set the mode now to
8587 -- ensure that the predicate functions are properly marked as Ghost.
f9e26ff7 8588
e02e4129 8589 Set_Ghost_Mode (Typ);
f9e26ff7 8590
eb66e842 8591 -- Prepare to construct predicate expression
d97beb2f 8592
eb66e842 8593 Expr := Empty;
d97beb2f 8594
9c20237a 8595 if Present (SId) then
8596 FDecl := Unit_Declaration_Node (SId);
8597
8598 else
8599 FDecl := Build_Predicate_Function_Declaration (Typ);
8600 SId := Defining_Entity (FDecl);
8601 end if;
8602
8603 -- Recover name of formal parameter of function that replaces references
8604 -- to the type in predicate expressions.
8605
8606 Object_Entity :=
8607 Defining_Identifier
8608 (First (Parameter_Specifications (Specification (FDecl))));
8609
8610 Object_Name := Chars (Object_Entity);
8611 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8612
75491446 8613 -- Add predicates for ancestor if present. These must come before the
8614 -- ones for the current type, as required by AI12-0071-1.
d97beb2f 8615
eb66e842 8616 declare
74d7e7f5 8617 Atyp : Entity_Id;
d9f6a4ee 8618 begin
74d7e7f5 8619 Atyp := Nearest_Ancestor (Typ);
8620
8621 -- The type may be private but the full view may inherit predicates
8622
8623 if No (Atyp) and then Is_Private_Type (Typ) then
8624 Atyp := Nearest_Ancestor (Full_View (Typ));
8625 end if;
8626
eb66e842 8627 if Present (Atyp) then
8628 Add_Call (Atyp);
8629 end if;
8630 end;
02e5d0d0 8631
75491446 8632 -- Add Predicates for the current type
8633
8634 Add_Predicates;
8635
eb66e842 8636 -- Case where predicates are present
9dc88aea 8637
eb66e842 8638 if Present (Expr) then
726fd56a 8639
eb66e842 8640 -- Test for raise expression present
726fd56a 8641
eb66e842 8642 Test_REs (Expr);
9dc88aea 8643
eb66e842 8644 -- If raise expression is present, capture a copy of Expr for use
8645 -- in building the predicateM function version later on. For this
8646 -- copy we replace references to Object_Entity by Object_Entity_M.
9dc88aea 8647
eb66e842 8648 if Raise_Expression_Present then
8649 declare
299b347e 8650 Map : constant Elist_Id := New_Elmt_List;
8651 New_V : Entity_Id := Empty;
8652
8653 -- The unanalyzed expression will be copied and appear in
8654 -- both functions. Normally expressions do not declare new
8655 -- entities, but quantified expressions do, so we need to
8656 -- create new entities for their bound variables, to prevent
8657 -- multiple definitions in gigi.
8658
8659 function Reset_Loop_Variable (N : Node_Id)
8660 return Traverse_Result;
8661
8662 procedure Collect_Loop_Variables is
8663 new Traverse_Proc (Reset_Loop_Variable);
8664
8665 ------------------------
8666 -- Reset_Loop_Variable --
8667 ------------------------
8668
8669 function Reset_Loop_Variable (N : Node_Id)
8670 return Traverse_Result
8671 is
8672 begin
8673 if Nkind (N) = N_Iterator_Specification then
8674 New_V := Make_Defining_Identifier
8675 (Sloc (N), Chars (Defining_Identifier (N)));
8676
8677 Set_Defining_Identifier (N, New_V);
8678 end if;
8679
8680 return OK;
8681 end Reset_Loop_Variable;
8682
eb66e842 8683 begin
8684 Append_Elmt (Object_Entity, Map);
8685 Append_Elmt (Object_Entity_M, Map);
8686 Expr_M := New_Copy_Tree (Expr, Map => Map);
299b347e 8687 Collect_Loop_Variables (Expr_M);
eb66e842 8688 end;
8689 end if;
d97beb2f 8690
eb66e842 8691 -- Build the main predicate function
9dc88aea 8692
eb66e842 8693 declare
eb66e842 8694 SIdB : constant Entity_Id :=
8695 Make_Defining_Identifier (Loc,
8696 Chars => New_External_Name (Chars (Typ), "Predicate"));
8697 -- The entity for the function body
9dc88aea 8698
eb66e842 8699 Spec : Node_Id;
eb66e842 8700 FBody : Node_Id;
9dc88aea 8701
eb66e842 8702 begin
eb66e842 8703 -- The predicate function is shared between views of a type
d97beb2f 8704
eb66e842 8705 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8706 Set_Predicate_Function (Full_View (Typ), SId);
d97beb2f 8707 end if;
d97beb2f 8708
eb66e842 8709 -- Build function body
d97beb2f 8710
eb66e842 8711 Spec :=
8712 Make_Function_Specification (Loc,
8713 Defining_Unit_Name => SIdB,
8714 Parameter_Specifications => New_List (
8715 Make_Parameter_Specification (Loc,
8716 Defining_Identifier =>
8717 Make_Defining_Identifier (Loc, Object_Name),
8718 Parameter_Type =>
8719 New_Occurrence_Of (Typ, Loc))),
8720 Result_Definition =>
8721 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8722
eb66e842 8723 FBody :=
8724 Make_Subprogram_Body (Loc,
8725 Specification => Spec,
8726 Declarations => Empty_List,
8727 Handled_Statement_Sequence =>
8728 Make_Handled_Sequence_Of_Statements (Loc,
8729 Statements => New_List (
8730 Make_Simple_Return_Statement (Loc,
8731 Expression => Expr))));
9dc88aea 8732
9c20237a 8733 -- If declaration has not been analyzed yet, Insert declaration
f021ee0f 8734 -- before freeze node. Insert body itself after freeze node.
9c20237a 8735
8736 if not Analyzed (FDecl) then
8737 Insert_Before_And_Analyze (N, FDecl);
8738 end if;
d97beb2f 8739
02e5d0d0 8740 Insert_After_And_Analyze (N, FBody);
6958c62c 8741
8742 -- Static predicate functions are always side-effect free, and
8743 -- in most cases dynamic predicate functions are as well. Mark
8744 -- them as such whenever possible, so redundant predicate checks
7dd0b9b3 8745 -- can be optimized. If there is a variable reference within the
8746 -- expression, the function is not pure.
b2e821de 8747
6958c62c 8748 if Expander_Active then
7dd0b9b3 8749 Set_Is_Pure (SId,
8750 Side_Effect_Free (Expr, Variable_Ref => True));
6958c62c 8751 Set_Is_Inlined (SId);
8752 end if;
d9f6a4ee 8753 end;
d97beb2f 8754
eb66e842 8755 -- Test for raise expressions present and if so build M version
d97beb2f 8756
eb66e842 8757 if Raise_Expression_Present then
8758 declare
8759 SId : constant Entity_Id :=
8760 Make_Defining_Identifier (Loc,
8761 Chars => New_External_Name (Chars (Typ), "PredicateM"));
c96806b2 8762 -- The entity for the function spec
d97beb2f 8763
eb66e842 8764 SIdB : constant Entity_Id :=
8765 Make_Defining_Identifier (Loc,
8766 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8767 -- The entity for the function body
b9e61b2a 8768
eb66e842 8769 Spec : Node_Id;
eb66e842 8770 FBody : Node_Id;
9c20237a 8771 FDecl : Node_Id;
eb66e842 8772 BTemp : Entity_Id;
d97beb2f 8773
eb66e842 8774 begin
8775 -- Mark any raise expressions for special expansion
d97beb2f 8776
eb66e842 8777 Process_REs (Expr_M);
d97beb2f 8778
eb66e842 8779 -- Build function declaration
d97beb2f 8780
eb66e842 8781 Set_Ekind (SId, E_Function);
8782 Set_Is_Predicate_Function_M (SId);
8783 Set_Predicate_Function_M (Typ, SId);
d97beb2f 8784
eb66e842 8785 -- The predicate function is shared between views of a type
d97beb2f 8786
eb66e842 8787 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8788 Set_Predicate_Function_M (Full_View (Typ), SId);
8789 end if;
9dc88aea 8790
eb66e842 8791 Spec :=
8792 Make_Function_Specification (Loc,
8793 Defining_Unit_Name => SId,
8794 Parameter_Specifications => New_List (
8795 Make_Parameter_Specification (Loc,
8796 Defining_Identifier => Object_Entity_M,
8797 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8798 Result_Definition =>
8799 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8800
eb66e842 8801 FDecl :=
8802 Make_Subprogram_Declaration (Loc,
8803 Specification => Spec);
9dc88aea 8804
eb66e842 8805 -- Build function body
9dc88aea 8806
eb66e842 8807 Spec :=
8808 Make_Function_Specification (Loc,
8809 Defining_Unit_Name => SIdB,
8810 Parameter_Specifications => New_List (
8811 Make_Parameter_Specification (Loc,
8812 Defining_Identifier =>
8813 Make_Defining_Identifier (Loc, Object_Name),
8814 Parameter_Type =>
8815 New_Occurrence_Of (Typ, Loc))),
8816 Result_Definition =>
8817 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8818
eb66e842 8819 -- Build the body, we declare the boolean expression before
8820 -- doing the return, because we are not really confident of
8821 -- what happens if a return appears within a return.
9dc88aea 8822
eb66e842 8823 BTemp :=
8824 Make_Defining_Identifier (Loc,
8825 Chars => New_Internal_Name ('B'));
9dc88aea 8826
eb66e842 8827 FBody :=
8828 Make_Subprogram_Body (Loc,
8829 Specification => Spec,
9dc88aea 8830
eb66e842 8831 Declarations => New_List (
8832 Make_Object_Declaration (Loc,
8833 Defining_Identifier => BTemp,
8834 Constant_Present => True,
8835 Object_Definition =>
8836 New_Occurrence_Of (Standard_Boolean, Loc),
8837 Expression => Expr_M)),
d97beb2f 8838
eb66e842 8839 Handled_Statement_Sequence =>
8840 Make_Handled_Sequence_Of_Statements (Loc,
8841 Statements => New_List (
8842 Make_Simple_Return_Statement (Loc,
8843 Expression => New_Occurrence_Of (BTemp, Loc)))));
d97beb2f 8844
eb66e842 8845 -- Insert declaration before freeze node and body after
d97beb2f 8846
eb66e842 8847 Insert_Before_And_Analyze (N, FDecl);
8848 Insert_After_And_Analyze (N, FBody);
8849 end;
8850 end if;
9dc88aea 8851
3b23aaa0 8852 -- See if we have a static predicate. Note that the answer may be
8853 -- yes even if we have an explicit Dynamic_Predicate present.
9dc88aea 8854
3b23aaa0 8855 declare
94d896aa 8856 PS : Boolean;
3b23aaa0 8857 EN : Node_Id;
9dc88aea 8858
3b23aaa0 8859 begin
94d896aa 8860 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8861 PS := False;
8862 else
8863 PS := Is_Predicate_Static (Expr, Object_Name);
8864 end if;
8865
a360a0f7 8866 -- Case where we have a predicate-static aspect
9dc88aea 8867
3b23aaa0 8868 if PS then
9dc88aea 8869
3b23aaa0 8870 -- We don't set Has_Static_Predicate_Aspect, since we can have
8871 -- any of the three cases (Predicate, Dynamic_Predicate, or
8872 -- Static_Predicate) generating a predicate with an expression
a360a0f7 8873 -- that is predicate-static. We just indicate that we have a
3b23aaa0 8874 -- predicate that can be treated as static.
d7c2851f 8875
3b23aaa0 8876 Set_Has_Static_Predicate (Typ);
d7c2851f 8877
3b23aaa0 8878 -- For discrete subtype, build the static predicate list
9dc88aea 8879
3b23aaa0 8880 if Is_Discrete_Type (Typ) then
8881 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8882
8883 -- If we don't get a static predicate list, it means that we
8884 -- have a case where this is not possible, most typically in
8885 -- the case where we inherit a dynamic predicate. We do not
8886 -- consider this an error, we just leave the predicate as
8887 -- dynamic. But if we do succeed in building the list, then
8888 -- we mark the predicate as static.
8889
5c6a5792 8890 if No (Static_Discrete_Predicate (Typ)) then
3b23aaa0 8891 Set_Has_Static_Predicate (Typ, False);
8892 end if;
94d896aa 8893
8894 -- For real or string subtype, save predicate expression
8895
8896 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8897 Set_Static_Real_Or_String_Predicate (Typ, Expr);
3b23aaa0 8898 end if;
8899
8900 -- Case of dynamic predicate (expression is not predicate-static)
9dc88aea 8901
eb66e842 8902 else
3b23aaa0 8903 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8904 -- is only set if we have an explicit Dynamic_Predicate aspect
8905 -- given. Here we may simply have a Predicate aspect where the
8906 -- expression happens not to be predicate-static.
8907
8908 -- Emit an error when the predicate is categorized as static
8909 -- but its expression is not predicate-static.
8910
8911 -- First a little fiddling to get a nice location for the
8912 -- message. If the expression is of the form (A and then B),
75491446 8913 -- where A is an inherited predicate, then use the right
8914 -- operand for the Sloc. This avoids getting confused by a call
8915 -- to an inherited predicate with a less convenient source
8916 -- location.
3b23aaa0 8917
8918 EN := Expr;
75491446 8919 while Nkind (EN) = N_And_Then
8920 and then Nkind (Left_Opnd (EN)) = N_Function_Call
8921 and then Is_Predicate_Function
8922 (Entity (Name (Left_Opnd (EN))))
8923 loop
8924 EN := Right_Opnd (EN);
3b23aaa0 8925 end loop;
8926
8927 -- Now post appropriate message
8928
8929 if Has_Static_Predicate_Aspect (Typ) then
94d896aa 8930 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
3b23aaa0 8931 Error_Msg_F
26279d91 8932 ("expression is not predicate-static (RM 3.2.4(16-22))",
3b23aaa0 8933 EN);
8934 else
94d896aa 8935 Error_Msg_F
8936 ("static predicate requires scalar or string type", EN);
3b23aaa0 8937 end if;
8938 end if;
eb66e842 8939 end if;
3b23aaa0 8940 end;
eb66e842 8941 end if;
f9e26ff7 8942
e02e4129 8943 Restore_Ghost_Mode (Saved_GM);
eb66e842 8944 end Build_Predicate_Functions;
9dc88aea 8945
9c20237a 8946 ------------------------------------------
8947 -- Build_Predicate_Function_Declaration --
8948 ------------------------------------------
8949
1ecdfe4b 8950 -- WARNING: This routine manages Ghost regions. Return statements must be
8951 -- replaced by gotos which jump to the end of the routine and restore the
8952 -- Ghost mode.
8953
9c20237a 8954 function Build_Predicate_Function_Declaration
8955 (Typ : Entity_Id) return Node_Id
8956 is
8957 Loc : constant Source_Ptr := Sloc (Typ);
8958
e02e4129 8959 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
8960 -- Save the Ghost mode to restore on exit
8961
72a98436 8962 Func_Decl : Node_Id;
8963 Func_Id : Entity_Id;
72a98436 8964 Spec : Node_Id;
9c20237a 8965
72a98436 8966 begin
8967 -- The related type may be subject to pragma Ghost. Set the mode now to
8968 -- ensure that the predicate functions are properly marked as Ghost.
9c20237a 8969
e02e4129 8970 Set_Ghost_Mode (Typ);
9c20237a 8971
72a98436 8972 Func_Id :=
8973 Make_Defining_Identifier (Loc,
8974 Chars => New_External_Name (Chars (Typ), "Predicate"));
9c20237a 8975
3db675d2 8976 -- The predicate function requires debug info when the predicates are
8977 -- subject to Source Coverage Obligations.
8978
8979 if Opt.Generate_SCO then
8980 Set_Debug_Info_Needed (Func_Id);
8981 end if;
8982
9c20237a 8983 Spec :=
8984 Make_Function_Specification (Loc,
72a98436 8985 Defining_Unit_Name => Func_Id,
9c20237a 8986 Parameter_Specifications => New_List (
8987 Make_Parameter_Specification (Loc,
72a98436 8988 Defining_Identifier => Make_Temporary (Loc, 'I'),
9c20237a 8989 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8990 Result_Definition =>
8991 New_Occurrence_Of (Standard_Boolean, Loc));
8992
72a98436 8993 Func_Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
9c20237a 8994
72a98436 8995 Set_Ekind (Func_Id, E_Function);
8996 Set_Etype (Func_Id, Standard_Boolean);
8997 Set_Is_Internal (Func_Id);
8998 Set_Is_Predicate_Function (Func_Id);
8999 Set_Predicate_Function (Typ, Func_Id);
9c20237a 9000
72a98436 9001 Insert_After (Parent (Typ), Func_Decl);
9002 Analyze (Func_Decl);
9c20237a 9003
e02e4129 9004 Restore_Ghost_Mode (Saved_GM);
9c20237a 9005
72a98436 9006 return Func_Decl;
9c20237a 9007 end Build_Predicate_Function_Declaration;
9008
d9f6a4ee 9009 -----------------------------------------
9010 -- Check_Aspect_At_End_Of_Declarations --
9011 -----------------------------------------
9dc88aea 9012
d9f6a4ee 9013 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9014 Ent : constant Entity_Id := Entity (ASN);
9015 Ident : constant Node_Id := Identifier (ASN);
9016 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
d7c2851f 9017
d9f6a4ee 9018 End_Decl_Expr : constant Node_Id := Entity (Ident);
9019 -- Expression to be analyzed at end of declarations
d7c2851f 9020
d9f6a4ee 9021 Freeze_Expr : constant Node_Id := Expression (ASN);
6da581c1 9022 -- Expression from call to Check_Aspect_At_Freeze_Point.
d7c2851f 9023
25e4fa47 9024 T : constant Entity_Id := Etype (Original_Node (Freeze_Expr));
6da581c1 9025 -- Type required for preanalyze call. We use the original expression to
9026 -- get the proper type, to prevent cascaded errors when the expression
9027 -- is constant-folded.
d7c2851f 9028
d9f6a4ee 9029 Err : Boolean;
9030 -- Set False if error
9dc88aea 9031
d9f6a4ee 9032 -- On entry to this procedure, Entity (Ident) contains a copy of the
9033 -- original expression from the aspect, saved for this purpose, and
9034 -- but Expression (Ident) is a preanalyzed copy of the expression,
9035 -- preanalyzed just after the freeze point.
9dc88aea 9036
d9f6a4ee 9037 procedure Check_Overloaded_Name;
9038 -- For aspects whose expression is simply a name, this routine checks if
9039 -- the name is overloaded or not. If so, it verifies there is an
9040 -- interpretation that matches the entity obtained at the freeze point,
9041 -- otherwise the compiler complains.
9dc88aea 9042
d9f6a4ee 9043 ---------------------------
9044 -- Check_Overloaded_Name --
9045 ---------------------------
9046
9047 procedure Check_Overloaded_Name is
d97beb2f 9048 begin
d9f6a4ee 9049 if not Is_Overloaded (End_Decl_Expr) then
5ac76cee 9050 Err := not Is_Entity_Name (End_Decl_Expr)
9051 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
d9f6a4ee 9052
d97beb2f 9053 else
d9f6a4ee 9054 Err := True;
9dc88aea 9055
d9f6a4ee 9056 declare
9057 Index : Interp_Index;
9058 It : Interp;
9dc88aea 9059
d9f6a4ee 9060 begin
9061 Get_First_Interp (End_Decl_Expr, Index, It);
9062 while Present (It.Typ) loop
9063 if It.Nam = Entity (Freeze_Expr) then
9064 Err := False;
9065 exit;
9066 end if;
9067
9068 Get_Next_Interp (Index, It);
9069 end loop;
9070 end;
9dc88aea 9071 end if;
d9f6a4ee 9072 end Check_Overloaded_Name;
9dc88aea 9073
d9f6a4ee 9074 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9dc88aea 9075
d9f6a4ee 9076 begin
da3cad01 9077 -- In an instance we do not perform the consistency check between freeze
9078 -- point and end of declarations, because it was done already in the
9079 -- analysis of the generic. Furthermore, the delayed analysis of an
9080 -- aspect of the instance may produce spurious errors when the generic
9081 -- is a child unit that references entities in the parent (which might
9082 -- not be in scope at the freeze point of the instance).
9083
9084 if In_Instance then
9085 return;
9086
1c164d44 9087 -- The enclosing scope may have been rewritten during expansion (.e.g. a
9088 -- task body is rewritten as a procedure) after this conformance check
9089 -- has been performed, so do not perform it again (it may not easily be
9090 -- done if full visibility of local entities is not available).
ce450a94 9091
9092 elsif not Comes_From_Source (Current_Scope) then
9093 return;
9094
d9f6a4ee 9095 -- Case of aspects Dimension, Dimension_System and Synchronization
9dc88aea 9096
da3cad01 9097 elsif A_Id = Aspect_Synchronization then
d9f6a4ee 9098 return;
d97beb2f 9099
d9f6a4ee 9100 -- Case of stream attributes, just have to compare entities. However,
9101 -- the expression is just a name (possibly overloaded), and there may
9102 -- be stream operations declared for unrelated types, so we just need
9103 -- to verify that one of these interpretations is the one available at
9104 -- at the freeze point.
9dc88aea 9105
d9f6a4ee 9106 elsif A_Id = Aspect_Input or else
f02a9a9a 9107 A_Id = Aspect_Output or else
9108 A_Id = Aspect_Read or else
9109 A_Id = Aspect_Write
d9f6a4ee 9110 then
9111 Analyze (End_Decl_Expr);
9112 Check_Overloaded_Name;
9dc88aea 9113
d9f6a4ee 9114 elsif A_Id = Aspect_Variable_Indexing or else
9115 A_Id = Aspect_Constant_Indexing or else
9116 A_Id = Aspect_Default_Iterator or else
9117 A_Id = Aspect_Iterator_Element
9118 then
9119 -- Make type unfrozen before analysis, to prevent spurious errors
9120 -- about late attributes.
9dc88aea 9121
d9f6a4ee 9122 Set_Is_Frozen (Ent, False);
9123 Analyze (End_Decl_Expr);
9124 Set_Is_Frozen (Ent, True);
9dc88aea 9125
d9f6a4ee 9126 -- If the end of declarations comes before any other freeze
9127 -- point, the Freeze_Expr is not analyzed: no check needed.
9dc88aea 9128
d9f6a4ee 9129 if Analyzed (Freeze_Expr) and then not In_Instance then
9130 Check_Overloaded_Name;
9131 else
9132 Err := False;
9133 end if;
55e8372b 9134
d9f6a4ee 9135 -- All other cases
55e8372b 9136
d9f6a4ee 9137 else
c1efebf9 9138 -- Indicate that the expression comes from an aspect specification,
9139 -- which is used in subsequent analysis even if expansion is off.
9140
9141 Set_Parent (End_Decl_Expr, ASN);
9142
d9f6a4ee 9143 -- In a generic context the aspect expressions have not been
9144 -- preanalyzed, so do it now. There are no conformance checks
9145 -- to perform in this case.
55e8372b 9146
d9f6a4ee 9147 if No (T) then
9148 Check_Aspect_At_Freeze_Point (ASN);
9149 return;
55e8372b 9150
d9f6a4ee 9151 -- The default values attributes may be defined in the private part,
9152 -- and the analysis of the expression may take place when only the
9153 -- partial view is visible. The expression must be scalar, so use
9154 -- the full view to resolve.
55e8372b 9155
d9f6a4ee 9156 elsif (A_Id = Aspect_Default_Value
9157 or else
9158 A_Id = Aspect_Default_Component_Value)
9159 and then Is_Private_Type (T)
9160 then
9161 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
c1efebf9 9162
d9f6a4ee 9163 else
9164 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9165 end if;
d97beb2f 9166
d9f6a4ee 9167 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9168 end if;
55e8372b 9169
c1efebf9 9170 -- Output error message if error. Force error on aspect specification
9171 -- even if there is an error on the expression itself.
55e8372b 9172
d9f6a4ee 9173 if Err then
9174 Error_Msg_NE
c1efebf9 9175 ("!visibility of aspect for& changes after freeze point",
d9f6a4ee 9176 ASN, Ent);
9177 Error_Msg_NE
9178 ("info: & is frozen here, aspects evaluated at this point??",
9179 Freeze_Node (Ent), Ent);
9180 end if;
9181 end Check_Aspect_At_End_Of_Declarations;
55e8372b 9182
d9f6a4ee 9183 ----------------------------------
9184 -- Check_Aspect_At_Freeze_Point --
9185 ----------------------------------
9dc88aea 9186
d9f6a4ee 9187 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9188 Ident : constant Node_Id := Identifier (ASN);
9189 -- Identifier (use Entity field to save expression)
9dc88aea 9190
d9f6a4ee 9191 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9dc88aea 9192
d9f6a4ee 9193 T : Entity_Id := Empty;
9194 -- Type required for preanalyze call
9dc88aea 9195
d9f6a4ee 9196 begin
9197 -- On entry to this procedure, Entity (Ident) contains a copy of the
9198 -- original expression from the aspect, saved for this purpose.
9dc88aea 9199
d9f6a4ee 9200 -- On exit from this procedure Entity (Ident) is unchanged, still
9201 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9202 -- of the expression, preanalyzed just after the freeze point.
d97beb2f 9203
d9f6a4ee 9204 -- Make a copy of the expression to be preanalyzed
d97beb2f 9205
d9f6a4ee 9206 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
d97beb2f 9207
d9f6a4ee 9208 -- Find type for preanalyze call
d97beb2f 9209
d9f6a4ee 9210 case A_Id is
9dc88aea 9211
d9f6a4ee 9212 -- No_Aspect should be impossible
d97beb2f 9213
d9f6a4ee 9214 when No_Aspect =>
9215 raise Program_Error;
9216
9217 -- Aspects taking an optional boolean argument
d97beb2f 9218
99378362 9219 when Boolean_Aspects
9220 | Library_Unit_Aspects
9221 =>
d9f6a4ee 9222 T := Standard_Boolean;
d7c2851f 9223
d9f6a4ee 9224 -- Aspects corresponding to attribute definition clauses
9dc88aea 9225
d9f6a4ee 9226 when Aspect_Address =>
9227 T := RTE (RE_Address);
9dc88aea 9228
d9f6a4ee 9229 when Aspect_Attach_Handler =>
9230 T := RTE (RE_Interrupt_ID);
d7c2851f 9231
99378362 9232 when Aspect_Bit_Order
9233 | Aspect_Scalar_Storage_Order
9234 =>
d9f6a4ee 9235 T := RTE (RE_Bit_Order);
d7c2851f 9236
d9f6a4ee 9237 when Aspect_Convention =>
9238 return;
d7c2851f 9239
d9f6a4ee 9240 when Aspect_CPU =>
9241 T := RTE (RE_CPU_Range);
d7c2851f 9242
d9f6a4ee 9243 -- Default_Component_Value is resolved with the component type
d7c2851f 9244
d9f6a4ee 9245 when Aspect_Default_Component_Value =>
9246 T := Component_Type (Entity (ASN));
d7c2851f 9247
647fab54 9248 when Aspect_Default_Storage_Pool =>
9249 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9250
d9f6a4ee 9251 -- Default_Value is resolved with the type entity in question
d7c2851f 9252
d9f6a4ee 9253 when Aspect_Default_Value =>
9254 T := Entity (ASN);
9dc88aea 9255
d9f6a4ee 9256 when Aspect_Dispatching_Domain =>
9257 T := RTE (RE_Dispatching_Domain);
9dc88aea 9258
d9f6a4ee 9259 when Aspect_External_Tag =>
9260 T := Standard_String;
9dc88aea 9261
d9f6a4ee 9262 when Aspect_External_Name =>
9263 T := Standard_String;
9dc88aea 9264
d9f6a4ee 9265 when Aspect_Link_Name =>
9266 T := Standard_String;
9dc88aea 9267
99378362 9268 when Aspect_Interrupt_Priority
9269 | Aspect_Priority
9270 =>
d9f6a4ee 9271 T := Standard_Integer;
d97beb2f 9272
d9f6a4ee 9273 when Aspect_Relative_Deadline =>
9274 T := RTE (RE_Time_Span);
d97beb2f 9275
e6ce0468 9276 when Aspect_Secondary_Stack_Size =>
9277 T := Standard_Integer;
9278
d9f6a4ee 9279 when Aspect_Small =>
9280 T := Universal_Real;
490beba6 9281
d9f6a4ee 9282 -- For a simple storage pool, we have to retrieve the type of the
9283 -- pool object associated with the aspect's corresponding attribute
9284 -- definition clause.
490beba6 9285
d9f6a4ee 9286 when Aspect_Simple_Storage_Pool =>
9287 T := Etype (Expression (Aspect_Rep_Item (ASN)));
d97beb2f 9288
d9f6a4ee 9289 when Aspect_Storage_Pool =>
9290 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
d97beb2f 9291
99378362 9292 when Aspect_Alignment
9293 | Aspect_Component_Size
9294 | Aspect_Machine_Radix
9295 | Aspect_Object_Size
9296 | Aspect_Size
9297 | Aspect_Storage_Size
9298 | Aspect_Stream_Size
9299 | Aspect_Value_Size
9300 =>
d9f6a4ee 9301 T := Any_Integer;
9dc88aea 9302
04ae062f 9303 when Aspect_Linker_Section =>
9304 T := Standard_String;
9305
d9f6a4ee 9306 when Aspect_Synchronization =>
9307 return;
7d20685d 9308
d9f6a4ee 9309 -- Special case, the expression of these aspects is just an entity
9310 -- that does not need any resolution, so just analyze.
7d20685d 9311
99378362 9312 when Aspect_Input
9313 | Aspect_Output
9314 | Aspect_Read
9315 | Aspect_Suppress
9316 | Aspect_Unsuppress
9317 | Aspect_Warnings
9318 | Aspect_Write
9319 =>
d9f6a4ee 9320 Analyze (Expression (ASN));
9321 return;
7d20685d 9322
d9f6a4ee 9323 -- Same for Iterator aspects, where the expression is a function
9324 -- name. Legality rules are checked separately.
89f1e35c 9325
99378362 9326 when Aspect_Constant_Indexing
9327 | Aspect_Default_Iterator
9328 | Aspect_Iterator_Element
9329 | Aspect_Variable_Indexing
9330 =>
d9f6a4ee 9331 Analyze (Expression (ASN));
9332 return;
7d20685d 9333
b3f8228a 9334 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9335
9336 when Aspect_Iterable =>
3061ffde 9337 T := Entity (ASN);
9338
b3f8228a 9339 declare
a9f5fea7 9340 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
3061ffde 9341 Assoc : Node_Id;
9342 Expr : Node_Id;
a9f5fea7 9343
b3f8228a 9344 begin
a9f5fea7 9345 if Cursor = Any_Type then
9346 return;
9347 end if;
9348
b3f8228a 9349 Assoc := First (Component_Associations (Expression (ASN)));
9350 while Present (Assoc) loop
3061ffde 9351 Expr := Expression (Assoc);
9352 Analyze (Expr);
a9f5fea7 9353
9354 if not Error_Posted (Expr) then
9355 Resolve_Iterable_Operation
9356 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9357 end if;
9358
b3f8228a 9359 Next (Assoc);
9360 end loop;
9361 end;
3061ffde 9362
b3f8228a 9363 return;
9364
d9f6a4ee 9365 -- Invariant/Predicate take boolean expressions
7d20685d 9366
99378362 9367 when Aspect_Dynamic_Predicate
9368 | Aspect_Invariant
9369 | Aspect_Predicate
9370 | Aspect_Static_Predicate
9371 | Aspect_Type_Invariant
9372 =>
d9f6a4ee 9373 T := Standard_Boolean;
7d20685d 9374
fdec445e 9375 when Aspect_Predicate_Failure =>
9376 T := Standard_String;
9377
d9f6a4ee 9378 -- Here is the list of aspects that don't require delay analysis
89f1e35c 9379
99378362 9380 when Aspect_Abstract_State
9381 | Aspect_Annotate
9382 | Aspect_Async_Readers
9383 | Aspect_Async_Writers
9384 | Aspect_Constant_After_Elaboration
9385 | Aspect_Contract_Cases
9386 | Aspect_Default_Initial_Condition
9387 | Aspect_Depends
9388 | Aspect_Dimension
9389 | Aspect_Dimension_System
9390 | Aspect_Effective_Reads
9391 | Aspect_Effective_Writes
9392 | Aspect_Extensions_Visible
9393 | Aspect_Ghost
9394 | Aspect_Global
9395 | Aspect_Implicit_Dereference
9396 | Aspect_Initial_Condition
9397 | Aspect_Initializes
9398 | Aspect_Max_Queue_Length
9399 | Aspect_Obsolescent
9400 | Aspect_Part_Of
9401 | Aspect_Post
9402 | Aspect_Postcondition
9403 | Aspect_Pre
9404 | Aspect_Precondition
9405 | Aspect_Refined_Depends
9406 | Aspect_Refined_Global
9407 | Aspect_Refined_Post
9408 | Aspect_Refined_State
9409 | Aspect_SPARK_Mode
9410 | Aspect_Test_Case
9411 | Aspect_Unimplemented
9412 | Aspect_Volatile_Function
9413 =>
d9f6a4ee 9414 raise Program_Error;
2b184b2f 9415
d9f6a4ee 9416 end case;
2b184b2f 9417
d9f6a4ee 9418 -- Do the preanalyze call
2b184b2f 9419
d9f6a4ee 9420 Preanalyze_Spec_Expression (Expression (ASN), T);
9421 end Check_Aspect_At_Freeze_Point;
2b184b2f 9422
d9f6a4ee 9423 -----------------------------------
9424 -- Check_Constant_Address_Clause --
9425 -----------------------------------
2b184b2f 9426
d9f6a4ee 9427 procedure Check_Constant_Address_Clause
9428 (Expr : Node_Id;
9429 U_Ent : Entity_Id)
9430 is
9431 procedure Check_At_Constant_Address (Nod : Node_Id);
9432 -- Checks that the given node N represents a name whose 'Address is
9433 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9434 -- address value is the same at the point of declaration of U_Ent and at
9435 -- the time of elaboration of the address clause.
84ed7523 9436
d9f6a4ee 9437 procedure Check_Expr_Constants (Nod : Node_Id);
9438 -- Checks that Nod meets the requirements for a constant address clause
9439 -- in the sense of the enclosing procedure.
84ed7523 9440
d9f6a4ee 9441 procedure Check_List_Constants (Lst : List_Id);
9442 -- Check that all elements of list Lst meet the requirements for a
9443 -- constant address clause in the sense of the enclosing procedure.
84ed7523 9444
d9f6a4ee 9445 -------------------------------
9446 -- Check_At_Constant_Address --
9447 -------------------------------
84ed7523 9448
d9f6a4ee 9449 procedure Check_At_Constant_Address (Nod : Node_Id) is
9450 begin
9451 if Is_Entity_Name (Nod) then
9452 if Present (Address_Clause (Entity ((Nod)))) then
9453 Error_Msg_NE
9454 ("invalid address clause for initialized object &!",
d9f6a4ee 9455 Nod, U_Ent);
99378362 9456 Error_Msg_NE
9457 ("address for& cannot depend on another address clause! "
9458 & "(RM 13.1(22))!", Nod, U_Ent);
84ed7523 9459
d9f6a4ee 9460 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9461 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9462 then
9463 Error_Msg_NE
9464 ("invalid address clause for initialized object &!",
9465 Nod, U_Ent);
9466 Error_Msg_Node_2 := U_Ent;
9467 Error_Msg_NE
9468 ("\& must be defined before & (RM 13.1(22))!",
9469 Nod, Entity (Nod));
9470 end if;
7d20685d 9471
d9f6a4ee 9472 elsif Nkind (Nod) = N_Selected_Component then
9473 declare
9474 T : constant Entity_Id := Etype (Prefix (Nod));
59f3e675 9475
d9f6a4ee 9476 begin
9477 if (Is_Record_Type (T)
9478 and then Has_Discriminants (T))
9479 or else
9480 (Is_Access_Type (T)
f02a9a9a 9481 and then Is_Record_Type (Designated_Type (T))
9482 and then Has_Discriminants (Designated_Type (T)))
d9f6a4ee 9483 then
9484 Error_Msg_NE
9485 ("invalid address clause for initialized object &!",
9486 Nod, U_Ent);
9487 Error_Msg_N
99378362 9488 ("\address cannot depend on component of discriminated "
9489 & "record (RM 13.1(22))!", Nod);
d9f6a4ee 9490 else
9491 Check_At_Constant_Address (Prefix (Nod));
9492 end if;
9493 end;
89cc7147 9494
d9f6a4ee 9495 elsif Nkind (Nod) = N_Indexed_Component then
9496 Check_At_Constant_Address (Prefix (Nod));
9497 Check_List_Constants (Expressions (Nod));
89cc7147 9498
84ed7523 9499 else
d9f6a4ee 9500 Check_Expr_Constants (Nod);
84ed7523 9501 end if;
d9f6a4ee 9502 end Check_At_Constant_Address;
81b424ac 9503
d9f6a4ee 9504 --------------------------
9505 -- Check_Expr_Constants --
9506 --------------------------
7b9b2f05 9507
d9f6a4ee 9508 procedure Check_Expr_Constants (Nod : Node_Id) is
9509 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9510 Ent : Entity_Id := Empty;
7b9b2f05 9511
d9f6a4ee 9512 begin
9513 if Nkind (Nod) in N_Has_Etype
9514 and then Etype (Nod) = Any_Type
7b9b2f05 9515 then
d9f6a4ee 9516 return;
309c3053 9517 end if;
9518
d9f6a4ee 9519 case Nkind (Nod) is
99378362 9520 when N_Empty
9521 | N_Error
9522 =>
d9f6a4ee 9523 return;
7d20685d 9524
99378362 9525 when N_Expanded_Name
9526 | N_Identifier
9527 =>
d9f6a4ee 9528 Ent := Entity (Nod);
7d20685d 9529
d9f6a4ee 9530 -- We need to look at the original node if it is different
9531 -- from the node, since we may have rewritten things and
9532 -- substituted an identifier representing the rewrite.
7d20685d 9533
d9f6a4ee 9534 if Original_Node (Nod) /= Nod then
9535 Check_Expr_Constants (Original_Node (Nod));
7d20685d 9536
d9f6a4ee 9537 -- If the node is an object declaration without initial
9538 -- value, some code has been expanded, and the expression
9539 -- is not constant, even if the constituents might be
9540 -- acceptable, as in A'Address + offset.
7d20685d 9541
d9f6a4ee 9542 if Ekind (Ent) = E_Variable
9543 and then
9544 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9545 and then
9546 No (Expression (Declaration_Node (Ent)))
9547 then
9548 Error_Msg_NE
9549 ("invalid address clause for initialized object &!",
9550 Nod, U_Ent);
89f1e35c 9551
d9f6a4ee 9552 -- If entity is constant, it may be the result of expanding
9553 -- a check. We must verify that its declaration appears
9554 -- before the object in question, else we also reject the
9555 -- address clause.
7d20685d 9556
d9f6a4ee 9557 elsif Ekind (Ent) = E_Constant
9558 and then In_Same_Source_Unit (Ent, U_Ent)
9559 and then Sloc (Ent) > Loc_U_Ent
9560 then
9561 Error_Msg_NE
9562 ("invalid address clause for initialized object &!",
9563 Nod, U_Ent);
9564 end if;
7d20685d 9565
d9f6a4ee 9566 return;
9567 end if;
7d20685d 9568
d9f6a4ee 9569 -- Otherwise look at the identifier and see if it is OK
7d20685d 9570
d9f6a4ee 9571 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9572 or else Is_Type (Ent)
9573 then
9574 return;
7d20685d 9575
f02a9a9a 9576 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9577
d9f6a4ee 9578 -- This is the case where we must have Ent defined before
9579 -- U_Ent. Clearly if they are in different units this
9580 -- requirement is met since the unit containing Ent is
9581 -- already processed.
7d20685d 9582
d9f6a4ee 9583 if not In_Same_Source_Unit (Ent, U_Ent) then
9584 return;
7d20685d 9585
d9f6a4ee 9586 -- Otherwise location of Ent must be before the location
9587 -- of U_Ent, that's what prior defined means.
7d20685d 9588
d9f6a4ee 9589 elsif Sloc (Ent) < Loc_U_Ent then
9590 return;
6c545057 9591
d9f6a4ee 9592 else
9593 Error_Msg_NE
9594 ("invalid address clause for initialized object &!",
9595 Nod, U_Ent);
9596 Error_Msg_Node_2 := U_Ent;
9597 Error_Msg_NE
9598 ("\& must be defined before & (RM 13.1(22))!",
9599 Nod, Ent);
9600 end if;
37c6e44c 9601
d9f6a4ee 9602 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9603 Check_Expr_Constants (Original_Node (Nod));
6c545057 9604
d9f6a4ee 9605 else
9606 Error_Msg_NE
9607 ("invalid address clause for initialized object &!",
9608 Nod, U_Ent);
3cdbaa5a 9609
d9f6a4ee 9610 if Comes_From_Source (Ent) then
9611 Error_Msg_NE
9612 ("\reference to variable& not allowed"
9613 & " (RM 13.1(22))!", Nod, Ent);
9614 else
9615 Error_Msg_N
9616 ("non-static expression not allowed"
9617 & " (RM 13.1(22))!", Nod);
9618 end if;
9619 end if;
3cdbaa5a 9620
d9f6a4ee 9621 when N_Integer_Literal =>
7f694ca2 9622
d9f6a4ee 9623 -- If this is a rewritten unchecked conversion, in a system
9624 -- where Address is an integer type, always use the base type
9625 -- for a literal value. This is user-friendly and prevents
9626 -- order-of-elaboration issues with instances of unchecked
9627 -- conversion.
3cdbaa5a 9628
d9f6a4ee 9629 if Nkind (Original_Node (Nod)) = N_Function_Call then
9630 Set_Etype (Nod, Base_Type (Etype (Nod)));
9631 end if;
e1cedbae 9632
99378362 9633 when N_Character_Literal
9634 | N_Real_Literal
9635 | N_String_Literal
9636 =>
d9f6a4ee 9637 return;
7d20685d 9638
d9f6a4ee 9639 when N_Range =>
9640 Check_Expr_Constants (Low_Bound (Nod));
9641 Check_Expr_Constants (High_Bound (Nod));
231eb581 9642
d9f6a4ee 9643 when N_Explicit_Dereference =>
9644 Check_Expr_Constants (Prefix (Nod));
231eb581 9645
d9f6a4ee 9646 when N_Indexed_Component =>
9647 Check_Expr_Constants (Prefix (Nod));
9648 Check_List_Constants (Expressions (Nod));
7d20685d 9649
d9f6a4ee 9650 when N_Slice =>
9651 Check_Expr_Constants (Prefix (Nod));
9652 Check_Expr_Constants (Discrete_Range (Nod));
cb4c311d 9653
d9f6a4ee 9654 when N_Selected_Component =>
9655 Check_Expr_Constants (Prefix (Nod));
6144c105 9656
d9f6a4ee 9657 when N_Attribute_Reference =>
9658 if Nam_In (Attribute_Name (Nod), Name_Address,
9659 Name_Access,
9660 Name_Unchecked_Access,
9661 Name_Unrestricted_Access)
9662 then
9663 Check_At_Constant_Address (Prefix (Nod));
6144c105 9664
d9f6a4ee 9665 else
9666 Check_Expr_Constants (Prefix (Nod));
9667 Check_List_Constants (Expressions (Nod));
9668 end if;
a7a4a7c2 9669
d9f6a4ee 9670 when N_Aggregate =>
9671 Check_List_Constants (Component_Associations (Nod));
9672 Check_List_Constants (Expressions (Nod));
7d20685d 9673
d9f6a4ee 9674 when N_Component_Association =>
9675 Check_Expr_Constants (Expression (Nod));
e1cedbae 9676
d9f6a4ee 9677 when N_Extension_Aggregate =>
9678 Check_Expr_Constants (Ancestor_Part (Nod));
9679 Check_List_Constants (Component_Associations (Nod));
9680 Check_List_Constants (Expressions (Nod));
3cdbaa5a 9681
d9f6a4ee 9682 when N_Null =>
9683 return;
3cdbaa5a 9684
99378362 9685 when N_Binary_Op
9686 | N_Membership_Test
9687 | N_Short_Circuit
9688 =>
d9f6a4ee 9689 Check_Expr_Constants (Left_Opnd (Nod));
9690 Check_Expr_Constants (Right_Opnd (Nod));
e1cedbae 9691
d9f6a4ee 9692 when N_Unary_Op =>
9693 Check_Expr_Constants (Right_Opnd (Nod));
7f694ca2 9694
99378362 9695 when N_Allocator
9696 | N_Qualified_Expression
9697 | N_Type_Conversion
9698 | N_Unchecked_Type_Conversion
9699 =>
d9f6a4ee 9700 Check_Expr_Constants (Expression (Nod));
47a46747 9701
d9f6a4ee 9702 when N_Function_Call =>
9703 if not Is_Pure (Entity (Name (Nod))) then
9704 Error_Msg_NE
9705 ("invalid address clause for initialized object &!",
9706 Nod, U_Ent);
7f694ca2 9707
d9f6a4ee 9708 Error_Msg_NE
9709 ("\function & is not pure (RM 13.1(22))!",
9710 Nod, Entity (Name (Nod)));
b55f7641 9711
d9f6a4ee 9712 else
9713 Check_List_Constants (Parameter_Associations (Nod));
9714 end if;
b55f7641 9715
d9f6a4ee 9716 when N_Parameter_Association =>
9717 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7d20685d 9718
d9f6a4ee 9719 when others =>
9720 Error_Msg_NE
9721 ("invalid address clause for initialized object &!",
9722 Nod, U_Ent);
9723 Error_Msg_NE
9724 ("\must be constant defined before& (RM 13.1(22))!",
9725 Nod, U_Ent);
9726 end case;
9727 end Check_Expr_Constants;
7d20685d 9728
d9f6a4ee 9729 --------------------------
9730 -- Check_List_Constants --
9731 --------------------------
89f1e35c 9732
d9f6a4ee 9733 procedure Check_List_Constants (Lst : List_Id) is
9734 Nod1 : Node_Id;
7d20685d 9735
d9f6a4ee 9736 begin
9737 if Present (Lst) then
9738 Nod1 := First (Lst);
9739 while Present (Nod1) loop
9740 Check_Expr_Constants (Nod1);
9741 Next (Nod1);
9742 end loop;
9743 end if;
9744 end Check_List_Constants;
81b424ac 9745
d9f6a4ee 9746 -- Start of processing for Check_Constant_Address_Clause
81b424ac 9747
d9f6a4ee 9748 begin
9749 -- If rep_clauses are to be ignored, no need for legality checks. In
9c7948d7 9750 -- particular, no need to pester user about rep clauses that violate the
9751 -- rule on constant addresses, given that these clauses will be removed
9752 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9753 -- we want to relax these checks.
7d20685d 9754
f1a9be43 9755 if not Ignore_Rep_Clauses and not CodePeer_Mode then
d9f6a4ee 9756 Check_Expr_Constants (Expr);
9757 end if;
9758 end Check_Constant_Address_Clause;
7d20685d 9759
6653b695 9760 ---------------------------
9761 -- Check_Pool_Size_Clash --
9762 ---------------------------
9763
9764 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9765 Post : Node_Id;
9766
9767 begin
9768 -- We need to find out which one came first. Note that in the case of
9769 -- aspects mixed with pragmas there are cases where the processing order
9770 -- is reversed, which is why we do the check here.
9771
9772 if Sloc (SP) < Sloc (SS) then
9773 Error_Msg_Sloc := Sloc (SP);
9774 Post := SS;
9775 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9776
9777 else
9778 Error_Msg_Sloc := Sloc (SS);
9779 Post := SP;
9780 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9781 end if;
9782
9783 Error_Msg_N
9784 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9785 end Check_Pool_Size_Clash;
9786
d9f6a4ee 9787 ----------------------------------------
9788 -- Check_Record_Representation_Clause --
9789 ----------------------------------------
85696508 9790
d9f6a4ee 9791 procedure Check_Record_Representation_Clause (N : Node_Id) is
9792 Loc : constant Source_Ptr := Sloc (N);
9793 Ident : constant Node_Id := Identifier (N);
9794 Rectype : Entity_Id;
9795 Fent : Entity_Id;
9796 CC : Node_Id;
9797 Fbit : Uint;
9798 Lbit : Uint;
9799 Hbit : Uint := Uint_0;
9800 Comp : Entity_Id;
9801 Pcomp : Entity_Id;
89f1e35c 9802
d9f6a4ee 9803 Max_Bit_So_Far : Uint;
9804 -- Records the maximum bit position so far. If all field positions
9805 -- are monotonically increasing, then we can skip the circuit for
9806 -- checking for overlap, since no overlap is possible.
85696508 9807
d9f6a4ee 9808 Tagged_Parent : Entity_Id := Empty;
9809 -- This is set in the case of a derived tagged type for which we have
9810 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9811 -- positioned by record representation clauses). In this case we must
9812 -- check for overlap between components of this tagged type, and the
9813 -- components of its parent. Tagged_Parent will point to this parent
9814 -- type. For all other cases Tagged_Parent is left set to Empty.
7d20685d 9815
d9f6a4ee 9816 Parent_Last_Bit : Uint;
9817 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9818 -- last bit position for any field in the parent type. We only need to
9819 -- check overlap for fields starting below this point.
7d20685d 9820
d9f6a4ee 9821 Overlap_Check_Required : Boolean;
9822 -- Used to keep track of whether or not an overlap check is required
7d20685d 9823
d9f6a4ee 9824 Overlap_Detected : Boolean := False;
9825 -- Set True if an overlap is detected
d6f39728 9826
d9f6a4ee 9827 Ccount : Natural := 0;
9828 -- Number of component clauses in record rep clause
d6f39728 9829
d9f6a4ee 9830 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9831 -- Given two entities for record components or discriminants, checks
9832 -- if they have overlapping component clauses and issues errors if so.
d6f39728 9833
d9f6a4ee 9834 procedure Find_Component;
9835 -- Finds component entity corresponding to current component clause (in
9836 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9837 -- start/stop bits for the field. If there is no matching component or
9838 -- if the matching component does not have a component clause, then
9839 -- that's an error and Comp is set to Empty, but no error message is
9840 -- issued, since the message was already given. Comp is also set to
9841 -- Empty if the current "component clause" is in fact a pragma.
d6f39728 9842
d9f6a4ee 9843 -----------------------------
9844 -- Check_Component_Overlap --
9845 -----------------------------
9846
9847 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9848 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9849 CC2 : constant Node_Id := Component_Clause (C2_Ent);
d6f39728 9850
d6f39728 9851 begin
d9f6a4ee 9852 if Present (CC1) and then Present (CC2) then
d6f39728 9853
d9f6a4ee 9854 -- Exclude odd case where we have two tag components in the same
9855 -- record, both at location zero. This seems a bit strange, but
9856 -- it seems to happen in some circumstances, perhaps on an error.
9857
9858 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9859 return;
d6f39728 9860 end if;
9861
d9f6a4ee 9862 -- Here we check if the two fields overlap
9863
d6f39728 9864 declare
d9f6a4ee 9865 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9866 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9867 E1 : constant Uint := S1 + Esize (C1_Ent);
9868 E2 : constant Uint := S2 + Esize (C2_Ent);
d6f39728 9869
9870 begin
d9f6a4ee 9871 if E2 <= S1 or else E1 <= S2 then
9872 null;
d6f39728 9873 else
d9f6a4ee 9874 Error_Msg_Node_2 := Component_Name (CC2);
9875 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9876 Error_Msg_Node_1 := Component_Name (CC1);
9877 Error_Msg_N
9878 ("component& overlaps & #", Component_Name (CC1));
9879 Overlap_Detected := True;
d6f39728 9880 end if;
9881 end;
d6f39728 9882 end if;
d9f6a4ee 9883 end Check_Component_Overlap;
d6f39728 9884
d9f6a4ee 9885 --------------------
9886 -- Find_Component --
9887 --------------------
9dfe12ae 9888
d9f6a4ee 9889 procedure Find_Component is
9dfe12ae 9890
d9f6a4ee 9891 procedure Search_Component (R : Entity_Id);
9892 -- Search components of R for a match. If found, Comp is set
9dfe12ae 9893
d9f6a4ee 9894 ----------------------
9895 -- Search_Component --
9896 ----------------------
e7b2d6bc 9897
d9f6a4ee 9898 procedure Search_Component (R : Entity_Id) is
9899 begin
9900 Comp := First_Component_Or_Discriminant (R);
9901 while Present (Comp) loop
e7b2d6bc 9902
d9f6a4ee 9903 -- Ignore error of attribute name for component name (we
9904 -- already gave an error message for this, so no need to
9905 -- complain here)
e7b2d6bc 9906
d9f6a4ee 9907 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9908 null;
9909 else
9910 exit when Chars (Comp) = Chars (Component_Name (CC));
9dfe12ae 9911 end if;
9912
d9f6a4ee 9913 Next_Component_Or_Discriminant (Comp);
9914 end loop;
9915 end Search_Component;
d6f39728 9916
d9f6a4ee 9917 -- Start of processing for Find_Component
d6f39728 9918
d9f6a4ee 9919 begin
9920 -- Return with Comp set to Empty if we have a pragma
d6f39728 9921
d9f6a4ee 9922 if Nkind (CC) = N_Pragma then
9923 Comp := Empty;
9924 return;
9925 end if;
d6f39728 9926
d9f6a4ee 9927 -- Search current record for matching component
d6f39728 9928
d9f6a4ee 9929 Search_Component (Rectype);
9dfe12ae 9930
d9f6a4ee 9931 -- If not found, maybe component of base type discriminant that is
9932 -- absent from statically constrained first subtype.
e7b2d6bc 9933
d9f6a4ee 9934 if No (Comp) then
9935 Search_Component (Base_Type (Rectype));
9936 end if;
e7b2d6bc 9937
d9f6a4ee 9938 -- If no component, or the component does not reference the component
9939 -- clause in question, then there was some previous error for which
9940 -- we already gave a message, so just return with Comp Empty.
d6f39728 9941
d9f6a4ee 9942 if No (Comp) or else Component_Clause (Comp) /= CC then
9943 Check_Error_Detected;
9944 Comp := Empty;
93735cb8 9945
d9f6a4ee 9946 -- Normal case where we have a component clause
93735cb8 9947
d9f6a4ee 9948 else
9949 Fbit := Component_Bit_Offset (Comp);
9950 Lbit := Fbit + Esize (Comp) - 1;
9951 end if;
9952 end Find_Component;
93735cb8 9953
d9f6a4ee 9954 -- Start of processing for Check_Record_Representation_Clause
d6f39728 9955
d9f6a4ee 9956 begin
9957 Find_Type (Ident);
9958 Rectype := Entity (Ident);
d6f39728 9959
d9f6a4ee 9960 if Rectype = Any_Type then
9961 return;
9962 else
9963 Rectype := Underlying_Type (Rectype);
9964 end if;
d6f39728 9965
d9f6a4ee 9966 -- See if we have a fully repped derived tagged type
d6f39728 9967
d9f6a4ee 9968 declare
9969 PS : constant Entity_Id := Parent_Subtype (Rectype);
d6f39728 9970
d9f6a4ee 9971 begin
9972 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9973 Tagged_Parent := PS;
d6f39728 9974
d9f6a4ee 9975 -- Find maximum bit of any component of the parent type
d6f39728 9976
d9f6a4ee 9977 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9978 Pcomp := First_Entity (Tagged_Parent);
9979 while Present (Pcomp) loop
9980 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9981 if Component_Bit_Offset (Pcomp) /= No_Uint
9982 and then Known_Static_Esize (Pcomp)
9983 then
9984 Parent_Last_Bit :=
9985 UI_Max
9986 (Parent_Last_Bit,
9987 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9988 end if;
b7df4cda 9989 else
9990
9991 -- Skip anonymous types generated for constrained array
9992 -- or record components.
d9f6a4ee 9993
b7df4cda 9994 null;
d6f39728 9995 end if;
b7df4cda 9996
9997 Next_Entity (Pcomp);
d9f6a4ee 9998 end loop;
9999 end if;
10000 end;
d6f39728 10001
d9f6a4ee 10002 -- All done if no component clauses
d6f39728 10003
d9f6a4ee 10004 CC := First (Component_Clauses (N));
d6f39728 10005
d9f6a4ee 10006 if No (CC) then
10007 return;
10008 end if;
d6f39728 10009
d9f6a4ee 10010 -- If a tag is present, then create a component clause that places it
10011 -- at the start of the record (otherwise gigi may place it after other
10012 -- fields that have rep clauses).
d6f39728 10013
d9f6a4ee 10014 Fent := First_Entity (Rectype);
d6f39728 10015
d9f6a4ee 10016 if Nkind (Fent) = N_Defining_Identifier
10017 and then Chars (Fent) = Name_uTag
10018 then
10019 Set_Component_Bit_Offset (Fent, Uint_0);
10020 Set_Normalized_Position (Fent, Uint_0);
10021 Set_Normalized_First_Bit (Fent, Uint_0);
10022 Set_Normalized_Position_Max (Fent, Uint_0);
10023 Init_Esize (Fent, System_Address_Size);
d6f39728 10024
d9f6a4ee 10025 Set_Component_Clause (Fent,
10026 Make_Component_Clause (Loc,
10027 Component_Name => Make_Identifier (Loc, Name_uTag),
d6f39728 10028
d9f6a4ee 10029 Position => Make_Integer_Literal (Loc, Uint_0),
10030 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10031 Last_Bit =>
10032 Make_Integer_Literal (Loc,
10033 UI_From_Int (System_Address_Size))));
d6f39728 10034
d9f6a4ee 10035 Ccount := Ccount + 1;
10036 end if;
d6f39728 10037
d9f6a4ee 10038 Max_Bit_So_Far := Uint_Minus_1;
10039 Overlap_Check_Required := False;
d6f39728 10040
d9f6a4ee 10041 -- Process the component clauses
d6f39728 10042
d9f6a4ee 10043 while Present (CC) loop
10044 Find_Component;
d6f39728 10045
d9f6a4ee 10046 if Present (Comp) then
10047 Ccount := Ccount + 1;
d6f39728 10048
d9f6a4ee 10049 -- We need a full overlap check if record positions non-monotonic
d6f39728 10050
d9f6a4ee 10051 if Fbit <= Max_Bit_So_Far then
10052 Overlap_Check_Required := True;
10053 end if;
d6f39728 10054
d9f6a4ee 10055 Max_Bit_So_Far := Lbit;
d6f39728 10056
d9f6a4ee 10057 -- Check bit position out of range of specified size
01cb2726 10058
d9f6a4ee 10059 if Has_Size_Clause (Rectype)
10060 and then RM_Size (Rectype) <= Lbit
10061 then
10062 Error_Msg_N
10063 ("bit number out of range of specified size",
10064 Last_Bit (CC));
d6f39728 10065
d9f6a4ee 10066 -- Check for overlap with tag component
67278d60 10067
d9f6a4ee 10068 else
10069 if Is_Tagged_Type (Rectype)
10070 and then Fbit < System_Address_Size
10071 then
10072 Error_Msg_NE
10073 ("component overlaps tag field of&",
10074 Component_Name (CC), Rectype);
10075 Overlap_Detected := True;
10076 end if;
67278d60 10077
d9f6a4ee 10078 if Hbit < Lbit then
10079 Hbit := Lbit;
10080 end if;
10081 end if;
67278d60 10082
d9f6a4ee 10083 -- Check parent overlap if component might overlap parent field
67278d60 10084
d9f6a4ee 10085 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
10086 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
10087 while Present (Pcomp) loop
10088 if not Is_Tag (Pcomp)
10089 and then Chars (Pcomp) /= Name_uParent
10090 then
10091 Check_Component_Overlap (Comp, Pcomp);
10092 end if;
67278d60 10093
d9f6a4ee 10094 Next_Component_Or_Discriminant (Pcomp);
10095 end loop;
10096 end if;
10097 end if;
67278d60 10098
d9f6a4ee 10099 Next (CC);
10100 end loop;
47495553 10101
d9f6a4ee 10102 -- Now that we have processed all the component clauses, check for
10103 -- overlap. We have to leave this till last, since the components can
10104 -- appear in any arbitrary order in the representation clause.
67278d60 10105
d9f6a4ee 10106 -- We do not need this check if all specified ranges were monotonic,
10107 -- as recorded by Overlap_Check_Required being False at this stage.
67278d60 10108
d9f6a4ee 10109 -- This first section checks if there are any overlapping entries at
10110 -- all. It does this by sorting all entries and then seeing if there are
10111 -- any overlaps. If there are none, then that is decisive, but if there
10112 -- are overlaps, they may still be OK (they may result from fields in
10113 -- different variants).
67278d60 10114
d9f6a4ee 10115 if Overlap_Check_Required then
10116 Overlap_Check1 : declare
67278d60 10117
d9f6a4ee 10118 OC_Fbit : array (0 .. Ccount) of Uint;
10119 -- First-bit values for component clauses, the value is the offset
10120 -- of the first bit of the field from start of record. The zero
10121 -- entry is for use in sorting.
47495553 10122
d9f6a4ee 10123 OC_Lbit : array (0 .. Ccount) of Uint;
10124 -- Last-bit values for component clauses, the value is the offset
10125 -- of the last bit of the field from start of record. The zero
10126 -- entry is for use in sorting.
10127
10128 OC_Count : Natural := 0;
10129 -- Count of entries in OC_Fbit and OC_Lbit
67278d60 10130
d9f6a4ee 10131 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10132 -- Compare routine for Sort
67278d60 10133
d9f6a4ee 10134 procedure OC_Move (From : Natural; To : Natural);
10135 -- Move routine for Sort
67278d60 10136
d9f6a4ee 10137 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
67278d60 10138
d9f6a4ee 10139 -----------
10140 -- OC_Lt --
10141 -----------
67278d60 10142
d9f6a4ee 10143 function OC_Lt (Op1, Op2 : Natural) return Boolean is
67278d60 10144 begin
d9f6a4ee 10145 return OC_Fbit (Op1) < OC_Fbit (Op2);
10146 end OC_Lt;
67278d60 10147
d9f6a4ee 10148 -------------
10149 -- OC_Move --
10150 -------------
67278d60 10151
d9f6a4ee 10152 procedure OC_Move (From : Natural; To : Natural) is
10153 begin
10154 OC_Fbit (To) := OC_Fbit (From);
10155 OC_Lbit (To) := OC_Lbit (From);
10156 end OC_Move;
67278d60 10157
d9f6a4ee 10158 -- Start of processing for Overlap_Check
67278d60 10159
67278d60 10160 begin
d9f6a4ee 10161 CC := First (Component_Clauses (N));
10162 while Present (CC) loop
67278d60 10163
d9f6a4ee 10164 -- Exclude component clause already marked in error
67278d60 10165
d9f6a4ee 10166 if not Error_Posted (CC) then
10167 Find_Component;
10168
10169 if Present (Comp) then
10170 OC_Count := OC_Count + 1;
10171 OC_Fbit (OC_Count) := Fbit;
10172 OC_Lbit (OC_Count) := Lbit;
10173 end if;
67278d60 10174 end if;
10175
d9f6a4ee 10176 Next (CC);
67278d60 10177 end loop;
67278d60 10178
d9f6a4ee 10179 Sorting.Sort (OC_Count);
67278d60 10180
d9f6a4ee 10181 Overlap_Check_Required := False;
10182 for J in 1 .. OC_Count - 1 loop
10183 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10184 Overlap_Check_Required := True;
10185 exit;
10186 end if;
10187 end loop;
10188 end Overlap_Check1;
10189 end if;
67278d60 10190
d9f6a4ee 10191 -- If Overlap_Check_Required is still True, then we have to do the full
10192 -- scale overlap check, since we have at least two fields that do
10193 -- overlap, and we need to know if that is OK since they are in
10194 -- different variant, or whether we have a definite problem.
67278d60 10195
d9f6a4ee 10196 if Overlap_Check_Required then
10197 Overlap_Check2 : declare
10198 C1_Ent, C2_Ent : Entity_Id;
10199 -- Entities of components being checked for overlap
67278d60 10200
d9f6a4ee 10201 Clist : Node_Id;
10202 -- Component_List node whose Component_Items are being checked
67278d60 10203
d9f6a4ee 10204 Citem : Node_Id;
10205 -- Component declaration for component being checked
67278d60 10206
d9f6a4ee 10207 begin
10208 C1_Ent := First_Entity (Base_Type (Rectype));
67278d60 10209
d9f6a4ee 10210 -- Loop through all components in record. For each component check
10211 -- for overlap with any of the preceding elements on the component
10212 -- list containing the component and also, if the component is in
10213 -- a variant, check against components outside the case structure.
10214 -- This latter test is repeated recursively up the variant tree.
67278d60 10215
d9f6a4ee 10216 Main_Component_Loop : while Present (C1_Ent) loop
10217 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10218 goto Continue_Main_Component_Loop;
10219 end if;
67278d60 10220
d9f6a4ee 10221 -- Skip overlap check if entity has no declaration node. This
10222 -- happens with discriminants in constrained derived types.
10223 -- Possibly we are missing some checks as a result, but that
10224 -- does not seem terribly serious.
67278d60 10225
d9f6a4ee 10226 if No (Declaration_Node (C1_Ent)) then
10227 goto Continue_Main_Component_Loop;
10228 end if;
67278d60 10229
d9f6a4ee 10230 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
67278d60 10231
d9f6a4ee 10232 -- Loop through component lists that need checking. Check the
10233 -- current component list and all lists in variants above us.
67278d60 10234
d9f6a4ee 10235 Component_List_Loop : loop
67278d60 10236
d9f6a4ee 10237 -- If derived type definition, go to full declaration
10238 -- If at outer level, check discriminants if there are any.
67278d60 10239
d9f6a4ee 10240 if Nkind (Clist) = N_Derived_Type_Definition then
10241 Clist := Parent (Clist);
10242 end if;
67278d60 10243
d9f6a4ee 10244 -- Outer level of record definition, check discriminants
67278d60 10245
d9f6a4ee 10246 if Nkind_In (Clist, N_Full_Type_Declaration,
10247 N_Private_Type_Declaration)
67278d60 10248 then
d9f6a4ee 10249 if Has_Discriminants (Defining_Identifier (Clist)) then
10250 C2_Ent :=
10251 First_Discriminant (Defining_Identifier (Clist));
10252 while Present (C2_Ent) loop
10253 exit when C1_Ent = C2_Ent;
10254 Check_Component_Overlap (C1_Ent, C2_Ent);
10255 Next_Discriminant (C2_Ent);
10256 end loop;
10257 end if;
67278d60 10258
d9f6a4ee 10259 -- Record extension case
67278d60 10260
d9f6a4ee 10261 elsif Nkind (Clist) = N_Derived_Type_Definition then
10262 Clist := Empty;
67278d60 10263
d9f6a4ee 10264 -- Otherwise check one component list
67278d60 10265
d9f6a4ee 10266 else
10267 Citem := First (Component_Items (Clist));
10268 while Present (Citem) loop
10269 if Nkind (Citem) = N_Component_Declaration then
10270 C2_Ent := Defining_Identifier (Citem);
10271 exit when C1_Ent = C2_Ent;
10272 Check_Component_Overlap (C1_Ent, C2_Ent);
10273 end if;
67278d60 10274
d9f6a4ee 10275 Next (Citem);
10276 end loop;
10277 end if;
67278d60 10278
d9f6a4ee 10279 -- Check for variants above us (the parent of the Clist can
10280 -- be a variant, in which case its parent is a variant part,
10281 -- and the parent of the variant part is a component list
10282 -- whose components must all be checked against the current
10283 -- component for overlap).
67278d60 10284
d9f6a4ee 10285 if Nkind (Parent (Clist)) = N_Variant then
10286 Clist := Parent (Parent (Parent (Clist)));
67278d60 10287
d9f6a4ee 10288 -- Check for possible discriminant part in record, this
10289 -- is treated essentially as another level in the
10290 -- recursion. For this case the parent of the component
10291 -- list is the record definition, and its parent is the
10292 -- full type declaration containing the discriminant
10293 -- specifications.
10294
10295 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10296 Clist := Parent (Parent ((Clist)));
10297
10298 -- If neither of these two cases, we are at the top of
10299 -- the tree.
10300
10301 else
10302 exit Component_List_Loop;
10303 end if;
10304 end loop Component_List_Loop;
67278d60 10305
d9f6a4ee 10306 <<Continue_Main_Component_Loop>>
10307 Next_Entity (C1_Ent);
67278d60 10308
d9f6a4ee 10309 end loop Main_Component_Loop;
10310 end Overlap_Check2;
67278d60 10311 end if;
10312
d9f6a4ee 10313 -- The following circuit deals with warning on record holes (gaps). We
10314 -- skip this check if overlap was detected, since it makes sense for the
10315 -- programmer to fix this illegality before worrying about warnings.
67278d60 10316
d9f6a4ee 10317 if not Overlap_Detected and Warn_On_Record_Holes then
10318 Record_Hole_Check : declare
10319 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10320 -- Full declaration of record type
67278d60 10321
d9f6a4ee 10322 procedure Check_Component_List
10323 (CL : Node_Id;
10324 Sbit : Uint;
10325 DS : List_Id);
10326 -- Check component list CL for holes. The starting bit should be
10327 -- Sbit. which is zero for the main record component list and set
10328 -- appropriately for recursive calls for variants. DS is set to
10329 -- a list of discriminant specifications to be included in the
10330 -- consideration of components. It is No_List if none to consider.
67278d60 10331
d9f6a4ee 10332 --------------------------
10333 -- Check_Component_List --
10334 --------------------------
47495553 10335
d9f6a4ee 10336 procedure Check_Component_List
10337 (CL : Node_Id;
10338 Sbit : Uint;
10339 DS : List_Id)
10340 is
10341 Compl : Integer;
67278d60 10342
d9f6a4ee 10343 begin
10344 Compl := Integer (List_Length (Component_Items (CL)));
47495553 10345
d9f6a4ee 10346 if DS /= No_List then
10347 Compl := Compl + Integer (List_Length (DS));
10348 end if;
67278d60 10349
d9f6a4ee 10350 declare
10351 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10352 -- Gather components (zero entry is for sort routine)
67278d60 10353
d9f6a4ee 10354 Ncomps : Natural := 0;
10355 -- Number of entries stored in Comps (starting at Comps (1))
67278d60 10356
d9f6a4ee 10357 Citem : Node_Id;
10358 -- One component item or discriminant specification
67278d60 10359
d9f6a4ee 10360 Nbit : Uint;
10361 -- Starting bit for next component
67278d60 10362
d9f6a4ee 10363 CEnt : Entity_Id;
10364 -- Component entity
67278d60 10365
d9f6a4ee 10366 Variant : Node_Id;
10367 -- One variant
67278d60 10368
d9f6a4ee 10369 function Lt (Op1, Op2 : Natural) return Boolean;
10370 -- Compare routine for Sort
67278d60 10371
d9f6a4ee 10372 procedure Move (From : Natural; To : Natural);
10373 -- Move routine for Sort
67278d60 10374
d9f6a4ee 10375 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
67278d60 10376
d9f6a4ee 10377 --------
10378 -- Lt --
10379 --------
67278d60 10380
d9f6a4ee 10381 function Lt (Op1, Op2 : Natural) return Boolean is
10382 begin
10383 return Component_Bit_Offset (Comps (Op1))
10384 <
10385 Component_Bit_Offset (Comps (Op2));
10386 end Lt;
67278d60 10387
d9f6a4ee 10388 ----------
10389 -- Move --
10390 ----------
67278d60 10391
d9f6a4ee 10392 procedure Move (From : Natural; To : Natural) is
10393 begin
10394 Comps (To) := Comps (From);
10395 end Move;
67278d60 10396
d9f6a4ee 10397 begin
10398 -- Gather discriminants into Comp
67278d60 10399
d9f6a4ee 10400 if DS /= No_List then
10401 Citem := First (DS);
10402 while Present (Citem) loop
10403 if Nkind (Citem) = N_Discriminant_Specification then
10404 declare
10405 Ent : constant Entity_Id :=
10406 Defining_Identifier (Citem);
10407 begin
10408 if Ekind (Ent) = E_Discriminant then
10409 Ncomps := Ncomps + 1;
10410 Comps (Ncomps) := Ent;
10411 end if;
10412 end;
10413 end if;
67278d60 10414
d9f6a4ee 10415 Next (Citem);
10416 end loop;
10417 end if;
67278d60 10418
d9f6a4ee 10419 -- Gather component entities into Comp
67278d60 10420
d9f6a4ee 10421 Citem := First (Component_Items (CL));
10422 while Present (Citem) loop
10423 if Nkind (Citem) = N_Component_Declaration then
10424 Ncomps := Ncomps + 1;
10425 Comps (Ncomps) := Defining_Identifier (Citem);
10426 end if;
67278d60 10427
d9f6a4ee 10428 Next (Citem);
10429 end loop;
67278d60 10430
d9f6a4ee 10431 -- Now sort the component entities based on the first bit.
10432 -- Note we already know there are no overlapping components.
67278d60 10433
d9f6a4ee 10434 Sorting.Sort (Ncomps);
67278d60 10435
d9f6a4ee 10436 -- Loop through entries checking for holes
67278d60 10437
d9f6a4ee 10438 Nbit := Sbit;
10439 for J in 1 .. Ncomps loop
10440 CEnt := Comps (J);
67278d60 10441
f55a6472 10442 declare
10443 CBO : constant Uint := Component_Bit_Offset (CEnt);
10444
10445 begin
10446 -- Skip components with unknown offsets
10447
10448 if CBO /= No_Uint and then CBO >= 0 then
10449 Error_Msg_Uint_1 := CBO - Nbit;
67278d60 10450
f55a6472 10451 if Error_Msg_Uint_1 > 0 then
10452 Error_Msg_NE
10453 ("?H?^-bit gap before component&",
10454 Component_Name (Component_Clause (CEnt)),
10455 CEnt);
10456 end if;
10457
10458 Nbit := CBO + Esize (CEnt);
10459 end if;
10460 end;
d9f6a4ee 10461 end loop;
67278d60 10462
d9f6a4ee 10463 -- Process variant parts recursively if present
67278d60 10464
d9f6a4ee 10465 if Present (Variant_Part (CL)) then
10466 Variant := First (Variants (Variant_Part (CL)));
10467 while Present (Variant) loop
10468 Check_Component_List
10469 (Component_List (Variant), Nbit, No_List);
10470 Next (Variant);
10471 end loop;
67278d60 10472 end if;
d9f6a4ee 10473 end;
10474 end Check_Component_List;
67278d60 10475
d9f6a4ee 10476 -- Start of processing for Record_Hole_Check
67278d60 10477
d9f6a4ee 10478 begin
10479 declare
10480 Sbit : Uint;
67278d60 10481
d9f6a4ee 10482 begin
10483 if Is_Tagged_Type (Rectype) then
10484 Sbit := UI_From_Int (System_Address_Size);
10485 else
10486 Sbit := Uint_0;
10487 end if;
10488
10489 if Nkind (Decl) = N_Full_Type_Declaration
10490 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10491 then
10492 Check_Component_List
10493 (Component_List (Type_Definition (Decl)),
10494 Sbit,
10495 Discriminant_Specifications (Decl));
67278d60 10496 end if;
d9f6a4ee 10497 end;
10498 end Record_Hole_Check;
67278d60 10499 end if;
10500
d9f6a4ee 10501 -- For records that have component clauses for all components, and whose
10502 -- size is less than or equal to 32, we need to know the size in the
10503 -- front end to activate possible packed array processing where the
10504 -- component type is a record.
67278d60 10505
d9f6a4ee 10506 -- At this stage Hbit + 1 represents the first unused bit from all the
10507 -- component clauses processed, so if the component clauses are
10508 -- complete, then this is the length of the record.
67278d60 10509
d9f6a4ee 10510 -- For records longer than System.Storage_Unit, and for those where not
10511 -- all components have component clauses, the back end determines the
10512 -- length (it may for example be appropriate to round up the size
10513 -- to some convenient boundary, based on alignment considerations, etc).
67278d60 10514
d9f6a4ee 10515 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
67278d60 10516
d9f6a4ee 10517 -- Nothing to do if at least one component has no component clause
67278d60 10518
d9f6a4ee 10519 Comp := First_Component_Or_Discriminant (Rectype);
10520 while Present (Comp) loop
10521 exit when No (Component_Clause (Comp));
10522 Next_Component_Or_Discriminant (Comp);
10523 end loop;
67278d60 10524
d9f6a4ee 10525 -- If we fall out of loop, all components have component clauses
10526 -- and so we can set the size to the maximum value.
67278d60 10527
d9f6a4ee 10528 if No (Comp) then
10529 Set_RM_Size (Rectype, Hbit + 1);
10530 end if;
10531 end if;
10532 end Check_Record_Representation_Clause;
67278d60 10533
d9f6a4ee 10534 ----------------
10535 -- Check_Size --
10536 ----------------
67278d60 10537
d9f6a4ee 10538 procedure Check_Size
10539 (N : Node_Id;
10540 T : Entity_Id;
10541 Siz : Uint;
10542 Biased : out Boolean)
10543 is
f74a102b 10544 procedure Size_Too_Small_Error (Min_Siz : Uint);
10545 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10546 -- minimum size.
10547
10548 --------------------------
10549 -- Size_Too_Small_Error --
10550 --------------------------
10551
10552 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10553 begin
10554 -- This error is suppressed in ASIS mode to allow for different ASIS
f9906591 10555 -- back ends or ASIS-based tools to query the illegal clause.
f74a102b 10556
10557 if not ASIS_Mode then
10558 Error_Msg_Uint_1 := Min_Siz;
6d22398d 10559 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
f74a102b 10560 end if;
10561 end Size_Too_Small_Error;
10562
10563 -- Local variables
10564
d9f6a4ee 10565 UT : constant Entity_Id := Underlying_Type (T);
10566 M : Uint;
67278d60 10567
f74a102b 10568 -- Start of processing for Check_Size
10569
d9f6a4ee 10570 begin
10571 Biased := False;
67278d60 10572
f74a102b 10573 -- Reject patently improper size values
67278d60 10574
d9f6a4ee 10575 if Is_Elementary_Type (T)
10576 and then Siz > UI_From_Int (Int'Last)
10577 then
10578 Error_Msg_N ("Size value too large for elementary type", N);
67278d60 10579
d9f6a4ee 10580 if Nkind (Original_Node (N)) = N_Op_Expon then
10581 Error_Msg_N
10582 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10583 end if;
10584 end if;
67278d60 10585
d9f6a4ee 10586 -- Dismiss generic types
67278d60 10587
d9f6a4ee 10588 if Is_Generic_Type (T)
10589 or else
10590 Is_Generic_Type (UT)
10591 or else
10592 Is_Generic_Type (Root_Type (UT))
10593 then
10594 return;
67278d60 10595
d9f6a4ee 10596 -- Guard against previous errors
67278d60 10597
d9f6a4ee 10598 elsif No (UT) or else UT = Any_Type then
10599 Check_Error_Detected;
10600 return;
67278d60 10601
d9f6a4ee 10602 -- Check case of bit packed array
67278d60 10603
d9f6a4ee 10604 elsif Is_Array_Type (UT)
10605 and then Known_Static_Component_Size (UT)
10606 and then Is_Bit_Packed_Array (UT)
10607 then
10608 declare
10609 Asiz : Uint;
10610 Indx : Node_Id;
10611 Ityp : Entity_Id;
67278d60 10612
d9f6a4ee 10613 begin
10614 Asiz := Component_Size (UT);
10615 Indx := First_Index (UT);
10616 loop
10617 Ityp := Etype (Indx);
67278d60 10618
d9f6a4ee 10619 -- If non-static bound, then we are not in the business of
10620 -- trying to check the length, and indeed an error will be
10621 -- issued elsewhere, since sizes of non-static array types
10622 -- cannot be set implicitly or explicitly.
67278d60 10623
cda40848 10624 if not Is_OK_Static_Subtype (Ityp) then
d9f6a4ee 10625 return;
10626 end if;
67278d60 10627
d9f6a4ee 10628 -- Otherwise accumulate next dimension
67278d60 10629
d9f6a4ee 10630 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10631 Expr_Value (Type_Low_Bound (Ityp)) +
10632 Uint_1);
67278d60 10633
d9f6a4ee 10634 Next_Index (Indx);
10635 exit when No (Indx);
10636 end loop;
67278d60 10637
d9f6a4ee 10638 if Asiz <= Siz then
10639 return;
67278d60 10640
d9f6a4ee 10641 else
f74a102b 10642 Size_Too_Small_Error (Asiz);
d9f6a4ee 10643 Set_Esize (T, Asiz);
10644 Set_RM_Size (T, Asiz);
10645 end if;
10646 end;
67278d60 10647
d9f6a4ee 10648 -- All other composite types are ignored
67278d60 10649
d9f6a4ee 10650 elsif Is_Composite_Type (UT) then
10651 return;
47495553 10652
d9f6a4ee 10653 -- For fixed-point types, don't check minimum if type is not frozen,
10654 -- since we don't know all the characteristics of the type that can
10655 -- affect the size (e.g. a specified small) till freeze time.
47495553 10656
f74a102b 10657 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
d9f6a4ee 10658 null;
47495553 10659
d9f6a4ee 10660 -- Cases for which a minimum check is required
47495553 10661
d9f6a4ee 10662 else
10663 -- Ignore if specified size is correct for the type
47495553 10664
d9f6a4ee 10665 if Known_Esize (UT) and then Siz = Esize (UT) then
10666 return;
10667 end if;
47495553 10668
d9f6a4ee 10669 -- Otherwise get minimum size
47495553 10670
d9f6a4ee 10671 M := UI_From_Int (Minimum_Size (UT));
47495553 10672
d9f6a4ee 10673 if Siz < M then
47495553 10674
d9f6a4ee 10675 -- Size is less than minimum size, but one possibility remains
10676 -- that we can manage with the new size if we bias the type.
47495553 10677
d9f6a4ee 10678 M := UI_From_Int (Minimum_Size (UT, Biased => True));
47495553 10679
d9f6a4ee 10680 if Siz < M then
f74a102b 10681 Size_Too_Small_Error (M);
10682 Set_Esize (T, M);
d9f6a4ee 10683 Set_RM_Size (T, M);
10684 else
10685 Biased := True;
10686 end if;
10687 end if;
10688 end if;
10689 end Check_Size;
47495553 10690
d9f6a4ee 10691 --------------------------
10692 -- Freeze_Entity_Checks --
10693 --------------------------
47495553 10694
d9f6a4ee 10695 procedure Freeze_Entity_Checks (N : Node_Id) is
8cf481c9 10696 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10697 -- Inspect the primitive operations of type Typ and hide all pairs of
3118058b 10698 -- implicitly declared non-overridden non-fully conformant homographs
10699 -- (Ada RM 8.3 12.3/2).
8cf481c9 10700
10701 -------------------------------------
10702 -- Hide_Non_Overridden_Subprograms --
10703 -------------------------------------
10704
10705 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10706 procedure Hide_Matching_Homographs
10707 (Subp_Id : Entity_Id;
10708 Start_Elmt : Elmt_Id);
10709 -- Inspect a list of primitive operations starting with Start_Elmt
3118058b 10710 -- and find matching implicitly declared non-overridden non-fully
10711 -- conformant homographs of Subp_Id. If found, all matches along
10712 -- with Subp_Id are hidden from all visibility.
8cf481c9 10713
10714 function Is_Non_Overridden_Or_Null_Procedure
10715 (Subp_Id : Entity_Id) return Boolean;
10716 -- Determine whether subprogram Subp_Id is implicitly declared non-
10717 -- overridden subprogram or an implicitly declared null procedure.
10718
10719 ------------------------------
10720 -- Hide_Matching_Homographs --
10721 ------------------------------
10722
10723 procedure Hide_Matching_Homographs
10724 (Subp_Id : Entity_Id;
10725 Start_Elmt : Elmt_Id)
10726 is
10727 Prim : Entity_Id;
10728 Prim_Elmt : Elmt_Id;
10729
10730 begin
10731 Prim_Elmt := Start_Elmt;
10732 while Present (Prim_Elmt) loop
10733 Prim := Node (Prim_Elmt);
10734
10735 -- The current primitive is implicitly declared non-overridden
3118058b 10736 -- non-fully conformant homograph of Subp_Id. Both subprograms
10737 -- must be hidden from visibility.
8cf481c9 10738
10739 if Chars (Prim) = Chars (Subp_Id)
8cf481c9 10740 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
3118058b 10741 and then not Fully_Conformant (Prim, Subp_Id)
8cf481c9 10742 then
8c7ee4ac 10743 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10744 Set_Is_Immediately_Visible (Prim, False);
10745 Set_Is_Potentially_Use_Visible (Prim, False);
8cf481c9 10746
8c7ee4ac 10747 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10748 Set_Is_Immediately_Visible (Subp_Id, False);
10749 Set_Is_Potentially_Use_Visible (Subp_Id, False);
8cf481c9 10750 end if;
10751
10752 Next_Elmt (Prim_Elmt);
10753 end loop;
10754 end Hide_Matching_Homographs;
10755
10756 -----------------------------------------
10757 -- Is_Non_Overridden_Or_Null_Procedure --
10758 -----------------------------------------
10759
10760 function Is_Non_Overridden_Or_Null_Procedure
10761 (Subp_Id : Entity_Id) return Boolean
10762 is
10763 Alias_Id : Entity_Id;
10764
10765 begin
10766 -- The subprogram is inherited (implicitly declared), it does not
10767 -- override and does not cover a primitive of an interface.
10768
10769 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10770 and then Present (Alias (Subp_Id))
10771 and then No (Interface_Alias (Subp_Id))
10772 and then No (Overridden_Operation (Subp_Id))
10773 then
10774 Alias_Id := Alias (Subp_Id);
10775
10776 if Requires_Overriding (Alias_Id) then
10777 return True;
10778
10779 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10780 and then Null_Present (Parent (Alias_Id))
10781 then
10782 return True;
10783 end if;
10784 end if;
10785
10786 return False;
10787 end Is_Non_Overridden_Or_Null_Procedure;
10788
10789 -- Local variables
10790
10791 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10792 Prim : Entity_Id;
10793 Prim_Elmt : Elmt_Id;
10794
10795 -- Start of processing for Hide_Non_Overridden_Subprograms
10796
10797 begin
3118058b 10798 -- Inspect the list of primitives looking for non-overridden
10799 -- subprograms.
8cf481c9 10800
10801 if Present (Prim_Ops) then
10802 Prim_Elmt := First_Elmt (Prim_Ops);
10803 while Present (Prim_Elmt) loop
10804 Prim := Node (Prim_Elmt);
10805 Next_Elmt (Prim_Elmt);
10806
10807 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10808 Hide_Matching_Homographs
10809 (Subp_Id => Prim,
10810 Start_Elmt => Prim_Elmt);
10811 end if;
10812 end loop;
10813 end if;
10814 end Hide_Non_Overridden_Subprograms;
10815
97c23bbe 10816 -- Local variables
8cf481c9 10817
d9f6a4ee 10818 E : constant Entity_Id := Entity (N);
47495553 10819
d9f6a4ee 10820 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10821 -- True in non-generic case. Some of the processing here is skipped
10822 -- for the generic case since it is not needed. Basically in the
10823 -- generic case, we only need to do stuff that might generate error
10824 -- messages or warnings.
8cf481c9 10825
10826 -- Start of processing for Freeze_Entity_Checks
10827
d9f6a4ee 10828 begin
10829 -- Remember that we are processing a freezing entity. Required to
10830 -- ensure correct decoration of internal entities associated with
10831 -- interfaces (see New_Overloaded_Entity).
47495553 10832
d9f6a4ee 10833 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
47495553 10834
d9f6a4ee 10835 -- For tagged types covering interfaces add internal entities that link
10836 -- the primitives of the interfaces with the primitives that cover them.
10837 -- Note: These entities were originally generated only when generating
10838 -- code because their main purpose was to provide support to initialize
10839 -- the secondary dispatch tables. They are now generated also when
10840 -- compiling with no code generation to provide ASIS the relationship
10841 -- between interface primitives and tagged type primitives. They are
10842 -- also used to locate primitives covering interfaces when processing
10843 -- generics (see Derive_Subprograms).
47495553 10844
d9f6a4ee 10845 -- This is not needed in the generic case
47495553 10846
d9f6a4ee 10847 if Ada_Version >= Ada_2005
10848 and then Non_Generic_Case
10849 and then Ekind (E) = E_Record_Type
10850 and then Is_Tagged_Type (E)
10851 and then not Is_Interface (E)
10852 and then Has_Interfaces (E)
10853 then
10854 -- This would be a good common place to call the routine that checks
10855 -- overriding of interface primitives (and thus factorize calls to
10856 -- Check_Abstract_Overriding located at different contexts in the
10857 -- compiler). However, this is not possible because it causes
10858 -- spurious errors in case of late overriding.
47495553 10859
d9f6a4ee 10860 Add_Internal_Interface_Entities (E);
10861 end if;
47495553 10862
8cf481c9 10863 -- After all forms of overriding have been resolved, a tagged type may
10864 -- be left with a set of implicitly declared and possibly erroneous
10865 -- abstract subprograms, null procedures and subprograms that require
0c4e0575 10866 -- overriding. If this set contains fully conformant homographs, then
10867 -- one is chosen arbitrarily (already done during resolution), otherwise
10868 -- all remaining non-fully conformant homographs are hidden from
10869 -- visibility (Ada RM 8.3 12.3/2).
8cf481c9 10870
10871 if Is_Tagged_Type (E) then
10872 Hide_Non_Overridden_Subprograms (E);
10873 end if;
10874
d9f6a4ee 10875 -- Check CPP types
47495553 10876
d9f6a4ee 10877 if Ekind (E) = E_Record_Type
10878 and then Is_CPP_Class (E)
10879 and then Is_Tagged_Type (E)
10880 and then Tagged_Type_Expansion
d9f6a4ee 10881 then
10882 if CPP_Num_Prims (E) = 0 then
47495553 10883
d9f6a4ee 10884 -- If the CPP type has user defined components then it must import
10885 -- primitives from C++. This is required because if the C++ class
10886 -- has no primitives then the C++ compiler does not added the _tag
10887 -- component to the type.
47495553 10888
d9f6a4ee 10889 if First_Entity (E) /= Last_Entity (E) then
10890 Error_Msg_N
10891 ("'C'P'P type must import at least one primitive from C++??",
10892 E);
10893 end if;
10894 end if;
47495553 10895
d9f6a4ee 10896 -- Check that all its primitives are abstract or imported from C++.
10897 -- Check also availability of the C++ constructor.
47495553 10898
d9f6a4ee 10899 declare
10900 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10901 Elmt : Elmt_Id;
10902 Error_Reported : Boolean := False;
10903 Prim : Node_Id;
47495553 10904
d9f6a4ee 10905 begin
10906 Elmt := First_Elmt (Primitive_Operations (E));
10907 while Present (Elmt) loop
10908 Prim := Node (Elmt);
47495553 10909
d9f6a4ee 10910 if Comes_From_Source (Prim) then
10911 if Is_Abstract_Subprogram (Prim) then
10912 null;
47495553 10913
d9f6a4ee 10914 elsif not Is_Imported (Prim)
10915 or else Convention (Prim) /= Convention_CPP
10916 then
10917 Error_Msg_N
10918 ("primitives of 'C'P'P types must be imported from C++ "
10919 & "or abstract??", Prim);
47495553 10920
d9f6a4ee 10921 elsif not Has_Constructors
10922 and then not Error_Reported
10923 then
10924 Error_Msg_Name_1 := Chars (E);
10925 Error_Msg_N
10926 ("??'C'P'P constructor required for type %", Prim);
10927 Error_Reported := True;
10928 end if;
10929 end if;
47495553 10930
d9f6a4ee 10931 Next_Elmt (Elmt);
10932 end loop;
10933 end;
10934 end if;
47495553 10935
d9f6a4ee 10936 -- Check Ada derivation of CPP type
47495553 10937
30ab103b 10938 if Expander_Active -- why? losing errors in -gnatc mode???
10939 and then Present (Etype (E)) -- defend against errors
d9f6a4ee 10940 and then Tagged_Type_Expansion
10941 and then Ekind (E) = E_Record_Type
10942 and then Etype (E) /= E
10943 and then Is_CPP_Class (Etype (E))
10944 and then CPP_Num_Prims (Etype (E)) > 0
10945 and then not Is_CPP_Class (E)
10946 and then not Has_CPP_Constructors (Etype (E))
10947 then
10948 -- If the parent has C++ primitives but it has no constructor then
10949 -- check that all the primitives are overridden in this derivation;
10950 -- otherwise the constructor of the parent is needed to build the
10951 -- dispatch table.
47495553 10952
d9f6a4ee 10953 declare
10954 Elmt : Elmt_Id;
10955 Prim : Node_Id;
47495553 10956
10957 begin
d9f6a4ee 10958 Elmt := First_Elmt (Primitive_Operations (E));
10959 while Present (Elmt) loop
10960 Prim := Node (Elmt);
47495553 10961
d9f6a4ee 10962 if not Is_Abstract_Subprogram (Prim)
10963 and then No (Interface_Alias (Prim))
10964 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
47495553 10965 then
d9f6a4ee 10966 Error_Msg_Name_1 := Chars (Etype (E));
10967 Error_Msg_N
10968 ("'C'P'P constructor required for parent type %", E);
10969 exit;
47495553 10970 end if;
d9f6a4ee 10971
10972 Next_Elmt (Elmt);
10973 end loop;
10974 end;
47495553 10975 end if;
10976
d9f6a4ee 10977 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
67278d60 10978
97c23bbe 10979 -- If we have a type with predicates, build predicate function. This is
10980 -- not needed in the generic case, nor within TSS subprograms and other
10981 -- predefined primitives.
67278d60 10982
97c23bbe 10983 if Is_Type (E)
10984 and then Non_Generic_Case
ea822fd4 10985 and then not Within_Internal_Subprogram
97c23bbe 10986 and then Has_Predicates (E)
ea822fd4 10987 then
d9f6a4ee 10988 Build_Predicate_Functions (E, N);
10989 end if;
67278d60 10990
d9f6a4ee 10991 -- If type has delayed aspects, this is where we do the preanalysis at
10992 -- the freeze point, as part of the consistent visibility check. Note
10993 -- that this must be done after calling Build_Predicate_Functions or
10994 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10995 -- the subtype name in the saved expression so that they will not cause
10996 -- trouble in the preanalysis.
67278d60 10997
61989dbb 10998 -- This is also not needed in the generic case
d9f6a4ee 10999
61989dbb 11000 if Non_Generic_Case
11001 and then Has_Delayed_Aspects (E)
d9f6a4ee 11002 and then Scope (E) = Current_Scope
11003 then
11004 -- Retrieve the visibility to the discriminants in order to properly
11005 -- analyze the aspects.
11006
11007 Push_Scope_And_Install_Discriminants (E);
11008
11009 declare
11010 Ritem : Node_Id;
11011
11012 begin
11013 -- Look for aspect specification entries for this entity
67278d60 11014
d9f6a4ee 11015 Ritem := First_Rep_Item (E);
11016 while Present (Ritem) loop
11017 if Nkind (Ritem) = N_Aspect_Specification
11018 and then Entity (Ritem) = E
11019 and then Is_Delayed_Aspect (Ritem)
11020 then
11021 Check_Aspect_At_Freeze_Point (Ritem);
11022 end if;
67278d60 11023
d9f6a4ee 11024 Next_Rep_Item (Ritem);
11025 end loop;
11026 end;
67278d60 11027
d9f6a4ee 11028 Uninstall_Discriminants_And_Pop_Scope (E);
67278d60 11029 end if;
67278d60 11030
d9f6a4ee 11031 -- For a record type, deal with variant parts. This has to be delayed
d0988351 11032 -- to this point, because of the issue of statically predicated
d9f6a4ee 11033 -- subtypes, which we have to ensure are frozen before checking
11034 -- choices, since we need to have the static choice list set.
d6f39728 11035
d9f6a4ee 11036 if Is_Record_Type (E) then
11037 Check_Variant_Part : declare
11038 D : constant Node_Id := Declaration_Node (E);
11039 T : Node_Id;
11040 C : Node_Id;
11041 VP : Node_Id;
d6f39728 11042
d9f6a4ee 11043 Others_Present : Boolean;
11044 pragma Warnings (Off, Others_Present);
11045 -- Indicates others present, not used in this case
d6f39728 11046
d9f6a4ee 11047 procedure Non_Static_Choice_Error (Choice : Node_Id);
11048 -- Error routine invoked by the generic instantiation below when
11049 -- the variant part has a non static choice.
f117057b 11050
d9f6a4ee 11051 procedure Process_Declarations (Variant : Node_Id);
11052 -- Processes declarations associated with a variant. We analyzed
11053 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11054 -- but we still need the recursive call to Check_Choices for any
11055 -- nested variant to get its choices properly processed. This is
11056 -- also where we expand out the choices if expansion is active.
1f526845 11057
d9f6a4ee 11058 package Variant_Choices_Processing is new
11059 Generic_Check_Choices
11060 (Process_Empty_Choice => No_OP,
11061 Process_Non_Static_Choice => Non_Static_Choice_Error,
11062 Process_Associated_Node => Process_Declarations);
11063 use Variant_Choices_Processing;
f117057b 11064
d9f6a4ee 11065 -----------------------------
11066 -- Non_Static_Choice_Error --
11067 -----------------------------
d6f39728 11068
d9f6a4ee 11069 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11070 begin
11071 Flag_Non_Static_Expr
11072 ("choice given in variant part is not static!", Choice);
11073 end Non_Static_Choice_Error;
d6f39728 11074
d9f6a4ee 11075 --------------------------
11076 -- Process_Declarations --
11077 --------------------------
dba36b60 11078
d9f6a4ee 11079 procedure Process_Declarations (Variant : Node_Id) is
11080 CL : constant Node_Id := Component_List (Variant);
11081 VP : Node_Id;
dba36b60 11082
d9f6a4ee 11083 begin
11084 -- Check for static predicate present in this variant
ea61a7ea 11085
d9f6a4ee 11086 if Has_SP_Choice (Variant) then
ea61a7ea 11087
d9f6a4ee 11088 -- Here we expand. You might expect to find this call in
11089 -- Expand_N_Variant_Part, but that is called when we first
11090 -- see the variant part, and we cannot do this expansion
11091 -- earlier than the freeze point, since for statically
11092 -- predicated subtypes, the predicate is not known till
11093 -- the freeze point.
ea61a7ea 11094
d9f6a4ee 11095 -- Furthermore, we do this expansion even if the expander
11096 -- is not active, because other semantic processing, e.g.
11097 -- for aggregates, requires the expanded list of choices.
ea61a7ea 11098
d9f6a4ee 11099 -- If the expander is not active, then we can't just clobber
11100 -- the list since it would invalidate the ASIS -gnatct tree.
11101 -- So we have to rewrite the variant part with a Rewrite
11102 -- call that replaces it with a copy and clobber the copy.
11103
11104 if not Expander_Active then
11105 declare
11106 NewV : constant Node_Id := New_Copy (Variant);
11107 begin
11108 Set_Discrete_Choices
11109 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11110 Rewrite (Variant, NewV);
11111 end;
11112 end if;
11113
11114 Expand_Static_Predicates_In_Choices (Variant);
ea61a7ea 11115 end if;
11116
d9f6a4ee 11117 -- We don't need to worry about the declarations in the variant
11118 -- (since they were analyzed by Analyze_Choices when we first
11119 -- encountered the variant), but we do need to take care of
11120 -- expansion of any nested variants.
ea61a7ea 11121
d9f6a4ee 11122 if not Null_Present (CL) then
11123 VP := Variant_Part (CL);
ea61a7ea 11124
d9f6a4ee 11125 if Present (VP) then
11126 Check_Choices
11127 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11128 end if;
11129 end if;
11130 end Process_Declarations;
ea61a7ea 11131
d9f6a4ee 11132 -- Start of processing for Check_Variant_Part
b9e61b2a 11133
d9f6a4ee 11134 begin
11135 -- Find component list
ea61a7ea 11136
d9f6a4ee 11137 C := Empty;
ea61a7ea 11138
d9f6a4ee 11139 if Nkind (D) = N_Full_Type_Declaration then
11140 T := Type_Definition (D);
ea61a7ea 11141
d9f6a4ee 11142 if Nkind (T) = N_Record_Definition then
11143 C := Component_List (T);
d6f39728 11144
d9f6a4ee 11145 elsif Nkind (T) = N_Derived_Type_Definition
11146 and then Present (Record_Extension_Part (T))
11147 then
11148 C := Component_List (Record_Extension_Part (T));
11149 end if;
11150 end if;
d6f39728 11151
d9f6a4ee 11152 -- Case of variant part present
d6f39728 11153
d9f6a4ee 11154 if Present (C) and then Present (Variant_Part (C)) then
11155 VP := Variant_Part (C);
ea61a7ea 11156
d9f6a4ee 11157 -- Check choices
ea61a7ea 11158
d9f6a4ee 11159 Check_Choices
11160 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
ea61a7ea 11161
d9f6a4ee 11162 -- If the last variant does not contain the Others choice,
11163 -- replace it with an N_Others_Choice node since Gigi always
11164 -- wants an Others. Note that we do not bother to call Analyze
11165 -- on the modified variant part, since its only effect would be
11166 -- to compute the Others_Discrete_Choices node laboriously, and
11167 -- of course we already know the list of choices corresponding
39a0c1d3 11168 -- to the others choice (it's the list we're replacing).
d6f39728 11169
d9f6a4ee 11170 -- We only want to do this if the expander is active, since
39a0c1d3 11171 -- we do not want to clobber the ASIS tree.
d6f39728 11172
d9f6a4ee 11173 if Expander_Active then
11174 declare
11175 Last_Var : constant Node_Id :=
11176 Last_Non_Pragma (Variants (VP));
d6f39728 11177
d9f6a4ee 11178 Others_Node : Node_Id;
d6f39728 11179
d9f6a4ee 11180 begin
11181 if Nkind (First (Discrete_Choices (Last_Var))) /=
11182 N_Others_Choice
11183 then
11184 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11185 Set_Others_Discrete_Choices
11186 (Others_Node, Discrete_Choices (Last_Var));
11187 Set_Discrete_Choices
11188 (Last_Var, New_List (Others_Node));
11189 end if;
11190 end;
11191 end if;
d6f39728 11192 end if;
d9f6a4ee 11193 end Check_Variant_Part;
d6f39728 11194 end if;
d9f6a4ee 11195 end Freeze_Entity_Checks;
d6f39728 11196
11197 -------------------------
11198 -- Get_Alignment_Value --
11199 -------------------------
11200
11201 function Get_Alignment_Value (Expr : Node_Id) return Uint is
f5d97bf5 11202 Align : constant Uint := Static_Integer (Expr);
f74a102b 11203
f5d97bf5 11204 begin
11205 if Align = No_Uint then
11206 return No_Uint;
11207
11208 elsif Align <= 0 then
f74a102b 11209
f74a102b 11210 -- This error is suppressed in ASIS mode to allow for different ASIS
f9906591 11211 -- back ends or ASIS-based tools to query the illegal clause.
f74a102b 11212
11213 if not ASIS_Mode then
11214 Error_Msg_N ("alignment value must be positive", Expr);
11215 end if;
f74a102b 11216
d6f39728 11217 return No_Uint;
11218
11219 else
11220 for J in Int range 0 .. 64 loop
11221 declare
11222 M : constant Uint := Uint_2 ** J;
11223
11224 begin
11225 exit when M = Align;
11226
11227 if M > Align then
f5d97bf5 11228
11229 -- This error is suppressed in ASIS mode to allow for
f9906591 11230 -- different ASIS back ends or ASIS-based tools to query the
f5d97bf5 11231 -- illegal clause.
11232
11233 if not ASIS_Mode then
11234 Error_Msg_N ("alignment value must be power of 2", Expr);
11235 end if;
11236
d6f39728 11237 return No_Uint;
11238 end if;
11239 end;
11240 end loop;
11241
11242 return Align;
11243 end if;
11244 end Get_Alignment_Value;
11245
99a2d5bd 11246 -------------------------------------
11247 -- Inherit_Aspects_At_Freeze_Point --
11248 -------------------------------------
11249
11250 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11251 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11252 (Rep_Item : Node_Id) return Boolean;
11253 -- This routine checks if Rep_Item is either a pragma or an aspect
11254 -- specification node whose correponding pragma (if any) is present in
11255 -- the Rep Item chain of the entity it has been specified to.
11256
11257 --------------------------------------------------
11258 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11259 --------------------------------------------------
11260
11261 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11262 (Rep_Item : Node_Id) return Boolean
11263 is
11264 begin
ec6f6da5 11265 return
11266 Nkind (Rep_Item) = N_Pragma
11267 or else Present_In_Rep_Item
11268 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
99a2d5bd 11269 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11270
29a9d4be 11271 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11272
99a2d5bd 11273 begin
11274 -- A representation item is either subtype-specific (Size and Alignment
11275 -- clauses) or type-related (all others). Subtype-specific aspects may
29a9d4be 11276 -- differ for different subtypes of the same type (RM 13.1.8).
99a2d5bd 11277
11278 -- A derived type inherits each type-related representation aspect of
11279 -- its parent type that was directly specified before the declaration of
29a9d4be 11280 -- the derived type (RM 13.1.15).
99a2d5bd 11281
11282 -- A derived subtype inherits each subtype-specific representation
11283 -- aspect of its parent subtype that was directly specified before the
29a9d4be 11284 -- declaration of the derived type (RM 13.1.15).
99a2d5bd 11285
11286 -- The general processing involves inheriting a representation aspect
11287 -- from a parent type whenever the first rep item (aspect specification,
11288 -- attribute definition clause, pragma) corresponding to the given
11289 -- representation aspect in the rep item chain of Typ, if any, isn't
11290 -- directly specified to Typ but to one of its parents.
11291
11292 -- ??? Note that, for now, just a limited number of representation
29a9d4be 11293 -- aspects have been inherited here so far. Many of them are
11294 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11295 -- a non- exhaustive list of aspects that likely also need to
11296 -- be moved to this routine: Alignment, Component_Alignment,
11297 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
99a2d5bd 11298 -- Preelaborable_Initialization, RM_Size and Small.
11299
8b6e9bf2 11300 -- In addition, Convention must be propagated from base type to subtype,
11301 -- because the subtype may have been declared on an incomplete view.
11302
99a2d5bd 11303 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11304 return;
11305 end if;
11306
11307 -- Ada_05/Ada_2005
11308
11309 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11310 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11311 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11312 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11313 then
11314 Set_Is_Ada_2005_Only (Typ);
11315 end if;
11316
11317 -- Ada_12/Ada_2012
11318
11319 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11320 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11321 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11322 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11323 then
11324 Set_Is_Ada_2012_Only (Typ);
11325 end if;
11326
11327 -- Atomic/Shared
11328
11329 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11330 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11331 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11332 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11333 then
11334 Set_Is_Atomic (Typ);
99a2d5bd 11335 Set_Is_Volatile (Typ);
4bf2acc9 11336 Set_Treat_As_Volatile (Typ);
99a2d5bd 11337 end if;
11338
8b6e9bf2 11339 -- Convention
11340
7ac4254e 11341 if Is_Record_Type (Typ)
11342 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11343 then
8b6e9bf2 11344 Set_Convention (Typ, Convention (Base_Type (Typ)));
11345 end if;
11346
29a9d4be 11347 -- Default_Component_Value
99a2d5bd 11348
81c2bc19 11349 -- Verify that there is no rep_item declared for the type, and there
11350 -- is one coming from an ancestor.
11351
99a2d5bd 11352 if Is_Array_Type (Typ)
f3d70f08 11353 and then Is_Base_Type (Typ)
81c2bc19 11354 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
99a2d5bd 11355 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11356 then
11357 Set_Default_Aspect_Component_Value (Typ,
11358 Default_Aspect_Component_Value
11359 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11360 end if;
11361
29a9d4be 11362 -- Default_Value
99a2d5bd 11363
11364 if Is_Scalar_Type (Typ)
f3d70f08 11365 and then Is_Base_Type (Typ)
81c2bc19 11366 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
99a2d5bd 11367 and then Has_Rep_Item (Typ, Name_Default_Value)
11368 then
81c2bc19 11369 Set_Has_Default_Aspect (Typ);
99a2d5bd 11370 Set_Default_Aspect_Value (Typ,
11371 Default_Aspect_Value
11372 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11373 end if;
11374
11375 -- Discard_Names
11376
11377 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11378 and then Has_Rep_Item (Typ, Name_Discard_Names)
11379 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11380 (Get_Rep_Item (Typ, Name_Discard_Names))
11381 then
11382 Set_Discard_Names (Typ);
11383 end if;
11384
99a2d5bd 11385 -- Volatile
11386
11387 if not Has_Rep_Item (Typ, Name_Volatile, False)
11388 and then Has_Rep_Item (Typ, Name_Volatile)
11389 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11390 (Get_Rep_Item (Typ, Name_Volatile))
11391 then
99a2d5bd 11392 Set_Is_Volatile (Typ);
4bf2acc9 11393 Set_Treat_As_Volatile (Typ);
99a2d5bd 11394 end if;
11395
2fe893b9 11396 -- Volatile_Full_Access
11397
11398 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11399 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11400 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11401 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11402 then
4bf2acc9 11403 Set_Is_Volatile_Full_Access (Typ);
2fe893b9 11404 Set_Is_Volatile (Typ);
4bf2acc9 11405 Set_Treat_As_Volatile (Typ);
2fe893b9 11406 end if;
11407
99a2d5bd 11408 -- Inheritance for derived types only
11409
11410 if Is_Derived_Type (Typ) then
11411 declare
11412 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11413 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11414
11415 begin
11416 -- Atomic_Components
11417
11418 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11419 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11420 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11421 (Get_Rep_Item (Typ, Name_Atomic_Components))
11422 then
11423 Set_Has_Atomic_Components (Imp_Bas_Typ);
11424 end if;
11425
11426 -- Volatile_Components
11427
11428 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11429 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11430 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11431 (Get_Rep_Item (Typ, Name_Volatile_Components))
11432 then
11433 Set_Has_Volatile_Components (Imp_Bas_Typ);
11434 end if;
11435
e81df51c 11436 -- Finalize_Storage_Only
99a2d5bd 11437
11438 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11439 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11440 then
11441 Set_Finalize_Storage_Only (Bas_Typ);
11442 end if;
11443
11444 -- Universal_Aliasing
11445
11446 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11447 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11448 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11449 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11450 then
11451 Set_Universal_Aliasing (Imp_Bas_Typ);
11452 end if;
11453
e81df51c 11454 -- Bit_Order
99a2d5bd 11455
11456 if Is_Record_Type (Typ) then
99a2d5bd 11457 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11458 and then Has_Rep_Item (Typ, Name_Bit_Order)
11459 then
11460 Set_Reverse_Bit_Order (Bas_Typ,
11461 Reverse_Bit_Order (Entity (Name
11462 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11463 end if;
e81df51c 11464 end if;
11465
e9218716 11466 -- Scalar_Storage_Order
11467
11468 -- Note: the aspect is specified on a first subtype, but recorded
11469 -- in a flag of the base type!
e81df51c 11470
11471 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
29b91bc7 11472 and then Typ = Bas_Typ
e81df51c 11473 then
e81df51c 11474 -- For a type extension, always inherit from parent; otherwise
11475 -- inherit if no default applies. Note: we do not check for
11476 -- an explicit rep item on the parent type when inheriting,
11477 -- because the parent SSO may itself have been set by default.
99a2d5bd 11478
e9218716 11479 if not Has_Rep_Item (First_Subtype (Typ),
11480 Name_Scalar_Storage_Order, False)
e81df51c 11481 and then (Is_Tagged_Type (Bas_Typ)
29b91bc7 11482 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11483 or else
11484 SSO_Set_High_By_Default (Bas_Typ)))
99a2d5bd 11485 then
11486 Set_Reverse_Storage_Order (Bas_Typ,
423b89fd 11487 Reverse_Storage_Order
11488 (Implementation_Base_Type (Etype (Bas_Typ))));
b64082f2 11489
11490 -- Clear default SSO indications, since the inherited aspect
11491 -- which was set explicitly overrides the default.
11492
11493 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11494 Set_SSO_Set_High_By_Default (Bas_Typ, False);
99a2d5bd 11495 end if;
11496 end if;
11497 end;
11498 end if;
11499 end Inherit_Aspects_At_Freeze_Point;
11500
d6f39728 11501 ----------------
11502 -- Initialize --
11503 ----------------
11504
11505 procedure Initialize is
11506 begin
7717ea00 11507 Address_Clause_Checks.Init;
76a6b7c7 11508 Compile_Time_Warnings_Errors.Init;
d6f39728 11509 Unchecked_Conversions.Init;
dba38d2f 11510
36ac5fbb 11511 if AAMP_On_Target then
dba38d2f 11512 Independence_Checks.Init;
11513 end if;
d6f39728 11514 end Initialize;
11515
2625eb01 11516 ---------------------------
11517 -- Install_Discriminants --
11518 ---------------------------
11519
11520 procedure Install_Discriminants (E : Entity_Id) is
11521 Disc : Entity_Id;
11522 Prev : Entity_Id;
11523 begin
11524 Disc := First_Discriminant (E);
11525 while Present (Disc) loop
11526 Prev := Current_Entity (Disc);
11527 Set_Current_Entity (Disc);
11528 Set_Is_Immediately_Visible (Disc);
11529 Set_Homonym (Disc, Prev);
11530 Next_Discriminant (Disc);
11531 end loop;
11532 end Install_Discriminants;
11533
d6f39728 11534 -------------------------
11535 -- Is_Operational_Item --
11536 -------------------------
11537
11538 function Is_Operational_Item (N : Node_Id) return Boolean is
11539 begin
11540 if Nkind (N) /= N_Attribute_Definition_Clause then
11541 return False;
b9e61b2a 11542
d6f39728 11543 else
11544 declare
b9e61b2a 11545 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
d6f39728 11546 begin
078a74b8 11547
55ab5265 11548 -- List of operational items is given in AARM 13.1(8.mm/1).
078a74b8 11549 -- It is clearly incomplete, as it does not include iterator
11550 -- aspects, among others.
11551
11552 return Id = Attribute_Constant_Indexing
11553 or else Id = Attribute_Default_Iterator
11554 or else Id = Attribute_Implicit_Dereference
11555 or else Id = Attribute_Input
11556 or else Id = Attribute_Iterator_Element
11557 or else Id = Attribute_Iterable
d6f39728 11558 or else Id = Attribute_Output
11559 or else Id = Attribute_Read
078a74b8 11560 or else Id = Attribute_Variable_Indexing
f15731c4 11561 or else Id = Attribute_Write
11562 or else Id = Attribute_External_Tag;
d6f39728 11563 end;
11564 end if;
11565 end Is_Operational_Item;
11566
3b23aaa0 11567 -------------------------
11568 -- Is_Predicate_Static --
11569 -------------------------
11570
94d896aa 11571 -- Note: the basic legality of the expression has already been checked, so
11572 -- we don't need to worry about cases or ranges on strings for example.
11573
3b23aaa0 11574 function Is_Predicate_Static
11575 (Expr : Node_Id;
11576 Nam : Name_Id) return Boolean
11577 is
11578 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
973c2fba 11579 -- Given a list of case expression alternatives, returns True if all
11580 -- the alternatives are static (have all static choices, and a static
11581 -- expression).
3b23aaa0 11582
11583 function All_Static_Choices (L : List_Id) return Boolean;
a360a0f7 11584 -- Returns true if all elements of the list are OK static choices
3b23aaa0 11585 -- as defined below for Is_Static_Choice. Used for case expression
973c2fba 11586 -- alternatives and for the right operand of a membership test. An
11587 -- others_choice is static if the corresponding expression is static.
7c0c95b8 11588 -- The staticness of the bounds is checked separately.
3b23aaa0 11589
11590 function Is_Static_Choice (N : Node_Id) return Boolean;
11591 -- Returns True if N represents a static choice (static subtype, or
a360a0f7 11592 -- static subtype indication, or static expression, or static range).
3b23aaa0 11593 --
11594 -- Note that this is a bit more inclusive than we actually need
11595 -- (in particular membership tests do not allow the use of subtype
a360a0f7 11596 -- indications). But that doesn't matter, we have already checked
3b23aaa0 11597 -- that the construct is legal to get this far.
11598
11599 function Is_Type_Ref (N : Node_Id) return Boolean;
11600 pragma Inline (Is_Type_Ref);
973c2fba 11601 -- Returns True if N is a reference to the type for the predicate in the
11602 -- expression (i.e. if it is an identifier whose Chars field matches the
11603 -- Nam given in the call). N must not be parenthesized, if the type name
11604 -- appears in parens, this routine will return False.
10f62e3a 11605 --
ea90be0f 11606 -- The routine also returns True for function calls generated during the
11607 -- expansion of comparison operators on strings, which are intended to
11608 -- be legal in static predicates, and are converted into calls to array
11609 -- comparison routines in the body of the corresponding predicate
11610 -- function.
11611
3b23aaa0 11612 ----------------------------------
11613 -- All_Static_Case_Alternatives --
11614 ----------------------------------
11615
11616 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11617 N : Node_Id;
11618
11619 begin
11620 N := First (L);
11621 while Present (N) loop
11622 if not (All_Static_Choices (Discrete_Choices (N))
11623 and then Is_OK_Static_Expression (Expression (N)))
11624 then
11625 return False;
11626 end if;
11627
11628 Next (N);
11629 end loop;
11630
11631 return True;
11632 end All_Static_Case_Alternatives;
11633
11634 ------------------------
11635 -- All_Static_Choices --
11636 ------------------------
11637
11638 function All_Static_Choices (L : List_Id) return Boolean is
11639 N : Node_Id;
11640
11641 begin
11642 N := First (L);
11643 while Present (N) loop
11644 if not Is_Static_Choice (N) then
11645 return False;
11646 end if;
11647
11648 Next (N);
11649 end loop;
11650
11651 return True;
11652 end All_Static_Choices;
11653
11654 ----------------------
11655 -- Is_Static_Choice --
11656 ----------------------
11657
11658 function Is_Static_Choice (N : Node_Id) return Boolean is
11659 begin
7c0c95b8 11660 return Nkind (N) = N_Others_Choice
11661 or else Is_OK_Static_Expression (N)
3b23aaa0 11662 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11663 and then Is_OK_Static_Subtype (Entity (N)))
11664 or else (Nkind (N) = N_Subtype_Indication
11665 and then Is_OK_Static_Subtype (Entity (N)))
11666 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11667 end Is_Static_Choice;
11668
11669 -----------------
11670 -- Is_Type_Ref --
11671 -----------------
11672
11673 function Is_Type_Ref (N : Node_Id) return Boolean is
11674 begin
ea90be0f 11675 return (Nkind (N) = N_Identifier
11676 and then Chars (N) = Nam
11677 and then Paren_Count (N) = 0)
11678 or else Nkind (N) = N_Function_Call;
3b23aaa0 11679 end Is_Type_Ref;
11680
11681 -- Start of processing for Is_Predicate_Static
11682
11683 begin
3b23aaa0 11684 -- Predicate_Static means one of the following holds. Numbers are the
11685 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11686
11687 -- 16: A static expression
11688
11689 if Is_OK_Static_Expression (Expr) then
11690 return True;
11691
11692 -- 17: A membership test whose simple_expression is the current
11693 -- instance, and whose membership_choice_list meets the requirements
11694 -- for a static membership test.
11695
11696 elsif Nkind (Expr) in N_Membership_Test
11697 and then ((Present (Right_Opnd (Expr))
11698 and then Is_Static_Choice (Right_Opnd (Expr)))
11699 or else
11700 (Present (Alternatives (Expr))
11701 and then All_Static_Choices (Alternatives (Expr))))
11702 then
11703 return True;
11704
11705 -- 18. A case_expression whose selecting_expression is the current
11706 -- instance, and whose dependent expressions are static expressions.
11707
11708 elsif Nkind (Expr) = N_Case_Expression
11709 and then Is_Type_Ref (Expression (Expr))
11710 and then All_Static_Case_Alternatives (Alternatives (Expr))
11711 then
11712 return True;
11713
11714 -- 19. A call to a predefined equality or ordering operator, where one
11715 -- operand is the current instance, and the other is a static
11716 -- expression.
11717
94d896aa 11718 -- Note: the RM is clearly wrong here in not excluding string types.
11719 -- Without this exclusion, we would allow expressions like X > "ABC"
11720 -- to be considered as predicate-static, which is clearly not intended,
11721 -- since the idea is for predicate-static to be a subset of normal
11722 -- static expressions (and "DEF" > "ABC" is not a static expression).
11723
11724 -- However, we do allow internally generated (not from source) equality
11725 -- and inequality operations to be valid on strings (this helps deal
11726 -- with cases where we transform A in "ABC" to A = "ABC).
11727
ea90be0f 11728 -- In fact, it appears that the intent of the ARG is to extend static
11729 -- predicates to strings, and that the extension should probably apply
11730 -- to static expressions themselves. The code below accepts comparison
11731 -- operators that apply to static strings.
11732
3b23aaa0 11733 elsif Nkind (Expr) in N_Op_Compare
11734 and then ((Is_Type_Ref (Left_Opnd (Expr))
11735 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11736 or else
11737 (Is_Type_Ref (Right_Opnd (Expr))
11738 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11739 then
11740 return True;
11741
11742 -- 20. A call to a predefined boolean logical operator, where each
11743 -- operand is predicate-static.
11744
11745 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11746 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11747 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11748 or else
11749 (Nkind (Expr) = N_Op_Not
11750 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11751 then
11752 return True;
11753
11754 -- 21. A short-circuit control form where both operands are
11755 -- predicate-static.
11756
11757 elsif Nkind (Expr) in N_Short_Circuit
11758 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11759 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11760 then
11761 return True;
11762
11763 -- 22. A parenthesized predicate-static expression. This does not
11764 -- require any special test, since we just ignore paren levels in
11765 -- all the cases above.
11766
11767 -- One more test that is an implementation artifact caused by the fact
499918a7 11768 -- that we are analyzing not the original expression, but the generated
3b23aaa0 11769 -- expression in the body of the predicate function. This can include
a360a0f7 11770 -- references to inherited predicates, so that the expression we are
3b23aaa0 11771 -- processing looks like:
11772
75491446 11773 -- xxPredicate (typ (Inns)) and then expression
3b23aaa0 11774
11775 -- Where the call is to a Predicate function for an inherited predicate.
60a4a5af 11776 -- We simply ignore such a call, which could be to either a dynamic or
11777 -- a static predicate. Note that if the parent predicate is dynamic then
11778 -- eventually this type will be marked as dynamic, but you are allowed
11779 -- to specify a static predicate for a subtype which is inheriting a
11780 -- dynamic predicate, so the static predicate validation here ignores
11781 -- the inherited predicate even if it is dynamic.
7db33803 11782 -- In all cases, a static predicate can only apply to a scalar type.
3b23aaa0 11783
11784 elsif Nkind (Expr) = N_Function_Call
11785 and then Is_Predicate_Function (Entity (Name (Expr)))
7db33803 11786 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
3b23aaa0 11787 then
11788 return True;
11789
11790 -- That's an exhaustive list of tests, all other cases are not
a360a0f7 11791 -- predicate-static, so we return False.
3b23aaa0 11792
11793 else
11794 return False;
11795 end if;
11796 end Is_Predicate_Static;
11797
2ff55065 11798 ---------------------
11799 -- Kill_Rep_Clause --
11800 ---------------------
11801
11802 procedure Kill_Rep_Clause (N : Node_Id) is
11803 begin
11804 pragma Assert (Ignore_Rep_Clauses);
360f426f 11805
11806 -- Note: we use Replace rather than Rewrite, because we don't want
11807 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11808 -- rep clause that is being replaced.
11809
4949ddd5 11810 Replace (N, Make_Null_Statement (Sloc (N)));
360f426f 11811
11812 -- The null statement must be marked as not coming from source. This is
37c6552c 11813 -- so that ASIS ignores it, and also the back end does not expect bogus
360f426f 11814 -- "from source" null statements in weird places (e.g. in declarative
11815 -- regions where such null statements are not allowed).
11816
11817 Set_Comes_From_Source (N, False);
2ff55065 11818 end Kill_Rep_Clause;
11819
d6f39728 11820 ------------------
11821 -- Minimum_Size --
11822 ------------------
11823
11824 function Minimum_Size
11825 (T : Entity_Id;
d5b349fa 11826 Biased : Boolean := False) return Nat
d6f39728 11827 is
11828 Lo : Uint := No_Uint;
11829 Hi : Uint := No_Uint;
11830 LoR : Ureal := No_Ureal;
11831 HiR : Ureal := No_Ureal;
11832 LoSet : Boolean := False;
11833 HiSet : Boolean := False;
11834 B : Uint;
11835 S : Nat;
11836 Ancest : Entity_Id;
f15731c4 11837 R_Typ : constant Entity_Id := Root_Type (T);
d6f39728 11838
11839 begin
11840 -- If bad type, return 0
11841
11842 if T = Any_Type then
11843 return 0;
11844
11845 -- For generic types, just return zero. There cannot be any legitimate
11846 -- need to know such a size, but this routine may be called with a
11847 -- generic type as part of normal processing.
11848
f02a9a9a 11849 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
d6f39728 11850 return 0;
11851
74c7ae52 11852 -- Access types (cannot have size smaller than System.Address)
d6f39728 11853
11854 elsif Is_Access_Type (T) then
74c7ae52 11855 return System_Address_Size;
d6f39728 11856
11857 -- Floating-point types
11858
11859 elsif Is_Floating_Point_Type (T) then
f15731c4 11860 return UI_To_Int (Esize (R_Typ));
d6f39728 11861
11862 -- Discrete types
11863
11864 elsif Is_Discrete_Type (T) then
11865
fdd294d1 11866 -- The following loop is looking for the nearest compile time known
11867 -- bounds following the ancestor subtype chain. The idea is to find
11868 -- the most restrictive known bounds information.
d6f39728 11869
11870 Ancest := T;
11871 loop
11872 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11873 return 0;
11874 end if;
11875
11876 if not LoSet then
11877 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11878 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11879 LoSet := True;
11880 exit when HiSet;
11881 end if;
11882 end if;
11883
11884 if not HiSet then
11885 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11886 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11887 HiSet := True;
11888 exit when LoSet;
11889 end if;
11890 end if;
11891
11892 Ancest := Ancestor_Subtype (Ancest);
11893
11894 if No (Ancest) then
11895 Ancest := Base_Type (T);
11896
11897 if Is_Generic_Type (Ancest) then
11898 return 0;
11899 end if;
11900 end if;
11901 end loop;
11902
11903 -- Fixed-point types. We can't simply use Expr_Value to get the
fdd294d1 11904 -- Corresponding_Integer_Value values of the bounds, since these do not
11905 -- get set till the type is frozen, and this routine can be called
11906 -- before the type is frozen. Similarly the test for bounds being static
11907 -- needs to include the case where we have unanalyzed real literals for
11908 -- the same reason.
d6f39728 11909
11910 elsif Is_Fixed_Point_Type (T) then
11911
fdd294d1 11912 -- The following loop is looking for the nearest compile time known
11913 -- bounds following the ancestor subtype chain. The idea is to find
11914 -- the most restrictive known bounds information.
d6f39728 11915
11916 Ancest := T;
11917 loop
11918 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11919 return 0;
11920 end if;
11921
3062c401 11922 -- Note: In the following two tests for LoSet and HiSet, it may
11923 -- seem redundant to test for N_Real_Literal here since normally
11924 -- one would assume that the test for the value being known at
11925 -- compile time includes this case. However, there is a glitch.
11926 -- If the real literal comes from folding a non-static expression,
11927 -- then we don't consider any non- static expression to be known
11928 -- at compile time if we are in configurable run time mode (needed
11929 -- in some cases to give a clearer definition of what is and what
11930 -- is not accepted). So the test is indeed needed. Without it, we
11931 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11932
d6f39728 11933 if not LoSet then
11934 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11935 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11936 then
11937 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11938 LoSet := True;
11939 exit when HiSet;
11940 end if;
11941 end if;
11942
11943 if not HiSet then
11944 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11945 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11946 then
11947 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11948 HiSet := True;
11949 exit when LoSet;
11950 end if;
11951 end if;
11952
11953 Ancest := Ancestor_Subtype (Ancest);
11954
11955 if No (Ancest) then
11956 Ancest := Base_Type (T);
11957
11958 if Is_Generic_Type (Ancest) then
11959 return 0;
11960 end if;
11961 end if;
11962 end loop;
11963
11964 Lo := UR_To_Uint (LoR / Small_Value (T));
11965 Hi := UR_To_Uint (HiR / Small_Value (T));
11966
11967 -- No other types allowed
11968
11969 else
11970 raise Program_Error;
11971 end if;
11972
2866d595 11973 -- Fall through with Hi and Lo set. Deal with biased case
d6f39728 11974
cc46ff4b 11975 if (Biased
11976 and then not Is_Fixed_Point_Type (T)
11977 and then not (Is_Enumeration_Type (T)
11978 and then Has_Non_Standard_Rep (T)))
d6f39728 11979 or else Has_Biased_Representation (T)
11980 then
11981 Hi := Hi - Lo;
11982 Lo := Uint_0;
11983 end if;
11984
005366f7 11985 -- Null range case, size is always zero. We only do this in the discrete
11986 -- type case, since that's the odd case that came up. Probably we should
11987 -- also do this in the fixed-point case, but doing so causes peculiar
11988 -- gigi failures, and it is not worth worrying about this incredibly
11989 -- marginal case (explicit null-range fixed-point type declarations)???
11990
11991 if Lo > Hi and then Is_Discrete_Type (T) then
11992 S := 0;
11993
d6f39728 11994 -- Signed case. Note that we consider types like range 1 .. -1 to be
fdd294d1 11995 -- signed for the purpose of computing the size, since the bounds have
1a34e48c 11996 -- to be accommodated in the base type.
d6f39728 11997
005366f7 11998 elsif Lo < 0 or else Hi < 0 then
d6f39728 11999 S := 1;
12000 B := Uint_1;
12001
da253936 12002 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
12003 -- Note that we accommodate the case where the bounds cross. This
d6f39728 12004 -- can happen either because of the way the bounds are declared
12005 -- or because of the algorithm in Freeze_Fixed_Point_Type.
12006
12007 while Lo < -B
12008 or else Hi < -B
12009 or else Lo >= B
12010 or else Hi >= B
12011 loop
12012 B := Uint_2 ** S;
12013 S := S + 1;
12014 end loop;
12015
12016 -- Unsigned case
12017
12018 else
12019 -- If both bounds are positive, make sure that both are represen-
12020 -- table in the case where the bounds are crossed. This can happen
12021 -- either because of the way the bounds are declared, or because of
12022 -- the algorithm in Freeze_Fixed_Point_Type.
12023
12024 if Lo > Hi then
12025 Hi := Lo;
12026 end if;
12027
da253936 12028 -- S = size, (can accommodate 0 .. (2**size - 1))
d6f39728 12029
12030 S := 0;
12031 while Hi >= Uint_2 ** S loop
12032 S := S + 1;
12033 end loop;
12034 end if;
12035
12036 return S;
12037 end Minimum_Size;
12038
44e4341e 12039 ---------------------------
12040 -- New_Stream_Subprogram --
12041 ---------------------------
d6f39728 12042
44e4341e 12043 procedure New_Stream_Subprogram
12044 (N : Node_Id;
12045 Ent : Entity_Id;
12046 Subp : Entity_Id;
12047 Nam : TSS_Name_Type)
d6f39728 12048 is
12049 Loc : constant Source_Ptr := Sloc (N);
9dfe12ae 12050 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
f15731c4 12051 Subp_Id : Entity_Id;
d6f39728 12052 Subp_Decl : Node_Id;
12053 F : Entity_Id;
12054 Etyp : Entity_Id;
12055
44e4341e 12056 Defer_Declaration : constant Boolean :=
12057 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12058 -- For a tagged type, there is a declaration for each stream attribute
12059 -- at the freeze point, and we must generate only a completion of this
12060 -- declaration. We do the same for private types, because the full view
12061 -- might be tagged. Otherwise we generate a declaration at the point of
449c4f58 12062 -- the attribute definition clause. If the attribute definition comes
12063 -- from an aspect specification the declaration is part of the freeze
12064 -- actions of the type.
44e4341e 12065
f15731c4 12066 function Build_Spec return Node_Id;
12067 -- Used for declaration and renaming declaration, so that this is
12068 -- treated as a renaming_as_body.
12069
12070 ----------------
12071 -- Build_Spec --
12072 ----------------
12073
d5b349fa 12074 function Build_Spec return Node_Id is
44e4341e 12075 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12076 Formals : List_Id;
12077 Spec : Node_Id;
83c6c069 12078 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
44e4341e 12079
f15731c4 12080 begin
9dfe12ae 12081 Subp_Id := Make_Defining_Identifier (Loc, Sname);
f15731c4 12082
44e4341e 12083 -- S : access Root_Stream_Type'Class
12084
12085 Formals := New_List (
12086 Make_Parameter_Specification (Loc,
12087 Defining_Identifier =>
12088 Make_Defining_Identifier (Loc, Name_S),
12089 Parameter_Type =>
12090 Make_Access_Definition (Loc,
12091 Subtype_Mark =>
83c6c069 12092 New_Occurrence_Of (
44e4341e 12093 Designated_Type (Etype (F)), Loc))));
12094
12095 if Nam = TSS_Stream_Input then
4bba0a8d 12096 Spec :=
12097 Make_Function_Specification (Loc,
12098 Defining_Unit_Name => Subp_Id,
12099 Parameter_Specifications => Formals,
12100 Result_Definition => T_Ref);
44e4341e 12101 else
12102 -- V : [out] T
f15731c4 12103
44e4341e 12104 Append_To (Formals,
12105 Make_Parameter_Specification (Loc,
12106 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12107 Out_Present => Out_P,
12108 Parameter_Type => T_Ref));
f15731c4 12109
d3ef794c 12110 Spec :=
12111 Make_Procedure_Specification (Loc,
12112 Defining_Unit_Name => Subp_Id,
12113 Parameter_Specifications => Formals);
44e4341e 12114 end if;
f15731c4 12115
44e4341e 12116 return Spec;
12117 end Build_Spec;
d6f39728 12118
44e4341e 12119 -- Start of processing for New_Stream_Subprogram
d6f39728 12120
44e4341e 12121 begin
12122 F := First_Formal (Subp);
12123
12124 if Ekind (Subp) = E_Procedure then
12125 Etyp := Etype (Next_Formal (F));
d6f39728 12126 else
44e4341e 12127 Etyp := Etype (Subp);
d6f39728 12128 end if;
f15731c4 12129
44e4341e 12130 -- Prepare subprogram declaration and insert it as an action on the
12131 -- clause node. The visibility for this entity is used to test for
12132 -- visibility of the attribute definition clause (in the sense of
12133 -- 8.3(23) as amended by AI-195).
9dfe12ae 12134
44e4341e 12135 if not Defer_Declaration then
f15731c4 12136 Subp_Decl :=
12137 Make_Subprogram_Declaration (Loc,
12138 Specification => Build_Spec);
44e4341e 12139
12140 -- For a tagged type, there is always a visible declaration for each
15ebb600 12141 -- stream TSS (it is a predefined primitive operation), and the
44e4341e 12142 -- completion of this declaration occurs at the freeze point, which is
12143 -- not always visible at places where the attribute definition clause is
12144 -- visible. So, we create a dummy entity here for the purpose of
12145 -- tracking the visibility of the attribute definition clause itself.
12146
12147 else
12148 Subp_Id :=
55868293 12149 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
44e4341e 12150 Subp_Decl :=
12151 Make_Object_Declaration (Loc,
12152 Defining_Identifier => Subp_Id,
12153 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
f15731c4 12154 end if;
12155
449c4f58 12156 if not Defer_Declaration
12157 and then From_Aspect_Specification (N)
12158 and then Has_Delayed_Freeze (Ent)
12159 then
12160 Append_Freeze_Action (Ent, Subp_Decl);
12161
12162 else
12163 Insert_Action (N, Subp_Decl);
12164 Set_Entity (N, Subp_Id);
12165 end if;
44e4341e 12166
d6f39728 12167 Subp_Decl :=
12168 Make_Subprogram_Renaming_Declaration (Loc,
f15731c4 12169 Specification => Build_Spec,
8acb75b4 12170 Name => New_Occurrence_Of (Subp, Loc));
d6f39728 12171
44e4341e 12172 if Defer_Declaration then
d6f39728 12173 Set_TSS (Base_Type (Ent), Subp_Id);
449c4f58 12174
d6f39728 12175 else
449c4f58 12176 if From_Aspect_Specification (N) then
12177 Append_Freeze_Action (Ent, Subp_Decl);
449c4f58 12178 else
12179 Insert_Action (N, Subp_Decl);
12180 end if;
12181
d6f39728 12182 Copy_TSS (Subp_Id, Base_Type (Ent));
12183 end if;
44e4341e 12184 end New_Stream_Subprogram;
d6f39728 12185
2625eb01 12186 ------------------------------------------
12187 -- Push_Scope_And_Install_Discriminants --
12188 ------------------------------------------
12189
12190 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12191 begin
12192 if Has_Discriminants (E) then
12193 Push_Scope (E);
12194
97c23bbe 12195 -- Make the discriminants visible for type declarations and protected
2625eb01 12196 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12197
12198 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12199 Install_Discriminants (E);
12200 end if;
12201 end if;
12202 end Push_Scope_And_Install_Discriminants;
12203
d6f39728 12204 ------------------------
12205 -- Rep_Item_Too_Early --
12206 ------------------------
12207
80d4fec4 12208 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
d6f39728 12209 begin
44e4341e 12210 -- Cannot apply non-operational rep items to generic types
d6f39728 12211
f15731c4 12212 if Is_Operational_Item (N) then
12213 return False;
12214
12215 elsif Is_Type (T)
d6f39728 12216 and then Is_Generic_Type (Root_Type (T))
e17c5076 12217 and then (Nkind (N) /= N_Pragma
12218 or else Get_Pragma_Id (N) /= Pragma_Convention)
d6f39728 12219 then
503f7fd3 12220 Error_Msg_N ("representation item not allowed for generic type", N);
d6f39728 12221 return True;
12222 end if;
12223
fdd294d1 12224 -- Otherwise check for incomplete type
d6f39728 12225
12226 if Is_Incomplete_Or_Private_Type (T)
12227 and then No (Underlying_Type (T))
d64221a7 12228 and then
12229 (Nkind (N) /= N_Pragma
60014bc9 12230 or else Get_Pragma_Id (N) /= Pragma_Import)
d6f39728 12231 then
12232 Error_Msg_N
12233 ("representation item must be after full type declaration", N);
12234 return True;
12235
1a34e48c 12236 -- If the type has incomplete components, a representation clause is
d6f39728 12237 -- illegal but stream attributes and Convention pragmas are correct.
12238
12239 elsif Has_Private_Component (T) then
f15731c4 12240 if Nkind (N) = N_Pragma then
d6f39728 12241 return False;
b9e61b2a 12242
d6f39728 12243 else
12244 Error_Msg_N
12245 ("representation item must appear after type is fully defined",
12246 N);
12247 return True;
12248 end if;
12249 else
12250 return False;
12251 end if;
12252 end Rep_Item_Too_Early;
12253
12254 -----------------------
12255 -- Rep_Item_Too_Late --
12256 -----------------------
12257
12258 function Rep_Item_Too_Late
12259 (T : Entity_Id;
12260 N : Node_Id;
d5b349fa 12261 FOnly : Boolean := False) return Boolean
d6f39728 12262 is
12263 S : Entity_Id;
12264 Parent_Type : Entity_Id;
12265
4d0944e9 12266 procedure No_Type_Rep_Item;
12267 -- Output message indicating that no type-related aspects can be
12268 -- specified due to some property of the parent type.
12269
d6f39728 12270 procedure Too_Late;
4d0944e9 12271 -- Output message for an aspect being specified too late
12272
12273 -- Note that neither of the above errors is considered a serious one,
12274 -- since the effect is simply that we ignore the representation clause
12275 -- in these cases.
04d38ee4 12276 -- Is this really true? In any case if we make this change we must
12277 -- document the requirement in the spec of Rep_Item_Too_Late that
12278 -- if True is returned, then the rep item must be completely ignored???
4d0944e9 12279
12280 ----------------------
12281 -- No_Type_Rep_Item --
12282 ----------------------
12283
12284 procedure No_Type_Rep_Item is
12285 begin
12286 Error_Msg_N ("|type-related representation item not permitted!", N);
12287 end No_Type_Rep_Item;
d53a018a 12288
12289 --------------
12290 -- Too_Late --
12291 --------------
d6f39728 12292
12293 procedure Too_Late is
12294 begin
ce4da1ed 12295 -- Other compilers seem more relaxed about rep items appearing too
12296 -- late. Since analysis tools typically don't care about rep items
12297 -- anyway, no reason to be too strict about this.
12298
a9cd517c 12299 if not Relaxed_RM_Semantics then
12300 Error_Msg_N ("|representation item appears too late!", N);
12301 end if;
d6f39728 12302 end Too_Late;
12303
12304 -- Start of processing for Rep_Item_Too_Late
12305
12306 begin
a3248fc4 12307 -- First make sure entity is not frozen (RM 13.1(9))
d6f39728 12308
12309 if Is_Frozen (T)
a3248fc4 12310
12311 -- Exclude imported types, which may be frozen if they appear in a
12312 -- representation clause for a local type.
12313
4aa270d8 12314 and then not From_Limited_With (T)
a3248fc4 12315
a9cd517c 12316 -- Exclude generated entities (not coming from source). The common
a3248fc4 12317 -- case is when we generate a renaming which prematurely freezes the
12318 -- renamed internal entity, but we still want to be able to set copies
12319 -- of attribute values such as Size/Alignment.
12320
12321 and then Comes_From_Source (T)
d6f39728 12322 then
58e133a6 12323 -- A self-referential aspect is illegal if it forces freezing the
12324 -- entity before the corresponding pragma has been analyzed.
12325
12326 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12327 and then From_Aspect_Specification (N)
12328 then
12329 Error_Msg_NE
ea90be0f 12330 ("aspect specification causes premature freezing of&", N, T);
58e133a6 12331 Set_Has_Delayed_Freeze (T, False);
12332 return True;
12333 end if;
12334
d6f39728 12335 Too_Late;
12336 S := First_Subtype (T);
12337
12338 if Present (Freeze_Node (S)) then
04d38ee4 12339 if not Relaxed_RM_Semantics then
12340 Error_Msg_NE
12341 ("??no more representation items for }", Freeze_Node (S), S);
12342 end if;
d6f39728 12343 end if;
12344
12345 return True;
12346
d1a2e31b 12347 -- Check for case of untagged derived type whose parent either has
4d0944e9 12348 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12349 -- this case we do not output a Too_Late message, since there is no
12350 -- earlier point where the rep item could be placed to make it legal.
d6f39728 12351
12352 elsif Is_Type (T)
12353 and then not FOnly
12354 and then Is_Derived_Type (T)
12355 and then not Is_Tagged_Type (T)
12356 then
12357 Parent_Type := Etype (Base_Type (T));
12358
12359 if Has_Primitive_Operations (Parent_Type) then
4d0944e9 12360 No_Type_Rep_Item;
04d38ee4 12361
12362 if not Relaxed_RM_Semantics then
12363 Error_Msg_NE
12364 ("\parent type & has primitive operations!", N, Parent_Type);
12365 end if;
12366
d6f39728 12367 return True;
12368
12369 elsif Is_By_Reference_Type (Parent_Type) then
4d0944e9 12370 No_Type_Rep_Item;
04d38ee4 12371
12372 if not Relaxed_RM_Semantics then
12373 Error_Msg_NE
12374 ("\parent type & is a by reference type!", N, Parent_Type);
12375 end if;
12376
d6f39728 12377 return True;
12378 end if;
12379 end if;
12380
04d38ee4 12381 -- No error, but one more warning to consider. The RM (surprisingly)
12382 -- allows this pattern:
12383
12384 -- type S is ...
12385 -- primitive operations for S
12386 -- type R is new S;
12387 -- rep clause for S
12388
12389 -- Meaning that calls on the primitive operations of S for values of
12390 -- type R may require possibly expensive implicit conversion operations.
12391 -- This is not an error, but is worth a warning.
12392
12393 if not Relaxed_RM_Semantics and then Is_Type (T) then
12394 declare
12395 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12396
12397 begin
12398 if Present (DTL)
12399 and then Has_Primitive_Operations (Base_Type (T))
12400
12401 -- For now, do not generate this warning for the case of aspect
12402 -- specification using Ada 2012 syntax, since we get wrong
12403 -- messages we do not understand. The whole business of derived
12404 -- types and rep items seems a bit confused when aspects are
12405 -- used, since the aspects are not evaluated till freeze time.
12406
12407 and then not From_Aspect_Specification (N)
12408 then
12409 Error_Msg_Sloc := Sloc (DTL);
12410 Error_Msg_N
12411 ("representation item for& appears after derived type "
12412 & "declaration#??", N);
12413 Error_Msg_NE
12414 ("\may result in implicit conversions for primitive "
12415 & "operations of&??", N, T);
12416 Error_Msg_NE
12417 ("\to change representations when called with arguments "
12418 & "of type&??", N, DTL);
12419 end if;
12420 end;
12421 end if;
12422
3062c401 12423 -- No error, link item into head of chain of rep items for the entity,
12424 -- but avoid chaining if we have an overloadable entity, and the pragma
12425 -- is one that can apply to multiple overloaded entities.
12426
b9e61b2a 12427 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
fdd294d1 12428 declare
ddccc924 12429 Pname : constant Name_Id := Pragma_Name (N);
fdd294d1 12430 begin
18393965 12431 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12432 Name_External, Name_Interface)
fdd294d1 12433 then
12434 return False;
12435 end if;
12436 end;
3062c401 12437 end if;
12438
fdd294d1 12439 Record_Rep_Item (T, N);
d6f39728 12440 return False;
12441 end Rep_Item_Too_Late;
12442
2072eaa9 12443 -------------------------------------
12444 -- Replace_Type_References_Generic --
12445 -------------------------------------
12446
37c6552c 12447 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12448 TName : constant Name_Id := Chars (T);
2072eaa9 12449
97c23bbe 12450 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
2072eaa9 12451 -- Processes a single node in the traversal procedure below, checking
12452 -- if node N should be replaced, and if so, doing the replacement.
12453
d0931270 12454 function Visible_Component (Comp : Name_Id) return Entity_Id;
12455 -- Given an identifier in the expression, check whether there is a
12456 -- discriminant or component of the type that is directy visible, and
12457 -- rewrite it as the corresponding selected component of the formal of
12458 -- the subprogram. The entity is located by a sequential search, which
12459 -- seems acceptable given the typical size of component lists and check
12460 -- expressions. Possible optimization ???
12461
97c23bbe 12462 ----------------------
12463 -- Replace_Type_Ref --
12464 ----------------------
2072eaa9 12465
97c23bbe 12466 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
d0931270 12467 Loc : constant Source_Ptr := Sloc (N);
2072eaa9 12468
d0931270 12469 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
77fd9c7a 12470 -- Add the proper prefix to a reference to a component of the type
12471 -- when it is not already a selected component.
d0931270 12472
12473 ----------------
12474 -- Add_Prefix --
12475 ----------------
2072eaa9 12476
d0931270 12477 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12478 begin
12479 Rewrite (Ref,
12480 Make_Selected_Component (Loc,
77fd9c7a 12481 Prefix => New_Occurrence_Of (T, Loc),
d0931270 12482 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12483 Replace_Type_Reference (Prefix (Ref));
12484 end Add_Prefix;
12485
77fd9c7a 12486 -- Local variables
12487
12488 Comp : Entity_Id;
12489 Pref : Node_Id;
12490 Scop : Entity_Id;
12491
d0931270 12492 -- Start of processing for Replace_Type_Ref
12493
12494 begin
2072eaa9 12495 if Nkind (N) = N_Identifier then
12496
97c23bbe 12497 -- If not the type name, check whether it is a reference to some
12498 -- other type, which must be frozen before the predicate function
12499 -- is analyzed, i.e. before the freeze node of the type to which
12500 -- the predicate applies.
2072eaa9 12501
12502 if Chars (N) /= TName then
37c6552c 12503 if Present (Current_Entity (N))
46532462 12504 and then Is_Type (Current_Entity (N))
37c6552c 12505 then
12506 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12507 end if;
12508
d0931270 12509 -- The components of the type are directly visible and can
12510 -- be referenced without a prefix.
12511
12512 if Nkind (Parent (N)) = N_Selected_Component then
12513 null;
12514
12515 -- In expression C (I), C may be a directly visible function
12516 -- or a visible component that has an array type. Disambiguate
12517 -- by examining the component type.
12518
12519 elsif Nkind (Parent (N)) = N_Indexed_Component
12520 and then N = Prefix (Parent (N))
12521 then
77fd9c7a 12522 Comp := Visible_Component (Chars (N));
d0931270 12523
77fd9c7a 12524 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
12525 Add_Prefix (N, Comp);
d0931270 12526 end if;
12527
12528 else
77fd9c7a 12529 Comp := Visible_Component (Chars (N));
d0931270 12530
77fd9c7a 12531 if Present (Comp) then
12532 Add_Prefix (N, Comp);
d0931270 12533 end if;
12534 end if;
12535
2072eaa9 12536 return Skip;
12537
12538 -- Otherwise do the replacement and we are done with this node
12539
12540 else
12541 Replace_Type_Reference (N);
12542 return Skip;
12543 end if;
12544
97c23bbe 12545 -- Case of selected component (which is what a qualification looks
12546 -- like in the unanalyzed tree, which is what we have.
2072eaa9 12547
12548 elsif Nkind (N) = N_Selected_Component then
12549
97c23bbe 12550 -- If selector name is not our type, keeping going (we might still
12551 -- have an occurrence of the type in the prefix).
2072eaa9 12552
12553 if Nkind (Selector_Name (N)) /= N_Identifier
12554 or else Chars (Selector_Name (N)) /= TName
12555 then
12556 return OK;
12557
12558 -- Selector name is our type, check qualification
12559
12560 else
12561 -- Loop through scopes and prefixes, doing comparison
12562
77fd9c7a 12563 Scop := Current_Scope;
12564 Pref := Prefix (N);
2072eaa9 12565 loop
12566 -- Continue if no more scopes or scope with no name
12567
77fd9c7a 12568 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
2072eaa9 12569 return OK;
12570 end if;
12571
97c23bbe 12572 -- Do replace if prefix is an identifier matching the scope
12573 -- that we are currently looking at.
2072eaa9 12574
77fd9c7a 12575 if Nkind (Pref) = N_Identifier
12576 and then Chars (Pref) = Chars (Scop)
2072eaa9 12577 then
12578 Replace_Type_Reference (N);
12579 return Skip;
12580 end if;
12581
97c23bbe 12582 -- Go check scope above us if prefix is itself of the form
12583 -- of a selected component, whose selector matches the scope
12584 -- we are currently looking at.
2072eaa9 12585
77fd9c7a 12586 if Nkind (Pref) = N_Selected_Component
12587 and then Nkind (Selector_Name (Pref)) = N_Identifier
12588 and then Chars (Selector_Name (Pref)) = Chars (Scop)
2072eaa9 12589 then
77fd9c7a 12590 Scop := Scope (Scop);
12591 Pref := Prefix (Pref);
2072eaa9 12592
12593 -- For anything else, we don't have a match, so keep on
12594 -- going, there are still some weird cases where we may
12595 -- still have a replacement within the prefix.
12596
12597 else
12598 return OK;
12599 end if;
12600 end loop;
12601 end if;
12602
ec6f6da5 12603 -- Continue for any other node kind
2072eaa9 12604
12605 else
12606 return OK;
12607 end if;
97c23bbe 12608 end Replace_Type_Ref;
12609
77fd9c7a 12610 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
12611
d0931270 12612 -----------------------
12613 -- Visible_Component --
12614 -----------------------
12615
12616 function Visible_Component (Comp : Name_Id) return Entity_Id is
12617 E : Entity_Id;
77fd9c7a 12618
d0931270 12619 begin
1a5b3a27 12620 -- Types with nameable components are records and discriminated
12621 -- private types.
12622
12623 if Ekind (T) = E_Record_Type
12624 or else (Is_Private_Type (T) and then Has_Discriminants (T))
12625 then
d0931270 12626 E := First_Entity (T);
12627 while Present (E) loop
77fd9c7a 12628 if Comes_From_Source (E) and then Chars (E) = Comp then
d0931270 12629 return E;
12630 end if;
12631
12632 Next_Entity (E);
12633 end loop;
d0931270 12634 end if;
1a5b3a27 12635
b58a7126 12636 -- Nothing by that name, or the type has no components
1a5b3a27 12637
12638 return Empty;
d0931270 12639 end Visible_Component;
12640
77fd9c7a 12641 -- Start of processing for Replace_Type_References_Generic
2072eaa9 12642
12643 begin
12644 Replace_Type_Refs (N);
12645 end Replace_Type_References_Generic;
12646
81bd1c0d 12647 --------------------------------
12648 -- Resolve_Aspect_Expressions --
12649 --------------------------------
12650
12651 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
81bd1c0d 12652
9c20237a 12653 function Resolve_Name (N : Node_Id) return Traverse_Result;
12654 -- Verify that all identifiers in the expression, with the exception
12655 -- of references to the current entity, denote visible entities. This
12656 -- is done only to detect visibility errors, as the expression will be
12657 -- properly analyzed/expanded during analysis of the predicate function
c098acfb 12658 -- body. We omit quantified expressions from this test, given that they
12659 -- introduce a local identifier that would require proper expansion to
12660 -- handle properly.
9c20237a 12661
25e4fa47 12662 -- In ASIS_Mode we preserve the entity in the source because there is
12663 -- no subsequent expansion to decorate the tree.
12664
9c20237a 12665 ------------------
12666 -- Resolve_Name --
12667 ------------------
12668
12669 function Resolve_Name (N : Node_Id) return Traverse_Result is
12670 begin
12671 if Nkind (N) = N_Selected_Component then
12672 if Nkind (Prefix (N)) = N_Identifier
12673 and then Chars (Prefix (N)) /= Chars (E)
12674 then
f4e18891 12675 Find_Selected_Component (N);
9c20237a 12676 end if;
02e5d0d0 12677
9c20237a 12678 return Skip;
12679
02e5d0d0 12680 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
9c20237a 12681 Find_Direct_Name (N);
25e4fa47 12682
6b1f5205 12683 if not ASIS_Mode then
25e4fa47 12684 Set_Entity (N, Empty);
12685 end if;
c098acfb 12686
12687 elsif Nkind (N) = N_Quantified_Expression then
12688 return Skip;
9c20237a 12689 end if;
12690
12691 return OK;
12692 end Resolve_Name;
12693
12694 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12695
bfed3e04 12696 ASN : Node_Id := First_Rep_Item (E);
12697
02e5d0d0 12698 -- Start of processing for Resolve_Aspect_Expressions
12699
81bd1c0d 12700 begin
bfed3e04 12701 -- Need to make sure discriminants, if any, are directly visible
81bd1c0d 12702
bfed3e04 12703 Push_Scope_And_Install_Discriminants (E);
97c23bbe 12704
bfed3e04 12705 while Present (ASN) loop
12706 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12707 declare
12708 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
12709 Expr : constant Node_Id := Expression (ASN);
5535eed4 12710
bfed3e04 12711 begin
12712 case A_Id is
5535eed4 12713
bfed3e04 12714 -- For now we only deal with aspects that do not generate
12715 -- subprograms, or that may mention current instances of
12716 -- types. These will require special handling (???TBD).
81bd1c0d 12717
bfed3e04 12718 when Aspect_Invariant
12719 | Aspect_Predicate
12720 | Aspect_Predicate_Failure
12721 =>
12722 null;
81bd1c0d 12723
bfed3e04 12724 when Aspect_Dynamic_Predicate
12725 | Aspect_Static_Predicate
12726 =>
12727 -- Build predicate function specification and preanalyze
12728 -- expression after type replacement.
9c20237a 12729
bfed3e04 12730 if No (Predicate_Function (E)) then
12731 declare
12732 FDecl : constant Node_Id :=
12733 Build_Predicate_Function_Declaration (E);
12734 pragma Unreferenced (FDecl);
12735 begin
12736 Resolve_Aspect_Expression (Expr);
12737 end;
12738 end if;
9c20237a 12739
bfed3e04 12740 when Pre_Post_Aspects =>
12741 null;
81bd1c0d 12742
bfed3e04 12743 when Aspect_Iterable =>
12744 if Nkind (Expr) = N_Aggregate then
12745 declare
12746 Assoc : Node_Id;
81bd1c0d 12747
bfed3e04 12748 begin
12749 Assoc := First (Component_Associations (Expr));
12750 while Present (Assoc) loop
12751 Find_Direct_Name (Expression (Assoc));
12752 Next (Assoc);
12753 end loop;
12754 end;
12755 end if;
81bd1c0d 12756
bfed3e04 12757 when others =>
12758 if Present (Expr) then
12759 case Aspect_Argument (A_Id) is
12760 when Expression
12761 | Optional_Expression
12762 =>
12763 Analyze_And_Resolve (Expr);
12764
12765 when Name
12766 | Optional_Name
12767 =>
12768 if Nkind (Expr) = N_Identifier then
12769 Find_Direct_Name (Expr);
12770
12771 elsif Nkind (Expr) = N_Selected_Component then
12772 Find_Selected_Component (Expr);
12773 end if;
12774 end case;
12775 end if;
12776 end case;
12777 end;
81bd1c0d 12778 end if;
12779
a738763e 12780 ASN := Next_Rep_Item (ASN);
81bd1c0d 12781 end loop;
bfed3e04 12782
12783 Uninstall_Discriminants_And_Pop_Scope (E);
81bd1c0d 12784 end Resolve_Aspect_Expressions;
12785
d6f39728 12786 -------------------------
12787 -- Same_Representation --
12788 -------------------------
12789
12790 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12791 T1 : constant Entity_Id := Underlying_Type (Typ1);
12792 T2 : constant Entity_Id := Underlying_Type (Typ2);
12793
12794 begin
12795 -- A quick check, if base types are the same, then we definitely have
12796 -- the same representation, because the subtype specific representation
12797 -- attributes (Size and Alignment) do not affect representation from
12798 -- the point of view of this test.
12799
12800 if Base_Type (T1) = Base_Type (T2) then
12801 return True;
12802
12803 elsif Is_Private_Type (Base_Type (T2))
12804 and then Base_Type (T1) = Full_View (Base_Type (T2))
12805 then
12806 return True;
12807 end if;
12808
3645e9c5 12809 -- Tagged types always have the same representation, because it is not
12810 -- possible to specify different representations for common fields.
d6f39728 12811
12812 if Is_Tagged_Type (T1) then
12813 return True;
12814 end if;
12815
12816 -- Representations are definitely different if conventions differ
12817
12818 if Convention (T1) /= Convention (T2) then
12819 return False;
12820 end if;
12821
ef0772bc 12822 -- Representations are different if component alignments or scalar
12823 -- storage orders differ.
d6f39728 12824
12825 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
726fd56a 12826 and then
d6f39728 12827 (Is_Record_Type (T2) or else Is_Array_Type (T2))
ef0772bc 12828 and then
12829 (Component_Alignment (T1) /= Component_Alignment (T2)
f02a9a9a 12830 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
d6f39728 12831 then
12832 return False;
12833 end if;
12834
12835 -- For arrays, the only real issue is component size. If we know the
12836 -- component size for both arrays, and it is the same, then that's
12837 -- good enough to know we don't have a change of representation.
12838
12839 if Is_Array_Type (T1) then
12840 if Known_Component_Size (T1)
12841 and then Known_Component_Size (T2)
12842 and then Component_Size (T1) = Component_Size (T2)
12843 then
36ac5fbb 12844 return True;
d6f39728 12845 end if;
12846 end if;
12847
3645e9c5 12848 -- For records, representations are different if reorderings differ
12849
12850 if Is_Record_Type (T1)
12851 and then Is_Record_Type (T2)
12852 and then No_Reordering (T1) /= No_Reordering (T2)
12853 then
12854 return False;
12855 end if;
12856
d6f39728 12857 -- Types definitely have same representation if neither has non-standard
12858 -- representation since default representations are always consistent.
12859 -- If only one has non-standard representation, and the other does not,
12860 -- then we consider that they do not have the same representation. They
12861 -- might, but there is no way of telling early enough.
12862
12863 if Has_Non_Standard_Rep (T1) then
12864 if not Has_Non_Standard_Rep (T2) then
12865 return False;
12866 end if;
12867 else
12868 return not Has_Non_Standard_Rep (T2);
12869 end if;
12870
fdd294d1 12871 -- Here the two types both have non-standard representation, and we need
12872 -- to determine if they have the same non-standard representation.
d6f39728 12873
12874 -- For arrays, we simply need to test if the component sizes are the
12875 -- same. Pragma Pack is reflected in modified component sizes, so this
12876 -- check also deals with pragma Pack.
12877
12878 if Is_Array_Type (T1) then
12879 return Component_Size (T1) = Component_Size (T2);
12880
d6f39728 12881 -- Case of record types
12882
12883 elsif Is_Record_Type (T1) then
12884
12885 -- Packed status must conform
12886
12887 if Is_Packed (T1) /= Is_Packed (T2) then
12888 return False;
12889
12890 -- Otherwise we must check components. Typ2 maybe a constrained
12891 -- subtype with fewer components, so we compare the components
12892 -- of the base types.
12893
12894 else
12895 Record_Case : declare
12896 CD1, CD2 : Entity_Id;
12897
12898 function Same_Rep return Boolean;
12899 -- CD1 and CD2 are either components or discriminants. This
ef0772bc 12900 -- function tests whether they have the same representation.
d6f39728 12901
80d4fec4 12902 --------------
12903 -- Same_Rep --
12904 --------------
12905
d6f39728 12906 function Same_Rep return Boolean is
12907 begin
12908 if No (Component_Clause (CD1)) then
12909 return No (Component_Clause (CD2));
d6f39728 12910 else
ef0772bc 12911 -- Note: at this point, component clauses have been
12912 -- normalized to the default bit order, so that the
12913 -- comparison of Component_Bit_Offsets is meaningful.
12914
d6f39728 12915 return
12916 Present (Component_Clause (CD2))
12917 and then
12918 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12919 and then
12920 Esize (CD1) = Esize (CD2);
12921 end if;
12922 end Same_Rep;
12923
1e35409d 12924 -- Start of processing for Record_Case
d6f39728 12925
12926 begin
12927 if Has_Discriminants (T1) then
d6f39728 12928
9dfe12ae 12929 -- The number of discriminants may be different if the
12930 -- derived type has fewer (constrained by values). The
12931 -- invisible discriminants retain the representation of
12932 -- the original, so the discrepancy does not per se
12933 -- indicate a different representation.
12934
b9e61b2a 12935 CD1 := First_Discriminant (T1);
12936 CD2 := First_Discriminant (T2);
12937 while Present (CD1) and then Present (CD2) loop
d6f39728 12938 if not Same_Rep then
12939 return False;
12940 else
12941 Next_Discriminant (CD1);
12942 Next_Discriminant (CD2);
12943 end if;
12944 end loop;
12945 end if;
12946
12947 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12948 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
d6f39728 12949 while Present (CD1) loop
12950 if not Same_Rep then
12951 return False;
12952 else
12953 Next_Component (CD1);
12954 Next_Component (CD2);
12955 end if;
12956 end loop;
12957
12958 return True;
12959 end Record_Case;
12960 end if;
12961
12962 -- For enumeration types, we must check each literal to see if the
12963 -- representation is the same. Note that we do not permit enumeration
1a34e48c 12964 -- representation clauses for Character and Wide_Character, so these
d6f39728 12965 -- cases were already dealt with.
12966
12967 elsif Is_Enumeration_Type (T1) then
d6f39728 12968 Enumeration_Case : declare
12969 L1, L2 : Entity_Id;
12970
12971 begin
12972 L1 := First_Literal (T1);
12973 L2 := First_Literal (T2);
d6f39728 12974 while Present (L1) loop
12975 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12976 return False;
12977 else
12978 Next_Literal (L1);
12979 Next_Literal (L2);
12980 end if;
12981 end loop;
12982
12983 return True;
d6f39728 12984 end Enumeration_Case;
12985
12986 -- Any other types have the same representation for these purposes
12987
12988 else
12989 return True;
12990 end if;
d6f39728 12991 end Same_Representation;
12992
3061ffde 12993 --------------------------------
12994 -- Resolve_Iterable_Operation --
12995 --------------------------------
12996
12997 procedure Resolve_Iterable_Operation
12998 (N : Node_Id;
12999 Cursor : Entity_Id;
13000 Typ : Entity_Id;
13001 Nam : Name_Id)
13002 is
13003 Ent : Entity_Id;
13004 F1 : Entity_Id;
13005 F2 : Entity_Id;
13006
13007 begin
13008 if not Is_Overloaded (N) then
13009 if not Is_Entity_Name (N)
13010 or else Ekind (Entity (N)) /= E_Function
13011 or else Scope (Entity (N)) /= Scope (Typ)
13012 or else No (First_Formal (Entity (N)))
13013 or else Etype (First_Formal (Entity (N))) /= Typ
13014 then
13015 Error_Msg_N ("iterable primitive must be local function name "
13016 & "whose first formal is an iterable type", N);
a9f5fea7 13017 return;
3061ffde 13018 end if;
13019
13020 Ent := Entity (N);
13021 F1 := First_Formal (Ent);
13022 if Nam = Name_First then
13023
13024 -- First (Container) => Cursor
13025
13026 if Etype (Ent) /= Cursor then
13027 Error_Msg_N ("primitive for First must yield a curosr", N);
13028 end if;
13029
13030 elsif Nam = Name_Next then
13031
13032 -- Next (Container, Cursor) => Cursor
13033
13034 F2 := Next_Formal (F1);
13035
13036 if Etype (F2) /= Cursor
13037 or else Etype (Ent) /= Cursor
13038 or else Present (Next_Formal (F2))
13039 then
13040 Error_Msg_N ("no match for Next iterable primitive", N);
13041 end if;
13042
13043 elsif Nam = Name_Has_Element then
13044
13045 -- Has_Element (Container, Cursor) => Boolean
13046
13047 F2 := Next_Formal (F1);
13048 if Etype (F2) /= Cursor
13049 or else Etype (Ent) /= Standard_Boolean
13050 or else Present (Next_Formal (F2))
13051 then
13052 Error_Msg_N ("no match for Has_Element iterable primitive", N);
13053 end if;
13054
13055 elsif Nam = Name_Element then
b9b03799 13056 F2 := Next_Formal (F1);
13057
13058 if No (F2)
13059 or else Etype (F2) /= Cursor
13060 or else Present (Next_Formal (F2))
13061 then
13062 Error_Msg_N ("no match for Element iterable primitive", N);
13063 end if;
3061ffde 13064 null;
13065
13066 else
13067 raise Program_Error;
13068 end if;
13069
13070 else
13071 -- Overloaded case: find subprogram with proper signature.
13072 -- Caller will report error if no match is found.
13073
13074 declare
13075 I : Interp_Index;
13076 It : Interp;
13077
13078 begin
13079 Get_First_Interp (N, I, It);
13080 while Present (It.Typ) loop
13081 if Ekind (It.Nam) = E_Function
b9b03799 13082 and then Scope (It.Nam) = Scope (Typ)
3061ffde 13083 and then Etype (First_Formal (It.Nam)) = Typ
13084 then
13085 F1 := First_Formal (It.Nam);
13086
13087 if Nam = Name_First then
13088 if Etype (It.Nam) = Cursor
13089 and then No (Next_Formal (F1))
13090 then
13091 Set_Entity (N, It.Nam);
13092 exit;
13093 end if;
13094
13095 elsif Nam = Name_Next then
13096 F2 := Next_Formal (F1);
13097
13098 if Present (F2)
13099 and then No (Next_Formal (F2))
13100 and then Etype (F2) = Cursor
13101 and then Etype (It.Nam) = Cursor
13102 then
13103 Set_Entity (N, It.Nam);
13104 exit;
13105 end if;
13106
13107 elsif Nam = Name_Has_Element then
13108 F2 := Next_Formal (F1);
13109
13110 if Present (F2)
13111 and then No (Next_Formal (F2))
13112 and then Etype (F2) = Cursor
13113 and then Etype (It.Nam) = Standard_Boolean
13114 then
13115 Set_Entity (N, It.Nam);
13116 F2 := Next_Formal (F1);
13117 exit;
13118 end if;
13119
13120 elsif Nam = Name_Element then
b9b03799 13121 F2 := Next_Formal (F1);
13122
3061ffde 13123 if Present (F2)
13124 and then No (Next_Formal (F2))
13125 and then Etype (F2) = Cursor
13126 then
13127 Set_Entity (N, It.Nam);
13128 exit;
13129 end if;
13130 end if;
13131 end if;
13132
13133 Get_Next_Interp (I, It);
13134 end loop;
13135 end;
13136 end if;
13137 end Resolve_Iterable_Operation;
13138
b77e4501 13139 ----------------
13140 -- Set_Biased --
13141 ----------------
13142
13143 procedure Set_Biased
13144 (E : Entity_Id;
13145 N : Node_Id;
13146 Msg : String;
13147 Biased : Boolean := True)
13148 is
13149 begin
13150 if Biased then
13151 Set_Has_Biased_Representation (E);
13152
13153 if Warn_On_Biased_Representation then
13154 Error_Msg_NE
1e3532e7 13155 ("?B?" & Msg & " forces biased representation for&", N, E);
b77e4501 13156 end if;
13157 end if;
13158 end Set_Biased;
13159
d6f39728 13160 --------------------
13161 -- Set_Enum_Esize --
13162 --------------------
13163
13164 procedure Set_Enum_Esize (T : Entity_Id) is
13165 Lo : Uint;
13166 Hi : Uint;
13167 Sz : Nat;
13168
13169 begin
13170 Init_Alignment (T);
13171
13172 -- Find the minimum standard size (8,16,32,64) that fits
13173
13174 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13175 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13176
13177 if Lo < 0 then
13178 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
f15731c4 13179 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 13180
13181 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13182 Sz := 16;
13183
13184 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13185 Sz := 32;
13186
13187 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13188 Sz := 64;
13189 end if;
13190
13191 else
13192 if Hi < Uint_2**08 then
f15731c4 13193 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 13194
13195 elsif Hi < Uint_2**16 then
13196 Sz := 16;
13197
13198 elsif Hi < Uint_2**32 then
13199 Sz := 32;
13200
13201 else pragma Assert (Hi < Uint_2**63);
13202 Sz := 64;
13203 end if;
13204 end if;
13205
13206 -- That minimum is the proper size unless we have a foreign convention
13207 -- and the size required is 32 or less, in which case we bump the size
13208 -- up to 32. This is required for C and C++ and seems reasonable for
13209 -- all other foreign conventions.
13210
13211 if Has_Foreign_Convention (T)
13212 and then Esize (T) < Standard_Integer_Size
db1eed69 13213
13214 -- Don't do this if Short_Enums on target
13215
e9185b9d 13216 and then not Target_Short_Enums
d6f39728 13217 then
13218 Init_Esize (T, Standard_Integer_Size);
d6f39728 13219 else
13220 Init_Esize (T, Sz);
13221 end if;
d6f39728 13222 end Set_Enum_Esize;
13223
2625eb01 13224 -----------------------------
13225 -- Uninstall_Discriminants --
13226 -----------------------------
13227
13228 procedure Uninstall_Discriminants (E : Entity_Id) is
13229 Disc : Entity_Id;
13230 Prev : Entity_Id;
13231 Outer : Entity_Id;
13232
13233 begin
13234 -- Discriminants have been made visible for type declarations and
13235 -- protected type declarations, not for subtype declarations.
13236
13237 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13238 Disc := First_Discriminant (E);
13239 while Present (Disc) loop
13240 if Disc /= Current_Entity (Disc) then
13241 Prev := Current_Entity (Disc);
13242 while Present (Prev)
13243 and then Present (Homonym (Prev))
13244 and then Homonym (Prev) /= Disc
13245 loop
13246 Prev := Homonym (Prev);
13247 end loop;
13248 else
13249 Prev := Empty;
13250 end if;
13251
13252 Set_Is_Immediately_Visible (Disc, False);
13253
13254 Outer := Homonym (Disc);
13255 while Present (Outer) and then Scope (Outer) = E loop
13256 Outer := Homonym (Outer);
13257 end loop;
13258
13259 -- Reset homonym link of other entities, but do not modify link
3ff5e35d 13260 -- between entities in current scope, so that the back end can
2625eb01 13261 -- have a proper count of local overloadings.
13262
13263 if No (Prev) then
13264 Set_Name_Entity_Id (Chars (Disc), Outer);
13265
13266 elsif Scope (Prev) /= Scope (Disc) then
13267 Set_Homonym (Prev, Outer);
13268 end if;
13269
13270 Next_Discriminant (Disc);
13271 end loop;
13272 end if;
13273 end Uninstall_Discriminants;
13274
13275 -------------------------------------------
13276 -- Uninstall_Discriminants_And_Pop_Scope --
13277 -------------------------------------------
13278
13279 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13280 begin
13281 if Has_Discriminants (E) then
13282 Uninstall_Discriminants (E);
13283 Pop_Scope;
13284 end if;
13285 end Uninstall_Discriminants_And_Pop_Scope;
13286
83f8f0a6 13287 ------------------------------
13288 -- Validate_Address_Clauses --
13289 ------------------------------
13290
13291 procedure Validate_Address_Clauses is
c7a1569a 13292 function Offset_Value (Expr : Node_Id) return Uint;
13293 -- Given an Address attribute reference, return the value in bits of its
13294 -- offset from the first bit of the underlying entity, or 0 if it is not
13295 -- known at compile time.
13296
13297 ------------------
13298 -- Offset_Value --
13299 ------------------
13300
13301 function Offset_Value (Expr : Node_Id) return Uint is
13302 N : Node_Id := Prefix (Expr);
13303 Off : Uint;
13304 Val : Uint := Uint_0;
13305
13306 begin
13307 -- Climb the prefix chain and compute the cumulative offset
13308
13309 loop
13310 if Is_Entity_Name (N) then
13311 return Val;
13312
13313 elsif Nkind (N) = N_Selected_Component then
13314 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13315 if Off /= No_Uint and then Off >= Uint_0 then
13316 Val := Val + Off;
13317 N := Prefix (N);
13318 else
13319 return Uint_0;
13320 end if;
13321
13322 elsif Nkind (N) = N_Indexed_Component then
13323 Off := Indexed_Component_Bit_Offset (N);
13324 if Off /= No_Uint then
13325 Val := Val + Off;
13326 N := Prefix (N);
13327 else
13328 return Uint_0;
13329 end if;
13330
13331 else
13332 return Uint_0;
13333 end if;
13334 end loop;
13335 end Offset_Value;
13336
13337 -- Start of processing for Validate_Address_Clauses
13338
83f8f0a6 13339 begin
13340 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13341 declare
13342 ACCR : Address_Clause_Check_Record
13343 renames Address_Clause_Checks.Table (J);
13344
d6da7448 13345 Expr : Node_Id;
13346
83f8f0a6 13347 X_Alignment : Uint;
f907cb13 13348 Y_Alignment : Uint := Uint_0;
83f8f0a6 13349
13350 X_Size : Uint;
f907cb13 13351 Y_Size : Uint := Uint_0;
83f8f0a6 13352
c7a1569a 13353 X_Offs : Uint;
13354
83f8f0a6 13355 begin
13356 -- Skip processing of this entry if warning already posted
13357
13358 if not Address_Warning_Posted (ACCR.N) then
d6da7448 13359 Expr := Original_Node (Expression (ACCR.N));
83f8f0a6 13360
514a5555 13361 -- Get alignments, sizes and offset, if any
83f8f0a6 13362
d6da7448 13363 X_Alignment := Alignment (ACCR.X);
8650387e 13364 X_Size := Esize (ACCR.X);
514a5555 13365
13366 if Present (ACCR.Y) then
13367 Y_Alignment := Alignment (ACCR.Y);
8650387e 13368 Y_Size := Esize (ACCR.Y);
514a5555 13369 end if;
83f8f0a6 13370
c7a1569a 13371 if ACCR.Off
13372 and then Nkind (Expr) = N_Attribute_Reference
13373 and then Attribute_Name (Expr) = Name_Address
13374 then
13375 X_Offs := Offset_Value (Expr);
13376 else
13377 X_Offs := Uint_0;
13378 end if;
13379
514a5555 13380 -- Check for known value not multiple of alignment
13381
13382 if No (ACCR.Y) then
13383 if not Alignment_Checks_Suppressed (ACCR.X)
13384 and then X_Alignment /= 0
13385 and then ACCR.A mod X_Alignment /= 0
13386 then
13387 Error_Msg_NE
13388 ("??specified address for& is inconsistent with "
13389 & "alignment", ACCR.N, ACCR.X);
13390 Error_Msg_N
13391 ("\??program execution may be erroneous (RM 13.3(27))",
13392 ACCR.N);
13393
13394 Error_Msg_Uint_1 := X_Alignment;
13395 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13396 end if;
13397
83f8f0a6 13398 -- Check for large object overlaying smaller one
13399
514a5555 13400 elsif Y_Size > Uint_0
83f8f0a6 13401 and then X_Size > Uint_0
c7a1569a 13402 and then X_Offs + X_Size > Y_Size
83f8f0a6 13403 then
7161e166 13404 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
83f8f0a6 13405 Error_Msg_N
1e3532e7 13406 ("\??program execution may be erroneous", ACCR.N);
7161e166 13407
83f8f0a6 13408 Error_Msg_Uint_1 := X_Size;
7161e166 13409 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13410
83f8f0a6 13411 Error_Msg_Uint_1 := Y_Size;
7161e166 13412 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
83f8f0a6 13413
f5cc2579 13414 if Y_Size >= X_Size then
c7a1569a 13415 Error_Msg_Uint_1 := X_Offs;
f5cc2579 13416 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
c7a1569a 13417 end if;
13418
d6da7448 13419 -- Check for inadequate alignment, both of the base object
e556831e 13420 -- and of the offset, if any. We only do this check if the
13421 -- run-time Alignment_Check is active. No point in warning
13422 -- if this check has been suppressed (or is suppressed by
13423 -- default in the non-strict alignment machine case).
83f8f0a6 13424
d6da7448 13425 -- Note: we do not check the alignment if we gave a size
13426 -- warning, since it would likely be redundant.
83f8f0a6 13427
514a5555 13428 elsif not Alignment_Checks_Suppressed (ACCR.X)
e556831e 13429 and then Y_Alignment /= Uint_0
7161e166 13430 and then
13431 (Y_Alignment < X_Alignment
13432 or else
13433 (ACCR.Off
13434 and then Nkind (Expr) = N_Attribute_Reference
13435 and then Attribute_Name (Expr) = Name_Address
13436 and then Has_Compatible_Alignment
13437 (ACCR.X, Prefix (Expr), True) /=
13438 Known_Compatible))
83f8f0a6 13439 then
13440 Error_Msg_NE
7161e166 13441 ("??specified address for& may be inconsistent with "
13442 & "alignment", ACCR.N, ACCR.X);
83f8f0a6 13443 Error_Msg_N
1e3532e7 13444 ("\??program execution may be erroneous (RM 13.3(27))",
83f8f0a6 13445 ACCR.N);
7161e166 13446
83f8f0a6 13447 Error_Msg_Uint_1 := X_Alignment;
7161e166 13448 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13449
83f8f0a6 13450 Error_Msg_Uint_1 := Y_Alignment;
7161e166 13451 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13452
d6da7448 13453 if Y_Alignment >= X_Alignment then
13454 Error_Msg_N
7161e166 13455 ("\??but offset is not multiple of alignment", ACCR.N);
d6da7448 13456 end if;
83f8f0a6 13457 end if;
13458 end if;
13459 end;
13460 end loop;
13461 end Validate_Address_Clauses;
13462
76a6b7c7 13463 -----------------------------------------
13464 -- Validate_Compile_Time_Warning_Error --
13465 -----------------------------------------
13466
13467 procedure Validate_Compile_Time_Warning_Error (N : Node_Id) is
13468 begin
13469 Compile_Time_Warnings_Errors.Append
13470 (New_Val => CTWE_Entry'(Eloc => Sloc (N),
13471 Scope => Current_Scope,
13472 Prag => N));
13473 end Validate_Compile_Time_Warning_Error;
13474
13475 ------------------------------------------
13476 -- Validate_Compile_Time_Warning_Errors --
13477 ------------------------------------------
13478
13479 procedure Validate_Compile_Time_Warning_Errors is
13480 procedure Set_Scope (S : Entity_Id);
13481 -- Install all enclosing scopes of S along with S itself
13482
13483 procedure Unset_Scope (S : Entity_Id);
13484 -- Uninstall all enclosing scopes of S along with S itself
13485
13486 ---------------
13487 -- Set_Scope --
13488 ---------------
13489
13490 procedure Set_Scope (S : Entity_Id) is
13491 begin
13492 if S /= Standard_Standard then
13493 Set_Scope (Scope (S));
13494 end if;
13495
13496 Push_Scope (S);
13497 end Set_Scope;
13498
13499 -----------------
13500 -- Unset_Scope --
13501 -----------------
13502
13503 procedure Unset_Scope (S : Entity_Id) is
13504 begin
13505 if S /= Standard_Standard then
13506 Unset_Scope (Scope (S));
13507 end if;
13508
13509 Pop_Scope;
13510 end Unset_Scope;
13511
13512 -- Start of processing for Validate_Compile_Time_Warning_Errors
13513
13514 begin
13515 Expander_Mode_Save_And_Set (False);
13516 In_Compile_Time_Warning_Or_Error := True;
13517
13518 for N in Compile_Time_Warnings_Errors.First ..
13519 Compile_Time_Warnings_Errors.Last
13520 loop
13521 declare
13522 T : CTWE_Entry renames Compile_Time_Warnings_Errors.Table (N);
13523
13524 begin
13525 Set_Scope (T.Scope);
13526 Reset_Analyzed_Flags (T.Prag);
13527 Process_Compile_Time_Warning_Or_Error (T.Prag, T.Eloc);
13528 Unset_Scope (T.Scope);
13529 end;
13530 end loop;
13531
13532 In_Compile_Time_Warning_Or_Error := False;
13533 Expander_Mode_Restore;
13534 end Validate_Compile_Time_Warning_Errors;
13535
7717ea00 13536 ---------------------------
13537 -- Validate_Independence --
13538 ---------------------------
13539
13540 procedure Validate_Independence is
13541 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13542 N : Node_Id;
13543 E : Entity_Id;
13544 IC : Boolean;
13545 Comp : Entity_Id;
13546 Addr : Node_Id;
13547 P : Node_Id;
13548
13549 procedure Check_Array_Type (Atyp : Entity_Id);
13550 -- Checks if the array type Atyp has independent components, and
13551 -- if not, outputs an appropriate set of error messages.
13552
13553 procedure No_Independence;
13554 -- Output message that independence cannot be guaranteed
13555
13556 function OK_Component (C : Entity_Id) return Boolean;
13557 -- Checks one component to see if it is independently accessible, and
13558 -- if so yields True, otherwise yields False if independent access
13559 -- cannot be guaranteed. This is a conservative routine, it only
13560 -- returns True if it knows for sure, it returns False if it knows
13561 -- there is a problem, or it cannot be sure there is no problem.
13562
13563 procedure Reason_Bad_Component (C : Entity_Id);
13564 -- Outputs continuation message if a reason can be determined for
13565 -- the component C being bad.
13566
13567 ----------------------
13568 -- Check_Array_Type --
13569 ----------------------
13570
13571 procedure Check_Array_Type (Atyp : Entity_Id) is
13572 Ctyp : constant Entity_Id := Component_Type (Atyp);
13573
13574 begin
13575 -- OK if no alignment clause, no pack, and no component size
13576
13577 if not Has_Component_Size_Clause (Atyp)
13578 and then not Has_Alignment_Clause (Atyp)
13579 and then not Is_Packed (Atyp)
13580 then
13581 return;
13582 end if;
13583
aa0a69ab 13584 -- Case of component size is greater than or equal to 64 and the
13585 -- alignment of the array is at least as large as the alignment
13586 -- of the component. We are definitely OK in this situation.
13587
13588 if Known_Component_Size (Atyp)
13589 and then Component_Size (Atyp) >= 64
13590 and then Known_Alignment (Atyp)
13591 and then Known_Alignment (Ctyp)
13592 and then Alignment (Atyp) >= Alignment (Ctyp)
13593 then
13594 return;
13595 end if;
13596
7717ea00 13597 -- Check actual component size
13598
13599 if not Known_Component_Size (Atyp)
13600 or else not (Addressable (Component_Size (Atyp))
aa0a69ab 13601 and then Component_Size (Atyp) < 64)
7717ea00 13602 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13603 then
13604 No_Independence;
13605
13606 -- Bad component size, check reason
13607
13608 if Has_Component_Size_Clause (Atyp) then
b9e61b2a 13609 P := Get_Attribute_Definition_Clause
13610 (Atyp, Attribute_Component_Size);
7717ea00 13611
13612 if Present (P) then
13613 Error_Msg_Sloc := Sloc (P);
13614 Error_Msg_N ("\because of Component_Size clause#", N);
13615 return;
13616 end if;
13617 end if;
13618
13619 if Is_Packed (Atyp) then
13620 P := Get_Rep_Pragma (Atyp, Name_Pack);
13621
13622 if Present (P) then
13623 Error_Msg_Sloc := Sloc (P);
13624 Error_Msg_N ("\because of pragma Pack#", N);
13625 return;
13626 end if;
13627 end if;
13628
13629 -- No reason found, just return
13630
13631 return;
13632 end if;
13633
13634 -- Array type is OK independence-wise
13635
13636 return;
13637 end Check_Array_Type;
13638
13639 ---------------------
13640 -- No_Independence --
13641 ---------------------
13642
13643 procedure No_Independence is
13644 begin
ddccc924 13645 if Pragma_Name (N) = Name_Independent then
18393965 13646 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
7717ea00 13647 else
13648 Error_Msg_NE
13649 ("independent components cannot be guaranteed for&", N, E);
13650 end if;
13651 end No_Independence;
13652
13653 ------------------
13654 -- OK_Component --
13655 ------------------
13656
13657 function OK_Component (C : Entity_Id) return Boolean is
13658 Rec : constant Entity_Id := Scope (C);
13659 Ctyp : constant Entity_Id := Etype (C);
13660
13661 begin
13662 -- OK if no component clause, no Pack, and no alignment clause
13663
13664 if No (Component_Clause (C))
13665 and then not Is_Packed (Rec)
13666 and then not Has_Alignment_Clause (Rec)
13667 then
13668 return True;
13669 end if;
13670
13671 -- Here we look at the actual component layout. A component is
13672 -- addressable if its size is a multiple of the Esize of the
13673 -- component type, and its starting position in the record has
13674 -- appropriate alignment, and the record itself has appropriate
13675 -- alignment to guarantee the component alignment.
13676
13677 -- Make sure sizes are static, always assume the worst for any
13678 -- cases where we cannot check static values.
13679
13680 if not (Known_Static_Esize (C)
b9e61b2a 13681 and then
13682 Known_Static_Esize (Ctyp))
7717ea00 13683 then
13684 return False;
13685 end if;
13686
13687 -- Size of component must be addressable or greater than 64 bits
13688 -- and a multiple of bytes.
13689
b9e61b2a 13690 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
7717ea00 13691 return False;
13692 end if;
13693
13694 -- Check size is proper multiple
13695
13696 if Esize (C) mod Esize (Ctyp) /= 0 then
13697 return False;
13698 end if;
13699
13700 -- Check alignment of component is OK
13701
13702 if not Known_Component_Bit_Offset (C)
13703 or else Component_Bit_Offset (C) < Uint_0
13704 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13705 then
13706 return False;
13707 end if;
13708
13709 -- Check alignment of record type is OK
13710
13711 if not Known_Alignment (Rec)
13712 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13713 then
13714 return False;
13715 end if;
13716
13717 -- All tests passed, component is addressable
13718
13719 return True;
13720 end OK_Component;
13721
13722 --------------------------
13723 -- Reason_Bad_Component --
13724 --------------------------
13725
13726 procedure Reason_Bad_Component (C : Entity_Id) is
13727 Rec : constant Entity_Id := Scope (C);
13728 Ctyp : constant Entity_Id := Etype (C);
13729
13730 begin
13731 -- If component clause present assume that's the problem
13732
13733 if Present (Component_Clause (C)) then
13734 Error_Msg_Sloc := Sloc (Component_Clause (C));
13735 Error_Msg_N ("\because of Component_Clause#", N);
13736 return;
13737 end if;
13738
13739 -- If pragma Pack clause present, assume that's the problem
13740
13741 if Is_Packed (Rec) then
13742 P := Get_Rep_Pragma (Rec, Name_Pack);
13743
13744 if Present (P) then
13745 Error_Msg_Sloc := Sloc (P);
13746 Error_Msg_N ("\because of pragma Pack#", N);
13747 return;
13748 end if;
13749 end if;
13750
13751 -- See if record has bad alignment clause
13752
13753 if Has_Alignment_Clause (Rec)
13754 and then Known_Alignment (Rec)
13755 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13756 then
13757 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13758
13759 if Present (P) then
13760 Error_Msg_Sloc := Sloc (P);
13761 Error_Msg_N ("\because of Alignment clause#", N);
13762 end if;
13763 end if;
13764
13765 -- Couldn't find a reason, so return without a message
13766
13767 return;
13768 end Reason_Bad_Component;
13769
13770 -- Start of processing for Validate_Independence
13771
13772 begin
13773 for J in Independence_Checks.First .. Independence_Checks.Last loop
13774 N := Independence_Checks.Table (J).N;
13775 E := Independence_Checks.Table (J).E;
ddccc924 13776 IC := Pragma_Name (N) = Name_Independent_Components;
7717ea00 13777
13778 -- Deal with component case
13779
13780 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13781 if not OK_Component (E) then
13782 No_Independence;
13783 Reason_Bad_Component (E);
13784 goto Continue;
13785 end if;
13786 end if;
13787
13788 -- Deal with record with Independent_Components
13789
13790 if IC and then Is_Record_Type (E) then
13791 Comp := First_Component_Or_Discriminant (E);
13792 while Present (Comp) loop
13793 if not OK_Component (Comp) then
13794 No_Independence;
13795 Reason_Bad_Component (Comp);
13796 goto Continue;
13797 end if;
13798
13799 Next_Component_Or_Discriminant (Comp);
13800 end loop;
13801 end if;
13802
13803 -- Deal with address clause case
13804
13805 if Is_Object (E) then
13806 Addr := Address_Clause (E);
13807
13808 if Present (Addr) then
13809 No_Independence;
13810 Error_Msg_Sloc := Sloc (Addr);
13811 Error_Msg_N ("\because of Address clause#", N);
13812 goto Continue;
13813 end if;
13814 end if;
13815
13816 -- Deal with independent components for array type
13817
13818 if IC and then Is_Array_Type (E) then
13819 Check_Array_Type (E);
13820 end if;
13821
13822 -- Deal with independent components for array object
13823
13824 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13825 Check_Array_Type (Etype (E));
13826 end if;
13827
13828 <<Continue>> null;
13829 end loop;
13830 end Validate_Independence;
13831
b3f8228a 13832 ------------------------------
13833 -- Validate_Iterable_Aspect --
13834 ------------------------------
13835
13836 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
3061ffde 13837 Assoc : Node_Id;
13838 Expr : Node_Id;
b3f8228a 13839
bde03454 13840 Prim : Node_Id;
a9f5fea7 13841 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
b3f8228a 13842
13843 First_Id : Entity_Id;
13844 Next_Id : Entity_Id;
13845 Has_Element_Id : Entity_Id;
13846 Element_Id : Entity_Id;
13847
b3f8228a 13848 begin
9698629c 13849 -- If previous error aspect is unusable
a9f5fea7 13850
13851 if Cursor = Any_Type then
3061ffde 13852 return;
13853 end if;
b3f8228a 13854
13855 First_Id := Empty;
13856 Next_Id := Empty;
13857 Has_Element_Id := Empty;
32de816b 13858 Element_Id := Empty;
b3f8228a 13859
13860 -- Each expression must resolve to a function with the proper signature
13861
13862 Assoc := First (Component_Associations (Expression (ASN)));
13863 while Present (Assoc) loop
13864 Expr := Expression (Assoc);
13865 Analyze (Expr);
13866
b3f8228a 13867 Prim := First (Choices (Assoc));
bde03454 13868
f02a9a9a 13869 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
b3f8228a 13870 Error_Msg_N ("illegal name in association", Prim);
13871
13872 elsif Chars (Prim) = Name_First then
3061ffde 13873 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
b3f8228a 13874 First_Id := Entity (Expr);
b3f8228a 13875
13876 elsif Chars (Prim) = Name_Next then
3061ffde 13877 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
b3f8228a 13878 Next_Id := Entity (Expr);
b3f8228a 13879
13880 elsif Chars (Prim) = Name_Has_Element then
3061ffde 13881 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
b3f8228a 13882 Has_Element_Id := Entity (Expr);
bde03454 13883
b3f8228a 13884 elsif Chars (Prim) = Name_Element then
3061ffde 13885 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
b3f8228a 13886 Element_Id := Entity (Expr);
b3f8228a 13887
13888 else
13889 Error_Msg_N ("invalid name for iterable function", Prim);
13890 end if;
13891
13892 Next (Assoc);
13893 end loop;
13894
13895 if No (First_Id) then
3061ffde 13896 Error_Msg_N ("match for First primitive not found", ASN);
b3f8228a 13897
13898 elsif No (Next_Id) then
3061ffde 13899 Error_Msg_N ("match for Next primitive not found", ASN);
b3f8228a 13900
13901 elsif No (Has_Element_Id) then
3061ffde 13902 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13903
13904 elsif No (Element_Id) then
13905 null; -- Optional.
b3f8228a 13906 end if;
13907 end Validate_Iterable_Aspect;
13908
d6f39728 13909 -----------------------------------
13910 -- Validate_Unchecked_Conversion --
13911 -----------------------------------
13912
13913 procedure Validate_Unchecked_Conversion
13914 (N : Node_Id;
13915 Act_Unit : Entity_Id)
13916 is
13917 Source : Entity_Id;
13918 Target : Entity_Id;
13919 Vnode : Node_Id;
13920
13921 begin
13922 -- Obtain source and target types. Note that we call Ancestor_Subtype
13923 -- here because the processing for generic instantiation always makes
13924 -- subtypes, and we want the original frozen actual types.
13925
13926 -- If we are dealing with private types, then do the check on their
13927 -- fully declared counterparts if the full declarations have been
39a0c1d3 13928 -- encountered (they don't have to be visible, but they must exist).
d6f39728 13929
13930 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13931
13932 if Is_Private_Type (Source)
13933 and then Present (Underlying_Type (Source))
13934 then
13935 Source := Underlying_Type (Source);
13936 end if;
13937
13938 Target := Ancestor_Subtype (Etype (Act_Unit));
13939
fdd294d1 13940 -- If either type is generic, the instantiation happens within a generic
95deda50 13941 -- unit, and there is nothing to check. The proper check will happen
13942 -- when the enclosing generic is instantiated.
d6f39728 13943
13944 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13945 return;
13946 end if;
13947
13948 if Is_Private_Type (Target)
13949 and then Present (Underlying_Type (Target))
13950 then
13951 Target := Underlying_Type (Target);
13952 end if;
13953
0924014e 13954 -- Source may be unconstrained array, but not target, except in relaxed
13955 -- semantics mode.
d6f39728 13956
0924014e 13957 if Is_Array_Type (Target)
13958 and then not Is_Constrained (Target)
13959 and then not Relaxed_RM_Semantics
13960 then
d6f39728 13961 Error_Msg_N
13962 ("unchecked conversion to unconstrained array not allowed", N);
13963 return;
13964 end if;
13965
fbc67f84 13966 -- Warn if conversion between two different convention pointers
13967
13968 if Is_Access_Type (Target)
13969 and then Is_Access_Type (Source)
13970 and then Convention (Target) /= Convention (Source)
13971 and then Warn_On_Unchecked_Conversion
13972 then
74c7ae52 13973 -- Give warnings for subprogram pointers only on most targets
fdd294d1 13974
13975 if Is_Access_Subprogram_Type (Target)
13976 or else Is_Access_Subprogram_Type (Source)
fdd294d1 13977 then
13978 Error_Msg_N
cb97ae5c 13979 ("?z?conversion between pointers with different conventions!",
1e3532e7 13980 N);
fdd294d1 13981 end if;
fbc67f84 13982 end if;
13983
3062c401 13984 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13985 -- warning when compiling GNAT-related sources.
13986
13987 if Warn_On_Unchecked_Conversion
13988 and then not In_Predefined_Unit (N)
13989 and then RTU_Loaded (Ada_Calendar)
f02a9a9a 13990 and then (Chars (Source) = Name_Time
13991 or else
13992 Chars (Target) = Name_Time)
3062c401 13993 then
13994 -- If Ada.Calendar is loaded and the name of one of the operands is
13995 -- Time, there is a good chance that this is Ada.Calendar.Time.
13996
13997 declare
f02a9a9a 13998 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
3062c401 13999 begin
14000 pragma Assert (Present (Calendar_Time));
14001
b9e61b2a 14002 if Source = Calendar_Time or else Target = Calendar_Time then
3062c401 14003 Error_Msg_N
f02a9a9a 14004 ("?z?representation of 'Time values may change between "
14005 & "'G'N'A'T versions", N);
3062c401 14006 end if;
14007 end;
14008 end if;
14009
fdd294d1 14010 -- Make entry in unchecked conversion table for later processing by
14011 -- Validate_Unchecked_Conversions, which will check sizes and alignments
3ff5e35d 14012 -- (using values set by the back end where possible). This is only done
fdd294d1 14013 -- if the appropriate warning is active.
d6f39728 14014
9dfe12ae 14015 if Warn_On_Unchecked_Conversion then
14016 Unchecked_Conversions.Append
86d32751 14017 (New_Val => UC_Entry'(Eloc => Sloc (N),
14018 Source => Source,
14019 Target => Target,
14020 Act_Unit => Act_Unit));
9dfe12ae 14021
f9906591 14022 -- If both sizes are known statically now, then back-end annotation
9dfe12ae 14023 -- is not required to do a proper check but if either size is not
14024 -- known statically, then we need the annotation.
14025
14026 if Known_Static_RM_Size (Source)
1e3532e7 14027 and then
14028 Known_Static_RM_Size (Target)
9dfe12ae 14029 then
14030 null;
14031 else
14032 Back_Annotate_Rep_Info := True;
14033 end if;
14034 end if;
d6f39728 14035
fdd294d1 14036 -- If unchecked conversion to access type, and access type is declared
95deda50 14037 -- in the same unit as the unchecked conversion, then set the flag
14038 -- No_Strict_Aliasing (no strict aliasing is implicit here)
28ed91d4 14039
14040 if Is_Access_Type (Target) and then
14041 In_Same_Source_Unit (Target, N)
14042 then
14043 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
14044 end if;
3d875462 14045
95deda50 14046 -- Generate N_Validate_Unchecked_Conversion node for back end in case
14047 -- the back end needs to perform special validation checks.
3d875462 14048
95deda50 14049 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
14050 -- have full expansion and the back end is called ???
3d875462 14051
14052 Vnode :=
14053 Make_Validate_Unchecked_Conversion (Sloc (N));
14054 Set_Source_Type (Vnode, Source);
14055 Set_Target_Type (Vnode, Target);
14056
fdd294d1 14057 -- If the unchecked conversion node is in a list, just insert before it.
14058 -- If not we have some strange case, not worth bothering about.
3d875462 14059
14060 if Is_List_Member (N) then
d6f39728 14061 Insert_After (N, Vnode);
14062 end if;
14063 end Validate_Unchecked_Conversion;
14064
14065 ------------------------------------
14066 -- Validate_Unchecked_Conversions --
14067 ------------------------------------
14068
14069 procedure Validate_Unchecked_Conversions is
14070 begin
14071 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
14072 declare
14073 T : UC_Entry renames Unchecked_Conversions.Table (N);
14074
e13b1635 14075 Act_Unit : constant Entity_Id := T.Act_Unit;
86d32751 14076 Eloc : constant Source_Ptr := T.Eloc;
14077 Source : constant Entity_Id := T.Source;
14078 Target : constant Entity_Id := T.Target;
d6f39728 14079
44705307 14080 Source_Siz : Uint;
14081 Target_Siz : Uint;
d6f39728 14082
14083 begin
86d32751 14084 -- Skip if function marked as warnings off
14085
14086 if Warnings_Off (Act_Unit) then
14087 goto Continue;
14088 end if;
14089
fdd294d1 14090 -- This validation check, which warns if we have unequal sizes for
14091 -- unchecked conversion, and thus potentially implementation
d6f39728 14092 -- dependent semantics, is one of the few occasions on which we
fdd294d1 14093 -- use the official RM size instead of Esize. See description in
14094 -- Einfo "Handling of Type'Size Values" for details.
d6f39728 14095
f15731c4 14096 if Serious_Errors_Detected = 0
d6f39728 14097 and then Known_Static_RM_Size (Source)
14098 and then Known_Static_RM_Size (Target)
f25f4252 14099
14100 -- Don't do the check if warnings off for either type, note the
14101 -- deliberate use of OR here instead of OR ELSE to get the flag
14102 -- Warnings_Off_Used set for both types if appropriate.
14103
14104 and then not (Has_Warnings_Off (Source)
14105 or
14106 Has_Warnings_Off (Target))
d6f39728 14107 then
14108 Source_Siz := RM_Size (Source);
14109 Target_Siz := RM_Size (Target);
14110
14111 if Source_Siz /= Target_Siz then
299480f9 14112 Error_Msg
cb97ae5c 14113 ("?z?types for unchecked conversion have different sizes!",
299480f9 14114 Eloc);
d6f39728 14115
14116 if All_Errors_Mode then
14117 Error_Msg_Name_1 := Chars (Source);
14118 Error_Msg_Uint_1 := Source_Siz;
14119 Error_Msg_Name_2 := Chars (Target);
14120 Error_Msg_Uint_2 := Target_Siz;
cb97ae5c 14121 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
d6f39728 14122
14123 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
14124
14125 if Is_Discrete_Type (Source)
b9e61b2a 14126 and then
14127 Is_Discrete_Type (Target)
d6f39728 14128 then
14129 if Source_Siz > Target_Siz then
299480f9 14130 Error_Msg
cb97ae5c 14131 ("\?z?^ high order bits of source will "
1e3532e7 14132 & "be ignored!", Eloc);
d6f39728 14133
9dfe12ae 14134 elsif Is_Unsigned_Type (Source) then
299480f9 14135 Error_Msg
cb97ae5c 14136 ("\?z?source will be extended with ^ high order "
1581f2d7 14137 & "zero bits!", Eloc);
d6f39728 14138
14139 else
299480f9 14140 Error_Msg
cb97ae5c 14141 ("\?z?source will be extended with ^ high order "
1e3532e7 14142 & "sign bits!", Eloc);
d6f39728 14143 end if;
14144
14145 elsif Source_Siz < Target_Siz then
14146 if Is_Discrete_Type (Target) then
14147 if Bytes_Big_Endian then
299480f9 14148 Error_Msg
cb97ae5c 14149 ("\?z?target value will include ^ undefined "
1e3532e7 14150 & "low order bits!", Eloc);
d6f39728 14151 else
299480f9 14152 Error_Msg
cb97ae5c 14153 ("\?z?target value will include ^ undefined "
1e3532e7 14154 & "high order bits!", Eloc);
d6f39728 14155 end if;
14156
14157 else
299480f9 14158 Error_Msg
cb97ae5c 14159 ("\?z?^ trailing bits of target value will be "
1e3532e7 14160 & "undefined!", Eloc);
d6f39728 14161 end if;
14162
14163 else pragma Assert (Source_Siz > Target_Siz);
0388e54e 14164 if Is_Discrete_Type (Source) then
14165 if Bytes_Big_Endian then
14166 Error_Msg
14167 ("\?z?^ low order bits of source will be "
14168 & "ignored!", Eloc);
14169 else
14170 Error_Msg
14171 ("\?z?^ high order bits of source will be "
14172 & "ignored!", Eloc);
14173 end if;
14174
14175 else
14176 Error_Msg
14177 ("\?z?^ trailing bits of source will be "
14178 & "ignored!", Eloc);
14179 end if;
d6f39728 14180 end if;
14181 end if;
d6f39728 14182 end if;
14183 end if;
14184
14185 -- If both types are access types, we need to check the alignment.
14186 -- If the alignment of both is specified, we can do it here.
14187
f15731c4 14188 if Serious_Errors_Detected = 0
2a10e737 14189 and then Is_Access_Type (Source)
14190 and then Is_Access_Type (Target)
d6f39728 14191 and then Target_Strict_Alignment
14192 and then Present (Designated_Type (Source))
14193 and then Present (Designated_Type (Target))
14194 then
14195 declare
14196 D_Source : constant Entity_Id := Designated_Type (Source);
14197 D_Target : constant Entity_Id := Designated_Type (Target);
14198
14199 begin
14200 if Known_Alignment (D_Source)
b9e61b2a 14201 and then
14202 Known_Alignment (D_Target)
d6f39728 14203 then
14204 declare
14205 Source_Align : constant Uint := Alignment (D_Source);
14206 Target_Align : constant Uint := Alignment (D_Target);
14207
14208 begin
14209 if Source_Align < Target_Align
14210 and then not Is_Tagged_Type (D_Source)
f25f4252 14211
14212 -- Suppress warning if warnings suppressed on either
14213 -- type or either designated type. Note the use of
14214 -- OR here instead of OR ELSE. That is intentional,
14215 -- we would like to set flag Warnings_Off_Used in
14216 -- all types for which warnings are suppressed.
14217
14218 and then not (Has_Warnings_Off (D_Source)
14219 or
14220 Has_Warnings_Off (D_Target)
14221 or
14222 Has_Warnings_Off (Source)
14223 or
14224 Has_Warnings_Off (Target))
d6f39728 14225 then
d6f39728 14226 Error_Msg_Uint_1 := Target_Align;
14227 Error_Msg_Uint_2 := Source_Align;
299480f9 14228 Error_Msg_Node_1 := D_Target;
d6f39728 14229 Error_Msg_Node_2 := D_Source;
299480f9 14230 Error_Msg
cb97ae5c 14231 ("?z?alignment of & (^) is stricter than "
1e3532e7 14232 & "alignment of & (^)!", Eloc);
f25f4252 14233 Error_Msg
cb97ae5c 14234 ("\?z?resulting access value may have invalid "
1e3532e7 14235 & "alignment!", Eloc);
d6f39728 14236 end if;
14237 end;
14238 end if;
14239 end;
14240 end if;
14241 end;
86d32751 14242
14243 <<Continue>>
14244 null;
d6f39728 14245 end loop;
14246 end Validate_Unchecked_Conversions;
14247
d6f39728 14248end Sem_Ch13;