]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch13.adb
2015-10-26 Joel Brobecker <brobecker@adacore.com brobecker>
[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-- --
60a4a5af 9-- Copyright (C) 1992-2015, 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;
d00681a7 33with Exp_Disp; use Exp_Disp;
d6f39728 34with Exp_Tss; use Exp_Tss;
35with Exp_Util; use Exp_Util;
37c6552c 36with Freeze; use Freeze;
f9e26ff7 37with Ghost; use Ghost;
d6f39728 38with Lib; use Lib;
83f8f0a6 39with Lib.Xref; use Lib.Xref;
15ebb600 40with Namet; use Namet;
d6f39728 41with Nlists; use Nlists;
42with Nmake; use Nmake;
43with Opt; use Opt;
e0521a36 44with Restrict; use Restrict;
45with Rident; use Rident;
d6f39728 46with Rtsfind; use Rtsfind;
47with Sem; use Sem;
d60c9ff7 48with Sem_Aux; use Sem_Aux;
be9124d0 49with Sem_Case; use Sem_Case;
40ca69b9 50with Sem_Ch3; use Sem_Ch3;
490beba6 51with Sem_Ch6; use Sem_Ch6;
d6f39728 52with Sem_Ch8; use Sem_Ch8;
85696508 53with Sem_Dim; use Sem_Dim;
85377c9b 54with Sem_Disp; use Sem_Disp;
d6f39728 55with Sem_Eval; use Sem_Eval;
51ea9c94 56with Sem_Prag; use Sem_Prag;
d6f39728 57with Sem_Res; use Sem_Res;
58with Sem_Type; use Sem_Type;
59with Sem_Util; use Sem_Util;
44e4341e 60with Sem_Warn; use Sem_Warn;
1e3c4ae6 61with Sinput; use Sinput;
9dfe12ae 62with Snames; use Snames;
d6f39728 63with Stand; use Stand;
64with Sinfo; use Sinfo;
5b5df4a9 65with Stringt; use Stringt;
93735cb8 66with Targparm; use Targparm;
d6f39728 67with Ttypes; use Ttypes;
68with Tbuild; use Tbuild;
69with Urealp; use Urealp;
f42f24d7 70with Warnsw; use Warnsw;
d6f39728 71
bfa5a9d9 72with GNAT.Heap_Sort_G;
d6f39728 73
74package body Sem_Ch13 is
75
76 SSU : constant Pos := System_Storage_Unit;
77 -- Convenient short hand for commonly used constant
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
1d366b32 83 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
84 -- This routine is called after setting one of the sizes of type entity
85 -- Typ to Size. The purpose is to deal with the situation of a derived
86 -- type whose inherited alignment is no longer appropriate for the new
87 -- size value. In this case, we reset the Alignment to unknown.
d6f39728 88
eb66e842 89 procedure Build_Discrete_Static_Predicate
d97beb2f 90 (Typ : Entity_Id;
91 Expr : Node_Id;
92 Nam : Name_Id);
d7c2851f 93 -- Given a predicated type Typ, where Typ is a discrete static subtype,
94 -- whose predicate expression is Expr, tests if Expr is a static predicate,
95 -- and if so, builds the predicate range list. Nam is the name of the one
96 -- argument to the predicate function. Occurrences of the type name in the
6fb3c314 97 -- predicate expression have been replaced by identifier references to this
d7c2851f 98 -- name, which is unique, so any identifier with Chars matching Nam must be
99 -- a reference to the type. If the predicate is non-static, this procedure
100 -- returns doing nothing. If the predicate is static, then the predicate
5c6a5792 101 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
102 -- rewritten as a canonicalized membership operation.
d97beb2f 103
eb66e842 104 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
105 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
106 -- then either there are pragma Predicate entries on the rep chain for the
107 -- type (note that Predicate aspects are converted to pragma Predicate), or
108 -- there are inherited aspects from a parent type, or ancestor subtypes.
109 -- This procedure builds the spec and body for the Predicate function that
110 -- tests these predicates. N is the freeze node for the type. The spec of
111 -- the function is inserted before the freeze node, and the body of the
112 -- function is inserted after the freeze node. If the predicate expression
113 -- has at least one Raise_Expression, then this procedure also builds the
114 -- M version of the predicate function for use in membership tests.
115
6653b695 116 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
117 -- Called if both Storage_Pool and Storage_Size attribute definition
118 -- clauses (SP and SS) are present for entity Ent. Issue error message.
119
d9f6a4ee 120 procedure Freeze_Entity_Checks (N : Node_Id);
121 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
122 -- to generate appropriate semantic checks that are delayed until this
123 -- point (they had to be delayed this long for cases of delayed aspects,
124 -- e.g. analysis of statically predicated subtypes in choices, for which
5f067114 125 -- we have to be sure the subtypes in question are frozen before checking).
d9f6a4ee 126
d6f39728 127 function Get_Alignment_Value (Expr : Node_Id) return Uint;
128 -- Given the expression for an alignment value, returns the corresponding
129 -- Uint value. If the value is inappropriate, then error messages are
130 -- posted as required, and a value of No_Uint is returned.
131
132 function Is_Operational_Item (N : Node_Id) return Boolean;
1e3c4ae6 133 -- A specification for a stream attribute is allowed before the full type
134 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
135 -- that do not specify a representation characteristic are operational
136 -- attributes.
d6f39728 137
3b23aaa0 138 function Is_Predicate_Static
139 (Expr : Node_Id;
140 Nam : Name_Id) return Boolean;
141 -- Given predicate expression Expr, tests if Expr is predicate-static in
142 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
143 -- name in the predicate expression have been replaced by references to
144 -- an identifier whose Chars field is Nam. This name is unique, so any
145 -- identifier with Chars matching Nam must be a reference to the type.
146 -- Returns True if the expression is predicate-static and False otherwise,
147 -- but is not in the business of setting flags or issuing error messages.
148 --
149 -- Only scalar types can have static predicates, so False is always
150 -- returned for non-scalar types.
151 --
152 -- Note: the RM seems to suggest that string types can also have static
153 -- predicates. But that really makes lttle sense as very few useful
154 -- predicates can be constructed for strings. Remember that:
155 --
156 -- "ABC" < "DEF"
157 --
158 -- is not a static expression. So even though the clearly faulty RM wording
159 -- allows the following:
160 --
161 -- subtype S is String with Static_Predicate => S < "DEF"
162 --
163 -- We can't allow this, otherwise we have predicate-static applying to a
164 -- larger class than static expressions, which was never intended.
165
44e4341e 166 procedure New_Stream_Subprogram
d6f39728 167 (N : Node_Id;
168 Ent : Entity_Id;
169 Subp : Entity_Id;
9dfe12ae 170 Nam : TSS_Name_Type);
44e4341e 171 -- Create a subprogram renaming of a given stream attribute to the
172 -- designated subprogram and then in the tagged case, provide this as a
d1a2e31b 173 -- primitive operation, or in the untagged case make an appropriate TSS
44e4341e 174 -- entry. This is more properly an expansion activity than just semantics,
d1a2e31b 175 -- but the presence of user-defined stream functions for limited types
176 -- is a legality check, which is why this takes place here rather than in
44e4341e 177 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
178 -- function to be generated.
9dfe12ae 179 --
f15731c4 180 -- To avoid elaboration anomalies with freeze nodes, for untagged types
181 -- we generate both a subprogram declaration and a subprogram renaming
182 -- declaration, so that the attribute specification is handled as a
183 -- renaming_as_body. For tagged types, the specification is one of the
184 -- primitive specs.
185
3061ffde 186 procedure Resolve_Iterable_Operation
187 (N : Node_Id;
188 Cursor : Entity_Id;
189 Typ : Entity_Id;
190 Nam : Name_Id);
191 -- If the name of a primitive operation for an Iterable aspect is
192 -- overloaded, resolve according to required signature.
193
b77e4501 194 procedure Set_Biased
195 (E : Entity_Id;
196 N : Node_Id;
197 Msg : String;
198 Biased : Boolean := True);
199 -- If Biased is True, sets Has_Biased_Representation flag for E, and
200 -- outputs a warning message at node N if Warn_On_Biased_Representation is
201 -- is True. This warning inserts the string Msg to describe the construct
202 -- causing biasing.
203
d6f39728 204 ----------------------------------------------
205 -- Table for Validate_Unchecked_Conversions --
206 ----------------------------------------------
207
208 -- The following table collects unchecked conversions for validation.
95deda50 209 -- Entries are made by Validate_Unchecked_Conversion and then the call
210 -- to Validate_Unchecked_Conversions does the actual error checking and
211 -- posting of warnings. The reason for this delayed processing is to take
212 -- advantage of back-annotations of size and alignment values performed by
213 -- the back end.
d6f39728 214
95deda50 215 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
216 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
217 -- already have modified all Sloc values if the -gnatD option is set.
299480f9 218
d6f39728 219 type UC_Entry is record
86d32751 220 Eloc : Source_Ptr; -- node used for posting warnings
221 Source : Entity_Id; -- source type for unchecked conversion
222 Target : Entity_Id; -- target type for unchecked conversion
223 Act_Unit : Entity_Id; -- actual function instantiated
d6f39728 224 end record;
225
226 package Unchecked_Conversions is new Table.Table (
227 Table_Component_Type => UC_Entry,
228 Table_Index_Type => Int,
229 Table_Low_Bound => 1,
230 Table_Initial => 50,
231 Table_Increment => 200,
232 Table_Name => "Unchecked_Conversions");
233
83f8f0a6 234 ----------------------------------------
235 -- Table for Validate_Address_Clauses --
236 ----------------------------------------
237
238 -- If an address clause has the form
239
240 -- for X'Address use Expr
241
95deda50 242 -- where Expr is of the form Y'Address or recursively is a reference to a
243 -- constant of either of these forms, and X and Y are entities of objects,
244 -- then if Y has a smaller alignment than X, that merits a warning about
245 -- possible bad alignment. The following table collects address clauses of
246 -- this kind. We put these in a table so that they can be checked after the
247 -- back end has completed annotation of the alignments of objects, since we
248 -- can catch more cases that way.
83f8f0a6 249
250 type Address_Clause_Check_Record is record
251 N : Node_Id;
252 -- The address clause
253
254 X : Entity_Id;
255 -- The entity of the object overlaying Y
256
257 Y : Entity_Id;
258 -- The entity of the object being overlaid
d6da7448 259
260 Off : Boolean;
6fb3c314 261 -- Whether the address is offset within Y
83f8f0a6 262 end record;
263
264 package Address_Clause_Checks is new Table.Table (
265 Table_Component_Type => Address_Clause_Check_Record,
266 Table_Index_Type => Int,
267 Table_Low_Bound => 1,
268 Table_Initial => 20,
269 Table_Increment => 200,
270 Table_Name => "Address_Clause_Checks");
271
59ac57b5 272 -----------------------------------------
273 -- Adjust_Record_For_Reverse_Bit_Order --
274 -----------------------------------------
275
276 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
67278d60 277 Comp : Node_Id;
278 CC : Node_Id;
59ac57b5 279
280 begin
67278d60 281 -- Processing depends on version of Ada
59ac57b5 282
6797073f 283 -- For Ada 95, we just renumber bits within a storage unit. We do the
568b0f6a 284 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
ab19a652 285 -- Ada 83, and are free to add this extension.
6797073f 286
287 if Ada_Version < Ada_2005 then
288 Comp := First_Component_Or_Discriminant (R);
289 while Present (Comp) loop
290 CC := Component_Clause (Comp);
291
292 -- If component clause is present, then deal with the non-default
293 -- bit order case for Ada 95 mode.
294
295 -- We only do this processing for the base type, and in fact that
296 -- is important, since otherwise if there are record subtypes, we
297 -- could reverse the bits once for each subtype, which is wrong.
298
b9e61b2a 299 if Present (CC) and then Ekind (R) = E_Record_Type then
6797073f 300 declare
301 CFB : constant Uint := Component_Bit_Offset (Comp);
302 CSZ : constant Uint := Esize (Comp);
303 CLC : constant Node_Id := Component_Clause (Comp);
304 Pos : constant Node_Id := Position (CLC);
305 FB : constant Node_Id := First_Bit (CLC);
306
307 Storage_Unit_Offset : constant Uint :=
308 CFB / System_Storage_Unit;
309
310 Start_Bit : constant Uint :=
311 CFB mod System_Storage_Unit;
59ac57b5 312
6797073f 313 begin
314 -- Cases where field goes over storage unit boundary
59ac57b5 315
6797073f 316 if Start_Bit + CSZ > System_Storage_Unit then
59ac57b5 317
6797073f 318 -- Allow multi-byte field but generate warning
59ac57b5 319
6797073f 320 if Start_Bit mod System_Storage_Unit = 0
321 and then CSZ mod System_Storage_Unit = 0
322 then
323 Error_Msg_N
7a41db5b 324 ("info: multi-byte field specified with "
325 & "non-standard Bit_Order?V?", CLC);
31486bc0 326
6797073f 327 if Bytes_Big_Endian then
31486bc0 328 Error_Msg_N
7a41db5b 329 ("\bytes are not reversed "
330 & "(component is big-endian)?V?", CLC);
31486bc0 331 else
332 Error_Msg_N
7a41db5b 333 ("\bytes are not reversed "
334 & "(component is little-endian)?V?", CLC);
31486bc0 335 end if;
59ac57b5 336
6797073f 337 -- Do not allow non-contiguous field
59ac57b5 338
67278d60 339 else
6797073f 340 Error_Msg_N
341 ("attempt to specify non-contiguous field "
342 & "not permitted", CLC);
343 Error_Msg_N
344 ("\caused by non-standard Bit_Order "
345 & "specified", CLC);
346 Error_Msg_N
347 ("\consider possibility of using "
348 & "Ada 2005 mode here", CLC);
349 end if;
59ac57b5 350
6797073f 351 -- Case where field fits in one storage unit
59ac57b5 352
6797073f 353 else
354 -- Give warning if suspicious component clause
59ac57b5 355
6797073f 356 if Intval (FB) >= System_Storage_Unit
357 and then Warn_On_Reverse_Bit_Order
358 then
359 Error_Msg_N
7a41db5b 360 ("info: Bit_Order clause does not affect " &
1e3532e7 361 "byte ordering?V?", Pos);
6797073f 362 Error_Msg_Uint_1 :=
363 Intval (Pos) + Intval (FB) /
364 System_Storage_Unit;
365 Error_Msg_N
7a41db5b 366 ("info: position normalized to ^ before bit " &
1e3532e7 367 "order interpreted?V?", Pos);
6797073f 368 end if;
59ac57b5 369
6797073f 370 -- Here is where we fix up the Component_Bit_Offset value
371 -- to account for the reverse bit order. Some examples of
372 -- what needs to be done are:
bfa5a9d9 373
6797073f 374 -- First_Bit .. Last_Bit Component_Bit_Offset
375 -- old new old new
59ac57b5 376
6797073f 377 -- 0 .. 0 7 .. 7 0 7
378 -- 0 .. 1 6 .. 7 0 6
379 -- 0 .. 2 5 .. 7 0 5
380 -- 0 .. 7 0 .. 7 0 4
59ac57b5 381
6797073f 382 -- 1 .. 1 6 .. 6 1 6
383 -- 1 .. 4 3 .. 6 1 3
384 -- 4 .. 7 0 .. 3 4 0
59ac57b5 385
6797073f 386 -- The rule is that the first bit is is obtained by
387 -- subtracting the old ending bit from storage_unit - 1.
59ac57b5 388
6797073f 389 Set_Component_Bit_Offset
390 (Comp,
391 (Storage_Unit_Offset * System_Storage_Unit) +
392 (System_Storage_Unit - 1) -
393 (Start_Bit + CSZ - 1));
59ac57b5 394
6797073f 395 Set_Normalized_First_Bit
396 (Comp,
397 Component_Bit_Offset (Comp) mod
398 System_Storage_Unit);
399 end if;
400 end;
401 end if;
402
403 Next_Component_Or_Discriminant (Comp);
404 end loop;
405
406 -- For Ada 2005, we do machine scalar processing, as fully described In
407 -- AI-133. This involves gathering all components which start at the
408 -- same byte offset and processing them together. Same approach is still
409 -- valid in later versions including Ada 2012.
410
411 else
412 declare
413 Max_Machine_Scalar_Size : constant Uint :=
414 UI_From_Int
415 (Standard_Long_Long_Integer_Size);
67278d60 416 -- We use this as the maximum machine scalar size
59ac57b5 417
6797073f 418 Num_CC : Natural;
419 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
59ac57b5 420
6797073f 421 begin
422 -- This first loop through components does two things. First it
423 -- deals with the case of components with component clauses whose
424 -- length is greater than the maximum machine scalar size (either
425 -- accepting them or rejecting as needed). Second, it counts the
426 -- number of components with component clauses whose length does
427 -- not exceed this maximum for later processing.
67278d60 428
6797073f 429 Num_CC := 0;
430 Comp := First_Component_Or_Discriminant (R);
431 while Present (Comp) loop
432 CC := Component_Clause (Comp);
67278d60 433
6797073f 434 if Present (CC) then
435 declare
1e3532e7 436 Fbit : constant Uint := Static_Integer (First_Bit (CC));
437 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
67278d60 438
6797073f 439 begin
b38e4131 440 -- Case of component with last bit >= max machine scalar
67278d60 441
b38e4131 442 if Lbit >= Max_Machine_Scalar_Size then
67278d60 443
b38e4131 444 -- This is allowed only if first bit is zero, and
445 -- last bit + 1 is a multiple of storage unit size.
67278d60 446
b38e4131 447 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
67278d60 448
b38e4131 449 -- This is the case to give a warning if enabled
67278d60 450
b38e4131 451 if Warn_On_Reverse_Bit_Order then
452 Error_Msg_N
7a41db5b 453 ("info: multi-byte field specified with "
1e3532e7 454 & " non-standard Bit_Order?V?", CC);
b38e4131 455
456 if Bytes_Big_Endian then
457 Error_Msg_N
458 ("\bytes are not reversed "
1e3532e7 459 & "(component is big-endian)?V?", CC);
b38e4131 460 else
461 Error_Msg_N
462 ("\bytes are not reversed "
1e3532e7 463 & "(component is little-endian)?V?", CC);
b38e4131 464 end if;
465 end if;
67278d60 466
7eb0e22f 467 -- Give error message for RM 13.5.1(10) violation
67278d60 468
b38e4131 469 else
470 Error_Msg_FE
471 ("machine scalar rules not followed for&",
472 First_Bit (CC), Comp);
67278d60 473
0c978552 474 Error_Msg_Uint_1 := Lbit + 1;
b38e4131 475 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
476 Error_Msg_F
0c978552 477 ("\last bit + 1 (^) exceeds maximum machine "
b38e4131 478 & "scalar size (^)",
479 First_Bit (CC));
67278d60 480
b38e4131 481 if (Lbit + 1) mod SSU /= 0 then
482 Error_Msg_Uint_1 := SSU;
483 Error_Msg_F
484 ("\and is not a multiple of Storage_Unit (^) "
0c978552 485 & "(RM 13.5.1(10))",
b38e4131 486 First_Bit (CC));
6797073f 487
6797073f 488 else
b38e4131 489 Error_Msg_Uint_1 := Fbit;
490 Error_Msg_F
491 ("\and first bit (^) is non-zero "
0cafb066 492 & "(RM 13.4.1(10))",
b38e4131 493 First_Bit (CC));
67278d60 494 end if;
6797073f 495 end if;
59ac57b5 496
b38e4131 497 -- OK case of machine scalar related component clause,
498 -- For now, just count them.
59ac57b5 499
6797073f 500 else
501 Num_CC := Num_CC + 1;
502 end if;
503 end;
504 end if;
59ac57b5 505
6797073f 506 Next_Component_Or_Discriminant (Comp);
507 end loop;
59ac57b5 508
6797073f 509 -- We need to sort the component clauses on the basis of the
510 -- Position values in the clause, so we can group clauses with
4a87c513 511 -- the same Position together to determine the relevant machine
6797073f 512 -- scalar size.
59ac57b5 513
6797073f 514 Sort_CC : declare
515 Comps : array (0 .. Num_CC) of Entity_Id;
516 -- Array to collect component and discriminant entities. The
517 -- data starts at index 1, the 0'th entry is for the sort
518 -- routine.
59ac57b5 519
6797073f 520 function CP_Lt (Op1, Op2 : Natural) return Boolean;
521 -- Compare routine for Sort
59ac57b5 522
6797073f 523 procedure CP_Move (From : Natural; To : Natural);
524 -- Move routine for Sort
59ac57b5 525
6797073f 526 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
59ac57b5 527
6797073f 528 Start : Natural;
529 Stop : Natural;
530 -- Start and stop positions in the component list of the set of
531 -- components with the same starting position (that constitute
532 -- components in a single machine scalar).
59ac57b5 533
6797073f 534 MaxL : Uint;
535 -- Maximum last bit value of any component in this set
59ac57b5 536
6797073f 537 MSS : Uint;
538 -- Corresponding machine scalar size
67278d60 539
6797073f 540 -----------
541 -- CP_Lt --
542 -----------
67278d60 543
6797073f 544 function CP_Lt (Op1, Op2 : Natural) return Boolean is
545 begin
546 return Position (Component_Clause (Comps (Op1))) <
547 Position (Component_Clause (Comps (Op2)));
548 end CP_Lt;
67278d60 549
6797073f 550 -------------
551 -- CP_Move --
552 -------------
67278d60 553
6797073f 554 procedure CP_Move (From : Natural; To : Natural) is
555 begin
556 Comps (To) := Comps (From);
557 end CP_Move;
67278d60 558
4a87c513 559 -- Start of processing for Sort_CC
59ac57b5 560
6797073f 561 begin
b38e4131 562 -- Collect the machine scalar relevant component clauses
59ac57b5 563
6797073f 564 Num_CC := 0;
565 Comp := First_Component_Or_Discriminant (R);
566 while Present (Comp) loop
b38e4131 567 declare
568 CC : constant Node_Id := Component_Clause (Comp);
569
570 begin
571 -- Collect only component clauses whose last bit is less
572 -- than machine scalar size. Any component clause whose
573 -- last bit exceeds this value does not take part in
574 -- machine scalar layout considerations. The test for
575 -- Error_Posted makes sure we exclude component clauses
576 -- for which we already posted an error.
577
578 if Present (CC)
579 and then not Error_Posted (Last_Bit (CC))
580 and then Static_Integer (Last_Bit (CC)) <
d64221a7 581 Max_Machine_Scalar_Size
b38e4131 582 then
583 Num_CC := Num_CC + 1;
584 Comps (Num_CC) := Comp;
585 end if;
586 end;
59ac57b5 587
6797073f 588 Next_Component_Or_Discriminant (Comp);
589 end loop;
67278d60 590
6797073f 591 -- Sort by ascending position number
67278d60 592
6797073f 593 Sorting.Sort (Num_CC);
67278d60 594
6797073f 595 -- We now have all the components whose size does not exceed
596 -- the max machine scalar value, sorted by starting position.
597 -- In this loop we gather groups of clauses starting at the
598 -- same position, to process them in accordance with AI-133.
67278d60 599
6797073f 600 Stop := 0;
601 while Stop < Num_CC loop
602 Start := Stop + 1;
603 Stop := Start;
604 MaxL :=
605 Static_Integer
606 (Last_Bit (Component_Clause (Comps (Start))));
67278d60 607 while Stop < Num_CC loop
6797073f 608 if Static_Integer
609 (Position (Component_Clause (Comps (Stop + 1)))) =
610 Static_Integer
611 (Position (Component_Clause (Comps (Stop))))
612 then
613 Stop := Stop + 1;
614 MaxL :=
615 UI_Max
616 (MaxL,
617 Static_Integer
618 (Last_Bit
619 (Component_Clause (Comps (Stop)))));
620 else
621 exit;
622 end if;
623 end loop;
67278d60 624
6797073f 625 -- Now we have a group of component clauses from Start to
626 -- Stop whose positions are identical, and MaxL is the
627 -- maximum last bit value of any of these components.
628
629 -- We need to determine the corresponding machine scalar
630 -- size. This loop assumes that machine scalar sizes are
631 -- even, and that each possible machine scalar has twice
632 -- as many bits as the next smaller one.
633
634 MSS := Max_Machine_Scalar_Size;
635 while MSS mod 2 = 0
636 and then (MSS / 2) >= SSU
637 and then (MSS / 2) > MaxL
638 loop
639 MSS := MSS / 2;
640 end loop;
67278d60 641
6797073f 642 -- Here is where we fix up the Component_Bit_Offset value
643 -- to account for the reverse bit order. Some examples of
644 -- what needs to be done for the case of a machine scalar
645 -- size of 8 are:
67278d60 646
6797073f 647 -- First_Bit .. Last_Bit Component_Bit_Offset
648 -- old new old new
67278d60 649
6797073f 650 -- 0 .. 0 7 .. 7 0 7
651 -- 0 .. 1 6 .. 7 0 6
652 -- 0 .. 2 5 .. 7 0 5
653 -- 0 .. 7 0 .. 7 0 4
67278d60 654
6797073f 655 -- 1 .. 1 6 .. 6 1 6
656 -- 1 .. 4 3 .. 6 1 3
657 -- 4 .. 7 0 .. 3 4 0
67278d60 658
6797073f 659 -- The rule is that the first bit is obtained by subtracting
660 -- the old ending bit from machine scalar size - 1.
67278d60 661
6797073f 662 for C in Start .. Stop loop
663 declare
664 Comp : constant Entity_Id := Comps (C);
b9e61b2a 665 CC : constant Node_Id := Component_Clause (Comp);
666
667 LB : constant Uint := Static_Integer (Last_Bit (CC));
6797073f 668 NFB : constant Uint := MSS - Uint_1 - LB;
669 NLB : constant Uint := NFB + Esize (Comp) - 1;
b9e61b2a 670 Pos : constant Uint := Static_Integer (Position (CC));
67278d60 671
6797073f 672 begin
673 if Warn_On_Reverse_Bit_Order then
674 Error_Msg_Uint_1 := MSS;
675 Error_Msg_N
676 ("info: reverse bit order in machine " &
1e3532e7 677 "scalar of length^?V?", First_Bit (CC));
6797073f 678 Error_Msg_Uint_1 := NFB;
679 Error_Msg_Uint_2 := NLB;
680
681 if Bytes_Big_Endian then
682 Error_Msg_NE
7a41db5b 683 ("\big-endian range for component "
684 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
6797073f 685 else
686 Error_Msg_NE
7a41db5b 687 ("\little-endian range for component"
688 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
67278d60 689 end if;
6797073f 690 end if;
67278d60 691
6797073f 692 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
693 Set_Normalized_First_Bit (Comp, NFB mod SSU);
694 end;
67278d60 695 end loop;
6797073f 696 end loop;
697 end Sort_CC;
698 end;
699 end if;
59ac57b5 700 end Adjust_Record_For_Reverse_Bit_Order;
701
1d366b32 702 -------------------------------------
703 -- Alignment_Check_For_Size_Change --
704 -------------------------------------
d6f39728 705
1d366b32 706 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
d6f39728 707 begin
708 -- If the alignment is known, and not set by a rep clause, and is
709 -- inconsistent with the size being set, then reset it to unknown,
710 -- we assume in this case that the size overrides the inherited
711 -- alignment, and that the alignment must be recomputed.
712
713 if Known_Alignment (Typ)
714 and then not Has_Alignment_Clause (Typ)
1d366b32 715 and then Size mod (Alignment (Typ) * SSU) /= 0
d6f39728 716 then
717 Init_Alignment (Typ);
718 end if;
1d366b32 719 end Alignment_Check_For_Size_Change;
d6f39728 720
06ef5f86 721 -------------------------------------
722 -- Analyze_Aspects_At_Freeze_Point --
723 -------------------------------------
724
725 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
726 ASN : Node_Id;
727 A_Id : Aspect_Id;
728 Ritem : Node_Id;
729
730 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
731 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
732 -- the aspect specification node ASN.
733
37c6e44c 734 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
735 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
736 -- a derived type can inherit aspects from its parent which have been
737 -- specified at the time of the derivation using an aspect, as in:
738 --
739 -- type A is range 1 .. 10
740 -- with Size => Not_Defined_Yet;
741 -- ..
742 -- type B is new A;
743 -- ..
744 -- Not_Defined_Yet : constant := 64;
745 --
746 -- In this example, the Size of A is considered to be specified prior
747 -- to the derivation, and thus inherited, even though the value is not
748 -- known at the time of derivation. To deal with this, we use two entity
749 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
750 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
751 -- the derived type (B here). If this flag is set when the derived type
752 -- is frozen, then this procedure is called to ensure proper inheritance
b21edad9 753 -- of all delayed aspects from the parent type. The derived type is E,
37c6e44c 754 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
755 -- aspect specification node in the Rep_Item chain for the parent type.
756
06ef5f86 757 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
758 -- Given an aspect specification node ASN whose expression is an
759 -- optional Boolean, this routines creates the corresponding pragma
760 -- at the freezing point.
761
762 ----------------------------------
763 -- Analyze_Aspect_Default_Value --
764 ----------------------------------
765
766 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
767 Ent : constant Entity_Id := Entity (ASN);
768 Expr : constant Node_Id := Expression (ASN);
769 Id : constant Node_Id := Identifier (ASN);
770
771 begin
772 Error_Msg_Name_1 := Chars (Id);
773
774 if not Is_Type (Ent) then
775 Error_Msg_N ("aspect% can only apply to a type", Id);
776 return;
777
778 elsif not Is_First_Subtype (Ent) then
779 Error_Msg_N ("aspect% cannot apply to subtype", Id);
780 return;
781
782 elsif A_Id = Aspect_Default_Value
783 and then not Is_Scalar_Type (Ent)
784 then
785 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
786 return;
787
788 elsif A_Id = Aspect_Default_Component_Value then
789 if not Is_Array_Type (Ent) then
790 Error_Msg_N ("aspect% can only be applied to array type", Id);
791 return;
792
793 elsif not Is_Scalar_Type (Component_Type (Ent)) then
794 Error_Msg_N ("aspect% requires scalar components", Id);
795 return;
796 end if;
797 end if;
798
799 Set_Has_Default_Aspect (Base_Type (Ent));
800
801 if Is_Scalar_Type (Ent) then
9f36e3fb 802 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
06ef5f86 803 else
f3d70f08 804 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
06ef5f86 805 end if;
806 end Analyze_Aspect_Default_Value;
807
37c6e44c 808 ---------------------------------
809 -- Inherit_Delayed_Rep_Aspects --
810 ---------------------------------
811
812 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
813 P : constant Entity_Id := Entity (ASN);
814 -- Entithy for parent type
815
816 N : Node_Id;
817 -- Item from Rep_Item chain
818
819 A : Aspect_Id;
820
821 begin
822 -- Loop through delayed aspects for the parent type
823
824 N := ASN;
825 while Present (N) loop
826 if Nkind (N) = N_Aspect_Specification then
827 exit when Entity (N) /= P;
828
829 if Is_Delayed_Aspect (N) then
830 A := Get_Aspect_Id (Chars (Identifier (N)));
831
832 -- Process delayed rep aspect. For Boolean attributes it is
833 -- not possible to cancel an attribute once set (the attempt
834 -- to use an aspect with xxx => False is an error) for a
835 -- derived type. So for those cases, we do not have to check
836 -- if a clause has been given for the derived type, since it
837 -- is harmless to set it again if it is already set.
838
839 case A is
840
841 -- Alignment
842
843 when Aspect_Alignment =>
844 if not Has_Alignment_Clause (E) then
845 Set_Alignment (E, Alignment (P));
846 end if;
847
848 -- Atomic
849
850 when Aspect_Atomic =>
851 if Is_Atomic (P) then
852 Set_Is_Atomic (E);
853 end if;
854
855 -- Atomic_Components
856
857 when Aspect_Atomic_Components =>
858 if Has_Atomic_Components (P) then
859 Set_Has_Atomic_Components (Base_Type (E));
860 end if;
861
862 -- Bit_Order
863
864 when Aspect_Bit_Order =>
865 if Is_Record_Type (E)
866 and then No (Get_Attribute_Definition_Clause
867 (E, Attribute_Bit_Order))
868 and then Reverse_Bit_Order (P)
869 then
870 Set_Reverse_Bit_Order (Base_Type (E));
871 end if;
872
873 -- Component_Size
874
875 when Aspect_Component_Size =>
876 if Is_Array_Type (E)
877 and then not Has_Component_Size_Clause (E)
878 then
879 Set_Component_Size
880 (Base_Type (E), Component_Size (P));
881 end if;
882
883 -- Machine_Radix
884
885 when Aspect_Machine_Radix =>
886 if Is_Decimal_Fixed_Point_Type (E)
887 and then not Has_Machine_Radix_Clause (E)
888 then
889 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
890 end if;
891
892 -- Object_Size (also Size which also sets Object_Size)
893
894 when Aspect_Object_Size | Aspect_Size =>
895 if not Has_Size_Clause (E)
896 and then
897 No (Get_Attribute_Definition_Clause
898 (E, Attribute_Object_Size))
899 then
900 Set_Esize (E, Esize (P));
901 end if;
902
903 -- Pack
904
905 when Aspect_Pack =>
906 if not Is_Packed (E) then
907 Set_Is_Packed (Base_Type (E));
908
909 if Is_Bit_Packed_Array (P) then
910 Set_Is_Bit_Packed_Array (Base_Type (E));
a88a5773 911 Set_Packed_Array_Impl_Type
912 (E, Packed_Array_Impl_Type (P));
37c6e44c 913 end if;
914 end if;
915
916 -- Scalar_Storage_Order
917
918 when Aspect_Scalar_Storage_Order =>
919 if (Is_Record_Type (E) or else Is_Array_Type (E))
920 and then No (Get_Attribute_Definition_Clause
e163cac8 921 (E, Attribute_Scalar_Storage_Order))
37c6e44c 922 and then Reverse_Storage_Order (P)
923 then
924 Set_Reverse_Storage_Order (Base_Type (E));
b64082f2 925
926 -- Clear default SSO indications, since the aspect
927 -- overrides the default.
928
929 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
930 Set_SSO_Set_High_By_Default (Base_Type (E), False);
37c6e44c 931 end if;
932
933 -- Small
934
935 when Aspect_Small =>
936 if Is_Fixed_Point_Type (E)
937 and then not Has_Small_Clause (E)
938 then
939 Set_Small_Value (E, Small_Value (P));
940 end if;
941
942 -- Storage_Size
943
944 when Aspect_Storage_Size =>
945 if (Is_Access_Type (E) or else Is_Task_Type (E))
946 and then not Has_Storage_Size_Clause (E)
947 then
948 Set_Storage_Size_Variable
949 (Base_Type (E), Storage_Size_Variable (P));
950 end if;
951
952 -- Value_Size
953
954 when Aspect_Value_Size =>
955
956 -- Value_Size is never inherited, it is either set by
957 -- default, or it is explicitly set for the derived
958 -- type. So nothing to do here.
959
960 null;
961
962 -- Volatile
963
964 when Aspect_Volatile =>
965 if Is_Volatile (P) then
966 Set_Is_Volatile (E);
967 end if;
968
2fe893b9 969 -- Volatile_Full_Access
970
971 when Aspect_Volatile_Full_Access =>
4bf2acc9 972 if Is_Volatile_Full_Access (P) then
973 Set_Is_Volatile_Full_Access (E);
2fe893b9 974 end if;
975
37c6e44c 976 -- Volatile_Components
977
978 when Aspect_Volatile_Components =>
979 if Has_Volatile_Components (P) then
980 Set_Has_Volatile_Components (Base_Type (E));
981 end if;
982
983 -- That should be all the Rep Aspects
984
985 when others =>
986 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
987 null;
988
989 end case;
990 end if;
991 end if;
992
993 N := Next_Rep_Item (N);
994 end loop;
995 end Inherit_Delayed_Rep_Aspects;
996
06ef5f86 997 -------------------------------------
998 -- Make_Pragma_From_Boolean_Aspect --
999 -------------------------------------
1000
1001 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1002 Ident : constant Node_Id := Identifier (ASN);
1003 A_Name : constant Name_Id := Chars (Ident);
1004 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1005 Ent : constant Entity_Id := Entity (ASN);
1006 Expr : constant Node_Id := Expression (ASN);
1007 Loc : constant Source_Ptr := Sloc (ASN);
1008
1009 Prag : Node_Id;
1010
1011 procedure Check_False_Aspect_For_Derived_Type;
1012 -- This procedure checks for the case of a false aspect for a derived
1013 -- type, which improperly tries to cancel an aspect inherited from
1014 -- the parent.
1015
1016 -----------------------------------------
1017 -- Check_False_Aspect_For_Derived_Type --
1018 -----------------------------------------
1019
1020 procedure Check_False_Aspect_For_Derived_Type is
1021 Par : Node_Id;
1022
1023 begin
1024 -- We are only checking derived types
1025
1026 if not Is_Derived_Type (E) then
1027 return;
1028 end if;
1029
1030 Par := Nearest_Ancestor (E);
1031
1032 case A_Id is
1033 when Aspect_Atomic | Aspect_Shared =>
1034 if not Is_Atomic (Par) then
1035 return;
1036 end if;
1037
1038 when Aspect_Atomic_Components =>
1039 if not Has_Atomic_Components (Par) then
1040 return;
1041 end if;
1042
1043 when Aspect_Discard_Names =>
1044 if not Discard_Names (Par) then
1045 return;
1046 end if;
1047
1048 when Aspect_Pack =>
1049 if not Is_Packed (Par) then
1050 return;
1051 end if;
1052
1053 when Aspect_Unchecked_Union =>
1054 if not Is_Unchecked_Union (Par) then
1055 return;
1056 end if;
1057
1058 when Aspect_Volatile =>
1059 if not Is_Volatile (Par) then
1060 return;
1061 end if;
1062
1063 when Aspect_Volatile_Components =>
1064 if not Has_Volatile_Components (Par) then
1065 return;
1066 end if;
1067
2fe893b9 1068 when Aspect_Volatile_Full_Access =>
4bf2acc9 1069 if not Is_Volatile_Full_Access (Par) then
2fe893b9 1070 return;
1071 end if;
1072
06ef5f86 1073 when others =>
1074 return;
1075 end case;
1076
1077 -- Fall through means we are canceling an inherited aspect
1078
1079 Error_Msg_Name_1 := A_Name;
37c6e44c 1080 Error_Msg_NE
1081 ("derived type& inherits aspect%, cannot cancel", Expr, E);
06ef5f86 1082 end Check_False_Aspect_For_Derived_Type;
1083
1084 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1085
1086 begin
37c6e44c 1087 -- Note that we know Expr is present, because for a missing Expr
1088 -- argument, we knew it was True and did not need to delay the
1089 -- evaluation to the freeze point.
1090
06ef5f86 1091 if Is_False (Static_Boolean (Expr)) then
1092 Check_False_Aspect_For_Derived_Type;
1093
1094 else
1095 Prag :=
1096 Make_Pragma (Loc,
1097 Pragma_Argument_Associations => New_List (
57cd943b 1098 Make_Pragma_Argument_Association (Sloc (Ident),
1099 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1100
06ef5f86 1101 Pragma_Identifier =>
1102 Make_Identifier (Sloc (Ident), Chars (Ident)));
1103
1104 Set_From_Aspect_Specification (Prag, True);
1105 Set_Corresponding_Aspect (Prag, ASN);
1106 Set_Aspect_Rep_Item (ASN, Prag);
1107 Set_Is_Delayed_Aspect (Prag);
1108 Set_Parent (Prag, ASN);
1109 end if;
06ef5f86 1110 end Make_Pragma_From_Boolean_Aspect;
1111
1112 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1113
1114 begin
29a9d4be 1115 -- Must be visible in current scope
06ef5f86 1116
ace3389d 1117 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
06ef5f86 1118 return;
1119 end if;
1120
1121 -- Look for aspect specification entries for this entity
1122
1123 ASN := First_Rep_Item (E);
06ef5f86 1124 while Present (ASN) loop
37c6e44c 1125 if Nkind (ASN) = N_Aspect_Specification then
1126 exit when Entity (ASN) /= E;
06ef5f86 1127
37c6e44c 1128 if Is_Delayed_Aspect (ASN) then
1129 A_Id := Get_Aspect_Id (ASN);
1130
1131 case A_Id is
e4c87fa5 1132
37c6e44c 1133 -- For aspects whose expression is an optional Boolean, make
7d6fb253 1134 -- the corresponding pragma at the freeze point.
06ef5f86 1135
7d6fb253 1136 when Boolean_Aspects |
1137 Library_Unit_Aspects =>
1138 Make_Pragma_From_Boolean_Aspect (ASN);
06ef5f86 1139
37c6e44c 1140 -- Special handling for aspects that don't correspond to
1141 -- pragmas/attributes.
06ef5f86 1142
7d6fb253 1143 when Aspect_Default_Value |
1144 Aspect_Default_Component_Value =>
81c2bc19 1145
1146 -- Do not inherit aspect for anonymous base type of a
1147 -- scalar or array type, because they apply to the first
1148 -- subtype of the type, and will be processed when that
1149 -- first subtype is frozen.
1150
1151 if Is_Derived_Type (E)
1152 and then not Comes_From_Source (E)
1153 and then E /= First_Subtype (E)
1154 then
1155 null;
1156 else
1157 Analyze_Aspect_Default_Value (ASN);
1158 end if;
06ef5f86 1159
37c6e44c 1160 -- Ditto for iterator aspects, because the corresponding
1161 -- attributes may not have been analyzed yet.
af9fed8f 1162
7d6fb253 1163 when Aspect_Constant_Indexing |
1164 Aspect_Variable_Indexing |
1165 Aspect_Default_Iterator |
1166 Aspect_Iterator_Element =>
1167 Analyze (Expression (ASN));
af9fed8f 1168
7d6fb253 1169 if Etype (Expression (ASN)) = Any_Type then
1170 Error_Msg_NE
1171 ("\aspect must be fully defined before & is frozen",
1172 ASN, E);
1173 end if;
b3f8228a 1174
7d6fb253 1175 when Aspect_Iterable =>
1176 Validate_Iterable_Aspect (E, ASN);
1177
1178 when others =>
1179 null;
37c6e44c 1180 end case;
06ef5f86 1181
37c6e44c 1182 Ritem := Aspect_Rep_Item (ASN);
06ef5f86 1183
37c6e44c 1184 if Present (Ritem) then
1185 Analyze (Ritem);
1186 end if;
06ef5f86 1187 end if;
1188 end if;
1189
1190 Next_Rep_Item (ASN);
1191 end loop;
37c6e44c 1192
1193 -- This is where we inherit delayed rep aspects from our parent. Note
1194 -- that if we fell out of the above loop with ASN non-empty, it means
1195 -- we hit an aspect for an entity other than E, and it must be the
1196 -- type from which we were derived.
1197
1198 if May_Inherit_Delayed_Rep_Aspects (E) then
1199 Inherit_Delayed_Rep_Aspects (ASN);
1200 end if;
06ef5f86 1201 end Analyze_Aspects_At_Freeze_Point;
1202
ae888dbd 1203 -----------------------------------
1204 -- Analyze_Aspect_Specifications --
1205 -----------------------------------
1206
21ea3a4f 1207 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
e2bf777d 1208 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
6c5793cd 1209 -- Establish linkages between an aspect and its corresponding pragma
5ddd846b 1210
50e44732 1211 procedure Insert_After_SPARK_Mode
1212 (Prag : Node_Id;
1213 Ins_Nod : Node_Id;
1214 Decls : List_Id);
3dbe7a69 1215 -- Subsidiary to the analysis of aspects Abstract_State, Ghost,
1216 -- Initializes, Initial_Condition and Refined_State. Insert node Prag
1217 -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip
1218 -- SPARK_Mode. Decls is the associated declarative list where Prag is to
1219 -- reside.
e2bf777d 1220
1221 procedure Insert_Pragma (Prag : Node_Id);
1222 -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases,
1223 -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global.
1224 -- Insert pragma Prag such that it mimics the placement of a source
1225 -- pragma of the same kind.
1226 --
1227 -- procedure Proc (Formal : ...) with Global => ...;
1228 --
1229 -- procedure Proc (Formal : ...);
1230 -- pragma Global (...);
1231
1232 --------------
1233 -- Decorate --
1234 --------------
1235
1236 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
5ddd846b 1237 begin
6c5793cd 1238 Set_Aspect_Rep_Item (Asp, Prag);
5ddd846b 1239 Set_Corresponding_Aspect (Prag, Asp);
1240 Set_From_Aspect_Specification (Prag);
5ddd846b 1241 Set_Parent (Prag, Asp);
e2bf777d 1242 end Decorate;
f0813d71 1243
50e44732 1244 -----------------------------
1245 -- Insert_After_SPARK_Mode --
1246 -----------------------------
1247
1248 procedure Insert_After_SPARK_Mode
1249 (Prag : Node_Id;
1250 Ins_Nod : Node_Id;
1251 Decls : List_Id)
1252 is
1253 Decl : Node_Id := Ins_Nod;
1254
1255 begin
1256 -- Skip SPARK_Mode
1257
1258 if Present (Decl)
1259 and then Nkind (Decl) = N_Pragma
1260 and then Pragma_Name (Decl) = Name_SPARK_Mode
1261 then
1262 Decl := Next (Decl);
1263 end if;
1264
1265 if Present (Decl) then
1266 Insert_Before (Decl, Prag);
1267
1268 -- Aitem acts as the last declaration
1269
1270 else
1271 Append_To (Decls, Prag);
1272 end if;
1273 end Insert_After_SPARK_Mode;
1274
e2bf777d 1275 -------------------
1276 -- Insert_Pragma --
1277 -------------------
c1006d6d 1278
e2bf777d 1279 procedure Insert_Pragma (Prag : Node_Id) is
1280 Aux : Node_Id;
1281 Decl : Node_Id;
c1006d6d 1282
1283 begin
ed695684 1284 if Nkind (N) = N_Subprogram_Body then
e2bf777d 1285 if Present (Declarations (N)) then
d324c418 1286
e2bf777d 1287 -- Skip other internally generated pragmas from aspects to find
1288 -- the proper insertion point. As a result the order of pragmas
1289 -- is the same as the order of aspects.
d324c418 1290
607bc8f5 1291 -- As precondition pragmas generated from conjuncts in the
1292 -- precondition aspect are presented in reverse order to
1293 -- Insert_Pragma, insert them in the correct order here by not
1294 -- skipping previously inserted precondition pragmas when the
1295 -- current pragma is a precondition.
1296
e2bf777d 1297 Decl := First (Declarations (N));
1298 while Present (Decl) loop
1299 if Nkind (Decl) = N_Pragma
1300 and then From_Aspect_Specification (Decl)
607bc8f5 1301 and then not (Get_Pragma_Id (Decl) = Pragma_Precondition
1302 and then
1303 Get_Pragma_Id (Prag) = Pragma_Precondition)
e2bf777d 1304 then
1305 Next (Decl);
d324c418 1306 else
e2bf777d 1307 exit;
d324c418 1308 end if;
e2bf777d 1309 end loop;
1310
1311 if Present (Decl) then
1312 Insert_Before (Decl, Prag);
1313 else
1314 Append (Prag, Declarations (N));
1315 end if;
1316 else
1317 Set_Declarations (N, New_List (Prag));
d324c418 1318 end if;
c1006d6d 1319
ed695684 1320 -- When the context is a library unit, the pragma is added to the
1321 -- Pragmas_After list.
1322
1323 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1324 Aux := Aux_Decls_Node (Parent (N));
1325
1326 if No (Pragmas_After (Aux)) then
1327 Set_Pragmas_After (Aux, New_List);
1328 end if;
1329
1330 Prepend (Prag, Pragmas_After (Aux));
1331
c1006d6d 1332 -- Default
1333
1334 else
1335 Insert_After (N, Prag);
c1006d6d 1336 end if;
e2bf777d 1337 end Insert_Pragma;
c1006d6d 1338
1339 -- Local variables
1340
ae888dbd 1341 Aspect : Node_Id;
d74fc39a 1342 Aitem : Node_Id;
ae888dbd 1343 Ent : Node_Id;
ae888dbd 1344
21ea3a4f 1345 L : constant List_Id := Aspect_Specifications (N);
1346
ae888dbd 1347 Ins_Node : Node_Id := N;
89f1e35c 1348 -- Insert pragmas/attribute definition clause after this node when no
1349 -- delayed analysis is required.
d74fc39a 1350
f0813d71 1351 -- Start of processing for Analyze_Aspect_Specifications
1352
d74fc39a 1353 -- The general processing involves building an attribute definition
89f1e35c 1354 -- clause or a pragma node that corresponds to the aspect. Then in order
1355 -- to delay the evaluation of this aspect to the freeze point, we attach
1356 -- the corresponding pragma/attribute definition clause to the aspect
1357 -- specification node, which is then placed in the Rep Item chain. In
1358 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1359 -- and we evaluate the rep item at the freeze point. When the aspect
1360 -- doesn't have a corresponding pragma/attribute definition clause, then
1361 -- its analysis is simply delayed at the freeze point.
1362
1363 -- Some special cases don't require delay analysis, thus the aspect is
1364 -- analyzed right now.
1365
51ea9c94 1366 -- Note that there is a special handling for Pre, Post, Test_Case,
e66f4e2a 1367 -- Contract_Cases aspects. In these cases, we do not have to worry
51ea9c94 1368 -- about delay issues, since the pragmas themselves deal with delay
1369 -- of visibility for the expression analysis. Thus, we just insert
1370 -- the pragma after the node N.
ae888dbd 1371
1372 begin
21ea3a4f 1373 pragma Assert (Present (L));
1374
6fb3c314 1375 -- Loop through aspects
f93e7257 1376
ae888dbd 1377 Aspect := First (L);
21ea3a4f 1378 Aspect_Loop : while Present (Aspect) loop
0fd13d32 1379 Analyze_One_Aspect : declare
94153a42 1380 Expr : constant Node_Id := Expression (Aspect);
89f1e35c 1381 Id : constant Node_Id := Identifier (Aspect);
1382 Loc : constant Source_Ptr := Sloc (Aspect);
94153a42 1383 Nam : constant Name_Id := Chars (Id);
1384 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
ae888dbd 1385 Anod : Node_Id;
1386
37c6e44c 1387 Delay_Required : Boolean;
89f1e35c 1388 -- Set False if delay is not required
1389
c0793fff 1390 Eloc : Source_Ptr := No_Location;
1391 -- Source location of expression, modified when we split PPC's. It
1392 -- is set below when Expr is present.
39e1f22f 1393
89f1e35c 1394 procedure Analyze_Aspect_External_Or_Link_Name;
0fd13d32 1395 -- Perform analysis of the External_Name or Link_Name aspects
21ea3a4f 1396
89f1e35c 1397 procedure Analyze_Aspect_Implicit_Dereference;
9ab32fe9 1398 -- Perform analysis of the Implicit_Dereference aspects
0fd13d32 1399
1400 procedure Make_Aitem_Pragma
1401 (Pragma_Argument_Associations : List_Id;
1402 Pragma_Name : Name_Id);
1403 -- This is a wrapper for Make_Pragma used for converting aspects
1404 -- to pragmas. It takes care of Sloc (set from Loc) and building
1405 -- the pragma identifier from the given name. In addition the
1406 -- flags Class_Present and Split_PPC are set from the aspect
1407 -- node, as well as Is_Ignored. This routine also sets the
1408 -- From_Aspect_Specification in the resulting pragma node to
1409 -- True, and sets Corresponding_Aspect to point to the aspect.
1410 -- The resulting pragma is assigned to Aitem.
21ea3a4f 1411
89f1e35c 1412 ------------------------------------------
1413 -- Analyze_Aspect_External_Or_Link_Name --
1414 ------------------------------------------
1415
1416 procedure Analyze_Aspect_External_Or_Link_Name is
21ea3a4f 1417 begin
89f1e35c 1418 -- Verify that there is an Import/Export aspect defined for the
1419 -- entity. The processing of that aspect in turn checks that
1420 -- there is a Convention aspect declared. The pragma is
1421 -- constructed when processing the Convention aspect.
21ea3a4f 1422
89f1e35c 1423 declare
1424 A : Node_Id;
21ea3a4f 1425
89f1e35c 1426 begin
1427 A := First (L);
89f1e35c 1428 while Present (A) loop
18393965 1429 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1430 Name_Import);
89f1e35c 1431 Next (A);
1432 end loop;
21ea3a4f 1433
89f1e35c 1434 if No (A) then
1435 Error_Msg_N
51ea9c94 1436 ("missing Import/Export for Link/External name",
8a1e3cde 1437 Aspect);
89f1e35c 1438 end if;
1439 end;
1440 end Analyze_Aspect_External_Or_Link_Name;
21ea3a4f 1441
89f1e35c 1442 -----------------------------------------
1443 -- Analyze_Aspect_Implicit_Dereference --
1444 -----------------------------------------
21ea3a4f 1445
89f1e35c 1446 procedure Analyze_Aspect_Implicit_Dereference is
1ff43c00 1447 Disc : Entity_Id;
1448 Parent_Disc : Entity_Id;
1449
89f1e35c 1450 begin
b9e61b2a 1451 if not Is_Type (E) or else not Has_Discriminants (E) then
89f1e35c 1452 Error_Msg_N
1ff43c00 1453 ("aspect must apply to a type with discriminants", Expr);
21ea3a4f 1454
1ff43c00 1455 elsif not Is_Entity_Name (Expr) then
1456 Error_Msg_N
1457 ("aspect must name a discriminant of current type", Expr);
21ea3a4f 1458
1ff43c00 1459 else
1460 Disc := First_Discriminant (E);
1461 while Present (Disc) loop
1462 if Chars (Expr) = Chars (Disc)
1463 and then Ekind (Etype (Disc)) =
1464 E_Anonymous_Access_Type
1465 then
1466 Set_Has_Implicit_Dereference (E);
1467 Set_Has_Implicit_Dereference (Disc);
1468 exit;
1469 end if;
21ea3a4f 1470
1ff43c00 1471 Next_Discriminant (Disc);
1472 end loop;
21ea3a4f 1473
9b5b11fb 1474 -- Error if no proper access discriminant
21ea3a4f 1475
1ff43c00 1476 if No (Disc) then
89f1e35c 1477 Error_Msg_NE
1478 ("not an access discriminant of&", Expr, E);
1ff43c00 1479 return;
1480 end if;
1481 end if;
1482
9b5b11fb 1483 -- For a type extension, check whether parent has a
1484 -- reference discriminant, to verify that use is proper.
1485
1ff43c00 1486 if Is_Derived_Type (E)
1487 and then Has_Discriminants (Etype (E))
1488 then
1489 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1490
1491 if Present (Parent_Disc)
1492 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1493 then
1494 Error_Msg_N ("reference discriminant does not match " &
1495 "discriminant of parent type", Expr);
1496 end if;
89f1e35c 1497 end if;
1498 end Analyze_Aspect_Implicit_Dereference;
21ea3a4f 1499
0fd13d32 1500 -----------------------
1501 -- Make_Aitem_Pragma --
1502 -----------------------
1503
1504 procedure Make_Aitem_Pragma
1505 (Pragma_Argument_Associations : List_Id;
1506 Pragma_Name : Name_Id)
1507 is
b855559d 1508 Args : List_Id := Pragma_Argument_Associations;
1509
0fd13d32 1510 begin
1511 -- We should never get here if aspect was disabled
1512
1513 pragma Assert (not Is_Disabled (Aspect));
1514
056dc987 1515 -- Certain aspects allow for an optional name or expression. Do
1516 -- not generate a pragma with empty argument association list.
b855559d 1517
1518 if No (Args) or else No (Expression (First (Args))) then
1519 Args := No_List;
1520 end if;
1521
0fd13d32 1522 -- Build the pragma
1523
1524 Aitem :=
1525 Make_Pragma (Loc,
b855559d 1526 Pragma_Argument_Associations => Args,
0fd13d32 1527 Pragma_Identifier =>
1528 Make_Identifier (Sloc (Id), Pragma_Name),
9ab32fe9 1529 Class_Present => Class_Present (Aspect),
1530 Split_PPC => Split_PPC (Aspect));
0fd13d32 1531
1532 -- Set additional semantic fields
1533
1534 if Is_Ignored (Aspect) then
1535 Set_Is_Ignored (Aitem);
57d8d1f3 1536 elsif Is_Checked (Aspect) then
a5109493 1537 Set_Is_Checked (Aitem);
0fd13d32 1538 end if;
1539
1540 Set_Corresponding_Aspect (Aitem, Aspect);
1541 Set_From_Aspect_Specification (Aitem, True);
1542 end Make_Aitem_Pragma;
1543
1544 -- Start of processing for Analyze_One_Aspect
1545
ae888dbd 1546 begin
2d1acfa7 1547 -- Skip aspect if already analyzed, to avoid looping in some cases
fb7f2fc4 1548
1549 if Analyzed (Aspect) then
1550 goto Continue;
1551 end if;
1552
ef957022 1553 -- Skip looking at aspect if it is totally disabled. Just mark it
1554 -- as such for later reference in the tree. This also sets the
1555 -- Is_Ignored and Is_Checked flags appropriately.
51ea9c94 1556
1557 Check_Applicable_Policy (Aspect);
1558
1559 if Is_Disabled (Aspect) then
1560 goto Continue;
1561 end if;
1562
c0793fff 1563 -- Set the source location of expression, used in the case of
1564 -- a failed precondition/postcondition or invariant. Note that
1565 -- the source location of the expression is not usually the best
1566 -- choice here. For example, it gets located on the last AND
1567 -- keyword in a chain of boolean expressiond AND'ed together.
1568 -- It is best to put the message on the first character of the
1569 -- assertion, which is the effect of the First_Node call here.
1570
1571 if Present (Expr) then
1572 Eloc := Sloc (First_Node (Expr));
1573 end if;
1574
d7ed83a2 1575 -- Check restriction No_Implementation_Aspect_Specifications
1576
c171e1be 1577 if Implementation_Defined_Aspect (A_Id) then
d7ed83a2 1578 Check_Restriction
1579 (No_Implementation_Aspect_Specifications, Aspect);
1580 end if;
1581
1582 -- Check restriction No_Specification_Of_Aspect
1583
1584 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1585
f67ed4f5 1586 -- Mark aspect analyzed (actual analysis is delayed till later)
d7ed83a2 1587
fb7f2fc4 1588 Set_Analyzed (Aspect);
d74fc39a 1589 Set_Entity (Aspect, E);
1590 Ent := New_Occurrence_Of (E, Sloc (Id));
1591
1e3c4ae6 1592 -- Check for duplicate aspect. Note that the Comes_From_Source
1593 -- test allows duplicate Pre/Post's that we generate internally
1594 -- to escape being flagged here.
ae888dbd 1595
6c545057 1596 if No_Duplicates_Allowed (A_Id) then
1597 Anod := First (L);
1598 while Anod /= Aspect loop
c171e1be 1599 if Comes_From_Source (Aspect)
1600 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
6c545057 1601 then
1602 Error_Msg_Name_1 := Nam;
1603 Error_Msg_Sloc := Sloc (Anod);
39e1f22f 1604
6c545057 1605 -- Case of same aspect specified twice
39e1f22f 1606
6c545057 1607 if Class_Present (Anod) = Class_Present (Aspect) then
1608 if not Class_Present (Anod) then
1609 Error_Msg_NE
1610 ("aspect% for & previously given#",
1611 Id, E);
1612 else
1613 Error_Msg_NE
1614 ("aspect `%''Class` for & previously given#",
1615 Id, E);
1616 end if;
39e1f22f 1617 end if;
6c545057 1618 end if;
ae888dbd 1619
6c545057 1620 Next (Anod);
1621 end loop;
1622 end if;
ae888dbd 1623
4db325e6 1624 -- Check some general restrictions on language defined aspects
1625
c171e1be 1626 if not Implementation_Defined_Aspect (A_Id) then
4db325e6 1627 Error_Msg_Name_1 := Nam;
1628
1629 -- Not allowed for renaming declarations
1630
1631 if Nkind (N) in N_Renaming_Declaration then
1632 Error_Msg_N
1633 ("aspect % not allowed for renaming declaration",
1634 Aspect);
1635 end if;
1636
1637 -- Not allowed for formal type declarations
1638
1639 if Nkind (N) = N_Formal_Type_Declaration then
1640 Error_Msg_N
1641 ("aspect % not allowed for formal type declaration",
1642 Aspect);
1643 end if;
1644 end if;
1645
7d20685d 1646 -- Copy expression for later processing by the procedures
1647 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1648
1649 Set_Entity (Id, New_Copy_Tree (Expr));
1650
37c6e44c 1651 -- Set Delay_Required as appropriate to aspect
1652
1653 case Aspect_Delay (A_Id) is
1654 when Always_Delay =>
1655 Delay_Required := True;
1656
1657 when Never_Delay =>
1658 Delay_Required := False;
1659
1660 when Rep_Aspect =>
1661
1662 -- If expression has the form of an integer literal, then
1663 -- do not delay, since we know the value cannot change.
1664 -- This optimization catches most rep clause cases.
1665
e43fc5c5 1666 -- For Boolean aspects, don't delay if no expression
1667
1668 if A_Id in Boolean_Aspects and then No (Expr) then
1669 Delay_Required := False;
1670
1671 -- For non-Boolean aspects, don't delay if integer literal
1672
1673 elsif A_Id not in Boolean_Aspects
1674 and then Present (Expr)
1675 and then Nkind (Expr) = N_Integer_Literal
1676 then
1677 Delay_Required := False;
1678
1679 -- All other cases are delayed
1680
1681 else
1682 Delay_Required := True;
1683 Set_Has_Delayed_Rep_Aspects (E);
1684 end if;
37c6e44c 1685 end case;
1686
ae888dbd 1687 -- Processing based on specific aspect
1688
d74fc39a 1689 case A_Id is
aa2f48d2 1690 when Aspect_Unimplemented =>
1691 null; -- ??? temp for now
ae888dbd 1692
1693 -- No_Aspect should be impossible
1694
1695 when No_Aspect =>
1696 raise Program_Error;
1697
89f1e35c 1698 -- Case 1: Aspects corresponding to attribute definition
1699 -- clauses.
ae888dbd 1700
b7b74740 1701 when Aspect_Address |
1702 Aspect_Alignment |
1703 Aspect_Bit_Order |
1704 Aspect_Component_Size |
89f1e35c 1705 Aspect_Constant_Indexing |
89f1e35c 1706 Aspect_Default_Iterator |
1707 Aspect_Dispatching_Domain |
b7b74740 1708 Aspect_External_Tag |
1709 Aspect_Input |
b3f8228a 1710 Aspect_Iterable |
89f1e35c 1711 Aspect_Iterator_Element |
b7b74740 1712 Aspect_Machine_Radix |
1713 Aspect_Object_Size |
1714 Aspect_Output |
1715 Aspect_Read |
1716 Aspect_Scalar_Storage_Order |
1717 Aspect_Size |
1718 Aspect_Small |
1719 Aspect_Simple_Storage_Pool |
1720 Aspect_Storage_Pool |
b7b74740 1721 Aspect_Stream_Size |
1722 Aspect_Value_Size |
89f1e35c 1723 Aspect_Variable_Indexing |
b7b74740 1724 Aspect_Write =>
d74fc39a 1725
89f1e35c 1726 -- Indexing aspects apply only to tagged type
1727
1728 if (A_Id = Aspect_Constant_Indexing
37c6e44c 1729 or else
1730 A_Id = Aspect_Variable_Indexing)
89f1e35c 1731 and then not (Is_Type (E)
1732 and then Is_Tagged_Type (E))
1733 then
05987af3 1734 Error_Msg_N
1735 ("indexing aspect can only apply to a tagged type",
3f4c9ffc 1736 Aspect);
89f1e35c 1737 goto Continue;
1738 end if;
1739
39616053 1740 -- For the case of aspect Address, we don't consider that we
588e7f97 1741 -- know the entity is never set in the source, since it is
1742 -- is likely aliasing is occurring.
1743
1744 -- Note: one might think that the analysis of the resulting
1745 -- attribute definition clause would take care of that, but
1746 -- that's not the case since it won't be from source.
1747
1748 if A_Id = Aspect_Address then
1749 Set_Never_Set_In_Source (E, False);
1750 end if;
1751
5ac76cee 1752 -- Correctness of the profile of a stream operation is
1753 -- verified at the freeze point, but we must detect the
1754 -- illegal specification of this aspect for a subtype now,
1755 -- to prevent malformed rep_item chains.
1756
fbf4d6ef 1757 if A_Id = Aspect_Input or else
1758 A_Id = Aspect_Output or else
1759 A_Id = Aspect_Read or else
1760 A_Id = Aspect_Write
5ac76cee 1761 then
fbf4d6ef 1762 if not Is_First_Subtype (E) then
1763 Error_Msg_N
1764 ("local name must be a first subtype", Aspect);
1765 goto Continue;
1766
1767 -- If stream aspect applies to the class-wide type,
1768 -- the generated attribute definition applies to the
1769 -- class-wide type as well.
1770
1771 elsif Class_Present (Aspect) then
1772 Ent :=
1773 Make_Attribute_Reference (Loc,
1774 Prefix => Ent,
1775 Attribute_Name => Name_Class);
1776 end if;
5ac76cee 1777 end if;
1778
d74fc39a 1779 -- Construct the attribute definition clause
1780
1781 Aitem :=
94153a42 1782 Make_Attribute_Definition_Clause (Loc,
d74fc39a 1783 Name => Ent,
ae888dbd 1784 Chars => Chars (Id),
1785 Expression => Relocate_Node (Expr));
1786
af9a0cc3 1787 -- If the address is specified, then we treat the entity as
41f06abf 1788 -- referenced, to avoid spurious warnings. This is analogous
1789 -- to what is done with an attribute definition clause, but
1790 -- here we don't want to generate a reference because this
1791 -- is the point of definition of the entity.
1792
1793 if A_Id = Aspect_Address then
1794 Set_Referenced (E);
1795 end if;
1796
51ea9c94 1797 -- Case 2: Aspects corresponding to pragmas
d74fc39a 1798
89f1e35c 1799 -- Case 2a: Aspects corresponding to pragmas with two
1800 -- arguments, where the first argument is a local name
1801 -- referring to the entity, and the second argument is the
1802 -- aspect definition expression.
ae888dbd 1803
04ae062f 1804 -- Linker_Section/Suppress/Unsuppress
0fd13d32 1805
04ae062f 1806 when Aspect_Linker_Section |
1807 Aspect_Suppress |
1808 Aspect_Unsuppress =>
ae888dbd 1809
0fd13d32 1810 Make_Aitem_Pragma
1811 (Pragma_Argument_Associations => New_List (
1812 Make_Pragma_Argument_Association (Loc,
1813 Expression => New_Occurrence_Of (E, Loc)),
1814 Make_Pragma_Argument_Association (Sloc (Expr),
1815 Expression => Relocate_Node (Expr))),
1816 Pragma_Name => Chars (Id));
57cd943b 1817
0fd13d32 1818 -- Synchronization
d74fc39a 1819
0fd13d32 1820 -- Corresponds to pragma Implemented, construct the pragma
49213728 1821
5bbfbad2 1822 when Aspect_Synchronization =>
0fd13d32 1823 Make_Aitem_Pragma
1824 (Pragma_Argument_Associations => New_List (
1825 Make_Pragma_Argument_Association (Loc,
1826 Expression => New_Occurrence_Of (E, Loc)),
1827 Make_Pragma_Argument_Association (Sloc (Expr),
1828 Expression => Relocate_Node (Expr))),
1829 Pragma_Name => Name_Implemented);
49213728 1830
e2bf777d 1831 -- Attach_Handler
0fd13d32 1832
89f1e35c 1833 when Aspect_Attach_Handler =>
0fd13d32 1834 Make_Aitem_Pragma
1835 (Pragma_Argument_Associations => New_List (
1836 Make_Pragma_Argument_Association (Sloc (Ent),
1837 Expression => Ent),
1838 Make_Pragma_Argument_Association (Sloc (Expr),
1839 Expression => Relocate_Node (Expr))),
1840 Pragma_Name => Name_Attach_Handler);
1841
f67ed4f5 1842 -- We need to insert this pragma into the tree to get proper
1843 -- processing and to look valid from a placement viewpoint.
1844
e2bf777d 1845 Insert_Pragma (Aitem);
f67ed4f5 1846 goto Continue;
1847
0fd13d32 1848 -- Dynamic_Predicate, Predicate, Static_Predicate
89f1e35c 1849
1850 when Aspect_Dynamic_Predicate |
1851 Aspect_Predicate |
1852 Aspect_Static_Predicate =>
1853
a47ce82d 1854 -- These aspects apply only to subtypes
1855
1856 if not Is_Type (E) then
1857 Error_Msg_N
1858 ("predicate can only be specified for a subtype",
1859 Aspect);
1860 goto Continue;
7c0c95b8 1861
1862 elsif Is_Incomplete_Type (E) then
1863 Error_Msg_N
1864 ("predicate cannot apply to incomplete view", Aspect);
1865 goto Continue;
a47ce82d 1866 end if;
1867
89f1e35c 1868 -- Construct the pragma (always a pragma Predicate, with
51ea9c94 1869 -- flags recording whether it is static/dynamic). We also
1870 -- set flags recording this in the type itself.
89f1e35c 1871
0fd13d32 1872 Make_Aitem_Pragma
1873 (Pragma_Argument_Associations => New_List (
1874 Make_Pragma_Argument_Association (Sloc (Ent),
1875 Expression => Ent),
1876 Make_Pragma_Argument_Association (Sloc (Expr),
1877 Expression => Relocate_Node (Expr))),
1878 Pragma_Name => Name_Predicate);
89f1e35c 1879
51ea9c94 1880 -- Mark type has predicates, and remember what kind of
1881 -- aspect lead to this predicate (we need this to access
1882 -- the right set of check policies later on).
1883
1884 Set_Has_Predicates (E);
1885
1886 if A_Id = Aspect_Dynamic_Predicate then
1887 Set_Has_Dynamic_Predicate_Aspect (E);
1888 elsif A_Id = Aspect_Static_Predicate then
1889 Set_Has_Static_Predicate_Aspect (E);
1890 end if;
1891
89f1e35c 1892 -- If the type is private, indicate that its completion
6653b695 1893 -- has a freeze node, because that is the one that will
1894 -- be visible at freeze time.
89f1e35c 1895
0fd13d32 1896 if Is_Private_Type (E) and then Present (Full_View (E)) then
89f1e35c 1897 Set_Has_Predicates (Full_View (E));
51ea9c94 1898
1899 if A_Id = Aspect_Dynamic_Predicate then
1900 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1901 elsif A_Id = Aspect_Static_Predicate then
1902 Set_Has_Static_Predicate_Aspect (Full_View (E));
1903 end if;
1904
89f1e35c 1905 Set_Has_Delayed_Aspects (Full_View (E));
1906 Ensure_Freeze_Node (Full_View (E));
1907 end if;
1908
1909 -- Case 2b: Aspects corresponding to pragmas with two
1910 -- arguments, where the second argument is a local name
1911 -- referring to the entity, and the first argument is the
1912 -- aspect definition expression.
ae888dbd 1913
0fd13d32 1914 -- Convention
1915
a5a64273 1916 when Aspect_Convention =>
1917
1918 -- The aspect may be part of the specification of an import
1919 -- or export pragma. Scan the aspect list to gather the
1920 -- other components, if any. The name of the generated
1921 -- pragma is one of Convention/Import/Export.
1922
1923 declare
97bf66e6 1924 Args : constant List_Id := New_List (
1925 Make_Pragma_Argument_Association (Sloc (Expr),
1926 Expression => Relocate_Node (Expr)),
1927 Make_Pragma_Argument_Association (Sloc (Ent),
1928 Expression => Ent));
1929
1930 Imp_Exp_Seen : Boolean := False;
1931 -- Flag set when aspect Import or Export has been seen
1932
1933 Imp_Seen : Boolean := False;
1934 -- Flag set when aspect Import has been seen
1935
1936 Asp : Node_Id;
1937 Asp_Nam : Name_Id;
1938 Extern_Arg : Node_Id;
1939 Link_Arg : Node_Id;
1940 Prag_Nam : Name_Id;
a5a64273 1941
1942 begin
97bf66e6 1943 Extern_Arg := Empty;
1944 Link_Arg := Empty;
1945 Prag_Nam := Chars (Id);
1946
1947 Asp := First (L);
1948 while Present (Asp) loop
1949 Asp_Nam := Chars (Identifier (Asp));
1950
1951 -- Aspects Import and Export take precedence over
1952 -- aspect Convention. As a result the generated pragma
1953 -- must carry the proper interfacing aspect's name.
1954
1955 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1956 if Imp_Exp_Seen then
1957 Error_Msg_N ("conflicting", Asp);
a5a64273 1958 else
97bf66e6 1959 Imp_Exp_Seen := True;
1960
1961 if Asp_Nam = Name_Import then
1962 Imp_Seen := True;
1963 end if;
a5a64273 1964 end if;
1965
97bf66e6 1966 Prag_Nam := Asp_Nam;
a5a64273 1967
97bf66e6 1968 -- Aspect External_Name adds an extra argument to the
1969 -- generated pragma.
1970
1971 elsif Asp_Nam = Name_External_Name then
1972 Extern_Arg :=
4bba0a8d 1973 Make_Pragma_Argument_Association (Loc,
97bf66e6 1974 Chars => Asp_Nam,
1975 Expression => Relocate_Node (Expression (Asp)));
1976
1977 -- Aspect Link_Name adds an extra argument to the
1978 -- generated pragma.
a5a64273 1979
97bf66e6 1980 elsif Asp_Nam = Name_Link_Name then
1981 Link_Arg :=
4bba0a8d 1982 Make_Pragma_Argument_Association (Loc,
97bf66e6 1983 Chars => Asp_Nam,
1984 Expression => Relocate_Node (Expression (Asp)));
a5a64273 1985 end if;
1986
97bf66e6 1987 Next (Asp);
a5a64273 1988 end loop;
1989
97bf66e6 1990 -- Assemble the full argument list
b9e61b2a 1991
97bf66e6 1992 if Present (Extern_Arg) then
1993 Append_To (Args, Extern_Arg);
a5a64273 1994 end if;
1995
8a1e3cde 1996 if Present (Link_Arg) then
1997 Append_To (Args, Link_Arg);
1998 end if;
1999
0fd13d32 2000 Make_Aitem_Pragma
97bf66e6 2001 (Pragma_Argument_Associations => Args,
2002 Pragma_Name => Prag_Nam);
2003
2004 -- Store the generated pragma Import in the related
2005 -- subprogram.
2006
2007 if Imp_Seen and then Is_Subprogram (E) then
2008 Set_Import_Pragma (E, Aitem);
2009 end if;
a5a64273 2010 end;
e1cedbae 2011
0fd13d32 2012 -- CPU, Interrupt_Priority, Priority
2013
d6814978 2014 -- These three aspects can be specified for a subprogram spec
2015 -- or body, in which case we analyze the expression and export
2016 -- the value of the aspect.
2017
2018 -- Previously, we generated an equivalent pragma for bodies
2019 -- (note that the specs cannot contain these pragmas). The
2020 -- pragma was inserted ahead of local declarations, rather than
2021 -- after the body. This leads to a certain duplication between
2022 -- the processing performed for the aspect and the pragma, but
2023 -- given the straightforward handling required it is simpler
2024 -- to duplicate than to translate the aspect in the spec into
2025 -- a pragma in the declarative part of the body.
3a72f9c3 2026
2027 when Aspect_CPU |
2028 Aspect_Interrupt_Priority |
2029 Aspect_Priority =>
51ea9c94 2030
d6814978 2031 if Nkind_In (N, N_Subprogram_Body,
2032 N_Subprogram_Declaration)
2033 then
2034 -- Analyze the aspect expression
2035
2036 Analyze_And_Resolve (Expr, Standard_Integer);
2037
2038 -- Interrupt_Priority aspect not allowed for main
078a74b8 2039 -- subprograms. RM D.1 does not forbid this explicitly,
2040 -- but RM J.15.11(6/3) does not permit pragma
d6814978 2041 -- Interrupt_Priority for subprograms.
2042
2043 if A_Id = Aspect_Interrupt_Priority then
2044 Error_Msg_N
2045 ("Interrupt_Priority aspect cannot apply to "
2046 & "subprogram", Expr);
2047
2048 -- The expression must be static
2049
cda40848 2050 elsif not Is_OK_Static_Expression (Expr) then
d6814978 2051 Flag_Non_Static_Expr
2052 ("aspect requires static expression!", Expr);
2053
24d7b9d6 2054 -- Check whether this is the main subprogram. Issue a
2055 -- warning only if it is obviously not a main program
2056 -- (when it has parameters or when the subprogram is
2057 -- within a package).
2058
2059 elsif Present (Parameter_Specifications
2060 (Specification (N)))
2061 or else not Is_Compilation_Unit (Defining_Entity (N))
d6814978 2062 then
078a74b8 2063 -- See RM D.1(14/3) and D.16(12/3)
d6814978 2064
2065 Error_Msg_N
2066 ("aspect applied to subprogram other than the "
2067 & "main subprogram has no effect??", Expr);
2068
2069 -- Otherwise check in range and export the value
2070
2071 -- For the CPU aspect
2072
2073 elsif A_Id = Aspect_CPU then
2074 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2075
2076 -- Value is correct so we export the value to make
2077 -- it available at execution time.
2078
2079 Set_Main_CPU
2080 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2081
2082 else
2083 Error_Msg_N
2084 ("main subprogram CPU is out of range", Expr);
2085 end if;
2086
2087 -- For the Priority aspect
2088
2089 elsif A_Id = Aspect_Priority then
2090 if Is_In_Range (Expr, RTE (RE_Priority)) then
2091
2092 -- Value is correct so we export the value to make
2093 -- it available at execution time.
2094
2095 Set_Main_Priority
2096 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2097
32572384 2098 -- Ignore pragma if Relaxed_RM_Semantics to support
2099 -- other targets/non GNAT compilers.
2100
2101 elsif not Relaxed_RM_Semantics then
d6814978 2102 Error_Msg_N
2103 ("main subprogram priority is out of range",
2104 Expr);
2105 end if;
2106 end if;
2107
2108 -- Load an arbitrary entity from System.Tasking.Stages
2109 -- or System.Tasking.Restricted.Stages (depending on
2110 -- the supported profile) to make sure that one of these
2111 -- packages is implicitly with'ed, since we need to have
2112 -- the tasking run time active for the pragma Priority to
a0c3eeb9 2113 -- have any effect. Previously we with'ed the package
d6814978 2114 -- System.Tasking, but this package does not trigger the
2115 -- required initialization of the run-time library.
2116
2117 declare
2118 Discard : Entity_Id;
d6814978 2119 begin
2120 if Restricted_Profile then
2121 Discard := RTE (RE_Activate_Restricted_Tasks);
2122 else
2123 Discard := RTE (RE_Activate_Tasks);
2124 end if;
2125 end;
2126
2127 -- Handling for these Aspects in subprograms is complete
2128
2129 goto Continue;
2130
2131 -- For tasks
0fd13d32 2132
3a72f9c3 2133 else
d6814978 2134 -- Pass the aspect as an attribute
2135
3a72f9c3 2136 Aitem :=
2137 Make_Attribute_Definition_Clause (Loc,
2138 Name => Ent,
2139 Chars => Chars (Id),
2140 Expression => Relocate_Node (Expr));
2141 end if;
2142
0fd13d32 2143 -- Warnings
2144
ae888dbd 2145 when Aspect_Warnings =>
0fd13d32 2146 Make_Aitem_Pragma
2147 (Pragma_Argument_Associations => New_List (
2148 Make_Pragma_Argument_Association (Sloc (Expr),
2149 Expression => Relocate_Node (Expr)),
2150 Make_Pragma_Argument_Association (Loc,
2151 Expression => New_Occurrence_Of (E, Loc))),
2152 Pragma_Name => Chars (Id));
ae888dbd 2153
89f1e35c 2154 -- Case 2c: Aspects corresponding to pragmas with three
2155 -- arguments.
d64221a7 2156
89f1e35c 2157 -- Invariant aspects have a first argument that references the
2158 -- entity, a second argument that is the expression and a third
2159 -- argument that is an appropriate message.
d64221a7 2160
0fd13d32 2161 -- Invariant, Type_Invariant
2162
89f1e35c 2163 when Aspect_Invariant |
2164 Aspect_Type_Invariant =>
d64221a7 2165
89f1e35c 2166 -- Analysis of the pragma will verify placement legality:
2167 -- an invariant must apply to a private type, or appear in
2168 -- the private part of a spec and apply to a completion.
d64221a7 2169
0fd13d32 2170 Make_Aitem_Pragma
2171 (Pragma_Argument_Associations => New_List (
2172 Make_Pragma_Argument_Association (Sloc (Ent),
2173 Expression => Ent),
2174 Make_Pragma_Argument_Association (Sloc (Expr),
2175 Expression => Relocate_Node (Expr))),
2176 Pragma_Name => Name_Invariant);
89f1e35c 2177
2178 -- Add message unless exception messages are suppressed
2179
2180 if not Opt.Exception_Locations_Suppressed then
2181 Append_To (Pragma_Argument_Associations (Aitem),
2182 Make_Pragma_Argument_Association (Eloc,
2183 Chars => Name_Message,
2184 Expression =>
2185 Make_String_Literal (Eloc,
2186 Strval => "failed invariant from "
2187 & Build_Location_String (Eloc))));
d64221a7 2188 end if;
2189
89f1e35c 2190 -- For Invariant case, insert immediately after the entity
2191 -- declaration. We do not have to worry about delay issues
2192 -- since the pragma processing takes care of this.
2193
89f1e35c 2194 Delay_Required := False;
d64221a7 2195
47a46747 2196 -- Case 2d : Aspects that correspond to a pragma with one
2197 -- argument.
2198
0fd13d32 2199 -- Abstract_State
115f7b08 2200
d4e369ad 2201 -- Aspect Abstract_State introduces implicit declarations for
2202 -- all state abstraction entities it defines. To emulate this
2203 -- behavior, insert the pragma at the beginning of the visible
2204 -- declarations of the related package so that it is analyzed
2205 -- immediately.
2206
9129c28f 2207 when Aspect_Abstract_State => Abstract_State : declare
eb4f7efa 2208 Context : Node_Id := N;
630b6d55 2209 Decl : Node_Id;
eb4f7efa 2210 Decls : List_Id;
9129c28f 2211
2212 begin
eb4f7efa 2213 -- When aspect Abstract_State appears on a generic package,
2214 -- it is propageted to the package instance. The context in
2215 -- this case is the instance spec.
2216
2217 if Nkind (Context) = N_Package_Instantiation then
2218 Context := Instance_Spec (Context);
2219 end if;
2220
2221 if Nkind_In (Context, N_Generic_Package_Declaration,
2222 N_Package_Declaration)
9129c28f 2223 then
9129c28f 2224 Make_Aitem_Pragma
2225 (Pragma_Argument_Associations => New_List (
2226 Make_Pragma_Argument_Association (Loc,
2227 Expression => Relocate_Node (Expr))),
2228 Pragma_Name => Name_Abstract_State);
e2bf777d 2229 Decorate (Aspect, Aitem);
9129c28f 2230
630b6d55 2231 Decls := Visible_Declarations (Specification (Context));
2232
2233 -- In general pragma Abstract_State must be at the top
2234 -- of the existing visible declarations to emulate its
2235 -- source counterpart. The only exception to this is a
2236 -- generic instance in which case the pragma must be
2237 -- inserted after the association renamings.
2238
2239 if Present (Decls) then
50e44732 2240 Decl := First (Decls);
630b6d55 2241
2242 -- The visible declarations of a generic instance have
2243 -- the following structure:
2244
2245 -- <renamings of generic formals>
2246 -- <renamings of internally-generated spec and body>
2247 -- <first source declaration>
2248
2249 -- The pragma must be inserted before the first source
50e44732 2250 -- declaration, skip the instance "header".
630b6d55 2251
2252 if Is_Generic_Instance (Defining_Entity (Context)) then
630b6d55 2253 while Present (Decl)
2254 and then not Comes_From_Source (Decl)
2255 loop
2256 Decl := Next (Decl);
2257 end loop;
50e44732 2258 end if;
630b6d55 2259
3dbe7a69 2260 -- When aspects Abstract_State, Ghost,
2261 -- Initial_Condition and Initializes are out of order,
2262 -- ensure that pragma SPARK_Mode is always at the top
2263 -- of the declarations to properly enabled/suppress
2264 -- errors.
630b6d55 2265
50e44732 2266 Insert_After_SPARK_Mode
2267 (Prag => Aitem,
2268 Ins_Nod => Decl,
2269 Decls => Decls);
630b6d55 2270
2271 -- Otherwise the pragma forms a new declarative list
2272
2273 else
2274 Set_Visible_Declarations
2275 (Specification (Context), New_List (Aitem));
2276 end if;
9129c28f 2277
2278 else
2279 Error_Msg_NE
2280 ("aspect & must apply to a package declaration",
2281 Aspect, Id);
2282 end if;
2283
2284 goto Continue;
2285 end Abstract_State;
115f7b08 2286
85ee12c0 2287 -- Aspect Async_Readers is never delayed because it is
2288 -- equivalent to a source pragma which appears after the
2289 -- related object declaration.
2290
2291 when Aspect_Async_Readers =>
2292 Make_Aitem_Pragma
2293 (Pragma_Argument_Associations => New_List (
2294 Make_Pragma_Argument_Association (Loc,
2295 Expression => Relocate_Node (Expr))),
2296 Pragma_Name => Name_Async_Readers);
2297
2298 Decorate (Aspect, Aitem);
2299 Insert_Pragma (Aitem);
2300 goto Continue;
2301
2302 -- Aspect Async_Writers is never delayed because it is
2303 -- equivalent to a source pragma which appears after the
2304 -- related object declaration.
2305
2306 when Aspect_Async_Writers =>
2307 Make_Aitem_Pragma
2308 (Pragma_Argument_Associations => New_List (
2309 Make_Pragma_Argument_Association (Loc,
2310 Expression => Relocate_Node (Expr))),
2311 Pragma_Name => Name_Async_Writers);
2312
2313 Decorate (Aspect, Aitem);
2314 Insert_Pragma (Aitem);
2315 goto Continue;
2316
d0849c23 2317 -- Aspect Constant_After_Elaboration is never delayed because
2318 -- it is equivalent to a source pragma which appears after the
2319 -- related object declaration.
2320
2321 when Aspect_Constant_After_Elaboration =>
2322 Make_Aitem_Pragma
2323 (Pragma_Argument_Associations => New_List (
2324 Make_Pragma_Argument_Association (Loc,
2325 Expression => Relocate_Node (Expr))),
2326 Pragma_Name =>
2327 Name_Constant_After_Elaboration);
2328
2329 Decorate (Aspect, Aitem);
2330 Insert_Pragma (Aitem);
2331 goto Continue;
2332
ec6f6da5 2333 -- Aspect Default_Internal_Condition is never delayed because
2334 -- it is equivalent to a source pragma which appears after the
2335 -- related private type. To deal with forward references, the
2336 -- generated pragma is stored in the rep chain of the related
2337 -- private type as types do not carry contracts. The pragma is
2338 -- wrapped inside of a procedure at the freeze point of the
2339 -- private type's full view.
2340
2341 when Aspect_Default_Initial_Condition =>
2342 Make_Aitem_Pragma
2343 (Pragma_Argument_Associations => New_List (
2344 Make_Pragma_Argument_Association (Loc,
2345 Expression => Relocate_Node (Expr))),
2346 Pragma_Name =>
2347 Name_Default_Initial_Condition);
2348
2349 Decorate (Aspect, Aitem);
2350 Insert_Pragma (Aitem);
2351 goto Continue;
2352
647fab54 2353 -- Default_Storage_Pool
2354
2355 when Aspect_Default_Storage_Pool =>
2356 Make_Aitem_Pragma
2357 (Pragma_Argument_Associations => New_List (
2358 Make_Pragma_Argument_Association (Loc,
2359 Expression => Relocate_Node (Expr))),
2360 Pragma_Name =>
2361 Name_Default_Storage_Pool);
2362
2363 Decorate (Aspect, Aitem);
2364 Insert_Pragma (Aitem);
2365 goto Continue;
2366
0fd13d32 2367 -- Depends
2368
e2bf777d 2369 -- Aspect Depends is never delayed because it is equivalent to
2370 -- a source pragma which appears after the related subprogram.
2371 -- To deal with forward references, the generated pragma is
2372 -- stored in the contract of the related subprogram and later
2373 -- analyzed at the end of the declarative region. See routine
2374 -- Analyze_Depends_In_Decl_Part for details.
6144c105 2375
12334c57 2376 when Aspect_Depends =>
0fd13d32 2377 Make_Aitem_Pragma
2378 (Pragma_Argument_Associations => New_List (
2379 Make_Pragma_Argument_Association (Loc,
2380 Expression => Relocate_Node (Expr))),
2381 Pragma_Name => Name_Depends);
2382
e2bf777d 2383 Decorate (Aspect, Aitem);
2384 Insert_Pragma (Aitem);
c1006d6d 2385 goto Continue;
2386
85ee12c0 2387 -- Aspect Effecitve_Reads is never delayed because it is
2388 -- equivalent to a source pragma which appears after the
2389 -- related object declaration.
2390
2391 when Aspect_Effective_Reads =>
2392 Make_Aitem_Pragma
2393 (Pragma_Argument_Associations => New_List (
2394 Make_Pragma_Argument_Association (Loc,
2395 Expression => Relocate_Node (Expr))),
2396 Pragma_Name => Name_Effective_Reads);
2397
2398 Decorate (Aspect, Aitem);
2399 Insert_Pragma (Aitem);
2400 goto Continue;
2401
2402 -- Aspect Effective_Writes is never delayed because it is
2403 -- equivalent to a source pragma which appears after the
2404 -- related object declaration.
2405
2406 when Aspect_Effective_Writes =>
2407 Make_Aitem_Pragma
2408 (Pragma_Argument_Associations => New_List (
2409 Make_Pragma_Argument_Association (Loc,
2410 Expression => Relocate_Node (Expr))),
2411 Pragma_Name => Name_Effective_Writes);
2412
2413 Decorate (Aspect, Aitem);
2414 Insert_Pragma (Aitem);
2415 goto Continue;
2416
cab27d2a 2417 -- Aspect Extensions_Visible is never delayed because it is
2418 -- equivalent to a source pragma which appears after the
2419 -- related subprogram.
2420
2421 when Aspect_Extensions_Visible =>
2422 Make_Aitem_Pragma
2423 (Pragma_Argument_Associations => New_List (
2424 Make_Pragma_Argument_Association (Loc,
2425 Expression => Relocate_Node (Expr))),
2426 Pragma_Name => Name_Extensions_Visible);
2427
2428 Decorate (Aspect, Aitem);
2429 Insert_Pragma (Aitem);
2430 goto Continue;
2431
3dbe7a69 2432 -- Aspect Ghost is never delayed because it is equivalent to a
2433 -- source pragma which appears at the top of [generic] package
2434 -- declarations or after an object, a [generic] subprogram, or
2435 -- a type declaration.
2436
2437 when Aspect_Ghost => Ghost : declare
2438 Decls : List_Id;
2439
2440 begin
2441 Make_Aitem_Pragma
2442 (Pragma_Argument_Associations => New_List (
2443 Make_Pragma_Argument_Association (Loc,
2444 Expression => Relocate_Node (Expr))),
2445 Pragma_Name => Name_Ghost);
2446
2447 Decorate (Aspect, Aitem);
2448
2449 -- When the aspect applies to a [generic] package, insert
2450 -- the pragma at the top of the visible declarations. This
2451 -- emulates the placement of a source pragma.
2452
2453 if Nkind_In (N, N_Generic_Package_Declaration,
2454 N_Package_Declaration)
2455 then
2456 Decls := Visible_Declarations (Specification (N));
2457
2458 if No (Decls) then
2459 Decls := New_List;
2460 Set_Visible_Declarations (N, Decls);
2461 end if;
2462
2463 -- When aspects Abstract_State, Ghost, Initial_Condition
2464 -- and Initializes are out of order, ensure that pragma
2465 -- SPARK_Mode is always at the top of the declarations to
2466 -- properly enabled/suppress errors.
2467
2468 Insert_After_SPARK_Mode
2469 (Prag => Aitem,
2470 Ins_Nod => First (Decls),
2471 Decls => Decls);
2472
2473 -- Otherwise the context is an object, [generic] subprogram
2474 -- or type declaration.
2475
2476 else
2477 Insert_Pragma (Aitem);
2478 end if;
2479
2480 goto Continue;
2481 end Ghost;
2482
0fd13d32 2483 -- Global
12334c57 2484
e2bf777d 2485 -- Aspect Global is never delayed because it is equivalent to
2486 -- a source pragma which appears after the related subprogram.
2487 -- To deal with forward references, the generated pragma is
2488 -- stored in the contract of the related subprogram and later
2489 -- analyzed at the end of the declarative region. See routine
2490 -- Analyze_Global_In_Decl_Part for details.
3cdbaa5a 2491
2492 when Aspect_Global =>
0fd13d32 2493 Make_Aitem_Pragma
2494 (Pragma_Argument_Associations => New_List (
2495 Make_Pragma_Argument_Association (Loc,
2496 Expression => Relocate_Node (Expr))),
2497 Pragma_Name => Name_Global);
2498
e2bf777d 2499 Decorate (Aspect, Aitem);
2500 Insert_Pragma (Aitem);
c1006d6d 2501 goto Continue;
2502
9c138530 2503 -- Initial_Condition
2504
e2bf777d 2505 -- Aspect Initial_Condition is never delayed because it is
2506 -- equivalent to a source pragma which appears after the
2507 -- related package. To deal with forward references, the
2508 -- generated pragma is stored in the contract of the related
2509 -- package and later analyzed at the end of the declarative
2510 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2511 -- for details.
9c138530 2512
2513 when Aspect_Initial_Condition => Initial_Condition : declare
eb4f7efa 2514 Context : Node_Id := N;
2515 Decls : List_Id;
9c138530 2516
2517 begin
e2bf777d 2518 -- When aspect Initial_Condition appears on a generic
2519 -- package, it is propageted to the package instance. The
2520 -- context in this case is the instance spec.
eb4f7efa 2521
2522 if Nkind (Context) = N_Package_Instantiation then
2523 Context := Instance_Spec (Context);
2524 end if;
2525
2526 if Nkind_In (Context, N_Generic_Package_Declaration,
2527 N_Package_Declaration)
9c138530 2528 then
eb4f7efa 2529 Decls := Visible_Declarations (Specification (Context));
9c138530 2530
2531 Make_Aitem_Pragma
2532 (Pragma_Argument_Associations => New_List (
2533 Make_Pragma_Argument_Association (Loc,
2534 Expression => Relocate_Node (Expr))),
2535 Pragma_Name =>
2536 Name_Initial_Condition);
e2bf777d 2537 Decorate (Aspect, Aitem);
9c138530 2538
2539 if No (Decls) then
2540 Decls := New_List;
eb4f7efa 2541 Set_Visible_Declarations (Context, Decls);
9c138530 2542 end if;
2543
3dbe7a69 2544 -- When aspects Abstract_State, Ghost, Initial_Condition
2545 -- and Initializes are out of order, ensure that pragma
2546 -- SPARK_Mode is always at the top of the declarations to
2547 -- properly enabled/suppress errors.
50e44732 2548
2549 Insert_After_SPARK_Mode
2550 (Prag => Aitem,
2551 Ins_Nod => First (Decls),
2552 Decls => Decls);
9c138530 2553
2554 else
2555 Error_Msg_NE
2556 ("aspect & must apply to a package declaration",
2557 Aspect, Id);
2558 end if;
2559
2560 goto Continue;
2561 end Initial_Condition;
2562
d4e369ad 2563 -- Initializes
2564
e2bf777d 2565 -- Aspect Initializes is never delayed because it is equivalent
2566 -- to a source pragma appearing after the related package. To
2567 -- deal with forward references, the generated pragma is stored
2568 -- in the contract of the related package and later analyzed at
2569 -- the end of the declarative region. For details, see routine
2570 -- Analyze_Initializes_In_Decl_Part.
d4e369ad 2571
2572 when Aspect_Initializes => Initializes : declare
eb4f7efa 2573 Context : Node_Id := N;
2574 Decls : List_Id;
d4e369ad 2575
2576 begin
50e44732 2577 -- When aspect Initializes appears on a generic package,
2578 -- it is propageted to the package instance. The context
2579 -- in this case is the instance spec.
eb4f7efa 2580
2581 if Nkind (Context) = N_Package_Instantiation then
2582 Context := Instance_Spec (Context);
2583 end if;
2584
2585 if Nkind_In (Context, N_Generic_Package_Declaration,
2586 N_Package_Declaration)
d4e369ad 2587 then
eb4f7efa 2588 Decls := Visible_Declarations (Specification (Context));
d4e369ad 2589
2590 Make_Aitem_Pragma
2591 (Pragma_Argument_Associations => New_List (
2592 Make_Pragma_Argument_Association (Loc,
2593 Expression => Relocate_Node (Expr))),
2594 Pragma_Name => Name_Initializes);
e2bf777d 2595 Decorate (Aspect, Aitem);
d4e369ad 2596
2597 if No (Decls) then
2598 Decls := New_List;
eb4f7efa 2599 Set_Visible_Declarations (Context, Decls);
d4e369ad 2600 end if;
2601
3dbe7a69 2602 -- When aspects Abstract_State, Ghost, Initial_Condition
2603 -- and Initializes are out of order, ensure that pragma
2604 -- SPARK_Mode is always at the top of the declarations to
2605 -- properly enabled/suppress errors.
50e44732 2606
2607 Insert_After_SPARK_Mode
2608 (Prag => Aitem,
2609 Ins_Nod => First (Decls),
2610 Decls => Decls);
d4e369ad 2611
2612 else
2613 Error_Msg_NE
2614 ("aspect & must apply to a package declaration",
2615 Aspect, Id);
2616 end if;
2617
2618 goto Continue;
2619 end Initializes;
2620
1fd4313f 2621 -- Obsolescent
2622
2623 when Aspect_Obsolescent => declare
2624 Args : List_Id;
2625
2626 begin
2627 if No (Expr) then
2628 Args := No_List;
2629 else
2630 Args := New_List (
2631 Make_Pragma_Argument_Association (Sloc (Expr),
2632 Expression => Relocate_Node (Expr)));
2633 end if;
2634
2635 Make_Aitem_Pragma
2636 (Pragma_Argument_Associations => Args,
2637 Pragma_Name => Chars (Id));
2638 end;
2639
5cc6f0cf 2640 -- Part_Of
2641
2642 when Aspect_Part_Of =>
2643 if Nkind_In (N, N_Object_Declaration,
2644 N_Package_Instantiation)
2645 then
2646 Make_Aitem_Pragma
2647 (Pragma_Argument_Associations => New_List (
2648 Make_Pragma_Argument_Association (Loc,
2649 Expression => Relocate_Node (Expr))),
2650 Pragma_Name => Name_Part_Of);
2651
2652 else
2653 Error_Msg_NE
2654 ("aspect & must apply to a variable or package "
2655 & "instantiation", Aspect, Id);
2656 end if;
2657
5dd93a61 2658 -- SPARK_Mode
2659
778ebf56 2660 when Aspect_SPARK_Mode => SPARK_Mode : declare
2661 Decls : List_Id;
2662
2663 begin
5dd93a61 2664 Make_Aitem_Pragma
2665 (Pragma_Argument_Associations => New_List (
2666 Make_Pragma_Argument_Association (Loc,
2667 Expression => Relocate_Node (Expr))),
2668 Pragma_Name => Name_SPARK_Mode);
5dd93a61 2669
b90d9656 2670 -- When the aspect appears on a package or a subprogram
2671 -- body, insert the generated pragma at the top of the body
2672 -- declarations to emulate the behavior of a source pragma.
778ebf56 2673
b90d9656 2674 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
e2bf777d 2675 Decorate (Aspect, Aitem);
5cc6f0cf 2676
778ebf56 2677 Decls := Declarations (N);
2678
2679 if No (Decls) then
2680 Decls := New_List;
2681 Set_Declarations (N, Decls);
2682 end if;
2683
a04f9d2e 2684 Prepend_To (Decls, Aitem);
2685 goto Continue;
2686
b90d9656 2687 -- When the aspect is associated with a [generic] package
2688 -- declaration, insert the generated pragma at the top of
2689 -- the visible declarations to emulate the behavior of a
2690 -- source pragma.
a04f9d2e 2691
b90d9656 2692 elsif Nkind_In (N, N_Generic_Package_Declaration,
2693 N_Package_Declaration)
2694 then
e2bf777d 2695 Decorate (Aspect, Aitem);
5cc6f0cf 2696
a04f9d2e 2697 Decls := Visible_Declarations (Specification (N));
2698
2699 if No (Decls) then
2700 Decls := New_List;
2701 Set_Visible_Declarations (Specification (N), Decls);
2702 end if;
2703
778ebf56 2704 Prepend_To (Decls, Aitem);
2705 goto Continue;
2706 end if;
2707 end SPARK_Mode;
2708
4befb1a0 2709 -- Refined_Depends
2710
e2bf777d 2711 -- Aspect Refined_Depends is never delayed because it is
2712 -- equivalent to a source pragma which appears in the
2713 -- declarations of the related subprogram body. To deal with
2714 -- forward references, the generated pragma is stored in the
2715 -- contract of the related subprogram body and later analyzed
2716 -- at the end of the declarative region. For details, see
2717 -- routine Analyze_Refined_Depends_In_Decl_Part.
4befb1a0 2718
2719 when Aspect_Refined_Depends =>
422073ed 2720 Make_Aitem_Pragma
2721 (Pragma_Argument_Associations => New_List (
2722 Make_Pragma_Argument_Association (Loc,
2723 Expression => Relocate_Node (Expr))),
2724 Pragma_Name => Name_Refined_Depends);
2725
e2bf777d 2726 Decorate (Aspect, Aitem);
2727 Insert_Pragma (Aitem);
422073ed 2728 goto Continue;
4befb1a0 2729
2730 -- Refined_Global
2731
e2bf777d 2732 -- Aspect Refined_Global is never delayed because it is
2733 -- equivalent to a source pragma which appears in the
2734 -- declarations of the related subprogram body. To deal with
2735 -- forward references, the generated pragma is stored in the
2736 -- contract of the related subprogram body and later analyzed
2737 -- at the end of the declarative region. For details, see
2738 -- routine Analyze_Refined_Global_In_Decl_Part.
4befb1a0 2739
2740 when Aspect_Refined_Global =>
28ff117f 2741 Make_Aitem_Pragma
2742 (Pragma_Argument_Associations => New_List (
2743 Make_Pragma_Argument_Association (Loc,
2744 Expression => Relocate_Node (Expr))),
2745 Pragma_Name => Name_Refined_Global);
2746
e2bf777d 2747 Decorate (Aspect, Aitem);
2748 Insert_Pragma (Aitem);
28ff117f 2749 goto Continue;
4befb1a0 2750
63b65b2d 2751 -- Refined_Post
2752
2753 when Aspect_Refined_Post =>
2754 Make_Aitem_Pragma
2755 (Pragma_Argument_Associations => New_List (
2756 Make_Pragma_Argument_Association (Loc,
2757 Expression => Relocate_Node (Expr))),
2758 Pragma_Name => Name_Refined_Post);
2759
9129c28f 2760 -- Refined_State
2761
2762 when Aspect_Refined_State => Refined_State : declare
2763 Decls : List_Id;
2764
2765 begin
2766 -- The corresponding pragma for Refined_State is inserted in
2767 -- the declarations of the related package body. This action
2768 -- synchronizes both the source and from-aspect versions of
2769 -- the pragma.
2770
2771 if Nkind (N) = N_Package_Body then
e2bf777d 2772 Decls := Declarations (N);
2773
9129c28f 2774 Make_Aitem_Pragma
2775 (Pragma_Argument_Associations => New_List (
2776 Make_Pragma_Argument_Association (Loc,
2777 Expression => Relocate_Node (Expr))),
2778 Pragma_Name => Name_Refined_State);
e2bf777d 2779 Decorate (Aspect, Aitem);
b9b2d6e5 2780
e2bf777d 2781 if No (Decls) then
2782 Decls := New_List;
2783 Set_Declarations (N, Decls);
2784 end if;
b9b2d6e5 2785
e2bf777d 2786 -- Pragma Refined_State must be inserted after pragma
2787 -- SPARK_Mode in the tree. This ensures that any error
2788 -- messages dependent on SPARK_Mode will be properly
2789 -- enabled/suppressed.
b9b2d6e5 2790
e2bf777d 2791 Insert_After_SPARK_Mode
2792 (Prag => Aitem,
2793 Ins_Nod => First (Decls),
2794 Decls => Decls);
9129c28f 2795
2796 else
2797 Error_Msg_NE
2798 ("aspect & must apply to a package body", Aspect, Id);
2799 end if;
2800
2801 goto Continue;
2802 end Refined_State;
2803
0fd13d32 2804 -- Relative_Deadline
3cdbaa5a 2805
2806 when Aspect_Relative_Deadline =>
0fd13d32 2807 Make_Aitem_Pragma
2808 (Pragma_Argument_Associations => New_List (
2809 Make_Pragma_Argument_Association (Loc,
2810 Expression => Relocate_Node (Expr))),
2811 Pragma_Name => Name_Relative_Deadline);
47a46747 2812
2813 -- If the aspect applies to a task, the corresponding pragma
2814 -- must appear within its declarations, not after.
2815
2816 if Nkind (N) = N_Task_Type_Declaration then
2817 declare
2818 Def : Node_Id;
2819 V : List_Id;
2820
2821 begin
2822 if No (Task_Definition (N)) then
2823 Set_Task_Definition (N,
2824 Make_Task_Definition (Loc,
2825 Visible_Declarations => New_List,
2826 End_Label => Empty));
2827 end if;
2828
2829 Def := Task_Definition (N);
2830 V := Visible_Declarations (Def);
2831 if not Is_Empty_List (V) then
2832 Insert_Before (First (V), Aitem);
2833
2834 else
2835 Set_Visible_Declarations (Def, New_List (Aitem));
2836 end if;
2837
2838 goto Continue;
2839 end;
2840 end if;
2841
85ee12c0 2842 -- Aspect Volatile_Function is never delayed because it is
2843 -- equivalent to a source pragma which appears after the
2844 -- related subprogram.
2845
2846 when Aspect_Volatile_Function =>
2847 Make_Aitem_Pragma
2848 (Pragma_Argument_Associations => New_List (
2849 Make_Pragma_Argument_Association (Loc,
2850 Expression => Relocate_Node (Expr))),
2851 Pragma_Name => Name_Volatile_Function);
2852
2853 Decorate (Aspect, Aitem);
2854 Insert_Pragma (Aitem);
2855 goto Continue;
2856
956ffaf4 2857 -- Case 2e: Annotate aspect
2858
2859 when Aspect_Annotate =>
2860 declare
2861 Args : List_Id;
2862 Pargs : List_Id;
2863 Arg : Node_Id;
2864
2865 begin
2866 -- The argument can be a single identifier
2867
2868 if Nkind (Expr) = N_Identifier then
2869
2870 -- One level of parens is allowed
2871
2872 if Paren_Count (Expr) > 1 then
2873 Error_Msg_F ("extra parentheses ignored", Expr);
2874 end if;
2875
2876 Set_Paren_Count (Expr, 0);
2877
2878 -- Add the single item to the list
2879
2880 Args := New_List (Expr);
2881
2882 -- Otherwise we must have an aggregate
2883
2884 elsif Nkind (Expr) = N_Aggregate then
2885
2886 -- Must be positional
2887
2888 if Present (Component_Associations (Expr)) then
2889 Error_Msg_F
2890 ("purely positional aggregate required", Expr);
2891 goto Continue;
2892 end if;
2893
2894 -- Must not be parenthesized
2895
2896 if Paren_Count (Expr) /= 0 then
2897 Error_Msg_F ("extra parentheses ignored", Expr);
2898 end if;
2899
2900 -- List of arguments is list of aggregate expressions
2901
2902 Args := Expressions (Expr);
2903
2904 -- Anything else is illegal
2905
2906 else
2907 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2908 goto Continue;
2909 end if;
2910
2911 -- Prepare pragma arguments
2912
2913 Pargs := New_List;
2914 Arg := First (Args);
2915 while Present (Arg) loop
2916 Append_To (Pargs,
2917 Make_Pragma_Argument_Association (Sloc (Arg),
2918 Expression => Relocate_Node (Arg)));
2919 Next (Arg);
2920 end loop;
2921
2922 Append_To (Pargs,
2923 Make_Pragma_Argument_Association (Sloc (Ent),
2924 Chars => Name_Entity,
2925 Expression => Ent));
2926
2927 Make_Aitem_Pragma
2928 (Pragma_Argument_Associations => Pargs,
2929 Pragma_Name => Name_Annotate);
2930 end;
2931
89f1e35c 2932 -- Case 3 : Aspects that don't correspond to pragma/attribute
2933 -- definition clause.
7b9b2f05 2934
89f1e35c 2935 -- Case 3a: The aspects listed below don't correspond to
2936 -- pragmas/attributes but do require delayed analysis.
7f694ca2 2937
51fa2a45 2938 -- Default_Value can only apply to a scalar type
2939
2940 when Aspect_Default_Value =>
2941 if not Is_Scalar_Type (E) then
2942 Error_Msg_N
1089ff19 2943 ("aspect Default_Value must apply to a scalar type", N);
51fa2a45 2944 end if;
2945
2946 Aitem := Empty;
2947
2948 -- Default_Component_Value can only apply to an array type
2949 -- with scalar components.
2950
2951 when Aspect_Default_Component_Value =>
2952 if not (Is_Array_Type (E)
3f4c9ffc 2953 and then Is_Scalar_Type (Component_Type (E)))
51fa2a45 2954 then
2955 Error_Msg_N ("aspect Default_Component_Value can only "
2956 & "apply to an array of scalar components", N);
2957 end if;
0fd13d32 2958
89f1e35c 2959 Aitem := Empty;
7f694ca2 2960
89f1e35c 2961 -- Case 3b: The aspects listed below don't correspond to
2962 -- pragmas/attributes and don't need delayed analysis.
95bc75fa 2963
0fd13d32 2964 -- Implicit_Dereference
2965
89f1e35c 2966 -- For Implicit_Dereference, External_Name and Link_Name, only
2967 -- the legality checks are done during the analysis, thus no
2968 -- delay is required.
a8e38e1d 2969
89f1e35c 2970 when Aspect_Implicit_Dereference =>
2971 Analyze_Aspect_Implicit_Dereference;
2972 goto Continue;
7f694ca2 2973
0fd13d32 2974 -- External_Name, Link_Name
2975
89f1e35c 2976 when Aspect_External_Name |
2977 Aspect_Link_Name =>
2978 Analyze_Aspect_External_Or_Link_Name;
2979 goto Continue;
7f694ca2 2980
0fd13d32 2981 -- Dimension
2982
89f1e35c 2983 when Aspect_Dimension =>
2984 Analyze_Aspect_Dimension (N, Id, Expr);
2985 goto Continue;
cb4c311d 2986
0fd13d32 2987 -- Dimension_System
2988
89f1e35c 2989 when Aspect_Dimension_System =>
2990 Analyze_Aspect_Dimension_System (N, Id, Expr);
2991 goto Continue;
7f694ca2 2992
ceec4f7c 2993 -- Case 4: Aspects requiring special handling
51ea9c94 2994
e66f4e2a 2995 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2996 -- pragmas take care of the delay.
7f694ca2 2997
0fd13d32 2998 -- Pre/Post
2999
1e3c4ae6 3000 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3001 -- with a first argument that is the expression, and a second
3002 -- argument that is an informative message if the test fails.
3003 -- This is inserted right after the declaration, to get the
5b5df4a9 3004 -- required pragma placement. The processing for the pragmas
3005 -- takes care of the required delay.
ae888dbd 3006
5ddd846b 3007 when Pre_Post_Aspects => Pre_Post : declare
1e3c4ae6 3008 Pname : Name_Id;
ae888dbd 3009
1e3c4ae6 3010 begin
77ae6789 3011 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1e3c4ae6 3012 Pname := Name_Precondition;
3013 else
3014 Pname := Name_Postcondition;
3015 end if;
d74fc39a 3016
1e3c4ae6 3017 -- If the expressions is of the form A and then B, then
3018 -- we generate separate Pre/Post aspects for the separate
3019 -- clauses. Since we allow multiple pragmas, there is no
3020 -- problem in allowing multiple Pre/Post aspects internally.
a273015d 3021 -- These should be treated in reverse order (B first and
3022 -- A second) since they are later inserted just after N in
3023 -- the order they are treated. This way, the pragma for A
3024 -- ends up preceding the pragma for B, which may have an
3025 -- importance for the error raised (either constraint error
3026 -- or precondition error).
1e3c4ae6 3027
39e1f22f 3028 -- We do not do this for Pre'Class, since we have to put
51fa2a45 3029 -- these conditions together in a complex OR expression.
ae888dbd 3030
4282d342 3031 -- We do not do this in ASIS mode, as ASIS relies on the
3032 -- original node representing the complete expression, when
3033 -- retrieving it through the source aspect table.
3034
3035 if not ASIS_Mode
3036 and then (Pname = Name_Postcondition
3037 or else not Class_Present (Aspect))
39e1f22f 3038 then
3039 while Nkind (Expr) = N_And_Then loop
3040 Insert_After (Aspect,
a273015d 3041 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
39e1f22f 3042 Identifier => Identifier (Aspect),
a273015d 3043 Expression => Relocate_Node (Left_Opnd (Expr)),
39e1f22f 3044 Class_Present => Class_Present (Aspect),
3045 Split_PPC => True));
a273015d 3046 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
39e1f22f 3047 Eloc := Sloc (Expr);
3048 end loop;
3049 end if;
ae888dbd 3050
48d6f069 3051 -- Build the precondition/postcondition pragma
3052
51fa2a45 3053 -- Add note about why we do NOT need Copy_Tree here???
d74fc39a 3054
0fd13d32 3055 Make_Aitem_Pragma
3056 (Pragma_Argument_Associations => New_List (
3057 Make_Pragma_Argument_Association (Eloc,
3058 Chars => Name_Check,
a19e1763 3059 Expression => Relocate_Node (Expr))),
0fd13d32 3060 Pragma_Name => Pname);
39e1f22f 3061
3062 -- Add message unless exception messages are suppressed
3063
3064 if not Opt.Exception_Locations_Suppressed then
3065 Append_To (Pragma_Argument_Associations (Aitem),
3066 Make_Pragma_Argument_Association (Eloc,
ed695684 3067 Chars => Name_Message,
39e1f22f 3068 Expression =>
3069 Make_String_Literal (Eloc,
3070 Strval => "failed "
3071 & Get_Name_String (Pname)
3072 & " from "
3073 & Build_Location_String (Eloc))));
3074 end if;
d74fc39a 3075
7d20685d 3076 Set_Is_Delayed_Aspect (Aspect);
d74fc39a 3077
1e3c4ae6 3078 -- For Pre/Post cases, insert immediately after the entity
3079 -- declaration, since that is the required pragma placement.
3080 -- Note that for these aspects, we do not have to worry
3081 -- about delay issues, since the pragmas themselves deal
3082 -- with delay of visibility for the expression analysis.
3083
e2bf777d 3084 Insert_Pragma (Aitem);
299b347e 3085
1e3c4ae6 3086 goto Continue;
5ddd846b 3087 end Pre_Post;
ae888dbd 3088
0fd13d32 3089 -- Test_Case
3090
e66f4e2a 3091 when Aspect_Test_Case => Test_Case : declare
3092 Args : List_Id;
3093 Comp_Expr : Node_Id;
3094 Comp_Assn : Node_Id;
3095 New_Expr : Node_Id;
57cd943b 3096
e66f4e2a 3097 begin
3098 Args := New_List;
b0bc40fd 3099
e66f4e2a 3100 if Nkind (Parent (N)) = N_Compilation_Unit then
3101 Error_Msg_Name_1 := Nam;
3102 Error_Msg_N ("incorrect placement of aspect `%`", E);
3103 goto Continue;
3104 end if;
6c545057 3105
e66f4e2a 3106 if Nkind (Expr) /= N_Aggregate then
3107 Error_Msg_Name_1 := Nam;
3108 Error_Msg_NE
3109 ("wrong syntax for aspect `%` for &", Id, E);
3110 goto Continue;
3111 end if;
6c545057 3112
e66f4e2a 3113 -- Make pragma expressions refer to the original aspect
51fa2a45 3114 -- expressions through the Original_Node link. This is used
3115 -- in semantic analysis for ASIS mode, so that the original
3116 -- expression also gets analyzed.
e66f4e2a 3117
3118 Comp_Expr := First (Expressions (Expr));
3119 while Present (Comp_Expr) loop
3120 New_Expr := Relocate_Node (Comp_Expr);
e66f4e2a 3121 Append_To (Args,
3122 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3123 Expression => New_Expr));
3124 Next (Comp_Expr);
3125 end loop;
3126
3127 Comp_Assn := First (Component_Associations (Expr));
3128 while Present (Comp_Assn) loop
3129 if List_Length (Choices (Comp_Assn)) /= 1
3130 or else
3131 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3132 then
fad014fe 3133 Error_Msg_Name_1 := Nam;
6c545057 3134 Error_Msg_NE
fad014fe 3135 ("wrong syntax for aspect `%` for &", Id, E);
6c545057 3136 goto Continue;
3137 end if;
3138
e66f4e2a 3139 Append_To (Args,
3140 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
ed695684 3141 Chars => Chars (First (Choices (Comp_Assn))),
3142 Expression =>
3143 Relocate_Node (Expression (Comp_Assn))));
e66f4e2a 3144 Next (Comp_Assn);
3145 end loop;
6c545057 3146
e66f4e2a 3147 -- Build the test-case pragma
6c545057 3148
0fd13d32 3149 Make_Aitem_Pragma
3150 (Pragma_Argument_Associations => Args,
3151 Pragma_Name => Nam);
e66f4e2a 3152 end Test_Case;
85696508 3153
0fd13d32 3154 -- Contract_Cases
3155
5ddd846b 3156 when Aspect_Contract_Cases =>
0fd13d32 3157 Make_Aitem_Pragma
3158 (Pragma_Argument_Associations => New_List (
3159 Make_Pragma_Argument_Association (Loc,
3160 Expression => Relocate_Node (Expr))),
3161 Pragma_Name => Nam);
3a128918 3162
e2bf777d 3163 Decorate (Aspect, Aitem);
3164 Insert_Pragma (Aitem);
5ddd846b 3165 goto Continue;
3a128918 3166
89f1e35c 3167 -- Case 5: Special handling for aspects with an optional
3168 -- boolean argument.
85696508 3169
6c5793cd 3170 -- In the delayed case, the corresponding pragma cannot be
0fd13d32 3171 -- generated yet because the evaluation of the boolean needs
3172 -- to be delayed till the freeze point.
3173
89f1e35c 3174 when Boolean_Aspects |
3175 Library_Unit_Aspects =>
a5a64273 3176
89f1e35c 3177 Set_Is_Boolean_Aspect (Aspect);
a5a64273 3178
89f1e35c 3179 -- Lock_Free aspect only apply to protected objects
e1cedbae 3180
89f1e35c 3181 if A_Id = Aspect_Lock_Free then
3182 if Ekind (E) /= E_Protected_Type then
99a2d5bd 3183 Error_Msg_Name_1 := Nam;
a5a64273 3184 Error_Msg_N
89f1e35c 3185 ("aspect % only applies to a protected object",
3186 Aspect);
3187
3188 else
3189 -- Set the Uses_Lock_Free flag to True if there is no
37c6e44c 3190 -- expression or if the expression is True. The
89f1e35c 3191 -- evaluation of this aspect should be delayed to the
37c6e44c 3192 -- freeze point (why???)
89f1e35c 3193
e81df51c 3194 if No (Expr)
3195 or else Is_True (Static_Boolean (Expr))
89f1e35c 3196 then
3197 Set_Uses_Lock_Free (E);
3198 end if;
caf125ce 3199
3200 Record_Rep_Item (E, Aspect);
a5a64273 3201 end if;
e1cedbae 3202
89f1e35c 3203 goto Continue;
ae888dbd 3204
17631aa0 3205 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
d74fc39a 3206
39616053 3207 -- For the case of aspects Import and Export, we don't
3208 -- consider that we know the entity is never set in the
3209 -- source, since it is is likely modified outside the
3210 -- program.
3211
3212 -- Note: one might think that the analysis of the
3213 -- resulting pragma would take care of that, but
3214 -- that's not the case since it won't be from source.
3215
3216 if Ekind (E) = E_Variable then
3217 Set_Never_Set_In_Source (E, False);
3218 end if;
3219
2e1821c9 3220 -- In older versions of Ada the corresponding pragmas
51fa2a45 3221 -- specified a Convention. In Ada 2012 the convention is
3222 -- specified as a separate aspect, and it is optional,
2e1821c9 3223 -- given that it defaults to Convention_Ada. The code
3224 -- that verifed that there was a matching convention
3225 -- is now obsolete.
d74fc39a 3226
718d0d92 3227 -- Resolve the expression of an Import or Export here,
3228 -- and require it to be of type Boolean and static. This
3229 -- is not quite right, because in general this should be
3230 -- delayed, but that seems tricky for these, because
3231 -- normally Boolean aspects are replaced with pragmas at
3232 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3233 -- but in the case of these aspects we can't generate
3234 -- a simple pragma with just the entity name. ???
3235
3236 if not Present (Expr)
3237 or else Is_True (Static_Boolean (Expr))
3238 then
3239 if A_Id = Aspect_Import then
3240 Set_Is_Imported (E);
19a2e7b4 3241 Set_Has_Completion (E);
dd4c44af 3242
718d0d92 3243 -- An imported entity cannot have an explicit
3244 -- initialization.
dd4c44af 3245
718d0d92 3246 if Nkind (N) = N_Object_Declaration
3247 and then Present (Expression (N))
3248 then
3249 Error_Msg_N
3250 ("imported entities cannot be initialized "
3251 & "(RM B.1(24))", Expression (N));
3252 end if;
3253
3254 elsif A_Id = Aspect_Export then
3255 Set_Is_Exported (E);
dd4c44af 3256 end if;
3257 end if;
3258
6c5793cd 3259 goto Continue;
3260
3261 -- Disable_Controlled
3262
3263 elsif A_Id = Aspect_Disable_Controlled then
3264 if Ekind (E) /= E_Record_Type
3265 or else not Is_Controlled (E)
3266 then
3267 Error_Msg_N
3268 ("aspect % requires controlled record type", Aspect);
3269 goto Continue;
3270 end if;
3271
3f716509 3272 -- If we're in a generic template, we don't want to try
3273 -- to disable controlled types, because typical usage is
3274 -- "Disable_Controlled => not <some_check>'Enabled", and
3275 -- the value of Enabled is not known until we see a
7e2d3667 3276 -- particular instance. In such a context, we just need
3277 -- to preanalyze the expression for legality.
3f716509 3278
3279 if Expander_Active then
aae9bc79 3280 Analyze_And_Resolve (Expr, Standard_Boolean);
3281
3f716509 3282 if not Present (Expr)
3283 or else Is_True (Static_Boolean (Expr))
3284 then
3285 Set_Disable_Controlled (E);
3286 end if;
7e2d3667 3287
3288 elsif Serious_Errors_Detected = 0 then
3289 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6c5793cd 3290 end if;
3291
89f1e35c 3292 goto Continue;
3293 end if;
d74fc39a 3294
37c6e44c 3295 -- Library unit aspects require special handling in the case
3296 -- of a package declaration, the pragma needs to be inserted
3297 -- in the list of declarations for the associated package.
3298 -- There is no issue of visibility delay for these aspects.
d64221a7 3299
89f1e35c 3300 if A_Id in Library_Unit_Aspects
178fec9b 3301 and then
3302 Nkind_In (N, N_Package_Declaration,
3303 N_Generic_Package_Declaration)
89f1e35c 3304 and then Nkind (Parent (N)) /= N_Compilation_Unit
3ad60f63 3305
3306 -- Aspect is legal on a local instantiation of a library-
3307 -- level generic unit.
3308
b94a633e 3309 and then not Is_Generic_Instance (Defining_Entity (N))
89f1e35c 3310 then
3311 Error_Msg_N
dd4c44af 3312 ("incorrect context for library unit aspect&", Id);
89f1e35c 3313 goto Continue;
3314 end if;
cce84b09 3315
51fa2a45 3316 -- Cases where we do not delay, includes all cases where the
3317 -- expression is missing other than the above cases.
d74fc39a 3318
85ee12c0 3319 if not Delay_Required or else No (Expr) then
0fd13d32 3320 Make_Aitem_Pragma
3321 (Pragma_Argument_Associations => New_List (
3322 Make_Pragma_Argument_Association (Sloc (Ent),
3323 Expression => Ent)),
3324 Pragma_Name => Chars (Id));
89f1e35c 3325 Delay_Required := False;
ddf1337b 3326
89f1e35c 3327 -- In general cases, the corresponding pragma/attribute
3328 -- definition clause will be inserted later at the freezing
294709fa 3329 -- point, and we do not need to build it now.
ddf1337b 3330
89f1e35c 3331 else
3332 Aitem := Empty;
3333 end if;
ceec4f7c 3334
3335 -- Storage_Size
3336
3337 -- This is special because for access types we need to generate
3338 -- an attribute definition clause. This also works for single
3339 -- task declarations, but it does not work for task type
3340 -- declarations, because we have the case where the expression
3341 -- references a discriminant of the task type. That can't use
3342 -- an attribute definition clause because we would not have
3343 -- visibility on the discriminant. For that case we must
3344 -- generate a pragma in the task definition.
3345
3346 when Aspect_Storage_Size =>
3347
3348 -- Task type case
3349
3350 if Ekind (E) = E_Task_Type then
3351 declare
3352 Decl : constant Node_Id := Declaration_Node (E);
3353
3354 begin
3355 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3356
3357 -- If no task definition, create one
3358
3359 if No (Task_Definition (Decl)) then
3360 Set_Task_Definition (Decl,
3361 Make_Task_Definition (Loc,
3362 Visible_Declarations => Empty_List,
3363 End_Label => Empty));
3364 end if;
3365
51fa2a45 3366 -- Create a pragma and put it at the start of the task
3367 -- definition for the task type declaration.
ceec4f7c 3368
3369 Make_Aitem_Pragma
3370 (Pragma_Argument_Associations => New_List (
3371 Make_Pragma_Argument_Association (Loc,
3372 Expression => Relocate_Node (Expr))),
3373 Pragma_Name => Name_Storage_Size);
3374
3375 Prepend
3376 (Aitem,
3377 Visible_Declarations (Task_Definition (Decl)));
3378 goto Continue;
3379 end;
3380
3381 -- All other cases, generate attribute definition
3382
3383 else
3384 Aitem :=
3385 Make_Attribute_Definition_Clause (Loc,
3386 Name => Ent,
3387 Chars => Chars (Id),
3388 Expression => Relocate_Node (Expr));
3389 end if;
89f1e35c 3390 end case;
ddf1337b 3391
89f1e35c 3392 -- Attach the corresponding pragma/attribute definition clause to
3393 -- the aspect specification node.
d74fc39a 3394
89f1e35c 3395 if Present (Aitem) then
e2bf777d 3396 Set_From_Aspect_Specification (Aitem);
89f1e35c 3397 end if;
53c179ea 3398
89f1e35c 3399 -- In the context of a compilation unit, we directly put the
0fd13d32 3400 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3401 -- node (no delay is required here) except for aspects on a
51fa2a45 3402 -- subprogram body (see below) and a generic package, for which we
3403 -- need to introduce the pragma before building the generic copy
3404 -- (see sem_ch12), and for package instantiations, where the
3405 -- library unit pragmas are better handled early.
ddf1337b 3406
9129c28f 3407 if Nkind (Parent (N)) = N_Compilation_Unit
89f1e35c 3408 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3409 then
3410 declare
3411 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
7f694ca2 3412
89f1e35c 3413 begin
3414 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
7f694ca2 3415
89f1e35c 3416 -- For a Boolean aspect, create the corresponding pragma if
3417 -- no expression or if the value is True.
7f694ca2 3418
b9e61b2a 3419 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
89f1e35c 3420 if Is_True (Static_Boolean (Expr)) then
0fd13d32 3421 Make_Aitem_Pragma
3422 (Pragma_Argument_Associations => New_List (
3423 Make_Pragma_Argument_Association (Sloc (Ent),
3424 Expression => Ent)),
3425 Pragma_Name => Chars (Id));
7f694ca2 3426
89f1e35c 3427 Set_From_Aspect_Specification (Aitem, True);
3428 Set_Corresponding_Aspect (Aitem, Aspect);
3429
3430 else
3431 goto Continue;
3432 end if;
3433 end if;
7f694ca2 3434
d6814978 3435 -- If the aspect is on a subprogram body (relevant aspect
3436 -- is Inline), add the pragma in front of the declarations.
3a72f9c3 3437
3438 if Nkind (N) = N_Subprogram_Body then
3439 if No (Declarations (N)) then
3440 Set_Declarations (N, New_List);
3441 end if;
3442
3443 Prepend (Aitem, Declarations (N));
3444
178fec9b 3445 elsif Nkind (N) = N_Generic_Package_Declaration then
3446 if No (Visible_Declarations (Specification (N))) then
3447 Set_Visible_Declarations (Specification (N), New_List);
3448 end if;
3449
3450 Prepend (Aitem,
3451 Visible_Declarations (Specification (N)));
3452
c39cce40 3453 elsif Nkind (N) = N_Package_Instantiation then
df8b0dae 3454 declare
3455 Spec : constant Node_Id :=
3456 Specification (Instance_Spec (N));
3457 begin
3458 if No (Visible_Declarations (Spec)) then
3459 Set_Visible_Declarations (Spec, New_List);
3460 end if;
3461
3462 Prepend (Aitem, Visible_Declarations (Spec));
3463 end;
3464
3a72f9c3 3465 else
3466 if No (Pragmas_After (Aux)) then
d4596fbe 3467 Set_Pragmas_After (Aux, New_List);
3a72f9c3 3468 end if;
3469
3470 Append (Aitem, Pragmas_After (Aux));
89f1e35c 3471 end if;
7f694ca2 3472
89f1e35c 3473 goto Continue;
3474 end;
3475 end if;
7f694ca2 3476
89f1e35c 3477 -- The evaluation of the aspect is delayed to the freezing point.
3478 -- The pragma or attribute clause if there is one is then attached
37c6e44c 3479 -- to the aspect specification which is put in the rep item list.
1a814552 3480
89f1e35c 3481 if Delay_Required then
3482 if Present (Aitem) then
3483 Set_Is_Delayed_Aspect (Aitem);
3484 Set_Aspect_Rep_Item (Aspect, Aitem);
3485 Set_Parent (Aitem, Aspect);
3486 end if;
1a814552 3487
89f1e35c 3488 Set_Is_Delayed_Aspect (Aspect);
9f36e3fb 3489
cba2ae82 3490 -- In the case of Default_Value, link the aspect to base type
3491 -- as well, even though it appears on a first subtype. This is
3492 -- mandated by the semantics of the aspect. Do not establish
3493 -- the link when processing the base type itself as this leads
3494 -- to a rep item circularity. Verify that we are dealing with
3495 -- a scalar type to prevent cascaded errors.
3496
3497 if A_Id = Aspect_Default_Value
3498 and then Is_Scalar_Type (E)
3499 and then Base_Type (E) /= E
3500 then
9f36e3fb 3501 Set_Has_Delayed_Aspects (Base_Type (E));
3502 Record_Rep_Item (Base_Type (E), Aspect);
3503 end if;
3504
89f1e35c 3505 Set_Has_Delayed_Aspects (E);
3506 Record_Rep_Item (E, Aspect);
ddf1337b 3507
b855559d 3508 -- When delay is not required and the context is a package or a
3509 -- subprogram body, insert the pragma in the body declarations.
f55ce169 3510
b855559d 3511 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
f55ce169 3512 if No (Declarations (N)) then
3513 Set_Declarations (N, New_List);
3514 end if;
3515
3516 -- The pragma is added before source declarations
3517
3518 Prepend_To (Declarations (N), Aitem);
3519
89f1e35c 3520 -- When delay is not required and the context is not a compilation
3521 -- unit, we simply insert the pragma/attribute definition clause
3522 -- in sequence.
ddf1337b 3523
89f1e35c 3524 else
3525 Insert_After (Ins_Node, Aitem);
3526 Ins_Node := Aitem;
d74fc39a 3527 end if;
0fd13d32 3528 end Analyze_One_Aspect;
ae888dbd 3529
d64221a7 3530 <<Continue>>
3531 Next (Aspect);
21ea3a4f 3532 end loop Aspect_Loop;
89f1e35c 3533
3534 if Has_Delayed_Aspects (E) then
3535 Ensure_Freeze_Node (E);
3536 end if;
21ea3a4f 3537 end Analyze_Aspect_Specifications;
ae888dbd 3538
eb8aeefc 3539 ---------------------------------------------------
3540 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3541 ---------------------------------------------------
3542
3543 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3544 Body_Id : constant Entity_Id := Defining_Entity (N);
3545
3546 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3547 -- Subprogram body [stub] N has aspects, but they are not properly
3548 -- placed. Emit an error message depending on the aspects involved.
3549 -- Spec_Id is the entity of the corresponding spec.
3550
3551 --------------------------------
3552 -- Diagnose_Misplaced_Aspects --
3553 --------------------------------
3554
3555 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3556 procedure Misplaced_Aspect_Error
3557 (Asp : Node_Id;
3558 Ref_Nam : Name_Id);
3559 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3560 -- the name of the refined version of the aspect.
3561
3562 ----------------------------
3563 -- Misplaced_Aspect_Error --
3564 ----------------------------
3565
3566 procedure Misplaced_Aspect_Error
3567 (Asp : Node_Id;
3568 Ref_Nam : Name_Id)
3569 is
3570 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3571 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3572
3573 begin
3574 -- The corresponding spec already contains the aspect in question
3575 -- and the one appearing on the body must be the refined form:
3576
3577 -- procedure P with Global ...;
3578 -- procedure P with Global ... is ... end P;
3579 -- ^
3580 -- Refined_Global
3581
3582 if Has_Aspect (Spec_Id, Asp_Id) then
3583 Error_Msg_Name_1 := Asp_Nam;
3584
3585 -- Subunits cannot carry aspects that apply to a subprogram
3586 -- declaration.
3587
3588 if Nkind (Parent (N)) = N_Subunit then
3589 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3590
3591 -- Otherwise suggest the refined form
3592
3593 else
3594 Error_Msg_Name_2 := Ref_Nam;
3595 Error_Msg_N ("aspect % should be %", Asp);
3596 end if;
3597
3598 -- Otherwise the aspect must appear on the spec, not on the body
3599
3600 -- procedure P;
3601 -- procedure P with Global ... is ... end P;
3602
3603 else
3604 Error_Msg_N
3605 ("aspect specification must appear in subprogram declaration",
3606 Asp);
3607 end if;
3608 end Misplaced_Aspect_Error;
3609
3610 -- Local variables
3611
3612 Asp : Node_Id;
3613 Asp_Nam : Name_Id;
3614
3615 -- Start of processing for Diagnose_Misplaced_Aspects
3616
3617 begin
3618 -- Iterate over the aspect specifications and emit specific errors
3619 -- where applicable.
3620
3621 Asp := First (Aspect_Specifications (N));
3622 while Present (Asp) loop
3623 Asp_Nam := Chars (Identifier (Asp));
3624
3625 -- Do not emit errors on aspects that can appear on a subprogram
3626 -- body. This scenario occurs when the aspect specification list
3627 -- contains both misplaced and properly placed aspects.
3628
3629 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3630 null;
3631
3632 -- Special diagnostics for SPARK aspects
3633
3634 elsif Asp_Nam = Name_Depends then
3635 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3636
3637 elsif Asp_Nam = Name_Global then
3638 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3639
3640 elsif Asp_Nam = Name_Post then
3641 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3642
3643 -- Otherwise a language-defined aspect is misplaced
3644
3645 else
3646 Error_Msg_N
3647 ("aspect specification must appear in subprogram declaration",
3648 Asp);
3649 end if;
3650
3651 Next (Asp);
3652 end loop;
3653 end Diagnose_Misplaced_Aspects;
3654
3655 -- Local variables
3656
3657 Spec_Id : Entity_Id;
3658
3659 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3660
3661 begin
3662 if Nkind (N) = N_Subprogram_Body_Stub then
3663 Spec_Id := Corresponding_Spec_Of_Stub (N);
3664 else
3665 Spec_Id := Corresponding_Spec (N);
3666 end if;
3667
3668 -- Language-defined aspects cannot be associated with a subprogram body
3669 -- [stub] if the subprogram has a spec. Certain implementation defined
3670 -- aspects are allowed to break this rule (for all applicable cases, see
3671 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3672
3673 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
3674 Diagnose_Misplaced_Aspects (Spec_Id);
3675 else
3676 Analyze_Aspect_Specifications (N, Body_Id);
3677 end if;
3678 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3679
d6f39728 3680 -----------------------
3681 -- Analyze_At_Clause --
3682 -----------------------
3683
3684 -- An at clause is replaced by the corresponding Address attribute
3685 -- definition clause that is the preferred approach in Ada 95.
3686
3687 procedure Analyze_At_Clause (N : Node_Id) is
177675a7 3688 CS : constant Boolean := Comes_From_Source (N);
3689
d6f39728 3690 begin
177675a7 3691 -- This is an obsolescent feature
3692
e0521a36 3693 Check_Restriction (No_Obsolescent_Features, N);
3694
9dfe12ae 3695 if Warn_On_Obsolescent_Feature then
3696 Error_Msg_N
b174444e 3697 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
9dfe12ae 3698 Error_Msg_N
b174444e 3699 ("\?j?use address attribute definition clause instead", N);
9dfe12ae 3700 end if;
3701
177675a7 3702 -- Rewrite as address clause
3703
d6f39728 3704 Rewrite (N,
3705 Make_Attribute_Definition_Clause (Sloc (N),
935e86e0 3706 Name => Identifier (N),
3707 Chars => Name_Address,
d6f39728 3708 Expression => Expression (N)));
177675a7 3709
2beb22b1 3710 -- We preserve Comes_From_Source, since logically the clause still comes
3711 -- from the source program even though it is changed in form.
177675a7 3712
3713 Set_Comes_From_Source (N, CS);
3714
3715 -- Analyze rewritten clause
3716
d6f39728 3717 Analyze_Attribute_Definition_Clause (N);
3718 end Analyze_At_Clause;
3719
3720 -----------------------------------------
3721 -- Analyze_Attribute_Definition_Clause --
3722 -----------------------------------------
3723
3724 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3725 Loc : constant Source_Ptr := Sloc (N);
3726 Nam : constant Node_Id := Name (N);
3727 Attr : constant Name_Id := Chars (N);
3728 Expr : constant Node_Id := Expression (N);
3729 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
d64221a7 3730
3731 Ent : Entity_Id;
3732 -- The entity of Nam after it is analyzed. In the case of an incomplete
3733 -- type, this is the underlying type.
3734
d6f39728 3735 U_Ent : Entity_Id;
d64221a7 3736 -- The underlying entity to which the attribute applies. Generally this
3737 -- is the Underlying_Type of Ent, except in the case where the clause
3738 -- applies to full view of incomplete type or private type in which case
3739 -- U_Ent is just a copy of Ent.
d6f39728 3740
3741 FOnly : Boolean := False;
3742 -- Reset to True for subtype specific attribute (Alignment, Size)
51fa2a45 3743 -- and for stream attributes, i.e. those cases where in the call to
3744 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3745 -- are checked. Note that the case of stream attributes is not clear
3746 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3747 -- Storage_Size for derived task types, but that is also clearly
3748 -- unintentional.
d6f39728 3749
9f373bb8 3750 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3751 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3752 -- definition clauses.
3753
ae888dbd 3754 function Duplicate_Clause return Boolean;
3755 -- This routine checks if the aspect for U_Ent being given by attribute
3756 -- definition clause N is for an aspect that has already been specified,
3757 -- and if so gives an error message. If there is a duplicate, True is
3758 -- returned, otherwise if there is no error, False is returned.
3759
81b424ac 3760 procedure Check_Indexing_Functions;
3761 -- Check that the function in Constant_Indexing or Variable_Indexing
3762 -- attribute has the proper type structure. If the name is overloaded,
cac18f71 3763 -- check that some interpretation is legal.
81b424ac 3764
89cc7147 3765 procedure Check_Iterator_Functions;
3766 -- Check that there is a single function in Default_Iterator attribute
8df4f2a5 3767 -- has the proper type structure.
89cc7147 3768
3769 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
d03bfaa1 3770 -- Common legality check for the previous two
89cc7147 3771
177675a7 3772 -----------------------------------
3773 -- Analyze_Stream_TSS_Definition --
3774 -----------------------------------
3775
9f373bb8 3776 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3777 Subp : Entity_Id := Empty;
3778 I : Interp_Index;
3779 It : Interp;
3780 Pnam : Entity_Id;
3781
3782 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
d64221a7 3783 -- True for Read attribute, false for other attributes
9f373bb8 3784
3785 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3786 -- Return true if the entity is a subprogram with an appropriate
3787 -- profile for the attribute being defined.
3788
3789 ----------------------
3790 -- Has_Good_Profile --
3791 ----------------------
3792
3793 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3794 F : Entity_Id;
3795 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3796 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3797 (False => E_Procedure, True => E_Function);
3798 Typ : Entity_Id;
3799
3800 begin
3801 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3802 return False;
3803 end if;
3804
3805 F := First_Formal (Subp);
3806
3807 if No (F)
3808 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3809 or else Designated_Type (Etype (F)) /=
3810 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3811 then
3812 return False;
3813 end if;
3814
3815 if not Is_Function then
3816 Next_Formal (F);
3817
3818 declare
3819 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3820 (False => E_In_Parameter,
3821 True => E_Out_Parameter);
3822 begin
3823 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3824 return False;
3825 end if;
3826 end;
3827
3828 Typ := Etype (F);
3829
b64082f2 3830 -- If the attribute specification comes from an aspect
51fa2a45 3831 -- specification for a class-wide stream, the parameter must be
3832 -- a class-wide type of the entity to which the aspect applies.
b64082f2 3833
3834 if From_Aspect_Specification (N)
3835 and then Class_Present (Parent (N))
3836 and then Is_Class_Wide_Type (Typ)
3837 then
3838 Typ := Etype (Typ);
3839 end if;
3840
9f373bb8 3841 else
3842 Typ := Etype (Subp);
3843 end if;
3844
51fa2a45 3845 -- Verify that the prefix of the attribute and the local name for
5a8fe506 3846 -- the type of the formal match, or one is the class-wide of the
3847 -- other, in the case of a class-wide stream operation.
48680a09 3848
b8eacb12 3849 if Base_Type (Typ) = Base_Type (Ent)
5a8fe506 3850 or else (Is_Class_Wide_Type (Typ)
2be1f7d7 3851 and then Typ = Class_Wide_Type (Base_Type (Ent)))
fbf4d6ef 3852 or else (Is_Class_Wide_Type (Ent)
3853 and then Ent = Class_Wide_Type (Base_Type (Typ)))
5a8fe506 3854 then
3855 null;
3856 else
3857 return False;
3858 end if;
3859
3860 if Present ((Next_Formal (F)))
48680a09 3861 then
3862 return False;
3863
3864 elsif not Is_Scalar_Type (Typ)
3865 and then not Is_First_Subtype (Typ)
3866 and then not Is_Class_Wide_Type (Typ)
3867 then
3868 return False;
3869
3870 else
3871 return True;
3872 end if;
9f373bb8 3873 end Has_Good_Profile;
3874
3875 -- Start of processing for Analyze_Stream_TSS_Definition
3876
3877 begin
3878 FOnly := True;
3879
3880 if not Is_Type (U_Ent) then
3881 Error_Msg_N ("local name must be a subtype", Nam);
3882 return;
48680a09 3883
3884 elsif not Is_First_Subtype (U_Ent) then
3885 Error_Msg_N ("local name must be a first subtype", Nam);
3886 return;
9f373bb8 3887 end if;
3888
3889 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3890
44e4341e 3891 -- If Pnam is present, it can be either inherited from an ancestor
3892 -- type (in which case it is legal to redefine it for this type), or
3893 -- be a previous definition of the attribute for the same type (in
3894 -- which case it is illegal).
3895
3896 -- In the first case, it will have been analyzed already, and we
3897 -- can check that its profile does not match the expected profile
3898 -- for a stream attribute of U_Ent. In the second case, either Pnam
3899 -- has been analyzed (and has the expected profile), or it has not
3900 -- been analyzed yet (case of a type that has not been frozen yet
3901 -- and for which the stream attribute has been set using Set_TSS).
3902
3903 if Present (Pnam)
3904 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3905 then
9f373bb8 3906 Error_Msg_Sloc := Sloc (Pnam);
3907 Error_Msg_Name_1 := Attr;
3908 Error_Msg_N ("% attribute already defined #", Nam);
3909 return;
3910 end if;
3911
3912 Analyze (Expr);
3913
3914 if Is_Entity_Name (Expr) then
3915 if not Is_Overloaded (Expr) then
3916 if Has_Good_Profile (Entity (Expr)) then
3917 Subp := Entity (Expr);
3918 end if;
3919
3920 else
3921 Get_First_Interp (Expr, I, It);
9f373bb8 3922 while Present (It.Nam) loop
3923 if Has_Good_Profile (It.Nam) then
3924 Subp := It.Nam;
3925 exit;
3926 end if;
3927
3928 Get_Next_Interp (I, It);
3929 end loop;
3930 end if;
3931 end if;
3932
3933 if Present (Subp) then
59ac57b5 3934 if Is_Abstract_Subprogram (Subp) then
9f373bb8 3935 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3936 return;
e12b2502 3937
299b347e 3938 -- A stream subprogram for an interface type must be a null
3939 -- procedure (RM 13.13.2 (38/3)).
e12b2502 3940
3941 elsif Is_Interface (U_Ent)
5a8fe506 3942 and then not Is_Class_Wide_Type (U_Ent)
e12b2502 3943 and then not Inside_A_Generic
e12b2502 3944 and then
5a8fe506 3945 (Ekind (Subp) = E_Function
3946 or else
3947 not Null_Present
2be1f7d7 3948 (Specification
3949 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
e12b2502 3950 then
3951 Error_Msg_N
3952 ("stream subprogram for interface type "
3953 & "must be null procedure", Expr);
9f373bb8 3954 end if;
3955
3956 Set_Entity (Expr, Subp);
3957 Set_Etype (Expr, Etype (Subp));
3958
44e4341e 3959 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
9f373bb8 3960
3961 else
3962 Error_Msg_Name_1 := Attr;
3963 Error_Msg_N ("incorrect expression for% attribute", Expr);
3964 end if;
3965 end Analyze_Stream_TSS_Definition;
3966
81b424ac 3967 ------------------------------
3968 -- Check_Indexing_Functions --
3969 ------------------------------
3970
3971 procedure Check_Indexing_Functions is
c8a2d809 3972 Indexing_Found : Boolean := False;
8df4f2a5 3973
44d567c8 3974 procedure Check_Inherited_Indexing;
3975 -- For a derived type, check that no indexing aspect is specified
3976 -- for the type if it is also inherited
3977
81b424ac 3978 procedure Check_One_Function (Subp : Entity_Id);
7796365f 3979 -- Check one possible interpretation. Sets Indexing_Found True if a
3980 -- legal indexing function is found.
81b424ac 3981
05987af3 3982 procedure Illegal_Indexing (Msg : String);
3983 -- Diagnose illegal indexing function if not overloaded. In the
3984 -- overloaded case indicate that no legal interpretation exists.
3985
44d567c8 3986 ------------------------------
3987 -- Check_Inherited_Indexing --
3988 ------------------------------
3989
3990 procedure Check_Inherited_Indexing is
3991 Inherited : Node_Id;
3992
3993 begin
3994 if Attr = Name_Constant_Indexing then
3995 Inherited :=
3996 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
3997 else pragma Assert (Attr = Name_Variable_Indexing);
3998 Inherited :=
3999 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4000 end if;
4001
4002 if Present (Inherited) then
4003 if Debug_Flag_Dot_XX then
4004 null;
4005
4006 -- OK if current attribute_definition_clause is expansion
4007 -- of inherited aspect.
4008
4009 elsif Aspect_Rep_Item (Inherited) = N then
4010 null;
4011
4012 -- Indicate the operation that must be overridden, rather
4013 -- than redefining the indexing aspect
4014
4015 else
4016 Illegal_Indexing
f2837ceb 4017 ("indexing function already inherited from parent type");
44d567c8 4018 Error_Msg_NE
4019 ("!override & instead",
4020 N, Entity (Expression (Inherited)));
4021 end if;
4022 end if;
4023 end Check_Inherited_Indexing;
4024
81b424ac 4025 ------------------------
4026 -- Check_One_Function --
4027 ------------------------
4028
4029 procedure Check_One_Function (Subp : Entity_Id) is
05987af3 4030 Default_Element : Node_Id;
4031 Ret_Type : constant Entity_Id := Etype (Subp);
1b7510f9 4032
81b424ac 4033 begin
05987af3 4034 if not Is_Overloadable (Subp) then
4035 Illegal_Indexing ("illegal indexing function for type&");
4036 return;
4037
7796365f 4038 elsif Scope (Subp) /= Scope (Ent) then
4039 if Nkind (Expr) = N_Expanded_Name then
4040
4041 -- Indexing function can't be declared elsewhere
4042
4043 Illegal_Indexing
4044 ("indexing function must be declared in scope of type&");
4045 end if;
4046
05987af3 4047 return;
4048
4049 elsif No (First_Formal (Subp)) then
4050 Illegal_Indexing
4051 ("Indexing requires a function that applies to type&");
4052 return;
4053
4054 elsif No (Next_Formal (First_Formal (Subp))) then
4055 Illegal_Indexing
2eb0ff42 4056 ("indexing function must have at least two parameters");
05987af3 4057 return;
4058
4059 elsif Is_Derived_Type (Ent) then
44d567c8 4060 Check_Inherited_Indexing;
05987af3 4061 end if;
4062
e81df51c 4063 if not Check_Primitive_Function (Subp) then
05987af3 4064 Illegal_Indexing
4065 ("Indexing aspect requires a function that applies to type&");
4066 return;
81b424ac 4067 end if;
4068
7796365f 4069 -- If partial declaration exists, verify that it is not tagged.
4070
4071 if Ekind (Current_Scope) = E_Package
4072 and then Has_Private_Declaration (Ent)
4073 and then From_Aspect_Specification (N)
7c0c95b8 4074 and then
4075 List_Containing (Parent (Ent)) =
4076 Private_Declarations
7796365f 4077 (Specification (Unit_Declaration_Node (Current_Scope)))
4078 and then Nkind (N) = N_Attribute_Definition_Clause
4079 then
4080 declare
4081 Decl : Node_Id;
4082
4083 begin
4084 Decl :=
4085 First (Visible_Declarations
7c0c95b8 4086 (Specification
4087 (Unit_Declaration_Node (Current_Scope))));
7796365f 4088
4089 while Present (Decl) loop
4090 if Nkind (Decl) = N_Private_Type_Declaration
4091 and then Ent = Full_View (Defining_Identifier (Decl))
4092 and then Tagged_Present (Decl)
4093 and then No (Aspect_Specifications (Decl))
4094 then
4095 Illegal_Indexing
4096 ("Indexing aspect cannot be specified on full view "
7c0c95b8 4097 & "if partial view is tagged");
7796365f 4098 return;
4099 end if;
4100
4101 Next (Decl);
4102 end loop;
4103 end;
4104 end if;
4105
1b7510f9 4106 -- An indexing function must return either the default element of
cac18f71 4107 -- the container, or a reference type. For variable indexing it
a45d946f 4108 -- must be the latter.
1b7510f9 4109
05987af3 4110 Default_Element :=
4111 Find_Value_Of_Aspect
4112 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4113
1b7510f9 4114 if Present (Default_Element) then
4115 Analyze (Default_Element);
a45d946f 4116
1b7510f9 4117 if Is_Entity_Name (Default_Element)
05987af3 4118 and then not Covers (Entity (Default_Element), Ret_Type)
4119 and then False
1b7510f9 4120 then
05987af3 4121 Illegal_Indexing
4122 ("wrong return type for indexing function");
1b7510f9 4123 return;
4124 end if;
4125 end if;
4126
a45d946f 4127 -- For variable_indexing the return type must be a reference type
1b7510f9 4128
05987af3 4129 if Attr = Name_Variable_Indexing then
4130 if not Has_Implicit_Dereference (Ret_Type) then
4131 Illegal_Indexing
4132 ("variable indexing must return a reference type");
4133 return;
4134
423b89fd 4135 elsif Is_Access_Constant
4136 (Etype (First_Discriminant (Ret_Type)))
05987af3 4137 then
4138 Illegal_Indexing
4139 ("variable indexing must return an access to variable");
4140 return;
4141 end if;
cac18f71 4142
4143 else
05987af3 4144 if Has_Implicit_Dereference (Ret_Type)
4145 and then not
4146 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4147 then
4148 Illegal_Indexing
4149 ("constant indexing must return an access to constant");
4150 return;
4151
4152 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4153 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4154 then
4155 Illegal_Indexing
4156 ("constant indexing must apply to an access to constant");
4157 return;
4158 end if;
81b424ac 4159 end if;
05987af3 4160
4161 -- All checks succeeded.
4162
4163 Indexing_Found := True;
81b424ac 4164 end Check_One_Function;
4165
05987af3 4166 -----------------------
4167 -- Illegal_Indexing --
4168 -----------------------
4169
4170 procedure Illegal_Indexing (Msg : String) is
4171 begin
7796365f 4172 Error_Msg_NE (Msg, N, Ent);
05987af3 4173 end Illegal_Indexing;
4174
81b424ac 4175 -- Start of processing for Check_Indexing_Functions
4176
4177 begin
89cc7147 4178 if In_Instance then
44d567c8 4179 Check_Inherited_Indexing;
89cc7147 4180 end if;
4181
81b424ac 4182 Analyze (Expr);
4183
4184 if not Is_Overloaded (Expr) then
4185 Check_One_Function (Entity (Expr));
4186
4187 else
4188 declare
2c5754de 4189 I : Interp_Index;
81b424ac 4190 It : Interp;
4191
4192 begin
cac18f71 4193 Indexing_Found := False;
81b424ac 4194 Get_First_Interp (Expr, I, It);
4195 while Present (It.Nam) loop
4196
4197 -- Note that analysis will have added the interpretation
4198 -- that corresponds to the dereference. We only check the
4199 -- subprogram itself.
4200
4201 if Is_Overloadable (It.Nam) then
4202 Check_One_Function (It.Nam);
4203 end if;
4204
4205 Get_Next_Interp (I, It);
4206 end loop;
4207 end;
4208 end if;
7796365f 4209
7c0c95b8 4210 if not Indexing_Found and then not Error_Posted (N) then
7796365f 4211 Error_Msg_NE
4212 ("aspect Indexing requires a local function that "
4213 & "applies to type&", Expr, Ent);
4214 end if;
81b424ac 4215 end Check_Indexing_Functions;
4216
89cc7147 4217 ------------------------------
4218 -- Check_Iterator_Functions --
4219 ------------------------------
4220
4221 procedure Check_Iterator_Functions is
89cc7147 4222 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
8df4f2a5 4223 -- Check one possible interpretation for validity
89cc7147 4224
4225 ----------------------------
4226 -- Valid_Default_Iterator --
4227 ----------------------------
4228
4229 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4230 Formal : Entity_Id;
4231
4232 begin
4233 if not Check_Primitive_Function (Subp) then
4234 return False;
4235 else
4236 Formal := First_Formal (Subp);
4237 end if;
4238
8df4f2a5 4239 -- False if any subsequent formal has no default expression
89cc7147 4240
8df4f2a5 4241 Formal := Next_Formal (Formal);
4242 while Present (Formal) loop
4243 if No (Expression (Parent (Formal))) then
4244 return False;
4245 end if;
89cc7147 4246
8df4f2a5 4247 Next_Formal (Formal);
4248 end loop;
89cc7147 4249
8df4f2a5 4250 -- True if all subsequent formals have default expressions
89cc7147 4251
4252 return True;
4253 end Valid_Default_Iterator;
4254
4255 -- Start of processing for Check_Iterator_Functions
4256
4257 begin
4258 Analyze (Expr);
4259
4260 if not Is_Entity_Name (Expr) then
4261 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4262 end if;
4263
4264 if not Is_Overloaded (Expr) then
4265 if not Check_Primitive_Function (Entity (Expr)) then
4266 Error_Msg_NE
4267 ("aspect Indexing requires a function that applies to type&",
4268 Entity (Expr), Ent);
4269 end if;
4270
05f6f999 4271 -- Flag the default_iterator as well as the denoted function.
4272
89cc7147 4273 if not Valid_Default_Iterator (Entity (Expr)) then
05f6f999 4274 Error_Msg_N ("improper function for default iterator!", Expr);
89cc7147 4275 end if;
4276
4277 else
89cc7147 4278 declare
270ee9c5 4279 Default : Entity_Id := Empty;
89cc7147 4280 I : Interp_Index;
4281 It : Interp;
4282
4283 begin
4284 Get_First_Interp (Expr, I, It);
4285 while Present (It.Nam) loop
4286 if not Check_Primitive_Function (It.Nam)
59f3e675 4287 or else not Valid_Default_Iterator (It.Nam)
89cc7147 4288 then
4289 Remove_Interp (I);
4290
4291 elsif Present (Default) then
4292 Error_Msg_N ("default iterator must be unique", Expr);
270ee9c5 4293 Error_Msg_Sloc := Sloc (Default);
4294 Error_Msg_N ("\\possible interpretation#", Expr);
4295 Error_Msg_Sloc := Sloc (It.Nam);
4296 Error_Msg_N ("\\possible interpretation#", Expr);
89cc7147 4297
4298 else
4299 Default := It.Nam;
4300 end if;
4301
4302 Get_Next_Interp (I, It);
4303 end loop;
89cc7147 4304
270ee9c5 4305 if Present (Default) then
4306 Set_Entity (Expr, Default);
4307 Set_Is_Overloaded (Expr, False);
4308 end if;
4309 end;
89cc7147 4310 end if;
4311 end Check_Iterator_Functions;
4312
4313 -------------------------------
4314 -- Check_Primitive_Function --
4315 -------------------------------
4316
4317 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4318 Ctrl : Entity_Id;
4319
4320 begin
4321 if Ekind (Subp) /= E_Function then
4322 return False;
4323 end if;
4324
4325 if No (First_Formal (Subp)) then
4326 return False;
4327 else
4328 Ctrl := Etype (First_Formal (Subp));
4329 end if;
4330
05f6f999 4331 -- To be a primitive operation subprogram has to be in same scope.
4332
4333 if Scope (Ctrl) /= Scope (Subp) then
4334 return False;
4335 end if;
4336
7d6fb253 4337 -- Type of formal may be the class-wide type, an access to such,
4338 -- or an incomplete view.
4339
89cc7147 4340 if Ctrl = Ent
4341 or else Ctrl = Class_Wide_Type (Ent)
4342 or else
4343 (Ekind (Ctrl) = E_Anonymous_Access_Type
b85d62ec 4344 and then (Designated_Type (Ctrl) = Ent
4345 or else
4346 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
7d6fb253 4347 or else
4348 (Ekind (Ctrl) = E_Incomplete_Type
4349 and then Full_View (Ctrl) = Ent)
89cc7147 4350 then
4351 null;
89cc7147 4352 else
4353 return False;
4354 end if;
4355
4356 return True;
4357 end Check_Primitive_Function;
4358
ae888dbd 4359 ----------------------
4360 -- Duplicate_Clause --
4361 ----------------------
4362
4363 function Duplicate_Clause return Boolean is
d74fc39a 4364 A : Node_Id;
ae888dbd 4365
4366 begin
c8969ba6 4367 -- Nothing to do if this attribute definition clause comes from
4368 -- an aspect specification, since we could not be duplicating an
ae888dbd 4369 -- explicit clause, and we dealt with the case of duplicated aspects
4370 -- in Analyze_Aspect_Specifications.
4371
4372 if From_Aspect_Specification (N) then
4373 return False;
4374 end if;
4375
89f1e35c 4376 -- Otherwise current clause may duplicate previous clause, or a
4377 -- previously given pragma or aspect specification for the same
4378 -- aspect.
d74fc39a 4379
89b3b365 4380 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
ae888dbd 4381
4382 if Present (A) then
89f1e35c 4383 Error_Msg_Name_1 := Chars (N);
4384 Error_Msg_Sloc := Sloc (A);
4385
89b3b365 4386 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
89f1e35c 4387 return True;
ae888dbd 4388 end if;
4389
4390 return False;
4391 end Duplicate_Clause;
4392
9f373bb8 4393 -- Start of processing for Analyze_Attribute_Definition_Clause
4394
d6f39728 4395 begin
d64221a7 4396 -- The following code is a defense against recursion. Not clear that
51fa2a45 4397 -- this can happen legitimately, but perhaps some error situations can
4398 -- cause it, and we did see this recursion during testing.
d64221a7 4399
4400 if Analyzed (N) then
4401 return;
4402 else
4403 Set_Analyzed (N, True);
4404 end if;
4405
a29bc1d9 4406 -- Ignore some selected attributes in CodePeer mode since they are not
4407 -- relevant in this context.
4408
4409 if CodePeer_Mode then
4410 case Id is
4411
4412 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4413 -- internal representation of types by implicitly packing them.
4414
4415 when Attribute_Component_Size =>
4416 Rewrite (N, Make_Null_Statement (Sloc (N)));
4417 return;
4418
4419 when others =>
4420 null;
4421 end case;
4422 end if;
4423
d8ba53a8 4424 -- Process Ignore_Rep_Clauses option
eef1ca1e 4425
d8ba53a8 4426 if Ignore_Rep_Clauses then
9d627c41 4427 case Id is
4428
eef1ca1e 4429 -- The following should be ignored. They do not affect legality
4430 -- and may be target dependent. The basic idea of -gnatI is to
4431 -- ignore any rep clauses that may be target dependent but do not
4432 -- affect legality (except possibly to be rejected because they
4433 -- are incompatible with the compilation target).
9d627c41 4434
2f1aac99 4435 when Attribute_Alignment |
9d627c41 4436 Attribute_Bit_Order |
4437 Attribute_Component_Size |
4438 Attribute_Machine_Radix |
4439 Attribute_Object_Size |
4440 Attribute_Size |
2ff55065 4441 Attribute_Small |
9d627c41 4442 Attribute_Stream_Size |
4443 Attribute_Value_Size =>
2ff55065 4444 Kill_Rep_Clause (N);
9d627c41 4445 return;
4446
eef1ca1e 4447 -- The following should not be ignored, because in the first place
51fa2a45 4448 -- they are reasonably portable, and should not cause problems
4449 -- in compiling code from another target, and also they do affect
4450 -- legality, e.g. failing to provide a stream attribute for a type
4451 -- may make a program illegal.
9d627c41 4452
b55f7641 4453 when Attribute_External_Tag |
4454 Attribute_Input |
4455 Attribute_Output |
4456 Attribute_Read |
4457 Attribute_Simple_Storage_Pool |
4458 Attribute_Storage_Pool |
4459 Attribute_Storage_Size |
4460 Attribute_Write =>
9d627c41 4461 null;
4462
2ff55065 4463 -- We do not do anything here with address clauses, they will be
4464 -- removed by Freeze later on, but for now, it works better to
4465 -- keep then in the tree.
4466
4467 when Attribute_Address =>
4468 null;
4469
b593a52c 4470 -- Other cases are errors ("attribute& cannot be set with
4471 -- definition clause"), which will be caught below.
9d627c41 4472
4473 when others =>
4474 null;
4475 end case;
fbc67f84 4476 end if;
4477
d6f39728 4478 Analyze (Nam);
4479 Ent := Entity (Nam);
4480
4481 if Rep_Item_Too_Early (Ent, N) then
4482 return;
4483 end if;
4484
9f373bb8 4485 -- Rep clause applies to full view of incomplete type or private type if
4486 -- we have one (if not, this is a premature use of the type). However,
4487 -- certain semantic checks need to be done on the specified entity (i.e.
4488 -- the private view), so we save it in Ent.
d6f39728 4489
4490 if Is_Private_Type (Ent)
4491 and then Is_Derived_Type (Ent)
4492 and then not Is_Tagged_Type (Ent)
4493 and then No (Full_View (Ent))
4494 then
9f373bb8 4495 -- If this is a private type whose completion is a derivation from
4496 -- another private type, there is no full view, and the attribute
4497 -- belongs to the type itself, not its underlying parent.
d6f39728 4498
4499 U_Ent := Ent;
4500
4501 elsif Ekind (Ent) = E_Incomplete_Type then
d5b349fa 4502
9f373bb8 4503 -- The attribute applies to the full view, set the entity of the
4504 -- attribute definition accordingly.
d5b349fa 4505
d6f39728 4506 Ent := Underlying_Type (Ent);
4507 U_Ent := Ent;
d5b349fa 4508 Set_Entity (Nam, Ent);
4509
d6f39728 4510 else
4511 U_Ent := Underlying_Type (Ent);
4512 end if;
4513
44705307 4514 -- Avoid cascaded error
d6f39728 4515
4516 if Etype (Nam) = Any_Type then
4517 return;
4518
89f1e35c 4519 -- Must be declared in current scope or in case of an aspect
ace3389d 4520 -- specification, must be visible in current scope.
44705307 4521
89f1e35c 4522 elsif Scope (Ent) /= Current_Scope
ace3389d 4523 and then
4524 not (From_Aspect_Specification (N)
4525 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
89f1e35c 4526 then
d6f39728 4527 Error_Msg_N ("entity must be declared in this scope", Nam);
4528 return;
4529
44705307 4530 -- Must not be a source renaming (we do have some cases where the
4531 -- expander generates a renaming, and those cases are OK, in such
a3248fc4 4532 -- cases any attribute applies to the renamed object as well).
44705307 4533
4534 elsif Is_Object (Ent)
4535 and then Present (Renamed_Object (Ent))
44705307 4536 then
a3248fc4 4537 -- Case of renamed object from source, this is an error
4538
4539 if Comes_From_Source (Renamed_Object (Ent)) then
4540 Get_Name_String (Chars (N));
4541 Error_Msg_Strlen := Name_Len;
4542 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4543 Error_Msg_N
4544 ("~ clause not allowed for a renaming declaration "
4545 & "(RM 13.1(6))", Nam);
4546 return;
4547
4548 -- For the case of a compiler generated renaming, the attribute
4549 -- definition clause applies to the renamed object created by the
4550 -- expander. The easiest general way to handle this is to create a
4551 -- copy of the attribute definition clause for this object.
4552
9a48fc56 4553 elsif Is_Entity_Name (Renamed_Object (Ent)) then
a3248fc4 4554 Insert_Action (N,
4555 Make_Attribute_Definition_Clause (Loc,
4556 Name =>
4557 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4558 Chars => Chars (N),
4559 Expression => Duplicate_Subexpr (Expression (N))));
9a48fc56 4560
4561 -- If the renamed object is not an entity, it must be a dereference
4562 -- of an unconstrained function call, and we must introduce a new
4563 -- declaration to capture the expression. This is needed in the case
4564 -- of 'Alignment, where the original declaration must be rewritten.
4565
4566 else
4567 pragma Assert
4568 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4569 null;
a3248fc4 4570 end if;
44705307 4571
4572 -- If no underlying entity, use entity itself, applies to some
4573 -- previously detected error cases ???
4574
f15731c4 4575 elsif No (U_Ent) then
4576 U_Ent := Ent;
4577
44705307 4578 -- Cannot specify for a subtype (exception Object/Value_Size)
4579
d6f39728 4580 elsif Is_Type (U_Ent)
4581 and then not Is_First_Subtype (U_Ent)
4582 and then Id /= Attribute_Object_Size
4583 and then Id /= Attribute_Value_Size
4584 and then not From_At_Mod (N)
4585 then
4586 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4587 return;
d6f39728 4588 end if;
4589
ae888dbd 4590 Set_Entity (N, U_Ent);
25e23a77 4591 Check_Restriction_No_Use_Of_Attribute (N);
ae888dbd 4592
d6f39728 4593 -- Switch on particular attribute
4594
4595 case Id is
4596
4597 -------------
4598 -- Address --
4599 -------------
4600
4601 -- Address attribute definition clause
4602
4603 when Attribute_Address => Address : begin
177675a7 4604
4605 -- A little error check, catch for X'Address use X'Address;
4606
4607 if Nkind (Nam) = N_Identifier
4608 and then Nkind (Expr) = N_Attribute_Reference
4609 and then Attribute_Name (Expr) = Name_Address
4610 and then Nkind (Prefix (Expr)) = N_Identifier
4611 and then Chars (Nam) = Chars (Prefix (Expr))
4612 then
4613 Error_Msg_NE
4614 ("address for & is self-referencing", Prefix (Expr), Ent);
4615 return;
4616 end if;
4617
4618 -- Not that special case, carry on with analysis of expression
4619
d6f39728 4620 Analyze_And_Resolve (Expr, RTE (RE_Address));
4621
2f1aac99 4622 -- Even when ignoring rep clauses we need to indicate that the
4623 -- entity has an address clause and thus it is legal to declare
2ff55065 4624 -- it imported. Freeze will get rid of the address clause later.
2f1aac99 4625
4626 if Ignore_Rep_Clauses then
d3ef794c 4627 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2f1aac99 4628 Record_Rep_Item (U_Ent, N);
4629 end if;
4630
4631 return;
4632 end if;
4633
ae888dbd 4634 if Duplicate_Clause then
4635 null;
d6f39728 4636
4637 -- Case of address clause for subprogram
4638
4639 elsif Is_Subprogram (U_Ent) then
d6f39728 4640 if Has_Homonym (U_Ent) then
4641 Error_Msg_N
4642 ("address clause cannot be given " &
4643 "for overloaded subprogram",
4644 Nam);
83f8f0a6 4645 return;
d6f39728 4646 end if;
4647
83f8f0a6 4648 -- For subprograms, all address clauses are permitted, and we
4649 -- mark the subprogram as having a deferred freeze so that Gigi
4650 -- will not elaborate it too soon.
d6f39728 4651
4652 -- Above needs more comments, what is too soon about???
4653
4654 Set_Has_Delayed_Freeze (U_Ent);
4655
4656 -- Case of address clause for entry
4657
4658 elsif Ekind (U_Ent) = E_Entry then
d6f39728 4659 if Nkind (Parent (N)) = N_Task_Body then
4660 Error_Msg_N
4661 ("entry address must be specified in task spec", Nam);
83f8f0a6 4662 return;
d6f39728 4663 end if;
4664
4665 -- For entries, we require a constant address
4666
4667 Check_Constant_Address_Clause (Expr, U_Ent);
4668
83f8f0a6 4669 -- Special checks for task types
4670
f15731c4 4671 if Is_Task_Type (Scope (U_Ent))
4672 and then Comes_From_Source (Scope (U_Ent))
4673 then
4674 Error_Msg_N
1e3532e7 4675 ("??entry address declared for entry in task type", N);
f15731c4 4676 Error_Msg_N
1e3532e7 4677 ("\??only one task can be declared of this type", N);
f15731c4 4678 end if;
4679
83f8f0a6 4680 -- Entry address clauses are obsolescent
4681
e0521a36 4682 Check_Restriction (No_Obsolescent_Features, N);
4683
9dfe12ae 4684 if Warn_On_Obsolescent_Feature then
4685 Error_Msg_N
1e3532e7 4686 ("?j?attaching interrupt to task entry is an " &
4687 "obsolescent feature (RM J.7.1)", N);
9dfe12ae 4688 Error_Msg_N
1e3532e7 4689 ("\?j?use interrupt procedure instead", N);
9dfe12ae 4690 end if;
4691
83f8f0a6 4692 -- Case of an address clause for a controlled object which we
4693 -- consider to be erroneous.
9dfe12ae 4694
83f8f0a6 4695 elsif Is_Controlled (Etype (U_Ent))
4696 or else Has_Controlled_Component (Etype (U_Ent))
4697 then
9dfe12ae 4698 Error_Msg_NE
1e3532e7 4699 ("??controlled object& must not be overlaid", Nam, U_Ent);
9dfe12ae 4700 Error_Msg_N
1e3532e7 4701 ("\??Program_Error will be raised at run time", Nam);
9dfe12ae 4702 Insert_Action (Declaration_Node (U_Ent),
4703 Make_Raise_Program_Error (Loc,
4704 Reason => PE_Overlaid_Controlled_Object));
83f8f0a6 4705 return;
9dfe12ae 4706
4707 -- Case of address clause for a (non-controlled) object
d6f39728 4708
f02a9a9a 4709 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
d6f39728 4710 declare
d6da7448 4711 Expr : constant Node_Id := Expression (N);
4712 O_Ent : Entity_Id;
4713 Off : Boolean;
d6f39728 4714
4715 begin
7ee315cc 4716 -- Exported variables cannot have an address clause, because
4717 -- this cancels the effect of the pragma Export.
d6f39728 4718
4719 if Is_Exported (U_Ent) then
4720 Error_Msg_N
4721 ("cannot export object with address clause", Nam);
83f8f0a6 4722 return;
d6da7448 4723 end if;
4724
4725 Find_Overlaid_Entity (N, O_Ent, Off);
d6f39728 4726
0c30cda1 4727 -- Overlaying controlled objects is erroneous.
4728 -- Emit warning but continue analysis because program is
4729 -- itself legal, and back-end must see address clause.
9dfe12ae 4730
d6da7448 4731 if Present (O_Ent)
4732 and then (Has_Controlled_Component (Etype (O_Ent))
f02a9a9a 4733 or else Is_Controlled (Etype (O_Ent)))
0c30cda1 4734 and then not Inside_A_Generic
9dfe12ae 4735 then
4736 Error_Msg_N
0c30cda1 4737 ("??cannot use overlays with controlled objects", Expr);
9dfe12ae 4738 Error_Msg_N
1e3532e7 4739 ("\??Program_Error will be raised at run time", Expr);
9dfe12ae 4740 Insert_Action (Declaration_Node (U_Ent),
4741 Make_Raise_Program_Error (Loc,
4742 Reason => PE_Overlaid_Controlled_Object));
4743
95009d64 4744 -- Issue an unconditional warning for a constant overlaying
4745 -- a variable. For the reverse case, we will issue it only
4746 -- if the variable is modified, see below.
4747
4748 elsif Address_Clause_Overlay_Warnings
4749 and then Present (O_Ent)
9dfe12ae 4750 and then Ekind (U_Ent) = E_Constant
d6da7448 4751 and then not Is_Constant_Object (O_Ent)
9dfe12ae 4752 then
1e3532e7 4753 Error_Msg_N ("??constant overlays a variable", Expr);
9dfe12ae 4754
d6f39728 4755 -- Imported variables can have an address clause, but then
4756 -- the import is pretty meaningless except to suppress
4757 -- initializations, so we do not need such variables to
4758 -- be statically allocated (and in fact it causes trouble
4759 -- if the address clause is a local value).
4760
4761 elsif Is_Imported (U_Ent) then
4762 Set_Is_Statically_Allocated (U_Ent, False);
4763 end if;
4764
4765 -- We mark a possible modification of a variable with an
4766 -- address clause, since it is likely aliasing is occurring.
4767
177675a7 4768 Note_Possible_Modification (Nam, Sure => False);
d6f39728 4769
83f8f0a6 4770 -- Here we are checking for explicit overlap of one variable
4771 -- by another, and if we find this then mark the overlapped
4772 -- variable as also being volatile to prevent unwanted
d6da7448 4773 -- optimizations. This is a significant pessimization so
4774 -- avoid it when there is an offset, i.e. when the object
4775 -- is composite; they cannot be optimized easily anyway.
d6f39728 4776
d6da7448 4777 if Present (O_Ent)
4778 and then Is_Object (O_Ent)
4779 and then not Off
ba5efa21 4780
4781 -- The following test is an expedient solution to what
4782 -- is really a problem in CodePeer. Suppressing the
4783 -- Set_Treat_As_Volatile call here prevents later
4784 -- generation (in some cases) of trees that CodePeer
4785 -- should, but currently does not, handle correctly.
4786 -- This test should probably be removed when CodePeer
4787 -- is improved, just because we want the tree CodePeer
4788 -- analyzes to match the tree for which we generate code
4789 -- as closely as is practical. ???
4790
4791 and then not CodePeer_Mode
d6da7448 4792 then
ba5efa21 4793 -- ??? O_Ent might not be in current unit
4794
d6da7448 4795 Set_Treat_As_Volatile (O_Ent);
d6f39728 4796 end if;
4797
9dfe12ae 4798 -- Legality checks on the address clause for initialized
4799 -- objects is deferred until the freeze point, because
2beb22b1 4800 -- a subsequent pragma might indicate that the object
42e09e36 4801 -- is imported and thus not initialized. Also, the address
4802 -- clause might involve entities that have yet to be
4803 -- elaborated.
9dfe12ae 4804
4805 Set_Has_Delayed_Freeze (U_Ent);
4806
51ad5ad2 4807 -- If an initialization call has been generated for this
4808 -- object, it needs to be deferred to after the freeze node
4809 -- we have just now added, otherwise GIGI will see a
4810 -- reference to the variable (as actual to the IP call)
4811 -- before its definition.
4812
4813 declare
df9fba45 4814 Init_Call : constant Node_Id :=
4815 Remove_Init_Call (U_Ent, N);
4bba0a8d 4816
51ad5ad2 4817 begin
4818 if Present (Init_Call) then
28a4283c 4819 Append_Freeze_Action (U_Ent, Init_Call);
df9fba45 4820
28a4283c 4821 -- Reset Initialization_Statements pointer so that
4822 -- if there is a pragma Import further down, it can
4823 -- clear any default initialization.
df9fba45 4824
28a4283c 4825 Set_Initialization_Statements (U_Ent, Init_Call);
51ad5ad2 4826 end if;
4827 end;
4828
44e4341e 4829 -- Entity has delayed freeze, so we will generate an
4830 -- alignment check at the freeze point unless suppressed.
d6f39728 4831
44e4341e 4832 if not Range_Checks_Suppressed (U_Ent)
4833 and then not Alignment_Checks_Suppressed (U_Ent)
4834 then
4835 Set_Check_Address_Alignment (N);
4836 end if;
d6f39728 4837
4838 -- Kill the size check code, since we are not allocating
4839 -- the variable, it is somewhere else.
4840
4841 Kill_Size_Check_Code (U_Ent);
83f8f0a6 4842
d6da7448 4843 -- If the address clause is of the form:
83f8f0a6 4844
d6da7448 4845 -- for Y'Address use X'Address
83f8f0a6 4846
d6da7448 4847 -- or
83f8f0a6 4848
d6da7448 4849 -- Const : constant Address := X'Address;
4850 -- ...
4851 -- for Y'Address use Const;
83f8f0a6 4852
d6da7448 4853 -- then we make an entry in the table for checking the size
4854 -- and alignment of the overlaying variable. We defer this
4855 -- check till after code generation to take full advantage
f4623c89 4856 -- of the annotation done by the back end.
d64221a7 4857
9474aa9c 4858 -- If the entity has a generic type, the check will be
43dd6937 4859 -- performed in the instance if the actual type justifies
4860 -- it, and we do not insert the clause in the table to
4861 -- prevent spurious warnings.
83f8f0a6 4862
f4623c89 4863 -- Note: we used to test Comes_From_Source and only give
4864 -- this warning for source entities, but we have removed
4865 -- this test. It really seems bogus to generate overlays
4866 -- that would trigger this warning in generated code.
4867 -- Furthermore, by removing the test, we handle the
4868 -- aspect case properly.
4869
d6da7448 4870 if Address_Clause_Overlay_Warnings
d6da7448 4871 and then Present (O_Ent)
4872 and then Is_Object (O_Ent)
4873 then
9474aa9c 4874 if not Is_Generic_Type (Etype (U_Ent)) then
4875 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4876 end if;
177675a7 4877
d6da7448 4878 -- If variable overlays a constant view, and we are
4879 -- warning on overlays, then mark the variable as
95009d64 4880 -- overlaying a constant and warn immediately if it
4881 -- is initialized. We will give other warnings later
4882 -- if the variable is assigned.
177675a7 4883
d6da7448 4884 if Is_Constant_Object (O_Ent)
4885 and then Ekind (U_Ent) = E_Variable
4886 then
95009d64 4887 declare
4888 Init : constant Node_Id :=
6a7c16f5 4889 Expression (Declaration_Node (U_Ent));
95009d64 4890 begin
4891 Set_Overlays_Constant (U_Ent);
6a7c16f5 4892
95009d64 4893 if Present (Init)
4894 and then Comes_From_Source (Init)
4895 then
4896 Error_Msg_Sloc := Sloc (N);
4897 Error_Msg_NE
4898 ("??constant& may be modified via address "
4899 & "clause#", Declaration_Node (U_Ent), O_Ent);
4900 end if;
4901 end;
83f8f0a6 4902 end if;
d6da7448 4903 end if;
4904 end;
83f8f0a6 4905
d6f39728 4906 -- Not a valid entity for an address clause
4907
4908 else
4909 Error_Msg_N ("address cannot be given for &", Nam);
4910 end if;
4911 end Address;
4912
4913 ---------------
4914 -- Alignment --
4915 ---------------
4916
4917 -- Alignment attribute definition clause
4918
b47769f0 4919 when Attribute_Alignment => Alignment : declare
208fd589 4920 Align : constant Uint := Get_Alignment_Value (Expr);
4921 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
41331dcf 4922
d6f39728 4923 begin
4924 FOnly := True;
4925
4926 if not Is_Type (U_Ent)
4927 and then Ekind (U_Ent) /= E_Variable
4928 and then Ekind (U_Ent) /= E_Constant
4929 then
4930 Error_Msg_N ("alignment cannot be given for &", Nam);
4931
ae888dbd 4932 elsif Duplicate_Clause then
4933 null;
d6f39728 4934
4935 elsif Align /= No_Uint then
4936 Set_Has_Alignment_Clause (U_Ent);
208fd589 4937
44705307 4938 -- Tagged type case, check for attempt to set alignment to a
4939 -- value greater than Max_Align, and reset if so.
4940
41331dcf 4941 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
208fd589 4942 Error_Msg_N
1e3532e7 4943 ("alignment for & set to Maximum_Aligment??", Nam);
44705307 4944 Set_Alignment (U_Ent, Max_Align);
4945
4946 -- All other cases
4947
208fd589 4948 else
4949 Set_Alignment (U_Ent, Align);
4950 end if;
b47769f0 4951
4952 -- For an array type, U_Ent is the first subtype. In that case,
4953 -- also set the alignment of the anonymous base type so that
4954 -- other subtypes (such as the itypes for aggregates of the
4955 -- type) also receive the expected alignment.
4956
4957 if Is_Array_Type (U_Ent) then
4958 Set_Alignment (Base_Type (U_Ent), Align);
4959 end if;
d6f39728 4960 end if;
b47769f0 4961 end Alignment;
d6f39728 4962
4963 ---------------
4964 -- Bit_Order --
4965 ---------------
4966
4967 -- Bit_Order attribute definition clause
4968
4969 when Attribute_Bit_Order => Bit_Order : declare
4970 begin
4971 if not Is_Record_Type (U_Ent) then
4972 Error_Msg_N
4973 ("Bit_Order can only be defined for record type", Nam);
4974
ae888dbd 4975 elsif Duplicate_Clause then
4976 null;
4977
d6f39728 4978 else
4979 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4980
4981 if Etype (Expr) = Any_Type then
4982 return;
4983
cda40848 4984 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 4985 Flag_Non_Static_Expr
4986 ("Bit_Order requires static expression!", Expr);
d6f39728 4987
4988 else
4989 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
fae4ea1f 4990 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
d6f39728 4991 end if;
4992 end if;
4993 end if;
4994 end Bit_Order;
4995
4996 --------------------
4997 -- Component_Size --
4998 --------------------
4999
5000 -- Component_Size attribute definition clause
5001
5002 when Attribute_Component_Size => Component_Size_Case : declare
5003 Csize : constant Uint := Static_Integer (Expr);
a0fc8c5b 5004 Ctyp : Entity_Id;
d6f39728 5005 Btype : Entity_Id;
5006 Biased : Boolean;
5007 New_Ctyp : Entity_Id;
5008 Decl : Node_Id;
5009
5010 begin
5011 if not Is_Array_Type (U_Ent) then
5012 Error_Msg_N ("component size requires array type", Nam);
5013 return;
5014 end if;
5015
5016 Btype := Base_Type (U_Ent);
a0fc8c5b 5017 Ctyp := Component_Type (Btype);
d6f39728 5018
ae888dbd 5019 if Duplicate_Clause then
5020 null;
d6f39728 5021
f3e4db96 5022 elsif Rep_Item_Too_Early (Btype, N) then
5023 null;
5024
d6f39728 5025 elsif Csize /= No_Uint then
a0fc8c5b 5026 Check_Size (Expr, Ctyp, Csize, Biased);
d6f39728 5027
d74fc39a 5028 -- For the biased case, build a declaration for a subtype that
5029 -- will be used to represent the biased subtype that reflects
5030 -- the biased representation of components. We need the subtype
5031 -- to get proper conversions on referencing elements of the
36ac5fbb 5032 -- array.
3062c401 5033
36ac5fbb 5034 if Biased then
5035 New_Ctyp :=
5036 Make_Defining_Identifier (Loc,
5037 Chars =>
5038 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3062c401 5039
36ac5fbb 5040 Decl :=
5041 Make_Subtype_Declaration (Loc,
5042 Defining_Identifier => New_Ctyp,
5043 Subtype_Indication =>
5044 New_Occurrence_Of (Component_Type (Btype), Loc));
5045
5046 Set_Parent (Decl, N);
5047 Analyze (Decl, Suppress => All_Checks);
5048
5049 Set_Has_Delayed_Freeze (New_Ctyp, False);
5050 Set_Esize (New_Ctyp, Csize);
5051 Set_RM_Size (New_Ctyp, Csize);
5052 Init_Alignment (New_Ctyp);
5053 Set_Is_Itype (New_Ctyp, True);
5054 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5055
5056 Set_Component_Type (Btype, New_Ctyp);
5057 Set_Biased (New_Ctyp, N, "component size clause");
d6f39728 5058 end if;
5059
36ac5fbb 5060 Set_Component_Size (Btype, Csize);
5061
a0fc8c5b 5062 -- Deal with warning on overridden size
5063
5064 if Warn_On_Overridden_Size
5065 and then Has_Size_Clause (Ctyp)
5066 and then RM_Size (Ctyp) /= Csize
5067 then
5068 Error_Msg_NE
1e3532e7 5069 ("component size overrides size clause for&?S?", N, Ctyp);
a0fc8c5b 5070 end if;
5071
d6f39728 5072 Set_Has_Component_Size_Clause (Btype, True);
f3e4db96 5073 Set_Has_Non_Standard_Rep (Btype, True);
d6f39728 5074 end if;
5075 end Component_Size_Case;
5076
81b424ac 5077 -----------------------
5078 -- Constant_Indexing --
5079 -----------------------
5080
5081 when Attribute_Constant_Indexing =>
5082 Check_Indexing_Functions;
5083
89f1e35c 5084 ---------
5085 -- CPU --
5086 ---------
5087
5088 when Attribute_CPU => CPU :
5089 begin
5090 -- CPU attribute definition clause not allowed except from aspect
5091 -- specification.
5092
5093 if From_Aspect_Specification (N) then
5094 if not Is_Task_Type (U_Ent) then
5095 Error_Msg_N ("CPU can only be defined for task", Nam);
5096
5097 elsif Duplicate_Clause then
5098 null;
5099
5100 else
5101 -- The expression must be analyzed in the special manner
5102 -- described in "Handling of Default and Per-Object
5103 -- Expressions" in sem.ads.
5104
5105 -- The visibility to the discriminants must be restored
5106
5107 Push_Scope_And_Install_Discriminants (U_Ent);
5108 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5109 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5110
cda40848 5111 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5112 Check_Restriction (Static_Priorities, Expr);
5113 end if;
5114 end if;
5115
5116 else
5117 Error_Msg_N
5118 ("attribute& cannot be set with definition clause", N);
5119 end if;
5120 end CPU;
5121
89cc7147 5122 ----------------------
5123 -- Default_Iterator --
5124 ----------------------
5125
5126 when Attribute_Default_Iterator => Default_Iterator : declare
5127 Func : Entity_Id;
fbf4d6ef 5128 Typ : Entity_Id;
89cc7147 5129
5130 begin
05f6f999 5131 -- If target type is untagged, further checks are irrelevant
5132
89cc7147 5133 if not Is_Tagged_Type (U_Ent) then
5134 Error_Msg_N
05f6f999 5135 ("aspect Default_Iterator applies to tagged type", Nam);
5136 return;
89cc7147 5137 end if;
5138
5139 Check_Iterator_Functions;
5140
5141 Analyze (Expr);
5142
5143 if not Is_Entity_Name (Expr)
5144 or else Ekind (Entity (Expr)) /= E_Function
5145 then
5146 Error_Msg_N ("aspect Iterator must be a function", Expr);
05f6f999 5147 return;
89cc7147 5148 else
5149 Func := Entity (Expr);
5150 end if;
5151
fbf4d6ef 5152 -- The type of the first parameter must be T, T'class, or a
05f6f999 5153 -- corresponding access type (5.5.1 (8/3). If function is
5154 -- parameterless label type accordingly.
fbf4d6ef 5155
5156 if No (First_Formal (Func)) then
05f6f999 5157 Typ := Any_Type;
fbf4d6ef 5158 else
5159 Typ := Etype (First_Formal (Func));
5160 end if;
5161
5162 if Typ = U_Ent
5163 or else Typ = Class_Wide_Type (U_Ent)
5164 or else (Is_Access_Type (Typ)
5165 and then Designated_Type (Typ) = U_Ent)
5166 or else (Is_Access_Type (Typ)
5167 and then Designated_Type (Typ) =
5168 Class_Wide_Type (U_Ent))
89cc7147 5169 then
fbf4d6ef 5170 null;
5171
5172 else
89cc7147 5173 Error_Msg_NE
5174 ("Default Iterator must be a primitive of&", Func, U_Ent);
5175 end if;
5176 end Default_Iterator;
5177
89f1e35c 5178 ------------------------
5179 -- Dispatching_Domain --
5180 ------------------------
5181
5182 when Attribute_Dispatching_Domain => Dispatching_Domain :
5183 begin
5184 -- Dispatching_Domain attribute definition clause not allowed
5185 -- except from aspect specification.
5186
5187 if From_Aspect_Specification (N) then
5188 if not Is_Task_Type (U_Ent) then
fbf4d6ef 5189 Error_Msg_N
5190 ("Dispatching_Domain can only be defined for task", Nam);
89f1e35c 5191
5192 elsif Duplicate_Clause then
5193 null;
5194
5195 else
5196 -- The expression must be analyzed in the special manner
5197 -- described in "Handling of Default and Per-Object
5198 -- Expressions" in sem.ads.
5199
5200 -- The visibility to the discriminants must be restored
5201
5202 Push_Scope_And_Install_Discriminants (U_Ent);
5203
5204 Preanalyze_Spec_Expression
5205 (Expr, RTE (RE_Dispatching_Domain));
5206
5207 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5208 end if;
5209
5210 else
5211 Error_Msg_N
5212 ("attribute& cannot be set with definition clause", N);
5213 end if;
5214 end Dispatching_Domain;
5215
d6f39728 5216 ------------------
5217 -- External_Tag --
5218 ------------------
5219
5220 when Attribute_External_Tag => External_Tag :
5221 begin
5222 if not Is_Tagged_Type (U_Ent) then
5223 Error_Msg_N ("should be a tagged type", Nam);
5224 end if;
5225
ae888dbd 5226 if Duplicate_Clause then
5227 null;
d6f39728 5228
9af0ddc7 5229 else
ae888dbd 5230 Analyze_And_Resolve (Expr, Standard_String);
fbc67f84 5231
cda40848 5232 if not Is_OK_Static_Expression (Expr) then
ae888dbd 5233 Flag_Non_Static_Expr
5234 ("static string required for tag name!", Nam);
5235 end if;
5236
ae888dbd 5237 if not Is_Library_Level_Entity (U_Ent) then
5238 Error_Msg_NE
1e3532e7 5239 ("??non-unique external tag supplied for &", N, U_Ent);
ae888dbd 5240 Error_Msg_N
1e3532e7 5241 ("\??same external tag applies to all "
5242 & "subprogram calls", N);
ae888dbd 5243 Error_Msg_N
1e3532e7 5244 ("\??corresponding internal tag cannot be obtained", N);
ae888dbd 5245 end if;
fbc67f84 5246 end if;
d6f39728 5247 end External_Tag;
5248
b57530b8 5249 --------------------------
5250 -- Implicit_Dereference --
5251 --------------------------
7947a439 5252
b57530b8 5253 when Attribute_Implicit_Dereference =>
7947a439 5254
2beb22b1 5255 -- Legality checks already performed at the point of the type
5256 -- declaration, aspect is not delayed.
7947a439 5257
89cc7147 5258 null;
b57530b8 5259
d6f39728 5260 -----------
5261 -- Input --
5262 -----------
5263
9f373bb8 5264 when Attribute_Input =>
5265 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5266 Set_Has_Specified_Stream_Input (Ent);
d6f39728 5267
89f1e35c 5268 ------------------------
5269 -- Interrupt_Priority --
5270 ------------------------
5271
5272 when Attribute_Interrupt_Priority => Interrupt_Priority :
5273 begin
5274 -- Interrupt_Priority attribute definition clause not allowed
5275 -- except from aspect specification.
5276
5277 if From_Aspect_Specification (N) then
f02a9a9a 5278 if not Is_Concurrent_Type (U_Ent) then
89f1e35c 5279 Error_Msg_N
f02a9a9a 5280 ("Interrupt_Priority can only be defined for task "
5281 & "and protected object", Nam);
89f1e35c 5282
5283 elsif Duplicate_Clause then
5284 null;
5285
5286 else
5287 -- The expression must be analyzed in the special manner
5288 -- described in "Handling of Default and Per-Object
5289 -- Expressions" in sem.ads.
5290
5291 -- The visibility to the discriminants must be restored
5292
5293 Push_Scope_And_Install_Discriminants (U_Ent);
5294
5295 Preanalyze_Spec_Expression
5296 (Expr, RTE (RE_Interrupt_Priority));
5297
5298 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
d4e1acfa 5299
5300 -- Check the No_Task_At_Interrupt_Priority restriction
5301
5302 if Is_Task_Type (U_Ent) then
5303 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5304 end if;
89f1e35c 5305 end if;
5306
5307 else
5308 Error_Msg_N
5309 ("attribute& cannot be set with definition clause", N);
5310 end if;
5311 end Interrupt_Priority;
5312
b3f8228a 5313 --------------
5314 -- Iterable --
5315 --------------
5316
5317 when Attribute_Iterable =>
5318 Analyze (Expr);
bde03454 5319
b3f8228a 5320 if Nkind (Expr) /= N_Aggregate then
5321 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5322 end if;
5323
5324 declare
5325 Assoc : Node_Id;
5326
5327 begin
5328 Assoc := First (Component_Associations (Expr));
5329 while Present (Assoc) loop
5330 if not Is_Entity_Name (Expression (Assoc)) then
5331 Error_Msg_N ("value must be a function", Assoc);
5332 end if;
bde03454 5333
b3f8228a 5334 Next (Assoc);
5335 end loop;
5336 end;
5337
89cc7147 5338 ----------------------
5339 -- Iterator_Element --
5340 ----------------------
5341
5342 when Attribute_Iterator_Element =>
5343 Analyze (Expr);
5344
5345 if not Is_Entity_Name (Expr)
5346 or else not Is_Type (Entity (Expr))
5347 then
5348 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5349 end if;
5350
d6f39728 5351 -------------------
5352 -- Machine_Radix --
5353 -------------------
5354
5355 -- Machine radix attribute definition clause
5356
5357 when Attribute_Machine_Radix => Machine_Radix : declare
5358 Radix : constant Uint := Static_Integer (Expr);
5359
5360 begin
5361 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5362 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5363
ae888dbd 5364 elsif Duplicate_Clause then
5365 null;
d6f39728 5366
5367 elsif Radix /= No_Uint then
5368 Set_Has_Machine_Radix_Clause (U_Ent);
5369 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5370
5371 if Radix = 2 then
5372 null;
5373 elsif Radix = 10 then
5374 Set_Machine_Radix_10 (U_Ent);
5375 else
5376 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5377 end if;
5378 end if;
5379 end Machine_Radix;
5380
5381 -----------------
5382 -- Object_Size --
5383 -----------------
5384
5385 -- Object_Size attribute definition clause
5386
5387 when Attribute_Object_Size => Object_Size : declare
bfa5a9d9 5388 Size : constant Uint := Static_Integer (Expr);
5389
d6f39728 5390 Biased : Boolean;
bfa5a9d9 5391 pragma Warnings (Off, Biased);
d6f39728 5392
5393 begin
5394 if not Is_Type (U_Ent) then
5395 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5396
ae888dbd 5397 elsif Duplicate_Clause then
5398 null;
d6f39728 5399
5400 else
5401 Check_Size (Expr, U_Ent, Size, Biased);
5402
829cd457 5403 if Is_Scalar_Type (U_Ent) then
5404 if Size /= 8 and then Size /= 16 and then Size /= 32
5405 and then UI_Mod (Size, 64) /= 0
5406 then
5407 Error_Msg_N
5408 ("Object_Size must be 8, 16, 32, or multiple of 64",
5409 Expr);
5410 end if;
5411
5412 elsif Size mod 8 /= 0 then
5413 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
d6f39728 5414 end if;
5415
5416 Set_Esize (U_Ent, Size);
5417 Set_Has_Object_Size_Clause (U_Ent);
1d366b32 5418 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5419 end if;
5420 end Object_Size;
5421
5422 ------------
5423 -- Output --
5424 ------------
5425
9f373bb8 5426 when Attribute_Output =>
5427 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5428 Set_Has_Specified_Stream_Output (Ent);
d6f39728 5429
89f1e35c 5430 --------------
5431 -- Priority --
5432 --------------
5433
5434 when Attribute_Priority => Priority :
5435 begin
5436 -- Priority attribute definition clause not allowed except from
5437 -- aspect specification.
5438
5439 if From_Aspect_Specification (N) then
f02a9a9a 5440 if not (Is_Concurrent_Type (U_Ent)
3a72f9c3 5441 or else Ekind (U_Ent) = E_Procedure)
89f1e35c 5442 then
5443 Error_Msg_N
f02a9a9a 5444 ("Priority can only be defined for task and protected "
5445 & "object", Nam);
89f1e35c 5446
5447 elsif Duplicate_Clause then
5448 null;
5449
5450 else
5451 -- The expression must be analyzed in the special manner
5452 -- described in "Handling of Default and Per-Object
5453 -- Expressions" in sem.ads.
5454
5455 -- The visibility to the discriminants must be restored
5456
5457 Push_Scope_And_Install_Discriminants (U_Ent);
5458 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5459 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5460
cda40848 5461 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5462 Check_Restriction (Static_Priorities, Expr);
5463 end if;
5464 end if;
5465
5466 else
5467 Error_Msg_N
5468 ("attribute& cannot be set with definition clause", N);
5469 end if;
5470 end Priority;
5471
d6f39728 5472 ----------
5473 -- Read --
5474 ----------
5475
9f373bb8 5476 when Attribute_Read =>
5477 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5478 Set_Has_Specified_Stream_Read (Ent);
d6f39728 5479
b7b74740 5480 --------------------------
5481 -- Scalar_Storage_Order --
5482 --------------------------
5483
5484 -- Scalar_Storage_Order attribute definition clause
5485
5486 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5487 begin
b43a5770 5488 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
b7b74740 5489 Error_Msg_N
b43a5770 5490 ("Scalar_Storage_Order can only be defined for "
5491 & "record or array type", Nam);
b7b74740 5492
5493 elsif Duplicate_Clause then
5494 null;
5495
5496 else
5497 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5498
5499 if Etype (Expr) = Any_Type then
5500 return;
5501
cda40848 5502 elsif not Is_OK_Static_Expression (Expr) then
b7b74740 5503 Flag_Non_Static_Expr
5504 ("Scalar_Storage_Order requires static expression!", Expr);
5505
c0912570 5506 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5507
5508 -- Here for the case of a non-default (i.e. non-confirming)
5509 -- Scalar_Storage_Order attribute definition.
5510
5511 if Support_Nondefault_SSO_On_Target then
d0a9ea3b 5512 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
c0912570 5513 else
5514 Error_Msg_N
5515 ("non-default Scalar_Storage_Order "
5516 & "not supported on target", Expr);
b7b74740 5517 end if;
5518 end if;
b64082f2 5519
5520 -- Clear SSO default indications since explicit setting of the
5521 -- order overrides the defaults.
5522
5523 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5524 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
b7b74740 5525 end if;
5526 end Scalar_Storage_Order;
5527
d6f39728 5528 ----------
5529 -- Size --
5530 ----------
5531
5532 -- Size attribute definition clause
5533
5534 when Attribute_Size => Size : declare
5535 Size : constant Uint := Static_Integer (Expr);
5536 Etyp : Entity_Id;
5537 Biased : Boolean;
5538
5539 begin
5540 FOnly := True;
5541
ae888dbd 5542 if Duplicate_Clause then
5543 null;
d6f39728 5544
5545 elsif not Is_Type (U_Ent)
5546 and then Ekind (U_Ent) /= E_Variable
5547 and then Ekind (U_Ent) /= E_Constant
5548 then
5549 Error_Msg_N ("size cannot be given for &", Nam);
5550
5551 elsif Is_Array_Type (U_Ent)
5552 and then not Is_Constrained (U_Ent)
5553 then
5554 Error_Msg_N
5555 ("size cannot be given for unconstrained array", Nam);
5556
c2b89d6e 5557 elsif Size /= No_Uint then
d6f39728 5558 if Is_Type (U_Ent) then
5559 Etyp := U_Ent;
5560 else
5561 Etyp := Etype (U_Ent);
5562 end if;
5563
59ac57b5 5564 -- Check size, note that Gigi is in charge of checking that the
5565 -- size of an array or record type is OK. Also we do not check
5566 -- the size in the ordinary fixed-point case, since it is too
5567 -- early to do so (there may be subsequent small clause that
5568 -- affects the size). We can check the size if a small clause
5569 -- has already been given.
d6f39728 5570
5571 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5572 or else Has_Small_Clause (U_Ent)
5573 then
5574 Check_Size (Expr, Etyp, Size, Biased);
b77e4501 5575 Set_Biased (U_Ent, N, "size clause", Biased);
d6f39728 5576 end if;
5577
5578 -- For types set RM_Size and Esize if possible
5579
5580 if Is_Type (U_Ent) then
5581 Set_RM_Size (U_Ent, Size);
5582
ada34def 5583 -- For elementary types, increase Object_Size to power of 2,
5584 -- but not less than a storage unit in any case (normally
59ac57b5 5585 -- this means it will be byte addressable).
d6f39728 5586
ada34def 5587 -- For all other types, nothing else to do, we leave Esize
5588 -- (object size) unset, the back end will set it from the
5589 -- size and alignment in an appropriate manner.
5590
1d366b32 5591 -- In both cases, we check whether the alignment must be
5592 -- reset in the wake of the size change.
5593
ada34def 5594 if Is_Elementary_Type (U_Ent) then
f15731c4 5595 if Size <= System_Storage_Unit then
5596 Init_Esize (U_Ent, System_Storage_Unit);
d6f39728 5597 elsif Size <= 16 then
5598 Init_Esize (U_Ent, 16);
5599 elsif Size <= 32 then
5600 Init_Esize (U_Ent, 32);
5601 else
5602 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5603 end if;
5604
1d366b32 5605 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5606 else
5607 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5608 end if;
5609
d6f39728 5610 -- For objects, set Esize only
5611
5612 else
9dfe12ae 5613 if Is_Elementary_Type (Etyp) then
5614 if Size /= System_Storage_Unit
5615 and then
5616 Size /= System_Storage_Unit * 2
5617 and then
5618 Size /= System_Storage_Unit * 4
5619 and then
5620 Size /= System_Storage_Unit * 8
5621 then
5c99c290 5622 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
87d5c1d0 5623 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
9dfe12ae 5624 Error_Msg_N
5c99c290 5625 ("size for primitive object must be a power of 2"
87d5c1d0 5626 & " in the range ^-^", N);
9dfe12ae 5627 end if;
5628 end if;
5629
d6f39728 5630 Set_Esize (U_Ent, Size);
5631 end if;
5632
5633 Set_Has_Size_Clause (U_Ent);
5634 end if;
5635 end Size;
5636
5637 -----------
5638 -- Small --
5639 -----------
5640
5641 -- Small attribute definition clause
5642
5643 when Attribute_Small => Small : declare
5644 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5645 Small : Ureal;
5646
5647 begin
5648 Analyze_And_Resolve (Expr, Any_Real);
5649
5650 if Etype (Expr) = Any_Type then
5651 return;
5652
cda40848 5653 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5654 Flag_Non_Static_Expr
5655 ("small requires static expression!", Expr);
d6f39728 5656 return;
5657
5658 else
5659 Small := Expr_Value_R (Expr);
5660
5661 if Small <= Ureal_0 then
5662 Error_Msg_N ("small value must be greater than zero", Expr);
5663 return;
5664 end if;
5665
5666 end if;
5667
5668 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5669 Error_Msg_N
5670 ("small requires an ordinary fixed point type", Nam);
5671
5672 elsif Has_Small_Clause (U_Ent) then
5673 Error_Msg_N ("small already given for &", Nam);
5674
5675 elsif Small > Delta_Value (U_Ent) then
5676 Error_Msg_N
ce3e25d6 5677 ("small value must not be greater than delta value", Nam);
d6f39728 5678
5679 else
5680 Set_Small_Value (U_Ent, Small);
5681 Set_Small_Value (Implicit_Base, Small);
5682 Set_Has_Small_Clause (U_Ent);
5683 Set_Has_Small_Clause (Implicit_Base);
5684 Set_Has_Non_Standard_Rep (Implicit_Base);
5685 end if;
5686 end Small;
5687
d6f39728 5688 ------------------
5689 -- Storage_Pool --
5690 ------------------
5691
5692 -- Storage_Pool attribute definition clause
5693
b55f7641 5694 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
d6f39728 5695 Pool : Entity_Id;
6b567c71 5696 T : Entity_Id;
d6f39728 5697
5698 begin
44e4341e 5699 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5700 Error_Msg_N
5701 ("storage pool cannot be given for access-to-subprogram type",
5702 Nam);
5703 return;
5704
d3ef794c 5705 elsif not
5706 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
d6f39728 5707 then
44e4341e 5708 Error_Msg_N
5709 ("storage pool can only be given for access types", Nam);
d6f39728 5710 return;
5711
5712 elsif Is_Derived_Type (U_Ent) then
5713 Error_Msg_N
5714 ("storage pool cannot be given for a derived access type",
5715 Nam);
5716
ae888dbd 5717 elsif Duplicate_Clause then
d6f39728 5718 return;
5719
5720 elsif Present (Associated_Storage_Pool (U_Ent)) then
5721 Error_Msg_N ("storage pool already given for &", Nam);
5722 return;
5723 end if;
5724
6653b695 5725 -- Check for Storage_Size previously given
5726
5727 declare
5728 SS : constant Node_Id :=
5729 Get_Attribute_Definition_Clause
5730 (U_Ent, Attribute_Storage_Size);
5731 begin
5732 if Present (SS) then
5733 Check_Pool_Size_Clash (U_Ent, N, SS);
5734 end if;
5735 end;
5736
5737 -- Storage_Pool case
5738
b55f7641 5739 if Id = Attribute_Storage_Pool then
5740 Analyze_And_Resolve
5741 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5742
5743 -- In the Simple_Storage_Pool case, we allow a variable of any
b15003c3 5744 -- simple storage pool type, so we Resolve without imposing an
b55f7641 5745 -- expected type.
5746
5747 else
5748 Analyze_And_Resolve (Expr);
5749
5750 if not Present (Get_Rep_Pragma
b15003c3 5751 (Etype (Expr), Name_Simple_Storage_Pool_Type))
b55f7641 5752 then
5753 Error_Msg_N
5754 ("expression must be of a simple storage pool type", Expr);
5755 end if;
5756 end if;
d6f39728 5757
8c5c7277 5758 if not Denotes_Variable (Expr) then
5759 Error_Msg_N ("storage pool must be a variable", Expr);
5760 return;
5761 end if;
5762
6b567c71 5763 if Nkind (Expr) = N_Type_Conversion then
5764 T := Etype (Expression (Expr));
5765 else
5766 T := Etype (Expr);
5767 end if;
5768
5769 -- The Stack_Bounded_Pool is used internally for implementing
d64221a7 5770 -- access types with a Storage_Size. Since it only work properly
5771 -- when used on one specific type, we need to check that it is not
5772 -- hijacked improperly:
5773
6b567c71 5774 -- type T is access Integer;
5775 -- for T'Storage_Size use n;
5776 -- type Q is access Float;
5777 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5778
15ebb600 5779 if RTE_Available (RE_Stack_Bounded_Pool)
5780 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5781 then
5782 Error_Msg_N ("non-shareable internal Pool", Expr);
6b567c71 5783 return;
5784 end if;
5785
d6f39728 5786 -- If the argument is a name that is not an entity name, then
5787 -- we construct a renaming operation to define an entity of
5788 -- type storage pool.
5789
5790 if not Is_Entity_Name (Expr)
5791 and then Is_Object_Reference (Expr)
5792 then
11deeeb6 5793 Pool := Make_Temporary (Loc, 'P', Expr);
d6f39728 5794
5795 declare
5796 Rnode : constant Node_Id :=
5797 Make_Object_Renaming_Declaration (Loc,
5798 Defining_Identifier => Pool,
5799 Subtype_Mark =>
5800 New_Occurrence_Of (Etype (Expr), Loc),
11deeeb6 5801 Name => Expr);
d6f39728 5802
5803 begin
f65f7fdf 5804 -- If the attribute definition clause comes from an aspect
5805 -- clause, then insert the renaming before the associated
5806 -- entity's declaration, since the attribute clause has
5807 -- not yet been appended to the declaration list.
5808
5809 if From_Aspect_Specification (N) then
5810 Insert_Before (Parent (Entity (N)), Rnode);
5811 else
5812 Insert_Before (N, Rnode);
5813 end if;
5814
d6f39728 5815 Analyze (Rnode);
5816 Set_Associated_Storage_Pool (U_Ent, Pool);
5817 end;
5818
5819 elsif Is_Entity_Name (Expr) then
5820 Pool := Entity (Expr);
5821
5822 -- If pool is a renamed object, get original one. This can
5823 -- happen with an explicit renaming, and within instances.
5824
5825 while Present (Renamed_Object (Pool))
5826 and then Is_Entity_Name (Renamed_Object (Pool))
5827 loop
5828 Pool := Entity (Renamed_Object (Pool));
5829 end loop;
5830
5831 if Present (Renamed_Object (Pool))
5832 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5833 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5834 then
5835 Pool := Entity (Expression (Renamed_Object (Pool)));
5836 end if;
5837
6b567c71 5838 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 5839
5840 elsif Nkind (Expr) = N_Type_Conversion
5841 and then Is_Entity_Name (Expression (Expr))
5842 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5843 then
5844 Pool := Entity (Expression (Expr));
6b567c71 5845 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 5846
5847 else
5848 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5849 return;
5850 end if;
b55f7641 5851 end;
d6f39728 5852
44e4341e 5853 ------------------
5854 -- Storage_Size --
5855 ------------------
5856
5857 -- Storage_Size attribute definition clause
5858
5859 when Attribute_Storage_Size => Storage_Size : declare
5860 Btype : constant Entity_Id := Base_Type (U_Ent);
44e4341e 5861
5862 begin
5863 if Is_Task_Type (U_Ent) then
44e4341e 5864
39a0c1d3 5865 -- Check obsolescent (but never obsolescent if from aspect)
ceec4f7c 5866
5867 if not From_Aspect_Specification (N) then
5868 Check_Restriction (No_Obsolescent_Features, N);
5869
5870 if Warn_On_Obsolescent_Feature then
5871 Error_Msg_N
5872 ("?j?storage size clause for task is an " &
5873 "obsolescent feature (RM J.9)", N);
5874 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5875 end if;
44e4341e 5876 end if;
5877
5878 FOnly := True;
5879 end if;
5880
5881 if not Is_Access_Type (U_Ent)
5882 and then Ekind (U_Ent) /= E_Task_Type
5883 then
5884 Error_Msg_N ("storage size cannot be given for &", Nam);
5885
5886 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5887 Error_Msg_N
5888 ("storage size cannot be given for a derived access type",
5889 Nam);
5890
ae888dbd 5891 elsif Duplicate_Clause then
5892 null;
44e4341e 5893
5894 else
5895 Analyze_And_Resolve (Expr, Any_Integer);
5896
5897 if Is_Access_Type (U_Ent) then
6653b695 5898
5899 -- Check for Storage_Pool previously given
5900
5901 declare
5902 SP : constant Node_Id :=
5903 Get_Attribute_Definition_Clause
5904 (U_Ent, Attribute_Storage_Pool);
5905
5906 begin
5907 if Present (SP) then
5908 Check_Pool_Size_Clash (U_Ent, SP, N);
5909 end if;
5910 end;
5911
5912 -- Special case of for x'Storage_Size use 0
44e4341e 5913
5941a4e9 5914 if Is_OK_Static_Expression (Expr)
44e4341e 5915 and then Expr_Value (Expr) = 0
5916 then
5917 Set_No_Pool_Assigned (Btype);
5918 end if;
44e4341e 5919 end if;
5920
5921 Set_Has_Storage_Size_Clause (Btype);
5922 end if;
5923 end Storage_Size;
5924
7189d17f 5925 -----------------
5926 -- Stream_Size --
5927 -----------------
5928
5929 when Attribute_Stream_Size => Stream_Size : declare
5930 Size : constant Uint := Static_Integer (Expr);
5931
5932 begin
15ebb600 5933 if Ada_Version <= Ada_95 then
5934 Check_Restriction (No_Implementation_Attributes, N);
5935 end if;
5936
ae888dbd 5937 if Duplicate_Clause then
5938 null;
7189d17f 5939
5940 elsif Is_Elementary_Type (U_Ent) then
5941 if Size /= System_Storage_Unit
5942 and then
5943 Size /= System_Storage_Unit * 2
5944 and then
5945 Size /= System_Storage_Unit * 4
5946 and then
5947 Size /= System_Storage_Unit * 8
5948 then
5949 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5950 Error_Msg_N
5951 ("stream size for elementary type must be a"
5952 & " power of 2 and at least ^", N);
5953
5954 elsif RM_Size (U_Ent) > Size then
5955 Error_Msg_Uint_1 := RM_Size (U_Ent);
5956 Error_Msg_N
5957 ("stream size for elementary type must be a"
5958 & " power of 2 and at least ^", N);
5959 end if;
5960
5961 Set_Has_Stream_Size_Clause (U_Ent);
5962
5963 else
5964 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5965 end if;
5966 end Stream_Size;
5967
d6f39728 5968 ----------------
5969 -- Value_Size --
5970 ----------------
5971
5972 -- Value_Size attribute definition clause
5973
5974 when Attribute_Value_Size => Value_Size : declare
5975 Size : constant Uint := Static_Integer (Expr);
5976 Biased : Boolean;
5977
5978 begin
5979 if not Is_Type (U_Ent) then
5980 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5981
ae888dbd 5982 elsif Duplicate_Clause then
5983 null;
d6f39728 5984
59ac57b5 5985 elsif Is_Array_Type (U_Ent)
5986 and then not Is_Constrained (U_Ent)
5987 then
5988 Error_Msg_N
5989 ("Value_Size cannot be given for unconstrained array", Nam);
5990
d6f39728 5991 else
5992 if Is_Elementary_Type (U_Ent) then
5993 Check_Size (Expr, U_Ent, Size, Biased);
b77e4501 5994 Set_Biased (U_Ent, N, "value size clause", Biased);
d6f39728 5995 end if;
5996
5997 Set_RM_Size (U_Ent, Size);
5998 end if;
5999 end Value_Size;
6000
81b424ac 6001 -----------------------
6002 -- Variable_Indexing --
6003 -----------------------
6004
6005 when Attribute_Variable_Indexing =>
6006 Check_Indexing_Functions;
6007
d6f39728 6008 -----------
6009 -- Write --
6010 -----------
6011
9f373bb8 6012 when Attribute_Write =>
6013 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6014 Set_Has_Specified_Stream_Write (Ent);
d6f39728 6015
6016 -- All other attributes cannot be set
6017
6018 when others =>
6019 Error_Msg_N
6020 ("attribute& cannot be set with definition clause", N);
d6f39728 6021 end case;
6022
d64221a7 6023 -- The test for the type being frozen must be performed after any
6024 -- expression the clause has been analyzed since the expression itself
6025 -- might cause freezing that makes the clause illegal.
d6f39728 6026
6027 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6028 return;
6029 end if;
6030 end Analyze_Attribute_Definition_Clause;
6031
6032 ----------------------------
6033 -- Analyze_Code_Statement --
6034 ----------------------------
6035
6036 procedure Analyze_Code_Statement (N : Node_Id) is
6037 HSS : constant Node_Id := Parent (N);
6038 SBody : constant Node_Id := Parent (HSS);
6039 Subp : constant Entity_Id := Current_Scope;
6040 Stmt : Node_Id;
6041 Decl : Node_Id;
6042 StmtO : Node_Id;
6043 DeclO : Node_Id;
6044
6045 begin
6046 -- Analyze and check we get right type, note that this implements the
6047 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
6048 -- is the only way that Asm_Insn could possibly be visible.
6049
6050 Analyze_And_Resolve (Expression (N));
6051
6052 if Etype (Expression (N)) = Any_Type then
6053 return;
6054 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6055 Error_Msg_N ("incorrect type for code statement", N);
6056 return;
6057 end if;
6058
44e4341e 6059 Check_Code_Statement (N);
6060
d6f39728 6061 -- Make sure we appear in the handled statement sequence of a
6062 -- subprogram (RM 13.8(3)).
6063
6064 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6065 or else Nkind (SBody) /= N_Subprogram_Body
6066 then
6067 Error_Msg_N
6068 ("code statement can only appear in body of subprogram", N);
6069 return;
6070 end if;
6071
6072 -- Do remaining checks (RM 13.8(3)) if not already done
6073
6074 if not Is_Machine_Code_Subprogram (Subp) then
6075 Set_Is_Machine_Code_Subprogram (Subp);
6076
6077 -- No exception handlers allowed
6078
6079 if Present (Exception_Handlers (HSS)) then
6080 Error_Msg_N
6081 ("exception handlers not permitted in machine code subprogram",
6082 First (Exception_Handlers (HSS)));
6083 end if;
6084
6085 -- No declarations other than use clauses and pragmas (we allow
6086 -- certain internally generated declarations as well).
6087
6088 Decl := First (Declarations (SBody));
6089 while Present (Decl) loop
6090 DeclO := Original_Node (Decl);
6091 if Comes_From_Source (DeclO)
fdd294d1 6092 and not Nkind_In (DeclO, N_Pragma,
6093 N_Use_Package_Clause,
6094 N_Use_Type_Clause,
6095 N_Implicit_Label_Declaration)
d6f39728 6096 then
6097 Error_Msg_N
6098 ("this declaration not allowed in machine code subprogram",
6099 DeclO);
6100 end if;
6101
6102 Next (Decl);
6103 end loop;
6104
6105 -- No statements other than code statements, pragmas, and labels.
6106 -- Again we allow certain internally generated statements.
3ab42ff7 6107
c3107527 6108 -- In Ada 2012, qualified expressions are names, and the code
6109 -- statement is initially parsed as a procedure call.
d6f39728 6110
6111 Stmt := First (Statements (HSS));
6112 while Present (Stmt) loop
6113 StmtO := Original_Node (Stmt);
c3107527 6114
59f2fcab 6115 -- A procedure call transformed into a code statement is OK.
6116
c3107527 6117 if Ada_Version >= Ada_2012
6118 and then Nkind (StmtO) = N_Procedure_Call_Statement
59f2fcab 6119 and then Nkind (Name (StmtO)) = N_Qualified_Expression
c3107527 6120 then
6121 null;
6122
6123 elsif Comes_From_Source (StmtO)
fdd294d1 6124 and then not Nkind_In (StmtO, N_Pragma,
6125 N_Label,
6126 N_Code_Statement)
d6f39728 6127 then
6128 Error_Msg_N
6129 ("this statement is not allowed in machine code subprogram",
6130 StmtO);
6131 end if;
6132
6133 Next (Stmt);
6134 end loop;
6135 end if;
d6f39728 6136 end Analyze_Code_Statement;
6137
6138 -----------------------------------------------
6139 -- Analyze_Enumeration_Representation_Clause --
6140 -----------------------------------------------
6141
6142 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6143 Ident : constant Node_Id := Identifier (N);
6144 Aggr : constant Node_Id := Array_Aggregate (N);
6145 Enumtype : Entity_Id;
6146 Elit : Entity_Id;
6147 Expr : Node_Id;
6148 Assoc : Node_Id;
6149 Choice : Node_Id;
6150 Val : Uint;
b3190af0 6151
6152 Err : Boolean := False;
098d3082 6153 -- Set True to avoid cascade errors and crashes on incorrect source code
d6f39728 6154
e30c7d84 6155 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6156 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6157 -- Allowed range of universal integer (= allowed range of enum lit vals)
6158
d6f39728 6159 Min : Uint;
6160 Max : Uint;
e30c7d84 6161 -- Minimum and maximum values of entries
6162
6163 Max_Node : Node_Id;
6164 -- Pointer to node for literal providing max value
d6f39728 6165
6166 begin
ca301e17 6167 if Ignore_Rep_Clauses then
2ff55065 6168 Kill_Rep_Clause (N);
fbc67f84 6169 return;
6170 end if;
6171
175a6969 6172 -- Ignore enumeration rep clauses by default in CodePeer mode,
6173 -- unless -gnatd.I is specified, as a work around for potential false
6174 -- positive messages.
6175
6176 if CodePeer_Mode and not Debug_Flag_Dot_II then
6177 return;
6178 end if;
6179
d6f39728 6180 -- First some basic error checks
6181
6182 Find_Type (Ident);
6183 Enumtype := Entity (Ident);
6184
6185 if Enumtype = Any_Type
6186 or else Rep_Item_Too_Early (Enumtype, N)
6187 then
6188 return;
6189 else
6190 Enumtype := Underlying_Type (Enumtype);
6191 end if;
6192
6193 if not Is_Enumeration_Type (Enumtype) then
6194 Error_Msg_NE
6195 ("enumeration type required, found}",
6196 Ident, First_Subtype (Enumtype));
6197 return;
6198 end if;
6199
9dfe12ae 6200 -- Ignore rep clause on generic actual type. This will already have
6201 -- been flagged on the template as an error, and this is the safest
6202 -- way to ensure we don't get a junk cascaded message in the instance.
6203
6204 if Is_Generic_Actual_Type (Enumtype) then
6205 return;
6206
6207 -- Type must be in current scope
6208
6209 elsif Scope (Enumtype) /= Current_Scope then
d6f39728 6210 Error_Msg_N ("type must be declared in this scope", Ident);
6211 return;
6212
9dfe12ae 6213 -- Type must be a first subtype
6214
d6f39728 6215 elsif not Is_First_Subtype (Enumtype) then
6216 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6217 return;
6218
9dfe12ae 6219 -- Ignore duplicate rep clause
6220
d6f39728 6221 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6222 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6223 return;
6224
7189d17f 6225 -- Don't allow rep clause for standard [wide_[wide_]]character
9dfe12ae 6226
177675a7 6227 elsif Is_Standard_Character_Type (Enumtype) then
d6f39728 6228 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
9dfe12ae 6229 return;
6230
d9125581 6231 -- Check that the expression is a proper aggregate (no parentheses)
6232
6233 elsif Paren_Count (Aggr) /= 0 then
6234 Error_Msg
6235 ("extra parentheses surrounding aggregate not allowed",
6236 First_Sloc (Aggr));
6237 return;
6238
9dfe12ae 6239 -- All tests passed, so set rep clause in place
d6f39728 6240
6241 else
6242 Set_Has_Enumeration_Rep_Clause (Enumtype);
6243 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6244 end if;
6245
6246 -- Now we process the aggregate. Note that we don't use the normal
6247 -- aggregate code for this purpose, because we don't want any of the
6248 -- normal expansion activities, and a number of special semantic
6249 -- rules apply (including the component type being any integer type)
6250
d6f39728 6251 Elit := First_Literal (Enumtype);
6252
6253 -- First the positional entries if any
6254
6255 if Present (Expressions (Aggr)) then
6256 Expr := First (Expressions (Aggr));
6257 while Present (Expr) loop
6258 if No (Elit) then
6259 Error_Msg_N ("too many entries in aggregate", Expr);
6260 return;
6261 end if;
6262
6263 Val := Static_Integer (Expr);
6264
d9125581 6265 -- Err signals that we found some incorrect entries processing
6266 -- the list. The final checks for completeness and ordering are
6267 -- skipped in this case.
6268
d6f39728 6269 if Val = No_Uint then
6270 Err := True;
f02a9a9a 6271
d6f39728 6272 elsif Val < Lo or else Hi < Val then
6273 Error_Msg_N ("value outside permitted range", Expr);
6274 Err := True;
6275 end if;
6276
6277 Set_Enumeration_Rep (Elit, Val);
6278 Set_Enumeration_Rep_Expr (Elit, Expr);
6279 Next (Expr);
6280 Next (Elit);
6281 end loop;
6282 end if;
6283
6284 -- Now process the named entries if present
6285
6286 if Present (Component_Associations (Aggr)) then
6287 Assoc := First (Component_Associations (Aggr));
6288 while Present (Assoc) loop
6289 Choice := First (Choices (Assoc));
6290
6291 if Present (Next (Choice)) then
6292 Error_Msg_N
6293 ("multiple choice not allowed here", Next (Choice));
6294 Err := True;
6295 end if;
6296
6297 if Nkind (Choice) = N_Others_Choice then
6298 Error_Msg_N ("others choice not allowed here", Choice);
6299 Err := True;
6300
6301 elsif Nkind (Choice) = N_Range then
b3190af0 6302
d6f39728 6303 -- ??? should allow zero/one element range here
b3190af0 6304
d6f39728 6305 Error_Msg_N ("range not allowed here", Choice);
6306 Err := True;
6307
6308 else
6309 Analyze_And_Resolve (Choice, Enumtype);
b3190af0 6310
098d3082 6311 if Error_Posted (Choice) then
d6f39728 6312 Err := True;
098d3082 6313 end if;
d6f39728 6314
098d3082 6315 if not Err then
6316 if Is_Entity_Name (Choice)
6317 and then Is_Type (Entity (Choice))
6318 then
6319 Error_Msg_N ("subtype name not allowed here", Choice);
d6f39728 6320 Err := True;
b3190af0 6321
098d3082 6322 -- ??? should allow static subtype with zero/one entry
d6f39728 6323
098d3082 6324 elsif Etype (Choice) = Base_Type (Enumtype) then
cda40848 6325 if not Is_OK_Static_Expression (Choice) then
098d3082 6326 Flag_Non_Static_Expr
6327 ("non-static expression used for choice!", Choice);
d6f39728 6328 Err := True;
d6f39728 6329
098d3082 6330 else
6331 Elit := Expr_Value_E (Choice);
6332
6333 if Present (Enumeration_Rep_Expr (Elit)) then
6334 Error_Msg_Sloc :=
6335 Sloc (Enumeration_Rep_Expr (Elit));
6336 Error_Msg_NE
6337 ("representation for& previously given#",
6338 Choice, Elit);
6339 Err := True;
6340 end if;
d6f39728 6341
098d3082 6342 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
d6f39728 6343
098d3082 6344 Expr := Expression (Assoc);
6345 Val := Static_Integer (Expr);
d6f39728 6346
098d3082 6347 if Val = No_Uint then
6348 Err := True;
6349
6350 elsif Val < Lo or else Hi < Val then
6351 Error_Msg_N ("value outside permitted range", Expr);
6352 Err := True;
6353 end if;
d6f39728 6354
098d3082 6355 Set_Enumeration_Rep (Elit, Val);
6356 end if;
d6f39728 6357 end if;
6358 end if;
6359 end if;
6360
6361 Next (Assoc);
6362 end loop;
6363 end if;
6364
6365 -- Aggregate is fully processed. Now we check that a full set of
6366 -- representations was given, and that they are in range and in order.
6367 -- These checks are only done if no other errors occurred.
6368
6369 if not Err then
6370 Min := No_Uint;
6371 Max := No_Uint;
6372
6373 Elit := First_Literal (Enumtype);
6374 while Present (Elit) loop
6375 if No (Enumeration_Rep_Expr (Elit)) then
6376 Error_Msg_NE ("missing representation for&!", N, Elit);
6377
6378 else
6379 Val := Enumeration_Rep (Elit);
6380
6381 if Min = No_Uint then
6382 Min := Val;
6383 end if;
6384
6385 if Val /= No_Uint then
6386 if Max /= No_Uint and then Val <= Max then
6387 Error_Msg_NE
6388 ("enumeration value for& not ordered!",
e30c7d84 6389 Enumeration_Rep_Expr (Elit), Elit);
d6f39728 6390 end if;
6391
e30c7d84 6392 Max_Node := Enumeration_Rep_Expr (Elit);
d6f39728 6393 Max := Val;
6394 end if;
6395
e30c7d84 6396 -- If there is at least one literal whose representation is not
6397 -- equal to the Pos value, then note that this enumeration type
6398 -- has a non-standard representation.
d6f39728 6399
6400 if Val /= Enumeration_Pos (Elit) then
6401 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6402 end if;
6403 end if;
6404
6405 Next (Elit);
6406 end loop;
6407
6408 -- Now set proper size information
6409
6410 declare
6411 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6412
6413 begin
6414 if Has_Size_Clause (Enumtype) then
e30c7d84 6415
6416 -- All OK, if size is OK now
6417
6418 if RM_Size (Enumtype) >= Minsize then
d6f39728 6419 null;
6420
6421 else
e30c7d84 6422 -- Try if we can get by with biasing
6423
d6f39728 6424 Minsize :=
6425 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6426
e30c7d84 6427 -- Error message if even biasing does not work
6428
6429 if RM_Size (Enumtype) < Minsize then
6430 Error_Msg_Uint_1 := RM_Size (Enumtype);
6431 Error_Msg_Uint_2 := Max;
6432 Error_Msg_N
6433 ("previously given size (^) is too small "
6434 & "for this value (^)", Max_Node);
6435
6436 -- If biasing worked, indicate that we now have biased rep
d6f39728 6437
6438 else
b77e4501 6439 Set_Biased
6440 (Enumtype, Size_Clause (Enumtype), "size clause");
d6f39728 6441 end if;
6442 end if;
6443
6444 else
6445 Set_RM_Size (Enumtype, Minsize);
6446 Set_Enum_Esize (Enumtype);
6447 end if;
6448
6449 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6450 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6451 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6452 end;
6453 end if;
6454
39a0c1d3 6455 -- We repeat the too late test in case it froze itself
d6f39728 6456
6457 if Rep_Item_Too_Late (Enumtype, N) then
6458 null;
6459 end if;
d6f39728 6460 end Analyze_Enumeration_Representation_Clause;
6461
6462 ----------------------------
6463 -- Analyze_Free_Statement --
6464 ----------------------------
6465
6466 procedure Analyze_Free_Statement (N : Node_Id) is
6467 begin
6468 Analyze (Expression (N));
6469 end Analyze_Free_Statement;
6470
40ca69b9 6471 ---------------------------
6472 -- Analyze_Freeze_Entity --
6473 ---------------------------
6474
6475 procedure Analyze_Freeze_Entity (N : Node_Id) is
40ca69b9 6476 begin
d9f6a4ee 6477 Freeze_Entity_Checks (N);
6478 end Analyze_Freeze_Entity;
98f7db28 6479
d9f6a4ee 6480 -----------------------------------
6481 -- Analyze_Freeze_Generic_Entity --
6482 -----------------------------------
98f7db28 6483
d9f6a4ee 6484 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6485 begin
6486 Freeze_Entity_Checks (N);
6487 end Analyze_Freeze_Generic_Entity;
40ca69b9 6488
d9f6a4ee 6489 ------------------------------------------
6490 -- Analyze_Record_Representation_Clause --
6491 ------------------------------------------
c8da6114 6492
d9f6a4ee 6493 -- Note: we check as much as we can here, but we can't do any checks
6494 -- based on the position values (e.g. overlap checks) until freeze time
6495 -- because especially in Ada 2005 (machine scalar mode), the processing
6496 -- for non-standard bit order can substantially change the positions.
6497 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6498 -- for the remainder of this processing.
d00681a7 6499
d9f6a4ee 6500 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6501 Ident : constant Node_Id := Identifier (N);
6502 Biased : Boolean;
6503 CC : Node_Id;
6504 Comp : Entity_Id;
6505 Fbit : Uint;
6506 Hbit : Uint := Uint_0;
6507 Lbit : Uint;
6508 Ocomp : Entity_Id;
6509 Posit : Uint;
6510 Rectype : Entity_Id;
6511 Recdef : Node_Id;
d00681a7 6512
d9f6a4ee 6513 function Is_Inherited (Comp : Entity_Id) return Boolean;
6514 -- True if Comp is an inherited component in a record extension
d00681a7 6515
d9f6a4ee 6516 ------------------
6517 -- Is_Inherited --
6518 ------------------
d00681a7 6519
d9f6a4ee 6520 function Is_Inherited (Comp : Entity_Id) return Boolean is
6521 Comp_Base : Entity_Id;
d00681a7 6522
d9f6a4ee 6523 begin
6524 if Ekind (Rectype) = E_Record_Subtype then
6525 Comp_Base := Original_Record_Component (Comp);
6526 else
6527 Comp_Base := Comp;
d00681a7 6528 end if;
6529
d9f6a4ee 6530 return Comp_Base /= Original_Record_Component (Comp_Base);
6531 end Is_Inherited;
d00681a7 6532
d9f6a4ee 6533 -- Local variables
d00681a7 6534
d9f6a4ee 6535 Is_Record_Extension : Boolean;
6536 -- True if Rectype is a record extension
d00681a7 6537
d9f6a4ee 6538 CR_Pragma : Node_Id := Empty;
6539 -- Points to N_Pragma node if Complete_Representation pragma present
d00681a7 6540
d9f6a4ee 6541 -- Start of processing for Analyze_Record_Representation_Clause
d00681a7 6542
d9f6a4ee 6543 begin
6544 if Ignore_Rep_Clauses then
2ff55065 6545 Kill_Rep_Clause (N);
d9f6a4ee 6546 return;
d00681a7 6547 end if;
98f7db28 6548
d9f6a4ee 6549 Find_Type (Ident);
6550 Rectype := Entity (Ident);
85377c9b 6551
d9f6a4ee 6552 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6553 return;
6554 else
6555 Rectype := Underlying_Type (Rectype);
6556 end if;
85377c9b 6557
d9f6a4ee 6558 -- First some basic error checks
85377c9b 6559
d9f6a4ee 6560 if not Is_Record_Type (Rectype) then
6561 Error_Msg_NE
6562 ("record type required, found}", Ident, First_Subtype (Rectype));
6563 return;
85377c9b 6564
d9f6a4ee 6565 elsif Scope (Rectype) /= Current_Scope then
6566 Error_Msg_N ("type must be declared in this scope", N);
6567 return;
85377c9b 6568
d9f6a4ee 6569 elsif not Is_First_Subtype (Rectype) then
6570 Error_Msg_N ("cannot give record rep clause for subtype", N);
6571 return;
9dc88aea 6572
d9f6a4ee 6573 elsif Has_Record_Rep_Clause (Rectype) then
6574 Error_Msg_N ("duplicate record rep clause ignored", N);
6575 return;
9dc88aea 6576
d9f6a4ee 6577 elsif Rep_Item_Too_Late (Rectype, N) then
6578 return;
9dc88aea 6579 end if;
fb7f2fc4 6580
2ced3742 6581 -- We know we have a first subtype, now possibly go to the anonymous
d9f6a4ee 6582 -- base type to determine whether Rectype is a record extension.
89f1e35c 6583
d9f6a4ee 6584 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6585 Is_Record_Extension :=
6586 Nkind (Recdef) = N_Derived_Type_Definition
6587 and then Present (Record_Extension_Part (Recdef));
89f1e35c 6588
d9f6a4ee 6589 if Present (Mod_Clause (N)) then
fb7f2fc4 6590 declare
d9f6a4ee 6591 Loc : constant Source_Ptr := Sloc (N);
6592 M : constant Node_Id := Mod_Clause (N);
6593 P : constant List_Id := Pragmas_Before (M);
6594 AtM_Nod : Node_Id;
6595
6596 Mod_Val : Uint;
6597 pragma Warnings (Off, Mod_Val);
fb7f2fc4 6598
6599 begin
d9f6a4ee 6600 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
fb7f2fc4 6601
d9f6a4ee 6602 if Warn_On_Obsolescent_Feature then
6603 Error_Msg_N
6604 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6605 Error_Msg_N
6606 ("\?j?use alignment attribute definition clause instead", N);
6607 end if;
fb7f2fc4 6608
d9f6a4ee 6609 if Present (P) then
6610 Analyze_List (P);
6611 end if;
89f1e35c 6612
d9f6a4ee 6613 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6614 -- the Mod clause into an alignment clause anyway, so that the
6615 -- back-end can compute and back-annotate properly the size and
6616 -- alignment of types that may include this record.
be9124d0 6617
d9f6a4ee 6618 -- This seems dubious, this destroys the source tree in a manner
6619 -- not detectable by ASIS ???
be9124d0 6620
d9f6a4ee 6621 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6622 AtM_Nod :=
6623 Make_Attribute_Definition_Clause (Loc,
83c6c069 6624 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
d9f6a4ee 6625 Chars => Name_Alignment,
6626 Expression => Relocate_Node (Expression (M)));
be9124d0 6627
d9f6a4ee 6628 Set_From_At_Mod (AtM_Nod);
6629 Insert_After (N, AtM_Nod);
6630 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6631 Set_Mod_Clause (N, Empty);
be9124d0 6632
d9f6a4ee 6633 else
6634 -- Get the alignment value to perform error checking
be9124d0 6635
d9f6a4ee 6636 Mod_Val := Get_Alignment_Value (Expression (M));
6637 end if;
6638 end;
6639 end if;
be9124d0 6640
d9f6a4ee 6641 -- For untagged types, clear any existing component clauses for the
6642 -- type. If the type is derived, this is what allows us to override
6643 -- a rep clause for the parent. For type extensions, the representation
6644 -- of the inherited components is inherited, so we want to keep previous
6645 -- component clauses for completeness.
be9124d0 6646
d9f6a4ee 6647 if not Is_Tagged_Type (Rectype) then
6648 Comp := First_Component_Or_Discriminant (Rectype);
6649 while Present (Comp) loop
6650 Set_Component_Clause (Comp, Empty);
6651 Next_Component_Or_Discriminant (Comp);
6652 end loop;
6653 end if;
be9124d0 6654
d9f6a4ee 6655 -- All done if no component clauses
be9124d0 6656
d9f6a4ee 6657 CC := First (Component_Clauses (N));
be9124d0 6658
d9f6a4ee 6659 if No (CC) then
6660 return;
6661 end if;
be9124d0 6662
d9f6a4ee 6663 -- A representation like this applies to the base type
be9124d0 6664
d9f6a4ee 6665 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6666 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6667 Set_Has_Specified_Layout (Base_Type (Rectype));
be9124d0 6668
d9f6a4ee 6669 -- Process the component clauses
be9124d0 6670
d9f6a4ee 6671 while Present (CC) loop
be9124d0 6672
d9f6a4ee 6673 -- Pragma
be9124d0 6674
d9f6a4ee 6675 if Nkind (CC) = N_Pragma then
6676 Analyze (CC);
be9124d0 6677
d9f6a4ee 6678 -- The only pragma of interest is Complete_Representation
be9124d0 6679
d9f6a4ee 6680 if Pragma_Name (CC) = Name_Complete_Representation then
6681 CR_Pragma := CC;
6682 end if;
be9124d0 6683
d9f6a4ee 6684 -- Processing for real component clause
be9124d0 6685
d9f6a4ee 6686 else
6687 Posit := Static_Integer (Position (CC));
6688 Fbit := Static_Integer (First_Bit (CC));
6689 Lbit := Static_Integer (Last_Bit (CC));
be9124d0 6690
d9f6a4ee 6691 if Posit /= No_Uint
6692 and then Fbit /= No_Uint
6693 and then Lbit /= No_Uint
6694 then
6695 if Posit < 0 then
6696 Error_Msg_N
6697 ("position cannot be negative", Position (CC));
be9124d0 6698
d9f6a4ee 6699 elsif Fbit < 0 then
6700 Error_Msg_N
6701 ("first bit cannot be negative", First_Bit (CC));
be9124d0 6702
d9f6a4ee 6703 -- The Last_Bit specified in a component clause must not be
6704 -- less than the First_Bit minus one (RM-13.5.1(10)).
be9124d0 6705
d9f6a4ee 6706 elsif Lbit < Fbit - 1 then
6707 Error_Msg_N
6708 ("last bit cannot be less than first bit minus one",
6709 Last_Bit (CC));
be9124d0 6710
d9f6a4ee 6711 -- Values look OK, so find the corresponding record component
6712 -- Even though the syntax allows an attribute reference for
6713 -- implementation-defined components, GNAT does not allow the
6714 -- tag to get an explicit position.
be9124d0 6715
d9f6a4ee 6716 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6717 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6718 Error_Msg_N ("position of tag cannot be specified", CC);
6719 else
6720 Error_Msg_N ("illegal component name", CC);
6721 end if;
be9124d0 6722
d9f6a4ee 6723 else
6724 Comp := First_Entity (Rectype);
6725 while Present (Comp) loop
6726 exit when Chars (Comp) = Chars (Component_Name (CC));
6727 Next_Entity (Comp);
6728 end loop;
be9124d0 6729
d9f6a4ee 6730 if No (Comp) then
be9124d0 6731
d9f6a4ee 6732 -- Maybe component of base type that is absent from
6733 -- statically constrained first subtype.
be9124d0 6734
d9f6a4ee 6735 Comp := First_Entity (Base_Type (Rectype));
6736 while Present (Comp) loop
6737 exit when Chars (Comp) = Chars (Component_Name (CC));
6738 Next_Entity (Comp);
6739 end loop;
6740 end if;
be9124d0 6741
d9f6a4ee 6742 if No (Comp) then
6743 Error_Msg_N
6744 ("component clause is for non-existent field", CC);
be9124d0 6745
d9f6a4ee 6746 -- Ada 2012 (AI05-0026): Any name that denotes a
6747 -- discriminant of an object of an unchecked union type
6748 -- shall not occur within a record_representation_clause.
be9124d0 6749
d9f6a4ee 6750 -- The general restriction of using record rep clauses on
6751 -- Unchecked_Union types has now been lifted. Since it is
6752 -- possible to introduce a record rep clause which mentions
6753 -- the discriminant of an Unchecked_Union in non-Ada 2012
6754 -- code, this check is applied to all versions of the
6755 -- language.
be9124d0 6756
d9f6a4ee 6757 elsif Ekind (Comp) = E_Discriminant
6758 and then Is_Unchecked_Union (Rectype)
6759 then
6760 Error_Msg_N
6761 ("cannot reference discriminant of unchecked union",
6762 Component_Name (CC));
be9124d0 6763
d9f6a4ee 6764 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6765 Error_Msg_NE
6766 ("component clause not allowed for inherited "
6767 & "component&", CC, Comp);
40ca69b9 6768
d9f6a4ee 6769 elsif Present (Component_Clause (Comp)) then
462a079f 6770
d9f6a4ee 6771 -- Diagnose duplicate rep clause, or check consistency
6772 -- if this is an inherited component. In a double fault,
6773 -- there may be a duplicate inconsistent clause for an
6774 -- inherited component.
462a079f 6775
d9f6a4ee 6776 if Scope (Original_Record_Component (Comp)) = Rectype
6777 or else Parent (Component_Clause (Comp)) = N
6778 then
6779 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6780 Error_Msg_N ("component clause previously given#", CC);
3062c401 6781
6782 else
6783 declare
6784 Rep1 : constant Node_Id := Component_Clause (Comp);
3062c401 6785 begin
6786 if Intval (Position (Rep1)) /=
6787 Intval (Position (CC))
6788 or else Intval (First_Bit (Rep1)) /=
6789 Intval (First_Bit (CC))
6790 or else Intval (Last_Bit (Rep1)) /=
6791 Intval (Last_Bit (CC))
6792 then
b9e61b2a 6793 Error_Msg_N
6794 ("component clause inconsistent "
6795 & "with representation of ancestor", CC);
6a06584c 6796
3062c401 6797 elsif Warn_On_Redundant_Constructs then
b9e61b2a 6798 Error_Msg_N
6a06584c 6799 ("?r?redundant confirming component clause "
6800 & "for component!", CC);
3062c401 6801 end if;
6802 end;
6803 end if;
d6f39728 6804
d2b860b4 6805 -- Normal case where this is the first component clause we
6806 -- have seen for this entity, so set it up properly.
6807
d6f39728 6808 else
83f8f0a6 6809 -- Make reference for field in record rep clause and set
6810 -- appropriate entity field in the field identifier.
6811
6812 Generate_Reference
6813 (Comp, Component_Name (CC), Set_Ref => False);
6814 Set_Entity (Component_Name (CC), Comp);
6815
2866d595 6816 -- Update Fbit and Lbit to the actual bit number
d6f39728 6817
6818 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6819 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6820
d6f39728 6821 if Has_Size_Clause (Rectype)
ada34def 6822 and then RM_Size (Rectype) <= Lbit
d6f39728 6823 then
6824 Error_Msg_N
6825 ("bit number out of range of specified size",
6826 Last_Bit (CC));
6827 else
6828 Set_Component_Clause (Comp, CC);
6829 Set_Component_Bit_Offset (Comp, Fbit);
6830 Set_Esize (Comp, 1 + (Lbit - Fbit));
6831 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6832 Set_Normalized_Position (Comp, Fbit / SSU);
6833
a0fc8c5b 6834 if Warn_On_Overridden_Size
6835 and then Has_Size_Clause (Etype (Comp))
6836 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6837 then
6838 Error_Msg_NE
1e3532e7 6839 ("?S?component size overrides size clause for&",
a0fc8c5b 6840 Component_Name (CC), Etype (Comp));
6841 end if;
6842
ea61a7ea 6843 -- This information is also set in the corresponding
6844 -- component of the base type, found by accessing the
6845 -- Original_Record_Component link if it is present.
d6f39728 6846
6847 Ocomp := Original_Record_Component (Comp);
6848
6849 if Hbit < Lbit then
6850 Hbit := Lbit;
6851 end if;
6852
6853 Check_Size
6854 (Component_Name (CC),
6855 Etype (Comp),
6856 Esize (Comp),
6857 Biased);
6858
b77e4501 6859 Set_Biased
6860 (Comp, First_Node (CC), "component clause", Biased);
cc46ff4b 6861
d6f39728 6862 if Present (Ocomp) then
6863 Set_Component_Clause (Ocomp, CC);
6864 Set_Component_Bit_Offset (Ocomp, Fbit);
6865 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6866 Set_Normalized_Position (Ocomp, Fbit / SSU);
6867 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6868
6869 Set_Normalized_Position_Max
6870 (Ocomp, Normalized_Position (Ocomp));
6871
b77e4501 6872 -- Note: we don't use Set_Biased here, because we
6873 -- already gave a warning above if needed, and we
6874 -- would get a duplicate for the same name here.
6875
d6f39728 6876 Set_Has_Biased_Representation
6877 (Ocomp, Has_Biased_Representation (Comp));
6878 end if;
6879
6880 if Esize (Comp) < 0 then
6881 Error_Msg_N ("component size is negative", CC);
6882 end if;
6883 end if;
6884 end if;
6885 end if;
6886 end if;
6887 end if;
6888
6889 Next (CC);
6890 end loop;
6891
67278d60 6892 -- Check missing components if Complete_Representation pragma appeared
d6f39728 6893
67278d60 6894 if Present (CR_Pragma) then
6895 Comp := First_Component_Or_Discriminant (Rectype);
6896 while Present (Comp) loop
6897 if No (Component_Clause (Comp)) then
6898 Error_Msg_NE
6899 ("missing component clause for &", CR_Pragma, Comp);
6900 end if;
d6f39728 6901
67278d60 6902 Next_Component_Or_Discriminant (Comp);
6903 end loop;
d6f39728 6904
1e3532e7 6905 -- Give missing components warning if required
15ebb600 6906
fdd294d1 6907 elsif Warn_On_Unrepped_Components then
15ebb600 6908 declare
6909 Num_Repped_Components : Nat := 0;
6910 Num_Unrepped_Components : Nat := 0;
6911
6912 begin
6913 -- First count number of repped and unrepped components
6914
6915 Comp := First_Component_Or_Discriminant (Rectype);
6916 while Present (Comp) loop
6917 if Present (Component_Clause (Comp)) then
6918 Num_Repped_Components := Num_Repped_Components + 1;
6919 else
6920 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6921 end if;
6922
6923 Next_Component_Or_Discriminant (Comp);
6924 end loop;
6925
6926 -- We are only interested in the case where there is at least one
6927 -- unrepped component, and at least half the components have rep
6928 -- clauses. We figure that if less than half have them, then the
87f9eef5 6929 -- partial rep clause is really intentional. If the component
6930 -- type has no underlying type set at this point (as for a generic
6931 -- formal type), we don't know enough to give a warning on the
6932 -- component.
15ebb600 6933
6934 if Num_Unrepped_Components > 0
6935 and then Num_Unrepped_Components < Num_Repped_Components
6936 then
6937 Comp := First_Component_Or_Discriminant (Rectype);
6938 while Present (Comp) loop
83f8f0a6 6939 if No (Component_Clause (Comp))
3062c401 6940 and then Comes_From_Source (Comp)
87f9eef5 6941 and then Present (Underlying_Type (Etype (Comp)))
83f8f0a6 6942 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
67278d60 6943 or else Size_Known_At_Compile_Time
6944 (Underlying_Type (Etype (Comp))))
fdd294d1 6945 and then not Has_Warnings_Off (Rectype)
2be1f7d7 6946
6947 -- Ignore discriminant in unchecked union, since it is
6948 -- not there, and cannot have a component clause.
6949
6950 and then (not Is_Unchecked_Union (Rectype)
6951 or else Ekind (Comp) /= E_Discriminant)
83f8f0a6 6952 then
15ebb600 6953 Error_Msg_Sloc := Sloc (Comp);
6954 Error_Msg_NE
1e3532e7 6955 ("?C?no component clause given for & declared #",
15ebb600 6956 N, Comp);
6957 end if;
6958
6959 Next_Component_Or_Discriminant (Comp);
6960 end loop;
6961 end if;
6962 end;
d6f39728 6963 end if;
d6f39728 6964 end Analyze_Record_Representation_Clause;
6965
eb66e842 6966 -------------------------------------
6967 -- Build_Discrete_Static_Predicate --
6968 -------------------------------------
9ea61fdd 6969
eb66e842 6970 procedure Build_Discrete_Static_Predicate
6971 (Typ : Entity_Id;
6972 Expr : Node_Id;
6973 Nam : Name_Id)
9ea61fdd 6974 is
eb66e842 6975 Loc : constant Source_Ptr := Sloc (Expr);
9ea61fdd 6976
eb66e842 6977 Non_Static : exception;
6978 -- Raised if something non-static is found
9ea61fdd 6979
eb66e842 6980 Btyp : constant Entity_Id := Base_Type (Typ);
9ea61fdd 6981
eb66e842 6982 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6983 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6984 -- Low bound and high bound value of base type of Typ
9ea61fdd 6985
afc229da 6986 TLo : Uint;
6987 THi : Uint;
6988 -- Bounds for constructing the static predicate. We use the bound of the
6989 -- subtype if it is static, otherwise the corresponding base type bound.
6990 -- Note: a non-static subtype can have a static predicate.
9ea61fdd 6991
eb66e842 6992 type REnt is record
6993 Lo, Hi : Uint;
6994 end record;
6995 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6996 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6997 -- value.
9ea61fdd 6998
eb66e842 6999 type RList is array (Nat range <>) of REnt;
7000 -- A list of ranges. The ranges are sorted in increasing order, and are
7001 -- disjoint (there is a gap of at least one value between each range in
7002 -- the table). A value is in the set of ranges in Rlist if it lies
7003 -- within one of these ranges.
9ea61fdd 7004
eb66e842 7005 False_Range : constant RList :=
7006 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7007 -- An empty set of ranges represents a range list that can never be
7008 -- satisfied, since there are no ranges in which the value could lie,
7009 -- so it does not lie in any of them. False_Range is a canonical value
7010 -- for this empty set, but general processing should test for an Rlist
7011 -- with length zero (see Is_False predicate), since other null ranges
7012 -- may appear which must be treated as False.
5b5df4a9 7013
eb66e842 7014 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7015 -- Range representing True, value must be in the base range
5b5df4a9 7016
eb66e842 7017 function "and" (Left : RList; Right : RList) return RList;
7018 -- And's together two range lists, returning a range list. This is a set
7019 -- intersection operation.
5b5df4a9 7020
eb66e842 7021 function "or" (Left : RList; Right : RList) return RList;
7022 -- Or's together two range lists, returning a range list. This is a set
7023 -- union operation.
87f3d5d3 7024
eb66e842 7025 function "not" (Right : RList) return RList;
7026 -- Returns complement of a given range list, i.e. a range list
7027 -- representing all the values in TLo .. THi that are not in the input
7028 -- operand Right.
ed4adc99 7029
eb66e842 7030 function Build_Val (V : Uint) return Node_Id;
7031 -- Return an analyzed N_Identifier node referencing this value, suitable
5c6a5792 7032 -- for use as an entry in the Static_Discrte_Predicate list. This node
7033 -- is typed with the base type.
5b5df4a9 7034
eb66e842 7035 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7036 -- Return an analyzed N_Range node referencing this range, suitable for
5c6a5792 7037 -- use as an entry in the Static_Discrete_Predicate list. This node is
7038 -- typed with the base type.
5b5df4a9 7039
eb66e842 7040 function Get_RList (Exp : Node_Id) return RList;
7041 -- This is a recursive routine that converts the given expression into a
7042 -- list of ranges, suitable for use in building the static predicate.
5b5df4a9 7043
eb66e842 7044 function Is_False (R : RList) return Boolean;
7045 pragma Inline (Is_False);
7046 -- Returns True if the given range list is empty, and thus represents a
7047 -- False list of ranges that can never be satisfied.
87f3d5d3 7048
eb66e842 7049 function Is_True (R : RList) return Boolean;
7050 -- Returns True if R trivially represents the True predicate by having a
7051 -- single range from BLo to BHi.
5b5df4a9 7052
eb66e842 7053 function Is_Type_Ref (N : Node_Id) return Boolean;
7054 pragma Inline (Is_Type_Ref);
7055 -- Returns if True if N is a reference to the type for the predicate in
7056 -- the expression (i.e. if it is an identifier whose Chars field matches
7de4cba3 7057 -- the Nam given in the call). N must not be parenthesized, if the type
7058 -- name appears in parens, this routine will return False.
5b5df4a9 7059
eb66e842 7060 function Lo_Val (N : Node_Id) return Uint;
5c6a5792 7061 -- Given an entry from a Static_Discrete_Predicate list that is either
7062 -- a static expression or static range, gets either the expression value
7063 -- or the low bound of the range.
5b5df4a9 7064
eb66e842 7065 function Hi_Val (N : Node_Id) return Uint;
5c6a5792 7066 -- Given an entry from a Static_Discrete_Predicate list that is either
7067 -- a static expression or static range, gets either the expression value
7068 -- or the high bound of the range.
5b5df4a9 7069
eb66e842 7070 function Membership_Entry (N : Node_Id) return RList;
7071 -- Given a single membership entry (range, value, or subtype), returns
7072 -- the corresponding range list. Raises Static_Error if not static.
5b5df4a9 7073
eb66e842 7074 function Membership_Entries (N : Node_Id) return RList;
7075 -- Given an element on an alternatives list of a membership operation,
7076 -- returns the range list corresponding to this entry and all following
7077 -- entries (i.e. returns the "or" of this list of values).
b9e61b2a 7078
eb66e842 7079 function Stat_Pred (Typ : Entity_Id) return RList;
7080 -- Given a type, if it has a static predicate, then return the predicate
7081 -- as a range list, otherwise raise Non_Static.
c4968aa2 7082
eb66e842 7083 -----------
7084 -- "and" --
7085 -----------
c4968aa2 7086
eb66e842 7087 function "and" (Left : RList; Right : RList) return RList is
7088 FEnt : REnt;
7089 -- First range of result
c4968aa2 7090
eb66e842 7091 SLeft : Nat := Left'First;
7092 -- Start of rest of left entries
c4968aa2 7093
eb66e842 7094 SRight : Nat := Right'First;
7095 -- Start of rest of right entries
2072eaa9 7096
eb66e842 7097 begin
7098 -- If either range is True, return the other
5b5df4a9 7099
eb66e842 7100 if Is_True (Left) then
7101 return Right;
7102 elsif Is_True (Right) then
7103 return Left;
7104 end if;
87f3d5d3 7105
eb66e842 7106 -- If either range is False, return False
5b5df4a9 7107
eb66e842 7108 if Is_False (Left) or else Is_False (Right) then
7109 return False_Range;
7110 end if;
4c1fd062 7111
eb66e842 7112 -- Loop to remove entries at start that are disjoint, and thus just
7113 -- get discarded from the result entirely.
5b5df4a9 7114
eb66e842 7115 loop
7116 -- If no operands left in either operand, result is false
5b5df4a9 7117
eb66e842 7118 if SLeft > Left'Last or else SRight > Right'Last then
7119 return False_Range;
5b5df4a9 7120
eb66e842 7121 -- Discard first left operand entry if disjoint with right
5b5df4a9 7122
eb66e842 7123 elsif Left (SLeft).Hi < Right (SRight).Lo then
7124 SLeft := SLeft + 1;
5b5df4a9 7125
eb66e842 7126 -- Discard first right operand entry if disjoint with left
5b5df4a9 7127
eb66e842 7128 elsif Right (SRight).Hi < Left (SLeft).Lo then
7129 SRight := SRight + 1;
5b5df4a9 7130
eb66e842 7131 -- Otherwise we have an overlapping entry
5b5df4a9 7132
eb66e842 7133 else
7134 exit;
7135 end if;
7136 end loop;
5b5df4a9 7137
eb66e842 7138 -- Now we have two non-null operands, and first entries overlap. The
7139 -- first entry in the result will be the overlapping part of these
7140 -- two entries.
47a46747 7141
eb66e842 7142 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7143 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
47a46747 7144
eb66e842 7145 -- Now we can remove the entry that ended at a lower value, since its
7146 -- contribution is entirely contained in Fent.
5b5df4a9 7147
eb66e842 7148 if Left (SLeft).Hi <= Right (SRight).Hi then
7149 SLeft := SLeft + 1;
7150 else
7151 SRight := SRight + 1;
7152 end if;
5b5df4a9 7153
eb66e842 7154 -- Compute result by concatenating this first entry with the "and" of
7155 -- the remaining parts of the left and right operands. Note that if
7156 -- either of these is empty, "and" will yield empty, so that we will
7157 -- end up with just Fent, which is what we want in that case.
5b5df4a9 7158
eb66e842 7159 return
7160 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7161 end "and";
fb7f2fc4 7162
eb66e842 7163 -----------
7164 -- "not" --
7165 -----------
fb7f2fc4 7166
eb66e842 7167 function "not" (Right : RList) return RList is
7168 begin
7169 -- Return True if False range
fb7f2fc4 7170
eb66e842 7171 if Is_False (Right) then
7172 return True_Range;
7173 end if;
ed4adc99 7174
eb66e842 7175 -- Return False if True range
fb7f2fc4 7176
eb66e842 7177 if Is_True (Right) then
7178 return False_Range;
7179 end if;
fb7f2fc4 7180
eb66e842 7181 -- Here if not trivial case
87f3d5d3 7182
eb66e842 7183 declare
7184 Result : RList (1 .. Right'Length + 1);
7185 -- May need one more entry for gap at beginning and end
87f3d5d3 7186
eb66e842 7187 Count : Nat := 0;
7188 -- Number of entries stored in Result
4098232e 7189
eb66e842 7190 begin
7191 -- Gap at start
4098232e 7192
eb66e842 7193 if Right (Right'First).Lo > TLo then
7194 Count := Count + 1;
7195 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7196 end if;
ed4adc99 7197
eb66e842 7198 -- Gaps between ranges
ed4adc99 7199
eb66e842 7200 for J in Right'First .. Right'Last - 1 loop
7201 Count := Count + 1;
7202 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7203 end loop;
5b5df4a9 7204
eb66e842 7205 -- Gap at end
5b5df4a9 7206
eb66e842 7207 if Right (Right'Last).Hi < THi then
7208 Count := Count + 1;
7209 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7210 end if;
5b5df4a9 7211
eb66e842 7212 return Result (1 .. Count);
7213 end;
7214 end "not";
5b5df4a9 7215
eb66e842 7216 ----------
7217 -- "or" --
7218 ----------
5b5df4a9 7219
eb66e842 7220 function "or" (Left : RList; Right : RList) return RList is
7221 FEnt : REnt;
7222 -- First range of result
5b5df4a9 7223
eb66e842 7224 SLeft : Nat := Left'First;
7225 -- Start of rest of left entries
5b5df4a9 7226
eb66e842 7227 SRight : Nat := Right'First;
7228 -- Start of rest of right entries
5b5df4a9 7229
eb66e842 7230 begin
7231 -- If either range is True, return True
5b5df4a9 7232
eb66e842 7233 if Is_True (Left) or else Is_True (Right) then
7234 return True_Range;
7235 end if;
5b5df4a9 7236
eb66e842 7237 -- If either range is False (empty), return the other
5b5df4a9 7238
eb66e842 7239 if Is_False (Left) then
7240 return Right;
7241 elsif Is_False (Right) then
7242 return Left;
7243 end if;
5b5df4a9 7244
eb66e842 7245 -- Initialize result first entry from left or right operand depending
7246 -- on which starts with the lower range.
5b5df4a9 7247
eb66e842 7248 if Left (SLeft).Lo < Right (SRight).Lo then
7249 FEnt := Left (SLeft);
7250 SLeft := SLeft + 1;
7251 else
7252 FEnt := Right (SRight);
7253 SRight := SRight + 1;
7254 end if;
5b5df4a9 7255
eb66e842 7256 -- This loop eats ranges from left and right operands that are
7257 -- contiguous with the first range we are gathering.
9ea61fdd 7258
eb66e842 7259 loop
7260 -- Eat first entry in left operand if contiguous or overlapped by
7261 -- gathered first operand of result.
9ea61fdd 7262
eb66e842 7263 if SLeft <= Left'Last
7264 and then Left (SLeft).Lo <= FEnt.Hi + 1
7265 then
7266 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7267 SLeft := SLeft + 1;
9ea61fdd 7268
eb66e842 7269 -- Eat first entry in right operand if contiguous or overlapped by
7270 -- gathered right operand of result.
9ea61fdd 7271
eb66e842 7272 elsif SRight <= Right'Last
7273 and then Right (SRight).Lo <= FEnt.Hi + 1
7274 then
7275 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7276 SRight := SRight + 1;
9ea61fdd 7277
eb66e842 7278 -- All done if no more entries to eat
5b5df4a9 7279
eb66e842 7280 else
7281 exit;
7282 end if;
7283 end loop;
5b5df4a9 7284
eb66e842 7285 -- Obtain result as the first entry we just computed, concatenated
7286 -- to the "or" of the remaining results (if one operand is empty,
7287 -- this will just concatenate with the other
5b5df4a9 7288
eb66e842 7289 return
7290 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7291 end "or";
5b5df4a9 7292
eb66e842 7293 -----------------
7294 -- Build_Range --
7295 -----------------
5b5df4a9 7296
eb66e842 7297 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7298 Result : Node_Id;
5b5df4a9 7299 begin
eb66e842 7300 Result :=
7301 Make_Range (Loc,
7302 Low_Bound => Build_Val (Lo),
7303 High_Bound => Build_Val (Hi));
7304 Set_Etype (Result, Btyp);
7305 Set_Analyzed (Result);
7306 return Result;
7307 end Build_Range;
5b5df4a9 7308
eb66e842 7309 ---------------
7310 -- Build_Val --
7311 ---------------
5b5df4a9 7312
eb66e842 7313 function Build_Val (V : Uint) return Node_Id is
7314 Result : Node_Id;
5b5df4a9 7315
eb66e842 7316 begin
7317 if Is_Enumeration_Type (Typ) then
7318 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7319 else
7320 Result := Make_Integer_Literal (Loc, V);
7321 end if;
5b5df4a9 7322
eb66e842 7323 Set_Etype (Result, Btyp);
7324 Set_Is_Static_Expression (Result);
7325 Set_Analyzed (Result);
7326 return Result;
7327 end Build_Val;
87f3d5d3 7328
eb66e842 7329 ---------------
7330 -- Get_RList --
7331 ---------------
87f3d5d3 7332
eb66e842 7333 function Get_RList (Exp : Node_Id) return RList is
7334 Op : Node_Kind;
7335 Val : Uint;
87f3d5d3 7336
eb66e842 7337 begin
7338 -- Static expression can only be true or false
87f3d5d3 7339
eb66e842 7340 if Is_OK_Static_Expression (Exp) then
7341 if Expr_Value (Exp) = 0 then
7342 return False_Range;
7343 else
7344 return True_Range;
9ea61fdd 7345 end if;
eb66e842 7346 end if;
87f3d5d3 7347
eb66e842 7348 -- Otherwise test node type
192b8dab 7349
eb66e842 7350 Op := Nkind (Exp);
192b8dab 7351
eb66e842 7352 case Op is
5d3fb947 7353
eb66e842 7354 -- And
5d3fb947 7355
eb66e842 7356 when N_Op_And | N_And_Then =>
7357 return Get_RList (Left_Opnd (Exp))
7358 and
7359 Get_RList (Right_Opnd (Exp));
5b5df4a9 7360
eb66e842 7361 -- Or
9dc88aea 7362
eb66e842 7363 when N_Op_Or | N_Or_Else =>
7364 return Get_RList (Left_Opnd (Exp))
7365 or
7366 Get_RList (Right_Opnd (Exp));
7c443ae8 7367
eb66e842 7368 -- Not
9dc88aea 7369
eb66e842 7370 when N_Op_Not =>
7371 return not Get_RList (Right_Opnd (Exp));
9dc88aea 7372
eb66e842 7373 -- Comparisons of type with static value
84c8f0b8 7374
eb66e842 7375 when N_Op_Compare =>
490beba6 7376
eb66e842 7377 -- Type is left operand
9dc88aea 7378
eb66e842 7379 if Is_Type_Ref (Left_Opnd (Exp))
7380 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7381 then
7382 Val := Expr_Value (Right_Opnd (Exp));
84c8f0b8 7383
eb66e842 7384 -- Typ is right operand
84c8f0b8 7385
eb66e842 7386 elsif Is_Type_Ref (Right_Opnd (Exp))
7387 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7388 then
7389 Val := Expr_Value (Left_Opnd (Exp));
84c8f0b8 7390
eb66e842 7391 -- Invert sense of comparison
84c8f0b8 7392
eb66e842 7393 case Op is
7394 when N_Op_Gt => Op := N_Op_Lt;
7395 when N_Op_Lt => Op := N_Op_Gt;
7396 when N_Op_Ge => Op := N_Op_Le;
7397 when N_Op_Le => Op := N_Op_Ge;
7398 when others => null;
7399 end case;
84c8f0b8 7400
eb66e842 7401 -- Other cases are non-static
34d045d3 7402
eb66e842 7403 else
7404 raise Non_Static;
7405 end if;
9dc88aea 7406
eb66e842 7407 -- Construct range according to comparison operation
9dc88aea 7408
eb66e842 7409 case Op is
7410 when N_Op_Eq =>
7411 return RList'(1 => REnt'(Val, Val));
9dc88aea 7412
eb66e842 7413 when N_Op_Ge =>
7414 return RList'(1 => REnt'(Val, BHi));
84c8f0b8 7415
eb66e842 7416 when N_Op_Gt =>
7417 return RList'(1 => REnt'(Val + 1, BHi));
84c8f0b8 7418
eb66e842 7419 when N_Op_Le =>
7420 return RList'(1 => REnt'(BLo, Val));
fb7f2fc4 7421
eb66e842 7422 when N_Op_Lt =>
7423 return RList'(1 => REnt'(BLo, Val - 1));
9dc88aea 7424
eb66e842 7425 when N_Op_Ne =>
7426 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9dc88aea 7427
eb66e842 7428 when others =>
7429 raise Program_Error;
7430 end case;
9dc88aea 7431
eb66e842 7432 -- Membership (IN)
9dc88aea 7433
eb66e842 7434 when N_In =>
7435 if not Is_Type_Ref (Left_Opnd (Exp)) then
7436 raise Non_Static;
7437 end if;
9dc88aea 7438
eb66e842 7439 if Present (Right_Opnd (Exp)) then
7440 return Membership_Entry (Right_Opnd (Exp));
7441 else
7442 return Membership_Entries (First (Alternatives (Exp)));
7443 end if;
9dc88aea 7444
eb66e842 7445 -- Negative membership (NOT IN)
9dc88aea 7446
eb66e842 7447 when N_Not_In =>
7448 if not Is_Type_Ref (Left_Opnd (Exp)) then
7449 raise Non_Static;
7450 end if;
9dc88aea 7451
eb66e842 7452 if Present (Right_Opnd (Exp)) then
7453 return not Membership_Entry (Right_Opnd (Exp));
7454 else
7455 return not Membership_Entries (First (Alternatives (Exp)));
7456 end if;
9dc88aea 7457
eb66e842 7458 -- Function call, may be call to static predicate
9dc88aea 7459
eb66e842 7460 when N_Function_Call =>
7461 if Is_Entity_Name (Name (Exp)) then
7462 declare
7463 Ent : constant Entity_Id := Entity (Name (Exp));
7464 begin
7465 if Is_Predicate_Function (Ent)
7466 or else
7467 Is_Predicate_Function_M (Ent)
7468 then
7469 return Stat_Pred (Etype (First_Formal (Ent)));
7470 end if;
7471 end;
7472 end if;
9dc88aea 7473
eb66e842 7474 -- Other function call cases are non-static
9dc88aea 7475
eb66e842 7476 raise Non_Static;
490beba6 7477
eb66e842 7478 -- Qualified expression, dig out the expression
c92e878b 7479
eb66e842 7480 when N_Qualified_Expression =>
7481 return Get_RList (Expression (Exp));
4c1fd062 7482
eb66e842 7483 when N_Case_Expression =>
7484 declare
7485 Alt : Node_Id;
7486 Choices : List_Id;
7487 Dep : Node_Id;
4c1fd062 7488
eb66e842 7489 begin
7490 if not Is_Entity_Name (Expression (Expr))
7491 or else Etype (Expression (Expr)) /= Typ
7492 then
7493 Error_Msg_N
7494 ("expression must denaote subtype", Expression (Expr));
7495 return False_Range;
7496 end if;
9dc88aea 7497
eb66e842 7498 -- Collect discrete choices in all True alternatives
9dc88aea 7499
eb66e842 7500 Choices := New_List;
7501 Alt := First (Alternatives (Exp));
7502 while Present (Alt) loop
7503 Dep := Expression (Alt);
34d045d3 7504
cda40848 7505 if not Is_OK_Static_Expression (Dep) then
eb66e842 7506 raise Non_Static;
ebbab42d 7507
eb66e842 7508 elsif Is_True (Expr_Value (Dep)) then
7509 Append_List_To (Choices,
7510 New_Copy_List (Discrete_Choices (Alt)));
7511 end if;
fb7f2fc4 7512
eb66e842 7513 Next (Alt);
7514 end loop;
9dc88aea 7515
eb66e842 7516 return Membership_Entries (First (Choices));
7517 end;
9dc88aea 7518
eb66e842 7519 -- Expression with actions: if no actions, dig out expression
9dc88aea 7520
eb66e842 7521 when N_Expression_With_Actions =>
7522 if Is_Empty_List (Actions (Exp)) then
7523 return Get_RList (Expression (Exp));
7524 else
7525 raise Non_Static;
7526 end if;
9dc88aea 7527
eb66e842 7528 -- Xor operator
490beba6 7529
eb66e842 7530 when N_Op_Xor =>
7531 return (Get_RList (Left_Opnd (Exp))
7532 and not Get_RList (Right_Opnd (Exp)))
7533 or (Get_RList (Right_Opnd (Exp))
7534 and not Get_RList (Left_Opnd (Exp)));
9dc88aea 7535
eb66e842 7536 -- Any other node type is non-static
fb7f2fc4 7537
eb66e842 7538 when others =>
7539 raise Non_Static;
7540 end case;
7541 end Get_RList;
fb7f2fc4 7542
eb66e842 7543 ------------
7544 -- Hi_Val --
7545 ------------
fb7f2fc4 7546
eb66e842 7547 function Hi_Val (N : Node_Id) return Uint is
7548 begin
cda40848 7549 if Is_OK_Static_Expression (N) then
eb66e842 7550 return Expr_Value (N);
7551 else
7552 pragma Assert (Nkind (N) = N_Range);
7553 return Expr_Value (High_Bound (N));
7554 end if;
7555 end Hi_Val;
fb7f2fc4 7556
eb66e842 7557 --------------
7558 -- Is_False --
7559 --------------
fb7f2fc4 7560
eb66e842 7561 function Is_False (R : RList) return Boolean is
7562 begin
7563 return R'Length = 0;
7564 end Is_False;
9dc88aea 7565
eb66e842 7566 -------------
7567 -- Is_True --
7568 -------------
9dc88aea 7569
eb66e842 7570 function Is_True (R : RList) return Boolean is
7571 begin
7572 return R'Length = 1
7573 and then R (R'First).Lo = BLo
7574 and then R (R'First).Hi = BHi;
7575 end Is_True;
9dc88aea 7576
eb66e842 7577 -----------------
7578 -- Is_Type_Ref --
7579 -----------------
9dc88aea 7580
eb66e842 7581 function Is_Type_Ref (N : Node_Id) return Boolean is
7582 begin
7de4cba3 7583 return Nkind (N) = N_Identifier
7584 and then Chars (N) = Nam
7585 and then Paren_Count (N) = 0;
eb66e842 7586 end Is_Type_Ref;
9dc88aea 7587
eb66e842 7588 ------------
7589 -- Lo_Val --
7590 ------------
9dc88aea 7591
eb66e842 7592 function Lo_Val (N : Node_Id) return Uint is
84c8f0b8 7593 begin
cda40848 7594 if Is_OK_Static_Expression (N) then
eb66e842 7595 return Expr_Value (N);
84c8f0b8 7596 else
eb66e842 7597 pragma Assert (Nkind (N) = N_Range);
7598 return Expr_Value (Low_Bound (N));
84c8f0b8 7599 end if;
eb66e842 7600 end Lo_Val;
d97beb2f 7601
eb66e842 7602 ------------------------
7603 -- Membership_Entries --
7604 ------------------------
d97beb2f 7605
eb66e842 7606 function Membership_Entries (N : Node_Id) return RList is
84c8f0b8 7607 begin
eb66e842 7608 if No (Next (N)) then
7609 return Membership_Entry (N);
84c8f0b8 7610 else
eb66e842 7611 return Membership_Entry (N) or Membership_Entries (Next (N));
84c8f0b8 7612 end if;
eb66e842 7613 end Membership_Entries;
84c8f0b8 7614
eb66e842 7615 ----------------------
7616 -- Membership_Entry --
7617 ----------------------
84c8f0b8 7618
eb66e842 7619 function Membership_Entry (N : Node_Id) return RList is
7620 Val : Uint;
7621 SLo : Uint;
7622 SHi : Uint;
d97beb2f 7623
eb66e842 7624 begin
7625 -- Range case
d97beb2f 7626
eb66e842 7627 if Nkind (N) = N_Range then
cda40848 7628 if not Is_OK_Static_Expression (Low_Bound (N))
eb66e842 7629 or else
cda40848 7630 not Is_OK_Static_Expression (High_Bound (N))
eb66e842 7631 then
7632 raise Non_Static;
7633 else
7634 SLo := Expr_Value (Low_Bound (N));
7635 SHi := Expr_Value (High_Bound (N));
7636 return RList'(1 => REnt'(SLo, SHi));
7637 end if;
84c8f0b8 7638
eb66e842 7639 -- Static expression case
84c8f0b8 7640
cda40848 7641 elsif Is_OK_Static_Expression (N) then
eb66e842 7642 Val := Expr_Value (N);
7643 return RList'(1 => REnt'(Val, Val));
d97beb2f 7644
eb66e842 7645 -- Identifier (other than static expression) case
d97beb2f 7646
eb66e842 7647 else pragma Assert (Nkind (N) = N_Identifier);
d97beb2f 7648
eb66e842 7649 -- Type case
d97beb2f 7650
eb66e842 7651 if Is_Type (Entity (N)) then
d97beb2f 7652
eb66e842 7653 -- If type has predicates, process them
d97beb2f 7654
eb66e842 7655 if Has_Predicates (Entity (N)) then
7656 return Stat_Pred (Entity (N));
d97beb2f 7657
eb66e842 7658 -- For static subtype without predicates, get range
9dc88aea 7659
cda40848 7660 elsif Is_OK_Static_Subtype (Entity (N)) then
eb66e842 7661 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7662 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7663 return RList'(1 => REnt'(SLo, SHi));
9f269bd8 7664
eb66e842 7665 -- Any other type makes us non-static
9f269bd8 7666
eb66e842 7667 else
7668 raise Non_Static;
7669 end if;
84c8f0b8 7670
eb66e842 7671 -- Any other kind of identifier in predicate (e.g. a non-static
7672 -- expression value) means this is not a static predicate.
84c8f0b8 7673
eb66e842 7674 else
7675 raise Non_Static;
7676 end if;
7677 end if;
7678 end Membership_Entry;
84c8f0b8 7679
eb66e842 7680 ---------------
7681 -- Stat_Pred --
7682 ---------------
84c8f0b8 7683
eb66e842 7684 function Stat_Pred (Typ : Entity_Id) return RList is
7685 begin
7686 -- Not static if type does not have static predicates
84c8f0b8 7687
5c6a5792 7688 if not Has_Static_Predicate (Typ) then
eb66e842 7689 raise Non_Static;
7690 end if;
84c8f0b8 7691
eb66e842 7692 -- Otherwise we convert the predicate list to a range list
84c8f0b8 7693
eb66e842 7694 declare
5c6a5792 7695 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7696 Result : RList (1 .. List_Length (Spred));
eb66e842 7697 P : Node_Id;
84c8f0b8 7698
eb66e842 7699 begin
5c6a5792 7700 P := First (Static_Discrete_Predicate (Typ));
eb66e842 7701 for J in Result'Range loop
7702 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7703 Next (P);
7704 end loop;
84c8f0b8 7705
eb66e842 7706 return Result;
7707 end;
7708 end Stat_Pred;
84c8f0b8 7709
eb66e842 7710 -- Start of processing for Build_Discrete_Static_Predicate
84c8f0b8 7711
eb66e842 7712 begin
afc229da 7713 -- Establish bounds for the predicate
7714
7715 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7716 TLo := Expr_Value (Type_Low_Bound (Typ));
7717 else
7718 TLo := BLo;
7719 end if;
7720
7721 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7722 THi := Expr_Value (Type_High_Bound (Typ));
7723 else
7724 THi := BHi;
7725 end if;
7726
eb66e842 7727 -- Analyze the expression to see if it is a static predicate
84c8f0b8 7728
eb66e842 7729 declare
7730 Ranges : constant RList := Get_RList (Expr);
7731 -- Range list from expression if it is static
84c8f0b8 7732
eb66e842 7733 Plist : List_Id;
84c8f0b8 7734
eb66e842 7735 begin
7736 -- Convert range list into a form for the static predicate. In the
7737 -- Ranges array, we just have raw ranges, these must be converted
7738 -- to properly typed and analyzed static expressions or range nodes.
84c8f0b8 7739
eb66e842 7740 -- Note: here we limit ranges to the ranges of the subtype, so that
7741 -- a predicate is always false for values outside the subtype. That
7742 -- seems fine, such values are invalid anyway, and considering them
7743 -- to fail the predicate seems allowed and friendly, and furthermore
7744 -- simplifies processing for case statements and loops.
84c8f0b8 7745
eb66e842 7746 Plist := New_List;
7747
7748 for J in Ranges'Range loop
84c8f0b8 7749 declare
eb66e842 7750 Lo : Uint := Ranges (J).Lo;
7751 Hi : Uint := Ranges (J).Hi;
84c8f0b8 7752
eb66e842 7753 begin
7754 -- Ignore completely out of range entry
84c8f0b8 7755
eb66e842 7756 if Hi < TLo or else Lo > THi then
7757 null;
84c8f0b8 7758
eb66e842 7759 -- Otherwise process entry
84c8f0b8 7760
eb66e842 7761 else
7762 -- Adjust out of range value to subtype range
490beba6 7763
eb66e842 7764 if Lo < TLo then
7765 Lo := TLo;
7766 end if;
490beba6 7767
eb66e842 7768 if Hi > THi then
7769 Hi := THi;
7770 end if;
84c8f0b8 7771
eb66e842 7772 -- Convert range into required form
84c8f0b8 7773
eb66e842 7774 Append_To (Plist, Build_Range (Lo, Hi));
84c8f0b8 7775 end if;
eb66e842 7776 end;
7777 end loop;
84c8f0b8 7778
eb66e842 7779 -- Processing was successful and all entries were static, so now we
7780 -- can store the result as the predicate list.
84c8f0b8 7781
5c6a5792 7782 Set_Static_Discrete_Predicate (Typ, Plist);
84c8f0b8 7783
eb66e842 7784 -- The processing for static predicates put the expression into
7785 -- canonical form as a series of ranges. It also eliminated
7786 -- duplicates and collapsed and combined ranges. We might as well
7787 -- replace the alternatives list of the right operand of the
7788 -- membership test with the static predicate list, which will
7789 -- usually be more efficient.
84c8f0b8 7790
eb66e842 7791 declare
7792 New_Alts : constant List_Id := New_List;
7793 Old_Node : Node_Id;
7794 New_Node : Node_Id;
84c8f0b8 7795
eb66e842 7796 begin
7797 Old_Node := First (Plist);
7798 while Present (Old_Node) loop
7799 New_Node := New_Copy (Old_Node);
84c8f0b8 7800
eb66e842 7801 if Nkind (New_Node) = N_Range then
7802 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7803 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7804 end if;
84c8f0b8 7805
eb66e842 7806 Append_To (New_Alts, New_Node);
7807 Next (Old_Node);
7808 end loop;
84c8f0b8 7809
eb66e842 7810 -- If empty list, replace by False
84c8f0b8 7811
eb66e842 7812 if Is_Empty_List (New_Alts) then
7813 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
84c8f0b8 7814
eb66e842 7815 -- Else replace by set membership test
84c8f0b8 7816
eb66e842 7817 else
7818 Rewrite (Expr,
7819 Make_In (Loc,
7820 Left_Opnd => Make_Identifier (Loc, Nam),
7821 Right_Opnd => Empty,
7822 Alternatives => New_Alts));
490beba6 7823
eb66e842 7824 -- Resolve new expression in function context
490beba6 7825
eb66e842 7826 Install_Formals (Predicate_Function (Typ));
7827 Push_Scope (Predicate_Function (Typ));
7828 Analyze_And_Resolve (Expr, Standard_Boolean);
7829 Pop_Scope;
7830 end if;
7831 end;
7832 end;
9ab32fe9 7833
eb66e842 7834 -- If non-static, return doing nothing
9ab32fe9 7835
eb66e842 7836 exception
7837 when Non_Static =>
7838 return;
7839 end Build_Discrete_Static_Predicate;
64cc9e5d 7840
eb66e842 7841 -------------------------------------------
7842 -- Build_Invariant_Procedure_Declaration --
7843 -------------------------------------------
9ab32fe9 7844
eb66e842 7845 function Build_Invariant_Procedure_Declaration
7846 (Typ : Entity_Id) return Node_Id
7847 is
30f8d103 7848 Loc : constant Source_Ptr := Sloc (Typ);
f9e26ff7 7849 Decl : Node_Id;
7850 Obj_Id : Entity_Id;
7851 SId : Entity_Id;
9ab32fe9 7852
30f8d103 7853 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
7854
eb66e842 7855 begin
8d8194a6 7856 -- Check for duplicate definitions
eb66e842 7857
7858 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7859 return Empty;
d97beb2f 7860 end if;
d97beb2f 7861
30f8d103 7862 -- The related type may be subject to pragma Ghost. Set the mode now to
39629e6a 7863 -- ensure that the invariant procedure is properly marked as Ghost.
f9e26ff7 7864
7865 Set_Ghost_Mode_From_Entity (Typ);
7866
eb66e842 7867 SId :=
7868 Make_Defining_Identifier (Loc,
7869 Chars => New_External_Name (Chars (Typ), "Invariant"));
7870 Set_Has_Invariants (Typ);
7871 Set_Ekind (SId, E_Procedure);
856a9917 7872 Set_Etype (SId, Standard_Void_Type);
eb66e842 7873 Set_Is_Invariant_Procedure (SId);
7874 Set_Invariant_Procedure (Typ, SId);
d97beb2f 7875
f9e26ff7 7876 -- Mark the invariant procedure explicitly as Ghost because it does not
7877 -- come from source.
7878
7879 if Ghost_Mode > None then
7880 Set_Is_Ghost_Entity (SId);
7881 end if;
7882
7883 Obj_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7884 Set_Etype (Obj_Id, Typ);
7885
7886 Decl :=
7887 Make_Subprogram_Declaration (Loc,
7888 Make_Procedure_Specification (Loc,
7889 Defining_Unit_Name => SId,
7890 Parameter_Specifications => New_List (
7891 Make_Parameter_Specification (Loc,
7892 Defining_Identifier => Obj_Id,
7893 Parameter_Type => New_Occurrence_Of (Typ, Loc)))));
7894
30f8d103 7895 Ghost_Mode := Save_Ghost_Mode;
f9e26ff7 7896
7897 return Decl;
eb66e842 7898 end Build_Invariant_Procedure_Declaration;
7899
7900 -------------------------------
7901 -- Build_Invariant_Procedure --
7902 -------------------------------
7903
7904 -- The procedure that is constructed here has the form
7905
7906 -- procedure typInvariant (Ixxx : typ) is
7907 -- begin
7908 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7909 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7910 -- ...
7911 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7912 -- ...
7913 -- end typInvariant;
7914
7915 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
39629e6a 7916 procedure Add_Invariants
7917 (T : Entity_Id;
7918 Obj_Id : Entity_Id;
7919 Stmts : in out List_Id;
7920 Inherit : Boolean);
eb66e842 7921 -- Appends statements to Stmts for any invariants in the rep item chain
7922 -- of the given type. If Inherit is False, then we only process entries
7923 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7924 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7925 -- "inherited" to the exception message and generating an informational
7926 -- message about the inheritance of an invariant.
d97beb2f 7927
eb66e842 7928 --------------------
7929 -- Add_Invariants --
7930 --------------------
d97beb2f 7931
39629e6a 7932 procedure Add_Invariants
7933 (T : Entity_Id;
7934 Obj_Id : Entity_Id;
7935 Stmts : in out List_Id;
7936 Inherit : Boolean)
7937 is
f9e26ff7 7938 procedure Add_Invariant (Prag : Node_Id);
7939 -- Create a runtime check to verify the exression of invariant pragma
7940 -- Prag. All generated code is added to list Stmts.
d97beb2f 7941
f9e26ff7 7942 -------------------
7943 -- Add_Invariant --
7944 -------------------
d97beb2f 7945
f9e26ff7 7946 procedure Add_Invariant (Prag : Node_Id) is
7947 procedure Replace_Type_Reference (N : Node_Id);
7948 -- Replace a single occurrence N of the subtype name with a
7949 -- reference to the formal of the predicate function. N can be an
7950 -- identifier referencing the subtype, or a selected component,
7951 -- representing an appropriately qualified occurrence of the
7952 -- subtype name.
7953
7954 procedure Replace_Type_References is
7955 new Replace_Type_References_Generic (Replace_Type_Reference);
7956 -- Traverse an expression replacing all occurrences of the subtype
7957 -- name with appropriate references to the formal of the predicate
7958 -- function. Note that we must ensure that the type and entity
7959 -- information is properly set in the replacement node, since we
7960 -- will do a Preanalyze call of this expression without proper
7961 -- visibility of the procedure argument.
7962
7963 ----------------------------
7964 -- Replace_Type_Reference --
7965 ----------------------------
7966
7967 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7968 -- regarding handling of Sloc and Comes_From_Source.
7969
7970 procedure Replace_Type_Reference (N : Node_Id) is
7971 Nloc : constant Source_Ptr := Sloc (N);
d97beb2f 7972
f9e26ff7 7973 begin
7974 -- Add semantic information to node to be rewritten, for ASIS
7975 -- navigation needs.
d97beb2f 7976
f9e26ff7 7977 if Nkind (N) = N_Identifier then
7978 Set_Entity (N, T);
7979 Set_Etype (N, T);
d7c2851f 7980
f9e26ff7 7981 elsif Nkind (N) = N_Selected_Component then
7982 Analyze (Prefix (N));
7983 Set_Entity (Selector_Name (N), T);
7984 Set_Etype (Selector_Name (N), T);
7985 end if;
d7c2851f 7986
f9e26ff7 7987 -- Invariant'Class, replace with T'Class (obj)
d97beb2f 7988
f9e26ff7 7989 if Class_Present (Prag) then
69004fe6 7990
f9e26ff7 7991 -- In ASIS mode, an inherited item is already analyzed,
7992 -- and the replacement has been done, so do not repeat
7993 -- the transformation to prevent a malformed tree.
d9f6a4ee 7994
f9e26ff7 7995 if ASIS_Mode
7996 and then Nkind (Parent (N)) = N_Attribute_Reference
7997 and then Attribute_Name (Parent (N)) = Name_Class
7998 then
7999 null;
d9f6a4ee 8000
f9e26ff7 8001 else
8002 Rewrite (N,
8003 Make_Type_Conversion (Nloc,
8004 Subtype_Mark =>
8005 Make_Attribute_Reference (Nloc,
8006 Prefix => New_Occurrence_Of (T, Nloc),
8007 Attribute_Name => Name_Class),
39629e6a 8008 Expression =>
8009 Make_Identifier (Nloc, Chars (Obj_Id))));
f9e26ff7 8010
39629e6a 8011 Set_Entity (Expression (N), Obj_Id);
f9e26ff7 8012 Set_Etype (Expression (N), Typ);
8013 end if;
d9f6a4ee 8014
f9e26ff7 8015 -- Invariant, replace with obj
d9f6a4ee 8016
f9e26ff7 8017 else
39629e6a 8018 Rewrite (N, Make_Identifier (Nloc, Chars (Obj_Id)));
8019 Set_Entity (N, Obj_Id);
f9e26ff7 8020 Set_Etype (N, Typ);
8021 end if;
d9f6a4ee 8022
f9e26ff7 8023 Set_Comes_From_Source (N, True);
8024 end Replace_Type_Reference;
d9f6a4ee 8025
f9e26ff7 8026 -- Local variables
d9f6a4ee 8027
f9e26ff7 8028 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8029 Nam : constant Name_Id := Original_Aspect_Pragma_Name (Prag);
8030 Ploc : constant Source_Ptr := Sloc (Prag);
8031 Arg1 : Node_Id;
8032 Arg2 : Node_Id;
8033 Arg3 : Node_Id;
8034 Assoc : List_Id;
8035 Expr : Node_Id;
8036 Str : String_Id;
d9f6a4ee 8037
f9e26ff7 8038 -- Start of processing for Add_Invariant
d9f6a4ee 8039
f9e26ff7 8040 begin
8041 -- Extract the arguments of the invariant pragma
d9f6a4ee 8042
f9e26ff7 8043 Arg1 := First (Pragma_Argument_Associations (Prag));
8044 Arg2 := Next (Arg1);
8045 Arg3 := Next (Arg2);
d9f6a4ee 8046
f9e26ff7 8047 Arg1 := Get_Pragma_Arg (Arg1);
8048 Arg2 := Get_Pragma_Arg (Arg2);
d9f6a4ee 8049
f9e26ff7 8050 -- The caller requests processing of all Invariant'Class pragmas,
8051 -- but the current pragma does not fall in this category. Return
8052 -- as there is nothing left to do.
d9f6a4ee 8053
f9e26ff7 8054 if Inherit then
8055 if not Class_Present (Prag) then
8056 return;
8057 end if;
d9f6a4ee 8058
f9e26ff7 8059 -- Otherwise the pragma must apply to the current type
d9f6a4ee 8060
f9e26ff7 8061 elsif Entity (Arg1) /= T then
8062 return;
8063 end if;
d9f6a4ee 8064
f9e26ff7 8065 Expr := New_Copy_Tree (Arg2);
d9f6a4ee 8066
f9e26ff7 8067 -- Replace all occurrences of the type's name with references to
8068 -- the formal parameter of the invariant procedure.
d9f6a4ee 8069
f9e26ff7 8070 Replace_Type_References (Expr, T);
d9f6a4ee 8071
f9e26ff7 8072 -- If the invariant pragma comes from an aspect, replace the saved
8073 -- expression because we need the subtype references replaced for
8074 -- the calls to Preanalyze_Spec_Expression in Check_Aspect_At_xxx
8075 -- routines.
d9f6a4ee 8076
f9e26ff7 8077 if Present (Asp) then
8078 Set_Entity (Identifier (Asp), New_Copy_Tree (Expr));
8079 end if;
d9f6a4ee 8080
f9e26ff7 8081 -- Preanalyze the invariant expression to capture the visibility
8082 -- of the proper package part. In general the expression is not
8083 -- fully analyzed until the body of the invariant procedure is
8084 -- analyzed at the end of the private part, but that yields the
8085 -- wrong visibility.
d9f6a4ee 8086
8d8194a6 8087 -- Historical note: we used to set N as the parent, but a package
f9e26ff7 8088 -- specification as the parent of an expression is bizarre.
d9f6a4ee 8089
f9e26ff7 8090 Set_Parent (Expr, Parent (Arg2));
8091 Preanalyze_Assert_Expression (Expr, Any_Boolean);
d9f6a4ee 8092
f9e26ff7 8093 -- A class-wide invariant may be inherited in a separate unit,
8094 -- where the corresponding expression cannot be resolved by
8095 -- visibility, because it refers to a local function. Propagate
8096 -- semantic information to the original representation item, to
8097 -- be used when an invariant procedure for a derived type is
8098 -- constructed.
d9f6a4ee 8099
f9e26ff7 8100 -- ??? Unclear how to handle class-wide invariants that are not
8101 -- function calls.
f02a9a9a 8102
f9e26ff7 8103 if not Inherit
8104 and then Class_Present (Prag)
8105 and then Nkind (Expr) = N_Function_Call
8106 and then Nkind (Arg2) = N_Indexed_Component
8107 then
8108 Rewrite (Arg2,
8109 Make_Function_Call (Ploc,
8110 Name =>
8111 New_Occurrence_Of (Entity (Name (Expr)), Ploc),
8112 Parameter_Associations =>
8113 New_Copy_List (Expressions (Arg2))));
8114 end if;
f02a9a9a 8115
f9e26ff7 8116 -- In ASIS mode, even if assertions are not enabled, we must
8117 -- analyze the original expression in the aspect specification
8118 -- because it is part of the original tree.
f02a9a9a 8119
f9e26ff7 8120 if ASIS_Mode and then Present (Asp) then
8121 declare
8122 Orig_Expr : constant Node_Id := Expression (Asp);
8123 begin
8124 Replace_Type_References (Orig_Expr, T);
8125 Preanalyze_Assert_Expression (Orig_Expr, Any_Boolean);
8126 end;
8127 end if;
d9f6a4ee 8128
f9e26ff7 8129 -- An ignored invariant must not generate a runtime check. Add a
8130 -- null statement to ensure that the invariant procedure does get
8131 -- a completing body.
d9f6a4ee 8132
f9e26ff7 8133 if No (Stmts) then
8134 Stmts := Empty_List;
8135 end if;
d9f6a4ee 8136
f9e26ff7 8137 if Is_Ignored (Prag) then
8138 Append_To (Stmts, Make_Null_Statement (Ploc));
d9f6a4ee 8139
f9e26ff7 8140 -- Otherwise the invariant is checked. Build a Check pragma to
8141 -- verify the expression at runtime.
d9f6a4ee 8142
f9e26ff7 8143 else
8144 Assoc := New_List (
8145 Make_Pragma_Argument_Association (Ploc,
8146 Expression => Make_Identifier (Ploc, Nam)),
8147 Make_Pragma_Argument_Association (Ploc,
8148 Expression => Expr));
d9f6a4ee 8149
f9e26ff7 8150 -- Handle the String argument (if any)
d9f6a4ee 8151
eb66e842 8152 if Present (Arg3) then
8153 Str := Strval (Get_Pragma_Arg (Arg3));
d9f6a4ee 8154
f9e26ff7 8155 -- When inheriting an invariant, modify the message from
8156 -- "failed invariant" to "failed inherited invariant".
d9f6a4ee 8157
eb66e842 8158 if Inherit then
8159 String_To_Name_Buffer (Str);
d9f6a4ee 8160
eb66e842 8161 if Name_Buffer (1 .. 16) = "failed invariant" then
8162 Insert_Str_In_Name_Buffer ("inherited ", 8);
8163 Str := String_From_Name_Buffer;
8164 end if;
8165 end if;
d9f6a4ee 8166
eb66e842 8167 Append_To (Assoc,
f9e26ff7 8168 Make_Pragma_Argument_Association (Ploc,
8169 Expression => Make_String_Literal (Ploc, Str)));
eb66e842 8170 end if;
d9f6a4ee 8171
f9e26ff7 8172 -- Generate:
8173 -- pragma Check (Nam, Expr, Str);
d97beb2f 8174
eb66e842 8175 Append_To (Stmts,
f9e26ff7 8176 Make_Pragma (Ploc,
eb66e842 8177 Pragma_Identifier =>
f9e26ff7 8178 Make_Identifier (Ploc, Name_Check),
eb66e842 8179 Pragma_Argument_Associations => Assoc));
f9e26ff7 8180 end if;
d97beb2f 8181
f9e26ff7 8182 -- Output an info message when inheriting an invariant and the
8183 -- listing option is enabled.
d97beb2f 8184
f9e26ff7 8185 if Inherit and Opt.List_Inherited_Aspects then
8186 Error_Msg_Sloc := Sloc (Prag);
8187 Error_Msg_N
8188 ("info: & inherits `Invariant''Class` aspect from #?L?", Typ);
8189 end if;
8190 end Add_Invariant;
8191
8192 -- Local variables
8193
8194 Ritem : Node_Id;
8195
8196 -- Start of processing for Add_Invariants
8197
8198 begin
8199 Ritem := First_Rep_Item (T);
8200 while Present (Ritem) loop
8201 if Nkind (Ritem) = N_Pragma
8202 and then Pragma_Name (Ritem) = Name_Invariant
8203 then
8204 Add_Invariant (Ritem);
d9f6a4ee 8205 end if;
d97beb2f 8206
eb66e842 8207 Next_Rep_Item (Ritem);
8208 end loop;
8209 end Add_Invariants;
d97beb2f 8210
39629e6a 8211 -- Local variables
8212
8213 Loc : constant Source_Ptr := Sloc (Typ);
8214 Priv_Decls : constant List_Id := Private_Declarations (N);
8215 Vis_Decls : constant List_Id := Visible_Declarations (N);
8216
8217 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
8218
8219 PBody : Node_Id;
8220 PDecl : Node_Id;
8221 SId : Entity_Id;
8222 Spec : Node_Id;
8223 Stmts : List_Id;
8224
8225 Obj_Id : Node_Id;
8226 -- The entity of the formal for the procedure
8227
eb66e842 8228 -- Start of processing for Build_Invariant_Procedure
d97beb2f 8229
eb66e842 8230 begin
39629e6a 8231 -- The related type may be subject to pragma Ghost. Set the mode now to
8232 -- ensure that the invariant procedure is properly marked as Ghost.
8233
8234 Set_Ghost_Mode_From_Entity (Typ);
8235
eb66e842 8236 Stmts := No_List;
8237 PDecl := Empty;
8238 PBody := Empty;
8239 SId := Empty;
d97beb2f 8240
eb66e842 8241 -- If the aspect specification exists for some view of the type, the
8242 -- declaration for the procedure has been created.
d97beb2f 8243
eb66e842 8244 if Has_Invariants (Typ) then
8245 SId := Invariant_Procedure (Typ);
8246 end if;
9dc88aea 8247
0c2bde47 8248 -- If the body is already present, nothing to do. This will occur when
8249 -- the type is already frozen, which is the case when the invariant
8250 -- appears in a private part, and the freezing takes place before the
8251 -- final pass over full declarations.
aba11c12 8252
8253 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
0c2bde47 8254
eb66e842 8255 if Present (SId) then
8256 PDecl := Unit_Declaration_Node (SId);
0c2bde47 8257
8258 if Present (PDecl)
8259 and then Nkind (PDecl) = N_Subprogram_Declaration
8260 and then Present (Corresponding_Body (PDecl))
8261 then
39629e6a 8262 Ghost_Mode := Save_Ghost_Mode;
0c2bde47 8263 return;
8264 end if;
8265
eb66e842 8266 else
8267 PDecl := Build_Invariant_Procedure_Declaration (Typ);
8268 end if;
9dc88aea 8269
eb66e842 8270 -- Recover formal of procedure, for use in the calls to invariant
8271 -- functions (including inherited ones).
d9f6a4ee 8272
39629e6a 8273 Obj_Id :=
eb66e842 8274 Defining_Identifier
8275 (First (Parameter_Specifications (Specification (PDecl))));
d9f6a4ee 8276
eb66e842 8277 -- Add invariants for the current type
9dc88aea 8278
39629e6a 8279 Add_Invariants
8280 (T => Typ,
8281 Obj_Id => Obj_Id,
8282 Stmts => Stmts,
8283 Inherit => False);
9dc88aea 8284
eb66e842 8285 -- Add invariants for parent types
9dc88aea 8286
eb66e842 8287 declare
8288 Current_Typ : Entity_Id;
8289 Parent_Typ : Entity_Id;
9dc88aea 8290
eb66e842 8291 begin
8292 Current_Typ := Typ;
d97beb2f 8293 loop
eb66e842 8294 Parent_Typ := Etype (Current_Typ);
9dc88aea 8295
eb66e842 8296 if Is_Private_Type (Parent_Typ)
8297 and then Present (Full_View (Base_Type (Parent_Typ)))
d9f6a4ee 8298 then
eb66e842 8299 Parent_Typ := Full_View (Base_Type (Parent_Typ));
8300 end if;
9dc88aea 8301
eb66e842 8302 exit when Parent_Typ = Current_Typ;
9dc88aea 8303
eb66e842 8304 Current_Typ := Parent_Typ;
39629e6a 8305 Add_Invariants
8306 (T => Current_Typ,
8307 Obj_Id => Obj_Id,
8308 Stmts => Stmts,
8309 Inherit => True);
eb66e842 8310 end loop;
8311 end;
ad274a73 8312
8313 -- Add invariants of progenitors
8314
8315 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
8316 declare
8317 Ifaces_List : Elist_Id;
8318 AI : Elmt_Id;
8319 Iface : Entity_Id;
8320
8321 begin
8322 Collect_Interfaces (Typ, Ifaces_List);
8323
8324 AI := First_Elmt (Ifaces_List);
8325 while Present (AI) loop
8326 Iface := Node (AI);
8327
8328 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
39629e6a 8329 Add_Invariants
8330 (T => Iface,
8331 Obj_Id => Obj_Id,
8332 Stmts => Stmts,
8333 Inherit => True);
ad274a73 8334 end if;
8335
8336 Next_Elmt (AI);
8337 end loop;
8338 end;
8339 end if;
9dc88aea 8340
eb66e842 8341 -- Build the procedure if we generated at least one Check pragma
9dc88aea 8342
eb66e842 8343 if Stmts /= No_List then
39629e6a 8344 Spec := Copy_Separate_Tree (Specification (PDecl));
9dc88aea 8345
eb66e842 8346 PBody :=
8347 Make_Subprogram_Body (Loc,
8348 Specification => Spec,
8349 Declarations => Empty_List,
8350 Handled_Statement_Sequence =>
8351 Make_Handled_Sequence_Of_Statements (Loc,
8352 Statements => Stmts));
9dc88aea 8353
eb66e842 8354 -- Insert procedure declaration and spec at the appropriate points.
8355 -- If declaration is already analyzed, it was processed by the
8356 -- generated pragma.
9dc88aea 8357
f9e26ff7 8358 if Present (Priv_Decls) then
d97beb2f 8359
eb66e842 8360 -- The spec goes at the end of visible declarations, but they have
8361 -- already been analyzed, so we need to explicitly do the analyze.
d9f6a4ee 8362
eb66e842 8363 if not Analyzed (PDecl) then
f9e26ff7 8364 Append_To (Vis_Decls, PDecl);
eb66e842 8365 Analyze (PDecl);
8366 end if;
d9f6a4ee 8367
eb66e842 8368 -- The body goes at the end of the private declarations, which we
8369 -- have not analyzed yet, so we do not need to perform an explicit
8370 -- analyze call. We skip this if there are no private declarations
8371 -- (this is an error that will be caught elsewhere);
d9f6a4ee 8372
f9e26ff7 8373 Append_To (Priv_Decls, PBody);
d9f6a4ee 8374
eb66e842 8375 -- If the invariant appears on the full view of a type, the
8376 -- analysis of the private part is complete, and we must
8377 -- analyze the new body explicitly.
d9f6a4ee 8378
eb66e842 8379 if In_Private_Part (Current_Scope) then
8380 Analyze (PBody);
8381 end if;
d97beb2f 8382
eb66e842 8383 -- If there are no private declarations this may be an error that
8384 -- will be diagnosed elsewhere. However, if this is a non-private
8385 -- type that inherits invariants, it needs no completion and there
8386 -- may be no private part. In this case insert invariant procedure
8387 -- at end of current declarative list, and analyze at once, given
8388 -- that the type is about to be frozen.
d97beb2f 8389
eb66e842 8390 elsif not Is_Private_Type (Typ) then
f9e26ff7 8391 Append_To (Vis_Decls, PDecl);
8392 Append_To (Vis_Decls, PBody);
eb66e842 8393 Analyze (PDecl);
8394 Analyze (PBody);
8395 end if;
8396 end if;
39629e6a 8397
8398 Ghost_Mode := Save_Ghost_Mode;
eb66e842 8399 end Build_Invariant_Procedure;
d9f6a4ee 8400
eb66e842 8401 -------------------------------
8402 -- Build_Predicate_Functions --
8403 -------------------------------
d9f6a4ee 8404
eb66e842 8405 -- The procedures that are constructed here have the form:
d9f6a4ee 8406
eb66e842 8407 -- function typPredicate (Ixxx : typ) return Boolean is
8408 -- begin
8409 -- return
8410 -- exp1 and then exp2 and then ...
8411 -- and then typ1Predicate (typ1 (Ixxx))
8412 -- and then typ2Predicate (typ2 (Ixxx))
8413 -- and then ...;
8414 -- end typPredicate;
d9f6a4ee 8415
eb66e842 8416 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8417 -- this is the point at which these expressions get analyzed, providing the
8418 -- required delay, and typ1, typ2, are entities from which predicates are
8419 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8420 -- use this function even if checks are off, e.g. for membership tests.
d9f6a4ee 8421
eb66e842 8422 -- If the expression has at least one Raise_Expression, then we also build
8423 -- the typPredicateM version of the function, in which any occurrence of a
8424 -- Raise_Expression is converted to "return False".
d9f6a4ee 8425
eb66e842 8426 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8427 Loc : constant Source_Ptr := Sloc (Typ);
d9f6a4ee 8428
eb66e842 8429 Expr : Node_Id;
8430 -- This is the expression for the result of the function. It is
8431 -- is build by connecting the component predicates with AND THEN.
d9f6a4ee 8432
eb66e842 8433 Expr_M : Node_Id;
8434 -- This is the corresponding return expression for the Predicate_M
8435 -- function. It differs in that raise expressions are marked for
8436 -- special expansion (see Process_REs).
d9f6a4ee 8437
eb66e842 8438 Object_Name : constant Name_Id := New_Internal_Name ('I');
8439 -- Name for argument of Predicate procedure. Note that we use the same
499918a7 8440 -- name for both predicate functions. That way the reference within the
eb66e842 8441 -- predicate expression is the same in both functions.
d9f6a4ee 8442
eb66e842 8443 Object_Entity : constant Entity_Id :=
8444 Make_Defining_Identifier (Loc, Chars => Object_Name);
8445 -- Entity for argument of Predicate procedure
d9f6a4ee 8446
eb66e842 8447 Object_Entity_M : constant Entity_Id :=
8448 Make_Defining_Identifier (Loc, Chars => Object_Name);
8449 -- Entity for argument of Predicate_M procedure
d9f6a4ee 8450
eb66e842 8451 Raise_Expression_Present : Boolean := False;
8452 -- Set True if Expr has at least one Raise_Expression
d9f6a4ee 8453
eb66e842 8454 procedure Add_Call (T : Entity_Id);
8455 -- Includes a call to the predicate function for type T in Expr if T
8456 -- has predicates and Predicate_Function (T) is non-empty.
d9f6a4ee 8457
eb66e842 8458 procedure Add_Predicates;
8459 -- Appends expressions for any Predicate pragmas in the rep item chain
8460 -- Typ to Expr. Note that we look only at items for this exact entity.
8461 -- Inheritance of predicates for the parent type is done by calling the
8462 -- Predicate_Function of the parent type, using Add_Call above.
d9f6a4ee 8463
eb66e842 8464 function Process_RE (N : Node_Id) return Traverse_Result;
8465 -- Used in Process REs, tests if node N is a raise expression, and if
8466 -- so, marks it to be converted to return False.
d9f6a4ee 8467
eb66e842 8468 procedure Process_REs is new Traverse_Proc (Process_RE);
8469 -- Marks any raise expressions in Expr_M to return False
d9f6a4ee 8470
f9e26ff7 8471 function Test_RE (N : Node_Id) return Traverse_Result;
8472 -- Used in Test_REs, tests one node for being a raise expression, and if
8473 -- so sets Raise_Expression_Present True.
8474
8475 procedure Test_REs is new Traverse_Proc (Test_RE);
8476 -- Tests to see if Expr contains any raise expressions
8477
eb66e842 8478 --------------
8479 -- Add_Call --
8480 --------------
d9f6a4ee 8481
eb66e842 8482 procedure Add_Call (T : Entity_Id) is
8483 Exp : Node_Id;
d9f6a4ee 8484
eb66e842 8485 begin
8486 if Present (T) and then Present (Predicate_Function (T)) then
8487 Set_Has_Predicates (Typ);
d9f6a4ee 8488
eb66e842 8489 -- Build the call to the predicate function of T
d9f6a4ee 8490
eb66e842 8491 Exp :=
8492 Make_Predicate_Call
8493 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
d9f6a4ee 8494
eb66e842 8495 -- Add call to evolving expression, using AND THEN if needed
d9f6a4ee 8496
eb66e842 8497 if No (Expr) then
8498 Expr := Exp;
3b23aaa0 8499
eb66e842 8500 else
8501 Expr :=
3b23aaa0 8502 Make_And_Then (Sloc (Expr),
eb66e842 8503 Left_Opnd => Relocate_Node (Expr),
8504 Right_Opnd => Exp);
8505 end if;
d9f6a4ee 8506
eb66e842 8507 -- Output info message on inheritance if required. Note we do not
8508 -- give this information for generic actual types, since it is
8509 -- unwelcome noise in that case in instantiations. We also
8510 -- generally suppress the message in instantiations, and also
8511 -- if it involves internal names.
d9f6a4ee 8512
eb66e842 8513 if Opt.List_Inherited_Aspects
8514 and then not Is_Generic_Actual_Type (Typ)
8515 and then Instantiation_Depth (Sloc (Typ)) = 0
8516 and then not Is_Internal_Name (Chars (T))
8517 and then not Is_Internal_Name (Chars (Typ))
8518 then
8519 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8520 Error_Msg_Node_2 := T;
8521 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8522 end if;
8523 end if;
8524 end Add_Call;
d9f6a4ee 8525
eb66e842 8526 --------------------
8527 -- Add_Predicates --
8528 --------------------
d9f6a4ee 8529
eb66e842 8530 procedure Add_Predicates is
f9e26ff7 8531 procedure Add_Predicate (Prag : Node_Id);
8532 -- Concatenate the expression of predicate pragma Prag to Expr by
8533 -- using a short circuit "and then" operator.
d9f6a4ee 8534
f9e26ff7 8535 -------------------
8536 -- Add_Predicate --
8537 -------------------
d9f6a4ee 8538
f9e26ff7 8539 procedure Add_Predicate (Prag : Node_Id) is
8540 procedure Replace_Type_Reference (N : Node_Id);
8541 -- Replace a single occurrence N of the subtype name with a
8542 -- reference to the formal of the predicate function. N can be an
8543 -- identifier referencing the subtype, or a selected component,
8544 -- representing an appropriately qualified occurrence of the
8545 -- subtype name.
8546
8547 procedure Replace_Type_References is
8548 new Replace_Type_References_Generic (Replace_Type_Reference);
8549 -- Traverse an expression changing every occurrence of an
8550 -- identifier whose name matches the name of the subtype with a
8551 -- reference to the formal parameter of the predicate function.
8552
8553 ----------------------------
8554 -- Replace_Type_Reference --
8555 ----------------------------
8556
8557 procedure Replace_Type_Reference (N : Node_Id) is
8558 begin
8559 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8560 -- Use the Sloc of the usage name, not the defining name
d9f6a4ee 8561
f9e26ff7 8562 Set_Etype (N, Typ);
8563 Set_Entity (N, Object_Entity);
d97beb2f 8564
f9e26ff7 8565 -- We want to treat the node as if it comes from source, so
8566 -- that ASIS will not ignore it.
d97beb2f 8567
f9e26ff7 8568 Set_Comes_From_Source (N, True);
8569 end Replace_Type_Reference;
d97beb2f 8570
f9e26ff7 8571 -- Local variables
d97beb2f 8572
f9e26ff7 8573 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8574 Arg1 : Node_Id;
8575 Arg2 : Node_Id;
d97beb2f 8576
f9e26ff7 8577 -- Start of processing for Add_Predicate
24c8d764 8578
f9e26ff7 8579 begin
8580 -- Extract the arguments of the pragma. The expression itself
8581 -- is copied for use in the predicate function, to preserve the
8582 -- original version for ASIS use.
d97beb2f 8583
f9e26ff7 8584 Arg1 := First (Pragma_Argument_Associations (Prag));
8585 Arg2 := Next (Arg1);
d97beb2f 8586
f9e26ff7 8587 Arg1 := Get_Pragma_Arg (Arg1);
8588 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
d97beb2f 8589
f9e26ff7 8590 -- When the predicate pragma applies to the current type or its
8591 -- full view, replace all occurrences of the subtype name with
8592 -- references to the formal parameter of the predicate function.
639c3741 8593
f9e26ff7 8594 if Entity (Arg1) = Typ
8595 or else Full_View (Entity (Arg1)) = Typ
8596 then
8597 Replace_Type_References (Arg2, Typ);
639c3741 8598
f9e26ff7 8599 -- If the predicate pragma comes from an aspect, replace the
8600 -- saved expression because we need the subtype references
8601 -- replaced for the calls to Preanalyze_Spec_Expression in
8602 -- Check_Aspect_At_xxx routines.
639c3741 8603
f9e26ff7 8604 if Present (Asp) then
639c3741 8605
f9e26ff7 8606 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8607 end if;
24c8d764 8608
f9e26ff7 8609 -- Concatenate to the existing predicate expressions by using
8610 -- "and then".
24c8d764 8611
f9e26ff7 8612 if Present (Expr) then
8613 Expr :=
8614 Make_And_Then (Loc,
8615 Left_Opnd => Relocate_Node (Expr),
8616 Right_Opnd => Relocate_Node (Arg2));
639c3741 8617
f9e26ff7 8618 -- Otherwise this is the first predicate expression
639c3741 8619
f9e26ff7 8620 else
8621 Expr := Relocate_Node (Arg2);
8622 end if;
8623 end if;
8624 end Add_Predicate;
737e8460 8625
f9e26ff7 8626 -- Local variables
737e8460 8627
f9e26ff7 8628 Ritem : Node_Id;
d97beb2f 8629
f9e26ff7 8630 -- Start of processing for Add_Predicates
d97beb2f 8631
f9e26ff7 8632 begin
8633 Ritem := First_Rep_Item (Typ);
8634 while Present (Ritem) loop
8635 if Nkind (Ritem) = N_Pragma
8636 and then Pragma_Name (Ritem) = Name_Predicate
8637 then
8638 Add_Predicate (Ritem);
eb66e842 8639 end if;
d97beb2f 8640
eb66e842 8641 Next_Rep_Item (Ritem);
8642 end loop;
8643 end Add_Predicates;
d97beb2f 8644
eb66e842 8645 ----------------
8646 -- Process_RE --
8647 ----------------
d97beb2f 8648
eb66e842 8649 function Process_RE (N : Node_Id) return Traverse_Result is
d9f6a4ee 8650 begin
eb66e842 8651 if Nkind (N) = N_Raise_Expression then
8652 Set_Convert_To_Return_False (N);
8653 return Skip;
d9f6a4ee 8654 else
eb66e842 8655 return OK;
d9f6a4ee 8656 end if;
eb66e842 8657 end Process_RE;
d7c2851f 8658
d9f6a4ee 8659 -------------
eb66e842 8660 -- Test_RE --
d9f6a4ee 8661 -------------
d7c2851f 8662
eb66e842 8663 function Test_RE (N : Node_Id) return Traverse_Result is
d97beb2f 8664 begin
eb66e842 8665 if Nkind (N) = N_Raise_Expression then
8666 Raise_Expression_Present := True;
8667 return Abandon;
8668 else
8669 return OK;
8670 end if;
8671 end Test_RE;
d97beb2f 8672
f9e26ff7 8673 -- Local variables
8674
30f8d103 8675 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
f9e26ff7 8676
eb66e842 8677 -- Start of processing for Build_Predicate_Functions
d97beb2f 8678
eb66e842 8679 begin
8680 -- Return if already built or if type does not have predicates
9dc88aea 8681
eb66e842 8682 if not Has_Predicates (Typ)
8683 or else Present (Predicate_Function (Typ))
8684 then
8685 return;
8686 end if;
d9f6a4ee 8687
30f8d103 8688 -- The related type may be subject to pragma Ghost. Set the mode now to
8689 -- ensure that the predicate functions are properly marked as Ghost.
f9e26ff7 8690
8691 Set_Ghost_Mode_From_Entity (Typ);
8692
eb66e842 8693 -- Prepare to construct predicate expression
d97beb2f 8694
eb66e842 8695 Expr := Empty;
d97beb2f 8696
eb66e842 8697 -- Add Predicates for the current type
d97beb2f 8698
eb66e842 8699 Add_Predicates;
d97beb2f 8700
eb66e842 8701 -- Add predicates for ancestor if present
d97beb2f 8702
eb66e842 8703 declare
8704 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
d9f6a4ee 8705 begin
eb66e842 8706 if Present (Atyp) then
8707 Add_Call (Atyp);
8708 end if;
8709 end;
9dc88aea 8710
eb66e842 8711 -- Case where predicates are present
9dc88aea 8712
eb66e842 8713 if Present (Expr) then
726fd56a 8714
eb66e842 8715 -- Test for raise expression present
726fd56a 8716
eb66e842 8717 Test_REs (Expr);
9dc88aea 8718
eb66e842 8719 -- If raise expression is present, capture a copy of Expr for use
8720 -- in building the predicateM function version later on. For this
8721 -- copy we replace references to Object_Entity by Object_Entity_M.
9dc88aea 8722
eb66e842 8723 if Raise_Expression_Present then
8724 declare
299b347e 8725 Map : constant Elist_Id := New_Elmt_List;
8726 New_V : Entity_Id := Empty;
8727
8728 -- The unanalyzed expression will be copied and appear in
8729 -- both functions. Normally expressions do not declare new
8730 -- entities, but quantified expressions do, so we need to
8731 -- create new entities for their bound variables, to prevent
8732 -- multiple definitions in gigi.
8733
8734 function Reset_Loop_Variable (N : Node_Id)
8735 return Traverse_Result;
8736
8737 procedure Collect_Loop_Variables is
8738 new Traverse_Proc (Reset_Loop_Variable);
8739
8740 ------------------------
8741 -- Reset_Loop_Variable --
8742 ------------------------
8743
8744 function Reset_Loop_Variable (N : Node_Id)
8745 return Traverse_Result
8746 is
8747 begin
8748 if Nkind (N) = N_Iterator_Specification then
8749 New_V := Make_Defining_Identifier
8750 (Sloc (N), Chars (Defining_Identifier (N)));
8751
8752 Set_Defining_Identifier (N, New_V);
8753 end if;
8754
8755 return OK;
8756 end Reset_Loop_Variable;
8757
eb66e842 8758 begin
8759 Append_Elmt (Object_Entity, Map);
8760 Append_Elmt (Object_Entity_M, Map);
8761 Expr_M := New_Copy_Tree (Expr, Map => Map);
299b347e 8762 Collect_Loop_Variables (Expr_M);
eb66e842 8763 end;
8764 end if;
d97beb2f 8765
eb66e842 8766 -- Build the main predicate function
9dc88aea 8767
eb66e842 8768 declare
8769 SId : constant Entity_Id :=
8770 Make_Defining_Identifier (Loc,
8771 Chars => New_External_Name (Chars (Typ), "Predicate"));
c96806b2 8772 -- The entity for the function spec
9dc88aea 8773
eb66e842 8774 SIdB : constant Entity_Id :=
8775 Make_Defining_Identifier (Loc,
8776 Chars => New_External_Name (Chars (Typ), "Predicate"));
8777 -- The entity for the function body
9dc88aea 8778
eb66e842 8779 Spec : Node_Id;
8780 FDecl : Node_Id;
8781 FBody : Node_Id;
9dc88aea 8782
eb66e842 8783 begin
8784 -- Build function declaration
d97beb2f 8785
eb66e842 8786 Set_Ekind (SId, E_Function);
8787 Set_Is_Internal (SId);
8788 Set_Is_Predicate_Function (SId);
8789 Set_Predicate_Function (Typ, SId);
d97beb2f 8790
eb66e842 8791 -- The predicate function is shared between views of a type
d97beb2f 8792
eb66e842 8793 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8794 Set_Predicate_Function (Full_View (Typ), SId);
d97beb2f 8795 end if;
d97beb2f 8796
f9e26ff7 8797 -- Mark the predicate function explicitly as Ghost because it does
8798 -- not come from source.
8799
8800 if Ghost_Mode > None then
8801 Set_Is_Ghost_Entity (SId);
8802 end if;
8803
eb66e842 8804 Spec :=
8805 Make_Function_Specification (Loc,
8806 Defining_Unit_Name => SId,
8807 Parameter_Specifications => New_List (
8808 Make_Parameter_Specification (Loc,
8809 Defining_Identifier => Object_Entity,
8810 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8811 Result_Definition =>
8812 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8813
eb66e842 8814 FDecl :=
8815 Make_Subprogram_Declaration (Loc,
8816 Specification => Spec);
d97beb2f 8817
eb66e842 8818 -- Build function body
d97beb2f 8819
eb66e842 8820 Spec :=
8821 Make_Function_Specification (Loc,
8822 Defining_Unit_Name => SIdB,
8823 Parameter_Specifications => New_List (
8824 Make_Parameter_Specification (Loc,
8825 Defining_Identifier =>
8826 Make_Defining_Identifier (Loc, Object_Name),
8827 Parameter_Type =>
8828 New_Occurrence_Of (Typ, Loc))),
8829 Result_Definition =>
8830 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8831
eb66e842 8832 FBody :=
8833 Make_Subprogram_Body (Loc,
8834 Specification => Spec,
8835 Declarations => Empty_List,
8836 Handled_Statement_Sequence =>
8837 Make_Handled_Sequence_Of_Statements (Loc,
8838 Statements => New_List (
8839 Make_Simple_Return_Statement (Loc,
8840 Expression => Expr))));
9dc88aea 8841
eb66e842 8842 -- Insert declaration before freeze node and body after
d97beb2f 8843
eb66e842 8844 Insert_Before_And_Analyze (N, FDecl);
8845 Insert_After_And_Analyze (N, FBody);
6958c62c 8846
8847 -- Static predicate functions are always side-effect free, and
8848 -- in most cases dynamic predicate functions are as well. Mark
8849 -- them as such whenever possible, so redundant predicate checks
7dd0b9b3 8850 -- can be optimized. If there is a variable reference within the
8851 -- expression, the function is not pure.
b2e821de 8852
6958c62c 8853 if Expander_Active then
7dd0b9b3 8854 Set_Is_Pure (SId,
8855 Side_Effect_Free (Expr, Variable_Ref => True));
6958c62c 8856 Set_Is_Inlined (SId);
8857 end if;
d9f6a4ee 8858 end;
d97beb2f 8859
eb66e842 8860 -- Test for raise expressions present and if so build M version
d97beb2f 8861
eb66e842 8862 if Raise_Expression_Present then
8863 declare
8864 SId : constant Entity_Id :=
8865 Make_Defining_Identifier (Loc,
8866 Chars => New_External_Name (Chars (Typ), "PredicateM"));
c96806b2 8867 -- The entity for the function spec
d97beb2f 8868
eb66e842 8869 SIdB : constant Entity_Id :=
8870 Make_Defining_Identifier (Loc,
8871 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8872 -- The entity for the function body
b9e61b2a 8873
eb66e842 8874 Spec : Node_Id;
8875 FDecl : Node_Id;
8876 FBody : Node_Id;
8877 BTemp : Entity_Id;
d97beb2f 8878
eb66e842 8879 begin
8880 -- Mark any raise expressions for special expansion
d97beb2f 8881
eb66e842 8882 Process_REs (Expr_M);
d97beb2f 8883
eb66e842 8884 -- Build function declaration
d97beb2f 8885
eb66e842 8886 Set_Ekind (SId, E_Function);
8887 Set_Is_Predicate_Function_M (SId);
8888 Set_Predicate_Function_M (Typ, SId);
d97beb2f 8889
eb66e842 8890 -- The predicate function is shared between views of a type
d97beb2f 8891
eb66e842 8892 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8893 Set_Predicate_Function_M (Full_View (Typ), SId);
8894 end if;
9dc88aea 8895
f9e26ff7 8896 -- Mark the predicate function explicitly as Ghost because it
8897 -- does not come from source.
8898
8899 if Ghost_Mode > None then
8900 Set_Is_Ghost_Entity (SId);
8901 end if;
8902
eb66e842 8903 Spec :=
8904 Make_Function_Specification (Loc,
8905 Defining_Unit_Name => SId,
8906 Parameter_Specifications => New_List (
8907 Make_Parameter_Specification (Loc,
8908 Defining_Identifier => Object_Entity_M,
8909 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8910 Result_Definition =>
8911 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8912
eb66e842 8913 FDecl :=
8914 Make_Subprogram_Declaration (Loc,
8915 Specification => Spec);
9dc88aea 8916
eb66e842 8917 -- Build function body
9dc88aea 8918
eb66e842 8919 Spec :=
8920 Make_Function_Specification (Loc,
8921 Defining_Unit_Name => SIdB,
8922 Parameter_Specifications => New_List (
8923 Make_Parameter_Specification (Loc,
8924 Defining_Identifier =>
8925 Make_Defining_Identifier (Loc, Object_Name),
8926 Parameter_Type =>
8927 New_Occurrence_Of (Typ, Loc))),
8928 Result_Definition =>
8929 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8930
eb66e842 8931 -- Build the body, we declare the boolean expression before
8932 -- doing the return, because we are not really confident of
8933 -- what happens if a return appears within a return.
9dc88aea 8934
eb66e842 8935 BTemp :=
8936 Make_Defining_Identifier (Loc,
8937 Chars => New_Internal_Name ('B'));
9dc88aea 8938
eb66e842 8939 FBody :=
8940 Make_Subprogram_Body (Loc,
8941 Specification => Spec,
9dc88aea 8942
eb66e842 8943 Declarations => New_List (
8944 Make_Object_Declaration (Loc,
8945 Defining_Identifier => BTemp,
8946 Constant_Present => True,
8947 Object_Definition =>
8948 New_Occurrence_Of (Standard_Boolean, Loc),
8949 Expression => Expr_M)),
d97beb2f 8950
eb66e842 8951 Handled_Statement_Sequence =>
8952 Make_Handled_Sequence_Of_Statements (Loc,
8953 Statements => New_List (
8954 Make_Simple_Return_Statement (Loc,
8955 Expression => New_Occurrence_Of (BTemp, Loc)))));
d97beb2f 8956
eb66e842 8957 -- Insert declaration before freeze node and body after
d97beb2f 8958
eb66e842 8959 Insert_Before_And_Analyze (N, FDecl);
8960 Insert_After_And_Analyze (N, FBody);
8961 end;
8962 end if;
9dc88aea 8963
3b23aaa0 8964 -- See if we have a static predicate. Note that the answer may be
8965 -- yes even if we have an explicit Dynamic_Predicate present.
9dc88aea 8966
3b23aaa0 8967 declare
94d896aa 8968 PS : Boolean;
3b23aaa0 8969 EN : Node_Id;
9dc88aea 8970
3b23aaa0 8971 begin
94d896aa 8972 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8973 PS := False;
8974 else
8975 PS := Is_Predicate_Static (Expr, Object_Name);
8976 end if;
8977
a360a0f7 8978 -- Case where we have a predicate-static aspect
9dc88aea 8979
3b23aaa0 8980 if PS then
9dc88aea 8981
3b23aaa0 8982 -- We don't set Has_Static_Predicate_Aspect, since we can have
8983 -- any of the three cases (Predicate, Dynamic_Predicate, or
8984 -- Static_Predicate) generating a predicate with an expression
a360a0f7 8985 -- that is predicate-static. We just indicate that we have a
3b23aaa0 8986 -- predicate that can be treated as static.
d7c2851f 8987
3b23aaa0 8988 Set_Has_Static_Predicate (Typ);
d7c2851f 8989
3b23aaa0 8990 -- For discrete subtype, build the static predicate list
9dc88aea 8991
3b23aaa0 8992 if Is_Discrete_Type (Typ) then
8993 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8994
8995 -- If we don't get a static predicate list, it means that we
8996 -- have a case where this is not possible, most typically in
8997 -- the case where we inherit a dynamic predicate. We do not
8998 -- consider this an error, we just leave the predicate as
8999 -- dynamic. But if we do succeed in building the list, then
9000 -- we mark the predicate as static.
9001
5c6a5792 9002 if No (Static_Discrete_Predicate (Typ)) then
3b23aaa0 9003 Set_Has_Static_Predicate (Typ, False);
9004 end if;
94d896aa 9005
9006 -- For real or string subtype, save predicate expression
9007
9008 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
9009 Set_Static_Real_Or_String_Predicate (Typ, Expr);
3b23aaa0 9010 end if;
9011
9012 -- Case of dynamic predicate (expression is not predicate-static)
9dc88aea 9013
eb66e842 9014 else
3b23aaa0 9015 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
9016 -- is only set if we have an explicit Dynamic_Predicate aspect
9017 -- given. Here we may simply have a Predicate aspect where the
9018 -- expression happens not to be predicate-static.
9019
9020 -- Emit an error when the predicate is categorized as static
9021 -- but its expression is not predicate-static.
9022
9023 -- First a little fiddling to get a nice location for the
9024 -- message. If the expression is of the form (A and then B),
9025 -- then use the left operand for the Sloc. This avoids getting
a360a0f7 9026 -- confused by a call to a higher-level predicate with a less
3b23aaa0 9027 -- convenient source location.
9028
9029 EN := Expr;
9030 while Nkind (EN) = N_And_Then loop
9031 EN := Left_Opnd (EN);
9032 end loop;
9033
9034 -- Now post appropriate message
9035
9036 if Has_Static_Predicate_Aspect (Typ) then
94d896aa 9037 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
3b23aaa0 9038 Error_Msg_F
26279d91 9039 ("expression is not predicate-static (RM 3.2.4(16-22))",
3b23aaa0 9040 EN);
9041 else
94d896aa 9042 Error_Msg_F
9043 ("static predicate requires scalar or string type", EN);
3b23aaa0 9044 end if;
9045 end if;
eb66e842 9046 end if;
3b23aaa0 9047 end;
eb66e842 9048 end if;
f9e26ff7 9049
30f8d103 9050 Ghost_Mode := Save_Ghost_Mode;
eb66e842 9051 end Build_Predicate_Functions;
9dc88aea 9052
d9f6a4ee 9053 -----------------------------------------
9054 -- Check_Aspect_At_End_Of_Declarations --
9055 -----------------------------------------
9dc88aea 9056
d9f6a4ee 9057 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
9058 Ent : constant Entity_Id := Entity (ASN);
9059 Ident : constant Node_Id := Identifier (ASN);
9060 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
d7c2851f 9061
d9f6a4ee 9062 End_Decl_Expr : constant Node_Id := Entity (Ident);
9063 -- Expression to be analyzed at end of declarations
d7c2851f 9064
d9f6a4ee 9065 Freeze_Expr : constant Node_Id := Expression (ASN);
9066 -- Expression from call to Check_Aspect_At_Freeze_Point
d7c2851f 9067
d9f6a4ee 9068 T : constant Entity_Id := Etype (Freeze_Expr);
9069 -- Type required for preanalyze call
d7c2851f 9070
d9f6a4ee 9071 Err : Boolean;
9072 -- Set False if error
9dc88aea 9073
d9f6a4ee 9074 -- On entry to this procedure, Entity (Ident) contains a copy of the
9075 -- original expression from the aspect, saved for this purpose, and
9076 -- but Expression (Ident) is a preanalyzed copy of the expression,
9077 -- preanalyzed just after the freeze point.
9dc88aea 9078
d9f6a4ee 9079 procedure Check_Overloaded_Name;
9080 -- For aspects whose expression is simply a name, this routine checks if
9081 -- the name is overloaded or not. If so, it verifies there is an
9082 -- interpretation that matches the entity obtained at the freeze point,
9083 -- otherwise the compiler complains.
9dc88aea 9084
d9f6a4ee 9085 ---------------------------
9086 -- Check_Overloaded_Name --
9087 ---------------------------
9088
9089 procedure Check_Overloaded_Name is
d97beb2f 9090 begin
d9f6a4ee 9091 if not Is_Overloaded (End_Decl_Expr) then
5ac76cee 9092 Err := not Is_Entity_Name (End_Decl_Expr)
9093 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
d9f6a4ee 9094
d97beb2f 9095 else
d9f6a4ee 9096 Err := True;
9dc88aea 9097
d9f6a4ee 9098 declare
9099 Index : Interp_Index;
9100 It : Interp;
9dc88aea 9101
d9f6a4ee 9102 begin
9103 Get_First_Interp (End_Decl_Expr, Index, It);
9104 while Present (It.Typ) loop
9105 if It.Nam = Entity (Freeze_Expr) then
9106 Err := False;
9107 exit;
9108 end if;
9109
9110 Get_Next_Interp (Index, It);
9111 end loop;
9112 end;
9dc88aea 9113 end if;
d9f6a4ee 9114 end Check_Overloaded_Name;
9dc88aea 9115
d9f6a4ee 9116 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9dc88aea 9117
d9f6a4ee 9118 begin
da3cad01 9119 -- In an instance we do not perform the consistency check between freeze
9120 -- point and end of declarations, because it was done already in the
9121 -- analysis of the generic. Furthermore, the delayed analysis of an
9122 -- aspect of the instance may produce spurious errors when the generic
9123 -- is a child unit that references entities in the parent (which might
9124 -- not be in scope at the freeze point of the instance).
9125
9126 if In_Instance then
9127 return;
9128
d9f6a4ee 9129 -- Case of aspects Dimension, Dimension_System and Synchronization
9dc88aea 9130
da3cad01 9131 elsif A_Id = Aspect_Synchronization then
d9f6a4ee 9132 return;
d97beb2f 9133
d9f6a4ee 9134 -- Case of stream attributes, just have to compare entities. However,
9135 -- the expression is just a name (possibly overloaded), and there may
9136 -- be stream operations declared for unrelated types, so we just need
9137 -- to verify that one of these interpretations is the one available at
9138 -- at the freeze point.
9dc88aea 9139
d9f6a4ee 9140 elsif A_Id = Aspect_Input or else
f02a9a9a 9141 A_Id = Aspect_Output or else
9142 A_Id = Aspect_Read or else
9143 A_Id = Aspect_Write
d9f6a4ee 9144 then
9145 Analyze (End_Decl_Expr);
9146 Check_Overloaded_Name;
9dc88aea 9147
d9f6a4ee 9148 elsif A_Id = Aspect_Variable_Indexing or else
9149 A_Id = Aspect_Constant_Indexing or else
9150 A_Id = Aspect_Default_Iterator or else
9151 A_Id = Aspect_Iterator_Element
9152 then
9153 -- Make type unfrozen before analysis, to prevent spurious errors
9154 -- about late attributes.
9dc88aea 9155
d9f6a4ee 9156 Set_Is_Frozen (Ent, False);
9157 Analyze (End_Decl_Expr);
9158 Set_Is_Frozen (Ent, True);
9dc88aea 9159
d9f6a4ee 9160 -- If the end of declarations comes before any other freeze
9161 -- point, the Freeze_Expr is not analyzed: no check needed.
9dc88aea 9162
d9f6a4ee 9163 if Analyzed (Freeze_Expr) and then not In_Instance then
9164 Check_Overloaded_Name;
9165 else
9166 Err := False;
9167 end if;
55e8372b 9168
d9f6a4ee 9169 -- All other cases
55e8372b 9170
d9f6a4ee 9171 else
c1efebf9 9172 -- Indicate that the expression comes from an aspect specification,
9173 -- which is used in subsequent analysis even if expansion is off.
9174
9175 Set_Parent (End_Decl_Expr, ASN);
9176
d9f6a4ee 9177 -- In a generic context the aspect expressions have not been
9178 -- preanalyzed, so do it now. There are no conformance checks
9179 -- to perform in this case.
55e8372b 9180
d9f6a4ee 9181 if No (T) then
9182 Check_Aspect_At_Freeze_Point (ASN);
9183 return;
55e8372b 9184
d9f6a4ee 9185 -- The default values attributes may be defined in the private part,
9186 -- and the analysis of the expression may take place when only the
9187 -- partial view is visible. The expression must be scalar, so use
9188 -- the full view to resolve.
55e8372b 9189
d9f6a4ee 9190 elsif (A_Id = Aspect_Default_Value
9191 or else
9192 A_Id = Aspect_Default_Component_Value)
9193 and then Is_Private_Type (T)
9194 then
9195 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
c1efebf9 9196
d9f6a4ee 9197 else
9198 Preanalyze_Spec_Expression (End_Decl_Expr, T);
9199 end if;
d97beb2f 9200
d9f6a4ee 9201 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9202 end if;
55e8372b 9203
c1efebf9 9204 -- Output error message if error. Force error on aspect specification
9205 -- even if there is an error on the expression itself.
55e8372b 9206
d9f6a4ee 9207 if Err then
9208 Error_Msg_NE
c1efebf9 9209 ("!visibility of aspect for& changes after freeze point",
d9f6a4ee 9210 ASN, Ent);
9211 Error_Msg_NE
9212 ("info: & is frozen here, aspects evaluated at this point??",
9213 Freeze_Node (Ent), Ent);
9214 end if;
9215 end Check_Aspect_At_End_Of_Declarations;
55e8372b 9216
d9f6a4ee 9217 ----------------------------------
9218 -- Check_Aspect_At_Freeze_Point --
9219 ----------------------------------
9dc88aea 9220
d9f6a4ee 9221 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9222 Ident : constant Node_Id := Identifier (ASN);
9223 -- Identifier (use Entity field to save expression)
9dc88aea 9224
d9f6a4ee 9225 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9dc88aea 9226
d9f6a4ee 9227 T : Entity_Id := Empty;
9228 -- Type required for preanalyze call
9dc88aea 9229
d9f6a4ee 9230 begin
9231 -- On entry to this procedure, Entity (Ident) contains a copy of the
9232 -- original expression from the aspect, saved for this purpose.
9dc88aea 9233
d9f6a4ee 9234 -- On exit from this procedure Entity (Ident) is unchanged, still
9235 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9236 -- of the expression, preanalyzed just after the freeze point.
d97beb2f 9237
d9f6a4ee 9238 -- Make a copy of the expression to be preanalyzed
d97beb2f 9239
d9f6a4ee 9240 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
d97beb2f 9241
d9f6a4ee 9242 -- Find type for preanalyze call
d97beb2f 9243
d9f6a4ee 9244 case A_Id is
9dc88aea 9245
d9f6a4ee 9246 -- No_Aspect should be impossible
d97beb2f 9247
d9f6a4ee 9248 when No_Aspect =>
9249 raise Program_Error;
9250
9251 -- Aspects taking an optional boolean argument
d97beb2f 9252
d9f6a4ee 9253 when Boolean_Aspects |
9254 Library_Unit_Aspects =>
9dc88aea 9255
d9f6a4ee 9256 T := Standard_Boolean;
d7c2851f 9257
d9f6a4ee 9258 -- Aspects corresponding to attribute definition clauses
9dc88aea 9259
d9f6a4ee 9260 when Aspect_Address =>
9261 T := RTE (RE_Address);
9dc88aea 9262
d9f6a4ee 9263 when Aspect_Attach_Handler =>
9264 T := RTE (RE_Interrupt_ID);
d7c2851f 9265
d9f6a4ee 9266 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9267 T := RTE (RE_Bit_Order);
d7c2851f 9268
d9f6a4ee 9269 when Aspect_Convention =>
9270 return;
d7c2851f 9271
d9f6a4ee 9272 when Aspect_CPU =>
9273 T := RTE (RE_CPU_Range);
d7c2851f 9274
d9f6a4ee 9275 -- Default_Component_Value is resolved with the component type
d7c2851f 9276
d9f6a4ee 9277 when Aspect_Default_Component_Value =>
9278 T := Component_Type (Entity (ASN));
d7c2851f 9279
647fab54 9280 when Aspect_Default_Storage_Pool =>
9281 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9282
d9f6a4ee 9283 -- Default_Value is resolved with the type entity in question
d7c2851f 9284
d9f6a4ee 9285 when Aspect_Default_Value =>
9286 T := Entity (ASN);
9dc88aea 9287
d9f6a4ee 9288 when Aspect_Dispatching_Domain =>
9289 T := RTE (RE_Dispatching_Domain);
9dc88aea 9290
d9f6a4ee 9291 when Aspect_External_Tag =>
9292 T := Standard_String;
9dc88aea 9293
d9f6a4ee 9294 when Aspect_External_Name =>
9295 T := Standard_String;
9dc88aea 9296
d9f6a4ee 9297 when Aspect_Link_Name =>
9298 T := Standard_String;
9dc88aea 9299
d9f6a4ee 9300 when Aspect_Priority | Aspect_Interrupt_Priority =>
9301 T := Standard_Integer;
d97beb2f 9302
d9f6a4ee 9303 when Aspect_Relative_Deadline =>
9304 T := RTE (RE_Time_Span);
d97beb2f 9305
d9f6a4ee 9306 when Aspect_Small =>
9307 T := Universal_Real;
490beba6 9308
d9f6a4ee 9309 -- For a simple storage pool, we have to retrieve the type of the
9310 -- pool object associated with the aspect's corresponding attribute
9311 -- definition clause.
490beba6 9312
d9f6a4ee 9313 when Aspect_Simple_Storage_Pool =>
9314 T := Etype (Expression (Aspect_Rep_Item (ASN)));
d97beb2f 9315
d9f6a4ee 9316 when Aspect_Storage_Pool =>
9317 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
d97beb2f 9318
d9f6a4ee 9319 when Aspect_Alignment |
9320 Aspect_Component_Size |
9321 Aspect_Machine_Radix |
9322 Aspect_Object_Size |
9323 Aspect_Size |
9324 Aspect_Storage_Size |
9325 Aspect_Stream_Size |
9326 Aspect_Value_Size =>
9327 T := Any_Integer;
9dc88aea 9328
04ae062f 9329 when Aspect_Linker_Section =>
9330 T := Standard_String;
9331
d9f6a4ee 9332 when Aspect_Synchronization =>
9333 return;
7d20685d 9334
d9f6a4ee 9335 -- Special case, the expression of these aspects is just an entity
9336 -- that does not need any resolution, so just analyze.
7d20685d 9337
d9f6a4ee 9338 when Aspect_Input |
9339 Aspect_Output |
9340 Aspect_Read |
9341 Aspect_Suppress |
9342 Aspect_Unsuppress |
9343 Aspect_Warnings |
9344 Aspect_Write =>
9345 Analyze (Expression (ASN));
9346 return;
7d20685d 9347
d9f6a4ee 9348 -- Same for Iterator aspects, where the expression is a function
9349 -- name. Legality rules are checked separately.
89f1e35c 9350
d9f6a4ee 9351 when Aspect_Constant_Indexing |
9352 Aspect_Default_Iterator |
9353 Aspect_Iterator_Element |
9354 Aspect_Variable_Indexing =>
9355 Analyze (Expression (ASN));
9356 return;
7d20685d 9357
b3f8228a 9358 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9359
9360 when Aspect_Iterable =>
3061ffde 9361 T := Entity (ASN);
9362
b3f8228a 9363 declare
a9f5fea7 9364 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
3061ffde 9365 Assoc : Node_Id;
9366 Expr : Node_Id;
a9f5fea7 9367
b3f8228a 9368 begin
a9f5fea7 9369 if Cursor = Any_Type then
9370 return;
9371 end if;
9372
b3f8228a 9373 Assoc := First (Component_Associations (Expression (ASN)));
9374 while Present (Assoc) loop
3061ffde 9375 Expr := Expression (Assoc);
9376 Analyze (Expr);
a9f5fea7 9377
9378 if not Error_Posted (Expr) then
9379 Resolve_Iterable_Operation
9380 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9381 end if;
9382
b3f8228a 9383 Next (Assoc);
9384 end loop;
9385 end;
3061ffde 9386
b3f8228a 9387 return;
9388
d9f6a4ee 9389 -- Invariant/Predicate take boolean expressions
7d20685d 9390
d9f6a4ee 9391 when Aspect_Dynamic_Predicate |
9392 Aspect_Invariant |
9393 Aspect_Predicate |
9394 Aspect_Static_Predicate |
9395 Aspect_Type_Invariant =>
9396 T := Standard_Boolean;
7d20685d 9397
d9f6a4ee 9398 -- Here is the list of aspects that don't require delay analysis
89f1e35c 9399
d0849c23 9400 when Aspect_Abstract_State |
9401 Aspect_Annotate |
85ee12c0 9402 Aspect_Async_Readers |
9403 Aspect_Async_Writers |
d0849c23 9404 Aspect_Constant_After_Elaboration |
9405 Aspect_Contract_Cases |
9406 Aspect_Default_Initial_Condition |
9407 Aspect_Depends |
9408 Aspect_Dimension |
9409 Aspect_Dimension_System |
85ee12c0 9410 Aspect_Effective_Reads |
9411 Aspect_Effective_Writes |
d0849c23 9412 Aspect_Extensions_Visible |
9413 Aspect_Ghost |
9414 Aspect_Global |
9415 Aspect_Implicit_Dereference |
9416 Aspect_Initial_Condition |
9417 Aspect_Initializes |
9418 Aspect_Obsolescent |
9419 Aspect_Part_Of |
9420 Aspect_Post |
9421 Aspect_Postcondition |
9422 Aspect_Pre |
9423 Aspect_Precondition |
9424 Aspect_Refined_Depends |
9425 Aspect_Refined_Global |
9426 Aspect_Refined_Post |
9427 Aspect_Refined_State |
9428 Aspect_SPARK_Mode |
9429 Aspect_Test_Case |
85ee12c0 9430 Aspect_Unimplemented |
9431 Aspect_Volatile_Function =>
d9f6a4ee 9432 raise Program_Error;
2b184b2f 9433
d9f6a4ee 9434 end case;
2b184b2f 9435
d9f6a4ee 9436 -- Do the preanalyze call
2b184b2f 9437
d9f6a4ee 9438 Preanalyze_Spec_Expression (Expression (ASN), T);
9439 end Check_Aspect_At_Freeze_Point;
2b184b2f 9440
d9f6a4ee 9441 -----------------------------------
9442 -- Check_Constant_Address_Clause --
9443 -----------------------------------
2b184b2f 9444
d9f6a4ee 9445 procedure Check_Constant_Address_Clause
9446 (Expr : Node_Id;
9447 U_Ent : Entity_Id)
9448 is
9449 procedure Check_At_Constant_Address (Nod : Node_Id);
9450 -- Checks that the given node N represents a name whose 'Address is
9451 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9452 -- address value is the same at the point of declaration of U_Ent and at
9453 -- the time of elaboration of the address clause.
84ed7523 9454
d9f6a4ee 9455 procedure Check_Expr_Constants (Nod : Node_Id);
9456 -- Checks that Nod meets the requirements for a constant address clause
9457 -- in the sense of the enclosing procedure.
84ed7523 9458
d9f6a4ee 9459 procedure Check_List_Constants (Lst : List_Id);
9460 -- Check that all elements of list Lst meet the requirements for a
9461 -- constant address clause in the sense of the enclosing procedure.
84ed7523 9462
d9f6a4ee 9463 -------------------------------
9464 -- Check_At_Constant_Address --
9465 -------------------------------
84ed7523 9466
d9f6a4ee 9467 procedure Check_At_Constant_Address (Nod : Node_Id) is
9468 begin
9469 if Is_Entity_Name (Nod) then
9470 if Present (Address_Clause (Entity ((Nod)))) then
9471 Error_Msg_NE
9472 ("invalid address clause for initialized object &!",
9473 Nod, U_Ent);
9474 Error_Msg_NE
9475 ("address for& cannot" &
9476 " depend on another address clause! (RM 13.1(22))!",
9477 Nod, U_Ent);
84ed7523 9478
d9f6a4ee 9479 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9480 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9481 then
9482 Error_Msg_NE
9483 ("invalid address clause for initialized object &!",
9484 Nod, U_Ent);
9485 Error_Msg_Node_2 := U_Ent;
9486 Error_Msg_NE
9487 ("\& must be defined before & (RM 13.1(22))!",
9488 Nod, Entity (Nod));
9489 end if;
7d20685d 9490
d9f6a4ee 9491 elsif Nkind (Nod) = N_Selected_Component then
9492 declare
9493 T : constant Entity_Id := Etype (Prefix (Nod));
59f3e675 9494
d9f6a4ee 9495 begin
9496 if (Is_Record_Type (T)
9497 and then Has_Discriminants (T))
9498 or else
9499 (Is_Access_Type (T)
f02a9a9a 9500 and then Is_Record_Type (Designated_Type (T))
9501 and then Has_Discriminants (Designated_Type (T)))
d9f6a4ee 9502 then
9503 Error_Msg_NE
9504 ("invalid address clause for initialized object &!",
9505 Nod, U_Ent);
9506 Error_Msg_N
9507 ("\address cannot depend on component" &
9508 " of discriminated record (RM 13.1(22))!",
9509 Nod);
9510 else
9511 Check_At_Constant_Address (Prefix (Nod));
9512 end if;
9513 end;
89cc7147 9514
d9f6a4ee 9515 elsif Nkind (Nod) = N_Indexed_Component then
9516 Check_At_Constant_Address (Prefix (Nod));
9517 Check_List_Constants (Expressions (Nod));
89cc7147 9518
84ed7523 9519 else
d9f6a4ee 9520 Check_Expr_Constants (Nod);
84ed7523 9521 end if;
d9f6a4ee 9522 end Check_At_Constant_Address;
81b424ac 9523
d9f6a4ee 9524 --------------------------
9525 -- Check_Expr_Constants --
9526 --------------------------
7b9b2f05 9527
d9f6a4ee 9528 procedure Check_Expr_Constants (Nod : Node_Id) is
9529 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9530 Ent : Entity_Id := Empty;
7b9b2f05 9531
d9f6a4ee 9532 begin
9533 if Nkind (Nod) in N_Has_Etype
9534 and then Etype (Nod) = Any_Type
7b9b2f05 9535 then
d9f6a4ee 9536 return;
309c3053 9537 end if;
9538
d9f6a4ee 9539 case Nkind (Nod) is
9540 when N_Empty | N_Error =>
9541 return;
7d20685d 9542
d9f6a4ee 9543 when N_Identifier | N_Expanded_Name =>
9544 Ent := Entity (Nod);
7d20685d 9545
d9f6a4ee 9546 -- We need to look at the original node if it is different
9547 -- from the node, since we may have rewritten things and
9548 -- substituted an identifier representing the rewrite.
7d20685d 9549
d9f6a4ee 9550 if Original_Node (Nod) /= Nod then
9551 Check_Expr_Constants (Original_Node (Nod));
7d20685d 9552
d9f6a4ee 9553 -- If the node is an object declaration without initial
9554 -- value, some code has been expanded, and the expression
9555 -- is not constant, even if the constituents might be
9556 -- acceptable, as in A'Address + offset.
7d20685d 9557
d9f6a4ee 9558 if Ekind (Ent) = E_Variable
9559 and then
9560 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9561 and then
9562 No (Expression (Declaration_Node (Ent)))
9563 then
9564 Error_Msg_NE
9565 ("invalid address clause for initialized object &!",
9566 Nod, U_Ent);
89f1e35c 9567
d9f6a4ee 9568 -- If entity is constant, it may be the result of expanding
9569 -- a check. We must verify that its declaration appears
9570 -- before the object in question, else we also reject the
9571 -- address clause.
7d20685d 9572
d9f6a4ee 9573 elsif Ekind (Ent) = E_Constant
9574 and then In_Same_Source_Unit (Ent, U_Ent)
9575 and then Sloc (Ent) > Loc_U_Ent
9576 then
9577 Error_Msg_NE
9578 ("invalid address clause for initialized object &!",
9579 Nod, U_Ent);
9580 end if;
7d20685d 9581
d9f6a4ee 9582 return;
9583 end if;
7d20685d 9584
d9f6a4ee 9585 -- Otherwise look at the identifier and see if it is OK
7d20685d 9586
d9f6a4ee 9587 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9588 or else Is_Type (Ent)
9589 then
9590 return;
7d20685d 9591
f02a9a9a 9592 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9593
d9f6a4ee 9594 -- This is the case where we must have Ent defined before
9595 -- U_Ent. Clearly if they are in different units this
9596 -- requirement is met since the unit containing Ent is
9597 -- already processed.
7d20685d 9598
d9f6a4ee 9599 if not In_Same_Source_Unit (Ent, U_Ent) then
9600 return;
7d20685d 9601
d9f6a4ee 9602 -- Otherwise location of Ent must be before the location
9603 -- of U_Ent, that's what prior defined means.
7d20685d 9604
d9f6a4ee 9605 elsif Sloc (Ent) < Loc_U_Ent then
9606 return;
6c545057 9607
d9f6a4ee 9608 else
9609 Error_Msg_NE
9610 ("invalid address clause for initialized object &!",
9611 Nod, U_Ent);
9612 Error_Msg_Node_2 := U_Ent;
9613 Error_Msg_NE
9614 ("\& must be defined before & (RM 13.1(22))!",
9615 Nod, Ent);
9616 end if;
37c6e44c 9617
d9f6a4ee 9618 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9619 Check_Expr_Constants (Original_Node (Nod));
6c545057 9620
d9f6a4ee 9621 else
9622 Error_Msg_NE
9623 ("invalid address clause for initialized object &!",
9624 Nod, U_Ent);
3cdbaa5a 9625
d9f6a4ee 9626 if Comes_From_Source (Ent) then
9627 Error_Msg_NE
9628 ("\reference to variable& not allowed"
9629 & " (RM 13.1(22))!", Nod, Ent);
9630 else
9631 Error_Msg_N
9632 ("non-static expression not allowed"
9633 & " (RM 13.1(22))!", Nod);
9634 end if;
9635 end if;
3cdbaa5a 9636
d9f6a4ee 9637 when N_Integer_Literal =>
7f694ca2 9638
d9f6a4ee 9639 -- If this is a rewritten unchecked conversion, in a system
9640 -- where Address is an integer type, always use the base type
9641 -- for a literal value. This is user-friendly and prevents
9642 -- order-of-elaboration issues with instances of unchecked
9643 -- conversion.
3cdbaa5a 9644
d9f6a4ee 9645 if Nkind (Original_Node (Nod)) = N_Function_Call then
9646 Set_Etype (Nod, Base_Type (Etype (Nod)));
9647 end if;
e1cedbae 9648
d9f6a4ee 9649 when N_Real_Literal |
9650 N_String_Literal |
9651 N_Character_Literal =>
9652 return;
7d20685d 9653
d9f6a4ee 9654 when N_Range =>
9655 Check_Expr_Constants (Low_Bound (Nod));
9656 Check_Expr_Constants (High_Bound (Nod));
231eb581 9657
d9f6a4ee 9658 when N_Explicit_Dereference =>
9659 Check_Expr_Constants (Prefix (Nod));
231eb581 9660
d9f6a4ee 9661 when N_Indexed_Component =>
9662 Check_Expr_Constants (Prefix (Nod));
9663 Check_List_Constants (Expressions (Nod));
7d20685d 9664
d9f6a4ee 9665 when N_Slice =>
9666 Check_Expr_Constants (Prefix (Nod));
9667 Check_Expr_Constants (Discrete_Range (Nod));
cb4c311d 9668
d9f6a4ee 9669 when N_Selected_Component =>
9670 Check_Expr_Constants (Prefix (Nod));
6144c105 9671
d9f6a4ee 9672 when N_Attribute_Reference =>
9673 if Nam_In (Attribute_Name (Nod), Name_Address,
9674 Name_Access,
9675 Name_Unchecked_Access,
9676 Name_Unrestricted_Access)
9677 then
9678 Check_At_Constant_Address (Prefix (Nod));
6144c105 9679
d9f6a4ee 9680 else
9681 Check_Expr_Constants (Prefix (Nod));
9682 Check_List_Constants (Expressions (Nod));
9683 end if;
a7a4a7c2 9684
d9f6a4ee 9685 when N_Aggregate =>
9686 Check_List_Constants (Component_Associations (Nod));
9687 Check_List_Constants (Expressions (Nod));
7d20685d 9688
d9f6a4ee 9689 when N_Component_Association =>
9690 Check_Expr_Constants (Expression (Nod));
e1cedbae 9691
d9f6a4ee 9692 when N_Extension_Aggregate =>
9693 Check_Expr_Constants (Ancestor_Part (Nod));
9694 Check_List_Constants (Component_Associations (Nod));
9695 Check_List_Constants (Expressions (Nod));
3cdbaa5a 9696
d9f6a4ee 9697 when N_Null =>
9698 return;
3cdbaa5a 9699
d9f6a4ee 9700 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9701 Check_Expr_Constants (Left_Opnd (Nod));
9702 Check_Expr_Constants (Right_Opnd (Nod));
e1cedbae 9703
d9f6a4ee 9704 when N_Unary_Op =>
9705 Check_Expr_Constants (Right_Opnd (Nod));
7f694ca2 9706
d9f6a4ee 9707 when N_Type_Conversion |
9708 N_Qualified_Expression |
9709 N_Allocator |
9710 N_Unchecked_Type_Conversion =>
9711 Check_Expr_Constants (Expression (Nod));
47a46747 9712
d9f6a4ee 9713 when N_Function_Call =>
9714 if not Is_Pure (Entity (Name (Nod))) then
9715 Error_Msg_NE
9716 ("invalid address clause for initialized object &!",
9717 Nod, U_Ent);
7f694ca2 9718
d9f6a4ee 9719 Error_Msg_NE
9720 ("\function & is not pure (RM 13.1(22))!",
9721 Nod, Entity (Name (Nod)));
b55f7641 9722
d9f6a4ee 9723 else
9724 Check_List_Constants (Parameter_Associations (Nod));
9725 end if;
b55f7641 9726
d9f6a4ee 9727 when N_Parameter_Association =>
9728 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7d20685d 9729
d9f6a4ee 9730 when others =>
9731 Error_Msg_NE
9732 ("invalid address clause for initialized object &!",
9733 Nod, U_Ent);
9734 Error_Msg_NE
9735 ("\must be constant defined before& (RM 13.1(22))!",
9736 Nod, U_Ent);
9737 end case;
9738 end Check_Expr_Constants;
7d20685d 9739
d9f6a4ee 9740 --------------------------
9741 -- Check_List_Constants --
9742 --------------------------
89f1e35c 9743
d9f6a4ee 9744 procedure Check_List_Constants (Lst : List_Id) is
9745 Nod1 : Node_Id;
7d20685d 9746
d9f6a4ee 9747 begin
9748 if Present (Lst) then
9749 Nod1 := First (Lst);
9750 while Present (Nod1) loop
9751 Check_Expr_Constants (Nod1);
9752 Next (Nod1);
9753 end loop;
9754 end if;
9755 end Check_List_Constants;
81b424ac 9756
d9f6a4ee 9757 -- Start of processing for Check_Constant_Address_Clause
81b424ac 9758
d9f6a4ee 9759 begin
9760 -- If rep_clauses are to be ignored, no need for legality checks. In
9c7948d7 9761 -- particular, no need to pester user about rep clauses that violate the
9762 -- rule on constant addresses, given that these clauses will be removed
9763 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9764 -- we want to relax these checks.
7d20685d 9765
f1a9be43 9766 if not Ignore_Rep_Clauses and not CodePeer_Mode then
d9f6a4ee 9767 Check_Expr_Constants (Expr);
9768 end if;
9769 end Check_Constant_Address_Clause;
7d20685d 9770
6653b695 9771 ---------------------------
9772 -- Check_Pool_Size_Clash --
9773 ---------------------------
9774
9775 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9776 Post : Node_Id;
9777
9778 begin
9779 -- We need to find out which one came first. Note that in the case of
9780 -- aspects mixed with pragmas there are cases where the processing order
9781 -- is reversed, which is why we do the check here.
9782
9783 if Sloc (SP) < Sloc (SS) then
9784 Error_Msg_Sloc := Sloc (SP);
9785 Post := SS;
9786 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9787
9788 else
9789 Error_Msg_Sloc := Sloc (SS);
9790 Post := SP;
9791 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9792 end if;
9793
9794 Error_Msg_N
9795 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9796 end Check_Pool_Size_Clash;
9797
d9f6a4ee 9798 ----------------------------------------
9799 -- Check_Record_Representation_Clause --
9800 ----------------------------------------
85696508 9801
d9f6a4ee 9802 procedure Check_Record_Representation_Clause (N : Node_Id) is
9803 Loc : constant Source_Ptr := Sloc (N);
9804 Ident : constant Node_Id := Identifier (N);
9805 Rectype : Entity_Id;
9806 Fent : Entity_Id;
9807 CC : Node_Id;
9808 Fbit : Uint;
9809 Lbit : Uint;
9810 Hbit : Uint := Uint_0;
9811 Comp : Entity_Id;
9812 Pcomp : Entity_Id;
89f1e35c 9813
d9f6a4ee 9814 Max_Bit_So_Far : Uint;
9815 -- Records the maximum bit position so far. If all field positions
9816 -- are monotonically increasing, then we can skip the circuit for
9817 -- checking for overlap, since no overlap is possible.
85696508 9818
d9f6a4ee 9819 Tagged_Parent : Entity_Id := Empty;
9820 -- This is set in the case of a derived tagged type for which we have
9821 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9822 -- positioned by record representation clauses). In this case we must
9823 -- check for overlap between components of this tagged type, and the
9824 -- components of its parent. Tagged_Parent will point to this parent
9825 -- type. For all other cases Tagged_Parent is left set to Empty.
7d20685d 9826
d9f6a4ee 9827 Parent_Last_Bit : Uint;
9828 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9829 -- last bit position for any field in the parent type. We only need to
9830 -- check overlap for fields starting below this point.
7d20685d 9831
d9f6a4ee 9832 Overlap_Check_Required : Boolean;
9833 -- Used to keep track of whether or not an overlap check is required
7d20685d 9834
d9f6a4ee 9835 Overlap_Detected : Boolean := False;
9836 -- Set True if an overlap is detected
d6f39728 9837
d9f6a4ee 9838 Ccount : Natural := 0;
9839 -- Number of component clauses in record rep clause
d6f39728 9840
d9f6a4ee 9841 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9842 -- Given two entities for record components or discriminants, checks
9843 -- if they have overlapping component clauses and issues errors if so.
d6f39728 9844
d9f6a4ee 9845 procedure Find_Component;
9846 -- Finds component entity corresponding to current component clause (in
9847 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9848 -- start/stop bits for the field. If there is no matching component or
9849 -- if the matching component does not have a component clause, then
9850 -- that's an error and Comp is set to Empty, but no error message is
9851 -- issued, since the message was already given. Comp is also set to
9852 -- Empty if the current "component clause" is in fact a pragma.
d6f39728 9853
d9f6a4ee 9854 -----------------------------
9855 -- Check_Component_Overlap --
9856 -----------------------------
9857
9858 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9859 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9860 CC2 : constant Node_Id := Component_Clause (C2_Ent);
d6f39728 9861
d6f39728 9862 begin
d9f6a4ee 9863 if Present (CC1) and then Present (CC2) then
d6f39728 9864
d9f6a4ee 9865 -- Exclude odd case where we have two tag components in the same
9866 -- record, both at location zero. This seems a bit strange, but
9867 -- it seems to happen in some circumstances, perhaps on an error.
9868
9869 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9870 return;
d6f39728 9871 end if;
9872
d9f6a4ee 9873 -- Here we check if the two fields overlap
9874
d6f39728 9875 declare
d9f6a4ee 9876 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9877 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9878 E1 : constant Uint := S1 + Esize (C1_Ent);
9879 E2 : constant Uint := S2 + Esize (C2_Ent);
d6f39728 9880
9881 begin
d9f6a4ee 9882 if E2 <= S1 or else E1 <= S2 then
9883 null;
d6f39728 9884 else
d9f6a4ee 9885 Error_Msg_Node_2 := Component_Name (CC2);
9886 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9887 Error_Msg_Node_1 := Component_Name (CC1);
9888 Error_Msg_N
9889 ("component& overlaps & #", Component_Name (CC1));
9890 Overlap_Detected := True;
d6f39728 9891 end if;
9892 end;
d6f39728 9893 end if;
d9f6a4ee 9894 end Check_Component_Overlap;
d6f39728 9895
d9f6a4ee 9896 --------------------
9897 -- Find_Component --
9898 --------------------
9dfe12ae 9899
d9f6a4ee 9900 procedure Find_Component is
9dfe12ae 9901
d9f6a4ee 9902 procedure Search_Component (R : Entity_Id);
9903 -- Search components of R for a match. If found, Comp is set
9dfe12ae 9904
d9f6a4ee 9905 ----------------------
9906 -- Search_Component --
9907 ----------------------
e7b2d6bc 9908
d9f6a4ee 9909 procedure Search_Component (R : Entity_Id) is
9910 begin
9911 Comp := First_Component_Or_Discriminant (R);
9912 while Present (Comp) loop
e7b2d6bc 9913
d9f6a4ee 9914 -- Ignore error of attribute name for component name (we
9915 -- already gave an error message for this, so no need to
9916 -- complain here)
e7b2d6bc 9917
d9f6a4ee 9918 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9919 null;
9920 else
9921 exit when Chars (Comp) = Chars (Component_Name (CC));
9dfe12ae 9922 end if;
9923
d9f6a4ee 9924 Next_Component_Or_Discriminant (Comp);
9925 end loop;
9926 end Search_Component;
d6f39728 9927
d9f6a4ee 9928 -- Start of processing for Find_Component
d6f39728 9929
d9f6a4ee 9930 begin
9931 -- Return with Comp set to Empty if we have a pragma
d6f39728 9932
d9f6a4ee 9933 if Nkind (CC) = N_Pragma then
9934 Comp := Empty;
9935 return;
9936 end if;
d6f39728 9937
d9f6a4ee 9938 -- Search current record for matching component
d6f39728 9939
d9f6a4ee 9940 Search_Component (Rectype);
9dfe12ae 9941
d9f6a4ee 9942 -- If not found, maybe component of base type discriminant that is
9943 -- absent from statically constrained first subtype.
e7b2d6bc 9944
d9f6a4ee 9945 if No (Comp) then
9946 Search_Component (Base_Type (Rectype));
9947 end if;
e7b2d6bc 9948
d9f6a4ee 9949 -- If no component, or the component does not reference the component
9950 -- clause in question, then there was some previous error for which
9951 -- we already gave a message, so just return with Comp Empty.
d6f39728 9952
d9f6a4ee 9953 if No (Comp) or else Component_Clause (Comp) /= CC then
9954 Check_Error_Detected;
9955 Comp := Empty;
93735cb8 9956
d9f6a4ee 9957 -- Normal case where we have a component clause
93735cb8 9958
d9f6a4ee 9959 else
9960 Fbit := Component_Bit_Offset (Comp);
9961 Lbit := Fbit + Esize (Comp) - 1;
9962 end if;
9963 end Find_Component;
93735cb8 9964
d9f6a4ee 9965 -- Start of processing for Check_Record_Representation_Clause
d6f39728 9966
d9f6a4ee 9967 begin
9968 Find_Type (Ident);
9969 Rectype := Entity (Ident);
d6f39728 9970
d9f6a4ee 9971 if Rectype = Any_Type then
9972 return;
9973 else
9974 Rectype := Underlying_Type (Rectype);
9975 end if;
d6f39728 9976
d9f6a4ee 9977 -- See if we have a fully repped derived tagged type
d6f39728 9978
d9f6a4ee 9979 declare
9980 PS : constant Entity_Id := Parent_Subtype (Rectype);
d6f39728 9981
d9f6a4ee 9982 begin
9983 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9984 Tagged_Parent := PS;
d6f39728 9985
d9f6a4ee 9986 -- Find maximum bit of any component of the parent type
d6f39728 9987
d9f6a4ee 9988 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9989 Pcomp := First_Entity (Tagged_Parent);
9990 while Present (Pcomp) loop
9991 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9992 if Component_Bit_Offset (Pcomp) /= No_Uint
9993 and then Known_Static_Esize (Pcomp)
9994 then
9995 Parent_Last_Bit :=
9996 UI_Max
9997 (Parent_Last_Bit,
9998 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9999 end if;
b7df4cda 10000 else
10001
10002 -- Skip anonymous types generated for constrained array
10003 -- or record components.
d9f6a4ee 10004
b7df4cda 10005 null;
d6f39728 10006 end if;
b7df4cda 10007
10008 Next_Entity (Pcomp);
d9f6a4ee 10009 end loop;
10010 end if;
10011 end;
d6f39728 10012
d9f6a4ee 10013 -- All done if no component clauses
d6f39728 10014
d9f6a4ee 10015 CC := First (Component_Clauses (N));
d6f39728 10016
d9f6a4ee 10017 if No (CC) then
10018 return;
10019 end if;
d6f39728 10020
d9f6a4ee 10021 -- If a tag is present, then create a component clause that places it
10022 -- at the start of the record (otherwise gigi may place it after other
10023 -- fields that have rep clauses).
d6f39728 10024
d9f6a4ee 10025 Fent := First_Entity (Rectype);
d6f39728 10026
d9f6a4ee 10027 if Nkind (Fent) = N_Defining_Identifier
10028 and then Chars (Fent) = Name_uTag
10029 then
10030 Set_Component_Bit_Offset (Fent, Uint_0);
10031 Set_Normalized_Position (Fent, Uint_0);
10032 Set_Normalized_First_Bit (Fent, Uint_0);
10033 Set_Normalized_Position_Max (Fent, Uint_0);
10034 Init_Esize (Fent, System_Address_Size);
d6f39728 10035
d9f6a4ee 10036 Set_Component_Clause (Fent,
10037 Make_Component_Clause (Loc,
10038 Component_Name => Make_Identifier (Loc, Name_uTag),
d6f39728 10039
d9f6a4ee 10040 Position => Make_Integer_Literal (Loc, Uint_0),
10041 First_Bit => Make_Integer_Literal (Loc, Uint_0),
10042 Last_Bit =>
10043 Make_Integer_Literal (Loc,
10044 UI_From_Int (System_Address_Size))));
d6f39728 10045
d9f6a4ee 10046 Ccount := Ccount + 1;
10047 end if;
d6f39728 10048
d9f6a4ee 10049 Max_Bit_So_Far := Uint_Minus_1;
10050 Overlap_Check_Required := False;
d6f39728 10051
d9f6a4ee 10052 -- Process the component clauses
d6f39728 10053
d9f6a4ee 10054 while Present (CC) loop
10055 Find_Component;
d6f39728 10056
d9f6a4ee 10057 if Present (Comp) then
10058 Ccount := Ccount + 1;
d6f39728 10059
d9f6a4ee 10060 -- We need a full overlap check if record positions non-monotonic
d6f39728 10061
d9f6a4ee 10062 if Fbit <= Max_Bit_So_Far then
10063 Overlap_Check_Required := True;
10064 end if;
d6f39728 10065
d9f6a4ee 10066 Max_Bit_So_Far := Lbit;
d6f39728 10067
d9f6a4ee 10068 -- Check bit position out of range of specified size
01cb2726 10069
d9f6a4ee 10070 if Has_Size_Clause (Rectype)
10071 and then RM_Size (Rectype) <= Lbit
10072 then
10073 Error_Msg_N
10074 ("bit number out of range of specified size",
10075 Last_Bit (CC));
d6f39728 10076
d9f6a4ee 10077 -- Check for overlap with tag component
67278d60 10078
d9f6a4ee 10079 else
10080 if Is_Tagged_Type (Rectype)
10081 and then Fbit < System_Address_Size
10082 then
10083 Error_Msg_NE
10084 ("component overlaps tag field of&",
10085 Component_Name (CC), Rectype);
10086 Overlap_Detected := True;
10087 end if;
67278d60 10088
d9f6a4ee 10089 if Hbit < Lbit then
10090 Hbit := Lbit;
10091 end if;
10092 end if;
67278d60 10093
d9f6a4ee 10094 -- Check parent overlap if component might overlap parent field
67278d60 10095
d9f6a4ee 10096 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
10097 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
10098 while Present (Pcomp) loop
10099 if not Is_Tag (Pcomp)
10100 and then Chars (Pcomp) /= Name_uParent
10101 then
10102 Check_Component_Overlap (Comp, Pcomp);
10103 end if;
67278d60 10104
d9f6a4ee 10105 Next_Component_Or_Discriminant (Pcomp);
10106 end loop;
10107 end if;
10108 end if;
67278d60 10109
d9f6a4ee 10110 Next (CC);
10111 end loop;
47495553 10112
d9f6a4ee 10113 -- Now that we have processed all the component clauses, check for
10114 -- overlap. We have to leave this till last, since the components can
10115 -- appear in any arbitrary order in the representation clause.
67278d60 10116
d9f6a4ee 10117 -- We do not need this check if all specified ranges were monotonic,
10118 -- as recorded by Overlap_Check_Required being False at this stage.
67278d60 10119
d9f6a4ee 10120 -- This first section checks if there are any overlapping entries at
10121 -- all. It does this by sorting all entries and then seeing if there are
10122 -- any overlaps. If there are none, then that is decisive, but if there
10123 -- are overlaps, they may still be OK (they may result from fields in
10124 -- different variants).
67278d60 10125
d9f6a4ee 10126 if Overlap_Check_Required then
10127 Overlap_Check1 : declare
67278d60 10128
d9f6a4ee 10129 OC_Fbit : array (0 .. Ccount) of Uint;
10130 -- First-bit values for component clauses, the value is the offset
10131 -- of the first bit of the field from start of record. The zero
10132 -- entry is for use in sorting.
47495553 10133
d9f6a4ee 10134 OC_Lbit : array (0 .. Ccount) of Uint;
10135 -- Last-bit values for component clauses, the value is the offset
10136 -- of the last bit of the field from start of record. The zero
10137 -- entry is for use in sorting.
10138
10139 OC_Count : Natural := 0;
10140 -- Count of entries in OC_Fbit and OC_Lbit
67278d60 10141
d9f6a4ee 10142 function OC_Lt (Op1, Op2 : Natural) return Boolean;
10143 -- Compare routine for Sort
67278d60 10144
d9f6a4ee 10145 procedure OC_Move (From : Natural; To : Natural);
10146 -- Move routine for Sort
67278d60 10147
d9f6a4ee 10148 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
67278d60 10149
d9f6a4ee 10150 -----------
10151 -- OC_Lt --
10152 -----------
67278d60 10153
d9f6a4ee 10154 function OC_Lt (Op1, Op2 : Natural) return Boolean is
67278d60 10155 begin
d9f6a4ee 10156 return OC_Fbit (Op1) < OC_Fbit (Op2);
10157 end OC_Lt;
67278d60 10158
d9f6a4ee 10159 -------------
10160 -- OC_Move --
10161 -------------
67278d60 10162
d9f6a4ee 10163 procedure OC_Move (From : Natural; To : Natural) is
10164 begin
10165 OC_Fbit (To) := OC_Fbit (From);
10166 OC_Lbit (To) := OC_Lbit (From);
10167 end OC_Move;
67278d60 10168
d9f6a4ee 10169 -- Start of processing for Overlap_Check
67278d60 10170
67278d60 10171 begin
d9f6a4ee 10172 CC := First (Component_Clauses (N));
10173 while Present (CC) loop
67278d60 10174
d9f6a4ee 10175 -- Exclude component clause already marked in error
67278d60 10176
d9f6a4ee 10177 if not Error_Posted (CC) then
10178 Find_Component;
10179
10180 if Present (Comp) then
10181 OC_Count := OC_Count + 1;
10182 OC_Fbit (OC_Count) := Fbit;
10183 OC_Lbit (OC_Count) := Lbit;
10184 end if;
67278d60 10185 end if;
10186
d9f6a4ee 10187 Next (CC);
67278d60 10188 end loop;
67278d60 10189
d9f6a4ee 10190 Sorting.Sort (OC_Count);
67278d60 10191
d9f6a4ee 10192 Overlap_Check_Required := False;
10193 for J in 1 .. OC_Count - 1 loop
10194 if OC_Lbit (J) >= OC_Fbit (J + 1) then
10195 Overlap_Check_Required := True;
10196 exit;
10197 end if;
10198 end loop;
10199 end Overlap_Check1;
10200 end if;
67278d60 10201
d9f6a4ee 10202 -- If Overlap_Check_Required is still True, then we have to do the full
10203 -- scale overlap check, since we have at least two fields that do
10204 -- overlap, and we need to know if that is OK since they are in
10205 -- different variant, or whether we have a definite problem.
67278d60 10206
d9f6a4ee 10207 if Overlap_Check_Required then
10208 Overlap_Check2 : declare
10209 C1_Ent, C2_Ent : Entity_Id;
10210 -- Entities of components being checked for overlap
67278d60 10211
d9f6a4ee 10212 Clist : Node_Id;
10213 -- Component_List node whose Component_Items are being checked
67278d60 10214
d9f6a4ee 10215 Citem : Node_Id;
10216 -- Component declaration for component being checked
67278d60 10217
d9f6a4ee 10218 begin
10219 C1_Ent := First_Entity (Base_Type (Rectype));
67278d60 10220
d9f6a4ee 10221 -- Loop through all components in record. For each component check
10222 -- for overlap with any of the preceding elements on the component
10223 -- list containing the component and also, if the component is in
10224 -- a variant, check against components outside the case structure.
10225 -- This latter test is repeated recursively up the variant tree.
67278d60 10226
d9f6a4ee 10227 Main_Component_Loop : while Present (C1_Ent) loop
10228 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10229 goto Continue_Main_Component_Loop;
10230 end if;
67278d60 10231
d9f6a4ee 10232 -- Skip overlap check if entity has no declaration node. This
10233 -- happens with discriminants in constrained derived types.
10234 -- Possibly we are missing some checks as a result, but that
10235 -- does not seem terribly serious.
67278d60 10236
d9f6a4ee 10237 if No (Declaration_Node (C1_Ent)) then
10238 goto Continue_Main_Component_Loop;
10239 end if;
67278d60 10240
d9f6a4ee 10241 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
67278d60 10242
d9f6a4ee 10243 -- Loop through component lists that need checking. Check the
10244 -- current component list and all lists in variants above us.
67278d60 10245
d9f6a4ee 10246 Component_List_Loop : loop
67278d60 10247
d9f6a4ee 10248 -- If derived type definition, go to full declaration
10249 -- If at outer level, check discriminants if there are any.
67278d60 10250
d9f6a4ee 10251 if Nkind (Clist) = N_Derived_Type_Definition then
10252 Clist := Parent (Clist);
10253 end if;
67278d60 10254
d9f6a4ee 10255 -- Outer level of record definition, check discriminants
67278d60 10256
d9f6a4ee 10257 if Nkind_In (Clist, N_Full_Type_Declaration,
10258 N_Private_Type_Declaration)
67278d60 10259 then
d9f6a4ee 10260 if Has_Discriminants (Defining_Identifier (Clist)) then
10261 C2_Ent :=
10262 First_Discriminant (Defining_Identifier (Clist));
10263 while Present (C2_Ent) loop
10264 exit when C1_Ent = C2_Ent;
10265 Check_Component_Overlap (C1_Ent, C2_Ent);
10266 Next_Discriminant (C2_Ent);
10267 end loop;
10268 end if;
67278d60 10269
d9f6a4ee 10270 -- Record extension case
67278d60 10271
d9f6a4ee 10272 elsif Nkind (Clist) = N_Derived_Type_Definition then
10273 Clist := Empty;
67278d60 10274
d9f6a4ee 10275 -- Otherwise check one component list
67278d60 10276
d9f6a4ee 10277 else
10278 Citem := First (Component_Items (Clist));
10279 while Present (Citem) loop
10280 if Nkind (Citem) = N_Component_Declaration then
10281 C2_Ent := Defining_Identifier (Citem);
10282 exit when C1_Ent = C2_Ent;
10283 Check_Component_Overlap (C1_Ent, C2_Ent);
10284 end if;
67278d60 10285
d9f6a4ee 10286 Next (Citem);
10287 end loop;
10288 end if;
67278d60 10289
d9f6a4ee 10290 -- Check for variants above us (the parent of the Clist can
10291 -- be a variant, in which case its parent is a variant part,
10292 -- and the parent of the variant part is a component list
10293 -- whose components must all be checked against the current
10294 -- component for overlap).
67278d60 10295
d9f6a4ee 10296 if Nkind (Parent (Clist)) = N_Variant then
10297 Clist := Parent (Parent (Parent (Clist)));
67278d60 10298
d9f6a4ee 10299 -- Check for possible discriminant part in record, this
10300 -- is treated essentially as another level in the
10301 -- recursion. For this case the parent of the component
10302 -- list is the record definition, and its parent is the
10303 -- full type declaration containing the discriminant
10304 -- specifications.
10305
10306 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10307 Clist := Parent (Parent ((Clist)));
10308
10309 -- If neither of these two cases, we are at the top of
10310 -- the tree.
10311
10312 else
10313 exit Component_List_Loop;
10314 end if;
10315 end loop Component_List_Loop;
67278d60 10316
d9f6a4ee 10317 <<Continue_Main_Component_Loop>>
10318 Next_Entity (C1_Ent);
67278d60 10319
d9f6a4ee 10320 end loop Main_Component_Loop;
10321 end Overlap_Check2;
67278d60 10322 end if;
10323
d9f6a4ee 10324 -- The following circuit deals with warning on record holes (gaps). We
10325 -- skip this check if overlap was detected, since it makes sense for the
10326 -- programmer to fix this illegality before worrying about warnings.
67278d60 10327
d9f6a4ee 10328 if not Overlap_Detected and Warn_On_Record_Holes then
10329 Record_Hole_Check : declare
10330 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10331 -- Full declaration of record type
67278d60 10332
d9f6a4ee 10333 procedure Check_Component_List
10334 (CL : Node_Id;
10335 Sbit : Uint;
10336 DS : List_Id);
10337 -- Check component list CL for holes. The starting bit should be
10338 -- Sbit. which is zero for the main record component list and set
10339 -- appropriately for recursive calls for variants. DS is set to
10340 -- a list of discriminant specifications to be included in the
10341 -- consideration of components. It is No_List if none to consider.
67278d60 10342
d9f6a4ee 10343 --------------------------
10344 -- Check_Component_List --
10345 --------------------------
47495553 10346
d9f6a4ee 10347 procedure Check_Component_List
10348 (CL : Node_Id;
10349 Sbit : Uint;
10350 DS : List_Id)
10351 is
10352 Compl : Integer;
67278d60 10353
d9f6a4ee 10354 begin
10355 Compl := Integer (List_Length (Component_Items (CL)));
47495553 10356
d9f6a4ee 10357 if DS /= No_List then
10358 Compl := Compl + Integer (List_Length (DS));
10359 end if;
67278d60 10360
d9f6a4ee 10361 declare
10362 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10363 -- Gather components (zero entry is for sort routine)
67278d60 10364
d9f6a4ee 10365 Ncomps : Natural := 0;
10366 -- Number of entries stored in Comps (starting at Comps (1))
67278d60 10367
d9f6a4ee 10368 Citem : Node_Id;
10369 -- One component item or discriminant specification
67278d60 10370
d9f6a4ee 10371 Nbit : Uint;
10372 -- Starting bit for next component
67278d60 10373
d9f6a4ee 10374 CEnt : Entity_Id;
10375 -- Component entity
67278d60 10376
d9f6a4ee 10377 Variant : Node_Id;
10378 -- One variant
67278d60 10379
d9f6a4ee 10380 function Lt (Op1, Op2 : Natural) return Boolean;
10381 -- Compare routine for Sort
67278d60 10382
d9f6a4ee 10383 procedure Move (From : Natural; To : Natural);
10384 -- Move routine for Sort
67278d60 10385
d9f6a4ee 10386 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
67278d60 10387
d9f6a4ee 10388 --------
10389 -- Lt --
10390 --------
67278d60 10391
d9f6a4ee 10392 function Lt (Op1, Op2 : Natural) return Boolean is
10393 begin
10394 return Component_Bit_Offset (Comps (Op1))
10395 <
10396 Component_Bit_Offset (Comps (Op2));
10397 end Lt;
67278d60 10398
d9f6a4ee 10399 ----------
10400 -- Move --
10401 ----------
67278d60 10402
d9f6a4ee 10403 procedure Move (From : Natural; To : Natural) is
10404 begin
10405 Comps (To) := Comps (From);
10406 end Move;
67278d60 10407
d9f6a4ee 10408 begin
10409 -- Gather discriminants into Comp
67278d60 10410
d9f6a4ee 10411 if DS /= No_List then
10412 Citem := First (DS);
10413 while Present (Citem) loop
10414 if Nkind (Citem) = N_Discriminant_Specification then
10415 declare
10416 Ent : constant Entity_Id :=
10417 Defining_Identifier (Citem);
10418 begin
10419 if Ekind (Ent) = E_Discriminant then
10420 Ncomps := Ncomps + 1;
10421 Comps (Ncomps) := Ent;
10422 end if;
10423 end;
10424 end if;
67278d60 10425
d9f6a4ee 10426 Next (Citem);
10427 end loop;
10428 end if;
67278d60 10429
d9f6a4ee 10430 -- Gather component entities into Comp
67278d60 10431
d9f6a4ee 10432 Citem := First (Component_Items (CL));
10433 while Present (Citem) loop
10434 if Nkind (Citem) = N_Component_Declaration then
10435 Ncomps := Ncomps + 1;
10436 Comps (Ncomps) := Defining_Identifier (Citem);
10437 end if;
67278d60 10438
d9f6a4ee 10439 Next (Citem);
10440 end loop;
67278d60 10441
d9f6a4ee 10442 -- Now sort the component entities based on the first bit.
10443 -- Note we already know there are no overlapping components.
67278d60 10444
d9f6a4ee 10445 Sorting.Sort (Ncomps);
67278d60 10446
d9f6a4ee 10447 -- Loop through entries checking for holes
67278d60 10448
d9f6a4ee 10449 Nbit := Sbit;
10450 for J in 1 .. Ncomps loop
10451 CEnt := Comps (J);
10452 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
67278d60 10453
d9f6a4ee 10454 if Error_Msg_Uint_1 > 0 then
10455 Error_Msg_NE
10456 ("?H?^-bit gap before component&",
10457 Component_Name (Component_Clause (CEnt)), CEnt);
10458 end if;
67278d60 10459
d9f6a4ee 10460 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
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
10544 UT : constant Entity_Id := Underlying_Type (T);
10545 M : Uint;
67278d60 10546
d9f6a4ee 10547 begin
10548 Biased := False;
67278d60 10549
d9f6a4ee 10550 -- Reject patently improper size values.
67278d60 10551
d9f6a4ee 10552 if Is_Elementary_Type (T)
10553 and then Siz > UI_From_Int (Int'Last)
10554 then
10555 Error_Msg_N ("Size value too large for elementary type", N);
67278d60 10556
d9f6a4ee 10557 if Nkind (Original_Node (N)) = N_Op_Expon then
10558 Error_Msg_N
10559 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10560 end if;
10561 end if;
67278d60 10562
d9f6a4ee 10563 -- Dismiss generic types
67278d60 10564
d9f6a4ee 10565 if Is_Generic_Type (T)
10566 or else
10567 Is_Generic_Type (UT)
10568 or else
10569 Is_Generic_Type (Root_Type (UT))
10570 then
10571 return;
67278d60 10572
d9f6a4ee 10573 -- Guard against previous errors
67278d60 10574
d9f6a4ee 10575 elsif No (UT) or else UT = Any_Type then
10576 Check_Error_Detected;
10577 return;
67278d60 10578
d9f6a4ee 10579 -- Check case of bit packed array
67278d60 10580
d9f6a4ee 10581 elsif Is_Array_Type (UT)
10582 and then Known_Static_Component_Size (UT)
10583 and then Is_Bit_Packed_Array (UT)
10584 then
10585 declare
10586 Asiz : Uint;
10587 Indx : Node_Id;
10588 Ityp : Entity_Id;
67278d60 10589
d9f6a4ee 10590 begin
10591 Asiz := Component_Size (UT);
10592 Indx := First_Index (UT);
10593 loop
10594 Ityp := Etype (Indx);
67278d60 10595
d9f6a4ee 10596 -- If non-static bound, then we are not in the business of
10597 -- trying to check the length, and indeed an error will be
10598 -- issued elsewhere, since sizes of non-static array types
10599 -- cannot be set implicitly or explicitly.
67278d60 10600
cda40848 10601 if not Is_OK_Static_Subtype (Ityp) then
d9f6a4ee 10602 return;
10603 end if;
67278d60 10604
d9f6a4ee 10605 -- Otherwise accumulate next dimension
67278d60 10606
d9f6a4ee 10607 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10608 Expr_Value (Type_Low_Bound (Ityp)) +
10609 Uint_1);
67278d60 10610
d9f6a4ee 10611 Next_Index (Indx);
10612 exit when No (Indx);
10613 end loop;
67278d60 10614
d9f6a4ee 10615 if Asiz <= Siz then
10616 return;
67278d60 10617
d9f6a4ee 10618 else
10619 Error_Msg_Uint_1 := Asiz;
10620 Error_Msg_NE
10621 ("size for& too small, minimum allowed is ^", N, T);
10622 Set_Esize (T, Asiz);
10623 Set_RM_Size (T, Asiz);
10624 end if;
10625 end;
67278d60 10626
d9f6a4ee 10627 -- All other composite types are ignored
67278d60 10628
d9f6a4ee 10629 elsif Is_Composite_Type (UT) then
10630 return;
47495553 10631
d9f6a4ee 10632 -- For fixed-point types, don't check minimum if type is not frozen,
10633 -- since we don't know all the characteristics of the type that can
10634 -- affect the size (e.g. a specified small) till freeze time.
47495553 10635
d9f6a4ee 10636 elsif Is_Fixed_Point_Type (UT)
10637 and then not Is_Frozen (UT)
10638 then
10639 null;
47495553 10640
d9f6a4ee 10641 -- Cases for which a minimum check is required
47495553 10642
d9f6a4ee 10643 else
10644 -- Ignore if specified size is correct for the type
47495553 10645
d9f6a4ee 10646 if Known_Esize (UT) and then Siz = Esize (UT) then
10647 return;
10648 end if;
47495553 10649
d9f6a4ee 10650 -- Otherwise get minimum size
47495553 10651
d9f6a4ee 10652 M := UI_From_Int (Minimum_Size (UT));
47495553 10653
d9f6a4ee 10654 if Siz < M then
47495553 10655
d9f6a4ee 10656 -- Size is less than minimum size, but one possibility remains
10657 -- that we can manage with the new size if we bias the type.
47495553 10658
d9f6a4ee 10659 M := UI_From_Int (Minimum_Size (UT, Biased => True));
47495553 10660
d9f6a4ee 10661 if Siz < M then
10662 Error_Msg_Uint_1 := M;
10663 Error_Msg_NE
10664 ("size for& too small, minimum allowed is ^", N, T);
10665 Set_Esize (T, M);
10666 Set_RM_Size (T, M);
10667 else
10668 Biased := True;
10669 end if;
10670 end if;
10671 end if;
10672 end Check_Size;
47495553 10673
d9f6a4ee 10674 --------------------------
10675 -- Freeze_Entity_Checks --
10676 --------------------------
47495553 10677
d9f6a4ee 10678 procedure Freeze_Entity_Checks (N : Node_Id) is
8cf481c9 10679 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10680 -- Inspect the primitive operations of type Typ and hide all pairs of
3118058b 10681 -- implicitly declared non-overridden non-fully conformant homographs
10682 -- (Ada RM 8.3 12.3/2).
8cf481c9 10683
10684 -------------------------------------
10685 -- Hide_Non_Overridden_Subprograms --
10686 -------------------------------------
10687
10688 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10689 procedure Hide_Matching_Homographs
10690 (Subp_Id : Entity_Id;
10691 Start_Elmt : Elmt_Id);
10692 -- Inspect a list of primitive operations starting with Start_Elmt
3118058b 10693 -- and find matching implicitly declared non-overridden non-fully
10694 -- conformant homographs of Subp_Id. If found, all matches along
10695 -- with Subp_Id are hidden from all visibility.
8cf481c9 10696
10697 function Is_Non_Overridden_Or_Null_Procedure
10698 (Subp_Id : Entity_Id) return Boolean;
10699 -- Determine whether subprogram Subp_Id is implicitly declared non-
10700 -- overridden subprogram or an implicitly declared null procedure.
10701
10702 ------------------------------
10703 -- Hide_Matching_Homographs --
10704 ------------------------------
10705
10706 procedure Hide_Matching_Homographs
10707 (Subp_Id : Entity_Id;
10708 Start_Elmt : Elmt_Id)
10709 is
10710 Prim : Entity_Id;
10711 Prim_Elmt : Elmt_Id;
10712
10713 begin
10714 Prim_Elmt := Start_Elmt;
10715 while Present (Prim_Elmt) loop
10716 Prim := Node (Prim_Elmt);
10717
10718 -- The current primitive is implicitly declared non-overridden
3118058b 10719 -- non-fully conformant homograph of Subp_Id. Both subprograms
10720 -- must be hidden from visibility.
8cf481c9 10721
10722 if Chars (Prim) = Chars (Subp_Id)
8cf481c9 10723 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
3118058b 10724 and then not Fully_Conformant (Prim, Subp_Id)
8cf481c9 10725 then
8c7ee4ac 10726 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10727 Set_Is_Immediately_Visible (Prim, False);
10728 Set_Is_Potentially_Use_Visible (Prim, False);
8cf481c9 10729
8c7ee4ac 10730 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10731 Set_Is_Immediately_Visible (Subp_Id, False);
10732 Set_Is_Potentially_Use_Visible (Subp_Id, False);
8cf481c9 10733 end if;
10734
10735 Next_Elmt (Prim_Elmt);
10736 end loop;
10737 end Hide_Matching_Homographs;
10738
10739 -----------------------------------------
10740 -- Is_Non_Overridden_Or_Null_Procedure --
10741 -----------------------------------------
10742
10743 function Is_Non_Overridden_Or_Null_Procedure
10744 (Subp_Id : Entity_Id) return Boolean
10745 is
10746 Alias_Id : Entity_Id;
10747
10748 begin
10749 -- The subprogram is inherited (implicitly declared), it does not
10750 -- override and does not cover a primitive of an interface.
10751
10752 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10753 and then Present (Alias (Subp_Id))
10754 and then No (Interface_Alias (Subp_Id))
10755 and then No (Overridden_Operation (Subp_Id))
10756 then
10757 Alias_Id := Alias (Subp_Id);
10758
10759 if Requires_Overriding (Alias_Id) then
10760 return True;
10761
10762 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10763 and then Null_Present (Parent (Alias_Id))
10764 then
10765 return True;
10766 end if;
10767 end if;
10768
10769 return False;
10770 end Is_Non_Overridden_Or_Null_Procedure;
10771
10772 -- Local variables
10773
10774 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10775 Prim : Entity_Id;
10776 Prim_Elmt : Elmt_Id;
10777
10778 -- Start of processing for Hide_Non_Overridden_Subprograms
10779
10780 begin
3118058b 10781 -- Inspect the list of primitives looking for non-overridden
10782 -- subprograms.
8cf481c9 10783
10784 if Present (Prim_Ops) then
10785 Prim_Elmt := First_Elmt (Prim_Ops);
10786 while Present (Prim_Elmt) loop
10787 Prim := Node (Prim_Elmt);
10788 Next_Elmt (Prim_Elmt);
10789
10790 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10791 Hide_Matching_Homographs
10792 (Subp_Id => Prim,
10793 Start_Elmt => Prim_Elmt);
10794 end if;
10795 end loop;
10796 end if;
10797 end Hide_Non_Overridden_Subprograms;
10798
10799 ---------------------
10800 -- Local variables --
10801 ---------------------
10802
d9f6a4ee 10803 E : constant Entity_Id := Entity (N);
47495553 10804
d9f6a4ee 10805 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10806 -- True in non-generic case. Some of the processing here is skipped
10807 -- for the generic case since it is not needed. Basically in the
10808 -- generic case, we only need to do stuff that might generate error
10809 -- messages or warnings.
8cf481c9 10810
10811 -- Start of processing for Freeze_Entity_Checks
10812
d9f6a4ee 10813 begin
10814 -- Remember that we are processing a freezing entity. Required to
10815 -- ensure correct decoration of internal entities associated with
10816 -- interfaces (see New_Overloaded_Entity).
47495553 10817
d9f6a4ee 10818 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
47495553 10819
d9f6a4ee 10820 -- For tagged types covering interfaces add internal entities that link
10821 -- the primitives of the interfaces with the primitives that cover them.
10822 -- Note: These entities were originally generated only when generating
10823 -- code because their main purpose was to provide support to initialize
10824 -- the secondary dispatch tables. They are now generated also when
10825 -- compiling with no code generation to provide ASIS the relationship
10826 -- between interface primitives and tagged type primitives. They are
10827 -- also used to locate primitives covering interfaces when processing
10828 -- generics (see Derive_Subprograms).
47495553 10829
d9f6a4ee 10830 -- This is not needed in the generic case
47495553 10831
d9f6a4ee 10832 if Ada_Version >= Ada_2005
10833 and then Non_Generic_Case
10834 and then Ekind (E) = E_Record_Type
10835 and then Is_Tagged_Type (E)
10836 and then not Is_Interface (E)
10837 and then Has_Interfaces (E)
10838 then
10839 -- This would be a good common place to call the routine that checks
10840 -- overriding of interface primitives (and thus factorize calls to
10841 -- Check_Abstract_Overriding located at different contexts in the
10842 -- compiler). However, this is not possible because it causes
10843 -- spurious errors in case of late overriding.
47495553 10844
d9f6a4ee 10845 Add_Internal_Interface_Entities (E);
10846 end if;
47495553 10847
8cf481c9 10848 -- After all forms of overriding have been resolved, a tagged type may
10849 -- be left with a set of implicitly declared and possibly erroneous
10850 -- abstract subprograms, null procedures and subprograms that require
10851 -- overriding. If this set contains fully conformat homographs, then one
10852 -- is chosen arbitrarily (already done during resolution), otherwise all
3118058b 10853 -- remaining non-fully conformant homographs are hidden from visibility
8cf481c9 10854 -- (Ada RM 8.3 12.3/2).
10855
10856 if Is_Tagged_Type (E) then
10857 Hide_Non_Overridden_Subprograms (E);
10858 end if;
10859
d9f6a4ee 10860 -- Check CPP types
47495553 10861
d9f6a4ee 10862 if Ekind (E) = E_Record_Type
10863 and then Is_CPP_Class (E)
10864 and then Is_Tagged_Type (E)
10865 and then Tagged_Type_Expansion
d9f6a4ee 10866 then
10867 if CPP_Num_Prims (E) = 0 then
47495553 10868
d9f6a4ee 10869 -- If the CPP type has user defined components then it must import
10870 -- primitives from C++. This is required because if the C++ class
10871 -- has no primitives then the C++ compiler does not added the _tag
10872 -- component to the type.
47495553 10873
d9f6a4ee 10874 if First_Entity (E) /= Last_Entity (E) then
10875 Error_Msg_N
10876 ("'C'P'P type must import at least one primitive from C++??",
10877 E);
10878 end if;
10879 end if;
47495553 10880
d9f6a4ee 10881 -- Check that all its primitives are abstract or imported from C++.
10882 -- Check also availability of the C++ constructor.
47495553 10883
d9f6a4ee 10884 declare
10885 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10886 Elmt : Elmt_Id;
10887 Error_Reported : Boolean := False;
10888 Prim : Node_Id;
47495553 10889
d9f6a4ee 10890 begin
10891 Elmt := First_Elmt (Primitive_Operations (E));
10892 while Present (Elmt) loop
10893 Prim := Node (Elmt);
47495553 10894
d9f6a4ee 10895 if Comes_From_Source (Prim) then
10896 if Is_Abstract_Subprogram (Prim) then
10897 null;
47495553 10898
d9f6a4ee 10899 elsif not Is_Imported (Prim)
10900 or else Convention (Prim) /= Convention_CPP
10901 then
10902 Error_Msg_N
10903 ("primitives of 'C'P'P types must be imported from C++ "
10904 & "or abstract??", Prim);
47495553 10905
d9f6a4ee 10906 elsif not Has_Constructors
10907 and then not Error_Reported
10908 then
10909 Error_Msg_Name_1 := Chars (E);
10910 Error_Msg_N
10911 ("??'C'P'P constructor required for type %", Prim);
10912 Error_Reported := True;
10913 end if;
10914 end if;
47495553 10915
d9f6a4ee 10916 Next_Elmt (Elmt);
10917 end loop;
10918 end;
10919 end if;
47495553 10920
d9f6a4ee 10921 -- Check Ada derivation of CPP type
47495553 10922
30ab103b 10923 if Expander_Active -- why? losing errors in -gnatc mode???
10924 and then Present (Etype (E)) -- defend against errors
d9f6a4ee 10925 and then Tagged_Type_Expansion
10926 and then Ekind (E) = E_Record_Type
10927 and then Etype (E) /= E
10928 and then Is_CPP_Class (Etype (E))
10929 and then CPP_Num_Prims (Etype (E)) > 0
10930 and then not Is_CPP_Class (E)
10931 and then not Has_CPP_Constructors (Etype (E))
10932 then
10933 -- If the parent has C++ primitives but it has no constructor then
10934 -- check that all the primitives are overridden in this derivation;
10935 -- otherwise the constructor of the parent is needed to build the
10936 -- dispatch table.
47495553 10937
d9f6a4ee 10938 declare
10939 Elmt : Elmt_Id;
10940 Prim : Node_Id;
47495553 10941
10942 begin
d9f6a4ee 10943 Elmt := First_Elmt (Primitive_Operations (E));
10944 while Present (Elmt) loop
10945 Prim := Node (Elmt);
47495553 10946
d9f6a4ee 10947 if not Is_Abstract_Subprogram (Prim)
10948 and then No (Interface_Alias (Prim))
10949 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
47495553 10950 then
d9f6a4ee 10951 Error_Msg_Name_1 := Chars (Etype (E));
10952 Error_Msg_N
10953 ("'C'P'P constructor required for parent type %", E);
10954 exit;
47495553 10955 end if;
d9f6a4ee 10956
10957 Next_Elmt (Elmt);
10958 end loop;
10959 end;
47495553 10960 end if;
10961
d9f6a4ee 10962 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
67278d60 10963
d9f6a4ee 10964 -- If we have a type with predicates, build predicate function. This
0e9014a7 10965 -- is not needed in the generic case, and is not needed within TSS
ea822fd4 10966 -- subprograms and other predefined primitives.
67278d60 10967
ea822fd4 10968 if Non_Generic_Case
10969 and then Is_Type (E)
10970 and then Has_Predicates (E)
10971 and then not Within_Internal_Subprogram
10972 then
d9f6a4ee 10973 Build_Predicate_Functions (E, N);
10974 end if;
67278d60 10975
d9f6a4ee 10976 -- If type has delayed aspects, this is where we do the preanalysis at
10977 -- the freeze point, as part of the consistent visibility check. Note
10978 -- that this must be done after calling Build_Predicate_Functions or
10979 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10980 -- the subtype name in the saved expression so that they will not cause
10981 -- trouble in the preanalysis.
67278d60 10982
d9f6a4ee 10983 -- This is also not needed in the generic case
10984
10985 if Non_Generic_Case
10986 and then Has_Delayed_Aspects (E)
10987 and then Scope (E) = Current_Scope
10988 then
10989 -- Retrieve the visibility to the discriminants in order to properly
10990 -- analyze the aspects.
10991
10992 Push_Scope_And_Install_Discriminants (E);
10993
10994 declare
10995 Ritem : Node_Id;
10996
10997 begin
10998 -- Look for aspect specification entries for this entity
67278d60 10999
d9f6a4ee 11000 Ritem := First_Rep_Item (E);
11001 while Present (Ritem) loop
11002 if Nkind (Ritem) = N_Aspect_Specification
11003 and then Entity (Ritem) = E
11004 and then Is_Delayed_Aspect (Ritem)
11005 then
11006 Check_Aspect_At_Freeze_Point (Ritem);
11007 end if;
67278d60 11008
d9f6a4ee 11009 Next_Rep_Item (Ritem);
11010 end loop;
11011 end;
67278d60 11012
d9f6a4ee 11013 Uninstall_Discriminants_And_Pop_Scope (E);
67278d60 11014 end if;
67278d60 11015
d9f6a4ee 11016 -- For a record type, deal with variant parts. This has to be delayed
d0988351 11017 -- to this point, because of the issue of statically predicated
d9f6a4ee 11018 -- subtypes, which we have to ensure are frozen before checking
11019 -- choices, since we need to have the static choice list set.
d6f39728 11020
d9f6a4ee 11021 if Is_Record_Type (E) then
11022 Check_Variant_Part : declare
11023 D : constant Node_Id := Declaration_Node (E);
11024 T : Node_Id;
11025 C : Node_Id;
11026 VP : Node_Id;
d6f39728 11027
d9f6a4ee 11028 Others_Present : Boolean;
11029 pragma Warnings (Off, Others_Present);
11030 -- Indicates others present, not used in this case
d6f39728 11031
d9f6a4ee 11032 procedure Non_Static_Choice_Error (Choice : Node_Id);
11033 -- Error routine invoked by the generic instantiation below when
11034 -- the variant part has a non static choice.
f117057b 11035
d9f6a4ee 11036 procedure Process_Declarations (Variant : Node_Id);
11037 -- Processes declarations associated with a variant. We analyzed
11038 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
11039 -- but we still need the recursive call to Check_Choices for any
11040 -- nested variant to get its choices properly processed. This is
11041 -- also where we expand out the choices if expansion is active.
1f526845 11042
d9f6a4ee 11043 package Variant_Choices_Processing is new
11044 Generic_Check_Choices
11045 (Process_Empty_Choice => No_OP,
11046 Process_Non_Static_Choice => Non_Static_Choice_Error,
11047 Process_Associated_Node => Process_Declarations);
11048 use Variant_Choices_Processing;
f117057b 11049
d9f6a4ee 11050 -----------------------------
11051 -- Non_Static_Choice_Error --
11052 -----------------------------
d6f39728 11053
d9f6a4ee 11054 procedure Non_Static_Choice_Error (Choice : Node_Id) is
11055 begin
11056 Flag_Non_Static_Expr
11057 ("choice given in variant part is not static!", Choice);
11058 end Non_Static_Choice_Error;
d6f39728 11059
d9f6a4ee 11060 --------------------------
11061 -- Process_Declarations --
11062 --------------------------
dba36b60 11063
d9f6a4ee 11064 procedure Process_Declarations (Variant : Node_Id) is
11065 CL : constant Node_Id := Component_List (Variant);
11066 VP : Node_Id;
dba36b60 11067
d9f6a4ee 11068 begin
11069 -- Check for static predicate present in this variant
ea61a7ea 11070
d9f6a4ee 11071 if Has_SP_Choice (Variant) then
ea61a7ea 11072
d9f6a4ee 11073 -- Here we expand. You might expect to find this call in
11074 -- Expand_N_Variant_Part, but that is called when we first
11075 -- see the variant part, and we cannot do this expansion
11076 -- earlier than the freeze point, since for statically
11077 -- predicated subtypes, the predicate is not known till
11078 -- the freeze point.
ea61a7ea 11079
d9f6a4ee 11080 -- Furthermore, we do this expansion even if the expander
11081 -- is not active, because other semantic processing, e.g.
11082 -- for aggregates, requires the expanded list of choices.
ea61a7ea 11083
d9f6a4ee 11084 -- If the expander is not active, then we can't just clobber
11085 -- the list since it would invalidate the ASIS -gnatct tree.
11086 -- So we have to rewrite the variant part with a Rewrite
11087 -- call that replaces it with a copy and clobber the copy.
11088
11089 if not Expander_Active then
11090 declare
11091 NewV : constant Node_Id := New_Copy (Variant);
11092 begin
11093 Set_Discrete_Choices
11094 (NewV, New_Copy_List (Discrete_Choices (Variant)));
11095 Rewrite (Variant, NewV);
11096 end;
11097 end if;
11098
11099 Expand_Static_Predicates_In_Choices (Variant);
ea61a7ea 11100 end if;
11101
d9f6a4ee 11102 -- We don't need to worry about the declarations in the variant
11103 -- (since they were analyzed by Analyze_Choices when we first
11104 -- encountered the variant), but we do need to take care of
11105 -- expansion of any nested variants.
ea61a7ea 11106
d9f6a4ee 11107 if not Null_Present (CL) then
11108 VP := Variant_Part (CL);
ea61a7ea 11109
d9f6a4ee 11110 if Present (VP) then
11111 Check_Choices
11112 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
11113 end if;
11114 end if;
11115 end Process_Declarations;
ea61a7ea 11116
d9f6a4ee 11117 -- Start of processing for Check_Variant_Part
b9e61b2a 11118
d9f6a4ee 11119 begin
11120 -- Find component list
ea61a7ea 11121
d9f6a4ee 11122 C := Empty;
ea61a7ea 11123
d9f6a4ee 11124 if Nkind (D) = N_Full_Type_Declaration then
11125 T := Type_Definition (D);
ea61a7ea 11126
d9f6a4ee 11127 if Nkind (T) = N_Record_Definition then
11128 C := Component_List (T);
d6f39728 11129
d9f6a4ee 11130 elsif Nkind (T) = N_Derived_Type_Definition
11131 and then Present (Record_Extension_Part (T))
11132 then
11133 C := Component_List (Record_Extension_Part (T));
11134 end if;
11135 end if;
d6f39728 11136
d9f6a4ee 11137 -- Case of variant part present
d6f39728 11138
d9f6a4ee 11139 if Present (C) and then Present (Variant_Part (C)) then
11140 VP := Variant_Part (C);
ea61a7ea 11141
d9f6a4ee 11142 -- Check choices
ea61a7ea 11143
d9f6a4ee 11144 Check_Choices
11145 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
ea61a7ea 11146
d9f6a4ee 11147 -- If the last variant does not contain the Others choice,
11148 -- replace it with an N_Others_Choice node since Gigi always
11149 -- wants an Others. Note that we do not bother to call Analyze
11150 -- on the modified variant part, since its only effect would be
11151 -- to compute the Others_Discrete_Choices node laboriously, and
11152 -- of course we already know the list of choices corresponding
39a0c1d3 11153 -- to the others choice (it's the list we're replacing).
d6f39728 11154
d9f6a4ee 11155 -- We only want to do this if the expander is active, since
39a0c1d3 11156 -- we do not want to clobber the ASIS tree.
d6f39728 11157
d9f6a4ee 11158 if Expander_Active then
11159 declare
11160 Last_Var : constant Node_Id :=
11161 Last_Non_Pragma (Variants (VP));
d6f39728 11162
d9f6a4ee 11163 Others_Node : Node_Id;
d6f39728 11164
d9f6a4ee 11165 begin
11166 if Nkind (First (Discrete_Choices (Last_Var))) /=
11167 N_Others_Choice
11168 then
11169 Others_Node := Make_Others_Choice (Sloc (Last_Var));
11170 Set_Others_Discrete_Choices
11171 (Others_Node, Discrete_Choices (Last_Var));
11172 Set_Discrete_Choices
11173 (Last_Var, New_List (Others_Node));
11174 end if;
11175 end;
11176 end if;
d6f39728 11177 end if;
d9f6a4ee 11178 end Check_Variant_Part;
d6f39728 11179 end if;
d9f6a4ee 11180 end Freeze_Entity_Checks;
d6f39728 11181
11182 -------------------------
11183 -- Get_Alignment_Value --
11184 -------------------------
11185
11186 function Get_Alignment_Value (Expr : Node_Id) return Uint is
11187 Align : constant Uint := Static_Integer (Expr);
11188
11189 begin
11190 if Align = No_Uint then
11191 return No_Uint;
11192
11193 elsif Align <= 0 then
11194 Error_Msg_N ("alignment value must be positive", Expr);
11195 return No_Uint;
11196
11197 else
11198 for J in Int range 0 .. 64 loop
11199 declare
11200 M : constant Uint := Uint_2 ** J;
11201
11202 begin
11203 exit when M = Align;
11204
11205 if M > Align then
11206 Error_Msg_N
11207 ("alignment value must be power of 2", Expr);
11208 return No_Uint;
11209 end if;
11210 end;
11211 end loop;
11212
11213 return Align;
11214 end if;
11215 end Get_Alignment_Value;
11216
99a2d5bd 11217 -------------------------------------
11218 -- Inherit_Aspects_At_Freeze_Point --
11219 -------------------------------------
11220
11221 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11222 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11223 (Rep_Item : Node_Id) return Boolean;
11224 -- This routine checks if Rep_Item is either a pragma or an aspect
11225 -- specification node whose correponding pragma (if any) is present in
11226 -- the Rep Item chain of the entity it has been specified to.
11227
11228 --------------------------------------------------
11229 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11230 --------------------------------------------------
11231
11232 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11233 (Rep_Item : Node_Id) return Boolean
11234 is
11235 begin
ec6f6da5 11236 return
11237 Nkind (Rep_Item) = N_Pragma
11238 or else Present_In_Rep_Item
11239 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
99a2d5bd 11240 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11241
29a9d4be 11242 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11243
99a2d5bd 11244 begin
11245 -- A representation item is either subtype-specific (Size and Alignment
11246 -- clauses) or type-related (all others). Subtype-specific aspects may
29a9d4be 11247 -- differ for different subtypes of the same type (RM 13.1.8).
99a2d5bd 11248
11249 -- A derived type inherits each type-related representation aspect of
11250 -- its parent type that was directly specified before the declaration of
29a9d4be 11251 -- the derived type (RM 13.1.15).
99a2d5bd 11252
11253 -- A derived subtype inherits each subtype-specific representation
11254 -- aspect of its parent subtype that was directly specified before the
29a9d4be 11255 -- declaration of the derived type (RM 13.1.15).
99a2d5bd 11256
11257 -- The general processing involves inheriting a representation aspect
11258 -- from a parent type whenever the first rep item (aspect specification,
11259 -- attribute definition clause, pragma) corresponding to the given
11260 -- representation aspect in the rep item chain of Typ, if any, isn't
11261 -- directly specified to Typ but to one of its parents.
11262
11263 -- ??? Note that, for now, just a limited number of representation
29a9d4be 11264 -- aspects have been inherited here so far. Many of them are
11265 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11266 -- a non- exhaustive list of aspects that likely also need to
11267 -- be moved to this routine: Alignment, Component_Alignment,
11268 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
99a2d5bd 11269 -- Preelaborable_Initialization, RM_Size and Small.
11270
8b6e9bf2 11271 -- In addition, Convention must be propagated from base type to subtype,
11272 -- because the subtype may have been declared on an incomplete view.
11273
99a2d5bd 11274 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11275 return;
11276 end if;
11277
11278 -- Ada_05/Ada_2005
11279
11280 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11281 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11282 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11283 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11284 then
11285 Set_Is_Ada_2005_Only (Typ);
11286 end if;
11287
11288 -- Ada_12/Ada_2012
11289
11290 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11291 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11292 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11293 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11294 then
11295 Set_Is_Ada_2012_Only (Typ);
11296 end if;
11297
11298 -- Atomic/Shared
11299
11300 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11301 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11302 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11303 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11304 then
11305 Set_Is_Atomic (Typ);
99a2d5bd 11306 Set_Is_Volatile (Typ);
4bf2acc9 11307 Set_Treat_As_Volatile (Typ);
99a2d5bd 11308 end if;
11309
8b6e9bf2 11310 -- Convention
11311
7ac4254e 11312 if Is_Record_Type (Typ)
11313 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11314 then
8b6e9bf2 11315 Set_Convention (Typ, Convention (Base_Type (Typ)));
11316 end if;
11317
29a9d4be 11318 -- Default_Component_Value
99a2d5bd 11319
81c2bc19 11320 -- Verify that there is no rep_item declared for the type, and there
11321 -- is one coming from an ancestor.
11322
99a2d5bd 11323 if Is_Array_Type (Typ)
f3d70f08 11324 and then Is_Base_Type (Typ)
81c2bc19 11325 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
99a2d5bd 11326 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11327 then
11328 Set_Default_Aspect_Component_Value (Typ,
11329 Default_Aspect_Component_Value
11330 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11331 end if;
11332
29a9d4be 11333 -- Default_Value
99a2d5bd 11334
11335 if Is_Scalar_Type (Typ)
f3d70f08 11336 and then Is_Base_Type (Typ)
81c2bc19 11337 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
99a2d5bd 11338 and then Has_Rep_Item (Typ, Name_Default_Value)
11339 then
81c2bc19 11340 Set_Has_Default_Aspect (Typ);
99a2d5bd 11341 Set_Default_Aspect_Value (Typ,
11342 Default_Aspect_Value
11343 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11344 end if;
11345
11346 -- Discard_Names
11347
11348 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11349 and then Has_Rep_Item (Typ, Name_Discard_Names)
11350 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11351 (Get_Rep_Item (Typ, Name_Discard_Names))
11352 then
11353 Set_Discard_Names (Typ);
11354 end if;
11355
11356 -- Invariants
11357
11358 if not Has_Rep_Item (Typ, Name_Invariant, False)
11359 and then Has_Rep_Item (Typ, Name_Invariant)
11360 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11361 (Get_Rep_Item (Typ, Name_Invariant))
11362 then
11363 Set_Has_Invariants (Typ);
11364
11365 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
11366 Set_Has_Inheritable_Invariants (Typ);
11367 end if;
d7487d7d 11368
953378ae 11369 -- If we have a subtype with invariants, whose base type does not have
11370 -- invariants, copy these invariants to the base type. This happens for
11371 -- the case of implicit base types created for scalar and array types.
d7487d7d 11372
953378ae 11373 elsif Has_Invariants (Typ)
d7487d7d 11374 and then not Has_Invariants (Base_Type (Typ))
11375 then
11376 Set_Has_Invariants (Base_Type (Typ));
11377 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
99a2d5bd 11378 end if;
11379
11380 -- Volatile
11381
11382 if not Has_Rep_Item (Typ, Name_Volatile, False)
11383 and then Has_Rep_Item (Typ, Name_Volatile)
11384 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11385 (Get_Rep_Item (Typ, Name_Volatile))
11386 then
99a2d5bd 11387 Set_Is_Volatile (Typ);
4bf2acc9 11388 Set_Treat_As_Volatile (Typ);
99a2d5bd 11389 end if;
11390
2fe893b9 11391 -- Volatile_Full_Access
11392
11393 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11394 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11395 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11396 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11397 then
4bf2acc9 11398 Set_Is_Volatile_Full_Access (Typ);
2fe893b9 11399 Set_Is_Volatile (Typ);
4bf2acc9 11400 Set_Treat_As_Volatile (Typ);
2fe893b9 11401 end if;
11402
99a2d5bd 11403 -- Inheritance for derived types only
11404
11405 if Is_Derived_Type (Typ) then
11406 declare
11407 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11408 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11409
11410 begin
11411 -- Atomic_Components
11412
11413 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11414 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11415 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11416 (Get_Rep_Item (Typ, Name_Atomic_Components))
11417 then
11418 Set_Has_Atomic_Components (Imp_Bas_Typ);
11419 end if;
11420
11421 -- Volatile_Components
11422
11423 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11424 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11425 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11426 (Get_Rep_Item (Typ, Name_Volatile_Components))
11427 then
11428 Set_Has_Volatile_Components (Imp_Bas_Typ);
11429 end if;
11430
e81df51c 11431 -- Finalize_Storage_Only
99a2d5bd 11432
11433 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11434 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11435 then
11436 Set_Finalize_Storage_Only (Bas_Typ);
11437 end if;
11438
11439 -- Universal_Aliasing
11440
11441 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11442 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11443 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11444 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11445 then
11446 Set_Universal_Aliasing (Imp_Bas_Typ);
11447 end if;
11448
e81df51c 11449 -- Bit_Order
99a2d5bd 11450
11451 if Is_Record_Type (Typ) then
99a2d5bd 11452 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11453 and then Has_Rep_Item (Typ, Name_Bit_Order)
11454 then
11455 Set_Reverse_Bit_Order (Bas_Typ,
11456 Reverse_Bit_Order (Entity (Name
11457 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11458 end if;
e81df51c 11459 end if;
11460
e9218716 11461 -- Scalar_Storage_Order
11462
11463 -- Note: the aspect is specified on a first subtype, but recorded
11464 -- in a flag of the base type!
e81df51c 11465
11466 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
29b91bc7 11467 and then Typ = Bas_Typ
e81df51c 11468 then
e81df51c 11469 -- For a type extension, always inherit from parent; otherwise
11470 -- inherit if no default applies. Note: we do not check for
11471 -- an explicit rep item on the parent type when inheriting,
11472 -- because the parent SSO may itself have been set by default.
99a2d5bd 11473
e9218716 11474 if not Has_Rep_Item (First_Subtype (Typ),
11475 Name_Scalar_Storage_Order, False)
e81df51c 11476 and then (Is_Tagged_Type (Bas_Typ)
29b91bc7 11477 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11478 or else
11479 SSO_Set_High_By_Default (Bas_Typ)))
99a2d5bd 11480 then
11481 Set_Reverse_Storage_Order (Bas_Typ,
423b89fd 11482 Reverse_Storage_Order
11483 (Implementation_Base_Type (Etype (Bas_Typ))));
b64082f2 11484
11485 -- Clear default SSO indications, since the inherited aspect
11486 -- which was set explicitly overrides the default.
11487
11488 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11489 Set_SSO_Set_High_By_Default (Bas_Typ, False);
99a2d5bd 11490 end if;
11491 end if;
11492 end;
11493 end if;
11494 end Inherit_Aspects_At_Freeze_Point;
11495
d6f39728 11496 ----------------
11497 -- Initialize --
11498 ----------------
11499
11500 procedure Initialize is
11501 begin
7717ea00 11502 Address_Clause_Checks.Init;
d6f39728 11503 Unchecked_Conversions.Init;
dba38d2f 11504
36ac5fbb 11505 if AAMP_On_Target then
dba38d2f 11506 Independence_Checks.Init;
11507 end if;
d6f39728 11508 end Initialize;
11509
2625eb01 11510 ---------------------------
11511 -- Install_Discriminants --
11512 ---------------------------
11513
11514 procedure Install_Discriminants (E : Entity_Id) is
11515 Disc : Entity_Id;
11516 Prev : Entity_Id;
11517 begin
11518 Disc := First_Discriminant (E);
11519 while Present (Disc) loop
11520 Prev := Current_Entity (Disc);
11521 Set_Current_Entity (Disc);
11522 Set_Is_Immediately_Visible (Disc);
11523 Set_Homonym (Disc, Prev);
11524 Next_Discriminant (Disc);
11525 end loop;
11526 end Install_Discriminants;
11527
d6f39728 11528 -------------------------
11529 -- Is_Operational_Item --
11530 -------------------------
11531
11532 function Is_Operational_Item (N : Node_Id) return Boolean is
11533 begin
11534 if Nkind (N) /= N_Attribute_Definition_Clause then
11535 return False;
b9e61b2a 11536
d6f39728 11537 else
11538 declare
b9e61b2a 11539 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
d6f39728 11540 begin
078a74b8 11541
55ab5265 11542 -- List of operational items is given in AARM 13.1(8.mm/1).
078a74b8 11543 -- It is clearly incomplete, as it does not include iterator
11544 -- aspects, among others.
11545
11546 return Id = Attribute_Constant_Indexing
11547 or else Id = Attribute_Default_Iterator
11548 or else Id = Attribute_Implicit_Dereference
11549 or else Id = Attribute_Input
11550 or else Id = Attribute_Iterator_Element
11551 or else Id = Attribute_Iterable
d6f39728 11552 or else Id = Attribute_Output
11553 or else Id = Attribute_Read
078a74b8 11554 or else Id = Attribute_Variable_Indexing
f15731c4 11555 or else Id = Attribute_Write
11556 or else Id = Attribute_External_Tag;
d6f39728 11557 end;
11558 end if;
11559 end Is_Operational_Item;
11560
3b23aaa0 11561 -------------------------
11562 -- Is_Predicate_Static --
11563 -------------------------
11564
94d896aa 11565 -- Note: the basic legality of the expression has already been checked, so
11566 -- we don't need to worry about cases or ranges on strings for example.
11567
3b23aaa0 11568 function Is_Predicate_Static
11569 (Expr : Node_Id;
11570 Nam : Name_Id) return Boolean
11571 is
11572 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
973c2fba 11573 -- Given a list of case expression alternatives, returns True if all
11574 -- the alternatives are static (have all static choices, and a static
11575 -- expression).
3b23aaa0 11576
11577 function All_Static_Choices (L : List_Id) return Boolean;
a360a0f7 11578 -- Returns true if all elements of the list are OK static choices
3b23aaa0 11579 -- as defined below for Is_Static_Choice. Used for case expression
973c2fba 11580 -- alternatives and for the right operand of a membership test. An
11581 -- others_choice is static if the corresponding expression is static.
7c0c95b8 11582 -- The staticness of the bounds is checked separately.
3b23aaa0 11583
11584 function Is_Static_Choice (N : Node_Id) return Boolean;
11585 -- Returns True if N represents a static choice (static subtype, or
a360a0f7 11586 -- static subtype indication, or static expression, or static range).
3b23aaa0 11587 --
11588 -- Note that this is a bit more inclusive than we actually need
11589 -- (in particular membership tests do not allow the use of subtype
a360a0f7 11590 -- indications). But that doesn't matter, we have already checked
3b23aaa0 11591 -- that the construct is legal to get this far.
11592
11593 function Is_Type_Ref (N : Node_Id) return Boolean;
11594 pragma Inline (Is_Type_Ref);
973c2fba 11595 -- Returns True if N is a reference to the type for the predicate in the
11596 -- expression (i.e. if it is an identifier whose Chars field matches the
11597 -- Nam given in the call). N must not be parenthesized, if the type name
11598 -- appears in parens, this routine will return False.
3b23aaa0 11599
11600 ----------------------------------
11601 -- All_Static_Case_Alternatives --
11602 ----------------------------------
11603
11604 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11605 N : Node_Id;
11606
11607 begin
11608 N := First (L);
11609 while Present (N) loop
11610 if not (All_Static_Choices (Discrete_Choices (N))
11611 and then Is_OK_Static_Expression (Expression (N)))
11612 then
11613 return False;
11614 end if;
11615
11616 Next (N);
11617 end loop;
11618
11619 return True;
11620 end All_Static_Case_Alternatives;
11621
11622 ------------------------
11623 -- All_Static_Choices --
11624 ------------------------
11625
11626 function All_Static_Choices (L : List_Id) return Boolean is
11627 N : Node_Id;
11628
11629 begin
11630 N := First (L);
11631 while Present (N) loop
11632 if not Is_Static_Choice (N) then
11633 return False;
11634 end if;
11635
11636 Next (N);
11637 end loop;
11638
11639 return True;
11640 end All_Static_Choices;
11641
11642 ----------------------
11643 -- Is_Static_Choice --
11644 ----------------------
11645
11646 function Is_Static_Choice (N : Node_Id) return Boolean is
11647 begin
7c0c95b8 11648 return Nkind (N) = N_Others_Choice
11649 or else Is_OK_Static_Expression (N)
3b23aaa0 11650 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11651 and then Is_OK_Static_Subtype (Entity (N)))
11652 or else (Nkind (N) = N_Subtype_Indication
11653 and then Is_OK_Static_Subtype (Entity (N)))
11654 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11655 end Is_Static_Choice;
11656
11657 -----------------
11658 -- Is_Type_Ref --
11659 -----------------
11660
11661 function Is_Type_Ref (N : Node_Id) return Boolean is
11662 begin
11663 return Nkind (N) = N_Identifier
11664 and then Chars (N) = Nam
11665 and then Paren_Count (N) = 0;
11666 end Is_Type_Ref;
11667
11668 -- Start of processing for Is_Predicate_Static
11669
11670 begin
3b23aaa0 11671 -- Predicate_Static means one of the following holds. Numbers are the
11672 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11673
11674 -- 16: A static expression
11675
11676 if Is_OK_Static_Expression (Expr) then
11677 return True;
11678
11679 -- 17: A membership test whose simple_expression is the current
11680 -- instance, and whose membership_choice_list meets the requirements
11681 -- for a static membership test.
11682
11683 elsif Nkind (Expr) in N_Membership_Test
11684 and then ((Present (Right_Opnd (Expr))
11685 and then Is_Static_Choice (Right_Opnd (Expr)))
11686 or else
11687 (Present (Alternatives (Expr))
11688 and then All_Static_Choices (Alternatives (Expr))))
11689 then
11690 return True;
11691
11692 -- 18. A case_expression whose selecting_expression is the current
11693 -- instance, and whose dependent expressions are static expressions.
11694
11695 elsif Nkind (Expr) = N_Case_Expression
11696 and then Is_Type_Ref (Expression (Expr))
11697 and then All_Static_Case_Alternatives (Alternatives (Expr))
11698 then
11699 return True;
11700
11701 -- 19. A call to a predefined equality or ordering operator, where one
11702 -- operand is the current instance, and the other is a static
11703 -- expression.
11704
94d896aa 11705 -- Note: the RM is clearly wrong here in not excluding string types.
11706 -- Without this exclusion, we would allow expressions like X > "ABC"
11707 -- to be considered as predicate-static, which is clearly not intended,
11708 -- since the idea is for predicate-static to be a subset of normal
11709 -- static expressions (and "DEF" > "ABC" is not a static expression).
11710
11711 -- However, we do allow internally generated (not from source) equality
11712 -- and inequality operations to be valid on strings (this helps deal
11713 -- with cases where we transform A in "ABC" to A = "ABC).
11714
3b23aaa0 11715 elsif Nkind (Expr) in N_Op_Compare
94d896aa 11716 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11717 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11718 and then not Comes_From_Source (Expr)))
3b23aaa0 11719 and then ((Is_Type_Ref (Left_Opnd (Expr))
11720 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11721 or else
11722 (Is_Type_Ref (Right_Opnd (Expr))
11723 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11724 then
11725 return True;
11726
11727 -- 20. A call to a predefined boolean logical operator, where each
11728 -- operand is predicate-static.
11729
11730 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11731 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11732 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11733 or else
11734 (Nkind (Expr) = N_Op_Not
11735 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11736 then
11737 return True;
11738
11739 -- 21. A short-circuit control form where both operands are
11740 -- predicate-static.
11741
11742 elsif Nkind (Expr) in N_Short_Circuit
11743 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11744 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11745 then
11746 return True;
11747
11748 -- 22. A parenthesized predicate-static expression. This does not
11749 -- require any special test, since we just ignore paren levels in
11750 -- all the cases above.
11751
11752 -- One more test that is an implementation artifact caused by the fact
499918a7 11753 -- that we are analyzing not the original expression, but the generated
3b23aaa0 11754 -- expression in the body of the predicate function. This can include
a360a0f7 11755 -- references to inherited predicates, so that the expression we are
3b23aaa0 11756 -- processing looks like:
11757
11758 -- expression and then xxPredicate (typ (Inns))
11759
11760 -- Where the call is to a Predicate function for an inherited predicate.
60a4a5af 11761 -- We simply ignore such a call, which could be to either a dynamic or
11762 -- a static predicate. Note that if the parent predicate is dynamic then
11763 -- eventually this type will be marked as dynamic, but you are allowed
11764 -- to specify a static predicate for a subtype which is inheriting a
11765 -- dynamic predicate, so the static predicate validation here ignores
11766 -- the inherited predicate even if it is dynamic.
3b23aaa0 11767
11768 elsif Nkind (Expr) = N_Function_Call
11769 and then Is_Predicate_Function (Entity (Name (Expr)))
11770 then
11771 return True;
11772
11773 -- That's an exhaustive list of tests, all other cases are not
a360a0f7 11774 -- predicate-static, so we return False.
3b23aaa0 11775
11776 else
11777 return False;
11778 end if;
11779 end Is_Predicate_Static;
11780
2ff55065 11781 ---------------------
11782 -- Kill_Rep_Clause --
11783 ---------------------
11784
11785 procedure Kill_Rep_Clause (N : Node_Id) is
11786 begin
11787 pragma Assert (Ignore_Rep_Clauses);
360f426f 11788
11789 -- Note: we use Replace rather than Rewrite, because we don't want
11790 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11791 -- rep clause that is being replaced.
11792
4949ddd5 11793 Replace (N, Make_Null_Statement (Sloc (N)));
360f426f 11794
11795 -- The null statement must be marked as not coming from source. This is
37c6552c 11796 -- so that ASIS ignores it, and also the back end does not expect bogus
360f426f 11797 -- "from source" null statements in weird places (e.g. in declarative
11798 -- regions where such null statements are not allowed).
11799
11800 Set_Comes_From_Source (N, False);
2ff55065 11801 end Kill_Rep_Clause;
11802
d6f39728 11803 ------------------
11804 -- Minimum_Size --
11805 ------------------
11806
11807 function Minimum_Size
11808 (T : Entity_Id;
d5b349fa 11809 Biased : Boolean := False) return Nat
d6f39728 11810 is
11811 Lo : Uint := No_Uint;
11812 Hi : Uint := No_Uint;
11813 LoR : Ureal := No_Ureal;
11814 HiR : Ureal := No_Ureal;
11815 LoSet : Boolean := False;
11816 HiSet : Boolean := False;
11817 B : Uint;
11818 S : Nat;
11819 Ancest : Entity_Id;
f15731c4 11820 R_Typ : constant Entity_Id := Root_Type (T);
d6f39728 11821
11822 begin
11823 -- If bad type, return 0
11824
11825 if T = Any_Type then
11826 return 0;
11827
11828 -- For generic types, just return zero. There cannot be any legitimate
11829 -- need to know such a size, but this routine may be called with a
11830 -- generic type as part of normal processing.
11831
f02a9a9a 11832 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
d6f39728 11833 return 0;
11834
74c7ae52 11835 -- Access types (cannot have size smaller than System.Address)
d6f39728 11836
11837 elsif Is_Access_Type (T) then
74c7ae52 11838 return System_Address_Size;
d6f39728 11839
11840 -- Floating-point types
11841
11842 elsif Is_Floating_Point_Type (T) then
f15731c4 11843 return UI_To_Int (Esize (R_Typ));
d6f39728 11844
11845 -- Discrete types
11846
11847 elsif Is_Discrete_Type (T) then
11848
fdd294d1 11849 -- The following loop is looking for the nearest compile time known
11850 -- bounds following the ancestor subtype chain. The idea is to find
11851 -- the most restrictive known bounds information.
d6f39728 11852
11853 Ancest := T;
11854 loop
11855 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11856 return 0;
11857 end if;
11858
11859 if not LoSet then
11860 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11861 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11862 LoSet := True;
11863 exit when HiSet;
11864 end if;
11865 end if;
11866
11867 if not HiSet then
11868 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11869 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11870 HiSet := True;
11871 exit when LoSet;
11872 end if;
11873 end if;
11874
11875 Ancest := Ancestor_Subtype (Ancest);
11876
11877 if No (Ancest) then
11878 Ancest := Base_Type (T);
11879
11880 if Is_Generic_Type (Ancest) then
11881 return 0;
11882 end if;
11883 end if;
11884 end loop;
11885
11886 -- Fixed-point types. We can't simply use Expr_Value to get the
fdd294d1 11887 -- Corresponding_Integer_Value values of the bounds, since these do not
11888 -- get set till the type is frozen, and this routine can be called
11889 -- before the type is frozen. Similarly the test for bounds being static
11890 -- needs to include the case where we have unanalyzed real literals for
11891 -- the same reason.
d6f39728 11892
11893 elsif Is_Fixed_Point_Type (T) then
11894
fdd294d1 11895 -- The following loop is looking for the nearest compile time known
11896 -- bounds following the ancestor subtype chain. The idea is to find
11897 -- the most restrictive known bounds information.
d6f39728 11898
11899 Ancest := T;
11900 loop
11901 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11902 return 0;
11903 end if;
11904
3062c401 11905 -- Note: In the following two tests for LoSet and HiSet, it may
11906 -- seem redundant to test for N_Real_Literal here since normally
11907 -- one would assume that the test for the value being known at
11908 -- compile time includes this case. However, there is a glitch.
11909 -- If the real literal comes from folding a non-static expression,
11910 -- then we don't consider any non- static expression to be known
11911 -- at compile time if we are in configurable run time mode (needed
11912 -- in some cases to give a clearer definition of what is and what
11913 -- is not accepted). So the test is indeed needed. Without it, we
11914 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11915
d6f39728 11916 if not LoSet then
11917 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11918 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11919 then
11920 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11921 LoSet := True;
11922 exit when HiSet;
11923 end if;
11924 end if;
11925
11926 if not HiSet then
11927 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11928 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11929 then
11930 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11931 HiSet := True;
11932 exit when LoSet;
11933 end if;
11934 end if;
11935
11936 Ancest := Ancestor_Subtype (Ancest);
11937
11938 if No (Ancest) then
11939 Ancest := Base_Type (T);
11940
11941 if Is_Generic_Type (Ancest) then
11942 return 0;
11943 end if;
11944 end if;
11945 end loop;
11946
11947 Lo := UR_To_Uint (LoR / Small_Value (T));
11948 Hi := UR_To_Uint (HiR / Small_Value (T));
11949
11950 -- No other types allowed
11951
11952 else
11953 raise Program_Error;
11954 end if;
11955
2866d595 11956 -- Fall through with Hi and Lo set. Deal with biased case
d6f39728 11957
cc46ff4b 11958 if (Biased
11959 and then not Is_Fixed_Point_Type (T)
11960 and then not (Is_Enumeration_Type (T)
11961 and then Has_Non_Standard_Rep (T)))
d6f39728 11962 or else Has_Biased_Representation (T)
11963 then
11964 Hi := Hi - Lo;
11965 Lo := Uint_0;
11966 end if;
11967
005366f7 11968 -- Null range case, size is always zero. We only do this in the discrete
11969 -- type case, since that's the odd case that came up. Probably we should
11970 -- also do this in the fixed-point case, but doing so causes peculiar
11971 -- gigi failures, and it is not worth worrying about this incredibly
11972 -- marginal case (explicit null-range fixed-point type declarations)???
11973
11974 if Lo > Hi and then Is_Discrete_Type (T) then
11975 S := 0;
11976
d6f39728 11977 -- Signed case. Note that we consider types like range 1 .. -1 to be
fdd294d1 11978 -- signed for the purpose of computing the size, since the bounds have
1a34e48c 11979 -- to be accommodated in the base type.
d6f39728 11980
005366f7 11981 elsif Lo < 0 or else Hi < 0 then
d6f39728 11982 S := 1;
11983 B := Uint_1;
11984
da253936 11985 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11986 -- Note that we accommodate the case where the bounds cross. This
d6f39728 11987 -- can happen either because of the way the bounds are declared
11988 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11989
11990 while Lo < -B
11991 or else Hi < -B
11992 or else Lo >= B
11993 or else Hi >= B
11994 loop
11995 B := Uint_2 ** S;
11996 S := S + 1;
11997 end loop;
11998
11999 -- Unsigned case
12000
12001 else
12002 -- If both bounds are positive, make sure that both are represen-
12003 -- table in the case where the bounds are crossed. This can happen
12004 -- either because of the way the bounds are declared, or because of
12005 -- the algorithm in Freeze_Fixed_Point_Type.
12006
12007 if Lo > Hi then
12008 Hi := Lo;
12009 end if;
12010
da253936 12011 -- S = size, (can accommodate 0 .. (2**size - 1))
d6f39728 12012
12013 S := 0;
12014 while Hi >= Uint_2 ** S loop
12015 S := S + 1;
12016 end loop;
12017 end if;
12018
12019 return S;
12020 end Minimum_Size;
12021
44e4341e 12022 ---------------------------
12023 -- New_Stream_Subprogram --
12024 ---------------------------
d6f39728 12025
44e4341e 12026 procedure New_Stream_Subprogram
12027 (N : Node_Id;
12028 Ent : Entity_Id;
12029 Subp : Entity_Id;
12030 Nam : TSS_Name_Type)
d6f39728 12031 is
12032 Loc : constant Source_Ptr := Sloc (N);
9dfe12ae 12033 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
f15731c4 12034 Subp_Id : Entity_Id;
d6f39728 12035 Subp_Decl : Node_Id;
12036 F : Entity_Id;
12037 Etyp : Entity_Id;
12038
44e4341e 12039 Defer_Declaration : constant Boolean :=
12040 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
12041 -- For a tagged type, there is a declaration for each stream attribute
12042 -- at the freeze point, and we must generate only a completion of this
12043 -- declaration. We do the same for private types, because the full view
12044 -- might be tagged. Otherwise we generate a declaration at the point of
12045 -- the attribute definition clause.
12046
f15731c4 12047 function Build_Spec return Node_Id;
12048 -- Used for declaration and renaming declaration, so that this is
12049 -- treated as a renaming_as_body.
12050
12051 ----------------
12052 -- Build_Spec --
12053 ----------------
12054
d5b349fa 12055 function Build_Spec return Node_Id is
44e4341e 12056 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
12057 Formals : List_Id;
12058 Spec : Node_Id;
83c6c069 12059 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
44e4341e 12060
f15731c4 12061 begin
9dfe12ae 12062 Subp_Id := Make_Defining_Identifier (Loc, Sname);
f15731c4 12063
44e4341e 12064 -- S : access Root_Stream_Type'Class
12065
12066 Formals := New_List (
12067 Make_Parameter_Specification (Loc,
12068 Defining_Identifier =>
12069 Make_Defining_Identifier (Loc, Name_S),
12070 Parameter_Type =>
12071 Make_Access_Definition (Loc,
12072 Subtype_Mark =>
83c6c069 12073 New_Occurrence_Of (
44e4341e 12074 Designated_Type (Etype (F)), Loc))));
12075
12076 if Nam = TSS_Stream_Input then
4bba0a8d 12077 Spec :=
12078 Make_Function_Specification (Loc,
12079 Defining_Unit_Name => Subp_Id,
12080 Parameter_Specifications => Formals,
12081 Result_Definition => T_Ref);
44e4341e 12082 else
12083 -- V : [out] T
f15731c4 12084
44e4341e 12085 Append_To (Formals,
12086 Make_Parameter_Specification (Loc,
12087 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
12088 Out_Present => Out_P,
12089 Parameter_Type => T_Ref));
f15731c4 12090
d3ef794c 12091 Spec :=
12092 Make_Procedure_Specification (Loc,
12093 Defining_Unit_Name => Subp_Id,
12094 Parameter_Specifications => Formals);
44e4341e 12095 end if;
f15731c4 12096
44e4341e 12097 return Spec;
12098 end Build_Spec;
d6f39728 12099
44e4341e 12100 -- Start of processing for New_Stream_Subprogram
d6f39728 12101
44e4341e 12102 begin
12103 F := First_Formal (Subp);
12104
12105 if Ekind (Subp) = E_Procedure then
12106 Etyp := Etype (Next_Formal (F));
d6f39728 12107 else
44e4341e 12108 Etyp := Etype (Subp);
d6f39728 12109 end if;
f15731c4 12110
44e4341e 12111 -- Prepare subprogram declaration and insert it as an action on the
12112 -- clause node. The visibility for this entity is used to test for
12113 -- visibility of the attribute definition clause (in the sense of
12114 -- 8.3(23) as amended by AI-195).
9dfe12ae 12115
44e4341e 12116 if not Defer_Declaration then
f15731c4 12117 Subp_Decl :=
12118 Make_Subprogram_Declaration (Loc,
12119 Specification => Build_Spec);
44e4341e 12120
12121 -- For a tagged type, there is always a visible declaration for each
15ebb600 12122 -- stream TSS (it is a predefined primitive operation), and the
44e4341e 12123 -- completion of this declaration occurs at the freeze point, which is
12124 -- not always visible at places where the attribute definition clause is
12125 -- visible. So, we create a dummy entity here for the purpose of
12126 -- tracking the visibility of the attribute definition clause itself.
12127
12128 else
12129 Subp_Id :=
55868293 12130 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
44e4341e 12131 Subp_Decl :=
12132 Make_Object_Declaration (Loc,
12133 Defining_Identifier => Subp_Id,
12134 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
f15731c4 12135 end if;
12136
44e4341e 12137 Insert_Action (N, Subp_Decl);
12138 Set_Entity (N, Subp_Id);
12139
d6f39728 12140 Subp_Decl :=
12141 Make_Subprogram_Renaming_Declaration (Loc,
f15731c4 12142 Specification => Build_Spec,
83c6c069 12143 Name => New_Occurrence_Of (Subp, Loc));
d6f39728 12144
44e4341e 12145 if Defer_Declaration then
d6f39728 12146 Set_TSS (Base_Type (Ent), Subp_Id);
12147 else
12148 Insert_Action (N, Subp_Decl);
12149 Copy_TSS (Subp_Id, Base_Type (Ent));
12150 end if;
44e4341e 12151 end New_Stream_Subprogram;
d6f39728 12152
2625eb01 12153 ------------------------------------------
12154 -- Push_Scope_And_Install_Discriminants --
12155 ------------------------------------------
12156
12157 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12158 begin
12159 if Has_Discriminants (E) then
12160 Push_Scope (E);
12161
12162 -- Make discriminants visible for type declarations and protected
12163 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12164
12165 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12166 Install_Discriminants (E);
12167 end if;
12168 end if;
12169 end Push_Scope_And_Install_Discriminants;
12170
d6f39728 12171 ------------------------
12172 -- Rep_Item_Too_Early --
12173 ------------------------
12174
80d4fec4 12175 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
d6f39728 12176 begin
44e4341e 12177 -- Cannot apply non-operational rep items to generic types
d6f39728 12178
f15731c4 12179 if Is_Operational_Item (N) then
12180 return False;
12181
12182 elsif Is_Type (T)
d6f39728 12183 and then Is_Generic_Type (Root_Type (T))
e17c5076 12184 and then (Nkind (N) /= N_Pragma
12185 or else Get_Pragma_Id (N) /= Pragma_Convention)
d6f39728 12186 then
503f7fd3 12187 Error_Msg_N ("representation item not allowed for generic type", N);
d6f39728 12188 return True;
12189 end if;
12190
fdd294d1 12191 -- Otherwise check for incomplete type
d6f39728 12192
12193 if Is_Incomplete_Or_Private_Type (T)
12194 and then No (Underlying_Type (T))
d64221a7 12195 and then
12196 (Nkind (N) /= N_Pragma
60014bc9 12197 or else Get_Pragma_Id (N) /= Pragma_Import)
d6f39728 12198 then
12199 Error_Msg_N
12200 ("representation item must be after full type declaration", N);
12201 return True;
12202
1a34e48c 12203 -- If the type has incomplete components, a representation clause is
d6f39728 12204 -- illegal but stream attributes and Convention pragmas are correct.
12205
12206 elsif Has_Private_Component (T) then
f15731c4 12207 if Nkind (N) = N_Pragma then
d6f39728 12208 return False;
b9e61b2a 12209
d6f39728 12210 else
12211 Error_Msg_N
12212 ("representation item must appear after type is fully defined",
12213 N);
12214 return True;
12215 end if;
12216 else
12217 return False;
12218 end if;
12219 end Rep_Item_Too_Early;
12220
12221 -----------------------
12222 -- Rep_Item_Too_Late --
12223 -----------------------
12224
12225 function Rep_Item_Too_Late
12226 (T : Entity_Id;
12227 N : Node_Id;
d5b349fa 12228 FOnly : Boolean := False) return Boolean
d6f39728 12229 is
12230 S : Entity_Id;
12231 Parent_Type : Entity_Id;
12232
4d0944e9 12233 procedure No_Type_Rep_Item;
12234 -- Output message indicating that no type-related aspects can be
12235 -- specified due to some property of the parent type.
12236
d6f39728 12237 procedure Too_Late;
4d0944e9 12238 -- Output message for an aspect being specified too late
12239
12240 -- Note that neither of the above errors is considered a serious one,
12241 -- since the effect is simply that we ignore the representation clause
12242 -- in these cases.
04d38ee4 12243 -- Is this really true? In any case if we make this change we must
12244 -- document the requirement in the spec of Rep_Item_Too_Late that
12245 -- if True is returned, then the rep item must be completely ignored???
4d0944e9 12246
12247 ----------------------
12248 -- No_Type_Rep_Item --
12249 ----------------------
12250
12251 procedure No_Type_Rep_Item is
12252 begin
12253 Error_Msg_N ("|type-related representation item not permitted!", N);
12254 end No_Type_Rep_Item;
d53a018a 12255
12256 --------------
12257 -- Too_Late --
12258 --------------
d6f39728 12259
12260 procedure Too_Late is
12261 begin
ce4da1ed 12262 -- Other compilers seem more relaxed about rep items appearing too
12263 -- late. Since analysis tools typically don't care about rep items
12264 -- anyway, no reason to be too strict about this.
12265
a9cd517c 12266 if not Relaxed_RM_Semantics then
12267 Error_Msg_N ("|representation item appears too late!", N);
12268 end if;
d6f39728 12269 end Too_Late;
12270
12271 -- Start of processing for Rep_Item_Too_Late
12272
12273 begin
a3248fc4 12274 -- First make sure entity is not frozen (RM 13.1(9))
d6f39728 12275
12276 if Is_Frozen (T)
a3248fc4 12277
12278 -- Exclude imported types, which may be frozen if they appear in a
12279 -- representation clause for a local type.
12280
4aa270d8 12281 and then not From_Limited_With (T)
a3248fc4 12282
a9cd517c 12283 -- Exclude generated entities (not coming from source). The common
a3248fc4 12284 -- case is when we generate a renaming which prematurely freezes the
12285 -- renamed internal entity, but we still want to be able to set copies
12286 -- of attribute values such as Size/Alignment.
12287
12288 and then Comes_From_Source (T)
d6f39728 12289 then
12290 Too_Late;
12291 S := First_Subtype (T);
12292
12293 if Present (Freeze_Node (S)) then
04d38ee4 12294 if not Relaxed_RM_Semantics then
12295 Error_Msg_NE
12296 ("??no more representation items for }", Freeze_Node (S), S);
12297 end if;
d6f39728 12298 end if;
12299
12300 return True;
12301
d1a2e31b 12302 -- Check for case of untagged derived type whose parent either has
4d0944e9 12303 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12304 -- this case we do not output a Too_Late message, since there is no
12305 -- earlier point where the rep item could be placed to make it legal.
d6f39728 12306
12307 elsif Is_Type (T)
12308 and then not FOnly
12309 and then Is_Derived_Type (T)
12310 and then not Is_Tagged_Type (T)
12311 then
12312 Parent_Type := Etype (Base_Type (T));
12313
12314 if Has_Primitive_Operations (Parent_Type) then
4d0944e9 12315 No_Type_Rep_Item;
04d38ee4 12316
12317 if not Relaxed_RM_Semantics then
12318 Error_Msg_NE
12319 ("\parent type & has primitive operations!", N, Parent_Type);
12320 end if;
12321
d6f39728 12322 return True;
12323
12324 elsif Is_By_Reference_Type (Parent_Type) then
4d0944e9 12325 No_Type_Rep_Item;
04d38ee4 12326
12327 if not Relaxed_RM_Semantics then
12328 Error_Msg_NE
12329 ("\parent type & is a by reference type!", N, Parent_Type);
12330 end if;
12331
d6f39728 12332 return True;
12333 end if;
12334 end if;
12335
04d38ee4 12336 -- No error, but one more warning to consider. The RM (surprisingly)
12337 -- allows this pattern:
12338
12339 -- type S is ...
12340 -- primitive operations for S
12341 -- type R is new S;
12342 -- rep clause for S
12343
12344 -- Meaning that calls on the primitive operations of S for values of
12345 -- type R may require possibly expensive implicit conversion operations.
12346 -- This is not an error, but is worth a warning.
12347
12348 if not Relaxed_RM_Semantics and then Is_Type (T) then
12349 declare
12350 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12351
12352 begin
12353 if Present (DTL)
12354 and then Has_Primitive_Operations (Base_Type (T))
12355
12356 -- For now, do not generate this warning for the case of aspect
12357 -- specification using Ada 2012 syntax, since we get wrong
12358 -- messages we do not understand. The whole business of derived
12359 -- types and rep items seems a bit confused when aspects are
12360 -- used, since the aspects are not evaluated till freeze time.
12361
12362 and then not From_Aspect_Specification (N)
12363 then
12364 Error_Msg_Sloc := Sloc (DTL);
12365 Error_Msg_N
12366 ("representation item for& appears after derived type "
12367 & "declaration#??", N);
12368 Error_Msg_NE
12369 ("\may result in implicit conversions for primitive "
12370 & "operations of&??", N, T);
12371 Error_Msg_NE
12372 ("\to change representations when called with arguments "
12373 & "of type&??", N, DTL);
12374 end if;
12375 end;
12376 end if;
12377
3062c401 12378 -- No error, link item into head of chain of rep items for the entity,
12379 -- but avoid chaining if we have an overloadable entity, and the pragma
12380 -- is one that can apply to multiple overloaded entities.
12381
b9e61b2a 12382 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
fdd294d1 12383 declare
12384 Pname : constant Name_Id := Pragma_Name (N);
12385 begin
18393965 12386 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12387 Name_External, Name_Interface)
fdd294d1 12388 then
12389 return False;
12390 end if;
12391 end;
3062c401 12392 end if;
12393
fdd294d1 12394 Record_Rep_Item (T, N);
d6f39728 12395 return False;
12396 end Rep_Item_Too_Late;
12397
2072eaa9 12398 -------------------------------------
12399 -- Replace_Type_References_Generic --
12400 -------------------------------------
12401
37c6552c 12402 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12403 TName : constant Name_Id := Chars (T);
2072eaa9 12404
12405 function Replace_Node (N : Node_Id) return Traverse_Result;
12406 -- Processes a single node in the traversal procedure below, checking
12407 -- if node N should be replaced, and if so, doing the replacement.
12408
12409 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
12410 -- This instantiation provides the body of Replace_Type_References
12411
12412 ------------------
12413 -- Replace_Node --
12414 ------------------
12415
12416 function Replace_Node (N : Node_Id) return Traverse_Result is
12417 S : Entity_Id;
12418 P : Node_Id;
12419
12420 begin
12421 -- Case of identifier
12422
12423 if Nkind (N) = N_Identifier then
12424
37c6552c 12425 -- If not the type name, check whether it is a reference to
12426 -- some other type, which must be frozen before the predicate
12427 -- function is analyzed, i.e. before the freeze node of the
12428 -- type to which the predicate applies.
2072eaa9 12429
12430 if Chars (N) /= TName then
37c6552c 12431 if Present (Current_Entity (N))
12432 and then Is_Type (Current_Entity (N))
12433 then
12434 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12435 end if;
12436
2072eaa9 12437 return Skip;
12438
12439 -- Otherwise do the replacement and we are done with this node
12440
12441 else
12442 Replace_Type_Reference (N);
12443 return Skip;
12444 end if;
12445
12446 -- Case of selected component (which is what a qualification
12447 -- looks like in the unanalyzed tree, which is what we have.
12448
12449 elsif Nkind (N) = N_Selected_Component then
12450
12451 -- If selector name is not our type, keeping going (we might
12452 -- still have an occurrence of the type in the prefix).
12453
12454 if Nkind (Selector_Name (N)) /= N_Identifier
12455 or else Chars (Selector_Name (N)) /= TName
12456 then
12457 return OK;
12458
12459 -- Selector name is our type, check qualification
12460
12461 else
12462 -- Loop through scopes and prefixes, doing comparison
12463
12464 S := Current_Scope;
12465 P := Prefix (N);
12466 loop
12467 -- Continue if no more scopes or scope with no name
12468
12469 if No (S) or else Nkind (S) not in N_Has_Chars then
12470 return OK;
12471 end if;
12472
12473 -- Do replace if prefix is an identifier matching the
12474 -- scope that we are currently looking at.
12475
12476 if Nkind (P) = N_Identifier
12477 and then Chars (P) = Chars (S)
12478 then
12479 Replace_Type_Reference (N);
12480 return Skip;
12481 end if;
12482
12483 -- Go check scope above us if prefix is itself of the
12484 -- form of a selected component, whose selector matches
12485 -- the scope we are currently looking at.
12486
12487 if Nkind (P) = N_Selected_Component
12488 and then Nkind (Selector_Name (P)) = N_Identifier
12489 and then Chars (Selector_Name (P)) = Chars (S)
12490 then
12491 S := Scope (S);
12492 P := Prefix (P);
12493
12494 -- For anything else, we don't have a match, so keep on
12495 -- going, there are still some weird cases where we may
12496 -- still have a replacement within the prefix.
12497
12498 else
12499 return OK;
12500 end if;
12501 end loop;
12502 end if;
12503
ec6f6da5 12504 -- Continue for any other node kind
2072eaa9 12505
12506 else
12507 return OK;
12508 end if;
12509 end Replace_Node;
12510
12511 begin
12512 Replace_Type_Refs (N);
12513 end Replace_Type_References_Generic;
12514
81bd1c0d 12515 --------------------------------
12516 -- Resolve_Aspect_Expressions --
12517 --------------------------------
12518
12519 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12520 ASN : Node_Id;
12521 A_Id : Aspect_Id;
12522 Expr : Node_Id;
12523
12524 begin
12525 ASN := First_Rep_Item (E);
12526 while Present (ASN) loop
12527 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12528 A_Id := Get_Aspect_Id (ASN);
12529 Expr := Expression (ASN);
12530
12531 case A_Id is
12532 -- For now we only deal with aspects that do not generate
12533 -- subprograms, or that may mention current instances of
12534 -- types. These will require special handling (TBD).
12535
12536 when Aspect_Predicate |
12537 Aspect_Invariant |
12538 Aspect_Static_Predicate |
12539 Aspect_Dynamic_Predicate =>
12540 null;
12541
12542 when Pre_Post_Aspects =>
12543 null;
12544
12545 when Aspect_Iterable =>
12546 if Nkind (Expr) = N_Aggregate then
12547 declare
12548 Assoc : Node_Id;
12549
12550 begin
12551 Assoc := First (Component_Associations (Expr));
12552 while Present (Assoc) loop
12553 Find_Direct_Name (Expression (Assoc));
12554 Next (Assoc);
12555 end loop;
12556 end;
12557 end if;
12558
12559 when others =>
12560 if Present (Expr) then
12561 case Aspect_Argument (A_Id) is
12562 when Expression | Optional_Expression =>
12563 Analyze_And_Resolve (Expression (ASN));
12564
12565 when Name | Optional_Name =>
12566 if Nkind (Expr) = N_Identifier then
12567 Find_Direct_Name (Expr);
12568
12569 elsif Nkind (Expr) = N_Selected_Component then
12570 Find_Selected_Component (Expr);
12571
12572 else
12573 null;
12574 end if;
12575 end case;
12576 end if;
12577 end case;
12578 end if;
12579
a738763e 12580 ASN := Next_Rep_Item (ASN);
81bd1c0d 12581 end loop;
12582 end Resolve_Aspect_Expressions;
12583
d6f39728 12584 -------------------------
12585 -- Same_Representation --
12586 -------------------------
12587
12588 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12589 T1 : constant Entity_Id := Underlying_Type (Typ1);
12590 T2 : constant Entity_Id := Underlying_Type (Typ2);
12591
12592 begin
12593 -- A quick check, if base types are the same, then we definitely have
12594 -- the same representation, because the subtype specific representation
12595 -- attributes (Size and Alignment) do not affect representation from
12596 -- the point of view of this test.
12597
12598 if Base_Type (T1) = Base_Type (T2) then
12599 return True;
12600
12601 elsif Is_Private_Type (Base_Type (T2))
12602 and then Base_Type (T1) = Full_View (Base_Type (T2))
12603 then
12604 return True;
12605 end if;
12606
12607 -- Tagged types never have differing representations
12608
12609 if Is_Tagged_Type (T1) then
12610 return True;
12611 end if;
12612
12613 -- Representations are definitely different if conventions differ
12614
12615 if Convention (T1) /= Convention (T2) then
12616 return False;
12617 end if;
12618
ef0772bc 12619 -- Representations are different if component alignments or scalar
12620 -- storage orders differ.
d6f39728 12621
12622 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
726fd56a 12623 and then
d6f39728 12624 (Is_Record_Type (T2) or else Is_Array_Type (T2))
ef0772bc 12625 and then
12626 (Component_Alignment (T1) /= Component_Alignment (T2)
f02a9a9a 12627 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
d6f39728 12628 then
12629 return False;
12630 end if;
12631
12632 -- For arrays, the only real issue is component size. If we know the
12633 -- component size for both arrays, and it is the same, then that's
12634 -- good enough to know we don't have a change of representation.
12635
12636 if Is_Array_Type (T1) then
12637 if Known_Component_Size (T1)
12638 and then Known_Component_Size (T2)
12639 and then Component_Size (T1) = Component_Size (T2)
12640 then
36ac5fbb 12641 return True;
d6f39728 12642 end if;
12643 end if;
12644
12645 -- Types definitely have same representation if neither has non-standard
12646 -- representation since default representations are always consistent.
12647 -- If only one has non-standard representation, and the other does not,
12648 -- then we consider that they do not have the same representation. They
12649 -- might, but there is no way of telling early enough.
12650
12651 if Has_Non_Standard_Rep (T1) then
12652 if not Has_Non_Standard_Rep (T2) then
12653 return False;
12654 end if;
12655 else
12656 return not Has_Non_Standard_Rep (T2);
12657 end if;
12658
fdd294d1 12659 -- Here the two types both have non-standard representation, and we need
12660 -- to determine if they have the same non-standard representation.
d6f39728 12661
12662 -- For arrays, we simply need to test if the component sizes are the
12663 -- same. Pragma Pack is reflected in modified component sizes, so this
12664 -- check also deals with pragma Pack.
12665
12666 if Is_Array_Type (T1) then
12667 return Component_Size (T1) = Component_Size (T2);
12668
12669 -- Tagged types always have the same representation, because it is not
12670 -- possible to specify different representations for common fields.
12671
12672 elsif Is_Tagged_Type (T1) then
12673 return True;
12674
12675 -- Case of record types
12676
12677 elsif Is_Record_Type (T1) then
12678
12679 -- Packed status must conform
12680
12681 if Is_Packed (T1) /= Is_Packed (T2) then
12682 return False;
12683
12684 -- Otherwise we must check components. Typ2 maybe a constrained
12685 -- subtype with fewer components, so we compare the components
12686 -- of the base types.
12687
12688 else
12689 Record_Case : declare
12690 CD1, CD2 : Entity_Id;
12691
12692 function Same_Rep return Boolean;
12693 -- CD1 and CD2 are either components or discriminants. This
ef0772bc 12694 -- function tests whether they have the same representation.
d6f39728 12695
80d4fec4 12696 --------------
12697 -- Same_Rep --
12698 --------------
12699
d6f39728 12700 function Same_Rep return Boolean is
12701 begin
12702 if No (Component_Clause (CD1)) then
12703 return No (Component_Clause (CD2));
d6f39728 12704 else
ef0772bc 12705 -- Note: at this point, component clauses have been
12706 -- normalized to the default bit order, so that the
12707 -- comparison of Component_Bit_Offsets is meaningful.
12708
d6f39728 12709 return
12710 Present (Component_Clause (CD2))
12711 and then
12712 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12713 and then
12714 Esize (CD1) = Esize (CD2);
12715 end if;
12716 end Same_Rep;
12717
1e35409d 12718 -- Start of processing for Record_Case
d6f39728 12719
12720 begin
12721 if Has_Discriminants (T1) then
d6f39728 12722
9dfe12ae 12723 -- The number of discriminants may be different if the
12724 -- derived type has fewer (constrained by values). The
12725 -- invisible discriminants retain the representation of
12726 -- the original, so the discrepancy does not per se
12727 -- indicate a different representation.
12728
b9e61b2a 12729 CD1 := First_Discriminant (T1);
12730 CD2 := First_Discriminant (T2);
12731 while Present (CD1) and then Present (CD2) loop
d6f39728 12732 if not Same_Rep then
12733 return False;
12734 else
12735 Next_Discriminant (CD1);
12736 Next_Discriminant (CD2);
12737 end if;
12738 end loop;
12739 end if;
12740
12741 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12742 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
d6f39728 12743 while Present (CD1) loop
12744 if not Same_Rep then
12745 return False;
12746 else
12747 Next_Component (CD1);
12748 Next_Component (CD2);
12749 end if;
12750 end loop;
12751
12752 return True;
12753 end Record_Case;
12754 end if;
12755
12756 -- For enumeration types, we must check each literal to see if the
12757 -- representation is the same. Note that we do not permit enumeration
1a34e48c 12758 -- representation clauses for Character and Wide_Character, so these
d6f39728 12759 -- cases were already dealt with.
12760
12761 elsif Is_Enumeration_Type (T1) then
d6f39728 12762 Enumeration_Case : declare
12763 L1, L2 : Entity_Id;
12764
12765 begin
12766 L1 := First_Literal (T1);
12767 L2 := First_Literal (T2);
d6f39728 12768 while Present (L1) loop
12769 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12770 return False;
12771 else
12772 Next_Literal (L1);
12773 Next_Literal (L2);
12774 end if;
12775 end loop;
12776
12777 return True;
d6f39728 12778 end Enumeration_Case;
12779
12780 -- Any other types have the same representation for these purposes
12781
12782 else
12783 return True;
12784 end if;
d6f39728 12785 end Same_Representation;
12786
3061ffde 12787 --------------------------------
12788 -- Resolve_Iterable_Operation --
12789 --------------------------------
12790
12791 procedure Resolve_Iterable_Operation
12792 (N : Node_Id;
12793 Cursor : Entity_Id;
12794 Typ : Entity_Id;
12795 Nam : Name_Id)
12796 is
12797 Ent : Entity_Id;
12798 F1 : Entity_Id;
12799 F2 : Entity_Id;
12800
12801 begin
12802 if not Is_Overloaded (N) then
12803 if not Is_Entity_Name (N)
12804 or else Ekind (Entity (N)) /= E_Function
12805 or else Scope (Entity (N)) /= Scope (Typ)
12806 or else No (First_Formal (Entity (N)))
12807 or else Etype (First_Formal (Entity (N))) /= Typ
12808 then
12809 Error_Msg_N ("iterable primitive must be local function name "
12810 & "whose first formal is an iterable type", N);
a9f5fea7 12811 return;
3061ffde 12812 end if;
12813
12814 Ent := Entity (N);
12815 F1 := First_Formal (Ent);
12816 if Nam = Name_First then
12817
12818 -- First (Container) => Cursor
12819
12820 if Etype (Ent) /= Cursor then
12821 Error_Msg_N ("primitive for First must yield a curosr", N);
12822 end if;
12823
12824 elsif Nam = Name_Next then
12825
12826 -- Next (Container, Cursor) => Cursor
12827
12828 F2 := Next_Formal (F1);
12829
12830 if Etype (F2) /= Cursor
12831 or else Etype (Ent) /= Cursor
12832 or else Present (Next_Formal (F2))
12833 then
12834 Error_Msg_N ("no match for Next iterable primitive", N);
12835 end if;
12836
12837 elsif Nam = Name_Has_Element then
12838
12839 -- Has_Element (Container, Cursor) => Boolean
12840
12841 F2 := Next_Formal (F1);
12842 if Etype (F2) /= Cursor
12843 or else Etype (Ent) /= Standard_Boolean
12844 or else Present (Next_Formal (F2))
12845 then
12846 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12847 end if;
12848
12849 elsif Nam = Name_Element then
b9b03799 12850 F2 := Next_Formal (F1);
12851
12852 if No (F2)
12853 or else Etype (F2) /= Cursor
12854 or else Present (Next_Formal (F2))
12855 then
12856 Error_Msg_N ("no match for Element iterable primitive", N);
12857 end if;
3061ffde 12858 null;
12859
12860 else
12861 raise Program_Error;
12862 end if;
12863
12864 else
12865 -- Overloaded case: find subprogram with proper signature.
12866 -- Caller will report error if no match is found.
12867
12868 declare
12869 I : Interp_Index;
12870 It : Interp;
12871
12872 begin
12873 Get_First_Interp (N, I, It);
12874 while Present (It.Typ) loop
12875 if Ekind (It.Nam) = E_Function
b9b03799 12876 and then Scope (It.Nam) = Scope (Typ)
3061ffde 12877 and then Etype (First_Formal (It.Nam)) = Typ
12878 then
12879 F1 := First_Formal (It.Nam);
12880
12881 if Nam = Name_First then
12882 if Etype (It.Nam) = Cursor
12883 and then No (Next_Formal (F1))
12884 then
12885 Set_Entity (N, It.Nam);
12886 exit;
12887 end if;
12888
12889 elsif Nam = Name_Next then
12890 F2 := Next_Formal (F1);
12891
12892 if Present (F2)
12893 and then No (Next_Formal (F2))
12894 and then Etype (F2) = Cursor
12895 and then Etype (It.Nam) = Cursor
12896 then
12897 Set_Entity (N, It.Nam);
12898 exit;
12899 end if;
12900
12901 elsif Nam = Name_Has_Element then
12902 F2 := Next_Formal (F1);
12903
12904 if Present (F2)
12905 and then No (Next_Formal (F2))
12906 and then Etype (F2) = Cursor
12907 and then Etype (It.Nam) = Standard_Boolean
12908 then
12909 Set_Entity (N, It.Nam);
12910 F2 := Next_Formal (F1);
12911 exit;
12912 end if;
12913
12914 elsif Nam = Name_Element then
b9b03799 12915 F2 := Next_Formal (F1);
12916
3061ffde 12917 if Present (F2)
12918 and then No (Next_Formal (F2))
12919 and then Etype (F2) = Cursor
12920 then
12921 Set_Entity (N, It.Nam);
12922 exit;
12923 end if;
12924 end if;
12925 end if;
12926
12927 Get_Next_Interp (I, It);
12928 end loop;
12929 end;
12930 end if;
12931 end Resolve_Iterable_Operation;
12932
b77e4501 12933 ----------------
12934 -- Set_Biased --
12935 ----------------
12936
12937 procedure Set_Biased
12938 (E : Entity_Id;
12939 N : Node_Id;
12940 Msg : String;
12941 Biased : Boolean := True)
12942 is
12943 begin
12944 if Biased then
12945 Set_Has_Biased_Representation (E);
12946
12947 if Warn_On_Biased_Representation then
12948 Error_Msg_NE
1e3532e7 12949 ("?B?" & Msg & " forces biased representation for&", N, E);
b77e4501 12950 end if;
12951 end if;
12952 end Set_Biased;
12953
d6f39728 12954 --------------------
12955 -- Set_Enum_Esize --
12956 --------------------
12957
12958 procedure Set_Enum_Esize (T : Entity_Id) is
12959 Lo : Uint;
12960 Hi : Uint;
12961 Sz : Nat;
12962
12963 begin
12964 Init_Alignment (T);
12965
12966 -- Find the minimum standard size (8,16,32,64) that fits
12967
12968 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12969 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12970
12971 if Lo < 0 then
12972 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
f15731c4 12973 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 12974
12975 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12976 Sz := 16;
12977
12978 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12979 Sz := 32;
12980
12981 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12982 Sz := 64;
12983 end if;
12984
12985 else
12986 if Hi < Uint_2**08 then
f15731c4 12987 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 12988
12989 elsif Hi < Uint_2**16 then
12990 Sz := 16;
12991
12992 elsif Hi < Uint_2**32 then
12993 Sz := 32;
12994
12995 else pragma Assert (Hi < Uint_2**63);
12996 Sz := 64;
12997 end if;
12998 end if;
12999
13000 -- That minimum is the proper size unless we have a foreign convention
13001 -- and the size required is 32 or less, in which case we bump the size
13002 -- up to 32. This is required for C and C++ and seems reasonable for
13003 -- all other foreign conventions.
13004
13005 if Has_Foreign_Convention (T)
13006 and then Esize (T) < Standard_Integer_Size
db1eed69 13007
13008 -- Don't do this if Short_Enums on target
13009
e9185b9d 13010 and then not Target_Short_Enums
d6f39728 13011 then
13012 Init_Esize (T, Standard_Integer_Size);
d6f39728 13013 else
13014 Init_Esize (T, Sz);
13015 end if;
d6f39728 13016 end Set_Enum_Esize;
13017
2625eb01 13018 -----------------------------
13019 -- Uninstall_Discriminants --
13020 -----------------------------
13021
13022 procedure Uninstall_Discriminants (E : Entity_Id) is
13023 Disc : Entity_Id;
13024 Prev : Entity_Id;
13025 Outer : Entity_Id;
13026
13027 begin
13028 -- Discriminants have been made visible for type declarations and
13029 -- protected type declarations, not for subtype declarations.
13030
13031 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13032 Disc := First_Discriminant (E);
13033 while Present (Disc) loop
13034 if Disc /= Current_Entity (Disc) then
13035 Prev := Current_Entity (Disc);
13036 while Present (Prev)
13037 and then Present (Homonym (Prev))
13038 and then Homonym (Prev) /= Disc
13039 loop
13040 Prev := Homonym (Prev);
13041 end loop;
13042 else
13043 Prev := Empty;
13044 end if;
13045
13046 Set_Is_Immediately_Visible (Disc, False);
13047
13048 Outer := Homonym (Disc);
13049 while Present (Outer) and then Scope (Outer) = E loop
13050 Outer := Homonym (Outer);
13051 end loop;
13052
13053 -- Reset homonym link of other entities, but do not modify link
13054 -- between entities in current scope, so that the back-end can
13055 -- have a proper count of local overloadings.
13056
13057 if No (Prev) then
13058 Set_Name_Entity_Id (Chars (Disc), Outer);
13059
13060 elsif Scope (Prev) /= Scope (Disc) then
13061 Set_Homonym (Prev, Outer);
13062 end if;
13063
13064 Next_Discriminant (Disc);
13065 end loop;
13066 end if;
13067 end Uninstall_Discriminants;
13068
13069 -------------------------------------------
13070 -- Uninstall_Discriminants_And_Pop_Scope --
13071 -------------------------------------------
13072
13073 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13074 begin
13075 if Has_Discriminants (E) then
13076 Uninstall_Discriminants (E);
13077 Pop_Scope;
13078 end if;
13079 end Uninstall_Discriminants_And_Pop_Scope;
13080
83f8f0a6 13081 ------------------------------
13082 -- Validate_Address_Clauses --
13083 ------------------------------
13084
13085 procedure Validate_Address_Clauses is
13086 begin
13087 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13088 declare
13089 ACCR : Address_Clause_Check_Record
13090 renames Address_Clause_Checks.Table (J);
13091
d6da7448 13092 Expr : Node_Id;
13093
83f8f0a6 13094 X_Alignment : Uint;
13095 Y_Alignment : Uint;
13096
13097 X_Size : Uint;
13098 Y_Size : Uint;
13099
13100 begin
13101 -- Skip processing of this entry if warning already posted
13102
13103 if not Address_Warning_Posted (ACCR.N) then
d6da7448 13104 Expr := Original_Node (Expression (ACCR.N));
83f8f0a6 13105
d6da7448 13106 -- Get alignments
83f8f0a6 13107
d6da7448 13108 X_Alignment := Alignment (ACCR.X);
13109 Y_Alignment := Alignment (ACCR.Y);
83f8f0a6 13110
13111 -- Similarly obtain sizes
13112
d6da7448 13113 X_Size := Esize (ACCR.X);
13114 Y_Size := Esize (ACCR.Y);
83f8f0a6 13115
13116 -- Check for large object overlaying smaller one
13117
13118 if Y_Size > Uint_0
13119 and then X_Size > Uint_0
13120 and then X_Size > Y_Size
13121 then
d6da7448 13122 Error_Msg_NE
1581f2d7 13123 ("??& overlays smaller object", ACCR.N, ACCR.X);
83f8f0a6 13124 Error_Msg_N
1e3532e7 13125 ("\??program execution may be erroneous", ACCR.N);
83f8f0a6 13126 Error_Msg_Uint_1 := X_Size;
13127 Error_Msg_NE
1e3532e7 13128 ("\??size of & is ^", ACCR.N, ACCR.X);
83f8f0a6 13129 Error_Msg_Uint_1 := Y_Size;
13130 Error_Msg_NE
1e3532e7 13131 ("\??size of & is ^", ACCR.N, ACCR.Y);
83f8f0a6 13132
d6da7448 13133 -- Check for inadequate alignment, both of the base object
e556831e 13134 -- and of the offset, if any. We only do this check if the
13135 -- run-time Alignment_Check is active. No point in warning
13136 -- if this check has been suppressed (or is suppressed by
13137 -- default in the non-strict alignment machine case).
83f8f0a6 13138
d6da7448 13139 -- Note: we do not check the alignment if we gave a size
13140 -- warning, since it would likely be redundant.
83f8f0a6 13141
e556831e 13142 elsif not Alignment_Checks_Suppressed (ACCR.Y)
13143 and then Y_Alignment /= Uint_0
d6da7448 13144 and then (Y_Alignment < X_Alignment
13145 or else (ACCR.Off
13146 and then
13147 Nkind (Expr) = N_Attribute_Reference
13148 and then
13149 Attribute_Name (Expr) = Name_Address
13150 and then
13151 Has_Compatible_Alignment
13152 (ACCR.X, Prefix (Expr))
13153 /= Known_Compatible))
83f8f0a6 13154 then
13155 Error_Msg_NE
1e3532e7 13156 ("??specified address for& may be inconsistent "
13157 & "with alignment", ACCR.N, ACCR.X);
83f8f0a6 13158 Error_Msg_N
1e3532e7 13159 ("\??program execution may be erroneous (RM 13.3(27))",
83f8f0a6 13160 ACCR.N);
13161 Error_Msg_Uint_1 := X_Alignment;
13162 Error_Msg_NE
1e3532e7 13163 ("\??alignment of & is ^", ACCR.N, ACCR.X);
83f8f0a6 13164 Error_Msg_Uint_1 := Y_Alignment;
13165 Error_Msg_NE
1e3532e7 13166 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
d6da7448 13167 if Y_Alignment >= X_Alignment then
13168 Error_Msg_N
1e3532e7 13169 ("\??but offset is not multiple of alignment", ACCR.N);
d6da7448 13170 end if;
83f8f0a6 13171 end if;
13172 end if;
13173 end;
13174 end loop;
13175 end Validate_Address_Clauses;
13176
7717ea00 13177 ---------------------------
13178 -- Validate_Independence --
13179 ---------------------------
13180
13181 procedure Validate_Independence is
13182 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13183 N : Node_Id;
13184 E : Entity_Id;
13185 IC : Boolean;
13186 Comp : Entity_Id;
13187 Addr : Node_Id;
13188 P : Node_Id;
13189
13190 procedure Check_Array_Type (Atyp : Entity_Id);
13191 -- Checks if the array type Atyp has independent components, and
13192 -- if not, outputs an appropriate set of error messages.
13193
13194 procedure No_Independence;
13195 -- Output message that independence cannot be guaranteed
13196
13197 function OK_Component (C : Entity_Id) return Boolean;
13198 -- Checks one component to see if it is independently accessible, and
13199 -- if so yields True, otherwise yields False if independent access
13200 -- cannot be guaranteed. This is a conservative routine, it only
13201 -- returns True if it knows for sure, it returns False if it knows
13202 -- there is a problem, or it cannot be sure there is no problem.
13203
13204 procedure Reason_Bad_Component (C : Entity_Id);
13205 -- Outputs continuation message if a reason can be determined for
13206 -- the component C being bad.
13207
13208 ----------------------
13209 -- Check_Array_Type --
13210 ----------------------
13211
13212 procedure Check_Array_Type (Atyp : Entity_Id) is
13213 Ctyp : constant Entity_Id := Component_Type (Atyp);
13214
13215 begin
13216 -- OK if no alignment clause, no pack, and no component size
13217
13218 if not Has_Component_Size_Clause (Atyp)
13219 and then not Has_Alignment_Clause (Atyp)
13220 and then not Is_Packed (Atyp)
13221 then
13222 return;
13223 end if;
13224
aa0a69ab 13225 -- Case of component size is greater than or equal to 64 and the
13226 -- alignment of the array is at least as large as the alignment
13227 -- of the component. We are definitely OK in this situation.
13228
13229 if Known_Component_Size (Atyp)
13230 and then Component_Size (Atyp) >= 64
13231 and then Known_Alignment (Atyp)
13232 and then Known_Alignment (Ctyp)
13233 and then Alignment (Atyp) >= Alignment (Ctyp)
13234 then
13235 return;
13236 end if;
13237
7717ea00 13238 -- Check actual component size
13239
13240 if not Known_Component_Size (Atyp)
13241 or else not (Addressable (Component_Size (Atyp))
aa0a69ab 13242 and then Component_Size (Atyp) < 64)
7717ea00 13243 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13244 then
13245 No_Independence;
13246
13247 -- Bad component size, check reason
13248
13249 if Has_Component_Size_Clause (Atyp) then
b9e61b2a 13250 P := Get_Attribute_Definition_Clause
13251 (Atyp, Attribute_Component_Size);
7717ea00 13252
13253 if Present (P) then
13254 Error_Msg_Sloc := Sloc (P);
13255 Error_Msg_N ("\because of Component_Size clause#", N);
13256 return;
13257 end if;
13258 end if;
13259
13260 if Is_Packed (Atyp) then
13261 P := Get_Rep_Pragma (Atyp, Name_Pack);
13262
13263 if Present (P) then
13264 Error_Msg_Sloc := Sloc (P);
13265 Error_Msg_N ("\because of pragma Pack#", N);
13266 return;
13267 end if;
13268 end if;
13269
13270 -- No reason found, just return
13271
13272 return;
13273 end if;
13274
13275 -- Array type is OK independence-wise
13276
13277 return;
13278 end Check_Array_Type;
13279
13280 ---------------------
13281 -- No_Independence --
13282 ---------------------
13283
13284 procedure No_Independence is
13285 begin
13286 if Pragma_Name (N) = Name_Independent then
18393965 13287 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
7717ea00 13288 else
13289 Error_Msg_NE
13290 ("independent components cannot be guaranteed for&", N, E);
13291 end if;
13292 end No_Independence;
13293
13294 ------------------
13295 -- OK_Component --
13296 ------------------
13297
13298 function OK_Component (C : Entity_Id) return Boolean is
13299 Rec : constant Entity_Id := Scope (C);
13300 Ctyp : constant Entity_Id := Etype (C);
13301
13302 begin
13303 -- OK if no component clause, no Pack, and no alignment clause
13304
13305 if No (Component_Clause (C))
13306 and then not Is_Packed (Rec)
13307 and then not Has_Alignment_Clause (Rec)
13308 then
13309 return True;
13310 end if;
13311
13312 -- Here we look at the actual component layout. A component is
13313 -- addressable if its size is a multiple of the Esize of the
13314 -- component type, and its starting position in the record has
13315 -- appropriate alignment, and the record itself has appropriate
13316 -- alignment to guarantee the component alignment.
13317
13318 -- Make sure sizes are static, always assume the worst for any
13319 -- cases where we cannot check static values.
13320
13321 if not (Known_Static_Esize (C)
b9e61b2a 13322 and then
13323 Known_Static_Esize (Ctyp))
7717ea00 13324 then
13325 return False;
13326 end if;
13327
13328 -- Size of component must be addressable or greater than 64 bits
13329 -- and a multiple of bytes.
13330
b9e61b2a 13331 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
7717ea00 13332 return False;
13333 end if;
13334
13335 -- Check size is proper multiple
13336
13337 if Esize (C) mod Esize (Ctyp) /= 0 then
13338 return False;
13339 end if;
13340
13341 -- Check alignment of component is OK
13342
13343 if not Known_Component_Bit_Offset (C)
13344 or else Component_Bit_Offset (C) < Uint_0
13345 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13346 then
13347 return False;
13348 end if;
13349
13350 -- Check alignment of record type is OK
13351
13352 if not Known_Alignment (Rec)
13353 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13354 then
13355 return False;
13356 end if;
13357
13358 -- All tests passed, component is addressable
13359
13360 return True;
13361 end OK_Component;
13362
13363 --------------------------
13364 -- Reason_Bad_Component --
13365 --------------------------
13366
13367 procedure Reason_Bad_Component (C : Entity_Id) is
13368 Rec : constant Entity_Id := Scope (C);
13369 Ctyp : constant Entity_Id := Etype (C);
13370
13371 begin
13372 -- If component clause present assume that's the problem
13373
13374 if Present (Component_Clause (C)) then
13375 Error_Msg_Sloc := Sloc (Component_Clause (C));
13376 Error_Msg_N ("\because of Component_Clause#", N);
13377 return;
13378 end if;
13379
13380 -- If pragma Pack clause present, assume that's the problem
13381
13382 if Is_Packed (Rec) then
13383 P := Get_Rep_Pragma (Rec, Name_Pack);
13384
13385 if Present (P) then
13386 Error_Msg_Sloc := Sloc (P);
13387 Error_Msg_N ("\because of pragma Pack#", N);
13388 return;
13389 end if;
13390 end if;
13391
13392 -- See if record has bad alignment clause
13393
13394 if Has_Alignment_Clause (Rec)
13395 and then Known_Alignment (Rec)
13396 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13397 then
13398 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13399
13400 if Present (P) then
13401 Error_Msg_Sloc := Sloc (P);
13402 Error_Msg_N ("\because of Alignment clause#", N);
13403 end if;
13404 end if;
13405
13406 -- Couldn't find a reason, so return without a message
13407
13408 return;
13409 end Reason_Bad_Component;
13410
13411 -- Start of processing for Validate_Independence
13412
13413 begin
13414 for J in Independence_Checks.First .. Independence_Checks.Last loop
13415 N := Independence_Checks.Table (J).N;
13416 E := Independence_Checks.Table (J).E;
13417 IC := Pragma_Name (N) = Name_Independent_Components;
13418
13419 -- Deal with component case
13420
13421 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13422 if not OK_Component (E) then
13423 No_Independence;
13424 Reason_Bad_Component (E);
13425 goto Continue;
13426 end if;
13427 end if;
13428
13429 -- Deal with record with Independent_Components
13430
13431 if IC and then Is_Record_Type (E) then
13432 Comp := First_Component_Or_Discriminant (E);
13433 while Present (Comp) loop
13434 if not OK_Component (Comp) then
13435 No_Independence;
13436 Reason_Bad_Component (Comp);
13437 goto Continue;
13438 end if;
13439
13440 Next_Component_Or_Discriminant (Comp);
13441 end loop;
13442 end if;
13443
13444 -- Deal with address clause case
13445
13446 if Is_Object (E) then
13447 Addr := Address_Clause (E);
13448
13449 if Present (Addr) then
13450 No_Independence;
13451 Error_Msg_Sloc := Sloc (Addr);
13452 Error_Msg_N ("\because of Address clause#", N);
13453 goto Continue;
13454 end if;
13455 end if;
13456
13457 -- Deal with independent components for array type
13458
13459 if IC and then Is_Array_Type (E) then
13460 Check_Array_Type (E);
13461 end if;
13462
13463 -- Deal with independent components for array object
13464
13465 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13466 Check_Array_Type (Etype (E));
13467 end if;
13468
13469 <<Continue>> null;
13470 end loop;
13471 end Validate_Independence;
13472
b3f8228a 13473 ------------------------------
13474 -- Validate_Iterable_Aspect --
13475 ------------------------------
13476
13477 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
3061ffde 13478 Assoc : Node_Id;
13479 Expr : Node_Id;
b3f8228a 13480
bde03454 13481 Prim : Node_Id;
a9f5fea7 13482 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
b3f8228a 13483
13484 First_Id : Entity_Id;
13485 Next_Id : Entity_Id;
13486 Has_Element_Id : Entity_Id;
13487 Element_Id : Entity_Id;
13488
b3f8228a 13489 begin
9698629c 13490 -- If previous error aspect is unusable
a9f5fea7 13491
13492 if Cursor = Any_Type then
3061ffde 13493 return;
13494 end if;
b3f8228a 13495
13496 First_Id := Empty;
13497 Next_Id := Empty;
13498 Has_Element_Id := Empty;
32de816b 13499 Element_Id := Empty;
b3f8228a 13500
13501 -- Each expression must resolve to a function with the proper signature
13502
13503 Assoc := First (Component_Associations (Expression (ASN)));
13504 while Present (Assoc) loop
13505 Expr := Expression (Assoc);
13506 Analyze (Expr);
13507
b3f8228a 13508 Prim := First (Choices (Assoc));
bde03454 13509
f02a9a9a 13510 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
b3f8228a 13511 Error_Msg_N ("illegal name in association", Prim);
13512
13513 elsif Chars (Prim) = Name_First then
3061ffde 13514 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
b3f8228a 13515 First_Id := Entity (Expr);
b3f8228a 13516
13517 elsif Chars (Prim) = Name_Next then
3061ffde 13518 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
b3f8228a 13519 Next_Id := Entity (Expr);
b3f8228a 13520
13521 elsif Chars (Prim) = Name_Has_Element then
3061ffde 13522 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
b3f8228a 13523 Has_Element_Id := Entity (Expr);
bde03454 13524
b3f8228a 13525 elsif Chars (Prim) = Name_Element then
3061ffde 13526 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
b3f8228a 13527 Element_Id := Entity (Expr);
b3f8228a 13528
13529 else
13530 Error_Msg_N ("invalid name for iterable function", Prim);
13531 end if;
13532
13533 Next (Assoc);
13534 end loop;
13535
13536 if No (First_Id) then
3061ffde 13537 Error_Msg_N ("match for First primitive not found", ASN);
b3f8228a 13538
13539 elsif No (Next_Id) then
3061ffde 13540 Error_Msg_N ("match for Next primitive not found", ASN);
b3f8228a 13541
13542 elsif No (Has_Element_Id) then
3061ffde 13543 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13544
13545 elsif No (Element_Id) then
13546 null; -- Optional.
b3f8228a 13547 end if;
13548 end Validate_Iterable_Aspect;
13549
d6f39728 13550 -----------------------------------
13551 -- Validate_Unchecked_Conversion --
13552 -----------------------------------
13553
13554 procedure Validate_Unchecked_Conversion
13555 (N : Node_Id;
13556 Act_Unit : Entity_Id)
13557 is
13558 Source : Entity_Id;
13559 Target : Entity_Id;
13560 Vnode : Node_Id;
13561
13562 begin
13563 -- Obtain source and target types. Note that we call Ancestor_Subtype
13564 -- here because the processing for generic instantiation always makes
13565 -- subtypes, and we want the original frozen actual types.
13566
13567 -- If we are dealing with private types, then do the check on their
13568 -- fully declared counterparts if the full declarations have been
39a0c1d3 13569 -- encountered (they don't have to be visible, but they must exist).
d6f39728 13570
13571 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13572
13573 if Is_Private_Type (Source)
13574 and then Present (Underlying_Type (Source))
13575 then
13576 Source := Underlying_Type (Source);
13577 end if;
13578
13579 Target := Ancestor_Subtype (Etype (Act_Unit));
13580
fdd294d1 13581 -- If either type is generic, the instantiation happens within a generic
95deda50 13582 -- unit, and there is nothing to check. The proper check will happen
13583 -- when the enclosing generic is instantiated.
d6f39728 13584
13585 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13586 return;
13587 end if;
13588
13589 if Is_Private_Type (Target)
13590 and then Present (Underlying_Type (Target))
13591 then
13592 Target := Underlying_Type (Target);
13593 end if;
13594
13595 -- Source may be unconstrained array, but not target
13596
b9e61b2a 13597 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
d6f39728 13598 Error_Msg_N
13599 ("unchecked conversion to unconstrained array not allowed", N);
13600 return;
13601 end if;
13602
fbc67f84 13603 -- Warn if conversion between two different convention pointers
13604
13605 if Is_Access_Type (Target)
13606 and then Is_Access_Type (Source)
13607 and then Convention (Target) /= Convention (Source)
13608 and then Warn_On_Unchecked_Conversion
13609 then
74c7ae52 13610 -- Give warnings for subprogram pointers only on most targets
fdd294d1 13611
13612 if Is_Access_Subprogram_Type (Target)
13613 or else Is_Access_Subprogram_Type (Source)
fdd294d1 13614 then
13615 Error_Msg_N
cb97ae5c 13616 ("?z?conversion between pointers with different conventions!",
1e3532e7 13617 N);
fdd294d1 13618 end if;
fbc67f84 13619 end if;
13620
3062c401 13621 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13622 -- warning when compiling GNAT-related sources.
13623
13624 if Warn_On_Unchecked_Conversion
13625 and then not In_Predefined_Unit (N)
13626 and then RTU_Loaded (Ada_Calendar)
f02a9a9a 13627 and then (Chars (Source) = Name_Time
13628 or else
13629 Chars (Target) = Name_Time)
3062c401 13630 then
13631 -- If Ada.Calendar is loaded and the name of one of the operands is
13632 -- Time, there is a good chance that this is Ada.Calendar.Time.
13633
13634 declare
f02a9a9a 13635 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
3062c401 13636 begin
13637 pragma Assert (Present (Calendar_Time));
13638
b9e61b2a 13639 if Source = Calendar_Time or else Target = Calendar_Time then
3062c401 13640 Error_Msg_N
f02a9a9a 13641 ("?z?representation of 'Time values may change between "
13642 & "'G'N'A'T versions", N);
3062c401 13643 end if;
13644 end;
13645 end if;
13646
fdd294d1 13647 -- Make entry in unchecked conversion table for later processing by
13648 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13649 -- (using values set by the back-end where possible). This is only done
13650 -- if the appropriate warning is active.
d6f39728 13651
9dfe12ae 13652 if Warn_On_Unchecked_Conversion then
13653 Unchecked_Conversions.Append
86d32751 13654 (New_Val => UC_Entry'(Eloc => Sloc (N),
13655 Source => Source,
13656 Target => Target,
13657 Act_Unit => Act_Unit));
9dfe12ae 13658
13659 -- If both sizes are known statically now, then back end annotation
13660 -- is not required to do a proper check but if either size is not
13661 -- known statically, then we need the annotation.
13662
13663 if Known_Static_RM_Size (Source)
1e3532e7 13664 and then
13665 Known_Static_RM_Size (Target)
9dfe12ae 13666 then
13667 null;
13668 else
13669 Back_Annotate_Rep_Info := True;
13670 end if;
13671 end if;
d6f39728 13672
fdd294d1 13673 -- If unchecked conversion to access type, and access type is declared
95deda50 13674 -- in the same unit as the unchecked conversion, then set the flag
13675 -- No_Strict_Aliasing (no strict aliasing is implicit here)
28ed91d4 13676
13677 if Is_Access_Type (Target) and then
13678 In_Same_Source_Unit (Target, N)
13679 then
13680 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13681 end if;
3d875462 13682
95deda50 13683 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13684 -- the back end needs to perform special validation checks.
3d875462 13685
95deda50 13686 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13687 -- have full expansion and the back end is called ???
3d875462 13688
13689 Vnode :=
13690 Make_Validate_Unchecked_Conversion (Sloc (N));
13691 Set_Source_Type (Vnode, Source);
13692 Set_Target_Type (Vnode, Target);
13693
fdd294d1 13694 -- If the unchecked conversion node is in a list, just insert before it.
13695 -- If not we have some strange case, not worth bothering about.
3d875462 13696
13697 if Is_List_Member (N) then
d6f39728 13698 Insert_After (N, Vnode);
13699 end if;
13700 end Validate_Unchecked_Conversion;
13701
13702 ------------------------------------
13703 -- Validate_Unchecked_Conversions --
13704 ------------------------------------
13705
13706 procedure Validate_Unchecked_Conversions is
13707 begin
13708 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13709 declare
13710 T : UC_Entry renames Unchecked_Conversions.Table (N);
13711
86d32751 13712 Eloc : constant Source_Ptr := T.Eloc;
13713 Source : constant Entity_Id := T.Source;
13714 Target : constant Entity_Id := T.Target;
13715 Act_Unit : constant Entity_Id := T.Act_Unit;
d6f39728 13716
44705307 13717 Source_Siz : Uint;
13718 Target_Siz : Uint;
d6f39728 13719
13720 begin
86d32751 13721 -- Skip if function marked as warnings off
13722
13723 if Warnings_Off (Act_Unit) then
13724 goto Continue;
13725 end if;
13726
fdd294d1 13727 -- This validation check, which warns if we have unequal sizes for
13728 -- unchecked conversion, and thus potentially implementation
d6f39728 13729 -- dependent semantics, is one of the few occasions on which we
fdd294d1 13730 -- use the official RM size instead of Esize. See description in
13731 -- Einfo "Handling of Type'Size Values" for details.
d6f39728 13732
f15731c4 13733 if Serious_Errors_Detected = 0
d6f39728 13734 and then Known_Static_RM_Size (Source)
13735 and then Known_Static_RM_Size (Target)
f25f4252 13736
13737 -- Don't do the check if warnings off for either type, note the
13738 -- deliberate use of OR here instead of OR ELSE to get the flag
13739 -- Warnings_Off_Used set for both types if appropriate.
13740
13741 and then not (Has_Warnings_Off (Source)
13742 or
13743 Has_Warnings_Off (Target))
d6f39728 13744 then
13745 Source_Siz := RM_Size (Source);
13746 Target_Siz := RM_Size (Target);
13747
13748 if Source_Siz /= Target_Siz then
299480f9 13749 Error_Msg
cb97ae5c 13750 ("?z?types for unchecked conversion have different sizes!",
299480f9 13751 Eloc);
d6f39728 13752
13753 if All_Errors_Mode then
13754 Error_Msg_Name_1 := Chars (Source);
13755 Error_Msg_Uint_1 := Source_Siz;
13756 Error_Msg_Name_2 := Chars (Target);
13757 Error_Msg_Uint_2 := Target_Siz;
cb97ae5c 13758 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
d6f39728 13759
13760 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13761
13762 if Is_Discrete_Type (Source)
b9e61b2a 13763 and then
13764 Is_Discrete_Type (Target)
d6f39728 13765 then
13766 if Source_Siz > Target_Siz then
299480f9 13767 Error_Msg
cb97ae5c 13768 ("\?z?^ high order bits of source will "
1e3532e7 13769 & "be ignored!", Eloc);
d6f39728 13770
9dfe12ae 13771 elsif Is_Unsigned_Type (Source) then
299480f9 13772 Error_Msg
cb97ae5c 13773 ("\?z?source will be extended with ^ high order "
1581f2d7 13774 & "zero bits!", Eloc);
d6f39728 13775
13776 else
299480f9 13777 Error_Msg
cb97ae5c 13778 ("\?z?source will be extended with ^ high order "
1e3532e7 13779 & "sign bits!", Eloc);
d6f39728 13780 end if;
13781
13782 elsif Source_Siz < Target_Siz then
13783 if Is_Discrete_Type (Target) then
13784 if Bytes_Big_Endian then
299480f9 13785 Error_Msg
cb97ae5c 13786 ("\?z?target value will include ^ undefined "
1e3532e7 13787 & "low order bits!", Eloc);
d6f39728 13788 else
299480f9 13789 Error_Msg
cb97ae5c 13790 ("\?z?target value will include ^ undefined "
1e3532e7 13791 & "high order bits!", Eloc);
d6f39728 13792 end if;
13793
13794 else
299480f9 13795 Error_Msg
cb97ae5c 13796 ("\?z?^ trailing bits of target value will be "
1e3532e7 13797 & "undefined!", Eloc);
d6f39728 13798 end if;
13799
13800 else pragma Assert (Source_Siz > Target_Siz);
0388e54e 13801 if Is_Discrete_Type (Source) then
13802 if Bytes_Big_Endian then
13803 Error_Msg
13804 ("\?z?^ low order bits of source will be "
13805 & "ignored!", Eloc);
13806 else
13807 Error_Msg
13808 ("\?z?^ high order bits of source will be "
13809 & "ignored!", Eloc);
13810 end if;
13811
13812 else
13813 Error_Msg
13814 ("\?z?^ trailing bits of source will be "
13815 & "ignored!", Eloc);
13816 end if;
d6f39728 13817 end if;
13818 end if;
d6f39728 13819 end if;
13820 end if;
13821
13822 -- If both types are access types, we need to check the alignment.
13823 -- If the alignment of both is specified, we can do it here.
13824
f15731c4 13825 if Serious_Errors_Detected = 0
2a10e737 13826 and then Is_Access_Type (Source)
13827 and then Is_Access_Type (Target)
d6f39728 13828 and then Target_Strict_Alignment
13829 and then Present (Designated_Type (Source))
13830 and then Present (Designated_Type (Target))
13831 then
13832 declare
13833 D_Source : constant Entity_Id := Designated_Type (Source);
13834 D_Target : constant Entity_Id := Designated_Type (Target);
13835
13836 begin
13837 if Known_Alignment (D_Source)
b9e61b2a 13838 and then
13839 Known_Alignment (D_Target)
d6f39728 13840 then
13841 declare
13842 Source_Align : constant Uint := Alignment (D_Source);
13843 Target_Align : constant Uint := Alignment (D_Target);
13844
13845 begin
13846 if Source_Align < Target_Align
13847 and then not Is_Tagged_Type (D_Source)
f25f4252 13848
13849 -- Suppress warning if warnings suppressed on either
13850 -- type or either designated type. Note the use of
13851 -- OR here instead of OR ELSE. That is intentional,
13852 -- we would like to set flag Warnings_Off_Used in
13853 -- all types for which warnings are suppressed.
13854
13855 and then not (Has_Warnings_Off (D_Source)
13856 or
13857 Has_Warnings_Off (D_Target)
13858 or
13859 Has_Warnings_Off (Source)
13860 or
13861 Has_Warnings_Off (Target))
d6f39728 13862 then
d6f39728 13863 Error_Msg_Uint_1 := Target_Align;
13864 Error_Msg_Uint_2 := Source_Align;
299480f9 13865 Error_Msg_Node_1 := D_Target;
d6f39728 13866 Error_Msg_Node_2 := D_Source;
299480f9 13867 Error_Msg
cb97ae5c 13868 ("?z?alignment of & (^) is stricter than "
1e3532e7 13869 & "alignment of & (^)!", Eloc);
f25f4252 13870 Error_Msg
cb97ae5c 13871 ("\?z?resulting access value may have invalid "
1e3532e7 13872 & "alignment!", Eloc);
d6f39728 13873 end if;
13874 end;
13875 end if;
13876 end;
13877 end if;
13878 end;
86d32751 13879
13880 <<Continue>>
13881 null;
d6f39728 13882 end loop;
13883 end Validate_Unchecked_Conversions;
13884
d6f39728 13885end Sem_Ch13;