]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch13.adb
2016-07-04 Gary Dismukes <dismukes@adacore.com>
[thirdparty/gcc.git] / gcc / ada / sem_ch13.adb
CommitLineData
d6f39728 1------------------------------------------------------------------------------
7189d17f 2-- --
d6f39728 3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 1 3 --
6-- --
7-- B o d y --
8-- --
9c20237a 9-- Copyright (C) 1992-2016, 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;
f9e26ff7 37with Ghost; use Ghost;
d6f39728 38with Lib; use Lib;
83f8f0a6 39with Lib.Xref; use Lib.Xref;
15ebb600 40with Namet; use Namet;
d6f39728 41with Nlists; use Nlists;
42with Nmake; use Nmake;
43with Opt; use Opt;
e0521a36 44with Restrict; use Restrict;
45with Rident; use Rident;
d6f39728 46with Rtsfind; use Rtsfind;
47with Sem; use Sem;
d60c9ff7 48with Sem_Aux; use Sem_Aux;
be9124d0 49with Sem_Case; use Sem_Case;
40ca69b9 50with Sem_Ch3; use Sem_Ch3;
490beba6 51with Sem_Ch6; use Sem_Ch6;
d6f39728 52with Sem_Ch8; use Sem_Ch8;
85696508 53with Sem_Dim; use Sem_Dim;
85377c9b 54with Sem_Disp; use Sem_Disp;
d6f39728 55with Sem_Eval; use Sem_Eval;
51ea9c94 56with Sem_Prag; use Sem_Prag;
d6f39728 57with Sem_Res; use Sem_Res;
58with Sem_Type; use Sem_Type;
59with Sem_Util; use Sem_Util;
44e4341e 60with Sem_Warn; use Sem_Warn;
1e3c4ae6 61with Sinput; use Sinput;
9dfe12ae 62with Snames; use Snames;
d6f39728 63with Stand; use Stand;
64with Sinfo; use Sinfo;
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
ee2b7923 103 function Build_Export_Import_Pragma
104 (Asp : Node_Id;
105 Id : Entity_Id) return Node_Id;
106 -- Create the corresponding pragma for aspect Export or Import denoted by
107 -- Asp. Id is the related entity subject to the aspect. Return Empty when
108 -- the expression of aspect Asp evaluates to False or is erroneous.
109
9c20237a 110 function Build_Predicate_Function_Declaration
111 (Typ : Entity_Id) return Node_Id;
112 -- Build the declaration for a predicate function. The declaration is built
113 -- at the end of the declarative part containing the type definition, which
114 -- may be before the freeze point of the type. The predicate expression is
115 -- pre-analyzed at this point, to catch visibility errors.
116
eb66e842 117 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
118 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
119 -- then either there are pragma Predicate entries on the rep chain for the
120 -- type (note that Predicate aspects are converted to pragma Predicate), or
121 -- there are inherited aspects from a parent type, or ancestor subtypes.
9c20237a 122 -- This procedure builds body for the Predicate function that tests these
123 -- predicates. N is the freeze node for the type. The spec of the function
124 -- is inserted before the freeze node, and the body of the function is
125 -- inserted after the freeze node. If the predicate expression has a least
126 -- one Raise_Expression, then this procedure also builds the M version of
127 -- the predicate function for use in membership tests.
eb66e842 128
6653b695 129 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
130 -- Called if both Storage_Pool and Storage_Size attribute definition
131 -- clauses (SP and SS) are present for entity Ent. Issue error message.
132
d9f6a4ee 133 procedure Freeze_Entity_Checks (N : Node_Id);
134 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
135 -- to generate appropriate semantic checks that are delayed until this
136 -- point (they had to be delayed this long for cases of delayed aspects,
137 -- e.g. analysis of statically predicated subtypes in choices, for which
5f067114 138 -- we have to be sure the subtypes in question are frozen before checking).
d9f6a4ee 139
d6f39728 140 function Get_Alignment_Value (Expr : Node_Id) return Uint;
141 -- Given the expression for an alignment value, returns the corresponding
142 -- Uint value. If the value is inappropriate, then error messages are
143 -- posted as required, and a value of No_Uint is returned.
144
ee2b7923 145 procedure Get_Interfacing_Aspects
146 (Iface_Asp : Node_Id;
147 Conv_Asp : out Node_Id;
148 EN_Asp : out Node_Id;
149 Expo_Asp : out Node_Id;
150 Imp_Asp : out Node_Id;
151 LN_Asp : out Node_Id;
152 Do_Checks : Boolean := False);
153 -- Given a single interfacing aspect Iface_Asp, retrieve other interfacing
154 -- aspects that apply to the same related entity. The aspects considered by
155 -- this routine are as follows:
156 --
157 -- Conv_Asp - aspect Convention
158 -- EN_Asp - aspect External_Name
159 -- Expo_Asp - aspect Export
160 -- Imp_Asp - aspect Import
161 -- LN_Asp - aspect Link_Name
162 --
163 -- When flag Do_Checks is set, this routine will flag duplicate uses of
164 -- aspects.
165
d6f39728 166 function Is_Operational_Item (N : Node_Id) return Boolean;
1e3c4ae6 167 -- A specification for a stream attribute is allowed before the full type
168 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
169 -- that do not specify a representation characteristic are operational
170 -- attributes.
d6f39728 171
3b23aaa0 172 function Is_Predicate_Static
173 (Expr : Node_Id;
174 Nam : Name_Id) return Boolean;
175 -- Given predicate expression Expr, tests if Expr is predicate-static in
176 -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type
177 -- name in the predicate expression have been replaced by references to
178 -- an identifier whose Chars field is Nam. This name is unique, so any
179 -- identifier with Chars matching Nam must be a reference to the type.
180 -- Returns True if the expression is predicate-static and False otherwise,
181 -- but is not in the business of setting flags or issuing error messages.
182 --
183 -- Only scalar types can have static predicates, so False is always
184 -- returned for non-scalar types.
185 --
186 -- Note: the RM seems to suggest that string types can also have static
187 -- predicates. But that really makes lttle sense as very few useful
188 -- predicates can be constructed for strings. Remember that:
189 --
190 -- "ABC" < "DEF"
191 --
192 -- is not a static expression. So even though the clearly faulty RM wording
193 -- allows the following:
194 --
195 -- subtype S is String with Static_Predicate => S < "DEF"
196 --
197 -- We can't allow this, otherwise we have predicate-static applying to a
198 -- larger class than static expressions, which was never intended.
199
44e4341e 200 procedure New_Stream_Subprogram
d6f39728 201 (N : Node_Id;
202 Ent : Entity_Id;
203 Subp : Entity_Id;
9dfe12ae 204 Nam : TSS_Name_Type);
44e4341e 205 -- Create a subprogram renaming of a given stream attribute to the
206 -- designated subprogram and then in the tagged case, provide this as a
d1a2e31b 207 -- primitive operation, or in the untagged case make an appropriate TSS
44e4341e 208 -- entry. This is more properly an expansion activity than just semantics,
d1a2e31b 209 -- but the presence of user-defined stream functions for limited types
210 -- is a legality check, which is why this takes place here rather than in
44e4341e 211 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
212 -- function to be generated.
9dfe12ae 213 --
f15731c4 214 -- To avoid elaboration anomalies with freeze nodes, for untagged types
215 -- we generate both a subprogram declaration and a subprogram renaming
216 -- declaration, so that the attribute specification is handled as a
217 -- renaming_as_body. For tagged types, the specification is one of the
218 -- primitive specs.
219
3061ffde 220 procedure Resolve_Iterable_Operation
221 (N : Node_Id;
222 Cursor : Entity_Id;
223 Typ : Entity_Id;
224 Nam : Name_Id);
225 -- If the name of a primitive operation for an Iterable aspect is
226 -- overloaded, resolve according to required signature.
227
b77e4501 228 procedure Set_Biased
229 (E : Entity_Id;
230 N : Node_Id;
231 Msg : String;
232 Biased : Boolean := True);
233 -- If Biased is True, sets Has_Biased_Representation flag for E, and
234 -- outputs a warning message at node N if Warn_On_Biased_Representation is
235 -- is True. This warning inserts the string Msg to describe the construct
236 -- causing biasing.
237
d6f39728 238 ----------------------------------------------
239 -- Table for Validate_Unchecked_Conversions --
240 ----------------------------------------------
241
242 -- The following table collects unchecked conversions for validation.
95deda50 243 -- Entries are made by Validate_Unchecked_Conversion and then the call
244 -- to Validate_Unchecked_Conversions does the actual error checking and
245 -- posting of warnings. The reason for this delayed processing is to take
246 -- advantage of back-annotations of size and alignment values performed by
247 -- the back end.
d6f39728 248
95deda50 249 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
250 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
251 -- already have modified all Sloc values if the -gnatD option is set.
299480f9 252
d6f39728 253 type UC_Entry is record
86d32751 254 Eloc : Source_Ptr; -- node used for posting warnings
255 Source : Entity_Id; -- source type for unchecked conversion
256 Target : Entity_Id; -- target type for unchecked conversion
257 Act_Unit : Entity_Id; -- actual function instantiated
d6f39728 258 end record;
259
260 package Unchecked_Conversions is new Table.Table (
261 Table_Component_Type => UC_Entry,
262 Table_Index_Type => Int,
263 Table_Low_Bound => 1,
264 Table_Initial => 50,
265 Table_Increment => 200,
266 Table_Name => "Unchecked_Conversions");
267
83f8f0a6 268 ----------------------------------------
269 -- Table for Validate_Address_Clauses --
270 ----------------------------------------
271
272 -- If an address clause has the form
273
274 -- for X'Address use Expr
275
514a5555 276 -- where Expr has a value known at compile time or is of the form Y'Address
277 -- or recursively is a reference to a constant initialized with either of
278 -- these forms, and the value of Expr is not a multiple of X's alignment,
279 -- or if Y has a smaller alignment than X, then that merits a warning about
95deda50 280 -- possible bad alignment. The following table collects address clauses of
281 -- this kind. We put these in a table so that they can be checked after the
282 -- back end has completed annotation of the alignments of objects, since we
283 -- can catch more cases that way.
83f8f0a6 284
285 type Address_Clause_Check_Record is record
286 N : Node_Id;
287 -- The address clause
288
289 X : Entity_Id;
514a5555 290 -- The entity of the object subject to the address clause
291
292 A : Uint;
293 -- The value of the address in the first case
83f8f0a6 294
295 Y : Entity_Id;
514a5555 296 -- The entity of the object being overlaid in the second case
d6da7448 297
298 Off : Boolean;
514a5555 299 -- Whether the address is offset within Y in the second case
83f8f0a6 300 end record;
301
302 package Address_Clause_Checks is new Table.Table (
303 Table_Component_Type => Address_Clause_Check_Record,
304 Table_Index_Type => Int,
305 Table_Low_Bound => 1,
306 Table_Initial => 20,
307 Table_Increment => 200,
308 Table_Name => "Address_Clause_Checks");
309
59ac57b5 310 -----------------------------------------
311 -- Adjust_Record_For_Reverse_Bit_Order --
312 -----------------------------------------
313
314 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
67278d60 315 Comp : Node_Id;
316 CC : Node_Id;
59ac57b5 317
318 begin
67278d60 319 -- Processing depends on version of Ada
59ac57b5 320
6797073f 321 -- For Ada 95, we just renumber bits within a storage unit. We do the
568b0f6a 322 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
ab19a652 323 -- Ada 83, and are free to add this extension.
6797073f 324
325 if Ada_Version < Ada_2005 then
326 Comp := First_Component_Or_Discriminant (R);
327 while Present (Comp) loop
328 CC := Component_Clause (Comp);
329
330 -- If component clause is present, then deal with the non-default
331 -- bit order case for Ada 95 mode.
332
333 -- We only do this processing for the base type, and in fact that
334 -- is important, since otherwise if there are record subtypes, we
335 -- could reverse the bits once for each subtype, which is wrong.
336
b9e61b2a 337 if Present (CC) and then Ekind (R) = E_Record_Type then
6797073f 338 declare
339 CFB : constant Uint := Component_Bit_Offset (Comp);
340 CSZ : constant Uint := Esize (Comp);
341 CLC : constant Node_Id := Component_Clause (Comp);
342 Pos : constant Node_Id := Position (CLC);
343 FB : constant Node_Id := First_Bit (CLC);
344
345 Storage_Unit_Offset : constant Uint :=
346 CFB / System_Storage_Unit;
347
348 Start_Bit : constant Uint :=
349 CFB mod System_Storage_Unit;
59ac57b5 350
6797073f 351 begin
352 -- Cases where field goes over storage unit boundary
59ac57b5 353
6797073f 354 if Start_Bit + CSZ > System_Storage_Unit then
59ac57b5 355
6797073f 356 -- Allow multi-byte field but generate warning
59ac57b5 357
6797073f 358 if Start_Bit mod System_Storage_Unit = 0
359 and then CSZ mod System_Storage_Unit = 0
360 then
361 Error_Msg_N
7a41db5b 362 ("info: multi-byte field specified with "
363 & "non-standard Bit_Order?V?", CLC);
31486bc0 364
6797073f 365 if Bytes_Big_Endian then
31486bc0 366 Error_Msg_N
7a41db5b 367 ("\bytes are not reversed "
368 & "(component is big-endian)?V?", CLC);
31486bc0 369 else
370 Error_Msg_N
7a41db5b 371 ("\bytes are not reversed "
372 & "(component is little-endian)?V?", CLC);
31486bc0 373 end if;
59ac57b5 374
cfc922ed 375 -- Do not allow non-contiguous field
59ac57b5 376
67278d60 377 else
6797073f 378 Error_Msg_N
379 ("attempt to specify non-contiguous field "
380 & "not permitted", CLC);
381 Error_Msg_N
382 ("\caused by non-standard Bit_Order "
383 & "specified", CLC);
384 Error_Msg_N
385 ("\consider possibility of using "
386 & "Ada 2005 mode here", CLC);
387 end if;
59ac57b5 388
6797073f 389 -- Case where field fits in one storage unit
59ac57b5 390
6797073f 391 else
392 -- Give warning if suspicious component clause
59ac57b5 393
6797073f 394 if Intval (FB) >= System_Storage_Unit
395 and then Warn_On_Reverse_Bit_Order
396 then
397 Error_Msg_N
7a41db5b 398 ("info: Bit_Order clause does not affect " &
1e3532e7 399 "byte ordering?V?", Pos);
6797073f 400 Error_Msg_Uint_1 :=
401 Intval (Pos) + Intval (FB) /
402 System_Storage_Unit;
403 Error_Msg_N
7a41db5b 404 ("info: position normalized to ^ before bit " &
1e3532e7 405 "order interpreted?V?", Pos);
6797073f 406 end if;
59ac57b5 407
6797073f 408 -- Here is where we fix up the Component_Bit_Offset value
409 -- to account for the reverse bit order. Some examples of
410 -- what needs to be done are:
bfa5a9d9 411
6797073f 412 -- First_Bit .. Last_Bit Component_Bit_Offset
413 -- old new old new
59ac57b5 414
6797073f 415 -- 0 .. 0 7 .. 7 0 7
416 -- 0 .. 1 6 .. 7 0 6
417 -- 0 .. 2 5 .. 7 0 5
418 -- 0 .. 7 0 .. 7 0 4
59ac57b5 419
6797073f 420 -- 1 .. 1 6 .. 6 1 6
421 -- 1 .. 4 3 .. 6 1 3
422 -- 4 .. 7 0 .. 3 4 0
59ac57b5 423
6797073f 424 -- The rule is that the first bit is is obtained by
425 -- subtracting the old ending bit from storage_unit - 1.
59ac57b5 426
6797073f 427 Set_Component_Bit_Offset
428 (Comp,
429 (Storage_Unit_Offset * System_Storage_Unit) +
430 (System_Storage_Unit - 1) -
431 (Start_Bit + CSZ - 1));
59ac57b5 432
6797073f 433 Set_Normalized_First_Bit
434 (Comp,
435 Component_Bit_Offset (Comp) mod
436 System_Storage_Unit);
437 end if;
438 end;
439 end if;
440
441 Next_Component_Or_Discriminant (Comp);
442 end loop;
443
444 -- For Ada 2005, we do machine scalar processing, as fully described In
445 -- AI-133. This involves gathering all components which start at the
446 -- same byte offset and processing them together. Same approach is still
447 -- valid in later versions including Ada 2012.
448
449 else
450 declare
451 Max_Machine_Scalar_Size : constant Uint :=
452 UI_From_Int
453 (Standard_Long_Long_Integer_Size);
67278d60 454 -- We use this as the maximum machine scalar size
59ac57b5 455
6797073f 456 Num_CC : Natural;
457 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
59ac57b5 458
6797073f 459 begin
460 -- This first loop through components does two things. First it
461 -- deals with the case of components with component clauses whose
462 -- length is greater than the maximum machine scalar size (either
463 -- accepting them or rejecting as needed). Second, it counts the
464 -- number of components with component clauses whose length does
465 -- not exceed this maximum for later processing.
67278d60 466
6797073f 467 Num_CC := 0;
468 Comp := First_Component_Or_Discriminant (R);
469 while Present (Comp) loop
470 CC := Component_Clause (Comp);
67278d60 471
6797073f 472 if Present (CC) then
473 declare
1e3532e7 474 Fbit : constant Uint := Static_Integer (First_Bit (CC));
475 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
67278d60 476
6797073f 477 begin
b38e4131 478 -- Case of component with last bit >= max machine scalar
67278d60 479
b38e4131 480 if Lbit >= Max_Machine_Scalar_Size then
67278d60 481
b38e4131 482 -- This is allowed only if first bit is zero, and
483 -- last bit + 1 is a multiple of storage unit size.
67278d60 484
b38e4131 485 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
67278d60 486
b38e4131 487 -- This is the case to give a warning if enabled
67278d60 488
b38e4131 489 if Warn_On_Reverse_Bit_Order then
490 Error_Msg_N
7a41db5b 491 ("info: multi-byte field specified with "
cfc922ed 492 & "non-standard Bit_Order?V?", CC);
b38e4131 493
494 if Bytes_Big_Endian then
495 Error_Msg_N
496 ("\bytes are not reversed "
1e3532e7 497 & "(component is big-endian)?V?", CC);
b38e4131 498 else
499 Error_Msg_N
500 ("\bytes are not reversed "
1e3532e7 501 & "(component is little-endian)?V?", CC);
b38e4131 502 end if;
503 end if;
67278d60 504
7eb0e22f 505 -- Give error message for RM 13.5.1(10) violation
67278d60 506
b38e4131 507 else
508 Error_Msg_FE
509 ("machine scalar rules not followed for&",
510 First_Bit (CC), Comp);
67278d60 511
0c978552 512 Error_Msg_Uint_1 := Lbit + 1;
b38e4131 513 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
514 Error_Msg_F
0c978552 515 ("\last bit + 1 (^) exceeds maximum machine "
b38e4131 516 & "scalar size (^)",
517 First_Bit (CC));
67278d60 518
b38e4131 519 if (Lbit + 1) mod SSU /= 0 then
520 Error_Msg_Uint_1 := SSU;
521 Error_Msg_F
522 ("\and is not a multiple of Storage_Unit (^) "
0c978552 523 & "(RM 13.5.1(10))",
b38e4131 524 First_Bit (CC));
6797073f 525
6797073f 526 else
b38e4131 527 Error_Msg_Uint_1 := Fbit;
528 Error_Msg_F
529 ("\and first bit (^) is non-zero "
0cafb066 530 & "(RM 13.4.1(10))",
b38e4131 531 First_Bit (CC));
67278d60 532 end if;
6797073f 533 end if;
59ac57b5 534
b38e4131 535 -- OK case of machine scalar related component clause,
536 -- For now, just count them.
59ac57b5 537
6797073f 538 else
539 Num_CC := Num_CC + 1;
540 end if;
541 end;
542 end if;
59ac57b5 543
6797073f 544 Next_Component_Or_Discriminant (Comp);
545 end loop;
59ac57b5 546
6797073f 547 -- We need to sort the component clauses on the basis of the
548 -- Position values in the clause, so we can group clauses with
4a87c513 549 -- the same Position together to determine the relevant machine
6797073f 550 -- scalar size.
59ac57b5 551
6797073f 552 Sort_CC : declare
553 Comps : array (0 .. Num_CC) of Entity_Id;
554 -- Array to collect component and discriminant entities. The
555 -- data starts at index 1, the 0'th entry is for the sort
556 -- routine.
59ac57b5 557
6797073f 558 function CP_Lt (Op1, Op2 : Natural) return Boolean;
559 -- Compare routine for Sort
59ac57b5 560
6797073f 561 procedure CP_Move (From : Natural; To : Natural);
562 -- Move routine for Sort
59ac57b5 563
6797073f 564 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
59ac57b5 565
6797073f 566 Start : Natural;
567 Stop : Natural;
568 -- Start and stop positions in the component list of the set of
569 -- components with the same starting position (that constitute
570 -- components in a single machine scalar).
59ac57b5 571
6797073f 572 MaxL : Uint;
573 -- Maximum last bit value of any component in this set
59ac57b5 574
6797073f 575 MSS : Uint;
576 -- Corresponding machine scalar size
67278d60 577
6797073f 578 -----------
579 -- CP_Lt --
580 -----------
67278d60 581
6797073f 582 function CP_Lt (Op1, Op2 : Natural) return Boolean is
583 begin
584 return Position (Component_Clause (Comps (Op1))) <
585 Position (Component_Clause (Comps (Op2)));
586 end CP_Lt;
67278d60 587
6797073f 588 -------------
589 -- CP_Move --
590 -------------
67278d60 591
6797073f 592 procedure CP_Move (From : Natural; To : Natural) is
593 begin
594 Comps (To) := Comps (From);
595 end CP_Move;
67278d60 596
4a87c513 597 -- Start of processing for Sort_CC
59ac57b5 598
6797073f 599 begin
b38e4131 600 -- Collect the machine scalar relevant component clauses
59ac57b5 601
6797073f 602 Num_CC := 0;
603 Comp := First_Component_Or_Discriminant (R);
604 while Present (Comp) loop
b38e4131 605 declare
606 CC : constant Node_Id := Component_Clause (Comp);
607
608 begin
609 -- Collect only component clauses whose last bit is less
610 -- than machine scalar size. Any component clause whose
611 -- last bit exceeds this value does not take part in
612 -- machine scalar layout considerations. The test for
613 -- Error_Posted makes sure we exclude component clauses
614 -- for which we already posted an error.
615
616 if Present (CC)
617 and then not Error_Posted (Last_Bit (CC))
618 and then Static_Integer (Last_Bit (CC)) <
d64221a7 619 Max_Machine_Scalar_Size
b38e4131 620 then
621 Num_CC := Num_CC + 1;
622 Comps (Num_CC) := Comp;
623 end if;
624 end;
59ac57b5 625
6797073f 626 Next_Component_Or_Discriminant (Comp);
627 end loop;
67278d60 628
6797073f 629 -- Sort by ascending position number
67278d60 630
6797073f 631 Sorting.Sort (Num_CC);
67278d60 632
6797073f 633 -- We now have all the components whose size does not exceed
634 -- the max machine scalar value, sorted by starting position.
635 -- In this loop we gather groups of clauses starting at the
636 -- same position, to process them in accordance with AI-133.
67278d60 637
6797073f 638 Stop := 0;
639 while Stop < Num_CC loop
640 Start := Stop + 1;
641 Stop := Start;
642 MaxL :=
643 Static_Integer
644 (Last_Bit (Component_Clause (Comps (Start))));
67278d60 645 while Stop < Num_CC loop
6797073f 646 if Static_Integer
647 (Position (Component_Clause (Comps (Stop + 1)))) =
648 Static_Integer
649 (Position (Component_Clause (Comps (Stop))))
650 then
651 Stop := Stop + 1;
652 MaxL :=
653 UI_Max
654 (MaxL,
655 Static_Integer
656 (Last_Bit
657 (Component_Clause (Comps (Stop)))));
658 else
659 exit;
660 end if;
661 end loop;
67278d60 662
6797073f 663 -- Now we have a group of component clauses from Start to
664 -- Stop whose positions are identical, and MaxL is the
665 -- maximum last bit value of any of these components.
666
667 -- We need to determine the corresponding machine scalar
668 -- size. This loop assumes that machine scalar sizes are
669 -- even, and that each possible machine scalar has twice
670 -- as many bits as the next smaller one.
671
672 MSS := Max_Machine_Scalar_Size;
673 while MSS mod 2 = 0
674 and then (MSS / 2) >= SSU
675 and then (MSS / 2) > MaxL
676 loop
677 MSS := MSS / 2;
678 end loop;
67278d60 679
6797073f 680 -- Here is where we fix up the Component_Bit_Offset value
681 -- to account for the reverse bit order. Some examples of
682 -- what needs to be done for the case of a machine scalar
683 -- size of 8 are:
67278d60 684
6797073f 685 -- First_Bit .. Last_Bit Component_Bit_Offset
686 -- old new old new
67278d60 687
6797073f 688 -- 0 .. 0 7 .. 7 0 7
689 -- 0 .. 1 6 .. 7 0 6
690 -- 0 .. 2 5 .. 7 0 5
691 -- 0 .. 7 0 .. 7 0 4
67278d60 692
6797073f 693 -- 1 .. 1 6 .. 6 1 6
694 -- 1 .. 4 3 .. 6 1 3
695 -- 4 .. 7 0 .. 3 4 0
67278d60 696
6797073f 697 -- The rule is that the first bit is obtained by subtracting
698 -- the old ending bit from machine scalar size - 1.
67278d60 699
6797073f 700 for C in Start .. Stop loop
701 declare
702 Comp : constant Entity_Id := Comps (C);
b9e61b2a 703 CC : constant Node_Id := Component_Clause (Comp);
704
705 LB : constant Uint := Static_Integer (Last_Bit (CC));
6797073f 706 NFB : constant Uint := MSS - Uint_1 - LB;
707 NLB : constant Uint := NFB + Esize (Comp) - 1;
b9e61b2a 708 Pos : constant Uint := Static_Integer (Position (CC));
67278d60 709
6797073f 710 begin
711 if Warn_On_Reverse_Bit_Order then
712 Error_Msg_Uint_1 := MSS;
713 Error_Msg_N
714 ("info: reverse bit order in machine " &
1e3532e7 715 "scalar of length^?V?", First_Bit (CC));
6797073f 716 Error_Msg_Uint_1 := NFB;
717 Error_Msg_Uint_2 := NLB;
718
719 if Bytes_Big_Endian then
720 Error_Msg_NE
7a41db5b 721 ("\big-endian range for component "
722 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
6797073f 723 else
724 Error_Msg_NE
7a41db5b 725 ("\little-endian range for component"
726 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
67278d60 727 end if;
6797073f 728 end if;
67278d60 729
6797073f 730 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
731 Set_Normalized_First_Bit (Comp, NFB mod SSU);
732 end;
67278d60 733 end loop;
6797073f 734 end loop;
735 end Sort_CC;
736 end;
737 end if;
59ac57b5 738 end Adjust_Record_For_Reverse_Bit_Order;
739
1d366b32 740 -------------------------------------
741 -- Alignment_Check_For_Size_Change --
742 -------------------------------------
d6f39728 743
1d366b32 744 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
d6f39728 745 begin
746 -- If the alignment is known, and not set by a rep clause, and is
747 -- inconsistent with the size being set, then reset it to unknown,
748 -- we assume in this case that the size overrides the inherited
749 -- alignment, and that the alignment must be recomputed.
750
751 if Known_Alignment (Typ)
752 and then not Has_Alignment_Clause (Typ)
1d366b32 753 and then Size mod (Alignment (Typ) * SSU) /= 0
d6f39728 754 then
755 Init_Alignment (Typ);
756 end if;
1d366b32 757 end Alignment_Check_For_Size_Change;
d6f39728 758
06ef5f86 759 -------------------------------------
760 -- Analyze_Aspects_At_Freeze_Point --
761 -------------------------------------
762
763 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
06ef5f86 764 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
765 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
766 -- the aspect specification node ASN.
767
37c6e44c 768 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
769 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
770 -- a derived type can inherit aspects from its parent which have been
771 -- specified at the time of the derivation using an aspect, as in:
772 --
773 -- type A is range 1 .. 10
774 -- with Size => Not_Defined_Yet;
775 -- ..
776 -- type B is new A;
777 -- ..
778 -- Not_Defined_Yet : constant := 64;
779 --
780 -- In this example, the Size of A is considered to be specified prior
781 -- to the derivation, and thus inherited, even though the value is not
782 -- known at the time of derivation. To deal with this, we use two entity
783 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
784 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
785 -- the derived type (B here). If this flag is set when the derived type
786 -- is frozen, then this procedure is called to ensure proper inheritance
b21edad9 787 -- of all delayed aspects from the parent type. The derived type is E,
37c6e44c 788 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
789 -- aspect specification node in the Rep_Item chain for the parent type.
790
06ef5f86 791 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
792 -- Given an aspect specification node ASN whose expression is an
793 -- optional Boolean, this routines creates the corresponding pragma
794 -- at the freezing point.
795
796 ----------------------------------
797 -- Analyze_Aspect_Default_Value --
798 ----------------------------------
799
800 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
ee2b7923 801 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
06ef5f86 802 Ent : constant Entity_Id := Entity (ASN);
803 Expr : constant Node_Id := Expression (ASN);
804 Id : constant Node_Id := Identifier (ASN);
805
806 begin
807 Error_Msg_Name_1 := Chars (Id);
808
809 if not Is_Type (Ent) then
810 Error_Msg_N ("aspect% can only apply to a type", Id);
811 return;
812
813 elsif not Is_First_Subtype (Ent) then
814 Error_Msg_N ("aspect% cannot apply to subtype", Id);
815 return;
816
817 elsif A_Id = Aspect_Default_Value
818 and then not Is_Scalar_Type (Ent)
819 then
820 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
821 return;
822
823 elsif A_Id = Aspect_Default_Component_Value then
824 if not Is_Array_Type (Ent) then
825 Error_Msg_N ("aspect% can only be applied to array type", Id);
826 return;
827
828 elsif not Is_Scalar_Type (Component_Type (Ent)) then
829 Error_Msg_N ("aspect% requires scalar components", Id);
830 return;
831 end if;
832 end if;
833
834 Set_Has_Default_Aspect (Base_Type (Ent));
835
836 if Is_Scalar_Type (Ent) then
9f36e3fb 837 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
06ef5f86 838 else
f3d70f08 839 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
06ef5f86 840 end if;
841 end Analyze_Aspect_Default_Value;
842
37c6e44c 843 ---------------------------------
844 -- Inherit_Delayed_Rep_Aspects --
845 ---------------------------------
846
847 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
ee2b7923 848 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
849 P : constant Entity_Id := Entity (ASN);
37c6e44c 850 -- Entithy for parent type
851
852 N : Node_Id;
853 -- Item from Rep_Item chain
854
855 A : Aspect_Id;
856
857 begin
858 -- Loop through delayed aspects for the parent type
859
860 N := ASN;
861 while Present (N) loop
862 if Nkind (N) = N_Aspect_Specification then
863 exit when Entity (N) /= P;
864
865 if Is_Delayed_Aspect (N) then
866 A := Get_Aspect_Id (Chars (Identifier (N)));
867
868 -- Process delayed rep aspect. For Boolean attributes it is
869 -- not possible to cancel an attribute once set (the attempt
870 -- to use an aspect with xxx => False is an error) for a
871 -- derived type. So for those cases, we do not have to check
872 -- if a clause has been given for the derived type, since it
873 -- is harmless to set it again if it is already set.
874
875 case A is
876
877 -- Alignment
878
879 when Aspect_Alignment =>
880 if not Has_Alignment_Clause (E) then
881 Set_Alignment (E, Alignment (P));
882 end if;
883
884 -- Atomic
885
886 when Aspect_Atomic =>
887 if Is_Atomic (P) then
888 Set_Is_Atomic (E);
889 end if;
890
891 -- Atomic_Components
892
893 when Aspect_Atomic_Components =>
894 if Has_Atomic_Components (P) then
895 Set_Has_Atomic_Components (Base_Type (E));
896 end if;
897
898 -- Bit_Order
899
900 when Aspect_Bit_Order =>
901 if Is_Record_Type (E)
902 and then No (Get_Attribute_Definition_Clause
903 (E, Attribute_Bit_Order))
904 and then Reverse_Bit_Order (P)
905 then
906 Set_Reverse_Bit_Order (Base_Type (E));
907 end if;
908
909 -- Component_Size
910
911 when Aspect_Component_Size =>
912 if Is_Array_Type (E)
913 and then not Has_Component_Size_Clause (E)
914 then
915 Set_Component_Size
916 (Base_Type (E), Component_Size (P));
917 end if;
918
919 -- Machine_Radix
920
921 when Aspect_Machine_Radix =>
922 if Is_Decimal_Fixed_Point_Type (E)
923 and then not Has_Machine_Radix_Clause (E)
924 then
925 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
926 end if;
927
928 -- Object_Size (also Size which also sets Object_Size)
929
930 when Aspect_Object_Size | Aspect_Size =>
931 if not Has_Size_Clause (E)
932 and then
933 No (Get_Attribute_Definition_Clause
934 (E, Attribute_Object_Size))
935 then
936 Set_Esize (E, Esize (P));
937 end if;
938
939 -- Pack
940
941 when Aspect_Pack =>
942 if not Is_Packed (E) then
943 Set_Is_Packed (Base_Type (E));
944
945 if Is_Bit_Packed_Array (P) then
946 Set_Is_Bit_Packed_Array (Base_Type (E));
a88a5773 947 Set_Packed_Array_Impl_Type
948 (E, Packed_Array_Impl_Type (P));
37c6e44c 949 end if;
950 end if;
951
952 -- Scalar_Storage_Order
953
954 when Aspect_Scalar_Storage_Order =>
955 if (Is_Record_Type (E) or else Is_Array_Type (E))
956 and then No (Get_Attribute_Definition_Clause
e163cac8 957 (E, Attribute_Scalar_Storage_Order))
37c6e44c 958 and then Reverse_Storage_Order (P)
959 then
960 Set_Reverse_Storage_Order (Base_Type (E));
b64082f2 961
962 -- Clear default SSO indications, since the aspect
963 -- overrides the default.
964
965 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
966 Set_SSO_Set_High_By_Default (Base_Type (E), False);
37c6e44c 967 end if;
968
969 -- Small
970
971 when Aspect_Small =>
972 if Is_Fixed_Point_Type (E)
973 and then not Has_Small_Clause (E)
974 then
975 Set_Small_Value (E, Small_Value (P));
976 end if;
977
978 -- Storage_Size
979
980 when Aspect_Storage_Size =>
981 if (Is_Access_Type (E) or else Is_Task_Type (E))
982 and then not Has_Storage_Size_Clause (E)
983 then
984 Set_Storage_Size_Variable
985 (Base_Type (E), Storage_Size_Variable (P));
986 end if;
987
988 -- Value_Size
989
990 when Aspect_Value_Size =>
991
992 -- Value_Size is never inherited, it is either set by
993 -- default, or it is explicitly set for the derived
994 -- type. So nothing to do here.
995
996 null;
997
998 -- Volatile
999
1000 when Aspect_Volatile =>
1001 if Is_Volatile (P) then
1002 Set_Is_Volatile (E);
1003 end if;
1004
2fe893b9 1005 -- Volatile_Full_Access
1006
1007 when Aspect_Volatile_Full_Access =>
4bf2acc9 1008 if Is_Volatile_Full_Access (P) then
1009 Set_Is_Volatile_Full_Access (E);
2fe893b9 1010 end if;
1011
37c6e44c 1012 -- Volatile_Components
1013
1014 when Aspect_Volatile_Components =>
1015 if Has_Volatile_Components (P) then
1016 Set_Has_Volatile_Components (Base_Type (E));
1017 end if;
1018
1019 -- That should be all the Rep Aspects
1020
1021 when others =>
1022 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1023 null;
1024
1025 end case;
1026 end if;
1027 end if;
1028
1029 N := Next_Rep_Item (N);
1030 end loop;
1031 end Inherit_Delayed_Rep_Aspects;
1032
06ef5f86 1033 -------------------------------------
1034 -- Make_Pragma_From_Boolean_Aspect --
1035 -------------------------------------
1036
1037 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1038 Ident : constant Node_Id := Identifier (ASN);
1039 A_Name : constant Name_Id := Chars (Ident);
1040 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1041 Ent : constant Entity_Id := Entity (ASN);
1042 Expr : constant Node_Id := Expression (ASN);
1043 Loc : constant Source_Ptr := Sloc (ASN);
1044
06ef5f86 1045 procedure Check_False_Aspect_For_Derived_Type;
1046 -- This procedure checks for the case of a false aspect for a derived
1047 -- type, which improperly tries to cancel an aspect inherited from
1048 -- the parent.
1049
1050 -----------------------------------------
1051 -- Check_False_Aspect_For_Derived_Type --
1052 -----------------------------------------
1053
1054 procedure Check_False_Aspect_For_Derived_Type is
1055 Par : Node_Id;
1056
1057 begin
1058 -- We are only checking derived types
1059
1060 if not Is_Derived_Type (E) then
1061 return;
1062 end if;
1063
1064 Par := Nearest_Ancestor (E);
1065
1066 case A_Id is
1067 when Aspect_Atomic | Aspect_Shared =>
1068 if not Is_Atomic (Par) then
1069 return;
1070 end if;
1071
1072 when Aspect_Atomic_Components =>
1073 if not Has_Atomic_Components (Par) then
1074 return;
1075 end if;
1076
1077 when Aspect_Discard_Names =>
1078 if not Discard_Names (Par) then
1079 return;
1080 end if;
1081
1082 when Aspect_Pack =>
1083 if not Is_Packed (Par) then
1084 return;
1085 end if;
1086
1087 when Aspect_Unchecked_Union =>
1088 if not Is_Unchecked_Union (Par) then
1089 return;
1090 end if;
1091
1092 when Aspect_Volatile =>
1093 if not Is_Volatile (Par) then
1094 return;
1095 end if;
1096
1097 when Aspect_Volatile_Components =>
1098 if not Has_Volatile_Components (Par) then
1099 return;
1100 end if;
1101
2fe893b9 1102 when Aspect_Volatile_Full_Access =>
4bf2acc9 1103 if not Is_Volatile_Full_Access (Par) then
2fe893b9 1104 return;
1105 end if;
1106
06ef5f86 1107 when others =>
1108 return;
1109 end case;
1110
1111 -- Fall through means we are canceling an inherited aspect
1112
1113 Error_Msg_Name_1 := A_Name;
37c6e44c 1114 Error_Msg_NE
1115 ("derived type& inherits aspect%, cannot cancel", Expr, E);
06ef5f86 1116 end Check_False_Aspect_For_Derived_Type;
1117
ee2b7923 1118 -- Local variables
1119
1120 Prag : Node_Id;
1121
06ef5f86 1122 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1123
1124 begin
37c6e44c 1125 -- Note that we know Expr is present, because for a missing Expr
1126 -- argument, we knew it was True and did not need to delay the
1127 -- evaluation to the freeze point.
1128
06ef5f86 1129 if Is_False (Static_Boolean (Expr)) then
1130 Check_False_Aspect_For_Derived_Type;
1131
1132 else
1133 Prag :=
1134 Make_Pragma (Loc,
ee2b7923 1135 Pragma_Identifier =>
1136 Make_Identifier (Sloc (Ident), Chars (Ident)),
06ef5f86 1137 Pragma_Argument_Associations => New_List (
57cd943b 1138 Make_Pragma_Argument_Association (Sloc (Ident),
ee2b7923 1139 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
06ef5f86 1140
1141 Set_From_Aspect_Specification (Prag, True);
1142 Set_Corresponding_Aspect (Prag, ASN);
1143 Set_Aspect_Rep_Item (ASN, Prag);
1144 Set_Is_Delayed_Aspect (Prag);
1145 Set_Parent (Prag, ASN);
1146 end if;
06ef5f86 1147 end Make_Pragma_From_Boolean_Aspect;
1148
ee2b7923 1149 -- Local variables
1150
1151 A_Id : Aspect_Id;
1152 ASN : Node_Id;
1153 Ritem : Node_Id;
1154
06ef5f86 1155 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1156
1157 begin
29a9d4be 1158 -- Must be visible in current scope
06ef5f86 1159
ace3389d 1160 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
06ef5f86 1161 return;
1162 end if;
1163
1164 -- Look for aspect specification entries for this entity
1165
1166 ASN := First_Rep_Item (E);
06ef5f86 1167 while Present (ASN) loop
37c6e44c 1168 if Nkind (ASN) = N_Aspect_Specification then
1169 exit when Entity (ASN) /= E;
06ef5f86 1170
37c6e44c 1171 if Is_Delayed_Aspect (ASN) then
1172 A_Id := Get_Aspect_Id (ASN);
1173
1174 case A_Id is
e4c87fa5 1175
37c6e44c 1176 -- For aspects whose expression is an optional Boolean, make
7d6fb253 1177 -- the corresponding pragma at the freeze point.
06ef5f86 1178
7d6fb253 1179 when Boolean_Aspects |
1180 Library_Unit_Aspects =>
ee2b7923 1181
1182 -- Aspects Export and Import require special handling.
1183 -- Both are by definition Boolean and may benefit from
1184 -- forward references, however their expressions are
1185 -- treated as static. In addition, the syntax of their
1186 -- corresponding pragmas requires extra "pieces" which
1187 -- may also contain forward references. To account for
1188 -- all of this, the corresponding pragma is created by
1189 -- Analyze_Aspect_Export_Import, but is not analyzed as
1190 -- the complete analysis must happen now.
1191
1192 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1193 null;
1194
1195 -- Otherwise create a corresponding pragma
1196
1197 else
1198 Make_Pragma_From_Boolean_Aspect (ASN);
1199 end if;
06ef5f86 1200
37c6e44c 1201 -- Special handling for aspects that don't correspond to
1202 -- pragmas/attributes.
06ef5f86 1203
7d6fb253 1204 when Aspect_Default_Value |
1205 Aspect_Default_Component_Value =>
81c2bc19 1206
1207 -- Do not inherit aspect for anonymous base type of a
1208 -- scalar or array type, because they apply to the first
1209 -- subtype of the type, and will be processed when that
1210 -- first subtype is frozen.
1211
1212 if Is_Derived_Type (E)
1213 and then not Comes_From_Source (E)
1214 and then E /= First_Subtype (E)
1215 then
1216 null;
1217 else
1218 Analyze_Aspect_Default_Value (ASN);
1219 end if;
06ef5f86 1220
37c6e44c 1221 -- Ditto for iterator aspects, because the corresponding
1222 -- attributes may not have been analyzed yet.
af9fed8f 1223
7d6fb253 1224 when Aspect_Constant_Indexing |
1225 Aspect_Variable_Indexing |
1226 Aspect_Default_Iterator |
1227 Aspect_Iterator_Element =>
1228 Analyze (Expression (ASN));
af9fed8f 1229
7d6fb253 1230 if Etype (Expression (ASN)) = Any_Type then
1231 Error_Msg_NE
1232 ("\aspect must be fully defined before & is frozen",
1233 ASN, E);
1234 end if;
b3f8228a 1235
7d6fb253 1236 when Aspect_Iterable =>
1237 Validate_Iterable_Aspect (E, ASN);
1238
1239 when others =>
1240 null;
37c6e44c 1241 end case;
06ef5f86 1242
37c6e44c 1243 Ritem := Aspect_Rep_Item (ASN);
06ef5f86 1244
37c6e44c 1245 if Present (Ritem) then
1246 Analyze (Ritem);
1247 end if;
06ef5f86 1248 end if;
1249 end if;
1250
1251 Next_Rep_Item (ASN);
1252 end loop;
37c6e44c 1253
1254 -- This is where we inherit delayed rep aspects from our parent. Note
1255 -- that if we fell out of the above loop with ASN non-empty, it means
1256 -- we hit an aspect for an entity other than E, and it must be the
1257 -- type from which we were derived.
1258
1259 if May_Inherit_Delayed_Rep_Aspects (E) then
1260 Inherit_Delayed_Rep_Aspects (ASN);
1261 end if;
06ef5f86 1262 end Analyze_Aspects_At_Freeze_Point;
1263
ae888dbd 1264 -----------------------------------
1265 -- Analyze_Aspect_Specifications --
1266 -----------------------------------
1267
21ea3a4f 1268 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
e2bf777d 1269 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
6c5793cd 1270 -- Establish linkages between an aspect and its corresponding pragma
5ddd846b 1271
5655be8a 1272 procedure Insert_Pragma
1273 (Prag : Node_Id;
1274 Is_Instance : Boolean := False);
2f06c88a 1275 -- Subsidiary to the analysis of aspects
1276 -- Abstract_State
2f06c88a 1277 -- Attach_Handler
1278 -- Contract_Cases
1279 -- Depends
5655be8a 1280 -- Ghost
2f06c88a 1281 -- Global
5655be8a 1282 -- Initial_Condition
1283 -- Initializes
2f06c88a 1284 -- Post
1285 -- Pre
1286 -- Refined_Depends
1287 -- Refined_Global
5655be8a 1288 -- Refined_State
2f06c88a 1289 -- SPARK_Mode
1290 -- Warnings
e2bf777d 1291 -- Insert pragma Prag such that it mimics the placement of a source
5655be8a 1292 -- pragma of the same kind. Flag Is_Generic should be set when the
1293 -- context denotes a generic instance.
e2bf777d 1294
1295 --------------
1296 -- Decorate --
1297 --------------
1298
1299 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
5ddd846b 1300 begin
6c5793cd 1301 Set_Aspect_Rep_Item (Asp, Prag);
5ddd846b 1302 Set_Corresponding_Aspect (Prag, Asp);
1303 Set_From_Aspect_Specification (Prag);
5ddd846b 1304 Set_Parent (Prag, Asp);
e2bf777d 1305 end Decorate;
f0813d71 1306
e2bf777d 1307 -------------------
1308 -- Insert_Pragma --
1309 -------------------
c1006d6d 1310
5655be8a 1311 procedure Insert_Pragma
1312 (Prag : Node_Id;
1313 Is_Instance : Boolean := False)
1314 is
3ff5e35d 1315 Aux : Node_Id;
1316 Decl : Node_Id;
1317 Decls : List_Id;
1318 Def : Node_Id;
1319 Inserted : Boolean := False;
c1006d6d 1320
1321 begin
3ff5e35d 1322 -- When the aspect appears on an entry, package, protected unit,
1323 -- subprogram, or task unit body, insert the generated pragma at the
1324 -- top of the body declarations to emulate the behavior of a source
1325 -- pragma.
2f06c88a 1326
1327 -- package body Pack with Aspect is
1328
1329 -- package body Pack is
1330 -- pragma Prag;
1331
3ff5e35d 1332 if Nkind_In (N, N_Entry_Body,
1333 N_Package_Body,
2f06c88a 1334 N_Protected_Body,
1335 N_Subprogram_Body,
1336 N_Task_Body)
1337 then
1338 Decls := Declarations (N);
1339
1340 if No (Decls) then
1341 Decls := New_List;
1342 Set_Declarations (N, Decls);
1343 end if;
e2bf777d 1344
3ff5e35d 1345 Prepend_To (Decls, Prag);
2f06c88a 1346
1347 -- When the aspect is associated with a [generic] package declaration
1348 -- insert the generated pragma at the top of the visible declarations
1349 -- to emulate the behavior of a source pragma.
1350
1351 -- package Pack with Aspect is
1352
1353 -- package Pack is
1354 -- pragma Prag;
1355
1356 elsif Nkind_In (N, N_Generic_Package_Declaration,
1357 N_Package_Declaration)
1358 then
1359 Decls := Visible_Declarations (Specification (N));
1360
1361 if No (Decls) then
1362 Decls := New_List;
1363 Set_Visible_Declarations (Specification (N), Decls);
1364 end if;
1365
5655be8a 1366 -- The visible declarations of a generic instance have the
1367 -- following structure:
1368
1369 -- <renamings of generic formals>
1370 -- <renamings of internally-generated spec and body>
1371 -- <first source declaration>
1372
1373 -- Insert the pragma before the first source declaration by
3ff5e35d 1374 -- skipping the instance "header" to ensure proper visibility of
1375 -- all formals.
5655be8a 1376
1377 if Is_Instance then
1378 Decl := First (Decls);
3ff5e35d 1379 while Present (Decl) loop
1380 if Comes_From_Source (Decl) then
1381 Insert_Before (Decl, Prag);
1382 Inserted := True;
1383 exit;
1384 else
1385 Next (Decl);
1386 end if;
5655be8a 1387 end loop;
1388
3ff5e35d 1389 -- The pragma is placed after the instance "header"
5655be8a 1390
3ff5e35d 1391 if not Inserted then
5655be8a 1392 Append_To (Decls, Prag);
1393 end if;
1394
1395 -- Otherwise this is not a generic instance
1396
1397 else
1398 Prepend_To (Decls, Prag);
1399 end if;
2f06c88a 1400
1401 -- When the aspect is associated with a protected unit declaration,
1402 -- insert the generated pragma at the top of the visible declarations
1403 -- the emulate the behavior of a source pragma.
1404
1405 -- protected [type] Prot with Aspect is
1406
1407 -- protected [type] Prot is
1408 -- pragma Prag;
1409
1410 elsif Nkind (N) = N_Protected_Type_Declaration then
736b80cc 1411 Def := Protected_Definition (N);
1412
1413 if No (Def) then
1414 Def :=
1415 Make_Protected_Definition (Sloc (N),
1416 Visible_Declarations => New_List,
1417 End_Label => Empty);
1418
1419 Set_Protected_Definition (N, Def);
1420 end if;
1421
1422 Decls := Visible_Declarations (Def);
2f06c88a 1423
1424 if No (Decls) then
1425 Decls := New_List;
736b80cc 1426 Set_Visible_Declarations (Def, Decls);
2f06c88a 1427 end if;
1428
1429 Prepend_To (Decls, Prag);
1430
736b80cc 1431 -- When the aspect is associated with a task unit declaration, insert
1432 -- insert the generated pragma at the top of the visible declarations
1433 -- the emulate the behavior of a source pragma.
2f06c88a 1434
1435 -- task [type] Prot with Aspect is
1436
1437 -- task [type] Prot is
1438 -- pragma Prag;
1439
736b80cc 1440 elsif Nkind (N) = N_Task_Type_Declaration then
1441 Def := Task_Definition (N);
1442
1443 if No (Def) then
1444 Def :=
1445 Make_Task_Definition (Sloc (N),
1446 Visible_Declarations => New_List,
1447 End_Label => Empty);
1448
1449 Set_Task_Definition (N, Def);
1450 end if;
1451
1452 Decls := Visible_Declarations (Def);
2f06c88a 1453
1454 if No (Decls) then
1455 Decls := New_List;
736b80cc 1456 Set_Visible_Declarations (Def, Decls);
d324c418 1457 end if;
c1006d6d 1458
2f06c88a 1459 Prepend_To (Decls, Prag);
1460
ed695684 1461 -- When the context is a library unit, the pragma is added to the
1462 -- Pragmas_After list.
1463
1464 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1465 Aux := Aux_Decls_Node (Parent (N));
1466
1467 if No (Pragmas_After (Aux)) then
1468 Set_Pragmas_After (Aux, New_List);
1469 end if;
1470
1471 Prepend (Prag, Pragmas_After (Aux));
1472
2f06c88a 1473 -- Default, the pragma is inserted after the context
c1006d6d 1474
1475 else
1476 Insert_After (N, Prag);
c1006d6d 1477 end if;
e2bf777d 1478 end Insert_Pragma;
c1006d6d 1479
1480 -- Local variables
1481
ae888dbd 1482 Aspect : Node_Id;
d74fc39a 1483 Aitem : Node_Id;
ae888dbd 1484 Ent : Node_Id;
ae888dbd 1485
21ea3a4f 1486 L : constant List_Id := Aspect_Specifications (N);
1487
ae888dbd 1488 Ins_Node : Node_Id := N;
89f1e35c 1489 -- Insert pragmas/attribute definition clause after this node when no
1490 -- delayed analysis is required.
d74fc39a 1491
ee2b7923 1492 -- Start of processing for Analyze_Aspect_Specifications
f0813d71 1493
ee2b7923 1494 begin
d74fc39a 1495 -- The general processing involves building an attribute definition
89f1e35c 1496 -- clause or a pragma node that corresponds to the aspect. Then in order
1497 -- to delay the evaluation of this aspect to the freeze point, we attach
1498 -- the corresponding pragma/attribute definition clause to the aspect
1499 -- specification node, which is then placed in the Rep Item chain. In
1500 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1501 -- and we evaluate the rep item at the freeze point. When the aspect
1502 -- doesn't have a corresponding pragma/attribute definition clause, then
1503 -- its analysis is simply delayed at the freeze point.
1504
1505 -- Some special cases don't require delay analysis, thus the aspect is
1506 -- analyzed right now.
1507
51ea9c94 1508 -- Note that there is a special handling for Pre, Post, Test_Case,
e66f4e2a 1509 -- Contract_Cases aspects. In these cases, we do not have to worry
51ea9c94 1510 -- about delay issues, since the pragmas themselves deal with delay
1511 -- of visibility for the expression analysis. Thus, we just insert
1512 -- the pragma after the node N.
ae888dbd 1513
21ea3a4f 1514 pragma Assert (Present (L));
1515
6fb3c314 1516 -- Loop through aspects
f93e7257 1517
ae888dbd 1518 Aspect := First (L);
21ea3a4f 1519 Aspect_Loop : while Present (Aspect) loop
0fd13d32 1520 Analyze_One_Aspect : declare
94153a42 1521 Expr : constant Node_Id := Expression (Aspect);
89f1e35c 1522 Id : constant Node_Id := Identifier (Aspect);
1523 Loc : constant Source_Ptr := Sloc (Aspect);
94153a42 1524 Nam : constant Name_Id := Chars (Id);
1525 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
ae888dbd 1526 Anod : Node_Id;
1527
37c6e44c 1528 Delay_Required : Boolean;
89f1e35c 1529 -- Set False if delay is not required
1530
c0793fff 1531 Eloc : Source_Ptr := No_Location;
1532 -- Source location of expression, modified when we split PPC's. It
1533 -- is set below when Expr is present.
39e1f22f 1534
ee2b7923 1535 procedure Analyze_Aspect_Convention;
1536 -- Perform analysis of aspect Convention
1537
1538 procedure Analyze_Aspect_Export_Import;
1539 -- Perform analysis of aspects Export or Import
1540
1541 procedure Analyze_Aspect_External_Link_Name;
1542 -- Perform analysis of aspects External_Name or Link_Name
21ea3a4f 1543
89f1e35c 1544 procedure Analyze_Aspect_Implicit_Dereference;
9ab32fe9 1545 -- Perform analysis of the Implicit_Dereference aspects
0fd13d32 1546
1547 procedure Make_Aitem_Pragma
1548 (Pragma_Argument_Associations : List_Id;
1549 Pragma_Name : Name_Id);
1550 -- This is a wrapper for Make_Pragma used for converting aspects
1551 -- to pragmas. It takes care of Sloc (set from Loc) and building
1552 -- the pragma identifier from the given name. In addition the
1553 -- flags Class_Present and Split_PPC are set from the aspect
1554 -- node, as well as Is_Ignored. This routine also sets the
1555 -- From_Aspect_Specification in the resulting pragma node to
1556 -- True, and sets Corresponding_Aspect to point to the aspect.
1557 -- The resulting pragma is assigned to Aitem.
21ea3a4f 1558
ee2b7923 1559 -------------------------------
1560 -- Analyze_Aspect_Convention --
1561 -------------------------------
1562
1563 procedure Analyze_Aspect_Convention is
1564 Conv : Node_Id;
1565 Dummy_1 : Node_Id;
1566 Dummy_2 : Node_Id;
1567 Dummy_3 : Node_Id;
1568 Expo : Node_Id;
1569 Imp : Node_Id;
89f1e35c 1570
21ea3a4f 1571 begin
ee2b7923 1572 -- Obtain all interfacing aspects that apply to the related
1573 -- entity.
1574
1575 Get_Interfacing_Aspects
1576 (Iface_Asp => Aspect,
1577 Conv_Asp => Dummy_1,
1578 EN_Asp => Dummy_2,
1579 Expo_Asp => Expo,
1580 Imp_Asp => Imp,
1581 LN_Asp => Dummy_3,
1582 Do_Checks => True);
1583
1584 -- The related entity is subject to aspect Export or Import.
1585 -- Do not process Convention now because it must be analysed
1586 -- as part of Export or Import.
1587
1588 if Present (Expo) or else Present (Imp) then
1589 return;
21ea3a4f 1590
ee2b7923 1591 -- Otherwise Convention appears by itself
21ea3a4f 1592
ee2b7923 1593 else
1594 -- The aspect specifies a particular convention
1595
1596 if Present (Expr) then
1597 Conv := New_Copy_Tree (Expr);
1598
1599 -- Otherwise assume convention Ada
1600
1601 else
1602 Conv := Make_Identifier (Loc, Name_Ada);
1603 end if;
1604
1605 -- Generate:
1606 -- pragma Convention (<Conv>, <E>);
1607
1608 Make_Aitem_Pragma
1609 (Pragma_Name => Name_Convention,
1610 Pragma_Argument_Associations => New_List (
1611 Make_Pragma_Argument_Association (Loc,
1612 Expression => Conv),
1613 Make_Pragma_Argument_Association (Loc,
1614 Expression => New_Occurrence_Of (E, Loc))));
1615
1616 Decorate (Aspect, Aitem);
1617 Insert_Pragma (Aitem);
1618 end if;
1619 end Analyze_Aspect_Convention;
1620
1621 ----------------------------------
1622 -- Analyze_Aspect_Export_Import --
1623 ----------------------------------
21ea3a4f 1624
ee2b7923 1625 procedure Analyze_Aspect_Export_Import is
1626 Dummy_1 : Node_Id;
1627 Dummy_2 : Node_Id;
1628 Dummy_3 : Node_Id;
1629 Expo : Node_Id;
1630 Imp : Node_Id;
1631
1632 begin
1633 -- Obtain all interfacing aspects that apply to the related
1634 -- entity.
1635
1636 Get_Interfacing_Aspects
1637 (Iface_Asp => Aspect,
1638 Conv_Asp => Dummy_1,
1639 EN_Asp => Dummy_2,
1640 Expo_Asp => Expo,
1641 Imp_Asp => Imp,
1642 LN_Asp => Dummy_3,
1643 Do_Checks => True);
1644
1645 -- The related entity cannot be subject to both aspects Export
1646 -- and Import.
1647
1648 if Present (Expo) and then Present (Imp) then
1649 Error_Msg_N
1650 ("incompatible interfacing aspects given for &", E);
1651 Error_Msg_Sloc := Sloc (Expo);
1652 Error_Msg_N ("\aspect `Export` #", E);
1653 Error_Msg_Sloc := Sloc (Imp);
1654 Error_Msg_N ("\aspect `Import` #", E);
1655 end if;
1656
1657 -- A variable is most likely modified from the outside. Take
1658 -- Take the optimistic approach to avoid spurious errors.
1659
1660 if Ekind (E) = E_Variable then
1661 Set_Never_Set_In_Source (E, False);
1662 end if;
1663
1664 -- Resolve the expression of an Import or Export here, and
1665 -- require it to be of type Boolean and static. This is not
1666 -- quite right, because in general this should be delayed,
1667 -- but that seems tricky for these, because normally Boolean
1668 -- aspects are replaced with pragmas at the freeze point in
1669 -- Make_Pragma_From_Boolean_Aspect.
1670
1671 if not Present (Expr)
1672 or else Is_True (Static_Boolean (Expr))
1673 then
1674 if A_Id = Aspect_Import then
1675 Set_Has_Completion (E);
1676 Set_Is_Imported (E);
1677
1678 -- An imported object cannot be explicitly initialized
1679
1680 if Nkind (N) = N_Object_Declaration
1681 and then Present (Expression (N))
1682 then
1683 Error_Msg_N
1684 ("imported entities cannot be initialized "
1685 & "(RM B.1(24))", Expression (N));
1686 end if;
1687
1688 else
1689 pragma Assert (A_Id = Aspect_Export);
1690 Set_Is_Exported (E);
1691 end if;
1692
1693 -- Create the proper form of pragma Export or Import taking
1694 -- into account Conversion, External_Name, and Link_Name.
1695
1696 Aitem := Build_Export_Import_Pragma (Aspect, E);
d8e539ae 1697
1698 -- Otherwise the expression is either False or erroneous. There
1699 -- is no corresponding pragma.
1700
1701 else
1702 Aitem := Empty;
ee2b7923 1703 end if;
1704 end Analyze_Aspect_Export_Import;
1705
1706 ---------------------------------------
1707 -- Analyze_Aspect_External_Link_Name --
1708 ---------------------------------------
1709
1710 procedure Analyze_Aspect_External_Link_Name is
1711 Dummy_1 : Node_Id;
1712 Dummy_2 : Node_Id;
1713 Dummy_3 : Node_Id;
1714 Expo : Node_Id;
1715 Imp : Node_Id;
1716
1717 begin
1718 -- Obtain all interfacing aspects that apply to the related
1719 -- entity.
1720
1721 Get_Interfacing_Aspects
1722 (Iface_Asp => Aspect,
1723 Conv_Asp => Dummy_1,
1724 EN_Asp => Dummy_2,
1725 Expo_Asp => Expo,
1726 Imp_Asp => Imp,
1727 LN_Asp => Dummy_3,
1728 Do_Checks => True);
1729
1730 -- Ensure that aspect External_Name applies to aspect Export or
1731 -- Import.
1732
1733 if A_Id = Aspect_External_Name then
1734 if No (Expo) and then No (Imp) then
89f1e35c 1735 Error_Msg_N
ee2b7923 1736 ("aspect `External_Name` requires aspect `Import` or "
1737 & "`Export`", Aspect);
89f1e35c 1738 end if;
ee2b7923 1739
1740 -- Otherwise ensure that aspect Link_Name applies to aspect
1741 -- Export or Import.
1742
1743 else
1744 pragma Assert (A_Id = Aspect_Link_Name);
1745 if No (Expo) and then No (Imp) then
1746 Error_Msg_N
1747 ("aspect `Link_Name` requires aspect `Import` or "
1748 & "`Export`", Aspect);
1749 end if;
1750 end if;
1751 end Analyze_Aspect_External_Link_Name;
21ea3a4f 1752
89f1e35c 1753 -----------------------------------------
1754 -- Analyze_Aspect_Implicit_Dereference --
1755 -----------------------------------------
21ea3a4f 1756
89f1e35c 1757 procedure Analyze_Aspect_Implicit_Dereference is
1ff43c00 1758 Disc : Entity_Id;
1759 Parent_Disc : Entity_Id;
1760
89f1e35c 1761 begin
b9e61b2a 1762 if not Is_Type (E) or else not Has_Discriminants (E) then
89f1e35c 1763 Error_Msg_N
1ff43c00 1764 ("aspect must apply to a type with discriminants", Expr);
21ea3a4f 1765
1ff43c00 1766 elsif not Is_Entity_Name (Expr) then
1767 Error_Msg_N
1768 ("aspect must name a discriminant of current type", Expr);
21ea3a4f 1769
1ff43c00 1770 else
1771 Disc := First_Discriminant (E);
1772 while Present (Disc) loop
1773 if Chars (Expr) = Chars (Disc)
1774 and then Ekind (Etype (Disc)) =
1775 E_Anonymous_Access_Type
1776 then
1777 Set_Has_Implicit_Dereference (E);
1778 Set_Has_Implicit_Dereference (Disc);
1779 exit;
1780 end if;
21ea3a4f 1781
1ff43c00 1782 Next_Discriminant (Disc);
1783 end loop;
21ea3a4f 1784
9b5b11fb 1785 -- Error if no proper access discriminant
21ea3a4f 1786
1ff43c00 1787 if No (Disc) then
ee2b7923 1788 Error_Msg_NE ("not an access discriminant of&", Expr, E);
1ff43c00 1789 return;
1790 end if;
1791 end if;
1792
9b5b11fb 1793 -- For a type extension, check whether parent has a
1794 -- reference discriminant, to verify that use is proper.
1795
1ff43c00 1796 if Is_Derived_Type (E)
1797 and then Has_Discriminants (Etype (E))
1798 then
1799 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1800
1801 if Present (Parent_Disc)
1802 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1803 then
ee2b7923 1804 Error_Msg_N
1805 ("reference discriminant does not match discriminant "
1806 & "of parent type", Expr);
1ff43c00 1807 end if;
89f1e35c 1808 end if;
1809 end Analyze_Aspect_Implicit_Dereference;
21ea3a4f 1810
0fd13d32 1811 -----------------------
1812 -- Make_Aitem_Pragma --
1813 -----------------------
1814
1815 procedure Make_Aitem_Pragma
1816 (Pragma_Argument_Associations : List_Id;
1817 Pragma_Name : Name_Id)
1818 is
b855559d 1819 Args : List_Id := Pragma_Argument_Associations;
1820
0fd13d32 1821 begin
1822 -- We should never get here if aspect was disabled
1823
1824 pragma Assert (not Is_Disabled (Aspect));
1825
056dc987 1826 -- Certain aspects allow for an optional name or expression. Do
1827 -- not generate a pragma with empty argument association list.
b855559d 1828
1829 if No (Args) or else No (Expression (First (Args))) then
1830 Args := No_List;
1831 end if;
1832
0fd13d32 1833 -- Build the pragma
1834
1835 Aitem :=
1836 Make_Pragma (Loc,
b855559d 1837 Pragma_Argument_Associations => Args,
0fd13d32 1838 Pragma_Identifier =>
1839 Make_Identifier (Sloc (Id), Pragma_Name),
9ab32fe9 1840 Class_Present => Class_Present (Aspect),
1841 Split_PPC => Split_PPC (Aspect));
0fd13d32 1842
1843 -- Set additional semantic fields
1844
1845 if Is_Ignored (Aspect) then
1846 Set_Is_Ignored (Aitem);
57d8d1f3 1847 elsif Is_Checked (Aspect) then
a5109493 1848 Set_Is_Checked (Aitem);
0fd13d32 1849 end if;
1850
1851 Set_Corresponding_Aspect (Aitem, Aspect);
fdec445e 1852 Set_From_Aspect_Specification (Aitem);
0fd13d32 1853 end Make_Aitem_Pragma;
1854
1855 -- Start of processing for Analyze_One_Aspect
1856
ae888dbd 1857 begin
2d1acfa7 1858 -- Skip aspect if already analyzed, to avoid looping in some cases
fb7f2fc4 1859
1860 if Analyzed (Aspect) then
1861 goto Continue;
1862 end if;
1863
ef957022 1864 -- Skip looking at aspect if it is totally disabled. Just mark it
1865 -- as such for later reference in the tree. This also sets the
1866 -- Is_Ignored and Is_Checked flags appropriately.
51ea9c94 1867
1868 Check_Applicable_Policy (Aspect);
1869
1870 if Is_Disabled (Aspect) then
1871 goto Continue;
1872 end if;
1873
c0793fff 1874 -- Set the source location of expression, used in the case of
1875 -- a failed precondition/postcondition or invariant. Note that
1876 -- the source location of the expression is not usually the best
1877 -- choice here. For example, it gets located on the last AND
1878 -- keyword in a chain of boolean expressiond AND'ed together.
1879 -- It is best to put the message on the first character of the
1880 -- assertion, which is the effect of the First_Node call here.
1881
1882 if Present (Expr) then
1883 Eloc := Sloc (First_Node (Expr));
1884 end if;
1885
d7ed83a2 1886 -- Check restriction No_Implementation_Aspect_Specifications
1887
c171e1be 1888 if Implementation_Defined_Aspect (A_Id) then
d7ed83a2 1889 Check_Restriction
1890 (No_Implementation_Aspect_Specifications, Aspect);
1891 end if;
1892
1893 -- Check restriction No_Specification_Of_Aspect
1894
1895 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1896
f67ed4f5 1897 -- Mark aspect analyzed (actual analysis is delayed till later)
d7ed83a2 1898
fb7f2fc4 1899 Set_Analyzed (Aspect);
d74fc39a 1900 Set_Entity (Aspect, E);
1901 Ent := New_Occurrence_Of (E, Sloc (Id));
1902
1e3c4ae6 1903 -- Check for duplicate aspect. Note that the Comes_From_Source
1904 -- test allows duplicate Pre/Post's that we generate internally
1905 -- to escape being flagged here.
ae888dbd 1906
6c545057 1907 if No_Duplicates_Allowed (A_Id) then
1908 Anod := First (L);
1909 while Anod /= Aspect loop
c171e1be 1910 if Comes_From_Source (Aspect)
1911 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
6c545057 1912 then
1913 Error_Msg_Name_1 := Nam;
1914 Error_Msg_Sloc := Sloc (Anod);
39e1f22f 1915
6c545057 1916 -- Case of same aspect specified twice
39e1f22f 1917
6c545057 1918 if Class_Present (Anod) = Class_Present (Aspect) then
1919 if not Class_Present (Anod) then
1920 Error_Msg_NE
1921 ("aspect% for & previously given#",
1922 Id, E);
1923 else
1924 Error_Msg_NE
1925 ("aspect `%''Class` for & previously given#",
1926 Id, E);
1927 end if;
39e1f22f 1928 end if;
6c545057 1929 end if;
ae888dbd 1930
6c545057 1931 Next (Anod);
1932 end loop;
1933 end if;
ae888dbd 1934
4db325e6 1935 -- Check some general restrictions on language defined aspects
1936
c171e1be 1937 if not Implementation_Defined_Aspect (A_Id) then
4db325e6 1938 Error_Msg_Name_1 := Nam;
1939
1940 -- Not allowed for renaming declarations
1941
1942 if Nkind (N) in N_Renaming_Declaration then
1943 Error_Msg_N
1944 ("aspect % not allowed for renaming declaration",
1945 Aspect);
1946 end if;
1947
1948 -- Not allowed for formal type declarations
1949
1950 if Nkind (N) = N_Formal_Type_Declaration then
1951 Error_Msg_N
1952 ("aspect % not allowed for formal type declaration",
1953 Aspect);
1954 end if;
1955 end if;
1956
7d20685d 1957 -- Copy expression for later processing by the procedures
1958 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1959
1960 Set_Entity (Id, New_Copy_Tree (Expr));
1961
37c6e44c 1962 -- Set Delay_Required as appropriate to aspect
1963
1964 case Aspect_Delay (A_Id) is
1965 when Always_Delay =>
1966 Delay_Required := True;
1967
1968 when Never_Delay =>
1969 Delay_Required := False;
1970
1971 when Rep_Aspect =>
1972
1973 -- If expression has the form of an integer literal, then
1974 -- do not delay, since we know the value cannot change.
1975 -- This optimization catches most rep clause cases.
1976
e43fc5c5 1977 -- For Boolean aspects, don't delay if no expression
1978
1979 if A_Id in Boolean_Aspects and then No (Expr) then
1980 Delay_Required := False;
1981
1982 -- For non-Boolean aspects, don't delay if integer literal
1983
1984 elsif A_Id not in Boolean_Aspects
1985 and then Present (Expr)
1986 and then Nkind (Expr) = N_Integer_Literal
1987 then
1988 Delay_Required := False;
1989
1990 -- All other cases are delayed
1991
1992 else
1993 Delay_Required := True;
1994 Set_Has_Delayed_Rep_Aspects (E);
1995 end if;
37c6e44c 1996 end case;
1997
ae888dbd 1998 -- Processing based on specific aspect
1999
d74fc39a 2000 case A_Id is
aa2f48d2 2001 when Aspect_Unimplemented =>
2002 null; -- ??? temp for now
ae888dbd 2003
2004 -- No_Aspect should be impossible
2005
2006 when No_Aspect =>
2007 raise Program_Error;
2008
89f1e35c 2009 -- Case 1: Aspects corresponding to attribute definition
2010 -- clauses.
ae888dbd 2011
b7b74740 2012 when Aspect_Address |
2013 Aspect_Alignment |
2014 Aspect_Bit_Order |
2015 Aspect_Component_Size |
89f1e35c 2016 Aspect_Constant_Indexing |
89f1e35c 2017 Aspect_Default_Iterator |
2018 Aspect_Dispatching_Domain |
b7b74740 2019 Aspect_External_Tag |
2020 Aspect_Input |
b3f8228a 2021 Aspect_Iterable |
89f1e35c 2022 Aspect_Iterator_Element |
b7b74740 2023 Aspect_Machine_Radix |
2024 Aspect_Object_Size |
2025 Aspect_Output |
2026 Aspect_Read |
2027 Aspect_Scalar_Storage_Order |
2028 Aspect_Size |
2029 Aspect_Small |
2030 Aspect_Simple_Storage_Pool |
2031 Aspect_Storage_Pool |
b7b74740 2032 Aspect_Stream_Size |
2033 Aspect_Value_Size |
89f1e35c 2034 Aspect_Variable_Indexing |
b7b74740 2035 Aspect_Write =>
d74fc39a 2036
89f1e35c 2037 -- Indexing aspects apply only to tagged type
2038
2039 if (A_Id = Aspect_Constant_Indexing
37c6e44c 2040 or else
2041 A_Id = Aspect_Variable_Indexing)
89f1e35c 2042 and then not (Is_Type (E)
2043 and then Is_Tagged_Type (E))
2044 then
05987af3 2045 Error_Msg_N
2046 ("indexing aspect can only apply to a tagged type",
3f4c9ffc 2047 Aspect);
89f1e35c 2048 goto Continue;
2049 end if;
2050
39616053 2051 -- For the case of aspect Address, we don't consider that we
588e7f97 2052 -- know the entity is never set in the source, since it is
2053 -- is likely aliasing is occurring.
2054
2055 -- Note: one might think that the analysis of the resulting
2056 -- attribute definition clause would take care of that, but
2057 -- that's not the case since it won't be from source.
2058
2059 if A_Id = Aspect_Address then
2060 Set_Never_Set_In_Source (E, False);
2061 end if;
2062
5ac76cee 2063 -- Correctness of the profile of a stream operation is
2064 -- verified at the freeze point, but we must detect the
2065 -- illegal specification of this aspect for a subtype now,
2066 -- to prevent malformed rep_item chains.
2067
fbf4d6ef 2068 if A_Id = Aspect_Input or else
2069 A_Id = Aspect_Output or else
2070 A_Id = Aspect_Read or else
2071 A_Id = Aspect_Write
5ac76cee 2072 then
fbf4d6ef 2073 if not Is_First_Subtype (E) then
2074 Error_Msg_N
2075 ("local name must be a first subtype", Aspect);
2076 goto Continue;
2077
2078 -- If stream aspect applies to the class-wide type,
2079 -- the generated attribute definition applies to the
2080 -- class-wide type as well.
2081
2082 elsif Class_Present (Aspect) then
2083 Ent :=
2084 Make_Attribute_Reference (Loc,
2085 Prefix => Ent,
2086 Attribute_Name => Name_Class);
2087 end if;
5ac76cee 2088 end if;
2089
d74fc39a 2090 -- Construct the attribute definition clause
2091
2092 Aitem :=
94153a42 2093 Make_Attribute_Definition_Clause (Loc,
d74fc39a 2094 Name => Ent,
ae888dbd 2095 Chars => Chars (Id),
2096 Expression => Relocate_Node (Expr));
2097
af9a0cc3 2098 -- If the address is specified, then we treat the entity as
41f06abf 2099 -- referenced, to avoid spurious warnings. This is analogous
2100 -- to what is done with an attribute definition clause, but
2101 -- here we don't want to generate a reference because this
2102 -- is the point of definition of the entity.
2103
2104 if A_Id = Aspect_Address then
2105 Set_Referenced (E);
2106 end if;
2107
51ea9c94 2108 -- Case 2: Aspects corresponding to pragmas
d74fc39a 2109
89f1e35c 2110 -- Case 2a: Aspects corresponding to pragmas with two
2111 -- arguments, where the first argument is a local name
2112 -- referring to the entity, and the second argument is the
2113 -- aspect definition expression.
ae888dbd 2114
04ae062f 2115 -- Linker_Section/Suppress/Unsuppress
0fd13d32 2116
04ae062f 2117 when Aspect_Linker_Section |
2118 Aspect_Suppress |
2119 Aspect_Unsuppress =>
ae888dbd 2120
0fd13d32 2121 Make_Aitem_Pragma
2122 (Pragma_Argument_Associations => New_List (
2123 Make_Pragma_Argument_Association (Loc,
2124 Expression => New_Occurrence_Of (E, Loc)),
2125 Make_Pragma_Argument_Association (Sloc (Expr),
2126 Expression => Relocate_Node (Expr))),
2127 Pragma_Name => Chars (Id));
57cd943b 2128
0fd13d32 2129 -- Synchronization
d74fc39a 2130
0fd13d32 2131 -- Corresponds to pragma Implemented, construct the pragma
49213728 2132
5bbfbad2 2133 when Aspect_Synchronization =>
0fd13d32 2134 Make_Aitem_Pragma
2135 (Pragma_Argument_Associations => New_List (
2136 Make_Pragma_Argument_Association (Loc,
2137 Expression => New_Occurrence_Of (E, Loc)),
2138 Make_Pragma_Argument_Association (Sloc (Expr),
2139 Expression => Relocate_Node (Expr))),
2140 Pragma_Name => Name_Implemented);
49213728 2141
e2bf777d 2142 -- Attach_Handler
0fd13d32 2143
89f1e35c 2144 when Aspect_Attach_Handler =>
0fd13d32 2145 Make_Aitem_Pragma
2146 (Pragma_Argument_Associations => New_List (
2147 Make_Pragma_Argument_Association (Sloc (Ent),
2148 Expression => Ent),
2149 Make_Pragma_Argument_Association (Sloc (Expr),
2150 Expression => Relocate_Node (Expr))),
2151 Pragma_Name => Name_Attach_Handler);
2152
f67ed4f5 2153 -- We need to insert this pragma into the tree to get proper
2154 -- processing and to look valid from a placement viewpoint.
2155
e2bf777d 2156 Insert_Pragma (Aitem);
f67ed4f5 2157 goto Continue;
2158
0fd13d32 2159 -- Dynamic_Predicate, Predicate, Static_Predicate
89f1e35c 2160
2161 when Aspect_Dynamic_Predicate |
2162 Aspect_Predicate |
2163 Aspect_Static_Predicate =>
2164
a47ce82d 2165 -- These aspects apply only to subtypes
2166
2167 if not Is_Type (E) then
2168 Error_Msg_N
2169 ("predicate can only be specified for a subtype",
2170 Aspect);
2171 goto Continue;
7c0c95b8 2172
2173 elsif Is_Incomplete_Type (E) then
2174 Error_Msg_N
2175 ("predicate cannot apply to incomplete view", Aspect);
2176 goto Continue;
a47ce82d 2177 end if;
2178
89f1e35c 2179 -- Construct the pragma (always a pragma Predicate, with
51ea9c94 2180 -- flags recording whether it is static/dynamic). We also
2181 -- set flags recording this in the type itself.
89f1e35c 2182
0fd13d32 2183 Make_Aitem_Pragma
2184 (Pragma_Argument_Associations => New_List (
2185 Make_Pragma_Argument_Association (Sloc (Ent),
2186 Expression => Ent),
2187 Make_Pragma_Argument_Association (Sloc (Expr),
2188 Expression => Relocate_Node (Expr))),
fdec445e 2189 Pragma_Name => Name_Predicate);
89f1e35c 2190
51ea9c94 2191 -- Mark type has predicates, and remember what kind of
2192 -- aspect lead to this predicate (we need this to access
2193 -- the right set of check policies later on).
2194
2195 Set_Has_Predicates (E);
2196
2197 if A_Id = Aspect_Dynamic_Predicate then
2198 Set_Has_Dynamic_Predicate_Aspect (E);
2199 elsif A_Id = Aspect_Static_Predicate then
2200 Set_Has_Static_Predicate_Aspect (E);
2201 end if;
2202
89f1e35c 2203 -- If the type is private, indicate that its completion
6653b695 2204 -- has a freeze node, because that is the one that will
2205 -- be visible at freeze time.
89f1e35c 2206
0fd13d32 2207 if Is_Private_Type (E) and then Present (Full_View (E)) then
89f1e35c 2208 Set_Has_Predicates (Full_View (E));
51ea9c94 2209
2210 if A_Id = Aspect_Dynamic_Predicate then
2211 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2212 elsif A_Id = Aspect_Static_Predicate then
2213 Set_Has_Static_Predicate_Aspect (Full_View (E));
2214 end if;
2215
89f1e35c 2216 Set_Has_Delayed_Aspects (Full_View (E));
2217 Ensure_Freeze_Node (Full_View (E));
2218 end if;
2219
fdec445e 2220 -- Predicate_Failure
2221
2222 when Aspect_Predicate_Failure =>
2223
2224 -- This aspect applies only to subtypes
2225
2226 if not Is_Type (E) then
2227 Error_Msg_N
2228 ("predicate can only be specified for a subtype",
2229 Aspect);
2230 goto Continue;
2231
2232 elsif Is_Incomplete_Type (E) then
2233 Error_Msg_N
2234 ("predicate cannot apply to incomplete view", Aspect);
2235 goto Continue;
2236 end if;
2237
2238 -- Construct the pragma
2239
2240 Make_Aitem_Pragma
2241 (Pragma_Argument_Associations => New_List (
2242 Make_Pragma_Argument_Association (Sloc (Ent),
2243 Expression => Ent),
2244 Make_Pragma_Argument_Association (Sloc (Expr),
2245 Expression => Relocate_Node (Expr))),
2246 Pragma_Name => Name_Predicate_Failure);
2247
2248 Set_Has_Predicates (E);
2249
2250 -- If the type is private, indicate that its completion
2251 -- has a freeze node, because that is the one that will
2252 -- be visible at freeze time.
2253
2254 if Is_Private_Type (E) and then Present (Full_View (E)) then
2255 Set_Has_Predicates (Full_View (E));
2256 Set_Has_Delayed_Aspects (Full_View (E));
2257 Ensure_Freeze_Node (Full_View (E));
2258 end if;
2259
89f1e35c 2260 -- Case 2b: Aspects corresponding to pragmas with two
2261 -- arguments, where the second argument is a local name
2262 -- referring to the entity, and the first argument is the
2263 -- aspect definition expression.
ae888dbd 2264
0fd13d32 2265 -- Convention
2266
ee2b7923 2267 when Aspect_Convention =>
2268 Analyze_Aspect_Convention;
2269 goto Continue;
97bf66e6 2270
ee2b7923 2271 -- External_Name, Link_Name
97bf66e6 2272
ee2b7923 2273 when Aspect_External_Name |
2274 Aspect_Link_Name =>
2275 Analyze_Aspect_External_Link_Name;
2276 goto Continue;
e1cedbae 2277
0fd13d32 2278 -- CPU, Interrupt_Priority, Priority
2279
d6814978 2280 -- These three aspects can be specified for a subprogram spec
2281 -- or body, in which case we analyze the expression and export
2282 -- the value of the aspect.
2283
2284 -- Previously, we generated an equivalent pragma for bodies
2285 -- (note that the specs cannot contain these pragmas). The
2286 -- pragma was inserted ahead of local declarations, rather than
2287 -- after the body. This leads to a certain duplication between
2288 -- the processing performed for the aspect and the pragma, but
2289 -- given the straightforward handling required it is simpler
2290 -- to duplicate than to translate the aspect in the spec into
2291 -- a pragma in the declarative part of the body.
3a72f9c3 2292
2293 when Aspect_CPU |
2294 Aspect_Interrupt_Priority |
2295 Aspect_Priority =>
51ea9c94 2296
d6814978 2297 if Nkind_In (N, N_Subprogram_Body,
2298 N_Subprogram_Declaration)
2299 then
2300 -- Analyze the aspect expression
2301
2302 Analyze_And_Resolve (Expr, Standard_Integer);
2303
2304 -- Interrupt_Priority aspect not allowed for main
078a74b8 2305 -- subprograms. RM D.1 does not forbid this explicitly,
2306 -- but RM J.15.11(6/3) does not permit pragma
d6814978 2307 -- Interrupt_Priority for subprograms.
2308
2309 if A_Id = Aspect_Interrupt_Priority then
2310 Error_Msg_N
2311 ("Interrupt_Priority aspect cannot apply to "
2312 & "subprogram", Expr);
2313
2314 -- The expression must be static
2315
cda40848 2316 elsif not Is_OK_Static_Expression (Expr) then
d6814978 2317 Flag_Non_Static_Expr
2318 ("aspect requires static expression!", Expr);
2319
24d7b9d6 2320 -- Check whether this is the main subprogram. Issue a
2321 -- warning only if it is obviously not a main program
2322 -- (when it has parameters or when the subprogram is
2323 -- within a package).
2324
2325 elsif Present (Parameter_Specifications
2326 (Specification (N)))
2327 or else not Is_Compilation_Unit (Defining_Entity (N))
d6814978 2328 then
078a74b8 2329 -- See RM D.1(14/3) and D.16(12/3)
d6814978 2330
2331 Error_Msg_N
2332 ("aspect applied to subprogram other than the "
2333 & "main subprogram has no effect??", Expr);
2334
2335 -- Otherwise check in range and export the value
2336
2337 -- For the CPU aspect
2338
2339 elsif A_Id = Aspect_CPU then
2340 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2341
2342 -- Value is correct so we export the value to make
2343 -- it available at execution time.
2344
2345 Set_Main_CPU
2346 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2347
2348 else
2349 Error_Msg_N
2350 ("main subprogram CPU is out of range", Expr);
2351 end if;
2352
2353 -- For the Priority aspect
2354
2355 elsif A_Id = Aspect_Priority then
2356 if Is_In_Range (Expr, RTE (RE_Priority)) then
2357
2358 -- Value is correct so we export the value to make
2359 -- it available at execution time.
2360
2361 Set_Main_Priority
2362 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2363
32572384 2364 -- Ignore pragma if Relaxed_RM_Semantics to support
2365 -- other targets/non GNAT compilers.
2366
2367 elsif not Relaxed_RM_Semantics then
d6814978 2368 Error_Msg_N
2369 ("main subprogram priority is out of range",
2370 Expr);
2371 end if;
2372 end if;
2373
2374 -- Load an arbitrary entity from System.Tasking.Stages
2375 -- or System.Tasking.Restricted.Stages (depending on
2376 -- the supported profile) to make sure that one of these
2377 -- packages is implicitly with'ed, since we need to have
2378 -- the tasking run time active for the pragma Priority to
a0c3eeb9 2379 -- have any effect. Previously we with'ed the package
d6814978 2380 -- System.Tasking, but this package does not trigger the
2381 -- required initialization of the run-time library.
2382
2383 declare
2384 Discard : Entity_Id;
d6814978 2385 begin
2386 if Restricted_Profile then
2387 Discard := RTE (RE_Activate_Restricted_Tasks);
2388 else
2389 Discard := RTE (RE_Activate_Tasks);
2390 end if;
2391 end;
2392
2393 -- Handling for these Aspects in subprograms is complete
2394
2395 goto Continue;
2396
2f06c88a 2397 -- For tasks pass the aspect as an attribute
0fd13d32 2398
3a72f9c3 2399 else
2400 Aitem :=
2401 Make_Attribute_Definition_Clause (Loc,
2402 Name => Ent,
2403 Chars => Chars (Id),
2404 Expression => Relocate_Node (Expr));
2405 end if;
2406
0fd13d32 2407 -- Warnings
2408
ae888dbd 2409 when Aspect_Warnings =>
0fd13d32 2410 Make_Aitem_Pragma
2411 (Pragma_Argument_Associations => New_List (
2412 Make_Pragma_Argument_Association (Sloc (Expr),
2413 Expression => Relocate_Node (Expr)),
2414 Make_Pragma_Argument_Association (Loc,
2415 Expression => New_Occurrence_Of (E, Loc))),
2416 Pragma_Name => Chars (Id));
ae888dbd 2417
2f06c88a 2418 Decorate (Aspect, Aitem);
2419 Insert_Pragma (Aitem);
2420 goto Continue;
2421
89f1e35c 2422 -- Case 2c: Aspects corresponding to pragmas with three
2423 -- arguments.
d64221a7 2424
89f1e35c 2425 -- Invariant aspects have a first argument that references the
2426 -- entity, a second argument that is the expression and a third
2427 -- argument that is an appropriate message.
d64221a7 2428
0fd13d32 2429 -- Invariant, Type_Invariant
2430
89f1e35c 2431 when Aspect_Invariant |
2432 Aspect_Type_Invariant =>
d64221a7 2433
89f1e35c 2434 -- Analysis of the pragma will verify placement legality:
2435 -- an invariant must apply to a private type, or appear in
2436 -- the private part of a spec and apply to a completion.
d64221a7 2437
0fd13d32 2438 Make_Aitem_Pragma
2439 (Pragma_Argument_Associations => New_List (
2440 Make_Pragma_Argument_Association (Sloc (Ent),
2441 Expression => Ent),
2442 Make_Pragma_Argument_Association (Sloc (Expr),
2443 Expression => Relocate_Node (Expr))),
2444 Pragma_Name => Name_Invariant);
89f1e35c 2445
2446 -- Add message unless exception messages are suppressed
2447
2448 if not Opt.Exception_Locations_Suppressed then
2449 Append_To (Pragma_Argument_Associations (Aitem),
2450 Make_Pragma_Argument_Association (Eloc,
2451 Chars => Name_Message,
2452 Expression =>
2453 Make_String_Literal (Eloc,
2454 Strval => "failed invariant from "
2455 & Build_Location_String (Eloc))));
d64221a7 2456 end if;
2457
89f1e35c 2458 -- For Invariant case, insert immediately after the entity
2459 -- declaration. We do not have to worry about delay issues
2460 -- since the pragma processing takes care of this.
2461
89f1e35c 2462 Delay_Required := False;
d64221a7 2463
47a46747 2464 -- Case 2d : Aspects that correspond to a pragma with one
2465 -- argument.
2466
0fd13d32 2467 -- Abstract_State
115f7b08 2468
d4e369ad 2469 -- Aspect Abstract_State introduces implicit declarations for
2470 -- all state abstraction entities it defines. To emulate this
2471 -- behavior, insert the pragma at the beginning of the visible
2472 -- declarations of the related package so that it is analyzed
2473 -- immediately.
2474
9129c28f 2475 when Aspect_Abstract_State => Abstract_State : declare
eb4f7efa 2476 Context : Node_Id := N;
9129c28f 2477
2478 begin
eb4f7efa 2479 -- When aspect Abstract_State appears on a generic package,
2480 -- it is propageted to the package instance. The context in
2481 -- this case is the instance spec.
2482
2483 if Nkind (Context) = N_Package_Instantiation then
2484 Context := Instance_Spec (Context);
2485 end if;
2486
2487 if Nkind_In (Context, N_Generic_Package_Declaration,
2488 N_Package_Declaration)
9129c28f 2489 then
9129c28f 2490 Make_Aitem_Pragma
2491 (Pragma_Argument_Associations => New_List (
2492 Make_Pragma_Argument_Association (Loc,
2493 Expression => Relocate_Node (Expr))),
2494 Pragma_Name => Name_Abstract_State);
630b6d55 2495
5655be8a 2496 Decorate (Aspect, Aitem);
2497 Insert_Pragma
2498 (Prag => Aitem,
2499 Is_Instance =>
2500 Is_Generic_Instance (Defining_Entity (Context)));
9129c28f 2501
2502 else
2503 Error_Msg_NE
2504 ("aspect & must apply to a package declaration",
2505 Aspect, Id);
2506 end if;
2507
2508 goto Continue;
2509 end Abstract_State;
115f7b08 2510
85ee12c0 2511 -- Aspect Async_Readers is never delayed because it is
2512 -- equivalent to a source pragma which appears after the
2513 -- related object declaration.
2514
2515 when Aspect_Async_Readers =>
2516 Make_Aitem_Pragma
2517 (Pragma_Argument_Associations => New_List (
2518 Make_Pragma_Argument_Association (Loc,
2519 Expression => Relocate_Node (Expr))),
2520 Pragma_Name => Name_Async_Readers);
2521
2522 Decorate (Aspect, Aitem);
2523 Insert_Pragma (Aitem);
2524 goto Continue;
2525
2526 -- Aspect Async_Writers is never delayed because it is
2527 -- equivalent to a source pragma which appears after the
2528 -- related object declaration.
2529
2530 when Aspect_Async_Writers =>
2531 Make_Aitem_Pragma
2532 (Pragma_Argument_Associations => New_List (
2533 Make_Pragma_Argument_Association (Loc,
2534 Expression => Relocate_Node (Expr))),
2535 Pragma_Name => Name_Async_Writers);
2536
2537 Decorate (Aspect, Aitem);
2538 Insert_Pragma (Aitem);
2539 goto Continue;
2540
d0849c23 2541 -- Aspect Constant_After_Elaboration is never delayed because
2542 -- it is equivalent to a source pragma which appears after the
2543 -- related object declaration.
2544
2545 when Aspect_Constant_After_Elaboration =>
2546 Make_Aitem_Pragma
2547 (Pragma_Argument_Associations => New_List (
2548 Make_Pragma_Argument_Association (Loc,
2549 Expression => Relocate_Node (Expr))),
2550 Pragma_Name =>
2551 Name_Constant_After_Elaboration);
2552
2553 Decorate (Aspect, Aitem);
2554 Insert_Pragma (Aitem);
2555 goto Continue;
2556
ec6f6da5 2557 -- Aspect Default_Internal_Condition is never delayed because
2558 -- it is equivalent to a source pragma which appears after the
2559 -- related private type. To deal with forward references, the
2560 -- generated pragma is stored in the rep chain of the related
2561 -- private type as types do not carry contracts. The pragma is
2562 -- wrapped inside of a procedure at the freeze point of the
2563 -- private type's full view.
2564
2565 when Aspect_Default_Initial_Condition =>
2566 Make_Aitem_Pragma
2567 (Pragma_Argument_Associations => New_List (
2568 Make_Pragma_Argument_Association (Loc,
2569 Expression => Relocate_Node (Expr))),
2570 Pragma_Name =>
2571 Name_Default_Initial_Condition);
2572
2573 Decorate (Aspect, Aitem);
2574 Insert_Pragma (Aitem);
2575 goto Continue;
2576
647fab54 2577 -- Default_Storage_Pool
2578
2579 when Aspect_Default_Storage_Pool =>
2580 Make_Aitem_Pragma
2581 (Pragma_Argument_Associations => New_List (
2582 Make_Pragma_Argument_Association (Loc,
2583 Expression => Relocate_Node (Expr))),
2584 Pragma_Name =>
2585 Name_Default_Storage_Pool);
2586
2587 Decorate (Aspect, Aitem);
2588 Insert_Pragma (Aitem);
2589 goto Continue;
2590
0fd13d32 2591 -- Depends
2592
e2bf777d 2593 -- Aspect Depends is never delayed because it is equivalent to
2594 -- a source pragma which appears after the related subprogram.
2595 -- To deal with forward references, the generated pragma is
2596 -- stored in the contract of the related subprogram and later
2597 -- analyzed at the end of the declarative region. See routine
2598 -- Analyze_Depends_In_Decl_Part for details.
6144c105 2599
12334c57 2600 when Aspect_Depends =>
0fd13d32 2601 Make_Aitem_Pragma
2602 (Pragma_Argument_Associations => New_List (
2603 Make_Pragma_Argument_Association (Loc,
2604 Expression => Relocate_Node (Expr))),
2605 Pragma_Name => Name_Depends);
2606
e2bf777d 2607 Decorate (Aspect, Aitem);
2608 Insert_Pragma (Aitem);
c1006d6d 2609 goto Continue;
2610
85ee12c0 2611 -- Aspect Effecitve_Reads is never delayed because it is
2612 -- equivalent to a source pragma which appears after the
2613 -- related object declaration.
2614
2615 when Aspect_Effective_Reads =>
2616 Make_Aitem_Pragma
2617 (Pragma_Argument_Associations => New_List (
2618 Make_Pragma_Argument_Association (Loc,
2619 Expression => Relocate_Node (Expr))),
2620 Pragma_Name => Name_Effective_Reads);
2621
2622 Decorate (Aspect, Aitem);
2623 Insert_Pragma (Aitem);
2624 goto Continue;
2625
2626 -- Aspect Effective_Writes is never delayed because it is
2627 -- equivalent to a source pragma which appears after the
2628 -- related object declaration.
2629
2630 when Aspect_Effective_Writes =>
2631 Make_Aitem_Pragma
2632 (Pragma_Argument_Associations => New_List (
2633 Make_Pragma_Argument_Association (Loc,
2634 Expression => Relocate_Node (Expr))),
2635 Pragma_Name => Name_Effective_Writes);
2636
2637 Decorate (Aspect, Aitem);
2638 Insert_Pragma (Aitem);
2639 goto Continue;
2640
cab27d2a 2641 -- Aspect Extensions_Visible is never delayed because it is
2642 -- equivalent to a source pragma which appears after the
2643 -- related subprogram.
2644
2645 when Aspect_Extensions_Visible =>
2646 Make_Aitem_Pragma
2647 (Pragma_Argument_Associations => New_List (
2648 Make_Pragma_Argument_Association (Loc,
2649 Expression => Relocate_Node (Expr))),
2650 Pragma_Name => Name_Extensions_Visible);
2651
2652 Decorate (Aspect, Aitem);
2653 Insert_Pragma (Aitem);
2654 goto Continue;
2655
3dbe7a69 2656 -- Aspect Ghost is never delayed because it is equivalent to a
2657 -- source pragma which appears at the top of [generic] package
2658 -- declarations or after an object, a [generic] subprogram, or
2659 -- a type declaration.
2660
5655be8a 2661 when Aspect_Ghost =>
3dbe7a69 2662 Make_Aitem_Pragma
2663 (Pragma_Argument_Associations => New_List (
2664 Make_Pragma_Argument_Association (Loc,
2665 Expression => Relocate_Node (Expr))),
2666 Pragma_Name => Name_Ghost);
2667
2668 Decorate (Aspect, Aitem);
5655be8a 2669 Insert_Pragma (Aitem);
3dbe7a69 2670 goto Continue;
3dbe7a69 2671
0fd13d32 2672 -- Global
12334c57 2673
e2bf777d 2674 -- Aspect Global is never delayed because it is equivalent to
2675 -- a source pragma which appears after the related subprogram.
2676 -- To deal with forward references, the generated pragma is
2677 -- stored in the contract of the related subprogram and later
2678 -- analyzed at the end of the declarative region. See routine
2679 -- Analyze_Global_In_Decl_Part for details.
3cdbaa5a 2680
2681 when Aspect_Global =>
0fd13d32 2682 Make_Aitem_Pragma
2683 (Pragma_Argument_Associations => New_List (
2684 Make_Pragma_Argument_Association (Loc,
2685 Expression => Relocate_Node (Expr))),
2686 Pragma_Name => Name_Global);
2687
e2bf777d 2688 Decorate (Aspect, Aitem);
2689 Insert_Pragma (Aitem);
c1006d6d 2690 goto Continue;
2691
9c138530 2692 -- Initial_Condition
2693
e2bf777d 2694 -- Aspect Initial_Condition is never delayed because it is
2695 -- equivalent to a source pragma which appears after the
2696 -- related package. To deal with forward references, the
2697 -- generated pragma is stored in the contract of the related
2698 -- package and later analyzed at the end of the declarative
2699 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2700 -- for details.
9c138530 2701
2702 when Aspect_Initial_Condition => Initial_Condition : declare
eb4f7efa 2703 Context : Node_Id := N;
9c138530 2704
2705 begin
e2bf777d 2706 -- When aspect Initial_Condition appears on a generic
2707 -- package, it is propageted to the package instance. The
2708 -- context in this case is the instance spec.
eb4f7efa 2709
2710 if Nkind (Context) = N_Package_Instantiation then
2711 Context := Instance_Spec (Context);
2712 end if;
2713
2714 if Nkind_In (Context, N_Generic_Package_Declaration,
2715 N_Package_Declaration)
9c138530 2716 then
9c138530 2717 Make_Aitem_Pragma
2718 (Pragma_Argument_Associations => New_List (
2719 Make_Pragma_Argument_Association (Loc,
2720 Expression => Relocate_Node (Expr))),
2721 Pragma_Name =>
2722 Name_Initial_Condition);
9c138530 2723
5655be8a 2724 Decorate (Aspect, Aitem);
2725 Insert_Pragma
2726 (Prag => Aitem,
2727 Is_Instance =>
2728 Is_Generic_Instance (Defining_Entity (Context)));
50e44732 2729
5655be8a 2730 -- Otherwise the context is illegal
9c138530 2731
2732 else
2733 Error_Msg_NE
2734 ("aspect & must apply to a package declaration",
2735 Aspect, Id);
2736 end if;
2737
2738 goto Continue;
2739 end Initial_Condition;
2740
d4e369ad 2741 -- Initializes
2742
e2bf777d 2743 -- Aspect Initializes is never delayed because it is equivalent
2744 -- to a source pragma appearing after the related package. To
2745 -- deal with forward references, the generated pragma is stored
2746 -- in the contract of the related package and later analyzed at
2747 -- the end of the declarative region. For details, see routine
2748 -- Analyze_Initializes_In_Decl_Part.
d4e369ad 2749
2750 when Aspect_Initializes => Initializes : declare
eb4f7efa 2751 Context : Node_Id := N;
d4e369ad 2752
2753 begin
50e44732 2754 -- When aspect Initializes appears on a generic package,
2755 -- it is propageted to the package instance. The context
2756 -- in this case is the instance spec.
eb4f7efa 2757
2758 if Nkind (Context) = N_Package_Instantiation then
2759 Context := Instance_Spec (Context);
2760 end if;
2761
2762 if Nkind_In (Context, N_Generic_Package_Declaration,
2763 N_Package_Declaration)
d4e369ad 2764 then
d4e369ad 2765 Make_Aitem_Pragma
2766 (Pragma_Argument_Associations => New_List (
2767 Make_Pragma_Argument_Association (Loc,
2768 Expression => Relocate_Node (Expr))),
2769 Pragma_Name => Name_Initializes);
d4e369ad 2770
5655be8a 2771 Decorate (Aspect, Aitem);
2772 Insert_Pragma
2773 (Prag => Aitem,
2774 Is_Instance =>
2775 Is_Generic_Instance (Defining_Entity (Context)));
50e44732 2776
5655be8a 2777 -- Otherwise the context is illegal
d4e369ad 2778
2779 else
2780 Error_Msg_NE
2781 ("aspect & must apply to a package declaration",
2782 Aspect, Id);
2783 end if;
2784
2785 goto Continue;
2786 end Initializes;
2787
1fd4313f 2788 -- Obsolescent
2789
2790 when Aspect_Obsolescent => declare
2791 Args : List_Id;
2792
2793 begin
2794 if No (Expr) then
2795 Args := No_List;
2796 else
2797 Args := New_List (
2798 Make_Pragma_Argument_Association (Sloc (Expr),
2799 Expression => Relocate_Node (Expr)));
2800 end if;
2801
2802 Make_Aitem_Pragma
2803 (Pragma_Argument_Associations => Args,
2804 Pragma_Name => Chars (Id));
2805 end;
2806
5cc6f0cf 2807 -- Part_Of
2808
2809 when Aspect_Part_Of =>
2810 if Nkind_In (N, N_Object_Declaration,
2811 N_Package_Instantiation)
736b80cc 2812 or else Is_Single_Concurrent_Type_Declaration (N)
5cc6f0cf 2813 then
2814 Make_Aitem_Pragma
2815 (Pragma_Argument_Associations => New_List (
2816 Make_Pragma_Argument_Association (Loc,
2817 Expression => Relocate_Node (Expr))),
2818 Pragma_Name => Name_Part_Of);
2819
736b80cc 2820 Decorate (Aspect, Aitem);
2821 Insert_Pragma (Aitem);
736b80cc 2822
5cc6f0cf 2823 else
2824 Error_Msg_NE
736b80cc 2825 ("aspect & must apply to package instantiation, "
2826 & "object, single protected type or single task type",
2827 Aspect, Id);
5cc6f0cf 2828 end if;
2829
d5c65b80 2830 goto Continue;
2831
5dd93a61 2832 -- SPARK_Mode
2833
2f06c88a 2834 when Aspect_SPARK_Mode =>
5dd93a61 2835 Make_Aitem_Pragma
2836 (Pragma_Argument_Associations => New_List (
2837 Make_Pragma_Argument_Association (Loc,
2838 Expression => Relocate_Node (Expr))),
2839 Pragma_Name => Name_SPARK_Mode);
5dd93a61 2840
2f06c88a 2841 Decorate (Aspect, Aitem);
2842 Insert_Pragma (Aitem);
2843 goto Continue;
778ebf56 2844
4befb1a0 2845 -- Refined_Depends
2846
e2bf777d 2847 -- Aspect Refined_Depends is never delayed because it is
2848 -- equivalent to a source pragma which appears in the
2849 -- declarations of the related subprogram body. To deal with
2850 -- forward references, the generated pragma is stored in the
2851 -- contract of the related subprogram body and later analyzed
2852 -- at the end of the declarative region. For details, see
2853 -- routine Analyze_Refined_Depends_In_Decl_Part.
4befb1a0 2854
2855 when Aspect_Refined_Depends =>
422073ed 2856 Make_Aitem_Pragma
2857 (Pragma_Argument_Associations => New_List (
2858 Make_Pragma_Argument_Association (Loc,
2859 Expression => Relocate_Node (Expr))),
2860 Pragma_Name => Name_Refined_Depends);
2861
e2bf777d 2862 Decorate (Aspect, Aitem);
2863 Insert_Pragma (Aitem);
422073ed 2864 goto Continue;
4befb1a0 2865
2866 -- Refined_Global
2867
e2bf777d 2868 -- Aspect Refined_Global is never delayed because it is
2869 -- equivalent to a source pragma which appears in the
2870 -- declarations of the related subprogram body. To deal with
2871 -- forward references, the generated pragma is stored in the
2872 -- contract of the related subprogram body and later analyzed
2873 -- at the end of the declarative region. For details, see
2874 -- routine Analyze_Refined_Global_In_Decl_Part.
4befb1a0 2875
2876 when Aspect_Refined_Global =>
28ff117f 2877 Make_Aitem_Pragma
2878 (Pragma_Argument_Associations => New_List (
2879 Make_Pragma_Argument_Association (Loc,
2880 Expression => Relocate_Node (Expr))),
2881 Pragma_Name => Name_Refined_Global);
2882
e2bf777d 2883 Decorate (Aspect, Aitem);
2884 Insert_Pragma (Aitem);
28ff117f 2885 goto Continue;
4befb1a0 2886
63b65b2d 2887 -- Refined_Post
2888
2889 when Aspect_Refined_Post =>
2890 Make_Aitem_Pragma
2891 (Pragma_Argument_Associations => New_List (
2892 Make_Pragma_Argument_Association (Loc,
2893 Expression => Relocate_Node (Expr))),
2894 Pragma_Name => Name_Refined_Post);
2895
3ff5e35d 2896 Decorate (Aspect, Aitem);
2897 Insert_Pragma (Aitem);
2898 goto Continue;
2899
9129c28f 2900 -- Refined_State
2901
5655be8a 2902 when Aspect_Refined_State =>
9129c28f 2903
9129c28f 2904 -- The corresponding pragma for Refined_State is inserted in
2905 -- the declarations of the related package body. This action
2906 -- synchronizes both the source and from-aspect versions of
2907 -- the pragma.
2908
2909 if Nkind (N) = N_Package_Body then
9129c28f 2910 Make_Aitem_Pragma
2911 (Pragma_Argument_Associations => New_List (
2912 Make_Pragma_Argument_Association (Loc,
2913 Expression => Relocate_Node (Expr))),
2914 Pragma_Name => Name_Refined_State);
b9b2d6e5 2915
5655be8a 2916 Decorate (Aspect, Aitem);
2917 Insert_Pragma (Aitem);
b9b2d6e5 2918
5655be8a 2919 -- Otherwise the context is illegal
9129c28f 2920
2921 else
2922 Error_Msg_NE
2923 ("aspect & must apply to a package body", Aspect, Id);
2924 end if;
2925
2926 goto Continue;
9129c28f 2927
0fd13d32 2928 -- Relative_Deadline
3cdbaa5a 2929
2930 when Aspect_Relative_Deadline =>
0fd13d32 2931 Make_Aitem_Pragma
2932 (Pragma_Argument_Associations => New_List (
2933 Make_Pragma_Argument_Association (Loc,
2934 Expression => Relocate_Node (Expr))),
2935 Pragma_Name => Name_Relative_Deadline);
47a46747 2936
2937 -- If the aspect applies to a task, the corresponding pragma
2938 -- must appear within its declarations, not after.
2939
2940 if Nkind (N) = N_Task_Type_Declaration then
2941 declare
2942 Def : Node_Id;
2943 V : List_Id;
2944
2945 begin
2946 if No (Task_Definition (N)) then
2947 Set_Task_Definition (N,
2948 Make_Task_Definition (Loc,
2949 Visible_Declarations => New_List,
2950 End_Label => Empty));
2951 end if;
2952
2953 Def := Task_Definition (N);
2954 V := Visible_Declarations (Def);
2955 if not Is_Empty_List (V) then
2956 Insert_Before (First (V), Aitem);
2957
2958 else
2959 Set_Visible_Declarations (Def, New_List (Aitem));
2960 end if;
2961
2962 goto Continue;
2963 end;
2964 end if;
2965
85ee12c0 2966 -- Aspect Volatile_Function is never delayed because it is
2967 -- equivalent to a source pragma which appears after the
2968 -- related subprogram.
2969
2970 when Aspect_Volatile_Function =>
2971 Make_Aitem_Pragma
2972 (Pragma_Argument_Associations => New_List (
2973 Make_Pragma_Argument_Association (Loc,
2974 Expression => Relocate_Node (Expr))),
2975 Pragma_Name => Name_Volatile_Function);
2976
2977 Decorate (Aspect, Aitem);
2978 Insert_Pragma (Aitem);
2979 goto Continue;
2980
956ffaf4 2981 -- Case 2e: Annotate aspect
2982
2983 when Aspect_Annotate =>
2984 declare
2985 Args : List_Id;
2986 Pargs : List_Id;
2987 Arg : Node_Id;
2988
2989 begin
2990 -- The argument can be a single identifier
2991
2992 if Nkind (Expr) = N_Identifier then
2993
2994 -- One level of parens is allowed
2995
2996 if Paren_Count (Expr) > 1 then
2997 Error_Msg_F ("extra parentheses ignored", Expr);
2998 end if;
2999
3000 Set_Paren_Count (Expr, 0);
3001
3002 -- Add the single item to the list
3003
3004 Args := New_List (Expr);
3005
3006 -- Otherwise we must have an aggregate
3007
3008 elsif Nkind (Expr) = N_Aggregate then
3009
3010 -- Must be positional
3011
3012 if Present (Component_Associations (Expr)) then
3013 Error_Msg_F
3014 ("purely positional aggregate required", Expr);
3015 goto Continue;
3016 end if;
3017
3018 -- Must not be parenthesized
3019
3020 if Paren_Count (Expr) /= 0 then
3021 Error_Msg_F ("extra parentheses ignored", Expr);
3022 end if;
3023
3024 -- List of arguments is list of aggregate expressions
3025
3026 Args := Expressions (Expr);
3027
3028 -- Anything else is illegal
3029
3030 else
3031 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3032 goto Continue;
3033 end if;
3034
3035 -- Prepare pragma arguments
3036
3037 Pargs := New_List;
3038 Arg := First (Args);
3039 while Present (Arg) loop
3040 Append_To (Pargs,
3041 Make_Pragma_Argument_Association (Sloc (Arg),
3042 Expression => Relocate_Node (Arg)));
3043 Next (Arg);
3044 end loop;
3045
3046 Append_To (Pargs,
3047 Make_Pragma_Argument_Association (Sloc (Ent),
3048 Chars => Name_Entity,
3049 Expression => Ent));
3050
3051 Make_Aitem_Pragma
3052 (Pragma_Argument_Associations => Pargs,
3053 Pragma_Name => Name_Annotate);
3054 end;
3055
89f1e35c 3056 -- Case 3 : Aspects that don't correspond to pragma/attribute
3057 -- definition clause.
7b9b2f05 3058
89f1e35c 3059 -- Case 3a: The aspects listed below don't correspond to
3060 -- pragmas/attributes but do require delayed analysis.
7f694ca2 3061
51fa2a45 3062 -- Default_Value can only apply to a scalar type
3063
3064 when Aspect_Default_Value =>
3065 if not Is_Scalar_Type (E) then
3066 Error_Msg_N
1089ff19 3067 ("aspect Default_Value must apply to a scalar type", N);
51fa2a45 3068 end if;
3069
3070 Aitem := Empty;
3071
3072 -- Default_Component_Value can only apply to an array type
3073 -- with scalar components.
3074
3075 when Aspect_Default_Component_Value =>
3076 if not (Is_Array_Type (E)
3f4c9ffc 3077 and then Is_Scalar_Type (Component_Type (E)))
51fa2a45 3078 then
ee2b7923 3079 Error_Msg_N
3080 ("aspect Default_Component_Value can only apply to an "
3081 & "array of scalar components", N);
51fa2a45 3082 end if;
0fd13d32 3083
89f1e35c 3084 Aitem := Empty;
7f694ca2 3085
89f1e35c 3086 -- Case 3b: The aspects listed below don't correspond to
3087 -- pragmas/attributes and don't need delayed analysis.
95bc75fa 3088
0fd13d32 3089 -- Implicit_Dereference
3090
89f1e35c 3091 -- For Implicit_Dereference, External_Name and Link_Name, only
3092 -- the legality checks are done during the analysis, thus no
3093 -- delay is required.
a8e38e1d 3094
89f1e35c 3095 when Aspect_Implicit_Dereference =>
3096 Analyze_Aspect_Implicit_Dereference;
3097 goto Continue;
7f694ca2 3098
0fd13d32 3099 -- Dimension
3100
89f1e35c 3101 when Aspect_Dimension =>
3102 Analyze_Aspect_Dimension (N, Id, Expr);
3103 goto Continue;
cb4c311d 3104
0fd13d32 3105 -- Dimension_System
3106
89f1e35c 3107 when Aspect_Dimension_System =>
3108 Analyze_Aspect_Dimension_System (N, Id, Expr);
3109 goto Continue;
7f694ca2 3110
ceec4f7c 3111 -- Case 4: Aspects requiring special handling
51ea9c94 3112
e66f4e2a 3113 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3114 -- pragmas take care of the delay.
7f694ca2 3115
0fd13d32 3116 -- Pre/Post
3117
1e3c4ae6 3118 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3119 -- with a first argument that is the expression, and a second
3120 -- argument that is an informative message if the test fails.
3121 -- This is inserted right after the declaration, to get the
5b5df4a9 3122 -- required pragma placement. The processing for the pragmas
3123 -- takes care of the required delay.
ae888dbd 3124
5ddd846b 3125 when Pre_Post_Aspects => Pre_Post : declare
1e3c4ae6 3126 Pname : Name_Id;
ae888dbd 3127
1e3c4ae6 3128 begin
77ae6789 3129 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1e3c4ae6 3130 Pname := Name_Precondition;
3131 else
3132 Pname := Name_Postcondition;
3133 end if;
d74fc39a 3134
26062729 3135 -- Check that the class-wide predicate cannot be applied to
3136 -- an operation of a synchronized type that is not a tagged
3137 -- type. Other legality checks are performed when analyzing
3138 -- the contract of the operation.
3139
3140 if Class_Present (Aspect)
3141 and then Is_Concurrent_Type (Current_Scope)
3142 and then not Is_Tagged_Type (Current_Scope)
3143 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3144 then
3145 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3146 Error_Msg_N
3147 ("aspect % can only be specified for a primitive "
3148 & "operation of a tagged type", Aspect);
3149
3150 goto Continue;
3151 end if;
3152
1e3c4ae6 3153 -- If the expressions is of the form A and then B, then
3154 -- we generate separate Pre/Post aspects for the separate
3155 -- clauses. Since we allow multiple pragmas, there is no
3156 -- problem in allowing multiple Pre/Post aspects internally.
a273015d 3157 -- These should be treated in reverse order (B first and
3158 -- A second) since they are later inserted just after N in
3159 -- the order they are treated. This way, the pragma for A
3160 -- ends up preceding the pragma for B, which may have an
3161 -- importance for the error raised (either constraint error
3162 -- or precondition error).
1e3c4ae6 3163
39e1f22f 3164 -- We do not do this for Pre'Class, since we have to put
51fa2a45 3165 -- these conditions together in a complex OR expression.
ae888dbd 3166
4282d342 3167 -- We do not do this in ASIS mode, as ASIS relies on the
3168 -- original node representing the complete expression, when
3169 -- retrieving it through the source aspect table.
3170
3171 if not ASIS_Mode
3172 and then (Pname = Name_Postcondition
3173 or else not Class_Present (Aspect))
39e1f22f 3174 then
3175 while Nkind (Expr) = N_And_Then loop
3176 Insert_After (Aspect,
a273015d 3177 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
39e1f22f 3178 Identifier => Identifier (Aspect),
a273015d 3179 Expression => Relocate_Node (Left_Opnd (Expr)),
39e1f22f 3180 Class_Present => Class_Present (Aspect),
3181 Split_PPC => True));
a273015d 3182 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
39e1f22f 3183 Eloc := Sloc (Expr);
3184 end loop;
3185 end if;
ae888dbd 3186
48d6f069 3187 -- Build the precondition/postcondition pragma
3188
51fa2a45 3189 -- Add note about why we do NOT need Copy_Tree here???
d74fc39a 3190
0fd13d32 3191 Make_Aitem_Pragma
3192 (Pragma_Argument_Associations => New_List (
3193 Make_Pragma_Argument_Association (Eloc,
3194 Chars => Name_Check,
a19e1763 3195 Expression => Relocate_Node (Expr))),
0fd13d32 3196 Pragma_Name => Pname);
39e1f22f 3197
3198 -- Add message unless exception messages are suppressed
3199
3200 if not Opt.Exception_Locations_Suppressed then
3201 Append_To (Pragma_Argument_Associations (Aitem),
3202 Make_Pragma_Argument_Association (Eloc,
ed695684 3203 Chars => Name_Message,
39e1f22f 3204 Expression =>
3205 Make_String_Literal (Eloc,
3206 Strval => "failed "
3207 & Get_Name_String (Pname)
3208 & " from "
3209 & Build_Location_String (Eloc))));
3210 end if;
d74fc39a 3211
7d20685d 3212 Set_Is_Delayed_Aspect (Aspect);
d74fc39a 3213
1e3c4ae6 3214 -- For Pre/Post cases, insert immediately after the entity
3215 -- declaration, since that is the required pragma placement.
3216 -- Note that for these aspects, we do not have to worry
3217 -- about delay issues, since the pragmas themselves deal
3218 -- with delay of visibility for the expression analysis.
3219
e2bf777d 3220 Insert_Pragma (Aitem);
299b347e 3221
1e3c4ae6 3222 goto Continue;
5ddd846b 3223 end Pre_Post;
ae888dbd 3224
0fd13d32 3225 -- Test_Case
3226
e66f4e2a 3227 when Aspect_Test_Case => Test_Case : declare
3228 Args : List_Id;
3229 Comp_Expr : Node_Id;
3230 Comp_Assn : Node_Id;
3231 New_Expr : Node_Id;
57cd943b 3232
e66f4e2a 3233 begin
3234 Args := New_List;
b0bc40fd 3235
e66f4e2a 3236 if Nkind (Parent (N)) = N_Compilation_Unit then
3237 Error_Msg_Name_1 := Nam;
3238 Error_Msg_N ("incorrect placement of aspect `%`", E);
3239 goto Continue;
3240 end if;
6c545057 3241
e66f4e2a 3242 if Nkind (Expr) /= N_Aggregate then
3243 Error_Msg_Name_1 := Nam;
3244 Error_Msg_NE
3245 ("wrong syntax for aspect `%` for &", Id, E);
3246 goto Continue;
3247 end if;
6c545057 3248
e66f4e2a 3249 -- Make pragma expressions refer to the original aspect
51fa2a45 3250 -- expressions through the Original_Node link. This is used
3251 -- in semantic analysis for ASIS mode, so that the original
3252 -- expression also gets analyzed.
e66f4e2a 3253
3254 Comp_Expr := First (Expressions (Expr));
3255 while Present (Comp_Expr) loop
3256 New_Expr := Relocate_Node (Comp_Expr);
e66f4e2a 3257 Append_To (Args,
3258 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3259 Expression => New_Expr));
3260 Next (Comp_Expr);
3261 end loop;
3262
3263 Comp_Assn := First (Component_Associations (Expr));
3264 while Present (Comp_Assn) loop
3265 if List_Length (Choices (Comp_Assn)) /= 1
3266 or else
3267 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3268 then
fad014fe 3269 Error_Msg_Name_1 := Nam;
6c545057 3270 Error_Msg_NE
fad014fe 3271 ("wrong syntax for aspect `%` for &", Id, E);
6c545057 3272 goto Continue;
3273 end if;
3274
e66f4e2a 3275 Append_To (Args,
3276 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
ed695684 3277 Chars => Chars (First (Choices (Comp_Assn))),
3278 Expression =>
3279 Relocate_Node (Expression (Comp_Assn))));
e66f4e2a 3280 Next (Comp_Assn);
3281 end loop;
6c545057 3282
e66f4e2a 3283 -- Build the test-case pragma
6c545057 3284
0fd13d32 3285 Make_Aitem_Pragma
3286 (Pragma_Argument_Associations => Args,
3287 Pragma_Name => Nam);
e66f4e2a 3288 end Test_Case;
85696508 3289
0fd13d32 3290 -- Contract_Cases
3291
5ddd846b 3292 when Aspect_Contract_Cases =>
0fd13d32 3293 Make_Aitem_Pragma
3294 (Pragma_Argument_Associations => New_List (
3295 Make_Pragma_Argument_Association (Loc,
3296 Expression => Relocate_Node (Expr))),
3297 Pragma_Name => Nam);
3a128918 3298
e2bf777d 3299 Decorate (Aspect, Aitem);
3300 Insert_Pragma (Aitem);
5ddd846b 3301 goto Continue;
3a128918 3302
89f1e35c 3303 -- Case 5: Special handling for aspects with an optional
3304 -- boolean argument.
85696508 3305
6c5793cd 3306 -- In the delayed case, the corresponding pragma cannot be
0fd13d32 3307 -- generated yet because the evaluation of the boolean needs
3308 -- to be delayed till the freeze point.
3309
89f1e35c 3310 when Boolean_Aspects |
3311 Library_Unit_Aspects =>
a5a64273 3312
89f1e35c 3313 Set_Is_Boolean_Aspect (Aspect);
a5a64273 3314
89f1e35c 3315 -- Lock_Free aspect only apply to protected objects
e1cedbae 3316
89f1e35c 3317 if A_Id = Aspect_Lock_Free then
3318 if Ekind (E) /= E_Protected_Type then
99a2d5bd 3319 Error_Msg_Name_1 := Nam;
a5a64273 3320 Error_Msg_N
89f1e35c 3321 ("aspect % only applies to a protected object",
3322 Aspect);
3323
3324 else
3325 -- Set the Uses_Lock_Free flag to True if there is no
37c6e44c 3326 -- expression or if the expression is True. The
89f1e35c 3327 -- evaluation of this aspect should be delayed to the
37c6e44c 3328 -- freeze point (why???)
89f1e35c 3329
e81df51c 3330 if No (Expr)
3331 or else Is_True (Static_Boolean (Expr))
89f1e35c 3332 then
3333 Set_Uses_Lock_Free (E);
3334 end if;
caf125ce 3335
3336 Record_Rep_Item (E, Aspect);
a5a64273 3337 end if;
e1cedbae 3338
89f1e35c 3339 goto Continue;
ae888dbd 3340
ee2b7923 3341 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3342 Analyze_Aspect_Export_Import;
6c5793cd 3343
3344 -- Disable_Controlled
3345
3346 elsif A_Id = Aspect_Disable_Controlled then
3347 if Ekind (E) /= E_Record_Type
3348 or else not Is_Controlled (E)
3349 then
3350 Error_Msg_N
3351 ("aspect % requires controlled record type", Aspect);
3352 goto Continue;
3353 end if;
3354
3f716509 3355 -- If we're in a generic template, we don't want to try
3356 -- to disable controlled types, because typical usage is
3357 -- "Disable_Controlled => not <some_check>'Enabled", and
3358 -- the value of Enabled is not known until we see a
7e2d3667 3359 -- particular instance. In such a context, we just need
3360 -- to preanalyze the expression for legality.
3f716509 3361
3362 if Expander_Active then
aae9bc79 3363 Analyze_And_Resolve (Expr, Standard_Boolean);
3364
3f716509 3365 if not Present (Expr)
3366 or else Is_True (Static_Boolean (Expr))
3367 then
3368 Set_Disable_Controlled (E);
3369 end if;
7e2d3667 3370
3371 elsif Serious_Errors_Detected = 0 then
3372 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6c5793cd 3373 end if;
3374
89f1e35c 3375 goto Continue;
3376 end if;
d74fc39a 3377
37c6e44c 3378 -- Library unit aspects require special handling in the case
3379 -- of a package declaration, the pragma needs to be inserted
3380 -- in the list of declarations for the associated package.
3381 -- There is no issue of visibility delay for these aspects.
d64221a7 3382
89f1e35c 3383 if A_Id in Library_Unit_Aspects
178fec9b 3384 and then
3385 Nkind_In (N, N_Package_Declaration,
3386 N_Generic_Package_Declaration)
89f1e35c 3387 and then Nkind (Parent (N)) /= N_Compilation_Unit
3ad60f63 3388
3389 -- Aspect is legal on a local instantiation of a library-
3390 -- level generic unit.
3391
b94a633e 3392 and then not Is_Generic_Instance (Defining_Entity (N))
89f1e35c 3393 then
3394 Error_Msg_N
dd4c44af 3395 ("incorrect context for library unit aspect&", Id);
89f1e35c 3396 goto Continue;
3397 end if;
cce84b09 3398
51fa2a45 3399 -- Cases where we do not delay, includes all cases where the
3400 -- expression is missing other than the above cases.
d74fc39a 3401
85ee12c0 3402 if not Delay_Required or else No (Expr) then
ee2b7923 3403
3404 -- Exclude aspects Export and Import because their pragma
3405 -- syntax does not map directly to a Boolean aspect.
3406
3407 if A_Id /= Aspect_Export
3408 and then A_Id /= Aspect_Import
3409 then
3410 Make_Aitem_Pragma
3411 (Pragma_Argument_Associations => New_List (
3412 Make_Pragma_Argument_Association (Sloc (Ent),
3413 Expression => Ent)),
3414 Pragma_Name => Chars (Id));
3415 end if;
3416
89f1e35c 3417 Delay_Required := False;
ddf1337b 3418
89f1e35c 3419 -- In general cases, the corresponding pragma/attribute
3420 -- definition clause will be inserted later at the freezing
294709fa 3421 -- point, and we do not need to build it now.
ddf1337b 3422
89f1e35c 3423 else
3424 Aitem := Empty;
3425 end if;
ceec4f7c 3426
3427 -- Storage_Size
3428
3429 -- This is special because for access types we need to generate
3430 -- an attribute definition clause. This also works for single
3431 -- task declarations, but it does not work for task type
3432 -- declarations, because we have the case where the expression
3433 -- references a discriminant of the task type. That can't use
3434 -- an attribute definition clause because we would not have
3435 -- visibility on the discriminant. For that case we must
3436 -- generate a pragma in the task definition.
3437
3438 when Aspect_Storage_Size =>
3439
3440 -- Task type case
3441
3442 if Ekind (E) = E_Task_Type then
3443 declare
3444 Decl : constant Node_Id := Declaration_Node (E);
3445
3446 begin
3447 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3448
3449 -- If no task definition, create one
3450
3451 if No (Task_Definition (Decl)) then
3452 Set_Task_Definition (Decl,
3453 Make_Task_Definition (Loc,
3454 Visible_Declarations => Empty_List,
3455 End_Label => Empty));
3456 end if;
3457
51fa2a45 3458 -- Create a pragma and put it at the start of the task
3459 -- definition for the task type declaration.
ceec4f7c 3460
3461 Make_Aitem_Pragma
3462 (Pragma_Argument_Associations => New_List (
3463 Make_Pragma_Argument_Association (Loc,
3464 Expression => Relocate_Node (Expr))),
3465 Pragma_Name => Name_Storage_Size);
3466
3467 Prepend
3468 (Aitem,
3469 Visible_Declarations (Task_Definition (Decl)));
3470 goto Continue;
3471 end;
3472
3473 -- All other cases, generate attribute definition
3474
3475 else
3476 Aitem :=
3477 Make_Attribute_Definition_Clause (Loc,
3478 Name => Ent,
3479 Chars => Chars (Id),
3480 Expression => Relocate_Node (Expr));
3481 end if;
89f1e35c 3482 end case;
ddf1337b 3483
89f1e35c 3484 -- Attach the corresponding pragma/attribute definition clause to
3485 -- the aspect specification node.
d74fc39a 3486
89f1e35c 3487 if Present (Aitem) then
e2bf777d 3488 Set_From_Aspect_Specification (Aitem);
89f1e35c 3489 end if;
53c179ea 3490
89f1e35c 3491 -- In the context of a compilation unit, we directly put the
0fd13d32 3492 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3493 -- node (no delay is required here) except for aspects on a
51fa2a45 3494 -- subprogram body (see below) and a generic package, for which we
3495 -- need to introduce the pragma before building the generic copy
3496 -- (see sem_ch12), and for package instantiations, where the
3497 -- library unit pragmas are better handled early.
ddf1337b 3498
9129c28f 3499 if Nkind (Parent (N)) = N_Compilation_Unit
89f1e35c 3500 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3501 then
3502 declare
3503 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
7f694ca2 3504
89f1e35c 3505 begin
3506 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
7f694ca2 3507
89f1e35c 3508 -- For a Boolean aspect, create the corresponding pragma if
3509 -- no expression or if the value is True.
7f694ca2 3510
b9e61b2a 3511 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
89f1e35c 3512 if Is_True (Static_Boolean (Expr)) then
0fd13d32 3513 Make_Aitem_Pragma
3514 (Pragma_Argument_Associations => New_List (
3515 Make_Pragma_Argument_Association (Sloc (Ent),
3516 Expression => Ent)),
3517 Pragma_Name => Chars (Id));
7f694ca2 3518
89f1e35c 3519 Set_From_Aspect_Specification (Aitem, True);
3520 Set_Corresponding_Aspect (Aitem, Aspect);
3521
3522 else
3523 goto Continue;
3524 end if;
3525 end if;
7f694ca2 3526
d6814978 3527 -- If the aspect is on a subprogram body (relevant aspect
3528 -- is Inline), add the pragma in front of the declarations.
3a72f9c3 3529
3530 if Nkind (N) = N_Subprogram_Body then
3531 if No (Declarations (N)) then
3532 Set_Declarations (N, New_List);
3533 end if;
3534
3535 Prepend (Aitem, Declarations (N));
3536
178fec9b 3537 elsif Nkind (N) = N_Generic_Package_Declaration then
3538 if No (Visible_Declarations (Specification (N))) then
3539 Set_Visible_Declarations (Specification (N), New_List);
3540 end if;
3541
3542 Prepend (Aitem,
3543 Visible_Declarations (Specification (N)));
3544
c39cce40 3545 elsif Nkind (N) = N_Package_Instantiation then
df8b0dae 3546 declare
3547 Spec : constant Node_Id :=
3548 Specification (Instance_Spec (N));
3549 begin
3550 if No (Visible_Declarations (Spec)) then
3551 Set_Visible_Declarations (Spec, New_List);
3552 end if;
3553
3554 Prepend (Aitem, Visible_Declarations (Spec));
3555 end;
3556
3a72f9c3 3557 else
3558 if No (Pragmas_After (Aux)) then
d4596fbe 3559 Set_Pragmas_After (Aux, New_List);
3a72f9c3 3560 end if;
3561
3562 Append (Aitem, Pragmas_After (Aux));
89f1e35c 3563 end if;
7f694ca2 3564
89f1e35c 3565 goto Continue;
3566 end;
3567 end if;
7f694ca2 3568
89f1e35c 3569 -- The evaluation of the aspect is delayed to the freezing point.
3570 -- The pragma or attribute clause if there is one is then attached
37c6e44c 3571 -- to the aspect specification which is put in the rep item list.
1a814552 3572
89f1e35c 3573 if Delay_Required then
3574 if Present (Aitem) then
3575 Set_Is_Delayed_Aspect (Aitem);
3576 Set_Aspect_Rep_Item (Aspect, Aitem);
3577 Set_Parent (Aitem, Aspect);
3578 end if;
1a814552 3579
89f1e35c 3580 Set_Is_Delayed_Aspect (Aspect);
9f36e3fb 3581
cba2ae82 3582 -- In the case of Default_Value, link the aspect to base type
3583 -- as well, even though it appears on a first subtype. This is
3584 -- mandated by the semantics of the aspect. Do not establish
3585 -- the link when processing the base type itself as this leads
3586 -- to a rep item circularity. Verify that we are dealing with
3587 -- a scalar type to prevent cascaded errors.
3588
3589 if A_Id = Aspect_Default_Value
3590 and then Is_Scalar_Type (E)
3591 and then Base_Type (E) /= E
3592 then
9f36e3fb 3593 Set_Has_Delayed_Aspects (Base_Type (E));
3594 Record_Rep_Item (Base_Type (E), Aspect);
3595 end if;
3596
89f1e35c 3597 Set_Has_Delayed_Aspects (E);
3598 Record_Rep_Item (E, Aspect);
ddf1337b 3599
b855559d 3600 -- When delay is not required and the context is a package or a
3601 -- subprogram body, insert the pragma in the body declarations.
f55ce169 3602
b855559d 3603 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
f55ce169 3604 if No (Declarations (N)) then
3605 Set_Declarations (N, New_List);
3606 end if;
3607
3608 -- The pragma is added before source declarations
3609
3610 Prepend_To (Declarations (N), Aitem);
3611
89f1e35c 3612 -- When delay is not required and the context is not a compilation
3613 -- unit, we simply insert the pragma/attribute definition clause
3614 -- in sequence.
ddf1337b 3615
ee2b7923 3616 elsif Present (Aitem) then
89f1e35c 3617 Insert_After (Ins_Node, Aitem);
3618 Ins_Node := Aitem;
d74fc39a 3619 end if;
0fd13d32 3620 end Analyze_One_Aspect;
ae888dbd 3621
d64221a7 3622 <<Continue>>
3623 Next (Aspect);
21ea3a4f 3624 end loop Aspect_Loop;
89f1e35c 3625
3626 if Has_Delayed_Aspects (E) then
3627 Ensure_Freeze_Node (E);
3628 end if;
21ea3a4f 3629 end Analyze_Aspect_Specifications;
ae888dbd 3630
eb8aeefc 3631 ---------------------------------------------------
3632 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3633 ---------------------------------------------------
3634
3635 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3636 Body_Id : constant Entity_Id := Defining_Entity (N);
3637
3638 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
c02dccca 3639 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3640 -- error message depending on the aspects involved. Spec_Id denotes the
3641 -- entity of the corresponding spec.
eb8aeefc 3642
3643 --------------------------------
3644 -- Diagnose_Misplaced_Aspects --
3645 --------------------------------
3646
3647 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3648 procedure Misplaced_Aspect_Error
3649 (Asp : Node_Id;
3650 Ref_Nam : Name_Id);
3651 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3652 -- the name of the refined version of the aspect.
3653
3654 ----------------------------
3655 -- Misplaced_Aspect_Error --
3656 ----------------------------
3657
3658 procedure Misplaced_Aspect_Error
3659 (Asp : Node_Id;
3660 Ref_Nam : Name_Id)
3661 is
3662 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3663 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3664
3665 begin
3666 -- The corresponding spec already contains the aspect in question
3667 -- and the one appearing on the body must be the refined form:
3668
3669 -- procedure P with Global ...;
3670 -- procedure P with Global ... is ... end P;
3671 -- ^
3672 -- Refined_Global
3673
3674 if Has_Aspect (Spec_Id, Asp_Id) then
3675 Error_Msg_Name_1 := Asp_Nam;
3676
3677 -- Subunits cannot carry aspects that apply to a subprogram
3678 -- declaration.
3679
3680 if Nkind (Parent (N)) = N_Subunit then
3681 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3682
3683 -- Otherwise suggest the refined form
3684
3685 else
3686 Error_Msg_Name_2 := Ref_Nam;
3687 Error_Msg_N ("aspect % should be %", Asp);
3688 end if;
3689
3690 -- Otherwise the aspect must appear on the spec, not on the body
3691
3692 -- procedure P;
3693 -- procedure P with Global ... is ... end P;
3694
3695 else
3696 Error_Msg_N
c02dccca 3697 ("aspect specification must appear on initial declaration",
eb8aeefc 3698 Asp);
3699 end if;
3700 end Misplaced_Aspect_Error;
3701
3702 -- Local variables
3703
3704 Asp : Node_Id;
3705 Asp_Nam : Name_Id;
3706
3707 -- Start of processing for Diagnose_Misplaced_Aspects
3708
3709 begin
3710 -- Iterate over the aspect specifications and emit specific errors
3711 -- where applicable.
3712
3713 Asp := First (Aspect_Specifications (N));
3714 while Present (Asp) loop
3715 Asp_Nam := Chars (Identifier (Asp));
3716
3717 -- Do not emit errors on aspects that can appear on a subprogram
3718 -- body. This scenario occurs when the aspect specification list
3719 -- contains both misplaced and properly placed aspects.
3720
3721 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3722 null;
3723
3724 -- Special diagnostics for SPARK aspects
3725
3726 elsif Asp_Nam = Name_Depends then
3727 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3728
3729 elsif Asp_Nam = Name_Global then
3730 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3731
3732 elsif Asp_Nam = Name_Post then
3733 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3734
3735 -- Otherwise a language-defined aspect is misplaced
3736
3737 else
3738 Error_Msg_N
c02dccca 3739 ("aspect specification must appear on initial declaration",
eb8aeefc 3740 Asp);
3741 end if;
3742
3743 Next (Asp);
3744 end loop;
3745 end Diagnose_Misplaced_Aspects;
3746
3747 -- Local variables
3748
c02dccca 3749 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
eb8aeefc 3750
3751 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3752
3753 begin
eb8aeefc 3754 -- Language-defined aspects cannot be associated with a subprogram body
3755 -- [stub] if the subprogram has a spec. Certain implementation defined
3756 -- aspects are allowed to break this rule (for all applicable cases, see
3757 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3758
c02dccca 3759 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
eb8aeefc 3760 Diagnose_Misplaced_Aspects (Spec_Id);
3761 else
3762 Analyze_Aspect_Specifications (N, Body_Id);
3763 end if;
3764 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3765
d6f39728 3766 -----------------------
3767 -- Analyze_At_Clause --
3768 -----------------------
3769
3770 -- An at clause is replaced by the corresponding Address attribute
3771 -- definition clause that is the preferred approach in Ada 95.
3772
3773 procedure Analyze_At_Clause (N : Node_Id) is
177675a7 3774 CS : constant Boolean := Comes_From_Source (N);
3775
d6f39728 3776 begin
177675a7 3777 -- This is an obsolescent feature
3778
e0521a36 3779 Check_Restriction (No_Obsolescent_Features, N);
3780
9dfe12ae 3781 if Warn_On_Obsolescent_Feature then
3782 Error_Msg_N
b174444e 3783 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
9dfe12ae 3784 Error_Msg_N
b174444e 3785 ("\?j?use address attribute definition clause instead", N);
9dfe12ae 3786 end if;
3787
177675a7 3788 -- Rewrite as address clause
3789
d6f39728 3790 Rewrite (N,
3791 Make_Attribute_Definition_Clause (Sloc (N),
935e86e0 3792 Name => Identifier (N),
3793 Chars => Name_Address,
d6f39728 3794 Expression => Expression (N)));
177675a7 3795
2beb22b1 3796 -- We preserve Comes_From_Source, since logically the clause still comes
3797 -- from the source program even though it is changed in form.
177675a7 3798
3799 Set_Comes_From_Source (N, CS);
3800
3801 -- Analyze rewritten clause
3802
d6f39728 3803 Analyze_Attribute_Definition_Clause (N);
3804 end Analyze_At_Clause;
3805
3806 -----------------------------------------
3807 -- Analyze_Attribute_Definition_Clause --
3808 -----------------------------------------
3809
3810 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3811 Loc : constant Source_Ptr := Sloc (N);
3812 Nam : constant Node_Id := Name (N);
3813 Attr : constant Name_Id := Chars (N);
3814 Expr : constant Node_Id := Expression (N);
3815 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
d64221a7 3816
3817 Ent : Entity_Id;
3818 -- The entity of Nam after it is analyzed. In the case of an incomplete
3819 -- type, this is the underlying type.
3820
d6f39728 3821 U_Ent : Entity_Id;
d64221a7 3822 -- The underlying entity to which the attribute applies. Generally this
3823 -- is the Underlying_Type of Ent, except in the case where the clause
3824 -- applies to full view of incomplete type or private type in which case
3825 -- U_Ent is just a copy of Ent.
d6f39728 3826
3827 FOnly : Boolean := False;
3828 -- Reset to True for subtype specific attribute (Alignment, Size)
51fa2a45 3829 -- and for stream attributes, i.e. those cases where in the call to
3830 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3831 -- are checked. Note that the case of stream attributes is not clear
3832 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3833 -- Storage_Size for derived task types, but that is also clearly
3834 -- unintentional.
d6f39728 3835
9f373bb8 3836 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3837 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3838 -- definition clauses.
3839
ae888dbd 3840 function Duplicate_Clause return Boolean;
3841 -- This routine checks if the aspect for U_Ent being given by attribute
3842 -- definition clause N is for an aspect that has already been specified,
3843 -- and if so gives an error message. If there is a duplicate, True is
3844 -- returned, otherwise if there is no error, False is returned.
3845
81b424ac 3846 procedure Check_Indexing_Functions;
3847 -- Check that the function in Constant_Indexing or Variable_Indexing
3848 -- attribute has the proper type structure. If the name is overloaded,
cac18f71 3849 -- check that some interpretation is legal.
81b424ac 3850
89cc7147 3851 procedure Check_Iterator_Functions;
3852 -- Check that there is a single function in Default_Iterator attribute
8df4f2a5 3853 -- has the proper type structure.
89cc7147 3854
3855 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
d03bfaa1 3856 -- Common legality check for the previous two
89cc7147 3857
177675a7 3858 -----------------------------------
3859 -- Analyze_Stream_TSS_Definition --
3860 -----------------------------------
3861
9f373bb8 3862 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3863 Subp : Entity_Id := Empty;
3864 I : Interp_Index;
3865 It : Interp;
3866 Pnam : Entity_Id;
3867
3868 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
ba662f09 3869 -- True for Read attribute, False for other attributes
9f373bb8 3870
c41e404d 3871 function Has_Good_Profile
3872 (Subp : Entity_Id;
3873 Report : Boolean := False) return Boolean;
9f373bb8 3874 -- Return true if the entity is a subprogram with an appropriate
ba662f09 3875 -- profile for the attribute being defined. If result is False and
3876 -- Report is True, function emits appropriate error.
9f373bb8 3877
3878 ----------------------
3879 -- Has_Good_Profile --
3880 ----------------------
3881
c41e404d 3882 function Has_Good_Profile
3883 (Subp : Entity_Id;
3884 Report : Boolean := False) return Boolean
3885 is
9f373bb8 3886 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3887 (False => E_Procedure, True => E_Function);
4a83cc35 3888 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3889 F : Entity_Id;
9f373bb8 3890 Typ : Entity_Id;
3891
3892 begin
3893 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3894 return False;
3895 end if;
3896
3897 F := First_Formal (Subp);
3898
3899 if No (F)
3900 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3901 or else Designated_Type (Etype (F)) /=
4a83cc35 3902 Class_Wide_Type (RTE (RE_Root_Stream_Type))
9f373bb8 3903 then
3904 return False;
3905 end if;
3906
3907 if not Is_Function then
3908 Next_Formal (F);
3909
3910 declare
3911 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3912 (False => E_In_Parameter,
3913 True => E_Out_Parameter);
3914 begin
3915 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3916 return False;
3917 end if;
3918 end;
3919
3920 Typ := Etype (F);
3921
b64082f2 3922 -- If the attribute specification comes from an aspect
51fa2a45 3923 -- specification for a class-wide stream, the parameter must be
3924 -- a class-wide type of the entity to which the aspect applies.
b64082f2 3925
3926 if From_Aspect_Specification (N)
3927 and then Class_Present (Parent (N))
3928 and then Is_Class_Wide_Type (Typ)
3929 then
3930 Typ := Etype (Typ);
3931 end if;
3932
9f373bb8 3933 else
3934 Typ := Etype (Subp);
3935 end if;
3936
51fa2a45 3937 -- Verify that the prefix of the attribute and the local name for
5a8fe506 3938 -- the type of the formal match, or one is the class-wide of the
3939 -- other, in the case of a class-wide stream operation.
48680a09 3940
b8eacb12 3941 if Base_Type (Typ) = Base_Type (Ent)
5a8fe506 3942 or else (Is_Class_Wide_Type (Typ)
2be1f7d7 3943 and then Typ = Class_Wide_Type (Base_Type (Ent)))
fbf4d6ef 3944 or else (Is_Class_Wide_Type (Ent)
3945 and then Ent = Class_Wide_Type (Base_Type (Typ)))
5a8fe506 3946 then
3947 null;
3948 else
3949 return False;
3950 end if;
3951
4a83cc35 3952 if Present (Next_Formal (F)) then
48680a09 3953 return False;
3954
3955 elsif not Is_Scalar_Type (Typ)
3956 and then not Is_First_Subtype (Typ)
3957 and then not Is_Class_Wide_Type (Typ)
3958 then
c41e404d 3959 if Report and not Is_First_Subtype (Typ) then
3960 Error_Msg_N
ba662f09 3961 ("subtype of formal in stream operation must be a first "
3962 & "subtype", Parameter_Type (Parent (F)));
c41e404d 3963 end if;
3964
48680a09 3965 return False;
3966
3967 else
3968 return True;
3969 end if;
9f373bb8 3970 end Has_Good_Profile;
3971
3972 -- Start of processing for Analyze_Stream_TSS_Definition
3973
3974 begin
3975 FOnly := True;
3976
3977 if not Is_Type (U_Ent) then
3978 Error_Msg_N ("local name must be a subtype", Nam);
3979 return;
48680a09 3980
3981 elsif not Is_First_Subtype (U_Ent) then
3982 Error_Msg_N ("local name must be a first subtype", Nam);
3983 return;
9f373bb8 3984 end if;
3985
3986 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3987
44e4341e 3988 -- If Pnam is present, it can be either inherited from an ancestor
3989 -- type (in which case it is legal to redefine it for this type), or
3990 -- be a previous definition of the attribute for the same type (in
3991 -- which case it is illegal).
3992
3993 -- In the first case, it will have been analyzed already, and we
3994 -- can check that its profile does not match the expected profile
3995 -- for a stream attribute of U_Ent. In the second case, either Pnam
3996 -- has been analyzed (and has the expected profile), or it has not
3997 -- been analyzed yet (case of a type that has not been frozen yet
3998 -- and for which the stream attribute has been set using Set_TSS).
3999
4000 if Present (Pnam)
4001 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
4002 then
9f373bb8 4003 Error_Msg_Sloc := Sloc (Pnam);
4004 Error_Msg_Name_1 := Attr;
4005 Error_Msg_N ("% attribute already defined #", Nam);
4006 return;
4007 end if;
4008
4009 Analyze (Expr);
4010
4011 if Is_Entity_Name (Expr) then
4012 if not Is_Overloaded (Expr) then
c41e404d 4013 if Has_Good_Profile (Entity (Expr), Report => True) then
9f373bb8 4014 Subp := Entity (Expr);
4015 end if;
4016
4017 else
4018 Get_First_Interp (Expr, I, It);
9f373bb8 4019 while Present (It.Nam) loop
4020 if Has_Good_Profile (It.Nam) then
4021 Subp := It.Nam;
4022 exit;
4023 end if;
4024
4025 Get_Next_Interp (I, It);
4026 end loop;
4027 end if;
4028 end if;
4029
4030 if Present (Subp) then
59ac57b5 4031 if Is_Abstract_Subprogram (Subp) then
9f373bb8 4032 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4033 return;
e12b2502 4034
299b347e 4035 -- A stream subprogram for an interface type must be a null
bfbd9cf4 4036 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4037 -- of an interface is not an interface type (3.9.4 (6.b/2)).
e12b2502 4038
4039 elsif Is_Interface (U_Ent)
5a8fe506 4040 and then not Is_Class_Wide_Type (U_Ent)
e12b2502 4041 and then not Inside_A_Generic
e12b2502 4042 and then
5a8fe506 4043 (Ekind (Subp) = E_Function
4044 or else
4045 not Null_Present
2be1f7d7 4046 (Specification
4047 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
e12b2502 4048 then
4049 Error_Msg_N
4a83cc35 4050 ("stream subprogram for interface type must be null "
4051 & "procedure", Expr);
9f373bb8 4052 end if;
4053
4054 Set_Entity (Expr, Subp);
4055 Set_Etype (Expr, Etype (Subp));
4056
44e4341e 4057 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
9f373bb8 4058
4059 else
4060 Error_Msg_Name_1 := Attr;
4061 Error_Msg_N ("incorrect expression for% attribute", Expr);
4062 end if;
4063 end Analyze_Stream_TSS_Definition;
4064
81b424ac 4065 ------------------------------
4066 -- Check_Indexing_Functions --
4067 ------------------------------
4068
4069 procedure Check_Indexing_Functions is
c8a2d809 4070 Indexing_Found : Boolean := False;
8df4f2a5 4071
44d567c8 4072 procedure Check_Inherited_Indexing;
4073 -- For a derived type, check that no indexing aspect is specified
4074 -- for the type if it is also inherited
4075
81b424ac 4076 procedure Check_One_Function (Subp : Entity_Id);
7796365f 4077 -- Check one possible interpretation. Sets Indexing_Found True if a
4078 -- legal indexing function is found.
81b424ac 4079
05987af3 4080 procedure Illegal_Indexing (Msg : String);
4081 -- Diagnose illegal indexing function if not overloaded. In the
4082 -- overloaded case indicate that no legal interpretation exists.
4083
44d567c8 4084 ------------------------------
4085 -- Check_Inherited_Indexing --
4086 ------------------------------
4087
4088 procedure Check_Inherited_Indexing is
4089 Inherited : Node_Id;
4090
4091 begin
4092 if Attr = Name_Constant_Indexing then
4093 Inherited :=
4094 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4095 else pragma Assert (Attr = Name_Variable_Indexing);
4096 Inherited :=
4097 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4098 end if;
4099
4100 if Present (Inherited) then
4101 if Debug_Flag_Dot_XX then
4102 null;
4103
83d39cd3 4104 -- OK if current attribute_definition_clause is expansion of
4105 -- inherited aspect.
44d567c8 4106
4107 elsif Aspect_Rep_Item (Inherited) = N then
4108 null;
4109
83d39cd3 4110 -- Indicate the operation that must be overridden, rather than
4111 -- redefining the indexing aspect.
44d567c8 4112
4113 else
4114 Illegal_Indexing
f2837ceb 4115 ("indexing function already inherited from parent type");
44d567c8 4116 Error_Msg_NE
4117 ("!override & instead",
4118 N, Entity (Expression (Inherited)));
4119 end if;
4120 end if;
4121 end Check_Inherited_Indexing;
4122
81b424ac 4123 ------------------------
4124 -- Check_One_Function --
4125 ------------------------
4126
4127 procedure Check_One_Function (Subp : Entity_Id) is
05987af3 4128 Default_Element : Node_Id;
4129 Ret_Type : constant Entity_Id := Etype (Subp);
1b7510f9 4130
81b424ac 4131 begin
05987af3 4132 if not Is_Overloadable (Subp) then
4133 Illegal_Indexing ("illegal indexing function for type&");
4134 return;
4135
7796365f 4136 elsif Scope (Subp) /= Scope (Ent) then
4137 if Nkind (Expr) = N_Expanded_Name then
4138
4139 -- Indexing function can't be declared elsewhere
4140
4141 Illegal_Indexing
4142 ("indexing function must be declared in scope of type&");
4143 end if;
4144
05987af3 4145 return;
4146
4147 elsif No (First_Formal (Subp)) then
4148 Illegal_Indexing
4149 ("Indexing requires a function that applies to type&");
4150 return;
4151
4152 elsif No (Next_Formal (First_Formal (Subp))) then
4153 Illegal_Indexing
2eb0ff42 4154 ("indexing function must have at least two parameters");
05987af3 4155 return;
4156
4157 elsif Is_Derived_Type (Ent) then
44d567c8 4158 Check_Inherited_Indexing;
05987af3 4159 end if;
4160
e81df51c 4161 if not Check_Primitive_Function (Subp) then
05987af3 4162 Illegal_Indexing
4163 ("Indexing aspect requires a function that applies to type&");
4164 return;
81b424ac 4165 end if;
4166
7796365f 4167 -- If partial declaration exists, verify that it is not tagged.
4168
4169 if Ekind (Current_Scope) = E_Package
4170 and then Has_Private_Declaration (Ent)
4171 and then From_Aspect_Specification (N)
7c0c95b8 4172 and then
4173 List_Containing (Parent (Ent)) =
4174 Private_Declarations
7796365f 4175 (Specification (Unit_Declaration_Node (Current_Scope)))
4176 and then Nkind (N) = N_Attribute_Definition_Clause
4177 then
4178 declare
4179 Decl : Node_Id;
4180
4181 begin
4182 Decl :=
4183 First (Visible_Declarations
7c0c95b8 4184 (Specification
4185 (Unit_Declaration_Node (Current_Scope))));
7796365f 4186
4187 while Present (Decl) loop
4188 if Nkind (Decl) = N_Private_Type_Declaration
4189 and then Ent = Full_View (Defining_Identifier (Decl))
4190 and then Tagged_Present (Decl)
4191 and then No (Aspect_Specifications (Decl))
4192 then
4193 Illegal_Indexing
4194 ("Indexing aspect cannot be specified on full view "
7c0c95b8 4195 & "if partial view is tagged");
7796365f 4196 return;
4197 end if;
4198
4199 Next (Decl);
4200 end loop;
4201 end;
4202 end if;
4203
1b7510f9 4204 -- An indexing function must return either the default element of
cac18f71 4205 -- the container, or a reference type. For variable indexing it
a45d946f 4206 -- must be the latter.
1b7510f9 4207
05987af3 4208 Default_Element :=
4209 Find_Value_Of_Aspect
4210 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4211
1b7510f9 4212 if Present (Default_Element) then
4213 Analyze (Default_Element);
a45d946f 4214
1b7510f9 4215 if Is_Entity_Name (Default_Element)
05987af3 4216 and then not Covers (Entity (Default_Element), Ret_Type)
4217 and then False
1b7510f9 4218 then
05987af3 4219 Illegal_Indexing
4220 ("wrong return type for indexing function");
1b7510f9 4221 return;
4222 end if;
4223 end if;
4224
a45d946f 4225 -- For variable_indexing the return type must be a reference type
1b7510f9 4226
05987af3 4227 if Attr = Name_Variable_Indexing then
4228 if not Has_Implicit_Dereference (Ret_Type) then
4229 Illegal_Indexing
4230 ("variable indexing must return a reference type");
4231 return;
4232
423b89fd 4233 elsif Is_Access_Constant
4234 (Etype (First_Discriminant (Ret_Type)))
05987af3 4235 then
4236 Illegal_Indexing
4237 ("variable indexing must return an access to variable");
4238 return;
4239 end if;
cac18f71 4240
4241 else
05987af3 4242 if Has_Implicit_Dereference (Ret_Type)
4243 and then not
4244 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4245 then
4246 Illegal_Indexing
4247 ("constant indexing must return an access to constant");
4248 return;
4249
4250 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4251 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4252 then
4253 Illegal_Indexing
4254 ("constant indexing must apply to an access to constant");
4255 return;
4256 end if;
81b424ac 4257 end if;
05987af3 4258
4259 -- All checks succeeded.
4260
4261 Indexing_Found := True;
81b424ac 4262 end Check_One_Function;
4263
05987af3 4264 -----------------------
4265 -- Illegal_Indexing --
4266 -----------------------
4267
4268 procedure Illegal_Indexing (Msg : String) is
4269 begin
7796365f 4270 Error_Msg_NE (Msg, N, Ent);
05987af3 4271 end Illegal_Indexing;
4272
81b424ac 4273 -- Start of processing for Check_Indexing_Functions
4274
4275 begin
89cc7147 4276 if In_Instance then
44d567c8 4277 Check_Inherited_Indexing;
89cc7147 4278 end if;
4279
81b424ac 4280 Analyze (Expr);
4281
4282 if not Is_Overloaded (Expr) then
4283 Check_One_Function (Entity (Expr));
4284
4285 else
4286 declare
2c5754de 4287 I : Interp_Index;
81b424ac 4288 It : Interp;
4289
4290 begin
cac18f71 4291 Indexing_Found := False;
81b424ac 4292 Get_First_Interp (Expr, I, It);
4293 while Present (It.Nam) loop
4294
4295 -- Note that analysis will have added the interpretation
4296 -- that corresponds to the dereference. We only check the
4297 -- subprogram itself.
4298
4299 if Is_Overloadable (It.Nam) then
4300 Check_One_Function (It.Nam);
4301 end if;
4302
4303 Get_Next_Interp (I, It);
4304 end loop;
4305 end;
4306 end if;
7796365f 4307
7c0c95b8 4308 if not Indexing_Found and then not Error_Posted (N) then
7796365f 4309 Error_Msg_NE
4310 ("aspect Indexing requires a local function that "
4311 & "applies to type&", Expr, Ent);
4312 end if;
81b424ac 4313 end Check_Indexing_Functions;
4314
89cc7147 4315 ------------------------------
4316 -- Check_Iterator_Functions --
4317 ------------------------------
4318
4319 procedure Check_Iterator_Functions is
89cc7147 4320 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
8df4f2a5 4321 -- Check one possible interpretation for validity
89cc7147 4322
4323 ----------------------------
4324 -- Valid_Default_Iterator --
4325 ----------------------------
4326
4327 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
8b8be176 4328 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
7f5dd8d8 4329 Formal : Entity_Id;
89cc7147 4330
4331 begin
4332 if not Check_Primitive_Function (Subp) then
4333 return False;
8b8be176 4334
4335 -- The return type must be derived from a type in an instance
4336 -- of Iterator.Interfaces, and thus its root type must have a
4337 -- predefined name.
4338
4339 elsif Chars (Root_T) /= Name_Forward_Iterator
4340 and then Chars (Root_T) /= Name_Reversible_Iterator
4341 then
4342 return False;
4343
89cc7147 4344 else
4345 Formal := First_Formal (Subp);
4346 end if;
4347
8df4f2a5 4348 -- False if any subsequent formal has no default expression
89cc7147 4349
8df4f2a5 4350 Formal := Next_Formal (Formal);
4351 while Present (Formal) loop
4352 if No (Expression (Parent (Formal))) then
4353 return False;
4354 end if;
89cc7147 4355
8df4f2a5 4356 Next_Formal (Formal);
4357 end loop;
89cc7147 4358
8df4f2a5 4359 -- True if all subsequent formals have default expressions
89cc7147 4360
4361 return True;
4362 end Valid_Default_Iterator;
4363
4364 -- Start of processing for Check_Iterator_Functions
4365
4366 begin
4367 Analyze (Expr);
4368
4369 if not Is_Entity_Name (Expr) then
4370 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4371 end if;
4372
4373 if not Is_Overloaded (Expr) then
4374 if not Check_Primitive_Function (Entity (Expr)) then
4375 Error_Msg_NE
4376 ("aspect Indexing requires a function that applies to type&",
4377 Entity (Expr), Ent);
4378 end if;
4379
05f6f999 4380 -- Flag the default_iterator as well as the denoted function.
4381
89cc7147 4382 if not Valid_Default_Iterator (Entity (Expr)) then
05f6f999 4383 Error_Msg_N ("improper function for default iterator!", Expr);
89cc7147 4384 end if;
4385
4386 else
89cc7147 4387 declare
270ee9c5 4388 Default : Entity_Id := Empty;
8be33fbe 4389 I : Interp_Index;
4390 It : Interp;
89cc7147 4391
4392 begin
4393 Get_First_Interp (Expr, I, It);
4394 while Present (It.Nam) loop
4395 if not Check_Primitive_Function (It.Nam)
59f3e675 4396 or else not Valid_Default_Iterator (It.Nam)
89cc7147 4397 then
4398 Remove_Interp (I);
4399
4400 elsif Present (Default) then
89cc7147 4401
8be33fbe 4402 -- An explicit one should override an implicit one
4403
4404 if Comes_From_Source (Default) =
4405 Comes_From_Source (It.Nam)
4406 then
4407 Error_Msg_N ("default iterator must be unique", Expr);
4408 Error_Msg_Sloc := Sloc (Default);
4409 Error_Msg_N ("\\possible interpretation#", Expr);
4410 Error_Msg_Sloc := Sloc (It.Nam);
4411 Error_Msg_N ("\\possible interpretation#", Expr);
4412
4413 elsif Comes_From_Source (It.Nam) then
4414 Default := It.Nam;
4415 end if;
89cc7147 4416 else
4417 Default := It.Nam;
4418 end if;
4419
4420 Get_Next_Interp (I, It);
4421 end loop;
89cc7147 4422
270ee9c5 4423 if Present (Default) then
4424 Set_Entity (Expr, Default);
4425 Set_Is_Overloaded (Expr, False);
8b8be176 4426 else
4427 Error_Msg_N
7f5dd8d8 4428 ("no interpretation is a valid default iterator!", Expr);
270ee9c5 4429 end if;
4430 end;
89cc7147 4431 end if;
4432 end Check_Iterator_Functions;
4433
4434 -------------------------------
4435 -- Check_Primitive_Function --
4436 -------------------------------
4437
4438 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4439 Ctrl : Entity_Id;
4440
4441 begin
4442 if Ekind (Subp) /= E_Function then
4443 return False;
4444 end if;
4445
4446 if No (First_Formal (Subp)) then
4447 return False;
4448 else
4449 Ctrl := Etype (First_Formal (Subp));
4450 end if;
4451
05f6f999 4452 -- To be a primitive operation subprogram has to be in same scope.
4453
4454 if Scope (Ctrl) /= Scope (Subp) then
4455 return False;
4456 end if;
4457
7d6fb253 4458 -- Type of formal may be the class-wide type, an access to such,
4459 -- or an incomplete view.
4460
89cc7147 4461 if Ctrl = Ent
4462 or else Ctrl = Class_Wide_Type (Ent)
4463 or else
4464 (Ekind (Ctrl) = E_Anonymous_Access_Type
b85d62ec 4465 and then (Designated_Type (Ctrl) = Ent
4466 or else
4467 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
7d6fb253 4468 or else
4469 (Ekind (Ctrl) = E_Incomplete_Type
4470 and then Full_View (Ctrl) = Ent)
89cc7147 4471 then
4472 null;
89cc7147 4473 else
4474 return False;
4475 end if;
4476
4477 return True;
4478 end Check_Primitive_Function;
4479
ae888dbd 4480 ----------------------
4481 -- Duplicate_Clause --
4482 ----------------------
4483
4484 function Duplicate_Clause return Boolean is
d74fc39a 4485 A : Node_Id;
ae888dbd 4486
4487 begin
c8969ba6 4488 -- Nothing to do if this attribute definition clause comes from
4489 -- an aspect specification, since we could not be duplicating an
ae888dbd 4490 -- explicit clause, and we dealt with the case of duplicated aspects
4491 -- in Analyze_Aspect_Specifications.
4492
4493 if From_Aspect_Specification (N) then
4494 return False;
4495 end if;
4496
89f1e35c 4497 -- Otherwise current clause may duplicate previous clause, or a
4498 -- previously given pragma or aspect specification for the same
4499 -- aspect.
d74fc39a 4500
89b3b365 4501 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
ae888dbd 4502
4503 if Present (A) then
89f1e35c 4504 Error_Msg_Name_1 := Chars (N);
4505 Error_Msg_Sloc := Sloc (A);
4506
89b3b365 4507 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
89f1e35c 4508 return True;
ae888dbd 4509 end if;
4510
4511 return False;
4512 end Duplicate_Clause;
4513
9f373bb8 4514 -- Start of processing for Analyze_Attribute_Definition_Clause
4515
d6f39728 4516 begin
d64221a7 4517 -- The following code is a defense against recursion. Not clear that
51fa2a45 4518 -- this can happen legitimately, but perhaps some error situations can
4519 -- cause it, and we did see this recursion during testing.
d64221a7 4520
4521 if Analyzed (N) then
4522 return;
4523 else
4524 Set_Analyzed (N, True);
4525 end if;
4526
2609e4d0 4527 Check_Restriction_No_Use_Of_Attribute (N);
4528
a29bc1d9 4529 -- Ignore some selected attributes in CodePeer mode since they are not
4530 -- relevant in this context.
4531
4532 if CodePeer_Mode then
4533 case Id is
4534
4535 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4536 -- internal representation of types by implicitly packing them.
4537
4538 when Attribute_Component_Size =>
4539 Rewrite (N, Make_Null_Statement (Sloc (N)));
4540 return;
4541
4542 when others =>
4543 null;
4544 end case;
4545 end if;
4546
d8ba53a8 4547 -- Process Ignore_Rep_Clauses option
eef1ca1e 4548
d8ba53a8 4549 if Ignore_Rep_Clauses then
9d627c41 4550 case Id is
4551
eef1ca1e 4552 -- The following should be ignored. They do not affect legality
4553 -- and may be target dependent. The basic idea of -gnatI is to
4554 -- ignore any rep clauses that may be target dependent but do not
4555 -- affect legality (except possibly to be rejected because they
4556 -- are incompatible with the compilation target).
9d627c41 4557
2f1aac99 4558 when Attribute_Alignment |
9d627c41 4559 Attribute_Bit_Order |
4560 Attribute_Component_Size |
4561 Attribute_Machine_Radix |
4562 Attribute_Object_Size |
4563 Attribute_Size |
2ff55065 4564 Attribute_Small |
9d627c41 4565 Attribute_Stream_Size |
4566 Attribute_Value_Size =>
2ff55065 4567 Kill_Rep_Clause (N);
9d627c41 4568 return;
4569
eef1ca1e 4570 -- The following should not be ignored, because in the first place
51fa2a45 4571 -- they are reasonably portable, and should not cause problems
4572 -- in compiling code from another target, and also they do affect
4573 -- legality, e.g. failing to provide a stream attribute for a type
4574 -- may make a program illegal.
9d627c41 4575
b55f7641 4576 when Attribute_External_Tag |
4577 Attribute_Input |
4578 Attribute_Output |
4579 Attribute_Read |
4580 Attribute_Simple_Storage_Pool |
4581 Attribute_Storage_Pool |
4582 Attribute_Storage_Size |
4583 Attribute_Write =>
9d627c41 4584 null;
4585
2ff55065 4586 -- We do not do anything here with address clauses, they will be
4587 -- removed by Freeze later on, but for now, it works better to
4588 -- keep then in the tree.
4589
4590 when Attribute_Address =>
4591 null;
4592
b593a52c 4593 -- Other cases are errors ("attribute& cannot be set with
4594 -- definition clause"), which will be caught below.
9d627c41 4595
4596 when others =>
4597 null;
4598 end case;
fbc67f84 4599 end if;
4600
d6f39728 4601 Analyze (Nam);
4602 Ent := Entity (Nam);
4603
4604 if Rep_Item_Too_Early (Ent, N) then
4605 return;
4606 end if;
4607
9f373bb8 4608 -- Rep clause applies to full view of incomplete type or private type if
4609 -- we have one (if not, this is a premature use of the type). However,
4610 -- certain semantic checks need to be done on the specified entity (i.e.
4611 -- the private view), so we save it in Ent.
d6f39728 4612
4613 if Is_Private_Type (Ent)
4614 and then Is_Derived_Type (Ent)
4615 and then not Is_Tagged_Type (Ent)
4616 and then No (Full_View (Ent))
4617 then
9f373bb8 4618 -- If this is a private type whose completion is a derivation from
4619 -- another private type, there is no full view, and the attribute
4620 -- belongs to the type itself, not its underlying parent.
d6f39728 4621
4622 U_Ent := Ent;
4623
4624 elsif Ekind (Ent) = E_Incomplete_Type then
d5b349fa 4625
9f373bb8 4626 -- The attribute applies to the full view, set the entity of the
4627 -- attribute definition accordingly.
d5b349fa 4628
d6f39728 4629 Ent := Underlying_Type (Ent);
4630 U_Ent := Ent;
d5b349fa 4631 Set_Entity (Nam, Ent);
4632
d6f39728 4633 else
4634 U_Ent := Underlying_Type (Ent);
4635 end if;
4636
44705307 4637 -- Avoid cascaded error
d6f39728 4638
4639 if Etype (Nam) = Any_Type then
4640 return;
4641
89f1e35c 4642 -- Must be declared in current scope or in case of an aspect
ace3389d 4643 -- specification, must be visible in current scope.
44705307 4644
89f1e35c 4645 elsif Scope (Ent) /= Current_Scope
ace3389d 4646 and then
4647 not (From_Aspect_Specification (N)
4648 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
89f1e35c 4649 then
d6f39728 4650 Error_Msg_N ("entity must be declared in this scope", Nam);
4651 return;
4652
44705307 4653 -- Must not be a source renaming (we do have some cases where the
4654 -- expander generates a renaming, and those cases are OK, in such
a3248fc4 4655 -- cases any attribute applies to the renamed object as well).
44705307 4656
4657 elsif Is_Object (Ent)
4658 and then Present (Renamed_Object (Ent))
44705307 4659 then
a3248fc4 4660 -- Case of renamed object from source, this is an error
4661
4662 if Comes_From_Source (Renamed_Object (Ent)) then
4663 Get_Name_String (Chars (N));
4664 Error_Msg_Strlen := Name_Len;
4665 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4666 Error_Msg_N
4667 ("~ clause not allowed for a renaming declaration "
4668 & "(RM 13.1(6))", Nam);
4669 return;
4670
4671 -- For the case of a compiler generated renaming, the attribute
4672 -- definition clause applies to the renamed object created by the
4673 -- expander. The easiest general way to handle this is to create a
4674 -- copy of the attribute definition clause for this object.
4675
9a48fc56 4676 elsif Is_Entity_Name (Renamed_Object (Ent)) then
a3248fc4 4677 Insert_Action (N,
4678 Make_Attribute_Definition_Clause (Loc,
4679 Name =>
4680 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4681 Chars => Chars (N),
4682 Expression => Duplicate_Subexpr (Expression (N))));
9a48fc56 4683
4684 -- If the renamed object is not an entity, it must be a dereference
4685 -- of an unconstrained function call, and we must introduce a new
4686 -- declaration to capture the expression. This is needed in the case
4687 -- of 'Alignment, where the original declaration must be rewritten.
4688
4689 else
4690 pragma Assert
4691 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4692 null;
a3248fc4 4693 end if;
44705307 4694
4695 -- If no underlying entity, use entity itself, applies to some
4696 -- previously detected error cases ???
4697
f15731c4 4698 elsif No (U_Ent) then
4699 U_Ent := Ent;
4700
44705307 4701 -- Cannot specify for a subtype (exception Object/Value_Size)
4702
d6f39728 4703 elsif Is_Type (U_Ent)
4704 and then not Is_First_Subtype (U_Ent)
4705 and then Id /= Attribute_Object_Size
4706 and then Id /= Attribute_Value_Size
4707 and then not From_At_Mod (N)
4708 then
4709 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4710 return;
d6f39728 4711 end if;
4712
ae888dbd 4713 Set_Entity (N, U_Ent);
4714
d6f39728 4715 -- Switch on particular attribute
4716
4717 case Id is
4718
4719 -------------
4720 -- Address --
4721 -------------
4722
4723 -- Address attribute definition clause
4724
4725 when Attribute_Address => Address : begin
177675a7 4726
4727 -- A little error check, catch for X'Address use X'Address;
4728
4729 if Nkind (Nam) = N_Identifier
4730 and then Nkind (Expr) = N_Attribute_Reference
4731 and then Attribute_Name (Expr) = Name_Address
4732 and then Nkind (Prefix (Expr)) = N_Identifier
4733 and then Chars (Nam) = Chars (Prefix (Expr))
4734 then
4735 Error_Msg_NE
4736 ("address for & is self-referencing", Prefix (Expr), Ent);
4737 return;
4738 end if;
4739
4740 -- Not that special case, carry on with analysis of expression
4741
d6f39728 4742 Analyze_And_Resolve (Expr, RTE (RE_Address));
4743
2f1aac99 4744 -- Even when ignoring rep clauses we need to indicate that the
4745 -- entity has an address clause and thus it is legal to declare
2ff55065 4746 -- it imported. Freeze will get rid of the address clause later.
2f1aac99 4747
4748 if Ignore_Rep_Clauses then
d3ef794c 4749 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2f1aac99 4750 Record_Rep_Item (U_Ent, N);
4751 end if;
4752
4753 return;
4754 end if;
4755
ae888dbd 4756 if Duplicate_Clause then
4757 null;
d6f39728 4758
4759 -- Case of address clause for subprogram
4760
4761 elsif Is_Subprogram (U_Ent) then
d6f39728 4762 if Has_Homonym (U_Ent) then
4763 Error_Msg_N
f74a102b 4764 ("address clause cannot be given for overloaded "
4765 & "subprogram", Nam);
83f8f0a6 4766 return;
d6f39728 4767 end if;
4768
83f8f0a6 4769 -- For subprograms, all address clauses are permitted, and we
4770 -- mark the subprogram as having a deferred freeze so that Gigi
4771 -- will not elaborate it too soon.
d6f39728 4772
4773 -- Above needs more comments, what is too soon about???
4774
4775 Set_Has_Delayed_Freeze (U_Ent);
4776
4777 -- Case of address clause for entry
4778
4779 elsif Ekind (U_Ent) = E_Entry then
d6f39728 4780 if Nkind (Parent (N)) = N_Task_Body then
4781 Error_Msg_N
4782 ("entry address must be specified in task spec", Nam);
83f8f0a6 4783 return;
d6f39728 4784 end if;
4785
4786 -- For entries, we require a constant address
4787
4788 Check_Constant_Address_Clause (Expr, U_Ent);
4789
83f8f0a6 4790 -- Special checks for task types
4791
f15731c4 4792 if Is_Task_Type (Scope (U_Ent))
4793 and then Comes_From_Source (Scope (U_Ent))
4794 then
4795 Error_Msg_N
1e3532e7 4796 ("??entry address declared for entry in task type", N);
f15731c4 4797 Error_Msg_N
1e3532e7 4798 ("\??only one task can be declared of this type", N);
f15731c4 4799 end if;
4800
83f8f0a6 4801 -- Entry address clauses are obsolescent
4802
e0521a36 4803 Check_Restriction (No_Obsolescent_Features, N);
4804
9dfe12ae 4805 if Warn_On_Obsolescent_Feature then
4806 Error_Msg_N
f74a102b 4807 ("?j?attaching interrupt to task entry is an obsolescent "
4808 & "feature (RM J.7.1)", N);
9dfe12ae 4809 Error_Msg_N
1e3532e7 4810 ("\?j?use interrupt procedure instead", N);
9dfe12ae 4811 end if;
4812
83f8f0a6 4813 -- Case of an address clause for a controlled object which we
4814 -- consider to be erroneous.
9dfe12ae 4815
83f8f0a6 4816 elsif Is_Controlled (Etype (U_Ent))
4817 or else Has_Controlled_Component (Etype (U_Ent))
4818 then
9dfe12ae 4819 Error_Msg_NE
1e3532e7 4820 ("??controlled object& must not be overlaid", Nam, U_Ent);
9dfe12ae 4821 Error_Msg_N
1e3532e7 4822 ("\??Program_Error will be raised at run time", Nam);
9dfe12ae 4823 Insert_Action (Declaration_Node (U_Ent),
4824 Make_Raise_Program_Error (Loc,
4825 Reason => PE_Overlaid_Controlled_Object));
83f8f0a6 4826 return;
9dfe12ae 4827
4828 -- Case of address clause for a (non-controlled) object
d6f39728 4829
f02a9a9a 4830 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
d6f39728 4831 declare
d6da7448 4832 Expr : constant Node_Id := Expression (N);
4833 O_Ent : Entity_Id;
4834 Off : Boolean;
d6f39728 4835
4836 begin
7ee315cc 4837 -- Exported variables cannot have an address clause, because
4838 -- this cancels the effect of the pragma Export.
d6f39728 4839
4840 if Is_Exported (U_Ent) then
4841 Error_Msg_N
4842 ("cannot export object with address clause", Nam);
83f8f0a6 4843 return;
d6da7448 4844 end if;
4845
4846 Find_Overlaid_Entity (N, O_Ent, Off);
d6f39728 4847
a9dd889b 4848 if Present (O_Ent) then
798dec73 4849
a9dd889b 4850 -- If the object overlays a constant object, mark it so
b2d32174 4851
a9dd889b 4852 if Is_Constant_Object (O_Ent) then
4853 Set_Overlays_Constant (U_Ent);
4854 end if;
798dec73 4855
514a5555 4856 -- If the address clause is of the form:
4857
4858 -- for X'Address use Y'Address;
4859
4860 -- or
4861
4862 -- C : constant Address := Y'Address;
4863 -- ...
4864 -- for X'Address use C;
4865
4866 -- then we make an entry in the table to check the size
4867 -- and alignment of the overlaying variable. But we defer
4868 -- this check till after code generation to take full
4869 -- advantage of the annotation done by the back end.
4870
4871 -- If the entity has a generic type, the check will be
4872 -- performed in the instance if the actual type justifies
4873 -- it, and we do not insert the clause in the table to
4874 -- prevent spurious warnings.
4875
4876 -- Note: we used to test Comes_From_Source and only give
4877 -- this warning for source entities, but we have removed
4878 -- this test. It really seems bogus to generate overlays
4879 -- that would trigger this warning in generated code.
4880 -- Furthermore, by removing the test, we handle the
4881 -- aspect case properly.
4882
4883 if Is_Object (O_Ent)
4884 and then not Is_Generic_Type (Etype (U_Ent))
4885 and then Address_Clause_Overlay_Warnings
4886 then
4887 Address_Clause_Checks.Append
4888 ((N, U_Ent, No_Uint, O_Ent, Off));
4889 end if;
a9dd889b 4890 else
4891 -- If this is not an overlay, mark a variable as being
4892 -- volatile to prevent unwanted optimizations. It's a
4893 -- conservative interpretation of RM 13.3(19) for the
4894 -- cases where the compiler cannot detect potential
4895 -- aliasing issues easily and it also covers the case
4896 -- of an absolute address where the volatile aspect is
4897 -- kind of implicit.
4898
4899 if Ekind (U_Ent) = E_Variable then
4900 Set_Treat_As_Volatile (U_Ent);
4901 end if;
514a5555 4902
4903 -- Make an entry in the table for an absolute address as
4904 -- above to check that the value is compatible with the
4905 -- alignment of the object.
4906
4907 declare
4908 Addr : constant Node_Id := Address_Value (Expr);
4909 begin
4910 if Compile_Time_Known_Value (Addr)
4911 and then Address_Clause_Overlay_Warnings
4912 then
4913 Address_Clause_Checks.Append
4914 ((N, U_Ent, Expr_Value (Addr), Empty, False));
4915 end if;
4916 end;
b2d32174 4917 end if;
4918
798dec73 4919 -- Overlaying controlled objects is erroneous. Emit warning
4920 -- but continue analysis because program is itself legal,
3ff5e35d 4921 -- and back end must see address clause.
9dfe12ae 4922
d6da7448 4923 if Present (O_Ent)
4924 and then (Has_Controlled_Component (Etype (O_Ent))
f02a9a9a 4925 or else Is_Controlled (Etype (O_Ent)))
0c30cda1 4926 and then not Inside_A_Generic
9dfe12ae 4927 then
4928 Error_Msg_N
0c30cda1 4929 ("??cannot use overlays with controlled objects", Expr);
9dfe12ae 4930 Error_Msg_N
1e3532e7 4931 ("\??Program_Error will be raised at run time", Expr);
9dfe12ae 4932 Insert_Action (Declaration_Node (U_Ent),
4933 Make_Raise_Program_Error (Loc,
4934 Reason => PE_Overlaid_Controlled_Object));
4935
95009d64 4936 -- Issue an unconditional warning for a constant overlaying
4937 -- a variable. For the reverse case, we will issue it only
b2d32174 4938 -- if the variable is modified.
95009d64 4939
b2d32174 4940 elsif Ekind (U_Ent) = E_Constant
95009d64 4941 and then Present (O_Ent)
b2d32174 4942 and then not Overlays_Constant (U_Ent)
4943 and then Address_Clause_Overlay_Warnings
9dfe12ae 4944 then
1e3532e7 4945 Error_Msg_N ("??constant overlays a variable", Expr);
9dfe12ae 4946
d6f39728 4947 -- Imported variables can have an address clause, but then
4948 -- the import is pretty meaningless except to suppress
4949 -- initializations, so we do not need such variables to
4950 -- be statically allocated (and in fact it causes trouble
4951 -- if the address clause is a local value).
4952
4953 elsif Is_Imported (U_Ent) then
4954 Set_Is_Statically_Allocated (U_Ent, False);
4955 end if;
4956
4957 -- We mark a possible modification of a variable with an
4958 -- address clause, since it is likely aliasing is occurring.
4959
177675a7 4960 Note_Possible_Modification (Nam, Sure => False);
d6f39728 4961
9dfe12ae 4962 -- Legality checks on the address clause for initialized
4963 -- objects is deferred until the freeze point, because
2beb22b1 4964 -- a subsequent pragma might indicate that the object
42e09e36 4965 -- is imported and thus not initialized. Also, the address
4966 -- clause might involve entities that have yet to be
4967 -- elaborated.
9dfe12ae 4968
4969 Set_Has_Delayed_Freeze (U_Ent);
4970
51ad5ad2 4971 -- If an initialization call has been generated for this
4972 -- object, it needs to be deferred to after the freeze node
4973 -- we have just now added, otherwise GIGI will see a
4974 -- reference to the variable (as actual to the IP call)
4975 -- before its definition.
4976
4977 declare
df9fba45 4978 Init_Call : constant Node_Id :=
4979 Remove_Init_Call (U_Ent, N);
4bba0a8d 4980
51ad5ad2 4981 begin
4982 if Present (Init_Call) then
28a4283c 4983 Append_Freeze_Action (U_Ent, Init_Call);
df9fba45 4984
28a4283c 4985 -- Reset Initialization_Statements pointer so that
4986 -- if there is a pragma Import further down, it can
4987 -- clear any default initialization.
df9fba45 4988
28a4283c 4989 Set_Initialization_Statements (U_Ent, Init_Call);
51ad5ad2 4990 end if;
4991 end;
4992
44e4341e 4993 -- Entity has delayed freeze, so we will generate an
4994 -- alignment check at the freeze point unless suppressed.
d6f39728 4995
44e4341e 4996 if not Range_Checks_Suppressed (U_Ent)
4997 and then not Alignment_Checks_Suppressed (U_Ent)
4998 then
4999 Set_Check_Address_Alignment (N);
5000 end if;
d6f39728 5001
5002 -- Kill the size check code, since we are not allocating
5003 -- the variable, it is somewhere else.
5004
5005 Kill_Size_Check_Code (U_Ent);
d6da7448 5006 end;
83f8f0a6 5007
d6f39728 5008 -- Not a valid entity for an address clause
5009
5010 else
5011 Error_Msg_N ("address cannot be given for &", Nam);
5012 end if;
5013 end Address;
5014
5015 ---------------
5016 -- Alignment --
5017 ---------------
5018
5019 -- Alignment attribute definition clause
5020
b47769f0 5021 when Attribute_Alignment => Alignment : declare
208fd589 5022 Align : constant Uint := Get_Alignment_Value (Expr);
5023 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
41331dcf 5024
d6f39728 5025 begin
5026 FOnly := True;
5027
5028 if not Is_Type (U_Ent)
5029 and then Ekind (U_Ent) /= E_Variable
5030 and then Ekind (U_Ent) /= E_Constant
5031 then
5032 Error_Msg_N ("alignment cannot be given for &", Nam);
5033
ae888dbd 5034 elsif Duplicate_Clause then
5035 null;
d6f39728 5036
5037 elsif Align /= No_Uint then
5038 Set_Has_Alignment_Clause (U_Ent);
208fd589 5039
44705307 5040 -- Tagged type case, check for attempt to set alignment to a
f74a102b 5041 -- value greater than Max_Align, and reset if so. This error
5042 -- is suppressed in ASIS mode to allow for different ASIS
f9906591 5043 -- back ends or ASIS-based tools to query the illegal clause.
44705307 5044
f74a102b 5045 if Is_Tagged_Type (U_Ent)
5046 and then Align > Max_Align
5047 and then not ASIS_Mode
5048 then
208fd589 5049 Error_Msg_N
1e3532e7 5050 ("alignment for & set to Maximum_Aligment??", Nam);
f74a102b 5051 Set_Alignment (U_Ent, Max_Align);
44705307 5052
5053 -- All other cases
5054
208fd589 5055 else
5056 Set_Alignment (U_Ent, Align);
5057 end if;
b47769f0 5058
5059 -- For an array type, U_Ent is the first subtype. In that case,
5060 -- also set the alignment of the anonymous base type so that
5061 -- other subtypes (such as the itypes for aggregates of the
5062 -- type) also receive the expected alignment.
5063
5064 if Is_Array_Type (U_Ent) then
5065 Set_Alignment (Base_Type (U_Ent), Align);
5066 end if;
d6f39728 5067 end if;
b47769f0 5068 end Alignment;
d6f39728 5069
5070 ---------------
5071 -- Bit_Order --
5072 ---------------
5073
5074 -- Bit_Order attribute definition clause
5075
5076 when Attribute_Bit_Order => Bit_Order : declare
5077 begin
5078 if not Is_Record_Type (U_Ent) then
5079 Error_Msg_N
5080 ("Bit_Order can only be defined for record type", Nam);
5081
ae888dbd 5082 elsif Duplicate_Clause then
5083 null;
5084
d6f39728 5085 else
5086 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5087
5088 if Etype (Expr) = Any_Type then
5089 return;
5090
cda40848 5091 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5092 Flag_Non_Static_Expr
5093 ("Bit_Order requires static expression!", Expr);
d6f39728 5094
5095 else
5096 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
fae4ea1f 5097 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
d6f39728 5098 end if;
5099 end if;
5100 end if;
5101 end Bit_Order;
5102
5103 --------------------
5104 -- Component_Size --
5105 --------------------
5106
5107 -- Component_Size attribute definition clause
5108
5109 when Attribute_Component_Size => Component_Size_Case : declare
5110 Csize : constant Uint := Static_Integer (Expr);
a0fc8c5b 5111 Ctyp : Entity_Id;
d6f39728 5112 Btype : Entity_Id;
5113 Biased : Boolean;
5114 New_Ctyp : Entity_Id;
5115 Decl : Node_Id;
5116
5117 begin
5118 if not Is_Array_Type (U_Ent) then
5119 Error_Msg_N ("component size requires array type", Nam);
5120 return;
5121 end if;
5122
5123 Btype := Base_Type (U_Ent);
f74a102b 5124 Ctyp := Component_Type (Btype);
d6f39728 5125
ae888dbd 5126 if Duplicate_Clause then
5127 null;
d6f39728 5128
f3e4db96 5129 elsif Rep_Item_Too_Early (Btype, N) then
5130 null;
5131
d6f39728 5132 elsif Csize /= No_Uint then
a0fc8c5b 5133 Check_Size (Expr, Ctyp, Csize, Biased);
d6f39728 5134
d74fc39a 5135 -- For the biased case, build a declaration for a subtype that
5136 -- will be used to represent the biased subtype that reflects
5137 -- the biased representation of components. We need the subtype
5138 -- to get proper conversions on referencing elements of the
36ac5fbb 5139 -- array.
3062c401 5140
36ac5fbb 5141 if Biased then
5142 New_Ctyp :=
5143 Make_Defining_Identifier (Loc,
5144 Chars =>
5145 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3062c401 5146
36ac5fbb 5147 Decl :=
5148 Make_Subtype_Declaration (Loc,
5149 Defining_Identifier => New_Ctyp,
5150 Subtype_Indication =>
5151 New_Occurrence_Of (Component_Type (Btype), Loc));
5152
5153 Set_Parent (Decl, N);
5154 Analyze (Decl, Suppress => All_Checks);
5155
5156 Set_Has_Delayed_Freeze (New_Ctyp, False);
5157 Set_Esize (New_Ctyp, Csize);
5158 Set_RM_Size (New_Ctyp, Csize);
5159 Init_Alignment (New_Ctyp);
5160 Set_Is_Itype (New_Ctyp, True);
5161 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5162
5163 Set_Component_Type (Btype, New_Ctyp);
5164 Set_Biased (New_Ctyp, N, "component size clause");
d6f39728 5165 end if;
5166
36ac5fbb 5167 Set_Component_Size (Btype, Csize);
5168
a0fc8c5b 5169 -- Deal with warning on overridden size
5170
5171 if Warn_On_Overridden_Size
5172 and then Has_Size_Clause (Ctyp)
5173 and then RM_Size (Ctyp) /= Csize
5174 then
5175 Error_Msg_NE
1e3532e7 5176 ("component size overrides size clause for&?S?", N, Ctyp);
a0fc8c5b 5177 end if;
5178
d6f39728 5179 Set_Has_Component_Size_Clause (Btype, True);
f3e4db96 5180 Set_Has_Non_Standard_Rep (Btype, True);
d6f39728 5181 end if;
5182 end Component_Size_Case;
5183
81b424ac 5184 -----------------------
5185 -- Constant_Indexing --
5186 -----------------------
5187
5188 when Attribute_Constant_Indexing =>
5189 Check_Indexing_Functions;
5190
89f1e35c 5191 ---------
5192 -- CPU --
5193 ---------
5194
5195 when Attribute_CPU => CPU :
5196 begin
5197 -- CPU attribute definition clause not allowed except from aspect
5198 -- specification.
5199
5200 if From_Aspect_Specification (N) then
5201 if not Is_Task_Type (U_Ent) then
5202 Error_Msg_N ("CPU can only be defined for task", Nam);
5203
5204 elsif Duplicate_Clause then
5205 null;
5206
5207 else
5208 -- The expression must be analyzed in the special manner
5209 -- described in "Handling of Default and Per-Object
5210 -- Expressions" in sem.ads.
5211
5212 -- The visibility to the discriminants must be restored
5213
5214 Push_Scope_And_Install_Discriminants (U_Ent);
5215 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5216 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5217
cda40848 5218 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5219 Check_Restriction (Static_Priorities, Expr);
5220 end if;
5221 end if;
5222
5223 else
5224 Error_Msg_N
5225 ("attribute& cannot be set with definition clause", N);
5226 end if;
5227 end CPU;
5228
89cc7147 5229 ----------------------
5230 -- Default_Iterator --
5231 ----------------------
5232
5233 when Attribute_Default_Iterator => Default_Iterator : declare
5234 Func : Entity_Id;
fbf4d6ef 5235 Typ : Entity_Id;
89cc7147 5236
5237 begin
05f6f999 5238 -- If target type is untagged, further checks are irrelevant
5239
89cc7147 5240 if not Is_Tagged_Type (U_Ent) then
5241 Error_Msg_N
05f6f999 5242 ("aspect Default_Iterator applies to tagged type", Nam);
5243 return;
89cc7147 5244 end if;
5245
5246 Check_Iterator_Functions;
5247
5248 Analyze (Expr);
5249
5250 if not Is_Entity_Name (Expr)
5251 or else Ekind (Entity (Expr)) /= E_Function
5252 then
5253 Error_Msg_N ("aspect Iterator must be a function", Expr);
05f6f999 5254 return;
89cc7147 5255 else
5256 Func := Entity (Expr);
5257 end if;
5258
fbf4d6ef 5259 -- The type of the first parameter must be T, T'class, or a
05f6f999 5260 -- corresponding access type (5.5.1 (8/3). If function is
5261 -- parameterless label type accordingly.
fbf4d6ef 5262
5263 if No (First_Formal (Func)) then
05f6f999 5264 Typ := Any_Type;
fbf4d6ef 5265 else
5266 Typ := Etype (First_Formal (Func));
5267 end if;
5268
5269 if Typ = U_Ent
5270 or else Typ = Class_Wide_Type (U_Ent)
5271 or else (Is_Access_Type (Typ)
5272 and then Designated_Type (Typ) = U_Ent)
5273 or else (Is_Access_Type (Typ)
5274 and then Designated_Type (Typ) =
5275 Class_Wide_Type (U_Ent))
89cc7147 5276 then
fbf4d6ef 5277 null;
5278
5279 else
89cc7147 5280 Error_Msg_NE
5281 ("Default Iterator must be a primitive of&", Func, U_Ent);
5282 end if;
5283 end Default_Iterator;
5284
89f1e35c 5285 ------------------------
5286 -- Dispatching_Domain --
5287 ------------------------
5288
5289 when Attribute_Dispatching_Domain => Dispatching_Domain :
5290 begin
5291 -- Dispatching_Domain attribute definition clause not allowed
5292 -- except from aspect specification.
5293
5294 if From_Aspect_Specification (N) then
5295 if not Is_Task_Type (U_Ent) then
fbf4d6ef 5296 Error_Msg_N
5297 ("Dispatching_Domain can only be defined for task", Nam);
89f1e35c 5298
5299 elsif Duplicate_Clause then
5300 null;
5301
5302 else
5303 -- The expression must be analyzed in the special manner
5304 -- described in "Handling of Default and Per-Object
5305 -- Expressions" in sem.ads.
5306
5307 -- The visibility to the discriminants must be restored
5308
5309 Push_Scope_And_Install_Discriminants (U_Ent);
5310
5311 Preanalyze_Spec_Expression
5312 (Expr, RTE (RE_Dispatching_Domain));
5313
5314 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5315 end if;
5316
5317 else
5318 Error_Msg_N
5319 ("attribute& cannot be set with definition clause", N);
5320 end if;
5321 end Dispatching_Domain;
5322
d6f39728 5323 ------------------
5324 -- External_Tag --
5325 ------------------
5326
5327 when Attribute_External_Tag => External_Tag :
5328 begin
5329 if not Is_Tagged_Type (U_Ent) then
5330 Error_Msg_N ("should be a tagged type", Nam);
5331 end if;
5332
ae888dbd 5333 if Duplicate_Clause then
5334 null;
d6f39728 5335
9af0ddc7 5336 else
ae888dbd 5337 Analyze_And_Resolve (Expr, Standard_String);
fbc67f84 5338
cda40848 5339 if not Is_OK_Static_Expression (Expr) then
ae888dbd 5340 Flag_Non_Static_Expr
5341 ("static string required for tag name!", Nam);
5342 end if;
5343
ae888dbd 5344 if not Is_Library_Level_Entity (U_Ent) then
5345 Error_Msg_NE
1e3532e7 5346 ("??non-unique external tag supplied for &", N, U_Ent);
ae888dbd 5347 Error_Msg_N
f74a102b 5348 ("\??same external tag applies to all subprogram calls",
5349 N);
ae888dbd 5350 Error_Msg_N
1e3532e7 5351 ("\??corresponding internal tag cannot be obtained", N);
ae888dbd 5352 end if;
fbc67f84 5353 end if;
d6f39728 5354 end External_Tag;
5355
b57530b8 5356 --------------------------
5357 -- Implicit_Dereference --
5358 --------------------------
7947a439 5359
b57530b8 5360 when Attribute_Implicit_Dereference =>
7947a439 5361
2beb22b1 5362 -- Legality checks already performed at the point of the type
5363 -- declaration, aspect is not delayed.
7947a439 5364
89cc7147 5365 null;
b57530b8 5366
d6f39728 5367 -----------
5368 -- Input --
5369 -----------
5370
9f373bb8 5371 when Attribute_Input =>
5372 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5373 Set_Has_Specified_Stream_Input (Ent);
d6f39728 5374
89f1e35c 5375 ------------------------
5376 -- Interrupt_Priority --
5377 ------------------------
5378
5379 when Attribute_Interrupt_Priority => Interrupt_Priority :
5380 begin
5381 -- Interrupt_Priority attribute definition clause not allowed
5382 -- except from aspect specification.
5383
5384 if From_Aspect_Specification (N) then
f02a9a9a 5385 if not Is_Concurrent_Type (U_Ent) then
89f1e35c 5386 Error_Msg_N
f74a102b 5387 ("Interrupt_Priority can only be defined for task and "
5388 & "protected object", Nam);
89f1e35c 5389
5390 elsif Duplicate_Clause then
5391 null;
5392
5393 else
5394 -- The expression must be analyzed in the special manner
5395 -- described in "Handling of Default and Per-Object
5396 -- Expressions" in sem.ads.
5397
5398 -- The visibility to the discriminants must be restored
5399
5400 Push_Scope_And_Install_Discriminants (U_Ent);
5401
5402 Preanalyze_Spec_Expression
5403 (Expr, RTE (RE_Interrupt_Priority));
5404
5405 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
d4e1acfa 5406
5407 -- Check the No_Task_At_Interrupt_Priority restriction
5408
5409 if Is_Task_Type (U_Ent) then
5410 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5411 end if;
89f1e35c 5412 end if;
5413
5414 else
5415 Error_Msg_N
5416 ("attribute& cannot be set with definition clause", N);
5417 end if;
5418 end Interrupt_Priority;
5419
b3f8228a 5420 --------------
5421 -- Iterable --
5422 --------------
5423
5424 when Attribute_Iterable =>
5425 Analyze (Expr);
bde03454 5426
b3f8228a 5427 if Nkind (Expr) /= N_Aggregate then
5428 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5429 end if;
5430
5431 declare
5432 Assoc : Node_Id;
5433
5434 begin
5435 Assoc := First (Component_Associations (Expr));
5436 while Present (Assoc) loop
5437 if not Is_Entity_Name (Expression (Assoc)) then
5438 Error_Msg_N ("value must be a function", Assoc);
5439 end if;
bde03454 5440
b3f8228a 5441 Next (Assoc);
5442 end loop;
5443 end;
5444
89cc7147 5445 ----------------------
5446 -- Iterator_Element --
5447 ----------------------
5448
5449 when Attribute_Iterator_Element =>
5450 Analyze (Expr);
5451
5452 if not Is_Entity_Name (Expr)
5453 or else not Is_Type (Entity (Expr))
5454 then
5455 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5456 end if;
5457
d6f39728 5458 -------------------
5459 -- Machine_Radix --
5460 -------------------
5461
5462 -- Machine radix attribute definition clause
5463
5464 when Attribute_Machine_Radix => Machine_Radix : declare
5465 Radix : constant Uint := Static_Integer (Expr);
5466
5467 begin
5468 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5469 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5470
ae888dbd 5471 elsif Duplicate_Clause then
5472 null;
d6f39728 5473
5474 elsif Radix /= No_Uint then
5475 Set_Has_Machine_Radix_Clause (U_Ent);
5476 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5477
5478 if Radix = 2 then
5479 null;
f74a102b 5480
d6f39728 5481 elsif Radix = 10 then
5482 Set_Machine_Radix_10 (U_Ent);
f74a102b 5483
5484 -- The following error is suppressed in ASIS mode to allow for
f9906591 5485 -- different ASIS back ends or ASIS-based tools to query the
f74a102b 5486 -- illegal clause.
5487
5488 elsif not ASIS_Mode then
d6f39728 5489 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5490 end if;
5491 end if;
5492 end Machine_Radix;
5493
5494 -----------------
5495 -- Object_Size --
5496 -----------------
5497
5498 -- Object_Size attribute definition clause
5499
5500 when Attribute_Object_Size => Object_Size : declare
bfa5a9d9 5501 Size : constant Uint := Static_Integer (Expr);
5502
d6f39728 5503 Biased : Boolean;
bfa5a9d9 5504 pragma Warnings (Off, Biased);
d6f39728 5505
5506 begin
5507 if not Is_Type (U_Ent) then
5508 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5509
ae888dbd 5510 elsif Duplicate_Clause then
5511 null;
d6f39728 5512
5513 else
5514 Check_Size (Expr, U_Ent, Size, Biased);
5515
f74a102b 5516 -- The following errors are suppressed in ASIS mode to allow
f9906591 5517 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 5518 -- the illegal clause.
5519
5520 if ASIS_Mode then
5521 null;
5522
5523 elsif Is_Scalar_Type (U_Ent) then
829cd457 5524 if Size /= 8 and then Size /= 16 and then Size /= 32
5525 and then UI_Mod (Size, 64) /= 0
5526 then
5527 Error_Msg_N
5528 ("Object_Size must be 8, 16, 32, or multiple of 64",
5529 Expr);
5530 end if;
5531
5532 elsif Size mod 8 /= 0 then
5533 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
d6f39728 5534 end if;
5535
5536 Set_Esize (U_Ent, Size);
5537 Set_Has_Object_Size_Clause (U_Ent);
1d366b32 5538 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5539 end if;
5540 end Object_Size;
5541
5542 ------------
5543 -- Output --
5544 ------------
5545
9f373bb8 5546 when Attribute_Output =>
5547 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5548 Set_Has_Specified_Stream_Output (Ent);
d6f39728 5549
89f1e35c 5550 --------------
5551 -- Priority --
5552 --------------
5553
5554 when Attribute_Priority => Priority :
5555 begin
5556 -- Priority attribute definition clause not allowed except from
5557 -- aspect specification.
5558
5559 if From_Aspect_Specification (N) then
f02a9a9a 5560 if not (Is_Concurrent_Type (U_Ent)
3a72f9c3 5561 or else Ekind (U_Ent) = E_Procedure)
89f1e35c 5562 then
5563 Error_Msg_N
f02a9a9a 5564 ("Priority can only be defined for task and protected "
5565 & "object", Nam);
89f1e35c 5566
5567 elsif Duplicate_Clause then
5568 null;
5569
5570 else
5571 -- The expression must be analyzed in the special manner
5572 -- described in "Handling of Default and Per-Object
5573 -- Expressions" in sem.ads.
5574
5575 -- The visibility to the discriminants must be restored
5576
5577 Push_Scope_And_Install_Discriminants (U_Ent);
5578 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5579 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5580
cda40848 5581 if not Is_OK_Static_Expression (Expr) then
89f1e35c 5582 Check_Restriction (Static_Priorities, Expr);
5583 end if;
5584 end if;
5585
5586 else
5587 Error_Msg_N
5588 ("attribute& cannot be set with definition clause", N);
5589 end if;
5590 end Priority;
5591
d6f39728 5592 ----------
5593 -- Read --
5594 ----------
5595
9f373bb8 5596 when Attribute_Read =>
5597 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5598 Set_Has_Specified_Stream_Read (Ent);
d6f39728 5599
b7b74740 5600 --------------------------
5601 -- Scalar_Storage_Order --
5602 --------------------------
5603
5604 -- Scalar_Storage_Order attribute definition clause
5605
5606 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5607 begin
b43a5770 5608 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
b7b74740 5609 Error_Msg_N
f74a102b 5610 ("Scalar_Storage_Order can only be defined for record or "
5611 & "array type", Nam);
b7b74740 5612
5613 elsif Duplicate_Clause then
5614 null;
5615
5616 else
5617 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5618
5619 if Etype (Expr) = Any_Type then
5620 return;
5621
cda40848 5622 elsif not Is_OK_Static_Expression (Expr) then
b7b74740 5623 Flag_Non_Static_Expr
5624 ("Scalar_Storage_Order requires static expression!", Expr);
5625
c0912570 5626 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5627
5628 -- Here for the case of a non-default (i.e. non-confirming)
5629 -- Scalar_Storage_Order attribute definition.
5630
5631 if Support_Nondefault_SSO_On_Target then
d0a9ea3b 5632 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
c0912570 5633 else
5634 Error_Msg_N
f74a102b 5635 ("non-default Scalar_Storage_Order not supported on "
5636 & "target", Expr);
b7b74740 5637 end if;
5638 end if;
b64082f2 5639
5640 -- Clear SSO default indications since explicit setting of the
5641 -- order overrides the defaults.
5642
5643 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5644 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
b7b74740 5645 end if;
5646 end Scalar_Storage_Order;
5647
d6f39728 5648 ----------
5649 -- Size --
5650 ----------
5651
5652 -- Size attribute definition clause
5653
5654 when Attribute_Size => Size : declare
5655 Size : constant Uint := Static_Integer (Expr);
5656 Etyp : Entity_Id;
5657 Biased : Boolean;
5658
5659 begin
5660 FOnly := True;
5661
ae888dbd 5662 if Duplicate_Clause then
5663 null;
d6f39728 5664
5665 elsif not Is_Type (U_Ent)
5666 and then Ekind (U_Ent) /= E_Variable
5667 and then Ekind (U_Ent) /= E_Constant
5668 then
5669 Error_Msg_N ("size cannot be given for &", Nam);
5670
5671 elsif Is_Array_Type (U_Ent)
5672 and then not Is_Constrained (U_Ent)
5673 then
5674 Error_Msg_N
5675 ("size cannot be given for unconstrained array", Nam);
5676
c2b89d6e 5677 elsif Size /= No_Uint then
d6f39728 5678 if Is_Type (U_Ent) then
5679 Etyp := U_Ent;
5680 else
5681 Etyp := Etype (U_Ent);
5682 end if;
5683
59ac57b5 5684 -- Check size, note that Gigi is in charge of checking that the
5685 -- size of an array or record type is OK. Also we do not check
5686 -- the size in the ordinary fixed-point case, since it is too
5687 -- early to do so (there may be subsequent small clause that
5688 -- affects the size). We can check the size if a small clause
5689 -- has already been given.
d6f39728 5690
5691 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5692 or else Has_Small_Clause (U_Ent)
5693 then
5694 Check_Size (Expr, Etyp, Size, Biased);
b77e4501 5695 Set_Biased (U_Ent, N, "size clause", Biased);
d6f39728 5696 end if;
5697
5698 -- For types set RM_Size and Esize if possible
5699
5700 if Is_Type (U_Ent) then
5701 Set_RM_Size (U_Ent, Size);
5702
ada34def 5703 -- For elementary types, increase Object_Size to power of 2,
5704 -- but not less than a storage unit in any case (normally
59ac57b5 5705 -- this means it will be byte addressable).
d6f39728 5706
ada34def 5707 -- For all other types, nothing else to do, we leave Esize
5708 -- (object size) unset, the back end will set it from the
5709 -- size and alignment in an appropriate manner.
5710
1d366b32 5711 -- In both cases, we check whether the alignment must be
5712 -- reset in the wake of the size change.
5713
ada34def 5714 if Is_Elementary_Type (U_Ent) then
f15731c4 5715 if Size <= System_Storage_Unit then
5716 Init_Esize (U_Ent, System_Storage_Unit);
d6f39728 5717 elsif Size <= 16 then
5718 Init_Esize (U_Ent, 16);
5719 elsif Size <= 32 then
5720 Init_Esize (U_Ent, 32);
5721 else
5722 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5723 end if;
5724
1d366b32 5725 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5726 else
5727 Alignment_Check_For_Size_Change (U_Ent, Size);
d6f39728 5728 end if;
5729
d6f39728 5730 -- For objects, set Esize only
5731
5732 else
f74a102b 5733 -- The following error is suppressed in ASIS mode to allow
f9906591 5734 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 5735 -- the illegal clause.
5736
5737 if Is_Elementary_Type (Etyp)
5738 and then Size /= System_Storage_Unit
5739 and then Size /= System_Storage_Unit * 2
5740 and then Size /= System_Storage_Unit * 4
5741 and then Size /= System_Storage_Unit * 8
5742 and then not ASIS_Mode
5743 then
5744 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5745 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5746 Error_Msg_N
5747 ("size for primitive object must be a power of 2 in "
5748 & "the range ^-^", N);
9dfe12ae 5749 end if;
5750
d6f39728 5751 Set_Esize (U_Ent, Size);
5752 end if;
5753
5754 Set_Has_Size_Clause (U_Ent);
5755 end if;
5756 end Size;
5757
5758 -----------
5759 -- Small --
5760 -----------
5761
5762 -- Small attribute definition clause
5763
5764 when Attribute_Small => Small : declare
5765 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5766 Small : Ureal;
5767
5768 begin
5769 Analyze_And_Resolve (Expr, Any_Real);
5770
5771 if Etype (Expr) = Any_Type then
5772 return;
5773
cda40848 5774 elsif not Is_OK_Static_Expression (Expr) then
9dfe12ae 5775 Flag_Non_Static_Expr
5776 ("small requires static expression!", Expr);
d6f39728 5777 return;
5778
5779 else
5780 Small := Expr_Value_R (Expr);
5781
5782 if Small <= Ureal_0 then
5783 Error_Msg_N ("small value must be greater than zero", Expr);
5784 return;
5785 end if;
5786
5787 end if;
5788
5789 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5790 Error_Msg_N
5791 ("small requires an ordinary fixed point type", Nam);
5792
5793 elsif Has_Small_Clause (U_Ent) then
5794 Error_Msg_N ("small already given for &", Nam);
5795
5796 elsif Small > Delta_Value (U_Ent) then
5797 Error_Msg_N
ce3e25d6 5798 ("small value must not be greater than delta value", Nam);
d6f39728 5799
5800 else
5801 Set_Small_Value (U_Ent, Small);
5802 Set_Small_Value (Implicit_Base, Small);
5803 Set_Has_Small_Clause (U_Ent);
5804 Set_Has_Small_Clause (Implicit_Base);
5805 Set_Has_Non_Standard_Rep (Implicit_Base);
5806 end if;
5807 end Small;
5808
d6f39728 5809 ------------------
5810 -- Storage_Pool --
5811 ------------------
5812
5813 -- Storage_Pool attribute definition clause
5814
b55f7641 5815 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
d6f39728 5816 Pool : Entity_Id;
6b567c71 5817 T : Entity_Id;
d6f39728 5818
5819 begin
44e4341e 5820 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5821 Error_Msg_N
5822 ("storage pool cannot be given for access-to-subprogram type",
5823 Nam);
5824 return;
5825
d3ef794c 5826 elsif not
5827 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
d6f39728 5828 then
44e4341e 5829 Error_Msg_N
5830 ("storage pool can only be given for access types", Nam);
d6f39728 5831 return;
5832
5833 elsif Is_Derived_Type (U_Ent) then
5834 Error_Msg_N
5835 ("storage pool cannot be given for a derived access type",
5836 Nam);
5837
ae888dbd 5838 elsif Duplicate_Clause then
d6f39728 5839 return;
5840
5841 elsif Present (Associated_Storage_Pool (U_Ent)) then
5842 Error_Msg_N ("storage pool already given for &", Nam);
5843 return;
5844 end if;
5845
6653b695 5846 -- Check for Storage_Size previously given
5847
5848 declare
5849 SS : constant Node_Id :=
5850 Get_Attribute_Definition_Clause
5851 (U_Ent, Attribute_Storage_Size);
5852 begin
5853 if Present (SS) then
5854 Check_Pool_Size_Clash (U_Ent, N, SS);
5855 end if;
5856 end;
5857
5858 -- Storage_Pool case
5859
b55f7641 5860 if Id = Attribute_Storage_Pool then
5861 Analyze_And_Resolve
5862 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5863
5864 -- In the Simple_Storage_Pool case, we allow a variable of any
b15003c3 5865 -- simple storage pool type, so we Resolve without imposing an
b55f7641 5866 -- expected type.
5867
5868 else
5869 Analyze_And_Resolve (Expr);
5870
5871 if not Present (Get_Rep_Pragma
b15003c3 5872 (Etype (Expr), Name_Simple_Storage_Pool_Type))
b55f7641 5873 then
5874 Error_Msg_N
5875 ("expression must be of a simple storage pool type", Expr);
5876 end if;
5877 end if;
d6f39728 5878
8c5c7277 5879 if not Denotes_Variable (Expr) then
5880 Error_Msg_N ("storage pool must be a variable", Expr);
5881 return;
5882 end if;
5883
6b567c71 5884 if Nkind (Expr) = N_Type_Conversion then
5885 T := Etype (Expression (Expr));
5886 else
5887 T := Etype (Expr);
5888 end if;
5889
5890 -- The Stack_Bounded_Pool is used internally for implementing
d64221a7 5891 -- access types with a Storage_Size. Since it only work properly
5892 -- when used on one specific type, we need to check that it is not
5893 -- hijacked improperly:
5894
6b567c71 5895 -- type T is access Integer;
5896 -- for T'Storage_Size use n;
5897 -- type Q is access Float;
5898 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5899
15ebb600 5900 if RTE_Available (RE_Stack_Bounded_Pool)
5901 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5902 then
5903 Error_Msg_N ("non-shareable internal Pool", Expr);
6b567c71 5904 return;
5905 end if;
5906
d6f39728 5907 -- If the argument is a name that is not an entity name, then
5908 -- we construct a renaming operation to define an entity of
5909 -- type storage pool.
5910
5911 if not Is_Entity_Name (Expr)
5912 and then Is_Object_Reference (Expr)
5913 then
11deeeb6 5914 Pool := Make_Temporary (Loc, 'P', Expr);
d6f39728 5915
5916 declare
5917 Rnode : constant Node_Id :=
5918 Make_Object_Renaming_Declaration (Loc,
5919 Defining_Identifier => Pool,
5920 Subtype_Mark =>
5921 New_Occurrence_Of (Etype (Expr), Loc),
11deeeb6 5922 Name => Expr);
d6f39728 5923
5924 begin
f65f7fdf 5925 -- If the attribute definition clause comes from an aspect
5926 -- clause, then insert the renaming before the associated
5927 -- entity's declaration, since the attribute clause has
5928 -- not yet been appended to the declaration list.
5929
5930 if From_Aspect_Specification (N) then
5931 Insert_Before (Parent (Entity (N)), Rnode);
5932 else
5933 Insert_Before (N, Rnode);
5934 end if;
5935
d6f39728 5936 Analyze (Rnode);
5937 Set_Associated_Storage_Pool (U_Ent, Pool);
5938 end;
5939
5940 elsif Is_Entity_Name (Expr) then
5941 Pool := Entity (Expr);
5942
5943 -- If pool is a renamed object, get original one. This can
5944 -- happen with an explicit renaming, and within instances.
5945
5946 while Present (Renamed_Object (Pool))
5947 and then Is_Entity_Name (Renamed_Object (Pool))
5948 loop
5949 Pool := Entity (Renamed_Object (Pool));
5950 end loop;
5951
5952 if Present (Renamed_Object (Pool))
5953 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5954 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5955 then
5956 Pool := Entity (Expression (Renamed_Object (Pool)));
5957 end if;
5958
6b567c71 5959 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 5960
5961 elsif Nkind (Expr) = N_Type_Conversion
5962 and then Is_Entity_Name (Expression (Expr))
5963 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5964 then
5965 Pool := Entity (Expression (Expr));
6b567c71 5966 Set_Associated_Storage_Pool (U_Ent, Pool);
d6f39728 5967
5968 else
5969 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5970 return;
5971 end if;
b55f7641 5972 end;
d6f39728 5973
44e4341e 5974 ------------------
5975 -- Storage_Size --
5976 ------------------
5977
5978 -- Storage_Size attribute definition clause
5979
5980 when Attribute_Storage_Size => Storage_Size : declare
5981 Btype : constant Entity_Id := Base_Type (U_Ent);
44e4341e 5982
5983 begin
5984 if Is_Task_Type (U_Ent) then
44e4341e 5985
39a0c1d3 5986 -- Check obsolescent (but never obsolescent if from aspect)
ceec4f7c 5987
5988 if not From_Aspect_Specification (N) then
5989 Check_Restriction (No_Obsolescent_Features, N);
5990
5991 if Warn_On_Obsolescent_Feature then
5992 Error_Msg_N
f74a102b 5993 ("?j?storage size clause for task is an obsolescent "
5994 & "feature (RM J.9)", N);
ceec4f7c 5995 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5996 end if;
44e4341e 5997 end if;
5998
5999 FOnly := True;
6000 end if;
6001
6002 if not Is_Access_Type (U_Ent)
6003 and then Ekind (U_Ent) /= E_Task_Type
6004 then
6005 Error_Msg_N ("storage size cannot be given for &", Nam);
6006
6007 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
6008 Error_Msg_N
6009 ("storage size cannot be given for a derived access type",
6010 Nam);
6011
ae888dbd 6012 elsif Duplicate_Clause then
6013 null;
44e4341e 6014
6015 else
6016 Analyze_And_Resolve (Expr, Any_Integer);
6017
6018 if Is_Access_Type (U_Ent) then
6653b695 6019
6020 -- Check for Storage_Pool previously given
6021
6022 declare
6023 SP : constant Node_Id :=
6024 Get_Attribute_Definition_Clause
6025 (U_Ent, Attribute_Storage_Pool);
6026
6027 begin
6028 if Present (SP) then
6029 Check_Pool_Size_Clash (U_Ent, SP, N);
6030 end if;
6031 end;
6032
6033 -- Special case of for x'Storage_Size use 0
44e4341e 6034
5941a4e9 6035 if Is_OK_Static_Expression (Expr)
44e4341e 6036 and then Expr_Value (Expr) = 0
6037 then
6038 Set_No_Pool_Assigned (Btype);
6039 end if;
44e4341e 6040 end if;
6041
6042 Set_Has_Storage_Size_Clause (Btype);
6043 end if;
6044 end Storage_Size;
6045
7189d17f 6046 -----------------
6047 -- Stream_Size --
6048 -----------------
6049
6050 when Attribute_Stream_Size => Stream_Size : declare
6051 Size : constant Uint := Static_Integer (Expr);
6052
6053 begin
15ebb600 6054 if Ada_Version <= Ada_95 then
6055 Check_Restriction (No_Implementation_Attributes, N);
6056 end if;
6057
ae888dbd 6058 if Duplicate_Clause then
6059 null;
7189d17f 6060
6061 elsif Is_Elementary_Type (U_Ent) then
f74a102b 6062
6063 -- The following errors are suppressed in ASIS mode to allow
f9906591 6064 -- for different ASIS back ends or ASIS-based tools to query
f74a102b 6065 -- the illegal clause.
6066
6067 if ASIS_Mode then
6068 null;
6069
6070 elsif Size /= System_Storage_Unit
6071 and then Size /= System_Storage_Unit * 2
6072 and then Size /= System_Storage_Unit * 4
6073 and then Size /= System_Storage_Unit * 8
7189d17f 6074 then
6075 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6076 Error_Msg_N
f74a102b 6077 ("stream size for elementary type must be a power of 2 "
6078 & "and at least ^", N);
7189d17f 6079
6080 elsif RM_Size (U_Ent) > Size then
6081 Error_Msg_Uint_1 := RM_Size (U_Ent);
6082 Error_Msg_N
f74a102b 6083 ("stream size for elementary type must be a power of 2 "
6084 & "and at least ^", N);
7189d17f 6085 end if;
6086
6087 Set_Has_Stream_Size_Clause (U_Ent);
6088
6089 else
6090 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6091 end if;
6092 end Stream_Size;
6093
d6f39728 6094 ----------------
6095 -- Value_Size --
6096 ----------------
6097
6098 -- Value_Size attribute definition clause
6099
6100 when Attribute_Value_Size => Value_Size : declare
6101 Size : constant Uint := Static_Integer (Expr);
6102 Biased : Boolean;
6103
6104 begin
6105 if not Is_Type (U_Ent) then
6106 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6107
ae888dbd 6108 elsif Duplicate_Clause then
6109 null;
d6f39728 6110
59ac57b5 6111 elsif Is_Array_Type (U_Ent)
6112 and then not Is_Constrained (U_Ent)
6113 then
6114 Error_Msg_N
6115 ("Value_Size cannot be given for unconstrained array", Nam);
6116
d6f39728 6117 else
6118 if Is_Elementary_Type (U_Ent) then
6119 Check_Size (Expr, U_Ent, Size, Biased);
b77e4501 6120 Set_Biased (U_Ent, N, "value size clause", Biased);
d6f39728 6121 end if;
6122
6123 Set_RM_Size (U_Ent, Size);
6124 end if;
6125 end Value_Size;
6126
81b424ac 6127 -----------------------
6128 -- Variable_Indexing --
6129 -----------------------
6130
6131 when Attribute_Variable_Indexing =>
6132 Check_Indexing_Functions;
6133
d6f39728 6134 -----------
6135 -- Write --
6136 -----------
6137
9f373bb8 6138 when Attribute_Write =>
6139 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6140 Set_Has_Specified_Stream_Write (Ent);
d6f39728 6141
6142 -- All other attributes cannot be set
6143
6144 when others =>
6145 Error_Msg_N
6146 ("attribute& cannot be set with definition clause", N);
d6f39728 6147 end case;
6148
d64221a7 6149 -- The test for the type being frozen must be performed after any
6150 -- expression the clause has been analyzed since the expression itself
6151 -- might cause freezing that makes the clause illegal.
d6f39728 6152
6153 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6154 return;
6155 end if;
6156 end Analyze_Attribute_Definition_Clause;
6157
6158 ----------------------------
6159 -- Analyze_Code_Statement --
6160 ----------------------------
6161
6162 procedure Analyze_Code_Statement (N : Node_Id) is
6163 HSS : constant Node_Id := Parent (N);
6164 SBody : constant Node_Id := Parent (HSS);
6165 Subp : constant Entity_Id := Current_Scope;
6166 Stmt : Node_Id;
6167 Decl : Node_Id;
6168 StmtO : Node_Id;
6169 DeclO : Node_Id;
6170
6171 begin
1d3f0c6b 6172 -- Accept foreign code statements for CodePeer. The analysis is skipped
6173 -- to avoid rejecting unrecognized constructs.
6174
6175 if CodePeer_Mode then
6176 Set_Analyzed (N);
6177 return;
6178 end if;
6179
d6f39728 6180 -- Analyze and check we get right type, note that this implements the
1d3f0c6b 6181 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6182 -- the only way that Asm_Insn could possibly be visible.
d6f39728 6183
6184 Analyze_And_Resolve (Expression (N));
6185
6186 if Etype (Expression (N)) = Any_Type then
6187 return;
6188 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6189 Error_Msg_N ("incorrect type for code statement", N);
6190 return;
6191 end if;
6192
44e4341e 6193 Check_Code_Statement (N);
6194
1d3f0c6b 6195 -- Make sure we appear in the handled statement sequence of a subprogram
6196 -- (RM 13.8(3)).
d6f39728 6197
6198 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6199 or else Nkind (SBody) /= N_Subprogram_Body
6200 then
6201 Error_Msg_N
6202 ("code statement can only appear in body of subprogram", N);
6203 return;
6204 end if;
6205
6206 -- Do remaining checks (RM 13.8(3)) if not already done
6207
6208 if not Is_Machine_Code_Subprogram (Subp) then
6209 Set_Is_Machine_Code_Subprogram (Subp);
6210
6211 -- No exception handlers allowed
6212
6213 if Present (Exception_Handlers (HSS)) then
6214 Error_Msg_N
6215 ("exception handlers not permitted in machine code subprogram",
6216 First (Exception_Handlers (HSS)));
6217 end if;
6218
6219 -- No declarations other than use clauses and pragmas (we allow
6220 -- certain internally generated declarations as well).
6221
6222 Decl := First (Declarations (SBody));
6223 while Present (Decl) loop
6224 DeclO := Original_Node (Decl);
6225 if Comes_From_Source (DeclO)
fdd294d1 6226 and not Nkind_In (DeclO, N_Pragma,
6227 N_Use_Package_Clause,
6228 N_Use_Type_Clause,
6229 N_Implicit_Label_Declaration)
d6f39728 6230 then
6231 Error_Msg_N
6232 ("this declaration not allowed in machine code subprogram",
6233 DeclO);
6234 end if;
6235
6236 Next (Decl);
6237 end loop;
6238
6239 -- No statements other than code statements, pragmas, and labels.
6240 -- Again we allow certain internally generated statements.
3ab42ff7 6241
c3107527 6242 -- In Ada 2012, qualified expressions are names, and the code
6243 -- statement is initially parsed as a procedure call.
d6f39728 6244
6245 Stmt := First (Statements (HSS));
6246 while Present (Stmt) loop
6247 StmtO := Original_Node (Stmt);
c3107527 6248
1d3f0c6b 6249 -- A procedure call transformed into a code statement is OK
59f2fcab 6250
c3107527 6251 if Ada_Version >= Ada_2012
6252 and then Nkind (StmtO) = N_Procedure_Call_Statement
59f2fcab 6253 and then Nkind (Name (StmtO)) = N_Qualified_Expression
c3107527 6254 then
6255 null;
6256
6257 elsif Comes_From_Source (StmtO)
fdd294d1 6258 and then not Nkind_In (StmtO, N_Pragma,
6259 N_Label,
6260 N_Code_Statement)
d6f39728 6261 then
6262 Error_Msg_N
6263 ("this statement is not allowed in machine code subprogram",
6264 StmtO);
6265 end if;
6266
6267 Next (Stmt);
6268 end loop;
6269 end if;
d6f39728 6270 end Analyze_Code_Statement;
6271
6272 -----------------------------------------------
6273 -- Analyze_Enumeration_Representation_Clause --
6274 -----------------------------------------------
6275
6276 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
21647c2d 6277 Ident : constant Node_Id := Identifier (N);
6278 Aggr : constant Node_Id := Array_Aggregate (N);
d6f39728 6279 Enumtype : Entity_Id;
6280 Elit : Entity_Id;
6281 Expr : Node_Id;
6282 Assoc : Node_Id;
6283 Choice : Node_Id;
6284 Val : Uint;
b3190af0 6285
6286 Err : Boolean := False;
098d3082 6287 -- Set True to avoid cascade errors and crashes on incorrect source code
d6f39728 6288
e30c7d84 6289 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6290 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6291 -- Allowed range of universal integer (= allowed range of enum lit vals)
6292
d6f39728 6293 Min : Uint;
6294 Max : Uint;
e30c7d84 6295 -- Minimum and maximum values of entries
6296
6297 Max_Node : Node_Id;
6298 -- Pointer to node for literal providing max value
d6f39728 6299
6300 begin
ca301e17 6301 if Ignore_Rep_Clauses then
2ff55065 6302 Kill_Rep_Clause (N);
fbc67f84 6303 return;
6304 end if;
6305
175a6969 6306 -- Ignore enumeration rep clauses by default in CodePeer mode,
6307 -- unless -gnatd.I is specified, as a work around for potential false
6308 -- positive messages.
6309
6310 if CodePeer_Mode and not Debug_Flag_Dot_II then
6311 return;
6312 end if;
6313
d6f39728 6314 -- First some basic error checks
6315
6316 Find_Type (Ident);
6317 Enumtype := Entity (Ident);
6318
6319 if Enumtype = Any_Type
6320 or else Rep_Item_Too_Early (Enumtype, N)
6321 then
6322 return;
6323 else
6324 Enumtype := Underlying_Type (Enumtype);
6325 end if;
6326
6327 if not Is_Enumeration_Type (Enumtype) then
6328 Error_Msg_NE
6329 ("enumeration type required, found}",
6330 Ident, First_Subtype (Enumtype));
6331 return;
6332 end if;
6333
9dfe12ae 6334 -- Ignore rep clause on generic actual type. This will already have
6335 -- been flagged on the template as an error, and this is the safest
6336 -- way to ensure we don't get a junk cascaded message in the instance.
6337
6338 if Is_Generic_Actual_Type (Enumtype) then
6339 return;
6340
6341 -- Type must be in current scope
6342
6343 elsif Scope (Enumtype) /= Current_Scope then
d6f39728 6344 Error_Msg_N ("type must be declared in this scope", Ident);
6345 return;
6346
9dfe12ae 6347 -- Type must be a first subtype
6348
d6f39728 6349 elsif not Is_First_Subtype (Enumtype) then
6350 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6351 return;
6352
9dfe12ae 6353 -- Ignore duplicate rep clause
6354
d6f39728 6355 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6356 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6357 return;
6358
7189d17f 6359 -- Don't allow rep clause for standard [wide_[wide_]]character
9dfe12ae 6360
177675a7 6361 elsif Is_Standard_Character_Type (Enumtype) then
d6f39728 6362 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
9dfe12ae 6363 return;
6364
d9125581 6365 -- Check that the expression is a proper aggregate (no parentheses)
6366
6367 elsif Paren_Count (Aggr) /= 0 then
6368 Error_Msg
6369 ("extra parentheses surrounding aggregate not allowed",
6370 First_Sloc (Aggr));
6371 return;
6372
9dfe12ae 6373 -- All tests passed, so set rep clause in place
d6f39728 6374
6375 else
6376 Set_Has_Enumeration_Rep_Clause (Enumtype);
6377 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6378 end if;
6379
6380 -- Now we process the aggregate. Note that we don't use the normal
6381 -- aggregate code for this purpose, because we don't want any of the
6382 -- normal expansion activities, and a number of special semantic
6383 -- rules apply (including the component type being any integer type)
6384
d6f39728 6385 Elit := First_Literal (Enumtype);
6386
6387 -- First the positional entries if any
6388
6389 if Present (Expressions (Aggr)) then
6390 Expr := First (Expressions (Aggr));
6391 while Present (Expr) loop
6392 if No (Elit) then
6393 Error_Msg_N ("too many entries in aggregate", Expr);
6394 return;
6395 end if;
6396
6397 Val := Static_Integer (Expr);
6398
d9125581 6399 -- Err signals that we found some incorrect entries processing
6400 -- the list. The final checks for completeness and ordering are
6401 -- skipped in this case.
6402
d6f39728 6403 if Val = No_Uint then
6404 Err := True;
f02a9a9a 6405
d6f39728 6406 elsif Val < Lo or else Hi < Val then
6407 Error_Msg_N ("value outside permitted range", Expr);
6408 Err := True;
6409 end if;
6410
6411 Set_Enumeration_Rep (Elit, Val);
6412 Set_Enumeration_Rep_Expr (Elit, Expr);
6413 Next (Expr);
6414 Next (Elit);
6415 end loop;
6416 end if;
6417
6418 -- Now process the named entries if present
6419
6420 if Present (Component_Associations (Aggr)) then
6421 Assoc := First (Component_Associations (Aggr));
6422 while Present (Assoc) loop
6423 Choice := First (Choices (Assoc));
6424
6425 if Present (Next (Choice)) then
6426 Error_Msg_N
6427 ("multiple choice not allowed here", Next (Choice));
6428 Err := True;
6429 end if;
6430
6431 if Nkind (Choice) = N_Others_Choice then
6432 Error_Msg_N ("others choice not allowed here", Choice);
6433 Err := True;
6434
6435 elsif Nkind (Choice) = N_Range then
b3190af0 6436
d6f39728 6437 -- ??? should allow zero/one element range here
b3190af0 6438
d6f39728 6439 Error_Msg_N ("range not allowed here", Choice);
6440 Err := True;
6441
6442 else
6443 Analyze_And_Resolve (Choice, Enumtype);
b3190af0 6444
098d3082 6445 if Error_Posted (Choice) then
d6f39728 6446 Err := True;
098d3082 6447 end if;
d6f39728 6448
098d3082 6449 if not Err then
6450 if Is_Entity_Name (Choice)
6451 and then Is_Type (Entity (Choice))
6452 then
6453 Error_Msg_N ("subtype name not allowed here", Choice);
d6f39728 6454 Err := True;
b3190af0 6455
098d3082 6456 -- ??? should allow static subtype with zero/one entry
d6f39728 6457
098d3082 6458 elsif Etype (Choice) = Base_Type (Enumtype) then
cda40848 6459 if not Is_OK_Static_Expression (Choice) then
098d3082 6460 Flag_Non_Static_Expr
6461 ("non-static expression used for choice!", Choice);
d6f39728 6462 Err := True;
d6f39728 6463
098d3082 6464 else
6465 Elit := Expr_Value_E (Choice);
6466
6467 if Present (Enumeration_Rep_Expr (Elit)) then
6468 Error_Msg_Sloc :=
6469 Sloc (Enumeration_Rep_Expr (Elit));
6470 Error_Msg_NE
6471 ("representation for& previously given#",
6472 Choice, Elit);
6473 Err := True;
6474 end if;
d6f39728 6475
098d3082 6476 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
d6f39728 6477
098d3082 6478 Expr := Expression (Assoc);
6479 Val := Static_Integer (Expr);
d6f39728 6480
098d3082 6481 if Val = No_Uint then
6482 Err := True;
6483
6484 elsif Val < Lo or else Hi < Val then
6485 Error_Msg_N ("value outside permitted range", Expr);
6486 Err := True;
6487 end if;
d6f39728 6488
098d3082 6489 Set_Enumeration_Rep (Elit, Val);
6490 end if;
d6f39728 6491 end if;
6492 end if;
6493 end if;
6494
6495 Next (Assoc);
6496 end loop;
6497 end if;
6498
6499 -- Aggregate is fully processed. Now we check that a full set of
6500 -- representations was given, and that they are in range and in order.
6501 -- These checks are only done if no other errors occurred.
6502
6503 if not Err then
6504 Min := No_Uint;
6505 Max := No_Uint;
6506
6507 Elit := First_Literal (Enumtype);
6508 while Present (Elit) loop
6509 if No (Enumeration_Rep_Expr (Elit)) then
6510 Error_Msg_NE ("missing representation for&!", N, Elit);
6511
6512 else
6513 Val := Enumeration_Rep (Elit);
6514
6515 if Min = No_Uint then
6516 Min := Val;
6517 end if;
6518
6519 if Val /= No_Uint then
6520 if Max /= No_Uint and then Val <= Max then
6521 Error_Msg_NE
6522 ("enumeration value for& not ordered!",
e30c7d84 6523 Enumeration_Rep_Expr (Elit), Elit);
d6f39728 6524 end if;
6525
e30c7d84 6526 Max_Node := Enumeration_Rep_Expr (Elit);
d6f39728 6527 Max := Val;
6528 end if;
6529
e30c7d84 6530 -- If there is at least one literal whose representation is not
6531 -- equal to the Pos value, then note that this enumeration type
6532 -- has a non-standard representation.
d6f39728 6533
6534 if Val /= Enumeration_Pos (Elit) then
6535 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6536 end if;
6537 end if;
6538
6539 Next (Elit);
6540 end loop;
6541
6542 -- Now set proper size information
6543
6544 declare
6545 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6546
6547 begin
6548 if Has_Size_Clause (Enumtype) then
e30c7d84 6549
6550 -- All OK, if size is OK now
6551
6552 if RM_Size (Enumtype) >= Minsize then
d6f39728 6553 null;
6554
6555 else
e30c7d84 6556 -- Try if we can get by with biasing
6557
d6f39728 6558 Minsize :=
6559 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6560
e30c7d84 6561 -- Error message if even biasing does not work
6562
6563 if RM_Size (Enumtype) < Minsize then
6564 Error_Msg_Uint_1 := RM_Size (Enumtype);
6565 Error_Msg_Uint_2 := Max;
6566 Error_Msg_N
6567 ("previously given size (^) is too small "
6568 & "for this value (^)", Max_Node);
6569
6570 -- If biasing worked, indicate that we now have biased rep
d6f39728 6571
6572 else
b77e4501 6573 Set_Biased
6574 (Enumtype, Size_Clause (Enumtype), "size clause");
d6f39728 6575 end if;
6576 end if;
6577
6578 else
6579 Set_RM_Size (Enumtype, Minsize);
6580 Set_Enum_Esize (Enumtype);
6581 end if;
6582
6583 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6584 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6585 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6586 end;
6587 end if;
6588
39a0c1d3 6589 -- We repeat the too late test in case it froze itself
d6f39728 6590
6591 if Rep_Item_Too_Late (Enumtype, N) then
6592 null;
6593 end if;
d6f39728 6594 end Analyze_Enumeration_Representation_Clause;
6595
6596 ----------------------------
6597 -- Analyze_Free_Statement --
6598 ----------------------------
6599
6600 procedure Analyze_Free_Statement (N : Node_Id) is
6601 begin
6602 Analyze (Expression (N));
6603 end Analyze_Free_Statement;
6604
40ca69b9 6605 ---------------------------
6606 -- Analyze_Freeze_Entity --
6607 ---------------------------
6608
6609 procedure Analyze_Freeze_Entity (N : Node_Id) is
40ca69b9 6610 begin
d9f6a4ee 6611 Freeze_Entity_Checks (N);
6612 end Analyze_Freeze_Entity;
98f7db28 6613
d9f6a4ee 6614 -----------------------------------
6615 -- Analyze_Freeze_Generic_Entity --
6616 -----------------------------------
98f7db28 6617
d9f6a4ee 6618 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6619 begin
6620 Freeze_Entity_Checks (N);
6621 end Analyze_Freeze_Generic_Entity;
40ca69b9 6622
d9f6a4ee 6623 ------------------------------------------
6624 -- Analyze_Record_Representation_Clause --
6625 ------------------------------------------
c8da6114 6626
d9f6a4ee 6627 -- Note: we check as much as we can here, but we can't do any checks
6628 -- based on the position values (e.g. overlap checks) until freeze time
6629 -- because especially in Ada 2005 (machine scalar mode), the processing
6630 -- for non-standard bit order can substantially change the positions.
6631 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6632 -- for the remainder of this processing.
d00681a7 6633
d9f6a4ee 6634 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6635 Ident : constant Node_Id := Identifier (N);
6636 Biased : Boolean;
6637 CC : Node_Id;
6638 Comp : Entity_Id;
6639 Fbit : Uint;
6640 Hbit : Uint := Uint_0;
6641 Lbit : Uint;
6642 Ocomp : Entity_Id;
6643 Posit : Uint;
6644 Rectype : Entity_Id;
6645 Recdef : Node_Id;
d00681a7 6646
d9f6a4ee 6647 function Is_Inherited (Comp : Entity_Id) return Boolean;
6648 -- True if Comp is an inherited component in a record extension
d00681a7 6649
d9f6a4ee 6650 ------------------
6651 -- Is_Inherited --
6652 ------------------
d00681a7 6653
d9f6a4ee 6654 function Is_Inherited (Comp : Entity_Id) return Boolean is
6655 Comp_Base : Entity_Id;
d00681a7 6656
d9f6a4ee 6657 begin
6658 if Ekind (Rectype) = E_Record_Subtype then
6659 Comp_Base := Original_Record_Component (Comp);
6660 else
6661 Comp_Base := Comp;
d00681a7 6662 end if;
6663
d9f6a4ee 6664 return Comp_Base /= Original_Record_Component (Comp_Base);
6665 end Is_Inherited;
d00681a7 6666
d9f6a4ee 6667 -- Local variables
d00681a7 6668
d9f6a4ee 6669 Is_Record_Extension : Boolean;
6670 -- True if Rectype is a record extension
d00681a7 6671
d9f6a4ee 6672 CR_Pragma : Node_Id := Empty;
6673 -- Points to N_Pragma node if Complete_Representation pragma present
d00681a7 6674
d9f6a4ee 6675 -- Start of processing for Analyze_Record_Representation_Clause
d00681a7 6676
d9f6a4ee 6677 begin
6678 if Ignore_Rep_Clauses then
2ff55065 6679 Kill_Rep_Clause (N);
d9f6a4ee 6680 return;
d00681a7 6681 end if;
98f7db28 6682
d9f6a4ee 6683 Find_Type (Ident);
6684 Rectype := Entity (Ident);
85377c9b 6685
d9f6a4ee 6686 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6687 return;
6688 else
6689 Rectype := Underlying_Type (Rectype);
6690 end if;
85377c9b 6691
d9f6a4ee 6692 -- First some basic error checks
85377c9b 6693
d9f6a4ee 6694 if not Is_Record_Type (Rectype) then
6695 Error_Msg_NE
6696 ("record type required, found}", Ident, First_Subtype (Rectype));
6697 return;
85377c9b 6698
d9f6a4ee 6699 elsif Scope (Rectype) /= Current_Scope then
6700 Error_Msg_N ("type must be declared in this scope", N);
6701 return;
85377c9b 6702
d9f6a4ee 6703 elsif not Is_First_Subtype (Rectype) then
6704 Error_Msg_N ("cannot give record rep clause for subtype", N);
6705 return;
9dc88aea 6706
d9f6a4ee 6707 elsif Has_Record_Rep_Clause (Rectype) then
6708 Error_Msg_N ("duplicate record rep clause ignored", N);
6709 return;
9dc88aea 6710
d9f6a4ee 6711 elsif Rep_Item_Too_Late (Rectype, N) then
6712 return;
9dc88aea 6713 end if;
fb7f2fc4 6714
2ced3742 6715 -- We know we have a first subtype, now possibly go to the anonymous
d9f6a4ee 6716 -- base type to determine whether Rectype is a record extension.
89f1e35c 6717
d9f6a4ee 6718 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6719 Is_Record_Extension :=
6720 Nkind (Recdef) = N_Derived_Type_Definition
6721 and then Present (Record_Extension_Part (Recdef));
89f1e35c 6722
d9f6a4ee 6723 if Present (Mod_Clause (N)) then
fb7f2fc4 6724 declare
d9f6a4ee 6725 Loc : constant Source_Ptr := Sloc (N);
6726 M : constant Node_Id := Mod_Clause (N);
6727 P : constant List_Id := Pragmas_Before (M);
6728 AtM_Nod : Node_Id;
6729
6730 Mod_Val : Uint;
6731 pragma Warnings (Off, Mod_Val);
fb7f2fc4 6732
6733 begin
d9f6a4ee 6734 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
fb7f2fc4 6735
d9f6a4ee 6736 if Warn_On_Obsolescent_Feature then
6737 Error_Msg_N
6738 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6739 Error_Msg_N
6740 ("\?j?use alignment attribute definition clause instead", N);
6741 end if;
fb7f2fc4 6742
d9f6a4ee 6743 if Present (P) then
6744 Analyze_List (P);
6745 end if;
89f1e35c 6746
d9f6a4ee 6747 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6748 -- the Mod clause into an alignment clause anyway, so that the
3ff5e35d 6749 -- back end can compute and back-annotate properly the size and
d9f6a4ee 6750 -- alignment of types that may include this record.
be9124d0 6751
d9f6a4ee 6752 -- This seems dubious, this destroys the source tree in a manner
6753 -- not detectable by ASIS ???
be9124d0 6754
d9f6a4ee 6755 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6756 AtM_Nod :=
6757 Make_Attribute_Definition_Clause (Loc,
83c6c069 6758 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
d9f6a4ee 6759 Chars => Name_Alignment,
6760 Expression => Relocate_Node (Expression (M)));
be9124d0 6761
d9f6a4ee 6762 Set_From_At_Mod (AtM_Nod);
6763 Insert_After (N, AtM_Nod);
6764 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6765 Set_Mod_Clause (N, Empty);
be9124d0 6766
d9f6a4ee 6767 else
6768 -- Get the alignment value to perform error checking
be9124d0 6769
d9f6a4ee 6770 Mod_Val := Get_Alignment_Value (Expression (M));
6771 end if;
6772 end;
6773 end if;
be9124d0 6774
d9f6a4ee 6775 -- For untagged types, clear any existing component clauses for the
6776 -- type. If the type is derived, this is what allows us to override
6777 -- a rep clause for the parent. For type extensions, the representation
6778 -- of the inherited components is inherited, so we want to keep previous
6779 -- component clauses for completeness.
be9124d0 6780
d9f6a4ee 6781 if not Is_Tagged_Type (Rectype) then
6782 Comp := First_Component_Or_Discriminant (Rectype);
6783 while Present (Comp) loop
6784 Set_Component_Clause (Comp, Empty);
6785 Next_Component_Or_Discriminant (Comp);
6786 end loop;
6787 end if;
be9124d0 6788
d9f6a4ee 6789 -- All done if no component clauses
be9124d0 6790
d9f6a4ee 6791 CC := First (Component_Clauses (N));
be9124d0 6792
d9f6a4ee 6793 if No (CC) then
6794 return;
6795 end if;
be9124d0 6796
d9f6a4ee 6797 -- A representation like this applies to the base type
be9124d0 6798
d9f6a4ee 6799 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6800 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6801 Set_Has_Specified_Layout (Base_Type (Rectype));
be9124d0 6802
d9f6a4ee 6803 -- Process the component clauses
be9124d0 6804
d9f6a4ee 6805 while Present (CC) loop
be9124d0 6806
d9f6a4ee 6807 -- Pragma
be9124d0 6808
d9f6a4ee 6809 if Nkind (CC) = N_Pragma then
6810 Analyze (CC);
be9124d0 6811
d9f6a4ee 6812 -- The only pragma of interest is Complete_Representation
be9124d0 6813
d9f6a4ee 6814 if Pragma_Name (CC) = Name_Complete_Representation then
6815 CR_Pragma := CC;
6816 end if;
be9124d0 6817
d9f6a4ee 6818 -- Processing for real component clause
be9124d0 6819
d9f6a4ee 6820 else
6821 Posit := Static_Integer (Position (CC));
6822 Fbit := Static_Integer (First_Bit (CC));
6823 Lbit := Static_Integer (Last_Bit (CC));
be9124d0 6824
d9f6a4ee 6825 if Posit /= No_Uint
6826 and then Fbit /= No_Uint
6827 and then Lbit /= No_Uint
6828 then
6829 if Posit < 0 then
f74a102b 6830 Error_Msg_N ("position cannot be negative", Position (CC));
be9124d0 6831
d9f6a4ee 6832 elsif Fbit < 0 then
f74a102b 6833 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
be9124d0 6834
d9f6a4ee 6835 -- The Last_Bit specified in a component clause must not be
6836 -- less than the First_Bit minus one (RM-13.5.1(10)).
be9124d0 6837
d9f6a4ee 6838 elsif Lbit < Fbit - 1 then
6839 Error_Msg_N
6840 ("last bit cannot be less than first bit minus one",
6841 Last_Bit (CC));
be9124d0 6842
d9f6a4ee 6843 -- Values look OK, so find the corresponding record component
6844 -- Even though the syntax allows an attribute reference for
6845 -- implementation-defined components, GNAT does not allow the
6846 -- tag to get an explicit position.
be9124d0 6847
d9f6a4ee 6848 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6849 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6850 Error_Msg_N ("position of tag cannot be specified", CC);
6851 else
6852 Error_Msg_N ("illegal component name", CC);
6853 end if;
be9124d0 6854
d9f6a4ee 6855 else
6856 Comp := First_Entity (Rectype);
6857 while Present (Comp) loop
6858 exit when Chars (Comp) = Chars (Component_Name (CC));
6859 Next_Entity (Comp);
6860 end loop;
be9124d0 6861
d9f6a4ee 6862 if No (Comp) then
be9124d0 6863
d9f6a4ee 6864 -- Maybe component of base type that is absent from
6865 -- statically constrained first subtype.
be9124d0 6866
d9f6a4ee 6867 Comp := First_Entity (Base_Type (Rectype));
6868 while Present (Comp) loop
6869 exit when Chars (Comp) = Chars (Component_Name (CC));
6870 Next_Entity (Comp);
6871 end loop;
6872 end if;
be9124d0 6873
d9f6a4ee 6874 if No (Comp) then
6875 Error_Msg_N
6876 ("component clause is for non-existent field", CC);
be9124d0 6877
d9f6a4ee 6878 -- Ada 2012 (AI05-0026): Any name that denotes a
6879 -- discriminant of an object of an unchecked union type
6880 -- shall not occur within a record_representation_clause.
be9124d0 6881
d9f6a4ee 6882 -- The general restriction of using record rep clauses on
6883 -- Unchecked_Union types has now been lifted. Since it is
6884 -- possible to introduce a record rep clause which mentions
6885 -- the discriminant of an Unchecked_Union in non-Ada 2012
6886 -- code, this check is applied to all versions of the
6887 -- language.
be9124d0 6888
d9f6a4ee 6889 elsif Ekind (Comp) = E_Discriminant
6890 and then Is_Unchecked_Union (Rectype)
6891 then
6892 Error_Msg_N
6893 ("cannot reference discriminant of unchecked union",
6894 Component_Name (CC));
be9124d0 6895
d9f6a4ee 6896 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6897 Error_Msg_NE
6898 ("component clause not allowed for inherited "
6899 & "component&", CC, Comp);
40ca69b9 6900
d9f6a4ee 6901 elsif Present (Component_Clause (Comp)) then
462a079f 6902
d9f6a4ee 6903 -- Diagnose duplicate rep clause, or check consistency
6904 -- if this is an inherited component. In a double fault,
6905 -- there may be a duplicate inconsistent clause for an
6906 -- inherited component.
462a079f 6907
d9f6a4ee 6908 if Scope (Original_Record_Component (Comp)) = Rectype
6909 or else Parent (Component_Clause (Comp)) = N
6910 then
6911 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6912 Error_Msg_N ("component clause previously given#", CC);
3062c401 6913
6914 else
6915 declare
6916 Rep1 : constant Node_Id := Component_Clause (Comp);
3062c401 6917 begin
6918 if Intval (Position (Rep1)) /=
6919 Intval (Position (CC))
6920 or else Intval (First_Bit (Rep1)) /=
6921 Intval (First_Bit (CC))
6922 or else Intval (Last_Bit (Rep1)) /=
6923 Intval (Last_Bit (CC))
6924 then
b9e61b2a 6925 Error_Msg_N
f74a102b 6926 ("component clause inconsistent with "
6927 & "representation of ancestor", CC);
6a06584c 6928
3062c401 6929 elsif Warn_On_Redundant_Constructs then
b9e61b2a 6930 Error_Msg_N
6a06584c 6931 ("?r?redundant confirming component clause "
6932 & "for component!", CC);
3062c401 6933 end if;
6934 end;
6935 end if;
d6f39728 6936
d2b860b4 6937 -- Normal case where this is the first component clause we
6938 -- have seen for this entity, so set it up properly.
6939
d6f39728 6940 else
83f8f0a6 6941 -- Make reference for field in record rep clause and set
6942 -- appropriate entity field in the field identifier.
6943
6944 Generate_Reference
6945 (Comp, Component_Name (CC), Set_Ref => False);
6946 Set_Entity (Component_Name (CC), Comp);
6947
2866d595 6948 -- Update Fbit and Lbit to the actual bit number
d6f39728 6949
6950 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6951 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6952
d6f39728 6953 if Has_Size_Clause (Rectype)
ada34def 6954 and then RM_Size (Rectype) <= Lbit
d6f39728 6955 then
6956 Error_Msg_N
6957 ("bit number out of range of specified size",
6958 Last_Bit (CC));
6959 else
6960 Set_Component_Clause (Comp, CC);
6961 Set_Component_Bit_Offset (Comp, Fbit);
6962 Set_Esize (Comp, 1 + (Lbit - Fbit));
6963 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6964 Set_Normalized_Position (Comp, Fbit / SSU);
6965
a0fc8c5b 6966 if Warn_On_Overridden_Size
6967 and then Has_Size_Clause (Etype (Comp))
6968 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6969 then
6970 Error_Msg_NE
1e3532e7 6971 ("?S?component size overrides size clause for&",
a0fc8c5b 6972 Component_Name (CC), Etype (Comp));
6973 end if;
6974
ea61a7ea 6975 -- This information is also set in the corresponding
6976 -- component of the base type, found by accessing the
6977 -- Original_Record_Component link if it is present.
d6f39728 6978
6979 Ocomp := Original_Record_Component (Comp);
6980
6981 if Hbit < Lbit then
6982 Hbit := Lbit;
6983 end if;
6984
6985 Check_Size
6986 (Component_Name (CC),
6987 Etype (Comp),
6988 Esize (Comp),
6989 Biased);
6990
b77e4501 6991 Set_Biased
6992 (Comp, First_Node (CC), "component clause", Biased);
cc46ff4b 6993
d6f39728 6994 if Present (Ocomp) then
6995 Set_Component_Clause (Ocomp, CC);
6996 Set_Component_Bit_Offset (Ocomp, Fbit);
6997 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6998 Set_Normalized_Position (Ocomp, Fbit / SSU);
6999 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
7000
7001 Set_Normalized_Position_Max
7002 (Ocomp, Normalized_Position (Ocomp));
7003
b77e4501 7004 -- Note: we don't use Set_Biased here, because we
7005 -- already gave a warning above if needed, and we
7006 -- would get a duplicate for the same name here.
7007
d6f39728 7008 Set_Has_Biased_Representation
7009 (Ocomp, Has_Biased_Representation (Comp));
7010 end if;
7011
7012 if Esize (Comp) < 0 then
7013 Error_Msg_N ("component size is negative", CC);
7014 end if;
7015 end if;
7016 end if;
7017 end if;
7018 end if;
7019 end if;
7020
7021 Next (CC);
7022 end loop;
7023
67278d60 7024 -- Check missing components if Complete_Representation pragma appeared
d6f39728 7025
67278d60 7026 if Present (CR_Pragma) then
7027 Comp := First_Component_Or_Discriminant (Rectype);
7028 while Present (Comp) loop
7029 if No (Component_Clause (Comp)) then
7030 Error_Msg_NE
7031 ("missing component clause for &", CR_Pragma, Comp);
7032 end if;
d6f39728 7033
67278d60 7034 Next_Component_Or_Discriminant (Comp);
7035 end loop;
d6f39728 7036
1e3532e7 7037 -- Give missing components warning if required
15ebb600 7038
fdd294d1 7039 elsif Warn_On_Unrepped_Components then
15ebb600 7040 declare
7041 Num_Repped_Components : Nat := 0;
7042 Num_Unrepped_Components : Nat := 0;
7043
7044 begin
7045 -- First count number of repped and unrepped components
7046
7047 Comp := First_Component_Or_Discriminant (Rectype);
7048 while Present (Comp) loop
7049 if Present (Component_Clause (Comp)) then
7050 Num_Repped_Components := Num_Repped_Components + 1;
7051 else
7052 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7053 end if;
7054
7055 Next_Component_Or_Discriminant (Comp);
7056 end loop;
7057
7058 -- We are only interested in the case where there is at least one
7059 -- unrepped component, and at least half the components have rep
7060 -- clauses. We figure that if less than half have them, then the
87f9eef5 7061 -- partial rep clause is really intentional. If the component
7062 -- type has no underlying type set at this point (as for a generic
7063 -- formal type), we don't know enough to give a warning on the
7064 -- component.
15ebb600 7065
7066 if Num_Unrepped_Components > 0
7067 and then Num_Unrepped_Components < Num_Repped_Components
7068 then
7069 Comp := First_Component_Or_Discriminant (Rectype);
7070 while Present (Comp) loop
83f8f0a6 7071 if No (Component_Clause (Comp))
3062c401 7072 and then Comes_From_Source (Comp)
87f9eef5 7073 and then Present (Underlying_Type (Etype (Comp)))
83f8f0a6 7074 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
67278d60 7075 or else Size_Known_At_Compile_Time
7076 (Underlying_Type (Etype (Comp))))
fdd294d1 7077 and then not Has_Warnings_Off (Rectype)
2be1f7d7 7078
7079 -- Ignore discriminant in unchecked union, since it is
7080 -- not there, and cannot have a component clause.
7081
7082 and then (not Is_Unchecked_Union (Rectype)
7083 or else Ekind (Comp) /= E_Discriminant)
83f8f0a6 7084 then
15ebb600 7085 Error_Msg_Sloc := Sloc (Comp);
7086 Error_Msg_NE
1e3532e7 7087 ("?C?no component clause given for & declared #",
15ebb600 7088 N, Comp);
7089 end if;
7090
7091 Next_Component_Or_Discriminant (Comp);
7092 end loop;
7093 end if;
7094 end;
d6f39728 7095 end if;
d6f39728 7096 end Analyze_Record_Representation_Clause;
7097
eb66e842 7098 -------------------------------------
7099 -- Build_Discrete_Static_Predicate --
7100 -------------------------------------
9ea61fdd 7101
eb66e842 7102 procedure Build_Discrete_Static_Predicate
7103 (Typ : Entity_Id;
7104 Expr : Node_Id;
7105 Nam : Name_Id)
9ea61fdd 7106 is
eb66e842 7107 Loc : constant Source_Ptr := Sloc (Expr);
9ea61fdd 7108
eb66e842 7109 Non_Static : exception;
7110 -- Raised if something non-static is found
9ea61fdd 7111
eb66e842 7112 Btyp : constant Entity_Id := Base_Type (Typ);
9ea61fdd 7113
eb66e842 7114 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7115 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7116 -- Low bound and high bound value of base type of Typ
9ea61fdd 7117
afc229da 7118 TLo : Uint;
7119 THi : Uint;
7120 -- Bounds for constructing the static predicate. We use the bound of the
7121 -- subtype if it is static, otherwise the corresponding base type bound.
7122 -- Note: a non-static subtype can have a static predicate.
9ea61fdd 7123
eb66e842 7124 type REnt is record
7125 Lo, Hi : Uint;
7126 end record;
7127 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7128 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7129 -- value.
9ea61fdd 7130
eb66e842 7131 type RList is array (Nat range <>) of REnt;
7132 -- A list of ranges. The ranges are sorted in increasing order, and are
7133 -- disjoint (there is a gap of at least one value between each range in
7134 -- the table). A value is in the set of ranges in Rlist if it lies
7135 -- within one of these ranges.
9ea61fdd 7136
eb66e842 7137 False_Range : constant RList :=
7138 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7139 -- An empty set of ranges represents a range list that can never be
7140 -- satisfied, since there are no ranges in which the value could lie,
7141 -- so it does not lie in any of them. False_Range is a canonical value
7142 -- for this empty set, but general processing should test for an Rlist
7143 -- with length zero (see Is_False predicate), since other null ranges
7144 -- may appear which must be treated as False.
5b5df4a9 7145
eb66e842 7146 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7147 -- Range representing True, value must be in the base range
5b5df4a9 7148
eb66e842 7149 function "and" (Left : RList; Right : RList) return RList;
7150 -- And's together two range lists, returning a range list. This is a set
7151 -- intersection operation.
5b5df4a9 7152
eb66e842 7153 function "or" (Left : RList; Right : RList) return RList;
7154 -- Or's together two range lists, returning a range list. This is a set
7155 -- union operation.
87f3d5d3 7156
eb66e842 7157 function "not" (Right : RList) return RList;
7158 -- Returns complement of a given range list, i.e. a range list
7159 -- representing all the values in TLo .. THi that are not in the input
7160 -- operand Right.
ed4adc99 7161
eb66e842 7162 function Build_Val (V : Uint) return Node_Id;
7163 -- Return an analyzed N_Identifier node referencing this value, suitable
5c6a5792 7164 -- for use as an entry in the Static_Discrte_Predicate list. This node
7165 -- is typed with the base type.
5b5df4a9 7166
eb66e842 7167 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7168 -- Return an analyzed N_Range node referencing this range, suitable for
5c6a5792 7169 -- use as an entry in the Static_Discrete_Predicate list. This node is
7170 -- typed with the base type.
5b5df4a9 7171
eb66e842 7172 function Get_RList (Exp : Node_Id) return RList;
7173 -- This is a recursive routine that converts the given expression into a
7174 -- list of ranges, suitable for use in building the static predicate.
5b5df4a9 7175
eb66e842 7176 function Is_False (R : RList) return Boolean;
7177 pragma Inline (Is_False);
7178 -- Returns True if the given range list is empty, and thus represents a
7179 -- False list of ranges that can never be satisfied.
87f3d5d3 7180
eb66e842 7181 function Is_True (R : RList) return Boolean;
7182 -- Returns True if R trivially represents the True predicate by having a
7183 -- single range from BLo to BHi.
5b5df4a9 7184
eb66e842 7185 function Is_Type_Ref (N : Node_Id) return Boolean;
7186 pragma Inline (Is_Type_Ref);
7187 -- Returns if True if N is a reference to the type for the predicate in
7188 -- the expression (i.e. if it is an identifier whose Chars field matches
7de4cba3 7189 -- the Nam given in the call). N must not be parenthesized, if the type
7190 -- name appears in parens, this routine will return False.
5b5df4a9 7191
eb66e842 7192 function Lo_Val (N : Node_Id) return Uint;
5c6a5792 7193 -- Given an entry from a Static_Discrete_Predicate list that is either
7194 -- a static expression or static range, gets either the expression value
7195 -- or the low bound of the range.
5b5df4a9 7196
eb66e842 7197 function Hi_Val (N : Node_Id) return Uint;
5c6a5792 7198 -- Given an entry from a Static_Discrete_Predicate list that is either
7199 -- a static expression or static range, gets either the expression value
7200 -- or the high bound of the range.
5b5df4a9 7201
eb66e842 7202 function Membership_Entry (N : Node_Id) return RList;
7203 -- Given a single membership entry (range, value, or subtype), returns
7204 -- the corresponding range list. Raises Static_Error if not static.
5b5df4a9 7205
eb66e842 7206 function Membership_Entries (N : Node_Id) return RList;
7207 -- Given an element on an alternatives list of a membership operation,
7208 -- returns the range list corresponding to this entry and all following
7209 -- entries (i.e. returns the "or" of this list of values).
b9e61b2a 7210
eb66e842 7211 function Stat_Pred (Typ : Entity_Id) return RList;
7212 -- Given a type, if it has a static predicate, then return the predicate
7213 -- as a range list, otherwise raise Non_Static.
c4968aa2 7214
eb66e842 7215 -----------
7216 -- "and" --
7217 -----------
c4968aa2 7218
eb66e842 7219 function "and" (Left : RList; Right : RList) return RList is
7220 FEnt : REnt;
7221 -- First range of result
c4968aa2 7222
eb66e842 7223 SLeft : Nat := Left'First;
7224 -- Start of rest of left entries
c4968aa2 7225
eb66e842 7226 SRight : Nat := Right'First;
7227 -- Start of rest of right entries
2072eaa9 7228
eb66e842 7229 begin
7230 -- If either range is True, return the other
5b5df4a9 7231
eb66e842 7232 if Is_True (Left) then
7233 return Right;
7234 elsif Is_True (Right) then
7235 return Left;
7236 end if;
87f3d5d3 7237
eb66e842 7238 -- If either range is False, return False
5b5df4a9 7239
eb66e842 7240 if Is_False (Left) or else Is_False (Right) then
7241 return False_Range;
7242 end if;
4c1fd062 7243
eb66e842 7244 -- Loop to remove entries at start that are disjoint, and thus just
7245 -- get discarded from the result entirely.
5b5df4a9 7246
eb66e842 7247 loop
7248 -- If no operands left in either operand, result is false
5b5df4a9 7249
eb66e842 7250 if SLeft > Left'Last or else SRight > Right'Last then
7251 return False_Range;
5b5df4a9 7252
eb66e842 7253 -- Discard first left operand entry if disjoint with right
5b5df4a9 7254
eb66e842 7255 elsif Left (SLeft).Hi < Right (SRight).Lo then
7256 SLeft := SLeft + 1;
5b5df4a9 7257
eb66e842 7258 -- Discard first right operand entry if disjoint with left
5b5df4a9 7259
eb66e842 7260 elsif Right (SRight).Hi < Left (SLeft).Lo then
7261 SRight := SRight + 1;
5b5df4a9 7262
eb66e842 7263 -- Otherwise we have an overlapping entry
5b5df4a9 7264
eb66e842 7265 else
7266 exit;
7267 end if;
7268 end loop;
5b5df4a9 7269
eb66e842 7270 -- Now we have two non-null operands, and first entries overlap. The
7271 -- first entry in the result will be the overlapping part of these
7272 -- two entries.
47a46747 7273
eb66e842 7274 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7275 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
47a46747 7276
eb66e842 7277 -- Now we can remove the entry that ended at a lower value, since its
7278 -- contribution is entirely contained in Fent.
5b5df4a9 7279
eb66e842 7280 if Left (SLeft).Hi <= Right (SRight).Hi then
7281 SLeft := SLeft + 1;
7282 else
7283 SRight := SRight + 1;
7284 end if;
5b5df4a9 7285
eb66e842 7286 -- Compute result by concatenating this first entry with the "and" of
7287 -- the remaining parts of the left and right operands. Note that if
7288 -- either of these is empty, "and" will yield empty, so that we will
7289 -- end up with just Fent, which is what we want in that case.
5b5df4a9 7290
eb66e842 7291 return
7292 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7293 end "and";
fb7f2fc4 7294
eb66e842 7295 -----------
7296 -- "not" --
7297 -----------
fb7f2fc4 7298
eb66e842 7299 function "not" (Right : RList) return RList is
7300 begin
7301 -- Return True if False range
fb7f2fc4 7302
eb66e842 7303 if Is_False (Right) then
7304 return True_Range;
7305 end if;
ed4adc99 7306
eb66e842 7307 -- Return False if True range
fb7f2fc4 7308
eb66e842 7309 if Is_True (Right) then
7310 return False_Range;
7311 end if;
fb7f2fc4 7312
eb66e842 7313 -- Here if not trivial case
87f3d5d3 7314
eb66e842 7315 declare
7316 Result : RList (1 .. Right'Length + 1);
7317 -- May need one more entry for gap at beginning and end
87f3d5d3 7318
eb66e842 7319 Count : Nat := 0;
7320 -- Number of entries stored in Result
4098232e 7321
eb66e842 7322 begin
7323 -- Gap at start
4098232e 7324
eb66e842 7325 if Right (Right'First).Lo > TLo then
7326 Count := Count + 1;
7327 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7328 end if;
ed4adc99 7329
eb66e842 7330 -- Gaps between ranges
ed4adc99 7331
eb66e842 7332 for J in Right'First .. Right'Last - 1 loop
7333 Count := Count + 1;
7334 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7335 end loop;
5b5df4a9 7336
eb66e842 7337 -- Gap at end
5b5df4a9 7338
eb66e842 7339 if Right (Right'Last).Hi < THi then
7340 Count := Count + 1;
7341 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7342 end if;
5b5df4a9 7343
eb66e842 7344 return Result (1 .. Count);
7345 end;
7346 end "not";
5b5df4a9 7347
eb66e842 7348 ----------
7349 -- "or" --
7350 ----------
5b5df4a9 7351
eb66e842 7352 function "or" (Left : RList; Right : RList) return RList is
7353 FEnt : REnt;
7354 -- First range of result
5b5df4a9 7355
eb66e842 7356 SLeft : Nat := Left'First;
7357 -- Start of rest of left entries
5b5df4a9 7358
eb66e842 7359 SRight : Nat := Right'First;
7360 -- Start of rest of right entries
5b5df4a9 7361
eb66e842 7362 begin
7363 -- If either range is True, return True
5b5df4a9 7364
eb66e842 7365 if Is_True (Left) or else Is_True (Right) then
7366 return True_Range;
7367 end if;
5b5df4a9 7368
eb66e842 7369 -- If either range is False (empty), return the other
5b5df4a9 7370
eb66e842 7371 if Is_False (Left) then
7372 return Right;
7373 elsif Is_False (Right) then
7374 return Left;
7375 end if;
5b5df4a9 7376
eb66e842 7377 -- Initialize result first entry from left or right operand depending
7378 -- on which starts with the lower range.
5b5df4a9 7379
eb66e842 7380 if Left (SLeft).Lo < Right (SRight).Lo then
7381 FEnt := Left (SLeft);
7382 SLeft := SLeft + 1;
7383 else
7384 FEnt := Right (SRight);
7385 SRight := SRight + 1;
7386 end if;
5b5df4a9 7387
eb66e842 7388 -- This loop eats ranges from left and right operands that are
7389 -- contiguous with the first range we are gathering.
9ea61fdd 7390
eb66e842 7391 loop
7392 -- Eat first entry in left operand if contiguous or overlapped by
7393 -- gathered first operand of result.
9ea61fdd 7394
eb66e842 7395 if SLeft <= Left'Last
7396 and then Left (SLeft).Lo <= FEnt.Hi + 1
7397 then
7398 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7399 SLeft := SLeft + 1;
9ea61fdd 7400
eb66e842 7401 -- Eat first entry in right operand if contiguous or overlapped by
7402 -- gathered right operand of result.
9ea61fdd 7403
eb66e842 7404 elsif SRight <= Right'Last
7405 and then Right (SRight).Lo <= FEnt.Hi + 1
7406 then
7407 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7408 SRight := SRight + 1;
9ea61fdd 7409
eb66e842 7410 -- All done if no more entries to eat
5b5df4a9 7411
eb66e842 7412 else
7413 exit;
7414 end if;
7415 end loop;
5b5df4a9 7416
eb66e842 7417 -- Obtain result as the first entry we just computed, concatenated
7418 -- to the "or" of the remaining results (if one operand is empty,
7419 -- this will just concatenate with the other
5b5df4a9 7420
eb66e842 7421 return
7422 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7423 end "or";
5b5df4a9 7424
eb66e842 7425 -----------------
7426 -- Build_Range --
7427 -----------------
5b5df4a9 7428
eb66e842 7429 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7430 Result : Node_Id;
5b5df4a9 7431 begin
eb66e842 7432 Result :=
7433 Make_Range (Loc,
7434 Low_Bound => Build_Val (Lo),
7435 High_Bound => Build_Val (Hi));
7436 Set_Etype (Result, Btyp);
7437 Set_Analyzed (Result);
7438 return Result;
7439 end Build_Range;
5b5df4a9 7440
eb66e842 7441 ---------------
7442 -- Build_Val --
7443 ---------------
5b5df4a9 7444
eb66e842 7445 function Build_Val (V : Uint) return Node_Id is
7446 Result : Node_Id;
5b5df4a9 7447
eb66e842 7448 begin
7449 if Is_Enumeration_Type (Typ) then
7450 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7451 else
7452 Result := Make_Integer_Literal (Loc, V);
7453 end if;
5b5df4a9 7454
eb66e842 7455 Set_Etype (Result, Btyp);
7456 Set_Is_Static_Expression (Result);
7457 Set_Analyzed (Result);
7458 return Result;
7459 end Build_Val;
87f3d5d3 7460
eb66e842 7461 ---------------
7462 -- Get_RList --
7463 ---------------
87f3d5d3 7464
eb66e842 7465 function Get_RList (Exp : Node_Id) return RList is
7466 Op : Node_Kind;
7467 Val : Uint;
87f3d5d3 7468
eb66e842 7469 begin
7470 -- Static expression can only be true or false
87f3d5d3 7471
eb66e842 7472 if Is_OK_Static_Expression (Exp) then
7473 if Expr_Value (Exp) = 0 then
7474 return False_Range;
7475 else
7476 return True_Range;
9ea61fdd 7477 end if;
eb66e842 7478 end if;
87f3d5d3 7479
eb66e842 7480 -- Otherwise test node type
192b8dab 7481
eb66e842 7482 Op := Nkind (Exp);
192b8dab 7483
eb66e842 7484 case Op is
5d3fb947 7485
eb66e842 7486 -- And
5d3fb947 7487
eb66e842 7488 when N_Op_And | N_And_Then =>
7489 return Get_RList (Left_Opnd (Exp))
7490 and
7491 Get_RList (Right_Opnd (Exp));
5b5df4a9 7492
eb66e842 7493 -- Or
9dc88aea 7494
eb66e842 7495 when N_Op_Or | N_Or_Else =>
7496 return Get_RList (Left_Opnd (Exp))
7497 or
7498 Get_RList (Right_Opnd (Exp));
7c443ae8 7499
eb66e842 7500 -- Not
9dc88aea 7501
eb66e842 7502 when N_Op_Not =>
7503 return not Get_RList (Right_Opnd (Exp));
9dc88aea 7504
eb66e842 7505 -- Comparisons of type with static value
84c8f0b8 7506
eb66e842 7507 when N_Op_Compare =>
490beba6 7508
eb66e842 7509 -- Type is left operand
9dc88aea 7510
eb66e842 7511 if Is_Type_Ref (Left_Opnd (Exp))
7512 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7513 then
7514 Val := Expr_Value (Right_Opnd (Exp));
84c8f0b8 7515
eb66e842 7516 -- Typ is right operand
84c8f0b8 7517
eb66e842 7518 elsif Is_Type_Ref (Right_Opnd (Exp))
7519 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7520 then
7521 Val := Expr_Value (Left_Opnd (Exp));
84c8f0b8 7522
eb66e842 7523 -- Invert sense of comparison
84c8f0b8 7524
eb66e842 7525 case Op is
7526 when N_Op_Gt => Op := N_Op_Lt;
7527 when N_Op_Lt => Op := N_Op_Gt;
7528 when N_Op_Ge => Op := N_Op_Le;
7529 when N_Op_Le => Op := N_Op_Ge;
7530 when others => null;
7531 end case;
84c8f0b8 7532
eb66e842 7533 -- Other cases are non-static
34d045d3 7534
eb66e842 7535 else
7536 raise Non_Static;
7537 end if;
9dc88aea 7538
eb66e842 7539 -- Construct range according to comparison operation
9dc88aea 7540
eb66e842 7541 case Op is
7542 when N_Op_Eq =>
7543 return RList'(1 => REnt'(Val, Val));
9dc88aea 7544
eb66e842 7545 when N_Op_Ge =>
7546 return RList'(1 => REnt'(Val, BHi));
84c8f0b8 7547
eb66e842 7548 when N_Op_Gt =>
7549 return RList'(1 => REnt'(Val + 1, BHi));
84c8f0b8 7550
eb66e842 7551 when N_Op_Le =>
7552 return RList'(1 => REnt'(BLo, Val));
fb7f2fc4 7553
eb66e842 7554 when N_Op_Lt =>
7555 return RList'(1 => REnt'(BLo, Val - 1));
9dc88aea 7556
eb66e842 7557 when N_Op_Ne =>
7558 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
9dc88aea 7559
eb66e842 7560 when others =>
7561 raise Program_Error;
7562 end case;
9dc88aea 7563
eb66e842 7564 -- Membership (IN)
9dc88aea 7565
eb66e842 7566 when N_In =>
7567 if not Is_Type_Ref (Left_Opnd (Exp)) then
7568 raise Non_Static;
7569 end if;
9dc88aea 7570
eb66e842 7571 if Present (Right_Opnd (Exp)) then
7572 return Membership_Entry (Right_Opnd (Exp));
7573 else
7574 return Membership_Entries (First (Alternatives (Exp)));
7575 end if;
9dc88aea 7576
eb66e842 7577 -- Negative membership (NOT IN)
9dc88aea 7578
eb66e842 7579 when N_Not_In =>
7580 if not Is_Type_Ref (Left_Opnd (Exp)) then
7581 raise Non_Static;
7582 end if;
9dc88aea 7583
eb66e842 7584 if Present (Right_Opnd (Exp)) then
7585 return not Membership_Entry (Right_Opnd (Exp));
7586 else
7587 return not Membership_Entries (First (Alternatives (Exp)));
7588 end if;
9dc88aea 7589
eb66e842 7590 -- Function call, may be call to static predicate
9dc88aea 7591
eb66e842 7592 when N_Function_Call =>
7593 if Is_Entity_Name (Name (Exp)) then
7594 declare
7595 Ent : constant Entity_Id := Entity (Name (Exp));
7596 begin
7597 if Is_Predicate_Function (Ent)
7598 or else
7599 Is_Predicate_Function_M (Ent)
7600 then
7601 return Stat_Pred (Etype (First_Formal (Ent)));
7602 end if;
7603 end;
7604 end if;
9dc88aea 7605
eb66e842 7606 -- Other function call cases are non-static
9dc88aea 7607
eb66e842 7608 raise Non_Static;
490beba6 7609
eb66e842 7610 -- Qualified expression, dig out the expression
c92e878b 7611
eb66e842 7612 when N_Qualified_Expression =>
7613 return Get_RList (Expression (Exp));
4c1fd062 7614
eb66e842 7615 when N_Case_Expression =>
7616 declare
7617 Alt : Node_Id;
7618 Choices : List_Id;
7619 Dep : Node_Id;
4c1fd062 7620
eb66e842 7621 begin
7622 if not Is_Entity_Name (Expression (Expr))
7623 or else Etype (Expression (Expr)) /= Typ
7624 then
7625 Error_Msg_N
7626 ("expression must denaote subtype", Expression (Expr));
7627 return False_Range;
7628 end if;
9dc88aea 7629
eb66e842 7630 -- Collect discrete choices in all True alternatives
9dc88aea 7631
eb66e842 7632 Choices := New_List;
7633 Alt := First (Alternatives (Exp));
7634 while Present (Alt) loop
7635 Dep := Expression (Alt);
34d045d3 7636
cda40848 7637 if not Is_OK_Static_Expression (Dep) then
eb66e842 7638 raise Non_Static;
ebbab42d 7639
eb66e842 7640 elsif Is_True (Expr_Value (Dep)) then
7641 Append_List_To (Choices,
7642 New_Copy_List (Discrete_Choices (Alt)));
7643 end if;
fb7f2fc4 7644
eb66e842 7645 Next (Alt);
7646 end loop;
9dc88aea 7647
eb66e842 7648 return Membership_Entries (First (Choices));
7649 end;
9dc88aea 7650
eb66e842 7651 -- Expression with actions: if no actions, dig out expression
9dc88aea 7652
eb66e842 7653 when N_Expression_With_Actions =>
7654 if Is_Empty_List (Actions (Exp)) then
7655 return Get_RList (Expression (Exp));
7656 else
7657 raise Non_Static;
7658 end if;
9dc88aea 7659
eb66e842 7660 -- Xor operator
490beba6 7661
eb66e842 7662 when N_Op_Xor =>
7663 return (Get_RList (Left_Opnd (Exp))
7664 and not Get_RList (Right_Opnd (Exp)))
7665 or (Get_RList (Right_Opnd (Exp))
7666 and not Get_RList (Left_Opnd (Exp)));
9dc88aea 7667
eb66e842 7668 -- Any other node type is non-static
fb7f2fc4 7669
eb66e842 7670 when others =>
7671 raise Non_Static;
7672 end case;
7673 end Get_RList;
fb7f2fc4 7674
eb66e842 7675 ------------
7676 -- Hi_Val --
7677 ------------
fb7f2fc4 7678
eb66e842 7679 function Hi_Val (N : Node_Id) return Uint is
7680 begin
cda40848 7681 if Is_OK_Static_Expression (N) then
eb66e842 7682 return Expr_Value (N);
7683 else
7684 pragma Assert (Nkind (N) = N_Range);
7685 return Expr_Value (High_Bound (N));
7686 end if;
7687 end Hi_Val;
fb7f2fc4 7688
eb66e842 7689 --------------
7690 -- Is_False --
7691 --------------
fb7f2fc4 7692
eb66e842 7693 function Is_False (R : RList) return Boolean is
7694 begin
7695 return R'Length = 0;
7696 end Is_False;
9dc88aea 7697
eb66e842 7698 -------------
7699 -- Is_True --
7700 -------------
9dc88aea 7701
eb66e842 7702 function Is_True (R : RList) return Boolean is
7703 begin
7704 return R'Length = 1
7705 and then R (R'First).Lo = BLo
7706 and then R (R'First).Hi = BHi;
7707 end Is_True;
9dc88aea 7708
eb66e842 7709 -----------------
7710 -- Is_Type_Ref --
7711 -----------------
9dc88aea 7712
eb66e842 7713 function Is_Type_Ref (N : Node_Id) return Boolean is
7714 begin
7de4cba3 7715 return Nkind (N) = N_Identifier
7716 and then Chars (N) = Nam
7717 and then Paren_Count (N) = 0;
eb66e842 7718 end Is_Type_Ref;
9dc88aea 7719
eb66e842 7720 ------------
7721 -- Lo_Val --
7722 ------------
9dc88aea 7723
eb66e842 7724 function Lo_Val (N : Node_Id) return Uint is
84c8f0b8 7725 begin
cda40848 7726 if Is_OK_Static_Expression (N) then
eb66e842 7727 return Expr_Value (N);
84c8f0b8 7728 else
eb66e842 7729 pragma Assert (Nkind (N) = N_Range);
7730 return Expr_Value (Low_Bound (N));
84c8f0b8 7731 end if;
eb66e842 7732 end Lo_Val;
d97beb2f 7733
eb66e842 7734 ------------------------
7735 -- Membership_Entries --
7736 ------------------------
d97beb2f 7737
eb66e842 7738 function Membership_Entries (N : Node_Id) return RList is
84c8f0b8 7739 begin
eb66e842 7740 if No (Next (N)) then
7741 return Membership_Entry (N);
84c8f0b8 7742 else
eb66e842 7743 return Membership_Entry (N) or Membership_Entries (Next (N));
84c8f0b8 7744 end if;
eb66e842 7745 end Membership_Entries;
84c8f0b8 7746
eb66e842 7747 ----------------------
7748 -- Membership_Entry --
7749 ----------------------
84c8f0b8 7750
eb66e842 7751 function Membership_Entry (N : Node_Id) return RList is
7752 Val : Uint;
7753 SLo : Uint;
7754 SHi : Uint;
d97beb2f 7755
eb66e842 7756 begin
7757 -- Range case
d97beb2f 7758
eb66e842 7759 if Nkind (N) = N_Range then
cda40848 7760 if not Is_OK_Static_Expression (Low_Bound (N))
eb66e842 7761 or else
cda40848 7762 not Is_OK_Static_Expression (High_Bound (N))
eb66e842 7763 then
7764 raise Non_Static;
7765 else
7766 SLo := Expr_Value (Low_Bound (N));
7767 SHi := Expr_Value (High_Bound (N));
7768 return RList'(1 => REnt'(SLo, SHi));
7769 end if;
84c8f0b8 7770
eb66e842 7771 -- Static expression case
84c8f0b8 7772
cda40848 7773 elsif Is_OK_Static_Expression (N) then
eb66e842 7774 Val := Expr_Value (N);
7775 return RList'(1 => REnt'(Val, Val));
d97beb2f 7776
eb66e842 7777 -- Identifier (other than static expression) case
d97beb2f 7778
eb66e842 7779 else pragma Assert (Nkind (N) = N_Identifier);
d97beb2f 7780
eb66e842 7781 -- Type case
d97beb2f 7782
eb66e842 7783 if Is_Type (Entity (N)) then
d97beb2f 7784
eb66e842 7785 -- If type has predicates, process them
d97beb2f 7786
eb66e842 7787 if Has_Predicates (Entity (N)) then
7788 return Stat_Pred (Entity (N));
d97beb2f 7789
eb66e842 7790 -- For static subtype without predicates, get range
9dc88aea 7791
cda40848 7792 elsif Is_OK_Static_Subtype (Entity (N)) then
eb66e842 7793 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7794 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7795 return RList'(1 => REnt'(SLo, SHi));
9f269bd8 7796
eb66e842 7797 -- Any other type makes us non-static
9f269bd8 7798
eb66e842 7799 else
7800 raise Non_Static;
7801 end if;
84c8f0b8 7802
eb66e842 7803 -- Any other kind of identifier in predicate (e.g. a non-static
7804 -- expression value) means this is not a static predicate.
84c8f0b8 7805
eb66e842 7806 else
7807 raise Non_Static;
7808 end if;
7809 end if;
7810 end Membership_Entry;
84c8f0b8 7811
eb66e842 7812 ---------------
7813 -- Stat_Pred --
7814 ---------------
84c8f0b8 7815
eb66e842 7816 function Stat_Pred (Typ : Entity_Id) return RList is
7817 begin
7818 -- Not static if type does not have static predicates
84c8f0b8 7819
5c6a5792 7820 if not Has_Static_Predicate (Typ) then
eb66e842 7821 raise Non_Static;
7822 end if;
84c8f0b8 7823
eb66e842 7824 -- Otherwise we convert the predicate list to a range list
84c8f0b8 7825
eb66e842 7826 declare
5c6a5792 7827 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7828 Result : RList (1 .. List_Length (Spred));
eb66e842 7829 P : Node_Id;
84c8f0b8 7830
eb66e842 7831 begin
5c6a5792 7832 P := First (Static_Discrete_Predicate (Typ));
eb66e842 7833 for J in Result'Range loop
7834 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7835 Next (P);
7836 end loop;
84c8f0b8 7837
eb66e842 7838 return Result;
7839 end;
7840 end Stat_Pred;
84c8f0b8 7841
eb66e842 7842 -- Start of processing for Build_Discrete_Static_Predicate
84c8f0b8 7843
eb66e842 7844 begin
fdec445e 7845 -- Establish bounds for the predicate
afc229da 7846
7847 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7848 TLo := Expr_Value (Type_Low_Bound (Typ));
7849 else
7850 TLo := BLo;
7851 end if;
7852
7853 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7854 THi := Expr_Value (Type_High_Bound (Typ));
7855 else
7856 THi := BHi;
7857 end if;
7858
eb66e842 7859 -- Analyze the expression to see if it is a static predicate
84c8f0b8 7860
eb66e842 7861 declare
7862 Ranges : constant RList := Get_RList (Expr);
7863 -- Range list from expression if it is static
84c8f0b8 7864
eb66e842 7865 Plist : List_Id;
84c8f0b8 7866
eb66e842 7867 begin
7868 -- Convert range list into a form for the static predicate. In the
7869 -- Ranges array, we just have raw ranges, these must be converted
7870 -- to properly typed and analyzed static expressions or range nodes.
84c8f0b8 7871
eb66e842 7872 -- Note: here we limit ranges to the ranges of the subtype, so that
7873 -- a predicate is always false for values outside the subtype. That
7874 -- seems fine, such values are invalid anyway, and considering them
7875 -- to fail the predicate seems allowed and friendly, and furthermore
7876 -- simplifies processing for case statements and loops.
84c8f0b8 7877
eb66e842 7878 Plist := New_List;
7879
7880 for J in Ranges'Range loop
84c8f0b8 7881 declare
eb66e842 7882 Lo : Uint := Ranges (J).Lo;
7883 Hi : Uint := Ranges (J).Hi;
84c8f0b8 7884
eb66e842 7885 begin
7886 -- Ignore completely out of range entry
84c8f0b8 7887
eb66e842 7888 if Hi < TLo or else Lo > THi then
7889 null;
84c8f0b8 7890
eb66e842 7891 -- Otherwise process entry
84c8f0b8 7892
eb66e842 7893 else
7894 -- Adjust out of range value to subtype range
490beba6 7895
eb66e842 7896 if Lo < TLo then
7897 Lo := TLo;
7898 end if;
490beba6 7899
eb66e842 7900 if Hi > THi then
7901 Hi := THi;
7902 end if;
84c8f0b8 7903
eb66e842 7904 -- Convert range into required form
84c8f0b8 7905
eb66e842 7906 Append_To (Plist, Build_Range (Lo, Hi));
84c8f0b8 7907 end if;
eb66e842 7908 end;
7909 end loop;
84c8f0b8 7910
eb66e842 7911 -- Processing was successful and all entries were static, so now we
7912 -- can store the result as the predicate list.
84c8f0b8 7913
5c6a5792 7914 Set_Static_Discrete_Predicate (Typ, Plist);
84c8f0b8 7915
eb66e842 7916 -- The processing for static predicates put the expression into
7917 -- canonical form as a series of ranges. It also eliminated
7918 -- duplicates and collapsed and combined ranges. We might as well
7919 -- replace the alternatives list of the right operand of the
7920 -- membership test with the static predicate list, which will
7921 -- usually be more efficient.
84c8f0b8 7922
eb66e842 7923 declare
7924 New_Alts : constant List_Id := New_List;
7925 Old_Node : Node_Id;
7926 New_Node : Node_Id;
84c8f0b8 7927
eb66e842 7928 begin
7929 Old_Node := First (Plist);
7930 while Present (Old_Node) loop
7931 New_Node := New_Copy (Old_Node);
84c8f0b8 7932
eb66e842 7933 if Nkind (New_Node) = N_Range then
7934 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7935 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7936 end if;
84c8f0b8 7937
eb66e842 7938 Append_To (New_Alts, New_Node);
7939 Next (Old_Node);
7940 end loop;
84c8f0b8 7941
eb66e842 7942 -- If empty list, replace by False
84c8f0b8 7943
eb66e842 7944 if Is_Empty_List (New_Alts) then
7945 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
84c8f0b8 7946
eb66e842 7947 -- Else replace by set membership test
84c8f0b8 7948
eb66e842 7949 else
7950 Rewrite (Expr,
7951 Make_In (Loc,
7952 Left_Opnd => Make_Identifier (Loc, Nam),
7953 Right_Opnd => Empty,
7954 Alternatives => New_Alts));
490beba6 7955
eb66e842 7956 -- Resolve new expression in function context
490beba6 7957
eb66e842 7958 Install_Formals (Predicate_Function (Typ));
7959 Push_Scope (Predicate_Function (Typ));
7960 Analyze_And_Resolve (Expr, Standard_Boolean);
7961 Pop_Scope;
7962 end if;
7963 end;
7964 end;
9ab32fe9 7965
eb66e842 7966 -- If non-static, return doing nothing
9ab32fe9 7967
eb66e842 7968 exception
7969 when Non_Static =>
7970 return;
7971 end Build_Discrete_Static_Predicate;
64cc9e5d 7972
ee2b7923 7973 --------------------------------
7974 -- Build_Export_Import_Pragma --
7975 --------------------------------
7976
7977 function Build_Export_Import_Pragma
7978 (Asp : Node_Id;
7979 Id : Entity_Id) return Node_Id
7980 is
7981 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
7982 Expr : constant Node_Id := Expression (Asp);
7983 Loc : constant Source_Ptr := Sloc (Asp);
7984
7985 Args : List_Id;
7986 Conv : Node_Id;
7987 Conv_Arg : Node_Id;
7988 Dummy_1 : Node_Id;
7989 Dummy_2 : Node_Id;
7990 EN : Node_Id;
7991 LN : Node_Id;
7992 Prag : Node_Id;
7993
7994 Create_Pragma : Boolean := False;
7995 -- This flag is set when the aspect form is such that it warrants the
7996 -- creation of a corresponding pragma.
7997
7998 begin
7999 if Present (Expr) then
8000 if Error_Posted (Expr) then
8001 null;
8002
8003 elsif Is_True (Expr_Value (Expr)) then
8004 Create_Pragma := True;
8005 end if;
8006
8007 -- Otherwise the aspect defaults to True
8008
8009 else
8010 Create_Pragma := True;
8011 end if;
8012
8013 -- Nothing to do when the expression is False or is erroneous
8014
8015 if not Create_Pragma then
8016 return Empty;
8017 end if;
8018
8019 -- Obtain all interfacing aspects that apply to the related entity
8020
8021 Get_Interfacing_Aspects
8022 (Iface_Asp => Asp,
8023 Conv_Asp => Conv,
8024 EN_Asp => EN,
8025 Expo_Asp => Dummy_1,
8026 Imp_Asp => Dummy_2,
8027 LN_Asp => LN);
8028
8029 Args := New_List;
8030
8031 -- Handle the convention argument
8032
8033 if Present (Conv) then
8034 Conv_Arg := New_Copy_Tree (Expression (Conv));
8035
8036 -- Assume convention "Ada' when aspect Convention is missing
8037
8038 else
8039 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8040 end if;
8041
8042 Append_To (Args,
8043 Make_Pragma_Argument_Association (Loc,
8044 Chars => Name_Convention,
8045 Expression => Conv_Arg));
8046
8047 -- Handle the entity argument
8048
8049 Append_To (Args,
8050 Make_Pragma_Argument_Association (Loc,
8051 Chars => Name_Entity,
8052 Expression => New_Occurrence_Of (Id, Loc)));
8053
8054 -- Handle the External_Name argument
8055
8056 if Present (EN) then
8057 Append_To (Args,
8058 Make_Pragma_Argument_Association (Loc,
8059 Chars => Name_External_Name,
8060 Expression => New_Copy_Tree (Expression (EN))));
8061 end if;
8062
8063 -- Handle the Link_Name argument
8064
8065 if Present (LN) then
8066 Append_To (Args,
8067 Make_Pragma_Argument_Association (Loc,
8068 Chars => Name_Link_Name,
8069 Expression => New_Copy_Tree (Expression (LN))));
8070 end if;
8071
8072 -- Generate:
8073 -- pragma Export/Import
8074 -- (Convention => <Conv>/Ada,
8075 -- Entity => <Id>,
8076 -- [External_Name => <EN>,]
8077 -- [Link_Name => <LN>]);
8078
8079 Prag :=
8080 Make_Pragma (Loc,
8081 Pragma_Identifier =>
8082 Make_Identifier (Loc, Chars (Identifier (Asp))),
8083 Pragma_Argument_Associations => Args);
8084
8085 -- Decorate the relevant aspect and the pragma
8086
8087 Set_Aspect_Rep_Item (Asp, Prag);
8088
8089 Set_Corresponding_Aspect (Prag, Asp);
8090 Set_From_Aspect_Specification (Prag);
8091 Set_Parent (Prag, Asp);
8092
8093 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8094 Set_Import_Pragma (Id, Prag);
8095 end if;
8096
8097 return Prag;
8098 end Build_Export_Import_Pragma;
8099
eb66e842 8100 -------------------------------
8101 -- Build_Predicate_Functions --
8102 -------------------------------
d9f6a4ee 8103
eb66e842 8104 -- The procedures that are constructed here have the form:
d9f6a4ee 8105
eb66e842 8106 -- function typPredicate (Ixxx : typ) return Boolean is
8107 -- begin
8108 -- return
75491446 8109 -- typ1Predicate (typ1 (Ixxx))
eb66e842 8110 -- and then typ2Predicate (typ2 (Ixxx))
8111 -- and then ...;
75491446 8112 -- exp1 and then exp2 and then ...
eb66e842 8113 -- end typPredicate;
d9f6a4ee 8114
eb66e842 8115 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8116 -- this is the point at which these expressions get analyzed, providing the
8117 -- required delay, and typ1, typ2, are entities from which predicates are
8118 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8119 -- use this function even if checks are off, e.g. for membership tests.
d9f6a4ee 8120
75491446 8121 -- Note that the inherited predicates are evaluated first, as required by
8122 -- AI12-0071-1.
8123
8124 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8125 -- the form of this return expression.
8126
eb66e842 8127 -- If the expression has at least one Raise_Expression, then we also build
8128 -- the typPredicateM version of the function, in which any occurrence of a
8129 -- Raise_Expression is converted to "return False".
d9f6a4ee 8130
eb66e842 8131 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8132 Loc : constant Source_Ptr := Sloc (Typ);
d9f6a4ee 8133
eb66e842 8134 Expr : Node_Id;
8135 -- This is the expression for the result of the function. It is
8136 -- is build by connecting the component predicates with AND THEN.
d9f6a4ee 8137
eb66e842 8138 Expr_M : Node_Id;
8139 -- This is the corresponding return expression for the Predicate_M
8140 -- function. It differs in that raise expressions are marked for
8141 -- special expansion (see Process_REs).
d9f6a4ee 8142
9c20237a 8143 Object_Name : Name_Id;
eb66e842 8144 -- Name for argument of Predicate procedure. Note that we use the same
499918a7 8145 -- name for both predicate functions. That way the reference within the
eb66e842 8146 -- predicate expression is the same in both functions.
d9f6a4ee 8147
9c20237a 8148 Object_Entity : Entity_Id;
eb66e842 8149 -- Entity for argument of Predicate procedure
d9f6a4ee 8150
9c20237a 8151 Object_Entity_M : Entity_Id;
8152 -- Entity for argument of separate Predicate procedure when exceptions
8153 -- are present in expression.
8154
02e5d0d0 8155 FDecl : Node_Id;
8156 -- The function declaration
9c20237a 8157
02e5d0d0 8158 SId : Entity_Id;
8159 -- Its entity
d9f6a4ee 8160
eb66e842 8161 Raise_Expression_Present : Boolean := False;
8162 -- Set True if Expr has at least one Raise_Expression
d9f6a4ee 8163
75491446 8164 procedure Add_Condition (Cond : Node_Id);
8165 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8166 -- Expr is empty).
d9f6a4ee 8167
eb66e842 8168 procedure Add_Predicates;
8169 -- Appends expressions for any Predicate pragmas in the rep item chain
8170 -- Typ to Expr. Note that we look only at items for this exact entity.
8171 -- Inheritance of predicates for the parent type is done by calling the
8172 -- Predicate_Function of the parent type, using Add_Call above.
d9f6a4ee 8173
75491446 8174 procedure Add_Call (T : Entity_Id);
8175 -- Includes a call to the predicate function for type T in Expr if T
8176 -- has predicates and Predicate_Function (T) is non-empty.
8177
eb66e842 8178 function Process_RE (N : Node_Id) return Traverse_Result;
8179 -- Used in Process REs, tests if node N is a raise expression, and if
8180 -- so, marks it to be converted to return False.
d9f6a4ee 8181
eb66e842 8182 procedure Process_REs is new Traverse_Proc (Process_RE);
8183 -- Marks any raise expressions in Expr_M to return False
d9f6a4ee 8184
f9e26ff7 8185 function Test_RE (N : Node_Id) return Traverse_Result;
8186 -- Used in Test_REs, tests one node for being a raise expression, and if
8187 -- so sets Raise_Expression_Present True.
8188
8189 procedure Test_REs is new Traverse_Proc (Test_RE);
8190 -- Tests to see if Expr contains any raise expressions
8191
eb66e842 8192 --------------
8193 -- Add_Call --
8194 --------------
d9f6a4ee 8195
eb66e842 8196 procedure Add_Call (T : Entity_Id) is
8197 Exp : Node_Id;
d9f6a4ee 8198
eb66e842 8199 begin
8200 if Present (T) and then Present (Predicate_Function (T)) then
8201 Set_Has_Predicates (Typ);
d9f6a4ee 8202
eb66e842 8203 -- Build the call to the predicate function of T
d9f6a4ee 8204
eb66e842 8205 Exp :=
8206 Make_Predicate_Call
8207 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
d9f6a4ee 8208
75491446 8209 -- "and"-in the call to evolving expression
d9f6a4ee 8210
75491446 8211 Add_Condition (Exp);
d9f6a4ee 8212
eb66e842 8213 -- Output info message on inheritance if required. Note we do not
8214 -- give this information for generic actual types, since it is
8215 -- unwelcome noise in that case in instantiations. We also
8216 -- generally suppress the message in instantiations, and also
8217 -- if it involves internal names.
d9f6a4ee 8218
eb66e842 8219 if Opt.List_Inherited_Aspects
8220 and then not Is_Generic_Actual_Type (Typ)
8221 and then Instantiation_Depth (Sloc (Typ)) = 0
8222 and then not Is_Internal_Name (Chars (T))
8223 and then not Is_Internal_Name (Chars (Typ))
8224 then
8225 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8226 Error_Msg_Node_2 := T;
8227 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8228 end if;
8229 end if;
8230 end Add_Call;
d9f6a4ee 8231
75491446 8232 -------------------
8233 -- Add_Condition --
8234 -------------------
8235
8236 procedure Add_Condition (Cond : Node_Id) is
8237 begin
8238 -- This is the first predicate expression
8239
8240 if No (Expr) then
8241 Expr := Cond;
8242
8243 -- Otherwise concatenate to the existing predicate expressions by
8244 -- using "and then".
8245
8246 else
8247 Expr :=
8248 Make_And_Then (Loc,
8249 Left_Opnd => Relocate_Node (Expr),
8250 Right_Opnd => Cond);
8251 end if;
8252 end Add_Condition;
8253
eb66e842 8254 --------------------
8255 -- Add_Predicates --
8256 --------------------
d9f6a4ee 8257
eb66e842 8258 procedure Add_Predicates is
f9e26ff7 8259 procedure Add_Predicate (Prag : Node_Id);
8260 -- Concatenate the expression of predicate pragma Prag to Expr by
8261 -- using a short circuit "and then" operator.
d9f6a4ee 8262
f9e26ff7 8263 -------------------
8264 -- Add_Predicate --
8265 -------------------
d9f6a4ee 8266
f9e26ff7 8267 procedure Add_Predicate (Prag : Node_Id) is
8268 procedure Replace_Type_Reference (N : Node_Id);
8269 -- Replace a single occurrence N of the subtype name with a
8270 -- reference to the formal of the predicate function. N can be an
8271 -- identifier referencing the subtype, or a selected component,
8272 -- representing an appropriately qualified occurrence of the
8273 -- subtype name.
8274
8275 procedure Replace_Type_References is
8276 new Replace_Type_References_Generic (Replace_Type_Reference);
8277 -- Traverse an expression changing every occurrence of an
8278 -- identifier whose name matches the name of the subtype with a
8279 -- reference to the formal parameter of the predicate function.
8280
8281 ----------------------------
8282 -- Replace_Type_Reference --
8283 ----------------------------
8284
8285 procedure Replace_Type_Reference (N : Node_Id) is
8286 begin
8287 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8288 -- Use the Sloc of the usage name, not the defining name
d9f6a4ee 8289
f9e26ff7 8290 Set_Etype (N, Typ);
8291 Set_Entity (N, Object_Entity);
d97beb2f 8292
f9e26ff7 8293 -- We want to treat the node as if it comes from source, so
8294 -- that ASIS will not ignore it.
d97beb2f 8295
f9e26ff7 8296 Set_Comes_From_Source (N, True);
8297 end Replace_Type_Reference;
d97beb2f 8298
f9e26ff7 8299 -- Local variables
d97beb2f 8300
f9e26ff7 8301 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8302 Arg1 : Node_Id;
8303 Arg2 : Node_Id;
d97beb2f 8304
f9e26ff7 8305 -- Start of processing for Add_Predicate
24c8d764 8306
f9e26ff7 8307 begin
8308 -- Extract the arguments of the pragma. The expression itself
8309 -- is copied for use in the predicate function, to preserve the
8310 -- original version for ASIS use.
d97beb2f 8311
f9e26ff7 8312 Arg1 := First (Pragma_Argument_Associations (Prag));
8313 Arg2 := Next (Arg1);
d97beb2f 8314
f9e26ff7 8315 Arg1 := Get_Pragma_Arg (Arg1);
8316 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
d97beb2f 8317
f9e26ff7 8318 -- When the predicate pragma applies to the current type or its
8319 -- full view, replace all occurrences of the subtype name with
8320 -- references to the formal parameter of the predicate function.
639c3741 8321
f9e26ff7 8322 if Entity (Arg1) = Typ
8323 or else Full_View (Entity (Arg1)) = Typ
8324 then
8325 Replace_Type_References (Arg2, Typ);
639c3741 8326
f9e26ff7 8327 -- If the predicate pragma comes from an aspect, replace the
8328 -- saved expression because we need the subtype references
8329 -- replaced for the calls to Preanalyze_Spec_Expression in
8330 -- Check_Aspect_At_xxx routines.
639c3741 8331
f9e26ff7 8332 if Present (Asp) then
f9e26ff7 8333 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8334 end if;
24c8d764 8335
75491446 8336 -- "and"-in the Arg2 condition to evolving expression
639c3741 8337
75491446 8338 Add_Condition (Relocate_Node (Arg2));
f9e26ff7 8339 end if;
8340 end Add_Predicate;
737e8460 8341
f9e26ff7 8342 -- Local variables
737e8460 8343
f9e26ff7 8344 Ritem : Node_Id;
d97beb2f 8345
f9e26ff7 8346 -- Start of processing for Add_Predicates
d97beb2f 8347
f9e26ff7 8348 begin
8349 Ritem := First_Rep_Item (Typ);
8350 while Present (Ritem) loop
8351 if Nkind (Ritem) = N_Pragma
8352 and then Pragma_Name (Ritem) = Name_Predicate
8353 then
8354 Add_Predicate (Ritem);
0ea02224 8355
8356 -- If the type is declared in an inner package it may be frozen
8357 -- outside of the package, and the generated pragma has not been
8358 -- analyzed yet, so capture the expression for the predicate
8359 -- function at this point.
8360
8361 elsif Nkind (Ritem) = N_Aspect_Specification
238921ae 8362 and then Present (Aspect_Rep_Item (Ritem))
8363 and then Scope (Typ) /= Current_Scope
0ea02224 8364 then
8365 declare
8366 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8367
8368 begin
8369 if Nkind (Prag) = N_Pragma
8370 and then Pragma_Name (Prag) = Name_Predicate
8371 then
8372 Add_Predicate (Prag);
8373 end if;
8374 end;
eb66e842 8375 end if;
d97beb2f 8376
eb66e842 8377 Next_Rep_Item (Ritem);
8378 end loop;
8379 end Add_Predicates;
d97beb2f 8380
eb66e842 8381 ----------------
8382 -- Process_RE --
8383 ----------------
d97beb2f 8384
eb66e842 8385 function Process_RE (N : Node_Id) return Traverse_Result is
d9f6a4ee 8386 begin
eb66e842 8387 if Nkind (N) = N_Raise_Expression then
8388 Set_Convert_To_Return_False (N);
8389 return Skip;
d9f6a4ee 8390 else
eb66e842 8391 return OK;
d9f6a4ee 8392 end if;
eb66e842 8393 end Process_RE;
d7c2851f 8394
d9f6a4ee 8395 -------------
eb66e842 8396 -- Test_RE --
d9f6a4ee 8397 -------------
d7c2851f 8398
eb66e842 8399 function Test_RE (N : Node_Id) return Traverse_Result is
d97beb2f 8400 begin
eb66e842 8401 if Nkind (N) = N_Raise_Expression then
8402 Raise_Expression_Present := True;
8403 return Abandon;
8404 else
8405 return OK;
8406 end if;
8407 end Test_RE;
d97beb2f 8408
f9e26ff7 8409 -- Local variables
8410
30f8d103 8411 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
f9e26ff7 8412
eb66e842 8413 -- Start of processing for Build_Predicate_Functions
d97beb2f 8414
eb66e842 8415 begin
8416 -- Return if already built or if type does not have predicates
9dc88aea 8417
9c20237a 8418 SId := Predicate_Function (Typ);
eb66e842 8419 if not Has_Predicates (Typ)
9c20237a 8420 or else (Present (SId) and then Has_Completion (SId))
eb66e842 8421 then
8422 return;
8423 end if;
d9f6a4ee 8424
30f8d103 8425 -- The related type may be subject to pragma Ghost. Set the mode now to
8426 -- ensure that the predicate functions are properly marked as Ghost.
f9e26ff7 8427
8428 Set_Ghost_Mode_From_Entity (Typ);
8429
eb66e842 8430 -- Prepare to construct predicate expression
d97beb2f 8431
eb66e842 8432 Expr := Empty;
d97beb2f 8433
9c20237a 8434 if Present (SId) then
8435 FDecl := Unit_Declaration_Node (SId);
8436
8437 else
8438 FDecl := Build_Predicate_Function_Declaration (Typ);
8439 SId := Defining_Entity (FDecl);
8440 end if;
8441
8442 -- Recover name of formal parameter of function that replaces references
8443 -- to the type in predicate expressions.
8444
8445 Object_Entity :=
8446 Defining_Identifier
8447 (First (Parameter_Specifications (Specification (FDecl))));
8448
8449 Object_Name := Chars (Object_Entity);
8450 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8451
75491446 8452 -- Add predicates for ancestor if present. These must come before the
8453 -- ones for the current type, as required by AI12-0071-1.
d97beb2f 8454
eb66e842 8455 declare
8456 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
d9f6a4ee 8457 begin
eb66e842 8458 if Present (Atyp) then
8459 Add_Call (Atyp);
8460 end if;
8461 end;
02e5d0d0 8462
75491446 8463 -- Add Predicates for the current type
8464
8465 Add_Predicates;
8466
eb66e842 8467 -- Case where predicates are present
9dc88aea 8468
eb66e842 8469 if Present (Expr) then
726fd56a 8470
eb66e842 8471 -- Test for raise expression present
726fd56a 8472
eb66e842 8473 Test_REs (Expr);
9dc88aea 8474
eb66e842 8475 -- If raise expression is present, capture a copy of Expr for use
8476 -- in building the predicateM function version later on. For this
8477 -- copy we replace references to Object_Entity by Object_Entity_M.
9dc88aea 8478
eb66e842 8479 if Raise_Expression_Present then
8480 declare
299b347e 8481 Map : constant Elist_Id := New_Elmt_List;
8482 New_V : Entity_Id := Empty;
8483
8484 -- The unanalyzed expression will be copied and appear in
8485 -- both functions. Normally expressions do not declare new
8486 -- entities, but quantified expressions do, so we need to
8487 -- create new entities for their bound variables, to prevent
8488 -- multiple definitions in gigi.
8489
8490 function Reset_Loop_Variable (N : Node_Id)
8491 return Traverse_Result;
8492
8493 procedure Collect_Loop_Variables is
8494 new Traverse_Proc (Reset_Loop_Variable);
8495
8496 ------------------------
8497 -- Reset_Loop_Variable --
8498 ------------------------
8499
8500 function Reset_Loop_Variable (N : Node_Id)
8501 return Traverse_Result
8502 is
8503 begin
8504 if Nkind (N) = N_Iterator_Specification then
8505 New_V := Make_Defining_Identifier
8506 (Sloc (N), Chars (Defining_Identifier (N)));
8507
8508 Set_Defining_Identifier (N, New_V);
8509 end if;
8510
8511 return OK;
8512 end Reset_Loop_Variable;
8513
eb66e842 8514 begin
8515 Append_Elmt (Object_Entity, Map);
8516 Append_Elmt (Object_Entity_M, Map);
8517 Expr_M := New_Copy_Tree (Expr, Map => Map);
299b347e 8518 Collect_Loop_Variables (Expr_M);
eb66e842 8519 end;
8520 end if;
d97beb2f 8521
eb66e842 8522 -- Build the main predicate function
9dc88aea 8523
eb66e842 8524 declare
eb66e842 8525 SIdB : constant Entity_Id :=
8526 Make_Defining_Identifier (Loc,
8527 Chars => New_External_Name (Chars (Typ), "Predicate"));
8528 -- The entity for the function body
9dc88aea 8529
eb66e842 8530 Spec : Node_Id;
eb66e842 8531 FBody : Node_Id;
9dc88aea 8532
eb66e842 8533 begin
d97beb2f 8534
eb66e842 8535 -- The predicate function is shared between views of a type
d97beb2f 8536
eb66e842 8537 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8538 Set_Predicate_Function (Full_View (Typ), SId);
d97beb2f 8539 end if;
d97beb2f 8540
f9e26ff7 8541 -- Mark the predicate function explicitly as Ghost because it does
8542 -- not come from source.
8543
8544 if Ghost_Mode > None then
8545 Set_Is_Ghost_Entity (SId);
8546 end if;
8547
eb66e842 8548 -- Build function body
d97beb2f 8549
eb66e842 8550 Spec :=
8551 Make_Function_Specification (Loc,
8552 Defining_Unit_Name => SIdB,
8553 Parameter_Specifications => New_List (
8554 Make_Parameter_Specification (Loc,
8555 Defining_Identifier =>
8556 Make_Defining_Identifier (Loc, Object_Name),
8557 Parameter_Type =>
8558 New_Occurrence_Of (Typ, Loc))),
8559 Result_Definition =>
8560 New_Occurrence_Of (Standard_Boolean, Loc));
d97beb2f 8561
eb66e842 8562 FBody :=
8563 Make_Subprogram_Body (Loc,
8564 Specification => Spec,
8565 Declarations => Empty_List,
8566 Handled_Statement_Sequence =>
8567 Make_Handled_Sequence_Of_Statements (Loc,
8568 Statements => New_List (
8569 Make_Simple_Return_Statement (Loc,
8570 Expression => Expr))));
9dc88aea 8571
9c20237a 8572 -- If declaration has not been analyzed yet, Insert declaration
7db33803 8573 -- before freeze node. Insert body itself after freeze node.
9c20237a 8574
8575 if not Analyzed (FDecl) then
8576 Insert_Before_And_Analyze (N, FDecl);
8577 end if;
d97beb2f 8578
02e5d0d0 8579 Insert_After_And_Analyze (N, FBody);
6958c62c 8580
8581 -- Static predicate functions are always side-effect free, and
8582 -- in most cases dynamic predicate functions are as well. Mark
8583 -- them as such whenever possible, so redundant predicate checks
7dd0b9b3 8584 -- can be optimized. If there is a variable reference within the
8585 -- expression, the function is not pure.
b2e821de 8586
6958c62c 8587 if Expander_Active then
7dd0b9b3 8588 Set_Is_Pure (SId,
8589 Side_Effect_Free (Expr, Variable_Ref => True));
6958c62c 8590 Set_Is_Inlined (SId);
8591 end if;
d9f6a4ee 8592 end;
d97beb2f 8593
eb66e842 8594 -- Test for raise expressions present and if so build M version
d97beb2f 8595
eb66e842 8596 if Raise_Expression_Present then
8597 declare
8598 SId : constant Entity_Id :=
8599 Make_Defining_Identifier (Loc,
8600 Chars => New_External_Name (Chars (Typ), "PredicateM"));
c96806b2 8601 -- The entity for the function spec
d97beb2f 8602
eb66e842 8603 SIdB : constant Entity_Id :=
8604 Make_Defining_Identifier (Loc,
8605 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8606 -- The entity for the function body
b9e61b2a 8607
eb66e842 8608 Spec : Node_Id;
eb66e842 8609 FBody : Node_Id;
9c20237a 8610 FDecl : Node_Id;
eb66e842 8611 BTemp : Entity_Id;
d97beb2f 8612
eb66e842 8613 begin
8614 -- Mark any raise expressions for special expansion
d97beb2f 8615
eb66e842 8616 Process_REs (Expr_M);
d97beb2f 8617
eb66e842 8618 -- Build function declaration
d97beb2f 8619
eb66e842 8620 Set_Ekind (SId, E_Function);
8621 Set_Is_Predicate_Function_M (SId);
8622 Set_Predicate_Function_M (Typ, SId);
d97beb2f 8623
eb66e842 8624 -- The predicate function is shared between views of a type
d97beb2f 8625
eb66e842 8626 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8627 Set_Predicate_Function_M (Full_View (Typ), SId);
8628 end if;
9dc88aea 8629
f9e26ff7 8630 -- Mark the predicate function explicitly as Ghost because it
8631 -- does not come from source.
8632
8633 if Ghost_Mode > None then
8634 Set_Is_Ghost_Entity (SId);
8635 end if;
8636
eb66e842 8637 Spec :=
8638 Make_Function_Specification (Loc,
8639 Defining_Unit_Name => SId,
8640 Parameter_Specifications => New_List (
8641 Make_Parameter_Specification (Loc,
8642 Defining_Identifier => Object_Entity_M,
8643 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8644 Result_Definition =>
8645 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8646
eb66e842 8647 FDecl :=
8648 Make_Subprogram_Declaration (Loc,
8649 Specification => Spec);
9dc88aea 8650
eb66e842 8651 -- Build function body
9dc88aea 8652
eb66e842 8653 Spec :=
8654 Make_Function_Specification (Loc,
8655 Defining_Unit_Name => SIdB,
8656 Parameter_Specifications => New_List (
8657 Make_Parameter_Specification (Loc,
8658 Defining_Identifier =>
8659 Make_Defining_Identifier (Loc, Object_Name),
8660 Parameter_Type =>
8661 New_Occurrence_Of (Typ, Loc))),
8662 Result_Definition =>
8663 New_Occurrence_Of (Standard_Boolean, Loc));
9dc88aea 8664
eb66e842 8665 -- Build the body, we declare the boolean expression before
8666 -- doing the return, because we are not really confident of
8667 -- what happens if a return appears within a return.
9dc88aea 8668
eb66e842 8669 BTemp :=
8670 Make_Defining_Identifier (Loc,
8671 Chars => New_Internal_Name ('B'));
9dc88aea 8672
eb66e842 8673 FBody :=
8674 Make_Subprogram_Body (Loc,
8675 Specification => Spec,
9dc88aea 8676
eb66e842 8677 Declarations => New_List (
8678 Make_Object_Declaration (Loc,
8679 Defining_Identifier => BTemp,
8680 Constant_Present => True,
8681 Object_Definition =>
8682 New_Occurrence_Of (Standard_Boolean, Loc),
8683 Expression => Expr_M)),
d97beb2f 8684
eb66e842 8685 Handled_Statement_Sequence =>
8686 Make_Handled_Sequence_Of_Statements (Loc,
8687 Statements => New_List (
8688 Make_Simple_Return_Statement (Loc,
8689 Expression => New_Occurrence_Of (BTemp, Loc)))));
d97beb2f 8690
eb66e842 8691 -- Insert declaration before freeze node and body after
d97beb2f 8692
eb66e842 8693 Insert_Before_And_Analyze (N, FDecl);
8694 Insert_After_And_Analyze (N, FBody);
8695 end;
8696 end if;
9dc88aea 8697
3b23aaa0 8698 -- See if we have a static predicate. Note that the answer may be
8699 -- yes even if we have an explicit Dynamic_Predicate present.
9dc88aea 8700
3b23aaa0 8701 declare
94d896aa 8702 PS : Boolean;
3b23aaa0 8703 EN : Node_Id;
9dc88aea 8704
3b23aaa0 8705 begin
94d896aa 8706 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8707 PS := False;
8708 else
8709 PS := Is_Predicate_Static (Expr, Object_Name);
8710 end if;
8711
a360a0f7 8712 -- Case where we have a predicate-static aspect
9dc88aea 8713
3b23aaa0 8714 if PS then
9dc88aea 8715
3b23aaa0 8716 -- We don't set Has_Static_Predicate_Aspect, since we can have
8717 -- any of the three cases (Predicate, Dynamic_Predicate, or
8718 -- Static_Predicate) generating a predicate with an expression
a360a0f7 8719 -- that is predicate-static. We just indicate that we have a
3b23aaa0 8720 -- predicate that can be treated as static.
d7c2851f 8721
3b23aaa0 8722 Set_Has_Static_Predicate (Typ);
d7c2851f 8723
3b23aaa0 8724 -- For discrete subtype, build the static predicate list
9dc88aea 8725
3b23aaa0 8726 if Is_Discrete_Type (Typ) then
8727 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8728
8729 -- If we don't get a static predicate list, it means that we
8730 -- have a case where this is not possible, most typically in
8731 -- the case where we inherit a dynamic predicate. We do not
8732 -- consider this an error, we just leave the predicate as
8733 -- dynamic. But if we do succeed in building the list, then
8734 -- we mark the predicate as static.
8735
5c6a5792 8736 if No (Static_Discrete_Predicate (Typ)) then
3b23aaa0 8737 Set_Has_Static_Predicate (Typ, False);
8738 end if;
94d896aa 8739
8740 -- For real or string subtype, save predicate expression
8741
8742 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8743 Set_Static_Real_Or_String_Predicate (Typ, Expr);
3b23aaa0 8744 end if;
8745
8746 -- Case of dynamic predicate (expression is not predicate-static)
9dc88aea 8747
eb66e842 8748 else
3b23aaa0 8749 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8750 -- is only set if we have an explicit Dynamic_Predicate aspect
8751 -- given. Here we may simply have a Predicate aspect where the
8752 -- expression happens not to be predicate-static.
8753
8754 -- Emit an error when the predicate is categorized as static
8755 -- but its expression is not predicate-static.
8756
8757 -- First a little fiddling to get a nice location for the
8758 -- message. If the expression is of the form (A and then B),
75491446 8759 -- where A is an inherited predicate, then use the right
8760 -- operand for the Sloc. This avoids getting confused by a call
8761 -- to an inherited predicate with a less convenient source
8762 -- location.
3b23aaa0 8763
8764 EN := Expr;
75491446 8765 while Nkind (EN) = N_And_Then
8766 and then Nkind (Left_Opnd (EN)) = N_Function_Call
8767 and then Is_Predicate_Function
8768 (Entity (Name (Left_Opnd (EN))))
8769 loop
8770 EN := Right_Opnd (EN);
3b23aaa0 8771 end loop;
8772
8773 -- Now post appropriate message
8774
8775 if Has_Static_Predicate_Aspect (Typ) then
94d896aa 8776 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
3b23aaa0 8777 Error_Msg_F
26279d91 8778 ("expression is not predicate-static (RM 3.2.4(16-22))",
3b23aaa0 8779 EN);
8780 else
94d896aa 8781 Error_Msg_F
8782 ("static predicate requires scalar or string type", EN);
3b23aaa0 8783 end if;
8784 end if;
eb66e842 8785 end if;
3b23aaa0 8786 end;
eb66e842 8787 end if;
f9e26ff7 8788
30f8d103 8789 Ghost_Mode := Save_Ghost_Mode;
eb66e842 8790 end Build_Predicate_Functions;
9dc88aea 8791
9c20237a 8792 ------------------------------------------
8793 -- Build_Predicate_Function_Declaration --
8794 ------------------------------------------
8795
8796 function Build_Predicate_Function_Declaration
8797 (Typ : Entity_Id) return Node_Id
8798 is
8799 Loc : constant Source_Ptr := Sloc (Typ);
8800
8801 Object_Entity : constant Entity_Id :=
02e5d0d0 8802 Make_Defining_Identifier (Loc,
8803 Chars => New_Internal_Name ('I'));
9c20237a 8804
8805 -- The formal parameter of the function
8806
8807 SId : constant Entity_Id :=
8808 Make_Defining_Identifier (Loc,
8809 Chars => New_External_Name (Chars (Typ), "Predicate"));
8810
8811 -- The entity for the function spec
8812
8813 FDecl : Node_Id;
8814 Spec : Node_Id;
8815
8816 begin
8817 Spec :=
8818 Make_Function_Specification (Loc,
8819 Defining_Unit_Name => SId,
8820 Parameter_Specifications => New_List (
8821 Make_Parameter_Specification (Loc,
8822 Defining_Identifier => Object_Entity,
8823 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8824 Result_Definition =>
8825 New_Occurrence_Of (Standard_Boolean, Loc));
8826
8827 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
8828
8829 Set_Ekind (SId, E_Function);
8830 Set_Etype (SId, Standard_Boolean);
8831 Set_Is_Internal (SId);
8832 Set_Is_Predicate_Function (SId);
8833 Set_Predicate_Function (Typ, SId);
8834
eb48e231 8835 Insert_After (Parent (Typ), FDecl);
9c20237a 8836
8837 Analyze (FDecl);
8838
8839 return FDecl;
8840 end Build_Predicate_Function_Declaration;
8841
d9f6a4ee 8842 -----------------------------------------
8843 -- Check_Aspect_At_End_Of_Declarations --
8844 -----------------------------------------
9dc88aea 8845
d9f6a4ee 8846 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8847 Ent : constant Entity_Id := Entity (ASN);
8848 Ident : constant Node_Id := Identifier (ASN);
8849 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
d7c2851f 8850
d9f6a4ee 8851 End_Decl_Expr : constant Node_Id := Entity (Ident);
8852 -- Expression to be analyzed at end of declarations
d7c2851f 8853
d9f6a4ee 8854 Freeze_Expr : constant Node_Id := Expression (ASN);
8855 -- Expression from call to Check_Aspect_At_Freeze_Point
d7c2851f 8856
d9f6a4ee 8857 T : constant Entity_Id := Etype (Freeze_Expr);
8858 -- Type required for preanalyze call
d7c2851f 8859
d9f6a4ee 8860 Err : Boolean;
8861 -- Set False if error
9dc88aea 8862
d9f6a4ee 8863 -- On entry to this procedure, Entity (Ident) contains a copy of the
8864 -- original expression from the aspect, saved for this purpose, and
8865 -- but Expression (Ident) is a preanalyzed copy of the expression,
8866 -- preanalyzed just after the freeze point.
9dc88aea 8867
d9f6a4ee 8868 procedure Check_Overloaded_Name;
8869 -- For aspects whose expression is simply a name, this routine checks if
8870 -- the name is overloaded or not. If so, it verifies there is an
8871 -- interpretation that matches the entity obtained at the freeze point,
8872 -- otherwise the compiler complains.
9dc88aea 8873
d9f6a4ee 8874 ---------------------------
8875 -- Check_Overloaded_Name --
8876 ---------------------------
8877
8878 procedure Check_Overloaded_Name is
d97beb2f 8879 begin
d9f6a4ee 8880 if not Is_Overloaded (End_Decl_Expr) then
5ac76cee 8881 Err := not Is_Entity_Name (End_Decl_Expr)
8882 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
d9f6a4ee 8883
d97beb2f 8884 else
d9f6a4ee 8885 Err := True;
9dc88aea 8886
d9f6a4ee 8887 declare
8888 Index : Interp_Index;
8889 It : Interp;
9dc88aea 8890
d9f6a4ee 8891 begin
8892 Get_First_Interp (End_Decl_Expr, Index, It);
8893 while Present (It.Typ) loop
8894 if It.Nam = Entity (Freeze_Expr) then
8895 Err := False;
8896 exit;
8897 end if;
8898
8899 Get_Next_Interp (Index, It);
8900 end loop;
8901 end;
9dc88aea 8902 end if;
d9f6a4ee 8903 end Check_Overloaded_Name;
9dc88aea 8904
d9f6a4ee 8905 -- Start of processing for Check_Aspect_At_End_Of_Declarations
9dc88aea 8906
d9f6a4ee 8907 begin
da3cad01 8908 -- In an instance we do not perform the consistency check between freeze
8909 -- point and end of declarations, because it was done already in the
8910 -- analysis of the generic. Furthermore, the delayed analysis of an
8911 -- aspect of the instance may produce spurious errors when the generic
8912 -- is a child unit that references entities in the parent (which might
8913 -- not be in scope at the freeze point of the instance).
8914
8915 if In_Instance then
8916 return;
8917
d9f6a4ee 8918 -- Case of aspects Dimension, Dimension_System and Synchronization
9dc88aea 8919
da3cad01 8920 elsif A_Id = Aspect_Synchronization then
d9f6a4ee 8921 return;
d97beb2f 8922
d9f6a4ee 8923 -- Case of stream attributes, just have to compare entities. However,
8924 -- the expression is just a name (possibly overloaded), and there may
8925 -- be stream operations declared for unrelated types, so we just need
8926 -- to verify that one of these interpretations is the one available at
8927 -- at the freeze point.
9dc88aea 8928
d9f6a4ee 8929 elsif A_Id = Aspect_Input or else
f02a9a9a 8930 A_Id = Aspect_Output or else
8931 A_Id = Aspect_Read or else
8932 A_Id = Aspect_Write
d9f6a4ee 8933 then
8934 Analyze (End_Decl_Expr);
8935 Check_Overloaded_Name;
9dc88aea 8936
d9f6a4ee 8937 elsif A_Id = Aspect_Variable_Indexing or else
8938 A_Id = Aspect_Constant_Indexing or else
8939 A_Id = Aspect_Default_Iterator or else
8940 A_Id = Aspect_Iterator_Element
8941 then
8942 -- Make type unfrozen before analysis, to prevent spurious errors
8943 -- about late attributes.
9dc88aea 8944
d9f6a4ee 8945 Set_Is_Frozen (Ent, False);
8946 Analyze (End_Decl_Expr);
8947 Set_Is_Frozen (Ent, True);
9dc88aea 8948
d9f6a4ee 8949 -- If the end of declarations comes before any other freeze
8950 -- point, the Freeze_Expr is not analyzed: no check needed.
9dc88aea 8951
d9f6a4ee 8952 if Analyzed (Freeze_Expr) and then not In_Instance then
8953 Check_Overloaded_Name;
8954 else
8955 Err := False;
8956 end if;
55e8372b 8957
d9f6a4ee 8958 -- All other cases
55e8372b 8959
d9f6a4ee 8960 else
c1efebf9 8961 -- Indicate that the expression comes from an aspect specification,
8962 -- which is used in subsequent analysis even if expansion is off.
8963
8964 Set_Parent (End_Decl_Expr, ASN);
8965
d9f6a4ee 8966 -- In a generic context the aspect expressions have not been
8967 -- preanalyzed, so do it now. There are no conformance checks
8968 -- to perform in this case.
55e8372b 8969
d9f6a4ee 8970 if No (T) then
8971 Check_Aspect_At_Freeze_Point (ASN);
8972 return;
55e8372b 8973
d9f6a4ee 8974 -- The default values attributes may be defined in the private part,
8975 -- and the analysis of the expression may take place when only the
8976 -- partial view is visible. The expression must be scalar, so use
8977 -- the full view to resolve.
55e8372b 8978
d9f6a4ee 8979 elsif (A_Id = Aspect_Default_Value
8980 or else
8981 A_Id = Aspect_Default_Component_Value)
8982 and then Is_Private_Type (T)
8983 then
8984 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
c1efebf9 8985
d9f6a4ee 8986 else
8987 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8988 end if;
d97beb2f 8989
d9f6a4ee 8990 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8991 end if;
55e8372b 8992
c1efebf9 8993 -- Output error message if error. Force error on aspect specification
8994 -- even if there is an error on the expression itself.
55e8372b 8995
d9f6a4ee 8996 if Err then
8997 Error_Msg_NE
c1efebf9 8998 ("!visibility of aspect for& changes after freeze point",
d9f6a4ee 8999 ASN, Ent);
9000 Error_Msg_NE
9001 ("info: & is frozen here, aspects evaluated at this point??",
9002 Freeze_Node (Ent), Ent);
9003 end if;
9004 end Check_Aspect_At_End_Of_Declarations;
55e8372b 9005
d9f6a4ee 9006 ----------------------------------
9007 -- Check_Aspect_At_Freeze_Point --
9008 ----------------------------------
9dc88aea 9009
d9f6a4ee 9010 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
9011 Ident : constant Node_Id := Identifier (ASN);
9012 -- Identifier (use Entity field to save expression)
9dc88aea 9013
d9f6a4ee 9014 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
9dc88aea 9015
d9f6a4ee 9016 T : Entity_Id := Empty;
9017 -- Type required for preanalyze call
9dc88aea 9018
d9f6a4ee 9019 begin
9020 -- On entry to this procedure, Entity (Ident) contains a copy of the
9021 -- original expression from the aspect, saved for this purpose.
9dc88aea 9022
d9f6a4ee 9023 -- On exit from this procedure Entity (Ident) is unchanged, still
9024 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9025 -- of the expression, preanalyzed just after the freeze point.
d97beb2f 9026
d9f6a4ee 9027 -- Make a copy of the expression to be preanalyzed
d97beb2f 9028
d9f6a4ee 9029 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
d97beb2f 9030
d9f6a4ee 9031 -- Find type for preanalyze call
d97beb2f 9032
d9f6a4ee 9033 case A_Id is
9dc88aea 9034
d9f6a4ee 9035 -- No_Aspect should be impossible
d97beb2f 9036
d9f6a4ee 9037 when No_Aspect =>
9038 raise Program_Error;
9039
9040 -- Aspects taking an optional boolean argument
d97beb2f 9041
d9f6a4ee 9042 when Boolean_Aspects |
9043 Library_Unit_Aspects =>
9dc88aea 9044
d9f6a4ee 9045 T := Standard_Boolean;
d7c2851f 9046
d9f6a4ee 9047 -- Aspects corresponding to attribute definition clauses
9dc88aea 9048
d9f6a4ee 9049 when Aspect_Address =>
9050 T := RTE (RE_Address);
9dc88aea 9051
d9f6a4ee 9052 when Aspect_Attach_Handler =>
9053 T := RTE (RE_Interrupt_ID);
d7c2851f 9054
d9f6a4ee 9055 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9056 T := RTE (RE_Bit_Order);
d7c2851f 9057
d9f6a4ee 9058 when Aspect_Convention =>
9059 return;
d7c2851f 9060
d9f6a4ee 9061 when Aspect_CPU =>
9062 T := RTE (RE_CPU_Range);
d7c2851f 9063
d9f6a4ee 9064 -- Default_Component_Value is resolved with the component type
d7c2851f 9065
d9f6a4ee 9066 when Aspect_Default_Component_Value =>
9067 T := Component_Type (Entity (ASN));
d7c2851f 9068
647fab54 9069 when Aspect_Default_Storage_Pool =>
9070 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9071
d9f6a4ee 9072 -- Default_Value is resolved with the type entity in question
d7c2851f 9073
d9f6a4ee 9074 when Aspect_Default_Value =>
9075 T := Entity (ASN);
9dc88aea 9076
d9f6a4ee 9077 when Aspect_Dispatching_Domain =>
9078 T := RTE (RE_Dispatching_Domain);
9dc88aea 9079
d9f6a4ee 9080 when Aspect_External_Tag =>
9081 T := Standard_String;
9dc88aea 9082
d9f6a4ee 9083 when Aspect_External_Name =>
9084 T := Standard_String;
9dc88aea 9085
d9f6a4ee 9086 when Aspect_Link_Name =>
9087 T := Standard_String;
9dc88aea 9088
d9f6a4ee 9089 when Aspect_Priority | Aspect_Interrupt_Priority =>
9090 T := Standard_Integer;
d97beb2f 9091
d9f6a4ee 9092 when Aspect_Relative_Deadline =>
9093 T := RTE (RE_Time_Span);
d97beb2f 9094
d9f6a4ee 9095 when Aspect_Small =>
9096 T := Universal_Real;
490beba6 9097
d9f6a4ee 9098 -- For a simple storage pool, we have to retrieve the type of the
9099 -- pool object associated with the aspect's corresponding attribute
9100 -- definition clause.
490beba6 9101
d9f6a4ee 9102 when Aspect_Simple_Storage_Pool =>
9103 T := Etype (Expression (Aspect_Rep_Item (ASN)));
d97beb2f 9104
d9f6a4ee 9105 when Aspect_Storage_Pool =>
9106 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
d97beb2f 9107
d9f6a4ee 9108 when Aspect_Alignment |
9109 Aspect_Component_Size |
9110 Aspect_Machine_Radix |
9111 Aspect_Object_Size |
9112 Aspect_Size |
9113 Aspect_Storage_Size |
9114 Aspect_Stream_Size |
9115 Aspect_Value_Size =>
9116 T := Any_Integer;
9dc88aea 9117
04ae062f 9118 when Aspect_Linker_Section =>
9119 T := Standard_String;
9120
d9f6a4ee 9121 when Aspect_Synchronization =>
9122 return;
7d20685d 9123
d9f6a4ee 9124 -- Special case, the expression of these aspects is just an entity
9125 -- that does not need any resolution, so just analyze.
7d20685d 9126
d9f6a4ee 9127 when Aspect_Input |
9128 Aspect_Output |
9129 Aspect_Read |
9130 Aspect_Suppress |
9131 Aspect_Unsuppress |
9132 Aspect_Warnings |
9133 Aspect_Write =>
9134 Analyze (Expression (ASN));
9135 return;
7d20685d 9136
d9f6a4ee 9137 -- Same for Iterator aspects, where the expression is a function
9138 -- name. Legality rules are checked separately.
89f1e35c 9139
d9f6a4ee 9140 when Aspect_Constant_Indexing |
9141 Aspect_Default_Iterator |
9142 Aspect_Iterator_Element |
9143 Aspect_Variable_Indexing =>
9144 Analyze (Expression (ASN));
9145 return;
7d20685d 9146
b3f8228a 9147 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9148
9149 when Aspect_Iterable =>
3061ffde 9150 T := Entity (ASN);
9151
b3f8228a 9152 declare
a9f5fea7 9153 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
3061ffde 9154 Assoc : Node_Id;
9155 Expr : Node_Id;
a9f5fea7 9156
b3f8228a 9157 begin
a9f5fea7 9158 if Cursor = Any_Type then
9159 return;
9160 end if;
9161
b3f8228a 9162 Assoc := First (Component_Associations (Expression (ASN)));
9163 while Present (Assoc) loop
3061ffde 9164 Expr := Expression (Assoc);
9165 Analyze (Expr);
a9f5fea7 9166
9167 if not Error_Posted (Expr) then
9168 Resolve_Iterable_Operation
9169 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9170 end if;
9171
b3f8228a 9172 Next (Assoc);
9173 end loop;
9174 end;
3061ffde 9175
b3f8228a 9176 return;
9177
d9f6a4ee 9178 -- Invariant/Predicate take boolean expressions
7d20685d 9179
d9f6a4ee 9180 when Aspect_Dynamic_Predicate |
9181 Aspect_Invariant |
9182 Aspect_Predicate |
9183 Aspect_Static_Predicate |
9184 Aspect_Type_Invariant =>
9185 T := Standard_Boolean;
7d20685d 9186
fdec445e 9187 when Aspect_Predicate_Failure =>
9188 T := Standard_String;
9189
d9f6a4ee 9190 -- Here is the list of aspects that don't require delay analysis
89f1e35c 9191
d0849c23 9192 when Aspect_Abstract_State |
9193 Aspect_Annotate |
85ee12c0 9194 Aspect_Async_Readers |
9195 Aspect_Async_Writers |
d0849c23 9196 Aspect_Constant_After_Elaboration |
9197 Aspect_Contract_Cases |
9198 Aspect_Default_Initial_Condition |
9199 Aspect_Depends |
9200 Aspect_Dimension |
9201 Aspect_Dimension_System |
85ee12c0 9202 Aspect_Effective_Reads |
9203 Aspect_Effective_Writes |
d0849c23 9204 Aspect_Extensions_Visible |
9205 Aspect_Ghost |
9206 Aspect_Global |
9207 Aspect_Implicit_Dereference |
9208 Aspect_Initial_Condition |
9209 Aspect_Initializes |
9210 Aspect_Obsolescent |
9211 Aspect_Part_Of |
9212 Aspect_Post |
9213 Aspect_Postcondition |
9214 Aspect_Pre |
9215 Aspect_Precondition |
9216 Aspect_Refined_Depends |
9217 Aspect_Refined_Global |
9218 Aspect_Refined_Post |
9219 Aspect_Refined_State |
9220 Aspect_SPARK_Mode |
9221 Aspect_Test_Case |
85ee12c0 9222 Aspect_Unimplemented |
9223 Aspect_Volatile_Function =>
d9f6a4ee 9224 raise Program_Error;
2b184b2f 9225
d9f6a4ee 9226 end case;
2b184b2f 9227
d9f6a4ee 9228 -- Do the preanalyze call
2b184b2f 9229
d9f6a4ee 9230 Preanalyze_Spec_Expression (Expression (ASN), T);
9231 end Check_Aspect_At_Freeze_Point;
2b184b2f 9232
d9f6a4ee 9233 -----------------------------------
9234 -- Check_Constant_Address_Clause --
9235 -----------------------------------
2b184b2f 9236
d9f6a4ee 9237 procedure Check_Constant_Address_Clause
9238 (Expr : Node_Id;
9239 U_Ent : Entity_Id)
9240 is
9241 procedure Check_At_Constant_Address (Nod : Node_Id);
9242 -- Checks that the given node N represents a name whose 'Address is
9243 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9244 -- address value is the same at the point of declaration of U_Ent and at
9245 -- the time of elaboration of the address clause.
84ed7523 9246
d9f6a4ee 9247 procedure Check_Expr_Constants (Nod : Node_Id);
9248 -- Checks that Nod meets the requirements for a constant address clause
9249 -- in the sense of the enclosing procedure.
84ed7523 9250
d9f6a4ee 9251 procedure Check_List_Constants (Lst : List_Id);
9252 -- Check that all elements of list Lst meet the requirements for a
9253 -- constant address clause in the sense of the enclosing procedure.
84ed7523 9254
d9f6a4ee 9255 -------------------------------
9256 -- Check_At_Constant_Address --
9257 -------------------------------
84ed7523 9258
d9f6a4ee 9259 procedure Check_At_Constant_Address (Nod : Node_Id) is
9260 begin
9261 if Is_Entity_Name (Nod) then
9262 if Present (Address_Clause (Entity ((Nod)))) then
9263 Error_Msg_NE
9264 ("invalid address clause for initialized object &!",
9265 Nod, U_Ent);
9266 Error_Msg_NE
9267 ("address for& cannot" &
9268 " depend on another address clause! (RM 13.1(22))!",
9269 Nod, U_Ent);
84ed7523 9270
d9f6a4ee 9271 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9272 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9273 then
9274 Error_Msg_NE
9275 ("invalid address clause for initialized object &!",
9276 Nod, U_Ent);
9277 Error_Msg_Node_2 := U_Ent;
9278 Error_Msg_NE
9279 ("\& must be defined before & (RM 13.1(22))!",
9280 Nod, Entity (Nod));
9281 end if;
7d20685d 9282
d9f6a4ee 9283 elsif Nkind (Nod) = N_Selected_Component then
9284 declare
9285 T : constant Entity_Id := Etype (Prefix (Nod));
59f3e675 9286
d9f6a4ee 9287 begin
9288 if (Is_Record_Type (T)
9289 and then Has_Discriminants (T))
9290 or else
9291 (Is_Access_Type (T)
f02a9a9a 9292 and then Is_Record_Type (Designated_Type (T))
9293 and then Has_Discriminants (Designated_Type (T)))
d9f6a4ee 9294 then
9295 Error_Msg_NE
9296 ("invalid address clause for initialized object &!",
9297 Nod, U_Ent);
9298 Error_Msg_N
9299 ("\address cannot depend on component" &
9300 " of discriminated record (RM 13.1(22))!",
9301 Nod);
9302 else
9303 Check_At_Constant_Address (Prefix (Nod));
9304 end if;
9305 end;
89cc7147 9306
d9f6a4ee 9307 elsif Nkind (Nod) = N_Indexed_Component then
9308 Check_At_Constant_Address (Prefix (Nod));
9309 Check_List_Constants (Expressions (Nod));
89cc7147 9310
84ed7523 9311 else
d9f6a4ee 9312 Check_Expr_Constants (Nod);
84ed7523 9313 end if;
d9f6a4ee 9314 end Check_At_Constant_Address;
81b424ac 9315
d9f6a4ee 9316 --------------------------
9317 -- Check_Expr_Constants --
9318 --------------------------
7b9b2f05 9319
d9f6a4ee 9320 procedure Check_Expr_Constants (Nod : Node_Id) is
9321 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9322 Ent : Entity_Id := Empty;
7b9b2f05 9323
d9f6a4ee 9324 begin
9325 if Nkind (Nod) in N_Has_Etype
9326 and then Etype (Nod) = Any_Type
7b9b2f05 9327 then
d9f6a4ee 9328 return;
309c3053 9329 end if;
9330
d9f6a4ee 9331 case Nkind (Nod) is
9332 when N_Empty | N_Error =>
9333 return;
7d20685d 9334
d9f6a4ee 9335 when N_Identifier | N_Expanded_Name =>
9336 Ent := Entity (Nod);
7d20685d 9337
d9f6a4ee 9338 -- We need to look at the original node if it is different
9339 -- from the node, since we may have rewritten things and
9340 -- substituted an identifier representing the rewrite.
7d20685d 9341
d9f6a4ee 9342 if Original_Node (Nod) /= Nod then
9343 Check_Expr_Constants (Original_Node (Nod));
7d20685d 9344
d9f6a4ee 9345 -- If the node is an object declaration without initial
9346 -- value, some code has been expanded, and the expression
9347 -- is not constant, even if the constituents might be
9348 -- acceptable, as in A'Address + offset.
7d20685d 9349
d9f6a4ee 9350 if Ekind (Ent) = E_Variable
9351 and then
9352 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9353 and then
9354 No (Expression (Declaration_Node (Ent)))
9355 then
9356 Error_Msg_NE
9357 ("invalid address clause for initialized object &!",
9358 Nod, U_Ent);
89f1e35c 9359
d9f6a4ee 9360 -- If entity is constant, it may be the result of expanding
9361 -- a check. We must verify that its declaration appears
9362 -- before the object in question, else we also reject the
9363 -- address clause.
7d20685d 9364
d9f6a4ee 9365 elsif Ekind (Ent) = E_Constant
9366 and then In_Same_Source_Unit (Ent, U_Ent)
9367 and then Sloc (Ent) > Loc_U_Ent
9368 then
9369 Error_Msg_NE
9370 ("invalid address clause for initialized object &!",
9371 Nod, U_Ent);
9372 end if;
7d20685d 9373
d9f6a4ee 9374 return;
9375 end if;
7d20685d 9376
d9f6a4ee 9377 -- Otherwise look at the identifier and see if it is OK
7d20685d 9378
d9f6a4ee 9379 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9380 or else Is_Type (Ent)
9381 then
9382 return;
7d20685d 9383
f02a9a9a 9384 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9385
d9f6a4ee 9386 -- This is the case where we must have Ent defined before
9387 -- U_Ent. Clearly if they are in different units this
9388 -- requirement is met since the unit containing Ent is
9389 -- already processed.
7d20685d 9390
d9f6a4ee 9391 if not In_Same_Source_Unit (Ent, U_Ent) then
9392 return;
7d20685d 9393
d9f6a4ee 9394 -- Otherwise location of Ent must be before the location
9395 -- of U_Ent, that's what prior defined means.
7d20685d 9396
d9f6a4ee 9397 elsif Sloc (Ent) < Loc_U_Ent then
9398 return;
6c545057 9399
d9f6a4ee 9400 else
9401 Error_Msg_NE
9402 ("invalid address clause for initialized object &!",
9403 Nod, U_Ent);
9404 Error_Msg_Node_2 := U_Ent;
9405 Error_Msg_NE
9406 ("\& must be defined before & (RM 13.1(22))!",
9407 Nod, Ent);
9408 end if;
37c6e44c 9409
d9f6a4ee 9410 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9411 Check_Expr_Constants (Original_Node (Nod));
6c545057 9412
d9f6a4ee 9413 else
9414 Error_Msg_NE
9415 ("invalid address clause for initialized object &!",
9416 Nod, U_Ent);
3cdbaa5a 9417
d9f6a4ee 9418 if Comes_From_Source (Ent) then
9419 Error_Msg_NE
9420 ("\reference to variable& not allowed"
9421 & " (RM 13.1(22))!", Nod, Ent);
9422 else
9423 Error_Msg_N
9424 ("non-static expression not allowed"
9425 & " (RM 13.1(22))!", Nod);
9426 end if;
9427 end if;
3cdbaa5a 9428
d9f6a4ee 9429 when N_Integer_Literal =>
7f694ca2 9430
d9f6a4ee 9431 -- If this is a rewritten unchecked conversion, in a system
9432 -- where Address is an integer type, always use the base type
9433 -- for a literal value. This is user-friendly and prevents
9434 -- order-of-elaboration issues with instances of unchecked
9435 -- conversion.
3cdbaa5a 9436
d9f6a4ee 9437 if Nkind (Original_Node (Nod)) = N_Function_Call then
9438 Set_Etype (Nod, Base_Type (Etype (Nod)));
9439 end if;
e1cedbae 9440
d9f6a4ee 9441 when N_Real_Literal |
9442 N_String_Literal |
9443 N_Character_Literal =>
9444 return;
7d20685d 9445
d9f6a4ee 9446 when N_Range =>
9447 Check_Expr_Constants (Low_Bound (Nod));
9448 Check_Expr_Constants (High_Bound (Nod));
231eb581 9449
d9f6a4ee 9450 when N_Explicit_Dereference =>
9451 Check_Expr_Constants (Prefix (Nod));
231eb581 9452
d9f6a4ee 9453 when N_Indexed_Component =>
9454 Check_Expr_Constants (Prefix (Nod));
9455 Check_List_Constants (Expressions (Nod));
7d20685d 9456
d9f6a4ee 9457 when N_Slice =>
9458 Check_Expr_Constants (Prefix (Nod));
9459 Check_Expr_Constants (Discrete_Range (Nod));
cb4c311d 9460
d9f6a4ee 9461 when N_Selected_Component =>
9462 Check_Expr_Constants (Prefix (Nod));
6144c105 9463
d9f6a4ee 9464 when N_Attribute_Reference =>
9465 if Nam_In (Attribute_Name (Nod), Name_Address,
9466 Name_Access,
9467 Name_Unchecked_Access,
9468 Name_Unrestricted_Access)
9469 then
9470 Check_At_Constant_Address (Prefix (Nod));
6144c105 9471
d9f6a4ee 9472 else
9473 Check_Expr_Constants (Prefix (Nod));
9474 Check_List_Constants (Expressions (Nod));
9475 end if;
a7a4a7c2 9476
d9f6a4ee 9477 when N_Aggregate =>
9478 Check_List_Constants (Component_Associations (Nod));
9479 Check_List_Constants (Expressions (Nod));
7d20685d 9480
d9f6a4ee 9481 when N_Component_Association =>
9482 Check_Expr_Constants (Expression (Nod));
e1cedbae 9483
d9f6a4ee 9484 when N_Extension_Aggregate =>
9485 Check_Expr_Constants (Ancestor_Part (Nod));
9486 Check_List_Constants (Component_Associations (Nod));
9487 Check_List_Constants (Expressions (Nod));
3cdbaa5a 9488
d9f6a4ee 9489 when N_Null =>
9490 return;
3cdbaa5a 9491
d9f6a4ee 9492 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9493 Check_Expr_Constants (Left_Opnd (Nod));
9494 Check_Expr_Constants (Right_Opnd (Nod));
e1cedbae 9495
d9f6a4ee 9496 when N_Unary_Op =>
9497 Check_Expr_Constants (Right_Opnd (Nod));
7f694ca2 9498
d9f6a4ee 9499 when N_Type_Conversion |
9500 N_Qualified_Expression |
9501 N_Allocator |
9502 N_Unchecked_Type_Conversion =>
9503 Check_Expr_Constants (Expression (Nod));
47a46747 9504
d9f6a4ee 9505 when N_Function_Call =>
9506 if not Is_Pure (Entity (Name (Nod))) then
9507 Error_Msg_NE
9508 ("invalid address clause for initialized object &!",
9509 Nod, U_Ent);
7f694ca2 9510
d9f6a4ee 9511 Error_Msg_NE
9512 ("\function & is not pure (RM 13.1(22))!",
9513 Nod, Entity (Name (Nod)));
b55f7641 9514
d9f6a4ee 9515 else
9516 Check_List_Constants (Parameter_Associations (Nod));
9517 end if;
b55f7641 9518
d9f6a4ee 9519 when N_Parameter_Association =>
9520 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7d20685d 9521
d9f6a4ee 9522 when others =>
9523 Error_Msg_NE
9524 ("invalid address clause for initialized object &!",
9525 Nod, U_Ent);
9526 Error_Msg_NE
9527 ("\must be constant defined before& (RM 13.1(22))!",
9528 Nod, U_Ent);
9529 end case;
9530 end Check_Expr_Constants;
7d20685d 9531
d9f6a4ee 9532 --------------------------
9533 -- Check_List_Constants --
9534 --------------------------
89f1e35c 9535
d9f6a4ee 9536 procedure Check_List_Constants (Lst : List_Id) is
9537 Nod1 : Node_Id;
7d20685d 9538
d9f6a4ee 9539 begin
9540 if Present (Lst) then
9541 Nod1 := First (Lst);
9542 while Present (Nod1) loop
9543 Check_Expr_Constants (Nod1);
9544 Next (Nod1);
9545 end loop;
9546 end if;
9547 end Check_List_Constants;
81b424ac 9548
d9f6a4ee 9549 -- Start of processing for Check_Constant_Address_Clause
81b424ac 9550
d9f6a4ee 9551 begin
9552 -- If rep_clauses are to be ignored, no need for legality checks. In
9c7948d7 9553 -- particular, no need to pester user about rep clauses that violate the
9554 -- rule on constant addresses, given that these clauses will be removed
9555 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9556 -- we want to relax these checks.
7d20685d 9557
f1a9be43 9558 if not Ignore_Rep_Clauses and not CodePeer_Mode then
d9f6a4ee 9559 Check_Expr_Constants (Expr);
9560 end if;
9561 end Check_Constant_Address_Clause;
7d20685d 9562
6653b695 9563 ---------------------------
9564 -- Check_Pool_Size_Clash --
9565 ---------------------------
9566
9567 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9568 Post : Node_Id;
9569
9570 begin
9571 -- We need to find out which one came first. Note that in the case of
9572 -- aspects mixed with pragmas there are cases where the processing order
9573 -- is reversed, which is why we do the check here.
9574
9575 if Sloc (SP) < Sloc (SS) then
9576 Error_Msg_Sloc := Sloc (SP);
9577 Post := SS;
9578 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9579
9580 else
9581 Error_Msg_Sloc := Sloc (SS);
9582 Post := SP;
9583 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9584 end if;
9585
9586 Error_Msg_N
9587 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9588 end Check_Pool_Size_Clash;
9589
d9f6a4ee 9590 ----------------------------------------
9591 -- Check_Record_Representation_Clause --
9592 ----------------------------------------
85696508 9593
d9f6a4ee 9594 procedure Check_Record_Representation_Clause (N : Node_Id) is
9595 Loc : constant Source_Ptr := Sloc (N);
9596 Ident : constant Node_Id := Identifier (N);
9597 Rectype : Entity_Id;
9598 Fent : Entity_Id;
9599 CC : Node_Id;
9600 Fbit : Uint;
9601 Lbit : Uint;
9602 Hbit : Uint := Uint_0;
9603 Comp : Entity_Id;
9604 Pcomp : Entity_Id;
89f1e35c 9605
d9f6a4ee 9606 Max_Bit_So_Far : Uint;
9607 -- Records the maximum bit position so far. If all field positions
9608 -- are monotonically increasing, then we can skip the circuit for
9609 -- checking for overlap, since no overlap is possible.
85696508 9610
d9f6a4ee 9611 Tagged_Parent : Entity_Id := Empty;
9612 -- This is set in the case of a derived tagged type for which we have
9613 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9614 -- positioned by record representation clauses). In this case we must
9615 -- check for overlap between components of this tagged type, and the
9616 -- components of its parent. Tagged_Parent will point to this parent
9617 -- type. For all other cases Tagged_Parent is left set to Empty.
7d20685d 9618
d9f6a4ee 9619 Parent_Last_Bit : Uint;
9620 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9621 -- last bit position for any field in the parent type. We only need to
9622 -- check overlap for fields starting below this point.
7d20685d 9623
d9f6a4ee 9624 Overlap_Check_Required : Boolean;
9625 -- Used to keep track of whether or not an overlap check is required
7d20685d 9626
d9f6a4ee 9627 Overlap_Detected : Boolean := False;
9628 -- Set True if an overlap is detected
d6f39728 9629
d9f6a4ee 9630 Ccount : Natural := 0;
9631 -- Number of component clauses in record rep clause
d6f39728 9632
d9f6a4ee 9633 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9634 -- Given two entities for record components or discriminants, checks
9635 -- if they have overlapping component clauses and issues errors if so.
d6f39728 9636
d9f6a4ee 9637 procedure Find_Component;
9638 -- Finds component entity corresponding to current component clause (in
9639 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9640 -- start/stop bits for the field. If there is no matching component or
9641 -- if the matching component does not have a component clause, then
9642 -- that's an error and Comp is set to Empty, but no error message is
9643 -- issued, since the message was already given. Comp is also set to
9644 -- Empty if the current "component clause" is in fact a pragma.
d6f39728 9645
d9f6a4ee 9646 -----------------------------
9647 -- Check_Component_Overlap --
9648 -----------------------------
9649
9650 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9651 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9652 CC2 : constant Node_Id := Component_Clause (C2_Ent);
d6f39728 9653
d6f39728 9654 begin
d9f6a4ee 9655 if Present (CC1) and then Present (CC2) then
d6f39728 9656
d9f6a4ee 9657 -- Exclude odd case where we have two tag components in the same
9658 -- record, both at location zero. This seems a bit strange, but
9659 -- it seems to happen in some circumstances, perhaps on an error.
9660
9661 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9662 return;
d6f39728 9663 end if;
9664
d9f6a4ee 9665 -- Here we check if the two fields overlap
9666
d6f39728 9667 declare
d9f6a4ee 9668 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9669 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9670 E1 : constant Uint := S1 + Esize (C1_Ent);
9671 E2 : constant Uint := S2 + Esize (C2_Ent);
d6f39728 9672
9673 begin
d9f6a4ee 9674 if E2 <= S1 or else E1 <= S2 then
9675 null;
d6f39728 9676 else
d9f6a4ee 9677 Error_Msg_Node_2 := Component_Name (CC2);
9678 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9679 Error_Msg_Node_1 := Component_Name (CC1);
9680 Error_Msg_N
9681 ("component& overlaps & #", Component_Name (CC1));
9682 Overlap_Detected := True;
d6f39728 9683 end if;
9684 end;
d6f39728 9685 end if;
d9f6a4ee 9686 end Check_Component_Overlap;
d6f39728 9687
d9f6a4ee 9688 --------------------
9689 -- Find_Component --
9690 --------------------
9dfe12ae 9691
d9f6a4ee 9692 procedure Find_Component is
9dfe12ae 9693
d9f6a4ee 9694 procedure Search_Component (R : Entity_Id);
9695 -- Search components of R for a match. If found, Comp is set
9dfe12ae 9696
d9f6a4ee 9697 ----------------------
9698 -- Search_Component --
9699 ----------------------
e7b2d6bc 9700
d9f6a4ee 9701 procedure Search_Component (R : Entity_Id) is
9702 begin
9703 Comp := First_Component_Or_Discriminant (R);
9704 while Present (Comp) loop
e7b2d6bc 9705
d9f6a4ee 9706 -- Ignore error of attribute name for component name (we
9707 -- already gave an error message for this, so no need to
9708 -- complain here)
e7b2d6bc 9709
d9f6a4ee 9710 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9711 null;
9712 else
9713 exit when Chars (Comp) = Chars (Component_Name (CC));
9dfe12ae 9714 end if;
9715
d9f6a4ee 9716 Next_Component_Or_Discriminant (Comp);
9717 end loop;
9718 end Search_Component;
d6f39728 9719
d9f6a4ee 9720 -- Start of processing for Find_Component
d6f39728 9721
d9f6a4ee 9722 begin
9723 -- Return with Comp set to Empty if we have a pragma
d6f39728 9724
d9f6a4ee 9725 if Nkind (CC) = N_Pragma then
9726 Comp := Empty;
9727 return;
9728 end if;
d6f39728 9729
d9f6a4ee 9730 -- Search current record for matching component
d6f39728 9731
d9f6a4ee 9732 Search_Component (Rectype);
9dfe12ae 9733
d9f6a4ee 9734 -- If not found, maybe component of base type discriminant that is
9735 -- absent from statically constrained first subtype.
e7b2d6bc 9736
d9f6a4ee 9737 if No (Comp) then
9738 Search_Component (Base_Type (Rectype));
9739 end if;
e7b2d6bc 9740
d9f6a4ee 9741 -- If no component, or the component does not reference the component
9742 -- clause in question, then there was some previous error for which
9743 -- we already gave a message, so just return with Comp Empty.
d6f39728 9744
d9f6a4ee 9745 if No (Comp) or else Component_Clause (Comp) /= CC then
9746 Check_Error_Detected;
9747 Comp := Empty;
93735cb8 9748
d9f6a4ee 9749 -- Normal case where we have a component clause
93735cb8 9750
d9f6a4ee 9751 else
9752 Fbit := Component_Bit_Offset (Comp);
9753 Lbit := Fbit + Esize (Comp) - 1;
9754 end if;
9755 end Find_Component;
93735cb8 9756
d9f6a4ee 9757 -- Start of processing for Check_Record_Representation_Clause
d6f39728 9758
d9f6a4ee 9759 begin
9760 Find_Type (Ident);
9761 Rectype := Entity (Ident);
d6f39728 9762
d9f6a4ee 9763 if Rectype = Any_Type then
9764 return;
9765 else
9766 Rectype := Underlying_Type (Rectype);
9767 end if;
d6f39728 9768
d9f6a4ee 9769 -- See if we have a fully repped derived tagged type
d6f39728 9770
d9f6a4ee 9771 declare
9772 PS : constant Entity_Id := Parent_Subtype (Rectype);
d6f39728 9773
d9f6a4ee 9774 begin
9775 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9776 Tagged_Parent := PS;
d6f39728 9777
d9f6a4ee 9778 -- Find maximum bit of any component of the parent type
d6f39728 9779
d9f6a4ee 9780 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9781 Pcomp := First_Entity (Tagged_Parent);
9782 while Present (Pcomp) loop
9783 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9784 if Component_Bit_Offset (Pcomp) /= No_Uint
9785 and then Known_Static_Esize (Pcomp)
9786 then
9787 Parent_Last_Bit :=
9788 UI_Max
9789 (Parent_Last_Bit,
9790 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9791 end if;
b7df4cda 9792 else
9793
9794 -- Skip anonymous types generated for constrained array
9795 -- or record components.
d9f6a4ee 9796
b7df4cda 9797 null;
d6f39728 9798 end if;
b7df4cda 9799
9800 Next_Entity (Pcomp);
d9f6a4ee 9801 end loop;
9802 end if;
9803 end;
d6f39728 9804
d9f6a4ee 9805 -- All done if no component clauses
d6f39728 9806
d9f6a4ee 9807 CC := First (Component_Clauses (N));
d6f39728 9808
d9f6a4ee 9809 if No (CC) then
9810 return;
9811 end if;
d6f39728 9812
d9f6a4ee 9813 -- If a tag is present, then create a component clause that places it
9814 -- at the start of the record (otherwise gigi may place it after other
9815 -- fields that have rep clauses).
d6f39728 9816
d9f6a4ee 9817 Fent := First_Entity (Rectype);
d6f39728 9818
d9f6a4ee 9819 if Nkind (Fent) = N_Defining_Identifier
9820 and then Chars (Fent) = Name_uTag
9821 then
9822 Set_Component_Bit_Offset (Fent, Uint_0);
9823 Set_Normalized_Position (Fent, Uint_0);
9824 Set_Normalized_First_Bit (Fent, Uint_0);
9825 Set_Normalized_Position_Max (Fent, Uint_0);
9826 Init_Esize (Fent, System_Address_Size);
d6f39728 9827
d9f6a4ee 9828 Set_Component_Clause (Fent,
9829 Make_Component_Clause (Loc,
9830 Component_Name => Make_Identifier (Loc, Name_uTag),
d6f39728 9831
d9f6a4ee 9832 Position => Make_Integer_Literal (Loc, Uint_0),
9833 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9834 Last_Bit =>
9835 Make_Integer_Literal (Loc,
9836 UI_From_Int (System_Address_Size))));
d6f39728 9837
d9f6a4ee 9838 Ccount := Ccount + 1;
9839 end if;
d6f39728 9840
d9f6a4ee 9841 Max_Bit_So_Far := Uint_Minus_1;
9842 Overlap_Check_Required := False;
d6f39728 9843
d9f6a4ee 9844 -- Process the component clauses
d6f39728 9845
d9f6a4ee 9846 while Present (CC) loop
9847 Find_Component;
d6f39728 9848
d9f6a4ee 9849 if Present (Comp) then
9850 Ccount := Ccount + 1;
d6f39728 9851
d9f6a4ee 9852 -- We need a full overlap check if record positions non-monotonic
d6f39728 9853
d9f6a4ee 9854 if Fbit <= Max_Bit_So_Far then
9855 Overlap_Check_Required := True;
9856 end if;
d6f39728 9857
d9f6a4ee 9858 Max_Bit_So_Far := Lbit;
d6f39728 9859
d9f6a4ee 9860 -- Check bit position out of range of specified size
01cb2726 9861
d9f6a4ee 9862 if Has_Size_Clause (Rectype)
9863 and then RM_Size (Rectype) <= Lbit
9864 then
9865 Error_Msg_N
9866 ("bit number out of range of specified size",
9867 Last_Bit (CC));
d6f39728 9868
d9f6a4ee 9869 -- Check for overlap with tag component
67278d60 9870
d9f6a4ee 9871 else
9872 if Is_Tagged_Type (Rectype)
9873 and then Fbit < System_Address_Size
9874 then
9875 Error_Msg_NE
9876 ("component overlaps tag field of&",
9877 Component_Name (CC), Rectype);
9878 Overlap_Detected := True;
9879 end if;
67278d60 9880
d9f6a4ee 9881 if Hbit < Lbit then
9882 Hbit := Lbit;
9883 end if;
9884 end if;
67278d60 9885
d9f6a4ee 9886 -- Check parent overlap if component might overlap parent field
67278d60 9887
d9f6a4ee 9888 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9889 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9890 while Present (Pcomp) loop
9891 if not Is_Tag (Pcomp)
9892 and then Chars (Pcomp) /= Name_uParent
9893 then
9894 Check_Component_Overlap (Comp, Pcomp);
9895 end if;
67278d60 9896
d9f6a4ee 9897 Next_Component_Or_Discriminant (Pcomp);
9898 end loop;
9899 end if;
9900 end if;
67278d60 9901
d9f6a4ee 9902 Next (CC);
9903 end loop;
47495553 9904
d9f6a4ee 9905 -- Now that we have processed all the component clauses, check for
9906 -- overlap. We have to leave this till last, since the components can
9907 -- appear in any arbitrary order in the representation clause.
67278d60 9908
d9f6a4ee 9909 -- We do not need this check if all specified ranges were monotonic,
9910 -- as recorded by Overlap_Check_Required being False at this stage.
67278d60 9911
d9f6a4ee 9912 -- This first section checks if there are any overlapping entries at
9913 -- all. It does this by sorting all entries and then seeing if there are
9914 -- any overlaps. If there are none, then that is decisive, but if there
9915 -- are overlaps, they may still be OK (they may result from fields in
9916 -- different variants).
67278d60 9917
d9f6a4ee 9918 if Overlap_Check_Required then
9919 Overlap_Check1 : declare
67278d60 9920
d9f6a4ee 9921 OC_Fbit : array (0 .. Ccount) of Uint;
9922 -- First-bit values for component clauses, the value is the offset
9923 -- of the first bit of the field from start of record. The zero
9924 -- entry is for use in sorting.
47495553 9925
d9f6a4ee 9926 OC_Lbit : array (0 .. Ccount) of Uint;
9927 -- Last-bit values for component clauses, the value is the offset
9928 -- of the last bit of the field from start of record. The zero
9929 -- entry is for use in sorting.
9930
9931 OC_Count : Natural := 0;
9932 -- Count of entries in OC_Fbit and OC_Lbit
67278d60 9933
d9f6a4ee 9934 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9935 -- Compare routine for Sort
67278d60 9936
d9f6a4ee 9937 procedure OC_Move (From : Natural; To : Natural);
9938 -- Move routine for Sort
67278d60 9939
d9f6a4ee 9940 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
67278d60 9941
d9f6a4ee 9942 -----------
9943 -- OC_Lt --
9944 -----------
67278d60 9945
d9f6a4ee 9946 function OC_Lt (Op1, Op2 : Natural) return Boolean is
67278d60 9947 begin
d9f6a4ee 9948 return OC_Fbit (Op1) < OC_Fbit (Op2);
9949 end OC_Lt;
67278d60 9950
d9f6a4ee 9951 -------------
9952 -- OC_Move --
9953 -------------
67278d60 9954
d9f6a4ee 9955 procedure OC_Move (From : Natural; To : Natural) is
9956 begin
9957 OC_Fbit (To) := OC_Fbit (From);
9958 OC_Lbit (To) := OC_Lbit (From);
9959 end OC_Move;
67278d60 9960
d9f6a4ee 9961 -- Start of processing for Overlap_Check
67278d60 9962
67278d60 9963 begin
d9f6a4ee 9964 CC := First (Component_Clauses (N));
9965 while Present (CC) loop
67278d60 9966
d9f6a4ee 9967 -- Exclude component clause already marked in error
67278d60 9968
d9f6a4ee 9969 if not Error_Posted (CC) then
9970 Find_Component;
9971
9972 if Present (Comp) then
9973 OC_Count := OC_Count + 1;
9974 OC_Fbit (OC_Count) := Fbit;
9975 OC_Lbit (OC_Count) := Lbit;
9976 end if;
67278d60 9977 end if;
9978
d9f6a4ee 9979 Next (CC);
67278d60 9980 end loop;
67278d60 9981
d9f6a4ee 9982 Sorting.Sort (OC_Count);
67278d60 9983
d9f6a4ee 9984 Overlap_Check_Required := False;
9985 for J in 1 .. OC_Count - 1 loop
9986 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9987 Overlap_Check_Required := True;
9988 exit;
9989 end if;
9990 end loop;
9991 end Overlap_Check1;
9992 end if;
67278d60 9993
d9f6a4ee 9994 -- If Overlap_Check_Required is still True, then we have to do the full
9995 -- scale overlap check, since we have at least two fields that do
9996 -- overlap, and we need to know if that is OK since they are in
9997 -- different variant, or whether we have a definite problem.
67278d60 9998
d9f6a4ee 9999 if Overlap_Check_Required then
10000 Overlap_Check2 : declare
10001 C1_Ent, C2_Ent : Entity_Id;
10002 -- Entities of components being checked for overlap
67278d60 10003
d9f6a4ee 10004 Clist : Node_Id;
10005 -- Component_List node whose Component_Items are being checked
67278d60 10006
d9f6a4ee 10007 Citem : Node_Id;
10008 -- Component declaration for component being checked
67278d60 10009
d9f6a4ee 10010 begin
10011 C1_Ent := First_Entity (Base_Type (Rectype));
67278d60 10012
d9f6a4ee 10013 -- Loop through all components in record. For each component check
10014 -- for overlap with any of the preceding elements on the component
10015 -- list containing the component and also, if the component is in
10016 -- a variant, check against components outside the case structure.
10017 -- This latter test is repeated recursively up the variant tree.
67278d60 10018
d9f6a4ee 10019 Main_Component_Loop : while Present (C1_Ent) loop
10020 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10021 goto Continue_Main_Component_Loop;
10022 end if;
67278d60 10023
d9f6a4ee 10024 -- Skip overlap check if entity has no declaration node. This
10025 -- happens with discriminants in constrained derived types.
10026 -- Possibly we are missing some checks as a result, but that
10027 -- does not seem terribly serious.
67278d60 10028
d9f6a4ee 10029 if No (Declaration_Node (C1_Ent)) then
10030 goto Continue_Main_Component_Loop;
10031 end if;
67278d60 10032
d9f6a4ee 10033 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
67278d60 10034
d9f6a4ee 10035 -- Loop through component lists that need checking. Check the
10036 -- current component list and all lists in variants above us.
67278d60 10037
d9f6a4ee 10038 Component_List_Loop : loop
67278d60 10039
d9f6a4ee 10040 -- If derived type definition, go to full declaration
10041 -- If at outer level, check discriminants if there are any.
67278d60 10042
d9f6a4ee 10043 if Nkind (Clist) = N_Derived_Type_Definition then
10044 Clist := Parent (Clist);
10045 end if;
67278d60 10046
d9f6a4ee 10047 -- Outer level of record definition, check discriminants
67278d60 10048
d9f6a4ee 10049 if Nkind_In (Clist, N_Full_Type_Declaration,
10050 N_Private_Type_Declaration)
67278d60 10051 then
d9f6a4ee 10052 if Has_Discriminants (Defining_Identifier (Clist)) then
10053 C2_Ent :=
10054 First_Discriminant (Defining_Identifier (Clist));
10055 while Present (C2_Ent) loop
10056 exit when C1_Ent = C2_Ent;
10057 Check_Component_Overlap (C1_Ent, C2_Ent);
10058 Next_Discriminant (C2_Ent);
10059 end loop;
10060 end if;
67278d60 10061
d9f6a4ee 10062 -- Record extension case
67278d60 10063
d9f6a4ee 10064 elsif Nkind (Clist) = N_Derived_Type_Definition then
10065 Clist := Empty;
67278d60 10066
d9f6a4ee 10067 -- Otherwise check one component list
67278d60 10068
d9f6a4ee 10069 else
10070 Citem := First (Component_Items (Clist));
10071 while Present (Citem) loop
10072 if Nkind (Citem) = N_Component_Declaration then
10073 C2_Ent := Defining_Identifier (Citem);
10074 exit when C1_Ent = C2_Ent;
10075 Check_Component_Overlap (C1_Ent, C2_Ent);
10076 end if;
67278d60 10077
d9f6a4ee 10078 Next (Citem);
10079 end loop;
10080 end if;
67278d60 10081
d9f6a4ee 10082 -- Check for variants above us (the parent of the Clist can
10083 -- be a variant, in which case its parent is a variant part,
10084 -- and the parent of the variant part is a component list
10085 -- whose components must all be checked against the current
10086 -- component for overlap).
67278d60 10087
d9f6a4ee 10088 if Nkind (Parent (Clist)) = N_Variant then
10089 Clist := Parent (Parent (Parent (Clist)));
67278d60 10090
d9f6a4ee 10091 -- Check for possible discriminant part in record, this
10092 -- is treated essentially as another level in the
10093 -- recursion. For this case the parent of the component
10094 -- list is the record definition, and its parent is the
10095 -- full type declaration containing the discriminant
10096 -- specifications.
10097
10098 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10099 Clist := Parent (Parent ((Clist)));
10100
10101 -- If neither of these two cases, we are at the top of
10102 -- the tree.
10103
10104 else
10105 exit Component_List_Loop;
10106 end if;
10107 end loop Component_List_Loop;
67278d60 10108
d9f6a4ee 10109 <<Continue_Main_Component_Loop>>
10110 Next_Entity (C1_Ent);
67278d60 10111
d9f6a4ee 10112 end loop Main_Component_Loop;
10113 end Overlap_Check2;
67278d60 10114 end if;
10115
d9f6a4ee 10116 -- The following circuit deals with warning on record holes (gaps). We
10117 -- skip this check if overlap was detected, since it makes sense for the
10118 -- programmer to fix this illegality before worrying about warnings.
67278d60 10119
d9f6a4ee 10120 if not Overlap_Detected and Warn_On_Record_Holes then
10121 Record_Hole_Check : declare
10122 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10123 -- Full declaration of record type
67278d60 10124
d9f6a4ee 10125 procedure Check_Component_List
10126 (CL : Node_Id;
10127 Sbit : Uint;
10128 DS : List_Id);
10129 -- Check component list CL for holes. The starting bit should be
10130 -- Sbit. which is zero for the main record component list and set
10131 -- appropriately for recursive calls for variants. DS is set to
10132 -- a list of discriminant specifications to be included in the
10133 -- consideration of components. It is No_List if none to consider.
67278d60 10134
d9f6a4ee 10135 --------------------------
10136 -- Check_Component_List --
10137 --------------------------
47495553 10138
d9f6a4ee 10139 procedure Check_Component_List
10140 (CL : Node_Id;
10141 Sbit : Uint;
10142 DS : List_Id)
10143 is
10144 Compl : Integer;
67278d60 10145
d9f6a4ee 10146 begin
10147 Compl := Integer (List_Length (Component_Items (CL)));
47495553 10148
d9f6a4ee 10149 if DS /= No_List then
10150 Compl := Compl + Integer (List_Length (DS));
10151 end if;
67278d60 10152
d9f6a4ee 10153 declare
10154 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10155 -- Gather components (zero entry is for sort routine)
67278d60 10156
d9f6a4ee 10157 Ncomps : Natural := 0;
10158 -- Number of entries stored in Comps (starting at Comps (1))
67278d60 10159
d9f6a4ee 10160 Citem : Node_Id;
10161 -- One component item or discriminant specification
67278d60 10162
d9f6a4ee 10163 Nbit : Uint;
10164 -- Starting bit for next component
67278d60 10165
d9f6a4ee 10166 CEnt : Entity_Id;
10167 -- Component entity
67278d60 10168
d9f6a4ee 10169 Variant : Node_Id;
10170 -- One variant
67278d60 10171
d9f6a4ee 10172 function Lt (Op1, Op2 : Natural) return Boolean;
10173 -- Compare routine for Sort
67278d60 10174
d9f6a4ee 10175 procedure Move (From : Natural; To : Natural);
10176 -- Move routine for Sort
67278d60 10177
d9f6a4ee 10178 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
67278d60 10179
d9f6a4ee 10180 --------
10181 -- Lt --
10182 --------
67278d60 10183
d9f6a4ee 10184 function Lt (Op1, Op2 : Natural) return Boolean is
10185 begin
10186 return Component_Bit_Offset (Comps (Op1))
10187 <
10188 Component_Bit_Offset (Comps (Op2));
10189 end Lt;
67278d60 10190
d9f6a4ee 10191 ----------
10192 -- Move --
10193 ----------
67278d60 10194
d9f6a4ee 10195 procedure Move (From : Natural; To : Natural) is
10196 begin
10197 Comps (To) := Comps (From);
10198 end Move;
67278d60 10199
d9f6a4ee 10200 begin
10201 -- Gather discriminants into Comp
67278d60 10202
d9f6a4ee 10203 if DS /= No_List then
10204 Citem := First (DS);
10205 while Present (Citem) loop
10206 if Nkind (Citem) = N_Discriminant_Specification then
10207 declare
10208 Ent : constant Entity_Id :=
10209 Defining_Identifier (Citem);
10210 begin
10211 if Ekind (Ent) = E_Discriminant then
10212 Ncomps := Ncomps + 1;
10213 Comps (Ncomps) := Ent;
10214 end if;
10215 end;
10216 end if;
67278d60 10217
d9f6a4ee 10218 Next (Citem);
10219 end loop;
10220 end if;
67278d60 10221
d9f6a4ee 10222 -- Gather component entities into Comp
67278d60 10223
d9f6a4ee 10224 Citem := First (Component_Items (CL));
10225 while Present (Citem) loop
10226 if Nkind (Citem) = N_Component_Declaration then
10227 Ncomps := Ncomps + 1;
10228 Comps (Ncomps) := Defining_Identifier (Citem);
10229 end if;
67278d60 10230
d9f6a4ee 10231 Next (Citem);
10232 end loop;
67278d60 10233
d9f6a4ee 10234 -- Now sort the component entities based on the first bit.
10235 -- Note we already know there are no overlapping components.
67278d60 10236
d9f6a4ee 10237 Sorting.Sort (Ncomps);
67278d60 10238
d9f6a4ee 10239 -- Loop through entries checking for holes
67278d60 10240
d9f6a4ee 10241 Nbit := Sbit;
10242 for J in 1 .. Ncomps loop
10243 CEnt := Comps (J);
10244 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
67278d60 10245
d9f6a4ee 10246 if Error_Msg_Uint_1 > 0 then
10247 Error_Msg_NE
10248 ("?H?^-bit gap before component&",
10249 Component_Name (Component_Clause (CEnt)), CEnt);
10250 end if;
67278d60 10251
d9f6a4ee 10252 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10253 end loop;
67278d60 10254
d9f6a4ee 10255 -- Process variant parts recursively if present
67278d60 10256
d9f6a4ee 10257 if Present (Variant_Part (CL)) then
10258 Variant := First (Variants (Variant_Part (CL)));
10259 while Present (Variant) loop
10260 Check_Component_List
10261 (Component_List (Variant), Nbit, No_List);
10262 Next (Variant);
10263 end loop;
67278d60 10264 end if;
d9f6a4ee 10265 end;
10266 end Check_Component_List;
67278d60 10267
d9f6a4ee 10268 -- Start of processing for Record_Hole_Check
67278d60 10269
d9f6a4ee 10270 begin
10271 declare
10272 Sbit : Uint;
67278d60 10273
d9f6a4ee 10274 begin
10275 if Is_Tagged_Type (Rectype) then
10276 Sbit := UI_From_Int (System_Address_Size);
10277 else
10278 Sbit := Uint_0;
10279 end if;
10280
10281 if Nkind (Decl) = N_Full_Type_Declaration
10282 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10283 then
10284 Check_Component_List
10285 (Component_List (Type_Definition (Decl)),
10286 Sbit,
10287 Discriminant_Specifications (Decl));
67278d60 10288 end if;
d9f6a4ee 10289 end;
10290 end Record_Hole_Check;
67278d60 10291 end if;
10292
d9f6a4ee 10293 -- For records that have component clauses for all components, and whose
10294 -- size is less than or equal to 32, we need to know the size in the
10295 -- front end to activate possible packed array processing where the
10296 -- component type is a record.
67278d60 10297
d9f6a4ee 10298 -- At this stage Hbit + 1 represents the first unused bit from all the
10299 -- component clauses processed, so if the component clauses are
10300 -- complete, then this is the length of the record.
67278d60 10301
d9f6a4ee 10302 -- For records longer than System.Storage_Unit, and for those where not
10303 -- all components have component clauses, the back end determines the
10304 -- length (it may for example be appropriate to round up the size
10305 -- to some convenient boundary, based on alignment considerations, etc).
67278d60 10306
d9f6a4ee 10307 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
67278d60 10308
d9f6a4ee 10309 -- Nothing to do if at least one component has no component clause
67278d60 10310
d9f6a4ee 10311 Comp := First_Component_Or_Discriminant (Rectype);
10312 while Present (Comp) loop
10313 exit when No (Component_Clause (Comp));
10314 Next_Component_Or_Discriminant (Comp);
10315 end loop;
67278d60 10316
d9f6a4ee 10317 -- If we fall out of loop, all components have component clauses
10318 -- and so we can set the size to the maximum value.
67278d60 10319
d9f6a4ee 10320 if No (Comp) then
10321 Set_RM_Size (Rectype, Hbit + 1);
10322 end if;
10323 end if;
10324 end Check_Record_Representation_Clause;
67278d60 10325
d9f6a4ee 10326 ----------------
10327 -- Check_Size --
10328 ----------------
67278d60 10329
d9f6a4ee 10330 procedure Check_Size
10331 (N : Node_Id;
10332 T : Entity_Id;
10333 Siz : Uint;
10334 Biased : out Boolean)
10335 is
f74a102b 10336 procedure Size_Too_Small_Error (Min_Siz : Uint);
10337 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10338 -- minimum size.
10339
10340 --------------------------
10341 -- Size_Too_Small_Error --
10342 --------------------------
10343
10344 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10345 begin
10346 -- This error is suppressed in ASIS mode to allow for different ASIS
f9906591 10347 -- back ends or ASIS-based tools to query the illegal clause.
f74a102b 10348
10349 if not ASIS_Mode then
10350 Error_Msg_Uint_1 := Min_Siz;
6d22398d 10351 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
f74a102b 10352 end if;
10353 end Size_Too_Small_Error;
10354
10355 -- Local variables
10356
d9f6a4ee 10357 UT : constant Entity_Id := Underlying_Type (T);
10358 M : Uint;
67278d60 10359
f74a102b 10360 -- Start of processing for Check_Size
10361
d9f6a4ee 10362 begin
10363 Biased := False;
67278d60 10364
f74a102b 10365 -- Reject patently improper size values
67278d60 10366
d9f6a4ee 10367 if Is_Elementary_Type (T)
10368 and then Siz > UI_From_Int (Int'Last)
10369 then
10370 Error_Msg_N ("Size value too large for elementary type", N);
67278d60 10371
d9f6a4ee 10372 if Nkind (Original_Node (N)) = N_Op_Expon then
10373 Error_Msg_N
10374 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10375 end if;
10376 end if;
67278d60 10377
d9f6a4ee 10378 -- Dismiss generic types
67278d60 10379
d9f6a4ee 10380 if Is_Generic_Type (T)
10381 or else
10382 Is_Generic_Type (UT)
10383 or else
10384 Is_Generic_Type (Root_Type (UT))
10385 then
10386 return;
67278d60 10387
d9f6a4ee 10388 -- Guard against previous errors
67278d60 10389
d9f6a4ee 10390 elsif No (UT) or else UT = Any_Type then
10391 Check_Error_Detected;
10392 return;
67278d60 10393
d9f6a4ee 10394 -- Check case of bit packed array
67278d60 10395
d9f6a4ee 10396 elsif Is_Array_Type (UT)
10397 and then Known_Static_Component_Size (UT)
10398 and then Is_Bit_Packed_Array (UT)
10399 then
10400 declare
10401 Asiz : Uint;
10402 Indx : Node_Id;
10403 Ityp : Entity_Id;
67278d60 10404
d9f6a4ee 10405 begin
10406 Asiz := Component_Size (UT);
10407 Indx := First_Index (UT);
10408 loop
10409 Ityp := Etype (Indx);
67278d60 10410
d9f6a4ee 10411 -- If non-static bound, then we are not in the business of
10412 -- trying to check the length, and indeed an error will be
10413 -- issued elsewhere, since sizes of non-static array types
10414 -- cannot be set implicitly or explicitly.
67278d60 10415
cda40848 10416 if not Is_OK_Static_Subtype (Ityp) then
d9f6a4ee 10417 return;
10418 end if;
67278d60 10419
d9f6a4ee 10420 -- Otherwise accumulate next dimension
67278d60 10421
d9f6a4ee 10422 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10423 Expr_Value (Type_Low_Bound (Ityp)) +
10424 Uint_1);
67278d60 10425
d9f6a4ee 10426 Next_Index (Indx);
10427 exit when No (Indx);
10428 end loop;
67278d60 10429
d9f6a4ee 10430 if Asiz <= Siz then
10431 return;
67278d60 10432
d9f6a4ee 10433 else
f74a102b 10434 Size_Too_Small_Error (Asiz);
d9f6a4ee 10435 Set_Esize (T, Asiz);
10436 Set_RM_Size (T, Asiz);
10437 end if;
10438 end;
67278d60 10439
d9f6a4ee 10440 -- All other composite types are ignored
67278d60 10441
d9f6a4ee 10442 elsif Is_Composite_Type (UT) then
10443 return;
47495553 10444
d9f6a4ee 10445 -- For fixed-point types, don't check minimum if type is not frozen,
10446 -- since we don't know all the characteristics of the type that can
10447 -- affect the size (e.g. a specified small) till freeze time.
47495553 10448
f74a102b 10449 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
d9f6a4ee 10450 null;
47495553 10451
d9f6a4ee 10452 -- Cases for which a minimum check is required
47495553 10453
d9f6a4ee 10454 else
10455 -- Ignore if specified size is correct for the type
47495553 10456
d9f6a4ee 10457 if Known_Esize (UT) and then Siz = Esize (UT) then
10458 return;
10459 end if;
47495553 10460
d9f6a4ee 10461 -- Otherwise get minimum size
47495553 10462
d9f6a4ee 10463 M := UI_From_Int (Minimum_Size (UT));
47495553 10464
d9f6a4ee 10465 if Siz < M then
47495553 10466
d9f6a4ee 10467 -- Size is less than minimum size, but one possibility remains
10468 -- that we can manage with the new size if we bias the type.
47495553 10469
d9f6a4ee 10470 M := UI_From_Int (Minimum_Size (UT, Biased => True));
47495553 10471
d9f6a4ee 10472 if Siz < M then
f74a102b 10473 Size_Too_Small_Error (M);
10474 Set_Esize (T, M);
d9f6a4ee 10475 Set_RM_Size (T, M);
10476 else
10477 Biased := True;
10478 end if;
10479 end if;
10480 end if;
10481 end Check_Size;
47495553 10482
d9f6a4ee 10483 --------------------------
10484 -- Freeze_Entity_Checks --
10485 --------------------------
47495553 10486
d9f6a4ee 10487 procedure Freeze_Entity_Checks (N : Node_Id) is
8cf481c9 10488 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10489 -- Inspect the primitive operations of type Typ and hide all pairs of
3118058b 10490 -- implicitly declared non-overridden non-fully conformant homographs
10491 -- (Ada RM 8.3 12.3/2).
8cf481c9 10492
10493 -------------------------------------
10494 -- Hide_Non_Overridden_Subprograms --
10495 -------------------------------------
10496
10497 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10498 procedure Hide_Matching_Homographs
10499 (Subp_Id : Entity_Id;
10500 Start_Elmt : Elmt_Id);
10501 -- Inspect a list of primitive operations starting with Start_Elmt
3118058b 10502 -- and find matching implicitly declared non-overridden non-fully
10503 -- conformant homographs of Subp_Id. If found, all matches along
10504 -- with Subp_Id are hidden from all visibility.
8cf481c9 10505
10506 function Is_Non_Overridden_Or_Null_Procedure
10507 (Subp_Id : Entity_Id) return Boolean;
10508 -- Determine whether subprogram Subp_Id is implicitly declared non-
10509 -- overridden subprogram or an implicitly declared null procedure.
10510
10511 ------------------------------
10512 -- Hide_Matching_Homographs --
10513 ------------------------------
10514
10515 procedure Hide_Matching_Homographs
10516 (Subp_Id : Entity_Id;
10517 Start_Elmt : Elmt_Id)
10518 is
10519 Prim : Entity_Id;
10520 Prim_Elmt : Elmt_Id;
10521
10522 begin
10523 Prim_Elmt := Start_Elmt;
10524 while Present (Prim_Elmt) loop
10525 Prim := Node (Prim_Elmt);
10526
10527 -- The current primitive is implicitly declared non-overridden
3118058b 10528 -- non-fully conformant homograph of Subp_Id. Both subprograms
10529 -- must be hidden from visibility.
8cf481c9 10530
10531 if Chars (Prim) = Chars (Subp_Id)
8cf481c9 10532 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
3118058b 10533 and then not Fully_Conformant (Prim, Subp_Id)
8cf481c9 10534 then
8c7ee4ac 10535 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10536 Set_Is_Immediately_Visible (Prim, False);
10537 Set_Is_Potentially_Use_Visible (Prim, False);
8cf481c9 10538
8c7ee4ac 10539 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10540 Set_Is_Immediately_Visible (Subp_Id, False);
10541 Set_Is_Potentially_Use_Visible (Subp_Id, False);
8cf481c9 10542 end if;
10543
10544 Next_Elmt (Prim_Elmt);
10545 end loop;
10546 end Hide_Matching_Homographs;
10547
10548 -----------------------------------------
10549 -- Is_Non_Overridden_Or_Null_Procedure --
10550 -----------------------------------------
10551
10552 function Is_Non_Overridden_Or_Null_Procedure
10553 (Subp_Id : Entity_Id) return Boolean
10554 is
10555 Alias_Id : Entity_Id;
10556
10557 begin
10558 -- The subprogram is inherited (implicitly declared), it does not
10559 -- override and does not cover a primitive of an interface.
10560
10561 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10562 and then Present (Alias (Subp_Id))
10563 and then No (Interface_Alias (Subp_Id))
10564 and then No (Overridden_Operation (Subp_Id))
10565 then
10566 Alias_Id := Alias (Subp_Id);
10567
10568 if Requires_Overriding (Alias_Id) then
10569 return True;
10570
10571 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10572 and then Null_Present (Parent (Alias_Id))
10573 then
10574 return True;
10575 end if;
10576 end if;
10577
10578 return False;
10579 end Is_Non_Overridden_Or_Null_Procedure;
10580
10581 -- Local variables
10582
10583 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10584 Prim : Entity_Id;
10585 Prim_Elmt : Elmt_Id;
10586
10587 -- Start of processing for Hide_Non_Overridden_Subprograms
10588
10589 begin
3118058b 10590 -- Inspect the list of primitives looking for non-overridden
10591 -- subprograms.
8cf481c9 10592
10593 if Present (Prim_Ops) then
10594 Prim_Elmt := First_Elmt (Prim_Ops);
10595 while Present (Prim_Elmt) loop
10596 Prim := Node (Prim_Elmt);
10597 Next_Elmt (Prim_Elmt);
10598
10599 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10600 Hide_Matching_Homographs
10601 (Subp_Id => Prim,
10602 Start_Elmt => Prim_Elmt);
10603 end if;
10604 end loop;
10605 end if;
10606 end Hide_Non_Overridden_Subprograms;
10607
97c23bbe 10608 -- Local variables
8cf481c9 10609
d9f6a4ee 10610 E : constant Entity_Id := Entity (N);
47495553 10611
d9f6a4ee 10612 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10613 -- True in non-generic case. Some of the processing here is skipped
10614 -- for the generic case since it is not needed. Basically in the
10615 -- generic case, we only need to do stuff that might generate error
10616 -- messages or warnings.
8cf481c9 10617
10618 -- Start of processing for Freeze_Entity_Checks
10619
d9f6a4ee 10620 begin
10621 -- Remember that we are processing a freezing entity. Required to
10622 -- ensure correct decoration of internal entities associated with
10623 -- interfaces (see New_Overloaded_Entity).
47495553 10624
d9f6a4ee 10625 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
47495553 10626
d9f6a4ee 10627 -- For tagged types covering interfaces add internal entities that link
10628 -- the primitives of the interfaces with the primitives that cover them.
10629 -- Note: These entities were originally generated only when generating
10630 -- code because their main purpose was to provide support to initialize
10631 -- the secondary dispatch tables. They are now generated also when
10632 -- compiling with no code generation to provide ASIS the relationship
10633 -- between interface primitives and tagged type primitives. They are
10634 -- also used to locate primitives covering interfaces when processing
10635 -- generics (see Derive_Subprograms).
47495553 10636
d9f6a4ee 10637 -- This is not needed in the generic case
47495553 10638
d9f6a4ee 10639 if Ada_Version >= Ada_2005
10640 and then Non_Generic_Case
10641 and then Ekind (E) = E_Record_Type
10642 and then Is_Tagged_Type (E)
10643 and then not Is_Interface (E)
10644 and then Has_Interfaces (E)
10645 then
10646 -- This would be a good common place to call the routine that checks
10647 -- overriding of interface primitives (and thus factorize calls to
10648 -- Check_Abstract_Overriding located at different contexts in the
10649 -- compiler). However, this is not possible because it causes
10650 -- spurious errors in case of late overriding.
47495553 10651
d9f6a4ee 10652 Add_Internal_Interface_Entities (E);
10653 end if;
47495553 10654
8cf481c9 10655 -- After all forms of overriding have been resolved, a tagged type may
10656 -- be left with a set of implicitly declared and possibly erroneous
10657 -- abstract subprograms, null procedures and subprograms that require
0c4e0575 10658 -- overriding. If this set contains fully conformant homographs, then
10659 -- one is chosen arbitrarily (already done during resolution), otherwise
10660 -- all remaining non-fully conformant homographs are hidden from
10661 -- visibility (Ada RM 8.3 12.3/2).
8cf481c9 10662
10663 if Is_Tagged_Type (E) then
10664 Hide_Non_Overridden_Subprograms (E);
10665 end if;
10666
d9f6a4ee 10667 -- Check CPP types
47495553 10668
d9f6a4ee 10669 if Ekind (E) = E_Record_Type
10670 and then Is_CPP_Class (E)
10671 and then Is_Tagged_Type (E)
10672 and then Tagged_Type_Expansion
d9f6a4ee 10673 then
10674 if CPP_Num_Prims (E) = 0 then
47495553 10675
d9f6a4ee 10676 -- If the CPP type has user defined components then it must import
10677 -- primitives from C++. This is required because if the C++ class
10678 -- has no primitives then the C++ compiler does not added the _tag
10679 -- component to the type.
47495553 10680
d9f6a4ee 10681 if First_Entity (E) /= Last_Entity (E) then
10682 Error_Msg_N
10683 ("'C'P'P type must import at least one primitive from C++??",
10684 E);
10685 end if;
10686 end if;
47495553 10687
d9f6a4ee 10688 -- Check that all its primitives are abstract or imported from C++.
10689 -- Check also availability of the C++ constructor.
47495553 10690
d9f6a4ee 10691 declare
10692 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10693 Elmt : Elmt_Id;
10694 Error_Reported : Boolean := False;
10695 Prim : Node_Id;
47495553 10696
d9f6a4ee 10697 begin
10698 Elmt := First_Elmt (Primitive_Operations (E));
10699 while Present (Elmt) loop
10700 Prim := Node (Elmt);
47495553 10701
d9f6a4ee 10702 if Comes_From_Source (Prim) then
10703 if Is_Abstract_Subprogram (Prim) then
10704 null;
47495553 10705
d9f6a4ee 10706 elsif not Is_Imported (Prim)
10707 or else Convention (Prim) /= Convention_CPP
10708 then
10709 Error_Msg_N
10710 ("primitives of 'C'P'P types must be imported from C++ "
10711 & "or abstract??", Prim);
47495553 10712
d9f6a4ee 10713 elsif not Has_Constructors
10714 and then not Error_Reported
10715 then
10716 Error_Msg_Name_1 := Chars (E);
10717 Error_Msg_N
10718 ("??'C'P'P constructor required for type %", Prim);
10719 Error_Reported := True;
10720 end if;
10721 end if;
47495553 10722
d9f6a4ee 10723 Next_Elmt (Elmt);
10724 end loop;
10725 end;
10726 end if;
47495553 10727
d9f6a4ee 10728 -- Check Ada derivation of CPP type
47495553 10729
30ab103b 10730 if Expander_Active -- why? losing errors in -gnatc mode???
10731 and then Present (Etype (E)) -- defend against errors
d9f6a4ee 10732 and then Tagged_Type_Expansion
10733 and then Ekind (E) = E_Record_Type
10734 and then Etype (E) /= E
10735 and then Is_CPP_Class (Etype (E))
10736 and then CPP_Num_Prims (Etype (E)) > 0
10737 and then not Is_CPP_Class (E)
10738 and then not Has_CPP_Constructors (Etype (E))
10739 then
10740 -- If the parent has C++ primitives but it has no constructor then
10741 -- check that all the primitives are overridden in this derivation;
10742 -- otherwise the constructor of the parent is needed to build the
10743 -- dispatch table.
47495553 10744
d9f6a4ee 10745 declare
10746 Elmt : Elmt_Id;
10747 Prim : Node_Id;
47495553 10748
10749 begin
d9f6a4ee 10750 Elmt := First_Elmt (Primitive_Operations (E));
10751 while Present (Elmt) loop
10752 Prim := Node (Elmt);
47495553 10753
d9f6a4ee 10754 if not Is_Abstract_Subprogram (Prim)
10755 and then No (Interface_Alias (Prim))
10756 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
47495553 10757 then
d9f6a4ee 10758 Error_Msg_Name_1 := Chars (Etype (E));
10759 Error_Msg_N
10760 ("'C'P'P constructor required for parent type %", E);
10761 exit;
47495553 10762 end if;
d9f6a4ee 10763
10764 Next_Elmt (Elmt);
10765 end loop;
10766 end;
47495553 10767 end if;
10768
d9f6a4ee 10769 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
67278d60 10770
97c23bbe 10771 -- If we have a type with predicates, build predicate function. This is
10772 -- not needed in the generic case, nor within TSS subprograms and other
10773 -- predefined primitives.
67278d60 10774
97c23bbe 10775 if Is_Type (E)
10776 and then Non_Generic_Case
ea822fd4 10777 and then not Within_Internal_Subprogram
97c23bbe 10778 and then Has_Predicates (E)
ea822fd4 10779 then
d9f6a4ee 10780 Build_Predicate_Functions (E, N);
10781 end if;
67278d60 10782
d9f6a4ee 10783 -- If type has delayed aspects, this is where we do the preanalysis at
10784 -- the freeze point, as part of the consistent visibility check. Note
10785 -- that this must be done after calling Build_Predicate_Functions or
10786 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10787 -- the subtype name in the saved expression so that they will not cause
10788 -- trouble in the preanalysis.
67278d60 10789
d9f6a4ee 10790 -- This is also not needed in the generic case
10791
10792 if Non_Generic_Case
10793 and then Has_Delayed_Aspects (E)
10794 and then Scope (E) = Current_Scope
10795 then
10796 -- Retrieve the visibility to the discriminants in order to properly
10797 -- analyze the aspects.
10798
10799 Push_Scope_And_Install_Discriminants (E);
10800
10801 declare
10802 Ritem : Node_Id;
10803
10804 begin
10805 -- Look for aspect specification entries for this entity
67278d60 10806
d9f6a4ee 10807 Ritem := First_Rep_Item (E);
10808 while Present (Ritem) loop
10809 if Nkind (Ritem) = N_Aspect_Specification
10810 and then Entity (Ritem) = E
10811 and then Is_Delayed_Aspect (Ritem)
10812 then
10813 Check_Aspect_At_Freeze_Point (Ritem);
10814 end if;
67278d60 10815
d9f6a4ee 10816 Next_Rep_Item (Ritem);
10817 end loop;
10818 end;
67278d60 10819
d9f6a4ee 10820 Uninstall_Discriminants_And_Pop_Scope (E);
67278d60 10821 end if;
67278d60 10822
d9f6a4ee 10823 -- For a record type, deal with variant parts. This has to be delayed
d0988351 10824 -- to this point, because of the issue of statically predicated
d9f6a4ee 10825 -- subtypes, which we have to ensure are frozen before checking
10826 -- choices, since we need to have the static choice list set.
d6f39728 10827
d9f6a4ee 10828 if Is_Record_Type (E) then
10829 Check_Variant_Part : declare
10830 D : constant Node_Id := Declaration_Node (E);
10831 T : Node_Id;
10832 C : Node_Id;
10833 VP : Node_Id;
d6f39728 10834
d9f6a4ee 10835 Others_Present : Boolean;
10836 pragma Warnings (Off, Others_Present);
10837 -- Indicates others present, not used in this case
d6f39728 10838
d9f6a4ee 10839 procedure Non_Static_Choice_Error (Choice : Node_Id);
10840 -- Error routine invoked by the generic instantiation below when
10841 -- the variant part has a non static choice.
f117057b 10842
d9f6a4ee 10843 procedure Process_Declarations (Variant : Node_Id);
10844 -- Processes declarations associated with a variant. We analyzed
10845 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10846 -- but we still need the recursive call to Check_Choices for any
10847 -- nested variant to get its choices properly processed. This is
10848 -- also where we expand out the choices if expansion is active.
1f526845 10849
d9f6a4ee 10850 package Variant_Choices_Processing is new
10851 Generic_Check_Choices
10852 (Process_Empty_Choice => No_OP,
10853 Process_Non_Static_Choice => Non_Static_Choice_Error,
10854 Process_Associated_Node => Process_Declarations);
10855 use Variant_Choices_Processing;
f117057b 10856
d9f6a4ee 10857 -----------------------------
10858 -- Non_Static_Choice_Error --
10859 -----------------------------
d6f39728 10860
d9f6a4ee 10861 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10862 begin
10863 Flag_Non_Static_Expr
10864 ("choice given in variant part is not static!", Choice);
10865 end Non_Static_Choice_Error;
d6f39728 10866
d9f6a4ee 10867 --------------------------
10868 -- Process_Declarations --
10869 --------------------------
dba36b60 10870
d9f6a4ee 10871 procedure Process_Declarations (Variant : Node_Id) is
10872 CL : constant Node_Id := Component_List (Variant);
10873 VP : Node_Id;
dba36b60 10874
d9f6a4ee 10875 begin
10876 -- Check for static predicate present in this variant
ea61a7ea 10877
d9f6a4ee 10878 if Has_SP_Choice (Variant) then
ea61a7ea 10879
d9f6a4ee 10880 -- Here we expand. You might expect to find this call in
10881 -- Expand_N_Variant_Part, but that is called when we first
10882 -- see the variant part, and we cannot do this expansion
10883 -- earlier than the freeze point, since for statically
10884 -- predicated subtypes, the predicate is not known till
10885 -- the freeze point.
ea61a7ea 10886
d9f6a4ee 10887 -- Furthermore, we do this expansion even if the expander
10888 -- is not active, because other semantic processing, e.g.
10889 -- for aggregates, requires the expanded list of choices.
ea61a7ea 10890
d9f6a4ee 10891 -- If the expander is not active, then we can't just clobber
10892 -- the list since it would invalidate the ASIS -gnatct tree.
10893 -- So we have to rewrite the variant part with a Rewrite
10894 -- call that replaces it with a copy and clobber the copy.
10895
10896 if not Expander_Active then
10897 declare
10898 NewV : constant Node_Id := New_Copy (Variant);
10899 begin
10900 Set_Discrete_Choices
10901 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10902 Rewrite (Variant, NewV);
10903 end;
10904 end if;
10905
10906 Expand_Static_Predicates_In_Choices (Variant);
ea61a7ea 10907 end if;
10908
d9f6a4ee 10909 -- We don't need to worry about the declarations in the variant
10910 -- (since they were analyzed by Analyze_Choices when we first
10911 -- encountered the variant), but we do need to take care of
10912 -- expansion of any nested variants.
ea61a7ea 10913
d9f6a4ee 10914 if not Null_Present (CL) then
10915 VP := Variant_Part (CL);
ea61a7ea 10916
d9f6a4ee 10917 if Present (VP) then
10918 Check_Choices
10919 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10920 end if;
10921 end if;
10922 end Process_Declarations;
ea61a7ea 10923
d9f6a4ee 10924 -- Start of processing for Check_Variant_Part
b9e61b2a 10925
d9f6a4ee 10926 begin
10927 -- Find component list
ea61a7ea 10928
d9f6a4ee 10929 C := Empty;
ea61a7ea 10930
d9f6a4ee 10931 if Nkind (D) = N_Full_Type_Declaration then
10932 T := Type_Definition (D);
ea61a7ea 10933
d9f6a4ee 10934 if Nkind (T) = N_Record_Definition then
10935 C := Component_List (T);
d6f39728 10936
d9f6a4ee 10937 elsif Nkind (T) = N_Derived_Type_Definition
10938 and then Present (Record_Extension_Part (T))
10939 then
10940 C := Component_List (Record_Extension_Part (T));
10941 end if;
10942 end if;
d6f39728 10943
d9f6a4ee 10944 -- Case of variant part present
d6f39728 10945
d9f6a4ee 10946 if Present (C) and then Present (Variant_Part (C)) then
10947 VP := Variant_Part (C);
ea61a7ea 10948
d9f6a4ee 10949 -- Check choices
ea61a7ea 10950
d9f6a4ee 10951 Check_Choices
10952 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
ea61a7ea 10953
d9f6a4ee 10954 -- If the last variant does not contain the Others choice,
10955 -- replace it with an N_Others_Choice node since Gigi always
10956 -- wants an Others. Note that we do not bother to call Analyze
10957 -- on the modified variant part, since its only effect would be
10958 -- to compute the Others_Discrete_Choices node laboriously, and
10959 -- of course we already know the list of choices corresponding
39a0c1d3 10960 -- to the others choice (it's the list we're replacing).
d6f39728 10961
d9f6a4ee 10962 -- We only want to do this if the expander is active, since
39a0c1d3 10963 -- we do not want to clobber the ASIS tree.
d6f39728 10964
d9f6a4ee 10965 if Expander_Active then
10966 declare
10967 Last_Var : constant Node_Id :=
10968 Last_Non_Pragma (Variants (VP));
d6f39728 10969
d9f6a4ee 10970 Others_Node : Node_Id;
d6f39728 10971
d9f6a4ee 10972 begin
10973 if Nkind (First (Discrete_Choices (Last_Var))) /=
10974 N_Others_Choice
10975 then
10976 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10977 Set_Others_Discrete_Choices
10978 (Others_Node, Discrete_Choices (Last_Var));
10979 Set_Discrete_Choices
10980 (Last_Var, New_List (Others_Node));
10981 end if;
10982 end;
10983 end if;
d6f39728 10984 end if;
d9f6a4ee 10985 end Check_Variant_Part;
d6f39728 10986 end if;
d9f6a4ee 10987 end Freeze_Entity_Checks;
d6f39728 10988
10989 -------------------------
10990 -- Get_Alignment_Value --
10991 -------------------------
10992
10993 function Get_Alignment_Value (Expr : Node_Id) return Uint is
f5d97bf5 10994 Align : constant Uint := Static_Integer (Expr);
f74a102b 10995
f5d97bf5 10996 begin
10997 if Align = No_Uint then
10998 return No_Uint;
10999
11000 elsif Align <= 0 then
f74a102b 11001
f74a102b 11002 -- This error is suppressed in ASIS mode to allow for different ASIS
f9906591 11003 -- back ends or ASIS-based tools to query the illegal clause.
f74a102b 11004
11005 if not ASIS_Mode then
11006 Error_Msg_N ("alignment value must be positive", Expr);
11007 end if;
f74a102b 11008
d6f39728 11009 return No_Uint;
11010
11011 else
11012 for J in Int range 0 .. 64 loop
11013 declare
11014 M : constant Uint := Uint_2 ** J;
11015
11016 begin
11017 exit when M = Align;
11018
11019 if M > Align then
f5d97bf5 11020
11021 -- This error is suppressed in ASIS mode to allow for
f9906591 11022 -- different ASIS back ends or ASIS-based tools to query the
f5d97bf5 11023 -- illegal clause.
11024
11025 if not ASIS_Mode then
11026 Error_Msg_N ("alignment value must be power of 2", Expr);
11027 end if;
11028
d6f39728 11029 return No_Uint;
11030 end if;
11031 end;
11032 end loop;
11033
11034 return Align;
11035 end if;
11036 end Get_Alignment_Value;
11037
ee2b7923 11038 -----------------------------
11039 -- Get_Interfacing_Aspects --
11040 -----------------------------
11041
11042 procedure Get_Interfacing_Aspects
11043 (Iface_Asp : Node_Id;
11044 Conv_Asp : out Node_Id;
11045 EN_Asp : out Node_Id;
11046 Expo_Asp : out Node_Id;
11047 Imp_Asp : out Node_Id;
11048 LN_Asp : out Node_Id;
11049 Do_Checks : Boolean := False)
11050 is
11051 procedure Save_Or_Duplication_Error
11052 (Asp : Node_Id;
11053 To : in out Node_Id);
11054 -- Save the value of aspect Asp in node To. If To already has a value,
11055 -- then this is considered a duplicate use of aspect. Emit an error if
11056 -- flag Do_Checks is set.
11057
11058 -------------------------------
11059 -- Save_Or_Duplication_Error --
11060 -------------------------------
11061
11062 procedure Save_Or_Duplication_Error
11063 (Asp : Node_Id;
11064 To : in out Node_Id)
11065 is
11066 begin
11067 -- Detect an extra aspect and issue an error
11068
11069 if Present (To) then
11070 if Do_Checks then
11071 Error_Msg_Name_1 := Chars (Identifier (Asp));
11072 Error_Msg_Sloc := Sloc (To);
11073 Error_Msg_N ("aspect % previously given #", Asp);
11074 end if;
11075
11076 -- Otherwise capture the aspect
11077
11078 else
11079 To := Asp;
11080 end if;
11081 end Save_Or_Duplication_Error;
11082
11083 -- Local variables
11084
11085 Asp : Node_Id;
11086 Asp_Id : Aspect_Id;
11087
11088 -- The following variables capture each individual aspect
11089
11090 Conv : Node_Id := Empty;
11091 EN : Node_Id := Empty;
11092 Expo : Node_Id := Empty;
11093 Imp : Node_Id := Empty;
11094 LN : Node_Id := Empty;
11095
11096 -- Start of processing for Get_Interfacing_Aspects
11097
11098 begin
11099 -- The input interfacing aspect should reside in an aspect specification
11100 -- list.
11101
11102 pragma Assert (Is_List_Member (Iface_Asp));
11103
11104 -- Examine the aspect specifications of the related entity. Find and
11105 -- capture all interfacing aspects. Detect duplicates and emit errors
11106 -- if applicable.
11107
11108 Asp := First (List_Containing (Iface_Asp));
11109 while Present (Asp) loop
11110 Asp_Id := Get_Aspect_Id (Asp);
11111
11112 if Asp_Id = Aspect_Convention then
11113 Save_Or_Duplication_Error (Asp, Conv);
11114
11115 elsif Asp_Id = Aspect_External_Name then
11116 Save_Or_Duplication_Error (Asp, EN);
11117
11118 elsif Asp_Id = Aspect_Export then
11119 Save_Or_Duplication_Error (Asp, Expo);
11120
11121 elsif Asp_Id = Aspect_Import then
11122 Save_Or_Duplication_Error (Asp, Imp);
11123
11124 elsif Asp_Id = Aspect_Link_Name then
11125 Save_Or_Duplication_Error (Asp, LN);
11126 end if;
11127
11128 Next (Asp);
11129 end loop;
11130
11131 Conv_Asp := Conv;
11132 EN_Asp := EN;
11133 Expo_Asp := Expo;
11134 Imp_Asp := Imp;
11135 LN_Asp := LN;
11136 end Get_Interfacing_Aspects;
11137
99a2d5bd 11138 -------------------------------------
11139 -- Inherit_Aspects_At_Freeze_Point --
11140 -------------------------------------
11141
11142 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11143 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11144 (Rep_Item : Node_Id) return Boolean;
11145 -- This routine checks if Rep_Item is either a pragma or an aspect
11146 -- specification node whose correponding pragma (if any) is present in
11147 -- the Rep Item chain of the entity it has been specified to.
11148
11149 --------------------------------------------------
11150 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11151 --------------------------------------------------
11152
11153 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11154 (Rep_Item : Node_Id) return Boolean
11155 is
11156 begin
ec6f6da5 11157 return
11158 Nkind (Rep_Item) = N_Pragma
11159 or else Present_In_Rep_Item
11160 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
99a2d5bd 11161 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11162
29a9d4be 11163 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11164
99a2d5bd 11165 begin
11166 -- A representation item is either subtype-specific (Size and Alignment
11167 -- clauses) or type-related (all others). Subtype-specific aspects may
29a9d4be 11168 -- differ for different subtypes of the same type (RM 13.1.8).
99a2d5bd 11169
11170 -- A derived type inherits each type-related representation aspect of
11171 -- its parent type that was directly specified before the declaration of
29a9d4be 11172 -- the derived type (RM 13.1.15).
99a2d5bd 11173
11174 -- A derived subtype inherits each subtype-specific representation
11175 -- aspect of its parent subtype that was directly specified before the
29a9d4be 11176 -- declaration of the derived type (RM 13.1.15).
99a2d5bd 11177
11178 -- The general processing involves inheriting a representation aspect
11179 -- from a parent type whenever the first rep item (aspect specification,
11180 -- attribute definition clause, pragma) corresponding to the given
11181 -- representation aspect in the rep item chain of Typ, if any, isn't
11182 -- directly specified to Typ but to one of its parents.
11183
11184 -- ??? Note that, for now, just a limited number of representation
29a9d4be 11185 -- aspects have been inherited here so far. Many of them are
11186 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11187 -- a non- exhaustive list of aspects that likely also need to
11188 -- be moved to this routine: Alignment, Component_Alignment,
11189 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
99a2d5bd 11190 -- Preelaborable_Initialization, RM_Size and Small.
11191
8b6e9bf2 11192 -- In addition, Convention must be propagated from base type to subtype,
11193 -- because the subtype may have been declared on an incomplete view.
11194
99a2d5bd 11195 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11196 return;
11197 end if;
11198
11199 -- Ada_05/Ada_2005
11200
11201 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11202 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11203 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11204 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11205 then
11206 Set_Is_Ada_2005_Only (Typ);
11207 end if;
11208
11209 -- Ada_12/Ada_2012
11210
11211 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11212 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11213 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11214 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11215 then
11216 Set_Is_Ada_2012_Only (Typ);
11217 end if;
11218
11219 -- Atomic/Shared
11220
11221 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11222 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11223 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11224 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11225 then
11226 Set_Is_Atomic (Typ);
99a2d5bd 11227 Set_Is_Volatile (Typ);
4bf2acc9 11228 Set_Treat_As_Volatile (Typ);
99a2d5bd 11229 end if;
11230
8b6e9bf2 11231 -- Convention
11232
7ac4254e 11233 if Is_Record_Type (Typ)
11234 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11235 then
8b6e9bf2 11236 Set_Convention (Typ, Convention (Base_Type (Typ)));
11237 end if;
11238
29a9d4be 11239 -- Default_Component_Value
99a2d5bd 11240
81c2bc19 11241 -- Verify that there is no rep_item declared for the type, and there
11242 -- is one coming from an ancestor.
11243
99a2d5bd 11244 if Is_Array_Type (Typ)
f3d70f08 11245 and then Is_Base_Type (Typ)
81c2bc19 11246 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
99a2d5bd 11247 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11248 then
11249 Set_Default_Aspect_Component_Value (Typ,
11250 Default_Aspect_Component_Value
11251 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11252 end if;
11253
29a9d4be 11254 -- Default_Value
99a2d5bd 11255
11256 if Is_Scalar_Type (Typ)
f3d70f08 11257 and then Is_Base_Type (Typ)
81c2bc19 11258 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
99a2d5bd 11259 and then Has_Rep_Item (Typ, Name_Default_Value)
11260 then
81c2bc19 11261 Set_Has_Default_Aspect (Typ);
99a2d5bd 11262 Set_Default_Aspect_Value (Typ,
11263 Default_Aspect_Value
11264 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11265 end if;
11266
11267 -- Discard_Names
11268
11269 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11270 and then Has_Rep_Item (Typ, Name_Discard_Names)
11271 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11272 (Get_Rep_Item (Typ, Name_Discard_Names))
11273 then
11274 Set_Discard_Names (Typ);
11275 end if;
11276
99a2d5bd 11277 -- Volatile
11278
11279 if not Has_Rep_Item (Typ, Name_Volatile, False)
11280 and then Has_Rep_Item (Typ, Name_Volatile)
11281 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11282 (Get_Rep_Item (Typ, Name_Volatile))
11283 then
99a2d5bd 11284 Set_Is_Volatile (Typ);
4bf2acc9 11285 Set_Treat_As_Volatile (Typ);
99a2d5bd 11286 end if;
11287
2fe893b9 11288 -- Volatile_Full_Access
11289
11290 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11291 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11292 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11293 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11294 then
4bf2acc9 11295 Set_Is_Volatile_Full_Access (Typ);
2fe893b9 11296 Set_Is_Volatile (Typ);
4bf2acc9 11297 Set_Treat_As_Volatile (Typ);
2fe893b9 11298 end if;
11299
99a2d5bd 11300 -- Inheritance for derived types only
11301
11302 if Is_Derived_Type (Typ) then
11303 declare
11304 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11305 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11306
11307 begin
11308 -- Atomic_Components
11309
11310 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11311 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11312 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11313 (Get_Rep_Item (Typ, Name_Atomic_Components))
11314 then
11315 Set_Has_Atomic_Components (Imp_Bas_Typ);
11316 end if;
11317
11318 -- Volatile_Components
11319
11320 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11321 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11322 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11323 (Get_Rep_Item (Typ, Name_Volatile_Components))
11324 then
11325 Set_Has_Volatile_Components (Imp_Bas_Typ);
11326 end if;
11327
e81df51c 11328 -- Finalize_Storage_Only
99a2d5bd 11329
11330 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11331 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11332 then
11333 Set_Finalize_Storage_Only (Bas_Typ);
11334 end if;
11335
11336 -- Universal_Aliasing
11337
11338 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11339 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11340 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11341 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11342 then
11343 Set_Universal_Aliasing (Imp_Bas_Typ);
11344 end if;
11345
e81df51c 11346 -- Bit_Order
99a2d5bd 11347
11348 if Is_Record_Type (Typ) then
99a2d5bd 11349 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11350 and then Has_Rep_Item (Typ, Name_Bit_Order)
11351 then
11352 Set_Reverse_Bit_Order (Bas_Typ,
11353 Reverse_Bit_Order (Entity (Name
11354 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11355 end if;
e81df51c 11356 end if;
11357
e9218716 11358 -- Scalar_Storage_Order
11359
11360 -- Note: the aspect is specified on a first subtype, but recorded
11361 -- in a flag of the base type!
e81df51c 11362
11363 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
29b91bc7 11364 and then Typ = Bas_Typ
e81df51c 11365 then
e81df51c 11366 -- For a type extension, always inherit from parent; otherwise
11367 -- inherit if no default applies. Note: we do not check for
11368 -- an explicit rep item on the parent type when inheriting,
11369 -- because the parent SSO may itself have been set by default.
99a2d5bd 11370
e9218716 11371 if not Has_Rep_Item (First_Subtype (Typ),
11372 Name_Scalar_Storage_Order, False)
e81df51c 11373 and then (Is_Tagged_Type (Bas_Typ)
29b91bc7 11374 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11375 or else
11376 SSO_Set_High_By_Default (Bas_Typ)))
99a2d5bd 11377 then
11378 Set_Reverse_Storage_Order (Bas_Typ,
423b89fd 11379 Reverse_Storage_Order
11380 (Implementation_Base_Type (Etype (Bas_Typ))));
b64082f2 11381
11382 -- Clear default SSO indications, since the inherited aspect
11383 -- which was set explicitly overrides the default.
11384
11385 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11386 Set_SSO_Set_High_By_Default (Bas_Typ, False);
99a2d5bd 11387 end if;
11388 end if;
11389 end;
11390 end if;
11391 end Inherit_Aspects_At_Freeze_Point;
11392
d6f39728 11393 ----------------
11394 -- Initialize --
11395 ----------------
11396
11397 procedure Initialize is
11398 begin
7717ea00 11399 Address_Clause_Checks.Init;
d6f39728 11400 Unchecked_Conversions.Init;
dba38d2f 11401
36ac5fbb 11402 if AAMP_On_Target then
dba38d2f 11403 Independence_Checks.Init;
11404 end if;
d6f39728 11405 end Initialize;
11406
2625eb01 11407 ---------------------------
11408 -- Install_Discriminants --
11409 ---------------------------
11410
11411 procedure Install_Discriminants (E : Entity_Id) is
11412 Disc : Entity_Id;
11413 Prev : Entity_Id;
11414 begin
11415 Disc := First_Discriminant (E);
11416 while Present (Disc) loop
11417 Prev := Current_Entity (Disc);
11418 Set_Current_Entity (Disc);
11419 Set_Is_Immediately_Visible (Disc);
11420 Set_Homonym (Disc, Prev);
11421 Next_Discriminant (Disc);
11422 end loop;
11423 end Install_Discriminants;
11424
d6f39728 11425 -------------------------
11426 -- Is_Operational_Item --
11427 -------------------------
11428
11429 function Is_Operational_Item (N : Node_Id) return Boolean is
11430 begin
11431 if Nkind (N) /= N_Attribute_Definition_Clause then
11432 return False;
b9e61b2a 11433
d6f39728 11434 else
11435 declare
b9e61b2a 11436 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
d6f39728 11437 begin
078a74b8 11438
55ab5265 11439 -- List of operational items is given in AARM 13.1(8.mm/1).
078a74b8 11440 -- It is clearly incomplete, as it does not include iterator
11441 -- aspects, among others.
11442
11443 return Id = Attribute_Constant_Indexing
11444 or else Id = Attribute_Default_Iterator
11445 or else Id = Attribute_Implicit_Dereference
11446 or else Id = Attribute_Input
11447 or else Id = Attribute_Iterator_Element
11448 or else Id = Attribute_Iterable
d6f39728 11449 or else Id = Attribute_Output
11450 or else Id = Attribute_Read
078a74b8 11451 or else Id = Attribute_Variable_Indexing
f15731c4 11452 or else Id = Attribute_Write
11453 or else Id = Attribute_External_Tag;
d6f39728 11454 end;
11455 end if;
11456 end Is_Operational_Item;
11457
3b23aaa0 11458 -------------------------
11459 -- Is_Predicate_Static --
11460 -------------------------
11461
94d896aa 11462 -- Note: the basic legality of the expression has already been checked, so
11463 -- we don't need to worry about cases or ranges on strings for example.
11464
3b23aaa0 11465 function Is_Predicate_Static
11466 (Expr : Node_Id;
11467 Nam : Name_Id) return Boolean
11468 is
11469 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
973c2fba 11470 -- Given a list of case expression alternatives, returns True if all
11471 -- the alternatives are static (have all static choices, and a static
11472 -- expression).
3b23aaa0 11473
11474 function All_Static_Choices (L : List_Id) return Boolean;
a360a0f7 11475 -- Returns true if all elements of the list are OK static choices
3b23aaa0 11476 -- as defined below for Is_Static_Choice. Used for case expression
973c2fba 11477 -- alternatives and for the right operand of a membership test. An
11478 -- others_choice is static if the corresponding expression is static.
7c0c95b8 11479 -- The staticness of the bounds is checked separately.
3b23aaa0 11480
11481 function Is_Static_Choice (N : Node_Id) return Boolean;
11482 -- Returns True if N represents a static choice (static subtype, or
a360a0f7 11483 -- static subtype indication, or static expression, or static range).
3b23aaa0 11484 --
11485 -- Note that this is a bit more inclusive than we actually need
11486 -- (in particular membership tests do not allow the use of subtype
a360a0f7 11487 -- indications). But that doesn't matter, we have already checked
3b23aaa0 11488 -- that the construct is legal to get this far.
11489
11490 function Is_Type_Ref (N : Node_Id) return Boolean;
11491 pragma Inline (Is_Type_Ref);
973c2fba 11492 -- Returns True if N is a reference to the type for the predicate in the
11493 -- expression (i.e. if it is an identifier whose Chars field matches the
11494 -- Nam given in the call). N must not be parenthesized, if the type name
11495 -- appears in parens, this routine will return False.
3b23aaa0 11496
11497 ----------------------------------
11498 -- All_Static_Case_Alternatives --
11499 ----------------------------------
11500
11501 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11502 N : Node_Id;
11503
11504 begin
11505 N := First (L);
11506 while Present (N) loop
11507 if not (All_Static_Choices (Discrete_Choices (N))
11508 and then Is_OK_Static_Expression (Expression (N)))
11509 then
11510 return False;
11511 end if;
11512
11513 Next (N);
11514 end loop;
11515
11516 return True;
11517 end All_Static_Case_Alternatives;
11518
11519 ------------------------
11520 -- All_Static_Choices --
11521 ------------------------
11522
11523 function All_Static_Choices (L : List_Id) return Boolean is
11524 N : Node_Id;
11525
11526 begin
11527 N := First (L);
11528 while Present (N) loop
11529 if not Is_Static_Choice (N) then
11530 return False;
11531 end if;
11532
11533 Next (N);
11534 end loop;
11535
11536 return True;
11537 end All_Static_Choices;
11538
11539 ----------------------
11540 -- Is_Static_Choice --
11541 ----------------------
11542
11543 function Is_Static_Choice (N : Node_Id) return Boolean is
11544 begin
7c0c95b8 11545 return Nkind (N) = N_Others_Choice
11546 or else Is_OK_Static_Expression (N)
3b23aaa0 11547 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11548 and then Is_OK_Static_Subtype (Entity (N)))
11549 or else (Nkind (N) = N_Subtype_Indication
11550 and then Is_OK_Static_Subtype (Entity (N)))
11551 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11552 end Is_Static_Choice;
11553
11554 -----------------
11555 -- Is_Type_Ref --
11556 -----------------
11557
11558 function Is_Type_Ref (N : Node_Id) return Boolean is
11559 begin
11560 return Nkind (N) = N_Identifier
11561 and then Chars (N) = Nam
11562 and then Paren_Count (N) = 0;
11563 end Is_Type_Ref;
11564
11565 -- Start of processing for Is_Predicate_Static
11566
11567 begin
3b23aaa0 11568 -- Predicate_Static means one of the following holds. Numbers are the
11569 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11570
11571 -- 16: A static expression
11572
11573 if Is_OK_Static_Expression (Expr) then
11574 return True;
11575
11576 -- 17: A membership test whose simple_expression is the current
11577 -- instance, and whose membership_choice_list meets the requirements
11578 -- for a static membership test.
11579
11580 elsif Nkind (Expr) in N_Membership_Test
11581 and then ((Present (Right_Opnd (Expr))
11582 and then Is_Static_Choice (Right_Opnd (Expr)))
11583 or else
11584 (Present (Alternatives (Expr))
11585 and then All_Static_Choices (Alternatives (Expr))))
11586 then
11587 return True;
11588
11589 -- 18. A case_expression whose selecting_expression is the current
11590 -- instance, and whose dependent expressions are static expressions.
11591
11592 elsif Nkind (Expr) = N_Case_Expression
11593 and then Is_Type_Ref (Expression (Expr))
11594 and then All_Static_Case_Alternatives (Alternatives (Expr))
11595 then
11596 return True;
11597
11598 -- 19. A call to a predefined equality or ordering operator, where one
11599 -- operand is the current instance, and the other is a static
11600 -- expression.
11601
94d896aa 11602 -- Note: the RM is clearly wrong here in not excluding string types.
11603 -- Without this exclusion, we would allow expressions like X > "ABC"
11604 -- to be considered as predicate-static, which is clearly not intended,
11605 -- since the idea is for predicate-static to be a subset of normal
11606 -- static expressions (and "DEF" > "ABC" is not a static expression).
11607
11608 -- However, we do allow internally generated (not from source) equality
11609 -- and inequality operations to be valid on strings (this helps deal
11610 -- with cases where we transform A in "ABC" to A = "ABC).
11611
3b23aaa0 11612 elsif Nkind (Expr) in N_Op_Compare
94d896aa 11613 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11614 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11615 and then not Comes_From_Source (Expr)))
3b23aaa0 11616 and then ((Is_Type_Ref (Left_Opnd (Expr))
11617 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11618 or else
11619 (Is_Type_Ref (Right_Opnd (Expr))
11620 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11621 then
11622 return True;
11623
11624 -- 20. A call to a predefined boolean logical operator, where each
11625 -- operand is predicate-static.
11626
11627 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11628 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11629 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11630 or else
11631 (Nkind (Expr) = N_Op_Not
11632 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11633 then
11634 return True;
11635
11636 -- 21. A short-circuit control form where both operands are
11637 -- predicate-static.
11638
11639 elsif Nkind (Expr) in N_Short_Circuit
11640 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11641 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11642 then
11643 return True;
11644
11645 -- 22. A parenthesized predicate-static expression. This does not
11646 -- require any special test, since we just ignore paren levels in
11647 -- all the cases above.
11648
11649 -- One more test that is an implementation artifact caused by the fact
499918a7 11650 -- that we are analyzing not the original expression, but the generated
3b23aaa0 11651 -- expression in the body of the predicate function. This can include
a360a0f7 11652 -- references to inherited predicates, so that the expression we are
3b23aaa0 11653 -- processing looks like:
11654
75491446 11655 -- xxPredicate (typ (Inns)) and then expression
3b23aaa0 11656
11657 -- Where the call is to a Predicate function for an inherited predicate.
60a4a5af 11658 -- We simply ignore such a call, which could be to either a dynamic or
11659 -- a static predicate. Note that if the parent predicate is dynamic then
11660 -- eventually this type will be marked as dynamic, but you are allowed
11661 -- to specify a static predicate for a subtype which is inheriting a
11662 -- dynamic predicate, so the static predicate validation here ignores
11663 -- the inherited predicate even if it is dynamic.
7db33803 11664 -- In all cases, a static predicate can only apply to a scalar type.
3b23aaa0 11665
11666 elsif Nkind (Expr) = N_Function_Call
11667 and then Is_Predicate_Function (Entity (Name (Expr)))
7db33803 11668 and then Is_Scalar_Type (Etype (First_Entity (Entity (Name (Expr)))))
3b23aaa0 11669 then
11670 return True;
11671
11672 -- That's an exhaustive list of tests, all other cases are not
a360a0f7 11673 -- predicate-static, so we return False.
3b23aaa0 11674
11675 else
11676 return False;
11677 end if;
11678 end Is_Predicate_Static;
11679
2ff55065 11680 ---------------------
11681 -- Kill_Rep_Clause --
11682 ---------------------
11683
11684 procedure Kill_Rep_Clause (N : Node_Id) is
11685 begin
11686 pragma Assert (Ignore_Rep_Clauses);
360f426f 11687
11688 -- Note: we use Replace rather than Rewrite, because we don't want
11689 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11690 -- rep clause that is being replaced.
11691
4949ddd5 11692 Replace (N, Make_Null_Statement (Sloc (N)));
360f426f 11693
11694 -- The null statement must be marked as not coming from source. This is
37c6552c 11695 -- so that ASIS ignores it, and also the back end does not expect bogus
360f426f 11696 -- "from source" null statements in weird places (e.g. in declarative
11697 -- regions where such null statements are not allowed).
11698
11699 Set_Comes_From_Source (N, False);
2ff55065 11700 end Kill_Rep_Clause;
11701
d6f39728 11702 ------------------
11703 -- Minimum_Size --
11704 ------------------
11705
11706 function Minimum_Size
11707 (T : Entity_Id;
d5b349fa 11708 Biased : Boolean := False) return Nat
d6f39728 11709 is
11710 Lo : Uint := No_Uint;
11711 Hi : Uint := No_Uint;
11712 LoR : Ureal := No_Ureal;
11713 HiR : Ureal := No_Ureal;
11714 LoSet : Boolean := False;
11715 HiSet : Boolean := False;
11716 B : Uint;
11717 S : Nat;
11718 Ancest : Entity_Id;
f15731c4 11719 R_Typ : constant Entity_Id := Root_Type (T);
d6f39728 11720
11721 begin
11722 -- If bad type, return 0
11723
11724 if T = Any_Type then
11725 return 0;
11726
11727 -- For generic types, just return zero. There cannot be any legitimate
11728 -- need to know such a size, but this routine may be called with a
11729 -- generic type as part of normal processing.
11730
f02a9a9a 11731 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
d6f39728 11732 return 0;
11733
74c7ae52 11734 -- Access types (cannot have size smaller than System.Address)
d6f39728 11735
11736 elsif Is_Access_Type (T) then
74c7ae52 11737 return System_Address_Size;
d6f39728 11738
11739 -- Floating-point types
11740
11741 elsif Is_Floating_Point_Type (T) then
f15731c4 11742 return UI_To_Int (Esize (R_Typ));
d6f39728 11743
11744 -- Discrete types
11745
11746 elsif Is_Discrete_Type (T) then
11747
fdd294d1 11748 -- The following loop is looking for the nearest compile time known
11749 -- bounds following the ancestor subtype chain. The idea is to find
11750 -- the most restrictive known bounds information.
d6f39728 11751
11752 Ancest := T;
11753 loop
11754 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11755 return 0;
11756 end if;
11757
11758 if not LoSet then
11759 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11760 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11761 LoSet := True;
11762 exit when HiSet;
11763 end if;
11764 end if;
11765
11766 if not HiSet then
11767 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11768 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11769 HiSet := True;
11770 exit when LoSet;
11771 end if;
11772 end if;
11773
11774 Ancest := Ancestor_Subtype (Ancest);
11775
11776 if No (Ancest) then
11777 Ancest := Base_Type (T);
11778
11779 if Is_Generic_Type (Ancest) then
11780 return 0;
11781 end if;
11782 end if;
11783 end loop;
11784
11785 -- Fixed-point types. We can't simply use Expr_Value to get the
fdd294d1 11786 -- Corresponding_Integer_Value values of the bounds, since these do not
11787 -- get set till the type is frozen, and this routine can be called
11788 -- before the type is frozen. Similarly the test for bounds being static
11789 -- needs to include the case where we have unanalyzed real literals for
11790 -- the same reason.
d6f39728 11791
11792 elsif Is_Fixed_Point_Type (T) then
11793
fdd294d1 11794 -- The following loop is looking for the nearest compile time known
11795 -- bounds following the ancestor subtype chain. The idea is to find
11796 -- the most restrictive known bounds information.
d6f39728 11797
11798 Ancest := T;
11799 loop
11800 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11801 return 0;
11802 end if;
11803
3062c401 11804 -- Note: In the following two tests for LoSet and HiSet, it may
11805 -- seem redundant to test for N_Real_Literal here since normally
11806 -- one would assume that the test for the value being known at
11807 -- compile time includes this case. However, there is a glitch.
11808 -- If the real literal comes from folding a non-static expression,
11809 -- then we don't consider any non- static expression to be known
11810 -- at compile time if we are in configurable run time mode (needed
11811 -- in some cases to give a clearer definition of what is and what
11812 -- is not accepted). So the test is indeed needed. Without it, we
11813 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11814
d6f39728 11815 if not LoSet then
11816 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11817 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11818 then
11819 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11820 LoSet := True;
11821 exit when HiSet;
11822 end if;
11823 end if;
11824
11825 if not HiSet then
11826 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11827 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11828 then
11829 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11830 HiSet := True;
11831 exit when LoSet;
11832 end if;
11833 end if;
11834
11835 Ancest := Ancestor_Subtype (Ancest);
11836
11837 if No (Ancest) then
11838 Ancest := Base_Type (T);
11839
11840 if Is_Generic_Type (Ancest) then
11841 return 0;
11842 end if;
11843 end if;
11844 end loop;
11845
11846 Lo := UR_To_Uint (LoR / Small_Value (T));
11847 Hi := UR_To_Uint (HiR / Small_Value (T));
11848
11849 -- No other types allowed
11850
11851 else
11852 raise Program_Error;
11853 end if;
11854
2866d595 11855 -- Fall through with Hi and Lo set. Deal with biased case
d6f39728 11856
cc46ff4b 11857 if (Biased
11858 and then not Is_Fixed_Point_Type (T)
11859 and then not (Is_Enumeration_Type (T)
11860 and then Has_Non_Standard_Rep (T)))
d6f39728 11861 or else Has_Biased_Representation (T)
11862 then
11863 Hi := Hi - Lo;
11864 Lo := Uint_0;
11865 end if;
11866
005366f7 11867 -- Null range case, size is always zero. We only do this in the discrete
11868 -- type case, since that's the odd case that came up. Probably we should
11869 -- also do this in the fixed-point case, but doing so causes peculiar
11870 -- gigi failures, and it is not worth worrying about this incredibly
11871 -- marginal case (explicit null-range fixed-point type declarations)???
11872
11873 if Lo > Hi and then Is_Discrete_Type (T) then
11874 S := 0;
11875
d6f39728 11876 -- Signed case. Note that we consider types like range 1 .. -1 to be
fdd294d1 11877 -- signed for the purpose of computing the size, since the bounds have
1a34e48c 11878 -- to be accommodated in the base type.
d6f39728 11879
005366f7 11880 elsif Lo < 0 or else Hi < 0 then
d6f39728 11881 S := 1;
11882 B := Uint_1;
11883
da253936 11884 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11885 -- Note that we accommodate the case where the bounds cross. This
d6f39728 11886 -- can happen either because of the way the bounds are declared
11887 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11888
11889 while Lo < -B
11890 or else Hi < -B
11891 or else Lo >= B
11892 or else Hi >= B
11893 loop
11894 B := Uint_2 ** S;
11895 S := S + 1;
11896 end loop;
11897
11898 -- Unsigned case
11899
11900 else
11901 -- If both bounds are positive, make sure that both are represen-
11902 -- table in the case where the bounds are crossed. This can happen
11903 -- either because of the way the bounds are declared, or because of
11904 -- the algorithm in Freeze_Fixed_Point_Type.
11905
11906 if Lo > Hi then
11907 Hi := Lo;
11908 end if;
11909
da253936 11910 -- S = size, (can accommodate 0 .. (2**size - 1))
d6f39728 11911
11912 S := 0;
11913 while Hi >= Uint_2 ** S loop
11914 S := S + 1;
11915 end loop;
11916 end if;
11917
11918 return S;
11919 end Minimum_Size;
11920
44e4341e 11921 ---------------------------
11922 -- New_Stream_Subprogram --
11923 ---------------------------
d6f39728 11924
44e4341e 11925 procedure New_Stream_Subprogram
11926 (N : Node_Id;
11927 Ent : Entity_Id;
11928 Subp : Entity_Id;
11929 Nam : TSS_Name_Type)
d6f39728 11930 is
11931 Loc : constant Source_Ptr := Sloc (N);
9dfe12ae 11932 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
f15731c4 11933 Subp_Id : Entity_Id;
d6f39728 11934 Subp_Decl : Node_Id;
11935 F : Entity_Id;
11936 Etyp : Entity_Id;
11937
44e4341e 11938 Defer_Declaration : constant Boolean :=
11939 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11940 -- For a tagged type, there is a declaration for each stream attribute
11941 -- at the freeze point, and we must generate only a completion of this
11942 -- declaration. We do the same for private types, because the full view
11943 -- might be tagged. Otherwise we generate a declaration at the point of
449c4f58 11944 -- the attribute definition clause. If the attribute definition comes
11945 -- from an aspect specification the declaration is part of the freeze
11946 -- actions of the type.
44e4341e 11947
f15731c4 11948 function Build_Spec return Node_Id;
11949 -- Used for declaration and renaming declaration, so that this is
11950 -- treated as a renaming_as_body.
11951
11952 ----------------
11953 -- Build_Spec --
11954 ----------------
11955
d5b349fa 11956 function Build_Spec return Node_Id is
44e4341e 11957 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11958 Formals : List_Id;
11959 Spec : Node_Id;
83c6c069 11960 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
44e4341e 11961
f15731c4 11962 begin
9dfe12ae 11963 Subp_Id := Make_Defining_Identifier (Loc, Sname);
f15731c4 11964
44e4341e 11965 -- S : access Root_Stream_Type'Class
11966
11967 Formals := New_List (
11968 Make_Parameter_Specification (Loc,
11969 Defining_Identifier =>
11970 Make_Defining_Identifier (Loc, Name_S),
11971 Parameter_Type =>
11972 Make_Access_Definition (Loc,
11973 Subtype_Mark =>
83c6c069 11974 New_Occurrence_Of (
44e4341e 11975 Designated_Type (Etype (F)), Loc))));
11976
11977 if Nam = TSS_Stream_Input then
4bba0a8d 11978 Spec :=
11979 Make_Function_Specification (Loc,
11980 Defining_Unit_Name => Subp_Id,
11981 Parameter_Specifications => Formals,
11982 Result_Definition => T_Ref);
44e4341e 11983 else
11984 -- V : [out] T
f15731c4 11985
44e4341e 11986 Append_To (Formals,
11987 Make_Parameter_Specification (Loc,
11988 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11989 Out_Present => Out_P,
11990 Parameter_Type => T_Ref));
f15731c4 11991
d3ef794c 11992 Spec :=
11993 Make_Procedure_Specification (Loc,
11994 Defining_Unit_Name => Subp_Id,
11995 Parameter_Specifications => Formals);
44e4341e 11996 end if;
f15731c4 11997
44e4341e 11998 return Spec;
11999 end Build_Spec;
d6f39728 12000
44e4341e 12001 -- Start of processing for New_Stream_Subprogram
d6f39728 12002
44e4341e 12003 begin
12004 F := First_Formal (Subp);
12005
12006 if Ekind (Subp) = E_Procedure then
12007 Etyp := Etype (Next_Formal (F));
d6f39728 12008 else
44e4341e 12009 Etyp := Etype (Subp);
d6f39728 12010 end if;
f15731c4 12011
44e4341e 12012 -- Prepare subprogram declaration and insert it as an action on the
12013 -- clause node. The visibility for this entity is used to test for
12014 -- visibility of the attribute definition clause (in the sense of
12015 -- 8.3(23) as amended by AI-195).
9dfe12ae 12016
44e4341e 12017 if not Defer_Declaration then
f15731c4 12018 Subp_Decl :=
12019 Make_Subprogram_Declaration (Loc,
12020 Specification => Build_Spec);
44e4341e 12021
12022 -- For a tagged type, there is always a visible declaration for each
15ebb600 12023 -- stream TSS (it is a predefined primitive operation), and the
44e4341e 12024 -- completion of this declaration occurs at the freeze point, which is
12025 -- not always visible at places where the attribute definition clause is
12026 -- visible. So, we create a dummy entity here for the purpose of
12027 -- tracking the visibility of the attribute definition clause itself.
12028
12029 else
12030 Subp_Id :=
55868293 12031 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
44e4341e 12032 Subp_Decl :=
12033 Make_Object_Declaration (Loc,
12034 Defining_Identifier => Subp_Id,
12035 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
f15731c4 12036 end if;
12037
449c4f58 12038 if not Defer_Declaration
12039 and then From_Aspect_Specification (N)
12040 and then Has_Delayed_Freeze (Ent)
12041 then
12042 Append_Freeze_Action (Ent, Subp_Decl);
12043
12044 else
12045 Insert_Action (N, Subp_Decl);
12046 Set_Entity (N, Subp_Id);
12047 end if;
44e4341e 12048
d6f39728 12049 Subp_Decl :=
12050 Make_Subprogram_Renaming_Declaration (Loc,
f15731c4 12051 Specification => Build_Spec,
83c6c069 12052 Name => New_Occurrence_Of (Subp, Loc));
d6f39728 12053
44e4341e 12054 if Defer_Declaration then
d6f39728 12055 Set_TSS (Base_Type (Ent), Subp_Id);
449c4f58 12056
d6f39728 12057 else
449c4f58 12058 if From_Aspect_Specification (N) then
12059 Append_Freeze_Action (Ent, Subp_Decl);
12060
12061 else
12062 Insert_Action (N, Subp_Decl);
12063 end if;
12064
d6f39728 12065 Copy_TSS (Subp_Id, Base_Type (Ent));
12066 end if;
44e4341e 12067 end New_Stream_Subprogram;
d6f39728 12068
2625eb01 12069 ------------------------------------------
12070 -- Push_Scope_And_Install_Discriminants --
12071 ------------------------------------------
12072
12073 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12074 begin
12075 if Has_Discriminants (E) then
12076 Push_Scope (E);
12077
97c23bbe 12078 -- Make the discriminants visible for type declarations and protected
2625eb01 12079 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12080
12081 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12082 Install_Discriminants (E);
12083 end if;
12084 end if;
12085 end Push_Scope_And_Install_Discriminants;
12086
d6f39728 12087 ------------------------
12088 -- Rep_Item_Too_Early --
12089 ------------------------
12090
80d4fec4 12091 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
d6f39728 12092 begin
44e4341e 12093 -- Cannot apply non-operational rep items to generic types
d6f39728 12094
f15731c4 12095 if Is_Operational_Item (N) then
12096 return False;
12097
12098 elsif Is_Type (T)
d6f39728 12099 and then Is_Generic_Type (Root_Type (T))
e17c5076 12100 and then (Nkind (N) /= N_Pragma
12101 or else Get_Pragma_Id (N) /= Pragma_Convention)
d6f39728 12102 then
503f7fd3 12103 Error_Msg_N ("representation item not allowed for generic type", N);
d6f39728 12104 return True;
12105 end if;
12106
fdd294d1 12107 -- Otherwise check for incomplete type
d6f39728 12108
12109 if Is_Incomplete_Or_Private_Type (T)
12110 and then No (Underlying_Type (T))
d64221a7 12111 and then
12112 (Nkind (N) /= N_Pragma
60014bc9 12113 or else Get_Pragma_Id (N) /= Pragma_Import)
d6f39728 12114 then
12115 Error_Msg_N
12116 ("representation item must be after full type declaration", N);
12117 return True;
12118
1a34e48c 12119 -- If the type has incomplete components, a representation clause is
d6f39728 12120 -- illegal but stream attributes and Convention pragmas are correct.
12121
12122 elsif Has_Private_Component (T) then
f15731c4 12123 if Nkind (N) = N_Pragma then
d6f39728 12124 return False;
b9e61b2a 12125
d6f39728 12126 else
12127 Error_Msg_N
12128 ("representation item must appear after type is fully defined",
12129 N);
12130 return True;
12131 end if;
12132 else
12133 return False;
12134 end if;
12135 end Rep_Item_Too_Early;
12136
12137 -----------------------
12138 -- Rep_Item_Too_Late --
12139 -----------------------
12140
12141 function Rep_Item_Too_Late
12142 (T : Entity_Id;
12143 N : Node_Id;
d5b349fa 12144 FOnly : Boolean := False) return Boolean
d6f39728 12145 is
12146 S : Entity_Id;
12147 Parent_Type : Entity_Id;
12148
4d0944e9 12149 procedure No_Type_Rep_Item;
12150 -- Output message indicating that no type-related aspects can be
12151 -- specified due to some property of the parent type.
12152
d6f39728 12153 procedure Too_Late;
4d0944e9 12154 -- Output message for an aspect being specified too late
12155
12156 -- Note that neither of the above errors is considered a serious one,
12157 -- since the effect is simply that we ignore the representation clause
12158 -- in these cases.
04d38ee4 12159 -- Is this really true? In any case if we make this change we must
12160 -- document the requirement in the spec of Rep_Item_Too_Late that
12161 -- if True is returned, then the rep item must be completely ignored???
4d0944e9 12162
12163 ----------------------
12164 -- No_Type_Rep_Item --
12165 ----------------------
12166
12167 procedure No_Type_Rep_Item is
12168 begin
12169 Error_Msg_N ("|type-related representation item not permitted!", N);
12170 end No_Type_Rep_Item;
d53a018a 12171
12172 --------------
12173 -- Too_Late --
12174 --------------
d6f39728 12175
12176 procedure Too_Late is
12177 begin
ce4da1ed 12178 -- Other compilers seem more relaxed about rep items appearing too
12179 -- late. Since analysis tools typically don't care about rep items
12180 -- anyway, no reason to be too strict about this.
12181
a9cd517c 12182 if not Relaxed_RM_Semantics then
12183 Error_Msg_N ("|representation item appears too late!", N);
12184 end if;
d6f39728 12185 end Too_Late;
12186
12187 -- Start of processing for Rep_Item_Too_Late
12188
12189 begin
a3248fc4 12190 -- First make sure entity is not frozen (RM 13.1(9))
d6f39728 12191
12192 if Is_Frozen (T)
a3248fc4 12193
12194 -- Exclude imported types, which may be frozen if they appear in a
12195 -- representation clause for a local type.
12196
4aa270d8 12197 and then not From_Limited_With (T)
a3248fc4 12198
a9cd517c 12199 -- Exclude generated entities (not coming from source). The common
a3248fc4 12200 -- case is when we generate a renaming which prematurely freezes the
12201 -- renamed internal entity, but we still want to be able to set copies
12202 -- of attribute values such as Size/Alignment.
12203
12204 and then Comes_From_Source (T)
d6f39728 12205 then
58e133a6 12206 -- A self-referential aspect is illegal if it forces freezing the
12207 -- entity before the corresponding pragma has been analyzed.
12208
12209 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12210 and then From_Aspect_Specification (N)
12211 then
12212 Error_Msg_NE
12213 ("aspect specification causes premature freezing of&", T, N);
12214 Set_Has_Delayed_Freeze (T, False);
12215 return True;
12216 end if;
12217
d6f39728 12218 Too_Late;
12219 S := First_Subtype (T);
12220
12221 if Present (Freeze_Node (S)) then
04d38ee4 12222 if not Relaxed_RM_Semantics then
12223 Error_Msg_NE
12224 ("??no more representation items for }", Freeze_Node (S), S);
12225 end if;
d6f39728 12226 end if;
12227
12228 return True;
12229
d1a2e31b 12230 -- Check for case of untagged derived type whose parent either has
4d0944e9 12231 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12232 -- this case we do not output a Too_Late message, since there is no
12233 -- earlier point where the rep item could be placed to make it legal.
d6f39728 12234
12235 elsif Is_Type (T)
12236 and then not FOnly
12237 and then Is_Derived_Type (T)
12238 and then not Is_Tagged_Type (T)
12239 then
12240 Parent_Type := Etype (Base_Type (T));
12241
12242 if Has_Primitive_Operations (Parent_Type) then
4d0944e9 12243 No_Type_Rep_Item;
04d38ee4 12244
12245 if not Relaxed_RM_Semantics then
12246 Error_Msg_NE
12247 ("\parent type & has primitive operations!", N, Parent_Type);
12248 end if;
12249
d6f39728 12250 return True;
12251
12252 elsif Is_By_Reference_Type (Parent_Type) then
4d0944e9 12253 No_Type_Rep_Item;
04d38ee4 12254
12255 if not Relaxed_RM_Semantics then
12256 Error_Msg_NE
12257 ("\parent type & is a by reference type!", N, Parent_Type);
12258 end if;
12259
d6f39728 12260 return True;
12261 end if;
12262 end if;
12263
04d38ee4 12264 -- No error, but one more warning to consider. The RM (surprisingly)
12265 -- allows this pattern:
12266
12267 -- type S is ...
12268 -- primitive operations for S
12269 -- type R is new S;
12270 -- rep clause for S
12271
12272 -- Meaning that calls on the primitive operations of S for values of
12273 -- type R may require possibly expensive implicit conversion operations.
12274 -- This is not an error, but is worth a warning.
12275
12276 if not Relaxed_RM_Semantics and then Is_Type (T) then
12277 declare
12278 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12279
12280 begin
12281 if Present (DTL)
12282 and then Has_Primitive_Operations (Base_Type (T))
12283
12284 -- For now, do not generate this warning for the case of aspect
12285 -- specification using Ada 2012 syntax, since we get wrong
12286 -- messages we do not understand. The whole business of derived
12287 -- types and rep items seems a bit confused when aspects are
12288 -- used, since the aspects are not evaluated till freeze time.
12289
12290 and then not From_Aspect_Specification (N)
12291 then
12292 Error_Msg_Sloc := Sloc (DTL);
12293 Error_Msg_N
12294 ("representation item for& appears after derived type "
12295 & "declaration#??", N);
12296 Error_Msg_NE
12297 ("\may result in implicit conversions for primitive "
12298 & "operations of&??", N, T);
12299 Error_Msg_NE
12300 ("\to change representations when called with arguments "
12301 & "of type&??", N, DTL);
12302 end if;
12303 end;
12304 end if;
12305
3062c401 12306 -- No error, link item into head of chain of rep items for the entity,
12307 -- but avoid chaining if we have an overloadable entity, and the pragma
12308 -- is one that can apply to multiple overloaded entities.
12309
b9e61b2a 12310 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
fdd294d1 12311 declare
12312 Pname : constant Name_Id := Pragma_Name (N);
12313 begin
18393965 12314 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12315 Name_External, Name_Interface)
fdd294d1 12316 then
12317 return False;
12318 end if;
12319 end;
3062c401 12320 end if;
12321
fdd294d1 12322 Record_Rep_Item (T, N);
d6f39728 12323 return False;
12324 end Rep_Item_Too_Late;
12325
2072eaa9 12326 -------------------------------------
12327 -- Replace_Type_References_Generic --
12328 -------------------------------------
12329
37c6552c 12330 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12331 TName : constant Name_Id := Chars (T);
2072eaa9 12332
97c23bbe 12333 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
2072eaa9 12334 -- Processes a single node in the traversal procedure below, checking
12335 -- if node N should be replaced, and if so, doing the replacement.
12336
d0931270 12337 function Visible_Component (Comp : Name_Id) return Entity_Id;
12338 -- Given an identifier in the expression, check whether there is a
12339 -- discriminant or component of the type that is directy visible, and
12340 -- rewrite it as the corresponding selected component of the formal of
12341 -- the subprogram. The entity is located by a sequential search, which
12342 -- seems acceptable given the typical size of component lists and check
12343 -- expressions. Possible optimization ???
12344
97c23bbe 12345 ----------------------
12346 -- Replace_Type_Ref --
12347 ----------------------
2072eaa9 12348
97c23bbe 12349 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
d0931270 12350 Loc : constant Source_Ptr := Sloc (N);
2072eaa9 12351
d0931270 12352 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
77fd9c7a 12353 -- Add the proper prefix to a reference to a component of the type
12354 -- when it is not already a selected component.
d0931270 12355
12356 ----------------
12357 -- Add_Prefix --
12358 ----------------
2072eaa9 12359
d0931270 12360 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12361 begin
12362 Rewrite (Ref,
12363 Make_Selected_Component (Loc,
77fd9c7a 12364 Prefix => New_Occurrence_Of (T, Loc),
d0931270 12365 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12366 Replace_Type_Reference (Prefix (Ref));
12367 end Add_Prefix;
12368
77fd9c7a 12369 -- Local variables
12370
12371 Comp : Entity_Id;
12372 Pref : Node_Id;
12373 Scop : Entity_Id;
12374
d0931270 12375 -- Start of processing for Replace_Type_Ref
12376
12377 begin
2072eaa9 12378 if Nkind (N) = N_Identifier then
12379
97c23bbe 12380 -- If not the type name, check whether it is a reference to some
12381 -- other type, which must be frozen before the predicate function
12382 -- is analyzed, i.e. before the freeze node of the type to which
12383 -- the predicate applies.
2072eaa9 12384
12385 if Chars (N) /= TName then
37c6552c 12386 if Present (Current_Entity (N))
46532462 12387 and then Is_Type (Current_Entity (N))
37c6552c 12388 then
12389 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12390 end if;
12391
d0931270 12392 -- The components of the type are directly visible and can
12393 -- be referenced without a prefix.
12394
12395 if Nkind (Parent (N)) = N_Selected_Component then
12396 null;
12397
12398 -- In expression C (I), C may be a directly visible function
12399 -- or a visible component that has an array type. Disambiguate
12400 -- by examining the component type.
12401
12402 elsif Nkind (Parent (N)) = N_Indexed_Component
12403 and then N = Prefix (Parent (N))
12404 then
77fd9c7a 12405 Comp := Visible_Component (Chars (N));
d0931270 12406
77fd9c7a 12407 if Present (Comp) and then Is_Array_Type (Etype (Comp)) then
12408 Add_Prefix (N, Comp);
d0931270 12409 end if;
12410
12411 else
77fd9c7a 12412 Comp := Visible_Component (Chars (N));
d0931270 12413
77fd9c7a 12414 if Present (Comp) then
12415 Add_Prefix (N, Comp);
d0931270 12416 end if;
12417 end if;
12418
2072eaa9 12419 return Skip;
12420
12421 -- Otherwise do the replacement and we are done with this node
12422
12423 else
12424 Replace_Type_Reference (N);
12425 return Skip;
12426 end if;
12427
97c23bbe 12428 -- Case of selected component (which is what a qualification looks
12429 -- like in the unanalyzed tree, which is what we have.
2072eaa9 12430
12431 elsif Nkind (N) = N_Selected_Component then
12432
97c23bbe 12433 -- If selector name is not our type, keeping going (we might still
12434 -- have an occurrence of the type in the prefix).
2072eaa9 12435
12436 if Nkind (Selector_Name (N)) /= N_Identifier
12437 or else Chars (Selector_Name (N)) /= TName
12438 then
12439 return OK;
12440
12441 -- Selector name is our type, check qualification
12442
12443 else
12444 -- Loop through scopes and prefixes, doing comparison
12445
77fd9c7a 12446 Scop := Current_Scope;
12447 Pref := Prefix (N);
2072eaa9 12448 loop
12449 -- Continue if no more scopes or scope with no name
12450
77fd9c7a 12451 if No (Scop) or else Nkind (Scop) not in N_Has_Chars then
2072eaa9 12452 return OK;
12453 end if;
12454
97c23bbe 12455 -- Do replace if prefix is an identifier matching the scope
12456 -- that we are currently looking at.
2072eaa9 12457
77fd9c7a 12458 if Nkind (Pref) = N_Identifier
12459 and then Chars (Pref) = Chars (Scop)
2072eaa9 12460 then
12461 Replace_Type_Reference (N);
12462 return Skip;
12463 end if;
12464
97c23bbe 12465 -- Go check scope above us if prefix is itself of the form
12466 -- of a selected component, whose selector matches the scope
12467 -- we are currently looking at.
2072eaa9 12468
77fd9c7a 12469 if Nkind (Pref) = N_Selected_Component
12470 and then Nkind (Selector_Name (Pref)) = N_Identifier
12471 and then Chars (Selector_Name (Pref)) = Chars (Scop)
2072eaa9 12472 then
77fd9c7a 12473 Scop := Scope (Scop);
12474 Pref := Prefix (Pref);
2072eaa9 12475
12476 -- For anything else, we don't have a match, so keep on
12477 -- going, there are still some weird cases where we may
12478 -- still have a replacement within the prefix.
12479
12480 else
12481 return OK;
12482 end if;
12483 end loop;
12484 end if;
12485
ec6f6da5 12486 -- Continue for any other node kind
2072eaa9 12487
12488 else
12489 return OK;
12490 end if;
97c23bbe 12491 end Replace_Type_Ref;
12492
77fd9c7a 12493 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
12494
d0931270 12495 -----------------------
12496 -- Visible_Component --
12497 -----------------------
12498
12499 function Visible_Component (Comp : Name_Id) return Entity_Id is
12500 E : Entity_Id;
77fd9c7a 12501
d0931270 12502 begin
12503 if Ekind (T) /= E_Record_Type then
12504 return Empty;
12505
12506 else
12507 E := First_Entity (T);
12508 while Present (E) loop
77fd9c7a 12509 if Comes_From_Source (E) and then Chars (E) = Comp then
d0931270 12510 return E;
12511 end if;
12512
12513 Next_Entity (E);
12514 end loop;
12515
12516 return Empty;
12517 end if;
12518 end Visible_Component;
12519
77fd9c7a 12520 -- Start of processing for Replace_Type_References_Generic
2072eaa9 12521
12522 begin
12523 Replace_Type_Refs (N);
12524 end Replace_Type_References_Generic;
12525
81bd1c0d 12526 --------------------------------
12527 -- Resolve_Aspect_Expressions --
12528 --------------------------------
12529
12530 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12531 ASN : Node_Id;
12532 A_Id : Aspect_Id;
12533 Expr : Node_Id;
12534
9c20237a 12535 function Resolve_Name (N : Node_Id) return Traverse_Result;
12536 -- Verify that all identifiers in the expression, with the exception
12537 -- of references to the current entity, denote visible entities. This
12538 -- is done only to detect visibility errors, as the expression will be
12539 -- properly analyzed/expanded during analysis of the predicate function
c098acfb 12540 -- body. We omit quantified expressions from this test, given that they
12541 -- introduce a local identifier that would require proper expansion to
12542 -- handle properly.
9c20237a 12543
12544 ------------------
12545 -- Resolve_Name --
12546 ------------------
12547
12548 function Resolve_Name (N : Node_Id) return Traverse_Result is
12549 begin
12550 if Nkind (N) = N_Selected_Component then
12551 if Nkind (Prefix (N)) = N_Identifier
12552 and then Chars (Prefix (N)) /= Chars (E)
12553 then
f4e18891 12554 Find_Selected_Component (N);
9c20237a 12555 end if;
02e5d0d0 12556
9c20237a 12557 return Skip;
12558
02e5d0d0 12559 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
9c20237a 12560 Find_Direct_Name (N);
12561 Set_Entity (N, Empty);
c098acfb 12562
12563 elsif Nkind (N) = N_Quantified_Expression then
12564 return Skip;
9c20237a 12565 end if;
12566
12567 return OK;
12568 end Resolve_Name;
12569
12570 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12571
02e5d0d0 12572 -- Start of processing for Resolve_Aspect_Expressions
12573
81bd1c0d 12574 begin
12575 ASN := First_Rep_Item (E);
12576 while Present (ASN) loop
12577 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12578 A_Id := Get_Aspect_Id (ASN);
12579 Expr := Expression (ASN);
12580
12581 case A_Id is
97c23bbe 12582
81bd1c0d 12583 -- For now we only deal with aspects that do not generate
12584 -- subprograms, or that may mention current instances of
fdec445e 12585 -- types. These will require special handling (???TBD).
81bd1c0d 12586
02e5d0d0 12587 when Aspect_Predicate |
fdec445e 12588 Aspect_Predicate_Failure |
97c23bbe 12589 Aspect_Invariant =>
81bd1c0d 12590 null;
12591
97c23bbe 12592 when Aspect_Dynamic_Predicate |
12593 Aspect_Static_Predicate =>
9c20237a 12594
02e5d0d0 12595 -- Build predicate function specification and preanalyze
9c20237a 12596 -- expression after type replacement.
12597
12598 if No (Predicate_Function (E)) then
12599 declare
12600 FDecl : constant Node_Id :=
02e5d0d0 12601 Build_Predicate_Function_Declaration (E);
9c20237a 12602 pragma Unreferenced (FDecl);
12603 begin
12604 Resolve_Aspect_Expression (Expr);
12605 end;
12606 end if;
12607
81bd1c0d 12608 when Pre_Post_Aspects =>
12609 null;
12610
12611 when Aspect_Iterable =>
12612 if Nkind (Expr) = N_Aggregate then
12613 declare
12614 Assoc : Node_Id;
12615
12616 begin
12617 Assoc := First (Component_Associations (Expr));
12618 while Present (Assoc) loop
12619 Find_Direct_Name (Expression (Assoc));
12620 Next (Assoc);
12621 end loop;
12622 end;
12623 end if;
12624
12625 when others =>
12626 if Present (Expr) then
12627 case Aspect_Argument (A_Id) is
12628 when Expression | Optional_Expression =>
12629 Analyze_And_Resolve (Expression (ASN));
12630
12631 when Name | Optional_Name =>
12632 if Nkind (Expr) = N_Identifier then
12633 Find_Direct_Name (Expr);
12634
12635 elsif Nkind (Expr) = N_Selected_Component then
12636 Find_Selected_Component (Expr);
12637
12638 else
12639 null;
12640 end if;
12641 end case;
12642 end if;
12643 end case;
12644 end if;
12645
a738763e 12646 ASN := Next_Rep_Item (ASN);
81bd1c0d 12647 end loop;
12648 end Resolve_Aspect_Expressions;
12649
d6f39728 12650 -------------------------
12651 -- Same_Representation --
12652 -------------------------
12653
12654 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12655 T1 : constant Entity_Id := Underlying_Type (Typ1);
12656 T2 : constant Entity_Id := Underlying_Type (Typ2);
12657
12658 begin
12659 -- A quick check, if base types are the same, then we definitely have
12660 -- the same representation, because the subtype specific representation
12661 -- attributes (Size and Alignment) do not affect representation from
12662 -- the point of view of this test.
12663
12664 if Base_Type (T1) = Base_Type (T2) then
12665 return True;
12666
12667 elsif Is_Private_Type (Base_Type (T2))
12668 and then Base_Type (T1) = Full_View (Base_Type (T2))
12669 then
12670 return True;
12671 end if;
12672
12673 -- Tagged types never have differing representations
12674
12675 if Is_Tagged_Type (T1) then
12676 return True;
12677 end if;
12678
12679 -- Representations are definitely different if conventions differ
12680
12681 if Convention (T1) /= Convention (T2) then
12682 return False;
12683 end if;
12684
ef0772bc 12685 -- Representations are different if component alignments or scalar
12686 -- storage orders differ.
d6f39728 12687
12688 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
726fd56a 12689 and then
d6f39728 12690 (Is_Record_Type (T2) or else Is_Array_Type (T2))
ef0772bc 12691 and then
12692 (Component_Alignment (T1) /= Component_Alignment (T2)
f02a9a9a 12693 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
d6f39728 12694 then
12695 return False;
12696 end if;
12697
12698 -- For arrays, the only real issue is component size. If we know the
12699 -- component size for both arrays, and it is the same, then that's
12700 -- good enough to know we don't have a change of representation.
12701
12702 if Is_Array_Type (T1) then
12703 if Known_Component_Size (T1)
12704 and then Known_Component_Size (T2)
12705 and then Component_Size (T1) = Component_Size (T2)
12706 then
36ac5fbb 12707 return True;
d6f39728 12708 end if;
12709 end if;
12710
12711 -- Types definitely have same representation if neither has non-standard
12712 -- representation since default representations are always consistent.
12713 -- If only one has non-standard representation, and the other does not,
12714 -- then we consider that they do not have the same representation. They
12715 -- might, but there is no way of telling early enough.
12716
12717 if Has_Non_Standard_Rep (T1) then
12718 if not Has_Non_Standard_Rep (T2) then
12719 return False;
12720 end if;
12721 else
12722 return not Has_Non_Standard_Rep (T2);
12723 end if;
12724
fdd294d1 12725 -- Here the two types both have non-standard representation, and we need
12726 -- to determine if they have the same non-standard representation.
d6f39728 12727
12728 -- For arrays, we simply need to test if the component sizes are the
12729 -- same. Pragma Pack is reflected in modified component sizes, so this
12730 -- check also deals with pragma Pack.
12731
12732 if Is_Array_Type (T1) then
12733 return Component_Size (T1) = Component_Size (T2);
12734
12735 -- Tagged types always have the same representation, because it is not
12736 -- possible to specify different representations for common fields.
12737
12738 elsif Is_Tagged_Type (T1) then
12739 return True;
12740
12741 -- Case of record types
12742
12743 elsif Is_Record_Type (T1) then
12744
12745 -- Packed status must conform
12746
12747 if Is_Packed (T1) /= Is_Packed (T2) then
12748 return False;
12749
12750 -- Otherwise we must check components. Typ2 maybe a constrained
12751 -- subtype with fewer components, so we compare the components
12752 -- of the base types.
12753
12754 else
12755 Record_Case : declare
12756 CD1, CD2 : Entity_Id;
12757
12758 function Same_Rep return Boolean;
12759 -- CD1 and CD2 are either components or discriminants. This
ef0772bc 12760 -- function tests whether they have the same representation.
d6f39728 12761
80d4fec4 12762 --------------
12763 -- Same_Rep --
12764 --------------
12765
d6f39728 12766 function Same_Rep return Boolean is
12767 begin
12768 if No (Component_Clause (CD1)) then
12769 return No (Component_Clause (CD2));
d6f39728 12770 else
ef0772bc 12771 -- Note: at this point, component clauses have been
12772 -- normalized to the default bit order, so that the
12773 -- comparison of Component_Bit_Offsets is meaningful.
12774
d6f39728 12775 return
12776 Present (Component_Clause (CD2))
12777 and then
12778 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12779 and then
12780 Esize (CD1) = Esize (CD2);
12781 end if;
12782 end Same_Rep;
12783
1e35409d 12784 -- Start of processing for Record_Case
d6f39728 12785
12786 begin
12787 if Has_Discriminants (T1) then
d6f39728 12788
9dfe12ae 12789 -- The number of discriminants may be different if the
12790 -- derived type has fewer (constrained by values). The
12791 -- invisible discriminants retain the representation of
12792 -- the original, so the discrepancy does not per se
12793 -- indicate a different representation.
12794
b9e61b2a 12795 CD1 := First_Discriminant (T1);
12796 CD2 := First_Discriminant (T2);
12797 while Present (CD1) and then Present (CD2) loop
d6f39728 12798 if not Same_Rep then
12799 return False;
12800 else
12801 Next_Discriminant (CD1);
12802 Next_Discriminant (CD2);
12803 end if;
12804 end loop;
12805 end if;
12806
12807 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12808 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
d6f39728 12809 while Present (CD1) loop
12810 if not Same_Rep then
12811 return False;
12812 else
12813 Next_Component (CD1);
12814 Next_Component (CD2);
12815 end if;
12816 end loop;
12817
12818 return True;
12819 end Record_Case;
12820 end if;
12821
12822 -- For enumeration types, we must check each literal to see if the
12823 -- representation is the same. Note that we do not permit enumeration
1a34e48c 12824 -- representation clauses for Character and Wide_Character, so these
d6f39728 12825 -- cases were already dealt with.
12826
12827 elsif Is_Enumeration_Type (T1) then
d6f39728 12828 Enumeration_Case : declare
12829 L1, L2 : Entity_Id;
12830
12831 begin
12832 L1 := First_Literal (T1);
12833 L2 := First_Literal (T2);
d6f39728 12834 while Present (L1) loop
12835 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12836 return False;
12837 else
12838 Next_Literal (L1);
12839 Next_Literal (L2);
12840 end if;
12841 end loop;
12842
12843 return True;
d6f39728 12844 end Enumeration_Case;
12845
12846 -- Any other types have the same representation for these purposes
12847
12848 else
12849 return True;
12850 end if;
d6f39728 12851 end Same_Representation;
12852
3061ffde 12853 --------------------------------
12854 -- Resolve_Iterable_Operation --
12855 --------------------------------
12856
12857 procedure Resolve_Iterable_Operation
12858 (N : Node_Id;
12859 Cursor : Entity_Id;
12860 Typ : Entity_Id;
12861 Nam : Name_Id)
12862 is
12863 Ent : Entity_Id;
12864 F1 : Entity_Id;
12865 F2 : Entity_Id;
12866
12867 begin
12868 if not Is_Overloaded (N) then
12869 if not Is_Entity_Name (N)
12870 or else Ekind (Entity (N)) /= E_Function
12871 or else Scope (Entity (N)) /= Scope (Typ)
12872 or else No (First_Formal (Entity (N)))
12873 or else Etype (First_Formal (Entity (N))) /= Typ
12874 then
12875 Error_Msg_N ("iterable primitive must be local function name "
12876 & "whose first formal is an iterable type", N);
a9f5fea7 12877 return;
3061ffde 12878 end if;
12879
12880 Ent := Entity (N);
12881 F1 := First_Formal (Ent);
12882 if Nam = Name_First then
12883
12884 -- First (Container) => Cursor
12885
12886 if Etype (Ent) /= Cursor then
12887 Error_Msg_N ("primitive for First must yield a curosr", N);
12888 end if;
12889
12890 elsif Nam = Name_Next then
12891
12892 -- Next (Container, Cursor) => Cursor
12893
12894 F2 := Next_Formal (F1);
12895
12896 if Etype (F2) /= Cursor
12897 or else Etype (Ent) /= Cursor
12898 or else Present (Next_Formal (F2))
12899 then
12900 Error_Msg_N ("no match for Next iterable primitive", N);
12901 end if;
12902
12903 elsif Nam = Name_Has_Element then
12904
12905 -- Has_Element (Container, Cursor) => Boolean
12906
12907 F2 := Next_Formal (F1);
12908 if Etype (F2) /= Cursor
12909 or else Etype (Ent) /= Standard_Boolean
12910 or else Present (Next_Formal (F2))
12911 then
12912 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12913 end if;
12914
12915 elsif Nam = Name_Element then
b9b03799 12916 F2 := Next_Formal (F1);
12917
12918 if No (F2)
12919 or else Etype (F2) /= Cursor
12920 or else Present (Next_Formal (F2))
12921 then
12922 Error_Msg_N ("no match for Element iterable primitive", N);
12923 end if;
3061ffde 12924 null;
12925
12926 else
12927 raise Program_Error;
12928 end if;
12929
12930 else
12931 -- Overloaded case: find subprogram with proper signature.
12932 -- Caller will report error if no match is found.
12933
12934 declare
12935 I : Interp_Index;
12936 It : Interp;
12937
12938 begin
12939 Get_First_Interp (N, I, It);
12940 while Present (It.Typ) loop
12941 if Ekind (It.Nam) = E_Function
b9b03799 12942 and then Scope (It.Nam) = Scope (Typ)
3061ffde 12943 and then Etype (First_Formal (It.Nam)) = Typ
12944 then
12945 F1 := First_Formal (It.Nam);
12946
12947 if Nam = Name_First then
12948 if Etype (It.Nam) = Cursor
12949 and then No (Next_Formal (F1))
12950 then
12951 Set_Entity (N, It.Nam);
12952 exit;
12953 end if;
12954
12955 elsif Nam = Name_Next then
12956 F2 := Next_Formal (F1);
12957
12958 if Present (F2)
12959 and then No (Next_Formal (F2))
12960 and then Etype (F2) = Cursor
12961 and then Etype (It.Nam) = Cursor
12962 then
12963 Set_Entity (N, It.Nam);
12964 exit;
12965 end if;
12966
12967 elsif Nam = Name_Has_Element then
12968 F2 := Next_Formal (F1);
12969
12970 if Present (F2)
12971 and then No (Next_Formal (F2))
12972 and then Etype (F2) = Cursor
12973 and then Etype (It.Nam) = Standard_Boolean
12974 then
12975 Set_Entity (N, It.Nam);
12976 F2 := Next_Formal (F1);
12977 exit;
12978 end if;
12979
12980 elsif Nam = Name_Element then
b9b03799 12981 F2 := Next_Formal (F1);
12982
3061ffde 12983 if Present (F2)
12984 and then No (Next_Formal (F2))
12985 and then Etype (F2) = Cursor
12986 then
12987 Set_Entity (N, It.Nam);
12988 exit;
12989 end if;
12990 end if;
12991 end if;
12992
12993 Get_Next_Interp (I, It);
12994 end loop;
12995 end;
12996 end if;
12997 end Resolve_Iterable_Operation;
12998
b77e4501 12999 ----------------
13000 -- Set_Biased --
13001 ----------------
13002
13003 procedure Set_Biased
13004 (E : Entity_Id;
13005 N : Node_Id;
13006 Msg : String;
13007 Biased : Boolean := True)
13008 is
13009 begin
13010 if Biased then
13011 Set_Has_Biased_Representation (E);
13012
13013 if Warn_On_Biased_Representation then
13014 Error_Msg_NE
1e3532e7 13015 ("?B?" & Msg & " forces biased representation for&", N, E);
b77e4501 13016 end if;
13017 end if;
13018 end Set_Biased;
13019
d6f39728 13020 --------------------
13021 -- Set_Enum_Esize --
13022 --------------------
13023
13024 procedure Set_Enum_Esize (T : Entity_Id) is
13025 Lo : Uint;
13026 Hi : Uint;
13027 Sz : Nat;
13028
13029 begin
13030 Init_Alignment (T);
13031
13032 -- Find the minimum standard size (8,16,32,64) that fits
13033
13034 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
13035 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
13036
13037 if Lo < 0 then
13038 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
f15731c4 13039 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 13040
13041 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13042 Sz := 16;
13043
13044 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13045 Sz := 32;
13046
13047 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13048 Sz := 64;
13049 end if;
13050
13051 else
13052 if Hi < Uint_2**08 then
f15731c4 13053 Sz := Standard_Character_Size; -- May be > 8 on some targets
d6f39728 13054
13055 elsif Hi < Uint_2**16 then
13056 Sz := 16;
13057
13058 elsif Hi < Uint_2**32 then
13059 Sz := 32;
13060
13061 else pragma Assert (Hi < Uint_2**63);
13062 Sz := 64;
13063 end if;
13064 end if;
13065
13066 -- That minimum is the proper size unless we have a foreign convention
13067 -- and the size required is 32 or less, in which case we bump the size
13068 -- up to 32. This is required for C and C++ and seems reasonable for
13069 -- all other foreign conventions.
13070
13071 if Has_Foreign_Convention (T)
13072 and then Esize (T) < Standard_Integer_Size
db1eed69 13073
13074 -- Don't do this if Short_Enums on target
13075
e9185b9d 13076 and then not Target_Short_Enums
d6f39728 13077 then
13078 Init_Esize (T, Standard_Integer_Size);
d6f39728 13079 else
13080 Init_Esize (T, Sz);
13081 end if;
d6f39728 13082 end Set_Enum_Esize;
13083
2625eb01 13084 -----------------------------
13085 -- Uninstall_Discriminants --
13086 -----------------------------
13087
13088 procedure Uninstall_Discriminants (E : Entity_Id) is
13089 Disc : Entity_Id;
13090 Prev : Entity_Id;
13091 Outer : Entity_Id;
13092
13093 begin
13094 -- Discriminants have been made visible for type declarations and
13095 -- protected type declarations, not for subtype declarations.
13096
13097 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13098 Disc := First_Discriminant (E);
13099 while Present (Disc) loop
13100 if Disc /= Current_Entity (Disc) then
13101 Prev := Current_Entity (Disc);
13102 while Present (Prev)
13103 and then Present (Homonym (Prev))
13104 and then Homonym (Prev) /= Disc
13105 loop
13106 Prev := Homonym (Prev);
13107 end loop;
13108 else
13109 Prev := Empty;
13110 end if;
13111
13112 Set_Is_Immediately_Visible (Disc, False);
13113
13114 Outer := Homonym (Disc);
13115 while Present (Outer) and then Scope (Outer) = E loop
13116 Outer := Homonym (Outer);
13117 end loop;
13118
13119 -- Reset homonym link of other entities, but do not modify link
3ff5e35d 13120 -- between entities in current scope, so that the back end can
2625eb01 13121 -- have a proper count of local overloadings.
13122
13123 if No (Prev) then
13124 Set_Name_Entity_Id (Chars (Disc), Outer);
13125
13126 elsif Scope (Prev) /= Scope (Disc) then
13127 Set_Homonym (Prev, Outer);
13128 end if;
13129
13130 Next_Discriminant (Disc);
13131 end loop;
13132 end if;
13133 end Uninstall_Discriminants;
13134
13135 -------------------------------------------
13136 -- Uninstall_Discriminants_And_Pop_Scope --
13137 -------------------------------------------
13138
13139 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13140 begin
13141 if Has_Discriminants (E) then
13142 Uninstall_Discriminants (E);
13143 Pop_Scope;
13144 end if;
13145 end Uninstall_Discriminants_And_Pop_Scope;
13146
83f8f0a6 13147 ------------------------------
13148 -- Validate_Address_Clauses --
13149 ------------------------------
13150
13151 procedure Validate_Address_Clauses is
c7a1569a 13152 function Offset_Value (Expr : Node_Id) return Uint;
13153 -- Given an Address attribute reference, return the value in bits of its
13154 -- offset from the first bit of the underlying entity, or 0 if it is not
13155 -- known at compile time.
13156
13157 ------------------
13158 -- Offset_Value --
13159 ------------------
13160
13161 function Offset_Value (Expr : Node_Id) return Uint is
13162 N : Node_Id := Prefix (Expr);
13163 Off : Uint;
13164 Val : Uint := Uint_0;
13165
13166 begin
13167 -- Climb the prefix chain and compute the cumulative offset
13168
13169 loop
13170 if Is_Entity_Name (N) then
13171 return Val;
13172
13173 elsif Nkind (N) = N_Selected_Component then
13174 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13175 if Off /= No_Uint and then Off >= Uint_0 then
13176 Val := Val + Off;
13177 N := Prefix (N);
13178 else
13179 return Uint_0;
13180 end if;
13181
13182 elsif Nkind (N) = N_Indexed_Component then
13183 Off := Indexed_Component_Bit_Offset (N);
13184 if Off /= No_Uint then
13185 Val := Val + Off;
13186 N := Prefix (N);
13187 else
13188 return Uint_0;
13189 end if;
13190
13191 else
13192 return Uint_0;
13193 end if;
13194 end loop;
13195 end Offset_Value;
13196
13197 -- Start of processing for Validate_Address_Clauses
13198
83f8f0a6 13199 begin
13200 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13201 declare
13202 ACCR : Address_Clause_Check_Record
13203 renames Address_Clause_Checks.Table (J);
13204
d6da7448 13205 Expr : Node_Id;
13206
83f8f0a6 13207 X_Alignment : Uint;
13208 Y_Alignment : Uint;
13209
13210 X_Size : Uint;
13211 Y_Size : Uint;
13212
c7a1569a 13213 X_Offs : Uint;
13214
83f8f0a6 13215 begin
13216 -- Skip processing of this entry if warning already posted
13217
13218 if not Address_Warning_Posted (ACCR.N) then
d6da7448 13219 Expr := Original_Node (Expression (ACCR.N));
83f8f0a6 13220
514a5555 13221 -- Get alignments, sizes and offset, if any
83f8f0a6 13222
d6da7448 13223 X_Alignment := Alignment (ACCR.X);
8650387e 13224 X_Size := Esize (ACCR.X);
514a5555 13225
13226 if Present (ACCR.Y) then
13227 Y_Alignment := Alignment (ACCR.Y);
8650387e 13228 Y_Size := Esize (ACCR.Y);
514a5555 13229 end if;
83f8f0a6 13230
c7a1569a 13231 if ACCR.Off
13232 and then Nkind (Expr) = N_Attribute_Reference
13233 and then Attribute_Name (Expr) = Name_Address
13234 then
13235 X_Offs := Offset_Value (Expr);
13236 else
13237 X_Offs := Uint_0;
13238 end if;
13239
514a5555 13240 -- Check for known value not multiple of alignment
13241
13242 if No (ACCR.Y) then
13243 if not Alignment_Checks_Suppressed (ACCR.X)
13244 and then X_Alignment /= 0
13245 and then ACCR.A mod X_Alignment /= 0
13246 then
13247 Error_Msg_NE
13248 ("??specified address for& is inconsistent with "
13249 & "alignment", ACCR.N, ACCR.X);
13250 Error_Msg_N
13251 ("\??program execution may be erroneous (RM 13.3(27))",
13252 ACCR.N);
13253
13254 Error_Msg_Uint_1 := X_Alignment;
13255 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13256 end if;
13257
83f8f0a6 13258 -- Check for large object overlaying smaller one
13259
514a5555 13260 elsif Y_Size > Uint_0
83f8f0a6 13261 and then X_Size > Uint_0
c7a1569a 13262 and then X_Offs + X_Size > Y_Size
83f8f0a6 13263 then
7161e166 13264 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
83f8f0a6 13265 Error_Msg_N
1e3532e7 13266 ("\??program execution may be erroneous", ACCR.N);
7161e166 13267
83f8f0a6 13268 Error_Msg_Uint_1 := X_Size;
7161e166 13269 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13270
83f8f0a6 13271 Error_Msg_Uint_1 := Y_Size;
7161e166 13272 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
83f8f0a6 13273
f5cc2579 13274 if Y_Size >= X_Size then
c7a1569a 13275 Error_Msg_Uint_1 := X_Offs;
f5cc2579 13276 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
c7a1569a 13277 end if;
13278
d6da7448 13279 -- Check for inadequate alignment, both of the base object
e556831e 13280 -- and of the offset, if any. We only do this check if the
13281 -- run-time Alignment_Check is active. No point in warning
13282 -- if this check has been suppressed (or is suppressed by
13283 -- default in the non-strict alignment machine case).
83f8f0a6 13284
d6da7448 13285 -- Note: we do not check the alignment if we gave a size
13286 -- warning, since it would likely be redundant.
83f8f0a6 13287
514a5555 13288 elsif not Alignment_Checks_Suppressed (ACCR.X)
e556831e 13289 and then Y_Alignment /= Uint_0
7161e166 13290 and then
13291 (Y_Alignment < X_Alignment
13292 or else
13293 (ACCR.Off
13294 and then Nkind (Expr) = N_Attribute_Reference
13295 and then Attribute_Name (Expr) = Name_Address
13296 and then Has_Compatible_Alignment
13297 (ACCR.X, Prefix (Expr), True) /=
13298 Known_Compatible))
83f8f0a6 13299 then
13300 Error_Msg_NE
7161e166 13301 ("??specified address for& may be inconsistent with "
13302 & "alignment", ACCR.N, ACCR.X);
83f8f0a6 13303 Error_Msg_N
1e3532e7 13304 ("\??program execution may be erroneous (RM 13.3(27))",
83f8f0a6 13305 ACCR.N);
7161e166 13306
83f8f0a6 13307 Error_Msg_Uint_1 := X_Alignment;
7161e166 13308 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13309
83f8f0a6 13310 Error_Msg_Uint_1 := Y_Alignment;
7161e166 13311 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13312
d6da7448 13313 if Y_Alignment >= X_Alignment then
13314 Error_Msg_N
7161e166 13315 ("\??but offset is not multiple of alignment", ACCR.N);
d6da7448 13316 end if;
83f8f0a6 13317 end if;
13318 end if;
13319 end;
13320 end loop;
13321 end Validate_Address_Clauses;
13322
7717ea00 13323 ---------------------------
13324 -- Validate_Independence --
13325 ---------------------------
13326
13327 procedure Validate_Independence is
13328 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13329 N : Node_Id;
13330 E : Entity_Id;
13331 IC : Boolean;
13332 Comp : Entity_Id;
13333 Addr : Node_Id;
13334 P : Node_Id;
13335
13336 procedure Check_Array_Type (Atyp : Entity_Id);
13337 -- Checks if the array type Atyp has independent components, and
13338 -- if not, outputs an appropriate set of error messages.
13339
13340 procedure No_Independence;
13341 -- Output message that independence cannot be guaranteed
13342
13343 function OK_Component (C : Entity_Id) return Boolean;
13344 -- Checks one component to see if it is independently accessible, and
13345 -- if so yields True, otherwise yields False if independent access
13346 -- cannot be guaranteed. This is a conservative routine, it only
13347 -- returns True if it knows for sure, it returns False if it knows
13348 -- there is a problem, or it cannot be sure there is no problem.
13349
13350 procedure Reason_Bad_Component (C : Entity_Id);
13351 -- Outputs continuation message if a reason can be determined for
13352 -- the component C being bad.
13353
13354 ----------------------
13355 -- Check_Array_Type --
13356 ----------------------
13357
13358 procedure Check_Array_Type (Atyp : Entity_Id) is
13359 Ctyp : constant Entity_Id := Component_Type (Atyp);
13360
13361 begin
13362 -- OK if no alignment clause, no pack, and no component size
13363
13364 if not Has_Component_Size_Clause (Atyp)
13365 and then not Has_Alignment_Clause (Atyp)
13366 and then not Is_Packed (Atyp)
13367 then
13368 return;
13369 end if;
13370
aa0a69ab 13371 -- Case of component size is greater than or equal to 64 and the
13372 -- alignment of the array is at least as large as the alignment
13373 -- of the component. We are definitely OK in this situation.
13374
13375 if Known_Component_Size (Atyp)
13376 and then Component_Size (Atyp) >= 64
13377 and then Known_Alignment (Atyp)
13378 and then Known_Alignment (Ctyp)
13379 and then Alignment (Atyp) >= Alignment (Ctyp)
13380 then
13381 return;
13382 end if;
13383
7717ea00 13384 -- Check actual component size
13385
13386 if not Known_Component_Size (Atyp)
13387 or else not (Addressable (Component_Size (Atyp))
aa0a69ab 13388 and then Component_Size (Atyp) < 64)
7717ea00 13389 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13390 then
13391 No_Independence;
13392
13393 -- Bad component size, check reason
13394
13395 if Has_Component_Size_Clause (Atyp) then
b9e61b2a 13396 P := Get_Attribute_Definition_Clause
13397 (Atyp, Attribute_Component_Size);
7717ea00 13398
13399 if Present (P) then
13400 Error_Msg_Sloc := Sloc (P);
13401 Error_Msg_N ("\because of Component_Size clause#", N);
13402 return;
13403 end if;
13404 end if;
13405
13406 if Is_Packed (Atyp) then
13407 P := Get_Rep_Pragma (Atyp, Name_Pack);
13408
13409 if Present (P) then
13410 Error_Msg_Sloc := Sloc (P);
13411 Error_Msg_N ("\because of pragma Pack#", N);
13412 return;
13413 end if;
13414 end if;
13415
13416 -- No reason found, just return
13417
13418 return;
13419 end if;
13420
13421 -- Array type is OK independence-wise
13422
13423 return;
13424 end Check_Array_Type;
13425
13426 ---------------------
13427 -- No_Independence --
13428 ---------------------
13429
13430 procedure No_Independence is
13431 begin
13432 if Pragma_Name (N) = Name_Independent then
18393965 13433 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
7717ea00 13434 else
13435 Error_Msg_NE
13436 ("independent components cannot be guaranteed for&", N, E);
13437 end if;
13438 end No_Independence;
13439
13440 ------------------
13441 -- OK_Component --
13442 ------------------
13443
13444 function OK_Component (C : Entity_Id) return Boolean is
13445 Rec : constant Entity_Id := Scope (C);
13446 Ctyp : constant Entity_Id := Etype (C);
13447
13448 begin
13449 -- OK if no component clause, no Pack, and no alignment clause
13450
13451 if No (Component_Clause (C))
13452 and then not Is_Packed (Rec)
13453 and then not Has_Alignment_Clause (Rec)
13454 then
13455 return True;
13456 end if;
13457
13458 -- Here we look at the actual component layout. A component is
13459 -- addressable if its size is a multiple of the Esize of the
13460 -- component type, and its starting position in the record has
13461 -- appropriate alignment, and the record itself has appropriate
13462 -- alignment to guarantee the component alignment.
13463
13464 -- Make sure sizes are static, always assume the worst for any
13465 -- cases where we cannot check static values.
13466
13467 if not (Known_Static_Esize (C)
b9e61b2a 13468 and then
13469 Known_Static_Esize (Ctyp))
7717ea00 13470 then
13471 return False;
13472 end if;
13473
13474 -- Size of component must be addressable or greater than 64 bits
13475 -- and a multiple of bytes.
13476
b9e61b2a 13477 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
7717ea00 13478 return False;
13479 end if;
13480
13481 -- Check size is proper multiple
13482
13483 if Esize (C) mod Esize (Ctyp) /= 0 then
13484 return False;
13485 end if;
13486
13487 -- Check alignment of component is OK
13488
13489 if not Known_Component_Bit_Offset (C)
13490 or else Component_Bit_Offset (C) < Uint_0
13491 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13492 then
13493 return False;
13494 end if;
13495
13496 -- Check alignment of record type is OK
13497
13498 if not Known_Alignment (Rec)
13499 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13500 then
13501 return False;
13502 end if;
13503
13504 -- All tests passed, component is addressable
13505
13506 return True;
13507 end OK_Component;
13508
13509 --------------------------
13510 -- Reason_Bad_Component --
13511 --------------------------
13512
13513 procedure Reason_Bad_Component (C : Entity_Id) is
13514 Rec : constant Entity_Id := Scope (C);
13515 Ctyp : constant Entity_Id := Etype (C);
13516
13517 begin
13518 -- If component clause present assume that's the problem
13519
13520 if Present (Component_Clause (C)) then
13521 Error_Msg_Sloc := Sloc (Component_Clause (C));
13522 Error_Msg_N ("\because of Component_Clause#", N);
13523 return;
13524 end if;
13525
13526 -- If pragma Pack clause present, assume that's the problem
13527
13528 if Is_Packed (Rec) then
13529 P := Get_Rep_Pragma (Rec, Name_Pack);
13530
13531 if Present (P) then
13532 Error_Msg_Sloc := Sloc (P);
13533 Error_Msg_N ("\because of pragma Pack#", N);
13534 return;
13535 end if;
13536 end if;
13537
13538 -- See if record has bad alignment clause
13539
13540 if Has_Alignment_Clause (Rec)
13541 and then Known_Alignment (Rec)
13542 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13543 then
13544 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13545
13546 if Present (P) then
13547 Error_Msg_Sloc := Sloc (P);
13548 Error_Msg_N ("\because of Alignment clause#", N);
13549 end if;
13550 end if;
13551
13552 -- Couldn't find a reason, so return without a message
13553
13554 return;
13555 end Reason_Bad_Component;
13556
13557 -- Start of processing for Validate_Independence
13558
13559 begin
13560 for J in Independence_Checks.First .. Independence_Checks.Last loop
13561 N := Independence_Checks.Table (J).N;
13562 E := Independence_Checks.Table (J).E;
13563 IC := Pragma_Name (N) = Name_Independent_Components;
13564
13565 -- Deal with component case
13566
13567 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13568 if not OK_Component (E) then
13569 No_Independence;
13570 Reason_Bad_Component (E);
13571 goto Continue;
13572 end if;
13573 end if;
13574
13575 -- Deal with record with Independent_Components
13576
13577 if IC and then Is_Record_Type (E) then
13578 Comp := First_Component_Or_Discriminant (E);
13579 while Present (Comp) loop
13580 if not OK_Component (Comp) then
13581 No_Independence;
13582 Reason_Bad_Component (Comp);
13583 goto Continue;
13584 end if;
13585
13586 Next_Component_Or_Discriminant (Comp);
13587 end loop;
13588 end if;
13589
13590 -- Deal with address clause case
13591
13592 if Is_Object (E) then
13593 Addr := Address_Clause (E);
13594
13595 if Present (Addr) then
13596 No_Independence;
13597 Error_Msg_Sloc := Sloc (Addr);
13598 Error_Msg_N ("\because of Address clause#", N);
13599 goto Continue;
13600 end if;
13601 end if;
13602
13603 -- Deal with independent components for array type
13604
13605 if IC and then Is_Array_Type (E) then
13606 Check_Array_Type (E);
13607 end if;
13608
13609 -- Deal with independent components for array object
13610
13611 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13612 Check_Array_Type (Etype (E));
13613 end if;
13614
13615 <<Continue>> null;
13616 end loop;
13617 end Validate_Independence;
13618
b3f8228a 13619 ------------------------------
13620 -- Validate_Iterable_Aspect --
13621 ------------------------------
13622
13623 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
3061ffde 13624 Assoc : Node_Id;
13625 Expr : Node_Id;
b3f8228a 13626
bde03454 13627 Prim : Node_Id;
a9f5fea7 13628 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
b3f8228a 13629
13630 First_Id : Entity_Id;
13631 Next_Id : Entity_Id;
13632 Has_Element_Id : Entity_Id;
13633 Element_Id : Entity_Id;
13634
b3f8228a 13635 begin
9698629c 13636 -- If previous error aspect is unusable
a9f5fea7 13637
13638 if Cursor = Any_Type then
3061ffde 13639 return;
13640 end if;
b3f8228a 13641
13642 First_Id := Empty;
13643 Next_Id := Empty;
13644 Has_Element_Id := Empty;
32de816b 13645 Element_Id := Empty;
b3f8228a 13646
13647 -- Each expression must resolve to a function with the proper signature
13648
13649 Assoc := First (Component_Associations (Expression (ASN)));
13650 while Present (Assoc) loop
13651 Expr := Expression (Assoc);
13652 Analyze (Expr);
13653
b3f8228a 13654 Prim := First (Choices (Assoc));
bde03454 13655
f02a9a9a 13656 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
b3f8228a 13657 Error_Msg_N ("illegal name in association", Prim);
13658
13659 elsif Chars (Prim) = Name_First then
3061ffde 13660 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
b3f8228a 13661 First_Id := Entity (Expr);
b3f8228a 13662
13663 elsif Chars (Prim) = Name_Next then
3061ffde 13664 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
b3f8228a 13665 Next_Id := Entity (Expr);
b3f8228a 13666
13667 elsif Chars (Prim) = Name_Has_Element then
3061ffde 13668 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
b3f8228a 13669 Has_Element_Id := Entity (Expr);
bde03454 13670
b3f8228a 13671 elsif Chars (Prim) = Name_Element then
3061ffde 13672 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
b3f8228a 13673 Element_Id := Entity (Expr);
b3f8228a 13674
13675 else
13676 Error_Msg_N ("invalid name for iterable function", Prim);
13677 end if;
13678
13679 Next (Assoc);
13680 end loop;
13681
13682 if No (First_Id) then
3061ffde 13683 Error_Msg_N ("match for First primitive not found", ASN);
b3f8228a 13684
13685 elsif No (Next_Id) then
3061ffde 13686 Error_Msg_N ("match for Next primitive not found", ASN);
b3f8228a 13687
13688 elsif No (Has_Element_Id) then
3061ffde 13689 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13690
13691 elsif No (Element_Id) then
13692 null; -- Optional.
b3f8228a 13693 end if;
13694 end Validate_Iterable_Aspect;
13695
d6f39728 13696 -----------------------------------
13697 -- Validate_Unchecked_Conversion --
13698 -----------------------------------
13699
13700 procedure Validate_Unchecked_Conversion
13701 (N : Node_Id;
13702 Act_Unit : Entity_Id)
13703 is
13704 Source : Entity_Id;
13705 Target : Entity_Id;
13706 Vnode : Node_Id;
13707
13708 begin
13709 -- Obtain source and target types. Note that we call Ancestor_Subtype
13710 -- here because the processing for generic instantiation always makes
13711 -- subtypes, and we want the original frozen actual types.
13712
13713 -- If we are dealing with private types, then do the check on their
13714 -- fully declared counterparts if the full declarations have been
39a0c1d3 13715 -- encountered (they don't have to be visible, but they must exist).
d6f39728 13716
13717 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13718
13719 if Is_Private_Type (Source)
13720 and then Present (Underlying_Type (Source))
13721 then
13722 Source := Underlying_Type (Source);
13723 end if;
13724
13725 Target := Ancestor_Subtype (Etype (Act_Unit));
13726
fdd294d1 13727 -- If either type is generic, the instantiation happens within a generic
95deda50 13728 -- unit, and there is nothing to check. The proper check will happen
13729 -- when the enclosing generic is instantiated.
d6f39728 13730
13731 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13732 return;
13733 end if;
13734
13735 if Is_Private_Type (Target)
13736 and then Present (Underlying_Type (Target))
13737 then
13738 Target := Underlying_Type (Target);
13739 end if;
13740
0924014e 13741 -- Source may be unconstrained array, but not target, except in relaxed
13742 -- semantics mode.
d6f39728 13743
0924014e 13744 if Is_Array_Type (Target)
13745 and then not Is_Constrained (Target)
13746 and then not Relaxed_RM_Semantics
13747 then
d6f39728 13748 Error_Msg_N
13749 ("unchecked conversion to unconstrained array not allowed", N);
13750 return;
13751 end if;
13752
fbc67f84 13753 -- Warn if conversion between two different convention pointers
13754
13755 if Is_Access_Type (Target)
13756 and then Is_Access_Type (Source)
13757 and then Convention (Target) /= Convention (Source)
13758 and then Warn_On_Unchecked_Conversion
13759 then
74c7ae52 13760 -- Give warnings for subprogram pointers only on most targets
fdd294d1 13761
13762 if Is_Access_Subprogram_Type (Target)
13763 or else Is_Access_Subprogram_Type (Source)
fdd294d1 13764 then
13765 Error_Msg_N
cb97ae5c 13766 ("?z?conversion between pointers with different conventions!",
1e3532e7 13767 N);
fdd294d1 13768 end if;
fbc67f84 13769 end if;
13770
3062c401 13771 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13772 -- warning when compiling GNAT-related sources.
13773
13774 if Warn_On_Unchecked_Conversion
13775 and then not In_Predefined_Unit (N)
13776 and then RTU_Loaded (Ada_Calendar)
f02a9a9a 13777 and then (Chars (Source) = Name_Time
13778 or else
13779 Chars (Target) = Name_Time)
3062c401 13780 then
13781 -- If Ada.Calendar is loaded and the name of one of the operands is
13782 -- Time, there is a good chance that this is Ada.Calendar.Time.
13783
13784 declare
f02a9a9a 13785 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
3062c401 13786 begin
13787 pragma Assert (Present (Calendar_Time));
13788
b9e61b2a 13789 if Source = Calendar_Time or else Target = Calendar_Time then
3062c401 13790 Error_Msg_N
f02a9a9a 13791 ("?z?representation of 'Time values may change between "
13792 & "'G'N'A'T versions", N);
3062c401 13793 end if;
13794 end;
13795 end if;
13796
fdd294d1 13797 -- Make entry in unchecked conversion table for later processing by
13798 -- Validate_Unchecked_Conversions, which will check sizes and alignments
3ff5e35d 13799 -- (using values set by the back end where possible). This is only done
fdd294d1 13800 -- if the appropriate warning is active.
d6f39728 13801
9dfe12ae 13802 if Warn_On_Unchecked_Conversion then
13803 Unchecked_Conversions.Append
86d32751 13804 (New_Val => UC_Entry'(Eloc => Sloc (N),
13805 Source => Source,
13806 Target => Target,
13807 Act_Unit => Act_Unit));
9dfe12ae 13808
f9906591 13809 -- If both sizes are known statically now, then back-end annotation
9dfe12ae 13810 -- is not required to do a proper check but if either size is not
13811 -- known statically, then we need the annotation.
13812
13813 if Known_Static_RM_Size (Source)
1e3532e7 13814 and then
13815 Known_Static_RM_Size (Target)
9dfe12ae 13816 then
13817 null;
13818 else
13819 Back_Annotate_Rep_Info := True;
13820 end if;
13821 end if;
d6f39728 13822
fdd294d1 13823 -- If unchecked conversion to access type, and access type is declared
95deda50 13824 -- in the same unit as the unchecked conversion, then set the flag
13825 -- No_Strict_Aliasing (no strict aliasing is implicit here)
28ed91d4 13826
13827 if Is_Access_Type (Target) and then
13828 In_Same_Source_Unit (Target, N)
13829 then
13830 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13831 end if;
3d875462 13832
95deda50 13833 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13834 -- the back end needs to perform special validation checks.
3d875462 13835
95deda50 13836 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13837 -- have full expansion and the back end is called ???
3d875462 13838
13839 Vnode :=
13840 Make_Validate_Unchecked_Conversion (Sloc (N));
13841 Set_Source_Type (Vnode, Source);
13842 Set_Target_Type (Vnode, Target);
13843
fdd294d1 13844 -- If the unchecked conversion node is in a list, just insert before it.
13845 -- If not we have some strange case, not worth bothering about.
3d875462 13846
13847 if Is_List_Member (N) then
d6f39728 13848 Insert_After (N, Vnode);
13849 end if;
13850 end Validate_Unchecked_Conversion;
13851
13852 ------------------------------------
13853 -- Validate_Unchecked_Conversions --
13854 ------------------------------------
13855
13856 procedure Validate_Unchecked_Conversions is
13857 begin
13858 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13859 declare
13860 T : UC_Entry renames Unchecked_Conversions.Table (N);
13861
e13b1635 13862 Act_Unit : constant Entity_Id := T.Act_Unit;
86d32751 13863 Eloc : constant Source_Ptr := T.Eloc;
13864 Source : constant Entity_Id := T.Source;
13865 Target : constant Entity_Id := T.Target;
d6f39728 13866
44705307 13867 Source_Siz : Uint;
13868 Target_Siz : Uint;
d6f39728 13869
13870 begin
86d32751 13871 -- Skip if function marked as warnings off
13872
13873 if Warnings_Off (Act_Unit) then
13874 goto Continue;
13875 end if;
13876
fdd294d1 13877 -- This validation check, which warns if we have unequal sizes for
13878 -- unchecked conversion, and thus potentially implementation
d6f39728 13879 -- dependent semantics, is one of the few occasions on which we
fdd294d1 13880 -- use the official RM size instead of Esize. See description in
13881 -- Einfo "Handling of Type'Size Values" for details.
d6f39728 13882
f15731c4 13883 if Serious_Errors_Detected = 0
d6f39728 13884 and then Known_Static_RM_Size (Source)
13885 and then Known_Static_RM_Size (Target)
f25f4252 13886
13887 -- Don't do the check if warnings off for either type, note the
13888 -- deliberate use of OR here instead of OR ELSE to get the flag
13889 -- Warnings_Off_Used set for both types if appropriate.
13890
13891 and then not (Has_Warnings_Off (Source)
13892 or
13893 Has_Warnings_Off (Target))
d6f39728 13894 then
13895 Source_Siz := RM_Size (Source);
13896 Target_Siz := RM_Size (Target);
13897
13898 if Source_Siz /= Target_Siz then
299480f9 13899 Error_Msg
cb97ae5c 13900 ("?z?types for unchecked conversion have different sizes!",
299480f9 13901 Eloc);
d6f39728 13902
13903 if All_Errors_Mode then
13904 Error_Msg_Name_1 := Chars (Source);
13905 Error_Msg_Uint_1 := Source_Siz;
13906 Error_Msg_Name_2 := Chars (Target);
13907 Error_Msg_Uint_2 := Target_Siz;
cb97ae5c 13908 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
d6f39728 13909
13910 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13911
13912 if Is_Discrete_Type (Source)
b9e61b2a 13913 and then
13914 Is_Discrete_Type (Target)
d6f39728 13915 then
13916 if Source_Siz > Target_Siz then
299480f9 13917 Error_Msg
cb97ae5c 13918 ("\?z?^ high order bits of source will "
1e3532e7 13919 & "be ignored!", Eloc);
d6f39728 13920
9dfe12ae 13921 elsif Is_Unsigned_Type (Source) then
299480f9 13922 Error_Msg
cb97ae5c 13923 ("\?z?source will be extended with ^ high order "
1581f2d7 13924 & "zero bits!", Eloc);
d6f39728 13925
13926 else
299480f9 13927 Error_Msg
cb97ae5c 13928 ("\?z?source will be extended with ^ high order "
1e3532e7 13929 & "sign bits!", Eloc);
d6f39728 13930 end if;
13931
13932 elsif Source_Siz < Target_Siz then
13933 if Is_Discrete_Type (Target) then
13934 if Bytes_Big_Endian then
299480f9 13935 Error_Msg
cb97ae5c 13936 ("\?z?target value will include ^ undefined "
1e3532e7 13937 & "low order bits!", Eloc);
d6f39728 13938 else
299480f9 13939 Error_Msg
cb97ae5c 13940 ("\?z?target value will include ^ undefined "
1e3532e7 13941 & "high order bits!", Eloc);
d6f39728 13942 end if;
13943
13944 else
299480f9 13945 Error_Msg
cb97ae5c 13946 ("\?z?^ trailing bits of target value will be "
1e3532e7 13947 & "undefined!", Eloc);
d6f39728 13948 end if;
13949
13950 else pragma Assert (Source_Siz > Target_Siz);
0388e54e 13951 if Is_Discrete_Type (Source) then
13952 if Bytes_Big_Endian then
13953 Error_Msg
13954 ("\?z?^ low order bits of source will be "
13955 & "ignored!", Eloc);
13956 else
13957 Error_Msg
13958 ("\?z?^ high order bits of source will be "
13959 & "ignored!", Eloc);
13960 end if;
13961
13962 else
13963 Error_Msg
13964 ("\?z?^ trailing bits of source will be "
13965 & "ignored!", Eloc);
13966 end if;
d6f39728 13967 end if;
13968 end if;
d6f39728 13969 end if;
13970 end if;
13971
13972 -- If both types are access types, we need to check the alignment.
13973 -- If the alignment of both is specified, we can do it here.
13974
f15731c4 13975 if Serious_Errors_Detected = 0
2a10e737 13976 and then Is_Access_Type (Source)
13977 and then Is_Access_Type (Target)
d6f39728 13978 and then Target_Strict_Alignment
13979 and then Present (Designated_Type (Source))
13980 and then Present (Designated_Type (Target))
13981 then
13982 declare
13983 D_Source : constant Entity_Id := Designated_Type (Source);
13984 D_Target : constant Entity_Id := Designated_Type (Target);
13985
13986 begin
13987 if Known_Alignment (D_Source)
b9e61b2a 13988 and then
13989 Known_Alignment (D_Target)
d6f39728 13990 then
13991 declare
13992 Source_Align : constant Uint := Alignment (D_Source);
13993 Target_Align : constant Uint := Alignment (D_Target);
13994
13995 begin
13996 if Source_Align < Target_Align
13997 and then not Is_Tagged_Type (D_Source)
f25f4252 13998
13999 -- Suppress warning if warnings suppressed on either
14000 -- type or either designated type. Note the use of
14001 -- OR here instead of OR ELSE. That is intentional,
14002 -- we would like to set flag Warnings_Off_Used in
14003 -- all types for which warnings are suppressed.
14004
14005 and then not (Has_Warnings_Off (D_Source)
14006 or
14007 Has_Warnings_Off (D_Target)
14008 or
14009 Has_Warnings_Off (Source)
14010 or
14011 Has_Warnings_Off (Target))
d6f39728 14012 then
d6f39728 14013 Error_Msg_Uint_1 := Target_Align;
14014 Error_Msg_Uint_2 := Source_Align;
299480f9 14015 Error_Msg_Node_1 := D_Target;
d6f39728 14016 Error_Msg_Node_2 := D_Source;
299480f9 14017 Error_Msg
cb97ae5c 14018 ("?z?alignment of & (^) is stricter than "
1e3532e7 14019 & "alignment of & (^)!", Eloc);
f25f4252 14020 Error_Msg
cb97ae5c 14021 ("\?z?resulting access value may have invalid "
1e3532e7 14022 & "alignment!", Eloc);
d6f39728 14023 end if;
14024 end;
14025 end if;
14026 end;
14027 end if;
14028 end;
86d32751 14029
14030 <<Continue>>
14031 null;
d6f39728 14032 end loop;
14033 end Validate_Unchecked_Conversions;
14034
d6f39728 14035end Sem_Ch13;