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