]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch13.adb
sem_ch13.adb (Visible_Component): New procedure...
[thirdparty/gcc.git] / gcc / ada / sem_ch13.adb
CommitLineData
996ae0b0 1------------------------------------------------------------------------------
82c80734 2-- --
996ae0b0
RK
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 1 3 --
6-- --
7-- B o d y --
8-- --
6905a049 9-- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
0f1a6a0b 26with Aspects; use Aspects;
996ae0b0 27with Atree; use Atree;
d087cd96 28with Checks; use Checks;
8bfbd380 29with Debug; use Debug;
996ae0b0 30with Einfo; use Einfo;
cefce34c 31with Elists; use Elists;
996ae0b0 32with Errout; use Errout;
cefce34c 33with Exp_Disp; use Exp_Disp;
996ae0b0
RK
34with Exp_Tss; use Exp_Tss;
35with Exp_Util; use Exp_Util;
8b034336 36with Freeze; use Freeze;
241ebe89 37with Ghost; use Ghost;
996ae0b0 38with Lib; use Lib;
2642f998 39with Lib.Xref; use Lib.Xref;
8f7770f9 40with Namet; use Namet;
996ae0b0
RK
41with Nlists; use Nlists;
42with Nmake; use Nmake;
43with Opt; use Opt;
5f3ab6fb
AC
44with Restrict; use Restrict;
45with Rident; use Rident;
996ae0b0
RK
46with Rtsfind; use Rtsfind;
47with Sem; use Sem;
a4100e55 48with Sem_Aux; use Sem_Aux;
c76bf0bf 49with Sem_Case; use Sem_Case;
3ff38f33 50with Sem_Ch3; use Sem_Ch3;
f2acf80c 51with Sem_Ch6; use Sem_Ch6;
996ae0b0 52with Sem_Ch8; use Sem_Ch8;
dec6faf1 53with Sem_Dim; use Sem_Dim;
70805b88 54with Sem_Disp; use Sem_Disp;
996ae0b0 55with Sem_Eval; use Sem_Eval;
aab45d22 56with Sem_Prag; use Sem_Prag;
996ae0b0
RK
57with Sem_Res; use Sem_Res;
58with Sem_Type; use Sem_Type;
59with Sem_Util; use Sem_Util;
affbee12 60with Sem_Warn; use Sem_Warn;
c775c209 61with Sinput; use Sinput;
fbf5a39b 62with Snames; use Snames;
996ae0b0
RK
63with Stand; use Stand;
64with Sinfo; use Sinfo;
9f4fd324 65with Targparm; use Targparm;
996ae0b0
RK
66with Ttypes; use Ttypes;
67with Tbuild; use Tbuild;
68with Urealp; use Urealp;
499769ec 69with Warnsw; use Warnsw;
996ae0b0 70
d4731b80 71with GNAT.Heap_Sort_G;
996ae0b0
RK
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
76af4137
AC
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.
996ae0b0 87
baa571ab 88 procedure Build_Discrete_Static_Predicate
1e194575
AC
89 (Typ : Entity_Id;
90 Expr : Node_Id;
91 Nam : Name_Id);
f6b5dc8e
AC
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
308e6f3a 96 -- predicate expression have been replaced by identifier references to this
f6b5dc8e
AC
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
60f908dd
RD
100 -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is
101 -- rewritten as a canonicalized membership operation.
1e194575 102
2e885a6f
AC
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
6905a049
AC
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
baa571ab
AC
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.
6905a049
AC
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.
baa571ab 128
113a62d9
RD
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
5a8a6763
RD
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
e1e307d9 138 -- we have to be sure the subtypes in question are frozen before checking).
5a8a6763 139
996ae0b0
RK
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
2e885a6f
AC
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
996ae0b0 166 function Is_Operational_Item (N : Node_Id) return Boolean;
c775c209
AC
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.
996ae0b0 171
ee4eee0a
AC
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
affbee12 200 procedure New_Stream_Subprogram
996ae0b0
RK
201 (N : Node_Id;
202 Ent : Entity_Id;
203 Subp : Entity_Id;
fbf5a39b 204 Nam : TSS_Name_Type);
affbee12
RD
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
1fb63e89 207 -- primitive operation, or in the untagged case make an appropriate TSS
affbee12 208 -- entry. This is more properly an expansion activity than just semantics,
1fb63e89
RD
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
affbee12
RD
211 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
212 -- function to be generated.
fbf5a39b 213 --
07fc65c4
GB
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
b4f149c2
AC
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
a3f2babd
AC
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
996ae0b0
RK
238 ----------------------------------------------
239 -- Table for Validate_Unchecked_Conversions --
240 ----------------------------------------------
241
242 -- The following table collects unchecked conversions for validation.
a1092b48
AC
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.
996ae0b0 248
a1092b48
AC
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.
f66d46ec 252
996ae0b0 253 type UC_Entry is record
4c51ff88
AC
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
996ae0b0
RK
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
2642f998
RD
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
a1092b48
AC
276 -- where Expr is of the form Y'Address or recursively is a reference to a
277 -- constant of either of these forms, and X and Y are entities of objects,
278 -- then if Y has a smaller alignment than X, that merits a warning about
279 -- possible bad alignment. The following table collects address clauses of
280 -- this kind. We put these in a table so that they can be checked after the
281 -- back end has completed annotation of the alignments of objects, since we
282 -- can catch more cases that way.
2642f998
RD
283
284 type Address_Clause_Check_Record is record
285 N : Node_Id;
286 -- The address clause
287
288 X : Entity_Id;
289 -- The entity of the object overlaying Y
290
291 Y : Entity_Id;
292 -- The entity of the object being overlaid
f4cd2542
EB
293
294 Off : Boolean;
308e6f3a 295 -- Whether the address is offset within Y
2642f998
RD
296 end record;
297
298 package Address_Clause_Checks is new Table.Table (
299 Table_Component_Type => Address_Clause_Check_Record,
300 Table_Index_Type => Int,
301 Table_Low_Bound => 1,
302 Table_Initial => 20,
303 Table_Increment => 200,
304 Table_Name => "Address_Clause_Checks");
305
a9a5b8ac
RD
306 -----------------------------------------
307 -- Adjust_Record_For_Reverse_Bit_Order --
308 -----------------------------------------
309
310 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
8a95f4e8
RD
311 Comp : Node_Id;
312 CC : Node_Id;
a9a5b8ac
RD
313
314 begin
8a95f4e8 315 -- Processing depends on version of Ada
a9a5b8ac 316
498d1b80 317 -- For Ada 95, we just renumber bits within a storage unit. We do the
8f66cda7 318 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
9b20e59b 319 -- Ada 83, and are free to add this extension.
498d1b80
AC
320
321 if Ada_Version < Ada_2005 then
322 Comp := First_Component_Or_Discriminant (R);
323 while Present (Comp) loop
324 CC := Component_Clause (Comp);
325
326 -- If component clause is present, then deal with the non-default
327 -- bit order case for Ada 95 mode.
328
329 -- We only do this processing for the base type, and in fact that
330 -- is important, since otherwise if there are record subtypes, we
331 -- could reverse the bits once for each subtype, which is wrong.
332
616547fa 333 if Present (CC) and then Ekind (R) = E_Record_Type then
498d1b80
AC
334 declare
335 CFB : constant Uint := Component_Bit_Offset (Comp);
336 CSZ : constant Uint := Esize (Comp);
337 CLC : constant Node_Id := Component_Clause (Comp);
338 Pos : constant Node_Id := Position (CLC);
339 FB : constant Node_Id := First_Bit (CLC);
340
341 Storage_Unit_Offset : constant Uint :=
342 CFB / System_Storage_Unit;
343
344 Start_Bit : constant Uint :=
345 CFB mod System_Storage_Unit;
a9a5b8ac 346
498d1b80
AC
347 begin
348 -- Cases where field goes over storage unit boundary
a9a5b8ac 349
498d1b80 350 if Start_Bit + CSZ > System_Storage_Unit then
a9a5b8ac 351
498d1b80 352 -- Allow multi-byte field but generate warning
a9a5b8ac 353
498d1b80
AC
354 if Start_Bit mod System_Storage_Unit = 0
355 and then CSZ mod System_Storage_Unit = 0
356 then
357 Error_Msg_N
ee10c0ec
RD
358 ("info: multi-byte field specified with "
359 & "non-standard Bit_Order?V?", CLC);
5e5dc75a 360
498d1b80 361 if Bytes_Big_Endian then
5e5dc75a 362 Error_Msg_N
ee10c0ec
RD
363 ("\bytes are not reversed "
364 & "(component is big-endian)?V?", CLC);
5e5dc75a
RD
365 else
366 Error_Msg_N
ee10c0ec
RD
367 ("\bytes are not reversed "
368 & "(component is little-endian)?V?", CLC);
5e5dc75a 369 end if;
a9a5b8ac 370
4afcf3a5 371 -- Do not allow non-contiguous field
a9a5b8ac 372
8a95f4e8 373 else
498d1b80
AC
374 Error_Msg_N
375 ("attempt to specify non-contiguous field "
376 & "not permitted", CLC);
377 Error_Msg_N
378 ("\caused by non-standard Bit_Order "
379 & "specified", CLC);
380 Error_Msg_N
381 ("\consider possibility of using "
382 & "Ada 2005 mode here", CLC);
383 end if;
a9a5b8ac 384
498d1b80 385 -- Case where field fits in one storage unit
a9a5b8ac 386
498d1b80
AC
387 else
388 -- Give warning if suspicious component clause
a9a5b8ac 389
498d1b80
AC
390 if Intval (FB) >= System_Storage_Unit
391 and then Warn_On_Reverse_Bit_Order
392 then
393 Error_Msg_N
ee10c0ec 394 ("info: Bit_Order clause does not affect " &
dbfeb4fa 395 "byte ordering?V?", Pos);
498d1b80
AC
396 Error_Msg_Uint_1 :=
397 Intval (Pos) + Intval (FB) /
398 System_Storage_Unit;
399 Error_Msg_N
ee10c0ec 400 ("info: position normalized to ^ before bit " &
dbfeb4fa 401 "order interpreted?V?", Pos);
498d1b80 402 end if;
a9a5b8ac 403
498d1b80
AC
404 -- Here is where we fix up the Component_Bit_Offset value
405 -- to account for the reverse bit order. Some examples of
406 -- what needs to be done are:
d4731b80 407
498d1b80
AC
408 -- First_Bit .. Last_Bit Component_Bit_Offset
409 -- old new old new
a9a5b8ac 410
498d1b80
AC
411 -- 0 .. 0 7 .. 7 0 7
412 -- 0 .. 1 6 .. 7 0 6
413 -- 0 .. 2 5 .. 7 0 5
414 -- 0 .. 7 0 .. 7 0 4
a9a5b8ac 415
498d1b80
AC
416 -- 1 .. 1 6 .. 6 1 6
417 -- 1 .. 4 3 .. 6 1 3
418 -- 4 .. 7 0 .. 3 4 0
a9a5b8ac 419
498d1b80
AC
420 -- The rule is that the first bit is is obtained by
421 -- subtracting the old ending bit from storage_unit - 1.
a9a5b8ac 422
498d1b80
AC
423 Set_Component_Bit_Offset
424 (Comp,
425 (Storage_Unit_Offset * System_Storage_Unit) +
426 (System_Storage_Unit - 1) -
427 (Start_Bit + CSZ - 1));
a9a5b8ac 428
498d1b80
AC
429 Set_Normalized_First_Bit
430 (Comp,
431 Component_Bit_Offset (Comp) mod
432 System_Storage_Unit);
433 end if;
434 end;
435 end if;
436
437 Next_Component_Or_Discriminant (Comp);
438 end loop;
439
440 -- For Ada 2005, we do machine scalar processing, as fully described In
441 -- AI-133. This involves gathering all components which start at the
442 -- same byte offset and processing them together. Same approach is still
443 -- valid in later versions including Ada 2012.
444
445 else
446 declare
447 Max_Machine_Scalar_Size : constant Uint :=
448 UI_From_Int
449 (Standard_Long_Long_Integer_Size);
8a95f4e8 450 -- We use this as the maximum machine scalar size
a9a5b8ac 451
498d1b80
AC
452 Num_CC : Natural;
453 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
a9a5b8ac 454
498d1b80
AC
455 begin
456 -- This first loop through components does two things. First it
457 -- deals with the case of components with component clauses whose
458 -- length is greater than the maximum machine scalar size (either
459 -- accepting them or rejecting as needed). Second, it counts the
460 -- number of components with component clauses whose length does
461 -- not exceed this maximum for later processing.
8a95f4e8 462
498d1b80
AC
463 Num_CC := 0;
464 Comp := First_Component_Or_Discriminant (R);
465 while Present (Comp) loop
466 CC := Component_Clause (Comp);
8a95f4e8 467
498d1b80
AC
468 if Present (CC) then
469 declare
dbfeb4fa
RD
470 Fbit : constant Uint := Static_Integer (First_Bit (CC));
471 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
8a95f4e8 472
498d1b80 473 begin
038140ed 474 -- Case of component with last bit >= max machine scalar
8a95f4e8 475
038140ed 476 if Lbit >= Max_Machine_Scalar_Size then
8a95f4e8 477
038140ed
AC
478 -- This is allowed only if first bit is zero, and
479 -- last bit + 1 is a multiple of storage unit size.
8a95f4e8 480
038140ed 481 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
8a95f4e8 482
038140ed 483 -- This is the case to give a warning if enabled
8a95f4e8 484
038140ed
AC
485 if Warn_On_Reverse_Bit_Order then
486 Error_Msg_N
ee10c0ec 487 ("info: multi-byte field specified with "
4afcf3a5 488 & "non-standard Bit_Order?V?", CC);
038140ed
AC
489
490 if Bytes_Big_Endian then
491 Error_Msg_N
492 ("\bytes are not reversed "
dbfeb4fa 493 & "(component is big-endian)?V?", CC);
038140ed
AC
494 else
495 Error_Msg_N
496 ("\bytes are not reversed "
dbfeb4fa 497 & "(component is little-endian)?V?", CC);
038140ed
AC
498 end if;
499 end if;
8a95f4e8 500
8777c5a6 501 -- Give error message for RM 13.5.1(10) violation
8a95f4e8 502
038140ed
AC
503 else
504 Error_Msg_FE
505 ("machine scalar rules not followed for&",
506 First_Bit (CC), Comp);
8a95f4e8 507
11352209 508 Error_Msg_Uint_1 := Lbit + 1;
038140ed
AC
509 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
510 Error_Msg_F
11352209 511 ("\last bit + 1 (^) exceeds maximum machine "
038140ed
AC
512 & "scalar size (^)",
513 First_Bit (CC));
8a95f4e8 514
038140ed
AC
515 if (Lbit + 1) mod SSU /= 0 then
516 Error_Msg_Uint_1 := SSU;
517 Error_Msg_F
518 ("\and is not a multiple of Storage_Unit (^) "
11352209 519 & "(RM 13.5.1(10))",
038140ed 520 First_Bit (CC));
498d1b80 521
498d1b80 522 else
038140ed
AC
523 Error_Msg_Uint_1 := Fbit;
524 Error_Msg_F
525 ("\and first bit (^) is non-zero "
a4640a39 526 & "(RM 13.4.1(10))",
038140ed 527 First_Bit (CC));
8a95f4e8 528 end if;
498d1b80 529 end if;
a9a5b8ac 530
038140ed
AC
531 -- OK case of machine scalar related component clause,
532 -- For now, just count them.
a9a5b8ac 533
498d1b80
AC
534 else
535 Num_CC := Num_CC + 1;
536 end if;
537 end;
538 end if;
a9a5b8ac 539
498d1b80
AC
540 Next_Component_Or_Discriminant (Comp);
541 end loop;
a9a5b8ac 542
498d1b80
AC
543 -- We need to sort the component clauses on the basis of the
544 -- Position values in the clause, so we can group clauses with
9db78a42 545 -- the same Position together to determine the relevant machine
498d1b80 546 -- scalar size.
a9a5b8ac 547
498d1b80
AC
548 Sort_CC : declare
549 Comps : array (0 .. Num_CC) of Entity_Id;
550 -- Array to collect component and discriminant entities. The
551 -- data starts at index 1, the 0'th entry is for the sort
552 -- routine.
a9a5b8ac 553
498d1b80
AC
554 function CP_Lt (Op1, Op2 : Natural) return Boolean;
555 -- Compare routine for Sort
a9a5b8ac 556
498d1b80
AC
557 procedure CP_Move (From : Natural; To : Natural);
558 -- Move routine for Sort
a9a5b8ac 559
498d1b80 560 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
a9a5b8ac 561
498d1b80
AC
562 Start : Natural;
563 Stop : Natural;
564 -- Start and stop positions in the component list of the set of
565 -- components with the same starting position (that constitute
566 -- components in a single machine scalar).
a9a5b8ac 567
498d1b80
AC
568 MaxL : Uint;
569 -- Maximum last bit value of any component in this set
a9a5b8ac 570
498d1b80
AC
571 MSS : Uint;
572 -- Corresponding machine scalar size
8a95f4e8 573
498d1b80
AC
574 -----------
575 -- CP_Lt --
576 -----------
8a95f4e8 577
498d1b80
AC
578 function CP_Lt (Op1, Op2 : Natural) return Boolean is
579 begin
580 return Position (Component_Clause (Comps (Op1))) <
581 Position (Component_Clause (Comps (Op2)));
582 end CP_Lt;
8a95f4e8 583
498d1b80
AC
584 -------------
585 -- CP_Move --
586 -------------
8a95f4e8 587
498d1b80
AC
588 procedure CP_Move (From : Natural; To : Natural) is
589 begin
590 Comps (To) := Comps (From);
591 end CP_Move;
8a95f4e8 592
9db78a42 593 -- Start of processing for Sort_CC
a9a5b8ac 594
498d1b80 595 begin
038140ed 596 -- Collect the machine scalar relevant component clauses
a9a5b8ac 597
498d1b80
AC
598 Num_CC := 0;
599 Comp := First_Component_Or_Discriminant (R);
600 while Present (Comp) loop
038140ed
AC
601 declare
602 CC : constant Node_Id := Component_Clause (Comp);
603
604 begin
605 -- Collect only component clauses whose last bit is less
606 -- than machine scalar size. Any component clause whose
607 -- last bit exceeds this value does not take part in
608 -- machine scalar layout considerations. The test for
609 -- Error_Posted makes sure we exclude component clauses
610 -- for which we already posted an error.
611
612 if Present (CC)
613 and then not Error_Posted (Last_Bit (CC))
614 and then Static_Integer (Last_Bit (CC)) <
a01b9df6 615 Max_Machine_Scalar_Size
038140ed
AC
616 then
617 Num_CC := Num_CC + 1;
618 Comps (Num_CC) := Comp;
619 end if;
620 end;
a9a5b8ac 621
498d1b80
AC
622 Next_Component_Or_Discriminant (Comp);
623 end loop;
8a95f4e8 624
498d1b80 625 -- Sort by ascending position number
8a95f4e8 626
498d1b80 627 Sorting.Sort (Num_CC);
8a95f4e8 628
498d1b80
AC
629 -- We now have all the components whose size does not exceed
630 -- the max machine scalar value, sorted by starting position.
631 -- In this loop we gather groups of clauses starting at the
632 -- same position, to process them in accordance with AI-133.
8a95f4e8 633
498d1b80
AC
634 Stop := 0;
635 while Stop < Num_CC loop
636 Start := Stop + 1;
637 Stop := Start;
638 MaxL :=
639 Static_Integer
640 (Last_Bit (Component_Clause (Comps (Start))));
8a95f4e8 641 while Stop < Num_CC loop
498d1b80
AC
642 if Static_Integer
643 (Position (Component_Clause (Comps (Stop + 1)))) =
644 Static_Integer
645 (Position (Component_Clause (Comps (Stop))))
646 then
647 Stop := Stop + 1;
648 MaxL :=
649 UI_Max
650 (MaxL,
651 Static_Integer
652 (Last_Bit
653 (Component_Clause (Comps (Stop)))));
654 else
655 exit;
656 end if;
657 end loop;
8a95f4e8 658
498d1b80
AC
659 -- Now we have a group of component clauses from Start to
660 -- Stop whose positions are identical, and MaxL is the
661 -- maximum last bit value of any of these components.
662
663 -- We need to determine the corresponding machine scalar
664 -- size. This loop assumes that machine scalar sizes are
665 -- even, and that each possible machine scalar has twice
666 -- as many bits as the next smaller one.
667
668 MSS := Max_Machine_Scalar_Size;
669 while MSS mod 2 = 0
670 and then (MSS / 2) >= SSU
671 and then (MSS / 2) > MaxL
672 loop
673 MSS := MSS / 2;
674 end loop;
8a95f4e8 675
498d1b80
AC
676 -- Here is where we fix up the Component_Bit_Offset value
677 -- to account for the reverse bit order. Some examples of
678 -- what needs to be done for the case of a machine scalar
679 -- size of 8 are:
8a95f4e8 680
498d1b80
AC
681 -- First_Bit .. Last_Bit Component_Bit_Offset
682 -- old new old new
8a95f4e8 683
498d1b80
AC
684 -- 0 .. 0 7 .. 7 0 7
685 -- 0 .. 1 6 .. 7 0 6
686 -- 0 .. 2 5 .. 7 0 5
687 -- 0 .. 7 0 .. 7 0 4
8a95f4e8 688
498d1b80
AC
689 -- 1 .. 1 6 .. 6 1 6
690 -- 1 .. 4 3 .. 6 1 3
691 -- 4 .. 7 0 .. 3 4 0
8a95f4e8 692
498d1b80
AC
693 -- The rule is that the first bit is obtained by subtracting
694 -- the old ending bit from machine scalar size - 1.
8a95f4e8 695
498d1b80
AC
696 for C in Start .. Stop loop
697 declare
698 Comp : constant Entity_Id := Comps (C);
616547fa
AC
699 CC : constant Node_Id := Component_Clause (Comp);
700
701 LB : constant Uint := Static_Integer (Last_Bit (CC));
498d1b80
AC
702 NFB : constant Uint := MSS - Uint_1 - LB;
703 NLB : constant Uint := NFB + Esize (Comp) - 1;
616547fa 704 Pos : constant Uint := Static_Integer (Position (CC));
8a95f4e8 705
498d1b80
AC
706 begin
707 if Warn_On_Reverse_Bit_Order then
708 Error_Msg_Uint_1 := MSS;
709 Error_Msg_N
710 ("info: reverse bit order in machine " &
dbfeb4fa 711 "scalar of length^?V?", First_Bit (CC));
498d1b80
AC
712 Error_Msg_Uint_1 := NFB;
713 Error_Msg_Uint_2 := NLB;
714
715 if Bytes_Big_Endian then
716 Error_Msg_NE
ee10c0ec
RD
717 ("\big-endian range for component "
718 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
498d1b80
AC
719 else
720 Error_Msg_NE
ee10c0ec
RD
721 ("\little-endian range for component"
722 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
8a95f4e8 723 end if;
498d1b80 724 end if;
8a95f4e8 725
498d1b80
AC
726 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
727 Set_Normalized_First_Bit (Comp, NFB mod SSU);
728 end;
8a95f4e8 729 end loop;
498d1b80
AC
730 end loop;
731 end Sort_CC;
732 end;
733 end if;
a9a5b8ac
RD
734 end Adjust_Record_For_Reverse_Bit_Order;
735
76af4137
AC
736 -------------------------------------
737 -- Alignment_Check_For_Size_Change --
738 -------------------------------------
996ae0b0 739
76af4137 740 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
996ae0b0
RK
741 begin
742 -- If the alignment is known, and not set by a rep clause, and is
743 -- inconsistent with the size being set, then reset it to unknown,
744 -- we assume in this case that the size overrides the inherited
745 -- alignment, and that the alignment must be recomputed.
746
747 if Known_Alignment (Typ)
748 and then not Has_Alignment_Clause (Typ)
76af4137 749 and then Size mod (Alignment (Typ) * SSU) /= 0
996ae0b0
RK
750 then
751 Init_Alignment (Typ);
752 end if;
76af4137 753 end Alignment_Check_For_Size_Change;
996ae0b0 754
8a0320ad
AC
755 -------------------------------------
756 -- Analyze_Aspects_At_Freeze_Point --
757 -------------------------------------
758
759 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
8a0320ad
AC
760 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
761 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
762 -- the aspect specification node ASN.
763
15e934bf
AC
764 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
765 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
766 -- a derived type can inherit aspects from its parent which have been
767 -- specified at the time of the derivation using an aspect, as in:
768 --
769 -- type A is range 1 .. 10
770 -- with Size => Not_Defined_Yet;
771 -- ..
772 -- type B is new A;
773 -- ..
774 -- Not_Defined_Yet : constant := 64;
775 --
776 -- In this example, the Size of A is considered to be specified prior
777 -- to the derivation, and thus inherited, even though the value is not
778 -- known at the time of derivation. To deal with this, we use two entity
779 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
780 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
781 -- the derived type (B here). If this flag is set when the derived type
782 -- is frozen, then this procedure is called to ensure proper inheritance
860917b6 783 -- of all delayed aspects from the parent type. The derived type is E,
15e934bf
AC
784 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
785 -- aspect specification node in the Rep_Item chain for the parent type.
786
8a0320ad
AC
787 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
788 -- Given an aspect specification node ASN whose expression is an
789 -- optional Boolean, this routines creates the corresponding pragma
790 -- at the freezing point.
791
792 ----------------------------------
793 -- Analyze_Aspect_Default_Value --
794 ----------------------------------
795
796 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
2e885a6f 797 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
8a0320ad
AC
798 Ent : constant Entity_Id := Entity (ASN);
799 Expr : constant Node_Id := Expression (ASN);
800 Id : constant Node_Id := Identifier (ASN);
801
802 begin
803 Error_Msg_Name_1 := Chars (Id);
804
805 if not Is_Type (Ent) then
806 Error_Msg_N ("aspect% can only apply to a type", Id);
807 return;
808
809 elsif not Is_First_Subtype (Ent) then
810 Error_Msg_N ("aspect% cannot apply to subtype", Id);
811 return;
812
813 elsif A_Id = Aspect_Default_Value
814 and then not Is_Scalar_Type (Ent)
815 then
816 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
817 return;
818
819 elsif A_Id = Aspect_Default_Component_Value then
820 if not Is_Array_Type (Ent) then
821 Error_Msg_N ("aspect% can only be applied to array type", Id);
822 return;
823
824 elsif not Is_Scalar_Type (Component_Type (Ent)) then
825 Error_Msg_N ("aspect% requires scalar components", Id);
826 return;
827 end if;
828 end if;
829
830 Set_Has_Default_Aspect (Base_Type (Ent));
831
832 if Is_Scalar_Type (Ent) then
7b55fea6 833 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
8a0320ad 834 else
688a9b51 835 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
8a0320ad
AC
836 end if;
837 end Analyze_Aspect_Default_Value;
838
15e934bf
AC
839 ---------------------------------
840 -- Inherit_Delayed_Rep_Aspects --
841 ---------------------------------
842
843 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
2e885a6f
AC
844 A_Id : constant Aspect_Id := Get_Aspect_Id (ASN);
845 P : constant Entity_Id := Entity (ASN);
15e934bf
AC
846 -- Entithy for parent type
847
848 N : Node_Id;
849 -- Item from Rep_Item chain
850
851 A : Aspect_Id;
852
853 begin
854 -- Loop through delayed aspects for the parent type
855
856 N := ASN;
857 while Present (N) loop
858 if Nkind (N) = N_Aspect_Specification then
859 exit when Entity (N) /= P;
860
861 if Is_Delayed_Aspect (N) then
862 A := Get_Aspect_Id (Chars (Identifier (N)));
863
864 -- Process delayed rep aspect. For Boolean attributes it is
865 -- not possible to cancel an attribute once set (the attempt
866 -- to use an aspect with xxx => False is an error) for a
867 -- derived type. So for those cases, we do not have to check
868 -- if a clause has been given for the derived type, since it
869 -- is harmless to set it again if it is already set.
870
871 case A is
872
873 -- Alignment
874
875 when Aspect_Alignment =>
876 if not Has_Alignment_Clause (E) then
877 Set_Alignment (E, Alignment (P));
878 end if;
879
880 -- Atomic
881
882 when Aspect_Atomic =>
883 if Is_Atomic (P) then
884 Set_Is_Atomic (E);
885 end if;
886
887 -- Atomic_Components
888
889 when Aspect_Atomic_Components =>
890 if Has_Atomic_Components (P) then
891 Set_Has_Atomic_Components (Base_Type (E));
892 end if;
893
894 -- Bit_Order
895
896 when Aspect_Bit_Order =>
897 if Is_Record_Type (E)
898 and then No (Get_Attribute_Definition_Clause
899 (E, Attribute_Bit_Order))
900 and then Reverse_Bit_Order (P)
901 then
902 Set_Reverse_Bit_Order (Base_Type (E));
903 end if;
904
905 -- Component_Size
906
907 when Aspect_Component_Size =>
908 if Is_Array_Type (E)
909 and then not Has_Component_Size_Clause (E)
910 then
911 Set_Component_Size
912 (Base_Type (E), Component_Size (P));
913 end if;
914
915 -- Machine_Radix
916
917 when Aspect_Machine_Radix =>
918 if Is_Decimal_Fixed_Point_Type (E)
919 and then not Has_Machine_Radix_Clause (E)
920 then
921 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
922 end if;
923
924 -- Object_Size (also Size which also sets Object_Size)
925
926 when Aspect_Object_Size | Aspect_Size =>
927 if not Has_Size_Clause (E)
928 and then
929 No (Get_Attribute_Definition_Clause
930 (E, Attribute_Object_Size))
931 then
932 Set_Esize (E, Esize (P));
933 end if;
934
935 -- Pack
936
937 when Aspect_Pack =>
938 if not Is_Packed (E) then
939 Set_Is_Packed (Base_Type (E));
940
941 if Is_Bit_Packed_Array (P) then
942 Set_Is_Bit_Packed_Array (Base_Type (E));
8ca597af
RD
943 Set_Packed_Array_Impl_Type
944 (E, Packed_Array_Impl_Type (P));
15e934bf
AC
945 end if;
946 end if;
947
948 -- Scalar_Storage_Order
949
950 when Aspect_Scalar_Storage_Order =>
951 if (Is_Record_Type (E) or else Is_Array_Type (E))
952 and then No (Get_Attribute_Definition_Clause
c1645ac8 953 (E, Attribute_Scalar_Storage_Order))
15e934bf
AC
954 and then Reverse_Storage_Order (P)
955 then
956 Set_Reverse_Storage_Order (Base_Type (E));
220d1fd9
AC
957
958 -- Clear default SSO indications, since the aspect
959 -- overrides the default.
960
961 Set_SSO_Set_Low_By_Default (Base_Type (E), False);
962 Set_SSO_Set_High_By_Default (Base_Type (E), False);
15e934bf
AC
963 end if;
964
965 -- Small
966
967 when Aspect_Small =>
968 if Is_Fixed_Point_Type (E)
969 and then not Has_Small_Clause (E)
970 then
971 Set_Small_Value (E, Small_Value (P));
972 end if;
973
974 -- Storage_Size
975
976 when Aspect_Storage_Size =>
977 if (Is_Access_Type (E) or else Is_Task_Type (E))
978 and then not Has_Storage_Size_Clause (E)
979 then
980 Set_Storage_Size_Variable
981 (Base_Type (E), Storage_Size_Variable (P));
982 end if;
983
984 -- Value_Size
985
986 when Aspect_Value_Size =>
987
988 -- Value_Size is never inherited, it is either set by
989 -- default, or it is explicitly set for the derived
990 -- type. So nothing to do here.
991
992 null;
993
994 -- Volatile
995
996 when Aspect_Volatile =>
997 if Is_Volatile (P) then
998 Set_Is_Volatile (E);
999 end if;
1000
f280dd8f
RD
1001 -- Volatile_Full_Access
1002
1003 when Aspect_Volatile_Full_Access =>
57abdadd
EB
1004 if Is_Volatile_Full_Access (P) then
1005 Set_Is_Volatile_Full_Access (E);
f280dd8f
RD
1006 end if;
1007
15e934bf
AC
1008 -- Volatile_Components
1009
1010 when Aspect_Volatile_Components =>
1011 if Has_Volatile_Components (P) then
1012 Set_Has_Volatile_Components (Base_Type (E));
1013 end if;
1014
1015 -- That should be all the Rep Aspects
1016
1017 when others =>
1018 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
1019 null;
1020
1021 end case;
1022 end if;
1023 end if;
1024
1025 N := Next_Rep_Item (N);
1026 end loop;
1027 end Inherit_Delayed_Rep_Aspects;
1028
8a0320ad
AC
1029 -------------------------------------
1030 -- Make_Pragma_From_Boolean_Aspect --
1031 -------------------------------------
1032
1033 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
1034 Ident : constant Node_Id := Identifier (ASN);
1035 A_Name : constant Name_Id := Chars (Ident);
1036 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
1037 Ent : constant Entity_Id := Entity (ASN);
1038 Expr : constant Node_Id := Expression (ASN);
1039 Loc : constant Source_Ptr := Sloc (ASN);
1040
8a0320ad
AC
1041 procedure Check_False_Aspect_For_Derived_Type;
1042 -- This procedure checks for the case of a false aspect for a derived
1043 -- type, which improperly tries to cancel an aspect inherited from
1044 -- the parent.
1045
1046 -----------------------------------------
1047 -- Check_False_Aspect_For_Derived_Type --
1048 -----------------------------------------
1049
1050 procedure Check_False_Aspect_For_Derived_Type is
1051 Par : Node_Id;
1052
1053 begin
1054 -- We are only checking derived types
1055
1056 if not Is_Derived_Type (E) then
1057 return;
1058 end if;
1059
1060 Par := Nearest_Ancestor (E);
1061
1062 case A_Id is
1063 when Aspect_Atomic | Aspect_Shared =>
1064 if not Is_Atomic (Par) then
1065 return;
1066 end if;
1067
1068 when Aspect_Atomic_Components =>
1069 if not Has_Atomic_Components (Par) then
1070 return;
1071 end if;
1072
1073 when Aspect_Discard_Names =>
1074 if not Discard_Names (Par) then
1075 return;
1076 end if;
1077
1078 when Aspect_Pack =>
1079 if not Is_Packed (Par) then
1080 return;
1081 end if;
1082
1083 when Aspect_Unchecked_Union =>
1084 if not Is_Unchecked_Union (Par) then
1085 return;
1086 end if;
1087
1088 when Aspect_Volatile =>
1089 if not Is_Volatile (Par) then
1090 return;
1091 end if;
1092
1093 when Aspect_Volatile_Components =>
1094 if not Has_Volatile_Components (Par) then
1095 return;
1096 end if;
1097
f280dd8f 1098 when Aspect_Volatile_Full_Access =>
57abdadd 1099 if not Is_Volatile_Full_Access (Par) then
f280dd8f
RD
1100 return;
1101 end if;
1102
8a0320ad
AC
1103 when others =>
1104 return;
1105 end case;
1106
1107 -- Fall through means we are canceling an inherited aspect
1108
1109 Error_Msg_Name_1 := A_Name;
15e934bf
AC
1110 Error_Msg_NE
1111 ("derived type& inherits aspect%, cannot cancel", Expr, E);
8a0320ad
AC
1112 end Check_False_Aspect_For_Derived_Type;
1113
2e885a6f
AC
1114 -- Local variables
1115
1116 Prag : Node_Id;
1117
8a0320ad
AC
1118 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1119
1120 begin
15e934bf
AC
1121 -- Note that we know Expr is present, because for a missing Expr
1122 -- argument, we knew it was True and did not need to delay the
1123 -- evaluation to the freeze point.
1124
8a0320ad
AC
1125 if Is_False (Static_Boolean (Expr)) then
1126 Check_False_Aspect_For_Derived_Type;
1127
1128 else
1129 Prag :=
1130 Make_Pragma (Loc,
2e885a6f
AC
1131 Pragma_Identifier =>
1132 Make_Identifier (Sloc (Ident), Chars (Ident)),
8a0320ad 1133 Pragma_Argument_Associations => New_List (
3860d469 1134 Make_Pragma_Argument_Association (Sloc (Ident),
2e885a6f 1135 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))));
8a0320ad
AC
1136
1137 Set_From_Aspect_Specification (Prag, True);
1138 Set_Corresponding_Aspect (Prag, ASN);
1139 Set_Aspect_Rep_Item (ASN, Prag);
1140 Set_Is_Delayed_Aspect (Prag);
1141 Set_Parent (Prag, ASN);
1142 end if;
8a0320ad
AC
1143 end Make_Pragma_From_Boolean_Aspect;
1144
2e885a6f
AC
1145 -- Local variables
1146
1147 A_Id : Aspect_Id;
1148 ASN : Node_Id;
1149 Ritem : Node_Id;
1150
8a0320ad
AC
1151 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1152
1153 begin
2791be24 1154 -- Must be visible in current scope
8a0320ad 1155
5eeeed5e 1156 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
8a0320ad
AC
1157 return;
1158 end if;
1159
1160 -- Look for aspect specification entries for this entity
1161
1162 ASN := First_Rep_Item (E);
8a0320ad 1163 while Present (ASN) loop
15e934bf
AC
1164 if Nkind (ASN) = N_Aspect_Specification then
1165 exit when Entity (ASN) /= E;
8a0320ad 1166
15e934bf
AC
1167 if Is_Delayed_Aspect (ASN) then
1168 A_Id := Get_Aspect_Id (ASN);
1169
1170 case A_Id is
21791d97 1171
15e934bf 1172 -- For aspects whose expression is an optional Boolean, make
3e1862b1 1173 -- the corresponding pragma at the freeze point.
8a0320ad 1174
3e1862b1
ES
1175 when Boolean_Aspects |
1176 Library_Unit_Aspects =>
2e885a6f
AC
1177
1178 -- Aspects Export and Import require special handling.
1179 -- Both are by definition Boolean and may benefit from
1180 -- forward references, however their expressions are
1181 -- treated as static. In addition, the syntax of their
1182 -- corresponding pragmas requires extra "pieces" which
1183 -- may also contain forward references. To account for
1184 -- all of this, the corresponding pragma is created by
1185 -- Analyze_Aspect_Export_Import, but is not analyzed as
1186 -- the complete analysis must happen now.
1187
1188 if A_Id = Aspect_Export or else A_Id = Aspect_Import then
1189 null;
1190
1191 -- Otherwise create a corresponding pragma
1192
1193 else
1194 Make_Pragma_From_Boolean_Aspect (ASN);
1195 end if;
8a0320ad 1196
15e934bf
AC
1197 -- Special handling for aspects that don't correspond to
1198 -- pragmas/attributes.
8a0320ad 1199
3e1862b1
ES
1200 when Aspect_Default_Value |
1201 Aspect_Default_Component_Value =>
731261c3
AC
1202
1203 -- Do not inherit aspect for anonymous base type of a
1204 -- scalar or array type, because they apply to the first
1205 -- subtype of the type, and will be processed when that
1206 -- first subtype is frozen.
1207
1208 if Is_Derived_Type (E)
1209 and then not Comes_From_Source (E)
1210 and then E /= First_Subtype (E)
1211 then
1212 null;
1213 else
1214 Analyze_Aspect_Default_Value (ASN);
1215 end if;
8a0320ad 1216
15e934bf
AC
1217 -- Ditto for iterator aspects, because the corresponding
1218 -- attributes may not have been analyzed yet.
7640ef8a 1219
3e1862b1
ES
1220 when Aspect_Constant_Indexing |
1221 Aspect_Variable_Indexing |
1222 Aspect_Default_Iterator |
1223 Aspect_Iterator_Element =>
1224 Analyze (Expression (ASN));
7640ef8a 1225
3e1862b1
ES
1226 if Etype (Expression (ASN)) = Any_Type then
1227 Error_Msg_NE
1228 ("\aspect must be fully defined before & is frozen",
1229 ASN, E);
1230 end if;
dd2bf554 1231
3e1862b1
ES
1232 when Aspect_Iterable =>
1233 Validate_Iterable_Aspect (E, ASN);
1234
1235 when others =>
1236 null;
15e934bf 1237 end case;
8a0320ad 1238
15e934bf 1239 Ritem := Aspect_Rep_Item (ASN);
8a0320ad 1240
15e934bf
AC
1241 if Present (Ritem) then
1242 Analyze (Ritem);
1243 end if;
8a0320ad
AC
1244 end if;
1245 end if;
1246
1247 Next_Rep_Item (ASN);
1248 end loop;
15e934bf
AC
1249
1250 -- This is where we inherit delayed rep aspects from our parent. Note
1251 -- that if we fell out of the above loop with ASN non-empty, it means
1252 -- we hit an aspect for an entity other than E, and it must be the
1253 -- type from which we were derived.
1254
1255 if May_Inherit_Delayed_Rep_Aspects (E) then
1256 Inherit_Delayed_Rep_Aspects (ASN);
1257 end if;
8a0320ad
AC
1258 end Analyze_Aspects_At_Freeze_Point;
1259
0f1a6a0b
AC
1260 -----------------------------------
1261 -- Analyze_Aspect_Specifications --
1262 -----------------------------------
1263
eaba57fb 1264 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
4e6768ab 1265 procedure Decorate (Asp : Node_Id; Prag : Node_Id);
c8593453 1266 -- Establish linkages between an aspect and its corresponding pragma
5afe5d2d 1267
21d7ef70
AC
1268 procedure Insert_Pragma
1269 (Prag : Node_Id;
1270 Is_Instance : Boolean := False);
877a5a12
AC
1271 -- Subsidiary to the analysis of aspects
1272 -- Abstract_State
877a5a12
AC
1273 -- Attach_Handler
1274 -- Contract_Cases
1275 -- Depends
21d7ef70 1276 -- Ghost
877a5a12 1277 -- Global
21d7ef70
AC
1278 -- Initial_Condition
1279 -- Initializes
877a5a12
AC
1280 -- Post
1281 -- Pre
1282 -- Refined_Depends
1283 -- Refined_Global
21d7ef70 1284 -- Refined_State
877a5a12
AC
1285 -- SPARK_Mode
1286 -- Warnings
4e6768ab 1287 -- Insert pragma Prag such that it mimics the placement of a source
21d7ef70
AC
1288 -- pragma of the same kind. Flag Is_Generic should be set when the
1289 -- context denotes a generic instance.
4e6768ab
AC
1290
1291 --------------
1292 -- Decorate --
1293 --------------
1294
1295 procedure Decorate (Asp : Node_Id; Prag : Node_Id) is
5afe5d2d 1296 begin
c8593453 1297 Set_Aspect_Rep_Item (Asp, Prag);
5afe5d2d
HK
1298 Set_Corresponding_Aspect (Prag, Asp);
1299 Set_From_Aspect_Specification (Prag);
5afe5d2d 1300 Set_Parent (Prag, Asp);
4e6768ab 1301 end Decorate;
dba44dbe 1302
4e6768ab
AC
1303 -------------------
1304 -- Insert_Pragma --
1305 -------------------
d6095153 1306
21d7ef70
AC
1307 procedure Insert_Pragma
1308 (Prag : Node_Id;
1309 Is_Instance : Boolean := False)
1310 is
8a0183fd
HK
1311 Aux : Node_Id;
1312 Decl : Node_Id;
1313 Decls : List_Id;
1314 Def : Node_Id;
1315 Inserted : Boolean := False;
d6095153
AC
1316
1317 begin
8a0183fd
HK
1318 -- When the aspect appears on an entry, package, protected unit,
1319 -- subprogram, or task unit body, insert the generated pragma at the
1320 -- top of the body declarations to emulate the behavior of a source
1321 -- pragma.
877a5a12
AC
1322
1323 -- package body Pack with Aspect is
1324
1325 -- package body Pack is
1326 -- pragma Prag;
1327
8a0183fd
HK
1328 if Nkind_In (N, N_Entry_Body,
1329 N_Package_Body,
877a5a12
AC
1330 N_Protected_Body,
1331 N_Subprogram_Body,
1332 N_Task_Body)
1333 then
1334 Decls := Declarations (N);
1335
1336 if No (Decls) then
1337 Decls := New_List;
1338 Set_Declarations (N, Decls);
1339 end if;
4e6768ab 1340
8a0183fd 1341 Prepend_To (Decls, Prag);
877a5a12
AC
1342
1343 -- When the aspect is associated with a [generic] package declaration
1344 -- insert the generated pragma at the top of the visible declarations
1345 -- to emulate the behavior of a source pragma.
1346
1347 -- package Pack with Aspect is
1348
1349 -- package Pack is
1350 -- pragma Prag;
1351
1352 elsif Nkind_In (N, N_Generic_Package_Declaration,
1353 N_Package_Declaration)
1354 then
1355 Decls := Visible_Declarations (Specification (N));
1356
1357 if No (Decls) then
1358 Decls := New_List;
1359 Set_Visible_Declarations (Specification (N), Decls);
1360 end if;
1361
21d7ef70
AC
1362 -- The visible declarations of a generic instance have the
1363 -- following structure:
1364
1365 -- <renamings of generic formals>
1366 -- <renamings of internally-generated spec and body>
1367 -- <first source declaration>
1368
1369 -- Insert the pragma before the first source declaration by
8a0183fd
HK
1370 -- skipping the instance "header" to ensure proper visibility of
1371 -- all formals.
21d7ef70
AC
1372
1373 if Is_Instance then
1374 Decl := First (Decls);
8a0183fd
HK
1375 while Present (Decl) loop
1376 if Comes_From_Source (Decl) then
1377 Insert_Before (Decl, Prag);
1378 Inserted := True;
1379 exit;
1380 else
1381 Next (Decl);
1382 end if;
21d7ef70
AC
1383 end loop;
1384
8a0183fd 1385 -- The pragma is placed after the instance "header"
21d7ef70 1386
8a0183fd 1387 if not Inserted then
21d7ef70
AC
1388 Append_To (Decls, Prag);
1389 end if;
1390
1391 -- Otherwise this is not a generic instance
1392
1393 else
1394 Prepend_To (Decls, Prag);
1395 end if;
877a5a12
AC
1396
1397 -- When the aspect is associated with a protected unit declaration,
1398 -- insert the generated pragma at the top of the visible declarations
1399 -- the emulate the behavior of a source pragma.
1400
1401 -- protected [type] Prot with Aspect is
1402
1403 -- protected [type] Prot is
1404 -- pragma Prag;
1405
1406 elsif Nkind (N) = N_Protected_Type_Declaration then
75b87c16
AC
1407 Def := Protected_Definition (N);
1408
1409 if No (Def) then
1410 Def :=
1411 Make_Protected_Definition (Sloc (N),
1412 Visible_Declarations => New_List,
1413 End_Label => Empty);
1414
1415 Set_Protected_Definition (N, Def);
1416 end if;
1417
1418 Decls := Visible_Declarations (Def);
877a5a12
AC
1419
1420 if No (Decls) then
1421 Decls := New_List;
75b87c16 1422 Set_Visible_Declarations (Def, Decls);
877a5a12
AC
1423 end if;
1424
1425 Prepend_To (Decls, Prag);
1426
75b87c16
AC
1427 -- When the aspect is associated with a task unit declaration, insert
1428 -- insert the generated pragma at the top of the visible declarations
1429 -- the emulate the behavior of a source pragma.
877a5a12
AC
1430
1431 -- task [type] Prot with Aspect is
1432
1433 -- task [type] Prot is
1434 -- pragma Prag;
1435
75b87c16
AC
1436 elsif Nkind (N) = N_Task_Type_Declaration then
1437 Def := Task_Definition (N);
1438
1439 if No (Def) then
1440 Def :=
1441 Make_Task_Definition (Sloc (N),
1442 Visible_Declarations => New_List,
1443 End_Label => Empty);
1444
1445 Set_Task_Definition (N, Def);
1446 end if;
1447
1448 Decls := Visible_Declarations (Def);
877a5a12
AC
1449
1450 if No (Decls) then
1451 Decls := New_List;
75b87c16 1452 Set_Visible_Declarations (Def, Decls);
2fc07285 1453 end if;
d6095153 1454
877a5a12
AC
1455 Prepend_To (Decls, Prag);
1456
c9d70ab1
AC
1457 -- When the context is a library unit, the pragma is added to the
1458 -- Pragmas_After list.
1459
1460 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1461 Aux := Aux_Decls_Node (Parent (N));
1462
1463 if No (Pragmas_After (Aux)) then
1464 Set_Pragmas_After (Aux, New_List);
1465 end if;
1466
1467 Prepend (Prag, Pragmas_After (Aux));
1468
877a5a12 1469 -- Default, the pragma is inserted after the context
d6095153
AC
1470
1471 else
1472 Insert_After (N, Prag);
d6095153 1473 end if;
4e6768ab 1474 end Insert_Pragma;
d6095153
AC
1475
1476 -- Local variables
1477
0f1a6a0b 1478 Aspect : Node_Id;
c159409f 1479 Aitem : Node_Id;
0f1a6a0b 1480 Ent : Node_Id;
0f1a6a0b 1481
eaba57fb
RD
1482 L : constant List_Id := Aspect_Specifications (N);
1483
0f1a6a0b 1484 Ins_Node : Node_Id := N;
b98e2969
AC
1485 -- Insert pragmas/attribute definition clause after this node when no
1486 -- delayed analysis is required.
c159409f 1487
2e885a6f 1488 -- Start of processing for Analyze_Aspect_Specifications
dba44dbe 1489
2e885a6f 1490 begin
c159409f 1491 -- The general processing involves building an attribute definition
b98e2969
AC
1492 -- clause or a pragma node that corresponds to the aspect. Then in order
1493 -- to delay the evaluation of this aspect to the freeze point, we attach
1494 -- the corresponding pragma/attribute definition clause to the aspect
1495 -- specification node, which is then placed in the Rep Item chain. In
1496 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1497 -- and we evaluate the rep item at the freeze point. When the aspect
1498 -- doesn't have a corresponding pragma/attribute definition clause, then
1499 -- its analysis is simply delayed at the freeze point.
1500
1501 -- Some special cases don't require delay analysis, thus the aspect is
1502 -- analyzed right now.
1503
aab45d22 1504 -- Note that there is a special handling for Pre, Post, Test_Case,
541fb4d9 1505 -- Contract_Cases aspects. In these cases, we do not have to worry
aab45d22
AC
1506 -- about delay issues, since the pragmas themselves deal with delay
1507 -- of visibility for the expression analysis. Thus, we just insert
1508 -- the pragma after the node N.
0f1a6a0b 1509
eaba57fb
RD
1510 pragma Assert (Present (L));
1511
308e6f3a 1512 -- Loop through aspects
2d4e0553 1513
0f1a6a0b 1514 Aspect := First (L);
eaba57fb 1515 Aspect_Loop : while Present (Aspect) loop
9d5598bf 1516 Analyze_One_Aspect : declare
811ef5ba 1517 Expr : constant Node_Id := Expression (Aspect);
b98e2969
AC
1518 Id : constant Node_Id := Identifier (Aspect);
1519 Loc : constant Source_Ptr := Sloc (Aspect);
811ef5ba
RD
1520 Nam : constant Name_Id := Chars (Id);
1521 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
0f1a6a0b
AC
1522 Anod : Node_Id;
1523
15e934bf 1524 Delay_Required : Boolean;
b98e2969
AC
1525 -- Set False if delay is not required
1526
95160516
AC
1527 Eloc : Source_Ptr := No_Location;
1528 -- Source location of expression, modified when we split PPC's. It
1529 -- is set below when Expr is present.
beacce02 1530
2e885a6f
AC
1531 procedure Analyze_Aspect_Convention;
1532 -- Perform analysis of aspect Convention
1533
1534 procedure Analyze_Aspect_Export_Import;
1535 -- Perform analysis of aspects Export or Import
1536
1537 procedure Analyze_Aspect_External_Link_Name;
1538 -- Perform analysis of aspects External_Name or Link_Name
eaba57fb 1539
b98e2969 1540 procedure Analyze_Aspect_Implicit_Dereference;
3b1d4d82 1541 -- Perform analysis of the Implicit_Dereference aspects
9d5598bf
AC
1542
1543 procedure Make_Aitem_Pragma
1544 (Pragma_Argument_Associations : List_Id;
1545 Pragma_Name : Name_Id);
1546 -- This is a wrapper for Make_Pragma used for converting aspects
1547 -- to pragmas. It takes care of Sloc (set from Loc) and building
1548 -- the pragma identifier from the given name. In addition the
1549 -- flags Class_Present and Split_PPC are set from the aspect
1550 -- node, as well as Is_Ignored. This routine also sets the
1551 -- From_Aspect_Specification in the resulting pragma node to
1552 -- True, and sets Corresponding_Aspect to point to the aspect.
1553 -- The resulting pragma is assigned to Aitem.
eaba57fb 1554
2e885a6f
AC
1555 -------------------------------
1556 -- Analyze_Aspect_Convention --
1557 -------------------------------
1558
1559 procedure Analyze_Aspect_Convention is
1560 Conv : Node_Id;
1561 Dummy_1 : Node_Id;
1562 Dummy_2 : Node_Id;
1563 Dummy_3 : Node_Id;
1564 Expo : Node_Id;
1565 Imp : Node_Id;
b98e2969 1566
eaba57fb 1567 begin
2e885a6f
AC
1568 -- Obtain all interfacing aspects that apply to the related
1569 -- entity.
1570
1571 Get_Interfacing_Aspects
1572 (Iface_Asp => Aspect,
1573 Conv_Asp => Dummy_1,
1574 EN_Asp => Dummy_2,
1575 Expo_Asp => Expo,
1576 Imp_Asp => Imp,
1577 LN_Asp => Dummy_3,
1578 Do_Checks => True);
1579
1580 -- The related entity is subject to aspect Export or Import.
1581 -- Do not process Convention now because it must be analysed
1582 -- as part of Export or Import.
1583
1584 if Present (Expo) or else Present (Imp) then
1585 return;
eaba57fb 1586
2e885a6f 1587 -- Otherwise Convention appears by itself
eaba57fb 1588
2e885a6f
AC
1589 else
1590 -- The aspect specifies a particular convention
1591
1592 if Present (Expr) then
1593 Conv := New_Copy_Tree (Expr);
1594
1595 -- Otherwise assume convention Ada
1596
1597 else
1598 Conv := Make_Identifier (Loc, Name_Ada);
1599 end if;
1600
1601 -- Generate:
1602 -- pragma Convention (<Conv>, <E>);
1603
1604 Make_Aitem_Pragma
1605 (Pragma_Name => Name_Convention,
1606 Pragma_Argument_Associations => New_List (
1607 Make_Pragma_Argument_Association (Loc,
1608 Expression => Conv),
1609 Make_Pragma_Argument_Association (Loc,
1610 Expression => New_Occurrence_Of (E, Loc))));
1611
1612 Decorate (Aspect, Aitem);
1613 Insert_Pragma (Aitem);
1614 end if;
1615 end Analyze_Aspect_Convention;
1616
1617 ----------------------------------
1618 -- Analyze_Aspect_Export_Import --
1619 ----------------------------------
eaba57fb 1620
2e885a6f
AC
1621 procedure Analyze_Aspect_Export_Import is
1622 Dummy_1 : Node_Id;
1623 Dummy_2 : Node_Id;
1624 Dummy_3 : Node_Id;
1625 Expo : Node_Id;
1626 Imp : Node_Id;
1627
1628 begin
1629 -- Obtain all interfacing aspects that apply to the related
1630 -- entity.
1631
1632 Get_Interfacing_Aspects
1633 (Iface_Asp => Aspect,
1634 Conv_Asp => Dummy_1,
1635 EN_Asp => Dummy_2,
1636 Expo_Asp => Expo,
1637 Imp_Asp => Imp,
1638 LN_Asp => Dummy_3,
1639 Do_Checks => True);
1640
1641 -- The related entity cannot be subject to both aspects Export
1642 -- and Import.
1643
1644 if Present (Expo) and then Present (Imp) then
1645 Error_Msg_N
1646 ("incompatible interfacing aspects given for &", E);
1647 Error_Msg_Sloc := Sloc (Expo);
1648 Error_Msg_N ("\aspect `Export` #", E);
1649 Error_Msg_Sloc := Sloc (Imp);
1650 Error_Msg_N ("\aspect `Import` #", E);
1651 end if;
1652
1653 -- A variable is most likely modified from the outside. Take
1654 -- Take the optimistic approach to avoid spurious errors.
1655
1656 if Ekind (E) = E_Variable then
1657 Set_Never_Set_In_Source (E, False);
1658 end if;
1659
1660 -- Resolve the expression of an Import or Export here, and
1661 -- require it to be of type Boolean and static. This is not
1662 -- quite right, because in general this should be delayed,
1663 -- but that seems tricky for these, because normally Boolean
1664 -- aspects are replaced with pragmas at the freeze point in
1665 -- Make_Pragma_From_Boolean_Aspect.
1666
1667 if not Present (Expr)
1668 or else Is_True (Static_Boolean (Expr))
1669 then
1670 if A_Id = Aspect_Import then
1671 Set_Has_Completion (E);
1672 Set_Is_Imported (E);
1673
1674 -- An imported object cannot be explicitly initialized
1675
1676 if Nkind (N) = N_Object_Declaration
1677 and then Present (Expression (N))
1678 then
1679 Error_Msg_N
1680 ("imported entities cannot be initialized "
1681 & "(RM B.1(24))", Expression (N));
1682 end if;
1683
1684 else
1685 pragma Assert (A_Id = Aspect_Export);
1686 Set_Is_Exported (E);
1687 end if;
1688
1689 -- Create the proper form of pragma Export or Import taking
1690 -- into account Conversion, External_Name, and Link_Name.
1691
1692 Aitem := Build_Export_Import_Pragma (Aspect, E);
c7518e6f
AC
1693
1694 -- Otherwise the expression is either False or erroneous. There
1695 -- is no corresponding pragma.
1696
1697 else
1698 Aitem := Empty;
2e885a6f
AC
1699 end if;
1700 end Analyze_Aspect_Export_Import;
1701
1702 ---------------------------------------
1703 -- Analyze_Aspect_External_Link_Name --
1704 ---------------------------------------
1705
1706 procedure Analyze_Aspect_External_Link_Name is
1707 Dummy_1 : Node_Id;
1708 Dummy_2 : Node_Id;
1709 Dummy_3 : Node_Id;
1710 Expo : Node_Id;
1711 Imp : Node_Id;
1712
1713 begin
1714 -- Obtain all interfacing aspects that apply to the related
1715 -- entity.
1716
1717 Get_Interfacing_Aspects
1718 (Iface_Asp => Aspect,
1719 Conv_Asp => Dummy_1,
1720 EN_Asp => Dummy_2,
1721 Expo_Asp => Expo,
1722 Imp_Asp => Imp,
1723 LN_Asp => Dummy_3,
1724 Do_Checks => True);
1725
1726 -- Ensure that aspect External_Name applies to aspect Export or
1727 -- Import.
1728
1729 if A_Id = Aspect_External_Name then
1730 if No (Expo) and then No (Imp) then
b98e2969 1731 Error_Msg_N
2e885a6f
AC
1732 ("aspect `External_Name` requires aspect `Import` or "
1733 & "`Export`", Aspect);
b98e2969 1734 end if;
2e885a6f
AC
1735
1736 -- Otherwise ensure that aspect Link_Name applies to aspect
1737 -- Export or Import.
1738
1739 else
1740 pragma Assert (A_Id = Aspect_Link_Name);
1741 if No (Expo) and then No (Imp) then
1742 Error_Msg_N
1743 ("aspect `Link_Name` requires aspect `Import` or "
1744 & "`Export`", Aspect);
1745 end if;
1746 end if;
1747 end Analyze_Aspect_External_Link_Name;
eaba57fb 1748
b98e2969
AC
1749 -----------------------------------------
1750 -- Analyze_Aspect_Implicit_Dereference --
1751 -----------------------------------------
eaba57fb 1752
b98e2969 1753 procedure Analyze_Aspect_Implicit_Dereference is
b8a18216
ES
1754 Disc : Entity_Id;
1755 Parent_Disc : Entity_Id;
1756
b98e2969 1757 begin
616547fa 1758 if not Is_Type (E) or else not Has_Discriminants (E) then
b98e2969 1759 Error_Msg_N
b8a18216 1760 ("aspect must apply to a type with discriminants", Expr);
eaba57fb 1761
b8a18216
ES
1762 elsif not Is_Entity_Name (Expr) then
1763 Error_Msg_N
1764 ("aspect must name a discriminant of current type", Expr);
eaba57fb 1765
b8a18216
ES
1766 else
1767 Disc := First_Discriminant (E);
1768 while Present (Disc) loop
1769 if Chars (Expr) = Chars (Disc)
1770 and then Ekind (Etype (Disc)) =
1771 E_Anonymous_Access_Type
1772 then
1773 Set_Has_Implicit_Dereference (E);
1774 Set_Has_Implicit_Dereference (Disc);
1775 exit;
1776 end if;
eaba57fb 1777
b8a18216
ES
1778 Next_Discriminant (Disc);
1779 end loop;
eaba57fb 1780
40417de8 1781 -- Error if no proper access discriminant
eaba57fb 1782
b8a18216 1783 if No (Disc) then
2e885a6f 1784 Error_Msg_NE ("not an access discriminant of&", Expr, E);
b8a18216
ES
1785 return;
1786 end if;
1787 end if;
1788
40417de8
AC
1789 -- For a type extension, check whether parent has a
1790 -- reference discriminant, to verify that use is proper.
1791
b8a18216
ES
1792 if Is_Derived_Type (E)
1793 and then Has_Discriminants (Etype (E))
1794 then
1795 Parent_Disc := Get_Reference_Discriminant (Etype (E));
1796
1797 if Present (Parent_Disc)
1798 and then Corresponding_Discriminant (Disc) /= Parent_Disc
1799 then
2e885a6f
AC
1800 Error_Msg_N
1801 ("reference discriminant does not match discriminant "
1802 & "of parent type", Expr);
b8a18216 1803 end if;
b98e2969
AC
1804 end if;
1805 end Analyze_Aspect_Implicit_Dereference;
eaba57fb 1806
9d5598bf
AC
1807 -----------------------
1808 -- Make_Aitem_Pragma --
1809 -----------------------
1810
1811 procedure Make_Aitem_Pragma
1812 (Pragma_Argument_Associations : List_Id;
1813 Pragma_Name : Name_Id)
1814 is
80e59506
AC
1815 Args : List_Id := Pragma_Argument_Associations;
1816
9d5598bf
AC
1817 begin
1818 -- We should never get here if aspect was disabled
1819
1820 pragma Assert (not Is_Disabled (Aspect));
1821
4169c2d2
AC
1822 -- Certain aspects allow for an optional name or expression. Do
1823 -- not generate a pragma with empty argument association list.
80e59506
AC
1824
1825 if No (Args) or else No (Expression (First (Args))) then
1826 Args := No_List;
1827 end if;
1828
9d5598bf
AC
1829 -- Build the pragma
1830
1831 Aitem :=
1832 Make_Pragma (Loc,
80e59506 1833 Pragma_Argument_Associations => Args,
9d5598bf
AC
1834 Pragma_Identifier =>
1835 Make_Identifier (Sloc (Id), Pragma_Name),
3b1d4d82
AC
1836 Class_Present => Class_Present (Aspect),
1837 Split_PPC => Split_PPC (Aspect));
9d5598bf
AC
1838
1839 -- Set additional semantic fields
1840
1841 if Is_Ignored (Aspect) then
1842 Set_Is_Ignored (Aitem);
7fe6c026 1843 elsif Is_Checked (Aspect) then
3699edc4 1844 Set_Is_Checked (Aitem);
9d5598bf
AC
1845 end if;
1846
1847 Set_Corresponding_Aspect (Aitem, Aspect);
a2c314c7 1848 Set_From_Aspect_Specification (Aitem);
9d5598bf
AC
1849 end Make_Aitem_Pragma;
1850
1851 -- Start of processing for Analyze_One_Aspect
1852
0f1a6a0b 1853 begin
2178830b 1854 -- Skip aspect if already analyzed, to avoid looping in some cases
bd949ee2
RD
1855
1856 if Analyzed (Aspect) then
1857 goto Continue;
1858 end if;
1859
882eadaf
RD
1860 -- Skip looking at aspect if it is totally disabled. Just mark it
1861 -- as such for later reference in the tree. This also sets the
1862 -- Is_Ignored and Is_Checked flags appropriately.
aab45d22
AC
1863
1864 Check_Applicable_Policy (Aspect);
1865
1866 if Is_Disabled (Aspect) then
1867 goto Continue;
1868 end if;
1869
95160516
AC
1870 -- Set the source location of expression, used in the case of
1871 -- a failed precondition/postcondition or invariant. Note that
1872 -- the source location of the expression is not usually the best
1873 -- choice here. For example, it gets located on the last AND
1874 -- keyword in a chain of boolean expressiond AND'ed together.
1875 -- It is best to put the message on the first character of the
1876 -- assertion, which is the effect of the First_Node call here.
1877
1878 if Present (Expr) then
1879 Eloc := Sloc (First_Node (Expr));
1880 end if;
1881
e7fceebc
AC
1882 -- Check restriction No_Implementation_Aspect_Specifications
1883
9a7049fd 1884 if Implementation_Defined_Aspect (A_Id) then
e7fceebc
AC
1885 Check_Restriction
1886 (No_Implementation_Aspect_Specifications, Aspect);
1887 end if;
1888
1889 -- Check restriction No_Specification_Of_Aspect
1890
1891 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1892
0df5ae93 1893 -- Mark aspect analyzed (actual analysis is delayed till later)
e7fceebc 1894
bd949ee2 1895 Set_Analyzed (Aspect);
c159409f
AC
1896 Set_Entity (Aspect, E);
1897 Ent := New_Occurrence_Of (E, Sloc (Id));
1898
c775c209
AC
1899 -- Check for duplicate aspect. Note that the Comes_From_Source
1900 -- test allows duplicate Pre/Post's that we generate internally
1901 -- to escape being flagged here.
0f1a6a0b 1902
dac3bede
YM
1903 if No_Duplicates_Allowed (A_Id) then
1904 Anod := First (L);
1905 while Anod /= Aspect loop
9a7049fd
AC
1906 if Comes_From_Source (Aspect)
1907 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
dac3bede
YM
1908 then
1909 Error_Msg_Name_1 := Nam;
1910 Error_Msg_Sloc := Sloc (Anod);
beacce02 1911
dac3bede 1912 -- Case of same aspect specified twice
beacce02 1913
dac3bede
YM
1914 if Class_Present (Anod) = Class_Present (Aspect) then
1915 if not Class_Present (Anod) then
1916 Error_Msg_NE
1917 ("aspect% for & previously given#",
1918 Id, E);
1919 else
1920 Error_Msg_NE
1921 ("aspect `%''Class` for & previously given#",
1922 Id, E);
1923 end if;
beacce02 1924 end if;
dac3bede 1925 end if;
0f1a6a0b 1926
dac3bede
YM
1927 Next (Anod);
1928 end loop;
1929 end if;
0f1a6a0b 1930
dd91386d
AC
1931 -- Check some general restrictions on language defined aspects
1932
9a7049fd 1933 if not Implementation_Defined_Aspect (A_Id) then
dd91386d
AC
1934 Error_Msg_Name_1 := Nam;
1935
1936 -- Not allowed for renaming declarations
1937
1938 if Nkind (N) in N_Renaming_Declaration then
1939 Error_Msg_N
1940 ("aspect % not allowed for renaming declaration",
1941 Aspect);
1942 end if;
1943
1944 -- Not allowed for formal type declarations
1945
1946 if Nkind (N) = N_Formal_Type_Declaration then
1947 Error_Msg_N
1948 ("aspect % not allowed for formal type declaration",
1949 Aspect);
1950 end if;
1951 end if;
1952
47e11d08
AC
1953 -- Copy expression for later processing by the procedures
1954 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1955
1956 Set_Entity (Id, New_Copy_Tree (Expr));
1957
15e934bf
AC
1958 -- Set Delay_Required as appropriate to aspect
1959
1960 case Aspect_Delay (A_Id) is
1961 when Always_Delay =>
1962 Delay_Required := True;
1963
1964 when Never_Delay =>
1965 Delay_Required := False;
1966
1967 when Rep_Aspect =>
1968
1969 -- If expression has the form of an integer literal, then
1970 -- do not delay, since we know the value cannot change.
1971 -- This optimization catches most rep clause cases.
1972
07a64c02
AC
1973 -- For Boolean aspects, don't delay if no expression
1974
1975 if A_Id in Boolean_Aspects and then No (Expr) then
1976 Delay_Required := False;
1977
1978 -- For non-Boolean aspects, don't delay if integer literal
1979
1980 elsif A_Id not in Boolean_Aspects
1981 and then Present (Expr)
1982 and then Nkind (Expr) = N_Integer_Literal
1983 then
1984 Delay_Required := False;
1985
1986 -- All other cases are delayed
1987
1988 else
1989 Delay_Required := True;
1990 Set_Has_Delayed_Rep_Aspects (E);
1991 end if;
15e934bf
AC
1992 end case;
1993
0f1a6a0b
AC
1994 -- Processing based on specific aspect
1995
c159409f 1996 case A_Id is
d3ef4bd6
AC
1997 when Aspect_Unimplemented =>
1998 null; -- ??? temp for now
0f1a6a0b
AC
1999
2000 -- No_Aspect should be impossible
2001
2002 when No_Aspect =>
2003 raise Program_Error;
2004
b98e2969
AC
2005 -- Case 1: Aspects corresponding to attribute definition
2006 -- clauses.
0f1a6a0b 2007
f91510fc
AC
2008 when Aspect_Address |
2009 Aspect_Alignment |
2010 Aspect_Bit_Order |
2011 Aspect_Component_Size |
b98e2969 2012 Aspect_Constant_Indexing |
b98e2969
AC
2013 Aspect_Default_Iterator |
2014 Aspect_Dispatching_Domain |
f91510fc
AC
2015 Aspect_External_Tag |
2016 Aspect_Input |
dd2bf554 2017 Aspect_Iterable |
b98e2969 2018 Aspect_Iterator_Element |
f91510fc
AC
2019 Aspect_Machine_Radix |
2020 Aspect_Object_Size |
2021 Aspect_Output |
2022 Aspect_Read |
2023 Aspect_Scalar_Storage_Order |
2024 Aspect_Size |
2025 Aspect_Small |
2026 Aspect_Simple_Storage_Pool |
2027 Aspect_Storage_Pool |
f91510fc
AC
2028 Aspect_Stream_Size |
2029 Aspect_Value_Size |
b98e2969 2030 Aspect_Variable_Indexing |
f91510fc 2031 Aspect_Write =>
c159409f 2032
b98e2969
AC
2033 -- Indexing aspects apply only to tagged type
2034
2035 if (A_Id = Aspect_Constant_Indexing
15e934bf
AC
2036 or else
2037 A_Id = Aspect_Variable_Indexing)
b98e2969
AC
2038 and then not (Is_Type (E)
2039 and then Is_Tagged_Type (E))
2040 then
f3296dd3
AC
2041 Error_Msg_N
2042 ("indexing aspect can only apply to a tagged type",
adc876a8 2043 Aspect);
b98e2969
AC
2044 goto Continue;
2045 end if;
2046
7f2c8954 2047 -- For the case of aspect Address, we don't consider that we
27a8f150
AC
2048 -- know the entity is never set in the source, since it is
2049 -- is likely aliasing is occurring.
2050
2051 -- Note: one might think that the analysis of the resulting
2052 -- attribute definition clause would take care of that, but
2053 -- that's not the case since it won't be from source.
2054
2055 if A_Id = Aspect_Address then
2056 Set_Never_Set_In_Source (E, False);
2057 end if;
2058
c74afd84
AC
2059 -- Correctness of the profile of a stream operation is
2060 -- verified at the freeze point, but we must detect the
2061 -- illegal specification of this aspect for a subtype now,
2062 -- to prevent malformed rep_item chains.
2063
72eaa365
AC
2064 if A_Id = Aspect_Input or else
2065 A_Id = Aspect_Output or else
2066 A_Id = Aspect_Read or else
2067 A_Id = Aspect_Write
c74afd84 2068 then
72eaa365
AC
2069 if not Is_First_Subtype (E) then
2070 Error_Msg_N
2071 ("local name must be a first subtype", Aspect);
2072 goto Continue;
2073
2074 -- If stream aspect applies to the class-wide type,
2075 -- the generated attribute definition applies to the
2076 -- class-wide type as well.
2077
2078 elsif Class_Present (Aspect) then
2079 Ent :=
2080 Make_Attribute_Reference (Loc,
2081 Prefix => Ent,
2082 Attribute_Name => Name_Class);
2083 end if;
c74afd84
AC
2084 end if;
2085
c159409f
AC
2086 -- Construct the attribute definition clause
2087
2088 Aitem :=
811ef5ba 2089 Make_Attribute_Definition_Clause (Loc,
c159409f 2090 Name => Ent,
0f1a6a0b
AC
2091 Chars => Chars (Id),
2092 Expression => Relocate_Node (Expr));
2093
dd3b3672 2094 -- If the address is specified, then we treat the entity as
b5bdffcc
AC
2095 -- referenced, to avoid spurious warnings. This is analogous
2096 -- to what is done with an attribute definition clause, but
2097 -- here we don't want to generate a reference because this
2098 -- is the point of definition of the entity.
2099
2100 if A_Id = Aspect_Address then
2101 Set_Referenced (E);
2102 end if;
2103
aab45d22 2104 -- Case 2: Aspects corresponding to pragmas
c159409f 2105
b98e2969
AC
2106 -- Case 2a: Aspects corresponding to pragmas with two
2107 -- arguments, where the first argument is a local name
2108 -- referring to the entity, and the second argument is the
2109 -- aspect definition expression.
0f1a6a0b 2110
19992053 2111 -- Linker_Section/Suppress/Unsuppress
9d5598bf 2112
19992053
AC
2113 when Aspect_Linker_Section |
2114 Aspect_Suppress |
2115 Aspect_Unsuppress =>
0f1a6a0b 2116
9d5598bf
AC
2117 Make_Aitem_Pragma
2118 (Pragma_Argument_Associations => New_List (
2119 Make_Pragma_Argument_Association (Loc,
2120 Expression => New_Occurrence_Of (E, Loc)),
2121 Make_Pragma_Argument_Association (Sloc (Expr),
2122 Expression => Relocate_Node (Expr))),
2123 Pragma_Name => Chars (Id));
3860d469 2124
9d5598bf 2125 -- Synchronization
c159409f 2126
9d5598bf 2127 -- Corresponds to pragma Implemented, construct the pragma
6cbab959 2128
d62520f3 2129 when Aspect_Synchronization =>
9d5598bf
AC
2130 Make_Aitem_Pragma
2131 (Pragma_Argument_Associations => New_List (
2132 Make_Pragma_Argument_Association (Loc,
2133 Expression => New_Occurrence_Of (E, Loc)),
2134 Make_Pragma_Argument_Association (Sloc (Expr),
2135 Expression => Relocate_Node (Expr))),
2136 Pragma_Name => Name_Implemented);
6cbab959 2137
4e6768ab 2138 -- Attach_Handler
9d5598bf 2139
b98e2969 2140 when Aspect_Attach_Handler =>
9d5598bf
AC
2141 Make_Aitem_Pragma
2142 (Pragma_Argument_Associations => New_List (
2143 Make_Pragma_Argument_Association (Sloc (Ent),
2144 Expression => Ent),
2145 Make_Pragma_Argument_Association (Sloc (Expr),
2146 Expression => Relocate_Node (Expr))),
2147 Pragma_Name => Name_Attach_Handler);
2148
0df5ae93
AC
2149 -- We need to insert this pragma into the tree to get proper
2150 -- processing and to look valid from a placement viewpoint.
2151
4e6768ab 2152 Insert_Pragma (Aitem);
0df5ae93
AC
2153 goto Continue;
2154
9d5598bf 2155 -- Dynamic_Predicate, Predicate, Static_Predicate
b98e2969
AC
2156
2157 when Aspect_Dynamic_Predicate |
2158 Aspect_Predicate |
2159 Aspect_Static_Predicate =>
2160
ac072cb2
AC
2161 -- These aspects apply only to subtypes
2162
2163 if not Is_Type (E) then
2164 Error_Msg_N
2165 ("predicate can only be specified for a subtype",
2166 Aspect);
2167 goto Continue;
fd7215d7
AC
2168
2169 elsif Is_Incomplete_Type (E) then
2170 Error_Msg_N
2171 ("predicate cannot apply to incomplete view", Aspect);
2172 goto Continue;
ac072cb2
AC
2173 end if;
2174
b98e2969 2175 -- Construct the pragma (always a pragma Predicate, with
aab45d22
AC
2176 -- flags recording whether it is static/dynamic). We also
2177 -- set flags recording this in the type itself.
b98e2969 2178
9d5598bf
AC
2179 Make_Aitem_Pragma
2180 (Pragma_Argument_Associations => New_List (
2181 Make_Pragma_Argument_Association (Sloc (Ent),
2182 Expression => Ent),
2183 Make_Pragma_Argument_Association (Sloc (Expr),
2184 Expression => Relocate_Node (Expr))),
a2c314c7 2185 Pragma_Name => Name_Predicate);
b98e2969 2186
aab45d22
AC
2187 -- Mark type has predicates, and remember what kind of
2188 -- aspect lead to this predicate (we need this to access
2189 -- the right set of check policies later on).
2190
2191 Set_Has_Predicates (E);
2192
2193 if A_Id = Aspect_Dynamic_Predicate then
2194 Set_Has_Dynamic_Predicate_Aspect (E);
2195 elsif A_Id = Aspect_Static_Predicate then
2196 Set_Has_Static_Predicate_Aspect (E);
2197 end if;
2198
b98e2969 2199 -- If the type is private, indicate that its completion
113a62d9
RD
2200 -- has a freeze node, because that is the one that will
2201 -- be visible at freeze time.
b98e2969 2202
9d5598bf 2203 if Is_Private_Type (E) and then Present (Full_View (E)) then
b98e2969 2204 Set_Has_Predicates (Full_View (E));
aab45d22
AC
2205
2206 if A_Id = Aspect_Dynamic_Predicate then
2207 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
2208 elsif A_Id = Aspect_Static_Predicate then
2209 Set_Has_Static_Predicate_Aspect (Full_View (E));
2210 end if;
2211
b98e2969
AC
2212 Set_Has_Delayed_Aspects (Full_View (E));
2213 Ensure_Freeze_Node (Full_View (E));
2214 end if;
2215
a2c314c7
AC
2216 -- Predicate_Failure
2217
2218 when Aspect_Predicate_Failure =>
2219
2220 -- This aspect applies only to subtypes
2221
2222 if not Is_Type (E) then
2223 Error_Msg_N
2224 ("predicate can only be specified for a subtype",
2225 Aspect);
2226 goto Continue;
2227
2228 elsif Is_Incomplete_Type (E) then
2229 Error_Msg_N
2230 ("predicate cannot apply to incomplete view", Aspect);
2231 goto Continue;
2232 end if;
2233
2234 -- Construct the pragma
2235
2236 Make_Aitem_Pragma
2237 (Pragma_Argument_Associations => New_List (
2238 Make_Pragma_Argument_Association (Sloc (Ent),
2239 Expression => Ent),
2240 Make_Pragma_Argument_Association (Sloc (Expr),
2241 Expression => Relocate_Node (Expr))),
2242 Pragma_Name => Name_Predicate_Failure);
2243
2244 Set_Has_Predicates (E);
2245
2246 -- If the type is private, indicate that its completion
2247 -- has a freeze node, because that is the one that will
2248 -- be visible at freeze time.
2249
2250 if Is_Private_Type (E) and then Present (Full_View (E)) then
2251 Set_Has_Predicates (Full_View (E));
2252 Set_Has_Delayed_Aspects (Full_View (E));
2253 Ensure_Freeze_Node (Full_View (E));
2254 end if;
2255
b98e2969
AC
2256 -- Case 2b: Aspects corresponding to pragmas with two
2257 -- arguments, where the second argument is a local name
2258 -- referring to the entity, and the first argument is the
2259 -- aspect definition expression.
0f1a6a0b 2260
9d5598bf
AC
2261 -- Convention
2262
2e885a6f
AC
2263 when Aspect_Convention =>
2264 Analyze_Aspect_Convention;
2265 goto Continue;
41d8ee1d 2266
2e885a6f 2267 -- External_Name, Link_Name
41d8ee1d 2268
2e885a6f
AC
2269 when Aspect_External_Name |
2270 Aspect_Link_Name =>
2271 Analyze_Aspect_External_Link_Name;
2272 goto Continue;
4169b895 2273
9d5598bf
AC
2274 -- CPU, Interrupt_Priority, Priority
2275
cf3b97ef
AC
2276 -- These three aspects can be specified for a subprogram spec
2277 -- or body, in which case we analyze the expression and export
2278 -- the value of the aspect.
2279
2280 -- Previously, we generated an equivalent pragma for bodies
2281 -- (note that the specs cannot contain these pragmas). The
2282 -- pragma was inserted ahead of local declarations, rather than
2283 -- after the body. This leads to a certain duplication between
2284 -- the processing performed for the aspect and the pragma, but
2285 -- given the straightforward handling required it is simpler
2286 -- to duplicate than to translate the aspect in the spec into
2287 -- a pragma in the declarative part of the body.
473e20df
AC
2288
2289 when Aspect_CPU |
2290 Aspect_Interrupt_Priority |
2291 Aspect_Priority =>
aab45d22 2292
cf3b97ef
AC
2293 if Nkind_In (N, N_Subprogram_Body,
2294 N_Subprogram_Declaration)
2295 then
2296 -- Analyze the aspect expression
2297
2298 Analyze_And_Resolve (Expr, Standard_Integer);
2299
2300 -- Interrupt_Priority aspect not allowed for main
e9f97e79
AC
2301 -- subprograms. RM D.1 does not forbid this explicitly,
2302 -- but RM J.15.11(6/3) does not permit pragma
cf3b97ef
AC
2303 -- Interrupt_Priority for subprograms.
2304
2305 if A_Id = Aspect_Interrupt_Priority then
2306 Error_Msg_N
2307 ("Interrupt_Priority aspect cannot apply to "
2308 & "subprogram", Expr);
2309
2310 -- The expression must be static
2311
edab6088 2312 elsif not Is_OK_Static_Expression (Expr) then
cf3b97ef
AC
2313 Flag_Non_Static_Expr
2314 ("aspect requires static expression!", Expr);
2315
5644b7e8
AC
2316 -- Check whether this is the main subprogram. Issue a
2317 -- warning only if it is obviously not a main program
2318 -- (when it has parameters or when the subprogram is
2319 -- within a package).
2320
2321 elsif Present (Parameter_Specifications
2322 (Specification (N)))
2323 or else not Is_Compilation_Unit (Defining_Entity (N))
cf3b97ef 2324 then
e9f97e79 2325 -- See RM D.1(14/3) and D.16(12/3)
cf3b97ef
AC
2326
2327 Error_Msg_N
2328 ("aspect applied to subprogram other than the "
2329 & "main subprogram has no effect??", Expr);
2330
2331 -- Otherwise check in range and export the value
2332
2333 -- For the CPU aspect
2334
2335 elsif A_Id = Aspect_CPU then
2336 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
2337
2338 -- Value is correct so we export the value to make
2339 -- it available at execution time.
2340
2341 Set_Main_CPU
2342 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2343
2344 else
2345 Error_Msg_N
2346 ("main subprogram CPU is out of range", Expr);
2347 end if;
2348
2349 -- For the Priority aspect
2350
2351 elsif A_Id = Aspect_Priority then
2352 if Is_In_Range (Expr, RTE (RE_Priority)) then
2353
2354 -- Value is correct so we export the value to make
2355 -- it available at execution time.
2356
2357 Set_Main_Priority
2358 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
2359
53f697ee
AC
2360 -- Ignore pragma if Relaxed_RM_Semantics to support
2361 -- other targets/non GNAT compilers.
2362
2363 elsif not Relaxed_RM_Semantics then
cf3b97ef
AC
2364 Error_Msg_N
2365 ("main subprogram priority is out of range",
2366 Expr);
2367 end if;
2368 end if;
2369
2370 -- Load an arbitrary entity from System.Tasking.Stages
2371 -- or System.Tasking.Restricted.Stages (depending on
2372 -- the supported profile) to make sure that one of these
2373 -- packages is implicitly with'ed, since we need to have
2374 -- the tasking run time active for the pragma Priority to
6be44a9a 2375 -- have any effect. Previously we with'ed the package
cf3b97ef
AC
2376 -- System.Tasking, but this package does not trigger the
2377 -- required initialization of the run-time library.
2378
2379 declare
2380 Discard : Entity_Id;
cf3b97ef
AC
2381 begin
2382 if Restricted_Profile then
2383 Discard := RTE (RE_Activate_Restricted_Tasks);
2384 else
2385 Discard := RTE (RE_Activate_Tasks);
2386 end if;
2387 end;
2388
2389 -- Handling for these Aspects in subprograms is complete
2390
2391 goto Continue;
2392
877a5a12 2393 -- For tasks pass the aspect as an attribute
9d5598bf 2394
473e20df
AC
2395 else
2396 Aitem :=
2397 Make_Attribute_Definition_Clause (Loc,
2398 Name => Ent,
2399 Chars => Chars (Id),
2400 Expression => Relocate_Node (Expr));
2401 end if;
2402
9d5598bf
AC
2403 -- Warnings
2404
0f1a6a0b 2405 when Aspect_Warnings =>
9d5598bf
AC
2406 Make_Aitem_Pragma
2407 (Pragma_Argument_Associations => New_List (
2408 Make_Pragma_Argument_Association (Sloc (Expr),
2409 Expression => Relocate_Node (Expr)),
2410 Make_Pragma_Argument_Association (Loc,
2411 Expression => New_Occurrence_Of (E, Loc))),
2412 Pragma_Name => Chars (Id));
0f1a6a0b 2413
877a5a12
AC
2414 Decorate (Aspect, Aitem);
2415 Insert_Pragma (Aitem);
2416 goto Continue;
2417
b98e2969
AC
2418 -- Case 2c: Aspects corresponding to pragmas with three
2419 -- arguments.
a01b9df6 2420
b98e2969
AC
2421 -- Invariant aspects have a first argument that references the
2422 -- entity, a second argument that is the expression and a third
2423 -- argument that is an appropriate message.
a01b9df6 2424
9d5598bf
AC
2425 -- Invariant, Type_Invariant
2426
b98e2969
AC
2427 when Aspect_Invariant |
2428 Aspect_Type_Invariant =>
a01b9df6 2429
b98e2969
AC
2430 -- Analysis of the pragma will verify placement legality:
2431 -- an invariant must apply to a private type, or appear in
2432 -- the private part of a spec and apply to a completion.
a01b9df6 2433
9d5598bf
AC
2434 Make_Aitem_Pragma
2435 (Pragma_Argument_Associations => New_List (
2436 Make_Pragma_Argument_Association (Sloc (Ent),
2437 Expression => Ent),
2438 Make_Pragma_Argument_Association (Sloc (Expr),
2439 Expression => Relocate_Node (Expr))),
2440 Pragma_Name => Name_Invariant);
b98e2969
AC
2441
2442 -- Add message unless exception messages are suppressed
2443
2444 if not Opt.Exception_Locations_Suppressed then
2445 Append_To (Pragma_Argument_Associations (Aitem),
2446 Make_Pragma_Argument_Association (Eloc,
2447 Chars => Name_Message,
2448 Expression =>
2449 Make_String_Literal (Eloc,
2450 Strval => "failed invariant from "
2451 & Build_Location_String (Eloc))));
a01b9df6
AC
2452 end if;
2453
b98e2969
AC
2454 -- For Invariant case, insert immediately after the entity
2455 -- declaration. We do not have to worry about delay issues
2456 -- since the pragma processing takes care of this.
2457
b98e2969 2458 Delay_Required := False;
a01b9df6 2459
c116143c
ES
2460 -- Case 2d : Aspects that correspond to a pragma with one
2461 -- argument.
2462
9d5598bf 2463 -- Abstract_State
cf6956bb 2464
54e28df2
HK
2465 -- Aspect Abstract_State introduces implicit declarations for
2466 -- all state abstraction entities it defines. To emulate this
2467 -- behavior, insert the pragma at the beginning of the visible
2468 -- declarations of the related package so that it is analyzed
2469 -- immediately.
2470
39af2bac 2471 when Aspect_Abstract_State => Abstract_State : declare
c0cdbd39 2472 Context : Node_Id := N;
39af2bac
AC
2473
2474 begin
c0cdbd39
AC
2475 -- When aspect Abstract_State appears on a generic package,
2476 -- it is propageted to the package instance. The context in
2477 -- this case is the instance spec.
2478
2479 if Nkind (Context) = N_Package_Instantiation then
2480 Context := Instance_Spec (Context);
2481 end if;
2482
2483 if Nkind_In (Context, N_Generic_Package_Declaration,
2484 N_Package_Declaration)
39af2bac 2485 then
39af2bac
AC
2486 Make_Aitem_Pragma
2487 (Pragma_Argument_Associations => New_List (
2488 Make_Pragma_Argument_Association (Loc,
2489 Expression => Relocate_Node (Expr))),
2490 Pragma_Name => Name_Abstract_State);
ebb6b0bd 2491
21d7ef70
AC
2492 Decorate (Aspect, Aitem);
2493 Insert_Pragma
2494 (Prag => Aitem,
2495 Is_Instance =>
2496 Is_Generic_Instance (Defining_Entity (Context)));
39af2bac
AC
2497
2498 else
2499 Error_Msg_NE
2500 ("aspect & must apply to a package declaration",
2501 Aspect, Id);
2502 end if;
2503
2504 goto Continue;
2505 end Abstract_State;
cf6956bb 2506
847d950d
HK
2507 -- Aspect Async_Readers is never delayed because it is
2508 -- equivalent to a source pragma which appears after the
2509 -- related object declaration.
2510
2511 when Aspect_Async_Readers =>
2512 Make_Aitem_Pragma
2513 (Pragma_Argument_Associations => New_List (
2514 Make_Pragma_Argument_Association (Loc,
2515 Expression => Relocate_Node (Expr))),
2516 Pragma_Name => Name_Async_Readers);
2517
2518 Decorate (Aspect, Aitem);
2519 Insert_Pragma (Aitem);
2520 goto Continue;
2521
2522 -- Aspect Async_Writers is never delayed because it is
2523 -- equivalent to a source pragma which appears after the
2524 -- related object declaration.
2525
2526 when Aspect_Async_Writers =>
2527 Make_Aitem_Pragma
2528 (Pragma_Argument_Associations => New_List (
2529 Make_Pragma_Argument_Association (Loc,
2530 Expression => Relocate_Node (Expr))),
2531 Pragma_Name => Name_Async_Writers);
2532
2533 Decorate (Aspect, Aitem);
2534 Insert_Pragma (Aitem);
2535 goto Continue;
2536
1df7c326
AC
2537 -- Aspect Constant_After_Elaboration is never delayed because
2538 -- it is equivalent to a source pragma which appears after the
2539 -- related object declaration.
2540
2541 when Aspect_Constant_After_Elaboration =>
2542 Make_Aitem_Pragma
2543 (Pragma_Argument_Associations => New_List (
2544 Make_Pragma_Argument_Association (Loc,
2545 Expression => Relocate_Node (Expr))),
2546 Pragma_Name =>
2547 Name_Constant_After_Elaboration);
2548
2549 Decorate (Aspect, Aitem);
2550 Insert_Pragma (Aitem);
2551 goto Continue;
2552
e477d718
AC
2553 -- Aspect Default_Internal_Condition is never delayed because
2554 -- it is equivalent to a source pragma which appears after the
2555 -- related private type. To deal with forward references, the
2556 -- generated pragma is stored in the rep chain of the related
2557 -- private type as types do not carry contracts. The pragma is
2558 -- wrapped inside of a procedure at the freeze point of the
2559 -- private type's full view.
2560
2561 when Aspect_Default_Initial_Condition =>
2562 Make_Aitem_Pragma
2563 (Pragma_Argument_Associations => New_List (
2564 Make_Pragma_Argument_Association (Loc,
2565 Expression => Relocate_Node (Expr))),
2566 Pragma_Name =>
2567 Name_Default_Initial_Condition);
2568
2569 Decorate (Aspect, Aitem);
2570 Insert_Pragma (Aitem);
2571 goto Continue;
2572
2ef05128
AC
2573 -- Default_Storage_Pool
2574
2575 when Aspect_Default_Storage_Pool =>
2576 Make_Aitem_Pragma
2577 (Pragma_Argument_Associations => New_List (
2578 Make_Pragma_Argument_Association (Loc,
2579 Expression => Relocate_Node (Expr))),
2580 Pragma_Name =>
2581 Name_Default_Storage_Pool);
2582
2583 Decorate (Aspect, Aitem);
2584 Insert_Pragma (Aitem);
2585 goto Continue;
2586
9d5598bf
AC
2587 -- Depends
2588
4e6768ab
AC
2589 -- Aspect Depends is never delayed because it is equivalent to
2590 -- a source pragma which appears after the related subprogram.
2591 -- To deal with forward references, the generated pragma is
2592 -- stored in the contract of the related subprogram and later
2593 -- analyzed at the end of the declarative region. See routine
2594 -- Analyze_Depends_In_Decl_Part for details.
fd8b4053 2595
fe96ecb9 2596 when Aspect_Depends =>
9d5598bf
AC
2597 Make_Aitem_Pragma
2598 (Pragma_Argument_Associations => New_List (
2599 Make_Pragma_Argument_Association (Loc,
2600 Expression => Relocate_Node (Expr))),
2601 Pragma_Name => Name_Depends);
2602
4e6768ab
AC
2603 Decorate (Aspect, Aitem);
2604 Insert_Pragma (Aitem);
d6095153
AC
2605 goto Continue;
2606
847d950d
HK
2607 -- Aspect Effecitve_Reads is never delayed because it is
2608 -- equivalent to a source pragma which appears after the
2609 -- related object declaration.
2610
2611 when Aspect_Effective_Reads =>
2612 Make_Aitem_Pragma
2613 (Pragma_Argument_Associations => New_List (
2614 Make_Pragma_Argument_Association (Loc,
2615 Expression => Relocate_Node (Expr))),
2616 Pragma_Name => Name_Effective_Reads);
2617
2618 Decorate (Aspect, Aitem);
2619 Insert_Pragma (Aitem);
2620 goto Continue;
2621
2622 -- Aspect Effective_Writes is never delayed because it is
2623 -- equivalent to a source pragma which appears after the
2624 -- related object declaration.
2625
2626 when Aspect_Effective_Writes =>
2627 Make_Aitem_Pragma
2628 (Pragma_Argument_Associations => New_List (
2629 Make_Pragma_Argument_Association (Loc,
2630 Expression => Relocate_Node (Expr))),
2631 Pragma_Name => Name_Effective_Writes);
2632
2633 Decorate (Aspect, Aitem);
2634 Insert_Pragma (Aitem);
2635 goto Continue;
2636
039538bc
AC
2637 -- Aspect Extensions_Visible is never delayed because it is
2638 -- equivalent to a source pragma which appears after the
2639 -- related subprogram.
2640
2641 when Aspect_Extensions_Visible =>
2642 Make_Aitem_Pragma
2643 (Pragma_Argument_Associations => New_List (
2644 Make_Pragma_Argument_Association (Loc,
2645 Expression => Relocate_Node (Expr))),
2646 Pragma_Name => Name_Extensions_Visible);
2647
2648 Decorate (Aspect, Aitem);
2649 Insert_Pragma (Aitem);
2650 goto Continue;
2651
c5cec2fe
AC
2652 -- Aspect Ghost is never delayed because it is equivalent to a
2653 -- source pragma which appears at the top of [generic] package
2654 -- declarations or after an object, a [generic] subprogram, or
2655 -- a type declaration.
2656
21d7ef70 2657 when Aspect_Ghost =>
c5cec2fe
AC
2658 Make_Aitem_Pragma
2659 (Pragma_Argument_Associations => New_List (
2660 Make_Pragma_Argument_Association (Loc,
2661 Expression => Relocate_Node (Expr))),
2662 Pragma_Name => Name_Ghost);
2663
2664 Decorate (Aspect, Aitem);
21d7ef70 2665 Insert_Pragma (Aitem);
c5cec2fe 2666 goto Continue;
c5cec2fe 2667
9d5598bf 2668 -- Global
fe96ecb9 2669
4e6768ab
AC
2670 -- Aspect Global is never delayed because it is equivalent to
2671 -- a source pragma which appears after the related subprogram.
2672 -- To deal with forward references, the generated pragma is
2673 -- stored in the contract of the related subprogram and later
2674 -- analyzed at the end of the declarative region. See routine
2675 -- Analyze_Global_In_Decl_Part for details.
7a1f1775
AC
2676
2677 when Aspect_Global =>
9d5598bf
AC
2678 Make_Aitem_Pragma
2679 (Pragma_Argument_Associations => New_List (
2680 Make_Pragma_Argument_Association (Loc,
2681 Expression => Relocate_Node (Expr))),
2682 Pragma_Name => Name_Global);
2683
4e6768ab
AC
2684 Decorate (Aspect, Aitem);
2685 Insert_Pragma (Aitem);
d6095153
AC
2686 goto Continue;
2687
9b2451e5
AC
2688 -- Initial_Condition
2689
4e6768ab
AC
2690 -- Aspect Initial_Condition is never delayed because it is
2691 -- equivalent to a source pragma which appears after the
2692 -- related package. To deal with forward references, the
2693 -- generated pragma is stored in the contract of the related
2694 -- package and later analyzed at the end of the declarative
2695 -- region. See routine Analyze_Initial_Condition_In_Decl_Part
2696 -- for details.
9b2451e5
AC
2697
2698 when Aspect_Initial_Condition => Initial_Condition : declare
c0cdbd39 2699 Context : Node_Id := N;
9b2451e5
AC
2700
2701 begin
4e6768ab
AC
2702 -- When aspect Initial_Condition appears on a generic
2703 -- package, it is propageted to the package instance. The
2704 -- context in this case is the instance spec.
c0cdbd39
AC
2705
2706 if Nkind (Context) = N_Package_Instantiation then
2707 Context := Instance_Spec (Context);
2708 end if;
2709
2710 if Nkind_In (Context, N_Generic_Package_Declaration,
2711 N_Package_Declaration)
9b2451e5 2712 then
9b2451e5
AC
2713 Make_Aitem_Pragma
2714 (Pragma_Argument_Associations => New_List (
2715 Make_Pragma_Argument_Association (Loc,
2716 Expression => Relocate_Node (Expr))),
2717 Pragma_Name =>
2718 Name_Initial_Condition);
9b2451e5 2719
21d7ef70
AC
2720 Decorate (Aspect, Aitem);
2721 Insert_Pragma
2722 (Prag => Aitem,
2723 Is_Instance =>
2724 Is_Generic_Instance (Defining_Entity (Context)));
e59243fa 2725
21d7ef70 2726 -- Otherwise the context is illegal
9b2451e5
AC
2727
2728 else
2729 Error_Msg_NE
2730 ("aspect & must apply to a package declaration",
2731 Aspect, Id);
2732 end if;
2733
2734 goto Continue;
2735 end Initial_Condition;
2736
54e28df2
HK
2737 -- Initializes
2738
4e6768ab
AC
2739 -- Aspect Initializes is never delayed because it is equivalent
2740 -- to a source pragma appearing after the related package. To
2741 -- deal with forward references, the generated pragma is stored
2742 -- in the contract of the related package and later analyzed at
2743 -- the end of the declarative region. For details, see routine
2744 -- Analyze_Initializes_In_Decl_Part.
54e28df2
HK
2745
2746 when Aspect_Initializes => Initializes : declare
c0cdbd39 2747 Context : Node_Id := N;
54e28df2
HK
2748
2749 begin
e59243fa
AC
2750 -- When aspect Initializes appears on a generic package,
2751 -- it is propageted to the package instance. The context
2752 -- in this case is the instance spec.
c0cdbd39
AC
2753
2754 if Nkind (Context) = N_Package_Instantiation then
2755 Context := Instance_Spec (Context);
2756 end if;
2757
2758 if Nkind_In (Context, N_Generic_Package_Declaration,
2759 N_Package_Declaration)
54e28df2 2760 then
54e28df2
HK
2761 Make_Aitem_Pragma
2762 (Pragma_Argument_Associations => New_List (
2763 Make_Pragma_Argument_Association (Loc,
2764 Expression => Relocate_Node (Expr))),
2765 Pragma_Name => Name_Initializes);
54e28df2 2766
21d7ef70
AC
2767 Decorate (Aspect, Aitem);
2768 Insert_Pragma
2769 (Prag => Aitem,
2770 Is_Instance =>
2771 Is_Generic_Instance (Defining_Entity (Context)));
e59243fa 2772
21d7ef70 2773 -- Otherwise the context is illegal
54e28df2
HK
2774
2775 else
2776 Error_Msg_NE
2777 ("aspect & must apply to a package declaration",
2778 Aspect, Id);
2779 end if;
2780
2781 goto Continue;
2782 end Initializes;
2783
c2a2dbcc
RD
2784 -- Obsolescent
2785
2786 when Aspect_Obsolescent => declare
2787 Args : List_Id;
2788
2789 begin
2790 if No (Expr) then
2791 Args := No_List;
2792 else
2793 Args := New_List (
2794 Make_Pragma_Argument_Association (Sloc (Expr),
2795 Expression => Relocate_Node (Expr)));
2796 end if;
2797
2798 Make_Aitem_Pragma
2799 (Pragma_Argument_Associations => Args,
2800 Pragma_Name => Chars (Id));
2801 end;
2802
d7af5ea5
HK
2803 -- Part_Of
2804
2805 when Aspect_Part_Of =>
2806 if Nkind_In (N, N_Object_Declaration,
2807 N_Package_Instantiation)
75b87c16 2808 or else Is_Single_Concurrent_Type_Declaration (N)
d7af5ea5
HK
2809 then
2810 Make_Aitem_Pragma
2811 (Pragma_Argument_Associations => New_List (
2812 Make_Pragma_Argument_Association (Loc,
2813 Expression => Relocate_Node (Expr))),
2814 Pragma_Name => Name_Part_Of);
2815
75b87c16
AC
2816 Decorate (Aspect, Aitem);
2817 Insert_Pragma (Aitem);
75b87c16 2818
d7af5ea5
HK
2819 else
2820 Error_Msg_NE
75b87c16
AC
2821 ("aspect & must apply to package instantiation, "
2822 & "object, single protected type or single task type",
2823 Aspect, Id);
d7af5ea5
HK
2824 end if;
2825
90b510e4
AC
2826 goto Continue;
2827
1c6269d3
HK
2828 -- SPARK_Mode
2829
877a5a12 2830 when Aspect_SPARK_Mode =>
1c6269d3
HK
2831 Make_Aitem_Pragma
2832 (Pragma_Argument_Associations => New_List (
2833 Make_Pragma_Argument_Association (Loc,
2834 Expression => Relocate_Node (Expr))),
2835 Pragma_Name => Name_SPARK_Mode);
1c6269d3 2836
877a5a12
AC
2837 Decorate (Aspect, Aitem);
2838 Insert_Pragma (Aitem);
2839 goto Continue;
5ff90f08 2840
ea3c0651
AC
2841 -- Refined_Depends
2842
4e6768ab
AC
2843 -- Aspect Refined_Depends is never delayed because it is
2844 -- equivalent to a source pragma which appears in the
2845 -- declarations of the related subprogram body. To deal with
2846 -- forward references, the generated pragma is stored in the
2847 -- contract of the related subprogram body and later analyzed
2848 -- at the end of the declarative region. For details, see
2849 -- routine Analyze_Refined_Depends_In_Decl_Part.
ea3c0651
AC
2850
2851 when Aspect_Refined_Depends =>
39d3009f
AC
2852 Make_Aitem_Pragma
2853 (Pragma_Argument_Associations => New_List (
2854 Make_Pragma_Argument_Association (Loc,
2855 Expression => Relocate_Node (Expr))),
2856 Pragma_Name => Name_Refined_Depends);
2857
4e6768ab
AC
2858 Decorate (Aspect, Aitem);
2859 Insert_Pragma (Aitem);
39d3009f 2860 goto Continue;
ea3c0651
AC
2861
2862 -- Refined_Global
2863
4e6768ab
AC
2864 -- Aspect Refined_Global is never delayed because it is
2865 -- equivalent to a source pragma which appears in the
2866 -- declarations of the related subprogram body. To deal with
2867 -- forward references, the generated pragma is stored in the
2868 -- contract of the related subprogram body and later analyzed
2869 -- at the end of the declarative region. For details, see
2870 -- routine Analyze_Refined_Global_In_Decl_Part.
ea3c0651
AC
2871
2872 when Aspect_Refined_Global =>
ab8843fa
HK
2873 Make_Aitem_Pragma
2874 (Pragma_Argument_Associations => New_List (
2875 Make_Pragma_Argument_Association (Loc,
2876 Expression => Relocate_Node (Expr))),
2877 Pragma_Name => Name_Refined_Global);
2878
4e6768ab
AC
2879 Decorate (Aspect, Aitem);
2880 Insert_Pragma (Aitem);
ab8843fa 2881 goto Continue;
ea3c0651 2882
e7f23f06
AC
2883 -- Refined_Post
2884
2885 when Aspect_Refined_Post =>
2886 Make_Aitem_Pragma
2887 (Pragma_Argument_Associations => New_List (
2888 Make_Pragma_Argument_Association (Loc,
2889 Expression => Relocate_Node (Expr))),
2890 Pragma_Name => Name_Refined_Post);
2891
8a0183fd
HK
2892 Decorate (Aspect, Aitem);
2893 Insert_Pragma (Aitem);
2894 goto Continue;
2895
39af2bac
AC
2896 -- Refined_State
2897
21d7ef70 2898 when Aspect_Refined_State =>
39af2bac 2899
39af2bac
AC
2900 -- The corresponding pragma for Refined_State is inserted in
2901 -- the declarations of the related package body. This action
2902 -- synchronizes both the source and from-aspect versions of
2903 -- the pragma.
2904
2905 if Nkind (N) = N_Package_Body then
39af2bac
AC
2906 Make_Aitem_Pragma
2907 (Pragma_Argument_Associations => New_List (
2908 Make_Pragma_Argument_Association (Loc,
2909 Expression => Relocate_Node (Expr))),
2910 Pragma_Name => Name_Refined_State);
2dade097 2911
21d7ef70
AC
2912 Decorate (Aspect, Aitem);
2913 Insert_Pragma (Aitem);
2dade097 2914
21d7ef70 2915 -- Otherwise the context is illegal
39af2bac
AC
2916
2917 else
2918 Error_Msg_NE
2919 ("aspect & must apply to a package body", Aspect, Id);
2920 end if;
2921
2922 goto Continue;
39af2bac 2923
9d5598bf 2924 -- Relative_Deadline
7a1f1775
AC
2925
2926 when Aspect_Relative_Deadline =>
9d5598bf
AC
2927 Make_Aitem_Pragma
2928 (Pragma_Argument_Associations => New_List (
2929 Make_Pragma_Argument_Association (Loc,
2930 Expression => Relocate_Node (Expr))),
2931 Pragma_Name => Name_Relative_Deadline);
c116143c
ES
2932
2933 -- If the aspect applies to a task, the corresponding pragma
2934 -- must appear within its declarations, not after.
2935
2936 if Nkind (N) = N_Task_Type_Declaration then
2937 declare
2938 Def : Node_Id;
2939 V : List_Id;
2940
2941 begin
2942 if No (Task_Definition (N)) then
2943 Set_Task_Definition (N,
2944 Make_Task_Definition (Loc,
2945 Visible_Declarations => New_List,
2946 End_Label => Empty));
2947 end if;
2948
2949 Def := Task_Definition (N);
2950 V := Visible_Declarations (Def);
2951 if not Is_Empty_List (V) then
2952 Insert_Before (First (V), Aitem);
2953
2954 else
2955 Set_Visible_Declarations (Def, New_List (Aitem));
2956 end if;
2957
2958 goto Continue;
2959 end;
2960 end if;
2961
847d950d
HK
2962 -- Aspect Volatile_Function is never delayed because it is
2963 -- equivalent to a source pragma which appears after the
2964 -- related subprogram.
2965
2966 when Aspect_Volatile_Function =>
2967 Make_Aitem_Pragma
2968 (Pragma_Argument_Associations => New_List (
2969 Make_Pragma_Argument_Association (Loc,
2970 Expression => Relocate_Node (Expr))),
2971 Pragma_Name => Name_Volatile_Function);
2972
2973 Decorate (Aspect, Aitem);
2974 Insert_Pragma (Aitem);
2975 goto Continue;
2976
52d9ba4d
AC
2977 -- Case 2e: Annotate aspect
2978
2979 when Aspect_Annotate =>
2980 declare
2981 Args : List_Id;
2982 Pargs : List_Id;
2983 Arg : Node_Id;
2984
2985 begin
2986 -- The argument can be a single identifier
2987
2988 if Nkind (Expr) = N_Identifier then
2989
2990 -- One level of parens is allowed
2991
2992 if Paren_Count (Expr) > 1 then
2993 Error_Msg_F ("extra parentheses ignored", Expr);
2994 end if;
2995
2996 Set_Paren_Count (Expr, 0);
2997
2998 -- Add the single item to the list
2999
3000 Args := New_List (Expr);
3001
3002 -- Otherwise we must have an aggregate
3003
3004 elsif Nkind (Expr) = N_Aggregate then
3005
3006 -- Must be positional
3007
3008 if Present (Component_Associations (Expr)) then
3009 Error_Msg_F
3010 ("purely positional aggregate required", Expr);
3011 goto Continue;
3012 end if;
3013
3014 -- Must not be parenthesized
3015
3016 if Paren_Count (Expr) /= 0 then
3017 Error_Msg_F ("extra parentheses ignored", Expr);
3018 end if;
3019
3020 -- List of arguments is list of aggregate expressions
3021
3022 Args := Expressions (Expr);
3023
3024 -- Anything else is illegal
3025
3026 else
3027 Error_Msg_F ("wrong form for Annotate aspect", Expr);
3028 goto Continue;
3029 end if;
3030
3031 -- Prepare pragma arguments
3032
3033 Pargs := New_List;
3034 Arg := First (Args);
3035 while Present (Arg) loop
3036 Append_To (Pargs,
3037 Make_Pragma_Argument_Association (Sloc (Arg),
3038 Expression => Relocate_Node (Arg)));
3039 Next (Arg);
3040 end loop;
3041
3042 Append_To (Pargs,
3043 Make_Pragma_Argument_Association (Sloc (Ent),
3044 Chars => Name_Entity,
3045 Expression => Ent));
3046
3047 Make_Aitem_Pragma
3048 (Pragma_Argument_Associations => Pargs,
3049 Pragma_Name => Name_Annotate);
3050 end;
3051
b98e2969
AC
3052 -- Case 3 : Aspects that don't correspond to pragma/attribute
3053 -- definition clause.
6d9e03cb 3054
b98e2969
AC
3055 -- Case 3a: The aspects listed below don't correspond to
3056 -- pragmas/attributes but do require delayed analysis.
2ef48385 3057
33c9f9af
AC
3058 -- Default_Value can only apply to a scalar type
3059
3060 when Aspect_Default_Value =>
3061 if not Is_Scalar_Type (E) then
3062 Error_Msg_N
ad075b50 3063 ("aspect Default_Value must apply to a scalar type", N);
33c9f9af
AC
3064 end if;
3065
3066 Aitem := Empty;
3067
3068 -- Default_Component_Value can only apply to an array type
3069 -- with scalar components.
3070
3071 when Aspect_Default_Component_Value =>
3072 if not (Is_Array_Type (E)
adc876a8 3073 and then Is_Scalar_Type (Component_Type (E)))
33c9f9af 3074 then
2e885a6f
AC
3075 Error_Msg_N
3076 ("aspect Default_Component_Value can only apply to an "
3077 & "array of scalar components", N);
33c9f9af 3078 end if;
9d5598bf 3079
b98e2969 3080 Aitem := Empty;
2ef48385 3081
b98e2969
AC
3082 -- Case 3b: The aspects listed below don't correspond to
3083 -- pragmas/attributes and don't need delayed analysis.
ddf67a1d 3084
9d5598bf
AC
3085 -- Implicit_Dereference
3086
b98e2969
AC
3087 -- For Implicit_Dereference, External_Name and Link_Name, only
3088 -- the legality checks are done during the analysis, thus no
3089 -- delay is required.
fecbd779 3090
b98e2969
AC
3091 when Aspect_Implicit_Dereference =>
3092 Analyze_Aspect_Implicit_Dereference;
3093 goto Continue;
2ef48385 3094
9d5598bf
AC
3095 -- Dimension
3096
b98e2969
AC
3097 when Aspect_Dimension =>
3098 Analyze_Aspect_Dimension (N, Id, Expr);
3099 goto Continue;
516f608f 3100
9d5598bf
AC
3101 -- Dimension_System
3102
b98e2969
AC
3103 when Aspect_Dimension_System =>
3104 Analyze_Aspect_Dimension_System (N, Id, Expr);
3105 goto Continue;
2ef48385 3106
2cbac6c6 3107 -- Case 4: Aspects requiring special handling
aab45d22 3108
541fb4d9
YM
3109 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
3110 -- pragmas take care of the delay.
2ef48385 3111
9d5598bf
AC
3112 -- Pre/Post
3113
c775c209
AC
3114 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
3115 -- with a first argument that is the expression, and a second
3116 -- argument that is an informative message if the test fails.
3117 -- This is inserted right after the declaration, to get the
e606088a
AC
3118 -- required pragma placement. The processing for the pragmas
3119 -- takes care of the required delay.
0f1a6a0b 3120
5afe5d2d 3121 when Pre_Post_Aspects => Pre_Post : declare
c775c209 3122 Pname : Name_Id;
0f1a6a0b 3123
c775c209 3124 begin
857ade1b 3125 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
c775c209
AC
3126 Pname := Name_Precondition;
3127 else
3128 Pname := Name_Postcondition;
3129 end if;
c159409f 3130
57323d5b
AC
3131 -- Check that the class-wide predicate cannot be applied to
3132 -- an operation of a synchronized type that is not a tagged
3133 -- type. Other legality checks are performed when analyzing
3134 -- the contract of the operation.
3135
3136 if Class_Present (Aspect)
3137 and then Is_Concurrent_Type (Current_Scope)
3138 and then not Is_Tagged_Type (Current_Scope)
3139 and then Ekind_In (E, E_Entry, E_Function, E_Procedure)
3140 then
3141 Error_Msg_Name_1 := Original_Aspect_Pragma_Name (Aspect);
3142 Error_Msg_N
3143 ("aspect % can only be specified for a primitive "
3144 & "operation of a tagged type", Aspect);
3145
3146 goto Continue;
3147 end if;
3148
c775c209
AC
3149 -- If the expressions is of the form A and then B, then
3150 -- we generate separate Pre/Post aspects for the separate
3151 -- clauses. Since we allow multiple pragmas, there is no
3152 -- problem in allowing multiple Pre/Post aspects internally.
2d395256
AC
3153 -- These should be treated in reverse order (B first and
3154 -- A second) since they are later inserted just after N in
3155 -- the order they are treated. This way, the pragma for A
3156 -- ends up preceding the pragma for B, which may have an
3157 -- importance for the error raised (either constraint error
3158 -- or precondition error).
c775c209 3159
beacce02 3160 -- We do not do this for Pre'Class, since we have to put
33c9f9af 3161 -- these conditions together in a complex OR expression.
0f1a6a0b 3162
8c18a165
AC
3163 -- We do not do this in ASIS mode, as ASIS relies on the
3164 -- original node representing the complete expression, when
3165 -- retrieving it through the source aspect table.
3166
3167 if not ASIS_Mode
3168 and then (Pname = Name_Postcondition
3169 or else not Class_Present (Aspect))
beacce02
AC
3170 then
3171 while Nkind (Expr) = N_And_Then loop
3172 Insert_After (Aspect,
2d395256 3173 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
beacce02 3174 Identifier => Identifier (Aspect),
2d395256 3175 Expression => Relocate_Node (Left_Opnd (Expr)),
beacce02
AC
3176 Class_Present => Class_Present (Aspect),
3177 Split_PPC => True));
2d395256 3178 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
beacce02
AC
3179 Eloc := Sloc (Expr);
3180 end loop;
3181 end if;
0f1a6a0b 3182
a9e892d0
AC
3183 -- Build the precondition/postcondition pragma
3184
33c9f9af 3185 -- Add note about why we do NOT need Copy_Tree here???
c159409f 3186
9d5598bf
AC
3187 Make_Aitem_Pragma
3188 (Pragma_Argument_Associations => New_List (
3189 Make_Pragma_Argument_Association (Eloc,
3190 Chars => Name_Check,
1bc33112 3191 Expression => Relocate_Node (Expr))),
9d5598bf 3192 Pragma_Name => Pname);
beacce02
AC
3193
3194 -- Add message unless exception messages are suppressed
3195
3196 if not Opt.Exception_Locations_Suppressed then
3197 Append_To (Pragma_Argument_Associations (Aitem),
3198 Make_Pragma_Argument_Association (Eloc,
c9d70ab1 3199 Chars => Name_Message,
beacce02
AC
3200 Expression =>
3201 Make_String_Literal (Eloc,
3202 Strval => "failed "
3203 & Get_Name_String (Pname)
3204 & " from "
3205 & Build_Location_String (Eloc))));
3206 end if;
c159409f 3207
47e11d08 3208 Set_Is_Delayed_Aspect (Aspect);
c159409f 3209
c775c209
AC
3210 -- For Pre/Post cases, insert immediately after the entity
3211 -- declaration, since that is the required pragma placement.
3212 -- Note that for these aspects, we do not have to worry
3213 -- about delay issues, since the pragmas themselves deal
3214 -- with delay of visibility for the expression analysis.
3215
4e6768ab 3216 Insert_Pragma (Aitem);
5c5e108f 3217
c775c209 3218 goto Continue;
5afe5d2d 3219 end Pre_Post;
0f1a6a0b 3220
9d5598bf
AC
3221 -- Test_Case
3222
541fb4d9
YM
3223 when Aspect_Test_Case => Test_Case : declare
3224 Args : List_Id;
3225 Comp_Expr : Node_Id;
3226 Comp_Assn : Node_Id;
3227 New_Expr : Node_Id;
3860d469 3228
541fb4d9
YM
3229 begin
3230 Args := New_List;
5accd7b6 3231
541fb4d9
YM
3232 if Nkind (Parent (N)) = N_Compilation_Unit then
3233 Error_Msg_Name_1 := Nam;
3234 Error_Msg_N ("incorrect placement of aspect `%`", E);
3235 goto Continue;
3236 end if;
dac3bede 3237
541fb4d9
YM
3238 if Nkind (Expr) /= N_Aggregate then
3239 Error_Msg_Name_1 := Nam;
3240 Error_Msg_NE
3241 ("wrong syntax for aspect `%` for &", Id, E);
3242 goto Continue;
3243 end if;
dac3bede 3244
541fb4d9 3245 -- Make pragma expressions refer to the original aspect
33c9f9af
AC
3246 -- expressions through the Original_Node link. This is used
3247 -- in semantic analysis for ASIS mode, so that the original
3248 -- expression also gets analyzed.
541fb4d9
YM
3249
3250 Comp_Expr := First (Expressions (Expr));
3251 while Present (Comp_Expr) loop
3252 New_Expr := Relocate_Node (Comp_Expr);
541fb4d9
YM
3253 Append_To (Args,
3254 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
3255 Expression => New_Expr));
3256 Next (Comp_Expr);
3257 end loop;
3258
3259 Comp_Assn := First (Component_Associations (Expr));
3260 while Present (Comp_Assn) loop
3261 if List_Length (Choices (Comp_Assn)) /= 1
3262 or else
3263 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
3264 then
90e85233 3265 Error_Msg_Name_1 := Nam;
dac3bede 3266 Error_Msg_NE
90e85233 3267 ("wrong syntax for aspect `%` for &", Id, E);
dac3bede
YM
3268 goto Continue;
3269 end if;
3270
541fb4d9
YM
3271 Append_To (Args,
3272 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
c9d70ab1
AC
3273 Chars => Chars (First (Choices (Comp_Assn))),
3274 Expression =>
3275 Relocate_Node (Expression (Comp_Assn))));
541fb4d9
YM
3276 Next (Comp_Assn);
3277 end loop;
dac3bede 3278
541fb4d9 3279 -- Build the test-case pragma
dac3bede 3280
9d5598bf
AC
3281 Make_Aitem_Pragma
3282 (Pragma_Argument_Associations => Args,
3283 Pragma_Name => Nam);
541fb4d9 3284 end Test_Case;
dec6faf1 3285
9d5598bf
AC
3286 -- Contract_Cases
3287
5afe5d2d 3288 when Aspect_Contract_Cases =>
9d5598bf
AC
3289 Make_Aitem_Pragma
3290 (Pragma_Argument_Associations => New_List (
3291 Make_Pragma_Argument_Association (Loc,
3292 Expression => Relocate_Node (Expr))),
3293 Pragma_Name => Nam);
570104df 3294
4e6768ab
AC
3295 Decorate (Aspect, Aitem);
3296 Insert_Pragma (Aitem);
5afe5d2d 3297 goto Continue;
570104df 3298
b98e2969
AC
3299 -- Case 5: Special handling for aspects with an optional
3300 -- boolean argument.
dec6faf1 3301
c8593453 3302 -- In the delayed case, the corresponding pragma cannot be
9d5598bf
AC
3303 -- generated yet because the evaluation of the boolean needs
3304 -- to be delayed till the freeze point.
3305
b98e2969
AC
3306 when Boolean_Aspects |
3307 Library_Unit_Aspects =>
9d6e4157 3308
b98e2969 3309 Set_Is_Boolean_Aspect (Aspect);
9d6e4157 3310
b98e2969 3311 -- Lock_Free aspect only apply to protected objects
4169b895 3312
b98e2969
AC
3313 if A_Id = Aspect_Lock_Free then
3314 if Ekind (E) /= E_Protected_Type then
dc3af7e2 3315 Error_Msg_Name_1 := Nam;
9d6e4157 3316 Error_Msg_N
b98e2969
AC
3317 ("aspect % only applies to a protected object",
3318 Aspect);
3319
3320 else
3321 -- Set the Uses_Lock_Free flag to True if there is no
15e934bf 3322 -- expression or if the expression is True. The
b98e2969 3323 -- evaluation of this aspect should be delayed to the
15e934bf 3324 -- freeze point (why???)
b98e2969 3325
eefd2467
AC
3326 if No (Expr)
3327 or else Is_True (Static_Boolean (Expr))
b98e2969
AC
3328 then
3329 Set_Uses_Lock_Free (E);
3330 end if;
2a290fec
AC
3331
3332 Record_Rep_Item (E, Aspect);
9d6e4157 3333 end if;
4169b895 3334
b98e2969 3335 goto Continue;
0f1a6a0b 3336
2e885a6f
AC
3337 elsif A_Id = Aspect_Export or else A_Id = Aspect_Import then
3338 Analyze_Aspect_Export_Import;
c8593453
AC
3339
3340 -- Disable_Controlled
3341
3342 elsif A_Id = Aspect_Disable_Controlled then
3343 if Ekind (E) /= E_Record_Type
3344 or else not Is_Controlled (E)
3345 then
3346 Error_Msg_N
3347 ("aspect % requires controlled record type", Aspect);
3348 goto Continue;
3349 end if;
3350
e8c84c8f
AC
3351 -- If we're in a generic template, we don't want to try
3352 -- to disable controlled types, because typical usage is
3353 -- "Disable_Controlled => not <some_check>'Enabled", and
3354 -- the value of Enabled is not known until we see a
58ef3d30
ES
3355 -- particular instance. In such a context, we just need
3356 -- to preanalyze the expression for legality.
e8c84c8f
AC
3357
3358 if Expander_Active then
14f73211
BD
3359 Analyze_And_Resolve (Expr, Standard_Boolean);
3360
e8c84c8f
AC
3361 if not Present (Expr)
3362 or else Is_True (Static_Boolean (Expr))
3363 then
3364 Set_Disable_Controlled (E);
3365 end if;
58ef3d30
ES
3366
3367 elsif Serious_Errors_Detected = 0 then
3368 Preanalyze_And_Resolve (Expr, Standard_Boolean);
c8593453
AC
3369 end if;
3370
b98e2969
AC
3371 goto Continue;
3372 end if;
c159409f 3373
15e934bf
AC
3374 -- Library unit aspects require special handling in the case
3375 -- of a package declaration, the pragma needs to be inserted
3376 -- in the list of declarations for the associated package.
3377 -- There is no issue of visibility delay for these aspects.
a01b9df6 3378
b98e2969 3379 if A_Id in Library_Unit_Aspects
7271429c
AC
3380 and then
3381 Nkind_In (N, N_Package_Declaration,
3382 N_Generic_Package_Declaration)
b98e2969 3383 and then Nkind (Parent (N)) /= N_Compilation_Unit
4d1429b2
AC
3384
3385 -- Aspect is legal on a local instantiation of a library-
3386 -- level generic unit.
3387
c9f95e4c 3388 and then not Is_Generic_Instance (Defining_Entity (N))
b98e2969
AC
3389 then
3390 Error_Msg_N
8894aa20 3391 ("incorrect context for library unit aspect&", Id);
b98e2969
AC
3392 goto Continue;
3393 end if;
ba759acd 3394
33c9f9af
AC
3395 -- Cases where we do not delay, includes all cases where the
3396 -- expression is missing other than the above cases.
c159409f 3397
847d950d 3398 if not Delay_Required or else No (Expr) then
2e885a6f
AC
3399
3400 -- Exclude aspects Export and Import because their pragma
3401 -- syntax does not map directly to a Boolean aspect.
3402
3403 if A_Id /= Aspect_Export
3404 and then A_Id /= Aspect_Import
3405 then
3406 Make_Aitem_Pragma
3407 (Pragma_Argument_Associations => New_List (
3408 Make_Pragma_Argument_Association (Sloc (Ent),
3409 Expression => Ent)),
3410 Pragma_Name => Chars (Id));
3411 end if;
3412
b98e2969 3413 Delay_Required := False;
1c54829e 3414
b98e2969
AC
3415 -- In general cases, the corresponding pragma/attribute
3416 -- definition clause will be inserted later at the freezing
2f6f8285 3417 -- point, and we do not need to build it now.
1c54829e 3418
b98e2969
AC
3419 else
3420 Aitem := Empty;
3421 end if;
2cbac6c6
AC
3422
3423 -- Storage_Size
3424
3425 -- This is special because for access types we need to generate
3426 -- an attribute definition clause. This also works for single
3427 -- task declarations, but it does not work for task type
3428 -- declarations, because we have the case where the expression
3429 -- references a discriminant of the task type. That can't use
3430 -- an attribute definition clause because we would not have
3431 -- visibility on the discriminant. For that case we must
3432 -- generate a pragma in the task definition.
3433
3434 when Aspect_Storage_Size =>
3435
3436 -- Task type case
3437
3438 if Ekind (E) = E_Task_Type then
3439 declare
3440 Decl : constant Node_Id := Declaration_Node (E);
3441
3442 begin
3443 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
3444
3445 -- If no task definition, create one
3446
3447 if No (Task_Definition (Decl)) then
3448 Set_Task_Definition (Decl,
3449 Make_Task_Definition (Loc,
3450 Visible_Declarations => Empty_List,
3451 End_Label => Empty));
3452 end if;
3453
33c9f9af
AC
3454 -- Create a pragma and put it at the start of the task
3455 -- definition for the task type declaration.
2cbac6c6
AC
3456
3457 Make_Aitem_Pragma
3458 (Pragma_Argument_Associations => New_List (
3459 Make_Pragma_Argument_Association (Loc,
3460 Expression => Relocate_Node (Expr))),
3461 Pragma_Name => Name_Storage_Size);
3462
3463 Prepend
3464 (Aitem,
3465 Visible_Declarations (Task_Definition (Decl)));
3466 goto Continue;
3467 end;
3468
3469 -- All other cases, generate attribute definition
3470
3471 else
3472 Aitem :=
3473 Make_Attribute_Definition_Clause (Loc,
3474 Name => Ent,
3475 Chars => Chars (Id),
3476 Expression => Relocate_Node (Expr));
3477 end if;
b98e2969 3478 end case;
1c54829e 3479
b98e2969
AC
3480 -- Attach the corresponding pragma/attribute definition clause to
3481 -- the aspect specification node.
c159409f 3482
b98e2969 3483 if Present (Aitem) then
4e6768ab 3484 Set_From_Aspect_Specification (Aitem);
b98e2969 3485 end if;
ca5af305 3486
b98e2969 3487 -- In the context of a compilation unit, we directly put the
9d5598bf
AC
3488 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
3489 -- node (no delay is required here) except for aspects on a
33c9f9af
AC
3490 -- subprogram body (see below) and a generic package, for which we
3491 -- need to introduce the pragma before building the generic copy
3492 -- (see sem_ch12), and for package instantiations, where the
3493 -- library unit pragmas are better handled early.
1c54829e 3494
39af2bac 3495 if Nkind (Parent (N)) = N_Compilation_Unit
b98e2969
AC
3496 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
3497 then
3498 declare
3499 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2ef48385 3500
b98e2969
AC
3501 begin
3502 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2ef48385 3503
b98e2969
AC
3504 -- For a Boolean aspect, create the corresponding pragma if
3505 -- no expression or if the value is True.
2ef48385 3506
616547fa 3507 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
b98e2969 3508 if Is_True (Static_Boolean (Expr)) then
9d5598bf
AC
3509 Make_Aitem_Pragma
3510 (Pragma_Argument_Associations => New_List (
3511 Make_Pragma_Argument_Association (Sloc (Ent),
3512 Expression => Ent)),
3513 Pragma_Name => Chars (Id));
2ef48385 3514
b98e2969
AC
3515 Set_From_Aspect_Specification (Aitem, True);
3516 Set_Corresponding_Aspect (Aitem, Aspect);
3517
3518 else
3519 goto Continue;
3520 end if;
3521 end if;
2ef48385 3522
cf3b97ef
AC
3523 -- If the aspect is on a subprogram body (relevant aspect
3524 -- is Inline), add the pragma in front of the declarations.
473e20df
AC
3525
3526 if Nkind (N) = N_Subprogram_Body then
3527 if No (Declarations (N)) then
3528 Set_Declarations (N, New_List);
3529 end if;
3530
3531 Prepend (Aitem, Declarations (N));
3532
7271429c
AC
3533 elsif Nkind (N) = N_Generic_Package_Declaration then
3534 if No (Visible_Declarations (Specification (N))) then
3535 Set_Visible_Declarations (Specification (N), New_List);
3536 end if;
3537
3538 Prepend (Aitem,
3539 Visible_Declarations (Specification (N)));
3540
f5da7a97 3541 elsif Nkind (N) = N_Package_Instantiation then
31dd3f4b
ES
3542 declare
3543 Spec : constant Node_Id :=
3544 Specification (Instance_Spec (N));
3545 begin
3546 if No (Visible_Declarations (Spec)) then
3547 Set_Visible_Declarations (Spec, New_List);
3548 end if;
3549
3550 Prepend (Aitem, Visible_Declarations (Spec));
3551 end;
3552
473e20df
AC
3553 else
3554 if No (Pragmas_After (Aux)) then
6a04272a 3555 Set_Pragmas_After (Aux, New_List);
473e20df
AC
3556 end if;
3557
3558 Append (Aitem, Pragmas_After (Aux));
b98e2969 3559 end if;
2ef48385 3560
b98e2969
AC
3561 goto Continue;
3562 end;
3563 end if;
2ef48385 3564
b98e2969
AC
3565 -- The evaluation of the aspect is delayed to the freezing point.
3566 -- The pragma or attribute clause if there is one is then attached
15e934bf 3567 -- to the aspect specification which is put in the rep item list.
b8789727 3568
b98e2969
AC
3569 if Delay_Required then
3570 if Present (Aitem) then
3571 Set_Is_Delayed_Aspect (Aitem);
3572 Set_Aspect_Rep_Item (Aspect, Aitem);
3573 Set_Parent (Aitem, Aspect);
3574 end if;
b8789727 3575
b98e2969 3576 Set_Is_Delayed_Aspect (Aspect);
7b55fea6 3577
289a994b
AC
3578 -- In the case of Default_Value, link the aspect to base type
3579 -- as well, even though it appears on a first subtype. This is
3580 -- mandated by the semantics of the aspect. Do not establish
3581 -- the link when processing the base type itself as this leads
3582 -- to a rep item circularity. Verify that we are dealing with
3583 -- a scalar type to prevent cascaded errors.
3584
3585 if A_Id = Aspect_Default_Value
3586 and then Is_Scalar_Type (E)
3587 and then Base_Type (E) /= E
3588 then
7b55fea6
AC
3589 Set_Has_Delayed_Aspects (Base_Type (E));
3590 Record_Rep_Item (Base_Type (E), Aspect);
3591 end if;
3592
b98e2969
AC
3593 Set_Has_Delayed_Aspects (E);
3594 Record_Rep_Item (E, Aspect);
1c54829e 3595
80e59506
AC
3596 -- When delay is not required and the context is a package or a
3597 -- subprogram body, insert the pragma in the body declarations.
cdcf1c7a 3598
80e59506 3599 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
cdcf1c7a
AC
3600 if No (Declarations (N)) then
3601 Set_Declarations (N, New_List);
3602 end if;
3603
3604 -- The pragma is added before source declarations
3605
3606 Prepend_To (Declarations (N), Aitem);
3607
b98e2969
AC
3608 -- When delay is not required and the context is not a compilation
3609 -- unit, we simply insert the pragma/attribute definition clause
3610 -- in sequence.
1c54829e 3611
2e885a6f 3612 elsif Present (Aitem) then
b98e2969
AC
3613 Insert_After (Ins_Node, Aitem);
3614 Ins_Node := Aitem;
c159409f 3615 end if;
9d5598bf 3616 end Analyze_One_Aspect;
0f1a6a0b 3617
a01b9df6
AC
3618 <<Continue>>
3619 Next (Aspect);
eaba57fb 3620 end loop Aspect_Loop;
b98e2969
AC
3621
3622 if Has_Delayed_Aspects (E) then
3623 Ensure_Freeze_Node (E);
3624 end if;
eaba57fb 3625 end Analyze_Aspect_Specifications;
0f1a6a0b 3626
caf07df9
AC
3627 ---------------------------------------------------
3628 -- Analyze_Aspect_Specifications_On_Body_Or_Stub --
3629 ---------------------------------------------------
3630
3631 procedure Analyze_Aspect_Specifications_On_Body_Or_Stub (N : Node_Id) is
3632 Body_Id : constant Entity_Id := Defining_Entity (N);
3633
3634 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id);
f99ff327
AC
3635 -- Body [stub] N has aspects, but they are not properly placed. Emit an
3636 -- error message depending on the aspects involved. Spec_Id denotes the
3637 -- entity of the corresponding spec.
caf07df9
AC
3638
3639 --------------------------------
3640 -- Diagnose_Misplaced_Aspects --
3641 --------------------------------
3642
3643 procedure Diagnose_Misplaced_Aspects (Spec_Id : Entity_Id) is
3644 procedure Misplaced_Aspect_Error
3645 (Asp : Node_Id;
3646 Ref_Nam : Name_Id);
3647 -- Emit an error message concerning misplaced aspect Asp. Ref_Nam is
3648 -- the name of the refined version of the aspect.
3649
3650 ----------------------------
3651 -- Misplaced_Aspect_Error --
3652 ----------------------------
3653
3654 procedure Misplaced_Aspect_Error
3655 (Asp : Node_Id;
3656 Ref_Nam : Name_Id)
3657 is
3658 Asp_Nam : constant Name_Id := Chars (Identifier (Asp));
3659 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp_Nam);
3660
3661 begin
3662 -- The corresponding spec already contains the aspect in question
3663 -- and the one appearing on the body must be the refined form:
3664
3665 -- procedure P with Global ...;
3666 -- procedure P with Global ... is ... end P;
3667 -- ^
3668 -- Refined_Global
3669
3670 if Has_Aspect (Spec_Id, Asp_Id) then
3671 Error_Msg_Name_1 := Asp_Nam;
3672
3673 -- Subunits cannot carry aspects that apply to a subprogram
3674 -- declaration.
3675
3676 if Nkind (Parent (N)) = N_Subunit then
3677 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
3678
3679 -- Otherwise suggest the refined form
3680
3681 else
3682 Error_Msg_Name_2 := Ref_Nam;
3683 Error_Msg_N ("aspect % should be %", Asp);
3684 end if;
3685
3686 -- Otherwise the aspect must appear on the spec, not on the body
3687
3688 -- procedure P;
3689 -- procedure P with Global ... is ... end P;
3690
3691 else
3692 Error_Msg_N
f99ff327 3693 ("aspect specification must appear on initial declaration",
caf07df9
AC
3694 Asp);
3695 end if;
3696 end Misplaced_Aspect_Error;
3697
3698 -- Local variables
3699
3700 Asp : Node_Id;
3701 Asp_Nam : Name_Id;
3702
3703 -- Start of processing for Diagnose_Misplaced_Aspects
3704
3705 begin
3706 -- Iterate over the aspect specifications and emit specific errors
3707 -- where applicable.
3708
3709 Asp := First (Aspect_Specifications (N));
3710 while Present (Asp) loop
3711 Asp_Nam := Chars (Identifier (Asp));
3712
3713 -- Do not emit errors on aspects that can appear on a subprogram
3714 -- body. This scenario occurs when the aspect specification list
3715 -- contains both misplaced and properly placed aspects.
3716
3717 if Aspect_On_Body_Or_Stub_OK (Get_Aspect_Id (Asp_Nam)) then
3718 null;
3719
3720 -- Special diagnostics for SPARK aspects
3721
3722 elsif Asp_Nam = Name_Depends then
3723 Misplaced_Aspect_Error (Asp, Name_Refined_Depends);
3724
3725 elsif Asp_Nam = Name_Global then
3726 Misplaced_Aspect_Error (Asp, Name_Refined_Global);
3727
3728 elsif Asp_Nam = Name_Post then
3729 Misplaced_Aspect_Error (Asp, Name_Refined_Post);
3730
3731 -- Otherwise a language-defined aspect is misplaced
3732
3733 else
3734 Error_Msg_N
f99ff327 3735 ("aspect specification must appear on initial declaration",
caf07df9
AC
3736 Asp);
3737 end if;
3738
3739 Next (Asp);
3740 end loop;
3741 end Diagnose_Misplaced_Aspects;
3742
3743 -- Local variables
3744
f99ff327 3745 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
caf07df9
AC
3746
3747 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
3748
3749 begin
caf07df9
AC
3750 -- Language-defined aspects cannot be associated with a subprogram body
3751 -- [stub] if the subprogram has a spec. Certain implementation defined
3752 -- aspects are allowed to break this rule (for all applicable cases, see
3753 -- table Aspects.Aspect_On_Body_Or_Stub_OK).
3754
f99ff327 3755 if Spec_Id /= Body_Id and then not Aspects_On_Body_Or_Stub_OK (N) then
caf07df9
AC
3756 Diagnose_Misplaced_Aspects (Spec_Id);
3757 else
3758 Analyze_Aspect_Specifications (N, Body_Id);
3759 end if;
3760 end Analyze_Aspect_Specifications_On_Body_Or_Stub;
3761
996ae0b0
RK
3762 -----------------------
3763 -- Analyze_At_Clause --
3764 -----------------------
3765
3766 -- An at clause is replaced by the corresponding Address attribute
3767 -- definition clause that is the preferred approach in Ada 95.
3768
3769 procedure Analyze_At_Clause (N : Node_Id) is
45fc7ddb
HK
3770 CS : constant Boolean := Comes_From_Source (N);
3771
996ae0b0 3772 begin
45fc7ddb
HK
3773 -- This is an obsolescent feature
3774
5f3ab6fb
AC
3775 Check_Restriction (No_Obsolescent_Features, N);
3776
fbf5a39b
AC
3777 if Warn_On_Obsolescent_Feature then
3778 Error_Msg_N
a3633438 3779 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
fbf5a39b 3780 Error_Msg_N
a3633438 3781 ("\?j?use address attribute definition clause instead", N);
fbf5a39b
AC
3782 end if;
3783
45fc7ddb
HK
3784 -- Rewrite as address clause
3785
996ae0b0
RK
3786 Rewrite (N,
3787 Make_Attribute_Definition_Clause (Sloc (N),
c5d00db0
AC
3788 Name => Identifier (N),
3789 Chars => Name_Address,
996ae0b0 3790 Expression => Expression (N)));
45fc7ddb 3791
29ba9f52
RD
3792 -- We preserve Comes_From_Source, since logically the clause still comes
3793 -- from the source program even though it is changed in form.
45fc7ddb
HK
3794
3795 Set_Comes_From_Source (N, CS);
3796
3797 -- Analyze rewritten clause
3798
996ae0b0
RK
3799 Analyze_Attribute_Definition_Clause (N);
3800 end Analyze_At_Clause;
3801
3802 -----------------------------------------
3803 -- Analyze_Attribute_Definition_Clause --
3804 -----------------------------------------
3805
3806 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3807 Loc : constant Source_Ptr := Sloc (N);
3808 Nam : constant Node_Id := Name (N);
3809 Attr : constant Name_Id := Chars (N);
3810 Expr : constant Node_Id := Expression (N);
3811 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
a01b9df6
AC
3812
3813 Ent : Entity_Id;
3814 -- The entity of Nam after it is analyzed. In the case of an incomplete
3815 -- type, this is the underlying type.
3816
996ae0b0 3817 U_Ent : Entity_Id;
a01b9df6
AC
3818 -- The underlying entity to which the attribute applies. Generally this
3819 -- is the Underlying_Type of Ent, except in the case where the clause
3820 -- applies to full view of incomplete type or private type in which case
3821 -- U_Ent is just a copy of Ent.
996ae0b0
RK
3822
3823 FOnly : Boolean := False;
3824 -- Reset to True for subtype specific attribute (Alignment, Size)
33c9f9af
AC
3825 -- and for stream attributes, i.e. those cases where in the call to
3826 -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules
3827 -- are checked. Note that the case of stream attributes is not clear
3828 -- from the RM, but see AI95-00137. Also, the RM seems to disallow
3829 -- Storage_Size for derived task types, but that is also clearly
3830 -- unintentional.
996ae0b0 3831
edd63e9b
ES
3832 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3833 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3834 -- definition clauses.
3835
0f1a6a0b
AC
3836 function Duplicate_Clause return Boolean;
3837 -- This routine checks if the aspect for U_Ent being given by attribute
3838 -- definition clause N is for an aspect that has already been specified,
3839 -- and if so gives an error message. If there is a duplicate, True is
3840 -- returned, otherwise if there is no error, False is returned.
3841
d50f4827
AC
3842 procedure Check_Indexing_Functions;
3843 -- Check that the function in Constant_Indexing or Variable_Indexing
3844 -- attribute has the proper type structure. If the name is overloaded,
2a7b8e18 3845 -- check that some interpretation is legal.
d50f4827 3846
d941cee6
AC
3847 procedure Check_Iterator_Functions;
3848 -- Check that there is a single function in Default_Iterator attribute
d986066d 3849 -- has the proper type structure.
d941cee6
AC
3850
3851 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
cb25faf8 3852 -- Common legality check for the previous two
d941cee6 3853
45fc7ddb
HK
3854 -----------------------------------
3855 -- Analyze_Stream_TSS_Definition --
3856 -----------------------------------
3857
edd63e9b
ES
3858 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3859 Subp : Entity_Id := Empty;
3860 I : Interp_Index;
3861 It : Interp;
3862 Pnam : Entity_Id;
3863
3864 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
876f1624 3865 -- True for Read attribute, False for other attributes
edd63e9b 3866
87feba05
AC
3867 function Has_Good_Profile
3868 (Subp : Entity_Id;
3869 Report : Boolean := False) return Boolean;
edd63e9b 3870 -- Return true if the entity is a subprogram with an appropriate
876f1624
AC
3871 -- profile for the attribute being defined. If result is False and
3872 -- Report is True, function emits appropriate error.
edd63e9b
ES
3873
3874 ----------------------
3875 -- Has_Good_Profile --
3876 ----------------------
3877
87feba05
AC
3878 function Has_Good_Profile
3879 (Subp : Entity_Id;
3880 Report : Boolean := False) return Boolean
3881 is
edd63e9b
ES
3882 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3883 (False => E_Procedure, True => E_Function);
77039fe2
AC
3884 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3885 F : Entity_Id;
edd63e9b
ES
3886 Typ : Entity_Id;
3887
3888 begin
3889 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3890 return False;
3891 end if;
3892
3893 F := First_Formal (Subp);
3894
3895 if No (F)
3896 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3897 or else Designated_Type (Etype (F)) /=
77039fe2 3898 Class_Wide_Type (RTE (RE_Root_Stream_Type))
edd63e9b
ES
3899 then
3900 return False;
3901 end if;
3902
3903 if not Is_Function then
3904 Next_Formal (F);
3905
3906 declare
3907 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3908 (False => E_In_Parameter,
3909 True => E_Out_Parameter);
3910 begin
3911 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3912 return False;
3913 end if;
3914 end;
3915
3916 Typ := Etype (F);
3917
220d1fd9 3918 -- If the attribute specification comes from an aspect
33c9f9af
AC
3919 -- specification for a class-wide stream, the parameter must be
3920 -- a class-wide type of the entity to which the aspect applies.
220d1fd9
AC
3921
3922 if From_Aspect_Specification (N)
3923 and then Class_Present (Parent (N))
3924 and then Is_Class_Wide_Type (Typ)
3925 then
3926 Typ := Etype (Typ);
3927 end if;
3928
edd63e9b
ES
3929 else
3930 Typ := Etype (Subp);
3931 end if;
3932
33c9f9af 3933 -- Verify that the prefix of the attribute and the local name for
d18b1548
AC
3934 -- the type of the formal match, or one is the class-wide of the
3935 -- other, in the case of a class-wide stream operation.
dda38714 3936
9fe696a3 3937 if Base_Type (Typ) = Base_Type (Ent)
d18b1548 3938 or else (Is_Class_Wide_Type (Typ)
3fbbbd1e 3939 and then Typ = Class_Wide_Type (Base_Type (Ent)))
72eaa365
AC
3940 or else (Is_Class_Wide_Type (Ent)
3941 and then Ent = Class_Wide_Type (Base_Type (Typ)))
d18b1548
AC
3942 then
3943 null;
3944 else
3945 return False;
3946 end if;
3947
77039fe2 3948 if Present (Next_Formal (F)) then
dda38714
AC
3949 return False;
3950
3951 elsif not Is_Scalar_Type (Typ)
3952 and then not Is_First_Subtype (Typ)
3953 and then not Is_Class_Wide_Type (Typ)
3954 then
87feba05
AC
3955 if Report and not Is_First_Subtype (Typ) then
3956 Error_Msg_N
876f1624
AC
3957 ("subtype of formal in stream operation must be a first "
3958 & "subtype", Parameter_Type (Parent (F)));
87feba05
AC
3959 end if;
3960
dda38714
AC
3961 return False;
3962
3963 else
3964 return True;
3965 end if;
edd63e9b
ES
3966 end Has_Good_Profile;
3967
3968 -- Start of processing for Analyze_Stream_TSS_Definition
3969
3970 begin
3971 FOnly := True;
3972
3973 if not Is_Type (U_Ent) then
3974 Error_Msg_N ("local name must be a subtype", Nam);
3975 return;
dda38714
AC
3976
3977 elsif not Is_First_Subtype (U_Ent) then
3978 Error_Msg_N ("local name must be a first subtype", Nam);
3979 return;
edd63e9b
ES
3980 end if;
3981
3982 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3983
affbee12
RD
3984 -- If Pnam is present, it can be either inherited from an ancestor
3985 -- type (in which case it is legal to redefine it for this type), or
3986 -- be a previous definition of the attribute for the same type (in
3987 -- which case it is illegal).
3988
3989 -- In the first case, it will have been analyzed already, and we
3990 -- can check that its profile does not match the expected profile
3991 -- for a stream attribute of U_Ent. In the second case, either Pnam
3992 -- has been analyzed (and has the expected profile), or it has not
3993 -- been analyzed yet (case of a type that has not been frozen yet
3994 -- and for which the stream attribute has been set using Set_TSS).
3995
3996 if Present (Pnam)
3997 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3998 then
edd63e9b
ES
3999 Error_Msg_Sloc := Sloc (Pnam);
4000 Error_Msg_Name_1 := Attr;
4001 Error_Msg_N ("% attribute already defined #", Nam);
4002 return;
4003 end if;
4004
4005 Analyze (Expr);
4006
4007 if Is_Entity_Name (Expr) then
4008 if not Is_Overloaded (Expr) then
87feba05 4009 if Has_Good_Profile (Entity (Expr), Report => True) then
edd63e9b
ES
4010 Subp := Entity (Expr);
4011 end if;
4012
4013 else
4014 Get_First_Interp (Expr, I, It);
edd63e9b
ES
4015 while Present (It.Nam) loop
4016 if Has_Good_Profile (It.Nam) then
4017 Subp := It.Nam;
4018 exit;
4019 end if;
4020
4021 Get_Next_Interp (I, It);
4022 end loop;
4023 end if;
4024 end if;
4025
4026 if Present (Subp) then
a9a5b8ac 4027 if Is_Abstract_Subprogram (Subp) then
edd63e9b
ES
4028 Error_Msg_N ("stream subprogram must not be abstract", Expr);
4029 return;
3e65bfab 4030
5c5e108f 4031 -- A stream subprogram for an interface type must be a null
6dc87f5f
AC
4032 -- procedure (RM 13.13.2 (38/3)). Note that the class-wide type
4033 -- of an interface is not an interface type (3.9.4 (6.b/2)).
3e65bfab
AC
4034
4035 elsif Is_Interface (U_Ent)
d18b1548 4036 and then not Is_Class_Wide_Type (U_Ent)
3e65bfab 4037 and then not Inside_A_Generic
3e65bfab 4038 and then
d18b1548
AC
4039 (Ekind (Subp) = E_Function
4040 or else
4041 not Null_Present
3fbbbd1e
AC
4042 (Specification
4043 (Unit_Declaration_Node (Ultimate_Alias (Subp)))))
3e65bfab
AC
4044 then
4045 Error_Msg_N
77039fe2
AC
4046 ("stream subprogram for interface type must be null "
4047 & "procedure", Expr);
edd63e9b
ES
4048 end if;
4049
4050 Set_Entity (Expr, Subp);
4051 Set_Etype (Expr, Etype (Subp));
4052
affbee12 4053 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
edd63e9b
ES
4054
4055 else
4056 Error_Msg_Name_1 := Attr;
4057 Error_Msg_N ("incorrect expression for% attribute", Expr);
4058 end if;
4059 end Analyze_Stream_TSS_Definition;
4060
d50f4827
AC
4061 ------------------------------
4062 -- Check_Indexing_Functions --
4063 ------------------------------
4064
4065 procedure Check_Indexing_Functions is
7b536495 4066 Indexing_Found : Boolean := False;
d986066d 4067
8c14315a
ES
4068 procedure Check_Inherited_Indexing;
4069 -- For a derived type, check that no indexing aspect is specified
4070 -- for the type if it is also inherited
4071
d50f4827 4072 procedure Check_One_Function (Subp : Entity_Id);
d7a93e45
AC
4073 -- Check one possible interpretation. Sets Indexing_Found True if a
4074 -- legal indexing function is found.
d50f4827 4075
f3296dd3
AC
4076 procedure Illegal_Indexing (Msg : String);
4077 -- Diagnose illegal indexing function if not overloaded. In the
4078 -- overloaded case indicate that no legal interpretation exists.
4079
8c14315a
ES
4080 ------------------------------
4081 -- Check_Inherited_Indexing --
4082 ------------------------------
4083
4084 procedure Check_Inherited_Indexing is
4085 Inherited : Node_Id;
4086
4087 begin
4088 if Attr = Name_Constant_Indexing then
4089 Inherited :=
4090 Find_Aspect (Etype (Ent), Aspect_Constant_Indexing);
4091 else pragma Assert (Attr = Name_Variable_Indexing);
4092 Inherited :=
4093 Find_Aspect (Etype (Ent), Aspect_Variable_Indexing);
4094 end if;
4095
4096 if Present (Inherited) then
4097 if Debug_Flag_Dot_XX then
4098 null;
4099
078b1a5f
AC
4100 -- OK if current attribute_definition_clause is expansion of
4101 -- inherited aspect.
8c14315a
ES
4102
4103 elsif Aspect_Rep_Item (Inherited) = N then
4104 null;
4105
078b1a5f
AC
4106 -- Indicate the operation that must be overridden, rather than
4107 -- redefining the indexing aspect.
8c14315a
ES
4108
4109 else
4110 Illegal_Indexing
a9fdbccb 4111 ("indexing function already inherited from parent type");
8c14315a
ES
4112 Error_Msg_NE
4113 ("!override & instead",
4114 N, Entity (Expression (Inherited)));
4115 end if;
4116 end if;
4117 end Check_Inherited_Indexing;
4118
d50f4827
AC
4119 ------------------------
4120 -- Check_One_Function --
4121 ------------------------
4122
4123 procedure Check_One_Function (Subp : Entity_Id) is
f3296dd3
AC
4124 Default_Element : Node_Id;
4125 Ret_Type : constant Entity_Id := Etype (Subp);
76d49f49 4126
d50f4827 4127 begin
f3296dd3
AC
4128 if not Is_Overloadable (Subp) then
4129 Illegal_Indexing ("illegal indexing function for type&");
4130 return;
4131
d7a93e45
AC
4132 elsif Scope (Subp) /= Scope (Ent) then
4133 if Nkind (Expr) = N_Expanded_Name then
4134
4135 -- Indexing function can't be declared elsewhere
4136
4137 Illegal_Indexing
4138 ("indexing function must be declared in scope of type&");
4139 end if;
4140
f3296dd3
AC
4141 return;
4142
4143 elsif No (First_Formal (Subp)) then
4144 Illegal_Indexing
4145 ("Indexing requires a function that applies to type&");
4146 return;
4147
4148 elsif No (Next_Formal (First_Formal (Subp))) then
4149 Illegal_Indexing
eb9008b7 4150 ("indexing function must have at least two parameters");
f3296dd3
AC
4151 return;
4152
4153 elsif Is_Derived_Type (Ent) then
8c14315a 4154 Check_Inherited_Indexing;
f3296dd3
AC
4155 end if;
4156
eefd2467 4157 if not Check_Primitive_Function (Subp) then
f3296dd3
AC
4158 Illegal_Indexing
4159 ("Indexing aspect requires a function that applies to type&");
4160 return;
d50f4827
AC
4161 end if;
4162
d7a93e45
AC
4163 -- If partial declaration exists, verify that it is not tagged.
4164
4165 if Ekind (Current_Scope) = E_Package
4166 and then Has_Private_Declaration (Ent)
4167 and then From_Aspect_Specification (N)
fd7215d7
AC
4168 and then
4169 List_Containing (Parent (Ent)) =
4170 Private_Declarations
d7a93e45
AC
4171 (Specification (Unit_Declaration_Node (Current_Scope)))
4172 and then Nkind (N) = N_Attribute_Definition_Clause
4173 then
4174 declare
4175 Decl : Node_Id;
4176
4177 begin
4178 Decl :=
4179 First (Visible_Declarations
fd7215d7
AC
4180 (Specification
4181 (Unit_Declaration_Node (Current_Scope))));
d7a93e45
AC
4182
4183 while Present (Decl) loop
4184 if Nkind (Decl) = N_Private_Type_Declaration
4185 and then Ent = Full_View (Defining_Identifier (Decl))
4186 and then Tagged_Present (Decl)
4187 and then No (Aspect_Specifications (Decl))
4188 then
4189 Illegal_Indexing
4190 ("Indexing aspect cannot be specified on full view "
fd7215d7 4191 & "if partial view is tagged");
d7a93e45
AC
4192 return;
4193 end if;
4194
4195 Next (Decl);
4196 end loop;
4197 end;
4198 end if;
4199
76d49f49 4200 -- An indexing function must return either the default element of
2a7b8e18 4201 -- the container, or a reference type. For variable indexing it
9a6dc470 4202 -- must be the latter.
76d49f49 4203
f3296dd3
AC
4204 Default_Element :=
4205 Find_Value_Of_Aspect
4206 (Etype (First_Formal (Subp)), Aspect_Iterator_Element);
4207
76d49f49
ES
4208 if Present (Default_Element) then
4209 Analyze (Default_Element);
9a6dc470 4210
76d49f49 4211 if Is_Entity_Name (Default_Element)
f3296dd3
AC
4212 and then not Covers (Entity (Default_Element), Ret_Type)
4213 and then False
76d49f49 4214 then
f3296dd3
AC
4215 Illegal_Indexing
4216 ("wrong return type for indexing function");
76d49f49
ES
4217 return;
4218 end if;
4219 end if;
4220
9a6dc470 4221 -- For variable_indexing the return type must be a reference type
76d49f49 4222
f3296dd3
AC
4223 if Attr = Name_Variable_Indexing then
4224 if not Has_Implicit_Dereference (Ret_Type) then
4225 Illegal_Indexing
4226 ("variable indexing must return a reference type");
4227 return;
4228
35e7063a
AC
4229 elsif Is_Access_Constant
4230 (Etype (First_Discriminant (Ret_Type)))
f3296dd3
AC
4231 then
4232 Illegal_Indexing
4233 ("variable indexing must return an access to variable");
4234 return;
4235 end if;
2a7b8e18
AC
4236
4237 else
f3296dd3
AC
4238 if Has_Implicit_Dereference (Ret_Type)
4239 and then not
4240 Is_Access_Constant (Etype (First_Discriminant (Ret_Type)))
4241 then
4242 Illegal_Indexing
4243 ("constant indexing must return an access to constant");
4244 return;
4245
4246 elsif Is_Access_Type (Etype (First_Formal (Subp)))
4247 and then not Is_Access_Constant (Etype (First_Formal (Subp)))
4248 then
4249 Illegal_Indexing
4250 ("constant indexing must apply to an access to constant");
4251 return;
4252 end if;
d50f4827 4253 end if;
f3296dd3
AC
4254
4255 -- All checks succeeded.
4256
4257 Indexing_Found := True;
d50f4827
AC
4258 end Check_One_Function;
4259
f3296dd3
AC
4260 -----------------------
4261 -- Illegal_Indexing --
4262 -----------------------
4263
4264 procedure Illegal_Indexing (Msg : String) is
4265 begin
d7a93e45 4266 Error_Msg_NE (Msg, N, Ent);
f3296dd3
AC
4267 end Illegal_Indexing;
4268
d50f4827
AC
4269 -- Start of processing for Check_Indexing_Functions
4270
4271 begin
d941cee6 4272 if In_Instance then
8c14315a 4273 Check_Inherited_Indexing;
d941cee6
AC
4274 end if;
4275
d50f4827
AC
4276 Analyze (Expr);
4277
4278 if not Is_Overloaded (Expr) then
4279 Check_One_Function (Entity (Expr));
4280
4281 else
4282 declare
b26f70a0 4283 I : Interp_Index;
d50f4827
AC
4284 It : Interp;
4285
4286 begin
2a7b8e18 4287 Indexing_Found := False;
d50f4827
AC
4288 Get_First_Interp (Expr, I, It);
4289 while Present (It.Nam) loop
4290
4291 -- Note that analysis will have added the interpretation
4292 -- that corresponds to the dereference. We only check the
4293 -- subprogram itself.
4294
4295 if Is_Overloadable (It.Nam) then
4296 Check_One_Function (It.Nam);
4297 end if;
4298
4299 Get_Next_Interp (I, It);
4300 end loop;
4301 end;
4302 end if;
d7a93e45 4303
fd7215d7 4304 if not Indexing_Found and then not Error_Posted (N) then
d7a93e45
AC
4305 Error_Msg_NE
4306 ("aspect Indexing requires a local function that "
4307 & "applies to type&", Expr, Ent);
4308 end if;
d50f4827
AC
4309 end Check_Indexing_Functions;
4310
d941cee6
AC
4311 ------------------------------
4312 -- Check_Iterator_Functions --
4313 ------------------------------
4314
4315 procedure Check_Iterator_Functions is
d941cee6 4316 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
d986066d 4317 -- Check one possible interpretation for validity
d941cee6
AC
4318
4319 ----------------------------
4320 -- Valid_Default_Iterator --
4321 ----------------------------
4322
4323 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
437244c7 4324 Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp)));
3702225c 4325 Formal : Entity_Id;
d941cee6
AC
4326
4327 begin
4328 if not Check_Primitive_Function (Subp) then
4329 return False;
437244c7
AC
4330
4331 -- The return type must be derived from a type in an instance
4332 -- of Iterator.Interfaces, and thus its root type must have a
4333 -- predefined name.
4334
4335 elsif Chars (Root_T) /= Name_Forward_Iterator
4336 and then Chars (Root_T) /= Name_Reversible_Iterator
4337 then
4338 return False;
4339
d941cee6
AC
4340 else
4341 Formal := First_Formal (Subp);
4342 end if;
4343
d986066d 4344 -- False if any subsequent formal has no default expression
d941cee6 4345
d986066d
AC
4346 Formal := Next_Formal (Formal);
4347 while Present (Formal) loop
4348 if No (Expression (Parent (Formal))) then
4349 return False;
4350 end if;
d941cee6 4351
d986066d
AC
4352 Next_Formal (Formal);
4353 end loop;
d941cee6 4354
d986066d 4355 -- True if all subsequent formals have default expressions
d941cee6
AC
4356
4357 return True;
4358 end Valid_Default_Iterator;
4359
4360 -- Start of processing for Check_Iterator_Functions
4361
4362 begin
4363 Analyze (Expr);
4364
4365 if not Is_Entity_Name (Expr) then
4366 Error_Msg_N ("aspect Iterator must be a function name", Expr);
4367 end if;
4368
4369 if not Is_Overloaded (Expr) then
4370 if not Check_Primitive_Function (Entity (Expr)) then
4371 Error_Msg_NE
4372 ("aspect Indexing requires a function that applies to type&",
4373 Entity (Expr), Ent);
4374 end if;
4375
4f2cae4a
ES
4376 -- Flag the default_iterator as well as the denoted function.
4377
d941cee6 4378 if not Valid_Default_Iterator (Entity (Expr)) then
4f2cae4a 4379 Error_Msg_N ("improper function for default iterator!", Expr);
d941cee6
AC
4380 end if;
4381
4382 else
d941cee6 4383 declare
02886c2e 4384 Default : Entity_Id := Empty;
77237288
AC
4385 I : Interp_Index;
4386 It : Interp;
d941cee6
AC
4387
4388 begin
4389 Get_First_Interp (Expr, I, It);
4390 while Present (It.Nam) loop
4391 if not Check_Primitive_Function (It.Nam)
57a8057a 4392 or else not Valid_Default_Iterator (It.Nam)
d941cee6
AC
4393 then
4394 Remove_Interp (I);
4395
4396 elsif Present (Default) then
d941cee6 4397
77237288
AC
4398 -- An explicit one should override an implicit one
4399
4400 if Comes_From_Source (Default) =
4401 Comes_From_Source (It.Nam)
4402 then
4403 Error_Msg_N ("default iterator must be unique", Expr);
4404 Error_Msg_Sloc := Sloc (Default);
4405 Error_Msg_N ("\\possible interpretation#", Expr);
4406 Error_Msg_Sloc := Sloc (It.Nam);
4407 Error_Msg_N ("\\possible interpretation#", Expr);
4408
4409 elsif Comes_From_Source (It.Nam) then
4410 Default := It.Nam;
4411 end if;
d941cee6
AC
4412 else
4413 Default := It.Nam;
4414 end if;
4415
4416 Get_Next_Interp (I, It);
4417 end loop;
d941cee6 4418
02886c2e
AC
4419 if Present (Default) then
4420 Set_Entity (Expr, Default);
4421 Set_Is_Overloaded (Expr, False);
437244c7
AC
4422 else
4423 Error_Msg_N
3702225c 4424 ("no interpretation is a valid default iterator!", Expr);
02886c2e
AC
4425 end if;
4426 end;
d941cee6
AC
4427 end if;
4428 end Check_Iterator_Functions;
4429
4430 -------------------------------
4431 -- Check_Primitive_Function --
4432 -------------------------------
4433
4434 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
4435 Ctrl : Entity_Id;
4436
4437 begin
4438 if Ekind (Subp) /= E_Function then
4439 return False;
4440 end if;
4441
4442 if No (First_Formal (Subp)) then
4443 return False;
4444 else
4445 Ctrl := Etype (First_Formal (Subp));
4446 end if;
4447
4f2cae4a
ES
4448 -- To be a primitive operation subprogram has to be in same scope.
4449
4450 if Scope (Ctrl) /= Scope (Subp) then
4451 return False;
4452 end if;
4453
3e1862b1
ES
4454 -- Type of formal may be the class-wide type, an access to such,
4455 -- or an incomplete view.
4456
d941cee6
AC
4457 if Ctrl = Ent
4458 or else Ctrl = Class_Wide_Type (Ent)
4459 or else
4460 (Ekind (Ctrl) = E_Anonymous_Access_Type
2e215573
RD
4461 and then (Designated_Type (Ctrl) = Ent
4462 or else
4463 Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3e1862b1
ES
4464 or else
4465 (Ekind (Ctrl) = E_Incomplete_Type
4466 and then Full_View (Ctrl) = Ent)
d941cee6
AC
4467 then
4468 null;
d941cee6
AC
4469 else
4470 return False;
4471 end if;
4472
4473 return True;
4474 end Check_Primitive_Function;
4475
0f1a6a0b
AC
4476 ----------------------
4477 -- Duplicate_Clause --
4478 ----------------------
4479
4480 function Duplicate_Clause return Boolean is
c159409f 4481 A : Node_Id;
0f1a6a0b
AC
4482
4483 begin
1928f450
AC
4484 -- Nothing to do if this attribute definition clause comes from
4485 -- an aspect specification, since we could not be duplicating an
0f1a6a0b
AC
4486 -- explicit clause, and we dealt with the case of duplicated aspects
4487 -- in Analyze_Aspect_Specifications.
4488
4489 if From_Aspect_Specification (N) then
4490 return False;
4491 end if;
4492
b98e2969
AC
4493 -- Otherwise current clause may duplicate previous clause, or a
4494 -- previously given pragma or aspect specification for the same
4495 -- aspect.
c159409f 4496
34f3a701 4497 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
0f1a6a0b
AC
4498
4499 if Present (A) then
b98e2969
AC
4500 Error_Msg_Name_1 := Chars (N);
4501 Error_Msg_Sloc := Sloc (A);
4502
34f3a701 4503 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
b98e2969 4504 return True;
0f1a6a0b
AC
4505 end if;
4506
4507 return False;
4508 end Duplicate_Clause;
4509
edd63e9b
ES
4510 -- Start of processing for Analyze_Attribute_Definition_Clause
4511
996ae0b0 4512 begin
a01b9df6 4513 -- The following code is a defense against recursion. Not clear that
33c9f9af
AC
4514 -- this can happen legitimately, but perhaps some error situations can
4515 -- cause it, and we did see this recursion during testing.
a01b9df6
AC
4516
4517 if Analyzed (N) then
4518 return;
4519 else
4520 Set_Analyzed (N, True);
4521 end if;
4522
58ba2415
HK
4523 Check_Restriction_No_Use_Of_Attribute (N);
4524
5ebfaacf
AC
4525 -- Ignore some selected attributes in CodePeer mode since they are not
4526 -- relevant in this context.
4527
4528 if CodePeer_Mode then
4529 case Id is
4530
4531 -- Ignore Component_Size in CodePeer mode, to avoid changing the
4532 -- internal representation of types by implicitly packing them.
4533
4534 when Attribute_Component_Size =>
4535 Rewrite (N, Make_Null_Statement (Sloc (N)));
4536 return;
4537
4538 when others =>
4539 null;
4540 end case;
4541 end if;
4542
1c163178 4543 -- Process Ignore_Rep_Clauses option
55c078ac 4544
1c163178 4545 if Ignore_Rep_Clauses then
70b70ce8
AC
4546 case Id is
4547
55c078ac
AC
4548 -- The following should be ignored. They do not affect legality
4549 -- and may be target dependent. The basic idea of -gnatI is to
4550 -- ignore any rep clauses that may be target dependent but do not
4551 -- affect legality (except possibly to be rejected because they
4552 -- are incompatible with the compilation target).
70b70ce8 4553
9d0c3761 4554 when Attribute_Alignment |
70b70ce8
AC
4555 Attribute_Bit_Order |
4556 Attribute_Component_Size |
4557 Attribute_Machine_Radix |
4558 Attribute_Object_Size |
4559 Attribute_Size |
cf28c974 4560 Attribute_Small |
70b70ce8
AC
4561 Attribute_Stream_Size |
4562 Attribute_Value_Size =>
cf28c974 4563 Kill_Rep_Clause (N);
70b70ce8
AC
4564 return;
4565
55c078ac 4566 -- The following should not be ignored, because in the first place
33c9f9af
AC
4567 -- they are reasonably portable, and should not cause problems
4568 -- in compiling code from another target, and also they do affect
4569 -- legality, e.g. failing to provide a stream attribute for a type
4570 -- may make a program illegal.
70b70ce8 4571
a8551b5f
AC
4572 when Attribute_External_Tag |
4573 Attribute_Input |
4574 Attribute_Output |
4575 Attribute_Read |
4576 Attribute_Simple_Storage_Pool |
4577 Attribute_Storage_Pool |
4578 Attribute_Storage_Size |
4579 Attribute_Write =>
70b70ce8
AC
4580 null;
4581
cf28c974
RD
4582 -- We do not do anything here with address clauses, they will be
4583 -- removed by Freeze later on, but for now, it works better to
4584 -- keep then in the tree.
4585
4586 when Attribute_Address =>
4587 null;
4588
b91fccb3
BD
4589 -- Other cases are errors ("attribute& cannot be set with
4590 -- definition clause"), which will be caught below.
70b70ce8
AC
4591
4592 when others =>
4593 null;
4594 end case;
c690a2ec
RD
4595 end if;
4596
996ae0b0
RK
4597 Analyze (Nam);
4598 Ent := Entity (Nam);
4599
4600 if Rep_Item_Too_Early (Ent, N) then
4601 return;
4602 end if;
4603
edd63e9b
ES
4604 -- Rep clause applies to full view of incomplete type or private type if
4605 -- we have one (if not, this is a premature use of the type). However,
4606 -- certain semantic checks need to be done on the specified entity (i.e.
4607 -- the private view), so we save it in Ent.
996ae0b0
RK
4608
4609 if Is_Private_Type (Ent)
4610 and then Is_Derived_Type (Ent)
4611 and then not Is_Tagged_Type (Ent)
4612 and then No (Full_View (Ent))
4613 then
edd63e9b
ES
4614 -- If this is a private type whose completion is a derivation from
4615 -- another private type, there is no full view, and the attribute
4616 -- belongs to the type itself, not its underlying parent.
996ae0b0
RK
4617
4618 U_Ent := Ent;
4619
4620 elsif Ekind (Ent) = E_Incomplete_Type then
b7e429ab 4621
edd63e9b
ES
4622 -- The attribute applies to the full view, set the entity of the
4623 -- attribute definition accordingly.
b7e429ab 4624
996ae0b0
RK
4625 Ent := Underlying_Type (Ent);
4626 U_Ent := Ent;
b7e429ab
AC
4627 Set_Entity (Nam, Ent);
4628
996ae0b0
RK
4629 else
4630 U_Ent := Underlying_Type (Ent);
4631 end if;
4632
54c04d6c 4633 -- Avoid cascaded error
996ae0b0
RK
4634
4635 if Etype (Nam) = Any_Type then
4636 return;
4637
b98e2969 4638 -- Must be declared in current scope or in case of an aspect
5eeeed5e 4639 -- specification, must be visible in current scope.
54c04d6c 4640
b98e2969 4641 elsif Scope (Ent) /= Current_Scope
5eeeed5e
AC
4642 and then
4643 not (From_Aspect_Specification (N)
4644 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
b98e2969 4645 then
996ae0b0
RK
4646 Error_Msg_N ("entity must be declared in this scope", Nam);
4647 return;
4648
54c04d6c
AC
4649 -- Must not be a source renaming (we do have some cases where the
4650 -- expander generates a renaming, and those cases are OK, in such
51e641f8 4651 -- cases any attribute applies to the renamed object as well).
54c04d6c
AC
4652
4653 elsif Is_Object (Ent)
4654 and then Present (Renamed_Object (Ent))
54c04d6c 4655 then
51e641f8
AC
4656 -- Case of renamed object from source, this is an error
4657
4658 if Comes_From_Source (Renamed_Object (Ent)) then
4659 Get_Name_String (Chars (N));
4660 Error_Msg_Strlen := Name_Len;
4661 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
4662 Error_Msg_N
4663 ("~ clause not allowed for a renaming declaration "
4664 & "(RM 13.1(6))", Nam);
4665 return;
4666
4667 -- For the case of a compiler generated renaming, the attribute
4668 -- definition clause applies to the renamed object created by the
4669 -- expander. The easiest general way to handle this is to create a
4670 -- copy of the attribute definition clause for this object.
4671
81bd8c90 4672 elsif Is_Entity_Name (Renamed_Object (Ent)) then
51e641f8
AC
4673 Insert_Action (N,
4674 Make_Attribute_Definition_Clause (Loc,
4675 Name =>
4676 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
4677 Chars => Chars (N),
4678 Expression => Duplicate_Subexpr (Expression (N))));
81bd8c90
AC
4679
4680 -- If the renamed object is not an entity, it must be a dereference
4681 -- of an unconstrained function call, and we must introduce a new
4682 -- declaration to capture the expression. This is needed in the case
4683 -- of 'Alignment, where the original declaration must be rewritten.
4684
4685 else
4686 pragma Assert
4687 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
4688 null;
51e641f8 4689 end if;
54c04d6c
AC
4690
4691 -- If no underlying entity, use entity itself, applies to some
4692 -- previously detected error cases ???
4693
07fc65c4
GB
4694 elsif No (U_Ent) then
4695 U_Ent := Ent;
4696
54c04d6c
AC
4697 -- Cannot specify for a subtype (exception Object/Value_Size)
4698
996ae0b0
RK
4699 elsif Is_Type (U_Ent)
4700 and then not Is_First_Subtype (U_Ent)
4701 and then Id /= Attribute_Object_Size
4702 and then Id /= Attribute_Value_Size
4703 and then not From_At_Mod (N)
4704 then
4705 Error_Msg_N ("cannot specify attribute for subtype", Nam);
4706 return;
996ae0b0
RK
4707 end if;
4708
0f1a6a0b
AC
4709 Set_Entity (N, U_Ent);
4710
996ae0b0
RK
4711 -- Switch on particular attribute
4712
4713 case Id is
4714
4715 -------------
4716 -- Address --
4717 -------------
4718
4719 -- Address attribute definition clause
4720
4721 when Attribute_Address => Address : begin
45fc7ddb
HK
4722
4723 -- A little error check, catch for X'Address use X'Address;
4724
4725 if Nkind (Nam) = N_Identifier
4726 and then Nkind (Expr) = N_Attribute_Reference
4727 and then Attribute_Name (Expr) = Name_Address
4728 and then Nkind (Prefix (Expr)) = N_Identifier
4729 and then Chars (Nam) = Chars (Prefix (Expr))
4730 then
4731 Error_Msg_NE
4732 ("address for & is self-referencing", Prefix (Expr), Ent);
4733 return;
4734 end if;
4735
4736 -- Not that special case, carry on with analysis of expression
4737
996ae0b0
RK
4738 Analyze_And_Resolve (Expr, RTE (RE_Address));
4739
9d0c3761
AC
4740 -- Even when ignoring rep clauses we need to indicate that the
4741 -- entity has an address clause and thus it is legal to declare
cf28c974 4742 -- it imported. Freeze will get rid of the address clause later.
9d0c3761
AC
4743
4744 if Ignore_Rep_Clauses then
bce79204 4745 if Ekind_In (U_Ent, E_Variable, E_Constant) then
9d0c3761
AC
4746 Record_Rep_Item (U_Ent, N);
4747 end if;
4748
4749 return;
4750 end if;
4751
0f1a6a0b
AC
4752 if Duplicate_Clause then
4753 null;
996ae0b0
RK
4754
4755 -- Case of address clause for subprogram
4756
4757 elsif Is_Subprogram (U_Ent) then
996ae0b0
RK
4758 if Has_Homonym (U_Ent) then
4759 Error_Msg_N
32b794c8
AC
4760 ("address clause cannot be given for overloaded "
4761 & "subprogram", Nam);
2642f998 4762 return;
996ae0b0
RK
4763 end if;
4764
2642f998
RD
4765 -- For subprograms, all address clauses are permitted, and we
4766 -- mark the subprogram as having a deferred freeze so that Gigi
4767 -- will not elaborate it too soon.
996ae0b0
RK
4768
4769 -- Above needs more comments, what is too soon about???
4770
4771 Set_Has_Delayed_Freeze (U_Ent);
4772
4773 -- Case of address clause for entry
4774
4775 elsif Ekind (U_Ent) = E_Entry then
996ae0b0
RK
4776 if Nkind (Parent (N)) = N_Task_Body then
4777 Error_Msg_N
4778 ("entry address must be specified in task spec", Nam);
2642f998 4779 return;
996ae0b0
RK
4780 end if;
4781
4782 -- For entries, we require a constant address
4783
4784 Check_Constant_Address_Clause (Expr, U_Ent);
4785
2642f998
RD
4786 -- Special checks for task types
4787
07fc65c4
GB
4788 if Is_Task_Type (Scope (U_Ent))
4789 and then Comes_From_Source (Scope (U_Ent))
4790 then
4791 Error_Msg_N
dbfeb4fa 4792 ("??entry address declared for entry in task type", N);
07fc65c4 4793 Error_Msg_N
dbfeb4fa 4794 ("\??only one task can be declared of this type", N);
07fc65c4
GB
4795 end if;
4796
2642f998
RD
4797 -- Entry address clauses are obsolescent
4798
5f3ab6fb
AC
4799 Check_Restriction (No_Obsolescent_Features, N);
4800
fbf5a39b
AC
4801 if Warn_On_Obsolescent_Feature then
4802 Error_Msg_N
32b794c8
AC
4803 ("?j?attaching interrupt to task entry is an obsolescent "
4804 & "feature (RM J.7.1)", N);
fbf5a39b 4805 Error_Msg_N
dbfeb4fa 4806 ("\?j?use interrupt procedure instead", N);
fbf5a39b
AC
4807 end if;
4808
2642f998
RD
4809 -- Case of an address clause for a controlled object which we
4810 -- consider to be erroneous.
fbf5a39b 4811
2642f998
RD
4812 elsif Is_Controlled (Etype (U_Ent))
4813 or else Has_Controlled_Component (Etype (U_Ent))
4814 then
fbf5a39b 4815 Error_Msg_NE
dbfeb4fa 4816 ("??controlled object& must not be overlaid", Nam, U_Ent);
fbf5a39b 4817 Error_Msg_N
dbfeb4fa 4818 ("\??Program_Error will be raised at run time", Nam);
fbf5a39b
AC
4819 Insert_Action (Declaration_Node (U_Ent),
4820 Make_Raise_Program_Error (Loc,
4821 Reason => PE_Overlaid_Controlled_Object));
2642f998 4822 return;
fbf5a39b
AC
4823
4824 -- Case of address clause for a (non-controlled) object
996ae0b0 4825
dc06dd83 4826 elsif Ekind_In (U_Ent, E_Variable, E_Constant) then
996ae0b0 4827 declare
f4cd2542
EB
4828 Expr : constant Node_Id := Expression (N);
4829 O_Ent : Entity_Id;
4830 Off : Boolean;
996ae0b0
RK
4831
4832 begin
5132708f
RD
4833 -- Exported variables cannot have an address clause, because
4834 -- this cancels the effect of the pragma Export.
996ae0b0
RK
4835
4836 if Is_Exported (U_Ent) then
4837 Error_Msg_N
4838 ("cannot export object with address clause", Nam);
2642f998 4839 return;
f4cd2542
EB
4840 end if;
4841
4842 Find_Overlaid_Entity (N, O_Ent, Off);
996ae0b0 4843
b3b5c6a2 4844 if Present (O_Ent) then
d6dffa66 4845
b3b5c6a2 4846 -- If the object overlays a constant object, mark it so
c31b57af 4847
b3b5c6a2
EB
4848 if Is_Constant_Object (O_Ent) then
4849 Set_Overlays_Constant (U_Ent);
4850 end if;
d6dffa66 4851
b3b5c6a2
EB
4852 else
4853 -- If this is not an overlay, mark a variable as being
4854 -- volatile to prevent unwanted optimizations. It's a
4855 -- conservative interpretation of RM 13.3(19) for the
4856 -- cases where the compiler cannot detect potential
4857 -- aliasing issues easily and it also covers the case
4858 -- of an absolute address where the volatile aspect is
4859 -- kind of implicit.
4860
4861 if Ekind (U_Ent) = E_Variable then
4862 Set_Treat_As_Volatile (U_Ent);
4863 end if;
c31b57af
EB
4864 end if;
4865
d6dffa66
HK
4866 -- Overlaying controlled objects is erroneous. Emit warning
4867 -- but continue analysis because program is itself legal,
8a0183fd 4868 -- and back end must see address clause.
fbf5a39b 4869
f4cd2542
EB
4870 if Present (O_Ent)
4871 and then (Has_Controlled_Component (Etype (O_Ent))
dc06dd83 4872 or else Is_Controlled (Etype (O_Ent)))
744c73a5 4873 and then not Inside_A_Generic
fbf5a39b
AC
4874 then
4875 Error_Msg_N
744c73a5 4876 ("??cannot use overlays with controlled objects", Expr);
fbf5a39b 4877 Error_Msg_N
dbfeb4fa 4878 ("\??Program_Error will be raised at run time", Expr);
fbf5a39b
AC
4879 Insert_Action (Declaration_Node (U_Ent),
4880 Make_Raise_Program_Error (Loc,
4881 Reason => PE_Overlaid_Controlled_Object));
4882
ed11bbfe
AC
4883 -- Issue an unconditional warning for a constant overlaying
4884 -- a variable. For the reverse case, we will issue it only
c31b57af 4885 -- if the variable is modified.
ed11bbfe 4886
c31b57af 4887 elsif Ekind (U_Ent) = E_Constant
ed11bbfe 4888 and then Present (O_Ent)
c31b57af
EB
4889 and then not Overlays_Constant (U_Ent)
4890 and then Address_Clause_Overlay_Warnings
fbf5a39b 4891 then
dbfeb4fa 4892 Error_Msg_N ("??constant overlays a variable", Expr);
fbf5a39b 4893
996ae0b0
RK
4894 -- Imported variables can have an address clause, but then
4895 -- the import is pretty meaningless except to suppress
4896 -- initializations, so we do not need such variables to
4897 -- be statically allocated (and in fact it causes trouble
4898 -- if the address clause is a local value).
4899
4900 elsif Is_Imported (U_Ent) then
4901 Set_Is_Statically_Allocated (U_Ent, False);
4902 end if;
4903
4904 -- We mark a possible modification of a variable with an
4905 -- address clause, since it is likely aliasing is occurring.
4906
45fc7ddb 4907 Note_Possible_Modification (Nam, Sure => False);
996ae0b0 4908
fbf5a39b
AC
4909 -- Legality checks on the address clause for initialized
4910 -- objects is deferred until the freeze point, because
29ba9f52 4911 -- a subsequent pragma might indicate that the object
02217452
AC
4912 -- is imported and thus not initialized. Also, the address
4913 -- clause might involve entities that have yet to be
4914 -- elaborated.
fbf5a39b
AC
4915
4916 Set_Has_Delayed_Freeze (U_Ent);
4917
f3b57ab0
AC
4918 -- If an initialization call has been generated for this
4919 -- object, it needs to be deferred to after the freeze node
4920 -- we have just now added, otherwise GIGI will see a
4921 -- reference to the variable (as actual to the IP call)
4922 -- before its definition.
4923
4924 declare
3a3af4c3
AC
4925 Init_Call : constant Node_Id :=
4926 Remove_Init_Call (U_Ent, N);
ae05cdd6 4927
f3b57ab0
AC
4928 begin
4929 if Present (Init_Call) then
c6d2191a 4930 Append_Freeze_Action (U_Ent, Init_Call);
3a3af4c3 4931
c6d2191a
AC
4932 -- Reset Initialization_Statements pointer so that
4933 -- if there is a pragma Import further down, it can
4934 -- clear any default initialization.
3a3af4c3 4935
c6d2191a 4936 Set_Initialization_Statements (U_Ent, Init_Call);
f3b57ab0
AC
4937 end if;
4938 end;
4939
affbee12
RD
4940 -- Entity has delayed freeze, so we will generate an
4941 -- alignment check at the freeze point unless suppressed.
996ae0b0 4942
affbee12
RD
4943 if not Range_Checks_Suppressed (U_Ent)
4944 and then not Alignment_Checks_Suppressed (U_Ent)
4945 then
4946 Set_Check_Address_Alignment (N);
4947 end if;
996ae0b0
RK
4948
4949 -- Kill the size check code, since we are not allocating
4950 -- the variable, it is somewhere else.
4951
4952 Kill_Size_Check_Code (U_Ent);
2642f998 4953
f4cd2542 4954 -- If the address clause is of the form:
2642f998 4955
f4cd2542 4956 -- for Y'Address use X'Address
2642f998 4957
f4cd2542 4958 -- or
2642f998 4959
f4cd2542
EB
4960 -- Const : constant Address := X'Address;
4961 -- ...
4962 -- for Y'Address use Const;
2642f998 4963
f4cd2542
EB
4964 -- then we make an entry in the table for checking the size
4965 -- and alignment of the overlaying variable. We defer this
4966 -- check till after code generation to take full advantage
11d59a86 4967 -- of the annotation done by the back end.
a01b9df6 4968
e074d476 4969 -- If the entity has a generic type, the check will be
eb23d93a
AC
4970 -- performed in the instance if the actual type justifies
4971 -- it, and we do not insert the clause in the table to
4972 -- prevent spurious warnings.
2642f998 4973
11d59a86
AC
4974 -- Note: we used to test Comes_From_Source and only give
4975 -- this warning for source entities, but we have removed
4976 -- this test. It really seems bogus to generate overlays
4977 -- that would trigger this warning in generated code.
4978 -- Furthermore, by removing the test, we handle the
4979 -- aspect case properly.
4980
c31b57af 4981 if Present (O_Ent)
f4cd2542 4982 and then Is_Object (O_Ent)
c31b57af
EB
4983 and then not Is_Generic_Type (Etype (U_Ent))
4984 and then Address_Clause_Overlay_Warnings
f4cd2542 4985 then
c31b57af 4986 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
f4cd2542
EB
4987 end if;
4988 end;
2642f998 4989
996ae0b0
RK
4990 -- Not a valid entity for an address clause
4991
4992 else
4993 Error_Msg_N ("address cannot be given for &", Nam);
4994 end if;
4995 end Address;
4996
4997 ---------------
4998 -- Alignment --
4999 ---------------
5000
5001 -- Alignment attribute definition clause
5002
f8c6086b 5003 when Attribute_Alignment => Alignment : declare
6bed26b5
AC
5004 Align : constant Uint := Get_Alignment_Value (Expr);
5005 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
033eaf85 5006
996ae0b0
RK
5007 begin
5008 FOnly := True;
5009
5010 if not Is_Type (U_Ent)
5011 and then Ekind (U_Ent) /= E_Variable
5012 and then Ekind (U_Ent) /= E_Constant
5013 then
5014 Error_Msg_N ("alignment cannot be given for &", Nam);
5015
0f1a6a0b
AC
5016 elsif Duplicate_Clause then
5017 null;
996ae0b0
RK
5018
5019 elsif Align /= No_Uint then
5020 Set_Has_Alignment_Clause (U_Ent);
6bed26b5 5021
54c04d6c 5022 -- Tagged type case, check for attempt to set alignment to a
32b794c8
AC
5023 -- value greater than Max_Align, and reset if so. This error
5024 -- is suppressed in ASIS mode to allow for different ASIS
2cc2e964 5025 -- back ends or ASIS-based tools to query the illegal clause.
54c04d6c 5026
32b794c8
AC
5027 if Is_Tagged_Type (U_Ent)
5028 and then Align > Max_Align
5029 and then not ASIS_Mode
5030 then
6bed26b5 5031 Error_Msg_N
dbfeb4fa 5032 ("alignment for & set to Maximum_Aligment??", Nam);
32b794c8 5033 Set_Alignment (U_Ent, Max_Align);
54c04d6c
AC
5034
5035 -- All other cases
5036
6bed26b5
AC
5037 else
5038 Set_Alignment (U_Ent, Align);
5039 end if;
f8c6086b
AC
5040
5041 -- For an array type, U_Ent is the first subtype. In that case,
5042 -- also set the alignment of the anonymous base type so that
5043 -- other subtypes (such as the itypes for aggregates of the
5044 -- type) also receive the expected alignment.
5045
5046 if Is_Array_Type (U_Ent) then
5047 Set_Alignment (Base_Type (U_Ent), Align);
5048 end if;
996ae0b0 5049 end if;
f8c6086b 5050 end Alignment;
996ae0b0
RK
5051
5052 ---------------
5053 -- Bit_Order --
5054 ---------------
5055
5056 -- Bit_Order attribute definition clause
5057
5058 when Attribute_Bit_Order => Bit_Order : declare
5059 begin
5060 if not Is_Record_Type (U_Ent) then
5061 Error_Msg_N
5062 ("Bit_Order can only be defined for record type", Nam);
5063
0f1a6a0b
AC
5064 elsif Duplicate_Clause then
5065 null;
5066
996ae0b0
RK
5067 else
5068 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5069
5070 if Etype (Expr) = Any_Type then
5071 return;
5072
edab6088 5073 elsif not Is_OK_Static_Expression (Expr) then
fbf5a39b
AC
5074 Flag_Non_Static_Expr
5075 ("Bit_Order requires static expression!", Expr);
996ae0b0
RK
5076
5077 else
5078 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
480156b2 5079 Set_Reverse_Bit_Order (Base_Type (U_Ent), True);
996ae0b0
RK
5080 end if;
5081 end if;
5082 end if;
5083 end Bit_Order;
5084
5085 --------------------
5086 -- Component_Size --
5087 --------------------
5088
5089 -- Component_Size attribute definition clause
5090
5091 when Attribute_Component_Size => Component_Size_Case : declare
5092 Csize : constant Uint := Static_Integer (Expr);
b3f48fd4 5093 Ctyp : Entity_Id;
996ae0b0
RK
5094 Btype : Entity_Id;
5095 Biased : Boolean;
5096 New_Ctyp : Entity_Id;
5097 Decl : Node_Id;
5098
5099 begin
5100 if not Is_Array_Type (U_Ent) then
5101 Error_Msg_N ("component size requires array type", Nam);
5102 return;
5103 end if;
5104
5105 Btype := Base_Type (U_Ent);
32b794c8 5106 Ctyp := Component_Type (Btype);
996ae0b0 5107
0f1a6a0b
AC
5108 if Duplicate_Clause then
5109 null;
996ae0b0 5110
094cefda
AC
5111 elsif Rep_Item_Too_Early (Btype, N) then
5112 null;
5113
996ae0b0 5114 elsif Csize /= No_Uint then
b3f48fd4 5115 Check_Size (Expr, Ctyp, Csize, Biased);
996ae0b0 5116
c159409f
AC
5117 -- For the biased case, build a declaration for a subtype that
5118 -- will be used to represent the biased subtype that reflects
5119 -- the biased representation of components. We need the subtype
5120 -- to get proper conversions on referencing elements of the
535a8637 5121 -- array.
800621e0 5122
535a8637
AC
5123 if Biased then
5124 New_Ctyp :=
5125 Make_Defining_Identifier (Loc,
5126 Chars =>
5127 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
800621e0 5128
535a8637
AC
5129 Decl :=
5130 Make_Subtype_Declaration (Loc,
5131 Defining_Identifier => New_Ctyp,
5132 Subtype_Indication =>
5133 New_Occurrence_Of (Component_Type (Btype), Loc));
5134
5135 Set_Parent (Decl, N);
5136 Analyze (Decl, Suppress => All_Checks);
5137
5138 Set_Has_Delayed_Freeze (New_Ctyp, False);
5139 Set_Esize (New_Ctyp, Csize);
5140 Set_RM_Size (New_Ctyp, Csize);
5141 Init_Alignment (New_Ctyp);
5142 Set_Is_Itype (New_Ctyp, True);
5143 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
5144
5145 Set_Component_Type (Btype, New_Ctyp);
5146 Set_Biased (New_Ctyp, N, "component size clause");
996ae0b0
RK
5147 end if;
5148
535a8637
AC
5149 Set_Component_Size (Btype, Csize);
5150
b3f48fd4
AC
5151 -- Deal with warning on overridden size
5152
5153 if Warn_On_Overridden_Size
5154 and then Has_Size_Clause (Ctyp)
5155 and then RM_Size (Ctyp) /= Csize
5156 then
5157 Error_Msg_NE
dbfeb4fa 5158 ("component size overrides size clause for&?S?", N, Ctyp);
b3f48fd4
AC
5159 end if;
5160
996ae0b0 5161 Set_Has_Component_Size_Clause (Btype, True);
094cefda 5162 Set_Has_Non_Standard_Rep (Btype, True);
996ae0b0
RK
5163 end if;
5164 end Component_Size_Case;
5165
d50f4827
AC
5166 -----------------------
5167 -- Constant_Indexing --
5168 -----------------------
5169
5170 when Attribute_Constant_Indexing =>
5171 Check_Indexing_Functions;
5172
b98e2969
AC
5173 ---------
5174 -- CPU --
5175 ---------
5176
5177 when Attribute_CPU => CPU :
5178 begin
5179 -- CPU attribute definition clause not allowed except from aspect
5180 -- specification.
5181
5182 if From_Aspect_Specification (N) then
5183 if not Is_Task_Type (U_Ent) then
5184 Error_Msg_N ("CPU can only be defined for task", Nam);
5185
5186 elsif Duplicate_Clause then
5187 null;
5188
5189 else
5190 -- The expression must be analyzed in the special manner
5191 -- described in "Handling of Default and Per-Object
5192 -- Expressions" in sem.ads.
5193
5194 -- The visibility to the discriminants must be restored
5195
5196 Push_Scope_And_Install_Discriminants (U_Ent);
5197 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
5198 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5199
edab6088 5200 if not Is_OK_Static_Expression (Expr) then
b98e2969
AC
5201 Check_Restriction (Static_Priorities, Expr);
5202 end if;
5203 end if;
5204
5205 else
5206 Error_Msg_N
5207 ("attribute& cannot be set with definition clause", N);
5208 end if;
5209 end CPU;
5210
d941cee6
AC
5211 ----------------------
5212 -- Default_Iterator --
5213 ----------------------
5214
5215 when Attribute_Default_Iterator => Default_Iterator : declare
5216 Func : Entity_Id;
72eaa365 5217 Typ : Entity_Id;
d941cee6
AC
5218
5219 begin
4f2cae4a
ES
5220 -- If target type is untagged, further checks are irrelevant
5221
d941cee6
AC
5222 if not Is_Tagged_Type (U_Ent) then
5223 Error_Msg_N
4f2cae4a
ES
5224 ("aspect Default_Iterator applies to tagged type", Nam);
5225 return;
d941cee6
AC
5226 end if;
5227
5228 Check_Iterator_Functions;
5229
5230 Analyze (Expr);
5231
5232 if not Is_Entity_Name (Expr)
5233 or else Ekind (Entity (Expr)) /= E_Function
5234 then
5235 Error_Msg_N ("aspect Iterator must be a function", Expr);
4f2cae4a 5236 return;
d941cee6
AC
5237 else
5238 Func := Entity (Expr);
5239 end if;
5240
72eaa365 5241 -- The type of the first parameter must be T, T'class, or a
4f2cae4a
ES
5242 -- corresponding access type (5.5.1 (8/3). If function is
5243 -- parameterless label type accordingly.
72eaa365
AC
5244
5245 if No (First_Formal (Func)) then
4f2cae4a 5246 Typ := Any_Type;
72eaa365
AC
5247 else
5248 Typ := Etype (First_Formal (Func));
5249 end if;
5250
5251 if Typ = U_Ent
5252 or else Typ = Class_Wide_Type (U_Ent)
5253 or else (Is_Access_Type (Typ)
5254 and then Designated_Type (Typ) = U_Ent)
5255 or else (Is_Access_Type (Typ)
5256 and then Designated_Type (Typ) =
5257 Class_Wide_Type (U_Ent))
d941cee6 5258 then
72eaa365
AC
5259 null;
5260
5261 else
d941cee6
AC
5262 Error_Msg_NE
5263 ("Default Iterator must be a primitive of&", Func, U_Ent);
5264 end if;
5265 end Default_Iterator;
5266
b98e2969
AC
5267 ------------------------
5268 -- Dispatching_Domain --
5269 ------------------------
5270
5271 when Attribute_Dispatching_Domain => Dispatching_Domain :
5272 begin
5273 -- Dispatching_Domain attribute definition clause not allowed
5274 -- except from aspect specification.
5275
5276 if From_Aspect_Specification (N) then
5277 if not Is_Task_Type (U_Ent) then
72eaa365
AC
5278 Error_Msg_N
5279 ("Dispatching_Domain can only be defined for task", Nam);
b98e2969
AC
5280
5281 elsif Duplicate_Clause then
5282 null;
5283
5284 else
5285 -- The expression must be analyzed in the special manner
5286 -- described in "Handling of Default and Per-Object
5287 -- Expressions" in sem.ads.
5288
5289 -- The visibility to the discriminants must be restored
5290
5291 Push_Scope_And_Install_Discriminants (U_Ent);
5292
5293 Preanalyze_Spec_Expression
5294 (Expr, RTE (RE_Dispatching_Domain));
5295
5296 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5297 end if;
5298
5299 else
5300 Error_Msg_N
5301 ("attribute& cannot be set with definition clause", N);
5302 end if;
5303 end Dispatching_Domain;
5304
996ae0b0
RK
5305 ------------------
5306 -- External_Tag --
5307 ------------------
5308
5309 when Attribute_External_Tag => External_Tag :
5310 begin
5311 if not Is_Tagged_Type (U_Ent) then
5312 Error_Msg_N ("should be a tagged type", Nam);
5313 end if;
5314
0f1a6a0b
AC
5315 if Duplicate_Clause then
5316 null;
996ae0b0 5317
76efd572 5318 else
0f1a6a0b 5319 Analyze_And_Resolve (Expr, Standard_String);
c690a2ec 5320
edab6088 5321 if not Is_OK_Static_Expression (Expr) then
0f1a6a0b
AC
5322 Flag_Non_Static_Expr
5323 ("static string required for tag name!", Nam);
5324 end if;
5325
0f1a6a0b
AC
5326 if not Is_Library_Level_Entity (U_Ent) then
5327 Error_Msg_NE
dbfeb4fa 5328 ("??non-unique external tag supplied for &", N, U_Ent);
0f1a6a0b 5329 Error_Msg_N
32b794c8
AC
5330 ("\??same external tag applies to all subprogram calls",
5331 N);
0f1a6a0b 5332 Error_Msg_N
dbfeb4fa 5333 ("\??corresponding internal tag cannot be obtained", N);
0f1a6a0b 5334 end if;
c690a2ec 5335 end if;
996ae0b0
RK
5336 end External_Tag;
5337
0da80d7d
AC
5338 --------------------------
5339 -- Implicit_Dereference --
5340 --------------------------
bb3c784c 5341
0da80d7d 5342 when Attribute_Implicit_Dereference =>
bb3c784c 5343
29ba9f52
RD
5344 -- Legality checks already performed at the point of the type
5345 -- declaration, aspect is not delayed.
bb3c784c 5346
d941cee6 5347 null;
0da80d7d 5348
996ae0b0
RK
5349 -----------
5350 -- Input --
5351 -----------
5352
edd63e9b
ES
5353 when Attribute_Input =>
5354 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
5355 Set_Has_Specified_Stream_Input (Ent);
996ae0b0 5356
b98e2969
AC
5357 ------------------------
5358 -- Interrupt_Priority --
5359 ------------------------
5360
5361 when Attribute_Interrupt_Priority => Interrupt_Priority :
5362 begin
5363 -- Interrupt_Priority attribute definition clause not allowed
5364 -- except from aspect specification.
5365
5366 if From_Aspect_Specification (N) then
dc06dd83 5367 if not Is_Concurrent_Type (U_Ent) then
b98e2969 5368 Error_Msg_N
32b794c8
AC
5369 ("Interrupt_Priority can only be defined for task and "
5370 & "protected object", Nam);
b98e2969
AC
5371
5372 elsif Duplicate_Clause then
5373 null;
5374
5375 else
5376 -- The expression must be analyzed in the special manner
5377 -- described in "Handling of Default and Per-Object
5378 -- Expressions" in sem.ads.
5379
5380 -- The visibility to the discriminants must be restored
5381
5382 Push_Scope_And_Install_Discriminants (U_Ent);
5383
5384 Preanalyze_Spec_Expression
5385 (Expr, RTE (RE_Interrupt_Priority));
5386
5387 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
6bf8c157
TG
5388
5389 -- Check the No_Task_At_Interrupt_Priority restriction
5390
5391 if Is_Task_Type (U_Ent) then
5392 Check_Restriction (No_Task_At_Interrupt_Priority, N);
5393 end if;
b98e2969
AC
5394 end if;
5395
5396 else
5397 Error_Msg_N
5398 ("attribute& cannot be set with definition clause", N);
5399 end if;
5400 end Interrupt_Priority;
5401
dd2bf554
ES
5402 --------------
5403 -- Iterable --
5404 --------------
5405
5406 when Attribute_Iterable =>
5407 Analyze (Expr);
82d4f390 5408
dd2bf554
ES
5409 if Nkind (Expr) /= N_Aggregate then
5410 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
5411 end if;
5412
5413 declare
5414 Assoc : Node_Id;
5415
5416 begin
5417 Assoc := First (Component_Associations (Expr));
5418 while Present (Assoc) loop
5419 if not Is_Entity_Name (Expression (Assoc)) then
5420 Error_Msg_N ("value must be a function", Assoc);
5421 end if;
82d4f390 5422
dd2bf554
ES
5423 Next (Assoc);
5424 end loop;
5425 end;
5426
d941cee6
AC
5427 ----------------------
5428 -- Iterator_Element --
5429 ----------------------
5430
5431 when Attribute_Iterator_Element =>
5432 Analyze (Expr);
5433
5434 if not Is_Entity_Name (Expr)
5435 or else not Is_Type (Entity (Expr))
5436 then
5437 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
5438 end if;
5439
996ae0b0
RK
5440 -------------------
5441 -- Machine_Radix --
5442 -------------------
5443
5444 -- Machine radix attribute definition clause
5445
5446 when Attribute_Machine_Radix => Machine_Radix : declare
5447 Radix : constant Uint := Static_Integer (Expr);
5448
5449 begin
5450 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
5451 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
5452
0f1a6a0b
AC
5453 elsif Duplicate_Clause then
5454 null;
996ae0b0
RK
5455
5456 elsif Radix /= No_Uint then
5457 Set_Has_Machine_Radix_Clause (U_Ent);
5458 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
5459
5460 if Radix = 2 then
5461 null;
32b794c8 5462
996ae0b0
RK
5463 elsif Radix = 10 then
5464 Set_Machine_Radix_10 (U_Ent);
32b794c8
AC
5465
5466 -- The following error is suppressed in ASIS mode to allow for
2cc2e964 5467 -- different ASIS back ends or ASIS-based tools to query the
32b794c8
AC
5468 -- illegal clause.
5469
5470 elsif not ASIS_Mode then
996ae0b0
RK
5471 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
5472 end if;
5473 end if;
5474 end Machine_Radix;
5475
5476 -----------------
5477 -- Object_Size --
5478 -----------------
5479
5480 -- Object_Size attribute definition clause
5481
5482 when Attribute_Object_Size => Object_Size : declare
d4731b80
BD
5483 Size : constant Uint := Static_Integer (Expr);
5484
996ae0b0 5485 Biased : Boolean;
d4731b80 5486 pragma Warnings (Off, Biased);
996ae0b0
RK
5487
5488 begin
5489 if not Is_Type (U_Ent) then
5490 Error_Msg_N ("Object_Size cannot be given for &", Nam);
5491
0f1a6a0b
AC
5492 elsif Duplicate_Clause then
5493 null;
996ae0b0
RK
5494
5495 else
5496 Check_Size (Expr, U_Ent, Size, Biased);
5497
32b794c8 5498 -- The following errors are suppressed in ASIS mode to allow
2cc2e964 5499 -- for different ASIS back ends or ASIS-based tools to query
32b794c8
AC
5500 -- the illegal clause.
5501
5502 if ASIS_Mode then
5503 null;
5504
5505 elsif Is_Scalar_Type (U_Ent) then
3e586e10
AC
5506 if Size /= 8 and then Size /= 16 and then Size /= 32
5507 and then UI_Mod (Size, 64) /= 0
5508 then
5509 Error_Msg_N
5510 ("Object_Size must be 8, 16, 32, or multiple of 64",
5511 Expr);
5512 end if;
5513
5514 elsif Size mod 8 /= 0 then
5515 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
996ae0b0
RK
5516 end if;
5517
5518 Set_Esize (U_Ent, Size);
5519 Set_Has_Object_Size_Clause (U_Ent);
76af4137 5520 Alignment_Check_For_Size_Change (U_Ent, Size);
996ae0b0
RK
5521 end if;
5522 end Object_Size;
5523
5524 ------------
5525 -- Output --
5526 ------------
5527
edd63e9b
ES
5528 when Attribute_Output =>
5529 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
5530 Set_Has_Specified_Stream_Output (Ent);
996ae0b0 5531
b98e2969
AC
5532 --------------
5533 -- Priority --
5534 --------------
5535
5536 when Attribute_Priority => Priority :
5537 begin
5538 -- Priority attribute definition clause not allowed except from
5539 -- aspect specification.
5540
5541 if From_Aspect_Specification (N) then
dc06dd83 5542 if not (Is_Concurrent_Type (U_Ent)
473e20df 5543 or else Ekind (U_Ent) = E_Procedure)
b98e2969
AC
5544 then
5545 Error_Msg_N
dc06dd83
AC
5546 ("Priority can only be defined for task and protected "
5547 & "object", Nam);
b98e2969
AC
5548
5549 elsif Duplicate_Clause then
5550 null;
5551
5552 else
5553 -- The expression must be analyzed in the special manner
5554 -- described in "Handling of Default and Per-Object
5555 -- Expressions" in sem.ads.
5556
5557 -- The visibility to the discriminants must be restored
5558
5559 Push_Scope_And_Install_Discriminants (U_Ent);
5560 Preanalyze_Spec_Expression (Expr, Standard_Integer);
5561 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
5562
edab6088 5563 if not Is_OK_Static_Expression (Expr) then
b98e2969
AC
5564 Check_Restriction (Static_Priorities, Expr);
5565 end if;
5566 end if;
5567
5568 else
5569 Error_Msg_N
5570 ("attribute& cannot be set with definition clause", N);
5571 end if;
5572 end Priority;
5573
996ae0b0
RK
5574 ----------
5575 -- Read --
5576 ----------
5577
edd63e9b
ES
5578 when Attribute_Read =>
5579 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
5580 Set_Has_Specified_Stream_Read (Ent);
996ae0b0 5581
f91510fc
AC
5582 --------------------------
5583 -- Scalar_Storage_Order --
5584 --------------------------
5585
5586 -- Scalar_Storage_Order attribute definition clause
5587
5588 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
5589 begin
a2c1791d 5590 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
f91510fc 5591 Error_Msg_N
32b794c8
AC
5592 ("Scalar_Storage_Order can only be defined for record or "
5593 & "array type", Nam);
f91510fc
AC
5594
5595 elsif Duplicate_Clause then
5596 null;
5597
5598 else
5599 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
5600
5601 if Etype (Expr) = Any_Type then
5602 return;
5603
edab6088 5604 elsif not Is_OK_Static_Expression (Expr) then
f91510fc
AC
5605 Flag_Non_Static_Expr
5606 ("Scalar_Storage_Order requires static expression!", Expr);
5607
d7761b2d
AC
5608 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
5609
5610 -- Here for the case of a non-default (i.e. non-confirming)
5611 -- Scalar_Storage_Order attribute definition.
5612
5613 if Support_Nondefault_SSO_On_Target then
1e4b91fc 5614 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
d7761b2d
AC
5615 else
5616 Error_Msg_N
32b794c8
AC
5617 ("non-default Scalar_Storage_Order not supported on "
5618 & "target", Expr);
f91510fc
AC
5619 end if;
5620 end if;
220d1fd9
AC
5621
5622 -- Clear SSO default indications since explicit setting of the
5623 -- order overrides the defaults.
5624
5625 Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False);
5626 Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False);
f91510fc
AC
5627 end if;
5628 end Scalar_Storage_Order;
5629
996ae0b0
RK
5630 ----------
5631 -- Size --
5632 ----------
5633
5634 -- Size attribute definition clause
5635
5636 when Attribute_Size => Size : declare
5637 Size : constant Uint := Static_Integer (Expr);
5638 Etyp : Entity_Id;
5639 Biased : Boolean;
5640
5641 begin
5642 FOnly := True;
5643
0f1a6a0b
AC
5644 if Duplicate_Clause then
5645 null;
996ae0b0
RK
5646
5647 elsif not Is_Type (U_Ent)
5648 and then Ekind (U_Ent) /= E_Variable
5649 and then Ekind (U_Ent) /= E_Constant
5650 then
5651 Error_Msg_N ("size cannot be given for &", Nam);
5652
5653 elsif Is_Array_Type (U_Ent)
5654 and then not Is_Constrained (U_Ent)
5655 then
5656 Error_Msg_N
5657 ("size cannot be given for unconstrained array", Nam);
5658
f4b049db 5659 elsif Size /= No_Uint then
996ae0b0
RK
5660 if Is_Type (U_Ent) then
5661 Etyp := U_Ent;
5662 else
5663 Etyp := Etype (U_Ent);
5664 end if;
5665
a9a5b8ac
RD
5666 -- Check size, note that Gigi is in charge of checking that the
5667 -- size of an array or record type is OK. Also we do not check
5668 -- the size in the ordinary fixed-point case, since it is too
5669 -- early to do so (there may be subsequent small clause that
5670 -- affects the size). We can check the size if a small clause
5671 -- has already been given.
996ae0b0
RK
5672
5673 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
5674 or else Has_Small_Clause (U_Ent)
5675 then
5676 Check_Size (Expr, Etyp, Size, Biased);
a3f2babd 5677 Set_Biased (U_Ent, N, "size clause", Biased);
996ae0b0
RK
5678 end if;
5679
5680 -- For types set RM_Size and Esize if possible
5681
5682 if Is_Type (U_Ent) then
5683 Set_RM_Size (U_Ent, Size);
5684
fc893455
AC
5685 -- For elementary types, increase Object_Size to power of 2,
5686 -- but not less than a storage unit in any case (normally
a9a5b8ac 5687 -- this means it will be byte addressable).
996ae0b0 5688
fc893455
AC
5689 -- For all other types, nothing else to do, we leave Esize
5690 -- (object size) unset, the back end will set it from the
5691 -- size and alignment in an appropriate manner.
5692
76af4137
AC
5693 -- In both cases, we check whether the alignment must be
5694 -- reset in the wake of the size change.
5695
fc893455 5696 if Is_Elementary_Type (U_Ent) then
07fc65c4
GB
5697 if Size <= System_Storage_Unit then
5698 Init_Esize (U_Ent, System_Storage_Unit);
996ae0b0
RK
5699 elsif Size <= 16 then
5700 Init_Esize (U_Ent, 16);
5701 elsif Size <= 32 then
5702 Init_Esize (U_Ent, 32);
5703 else
5704 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
5705 end if;
5706
76af4137
AC
5707 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
5708 else
5709 Alignment_Check_For_Size_Change (U_Ent, Size);
996ae0b0
RK
5710 end if;
5711
996ae0b0
RK
5712 -- For objects, set Esize only
5713
5714 else
32b794c8 5715 -- The following error is suppressed in ASIS mode to allow
2cc2e964 5716 -- for different ASIS back ends or ASIS-based tools to query
32b794c8
AC
5717 -- the illegal clause.
5718
5719 if Is_Elementary_Type (Etyp)
5720 and then Size /= System_Storage_Unit
5721 and then Size /= System_Storage_Unit * 2
5722 and then Size /= System_Storage_Unit * 4
5723 and then Size /= System_Storage_Unit * 8
5724 and then not ASIS_Mode
5725 then
5726 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5727 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
5728 Error_Msg_N
5729 ("size for primitive object must be a power of 2 in "
5730 & "the range ^-^", N);
fbf5a39b
AC
5731 end if;
5732
996ae0b0
RK
5733 Set_Esize (U_Ent, Size);
5734 end if;
5735
5736 Set_Has_Size_Clause (U_Ent);
5737 end if;
5738 end Size;
5739
5740 -----------
5741 -- Small --
5742 -----------
5743
5744 -- Small attribute definition clause
5745
5746 when Attribute_Small => Small : declare
5747 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
5748 Small : Ureal;
5749
5750 begin
5751 Analyze_And_Resolve (Expr, Any_Real);
5752
5753 if Etype (Expr) = Any_Type then
5754 return;
5755
edab6088 5756 elsif not Is_OK_Static_Expression (Expr) then
fbf5a39b
AC
5757 Flag_Non_Static_Expr
5758 ("small requires static expression!", Expr);
996ae0b0
RK
5759 return;
5760
5761 else
5762 Small := Expr_Value_R (Expr);
5763
5764 if Small <= Ureal_0 then
5765 Error_Msg_N ("small value must be greater than zero", Expr);
5766 return;
5767 end if;
5768
5769 end if;
5770
5771 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
5772 Error_Msg_N
5773 ("small requires an ordinary fixed point type", Nam);
5774
5775 elsif Has_Small_Clause (U_Ent) then
5776 Error_Msg_N ("small already given for &", Nam);
5777
5778 elsif Small > Delta_Value (U_Ent) then
5779 Error_Msg_N
d99ff0f4 5780 ("small value must not be greater than delta value", Nam);
996ae0b0
RK
5781
5782 else
5783 Set_Small_Value (U_Ent, Small);
5784 Set_Small_Value (Implicit_Base, Small);
5785 Set_Has_Small_Clause (U_Ent);
5786 Set_Has_Small_Clause (Implicit_Base);
5787 Set_Has_Non_Standard_Rep (Implicit_Base);
5788 end if;
5789 end Small;
5790
996ae0b0
RK
5791 ------------------
5792 -- Storage_Pool --
5793 ------------------
5794
5795 -- Storage_Pool attribute definition clause
5796
a8551b5f 5797 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
996ae0b0 5798 Pool : Entity_Id;
fe98a6aa 5799 T : Entity_Id;
996ae0b0
RK
5800
5801 begin
affbee12
RD
5802 if Ekind (U_Ent) = E_Access_Subprogram_Type then
5803 Error_Msg_N
5804 ("storage pool cannot be given for access-to-subprogram type",
5805 Nam);
5806 return;
5807
bce79204
AC
5808 elsif not
5809 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
996ae0b0 5810 then
affbee12
RD
5811 Error_Msg_N
5812 ("storage pool can only be given for access types", Nam);
996ae0b0
RK
5813 return;
5814
5815 elsif Is_Derived_Type (U_Ent) then
5816 Error_Msg_N
5817 ("storage pool cannot be given for a derived access type",
5818 Nam);
5819
0f1a6a0b 5820 elsif Duplicate_Clause then
996ae0b0
RK
5821 return;
5822
5823 elsif Present (Associated_Storage_Pool (U_Ent)) then
5824 Error_Msg_N ("storage pool already given for &", Nam);
5825 return;
5826 end if;
5827
113a62d9
RD
5828 -- Check for Storage_Size previously given
5829
5830 declare
5831 SS : constant Node_Id :=
5832 Get_Attribute_Definition_Clause
5833 (U_Ent, Attribute_Storage_Size);
5834 begin
5835 if Present (SS) then
5836 Check_Pool_Size_Clash (U_Ent, N, SS);
5837 end if;
5838 end;
5839
5840 -- Storage_Pool case
5841
a8551b5f
AC
5842 if Id = Attribute_Storage_Pool then
5843 Analyze_And_Resolve
5844 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
5845
5846 -- In the Simple_Storage_Pool case, we allow a variable of any
f6205414 5847 -- simple storage pool type, so we Resolve without imposing an
a8551b5f
AC
5848 -- expected type.
5849
5850 else
5851 Analyze_And_Resolve (Expr);
5852
5853 if not Present (Get_Rep_Pragma
f6205414 5854 (Etype (Expr), Name_Simple_Storage_Pool_Type))
a8551b5f
AC
5855 then
5856 Error_Msg_N
5857 ("expression must be of a simple storage pool type", Expr);
5858 end if;
5859 end if;
996ae0b0 5860
cb572b75
ST
5861 if not Denotes_Variable (Expr) then
5862 Error_Msg_N ("storage pool must be a variable", Expr);
5863 return;
5864 end if;
5865
fe98a6aa
CC
5866 if Nkind (Expr) = N_Type_Conversion then
5867 T := Etype (Expression (Expr));
5868 else
5869 T := Etype (Expr);
5870 end if;
5871
5872 -- The Stack_Bounded_Pool is used internally for implementing
a01b9df6
AC
5873 -- access types with a Storage_Size. Since it only work properly
5874 -- when used on one specific type, we need to check that it is not
5875 -- hijacked improperly:
5876
fe98a6aa
CC
5877 -- type T is access Integer;
5878 -- for T'Storage_Size use n;
5879 -- type Q is access Float;
5880 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
5881
8f7770f9
RD
5882 if RTE_Available (RE_Stack_Bounded_Pool)
5883 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
5884 then
5885 Error_Msg_N ("non-shareable internal Pool", Expr);
fe98a6aa
CC
5886 return;
5887 end if;
5888
996ae0b0
RK
5889 -- If the argument is a name that is not an entity name, then
5890 -- we construct a renaming operation to define an entity of
5891 -- type storage pool.
5892
5893 if not Is_Entity_Name (Expr)
5894 and then Is_Object_Reference (Expr)
5895 then
092ef350 5896 Pool := Make_Temporary (Loc, 'P', Expr);
996ae0b0
RK
5897
5898 declare
5899 Rnode : constant Node_Id :=
5900 Make_Object_Renaming_Declaration (Loc,
5901 Defining_Identifier => Pool,
5902 Subtype_Mark =>
5903 New_Occurrence_Of (Etype (Expr), Loc),
092ef350 5904 Name => Expr);
996ae0b0
RK
5905
5906 begin
9e92ad49
AC
5907 -- If the attribute definition clause comes from an aspect
5908 -- clause, then insert the renaming before the associated
5909 -- entity's declaration, since the attribute clause has
5910 -- not yet been appended to the declaration list.
5911
5912 if From_Aspect_Specification (N) then
5913 Insert_Before (Parent (Entity (N)), Rnode);
5914 else
5915 Insert_Before (N, Rnode);
5916 end if;
5917
996ae0b0
RK
5918 Analyze (Rnode);
5919 Set_Associated_Storage_Pool (U_Ent, Pool);
5920 end;
5921
5922 elsif Is_Entity_Name (Expr) then
5923 Pool := Entity (Expr);
5924
5925 -- If pool is a renamed object, get original one. This can
5926 -- happen with an explicit renaming, and within instances.
5927
5928 while Present (Renamed_Object (Pool))
5929 and then Is_Entity_Name (Renamed_Object (Pool))
5930 loop
5931 Pool := Entity (Renamed_Object (Pool));
5932 end loop;
5933
5934 if Present (Renamed_Object (Pool))
5935 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5936 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5937 then
5938 Pool := Entity (Expression (Renamed_Object (Pool)));
5939 end if;
5940
fe98a6aa 5941 Set_Associated_Storage_Pool (U_Ent, Pool);
996ae0b0
RK
5942
5943 elsif Nkind (Expr) = N_Type_Conversion
5944 and then Is_Entity_Name (Expression (Expr))
5945 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5946 then
5947 Pool := Entity (Expression (Expr));
fe98a6aa 5948 Set_Associated_Storage_Pool (U_Ent, Pool);
996ae0b0
RK
5949
5950 else
5951 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5952 return;
5953 end if;
a8551b5f 5954 end;
996ae0b0 5955
affbee12
RD
5956 ------------------
5957 -- Storage_Size --
5958 ------------------
5959
5960 -- Storage_Size attribute definition clause
5961
5962 when Attribute_Storage_Size => Storage_Size : declare
5963 Btype : constant Entity_Id := Base_Type (U_Ent);
affbee12
RD
5964
5965 begin
5966 if Is_Task_Type (U_Ent) then
affbee12 5967
a90bd866 5968 -- Check obsolescent (but never obsolescent if from aspect)
2cbac6c6
AC
5969
5970 if not From_Aspect_Specification (N) then
5971 Check_Restriction (No_Obsolescent_Features, N);
5972
5973 if Warn_On_Obsolescent_Feature then
5974 Error_Msg_N
32b794c8
AC
5975 ("?j?storage size clause for task is an obsolescent "
5976 & "feature (RM J.9)", N);
2cbac6c6
AC
5977 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5978 end if;
affbee12
RD
5979 end if;
5980
5981 FOnly := True;
5982 end if;
5983
5984 if not Is_Access_Type (U_Ent)
5985 and then Ekind (U_Ent) /= E_Task_Type
5986 then
5987 Error_Msg_N ("storage size cannot be given for &", Nam);
5988
5989 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5990 Error_Msg_N
5991 ("storage size cannot be given for a derived access type",
5992 Nam);
5993
0f1a6a0b
AC
5994 elsif Duplicate_Clause then
5995 null;
affbee12
RD
5996
5997 else
5998 Analyze_And_Resolve (Expr, Any_Integer);
5999
6000 if Is_Access_Type (U_Ent) then
113a62d9
RD
6001
6002 -- Check for Storage_Pool previously given
6003
6004 declare
6005 SP : constant Node_Id :=
6006 Get_Attribute_Definition_Clause
6007 (U_Ent, Attribute_Storage_Pool);
6008
6009 begin
6010 if Present (SP) then
6011 Check_Pool_Size_Clash (U_Ent, SP, N);
6012 end if;
6013 end;
6014
6015 -- Special case of for x'Storage_Size use 0
affbee12 6016
8da337c5 6017 if Is_OK_Static_Expression (Expr)
affbee12
RD
6018 and then Expr_Value (Expr) = 0
6019 then
6020 Set_No_Pool_Assigned (Btype);
6021 end if;
affbee12
RD
6022 end if;
6023
6024 Set_Has_Storage_Size_Clause (Btype);
6025 end if;
6026 end Storage_Size;
6027
82c80734
RD
6028 -----------------
6029 -- Stream_Size --
6030 -----------------
6031
6032 when Attribute_Stream_Size => Stream_Size : declare
6033 Size : constant Uint := Static_Integer (Expr);
6034
6035 begin
8f7770f9
RD
6036 if Ada_Version <= Ada_95 then
6037 Check_Restriction (No_Implementation_Attributes, N);
6038 end if;
6039
0f1a6a0b
AC
6040 if Duplicate_Clause then
6041 null;
82c80734
RD
6042
6043 elsif Is_Elementary_Type (U_Ent) then
32b794c8
AC
6044
6045 -- The following errors are suppressed in ASIS mode to allow
2cc2e964 6046 -- for different ASIS back ends or ASIS-based tools to query
32b794c8
AC
6047 -- the illegal clause.
6048
6049 if ASIS_Mode then
6050 null;
6051
6052 elsif Size /= System_Storage_Unit
6053 and then Size /= System_Storage_Unit * 2
6054 and then Size /= System_Storage_Unit * 4
6055 and then Size /= System_Storage_Unit * 8
82c80734
RD
6056 then
6057 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
6058 Error_Msg_N
32b794c8
AC
6059 ("stream size for elementary type must be a power of 2 "
6060 & "and at least ^", N);
82c80734
RD
6061
6062 elsif RM_Size (U_Ent) > Size then
6063 Error_Msg_Uint_1 := RM_Size (U_Ent);
6064 Error_Msg_N
32b794c8
AC
6065 ("stream size for elementary type must be a power of 2 "
6066 & "and at least ^", N);
82c80734
RD
6067 end if;
6068
6069 Set_Has_Stream_Size_Clause (U_Ent);
6070
6071 else
6072 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
6073 end if;
6074 end Stream_Size;
6075
996ae0b0
RK
6076 ----------------
6077 -- Value_Size --
6078 ----------------
6079
6080 -- Value_Size attribute definition clause
6081
6082 when Attribute_Value_Size => Value_Size : declare
6083 Size : constant Uint := Static_Integer (Expr);
6084 Biased : Boolean;
6085
6086 begin
6087 if not Is_Type (U_Ent) then
6088 Error_Msg_N ("Value_Size cannot be given for &", Nam);
6089
0f1a6a0b
AC
6090 elsif Duplicate_Clause then
6091 null;
996ae0b0 6092
a9a5b8ac
RD
6093 elsif Is_Array_Type (U_Ent)
6094 and then not Is_Constrained (U_Ent)
6095 then
6096 Error_Msg_N
6097 ("Value_Size cannot be given for unconstrained array", Nam);
6098
996ae0b0
RK
6099 else
6100 if Is_Elementary_Type (U_Ent) then
6101 Check_Size (Expr, U_Ent, Size, Biased);
a3f2babd 6102 Set_Biased (U_Ent, N, "value size clause", Biased);
996ae0b0
RK
6103 end if;
6104
6105 Set_RM_Size (U_Ent, Size);
6106 end if;
6107 end Value_Size;
6108
d50f4827
AC
6109 -----------------------
6110 -- Variable_Indexing --
6111 -----------------------
6112
6113 when Attribute_Variable_Indexing =>
6114 Check_Indexing_Functions;
6115
996ae0b0
RK
6116 -----------
6117 -- Write --
6118 -----------
6119
edd63e9b
ES
6120 when Attribute_Write =>
6121 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
6122 Set_Has_Specified_Stream_Write (Ent);
996ae0b0
RK
6123
6124 -- All other attributes cannot be set
6125
6126 when others =>
6127 Error_Msg_N
6128 ("attribute& cannot be set with definition clause", N);
996ae0b0
RK
6129 end case;
6130
a01b9df6
AC
6131 -- The test for the type being frozen must be performed after any
6132 -- expression the clause has been analyzed since the expression itself
6133 -- might cause freezing that makes the clause illegal.
996ae0b0
RK
6134
6135 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
6136 return;
6137 end if;
6138 end Analyze_Attribute_Definition_Clause;
6139
6140 ----------------------------
6141 -- Analyze_Code_Statement --
6142 ----------------------------
6143
6144 procedure Analyze_Code_Statement (N : Node_Id) is
6145 HSS : constant Node_Id := Parent (N);
6146 SBody : constant Node_Id := Parent (HSS);
6147 Subp : constant Entity_Id := Current_Scope;
6148 Stmt : Node_Id;
6149 Decl : Node_Id;
6150 StmtO : Node_Id;
6151 DeclO : Node_Id;
6152
6153 begin
0fe797c5
SB
6154 -- Accept foreign code statements for CodePeer. The analysis is skipped
6155 -- to avoid rejecting unrecognized constructs.
6156
6157 if CodePeer_Mode then
6158 Set_Analyzed (N);
6159 return;
6160 end if;
6161
996ae0b0 6162 -- Analyze and check we get right type, note that this implements the
0fe797c5
SB
6163 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that is
6164 -- the only way that Asm_Insn could possibly be visible.
996ae0b0
RK
6165
6166 Analyze_And_Resolve (Expression (N));
6167
6168 if Etype (Expression (N)) = Any_Type then
6169 return;
6170 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
6171 Error_Msg_N ("incorrect type for code statement", N);
6172 return;
6173 end if;
6174
affbee12
RD
6175 Check_Code_Statement (N);
6176
0fe797c5
SB
6177 -- Make sure we appear in the handled statement sequence of a subprogram
6178 -- (RM 13.8(3)).
996ae0b0
RK
6179
6180 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
6181 or else Nkind (SBody) /= N_Subprogram_Body
6182 then
6183 Error_Msg_N
6184 ("code statement can only appear in body of subprogram", N);
6185 return;
6186 end if;
6187
6188 -- Do remaining checks (RM 13.8(3)) if not already done
6189
6190 if not Is_Machine_Code_Subprogram (Subp) then
6191 Set_Is_Machine_Code_Subprogram (Subp);
6192
6193 -- No exception handlers allowed
6194
6195 if Present (Exception_Handlers (HSS)) then
6196 Error_Msg_N
6197 ("exception handlers not permitted in machine code subprogram",
6198 First (Exception_Handlers (HSS)));
6199 end if;
6200
6201 -- No declarations other than use clauses and pragmas (we allow
6202 -- certain internally generated declarations as well).
6203
6204 Decl := First (Declarations (SBody));
6205 while Present (Decl) loop
6206 DeclO := Original_Node (Decl);
6207 if Comes_From_Source (DeclO)
0503c53a
RD
6208 and not Nkind_In (DeclO, N_Pragma,
6209 N_Use_Package_Clause,
6210 N_Use_Type_Clause,
6211 N_Implicit_Label_Declaration)
996ae0b0
RK
6212 then
6213 Error_Msg_N
6214 ("this declaration not allowed in machine code subprogram",
6215 DeclO);
6216 end if;
6217
6218 Next (Decl);
6219 end loop;
6220
6221 -- No statements other than code statements, pragmas, and labels.
6222 -- Again we allow certain internally generated statements.
3e7302c3 6223
9f8d1e5c
AC
6224 -- In Ada 2012, qualified expressions are names, and the code
6225 -- statement is initially parsed as a procedure call.
996ae0b0
RK
6226
6227 Stmt := First (Statements (HSS));
6228 while Present (Stmt) loop
6229 StmtO := Original_Node (Stmt);
9f8d1e5c 6230
0fe797c5 6231 -- A procedure call transformed into a code statement is OK
af89615f 6232
9f8d1e5c
AC
6233 if Ada_Version >= Ada_2012
6234 and then Nkind (StmtO) = N_Procedure_Call_Statement
af89615f 6235 and then Nkind (Name (StmtO)) = N_Qualified_Expression
9f8d1e5c
AC
6236 then
6237 null;
6238
6239 elsif Comes_From_Source (StmtO)
0503c53a
RD
6240 and then not Nkind_In (StmtO, N_Pragma,
6241 N_Label,
6242 N_Code_Statement)
996ae0b0
RK
6243 then
6244 Error_Msg_N
6245 ("this statement is not allowed in machine code subprogram",
6246 StmtO);
6247 end if;
6248
6249 Next (Stmt);
6250 end loop;
6251 end if;
996ae0b0
RK
6252 end Analyze_Code_Statement;
6253
6254 -----------------------------------------------
6255 -- Analyze_Enumeration_Representation_Clause --
6256 -----------------------------------------------
6257
6258 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
ac566cbe
AC
6259 Ident : constant Node_Id := Identifier (N);
6260 Aggr : constant Node_Id := Array_Aggregate (N);
996ae0b0
RK
6261 Enumtype : Entity_Id;
6262 Elit : Entity_Id;
6263 Expr : Node_Id;
6264 Assoc : Node_Id;
6265 Choice : Node_Id;
6266 Val : Uint;
2c1b72d7
AC
6267
6268 Err : Boolean := False;
686d0984 6269 -- Set True to avoid cascade errors and crashes on incorrect source code
996ae0b0 6270
d69cf005
AC
6271 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
6272 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
6273 -- Allowed range of universal integer (= allowed range of enum lit vals)
6274
996ae0b0
RK
6275 Min : Uint;
6276 Max : Uint;
d69cf005
AC
6277 -- Minimum and maximum values of entries
6278
6279 Max_Node : Node_Id;
6280 -- Pointer to node for literal providing max value
996ae0b0
RK
6281
6282 begin
f5afb270 6283 if Ignore_Rep_Clauses then
cf28c974 6284 Kill_Rep_Clause (N);
c690a2ec
RD
6285 return;
6286 end if;
6287
8bfbd380
AC
6288 -- Ignore enumeration rep clauses by default in CodePeer mode,
6289 -- unless -gnatd.I is specified, as a work around for potential false
6290 -- positive messages.
6291
6292 if CodePeer_Mode and not Debug_Flag_Dot_II then
6293 return;
6294 end if;
6295
996ae0b0
RK
6296 -- First some basic error checks
6297
6298 Find_Type (Ident);
6299 Enumtype := Entity (Ident);
6300
6301 if Enumtype = Any_Type
6302 or else Rep_Item_Too_Early (Enumtype, N)
6303 then
6304 return;
6305 else
6306 Enumtype := Underlying_Type (Enumtype);
6307 end if;
6308
6309 if not Is_Enumeration_Type (Enumtype) then
6310 Error_Msg_NE
6311 ("enumeration type required, found}",
6312 Ident, First_Subtype (Enumtype));
6313 return;
6314 end if;
6315
fbf5a39b
AC
6316 -- Ignore rep clause on generic actual type. This will already have
6317 -- been flagged on the template as an error, and this is the safest
6318 -- way to ensure we don't get a junk cascaded message in the instance.
6319
6320 if Is_Generic_Actual_Type (Enumtype) then
6321 return;
6322
6323 -- Type must be in current scope
6324
6325 elsif Scope (Enumtype) /= Current_Scope then
996ae0b0
RK
6326 Error_Msg_N ("type must be declared in this scope", Ident);
6327 return;
6328
fbf5a39b
AC
6329 -- Type must be a first subtype
6330
996ae0b0
RK
6331 elsif not Is_First_Subtype (Enumtype) then
6332 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
6333 return;
6334
fbf5a39b
AC
6335 -- Ignore duplicate rep clause
6336
996ae0b0
RK
6337 elsif Has_Enumeration_Rep_Clause (Enumtype) then
6338 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
6339 return;
6340
82c80734 6341 -- Don't allow rep clause for standard [wide_[wide_]]character
fbf5a39b 6342
45fc7ddb 6343 elsif Is_Standard_Character_Type (Enumtype) then
996ae0b0 6344 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
fbf5a39b
AC
6345 return;
6346
27e6455d
TQ
6347 -- Check that the expression is a proper aggregate (no parentheses)
6348
6349 elsif Paren_Count (Aggr) /= 0 then
6350 Error_Msg
6351 ("extra parentheses surrounding aggregate not allowed",
6352 First_Sloc (Aggr));
6353 return;
6354
fbf5a39b 6355 -- All tests passed, so set rep clause in place
996ae0b0
RK
6356
6357 else
6358 Set_Has_Enumeration_Rep_Clause (Enumtype);
6359 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
6360 end if;
6361
6362 -- Now we process the aggregate. Note that we don't use the normal
6363 -- aggregate code for this purpose, because we don't want any of the
6364 -- normal expansion activities, and a number of special semantic
6365 -- rules apply (including the component type being any integer type)
6366
996ae0b0
RK
6367 Elit := First_Literal (Enumtype);
6368
6369 -- First the positional entries if any
6370
6371 if Present (Expressions (Aggr)) then
6372 Expr := First (Expressions (Aggr));
6373 while Present (Expr) loop
6374 if No (Elit) then
6375 Error_Msg_N ("too many entries in aggregate", Expr);
6376 return;
6377 end if;
6378
6379 Val := Static_Integer (Expr);
6380
27e6455d
TQ
6381 -- Err signals that we found some incorrect entries processing
6382 -- the list. The final checks for completeness and ordering are
6383 -- skipped in this case.
6384
996ae0b0
RK
6385 if Val = No_Uint then
6386 Err := True;
dc06dd83 6387
996ae0b0
RK
6388 elsif Val < Lo or else Hi < Val then
6389 Error_Msg_N ("value outside permitted range", Expr);
6390 Err := True;
6391 end if;
6392
6393 Set_Enumeration_Rep (Elit, Val);
6394 Set_Enumeration_Rep_Expr (Elit, Expr);
6395 Next (Expr);
6396 Next (Elit);
6397 end loop;
6398 end if;
6399
6400 -- Now process the named entries if present
6401
6402 if Present (Component_Associations (Aggr)) then
6403 Assoc := First (Component_Associations (Aggr));
6404 while Present (Assoc) loop
6405 Choice := First (Choices (Assoc));
6406
6407 if Present (Next (Choice)) then
6408 Error_Msg_N
6409 ("multiple choice not allowed here", Next (Choice));
6410 Err := True;
6411 end if;
6412
6413 if Nkind (Choice) = N_Others_Choice then
6414 Error_Msg_N ("others choice not allowed here", Choice);
6415 Err := True;
6416
6417 elsif Nkind (Choice) = N_Range then
2c1b72d7 6418
996ae0b0 6419 -- ??? should allow zero/one element range here
2c1b72d7 6420
996ae0b0
RK
6421 Error_Msg_N ("range not allowed here", Choice);
6422 Err := True;
6423
6424 else
6425 Analyze_And_Resolve (Choice, Enumtype);
2c1b72d7 6426
686d0984 6427 if Error_Posted (Choice) then
996ae0b0 6428 Err := True;
686d0984 6429 end if;
996ae0b0 6430
686d0984
AC
6431 if not Err then
6432 if Is_Entity_Name (Choice)
6433 and then Is_Type (Entity (Choice))
6434 then
6435 Error_Msg_N ("subtype name not allowed here", Choice);
996ae0b0 6436 Err := True;
2c1b72d7 6437
686d0984 6438 -- ??? should allow static subtype with zero/one entry
996ae0b0 6439
686d0984 6440 elsif Etype (Choice) = Base_Type (Enumtype) then
edab6088 6441 if not Is_OK_Static_Expression (Choice) then
686d0984
AC
6442 Flag_Non_Static_Expr
6443 ("non-static expression used for choice!", Choice);
996ae0b0 6444 Err := True;
996ae0b0 6445
686d0984
AC
6446 else
6447 Elit := Expr_Value_E (Choice);
6448
6449 if Present (Enumeration_Rep_Expr (Elit)) then
6450 Error_Msg_Sloc :=
6451 Sloc (Enumeration_Rep_Expr (Elit));
6452 Error_Msg_NE
6453 ("representation for& previously given#",
6454 Choice, Elit);
6455 Err := True;
6456 end if;
996ae0b0 6457
686d0984 6458 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
996ae0b0 6459
686d0984
AC
6460 Expr := Expression (Assoc);
6461 Val := Static_Integer (Expr);
996ae0b0 6462
686d0984
AC
6463 if Val = No_Uint then
6464 Err := True;
6465
6466 elsif Val < Lo or else Hi < Val then
6467 Error_Msg_N ("value outside permitted range", Expr);
6468 Err := True;
6469 end if;
996ae0b0 6470
686d0984
AC
6471 Set_Enumeration_Rep (Elit, Val);
6472 end if;
996ae0b0
RK
6473 end if;
6474 end if;
6475 end if;
6476
6477 Next (Assoc);
6478 end loop;
6479 end if;
6480
6481 -- Aggregate is fully processed. Now we check that a full set of
6482 -- representations was given, and that they are in range and in order.
6483 -- These checks are only done if no other errors occurred.
6484
6485 if not Err then
6486 Min := No_Uint;
6487 Max := No_Uint;
6488
6489 Elit := First_Literal (Enumtype);
6490 while Present (Elit) loop
6491 if No (Enumeration_Rep_Expr (Elit)) then
6492 Error_Msg_NE ("missing representation for&!", N, Elit);
6493
6494 else
6495 Val := Enumeration_Rep (Elit);
6496
6497 if Min = No_Uint then
6498 Min := Val;
6499 end if;
6500
6501 if Val /= No_Uint then
6502 if Max /= No_Uint and then Val <= Max then
6503 Error_Msg_NE
6504 ("enumeration value for& not ordered!",
d69cf005 6505 Enumeration_Rep_Expr (Elit), Elit);
996ae0b0
RK
6506 end if;
6507
d69cf005 6508 Max_Node := Enumeration_Rep_Expr (Elit);
996ae0b0
RK
6509 Max := Val;
6510 end if;
6511
d69cf005
AC
6512 -- If there is at least one literal whose representation is not
6513 -- equal to the Pos value, then note that this enumeration type
6514 -- has a non-standard representation.
996ae0b0
RK
6515
6516 if Val /= Enumeration_Pos (Elit) then
6517 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
6518 end if;
6519 end if;
6520
6521 Next (Elit);
6522 end loop;
6523
6524 -- Now set proper size information
6525
6526 declare
6527 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
6528
6529 begin
6530 if Has_Size_Clause (Enumtype) then
d69cf005
AC
6531
6532 -- All OK, if size is OK now
6533
6534 if RM_Size (Enumtype) >= Minsize then
996ae0b0
RK
6535 null;
6536
6537 else
d69cf005
AC
6538 -- Try if we can get by with biasing
6539
996ae0b0
RK
6540 Minsize :=
6541 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
6542
d69cf005
AC
6543 -- Error message if even biasing does not work
6544
6545 if RM_Size (Enumtype) < Minsize then
6546 Error_Msg_Uint_1 := RM_Size (Enumtype);
6547 Error_Msg_Uint_2 := Max;
6548 Error_Msg_N
6549 ("previously given size (^) is too small "
6550 & "for this value (^)", Max_Node);
6551
6552 -- If biasing worked, indicate that we now have biased rep
996ae0b0
RK
6553
6554 else
a3f2babd
AC
6555 Set_Biased
6556 (Enumtype, Size_Clause (Enumtype), "size clause");
996ae0b0
RK
6557 end if;
6558 end if;
6559
6560 else
6561 Set_RM_Size (Enumtype, Minsize);
6562 Set_Enum_Esize (Enumtype);
6563 end if;
6564
6565 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
6566 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
6567 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
6568 end;
6569 end if;
6570
a90bd866 6571 -- We repeat the too late test in case it froze itself
996ae0b0
RK
6572
6573 if Rep_Item_Too_Late (Enumtype, N) then
6574 null;
6575 end if;
996ae0b0
RK
6576 end Analyze_Enumeration_Representation_Clause;
6577
6578 ----------------------------
6579 -- Analyze_Free_Statement --
6580 ----------------------------
6581
6582 procedure Analyze_Free_Statement (N : Node_Id) is
6583 begin
6584 Analyze (Expression (N));
6585 end Analyze_Free_Statement;
6586
3ff38f33
JM
6587 ---------------------------
6588 -- Analyze_Freeze_Entity --
6589 ---------------------------
6590
6591 procedure Analyze_Freeze_Entity (N : Node_Id) is
3ff38f33 6592 begin
5a8a6763
RD
6593 Freeze_Entity_Checks (N);
6594 end Analyze_Freeze_Entity;
947430d5 6595
5a8a6763
RD
6596 -----------------------------------
6597 -- Analyze_Freeze_Generic_Entity --
6598 -----------------------------------
947430d5 6599
5a8a6763
RD
6600 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
6601 begin
6602 Freeze_Entity_Checks (N);
6603 end Analyze_Freeze_Generic_Entity;
3ff38f33 6604
5a8a6763
RD
6605 ------------------------------------------
6606 -- Analyze_Record_Representation_Clause --
6607 ------------------------------------------
b4d7b435 6608
5a8a6763
RD
6609 -- Note: we check as much as we can here, but we can't do any checks
6610 -- based on the position values (e.g. overlap checks) until freeze time
6611 -- because especially in Ada 2005 (machine scalar mode), the processing
6612 -- for non-standard bit order can substantially change the positions.
6613 -- See procedure Check_Record_Representation_Clause (called from Freeze)
6614 -- for the remainder of this processing.
cefce34c 6615
5a8a6763
RD
6616 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
6617 Ident : constant Node_Id := Identifier (N);
6618 Biased : Boolean;
6619 CC : Node_Id;
6620 Comp : Entity_Id;
6621 Fbit : Uint;
6622 Hbit : Uint := Uint_0;
6623 Lbit : Uint;
6624 Ocomp : Entity_Id;
6625 Posit : Uint;
6626 Rectype : Entity_Id;
6627 Recdef : Node_Id;
cefce34c 6628
5a8a6763
RD
6629 function Is_Inherited (Comp : Entity_Id) return Boolean;
6630 -- True if Comp is an inherited component in a record extension
cefce34c 6631
5a8a6763
RD
6632 ------------------
6633 -- Is_Inherited --
6634 ------------------
cefce34c 6635
5a8a6763
RD
6636 function Is_Inherited (Comp : Entity_Id) return Boolean is
6637 Comp_Base : Entity_Id;
cefce34c 6638
5a8a6763
RD
6639 begin
6640 if Ekind (Rectype) = E_Record_Subtype then
6641 Comp_Base := Original_Record_Component (Comp);
6642 else
6643 Comp_Base := Comp;
cefce34c
JM
6644 end if;
6645
5a8a6763
RD
6646 return Comp_Base /= Original_Record_Component (Comp_Base);
6647 end Is_Inherited;
cefce34c 6648
5a8a6763 6649 -- Local variables
cefce34c 6650
5a8a6763
RD
6651 Is_Record_Extension : Boolean;
6652 -- True if Rectype is a record extension
cefce34c 6653
5a8a6763
RD
6654 CR_Pragma : Node_Id := Empty;
6655 -- Points to N_Pragma node if Complete_Representation pragma present
cefce34c 6656
5a8a6763 6657 -- Start of processing for Analyze_Record_Representation_Clause
cefce34c 6658
5a8a6763
RD
6659 begin
6660 if Ignore_Rep_Clauses then
cf28c974 6661 Kill_Rep_Clause (N);
5a8a6763 6662 return;
cefce34c 6663 end if;
947430d5 6664
5a8a6763
RD
6665 Find_Type (Ident);
6666 Rectype := Entity (Ident);
70805b88 6667
5a8a6763
RD
6668 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
6669 return;
6670 else
6671 Rectype := Underlying_Type (Rectype);
6672 end if;
70805b88 6673
5a8a6763 6674 -- First some basic error checks
70805b88 6675
5a8a6763
RD
6676 if not Is_Record_Type (Rectype) then
6677 Error_Msg_NE
6678 ("record type required, found}", Ident, First_Subtype (Rectype));
6679 return;
70805b88 6680
5a8a6763
RD
6681 elsif Scope (Rectype) /= Current_Scope then
6682 Error_Msg_N ("type must be declared in this scope", N);
6683 return;
70805b88 6684
5a8a6763
RD
6685 elsif not Is_First_Subtype (Rectype) then
6686 Error_Msg_N ("cannot give record rep clause for subtype", N);
6687 return;
86200f66 6688
5a8a6763
RD
6689 elsif Has_Record_Rep_Clause (Rectype) then
6690 Error_Msg_N ("duplicate record rep clause ignored", N);
6691 return;
86200f66 6692
5a8a6763
RD
6693 elsif Rep_Item_Too_Late (Rectype, N) then
6694 return;
86200f66 6695 end if;
bd949ee2 6696
7d9880c9 6697 -- We know we have a first subtype, now possibly go to the anonymous
5a8a6763 6698 -- base type to determine whether Rectype is a record extension.
b98e2969 6699
5a8a6763
RD
6700 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
6701 Is_Record_Extension :=
6702 Nkind (Recdef) = N_Derived_Type_Definition
6703 and then Present (Record_Extension_Part (Recdef));
b98e2969 6704
5a8a6763 6705 if Present (Mod_Clause (N)) then
bd949ee2 6706 declare
5a8a6763
RD
6707 Loc : constant Source_Ptr := Sloc (N);
6708 M : constant Node_Id := Mod_Clause (N);
6709 P : constant List_Id := Pragmas_Before (M);
6710 AtM_Nod : Node_Id;
6711
6712 Mod_Val : Uint;
6713 pragma Warnings (Off, Mod_Val);
bd949ee2
RD
6714
6715 begin
5a8a6763 6716 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
bd949ee2 6717
5a8a6763
RD
6718 if Warn_On_Obsolescent_Feature then
6719 Error_Msg_N
6720 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
6721 Error_Msg_N
6722 ("\?j?use alignment attribute definition clause instead", N);
6723 end if;
bd949ee2 6724
5a8a6763
RD
6725 if Present (P) then
6726 Analyze_List (P);
6727 end if;
b98e2969 6728
5a8a6763
RD
6729 -- In ASIS_Mode mode, expansion is disabled, but we must convert
6730 -- the Mod clause into an alignment clause anyway, so that the
8a0183fd 6731 -- back end can compute and back-annotate properly the size and
5a8a6763 6732 -- alignment of types that may include this record.
c76bf0bf 6733
5a8a6763
RD
6734 -- This seems dubious, this destroys the source tree in a manner
6735 -- not detectable by ASIS ???
c76bf0bf 6736
5a8a6763
RD
6737 if Operating_Mode = Check_Semantics and then ASIS_Mode then
6738 AtM_Nod :=
6739 Make_Attribute_Definition_Clause (Loc,
e4494292 6740 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
5a8a6763
RD
6741 Chars => Name_Alignment,
6742 Expression => Relocate_Node (Expression (M)));
c76bf0bf 6743
5a8a6763
RD
6744 Set_From_At_Mod (AtM_Nod);
6745 Insert_After (N, AtM_Nod);
6746 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
6747 Set_Mod_Clause (N, Empty);
c76bf0bf 6748
5a8a6763
RD
6749 else
6750 -- Get the alignment value to perform error checking
c76bf0bf 6751
5a8a6763
RD
6752 Mod_Val := Get_Alignment_Value (Expression (M));
6753 end if;
6754 end;
6755 end if;
c76bf0bf 6756
5a8a6763
RD
6757 -- For untagged types, clear any existing component clauses for the
6758 -- type. If the type is derived, this is what allows us to override
6759 -- a rep clause for the parent. For type extensions, the representation
6760 -- of the inherited components is inherited, so we want to keep previous
6761 -- component clauses for completeness.
c76bf0bf 6762
5a8a6763
RD
6763 if not Is_Tagged_Type (Rectype) then
6764 Comp := First_Component_Or_Discriminant (Rectype);
6765 while Present (Comp) loop
6766 Set_Component_Clause (Comp, Empty);
6767 Next_Component_Or_Discriminant (Comp);
6768 end loop;
6769 end if;
c76bf0bf 6770
5a8a6763 6771 -- All done if no component clauses
c76bf0bf 6772
5a8a6763 6773 CC := First (Component_Clauses (N));
c76bf0bf 6774
5a8a6763
RD
6775 if No (CC) then
6776 return;
6777 end if;
c76bf0bf 6778
5a8a6763 6779 -- A representation like this applies to the base type
c76bf0bf 6780
5a8a6763
RD
6781 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
6782 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
6783 Set_Has_Specified_Layout (Base_Type (Rectype));
c76bf0bf 6784
5a8a6763 6785 -- Process the component clauses
c76bf0bf 6786
5a8a6763 6787 while Present (CC) loop
c76bf0bf 6788
5a8a6763 6789 -- Pragma
c76bf0bf 6790
5a8a6763
RD
6791 if Nkind (CC) = N_Pragma then
6792 Analyze (CC);
c76bf0bf 6793
5a8a6763 6794 -- The only pragma of interest is Complete_Representation
c76bf0bf 6795
5a8a6763
RD
6796 if Pragma_Name (CC) = Name_Complete_Representation then
6797 CR_Pragma := CC;
6798 end if;
c76bf0bf 6799
5a8a6763 6800 -- Processing for real component clause
c76bf0bf 6801
5a8a6763
RD
6802 else
6803 Posit := Static_Integer (Position (CC));
6804 Fbit := Static_Integer (First_Bit (CC));
6805 Lbit := Static_Integer (Last_Bit (CC));
c76bf0bf 6806
5a8a6763
RD
6807 if Posit /= No_Uint
6808 and then Fbit /= No_Uint
6809 and then Lbit /= No_Uint
6810 then
6811 if Posit < 0 then
32b794c8 6812 Error_Msg_N ("position cannot be negative", Position (CC));
c76bf0bf 6813
5a8a6763 6814 elsif Fbit < 0 then
32b794c8 6815 Error_Msg_N ("first bit cannot be negative", First_Bit (CC));
c76bf0bf 6816
5a8a6763
RD
6817 -- The Last_Bit specified in a component clause must not be
6818 -- less than the First_Bit minus one (RM-13.5.1(10)).
c76bf0bf 6819
5a8a6763
RD
6820 elsif Lbit < Fbit - 1 then
6821 Error_Msg_N
6822 ("last bit cannot be less than first bit minus one",
6823 Last_Bit (CC));
c76bf0bf 6824
5a8a6763
RD
6825 -- Values look OK, so find the corresponding record component
6826 -- Even though the syntax allows an attribute reference for
6827 -- implementation-defined components, GNAT does not allow the
6828 -- tag to get an explicit position.
c76bf0bf 6829
5a8a6763
RD
6830 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
6831 if Attribute_Name (Component_Name (CC)) = Name_Tag then
6832 Error_Msg_N ("position of tag cannot be specified", CC);
6833 else
6834 Error_Msg_N ("illegal component name", CC);
6835 end if;
c76bf0bf 6836
5a8a6763
RD
6837 else
6838 Comp := First_Entity (Rectype);
6839 while Present (Comp) loop
6840 exit when Chars (Comp) = Chars (Component_Name (CC));
6841 Next_Entity (Comp);
6842 end loop;
c76bf0bf 6843
5a8a6763 6844 if No (Comp) then
c76bf0bf 6845
5a8a6763
RD
6846 -- Maybe component of base type that is absent from
6847 -- statically constrained first subtype.
c76bf0bf 6848
5a8a6763
RD
6849 Comp := First_Entity (Base_Type (Rectype));
6850 while Present (Comp) loop
6851 exit when Chars (Comp) = Chars (Component_Name (CC));
6852 Next_Entity (Comp);
6853 end loop;
6854 end if;
c76bf0bf 6855
5a8a6763
RD
6856 if No (Comp) then
6857 Error_Msg_N
6858 ("component clause is for non-existent field", CC);
c76bf0bf 6859
5a8a6763
RD
6860 -- Ada 2012 (AI05-0026): Any name that denotes a
6861 -- discriminant of an object of an unchecked union type
6862 -- shall not occur within a record_representation_clause.
c76bf0bf 6863
5a8a6763
RD
6864 -- The general restriction of using record rep clauses on
6865 -- Unchecked_Union types has now been lifted. Since it is
6866 -- possible to introduce a record rep clause which mentions
6867 -- the discriminant of an Unchecked_Union in non-Ada 2012
6868 -- code, this check is applied to all versions of the
6869 -- language.
c76bf0bf 6870
5a8a6763
RD
6871 elsif Ekind (Comp) = E_Discriminant
6872 and then Is_Unchecked_Union (Rectype)
6873 then
6874 Error_Msg_N
6875 ("cannot reference discriminant of unchecked union",
6876 Component_Name (CC));
c76bf0bf 6877
5a8a6763
RD
6878 elsif Is_Record_Extension and then Is_Inherited (Comp) then
6879 Error_Msg_NE
6880 ("component clause not allowed for inherited "
6881 & "component&", CC, Comp);
3ff38f33 6882
5a8a6763 6883 elsif Present (Component_Clause (Comp)) then
3cd4a210 6884
5a8a6763
RD
6885 -- Diagnose duplicate rep clause, or check consistency
6886 -- if this is an inherited component. In a double fault,
6887 -- there may be a duplicate inconsistent clause for an
6888 -- inherited component.
3cd4a210 6889
5a8a6763
RD
6890 if Scope (Original_Record_Component (Comp)) = Rectype
6891 or else Parent (Component_Clause (Comp)) = N
6892 then
6893 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
6894 Error_Msg_N ("component clause previously given#", CC);
800621e0
RD
6895
6896 else
6897 declare
6898 Rep1 : constant Node_Id := Component_Clause (Comp);
800621e0
RD
6899 begin
6900 if Intval (Position (Rep1)) /=
6901 Intval (Position (CC))
6902 or else Intval (First_Bit (Rep1)) /=
6903 Intval (First_Bit (CC))
6904 or else Intval (Last_Bit (Rep1)) /=
6905 Intval (Last_Bit (CC))
6906 then
616547fa 6907 Error_Msg_N
32b794c8
AC
6908 ("component clause inconsistent with "
6909 & "representation of ancestor", CC);
8190087e 6910
800621e0 6911 elsif Warn_On_Redundant_Constructs then
616547fa 6912 Error_Msg_N
8190087e
AC
6913 ("?r?redundant confirming component clause "
6914 & "for component!", CC);
800621e0
RD
6915 end if;
6916 end;
6917 end if;
996ae0b0 6918
6fb4cdde
AC
6919 -- Normal case where this is the first component clause we
6920 -- have seen for this entity, so set it up properly.
6921
996ae0b0 6922 else
2642f998
RD
6923 -- Make reference for field in record rep clause and set
6924 -- appropriate entity field in the field identifier.
6925
6926 Generate_Reference
6927 (Comp, Component_Name (CC), Set_Ref => False);
6928 Set_Entity (Component_Name (CC), Comp);
6929
a5b62485 6930 -- Update Fbit and Lbit to the actual bit number
996ae0b0
RK
6931
6932 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6933 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6934
996ae0b0 6935 if Has_Size_Clause (Rectype)
fc893455 6936 and then RM_Size (Rectype) <= Lbit
996ae0b0
RK
6937 then
6938 Error_Msg_N
6939 ("bit number out of range of specified size",
6940 Last_Bit (CC));
6941 else
6942 Set_Component_Clause (Comp, CC);
6943 Set_Component_Bit_Offset (Comp, Fbit);
6944 Set_Esize (Comp, 1 + (Lbit - Fbit));
6945 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6946 Set_Normalized_Position (Comp, Fbit / SSU);
6947
b3f48fd4
AC
6948 if Warn_On_Overridden_Size
6949 and then Has_Size_Clause (Etype (Comp))
6950 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6951 then
6952 Error_Msg_NE
dbfeb4fa 6953 ("?S?component size overrides size clause for&",
b3f48fd4
AC
6954 Component_Name (CC), Etype (Comp));
6955 end if;
6956
d05ef0ab
AC
6957 -- This information is also set in the corresponding
6958 -- component of the base type, found by accessing the
6959 -- Original_Record_Component link if it is present.
996ae0b0
RK
6960
6961 Ocomp := Original_Record_Component (Comp);
6962
6963 if Hbit < Lbit then
6964 Hbit := Lbit;
6965 end if;
6966
6967 Check_Size
6968 (Component_Name (CC),
6969 Etype (Comp),
6970 Esize (Comp),
6971 Biased);
6972
a3f2babd
AC
6973 Set_Biased
6974 (Comp, First_Node (CC), "component clause", Biased);
4ae23b62 6975
996ae0b0
RK
6976 if Present (Ocomp) then
6977 Set_Component_Clause (Ocomp, CC);
6978 Set_Component_Bit_Offset (Ocomp, Fbit);
6979 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6980 Set_Normalized_Position (Ocomp, Fbit / SSU);
6981 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6982
6983 Set_Normalized_Position_Max
6984 (Ocomp, Normalized_Position (Ocomp));
6985
a3f2babd
AC
6986 -- Note: we don't use Set_Biased here, because we
6987 -- already gave a warning above if needed, and we
6988 -- would get a duplicate for the same name here.
6989
996ae0b0
RK
6990 Set_Has_Biased_Representation
6991 (Ocomp, Has_Biased_Representation (Comp));
6992 end if;
6993
6994 if Esize (Comp) < 0 then
6995 Error_Msg_N ("component size is negative", CC);
6996 end if;
6997 end if;
6998 end if;
6999 end if;
7000 end if;
7001 end if;
7002
7003 Next (CC);
7004 end loop;
7005
8a95f4e8 7006 -- Check missing components if Complete_Representation pragma appeared
996ae0b0 7007
8a95f4e8
RD
7008 if Present (CR_Pragma) then
7009 Comp := First_Component_Or_Discriminant (Rectype);
7010 while Present (Comp) loop
7011 if No (Component_Clause (Comp)) then
7012 Error_Msg_NE
7013 ("missing component clause for &", CR_Pragma, Comp);
7014 end if;
996ae0b0 7015
8a95f4e8
RD
7016 Next_Component_Or_Discriminant (Comp);
7017 end loop;
996ae0b0 7018
dbfeb4fa 7019 -- Give missing components warning if required
8f7770f9 7020
0503c53a 7021 elsif Warn_On_Unrepped_Components then
8f7770f9
RD
7022 declare
7023 Num_Repped_Components : Nat := 0;
7024 Num_Unrepped_Components : Nat := 0;
7025
7026 begin
7027 -- First count number of repped and unrepped components
7028
7029 Comp := First_Component_Or_Discriminant (Rectype);
7030 while Present (Comp) loop
7031 if Present (Component_Clause (Comp)) then
7032 Num_Repped_Components := Num_Repped_Components + 1;
7033 else
7034 Num_Unrepped_Components := Num_Unrepped_Components + 1;
7035 end if;
7036
7037 Next_Component_Or_Discriminant (Comp);
7038 end loop;
7039
7040 -- We are only interested in the case where there is at least one
7041 -- unrepped component, and at least half the components have rep
7042 -- clauses. We figure that if less than half have them, then the
f5c064ab
ST
7043 -- partial rep clause is really intentional. If the component
7044 -- type has no underlying type set at this point (as for a generic
7045 -- formal type), we don't know enough to give a warning on the
7046 -- component.
8f7770f9
RD
7047
7048 if Num_Unrepped_Components > 0
7049 and then Num_Unrepped_Components < Num_Repped_Components
7050 then
7051 Comp := First_Component_Or_Discriminant (Rectype);
7052 while Present (Comp) loop
2642f998 7053 if No (Component_Clause (Comp))
800621e0 7054 and then Comes_From_Source (Comp)
f5c064ab 7055 and then Present (Underlying_Type (Etype (Comp)))
2642f998 7056 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
8a95f4e8
RD
7057 or else Size_Known_At_Compile_Time
7058 (Underlying_Type (Etype (Comp))))
0503c53a 7059 and then not Has_Warnings_Off (Rectype)
3fbbbd1e
AC
7060
7061 -- Ignore discriminant in unchecked union, since it is
7062 -- not there, and cannot have a component clause.
7063
7064 and then (not Is_Unchecked_Union (Rectype)
7065 or else Ekind (Comp) /= E_Discriminant)
2642f998 7066 then
8f7770f9
RD
7067 Error_Msg_Sloc := Sloc (Comp);
7068 Error_Msg_NE
dbfeb4fa 7069 ("?C?no component clause given for & declared #",
8f7770f9
RD
7070 N, Comp);
7071 end if;
7072
7073 Next_Component_Or_Discriminant (Comp);
7074 end loop;
7075 end if;
7076 end;
996ae0b0 7077 end if;
996ae0b0
RK
7078 end Analyze_Record_Representation_Clause;
7079
baa571ab
AC
7080 -------------------------------------
7081 -- Build_Discrete_Static_Predicate --
7082 -------------------------------------
95081e99 7083
baa571ab
AC
7084 procedure Build_Discrete_Static_Predicate
7085 (Typ : Entity_Id;
7086 Expr : Node_Id;
7087 Nam : Name_Id)
95081e99 7088 is
baa571ab 7089 Loc : constant Source_Ptr := Sloc (Expr);
95081e99 7090
baa571ab
AC
7091 Non_Static : exception;
7092 -- Raised if something non-static is found
95081e99 7093
baa571ab 7094 Btyp : constant Entity_Id := Base_Type (Typ);
95081e99 7095
baa571ab
AC
7096 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
7097 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
7098 -- Low bound and high bound value of base type of Typ
95081e99 7099
67c0e662
RD
7100 TLo : Uint;
7101 THi : Uint;
7102 -- Bounds for constructing the static predicate. We use the bound of the
7103 -- subtype if it is static, otherwise the corresponding base type bound.
7104 -- Note: a non-static subtype can have a static predicate.
95081e99 7105
baa571ab
AC
7106 type REnt is record
7107 Lo, Hi : Uint;
7108 end record;
7109 -- One entry in a Rlist value, a single REnt (range entry) value denotes
7110 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
7111 -- value.
95081e99 7112
baa571ab
AC
7113 type RList is array (Nat range <>) of REnt;
7114 -- A list of ranges. The ranges are sorted in increasing order, and are
7115 -- disjoint (there is a gap of at least one value between each range in
7116 -- the table). A value is in the set of ranges in Rlist if it lies
7117 -- within one of these ranges.
95081e99 7118
baa571ab
AC
7119 False_Range : constant RList :=
7120 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
7121 -- An empty set of ranges represents a range list that can never be
7122 -- satisfied, since there are no ranges in which the value could lie,
7123 -- so it does not lie in any of them. False_Range is a canonical value
7124 -- for this empty set, but general processing should test for an Rlist
7125 -- with length zero (see Is_False predicate), since other null ranges
7126 -- may appear which must be treated as False.
e606088a 7127
baa571ab
AC
7128 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
7129 -- Range representing True, value must be in the base range
e606088a 7130
baa571ab
AC
7131 function "and" (Left : RList; Right : RList) return RList;
7132 -- And's together two range lists, returning a range list. This is a set
7133 -- intersection operation.
e606088a 7134
baa571ab
AC
7135 function "or" (Left : RList; Right : RList) return RList;
7136 -- Or's together two range lists, returning a range list. This is a set
7137 -- union operation.
f2264ac2 7138
baa571ab
AC
7139 function "not" (Right : RList) return RList;
7140 -- Returns complement of a given range list, i.e. a range list
7141 -- representing all the values in TLo .. THi that are not in the input
7142 -- operand Right.
6ccdd977 7143
baa571ab
AC
7144 function Build_Val (V : Uint) return Node_Id;
7145 -- Return an analyzed N_Identifier node referencing this value, suitable
60f908dd
RD
7146 -- for use as an entry in the Static_Discrte_Predicate list. This node
7147 -- is typed with the base type.
e606088a 7148
baa571ab
AC
7149 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
7150 -- Return an analyzed N_Range node referencing this range, suitable for
60f908dd
RD
7151 -- use as an entry in the Static_Discrete_Predicate list. This node is
7152 -- typed with the base type.
e606088a 7153
baa571ab
AC
7154 function Get_RList (Exp : Node_Id) return RList;
7155 -- This is a recursive routine that converts the given expression into a
7156 -- list of ranges, suitable for use in building the static predicate.
e606088a 7157
baa571ab
AC
7158 function Is_False (R : RList) return Boolean;
7159 pragma Inline (Is_False);
7160 -- Returns True if the given range list is empty, and thus represents a
7161 -- False list of ranges that can never be satisfied.
f2264ac2 7162
baa571ab
AC
7163 function Is_True (R : RList) return Boolean;
7164 -- Returns True if R trivially represents the True predicate by having a
7165 -- single range from BLo to BHi.
e606088a 7166
baa571ab
AC
7167 function Is_Type_Ref (N : Node_Id) return Boolean;
7168 pragma Inline (Is_Type_Ref);
7169 -- Returns if True if N is a reference to the type for the predicate in
7170 -- the expression (i.e. if it is an identifier whose Chars field matches
623267dc
AC
7171 -- the Nam given in the call). N must not be parenthesized, if the type
7172 -- name appears in parens, this routine will return False.
e606088a 7173
baa571ab 7174 function Lo_Val (N : Node_Id) return Uint;
60f908dd
RD
7175 -- Given an entry from a Static_Discrete_Predicate list that is either
7176 -- a static expression or static range, gets either the expression value
7177 -- or the low bound of the range.
e606088a 7178
baa571ab 7179 function Hi_Val (N : Node_Id) return Uint;
60f908dd
RD
7180 -- Given an entry from a Static_Discrete_Predicate list that is either
7181 -- a static expression or static range, gets either the expression value
7182 -- or the high bound of the range.
e606088a 7183
baa571ab
AC
7184 function Membership_Entry (N : Node_Id) return RList;
7185 -- Given a single membership entry (range, value, or subtype), returns
7186 -- the corresponding range list. Raises Static_Error if not static.
e606088a 7187
baa571ab
AC
7188 function Membership_Entries (N : Node_Id) return RList;
7189 -- Given an element on an alternatives list of a membership operation,
7190 -- returns the range list corresponding to this entry and all following
7191 -- entries (i.e. returns the "or" of this list of values).
616547fa 7192
baa571ab
AC
7193 function Stat_Pred (Typ : Entity_Id) return RList;
7194 -- Given a type, if it has a static predicate, then return the predicate
7195 -- as a range list, otherwise raise Non_Static.
43417b90 7196
baa571ab
AC
7197 -----------
7198 -- "and" --
7199 -----------
43417b90 7200
baa571ab
AC
7201 function "and" (Left : RList; Right : RList) return RList is
7202 FEnt : REnt;
7203 -- First range of result
43417b90 7204
baa571ab
AC
7205 SLeft : Nat := Left'First;
7206 -- Start of rest of left entries
43417b90 7207
baa571ab
AC
7208 SRight : Nat := Right'First;
7209 -- Start of rest of right entries
3b097d11 7210
baa571ab
AC
7211 begin
7212 -- If either range is True, return the other
e606088a 7213
baa571ab
AC
7214 if Is_True (Left) then
7215 return Right;
7216 elsif Is_True (Right) then
7217 return Left;
7218 end if;
f2264ac2 7219
baa571ab 7220 -- If either range is False, return False
e606088a 7221
baa571ab
AC
7222 if Is_False (Left) or else Is_False (Right) then
7223 return False_Range;
7224 end if;
30ebb114 7225
baa571ab
AC
7226 -- Loop to remove entries at start that are disjoint, and thus just
7227 -- get discarded from the result entirely.
e606088a 7228
baa571ab
AC
7229 loop
7230 -- If no operands left in either operand, result is false
e606088a 7231
baa571ab
AC
7232 if SLeft > Left'Last or else SRight > Right'Last then
7233 return False_Range;
e606088a 7234
baa571ab 7235 -- Discard first left operand entry if disjoint with right
e606088a 7236
baa571ab
AC
7237 elsif Left (SLeft).Hi < Right (SRight).Lo then
7238 SLeft := SLeft + 1;
e606088a 7239
baa571ab 7240 -- Discard first right operand entry if disjoint with left
e606088a 7241
baa571ab
AC
7242 elsif Right (SRight).Hi < Left (SLeft).Lo then
7243 SRight := SRight + 1;
e606088a 7244
baa571ab 7245 -- Otherwise we have an overlapping entry
e606088a 7246
baa571ab
AC
7247 else
7248 exit;
7249 end if;
7250 end loop;
e606088a 7251
baa571ab
AC
7252 -- Now we have two non-null operands, and first entries overlap. The
7253 -- first entry in the result will be the overlapping part of these
7254 -- two entries.
c116143c 7255
baa571ab
AC
7256 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
7257 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
c116143c 7258
baa571ab
AC
7259 -- Now we can remove the entry that ended at a lower value, since its
7260 -- contribution is entirely contained in Fent.
e606088a 7261
baa571ab
AC
7262 if Left (SLeft).Hi <= Right (SRight).Hi then
7263 SLeft := SLeft + 1;
7264 else
7265 SRight := SRight + 1;
7266 end if;
e606088a 7267
baa571ab
AC
7268 -- Compute result by concatenating this first entry with the "and" of
7269 -- the remaining parts of the left and right operands. Note that if
7270 -- either of these is empty, "and" will yield empty, so that we will
7271 -- end up with just Fent, which is what we want in that case.
e606088a 7272
baa571ab
AC
7273 return
7274 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
7275 end "and";
bd949ee2 7276
baa571ab
AC
7277 -----------
7278 -- "not" --
7279 -----------
bd949ee2 7280
baa571ab
AC
7281 function "not" (Right : RList) return RList is
7282 begin
7283 -- Return True if False range
bd949ee2 7284
baa571ab
AC
7285 if Is_False (Right) then
7286 return True_Range;
7287 end if;
6ccdd977 7288
baa571ab 7289 -- Return False if True range
bd949ee2 7290
baa571ab
AC
7291 if Is_True (Right) then
7292 return False_Range;
7293 end if;
bd949ee2 7294
baa571ab 7295 -- Here if not trivial case
f2264ac2 7296
baa571ab
AC
7297 declare
7298 Result : RList (1 .. Right'Length + 1);
7299 -- May need one more entry for gap at beginning and end
f2264ac2 7300
baa571ab
AC
7301 Count : Nat := 0;
7302 -- Number of entries stored in Result
4a28b181 7303
baa571ab
AC
7304 begin
7305 -- Gap at start
4a28b181 7306
baa571ab
AC
7307 if Right (Right'First).Lo > TLo then
7308 Count := Count + 1;
7309 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
7310 end if;
6ccdd977 7311
baa571ab 7312 -- Gaps between ranges
6ccdd977 7313
baa571ab
AC
7314 for J in Right'First .. Right'Last - 1 loop
7315 Count := Count + 1;
7316 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
7317 end loop;
e606088a 7318
baa571ab 7319 -- Gap at end
e606088a 7320
baa571ab
AC
7321 if Right (Right'Last).Hi < THi then
7322 Count := Count + 1;
7323 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
7324 end if;
e606088a 7325
baa571ab
AC
7326 return Result (1 .. Count);
7327 end;
7328 end "not";
e606088a 7329
baa571ab
AC
7330 ----------
7331 -- "or" --
7332 ----------
e606088a 7333
baa571ab
AC
7334 function "or" (Left : RList; Right : RList) return RList is
7335 FEnt : REnt;
7336 -- First range of result
e606088a 7337
baa571ab
AC
7338 SLeft : Nat := Left'First;
7339 -- Start of rest of left entries
e606088a 7340
baa571ab
AC
7341 SRight : Nat := Right'First;
7342 -- Start of rest of right entries
e606088a 7343
baa571ab
AC
7344 begin
7345 -- If either range is True, return True
e606088a 7346
baa571ab
AC
7347 if Is_True (Left) or else Is_True (Right) then
7348 return True_Range;
7349 end if;
e606088a 7350
baa571ab 7351 -- If either range is False (empty), return the other
e606088a 7352
baa571ab
AC
7353 if Is_False (Left) then
7354 return Right;
7355 elsif Is_False (Right) then
7356 return Left;
7357 end if;
e606088a 7358
baa571ab
AC
7359 -- Initialize result first entry from left or right operand depending
7360 -- on which starts with the lower range.
e606088a 7361
baa571ab
AC
7362 if Left (SLeft).Lo < Right (SRight).Lo then
7363 FEnt := Left (SLeft);
7364 SLeft := SLeft + 1;
7365 else
7366 FEnt := Right (SRight);
7367 SRight := SRight + 1;
7368 end if;
e606088a 7369
baa571ab
AC
7370 -- This loop eats ranges from left and right operands that are
7371 -- contiguous with the first range we are gathering.
95081e99 7372
baa571ab
AC
7373 loop
7374 -- Eat first entry in left operand if contiguous or overlapped by
7375 -- gathered first operand of result.
95081e99 7376
baa571ab
AC
7377 if SLeft <= Left'Last
7378 and then Left (SLeft).Lo <= FEnt.Hi + 1
7379 then
7380 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
7381 SLeft := SLeft + 1;
95081e99 7382
baa571ab
AC
7383 -- Eat first entry in right operand if contiguous or overlapped by
7384 -- gathered right operand of result.
95081e99 7385
baa571ab
AC
7386 elsif SRight <= Right'Last
7387 and then Right (SRight).Lo <= FEnt.Hi + 1
7388 then
7389 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
7390 SRight := SRight + 1;
95081e99 7391
baa571ab 7392 -- All done if no more entries to eat
e606088a 7393
baa571ab
AC
7394 else
7395 exit;
7396 end if;
7397 end loop;
e606088a 7398
baa571ab
AC
7399 -- Obtain result as the first entry we just computed, concatenated
7400 -- to the "or" of the remaining results (if one operand is empty,
7401 -- this will just concatenate with the other
e606088a 7402
baa571ab
AC
7403 return
7404 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
7405 end "or";
e606088a 7406
baa571ab
AC
7407 -----------------
7408 -- Build_Range --
7409 -----------------
e606088a 7410
baa571ab
AC
7411 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
7412 Result : Node_Id;
e606088a 7413 begin
baa571ab
AC
7414 Result :=
7415 Make_Range (Loc,
7416 Low_Bound => Build_Val (Lo),
7417 High_Bound => Build_Val (Hi));
7418 Set_Etype (Result, Btyp);
7419 Set_Analyzed (Result);
7420 return Result;
7421 end Build_Range;
e606088a 7422
baa571ab
AC
7423 ---------------
7424 -- Build_Val --
7425 ---------------
e606088a 7426
baa571ab
AC
7427 function Build_Val (V : Uint) return Node_Id is
7428 Result : Node_Id;
e606088a 7429
baa571ab
AC
7430 begin
7431 if Is_Enumeration_Type (Typ) then
7432 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
7433 else
7434 Result := Make_Integer_Literal (Loc, V);
7435 end if;
e606088a 7436
baa571ab
AC
7437 Set_Etype (Result, Btyp);
7438 Set_Is_Static_Expression (Result);
7439 Set_Analyzed (Result);
7440 return Result;
7441 end Build_Val;
f2264ac2 7442
baa571ab
AC
7443 ---------------
7444 -- Get_RList --
7445 ---------------
f2264ac2 7446
baa571ab
AC
7447 function Get_RList (Exp : Node_Id) return RList is
7448 Op : Node_Kind;
7449 Val : Uint;
f2264ac2 7450
baa571ab
AC
7451 begin
7452 -- Static expression can only be true or false
f2264ac2 7453
baa571ab
AC
7454 if Is_OK_Static_Expression (Exp) then
7455 if Expr_Value (Exp) = 0 then
7456 return False_Range;
7457 else
7458 return True_Range;
95081e99 7459 end if;
baa571ab 7460 end if;
f2264ac2 7461
baa571ab 7462 -- Otherwise test node type
4172a8e3 7463
baa571ab 7464 Op := Nkind (Exp);
4172a8e3 7465
baa571ab 7466 case Op is
9e1902a9 7467
baa571ab 7468 -- And
9e1902a9 7469
baa571ab
AC
7470 when N_Op_And | N_And_Then =>
7471 return Get_RList (Left_Opnd (Exp))
7472 and
7473 Get_RList (Right_Opnd (Exp));
e606088a 7474
baa571ab 7475 -- Or
86200f66 7476
baa571ab
AC
7477 when N_Op_Or | N_Or_Else =>
7478 return Get_RList (Left_Opnd (Exp))
7479 or
7480 Get_RList (Right_Opnd (Exp));
c5a26133 7481
baa571ab 7482 -- Not
86200f66 7483
baa571ab
AC
7484 when N_Op_Not =>
7485 return not Get_RList (Right_Opnd (Exp));
86200f66 7486
baa571ab 7487 -- Comparisons of type with static value
fc142f63 7488
baa571ab 7489 when N_Op_Compare =>
f2acf80c 7490
baa571ab 7491 -- Type is left operand
86200f66 7492
baa571ab
AC
7493 if Is_Type_Ref (Left_Opnd (Exp))
7494 and then Is_OK_Static_Expression (Right_Opnd (Exp))
7495 then
7496 Val := Expr_Value (Right_Opnd (Exp));
fc142f63 7497
baa571ab 7498 -- Typ is right operand
fc142f63 7499
baa571ab
AC
7500 elsif Is_Type_Ref (Right_Opnd (Exp))
7501 and then Is_OK_Static_Expression (Left_Opnd (Exp))
7502 then
7503 Val := Expr_Value (Left_Opnd (Exp));
fc142f63 7504
baa571ab 7505 -- Invert sense of comparison
fc142f63 7506
baa571ab
AC
7507 case Op is
7508 when N_Op_Gt => Op := N_Op_Lt;
7509 when N_Op_Lt => Op := N_Op_Gt;
7510 when N_Op_Ge => Op := N_Op_Le;
7511 when N_Op_Le => Op := N_Op_Ge;
7512 when others => null;
7513 end case;
fc142f63 7514
baa571ab 7515 -- Other cases are non-static
42fdc85b 7516
baa571ab
AC
7517 else
7518 raise Non_Static;
7519 end if;
86200f66 7520
baa571ab 7521 -- Construct range according to comparison operation
86200f66 7522
baa571ab
AC
7523 case Op is
7524 when N_Op_Eq =>
7525 return RList'(1 => REnt'(Val, Val));
86200f66 7526
baa571ab
AC
7527 when N_Op_Ge =>
7528 return RList'(1 => REnt'(Val, BHi));
fc142f63 7529
baa571ab
AC
7530 when N_Op_Gt =>
7531 return RList'(1 => REnt'(Val + 1, BHi));
fc142f63 7532
baa571ab
AC
7533 when N_Op_Le =>
7534 return RList'(1 => REnt'(BLo, Val));
bd949ee2 7535
baa571ab
AC
7536 when N_Op_Lt =>
7537 return RList'(1 => REnt'(BLo, Val - 1));
86200f66 7538
baa571ab
AC
7539 when N_Op_Ne =>
7540 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
86200f66 7541
baa571ab
AC
7542 when others =>
7543 raise Program_Error;
7544 end case;
86200f66 7545
baa571ab 7546 -- Membership (IN)
86200f66 7547
baa571ab
AC
7548 when N_In =>
7549 if not Is_Type_Ref (Left_Opnd (Exp)) then
7550 raise Non_Static;
7551 end if;
86200f66 7552
baa571ab
AC
7553 if Present (Right_Opnd (Exp)) then
7554 return Membership_Entry (Right_Opnd (Exp));
7555 else
7556 return Membership_Entries (First (Alternatives (Exp)));
7557 end if;
86200f66 7558
baa571ab 7559 -- Negative membership (NOT IN)
86200f66 7560
baa571ab
AC
7561 when N_Not_In =>
7562 if not Is_Type_Ref (Left_Opnd (Exp)) then
7563 raise Non_Static;
7564 end if;
86200f66 7565
baa571ab
AC
7566 if Present (Right_Opnd (Exp)) then
7567 return not Membership_Entry (Right_Opnd (Exp));
7568 else
7569 return not Membership_Entries (First (Alternatives (Exp)));
7570 end if;
86200f66 7571
baa571ab 7572 -- Function call, may be call to static predicate
86200f66 7573
baa571ab
AC
7574 when N_Function_Call =>
7575 if Is_Entity_Name (Name (Exp)) then
7576 declare
7577 Ent : constant Entity_Id := Entity (Name (Exp));
7578 begin
7579 if Is_Predicate_Function (Ent)
7580 or else
7581 Is_Predicate_Function_M (Ent)
7582 then
7583 return Stat_Pred (Etype (First_Formal (Ent)));
7584 end if;
7585 end;
7586 end if;
86200f66 7587
baa571ab 7588 -- Other function call cases are non-static
86200f66 7589
baa571ab 7590 raise Non_Static;
f2acf80c 7591
baa571ab 7592 -- Qualified expression, dig out the expression
415450ea 7593
baa571ab
AC
7594 when N_Qualified_Expression =>
7595 return Get_RList (Expression (Exp));
30ebb114 7596
baa571ab
AC
7597 when N_Case_Expression =>
7598 declare
7599 Alt : Node_Id;
7600 Choices : List_Id;
7601 Dep : Node_Id;
30ebb114 7602
baa571ab
AC
7603 begin
7604 if not Is_Entity_Name (Expression (Expr))
7605 or else Etype (Expression (Expr)) /= Typ
7606 then
7607 Error_Msg_N
7608 ("expression must denaote subtype", Expression (Expr));
7609 return False_Range;
7610 end if;
86200f66 7611
baa571ab 7612 -- Collect discrete choices in all True alternatives
86200f66 7613
baa571ab
AC
7614 Choices := New_List;
7615 Alt := First (Alternatives (Exp));
7616 while Present (Alt) loop
7617 Dep := Expression (Alt);
42fdc85b 7618
edab6088 7619 if not Is_OK_Static_Expression (Dep) then
baa571ab 7620 raise Non_Static;
f1c952af 7621
baa571ab
AC
7622 elsif Is_True (Expr_Value (Dep)) then
7623 Append_List_To (Choices,
7624 New_Copy_List (Discrete_Choices (Alt)));
7625 end if;
bd949ee2 7626
baa571ab
AC
7627 Next (Alt);
7628 end loop;
86200f66 7629
baa571ab
AC
7630 return Membership_Entries (First (Choices));
7631 end;
86200f66 7632
baa571ab 7633 -- Expression with actions: if no actions, dig out expression
86200f66 7634
baa571ab
AC
7635 when N_Expression_With_Actions =>
7636 if Is_Empty_List (Actions (Exp)) then
7637 return Get_RList (Expression (Exp));
7638 else
7639 raise Non_Static;
7640 end if;
86200f66 7641
baa571ab 7642 -- Xor operator
f2acf80c 7643
baa571ab
AC
7644 when N_Op_Xor =>
7645 return (Get_RList (Left_Opnd (Exp))
7646 and not Get_RList (Right_Opnd (Exp)))
7647 or (Get_RList (Right_Opnd (Exp))
7648 and not Get_RList (Left_Opnd (Exp)));
86200f66 7649
baa571ab 7650 -- Any other node type is non-static
bd949ee2 7651
baa571ab
AC
7652 when others =>
7653 raise Non_Static;
7654 end case;
7655 end Get_RList;
bd949ee2 7656
baa571ab
AC
7657 ------------
7658 -- Hi_Val --
7659 ------------
bd949ee2 7660
baa571ab
AC
7661 function Hi_Val (N : Node_Id) return Uint is
7662 begin
edab6088 7663 if Is_OK_Static_Expression (N) then
baa571ab
AC
7664 return Expr_Value (N);
7665 else
7666 pragma Assert (Nkind (N) = N_Range);
7667 return Expr_Value (High_Bound (N));
7668 end if;
7669 end Hi_Val;
bd949ee2 7670
baa571ab
AC
7671 --------------
7672 -- Is_False --
7673 --------------
bd949ee2 7674
baa571ab
AC
7675 function Is_False (R : RList) return Boolean is
7676 begin
7677 return R'Length = 0;
7678 end Is_False;
86200f66 7679
baa571ab
AC
7680 -------------
7681 -- Is_True --
7682 -------------
86200f66 7683
baa571ab
AC
7684 function Is_True (R : RList) return Boolean is
7685 begin
7686 return R'Length = 1
7687 and then R (R'First).Lo = BLo
7688 and then R (R'First).Hi = BHi;
7689 end Is_True;
86200f66 7690
baa571ab
AC
7691 -----------------
7692 -- Is_Type_Ref --
7693 -----------------
86200f66 7694
baa571ab
AC
7695 function Is_Type_Ref (N : Node_Id) return Boolean is
7696 begin
623267dc
AC
7697 return Nkind (N) = N_Identifier
7698 and then Chars (N) = Nam
7699 and then Paren_Count (N) = 0;
baa571ab 7700 end Is_Type_Ref;
86200f66 7701
baa571ab
AC
7702 ------------
7703 -- Lo_Val --
7704 ------------
86200f66 7705
baa571ab 7706 function Lo_Val (N : Node_Id) return Uint is
fc142f63 7707 begin
edab6088 7708 if Is_OK_Static_Expression (N) then
baa571ab 7709 return Expr_Value (N);
fc142f63 7710 else
baa571ab
AC
7711 pragma Assert (Nkind (N) = N_Range);
7712 return Expr_Value (Low_Bound (N));
fc142f63 7713 end if;
baa571ab 7714 end Lo_Val;
1e194575 7715
baa571ab
AC
7716 ------------------------
7717 -- Membership_Entries --
7718 ------------------------
1e194575 7719
baa571ab 7720 function Membership_Entries (N : Node_Id) return RList is
fc142f63 7721 begin
baa571ab
AC
7722 if No (Next (N)) then
7723 return Membership_Entry (N);
fc142f63 7724 else
baa571ab 7725 return Membership_Entry (N) or Membership_Entries (Next (N));
fc142f63 7726 end if;
baa571ab 7727 end Membership_Entries;
fc142f63 7728
baa571ab
AC
7729 ----------------------
7730 -- Membership_Entry --
7731 ----------------------
fc142f63 7732
baa571ab
AC
7733 function Membership_Entry (N : Node_Id) return RList is
7734 Val : Uint;
7735 SLo : Uint;
7736 SHi : Uint;
1e194575 7737
baa571ab
AC
7738 begin
7739 -- Range case
1e194575 7740
baa571ab 7741 if Nkind (N) = N_Range then
edab6088 7742 if not Is_OK_Static_Expression (Low_Bound (N))
baa571ab 7743 or else
edab6088 7744 not Is_OK_Static_Expression (High_Bound (N))
baa571ab
AC
7745 then
7746 raise Non_Static;
7747 else
7748 SLo := Expr_Value (Low_Bound (N));
7749 SHi := Expr_Value (High_Bound (N));
7750 return RList'(1 => REnt'(SLo, SHi));
7751 end if;
fc142f63 7752
baa571ab 7753 -- Static expression case
fc142f63 7754
edab6088 7755 elsif Is_OK_Static_Expression (N) then
baa571ab
AC
7756 Val := Expr_Value (N);
7757 return RList'(1 => REnt'(Val, Val));
1e194575 7758
baa571ab 7759 -- Identifier (other than static expression) case
1e194575 7760
baa571ab 7761 else pragma Assert (Nkind (N) = N_Identifier);
1e194575 7762
baa571ab 7763 -- Type case
1e194575 7764
baa571ab 7765 if Is_Type (Entity (N)) then
1e194575 7766
baa571ab 7767 -- If type has predicates, process them
1e194575 7768
baa571ab
AC
7769 if Has_Predicates (Entity (N)) then
7770 return Stat_Pred (Entity (N));
1e194575 7771
baa571ab 7772 -- For static subtype without predicates, get range
86200f66 7773
edab6088 7774 elsif Is_OK_Static_Subtype (Entity (N)) then
baa571ab
AC
7775 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7776 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7777 return RList'(1 => REnt'(SLo, SHi));
de6e4fc4 7778
baa571ab 7779 -- Any other type makes us non-static
de6e4fc4 7780
baa571ab
AC
7781 else
7782 raise Non_Static;
7783 end if;
fc142f63 7784
baa571ab
AC
7785 -- Any other kind of identifier in predicate (e.g. a non-static
7786 -- expression value) means this is not a static predicate.
fc142f63 7787
baa571ab
AC
7788 else
7789 raise Non_Static;
7790 end if;
7791 end if;
7792 end Membership_Entry;
fc142f63 7793
baa571ab
AC
7794 ---------------
7795 -- Stat_Pred --
7796 ---------------
fc142f63 7797
baa571ab
AC
7798 function Stat_Pred (Typ : Entity_Id) return RList is
7799 begin
7800 -- Not static if type does not have static predicates
fc142f63 7801
60f908dd 7802 if not Has_Static_Predicate (Typ) then
baa571ab
AC
7803 raise Non_Static;
7804 end if;
fc142f63 7805
baa571ab 7806 -- Otherwise we convert the predicate list to a range list
fc142f63 7807
baa571ab 7808 declare
60f908dd
RD
7809 Spred : constant List_Id := Static_Discrete_Predicate (Typ);
7810 Result : RList (1 .. List_Length (Spred));
baa571ab 7811 P : Node_Id;
fc142f63 7812
baa571ab 7813 begin
60f908dd 7814 P := First (Static_Discrete_Predicate (Typ));
baa571ab
AC
7815 for J in Result'Range loop
7816 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7817 Next (P);
7818 end loop;
fc142f63 7819
baa571ab
AC
7820 return Result;
7821 end;
7822 end Stat_Pred;
fc142f63 7823
baa571ab 7824 -- Start of processing for Build_Discrete_Static_Predicate
fc142f63 7825
baa571ab 7826 begin
a2c314c7 7827 -- Establish bounds for the predicate
67c0e662
RD
7828
7829 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
7830 TLo := Expr_Value (Type_Low_Bound (Typ));
7831 else
7832 TLo := BLo;
7833 end if;
7834
7835 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
7836 THi := Expr_Value (Type_High_Bound (Typ));
7837 else
7838 THi := BHi;
7839 end if;
7840
baa571ab 7841 -- Analyze the expression to see if it is a static predicate
fc142f63 7842
baa571ab
AC
7843 declare
7844 Ranges : constant RList := Get_RList (Expr);
7845 -- Range list from expression if it is static
fc142f63 7846
baa571ab 7847 Plist : List_Id;
fc142f63 7848
baa571ab
AC
7849 begin
7850 -- Convert range list into a form for the static predicate. In the
7851 -- Ranges array, we just have raw ranges, these must be converted
7852 -- to properly typed and analyzed static expressions or range nodes.
fc142f63 7853
baa571ab
AC
7854 -- Note: here we limit ranges to the ranges of the subtype, so that
7855 -- a predicate is always false for values outside the subtype. That
7856 -- seems fine, such values are invalid anyway, and considering them
7857 -- to fail the predicate seems allowed and friendly, and furthermore
7858 -- simplifies processing for case statements and loops.
fc142f63 7859
baa571ab
AC
7860 Plist := New_List;
7861
7862 for J in Ranges'Range loop
fc142f63 7863 declare
baa571ab
AC
7864 Lo : Uint := Ranges (J).Lo;
7865 Hi : Uint := Ranges (J).Hi;
fc142f63 7866
baa571ab
AC
7867 begin
7868 -- Ignore completely out of range entry
fc142f63 7869
baa571ab
AC
7870 if Hi < TLo or else Lo > THi then
7871 null;
fc142f63 7872
baa571ab 7873 -- Otherwise process entry
fc142f63 7874
baa571ab
AC
7875 else
7876 -- Adjust out of range value to subtype range
f2acf80c 7877
baa571ab
AC
7878 if Lo < TLo then
7879 Lo := TLo;
7880 end if;
f2acf80c 7881
baa571ab
AC
7882 if Hi > THi then
7883 Hi := THi;
7884 end if;
fc142f63 7885
baa571ab 7886 -- Convert range into required form
fc142f63 7887
baa571ab 7888 Append_To (Plist, Build_Range (Lo, Hi));
fc142f63 7889 end if;
baa571ab
AC
7890 end;
7891 end loop;
fc142f63 7892
baa571ab
AC
7893 -- Processing was successful and all entries were static, so now we
7894 -- can store the result as the predicate list.
fc142f63 7895
60f908dd 7896 Set_Static_Discrete_Predicate (Typ, Plist);
fc142f63 7897
baa571ab
AC
7898 -- The processing for static predicates put the expression into
7899 -- canonical form as a series of ranges. It also eliminated
7900 -- duplicates and collapsed and combined ranges. We might as well
7901 -- replace the alternatives list of the right operand of the
7902 -- membership test with the static predicate list, which will
7903 -- usually be more efficient.
fc142f63 7904
baa571ab
AC
7905 declare
7906 New_Alts : constant List_Id := New_List;
7907 Old_Node : Node_Id;
7908 New_Node : Node_Id;
fc142f63 7909
baa571ab
AC
7910 begin
7911 Old_Node := First (Plist);
7912 while Present (Old_Node) loop
7913 New_Node := New_Copy (Old_Node);
fc142f63 7914
baa571ab
AC
7915 if Nkind (New_Node) = N_Range then
7916 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7917 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7918 end if;
fc142f63 7919
baa571ab
AC
7920 Append_To (New_Alts, New_Node);
7921 Next (Old_Node);
7922 end loop;
fc142f63 7923
baa571ab 7924 -- If empty list, replace by False
fc142f63 7925
baa571ab
AC
7926 if Is_Empty_List (New_Alts) then
7927 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
fc142f63 7928
baa571ab 7929 -- Else replace by set membership test
fc142f63 7930
baa571ab
AC
7931 else
7932 Rewrite (Expr,
7933 Make_In (Loc,
7934 Left_Opnd => Make_Identifier (Loc, Nam),
7935 Right_Opnd => Empty,
7936 Alternatives => New_Alts));
f2acf80c 7937
baa571ab 7938 -- Resolve new expression in function context
f2acf80c 7939
baa571ab
AC
7940 Install_Formals (Predicate_Function (Typ));
7941 Push_Scope (Predicate_Function (Typ));
7942 Analyze_And_Resolve (Expr, Standard_Boolean);
7943 Pop_Scope;
7944 end if;
7945 end;
7946 end;
3b1d4d82 7947
baa571ab 7948 -- If non-static, return doing nothing
3b1d4d82 7949
baa571ab
AC
7950 exception
7951 when Non_Static =>
7952 return;
7953 end Build_Discrete_Static_Predicate;
0d5fbf52 7954
2e885a6f
AC
7955 --------------------------------
7956 -- Build_Export_Import_Pragma --
7957 --------------------------------
7958
7959 function Build_Export_Import_Pragma
7960 (Asp : Node_Id;
7961 Id : Entity_Id) return Node_Id
7962 is
7963 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
7964 Expr : constant Node_Id := Expression (Asp);
7965 Loc : constant Source_Ptr := Sloc (Asp);
7966
7967 Args : List_Id;
7968 Conv : Node_Id;
7969 Conv_Arg : Node_Id;
7970 Dummy_1 : Node_Id;
7971 Dummy_2 : Node_Id;
7972 EN : Node_Id;
7973 LN : Node_Id;
7974 Prag : Node_Id;
7975
7976 Create_Pragma : Boolean := False;
7977 -- This flag is set when the aspect form is such that it warrants the
7978 -- creation of a corresponding pragma.
7979
7980 begin
7981 if Present (Expr) then
7982 if Error_Posted (Expr) then
7983 null;
7984
7985 elsif Is_True (Expr_Value (Expr)) then
7986 Create_Pragma := True;
7987 end if;
7988
7989 -- Otherwise the aspect defaults to True
7990
7991 else
7992 Create_Pragma := True;
7993 end if;
7994
7995 -- Nothing to do when the expression is False or is erroneous
7996
7997 if not Create_Pragma then
7998 return Empty;
7999 end if;
8000
8001 -- Obtain all interfacing aspects that apply to the related entity
8002
8003 Get_Interfacing_Aspects
8004 (Iface_Asp => Asp,
8005 Conv_Asp => Conv,
8006 EN_Asp => EN,
8007 Expo_Asp => Dummy_1,
8008 Imp_Asp => Dummy_2,
8009 LN_Asp => LN);
8010
8011 Args := New_List;
8012
8013 -- Handle the convention argument
8014
8015 if Present (Conv) then
8016 Conv_Arg := New_Copy_Tree (Expression (Conv));
8017
8018 -- Assume convention "Ada' when aspect Convention is missing
8019
8020 else
8021 Conv_Arg := Make_Identifier (Loc, Name_Ada);
8022 end if;
8023
8024 Append_To (Args,
8025 Make_Pragma_Argument_Association (Loc,
8026 Chars => Name_Convention,
8027 Expression => Conv_Arg));
8028
8029 -- Handle the entity argument
8030
8031 Append_To (Args,
8032 Make_Pragma_Argument_Association (Loc,
8033 Chars => Name_Entity,
8034 Expression => New_Occurrence_Of (Id, Loc)));
8035
8036 -- Handle the External_Name argument
8037
8038 if Present (EN) then
8039 Append_To (Args,
8040 Make_Pragma_Argument_Association (Loc,
8041 Chars => Name_External_Name,
8042 Expression => New_Copy_Tree (Expression (EN))));
8043 end if;
8044
8045 -- Handle the Link_Name argument
8046
8047 if Present (LN) then
8048 Append_To (Args,
8049 Make_Pragma_Argument_Association (Loc,
8050 Chars => Name_Link_Name,
8051 Expression => New_Copy_Tree (Expression (LN))));
8052 end if;
8053
8054 -- Generate:
8055 -- pragma Export/Import
8056 -- (Convention => <Conv>/Ada,
8057 -- Entity => <Id>,
8058 -- [External_Name => <EN>,]
8059 -- [Link_Name => <LN>]);
8060
8061 Prag :=
8062 Make_Pragma (Loc,
8063 Pragma_Identifier =>
8064 Make_Identifier (Loc, Chars (Identifier (Asp))),
8065 Pragma_Argument_Associations => Args);
8066
8067 -- Decorate the relevant aspect and the pragma
8068
8069 Set_Aspect_Rep_Item (Asp, Prag);
8070
8071 Set_Corresponding_Aspect (Prag, Asp);
8072 Set_From_Aspect_Specification (Prag);
8073 Set_Parent (Prag, Asp);
8074
8075 if Asp_Id = Aspect_Import and then Is_Subprogram (Id) then
8076 Set_Import_Pragma (Id, Prag);
8077 end if;
8078
8079 return Prag;
8080 end Build_Export_Import_Pragma;
8081
baa571ab
AC
8082 -------------------------------
8083 -- Build_Predicate_Functions --
8084 -------------------------------
5a8a6763 8085
baa571ab 8086 -- The procedures that are constructed here have the form:
5a8a6763 8087
baa571ab
AC
8088 -- function typPredicate (Ixxx : typ) return Boolean is
8089 -- begin
8090 -- return
9bdc432a 8091 -- typ1Predicate (typ1 (Ixxx))
baa571ab
AC
8092 -- and then typ2Predicate (typ2 (Ixxx))
8093 -- and then ...;
9bdc432a 8094 -- exp1 and then exp2 and then ...
baa571ab 8095 -- end typPredicate;
5a8a6763 8096
baa571ab
AC
8097 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
8098 -- this is the point at which these expressions get analyzed, providing the
8099 -- required delay, and typ1, typ2, are entities from which predicates are
8100 -- inherited. Note that we do NOT generate Check pragmas, that's because we
8101 -- use this function even if checks are off, e.g. for membership tests.
5a8a6763 8102
9bdc432a
AC
8103 -- Note that the inherited predicates are evaluated first, as required by
8104 -- AI12-0071-1.
8105
8106 -- Note that Sem_Eval.Real_Or_String_Static_Predicate_Matches depends on
8107 -- the form of this return expression.
8108
baa571ab
AC
8109 -- If the expression has at least one Raise_Expression, then we also build
8110 -- the typPredicateM version of the function, in which any occurrence of a
8111 -- Raise_Expression is converted to "return False".
5a8a6763 8112
baa571ab
AC
8113 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
8114 Loc : constant Source_Ptr := Sloc (Typ);
5a8a6763 8115
baa571ab
AC
8116 Expr : Node_Id;
8117 -- This is the expression for the result of the function. It is
8118 -- is build by connecting the component predicates with AND THEN.
5a8a6763 8119
baa571ab
AC
8120 Expr_M : Node_Id;
8121 -- This is the corresponding return expression for the Predicate_M
8122 -- function. It differs in that raise expressions are marked for
8123 -- special expansion (see Process_REs).
5a8a6763 8124
6905a049 8125 Object_Name : Name_Id;
baa571ab 8126 -- Name for argument of Predicate procedure. Note that we use the same
87e69720 8127 -- name for both predicate functions. That way the reference within the
baa571ab 8128 -- predicate expression is the same in both functions.
5a8a6763 8129
6905a049 8130 Object_Entity : Entity_Id;
baa571ab 8131 -- Entity for argument of Predicate procedure
5a8a6763 8132
6905a049
AC
8133 Object_Entity_M : Entity_Id;
8134 -- Entity for argument of separate Predicate procedure when exceptions
8135 -- are present in expression.
8136
2cc7967f
AC
8137 FDecl : Node_Id;
8138 -- The function declaration
6905a049 8139
2cc7967f
AC
8140 SId : Entity_Id;
8141 -- Its entity
5a8a6763 8142
baa571ab
AC
8143 Raise_Expression_Present : Boolean := False;
8144 -- Set True if Expr has at least one Raise_Expression
5a8a6763 8145
9bdc432a
AC
8146 procedure Add_Condition (Cond : Node_Id);
8147 -- Append Cond to Expr using "and then" (or just copy Cond to Expr if
8148 -- Expr is empty).
5a8a6763 8149
baa571ab
AC
8150 procedure Add_Predicates;
8151 -- Appends expressions for any Predicate pragmas in the rep item chain
8152 -- Typ to Expr. Note that we look only at items for this exact entity.
8153 -- Inheritance of predicates for the parent type is done by calling the
8154 -- Predicate_Function of the parent type, using Add_Call above.
5a8a6763 8155
9bdc432a
AC
8156 procedure Add_Call (T : Entity_Id);
8157 -- Includes a call to the predicate function for type T in Expr if T
8158 -- has predicates and Predicate_Function (T) is non-empty.
8159
baa571ab
AC
8160 function Process_RE (N : Node_Id) return Traverse_Result;
8161 -- Used in Process REs, tests if node N is a raise expression, and if
8162 -- so, marks it to be converted to return False.
5a8a6763 8163
baa571ab
AC
8164 procedure Process_REs is new Traverse_Proc (Process_RE);
8165 -- Marks any raise expressions in Expr_M to return False
5a8a6763 8166
241ebe89
HK
8167 function Test_RE (N : Node_Id) return Traverse_Result;
8168 -- Used in Test_REs, tests one node for being a raise expression, and if
8169 -- so sets Raise_Expression_Present True.
8170
8171 procedure Test_REs is new Traverse_Proc (Test_RE);
8172 -- Tests to see if Expr contains any raise expressions
8173
baa571ab
AC
8174 --------------
8175 -- Add_Call --
8176 --------------
5a8a6763 8177
baa571ab
AC
8178 procedure Add_Call (T : Entity_Id) is
8179 Exp : Node_Id;
5a8a6763 8180
baa571ab
AC
8181 begin
8182 if Present (T) and then Present (Predicate_Function (T)) then
8183 Set_Has_Predicates (Typ);
5a8a6763 8184
baa571ab 8185 -- Build the call to the predicate function of T
5a8a6763 8186
baa571ab
AC
8187 Exp :=
8188 Make_Predicate_Call
8189 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5a8a6763 8190
9bdc432a 8191 -- "and"-in the call to evolving expression
5a8a6763 8192
9bdc432a 8193 Add_Condition (Exp);
5a8a6763 8194
baa571ab
AC
8195 -- Output info message on inheritance if required. Note we do not
8196 -- give this information for generic actual types, since it is
8197 -- unwelcome noise in that case in instantiations. We also
8198 -- generally suppress the message in instantiations, and also
8199 -- if it involves internal names.
5a8a6763 8200
baa571ab
AC
8201 if Opt.List_Inherited_Aspects
8202 and then not Is_Generic_Actual_Type (Typ)
8203 and then Instantiation_Depth (Sloc (Typ)) = 0
8204 and then not Is_Internal_Name (Chars (T))
8205 and then not Is_Internal_Name (Chars (Typ))
8206 then
8207 Error_Msg_Sloc := Sloc (Predicate_Function (T));
8208 Error_Msg_Node_2 := T;
8209 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
8210 end if;
8211 end if;
8212 end Add_Call;
5a8a6763 8213
9bdc432a
AC
8214 -------------------
8215 -- Add_Condition --
8216 -------------------
8217
8218 procedure Add_Condition (Cond : Node_Id) is
8219 begin
8220 -- This is the first predicate expression
8221
8222 if No (Expr) then
8223 Expr := Cond;
8224
8225 -- Otherwise concatenate to the existing predicate expressions by
8226 -- using "and then".
8227
8228 else
8229 Expr :=
8230 Make_And_Then (Loc,
8231 Left_Opnd => Relocate_Node (Expr),
8232 Right_Opnd => Cond);
8233 end if;
8234 end Add_Condition;
8235
baa571ab
AC
8236 --------------------
8237 -- Add_Predicates --
8238 --------------------
5a8a6763 8239
baa571ab 8240 procedure Add_Predicates is
241ebe89
HK
8241 procedure Add_Predicate (Prag : Node_Id);
8242 -- Concatenate the expression of predicate pragma Prag to Expr by
8243 -- using a short circuit "and then" operator.
5a8a6763 8244
241ebe89
HK
8245 -------------------
8246 -- Add_Predicate --
8247 -------------------
5a8a6763 8248
241ebe89
HK
8249 procedure Add_Predicate (Prag : Node_Id) is
8250 procedure Replace_Type_Reference (N : Node_Id);
8251 -- Replace a single occurrence N of the subtype name with a
8252 -- reference to the formal of the predicate function. N can be an
8253 -- identifier referencing the subtype, or a selected component,
8254 -- representing an appropriately qualified occurrence of the
8255 -- subtype name.
8256
8257 procedure Replace_Type_References is
8258 new Replace_Type_References_Generic (Replace_Type_Reference);
8259 -- Traverse an expression changing every occurrence of an
8260 -- identifier whose name matches the name of the subtype with a
8261 -- reference to the formal parameter of the predicate function.
8262
8263 ----------------------------
8264 -- Replace_Type_Reference --
8265 ----------------------------
8266
8267 procedure Replace_Type_Reference (N : Node_Id) is
8268 begin
8269 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
8270 -- Use the Sloc of the usage name, not the defining name
5a8a6763 8271
241ebe89
HK
8272 Set_Etype (N, Typ);
8273 Set_Entity (N, Object_Entity);
1e194575 8274
241ebe89
HK
8275 -- We want to treat the node as if it comes from source, so
8276 -- that ASIS will not ignore it.
1e194575 8277
241ebe89
HK
8278 Set_Comes_From_Source (N, True);
8279 end Replace_Type_Reference;
1e194575 8280
241ebe89 8281 -- Local variables
1e194575 8282
241ebe89
HK
8283 Asp : constant Node_Id := Corresponding_Aspect (Prag);
8284 Arg1 : Node_Id;
8285 Arg2 : Node_Id;
1e194575 8286
241ebe89 8287 -- Start of processing for Add_Predicate
596b25f9 8288
241ebe89
HK
8289 begin
8290 -- Extract the arguments of the pragma. The expression itself
8291 -- is copied for use in the predicate function, to preserve the
8292 -- original version for ASIS use.
1e194575 8293
241ebe89
HK
8294 Arg1 := First (Pragma_Argument_Associations (Prag));
8295 Arg2 := Next (Arg1);
1e194575 8296
241ebe89
HK
8297 Arg1 := Get_Pragma_Arg (Arg1);
8298 Arg2 := New_Copy_Tree (Get_Pragma_Arg (Arg2));
1e194575 8299
241ebe89
HK
8300 -- When the predicate pragma applies to the current type or its
8301 -- full view, replace all occurrences of the subtype name with
8302 -- references to the formal parameter of the predicate function.
ff1f1705 8303
241ebe89
HK
8304 if Entity (Arg1) = Typ
8305 or else Full_View (Entity (Arg1)) = Typ
8306 then
8307 Replace_Type_References (Arg2, Typ);
ff1f1705 8308
241ebe89
HK
8309 -- If the predicate pragma comes from an aspect, replace the
8310 -- saved expression because we need the subtype references
8311 -- replaced for the calls to Preanalyze_Spec_Expression in
8312 -- Check_Aspect_At_xxx routines.
ff1f1705 8313
241ebe89 8314 if Present (Asp) then
241ebe89
HK
8315 Set_Entity (Identifier (Asp), New_Copy_Tree (Arg2));
8316 end if;
596b25f9 8317
9bdc432a 8318 -- "and"-in the Arg2 condition to evolving expression
ff1f1705 8319
9bdc432a 8320 Add_Condition (Relocate_Node (Arg2));
241ebe89
HK
8321 end if;
8322 end Add_Predicate;
064f4527 8323
241ebe89 8324 -- Local variables
064f4527 8325
241ebe89 8326 Ritem : Node_Id;
1e194575 8327
241ebe89 8328 -- Start of processing for Add_Predicates
1e194575 8329
241ebe89
HK
8330 begin
8331 Ritem := First_Rep_Item (Typ);
8332 while Present (Ritem) loop
8333 if Nkind (Ritem) = N_Pragma
8334 and then Pragma_Name (Ritem) = Name_Predicate
8335 then
8336 Add_Predicate (Ritem);
22a4f9d5
AC
8337
8338 -- If the type is declared in an inner package it may be frozen
8339 -- outside of the package, and the generated pragma has not been
8340 -- analyzed yet, so capture the expression for the predicate
8341 -- function at this point.
8342
8343 elsif Nkind (Ritem) = N_Aspect_Specification
f76647c2
AC
8344 and then Present (Aspect_Rep_Item (Ritem))
8345 and then Scope (Typ) /= Current_Scope
22a4f9d5
AC
8346 then
8347 declare
8348 Prag : constant Node_Id := Aspect_Rep_Item (Ritem);
8349
8350 begin
8351 if Nkind (Prag) = N_Pragma
8352 and then Pragma_Name (Prag) = Name_Predicate
8353 then
8354 Add_Predicate (Prag);
8355 end if;
8356 end;
baa571ab 8357 end if;
1e194575 8358
baa571ab
AC
8359 Next_Rep_Item (Ritem);
8360 end loop;
8361 end Add_Predicates;
1e194575 8362
baa571ab
AC
8363 ----------------
8364 -- Process_RE --
8365 ----------------
1e194575 8366
baa571ab 8367 function Process_RE (N : Node_Id) return Traverse_Result is
5a8a6763 8368 begin
baa571ab
AC
8369 if Nkind (N) = N_Raise_Expression then
8370 Set_Convert_To_Return_False (N);
8371 return Skip;
5a8a6763 8372 else
baa571ab 8373 return OK;
5a8a6763 8374 end if;
baa571ab 8375 end Process_RE;
f6b5dc8e 8376
5a8a6763 8377 -------------
baa571ab 8378 -- Test_RE --
5a8a6763 8379 -------------
f6b5dc8e 8380
baa571ab 8381 function Test_RE (N : Node_Id) return Traverse_Result is
1e194575 8382 begin
baa571ab
AC
8383 if Nkind (N) = N_Raise_Expression then
8384 Raise_Expression_Present := True;
8385 return Abandon;
8386 else
8387 return OK;
8388 end if;
8389 end Test_RE;
1e194575 8390
241ebe89
HK
8391 -- Local variables
8392
1af4455a 8393 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
241ebe89 8394
baa571ab 8395 -- Start of processing for Build_Predicate_Functions
1e194575 8396
baa571ab
AC
8397 begin
8398 -- Return if already built or if type does not have predicates
86200f66 8399
6905a049 8400 SId := Predicate_Function (Typ);
baa571ab 8401 if not Has_Predicates (Typ)
6905a049 8402 or else (Present (SId) and then Has_Completion (SId))
baa571ab
AC
8403 then
8404 return;
8405 end if;
5a8a6763 8406
1af4455a
HK
8407 -- The related type may be subject to pragma Ghost. Set the mode now to
8408 -- ensure that the predicate functions are properly marked as Ghost.
241ebe89
HK
8409
8410 Set_Ghost_Mode_From_Entity (Typ);
8411
baa571ab 8412 -- Prepare to construct predicate expression
1e194575 8413
baa571ab 8414 Expr := Empty;
1e194575 8415
6905a049
AC
8416 if Present (SId) then
8417 FDecl := Unit_Declaration_Node (SId);
8418
8419 else
8420 FDecl := Build_Predicate_Function_Declaration (Typ);
8421 SId := Defining_Entity (FDecl);
8422 end if;
8423
8424 -- Recover name of formal parameter of function that replaces references
8425 -- to the type in predicate expressions.
8426
8427 Object_Entity :=
8428 Defining_Identifier
8429 (First (Parameter_Specifications (Specification (FDecl))));
8430
8431 Object_Name := Chars (Object_Entity);
8432 Object_Entity_M := Make_Defining_Identifier (Loc, Chars => Object_Name);
8433
9bdc432a
AC
8434 -- Add predicates for ancestor if present. These must come before the
8435 -- ones for the current type, as required by AI12-0071-1.
1e194575 8436
baa571ab
AC
8437 declare
8438 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5a8a6763 8439 begin
baa571ab
AC
8440 if Present (Atyp) then
8441 Add_Call (Atyp);
8442 end if;
8443 end;
2cc7967f 8444
9bdc432a
AC
8445 -- Add Predicates for the current type
8446
8447 Add_Predicates;
8448
baa571ab 8449 -- Case where predicates are present
86200f66 8450
baa571ab 8451 if Present (Expr) then
752b81d9 8452
baa571ab 8453 -- Test for raise expression present
752b81d9 8454
baa571ab 8455 Test_REs (Expr);
86200f66 8456
baa571ab
AC
8457 -- If raise expression is present, capture a copy of Expr for use
8458 -- in building the predicateM function version later on. For this
8459 -- copy we replace references to Object_Entity by Object_Entity_M.
86200f66 8460
baa571ab
AC
8461 if Raise_Expression_Present then
8462 declare
5c5e108f
AC
8463 Map : constant Elist_Id := New_Elmt_List;
8464 New_V : Entity_Id := Empty;
8465
8466 -- The unanalyzed expression will be copied and appear in
8467 -- both functions. Normally expressions do not declare new
8468 -- entities, but quantified expressions do, so we need to
8469 -- create new entities for their bound variables, to prevent
8470 -- multiple definitions in gigi.
8471
8472 function Reset_Loop_Variable (N : Node_Id)
8473 return Traverse_Result;
8474
8475 procedure Collect_Loop_Variables is
8476 new Traverse_Proc (Reset_Loop_Variable);
8477
8478 ------------------------
8479 -- Reset_Loop_Variable --
8480 ------------------------
8481
8482 function Reset_Loop_Variable (N : Node_Id)
8483 return Traverse_Result
8484 is
8485 begin
8486 if Nkind (N) = N_Iterator_Specification then
8487 New_V := Make_Defining_Identifier
8488 (Sloc (N), Chars (Defining_Identifier (N)));
8489
8490 Set_Defining_Identifier (N, New_V);
8491 end if;
8492
8493 return OK;
8494 end Reset_Loop_Variable;
8495
baa571ab
AC
8496 begin
8497 Append_Elmt (Object_Entity, Map);
8498 Append_Elmt (Object_Entity_M, Map);
8499 Expr_M := New_Copy_Tree (Expr, Map => Map);
5c5e108f 8500 Collect_Loop_Variables (Expr_M);
baa571ab
AC
8501 end;
8502 end if;
1e194575 8503
baa571ab 8504 -- Build the main predicate function
86200f66 8505
baa571ab 8506 declare
baa571ab
AC
8507 SIdB : constant Entity_Id :=
8508 Make_Defining_Identifier (Loc,
8509 Chars => New_External_Name (Chars (Typ), "Predicate"));
8510 -- The entity for the function body
86200f66 8511
baa571ab 8512 Spec : Node_Id;
baa571ab 8513 FBody : Node_Id;
86200f66 8514
baa571ab 8515 begin
1e194575 8516
baa571ab 8517 -- The predicate function is shared between views of a type
1e194575 8518
baa571ab
AC
8519 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8520 Set_Predicate_Function (Full_View (Typ), SId);
1e194575 8521 end if;
1e194575 8522
241ebe89
HK
8523 -- Mark the predicate function explicitly as Ghost because it does
8524 -- not come from source.
8525
8526 if Ghost_Mode > None then
8527 Set_Is_Ghost_Entity (SId);
8528 end if;
8529
baa571ab 8530 -- Build function body
1e194575 8531
baa571ab
AC
8532 Spec :=
8533 Make_Function_Specification (Loc,
8534 Defining_Unit_Name => SIdB,
8535 Parameter_Specifications => New_List (
8536 Make_Parameter_Specification (Loc,
8537 Defining_Identifier =>
8538 Make_Defining_Identifier (Loc, Object_Name),
8539 Parameter_Type =>
8540 New_Occurrence_Of (Typ, Loc))),
8541 Result_Definition =>
8542 New_Occurrence_Of (Standard_Boolean, Loc));
1e194575 8543
baa571ab
AC
8544 FBody :=
8545 Make_Subprogram_Body (Loc,
8546 Specification => Spec,
8547 Declarations => Empty_List,
8548 Handled_Statement_Sequence =>
8549 Make_Handled_Sequence_Of_Statements (Loc,
8550 Statements => New_List (
8551 Make_Simple_Return_Statement (Loc,
8552 Expression => Expr))));
86200f66 8553
6905a049
AC
8554 -- If declaration has not been analyzed yet, Insert declaration
8555 -- before freeze node.
8556 -- Insert body after freeze node.
8557
8558 if not Analyzed (FDecl) then
8559 Insert_Before_And_Analyze (N, FDecl);
8560 end if;
1e194575 8561
2cc7967f 8562 Insert_After_And_Analyze (N, FBody);
e3d9f448
AC
8563
8564 -- Static predicate functions are always side-effect free, and
8565 -- in most cases dynamic predicate functions are as well. Mark
8566 -- them as such whenever possible, so redundant predicate checks
33398e3c
AC
8567 -- can be optimized. If there is a variable reference within the
8568 -- expression, the function is not pure.
00c93ba2 8569
e3d9f448 8570 if Expander_Active then
33398e3c
AC
8571 Set_Is_Pure (SId,
8572 Side_Effect_Free (Expr, Variable_Ref => True));
e3d9f448
AC
8573 Set_Is_Inlined (SId);
8574 end if;
5a8a6763 8575 end;
1e194575 8576
baa571ab 8577 -- Test for raise expressions present and if so build M version
1e194575 8578
baa571ab
AC
8579 if Raise_Expression_Present then
8580 declare
8581 SId : constant Entity_Id :=
8582 Make_Defining_Identifier (Loc,
8583 Chars => New_External_Name (Chars (Typ), "PredicateM"));
50ef946c 8584 -- The entity for the function spec
1e194575 8585
baa571ab
AC
8586 SIdB : constant Entity_Id :=
8587 Make_Defining_Identifier (Loc,
8588 Chars => New_External_Name (Chars (Typ), "PredicateM"));
8589 -- The entity for the function body
616547fa 8590
baa571ab 8591 Spec : Node_Id;
baa571ab 8592 FBody : Node_Id;
6905a049 8593 FDecl : Node_Id;
baa571ab 8594 BTemp : Entity_Id;
1e194575 8595
baa571ab
AC
8596 begin
8597 -- Mark any raise expressions for special expansion
1e194575 8598
baa571ab 8599 Process_REs (Expr_M);
1e194575 8600
baa571ab 8601 -- Build function declaration
1e194575 8602
baa571ab
AC
8603 Set_Ekind (SId, E_Function);
8604 Set_Is_Predicate_Function_M (SId);
8605 Set_Predicate_Function_M (Typ, SId);
1e194575 8606
baa571ab 8607 -- The predicate function is shared between views of a type
1e194575 8608
baa571ab
AC
8609 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
8610 Set_Predicate_Function_M (Full_View (Typ), SId);
8611 end if;
86200f66 8612
241ebe89
HK
8613 -- Mark the predicate function explicitly as Ghost because it
8614 -- does not come from source.
8615
8616 if Ghost_Mode > None then
8617 Set_Is_Ghost_Entity (SId);
8618 end if;
8619
baa571ab
AC
8620 Spec :=
8621 Make_Function_Specification (Loc,
8622 Defining_Unit_Name => SId,
8623 Parameter_Specifications => New_List (
8624 Make_Parameter_Specification (Loc,
8625 Defining_Identifier => Object_Entity_M,
8626 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8627 Result_Definition =>
8628 New_Occurrence_Of (Standard_Boolean, Loc));
86200f66 8629
baa571ab
AC
8630 FDecl :=
8631 Make_Subprogram_Declaration (Loc,
8632 Specification => Spec);
86200f66 8633
baa571ab 8634 -- Build function body
86200f66 8635
baa571ab
AC
8636 Spec :=
8637 Make_Function_Specification (Loc,
8638 Defining_Unit_Name => SIdB,
8639 Parameter_Specifications => New_List (
8640 Make_Parameter_Specification (Loc,
8641 Defining_Identifier =>
8642 Make_Defining_Identifier (Loc, Object_Name),
8643 Parameter_Type =>
8644 New_Occurrence_Of (Typ, Loc))),
8645 Result_Definition =>
8646 New_Occurrence_Of (Standard_Boolean, Loc));
86200f66 8647
baa571ab
AC
8648 -- Build the body, we declare the boolean expression before
8649 -- doing the return, because we are not really confident of
8650 -- what happens if a return appears within a return.
86200f66 8651
baa571ab
AC
8652 BTemp :=
8653 Make_Defining_Identifier (Loc,
8654 Chars => New_Internal_Name ('B'));
86200f66 8655
baa571ab
AC
8656 FBody :=
8657 Make_Subprogram_Body (Loc,
8658 Specification => Spec,
86200f66 8659
baa571ab
AC
8660 Declarations => New_List (
8661 Make_Object_Declaration (Loc,
8662 Defining_Identifier => BTemp,
8663 Constant_Present => True,
8664 Object_Definition =>
8665 New_Occurrence_Of (Standard_Boolean, Loc),
8666 Expression => Expr_M)),
1e194575 8667
baa571ab
AC
8668 Handled_Statement_Sequence =>
8669 Make_Handled_Sequence_Of_Statements (Loc,
8670 Statements => New_List (
8671 Make_Simple_Return_Statement (Loc,
8672 Expression => New_Occurrence_Of (BTemp, Loc)))));
1e194575 8673
baa571ab 8674 -- Insert declaration before freeze node and body after
1e194575 8675
baa571ab
AC
8676 Insert_Before_And_Analyze (N, FDecl);
8677 Insert_After_And_Analyze (N, FBody);
8678 end;
8679 end if;
86200f66 8680
ee4eee0a
AC
8681 -- See if we have a static predicate. Note that the answer may be
8682 -- yes even if we have an explicit Dynamic_Predicate present.
86200f66 8683
ee4eee0a 8684 declare
fc3a3f3b 8685 PS : Boolean;
ee4eee0a 8686 EN : Node_Id;
86200f66 8687
ee4eee0a 8688 begin
fc3a3f3b
RD
8689 if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then
8690 PS := False;
8691 else
8692 PS := Is_Predicate_Static (Expr, Object_Name);
8693 end if;
8694
c624298a 8695 -- Case where we have a predicate-static aspect
86200f66 8696
ee4eee0a 8697 if PS then
86200f66 8698
ee4eee0a
AC
8699 -- We don't set Has_Static_Predicate_Aspect, since we can have
8700 -- any of the three cases (Predicate, Dynamic_Predicate, or
8701 -- Static_Predicate) generating a predicate with an expression
c624298a 8702 -- that is predicate-static. We just indicate that we have a
ee4eee0a 8703 -- predicate that can be treated as static.
f6b5dc8e 8704
ee4eee0a 8705 Set_Has_Static_Predicate (Typ);
f6b5dc8e 8706
ee4eee0a 8707 -- For discrete subtype, build the static predicate list
86200f66 8708
ee4eee0a
AC
8709 if Is_Discrete_Type (Typ) then
8710 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
8711
8712 -- If we don't get a static predicate list, it means that we
8713 -- have a case where this is not possible, most typically in
8714 -- the case where we inherit a dynamic predicate. We do not
8715 -- consider this an error, we just leave the predicate as
8716 -- dynamic. But if we do succeed in building the list, then
8717 -- we mark the predicate as static.
8718
60f908dd 8719 if No (Static_Discrete_Predicate (Typ)) then
ee4eee0a
AC
8720 Set_Has_Static_Predicate (Typ, False);
8721 end if;
fc3a3f3b
RD
8722
8723 -- For real or string subtype, save predicate expression
8724
8725 elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then
8726 Set_Static_Real_Or_String_Predicate (Typ, Expr);
ee4eee0a
AC
8727 end if;
8728
8729 -- Case of dynamic predicate (expression is not predicate-static)
86200f66 8730
baa571ab 8731 else
ee4eee0a
AC
8732 -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that
8733 -- is only set if we have an explicit Dynamic_Predicate aspect
8734 -- given. Here we may simply have a Predicate aspect where the
8735 -- expression happens not to be predicate-static.
8736
8737 -- Emit an error when the predicate is categorized as static
8738 -- but its expression is not predicate-static.
8739
8740 -- First a little fiddling to get a nice location for the
8741 -- message. If the expression is of the form (A and then B),
9bdc432a
AC
8742 -- where A is an inherited predicate, then use the right
8743 -- operand for the Sloc. This avoids getting confused by a call
8744 -- to an inherited predicate with a less convenient source
8745 -- location.
ee4eee0a
AC
8746
8747 EN := Expr;
9bdc432a
AC
8748 while Nkind (EN) = N_And_Then
8749 and then Nkind (Left_Opnd (EN)) = N_Function_Call
8750 and then Is_Predicate_Function
8751 (Entity (Name (Left_Opnd (EN))))
8752 loop
8753 EN := Right_Opnd (EN);
ee4eee0a
AC
8754 end loop;
8755
8756 -- Now post appropriate message
8757
8758 if Has_Static_Predicate_Aspect (Typ) then
fc3a3f3b 8759 if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then
ee4eee0a 8760 Error_Msg_F
28e18b4f 8761 ("expression is not predicate-static (RM 3.2.4(16-22))",
ee4eee0a
AC
8762 EN);
8763 else
fc3a3f3b
RD
8764 Error_Msg_F
8765 ("static predicate requires scalar or string type", EN);
ee4eee0a
AC
8766 end if;
8767 end if;
baa571ab 8768 end if;
ee4eee0a 8769 end;
baa571ab 8770 end if;
241ebe89 8771
1af4455a 8772 Ghost_Mode := Save_Ghost_Mode;
baa571ab 8773 end Build_Predicate_Functions;
86200f66 8774
6905a049
AC
8775 ------------------------------------------
8776 -- Build_Predicate_Function_Declaration --
8777 ------------------------------------------
8778
8779 function Build_Predicate_Function_Declaration
8780 (Typ : Entity_Id) return Node_Id
8781 is
8782 Loc : constant Source_Ptr := Sloc (Typ);
8783
8784 Object_Entity : constant Entity_Id :=
2cc7967f
AC
8785 Make_Defining_Identifier (Loc,
8786 Chars => New_Internal_Name ('I'));
6905a049
AC
8787
8788 -- The formal parameter of the function
8789
8790 SId : constant Entity_Id :=
8791 Make_Defining_Identifier (Loc,
8792 Chars => New_External_Name (Chars (Typ), "Predicate"));
8793
8794 -- The entity for the function spec
8795
8796 FDecl : Node_Id;
8797 Spec : Node_Id;
8798
8799 begin
8800 Spec :=
8801 Make_Function_Specification (Loc,
8802 Defining_Unit_Name => SId,
8803 Parameter_Specifications => New_List (
8804 Make_Parameter_Specification (Loc,
8805 Defining_Identifier => Object_Entity,
8806 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8807 Result_Definition =>
8808 New_Occurrence_Of (Standard_Boolean, Loc));
8809
8810 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
8811
8812 Set_Ekind (SId, E_Function);
8813 Set_Etype (SId, Standard_Boolean);
8814 Set_Is_Internal (SId);
8815 Set_Is_Predicate_Function (SId);
8816 Set_Predicate_Function (Typ, SId);
8817
128a98ea 8818 Insert_After (Parent (Typ), FDecl);
6905a049
AC
8819
8820 Analyze (FDecl);
8821
8822 return FDecl;
8823 end Build_Predicate_Function_Declaration;
8824
5a8a6763
RD
8825 -----------------------------------------
8826 -- Check_Aspect_At_End_Of_Declarations --
8827 -----------------------------------------
86200f66 8828
5a8a6763
RD
8829 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8830 Ent : constant Entity_Id := Entity (ASN);
8831 Ident : constant Node_Id := Identifier (ASN);
8832 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
f6b5dc8e 8833
5a8a6763
RD
8834 End_Decl_Expr : constant Node_Id := Entity (Ident);
8835 -- Expression to be analyzed at end of declarations
f6b5dc8e 8836
5a8a6763
RD
8837 Freeze_Expr : constant Node_Id := Expression (ASN);
8838 -- Expression from call to Check_Aspect_At_Freeze_Point
f6b5dc8e 8839
5a8a6763
RD
8840 T : constant Entity_Id := Etype (Freeze_Expr);
8841 -- Type required for preanalyze call
f6b5dc8e 8842
5a8a6763
RD
8843 Err : Boolean;
8844 -- Set False if error
86200f66 8845
5a8a6763
RD
8846 -- On entry to this procedure, Entity (Ident) contains a copy of the
8847 -- original expression from the aspect, saved for this purpose, and
8848 -- but Expression (Ident) is a preanalyzed copy of the expression,
8849 -- preanalyzed just after the freeze point.
86200f66 8850
5a8a6763
RD
8851 procedure Check_Overloaded_Name;
8852 -- For aspects whose expression is simply a name, this routine checks if
8853 -- the name is overloaded or not. If so, it verifies there is an
8854 -- interpretation that matches the entity obtained at the freeze point,
8855 -- otherwise the compiler complains.
86200f66 8856
5a8a6763
RD
8857 ---------------------------
8858 -- Check_Overloaded_Name --
8859 ---------------------------
8860
8861 procedure Check_Overloaded_Name is
1e194575 8862 begin
5a8a6763 8863 if not Is_Overloaded (End_Decl_Expr) then
c74afd84
AC
8864 Err := not Is_Entity_Name (End_Decl_Expr)
8865 or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
5a8a6763 8866
1e194575 8867 else
5a8a6763 8868 Err := True;
86200f66 8869
5a8a6763
RD
8870 declare
8871 Index : Interp_Index;
8872 It : Interp;
86200f66 8873
5a8a6763
RD
8874 begin
8875 Get_First_Interp (End_Decl_Expr, Index, It);
8876 while Present (It.Typ) loop
8877 if It.Nam = Entity (Freeze_Expr) then
8878 Err := False;
8879 exit;
8880 end if;
8881
8882 Get_Next_Interp (Index, It);
8883 end loop;
8884 end;
86200f66 8885 end if;
5a8a6763 8886 end Check_Overloaded_Name;
86200f66 8887
5a8a6763 8888 -- Start of processing for Check_Aspect_At_End_Of_Declarations
86200f66 8889
5a8a6763 8890 begin
e96b7045
AC
8891 -- In an instance we do not perform the consistency check between freeze
8892 -- point and end of declarations, because it was done already in the
8893 -- analysis of the generic. Furthermore, the delayed analysis of an
8894 -- aspect of the instance may produce spurious errors when the generic
8895 -- is a child unit that references entities in the parent (which might
8896 -- not be in scope at the freeze point of the instance).
8897
8898 if In_Instance then
8899 return;
8900
5a8a6763 8901 -- Case of aspects Dimension, Dimension_System and Synchronization
86200f66 8902
e96b7045 8903 elsif A_Id = Aspect_Synchronization then
5a8a6763 8904 return;
1e194575 8905
5a8a6763
RD
8906 -- Case of stream attributes, just have to compare entities. However,
8907 -- the expression is just a name (possibly overloaded), and there may
8908 -- be stream operations declared for unrelated types, so we just need
8909 -- to verify that one of these interpretations is the one available at
8910 -- at the freeze point.
86200f66 8911
5a8a6763 8912 elsif A_Id = Aspect_Input or else
dc06dd83
AC
8913 A_Id = Aspect_Output or else
8914 A_Id = Aspect_Read or else
8915 A_Id = Aspect_Write
5a8a6763
RD
8916 then
8917 Analyze (End_Decl_Expr);
8918 Check_Overloaded_Name;
86200f66 8919
5a8a6763
RD
8920 elsif A_Id = Aspect_Variable_Indexing or else
8921 A_Id = Aspect_Constant_Indexing or else
8922 A_Id = Aspect_Default_Iterator or else
8923 A_Id = Aspect_Iterator_Element
8924 then
8925 -- Make type unfrozen before analysis, to prevent spurious errors
8926 -- about late attributes.
86200f66 8927
5a8a6763
RD
8928 Set_Is_Frozen (Ent, False);
8929 Analyze (End_Decl_Expr);
8930 Set_Is_Frozen (Ent, True);
86200f66 8931
5a8a6763
RD
8932 -- If the end of declarations comes before any other freeze
8933 -- point, the Freeze_Expr is not analyzed: no check needed.
86200f66 8934
5a8a6763
RD
8935 if Analyzed (Freeze_Expr) and then not In_Instance then
8936 Check_Overloaded_Name;
8937 else
8938 Err := False;
8939 end if;
c7532b2d 8940
5a8a6763 8941 -- All other cases
c7532b2d 8942
5a8a6763 8943 else
d2d8b2a7
AC
8944 -- Indicate that the expression comes from an aspect specification,
8945 -- which is used in subsequent analysis even if expansion is off.
8946
8947 Set_Parent (End_Decl_Expr, ASN);
8948
5a8a6763
RD
8949 -- In a generic context the aspect expressions have not been
8950 -- preanalyzed, so do it now. There are no conformance checks
8951 -- to perform in this case.
c7532b2d 8952
5a8a6763
RD
8953 if No (T) then
8954 Check_Aspect_At_Freeze_Point (ASN);
8955 return;
c7532b2d 8956
5a8a6763
RD
8957 -- The default values attributes may be defined in the private part,
8958 -- and the analysis of the expression may take place when only the
8959 -- partial view is visible. The expression must be scalar, so use
8960 -- the full view to resolve.
c7532b2d 8961
5a8a6763
RD
8962 elsif (A_Id = Aspect_Default_Value
8963 or else
8964 A_Id = Aspect_Default_Component_Value)
8965 and then Is_Private_Type (T)
8966 then
8967 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
d2d8b2a7 8968
5a8a6763
RD
8969 else
8970 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8971 end if;
1e194575 8972
5a8a6763
RD
8973 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8974 end if;
c7532b2d 8975
d2d8b2a7
AC
8976 -- Output error message if error. Force error on aspect specification
8977 -- even if there is an error on the expression itself.
c7532b2d 8978
5a8a6763
RD
8979 if Err then
8980 Error_Msg_NE
d2d8b2a7 8981 ("!visibility of aspect for& changes after freeze point",
5a8a6763
RD
8982 ASN, Ent);
8983 Error_Msg_NE
8984 ("info: & is frozen here, aspects evaluated at this point??",
8985 Freeze_Node (Ent), Ent);
8986 end if;
8987 end Check_Aspect_At_End_Of_Declarations;
c7532b2d 8988
5a8a6763
RD
8989 ----------------------------------
8990 -- Check_Aspect_At_Freeze_Point --
8991 ----------------------------------
86200f66 8992
5a8a6763
RD
8993 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8994 Ident : constant Node_Id := Identifier (ASN);
8995 -- Identifier (use Entity field to save expression)
86200f66 8996
5a8a6763 8997 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
86200f66 8998
5a8a6763
RD
8999 T : Entity_Id := Empty;
9000 -- Type required for preanalyze call
86200f66 9001
5a8a6763
RD
9002 begin
9003 -- On entry to this procedure, Entity (Ident) contains a copy of the
9004 -- original expression from the aspect, saved for this purpose.
86200f66 9005
5a8a6763
RD
9006 -- On exit from this procedure Entity (Ident) is unchanged, still
9007 -- containing that copy, but Expression (Ident) is a preanalyzed copy
9008 -- of the expression, preanalyzed just after the freeze point.
1e194575 9009
5a8a6763 9010 -- Make a copy of the expression to be preanalyzed
1e194575 9011
5a8a6763 9012 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
1e194575 9013
5a8a6763 9014 -- Find type for preanalyze call
1e194575 9015
5a8a6763 9016 case A_Id is
86200f66 9017
5a8a6763 9018 -- No_Aspect should be impossible
1e194575 9019
5a8a6763
RD
9020 when No_Aspect =>
9021 raise Program_Error;
9022
9023 -- Aspects taking an optional boolean argument
1e194575 9024
5a8a6763
RD
9025 when Boolean_Aspects |
9026 Library_Unit_Aspects =>
86200f66 9027
5a8a6763 9028 T := Standard_Boolean;
f6b5dc8e 9029
5a8a6763 9030 -- Aspects corresponding to attribute definition clauses
86200f66 9031
5a8a6763
RD
9032 when Aspect_Address =>
9033 T := RTE (RE_Address);
86200f66 9034
5a8a6763
RD
9035 when Aspect_Attach_Handler =>
9036 T := RTE (RE_Interrupt_ID);
f6b5dc8e 9037
5a8a6763
RD
9038 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
9039 T := RTE (RE_Bit_Order);
f6b5dc8e 9040
5a8a6763
RD
9041 when Aspect_Convention =>
9042 return;
f6b5dc8e 9043
5a8a6763
RD
9044 when Aspect_CPU =>
9045 T := RTE (RE_CPU_Range);
f6b5dc8e 9046
5a8a6763 9047 -- Default_Component_Value is resolved with the component type
f6b5dc8e 9048
5a8a6763
RD
9049 when Aspect_Default_Component_Value =>
9050 T := Component_Type (Entity (ASN));
f6b5dc8e 9051
2ef05128
AC
9052 when Aspect_Default_Storage_Pool =>
9053 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
9054
5a8a6763 9055 -- Default_Value is resolved with the type entity in question
f6b5dc8e 9056
5a8a6763
RD
9057 when Aspect_Default_Value =>
9058 T := Entity (ASN);
86200f66 9059
5a8a6763
RD
9060 when Aspect_Dispatching_Domain =>
9061 T := RTE (RE_Dispatching_Domain);
86200f66 9062
5a8a6763
RD
9063 when Aspect_External_Tag =>
9064 T := Standard_String;
86200f66 9065
5a8a6763
RD
9066 when Aspect_External_Name =>
9067 T := Standard_String;
86200f66 9068
5a8a6763
RD
9069 when Aspect_Link_Name =>
9070 T := Standard_String;
86200f66 9071
5a8a6763
RD
9072 when Aspect_Priority | Aspect_Interrupt_Priority =>
9073 T := Standard_Integer;
1e194575 9074
5a8a6763
RD
9075 when Aspect_Relative_Deadline =>
9076 T := RTE (RE_Time_Span);
1e194575 9077
5a8a6763
RD
9078 when Aspect_Small =>
9079 T := Universal_Real;
f2acf80c 9080
5a8a6763
RD
9081 -- For a simple storage pool, we have to retrieve the type of the
9082 -- pool object associated with the aspect's corresponding attribute
9083 -- definition clause.
f2acf80c 9084
5a8a6763
RD
9085 when Aspect_Simple_Storage_Pool =>
9086 T := Etype (Expression (Aspect_Rep_Item (ASN)));
1e194575 9087
5a8a6763
RD
9088 when Aspect_Storage_Pool =>
9089 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
1e194575 9090
5a8a6763
RD
9091 when Aspect_Alignment |
9092 Aspect_Component_Size |
9093 Aspect_Machine_Radix |
9094 Aspect_Object_Size |
9095 Aspect_Size |
9096 Aspect_Storage_Size |
9097 Aspect_Stream_Size |
9098 Aspect_Value_Size =>
9099 T := Any_Integer;
86200f66 9100
19992053
AC
9101 when Aspect_Linker_Section =>
9102 T := Standard_String;
9103
5a8a6763
RD
9104 when Aspect_Synchronization =>
9105 return;
47e11d08 9106
5a8a6763
RD
9107 -- Special case, the expression of these aspects is just an entity
9108 -- that does not need any resolution, so just analyze.
47e11d08 9109
5a8a6763
RD
9110 when Aspect_Input |
9111 Aspect_Output |
9112 Aspect_Read |
9113 Aspect_Suppress |
9114 Aspect_Unsuppress |
9115 Aspect_Warnings |
9116 Aspect_Write =>
9117 Analyze (Expression (ASN));
9118 return;
47e11d08 9119
5a8a6763
RD
9120 -- Same for Iterator aspects, where the expression is a function
9121 -- name. Legality rules are checked separately.
b98e2969 9122
5a8a6763
RD
9123 when Aspect_Constant_Indexing |
9124 Aspect_Default_Iterator |
9125 Aspect_Iterator_Element |
9126 Aspect_Variable_Indexing =>
9127 Analyze (Expression (ASN));
9128 return;
47e11d08 9129
dd2bf554
ES
9130 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
9131
9132 when Aspect_Iterable =>
b4f149c2
AC
9133 T := Entity (ASN);
9134
dd2bf554 9135 declare
a6ce7e76 9136 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
b4f149c2
AC
9137 Assoc : Node_Id;
9138 Expr : Node_Id;
a6ce7e76 9139
dd2bf554 9140 begin
a6ce7e76
AC
9141 if Cursor = Any_Type then
9142 return;
9143 end if;
9144
dd2bf554
ES
9145 Assoc := First (Component_Associations (Expression (ASN)));
9146 while Present (Assoc) loop
b4f149c2
AC
9147 Expr := Expression (Assoc);
9148 Analyze (Expr);
a6ce7e76
AC
9149
9150 if not Error_Posted (Expr) then
9151 Resolve_Iterable_Operation
9152 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
9153 end if;
9154
dd2bf554
ES
9155 Next (Assoc);
9156 end loop;
9157 end;
b4f149c2 9158
dd2bf554
ES
9159 return;
9160
5a8a6763 9161 -- Invariant/Predicate take boolean expressions
47e11d08 9162
5a8a6763
RD
9163 when Aspect_Dynamic_Predicate |
9164 Aspect_Invariant |
9165 Aspect_Predicate |
9166 Aspect_Static_Predicate |
9167 Aspect_Type_Invariant =>
9168 T := Standard_Boolean;
47e11d08 9169
a2c314c7
AC
9170 when Aspect_Predicate_Failure =>
9171 T := Standard_String;
9172
5a8a6763 9173 -- Here is the list of aspects that don't require delay analysis
b98e2969 9174
1df7c326
AC
9175 when Aspect_Abstract_State |
9176 Aspect_Annotate |
847d950d
HK
9177 Aspect_Async_Readers |
9178 Aspect_Async_Writers |
1df7c326
AC
9179 Aspect_Constant_After_Elaboration |
9180 Aspect_Contract_Cases |
9181 Aspect_Default_Initial_Condition |
9182 Aspect_Depends |
9183 Aspect_Dimension |
9184 Aspect_Dimension_System |
847d950d
HK
9185 Aspect_Effective_Reads |
9186 Aspect_Effective_Writes |
1df7c326
AC
9187 Aspect_Extensions_Visible |
9188 Aspect_Ghost |
9189 Aspect_Global |
9190 Aspect_Implicit_Dereference |
9191 Aspect_Initial_Condition |
9192 Aspect_Initializes |
9193 Aspect_Obsolescent |
9194 Aspect_Part_Of |
9195 Aspect_Post |
9196 Aspect_Postcondition |
9197 Aspect_Pre |
9198 Aspect_Precondition |
9199 Aspect_Refined_Depends |
9200 Aspect_Refined_Global |
9201 Aspect_Refined_Post |
9202 Aspect_Refined_State |
9203 Aspect_SPARK_Mode |
9204 Aspect_Test_Case |
847d950d
HK
9205 Aspect_Unimplemented |
9206 Aspect_Volatile_Function =>
5a8a6763 9207 raise Program_Error;
df378148 9208
5a8a6763 9209 end case;
df378148 9210
5a8a6763 9211 -- Do the preanalyze call
df378148 9212
5a8a6763
RD
9213 Preanalyze_Spec_Expression (Expression (ASN), T);
9214 end Check_Aspect_At_Freeze_Point;
df378148 9215
5a8a6763
RD
9216 -----------------------------------
9217 -- Check_Constant_Address_Clause --
9218 -----------------------------------
df378148 9219
5a8a6763
RD
9220 procedure Check_Constant_Address_Clause
9221 (Expr : Node_Id;
9222 U_Ent : Entity_Id)
9223 is
9224 procedure Check_At_Constant_Address (Nod : Node_Id);
9225 -- Checks that the given node N represents a name whose 'Address is
9226 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
9227 -- address value is the same at the point of declaration of U_Ent and at
9228 -- the time of elaboration of the address clause.
b3f532ce 9229
5a8a6763
RD
9230 procedure Check_Expr_Constants (Nod : Node_Id);
9231 -- Checks that Nod meets the requirements for a constant address clause
9232 -- in the sense of the enclosing procedure.
b3f532ce 9233
5a8a6763
RD
9234 procedure Check_List_Constants (Lst : List_Id);
9235 -- Check that all elements of list Lst meet the requirements for a
9236 -- constant address clause in the sense of the enclosing procedure.
b3f532ce 9237
5a8a6763
RD
9238 -------------------------------
9239 -- Check_At_Constant_Address --
9240 -------------------------------
b3f532ce 9241
5a8a6763
RD
9242 procedure Check_At_Constant_Address (Nod : Node_Id) is
9243 begin
9244 if Is_Entity_Name (Nod) then
9245 if Present (Address_Clause (Entity ((Nod)))) then
9246 Error_Msg_NE
9247 ("invalid address clause for initialized object &!",
9248 Nod, U_Ent);
9249 Error_Msg_NE
9250 ("address for& cannot" &
9251 " depend on another address clause! (RM 13.1(22))!",
9252 Nod, U_Ent);
b3f532ce 9253
5a8a6763
RD
9254 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
9255 and then Sloc (U_Ent) < Sloc (Entity (Nod))
9256 then
9257 Error_Msg_NE
9258 ("invalid address clause for initialized object &!",
9259 Nod, U_Ent);
9260 Error_Msg_Node_2 := U_Ent;
9261 Error_Msg_NE
9262 ("\& must be defined before & (RM 13.1(22))!",
9263 Nod, Entity (Nod));
9264 end if;
47e11d08 9265
5a8a6763
RD
9266 elsif Nkind (Nod) = N_Selected_Component then
9267 declare
9268 T : constant Entity_Id := Etype (Prefix (Nod));
57a8057a 9269
5a8a6763
RD
9270 begin
9271 if (Is_Record_Type (T)
9272 and then Has_Discriminants (T))
9273 or else
9274 (Is_Access_Type (T)
dc06dd83
AC
9275 and then Is_Record_Type (Designated_Type (T))
9276 and then Has_Discriminants (Designated_Type (T)))
5a8a6763
RD
9277 then
9278 Error_Msg_NE
9279 ("invalid address clause for initialized object &!",
9280 Nod, U_Ent);
9281 Error_Msg_N
9282 ("\address cannot depend on component" &
9283 " of discriminated record (RM 13.1(22))!",
9284 Nod);
9285 else
9286 Check_At_Constant_Address (Prefix (Nod));
9287 end if;
9288 end;
d941cee6 9289
5a8a6763
RD
9290 elsif Nkind (Nod) = N_Indexed_Component then
9291 Check_At_Constant_Address (Prefix (Nod));
9292 Check_List_Constants (Expressions (Nod));
d941cee6 9293
b3f532ce 9294 else
5a8a6763 9295 Check_Expr_Constants (Nod);
b3f532ce 9296 end if;
5a8a6763 9297 end Check_At_Constant_Address;
d50f4827 9298
5a8a6763
RD
9299 --------------------------
9300 -- Check_Expr_Constants --
9301 --------------------------
6d9e03cb 9302
5a8a6763
RD
9303 procedure Check_Expr_Constants (Nod : Node_Id) is
9304 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
9305 Ent : Entity_Id := Empty;
6d9e03cb 9306
5a8a6763
RD
9307 begin
9308 if Nkind (Nod) in N_Has_Etype
9309 and then Etype (Nod) = Any_Type
6d9e03cb 9310 then
5a8a6763 9311 return;
e8bd500e
AC
9312 end if;
9313
5a8a6763
RD
9314 case Nkind (Nod) is
9315 when N_Empty | N_Error =>
9316 return;
47e11d08 9317
5a8a6763
RD
9318 when N_Identifier | N_Expanded_Name =>
9319 Ent := Entity (Nod);
47e11d08 9320
5a8a6763
RD
9321 -- We need to look at the original node if it is different
9322 -- from the node, since we may have rewritten things and
9323 -- substituted an identifier representing the rewrite.
47e11d08 9324
5a8a6763
RD
9325 if Original_Node (Nod) /= Nod then
9326 Check_Expr_Constants (Original_Node (Nod));
47e11d08 9327
5a8a6763
RD
9328 -- If the node is an object declaration without initial
9329 -- value, some code has been expanded, and the expression
9330 -- is not constant, even if the constituents might be
9331 -- acceptable, as in A'Address + offset.
47e11d08 9332
5a8a6763
RD
9333 if Ekind (Ent) = E_Variable
9334 and then
9335 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
9336 and then
9337 No (Expression (Declaration_Node (Ent)))
9338 then
9339 Error_Msg_NE
9340 ("invalid address clause for initialized object &!",
9341 Nod, U_Ent);
b98e2969 9342
5a8a6763
RD
9343 -- If entity is constant, it may be the result of expanding
9344 -- a check. We must verify that its declaration appears
9345 -- before the object in question, else we also reject the
9346 -- address clause.
47e11d08 9347
5a8a6763
RD
9348 elsif Ekind (Ent) = E_Constant
9349 and then In_Same_Source_Unit (Ent, U_Ent)
9350 and then Sloc (Ent) > Loc_U_Ent
9351 then
9352 Error_Msg_NE
9353 ("invalid address clause for initialized object &!",
9354 Nod, U_Ent);
9355 end if;
47e11d08 9356
5a8a6763
RD
9357 return;
9358 end if;
47e11d08 9359
5a8a6763 9360 -- Otherwise look at the identifier and see if it is OK
47e11d08 9361
5a8a6763
RD
9362 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
9363 or else Is_Type (Ent)
9364 then
9365 return;
47e11d08 9366
dc06dd83
AC
9367 elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then
9368
5a8a6763
RD
9369 -- This is the case where we must have Ent defined before
9370 -- U_Ent. Clearly if they are in different units this
9371 -- requirement is met since the unit containing Ent is
9372 -- already processed.
47e11d08 9373
5a8a6763
RD
9374 if not In_Same_Source_Unit (Ent, U_Ent) then
9375 return;
47e11d08 9376
5a8a6763
RD
9377 -- Otherwise location of Ent must be before the location
9378 -- of U_Ent, that's what prior defined means.
47e11d08 9379
5a8a6763
RD
9380 elsif Sloc (Ent) < Loc_U_Ent then
9381 return;
dac3bede 9382
5a8a6763
RD
9383 else
9384 Error_Msg_NE
9385 ("invalid address clause for initialized object &!",
9386 Nod, U_Ent);
9387 Error_Msg_Node_2 := U_Ent;
9388 Error_Msg_NE
9389 ("\& must be defined before & (RM 13.1(22))!",
9390 Nod, Ent);
9391 end if;
15e934bf 9392
5a8a6763
RD
9393 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
9394 Check_Expr_Constants (Original_Node (Nod));
dac3bede 9395
5a8a6763
RD
9396 else
9397 Error_Msg_NE
9398 ("invalid address clause for initialized object &!",
9399 Nod, U_Ent);
7a1f1775 9400
5a8a6763
RD
9401 if Comes_From_Source (Ent) then
9402 Error_Msg_NE
9403 ("\reference to variable& not allowed"
9404 & " (RM 13.1(22))!", Nod, Ent);
9405 else
9406 Error_Msg_N
9407 ("non-static expression not allowed"
9408 & " (RM 13.1(22))!", Nod);
9409 end if;
9410 end if;
7a1f1775 9411
5a8a6763 9412 when N_Integer_Literal =>
2ef48385 9413
5a8a6763
RD
9414 -- If this is a rewritten unchecked conversion, in a system
9415 -- where Address is an integer type, always use the base type
9416 -- for a literal value. This is user-friendly and prevents
9417 -- order-of-elaboration issues with instances of unchecked
9418 -- conversion.
7a1f1775 9419
5a8a6763
RD
9420 if Nkind (Original_Node (Nod)) = N_Function_Call then
9421 Set_Etype (Nod, Base_Type (Etype (Nod)));
9422 end if;
4169b895 9423
5a8a6763
RD
9424 when N_Real_Literal |
9425 N_String_Literal |
9426 N_Character_Literal =>
9427 return;
47e11d08 9428
5a8a6763
RD
9429 when N_Range =>
9430 Check_Expr_Constants (Low_Bound (Nod));
9431 Check_Expr_Constants (High_Bound (Nod));
d8b962d8 9432
5a8a6763
RD
9433 when N_Explicit_Dereference =>
9434 Check_Expr_Constants (Prefix (Nod));
d8b962d8 9435
5a8a6763
RD
9436 when N_Indexed_Component =>
9437 Check_Expr_Constants (Prefix (Nod));
9438 Check_List_Constants (Expressions (Nod));
47e11d08 9439
5a8a6763
RD
9440 when N_Slice =>
9441 Check_Expr_Constants (Prefix (Nod));
9442 Check_Expr_Constants (Discrete_Range (Nod));
516f608f 9443
5a8a6763
RD
9444 when N_Selected_Component =>
9445 Check_Expr_Constants (Prefix (Nod));
fd8b4053 9446
5a8a6763
RD
9447 when N_Attribute_Reference =>
9448 if Nam_In (Attribute_Name (Nod), Name_Address,
9449 Name_Access,
9450 Name_Unchecked_Access,
9451 Name_Unrestricted_Access)
9452 then
9453 Check_At_Constant_Address (Prefix (Nod));
fd8b4053 9454
5a8a6763
RD
9455 else
9456 Check_Expr_Constants (Prefix (Nod));
9457 Check_List_Constants (Expressions (Nod));
9458 end if;
67645bde 9459
5a8a6763
RD
9460 when N_Aggregate =>
9461 Check_List_Constants (Component_Associations (Nod));
9462 Check_List_Constants (Expressions (Nod));
47e11d08 9463
5a8a6763
RD
9464 when N_Component_Association =>
9465 Check_Expr_Constants (Expression (Nod));
4169b895 9466
5a8a6763
RD
9467 when N_Extension_Aggregate =>
9468 Check_Expr_Constants (Ancestor_Part (Nod));
9469 Check_List_Constants (Component_Associations (Nod));
9470 Check_List_Constants (Expressions (Nod));
7a1f1775 9471
5a8a6763
RD
9472 when N_Null =>
9473 return;
7a1f1775 9474
5a8a6763
RD
9475 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
9476 Check_Expr_Constants (Left_Opnd (Nod));
9477 Check_Expr_Constants (Right_Opnd (Nod));
4169b895 9478
5a8a6763
RD
9479 when N_Unary_Op =>
9480 Check_Expr_Constants (Right_Opnd (Nod));
2ef48385 9481
5a8a6763
RD
9482 when N_Type_Conversion |
9483 N_Qualified_Expression |
9484 N_Allocator |
9485 N_Unchecked_Type_Conversion =>
9486 Check_Expr_Constants (Expression (Nod));
c116143c 9487
5a8a6763
RD
9488 when N_Function_Call =>
9489 if not Is_Pure (Entity (Name (Nod))) then
9490 Error_Msg_NE
9491 ("invalid address clause for initialized object &!",
9492 Nod, U_Ent);
2ef48385 9493
5a8a6763
RD
9494 Error_Msg_NE
9495 ("\function & is not pure (RM 13.1(22))!",
9496 Nod, Entity (Name (Nod)));
a8551b5f 9497
5a8a6763
RD
9498 else
9499 Check_List_Constants (Parameter_Associations (Nod));
9500 end if;
a8551b5f 9501
5a8a6763
RD
9502 when N_Parameter_Association =>
9503 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
47e11d08 9504
5a8a6763
RD
9505 when others =>
9506 Error_Msg_NE
9507 ("invalid address clause for initialized object &!",
9508 Nod, U_Ent);
9509 Error_Msg_NE
9510 ("\must be constant defined before& (RM 13.1(22))!",
9511 Nod, U_Ent);
9512 end case;
9513 end Check_Expr_Constants;
47e11d08 9514
5a8a6763
RD
9515 --------------------------
9516 -- Check_List_Constants --
9517 --------------------------
b98e2969 9518
5a8a6763
RD
9519 procedure Check_List_Constants (Lst : List_Id) is
9520 Nod1 : Node_Id;
47e11d08 9521
5a8a6763
RD
9522 begin
9523 if Present (Lst) then
9524 Nod1 := First (Lst);
9525 while Present (Nod1) loop
9526 Check_Expr_Constants (Nod1);
9527 Next (Nod1);
9528 end loop;
9529 end if;
9530 end Check_List_Constants;
d50f4827 9531
5a8a6763 9532 -- Start of processing for Check_Constant_Address_Clause
d50f4827 9533
5a8a6763
RD
9534 begin
9535 -- If rep_clauses are to be ignored, no need for legality checks. In
3c756b76
AC
9536 -- particular, no need to pester user about rep clauses that violate the
9537 -- rule on constant addresses, given that these clauses will be removed
9538 -- by Freeze before they reach the back end. Similarly in CodePeer mode,
9539 -- we want to relax these checks.
47e11d08 9540
d99565f8 9541 if not Ignore_Rep_Clauses and not CodePeer_Mode then
5a8a6763
RD
9542 Check_Expr_Constants (Expr);
9543 end if;
9544 end Check_Constant_Address_Clause;
47e11d08 9545
113a62d9
RD
9546 ---------------------------
9547 -- Check_Pool_Size_Clash --
9548 ---------------------------
9549
9550 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
9551 Post : Node_Id;
9552
9553 begin
9554 -- We need to find out which one came first. Note that in the case of
9555 -- aspects mixed with pragmas there are cases where the processing order
9556 -- is reversed, which is why we do the check here.
9557
9558 if Sloc (SP) < Sloc (SS) then
9559 Error_Msg_Sloc := Sloc (SP);
9560 Post := SS;
9561 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
9562
9563 else
9564 Error_Msg_Sloc := Sloc (SS);
9565 Post := SP;
9566 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
9567 end if;
9568
9569 Error_Msg_N
9570 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
9571 end Check_Pool_Size_Clash;
9572
5a8a6763
RD
9573 ----------------------------------------
9574 -- Check_Record_Representation_Clause --
9575 ----------------------------------------
dec6faf1 9576
5a8a6763
RD
9577 procedure Check_Record_Representation_Clause (N : Node_Id) is
9578 Loc : constant Source_Ptr := Sloc (N);
9579 Ident : constant Node_Id := Identifier (N);
9580 Rectype : Entity_Id;
9581 Fent : Entity_Id;
9582 CC : Node_Id;
9583 Fbit : Uint;
9584 Lbit : Uint;
9585 Hbit : Uint := Uint_0;
9586 Comp : Entity_Id;
9587 Pcomp : Entity_Id;
b98e2969 9588
5a8a6763
RD
9589 Max_Bit_So_Far : Uint;
9590 -- Records the maximum bit position so far. If all field positions
9591 -- are monotonically increasing, then we can skip the circuit for
9592 -- checking for overlap, since no overlap is possible.
dec6faf1 9593
5a8a6763
RD
9594 Tagged_Parent : Entity_Id := Empty;
9595 -- This is set in the case of a derived tagged type for which we have
9596 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
9597 -- positioned by record representation clauses). In this case we must
9598 -- check for overlap between components of this tagged type, and the
9599 -- components of its parent. Tagged_Parent will point to this parent
9600 -- type. For all other cases Tagged_Parent is left set to Empty.
47e11d08 9601
5a8a6763
RD
9602 Parent_Last_Bit : Uint;
9603 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
9604 -- last bit position for any field in the parent type. We only need to
9605 -- check overlap for fields starting below this point.
47e11d08 9606
5a8a6763
RD
9607 Overlap_Check_Required : Boolean;
9608 -- Used to keep track of whether or not an overlap check is required
47e11d08 9609
5a8a6763
RD
9610 Overlap_Detected : Boolean := False;
9611 -- Set True if an overlap is detected
996ae0b0 9612
5a8a6763
RD
9613 Ccount : Natural := 0;
9614 -- Number of component clauses in record rep clause
996ae0b0 9615
5a8a6763
RD
9616 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
9617 -- Given two entities for record components or discriminants, checks
9618 -- if they have overlapping component clauses and issues errors if so.
996ae0b0 9619
5a8a6763
RD
9620 procedure Find_Component;
9621 -- Finds component entity corresponding to current component clause (in
9622 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
9623 -- start/stop bits for the field. If there is no matching component or
9624 -- if the matching component does not have a component clause, then
9625 -- that's an error and Comp is set to Empty, but no error message is
9626 -- issued, since the message was already given. Comp is also set to
9627 -- Empty if the current "component clause" is in fact a pragma.
996ae0b0 9628
5a8a6763
RD
9629 -----------------------------
9630 -- Check_Component_Overlap --
9631 -----------------------------
9632
9633 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
9634 CC1 : constant Node_Id := Component_Clause (C1_Ent);
9635 CC2 : constant Node_Id := Component_Clause (C2_Ent);
996ae0b0 9636
996ae0b0 9637 begin
5a8a6763 9638 if Present (CC1) and then Present (CC2) then
996ae0b0 9639
5a8a6763
RD
9640 -- Exclude odd case where we have two tag components in the same
9641 -- record, both at location zero. This seems a bit strange, but
9642 -- it seems to happen in some circumstances, perhaps on an error.
9643
9644 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
9645 return;
996ae0b0
RK
9646 end if;
9647
5a8a6763
RD
9648 -- Here we check if the two fields overlap
9649
996ae0b0 9650 declare
5a8a6763
RD
9651 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
9652 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
9653 E1 : constant Uint := S1 + Esize (C1_Ent);
9654 E2 : constant Uint := S2 + Esize (C2_Ent);
996ae0b0
RK
9655
9656 begin
5a8a6763
RD
9657 if E2 <= S1 or else E1 <= S2 then
9658 null;
996ae0b0 9659 else
5a8a6763
RD
9660 Error_Msg_Node_2 := Component_Name (CC2);
9661 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
9662 Error_Msg_Node_1 := Component_Name (CC1);
9663 Error_Msg_N
9664 ("component& overlaps & #", Component_Name (CC1));
9665 Overlap_Detected := True;
996ae0b0
RK
9666 end if;
9667 end;
996ae0b0 9668 end if;
5a8a6763 9669 end Check_Component_Overlap;
996ae0b0 9670
5a8a6763
RD
9671 --------------------
9672 -- Find_Component --
9673 --------------------
fbf5a39b 9674
5a8a6763 9675 procedure Find_Component is
fbf5a39b 9676
5a8a6763
RD
9677 procedure Search_Component (R : Entity_Id);
9678 -- Search components of R for a match. If found, Comp is set
fbf5a39b 9679
5a8a6763
RD
9680 ----------------------
9681 -- Search_Component --
9682 ----------------------
9596236a 9683
5a8a6763
RD
9684 procedure Search_Component (R : Entity_Id) is
9685 begin
9686 Comp := First_Component_Or_Discriminant (R);
9687 while Present (Comp) loop
9596236a 9688
5a8a6763
RD
9689 -- Ignore error of attribute name for component name (we
9690 -- already gave an error message for this, so no need to
9691 -- complain here)
9596236a 9692
5a8a6763
RD
9693 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
9694 null;
9695 else
9696 exit when Chars (Comp) = Chars (Component_Name (CC));
fbf5a39b
AC
9697 end if;
9698
5a8a6763
RD
9699 Next_Component_Or_Discriminant (Comp);
9700 end loop;
9701 end Search_Component;
996ae0b0 9702
5a8a6763 9703 -- Start of processing for Find_Component
996ae0b0 9704
5a8a6763
RD
9705 begin
9706 -- Return with Comp set to Empty if we have a pragma
996ae0b0 9707
5a8a6763
RD
9708 if Nkind (CC) = N_Pragma then
9709 Comp := Empty;
9710 return;
9711 end if;
996ae0b0 9712
5a8a6763 9713 -- Search current record for matching component
996ae0b0 9714
5a8a6763 9715 Search_Component (Rectype);
fbf5a39b 9716
5a8a6763
RD
9717 -- If not found, maybe component of base type discriminant that is
9718 -- absent from statically constrained first subtype.
9596236a 9719
5a8a6763
RD
9720 if No (Comp) then
9721 Search_Component (Base_Type (Rectype));
9722 end if;
9596236a 9723
5a8a6763
RD
9724 -- If no component, or the component does not reference the component
9725 -- clause in question, then there was some previous error for which
9726 -- we already gave a message, so just return with Comp Empty.
996ae0b0 9727
5a8a6763
RD
9728 if No (Comp) or else Component_Clause (Comp) /= CC then
9729 Check_Error_Detected;
9730 Comp := Empty;
9f4fd324 9731
5a8a6763 9732 -- Normal case where we have a component clause
9f4fd324 9733
5a8a6763
RD
9734 else
9735 Fbit := Component_Bit_Offset (Comp);
9736 Lbit := Fbit + Esize (Comp) - 1;
9737 end if;
9738 end Find_Component;
9f4fd324 9739
5a8a6763 9740 -- Start of processing for Check_Record_Representation_Clause
996ae0b0 9741
5a8a6763
RD
9742 begin
9743 Find_Type (Ident);
9744 Rectype := Entity (Ident);
996ae0b0 9745
5a8a6763
RD
9746 if Rectype = Any_Type then
9747 return;
9748 else
9749 Rectype := Underlying_Type (Rectype);
9750 end if;
996ae0b0 9751
5a8a6763 9752 -- See if we have a fully repped derived tagged type
996ae0b0 9753
5a8a6763
RD
9754 declare
9755 PS : constant Entity_Id := Parent_Subtype (Rectype);
996ae0b0 9756
5a8a6763
RD
9757 begin
9758 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
9759 Tagged_Parent := PS;
996ae0b0 9760
5a8a6763 9761 -- Find maximum bit of any component of the parent type
996ae0b0 9762
5a8a6763
RD
9763 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
9764 Pcomp := First_Entity (Tagged_Parent);
9765 while Present (Pcomp) loop
9766 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
9767 if Component_Bit_Offset (Pcomp) /= No_Uint
9768 and then Known_Static_Esize (Pcomp)
9769 then
9770 Parent_Last_Bit :=
9771 UI_Max
9772 (Parent_Last_Bit,
9773 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
9774 end if;
2a738b34
AC
9775 else
9776
9777 -- Skip anonymous types generated for constrained array
9778 -- or record components.
5a8a6763 9779
2a738b34 9780 null;
996ae0b0 9781 end if;
2a738b34
AC
9782
9783 Next_Entity (Pcomp);
5a8a6763
RD
9784 end loop;
9785 end if;
9786 end;
996ae0b0 9787
5a8a6763 9788 -- All done if no component clauses
996ae0b0 9789
5a8a6763 9790 CC := First (Component_Clauses (N));
996ae0b0 9791
5a8a6763
RD
9792 if No (CC) then
9793 return;
9794 end if;
996ae0b0 9795
5a8a6763
RD
9796 -- If a tag is present, then create a component clause that places it
9797 -- at the start of the record (otherwise gigi may place it after other
9798 -- fields that have rep clauses).
996ae0b0 9799
5a8a6763 9800 Fent := First_Entity (Rectype);
996ae0b0 9801
5a8a6763
RD
9802 if Nkind (Fent) = N_Defining_Identifier
9803 and then Chars (Fent) = Name_uTag
9804 then
9805 Set_Component_Bit_Offset (Fent, Uint_0);
9806 Set_Normalized_Position (Fent, Uint_0);
9807 Set_Normalized_First_Bit (Fent, Uint_0);
9808 Set_Normalized_Position_Max (Fent, Uint_0);
9809 Init_Esize (Fent, System_Address_Size);
996ae0b0 9810
5a8a6763
RD
9811 Set_Component_Clause (Fent,
9812 Make_Component_Clause (Loc,
9813 Component_Name => Make_Identifier (Loc, Name_uTag),
996ae0b0 9814
5a8a6763
RD
9815 Position => Make_Integer_Literal (Loc, Uint_0),
9816 First_Bit => Make_Integer_Literal (Loc, Uint_0),
9817 Last_Bit =>
9818 Make_Integer_Literal (Loc,
9819 UI_From_Int (System_Address_Size))));
996ae0b0 9820
5a8a6763
RD
9821 Ccount := Ccount + 1;
9822 end if;
996ae0b0 9823
5a8a6763
RD
9824 Max_Bit_So_Far := Uint_Minus_1;
9825 Overlap_Check_Required := False;
996ae0b0 9826
5a8a6763 9827 -- Process the component clauses
996ae0b0 9828
5a8a6763
RD
9829 while Present (CC) loop
9830 Find_Component;
996ae0b0 9831
5a8a6763
RD
9832 if Present (Comp) then
9833 Ccount := Ccount + 1;
996ae0b0 9834
5a8a6763 9835 -- We need a full overlap check if record positions non-monotonic
996ae0b0 9836
5a8a6763
RD
9837 if Fbit <= Max_Bit_So_Far then
9838 Overlap_Check_Required := True;
9839 end if;
996ae0b0 9840
5a8a6763 9841 Max_Bit_So_Far := Lbit;
996ae0b0 9842
5a8a6763 9843 -- Check bit position out of range of specified size
1d57c04f 9844
5a8a6763
RD
9845 if Has_Size_Clause (Rectype)
9846 and then RM_Size (Rectype) <= Lbit
9847 then
9848 Error_Msg_N
9849 ("bit number out of range of specified size",
9850 Last_Bit (CC));
996ae0b0 9851
5a8a6763 9852 -- Check for overlap with tag component
8a95f4e8 9853
5a8a6763
RD
9854 else
9855 if Is_Tagged_Type (Rectype)
9856 and then Fbit < System_Address_Size
9857 then
9858 Error_Msg_NE
9859 ("component overlaps tag field of&",
9860 Component_Name (CC), Rectype);
9861 Overlap_Detected := True;
9862 end if;
8a95f4e8 9863
5a8a6763
RD
9864 if Hbit < Lbit then
9865 Hbit := Lbit;
9866 end if;
9867 end if;
8a95f4e8 9868
5a8a6763 9869 -- Check parent overlap if component might overlap parent field
8a95f4e8 9870
5a8a6763
RD
9871 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9872 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9873 while Present (Pcomp) loop
9874 if not Is_Tag (Pcomp)
9875 and then Chars (Pcomp) /= Name_uParent
9876 then
9877 Check_Component_Overlap (Comp, Pcomp);
9878 end if;
8a95f4e8 9879
5a8a6763
RD
9880 Next_Component_Or_Discriminant (Pcomp);
9881 end loop;
9882 end if;
9883 end if;
8a95f4e8 9884
5a8a6763
RD
9885 Next (CC);
9886 end loop;
9a1bc6d5 9887
5a8a6763
RD
9888 -- Now that we have processed all the component clauses, check for
9889 -- overlap. We have to leave this till last, since the components can
9890 -- appear in any arbitrary order in the representation clause.
8a95f4e8 9891
5a8a6763
RD
9892 -- We do not need this check if all specified ranges were monotonic,
9893 -- as recorded by Overlap_Check_Required being False at this stage.
8a95f4e8 9894
5a8a6763
RD
9895 -- This first section checks if there are any overlapping entries at
9896 -- all. It does this by sorting all entries and then seeing if there are
9897 -- any overlaps. If there are none, then that is decisive, but if there
9898 -- are overlaps, they may still be OK (they may result from fields in
9899 -- different variants).
8a95f4e8 9900
5a8a6763
RD
9901 if Overlap_Check_Required then
9902 Overlap_Check1 : declare
8a95f4e8 9903
5a8a6763
RD
9904 OC_Fbit : array (0 .. Ccount) of Uint;
9905 -- First-bit values for component clauses, the value is the offset
9906 -- of the first bit of the field from start of record. The zero
9907 -- entry is for use in sorting.
9a1bc6d5 9908
5a8a6763
RD
9909 OC_Lbit : array (0 .. Ccount) of Uint;
9910 -- Last-bit values for component clauses, the value is the offset
9911 -- of the last bit of the field from start of record. The zero
9912 -- entry is for use in sorting.
9913
9914 OC_Count : Natural := 0;
9915 -- Count of entries in OC_Fbit and OC_Lbit
8a95f4e8 9916
5a8a6763
RD
9917 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9918 -- Compare routine for Sort
8a95f4e8 9919
5a8a6763
RD
9920 procedure OC_Move (From : Natural; To : Natural);
9921 -- Move routine for Sort
8a95f4e8 9922
5a8a6763 9923 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8a95f4e8 9924
5a8a6763
RD
9925 -----------
9926 -- OC_Lt --
9927 -----------
8a95f4e8 9928
5a8a6763 9929 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8a95f4e8 9930 begin
5a8a6763
RD
9931 return OC_Fbit (Op1) < OC_Fbit (Op2);
9932 end OC_Lt;
8a95f4e8 9933
5a8a6763
RD
9934 -------------
9935 -- OC_Move --
9936 -------------
8a95f4e8 9937
5a8a6763
RD
9938 procedure OC_Move (From : Natural; To : Natural) is
9939 begin
9940 OC_Fbit (To) := OC_Fbit (From);
9941 OC_Lbit (To) := OC_Lbit (From);
9942 end OC_Move;
8a95f4e8 9943
5a8a6763 9944 -- Start of processing for Overlap_Check
8a95f4e8 9945
8a95f4e8 9946 begin
5a8a6763
RD
9947 CC := First (Component_Clauses (N));
9948 while Present (CC) loop
8a95f4e8 9949
5a8a6763 9950 -- Exclude component clause already marked in error
8a95f4e8 9951
5a8a6763
RD
9952 if not Error_Posted (CC) then
9953 Find_Component;
9954
9955 if Present (Comp) then
9956 OC_Count := OC_Count + 1;
9957 OC_Fbit (OC_Count) := Fbit;
9958 OC_Lbit (OC_Count) := Lbit;
9959 end if;
8a95f4e8
RD
9960 end if;
9961
5a8a6763 9962 Next (CC);
8a95f4e8 9963 end loop;
8a95f4e8 9964
5a8a6763 9965 Sorting.Sort (OC_Count);
8a95f4e8 9966
5a8a6763
RD
9967 Overlap_Check_Required := False;
9968 for J in 1 .. OC_Count - 1 loop
9969 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9970 Overlap_Check_Required := True;
9971 exit;
9972 end if;
9973 end loop;
9974 end Overlap_Check1;
9975 end if;
8a95f4e8 9976
5a8a6763
RD
9977 -- If Overlap_Check_Required is still True, then we have to do the full
9978 -- scale overlap check, since we have at least two fields that do
9979 -- overlap, and we need to know if that is OK since they are in
9980 -- different variant, or whether we have a definite problem.
8a95f4e8 9981
5a8a6763
RD
9982 if Overlap_Check_Required then
9983 Overlap_Check2 : declare
9984 C1_Ent, C2_Ent : Entity_Id;
9985 -- Entities of components being checked for overlap
8a95f4e8 9986
5a8a6763
RD
9987 Clist : Node_Id;
9988 -- Component_List node whose Component_Items are being checked
8a95f4e8 9989
5a8a6763
RD
9990 Citem : Node_Id;
9991 -- Component declaration for component being checked
8a95f4e8 9992
5a8a6763
RD
9993 begin
9994 C1_Ent := First_Entity (Base_Type (Rectype));
8a95f4e8 9995
5a8a6763
RD
9996 -- Loop through all components in record. For each component check
9997 -- for overlap with any of the preceding elements on the component
9998 -- list containing the component and also, if the component is in
9999 -- a variant, check against components outside the case structure.
10000 -- This latter test is repeated recursively up the variant tree.
8a95f4e8 10001
5a8a6763
RD
10002 Main_Component_Loop : while Present (C1_Ent) loop
10003 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
10004 goto Continue_Main_Component_Loop;
10005 end if;
8a95f4e8 10006
5a8a6763
RD
10007 -- Skip overlap check if entity has no declaration node. This
10008 -- happens with discriminants in constrained derived types.
10009 -- Possibly we are missing some checks as a result, but that
10010 -- does not seem terribly serious.
8a95f4e8 10011
5a8a6763
RD
10012 if No (Declaration_Node (C1_Ent)) then
10013 goto Continue_Main_Component_Loop;
10014 end if;
8a95f4e8 10015
5a8a6763 10016 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8a95f4e8 10017
5a8a6763
RD
10018 -- Loop through component lists that need checking. Check the
10019 -- current component list and all lists in variants above us.
8a95f4e8 10020
5a8a6763 10021 Component_List_Loop : loop
8a95f4e8 10022
5a8a6763
RD
10023 -- If derived type definition, go to full declaration
10024 -- If at outer level, check discriminants if there are any.
8a95f4e8 10025
5a8a6763
RD
10026 if Nkind (Clist) = N_Derived_Type_Definition then
10027 Clist := Parent (Clist);
10028 end if;
8a95f4e8 10029
5a8a6763 10030 -- Outer level of record definition, check discriminants
8a95f4e8 10031
5a8a6763
RD
10032 if Nkind_In (Clist, N_Full_Type_Declaration,
10033 N_Private_Type_Declaration)
8a95f4e8 10034 then
5a8a6763
RD
10035 if Has_Discriminants (Defining_Identifier (Clist)) then
10036 C2_Ent :=
10037 First_Discriminant (Defining_Identifier (Clist));
10038 while Present (C2_Ent) loop
10039 exit when C1_Ent = C2_Ent;
10040 Check_Component_Overlap (C1_Ent, C2_Ent);
10041 Next_Discriminant (C2_Ent);
10042 end loop;
10043 end if;
8a95f4e8 10044
5a8a6763 10045 -- Record extension case
8a95f4e8 10046
5a8a6763
RD
10047 elsif Nkind (Clist) = N_Derived_Type_Definition then
10048 Clist := Empty;
8a95f4e8 10049
5a8a6763 10050 -- Otherwise check one component list
8a95f4e8 10051
5a8a6763
RD
10052 else
10053 Citem := First (Component_Items (Clist));
10054 while Present (Citem) loop
10055 if Nkind (Citem) = N_Component_Declaration then
10056 C2_Ent := Defining_Identifier (Citem);
10057 exit when C1_Ent = C2_Ent;
10058 Check_Component_Overlap (C1_Ent, C2_Ent);
10059 end if;
8a95f4e8 10060
5a8a6763
RD
10061 Next (Citem);
10062 end loop;
10063 end if;
8a95f4e8 10064
5a8a6763
RD
10065 -- Check for variants above us (the parent of the Clist can
10066 -- be a variant, in which case its parent is a variant part,
10067 -- and the parent of the variant part is a component list
10068 -- whose components must all be checked against the current
10069 -- component for overlap).
8a95f4e8 10070
5a8a6763
RD
10071 if Nkind (Parent (Clist)) = N_Variant then
10072 Clist := Parent (Parent (Parent (Clist)));
8a95f4e8 10073
5a8a6763
RD
10074 -- Check for possible discriminant part in record, this
10075 -- is treated essentially as another level in the
10076 -- recursion. For this case the parent of the component
10077 -- list is the record definition, and its parent is the
10078 -- full type declaration containing the discriminant
10079 -- specifications.
10080
10081 elsif Nkind (Parent (Clist)) = N_Record_Definition then
10082 Clist := Parent (Parent ((Clist)));
10083
10084 -- If neither of these two cases, we are at the top of
10085 -- the tree.
10086
10087 else
10088 exit Component_List_Loop;
10089 end if;
10090 end loop Component_List_Loop;
8a95f4e8 10091
5a8a6763
RD
10092 <<Continue_Main_Component_Loop>>
10093 Next_Entity (C1_Ent);
8a95f4e8 10094
5a8a6763
RD
10095 end loop Main_Component_Loop;
10096 end Overlap_Check2;
8a95f4e8
RD
10097 end if;
10098
5a8a6763
RD
10099 -- The following circuit deals with warning on record holes (gaps). We
10100 -- skip this check if overlap was detected, since it makes sense for the
10101 -- programmer to fix this illegality before worrying about warnings.
8a95f4e8 10102
5a8a6763
RD
10103 if not Overlap_Detected and Warn_On_Record_Holes then
10104 Record_Hole_Check : declare
10105 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
10106 -- Full declaration of record type
8a95f4e8 10107
5a8a6763
RD
10108 procedure Check_Component_List
10109 (CL : Node_Id;
10110 Sbit : Uint;
10111 DS : List_Id);
10112 -- Check component list CL for holes. The starting bit should be
10113 -- Sbit. which is zero for the main record component list and set
10114 -- appropriately for recursive calls for variants. DS is set to
10115 -- a list of discriminant specifications to be included in the
10116 -- consideration of components. It is No_List if none to consider.
8a95f4e8 10117
5a8a6763
RD
10118 --------------------------
10119 -- Check_Component_List --
10120 --------------------------
9a1bc6d5 10121
5a8a6763
RD
10122 procedure Check_Component_List
10123 (CL : Node_Id;
10124 Sbit : Uint;
10125 DS : List_Id)
10126 is
10127 Compl : Integer;
8a95f4e8 10128
5a8a6763
RD
10129 begin
10130 Compl := Integer (List_Length (Component_Items (CL)));
9a1bc6d5 10131
5a8a6763
RD
10132 if DS /= No_List then
10133 Compl := Compl + Integer (List_Length (DS));
10134 end if;
8a95f4e8 10135
5a8a6763
RD
10136 declare
10137 Comps : array (Natural range 0 .. Compl) of Entity_Id;
10138 -- Gather components (zero entry is for sort routine)
8a95f4e8 10139
5a8a6763
RD
10140 Ncomps : Natural := 0;
10141 -- Number of entries stored in Comps (starting at Comps (1))
8a95f4e8 10142
5a8a6763
RD
10143 Citem : Node_Id;
10144 -- One component item or discriminant specification
8a95f4e8 10145
5a8a6763
RD
10146 Nbit : Uint;
10147 -- Starting bit for next component
8a95f4e8 10148
5a8a6763
RD
10149 CEnt : Entity_Id;
10150 -- Component entity
8a95f4e8 10151
5a8a6763
RD
10152 Variant : Node_Id;
10153 -- One variant
8a95f4e8 10154
5a8a6763
RD
10155 function Lt (Op1, Op2 : Natural) return Boolean;
10156 -- Compare routine for Sort
8a95f4e8 10157
5a8a6763
RD
10158 procedure Move (From : Natural; To : Natural);
10159 -- Move routine for Sort
8a95f4e8 10160
5a8a6763 10161 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8a95f4e8 10162
5a8a6763
RD
10163 --------
10164 -- Lt --
10165 --------
8a95f4e8 10166
5a8a6763
RD
10167 function Lt (Op1, Op2 : Natural) return Boolean is
10168 begin
10169 return Component_Bit_Offset (Comps (Op1))
10170 <
10171 Component_Bit_Offset (Comps (Op2));
10172 end Lt;
8a95f4e8 10173
5a8a6763
RD
10174 ----------
10175 -- Move --
10176 ----------
8a95f4e8 10177
5a8a6763
RD
10178 procedure Move (From : Natural; To : Natural) is
10179 begin
10180 Comps (To) := Comps (From);
10181 end Move;
8a95f4e8 10182
5a8a6763
RD
10183 begin
10184 -- Gather discriminants into Comp
8a95f4e8 10185
5a8a6763
RD
10186 if DS /= No_List then
10187 Citem := First (DS);
10188 while Present (Citem) loop
10189 if Nkind (Citem) = N_Discriminant_Specification then
10190 declare
10191 Ent : constant Entity_Id :=
10192 Defining_Identifier (Citem);
10193 begin
10194 if Ekind (Ent) = E_Discriminant then
10195 Ncomps := Ncomps + 1;
10196 Comps (Ncomps) := Ent;
10197 end if;
10198 end;
10199 end if;
8a95f4e8 10200
5a8a6763
RD
10201 Next (Citem);
10202 end loop;
10203 end if;
8a95f4e8 10204
5a8a6763 10205 -- Gather component entities into Comp
8a95f4e8 10206
5a8a6763
RD
10207 Citem := First (Component_Items (CL));
10208 while Present (Citem) loop
10209 if Nkind (Citem) = N_Component_Declaration then
10210 Ncomps := Ncomps + 1;
10211 Comps (Ncomps) := Defining_Identifier (Citem);
10212 end if;
8a95f4e8 10213
5a8a6763
RD
10214 Next (Citem);
10215 end loop;
8a95f4e8 10216
5a8a6763
RD
10217 -- Now sort the component entities based on the first bit.
10218 -- Note we already know there are no overlapping components.
8a95f4e8 10219
5a8a6763 10220 Sorting.Sort (Ncomps);
8a95f4e8 10221
5a8a6763 10222 -- Loop through entries checking for holes
8a95f4e8 10223
5a8a6763
RD
10224 Nbit := Sbit;
10225 for J in 1 .. Ncomps loop
10226 CEnt := Comps (J);
10227 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
8a95f4e8 10228
5a8a6763
RD
10229 if Error_Msg_Uint_1 > 0 then
10230 Error_Msg_NE
10231 ("?H?^-bit gap before component&",
10232 Component_Name (Component_Clause (CEnt)), CEnt);
10233 end if;
8a95f4e8 10234
5a8a6763
RD
10235 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
10236 end loop;
8a95f4e8 10237
5a8a6763 10238 -- Process variant parts recursively if present
8a95f4e8 10239
5a8a6763
RD
10240 if Present (Variant_Part (CL)) then
10241 Variant := First (Variants (Variant_Part (CL)));
10242 while Present (Variant) loop
10243 Check_Component_List
10244 (Component_List (Variant), Nbit, No_List);
10245 Next (Variant);
10246 end loop;
8a95f4e8 10247 end if;
5a8a6763
RD
10248 end;
10249 end Check_Component_List;
8a95f4e8 10250
5a8a6763 10251 -- Start of processing for Record_Hole_Check
8a95f4e8 10252
5a8a6763
RD
10253 begin
10254 declare
10255 Sbit : Uint;
8a95f4e8 10256
5a8a6763
RD
10257 begin
10258 if Is_Tagged_Type (Rectype) then
10259 Sbit := UI_From_Int (System_Address_Size);
10260 else
10261 Sbit := Uint_0;
10262 end if;
10263
10264 if Nkind (Decl) = N_Full_Type_Declaration
10265 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
10266 then
10267 Check_Component_List
10268 (Component_List (Type_Definition (Decl)),
10269 Sbit,
10270 Discriminant_Specifications (Decl));
8a95f4e8 10271 end if;
5a8a6763
RD
10272 end;
10273 end Record_Hole_Check;
8a95f4e8
RD
10274 end if;
10275
5a8a6763
RD
10276 -- For records that have component clauses for all components, and whose
10277 -- size is less than or equal to 32, we need to know the size in the
10278 -- front end to activate possible packed array processing where the
10279 -- component type is a record.
8a95f4e8 10280
5a8a6763
RD
10281 -- At this stage Hbit + 1 represents the first unused bit from all the
10282 -- component clauses processed, so if the component clauses are
10283 -- complete, then this is the length of the record.
8a95f4e8 10284
5a8a6763
RD
10285 -- For records longer than System.Storage_Unit, and for those where not
10286 -- all components have component clauses, the back end determines the
10287 -- length (it may for example be appropriate to round up the size
10288 -- to some convenient boundary, based on alignment considerations, etc).
8a95f4e8 10289
5a8a6763 10290 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
8a95f4e8 10291
5a8a6763 10292 -- Nothing to do if at least one component has no component clause
8a95f4e8 10293
5a8a6763
RD
10294 Comp := First_Component_Or_Discriminant (Rectype);
10295 while Present (Comp) loop
10296 exit when No (Component_Clause (Comp));
10297 Next_Component_Or_Discriminant (Comp);
10298 end loop;
8a95f4e8 10299
5a8a6763
RD
10300 -- If we fall out of loop, all components have component clauses
10301 -- and so we can set the size to the maximum value.
8a95f4e8 10302
5a8a6763
RD
10303 if No (Comp) then
10304 Set_RM_Size (Rectype, Hbit + 1);
10305 end if;
10306 end if;
10307 end Check_Record_Representation_Clause;
8a95f4e8 10308
5a8a6763
RD
10309 ----------------
10310 -- Check_Size --
10311 ----------------
8a95f4e8 10312
5a8a6763
RD
10313 procedure Check_Size
10314 (N : Node_Id;
10315 T : Entity_Id;
10316 Siz : Uint;
10317 Biased : out Boolean)
10318 is
32b794c8
AC
10319 procedure Size_Too_Small_Error (Min_Siz : Uint);
10320 -- Emit an error concerning illegal size Siz. Min_Siz denotes the
10321 -- minimum size.
10322
10323 --------------------------
10324 -- Size_Too_Small_Error --
10325 --------------------------
10326
10327 procedure Size_Too_Small_Error (Min_Siz : Uint) is
10328 begin
10329 -- This error is suppressed in ASIS mode to allow for different ASIS
2cc2e964 10330 -- back ends or ASIS-based tools to query the illegal clause.
32b794c8
AC
10331
10332 if not ASIS_Mode then
10333 Error_Msg_Uint_1 := Min_Siz;
331e5015 10334 Error_Msg_NE ("size for& too small, minimum allowed is ^", N, T);
32b794c8
AC
10335 end if;
10336 end Size_Too_Small_Error;
10337
10338 -- Local variables
10339
5a8a6763
RD
10340 UT : constant Entity_Id := Underlying_Type (T);
10341 M : Uint;
8a95f4e8 10342
32b794c8
AC
10343 -- Start of processing for Check_Size
10344
5a8a6763
RD
10345 begin
10346 Biased := False;
8a95f4e8 10347
32b794c8 10348 -- Reject patently improper size values
8a95f4e8 10349
5a8a6763
RD
10350 if Is_Elementary_Type (T)
10351 and then Siz > UI_From_Int (Int'Last)
10352 then
10353 Error_Msg_N ("Size value too large for elementary type", N);
8a95f4e8 10354
5a8a6763
RD
10355 if Nkind (Original_Node (N)) = N_Op_Expon then
10356 Error_Msg_N
10357 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
10358 end if;
10359 end if;
8a95f4e8 10360
5a8a6763 10361 -- Dismiss generic types
8a95f4e8 10362
5a8a6763
RD
10363 if Is_Generic_Type (T)
10364 or else
10365 Is_Generic_Type (UT)
10366 or else
10367 Is_Generic_Type (Root_Type (UT))
10368 then
10369 return;
8a95f4e8 10370
5a8a6763 10371 -- Guard against previous errors
8a95f4e8 10372
5a8a6763
RD
10373 elsif No (UT) or else UT = Any_Type then
10374 Check_Error_Detected;
10375 return;
8a95f4e8 10376
5a8a6763 10377 -- Check case of bit packed array
8a95f4e8 10378
5a8a6763
RD
10379 elsif Is_Array_Type (UT)
10380 and then Known_Static_Component_Size (UT)
10381 and then Is_Bit_Packed_Array (UT)
10382 then
10383 declare
10384 Asiz : Uint;
10385 Indx : Node_Id;
10386 Ityp : Entity_Id;
8a95f4e8 10387
5a8a6763
RD
10388 begin
10389 Asiz := Component_Size (UT);
10390 Indx := First_Index (UT);
10391 loop
10392 Ityp := Etype (Indx);
8a95f4e8 10393
5a8a6763
RD
10394 -- If non-static bound, then we are not in the business of
10395 -- trying to check the length, and indeed an error will be
10396 -- issued elsewhere, since sizes of non-static array types
10397 -- cannot be set implicitly or explicitly.
8a95f4e8 10398
edab6088 10399 if not Is_OK_Static_Subtype (Ityp) then
5a8a6763
RD
10400 return;
10401 end if;
8a95f4e8 10402
5a8a6763 10403 -- Otherwise accumulate next dimension
8a95f4e8 10404
5a8a6763
RD
10405 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
10406 Expr_Value (Type_Low_Bound (Ityp)) +
10407 Uint_1);
8a95f4e8 10408
5a8a6763
RD
10409 Next_Index (Indx);
10410 exit when No (Indx);
10411 end loop;
8a95f4e8 10412
5a8a6763
RD
10413 if Asiz <= Siz then
10414 return;
8a95f4e8 10415
5a8a6763 10416 else
32b794c8 10417 Size_Too_Small_Error (Asiz);
5a8a6763
RD
10418 Set_Esize (T, Asiz);
10419 Set_RM_Size (T, Asiz);
10420 end if;
10421 end;
8a95f4e8 10422
5a8a6763 10423 -- All other composite types are ignored
8a95f4e8 10424
5a8a6763
RD
10425 elsif Is_Composite_Type (UT) then
10426 return;
9a1bc6d5 10427
5a8a6763
RD
10428 -- For fixed-point types, don't check minimum if type is not frozen,
10429 -- since we don't know all the characteristics of the type that can
10430 -- affect the size (e.g. a specified small) till freeze time.
9a1bc6d5 10431
32b794c8 10432 elsif Is_Fixed_Point_Type (UT) and then not Is_Frozen (UT) then
5a8a6763 10433 null;
9a1bc6d5 10434
5a8a6763 10435 -- Cases for which a minimum check is required
9a1bc6d5 10436
5a8a6763
RD
10437 else
10438 -- Ignore if specified size is correct for the type
9a1bc6d5 10439
5a8a6763
RD
10440 if Known_Esize (UT) and then Siz = Esize (UT) then
10441 return;
10442 end if;
9a1bc6d5 10443
5a8a6763 10444 -- Otherwise get minimum size
9a1bc6d5 10445
5a8a6763 10446 M := UI_From_Int (Minimum_Size (UT));
9a1bc6d5 10447
5a8a6763 10448 if Siz < M then
9a1bc6d5 10449
5a8a6763
RD
10450 -- Size is less than minimum size, but one possibility remains
10451 -- that we can manage with the new size if we bias the type.
9a1bc6d5 10452
5a8a6763 10453 M := UI_From_Int (Minimum_Size (UT, Biased => True));
9a1bc6d5 10454
5a8a6763 10455 if Siz < M then
32b794c8
AC
10456 Size_Too_Small_Error (M);
10457 Set_Esize (T, M);
5a8a6763
RD
10458 Set_RM_Size (T, M);
10459 else
10460 Biased := True;
10461 end if;
10462 end if;
10463 end if;
10464 end Check_Size;
9a1bc6d5 10465
5a8a6763
RD
10466 --------------------------
10467 -- Freeze_Entity_Checks --
10468 --------------------------
9a1bc6d5 10469
5a8a6763 10470 procedure Freeze_Entity_Checks (N : Node_Id) is
09c954dc
AC
10471 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id);
10472 -- Inspect the primitive operations of type Typ and hide all pairs of
79859568
AC
10473 -- implicitly declared non-overridden non-fully conformant homographs
10474 -- (Ada RM 8.3 12.3/2).
09c954dc
AC
10475
10476 -------------------------------------
10477 -- Hide_Non_Overridden_Subprograms --
10478 -------------------------------------
10479
10480 procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is
10481 procedure Hide_Matching_Homographs
10482 (Subp_Id : Entity_Id;
10483 Start_Elmt : Elmt_Id);
10484 -- Inspect a list of primitive operations starting with Start_Elmt
79859568
AC
10485 -- and find matching implicitly declared non-overridden non-fully
10486 -- conformant homographs of Subp_Id. If found, all matches along
10487 -- with Subp_Id are hidden from all visibility.
09c954dc
AC
10488
10489 function Is_Non_Overridden_Or_Null_Procedure
10490 (Subp_Id : Entity_Id) return Boolean;
10491 -- Determine whether subprogram Subp_Id is implicitly declared non-
10492 -- overridden subprogram or an implicitly declared null procedure.
10493
10494 ------------------------------
10495 -- Hide_Matching_Homographs --
10496 ------------------------------
10497
10498 procedure Hide_Matching_Homographs
10499 (Subp_Id : Entity_Id;
10500 Start_Elmt : Elmt_Id)
10501 is
10502 Prim : Entity_Id;
10503 Prim_Elmt : Elmt_Id;
10504
10505 begin
10506 Prim_Elmt := Start_Elmt;
10507 while Present (Prim_Elmt) loop
10508 Prim := Node (Prim_Elmt);
10509
10510 -- The current primitive is implicitly declared non-overridden
79859568
AC
10511 -- non-fully conformant homograph of Subp_Id. Both subprograms
10512 -- must be hidden from visibility.
09c954dc
AC
10513
10514 if Chars (Prim) = Chars (Subp_Id)
09c954dc 10515 and then Is_Non_Overridden_Or_Null_Procedure (Prim)
79859568 10516 and then not Fully_Conformant (Prim, Subp_Id)
09c954dc 10517 then
7b4ebba5
AC
10518 Set_Is_Hidden_Non_Overridden_Subpgm (Prim);
10519 Set_Is_Immediately_Visible (Prim, False);
10520 Set_Is_Potentially_Use_Visible (Prim, False);
09c954dc 10521
7b4ebba5
AC
10522 Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id);
10523 Set_Is_Immediately_Visible (Subp_Id, False);
10524 Set_Is_Potentially_Use_Visible (Subp_Id, False);
09c954dc
AC
10525 end if;
10526
10527 Next_Elmt (Prim_Elmt);
10528 end loop;
10529 end Hide_Matching_Homographs;
10530
10531 -----------------------------------------
10532 -- Is_Non_Overridden_Or_Null_Procedure --
10533 -----------------------------------------
10534
10535 function Is_Non_Overridden_Or_Null_Procedure
10536 (Subp_Id : Entity_Id) return Boolean
10537 is
10538 Alias_Id : Entity_Id;
10539
10540 begin
10541 -- The subprogram is inherited (implicitly declared), it does not
10542 -- override and does not cover a primitive of an interface.
10543
10544 if Ekind_In (Subp_Id, E_Function, E_Procedure)
10545 and then Present (Alias (Subp_Id))
10546 and then No (Interface_Alias (Subp_Id))
10547 and then No (Overridden_Operation (Subp_Id))
10548 then
10549 Alias_Id := Alias (Subp_Id);
10550
10551 if Requires_Overriding (Alias_Id) then
10552 return True;
10553
10554 elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification
10555 and then Null_Present (Parent (Alias_Id))
10556 then
10557 return True;
10558 end if;
10559 end if;
10560
10561 return False;
10562 end Is_Non_Overridden_Or_Null_Procedure;
10563
10564 -- Local variables
10565
10566 Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ);
10567 Prim : Entity_Id;
10568 Prim_Elmt : Elmt_Id;
10569
10570 -- Start of processing for Hide_Non_Overridden_Subprograms
10571
10572 begin
79859568
AC
10573 -- Inspect the list of primitives looking for non-overridden
10574 -- subprograms.
09c954dc
AC
10575
10576 if Present (Prim_Ops) then
10577 Prim_Elmt := First_Elmt (Prim_Ops);
10578 while Present (Prim_Elmt) loop
10579 Prim := Node (Prim_Elmt);
10580 Next_Elmt (Prim_Elmt);
10581
10582 if Is_Non_Overridden_Or_Null_Procedure (Prim) then
10583 Hide_Matching_Homographs
10584 (Subp_Id => Prim,
10585 Start_Elmt => Prim_Elmt);
10586 end if;
10587 end loop;
10588 end if;
10589 end Hide_Non_Overridden_Subprograms;
10590
3ddfabe3 10591 -- Local variables
09c954dc 10592
5a8a6763 10593 E : constant Entity_Id := Entity (N);
9a1bc6d5 10594
5a8a6763
RD
10595 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
10596 -- True in non-generic case. Some of the processing here is skipped
10597 -- for the generic case since it is not needed. Basically in the
10598 -- generic case, we only need to do stuff that might generate error
10599 -- messages or warnings.
09c954dc
AC
10600
10601 -- Start of processing for Freeze_Entity_Checks
10602
5a8a6763
RD
10603 begin
10604 -- Remember that we are processing a freezing entity. Required to
10605 -- ensure correct decoration of internal entities associated with
10606 -- interfaces (see New_Overloaded_Entity).
9a1bc6d5 10607
5a8a6763 10608 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
9a1bc6d5 10609
5a8a6763
RD
10610 -- For tagged types covering interfaces add internal entities that link
10611 -- the primitives of the interfaces with the primitives that cover them.
10612 -- Note: These entities were originally generated only when generating
10613 -- code because their main purpose was to provide support to initialize
10614 -- the secondary dispatch tables. They are now generated also when
10615 -- compiling with no code generation to provide ASIS the relationship
10616 -- between interface primitives and tagged type primitives. They are
10617 -- also used to locate primitives covering interfaces when processing
10618 -- generics (see Derive_Subprograms).
9a1bc6d5 10619
5a8a6763 10620 -- This is not needed in the generic case
9a1bc6d5 10621
5a8a6763
RD
10622 if Ada_Version >= Ada_2005
10623 and then Non_Generic_Case
10624 and then Ekind (E) = E_Record_Type
10625 and then Is_Tagged_Type (E)
10626 and then not Is_Interface (E)
10627 and then Has_Interfaces (E)
10628 then
10629 -- This would be a good common place to call the routine that checks
10630 -- overriding of interface primitives (and thus factorize calls to
10631 -- Check_Abstract_Overriding located at different contexts in the
10632 -- compiler). However, this is not possible because it causes
10633 -- spurious errors in case of late overriding.
9a1bc6d5 10634
5a8a6763
RD
10635 Add_Internal_Interface_Entities (E);
10636 end if;
9a1bc6d5 10637
09c954dc
AC
10638 -- After all forms of overriding have been resolved, a tagged type may
10639 -- be left with a set of implicitly declared and possibly erroneous
10640 -- abstract subprograms, null procedures and subprograms that require
75a957f5
AC
10641 -- overriding. If this set contains fully conformant homographs, then
10642 -- one is chosen arbitrarily (already done during resolution), otherwise
10643 -- all remaining non-fully conformant homographs are hidden from
10644 -- visibility (Ada RM 8.3 12.3/2).
09c954dc
AC
10645
10646 if Is_Tagged_Type (E) then
10647 Hide_Non_Overridden_Subprograms (E);
10648 end if;
10649
5a8a6763 10650 -- Check CPP types
9a1bc6d5 10651
5a8a6763
RD
10652 if Ekind (E) = E_Record_Type
10653 and then Is_CPP_Class (E)
10654 and then Is_Tagged_Type (E)
10655 and then Tagged_Type_Expansion
5a8a6763
RD
10656 then
10657 if CPP_Num_Prims (E) = 0 then
9a1bc6d5 10658
5a8a6763
RD
10659 -- If the CPP type has user defined components then it must import
10660 -- primitives from C++. This is required because if the C++ class
10661 -- has no primitives then the C++ compiler does not added the _tag
10662 -- component to the type.
9a1bc6d5 10663
5a8a6763
RD
10664 if First_Entity (E) /= Last_Entity (E) then
10665 Error_Msg_N
10666 ("'C'P'P type must import at least one primitive from C++??",
10667 E);
10668 end if;
10669 end if;
9a1bc6d5 10670
5a8a6763
RD
10671 -- Check that all its primitives are abstract or imported from C++.
10672 -- Check also availability of the C++ constructor.
9a1bc6d5 10673
5a8a6763
RD
10674 declare
10675 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
10676 Elmt : Elmt_Id;
10677 Error_Reported : Boolean := False;
10678 Prim : Node_Id;
9a1bc6d5 10679
5a8a6763
RD
10680 begin
10681 Elmt := First_Elmt (Primitive_Operations (E));
10682 while Present (Elmt) loop
10683 Prim := Node (Elmt);
9a1bc6d5 10684
5a8a6763
RD
10685 if Comes_From_Source (Prim) then
10686 if Is_Abstract_Subprogram (Prim) then
10687 null;
9a1bc6d5 10688
5a8a6763
RD
10689 elsif not Is_Imported (Prim)
10690 or else Convention (Prim) /= Convention_CPP
10691 then
10692 Error_Msg_N
10693 ("primitives of 'C'P'P types must be imported from C++ "
10694 & "or abstract??", Prim);
9a1bc6d5 10695
5a8a6763
RD
10696 elsif not Has_Constructors
10697 and then not Error_Reported
10698 then
10699 Error_Msg_Name_1 := Chars (E);
10700 Error_Msg_N
10701 ("??'C'P'P constructor required for type %", Prim);
10702 Error_Reported := True;
10703 end if;
10704 end if;
9a1bc6d5 10705
5a8a6763
RD
10706 Next_Elmt (Elmt);
10707 end loop;
10708 end;
10709 end if;
9a1bc6d5 10710
5a8a6763 10711 -- Check Ada derivation of CPP type
9a1bc6d5 10712
43c58950
AC
10713 if Expander_Active -- why? losing errors in -gnatc mode???
10714 and then Present (Etype (E)) -- defend against errors
5a8a6763
RD
10715 and then Tagged_Type_Expansion
10716 and then Ekind (E) = E_Record_Type
10717 and then Etype (E) /= E
10718 and then Is_CPP_Class (Etype (E))
10719 and then CPP_Num_Prims (Etype (E)) > 0
10720 and then not Is_CPP_Class (E)
10721 and then not Has_CPP_Constructors (Etype (E))
10722 then
10723 -- If the parent has C++ primitives but it has no constructor then
10724 -- check that all the primitives are overridden in this derivation;
10725 -- otherwise the constructor of the parent is needed to build the
10726 -- dispatch table.
9a1bc6d5 10727
5a8a6763
RD
10728 declare
10729 Elmt : Elmt_Id;
10730 Prim : Node_Id;
9a1bc6d5
AC
10731
10732 begin
5a8a6763
RD
10733 Elmt := First_Elmt (Primitive_Operations (E));
10734 while Present (Elmt) loop
10735 Prim := Node (Elmt);
9a1bc6d5 10736
5a8a6763
RD
10737 if not Is_Abstract_Subprogram (Prim)
10738 and then No (Interface_Alias (Prim))
10739 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
9a1bc6d5 10740 then
5a8a6763
RD
10741 Error_Msg_Name_1 := Chars (Etype (E));
10742 Error_Msg_N
10743 ("'C'P'P constructor required for parent type %", E);
10744 exit;
9a1bc6d5 10745 end if;
5a8a6763
RD
10746
10747 Next_Elmt (Elmt);
10748 end loop;
10749 end;
9a1bc6d5
AC
10750 end if;
10751
5a8a6763 10752 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
8a95f4e8 10753
3ddfabe3
AC
10754 -- If we have a type with predicates, build predicate function. This is
10755 -- not needed in the generic case, nor within TSS subprograms and other
10756 -- predefined primitives.
8a95f4e8 10757
3ddfabe3
AC
10758 if Is_Type (E)
10759 and then Non_Generic_Case
8e1e62e3 10760 and then not Within_Internal_Subprogram
3ddfabe3 10761 and then Has_Predicates (E)
8e1e62e3 10762 then
5a8a6763
RD
10763 Build_Predicate_Functions (E, N);
10764 end if;
8a95f4e8 10765
5a8a6763
RD
10766 -- If type has delayed aspects, this is where we do the preanalysis at
10767 -- the freeze point, as part of the consistent visibility check. Note
10768 -- that this must be done after calling Build_Predicate_Functions or
10769 -- Build_Invariant_Procedure since these subprograms fix occurrences of
10770 -- the subtype name in the saved expression so that they will not cause
10771 -- trouble in the preanalysis.
8a95f4e8 10772
5a8a6763
RD
10773 -- This is also not needed in the generic case
10774
10775 if Non_Generic_Case
10776 and then Has_Delayed_Aspects (E)
10777 and then Scope (E) = Current_Scope
10778 then
10779 -- Retrieve the visibility to the discriminants in order to properly
10780 -- analyze the aspects.
10781
10782 Push_Scope_And_Install_Discriminants (E);
10783
10784 declare
10785 Ritem : Node_Id;
10786
10787 begin
10788 -- Look for aspect specification entries for this entity
8a95f4e8 10789
5a8a6763
RD
10790 Ritem := First_Rep_Item (E);
10791 while Present (Ritem) loop
10792 if Nkind (Ritem) = N_Aspect_Specification
10793 and then Entity (Ritem) = E
10794 and then Is_Delayed_Aspect (Ritem)
10795 then
10796 Check_Aspect_At_Freeze_Point (Ritem);
10797 end if;
8a95f4e8 10798
5a8a6763
RD
10799 Next_Rep_Item (Ritem);
10800 end loop;
10801 end;
8a95f4e8 10802
5a8a6763 10803 Uninstall_Discriminants_And_Pop_Scope (E);
8a95f4e8 10804 end if;
8a95f4e8 10805
5a8a6763 10806 -- For a record type, deal with variant parts. This has to be delayed
a6282852 10807 -- to this point, because of the issue of statically predicated
5a8a6763
RD
10808 -- subtypes, which we have to ensure are frozen before checking
10809 -- choices, since we need to have the static choice list set.
996ae0b0 10810
5a8a6763
RD
10811 if Is_Record_Type (E) then
10812 Check_Variant_Part : declare
10813 D : constant Node_Id := Declaration_Node (E);
10814 T : Node_Id;
10815 C : Node_Id;
10816 VP : Node_Id;
996ae0b0 10817
5a8a6763
RD
10818 Others_Present : Boolean;
10819 pragma Warnings (Off, Others_Present);
10820 -- Indicates others present, not used in this case
996ae0b0 10821
5a8a6763
RD
10822 procedure Non_Static_Choice_Error (Choice : Node_Id);
10823 -- Error routine invoked by the generic instantiation below when
10824 -- the variant part has a non static choice.
22a83cea 10825
5a8a6763
RD
10826 procedure Process_Declarations (Variant : Node_Id);
10827 -- Processes declarations associated with a variant. We analyzed
10828 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
10829 -- but we still need the recursive call to Check_Choices for any
10830 -- nested variant to get its choices properly processed. This is
10831 -- also where we expand out the choices if expansion is active.
d27f3ff4 10832
5a8a6763
RD
10833 package Variant_Choices_Processing is new
10834 Generic_Check_Choices
10835 (Process_Empty_Choice => No_OP,
10836 Process_Non_Static_Choice => Non_Static_Choice_Error,
10837 Process_Associated_Node => Process_Declarations);
10838 use Variant_Choices_Processing;
22a83cea 10839
5a8a6763
RD
10840 -----------------------------
10841 -- Non_Static_Choice_Error --
10842 -----------------------------
996ae0b0 10843
5a8a6763
RD
10844 procedure Non_Static_Choice_Error (Choice : Node_Id) is
10845 begin
10846 Flag_Non_Static_Expr
10847 ("choice given in variant part is not static!", Choice);
10848 end Non_Static_Choice_Error;
996ae0b0 10849
5a8a6763
RD
10850 --------------------------
10851 -- Process_Declarations --
10852 --------------------------
ee2ba856 10853
5a8a6763
RD
10854 procedure Process_Declarations (Variant : Node_Id) is
10855 CL : constant Node_Id := Component_List (Variant);
10856 VP : Node_Id;
ee2ba856 10857
5a8a6763
RD
10858 begin
10859 -- Check for static predicate present in this variant
d05ef0ab 10860
5a8a6763 10861 if Has_SP_Choice (Variant) then
d05ef0ab 10862
5a8a6763
RD
10863 -- Here we expand. You might expect to find this call in
10864 -- Expand_N_Variant_Part, but that is called when we first
10865 -- see the variant part, and we cannot do this expansion
10866 -- earlier than the freeze point, since for statically
10867 -- predicated subtypes, the predicate is not known till
10868 -- the freeze point.
d05ef0ab 10869
5a8a6763
RD
10870 -- Furthermore, we do this expansion even if the expander
10871 -- is not active, because other semantic processing, e.g.
10872 -- for aggregates, requires the expanded list of choices.
d05ef0ab 10873
5a8a6763
RD
10874 -- If the expander is not active, then we can't just clobber
10875 -- the list since it would invalidate the ASIS -gnatct tree.
10876 -- So we have to rewrite the variant part with a Rewrite
10877 -- call that replaces it with a copy and clobber the copy.
10878
10879 if not Expander_Active then
10880 declare
10881 NewV : constant Node_Id := New_Copy (Variant);
10882 begin
10883 Set_Discrete_Choices
10884 (NewV, New_Copy_List (Discrete_Choices (Variant)));
10885 Rewrite (Variant, NewV);
10886 end;
10887 end if;
10888
10889 Expand_Static_Predicates_In_Choices (Variant);
d05ef0ab
AC
10890 end if;
10891
5a8a6763
RD
10892 -- We don't need to worry about the declarations in the variant
10893 -- (since they were analyzed by Analyze_Choices when we first
10894 -- encountered the variant), but we do need to take care of
10895 -- expansion of any nested variants.
d05ef0ab 10896
5a8a6763
RD
10897 if not Null_Present (CL) then
10898 VP := Variant_Part (CL);
d05ef0ab 10899
5a8a6763
RD
10900 if Present (VP) then
10901 Check_Choices
10902 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
10903 end if;
10904 end if;
10905 end Process_Declarations;
d05ef0ab 10906
5a8a6763 10907 -- Start of processing for Check_Variant_Part
616547fa 10908
5a8a6763
RD
10909 begin
10910 -- Find component list
d05ef0ab 10911
5a8a6763 10912 C := Empty;
d05ef0ab 10913
5a8a6763
RD
10914 if Nkind (D) = N_Full_Type_Declaration then
10915 T := Type_Definition (D);
d05ef0ab 10916
5a8a6763
RD
10917 if Nkind (T) = N_Record_Definition then
10918 C := Component_List (T);
996ae0b0 10919
5a8a6763
RD
10920 elsif Nkind (T) = N_Derived_Type_Definition
10921 and then Present (Record_Extension_Part (T))
10922 then
10923 C := Component_List (Record_Extension_Part (T));
10924 end if;
10925 end if;
996ae0b0 10926
5a8a6763 10927 -- Case of variant part present
996ae0b0 10928
5a8a6763
RD
10929 if Present (C) and then Present (Variant_Part (C)) then
10930 VP := Variant_Part (C);
d05ef0ab 10931
5a8a6763 10932 -- Check choices
d05ef0ab 10933
5a8a6763
RD
10934 Check_Choices
10935 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
d05ef0ab 10936
5a8a6763
RD
10937 -- If the last variant does not contain the Others choice,
10938 -- replace it with an N_Others_Choice node since Gigi always
10939 -- wants an Others. Note that we do not bother to call Analyze
10940 -- on the modified variant part, since its only effect would be
10941 -- to compute the Others_Discrete_Choices node laboriously, and
10942 -- of course we already know the list of choices corresponding
a90bd866 10943 -- to the others choice (it's the list we're replacing).
996ae0b0 10944
5a8a6763 10945 -- We only want to do this if the expander is active, since
a90bd866 10946 -- we do not want to clobber the ASIS tree.
996ae0b0 10947
5a8a6763
RD
10948 if Expander_Active then
10949 declare
10950 Last_Var : constant Node_Id :=
10951 Last_Non_Pragma (Variants (VP));
996ae0b0 10952
5a8a6763 10953 Others_Node : Node_Id;
996ae0b0 10954
5a8a6763
RD
10955 begin
10956 if Nkind (First (Discrete_Choices (Last_Var))) /=
10957 N_Others_Choice
10958 then
10959 Others_Node := Make_Others_Choice (Sloc (Last_Var));
10960 Set_Others_Discrete_Choices
10961 (Others_Node, Discrete_Choices (Last_Var));
10962 Set_Discrete_Choices
10963 (Last_Var, New_List (Others_Node));
10964 end if;
10965 end;
10966 end if;
996ae0b0 10967 end if;
5a8a6763 10968 end Check_Variant_Part;
996ae0b0 10969 end if;
5a8a6763 10970 end Freeze_Entity_Checks;
996ae0b0
RK
10971
10972 -------------------------
10973 -- Get_Alignment_Value --
10974 -------------------------
10975
10976 function Get_Alignment_Value (Expr : Node_Id) return Uint is
51122913 10977 Align : constant Uint := Static_Integer (Expr);
32b794c8 10978
51122913
HK
10979 begin
10980 if Align = No_Uint then
10981 return No_Uint;
10982
10983 elsif Align <= 0 then
32b794c8 10984
32b794c8 10985 -- This error is suppressed in ASIS mode to allow for different ASIS
2cc2e964 10986 -- back ends or ASIS-based tools to query the illegal clause.
32b794c8
AC
10987
10988 if not ASIS_Mode then
10989 Error_Msg_N ("alignment value must be positive", Expr);
10990 end if;
32b794c8 10991
996ae0b0
RK
10992 return No_Uint;
10993
10994 else
10995 for J in Int range 0 .. 64 loop
10996 declare
10997 M : constant Uint := Uint_2 ** J;
10998
10999 begin
11000 exit when M = Align;
11001
11002 if M > Align then
51122913
HK
11003
11004 -- This error is suppressed in ASIS mode to allow for
2cc2e964 11005 -- different ASIS back ends or ASIS-based tools to query the
51122913
HK
11006 -- illegal clause.
11007
11008 if not ASIS_Mode then
11009 Error_Msg_N ("alignment value must be power of 2", Expr);
11010 end if;
11011
996ae0b0
RK
11012 return No_Uint;
11013 end if;
11014 end;
11015 end loop;
11016
11017 return Align;
11018 end if;
11019 end Get_Alignment_Value;
11020
2e885a6f
AC
11021 -----------------------------
11022 -- Get_Interfacing_Aspects --
11023 -----------------------------
11024
11025 procedure Get_Interfacing_Aspects
11026 (Iface_Asp : Node_Id;
11027 Conv_Asp : out Node_Id;
11028 EN_Asp : out Node_Id;
11029 Expo_Asp : out Node_Id;
11030 Imp_Asp : out Node_Id;
11031 LN_Asp : out Node_Id;
11032 Do_Checks : Boolean := False)
11033 is
11034 procedure Save_Or_Duplication_Error
11035 (Asp : Node_Id;
11036 To : in out Node_Id);
11037 -- Save the value of aspect Asp in node To. If To already has a value,
11038 -- then this is considered a duplicate use of aspect. Emit an error if
11039 -- flag Do_Checks is set.
11040
11041 -------------------------------
11042 -- Save_Or_Duplication_Error --
11043 -------------------------------
11044
11045 procedure Save_Or_Duplication_Error
11046 (Asp : Node_Id;
11047 To : in out Node_Id)
11048 is
11049 begin
11050 -- Detect an extra aspect and issue an error
11051
11052 if Present (To) then
11053 if Do_Checks then
11054 Error_Msg_Name_1 := Chars (Identifier (Asp));
11055 Error_Msg_Sloc := Sloc (To);
11056 Error_Msg_N ("aspect % previously given #", Asp);
11057 end if;
11058
11059 -- Otherwise capture the aspect
11060
11061 else
11062 To := Asp;
11063 end if;
11064 end Save_Or_Duplication_Error;
11065
11066 -- Local variables
11067
11068 Asp : Node_Id;
11069 Asp_Id : Aspect_Id;
11070
11071 -- The following variables capture each individual aspect
11072
11073 Conv : Node_Id := Empty;
11074 EN : Node_Id := Empty;
11075 Expo : Node_Id := Empty;
11076 Imp : Node_Id := Empty;
11077 LN : Node_Id := Empty;
11078
11079 -- Start of processing for Get_Interfacing_Aspects
11080
11081 begin
11082 -- The input interfacing aspect should reside in an aspect specification
11083 -- list.
11084
11085 pragma Assert (Is_List_Member (Iface_Asp));
11086
11087 -- Examine the aspect specifications of the related entity. Find and
11088 -- capture all interfacing aspects. Detect duplicates and emit errors
11089 -- if applicable.
11090
11091 Asp := First (List_Containing (Iface_Asp));
11092 while Present (Asp) loop
11093 Asp_Id := Get_Aspect_Id (Asp);
11094
11095 if Asp_Id = Aspect_Convention then
11096 Save_Or_Duplication_Error (Asp, Conv);
11097
11098 elsif Asp_Id = Aspect_External_Name then
11099 Save_Or_Duplication_Error (Asp, EN);
11100
11101 elsif Asp_Id = Aspect_Export then
11102 Save_Or_Duplication_Error (Asp, Expo);
11103
11104 elsif Asp_Id = Aspect_Import then
11105 Save_Or_Duplication_Error (Asp, Imp);
11106
11107 elsif Asp_Id = Aspect_Link_Name then
11108 Save_Or_Duplication_Error (Asp, LN);
11109 end if;
11110
11111 Next (Asp);
11112 end loop;
11113
11114 Conv_Asp := Conv;
11115 EN_Asp := EN;
11116 Expo_Asp := Expo;
11117 Imp_Asp := Imp;
11118 LN_Asp := LN;
11119 end Get_Interfacing_Aspects;
11120
dc3af7e2
AC
11121 -------------------------------------
11122 -- Inherit_Aspects_At_Freeze_Point --
11123 -------------------------------------
11124
11125 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
11126 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11127 (Rep_Item : Node_Id) return Boolean;
11128 -- This routine checks if Rep_Item is either a pragma or an aspect
11129 -- specification node whose correponding pragma (if any) is present in
11130 -- the Rep Item chain of the entity it has been specified to.
11131
11132 --------------------------------------------------
11133 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
11134 --------------------------------------------------
11135
11136 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11137 (Rep_Item : Node_Id) return Boolean
11138 is
11139 begin
e477d718
AC
11140 return
11141 Nkind (Rep_Item) = N_Pragma
11142 or else Present_In_Rep_Item
11143 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
dc3af7e2
AC
11144 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
11145
2791be24
AC
11146 -- Start of processing for Inherit_Aspects_At_Freeze_Point
11147
dc3af7e2
AC
11148 begin
11149 -- A representation item is either subtype-specific (Size and Alignment
11150 -- clauses) or type-related (all others). Subtype-specific aspects may
2791be24 11151 -- differ for different subtypes of the same type (RM 13.1.8).
dc3af7e2
AC
11152
11153 -- A derived type inherits each type-related representation aspect of
11154 -- its parent type that was directly specified before the declaration of
2791be24 11155 -- the derived type (RM 13.1.15).
dc3af7e2
AC
11156
11157 -- A derived subtype inherits each subtype-specific representation
11158 -- aspect of its parent subtype that was directly specified before the
2791be24 11159 -- declaration of the derived type (RM 13.1.15).
dc3af7e2
AC
11160
11161 -- The general processing involves inheriting a representation aspect
11162 -- from a parent type whenever the first rep item (aspect specification,
11163 -- attribute definition clause, pragma) corresponding to the given
11164 -- representation aspect in the rep item chain of Typ, if any, isn't
11165 -- directly specified to Typ but to one of its parents.
11166
11167 -- ??? Note that, for now, just a limited number of representation
2791be24
AC
11168 -- aspects have been inherited here so far. Many of them are
11169 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
11170 -- a non- exhaustive list of aspects that likely also need to
11171 -- be moved to this routine: Alignment, Component_Alignment,
11172 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
dc3af7e2
AC
11173 -- Preelaborable_Initialization, RM_Size and Small.
11174
dbb4cfef
AC
11175 -- In addition, Convention must be propagated from base type to subtype,
11176 -- because the subtype may have been declared on an incomplete view.
11177
dc3af7e2
AC
11178 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
11179 return;
11180 end if;
11181
11182 -- Ada_05/Ada_2005
11183
11184 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
11185 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
11186 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11187 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
11188 then
11189 Set_Is_Ada_2005_Only (Typ);
11190 end if;
11191
11192 -- Ada_12/Ada_2012
11193
11194 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
11195 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
11196 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11197 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
11198 then
11199 Set_Is_Ada_2012_Only (Typ);
11200 end if;
11201
11202 -- Atomic/Shared
11203
11204 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
11205 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
11206 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11207 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
11208 then
11209 Set_Is_Atomic (Typ);
dc3af7e2 11210 Set_Is_Volatile (Typ);
57abdadd 11211 Set_Treat_As_Volatile (Typ);
dc3af7e2
AC
11212 end if;
11213
dbb4cfef
AC
11214 -- Convention
11215
64dbfdec
AC
11216 if Is_Record_Type (Typ)
11217 and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ))
11218 then
dbb4cfef
AC
11219 Set_Convention (Typ, Convention (Base_Type (Typ)));
11220 end if;
11221
2791be24 11222 -- Default_Component_Value
dc3af7e2 11223
731261c3
AC
11224 -- Verify that there is no rep_item declared for the type, and there
11225 -- is one coming from an ancestor.
11226
dc3af7e2 11227 if Is_Array_Type (Typ)
688a9b51 11228 and then Is_Base_Type (Typ)
731261c3 11229 and then not Has_Rep_Item (Typ, Name_Default_Component_Value, False)
dc3af7e2
AC
11230 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
11231 then
11232 Set_Default_Aspect_Component_Value (Typ,
11233 Default_Aspect_Component_Value
11234 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
11235 end if;
11236
2791be24 11237 -- Default_Value
dc3af7e2
AC
11238
11239 if Is_Scalar_Type (Typ)
688a9b51 11240 and then Is_Base_Type (Typ)
731261c3 11241 and then not Has_Rep_Item (Typ, Name_Default_Value, False)
dc3af7e2
AC
11242 and then Has_Rep_Item (Typ, Name_Default_Value)
11243 then
731261c3 11244 Set_Has_Default_Aspect (Typ);
dc3af7e2
AC
11245 Set_Default_Aspect_Value (Typ,
11246 Default_Aspect_Value
11247 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
11248 end if;
11249
11250 -- Discard_Names
11251
11252 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
11253 and then Has_Rep_Item (Typ, Name_Discard_Names)
11254 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11255 (Get_Rep_Item (Typ, Name_Discard_Names))
11256 then
11257 Set_Discard_Names (Typ);
11258 end if;
11259
dc3af7e2
AC
11260 -- Volatile
11261
11262 if not Has_Rep_Item (Typ, Name_Volatile, False)
11263 and then Has_Rep_Item (Typ, Name_Volatile)
11264 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11265 (Get_Rep_Item (Typ, Name_Volatile))
11266 then
dc3af7e2 11267 Set_Is_Volatile (Typ);
57abdadd 11268 Set_Treat_As_Volatile (Typ);
dc3af7e2
AC
11269 end if;
11270
f280dd8f
RD
11271 -- Volatile_Full_Access
11272
11273 if not Has_Rep_Item (Typ, Name_Volatile_Full_Access, False)
11274 and then Has_Rep_Pragma (Typ, Name_Volatile_Full_Access)
11275 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11276 (Get_Rep_Item (Typ, Name_Volatile_Full_Access))
11277 then
57abdadd 11278 Set_Is_Volatile_Full_Access (Typ);
f280dd8f 11279 Set_Is_Volatile (Typ);
57abdadd 11280 Set_Treat_As_Volatile (Typ);
f280dd8f
RD
11281 end if;
11282
dc3af7e2
AC
11283 -- Inheritance for derived types only
11284
11285 if Is_Derived_Type (Typ) then
11286 declare
11287 Bas_Typ : constant Entity_Id := Base_Type (Typ);
11288 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
11289
11290 begin
11291 -- Atomic_Components
11292
11293 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
11294 and then Has_Rep_Item (Typ, Name_Atomic_Components)
11295 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11296 (Get_Rep_Item (Typ, Name_Atomic_Components))
11297 then
11298 Set_Has_Atomic_Components (Imp_Bas_Typ);
11299 end if;
11300
11301 -- Volatile_Components
11302
11303 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
11304 and then Has_Rep_Item (Typ, Name_Volatile_Components)
11305 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11306 (Get_Rep_Item (Typ, Name_Volatile_Components))
11307 then
11308 Set_Has_Volatile_Components (Imp_Bas_Typ);
11309 end if;
11310
eefd2467 11311 -- Finalize_Storage_Only
dc3af7e2
AC
11312
11313 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
11314 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
11315 then
11316 Set_Finalize_Storage_Only (Bas_Typ);
11317 end if;
11318
11319 -- Universal_Aliasing
11320
11321 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
11322 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
11323 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
11324 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
11325 then
11326 Set_Universal_Aliasing (Imp_Bas_Typ);
11327 end if;
11328
eefd2467 11329 -- Bit_Order
dc3af7e2
AC
11330
11331 if Is_Record_Type (Typ) then
dc3af7e2
AC
11332 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
11333 and then Has_Rep_Item (Typ, Name_Bit_Order)
11334 then
11335 Set_Reverse_Bit_Order (Bas_Typ,
11336 Reverse_Bit_Order (Entity (Name
11337 (Get_Rep_Item (Typ, Name_Bit_Order)))));
11338 end if;
eefd2467
AC
11339 end if;
11340
8b64ed4c
TQ
11341 -- Scalar_Storage_Order
11342
11343 -- Note: the aspect is specified on a first subtype, but recorded
11344 -- in a flag of the base type!
eefd2467
AC
11345
11346 if (Is_Record_Type (Typ) or else Is_Array_Type (Typ))
bcdb6b04 11347 and then Typ = Bas_Typ
eefd2467 11348 then
eefd2467
AC
11349 -- For a type extension, always inherit from parent; otherwise
11350 -- inherit if no default applies. Note: we do not check for
11351 -- an explicit rep item on the parent type when inheriting,
11352 -- because the parent SSO may itself have been set by default.
dc3af7e2 11353
8b64ed4c
TQ
11354 if not Has_Rep_Item (First_Subtype (Typ),
11355 Name_Scalar_Storage_Order, False)
eefd2467 11356 and then (Is_Tagged_Type (Bas_Typ)
bcdb6b04
AC
11357 or else not (SSO_Set_Low_By_Default (Bas_Typ)
11358 or else
11359 SSO_Set_High_By_Default (Bas_Typ)))
dc3af7e2
AC
11360 then
11361 Set_Reverse_Storage_Order (Bas_Typ,
35e7063a
AC
11362 Reverse_Storage_Order
11363 (Implementation_Base_Type (Etype (Bas_Typ))));
220d1fd9
AC
11364
11365 -- Clear default SSO indications, since the inherited aspect
11366 -- which was set explicitly overrides the default.
11367
11368 Set_SSO_Set_Low_By_Default (Bas_Typ, False);
11369 Set_SSO_Set_High_By_Default (Bas_Typ, False);
dc3af7e2
AC
11370 end if;
11371 end if;
11372 end;
11373 end if;
11374 end Inherit_Aspects_At_Freeze_Point;
11375
996ae0b0
RK
11376 ----------------
11377 -- Initialize --
11378 ----------------
11379
11380 procedure Initialize is
11381 begin
105b5e65 11382 Address_Clause_Checks.Init;
996ae0b0 11383 Unchecked_Conversions.Init;
07aff4e3 11384
535a8637 11385 if AAMP_On_Target then
07aff4e3
AC
11386 Independence_Checks.Init;
11387 end if;
996ae0b0
RK
11388 end Initialize;
11389
77a40ec1
AC
11390 ---------------------------
11391 -- Install_Discriminants --
11392 ---------------------------
11393
11394 procedure Install_Discriminants (E : Entity_Id) is
11395 Disc : Entity_Id;
11396 Prev : Entity_Id;
11397 begin
11398 Disc := First_Discriminant (E);
11399 while Present (Disc) loop
11400 Prev := Current_Entity (Disc);
11401 Set_Current_Entity (Disc);
11402 Set_Is_Immediately_Visible (Disc);
11403 Set_Homonym (Disc, Prev);
11404 Next_Discriminant (Disc);
11405 end loop;
11406 end Install_Discriminants;
11407
996ae0b0
RK
11408 -------------------------
11409 -- Is_Operational_Item --
11410 -------------------------
11411
11412 function Is_Operational_Item (N : Node_Id) return Boolean is
11413 begin
11414 if Nkind (N) /= N_Attribute_Definition_Clause then
11415 return False;
616547fa 11416
996ae0b0
RK
11417 else
11418 declare
616547fa 11419 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
996ae0b0 11420 begin
e9f97e79 11421
47b79f78 11422 -- List of operational items is given in AARM 13.1(8.mm/1).
e9f97e79
AC
11423 -- It is clearly incomplete, as it does not include iterator
11424 -- aspects, among others.
11425
11426 return Id = Attribute_Constant_Indexing
11427 or else Id = Attribute_Default_Iterator
11428 or else Id = Attribute_Implicit_Dereference
11429 or else Id = Attribute_Input
11430 or else Id = Attribute_Iterator_Element
11431 or else Id = Attribute_Iterable
996ae0b0
RK
11432 or else Id = Attribute_Output
11433 or else Id = Attribute_Read
e9f97e79 11434 or else Id = Attribute_Variable_Indexing
07fc65c4
GB
11435 or else Id = Attribute_Write
11436 or else Id = Attribute_External_Tag;
996ae0b0
RK
11437 end;
11438 end if;
11439 end Is_Operational_Item;
11440
ee4eee0a
AC
11441 -------------------------
11442 -- Is_Predicate_Static --
11443 -------------------------
11444
fc3a3f3b
RD
11445 -- Note: the basic legality of the expression has already been checked, so
11446 -- we don't need to worry about cases or ranges on strings for example.
11447
ee4eee0a
AC
11448 function Is_Predicate_Static
11449 (Expr : Node_Id;
11450 Nam : Name_Id) return Boolean
11451 is
11452 function All_Static_Case_Alternatives (L : List_Id) return Boolean;
f9648959
AC
11453 -- Given a list of case expression alternatives, returns True if all
11454 -- the alternatives are static (have all static choices, and a static
11455 -- expression).
ee4eee0a
AC
11456
11457 function All_Static_Choices (L : List_Id) return Boolean;
c624298a 11458 -- Returns true if all elements of the list are OK static choices
ee4eee0a 11459 -- as defined below for Is_Static_Choice. Used for case expression
f9648959
AC
11460 -- alternatives and for the right operand of a membership test. An
11461 -- others_choice is static if the corresponding expression is static.
fd7215d7 11462 -- The staticness of the bounds is checked separately.
ee4eee0a
AC
11463
11464 function Is_Static_Choice (N : Node_Id) return Boolean;
11465 -- Returns True if N represents a static choice (static subtype, or
c624298a 11466 -- static subtype indication, or static expression, or static range).
ee4eee0a
AC
11467 --
11468 -- Note that this is a bit more inclusive than we actually need
11469 -- (in particular membership tests do not allow the use of subtype
c624298a 11470 -- indications). But that doesn't matter, we have already checked
ee4eee0a
AC
11471 -- that the construct is legal to get this far.
11472
11473 function Is_Type_Ref (N : Node_Id) return Boolean;
11474 pragma Inline (Is_Type_Ref);
f9648959
AC
11475 -- Returns True if N is a reference to the type for the predicate in the
11476 -- expression (i.e. if it is an identifier whose Chars field matches the
11477 -- Nam given in the call). N must not be parenthesized, if the type name
11478 -- appears in parens, this routine will return False.
ee4eee0a
AC
11479
11480 ----------------------------------
11481 -- All_Static_Case_Alternatives --
11482 ----------------------------------
11483
11484 function All_Static_Case_Alternatives (L : List_Id) return Boolean is
11485 N : Node_Id;
11486
11487 begin
11488 N := First (L);
11489 while Present (N) loop
11490 if not (All_Static_Choices (Discrete_Choices (N))
11491 and then Is_OK_Static_Expression (Expression (N)))
11492 then
11493 return False;
11494 end if;
11495
11496 Next (N);
11497 end loop;
11498
11499 return True;
11500 end All_Static_Case_Alternatives;
11501
11502 ------------------------
11503 -- All_Static_Choices --
11504 ------------------------
11505
11506 function All_Static_Choices (L : List_Id) return Boolean is
11507 N : Node_Id;
11508
11509 begin
11510 N := First (L);
11511 while Present (N) loop
11512 if not Is_Static_Choice (N) then
11513 return False;
11514 end if;
11515
11516 Next (N);
11517 end loop;
11518
11519 return True;
11520 end All_Static_Choices;
11521
11522 ----------------------
11523 -- Is_Static_Choice --
11524 ----------------------
11525
11526 function Is_Static_Choice (N : Node_Id) return Boolean is
11527 begin
fd7215d7
AC
11528 return Nkind (N) = N_Others_Choice
11529 or else Is_OK_Static_Expression (N)
ee4eee0a
AC
11530 or else (Is_Entity_Name (N) and then Is_Type (Entity (N))
11531 and then Is_OK_Static_Subtype (Entity (N)))
11532 or else (Nkind (N) = N_Subtype_Indication
11533 and then Is_OK_Static_Subtype (Entity (N)))
11534 or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N));
11535 end Is_Static_Choice;
11536
11537 -----------------
11538 -- Is_Type_Ref --
11539 -----------------
11540
11541 function Is_Type_Ref (N : Node_Id) return Boolean is
11542 begin
11543 return Nkind (N) = N_Identifier
11544 and then Chars (N) = Nam
11545 and then Paren_Count (N) = 0;
11546 end Is_Type_Ref;
11547
11548 -- Start of processing for Is_Predicate_Static
11549
11550 begin
ee4eee0a
AC
11551 -- Predicate_Static means one of the following holds. Numbers are the
11552 -- corresponding paragraph numbers in (RM 3.2.4(16-22)).
11553
11554 -- 16: A static expression
11555
11556 if Is_OK_Static_Expression (Expr) then
11557 return True;
11558
11559 -- 17: A membership test whose simple_expression is the current
11560 -- instance, and whose membership_choice_list meets the requirements
11561 -- for a static membership test.
11562
11563 elsif Nkind (Expr) in N_Membership_Test
11564 and then ((Present (Right_Opnd (Expr))
11565 and then Is_Static_Choice (Right_Opnd (Expr)))
11566 or else
11567 (Present (Alternatives (Expr))
11568 and then All_Static_Choices (Alternatives (Expr))))
11569 then
11570 return True;
11571
11572 -- 18. A case_expression whose selecting_expression is the current
11573 -- instance, and whose dependent expressions are static expressions.
11574
11575 elsif Nkind (Expr) = N_Case_Expression
11576 and then Is_Type_Ref (Expression (Expr))
11577 and then All_Static_Case_Alternatives (Alternatives (Expr))
11578 then
11579 return True;
11580
11581 -- 19. A call to a predefined equality or ordering operator, where one
11582 -- operand is the current instance, and the other is a static
11583 -- expression.
11584
fc3a3f3b
RD
11585 -- Note: the RM is clearly wrong here in not excluding string types.
11586 -- Without this exclusion, we would allow expressions like X > "ABC"
11587 -- to be considered as predicate-static, which is clearly not intended,
11588 -- since the idea is for predicate-static to be a subset of normal
11589 -- static expressions (and "DEF" > "ABC" is not a static expression).
11590
11591 -- However, we do allow internally generated (not from source) equality
11592 -- and inequality operations to be valid on strings (this helps deal
11593 -- with cases where we transform A in "ABC" to A = "ABC).
11594
ee4eee0a 11595 elsif Nkind (Expr) in N_Op_Compare
fc3a3f3b
RD
11596 and then ((not Is_String_Type (Etype (Left_Opnd (Expr))))
11597 or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne)
11598 and then not Comes_From_Source (Expr)))
ee4eee0a
AC
11599 and then ((Is_Type_Ref (Left_Opnd (Expr))
11600 and then Is_OK_Static_Expression (Right_Opnd (Expr)))
11601 or else
11602 (Is_Type_Ref (Right_Opnd (Expr))
11603 and then Is_OK_Static_Expression (Left_Opnd (Expr))))
11604 then
11605 return True;
11606
11607 -- 20. A call to a predefined boolean logical operator, where each
11608 -- operand is predicate-static.
11609
11610 elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor)
11611 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11612 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11613 or else
11614 (Nkind (Expr) = N_Op_Not
11615 and then Is_Predicate_Static (Right_Opnd (Expr), Nam))
11616 then
11617 return True;
11618
11619 -- 21. A short-circuit control form where both operands are
11620 -- predicate-static.
11621
11622 elsif Nkind (Expr) in N_Short_Circuit
11623 and then Is_Predicate_Static (Left_Opnd (Expr), Nam)
11624 and then Is_Predicate_Static (Right_Opnd (Expr), Nam)
11625 then
11626 return True;
11627
11628 -- 22. A parenthesized predicate-static expression. This does not
11629 -- require any special test, since we just ignore paren levels in
11630 -- all the cases above.
11631
11632 -- One more test that is an implementation artifact caused by the fact
87e69720 11633 -- that we are analyzing not the original expression, but the generated
ee4eee0a 11634 -- expression in the body of the predicate function. This can include
c624298a 11635 -- references to inherited predicates, so that the expression we are
ee4eee0a
AC
11636 -- processing looks like:
11637
9bdc432a 11638 -- xxPredicate (typ (Inns)) and then expression
ee4eee0a
AC
11639
11640 -- Where the call is to a Predicate function for an inherited predicate.
622599c6
RD
11641 -- We simply ignore such a call, which could be to either a dynamic or
11642 -- a static predicate. Note that if the parent predicate is dynamic then
11643 -- eventually this type will be marked as dynamic, but you are allowed
11644 -- to specify a static predicate for a subtype which is inheriting a
11645 -- dynamic predicate, so the static predicate validation here ignores
11646 -- the inherited predicate even if it is dynamic.
ee4eee0a
AC
11647
11648 elsif Nkind (Expr) = N_Function_Call
11649 and then Is_Predicate_Function (Entity (Name (Expr)))
11650 then
11651 return True;
11652
11653 -- That's an exhaustive list of tests, all other cases are not
c624298a 11654 -- predicate-static, so we return False.
ee4eee0a
AC
11655
11656 else
11657 return False;
11658 end if;
11659 end Is_Predicate_Static;
11660
cf28c974
RD
11661 ---------------------
11662 -- Kill_Rep_Clause --
11663 ---------------------
11664
11665 procedure Kill_Rep_Clause (N : Node_Id) is
11666 begin
11667 pragma Assert (Ignore_Rep_Clauses);
783d035b
AC
11668
11669 -- Note: we use Replace rather than Rewrite, because we don't want
11670 -- ASIS to be able to use Original_Node to dig out the (undecorated)
11671 -- rep clause that is being replaced.
11672
39678b1c 11673 Replace (N, Make_Null_Statement (Sloc (N)));
783d035b
AC
11674
11675 -- The null statement must be marked as not coming from source. This is
8b034336 11676 -- so that ASIS ignores it, and also the back end does not expect bogus
783d035b
AC
11677 -- "from source" null statements in weird places (e.g. in declarative
11678 -- regions where such null statements are not allowed).
11679
11680 Set_Comes_From_Source (N, False);
cf28c974
RD
11681 end Kill_Rep_Clause;
11682
996ae0b0
RK
11683 ------------------
11684 -- Minimum_Size --
11685 ------------------
11686
11687 function Minimum_Size
11688 (T : Entity_Id;
b7e429ab 11689 Biased : Boolean := False) return Nat
996ae0b0
RK
11690 is
11691 Lo : Uint := No_Uint;
11692 Hi : Uint := No_Uint;
11693 LoR : Ureal := No_Ureal;
11694 HiR : Ureal := No_Ureal;
11695 LoSet : Boolean := False;
11696 HiSet : Boolean := False;
11697 B : Uint;
11698 S : Nat;
11699 Ancest : Entity_Id;
07fc65c4 11700 R_Typ : constant Entity_Id := Root_Type (T);
996ae0b0
RK
11701
11702 begin
11703 -- If bad type, return 0
11704
11705 if T = Any_Type then
11706 return 0;
11707
11708 -- For generic types, just return zero. There cannot be any legitimate
11709 -- need to know such a size, but this routine may be called with a
11710 -- generic type as part of normal processing.
11711
dc06dd83 11712 elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then
996ae0b0
RK
11713 return 0;
11714
ba0c6e47 11715 -- Access types (cannot have size smaller than System.Address)
996ae0b0
RK
11716
11717 elsif Is_Access_Type (T) then
ba0c6e47 11718 return System_Address_Size;
996ae0b0
RK
11719
11720 -- Floating-point types
11721
11722 elsif Is_Floating_Point_Type (T) then
07fc65c4 11723 return UI_To_Int (Esize (R_Typ));
996ae0b0
RK
11724
11725 -- Discrete types
11726
11727 elsif Is_Discrete_Type (T) then
11728
0503c53a
RD
11729 -- The following loop is looking for the nearest compile time known
11730 -- bounds following the ancestor subtype chain. The idea is to find
11731 -- the most restrictive known bounds information.
996ae0b0
RK
11732
11733 Ancest := T;
11734 loop
11735 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11736 return 0;
11737 end if;
11738
11739 if not LoSet then
11740 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
11741 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
11742 LoSet := True;
11743 exit when HiSet;
11744 end if;
11745 end if;
11746
11747 if not HiSet then
11748 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
11749 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
11750 HiSet := True;
11751 exit when LoSet;
11752 end if;
11753 end if;
11754
11755 Ancest := Ancestor_Subtype (Ancest);
11756
11757 if No (Ancest) then
11758 Ancest := Base_Type (T);
11759
11760 if Is_Generic_Type (Ancest) then
11761 return 0;
11762 end if;
11763 end if;
11764 end loop;
11765
11766 -- Fixed-point types. We can't simply use Expr_Value to get the
0503c53a
RD
11767 -- Corresponding_Integer_Value values of the bounds, since these do not
11768 -- get set till the type is frozen, and this routine can be called
11769 -- before the type is frozen. Similarly the test for bounds being static
11770 -- needs to include the case where we have unanalyzed real literals for
11771 -- the same reason.
996ae0b0
RK
11772
11773 elsif Is_Fixed_Point_Type (T) then
11774
0503c53a
RD
11775 -- The following loop is looking for the nearest compile time known
11776 -- bounds following the ancestor subtype chain. The idea is to find
11777 -- the most restrictive known bounds information.
996ae0b0
RK
11778
11779 Ancest := T;
11780 loop
11781 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
11782 return 0;
11783 end if;
11784
800621e0
RD
11785 -- Note: In the following two tests for LoSet and HiSet, it may
11786 -- seem redundant to test for N_Real_Literal here since normally
11787 -- one would assume that the test for the value being known at
11788 -- compile time includes this case. However, there is a glitch.
11789 -- If the real literal comes from folding a non-static expression,
11790 -- then we don't consider any non- static expression to be known
11791 -- at compile time if we are in configurable run time mode (needed
11792 -- in some cases to give a clearer definition of what is and what
11793 -- is not accepted). So the test is indeed needed. Without it, we
11794 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
11795
996ae0b0
RK
11796 if not LoSet then
11797 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
11798 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
11799 then
11800 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
11801 LoSet := True;
11802 exit when HiSet;
11803 end if;
11804 end if;
11805
11806 if not HiSet then
11807 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
11808 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
11809 then
11810 HiR := Expr_Value_R (Type_High_Bound (Ancest));
11811 HiSet := True;
11812 exit when LoSet;
11813 end if;
11814 end if;
11815
11816 Ancest := Ancestor_Subtype (Ancest);
11817
11818 if No (Ancest) then
11819 Ancest := Base_Type (T);
11820
11821 if Is_Generic_Type (Ancest) then
11822 return 0;
11823 end if;
11824 end if;
11825 end loop;
11826
11827 Lo := UR_To_Uint (LoR / Small_Value (T));
11828 Hi := UR_To_Uint (HiR / Small_Value (T));
11829
11830 -- No other types allowed
11831
11832 else
11833 raise Program_Error;
11834 end if;
11835
a5b62485 11836 -- Fall through with Hi and Lo set. Deal with biased case
996ae0b0 11837
4ae23b62
AC
11838 if (Biased
11839 and then not Is_Fixed_Point_Type (T)
11840 and then not (Is_Enumeration_Type (T)
11841 and then Has_Non_Standard_Rep (T)))
996ae0b0
RK
11842 or else Has_Biased_Representation (T)
11843 then
11844 Hi := Hi - Lo;
11845 Lo := Uint_0;
11846 end if;
11847
57d22af2
AC
11848 -- Null range case, size is always zero. We only do this in the discrete
11849 -- type case, since that's the odd case that came up. Probably we should
11850 -- also do this in the fixed-point case, but doing so causes peculiar
11851 -- gigi failures, and it is not worth worrying about this incredibly
11852 -- marginal case (explicit null-range fixed-point type declarations)???
11853
11854 if Lo > Hi and then Is_Discrete_Type (T) then
11855 S := 0;
11856
996ae0b0 11857 -- Signed case. Note that we consider types like range 1 .. -1 to be
0503c53a 11858 -- signed for the purpose of computing the size, since the bounds have
f3d57416 11859 -- to be accommodated in the base type.
996ae0b0 11860
57d22af2 11861 elsif Lo < 0 or else Hi < 0 then
996ae0b0
RK
11862 S := 1;
11863 B := Uint_1;
11864
638e383e
JM
11865 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
11866 -- Note that we accommodate the case where the bounds cross. This
996ae0b0
RK
11867 -- can happen either because of the way the bounds are declared
11868 -- or because of the algorithm in Freeze_Fixed_Point_Type.
11869
11870 while Lo < -B
11871 or else Hi < -B
11872 or else Lo >= B
11873 or else Hi >= B
11874 loop
11875 B := Uint_2 ** S;
11876 S := S + 1;
11877 end loop;
11878
11879 -- Unsigned case
11880
11881 else
11882 -- If both bounds are positive, make sure that both are represen-
11883 -- table in the case where the bounds are crossed. This can happen
11884 -- either because of the way the bounds are declared, or because of
11885 -- the algorithm in Freeze_Fixed_Point_Type.
11886
11887 if Lo > Hi then
11888 Hi := Lo;
11889 end if;
11890
638e383e 11891 -- S = size, (can accommodate 0 .. (2**size - 1))
996ae0b0
RK
11892
11893 S := 0;
11894 while Hi >= Uint_2 ** S loop
11895 S := S + 1;
11896 end loop;
11897 end if;
11898
11899 return S;
11900 end Minimum_Size;
11901
affbee12
RD
11902 ---------------------------
11903 -- New_Stream_Subprogram --
11904 ---------------------------
996ae0b0 11905
affbee12
RD
11906 procedure New_Stream_Subprogram
11907 (N : Node_Id;
11908 Ent : Entity_Id;
11909 Subp : Entity_Id;
11910 Nam : TSS_Name_Type)
996ae0b0
RK
11911 is
11912 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 11913 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
07fc65c4 11914 Subp_Id : Entity_Id;
996ae0b0
RK
11915 Subp_Decl : Node_Id;
11916 F : Entity_Id;
11917 Etyp : Entity_Id;
11918
affbee12
RD
11919 Defer_Declaration : constant Boolean :=
11920 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
11921 -- For a tagged type, there is a declaration for each stream attribute
11922 -- at the freeze point, and we must generate only a completion of this
11923 -- declaration. We do the same for private types, because the full view
11924 -- might be tagged. Otherwise we generate a declaration at the point of
11925 -- the attribute definition clause.
11926
07fc65c4
GB
11927 function Build_Spec return Node_Id;
11928 -- Used for declaration and renaming declaration, so that this is
11929 -- treated as a renaming_as_body.
11930
11931 ----------------
11932 -- Build_Spec --
11933 ----------------
11934
b7e429ab 11935 function Build_Spec return Node_Id is
affbee12
RD
11936 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
11937 Formals : List_Id;
11938 Spec : Node_Id;
e4494292 11939 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
affbee12 11940
07fc65c4 11941 begin
fbf5a39b 11942 Subp_Id := Make_Defining_Identifier (Loc, Sname);
07fc65c4 11943
affbee12
RD
11944 -- S : access Root_Stream_Type'Class
11945
11946 Formals := New_List (
11947 Make_Parameter_Specification (Loc,
11948 Defining_Identifier =>
11949 Make_Defining_Identifier (Loc, Name_S),
11950 Parameter_Type =>
11951 Make_Access_Definition (Loc,
11952 Subtype_Mark =>
e4494292 11953 New_Occurrence_Of (
affbee12
RD
11954 Designated_Type (Etype (F)), Loc))));
11955
11956 if Nam = TSS_Stream_Input then
ae05cdd6
RD
11957 Spec :=
11958 Make_Function_Specification (Loc,
11959 Defining_Unit_Name => Subp_Id,
11960 Parameter_Specifications => Formals,
11961 Result_Definition => T_Ref);
affbee12
RD
11962 else
11963 -- V : [out] T
07fc65c4 11964
affbee12
RD
11965 Append_To (Formals,
11966 Make_Parameter_Specification (Loc,
11967 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
11968 Out_Present => Out_P,
11969 Parameter_Type => T_Ref));
07fc65c4 11970
bce79204
AC
11971 Spec :=
11972 Make_Procedure_Specification (Loc,
11973 Defining_Unit_Name => Subp_Id,
11974 Parameter_Specifications => Formals);
affbee12 11975 end if;
07fc65c4 11976
affbee12
RD
11977 return Spec;
11978 end Build_Spec;
996ae0b0 11979
affbee12 11980 -- Start of processing for New_Stream_Subprogram
996ae0b0 11981
affbee12
RD
11982 begin
11983 F := First_Formal (Subp);
11984
11985 if Ekind (Subp) = E_Procedure then
11986 Etyp := Etype (Next_Formal (F));
996ae0b0 11987 else
affbee12 11988 Etyp := Etype (Subp);
996ae0b0 11989 end if;
07fc65c4 11990
affbee12
RD
11991 -- Prepare subprogram declaration and insert it as an action on the
11992 -- clause node. The visibility for this entity is used to test for
11993 -- visibility of the attribute definition clause (in the sense of
11994 -- 8.3(23) as amended by AI-195).
fbf5a39b 11995
affbee12 11996 if not Defer_Declaration then
07fc65c4
GB
11997 Subp_Decl :=
11998 Make_Subprogram_Declaration (Loc,
11999 Specification => Build_Spec);
affbee12
RD
12000
12001 -- For a tagged type, there is always a visible declaration for each
8f7770f9 12002 -- stream TSS (it is a predefined primitive operation), and the
affbee12
RD
12003 -- completion of this declaration occurs at the freeze point, which is
12004 -- not always visible at places where the attribute definition clause is
12005 -- visible. So, we create a dummy entity here for the purpose of
12006 -- tracking the visibility of the attribute definition clause itself.
12007
12008 else
12009 Subp_Id :=
7675ad4f 12010 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
affbee12
RD
12011 Subp_Decl :=
12012 Make_Object_Declaration (Loc,
12013 Defining_Identifier => Subp_Id,
12014 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
07fc65c4
GB
12015 end if;
12016
affbee12
RD
12017 Insert_Action (N, Subp_Decl);
12018 Set_Entity (N, Subp_Id);
12019
996ae0b0
RK
12020 Subp_Decl :=
12021 Make_Subprogram_Renaming_Declaration (Loc,
07fc65c4 12022 Specification => Build_Spec,
e4494292 12023 Name => New_Occurrence_Of (Subp, Loc));
996ae0b0 12024
affbee12 12025 if Defer_Declaration then
996ae0b0
RK
12026 Set_TSS (Base_Type (Ent), Subp_Id);
12027 else
12028 Insert_Action (N, Subp_Decl);
12029 Copy_TSS (Subp_Id, Base_Type (Ent));
12030 end if;
affbee12 12031 end New_Stream_Subprogram;
996ae0b0 12032
77a40ec1
AC
12033 ------------------------------------------
12034 -- Push_Scope_And_Install_Discriminants --
12035 ------------------------------------------
12036
12037 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
12038 begin
12039 if Has_Discriminants (E) then
12040 Push_Scope (E);
12041
3ddfabe3 12042 -- Make the discriminants visible for type declarations and protected
77a40ec1
AC
12043 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
12044
12045 if Nkind (Parent (E)) /= N_Subtype_Declaration then
12046 Install_Discriminants (E);
12047 end if;
12048 end if;
12049 end Push_Scope_And_Install_Discriminants;
12050
996ae0b0
RK
12051 ------------------------
12052 -- Rep_Item_Too_Early --
12053 ------------------------
12054
0da2c8ac 12055 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
996ae0b0 12056 begin
affbee12 12057 -- Cannot apply non-operational rep items to generic types
996ae0b0 12058
07fc65c4
GB
12059 if Is_Operational_Item (N) then
12060 return False;
12061
12062 elsif Is_Type (T)
996ae0b0 12063 and then Is_Generic_Type (Root_Type (T))
5f6061af
AC
12064 and then (Nkind (N) /= N_Pragma
12065 or else Get_Pragma_Id (N) /= Pragma_Convention)
996ae0b0 12066 then
ed2233dc 12067 Error_Msg_N ("representation item not allowed for generic type", N);
996ae0b0
RK
12068 return True;
12069 end if;
12070
0503c53a 12071 -- Otherwise check for incomplete type
996ae0b0
RK
12072
12073 if Is_Incomplete_Or_Private_Type (T)
12074 and then No (Underlying_Type (T))
a01b9df6
AC
12075 and then
12076 (Nkind (N) /= N_Pragma
d2d9cc22 12077 or else Get_Pragma_Id (N) /= Pragma_Import)
996ae0b0
RK
12078 then
12079 Error_Msg_N
12080 ("representation item must be after full type declaration", N);
12081 return True;
12082
f3d57416 12083 -- If the type has incomplete components, a representation clause is
996ae0b0
RK
12084 -- illegal but stream attributes and Convention pragmas are correct.
12085
12086 elsif Has_Private_Component (T) then
07fc65c4 12087 if Nkind (N) = N_Pragma then
996ae0b0 12088 return False;
616547fa 12089
996ae0b0
RK
12090 else
12091 Error_Msg_N
12092 ("representation item must appear after type is fully defined",
12093 N);
12094 return True;
12095 end if;
12096 else
12097 return False;
12098 end if;
12099 end Rep_Item_Too_Early;
12100
12101 -----------------------
12102 -- Rep_Item_Too_Late --
12103 -----------------------
12104
12105 function Rep_Item_Too_Late
12106 (T : Entity_Id;
12107 N : Node_Id;
b7e429ab 12108 FOnly : Boolean := False) return Boolean
996ae0b0
RK
12109 is
12110 S : Entity_Id;
12111 Parent_Type : Entity_Id;
12112
84c0a895
AC
12113 procedure No_Type_Rep_Item;
12114 -- Output message indicating that no type-related aspects can be
12115 -- specified due to some property of the parent type.
12116
996ae0b0 12117 procedure Too_Late;
84c0a895
AC
12118 -- Output message for an aspect being specified too late
12119
12120 -- Note that neither of the above errors is considered a serious one,
12121 -- since the effect is simply that we ignore the representation clause
12122 -- in these cases.
ab01e614
AC
12123 -- Is this really true? In any case if we make this change we must
12124 -- document the requirement in the spec of Rep_Item_Too_Late that
12125 -- if True is returned, then the rep item must be completely ignored???
84c0a895
AC
12126
12127 ----------------------
12128 -- No_Type_Rep_Item --
12129 ----------------------
12130
12131 procedure No_Type_Rep_Item is
12132 begin
12133 Error_Msg_N ("|type-related representation item not permitted!", N);
12134 end No_Type_Rep_Item;
1c6c6771
ES
12135
12136 --------------
12137 -- Too_Late --
12138 --------------
996ae0b0
RK
12139
12140 procedure Too_Late is
12141 begin
5b75bf57
AC
12142 -- Other compilers seem more relaxed about rep items appearing too
12143 -- late. Since analysis tools typically don't care about rep items
12144 -- anyway, no reason to be too strict about this.
12145
303fbb20
AC
12146 if not Relaxed_RM_Semantics then
12147 Error_Msg_N ("|representation item appears too late!", N);
12148 end if;
996ae0b0
RK
12149 end Too_Late;
12150
12151 -- Start of processing for Rep_Item_Too_Late
12152
12153 begin
51e641f8 12154 -- First make sure entity is not frozen (RM 13.1(9))
996ae0b0
RK
12155
12156 if Is_Frozen (T)
51e641f8
AC
12157
12158 -- Exclude imported types, which may be frozen if they appear in a
12159 -- representation clause for a local type.
12160
7b56a91b 12161 and then not From_Limited_With (T)
51e641f8 12162
303fbb20 12163 -- Exclude generated entities (not coming from source). The common
51e641f8
AC
12164 -- case is when we generate a renaming which prematurely freezes the
12165 -- renamed internal entity, but we still want to be able to set copies
12166 -- of attribute values such as Size/Alignment.
12167
12168 and then Comes_From_Source (T)
996ae0b0 12169 then
7e22a38c
AC
12170 -- A self-referential aspect is illegal if it forces freezing the
12171 -- entity before the corresponding pragma has been analyzed.
12172
12173 if Nkind_In (N, N_Attribute_Definition_Clause, N_Pragma)
12174 and then From_Aspect_Specification (N)
12175 then
12176 Error_Msg_NE
12177 ("aspect specification causes premature freezing of&", T, N);
12178 Set_Has_Delayed_Freeze (T, False);
12179 return True;
12180 end if;
12181
996ae0b0
RK
12182 Too_Late;
12183 S := First_Subtype (T);
12184
12185 if Present (Freeze_Node (S)) then
ab01e614
AC
12186 if not Relaxed_RM_Semantics then
12187 Error_Msg_NE
12188 ("??no more representation items for }", Freeze_Node (S), S);
12189 end if;
996ae0b0
RK
12190 end if;
12191
12192 return True;
12193
1fb63e89 12194 -- Check for case of untagged derived type whose parent either has
84c0a895
AC
12195 -- primitive operations, or is a by reference type (RM 13.1(10)). In
12196 -- this case we do not output a Too_Late message, since there is no
12197 -- earlier point where the rep item could be placed to make it legal.
996ae0b0
RK
12198
12199 elsif Is_Type (T)
12200 and then not FOnly
12201 and then Is_Derived_Type (T)
12202 and then not Is_Tagged_Type (T)
12203 then
12204 Parent_Type := Etype (Base_Type (T));
12205
12206 if Has_Primitive_Operations (Parent_Type) then
84c0a895 12207 No_Type_Rep_Item;
ab01e614
AC
12208
12209 if not Relaxed_RM_Semantics then
12210 Error_Msg_NE
12211 ("\parent type & has primitive operations!", N, Parent_Type);
12212 end if;
12213
996ae0b0
RK
12214 return True;
12215
12216 elsif Is_By_Reference_Type (Parent_Type) then
84c0a895 12217 No_Type_Rep_Item;
ab01e614
AC
12218
12219 if not Relaxed_RM_Semantics then
12220 Error_Msg_NE
12221 ("\parent type & is a by reference type!", N, Parent_Type);
12222 end if;
12223
996ae0b0
RK
12224 return True;
12225 end if;
12226 end if;
12227
ab01e614
AC
12228 -- No error, but one more warning to consider. The RM (surprisingly)
12229 -- allows this pattern:
12230
12231 -- type S is ...
12232 -- primitive operations for S
12233 -- type R is new S;
12234 -- rep clause for S
12235
12236 -- Meaning that calls on the primitive operations of S for values of
12237 -- type R may require possibly expensive implicit conversion operations.
12238 -- This is not an error, but is worth a warning.
12239
12240 if not Relaxed_RM_Semantics and then Is_Type (T) then
12241 declare
12242 DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T));
12243
12244 begin
12245 if Present (DTL)
12246 and then Has_Primitive_Operations (Base_Type (T))
12247
12248 -- For now, do not generate this warning for the case of aspect
12249 -- specification using Ada 2012 syntax, since we get wrong
12250 -- messages we do not understand. The whole business of derived
12251 -- types and rep items seems a bit confused when aspects are
12252 -- used, since the aspects are not evaluated till freeze time.
12253
12254 and then not From_Aspect_Specification (N)
12255 then
12256 Error_Msg_Sloc := Sloc (DTL);
12257 Error_Msg_N
12258 ("representation item for& appears after derived type "
12259 & "declaration#??", N);
12260 Error_Msg_NE
12261 ("\may result in implicit conversions for primitive "
12262 & "operations of&??", N, T);
12263 Error_Msg_NE
12264 ("\to change representations when called with arguments "
12265 & "of type&??", N, DTL);
12266 end if;
12267 end;
12268 end if;
12269
800621e0
RD
12270 -- No error, link item into head of chain of rep items for the entity,
12271 -- but avoid chaining if we have an overloadable entity, and the pragma
12272 -- is one that can apply to multiple overloaded entities.
12273
616547fa 12274 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
0503c53a
RD
12275 declare
12276 Pname : constant Name_Id := Pragma_Name (N);
12277 begin
b69cd36a
AC
12278 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
12279 Name_External, Name_Interface)
0503c53a
RD
12280 then
12281 return False;
12282 end if;
12283 end;
800621e0
RD
12284 end if;
12285
0503c53a 12286 Record_Rep_Item (T, N);
996ae0b0
RK
12287 return False;
12288 end Rep_Item_Too_Late;
12289
3b097d11
AC
12290 -------------------------------------
12291 -- Replace_Type_References_Generic --
12292 -------------------------------------
12293
8b034336
AC
12294 procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is
12295 TName : constant Name_Id := Chars (T);
3b097d11 12296
3ddfabe3 12297 function Replace_Type_Ref (N : Node_Id) return Traverse_Result;
3b097d11
AC
12298 -- Processes a single node in the traversal procedure below, checking
12299 -- if node N should be replaced, and if so, doing the replacement.
12300
9e3be36e
ES
12301 function Visible_Component (Comp : Name_Id) return Entity_Id;
12302 -- Given an identifier in the expression, check whether there is a
12303 -- discriminant or component of the type that is directy visible, and
12304 -- rewrite it as the corresponding selected component of the formal of
12305 -- the subprogram. The entity is located by a sequential search, which
12306 -- seems acceptable given the typical size of component lists and check
12307 -- expressions. Possible optimization ???
12308
3ddfabe3
AC
12309 ----------------------
12310 -- Replace_Type_Ref --
12311 ----------------------
3b097d11 12312
3ddfabe3 12313 function Replace_Type_Ref (N : Node_Id) return Traverse_Result is
9e3be36e
ES
12314 Loc : constant Source_Ptr := Sloc (N);
12315 C : Entity_Id;
12316 S : Entity_Id;
12317 P : Node_Id;
3b097d11 12318
9e3be36e
ES
12319 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id);
12320 -- Add the proper prefix to a reference to a component of the
12321 -- type when it is not already a selected component.
12322
12323 ----------------
12324 -- Add_Prefix --
12325 ----------------
3b097d11 12326
9e3be36e
ES
12327 procedure Add_Prefix (Ref : Node_Id; Comp : Entity_Id) is
12328 begin
12329 Rewrite (Ref,
12330 Make_Selected_Component (Loc,
12331 Prefix => New_Occurrence_Of (T, Loc),
12332 Selector_Name => New_Occurrence_Of (Comp, Loc)));
12333 Replace_Type_Reference (Prefix (Ref));
12334 end Add_Prefix;
12335
12336 -- Start of processing for Replace_Type_Ref
12337
12338 begin
3b097d11
AC
12339 if Nkind (N) = N_Identifier then
12340
3ddfabe3
AC
12341 -- If not the type name, check whether it is a reference to some
12342 -- other type, which must be frozen before the predicate function
12343 -- is analyzed, i.e. before the freeze node of the type to which
12344 -- the predicate applies.
3b097d11
AC
12345
12346 if Chars (N) /= TName then
8b034336
AC
12347 if Present (Current_Entity (N))
12348 and then Is_Type (Current_Entity (N))
12349 then
12350 Freeze_Before (Freeze_Node (T), Current_Entity (N));
12351 end if;
12352
9e3be36e
ES
12353 -- The components of the type are directly visible and can
12354 -- be referenced without a prefix.
12355
12356 if Nkind (Parent (N)) = N_Selected_Component then
12357 null;
12358
12359 -- In expression C (I), C may be a directly visible function
12360 -- or a visible component that has an array type. Disambiguate
12361 -- by examining the component type.
12362
12363 elsif Nkind (Parent (N)) = N_Indexed_Component
12364 and then N = Prefix (Parent (N))
12365 then
12366 C := Visible_Component (Chars (N));
12367
12368 if Present (C) and then Is_Array_Type (Etype (C)) then
12369 Add_Prefix (N, C);
12370 end if;
12371
12372 else
12373 C := Visible_Component (Chars (N));
12374
12375 if Present (C) then
12376 Add_Prefix (N, C);
12377 end if;
12378 end if;
12379
3b097d11
AC
12380 return Skip;
12381
12382 -- Otherwise do the replacement and we are done with this node
12383
12384 else
12385 Replace_Type_Reference (N);
12386 return Skip;
12387 end if;
12388
3ddfabe3
AC
12389 -- Case of selected component (which is what a qualification looks
12390 -- like in the unanalyzed tree, which is what we have.
3b097d11
AC
12391
12392 elsif Nkind (N) = N_Selected_Component then
12393
3ddfabe3
AC
12394 -- If selector name is not our type, keeping going (we might still
12395 -- have an occurrence of the type in the prefix).
3b097d11
AC
12396
12397 if Nkind (Selector_Name (N)) /= N_Identifier
12398 or else Chars (Selector_Name (N)) /= TName
12399 then
12400 return OK;
12401
12402 -- Selector name is our type, check qualification
12403
12404 else
12405 -- Loop through scopes and prefixes, doing comparison
12406
12407 S := Current_Scope;
12408 P := Prefix (N);
12409 loop
12410 -- Continue if no more scopes or scope with no name
12411
12412 if No (S) or else Nkind (S) not in N_Has_Chars then
12413 return OK;
12414 end if;
12415
3ddfabe3
AC
12416 -- Do replace if prefix is an identifier matching the scope
12417 -- that we are currently looking at.
3b097d11
AC
12418
12419 if Nkind (P) = N_Identifier
12420 and then Chars (P) = Chars (S)
12421 then
12422 Replace_Type_Reference (N);
12423 return Skip;
12424 end if;
12425
3ddfabe3
AC
12426 -- Go check scope above us if prefix is itself of the form
12427 -- of a selected component, whose selector matches the scope
12428 -- we are currently looking at.
3b097d11
AC
12429
12430 if Nkind (P) = N_Selected_Component
12431 and then Nkind (Selector_Name (P)) = N_Identifier
12432 and then Chars (Selector_Name (P)) = Chars (S)
12433 then
12434 S := Scope (S);
12435 P := Prefix (P);
12436
12437 -- For anything else, we don't have a match, so keep on
12438 -- going, there are still some weird cases where we may
12439 -- still have a replacement within the prefix.
12440
12441 else
12442 return OK;
12443 end if;
12444 end loop;
12445 end if;
12446
e477d718 12447 -- Continue for any other node kind
3b097d11
AC
12448
12449 else
12450 return OK;
12451 end if;
3ddfabe3
AC
12452 end Replace_Type_Ref;
12453
9e3be36e
ES
12454 -----------------------
12455 -- Visible_Component --
12456 -----------------------
12457
12458 function Visible_Component (Comp : Name_Id) return Entity_Id is
12459 E : Entity_Id;
12460 begin
12461 if Ekind (T) /= E_Record_Type then
12462 return Empty;
12463
12464 else
12465 E := First_Entity (T);
12466 while Present (E) loop
12467 if Comes_From_Source (E)
12468 and then Chars (E) = Comp
12469 then
12470 return E;
12471 end if;
12472
12473 Next_Entity (E);
12474 end loop;
12475
12476 return Empty;
12477 end if;
12478 end Visible_Component;
12479
3ddfabe3 12480 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Type_Ref);
3b097d11
AC
12481
12482 begin
12483 Replace_Type_Refs (N);
12484 end Replace_Type_References_Generic;
12485
f06f5f6b
AC
12486 --------------------------------
12487 -- Resolve_Aspect_Expressions --
12488 --------------------------------
12489
12490 procedure Resolve_Aspect_Expressions (E : Entity_Id) is
12491 ASN : Node_Id;
12492 A_Id : Aspect_Id;
12493 Expr : Node_Id;
12494
6905a049
AC
12495 function Resolve_Name (N : Node_Id) return Traverse_Result;
12496 -- Verify that all identifiers in the expression, with the exception
12497 -- of references to the current entity, denote visible entities. This
12498 -- is done only to detect visibility errors, as the expression will be
12499 -- properly analyzed/expanded during analysis of the predicate function
d74716b3
AC
12500 -- body. We omit quantified expressions from this test, given that they
12501 -- introduce a local identifier that would require proper expansion to
12502 -- handle properly.
6905a049
AC
12503
12504 ------------------
12505 -- Resolve_Name --
12506 ------------------
12507
12508 function Resolve_Name (N : Node_Id) return Traverse_Result is
12509 begin
12510 if Nkind (N) = N_Selected_Component then
12511 if Nkind (Prefix (N)) = N_Identifier
12512 and then Chars (Prefix (N)) /= Chars (E)
12513 then
a14bbbb4 12514 Find_Selected_Component (N);
6905a049 12515 end if;
2cc7967f 12516
6905a049
AC
12517 return Skip;
12518
2cc7967f 12519 elsif Nkind (N) = N_Identifier and then Chars (N) /= Chars (E) then
6905a049
AC
12520 Find_Direct_Name (N);
12521 Set_Entity (N, Empty);
d74716b3
AC
12522
12523 elsif Nkind (N) = N_Quantified_Expression then
12524 return Skip;
6905a049
AC
12525 end if;
12526
12527 return OK;
12528 end Resolve_Name;
12529
12530 procedure Resolve_Aspect_Expression is new Traverse_Proc (Resolve_Name);
12531
2cc7967f
AC
12532 -- Start of processing for Resolve_Aspect_Expressions
12533
f06f5f6b
AC
12534 begin
12535 ASN := First_Rep_Item (E);
12536 while Present (ASN) loop
12537 if Nkind (ASN) = N_Aspect_Specification and then Entity (ASN) = E then
12538 A_Id := Get_Aspect_Id (ASN);
12539 Expr := Expression (ASN);
12540
12541 case A_Id is
3ddfabe3 12542
f06f5f6b
AC
12543 -- For now we only deal with aspects that do not generate
12544 -- subprograms, or that may mention current instances of
a2c314c7 12545 -- types. These will require special handling (???TBD).
f06f5f6b 12546
2cc7967f 12547 when Aspect_Predicate |
a2c314c7 12548 Aspect_Predicate_Failure |
3ddfabe3 12549 Aspect_Invariant =>
f06f5f6b
AC
12550 null;
12551
3ddfabe3
AC
12552 when Aspect_Dynamic_Predicate |
12553 Aspect_Static_Predicate =>
6905a049 12554
2cc7967f 12555 -- Build predicate function specification and preanalyze
6905a049
AC
12556 -- expression after type replacement.
12557
12558 if No (Predicate_Function (E)) then
12559 declare
12560 FDecl : constant Node_Id :=
2cc7967f 12561 Build_Predicate_Function_Declaration (E);
6905a049
AC
12562 pragma Unreferenced (FDecl);
12563 begin
12564 Resolve_Aspect_Expression (Expr);
12565 end;
12566 end if;
12567
f06f5f6b
AC
12568 when Pre_Post_Aspects =>
12569 null;
12570
12571 when Aspect_Iterable =>
12572 if Nkind (Expr) = N_Aggregate then
12573 declare
12574 Assoc : Node_Id;
12575
12576 begin
12577 Assoc := First (Component_Associations (Expr));
12578 while Present (Assoc) loop
12579 Find_Direct_Name (Expression (Assoc));
12580 Next (Assoc);
12581 end loop;
12582 end;
12583 end if;
12584
12585 when others =>
12586 if Present (Expr) then
12587 case Aspect_Argument (A_Id) is
12588 when Expression | Optional_Expression =>
12589 Analyze_And_Resolve (Expression (ASN));
12590
12591 when Name | Optional_Name =>
12592 if Nkind (Expr) = N_Identifier then
12593 Find_Direct_Name (Expr);
12594
12595 elsif Nkind (Expr) = N_Selected_Component then
12596 Find_Selected_Component (Expr);
12597
12598 else
12599 null;
12600 end if;
12601 end case;
12602 end if;
12603 end case;
12604 end if;
12605
b7737d1d 12606 ASN := Next_Rep_Item (ASN);
f06f5f6b
AC
12607 end loop;
12608 end Resolve_Aspect_Expressions;
12609
996ae0b0
RK
12610 -------------------------
12611 -- Same_Representation --
12612 -------------------------
12613
12614 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
12615 T1 : constant Entity_Id := Underlying_Type (Typ1);
12616 T2 : constant Entity_Id := Underlying_Type (Typ2);
12617
12618 begin
12619 -- A quick check, if base types are the same, then we definitely have
12620 -- the same representation, because the subtype specific representation
12621 -- attributes (Size and Alignment) do not affect representation from
12622 -- the point of view of this test.
12623
12624 if Base_Type (T1) = Base_Type (T2) then
12625 return True;
12626
12627 elsif Is_Private_Type (Base_Type (T2))
12628 and then Base_Type (T1) = Full_View (Base_Type (T2))
12629 then
12630 return True;
12631 end if;
12632
12633 -- Tagged types never have differing representations
12634
12635 if Is_Tagged_Type (T1) then
12636 return True;
12637 end if;
12638
12639 -- Representations are definitely different if conventions differ
12640
12641 if Convention (T1) /= Convention (T2) then
12642 return False;
12643 end if;
12644
03eb6036
AC
12645 -- Representations are different if component alignments or scalar
12646 -- storage orders differ.
996ae0b0
RK
12647
12648 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
752b81d9 12649 and then
996ae0b0 12650 (Is_Record_Type (T2) or else Is_Array_Type (T2))
03eb6036
AC
12651 and then
12652 (Component_Alignment (T1) /= Component_Alignment (T2)
dc06dd83 12653 or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
996ae0b0
RK
12654 then
12655 return False;
12656 end if;
12657
12658 -- For arrays, the only real issue is component size. If we know the
12659 -- component size for both arrays, and it is the same, then that's
12660 -- good enough to know we don't have a change of representation.
12661
12662 if Is_Array_Type (T1) then
12663 if Known_Component_Size (T1)
12664 and then Known_Component_Size (T2)
12665 and then Component_Size (T1) = Component_Size (T2)
12666 then
535a8637 12667 return True;
996ae0b0
RK
12668 end if;
12669 end if;
12670
12671 -- Types definitely have same representation if neither has non-standard
12672 -- representation since default representations are always consistent.
12673 -- If only one has non-standard representation, and the other does not,
12674 -- then we consider that they do not have the same representation. They
12675 -- might, but there is no way of telling early enough.
12676
12677 if Has_Non_Standard_Rep (T1) then
12678 if not Has_Non_Standard_Rep (T2) then
12679 return False;
12680 end if;
12681 else
12682 return not Has_Non_Standard_Rep (T2);
12683 end if;
12684
0503c53a
RD
12685 -- Here the two types both have non-standard representation, and we need
12686 -- to determine if they have the same non-standard representation.
996ae0b0
RK
12687
12688 -- For arrays, we simply need to test if the component sizes are the
12689 -- same. Pragma Pack is reflected in modified component sizes, so this
12690 -- check also deals with pragma Pack.
12691
12692 if Is_Array_Type (T1) then
12693 return Component_Size (T1) = Component_Size (T2);
12694
12695 -- Tagged types always have the same representation, because it is not
12696 -- possible to specify different representations for common fields.
12697
12698 elsif Is_Tagged_Type (T1) then
12699 return True;
12700
12701 -- Case of record types
12702
12703 elsif Is_Record_Type (T1) then
12704
12705 -- Packed status must conform
12706
12707 if Is_Packed (T1) /= Is_Packed (T2) then
12708 return False;
12709
12710 -- Otherwise we must check components. Typ2 maybe a constrained
12711 -- subtype with fewer components, so we compare the components
12712 -- of the base types.
12713
12714 else
12715 Record_Case : declare
12716 CD1, CD2 : Entity_Id;
12717
12718 function Same_Rep return Boolean;
12719 -- CD1 and CD2 are either components or discriminants. This
03eb6036 12720 -- function tests whether they have the same representation.
996ae0b0 12721
0da2c8ac
AC
12722 --------------
12723 -- Same_Rep --
12724 --------------
12725
996ae0b0
RK
12726 function Same_Rep return Boolean is
12727 begin
12728 if No (Component_Clause (CD1)) then
12729 return No (Component_Clause (CD2));
996ae0b0 12730 else
03eb6036
AC
12731 -- Note: at this point, component clauses have been
12732 -- normalized to the default bit order, so that the
12733 -- comparison of Component_Bit_Offsets is meaningful.
12734
996ae0b0
RK
12735 return
12736 Present (Component_Clause (CD2))
12737 and then
12738 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
12739 and then
12740 Esize (CD1) = Esize (CD2);
12741 end if;
12742 end Same_Rep;
12743
d8221f45 12744 -- Start of processing for Record_Case
996ae0b0
RK
12745
12746 begin
12747 if Has_Discriminants (T1) then
996ae0b0 12748
fbf5a39b
AC
12749 -- The number of discriminants may be different if the
12750 -- derived type has fewer (constrained by values). The
12751 -- invisible discriminants retain the representation of
12752 -- the original, so the discrepancy does not per se
12753 -- indicate a different representation.
12754
616547fa
AC
12755 CD1 := First_Discriminant (T1);
12756 CD2 := First_Discriminant (T2);
12757 while Present (CD1) and then Present (CD2) loop
996ae0b0
RK
12758 if not Same_Rep then
12759 return False;
12760 else
12761 Next_Discriminant (CD1);
12762 Next_Discriminant (CD2);
12763 end if;
12764 end loop;
12765 end if;
12766
12767 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
12768 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
996ae0b0
RK
12769 while Present (CD1) loop
12770 if not Same_Rep then
12771 return False;
12772 else
12773 Next_Component (CD1);
12774 Next_Component (CD2);
12775 end if;
12776 end loop;
12777
12778 return True;
12779 end Record_Case;
12780 end if;
12781
12782 -- For enumeration types, we must check each literal to see if the
12783 -- representation is the same. Note that we do not permit enumeration
f3d57416 12784 -- representation clauses for Character and Wide_Character, so these
996ae0b0
RK
12785 -- cases were already dealt with.
12786
12787 elsif Is_Enumeration_Type (T1) then
996ae0b0
RK
12788 Enumeration_Case : declare
12789 L1, L2 : Entity_Id;
12790
12791 begin
12792 L1 := First_Literal (T1);
12793 L2 := First_Literal (T2);
996ae0b0
RK
12794 while Present (L1) loop
12795 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
12796 return False;
12797 else
12798 Next_Literal (L1);
12799 Next_Literal (L2);
12800 end if;
12801 end loop;
12802
12803 return True;
996ae0b0
RK
12804 end Enumeration_Case;
12805
12806 -- Any other types have the same representation for these purposes
12807
12808 else
12809 return True;
12810 end if;
996ae0b0
RK
12811 end Same_Representation;
12812
b4f149c2
AC
12813 --------------------------------
12814 -- Resolve_Iterable_Operation --
12815 --------------------------------
12816
12817 procedure Resolve_Iterable_Operation
12818 (N : Node_Id;
12819 Cursor : Entity_Id;
12820 Typ : Entity_Id;
12821 Nam : Name_Id)
12822 is
12823 Ent : Entity_Id;
12824 F1 : Entity_Id;
12825 F2 : Entity_Id;
12826
12827 begin
12828 if not Is_Overloaded (N) then
12829 if not Is_Entity_Name (N)
12830 or else Ekind (Entity (N)) /= E_Function
12831 or else Scope (Entity (N)) /= Scope (Typ)
12832 or else No (First_Formal (Entity (N)))
12833 or else Etype (First_Formal (Entity (N))) /= Typ
12834 then
12835 Error_Msg_N ("iterable primitive must be local function name "
12836 & "whose first formal is an iterable type", N);
a6ce7e76 12837 return;
b4f149c2
AC
12838 end if;
12839
12840 Ent := Entity (N);
12841 F1 := First_Formal (Ent);
12842 if Nam = Name_First then
12843
12844 -- First (Container) => Cursor
12845
12846 if Etype (Ent) /= Cursor then
12847 Error_Msg_N ("primitive for First must yield a curosr", N);
12848 end if;
12849
12850 elsif Nam = Name_Next then
12851
12852 -- Next (Container, Cursor) => Cursor
12853
12854 F2 := Next_Formal (F1);
12855
12856 if Etype (F2) /= Cursor
12857 or else Etype (Ent) /= Cursor
12858 or else Present (Next_Formal (F2))
12859 then
12860 Error_Msg_N ("no match for Next iterable primitive", N);
12861 end if;
12862
12863 elsif Nam = Name_Has_Element then
12864
12865 -- Has_Element (Container, Cursor) => Boolean
12866
12867 F2 := Next_Formal (F1);
12868 if Etype (F2) /= Cursor
12869 or else Etype (Ent) /= Standard_Boolean
12870 or else Present (Next_Formal (F2))
12871 then
12872 Error_Msg_N ("no match for Has_Element iterable primitive", N);
12873 end if;
12874
12875 elsif Nam = Name_Element then
ffdeb702
AC
12876 F2 := Next_Formal (F1);
12877
12878 if No (F2)
12879 or else Etype (F2) /= Cursor
12880 or else Present (Next_Formal (F2))
12881 then
12882 Error_Msg_N ("no match for Element iterable primitive", N);
12883 end if;
b4f149c2
AC
12884 null;
12885
12886 else
12887 raise Program_Error;
12888 end if;
12889
12890 else
12891 -- Overloaded case: find subprogram with proper signature.
12892 -- Caller will report error if no match is found.
12893
12894 declare
12895 I : Interp_Index;
12896 It : Interp;
12897
12898 begin
12899 Get_First_Interp (N, I, It);
12900 while Present (It.Typ) loop
12901 if Ekind (It.Nam) = E_Function
ffdeb702 12902 and then Scope (It.Nam) = Scope (Typ)
b4f149c2
AC
12903 and then Etype (First_Formal (It.Nam)) = Typ
12904 then
12905 F1 := First_Formal (It.Nam);
12906
12907 if Nam = Name_First then
12908 if Etype (It.Nam) = Cursor
12909 and then No (Next_Formal (F1))
12910 then
12911 Set_Entity (N, It.Nam);
12912 exit;
12913 end if;
12914
12915 elsif Nam = Name_Next then
12916 F2 := Next_Formal (F1);
12917
12918 if Present (F2)
12919 and then No (Next_Formal (F2))
12920 and then Etype (F2) = Cursor
12921 and then Etype (It.Nam) = Cursor
12922 then
12923 Set_Entity (N, It.Nam);
12924 exit;
12925 end if;
12926
12927 elsif Nam = Name_Has_Element then
12928 F2 := Next_Formal (F1);
12929
12930 if Present (F2)
12931 and then No (Next_Formal (F2))
12932 and then Etype (F2) = Cursor
12933 and then Etype (It.Nam) = Standard_Boolean
12934 then
12935 Set_Entity (N, It.Nam);
12936 F2 := Next_Formal (F1);
12937 exit;
12938 end if;
12939
12940 elsif Nam = Name_Element then
ffdeb702
AC
12941 F2 := Next_Formal (F1);
12942
b4f149c2
AC
12943 if Present (F2)
12944 and then No (Next_Formal (F2))
12945 and then Etype (F2) = Cursor
12946 then
12947 Set_Entity (N, It.Nam);
12948 exit;
12949 end if;
12950 end if;
12951 end if;
12952
12953 Get_Next_Interp (I, It);
12954 end loop;
12955 end;
12956 end if;
12957 end Resolve_Iterable_Operation;
12958
a3f2babd
AC
12959 ----------------
12960 -- Set_Biased --
12961 ----------------
12962
12963 procedure Set_Biased
12964 (E : Entity_Id;
12965 N : Node_Id;
12966 Msg : String;
12967 Biased : Boolean := True)
12968 is
12969 begin
12970 if Biased then
12971 Set_Has_Biased_Representation (E);
12972
12973 if Warn_On_Biased_Representation then
12974 Error_Msg_NE
dbfeb4fa 12975 ("?B?" & Msg & " forces biased representation for&", N, E);
a3f2babd
AC
12976 end if;
12977 end if;
12978 end Set_Biased;
12979
996ae0b0
RK
12980 --------------------
12981 -- Set_Enum_Esize --
12982 --------------------
12983
12984 procedure Set_Enum_Esize (T : Entity_Id) is
12985 Lo : Uint;
12986 Hi : Uint;
12987 Sz : Nat;
12988
12989 begin
12990 Init_Alignment (T);
12991
12992 -- Find the minimum standard size (8,16,32,64) that fits
12993
12994 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
12995 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
12996
12997 if Lo < 0 then
12998 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
07fc65c4 12999 Sz := Standard_Character_Size; -- May be > 8 on some targets
996ae0b0
RK
13000
13001 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
13002 Sz := 16;
13003
13004 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
13005 Sz := 32;
13006
13007 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
13008 Sz := 64;
13009 end if;
13010
13011 else
13012 if Hi < Uint_2**08 then
07fc65c4 13013 Sz := Standard_Character_Size; -- May be > 8 on some targets
996ae0b0
RK
13014
13015 elsif Hi < Uint_2**16 then
13016 Sz := 16;
13017
13018 elsif Hi < Uint_2**32 then
13019 Sz := 32;
13020
13021 else pragma Assert (Hi < Uint_2**63);
13022 Sz := 64;
13023 end if;
13024 end if;
13025
13026 -- That minimum is the proper size unless we have a foreign convention
13027 -- and the size required is 32 or less, in which case we bump the size
13028 -- up to 32. This is required for C and C++ and seems reasonable for
13029 -- all other foreign conventions.
13030
13031 if Has_Foreign_Convention (T)
13032 and then Esize (T) < Standard_Integer_Size
d0ef7921
AC
13033
13034 -- Don't do this if Short_Enums on target
13035
f27ad2b2 13036 and then not Target_Short_Enums
996ae0b0
RK
13037 then
13038 Init_Esize (T, Standard_Integer_Size);
996ae0b0
RK
13039 else
13040 Init_Esize (T, Sz);
13041 end if;
996ae0b0
RK
13042 end Set_Enum_Esize;
13043
77a40ec1
AC
13044 -----------------------------
13045 -- Uninstall_Discriminants --
13046 -----------------------------
13047
13048 procedure Uninstall_Discriminants (E : Entity_Id) is
13049 Disc : Entity_Id;
13050 Prev : Entity_Id;
13051 Outer : Entity_Id;
13052
13053 begin
13054 -- Discriminants have been made visible for type declarations and
13055 -- protected type declarations, not for subtype declarations.
13056
13057 if Nkind (Parent (E)) /= N_Subtype_Declaration then
13058 Disc := First_Discriminant (E);
13059 while Present (Disc) loop
13060 if Disc /= Current_Entity (Disc) then
13061 Prev := Current_Entity (Disc);
13062 while Present (Prev)
13063 and then Present (Homonym (Prev))
13064 and then Homonym (Prev) /= Disc
13065 loop
13066 Prev := Homonym (Prev);
13067 end loop;
13068 else
13069 Prev := Empty;
13070 end if;
13071
13072 Set_Is_Immediately_Visible (Disc, False);
13073
13074 Outer := Homonym (Disc);
13075 while Present (Outer) and then Scope (Outer) = E loop
13076 Outer := Homonym (Outer);
13077 end loop;
13078
13079 -- Reset homonym link of other entities, but do not modify link
8a0183fd 13080 -- between entities in current scope, so that the back end can
77a40ec1
AC
13081 -- have a proper count of local overloadings.
13082
13083 if No (Prev) then
13084 Set_Name_Entity_Id (Chars (Disc), Outer);
13085
13086 elsif Scope (Prev) /= Scope (Disc) then
13087 Set_Homonym (Prev, Outer);
13088 end if;
13089
13090 Next_Discriminant (Disc);
13091 end loop;
13092 end if;
13093 end Uninstall_Discriminants;
13094
13095 -------------------------------------------
13096 -- Uninstall_Discriminants_And_Pop_Scope --
13097 -------------------------------------------
13098
13099 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
13100 begin
13101 if Has_Discriminants (E) then
13102 Uninstall_Discriminants (E);
13103 Pop_Scope;
13104 end if;
13105 end Uninstall_Discriminants_And_Pop_Scope;
13106
2642f998
RD
13107 ------------------------------
13108 -- Validate_Address_Clauses --
13109 ------------------------------
13110
13111 procedure Validate_Address_Clauses is
36d3d5d3
EB
13112 function Offset_Value (Expr : Node_Id) return Uint;
13113 -- Given an Address attribute reference, return the value in bits of its
13114 -- offset from the first bit of the underlying entity, or 0 if it is not
13115 -- known at compile time.
13116
13117 ------------------
13118 -- Offset_Value --
13119 ------------------
13120
13121 function Offset_Value (Expr : Node_Id) return Uint is
13122 N : Node_Id := Prefix (Expr);
13123 Off : Uint;
13124 Val : Uint := Uint_0;
13125
13126 begin
13127 -- Climb the prefix chain and compute the cumulative offset
13128
13129 loop
13130 if Is_Entity_Name (N) then
13131 return Val;
13132
13133 elsif Nkind (N) = N_Selected_Component then
13134 Off := Component_Bit_Offset (Entity (Selector_Name (N)));
13135 if Off /= No_Uint and then Off >= Uint_0 then
13136 Val := Val + Off;
13137 N := Prefix (N);
13138 else
13139 return Uint_0;
13140 end if;
13141
13142 elsif Nkind (N) = N_Indexed_Component then
13143 Off := Indexed_Component_Bit_Offset (N);
13144 if Off /= No_Uint then
13145 Val := Val + Off;
13146 N := Prefix (N);
13147 else
13148 return Uint_0;
13149 end if;
13150
13151 else
13152 return Uint_0;
13153 end if;
13154 end loop;
13155 end Offset_Value;
13156
13157 -- Start of processing for Validate_Address_Clauses
13158
2642f998
RD
13159 begin
13160 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
13161 declare
13162 ACCR : Address_Clause_Check_Record
13163 renames Address_Clause_Checks.Table (J);
13164
f4cd2542
EB
13165 Expr : Node_Id;
13166
2642f998
RD
13167 X_Alignment : Uint;
13168 Y_Alignment : Uint;
13169
13170 X_Size : Uint;
13171 Y_Size : Uint;
13172
36d3d5d3
EB
13173 X_Offs : Uint;
13174
2642f998
RD
13175 begin
13176 -- Skip processing of this entry if warning already posted
13177
13178 if not Address_Warning_Posted (ACCR.N) then
f4cd2542 13179 Expr := Original_Node (Expression (ACCR.N));
2642f998 13180
f4cd2542 13181 -- Get alignments
2642f998 13182
f4cd2542
EB
13183 X_Alignment := Alignment (ACCR.X);
13184 Y_Alignment := Alignment (ACCR.Y);
2642f998 13185
36d3d5d3 13186 -- Similarly obtain sizes and offset
2642f998 13187
f4cd2542
EB
13188 X_Size := Esize (ACCR.X);
13189 Y_Size := Esize (ACCR.Y);
2642f998 13190
36d3d5d3
EB
13191 if ACCR.Off
13192 and then Nkind (Expr) = N_Attribute_Reference
13193 and then Attribute_Name (Expr) = Name_Address
13194 then
13195 X_Offs := Offset_Value (Expr);
13196 else
13197 X_Offs := Uint_0;
13198 end if;
13199
2642f998
RD
13200 -- Check for large object overlaying smaller one
13201
13202 if Y_Size > Uint_0
13203 and then X_Size > Uint_0
36d3d5d3 13204 and then X_Offs + X_Size > Y_Size
2642f998 13205 then
e9c12b91 13206 Error_Msg_NE ("??& overlays smaller object", ACCR.N, ACCR.X);
2642f998 13207 Error_Msg_N
dbfeb4fa 13208 ("\??program execution may be erroneous", ACCR.N);
e9c12b91 13209
2642f998 13210 Error_Msg_Uint_1 := X_Size;
e9c12b91
AC
13211 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.X);
13212
2642f998 13213 Error_Msg_Uint_1 := Y_Size;
e9c12b91 13214 Error_Msg_NE ("\??size of & is ^", ACCR.N, ACCR.Y);
2642f998 13215
73170f9e 13216 if Y_Size >= X_Size then
36d3d5d3 13217 Error_Msg_Uint_1 := X_Offs;
73170f9e 13218 Error_Msg_NE ("\??but offset of & is ^", ACCR.N, ACCR.X);
36d3d5d3
EB
13219 end if;
13220
f4cd2542 13221 -- Check for inadequate alignment, both of the base object
c944345b
RD
13222 -- and of the offset, if any. We only do this check if the
13223 -- run-time Alignment_Check is active. No point in warning
13224 -- if this check has been suppressed (or is suppressed by
13225 -- default in the non-strict alignment machine case).
2642f998 13226
f4cd2542
EB
13227 -- Note: we do not check the alignment if we gave a size
13228 -- warning, since it would likely be redundant.
2642f998 13229
c944345b
RD
13230 elsif not Alignment_Checks_Suppressed (ACCR.Y)
13231 and then Y_Alignment /= Uint_0
e9c12b91
AC
13232 and then
13233 (Y_Alignment < X_Alignment
13234 or else
13235 (ACCR.Off
13236 and then Nkind (Expr) = N_Attribute_Reference
13237 and then Attribute_Name (Expr) = Name_Address
13238 and then Has_Compatible_Alignment
13239 (ACCR.X, Prefix (Expr), True) /=
13240 Known_Compatible))
2642f998
RD
13241 then
13242 Error_Msg_NE
e9c12b91
AC
13243 ("??specified address for& may be inconsistent with "
13244 & "alignment", ACCR.N, ACCR.X);
2642f998 13245 Error_Msg_N
dbfeb4fa 13246 ("\??program execution may be erroneous (RM 13.3(27))",
2642f998 13247 ACCR.N);
e9c12b91 13248
2642f998 13249 Error_Msg_Uint_1 := X_Alignment;
e9c12b91
AC
13250 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.X);
13251
2642f998 13252 Error_Msg_Uint_1 := Y_Alignment;
e9c12b91
AC
13253 Error_Msg_NE ("\??alignment of & is ^", ACCR.N, ACCR.Y);
13254
f4cd2542
EB
13255 if Y_Alignment >= X_Alignment then
13256 Error_Msg_N
e9c12b91 13257 ("\??but offset is not multiple of alignment", ACCR.N);
f4cd2542 13258 end if;
2642f998
RD
13259 end if;
13260 end if;
13261 end;
13262 end loop;
13263 end Validate_Address_Clauses;
13264
105b5e65
AC
13265 ---------------------------
13266 -- Validate_Independence --
13267 ---------------------------
13268
13269 procedure Validate_Independence is
13270 SU : constant Uint := UI_From_Int (System_Storage_Unit);
13271 N : Node_Id;
13272 E : Entity_Id;
13273 IC : Boolean;
13274 Comp : Entity_Id;
13275 Addr : Node_Id;
13276 P : Node_Id;
13277
13278 procedure Check_Array_Type (Atyp : Entity_Id);
13279 -- Checks if the array type Atyp has independent components, and
13280 -- if not, outputs an appropriate set of error messages.
13281
13282 procedure No_Independence;
13283 -- Output message that independence cannot be guaranteed
13284
13285 function OK_Component (C : Entity_Id) return Boolean;
13286 -- Checks one component to see if it is independently accessible, and
13287 -- if so yields True, otherwise yields False if independent access
13288 -- cannot be guaranteed. This is a conservative routine, it only
13289 -- returns True if it knows for sure, it returns False if it knows
13290 -- there is a problem, or it cannot be sure there is no problem.
13291
13292 procedure Reason_Bad_Component (C : Entity_Id);
13293 -- Outputs continuation message if a reason can be determined for
13294 -- the component C being bad.
13295
13296 ----------------------
13297 -- Check_Array_Type --
13298 ----------------------
13299
13300 procedure Check_Array_Type (Atyp : Entity_Id) is
13301 Ctyp : constant Entity_Id := Component_Type (Atyp);
13302
13303 begin
13304 -- OK if no alignment clause, no pack, and no component size
13305
13306 if not Has_Component_Size_Clause (Atyp)
13307 and then not Has_Alignment_Clause (Atyp)
13308 and then not Is_Packed (Atyp)
13309 then
13310 return;
13311 end if;
13312
9d2a2071
AC
13313 -- Case of component size is greater than or equal to 64 and the
13314 -- alignment of the array is at least as large as the alignment
13315 -- of the component. We are definitely OK in this situation.
13316
13317 if Known_Component_Size (Atyp)
13318 and then Component_Size (Atyp) >= 64
13319 and then Known_Alignment (Atyp)
13320 and then Known_Alignment (Ctyp)
13321 and then Alignment (Atyp) >= Alignment (Ctyp)
13322 then
13323 return;
13324 end if;
13325
105b5e65
AC
13326 -- Check actual component size
13327
13328 if not Known_Component_Size (Atyp)
13329 or else not (Addressable (Component_Size (Atyp))
9d2a2071 13330 and then Component_Size (Atyp) < 64)
105b5e65
AC
13331 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
13332 then
13333 No_Independence;
13334
13335 -- Bad component size, check reason
13336
13337 if Has_Component_Size_Clause (Atyp) then
616547fa
AC
13338 P := Get_Attribute_Definition_Clause
13339 (Atyp, Attribute_Component_Size);
105b5e65
AC
13340
13341 if Present (P) then
13342 Error_Msg_Sloc := Sloc (P);
13343 Error_Msg_N ("\because of Component_Size clause#", N);
13344 return;
13345 end if;
13346 end if;
13347
13348 if Is_Packed (Atyp) then
13349 P := Get_Rep_Pragma (Atyp, Name_Pack);
13350
13351 if Present (P) then
13352 Error_Msg_Sloc := Sloc (P);
13353 Error_Msg_N ("\because of pragma Pack#", N);
13354 return;
13355 end if;
13356 end if;
13357
13358 -- No reason found, just return
13359
13360 return;
13361 end if;
13362
13363 -- Array type is OK independence-wise
13364
13365 return;
13366 end Check_Array_Type;
13367
13368 ---------------------
13369 -- No_Independence --
13370 ---------------------
13371
13372 procedure No_Independence is
13373 begin
13374 if Pragma_Name (N) = Name_Independent then
b69cd36a 13375 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
105b5e65
AC
13376 else
13377 Error_Msg_NE
13378 ("independent components cannot be guaranteed for&", N, E);
13379 end if;
13380 end No_Independence;
13381
13382 ------------------
13383 -- OK_Component --
13384 ------------------
13385
13386 function OK_Component (C : Entity_Id) return Boolean is
13387 Rec : constant Entity_Id := Scope (C);
13388 Ctyp : constant Entity_Id := Etype (C);
13389
13390 begin
13391 -- OK if no component clause, no Pack, and no alignment clause
13392
13393 if No (Component_Clause (C))
13394 and then not Is_Packed (Rec)
13395 and then not Has_Alignment_Clause (Rec)
13396 then
13397 return True;
13398 end if;
13399
13400 -- Here we look at the actual component layout. A component is
13401 -- addressable if its size is a multiple of the Esize of the
13402 -- component type, and its starting position in the record has
13403 -- appropriate alignment, and the record itself has appropriate
13404 -- alignment to guarantee the component alignment.
13405
13406 -- Make sure sizes are static, always assume the worst for any
13407 -- cases where we cannot check static values.
13408
13409 if not (Known_Static_Esize (C)
616547fa
AC
13410 and then
13411 Known_Static_Esize (Ctyp))
105b5e65
AC
13412 then
13413 return False;
13414 end if;
13415
13416 -- Size of component must be addressable or greater than 64 bits
13417 -- and a multiple of bytes.
13418
616547fa 13419 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
105b5e65
AC
13420 return False;
13421 end if;
13422
13423 -- Check size is proper multiple
13424
13425 if Esize (C) mod Esize (Ctyp) /= 0 then
13426 return False;
13427 end if;
13428
13429 -- Check alignment of component is OK
13430
13431 if not Known_Component_Bit_Offset (C)
13432 or else Component_Bit_Offset (C) < Uint_0
13433 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
13434 then
13435 return False;
13436 end if;
13437
13438 -- Check alignment of record type is OK
13439
13440 if not Known_Alignment (Rec)
13441 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13442 then
13443 return False;
13444 end if;
13445
13446 -- All tests passed, component is addressable
13447
13448 return True;
13449 end OK_Component;
13450
13451 --------------------------
13452 -- Reason_Bad_Component --
13453 --------------------------
13454
13455 procedure Reason_Bad_Component (C : Entity_Id) is
13456 Rec : constant Entity_Id := Scope (C);
13457 Ctyp : constant Entity_Id := Etype (C);
13458
13459 begin
13460 -- If component clause present assume that's the problem
13461
13462 if Present (Component_Clause (C)) then
13463 Error_Msg_Sloc := Sloc (Component_Clause (C));
13464 Error_Msg_N ("\because of Component_Clause#", N);
13465 return;
13466 end if;
13467
13468 -- If pragma Pack clause present, assume that's the problem
13469
13470 if Is_Packed (Rec) then
13471 P := Get_Rep_Pragma (Rec, Name_Pack);
13472
13473 if Present (P) then
13474 Error_Msg_Sloc := Sloc (P);
13475 Error_Msg_N ("\because of pragma Pack#", N);
13476 return;
13477 end if;
13478 end if;
13479
13480 -- See if record has bad alignment clause
13481
13482 if Has_Alignment_Clause (Rec)
13483 and then Known_Alignment (Rec)
13484 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
13485 then
13486 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
13487
13488 if Present (P) then
13489 Error_Msg_Sloc := Sloc (P);
13490 Error_Msg_N ("\because of Alignment clause#", N);
13491 end if;
13492 end if;
13493
13494 -- Couldn't find a reason, so return without a message
13495
13496 return;
13497 end Reason_Bad_Component;
13498
13499 -- Start of processing for Validate_Independence
13500
13501 begin
13502 for J in Independence_Checks.First .. Independence_Checks.Last loop
13503 N := Independence_Checks.Table (J).N;
13504 E := Independence_Checks.Table (J).E;
13505 IC := Pragma_Name (N) = Name_Independent_Components;
13506
13507 -- Deal with component case
13508
13509 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
13510 if not OK_Component (E) then
13511 No_Independence;
13512 Reason_Bad_Component (E);
13513 goto Continue;
13514 end if;
13515 end if;
13516
13517 -- Deal with record with Independent_Components
13518
13519 if IC and then Is_Record_Type (E) then
13520 Comp := First_Component_Or_Discriminant (E);
13521 while Present (Comp) loop
13522 if not OK_Component (Comp) then
13523 No_Independence;
13524 Reason_Bad_Component (Comp);
13525 goto Continue;
13526 end if;
13527
13528 Next_Component_Or_Discriminant (Comp);
13529 end loop;
13530 end if;
13531
13532 -- Deal with address clause case
13533
13534 if Is_Object (E) then
13535 Addr := Address_Clause (E);
13536
13537 if Present (Addr) then
13538 No_Independence;
13539 Error_Msg_Sloc := Sloc (Addr);
13540 Error_Msg_N ("\because of Address clause#", N);
13541 goto Continue;
13542 end if;
13543 end if;
13544
13545 -- Deal with independent components for array type
13546
13547 if IC and then Is_Array_Type (E) then
13548 Check_Array_Type (E);
13549 end if;
13550
13551 -- Deal with independent components for array object
13552
13553 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
13554 Check_Array_Type (Etype (E));
13555 end if;
13556
13557 <<Continue>> null;
13558 end loop;
13559 end Validate_Independence;
13560
dd2bf554
ES
13561 ------------------------------
13562 -- Validate_Iterable_Aspect --
13563 ------------------------------
13564
13565 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
b4f149c2
AC
13566 Assoc : Node_Id;
13567 Expr : Node_Id;
dd2bf554 13568
82d4f390 13569 Prim : Node_Id;
a6ce7e76 13570 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
dd2bf554
ES
13571
13572 First_Id : Entity_Id;
13573 Next_Id : Entity_Id;
13574 Has_Element_Id : Entity_Id;
13575 Element_Id : Entity_Id;
13576
dd2bf554 13577 begin
edbd98c4 13578 -- If previous error aspect is unusable
a6ce7e76
AC
13579
13580 if Cursor = Any_Type then
b4f149c2
AC
13581 return;
13582 end if;
dd2bf554
ES
13583
13584 First_Id := Empty;
13585 Next_Id := Empty;
13586 Has_Element_Id := Empty;
0f361206 13587 Element_Id := Empty;
dd2bf554
ES
13588
13589 -- Each expression must resolve to a function with the proper signature
13590
13591 Assoc := First (Component_Associations (Expression (ASN)));
13592 while Present (Assoc) loop
13593 Expr := Expression (Assoc);
13594 Analyze (Expr);
13595
dd2bf554 13596 Prim := First (Choices (Assoc));
82d4f390 13597
dc06dd83 13598 if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then
dd2bf554
ES
13599 Error_Msg_N ("illegal name in association", Prim);
13600
13601 elsif Chars (Prim) = Name_First then
b4f149c2 13602 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
dd2bf554 13603 First_Id := Entity (Expr);
dd2bf554
ES
13604
13605 elsif Chars (Prim) = Name_Next then
b4f149c2 13606 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
dd2bf554 13607 Next_Id := Entity (Expr);
dd2bf554
ES
13608
13609 elsif Chars (Prim) = Name_Has_Element then
b4f149c2 13610 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
dd2bf554 13611 Has_Element_Id := Entity (Expr);
82d4f390 13612
dd2bf554 13613 elsif Chars (Prim) = Name_Element then
b4f149c2 13614 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
dd2bf554 13615 Element_Id := Entity (Expr);
dd2bf554
ES
13616
13617 else
13618 Error_Msg_N ("invalid name for iterable function", Prim);
13619 end if;
13620
13621 Next (Assoc);
13622 end loop;
13623
13624 if No (First_Id) then
b4f149c2 13625 Error_Msg_N ("match for First primitive not found", ASN);
dd2bf554
ES
13626
13627 elsif No (Next_Id) then
b4f149c2 13628 Error_Msg_N ("match for Next primitive not found", ASN);
dd2bf554
ES
13629
13630 elsif No (Has_Element_Id) then
b4f149c2
AC
13631 Error_Msg_N ("match for Has_Element primitive not found", ASN);
13632
13633 elsif No (Element_Id) then
13634 null; -- Optional.
dd2bf554
ES
13635 end if;
13636 end Validate_Iterable_Aspect;
13637
996ae0b0
RK
13638 -----------------------------------
13639 -- Validate_Unchecked_Conversion --
13640 -----------------------------------
13641
13642 procedure Validate_Unchecked_Conversion
13643 (N : Node_Id;
13644 Act_Unit : Entity_Id)
13645 is
13646 Source : Entity_Id;
13647 Target : Entity_Id;
13648 Vnode : Node_Id;
13649
13650 begin
13651 -- Obtain source and target types. Note that we call Ancestor_Subtype
13652 -- here because the processing for generic instantiation always makes
13653 -- subtypes, and we want the original frozen actual types.
13654
13655 -- If we are dealing with private types, then do the check on their
13656 -- fully declared counterparts if the full declarations have been
a90bd866 13657 -- encountered (they don't have to be visible, but they must exist).
996ae0b0
RK
13658
13659 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
13660
13661 if Is_Private_Type (Source)
13662 and then Present (Underlying_Type (Source))
13663 then
13664 Source := Underlying_Type (Source);
13665 end if;
13666
13667 Target := Ancestor_Subtype (Etype (Act_Unit));
13668
0503c53a 13669 -- If either type is generic, the instantiation happens within a generic
a1092b48
AC
13670 -- unit, and there is nothing to check. The proper check will happen
13671 -- when the enclosing generic is instantiated.
996ae0b0
RK
13672
13673 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
13674 return;
13675 end if;
13676
13677 if Is_Private_Type (Target)
13678 and then Present (Underlying_Type (Target))
13679 then
13680 Target := Underlying_Type (Target);
13681 end if;
13682
5a99fda7
AC
13683 -- Source may be unconstrained array, but not target, except in relaxed
13684 -- semantics mode.
996ae0b0 13685
5a99fda7
AC
13686 if Is_Array_Type (Target)
13687 and then not Is_Constrained (Target)
13688 and then not Relaxed_RM_Semantics
13689 then
996ae0b0
RK
13690 Error_Msg_N
13691 ("unchecked conversion to unconstrained array not allowed", N);
13692 return;
13693 end if;
13694
c690a2ec
RD
13695 -- Warn if conversion between two different convention pointers
13696
13697 if Is_Access_Type (Target)
13698 and then Is_Access_Type (Source)
13699 and then Convention (Target) /= Convention (Source)
13700 and then Warn_On_Unchecked_Conversion
13701 then
ba0c6e47 13702 -- Give warnings for subprogram pointers only on most targets
0503c53a
RD
13703
13704 if Is_Access_Subprogram_Type (Target)
13705 or else Is_Access_Subprogram_Type (Source)
0503c53a
RD
13706 then
13707 Error_Msg_N
685bc70f 13708 ("?z?conversion between pointers with different conventions!",
dbfeb4fa 13709 N);
0503c53a 13710 end if;
c690a2ec
RD
13711 end if;
13712
800621e0
RD
13713 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
13714 -- warning when compiling GNAT-related sources.
13715
13716 if Warn_On_Unchecked_Conversion
13717 and then not In_Predefined_Unit (N)
13718 and then RTU_Loaded (Ada_Calendar)
dc06dd83
AC
13719 and then (Chars (Source) = Name_Time
13720 or else
13721 Chars (Target) = Name_Time)
800621e0
RD
13722 then
13723 -- If Ada.Calendar is loaded and the name of one of the operands is
13724 -- Time, there is a good chance that this is Ada.Calendar.Time.
13725
13726 declare
dc06dd83 13727 Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time));
800621e0
RD
13728 begin
13729 pragma Assert (Present (Calendar_Time));
13730
616547fa 13731 if Source = Calendar_Time or else Target = Calendar_Time then
800621e0 13732 Error_Msg_N
dc06dd83
AC
13733 ("?z?representation of 'Time values may change between "
13734 & "'G'N'A'T versions", N);
800621e0
RD
13735 end if;
13736 end;
13737 end if;
13738
0503c53a
RD
13739 -- Make entry in unchecked conversion table for later processing by
13740 -- Validate_Unchecked_Conversions, which will check sizes and alignments
8a0183fd 13741 -- (using values set by the back end where possible). This is only done
0503c53a 13742 -- if the appropriate warning is active.
996ae0b0 13743
fbf5a39b
AC
13744 if Warn_On_Unchecked_Conversion then
13745 Unchecked_Conversions.Append
4c51ff88
AC
13746 (New_Val => UC_Entry'(Eloc => Sloc (N),
13747 Source => Source,
13748 Target => Target,
13749 Act_Unit => Act_Unit));
fbf5a39b 13750
2cc2e964 13751 -- If both sizes are known statically now, then back-end annotation
fbf5a39b
AC
13752 -- is not required to do a proper check but if either size is not
13753 -- known statically, then we need the annotation.
13754
13755 if Known_Static_RM_Size (Source)
dbfeb4fa
RD
13756 and then
13757 Known_Static_RM_Size (Target)
fbf5a39b
AC
13758 then
13759 null;
13760 else
13761 Back_Annotate_Rep_Info := True;
13762 end if;
13763 end if;
996ae0b0 13764
0503c53a 13765 -- If unchecked conversion to access type, and access type is declared
a1092b48
AC
13766 -- in the same unit as the unchecked conversion, then set the flag
13767 -- No_Strict_Aliasing (no strict aliasing is implicit here)
e6f69614
AC
13768
13769 if Is_Access_Type (Target) and then
13770 In_Same_Source_Unit (Target, N)
13771 then
13772 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
13773 end if;
8a6a52dc 13774
a1092b48
AC
13775 -- Generate N_Validate_Unchecked_Conversion node for back end in case
13776 -- the back end needs to perform special validation checks.
8a6a52dc 13777
a1092b48
AC
13778 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
13779 -- have full expansion and the back end is called ???
8a6a52dc
AC
13780
13781 Vnode :=
13782 Make_Validate_Unchecked_Conversion (Sloc (N));
13783 Set_Source_Type (Vnode, Source);
13784 Set_Target_Type (Vnode, Target);
13785
0503c53a
RD
13786 -- If the unchecked conversion node is in a list, just insert before it.
13787 -- If not we have some strange case, not worth bothering about.
8a6a52dc
AC
13788
13789 if Is_List_Member (N) then
996ae0b0
RK
13790 Insert_After (N, Vnode);
13791 end if;
13792 end Validate_Unchecked_Conversion;
13793
13794 ------------------------------------
13795 -- Validate_Unchecked_Conversions --
13796 ------------------------------------
13797
13798 procedure Validate_Unchecked_Conversions is
13799 begin
13800 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
13801 declare
13802 T : UC_Entry renames Unchecked_Conversions.Table (N);
13803
2f8d7dfe 13804 Act_Unit : constant Entity_Id := T.Act_Unit;
4c51ff88
AC
13805 Eloc : constant Source_Ptr := T.Eloc;
13806 Source : constant Entity_Id := T.Source;
13807 Target : constant Entity_Id := T.Target;
996ae0b0 13808
54c04d6c
AC
13809 Source_Siz : Uint;
13810 Target_Siz : Uint;
996ae0b0
RK
13811
13812 begin
4c51ff88
AC
13813 -- Skip if function marked as warnings off
13814
13815 if Warnings_Off (Act_Unit) then
13816 goto Continue;
13817 end if;
13818
0503c53a
RD
13819 -- This validation check, which warns if we have unequal sizes for
13820 -- unchecked conversion, and thus potentially implementation
996ae0b0 13821 -- dependent semantics, is one of the few occasions on which we
0503c53a
RD
13822 -- use the official RM size instead of Esize. See description in
13823 -- Einfo "Handling of Type'Size Values" for details.
996ae0b0 13824
07fc65c4 13825 if Serious_Errors_Detected = 0
996ae0b0
RK
13826 and then Known_Static_RM_Size (Source)
13827 and then Known_Static_RM_Size (Target)
9fcf2a0b
AC
13828
13829 -- Don't do the check if warnings off for either type, note the
13830 -- deliberate use of OR here instead of OR ELSE to get the flag
13831 -- Warnings_Off_Used set for both types if appropriate.
13832
13833 and then not (Has_Warnings_Off (Source)
13834 or
13835 Has_Warnings_Off (Target))
996ae0b0
RK
13836 then
13837 Source_Siz := RM_Size (Source);
13838 Target_Siz := RM_Size (Target);
13839
13840 if Source_Siz /= Target_Siz then
f66d46ec 13841 Error_Msg
685bc70f 13842 ("?z?types for unchecked conversion have different sizes!",
f66d46ec 13843 Eloc);
996ae0b0
RK
13844
13845 if All_Errors_Mode then
13846 Error_Msg_Name_1 := Chars (Source);
13847 Error_Msg_Uint_1 := Source_Siz;
13848 Error_Msg_Name_2 := Chars (Target);
13849 Error_Msg_Uint_2 := Target_Siz;
685bc70f 13850 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
996ae0b0
RK
13851
13852 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
13853
13854 if Is_Discrete_Type (Source)
616547fa
AC
13855 and then
13856 Is_Discrete_Type (Target)
996ae0b0
RK
13857 then
13858 if Source_Siz > Target_Siz then
f66d46ec 13859 Error_Msg
685bc70f 13860 ("\?z?^ high order bits of source will "
dbfeb4fa 13861 & "be ignored!", Eloc);
996ae0b0 13862
fbf5a39b 13863 elsif Is_Unsigned_Type (Source) then
f66d46ec 13864 Error_Msg
685bc70f 13865 ("\?z?source will be extended with ^ high order "
b785e0b8 13866 & "zero bits!", Eloc);
996ae0b0
RK
13867
13868 else
f66d46ec 13869 Error_Msg
685bc70f 13870 ("\?z?source will be extended with ^ high order "
dbfeb4fa 13871 & "sign bits!", Eloc);
996ae0b0
RK
13872 end if;
13873
13874 elsif Source_Siz < Target_Siz then
13875 if Is_Discrete_Type (Target) then
13876 if Bytes_Big_Endian then
f66d46ec 13877 Error_Msg
685bc70f 13878 ("\?z?target value will include ^ undefined "
dbfeb4fa 13879 & "low order bits!", Eloc);
996ae0b0 13880 else
f66d46ec 13881 Error_Msg
685bc70f 13882 ("\?z?target value will include ^ undefined "
dbfeb4fa 13883 & "high order bits!", Eloc);
996ae0b0
RK
13884 end if;
13885
13886 else
f66d46ec 13887 Error_Msg
685bc70f 13888 ("\?z?^ trailing bits of target value will be "
dbfeb4fa 13889 & "undefined!", Eloc);
996ae0b0
RK
13890 end if;
13891
13892 else pragma Assert (Source_Siz > Target_Siz);
ccd6f414
AC
13893 if Is_Discrete_Type (Source) then
13894 if Bytes_Big_Endian then
13895 Error_Msg
13896 ("\?z?^ low order bits of source will be "
13897 & "ignored!", Eloc);
13898 else
13899 Error_Msg
13900 ("\?z?^ high order bits of source will be "
13901 & "ignored!", Eloc);
13902 end if;
13903
13904 else
13905 Error_Msg
13906 ("\?z?^ trailing bits of source will be "
13907 & "ignored!", Eloc);
13908 end if;
996ae0b0
RK
13909 end if;
13910 end if;
996ae0b0
RK
13911 end if;
13912 end if;
13913
13914 -- If both types are access types, we need to check the alignment.
13915 -- If the alignment of both is specified, we can do it here.
13916
07fc65c4 13917 if Serious_Errors_Detected = 0
3f1bc2cf
AC
13918 and then Is_Access_Type (Source)
13919 and then Is_Access_Type (Target)
996ae0b0
RK
13920 and then Target_Strict_Alignment
13921 and then Present (Designated_Type (Source))
13922 and then Present (Designated_Type (Target))
13923 then
13924 declare
13925 D_Source : constant Entity_Id := Designated_Type (Source);
13926 D_Target : constant Entity_Id := Designated_Type (Target);
13927
13928 begin
13929 if Known_Alignment (D_Source)
616547fa
AC
13930 and then
13931 Known_Alignment (D_Target)
996ae0b0
RK
13932 then
13933 declare
13934 Source_Align : constant Uint := Alignment (D_Source);
13935 Target_Align : constant Uint := Alignment (D_Target);
13936
13937 begin
13938 if Source_Align < Target_Align
13939 and then not Is_Tagged_Type (D_Source)
9fcf2a0b
AC
13940
13941 -- Suppress warning if warnings suppressed on either
13942 -- type or either designated type. Note the use of
13943 -- OR here instead of OR ELSE. That is intentional,
13944 -- we would like to set flag Warnings_Off_Used in
13945 -- all types for which warnings are suppressed.
13946
13947 and then not (Has_Warnings_Off (D_Source)
13948 or
13949 Has_Warnings_Off (D_Target)
13950 or
13951 Has_Warnings_Off (Source)
13952 or
13953 Has_Warnings_Off (Target))
996ae0b0 13954 then
996ae0b0
RK
13955 Error_Msg_Uint_1 := Target_Align;
13956 Error_Msg_Uint_2 := Source_Align;
f66d46ec 13957 Error_Msg_Node_1 := D_Target;
996ae0b0 13958 Error_Msg_Node_2 := D_Source;
f66d46ec 13959 Error_Msg
685bc70f 13960 ("?z?alignment of & (^) is stricter than "
dbfeb4fa 13961 & "alignment of & (^)!", Eloc);
9fcf2a0b 13962 Error_Msg
685bc70f 13963 ("\?z?resulting access value may have invalid "
dbfeb4fa 13964 & "alignment!", Eloc);
996ae0b0
RK
13965 end if;
13966 end;
13967 end if;
13968 end;
13969 end if;
13970 end;
4c51ff88
AC
13971
13972 <<Continue>>
13973 null;
996ae0b0
RK
13974 end loop;
13975 end Validate_Unchecked_Conversions;
13976
996ae0b0 13977end Sem_Ch13;