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