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