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