]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch13.adb
2015-05-22 Robert Dewar <dewar@adacore.com>
[thirdparty/gcc.git] / gcc / ada / sem_ch13.adb
CommitLineData
d6f39728 1------------------------------------------------------------------------------
7189d17f 2-- --
d6f39728 3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 1 3 --
6-- --
7-- B o d y --
8-- --
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;
d6f39728 37with Lib; use Lib;
83f8f0a6 38with Lib.Xref; use Lib.Xref;
15ebb600 39with Namet; use Namet;
d6f39728 40with Nlists; use Nlists;
41with Nmake; use Nmake;
42with Opt; use Opt;
e0521a36 43with Restrict; use Restrict;
44with Rident; use Rident;
d6f39728 45with Rtsfind; use Rtsfind;
46with Sem; use Sem;
d60c9ff7 47with Sem_Aux; use Sem_Aux;
be9124d0 48with Sem_Case; use Sem_Case;
40ca69b9 49with Sem_Ch3; use Sem_Ch3;
490beba6 50with Sem_Ch6; use Sem_Ch6;
d6f39728 51with Sem_Ch8; use Sem_Ch8;
85696508 52with Sem_Dim; use Sem_Dim;
85377c9b 53with Sem_Disp; use Sem_Disp;
d6f39728 54with Sem_Eval; use Sem_Eval;
51ea9c94 55with Sem_Prag; use Sem_Prag;
d6f39728 56with Sem_Res; use Sem_Res;
57with Sem_Type; use Sem_Type;
58with Sem_Util; use Sem_Util;
44e4341e 59with Sem_Warn; use Sem_Warn;
1e3c4ae6 60with Sinput; use Sinput;
9dfe12ae 61with Snames; use Snames;
d6f39728 62with Stand; use Stand;
63with Sinfo; use Sinfo;
5b5df4a9 64with Stringt; use Stringt;
93735cb8 65with Targparm; use Targparm;
d6f39728 66with Ttypes; use Ttypes;
67with Tbuild; use Tbuild;
68with Urealp; use Urealp;
f42f24d7 69with Warnsw; use Warnsw;
d6f39728 70
bfa5a9d9 71with GNAT.Heap_Sort_G;
d6f39728 72
73package body Sem_Ch13 is
74
75 SSU : constant Pos := System_Storage_Unit;
76 -- Convenient short hand for commonly used constant
77
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
81
1d366b32 82 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
83 -- This routine is called after setting one of the sizes of type entity
84 -- Typ to Size. The purpose is to deal with the situation of a derived
85 -- type whose inherited alignment is no longer appropriate for the new
86 -- size value. In this case, we reset the Alignment to unknown.
d6f39728 87
eb66e842 88 procedure Build_Discrete_Static_Predicate
d97beb2f 89 (Typ : Entity_Id;
90 Expr : Node_Id;
91 Nam : Name_Id);
d7c2851f 92 -- Given a predicated type Typ, where Typ is a discrete static subtype,
93 -- whose predicate expression is Expr, tests if Expr is a static predicate,
94 -- and if so, builds the predicate range list. Nam is the name of the one
95 -- argument to the predicate function. Occurrences of the type name in the
6fb3c314 96 -- predicate expression have been replaced by identifier references to this
d7c2851f 97 -- name, which is unique, so any identifier with Chars matching Nam must be
98 -- a reference to the type. If the predicate is non-static, this procedure
99 -- returns doing nothing. If the predicate is static, then the predicate
5c6a5792 100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
d97beb2f 102
eb66e842 103 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
104 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
105 -- then either there are pragma Predicate entries on the rep chain for the
106 -- type (note that Predicate aspects are converted to pragma Predicate), or
107 -- there are inherited aspects from a parent type, or ancestor subtypes.
108 -- This procedure builds the spec and body for the Predicate function that
109 -- tests these predicates. N is the freeze node for the type. The spec of
110 -- the function is inserted before the freeze node, and the body of the
111 -- function is inserted after the freeze node. If the predicate expression
112 -- has at least one Raise_Expression, then this procedure also builds the
113 -- M version of the predicate function for use in membership tests.
114
6653b695 115 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
116 -- Called if both Storage_Pool and Storage_Size attribute definition
117 -- clauses (SP and SS) are present for entity Ent. Issue error message.
118
d9f6a4ee 119 procedure Freeze_Entity_Checks (N : Node_Id);
120 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
121 -- to generate appropriate semantic checks that are delayed until this
122 -- point (they had to be delayed this long for cases of delayed aspects,
123 -- e.g. analysis of statically predicated subtypes in choices, for which
124 -- we have to be sure the subtypes in question are frozen before checking.
125
d6f39728 126 function Get_Alignment_Value (Expr : Node_Id) return Uint;
127 -- Given the expression for an alignment value, returns the corresponding
128 -- Uint value. If the value is inappropriate, then error messages are
129 -- posted as required, and a value of No_Uint is returned.
130
131 function Is_Operational_Item (N : Node_Id) return Boolean;
1e3c4ae6 132 -- A specification for a stream attribute is allowed before the full type
133 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
134 -- that do not specify a representation characteristic are operational
135 -- attributes.
d6f39728 136
3b23aaa0 137 function Is_Predicate_Static
138 (Expr : Node_Id;
139 Nam : Name_Id) return Boolean;
140 -- Given predicate expression Expr, tests if Expr is predicate-static in
141 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
142 -- name in the predicate expression have been replaced by references to
143 -- an identifier whose Chars field is Nam. This name is unique, so any
144 -- identifier with Chars matching Nam must be a reference to the type.
145 -- Returns True if the expression is predicate-static and False otherwise,
146 -- but is not in the business of setting flags or issuing error messages.
147 --
148 -- Only scalar types can have static predicates, so False is always
149 -- returned for non-scalar types.
150 --
151 -- Note: the RM seems to suggest that string types can also have static
152 -- predicates. But that really makes lttle sense as very few useful
153 -- predicates can be constructed for strings. Remember that:
154 --
155 -- "ABC" < "DEF"
156 --
157 -- is not a static expression. So even though the clearly faulty RM wording
158 -- allows the following:
159 --
160 -- subtype S is String with Static_Predicate => S < "DEF"
161 --
162 -- We can't allow this, otherwise we have predicate-static applying to a
163 -- larger class than static expressions, which was never intended.
164
44e4341e 165 procedure New_Stream_Subprogram
d6f39728 166 (N : Node_Id;
167 Ent : Entity_Id;
168 Subp : Entity_Id;
9dfe12ae 169 Nam : TSS_Name_Type);
44e4341e 170 -- Create a subprogram renaming of a given stream attribute to the
171 -- designated subprogram and then in the tagged case, provide this as a
d1a2e31b 172 -- primitive operation, or in the untagged case make an appropriate TSS
44e4341e 173 -- entry. This is more properly an expansion activity than just semantics,
d1a2e31b 174 -- but the presence of user-defined stream functions for limited types
175 -- is a legality check, which is why this takes place here rather than in
44e4341e 176 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
177 -- function to be generated.
9dfe12ae 178 --
f15731c4 179 -- To avoid elaboration anomalies with freeze nodes, for untagged types
180 -- we generate both a subprogram declaration and a subprogram renaming
181 -- declaration, so that the attribute specification is handled as a
182 -- renaming_as_body. For tagged types, the specification is one of the
183 -- primitive specs.
184
3061ffde 185 procedure Resolve_Iterable_Operation
186 (N : Node_Id;
187 Cursor : Entity_Id;
188 Typ : Entity_Id;
189 Nam : Name_Id);
190 -- If the name of a primitive operation for an Iterable aspect is
191 -- overloaded, resolve according to required signature.
192
b77e4501 193 procedure Set_Biased
194 (E : Entity_Id;
195 N : Node_Id;
196 Msg : String;
197 Biased : Boolean := True);
198 -- If Biased is True, sets Has_Biased_Representation flag for E, and
199 -- outputs a warning message at node N if Warn_On_Biased_Representation is
200 -- is True. This warning inserts the string Msg to describe the construct
201 -- causing biasing.
202
d6f39728 203 ----------------------------------------------
204 -- Table for Validate_Unchecked_Conversions --
205 ----------------------------------------------
206
207 -- The following table collects unchecked conversions for validation.
95deda50 208 -- Entries are made by Validate_Unchecked_Conversion and then the call
209 -- to Validate_Unchecked_Conversions does the actual error checking and
210 -- posting of warnings. The reason for this delayed processing is to take
211 -- advantage of back-annotations of size and alignment values performed by
212 -- the back end.
d6f39728 213
95deda50 214 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
215 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
216 -- already have modified all Sloc values if the -gnatD option is set.
299480f9 217
d6f39728 218 type UC_Entry is record
86d32751 219 Eloc : Source_Ptr; -- node used for posting warnings
220 Source : Entity_Id; -- source type for unchecked conversion
221 Target : Entity_Id; -- target type for unchecked conversion
222 Act_Unit : Entity_Id; -- actual function instantiated
d6f39728 223 end record;
224
225 package Unchecked_Conversions is new Table.Table (
226 Table_Component_Type => UC_Entry,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
229 Table_Initial => 50,
230 Table_Increment => 200,
231 Table_Name => "Unchecked_Conversions");
232
83f8f0a6 233 ----------------------------------------
234 -- Table for Validate_Address_Clauses --
235 ----------------------------------------
236
237 -- If an address clause has the form
238
239 -- for X'Address use Expr
240
95deda50 241 -- where Expr is of the form Y'Address or recursively is a reference to a
242 -- constant of either of these forms, and X and Y are entities of objects,
243 -- then if Y has a smaller alignment than X, that merits a warning about
244 -- possible bad alignment. The following table collects address clauses of
245 -- this kind. We put these in a table so that they can be checked after the
246 -- back end has completed annotation of the alignments of objects, since we
247 -- can catch more cases that way.
83f8f0a6 248
249 type Address_Clause_Check_Record is record
250 N : Node_Id;
251 -- The address clause
252
253 X : Entity_Id;
254 -- The entity of the object overlaying Y
255
256 Y : Entity_Id;
257 -- The entity of the object being overlaid
d6da7448 258
259 Off : Boolean;
6fb3c314 260 -- Whether the address is offset within Y
83f8f0a6 261 end record;
262
263 package Address_Clause_Checks is new Table.Table (
264 Table_Component_Type => Address_Clause_Check_Record,
265 Table_Index_Type => Int,
266 Table_Low_Bound => 1,
267 Table_Initial => 20,
268 Table_Increment => 200,
269 Table_Name => "Address_Clause_Checks");
270
59ac57b5 271 -----------------------------------------
272 -- Adjust_Record_For_Reverse_Bit_Order --
273 -----------------------------------------
274
275 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
67278d60 276 Comp : Node_Id;
277 CC : Node_Id;
59ac57b5 278
279 begin
67278d60 280 -- Processing depends on version of Ada
59ac57b5 281
6797073f 282 -- For Ada 95, we just renumber bits within a storage unit. We do the
568b0f6a 283 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
ab19a652 284 -- Ada 83, and are free to add this extension.
6797073f 285
286 if Ada_Version < Ada_2005 then
287 Comp := First_Component_Or_Discriminant (R);
288 while Present (Comp) loop
289 CC := Component_Clause (Comp);
290
291 -- If component clause is present, then deal with the non-default
292 -- bit order case for Ada 95 mode.
293
294 -- We only do this processing for the base type, and in fact that
295 -- is important, since otherwise if there are record subtypes, we
296 -- could reverse the bits once for each subtype, which is wrong.
297
b9e61b2a 298 if Present (CC) and then Ekind (R) = E_Record_Type then
6797073f 299 declare
300 CFB : constant Uint := Component_Bit_Offset (Comp);
301 CSZ : constant Uint := Esize (Comp);
302 CLC : constant Node_Id := Component_Clause (Comp);
303 Pos : constant Node_Id := Position (CLC);
304 FB : constant Node_Id := First_Bit (CLC);
305
306 Storage_Unit_Offset : constant Uint :=
307 CFB / System_Storage_Unit;
308
309 Start_Bit : constant Uint :=
310 CFB mod System_Storage_Unit;
59ac57b5 311
6797073f 312 begin
313 -- Cases where field goes over storage unit boundary
59ac57b5 314
6797073f 315 if Start_Bit + CSZ > System_Storage_Unit then
59ac57b5 316
6797073f 317 -- Allow multi-byte field but generate warning
59ac57b5 318
6797073f 319 if Start_Bit mod System_Storage_Unit = 0
320 and then CSZ mod System_Storage_Unit = 0
321 then
322 Error_Msg_N
7a41db5b 323 ("info: multi-byte field specified with "
324 & "non-standard Bit_Order?V?", CLC);
31486bc0 325
6797073f 326 if Bytes_Big_Endian then
31486bc0 327 Error_Msg_N
7a41db5b 328 ("\bytes are not reversed "
329 & "(component is big-endian)?V?", CLC);
31486bc0 330 else
331 Error_Msg_N
7a41db5b 332 ("\bytes are not reversed "
333 & "(component is little-endian)?V?", CLC);
31486bc0 334 end if;
59ac57b5 335
6797073f 336 -- Do not allow non-contiguous field
59ac57b5 337
67278d60 338 else
6797073f 339 Error_Msg_N
340 ("attempt to specify non-contiguous field "
341 & "not permitted", CLC);
342 Error_Msg_N
343 ("\caused by non-standard Bit_Order "
344 & "specified", CLC);
345 Error_Msg_N
346 ("\consider possibility of using "
347 & "Ada 2005 mode here", CLC);
348 end if;
59ac57b5 349
6797073f 350 -- Case where field fits in one storage unit
59ac57b5 351
6797073f 352 else
353 -- Give warning if suspicious component clause
59ac57b5 354
6797073f 355 if Intval (FB) >= System_Storage_Unit
356 and then Warn_On_Reverse_Bit_Order
357 then
358 Error_Msg_N
7a41db5b 359 ("info: Bit_Order clause does not affect " &
1e3532e7 360 "byte ordering?V?", Pos);
6797073f 361 Error_Msg_Uint_1 :=
362 Intval (Pos) + Intval (FB) /
363 System_Storage_Unit;
364 Error_Msg_N
7a41db5b 365 ("info: position normalized to ^ before bit " &
1e3532e7 366 "order interpreted?V?", Pos);
6797073f 367 end if;
59ac57b5 368
6797073f 369 -- Here is where we fix up the Component_Bit_Offset value
370 -- to account for the reverse bit order. Some examples of
371 -- what needs to be done are:
bfa5a9d9 372
6797073f 373 -- First_Bit .. Last_Bit Component_Bit_Offset
374 -- old new old new
59ac57b5 375
6797073f 376 -- 0 .. 0 7 .. 7 0 7
377 -- 0 .. 1 6 .. 7 0 6
378 -- 0 .. 2 5 .. 7 0 5
379 -- 0 .. 7 0 .. 7 0 4
59ac57b5 380
6797073f 381 -- 1 .. 1 6 .. 6 1 6
382 -- 1 .. 4 3 .. 6 1 3
383 -- 4 .. 7 0 .. 3 4 0
59ac57b5 384
6797073f 385 -- The rule is that the first bit is is obtained by
386 -- subtracting the old ending bit from storage_unit - 1.
59ac57b5 387
6797073f 388 Set_Component_Bit_Offset
389 (Comp,
390 (Storage_Unit_Offset * System_Storage_Unit) +
391 (System_Storage_Unit - 1) -
392 (Start_Bit + CSZ - 1));
59ac57b5 393
6797073f 394 Set_Normalized_First_Bit
395 (Comp,
396 Component_Bit_Offset (Comp) mod
397 System_Storage_Unit);
398 end if;
399 end;
400 end if;
401
402 Next_Component_Or_Discriminant (Comp);
403 end loop;
404
405 -- For Ada 2005, we do machine scalar processing, as fully described In
406 -- AI-133. This involves gathering all components which start at the
407 -- same byte offset and processing them together. Same approach is still
408 -- valid in later versions including Ada 2012.
409
410 else
411 declare
412 Max_Machine_Scalar_Size : constant Uint :=
413 UI_From_Int
414 (Standard_Long_Long_Integer_Size);
67278d60 415 -- We use this as the maximum machine scalar size
59ac57b5 416
6797073f 417 Num_CC : Natural;
418 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
59ac57b5 419
6797073f 420 begin
421 -- This first loop through components does two things. First it
422 -- deals with the case of components with component clauses whose
423 -- length is greater than the maximum machine scalar size (either
424 -- accepting them or rejecting as needed). Second, it counts the
425 -- number of components with component clauses whose length does
426 -- not exceed this maximum for later processing.
67278d60 427
6797073f 428 Num_CC := 0;
429 Comp := First_Component_Or_Discriminant (R);
430 while Present (Comp) loop
431 CC := Component_Clause (Comp);
67278d60 432
6797073f 433 if Present (CC) then
434 declare
1e3532e7 435 Fbit : constant Uint := Static_Integer (First_Bit (CC));
436 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
67278d60 437
6797073f 438 begin
b38e4131 439 -- Case of component with last bit >= max machine scalar
67278d60 440
b38e4131 441 if Lbit >= Max_Machine_Scalar_Size then
67278d60 442
b38e4131 443 -- This is allowed only if first bit is zero, and
444 -- last bit + 1 is a multiple of storage unit size.
67278d60 445
b38e4131 446 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
67278d60 447
b38e4131 448 -- This is the case to give a warning if enabled
67278d60 449
b38e4131 450 if Warn_On_Reverse_Bit_Order then
451 Error_Msg_N
7a41db5b 452 ("info: multi-byte field specified with "
1e3532e7 453 & " non-standard Bit_Order?V?", CC);
b38e4131 454
455 if Bytes_Big_Endian then
456 Error_Msg_N
457 ("\bytes are not reversed "
1e3532e7 458 & "(component is big-endian)?V?", CC);
b38e4131 459 else
460 Error_Msg_N
461 ("\bytes are not reversed "
1e3532e7 462 & "(component is little-endian)?V?", CC);
b38e4131 463 end if;
464 end if;
67278d60 465
7eb0e22f 466 -- Give error message for RM 13.5.1(10) violation
67278d60 467
b38e4131 468 else
469 Error_Msg_FE
470 ("machine scalar rules not followed for&",
471 First_Bit (CC), Comp);
67278d60 472
b38e4131 473 Error_Msg_Uint_1 := Lbit;
474 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
475 Error_Msg_F
476 ("\last bit (^) exceeds maximum machine "
477 & "scalar size (^)",
478 First_Bit (CC));
67278d60 479
b38e4131 480 if (Lbit + 1) mod SSU /= 0 then
481 Error_Msg_Uint_1 := SSU;
482 Error_Msg_F
483 ("\and is not a multiple of Storage_Unit (^) "
0cafb066 484 & "(RM 13.4.1(10))",
b38e4131 485 First_Bit (CC));
6797073f 486
6797073f 487 else
b38e4131 488 Error_Msg_Uint_1 := Fbit;
489 Error_Msg_F
490 ("\and first bit (^) is non-zero "
0cafb066 491 & "(RM 13.4.1(10))",
b38e4131 492 First_Bit (CC));
67278d60 493 end if;
6797073f 494 end if;
59ac57b5 495
b38e4131 496 -- OK case of machine scalar related component clause,
497 -- For now, just count them.
59ac57b5 498
6797073f 499 else
500 Num_CC := Num_CC + 1;
501 end if;
502 end;
503 end if;
59ac57b5 504
6797073f 505 Next_Component_Or_Discriminant (Comp);
506 end loop;
59ac57b5 507
6797073f 508 -- We need to sort the component clauses on the basis of the
509 -- Position values in the clause, so we can group clauses with
4a87c513 510 -- the same Position together to determine the relevant machine
6797073f 511 -- scalar size.
59ac57b5 512
6797073f 513 Sort_CC : declare
514 Comps : array (0 .. Num_CC) of Entity_Id;
515 -- Array to collect component and discriminant entities. The
516 -- data starts at index 1, the 0'th entry is for the sort
517 -- routine.
59ac57b5 518
6797073f 519 function CP_Lt (Op1, Op2 : Natural) return Boolean;
520 -- Compare routine for Sort
59ac57b5 521
6797073f 522 procedure CP_Move (From : Natural; To : Natural);
523 -- Move routine for Sort
59ac57b5 524
6797073f 525 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
59ac57b5 526
6797073f 527 Start : Natural;
528 Stop : Natural;
529 -- Start and stop positions in the component list of the set of
530 -- components with the same starting position (that constitute
531 -- components in a single machine scalar).
59ac57b5 532
6797073f 533 MaxL : Uint;
534 -- Maximum last bit value of any component in this set
59ac57b5 535
6797073f 536 MSS : Uint;
537 -- Corresponding machine scalar size
67278d60 538
6797073f 539 -----------
540 -- CP_Lt --
541 -----------
67278d60 542
6797073f 543 function CP_Lt (Op1, Op2 : Natural) return Boolean is
544 begin
545 return Position (Component_Clause (Comps (Op1))) <
546 Position (Component_Clause (Comps (Op2)));
547 end CP_Lt;
67278d60 548
6797073f 549 -------------
550 -- CP_Move --
551 -------------
67278d60 552
6797073f 553 procedure CP_Move (From : Natural; To : Natural) is
554 begin
555 Comps (To) := Comps (From);
556 end CP_Move;
67278d60 557
4a87c513 558 -- Start of processing for Sort_CC
59ac57b5 559
6797073f 560 begin
b38e4131 561 -- Collect the machine scalar relevant component clauses
59ac57b5 562
6797073f 563 Num_CC := 0;
564 Comp := First_Component_Or_Discriminant (R);
565 while Present (Comp) loop
b38e4131 566 declare
567 CC : constant Node_Id := Component_Clause (Comp);
568
569 begin
570 -- Collect only component clauses whose last bit is less
571 -- than machine scalar size. Any component clause whose
572 -- last bit exceeds this value does not take part in
573 -- machine scalar layout considerations. The test for
574 -- Error_Posted makes sure we exclude component clauses
575 -- for which we already posted an error.
576
577 if Present (CC)
578 and then not Error_Posted (Last_Bit (CC))
579 and then Static_Integer (Last_Bit (CC)) <
d64221a7 580 Max_Machine_Scalar_Size
b38e4131 581 then
582 Num_CC := Num_CC + 1;
583 Comps (Num_CC) := Comp;
584 end if;
585 end;
59ac57b5 586
6797073f 587 Next_Component_Or_Discriminant (Comp);
588 end loop;
67278d60 589
6797073f 590 -- Sort by ascending position number
67278d60 591
6797073f 592 Sorting.Sort (Num_CC);
67278d60 593
6797073f 594 -- We now have all the components whose size does not exceed
595 -- the max machine scalar value, sorted by starting position.
596 -- In this loop we gather groups of clauses starting at the
597 -- same position, to process them in accordance with AI-133.
67278d60 598
6797073f 599 Stop := 0;
600 while Stop < Num_CC loop
601 Start := Stop + 1;
602 Stop := Start;
603 MaxL :=
604 Static_Integer
605 (Last_Bit (Component_Clause (Comps (Start))));
67278d60 606 while Stop < Num_CC loop
6797073f 607 if Static_Integer
608 (Position (Component_Clause (Comps (Stop + 1)))) =
609 Static_Integer
610 (Position (Component_Clause (Comps (Stop))))
611 then
612 Stop := Stop + 1;
613 MaxL :=
614 UI_Max
615 (MaxL,
616 Static_Integer
617 (Last_Bit
618 (Component_Clause (Comps (Stop)))));
619 else
620 exit;
621 end if;
622 end loop;
67278d60 623
6797073f 624 -- Now we have a group of component clauses from Start to
625 -- Stop whose positions are identical, and MaxL is the
626 -- maximum last bit value of any of these components.
627
628 -- We need to determine the corresponding machine scalar
629 -- size. This loop assumes that machine scalar sizes are
630 -- even, and that each possible machine scalar has twice
631 -- as many bits as the next smaller one.
632
633 MSS := Max_Machine_Scalar_Size;
634 while MSS mod 2 = 0
635 and then (MSS / 2) >= SSU
636 and then (MSS / 2) > MaxL
637 loop
638 MSS := MSS / 2;
639 end loop;
67278d60 640
6797073f 641 -- Here is where we fix up the Component_Bit_Offset value
642 -- to account for the reverse bit order. Some examples of
643 -- what needs to be done for the case of a machine scalar
644 -- size of 8 are:
67278d60 645
6797073f 646 -- First_Bit .. Last_Bit Component_Bit_Offset
647 -- old new old new
67278d60 648
6797073f 649 -- 0 .. 0 7 .. 7 0 7
650 -- 0 .. 1 6 .. 7 0 6
651 -- 0 .. 2 5 .. 7 0 5
652 -- 0 .. 7 0 .. 7 0 4
67278d60 653
6797073f 654 -- 1 .. 1 6 .. 6 1 6
655 -- 1 .. 4 3 .. 6 1 3
656 -- 4 .. 7 0 .. 3 4 0
67278d60 657
6797073f 658 -- The rule is that the first bit is obtained by subtracting
659 -- the old ending bit from machine scalar size - 1.
67278d60 660
6797073f 661 for C in Start .. Stop loop
662 declare
663 Comp : constant Entity_Id := Comps (C);
b9e61b2a 664 CC : constant Node_Id := Component_Clause (Comp);
665
666 LB : constant Uint := Static_Integer (Last_Bit (CC));
6797073f 667 NFB : constant Uint := MSS - Uint_1 - LB;
668 NLB : constant Uint := NFB + Esize (Comp) - 1;
b9e61b2a 669 Pos : constant Uint := Static_Integer (Position (CC));
67278d60 670
6797073f 671 begin
672 if Warn_On_Reverse_Bit_Order then
673 Error_Msg_Uint_1 := MSS;
674 Error_Msg_N
675 ("info: reverse bit order in machine " &
1e3532e7 676 "scalar of length^?V?", First_Bit (CC));
6797073f 677 Error_Msg_Uint_1 := NFB;
678 Error_Msg_Uint_2 := NLB;
679
680 if Bytes_Big_Endian then
681 Error_Msg_NE
7a41db5b 682 ("\big-endian range for component "
683 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
6797073f 684 else
685 Error_Msg_NE
7a41db5b 686 ("\little-endian range for component"
687 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
67278d60 688 end if;
6797073f 689 end if;
67278d60 690
6797073f 691 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
692 Set_Normalized_First_Bit (Comp, NFB mod SSU);
693 end;
67278d60 694 end loop;
6797073f 695 end loop;
696 end Sort_CC;
697 end;
698 end if;
59ac57b5 699 end Adjust_Record_For_Reverse_Bit_Order;
700
1d366b32 701 -------------------------------------
702 -- Alignment_Check_For_Size_Change --
703 -------------------------------------
d6f39728 704
1d366b32 705 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
d6f39728 706 begin
707 -- If the alignment is known, and not set by a rep clause, and is
708 -- inconsistent with the size being set, then reset it to unknown,
709 -- we assume in this case that the size overrides the inherited
710 -- alignment, and that the alignment must be recomputed.
711
712 if Known_Alignment (Typ)
713 and then not Has_Alignment_Clause (Typ)
1d366b32 714 and then Size mod (Alignment (Typ) * SSU) /= 0
d6f39728 715 then
716 Init_Alignment (Typ);
717 end if;
1d366b32 718 end Alignment_Check_For_Size_Change;
d6f39728 719
06ef5f86 720 -------------------------------------
721 -- Analyze_Aspects_At_Freeze_Point --
722 -------------------------------------
723
724 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
725 ASN : Node_Id;
726 A_Id : Aspect_Id;
727 Ritem : Node_Id;
728
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
730 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
731 -- the aspect specification node ASN.
732
37c6e44c 733 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
734 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
735 -- a derived type can inherit aspects from its parent which have been
736 -- specified at the time of the derivation using an aspect, as in:
737 --
738 -- type A is range 1 .. 10
739 -- with Size => Not_Defined_Yet;
740 -- ..
741 -- type B is new A;
742 -- ..
743 -- Not_Defined_Yet : constant := 64;
744 --
745 -- In this example, the Size of A is considered to be specified prior
746 -- to the derivation, and thus inherited, even though the value is not
747 -- known at the time of derivation. To deal with this, we use two entity
748 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
749 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
750 -- the derived type (B here). If this flag is set when the derived type
751 -- is frozen, then this procedure is called to ensure proper inheritance
b21edad9 752 -- of all delayed aspects from the parent type. The derived type is E,
37c6e44c 753 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
754 -- aspect specification node in the Rep_Item chain for the parent type.
755
06ef5f86 756 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
757 -- Given an aspect specification node ASN whose expression is an
758 -- optional Boolean, this routines creates the corresponding pragma
759 -- at the freezing point.
760
761 ----------------------------------
762 -- Analyze_Aspect_Default_Value --
763 ----------------------------------
764
765 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
766 Ent : constant Entity_Id := Entity (ASN);
767 Expr : constant Node_Id := Expression (ASN);
768 Id : constant Node_Id := Identifier (ASN);
769
770 begin
771 Error_Msg_Name_1 := Chars (Id);
772
773 if not Is_Type (Ent) then
774 Error_Msg_N ("aspect% can only apply to a type", Id);
775 return;
776
777 elsif not Is_First_Subtype (Ent) then
778 Error_Msg_N ("aspect% cannot apply to subtype", Id);
779 return;
780
781 elsif A_Id = Aspect_Default_Value
782 and then not Is_Scalar_Type (Ent)
783 then
784 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
785 return;
786
787 elsif A_Id = Aspect_Default_Component_Value then
788 if not Is_Array_Type (Ent) then
789 Error_Msg_N ("aspect% can only be applied to array type", Id);
790 return;
791
792 elsif not Is_Scalar_Type (Component_Type (Ent)) then
793 Error_Msg_N ("aspect% requires scalar components", Id);
794 return;
795 end if;
796 end if;
797
798 Set_Has_Default_Aspect (Base_Type (Ent));
799
800 if Is_Scalar_Type (Ent) then
9f36e3fb 801 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
06ef5f86 802 else
f3d70f08 803 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
06ef5f86 804 end if;
805 end Analyze_Aspect_Default_Value;
806
37c6e44c 807 ---------------------------------
808 -- Inherit_Delayed_Rep_Aspects --
809 ---------------------------------
810
811 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
812 P : constant Entity_Id := Entity (ASN);
813 -- Entithy for parent type
814
815 N : Node_Id;
816 -- Item from Rep_Item chain
817
818 A : Aspect_Id;
819
820 begin
821 -- Loop through delayed aspects for the parent type
822
823 N := ASN;
824 while Present (N) loop
825 if Nkind (N) = N_Aspect_Specification then
826 exit when Entity (N) /= P;
827
828 if Is_Delayed_Aspect (N) then
829 A := Get_Aspect_Id (Chars (Identifier (N)));
830
831 -- Process delayed rep aspect. For Boolean attributes it is
832 -- not possible to cancel an attribute once set (the attempt
833 -- to use an aspect with xxx => False is an error) for a
834 -- derived type. So for those cases, we do not have to check
835 -- if a clause has been given for the derived type, since it
836 -- is harmless to set it again if it is already set.
837
838 case A is
839
840 -- Alignment
841
842 when Aspect_Alignment =>
843 if not Has_Alignment_Clause (E) then
844 Set_Alignment (E, Alignment (P));
845 end if;
846
847 -- Atomic
848
849 when Aspect_Atomic =>
850 if Is_Atomic (P) then
851 Set_Is_Atomic (E);
852 end if;
853
854 -- Atomic_Components
855
856 when Aspect_Atomic_Components =>
857 if Has_Atomic_Components (P) then
858 Set_Has_Atomic_Components (Base_Type (E));
859 end if;
860
861 -- Bit_Order
862
863 when Aspect_Bit_Order =>
864 if Is_Record_Type (E)
865 and then No (Get_Attribute_Definition_Clause
866 (E, Attribute_Bit_Order))
867 and then Reverse_Bit_Order (P)
868 then
869 Set_Reverse_Bit_Order (Base_Type (E));
870 end if;
871
872 -- Component_Size
873
874 when Aspect_Component_Size =>
875 if Is_Array_Type (E)
876 and then not Has_Component_Size_Clause (E)
877 then
878 Set_Component_Size
879 (Base_Type (E), Component_Size (P));
880 end if;
881
882 -- Machine_Radix
883
884 when Aspect_Machine_Radix =>
885 if Is_Decimal_Fixed_Point_Type (E)
886 and then not Has_Machine_Radix_Clause (E)
887 then
888 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
889 end if;
890
891 -- Object_Size (also Size which also sets Object_Size)
892
893 when Aspect_Object_Size | Aspect_Size =>
894 if not Has_Size_Clause (E)
895 and then
896 No (Get_Attribute_Definition_Clause
897 (E, Attribute_Object_Size))
898 then
899 Set_Esize (E, Esize (P));
900 end if;
901
902 -- Pack
903
904 when Aspect_Pack =>
905 if not Is_Packed (E) then
906 Set_Is_Packed (Base_Type (E));
907
908 if Is_Bit_Packed_Array (P) then
909 Set_Is_Bit_Packed_Array (Base_Type (E));
a88a5773 910 Set_Packed_Array_Impl_Type
911 (E, Packed_Array_Impl_Type (P));
37c6e44c 912 end if;
913 end if;
914
915 -- Scalar_Storage_Order
916
917 when Aspect_Scalar_Storage_Order =>
918 if (Is_Record_Type (E) or else Is_Array_Type (E))
919 and then No (Get_Attribute_Definition_Clause
e163cac8 920 (E, Attribute_Scalar_Storage_Order))
37c6e44c 921 and then Reverse_Storage_Order (P)
922 then
923 Set_Reverse_Storage_Order (Base_Type (E));
b64082f2 924
925 -- Clear default SSO indications, since the aspect
926 -- overrides the default.
927
928 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
929 Set_SSO_Set_High_By_Default (Base_Type (E), False);
37c6e44c 930 end if;
931
932 -- Small
933
934 when Aspect_Small =>
935 if Is_Fixed_Point_Type (E)
936 and then not Has_Small_Clause (E)
937 then
938 Set_Small_Value (E, Small_Value (P));
939 end if;
940
941 -- Storage_Size
942
943 when Aspect_Storage_Size =>
944 if (Is_Access_Type (E) or else Is_Task_Type (E))
945 and then not Has_Storage_Size_Clause (E)
946 then
947 Set_Storage_Size_Variable
948 (Base_Type (E), Storage_Size_Variable (P));
949 end if;
950
951 -- Value_Size
952
953 when Aspect_Value_Size =>
954
955 -- Value_Size is never inherited, it is either set by
956 -- default, or it is explicitly set for the derived
957 -- type. So nothing to do here.
958
959 null;
960
961 -- Volatile
962
963 when Aspect_Volatile =>
964 if Is_Volatile (P) then
965 Set_Is_Volatile (E);
966 end if;
967
2fe893b9 968 -- Volatile_Full_Access
969
970 when Aspect_Volatile_Full_Access =>
971 if Has_Volatile_Full_Access (P) then
972 Set_Has_Volatile_Full_Access (E);
973 end if;
974
37c6e44c 975 -- Volatile_Components
976
977 when Aspect_Volatile_Components =>
978 if Has_Volatile_Components (P) then
979 Set_Has_Volatile_Components (Base_Type (E));
980 end if;
981
982 -- That should be all the Rep Aspects
983
984 when others =>
985 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
986 null;
987
988 end case;
989 end if;
990 end if;
991
992 N := Next_Rep_Item (N);
993 end loop;
994 end Inherit_Delayed_Rep_Aspects;
995
06ef5f86 996 -------------------------------------
997 -- Make_Pragma_From_Boolean_Aspect --
998 -------------------------------------
999
1000 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1001 Ident : constant Node_Id := Identifier (ASN);
1002 A_Name : constant Name_Id := Chars (Ident);
1003 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1004 Ent : constant Entity_Id := Entity (ASN);
1005 Expr : constant Node_Id := Expression (ASN);
1006 Loc : constant Source_Ptr := Sloc (ASN);
1007
1008 Prag : Node_Id;
1009
1010 procedure Check_False_Aspect_For_Derived_Type;
1011 -- This procedure checks for the case of a false aspect for a derived
1012 -- type, which improperly tries to cancel an aspect inherited from
1013 -- the parent.
1014
1015 -----------------------------------------
1016 -- Check_False_Aspect_For_Derived_Type --
1017 -----------------------------------------
1018
1019 procedure Check_False_Aspect_For_Derived_Type is
1020 Par : Node_Id;
1021
1022 begin
1023 -- We are only checking derived types
1024
1025 if not Is_Derived_Type (E) then
1026 return;
1027 end if;
1028
1029 Par := Nearest_Ancestor (E);
1030
1031 case A_Id is
1032 when Aspect_Atomic | Aspect_Shared =>
1033 if not Is_Atomic (Par) then
1034 return;
1035 end if;
1036
1037 when Aspect_Atomic_Components =>
1038 if not Has_Atomic_Components (Par) then
1039 return;
1040 end if;
1041
1042 when Aspect_Discard_Names =>
1043 if not Discard_Names (Par) then
1044 return;
1045 end if;
1046
1047 when Aspect_Pack =>
1048 if not Is_Packed (Par) then
1049 return;
1050 end if;
1051
1052 when Aspect_Unchecked_Union =>
1053 if not Is_Unchecked_Union (Par) then
1054 return;
1055 end if;
1056
1057 when Aspect_Volatile =>
1058 if not Is_Volatile (Par) then
1059 return;
1060 end if;
1061
1062 when Aspect_Volatile_Components =>
1063 if not Has_Volatile_Components (Par) then
1064 return;
1065 end if;
1066
2fe893b9 1067 when Aspect_Volatile_Full_Access =>
1068 if not Has_Volatile_Full_Access (Par) then
1069 return;
1070 end if;
1071
06ef5f86 1072 when others =>
1073 return;
1074 end case;
1075
1076 -- Fall through means we are canceling an inherited aspect
1077
1078 Error_Msg_Name_1 := A_Name;
37c6e44c 1079 Error_Msg_NE
1080 ("derived type& inherits aspect%, cannot cancel", Expr, E);
06ef5f86 1081 end Check_False_Aspect_For_Derived_Type;
1082
1083 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1084
1085 begin
37c6e44c 1086 -- Note that we know Expr is present, because for a missing Expr
1087 -- argument, we knew it was True and did not need to delay the
1088 -- evaluation to the freeze point.
1089
06ef5f86 1090 if Is_False (Static_Boolean (Expr)) then
1091 Check_False_Aspect_For_Derived_Type;
1092
1093 else
1094 Prag :=
1095 Make_Pragma (Loc,
1096 Pragma_Argument_Associations => New_List (
57cd943b 1097 Make_Pragma_Argument_Association (Sloc (Ident),
1098 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1099
06ef5f86 1100 Pragma_Identifier =>
1101 Make_Identifier (Sloc (Ident), Chars (Ident)));
1102
1103 Set_From_Aspect_Specification (Prag, True);
1104 Set_Corresponding_Aspect (Prag, ASN);
1105 Set_Aspect_Rep_Item (ASN, Prag);
1106 Set_Is_Delayed_Aspect (Prag);
1107 Set_Parent (Prag, ASN);
1108 end if;
06ef5f86 1109 end Make_Pragma_From_Boolean_Aspect;
1110
1111 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1112
1113 begin
29a9d4be 1114 -- Must be visible in current scope
06ef5f86 1115
ace3389d 1116 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
06ef5f86 1117 return;
1118 end if;
1119
1120 -- Look for aspect specification entries for this entity
1121
1122 ASN := First_Rep_Item (E);
06ef5f86 1123 while Present (ASN) loop
37c6e44c 1124 if Nkind (ASN) = N_Aspect_Specification then
1125 exit when Entity (ASN) /= E;
06ef5f86 1126
37c6e44c 1127 if Is_Delayed_Aspect (ASN) then
1128 A_Id := Get_Aspect_Id (ASN);
1129
1130 case A_Id is
e4c87fa5 1131
37c6e44c 1132 -- For aspects whose expression is an optional Boolean, make
7d6fb253 1133 -- the corresponding pragma at the freeze point.
06ef5f86 1134
7d6fb253 1135 when Boolean_Aspects |
1136 Library_Unit_Aspects =>
1137 Make_Pragma_From_Boolean_Aspect (ASN);
06ef5f86 1138
37c6e44c 1139 -- Special handling for aspects that don't correspond to
1140 -- pragmas/attributes.
06ef5f86 1141
7d6fb253 1142 when Aspect_Default_Value |
1143 Aspect_Default_Component_Value =>
81c2bc19 1144
1145 -- Do not inherit aspect for anonymous base type of a
1146 -- scalar or array type, because they apply to the first
1147 -- subtype of the type, and will be processed when that
1148 -- first subtype is frozen.
1149
1150 if Is_Derived_Type (E)
1151 and then not Comes_From_Source (E)
1152 and then E /= First_Subtype (E)
1153 then
1154 null;
1155 else
1156 Analyze_Aspect_Default_Value (ASN);
1157 end if;
06ef5f86 1158
37c6e44c 1159 -- Ditto for iterator aspects, because the corresponding
1160 -- attributes may not have been analyzed yet.
af9fed8f 1161
7d6fb253 1162 when Aspect_Constant_Indexing |
1163 Aspect_Variable_Indexing |
1164 Aspect_Default_Iterator |
1165 Aspect_Iterator_Element =>
1166 Analyze (Expression (ASN));
af9fed8f 1167
7d6fb253 1168 if Etype (Expression (ASN)) = Any_Type then
1169 Error_Msg_NE
1170 ("\aspect must be fully defined before & is frozen",
1171 ASN, E);
1172 end if;
b3f8228a 1173
7d6fb253 1174 when Aspect_Iterable =>
1175 Validate_Iterable_Aspect (E, ASN);
1176
1177 when others =>
1178 null;
37c6e44c 1179 end case;
06ef5f86 1180
37c6e44c 1181 Ritem := Aspect_Rep_Item (ASN);
06ef5f86 1182
37c6e44c 1183 if Present (Ritem) then
1184 Analyze (Ritem);
1185 end if;
06ef5f86 1186 end if;
1187 end if;
1188
1189 Next_Rep_Item (ASN);
1190 end loop;
37c6e44c 1191
1192 -- This is where we inherit delayed rep aspects from our parent. Note
1193 -- that if we fell out of the above loop with ASN non-empty, it means
1194 -- we hit an aspect for an entity other than E, and it must be the
1195 -- type from which we were derived.
1196
1197 if May_Inherit_Delayed_Rep_Aspects (E) then
1198 Inherit_Delayed_Rep_Aspects (ASN);
1199 end if;
06ef5f86 1200 end Analyze_Aspects_At_Freeze_Point;
1201
ae888dbd 1202 -----------------------------------
1203 -- Analyze_Aspect_Specifications --
1204 -----------------------------------
1205
21ea3a4f 1206 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
e2bf777d 1207 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
c4369687 1208 -- Establish linkages between an aspect and its corresponding
e2bf777d 1209 -- 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
5cc6f0cf 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
1447 begin
b9e61b2a 1448 if not Is_Type (E) or else not Has_Discriminants (E) then
89f1e35c 1449 Error_Msg_N
51ea9c94 1450 ("aspect must apply to a type with discriminants", N);
21ea3a4f 1451
89f1e35c 1452 else
1453 declare
1454 Disc : Entity_Id;
21ea3a4f 1455
89f1e35c 1456 begin
1457 Disc := First_Discriminant (E);
89f1e35c 1458 while Present (Disc) loop
1459 if Chars (Expr) = Chars (Disc)
1460 and then Ekind (Etype (Disc)) =
1461 E_Anonymous_Access_Type
1462 then
1463 Set_Has_Implicit_Dereference (E);
1464 Set_Has_Implicit_Dereference (Disc);
1465 return;
1466 end if;
21ea3a4f 1467
89f1e35c 1468 Next_Discriminant (Disc);
1469 end loop;
21ea3a4f 1470
89f1e35c 1471 -- Error if no proper access discriminant.
21ea3a4f 1472
89f1e35c 1473 Error_Msg_NE
1474 ("not an access discriminant of&", Expr, E);
1475 end;
1476 end if;
1477 end Analyze_Aspect_Implicit_Dereference;
21ea3a4f 1478
0fd13d32 1479 -----------------------
1480 -- Make_Aitem_Pragma --
1481 -----------------------
1482
1483 procedure Make_Aitem_Pragma
1484 (Pragma_Argument_Associations : List_Id;
1485 Pragma_Name : Name_Id)
1486 is
b855559d 1487 Args : List_Id := Pragma_Argument_Associations;
1488
0fd13d32 1489 begin
1490 -- We should never get here if aspect was disabled
1491
1492 pragma Assert (not Is_Disabled (Aspect));
1493
056dc987 1494 -- Certain aspects allow for an optional name or expression. Do
1495 -- not generate a pragma with empty argument association list.
b855559d 1496
1497 if No (Args) or else No (Expression (First (Args))) then
1498 Args := No_List;
1499 end if;
1500
0fd13d32 1501 -- Build the pragma
1502
1503 Aitem :=
1504 Make_Pragma (Loc,
b855559d 1505 Pragma_Argument_Associations => Args,
0fd13d32 1506 Pragma_Identifier =>
1507 Make_Identifier (Sloc (Id), Pragma_Name),
9ab32fe9 1508 Class_Present => Class_Present (Aspect),
1509 Split_PPC => Split_PPC (Aspect));
0fd13d32 1510
1511 -- Set additional semantic fields
1512
1513 if Is_Ignored (Aspect) then
1514 Set_Is_Ignored (Aitem);
57d8d1f3 1515 elsif Is_Checked (Aspect) then
a5109493 1516 Set_Is_Checked (Aitem);
0fd13d32 1517 end if;
1518
1519 Set_Corresponding_Aspect (Aitem, Aspect);
1520 Set_From_Aspect_Specification (Aitem, True);
1521 end Make_Aitem_Pragma;
1522
1523 -- Start of processing for Analyze_One_Aspect
1524
ae888dbd 1525 begin
2d1acfa7 1526 -- Skip aspect if already analyzed, to avoid looping in some cases
fb7f2fc4 1527
1528 if Analyzed (Aspect) then
1529 goto Continue;
1530 end if;
1531
ef957022 1532 -- Skip looking at aspect if it is totally disabled. Just mark it
1533 -- as such for later reference in the tree. This also sets the
1534 -- Is_Ignored and Is_Checked flags appropriately.
51ea9c94 1535
1536 Check_Applicable_Policy (Aspect);
1537
1538 if Is_Disabled (Aspect) then
1539 goto Continue;
1540 end if;
1541
c0793fff 1542 -- Set the source location of expression, used in the case of
1543 -- a failed precondition/postcondition or invariant. Note that
1544 -- the source location of the expression is not usually the best
1545 -- choice here. For example, it gets located on the last AND
1546 -- keyword in a chain of boolean expressiond AND'ed together.
1547 -- It is best to put the message on the first character of the
1548 -- assertion, which is the effect of the First_Node call here.
1549
1550 if Present (Expr) then
1551 Eloc := Sloc (First_Node (Expr));
1552 end if;
1553
d7ed83a2 1554 -- Check restriction No_Implementation_Aspect_Specifications
1555
c171e1be 1556 if Implementation_Defined_Aspect (A_Id) then
d7ed83a2 1557 Check_Restriction
1558 (No_Implementation_Aspect_Specifications, Aspect);
1559 end if;
1560
1561 -- Check restriction No_Specification_Of_Aspect
1562
1563 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1564
f67ed4f5 1565 -- Mark aspect analyzed (actual analysis is delayed till later)
d7ed83a2 1566
fb7f2fc4 1567 Set_Analyzed (Aspect);
d74fc39a 1568 Set_Entity (Aspect, E);
1569 Ent := New_Occurrence_Of (E, Sloc (Id));
1570
1e3c4ae6 1571 -- Check for duplicate aspect. Note that the Comes_From_Source
1572 -- test allows duplicate Pre/Post's that we generate internally
1573 -- to escape being flagged here.
ae888dbd 1574
6c545057 1575 if No_Duplicates_Allowed (A_Id) then
1576 Anod := First (L);
1577 while Anod /= Aspect loop
c171e1be 1578 if Comes_From_Source (Aspect)
1579 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
6c545057 1580 then
1581 Error_Msg_Name_1 := Nam;
1582 Error_Msg_Sloc := Sloc (Anod);
39e1f22f 1583
6c545057 1584 -- Case of same aspect specified twice
39e1f22f 1585
6c545057 1586 if Class_Present (Anod) = Class_Present (Aspect) then
1587 if not Class_Present (Anod) then
1588 Error_Msg_NE
1589 ("aspect% for & previously given#",
1590 Id, E);
1591 else
1592 Error_Msg_NE
1593 ("aspect `%''Class` for & previously given#",
1594 Id, E);
1595 end if;
39e1f22f 1596 end if;
6c545057 1597 end if;
ae888dbd 1598
6c545057 1599 Next (Anod);
1600 end loop;
1601 end if;
ae888dbd 1602
4db325e6 1603 -- Check some general restrictions on language defined aspects
1604
c171e1be 1605 if not Implementation_Defined_Aspect (A_Id) then
4db325e6 1606 Error_Msg_Name_1 := Nam;
1607
1608 -- Not allowed for renaming declarations
1609
1610 if Nkind (N) in N_Renaming_Declaration then
1611 Error_Msg_N
1612 ("aspect % not allowed for renaming declaration",
1613 Aspect);
1614 end if;
1615
1616 -- Not allowed for formal type declarations
1617
1618 if Nkind (N) = N_Formal_Type_Declaration then
1619 Error_Msg_N
1620 ("aspect % not allowed for formal type declaration",
1621 Aspect);
1622 end if;
1623 end if;
1624
7d20685d 1625 -- Copy expression for later processing by the procedures
1626 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1627
1628 Set_Entity (Id, New_Copy_Tree (Expr));
1629
37c6e44c 1630 -- Set Delay_Required as appropriate to aspect
1631
1632 case Aspect_Delay (A_Id) is
1633 when Always_Delay =>
1634 Delay_Required := True;
1635
1636 when Never_Delay =>
1637 Delay_Required := False;
1638
1639 when Rep_Aspect =>
1640
1641 -- If expression has the form of an integer literal, then
1642 -- do not delay, since we know the value cannot change.
1643 -- This optimization catches most rep clause cases.
1644
e43fc5c5 1645 -- For Boolean aspects, don't delay if no expression
1646
1647 if A_Id in Boolean_Aspects and then No (Expr) then
1648 Delay_Required := False;
1649
1650 -- For non-Boolean aspects, don't delay if integer literal
1651
1652 elsif A_Id not in Boolean_Aspects
1653 and then Present (Expr)
1654 and then Nkind (Expr) = N_Integer_Literal
1655 then
1656 Delay_Required := False;
1657
1658 -- All other cases are delayed
1659
1660 else
1661 Delay_Required := True;
1662 Set_Has_Delayed_Rep_Aspects (E);
1663 end if;
37c6e44c 1664 end case;
1665
ae888dbd 1666 -- Processing based on specific aspect
1667
d74fc39a 1668 case A_Id is
aa2f48d2 1669 when Aspect_Unimplemented =>
1670 null; -- ??? temp for now
ae888dbd 1671
1672 -- No_Aspect should be impossible
1673
1674 when No_Aspect =>
1675 raise Program_Error;
1676
89f1e35c 1677 -- Case 1: Aspects corresponding to attribute definition
1678 -- clauses.
ae888dbd 1679
b7b74740 1680 when Aspect_Address |
1681 Aspect_Alignment |
1682 Aspect_Bit_Order |
1683 Aspect_Component_Size |
89f1e35c 1684 Aspect_Constant_Indexing |
89f1e35c 1685 Aspect_Default_Iterator |
1686 Aspect_Dispatching_Domain |
b7b74740 1687 Aspect_External_Tag |
1688 Aspect_Input |
b3f8228a 1689 Aspect_Iterable |
89f1e35c 1690 Aspect_Iterator_Element |
b7b74740 1691 Aspect_Machine_Radix |
1692 Aspect_Object_Size |
1693 Aspect_Output |
1694 Aspect_Read |
1695 Aspect_Scalar_Storage_Order |
1696 Aspect_Size |
1697 Aspect_Small |
1698 Aspect_Simple_Storage_Pool |
1699 Aspect_Storage_Pool |
b7b74740 1700 Aspect_Stream_Size |
1701 Aspect_Value_Size |
89f1e35c 1702 Aspect_Variable_Indexing |
b7b74740 1703 Aspect_Write =>
d74fc39a 1704
89f1e35c 1705 -- Indexing aspects apply only to tagged type
1706
1707 if (A_Id = Aspect_Constant_Indexing
37c6e44c 1708 or else
1709 A_Id = Aspect_Variable_Indexing)
89f1e35c 1710 and then not (Is_Type (E)
1711 and then Is_Tagged_Type (E))
1712 then
05987af3 1713 Error_Msg_N
1714 ("indexing aspect can only apply to a tagged type",
3f4c9ffc 1715 Aspect);
89f1e35c 1716 goto Continue;
1717 end if;
1718
39616053 1719 -- For the case of aspect Address, we don't consider that we
588e7f97 1720 -- know the entity is never set in the source, since it is
1721 -- is likely aliasing is occurring.
1722
1723 -- Note: one might think that the analysis of the resulting
1724 -- attribute definition clause would take care of that, but
1725 -- that's not the case since it won't be from source.
1726
1727 if A_Id = Aspect_Address then
1728 Set_Never_Set_In_Source (E, False);
1729 end if;
1730
5ac76cee 1731 -- Correctness of the profile of a stream operation is
1732 -- verified at the freeze point, but we must detect the
1733 -- illegal specification of this aspect for a subtype now,
1734 -- to prevent malformed rep_item chains.
1735
fbf4d6ef 1736 if A_Id = Aspect_Input or else
1737 A_Id = Aspect_Output or else
1738 A_Id = Aspect_Read or else
1739 A_Id = Aspect_Write
5ac76cee 1740 then
fbf4d6ef 1741 if not Is_First_Subtype (E) then
1742 Error_Msg_N
1743 ("local name must be a first subtype", Aspect);
1744 goto Continue;
1745
1746 -- If stream aspect applies to the class-wide type,
1747 -- the generated attribute definition applies to the
1748 -- class-wide type as well.
1749
1750 elsif Class_Present (Aspect) then
1751 Ent :=
1752 Make_Attribute_Reference (Loc,
1753 Prefix => Ent,
1754 Attribute_Name => Name_Class);
1755 end if;
5ac76cee 1756 end if;
1757
d74fc39a 1758 -- Construct the attribute definition clause
1759
1760 Aitem :=
94153a42 1761 Make_Attribute_Definition_Clause (Loc,
d74fc39a 1762 Name => Ent,
ae888dbd 1763 Chars => Chars (Id),
1764 Expression => Relocate_Node (Expr));
1765
af9a0cc3 1766 -- If the address is specified, then we treat the entity as
41f06abf 1767 -- referenced, to avoid spurious warnings. This is analogous
1768 -- to what is done with an attribute definition clause, but
1769 -- here we don't want to generate a reference because this
1770 -- is the point of definition of the entity.
1771
1772 if A_Id = Aspect_Address then
1773 Set_Referenced (E);
1774 end if;
1775
51ea9c94 1776 -- Case 2: Aspects corresponding to pragmas
d74fc39a 1777
89f1e35c 1778 -- Case 2a: Aspects corresponding to pragmas with two
1779 -- arguments, where the first argument is a local name
1780 -- referring to the entity, and the second argument is the
1781 -- aspect definition expression.
ae888dbd 1782
04ae062f 1783 -- Linker_Section/Suppress/Unsuppress
0fd13d32 1784
04ae062f 1785 when Aspect_Linker_Section |
1786 Aspect_Suppress |
1787 Aspect_Unsuppress =>
ae888dbd 1788
0fd13d32 1789 Make_Aitem_Pragma
1790 (Pragma_Argument_Associations => New_List (
1791 Make_Pragma_Argument_Association (Loc,
1792 Expression => New_Occurrence_Of (E, Loc)),
1793 Make_Pragma_Argument_Association (Sloc (Expr),
1794 Expression => Relocate_Node (Expr))),
1795 Pragma_Name => Chars (Id));
57cd943b 1796
0fd13d32 1797 -- Synchronization
d74fc39a 1798
0fd13d32 1799 -- Corresponds to pragma Implemented, construct the pragma
49213728 1800
5bbfbad2 1801 when Aspect_Synchronization =>
0fd13d32 1802 Make_Aitem_Pragma
1803 (Pragma_Argument_Associations => New_List (
1804 Make_Pragma_Argument_Association (Loc,
1805 Expression => New_Occurrence_Of (E, Loc)),
1806 Make_Pragma_Argument_Association (Sloc (Expr),
1807 Expression => Relocate_Node (Expr))),
1808 Pragma_Name => Name_Implemented);
49213728 1809
e2bf777d 1810 -- Attach_Handler
0fd13d32 1811
89f1e35c 1812 when Aspect_Attach_Handler =>
0fd13d32 1813 Make_Aitem_Pragma
1814 (Pragma_Argument_Associations => New_List (
1815 Make_Pragma_Argument_Association (Sloc (Ent),
1816 Expression => Ent),
1817 Make_Pragma_Argument_Association (Sloc (Expr),
1818 Expression => Relocate_Node (Expr))),
1819 Pragma_Name => Name_Attach_Handler);
1820
f67ed4f5 1821 -- We need to insert this pragma into the tree to get proper
1822 -- processing and to look valid from a placement viewpoint.
1823
e2bf777d 1824 Insert_Pragma (Aitem);
f67ed4f5 1825 goto Continue;
1826
0fd13d32 1827 -- Dynamic_Predicate, Predicate, Static_Predicate
89f1e35c 1828
1829 when Aspect_Dynamic_Predicate |
1830 Aspect_Predicate |
1831 Aspect_Static_Predicate =>
1832
a47ce82d 1833 -- These aspects apply only to subtypes
1834
1835 if not Is_Type (E) then
1836 Error_Msg_N
1837 ("predicate can only be specified for a subtype",
1838 Aspect);
1839 goto Continue;
7c0c95b8 1840
1841 elsif Is_Incomplete_Type (E) then
1842 Error_Msg_N
1843 ("predicate cannot apply to incomplete view", Aspect);
1844 goto Continue;
a47ce82d 1845 end if;
1846
89f1e35c 1847 -- Construct the pragma (always a pragma Predicate, with
51ea9c94 1848 -- flags recording whether it is static/dynamic). We also
1849 -- set flags recording this in the type itself.
89f1e35c 1850
0fd13d32 1851 Make_Aitem_Pragma
1852 (Pragma_Argument_Associations => New_List (
1853 Make_Pragma_Argument_Association (Sloc (Ent),
1854 Expression => Ent),
1855 Make_Pragma_Argument_Association (Sloc (Expr),
1856 Expression => Relocate_Node (Expr))),
1857 Pragma_Name => Name_Predicate);
89f1e35c 1858
51ea9c94 1859 -- Mark type has predicates, and remember what kind of
1860 -- aspect lead to this predicate (we need this to access
1861 -- the right set of check policies later on).
1862
1863 Set_Has_Predicates (E);
1864
1865 if A_Id = Aspect_Dynamic_Predicate then
1866 Set_Has_Dynamic_Predicate_Aspect (E);
1867 elsif A_Id = Aspect_Static_Predicate then
1868 Set_Has_Static_Predicate_Aspect (E);
1869 end if;
1870
89f1e35c 1871 -- If the type is private, indicate that its completion
6653b695 1872 -- has a freeze node, because that is the one that will
1873 -- be visible at freeze time.
89f1e35c 1874
0fd13d32 1875 if Is_Private_Type (E) and then Present (Full_View (E)) then
89f1e35c 1876 Set_Has_Predicates (Full_View (E));
51ea9c94 1877
1878 if A_Id = Aspect_Dynamic_Predicate then
1879 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1880 elsif A_Id = Aspect_Static_Predicate then
1881 Set_Has_Static_Predicate_Aspect (Full_View (E));
1882 end if;
1883
89f1e35c 1884 Set_Has_Delayed_Aspects (Full_View (E));
1885 Ensure_Freeze_Node (Full_View (E));
1886 end if;
1887
1888 -- Case 2b: Aspects corresponding to pragmas with two
1889 -- arguments, where the second argument is a local name
1890 -- referring to the entity, and the first argument is the
1891 -- aspect definition expression.
ae888dbd 1892
0fd13d32 1893 -- Convention
1894
a5a64273 1895 when Aspect_Convention =>
1896
1897 -- The aspect may be part of the specification of an import
1898 -- or export pragma. Scan the aspect list to gather the
1899 -- other components, if any. The name of the generated
1900 -- pragma is one of Convention/Import/Export.
1901
1902 declare
97bf66e6 1903 Args : constant List_Id := New_List (
1904 Make_Pragma_Argument_Association (Sloc (Expr),
1905 Expression => Relocate_Node (Expr)),
1906 Make_Pragma_Argument_Association (Sloc (Ent),
1907 Expression => Ent));
1908
1909 Imp_Exp_Seen : Boolean := False;
1910 -- Flag set when aspect Import or Export has been seen
1911
1912 Imp_Seen : Boolean := False;
1913 -- Flag set when aspect Import has been seen
1914
1915 Asp : Node_Id;
1916 Asp_Nam : Name_Id;
1917 Extern_Arg : Node_Id;
1918 Link_Arg : Node_Id;
1919 Prag_Nam : Name_Id;
a5a64273 1920
1921 begin
97bf66e6 1922 Extern_Arg := Empty;
1923 Link_Arg := Empty;
1924 Prag_Nam := Chars (Id);
1925
1926 Asp := First (L);
1927 while Present (Asp) loop
1928 Asp_Nam := Chars (Identifier (Asp));
1929
1930 -- Aspects Import and Export take precedence over
1931 -- aspect Convention. As a result the generated pragma
1932 -- must carry the proper interfacing aspect's name.
1933
1934 if Nam_In (Asp_Nam, Name_Import, Name_Export) then
1935 if Imp_Exp_Seen then
1936 Error_Msg_N ("conflicting", Asp);
a5a64273 1937 else
97bf66e6 1938 Imp_Exp_Seen := True;
1939
1940 if Asp_Nam = Name_Import then
1941 Imp_Seen := True;
1942 end if;
a5a64273 1943 end if;
1944
97bf66e6 1945 Prag_Nam := Asp_Nam;
a5a64273 1946
97bf66e6 1947 -- Aspect External_Name adds an extra argument to the
1948 -- generated pragma.
1949
1950 elsif Asp_Nam = Name_External_Name then
1951 Extern_Arg :=
4bba0a8d 1952 Make_Pragma_Argument_Association (Loc,
97bf66e6 1953 Chars => Asp_Nam,
1954 Expression => Relocate_Node (Expression (Asp)));
1955
1956 -- Aspect Link_Name adds an extra argument to the
1957 -- generated pragma.
a5a64273 1958
97bf66e6 1959 elsif Asp_Nam = Name_Link_Name then
1960 Link_Arg :=
4bba0a8d 1961 Make_Pragma_Argument_Association (Loc,
97bf66e6 1962 Chars => Asp_Nam,
1963 Expression => Relocate_Node (Expression (Asp)));
a5a64273 1964 end if;
1965
97bf66e6 1966 Next (Asp);
a5a64273 1967 end loop;
1968
97bf66e6 1969 -- Assemble the full argument list
b9e61b2a 1970
97bf66e6 1971 if Present (Extern_Arg) then
1972 Append_To (Args, Extern_Arg);
a5a64273 1973 end if;
1974
8a1e3cde 1975 if Present (Link_Arg) then
1976 Append_To (Args, Link_Arg);
1977 end if;
1978
0fd13d32 1979 Make_Aitem_Pragma
97bf66e6 1980 (Pragma_Argument_Associations => Args,
1981 Pragma_Name => Prag_Nam);
1982
1983 -- Store the generated pragma Import in the related
1984 -- subprogram.
1985
1986 if Imp_Seen and then Is_Subprogram (E) then
1987 Set_Import_Pragma (E, Aitem);
1988 end if;
a5a64273 1989 end;
e1cedbae 1990
0fd13d32 1991 -- CPU, Interrupt_Priority, Priority
1992
d6814978 1993 -- These three aspects can be specified for a subprogram spec
1994 -- or body, in which case we analyze the expression and export
1995 -- the value of the aspect.
1996
1997 -- Previously, we generated an equivalent pragma for bodies
1998 -- (note that the specs cannot contain these pragmas). The
1999 -- pragma was inserted ahead of local declarations, rather than
2000 -- after the body. This leads to a certain duplication between
2001 -- the processing performed for the aspect and the pragma, but
2002 -- given the straightforward handling required it is simpler
2003 -- to duplicate than to translate the aspect in the spec into
2004 -- a pragma in the declarative part of the body.
3a72f9c3 2005
2006 when Aspect_CPU |
2007 Aspect_Interrupt_Priority |
2008 Aspect_Priority =>
51ea9c94 2009
d6814978 2010 if Nkind_In (N, N_Subprogram_Body,
2011 N_Subprogram_Declaration)
2012 then
2013 -- Analyze the aspect expression
2014
2015 Analyze_And_Resolve (Expr, Standard_Integer);
2016
2017 -- Interrupt_Priority aspect not allowed for main
2018 -- subprograms. ARM D.1 does not forbid this explicitly,
2019 -- but ARM J.15.11 (6/3) does not permit pragma
2020 -- Interrupt_Priority for subprograms.
2021
2022 if A_Id = Aspect_Interrupt_Priority then
2023 Error_Msg_N
2024 ("Interrupt_Priority aspect cannot apply to "
2025 & "subprogram", Expr);
2026
2027 -- The expression must be static
2028
cda40848 2029 elsif not Is_OK_Static_Expression (Expr) then
d6814978 2030 Flag_Non_Static_Expr
2031 ("aspect requires static expression!", Expr);
2032
24d7b9d6 2033 -- Check whether this is the main subprogram. Issue a
2034 -- warning only if it is obviously not a main program
2035 -- (when it has parameters or when the subprogram is
2036 -- within a package).
2037
2038 elsif Present (Parameter_Specifications
2039 (Specification (N)))
2040 or else not Is_Compilation_Unit (Defining_Entity (N))
d6814978 2041 then
2042 -- See ARM D.1 (14/3) and D.16 (12/3)
2043
2044 Error_Msg_N
2045 ("aspect applied to subprogram other than the "
2046 & "main subprogram has no effect??", Expr);
2047
2048 -- Otherwise check in range and export the value
2049
2050 -- For the CPU aspect
2051
2052 elsif A_Id = Aspect_CPU then
2053 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2054
2055 -- Value is correct so we export the value to make
2056 -- it available at execution time.
2057
2058 Set_Main_CPU
2059 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2060
2061 else
2062 Error_Msg_N
2063 ("main subprogram CPU is out of range", Expr);
2064 end if;
2065
2066 -- For the Priority aspect
2067
2068 elsif A_Id = Aspect_Priority then
2069 if Is_In_Range (Expr, RTE (RE_Priority)) then
2070
2071 -- Value is correct so we export the value to make
2072 -- it available at execution time.
2073
2074 Set_Main_Priority
2075 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2076
32572384 2077 -- Ignore pragma if Relaxed_RM_Semantics to support
2078 -- other targets/non GNAT compilers.
2079
2080 elsif not Relaxed_RM_Semantics then
d6814978 2081 Error_Msg_N
2082 ("main subprogram priority is out of range",
2083 Expr);
2084 end if;
2085 end if;
2086
2087 -- Load an arbitrary entity from System.Tasking.Stages
2088 -- or System.Tasking.Restricted.Stages (depending on
2089 -- the supported profile) to make sure that one of these
2090 -- packages is implicitly with'ed, since we need to have
2091 -- the tasking run time active for the pragma Priority to
a0c3eeb9 2092 -- have any effect. Previously we with'ed the package
d6814978 2093 -- System.Tasking, but this package does not trigger the
2094 -- required initialization of the run-time library.
2095
2096 declare
2097 Discard : Entity_Id;
d6814978 2098 begin
2099 if Restricted_Profile then
2100 Discard := RTE (RE_Activate_Restricted_Tasks);
2101 else
2102 Discard := RTE (RE_Activate_Tasks);
2103 end if;
2104 end;
2105
2106 -- Handling for these Aspects in subprograms is complete
2107
2108 goto Continue;
2109
2110 -- For tasks
0fd13d32 2111
3a72f9c3 2112 else
d6814978 2113 -- Pass the aspect as an attribute
2114
3a72f9c3 2115 Aitem :=
2116 Make_Attribute_Definition_Clause (Loc,
2117 Name => Ent,
2118 Chars => Chars (Id),
2119 Expression => Relocate_Node (Expr));
2120 end if;
2121
0fd13d32 2122 -- Warnings
2123
ae888dbd 2124 when Aspect_Warnings =>
0fd13d32 2125 Make_Aitem_Pragma
2126 (Pragma_Argument_Associations => New_List (
2127 Make_Pragma_Argument_Association (Sloc (Expr),
2128 Expression => Relocate_Node (Expr)),
2129 Make_Pragma_Argument_Association (Loc,
2130 Expression => New_Occurrence_Of (E, Loc))),
2131 Pragma_Name => Chars (Id));
ae888dbd 2132
89f1e35c 2133 -- Case 2c: Aspects corresponding to pragmas with three
2134 -- arguments.
d64221a7 2135
89f1e35c 2136 -- Invariant aspects have a first argument that references the
2137 -- entity, a second argument that is the expression and a third
2138 -- argument that is an appropriate message.
d64221a7 2139
0fd13d32 2140 -- Invariant, Type_Invariant
2141
89f1e35c 2142 when Aspect_Invariant |
2143 Aspect_Type_Invariant =>
d64221a7 2144
89f1e35c 2145 -- Analysis of the pragma will verify placement legality:
2146 -- an invariant must apply to a private type, or appear in
2147 -- the private part of a spec and apply to a completion.
d64221a7 2148
0fd13d32 2149 Make_Aitem_Pragma
2150 (Pragma_Argument_Associations => New_List (
2151 Make_Pragma_Argument_Association (Sloc (Ent),
2152 Expression => Ent),
2153 Make_Pragma_Argument_Association (Sloc (Expr),
2154 Expression => Relocate_Node (Expr))),
2155 Pragma_Name => Name_Invariant);
89f1e35c 2156
2157 -- Add message unless exception messages are suppressed
2158
2159 if not Opt.Exception_Locations_Suppressed then
2160 Append_To (Pragma_Argument_Associations (Aitem),
2161 Make_Pragma_Argument_Association (Eloc,
2162 Chars => Name_Message,
2163 Expression =>
2164 Make_String_Literal (Eloc,
2165 Strval => "failed invariant from "
2166 & Build_Location_String (Eloc))));
d64221a7 2167 end if;
2168
89f1e35c 2169 -- For Invariant case, insert immediately after the entity
2170 -- declaration. We do not have to worry about delay issues
2171 -- since the pragma processing takes care of this.
2172
89f1e35c 2173 Delay_Required := False;
d64221a7 2174
47a46747 2175 -- Case 2d : Aspects that correspond to a pragma with one
2176 -- argument.
2177
0fd13d32 2178 -- Abstract_State
115f7b08 2179
d4e369ad 2180 -- Aspect Abstract_State introduces implicit declarations for
2181 -- all state abstraction entities it defines. To emulate this
2182 -- behavior, insert the pragma at the beginning of the visible
2183 -- declarations of the related package so that it is analyzed
2184 -- immediately.
2185
9129c28f 2186 when Aspect_Abstract_State => Abstract_State : declare
eb4f7efa 2187 Context : Node_Id := N;
630b6d55 2188 Decl : Node_Id;
eb4f7efa 2189 Decls : List_Id;
9129c28f 2190
2191 begin
eb4f7efa 2192 -- When aspect Abstract_State appears on a generic package,
2193 -- it is propageted to the package instance. The context in
2194 -- this case is the instance spec.
2195
2196 if Nkind (Context) = N_Package_Instantiation then
2197 Context := Instance_Spec (Context);
2198 end if;
2199
2200 if Nkind_In (Context, N_Generic_Package_Declaration,
2201 N_Package_Declaration)
9129c28f 2202 then
9129c28f 2203 Make_Aitem_Pragma
2204 (Pragma_Argument_Associations => New_List (
2205 Make_Pragma_Argument_Association (Loc,
2206 Expression => Relocate_Node (Expr))),
2207 Pragma_Name => Name_Abstract_State);
e2bf777d 2208 Decorate (Aspect, Aitem);
9129c28f 2209
630b6d55 2210 Decls := Visible_Declarations (Specification (Context));
2211
2212 -- In general pragma Abstract_State must be at the top
2213 -- of the existing visible declarations to emulate its
2214 -- source counterpart. The only exception to this is a
2215 -- generic instance in which case the pragma must be
2216 -- inserted after the association renamings.
2217
2218 if Present (Decls) then
50e44732 2219 Decl := First (Decls);
630b6d55 2220
2221 -- The visible declarations of a generic instance have
2222 -- the following structure:
2223
2224 -- <renamings of generic formals>
2225 -- <renamings of internally-generated spec and body>
2226 -- <first source declaration>
2227
2228 -- The pragma must be inserted before the first source
50e44732 2229 -- declaration, skip the instance "header".
630b6d55 2230
2231 if Is_Generic_Instance (Defining_Entity (Context)) then
630b6d55 2232 while Present (Decl)
2233 and then not Comes_From_Source (Decl)
2234 loop
2235 Decl := Next (Decl);
2236 end loop;
50e44732 2237 end if;
630b6d55 2238
3dbe7a69 2239 -- When aspects Abstract_State, Ghost,
2240 -- Initial_Condition and Initializes are out of order,
2241 -- ensure that pragma SPARK_Mode is always at the top
2242 -- of the declarations to properly enabled/suppress
2243 -- errors.
630b6d55 2244
50e44732 2245 Insert_After_SPARK_Mode
2246 (Prag => Aitem,
2247 Ins_Nod => Decl,
2248 Decls => Decls);
630b6d55 2249
2250 -- Otherwise the pragma forms a new declarative list
2251
2252 else
2253 Set_Visible_Declarations
2254 (Specification (Context), New_List (Aitem));
2255 end if;
9129c28f 2256
2257 else
2258 Error_Msg_NE
2259 ("aspect & must apply to a package declaration",
2260 Aspect, Id);
2261 end if;
2262
2263 goto Continue;
2264 end Abstract_State;
115f7b08 2265
ec6f6da5 2266 -- Aspect Default_Internal_Condition is never delayed because
2267 -- it is equivalent to a source pragma which appears after the
2268 -- related private type. To deal with forward references, the
2269 -- generated pragma is stored in the rep chain of the related
2270 -- private type as types do not carry contracts. The pragma is
2271 -- wrapped inside of a procedure at the freeze point of the
2272 -- private type's full view.
2273
2274 when Aspect_Default_Initial_Condition =>
2275 Make_Aitem_Pragma
2276 (Pragma_Argument_Associations => New_List (
2277 Make_Pragma_Argument_Association (Loc,
2278 Expression => Relocate_Node (Expr))),
2279 Pragma_Name =>
2280 Name_Default_Initial_Condition);
2281
2282 Decorate (Aspect, Aitem);
2283 Insert_Pragma (Aitem);
2284 goto Continue;
2285
647fab54 2286 -- Default_Storage_Pool
2287
2288 when Aspect_Default_Storage_Pool =>
2289 Make_Aitem_Pragma
2290 (Pragma_Argument_Associations => New_List (
2291 Make_Pragma_Argument_Association (Loc,
2292 Expression => Relocate_Node (Expr))),
2293 Pragma_Name =>
2294 Name_Default_Storage_Pool);
2295
2296 Decorate (Aspect, Aitem);
2297 Insert_Pragma (Aitem);
2298 goto Continue;
2299
0fd13d32 2300 -- Depends
2301
e2bf777d 2302 -- Aspect Depends is never delayed because it is equivalent to
2303 -- a source pragma which appears after the related subprogram.
2304 -- To deal with forward references, the generated pragma is
2305 -- stored in the contract of the related subprogram and later
2306 -- analyzed at the end of the declarative region. See routine
2307 -- Analyze_Depends_In_Decl_Part for details.
6144c105 2308
12334c57 2309 when Aspect_Depends =>
0fd13d32 2310 Make_Aitem_Pragma
2311 (Pragma_Argument_Associations => New_List (
2312 Make_Pragma_Argument_Association (Loc,
2313 Expression => Relocate_Node (Expr))),
2314 Pragma_Name => Name_Depends);
2315
e2bf777d 2316 Decorate (Aspect, Aitem);
2317 Insert_Pragma (Aitem);
c1006d6d 2318 goto Continue;
2319
cab27d2a 2320 -- Aspect Extensions_Visible is never delayed because it is
2321 -- equivalent to a source pragma which appears after the
2322 -- related subprogram.
2323
2324 when Aspect_Extensions_Visible =>
2325 Make_Aitem_Pragma
2326 (Pragma_Argument_Associations => New_List (
2327 Make_Pragma_Argument_Association (Loc,
2328 Expression => Relocate_Node (Expr))),
2329 Pragma_Name => Name_Extensions_Visible);
2330
2331 Decorate (Aspect, Aitem);
2332 Insert_Pragma (Aitem);
2333 goto Continue;
2334
3dbe7a69 2335 -- Aspect Ghost is never delayed because it is equivalent to a
2336 -- source pragma which appears at the top of [generic] package
2337 -- declarations or after an object, a [generic] subprogram, or
2338 -- a type declaration.
2339
2340 when Aspect_Ghost => Ghost : declare
2341 Decls : List_Id;
2342
2343 begin
2344 Make_Aitem_Pragma
2345 (Pragma_Argument_Associations => New_List (
2346 Make_Pragma_Argument_Association (Loc,
2347 Expression => Relocate_Node (Expr))),
2348 Pragma_Name => Name_Ghost);
2349
2350 Decorate (Aspect, Aitem);
2351
2352 -- When the aspect applies to a [generic] package, insert
2353 -- the pragma at the top of the visible declarations. This
2354 -- emulates the placement of a source pragma.
2355
2356 if Nkind_In (N, N_Generic_Package_Declaration,
2357 N_Package_Declaration)
2358 then
2359 Decls := Visible_Declarations (Specification (N));
2360
2361 if No (Decls) then
2362 Decls := New_List;
2363 Set_Visible_Declarations (N, Decls);
2364 end if;
2365
2366 -- When aspects Abstract_State, Ghost, Initial_Condition
2367 -- and Initializes are out of order, ensure that pragma
2368 -- SPARK_Mode is always at the top of the declarations to
2369 -- properly enabled/suppress errors.
2370
2371 Insert_After_SPARK_Mode
2372 (Prag => Aitem,
2373 Ins_Nod => First (Decls),
2374 Decls => Decls);
2375
2376 -- Otherwise the context is an object, [generic] subprogram
2377 -- or type declaration.
2378
2379 else
2380 Insert_Pragma (Aitem);
2381 end if;
2382
2383 goto Continue;
2384 end Ghost;
2385
0fd13d32 2386 -- Global
12334c57 2387
e2bf777d 2388 -- Aspect Global is never delayed because it is equivalent to
2389 -- a source pragma which appears after the related subprogram.
2390 -- To deal with forward references, the generated pragma is
2391 -- stored in the contract of the related subprogram and later
2392 -- analyzed at the end of the declarative region. See routine
2393 -- Analyze_Global_In_Decl_Part for details.
3cdbaa5a 2394
2395 when Aspect_Global =>
0fd13d32 2396 Make_Aitem_Pragma
2397 (Pragma_Argument_Associations => New_List (
2398 Make_Pragma_Argument_Association (Loc,
2399 Expression => Relocate_Node (Expr))),
2400 Pragma_Name => Name_Global);
2401
e2bf777d 2402 Decorate (Aspect, Aitem);
2403 Insert_Pragma (Aitem);
c1006d6d 2404 goto Continue;
2405
9c138530 2406 -- Initial_Condition
2407
e2bf777d 2408 -- Aspect Initial_Condition is never delayed because it is
2409 -- equivalent to a source pragma which appears after the
2410 -- related package. To deal with forward references, the
2411 -- generated pragma is stored in the contract of the related
2412 -- package and later analyzed at the end of the declarative
2413 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2414 -- for details.
9c138530 2415
2416 when Aspect_Initial_Condition => Initial_Condition : declare
eb4f7efa 2417 Context : Node_Id := N;
2418 Decls : List_Id;
9c138530 2419
2420 begin
e2bf777d 2421 -- When aspect Initial_Condition appears on a generic
2422 -- package, it is propageted to the package instance. The
2423 -- context in this case is the instance spec.
eb4f7efa 2424
2425 if Nkind (Context) = N_Package_Instantiation then
2426 Context := Instance_Spec (Context);
2427 end if;
2428
2429 if Nkind_In (Context, N_Generic_Package_Declaration,
2430 N_Package_Declaration)
9c138530 2431 then
eb4f7efa 2432 Decls := Visible_Declarations (Specification (Context));
9c138530 2433
2434 Make_Aitem_Pragma
2435 (Pragma_Argument_Associations => New_List (
2436 Make_Pragma_Argument_Association (Loc,
2437 Expression => Relocate_Node (Expr))),
2438 Pragma_Name =>
2439 Name_Initial_Condition);
e2bf777d 2440 Decorate (Aspect, Aitem);
9c138530 2441
2442 if No (Decls) then
2443 Decls := New_List;
eb4f7efa 2444 Set_Visible_Declarations (Context, Decls);
9c138530 2445 end if;
2446
3dbe7a69 2447 -- When aspects Abstract_State, Ghost, Initial_Condition
2448 -- and Initializes are out of order, ensure that pragma
2449 -- SPARK_Mode is always at the top of the declarations to
2450 -- properly enabled/suppress errors.
50e44732 2451
2452 Insert_After_SPARK_Mode
2453 (Prag => Aitem,
2454 Ins_Nod => First (Decls),
2455 Decls => Decls);
9c138530 2456
2457 else
2458 Error_Msg_NE
2459 ("aspect & must apply to a package declaration",
2460 Aspect, Id);
2461 end if;
2462
2463 goto Continue;
2464 end Initial_Condition;
2465
d4e369ad 2466 -- Initializes
2467
e2bf777d 2468 -- Aspect Initializes is never delayed because it is equivalent
2469 -- to a source pragma appearing after the related package. To
2470 -- deal with forward references, the generated pragma is stored
2471 -- in the contract of the related package and later analyzed at
2472 -- the end of the declarative region. For details, see routine
2473 -- Analyze_Initializes_In_Decl_Part.
d4e369ad 2474
2475 when Aspect_Initializes => Initializes : declare
eb4f7efa 2476 Context : Node_Id := N;
2477 Decls : List_Id;
d4e369ad 2478
2479 begin
50e44732 2480 -- When aspect Initializes appears on a generic package,
2481 -- it is propageted to the package instance. The context
2482 -- in this case is the instance spec.
eb4f7efa 2483
2484 if Nkind (Context) = N_Package_Instantiation then
2485 Context := Instance_Spec (Context);
2486 end if;
2487
2488 if Nkind_In (Context, N_Generic_Package_Declaration,
2489 N_Package_Declaration)
d4e369ad 2490 then
eb4f7efa 2491 Decls := Visible_Declarations (Specification (Context));
d4e369ad 2492
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_Initializes);
e2bf777d 2498 Decorate (Aspect, Aitem);
d4e369ad 2499
2500 if No (Decls) then
2501 Decls := New_List;
eb4f7efa 2502 Set_Visible_Declarations (Context, Decls);
d4e369ad 2503 end if;
2504
3dbe7a69 2505 -- When aspects Abstract_State, Ghost, Initial_Condition
2506 -- and Initializes are out of order, ensure that pragma
2507 -- SPARK_Mode is always at the top of the declarations to
2508 -- properly enabled/suppress errors.
50e44732 2509
2510 Insert_After_SPARK_Mode
2511 (Prag => Aitem,
2512 Ins_Nod => First (Decls),
2513 Decls => Decls);
d4e369ad 2514
2515 else
2516 Error_Msg_NE
2517 ("aspect & must apply to a package declaration",
2518 Aspect, Id);
2519 end if;
2520
2521 goto Continue;
2522 end Initializes;
2523
1fd4313f 2524 -- Obsolescent
2525
2526 when Aspect_Obsolescent => declare
2527 Args : List_Id;
2528
2529 begin
2530 if No (Expr) then
2531 Args := No_List;
2532 else
2533 Args := New_List (
2534 Make_Pragma_Argument_Association (Sloc (Expr),
2535 Expression => Relocate_Node (Expr)));
2536 end if;
2537
2538 Make_Aitem_Pragma
2539 (Pragma_Argument_Associations => Args,
2540 Pragma_Name => Chars (Id));
2541 end;
2542
5cc6f0cf 2543 -- Part_Of
2544
2545 when Aspect_Part_Of =>
2546 if Nkind_In (N, N_Object_Declaration,
2547 N_Package_Instantiation)
2548 then
2549 Make_Aitem_Pragma
2550 (Pragma_Argument_Associations => New_List (
2551 Make_Pragma_Argument_Association (Loc,
2552 Expression => Relocate_Node (Expr))),
2553 Pragma_Name => Name_Part_Of);
2554
2555 else
2556 Error_Msg_NE
2557 ("aspect & must apply to a variable or package "
2558 & "instantiation", Aspect, Id);
2559 end if;
2560
5dd93a61 2561 -- SPARK_Mode
2562
778ebf56 2563 when Aspect_SPARK_Mode => SPARK_Mode : declare
2564 Decls : List_Id;
2565
2566 begin
5dd93a61 2567 Make_Aitem_Pragma
2568 (Pragma_Argument_Associations => New_List (
2569 Make_Pragma_Argument_Association (Loc,
2570 Expression => Relocate_Node (Expr))),
2571 Pragma_Name => Name_SPARK_Mode);
5dd93a61 2572
b90d9656 2573 -- When the aspect appears on a package or a subprogram
2574 -- body, insert the generated pragma at the top of the body
2575 -- declarations to emulate the behavior of a source pragma.
778ebf56 2576
b90d9656 2577 if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
e2bf777d 2578 Decorate (Aspect, Aitem);
5cc6f0cf 2579
778ebf56 2580 Decls := Declarations (N);
2581
2582 if No (Decls) then
2583 Decls := New_List;
2584 Set_Declarations (N, Decls);
2585 end if;
2586
a04f9d2e 2587 Prepend_To (Decls, Aitem);
2588 goto Continue;
2589
b90d9656 2590 -- When the aspect is associated with a [generic] package
2591 -- declaration, insert the generated pragma at the top of
2592 -- the visible declarations to emulate the behavior of a
2593 -- source pragma.
a04f9d2e 2594
b90d9656 2595 elsif Nkind_In (N, N_Generic_Package_Declaration,
2596 N_Package_Declaration)
2597 then
e2bf777d 2598 Decorate (Aspect, Aitem);
5cc6f0cf 2599
a04f9d2e 2600 Decls := Visible_Declarations (Specification (N));
2601
2602 if No (Decls) then
2603 Decls := New_List;
2604 Set_Visible_Declarations (Specification (N), Decls);
2605 end if;
2606
778ebf56 2607 Prepend_To (Decls, Aitem);
2608 goto Continue;
2609 end if;
2610 end SPARK_Mode;
2611
4befb1a0 2612 -- Refined_Depends
2613
e2bf777d 2614 -- Aspect Refined_Depends is never delayed because it is
2615 -- equivalent to a source pragma which appears in the
2616 -- declarations of the related subprogram body. To deal with
2617 -- forward references, the generated pragma is stored in the
2618 -- contract of the related subprogram body and later analyzed
2619 -- at the end of the declarative region. For details, see
2620 -- routine Analyze_Refined_Depends_In_Decl_Part.
4befb1a0 2621
2622 when Aspect_Refined_Depends =>
422073ed 2623 Make_Aitem_Pragma
2624 (Pragma_Argument_Associations => New_List (
2625 Make_Pragma_Argument_Association (Loc,
2626 Expression => Relocate_Node (Expr))),
2627 Pragma_Name => Name_Refined_Depends);
2628
e2bf777d 2629 Decorate (Aspect, Aitem);
2630 Insert_Pragma (Aitem);
422073ed 2631 goto Continue;
4befb1a0 2632
2633 -- Refined_Global
2634
e2bf777d 2635 -- Aspect Refined_Global is never delayed because it is
2636 -- equivalent to a source pragma which appears in the
2637 -- declarations of the related subprogram body. To deal with
2638 -- forward references, the generated pragma is stored in the
2639 -- contract of the related subprogram body and later analyzed
2640 -- at the end of the declarative region. For details, see
2641 -- routine Analyze_Refined_Global_In_Decl_Part.
4befb1a0 2642
2643 when Aspect_Refined_Global =>
28ff117f 2644 Make_Aitem_Pragma
2645 (Pragma_Argument_Associations => New_List (
2646 Make_Pragma_Argument_Association (Loc,
2647 Expression => Relocate_Node (Expr))),
2648 Pragma_Name => Name_Refined_Global);
2649
e2bf777d 2650 Decorate (Aspect, Aitem);
2651 Insert_Pragma (Aitem);
28ff117f 2652 goto Continue;
4befb1a0 2653
63b65b2d 2654 -- Refined_Post
2655
2656 when Aspect_Refined_Post =>
2657 Make_Aitem_Pragma
2658 (Pragma_Argument_Associations => New_List (
2659 Make_Pragma_Argument_Association (Loc,
2660 Expression => Relocate_Node (Expr))),
2661 Pragma_Name => Name_Refined_Post);
2662
9129c28f 2663 -- Refined_State
2664
2665 when Aspect_Refined_State => Refined_State : declare
2666 Decls : List_Id;
2667
2668 begin
2669 -- The corresponding pragma for Refined_State is inserted in
2670 -- the declarations of the related package body. This action
2671 -- synchronizes both the source and from-aspect versions of
2672 -- the pragma.
2673
2674 if Nkind (N) = N_Package_Body then
e2bf777d 2675 Decls := Declarations (N);
2676
9129c28f 2677 Make_Aitem_Pragma
2678 (Pragma_Argument_Associations => New_List (
2679 Make_Pragma_Argument_Association (Loc,
2680 Expression => Relocate_Node (Expr))),
2681 Pragma_Name => Name_Refined_State);
e2bf777d 2682 Decorate (Aspect, Aitem);
b9b2d6e5 2683
e2bf777d 2684 if No (Decls) then
2685 Decls := New_List;
2686 Set_Declarations (N, Decls);
2687 end if;
b9b2d6e5 2688
e2bf777d 2689 -- Pragma Refined_State must be inserted after pragma
2690 -- SPARK_Mode in the tree. This ensures that any error
2691 -- messages dependent on SPARK_Mode will be properly
2692 -- enabled/suppressed.
b9b2d6e5 2693
e2bf777d 2694 Insert_After_SPARK_Mode
2695 (Prag => Aitem,
2696 Ins_Nod => First (Decls),
2697 Decls => Decls);
9129c28f 2698
2699 else
2700 Error_Msg_NE
2701 ("aspect & must apply to a package body", Aspect, Id);
2702 end if;
2703
2704 goto Continue;
2705 end Refined_State;
2706
0fd13d32 2707 -- Relative_Deadline
3cdbaa5a 2708
2709 when Aspect_Relative_Deadline =>
0fd13d32 2710 Make_Aitem_Pragma
2711 (Pragma_Argument_Associations => New_List (
2712 Make_Pragma_Argument_Association (Loc,
2713 Expression => Relocate_Node (Expr))),
2714 Pragma_Name => Name_Relative_Deadline);
47a46747 2715
2716 -- If the aspect applies to a task, the corresponding pragma
2717 -- must appear within its declarations, not after.
2718
2719 if Nkind (N) = N_Task_Type_Declaration then
2720 declare
2721 Def : Node_Id;
2722 V : List_Id;
2723
2724 begin
2725 if No (Task_Definition (N)) then
2726 Set_Task_Definition (N,
2727 Make_Task_Definition (Loc,
2728 Visible_Declarations => New_List,
2729 End_Label => Empty));
2730 end if;
2731
2732 Def := Task_Definition (N);
2733 V := Visible_Declarations (Def);
2734 if not Is_Empty_List (V) then
2735 Insert_Before (First (V), Aitem);
2736
2737 else
2738 Set_Visible_Declarations (Def, New_List (Aitem));
2739 end if;
2740
2741 goto Continue;
2742 end;
2743 end if;
2744
956ffaf4 2745 -- Case 2e: Annotate aspect
2746
2747 when Aspect_Annotate =>
2748 declare
2749 Args : List_Id;
2750 Pargs : List_Id;
2751 Arg : Node_Id;
2752
2753 begin
2754 -- The argument can be a single identifier
2755
2756 if Nkind (Expr) = N_Identifier then
2757
2758 -- One level of parens is allowed
2759
2760 if Paren_Count (Expr) > 1 then
2761 Error_Msg_F ("extra parentheses ignored", Expr);
2762 end if;
2763
2764 Set_Paren_Count (Expr, 0);
2765
2766 -- Add the single item to the list
2767
2768 Args := New_List (Expr);
2769
2770 -- Otherwise we must have an aggregate
2771
2772 elsif Nkind (Expr) = N_Aggregate then
2773
2774 -- Must be positional
2775
2776 if Present (Component_Associations (Expr)) then
2777 Error_Msg_F
2778 ("purely positional aggregate required", Expr);
2779 goto Continue;
2780 end if;
2781
2782 -- Must not be parenthesized
2783
2784 if Paren_Count (Expr) /= 0 then
2785 Error_Msg_F ("extra parentheses ignored", Expr);
2786 end if;
2787
2788 -- List of arguments is list of aggregate expressions
2789
2790 Args := Expressions (Expr);
2791
2792 -- Anything else is illegal
2793
2794 else
2795 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2796 goto Continue;
2797 end if;
2798
2799 -- Prepare pragma arguments
2800
2801 Pargs := New_List;
2802 Arg := First (Args);
2803 while Present (Arg) loop
2804 Append_To (Pargs,
2805 Make_Pragma_Argument_Association (Sloc (Arg),
2806 Expression => Relocate_Node (Arg)));
2807 Next (Arg);
2808 end loop;
2809
2810 Append_To (Pargs,
2811 Make_Pragma_Argument_Association (Sloc (Ent),
2812 Chars => Name_Entity,
2813 Expression => Ent));
2814
2815 Make_Aitem_Pragma
2816 (Pragma_Argument_Associations => Pargs,
2817 Pragma_Name => Name_Annotate);
2818 end;
2819
89f1e35c 2820 -- Case 3 : Aspects that don't correspond to pragma/attribute
2821 -- definition clause.
7b9b2f05 2822
89f1e35c 2823 -- Case 3a: The aspects listed below don't correspond to
2824 -- pragmas/attributes but do require delayed analysis.
7f694ca2 2825
51fa2a45 2826 -- Default_Value can only apply to a scalar type
2827
2828 when Aspect_Default_Value =>
2829 if not Is_Scalar_Type (E) then
2830 Error_Msg_N
1089ff19 2831 ("aspect Default_Value must apply to a scalar type", N);
51fa2a45 2832 end if;
2833
2834 Aitem := Empty;
2835
2836 -- Default_Component_Value can only apply to an array type
2837 -- with scalar components.
2838
2839 when Aspect_Default_Component_Value =>
2840 if not (Is_Array_Type (E)
3f4c9ffc 2841 and then Is_Scalar_Type (Component_Type (E)))
51fa2a45 2842 then
2843 Error_Msg_N ("aspect Default_Component_Value can only "
2844 & "apply to an array of scalar components", N);
2845 end if;
0fd13d32 2846
89f1e35c 2847 Aitem := Empty;
7f694ca2 2848
89f1e35c 2849 -- Case 3b: The aspects listed below don't correspond to
2850 -- pragmas/attributes and don't need delayed analysis.
95bc75fa 2851
0fd13d32 2852 -- Implicit_Dereference
2853
89f1e35c 2854 -- For Implicit_Dereference, External_Name and Link_Name, only
2855 -- the legality checks are done during the analysis, thus no
2856 -- delay is required.
a8e38e1d 2857
89f1e35c 2858 when Aspect_Implicit_Dereference =>
2859 Analyze_Aspect_Implicit_Dereference;
2860 goto Continue;
7f694ca2 2861
0fd13d32 2862 -- External_Name, Link_Name
2863
89f1e35c 2864 when Aspect_External_Name |
2865 Aspect_Link_Name =>
2866 Analyze_Aspect_External_Or_Link_Name;
2867 goto Continue;
7f694ca2 2868
0fd13d32 2869 -- Dimension
2870
89f1e35c 2871 when Aspect_Dimension =>
2872 Analyze_Aspect_Dimension (N, Id, Expr);
2873 goto Continue;
cb4c311d 2874
0fd13d32 2875 -- Dimension_System
2876
89f1e35c 2877 when Aspect_Dimension_System =>
2878 Analyze_Aspect_Dimension_System (N, Id, Expr);
2879 goto Continue;
7f694ca2 2880
ceec4f7c 2881 -- Case 4: Aspects requiring special handling
51ea9c94 2882
e66f4e2a 2883 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2884 -- pragmas take care of the delay.
7f694ca2 2885
0fd13d32 2886 -- Pre/Post
2887
1e3c4ae6 2888 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2889 -- with a first argument that is the expression, and a second
2890 -- argument that is an informative message if the test fails.
2891 -- This is inserted right after the declaration, to get the
5b5df4a9 2892 -- required pragma placement. The processing for the pragmas
2893 -- takes care of the required delay.
ae888dbd 2894
5ddd846b 2895 when Pre_Post_Aspects => Pre_Post : declare
1e3c4ae6 2896 Pname : Name_Id;
ae888dbd 2897
1e3c4ae6 2898 begin
77ae6789 2899 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1e3c4ae6 2900 Pname := Name_Precondition;
2901 else
2902 Pname := Name_Postcondition;
2903 end if;
d74fc39a 2904
1e3c4ae6 2905 -- If the expressions is of the form A and then B, then
2906 -- we generate separate Pre/Post aspects for the separate
2907 -- clauses. Since we allow multiple pragmas, there is no
2908 -- problem in allowing multiple Pre/Post aspects internally.
a273015d 2909 -- These should be treated in reverse order (B first and
2910 -- A second) since they are later inserted just after N in
2911 -- the order they are treated. This way, the pragma for A
2912 -- ends up preceding the pragma for B, which may have an
2913 -- importance for the error raised (either constraint error
2914 -- or precondition error).
1e3c4ae6 2915
39e1f22f 2916 -- We do not do this for Pre'Class, since we have to put
51fa2a45 2917 -- these conditions together in a complex OR expression.
ae888dbd 2918
4282d342 2919 -- We do not do this in ASIS mode, as ASIS relies on the
2920 -- original node representing the complete expression, when
2921 -- retrieving it through the source aspect table.
2922
2923 if not ASIS_Mode
2924 and then (Pname = Name_Postcondition
2925 or else not Class_Present (Aspect))
39e1f22f 2926 then
2927 while Nkind (Expr) = N_And_Then loop
2928 Insert_After (Aspect,
a273015d 2929 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
39e1f22f 2930 Identifier => Identifier (Aspect),
a273015d 2931 Expression => Relocate_Node (Left_Opnd (Expr)),
39e1f22f 2932 Class_Present => Class_Present (Aspect),
2933 Split_PPC => True));
a273015d 2934 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
39e1f22f 2935 Eloc := Sloc (Expr);
2936 end loop;
2937 end if;
ae888dbd 2938
48d6f069 2939 -- Build the precondition/postcondition pragma
2940
51fa2a45 2941 -- Add note about why we do NOT need Copy_Tree here???
d74fc39a 2942
0fd13d32 2943 Make_Aitem_Pragma
2944 (Pragma_Argument_Associations => New_List (
2945 Make_Pragma_Argument_Association (Eloc,
2946 Chars => Name_Check,
a19e1763 2947 Expression => Relocate_Node (Expr))),
0fd13d32 2948 Pragma_Name => Pname);
39e1f22f 2949
2950 -- Add message unless exception messages are suppressed
2951
2952 if not Opt.Exception_Locations_Suppressed then
2953 Append_To (Pragma_Argument_Associations (Aitem),
2954 Make_Pragma_Argument_Association (Eloc,
ed695684 2955 Chars => Name_Message,
39e1f22f 2956 Expression =>
2957 Make_String_Literal (Eloc,
2958 Strval => "failed "
2959 & Get_Name_String (Pname)
2960 & " from "
2961 & Build_Location_String (Eloc))));
2962 end if;
d74fc39a 2963
7d20685d 2964 Set_Is_Delayed_Aspect (Aspect);
d74fc39a 2965
1e3c4ae6 2966 -- For Pre/Post cases, insert immediately after the entity
2967 -- declaration, since that is the required pragma placement.
2968 -- Note that for these aspects, we do not have to worry
2969 -- about delay issues, since the pragmas themselves deal
2970 -- with delay of visibility for the expression analysis.
2971
e2bf777d 2972 Insert_Pragma (Aitem);
299b347e 2973
1e3c4ae6 2974 goto Continue;
5ddd846b 2975 end Pre_Post;
ae888dbd 2976
0fd13d32 2977 -- Test_Case
2978
e66f4e2a 2979 when Aspect_Test_Case => Test_Case : declare
2980 Args : List_Id;
2981 Comp_Expr : Node_Id;
2982 Comp_Assn : Node_Id;
2983 New_Expr : Node_Id;
57cd943b 2984
e66f4e2a 2985 begin
2986 Args := New_List;
b0bc40fd 2987
e66f4e2a 2988 if Nkind (Parent (N)) = N_Compilation_Unit then
2989 Error_Msg_Name_1 := Nam;
2990 Error_Msg_N ("incorrect placement of aspect `%`", E);
2991 goto Continue;
2992 end if;
6c545057 2993
e66f4e2a 2994 if Nkind (Expr) /= N_Aggregate then
2995 Error_Msg_Name_1 := Nam;
2996 Error_Msg_NE
2997 ("wrong syntax for aspect `%` for &", Id, E);
2998 goto Continue;
2999 end if;
6c545057 3000
e66f4e2a 3001 -- Make pragma expressions refer to the original aspect
51fa2a45 3002 -- expressions through the Original_Node link. This is used
3003 -- in semantic analysis for ASIS mode, so that the original
3004 -- expression also gets analyzed.
e66f4e2a 3005
3006 Comp_Expr := First (Expressions (Expr));
3007 while Present (Comp_Expr) loop
3008 New_Expr := Relocate_Node (Comp_Expr);
e66f4e2a 3009 Append_To (Args,
3010 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3011 Expression => New_Expr));
3012 Next (Comp_Expr);
3013 end loop;
3014
3015 Comp_Assn := First (Component_Associations (Expr));
3016 while Present (Comp_Assn) loop
3017 if List_Length (Choices (Comp_Assn)) /= 1
3018 or else
3019 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3020 then
fad014fe 3021 Error_Msg_Name_1 := Nam;
6c545057 3022 Error_Msg_NE
fad014fe 3023 ("wrong syntax for aspect `%` for &", Id, E);
6c545057 3024 goto Continue;
3025 end if;
3026
e66f4e2a 3027 Append_To (Args,
3028 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
ed695684 3029 Chars => Chars (First (Choices (Comp_Assn))),
3030 Expression =>
3031 Relocate_Node (Expression (Comp_Assn))));
e66f4e2a 3032 Next (Comp_Assn);
3033 end loop;
6c545057 3034
e66f4e2a 3035 -- Build the test-case pragma
6c545057 3036
0fd13d32 3037 Make_Aitem_Pragma
3038 (Pragma_Argument_Associations => Args,
3039 Pragma_Name => Nam);
e66f4e2a 3040 end Test_Case;
85696508 3041
0fd13d32 3042 -- Contract_Cases
3043
5ddd846b 3044 when Aspect_Contract_Cases =>
0fd13d32 3045 Make_Aitem_Pragma
3046 (Pragma_Argument_Associations => New_List (
3047 Make_Pragma_Argument_Association (Loc,
3048 Expression => Relocate_Node (Expr))),
3049 Pragma_Name => Nam);
3a128918 3050
e2bf777d 3051 Decorate (Aspect, Aitem);
3052 Insert_Pragma (Aitem);
5ddd846b 3053 goto Continue;
3a128918 3054
89f1e35c 3055 -- Case 5: Special handling for aspects with an optional
3056 -- boolean argument.
85696508 3057
89f1e35c 3058 -- In the general case, the corresponding pragma cannot be
0fd13d32 3059 -- generated yet because the evaluation of the boolean needs
3060 -- to be delayed till the freeze point.
3061
89f1e35c 3062 when Boolean_Aspects |
3063 Library_Unit_Aspects =>
a5a64273 3064
89f1e35c 3065 Set_Is_Boolean_Aspect (Aspect);
a5a64273 3066
89f1e35c 3067 -- Lock_Free aspect only apply to protected objects
e1cedbae 3068
89f1e35c 3069 if A_Id = Aspect_Lock_Free then
3070 if Ekind (E) /= E_Protected_Type then
99a2d5bd 3071 Error_Msg_Name_1 := Nam;
a5a64273 3072 Error_Msg_N
89f1e35c 3073 ("aspect % only applies to a protected object",
3074 Aspect);
3075
3076 else
3077 -- Set the Uses_Lock_Free flag to True if there is no
37c6e44c 3078 -- expression or if the expression is True. The
89f1e35c 3079 -- evaluation of this aspect should be delayed to the
37c6e44c 3080 -- freeze point (why???)
89f1e35c 3081
e81df51c 3082 if No (Expr)
3083 or else Is_True (Static_Boolean (Expr))
89f1e35c 3084 then
3085 Set_Uses_Lock_Free (E);
3086 end if;
caf125ce 3087
3088 Record_Rep_Item (E, Aspect);
a5a64273 3089 end if;
e1cedbae 3090
89f1e35c 3091 goto Continue;
ae888dbd 3092
17631aa0 3093 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
d74fc39a 3094
39616053 3095 -- For the case of aspects Import and Export, we don't
3096 -- consider that we know the entity is never set in the
3097 -- source, since it is is likely modified outside the
3098 -- program.
3099
3100 -- Note: one might think that the analysis of the
3101 -- resulting pragma would take care of that, but
3102 -- that's not the case since it won't be from source.
3103
3104 if Ekind (E) = E_Variable then
3105 Set_Never_Set_In_Source (E, False);
3106 end if;
3107
2e1821c9 3108 -- In older versions of Ada the corresponding pragmas
51fa2a45 3109 -- specified a Convention. In Ada 2012 the convention is
3110 -- specified as a separate aspect, and it is optional,
2e1821c9 3111 -- given that it defaults to Convention_Ada. The code
3112 -- that verifed that there was a matching convention
3113 -- is now obsolete.
d74fc39a 3114
718d0d92 3115 -- Resolve the expression of an Import or Export here,
3116 -- and require it to be of type Boolean and static. This
3117 -- is not quite right, because in general this should be
3118 -- delayed, but that seems tricky for these, because
3119 -- normally Boolean aspects are replaced with pragmas at
3120 -- the freeze point (in Make_Pragma_From_Boolean_Aspect),
3121 -- but in the case of these aspects we can't generate
3122 -- a simple pragma with just the entity name. ???
3123
3124 if not Present (Expr)
3125 or else Is_True (Static_Boolean (Expr))
3126 then
3127 if A_Id = Aspect_Import then
3128 Set_Is_Imported (E);
19a2e7b4 3129 Set_Has_Completion (E);
dd4c44af 3130
718d0d92 3131 -- An imported entity cannot have an explicit
3132 -- initialization.
dd4c44af 3133
718d0d92 3134 if Nkind (N) = N_Object_Declaration
3135 and then Present (Expression (N))
3136 then
3137 Error_Msg_N
3138 ("imported entities cannot be initialized "
3139 & "(RM B.1(24))", Expression (N));
3140 end if;
3141
3142 elsif A_Id = Aspect_Export then
3143 Set_Is_Exported (E);
dd4c44af 3144 end if;
3145 end if;
3146
89f1e35c 3147 goto Continue;
3148 end if;
d74fc39a 3149
37c6e44c 3150 -- Library unit aspects require special handling in the case
3151 -- of a package declaration, the pragma needs to be inserted
3152 -- in the list of declarations for the associated package.
3153 -- There is no issue of visibility delay for these aspects.
d64221a7 3154
89f1e35c 3155 if A_Id in Library_Unit_Aspects
178fec9b 3156 and then
3157 Nkind_In (N, N_Package_Declaration,
3158 N_Generic_Package_Declaration)
89f1e35c 3159 and then Nkind (Parent (N)) /= N_Compilation_Unit
3ad60f63 3160
3161 -- Aspect is legal on a local instantiation of a library-
3162 -- level generic unit.
3163
b94a633e 3164 and then not Is_Generic_Instance (Defining_Entity (N))
89f1e35c 3165 then
3166 Error_Msg_N
dd4c44af 3167 ("incorrect context for library unit aspect&", Id);
89f1e35c 3168 goto Continue;
3169 end if;
cce84b09 3170
294709fa 3171 -- External property aspects are Boolean by nature, but
3172 -- their pragmas must contain two arguments, the second
3173 -- being the optional Boolean expression.
3174
a7ed0410 3175 if A_Id = Aspect_Async_Readers or else
3176 A_Id = Aspect_Async_Writers or else
3177 A_Id = Aspect_Effective_Reads or else
3178 A_Id = Aspect_Effective_Writes
294709fa 3179 then
3180 declare
3181 Args : List_Id;
3182
3183 begin
3184 -- The first argument of the external property pragma
3185 -- is the related object.
3186
a7ed0410 3187 Args :=
3188 New_List (
3189 Make_Pragma_Argument_Association (Sloc (Ent),
3190 Expression => Ent));
294709fa 3191
3192 -- The second argument is the optional Boolean
3193 -- expression which must be propagated even if it
3194 -- evaluates to False as this has special semantic
3195 -- meaning.
3196
3197 if Present (Expr) then
3198 Append_To (Args,
3199 Make_Pragma_Argument_Association (Loc,
3200 Expression => Relocate_Node (Expr)));
3201 end if;
3202
3203 Make_Aitem_Pragma
3204 (Pragma_Argument_Associations => Args,
3205 Pragma_Name => Nam);
3206 end;
3207
51fa2a45 3208 -- Cases where we do not delay, includes all cases where the
3209 -- expression is missing other than the above cases.
d74fc39a 3210
294709fa 3211 elsif not Delay_Required or else No (Expr) then
0fd13d32 3212 Make_Aitem_Pragma
3213 (Pragma_Argument_Associations => New_List (
3214 Make_Pragma_Argument_Association (Sloc (Ent),
3215 Expression => Ent)),
3216 Pragma_Name => Chars (Id));
89f1e35c 3217 Delay_Required := False;
ddf1337b 3218
89f1e35c 3219 -- In general cases, the corresponding pragma/attribute
3220 -- definition clause will be inserted later at the freezing
294709fa 3221 -- point, and we do not need to build it now.
ddf1337b 3222
89f1e35c 3223 else
3224 Aitem := Empty;
3225 end if;
ceec4f7c 3226
3227 -- Storage_Size
3228
3229 -- This is special because for access types we need to generate
3230 -- an attribute definition clause. This also works for single
3231 -- task declarations, but it does not work for task type
3232 -- declarations, because we have the case where the expression
3233 -- references a discriminant of the task type. That can't use
3234 -- an attribute definition clause because we would not have
3235 -- visibility on the discriminant. For that case we must
3236 -- generate a pragma in the task definition.
3237
3238 when Aspect_Storage_Size =>
3239
3240 -- Task type case
3241
3242 if Ekind (E) = E_Task_Type then
3243 declare
3244 Decl : constant Node_Id := Declaration_Node (E);
3245
3246 begin
3247 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3248
3249 -- If no task definition, create one
3250
3251 if No (Task_Definition (Decl)) then
3252 Set_Task_Definition (Decl,
3253 Make_Task_Definition (Loc,
3254 Visible_Declarations => Empty_List,
3255 End_Label => Empty));
3256 end if;
3257
51fa2a45 3258 -- Create a pragma and put it at the start of the task
3259 -- definition for the task type declaration.
ceec4f7c 3260
3261 Make_Aitem_Pragma
3262 (Pragma_Argument_Associations => New_List (
3263 Make_Pragma_Argument_Association (Loc,
3264 Expression => Relocate_Node (Expr))),
3265 Pragma_Name => Name_Storage_Size);
3266
3267 Prepend
3268 (Aitem,
3269 Visible_Declarations (Task_Definition (Decl)));
3270 goto Continue;
3271 end;
3272
3273 -- All other cases, generate attribute definition
3274
3275 else
3276 Aitem :=
3277 Make_Attribute_Definition_Clause (Loc,
3278 Name => Ent,
3279 Chars => Chars (Id),
3280 Expression => Relocate_Node (Expr));
3281 end if;
89f1e35c 3282 end case;
ddf1337b 3283
89f1e35c 3284 -- Attach the corresponding pragma/attribute definition clause to
3285 -- the aspect specification node.
d74fc39a 3286
89f1e35c 3287 if Present (Aitem) then
e2bf777d 3288 Set_From_Aspect_Specification (Aitem);
89f1e35c 3289 end if;
53c179ea 3290
89f1e35c 3291 -- In the context of a compilation unit, we directly put the
0fd13d32 3292 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3293 -- node (no delay is required here) except for aspects on a
51fa2a45 3294 -- subprogram body (see below) and a generic package, for which we
3295 -- need to introduce the pragma before building the generic copy
3296 -- (see sem_ch12), and for package instantiations, where the
3297 -- library unit pragmas are better handled early.
ddf1337b 3298
9129c28f 3299 if Nkind (Parent (N)) = N_Compilation_Unit
89f1e35c 3300 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3301 then
3302 declare
3303 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
7f694ca2 3304
89f1e35c 3305 begin
3306 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
7f694ca2 3307
89f1e35c 3308 -- For a Boolean aspect, create the corresponding pragma if
3309 -- no expression or if the value is True.
7f694ca2 3310
b9e61b2a 3311 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
89f1e35c 3312 if Is_True (Static_Boolean (Expr)) then
0fd13d32 3313 Make_Aitem_Pragma
3314 (Pragma_Argument_Associations => New_List (
3315 Make_Pragma_Argument_Association (Sloc (Ent),
3316 Expression => Ent)),
3317 Pragma_Name => Chars (Id));
7f694ca2 3318
89f1e35c 3319 Set_From_Aspect_Specification (Aitem, True);
3320 Set_Corresponding_Aspect (Aitem, Aspect);
3321
3322 else
3323 goto Continue;
3324 end if;
3325 end if;
7f694ca2 3326
d6814978 3327 -- If the aspect is on a subprogram body (relevant aspect
3328 -- is Inline), add the pragma in front of the declarations.
3a72f9c3 3329
3330 if Nkind (N) = N_Subprogram_Body then
3331 if No (Declarations (N)) then
3332 Set_Declarations (N, New_List);
3333 end if;
3334
3335 Prepend (Aitem, Declarations (N));
3336
178fec9b 3337 elsif Nkind (N) = N_Generic_Package_Declaration then
3338 if No (Visible_Declarations (Specification (N))) then
3339 Set_Visible_Declarations (Specification (N), New_List);
3340 end if;
3341
3342 Prepend (Aitem,
3343 Visible_Declarations (Specification (N)));
3344
c39cce40 3345 elsif Nkind (N) = N_Package_Instantiation then
df8b0dae 3346 declare
3347 Spec : constant Node_Id :=
3348 Specification (Instance_Spec (N));
3349 begin
3350 if No (Visible_Declarations (Spec)) then
3351 Set_Visible_Declarations (Spec, New_List);
3352 end if;
3353
3354 Prepend (Aitem, Visible_Declarations (Spec));
3355 end;
3356
3a72f9c3 3357 else
3358 if No (Pragmas_After (Aux)) then
d4596fbe 3359 Set_Pragmas_After (Aux, New_List);
3a72f9c3 3360 end if;
3361
3362 Append (Aitem, Pragmas_After (Aux));
89f1e35c 3363 end if;
7f694ca2 3364
89f1e35c 3365 goto Continue;
3366 end;
3367 end if;
7f694ca2 3368
89f1e35c 3369 -- The evaluation of the aspect is delayed to the freezing point.
3370 -- The pragma or attribute clause if there is one is then attached
37c6e44c 3371 -- to the aspect specification which is put in the rep item list.
1a814552 3372
89f1e35c 3373 if Delay_Required then
3374 if Present (Aitem) then
3375 Set_Is_Delayed_Aspect (Aitem);
3376 Set_Aspect_Rep_Item (Aspect, Aitem);
3377 Set_Parent (Aitem, Aspect);
3378 end if;
1a814552 3379
89f1e35c 3380 Set_Is_Delayed_Aspect (Aspect);
9f36e3fb 3381
cba2ae82 3382 -- In the case of Default_Value, link the aspect to base type
3383 -- as well, even though it appears on a first subtype. This is
3384 -- mandated by the semantics of the aspect. Do not establish
3385 -- the link when processing the base type itself as this leads
3386 -- to a rep item circularity. Verify that we are dealing with
3387 -- a scalar type to prevent cascaded errors.
3388
3389 if A_Id = Aspect_Default_Value
3390 and then Is_Scalar_Type (E)
3391 and then Base_Type (E) /= E
3392 then
9f36e3fb 3393 Set_Has_Delayed_Aspects (Base_Type (E));
3394 Record_Rep_Item (Base_Type (E), Aspect);
3395 end if;
3396
89f1e35c 3397 Set_Has_Delayed_Aspects (E);
3398 Record_Rep_Item (E, Aspect);
ddf1337b 3399
b855559d 3400 -- When delay is not required and the context is a package or a
3401 -- subprogram body, insert the pragma in the body declarations.
f55ce169 3402
b855559d 3403 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
f55ce169 3404 if No (Declarations (N)) then
3405 Set_Declarations (N, New_List);
3406 end if;
3407
3408 -- The pragma is added before source declarations
3409
3410 Prepend_To (Declarations (N), Aitem);
3411
89f1e35c 3412 -- When delay is not required and the context is not a compilation
3413 -- unit, we simply insert the pragma/attribute definition clause
3414 -- in sequence.
ddf1337b 3415
89f1e35c 3416 else
3417 Insert_After (Ins_Node, Aitem);
3418 Ins_Node := Aitem;
d74fc39a 3419 end if;
0fd13d32 3420 end Analyze_One_Aspect;
ae888dbd 3421
d64221a7 3422 <<Continue>>
3423 Next (Aspect);
21ea3a4f 3424 end loop Aspect_Loop;
89f1e35c 3425
3426 if Has_Delayed_Aspects (E) then
3427 Ensure_Freeze_Node (E);
3428 end if;
21ea3a4f 3429 end Analyze_Aspect_Specifications;
ae888dbd 3430
eb8aeefc 3431 ---------------------------------------------------
3432 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3433 ---------------------------------------------------
3434
3435 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3436 Body_Id : constant Entity_Id := Defining_Entity (N);
3437
3438 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
3439 -- Subprogram body [stub] N has aspects, but they are not properly
3440 -- placed. Emit an error message depending on the aspects involved.
3441 -- Spec_Id is the entity of the corresponding spec.
3442
3443 --------------------------------
3444 -- Diagnose_Misplaced_Aspects --
3445 --------------------------------
3446
3447 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3448 procedure Misplaced_Aspect_Error
3449 (Asp : Node_Id;
3450 Ref_Nam : Name_Id);
3451 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3452 -- the name of the refined version of the aspect.
3453
3454 ----------------------------
3455 -- Misplaced_Aspect_Error --
3456 ----------------------------
3457
3458 procedure Misplaced_Aspect_Error
3459 (Asp : Node_Id;
3460 Ref_Nam : Name_Id)
3461 is
3462 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3463 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3464
3465 begin
3466 -- The corresponding spec already contains the aspect in question
3467 -- and the one appearing on the body must be the refined form:
3468
3469 -- procedure P with Global ...;
3470 -- procedure P with Global ... is ... end P;
3471 -- ^
3472 -- Refined_Global
3473
3474 if Has_Aspect (Spec_Id, Asp_Id) then
3475 Error_Msg_Name_1 := Asp_Nam;
3476
3477 -- Subunits cannot carry aspects that apply to a subprogram
3478 -- declaration.
3479
3480 if Nkind (Parent (N)) = N_Subunit then
3481 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3482
3483 -- Otherwise suggest the refined form
3484
3485 else
3486 Error_Msg_Name_2 := Ref_Nam;
3487 Error_Msg_N ("aspect % should be %", Asp);
3488 end if;
3489
3490 -- Otherwise the aspect must appear on the spec, not on the body
3491
3492 -- procedure P;
3493 -- procedure P with Global ... is ... end P;
3494
3495 else
3496 Error_Msg_N
3497 ("aspect specification must appear in subprogram declaration",
3498 Asp);
3499 end if;
3500 end Misplaced_Aspect_Error;
3501
3502 -- Local variables
3503
3504 Asp : Node_Id;
3505 Asp_Nam : Name_Id;
3506
3507 -- Start of processing for Diagnose_Misplaced_Aspects
3508
3509 begin
3510 -- Iterate over the aspect specifications and emit specific errors
3511 -- where applicable.
3512
3513 Asp := First (Aspect_Specifications (N));
3514 while Present (Asp) loop
3515 Asp_Nam := Chars (Identifier (Asp));
3516
3517 -- Do not emit errors on aspects that can appear on a subprogram
3518 -- body. This scenario occurs when the aspect specification list
3519 -- contains both misplaced and properly placed aspects.
3520
3521 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3522 null;
3523
3524 -- Special diagnostics for SPARK aspects
3525
3526 elsif Asp_Nam = Name_Depends then
3527 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3528
3529 elsif Asp_Nam = Name_Global then
3530 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3531
3532 elsif Asp_Nam = Name_Post then
3533 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3534
3535 -- Otherwise a language-defined aspect is misplaced
3536
3537 else
3538 Error_Msg_N
3539 ("aspect specification must appear in subprogram declaration",
3540 Asp);
3541 end if;
3542
3543 Next (Asp);
3544 end loop;
3545 end Diagnose_Misplaced_Aspects;
3546
3547 -- Local variables
3548
3549 Spec_Id : Entity_Id;
3550
3551 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3552
3553 begin
3554 if Nkind (N) = N_Subprogram_Body_Stub then
3555 Spec_Id := Corresponding_Spec_Of_Stub (N);
3556 else
3557 Spec_Id := Corresponding_Spec (N);
3558 end if;
3559
3560 -- Language-defined aspects cannot be associated with a subprogram body
3561 -- [stub] if the subprogram has a spec. Certain implementation defined
3562 -- aspects are allowed to break this rule (for all applicable cases, see
3563 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3564
3565 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
3566 Diagnose_Misplaced_Aspects (Spec_Id);
3567 else
3568 Analyze_Aspect_Specifications (N, Body_Id);
3569 end if;
3570 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3571
d6f39728 3572 -----------------------
3573 -- Analyze_At_Clause --
3574 -----------------------
3575
3576 -- An at clause is replaced by the corresponding Address attribute
3577 -- definition clause that is the preferred approach in Ada 95.
3578
3579 procedure Analyze_At_Clause (N : Node_Id) is
177675a7 3580 CS : constant Boolean := Comes_From_Source (N);
3581
d6f39728 3582 begin
177675a7 3583 -- This is an obsolescent feature
3584
e0521a36 3585 Check_Restriction (No_Obsolescent_Features, N);
3586
9dfe12ae 3587 if Warn_On_Obsolescent_Feature then
3588 Error_Msg_N
b174444e 3589 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
9dfe12ae 3590 Error_Msg_N
b174444e 3591 ("\?j?use address attribute definition clause instead", N);
9dfe12ae 3592 end if;
3593
177675a7 3594 -- Rewrite as address clause
3595
d6f39728 3596 Rewrite (N,
3597 Make_Attribute_Definition_Clause (Sloc (N),
935e86e0 3598 Name => Identifier (N),
3599 Chars => Name_Address,
d6f39728 3600 Expression => Expression (N)));
177675a7 3601
2beb22b1 3602 -- We preserve Comes_From_Source, since logically the clause still comes
3603 -- from the source program even though it is changed in form.
177675a7 3604
3605 Set_Comes_From_Source (N, CS);
3606
3607 -- Analyze rewritten clause
3608
d6f39728 3609 Analyze_Attribute_Definition_Clause (N);
3610 end Analyze_At_Clause;
3611
3612 -----------------------------------------
3613 -- Analyze_Attribute_Definition_Clause --
3614 -----------------------------------------
3615
3616 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3617 Loc : constant Source_Ptr := Sloc (N);
3618 Nam : constant Node_Id := Name (N);
3619 Attr : constant Name_Id := Chars (N);
3620 Expr : constant Node_Id := Expression (N);
3621 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
d64221a7 3622
3623 Ent : Entity_Id;
3624 -- The entity of Nam after it is analyzed. In the case of an incomplete
3625 -- type, this is the underlying type.
3626
d6f39728 3627 U_Ent : Entity_Id;
d64221a7 3628 -- The underlying entity to which the attribute applies. Generally this
3629 -- is the Underlying_Type of Ent, except in the case where the clause
3630 -- applies to full view of incomplete type or private type in which case
3631 -- U_Ent is just a copy of Ent.
d6f39728 3632
3633 FOnly : Boolean := False;
3634 -- Reset to True for subtype specific attribute (Alignment, Size)
51fa2a45 3635 -- and for stream attributes, i.e. those cases where in the call to
3636 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3637 -- are checked. Note that the case of stream attributes is not clear
3638 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3639 -- Storage_Size for derived task types, but that is also clearly
3640 -- unintentional.
d6f39728 3641
9f373bb8 3642 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3643 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3644 -- definition clauses.
3645
ae888dbd 3646 function Duplicate_Clause return Boolean;
3647 -- This routine checks if the aspect for U_Ent being given by attribute
3648 -- definition clause N is for an aspect that has already been specified,
3649 -- and if so gives an error message. If there is a duplicate, True is
3650 -- returned, otherwise if there is no error, False is returned.
3651
81b424ac 3652 procedure Check_Indexing_Functions;
3653 -- Check that the function in Constant_Indexing or Variable_Indexing
3654 -- attribute has the proper type structure. If the name is overloaded,
cac18f71 3655 -- check that some interpretation is legal.
81b424ac 3656
89cc7147 3657 procedure Check_Iterator_Functions;
3658 -- Check that there is a single function in Default_Iterator attribute
8df4f2a5 3659 -- has the proper type structure.
89cc7147 3660
3661 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
d03bfaa1 3662 -- Common legality check for the previous two
89cc7147 3663
177675a7 3664 -----------------------------------
3665 -- Analyze_Stream_TSS_Definition --
3666 -----------------------------------
3667
9f373bb8 3668 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3669 Subp : Entity_Id := Empty;
3670 I : Interp_Index;
3671 It : Interp;
3672 Pnam : Entity_Id;
3673
3674 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
d64221a7 3675 -- True for Read attribute, false for other attributes
9f373bb8 3676
3677 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3678 -- Return true if the entity is a subprogram with an appropriate
3679 -- profile for the attribute being defined.
3680
3681 ----------------------
3682 -- Has_Good_Profile --
3683 ----------------------
3684
3685 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3686 F : Entity_Id;
3687 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3688 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3689 (False => E_Procedure, True => E_Function);
3690 Typ : Entity_Id;
3691
3692 begin
3693 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3694 return False;
3695 end if;
3696
3697 F := First_Formal (Subp);
3698
3699 if No (F)
3700 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3701 or else Designated_Type (Etype (F)) /=
3702 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3703 then
3704 return False;
3705 end if;
3706
3707 if not Is_Function then
3708 Next_Formal (F);
3709
3710 declare
3711 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3712 (False => E_In_Parameter,
3713 True => E_Out_Parameter);
3714 begin
3715 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3716 return False;
3717 end if;
3718 end;
3719
3720 Typ := Etype (F);
3721
b64082f2 3722 -- If the attribute specification comes from an aspect
51fa2a45 3723 -- specification for a class-wide stream, the parameter must be
3724 -- a class-wide type of the entity to which the aspect applies.
b64082f2 3725
3726 if From_Aspect_Specification (N)
3727 and then Class_Present (Parent (N))
3728 and then Is_Class_Wide_Type (Typ)
3729 then
3730 Typ := Etype (Typ);
3731 end if;
3732
9f373bb8 3733 else
3734 Typ := Etype (Subp);
3735 end if;
3736
51fa2a45 3737 -- Verify that the prefix of the attribute and the local name for
5a8fe506 3738 -- the type of the formal match, or one is the class-wide of the
3739 -- other, in the case of a class-wide stream operation.
48680a09 3740
5a8fe506 3741 if Base_Type (Typ) = Base_Type (Ent)
3742 or else (Is_Class_Wide_Type (Typ)
2be1f7d7 3743 and then Typ = Class_Wide_Type (Base_Type (Ent)))
fbf4d6ef 3744 or else (Is_Class_Wide_Type (Ent)
3745 and then Ent = Class_Wide_Type (Base_Type (Typ)))
5a8fe506 3746 then
3747 null;
3748 else
3749 return False;
3750 end if;
3751
3752 if Present ((Next_Formal (F)))
48680a09 3753 then
3754 return False;
3755
3756 elsif not Is_Scalar_Type (Typ)
3757 and then not Is_First_Subtype (Typ)
3758 and then not Is_Class_Wide_Type (Typ)
3759 then
3760 return False;
3761
3762 else
3763 return True;
3764 end if;
9f373bb8 3765 end Has_Good_Profile;
3766
3767 -- Start of processing for Analyze_Stream_TSS_Definition
3768
3769 begin
3770 FOnly := True;
3771
3772 if not Is_Type (U_Ent) then
3773 Error_Msg_N ("local name must be a subtype", Nam);
3774 return;
48680a09 3775
3776 elsif not Is_First_Subtype (U_Ent) then
3777 Error_Msg_N ("local name must be a first subtype", Nam);
3778 return;
9f373bb8 3779 end if;
3780
3781 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3782
44e4341e 3783 -- If Pnam is present, it can be either inherited from an ancestor
3784 -- type (in which case it is legal to redefine it for this type), or
3785 -- be a previous definition of the attribute for the same type (in
3786 -- which case it is illegal).
3787
3788 -- In the first case, it will have been analyzed already, and we
3789 -- can check that its profile does not match the expected profile
3790 -- for a stream attribute of U_Ent. In the second case, either Pnam
3791 -- has been analyzed (and has the expected profile), or it has not
3792 -- been analyzed yet (case of a type that has not been frozen yet
3793 -- and for which the stream attribute has been set using Set_TSS).
3794
3795 if Present (Pnam)
3796 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3797 then
9f373bb8 3798 Error_Msg_Sloc := Sloc (Pnam);
3799 Error_Msg_Name_1 := Attr;
3800 Error_Msg_N ("% attribute already defined #", Nam);
3801 return;
3802 end if;
3803
3804 Analyze (Expr);
3805
3806 if Is_Entity_Name (Expr) then
3807 if not Is_Overloaded (Expr) then
3808 if Has_Good_Profile (Entity (Expr)) then
3809 Subp := Entity (Expr);
3810 end if;
3811
3812 else
3813 Get_First_Interp (Expr, I, It);
9f373bb8 3814 while Present (It.Nam) loop
3815 if Has_Good_Profile (It.Nam) then
3816 Subp := It.Nam;
3817 exit;
3818 end if;
3819
3820 Get_Next_Interp (I, It);
3821 end loop;
3822 end if;
3823 end if;
3824
3825 if Present (Subp) then
59ac57b5 3826 if Is_Abstract_Subprogram (Subp) then
9f373bb8 3827 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3828 return;
e12b2502 3829
299b347e 3830 -- A stream subprogram for an interface type must be a null
3831 -- procedure (RM 13.13.2 (38/3)).
e12b2502 3832
3833 elsif Is_Interface (U_Ent)
5a8fe506 3834 and then not Is_Class_Wide_Type (U_Ent)
e12b2502 3835 and then not Inside_A_Generic
e12b2502 3836 and then
5a8fe506 3837 (Ekind (Subp) = E_Function
3838 or else
3839 not Null_Present
2be1f7d7 3840 (Specification
3841 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
e12b2502 3842 then
3843 Error_Msg_N
3844 ("stream subprogram for interface type "
3845 & "must be null procedure", Expr);
9f373bb8 3846 end if;
3847
3848 Set_Entity (Expr, Subp);
3849 Set_Etype (Expr, Etype (Subp));
3850
44e4341e 3851 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
9f373bb8 3852
3853 else
3854 Error_Msg_Name_1 := Attr;
3855 Error_Msg_N ("incorrect expression for% attribute", Expr);
3856 end if;
3857 end Analyze_Stream_TSS_Definition;
3858
81b424ac 3859 ------------------------------
3860 -- Check_Indexing_Functions --
3861 ------------------------------
3862
3863 procedure Check_Indexing_Functions is
c8a2d809 3864 Indexing_Found : Boolean := False;
8df4f2a5 3865
81b424ac 3866 procedure Check_One_Function (Subp : Entity_Id);
7796365f 3867 -- Check one possible interpretation. Sets Indexing_Found True if a
3868 -- legal indexing function is found.
81b424ac 3869
05987af3 3870 procedure Illegal_Indexing (Msg : String);
3871 -- Diagnose illegal indexing function if not overloaded. In the
3872 -- overloaded case indicate that no legal interpretation exists.
3873
81b424ac 3874 ------------------------
3875 -- Check_One_Function --
3876 ------------------------
3877
3878 procedure Check_One_Function (Subp : Entity_Id) is
05987af3 3879 Default_Element : Node_Id;
3880 Ret_Type : constant Entity_Id := Etype (Subp);
1b7510f9 3881
81b424ac 3882 begin
05987af3 3883 if not Is_Overloadable (Subp) then
3884 Illegal_Indexing ("illegal indexing function for type&");
3885 return;
3886
7796365f 3887 elsif Scope (Subp) /= Scope (Ent) then
3888 if Nkind (Expr) = N_Expanded_Name then
3889
3890 -- Indexing function can't be declared elsewhere
3891
3892 Illegal_Indexing
3893 ("indexing function must be declared in scope of type&");
3894 end if;
3895
05987af3 3896 return;
3897
3898 elsif No (First_Formal (Subp)) then
3899 Illegal_Indexing
3900 ("Indexing requires a function that applies to type&");
3901 return;
3902
3903 elsif No (Next_Formal (First_Formal (Subp))) then
3904 Illegal_Indexing
2eb0ff42 3905 ("indexing function must have at least two parameters");
05987af3 3906 return;
3907
e7d733fc 3908 -- For a derived type, check that no indexing aspect is specified
3909 -- for the type if it is also inherited
22e724e8 3910
05987af3 3911 elsif Is_Derived_Type (Ent) then
2eb0ff42 3912 declare
3913 Inherited : Node_Id;
05987af3 3914
2eb0ff42 3915 begin
3916 if Attr = Name_Constant_Indexing then
3917 Inherited :=
3918 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
e7d733fc 3919 else pragma Assert (Attr = Name_Variable_Indexing);
2eb0ff42 3920 Inherited :=
3921 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
05987af3 3922 end if;
2eb0ff42 3923
2eb0ff42 3924 if Present (Inherited) then
3925 if Debug_Flag_Dot_XX then
3926 null;
3927
e7d733fc 3928 -- Indicate the operation that must be overridden, rather
3929 -- than redefining the indexing aspect
2eb0ff42 3930
3931 else
3932 Illegal_Indexing
3933 ("indexing function already inherited "
3934 & "from parent type");
3935 Error_Msg_NE
e7d733fc 3936 ("!override & instead",
2eb0ff42 3937 N, Entity (Expression (Inherited)));
3938 return;
3939 end if;
3940 end if;
3941 end;
05987af3 3942 end if;
3943
e81df51c 3944 if not Check_Primitive_Function (Subp) then
05987af3 3945 Illegal_Indexing
3946 ("Indexing aspect requires a function that applies to type&");
3947 return;
81b424ac 3948 end if;
3949
7796365f 3950 -- If partial declaration exists, verify that it is not tagged.
3951
3952 if Ekind (Current_Scope) = E_Package
3953 and then Has_Private_Declaration (Ent)
3954 and then From_Aspect_Specification (N)
7c0c95b8 3955 and then
3956 List_Containing (Parent (Ent)) =
3957 Private_Declarations
7796365f 3958 (Specification (Unit_Declaration_Node (Current_Scope)))
3959 and then Nkind (N) = N_Attribute_Definition_Clause
3960 then
3961 declare
3962 Decl : Node_Id;
3963
3964 begin
3965 Decl :=
3966 First (Visible_Declarations
7c0c95b8 3967 (Specification
3968 (Unit_Declaration_Node (Current_Scope))));
7796365f 3969
3970 while Present (Decl) loop
3971 if Nkind (Decl) = N_Private_Type_Declaration
3972 and then Ent = Full_View (Defining_Identifier (Decl))
3973 and then Tagged_Present (Decl)
3974 and then No (Aspect_Specifications (Decl))
3975 then
3976 Illegal_Indexing
3977 ("Indexing aspect cannot be specified on full view "
7c0c95b8 3978 & "if partial view is tagged");
7796365f 3979 return;
3980 end if;
3981
3982 Next (Decl);
3983 end loop;
3984 end;
3985 end if;
3986
1b7510f9 3987 -- An indexing function must return either the default element of
cac18f71 3988 -- the container, or a reference type. For variable indexing it
a45d946f 3989 -- must be the latter.
1b7510f9 3990
05987af3 3991 Default_Element :=
3992 Find_Value_Of_Aspect
3993 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
3994
1b7510f9 3995 if Present (Default_Element) then
3996 Analyze (Default_Element);
a45d946f 3997
1b7510f9 3998 if Is_Entity_Name (Default_Element)
05987af3 3999 and then not Covers (Entity (Default_Element), Ret_Type)
4000 and then False
1b7510f9 4001 then
05987af3 4002 Illegal_Indexing
4003 ("wrong return type for indexing function");
1b7510f9 4004 return;
4005 end if;
4006 end if;
4007
a45d946f 4008 -- For variable_indexing the return type must be a reference type
1b7510f9 4009
05987af3 4010 if Attr = Name_Variable_Indexing then
4011 if not Has_Implicit_Dereference (Ret_Type) then
4012 Illegal_Indexing
4013 ("variable indexing must return a reference type");
4014 return;
4015
423b89fd 4016 elsif Is_Access_Constant
4017 (Etype (First_Discriminant (Ret_Type)))
05987af3 4018 then
4019 Illegal_Indexing
4020 ("variable indexing must return an access to variable");
4021 return;
4022 end if;
cac18f71 4023
4024 else
05987af3 4025 if Has_Implicit_Dereference (Ret_Type)
4026 and then not
4027 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4028 then
4029 Illegal_Indexing
4030 ("constant indexing must return an access to constant");
4031 return;
4032
4033 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4034 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4035 then
4036 Illegal_Indexing
4037 ("constant indexing must apply to an access to constant");
4038 return;
4039 end if;
81b424ac 4040 end if;
05987af3 4041
4042 -- All checks succeeded.
4043
4044 Indexing_Found := True;
81b424ac 4045 end Check_One_Function;
4046
05987af3 4047 -----------------------
4048 -- Illegal_Indexing --
4049 -----------------------
4050
4051 procedure Illegal_Indexing (Msg : String) is
4052 begin
7796365f 4053 Error_Msg_NE (Msg, N, Ent);
05987af3 4054 end Illegal_Indexing;
4055
81b424ac 4056 -- Start of processing for Check_Indexing_Functions
4057
4058 begin
89cc7147 4059 if In_Instance then
4060 return;
4061 end if;
4062
81b424ac 4063 Analyze (Expr);
4064
4065 if not Is_Overloaded (Expr) then
4066 Check_One_Function (Entity (Expr));
4067
4068 else
4069 declare
2c5754de 4070 I : Interp_Index;
81b424ac 4071 It : Interp;
4072
4073 begin
cac18f71 4074 Indexing_Found := False;
81b424ac 4075 Get_First_Interp (Expr, I, It);
4076 while Present (It.Nam) loop
4077
4078 -- Note that analysis will have added the interpretation
4079 -- that corresponds to the dereference. We only check the
4080 -- subprogram itself.
4081
4082 if Is_Overloadable (It.Nam) then
4083 Check_One_Function (It.Nam);
4084 end if;
4085
4086 Get_Next_Interp (I, It);
4087 end loop;
4088 end;
4089 end if;
7796365f 4090
7c0c95b8 4091 if not Indexing_Found and then not Error_Posted (N) then
7796365f 4092 Error_Msg_NE
4093 ("aspect Indexing requires a local function that "
4094 & "applies to type&", Expr, Ent);
4095 end if;
81b424ac 4096 end Check_Indexing_Functions;
4097
89cc7147 4098 ------------------------------
4099 -- Check_Iterator_Functions --
4100 ------------------------------
4101
4102 procedure Check_Iterator_Functions is
4103 Default : Entity_Id;
4104
4105 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
8df4f2a5 4106 -- Check one possible interpretation for validity
89cc7147 4107
4108 ----------------------------
4109 -- Valid_Default_Iterator --
4110 ----------------------------
4111
4112 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
4113 Formal : Entity_Id;
4114
4115 begin
4116 if not Check_Primitive_Function (Subp) then
4117 return False;
4118 else
4119 Formal := First_Formal (Subp);
4120 end if;
4121
8df4f2a5 4122 -- False if any subsequent formal has no default expression
89cc7147 4123
8df4f2a5 4124 Formal := Next_Formal (Formal);
4125 while Present (Formal) loop
4126 if No (Expression (Parent (Formal))) then
4127 return False;
4128 end if;
89cc7147 4129
8df4f2a5 4130 Next_Formal (Formal);
4131 end loop;
89cc7147 4132
8df4f2a5 4133 -- True if all subsequent formals have default expressions
89cc7147 4134
4135 return True;
4136 end Valid_Default_Iterator;
4137
4138 -- Start of processing for Check_Iterator_Functions
4139
4140 begin
4141 Analyze (Expr);
4142
4143 if not Is_Entity_Name (Expr) then
4144 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4145 end if;
4146
4147 if not Is_Overloaded (Expr) then
4148 if not Check_Primitive_Function (Entity (Expr)) then
4149 Error_Msg_NE
4150 ("aspect Indexing requires a function that applies to type&",
4151 Entity (Expr), Ent);
4152 end if;
4153
05f6f999 4154 -- Flag the default_iterator as well as the denoted function.
4155
89cc7147 4156 if not Valid_Default_Iterator (Entity (Expr)) then
05f6f999 4157 Error_Msg_N ("improper function for default iterator!", Expr);
89cc7147 4158 end if;
4159
4160 else
4161 Default := Empty;
4162 declare
4163 I : Interp_Index;
4164 It : Interp;
4165
4166 begin
4167 Get_First_Interp (Expr, I, It);
4168 while Present (It.Nam) loop
4169 if not Check_Primitive_Function (It.Nam)
59f3e675 4170 or else not Valid_Default_Iterator (It.Nam)
89cc7147 4171 then
4172 Remove_Interp (I);
4173
4174 elsif Present (Default) then
4175 Error_Msg_N ("default iterator must be unique", Expr);
4176
4177 else
4178 Default := It.Nam;
4179 end if;
4180
4181 Get_Next_Interp (I, It);
4182 end loop;
4183 end;
4184
4185 if Present (Default) then
4186 Set_Entity (Expr, Default);
4187 Set_Is_Overloaded (Expr, False);
4188 end if;
4189 end if;
4190 end Check_Iterator_Functions;
4191
4192 -------------------------------
4193 -- Check_Primitive_Function --
4194 -------------------------------
4195
4196 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4197 Ctrl : Entity_Id;
4198
4199 begin
4200 if Ekind (Subp) /= E_Function then
4201 return False;
4202 end if;
4203
4204 if No (First_Formal (Subp)) then
4205 return False;
4206 else
4207 Ctrl := Etype (First_Formal (Subp));
4208 end if;
4209
05f6f999 4210 -- To be a primitive operation subprogram has to be in same scope.
4211
4212 if Scope (Ctrl) /= Scope (Subp) then
4213 return False;
4214 end if;
4215
7d6fb253 4216 -- Type of formal may be the class-wide type, an access to such,
4217 -- or an incomplete view.
4218
89cc7147 4219 if Ctrl = Ent
4220 or else Ctrl = Class_Wide_Type (Ent)
4221 or else
4222 (Ekind (Ctrl) = E_Anonymous_Access_Type
b85d62ec 4223 and then (Designated_Type (Ctrl) = Ent
4224 or else
4225 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
7d6fb253 4226 or else
4227 (Ekind (Ctrl) = E_Incomplete_Type
4228 and then Full_View (Ctrl) = Ent)
89cc7147 4229 then
4230 null;
89cc7147 4231 else
4232 return False;
4233 end if;
4234
4235 return True;
4236 end Check_Primitive_Function;
4237
ae888dbd 4238 ----------------------
4239 -- Duplicate_Clause --
4240 ----------------------
4241
4242 function Duplicate_Clause return Boolean is
d74fc39a 4243 A : Node_Id;
ae888dbd 4244
4245 begin
c8969ba6 4246 -- Nothing to do if this attribute definition clause comes from
4247 -- an aspect specification, since we could not be duplicating an
ae888dbd 4248 -- explicit clause, and we dealt with the case of duplicated aspects
4249 -- in Analyze_Aspect_Specifications.
4250
4251 if From_Aspect_Specification (N) then
4252 return False;
4253 end if;
4254
89f1e35c 4255 -- Otherwise current clause may duplicate previous clause, or a
4256 -- previously given pragma or aspect specification for the same
4257 -- aspect.
d74fc39a 4258
89b3b365 4259 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
ae888dbd 4260
4261 if Present (A) then
89f1e35c 4262 Error_Msg_Name_1 := Chars (N);
4263 Error_Msg_Sloc := Sloc (A);
4264
89b3b365 4265 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
89f1e35c 4266 return True;
ae888dbd 4267 end if;
4268
4269 return False;
4270 end Duplicate_Clause;
4271
9f373bb8 4272 -- Start of processing for Analyze_Attribute_Definition_Clause
4273
d6f39728 4274 begin
d64221a7 4275 -- The following code is a defense against recursion. Not clear that
51fa2a45 4276 -- this can happen legitimately, but perhaps some error situations can
4277 -- cause it, and we did see this recursion during testing.
d64221a7 4278
4279 if Analyzed (N) then
4280 return;
4281 else
4282 Set_Analyzed (N, True);
4283 end if;
4284
a29bc1d9 4285 -- Ignore some selected attributes in CodePeer mode since they are not
4286 -- relevant in this context.
4287
4288 if CodePeer_Mode then
4289 case Id is
4290
4291 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4292 -- internal representation of types by implicitly packing them.
4293
4294 when Attribute_Component_Size =>
4295 Rewrite (N, Make_Null_Statement (Sloc (N)));
4296 return;
4297
4298 when others =>
4299 null;
4300 end case;
4301 end if;
4302
d8ba53a8 4303 -- Process Ignore_Rep_Clauses option
eef1ca1e 4304
d8ba53a8 4305 if Ignore_Rep_Clauses then
9d627c41 4306 case Id is
4307
eef1ca1e 4308 -- The following should be ignored. They do not affect legality
4309 -- and may be target dependent. The basic idea of -gnatI is to
4310 -- ignore any rep clauses that may be target dependent but do not
4311 -- affect legality (except possibly to be rejected because they
4312 -- are incompatible with the compilation target).
9d627c41 4313
2f1aac99 4314 when Attribute_Alignment |
9d627c41 4315 Attribute_Bit_Order |
4316 Attribute_Component_Size |
4317 Attribute_Machine_Radix |
4318 Attribute_Object_Size |
4319 Attribute_Size |
2ff55065 4320 Attribute_Small |
9d627c41 4321 Attribute_Stream_Size |
4322 Attribute_Value_Size =>
2ff55065 4323 Kill_Rep_Clause (N);
9d627c41 4324 return;
4325
eef1ca1e 4326 -- The following should not be ignored, because in the first place
51fa2a45 4327 -- they are reasonably portable, and should not cause problems
4328 -- in compiling code from another target, and also they do affect
4329 -- legality, e.g. failing to provide a stream attribute for a type
4330 -- may make a program illegal.
9d627c41 4331
b55f7641 4332 when Attribute_External_Tag |
4333 Attribute_Input |
4334 Attribute_Output |
4335 Attribute_Read |
4336 Attribute_Simple_Storage_Pool |
4337 Attribute_Storage_Pool |
4338 Attribute_Storage_Size |
4339 Attribute_Write =>
9d627c41 4340 null;
4341
2ff55065 4342 -- We do not do anything here with address clauses, they will be
4343 -- removed by Freeze later on, but for now, it works better to
4344 -- keep then in the tree.
4345
4346 when Attribute_Address =>
4347 null;
4348
b593a52c 4349 -- Other cases are errors ("attribute& cannot be set with
4350 -- definition clause"), which will be caught below.
9d627c41 4351
4352 when others =>
4353 null;
4354 end case;
fbc67f84 4355 end if;
4356
d6f39728 4357 Analyze (Nam);
4358 Ent := Entity (Nam);
4359
4360 if Rep_Item_Too_Early (Ent, N) then
4361 return;
4362 end if;
4363
9f373bb8 4364 -- Rep clause applies to full view of incomplete type or private type if
4365 -- we have one (if not, this is a premature use of the type). However,
4366 -- certain semantic checks need to be done on the specified entity (i.e.
4367 -- the private view), so we save it in Ent.
d6f39728 4368
4369 if Is_Private_Type (Ent)
4370 and then Is_Derived_Type (Ent)
4371 and then not Is_Tagged_Type (Ent)
4372 and then No (Full_View (Ent))
4373 then
9f373bb8 4374 -- If this is a private type whose completion is a derivation from
4375 -- another private type, there is no full view, and the attribute
4376 -- belongs to the type itself, not its underlying parent.
d6f39728 4377
4378 U_Ent := Ent;
4379
4380 elsif Ekind (Ent) = E_Incomplete_Type then
d5b349fa 4381
9f373bb8 4382 -- The attribute applies to the full view, set the entity of the
4383 -- attribute definition accordingly.
d5b349fa 4384
d6f39728 4385 Ent := Underlying_Type (Ent);
4386 U_Ent := Ent;
d5b349fa 4387 Set_Entity (Nam, Ent);
4388
d6f39728 4389 else
4390 U_Ent := Underlying_Type (Ent);
4391 end if;
4392
44705307 4393 -- Avoid cascaded error
d6f39728 4394
4395 if Etype (Nam) = Any_Type then
4396 return;
4397
89f1e35c 4398 -- Must be declared in current scope or in case of an aspect
ace3389d 4399 -- specification, must be visible in current scope.
44705307 4400
89f1e35c 4401 elsif Scope (Ent) /= Current_Scope
ace3389d 4402 and then
4403 not (From_Aspect_Specification (N)
4404 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
89f1e35c 4405 then
d6f39728 4406 Error_Msg_N ("entity must be declared in this scope", Nam);
4407 return;
4408
44705307 4409 -- Must not be a source renaming (we do have some cases where the
4410 -- expander generates a renaming, and those cases are OK, in such
a3248fc4 4411 -- cases any attribute applies to the renamed object as well).
44705307 4412
4413 elsif Is_Object (Ent)
4414 and then Present (Renamed_Object (Ent))
44705307 4415 then
a3248fc4 4416 -- Case of renamed object from source, this is an error
4417
4418 if Comes_From_Source (Renamed_Object (Ent)) then
4419 Get_Name_String (Chars (N));
4420 Error_Msg_Strlen := Name_Len;
4421 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4422 Error_Msg_N
4423 ("~ clause not allowed for a renaming declaration "
4424 & "(RM 13.1(6))", Nam);
4425 return;
4426
4427 -- For the case of a compiler generated renaming, the attribute
4428 -- definition clause applies to the renamed object created by the
4429 -- expander. The easiest general way to handle this is to create a
4430 -- copy of the attribute definition clause for this object.
4431
9a48fc56 4432 elsif Is_Entity_Name (Renamed_Object (Ent)) then
a3248fc4 4433 Insert_Action (N,
4434 Make_Attribute_Definition_Clause (Loc,
4435 Name =>
4436 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4437 Chars => Chars (N),
4438 Expression => Duplicate_Subexpr (Expression (N))));
9a48fc56 4439
4440 -- If the renamed object is not an entity, it must be a dereference
4441 -- of an unconstrained function call, and we must introduce a new
4442 -- declaration to capture the expression. This is needed in the case
4443 -- of 'Alignment, where the original declaration must be rewritten.
4444
4445 else
4446 pragma Assert
4447 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4448 null;
a3248fc4 4449 end if;
44705307 4450
4451 -- If no underlying entity, use entity itself, applies to some
4452 -- previously detected error cases ???
4453
f15731c4 4454 elsif No (U_Ent) then
4455 U_Ent := Ent;
4456
44705307 4457 -- Cannot specify for a subtype (exception Object/Value_Size)
4458
d6f39728 4459 elsif Is_Type (U_Ent)
4460 and then not Is_First_Subtype (U_Ent)
4461 and then Id /= Attribute_Object_Size
4462 and then Id /= Attribute_Value_Size
4463 and then not From_At_Mod (N)
4464 then
4465 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4466 return;
d6f39728 4467 end if;
4468
ae888dbd 4469 Set_Entity (N, U_Ent);
25e23a77 4470 Check_Restriction_No_Use_Of_Attribute (N);
ae888dbd 4471
d6f39728 4472 -- Switch on particular attribute
4473
4474 case Id is
4475
4476 -------------
4477 -- Address --
4478 -------------
4479
4480 -- Address attribute definition clause
4481
4482 when Attribute_Address => Address : begin
177675a7 4483
4484 -- A little error check, catch for X'Address use X'Address;
4485
4486 if Nkind (Nam) = N_Identifier
4487 and then Nkind (Expr) = N_Attribute_Reference
4488 and then Attribute_Name (Expr) = Name_Address
4489 and then Nkind (Prefix (Expr)) = N_Identifier
4490 and then Chars (Nam) = Chars (Prefix (Expr))
4491 then
4492 Error_Msg_NE
4493 ("address for & is self-referencing", Prefix (Expr), Ent);
4494 return;
4495 end if;
4496
4497 -- Not that special case, carry on with analysis of expression
4498
d6f39728 4499 Analyze_And_Resolve (Expr, RTE (RE_Address));
4500
2f1aac99 4501 -- Even when ignoring rep clauses we need to indicate that the
4502 -- entity has an address clause and thus it is legal to declare
2ff55065 4503 -- it imported. Freeze will get rid of the address clause later.
2f1aac99 4504
4505 if Ignore_Rep_Clauses then
d3ef794c 4506 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2f1aac99 4507 Record_Rep_Item (U_Ent, N);
4508 end if;
4509
4510 return;
4511 end if;
4512
ae888dbd 4513 if Duplicate_Clause then
4514 null;
d6f39728 4515
4516 -- Case of address clause for subprogram
4517
4518 elsif Is_Subprogram (U_Ent) then
d6f39728 4519 if Has_Homonym (U_Ent) then
4520 Error_Msg_N
4521 ("address clause cannot be given " &
4522 "for overloaded subprogram",
4523 Nam);
83f8f0a6 4524 return;
d6f39728 4525 end if;
4526
83f8f0a6 4527 -- For subprograms, all address clauses are permitted, and we
4528 -- mark the subprogram as having a deferred freeze so that Gigi
4529 -- will not elaborate it too soon.
d6f39728 4530
4531 -- Above needs more comments, what is too soon about???
4532
4533 Set_Has_Delayed_Freeze (U_Ent);
4534
4535 -- Case of address clause for entry
4536
4537 elsif Ekind (U_Ent) = E_Entry then
d6f39728 4538 if Nkind (Parent (N)) = N_Task_Body then
4539 Error_Msg_N
4540 ("entry address must be specified in task spec", Nam);
83f8f0a6 4541 return;
d6f39728 4542 end if;
4543
4544 -- For entries, we require a constant address
4545
4546 Check_Constant_Address_Clause (Expr, U_Ent);
4547
83f8f0a6 4548 -- Special checks for task types
4549
f15731c4 4550 if Is_Task_Type (Scope (U_Ent))
4551 and then Comes_From_Source (Scope (U_Ent))
4552 then
4553 Error_Msg_N
1e3532e7 4554 ("??entry address declared for entry in task type", N);
f15731c4 4555 Error_Msg_N
1e3532e7 4556 ("\??only one task can be declared of this type", N);
f15731c4 4557 end if;
4558
83f8f0a6 4559 -- Entry address clauses are obsolescent
4560
e0521a36 4561 Check_Restriction (No_Obsolescent_Features, N);
4562
9dfe12ae 4563 if Warn_On_Obsolescent_Feature then
4564 Error_Msg_N
1e3532e7 4565 ("?j?attaching interrupt to task entry is an " &
4566 "obsolescent feature (RM J.7.1)", N);
9dfe12ae 4567 Error_Msg_N
1e3532e7 4568 ("\?j?use interrupt procedure instead", N);
9dfe12ae 4569 end if;
4570
83f8f0a6 4571 -- Case of an address clause for a controlled object which we
4572 -- consider to be erroneous.
9dfe12ae 4573
83f8f0a6 4574 elsif Is_Controlled (Etype (U_Ent))
4575 or else Has_Controlled_Component (Etype (U_Ent))
4576 then
9dfe12ae 4577 Error_Msg_NE
1e3532e7 4578 ("??controlled object& must not be overlaid", Nam, U_Ent);
9dfe12ae 4579 Error_Msg_N
1e3532e7 4580 ("\??Program_Error will be raised at run time", Nam);
9dfe12ae 4581 Insert_Action (Declaration_Node (U_Ent),
4582 Make_Raise_Program_Error (Loc,
4583 Reason => PE_Overlaid_Controlled_Object));
83f8f0a6 4584 return;
9dfe12ae 4585
4586 -- Case of address clause for a (non-controlled) object
d6f39728 4587
f02a9a9a 4588 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
d6f39728 4589 declare
d6da7448 4590 Expr : constant Node_Id := Expression (N);
4591 O_Ent : Entity_Id;
4592 Off : Boolean;
d6f39728 4593
4594 begin
7ee315cc 4595 -- Exported variables cannot have an address clause, because
4596 -- this cancels the effect of the pragma Export.
d6f39728 4597
4598 if Is_Exported (U_Ent) then
4599 Error_Msg_N
4600 ("cannot export object with address clause", Nam);
83f8f0a6 4601 return;
d6da7448 4602 end if;
4603
4604 Find_Overlaid_Entity (N, O_Ent, Off);
d6f39728 4605
9dfe12ae 4606 -- Overlaying controlled objects is erroneous
4607
d6da7448 4608 if Present (O_Ent)
4609 and then (Has_Controlled_Component (Etype (O_Ent))
f02a9a9a 4610 or else Is_Controlled (Etype (O_Ent)))
9dfe12ae 4611 then
4612 Error_Msg_N
1e3532e7 4613 ("??cannot overlay with controlled object", Expr);
9dfe12ae 4614 Error_Msg_N
1e3532e7 4615 ("\??Program_Error will be raised at run time", Expr);
9dfe12ae 4616 Insert_Action (Declaration_Node (U_Ent),
4617 Make_Raise_Program_Error (Loc,
4618 Reason => PE_Overlaid_Controlled_Object));
83f8f0a6 4619 return;
9dfe12ae 4620
d6da7448 4621 elsif Present (O_Ent)
9dfe12ae 4622 and then Ekind (U_Ent) = E_Constant
d6da7448 4623 and then not Is_Constant_Object (O_Ent)
9dfe12ae 4624 then
1e3532e7 4625 Error_Msg_N ("??constant overlays a variable", Expr);
9dfe12ae 4626
d6f39728 4627 -- Imported variables can have an address clause, but then
4628 -- the import is pretty meaningless except to suppress
4629 -- initializations, so we do not need such variables to
4630 -- be statically allocated (and in fact it causes trouble
4631 -- if the address clause is a local value).
4632
4633 elsif Is_Imported (U_Ent) then
4634 Set_Is_Statically_Allocated (U_Ent, False);
4635 end if;
4636
4637 -- We mark a possible modification of a variable with an
4638 -- address clause, since it is likely aliasing is occurring.
4639
177675a7 4640 Note_Possible_Modification (Nam, Sure => False);
d6f39728 4641
83f8f0a6 4642 -- Here we are checking for explicit overlap of one variable
4643 -- by another, and if we find this then mark the overlapped
4644 -- variable as also being volatile to prevent unwanted
d6da7448 4645 -- optimizations. This is a significant pessimization so
4646 -- avoid it when there is an offset, i.e. when the object
4647 -- is composite; they cannot be optimized easily anyway.
d6f39728 4648
d6da7448 4649 if Present (O_Ent)
4650 and then Is_Object (O_Ent)
4651 and then not Off
ba5efa21 4652
4653 -- The following test is an expedient solution to what
4654 -- is really a problem in CodePeer. Suppressing the
4655 -- Set_Treat_As_Volatile call here prevents later
4656 -- generation (in some cases) of trees that CodePeer
4657 -- should, but currently does not, handle correctly.
4658 -- This test should probably be removed when CodePeer
4659 -- is improved, just because we want the tree CodePeer
4660 -- analyzes to match the tree for which we generate code
4661 -- as closely as is practical. ???
4662
4663 and then not CodePeer_Mode
d6da7448 4664 then
ba5efa21 4665 -- ??? O_Ent might not be in current unit
4666
d6da7448 4667 Set_Treat_As_Volatile (O_Ent);
d6f39728 4668 end if;
4669
9dfe12ae 4670 -- Legality checks on the address clause for initialized
4671 -- objects is deferred until the freeze point, because
2beb22b1 4672 -- a subsequent pragma might indicate that the object
42e09e36 4673 -- is imported and thus not initialized. Also, the address
4674 -- clause might involve entities that have yet to be
4675 -- elaborated.
9dfe12ae 4676
4677 Set_Has_Delayed_Freeze (U_Ent);
4678
51ad5ad2 4679 -- If an initialization call has been generated for this
4680 -- object, it needs to be deferred to after the freeze node
4681 -- we have just now added, otherwise GIGI will see a
4682 -- reference to the variable (as actual to the IP call)
4683 -- before its definition.
4684
4685 declare
df9fba45 4686 Init_Call : constant Node_Id :=
4687 Remove_Init_Call (U_Ent, N);
4bba0a8d 4688
51ad5ad2 4689 begin
4690 if Present (Init_Call) then
28a4283c 4691 Append_Freeze_Action (U_Ent, Init_Call);
df9fba45 4692
28a4283c 4693 -- Reset Initialization_Statements pointer so that
4694 -- if there is a pragma Import further down, it can
4695 -- clear any default initialization.
df9fba45 4696
28a4283c 4697 Set_Initialization_Statements (U_Ent, Init_Call);
51ad5ad2 4698 end if;
4699 end;
4700
d6f39728 4701 if Is_Exported (U_Ent) then
4702 Error_Msg_N
4703 ("& cannot be exported if an address clause is given",
4704 Nam);
4705 Error_Msg_N
4bba0a8d 4706 ("\define and export a variable "
4707 & "that holds its address instead", Nam);
d6f39728 4708 end if;
4709
44e4341e 4710 -- Entity has delayed freeze, so we will generate an
4711 -- alignment check at the freeze point unless suppressed.
d6f39728 4712
44e4341e 4713 if not Range_Checks_Suppressed (U_Ent)
4714 and then not Alignment_Checks_Suppressed (U_Ent)
4715 then
4716 Set_Check_Address_Alignment (N);
4717 end if;
d6f39728 4718
4719 -- Kill the size check code, since we are not allocating
4720 -- the variable, it is somewhere else.
4721
4722 Kill_Size_Check_Code (U_Ent);
83f8f0a6 4723
d6da7448 4724 -- If the address clause is of the form:
83f8f0a6 4725
d6da7448 4726 -- for Y'Address use X'Address
83f8f0a6 4727
d6da7448 4728 -- or
83f8f0a6 4729
d6da7448 4730 -- Const : constant Address := X'Address;
4731 -- ...
4732 -- for Y'Address use Const;
83f8f0a6 4733
d6da7448 4734 -- then we make an entry in the table for checking the size
4735 -- and alignment of the overlaying variable. We defer this
4736 -- check till after code generation to take full advantage
f4623c89 4737 -- of the annotation done by the back end.
d64221a7 4738
9474aa9c 4739 -- If the entity has a generic type, the check will be
43dd6937 4740 -- performed in the instance if the actual type justifies
4741 -- it, and we do not insert the clause in the table to
4742 -- prevent spurious warnings.
83f8f0a6 4743
f4623c89 4744 -- Note: we used to test Comes_From_Source and only give
4745 -- this warning for source entities, but we have removed
4746 -- this test. It really seems bogus to generate overlays
4747 -- that would trigger this warning in generated code.
4748 -- Furthermore, by removing the test, we handle the
4749 -- aspect case properly.
4750
d6da7448 4751 if Address_Clause_Overlay_Warnings
d6da7448 4752 and then Present (O_Ent)
4753 and then Is_Object (O_Ent)
4754 then
9474aa9c 4755 if not Is_Generic_Type (Etype (U_Ent)) then
4756 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4757 end if;
177675a7 4758
d6da7448 4759 -- If variable overlays a constant view, and we are
4760 -- warning on overlays, then mark the variable as
4761 -- overlaying a constant (we will give warnings later
4762 -- if this variable is assigned).
177675a7 4763
d6da7448 4764 if Is_Constant_Object (O_Ent)
4765 and then Ekind (U_Ent) = E_Variable
4766 then
4767 Set_Overlays_Constant (U_Ent);
83f8f0a6 4768 end if;
d6da7448 4769 end if;
4770 end;
83f8f0a6 4771
d6f39728 4772 -- Not a valid entity for an address clause
4773
4774 else
4775 Error_Msg_N ("address cannot be given for &", Nam);
4776 end if;
4777 end Address;
4778
4779 ---------------
4780 -- Alignment --
4781 ---------------
4782
4783 -- Alignment attribute definition clause
4784
b47769f0 4785 when Attribute_Alignment => Alignment : declare
208fd589 4786 Align : constant Uint := Get_Alignment_Value (Expr);
4787 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
41331dcf 4788
d6f39728 4789 begin
4790 FOnly := True;
4791
4792 if not Is_Type (U_Ent)
4793 and then Ekind (U_Ent) /= E_Variable
4794 and then Ekind (U_Ent) /= E_Constant
4795 then
4796 Error_Msg_N ("alignment cannot be given for &", Nam);
4797
ae888dbd 4798 elsif Duplicate_Clause then
4799 null;
d6f39728 4800
4801 elsif Align /= No_Uint then
4802 Set_Has_Alignment_Clause (U_Ent);
208fd589 4803
44705307 4804 -- Tagged type case, check for attempt to set alignment to a
4805 -- value greater than Max_Align, and reset if so.
4806
41331dcf 4807 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
208fd589 4808 Error_Msg_N
1e3532e7 4809 ("alignment for & set to Maximum_Aligment??", Nam);
44705307 4810 Set_Alignment (U_Ent, Max_Align);
4811
4812 -- All other cases
4813
208fd589 4814 else
4815 Set_Alignment (U_Ent, Align);
4816 end if;
b47769f0 4817
4818 -- For an array type, U_Ent is the first subtype. In that case,
4819 -- also set the alignment of the anonymous base type so that
4820 -- other subtypes (such as the itypes for aggregates of the
4821 -- type) also receive the expected alignment.
4822
4823 if Is_Array_Type (U_Ent) then
4824 Set_Alignment (Base_Type (U_Ent), Align);
4825 end if;
d6f39728 4826 end if;
b47769f0 4827 end Alignment;
d6f39728 4828
4829 ---------------
4830 -- Bit_Order --
4831 ---------------
4832
4833 -- Bit_Order attribute definition clause
4834
4835 when Attribute_Bit_Order => Bit_Order : declare
4836 begin
4837 if not Is_Record_Type (U_Ent) then
4838 Error_Msg_N
4839 ("Bit_Order can only be defined for record type", Nam);
4840
ae888dbd 4841 elsif Duplicate_Clause then
4842 null;
4843
d6f39728 4844 else
4845 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4846
4847 if Etype (Expr) = Any_Type then
4848 return;
4849
cda40848 4850 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 4851 Flag_Non_Static_Expr
4852 ("Bit_Order requires static expression!", Expr);
d6f39728 4853
4854 else
4855 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
fae4ea1f 4856 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
d6f39728 4857 end if;
4858 end if;
4859 end if;
4860 end Bit_Order;
4861
4862 --------------------
4863 -- Component_Size --
4864 --------------------
4865
4866 -- Component_Size attribute definition clause
4867
4868 when Attribute_Component_Size => Component_Size_Case : declare
4869 Csize : constant Uint := Static_Integer (Expr);
a0fc8c5b 4870 Ctyp : Entity_Id;
d6f39728 4871 Btype : Entity_Id;
4872 Biased : Boolean;
4873 New_Ctyp : Entity_Id;
4874 Decl : Node_Id;
4875
4876 begin
4877 if not Is_Array_Type (U_Ent) then
4878 Error_Msg_N ("component size requires array type", Nam);
4879 return;
4880 end if;
4881
4882 Btype := Base_Type (U_Ent);
a0fc8c5b 4883 Ctyp := Component_Type (Btype);
d6f39728 4884
ae888dbd 4885 if Duplicate_Clause then
4886 null;
d6f39728 4887
f3e4db96 4888 elsif Rep_Item_Too_Early (Btype, N) then
4889 null;
4890
d6f39728 4891 elsif Csize /= No_Uint then
a0fc8c5b 4892 Check_Size (Expr, Ctyp, Csize, Biased);
d6f39728 4893
d74fc39a 4894 -- For the biased case, build a declaration for a subtype that
4895 -- will be used to represent the biased subtype that reflects
4896 -- the biased representation of components. We need the subtype
4897 -- to get proper conversions on referencing elements of the
4898 -- array. Note: component size clauses are ignored in VM mode.
3062c401 4899
4900 if VM_Target = No_VM then
4901 if Biased then
4902 New_Ctyp :=
4903 Make_Defining_Identifier (Loc,
4904 Chars =>
4905 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4906
4907 Decl :=
4908 Make_Subtype_Declaration (Loc,
4909 Defining_Identifier => New_Ctyp,
4910 Subtype_Indication =>
4911 New_Occurrence_Of (Component_Type (Btype), Loc));
4912
4913 Set_Parent (Decl, N);
4914 Analyze (Decl, Suppress => All_Checks);
4915
4916 Set_Has_Delayed_Freeze (New_Ctyp, False);
4917 Set_Esize (New_Ctyp, Csize);
4918 Set_RM_Size (New_Ctyp, Csize);
4919 Init_Alignment (New_Ctyp);
3062c401 4920 Set_Is_Itype (New_Ctyp, True);
4921 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4922
4923 Set_Component_Type (Btype, New_Ctyp);
b77e4501 4924 Set_Biased (New_Ctyp, N, "component size clause");
3062c401 4925 end if;
4926
4927 Set_Component_Size (Btype, Csize);
4928
4929 -- For VM case, we ignore component size clauses
4930
4931 else
4932 -- Give a warning unless we are in GNAT mode, in which case
4933 -- the warning is suppressed since it is not useful.
4934
4935 if not GNAT_Mode then
4936 Error_Msg_N
1e3532e7 4937 ("component size ignored in this configuration??", N);
3062c401 4938 end if;
d6f39728 4939 end if;
4940
a0fc8c5b 4941 -- Deal with warning on overridden size
4942
4943 if Warn_On_Overridden_Size
4944 and then Has_Size_Clause (Ctyp)
4945 and then RM_Size (Ctyp) /= Csize
4946 then
4947 Error_Msg_NE
1e3532e7 4948 ("component size overrides size clause for&?S?", N, Ctyp);
a0fc8c5b 4949 end if;
4950
d6f39728 4951 Set_Has_Component_Size_Clause (Btype, True);
f3e4db96 4952 Set_Has_Non_Standard_Rep (Btype, True);
d6f39728 4953 end if;
4954 end Component_Size_Case;
4955
81b424ac 4956 -----------------------
4957 -- Constant_Indexing --
4958 -----------------------
4959
4960 when Attribute_Constant_Indexing =>
4961 Check_Indexing_Functions;
4962
89f1e35c 4963 ---------
4964 -- CPU --
4965 ---------
4966
4967 when Attribute_CPU => CPU :
4968 begin
4969 -- CPU attribute definition clause not allowed except from aspect
4970 -- specification.
4971
4972 if From_Aspect_Specification (N) then
4973 if not Is_Task_Type (U_Ent) then
4974 Error_Msg_N ("CPU can only be defined for task", Nam);
4975
4976 elsif Duplicate_Clause then
4977 null;
4978
4979 else
4980 -- The expression must be analyzed in the special manner
4981 -- described in "Handling of Default and Per-Object
4982 -- Expressions" in sem.ads.
4983
4984 -- The visibility to the discriminants must be restored
4985
4986 Push_Scope_And_Install_Discriminants (U_Ent);
4987 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4988 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4989
cda40848 4990 if not Is_OK_Static_Expression (Expr) then
89f1e35c 4991 Check_Restriction (Static_Priorities, Expr);
4992 end if;
4993 end if;
4994
4995 else
4996 Error_Msg_N
4997 ("attribute& cannot be set with definition clause", N);
4998 end if;
4999 end CPU;
5000
89cc7147 5001 ----------------------
5002 -- Default_Iterator --
5003 ----------------------
5004
5005 when Attribute_Default_Iterator => Default_Iterator : declare
5006 Func : Entity_Id;
fbf4d6ef 5007 Typ : Entity_Id;
89cc7147 5008
5009 begin
05f6f999 5010 -- If target type is untagged, further checks are irrelevant
5011
89cc7147 5012 if not Is_Tagged_Type (U_Ent) then
5013 Error_Msg_N
05f6f999 5014 ("aspect Default_Iterator applies to tagged type", Nam);
5015 return;
89cc7147 5016 end if;
5017
5018 Check_Iterator_Functions;
5019
5020 Analyze (Expr);
5021
5022 if not Is_Entity_Name (Expr)
5023 or else Ekind (Entity (Expr)) /= E_Function
5024 then
5025 Error_Msg_N ("aspect Iterator must be a function", Expr);
05f6f999 5026 return;
89cc7147 5027 else
5028 Func := Entity (Expr);
5029 end if;
5030
fbf4d6ef 5031 -- The type of the first parameter must be T, T'class, or a
05f6f999 5032 -- corresponding access type (5.5.1 (8/3). If function is
5033 -- parameterless label type accordingly.
fbf4d6ef 5034
5035 if No (First_Formal (Func)) then
05f6f999 5036 Typ := Any_Type;
fbf4d6ef 5037 else
5038 Typ := Etype (First_Formal (Func));
5039 end if;
5040
5041 if Typ = U_Ent
5042 or else Typ = Class_Wide_Type (U_Ent)
5043 or else (Is_Access_Type (Typ)
5044 and then Designated_Type (Typ) = U_Ent)
5045 or else (Is_Access_Type (Typ)
5046 and then Designated_Type (Typ) =
5047 Class_Wide_Type (U_Ent))
89cc7147 5048 then
fbf4d6ef 5049 null;
5050
5051 else
89cc7147 5052 Error_Msg_NE
5053 ("Default Iterator must be a primitive of&", Func, U_Ent);
5054 end if;
5055 end Default_Iterator;
5056
89f1e35c 5057 ------------------------
5058 -- Dispatching_Domain --
5059 ------------------------
5060
5061 when Attribute_Dispatching_Domain => Dispatching_Domain :
5062 begin
5063 -- Dispatching_Domain attribute definition clause not allowed
5064 -- except from aspect specification.
5065
5066 if From_Aspect_Specification (N) then
5067 if not Is_Task_Type (U_Ent) then
fbf4d6ef 5068 Error_Msg_N
5069 ("Dispatching_Domain can only be defined for task", Nam);
89f1e35c 5070
5071 elsif Duplicate_Clause then
5072 null;
5073
5074 else
5075 -- The expression must be analyzed in the special manner
5076 -- described in "Handling of Default and Per-Object
5077 -- Expressions" in sem.ads.
5078
5079 -- The visibility to the discriminants must be restored
5080
5081 Push_Scope_And_Install_Discriminants (U_Ent);
5082
5083 Preanalyze_Spec_Expression
5084 (Expr, RTE (RE_Dispatching_Domain));
5085
5086 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5087 end if;
5088
5089 else
5090 Error_Msg_N
5091 ("attribute& cannot be set with definition clause", N);
5092 end if;
5093 end Dispatching_Domain;
5094
d6f39728 5095 ------------------
5096 -- External_Tag --
5097 ------------------
5098
5099 when Attribute_External_Tag => External_Tag :
5100 begin
5101 if not Is_Tagged_Type (U_Ent) then
5102 Error_Msg_N ("should be a tagged type", Nam);
5103 end if;
5104
ae888dbd 5105 if Duplicate_Clause then
5106 null;
d6f39728 5107
9af0ddc7 5108 else
ae888dbd 5109 Analyze_And_Resolve (Expr, Standard_String);
fbc67f84 5110
cda40848 5111 if not Is_OK_Static_Expression (Expr) then
ae888dbd 5112 Flag_Non_Static_Expr
5113 ("static string required for tag name!", Nam);
5114 end if;
5115
15a67a0a 5116 if VM_Target /= No_VM then
ae888dbd 5117 Error_Msg_Name_1 := Attr;
5118 Error_Msg_N
5119 ("% attribute unsupported in this configuration", Nam);
5120 end if;
5121
5122 if not Is_Library_Level_Entity (U_Ent) then
5123 Error_Msg_NE
1e3532e7 5124 ("??non-unique external tag supplied for &", N, U_Ent);
ae888dbd 5125 Error_Msg_N
1e3532e7 5126 ("\??same external tag applies to all "
5127 & "subprogram calls", N);
ae888dbd 5128 Error_Msg_N
1e3532e7 5129 ("\??corresponding internal tag cannot be obtained", N);
ae888dbd 5130 end if;
fbc67f84 5131 end if;
d6f39728 5132 end External_Tag;
5133
b57530b8 5134 --------------------------
5135 -- Implicit_Dereference --
5136 --------------------------
7947a439 5137
b57530b8 5138 when Attribute_Implicit_Dereference =>
7947a439 5139
2beb22b1 5140 -- Legality checks already performed at the point of the type
5141 -- declaration, aspect is not delayed.
7947a439 5142
89cc7147 5143 null;
b57530b8 5144
d6f39728 5145 -----------
5146 -- Input --
5147 -----------
5148
9f373bb8 5149 when Attribute_Input =>
5150 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5151 Set_Has_Specified_Stream_Input (Ent);
d6f39728 5152
89f1e35c 5153 ------------------------
5154 -- Interrupt_Priority --
5155 ------------------------
5156
5157 when Attribute_Interrupt_Priority => Interrupt_Priority :
5158 begin
5159 -- Interrupt_Priority attribute definition clause not allowed
5160 -- except from aspect specification.
5161
5162 if From_Aspect_Specification (N) then
f02a9a9a 5163 if not Is_Concurrent_Type (U_Ent) then
89f1e35c 5164 Error_Msg_N
f02a9a9a 5165 ("Interrupt_Priority can only be defined for task "
5166 & "and protected object", Nam);
89f1e35c 5167
5168 elsif Duplicate_Clause then
5169 null;
5170
5171 else
5172 -- The expression must be analyzed in the special manner
5173 -- described in "Handling of Default and Per-Object
5174 -- Expressions" in sem.ads.
5175
5176 -- The visibility to the discriminants must be restored
5177
5178 Push_Scope_And_Install_Discriminants (U_Ent);
5179
5180 Preanalyze_Spec_Expression
5181 (Expr, RTE (RE_Interrupt_Priority));
5182
5183 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5184 end if;
5185
5186 else
5187 Error_Msg_N
5188 ("attribute& cannot be set with definition clause", N);
5189 end if;
5190 end Interrupt_Priority;
5191
b3f8228a 5192 --------------
5193 -- Iterable --
5194 --------------
5195
5196 when Attribute_Iterable =>
5197 Analyze (Expr);
bde03454 5198
b3f8228a 5199 if Nkind (Expr) /= N_Aggregate then
5200 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5201 end if;
5202
5203 declare
5204 Assoc : Node_Id;
5205
5206 begin
5207 Assoc := First (Component_Associations (Expr));
5208 while Present (Assoc) loop
5209 if not Is_Entity_Name (Expression (Assoc)) then
5210 Error_Msg_N ("value must be a function", Assoc);
5211 end if;
bde03454 5212
b3f8228a 5213 Next (Assoc);
5214 end loop;
5215 end;
5216
89cc7147 5217 ----------------------
5218 -- Iterator_Element --
5219 ----------------------
5220
5221 when Attribute_Iterator_Element =>
5222 Analyze (Expr);
5223
5224 if not Is_Entity_Name (Expr)
5225 or else not Is_Type (Entity (Expr))
5226 then
5227 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5228 end if;
5229
d6f39728 5230 -------------------
5231 -- Machine_Radix --
5232 -------------------
5233
5234 -- Machine radix attribute definition clause
5235
5236 when Attribute_Machine_Radix => Machine_Radix : declare
5237 Radix : constant Uint := Static_Integer (Expr);
5238
5239 begin
5240 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5241 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5242
ae888dbd 5243 elsif Duplicate_Clause then
5244 null;
d6f39728 5245
5246 elsif Radix /= No_Uint then
5247 Set_Has_Machine_Radix_Clause (U_Ent);
5248 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5249
5250 if Radix = 2 then
5251 null;
5252 elsif Radix = 10 then
5253 Set_Machine_Radix_10 (U_Ent);
5254 else
5255 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5256 end if;
5257 end if;
5258 end Machine_Radix;
5259
5260 -----------------
5261 -- Object_Size --
5262 -----------------
5263
5264 -- Object_Size attribute definition clause
5265
5266 when Attribute_Object_Size => Object_Size : declare
bfa5a9d9 5267 Size : constant Uint := Static_Integer (Expr);
5268
d6f39728 5269 Biased : Boolean;
bfa5a9d9 5270 pragma Warnings (Off, Biased);
d6f39728 5271
5272 begin
5273 if not Is_Type (U_Ent) then
5274 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5275
ae888dbd 5276 elsif Duplicate_Clause then
5277 null;
d6f39728 5278
5279 else
5280 Check_Size (Expr, U_Ent, Size, Biased);
5281
829cd457 5282 if Is_Scalar_Type (U_Ent) then
5283 if Size /= 8 and then Size /= 16 and then Size /= 32
5284 and then UI_Mod (Size, 64) /= 0
5285 then
5286 Error_Msg_N
5287 ("Object_Size must be 8, 16, 32, or multiple of 64",
5288 Expr);
5289 end if;
5290
5291 elsif Size mod 8 /= 0 then
5292 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
d6f39728 5293 end if;
5294
5295 Set_Esize (U_Ent, Size);
5296 Set_Has_Object_Size_Clause (U_Ent);
1d366b32 5297 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5298 end if;
5299 end Object_Size;
5300
5301 ------------
5302 -- Output --
5303 ------------
5304
9f373bb8 5305 when Attribute_Output =>
5306 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5307 Set_Has_Specified_Stream_Output (Ent);
d6f39728 5308
89f1e35c 5309 --------------
5310 -- Priority --
5311 --------------
5312
5313 when Attribute_Priority => Priority :
5314 begin
5315 -- Priority attribute definition clause not allowed except from
5316 -- aspect specification.
5317
5318 if From_Aspect_Specification (N) then
f02a9a9a 5319 if not (Is_Concurrent_Type (U_Ent)
3a72f9c3 5320 or else Ekind (U_Ent) = E_Procedure)
89f1e35c 5321 then
5322 Error_Msg_N
f02a9a9a 5323 ("Priority can only be defined for task and protected "
5324 & "object", Nam);
89f1e35c 5325
5326 elsif Duplicate_Clause then
5327 null;
5328
5329 else
5330 -- The expression must be analyzed in the special manner
5331 -- described in "Handling of Default and Per-Object
5332 -- Expressions" in sem.ads.
5333
5334 -- The visibility to the discriminants must be restored
5335
5336 Push_Scope_And_Install_Discriminants (U_Ent);
5337 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5338 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5339
cda40848 5340 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5341 Check_Restriction (Static_Priorities, Expr);
5342 end if;
5343 end if;
5344
5345 else
5346 Error_Msg_N
5347 ("attribute& cannot be set with definition clause", N);
5348 end if;
5349 end Priority;
5350
d6f39728 5351 ----------
5352 -- Read --
5353 ----------
5354
9f373bb8 5355 when Attribute_Read =>
5356 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5357 Set_Has_Specified_Stream_Read (Ent);
d6f39728 5358
b7b74740 5359 --------------------------
5360 -- Scalar_Storage_Order --
5361 --------------------------
5362
5363 -- Scalar_Storage_Order attribute definition clause
5364
5365 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5366 begin
b43a5770 5367 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
b7b74740 5368 Error_Msg_N
b43a5770 5369 ("Scalar_Storage_Order can only be defined for "
5370 & "record or array type", Nam);
b7b74740 5371
5372 elsif Duplicate_Clause then
5373 null;
5374
5375 else
5376 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5377
5378 if Etype (Expr) = Any_Type then
5379 return;
5380
cda40848 5381 elsif not Is_OK_Static_Expression (Expr) then
b7b74740 5382 Flag_Non_Static_Expr
5383 ("Scalar_Storage_Order requires static expression!", Expr);
5384
c0912570 5385 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5386
5387 -- Here for the case of a non-default (i.e. non-confirming)
5388 -- Scalar_Storage_Order attribute definition.
5389
5390 if Support_Nondefault_SSO_On_Target then
d0a9ea3b 5391 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
c0912570 5392 else
5393 Error_Msg_N
5394 ("non-default Scalar_Storage_Order "
5395 & "not supported on target", Expr);
b7b74740 5396 end if;
5397 end if;
b64082f2 5398
5399 -- Clear SSO default indications since explicit setting of the
5400 -- order overrides the defaults.
5401
5402 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5403 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
b7b74740 5404 end if;
5405 end Scalar_Storage_Order;
5406
d6f39728 5407 ----------
5408 -- Size --
5409 ----------
5410
5411 -- Size attribute definition clause
5412
5413 when Attribute_Size => Size : declare
5414 Size : constant Uint := Static_Integer (Expr);
5415 Etyp : Entity_Id;
5416 Biased : Boolean;
5417
5418 begin
5419 FOnly := True;
5420
ae888dbd 5421 if Duplicate_Clause then
5422 null;
d6f39728 5423
5424 elsif not Is_Type (U_Ent)
5425 and then Ekind (U_Ent) /= E_Variable
5426 and then Ekind (U_Ent) /= E_Constant
5427 then
5428 Error_Msg_N ("size cannot be given for &", Nam);
5429
5430 elsif Is_Array_Type (U_Ent)
5431 and then not Is_Constrained (U_Ent)
5432 then
5433 Error_Msg_N
5434 ("size cannot be given for unconstrained array", Nam);
5435
c2b89d6e 5436 elsif Size /= No_Uint then
c2b89d6e 5437 if VM_Target /= No_VM and then not GNAT_Mode then
47495553 5438
c2b89d6e 5439 -- Size clause is not handled properly on VM targets.
5440 -- Display a warning unless we are in GNAT mode, in which
5441 -- case this is useless.
47495553 5442
682fa897 5443 Error_Msg_N
1e3532e7 5444 ("size clauses are ignored in this configuration??", N);
682fa897 5445 end if;
5446
d6f39728 5447 if Is_Type (U_Ent) then
5448 Etyp := U_Ent;
5449 else
5450 Etyp := Etype (U_Ent);
5451 end if;
5452
59ac57b5 5453 -- Check size, note that Gigi is in charge of checking that the
5454 -- size of an array or record type is OK. Also we do not check
5455 -- the size in the ordinary fixed-point case, since it is too
5456 -- early to do so (there may be subsequent small clause that
5457 -- affects the size). We can check the size if a small clause
5458 -- has already been given.
d6f39728 5459
5460 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5461 or else Has_Small_Clause (U_Ent)
5462 then
5463 Check_Size (Expr, Etyp, Size, Biased);
b77e4501 5464 Set_Biased (U_Ent, N, "size clause", Biased);
d6f39728 5465 end if;
5466
5467 -- For types set RM_Size and Esize if possible
5468
5469 if Is_Type (U_Ent) then
5470 Set_RM_Size (U_Ent, Size);
5471
ada34def 5472 -- For elementary types, increase Object_Size to power of 2,
5473 -- but not less than a storage unit in any case (normally
59ac57b5 5474 -- this means it will be byte addressable).
d6f39728 5475
ada34def 5476 -- For all other types, nothing else to do, we leave Esize
5477 -- (object size) unset, the back end will set it from the
5478 -- size and alignment in an appropriate manner.
5479
1d366b32 5480 -- In both cases, we check whether the alignment must be
5481 -- reset in the wake of the size change.
5482
ada34def 5483 if Is_Elementary_Type (U_Ent) then
f15731c4 5484 if Size <= System_Storage_Unit then
5485 Init_Esize (U_Ent, System_Storage_Unit);
d6f39728 5486 elsif Size <= 16 then
5487 Init_Esize (U_Ent, 16);
5488 elsif Size <= 32 then
5489 Init_Esize (U_Ent, 32);
5490 else
5491 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5492 end if;
5493
1d366b32 5494 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5495 else
5496 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5497 end if;
5498
d6f39728 5499 -- For objects, set Esize only
5500
5501 else
9dfe12ae 5502 if Is_Elementary_Type (Etyp) then
5503 if Size /= System_Storage_Unit
5504 and then
5505 Size /= System_Storage_Unit * 2
5506 and then
5507 Size /= System_Storage_Unit * 4
5508 and then
5509 Size /= System_Storage_Unit * 8
5510 then
5c99c290 5511 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
87d5c1d0 5512 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
9dfe12ae 5513 Error_Msg_N
5c99c290 5514 ("size for primitive object must be a power of 2"
87d5c1d0 5515 & " in the range ^-^", N);
9dfe12ae 5516 end if;
5517 end if;
5518
d6f39728 5519 Set_Esize (U_Ent, Size);
5520 end if;
5521
5522 Set_Has_Size_Clause (U_Ent);
5523 end if;
5524 end Size;
5525
5526 -----------
5527 -- Small --
5528 -----------
5529
5530 -- Small attribute definition clause
5531
5532 when Attribute_Small => Small : declare
5533 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5534 Small : Ureal;
5535
5536 begin
5537 Analyze_And_Resolve (Expr, Any_Real);
5538
5539 if Etype (Expr) = Any_Type then
5540 return;
5541
cda40848 5542 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5543 Flag_Non_Static_Expr
5544 ("small requires static expression!", Expr);
d6f39728 5545 return;
5546
5547 else
5548 Small := Expr_Value_R (Expr);
5549
5550 if Small <= Ureal_0 then
5551 Error_Msg_N ("small value must be greater than zero", Expr);
5552 return;
5553 end if;
5554
5555 end if;
5556
5557 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5558 Error_Msg_N
5559 ("small requires an ordinary fixed point type", Nam);
5560
5561 elsif Has_Small_Clause (U_Ent) then
5562 Error_Msg_N ("small already given for &", Nam);
5563
5564 elsif Small > Delta_Value (U_Ent) then
5565 Error_Msg_N
ce3e25d6 5566 ("small value must not be greater than delta value", Nam);
d6f39728 5567
5568 else
5569 Set_Small_Value (U_Ent, Small);
5570 Set_Small_Value (Implicit_Base, Small);
5571 Set_Has_Small_Clause (U_Ent);
5572 Set_Has_Small_Clause (Implicit_Base);
5573 Set_Has_Non_Standard_Rep (Implicit_Base);
5574 end if;
5575 end Small;
5576
d6f39728 5577 ------------------
5578 -- Storage_Pool --
5579 ------------------
5580
5581 -- Storage_Pool attribute definition clause
5582
b55f7641 5583 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
d6f39728 5584 Pool : Entity_Id;
6b567c71 5585 T : Entity_Id;
d6f39728 5586
5587 begin
44e4341e 5588 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5589 Error_Msg_N
5590 ("storage pool cannot be given for access-to-subprogram type",
5591 Nam);
5592 return;
5593
d3ef794c 5594 elsif not
5595 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
d6f39728 5596 then
44e4341e 5597 Error_Msg_N
5598 ("storage pool can only be given for access types", Nam);
d6f39728 5599 return;
5600
5601 elsif Is_Derived_Type (U_Ent) then
5602 Error_Msg_N
5603 ("storage pool cannot be given for a derived access type",
5604 Nam);
5605
ae888dbd 5606 elsif Duplicate_Clause then
d6f39728 5607 return;
5608
5609 elsif Present (Associated_Storage_Pool (U_Ent)) then
5610 Error_Msg_N ("storage pool already given for &", Nam);
5611 return;
5612 end if;
5613
6653b695 5614 -- Check for Storage_Size previously given
5615
5616 declare
5617 SS : constant Node_Id :=
5618 Get_Attribute_Definition_Clause
5619 (U_Ent, Attribute_Storage_Size);
5620 begin
5621 if Present (SS) then
5622 Check_Pool_Size_Clash (U_Ent, N, SS);
5623 end if;
5624 end;
5625
5626 -- Storage_Pool case
5627
b55f7641 5628 if Id = Attribute_Storage_Pool then
5629 Analyze_And_Resolve
5630 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5631
5632 -- In the Simple_Storage_Pool case, we allow a variable of any
b15003c3 5633 -- simple storage pool type, so we Resolve without imposing an
b55f7641 5634 -- expected type.
5635
5636 else
5637 Analyze_And_Resolve (Expr);
5638
5639 if not Present (Get_Rep_Pragma
b15003c3 5640 (Etype (Expr), Name_Simple_Storage_Pool_Type))
b55f7641 5641 then
5642 Error_Msg_N
5643 ("expression must be of a simple storage pool type", Expr);
5644 end if;
5645 end if;
d6f39728 5646
8c5c7277 5647 if not Denotes_Variable (Expr) then
5648 Error_Msg_N ("storage pool must be a variable", Expr);
5649 return;
5650 end if;
5651
6b567c71 5652 if Nkind (Expr) = N_Type_Conversion then
5653 T := Etype (Expression (Expr));
5654 else
5655 T := Etype (Expr);
5656 end if;
5657
5658 -- The Stack_Bounded_Pool is used internally for implementing
d64221a7 5659 -- access types with a Storage_Size. Since it only work properly
5660 -- when used on one specific type, we need to check that it is not
5661 -- hijacked improperly:
5662
6b567c71 5663 -- type T is access Integer;
5664 -- for T'Storage_Size use n;
5665 -- type Q is access Float;
5666 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5667
15ebb600 5668 if RTE_Available (RE_Stack_Bounded_Pool)
5669 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5670 then
5671 Error_Msg_N ("non-shareable internal Pool", Expr);
6b567c71 5672 return;
5673 end if;
5674
d6f39728 5675 -- If the argument is a name that is not an entity name, then
5676 -- we construct a renaming operation to define an entity of
5677 -- type storage pool.
5678
5679 if not Is_Entity_Name (Expr)
5680 and then Is_Object_Reference (Expr)
5681 then
11deeeb6 5682 Pool := Make_Temporary (Loc, 'P', Expr);
d6f39728 5683
5684 declare
5685 Rnode : constant Node_Id :=
5686 Make_Object_Renaming_Declaration (Loc,
5687 Defining_Identifier => Pool,
5688 Subtype_Mark =>
5689 New_Occurrence_Of (Etype (Expr), Loc),
11deeeb6 5690 Name => Expr);
d6f39728 5691
5692 begin
f65f7fdf 5693 -- If the attribute definition clause comes from an aspect
5694 -- clause, then insert the renaming before the associated
5695 -- entity's declaration, since the attribute clause has
5696 -- not yet been appended to the declaration list.
5697
5698 if From_Aspect_Specification (N) then
5699 Insert_Before (Parent (Entity (N)), Rnode);
5700 else
5701 Insert_Before (N, Rnode);
5702 end if;
5703
d6f39728 5704 Analyze (Rnode);
5705 Set_Associated_Storage_Pool (U_Ent, Pool);
5706 end;
5707
5708 elsif Is_Entity_Name (Expr) then
5709 Pool := Entity (Expr);
5710
5711 -- If pool is a renamed object, get original one. This can
5712 -- happen with an explicit renaming, and within instances.
5713
5714 while Present (Renamed_Object (Pool))
5715 and then Is_Entity_Name (Renamed_Object (Pool))
5716 loop
5717 Pool := Entity (Renamed_Object (Pool));
5718 end loop;
5719
5720 if Present (Renamed_Object (Pool))
5721 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5722 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5723 then
5724 Pool := Entity (Expression (Renamed_Object (Pool)));
5725 end if;
5726
6b567c71 5727 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 5728
5729 elsif Nkind (Expr) = N_Type_Conversion
5730 and then Is_Entity_Name (Expression (Expr))
5731 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5732 then
5733 Pool := Entity (Expression (Expr));
6b567c71 5734 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 5735
5736 else
5737 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5738 return;
5739 end if;
b55f7641 5740 end;
d6f39728 5741
44e4341e 5742 ------------------
5743 -- Storage_Size --
5744 ------------------
5745
5746 -- Storage_Size attribute definition clause
5747
5748 when Attribute_Storage_Size => Storage_Size : declare
5749 Btype : constant Entity_Id := Base_Type (U_Ent);
44e4341e 5750
5751 begin
5752 if Is_Task_Type (U_Ent) then
44e4341e 5753
39a0c1d3 5754 -- Check obsolescent (but never obsolescent if from aspect)
ceec4f7c 5755
5756 if not From_Aspect_Specification (N) then
5757 Check_Restriction (No_Obsolescent_Features, N);
5758
5759 if Warn_On_Obsolescent_Feature then
5760 Error_Msg_N
5761 ("?j?storage size clause for task is an " &
5762 "obsolescent feature (RM J.9)", N);
5763 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5764 end if;
44e4341e 5765 end if;
5766
5767 FOnly := True;
5768 end if;
5769
5770 if not Is_Access_Type (U_Ent)
5771 and then Ekind (U_Ent) /= E_Task_Type
5772 then
5773 Error_Msg_N ("storage size cannot be given for &", Nam);
5774
5775 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5776 Error_Msg_N
5777 ("storage size cannot be given for a derived access type",
5778 Nam);
5779
ae888dbd 5780 elsif Duplicate_Clause then
5781 null;
44e4341e 5782
5783 else
5784 Analyze_And_Resolve (Expr, Any_Integer);
5785
5786 if Is_Access_Type (U_Ent) then
6653b695 5787
5788 -- Check for Storage_Pool previously given
5789
5790 declare
5791 SP : constant Node_Id :=
5792 Get_Attribute_Definition_Clause
5793 (U_Ent, Attribute_Storage_Pool);
5794
5795 begin
5796 if Present (SP) then
5797 Check_Pool_Size_Clash (U_Ent, SP, N);
5798 end if;
5799 end;
5800
5801 -- Special case of for x'Storage_Size use 0
44e4341e 5802
5941a4e9 5803 if Is_OK_Static_Expression (Expr)
44e4341e 5804 and then Expr_Value (Expr) = 0
5805 then
5806 Set_No_Pool_Assigned (Btype);
5807 end if;
44e4341e 5808 end if;
5809
5810 Set_Has_Storage_Size_Clause (Btype);
5811 end if;
5812 end Storage_Size;
5813
7189d17f 5814 -----------------
5815 -- Stream_Size --
5816 -----------------
5817
5818 when Attribute_Stream_Size => Stream_Size : declare
5819 Size : constant Uint := Static_Integer (Expr);
5820
5821 begin
15ebb600 5822 if Ada_Version <= Ada_95 then
5823 Check_Restriction (No_Implementation_Attributes, N);
5824 end if;
5825
ae888dbd 5826 if Duplicate_Clause then
5827 null;
7189d17f 5828
5829 elsif Is_Elementary_Type (U_Ent) then
5830 if Size /= System_Storage_Unit
5831 and then
5832 Size /= System_Storage_Unit * 2
5833 and then
5834 Size /= System_Storage_Unit * 4
5835 and then
5836 Size /= System_Storage_Unit * 8
5837 then
5838 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5839 Error_Msg_N
5840 ("stream size for elementary type must be a"
5841 & " power of 2 and at least ^", N);
5842
5843 elsif RM_Size (U_Ent) > Size then
5844 Error_Msg_Uint_1 := RM_Size (U_Ent);
5845 Error_Msg_N
5846 ("stream size for elementary type must be a"
5847 & " power of 2 and at least ^", N);
5848 end if;
5849
5850 Set_Has_Stream_Size_Clause (U_Ent);
5851
5852 else
5853 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5854 end if;
5855 end Stream_Size;
5856
d6f39728 5857 ----------------
5858 -- Value_Size --
5859 ----------------
5860
5861 -- Value_Size attribute definition clause
5862
5863 when Attribute_Value_Size => Value_Size : declare
5864 Size : constant Uint := Static_Integer (Expr);
5865 Biased : Boolean;
5866
5867 begin
5868 if not Is_Type (U_Ent) then
5869 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5870
ae888dbd 5871 elsif Duplicate_Clause then
5872 null;
d6f39728 5873
59ac57b5 5874 elsif Is_Array_Type (U_Ent)
5875 and then not Is_Constrained (U_Ent)
5876 then
5877 Error_Msg_N
5878 ("Value_Size cannot be given for unconstrained array", Nam);
5879
d6f39728 5880 else
5881 if Is_Elementary_Type (U_Ent) then
5882 Check_Size (Expr, U_Ent, Size, Biased);
b77e4501 5883 Set_Biased (U_Ent, N, "value size clause", Biased);
d6f39728 5884 end if;
5885
5886 Set_RM_Size (U_Ent, Size);
5887 end if;
5888 end Value_Size;
5889
81b424ac 5890 -----------------------
5891 -- Variable_Indexing --
5892 -----------------------
5893
5894 when Attribute_Variable_Indexing =>
5895 Check_Indexing_Functions;
5896
d6f39728 5897 -----------
5898 -- Write --
5899 -----------
5900
9f373bb8 5901 when Attribute_Write =>
5902 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5903 Set_Has_Specified_Stream_Write (Ent);
d6f39728 5904
5905 -- All other attributes cannot be set
5906
5907 when others =>
5908 Error_Msg_N
5909 ("attribute& cannot be set with definition clause", N);
d6f39728 5910 end case;
5911
d64221a7 5912 -- The test for the type being frozen must be performed after any
5913 -- expression the clause has been analyzed since the expression itself
5914 -- might cause freezing that makes the clause illegal.
d6f39728 5915
5916 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5917 return;
5918 end if;
5919 end Analyze_Attribute_Definition_Clause;
5920
5921 ----------------------------
5922 -- Analyze_Code_Statement --
5923 ----------------------------
5924
5925 procedure Analyze_Code_Statement (N : Node_Id) is
5926 HSS : constant Node_Id := Parent (N);
5927 SBody : constant Node_Id := Parent (HSS);
5928 Subp : constant Entity_Id := Current_Scope;
5929 Stmt : Node_Id;
5930 Decl : Node_Id;
5931 StmtO : Node_Id;
5932 DeclO : Node_Id;
5933
5934 begin
5935 -- Analyze and check we get right type, note that this implements the
5936 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5937 -- is the only way that Asm_Insn could possibly be visible.
5938
5939 Analyze_And_Resolve (Expression (N));
5940
5941 if Etype (Expression (N)) = Any_Type then
5942 return;
5943 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5944 Error_Msg_N ("incorrect type for code statement", N);
5945 return;
5946 end if;
5947
44e4341e 5948 Check_Code_Statement (N);
5949
d6f39728 5950 -- Make sure we appear in the handled statement sequence of a
5951 -- subprogram (RM 13.8(3)).
5952
5953 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5954 or else Nkind (SBody) /= N_Subprogram_Body
5955 then
5956 Error_Msg_N
5957 ("code statement can only appear in body of subprogram", N);
5958 return;
5959 end if;
5960
5961 -- Do remaining checks (RM 13.8(3)) if not already done
5962
5963 if not Is_Machine_Code_Subprogram (Subp) then
5964 Set_Is_Machine_Code_Subprogram (Subp);
5965
5966 -- No exception handlers allowed
5967
5968 if Present (Exception_Handlers (HSS)) then
5969 Error_Msg_N
5970 ("exception handlers not permitted in machine code subprogram",
5971 First (Exception_Handlers (HSS)));
5972 end if;
5973
5974 -- No declarations other than use clauses and pragmas (we allow
5975 -- certain internally generated declarations as well).
5976
5977 Decl := First (Declarations (SBody));
5978 while Present (Decl) loop
5979 DeclO := Original_Node (Decl);
5980 if Comes_From_Source (DeclO)
fdd294d1 5981 and not Nkind_In (DeclO, N_Pragma,
5982 N_Use_Package_Clause,
5983 N_Use_Type_Clause,
5984 N_Implicit_Label_Declaration)
d6f39728 5985 then
5986 Error_Msg_N
5987 ("this declaration not allowed in machine code subprogram",
5988 DeclO);
5989 end if;
5990
5991 Next (Decl);
5992 end loop;
5993
5994 -- No statements other than code statements, pragmas, and labels.
5995 -- Again we allow certain internally generated statements.
3ab42ff7 5996
c3107527 5997 -- In Ada 2012, qualified expressions are names, and the code
5998 -- statement is initially parsed as a procedure call.
d6f39728 5999
6000 Stmt := First (Statements (HSS));
6001 while Present (Stmt) loop
6002 StmtO := Original_Node (Stmt);
c3107527 6003
59f2fcab 6004 -- A procedure call transformed into a code statement is OK.
6005
c3107527 6006 if Ada_Version >= Ada_2012
6007 and then Nkind (StmtO) = N_Procedure_Call_Statement
59f2fcab 6008 and then Nkind (Name (StmtO)) = N_Qualified_Expression
c3107527 6009 then
6010 null;
6011
6012 elsif Comes_From_Source (StmtO)
fdd294d1 6013 and then not Nkind_In (StmtO, N_Pragma,
6014 N_Label,
6015 N_Code_Statement)
d6f39728 6016 then
6017 Error_Msg_N
6018 ("this statement is not allowed in machine code subprogram",
6019 StmtO);
6020 end if;
6021
6022 Next (Stmt);
6023 end loop;
6024 end if;
d6f39728 6025 end Analyze_Code_Statement;
6026
6027 -----------------------------------------------
6028 -- Analyze_Enumeration_Representation_Clause --
6029 -----------------------------------------------
6030
6031 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
6032 Ident : constant Node_Id := Identifier (N);
6033 Aggr : constant Node_Id := Array_Aggregate (N);
6034 Enumtype : Entity_Id;
6035 Elit : Entity_Id;
6036 Expr : Node_Id;
6037 Assoc : Node_Id;
6038 Choice : Node_Id;
6039 Val : Uint;
b3190af0 6040
6041 Err : Boolean := False;
098d3082 6042 -- Set True to avoid cascade errors and crashes on incorrect source code
d6f39728 6043
e30c7d84 6044 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6045 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6046 -- Allowed range of universal integer (= allowed range of enum lit vals)
6047
d6f39728 6048 Min : Uint;
6049 Max : Uint;
e30c7d84 6050 -- Minimum and maximum values of entries
6051
6052 Max_Node : Node_Id;
6053 -- Pointer to node for literal providing max value
d6f39728 6054
6055 begin
ca301e17 6056 if Ignore_Rep_Clauses then
2ff55065 6057 Kill_Rep_Clause (N);
fbc67f84 6058 return;
6059 end if;
6060
175a6969 6061 -- Ignore enumeration rep clauses by default in CodePeer mode,
6062 -- unless -gnatd.I is specified, as a work around for potential false
6063 -- positive messages.
6064
6065 if CodePeer_Mode and not Debug_Flag_Dot_II then
6066 return;
6067 end if;
6068
d6f39728 6069 -- First some basic error checks
6070
6071 Find_Type (Ident);
6072 Enumtype := Entity (Ident);
6073
6074 if Enumtype = Any_Type
6075 or else Rep_Item_Too_Early (Enumtype, N)
6076 then
6077 return;
6078 else
6079 Enumtype := Underlying_Type (Enumtype);
6080 end if;
6081
6082 if not Is_Enumeration_Type (Enumtype) then
6083 Error_Msg_NE
6084 ("enumeration type required, found}",
6085 Ident, First_Subtype (Enumtype));
6086 return;
6087 end if;
6088
9dfe12ae 6089 -- Ignore rep clause on generic actual type. This will already have
6090 -- been flagged on the template as an error, and this is the safest
6091 -- way to ensure we don't get a junk cascaded message in the instance.
6092
6093 if Is_Generic_Actual_Type (Enumtype) then
6094 return;
6095
6096 -- Type must be in current scope
6097
6098 elsif Scope (Enumtype) /= Current_Scope then
d6f39728 6099 Error_Msg_N ("type must be declared in this scope", Ident);
6100 return;
6101
9dfe12ae 6102 -- Type must be a first subtype
6103
d6f39728 6104 elsif not Is_First_Subtype (Enumtype) then
6105 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6106 return;
6107
9dfe12ae 6108 -- Ignore duplicate rep clause
6109
d6f39728 6110 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6111 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6112 return;
6113
7189d17f 6114 -- Don't allow rep clause for standard [wide_[wide_]]character
9dfe12ae 6115
177675a7 6116 elsif Is_Standard_Character_Type (Enumtype) then
d6f39728 6117 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
9dfe12ae 6118 return;
6119
d9125581 6120 -- Check that the expression is a proper aggregate (no parentheses)
6121
6122 elsif Paren_Count (Aggr) /= 0 then
6123 Error_Msg
6124 ("extra parentheses surrounding aggregate not allowed",
6125 First_Sloc (Aggr));
6126 return;
6127
9dfe12ae 6128 -- All tests passed, so set rep clause in place
d6f39728 6129
6130 else
6131 Set_Has_Enumeration_Rep_Clause (Enumtype);
6132 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6133 end if;
6134
6135 -- Now we process the aggregate. Note that we don't use the normal
6136 -- aggregate code for this purpose, because we don't want any of the
6137 -- normal expansion activities, and a number of special semantic
6138 -- rules apply (including the component type being any integer type)
6139
d6f39728 6140 Elit := First_Literal (Enumtype);
6141
6142 -- First the positional entries if any
6143
6144 if Present (Expressions (Aggr)) then
6145 Expr := First (Expressions (Aggr));
6146 while Present (Expr) loop
6147 if No (Elit) then
6148 Error_Msg_N ("too many entries in aggregate", Expr);
6149 return;
6150 end if;
6151
6152 Val := Static_Integer (Expr);
6153
d9125581 6154 -- Err signals that we found some incorrect entries processing
6155 -- the list. The final checks for completeness and ordering are
6156 -- skipped in this case.
6157
d6f39728 6158 if Val = No_Uint then
6159 Err := True;
f02a9a9a 6160
d6f39728 6161 elsif Val < Lo or else Hi < Val then
6162 Error_Msg_N ("value outside permitted range", Expr);
6163 Err := True;
6164 end if;
6165
6166 Set_Enumeration_Rep (Elit, Val);
6167 Set_Enumeration_Rep_Expr (Elit, Expr);
6168 Next (Expr);
6169 Next (Elit);
6170 end loop;
6171 end if;
6172
6173 -- Now process the named entries if present
6174
6175 if Present (Component_Associations (Aggr)) then
6176 Assoc := First (Component_Associations (Aggr));
6177 while Present (Assoc) loop
6178 Choice := First (Choices (Assoc));
6179
6180 if Present (Next (Choice)) then
6181 Error_Msg_N
6182 ("multiple choice not allowed here", Next (Choice));
6183 Err := True;
6184 end if;
6185
6186 if Nkind (Choice) = N_Others_Choice then
6187 Error_Msg_N ("others choice not allowed here", Choice);
6188 Err := True;
6189
6190 elsif Nkind (Choice) = N_Range then
b3190af0 6191
d6f39728 6192 -- ??? should allow zero/one element range here
b3190af0 6193
d6f39728 6194 Error_Msg_N ("range not allowed here", Choice);
6195 Err := True;
6196
6197 else
6198 Analyze_And_Resolve (Choice, Enumtype);
b3190af0 6199
098d3082 6200 if Error_Posted (Choice) then
d6f39728 6201 Err := True;
098d3082 6202 end if;
d6f39728 6203
098d3082 6204 if not Err then
6205 if Is_Entity_Name (Choice)
6206 and then Is_Type (Entity (Choice))
6207 then
6208 Error_Msg_N ("subtype name not allowed here", Choice);
d6f39728 6209 Err := True;
b3190af0 6210
098d3082 6211 -- ??? should allow static subtype with zero/one entry
d6f39728 6212
098d3082 6213 elsif Etype (Choice) = Base_Type (Enumtype) then
cda40848 6214 if not Is_OK_Static_Expression (Choice) then
098d3082 6215 Flag_Non_Static_Expr
6216 ("non-static expression used for choice!", Choice);
d6f39728 6217 Err := True;
d6f39728 6218
098d3082 6219 else
6220 Elit := Expr_Value_E (Choice);
6221
6222 if Present (Enumeration_Rep_Expr (Elit)) then
6223 Error_Msg_Sloc :=
6224 Sloc (Enumeration_Rep_Expr (Elit));
6225 Error_Msg_NE
6226 ("representation for& previously given#",
6227 Choice, Elit);
6228 Err := True;
6229 end if;
d6f39728 6230
098d3082 6231 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
d6f39728 6232
098d3082 6233 Expr := Expression (Assoc);
6234 Val := Static_Integer (Expr);
d6f39728 6235
098d3082 6236 if Val = No_Uint then
6237 Err := True;
6238
6239 elsif Val < Lo or else Hi < Val then
6240 Error_Msg_N ("value outside permitted range", Expr);
6241 Err := True;
6242 end if;
d6f39728 6243
098d3082 6244 Set_Enumeration_Rep (Elit, Val);
6245 end if;
d6f39728 6246 end if;
6247 end if;
6248 end if;
6249
6250 Next (Assoc);
6251 end loop;
6252 end if;
6253
6254 -- Aggregate is fully processed. Now we check that a full set of
6255 -- representations was given, and that they are in range and in order.
6256 -- These checks are only done if no other errors occurred.
6257
6258 if not Err then
6259 Min := No_Uint;
6260 Max := No_Uint;
6261
6262 Elit := First_Literal (Enumtype);
6263 while Present (Elit) loop
6264 if No (Enumeration_Rep_Expr (Elit)) then
6265 Error_Msg_NE ("missing representation for&!", N, Elit);
6266
6267 else
6268 Val := Enumeration_Rep (Elit);
6269
6270 if Min = No_Uint then
6271 Min := Val;
6272 end if;
6273
6274 if Val /= No_Uint then
6275 if Max /= No_Uint and then Val <= Max then
6276 Error_Msg_NE
6277 ("enumeration value for& not ordered!",
e30c7d84 6278 Enumeration_Rep_Expr (Elit), Elit);
d6f39728 6279 end if;
6280
e30c7d84 6281 Max_Node := Enumeration_Rep_Expr (Elit);
d6f39728 6282 Max := Val;
6283 end if;
6284
e30c7d84 6285 -- If there is at least one literal whose representation is not
6286 -- equal to the Pos value, then note that this enumeration type
6287 -- has a non-standard representation.
d6f39728 6288
6289 if Val /= Enumeration_Pos (Elit) then
6290 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6291 end if;
6292 end if;
6293
6294 Next (Elit);
6295 end loop;
6296
6297 -- Now set proper size information
6298
6299 declare
6300 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6301
6302 begin
6303 if Has_Size_Clause (Enumtype) then
e30c7d84 6304
6305 -- All OK, if size is OK now
6306
6307 if RM_Size (Enumtype) >= Minsize then
d6f39728 6308 null;
6309
6310 else
e30c7d84 6311 -- Try if we can get by with biasing
6312
d6f39728 6313 Minsize :=
6314 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6315
e30c7d84 6316 -- Error message if even biasing does not work
6317
6318 if RM_Size (Enumtype) < Minsize then
6319 Error_Msg_Uint_1 := RM_Size (Enumtype);
6320 Error_Msg_Uint_2 := Max;
6321 Error_Msg_N
6322 ("previously given size (^) is too small "
6323 & "for this value (^)", Max_Node);
6324
6325 -- If biasing worked, indicate that we now have biased rep
d6f39728 6326
6327 else
b77e4501 6328 Set_Biased
6329 (Enumtype, Size_Clause (Enumtype), "size clause");
d6f39728 6330 end if;
6331 end if;
6332
6333 else
6334 Set_RM_Size (Enumtype, Minsize);
6335 Set_Enum_Esize (Enumtype);
6336 end if;
6337
6338 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6339 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6340 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6341 end;
6342 end if;
6343
39a0c1d3 6344 -- We repeat the too late test in case it froze itself
d6f39728 6345
6346 if Rep_Item_Too_Late (Enumtype, N) then
6347 null;
6348 end if;
d6f39728 6349 end Analyze_Enumeration_Representation_Clause;
6350
6351 ----------------------------
6352 -- Analyze_Free_Statement --
6353 ----------------------------
6354
6355 procedure Analyze_Free_Statement (N : Node_Id) is
6356 begin
6357 Analyze (Expression (N));
6358 end Analyze_Free_Statement;
6359
40ca69b9 6360 ---------------------------
6361 -- Analyze_Freeze_Entity --
6362 ---------------------------
6363
6364 procedure Analyze_Freeze_Entity (N : Node_Id) is
40ca69b9 6365 begin
d9f6a4ee 6366 Freeze_Entity_Checks (N);
6367 end Analyze_Freeze_Entity;
98f7db28 6368
d9f6a4ee 6369 -----------------------------------
6370 -- Analyze_Freeze_Generic_Entity --
6371 -----------------------------------
98f7db28 6372
d9f6a4ee 6373 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6374 begin
6375 Freeze_Entity_Checks (N);
6376 end Analyze_Freeze_Generic_Entity;
40ca69b9 6377
d9f6a4ee 6378 ------------------------------------------
6379 -- Analyze_Record_Representation_Clause --
6380 ------------------------------------------
c8da6114 6381
d9f6a4ee 6382 -- Note: we check as much as we can here, but we can't do any checks
6383 -- based on the position values (e.g. overlap checks) until freeze time
6384 -- because especially in Ada 2005 (machine scalar mode), the processing
6385 -- for non-standard bit order can substantially change the positions.
6386 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6387 -- for the remainder of this processing.
d00681a7 6388
d9f6a4ee 6389 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6390 Ident : constant Node_Id := Identifier (N);
6391 Biased : Boolean;
6392 CC : Node_Id;
6393 Comp : Entity_Id;
6394 Fbit : Uint;
6395 Hbit : Uint := Uint_0;
6396 Lbit : Uint;
6397 Ocomp : Entity_Id;
6398 Posit : Uint;
6399 Rectype : Entity_Id;
6400 Recdef : Node_Id;
d00681a7 6401
d9f6a4ee 6402 function Is_Inherited (Comp : Entity_Id) return Boolean;
6403 -- True if Comp is an inherited component in a record extension
d00681a7 6404
d9f6a4ee 6405 ------------------
6406 -- Is_Inherited --
6407 ------------------
d00681a7 6408
d9f6a4ee 6409 function Is_Inherited (Comp : Entity_Id) return Boolean is
6410 Comp_Base : Entity_Id;
d00681a7 6411
d9f6a4ee 6412 begin
6413 if Ekind (Rectype) = E_Record_Subtype then
6414 Comp_Base := Original_Record_Component (Comp);
6415 else
6416 Comp_Base := Comp;
d00681a7 6417 end if;
6418
d9f6a4ee 6419 return Comp_Base /= Original_Record_Component (Comp_Base);
6420 end Is_Inherited;
d00681a7 6421
d9f6a4ee 6422 -- Local variables
d00681a7 6423
d9f6a4ee 6424 Is_Record_Extension : Boolean;
6425 -- True if Rectype is a record extension
d00681a7 6426
d9f6a4ee 6427 CR_Pragma : Node_Id := Empty;
6428 -- Points to N_Pragma node if Complete_Representation pragma present
d00681a7 6429
d9f6a4ee 6430 -- Start of processing for Analyze_Record_Representation_Clause
d00681a7 6431
d9f6a4ee 6432 begin
6433 if Ignore_Rep_Clauses then
2ff55065 6434 Kill_Rep_Clause (N);
d9f6a4ee 6435 return;
d00681a7 6436 end if;
98f7db28 6437
d9f6a4ee 6438 Find_Type (Ident);
6439 Rectype := Entity (Ident);
85377c9b 6440
d9f6a4ee 6441 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6442 return;
6443 else
6444 Rectype := Underlying_Type (Rectype);
6445 end if;
85377c9b 6446
d9f6a4ee 6447 -- First some basic error checks
85377c9b 6448
d9f6a4ee 6449 if not Is_Record_Type (Rectype) then
6450 Error_Msg_NE
6451 ("record type required, found}", Ident, First_Subtype (Rectype));
6452 return;
85377c9b 6453
d9f6a4ee 6454 elsif Scope (Rectype) /= Current_Scope then
6455 Error_Msg_N ("type must be declared in this scope", N);
6456 return;
85377c9b 6457
d9f6a4ee 6458 elsif not Is_First_Subtype (Rectype) then
6459 Error_Msg_N ("cannot give record rep clause for subtype", N);
6460 return;
9dc88aea 6461
d9f6a4ee 6462 elsif Has_Record_Rep_Clause (Rectype) then
6463 Error_Msg_N ("duplicate record rep clause ignored", N);
6464 return;
9dc88aea 6465
d9f6a4ee 6466 elsif Rep_Item_Too_Late (Rectype, N) then
6467 return;
9dc88aea 6468 end if;
fb7f2fc4 6469
d9f6a4ee 6470 -- We know we have a first subtype, now possibly go the the anonymous
6471 -- base type to determine whether Rectype is a record extension.
89f1e35c 6472
d9f6a4ee 6473 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6474 Is_Record_Extension :=
6475 Nkind (Recdef) = N_Derived_Type_Definition
6476 and then Present (Record_Extension_Part (Recdef));
89f1e35c 6477
d9f6a4ee 6478 if Present (Mod_Clause (N)) then
fb7f2fc4 6479 declare
d9f6a4ee 6480 Loc : constant Source_Ptr := Sloc (N);
6481 M : constant Node_Id := Mod_Clause (N);
6482 P : constant List_Id := Pragmas_Before (M);
6483 AtM_Nod : Node_Id;
6484
6485 Mod_Val : Uint;
6486 pragma Warnings (Off, Mod_Val);
fb7f2fc4 6487
6488 begin
d9f6a4ee 6489 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
fb7f2fc4 6490
d9f6a4ee 6491 if Warn_On_Obsolescent_Feature then
6492 Error_Msg_N
6493 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6494 Error_Msg_N
6495 ("\?j?use alignment attribute definition clause instead", N);
6496 end if;
fb7f2fc4 6497
d9f6a4ee 6498 if Present (P) then
6499 Analyze_List (P);
6500 end if;
89f1e35c 6501
d9f6a4ee 6502 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6503 -- the Mod clause into an alignment clause anyway, so that the
6504 -- back-end can compute and back-annotate properly the size and
6505 -- alignment of types that may include this record.
be9124d0 6506
d9f6a4ee 6507 -- This seems dubious, this destroys the source tree in a manner
6508 -- not detectable by ASIS ???
be9124d0 6509
d9f6a4ee 6510 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6511 AtM_Nod :=
6512 Make_Attribute_Definition_Clause (Loc,
83c6c069 6513 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
d9f6a4ee 6514 Chars => Name_Alignment,
6515 Expression => Relocate_Node (Expression (M)));
be9124d0 6516
d9f6a4ee 6517 Set_From_At_Mod (AtM_Nod);
6518 Insert_After (N, AtM_Nod);
6519 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6520 Set_Mod_Clause (N, Empty);
be9124d0 6521
d9f6a4ee 6522 else
6523 -- Get the alignment value to perform error checking
be9124d0 6524
d9f6a4ee 6525 Mod_Val := Get_Alignment_Value (Expression (M));
6526 end if;
6527 end;
6528 end if;
be9124d0 6529
d9f6a4ee 6530 -- For untagged types, clear any existing component clauses for the
6531 -- type. If the type is derived, this is what allows us to override
6532 -- a rep clause for the parent. For type extensions, the representation
6533 -- of the inherited components is inherited, so we want to keep previous
6534 -- component clauses for completeness.
be9124d0 6535
d9f6a4ee 6536 if not Is_Tagged_Type (Rectype) then
6537 Comp := First_Component_Or_Discriminant (Rectype);
6538 while Present (Comp) loop
6539 Set_Component_Clause (Comp, Empty);
6540 Next_Component_Or_Discriminant (Comp);
6541 end loop;
6542 end if;
be9124d0 6543
d9f6a4ee 6544 -- All done if no component clauses
be9124d0 6545
d9f6a4ee 6546 CC := First (Component_Clauses (N));
be9124d0 6547
d9f6a4ee 6548 if No (CC) then
6549 return;
6550 end if;
be9124d0 6551
d9f6a4ee 6552 -- A representation like this applies to the base type
be9124d0 6553
d9f6a4ee 6554 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6555 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6556 Set_Has_Specified_Layout (Base_Type (Rectype));
be9124d0 6557
d9f6a4ee 6558 -- Process the component clauses
be9124d0 6559
d9f6a4ee 6560 while Present (CC) loop
be9124d0 6561
d9f6a4ee 6562 -- Pragma
be9124d0 6563
d9f6a4ee 6564 if Nkind (CC) = N_Pragma then
6565 Analyze (CC);
be9124d0 6566
d9f6a4ee 6567 -- The only pragma of interest is Complete_Representation
be9124d0 6568
d9f6a4ee 6569 if Pragma_Name (CC) = Name_Complete_Representation then
6570 CR_Pragma := CC;
6571 end if;
be9124d0 6572
d9f6a4ee 6573 -- Processing for real component clause
be9124d0 6574
d9f6a4ee 6575 else
6576 Posit := Static_Integer (Position (CC));
6577 Fbit := Static_Integer (First_Bit (CC));
6578 Lbit := Static_Integer (Last_Bit (CC));
be9124d0 6579
d9f6a4ee 6580 if Posit /= No_Uint
6581 and then Fbit /= No_Uint
6582 and then Lbit /= No_Uint
6583 then
6584 if Posit < 0 then
6585 Error_Msg_N
6586 ("position cannot be negative", Position (CC));
be9124d0 6587
d9f6a4ee 6588 elsif Fbit < 0 then
6589 Error_Msg_N
6590 ("first bit cannot be negative", First_Bit (CC));
be9124d0 6591
d9f6a4ee 6592 -- The Last_Bit specified in a component clause must not be
6593 -- less than the First_Bit minus one (RM-13.5.1(10)).
be9124d0 6594
d9f6a4ee 6595 elsif Lbit < Fbit - 1 then
6596 Error_Msg_N
6597 ("last bit cannot be less than first bit minus one",
6598 Last_Bit (CC));
be9124d0 6599
d9f6a4ee 6600 -- Values look OK, so find the corresponding record component
6601 -- Even though the syntax allows an attribute reference for
6602 -- implementation-defined components, GNAT does not allow the
6603 -- tag to get an explicit position.
be9124d0 6604
d9f6a4ee 6605 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6606 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6607 Error_Msg_N ("position of tag cannot be specified", CC);
6608 else
6609 Error_Msg_N ("illegal component name", CC);
6610 end if;
be9124d0 6611
d9f6a4ee 6612 else
6613 Comp := First_Entity (Rectype);
6614 while Present (Comp) loop
6615 exit when Chars (Comp) = Chars (Component_Name (CC));
6616 Next_Entity (Comp);
6617 end loop;
be9124d0 6618
d9f6a4ee 6619 if No (Comp) then
be9124d0 6620
d9f6a4ee 6621 -- Maybe component of base type that is absent from
6622 -- statically constrained first subtype.
be9124d0 6623
d9f6a4ee 6624 Comp := First_Entity (Base_Type (Rectype));
6625 while Present (Comp) loop
6626 exit when Chars (Comp) = Chars (Component_Name (CC));
6627 Next_Entity (Comp);
6628 end loop;
6629 end if;
be9124d0 6630
d9f6a4ee 6631 if No (Comp) then
6632 Error_Msg_N
6633 ("component clause is for non-existent field", CC);
be9124d0 6634
d9f6a4ee 6635 -- Ada 2012 (AI05-0026): Any name that denotes a
6636 -- discriminant of an object of an unchecked union type
6637 -- shall not occur within a record_representation_clause.
be9124d0 6638
d9f6a4ee 6639 -- The general restriction of using record rep clauses on
6640 -- Unchecked_Union types has now been lifted. Since it is
6641 -- possible to introduce a record rep clause which mentions
6642 -- the discriminant of an Unchecked_Union in non-Ada 2012
6643 -- code, this check is applied to all versions of the
6644 -- language.
be9124d0 6645
d9f6a4ee 6646 elsif Ekind (Comp) = E_Discriminant
6647 and then Is_Unchecked_Union (Rectype)
6648 then
6649 Error_Msg_N
6650 ("cannot reference discriminant of unchecked union",
6651 Component_Name (CC));
be9124d0 6652
d9f6a4ee 6653 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6654 Error_Msg_NE
6655 ("component clause not allowed for inherited "
6656 & "component&", CC, Comp);
40ca69b9 6657
d9f6a4ee 6658 elsif Present (Component_Clause (Comp)) then
462a079f 6659
d9f6a4ee 6660 -- Diagnose duplicate rep clause, or check consistency
6661 -- if this is an inherited component. In a double fault,
6662 -- there may be a duplicate inconsistent clause for an
6663 -- inherited component.
462a079f 6664
d9f6a4ee 6665 if Scope (Original_Record_Component (Comp)) = Rectype
6666 or else Parent (Component_Clause (Comp)) = N
6667 then
6668 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6669 Error_Msg_N ("component clause previously given#", CC);
3062c401 6670
6671 else
6672 declare
6673 Rep1 : constant Node_Id := Component_Clause (Comp);
3062c401 6674 begin
6675 if Intval (Position (Rep1)) /=
6676 Intval (Position (CC))
6677 or else Intval (First_Bit (Rep1)) /=
6678 Intval (First_Bit (CC))
6679 or else Intval (Last_Bit (Rep1)) /=
6680 Intval (Last_Bit (CC))
6681 then
b9e61b2a 6682 Error_Msg_N
6683 ("component clause inconsistent "
6684 & "with representation of ancestor", CC);
6a06584c 6685
3062c401 6686 elsif Warn_On_Redundant_Constructs then
b9e61b2a 6687 Error_Msg_N
6a06584c 6688 ("?r?redundant confirming component clause "
6689 & "for component!", CC);
3062c401 6690 end if;
6691 end;
6692 end if;
d6f39728 6693
d2b860b4 6694 -- Normal case where this is the first component clause we
6695 -- have seen for this entity, so set it up properly.
6696
d6f39728 6697 else
83f8f0a6 6698 -- Make reference for field in record rep clause and set
6699 -- appropriate entity field in the field identifier.
6700
6701 Generate_Reference
6702 (Comp, Component_Name (CC), Set_Ref => False);
6703 Set_Entity (Component_Name (CC), Comp);
6704
2866d595 6705 -- Update Fbit and Lbit to the actual bit number
d6f39728 6706
6707 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6708 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6709
d6f39728 6710 if Has_Size_Clause (Rectype)
ada34def 6711 and then RM_Size (Rectype) <= Lbit
d6f39728 6712 then
6713 Error_Msg_N
6714 ("bit number out of range of specified size",
6715 Last_Bit (CC));
6716 else
6717 Set_Component_Clause (Comp, CC);
6718 Set_Component_Bit_Offset (Comp, Fbit);
6719 Set_Esize (Comp, 1 + (Lbit - Fbit));
6720 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6721 Set_Normalized_Position (Comp, Fbit / SSU);
6722
a0fc8c5b 6723 if Warn_On_Overridden_Size
6724 and then Has_Size_Clause (Etype (Comp))
6725 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6726 then
6727 Error_Msg_NE
1e3532e7 6728 ("?S?component size overrides size clause for&",
a0fc8c5b 6729 Component_Name (CC), Etype (Comp));
6730 end if;
6731
ea61a7ea 6732 -- This information is also set in the corresponding
6733 -- component of the base type, found by accessing the
6734 -- Original_Record_Component link if it is present.
d6f39728 6735
6736 Ocomp := Original_Record_Component (Comp);
6737
6738 if Hbit < Lbit then
6739 Hbit := Lbit;
6740 end if;
6741
6742 Check_Size
6743 (Component_Name (CC),
6744 Etype (Comp),
6745 Esize (Comp),
6746 Biased);
6747
b77e4501 6748 Set_Biased
6749 (Comp, First_Node (CC), "component clause", Biased);
cc46ff4b 6750
d6f39728 6751 if Present (Ocomp) then
6752 Set_Component_Clause (Ocomp, CC);
6753 Set_Component_Bit_Offset (Ocomp, Fbit);
6754 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6755 Set_Normalized_Position (Ocomp, Fbit / SSU);
6756 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6757
6758 Set_Normalized_Position_Max
6759 (Ocomp, Normalized_Position (Ocomp));
6760
b77e4501 6761 -- Note: we don't use Set_Biased here, because we
6762 -- already gave a warning above if needed, and we
6763 -- would get a duplicate for the same name here.
6764
d6f39728 6765 Set_Has_Biased_Representation
6766 (Ocomp, Has_Biased_Representation (Comp));
6767 end if;
6768
6769 if Esize (Comp) < 0 then
6770 Error_Msg_N ("component size is negative", CC);
6771 end if;
6772 end if;
6773 end if;
6774 end if;
6775 end if;
6776 end if;
6777
6778 Next (CC);
6779 end loop;
6780
67278d60 6781 -- Check missing components if Complete_Representation pragma appeared
d6f39728 6782
67278d60 6783 if Present (CR_Pragma) then
6784 Comp := First_Component_Or_Discriminant (Rectype);
6785 while Present (Comp) loop
6786 if No (Component_Clause (Comp)) then
6787 Error_Msg_NE
6788 ("missing component clause for &", CR_Pragma, Comp);
6789 end if;
d6f39728 6790
67278d60 6791 Next_Component_Or_Discriminant (Comp);
6792 end loop;
d6f39728 6793
1e3532e7 6794 -- Give missing components warning if required
15ebb600 6795
fdd294d1 6796 elsif Warn_On_Unrepped_Components then
15ebb600 6797 declare
6798 Num_Repped_Components : Nat := 0;
6799 Num_Unrepped_Components : Nat := 0;
6800
6801 begin
6802 -- First count number of repped and unrepped components
6803
6804 Comp := First_Component_Or_Discriminant (Rectype);
6805 while Present (Comp) loop
6806 if Present (Component_Clause (Comp)) then
6807 Num_Repped_Components := Num_Repped_Components + 1;
6808 else
6809 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6810 end if;
6811
6812 Next_Component_Or_Discriminant (Comp);
6813 end loop;
6814
6815 -- We are only interested in the case where there is at least one
6816 -- unrepped component, and at least half the components have rep
6817 -- clauses. We figure that if less than half have them, then the
87f9eef5 6818 -- partial rep clause is really intentional. If the component
6819 -- type has no underlying type set at this point (as for a generic
6820 -- formal type), we don't know enough to give a warning on the
6821 -- component.
15ebb600 6822
6823 if Num_Unrepped_Components > 0
6824 and then Num_Unrepped_Components < Num_Repped_Components
6825 then
6826 Comp := First_Component_Or_Discriminant (Rectype);
6827 while Present (Comp) loop
83f8f0a6 6828 if No (Component_Clause (Comp))
3062c401 6829 and then Comes_From_Source (Comp)
87f9eef5 6830 and then Present (Underlying_Type (Etype (Comp)))
83f8f0a6 6831 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
67278d60 6832 or else Size_Known_At_Compile_Time
6833 (Underlying_Type (Etype (Comp))))
fdd294d1 6834 and then not Has_Warnings_Off (Rectype)
2be1f7d7 6835
6836 -- Ignore discriminant in unchecked union, since it is
6837 -- not there, and cannot have a component clause.
6838
6839 and then (not Is_Unchecked_Union (Rectype)
6840 or else Ekind (Comp) /= E_Discriminant)
83f8f0a6 6841 then
15ebb600 6842 Error_Msg_Sloc := Sloc (Comp);
6843 Error_Msg_NE
1e3532e7 6844 ("?C?no component clause given for & declared #",
15ebb600 6845 N, Comp);
6846 end if;
6847
6848 Next_Component_Or_Discriminant (Comp);
6849 end loop;
6850 end if;
6851 end;
d6f39728 6852 end if;
d6f39728 6853 end Analyze_Record_Representation_Clause;
6854
eb66e842 6855 -------------------------------------
6856 -- Build_Discrete_Static_Predicate --
6857 -------------------------------------
9ea61fdd 6858
eb66e842 6859 procedure Build_Discrete_Static_Predicate
6860 (Typ : Entity_Id;
6861 Expr : Node_Id;
6862 Nam : Name_Id)
9ea61fdd 6863 is
eb66e842 6864 Loc : constant Source_Ptr := Sloc (Expr);
9ea61fdd 6865
eb66e842 6866 Non_Static : exception;
6867 -- Raised if something non-static is found
9ea61fdd 6868
eb66e842 6869 Btyp : constant Entity_Id := Base_Type (Typ);
9ea61fdd 6870
eb66e842 6871 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6872 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6873 -- Low bound and high bound value of base type of Typ
9ea61fdd 6874
afc229da 6875 TLo : Uint;
6876 THi : Uint;
6877 -- Bounds for constructing the static predicate. We use the bound of the
6878 -- subtype if it is static, otherwise the corresponding base type bound.
6879 -- Note: a non-static subtype can have a static predicate.
9ea61fdd 6880
eb66e842 6881 type REnt is record
6882 Lo, Hi : Uint;
6883 end record;
6884 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6885 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6886 -- value.
9ea61fdd 6887
eb66e842 6888 type RList is array (Nat range <>) of REnt;
6889 -- A list of ranges. The ranges are sorted in increasing order, and are
6890 -- disjoint (there is a gap of at least one value between each range in
6891 -- the table). A value is in the set of ranges in Rlist if it lies
6892 -- within one of these ranges.
9ea61fdd 6893
eb66e842 6894 False_Range : constant RList :=
6895 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6896 -- An empty set of ranges represents a range list that can never be
6897 -- satisfied, since there are no ranges in which the value could lie,
6898 -- so it does not lie in any of them. False_Range is a canonical value
6899 -- for this empty set, but general processing should test for an Rlist
6900 -- with length zero (see Is_False predicate), since other null ranges
6901 -- may appear which must be treated as False.
5b5df4a9 6902
eb66e842 6903 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6904 -- Range representing True, value must be in the base range
5b5df4a9 6905
eb66e842 6906 function "and" (Left : RList; Right : RList) return RList;
6907 -- And's together two range lists, returning a range list. This is a set
6908 -- intersection operation.
5b5df4a9 6909
eb66e842 6910 function "or" (Left : RList; Right : RList) return RList;
6911 -- Or's together two range lists, returning a range list. This is a set
6912 -- union operation.
87f3d5d3 6913
eb66e842 6914 function "not" (Right : RList) return RList;
6915 -- Returns complement of a given range list, i.e. a range list
6916 -- representing all the values in TLo .. THi that are not in the input
6917 -- operand Right.
ed4adc99 6918
eb66e842 6919 function Build_Val (V : Uint) return Node_Id;
6920 -- Return an analyzed N_Identifier node referencing this value, suitable
5c6a5792 6921 -- for use as an entry in the Static_Discrte_Predicate list. This node
6922 -- is typed with the base type.
5b5df4a9 6923
eb66e842 6924 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6925 -- Return an analyzed N_Range node referencing this range, suitable for
5c6a5792 6926 -- use as an entry in the Static_Discrete_Predicate list. This node is
6927 -- typed with the base type.
5b5df4a9 6928
eb66e842 6929 function Get_RList (Exp : Node_Id) return RList;
6930 -- This is a recursive routine that converts the given expression into a
6931 -- list of ranges, suitable for use in building the static predicate.
5b5df4a9 6932
eb66e842 6933 function Is_False (R : RList) return Boolean;
6934 pragma Inline (Is_False);
6935 -- Returns True if the given range list is empty, and thus represents a
6936 -- False list of ranges that can never be satisfied.
87f3d5d3 6937
eb66e842 6938 function Is_True (R : RList) return Boolean;
6939 -- Returns True if R trivially represents the True predicate by having a
6940 -- single range from BLo to BHi.
5b5df4a9 6941
eb66e842 6942 function Is_Type_Ref (N : Node_Id) return Boolean;
6943 pragma Inline (Is_Type_Ref);
6944 -- Returns if True if N is a reference to the type for the predicate in
6945 -- the expression (i.e. if it is an identifier whose Chars field matches
7de4cba3 6946 -- the Nam given in the call). N must not be parenthesized, if the type
6947 -- name appears in parens, this routine will return False.
5b5df4a9 6948
eb66e842 6949 function Lo_Val (N : Node_Id) return Uint;
5c6a5792 6950 -- Given an entry from a Static_Discrete_Predicate list that is either
6951 -- a static expression or static range, gets either the expression value
6952 -- or the low bound of the range.
5b5df4a9 6953
eb66e842 6954 function Hi_Val (N : Node_Id) return Uint;
5c6a5792 6955 -- Given an entry from a Static_Discrete_Predicate list that is either
6956 -- a static expression or static range, gets either the expression value
6957 -- or the high bound of the range.
5b5df4a9 6958
eb66e842 6959 function Membership_Entry (N : Node_Id) return RList;
6960 -- Given a single membership entry (range, value, or subtype), returns
6961 -- the corresponding range list. Raises Static_Error if not static.
5b5df4a9 6962
eb66e842 6963 function Membership_Entries (N : Node_Id) return RList;
6964 -- Given an element on an alternatives list of a membership operation,
6965 -- returns the range list corresponding to this entry and all following
6966 -- entries (i.e. returns the "or" of this list of values).
b9e61b2a 6967
eb66e842 6968 function Stat_Pred (Typ : Entity_Id) return RList;
6969 -- Given a type, if it has a static predicate, then return the predicate
6970 -- as a range list, otherwise raise Non_Static.
c4968aa2 6971
eb66e842 6972 -----------
6973 -- "and" --
6974 -----------
c4968aa2 6975
eb66e842 6976 function "and" (Left : RList; Right : RList) return RList is
6977 FEnt : REnt;
6978 -- First range of result
c4968aa2 6979
eb66e842 6980 SLeft : Nat := Left'First;
6981 -- Start of rest of left entries
c4968aa2 6982
eb66e842 6983 SRight : Nat := Right'First;
6984 -- Start of rest of right entries
2072eaa9 6985
eb66e842 6986 begin
6987 -- If either range is True, return the other
5b5df4a9 6988
eb66e842 6989 if Is_True (Left) then
6990 return Right;
6991 elsif Is_True (Right) then
6992 return Left;
6993 end if;
87f3d5d3 6994
eb66e842 6995 -- If either range is False, return False
5b5df4a9 6996
eb66e842 6997 if Is_False (Left) or else Is_False (Right) then
6998 return False_Range;
6999 end if;
4c1fd062 7000
eb66e842 7001 -- Loop to remove entries at start that are disjoint, and thus just
7002 -- get discarded from the result entirely.
5b5df4a9 7003
eb66e842 7004 loop
7005 -- If no operands left in either operand, result is false
5b5df4a9 7006
eb66e842 7007 if SLeft > Left'Last or else SRight > Right'Last then
7008 return False_Range;
5b5df4a9 7009
eb66e842 7010 -- Discard first left operand entry if disjoint with right
5b5df4a9 7011
eb66e842 7012 elsif Left (SLeft).Hi < Right (SRight).Lo then
7013 SLeft := SLeft + 1;
5b5df4a9 7014
eb66e842 7015 -- Discard first right operand entry if disjoint with left
5b5df4a9 7016
eb66e842 7017 elsif Right (SRight).Hi < Left (SLeft).Lo then
7018 SRight := SRight + 1;
5b5df4a9 7019
eb66e842 7020 -- Otherwise we have an overlapping entry
5b5df4a9 7021
eb66e842 7022 else
7023 exit;
7024 end if;
7025 end loop;
5b5df4a9 7026
eb66e842 7027 -- Now we have two non-null operands, and first entries overlap. The
7028 -- first entry in the result will be the overlapping part of these
7029 -- two entries.
47a46747 7030
eb66e842 7031 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7032 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
47a46747 7033
eb66e842 7034 -- Now we can remove the entry that ended at a lower value, since its
7035 -- contribution is entirely contained in Fent.
5b5df4a9 7036
eb66e842 7037 if Left (SLeft).Hi <= Right (SRight).Hi then
7038 SLeft := SLeft + 1;
7039 else
7040 SRight := SRight + 1;
7041 end if;
5b5df4a9 7042
eb66e842 7043 -- Compute result by concatenating this first entry with the "and" of
7044 -- the remaining parts of the left and right operands. Note that if
7045 -- either of these is empty, "and" will yield empty, so that we will
7046 -- end up with just Fent, which is what we want in that case.
5b5df4a9 7047
eb66e842 7048 return
7049 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7050 end "and";
fb7f2fc4 7051
eb66e842 7052 -----------
7053 -- "not" --
7054 -----------
fb7f2fc4 7055
eb66e842 7056 function "not" (Right : RList) return RList is
7057 begin
7058 -- Return True if False range
fb7f2fc4 7059
eb66e842 7060 if Is_False (Right) then
7061 return True_Range;
7062 end if;
ed4adc99 7063
eb66e842 7064 -- Return False if True range
fb7f2fc4 7065
eb66e842 7066 if Is_True (Right) then
7067 return False_Range;
7068 end if;
fb7f2fc4 7069
eb66e842 7070 -- Here if not trivial case
87f3d5d3 7071
eb66e842 7072 declare
7073 Result : RList (1 .. Right'Length + 1);
7074 -- May need one more entry for gap at beginning and end
87f3d5d3 7075
eb66e842 7076 Count : Nat := 0;
7077 -- Number of entries stored in Result
4098232e 7078
eb66e842 7079 begin
7080 -- Gap at start
4098232e 7081
eb66e842 7082 if Right (Right'First).Lo > TLo then
7083 Count := Count + 1;
7084 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7085 end if;
ed4adc99 7086
eb66e842 7087 -- Gaps between ranges
ed4adc99 7088
eb66e842 7089 for J in Right'First .. Right'Last - 1 loop
7090 Count := Count + 1;
7091 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7092 end loop;
5b5df4a9 7093
eb66e842 7094 -- Gap at end
5b5df4a9 7095
eb66e842 7096 if Right (Right'Last).Hi < THi then
7097 Count := Count + 1;
7098 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7099 end if;
5b5df4a9 7100
eb66e842 7101 return Result (1 .. Count);
7102 end;
7103 end "not";
5b5df4a9 7104
eb66e842 7105 ----------
7106 -- "or" --
7107 ----------
5b5df4a9 7108
eb66e842 7109 function "or" (Left : RList; Right : RList) return RList is
7110 FEnt : REnt;
7111 -- First range of result
5b5df4a9 7112
eb66e842 7113 SLeft : Nat := Left'First;
7114 -- Start of rest of left entries
5b5df4a9 7115
eb66e842 7116 SRight : Nat := Right'First;
7117 -- Start of rest of right entries
5b5df4a9 7118
eb66e842 7119 begin
7120 -- If either range is True, return True
5b5df4a9 7121
eb66e842 7122 if Is_True (Left) or else Is_True (Right) then
7123 return True_Range;
7124 end if;
5b5df4a9 7125
eb66e842 7126 -- If either range is False (empty), return the other
5b5df4a9 7127
eb66e842 7128 if Is_False (Left) then
7129 return Right;
7130 elsif Is_False (Right) then
7131 return Left;
7132 end if;
5b5df4a9 7133
eb66e842 7134 -- Initialize result first entry from left or right operand depending
7135 -- on which starts with the lower range.
5b5df4a9 7136
eb66e842 7137 if Left (SLeft).Lo < Right (SRight).Lo then
7138 FEnt := Left (SLeft);
7139 SLeft := SLeft + 1;
7140 else
7141 FEnt := Right (SRight);
7142 SRight := SRight + 1;
7143 end if;
5b5df4a9 7144
eb66e842 7145 -- This loop eats ranges from left and right operands that are
7146 -- contiguous with the first range we are gathering.
9ea61fdd 7147
eb66e842 7148 loop
7149 -- Eat first entry in left operand if contiguous or overlapped by
7150 -- gathered first operand of result.
9ea61fdd 7151
eb66e842 7152 if SLeft <= Left'Last
7153 and then Left (SLeft).Lo <= FEnt.Hi + 1
7154 then
7155 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7156 SLeft := SLeft + 1;
9ea61fdd 7157
eb66e842 7158 -- Eat first entry in right operand if contiguous or overlapped by
7159 -- gathered right operand of result.
9ea61fdd 7160
eb66e842 7161 elsif SRight <= Right'Last
7162 and then Right (SRight).Lo <= FEnt.Hi + 1
7163 then
7164 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7165 SRight := SRight + 1;
9ea61fdd 7166
eb66e842 7167 -- All done if no more entries to eat
5b5df4a9 7168
eb66e842 7169 else
7170 exit;
7171 end if;
7172 end loop;
5b5df4a9 7173
eb66e842 7174 -- Obtain result as the first entry we just computed, concatenated
7175 -- to the "or" of the remaining results (if one operand is empty,
7176 -- this will just concatenate with the other
5b5df4a9 7177
eb66e842 7178 return
7179 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7180 end "or";
5b5df4a9 7181
eb66e842 7182 -----------------
7183 -- Build_Range --
7184 -----------------
5b5df4a9 7185
eb66e842 7186 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7187 Result : Node_Id;
5b5df4a9 7188 begin
eb66e842 7189 Result :=
7190 Make_Range (Loc,
7191 Low_Bound => Build_Val (Lo),
7192 High_Bound => Build_Val (Hi));
7193 Set_Etype (Result, Btyp);
7194 Set_Analyzed (Result);
7195 return Result;
7196 end Build_Range;
5b5df4a9 7197
eb66e842 7198 ---------------
7199 -- Build_Val --
7200 ---------------
5b5df4a9 7201
eb66e842 7202 function Build_Val (V : Uint) return Node_Id is
7203 Result : Node_Id;
5b5df4a9 7204
eb66e842 7205 begin
7206 if Is_Enumeration_Type (Typ) then
7207 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7208 else
7209 Result := Make_Integer_Literal (Loc, V);
7210 end if;
5b5df4a9 7211
eb66e842 7212 Set_Etype (Result, Btyp);
7213 Set_Is_Static_Expression (Result);
7214 Set_Analyzed (Result);
7215 return Result;
7216 end Build_Val;
87f3d5d3 7217
eb66e842 7218 ---------------
7219 -- Get_RList --
7220 ---------------
87f3d5d3 7221
eb66e842 7222 function Get_RList (Exp : Node_Id) return RList is
7223 Op : Node_Kind;
7224 Val : Uint;
87f3d5d3 7225
eb66e842 7226 begin
7227 -- Static expression can only be true or false
87f3d5d3 7228
eb66e842 7229 if Is_OK_Static_Expression (Exp) then
7230 if Expr_Value (Exp) = 0 then
7231 return False_Range;
7232 else
7233 return True_Range;
9ea61fdd 7234 end if;
eb66e842 7235 end if;
87f3d5d3 7236
eb66e842 7237 -- Otherwise test node type
192b8dab 7238
eb66e842 7239 Op := Nkind (Exp);
192b8dab 7240
eb66e842 7241 case Op is
5d3fb947 7242
eb66e842 7243 -- And
5d3fb947 7244
eb66e842 7245 when N_Op_And | N_And_Then =>
7246 return Get_RList (Left_Opnd (Exp))
7247 and
7248 Get_RList (Right_Opnd (Exp));
5b5df4a9 7249
eb66e842 7250 -- Or
9dc88aea 7251
eb66e842 7252 when N_Op_Or | N_Or_Else =>
7253 return Get_RList (Left_Opnd (Exp))
7254 or
7255 Get_RList (Right_Opnd (Exp));
7c443ae8 7256
eb66e842 7257 -- Not
9dc88aea 7258
eb66e842 7259 when N_Op_Not =>
7260 return not Get_RList (Right_Opnd (Exp));
9dc88aea 7261
eb66e842 7262 -- Comparisons of type with static value
84c8f0b8 7263
eb66e842 7264 when N_Op_Compare =>
490beba6 7265
eb66e842 7266 -- Type is left operand
9dc88aea 7267
eb66e842 7268 if Is_Type_Ref (Left_Opnd (Exp))
7269 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7270 then
7271 Val := Expr_Value (Right_Opnd (Exp));
84c8f0b8 7272
eb66e842 7273 -- Typ is right operand
84c8f0b8 7274
eb66e842 7275 elsif Is_Type_Ref (Right_Opnd (Exp))
7276 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7277 then
7278 Val := Expr_Value (Left_Opnd (Exp));
84c8f0b8 7279
eb66e842 7280 -- Invert sense of comparison
84c8f0b8 7281
eb66e842 7282 case Op is
7283 when N_Op_Gt => Op := N_Op_Lt;
7284 when N_Op_Lt => Op := N_Op_Gt;
7285 when N_Op_Ge => Op := N_Op_Le;
7286 when N_Op_Le => Op := N_Op_Ge;
7287 when others => null;
7288 end case;
84c8f0b8 7289
eb66e842 7290 -- Other cases are non-static
34d045d3 7291
eb66e842 7292 else
7293 raise Non_Static;
7294 end if;
9dc88aea 7295
eb66e842 7296 -- Construct range according to comparison operation
9dc88aea 7297
eb66e842 7298 case Op is
7299 when N_Op_Eq =>
7300 return RList'(1 => REnt'(Val, Val));
9dc88aea 7301
eb66e842 7302 when N_Op_Ge =>
7303 return RList'(1 => REnt'(Val, BHi));
84c8f0b8 7304
eb66e842 7305 when N_Op_Gt =>
7306 return RList'(1 => REnt'(Val + 1, BHi));
84c8f0b8 7307
eb66e842 7308 when N_Op_Le =>
7309 return RList'(1 => REnt'(BLo, Val));
fb7f2fc4 7310
eb66e842 7311 when N_Op_Lt =>
7312 return RList'(1 => REnt'(BLo, Val - 1));
9dc88aea 7313
eb66e842 7314 when N_Op_Ne =>
7315 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9dc88aea 7316
eb66e842 7317 when others =>
7318 raise Program_Error;
7319 end case;
9dc88aea 7320
eb66e842 7321 -- Membership (IN)
9dc88aea 7322
eb66e842 7323 when N_In =>
7324 if not Is_Type_Ref (Left_Opnd (Exp)) then
7325 raise Non_Static;
7326 end if;
9dc88aea 7327
eb66e842 7328 if Present (Right_Opnd (Exp)) then
7329 return Membership_Entry (Right_Opnd (Exp));
7330 else
7331 return Membership_Entries (First (Alternatives (Exp)));
7332 end if;
9dc88aea 7333
eb66e842 7334 -- Negative membership (NOT IN)
9dc88aea 7335
eb66e842 7336 when N_Not_In =>
7337 if not Is_Type_Ref (Left_Opnd (Exp)) then
7338 raise Non_Static;
7339 end if;
9dc88aea 7340
eb66e842 7341 if Present (Right_Opnd (Exp)) then
7342 return not Membership_Entry (Right_Opnd (Exp));
7343 else
7344 return not Membership_Entries (First (Alternatives (Exp)));
7345 end if;
9dc88aea 7346
eb66e842 7347 -- Function call, may be call to static predicate
9dc88aea 7348
eb66e842 7349 when N_Function_Call =>
7350 if Is_Entity_Name (Name (Exp)) then
7351 declare
7352 Ent : constant Entity_Id := Entity (Name (Exp));
7353 begin
7354 if Is_Predicate_Function (Ent)
7355 or else
7356 Is_Predicate_Function_M (Ent)
7357 then
7358 return Stat_Pred (Etype (First_Formal (Ent)));
7359 end if;
7360 end;
7361 end if;
9dc88aea 7362
eb66e842 7363 -- Other function call cases are non-static
9dc88aea 7364
eb66e842 7365 raise Non_Static;
490beba6 7366
eb66e842 7367 -- Qualified expression, dig out the expression
c92e878b 7368
eb66e842 7369 when N_Qualified_Expression =>
7370 return Get_RList (Expression (Exp));
4c1fd062 7371
eb66e842 7372 when N_Case_Expression =>
7373 declare
7374 Alt : Node_Id;
7375 Choices : List_Id;
7376 Dep : Node_Id;
4c1fd062 7377
eb66e842 7378 begin
7379 if not Is_Entity_Name (Expression (Expr))
7380 or else Etype (Expression (Expr)) /= Typ
7381 then
7382 Error_Msg_N
7383 ("expression must denaote subtype", Expression (Expr));
7384 return False_Range;
7385 end if;
9dc88aea 7386
eb66e842 7387 -- Collect discrete choices in all True alternatives
9dc88aea 7388
eb66e842 7389 Choices := New_List;
7390 Alt := First (Alternatives (Exp));
7391 while Present (Alt) loop
7392 Dep := Expression (Alt);
34d045d3 7393
cda40848 7394 if not Is_OK_Static_Expression (Dep) then
eb66e842 7395 raise Non_Static;
ebbab42d 7396
eb66e842 7397 elsif Is_True (Expr_Value (Dep)) then
7398 Append_List_To (Choices,
7399 New_Copy_List (Discrete_Choices (Alt)));
7400 end if;
fb7f2fc4 7401
eb66e842 7402 Next (Alt);
7403 end loop;
9dc88aea 7404
eb66e842 7405 return Membership_Entries (First (Choices));
7406 end;
9dc88aea 7407
eb66e842 7408 -- Expression with actions: if no actions, dig out expression
9dc88aea 7409
eb66e842 7410 when N_Expression_With_Actions =>
7411 if Is_Empty_List (Actions (Exp)) then
7412 return Get_RList (Expression (Exp));
7413 else
7414 raise Non_Static;
7415 end if;
9dc88aea 7416
eb66e842 7417 -- Xor operator
490beba6 7418
eb66e842 7419 when N_Op_Xor =>
7420 return (Get_RList (Left_Opnd (Exp))
7421 and not Get_RList (Right_Opnd (Exp)))
7422 or (Get_RList (Right_Opnd (Exp))
7423 and not Get_RList (Left_Opnd (Exp)));
9dc88aea 7424
eb66e842 7425 -- Any other node type is non-static
fb7f2fc4 7426
eb66e842 7427 when others =>
7428 raise Non_Static;
7429 end case;
7430 end Get_RList;
fb7f2fc4 7431
eb66e842 7432 ------------
7433 -- Hi_Val --
7434 ------------
fb7f2fc4 7435
eb66e842 7436 function Hi_Val (N : Node_Id) return Uint is
7437 begin
cda40848 7438 if Is_OK_Static_Expression (N) then
eb66e842 7439 return Expr_Value (N);
7440 else
7441 pragma Assert (Nkind (N) = N_Range);
7442 return Expr_Value (High_Bound (N));
7443 end if;
7444 end Hi_Val;
fb7f2fc4 7445
eb66e842 7446 --------------
7447 -- Is_False --
7448 --------------
fb7f2fc4 7449
eb66e842 7450 function Is_False (R : RList) return Boolean is
7451 begin
7452 return R'Length = 0;
7453 end Is_False;
9dc88aea 7454
eb66e842 7455 -------------
7456 -- Is_True --
7457 -------------
9dc88aea 7458
eb66e842 7459 function Is_True (R : RList) return Boolean is
7460 begin
7461 return R'Length = 1
7462 and then R (R'First).Lo = BLo
7463 and then R (R'First).Hi = BHi;
7464 end Is_True;
9dc88aea 7465
eb66e842 7466 -----------------
7467 -- Is_Type_Ref --
7468 -----------------
9dc88aea 7469
eb66e842 7470 function Is_Type_Ref (N : Node_Id) return Boolean is
7471 begin
7de4cba3 7472 return Nkind (N) = N_Identifier
7473 and then Chars (N) = Nam
7474 and then Paren_Count (N) = 0;
eb66e842 7475 end Is_Type_Ref;
9dc88aea 7476
eb66e842 7477 ------------
7478 -- Lo_Val --
7479 ------------
9dc88aea 7480
eb66e842 7481 function Lo_Val (N : Node_Id) return Uint is
84c8f0b8 7482 begin
cda40848 7483 if Is_OK_Static_Expression (N) then
eb66e842 7484 return Expr_Value (N);
84c8f0b8 7485 else
eb66e842 7486 pragma Assert (Nkind (N) = N_Range);
7487 return Expr_Value (Low_Bound (N));
84c8f0b8 7488 end if;
eb66e842 7489 end Lo_Val;
d97beb2f 7490
eb66e842 7491 ------------------------
7492 -- Membership_Entries --
7493 ------------------------
d97beb2f 7494
eb66e842 7495 function Membership_Entries (N : Node_Id) return RList is
84c8f0b8 7496 begin
eb66e842 7497 if No (Next (N)) then
7498 return Membership_Entry (N);
84c8f0b8 7499 else
eb66e842 7500 return Membership_Entry (N) or Membership_Entries (Next (N));
84c8f0b8 7501 end if;
eb66e842 7502 end Membership_Entries;
84c8f0b8 7503
eb66e842 7504 ----------------------
7505 -- Membership_Entry --
7506 ----------------------
84c8f0b8 7507
eb66e842 7508 function Membership_Entry (N : Node_Id) return RList is
7509 Val : Uint;
7510 SLo : Uint;
7511 SHi : Uint;
d97beb2f 7512
eb66e842 7513 begin
7514 -- Range case
d97beb2f 7515
eb66e842 7516 if Nkind (N) = N_Range then
cda40848 7517 if not Is_OK_Static_Expression (Low_Bound (N))
eb66e842 7518 or else
cda40848 7519 not Is_OK_Static_Expression (High_Bound (N))
eb66e842 7520 then
7521 raise Non_Static;
7522 else
7523 SLo := Expr_Value (Low_Bound (N));
7524 SHi := Expr_Value (High_Bound (N));
7525 return RList'(1 => REnt'(SLo, SHi));
7526 end if;
84c8f0b8 7527
eb66e842 7528 -- Static expression case
84c8f0b8 7529
cda40848 7530 elsif Is_OK_Static_Expression (N) then
eb66e842 7531 Val := Expr_Value (N);
7532 return RList'(1 => REnt'(Val, Val));
d97beb2f 7533
eb66e842 7534 -- Identifier (other than static expression) case
d97beb2f 7535
eb66e842 7536 else pragma Assert (Nkind (N) = N_Identifier);
d97beb2f 7537
eb66e842 7538 -- Type case
d97beb2f 7539
eb66e842 7540 if Is_Type (Entity (N)) then
d97beb2f 7541
eb66e842 7542 -- If type has predicates, process them
d97beb2f 7543
eb66e842 7544 if Has_Predicates (Entity (N)) then
7545 return Stat_Pred (Entity (N));
d97beb2f 7546
eb66e842 7547 -- For static subtype without predicates, get range
9dc88aea 7548
cda40848 7549 elsif Is_OK_Static_Subtype (Entity (N)) then
eb66e842 7550 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7551 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7552 return RList'(1 => REnt'(SLo, SHi));
9f269bd8 7553
eb66e842 7554 -- Any other type makes us non-static
9f269bd8 7555
eb66e842 7556 else
7557 raise Non_Static;
7558 end if;
84c8f0b8 7559
eb66e842 7560 -- Any other kind of identifier in predicate (e.g. a non-static
7561 -- expression value) means this is not a static predicate.
84c8f0b8 7562
eb66e842 7563 else
7564 raise Non_Static;
7565 end if;
7566 end if;
7567 end Membership_Entry;
84c8f0b8 7568
eb66e842 7569 ---------------
7570 -- Stat_Pred --
7571 ---------------
84c8f0b8 7572
eb66e842 7573 function Stat_Pred (Typ : Entity_Id) return RList is
7574 begin
7575 -- Not static if type does not have static predicates
84c8f0b8 7576
5c6a5792 7577 if not Has_Static_Predicate (Typ) then
eb66e842 7578 raise Non_Static;
7579 end if;
84c8f0b8 7580
eb66e842 7581 -- Otherwise we convert the predicate list to a range list
84c8f0b8 7582
eb66e842 7583 declare
5c6a5792 7584 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7585 Result : RList (1 .. List_Length (Spred));
eb66e842 7586 P : Node_Id;
84c8f0b8 7587
eb66e842 7588 begin
5c6a5792 7589 P := First (Static_Discrete_Predicate (Typ));
eb66e842 7590 for J in Result'Range loop
7591 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7592 Next (P);
7593 end loop;
84c8f0b8 7594
eb66e842 7595 return Result;
7596 end;
7597 end Stat_Pred;
84c8f0b8 7598
eb66e842 7599 -- Start of processing for Build_Discrete_Static_Predicate
84c8f0b8 7600
eb66e842 7601 begin
afc229da 7602 -- Establish bounds for the predicate
7603
7604 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7605 TLo := Expr_Value (Type_Low_Bound (Typ));
7606 else
7607 TLo := BLo;
7608 end if;
7609
7610 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7611 THi := Expr_Value (Type_High_Bound (Typ));
7612 else
7613 THi := BHi;
7614 end if;
7615
eb66e842 7616 -- Analyze the expression to see if it is a static predicate
84c8f0b8 7617
eb66e842 7618 declare
7619 Ranges : constant RList := Get_RList (Expr);
7620 -- Range list from expression if it is static
84c8f0b8 7621
eb66e842 7622 Plist : List_Id;
84c8f0b8 7623
eb66e842 7624 begin
7625 -- Convert range list into a form for the static predicate. In the
7626 -- Ranges array, we just have raw ranges, these must be converted
7627 -- to properly typed and analyzed static expressions or range nodes.
84c8f0b8 7628
eb66e842 7629 -- Note: here we limit ranges to the ranges of the subtype, so that
7630 -- a predicate is always false for values outside the subtype. That
7631 -- seems fine, such values are invalid anyway, and considering them
7632 -- to fail the predicate seems allowed and friendly, and furthermore
7633 -- simplifies processing for case statements and loops.
84c8f0b8 7634
eb66e842 7635 Plist := New_List;
7636
7637 for J in Ranges'Range loop
84c8f0b8 7638 declare
eb66e842 7639 Lo : Uint := Ranges (J).Lo;
7640 Hi : Uint := Ranges (J).Hi;
84c8f0b8 7641
eb66e842 7642 begin
7643 -- Ignore completely out of range entry
84c8f0b8 7644
eb66e842 7645 if Hi < TLo or else Lo > THi then
7646 null;
84c8f0b8 7647
eb66e842 7648 -- Otherwise process entry
84c8f0b8 7649
eb66e842 7650 else
7651 -- Adjust out of range value to subtype range
490beba6 7652
eb66e842 7653 if Lo < TLo then
7654 Lo := TLo;
7655 end if;
490beba6 7656
eb66e842 7657 if Hi > THi then
7658 Hi := THi;
7659 end if;
84c8f0b8 7660
eb66e842 7661 -- Convert range into required form
84c8f0b8 7662
eb66e842 7663 Append_To (Plist, Build_Range (Lo, Hi));
84c8f0b8 7664 end if;
eb66e842 7665 end;
7666 end loop;
84c8f0b8 7667
eb66e842 7668 -- Processing was successful and all entries were static, so now we
7669 -- can store the result as the predicate list.
84c8f0b8 7670
5c6a5792 7671 Set_Static_Discrete_Predicate (Typ, Plist);
84c8f0b8 7672
eb66e842 7673 -- The processing for static predicates put the expression into
7674 -- canonical form as a series of ranges. It also eliminated
7675 -- duplicates and collapsed and combined ranges. We might as well
7676 -- replace the alternatives list of the right operand of the
7677 -- membership test with the static predicate list, which will
7678 -- usually be more efficient.
84c8f0b8 7679
eb66e842 7680 declare
7681 New_Alts : constant List_Id := New_List;
7682 Old_Node : Node_Id;
7683 New_Node : Node_Id;
84c8f0b8 7684
eb66e842 7685 begin
7686 Old_Node := First (Plist);
7687 while Present (Old_Node) loop
7688 New_Node := New_Copy (Old_Node);
84c8f0b8 7689
eb66e842 7690 if Nkind (New_Node) = N_Range then
7691 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7692 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7693 end if;
84c8f0b8 7694
eb66e842 7695 Append_To (New_Alts, New_Node);
7696 Next (Old_Node);
7697 end loop;
84c8f0b8 7698
eb66e842 7699 -- If empty list, replace by False
84c8f0b8 7700
eb66e842 7701 if Is_Empty_List (New_Alts) then
7702 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
84c8f0b8 7703
eb66e842 7704 -- Else replace by set membership test
84c8f0b8 7705
eb66e842 7706 else
7707 Rewrite (Expr,
7708 Make_In (Loc,
7709 Left_Opnd => Make_Identifier (Loc, Nam),
7710 Right_Opnd => Empty,
7711 Alternatives => New_Alts));
490beba6 7712
eb66e842 7713 -- Resolve new expression in function context
490beba6 7714
eb66e842 7715 Install_Formals (Predicate_Function (Typ));
7716 Push_Scope (Predicate_Function (Typ));
7717 Analyze_And_Resolve (Expr, Standard_Boolean);
7718 Pop_Scope;
7719 end if;
7720 end;
7721 end;
9ab32fe9 7722
eb66e842 7723 -- If non-static, return doing nothing
9ab32fe9 7724
eb66e842 7725 exception
7726 when Non_Static =>
7727 return;
7728 end Build_Discrete_Static_Predicate;
64cc9e5d 7729
eb66e842 7730 -------------------------------------------
7731 -- Build_Invariant_Procedure_Declaration --
7732 -------------------------------------------
9ab32fe9 7733
eb66e842 7734 function Build_Invariant_Procedure_Declaration
7735 (Typ : Entity_Id) return Node_Id
7736 is
7737 Loc : constant Source_Ptr := Sloc (Typ);
7738 Object_Entity : constant Entity_Id :=
7739 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7740 Spec : Node_Id;
7741 SId : Entity_Id;
9ab32fe9 7742
eb66e842 7743 begin
7744 Set_Etype (Object_Entity, Typ);
7745
7746 -- Check for duplicate definiations.
7747
7748 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7749 return Empty;
d97beb2f 7750 end if;
d97beb2f 7751
eb66e842 7752 SId :=
7753 Make_Defining_Identifier (Loc,
7754 Chars => New_External_Name (Chars (Typ), "Invariant"));
7755 Set_Has_Invariants (Typ);
7756 Set_Ekind (SId, E_Procedure);
856a9917 7757 Set_Etype (SId, Standard_Void_Type);
eb66e842 7758 Set_Is_Invariant_Procedure (SId);
7759 Set_Invariant_Procedure (Typ, SId);
d97beb2f 7760
eb66e842 7761 Spec :=
7762 Make_Procedure_Specification (Loc,
7763 Defining_Unit_Name => SId,
7764 Parameter_Specifications => New_List (
7765 Make_Parameter_Specification (Loc,
7766 Defining_Identifier => Object_Entity,
7767 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
d97beb2f 7768
eb66e842 7769 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7770 end Build_Invariant_Procedure_Declaration;
7771
7772 -------------------------------
7773 -- Build_Invariant_Procedure --
7774 -------------------------------
7775
7776 -- The procedure that is constructed here has the form
7777
7778 -- procedure typInvariant (Ixxx : typ) is
7779 -- begin
7780 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7781 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7782 -- ...
7783 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7784 -- ...
7785 -- end typInvariant;
7786
7787 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7788 Loc : constant Source_Ptr := Sloc (Typ);
7789 Stmts : List_Id;
7790 Spec : Node_Id;
7791 SId : Entity_Id;
7792 PDecl : Node_Id;
7793 PBody : Node_Id;
d97beb2f 7794
eb66e842 7795 Nam : Name_Id;
7796 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7797 -- if we come from a Type_Invariant aspect, we make sure to build the
7798 -- Check pragma with the right name, so that Check_Policy works right.
d7c2851f 7799
eb66e842 7800 Visible_Decls : constant List_Id := Visible_Declarations (N);
7801 Private_Decls : constant List_Id := Private_Declarations (N);
d7c2851f 7802
eb66e842 7803 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7804 -- Appends statements to Stmts for any invariants in the rep item chain
7805 -- of the given type. If Inherit is False, then we only process entries
7806 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7807 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7808 -- "inherited" to the exception message and generating an informational
7809 -- message about the inheritance of an invariant.
d97beb2f 7810
eb66e842 7811 Object_Name : Name_Id;
7812 -- Name for argument of invariant procedure
d97beb2f 7813
eb66e842 7814 Object_Entity : Node_Id;
7815 -- The entity of the formal for the procedure
d97beb2f 7816
eb66e842 7817 --------------------
7818 -- Add_Invariants --
7819 --------------------
d97beb2f 7820
eb66e842 7821 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7822 Ritem : Node_Id;
7823 Arg1 : Node_Id;
7824 Arg2 : Node_Id;
7825 Arg3 : Node_Id;
7826 Exp : Node_Id;
7827 Loc : Source_Ptr;
7828 Assoc : List_Id;
7829 Str : String_Id;
d97beb2f 7830
eb66e842 7831 procedure Replace_Type_Reference (N : Node_Id);
7832 -- Replace a single occurrence N of the subtype name with a reference
7833 -- to the formal of the predicate function. N can be an identifier
7834 -- referencing the subtype, or a selected component, representing an
7835 -- appropriately qualified occurrence of the subtype name.
d97beb2f 7836
eb66e842 7837 procedure Replace_Type_References is
7838 new Replace_Type_References_Generic (Replace_Type_Reference);
7839 -- Traverse an expression replacing all occurrences of the subtype
7840 -- name with appropriate references to the object that is the formal
7841 -- parameter of the predicate function. Note that we must ensure
7842 -- that the type and entity information is properly set in the
7843 -- replacement node, since we will do a Preanalyze call of this
7844 -- expression without proper visibility of the procedure argument.
d97beb2f 7845
eb66e842 7846 ----------------------------
7847 -- Replace_Type_Reference --
7848 ----------------------------
d97beb2f 7849
eb66e842 7850 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7851 -- regarding handling of Sloc and Comes_From_Source.
d97beb2f 7852
eb66e842 7853 procedure Replace_Type_Reference (N : Node_Id) is
7854 begin
d97beb2f 7855
eb66e842 7856 -- Add semantic information to node to be rewritten, for ASIS
7857 -- navigation needs.
d97beb2f 7858
eb66e842 7859 if Nkind (N) = N_Identifier then
7860 Set_Entity (N, T);
7861 Set_Etype (N, T);
d7c2851f 7862
eb66e842 7863 elsif Nkind (N) = N_Selected_Component then
7864 Analyze (Prefix (N));
7865 Set_Entity (Selector_Name (N), T);
7866 Set_Etype (Selector_Name (N), T);
7867 end if;
d7c2851f 7868
eb66e842 7869 -- Invariant'Class, replace with T'Class (obj)
69004fe6 7870 -- In ASIS mode, an inherited item is analyzed already, and the
7871 -- replacement has been done, so do not repeat transformation
7872 -- to prevent ill-formed tree.
d97beb2f 7873
eb66e842 7874 if Class_Present (Ritem) then
69004fe6 7875 if ASIS_Mode
7876 and then Nkind (Parent (N)) = N_Attribute_Reference
7877 and then Attribute_Name (Parent (N)) = Name_Class
7878 then
7879 null;
7880
7881 else
7882 Rewrite (N,
7883 Make_Type_Conversion (Sloc (N),
7884 Subtype_Mark =>
7885 Make_Attribute_Reference (Sloc (N),
7886 Prefix => New_Occurrence_Of (T, Sloc (N)),
7887 Attribute_Name => Name_Class),
7888 Expression =>
7889 Make_Identifier (Sloc (N), Object_Name)));
7890
7891 Set_Entity (Expression (N), Object_Entity);
7892 Set_Etype (Expression (N), Typ);
7893 end if;
d9f6a4ee 7894
eb66e842 7895 -- Invariant, replace with obj
d9f6a4ee 7896
eb66e842 7897 else
7898 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7899 Set_Entity (N, Object_Entity);
7900 Set_Etype (N, Typ);
7901 end if;
d9f6a4ee 7902
eb66e842 7903 Set_Comes_From_Source (N, True);
7904 end Replace_Type_Reference;
d9f6a4ee 7905
eb66e842 7906 -- Start of processing for Add_Invariants
d9f6a4ee 7907
eb66e842 7908 begin
7909 Ritem := First_Rep_Item (T);
7910 while Present (Ritem) loop
7911 if Nkind (Ritem) = N_Pragma
7912 and then Pragma_Name (Ritem) = Name_Invariant
7913 then
7914 Arg1 := First (Pragma_Argument_Associations (Ritem));
7915 Arg2 := Next (Arg1);
7916 Arg3 := Next (Arg2);
d9f6a4ee 7917
eb66e842 7918 Arg1 := Get_Pragma_Arg (Arg1);
7919 Arg2 := Get_Pragma_Arg (Arg2);
d9f6a4ee 7920
eb66e842 7921 -- For Inherit case, ignore Invariant, process only Class case
d9f6a4ee 7922
eb66e842 7923 if Inherit then
7924 if not Class_Present (Ritem) then
7925 goto Continue;
7926 end if;
d9f6a4ee 7927
eb66e842 7928 -- For Inherit false, process only item for right type
d9f6a4ee 7929
eb66e842 7930 else
7931 if Entity (Arg1) /= Typ then
7932 goto Continue;
7933 end if;
7934 end if;
d9f6a4ee 7935
eb66e842 7936 if No (Stmts) then
7937 Stmts := Empty_List;
7938 end if;
d9f6a4ee 7939
eb66e842 7940 Exp := New_Copy_Tree (Arg2);
d9f6a4ee 7941
eb66e842 7942 -- Preserve sloc of original pragma Invariant
d9f6a4ee 7943
eb66e842 7944 Loc := Sloc (Ritem);
d9f6a4ee 7945
eb66e842 7946 -- We need to replace any occurrences of the name of the type
7947 -- with references to the object, converted to type'Class in
7948 -- the case of Invariant'Class aspects.
d9f6a4ee 7949
37c6552c 7950 Replace_Type_References (Exp, T);
d9f6a4ee 7951
eb66e842 7952 -- If this invariant comes from an aspect, find the aspect
7953 -- specification, and replace the saved expression because
7954 -- we need the subtype references replaced for the calls to
7955 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7956 -- and Check_Aspect_At_End_Of_Declarations.
d9f6a4ee 7957
eb66e842 7958 if From_Aspect_Specification (Ritem) then
7959 declare
7960 Aitem : Node_Id;
d9f6a4ee 7961
eb66e842 7962 begin
7963 -- Loop to find corresponding aspect, note that this
7964 -- must be present given the pragma is marked delayed.
d9f6a4ee 7965
eb66e842 7966 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7967 -- this loop does nothing. Furthermore, why isn't this
7968 -- simply Corresponding_Aspect ???
d9f6a4ee 7969
eb66e842 7970 Aitem := Next_Rep_Item (Ritem);
7971 while Present (Aitem) loop
7972 if Nkind (Aitem) = N_Aspect_Specification
7973 and then Aspect_Rep_Item (Aitem) = Ritem
7974 then
7975 Set_Entity
7976 (Identifier (Aitem), New_Copy_Tree (Exp));
7977 exit;
7978 end if;
d9f6a4ee 7979
eb66e842 7980 Aitem := Next_Rep_Item (Aitem);
7981 end loop;
7982 end;
7983 end if;
d9f6a4ee 7984
eb66e842 7985 -- Now we need to preanalyze the expression to properly capture
7986 -- the visibility in the visible part. The expression will not
7987 -- be analyzed for real until the body is analyzed, but that is
7988 -- at the end of the private part and has the wrong visibility.
d9f6a4ee 7989
eb66e842 7990 Set_Parent (Exp, N);
31831d39 7991 Preanalyze_Assert_Expression (Exp, Any_Boolean);
d9f6a4ee 7992
f02a9a9a 7993 -- A class-wide invariant may be inherited in a separate unit,
7994 -- where the corresponding expression cannot be resolved by
7995 -- visibility, because it refers to a local function. Propagate
7996 -- semantic information to the original representation item, to
7997 -- be used when an invariant procedure for a derived type is
7998 -- constructed.
7999
8000 -- Unclear how to handle class-wide invariants that are not
8001 -- function calls ???
8002
8003 if not Inherit
8004 and then Class_Present (Ritem)
8005 and then Nkind (Exp) = N_Function_Call
8006 and then Nkind (Arg2) = N_Indexed_Component
8007 then
8008 Rewrite (Arg2,
8009 Make_Function_Call (Loc,
8010 Name =>
8011 New_Occurrence_Of (Entity (Name (Exp)), Loc),
8012 Parameter_Associations =>
8013 New_Copy_List (Expressions (Arg2))));
8014 end if;
8015
eb66e842 8016 -- In ASIS mode, even if assertions are not enabled, we must
8017 -- analyze the original expression in the aspect specification
8018 -- because it is part of the original tree.
d9f6a4ee 8019
eb66e842 8020 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
8021 declare
8022 Inv : constant Node_Id :=
8023 Expression (Corresponding_Aspect (Ritem));
8024 begin
37c6552c 8025 Replace_Type_References (Inv, T);
eb66e842 8026 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
8027 end;
8028 end if;
d9f6a4ee 8029
eb66e842 8030 -- Get name to be used for Check pragma
d9f6a4ee 8031
eb66e842 8032 if not From_Aspect_Specification (Ritem) then
8033 Nam := Name_Invariant;
8034 else
8035 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
8036 end if;
d9f6a4ee 8037
eb66e842 8038 -- Build first two arguments for Check pragma
d9f6a4ee 8039
eb66e842 8040 Assoc :=
8041 New_List (
8042 Make_Pragma_Argument_Association (Loc,
8043 Expression => Make_Identifier (Loc, Chars => Nam)),
8044 Make_Pragma_Argument_Association (Loc,
8045 Expression => Exp));
d9f6a4ee 8046
eb66e842 8047 -- Add message if present in Invariant pragma
d9f6a4ee 8048
eb66e842 8049 if Present (Arg3) then
8050 Str := Strval (Get_Pragma_Arg (Arg3));
d9f6a4ee 8051
eb66e842 8052 -- If inherited case, and message starts "failed invariant",
8053 -- change it to be "failed inherited invariant".
d9f6a4ee 8054
eb66e842 8055 if Inherit then
8056 String_To_Name_Buffer (Str);
d9f6a4ee 8057
eb66e842 8058 if Name_Buffer (1 .. 16) = "failed invariant" then
8059 Insert_Str_In_Name_Buffer ("inherited ", 8);
8060 Str := String_From_Name_Buffer;
8061 end if;
8062 end if;
d9f6a4ee 8063
eb66e842 8064 Append_To (Assoc,
8065 Make_Pragma_Argument_Association (Loc,
8066 Expression => Make_String_Literal (Loc, Str)));
8067 end if;
d9f6a4ee 8068
eb66e842 8069 -- Add Check pragma to list of statements
d97beb2f 8070
eb66e842 8071 Append_To (Stmts,
8072 Make_Pragma (Loc,
8073 Pragma_Identifier =>
8074 Make_Identifier (Loc, Name_Check),
8075 Pragma_Argument_Associations => Assoc));
d97beb2f 8076
eb66e842 8077 -- If Inherited case and option enabled, output info msg. Note
8078 -- that we know this is a case of Invariant'Class.
d97beb2f 8079
eb66e842 8080 if Inherit and Opt.List_Inherited_Aspects then
8081 Error_Msg_Sloc := Sloc (Ritem);
8082 Error_Msg_N
8083 ("info: & inherits `Invariant''Class` aspect from #?L?",
8084 Typ);
8085 end if;
d9f6a4ee 8086 end if;
d97beb2f 8087
eb66e842 8088 <<Continue>>
8089 Next_Rep_Item (Ritem);
8090 end loop;
8091 end Add_Invariants;
d97beb2f 8092
eb66e842 8093 -- Start of processing for Build_Invariant_Procedure
d97beb2f 8094
eb66e842 8095 begin
8096 Stmts := No_List;
8097 PDecl := Empty;
8098 PBody := Empty;
8099 SId := Empty;
d97beb2f 8100
eb66e842 8101 -- If the aspect specification exists for some view of the type, the
8102 -- declaration for the procedure has been created.
d97beb2f 8103
eb66e842 8104 if Has_Invariants (Typ) then
8105 SId := Invariant_Procedure (Typ);
8106 end if;
9dc88aea 8107
0c2bde47 8108 -- If the body is already present, nothing to do. This will occur when
8109 -- the type is already frozen, which is the case when the invariant
8110 -- appears in a private part, and the freezing takes place before the
8111 -- final pass over full declarations.
aba11c12 8112
8113 -- See Exp_Ch3.Insert_Component_Invariant_Checks for details.
0c2bde47 8114
eb66e842 8115 if Present (SId) then
8116 PDecl := Unit_Declaration_Node (SId);
0c2bde47 8117
8118 if Present (PDecl)
8119 and then Nkind (PDecl) = N_Subprogram_Declaration
8120 and then Present (Corresponding_Body (PDecl))
8121 then
8122 return;
8123 end if;
8124
eb66e842 8125 else
8126 PDecl := Build_Invariant_Procedure_Declaration (Typ);
8127 end if;
9dc88aea 8128
eb66e842 8129 -- Recover formal of procedure, for use in the calls to invariant
8130 -- functions (including inherited ones).
d9f6a4ee 8131
eb66e842 8132 Object_Entity :=
8133 Defining_Identifier
8134 (First (Parameter_Specifications (Specification (PDecl))));
8135 Object_Name := Chars (Object_Entity);
d9f6a4ee 8136
eb66e842 8137 -- Add invariants for the current type
9dc88aea 8138
eb66e842 8139 Add_Invariants (Typ, Inherit => False);
9dc88aea 8140
eb66e842 8141 -- Add invariants for parent types
9dc88aea 8142
eb66e842 8143 declare
8144 Current_Typ : Entity_Id;
8145 Parent_Typ : Entity_Id;
9dc88aea 8146
eb66e842 8147 begin
8148 Current_Typ := Typ;
d97beb2f 8149 loop
eb66e842 8150 Parent_Typ := Etype (Current_Typ);
9dc88aea 8151
eb66e842 8152 if Is_Private_Type (Parent_Typ)
8153 and then Present (Full_View (Base_Type (Parent_Typ)))
d9f6a4ee 8154 then
eb66e842 8155 Parent_Typ := Full_View (Base_Type (Parent_Typ));
8156 end if;
9dc88aea 8157
eb66e842 8158 exit when Parent_Typ = Current_Typ;
9dc88aea 8159
eb66e842 8160 Current_Typ := Parent_Typ;
8161 Add_Invariants (Current_Typ, Inherit => True);
8162 end loop;
8163 end;
ad274a73 8164
8165 -- Add invariants of progenitors
8166
8167 if Is_Tagged_Type (Typ) and then not Is_Interface (Typ) then
8168 declare
8169 Ifaces_List : Elist_Id;
8170 AI : Elmt_Id;
8171 Iface : Entity_Id;
8172
8173 begin
8174 Collect_Interfaces (Typ, Ifaces_List);
8175
8176 AI := First_Elmt (Ifaces_List);
8177 while Present (AI) loop
8178 Iface := Node (AI);
8179
8180 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8181 Add_Invariants (Iface, Inherit => True);
8182 end if;
8183
8184 Next_Elmt (AI);
8185 end loop;
8186 end;
8187 end if;
9dc88aea 8188
eb66e842 8189 -- Build the procedure if we generated at least one Check pragma
9dc88aea 8190
eb66e842 8191 if Stmts /= No_List then
8192 Spec := Copy_Separate_Tree (Specification (PDecl));
9dc88aea 8193
eb66e842 8194 PBody :=
8195 Make_Subprogram_Body (Loc,
8196 Specification => Spec,
8197 Declarations => Empty_List,
8198 Handled_Statement_Sequence =>
8199 Make_Handled_Sequence_Of_Statements (Loc,
8200 Statements => Stmts));
9dc88aea 8201
eb66e842 8202 -- Insert procedure declaration and spec at the appropriate points.
8203 -- If declaration is already analyzed, it was processed by the
8204 -- generated pragma.
9dc88aea 8205
eb66e842 8206 if Present (Private_Decls) then
d97beb2f 8207
eb66e842 8208 -- The spec goes at the end of visible declarations, but they have
8209 -- already been analyzed, so we need to explicitly do the analyze.
d9f6a4ee 8210
eb66e842 8211 if not Analyzed (PDecl) then
8212 Append_To (Visible_Decls, PDecl);
8213 Analyze (PDecl);
8214 end if;
d9f6a4ee 8215
eb66e842 8216 -- The body goes at the end of the private declarations, which we
8217 -- have not analyzed yet, so we do not need to perform an explicit
8218 -- analyze call. We skip this if there are no private declarations
8219 -- (this is an error that will be caught elsewhere);
d9f6a4ee 8220
eb66e842 8221 Append_To (Private_Decls, PBody);
d9f6a4ee 8222
eb66e842 8223 -- If the invariant appears on the full view of a type, the
8224 -- analysis of the private part is complete, and we must
8225 -- analyze the new body explicitly.
d9f6a4ee 8226
eb66e842 8227 if In_Private_Part (Current_Scope) then
8228 Analyze (PBody);
8229 end if;
d97beb2f 8230
eb66e842 8231 -- If there are no private declarations this may be an error that
8232 -- will be diagnosed elsewhere. However, if this is a non-private
8233 -- type that inherits invariants, it needs no completion and there
8234 -- may be no private part. In this case insert invariant procedure
8235 -- at end of current declarative list, and analyze at once, given
8236 -- that the type is about to be frozen.
d97beb2f 8237
eb66e842 8238 elsif not Is_Private_Type (Typ) then
8239 Append_To (Visible_Decls, PDecl);
8240 Append_To (Visible_Decls, PBody);
8241 Analyze (PDecl);
8242 Analyze (PBody);
8243 end if;
8244 end if;
8245 end Build_Invariant_Procedure;
d9f6a4ee 8246
eb66e842 8247 -------------------------------
8248 -- Build_Predicate_Functions --
8249 -------------------------------
d9f6a4ee 8250
eb66e842 8251 -- The procedures that are constructed here have the form:
d9f6a4ee 8252
eb66e842 8253 -- function typPredicate (Ixxx : typ) return Boolean is
8254 -- begin
8255 -- return
8256 -- exp1 and then exp2 and then ...
8257 -- and then typ1Predicate (typ1 (Ixxx))
8258 -- and then typ2Predicate (typ2 (Ixxx))
8259 -- and then ...;
8260 -- end typPredicate;
d9f6a4ee 8261
eb66e842 8262 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8263 -- this is the point at which these expressions get analyzed, providing the
8264 -- required delay, and typ1, typ2, are entities from which predicates are
8265 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8266 -- use this function even if checks are off, e.g. for membership tests.
d9f6a4ee 8267
eb66e842 8268 -- If the expression has at least one Raise_Expression, then we also build
8269 -- the typPredicateM version of the function, in which any occurrence of a
8270 -- Raise_Expression is converted to "return False".
d9f6a4ee 8271
eb66e842 8272 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8273 Loc : constant Source_Ptr := Sloc (Typ);
d9f6a4ee 8274
eb66e842 8275 Expr : Node_Id;
8276 -- This is the expression for the result of the function. It is
8277 -- is build by connecting the component predicates with AND THEN.
d9f6a4ee 8278
eb66e842 8279 Expr_M : Node_Id;
8280 -- This is the corresponding return expression for the Predicate_M
8281 -- function. It differs in that raise expressions are marked for
8282 -- special expansion (see Process_REs).
d9f6a4ee 8283
eb66e842 8284 Object_Name : constant Name_Id := New_Internal_Name ('I');
8285 -- Name for argument of Predicate procedure. Note that we use the same
499918a7 8286 -- name for both predicate functions. That way the reference within the
eb66e842 8287 -- predicate expression is the same in both functions.
d9f6a4ee 8288
eb66e842 8289 Object_Entity : constant Entity_Id :=
8290 Make_Defining_Identifier (Loc, Chars => Object_Name);
8291 -- Entity for argument of Predicate procedure
d9f6a4ee 8292
eb66e842 8293 Object_Entity_M : constant Entity_Id :=
8294 Make_Defining_Identifier (Loc, Chars => Object_Name);
8295 -- Entity for argument of Predicate_M procedure
d9f6a4ee 8296
eb66e842 8297 Raise_Expression_Present : Boolean := False;
8298 -- Set True if Expr has at least one Raise_Expression
d9f6a4ee 8299
eb66e842 8300 procedure Add_Call (T : Entity_Id);
8301 -- Includes a call to the predicate function for type T in Expr if T
8302 -- has predicates and Predicate_Function (T) is non-empty.
d9f6a4ee 8303
eb66e842 8304 procedure Add_Predicates;
8305 -- Appends expressions for any Predicate pragmas in the rep item chain
8306 -- Typ to Expr. Note that we look only at items for this exact entity.
8307 -- Inheritance of predicates for the parent type is done by calling the
8308 -- Predicate_Function of the parent type, using Add_Call above.
d9f6a4ee 8309
eb66e842 8310 function Test_RE (N : Node_Id) return Traverse_Result;
8311 -- Used in Test_REs, tests one node for being a raise expression, and if
8312 -- so sets Raise_Expression_Present True.
d9f6a4ee 8313
eb66e842 8314 procedure Test_REs is new Traverse_Proc (Test_RE);
8315 -- Tests to see if Expr contains any raise expressions
d9f6a4ee 8316
eb66e842 8317 function Process_RE (N : Node_Id) return Traverse_Result;
8318 -- Used in Process REs, tests if node N is a raise expression, and if
8319 -- so, marks it to be converted to return False.
d9f6a4ee 8320
eb66e842 8321 procedure Process_REs is new Traverse_Proc (Process_RE);
8322 -- Marks any raise expressions in Expr_M to return False
d9f6a4ee 8323
eb66e842 8324 --------------
8325 -- Add_Call --
8326 --------------
d9f6a4ee 8327
eb66e842 8328 procedure Add_Call (T : Entity_Id) is
8329 Exp : Node_Id;
d9f6a4ee 8330
eb66e842 8331 begin
8332 if Present (T) and then Present (Predicate_Function (T)) then
8333 Set_Has_Predicates (Typ);
d9f6a4ee 8334
eb66e842 8335 -- Build the call to the predicate function of T
d9f6a4ee 8336
eb66e842 8337 Exp :=
8338 Make_Predicate_Call
8339 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
d9f6a4ee 8340
eb66e842 8341 -- Add call to evolving expression, using AND THEN if needed
d9f6a4ee 8342
eb66e842 8343 if No (Expr) then
8344 Expr := Exp;
3b23aaa0 8345
eb66e842 8346 else
8347 Expr :=
3b23aaa0 8348 Make_And_Then (Sloc (Expr),
eb66e842 8349 Left_Opnd => Relocate_Node (Expr),
8350 Right_Opnd => Exp);
8351 end if;
d9f6a4ee 8352
eb66e842 8353 -- Output info message on inheritance if required. Note we do not
8354 -- give this information for generic actual types, since it is
8355 -- unwelcome noise in that case in instantiations. We also
8356 -- generally suppress the message in instantiations, and also
8357 -- if it involves internal names.
d9f6a4ee 8358
eb66e842 8359 if Opt.List_Inherited_Aspects
8360 and then not Is_Generic_Actual_Type (Typ)
8361 and then Instantiation_Depth (Sloc (Typ)) = 0
8362 and then not Is_Internal_Name (Chars (T))
8363 and then not Is_Internal_Name (Chars (Typ))
8364 then
8365 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8366 Error_Msg_Node_2 := T;
8367 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8368 end if;
8369 end if;
8370 end Add_Call;
d9f6a4ee 8371
eb66e842 8372 --------------------
8373 -- Add_Predicates --
8374 --------------------
d9f6a4ee 8375
eb66e842 8376 procedure Add_Predicates is
8377 Ritem : Node_Id;
8378 Arg1 : Node_Id;
8379 Arg2 : Node_Id;
d9f6a4ee 8380
eb66e842 8381 procedure Replace_Type_Reference (N : Node_Id);
8382 -- Replace a single occurrence N of the subtype name with a reference
8383 -- to the formal of the predicate function. N can be an identifier
8384 -- referencing the subtype, or a selected component, representing an
8385 -- appropriately qualified occurrence of the subtype name.
d9f6a4ee 8386
eb66e842 8387 procedure Replace_Type_References is
8388 new Replace_Type_References_Generic (Replace_Type_Reference);
8389 -- Traverse an expression changing every occurrence of an identifier
8390 -- whose name matches the name of the subtype with a reference to
8391 -- the formal parameter of the predicate function.
d9f6a4ee 8392
eb66e842 8393 ----------------------------
8394 -- Replace_Type_Reference --
8395 ----------------------------
d9f6a4ee 8396
eb66e842 8397 procedure Replace_Type_Reference (N : Node_Id) is
8398 begin
8399 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8400 -- Use the Sloc of the usage name, not the defining name
d97beb2f 8401
eb66e842 8402 Set_Etype (N, Typ);
8403 Set_Entity (N, Object_Entity);
d97beb2f 8404
eb66e842 8405 -- We want to treat the node as if it comes from source, so that
8406 -- ASIS will not ignore it
d97beb2f 8407
eb66e842 8408 Set_Comes_From_Source (N, True);
8409 end Replace_Type_Reference;
d97beb2f 8410
eb66e842 8411 -- Start of processing for Add_Predicates
d97beb2f 8412
eb66e842 8413 begin
8414 Ritem := First_Rep_Item (Typ);
8415 while Present (Ritem) loop
8416 if Nkind (Ritem) = N_Pragma
8417 and then Pragma_Name (Ritem) = Name_Predicate
8418 then
eb66e842 8419 -- Acquire arguments
d97beb2f 8420
eb66e842 8421 Arg1 := First (Pragma_Argument_Associations (Ritem));
8422 Arg2 := Next (Arg1);
d97beb2f 8423
eb66e842 8424 Arg1 := Get_Pragma_Arg (Arg1);
8425 Arg2 := Get_Pragma_Arg (Arg2);
d97beb2f 8426
eb66e842 8427 -- See if this predicate pragma is for the current type or for
8428 -- its full view. A predicate on a private completion is placed
8429 -- on the partial view beause this is the visible entity that
8430 -- is frozen.
639c3741 8431
eb66e842 8432 if Entity (Arg1) = Typ
8433 or else Full_View (Entity (Arg1)) = Typ
639c3741 8434 then
eb66e842 8435 -- We have a match, this entry is for our subtype
639c3741 8436
eb66e842 8437 -- We need to replace any occurrences of the name of the
8438 -- type with references to the object.
639c3741 8439
37c6552c 8440 Replace_Type_References (Arg2, Typ);
639c3741 8441
eb66e842 8442 -- If this predicate comes from an aspect, find the aspect
8443 -- specification, and replace the saved expression because
8444 -- we need the subtype references replaced for the calls to
8445 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
8446 -- and Check_Aspect_At_End_Of_Declarations.
639c3741 8447
eb66e842 8448 if From_Aspect_Specification (Ritem) then
8449 declare
8450 Aitem : Node_Id;
639c3741 8451
eb66e842 8452 begin
8453 -- Loop to find corresponding aspect, note that this
8454 -- must be present given the pragma is marked delayed.
639c3741 8455
eb66e842 8456 Aitem := Next_Rep_Item (Ritem);
8457 loop
8458 if Nkind (Aitem) = N_Aspect_Specification
8459 and then Aspect_Rep_Item (Aitem) = Ritem
8460 then
8461 Set_Entity
8462 (Identifier (Aitem), New_Copy_Tree (Arg2));
8463 exit;
8464 end if;
639c3741 8465
eb66e842 8466 Aitem := Next_Rep_Item (Aitem);
8467 end loop;
8468 end;
8469 end if;
737e8460 8470
eb66e842 8471 -- Now we can add the expression
737e8460 8472
eb66e842 8473 if No (Expr) then
8474 Expr := Relocate_Node (Arg2);
d97beb2f 8475
eb66e842 8476 -- There already was a predicate, so add to it
d97beb2f 8477
eb66e842 8478 else
8479 Expr :=
8480 Make_And_Then (Loc,
8481 Left_Opnd => Relocate_Node (Expr),
8482 Right_Opnd => Relocate_Node (Arg2));
8483 end if;
8484 end if;
8485 end if;
d97beb2f 8486
eb66e842 8487 Next_Rep_Item (Ritem);
8488 end loop;
8489 end Add_Predicates;
d97beb2f 8490
eb66e842 8491 ----------------
8492 -- Process_RE --
8493 ----------------
d97beb2f 8494
eb66e842 8495 function Process_RE (N : Node_Id) return Traverse_Result is
d9f6a4ee 8496 begin
eb66e842 8497 if Nkind (N) = N_Raise_Expression then
8498 Set_Convert_To_Return_False (N);
8499 return Skip;
d9f6a4ee 8500 else
eb66e842 8501 return OK;
d9f6a4ee 8502 end if;
eb66e842 8503 end Process_RE;
d7c2851f 8504
d9f6a4ee 8505 -------------
eb66e842 8506 -- Test_RE --
d9f6a4ee 8507 -------------
d7c2851f 8508
eb66e842 8509 function Test_RE (N : Node_Id) return Traverse_Result is
d97beb2f 8510 begin
eb66e842 8511 if Nkind (N) = N_Raise_Expression then
8512 Raise_Expression_Present := True;
8513 return Abandon;
8514 else
8515 return OK;
8516 end if;
8517 end Test_RE;
d97beb2f 8518
eb66e842 8519 -- Start of processing for Build_Predicate_Functions
d97beb2f 8520
eb66e842 8521 begin
8522 -- Return if already built or if type does not have predicates
9dc88aea 8523
eb66e842 8524 if not Has_Predicates (Typ)
8525 or else Present (Predicate_Function (Typ))
8526 then
8527 return;
8528 end if;
d9f6a4ee 8529
eb66e842 8530 -- Prepare to construct predicate expression
d97beb2f 8531
eb66e842 8532 Expr := Empty;
d97beb2f 8533
eb66e842 8534 -- Add Predicates for the current type
d97beb2f 8535
eb66e842 8536 Add_Predicates;
d97beb2f 8537
eb66e842 8538 -- Add predicates for ancestor if present
d97beb2f 8539
eb66e842 8540 declare
8541 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
d9f6a4ee 8542 begin
eb66e842 8543 if Present (Atyp) then
8544 Add_Call (Atyp);
8545 end if;
8546 end;
9dc88aea 8547
eb66e842 8548 -- Case where predicates are present
9dc88aea 8549
eb66e842 8550 if Present (Expr) then
726fd56a 8551
eb66e842 8552 -- Test for raise expression present
726fd56a 8553
eb66e842 8554 Test_REs (Expr);
9dc88aea 8555
eb66e842 8556 -- If raise expression is present, capture a copy of Expr for use
8557 -- in building the predicateM function version later on. For this
8558 -- copy we replace references to Object_Entity by Object_Entity_M.
9dc88aea 8559
eb66e842 8560 if Raise_Expression_Present then
8561 declare
299b347e 8562 Map : constant Elist_Id := New_Elmt_List;
8563 New_V : Entity_Id := Empty;
8564
8565 -- The unanalyzed expression will be copied and appear in
8566 -- both functions. Normally expressions do not declare new
8567 -- entities, but quantified expressions do, so we need to
8568 -- create new entities for their bound variables, to prevent
8569 -- multiple definitions in gigi.
8570
8571 function Reset_Loop_Variable (N : Node_Id)
8572 return Traverse_Result;
8573
8574 procedure Collect_Loop_Variables is
8575 new Traverse_Proc (Reset_Loop_Variable);
8576
8577 ------------------------
8578 -- Reset_Loop_Variable --
8579 ------------------------
8580
8581 function Reset_Loop_Variable (N : Node_Id)
8582 return Traverse_Result
8583 is
8584 begin
8585 if Nkind (N) = N_Iterator_Specification then
8586 New_V := Make_Defining_Identifier
8587 (Sloc (N), Chars (Defining_Identifier (N)));
8588
8589 Set_Defining_Identifier (N, New_V);
8590 end if;
8591
8592 return OK;
8593 end Reset_Loop_Variable;
8594
eb66e842 8595 begin
8596 Append_Elmt (Object_Entity, Map);
8597 Append_Elmt (Object_Entity_M, Map);
8598 Expr_M := New_Copy_Tree (Expr, Map => Map);
299b347e 8599 Collect_Loop_Variables (Expr_M);
eb66e842 8600 end;
8601 end if;
d97beb2f 8602
eb66e842 8603 -- Build the main predicate function
9dc88aea 8604
eb66e842 8605 declare
8606 SId : constant Entity_Id :=
8607 Make_Defining_Identifier (Loc,
8608 Chars => New_External_Name (Chars (Typ), "Predicate"));
8609 -- The entity for the the function spec
9dc88aea 8610
eb66e842 8611 SIdB : constant Entity_Id :=
8612 Make_Defining_Identifier (Loc,
8613 Chars => New_External_Name (Chars (Typ), "Predicate"));
8614 -- The entity for the function body
9dc88aea 8615
eb66e842 8616 Spec : Node_Id;
8617 FDecl : Node_Id;
8618 FBody : Node_Id;
9dc88aea 8619
eb66e842 8620 begin
8621 -- Build function declaration
d97beb2f 8622
eb66e842 8623 Set_Ekind (SId, E_Function);
8624 Set_Is_Internal (SId);
8625 Set_Is_Predicate_Function (SId);
8626 Set_Predicate_Function (Typ, SId);
d97beb2f 8627
eb66e842 8628 -- The predicate function is shared between views of a type
d97beb2f 8629
eb66e842 8630 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8631 Set_Predicate_Function (Full_View (Typ), SId);
d97beb2f 8632 end if;
d97beb2f 8633
eb66e842 8634 Spec :=
8635 Make_Function_Specification (Loc,
8636 Defining_Unit_Name => SId,
8637 Parameter_Specifications => New_List (
8638 Make_Parameter_Specification (Loc,
8639 Defining_Identifier => Object_Entity,
8640 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8641 Result_Definition =>
8642 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8643
eb66e842 8644 FDecl :=
8645 Make_Subprogram_Declaration (Loc,
8646 Specification => Spec);
d97beb2f 8647
eb66e842 8648 -- Build function body
d97beb2f 8649
eb66e842 8650 Spec :=
8651 Make_Function_Specification (Loc,
8652 Defining_Unit_Name => SIdB,
8653 Parameter_Specifications => New_List (
8654 Make_Parameter_Specification (Loc,
8655 Defining_Identifier =>
8656 Make_Defining_Identifier (Loc, Object_Name),
8657 Parameter_Type =>
8658 New_Occurrence_Of (Typ, Loc))),
8659 Result_Definition =>
8660 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8661
eb66e842 8662 FBody :=
8663 Make_Subprogram_Body (Loc,
8664 Specification => Spec,
8665 Declarations => Empty_List,
8666 Handled_Statement_Sequence =>
8667 Make_Handled_Sequence_Of_Statements (Loc,
8668 Statements => New_List (
8669 Make_Simple_Return_Statement (Loc,
8670 Expression => Expr))));
9dc88aea 8671
eb66e842 8672 -- Insert declaration before freeze node and body after
d97beb2f 8673
eb66e842 8674 Insert_Before_And_Analyze (N, FDecl);
8675 Insert_After_And_Analyze (N, FBody);
d9f6a4ee 8676 end;
d97beb2f 8677
eb66e842 8678 -- Test for raise expressions present and if so build M version
d97beb2f 8679
eb66e842 8680 if Raise_Expression_Present then
8681 declare
8682 SId : constant Entity_Id :=
8683 Make_Defining_Identifier (Loc,
8684 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8685 -- The entity for the the function spec
d97beb2f 8686
eb66e842 8687 SIdB : constant Entity_Id :=
8688 Make_Defining_Identifier (Loc,
8689 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8690 -- The entity for the function body
b9e61b2a 8691
eb66e842 8692 Spec : Node_Id;
8693 FDecl : Node_Id;
8694 FBody : Node_Id;
8695 BTemp : Entity_Id;
d97beb2f 8696
eb66e842 8697 begin
8698 -- Mark any raise expressions for special expansion
d97beb2f 8699
eb66e842 8700 Process_REs (Expr_M);
d97beb2f 8701
eb66e842 8702 -- Build function declaration
d97beb2f 8703
eb66e842 8704 Set_Ekind (SId, E_Function);
8705 Set_Is_Predicate_Function_M (SId);
8706 Set_Predicate_Function_M (Typ, SId);
d97beb2f 8707
eb66e842 8708 -- The predicate function is shared between views of a type
d97beb2f 8709
eb66e842 8710 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8711 Set_Predicate_Function_M (Full_View (Typ), SId);
8712 end if;
9dc88aea 8713
eb66e842 8714 Spec :=
8715 Make_Function_Specification (Loc,
8716 Defining_Unit_Name => SId,
8717 Parameter_Specifications => New_List (
8718 Make_Parameter_Specification (Loc,
8719 Defining_Identifier => Object_Entity_M,
8720 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8721 Result_Definition =>
8722 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8723
eb66e842 8724 FDecl :=
8725 Make_Subprogram_Declaration (Loc,
8726 Specification => Spec);
9dc88aea 8727
eb66e842 8728 -- Build function body
9dc88aea 8729
eb66e842 8730 Spec :=
8731 Make_Function_Specification (Loc,
8732 Defining_Unit_Name => SIdB,
8733 Parameter_Specifications => New_List (
8734 Make_Parameter_Specification (Loc,
8735 Defining_Identifier =>
8736 Make_Defining_Identifier (Loc, Object_Name),
8737 Parameter_Type =>
8738 New_Occurrence_Of (Typ, Loc))),
8739 Result_Definition =>
8740 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8741
eb66e842 8742 -- Build the body, we declare the boolean expression before
8743 -- doing the return, because we are not really confident of
8744 -- what happens if a return appears within a return.
9dc88aea 8745
eb66e842 8746 BTemp :=
8747 Make_Defining_Identifier (Loc,
8748 Chars => New_Internal_Name ('B'));
9dc88aea 8749
eb66e842 8750 FBody :=
8751 Make_Subprogram_Body (Loc,
8752 Specification => Spec,
9dc88aea 8753
eb66e842 8754 Declarations => New_List (
8755 Make_Object_Declaration (Loc,
8756 Defining_Identifier => BTemp,
8757 Constant_Present => True,
8758 Object_Definition =>
8759 New_Occurrence_Of (Standard_Boolean, Loc),
8760 Expression => Expr_M)),
d97beb2f 8761
eb66e842 8762 Handled_Statement_Sequence =>
8763 Make_Handled_Sequence_Of_Statements (Loc,
8764 Statements => New_List (
8765 Make_Simple_Return_Statement (Loc,
8766 Expression => New_Occurrence_Of (BTemp, Loc)))));
d97beb2f 8767
eb66e842 8768 -- Insert declaration before freeze node and body after
d97beb2f 8769
eb66e842 8770 Insert_Before_And_Analyze (N, FDecl);
8771 Insert_After_And_Analyze (N, FBody);
8772 end;
8773 end if;
9dc88aea 8774
3b23aaa0 8775 -- See if we have a static predicate. Note that the answer may be
8776 -- yes even if we have an explicit Dynamic_Predicate present.
9dc88aea 8777
3b23aaa0 8778 declare
94d896aa 8779 PS : Boolean;
3b23aaa0 8780 EN : Node_Id;
9dc88aea 8781
3b23aaa0 8782 begin
94d896aa 8783 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8784 PS := False;
8785 else
8786 PS := Is_Predicate_Static (Expr, Object_Name);
8787 end if;
8788
a360a0f7 8789 -- Case where we have a predicate-static aspect
9dc88aea 8790
3b23aaa0 8791 if PS then
9dc88aea 8792
3b23aaa0 8793 -- We don't set Has_Static_Predicate_Aspect, since we can have
8794 -- any of the three cases (Predicate, Dynamic_Predicate, or
8795 -- Static_Predicate) generating a predicate with an expression
a360a0f7 8796 -- that is predicate-static. We just indicate that we have a
3b23aaa0 8797 -- predicate that can be treated as static.
d7c2851f 8798
3b23aaa0 8799 Set_Has_Static_Predicate (Typ);
d7c2851f 8800
3b23aaa0 8801 -- For discrete subtype, build the static predicate list
9dc88aea 8802
3b23aaa0 8803 if Is_Discrete_Type (Typ) then
8804 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8805
8806 -- If we don't get a static predicate list, it means that we
8807 -- have a case where this is not possible, most typically in
8808 -- the case where we inherit a dynamic predicate. We do not
8809 -- consider this an error, we just leave the predicate as
8810 -- dynamic. But if we do succeed in building the list, then
8811 -- we mark the predicate as static.
8812
5c6a5792 8813 if No (Static_Discrete_Predicate (Typ)) then
3b23aaa0 8814 Set_Has_Static_Predicate (Typ, False);
8815 end if;
94d896aa 8816
8817 -- For real or string subtype, save predicate expression
8818
8819 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8820 Set_Static_Real_Or_String_Predicate (Typ, Expr);
3b23aaa0 8821 end if;
8822
8823 -- Case of dynamic predicate (expression is not predicate-static)
9dc88aea 8824
eb66e842 8825 else
3b23aaa0 8826 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8827 -- is only set if we have an explicit Dynamic_Predicate aspect
8828 -- given. Here we may simply have a Predicate aspect where the
8829 -- expression happens not to be predicate-static.
8830
8831 -- Emit an error when the predicate is categorized as static
8832 -- but its expression is not predicate-static.
8833
8834 -- First a little fiddling to get a nice location for the
8835 -- message. If the expression is of the form (A and then B),
8836 -- then use the left operand for the Sloc. This avoids getting
a360a0f7 8837 -- confused by a call to a higher-level predicate with a less
3b23aaa0 8838 -- convenient source location.
8839
8840 EN := Expr;
8841 while Nkind (EN) = N_And_Then loop
8842 EN := Left_Opnd (EN);
8843 end loop;
8844
8845 -- Now post appropriate message
8846
8847 if Has_Static_Predicate_Aspect (Typ) then
94d896aa 8848 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
3b23aaa0 8849 Error_Msg_F
26279d91 8850 ("expression is not predicate-static (RM 3.2.4(16-22))",
3b23aaa0 8851 EN);
8852 else
94d896aa 8853 Error_Msg_F
8854 ("static predicate requires scalar or string type", EN);
3b23aaa0 8855 end if;
8856 end if;
eb66e842 8857 end if;
3b23aaa0 8858 end;
eb66e842 8859 end if;
8860 end Build_Predicate_Functions;
9dc88aea 8861
d9f6a4ee 8862 -----------------------------------------
8863 -- Check_Aspect_At_End_Of_Declarations --
8864 -----------------------------------------
9dc88aea 8865
d9f6a4ee 8866 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8867 Ent : constant Entity_Id := Entity (ASN);
8868 Ident : constant Node_Id := Identifier (ASN);
8869 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
d7c2851f 8870
d9f6a4ee 8871 End_Decl_Expr : constant Node_Id := Entity (Ident);
8872 -- Expression to be analyzed at end of declarations
d7c2851f 8873
d9f6a4ee 8874 Freeze_Expr : constant Node_Id := Expression (ASN);
8875 -- Expression from call to Check_Aspect_At_Freeze_Point
d7c2851f 8876
d9f6a4ee 8877 T : constant Entity_Id := Etype (Freeze_Expr);
8878 -- Type required for preanalyze call
d7c2851f 8879
d9f6a4ee 8880 Err : Boolean;
8881 -- Set False if error
9dc88aea 8882
d9f6a4ee 8883 -- On entry to this procedure, Entity (Ident) contains a copy of the
8884 -- original expression from the aspect, saved for this purpose, and
8885 -- but Expression (Ident) is a preanalyzed copy of the expression,
8886 -- preanalyzed just after the freeze point.
9dc88aea 8887
d9f6a4ee 8888 procedure Check_Overloaded_Name;
8889 -- For aspects whose expression is simply a name, this routine checks if
8890 -- the name is overloaded or not. If so, it verifies there is an
8891 -- interpretation that matches the entity obtained at the freeze point,
8892 -- otherwise the compiler complains.
9dc88aea 8893
d9f6a4ee 8894 ---------------------------
8895 -- Check_Overloaded_Name --
8896 ---------------------------
8897
8898 procedure Check_Overloaded_Name is
d97beb2f 8899 begin
d9f6a4ee 8900 if not Is_Overloaded (End_Decl_Expr) then
5ac76cee 8901 Err := not Is_Entity_Name (End_Decl_Expr)
8902 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
d9f6a4ee 8903
d97beb2f 8904 else
d9f6a4ee 8905 Err := True;
9dc88aea 8906
d9f6a4ee 8907 declare
8908 Index : Interp_Index;
8909 It : Interp;
9dc88aea 8910
d9f6a4ee 8911 begin
8912 Get_First_Interp (End_Decl_Expr, Index, It);
8913 while Present (It.Typ) loop
8914 if It.Nam = Entity (Freeze_Expr) then
8915 Err := False;
8916 exit;
8917 end if;
8918
8919 Get_Next_Interp (Index, It);
8920 end loop;
8921 end;
9dc88aea 8922 end if;
d9f6a4ee 8923 end Check_Overloaded_Name;
9dc88aea 8924
d9f6a4ee 8925 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9dc88aea 8926
d9f6a4ee 8927 begin
8928 -- Case of aspects Dimension, Dimension_System and Synchronization
9dc88aea 8929
d9f6a4ee 8930 if A_Id = Aspect_Synchronization then
8931 return;
d97beb2f 8932
d9f6a4ee 8933 -- Case of stream attributes, just have to compare entities. However,
8934 -- the expression is just a name (possibly overloaded), and there may
8935 -- be stream operations declared for unrelated types, so we just need
8936 -- to verify that one of these interpretations is the one available at
8937 -- at the freeze point.
9dc88aea 8938
d9f6a4ee 8939 elsif A_Id = Aspect_Input or else
f02a9a9a 8940 A_Id = Aspect_Output or else
8941 A_Id = Aspect_Read or else
8942 A_Id = Aspect_Write
d9f6a4ee 8943 then
8944 Analyze (End_Decl_Expr);
8945 Check_Overloaded_Name;
9dc88aea 8946
d9f6a4ee 8947 elsif A_Id = Aspect_Variable_Indexing or else
8948 A_Id = Aspect_Constant_Indexing or else
8949 A_Id = Aspect_Default_Iterator or else
8950 A_Id = Aspect_Iterator_Element
8951 then
8952 -- Make type unfrozen before analysis, to prevent spurious errors
8953 -- about late attributes.
9dc88aea 8954
d9f6a4ee 8955 Set_Is_Frozen (Ent, False);
8956 Analyze (End_Decl_Expr);
8957 Set_Is_Frozen (Ent, True);
9dc88aea 8958
d9f6a4ee 8959 -- If the end of declarations comes before any other freeze
8960 -- point, the Freeze_Expr is not analyzed: no check needed.
9dc88aea 8961
d9f6a4ee 8962 if Analyzed (Freeze_Expr) and then not In_Instance then
8963 Check_Overloaded_Name;
8964 else
8965 Err := False;
8966 end if;
55e8372b 8967
d9f6a4ee 8968 -- All other cases
55e8372b 8969
d9f6a4ee 8970 else
c1efebf9 8971 -- Indicate that the expression comes from an aspect specification,
8972 -- which is used in subsequent analysis even if expansion is off.
8973
8974 Set_Parent (End_Decl_Expr, ASN);
8975
d9f6a4ee 8976 -- In a generic context the aspect expressions have not been
8977 -- preanalyzed, so do it now. There are no conformance checks
8978 -- to perform in this case.
55e8372b 8979
d9f6a4ee 8980 if No (T) then
8981 Check_Aspect_At_Freeze_Point (ASN);
8982 return;
55e8372b 8983
d9f6a4ee 8984 -- The default values attributes may be defined in the private part,
8985 -- and the analysis of the expression may take place when only the
8986 -- partial view is visible. The expression must be scalar, so use
8987 -- the full view to resolve.
55e8372b 8988
d9f6a4ee 8989 elsif (A_Id = Aspect_Default_Value
8990 or else
8991 A_Id = Aspect_Default_Component_Value)
8992 and then Is_Private_Type (T)
8993 then
8994 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
c1efebf9 8995
d9f6a4ee 8996 else
8997 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8998 end if;
d97beb2f 8999
d9f6a4ee 9000 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
9001 end if;
55e8372b 9002
c1efebf9 9003 -- Output error message if error. Force error on aspect specification
9004 -- even if there is an error on the expression itself.
55e8372b 9005
d9f6a4ee 9006 if Err then
9007 Error_Msg_NE
c1efebf9 9008 ("!visibility of aspect for& changes after freeze point",
d9f6a4ee 9009 ASN, Ent);
9010 Error_Msg_NE
9011 ("info: & is frozen here, aspects evaluated at this point??",
9012 Freeze_Node (Ent), Ent);
9013 end if;
9014 end Check_Aspect_At_End_Of_Declarations;
55e8372b 9015
d9f6a4ee 9016 ----------------------------------
9017 -- Check_Aspect_At_Freeze_Point --
9018 ----------------------------------
9dc88aea 9019
d9f6a4ee 9020 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9021 Ident : constant Node_Id := Identifier (ASN);
9022 -- Identifier (use Entity field to save expression)
9dc88aea 9023
d9f6a4ee 9024 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9dc88aea 9025
d9f6a4ee 9026 T : Entity_Id := Empty;
9027 -- Type required for preanalyze call
9dc88aea 9028
d9f6a4ee 9029 begin
9030 -- On entry to this procedure, Entity (Ident) contains a copy of the
9031 -- original expression from the aspect, saved for this purpose.
9dc88aea 9032
d9f6a4ee 9033 -- On exit from this procedure Entity (Ident) is unchanged, still
9034 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9035 -- of the expression, preanalyzed just after the freeze point.
d97beb2f 9036
d9f6a4ee 9037 -- Make a copy of the expression to be preanalyzed
d97beb2f 9038
d9f6a4ee 9039 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
d97beb2f 9040
d9f6a4ee 9041 -- Find type for preanalyze call
d97beb2f 9042
d9f6a4ee 9043 case A_Id is
9dc88aea 9044
d9f6a4ee 9045 -- No_Aspect should be impossible
d97beb2f 9046
d9f6a4ee 9047 when No_Aspect =>
9048 raise Program_Error;
9049
9050 -- Aspects taking an optional boolean argument
d97beb2f 9051
d9f6a4ee 9052 when Boolean_Aspects |
9053 Library_Unit_Aspects =>
9dc88aea 9054
d9f6a4ee 9055 T := Standard_Boolean;
d7c2851f 9056
d9f6a4ee 9057 -- Aspects corresponding to attribute definition clauses
9dc88aea 9058
d9f6a4ee 9059 when Aspect_Address =>
9060 T := RTE (RE_Address);
9dc88aea 9061
d9f6a4ee 9062 when Aspect_Attach_Handler =>
9063 T := RTE (RE_Interrupt_ID);
d7c2851f 9064
d9f6a4ee 9065 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9066 T := RTE (RE_Bit_Order);
d7c2851f 9067
d9f6a4ee 9068 when Aspect_Convention =>
9069 return;
d7c2851f 9070
d9f6a4ee 9071 when Aspect_CPU =>
9072 T := RTE (RE_CPU_Range);
d7c2851f 9073
d9f6a4ee 9074 -- Default_Component_Value is resolved with the component type
d7c2851f 9075
d9f6a4ee 9076 when Aspect_Default_Component_Value =>
9077 T := Component_Type (Entity (ASN));
d7c2851f 9078
647fab54 9079 when Aspect_Default_Storage_Pool =>
9080 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9081
d9f6a4ee 9082 -- Default_Value is resolved with the type entity in question
d7c2851f 9083
d9f6a4ee 9084 when Aspect_Default_Value =>
9085 T := Entity (ASN);
9dc88aea 9086
d9f6a4ee 9087 when Aspect_Dispatching_Domain =>
9088 T := RTE (RE_Dispatching_Domain);
9dc88aea 9089
d9f6a4ee 9090 when Aspect_External_Tag =>
9091 T := Standard_String;
9dc88aea 9092
d9f6a4ee 9093 when Aspect_External_Name =>
9094 T := Standard_String;
9dc88aea 9095
d9f6a4ee 9096 when Aspect_Link_Name =>
9097 T := Standard_String;
9dc88aea 9098
d9f6a4ee 9099 when Aspect_Priority | Aspect_Interrupt_Priority =>
9100 T := Standard_Integer;
d97beb2f 9101
d9f6a4ee 9102 when Aspect_Relative_Deadline =>
9103 T := RTE (RE_Time_Span);
d97beb2f 9104
d9f6a4ee 9105 when Aspect_Small =>
9106 T := Universal_Real;
490beba6 9107
d9f6a4ee 9108 -- For a simple storage pool, we have to retrieve the type of the
9109 -- pool object associated with the aspect's corresponding attribute
9110 -- definition clause.
490beba6 9111
d9f6a4ee 9112 when Aspect_Simple_Storage_Pool =>
9113 T := Etype (Expression (Aspect_Rep_Item (ASN)));
d97beb2f 9114
d9f6a4ee 9115 when Aspect_Storage_Pool =>
9116 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
d97beb2f 9117
d9f6a4ee 9118 when Aspect_Alignment |
9119 Aspect_Component_Size |
9120 Aspect_Machine_Radix |
9121 Aspect_Object_Size |
9122 Aspect_Size |
9123 Aspect_Storage_Size |
9124 Aspect_Stream_Size |
9125 Aspect_Value_Size =>
9126 T := Any_Integer;
9dc88aea 9127
04ae062f 9128 when Aspect_Linker_Section =>
9129 T := Standard_String;
9130
d9f6a4ee 9131 when Aspect_Synchronization =>
9132 return;
7d20685d 9133
d9f6a4ee 9134 -- Special case, the expression of these aspects is just an entity
9135 -- that does not need any resolution, so just analyze.
7d20685d 9136
d9f6a4ee 9137 when Aspect_Input |
9138 Aspect_Output |
9139 Aspect_Read |
9140 Aspect_Suppress |
9141 Aspect_Unsuppress |
9142 Aspect_Warnings |
9143 Aspect_Write =>
9144 Analyze (Expression (ASN));
9145 return;
7d20685d 9146
d9f6a4ee 9147 -- Same for Iterator aspects, where the expression is a function
9148 -- name. Legality rules are checked separately.
89f1e35c 9149
d9f6a4ee 9150 when Aspect_Constant_Indexing |
9151 Aspect_Default_Iterator |
9152 Aspect_Iterator_Element |
9153 Aspect_Variable_Indexing =>
9154 Analyze (Expression (ASN));
9155 return;
7d20685d 9156
b3f8228a 9157 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9158
9159 when Aspect_Iterable =>
3061ffde 9160 T := Entity (ASN);
9161
b3f8228a 9162 declare
a9f5fea7 9163 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
3061ffde 9164 Assoc : Node_Id;
9165 Expr : Node_Id;
a9f5fea7 9166
b3f8228a 9167 begin
a9f5fea7 9168 if Cursor = Any_Type then
9169 return;
9170 end if;
9171
b3f8228a 9172 Assoc := First (Component_Associations (Expression (ASN)));
9173 while Present (Assoc) loop
3061ffde 9174 Expr := Expression (Assoc);
9175 Analyze (Expr);
a9f5fea7 9176
9177 if not Error_Posted (Expr) then
9178 Resolve_Iterable_Operation
9179 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9180 end if;
9181
b3f8228a 9182 Next (Assoc);
9183 end loop;
9184 end;
3061ffde 9185
b3f8228a 9186 return;
9187
d9f6a4ee 9188 -- Invariant/Predicate take boolean expressions
7d20685d 9189
d9f6a4ee 9190 when Aspect_Dynamic_Predicate |
9191 Aspect_Invariant |
9192 Aspect_Predicate |
9193 Aspect_Static_Predicate |
9194 Aspect_Type_Invariant =>
9195 T := Standard_Boolean;
7d20685d 9196
d9f6a4ee 9197 -- Here is the list of aspects that don't require delay analysis
89f1e35c 9198
ec6f6da5 9199 when Aspect_Abstract_State |
9200 Aspect_Annotate |
9201 Aspect_Contract_Cases |
9202 Aspect_Default_Initial_Condition |
3dbe7a69 9203 Aspect_Depends |
ec6f6da5 9204 Aspect_Dimension |
9205 Aspect_Dimension_System |
cab27d2a 9206 Aspect_Extensions_Visible |
3dbe7a69 9207 Aspect_Ghost |
9208 Aspect_Global |
ec6f6da5 9209 Aspect_Implicit_Dereference |
9210 Aspect_Initial_Condition |
9211 Aspect_Initializes |
1fd4313f 9212 Aspect_Obsolescent |
ec6f6da5 9213 Aspect_Part_Of |
9214 Aspect_Post |
9215 Aspect_Postcondition |
9216 Aspect_Pre |
9217 Aspect_Precondition |
9218 Aspect_Refined_Depends |
9219 Aspect_Refined_Global |
9220 Aspect_Refined_Post |
9221 Aspect_Refined_State |
9222 Aspect_SPARK_Mode |
aa2f48d2 9223 Aspect_Test_Case |
9224 Aspect_Unimplemented =>
d9f6a4ee 9225 raise Program_Error;
2b184b2f 9226
d9f6a4ee 9227 end case;
2b184b2f 9228
d9f6a4ee 9229 -- Do the preanalyze call
2b184b2f 9230
d9f6a4ee 9231 Preanalyze_Spec_Expression (Expression (ASN), T);
9232 end Check_Aspect_At_Freeze_Point;
2b184b2f 9233
d9f6a4ee 9234 -----------------------------------
9235 -- Check_Constant_Address_Clause --
9236 -----------------------------------
2b184b2f 9237
d9f6a4ee 9238 procedure Check_Constant_Address_Clause
9239 (Expr : Node_Id;
9240 U_Ent : Entity_Id)
9241 is
9242 procedure Check_At_Constant_Address (Nod : Node_Id);
9243 -- Checks that the given node N represents a name whose 'Address is
9244 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9245 -- address value is the same at the point of declaration of U_Ent and at
9246 -- the time of elaboration of the address clause.
84ed7523 9247
d9f6a4ee 9248 procedure Check_Expr_Constants (Nod : Node_Id);
9249 -- Checks that Nod meets the requirements for a constant address clause
9250 -- in the sense of the enclosing procedure.
84ed7523 9251
d9f6a4ee 9252 procedure Check_List_Constants (Lst : List_Id);
9253 -- Check that all elements of list Lst meet the requirements for a
9254 -- constant address clause in the sense of the enclosing procedure.
84ed7523 9255
d9f6a4ee 9256 -------------------------------
9257 -- Check_At_Constant_Address --
9258 -------------------------------
84ed7523 9259
d9f6a4ee 9260 procedure Check_At_Constant_Address (Nod : Node_Id) is
9261 begin
9262 if Is_Entity_Name (Nod) then
9263 if Present (Address_Clause (Entity ((Nod)))) then
9264 Error_Msg_NE
9265 ("invalid address clause for initialized object &!",
9266 Nod, U_Ent);
9267 Error_Msg_NE
9268 ("address for& cannot" &
9269 " depend on another address clause! (RM 13.1(22))!",
9270 Nod, U_Ent);
84ed7523 9271
d9f6a4ee 9272 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9273 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9274 then
9275 Error_Msg_NE
9276 ("invalid address clause for initialized object &!",
9277 Nod, U_Ent);
9278 Error_Msg_Node_2 := U_Ent;
9279 Error_Msg_NE
9280 ("\& must be defined before & (RM 13.1(22))!",
9281 Nod, Entity (Nod));
9282 end if;
7d20685d 9283
d9f6a4ee 9284 elsif Nkind (Nod) = N_Selected_Component then
9285 declare
9286 T : constant Entity_Id := Etype (Prefix (Nod));
59f3e675 9287
d9f6a4ee 9288 begin
9289 if (Is_Record_Type (T)
9290 and then Has_Discriminants (T))
9291 or else
9292 (Is_Access_Type (T)
f02a9a9a 9293 and then Is_Record_Type (Designated_Type (T))
9294 and then Has_Discriminants (Designated_Type (T)))
d9f6a4ee 9295 then
9296 Error_Msg_NE
9297 ("invalid address clause for initialized object &!",
9298 Nod, U_Ent);
9299 Error_Msg_N
9300 ("\address cannot depend on component" &
9301 " of discriminated record (RM 13.1(22))!",
9302 Nod);
9303 else
9304 Check_At_Constant_Address (Prefix (Nod));
9305 end if;
9306 end;
89cc7147 9307
d9f6a4ee 9308 elsif Nkind (Nod) = N_Indexed_Component then
9309 Check_At_Constant_Address (Prefix (Nod));
9310 Check_List_Constants (Expressions (Nod));
89cc7147 9311
84ed7523 9312 else
d9f6a4ee 9313 Check_Expr_Constants (Nod);
84ed7523 9314 end if;
d9f6a4ee 9315 end Check_At_Constant_Address;
81b424ac 9316
d9f6a4ee 9317 --------------------------
9318 -- Check_Expr_Constants --
9319 --------------------------
7b9b2f05 9320
d9f6a4ee 9321 procedure Check_Expr_Constants (Nod : Node_Id) is
9322 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9323 Ent : Entity_Id := Empty;
7b9b2f05 9324
d9f6a4ee 9325 begin
9326 if Nkind (Nod) in N_Has_Etype
9327 and then Etype (Nod) = Any_Type
7b9b2f05 9328 then
d9f6a4ee 9329 return;
309c3053 9330 end if;
9331
d9f6a4ee 9332 case Nkind (Nod) is
9333 when N_Empty | N_Error =>
9334 return;
7d20685d 9335
d9f6a4ee 9336 when N_Identifier | N_Expanded_Name =>
9337 Ent := Entity (Nod);
7d20685d 9338
d9f6a4ee 9339 -- We need to look at the original node if it is different
9340 -- from the node, since we may have rewritten things and
9341 -- substituted an identifier representing the rewrite.
7d20685d 9342
d9f6a4ee 9343 if Original_Node (Nod) /= Nod then
9344 Check_Expr_Constants (Original_Node (Nod));
7d20685d 9345
d9f6a4ee 9346 -- If the node is an object declaration without initial
9347 -- value, some code has been expanded, and the expression
9348 -- is not constant, even if the constituents might be
9349 -- acceptable, as in A'Address + offset.
7d20685d 9350
d9f6a4ee 9351 if Ekind (Ent) = E_Variable
9352 and then
9353 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9354 and then
9355 No (Expression (Declaration_Node (Ent)))
9356 then
9357 Error_Msg_NE
9358 ("invalid address clause for initialized object &!",
9359 Nod, U_Ent);
89f1e35c 9360
d9f6a4ee 9361 -- If entity is constant, it may be the result of expanding
9362 -- a check. We must verify that its declaration appears
9363 -- before the object in question, else we also reject the
9364 -- address clause.
7d20685d 9365
d9f6a4ee 9366 elsif Ekind (Ent) = E_Constant
9367 and then In_Same_Source_Unit (Ent, U_Ent)
9368 and then Sloc (Ent) > Loc_U_Ent
9369 then
9370 Error_Msg_NE
9371 ("invalid address clause for initialized object &!",
9372 Nod, U_Ent);
9373 end if;
7d20685d 9374
d9f6a4ee 9375 return;
9376 end if;
7d20685d 9377
d9f6a4ee 9378 -- Otherwise look at the identifier and see if it is OK
7d20685d 9379
d9f6a4ee 9380 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9381 or else Is_Type (Ent)
9382 then
9383 return;
7d20685d 9384
f02a9a9a 9385 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9386
d9f6a4ee 9387 -- This is the case where we must have Ent defined before
9388 -- U_Ent. Clearly if they are in different units this
9389 -- requirement is met since the unit containing Ent is
9390 -- already processed.
7d20685d 9391
d9f6a4ee 9392 if not In_Same_Source_Unit (Ent, U_Ent) then
9393 return;
7d20685d 9394
d9f6a4ee 9395 -- Otherwise location of Ent must be before the location
9396 -- of U_Ent, that's what prior defined means.
7d20685d 9397
d9f6a4ee 9398 elsif Sloc (Ent) < Loc_U_Ent then
9399 return;
6c545057 9400
d9f6a4ee 9401 else
9402 Error_Msg_NE
9403 ("invalid address clause for initialized object &!",
9404 Nod, U_Ent);
9405 Error_Msg_Node_2 := U_Ent;
9406 Error_Msg_NE
9407 ("\& must be defined before & (RM 13.1(22))!",
9408 Nod, Ent);
9409 end if;
37c6e44c 9410
d9f6a4ee 9411 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9412 Check_Expr_Constants (Original_Node (Nod));
6c545057 9413
d9f6a4ee 9414 else
9415 Error_Msg_NE
9416 ("invalid address clause for initialized object &!",
9417 Nod, U_Ent);
3cdbaa5a 9418
d9f6a4ee 9419 if Comes_From_Source (Ent) then
9420 Error_Msg_NE
9421 ("\reference to variable& not allowed"
9422 & " (RM 13.1(22))!", Nod, Ent);
9423 else
9424 Error_Msg_N
9425 ("non-static expression not allowed"
9426 & " (RM 13.1(22))!", Nod);
9427 end if;
9428 end if;
3cdbaa5a 9429
d9f6a4ee 9430 when N_Integer_Literal =>
7f694ca2 9431
d9f6a4ee 9432 -- If this is a rewritten unchecked conversion, in a system
9433 -- where Address is an integer type, always use the base type
9434 -- for a literal value. This is user-friendly and prevents
9435 -- order-of-elaboration issues with instances of unchecked
9436 -- conversion.
3cdbaa5a 9437
d9f6a4ee 9438 if Nkind (Original_Node (Nod)) = N_Function_Call then
9439 Set_Etype (Nod, Base_Type (Etype (Nod)));
9440 end if;
e1cedbae 9441
d9f6a4ee 9442 when N_Real_Literal |
9443 N_String_Literal |
9444 N_Character_Literal =>
9445 return;
7d20685d 9446
d9f6a4ee 9447 when N_Range =>
9448 Check_Expr_Constants (Low_Bound (Nod));
9449 Check_Expr_Constants (High_Bound (Nod));
231eb581 9450
d9f6a4ee 9451 when N_Explicit_Dereference =>
9452 Check_Expr_Constants (Prefix (Nod));
231eb581 9453
d9f6a4ee 9454 when N_Indexed_Component =>
9455 Check_Expr_Constants (Prefix (Nod));
9456 Check_List_Constants (Expressions (Nod));
7d20685d 9457
d9f6a4ee 9458 when N_Slice =>
9459 Check_Expr_Constants (Prefix (Nod));
9460 Check_Expr_Constants (Discrete_Range (Nod));
cb4c311d 9461
d9f6a4ee 9462 when N_Selected_Component =>
9463 Check_Expr_Constants (Prefix (Nod));
6144c105 9464
d9f6a4ee 9465 when N_Attribute_Reference =>
9466 if Nam_In (Attribute_Name (Nod), Name_Address,
9467 Name_Access,
9468 Name_Unchecked_Access,
9469 Name_Unrestricted_Access)
9470 then
9471 Check_At_Constant_Address (Prefix (Nod));
6144c105 9472
d9f6a4ee 9473 else
9474 Check_Expr_Constants (Prefix (Nod));
9475 Check_List_Constants (Expressions (Nod));
9476 end if;
a7a4a7c2 9477
d9f6a4ee 9478 when N_Aggregate =>
9479 Check_List_Constants (Component_Associations (Nod));
9480 Check_List_Constants (Expressions (Nod));
7d20685d 9481
d9f6a4ee 9482 when N_Component_Association =>
9483 Check_Expr_Constants (Expression (Nod));
e1cedbae 9484
d9f6a4ee 9485 when N_Extension_Aggregate =>
9486 Check_Expr_Constants (Ancestor_Part (Nod));
9487 Check_List_Constants (Component_Associations (Nod));
9488 Check_List_Constants (Expressions (Nod));
3cdbaa5a 9489
d9f6a4ee 9490 when N_Null =>
9491 return;
3cdbaa5a 9492
d9f6a4ee 9493 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9494 Check_Expr_Constants (Left_Opnd (Nod));
9495 Check_Expr_Constants (Right_Opnd (Nod));
e1cedbae 9496
d9f6a4ee 9497 when N_Unary_Op =>
9498 Check_Expr_Constants (Right_Opnd (Nod));
7f694ca2 9499
d9f6a4ee 9500 when N_Type_Conversion |
9501 N_Qualified_Expression |
9502 N_Allocator |
9503 N_Unchecked_Type_Conversion =>
9504 Check_Expr_Constants (Expression (Nod));
47a46747 9505
d9f6a4ee 9506 when N_Function_Call =>
9507 if not Is_Pure (Entity (Name (Nod))) then
9508 Error_Msg_NE
9509 ("invalid address clause for initialized object &!",
9510 Nod, U_Ent);
7f694ca2 9511
d9f6a4ee 9512 Error_Msg_NE
9513 ("\function & is not pure (RM 13.1(22))!",
9514 Nod, Entity (Name (Nod)));
b55f7641 9515
d9f6a4ee 9516 else
9517 Check_List_Constants (Parameter_Associations (Nod));
9518 end if;
b55f7641 9519
d9f6a4ee 9520 when N_Parameter_Association =>
9521 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7d20685d 9522
d9f6a4ee 9523 when others =>
9524 Error_Msg_NE
9525 ("invalid address clause for initialized object &!",
9526 Nod, U_Ent);
9527 Error_Msg_NE
9528 ("\must be constant defined before& (RM 13.1(22))!",
9529 Nod, U_Ent);
9530 end case;
9531 end Check_Expr_Constants;
7d20685d 9532
d9f6a4ee 9533 --------------------------
9534 -- Check_List_Constants --
9535 --------------------------
89f1e35c 9536
d9f6a4ee 9537 procedure Check_List_Constants (Lst : List_Id) is
9538 Nod1 : Node_Id;
7d20685d 9539
d9f6a4ee 9540 begin
9541 if Present (Lst) then
9542 Nod1 := First (Lst);
9543 while Present (Nod1) loop
9544 Check_Expr_Constants (Nod1);
9545 Next (Nod1);
9546 end loop;
9547 end if;
9548 end Check_List_Constants;
81b424ac 9549
d9f6a4ee 9550 -- Start of processing for Check_Constant_Address_Clause
81b424ac 9551
d9f6a4ee 9552 begin
9553 -- If rep_clauses are to be ignored, no need for legality checks. In
9c7948d7 9554 -- particular, no need to pester user about rep clauses that violate the
9555 -- rule on constant addresses, given that these clauses will be removed
9556 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9557 -- we want to relax these checks.
7d20685d 9558
f1a9be43 9559 if not Ignore_Rep_Clauses and not CodePeer_Mode then
d9f6a4ee 9560 Check_Expr_Constants (Expr);
9561 end if;
9562 end Check_Constant_Address_Clause;
7d20685d 9563
6653b695 9564 ---------------------------
9565 -- Check_Pool_Size_Clash --
9566 ---------------------------
9567
9568 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9569 Post : Node_Id;
9570
9571 begin
9572 -- We need to find out which one came first. Note that in the case of
9573 -- aspects mixed with pragmas there are cases where the processing order
9574 -- is reversed, which is why we do the check here.
9575
9576 if Sloc (SP) < Sloc (SS) then
9577 Error_Msg_Sloc := Sloc (SP);
9578 Post := SS;
9579 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9580
9581 else
9582 Error_Msg_Sloc := Sloc (SS);
9583 Post := SP;
9584 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9585 end if;
9586
9587 Error_Msg_N
9588 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9589 end Check_Pool_Size_Clash;
9590
d9f6a4ee 9591 ----------------------------------------
9592 -- Check_Record_Representation_Clause --
9593 ----------------------------------------
85696508 9594
d9f6a4ee 9595 procedure Check_Record_Representation_Clause (N : Node_Id) is
9596 Loc : constant Source_Ptr := Sloc (N);
9597 Ident : constant Node_Id := Identifier (N);
9598 Rectype : Entity_Id;
9599 Fent : Entity_Id;
9600 CC : Node_Id;
9601 Fbit : Uint;
9602 Lbit : Uint;
9603 Hbit : Uint := Uint_0;
9604 Comp : Entity_Id;
9605 Pcomp : Entity_Id;
89f1e35c 9606
d9f6a4ee 9607 Max_Bit_So_Far : Uint;
9608 -- Records the maximum bit position so far. If all field positions
9609 -- are monotonically increasing, then we can skip the circuit for
9610 -- checking for overlap, since no overlap is possible.
85696508 9611
d9f6a4ee 9612 Tagged_Parent : Entity_Id := Empty;
9613 -- This is set in the case of a derived tagged type for which we have
9614 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9615 -- positioned by record representation clauses). In this case we must
9616 -- check for overlap between components of this tagged type, and the
9617 -- components of its parent. Tagged_Parent will point to this parent
9618 -- type. For all other cases Tagged_Parent is left set to Empty.
7d20685d 9619
d9f6a4ee 9620 Parent_Last_Bit : Uint;
9621 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9622 -- last bit position for any field in the parent type. We only need to
9623 -- check overlap for fields starting below this point.
7d20685d 9624
d9f6a4ee 9625 Overlap_Check_Required : Boolean;
9626 -- Used to keep track of whether or not an overlap check is required
7d20685d 9627
d9f6a4ee 9628 Overlap_Detected : Boolean := False;
9629 -- Set True if an overlap is detected
d6f39728 9630
d9f6a4ee 9631 Ccount : Natural := 0;
9632 -- Number of component clauses in record rep clause
d6f39728 9633
d9f6a4ee 9634 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9635 -- Given two entities for record components or discriminants, checks
9636 -- if they have overlapping component clauses and issues errors if so.
d6f39728 9637
d9f6a4ee 9638 procedure Find_Component;
9639 -- Finds component entity corresponding to current component clause (in
9640 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9641 -- start/stop bits for the field. If there is no matching component or
9642 -- if the matching component does not have a component clause, then
9643 -- that's an error and Comp is set to Empty, but no error message is
9644 -- issued, since the message was already given. Comp is also set to
9645 -- Empty if the current "component clause" is in fact a pragma.
d6f39728 9646
d9f6a4ee 9647 -----------------------------
9648 -- Check_Component_Overlap --
9649 -----------------------------
9650
9651 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9652 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9653 CC2 : constant Node_Id := Component_Clause (C2_Ent);
d6f39728 9654
d6f39728 9655 begin
d9f6a4ee 9656 if Present (CC1) and then Present (CC2) then
d6f39728 9657
d9f6a4ee 9658 -- Exclude odd case where we have two tag components in the same
9659 -- record, both at location zero. This seems a bit strange, but
9660 -- it seems to happen in some circumstances, perhaps on an error.
9661
9662 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9663 return;
d6f39728 9664 end if;
9665
d9f6a4ee 9666 -- Here we check if the two fields overlap
9667
d6f39728 9668 declare
d9f6a4ee 9669 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9670 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9671 E1 : constant Uint := S1 + Esize (C1_Ent);
9672 E2 : constant Uint := S2 + Esize (C2_Ent);
d6f39728 9673
9674 begin
d9f6a4ee 9675 if E2 <= S1 or else E1 <= S2 then
9676 null;
d6f39728 9677 else
d9f6a4ee 9678 Error_Msg_Node_2 := Component_Name (CC2);
9679 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9680 Error_Msg_Node_1 := Component_Name (CC1);
9681 Error_Msg_N
9682 ("component& overlaps & #", Component_Name (CC1));
9683 Overlap_Detected := True;
d6f39728 9684 end if;
9685 end;
d6f39728 9686 end if;
d9f6a4ee 9687 end Check_Component_Overlap;
d6f39728 9688
d9f6a4ee 9689 --------------------
9690 -- Find_Component --
9691 --------------------
9dfe12ae 9692
d9f6a4ee 9693 procedure Find_Component is
9dfe12ae 9694
d9f6a4ee 9695 procedure Search_Component (R : Entity_Id);
9696 -- Search components of R for a match. If found, Comp is set
9dfe12ae 9697
d9f6a4ee 9698 ----------------------
9699 -- Search_Component --
9700 ----------------------
e7b2d6bc 9701
d9f6a4ee 9702 procedure Search_Component (R : Entity_Id) is
9703 begin
9704 Comp := First_Component_Or_Discriminant (R);
9705 while Present (Comp) loop
e7b2d6bc 9706
d9f6a4ee 9707 -- Ignore error of attribute name for component name (we
9708 -- already gave an error message for this, so no need to
9709 -- complain here)
e7b2d6bc 9710
d9f6a4ee 9711 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9712 null;
9713 else
9714 exit when Chars (Comp) = Chars (Component_Name (CC));
9dfe12ae 9715 end if;
9716
d9f6a4ee 9717 Next_Component_Or_Discriminant (Comp);
9718 end loop;
9719 end Search_Component;
d6f39728 9720
d9f6a4ee 9721 -- Start of processing for Find_Component
d6f39728 9722
d9f6a4ee 9723 begin
9724 -- Return with Comp set to Empty if we have a pragma
d6f39728 9725
d9f6a4ee 9726 if Nkind (CC) = N_Pragma then
9727 Comp := Empty;
9728 return;
9729 end if;
d6f39728 9730
d9f6a4ee 9731 -- Search current record for matching component
d6f39728 9732
d9f6a4ee 9733 Search_Component (Rectype);
9dfe12ae 9734
d9f6a4ee 9735 -- If not found, maybe component of base type discriminant that is
9736 -- absent from statically constrained first subtype.
e7b2d6bc 9737
d9f6a4ee 9738 if No (Comp) then
9739 Search_Component (Base_Type (Rectype));
9740 end if;
e7b2d6bc 9741
d9f6a4ee 9742 -- If no component, or the component does not reference the component
9743 -- clause in question, then there was some previous error for which
9744 -- we already gave a message, so just return with Comp Empty.
d6f39728 9745
d9f6a4ee 9746 if No (Comp) or else Component_Clause (Comp) /= CC then
9747 Check_Error_Detected;
9748 Comp := Empty;
93735cb8 9749
d9f6a4ee 9750 -- Normal case where we have a component clause
93735cb8 9751
d9f6a4ee 9752 else
9753 Fbit := Component_Bit_Offset (Comp);
9754 Lbit := Fbit + Esize (Comp) - 1;
9755 end if;
9756 end Find_Component;
93735cb8 9757
d9f6a4ee 9758 -- Start of processing for Check_Record_Representation_Clause
d6f39728 9759
d9f6a4ee 9760 begin
9761 Find_Type (Ident);
9762 Rectype := Entity (Ident);
d6f39728 9763
d9f6a4ee 9764 if Rectype = Any_Type then
9765 return;
9766 else
9767 Rectype := Underlying_Type (Rectype);
9768 end if;
d6f39728 9769
d9f6a4ee 9770 -- See if we have a fully repped derived tagged type
d6f39728 9771
d9f6a4ee 9772 declare
9773 PS : constant Entity_Id := Parent_Subtype (Rectype);
d6f39728 9774
d9f6a4ee 9775 begin
9776 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9777 Tagged_Parent := PS;
d6f39728 9778
d9f6a4ee 9779 -- Find maximum bit of any component of the parent type
d6f39728 9780
d9f6a4ee 9781 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9782 Pcomp := First_Entity (Tagged_Parent);
9783 while Present (Pcomp) loop
9784 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9785 if Component_Bit_Offset (Pcomp) /= No_Uint
9786 and then Known_Static_Esize (Pcomp)
9787 then
9788 Parent_Last_Bit :=
9789 UI_Max
9790 (Parent_Last_Bit,
9791 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9792 end if;
9793
9794 Next_Entity (Pcomp);
d6f39728 9795 end if;
d9f6a4ee 9796 end loop;
9797 end if;
9798 end;
d6f39728 9799
d9f6a4ee 9800 -- All done if no component clauses
d6f39728 9801
d9f6a4ee 9802 CC := First (Component_Clauses (N));
d6f39728 9803
d9f6a4ee 9804 if No (CC) then
9805 return;
9806 end if;
d6f39728 9807
d9f6a4ee 9808 -- If a tag is present, then create a component clause that places it
9809 -- at the start of the record (otherwise gigi may place it after other
9810 -- fields that have rep clauses).
d6f39728 9811
d9f6a4ee 9812 Fent := First_Entity (Rectype);
d6f39728 9813
d9f6a4ee 9814 if Nkind (Fent) = N_Defining_Identifier
9815 and then Chars (Fent) = Name_uTag
9816 then
9817 Set_Component_Bit_Offset (Fent, Uint_0);
9818 Set_Normalized_Position (Fent, Uint_0);
9819 Set_Normalized_First_Bit (Fent, Uint_0);
9820 Set_Normalized_Position_Max (Fent, Uint_0);
9821 Init_Esize (Fent, System_Address_Size);
d6f39728 9822
d9f6a4ee 9823 Set_Component_Clause (Fent,
9824 Make_Component_Clause (Loc,
9825 Component_Name => Make_Identifier (Loc, Name_uTag),
d6f39728 9826
d9f6a4ee 9827 Position => Make_Integer_Literal (Loc, Uint_0),
9828 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9829 Last_Bit =>
9830 Make_Integer_Literal (Loc,
9831 UI_From_Int (System_Address_Size))));
d6f39728 9832
d9f6a4ee 9833 Ccount := Ccount + 1;
9834 end if;
d6f39728 9835
d9f6a4ee 9836 Max_Bit_So_Far := Uint_Minus_1;
9837 Overlap_Check_Required := False;
d6f39728 9838
d9f6a4ee 9839 -- Process the component clauses
d6f39728 9840
d9f6a4ee 9841 while Present (CC) loop
9842 Find_Component;
d6f39728 9843
d9f6a4ee 9844 if Present (Comp) then
9845 Ccount := Ccount + 1;
d6f39728 9846
d9f6a4ee 9847 -- We need a full overlap check if record positions non-monotonic
d6f39728 9848
d9f6a4ee 9849 if Fbit <= Max_Bit_So_Far then
9850 Overlap_Check_Required := True;
9851 end if;
d6f39728 9852
d9f6a4ee 9853 Max_Bit_So_Far := Lbit;
d6f39728 9854
d9f6a4ee 9855 -- Check bit position out of range of specified size
01cb2726 9856
d9f6a4ee 9857 if Has_Size_Clause (Rectype)
9858 and then RM_Size (Rectype) <= Lbit
9859 then
9860 Error_Msg_N
9861 ("bit number out of range of specified size",
9862 Last_Bit (CC));
d6f39728 9863
d9f6a4ee 9864 -- Check for overlap with tag component
67278d60 9865
d9f6a4ee 9866 else
9867 if Is_Tagged_Type (Rectype)
9868 and then Fbit < System_Address_Size
9869 then
9870 Error_Msg_NE
9871 ("component overlaps tag field of&",
9872 Component_Name (CC), Rectype);
9873 Overlap_Detected := True;
9874 end if;
67278d60 9875
d9f6a4ee 9876 if Hbit < Lbit then
9877 Hbit := Lbit;
9878 end if;
9879 end if;
67278d60 9880
d9f6a4ee 9881 -- Check parent overlap if component might overlap parent field
67278d60 9882
d9f6a4ee 9883 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9884 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9885 while Present (Pcomp) loop
9886 if not Is_Tag (Pcomp)
9887 and then Chars (Pcomp) /= Name_uParent
9888 then
9889 Check_Component_Overlap (Comp, Pcomp);
9890 end if;
67278d60 9891
d9f6a4ee 9892 Next_Component_Or_Discriminant (Pcomp);
9893 end loop;
9894 end if;
9895 end if;
67278d60 9896
d9f6a4ee 9897 Next (CC);
9898 end loop;
47495553 9899
d9f6a4ee 9900 -- Now that we have processed all the component clauses, check for
9901 -- overlap. We have to leave this till last, since the components can
9902 -- appear in any arbitrary order in the representation clause.
67278d60 9903
d9f6a4ee 9904 -- We do not need this check if all specified ranges were monotonic,
9905 -- as recorded by Overlap_Check_Required being False at this stage.
67278d60 9906
d9f6a4ee 9907 -- This first section checks if there are any overlapping entries at
9908 -- all. It does this by sorting all entries and then seeing if there are
9909 -- any overlaps. If there are none, then that is decisive, but if there
9910 -- are overlaps, they may still be OK (they may result from fields in
9911 -- different variants).
67278d60 9912
d9f6a4ee 9913 if Overlap_Check_Required then
9914 Overlap_Check1 : declare
67278d60 9915
d9f6a4ee 9916 OC_Fbit : array (0 .. Ccount) of Uint;
9917 -- First-bit values for component clauses, the value is the offset
9918 -- of the first bit of the field from start of record. The zero
9919 -- entry is for use in sorting.
47495553 9920
d9f6a4ee 9921 OC_Lbit : array (0 .. Ccount) of Uint;
9922 -- Last-bit values for component clauses, the value is the offset
9923 -- of the last bit of the field from start of record. The zero
9924 -- entry is for use in sorting.
9925
9926 OC_Count : Natural := 0;
9927 -- Count of entries in OC_Fbit and OC_Lbit
67278d60 9928
d9f6a4ee 9929 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9930 -- Compare routine for Sort
67278d60 9931
d9f6a4ee 9932 procedure OC_Move (From : Natural; To : Natural);
9933 -- Move routine for Sort
67278d60 9934
d9f6a4ee 9935 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
67278d60 9936
d9f6a4ee 9937 -----------
9938 -- OC_Lt --
9939 -----------
67278d60 9940
d9f6a4ee 9941 function OC_Lt (Op1, Op2 : Natural) return Boolean is
67278d60 9942 begin
d9f6a4ee 9943 return OC_Fbit (Op1) < OC_Fbit (Op2);
9944 end OC_Lt;
67278d60 9945
d9f6a4ee 9946 -------------
9947 -- OC_Move --
9948 -------------
67278d60 9949
d9f6a4ee 9950 procedure OC_Move (From : Natural; To : Natural) is
9951 begin
9952 OC_Fbit (To) := OC_Fbit (From);
9953 OC_Lbit (To) := OC_Lbit (From);
9954 end OC_Move;
67278d60 9955
d9f6a4ee 9956 -- Start of processing for Overlap_Check
67278d60 9957
67278d60 9958 begin
d9f6a4ee 9959 CC := First (Component_Clauses (N));
9960 while Present (CC) loop
67278d60 9961
d9f6a4ee 9962 -- Exclude component clause already marked in error
67278d60 9963
d9f6a4ee 9964 if not Error_Posted (CC) then
9965 Find_Component;
9966
9967 if Present (Comp) then
9968 OC_Count := OC_Count + 1;
9969 OC_Fbit (OC_Count) := Fbit;
9970 OC_Lbit (OC_Count) := Lbit;
9971 end if;
67278d60 9972 end if;
9973
d9f6a4ee 9974 Next (CC);
67278d60 9975 end loop;
67278d60 9976
d9f6a4ee 9977 Sorting.Sort (OC_Count);
67278d60 9978
d9f6a4ee 9979 Overlap_Check_Required := False;
9980 for J in 1 .. OC_Count - 1 loop
9981 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9982 Overlap_Check_Required := True;
9983 exit;
9984 end if;
9985 end loop;
9986 end Overlap_Check1;
9987 end if;
67278d60 9988
d9f6a4ee 9989 -- If Overlap_Check_Required is still True, then we have to do the full
9990 -- scale overlap check, since we have at least two fields that do
9991 -- overlap, and we need to know if that is OK since they are in
9992 -- different variant, or whether we have a definite problem.
67278d60 9993
d9f6a4ee 9994 if Overlap_Check_Required then
9995 Overlap_Check2 : declare
9996 C1_Ent, C2_Ent : Entity_Id;
9997 -- Entities of components being checked for overlap
67278d60 9998
d9f6a4ee 9999 Clist : Node_Id;
10000 -- Component_List node whose Component_Items are being checked
67278d60 10001
d9f6a4ee 10002 Citem : Node_Id;
10003 -- Component declaration for component being checked
67278d60 10004
d9f6a4ee 10005 begin
10006 C1_Ent := First_Entity (Base_Type (Rectype));
67278d60 10007
d9f6a4ee 10008 -- Loop through all components in record. For each component check
10009 -- for overlap with any of the preceding elements on the component
10010 -- list containing the component and also, if the component is in
10011 -- a variant, check against components outside the case structure.
10012 -- This latter test is repeated recursively up the variant tree.
67278d60 10013
d9f6a4ee 10014 Main_Component_Loop : while Present (C1_Ent) loop
10015 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10016 goto Continue_Main_Component_Loop;
10017 end if;
67278d60 10018
d9f6a4ee 10019 -- Skip overlap check if entity has no declaration node. This
10020 -- happens with discriminants in constrained derived types.
10021 -- Possibly we are missing some checks as a result, but that
10022 -- does not seem terribly serious.
67278d60 10023
d9f6a4ee 10024 if No (Declaration_Node (C1_Ent)) then
10025 goto Continue_Main_Component_Loop;
10026 end if;
67278d60 10027
d9f6a4ee 10028 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
67278d60 10029
d9f6a4ee 10030 -- Loop through component lists that need checking. Check the
10031 -- current component list and all lists in variants above us.
67278d60 10032
d9f6a4ee 10033 Component_List_Loop : loop
67278d60 10034
d9f6a4ee 10035 -- If derived type definition, go to full declaration
10036 -- If at outer level, check discriminants if there are any.
67278d60 10037
d9f6a4ee 10038 if Nkind (Clist) = N_Derived_Type_Definition then
10039 Clist := Parent (Clist);
10040 end if;
67278d60 10041
d9f6a4ee 10042 -- Outer level of record definition, check discriminants
67278d60 10043
d9f6a4ee 10044 if Nkind_In (Clist, N_Full_Type_Declaration,
10045 N_Private_Type_Declaration)
67278d60 10046 then
d9f6a4ee 10047 if Has_Discriminants (Defining_Identifier (Clist)) then
10048 C2_Ent :=
10049 First_Discriminant (Defining_Identifier (Clist));
10050 while Present (C2_Ent) loop
10051 exit when C1_Ent = C2_Ent;
10052 Check_Component_Overlap (C1_Ent, C2_Ent);
10053 Next_Discriminant (C2_Ent);
10054 end loop;
10055 end if;
67278d60 10056
d9f6a4ee 10057 -- Record extension case
67278d60 10058
d9f6a4ee 10059 elsif Nkind (Clist) = N_Derived_Type_Definition then
10060 Clist := Empty;
67278d60 10061
d9f6a4ee 10062 -- Otherwise check one component list
67278d60 10063
d9f6a4ee 10064 else
10065 Citem := First (Component_Items (Clist));
10066 while Present (Citem) loop
10067 if Nkind (Citem) = N_Component_Declaration then
10068 C2_Ent := Defining_Identifier (Citem);
10069 exit when C1_Ent = C2_Ent;
10070 Check_Component_Overlap (C1_Ent, C2_Ent);
10071 end if;
67278d60 10072
d9f6a4ee 10073 Next (Citem);
10074 end loop;
10075 end if;
67278d60 10076
d9f6a4ee 10077 -- Check for variants above us (the parent of the Clist can
10078 -- be a variant, in which case its parent is a variant part,
10079 -- and the parent of the variant part is a component list
10080 -- whose components must all be checked against the current
10081 -- component for overlap).
67278d60 10082
d9f6a4ee 10083 if Nkind (Parent (Clist)) = N_Variant then
10084 Clist := Parent (Parent (Parent (Clist)));
67278d60 10085
d9f6a4ee 10086 -- Check for possible discriminant part in record, this
10087 -- is treated essentially as another level in the
10088 -- recursion. For this case the parent of the component
10089 -- list is the record definition, and its parent is the
10090 -- full type declaration containing the discriminant
10091 -- specifications.
10092
10093 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10094 Clist := Parent (Parent ((Clist)));
10095
10096 -- If neither of these two cases, we are at the top of
10097 -- the tree.
10098
10099 else
10100 exit Component_List_Loop;
10101 end if;
10102 end loop Component_List_Loop;
67278d60 10103
d9f6a4ee 10104 <<Continue_Main_Component_Loop>>
10105 Next_Entity (C1_Ent);
67278d60 10106
d9f6a4ee 10107 end loop Main_Component_Loop;
10108 end Overlap_Check2;
67278d60 10109 end if;
10110
d9f6a4ee 10111 -- The following circuit deals with warning on record holes (gaps). We
10112 -- skip this check if overlap was detected, since it makes sense for the
10113 -- programmer to fix this illegality before worrying about warnings.
67278d60 10114
d9f6a4ee 10115 if not Overlap_Detected and Warn_On_Record_Holes then
10116 Record_Hole_Check : declare
10117 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10118 -- Full declaration of record type
67278d60 10119
d9f6a4ee 10120 procedure Check_Component_List
10121 (CL : Node_Id;
10122 Sbit : Uint;
10123 DS : List_Id);
10124 -- Check component list CL for holes. The starting bit should be
10125 -- Sbit. which is zero for the main record component list and set
10126 -- appropriately for recursive calls for variants. DS is set to
10127 -- a list of discriminant specifications to be included in the
10128 -- consideration of components. It is No_List if none to consider.
67278d60 10129
d9f6a4ee 10130 --------------------------
10131 -- Check_Component_List --
10132 --------------------------
47495553 10133
d9f6a4ee 10134 procedure Check_Component_List
10135 (CL : Node_Id;
10136 Sbit : Uint;
10137 DS : List_Id)
10138 is
10139 Compl : Integer;
67278d60 10140
d9f6a4ee 10141 begin
10142 Compl := Integer (List_Length (Component_Items (CL)));
47495553 10143
d9f6a4ee 10144 if DS /= No_List then
10145 Compl := Compl + Integer (List_Length (DS));
10146 end if;
67278d60 10147
d9f6a4ee 10148 declare
10149 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10150 -- Gather components (zero entry is for sort routine)
67278d60 10151
d9f6a4ee 10152 Ncomps : Natural := 0;
10153 -- Number of entries stored in Comps (starting at Comps (1))
67278d60 10154
d9f6a4ee 10155 Citem : Node_Id;
10156 -- One component item or discriminant specification
67278d60 10157
d9f6a4ee 10158 Nbit : Uint;
10159 -- Starting bit for next component
67278d60 10160
d9f6a4ee 10161 CEnt : Entity_Id;
10162 -- Component entity
67278d60 10163
d9f6a4ee 10164 Variant : Node_Id;
10165 -- One variant
67278d60 10166
d9f6a4ee 10167 function Lt (Op1, Op2 : Natural) return Boolean;
10168 -- Compare routine for Sort
67278d60 10169
d9f6a4ee 10170 procedure Move (From : Natural; To : Natural);
10171 -- Move routine for Sort
67278d60 10172
d9f6a4ee 10173 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
67278d60 10174
d9f6a4ee 10175 --------
10176 -- Lt --
10177 --------
67278d60 10178
d9f6a4ee 10179 function Lt (Op1, Op2 : Natural) return Boolean is
10180 begin
10181 return Component_Bit_Offset (Comps (Op1))
10182 <
10183 Component_Bit_Offset (Comps (Op2));
10184 end Lt;
67278d60 10185
d9f6a4ee 10186 ----------
10187 -- Move --
10188 ----------
67278d60 10189
d9f6a4ee 10190 procedure Move (From : Natural; To : Natural) is
10191 begin
10192 Comps (To) := Comps (From);
10193 end Move;
67278d60 10194
d9f6a4ee 10195 begin
10196 -- Gather discriminants into Comp
67278d60 10197
d9f6a4ee 10198 if DS /= No_List then
10199 Citem := First (DS);
10200 while Present (Citem) loop
10201 if Nkind (Citem) = N_Discriminant_Specification then
10202 declare
10203 Ent : constant Entity_Id :=
10204 Defining_Identifier (Citem);
10205 begin
10206 if Ekind (Ent) = E_Discriminant then
10207 Ncomps := Ncomps + 1;
10208 Comps (Ncomps) := Ent;
10209 end if;
10210 end;
10211 end if;
67278d60 10212
d9f6a4ee 10213 Next (Citem);
10214 end loop;
10215 end if;
67278d60 10216
d9f6a4ee 10217 -- Gather component entities into Comp
67278d60 10218
d9f6a4ee 10219 Citem := First (Component_Items (CL));
10220 while Present (Citem) loop
10221 if Nkind (Citem) = N_Component_Declaration then
10222 Ncomps := Ncomps + 1;
10223 Comps (Ncomps) := Defining_Identifier (Citem);
10224 end if;
67278d60 10225
d9f6a4ee 10226 Next (Citem);
10227 end loop;
67278d60 10228
d9f6a4ee 10229 -- Now sort the component entities based on the first bit.
10230 -- Note we already know there are no overlapping components.
67278d60 10231
d9f6a4ee 10232 Sorting.Sort (Ncomps);
67278d60 10233
d9f6a4ee 10234 -- Loop through entries checking for holes
67278d60 10235
d9f6a4ee 10236 Nbit := Sbit;
10237 for J in 1 .. Ncomps loop
10238 CEnt := Comps (J);
10239 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
67278d60 10240
d9f6a4ee 10241 if Error_Msg_Uint_1 > 0 then
10242 Error_Msg_NE
10243 ("?H?^-bit gap before component&",
10244 Component_Name (Component_Clause (CEnt)), CEnt);
10245 end if;
67278d60 10246
d9f6a4ee 10247 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10248 end loop;
67278d60 10249
d9f6a4ee 10250 -- Process variant parts recursively if present
67278d60 10251
d9f6a4ee 10252 if Present (Variant_Part (CL)) then
10253 Variant := First (Variants (Variant_Part (CL)));
10254 while Present (Variant) loop
10255 Check_Component_List
10256 (Component_List (Variant), Nbit, No_List);
10257 Next (Variant);
10258 end loop;
67278d60 10259 end if;
d9f6a4ee 10260 end;
10261 end Check_Component_List;
67278d60 10262
d9f6a4ee 10263 -- Start of processing for Record_Hole_Check
67278d60 10264
d9f6a4ee 10265 begin
10266 declare
10267 Sbit : Uint;
67278d60 10268
d9f6a4ee 10269 begin
10270 if Is_Tagged_Type (Rectype) then
10271 Sbit := UI_From_Int (System_Address_Size);
10272 else
10273 Sbit := Uint_0;
10274 end if;
10275
10276 if Nkind (Decl) = N_Full_Type_Declaration
10277 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10278 then
10279 Check_Component_List
10280 (Component_List (Type_Definition (Decl)),
10281 Sbit,
10282 Discriminant_Specifications (Decl));
67278d60 10283 end if;
d9f6a4ee 10284 end;
10285 end Record_Hole_Check;
67278d60 10286 end if;
10287
d9f6a4ee 10288 -- For records that have component clauses for all components, and whose
10289 -- size is less than or equal to 32, we need to know the size in the
10290 -- front end to activate possible packed array processing where the
10291 -- component type is a record.
67278d60 10292
d9f6a4ee 10293 -- At this stage Hbit + 1 represents the first unused bit from all the
10294 -- component clauses processed, so if the component clauses are
10295 -- complete, then this is the length of the record.
67278d60 10296
d9f6a4ee 10297 -- For records longer than System.Storage_Unit, and for those where not
10298 -- all components have component clauses, the back end determines the
10299 -- length (it may for example be appropriate to round up the size
10300 -- to some convenient boundary, based on alignment considerations, etc).
67278d60 10301
d9f6a4ee 10302 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
67278d60 10303
d9f6a4ee 10304 -- Nothing to do if at least one component has no component clause
67278d60 10305
d9f6a4ee 10306 Comp := First_Component_Or_Discriminant (Rectype);
10307 while Present (Comp) loop
10308 exit when No (Component_Clause (Comp));
10309 Next_Component_Or_Discriminant (Comp);
10310 end loop;
67278d60 10311
d9f6a4ee 10312 -- If we fall out of loop, all components have component clauses
10313 -- and so we can set the size to the maximum value.
67278d60 10314
d9f6a4ee 10315 if No (Comp) then
10316 Set_RM_Size (Rectype, Hbit + 1);
10317 end if;
10318 end if;
10319 end Check_Record_Representation_Clause;
67278d60 10320
d9f6a4ee 10321 ----------------
10322 -- Check_Size --
10323 ----------------
67278d60 10324
d9f6a4ee 10325 procedure Check_Size
10326 (N : Node_Id;
10327 T : Entity_Id;
10328 Siz : Uint;
10329 Biased : out Boolean)
10330 is
10331 UT : constant Entity_Id := Underlying_Type (T);
10332 M : Uint;
67278d60 10333
d9f6a4ee 10334 begin
10335 Biased := False;
67278d60 10336
d9f6a4ee 10337 -- Reject patently improper size values.
67278d60 10338
d9f6a4ee 10339 if Is_Elementary_Type (T)
10340 and then Siz > UI_From_Int (Int'Last)
10341 then
10342 Error_Msg_N ("Size value too large for elementary type", N);
67278d60 10343
d9f6a4ee 10344 if Nkind (Original_Node (N)) = N_Op_Expon then
10345 Error_Msg_N
10346 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10347 end if;
10348 end if;
67278d60 10349
d9f6a4ee 10350 -- Dismiss generic types
67278d60 10351
d9f6a4ee 10352 if Is_Generic_Type (T)
10353 or else
10354 Is_Generic_Type (UT)
10355 or else
10356 Is_Generic_Type (Root_Type (UT))
10357 then
10358 return;
67278d60 10359
d9f6a4ee 10360 -- Guard against previous errors
67278d60 10361
d9f6a4ee 10362 elsif No (UT) or else UT = Any_Type then
10363 Check_Error_Detected;
10364 return;
67278d60 10365
d9f6a4ee 10366 -- Check case of bit packed array
67278d60 10367
d9f6a4ee 10368 elsif Is_Array_Type (UT)
10369 and then Known_Static_Component_Size (UT)
10370 and then Is_Bit_Packed_Array (UT)
10371 then
10372 declare
10373 Asiz : Uint;
10374 Indx : Node_Id;
10375 Ityp : Entity_Id;
67278d60 10376
d9f6a4ee 10377 begin
10378 Asiz := Component_Size (UT);
10379 Indx := First_Index (UT);
10380 loop
10381 Ityp := Etype (Indx);
67278d60 10382
d9f6a4ee 10383 -- If non-static bound, then we are not in the business of
10384 -- trying to check the length, and indeed an error will be
10385 -- issued elsewhere, since sizes of non-static array types
10386 -- cannot be set implicitly or explicitly.
67278d60 10387
cda40848 10388 if not Is_OK_Static_Subtype (Ityp) then
d9f6a4ee 10389 return;
10390 end if;
67278d60 10391
d9f6a4ee 10392 -- Otherwise accumulate next dimension
67278d60 10393
d9f6a4ee 10394 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10395 Expr_Value (Type_Low_Bound (Ityp)) +
10396 Uint_1);
67278d60 10397
d9f6a4ee 10398 Next_Index (Indx);
10399 exit when No (Indx);
10400 end loop;
67278d60 10401
d9f6a4ee 10402 if Asiz <= Siz then
10403 return;
67278d60 10404
d9f6a4ee 10405 else
10406 Error_Msg_Uint_1 := Asiz;
10407 Error_Msg_NE
10408 ("size for& too small, minimum allowed is ^", N, T);
10409 Set_Esize (T, Asiz);
10410 Set_RM_Size (T, Asiz);
10411 end if;
10412 end;
67278d60 10413
d9f6a4ee 10414 -- All other composite types are ignored
67278d60 10415
d9f6a4ee 10416 elsif Is_Composite_Type (UT) then
10417 return;
47495553 10418
d9f6a4ee 10419 -- For fixed-point types, don't check minimum if type is not frozen,
10420 -- since we don't know all the characteristics of the type that can
10421 -- affect the size (e.g. a specified small) till freeze time.
47495553 10422
d9f6a4ee 10423 elsif Is_Fixed_Point_Type (UT)
10424 and then not Is_Frozen (UT)
10425 then
10426 null;
47495553 10427
d9f6a4ee 10428 -- Cases for which a minimum check is required
47495553 10429
d9f6a4ee 10430 else
10431 -- Ignore if specified size is correct for the type
47495553 10432
d9f6a4ee 10433 if Known_Esize (UT) and then Siz = Esize (UT) then
10434 return;
10435 end if;
47495553 10436
d9f6a4ee 10437 -- Otherwise get minimum size
47495553 10438
d9f6a4ee 10439 M := UI_From_Int (Minimum_Size (UT));
47495553 10440
d9f6a4ee 10441 if Siz < M then
47495553 10442
d9f6a4ee 10443 -- Size is less than minimum size, but one possibility remains
10444 -- that we can manage with the new size if we bias the type.
47495553 10445
d9f6a4ee 10446 M := UI_From_Int (Minimum_Size (UT, Biased => True));
47495553 10447
d9f6a4ee 10448 if Siz < M then
10449 Error_Msg_Uint_1 := M;
10450 Error_Msg_NE
10451 ("size for& too small, minimum allowed is ^", N, T);
10452 Set_Esize (T, M);
10453 Set_RM_Size (T, M);
10454 else
10455 Biased := True;
10456 end if;
10457 end if;
10458 end if;
10459 end Check_Size;
47495553 10460
d9f6a4ee 10461 --------------------------
10462 -- Freeze_Entity_Checks --
10463 --------------------------
47495553 10464
d9f6a4ee 10465 procedure Freeze_Entity_Checks (N : Node_Id) is
8cf481c9 10466 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10467 -- Inspect the primitive operations of type Typ and hide all pairs of
3118058b 10468 -- implicitly declared non-overridden non-fully conformant homographs
10469 -- (Ada RM 8.3 12.3/2).
8cf481c9 10470
10471 -------------------------------------
10472 -- Hide_Non_Overridden_Subprograms --
10473 -------------------------------------
10474
10475 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10476 procedure Hide_Matching_Homographs
10477 (Subp_Id : Entity_Id;
10478 Start_Elmt : Elmt_Id);
10479 -- Inspect a list of primitive operations starting with Start_Elmt
3118058b 10480 -- and find matching implicitly declared non-overridden non-fully
10481 -- conformant homographs of Subp_Id. If found, all matches along
10482 -- with Subp_Id are hidden from all visibility.
8cf481c9 10483
10484 function Is_Non_Overridden_Or_Null_Procedure
10485 (Subp_Id : Entity_Id) return Boolean;
10486 -- Determine whether subprogram Subp_Id is implicitly declared non-
10487 -- overridden subprogram or an implicitly declared null procedure.
10488
10489 ------------------------------
10490 -- Hide_Matching_Homographs --
10491 ------------------------------
10492
10493 procedure Hide_Matching_Homographs
10494 (Subp_Id : Entity_Id;
10495 Start_Elmt : Elmt_Id)
10496 is
10497 Prim : Entity_Id;
10498 Prim_Elmt : Elmt_Id;
10499
10500 begin
10501 Prim_Elmt := Start_Elmt;
10502 while Present (Prim_Elmt) loop
10503 Prim := Node (Prim_Elmt);
10504
10505 -- The current primitive is implicitly declared non-overridden
3118058b 10506 -- non-fully conformant homograph of Subp_Id. Both subprograms
10507 -- must be hidden from visibility.
8cf481c9 10508
10509 if Chars (Prim) = Chars (Subp_Id)
8cf481c9 10510 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
3118058b 10511 and then not Fully_Conformant (Prim, Subp_Id)
8cf481c9 10512 then
8c7ee4ac 10513 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10514 Set_Is_Immediately_Visible (Prim, False);
10515 Set_Is_Potentially_Use_Visible (Prim, False);
8cf481c9 10516
8c7ee4ac 10517 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10518 Set_Is_Immediately_Visible (Subp_Id, False);
10519 Set_Is_Potentially_Use_Visible (Subp_Id, False);
8cf481c9 10520 end if;
10521
10522 Next_Elmt (Prim_Elmt);
10523 end loop;
10524 end Hide_Matching_Homographs;
10525
10526 -----------------------------------------
10527 -- Is_Non_Overridden_Or_Null_Procedure --
10528 -----------------------------------------
10529
10530 function Is_Non_Overridden_Or_Null_Procedure
10531 (Subp_Id : Entity_Id) return Boolean
10532 is
10533 Alias_Id : Entity_Id;
10534
10535 begin
10536 -- The subprogram is inherited (implicitly declared), it does not
10537 -- override and does not cover a primitive of an interface.
10538
10539 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10540 and then Present (Alias (Subp_Id))
10541 and then No (Interface_Alias (Subp_Id))
10542 and then No (Overridden_Operation (Subp_Id))
10543 then
10544 Alias_Id := Alias (Subp_Id);
10545
10546 if Requires_Overriding (Alias_Id) then
10547 return True;
10548
10549 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10550 and then Null_Present (Parent (Alias_Id))
10551 then
10552 return True;
10553 end if;
10554 end if;
10555
10556 return False;
10557 end Is_Non_Overridden_Or_Null_Procedure;
10558
10559 -- Local variables
10560
10561 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10562 Prim : Entity_Id;
10563 Prim_Elmt : Elmt_Id;
10564
10565 -- Start of processing for Hide_Non_Overridden_Subprograms
10566
10567 begin
3118058b 10568 -- Inspect the list of primitives looking for non-overridden
10569 -- subprograms.
8cf481c9 10570
10571 if Present (Prim_Ops) then
10572 Prim_Elmt := First_Elmt (Prim_Ops);
10573 while Present (Prim_Elmt) loop
10574 Prim := Node (Prim_Elmt);
10575 Next_Elmt (Prim_Elmt);
10576
10577 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10578 Hide_Matching_Homographs
10579 (Subp_Id => Prim,
10580 Start_Elmt => Prim_Elmt);
10581 end if;
10582 end loop;
10583 end if;
10584 end Hide_Non_Overridden_Subprograms;
10585
10586 ---------------------
10587 -- Local variables --
10588 ---------------------
10589
d9f6a4ee 10590 E : constant Entity_Id := Entity (N);
47495553 10591
d9f6a4ee 10592 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10593 -- True in non-generic case. Some of the processing here is skipped
10594 -- for the generic case since it is not needed. Basically in the
10595 -- generic case, we only need to do stuff that might generate error
10596 -- messages or warnings.
8cf481c9 10597
10598 -- Start of processing for Freeze_Entity_Checks
10599
d9f6a4ee 10600 begin
10601 -- Remember that we are processing a freezing entity. Required to
10602 -- ensure correct decoration of internal entities associated with
10603 -- interfaces (see New_Overloaded_Entity).
47495553 10604
d9f6a4ee 10605 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
47495553 10606
d9f6a4ee 10607 -- For tagged types covering interfaces add internal entities that link
10608 -- the primitives of the interfaces with the primitives that cover them.
10609 -- Note: These entities were originally generated only when generating
10610 -- code because their main purpose was to provide support to initialize
10611 -- the secondary dispatch tables. They are now generated also when
10612 -- compiling with no code generation to provide ASIS the relationship
10613 -- between interface primitives and tagged type primitives. They are
10614 -- also used to locate primitives covering interfaces when processing
10615 -- generics (see Derive_Subprograms).
47495553 10616
d9f6a4ee 10617 -- This is not needed in the generic case
47495553 10618
d9f6a4ee 10619 if Ada_Version >= Ada_2005
10620 and then Non_Generic_Case
10621 and then Ekind (E) = E_Record_Type
10622 and then Is_Tagged_Type (E)
10623 and then not Is_Interface (E)
10624 and then Has_Interfaces (E)
10625 then
10626 -- This would be a good common place to call the routine that checks
10627 -- overriding of interface primitives (and thus factorize calls to
10628 -- Check_Abstract_Overriding located at different contexts in the
10629 -- compiler). However, this is not possible because it causes
10630 -- spurious errors in case of late overriding.
47495553 10631
d9f6a4ee 10632 Add_Internal_Interface_Entities (E);
10633 end if;
47495553 10634
8cf481c9 10635 -- After all forms of overriding have been resolved, a tagged type may
10636 -- be left with a set of implicitly declared and possibly erroneous
10637 -- abstract subprograms, null procedures and subprograms that require
10638 -- overriding. If this set contains fully conformat homographs, then one
10639 -- is chosen arbitrarily (already done during resolution), otherwise all
3118058b 10640 -- remaining non-fully conformant homographs are hidden from visibility
8cf481c9 10641 -- (Ada RM 8.3 12.3/2).
10642
10643 if Is_Tagged_Type (E) then
10644 Hide_Non_Overridden_Subprograms (E);
10645 end if;
10646
d9f6a4ee 10647 -- Check CPP types
47495553 10648
d9f6a4ee 10649 if Ekind (E) = E_Record_Type
10650 and then Is_CPP_Class (E)
10651 and then Is_Tagged_Type (E)
10652 and then Tagged_Type_Expansion
d9f6a4ee 10653 then
10654 if CPP_Num_Prims (E) = 0 then
47495553 10655
d9f6a4ee 10656 -- If the CPP type has user defined components then it must import
10657 -- primitives from C++. This is required because if the C++ class
10658 -- has no primitives then the C++ compiler does not added the _tag
10659 -- component to the type.
47495553 10660
d9f6a4ee 10661 if First_Entity (E) /= Last_Entity (E) then
10662 Error_Msg_N
10663 ("'C'P'P type must import at least one primitive from C++??",
10664 E);
10665 end if;
10666 end if;
47495553 10667
d9f6a4ee 10668 -- Check that all its primitives are abstract or imported from C++.
10669 -- Check also availability of the C++ constructor.
47495553 10670
d9f6a4ee 10671 declare
10672 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10673 Elmt : Elmt_Id;
10674 Error_Reported : Boolean := False;
10675 Prim : Node_Id;
47495553 10676
d9f6a4ee 10677 begin
10678 Elmt := First_Elmt (Primitive_Operations (E));
10679 while Present (Elmt) loop
10680 Prim := Node (Elmt);
47495553 10681
d9f6a4ee 10682 if Comes_From_Source (Prim) then
10683 if Is_Abstract_Subprogram (Prim) then
10684 null;
47495553 10685
d9f6a4ee 10686 elsif not Is_Imported (Prim)
10687 or else Convention (Prim) /= Convention_CPP
10688 then
10689 Error_Msg_N
10690 ("primitives of 'C'P'P types must be imported from C++ "
10691 & "or abstract??", Prim);
47495553 10692
d9f6a4ee 10693 elsif not Has_Constructors
10694 and then not Error_Reported
10695 then
10696 Error_Msg_Name_1 := Chars (E);
10697 Error_Msg_N
10698 ("??'C'P'P constructor required for type %", Prim);
10699 Error_Reported := True;
10700 end if;
10701 end if;
47495553 10702
d9f6a4ee 10703 Next_Elmt (Elmt);
10704 end loop;
10705 end;
10706 end if;
47495553 10707
d9f6a4ee 10708 -- Check Ada derivation of CPP type
47495553 10709
30ab103b 10710 if Expander_Active -- why? losing errors in -gnatc mode???
10711 and then Present (Etype (E)) -- defend against errors
d9f6a4ee 10712 and then Tagged_Type_Expansion
10713 and then Ekind (E) = E_Record_Type
10714 and then Etype (E) /= E
10715 and then Is_CPP_Class (Etype (E))
10716 and then CPP_Num_Prims (Etype (E)) > 0
10717 and then not Is_CPP_Class (E)
10718 and then not Has_CPP_Constructors (Etype (E))
10719 then
10720 -- If the parent has C++ primitives but it has no constructor then
10721 -- check that all the primitives are overridden in this derivation;
10722 -- otherwise the constructor of the parent is needed to build the
10723 -- dispatch table.
47495553 10724
d9f6a4ee 10725 declare
10726 Elmt : Elmt_Id;
10727 Prim : Node_Id;
47495553 10728
10729 begin
d9f6a4ee 10730 Elmt := First_Elmt (Primitive_Operations (E));
10731 while Present (Elmt) loop
10732 Prim := Node (Elmt);
47495553 10733
d9f6a4ee 10734 if not Is_Abstract_Subprogram (Prim)
10735 and then No (Interface_Alias (Prim))
10736 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
47495553 10737 then
d9f6a4ee 10738 Error_Msg_Name_1 := Chars (Etype (E));
10739 Error_Msg_N
10740 ("'C'P'P constructor required for parent type %", E);
10741 exit;
47495553 10742 end if;
d9f6a4ee 10743
10744 Next_Elmt (Elmt);
10745 end loop;
10746 end;
47495553 10747 end if;
10748
d9f6a4ee 10749 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
67278d60 10750
d9f6a4ee 10751 -- If we have a type with predicates, build predicate function. This
0e9014a7 10752 -- is not needed in the generic case, and is not needed within TSS
ea822fd4 10753 -- subprograms and other predefined primitives.
67278d60 10754
ea822fd4 10755 if Non_Generic_Case
10756 and then Is_Type (E)
10757 and then Has_Predicates (E)
10758 and then not Within_Internal_Subprogram
10759 then
d9f6a4ee 10760 Build_Predicate_Functions (E, N);
10761 end if;
67278d60 10762
d9f6a4ee 10763 -- If type has delayed aspects, this is where we do the preanalysis at
10764 -- the freeze point, as part of the consistent visibility check. Note
10765 -- that this must be done after calling Build_Predicate_Functions or
10766 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10767 -- the subtype name in the saved expression so that they will not cause
10768 -- trouble in the preanalysis.
67278d60 10769
d9f6a4ee 10770 -- This is also not needed in the generic case
10771
10772 if Non_Generic_Case
10773 and then Has_Delayed_Aspects (E)
10774 and then Scope (E) = Current_Scope
10775 then
10776 -- Retrieve the visibility to the discriminants in order to properly
10777 -- analyze the aspects.
10778
10779 Push_Scope_And_Install_Discriminants (E);
10780
10781 declare
10782 Ritem : Node_Id;
10783
10784 begin
10785 -- Look for aspect specification entries for this entity
67278d60 10786
d9f6a4ee 10787 Ritem := First_Rep_Item (E);
10788 while Present (Ritem) loop
10789 if Nkind (Ritem) = N_Aspect_Specification
10790 and then Entity (Ritem) = E
10791 and then Is_Delayed_Aspect (Ritem)
10792 then
10793 Check_Aspect_At_Freeze_Point (Ritem);
10794 end if;
67278d60 10795
d9f6a4ee 10796 Next_Rep_Item (Ritem);
10797 end loop;
10798 end;
67278d60 10799
d9f6a4ee 10800 Uninstall_Discriminants_And_Pop_Scope (E);
67278d60 10801 end if;
67278d60 10802
d9f6a4ee 10803 -- For a record type, deal with variant parts. This has to be delayed
d0988351 10804 -- to this point, because of the issue of statically predicated
d9f6a4ee 10805 -- subtypes, which we have to ensure are frozen before checking
10806 -- choices, since we need to have the static choice list set.
d6f39728 10807
d9f6a4ee 10808 if Is_Record_Type (E) then
10809 Check_Variant_Part : declare
10810 D : constant Node_Id := Declaration_Node (E);
10811 T : Node_Id;
10812 C : Node_Id;
10813 VP : Node_Id;
d6f39728 10814
d9f6a4ee 10815 Others_Present : Boolean;
10816 pragma Warnings (Off, Others_Present);
10817 -- Indicates others present, not used in this case
d6f39728 10818
d9f6a4ee 10819 procedure Non_Static_Choice_Error (Choice : Node_Id);
10820 -- Error routine invoked by the generic instantiation below when
10821 -- the variant part has a non static choice.
f117057b 10822
d9f6a4ee 10823 procedure Process_Declarations (Variant : Node_Id);
10824 -- Processes declarations associated with a variant. We analyzed
10825 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10826 -- but we still need the recursive call to Check_Choices for any
10827 -- nested variant to get its choices properly processed. This is
10828 -- also where we expand out the choices if expansion is active.
1f526845 10829
d9f6a4ee 10830 package Variant_Choices_Processing is new
10831 Generic_Check_Choices
10832 (Process_Empty_Choice => No_OP,
10833 Process_Non_Static_Choice => Non_Static_Choice_Error,
10834 Process_Associated_Node => Process_Declarations);
10835 use Variant_Choices_Processing;
f117057b 10836
d9f6a4ee 10837 -----------------------------
10838 -- Non_Static_Choice_Error --
10839 -----------------------------
d6f39728 10840
d9f6a4ee 10841 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10842 begin
10843 Flag_Non_Static_Expr
10844 ("choice given in variant part is not static!", Choice);
10845 end Non_Static_Choice_Error;
d6f39728 10846
d9f6a4ee 10847 --------------------------
10848 -- Process_Declarations --
10849 --------------------------
dba36b60 10850
d9f6a4ee 10851 procedure Process_Declarations (Variant : Node_Id) is
10852 CL : constant Node_Id := Component_List (Variant);
10853 VP : Node_Id;
dba36b60 10854
d9f6a4ee 10855 begin
10856 -- Check for static predicate present in this variant
ea61a7ea 10857
d9f6a4ee 10858 if Has_SP_Choice (Variant) then
ea61a7ea 10859
d9f6a4ee 10860 -- Here we expand. You might expect to find this call in
10861 -- Expand_N_Variant_Part, but that is called when we first
10862 -- see the variant part, and we cannot do this expansion
10863 -- earlier than the freeze point, since for statically
10864 -- predicated subtypes, the predicate is not known till
10865 -- the freeze point.
ea61a7ea 10866
d9f6a4ee 10867 -- Furthermore, we do this expansion even if the expander
10868 -- is not active, because other semantic processing, e.g.
10869 -- for aggregates, requires the expanded list of choices.
ea61a7ea 10870
d9f6a4ee 10871 -- If the expander is not active, then we can't just clobber
10872 -- the list since it would invalidate the ASIS -gnatct tree.
10873 -- So we have to rewrite the variant part with a Rewrite
10874 -- call that replaces it with a copy and clobber the copy.
10875
10876 if not Expander_Active then
10877 declare
10878 NewV : constant Node_Id := New_Copy (Variant);
10879 begin
10880 Set_Discrete_Choices
10881 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10882 Rewrite (Variant, NewV);
10883 end;
10884 end if;
10885
10886 Expand_Static_Predicates_In_Choices (Variant);
ea61a7ea 10887 end if;
10888
d9f6a4ee 10889 -- We don't need to worry about the declarations in the variant
10890 -- (since they were analyzed by Analyze_Choices when we first
10891 -- encountered the variant), but we do need to take care of
10892 -- expansion of any nested variants.
ea61a7ea 10893
d9f6a4ee 10894 if not Null_Present (CL) then
10895 VP := Variant_Part (CL);
ea61a7ea 10896
d9f6a4ee 10897 if Present (VP) then
10898 Check_Choices
10899 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10900 end if;
10901 end if;
10902 end Process_Declarations;
ea61a7ea 10903
d9f6a4ee 10904 -- Start of processing for Check_Variant_Part
b9e61b2a 10905
d9f6a4ee 10906 begin
10907 -- Find component list
ea61a7ea 10908
d9f6a4ee 10909 C := Empty;
ea61a7ea 10910
d9f6a4ee 10911 if Nkind (D) = N_Full_Type_Declaration then
10912 T := Type_Definition (D);
ea61a7ea 10913
d9f6a4ee 10914 if Nkind (T) = N_Record_Definition then
10915 C := Component_List (T);
d6f39728 10916
d9f6a4ee 10917 elsif Nkind (T) = N_Derived_Type_Definition
10918 and then Present (Record_Extension_Part (T))
10919 then
10920 C := Component_List (Record_Extension_Part (T));
10921 end if;
10922 end if;
d6f39728 10923
d9f6a4ee 10924 -- Case of variant part present
d6f39728 10925
d9f6a4ee 10926 if Present (C) and then Present (Variant_Part (C)) then
10927 VP := Variant_Part (C);
ea61a7ea 10928
d9f6a4ee 10929 -- Check choices
ea61a7ea 10930
d9f6a4ee 10931 Check_Choices
10932 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
ea61a7ea 10933
d9f6a4ee 10934 -- If the last variant does not contain the Others choice,
10935 -- replace it with an N_Others_Choice node since Gigi always
10936 -- wants an Others. Note that we do not bother to call Analyze
10937 -- on the modified variant part, since its only effect would be
10938 -- to compute the Others_Discrete_Choices node laboriously, and
10939 -- of course we already know the list of choices corresponding
39a0c1d3 10940 -- to the others choice (it's the list we're replacing).
d6f39728 10941
d9f6a4ee 10942 -- We only want to do this if the expander is active, since
39a0c1d3 10943 -- we do not want to clobber the ASIS tree.
d6f39728 10944
d9f6a4ee 10945 if Expander_Active then
10946 declare
10947 Last_Var : constant Node_Id :=
10948 Last_Non_Pragma (Variants (VP));
d6f39728 10949
d9f6a4ee 10950 Others_Node : Node_Id;
d6f39728 10951
d9f6a4ee 10952 begin
10953 if Nkind (First (Discrete_Choices (Last_Var))) /=
10954 N_Others_Choice
10955 then
10956 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10957 Set_Others_Discrete_Choices
10958 (Others_Node, Discrete_Choices (Last_Var));
10959 Set_Discrete_Choices
10960 (Last_Var, New_List (Others_Node));
10961 end if;
10962 end;
10963 end if;
d6f39728 10964 end if;
d9f6a4ee 10965 end Check_Variant_Part;
d6f39728 10966 end if;
d9f6a4ee 10967 end Freeze_Entity_Checks;
d6f39728 10968
10969 -------------------------
10970 -- Get_Alignment_Value --
10971 -------------------------
10972
10973 function Get_Alignment_Value (Expr : Node_Id) return Uint is
10974 Align : constant Uint := Static_Integer (Expr);
10975
10976 begin
10977 if Align = No_Uint then
10978 return No_Uint;
10979
10980 elsif Align <= 0 then
10981 Error_Msg_N ("alignment value must be positive", Expr);
10982 return No_Uint;
10983
10984 else
10985 for J in Int range 0 .. 64 loop
10986 declare
10987 M : constant Uint := Uint_2 ** J;
10988
10989 begin
10990 exit when M = Align;
10991
10992 if M > Align then
10993 Error_Msg_N
10994 ("alignment value must be power of 2", Expr);
10995 return No_Uint;
10996 end if;
10997 end;
10998 end loop;
10999
11000 return Align;
11001 end if;
11002 end Get_Alignment_Value;
11003
99a2d5bd 11004 -------------------------------------
11005 -- Inherit_Aspects_At_Freeze_Point --
11006 -------------------------------------
11007
11008 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11009 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11010 (Rep_Item : Node_Id) return Boolean;
11011 -- This routine checks if Rep_Item is either a pragma or an aspect
11012 -- specification node whose correponding pragma (if any) is present in
11013 -- the Rep Item chain of the entity it has been specified to.
11014
11015 --------------------------------------------------
11016 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11017 --------------------------------------------------
11018
11019 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11020 (Rep_Item : Node_Id) return Boolean
11021 is
11022 begin
ec6f6da5 11023 return
11024 Nkind (Rep_Item) = N_Pragma
11025 or else Present_In_Rep_Item
11026 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
99a2d5bd 11027 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11028
29a9d4be 11029 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11030
99a2d5bd 11031 begin
11032 -- A representation item is either subtype-specific (Size and Alignment
11033 -- clauses) or type-related (all others). Subtype-specific aspects may
29a9d4be 11034 -- differ for different subtypes of the same type (RM 13.1.8).
99a2d5bd 11035
11036 -- A derived type inherits each type-related representation aspect of
11037 -- its parent type that was directly specified before the declaration of
29a9d4be 11038 -- the derived type (RM 13.1.15).
99a2d5bd 11039
11040 -- A derived subtype inherits each subtype-specific representation
11041 -- aspect of its parent subtype that was directly specified before the
29a9d4be 11042 -- declaration of the derived type (RM 13.1.15).
99a2d5bd 11043
11044 -- The general processing involves inheriting a representation aspect
11045 -- from a parent type whenever the first rep item (aspect specification,
11046 -- attribute definition clause, pragma) corresponding to the given
11047 -- representation aspect in the rep item chain of Typ, if any, isn't
11048 -- directly specified to Typ but to one of its parents.
11049
11050 -- ??? Note that, for now, just a limited number of representation
29a9d4be 11051 -- aspects have been inherited here so far. Many of them are
11052 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11053 -- a non- exhaustive list of aspects that likely also need to
11054 -- be moved to this routine: Alignment, Component_Alignment,
11055 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
99a2d5bd 11056 -- Preelaborable_Initialization, RM_Size and Small.
11057
8b6e9bf2 11058 -- In addition, Convention must be propagated from base type to subtype,
11059 -- because the subtype may have been declared on an incomplete view.
11060
99a2d5bd 11061 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11062 return;
11063 end if;
11064
11065 -- Ada_05/Ada_2005
11066
11067 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11068 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11069 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11070 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11071 then
11072 Set_Is_Ada_2005_Only (Typ);
11073 end if;
11074
11075 -- Ada_12/Ada_2012
11076
11077 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11078 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11079 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11080 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11081 then
11082 Set_Is_Ada_2012_Only (Typ);
11083 end if;
11084
11085 -- Atomic/Shared
11086
11087 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11088 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11089 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11090 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11091 then
11092 Set_Is_Atomic (Typ);
11093 Set_Treat_As_Volatile (Typ);
11094 Set_Is_Volatile (Typ);
11095 end if;
11096
8b6e9bf2 11097 -- Convention
11098
7ac4254e 11099 if Is_Record_Type (Typ)
11100 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11101 then
8b6e9bf2 11102 Set_Convention (Typ, Convention (Base_Type (Typ)));
11103 end if;
11104
29a9d4be 11105 -- Default_Component_Value
99a2d5bd 11106
81c2bc19 11107 -- Verify that there is no rep_item declared for the type, and there
11108 -- is one coming from an ancestor.
11109
99a2d5bd 11110 if Is_Array_Type (Typ)
f3d70f08 11111 and then Is_Base_Type (Typ)
81c2bc19 11112 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
99a2d5bd 11113 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11114 then
11115 Set_Default_Aspect_Component_Value (Typ,
11116 Default_Aspect_Component_Value
11117 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11118 end if;
11119
29a9d4be 11120 -- Default_Value
99a2d5bd 11121
11122 if Is_Scalar_Type (Typ)
f3d70f08 11123 and then Is_Base_Type (Typ)
81c2bc19 11124 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
99a2d5bd 11125 and then Has_Rep_Item (Typ, Name_Default_Value)
11126 then
81c2bc19 11127 Set_Has_Default_Aspect (Typ);
99a2d5bd 11128 Set_Default_Aspect_Value (Typ,
11129 Default_Aspect_Value
11130 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11131 end if;
11132
11133 -- Discard_Names
11134
11135 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11136 and then Has_Rep_Item (Typ, Name_Discard_Names)
11137 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11138 (Get_Rep_Item (Typ, Name_Discard_Names))
11139 then
11140 Set_Discard_Names (Typ);
11141 end if;
11142
11143 -- Invariants
11144
11145 if not Has_Rep_Item (Typ, Name_Invariant, False)
11146 and then Has_Rep_Item (Typ, Name_Invariant)
11147 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11148 (Get_Rep_Item (Typ, Name_Invariant))
11149 then
11150 Set_Has_Invariants (Typ);
11151
11152 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
11153 Set_Has_Inheritable_Invariants (Typ);
11154 end if;
d7487d7d 11155
953378ae 11156 -- If we have a subtype with invariants, whose base type does not have
11157 -- invariants, copy these invariants to the base type. This happens for
11158 -- the case of implicit base types created for scalar and array types.
d7487d7d 11159
953378ae 11160 elsif Has_Invariants (Typ)
d7487d7d 11161 and then not Has_Invariants (Base_Type (Typ))
11162 then
11163 Set_Has_Invariants (Base_Type (Typ));
11164 Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ));
99a2d5bd 11165 end if;
11166
11167 -- Volatile
11168
11169 if not Has_Rep_Item (Typ, Name_Volatile, False)
11170 and then Has_Rep_Item (Typ, Name_Volatile)
11171 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11172 (Get_Rep_Item (Typ, Name_Volatile))
11173 then
11174 Set_Treat_As_Volatile (Typ);
11175 Set_Is_Volatile (Typ);
11176 end if;
11177
2fe893b9 11178 -- Volatile_Full_Access
11179
11180 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11181 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11182 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11183 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11184 then
11185 Set_Has_Volatile_Full_Access (Typ);
11186 Set_Treat_As_Volatile (Typ);
11187 Set_Is_Volatile (Typ);
11188 end if;
11189
99a2d5bd 11190 -- Inheritance for derived types only
11191
11192 if Is_Derived_Type (Typ) then
11193 declare
11194 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11195 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11196
11197 begin
11198 -- Atomic_Components
11199
11200 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11201 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11202 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11203 (Get_Rep_Item (Typ, Name_Atomic_Components))
11204 then
11205 Set_Has_Atomic_Components (Imp_Bas_Typ);
11206 end if;
11207
11208 -- Volatile_Components
11209
11210 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11211 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11212 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11213 (Get_Rep_Item (Typ, Name_Volatile_Components))
11214 then
11215 Set_Has_Volatile_Components (Imp_Bas_Typ);
11216 end if;
11217
e81df51c 11218 -- Finalize_Storage_Only
99a2d5bd 11219
11220 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11221 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11222 then
11223 Set_Finalize_Storage_Only (Bas_Typ);
11224 end if;
11225
11226 -- Universal_Aliasing
11227
11228 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11229 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11230 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11231 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11232 then
11233 Set_Universal_Aliasing (Imp_Bas_Typ);
11234 end if;
11235
e81df51c 11236 -- Bit_Order
99a2d5bd 11237
11238 if Is_Record_Type (Typ) then
99a2d5bd 11239 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11240 and then Has_Rep_Item (Typ, Name_Bit_Order)
11241 then
11242 Set_Reverse_Bit_Order (Bas_Typ,
11243 Reverse_Bit_Order (Entity (Name
11244 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11245 end if;
e81df51c 11246 end if;
11247
e9218716 11248 -- Scalar_Storage_Order
11249
11250 -- Note: the aspect is specified on a first subtype, but recorded
11251 -- in a flag of the base type!
e81df51c 11252
11253 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
29b91bc7 11254 and then Typ = Bas_Typ
e81df51c 11255 then
e81df51c 11256 -- For a type extension, always inherit from parent; otherwise
11257 -- inherit if no default applies. Note: we do not check for
11258 -- an explicit rep item on the parent type when inheriting,
11259 -- because the parent SSO may itself have been set by default.
99a2d5bd 11260
e9218716 11261 if not Has_Rep_Item (First_Subtype (Typ),
11262 Name_Scalar_Storage_Order, False)
e81df51c 11263 and then (Is_Tagged_Type (Bas_Typ)
29b91bc7 11264 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11265 or else
11266 SSO_Set_High_By_Default (Bas_Typ)))
99a2d5bd 11267 then
11268 Set_Reverse_Storage_Order (Bas_Typ,
423b89fd 11269 Reverse_Storage_Order
11270 (Implementation_Base_Type (Etype (Bas_Typ))));
b64082f2 11271
11272 -- Clear default SSO indications, since the inherited aspect
11273 -- which was set explicitly overrides the default.
11274
11275 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11276 Set_SSO_Set_High_By_Default (Bas_Typ, False);
99a2d5bd 11277 end if;
11278 end if;
11279 end;
11280 end if;
11281 end Inherit_Aspects_At_Freeze_Point;
11282
d6f39728 11283 ----------------
11284 -- Initialize --
11285 ----------------
11286
11287 procedure Initialize is
11288 begin
7717ea00 11289 Address_Clause_Checks.Init;
d6f39728 11290 Unchecked_Conversions.Init;
dba38d2f 11291
11292 if VM_Target /= No_VM or else AAMP_On_Target then
11293 Independence_Checks.Init;
11294 end if;
d6f39728 11295 end Initialize;
11296
2625eb01 11297 ---------------------------
11298 -- Install_Discriminants --
11299 ---------------------------
11300
11301 procedure Install_Discriminants (E : Entity_Id) is
11302 Disc : Entity_Id;
11303 Prev : Entity_Id;
11304 begin
11305 Disc := First_Discriminant (E);
11306 while Present (Disc) loop
11307 Prev := Current_Entity (Disc);
11308 Set_Current_Entity (Disc);
11309 Set_Is_Immediately_Visible (Disc);
11310 Set_Homonym (Disc, Prev);
11311 Next_Discriminant (Disc);
11312 end loop;
11313 end Install_Discriminants;
11314
d6f39728 11315 -------------------------
11316 -- Is_Operational_Item --
11317 -------------------------
11318
11319 function Is_Operational_Item (N : Node_Id) return Boolean is
11320 begin
11321 if Nkind (N) /= N_Attribute_Definition_Clause then
11322 return False;
b9e61b2a 11323
d6f39728 11324 else
11325 declare
b9e61b2a 11326 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
d6f39728 11327 begin
b9e61b2a 11328 return Id = Attribute_Input
d6f39728 11329 or else Id = Attribute_Output
11330 or else Id = Attribute_Read
f15731c4 11331 or else Id = Attribute_Write
11332 or else Id = Attribute_External_Tag;
d6f39728 11333 end;
11334 end if;
11335 end Is_Operational_Item;
11336
3b23aaa0 11337 -------------------------
11338 -- Is_Predicate_Static --
11339 -------------------------
11340
94d896aa 11341 -- Note: the basic legality of the expression has already been checked, so
11342 -- we don't need to worry about cases or ranges on strings for example.
11343
3b23aaa0 11344 function Is_Predicate_Static
11345 (Expr : Node_Id;
11346 Nam : Name_Id) return Boolean
11347 is
11348 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
973c2fba 11349 -- Given a list of case expression alternatives, returns True if all
11350 -- the alternatives are static (have all static choices, and a static
11351 -- expression).
3b23aaa0 11352
11353 function All_Static_Choices (L : List_Id) return Boolean;
a360a0f7 11354 -- Returns true if all elements of the list are OK static choices
3b23aaa0 11355 -- as defined below for Is_Static_Choice. Used for case expression
973c2fba 11356 -- alternatives and for the right operand of a membership test. An
11357 -- others_choice is static if the corresponding expression is static.
7c0c95b8 11358 -- The staticness of the bounds is checked separately.
3b23aaa0 11359
11360 function Is_Static_Choice (N : Node_Id) return Boolean;
11361 -- Returns True if N represents a static choice (static subtype, or
a360a0f7 11362 -- static subtype indication, or static expression, or static range).
3b23aaa0 11363 --
11364 -- Note that this is a bit more inclusive than we actually need
11365 -- (in particular membership tests do not allow the use of subtype
a360a0f7 11366 -- indications). But that doesn't matter, we have already checked
3b23aaa0 11367 -- that the construct is legal to get this far.
11368
11369 function Is_Type_Ref (N : Node_Id) return Boolean;
11370 pragma Inline (Is_Type_Ref);
973c2fba 11371 -- Returns True if N is a reference to the type for the predicate in the
11372 -- expression (i.e. if it is an identifier whose Chars field matches the
11373 -- Nam given in the call). N must not be parenthesized, if the type name
11374 -- appears in parens, this routine will return False.
3b23aaa0 11375
11376 ----------------------------------
11377 -- All_Static_Case_Alternatives --
11378 ----------------------------------
11379
11380 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11381 N : Node_Id;
11382
11383 begin
11384 N := First (L);
11385 while Present (N) loop
11386 if not (All_Static_Choices (Discrete_Choices (N))
11387 and then Is_OK_Static_Expression (Expression (N)))
11388 then
11389 return False;
11390 end if;
11391
11392 Next (N);
11393 end loop;
11394
11395 return True;
11396 end All_Static_Case_Alternatives;
11397
11398 ------------------------
11399 -- All_Static_Choices --
11400 ------------------------
11401
11402 function All_Static_Choices (L : List_Id) return Boolean is
11403 N : Node_Id;
11404
11405 begin
11406 N := First (L);
11407 while Present (N) loop
11408 if not Is_Static_Choice (N) then
11409 return False;
11410 end if;
11411
11412 Next (N);
11413 end loop;
11414
11415 return True;
11416 end All_Static_Choices;
11417
11418 ----------------------
11419 -- Is_Static_Choice --
11420 ----------------------
11421
11422 function Is_Static_Choice (N : Node_Id) return Boolean is
11423 begin
7c0c95b8 11424 return Nkind (N) = N_Others_Choice
11425 or else Is_OK_Static_Expression (N)
3b23aaa0 11426 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11427 and then Is_OK_Static_Subtype (Entity (N)))
11428 or else (Nkind (N) = N_Subtype_Indication
11429 and then Is_OK_Static_Subtype (Entity (N)))
11430 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11431 end Is_Static_Choice;
11432
11433 -----------------
11434 -- Is_Type_Ref --
11435 -----------------
11436
11437 function Is_Type_Ref (N : Node_Id) return Boolean is
11438 begin
11439 return Nkind (N) = N_Identifier
11440 and then Chars (N) = Nam
11441 and then Paren_Count (N) = 0;
11442 end Is_Type_Ref;
11443
11444 -- Start of processing for Is_Predicate_Static
11445
11446 begin
3b23aaa0 11447 -- Predicate_Static means one of the following holds. Numbers are the
11448 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11449
11450 -- 16: A static expression
11451
11452 if Is_OK_Static_Expression (Expr) then
11453 return True;
11454
11455 -- 17: A membership test whose simple_expression is the current
11456 -- instance, and whose membership_choice_list meets the requirements
11457 -- for a static membership test.
11458
11459 elsif Nkind (Expr) in N_Membership_Test
11460 and then ((Present (Right_Opnd (Expr))
11461 and then Is_Static_Choice (Right_Opnd (Expr)))
11462 or else
11463 (Present (Alternatives (Expr))
11464 and then All_Static_Choices (Alternatives (Expr))))
11465 then
11466 return True;
11467
11468 -- 18. A case_expression whose selecting_expression is the current
11469 -- instance, and whose dependent expressions are static expressions.
11470
11471 elsif Nkind (Expr) = N_Case_Expression
11472 and then Is_Type_Ref (Expression (Expr))
11473 and then All_Static_Case_Alternatives (Alternatives (Expr))
11474 then
11475 return True;
11476
11477 -- 19. A call to a predefined equality or ordering operator, where one
11478 -- operand is the current instance, and the other is a static
11479 -- expression.
11480
94d896aa 11481 -- Note: the RM is clearly wrong here in not excluding string types.
11482 -- Without this exclusion, we would allow expressions like X > "ABC"
11483 -- to be considered as predicate-static, which is clearly not intended,
11484 -- since the idea is for predicate-static to be a subset of normal
11485 -- static expressions (and "DEF" > "ABC" is not a static expression).
11486
11487 -- However, we do allow internally generated (not from source) equality
11488 -- and inequality operations to be valid on strings (this helps deal
11489 -- with cases where we transform A in "ABC" to A = "ABC).
11490
3b23aaa0 11491 elsif Nkind (Expr) in N_Op_Compare
94d896aa 11492 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11493 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11494 and then not Comes_From_Source (Expr)))
3b23aaa0 11495 and then ((Is_Type_Ref (Left_Opnd (Expr))
11496 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11497 or else
11498 (Is_Type_Ref (Right_Opnd (Expr))
11499 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11500 then
11501 return True;
11502
11503 -- 20. A call to a predefined boolean logical operator, where each
11504 -- operand is predicate-static.
11505
11506 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11507 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11508 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11509 or else
11510 (Nkind (Expr) = N_Op_Not
11511 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11512 then
11513 return True;
11514
11515 -- 21. A short-circuit control form where both operands are
11516 -- predicate-static.
11517
11518 elsif Nkind (Expr) in N_Short_Circuit
11519 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11520 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11521 then
11522 return True;
11523
11524 -- 22. A parenthesized predicate-static expression. This does not
11525 -- require any special test, since we just ignore paren levels in
11526 -- all the cases above.
11527
11528 -- One more test that is an implementation artifact caused by the fact
499918a7 11529 -- that we are analyzing not the original expression, but the generated
3b23aaa0 11530 -- expression in the body of the predicate function. This can include
a360a0f7 11531 -- references to inherited predicates, so that the expression we are
3b23aaa0 11532 -- processing looks like:
11533
11534 -- expression and then xxPredicate (typ (Inns))
11535
11536 -- Where the call is to a Predicate function for an inherited predicate.
60a4a5af 11537 -- We simply ignore such a call, which could be to either a dynamic or
11538 -- a static predicate. Note that if the parent predicate is dynamic then
11539 -- eventually this type will be marked as dynamic, but you are allowed
11540 -- to specify a static predicate for a subtype which is inheriting a
11541 -- dynamic predicate, so the static predicate validation here ignores
11542 -- the inherited predicate even if it is dynamic.
3b23aaa0 11543
11544 elsif Nkind (Expr) = N_Function_Call
11545 and then Is_Predicate_Function (Entity (Name (Expr)))
11546 then
11547 return True;
11548
11549 -- That's an exhaustive list of tests, all other cases are not
a360a0f7 11550 -- predicate-static, so we return False.
3b23aaa0 11551
11552 else
11553 return False;
11554 end if;
11555 end Is_Predicate_Static;
11556
2ff55065 11557 ---------------------
11558 -- Kill_Rep_Clause --
11559 ---------------------
11560
11561 procedure Kill_Rep_Clause (N : Node_Id) is
11562 begin
11563 pragma Assert (Ignore_Rep_Clauses);
360f426f 11564
11565 -- Note: we use Replace rather than Rewrite, because we don't want
11566 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11567 -- rep clause that is being replaced.
11568
4949ddd5 11569 Replace (N, Make_Null_Statement (Sloc (N)));
360f426f 11570
11571 -- The null statement must be marked as not coming from source. This is
37c6552c 11572 -- so that ASIS ignores it, and also the back end does not expect bogus
360f426f 11573 -- "from source" null statements in weird places (e.g. in declarative
11574 -- regions where such null statements are not allowed).
11575
11576 Set_Comes_From_Source (N, False);
2ff55065 11577 end Kill_Rep_Clause;
11578
d6f39728 11579 ------------------
11580 -- Minimum_Size --
11581 ------------------
11582
11583 function Minimum_Size
11584 (T : Entity_Id;
d5b349fa 11585 Biased : Boolean := False) return Nat
d6f39728 11586 is
11587 Lo : Uint := No_Uint;
11588 Hi : Uint := No_Uint;
11589 LoR : Ureal := No_Ureal;
11590 HiR : Ureal := No_Ureal;
11591 LoSet : Boolean := False;
11592 HiSet : Boolean := False;
11593 B : Uint;
11594 S : Nat;
11595 Ancest : Entity_Id;
f15731c4 11596 R_Typ : constant Entity_Id := Root_Type (T);
d6f39728 11597
11598 begin
11599 -- If bad type, return 0
11600
11601 if T = Any_Type then
11602 return 0;
11603
11604 -- For generic types, just return zero. There cannot be any legitimate
11605 -- need to know such a size, but this routine may be called with a
11606 -- generic type as part of normal processing.
11607
f02a9a9a 11608 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
d6f39728 11609 return 0;
11610
74c7ae52 11611 -- Access types (cannot have size smaller than System.Address)
d6f39728 11612
11613 elsif Is_Access_Type (T) then
74c7ae52 11614 return System_Address_Size;
d6f39728 11615
11616 -- Floating-point types
11617
11618 elsif Is_Floating_Point_Type (T) then
f15731c4 11619 return UI_To_Int (Esize (R_Typ));
d6f39728 11620
11621 -- Discrete types
11622
11623 elsif Is_Discrete_Type (T) then
11624
fdd294d1 11625 -- The following loop is looking for the nearest compile time known
11626 -- bounds following the ancestor subtype chain. The idea is to find
11627 -- the most restrictive known bounds information.
d6f39728 11628
11629 Ancest := T;
11630 loop
11631 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11632 return 0;
11633 end if;
11634
11635 if not LoSet then
11636 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11637 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11638 LoSet := True;
11639 exit when HiSet;
11640 end if;
11641 end if;
11642
11643 if not HiSet then
11644 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11645 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11646 HiSet := True;
11647 exit when LoSet;
11648 end if;
11649 end if;
11650
11651 Ancest := Ancestor_Subtype (Ancest);
11652
11653 if No (Ancest) then
11654 Ancest := Base_Type (T);
11655
11656 if Is_Generic_Type (Ancest) then
11657 return 0;
11658 end if;
11659 end if;
11660 end loop;
11661
11662 -- Fixed-point types. We can't simply use Expr_Value to get the
fdd294d1 11663 -- Corresponding_Integer_Value values of the bounds, since these do not
11664 -- get set till the type is frozen, and this routine can be called
11665 -- before the type is frozen. Similarly the test for bounds being static
11666 -- needs to include the case where we have unanalyzed real literals for
11667 -- the same reason.
d6f39728 11668
11669 elsif Is_Fixed_Point_Type (T) then
11670
fdd294d1 11671 -- The following loop is looking for the nearest compile time known
11672 -- bounds following the ancestor subtype chain. The idea is to find
11673 -- the most restrictive known bounds information.
d6f39728 11674
11675 Ancest := T;
11676 loop
11677 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11678 return 0;
11679 end if;
11680
3062c401 11681 -- Note: In the following two tests for LoSet and HiSet, it may
11682 -- seem redundant to test for N_Real_Literal here since normally
11683 -- one would assume that the test for the value being known at
11684 -- compile time includes this case. However, there is a glitch.
11685 -- If the real literal comes from folding a non-static expression,
11686 -- then we don't consider any non- static expression to be known
11687 -- at compile time if we are in configurable run time mode (needed
11688 -- in some cases to give a clearer definition of what is and what
11689 -- is not accepted). So the test is indeed needed. Without it, we
11690 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11691
d6f39728 11692 if not LoSet then
11693 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11694 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11695 then
11696 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11697 LoSet := True;
11698 exit when HiSet;
11699 end if;
11700 end if;
11701
11702 if not HiSet then
11703 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11704 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11705 then
11706 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11707 HiSet := True;
11708 exit when LoSet;
11709 end if;
11710 end if;
11711
11712 Ancest := Ancestor_Subtype (Ancest);
11713
11714 if No (Ancest) then
11715 Ancest := Base_Type (T);
11716
11717 if Is_Generic_Type (Ancest) then
11718 return 0;
11719 end if;
11720 end if;
11721 end loop;
11722
11723 Lo := UR_To_Uint (LoR / Small_Value (T));
11724 Hi := UR_To_Uint (HiR / Small_Value (T));
11725
11726 -- No other types allowed
11727
11728 else
11729 raise Program_Error;
11730 end if;
11731
2866d595 11732 -- Fall through with Hi and Lo set. Deal with biased case
d6f39728 11733
cc46ff4b 11734 if (Biased
11735 and then not Is_Fixed_Point_Type (T)
11736 and then not (Is_Enumeration_Type (T)
11737 and then Has_Non_Standard_Rep (T)))
d6f39728 11738 or else Has_Biased_Representation (T)
11739 then
11740 Hi := Hi - Lo;
11741 Lo := Uint_0;
11742 end if;
11743
005366f7 11744 -- Null range case, size is always zero. We only do this in the discrete
11745 -- type case, since that's the odd case that came up. Probably we should
11746 -- also do this in the fixed-point case, but doing so causes peculiar
11747 -- gigi failures, and it is not worth worrying about this incredibly
11748 -- marginal case (explicit null-range fixed-point type declarations)???
11749
11750 if Lo > Hi and then Is_Discrete_Type (T) then
11751 S := 0;
11752
d6f39728 11753 -- Signed case. Note that we consider types like range 1 .. -1 to be
fdd294d1 11754 -- signed for the purpose of computing the size, since the bounds have
1a34e48c 11755 -- to be accommodated in the base type.
d6f39728 11756
005366f7 11757 elsif Lo < 0 or else Hi < 0 then
d6f39728 11758 S := 1;
11759 B := Uint_1;
11760
da253936 11761 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11762 -- Note that we accommodate the case where the bounds cross. This
d6f39728 11763 -- can happen either because of the way the bounds are declared
11764 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11765
11766 while Lo < -B
11767 or else Hi < -B
11768 or else Lo >= B
11769 or else Hi >= B
11770 loop
11771 B := Uint_2 ** S;
11772 S := S + 1;
11773 end loop;
11774
11775 -- Unsigned case
11776
11777 else
11778 -- If both bounds are positive, make sure that both are represen-
11779 -- table in the case where the bounds are crossed. This can happen
11780 -- either because of the way the bounds are declared, or because of
11781 -- the algorithm in Freeze_Fixed_Point_Type.
11782
11783 if Lo > Hi then
11784 Hi := Lo;
11785 end if;
11786
da253936 11787 -- S = size, (can accommodate 0 .. (2**size - 1))
d6f39728 11788
11789 S := 0;
11790 while Hi >= Uint_2 ** S loop
11791 S := S + 1;
11792 end loop;
11793 end if;
11794
11795 return S;
11796 end Minimum_Size;
11797
44e4341e 11798 ---------------------------
11799 -- New_Stream_Subprogram --
11800 ---------------------------
d6f39728 11801
44e4341e 11802 procedure New_Stream_Subprogram
11803 (N : Node_Id;
11804 Ent : Entity_Id;
11805 Subp : Entity_Id;
11806 Nam : TSS_Name_Type)
d6f39728 11807 is
11808 Loc : constant Source_Ptr := Sloc (N);
9dfe12ae 11809 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
f15731c4 11810 Subp_Id : Entity_Id;
d6f39728 11811 Subp_Decl : Node_Id;
11812 F : Entity_Id;
11813 Etyp : Entity_Id;
11814
44e4341e 11815 Defer_Declaration : constant Boolean :=
11816 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11817 -- For a tagged type, there is a declaration for each stream attribute
11818 -- at the freeze point, and we must generate only a completion of this
11819 -- declaration. We do the same for private types, because the full view
11820 -- might be tagged. Otherwise we generate a declaration at the point of
11821 -- the attribute definition clause.
11822
f15731c4 11823 function Build_Spec return Node_Id;
11824 -- Used for declaration and renaming declaration, so that this is
11825 -- treated as a renaming_as_body.
11826
11827 ----------------
11828 -- Build_Spec --
11829 ----------------
11830
d5b349fa 11831 function Build_Spec return Node_Id is
44e4341e 11832 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11833 Formals : List_Id;
11834 Spec : Node_Id;
83c6c069 11835 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
44e4341e 11836
f15731c4 11837 begin
9dfe12ae 11838 Subp_Id := Make_Defining_Identifier (Loc, Sname);
f15731c4 11839
44e4341e 11840 -- S : access Root_Stream_Type'Class
11841
11842 Formals := New_List (
11843 Make_Parameter_Specification (Loc,
11844 Defining_Identifier =>
11845 Make_Defining_Identifier (Loc, Name_S),
11846 Parameter_Type =>
11847 Make_Access_Definition (Loc,
11848 Subtype_Mark =>
83c6c069 11849 New_Occurrence_Of (
44e4341e 11850 Designated_Type (Etype (F)), Loc))));
11851
11852 if Nam = TSS_Stream_Input then
4bba0a8d 11853 Spec :=
11854 Make_Function_Specification (Loc,
11855 Defining_Unit_Name => Subp_Id,
11856 Parameter_Specifications => Formals,
11857 Result_Definition => T_Ref);
44e4341e 11858 else
11859 -- V : [out] T
f15731c4 11860
44e4341e 11861 Append_To (Formals,
11862 Make_Parameter_Specification (Loc,
11863 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11864 Out_Present => Out_P,
11865 Parameter_Type => T_Ref));
f15731c4 11866
d3ef794c 11867 Spec :=
11868 Make_Procedure_Specification (Loc,
11869 Defining_Unit_Name => Subp_Id,
11870 Parameter_Specifications => Formals);
44e4341e 11871 end if;
f15731c4 11872
44e4341e 11873 return Spec;
11874 end Build_Spec;
d6f39728 11875
44e4341e 11876 -- Start of processing for New_Stream_Subprogram
d6f39728 11877
44e4341e 11878 begin
11879 F := First_Formal (Subp);
11880
11881 if Ekind (Subp) = E_Procedure then
11882 Etyp := Etype (Next_Formal (F));
d6f39728 11883 else
44e4341e 11884 Etyp := Etype (Subp);
d6f39728 11885 end if;
f15731c4 11886
44e4341e 11887 -- Prepare subprogram declaration and insert it as an action on the
11888 -- clause node. The visibility for this entity is used to test for
11889 -- visibility of the attribute definition clause (in the sense of
11890 -- 8.3(23) as amended by AI-195).
9dfe12ae 11891
44e4341e 11892 if not Defer_Declaration then
f15731c4 11893 Subp_Decl :=
11894 Make_Subprogram_Declaration (Loc,
11895 Specification => Build_Spec);
44e4341e 11896
11897 -- For a tagged type, there is always a visible declaration for each
15ebb600 11898 -- stream TSS (it is a predefined primitive operation), and the
44e4341e 11899 -- completion of this declaration occurs at the freeze point, which is
11900 -- not always visible at places where the attribute definition clause is
11901 -- visible. So, we create a dummy entity here for the purpose of
11902 -- tracking the visibility of the attribute definition clause itself.
11903
11904 else
11905 Subp_Id :=
55868293 11906 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
44e4341e 11907 Subp_Decl :=
11908 Make_Object_Declaration (Loc,
11909 Defining_Identifier => Subp_Id,
11910 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
f15731c4 11911 end if;
11912
44e4341e 11913 Insert_Action (N, Subp_Decl);
11914 Set_Entity (N, Subp_Id);
11915
d6f39728 11916 Subp_Decl :=
11917 Make_Subprogram_Renaming_Declaration (Loc,
f15731c4 11918 Specification => Build_Spec,
83c6c069 11919 Name => New_Occurrence_Of (Subp, Loc));
d6f39728 11920
44e4341e 11921 if Defer_Declaration then
d6f39728 11922 Set_TSS (Base_Type (Ent), Subp_Id);
11923 else
11924 Insert_Action (N, Subp_Decl);
11925 Copy_TSS (Subp_Id, Base_Type (Ent));
11926 end if;
44e4341e 11927 end New_Stream_Subprogram;
d6f39728 11928
2625eb01 11929 ------------------------------------------
11930 -- Push_Scope_And_Install_Discriminants --
11931 ------------------------------------------
11932
11933 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
11934 begin
11935 if Has_Discriminants (E) then
11936 Push_Scope (E);
11937
11938 -- Make discriminants visible for type declarations and protected
11939 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
11940
11941 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11942 Install_Discriminants (E);
11943 end if;
11944 end if;
11945 end Push_Scope_And_Install_Discriminants;
11946
d6f39728 11947 ------------------------
11948 -- Rep_Item_Too_Early --
11949 ------------------------
11950
80d4fec4 11951 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
d6f39728 11952 begin
44e4341e 11953 -- Cannot apply non-operational rep items to generic types
d6f39728 11954
f15731c4 11955 if Is_Operational_Item (N) then
11956 return False;
11957
11958 elsif Is_Type (T)
d6f39728 11959 and then Is_Generic_Type (Root_Type (T))
e17c5076 11960 and then (Nkind (N) /= N_Pragma
11961 or else Get_Pragma_Id (N) /= Pragma_Convention)
d6f39728 11962 then
503f7fd3 11963 Error_Msg_N ("representation item not allowed for generic type", N);
d6f39728 11964 return True;
11965 end if;
11966
fdd294d1 11967 -- Otherwise check for incomplete type
d6f39728 11968
11969 if Is_Incomplete_Or_Private_Type (T)
11970 and then No (Underlying_Type (T))
d64221a7 11971 and then
11972 (Nkind (N) /= N_Pragma
60014bc9 11973 or else Get_Pragma_Id (N) /= Pragma_Import)
d6f39728 11974 then
11975 Error_Msg_N
11976 ("representation item must be after full type declaration", N);
11977 return True;
11978
1a34e48c 11979 -- If the type has incomplete components, a representation clause is
d6f39728 11980 -- illegal but stream attributes and Convention pragmas are correct.
11981
11982 elsif Has_Private_Component (T) then
f15731c4 11983 if Nkind (N) = N_Pragma then
d6f39728 11984 return False;
b9e61b2a 11985
d6f39728 11986 else
11987 Error_Msg_N
11988 ("representation item must appear after type is fully defined",
11989 N);
11990 return True;
11991 end if;
11992 else
11993 return False;
11994 end if;
11995 end Rep_Item_Too_Early;
11996
11997 -----------------------
11998 -- Rep_Item_Too_Late --
11999 -----------------------
12000
12001 function Rep_Item_Too_Late
12002 (T : Entity_Id;
12003 N : Node_Id;
d5b349fa 12004 FOnly : Boolean := False) return Boolean
d6f39728 12005 is
12006 S : Entity_Id;
12007 Parent_Type : Entity_Id;
12008
4d0944e9 12009 procedure No_Type_Rep_Item;
12010 -- Output message indicating that no type-related aspects can be
12011 -- specified due to some property of the parent type.
12012
d6f39728 12013 procedure Too_Late;
4d0944e9 12014 -- Output message for an aspect being specified too late
12015
12016 -- Note that neither of the above errors is considered a serious one,
12017 -- since the effect is simply that we ignore the representation clause
12018 -- in these cases.
04d38ee4 12019 -- Is this really true? In any case if we make this change we must
12020 -- document the requirement in the spec of Rep_Item_Too_Late that
12021 -- if True is returned, then the rep item must be completely ignored???
4d0944e9 12022
12023 ----------------------
12024 -- No_Type_Rep_Item --
12025 ----------------------
12026
12027 procedure No_Type_Rep_Item is
12028 begin
12029 Error_Msg_N ("|type-related representation item not permitted!", N);
12030 end No_Type_Rep_Item;
d53a018a 12031
12032 --------------
12033 -- Too_Late --
12034 --------------
d6f39728 12035
12036 procedure Too_Late is
12037 begin
ce4da1ed 12038 -- Other compilers seem more relaxed about rep items appearing too
12039 -- late. Since analysis tools typically don't care about rep items
12040 -- anyway, no reason to be too strict about this.
12041
a9cd517c 12042 if not Relaxed_RM_Semantics then
12043 Error_Msg_N ("|representation item appears too late!", N);
12044 end if;
d6f39728 12045 end Too_Late;
12046
12047 -- Start of processing for Rep_Item_Too_Late
12048
12049 begin
a3248fc4 12050 -- First make sure entity is not frozen (RM 13.1(9))
d6f39728 12051
12052 if Is_Frozen (T)
a3248fc4 12053
12054 -- Exclude imported types, which may be frozen if they appear in a
12055 -- representation clause for a local type.
12056
4aa270d8 12057 and then not From_Limited_With (T)
a3248fc4 12058
a9cd517c 12059 -- Exclude generated entities (not coming from source). The common
a3248fc4 12060 -- case is when we generate a renaming which prematurely freezes the
12061 -- renamed internal entity, but we still want to be able to set copies
12062 -- of attribute values such as Size/Alignment.
12063
12064 and then Comes_From_Source (T)
d6f39728 12065 then
12066 Too_Late;
12067 S := First_Subtype (T);
12068
12069 if Present (Freeze_Node (S)) then
04d38ee4 12070 if not Relaxed_RM_Semantics then
12071 Error_Msg_NE
12072 ("??no more representation items for }", Freeze_Node (S), S);
12073 end if;
d6f39728 12074 end if;
12075
12076 return True;
12077
d1a2e31b 12078 -- Check for case of untagged derived type whose parent either has
4d0944e9 12079 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12080 -- this case we do not output a Too_Late message, since there is no
12081 -- earlier point where the rep item could be placed to make it legal.
d6f39728 12082
12083 elsif Is_Type (T)
12084 and then not FOnly
12085 and then Is_Derived_Type (T)
12086 and then not Is_Tagged_Type (T)
12087 then
12088 Parent_Type := Etype (Base_Type (T));
12089
12090 if Has_Primitive_Operations (Parent_Type) then
4d0944e9 12091 No_Type_Rep_Item;
04d38ee4 12092
12093 if not Relaxed_RM_Semantics then
12094 Error_Msg_NE
12095 ("\parent type & has primitive operations!", N, Parent_Type);
12096 end if;
12097
d6f39728 12098 return True;
12099
12100 elsif Is_By_Reference_Type (Parent_Type) then
4d0944e9 12101 No_Type_Rep_Item;
04d38ee4 12102
12103 if not Relaxed_RM_Semantics then
12104 Error_Msg_NE
12105 ("\parent type & is a by reference type!", N, Parent_Type);
12106 end if;
12107
d6f39728 12108 return True;
12109 end if;
12110 end if;
12111
04d38ee4 12112 -- No error, but one more warning to consider. The RM (surprisingly)
12113 -- allows this pattern:
12114
12115 -- type S is ...
12116 -- primitive operations for S
12117 -- type R is new S;
12118 -- rep clause for S
12119
12120 -- Meaning that calls on the primitive operations of S for values of
12121 -- type R may require possibly expensive implicit conversion operations.
12122 -- This is not an error, but is worth a warning.
12123
12124 if not Relaxed_RM_Semantics and then Is_Type (T) then
12125 declare
12126 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12127
12128 begin
12129 if Present (DTL)
12130 and then Has_Primitive_Operations (Base_Type (T))
12131
12132 -- For now, do not generate this warning for the case of aspect
12133 -- specification using Ada 2012 syntax, since we get wrong
12134 -- messages we do not understand. The whole business of derived
12135 -- types and rep items seems a bit confused when aspects are
12136 -- used, since the aspects are not evaluated till freeze time.
12137
12138 and then not From_Aspect_Specification (N)
12139 then
12140 Error_Msg_Sloc := Sloc (DTL);
12141 Error_Msg_N
12142 ("representation item for& appears after derived type "
12143 & "declaration#??", N);
12144 Error_Msg_NE
12145 ("\may result in implicit conversions for primitive "
12146 & "operations of&??", N, T);
12147 Error_Msg_NE
12148 ("\to change representations when called with arguments "
12149 & "of type&??", N, DTL);
12150 end if;
12151 end;
12152 end if;
12153
3062c401 12154 -- No error, link item into head of chain of rep items for the entity,
12155 -- but avoid chaining if we have an overloadable entity, and the pragma
12156 -- is one that can apply to multiple overloaded entities.
12157
b9e61b2a 12158 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
fdd294d1 12159 declare
12160 Pname : constant Name_Id := Pragma_Name (N);
12161 begin
18393965 12162 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12163 Name_External, Name_Interface)
fdd294d1 12164 then
12165 return False;
12166 end if;
12167 end;
3062c401 12168 end if;
12169
fdd294d1 12170 Record_Rep_Item (T, N);
d6f39728 12171 return False;
12172 end Rep_Item_Too_Late;
12173
2072eaa9 12174 -------------------------------------
12175 -- Replace_Type_References_Generic --
12176 -------------------------------------
12177
37c6552c 12178 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12179 TName : constant Name_Id := Chars (T);
2072eaa9 12180
12181 function Replace_Node (N : Node_Id) return Traverse_Result;
12182 -- Processes a single node in the traversal procedure below, checking
12183 -- if node N should be replaced, and if so, doing the replacement.
12184
12185 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
12186 -- This instantiation provides the body of Replace_Type_References
12187
12188 ------------------
12189 -- Replace_Node --
12190 ------------------
12191
12192 function Replace_Node (N : Node_Id) return Traverse_Result is
12193 S : Entity_Id;
12194 P : Node_Id;
12195
12196 begin
12197 -- Case of identifier
12198
12199 if Nkind (N) = N_Identifier then
12200
37c6552c 12201 -- If not the type name, check whether it is a reference to
12202 -- some other type, which must be frozen before the predicate
12203 -- function is analyzed, i.e. before the freeze node of the
12204 -- type to which the predicate applies.
2072eaa9 12205
12206 if Chars (N) /= TName then
37c6552c 12207 if Present (Current_Entity (N))
12208 and then Is_Type (Current_Entity (N))
12209 then
12210 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12211 end if;
12212
2072eaa9 12213 return Skip;
12214
12215 -- Otherwise do the replacement and we are done with this node
12216
12217 else
12218 Replace_Type_Reference (N);
12219 return Skip;
12220 end if;
12221
12222 -- Case of selected component (which is what a qualification
12223 -- looks like in the unanalyzed tree, which is what we have.
12224
12225 elsif Nkind (N) = N_Selected_Component then
12226
12227 -- If selector name is not our type, keeping going (we might
12228 -- still have an occurrence of the type in the prefix).
12229
12230 if Nkind (Selector_Name (N)) /= N_Identifier
12231 or else Chars (Selector_Name (N)) /= TName
12232 then
12233 return OK;
12234
12235 -- Selector name is our type, check qualification
12236
12237 else
12238 -- Loop through scopes and prefixes, doing comparison
12239
12240 S := Current_Scope;
12241 P := Prefix (N);
12242 loop
12243 -- Continue if no more scopes or scope with no name
12244
12245 if No (S) or else Nkind (S) not in N_Has_Chars then
12246 return OK;
12247 end if;
12248
12249 -- Do replace if prefix is an identifier matching the
12250 -- scope that we are currently looking at.
12251
12252 if Nkind (P) = N_Identifier
12253 and then Chars (P) = Chars (S)
12254 then
12255 Replace_Type_Reference (N);
12256 return Skip;
12257 end if;
12258
12259 -- Go check scope above us if prefix is itself of the
12260 -- form of a selected component, whose selector matches
12261 -- the scope we are currently looking at.
12262
12263 if Nkind (P) = N_Selected_Component
12264 and then Nkind (Selector_Name (P)) = N_Identifier
12265 and then Chars (Selector_Name (P)) = Chars (S)
12266 then
12267 S := Scope (S);
12268 P := Prefix (P);
12269
12270 -- For anything else, we don't have a match, so keep on
12271 -- going, there are still some weird cases where we may
12272 -- still have a replacement within the prefix.
12273
12274 else
12275 return OK;
12276 end if;
12277 end loop;
12278 end if;
12279
ec6f6da5 12280 -- Continue for any other node kind
2072eaa9 12281
12282 else
12283 return OK;
12284 end if;
12285 end Replace_Node;
12286
12287 begin
12288 Replace_Type_Refs (N);
12289 end Replace_Type_References_Generic;
12290
d6f39728 12291 -------------------------
12292 -- Same_Representation --
12293 -------------------------
12294
12295 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12296 T1 : constant Entity_Id := Underlying_Type (Typ1);
12297 T2 : constant Entity_Id := Underlying_Type (Typ2);
12298
12299 begin
12300 -- A quick check, if base types are the same, then we definitely have
12301 -- the same representation, because the subtype specific representation
12302 -- attributes (Size and Alignment) do not affect representation from
12303 -- the point of view of this test.
12304
12305 if Base_Type (T1) = Base_Type (T2) then
12306 return True;
12307
12308 elsif Is_Private_Type (Base_Type (T2))
12309 and then Base_Type (T1) = Full_View (Base_Type (T2))
12310 then
12311 return True;
12312 end if;
12313
12314 -- Tagged types never have differing representations
12315
12316 if Is_Tagged_Type (T1) then
12317 return True;
12318 end if;
12319
12320 -- Representations are definitely different if conventions differ
12321
12322 if Convention (T1) /= Convention (T2) then
12323 return False;
12324 end if;
12325
ef0772bc 12326 -- Representations are different if component alignments or scalar
12327 -- storage orders differ.
d6f39728 12328
12329 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
726fd56a 12330 and then
d6f39728 12331 (Is_Record_Type (T2) or else Is_Array_Type (T2))
ef0772bc 12332 and then
12333 (Component_Alignment (T1) /= Component_Alignment (T2)
f02a9a9a 12334 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
d6f39728 12335 then
12336 return False;
12337 end if;
12338
12339 -- For arrays, the only real issue is component size. If we know the
12340 -- component size for both arrays, and it is the same, then that's
12341 -- good enough to know we don't have a change of representation.
12342
12343 if Is_Array_Type (T1) then
12344 if Known_Component_Size (T1)
12345 and then Known_Component_Size (T2)
12346 and then Component_Size (T1) = Component_Size (T2)
12347 then
9f1130cc 12348 if VM_Target = No_VM then
12349 return True;
12350
12351 -- In VM targets the representation of arrays with aliased
12352 -- components differs from arrays with non-aliased components
12353
12354 else
12355 return Has_Aliased_Components (Base_Type (T1))
0ba3592b 12356 =
12357 Has_Aliased_Components (Base_Type (T2));
9f1130cc 12358 end if;
d6f39728 12359 end if;
12360 end if;
12361
12362 -- Types definitely have same representation if neither has non-standard
12363 -- representation since default representations are always consistent.
12364 -- If only one has non-standard representation, and the other does not,
12365 -- then we consider that they do not have the same representation. They
12366 -- might, but there is no way of telling early enough.
12367
12368 if Has_Non_Standard_Rep (T1) then
12369 if not Has_Non_Standard_Rep (T2) then
12370 return False;
12371 end if;
12372 else
12373 return not Has_Non_Standard_Rep (T2);
12374 end if;
12375
fdd294d1 12376 -- Here the two types both have non-standard representation, and we need
12377 -- to determine if they have the same non-standard representation.
d6f39728 12378
12379 -- For arrays, we simply need to test if the component sizes are the
12380 -- same. Pragma Pack is reflected in modified component sizes, so this
12381 -- check also deals with pragma Pack.
12382
12383 if Is_Array_Type (T1) then
12384 return Component_Size (T1) = Component_Size (T2);
12385
12386 -- Tagged types always have the same representation, because it is not
12387 -- possible to specify different representations for common fields.
12388
12389 elsif Is_Tagged_Type (T1) then
12390 return True;
12391
12392 -- Case of record types
12393
12394 elsif Is_Record_Type (T1) then
12395
12396 -- Packed status must conform
12397
12398 if Is_Packed (T1) /= Is_Packed (T2) then
12399 return False;
12400
12401 -- Otherwise we must check components. Typ2 maybe a constrained
12402 -- subtype with fewer components, so we compare the components
12403 -- of the base types.
12404
12405 else
12406 Record_Case : declare
12407 CD1, CD2 : Entity_Id;
12408
12409 function Same_Rep return Boolean;
12410 -- CD1 and CD2 are either components or discriminants. This
ef0772bc 12411 -- function tests whether they have the same representation.
d6f39728 12412
80d4fec4 12413 --------------
12414 -- Same_Rep --
12415 --------------
12416
d6f39728 12417 function Same_Rep return Boolean is
12418 begin
12419 if No (Component_Clause (CD1)) then
12420 return No (Component_Clause (CD2));
d6f39728 12421 else
ef0772bc 12422 -- Note: at this point, component clauses have been
12423 -- normalized to the default bit order, so that the
12424 -- comparison of Component_Bit_Offsets is meaningful.
12425
d6f39728 12426 return
12427 Present (Component_Clause (CD2))
12428 and then
12429 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12430 and then
12431 Esize (CD1) = Esize (CD2);
12432 end if;
12433 end Same_Rep;
12434
1e35409d 12435 -- Start of processing for Record_Case
d6f39728 12436
12437 begin
12438 if Has_Discriminants (T1) then
d6f39728 12439
9dfe12ae 12440 -- The number of discriminants may be different if the
12441 -- derived type has fewer (constrained by values). The
12442 -- invisible discriminants retain the representation of
12443 -- the original, so the discrepancy does not per se
12444 -- indicate a different representation.
12445
b9e61b2a 12446 CD1 := First_Discriminant (T1);
12447 CD2 := First_Discriminant (T2);
12448 while Present (CD1) and then Present (CD2) loop
d6f39728 12449 if not Same_Rep then
12450 return False;
12451 else
12452 Next_Discriminant (CD1);
12453 Next_Discriminant (CD2);
12454 end if;
12455 end loop;
12456 end if;
12457
12458 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12459 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
d6f39728 12460 while Present (CD1) loop
12461 if not Same_Rep then
12462 return False;
12463 else
12464 Next_Component (CD1);
12465 Next_Component (CD2);
12466 end if;
12467 end loop;
12468
12469 return True;
12470 end Record_Case;
12471 end if;
12472
12473 -- For enumeration types, we must check each literal to see if the
12474 -- representation is the same. Note that we do not permit enumeration
1a34e48c 12475 -- representation clauses for Character and Wide_Character, so these
d6f39728 12476 -- cases were already dealt with.
12477
12478 elsif Is_Enumeration_Type (T1) then
d6f39728 12479 Enumeration_Case : declare
12480 L1, L2 : Entity_Id;
12481
12482 begin
12483 L1 := First_Literal (T1);
12484 L2 := First_Literal (T2);
d6f39728 12485 while Present (L1) loop
12486 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12487 return False;
12488 else
12489 Next_Literal (L1);
12490 Next_Literal (L2);
12491 end if;
12492 end loop;
12493
12494 return True;
d6f39728 12495 end Enumeration_Case;
12496
12497 -- Any other types have the same representation for these purposes
12498
12499 else
12500 return True;
12501 end if;
d6f39728 12502 end Same_Representation;
12503
3061ffde 12504 --------------------------------
12505 -- Resolve_Iterable_Operation --
12506 --------------------------------
12507
12508 procedure Resolve_Iterable_Operation
12509 (N : Node_Id;
12510 Cursor : Entity_Id;
12511 Typ : Entity_Id;
12512 Nam : Name_Id)
12513 is
12514 Ent : Entity_Id;
12515 F1 : Entity_Id;
12516 F2 : Entity_Id;
12517
12518 begin
12519 if not Is_Overloaded (N) then
12520 if not Is_Entity_Name (N)
12521 or else Ekind (Entity (N)) /= E_Function
12522 or else Scope (Entity (N)) /= Scope (Typ)
12523 or else No (First_Formal (Entity (N)))
12524 or else Etype (First_Formal (Entity (N))) /= Typ
12525 then
12526 Error_Msg_N ("iterable primitive must be local function name "
12527 & "whose first formal is an iterable type", N);
a9f5fea7 12528 return;
3061ffde 12529 end if;
12530
12531 Ent := Entity (N);
12532 F1 := First_Formal (Ent);
12533 if Nam = Name_First then
12534
12535 -- First (Container) => Cursor
12536
12537 if Etype (Ent) /= Cursor then
12538 Error_Msg_N ("primitive for First must yield a curosr", N);
12539 end if;
12540
12541 elsif Nam = Name_Next then
12542
12543 -- Next (Container, Cursor) => Cursor
12544
12545 F2 := Next_Formal (F1);
12546
12547 if Etype (F2) /= Cursor
12548 or else Etype (Ent) /= Cursor
12549 or else Present (Next_Formal (F2))
12550 then
12551 Error_Msg_N ("no match for Next iterable primitive", N);
12552 end if;
12553
12554 elsif Nam = Name_Has_Element then
12555
12556 -- Has_Element (Container, Cursor) => Boolean
12557
12558 F2 := Next_Formal (F1);
12559 if Etype (F2) /= Cursor
12560 or else Etype (Ent) /= Standard_Boolean
12561 or else Present (Next_Formal (F2))
12562 then
12563 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12564 end if;
12565
12566 elsif Nam = Name_Element then
b9b03799 12567 F2 := Next_Formal (F1);
12568
12569 if No (F2)
12570 or else Etype (F2) /= Cursor
12571 or else Present (Next_Formal (F2))
12572 then
12573 Error_Msg_N ("no match for Element iterable primitive", N);
12574 end if;
3061ffde 12575 null;
12576
12577 else
12578 raise Program_Error;
12579 end if;
12580
12581 else
12582 -- Overloaded case: find subprogram with proper signature.
12583 -- Caller will report error if no match is found.
12584
12585 declare
12586 I : Interp_Index;
12587 It : Interp;
12588
12589 begin
12590 Get_First_Interp (N, I, It);
12591 while Present (It.Typ) loop
12592 if Ekind (It.Nam) = E_Function
b9b03799 12593 and then Scope (It.Nam) = Scope (Typ)
3061ffde 12594 and then Etype (First_Formal (It.Nam)) = Typ
12595 then
12596 F1 := First_Formal (It.Nam);
12597
12598 if Nam = Name_First then
12599 if Etype (It.Nam) = Cursor
12600 and then No (Next_Formal (F1))
12601 then
12602 Set_Entity (N, It.Nam);
12603 exit;
12604 end if;
12605
12606 elsif Nam = Name_Next then
12607 F2 := Next_Formal (F1);
12608
12609 if Present (F2)
12610 and then No (Next_Formal (F2))
12611 and then Etype (F2) = Cursor
12612 and then Etype (It.Nam) = Cursor
12613 then
12614 Set_Entity (N, It.Nam);
12615 exit;
12616 end if;
12617
12618 elsif Nam = Name_Has_Element then
12619 F2 := Next_Formal (F1);
12620
12621 if Present (F2)
12622 and then No (Next_Formal (F2))
12623 and then Etype (F2) = Cursor
12624 and then Etype (It.Nam) = Standard_Boolean
12625 then
12626 Set_Entity (N, It.Nam);
12627 F2 := Next_Formal (F1);
12628 exit;
12629 end if;
12630
12631 elsif Nam = Name_Element then
b9b03799 12632 F2 := Next_Formal (F1);
12633
3061ffde 12634 if Present (F2)
12635 and then No (Next_Formal (F2))
12636 and then Etype (F2) = Cursor
12637 then
12638 Set_Entity (N, It.Nam);
12639 exit;
12640 end if;
12641 end if;
12642 end if;
12643
12644 Get_Next_Interp (I, It);
12645 end loop;
12646 end;
12647 end if;
12648 end Resolve_Iterable_Operation;
12649
b77e4501 12650 ----------------
12651 -- Set_Biased --
12652 ----------------
12653
12654 procedure Set_Biased
12655 (E : Entity_Id;
12656 N : Node_Id;
12657 Msg : String;
12658 Biased : Boolean := True)
12659 is
12660 begin
12661 if Biased then
12662 Set_Has_Biased_Representation (E);
12663
12664 if Warn_On_Biased_Representation then
12665 Error_Msg_NE
1e3532e7 12666 ("?B?" & Msg & " forces biased representation for&", N, E);
b77e4501 12667 end if;
12668 end if;
12669 end Set_Biased;
12670
d6f39728 12671 --------------------
12672 -- Set_Enum_Esize --
12673 --------------------
12674
12675 procedure Set_Enum_Esize (T : Entity_Id) is
12676 Lo : Uint;
12677 Hi : Uint;
12678 Sz : Nat;
12679
12680 begin
12681 Init_Alignment (T);
12682
12683 -- Find the minimum standard size (8,16,32,64) that fits
12684
12685 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12686 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12687
12688 if Lo < 0 then
12689 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
f15731c4 12690 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 12691
12692 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
12693 Sz := 16;
12694
12695 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
12696 Sz := 32;
12697
12698 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
12699 Sz := 64;
12700 end if;
12701
12702 else
12703 if Hi < Uint_2**08 then
f15731c4 12704 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 12705
12706 elsif Hi < Uint_2**16 then
12707 Sz := 16;
12708
12709 elsif Hi < Uint_2**32 then
12710 Sz := 32;
12711
12712 else pragma Assert (Hi < Uint_2**63);
12713 Sz := 64;
12714 end if;
12715 end if;
12716
12717 -- That minimum is the proper size unless we have a foreign convention
12718 -- and the size required is 32 or less, in which case we bump the size
12719 -- up to 32. This is required for C and C++ and seems reasonable for
12720 -- all other foreign conventions.
12721
12722 if Has_Foreign_Convention (T)
12723 and then Esize (T) < Standard_Integer_Size
db1eed69 12724
12725 -- Don't do this if Short_Enums on target
12726
e9185b9d 12727 and then not Target_Short_Enums
d6f39728 12728 then
12729 Init_Esize (T, Standard_Integer_Size);
d6f39728 12730 else
12731 Init_Esize (T, Sz);
12732 end if;
d6f39728 12733 end Set_Enum_Esize;
12734
2625eb01 12735 -----------------------------
12736 -- Uninstall_Discriminants --
12737 -----------------------------
12738
12739 procedure Uninstall_Discriminants (E : Entity_Id) is
12740 Disc : Entity_Id;
12741 Prev : Entity_Id;
12742 Outer : Entity_Id;
12743
12744 begin
12745 -- Discriminants have been made visible for type declarations and
12746 -- protected type declarations, not for subtype declarations.
12747
12748 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12749 Disc := First_Discriminant (E);
12750 while Present (Disc) loop
12751 if Disc /= Current_Entity (Disc) then
12752 Prev := Current_Entity (Disc);
12753 while Present (Prev)
12754 and then Present (Homonym (Prev))
12755 and then Homonym (Prev) /= Disc
12756 loop
12757 Prev := Homonym (Prev);
12758 end loop;
12759 else
12760 Prev := Empty;
12761 end if;
12762
12763 Set_Is_Immediately_Visible (Disc, False);
12764
12765 Outer := Homonym (Disc);
12766 while Present (Outer) and then Scope (Outer) = E loop
12767 Outer := Homonym (Outer);
12768 end loop;
12769
12770 -- Reset homonym link of other entities, but do not modify link
12771 -- between entities in current scope, so that the back-end can
12772 -- have a proper count of local overloadings.
12773
12774 if No (Prev) then
12775 Set_Name_Entity_Id (Chars (Disc), Outer);
12776
12777 elsif Scope (Prev) /= Scope (Disc) then
12778 Set_Homonym (Prev, Outer);
12779 end if;
12780
12781 Next_Discriminant (Disc);
12782 end loop;
12783 end if;
12784 end Uninstall_Discriminants;
12785
12786 -------------------------------------------
12787 -- Uninstall_Discriminants_And_Pop_Scope --
12788 -------------------------------------------
12789
12790 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
12791 begin
12792 if Has_Discriminants (E) then
12793 Uninstall_Discriminants (E);
12794 Pop_Scope;
12795 end if;
12796 end Uninstall_Discriminants_And_Pop_Scope;
12797
83f8f0a6 12798 ------------------------------
12799 -- Validate_Address_Clauses --
12800 ------------------------------
12801
12802 procedure Validate_Address_Clauses is
12803 begin
12804 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
12805 declare
12806 ACCR : Address_Clause_Check_Record
12807 renames Address_Clause_Checks.Table (J);
12808
d6da7448 12809 Expr : Node_Id;
12810
83f8f0a6 12811 X_Alignment : Uint;
12812 Y_Alignment : Uint;
12813
12814 X_Size : Uint;
12815 Y_Size : Uint;
12816
12817 begin
12818 -- Skip processing of this entry if warning already posted
12819
12820 if not Address_Warning_Posted (ACCR.N) then
d6da7448 12821 Expr := Original_Node (Expression (ACCR.N));
83f8f0a6 12822
d6da7448 12823 -- Get alignments
83f8f0a6 12824
d6da7448 12825 X_Alignment := Alignment (ACCR.X);
12826 Y_Alignment := Alignment (ACCR.Y);
83f8f0a6 12827
12828 -- Similarly obtain sizes
12829
d6da7448 12830 X_Size := Esize (ACCR.X);
12831 Y_Size := Esize (ACCR.Y);
83f8f0a6 12832
12833 -- Check for large object overlaying smaller one
12834
12835 if Y_Size > Uint_0
12836 and then X_Size > Uint_0
12837 and then X_Size > Y_Size
12838 then
d6da7448 12839 Error_Msg_NE
1581f2d7 12840 ("??& overlays smaller object", ACCR.N, ACCR.X);
83f8f0a6 12841 Error_Msg_N
1e3532e7 12842 ("\??program execution may be erroneous", ACCR.N);
83f8f0a6 12843 Error_Msg_Uint_1 := X_Size;
12844 Error_Msg_NE
1e3532e7 12845 ("\??size of & is ^", ACCR.N, ACCR.X);
83f8f0a6 12846 Error_Msg_Uint_1 := Y_Size;
12847 Error_Msg_NE
1e3532e7 12848 ("\??size of & is ^", ACCR.N, ACCR.Y);
83f8f0a6 12849
d6da7448 12850 -- Check for inadequate alignment, both of the base object
e556831e 12851 -- and of the offset, if any. We only do this check if the
12852 -- run-time Alignment_Check is active. No point in warning
12853 -- if this check has been suppressed (or is suppressed by
12854 -- default in the non-strict alignment machine case).
83f8f0a6 12855
d6da7448 12856 -- Note: we do not check the alignment if we gave a size
12857 -- warning, since it would likely be redundant.
83f8f0a6 12858
e556831e 12859 elsif not Alignment_Checks_Suppressed (ACCR.Y)
12860 and then Y_Alignment /= Uint_0
d6da7448 12861 and then (Y_Alignment < X_Alignment
12862 or else (ACCR.Off
12863 and then
12864 Nkind (Expr) = N_Attribute_Reference
12865 and then
12866 Attribute_Name (Expr) = Name_Address
12867 and then
12868 Has_Compatible_Alignment
12869 (ACCR.X, Prefix (Expr))
12870 /= Known_Compatible))
83f8f0a6 12871 then
12872 Error_Msg_NE
1e3532e7 12873 ("??specified address for& may be inconsistent "
12874 & "with alignment", ACCR.N, ACCR.X);
83f8f0a6 12875 Error_Msg_N
1e3532e7 12876 ("\??program execution may be erroneous (RM 13.3(27))",
83f8f0a6 12877 ACCR.N);
12878 Error_Msg_Uint_1 := X_Alignment;
12879 Error_Msg_NE
1e3532e7 12880 ("\??alignment of & is ^", ACCR.N, ACCR.X);
83f8f0a6 12881 Error_Msg_Uint_1 := Y_Alignment;
12882 Error_Msg_NE
1e3532e7 12883 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
d6da7448 12884 if Y_Alignment >= X_Alignment then
12885 Error_Msg_N
1e3532e7 12886 ("\??but offset is not multiple of alignment", ACCR.N);
d6da7448 12887 end if;
83f8f0a6 12888 end if;
12889 end if;
12890 end;
12891 end loop;
12892 end Validate_Address_Clauses;
12893
7717ea00 12894 ---------------------------
12895 -- Validate_Independence --
12896 ---------------------------
12897
12898 procedure Validate_Independence is
12899 SU : constant Uint := UI_From_Int (System_Storage_Unit);
12900 N : Node_Id;
12901 E : Entity_Id;
12902 IC : Boolean;
12903 Comp : Entity_Id;
12904 Addr : Node_Id;
12905 P : Node_Id;
12906
12907 procedure Check_Array_Type (Atyp : Entity_Id);
12908 -- Checks if the array type Atyp has independent components, and
12909 -- if not, outputs an appropriate set of error messages.
12910
12911 procedure No_Independence;
12912 -- Output message that independence cannot be guaranteed
12913
12914 function OK_Component (C : Entity_Id) return Boolean;
12915 -- Checks one component to see if it is independently accessible, and
12916 -- if so yields True, otherwise yields False if independent access
12917 -- cannot be guaranteed. This is a conservative routine, it only
12918 -- returns True if it knows for sure, it returns False if it knows
12919 -- there is a problem, or it cannot be sure there is no problem.
12920
12921 procedure Reason_Bad_Component (C : Entity_Id);
12922 -- Outputs continuation message if a reason can be determined for
12923 -- the component C being bad.
12924
12925 ----------------------
12926 -- Check_Array_Type --
12927 ----------------------
12928
12929 procedure Check_Array_Type (Atyp : Entity_Id) is
12930 Ctyp : constant Entity_Id := Component_Type (Atyp);
12931
12932 begin
12933 -- OK if no alignment clause, no pack, and no component size
12934
12935 if not Has_Component_Size_Clause (Atyp)
12936 and then not Has_Alignment_Clause (Atyp)
12937 and then not Is_Packed (Atyp)
12938 then
12939 return;
12940 end if;
12941
aa0a69ab 12942 -- Case of component size is greater than or equal to 64 and the
12943 -- alignment of the array is at least as large as the alignment
12944 -- of the component. We are definitely OK in this situation.
12945
12946 if Known_Component_Size (Atyp)
12947 and then Component_Size (Atyp) >= 64
12948 and then Known_Alignment (Atyp)
12949 and then Known_Alignment (Ctyp)
12950 and then Alignment (Atyp) >= Alignment (Ctyp)
12951 then
12952 return;
12953 end if;
12954
7717ea00 12955 -- Check actual component size
12956
12957 if not Known_Component_Size (Atyp)
12958 or else not (Addressable (Component_Size (Atyp))
aa0a69ab 12959 and then Component_Size (Atyp) < 64)
7717ea00 12960 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
12961 then
12962 No_Independence;
12963
12964 -- Bad component size, check reason
12965
12966 if Has_Component_Size_Clause (Atyp) then
b9e61b2a 12967 P := Get_Attribute_Definition_Clause
12968 (Atyp, Attribute_Component_Size);
7717ea00 12969
12970 if Present (P) then
12971 Error_Msg_Sloc := Sloc (P);
12972 Error_Msg_N ("\because of Component_Size clause#", N);
12973 return;
12974 end if;
12975 end if;
12976
12977 if Is_Packed (Atyp) then
12978 P := Get_Rep_Pragma (Atyp, Name_Pack);
12979
12980 if Present (P) then
12981 Error_Msg_Sloc := Sloc (P);
12982 Error_Msg_N ("\because of pragma Pack#", N);
12983 return;
12984 end if;
12985 end if;
12986
12987 -- No reason found, just return
12988
12989 return;
12990 end if;
12991
12992 -- Array type is OK independence-wise
12993
12994 return;
12995 end Check_Array_Type;
12996
12997 ---------------------
12998 -- No_Independence --
12999 ---------------------
13000
13001 procedure No_Independence is
13002 begin
13003 if Pragma_Name (N) = Name_Independent then
18393965 13004 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
7717ea00 13005 else
13006 Error_Msg_NE
13007 ("independent components cannot be guaranteed for&", N, E);
13008 end if;
13009 end No_Independence;
13010
13011 ------------------
13012 -- OK_Component --
13013 ------------------
13014
13015 function OK_Component (C : Entity_Id) return Boolean is
13016 Rec : constant Entity_Id := Scope (C);
13017 Ctyp : constant Entity_Id := Etype (C);
13018
13019 begin
13020 -- OK if no component clause, no Pack, and no alignment clause
13021
13022 if No (Component_Clause (C))
13023 and then not Is_Packed (Rec)
13024 and then not Has_Alignment_Clause (Rec)
13025 then
13026 return True;
13027 end if;
13028
13029 -- Here we look at the actual component layout. A component is
13030 -- addressable if its size is a multiple of the Esize of the
13031 -- component type, and its starting position in the record has
13032 -- appropriate alignment, and the record itself has appropriate
13033 -- alignment to guarantee the component alignment.
13034
13035 -- Make sure sizes are static, always assume the worst for any
13036 -- cases where we cannot check static values.
13037
13038 if not (Known_Static_Esize (C)
b9e61b2a 13039 and then
13040 Known_Static_Esize (Ctyp))
7717ea00 13041 then
13042 return False;
13043 end if;
13044
13045 -- Size of component must be addressable or greater than 64 bits
13046 -- and a multiple of bytes.
13047
b9e61b2a 13048 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
7717ea00 13049 return False;
13050 end if;
13051
13052 -- Check size is proper multiple
13053
13054 if Esize (C) mod Esize (Ctyp) /= 0 then
13055 return False;
13056 end if;
13057
13058 -- Check alignment of component is OK
13059
13060 if not Known_Component_Bit_Offset (C)
13061 or else Component_Bit_Offset (C) < Uint_0
13062 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13063 then
13064 return False;
13065 end if;
13066
13067 -- Check alignment of record type is OK
13068
13069 if not Known_Alignment (Rec)
13070 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13071 then
13072 return False;
13073 end if;
13074
13075 -- All tests passed, component is addressable
13076
13077 return True;
13078 end OK_Component;
13079
13080 --------------------------
13081 -- Reason_Bad_Component --
13082 --------------------------
13083
13084 procedure Reason_Bad_Component (C : Entity_Id) is
13085 Rec : constant Entity_Id := Scope (C);
13086 Ctyp : constant Entity_Id := Etype (C);
13087
13088 begin
13089 -- If component clause present assume that's the problem
13090
13091 if Present (Component_Clause (C)) then
13092 Error_Msg_Sloc := Sloc (Component_Clause (C));
13093 Error_Msg_N ("\because of Component_Clause#", N);
13094 return;
13095 end if;
13096
13097 -- If pragma Pack clause present, assume that's the problem
13098
13099 if Is_Packed (Rec) then
13100 P := Get_Rep_Pragma (Rec, Name_Pack);
13101
13102 if Present (P) then
13103 Error_Msg_Sloc := Sloc (P);
13104 Error_Msg_N ("\because of pragma Pack#", N);
13105 return;
13106 end if;
13107 end if;
13108
13109 -- See if record has bad alignment clause
13110
13111 if Has_Alignment_Clause (Rec)
13112 and then Known_Alignment (Rec)
13113 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13114 then
13115 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13116
13117 if Present (P) then
13118 Error_Msg_Sloc := Sloc (P);
13119 Error_Msg_N ("\because of Alignment clause#", N);
13120 end if;
13121 end if;
13122
13123 -- Couldn't find a reason, so return without a message
13124
13125 return;
13126 end Reason_Bad_Component;
13127
13128 -- Start of processing for Validate_Independence
13129
13130 begin
13131 for J in Independence_Checks.First .. Independence_Checks.Last loop
13132 N := Independence_Checks.Table (J).N;
13133 E := Independence_Checks.Table (J).E;
13134 IC := Pragma_Name (N) = Name_Independent_Components;
13135
13136 -- Deal with component case
13137
13138 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13139 if not OK_Component (E) then
13140 No_Independence;
13141 Reason_Bad_Component (E);
13142 goto Continue;
13143 end if;
13144 end if;
13145
13146 -- Deal with record with Independent_Components
13147
13148 if IC and then Is_Record_Type (E) then
13149 Comp := First_Component_Or_Discriminant (E);
13150 while Present (Comp) loop
13151 if not OK_Component (Comp) then
13152 No_Independence;
13153 Reason_Bad_Component (Comp);
13154 goto Continue;
13155 end if;
13156
13157 Next_Component_Or_Discriminant (Comp);
13158 end loop;
13159 end if;
13160
13161 -- Deal with address clause case
13162
13163 if Is_Object (E) then
13164 Addr := Address_Clause (E);
13165
13166 if Present (Addr) then
13167 No_Independence;
13168 Error_Msg_Sloc := Sloc (Addr);
13169 Error_Msg_N ("\because of Address clause#", N);
13170 goto Continue;
13171 end if;
13172 end if;
13173
13174 -- Deal with independent components for array type
13175
13176 if IC and then Is_Array_Type (E) then
13177 Check_Array_Type (E);
13178 end if;
13179
13180 -- Deal with independent components for array object
13181
13182 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13183 Check_Array_Type (Etype (E));
13184 end if;
13185
13186 <<Continue>> null;
13187 end loop;
13188 end Validate_Independence;
13189
b3f8228a 13190 ------------------------------
13191 -- Validate_Iterable_Aspect --
13192 ------------------------------
13193
13194 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
3061ffde 13195 Assoc : Node_Id;
13196 Expr : Node_Id;
b3f8228a 13197
bde03454 13198 Prim : Node_Id;
a9f5fea7 13199 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
b3f8228a 13200
13201 First_Id : Entity_Id;
13202 Next_Id : Entity_Id;
13203 Has_Element_Id : Entity_Id;
13204 Element_Id : Entity_Id;
13205
b3f8228a 13206 begin
9698629c 13207 -- If previous error aspect is unusable
a9f5fea7 13208
13209 if Cursor = Any_Type then
3061ffde 13210 return;
13211 end if;
b3f8228a 13212
13213 First_Id := Empty;
13214 Next_Id := Empty;
13215 Has_Element_Id := Empty;
32de816b 13216 Element_Id := Empty;
b3f8228a 13217
13218 -- Each expression must resolve to a function with the proper signature
13219
13220 Assoc := First (Component_Associations (Expression (ASN)));
13221 while Present (Assoc) loop
13222 Expr := Expression (Assoc);
13223 Analyze (Expr);
13224
b3f8228a 13225 Prim := First (Choices (Assoc));
bde03454 13226
f02a9a9a 13227 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
b3f8228a 13228 Error_Msg_N ("illegal name in association", Prim);
13229
13230 elsif Chars (Prim) = Name_First then
3061ffde 13231 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
b3f8228a 13232 First_Id := Entity (Expr);
b3f8228a 13233
13234 elsif Chars (Prim) = Name_Next then
3061ffde 13235 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
b3f8228a 13236 Next_Id := Entity (Expr);
b3f8228a 13237
13238 elsif Chars (Prim) = Name_Has_Element then
3061ffde 13239 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
b3f8228a 13240 Has_Element_Id := Entity (Expr);
bde03454 13241
b3f8228a 13242 elsif Chars (Prim) = Name_Element then
3061ffde 13243 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
b3f8228a 13244 Element_Id := Entity (Expr);
b3f8228a 13245
13246 else
13247 Error_Msg_N ("invalid name for iterable function", Prim);
13248 end if;
13249
13250 Next (Assoc);
13251 end loop;
13252
13253 if No (First_Id) then
3061ffde 13254 Error_Msg_N ("match for First primitive not found", ASN);
b3f8228a 13255
13256 elsif No (Next_Id) then
3061ffde 13257 Error_Msg_N ("match for Next primitive not found", ASN);
b3f8228a 13258
13259 elsif No (Has_Element_Id) then
3061ffde 13260 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13261
13262 elsif No (Element_Id) then
13263 null; -- Optional.
b3f8228a 13264 end if;
13265 end Validate_Iterable_Aspect;
13266
d6f39728 13267 -----------------------------------
13268 -- Validate_Unchecked_Conversion --
13269 -----------------------------------
13270
13271 procedure Validate_Unchecked_Conversion
13272 (N : Node_Id;
13273 Act_Unit : Entity_Id)
13274 is
13275 Source : Entity_Id;
13276 Target : Entity_Id;
13277 Vnode : Node_Id;
13278
13279 begin
13280 -- Obtain source and target types. Note that we call Ancestor_Subtype
13281 -- here because the processing for generic instantiation always makes
13282 -- subtypes, and we want the original frozen actual types.
13283
13284 -- If we are dealing with private types, then do the check on their
13285 -- fully declared counterparts if the full declarations have been
39a0c1d3 13286 -- encountered (they don't have to be visible, but they must exist).
d6f39728 13287
13288 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13289
13290 if Is_Private_Type (Source)
13291 and then Present (Underlying_Type (Source))
13292 then
13293 Source := Underlying_Type (Source);
13294 end if;
13295
13296 Target := Ancestor_Subtype (Etype (Act_Unit));
13297
fdd294d1 13298 -- If either type is generic, the instantiation happens within a generic
95deda50 13299 -- unit, and there is nothing to check. The proper check will happen
13300 -- when the enclosing generic is instantiated.
d6f39728 13301
13302 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13303 return;
13304 end if;
13305
13306 if Is_Private_Type (Target)
13307 and then Present (Underlying_Type (Target))
13308 then
13309 Target := Underlying_Type (Target);
13310 end if;
13311
13312 -- Source may be unconstrained array, but not target
13313
b9e61b2a 13314 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
d6f39728 13315 Error_Msg_N
13316 ("unchecked conversion to unconstrained array not allowed", N);
13317 return;
13318 end if;
13319
fbc67f84 13320 -- Warn if conversion between two different convention pointers
13321
13322 if Is_Access_Type (Target)
13323 and then Is_Access_Type (Source)
13324 and then Convention (Target) /= Convention (Source)
13325 and then Warn_On_Unchecked_Conversion
13326 then
74c7ae52 13327 -- Give warnings for subprogram pointers only on most targets
fdd294d1 13328
13329 if Is_Access_Subprogram_Type (Target)
13330 or else Is_Access_Subprogram_Type (Source)
fdd294d1 13331 then
13332 Error_Msg_N
cb97ae5c 13333 ("?z?conversion between pointers with different conventions!",
1e3532e7 13334 N);
fdd294d1 13335 end if;
fbc67f84 13336 end if;
13337
3062c401 13338 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13339 -- warning when compiling GNAT-related sources.
13340
13341 if Warn_On_Unchecked_Conversion
13342 and then not In_Predefined_Unit (N)
13343 and then RTU_Loaded (Ada_Calendar)
f02a9a9a 13344 and then (Chars (Source) = Name_Time
13345 or else
13346 Chars (Target) = Name_Time)
3062c401 13347 then
13348 -- If Ada.Calendar is loaded and the name of one of the operands is
13349 -- Time, there is a good chance that this is Ada.Calendar.Time.
13350
13351 declare
f02a9a9a 13352 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
3062c401 13353 begin
13354 pragma Assert (Present (Calendar_Time));
13355
b9e61b2a 13356 if Source = Calendar_Time or else Target = Calendar_Time then
3062c401 13357 Error_Msg_N
f02a9a9a 13358 ("?z?representation of 'Time values may change between "
13359 & "'G'N'A'T versions", N);
3062c401 13360 end if;
13361 end;
13362 end if;
13363
fdd294d1 13364 -- Make entry in unchecked conversion table for later processing by
13365 -- Validate_Unchecked_Conversions, which will check sizes and alignments
13366 -- (using values set by the back-end where possible). This is only done
13367 -- if the appropriate warning is active.
d6f39728 13368
9dfe12ae 13369 if Warn_On_Unchecked_Conversion then
13370 Unchecked_Conversions.Append
86d32751 13371 (New_Val => UC_Entry'(Eloc => Sloc (N),
13372 Source => Source,
13373 Target => Target,
13374 Act_Unit => Act_Unit));
9dfe12ae 13375
13376 -- If both sizes are known statically now, then back end annotation
13377 -- is not required to do a proper check but if either size is not
13378 -- known statically, then we need the annotation.
13379
13380 if Known_Static_RM_Size (Source)
1e3532e7 13381 and then
13382 Known_Static_RM_Size (Target)
9dfe12ae 13383 then
13384 null;
13385 else
13386 Back_Annotate_Rep_Info := True;
13387 end if;
13388 end if;
d6f39728 13389
fdd294d1 13390 -- If unchecked conversion to access type, and access type is declared
95deda50 13391 -- in the same unit as the unchecked conversion, then set the flag
13392 -- No_Strict_Aliasing (no strict aliasing is implicit here)
28ed91d4 13393
13394 if Is_Access_Type (Target) and then
13395 In_Same_Source_Unit (Target, N)
13396 then
13397 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13398 end if;
3d875462 13399
95deda50 13400 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13401 -- the back end needs to perform special validation checks.
3d875462 13402
95deda50 13403 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13404 -- have full expansion and the back end is called ???
3d875462 13405
13406 Vnode :=
13407 Make_Validate_Unchecked_Conversion (Sloc (N));
13408 Set_Source_Type (Vnode, Source);
13409 Set_Target_Type (Vnode, Target);
13410
fdd294d1 13411 -- If the unchecked conversion node is in a list, just insert before it.
13412 -- If not we have some strange case, not worth bothering about.
3d875462 13413
13414 if Is_List_Member (N) then
d6f39728 13415 Insert_After (N, Vnode);
13416 end if;
13417 end Validate_Unchecked_Conversion;
13418
13419 ------------------------------------
13420 -- Validate_Unchecked_Conversions --
13421 ------------------------------------
13422
13423 procedure Validate_Unchecked_Conversions is
13424 begin
13425 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13426 declare
13427 T : UC_Entry renames Unchecked_Conversions.Table (N);
13428
86d32751 13429 Eloc : constant Source_Ptr := T.Eloc;
13430 Source : constant Entity_Id := T.Source;
13431 Target : constant Entity_Id := T.Target;
13432 Act_Unit : constant Entity_Id := T.Act_Unit;
d6f39728 13433
44705307 13434 Source_Siz : Uint;
13435 Target_Siz : Uint;
d6f39728 13436
13437 begin
86d32751 13438 -- Skip if function marked as warnings off
13439
13440 if Warnings_Off (Act_Unit) then
13441 goto Continue;
13442 end if;
13443
fdd294d1 13444 -- This validation check, which warns if we have unequal sizes for
13445 -- unchecked conversion, and thus potentially implementation
d6f39728 13446 -- dependent semantics, is one of the few occasions on which we
fdd294d1 13447 -- use the official RM size instead of Esize. See description in
13448 -- Einfo "Handling of Type'Size Values" for details.
d6f39728 13449
f15731c4 13450 if Serious_Errors_Detected = 0
d6f39728 13451 and then Known_Static_RM_Size (Source)
13452 and then Known_Static_RM_Size (Target)
f25f4252 13453
13454 -- Don't do the check if warnings off for either type, note the
13455 -- deliberate use of OR here instead of OR ELSE to get the flag
13456 -- Warnings_Off_Used set for both types if appropriate.
13457
13458 and then not (Has_Warnings_Off (Source)
13459 or
13460 Has_Warnings_Off (Target))
d6f39728 13461 then
13462 Source_Siz := RM_Size (Source);
13463 Target_Siz := RM_Size (Target);
13464
13465 if Source_Siz /= Target_Siz then
299480f9 13466 Error_Msg
cb97ae5c 13467 ("?z?types for unchecked conversion have different sizes!",
299480f9 13468 Eloc);
d6f39728 13469
13470 if All_Errors_Mode then
13471 Error_Msg_Name_1 := Chars (Source);
13472 Error_Msg_Uint_1 := Source_Siz;
13473 Error_Msg_Name_2 := Chars (Target);
13474 Error_Msg_Uint_2 := Target_Siz;
cb97ae5c 13475 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
d6f39728 13476
13477 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13478
13479 if Is_Discrete_Type (Source)
b9e61b2a 13480 and then
13481 Is_Discrete_Type (Target)
d6f39728 13482 then
13483 if Source_Siz > Target_Siz then
299480f9 13484 Error_Msg
cb97ae5c 13485 ("\?z?^ high order bits of source will "
1e3532e7 13486 & "be ignored!", Eloc);
d6f39728 13487
9dfe12ae 13488 elsif Is_Unsigned_Type (Source) then
299480f9 13489 Error_Msg
cb97ae5c 13490 ("\?z?source will be extended with ^ high order "
1581f2d7 13491 & "zero bits!", Eloc);
d6f39728 13492
13493 else
299480f9 13494 Error_Msg
cb97ae5c 13495 ("\?z?source will be extended with ^ high order "
1e3532e7 13496 & "sign bits!", Eloc);
d6f39728 13497 end if;
13498
13499 elsif Source_Siz < Target_Siz then
13500 if Is_Discrete_Type (Target) then
13501 if Bytes_Big_Endian then
299480f9 13502 Error_Msg
cb97ae5c 13503 ("\?z?target value will include ^ undefined "
1e3532e7 13504 & "low order bits!", Eloc);
d6f39728 13505 else
299480f9 13506 Error_Msg
cb97ae5c 13507 ("\?z?target value will include ^ undefined "
1e3532e7 13508 & "high order bits!", Eloc);
d6f39728 13509 end if;
13510
13511 else
299480f9 13512 Error_Msg
cb97ae5c 13513 ("\?z?^ trailing bits of target value will be "
1e3532e7 13514 & "undefined!", Eloc);
d6f39728 13515 end if;
13516
13517 else pragma Assert (Source_Siz > Target_Siz);
0388e54e 13518 if Is_Discrete_Type (Source) then
13519 if Bytes_Big_Endian then
13520 Error_Msg
13521 ("\?z?^ low order bits of source will be "
13522 & "ignored!", Eloc);
13523 else
13524 Error_Msg
13525 ("\?z?^ high order bits of source will be "
13526 & "ignored!", Eloc);
13527 end if;
13528
13529 else
13530 Error_Msg
13531 ("\?z?^ trailing bits of source will be "
13532 & "ignored!", Eloc);
13533 end if;
d6f39728 13534 end if;
13535 end if;
d6f39728 13536 end if;
13537 end if;
13538
13539 -- If both types are access types, we need to check the alignment.
13540 -- If the alignment of both is specified, we can do it here.
13541
f15731c4 13542 if Serious_Errors_Detected = 0
2a10e737 13543 and then Is_Access_Type (Source)
13544 and then Is_Access_Type (Target)
d6f39728 13545 and then Target_Strict_Alignment
13546 and then Present (Designated_Type (Source))
13547 and then Present (Designated_Type (Target))
13548 then
13549 declare
13550 D_Source : constant Entity_Id := Designated_Type (Source);
13551 D_Target : constant Entity_Id := Designated_Type (Target);
13552
13553 begin
13554 if Known_Alignment (D_Source)
b9e61b2a 13555 and then
13556 Known_Alignment (D_Target)
d6f39728 13557 then
13558 declare
13559 Source_Align : constant Uint := Alignment (D_Source);
13560 Target_Align : constant Uint := Alignment (D_Target);
13561
13562 begin
13563 if Source_Align < Target_Align
13564 and then not Is_Tagged_Type (D_Source)
f25f4252 13565
13566 -- Suppress warning if warnings suppressed on either
13567 -- type or either designated type. Note the use of
13568 -- OR here instead of OR ELSE. That is intentional,
13569 -- we would like to set flag Warnings_Off_Used in
13570 -- all types for which warnings are suppressed.
13571
13572 and then not (Has_Warnings_Off (D_Source)
13573 or
13574 Has_Warnings_Off (D_Target)
13575 or
13576 Has_Warnings_Off (Source)
13577 or
13578 Has_Warnings_Off (Target))
d6f39728 13579 then
d6f39728 13580 Error_Msg_Uint_1 := Target_Align;
13581 Error_Msg_Uint_2 := Source_Align;
299480f9 13582 Error_Msg_Node_1 := D_Target;
d6f39728 13583 Error_Msg_Node_2 := D_Source;
299480f9 13584 Error_Msg
cb97ae5c 13585 ("?z?alignment of & (^) is stricter than "
1e3532e7 13586 & "alignment of & (^)!", Eloc);
f25f4252 13587 Error_Msg
cb97ae5c 13588 ("\?z?resulting access value may have invalid "
1e3532e7 13589 & "alignment!", Eloc);
d6f39728 13590 end if;
13591 end;
13592 end if;
13593 end;
13594 end if;
13595 end;
86d32751 13596
13597 <<Continue>>
13598 null;
d6f39728 13599 end loop;
13600 end Validate_Unchecked_Conversions;
13601
d6f39728 13602end Sem_Ch13;