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