]>
Commit | Line | Data |
---|---|---|
d6f39728 | 1 | ------------------------------------------------------------------------------ |
7189d17f | 2 | -- -- |
d6f39728 | 3 | -- GNAT COMPILER COMPONENTS -- |
4 | -- -- | |
5 | -- S E M _ C H 1 3 -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
503f7fd3 | 9 | -- Copyright (C) 1992-2010, Free Software Foundation, Inc. -- |
d6f39728 | 10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
80df182a | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
d6f39728 | 14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
80df182a | 18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
d6f39728 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
d6f39728 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
ae888dbd | 26 | with Aspects; use Aspects; |
d6f39728 | 27 | with Atree; use Atree; |
713c00d6 | 28 | with Checks; use Checks; |
d6f39728 | 29 | with Einfo; use Einfo; |
d00681a7 | 30 | with Elists; use Elists; |
d6f39728 | 31 | with Errout; use Errout; |
d00681a7 | 32 | with Exp_Disp; use Exp_Disp; |
d6f39728 | 33 | with Exp_Tss; use Exp_Tss; |
34 | with Exp_Util; use Exp_Util; | |
d6f39728 | 35 | with Lib; use Lib; |
83f8f0a6 | 36 | with Lib.Xref; use Lib.Xref; |
15ebb600 | 37 | with Namet; use Namet; |
d6f39728 | 38 | with Nlists; use Nlists; |
39 | with Nmake; use Nmake; | |
40 | with Opt; use Opt; | |
e0521a36 | 41 | with Restrict; use Restrict; |
42 | with Rident; use Rident; | |
d6f39728 | 43 | with Rtsfind; use Rtsfind; |
44 | with Sem; use Sem; | |
d60c9ff7 | 45 | with Sem_Aux; use Sem_Aux; |
40ca69b9 | 46 | with Sem_Ch3; use Sem_Ch3; |
d6f39728 | 47 | with Sem_Ch8; use Sem_Ch8; |
48 | with Sem_Eval; use Sem_Eval; | |
49 | with Sem_Res; use Sem_Res; | |
50 | with Sem_Type; use Sem_Type; | |
51 | with Sem_Util; use Sem_Util; | |
44e4341e | 52 | with Sem_Warn; use Sem_Warn; |
1e3c4ae6 | 53 | with Sinput; use Sinput; |
9dfe12ae | 54 | with Snames; use Snames; |
d6f39728 | 55 | with Stand; use Stand; |
56 | with Sinfo; use Sinfo; | |
5b5df4a9 | 57 | with Stringt; use Stringt; |
93735cb8 | 58 | with Targparm; use Targparm; |
d6f39728 | 59 | with Ttypes; use Ttypes; |
60 | with Tbuild; use Tbuild; | |
61 | with Urealp; use Urealp; | |
62 | ||
bfa5a9d9 | 63 | with GNAT.Heap_Sort_G; |
d6f39728 | 64 | |
65 | package body Sem_Ch13 is | |
66 | ||
67 | SSU : constant Pos := System_Storage_Unit; | |
68 | -- Convenient short hand for commonly used constant | |
69 | ||
70 | ----------------------- | |
71 | -- Local Subprograms -- | |
72 | ----------------------- | |
73 | ||
74 | procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id); | |
75 | -- This routine is called after setting the Esize of type entity Typ. | |
1a34e48c | 76 | -- The purpose is to deal with the situation where an alignment has been |
d6f39728 | 77 | -- inherited from a derived type that is no longer appropriate for the |
78 | -- new Esize value. In this case, we reset the Alignment to unknown. | |
79 | ||
9dc88aea | 80 | procedure Build_Predicate_Function |
81 | (Typ : Entity_Id; | |
82 | FDecl : out Node_Id; | |
83 | FBody : out Node_Id); | |
84 | -- If Typ has predicates (indicated by Has_Predicates being set for Typ, | |
85 | -- then either there are pragma Invariant entries on the rep chain for the | |
86 | -- type (note that Predicate aspects are converted to pragam Predicate), or | |
87 | -- there are inherited aspects from a parent type, or ancestor subtypes, | |
88 | -- or interfaces. This procedure builds the spec and body for the Predicate | |
89 | -- function that tests these predicates, returning them in PDecl and Pbody | |
90 | -- and setting Predicate_Procedure for Typ. In some error situations no | |
91 | -- procedure is built, in which case PDecl/PBody are empty on return. | |
92 | ||
d97beb2f | 93 | procedure Build_Static_Predicate |
94 | (Typ : Entity_Id; | |
95 | Expr : Node_Id; | |
96 | Nam : Name_Id); | |
97 | -- Given a predicated type Typ, whose predicate expression is Expr, tests | |
98 | -- if Expr is a static predicate, and if so, builds the predicate range | |
99 | -- list. Nam is the name of the argument to the predicate function. | |
100 | -- Occurrences of the type name in the predicate expression have been | |
101 | -- replaced by identifer references to this name, which is unique, so any | |
102 | -- identifier with Chars matching Nam must be a reference to the type. If | |
103 | -- the predicate is non-static, this procedure returns doing nothing. If | |
104 | -- the predicate is static, then the corresponding predicate list is stored | |
105 | -- in Static_Predicate (Typ), and the Expr is rewritten as a canonicalized | |
106 | -- membership operation. | |
107 | ||
d6f39728 | 108 | function Get_Alignment_Value (Expr : Node_Id) return Uint; |
109 | -- Given the expression for an alignment value, returns the corresponding | |
110 | -- Uint value. If the value is inappropriate, then error messages are | |
111 | -- posted as required, and a value of No_Uint is returned. | |
112 | ||
113 | function Is_Operational_Item (N : Node_Id) return Boolean; | |
1e3c4ae6 | 114 | -- A specification for a stream attribute is allowed before the full type |
115 | -- is declared, as explained in AI-00137 and the corrigendum. Attributes | |
116 | -- that do not specify a representation characteristic are operational | |
117 | -- attributes. | |
d6f39728 | 118 | |
44e4341e | 119 | procedure New_Stream_Subprogram |
d6f39728 | 120 | (N : Node_Id; |
121 | Ent : Entity_Id; | |
122 | Subp : Entity_Id; | |
9dfe12ae | 123 | Nam : TSS_Name_Type); |
44e4341e | 124 | -- Create a subprogram renaming of a given stream attribute to the |
125 | -- designated subprogram and then in the tagged case, provide this as a | |
126 | -- primitive operation, or in the non-tagged case make an appropriate TSS | |
127 | -- entry. This is more properly an expansion activity than just semantics, | |
128 | -- but the presence of user-defined stream functions for limited types is a | |
129 | -- legality check, which is why this takes place here rather than in | |
130 | -- exp_ch13, where it was previously. Nam indicates the name of the TSS | |
131 | -- function to be generated. | |
9dfe12ae | 132 | -- |
f15731c4 | 133 | -- To avoid elaboration anomalies with freeze nodes, for untagged types |
134 | -- we generate both a subprogram declaration and a subprogram renaming | |
135 | -- declaration, so that the attribute specification is handled as a | |
136 | -- renaming_as_body. For tagged types, the specification is one of the | |
137 | -- primitive specs. | |
138 | ||
b77e4501 | 139 | procedure Set_Biased |
140 | (E : Entity_Id; | |
141 | N : Node_Id; | |
142 | Msg : String; | |
143 | Biased : Boolean := True); | |
144 | -- If Biased is True, sets Has_Biased_Representation flag for E, and | |
145 | -- outputs a warning message at node N if Warn_On_Biased_Representation is | |
146 | -- is True. This warning inserts the string Msg to describe the construct | |
147 | -- causing biasing. | |
148 | ||
d6f39728 | 149 | ---------------------------------------------- |
150 | -- Table for Validate_Unchecked_Conversions -- | |
151 | ---------------------------------------------- | |
152 | ||
153 | -- The following table collects unchecked conversions for validation. | |
154 | -- Entries are made by Validate_Unchecked_Conversion and then the | |
155 | -- call to Validate_Unchecked_Conversions does the actual error | |
156 | -- checking and posting of warnings. The reason for this delayed | |
157 | -- processing is to take advantage of back-annotations of size and | |
1a34e48c | 158 | -- alignment values performed by the back end. |
d6f39728 | 159 | |
299480f9 | 160 | -- Note: the reason we store a Source_Ptr value instead of a Node_Id |
161 | -- is that by the time Validate_Unchecked_Conversions is called, Sprint | |
162 | -- will already have modified all Sloc values if the -gnatD option is set. | |
163 | ||
d6f39728 | 164 | type UC_Entry is record |
299480f9 | 165 | Eloc : Source_Ptr; -- node used for posting warnings |
166 | Source : Entity_Id; -- source type for unchecked conversion | |
167 | Target : Entity_Id; -- target type for unchecked conversion | |
d6f39728 | 168 | end record; |
169 | ||
170 | package Unchecked_Conversions is new Table.Table ( | |
171 | Table_Component_Type => UC_Entry, | |
172 | Table_Index_Type => Int, | |
173 | Table_Low_Bound => 1, | |
174 | Table_Initial => 50, | |
175 | Table_Increment => 200, | |
176 | Table_Name => "Unchecked_Conversions"); | |
177 | ||
83f8f0a6 | 178 | ---------------------------------------- |
179 | -- Table for Validate_Address_Clauses -- | |
180 | ---------------------------------------- | |
181 | ||
182 | -- If an address clause has the form | |
183 | ||
184 | -- for X'Address use Expr | |
185 | ||
186 | -- where Expr is of the form Y'Address or recursively is a reference | |
187 | -- to a constant of either of these forms, and X and Y are entities of | |
188 | -- objects, then if Y has a smaller alignment than X, that merits a | |
189 | -- warning about possible bad alignment. The following table collects | |
190 | -- address clauses of this kind. We put these in a table so that they | |
191 | -- can be checked after the back end has completed annotation of the | |
192 | -- alignments of objects, since we can catch more cases that way. | |
193 | ||
194 | type Address_Clause_Check_Record is record | |
195 | N : Node_Id; | |
196 | -- The address clause | |
197 | ||
198 | X : Entity_Id; | |
199 | -- The entity of the object overlaying Y | |
200 | ||
201 | Y : Entity_Id; | |
202 | -- The entity of the object being overlaid | |
d6da7448 | 203 | |
204 | Off : Boolean; | |
205 | -- Whether the address is offseted within Y | |
83f8f0a6 | 206 | end record; |
207 | ||
208 | package Address_Clause_Checks is new Table.Table ( | |
209 | Table_Component_Type => Address_Clause_Check_Record, | |
210 | Table_Index_Type => Int, | |
211 | Table_Low_Bound => 1, | |
212 | Table_Initial => 20, | |
213 | Table_Increment => 200, | |
214 | Table_Name => "Address_Clause_Checks"); | |
215 | ||
59ac57b5 | 216 | ----------------------------------------- |
217 | -- Adjust_Record_For_Reverse_Bit_Order -- | |
218 | ----------------------------------------- | |
219 | ||
220 | procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is | |
67278d60 | 221 | Comp : Node_Id; |
222 | CC : Node_Id; | |
59ac57b5 | 223 | |
224 | begin | |
67278d60 | 225 | -- Processing depends on version of Ada |
59ac57b5 | 226 | |
6797073f | 227 | -- For Ada 95, we just renumber bits within a storage unit. We do the |
228 | -- same for Ada 83 mode, since we recognize pragma Bit_Order in Ada 83, | |
229 | -- and are free to add this extension. | |
230 | ||
231 | if Ada_Version < Ada_2005 then | |
232 | Comp := First_Component_Or_Discriminant (R); | |
233 | while Present (Comp) loop | |
234 | CC := Component_Clause (Comp); | |
235 | ||
236 | -- If component clause is present, then deal with the non-default | |
237 | -- bit order case for Ada 95 mode. | |
238 | ||
239 | -- We only do this processing for the base type, and in fact that | |
240 | -- is important, since otherwise if there are record subtypes, we | |
241 | -- could reverse the bits once for each subtype, which is wrong. | |
242 | ||
243 | if Present (CC) | |
244 | and then Ekind (R) = E_Record_Type | |
245 | then | |
246 | declare | |
247 | CFB : constant Uint := Component_Bit_Offset (Comp); | |
248 | CSZ : constant Uint := Esize (Comp); | |
249 | CLC : constant Node_Id := Component_Clause (Comp); | |
250 | Pos : constant Node_Id := Position (CLC); | |
251 | FB : constant Node_Id := First_Bit (CLC); | |
252 | ||
253 | Storage_Unit_Offset : constant Uint := | |
254 | CFB / System_Storage_Unit; | |
255 | ||
256 | Start_Bit : constant Uint := | |
257 | CFB mod System_Storage_Unit; | |
59ac57b5 | 258 | |
6797073f | 259 | begin |
260 | -- Cases where field goes over storage unit boundary | |
59ac57b5 | 261 | |
6797073f | 262 | if Start_Bit + CSZ > System_Storage_Unit then |
59ac57b5 | 263 | |
6797073f | 264 | -- Allow multi-byte field but generate warning |
59ac57b5 | 265 | |
6797073f | 266 | if Start_Bit mod System_Storage_Unit = 0 |
267 | and then CSZ mod System_Storage_Unit = 0 | |
268 | then | |
269 | Error_Msg_N | |
270 | ("multi-byte field specified with non-standard" | |
271 | & " Bit_Order?", CLC); | |
31486bc0 | 272 | |
6797073f | 273 | if Bytes_Big_Endian then |
31486bc0 | 274 | Error_Msg_N |
6797073f | 275 | ("bytes are not reversed " |
276 | & "(component is big-endian)?", CLC); | |
31486bc0 | 277 | else |
278 | Error_Msg_N | |
6797073f | 279 | ("bytes are not reversed " |
280 | & "(component is little-endian)?", CLC); | |
31486bc0 | 281 | end if; |
59ac57b5 | 282 | |
6797073f | 283 | -- Do not allow non-contiguous field |
59ac57b5 | 284 | |
67278d60 | 285 | else |
6797073f | 286 | Error_Msg_N |
287 | ("attempt to specify non-contiguous field " | |
288 | & "not permitted", CLC); | |
289 | Error_Msg_N | |
290 | ("\caused by non-standard Bit_Order " | |
291 | & "specified", CLC); | |
292 | Error_Msg_N | |
293 | ("\consider possibility of using " | |
294 | & "Ada 2005 mode here", CLC); | |
295 | end if; | |
59ac57b5 | 296 | |
6797073f | 297 | -- Case where field fits in one storage unit |
59ac57b5 | 298 | |
6797073f | 299 | else |
300 | -- Give warning if suspicious component clause | |
59ac57b5 | 301 | |
6797073f | 302 | if Intval (FB) >= System_Storage_Unit |
303 | and then Warn_On_Reverse_Bit_Order | |
304 | then | |
305 | Error_Msg_N | |
306 | ("?Bit_Order clause does not affect " & | |
307 | "byte ordering", Pos); | |
308 | Error_Msg_Uint_1 := | |
309 | Intval (Pos) + Intval (FB) / | |
310 | System_Storage_Unit; | |
311 | Error_Msg_N | |
312 | ("?position normalized to ^ before bit " & | |
313 | "order interpreted", Pos); | |
314 | end if; | |
59ac57b5 | 315 | |
6797073f | 316 | -- Here is where we fix up the Component_Bit_Offset value |
317 | -- to account for the reverse bit order. Some examples of | |
318 | -- what needs to be done are: | |
bfa5a9d9 | 319 | |
6797073f | 320 | -- First_Bit .. Last_Bit Component_Bit_Offset |
321 | -- old new old new | |
59ac57b5 | 322 | |
6797073f | 323 | -- 0 .. 0 7 .. 7 0 7 |
324 | -- 0 .. 1 6 .. 7 0 6 | |
325 | -- 0 .. 2 5 .. 7 0 5 | |
326 | -- 0 .. 7 0 .. 7 0 4 | |
59ac57b5 | 327 | |
6797073f | 328 | -- 1 .. 1 6 .. 6 1 6 |
329 | -- 1 .. 4 3 .. 6 1 3 | |
330 | -- 4 .. 7 0 .. 3 4 0 | |
59ac57b5 | 331 | |
6797073f | 332 | -- The rule is that the first bit is is obtained by |
333 | -- subtracting the old ending bit from storage_unit - 1. | |
59ac57b5 | 334 | |
6797073f | 335 | Set_Component_Bit_Offset |
336 | (Comp, | |
337 | (Storage_Unit_Offset * System_Storage_Unit) + | |
338 | (System_Storage_Unit - 1) - | |
339 | (Start_Bit + CSZ - 1)); | |
59ac57b5 | 340 | |
6797073f | 341 | Set_Normalized_First_Bit |
342 | (Comp, | |
343 | Component_Bit_Offset (Comp) mod | |
344 | System_Storage_Unit); | |
345 | end if; | |
346 | end; | |
347 | end if; | |
348 | ||
349 | Next_Component_Or_Discriminant (Comp); | |
350 | end loop; | |
351 | ||
352 | -- For Ada 2005, we do machine scalar processing, as fully described In | |
353 | -- AI-133. This involves gathering all components which start at the | |
354 | -- same byte offset and processing them together. Same approach is still | |
355 | -- valid in later versions including Ada 2012. | |
356 | ||
357 | else | |
358 | declare | |
359 | Max_Machine_Scalar_Size : constant Uint := | |
360 | UI_From_Int | |
361 | (Standard_Long_Long_Integer_Size); | |
67278d60 | 362 | -- We use this as the maximum machine scalar size |
59ac57b5 | 363 | |
6797073f | 364 | Num_CC : Natural; |
365 | SSU : constant Uint := UI_From_Int (System_Storage_Unit); | |
59ac57b5 | 366 | |
6797073f | 367 | begin |
368 | -- This first loop through components does two things. First it | |
369 | -- deals with the case of components with component clauses whose | |
370 | -- length is greater than the maximum machine scalar size (either | |
371 | -- accepting them or rejecting as needed). Second, it counts the | |
372 | -- number of components with component clauses whose length does | |
373 | -- not exceed this maximum for later processing. | |
67278d60 | 374 | |
6797073f | 375 | Num_CC := 0; |
376 | Comp := First_Component_Or_Discriminant (R); | |
377 | while Present (Comp) loop | |
378 | CC := Component_Clause (Comp); | |
67278d60 | 379 | |
6797073f | 380 | if Present (CC) then |
381 | declare | |
382 | Fbit : constant Uint := | |
383 | Static_Integer (First_Bit (CC)); | |
67278d60 | 384 | |
6797073f | 385 | begin |
386 | -- Case of component with size > max machine scalar | |
67278d60 | 387 | |
6797073f | 388 | if Esize (Comp) > Max_Machine_Scalar_Size then |
67278d60 | 389 | |
6797073f | 390 | -- Must begin on byte boundary |
67278d60 | 391 | |
6797073f | 392 | if Fbit mod SSU /= 0 then |
393 | Error_Msg_N | |
394 | ("illegal first bit value for " | |
395 | & "reverse bit order", | |
396 | First_Bit (CC)); | |
397 | Error_Msg_Uint_1 := SSU; | |
398 | Error_Msg_Uint_2 := Max_Machine_Scalar_Size; | |
67278d60 | 399 | |
6797073f | 400 | Error_Msg_N |
401 | ("\must be a multiple of ^ " | |
402 | & "if size greater than ^", | |
403 | First_Bit (CC)); | |
67278d60 | 404 | |
6797073f | 405 | -- Must end on byte boundary |
67278d60 | 406 | |
6797073f | 407 | elsif Esize (Comp) mod SSU /= 0 then |
408 | Error_Msg_N | |
409 | ("illegal last bit value for " | |
410 | & "reverse bit order", | |
411 | Last_Bit (CC)); | |
412 | Error_Msg_Uint_1 := SSU; | |
413 | Error_Msg_Uint_2 := Max_Machine_Scalar_Size; | |
67278d60 | 414 | |
6797073f | 415 | Error_Msg_N |
416 | ("\must be a multiple of ^ if size " | |
417 | & "greater than ^", | |
418 | Last_Bit (CC)); | |
67278d60 | 419 | |
6797073f | 420 | -- OK, give warning if enabled |
67278d60 | 421 | |
6797073f | 422 | elsif Warn_On_Reverse_Bit_Order then |
423 | Error_Msg_N | |
424 | ("multi-byte field specified with " | |
425 | & " non-standard Bit_Order?", CC); | |
426 | ||
427 | if Bytes_Big_Endian then | |
428 | Error_Msg_N | |
429 | ("\bytes are not reversed " | |
430 | & "(component is big-endian)?", CC); | |
431 | else | |
67278d60 | 432 | Error_Msg_N |
6797073f | 433 | ("\bytes are not reversed " |
434 | & "(component is little-endian)?", CC); | |
67278d60 | 435 | end if; |
6797073f | 436 | end if; |
59ac57b5 | 437 | |
6797073f | 438 | -- Case where size is not greater than max machine |
439 | -- scalar. For now, we just count these. | |
59ac57b5 | 440 | |
6797073f | 441 | else |
442 | Num_CC := Num_CC + 1; | |
443 | end if; | |
444 | end; | |
445 | end if; | |
59ac57b5 | 446 | |
6797073f | 447 | Next_Component_Or_Discriminant (Comp); |
448 | end loop; | |
59ac57b5 | 449 | |
6797073f | 450 | -- We need to sort the component clauses on the basis of the |
451 | -- Position values in the clause, so we can group clauses with | |
452 | -- the same Position. together to determine the relevant machine | |
453 | -- scalar size. | |
59ac57b5 | 454 | |
6797073f | 455 | Sort_CC : declare |
456 | Comps : array (0 .. Num_CC) of Entity_Id; | |
457 | -- Array to collect component and discriminant entities. The | |
458 | -- data starts at index 1, the 0'th entry is for the sort | |
459 | -- routine. | |
59ac57b5 | 460 | |
6797073f | 461 | function CP_Lt (Op1, Op2 : Natural) return Boolean; |
462 | -- Compare routine for Sort | |
59ac57b5 | 463 | |
6797073f | 464 | procedure CP_Move (From : Natural; To : Natural); |
465 | -- Move routine for Sort | |
59ac57b5 | 466 | |
6797073f | 467 | package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt); |
59ac57b5 | 468 | |
6797073f | 469 | Start : Natural; |
470 | Stop : Natural; | |
471 | -- Start and stop positions in the component list of the set of | |
472 | -- components with the same starting position (that constitute | |
473 | -- components in a single machine scalar). | |
59ac57b5 | 474 | |
6797073f | 475 | MaxL : Uint; |
476 | -- Maximum last bit value of any component in this set | |
59ac57b5 | 477 | |
6797073f | 478 | MSS : Uint; |
479 | -- Corresponding machine scalar size | |
67278d60 | 480 | |
6797073f | 481 | ----------- |
482 | -- CP_Lt -- | |
483 | ----------- | |
67278d60 | 484 | |
6797073f | 485 | function CP_Lt (Op1, Op2 : Natural) return Boolean is |
486 | begin | |
487 | return Position (Component_Clause (Comps (Op1))) < | |
488 | Position (Component_Clause (Comps (Op2))); | |
489 | end CP_Lt; | |
67278d60 | 490 | |
6797073f | 491 | ------------- |
492 | -- CP_Move -- | |
493 | ------------- | |
67278d60 | 494 | |
6797073f | 495 | procedure CP_Move (From : Natural; To : Natural) is |
496 | begin | |
497 | Comps (To) := Comps (From); | |
498 | end CP_Move; | |
67278d60 | 499 | |
500 | -- Start of processing for Sort_CC | |
59ac57b5 | 501 | |
6797073f | 502 | begin |
503 | -- Collect the component clauses | |
59ac57b5 | 504 | |
6797073f | 505 | Num_CC := 0; |
506 | Comp := First_Component_Or_Discriminant (R); | |
507 | while Present (Comp) loop | |
508 | if Present (Component_Clause (Comp)) | |
509 | and then Esize (Comp) <= Max_Machine_Scalar_Size | |
510 | then | |
511 | Num_CC := Num_CC + 1; | |
512 | Comps (Num_CC) := Comp; | |
513 | end if; | |
59ac57b5 | 514 | |
6797073f | 515 | Next_Component_Or_Discriminant (Comp); |
516 | end loop; | |
67278d60 | 517 | |
6797073f | 518 | -- Sort by ascending position number |
67278d60 | 519 | |
6797073f | 520 | Sorting.Sort (Num_CC); |
67278d60 | 521 | |
6797073f | 522 | -- We now have all the components whose size does not exceed |
523 | -- the max machine scalar value, sorted by starting position. | |
524 | -- In this loop we gather groups of clauses starting at the | |
525 | -- same position, to process them in accordance with AI-133. | |
67278d60 | 526 | |
6797073f | 527 | Stop := 0; |
528 | while Stop < Num_CC loop | |
529 | Start := Stop + 1; | |
530 | Stop := Start; | |
531 | MaxL := | |
532 | Static_Integer | |
533 | (Last_Bit (Component_Clause (Comps (Start)))); | |
67278d60 | 534 | while Stop < Num_CC loop |
6797073f | 535 | if Static_Integer |
536 | (Position (Component_Clause (Comps (Stop + 1)))) = | |
537 | Static_Integer | |
538 | (Position (Component_Clause (Comps (Stop)))) | |
539 | then | |
540 | Stop := Stop + 1; | |
541 | MaxL := | |
542 | UI_Max | |
543 | (MaxL, | |
544 | Static_Integer | |
545 | (Last_Bit | |
546 | (Component_Clause (Comps (Stop))))); | |
547 | else | |
548 | exit; | |
549 | end if; | |
550 | end loop; | |
67278d60 | 551 | |
6797073f | 552 | -- Now we have a group of component clauses from Start to |
553 | -- Stop whose positions are identical, and MaxL is the | |
554 | -- maximum last bit value of any of these components. | |
555 | ||
556 | -- We need to determine the corresponding machine scalar | |
557 | -- size. This loop assumes that machine scalar sizes are | |
558 | -- even, and that each possible machine scalar has twice | |
559 | -- as many bits as the next smaller one. | |
560 | ||
561 | MSS := Max_Machine_Scalar_Size; | |
562 | while MSS mod 2 = 0 | |
563 | and then (MSS / 2) >= SSU | |
564 | and then (MSS / 2) > MaxL | |
565 | loop | |
566 | MSS := MSS / 2; | |
567 | end loop; | |
67278d60 | 568 | |
6797073f | 569 | -- Here is where we fix up the Component_Bit_Offset value |
570 | -- to account for the reverse bit order. Some examples of | |
571 | -- what needs to be done for the case of a machine scalar | |
572 | -- size of 8 are: | |
67278d60 | 573 | |
6797073f | 574 | -- First_Bit .. Last_Bit Component_Bit_Offset |
575 | -- old new old new | |
67278d60 | 576 | |
6797073f | 577 | -- 0 .. 0 7 .. 7 0 7 |
578 | -- 0 .. 1 6 .. 7 0 6 | |
579 | -- 0 .. 2 5 .. 7 0 5 | |
580 | -- 0 .. 7 0 .. 7 0 4 | |
67278d60 | 581 | |
6797073f | 582 | -- 1 .. 1 6 .. 6 1 6 |
583 | -- 1 .. 4 3 .. 6 1 3 | |
584 | -- 4 .. 7 0 .. 3 4 0 | |
67278d60 | 585 | |
6797073f | 586 | -- The rule is that the first bit is obtained by subtracting |
587 | -- the old ending bit from machine scalar size - 1. | |
67278d60 | 588 | |
6797073f | 589 | for C in Start .. Stop loop |
590 | declare | |
591 | Comp : constant Entity_Id := Comps (C); | |
592 | CC : constant Node_Id := | |
593 | Component_Clause (Comp); | |
594 | LB : constant Uint := | |
595 | Static_Integer (Last_Bit (CC)); | |
596 | NFB : constant Uint := MSS - Uint_1 - LB; | |
597 | NLB : constant Uint := NFB + Esize (Comp) - 1; | |
598 | Pos : constant Uint := | |
599 | Static_Integer (Position (CC)); | |
67278d60 | 600 | |
6797073f | 601 | begin |
602 | if Warn_On_Reverse_Bit_Order then | |
603 | Error_Msg_Uint_1 := MSS; | |
604 | Error_Msg_N | |
605 | ("info: reverse bit order in machine " & | |
606 | "scalar of length^?", First_Bit (CC)); | |
607 | Error_Msg_Uint_1 := NFB; | |
608 | Error_Msg_Uint_2 := NLB; | |
609 | ||
610 | if Bytes_Big_Endian then | |
611 | Error_Msg_NE | |
612 | ("?\info: big-endian range for " | |
613 | & "component & is ^ .. ^", | |
614 | First_Bit (CC), Comp); | |
615 | else | |
616 | Error_Msg_NE | |
617 | ("?\info: little-endian range " | |
618 | & "for component & is ^ .. ^", | |
619 | First_Bit (CC), Comp); | |
67278d60 | 620 | end if; |
6797073f | 621 | end if; |
67278d60 | 622 | |
6797073f | 623 | Set_Component_Bit_Offset (Comp, Pos * SSU + NFB); |
624 | Set_Normalized_First_Bit (Comp, NFB mod SSU); | |
625 | end; | |
67278d60 | 626 | end loop; |
6797073f | 627 | end loop; |
628 | end Sort_CC; | |
629 | end; | |
630 | end if; | |
59ac57b5 | 631 | end Adjust_Record_For_Reverse_Bit_Order; |
632 | ||
d6f39728 | 633 | -------------------------------------- |
634 | -- Alignment_Check_For_Esize_Change -- | |
635 | -------------------------------------- | |
636 | ||
637 | procedure Alignment_Check_For_Esize_Change (Typ : Entity_Id) is | |
638 | begin | |
639 | -- If the alignment is known, and not set by a rep clause, and is | |
640 | -- inconsistent with the size being set, then reset it to unknown, | |
641 | -- we assume in this case that the size overrides the inherited | |
642 | -- alignment, and that the alignment must be recomputed. | |
643 | ||
644 | if Known_Alignment (Typ) | |
645 | and then not Has_Alignment_Clause (Typ) | |
646 | and then Esize (Typ) mod (Alignment (Typ) * SSU) /= 0 | |
647 | then | |
648 | Init_Alignment (Typ); | |
649 | end if; | |
650 | end Alignment_Check_For_Esize_Change; | |
651 | ||
ae888dbd | 652 | ----------------------------------- |
653 | -- Analyze_Aspect_Specifications -- | |
654 | ----------------------------------- | |
655 | ||
656 | procedure Analyze_Aspect_Specifications | |
657 | (N : Node_Id; | |
658 | E : Entity_Id; | |
659 | L : List_Id) | |
660 | is | |
661 | Aspect : Node_Id; | |
d74fc39a | 662 | Aitem : Node_Id; |
ae888dbd | 663 | Ent : Node_Id; |
ae888dbd | 664 | |
665 | Ins_Node : Node_Id := N; | |
f54f1dff | 666 | -- Insert pragmas (except Pre/Post/Invariant/Predicate) after this node |
d74fc39a | 667 | |
668 | -- The general processing involves building an attribute definition | |
669 | -- clause or a pragma node that corresponds to the access type. Then | |
670 | -- one of two things happens: | |
671 | ||
672 | -- If we are required to delay the evaluation of this aspect to the | |
673 | -- freeze point, we preanalyze the relevant argument, and then attach | |
674 | -- the corresponding pragma/attribute definition clause to the aspect | |
675 | -- specification node, which is then placed in the Rep Item chain. | |
676 | -- In this case we mark the entity with the Has_Delayed_Aspects flag, | |
677 | -- and we evaluate the rep item at the freeze point. | |
678 | ||
679 | -- If no delay is required, we just insert the pragma or attribute | |
680 | -- after the declaration, and it will get processed by the normal | |
681 | -- circuit. The From_Aspect_Specification flag is set on the pragma | |
682 | -- or attribute definition node in either case to activate special | |
683 | -- processing (e.g. not traversing the list of homonyms for inline). | |
684 | ||
685 | Delay_Required : Boolean; | |
686 | -- Set True if delay is required | |
ae888dbd | 687 | |
688 | begin | |
f93e7257 | 689 | -- Return if no aspects |
690 | ||
ae888dbd | 691 | if L = No_List then |
692 | return; | |
693 | end if; | |
694 | ||
f93e7257 | 695 | -- Return if already analyzed (avoids duplicate calls in some cases |
696 | -- where type declarations get rewritten and proessed twice). | |
697 | ||
698 | if Analyzed (N) then | |
699 | return; | |
700 | end if; | |
701 | ||
702 | -- Loop through apsects | |
703 | ||
ae888dbd | 704 | Aspect := First (L); |
705 | while Present (Aspect) loop | |
706 | declare | |
94153a42 | 707 | Loc : constant Source_Ptr := Sloc (Aspect); |
708 | Id : constant Node_Id := Identifier (Aspect); | |
709 | Expr : constant Node_Id := Expression (Aspect); | |
710 | Nam : constant Name_Id := Chars (Id); | |
711 | A_Id : constant Aspect_Id := Get_Aspect_Id (Nam); | |
ae888dbd | 712 | Anod : Node_Id; |
d74fc39a | 713 | T : Entity_Id; |
ae888dbd | 714 | |
39e1f22f | 715 | Eloc : Source_Ptr := Sloc (Expr); |
716 | -- Source location of expression, modified when we split PPC's | |
717 | ||
ae888dbd | 718 | begin |
d74fc39a | 719 | Set_Entity (Aspect, E); |
720 | Ent := New_Occurrence_Of (E, Sloc (Id)); | |
721 | ||
1e3c4ae6 | 722 | -- Check for duplicate aspect. Note that the Comes_From_Source |
723 | -- test allows duplicate Pre/Post's that we generate internally | |
724 | -- to escape being flagged here. | |
ae888dbd | 725 | |
726 | Anod := First (L); | |
727 | while Anod /= Aspect loop | |
1e3c4ae6 | 728 | if Nam = Chars (Identifier (Anod)) |
729 | and then Comes_From_Source (Aspect) | |
730 | then | |
ae888dbd | 731 | Error_Msg_Name_1 := Nam; |
732 | Error_Msg_Sloc := Sloc (Anod); | |
39e1f22f | 733 | |
734 | -- Case of same aspect specified twice | |
735 | ||
736 | if Class_Present (Anod) = Class_Present (Aspect) then | |
737 | if not Class_Present (Anod) then | |
738 | Error_Msg_NE | |
739 | ("aspect% for & previously given#", | |
740 | Id, E); | |
741 | else | |
742 | Error_Msg_NE | |
743 | ("aspect `%''Class` for & previously given#", | |
744 | Id, E); | |
745 | end if; | |
746 | ||
747 | -- Case of Pre and Pre'Class both specified | |
748 | ||
749 | elsif Nam = Name_Pre then | |
750 | if Class_Present (Aspect) then | |
751 | Error_Msg_NE | |
752 | ("aspect `Pre''Class` for & is not allowed here", | |
753 | Id, E); | |
754 | Error_Msg_NE | |
755 | ("\since aspect `Pre` previously given#", | |
756 | Id, E); | |
757 | ||
758 | else | |
759 | Error_Msg_NE | |
760 | ("aspect `Pre` for & is not allowed here", | |
761 | Id, E); | |
762 | Error_Msg_NE | |
763 | ("\since aspect `Pre''Class` previously given#", | |
764 | Id, E); | |
765 | end if; | |
766 | end if; | |
767 | ||
ae888dbd | 768 | goto Continue; |
769 | end if; | |
770 | ||
771 | Next (Anod); | |
772 | end loop; | |
773 | ||
774 | -- Processing based on specific aspect | |
775 | ||
d74fc39a | 776 | case A_Id is |
ae888dbd | 777 | |
778 | -- No_Aspect should be impossible | |
779 | ||
780 | when No_Aspect => | |
781 | raise Program_Error; | |
782 | ||
5b5df4a9 | 783 | -- Aspects taking an optional boolean argument. For all of |
784 | -- these we just create a matching pragma and insert it, | |
785 | -- setting flag Cancel_Aspect if the expression is False. | |
ae888dbd | 786 | |
787 | when Aspect_Ada_2005 | | |
788 | Aspect_Ada_2012 | | |
789 | Aspect_Atomic | | |
790 | Aspect_Atomic_Components | | |
791 | Aspect_Discard_Names | | |
792 | Aspect_Favor_Top_Level | | |
793 | Aspect_Inline | | |
794 | Aspect_Inline_Always | | |
795 | Aspect_No_Return | | |
796 | Aspect_Pack | | |
797 | Aspect_Persistent_BSS | | |
798 | Aspect_Preelaborable_Initialization | | |
799 | Aspect_Pure_Function | | |
800 | Aspect_Shared | | |
801 | Aspect_Suppress_Debug_Info | | |
802 | Aspect_Unchecked_Union | | |
803 | Aspect_Universal_Aliasing | | |
804 | Aspect_Unmodified | | |
805 | Aspect_Unreferenced | | |
806 | Aspect_Unreferenced_Objects | | |
807 | Aspect_Volatile | | |
808 | Aspect_Volatile_Components => | |
809 | ||
d74fc39a | 810 | -- Build corresponding pragma node |
811 | ||
812 | Aitem := | |
94153a42 | 813 | Make_Pragma (Loc, |
d74fc39a | 814 | Pragma_Argument_Associations => New_List (Ent), |
815 | Pragma_Identifier => | |
816 | Make_Identifier (Sloc (Id), Chars (Id))); | |
817 | ||
818 | -- Deal with missing expression case, delay never needed | |
819 | ||
ae888dbd | 820 | if No (Expr) then |
d74fc39a | 821 | Delay_Required := False; |
822 | ||
823 | -- Expression is present | |
ae888dbd | 824 | |
825 | else | |
d74fc39a | 826 | Preanalyze_Spec_Expression (Expr, Standard_Boolean); |
ae888dbd | 827 | |
d74fc39a | 828 | -- If preanalysis gives a static expression, we don't |
829 | -- need to delay (this will happen often in practice). | |
ae888dbd | 830 | |
d74fc39a | 831 | if Is_OK_Static_Expression (Expr) then |
832 | Delay_Required := False; | |
ae888dbd | 833 | |
d74fc39a | 834 | if Is_False (Expr_Value (Expr)) then |
835 | Set_Aspect_Cancel (Aitem); | |
836 | end if; | |
ae888dbd | 837 | |
d74fc39a | 838 | -- If we don't get a static expression, then delay, the |
839 | -- expression may turn out static by freeze time. | |
ae888dbd | 840 | |
d74fc39a | 841 | else |
842 | Delay_Required := True; | |
843 | end if; | |
ae888dbd | 844 | end if; |
845 | ||
5b5df4a9 | 846 | -- Aspects corresponding to attribute definition clauses |
ae888dbd | 847 | |
c8969ba6 | 848 | when Aspect_Address | |
849 | Aspect_Alignment | | |
ae888dbd | 850 | Aspect_Bit_Order | |
851 | Aspect_Component_Size | | |
852 | Aspect_External_Tag | | |
853 | Aspect_Machine_Radix | | |
854 | Aspect_Object_Size | | |
855 | Aspect_Size | | |
856 | Aspect_Storage_Pool | | |
857 | Aspect_Storage_Size | | |
858 | Aspect_Stream_Size | | |
859 | Aspect_Value_Size => | |
860 | ||
d74fc39a | 861 | -- Preanalyze the expression with the appropriate type |
862 | ||
863 | case A_Id is | |
c8969ba6 | 864 | when Aspect_Address => |
865 | T := RTE (RE_Address); | |
d74fc39a | 866 | when Aspect_Bit_Order => |
867 | T := RTE (RE_Bit_Order); | |
868 | when Aspect_External_Tag => | |
869 | T := Standard_String; | |
870 | when Aspect_Storage_Pool => | |
871 | T := Class_Wide_Type (RTE (RE_Root_Storage_Pool)); | |
872 | when others => | |
873 | T := Any_Integer; | |
874 | end case; | |
875 | ||
876 | Preanalyze_Spec_Expression (Expr, T); | |
877 | ||
878 | -- Construct the attribute definition clause | |
879 | ||
880 | Aitem := | |
94153a42 | 881 | Make_Attribute_Definition_Clause (Loc, |
d74fc39a | 882 | Name => Ent, |
ae888dbd | 883 | Chars => Chars (Id), |
884 | Expression => Relocate_Node (Expr)); | |
885 | ||
d74fc39a | 886 | -- We do not need a delay if we have a static expression |
887 | ||
888 | if Is_OK_Static_Expression (Expression (Aitem)) then | |
889 | Delay_Required := False; | |
890 | ||
891 | -- Here a delay is required | |
892 | ||
893 | else | |
894 | Delay_Required := True; | |
895 | end if; | |
896 | ||
ae888dbd | 897 | -- Aspects corresponding to pragmas with two arguments, where |
898 | -- the first argument is a local name referring to the entity, | |
899 | -- and the second argument is the aspect definition expression. | |
900 | ||
901 | when Aspect_Suppress | | |
902 | Aspect_Unsuppress => | |
903 | ||
d74fc39a | 904 | -- Construct the pragma |
905 | ||
906 | Aitem := | |
94153a42 | 907 | Make_Pragma (Loc, |
ae888dbd | 908 | Pragma_Argument_Associations => New_List ( |
1e3c4ae6 | 909 | New_Occurrence_Of (E, Eloc), |
ae888dbd | 910 | Relocate_Node (Expr)), |
911 | Pragma_Identifier => | |
e7823792 | 912 | Make_Identifier (Sloc (Id), Chars (Id))); |
d74fc39a | 913 | |
914 | -- We don't have to play the delay game here, since the only | |
915 | -- values are check names which don't get analyzed anyway. | |
916 | ||
917 | Delay_Required := False; | |
ae888dbd | 918 | |
e7823792 | 919 | -- Aspects corresponding to stream routines |
920 | ||
921 | when Aspect_Input | | |
922 | Aspect_Output | | |
923 | Aspect_Read | | |
924 | Aspect_Write => | |
925 | ||
926 | -- Construct the attribute definition clause | |
927 | ||
928 | Aitem := | |
929 | Make_Attribute_Definition_Clause (Loc, | |
930 | Name => Ent, | |
931 | Chars => Chars (Id), | |
932 | Expression => Relocate_Node (Expr)); | |
933 | ||
934 | -- These are always delayed (typically the subprogram that | |
935 | -- is referenced cannot have been declared yet, since it has | |
936 | -- a reference to the type for which this aspect is defined. | |
937 | ||
938 | Delay_Required := True; | |
939 | ||
ae888dbd | 940 | -- Aspects corresponding to pragmas with two arguments, where |
941 | -- the second argument is a local name referring to the entity, | |
942 | -- and the first argument is the aspect definition expression. | |
943 | ||
944 | when Aspect_Warnings => | |
945 | ||
d74fc39a | 946 | -- Construct the pragma |
947 | ||
948 | Aitem := | |
94153a42 | 949 | Make_Pragma (Loc, |
ae888dbd | 950 | Pragma_Argument_Associations => New_List ( |
951 | Relocate_Node (Expr), | |
1e3c4ae6 | 952 | New_Occurrence_Of (E, Eloc)), |
ae888dbd | 953 | Pragma_Identifier => |
94153a42 | 954 | Make_Identifier (Sloc (Id), Chars (Id)), |
955 | Class_Present => Class_Present (Aspect)); | |
ae888dbd | 956 | |
d74fc39a | 957 | -- We don't have to play the delay game here, since the only |
958 | -- values are check names which don't get analyzed anyway. | |
959 | ||
960 | Delay_Required := False; | |
961 | ||
1e3c4ae6 | 962 | -- Aspects Pre/Post generate Precondition/Postcondition pragmas |
963 | -- with a first argument that is the expression, and a second | |
964 | -- argument that is an informative message if the test fails. | |
965 | -- This is inserted right after the declaration, to get the | |
5b5df4a9 | 966 | -- required pragma placement. The processing for the pragmas |
967 | -- takes care of the required delay. | |
ae888dbd | 968 | |
1e3c4ae6 | 969 | when Aspect_Pre | Aspect_Post => declare |
970 | Pname : Name_Id; | |
ae888dbd | 971 | |
1e3c4ae6 | 972 | begin |
973 | if A_Id = Aspect_Pre then | |
974 | Pname := Name_Precondition; | |
975 | else | |
976 | Pname := Name_Postcondition; | |
977 | end if; | |
d74fc39a | 978 | |
1e3c4ae6 | 979 | -- If the expressions is of the form A and then B, then |
980 | -- we generate separate Pre/Post aspects for the separate | |
981 | -- clauses. Since we allow multiple pragmas, there is no | |
982 | -- problem in allowing multiple Pre/Post aspects internally. | |
983 | ||
39e1f22f | 984 | -- We do not do this for Pre'Class, since we have to put |
985 | -- these conditions together in a complex OR expression | |
ae888dbd | 986 | |
39e1f22f | 987 | if Pname = Name_Postcondition |
988 | or else not Class_Present (Aspect) | |
989 | then | |
990 | while Nkind (Expr) = N_And_Then loop | |
991 | Insert_After (Aspect, | |
992 | Make_Aspect_Specification (Sloc (Right_Opnd (Expr)), | |
993 | Identifier => Identifier (Aspect), | |
994 | Expression => Relocate_Node (Right_Opnd (Expr)), | |
995 | Class_Present => Class_Present (Aspect), | |
996 | Split_PPC => True)); | |
997 | Rewrite (Expr, Relocate_Node (Left_Opnd (Expr))); | |
998 | Eloc := Sloc (Expr); | |
999 | end loop; | |
1000 | end if; | |
ae888dbd | 1001 | |
39e1f22f | 1002 | -- Build the precondition/postcondition pragma |
d74fc39a | 1003 | |
1004 | Aitem := | |
1e3c4ae6 | 1005 | Make_Pragma (Loc, |
ae888dbd | 1006 | Pragma_Identifier => |
1e3c4ae6 | 1007 | Make_Identifier (Sloc (Id), |
1008 | Chars => Pname), | |
94153a42 | 1009 | Class_Present => Class_Present (Aspect), |
39e1f22f | 1010 | Split_PPC => Split_PPC (Aspect), |
94153a42 | 1011 | Pragma_Argument_Associations => New_List ( |
1e3c4ae6 | 1012 | Make_Pragma_Argument_Association (Eloc, |
94153a42 | 1013 | Chars => Name_Check, |
39e1f22f | 1014 | Expression => Relocate_Node (Expr)))); |
1015 | ||
1016 | -- Add message unless exception messages are suppressed | |
1017 | ||
1018 | if not Opt.Exception_Locations_Suppressed then | |
1019 | Append_To (Pragma_Argument_Associations (Aitem), | |
1020 | Make_Pragma_Argument_Association (Eloc, | |
1021 | Chars => Name_Message, | |
1022 | Expression => | |
1023 | Make_String_Literal (Eloc, | |
1024 | Strval => "failed " | |
1025 | & Get_Name_String (Pname) | |
1026 | & " from " | |
1027 | & Build_Location_String (Eloc)))); | |
1028 | end if; | |
d74fc39a | 1029 | |
1e3c4ae6 | 1030 | Set_From_Aspect_Specification (Aitem, True); |
d74fc39a | 1031 | |
1e3c4ae6 | 1032 | -- For Pre/Post cases, insert immediately after the entity |
1033 | -- declaration, since that is the required pragma placement. | |
1034 | -- Note that for these aspects, we do not have to worry | |
1035 | -- about delay issues, since the pragmas themselves deal | |
1036 | -- with delay of visibility for the expression analysis. | |
1037 | ||
d2be415f | 1038 | -- If the entity is a library-level subprogram, the pre/ |
1039 | -- postconditions must be treated as late pragmas. | |
1040 | ||
1041 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1042 | Add_Global_Declaration (Aitem); | |
1043 | else | |
1044 | Insert_After (N, Aitem); | |
1045 | end if; | |
1046 | ||
1e3c4ae6 | 1047 | goto Continue; |
1048 | end; | |
ae888dbd | 1049 | |
4aed5405 | 1050 | -- Invariant aspects generate a corresponding pragma with a |
1051 | -- first argument that is the entity, and the second argument | |
1052 | -- is the expression and anthird argument with an appropriate | |
1053 | -- message. This is inserted right after the declaration, to | |
1054 | -- get the required pragma placement. The pragma processing | |
1055 | -- takes care of the required delay. | |
ae888dbd | 1056 | |
4aed5405 | 1057 | when Aspect_Invariant => |
ae888dbd | 1058 | |
5b5df4a9 | 1059 | -- Construct the pragma |
1060 | ||
1061 | Aitem := | |
1062 | Make_Pragma (Loc, | |
1063 | Pragma_Argument_Associations => | |
1064 | New_List (Ent, Relocate_Node (Expr)), | |
1065 | Class_Present => Class_Present (Aspect), | |
1066 | Pragma_Identifier => | |
4aed5405 | 1067 | Make_Identifier (Sloc (Id), Name_Invariant)); |
5b5df4a9 | 1068 | |
1069 | -- Add message unless exception messages are suppressed | |
1070 | ||
1071 | if not Opt.Exception_Locations_Suppressed then | |
1072 | Append_To (Pragma_Argument_Associations (Aitem), | |
1073 | Make_Pragma_Argument_Association (Eloc, | |
4aed5405 | 1074 | Chars => Name_Message, |
5b5df4a9 | 1075 | Expression => |
1076 | Make_String_Literal (Eloc, | |
1077 | Strval => "failed invariant from " | |
1078 | & Build_Location_String (Eloc)))); | |
1079 | end if; | |
1080 | ||
1081 | Set_From_Aspect_Specification (Aitem, True); | |
1082 | ||
4aed5405 | 1083 | -- For Invariant case, insert immediately after the entity |
1084 | -- declaration. We do not have to worry about delay issues | |
1085 | -- since the pragma processing takes care of this. | |
1086 | ||
1087 | Insert_After (N, Aitem); | |
1088 | goto Continue; | |
1089 | ||
1090 | -- Predicate aspects generate a corresponding pragma with a | |
1091 | -- first argument that is the entity, and the second argument | |
1092 | -- is the expression. This is inserted immediately after the | |
1093 | -- declaration, to get the required pragma placement. The | |
1094 | -- pragma processing takes care of the required delay. | |
1095 | ||
1096 | when Aspect_Predicate => | |
1097 | ||
1098 | -- Construct the pragma | |
1099 | ||
1100 | Aitem := | |
1101 | Make_Pragma (Loc, | |
1102 | Pragma_Argument_Associations => | |
1103 | New_List (Ent, Relocate_Node (Expr)), | |
1104 | Class_Present => Class_Present (Aspect), | |
1105 | Pragma_Identifier => | |
1106 | Make_Identifier (Sloc (Id), Name_Predicate)); | |
1107 | ||
1108 | Set_From_Aspect_Specification (Aitem, True); | |
1109 | ||
f93e7257 | 1110 | -- Make sure we have a freeze node (it might otherwise be |
1111 | -- missing in cases like subtype X is Y, and we would not | |
1112 | -- have a place to build the predicate function). | |
1113 | ||
1114 | Ensure_Freeze_Node (E); | |
1115 | ||
4aed5405 | 1116 | -- For Predicate case, insert immediately after the entity |
1117 | -- declaration. We do not have to worry about delay issues | |
1118 | -- since the pragma processing takes care of this. | |
5b5df4a9 | 1119 | |
1120 | Insert_After (N, Aitem); | |
1121 | goto Continue; | |
ae888dbd | 1122 | end case; |
1123 | ||
d74fc39a | 1124 | Set_From_Aspect_Specification (Aitem, True); |
1125 | ||
1126 | -- If a delay is required, we delay the freeze (not much point in | |
1127 | -- delaying the aspect if we don't delay the freeze!). The pragma | |
1128 | -- or clause is then attached to the aspect specification which | |
1129 | -- is placed in the rep item list. | |
1130 | ||
1131 | if Delay_Required then | |
1132 | Ensure_Freeze_Node (E); | |
1133 | Set_Is_Delayed_Aspect (Aitem); | |
1134 | Set_Has_Delayed_Aspects (E); | |
1135 | Set_Aspect_Rep_Item (Aspect, Aitem); | |
1136 | Record_Rep_Item (E, Aspect); | |
1137 | ||
1138 | -- If no delay required, insert the pragma/clause in the tree | |
1139 | ||
1140 | else | |
1141 | -- For Pre/Post cases, insert immediately after the entity | |
1142 | -- declaration, since that is the required pragma placement. | |
1143 | ||
1144 | if A_Id = Aspect_Pre or else A_Id = Aspect_Post then | |
1145 | Insert_After (N, Aitem); | |
1146 | ||
1147 | -- For all other cases, insert in sequence | |
1148 | ||
1149 | else | |
1150 | Insert_After (Ins_Node, Aitem); | |
1151 | Ins_Node := Aitem; | |
1152 | end if; | |
1153 | end if; | |
ae888dbd | 1154 | end; |
1155 | ||
1156 | <<Continue>> | |
1157 | Next (Aspect); | |
1158 | end loop; | |
1159 | end Analyze_Aspect_Specifications; | |
1160 | ||
d6f39728 | 1161 | ----------------------- |
1162 | -- Analyze_At_Clause -- | |
1163 | ----------------------- | |
1164 | ||
1165 | -- An at clause is replaced by the corresponding Address attribute | |
1166 | -- definition clause that is the preferred approach in Ada 95. | |
1167 | ||
1168 | procedure Analyze_At_Clause (N : Node_Id) is | |
177675a7 | 1169 | CS : constant Boolean := Comes_From_Source (N); |
1170 | ||
d6f39728 | 1171 | begin |
177675a7 | 1172 | -- This is an obsolescent feature |
1173 | ||
e0521a36 | 1174 | Check_Restriction (No_Obsolescent_Features, N); |
1175 | ||
9dfe12ae | 1176 | if Warn_On_Obsolescent_Feature then |
1177 | Error_Msg_N | |
fbc67f84 | 1178 | ("at clause is an obsolescent feature (RM J.7(2))?", N); |
9dfe12ae | 1179 | Error_Msg_N |
d53a018a | 1180 | ("\use address attribute definition clause instead?", N); |
9dfe12ae | 1181 | end if; |
1182 | ||
177675a7 | 1183 | -- Rewrite as address clause |
1184 | ||
d6f39728 | 1185 | Rewrite (N, |
1186 | Make_Attribute_Definition_Clause (Sloc (N), | |
1187 | Name => Identifier (N), | |
1188 | Chars => Name_Address, | |
1189 | Expression => Expression (N))); | |
177675a7 | 1190 | |
1191 | -- We preserve Comes_From_Source, since logically the clause still | |
1192 | -- comes from the source program even though it is changed in form. | |
1193 | ||
1194 | Set_Comes_From_Source (N, CS); | |
1195 | ||
1196 | -- Analyze rewritten clause | |
1197 | ||
d6f39728 | 1198 | Analyze_Attribute_Definition_Clause (N); |
1199 | end Analyze_At_Clause; | |
1200 | ||
1201 | ----------------------------------------- | |
1202 | -- Analyze_Attribute_Definition_Clause -- | |
1203 | ----------------------------------------- | |
1204 | ||
1205 | procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is | |
1206 | Loc : constant Source_Ptr := Sloc (N); | |
1207 | Nam : constant Node_Id := Name (N); | |
1208 | Attr : constant Name_Id := Chars (N); | |
1209 | Expr : constant Node_Id := Expression (N); | |
1210 | Id : constant Attribute_Id := Get_Attribute_Id (Attr); | |
1211 | Ent : Entity_Id; | |
1212 | U_Ent : Entity_Id; | |
1213 | ||
1214 | FOnly : Boolean := False; | |
1215 | -- Reset to True for subtype specific attribute (Alignment, Size) | |
1216 | -- and for stream attributes, i.e. those cases where in the call | |
1217 | -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing | |
1218 | -- rules are checked. Note that the case of stream attributes is not | |
1219 | -- clear from the RM, but see AI95-00137. Also, the RM seems to | |
1220 | -- disallow Storage_Size for derived task types, but that is also | |
1221 | -- clearly unintentional. | |
1222 | ||
9f373bb8 | 1223 | procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type); |
1224 | -- Common processing for 'Read, 'Write, 'Input and 'Output attribute | |
1225 | -- definition clauses. | |
1226 | ||
ae888dbd | 1227 | function Duplicate_Clause return Boolean; |
1228 | -- This routine checks if the aspect for U_Ent being given by attribute | |
1229 | -- definition clause N is for an aspect that has already been specified, | |
1230 | -- and if so gives an error message. If there is a duplicate, True is | |
1231 | -- returned, otherwise if there is no error, False is returned. | |
1232 | ||
177675a7 | 1233 | ----------------------------------- |
1234 | -- Analyze_Stream_TSS_Definition -- | |
1235 | ----------------------------------- | |
1236 | ||
9f373bb8 | 1237 | procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is |
1238 | Subp : Entity_Id := Empty; | |
1239 | I : Interp_Index; | |
1240 | It : Interp; | |
1241 | Pnam : Entity_Id; | |
1242 | ||
1243 | Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read); | |
1244 | ||
1245 | function Has_Good_Profile (Subp : Entity_Id) return Boolean; | |
1246 | -- Return true if the entity is a subprogram with an appropriate | |
1247 | -- profile for the attribute being defined. | |
1248 | ||
1249 | ---------------------- | |
1250 | -- Has_Good_Profile -- | |
1251 | ---------------------- | |
1252 | ||
1253 | function Has_Good_Profile (Subp : Entity_Id) return Boolean is | |
1254 | F : Entity_Id; | |
1255 | Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input); | |
1256 | Expected_Ekind : constant array (Boolean) of Entity_Kind := | |
1257 | (False => E_Procedure, True => E_Function); | |
1258 | Typ : Entity_Id; | |
1259 | ||
1260 | begin | |
1261 | if Ekind (Subp) /= Expected_Ekind (Is_Function) then | |
1262 | return False; | |
1263 | end if; | |
1264 | ||
1265 | F := First_Formal (Subp); | |
1266 | ||
1267 | if No (F) | |
1268 | or else Ekind (Etype (F)) /= E_Anonymous_Access_Type | |
1269 | or else Designated_Type (Etype (F)) /= | |
1270 | Class_Wide_Type (RTE (RE_Root_Stream_Type)) | |
1271 | then | |
1272 | return False; | |
1273 | end if; | |
1274 | ||
1275 | if not Is_Function then | |
1276 | Next_Formal (F); | |
1277 | ||
1278 | declare | |
1279 | Expected_Mode : constant array (Boolean) of Entity_Kind := | |
1280 | (False => E_In_Parameter, | |
1281 | True => E_Out_Parameter); | |
1282 | begin | |
1283 | if Parameter_Mode (F) /= Expected_Mode (Is_Read) then | |
1284 | return False; | |
1285 | end if; | |
1286 | end; | |
1287 | ||
1288 | Typ := Etype (F); | |
1289 | ||
1290 | else | |
1291 | Typ := Etype (Subp); | |
1292 | end if; | |
1293 | ||
1294 | return Base_Type (Typ) = Base_Type (Ent) | |
1295 | and then No (Next_Formal (F)); | |
9f373bb8 | 1296 | end Has_Good_Profile; |
1297 | ||
1298 | -- Start of processing for Analyze_Stream_TSS_Definition | |
1299 | ||
1300 | begin | |
1301 | FOnly := True; | |
1302 | ||
1303 | if not Is_Type (U_Ent) then | |
1304 | Error_Msg_N ("local name must be a subtype", Nam); | |
1305 | return; | |
1306 | end if; | |
1307 | ||
1308 | Pnam := TSS (Base_Type (U_Ent), TSS_Nam); | |
1309 | ||
44e4341e | 1310 | -- If Pnam is present, it can be either inherited from an ancestor |
1311 | -- type (in which case it is legal to redefine it for this type), or | |
1312 | -- be a previous definition of the attribute for the same type (in | |
1313 | -- which case it is illegal). | |
1314 | ||
1315 | -- In the first case, it will have been analyzed already, and we | |
1316 | -- can check that its profile does not match the expected profile | |
1317 | -- for a stream attribute of U_Ent. In the second case, either Pnam | |
1318 | -- has been analyzed (and has the expected profile), or it has not | |
1319 | -- been analyzed yet (case of a type that has not been frozen yet | |
1320 | -- and for which the stream attribute has been set using Set_TSS). | |
1321 | ||
1322 | if Present (Pnam) | |
1323 | and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam)) | |
1324 | then | |
9f373bb8 | 1325 | Error_Msg_Sloc := Sloc (Pnam); |
1326 | Error_Msg_Name_1 := Attr; | |
1327 | Error_Msg_N ("% attribute already defined #", Nam); | |
1328 | return; | |
1329 | end if; | |
1330 | ||
1331 | Analyze (Expr); | |
1332 | ||
1333 | if Is_Entity_Name (Expr) then | |
1334 | if not Is_Overloaded (Expr) then | |
1335 | if Has_Good_Profile (Entity (Expr)) then | |
1336 | Subp := Entity (Expr); | |
1337 | end if; | |
1338 | ||
1339 | else | |
1340 | Get_First_Interp (Expr, I, It); | |
9f373bb8 | 1341 | while Present (It.Nam) loop |
1342 | if Has_Good_Profile (It.Nam) then | |
1343 | Subp := It.Nam; | |
1344 | exit; | |
1345 | end if; | |
1346 | ||
1347 | Get_Next_Interp (I, It); | |
1348 | end loop; | |
1349 | end if; | |
1350 | end if; | |
1351 | ||
1352 | if Present (Subp) then | |
59ac57b5 | 1353 | if Is_Abstract_Subprogram (Subp) then |
9f373bb8 | 1354 | Error_Msg_N ("stream subprogram must not be abstract", Expr); |
1355 | return; | |
1356 | end if; | |
1357 | ||
1358 | Set_Entity (Expr, Subp); | |
1359 | Set_Etype (Expr, Etype (Subp)); | |
1360 | ||
44e4341e | 1361 | New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam); |
9f373bb8 | 1362 | |
1363 | else | |
1364 | Error_Msg_Name_1 := Attr; | |
1365 | Error_Msg_N ("incorrect expression for% attribute", Expr); | |
1366 | end if; | |
1367 | end Analyze_Stream_TSS_Definition; | |
1368 | ||
ae888dbd | 1369 | ---------------------- |
1370 | -- Duplicate_Clause -- | |
1371 | ---------------------- | |
1372 | ||
1373 | function Duplicate_Clause return Boolean is | |
d74fc39a | 1374 | A : Node_Id; |
ae888dbd | 1375 | |
1376 | begin | |
c8969ba6 | 1377 | -- Nothing to do if this attribute definition clause comes from |
1378 | -- an aspect specification, since we could not be duplicating an | |
ae888dbd | 1379 | -- explicit clause, and we dealt with the case of duplicated aspects |
1380 | -- in Analyze_Aspect_Specifications. | |
1381 | ||
1382 | if From_Aspect_Specification (N) then | |
1383 | return False; | |
1384 | end if; | |
1385 | ||
d74fc39a | 1386 | -- Otherwise current clause may duplicate previous clause or a |
1387 | -- previously given aspect specification for the same aspect. | |
1388 | ||
1389 | A := Get_Rep_Item_For_Entity (U_Ent, Chars (N)); | |
ae888dbd | 1390 | |
1391 | if Present (A) then | |
1392 | if Entity (A) = U_Ent then | |
1393 | Error_Msg_Name_1 := Chars (N); | |
1394 | Error_Msg_Sloc := Sloc (A); | |
39e1f22f | 1395 | Error_Msg_NE ("aspect% for & previously given#", N, U_Ent); |
ae888dbd | 1396 | return True; |
1397 | end if; | |
1398 | end if; | |
1399 | ||
1400 | return False; | |
1401 | end Duplicate_Clause; | |
1402 | ||
9f373bb8 | 1403 | -- Start of processing for Analyze_Attribute_Definition_Clause |
1404 | ||
d6f39728 | 1405 | begin |
eef1ca1e | 1406 | -- Process Ignore_Rep_Clauses option |
1407 | ||
fbc67f84 | 1408 | if Ignore_Rep_Clauses then |
9d627c41 | 1409 | case Id is |
1410 | ||
eef1ca1e | 1411 | -- The following should be ignored. They do not affect legality |
1412 | -- and may be target dependent. The basic idea of -gnatI is to | |
1413 | -- ignore any rep clauses that may be target dependent but do not | |
1414 | -- affect legality (except possibly to be rejected because they | |
1415 | -- are incompatible with the compilation target). | |
9d627c41 | 1416 | |
2f1aac99 | 1417 | when Attribute_Alignment | |
9d627c41 | 1418 | Attribute_Bit_Order | |
1419 | Attribute_Component_Size | | |
1420 | Attribute_Machine_Radix | | |
1421 | Attribute_Object_Size | | |
1422 | Attribute_Size | | |
1423 | Attribute_Small | | |
1424 | Attribute_Stream_Size | | |
1425 | Attribute_Value_Size => | |
1426 | ||
1427 | Rewrite (N, Make_Null_Statement (Sloc (N))); | |
1428 | return; | |
1429 | ||
eef1ca1e | 1430 | -- The following should not be ignored, because in the first place |
1431 | -- they are reasonably portable, and should not cause problems in | |
1432 | -- compiling code from another target, and also they do affect | |
1433 | -- legality, e.g. failing to provide a stream attribute for a | |
1434 | -- type may make a program illegal. | |
9d627c41 | 1435 | |
1436 | when Attribute_External_Tag | | |
1437 | Attribute_Input | | |
1438 | Attribute_Output | | |
1439 | Attribute_Read | | |
1440 | Attribute_Storage_Pool | | |
1441 | Attribute_Storage_Size | | |
1442 | Attribute_Write => | |
1443 | null; | |
1444 | ||
b593a52c | 1445 | -- Other cases are errors ("attribute& cannot be set with |
1446 | -- definition clause"), which will be caught below. | |
9d627c41 | 1447 | |
1448 | when others => | |
1449 | null; | |
1450 | end case; | |
fbc67f84 | 1451 | end if; |
1452 | ||
d6f39728 | 1453 | Analyze (Nam); |
1454 | Ent := Entity (Nam); | |
1455 | ||
1456 | if Rep_Item_Too_Early (Ent, N) then | |
1457 | return; | |
1458 | end if; | |
1459 | ||
9f373bb8 | 1460 | -- Rep clause applies to full view of incomplete type or private type if |
1461 | -- we have one (if not, this is a premature use of the type). However, | |
1462 | -- certain semantic checks need to be done on the specified entity (i.e. | |
1463 | -- the private view), so we save it in Ent. | |
d6f39728 | 1464 | |
1465 | if Is_Private_Type (Ent) | |
1466 | and then Is_Derived_Type (Ent) | |
1467 | and then not Is_Tagged_Type (Ent) | |
1468 | and then No (Full_View (Ent)) | |
1469 | then | |
9f373bb8 | 1470 | -- If this is a private type whose completion is a derivation from |
1471 | -- another private type, there is no full view, and the attribute | |
1472 | -- belongs to the type itself, not its underlying parent. | |
d6f39728 | 1473 | |
1474 | U_Ent := Ent; | |
1475 | ||
1476 | elsif Ekind (Ent) = E_Incomplete_Type then | |
d5b349fa | 1477 | |
9f373bb8 | 1478 | -- The attribute applies to the full view, set the entity of the |
1479 | -- attribute definition accordingly. | |
d5b349fa | 1480 | |
d6f39728 | 1481 | Ent := Underlying_Type (Ent); |
1482 | U_Ent := Ent; | |
d5b349fa | 1483 | Set_Entity (Nam, Ent); |
1484 | ||
d6f39728 | 1485 | else |
1486 | U_Ent := Underlying_Type (Ent); | |
1487 | end if; | |
1488 | ||
1489 | -- Complete other routine error checks | |
1490 | ||
1491 | if Etype (Nam) = Any_Type then | |
1492 | return; | |
1493 | ||
1494 | elsif Scope (Ent) /= Current_Scope then | |
1495 | Error_Msg_N ("entity must be declared in this scope", Nam); | |
1496 | return; | |
1497 | ||
f15731c4 | 1498 | elsif No (U_Ent) then |
1499 | U_Ent := Ent; | |
1500 | ||
d6f39728 | 1501 | elsif Is_Type (U_Ent) |
1502 | and then not Is_First_Subtype (U_Ent) | |
1503 | and then Id /= Attribute_Object_Size | |
1504 | and then Id /= Attribute_Value_Size | |
1505 | and then not From_At_Mod (N) | |
1506 | then | |
1507 | Error_Msg_N ("cannot specify attribute for subtype", Nam); | |
1508 | return; | |
d6f39728 | 1509 | end if; |
1510 | ||
ae888dbd | 1511 | Set_Entity (N, U_Ent); |
1512 | ||
d6f39728 | 1513 | -- Switch on particular attribute |
1514 | ||
1515 | case Id is | |
1516 | ||
1517 | ------------- | |
1518 | -- Address -- | |
1519 | ------------- | |
1520 | ||
1521 | -- Address attribute definition clause | |
1522 | ||
1523 | when Attribute_Address => Address : begin | |
177675a7 | 1524 | |
1525 | -- A little error check, catch for X'Address use X'Address; | |
1526 | ||
1527 | if Nkind (Nam) = N_Identifier | |
1528 | and then Nkind (Expr) = N_Attribute_Reference | |
1529 | and then Attribute_Name (Expr) = Name_Address | |
1530 | and then Nkind (Prefix (Expr)) = N_Identifier | |
1531 | and then Chars (Nam) = Chars (Prefix (Expr)) | |
1532 | then | |
1533 | Error_Msg_NE | |
1534 | ("address for & is self-referencing", Prefix (Expr), Ent); | |
1535 | return; | |
1536 | end if; | |
1537 | ||
1538 | -- Not that special case, carry on with analysis of expression | |
1539 | ||
d6f39728 | 1540 | Analyze_And_Resolve (Expr, RTE (RE_Address)); |
1541 | ||
2f1aac99 | 1542 | -- Even when ignoring rep clauses we need to indicate that the |
1543 | -- entity has an address clause and thus it is legal to declare | |
1544 | -- it imported. | |
1545 | ||
1546 | if Ignore_Rep_Clauses then | |
d3ef794c | 1547 | if Ekind_In (U_Ent, E_Variable, E_Constant) then |
2f1aac99 | 1548 | Record_Rep_Item (U_Ent, N); |
1549 | end if; | |
1550 | ||
1551 | return; | |
1552 | end if; | |
1553 | ||
ae888dbd | 1554 | if Duplicate_Clause then |
1555 | null; | |
d6f39728 | 1556 | |
1557 | -- Case of address clause for subprogram | |
1558 | ||
1559 | elsif Is_Subprogram (U_Ent) then | |
d6f39728 | 1560 | if Has_Homonym (U_Ent) then |
1561 | Error_Msg_N | |
1562 | ("address clause cannot be given " & | |
1563 | "for overloaded subprogram", | |
1564 | Nam); | |
83f8f0a6 | 1565 | return; |
d6f39728 | 1566 | end if; |
1567 | ||
83f8f0a6 | 1568 | -- For subprograms, all address clauses are permitted, and we |
1569 | -- mark the subprogram as having a deferred freeze so that Gigi | |
1570 | -- will not elaborate it too soon. | |
d6f39728 | 1571 | |
1572 | -- Above needs more comments, what is too soon about??? | |
1573 | ||
1574 | Set_Has_Delayed_Freeze (U_Ent); | |
1575 | ||
1576 | -- Case of address clause for entry | |
1577 | ||
1578 | elsif Ekind (U_Ent) = E_Entry then | |
d6f39728 | 1579 | if Nkind (Parent (N)) = N_Task_Body then |
1580 | Error_Msg_N | |
1581 | ("entry address must be specified in task spec", Nam); | |
83f8f0a6 | 1582 | return; |
d6f39728 | 1583 | end if; |
1584 | ||
1585 | -- For entries, we require a constant address | |
1586 | ||
1587 | Check_Constant_Address_Clause (Expr, U_Ent); | |
1588 | ||
83f8f0a6 | 1589 | -- Special checks for task types |
1590 | ||
f15731c4 | 1591 | if Is_Task_Type (Scope (U_Ent)) |
1592 | and then Comes_From_Source (Scope (U_Ent)) | |
1593 | then | |
1594 | Error_Msg_N | |
1595 | ("?entry address declared for entry in task type", N); | |
1596 | Error_Msg_N | |
1597 | ("\?only one task can be declared of this type", N); | |
1598 | end if; | |
1599 | ||
83f8f0a6 | 1600 | -- Entry address clauses are obsolescent |
1601 | ||
e0521a36 | 1602 | Check_Restriction (No_Obsolescent_Features, N); |
1603 | ||
9dfe12ae | 1604 | if Warn_On_Obsolescent_Feature then |
1605 | Error_Msg_N | |
1606 | ("attaching interrupt to task entry is an " & | |
fbc67f84 | 1607 | "obsolescent feature (RM J.7.1)?", N); |
9dfe12ae | 1608 | Error_Msg_N |
d53a018a | 1609 | ("\use interrupt procedure instead?", N); |
9dfe12ae | 1610 | end if; |
1611 | ||
83f8f0a6 | 1612 | -- Case of an address clause for a controlled object which we |
1613 | -- consider to be erroneous. | |
9dfe12ae | 1614 | |
83f8f0a6 | 1615 | elsif Is_Controlled (Etype (U_Ent)) |
1616 | or else Has_Controlled_Component (Etype (U_Ent)) | |
1617 | then | |
9dfe12ae | 1618 | Error_Msg_NE |
1619 | ("?controlled object& must not be overlaid", Nam, U_Ent); | |
1620 | Error_Msg_N | |
1621 | ("\?Program_Error will be raised at run time", Nam); | |
1622 | Insert_Action (Declaration_Node (U_Ent), | |
1623 | Make_Raise_Program_Error (Loc, | |
1624 | Reason => PE_Overlaid_Controlled_Object)); | |
83f8f0a6 | 1625 | return; |
9dfe12ae | 1626 | |
1627 | -- Case of address clause for a (non-controlled) object | |
d6f39728 | 1628 | |
1629 | elsif | |
1630 | Ekind (U_Ent) = E_Variable | |
1631 | or else | |
1632 | Ekind (U_Ent) = E_Constant | |
1633 | then | |
1634 | declare | |
d6da7448 | 1635 | Expr : constant Node_Id := Expression (N); |
1636 | O_Ent : Entity_Id; | |
1637 | Off : Boolean; | |
d6f39728 | 1638 | |
1639 | begin | |
7ee315cc | 1640 | -- Exported variables cannot have an address clause, because |
1641 | -- this cancels the effect of the pragma Export. | |
d6f39728 | 1642 | |
1643 | if Is_Exported (U_Ent) then | |
1644 | Error_Msg_N | |
1645 | ("cannot export object with address clause", Nam); | |
83f8f0a6 | 1646 | return; |
d6da7448 | 1647 | end if; |
1648 | ||
1649 | Find_Overlaid_Entity (N, O_Ent, Off); | |
d6f39728 | 1650 | |
9dfe12ae | 1651 | -- Overlaying controlled objects is erroneous |
1652 | ||
d6da7448 | 1653 | if Present (O_Ent) |
1654 | and then (Has_Controlled_Component (Etype (O_Ent)) | |
1655 | or else Is_Controlled (Etype (O_Ent))) | |
9dfe12ae | 1656 | then |
1657 | Error_Msg_N | |
83f8f0a6 | 1658 | ("?cannot overlay with controlled object", Expr); |
9dfe12ae | 1659 | Error_Msg_N |
1660 | ("\?Program_Error will be raised at run time", Expr); | |
1661 | Insert_Action (Declaration_Node (U_Ent), | |
1662 | Make_Raise_Program_Error (Loc, | |
1663 | Reason => PE_Overlaid_Controlled_Object)); | |
83f8f0a6 | 1664 | return; |
9dfe12ae | 1665 | |
d6da7448 | 1666 | elsif Present (O_Ent) |
9dfe12ae | 1667 | and then Ekind (U_Ent) = E_Constant |
d6da7448 | 1668 | and then not Is_Constant_Object (O_Ent) |
9dfe12ae | 1669 | then |
1670 | Error_Msg_N ("constant overlays a variable?", Expr); | |
1671 | ||
1672 | elsif Present (Renamed_Object (U_Ent)) then | |
1673 | Error_Msg_N | |
1674 | ("address clause not allowed" | |
fbc67f84 | 1675 | & " for a renaming declaration (RM 13.1(6))", Nam); |
83f8f0a6 | 1676 | return; |
9dfe12ae | 1677 | |
d6f39728 | 1678 | -- Imported variables can have an address clause, but then |
1679 | -- the import is pretty meaningless except to suppress | |
1680 | -- initializations, so we do not need such variables to | |
1681 | -- be statically allocated (and in fact it causes trouble | |
1682 | -- if the address clause is a local value). | |
1683 | ||
1684 | elsif Is_Imported (U_Ent) then | |
1685 | Set_Is_Statically_Allocated (U_Ent, False); | |
1686 | end if; | |
1687 | ||
1688 | -- We mark a possible modification of a variable with an | |
1689 | -- address clause, since it is likely aliasing is occurring. | |
1690 | ||
177675a7 | 1691 | Note_Possible_Modification (Nam, Sure => False); |
d6f39728 | 1692 | |
83f8f0a6 | 1693 | -- Here we are checking for explicit overlap of one variable |
1694 | -- by another, and if we find this then mark the overlapped | |
1695 | -- variable as also being volatile to prevent unwanted | |
d6da7448 | 1696 | -- optimizations. This is a significant pessimization so |
1697 | -- avoid it when there is an offset, i.e. when the object | |
1698 | -- is composite; they cannot be optimized easily anyway. | |
d6f39728 | 1699 | |
d6da7448 | 1700 | if Present (O_Ent) |
1701 | and then Is_Object (O_Ent) | |
1702 | and then not Off | |
1703 | then | |
1704 | Set_Treat_As_Volatile (O_Ent); | |
d6f39728 | 1705 | end if; |
1706 | ||
9dfe12ae | 1707 | -- Legality checks on the address clause for initialized |
1708 | -- objects is deferred until the freeze point, because | |
1709 | -- a subsequent pragma might indicate that the object is | |
1710 | -- imported and thus not initialized. | |
1711 | ||
1712 | Set_Has_Delayed_Freeze (U_Ent); | |
1713 | ||
51ad5ad2 | 1714 | -- If an initialization call has been generated for this |
1715 | -- object, it needs to be deferred to after the freeze node | |
1716 | -- we have just now added, otherwise GIGI will see a | |
1717 | -- reference to the variable (as actual to the IP call) | |
1718 | -- before its definition. | |
1719 | ||
1720 | declare | |
1721 | Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N); | |
1722 | begin | |
1723 | if Present (Init_Call) then | |
1724 | Remove (Init_Call); | |
1725 | Append_Freeze_Action (U_Ent, Init_Call); | |
1726 | end if; | |
1727 | end; | |
1728 | ||
d6f39728 | 1729 | if Is_Exported (U_Ent) then |
1730 | Error_Msg_N | |
1731 | ("& cannot be exported if an address clause is given", | |
1732 | Nam); | |
1733 | Error_Msg_N | |
1734 | ("\define and export a variable " & | |
1735 | "that holds its address instead", | |
1736 | Nam); | |
1737 | end if; | |
1738 | ||
44e4341e | 1739 | -- Entity has delayed freeze, so we will generate an |
1740 | -- alignment check at the freeze point unless suppressed. | |
d6f39728 | 1741 | |
44e4341e | 1742 | if not Range_Checks_Suppressed (U_Ent) |
1743 | and then not Alignment_Checks_Suppressed (U_Ent) | |
1744 | then | |
1745 | Set_Check_Address_Alignment (N); | |
1746 | end if; | |
d6f39728 | 1747 | |
1748 | -- Kill the size check code, since we are not allocating | |
1749 | -- the variable, it is somewhere else. | |
1750 | ||
1751 | Kill_Size_Check_Code (U_Ent); | |
83f8f0a6 | 1752 | |
d6da7448 | 1753 | -- If the address clause is of the form: |
83f8f0a6 | 1754 | |
d6da7448 | 1755 | -- for Y'Address use X'Address |
83f8f0a6 | 1756 | |
d6da7448 | 1757 | -- or |
83f8f0a6 | 1758 | |
d6da7448 | 1759 | -- Const : constant Address := X'Address; |
1760 | -- ... | |
1761 | -- for Y'Address use Const; | |
83f8f0a6 | 1762 | |
d6da7448 | 1763 | -- then we make an entry in the table for checking the size |
1764 | -- and alignment of the overlaying variable. We defer this | |
1765 | -- check till after code generation to take full advantage | |
1766 | -- of the annotation done by the back end. This entry is | |
1767 | -- only made if the address clause comes from source. | |
9474aa9c | 1768 | -- If the entity has a generic type, the check will be |
43dd6937 | 1769 | -- performed in the instance if the actual type justifies |
1770 | -- it, and we do not insert the clause in the table to | |
1771 | -- prevent spurious warnings. | |
83f8f0a6 | 1772 | |
d6da7448 | 1773 | if Address_Clause_Overlay_Warnings |
1774 | and then Comes_From_Source (N) | |
1775 | and then Present (O_Ent) | |
1776 | and then Is_Object (O_Ent) | |
1777 | then | |
9474aa9c | 1778 | if not Is_Generic_Type (Etype (U_Ent)) then |
1779 | Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off)); | |
1780 | end if; | |
177675a7 | 1781 | |
d6da7448 | 1782 | -- If variable overlays a constant view, and we are |
1783 | -- warning on overlays, then mark the variable as | |
1784 | -- overlaying a constant (we will give warnings later | |
1785 | -- if this variable is assigned). | |
177675a7 | 1786 | |
d6da7448 | 1787 | if Is_Constant_Object (O_Ent) |
1788 | and then Ekind (U_Ent) = E_Variable | |
1789 | then | |
1790 | Set_Overlays_Constant (U_Ent); | |
83f8f0a6 | 1791 | end if; |
d6da7448 | 1792 | end if; |
1793 | end; | |
83f8f0a6 | 1794 | |
d6f39728 | 1795 | -- Not a valid entity for an address clause |
1796 | ||
1797 | else | |
1798 | Error_Msg_N ("address cannot be given for &", Nam); | |
1799 | end if; | |
1800 | end Address; | |
1801 | ||
1802 | --------------- | |
1803 | -- Alignment -- | |
1804 | --------------- | |
1805 | ||
1806 | -- Alignment attribute definition clause | |
1807 | ||
b47769f0 | 1808 | when Attribute_Alignment => Alignment : declare |
9dfe12ae | 1809 | Align : constant Uint := Get_Alignment_Value (Expr); |
d6f39728 | 1810 | |
1811 | begin | |
1812 | FOnly := True; | |
1813 | ||
1814 | if not Is_Type (U_Ent) | |
1815 | and then Ekind (U_Ent) /= E_Variable | |
1816 | and then Ekind (U_Ent) /= E_Constant | |
1817 | then | |
1818 | Error_Msg_N ("alignment cannot be given for &", Nam); | |
1819 | ||
ae888dbd | 1820 | elsif Duplicate_Clause then |
1821 | null; | |
d6f39728 | 1822 | |
1823 | elsif Align /= No_Uint then | |
1824 | Set_Has_Alignment_Clause (U_Ent); | |
1825 | Set_Alignment (U_Ent, Align); | |
b47769f0 | 1826 | |
1827 | -- For an array type, U_Ent is the first subtype. In that case, | |
1828 | -- also set the alignment of the anonymous base type so that | |
1829 | -- other subtypes (such as the itypes for aggregates of the | |
1830 | -- type) also receive the expected alignment. | |
1831 | ||
1832 | if Is_Array_Type (U_Ent) then | |
1833 | Set_Alignment (Base_Type (U_Ent), Align); | |
1834 | end if; | |
d6f39728 | 1835 | end if; |
b47769f0 | 1836 | end Alignment; |
d6f39728 | 1837 | |
1838 | --------------- | |
1839 | -- Bit_Order -- | |
1840 | --------------- | |
1841 | ||
1842 | -- Bit_Order attribute definition clause | |
1843 | ||
1844 | when Attribute_Bit_Order => Bit_Order : declare | |
1845 | begin | |
1846 | if not Is_Record_Type (U_Ent) then | |
1847 | Error_Msg_N | |
1848 | ("Bit_Order can only be defined for record type", Nam); | |
1849 | ||
ae888dbd | 1850 | elsif Duplicate_Clause then |
1851 | null; | |
1852 | ||
d6f39728 | 1853 | else |
1854 | Analyze_And_Resolve (Expr, RTE (RE_Bit_Order)); | |
1855 | ||
1856 | if Etype (Expr) = Any_Type then | |
1857 | return; | |
1858 | ||
1859 | elsif not Is_Static_Expression (Expr) then | |
9dfe12ae | 1860 | Flag_Non_Static_Expr |
1861 | ("Bit_Order requires static expression!", Expr); | |
d6f39728 | 1862 | |
1863 | else | |
1864 | if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then | |
1865 | Set_Reverse_Bit_Order (U_Ent, True); | |
1866 | end if; | |
1867 | end if; | |
1868 | end if; | |
1869 | end Bit_Order; | |
1870 | ||
1871 | -------------------- | |
1872 | -- Component_Size -- | |
1873 | -------------------- | |
1874 | ||
1875 | -- Component_Size attribute definition clause | |
1876 | ||
1877 | when Attribute_Component_Size => Component_Size_Case : declare | |
1878 | Csize : constant Uint := Static_Integer (Expr); | |
a0fc8c5b | 1879 | Ctyp : Entity_Id; |
d6f39728 | 1880 | Btype : Entity_Id; |
1881 | Biased : Boolean; | |
1882 | New_Ctyp : Entity_Id; | |
1883 | Decl : Node_Id; | |
1884 | ||
1885 | begin | |
1886 | if not Is_Array_Type (U_Ent) then | |
1887 | Error_Msg_N ("component size requires array type", Nam); | |
1888 | return; | |
1889 | end if; | |
1890 | ||
1891 | Btype := Base_Type (U_Ent); | |
a0fc8c5b | 1892 | Ctyp := Component_Type (Btype); |
d6f39728 | 1893 | |
ae888dbd | 1894 | if Duplicate_Clause then |
1895 | null; | |
d6f39728 | 1896 | |
f3e4db96 | 1897 | elsif Rep_Item_Too_Early (Btype, N) then |
1898 | null; | |
1899 | ||
d6f39728 | 1900 | elsif Csize /= No_Uint then |
a0fc8c5b | 1901 | Check_Size (Expr, Ctyp, Csize, Biased); |
d6f39728 | 1902 | |
d74fc39a | 1903 | -- For the biased case, build a declaration for a subtype that |
1904 | -- will be used to represent the biased subtype that reflects | |
1905 | -- the biased representation of components. We need the subtype | |
1906 | -- to get proper conversions on referencing elements of the | |
1907 | -- array. Note: component size clauses are ignored in VM mode. | |
3062c401 | 1908 | |
1909 | if VM_Target = No_VM then | |
1910 | if Biased then | |
1911 | New_Ctyp := | |
1912 | Make_Defining_Identifier (Loc, | |
1913 | Chars => | |
1914 | New_External_Name (Chars (U_Ent), 'C', 0, 'T')); | |
1915 | ||
1916 | Decl := | |
1917 | Make_Subtype_Declaration (Loc, | |
1918 | Defining_Identifier => New_Ctyp, | |
1919 | Subtype_Indication => | |
1920 | New_Occurrence_Of (Component_Type (Btype), Loc)); | |
1921 | ||
1922 | Set_Parent (Decl, N); | |
1923 | Analyze (Decl, Suppress => All_Checks); | |
1924 | ||
1925 | Set_Has_Delayed_Freeze (New_Ctyp, False); | |
1926 | Set_Esize (New_Ctyp, Csize); | |
1927 | Set_RM_Size (New_Ctyp, Csize); | |
1928 | Init_Alignment (New_Ctyp); | |
3062c401 | 1929 | Set_Is_Itype (New_Ctyp, True); |
1930 | Set_Associated_Node_For_Itype (New_Ctyp, U_Ent); | |
1931 | ||
1932 | Set_Component_Type (Btype, New_Ctyp); | |
b77e4501 | 1933 | Set_Biased (New_Ctyp, N, "component size clause"); |
3062c401 | 1934 | end if; |
1935 | ||
1936 | Set_Component_Size (Btype, Csize); | |
1937 | ||
1938 | -- For VM case, we ignore component size clauses | |
1939 | ||
1940 | else | |
1941 | -- Give a warning unless we are in GNAT mode, in which case | |
1942 | -- the warning is suppressed since it is not useful. | |
1943 | ||
1944 | if not GNAT_Mode then | |
1945 | Error_Msg_N | |
1946 | ("?component size ignored in this configuration", N); | |
1947 | end if; | |
d6f39728 | 1948 | end if; |
1949 | ||
a0fc8c5b | 1950 | -- Deal with warning on overridden size |
1951 | ||
1952 | if Warn_On_Overridden_Size | |
1953 | and then Has_Size_Clause (Ctyp) | |
1954 | and then RM_Size (Ctyp) /= Csize | |
1955 | then | |
1956 | Error_Msg_NE | |
1957 | ("?component size overrides size clause for&", | |
1958 | N, Ctyp); | |
1959 | end if; | |
1960 | ||
d6f39728 | 1961 | Set_Has_Component_Size_Clause (Btype, True); |
f3e4db96 | 1962 | Set_Has_Non_Standard_Rep (Btype, True); |
d6f39728 | 1963 | end if; |
1964 | end Component_Size_Case; | |
1965 | ||
1966 | ------------------ | |
1967 | -- External_Tag -- | |
1968 | ------------------ | |
1969 | ||
1970 | when Attribute_External_Tag => External_Tag : | |
1971 | begin | |
1972 | if not Is_Tagged_Type (U_Ent) then | |
1973 | Error_Msg_N ("should be a tagged type", Nam); | |
1974 | end if; | |
1975 | ||
ae888dbd | 1976 | if Duplicate_Clause then |
1977 | null; | |
d6f39728 | 1978 | |
9af0ddc7 | 1979 | else |
ae888dbd | 1980 | Analyze_And_Resolve (Expr, Standard_String); |
fbc67f84 | 1981 | |
ae888dbd | 1982 | if not Is_Static_Expression (Expr) then |
1983 | Flag_Non_Static_Expr | |
1984 | ("static string required for tag name!", Nam); | |
1985 | end if; | |
1986 | ||
1987 | if VM_Target = No_VM then | |
1988 | Set_Has_External_Tag_Rep_Clause (U_Ent); | |
1989 | else | |
1990 | Error_Msg_Name_1 := Attr; | |
1991 | Error_Msg_N | |
1992 | ("% attribute unsupported in this configuration", Nam); | |
1993 | end if; | |
1994 | ||
1995 | if not Is_Library_Level_Entity (U_Ent) then | |
1996 | Error_Msg_NE | |
1997 | ("?non-unique external tag supplied for &", N, U_Ent); | |
1998 | Error_Msg_N | |
1999 | ("?\same external tag applies to all subprogram calls", N); | |
2000 | Error_Msg_N | |
2001 | ("?\corresponding internal tag cannot be obtained", N); | |
2002 | end if; | |
fbc67f84 | 2003 | end if; |
d6f39728 | 2004 | end External_Tag; |
2005 | ||
2006 | ----------- | |
2007 | -- Input -- | |
2008 | ----------- | |
2009 | ||
9f373bb8 | 2010 | when Attribute_Input => |
2011 | Analyze_Stream_TSS_Definition (TSS_Stream_Input); | |
2012 | Set_Has_Specified_Stream_Input (Ent); | |
d6f39728 | 2013 | |
2014 | ------------------- | |
2015 | -- Machine_Radix -- | |
2016 | ------------------- | |
2017 | ||
2018 | -- Machine radix attribute definition clause | |
2019 | ||
2020 | when Attribute_Machine_Radix => Machine_Radix : declare | |
2021 | Radix : constant Uint := Static_Integer (Expr); | |
2022 | ||
2023 | begin | |
2024 | if not Is_Decimal_Fixed_Point_Type (U_Ent) then | |
2025 | Error_Msg_N ("decimal fixed-point type expected for &", Nam); | |
2026 | ||
ae888dbd | 2027 | elsif Duplicate_Clause then |
2028 | null; | |
d6f39728 | 2029 | |
2030 | elsif Radix /= No_Uint then | |
2031 | Set_Has_Machine_Radix_Clause (U_Ent); | |
2032 | Set_Has_Non_Standard_Rep (Base_Type (U_Ent)); | |
2033 | ||
2034 | if Radix = 2 then | |
2035 | null; | |
2036 | elsif Radix = 10 then | |
2037 | Set_Machine_Radix_10 (U_Ent); | |
2038 | else | |
2039 | Error_Msg_N ("machine radix value must be 2 or 10", Expr); | |
2040 | end if; | |
2041 | end if; | |
2042 | end Machine_Radix; | |
2043 | ||
2044 | ----------------- | |
2045 | -- Object_Size -- | |
2046 | ----------------- | |
2047 | ||
2048 | -- Object_Size attribute definition clause | |
2049 | ||
2050 | when Attribute_Object_Size => Object_Size : declare | |
bfa5a9d9 | 2051 | Size : constant Uint := Static_Integer (Expr); |
2052 | ||
d6f39728 | 2053 | Biased : Boolean; |
bfa5a9d9 | 2054 | pragma Warnings (Off, Biased); |
d6f39728 | 2055 | |
2056 | begin | |
2057 | if not Is_Type (U_Ent) then | |
2058 | Error_Msg_N ("Object_Size cannot be given for &", Nam); | |
2059 | ||
ae888dbd | 2060 | elsif Duplicate_Clause then |
2061 | null; | |
d6f39728 | 2062 | |
2063 | else | |
2064 | Check_Size (Expr, U_Ent, Size, Biased); | |
2065 | ||
2066 | if Size /= 8 | |
2067 | and then | |
2068 | Size /= 16 | |
2069 | and then | |
2070 | Size /= 32 | |
2071 | and then | |
2072 | UI_Mod (Size, 64) /= 0 | |
2073 | then | |
2074 | Error_Msg_N | |
2075 | ("Object_Size must be 8, 16, 32, or multiple of 64", | |
2076 | Expr); | |
2077 | end if; | |
2078 | ||
2079 | Set_Esize (U_Ent, Size); | |
2080 | Set_Has_Object_Size_Clause (U_Ent); | |
2081 | Alignment_Check_For_Esize_Change (U_Ent); | |
2082 | end if; | |
2083 | end Object_Size; | |
2084 | ||
2085 | ------------ | |
2086 | -- Output -- | |
2087 | ------------ | |
2088 | ||
9f373bb8 | 2089 | when Attribute_Output => |
2090 | Analyze_Stream_TSS_Definition (TSS_Stream_Output); | |
2091 | Set_Has_Specified_Stream_Output (Ent); | |
d6f39728 | 2092 | |
2093 | ---------- | |
2094 | -- Read -- | |
2095 | ---------- | |
2096 | ||
9f373bb8 | 2097 | when Attribute_Read => |
2098 | Analyze_Stream_TSS_Definition (TSS_Stream_Read); | |
2099 | Set_Has_Specified_Stream_Read (Ent); | |
d6f39728 | 2100 | |
2101 | ---------- | |
2102 | -- Size -- | |
2103 | ---------- | |
2104 | ||
2105 | -- Size attribute definition clause | |
2106 | ||
2107 | when Attribute_Size => Size : declare | |
2108 | Size : constant Uint := Static_Integer (Expr); | |
2109 | Etyp : Entity_Id; | |
2110 | Biased : Boolean; | |
2111 | ||
2112 | begin | |
2113 | FOnly := True; | |
2114 | ||
ae888dbd | 2115 | if Duplicate_Clause then |
2116 | null; | |
d6f39728 | 2117 | |
2118 | elsif not Is_Type (U_Ent) | |
2119 | and then Ekind (U_Ent) /= E_Variable | |
2120 | and then Ekind (U_Ent) /= E_Constant | |
2121 | then | |
2122 | Error_Msg_N ("size cannot be given for &", Nam); | |
2123 | ||
2124 | elsif Is_Array_Type (U_Ent) | |
2125 | and then not Is_Constrained (U_Ent) | |
2126 | then | |
2127 | Error_Msg_N | |
2128 | ("size cannot be given for unconstrained array", Nam); | |
2129 | ||
c2b89d6e | 2130 | elsif Size /= No_Uint then |
682fa897 | 2131 | |
c2b89d6e | 2132 | if VM_Target /= No_VM and then not GNAT_Mode then |
47495553 | 2133 | |
c2b89d6e | 2134 | -- Size clause is not handled properly on VM targets. |
2135 | -- Display a warning unless we are in GNAT mode, in which | |
2136 | -- case this is useless. | |
47495553 | 2137 | |
682fa897 | 2138 | Error_Msg_N |
2139 | ("?size clauses are ignored in this configuration", N); | |
2140 | end if; | |
2141 | ||
d6f39728 | 2142 | if Is_Type (U_Ent) then |
2143 | Etyp := U_Ent; | |
2144 | else | |
2145 | Etyp := Etype (U_Ent); | |
2146 | end if; | |
2147 | ||
59ac57b5 | 2148 | -- Check size, note that Gigi is in charge of checking that the |
2149 | -- size of an array or record type is OK. Also we do not check | |
2150 | -- the size in the ordinary fixed-point case, since it is too | |
2151 | -- early to do so (there may be subsequent small clause that | |
2152 | -- affects the size). We can check the size if a small clause | |
2153 | -- has already been given. | |
d6f39728 | 2154 | |
2155 | if not Is_Ordinary_Fixed_Point_Type (U_Ent) | |
2156 | or else Has_Small_Clause (U_Ent) | |
2157 | then | |
2158 | Check_Size (Expr, Etyp, Size, Biased); | |
b77e4501 | 2159 | Set_Biased (U_Ent, N, "size clause", Biased); |
d6f39728 | 2160 | end if; |
2161 | ||
2162 | -- For types set RM_Size and Esize if possible | |
2163 | ||
2164 | if Is_Type (U_Ent) then | |
2165 | Set_RM_Size (U_Ent, Size); | |
2166 | ||
59ac57b5 | 2167 | -- For scalar types, increase Object_Size to power of 2, but |
2168 | -- not less than a storage unit in any case (i.e., normally | |
2169 | -- this means it will be byte addressable). | |
d6f39728 | 2170 | |
2171 | if Is_Scalar_Type (U_Ent) then | |
f15731c4 | 2172 | if Size <= System_Storage_Unit then |
2173 | Init_Esize (U_Ent, System_Storage_Unit); | |
d6f39728 | 2174 | elsif Size <= 16 then |
2175 | Init_Esize (U_Ent, 16); | |
2176 | elsif Size <= 32 then | |
2177 | Init_Esize (U_Ent, 32); | |
2178 | else | |
2179 | Set_Esize (U_Ent, (Size + 63) / 64 * 64); | |
2180 | end if; | |
2181 | ||
2182 | -- For all other types, object size = value size. The | |
2183 | -- backend will adjust as needed. | |
2184 | ||
2185 | else | |
2186 | Set_Esize (U_Ent, Size); | |
2187 | end if; | |
2188 | ||
2189 | Alignment_Check_For_Esize_Change (U_Ent); | |
2190 | ||
2191 | -- For objects, set Esize only | |
2192 | ||
2193 | else | |
9dfe12ae | 2194 | if Is_Elementary_Type (Etyp) then |
2195 | if Size /= System_Storage_Unit | |
2196 | and then | |
2197 | Size /= System_Storage_Unit * 2 | |
2198 | and then | |
2199 | Size /= System_Storage_Unit * 4 | |
2200 | and then | |
2201 | Size /= System_Storage_Unit * 8 | |
2202 | then | |
5c99c290 | 2203 | Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit); |
87d5c1d0 | 2204 | Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8; |
9dfe12ae | 2205 | Error_Msg_N |
5c99c290 | 2206 | ("size for primitive object must be a power of 2" |
87d5c1d0 | 2207 | & " in the range ^-^", N); |
9dfe12ae | 2208 | end if; |
2209 | end if; | |
2210 | ||
d6f39728 | 2211 | Set_Esize (U_Ent, Size); |
2212 | end if; | |
2213 | ||
2214 | Set_Has_Size_Clause (U_Ent); | |
2215 | end if; | |
2216 | end Size; | |
2217 | ||
2218 | ----------- | |
2219 | -- Small -- | |
2220 | ----------- | |
2221 | ||
2222 | -- Small attribute definition clause | |
2223 | ||
2224 | when Attribute_Small => Small : declare | |
2225 | Implicit_Base : constant Entity_Id := Base_Type (U_Ent); | |
2226 | Small : Ureal; | |
2227 | ||
2228 | begin | |
2229 | Analyze_And_Resolve (Expr, Any_Real); | |
2230 | ||
2231 | if Etype (Expr) = Any_Type then | |
2232 | return; | |
2233 | ||
2234 | elsif not Is_Static_Expression (Expr) then | |
9dfe12ae | 2235 | Flag_Non_Static_Expr |
2236 | ("small requires static expression!", Expr); | |
d6f39728 | 2237 | return; |
2238 | ||
2239 | else | |
2240 | Small := Expr_Value_R (Expr); | |
2241 | ||
2242 | if Small <= Ureal_0 then | |
2243 | Error_Msg_N ("small value must be greater than zero", Expr); | |
2244 | return; | |
2245 | end if; | |
2246 | ||
2247 | end if; | |
2248 | ||
2249 | if not Is_Ordinary_Fixed_Point_Type (U_Ent) then | |
2250 | Error_Msg_N | |
2251 | ("small requires an ordinary fixed point type", Nam); | |
2252 | ||
2253 | elsif Has_Small_Clause (U_Ent) then | |
2254 | Error_Msg_N ("small already given for &", Nam); | |
2255 | ||
2256 | elsif Small > Delta_Value (U_Ent) then | |
2257 | Error_Msg_N | |
2258 | ("small value must not be greater then delta value", Nam); | |
2259 | ||
2260 | else | |
2261 | Set_Small_Value (U_Ent, Small); | |
2262 | Set_Small_Value (Implicit_Base, Small); | |
2263 | Set_Has_Small_Clause (U_Ent); | |
2264 | Set_Has_Small_Clause (Implicit_Base); | |
2265 | Set_Has_Non_Standard_Rep (Implicit_Base); | |
2266 | end if; | |
2267 | end Small; | |
2268 | ||
d6f39728 | 2269 | ------------------ |
2270 | -- Storage_Pool -- | |
2271 | ------------------ | |
2272 | ||
2273 | -- Storage_Pool attribute definition clause | |
2274 | ||
2275 | when Attribute_Storage_Pool => Storage_Pool : declare | |
2276 | Pool : Entity_Id; | |
6b567c71 | 2277 | T : Entity_Id; |
d6f39728 | 2278 | |
2279 | begin | |
44e4341e | 2280 | if Ekind (U_Ent) = E_Access_Subprogram_Type then |
2281 | Error_Msg_N | |
2282 | ("storage pool cannot be given for access-to-subprogram type", | |
2283 | Nam); | |
2284 | return; | |
2285 | ||
d3ef794c | 2286 | elsif not |
2287 | Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type) | |
d6f39728 | 2288 | then |
44e4341e | 2289 | Error_Msg_N |
2290 | ("storage pool can only be given for access types", Nam); | |
d6f39728 | 2291 | return; |
2292 | ||
2293 | elsif Is_Derived_Type (U_Ent) then | |
2294 | Error_Msg_N | |
2295 | ("storage pool cannot be given for a derived access type", | |
2296 | Nam); | |
2297 | ||
ae888dbd | 2298 | elsif Duplicate_Clause then |
d6f39728 | 2299 | return; |
2300 | ||
2301 | elsif Present (Associated_Storage_Pool (U_Ent)) then | |
2302 | Error_Msg_N ("storage pool already given for &", Nam); | |
2303 | return; | |
2304 | end if; | |
2305 | ||
2306 | Analyze_And_Resolve | |
2307 | (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool))); | |
2308 | ||
8c5c7277 | 2309 | if not Denotes_Variable (Expr) then |
2310 | Error_Msg_N ("storage pool must be a variable", Expr); | |
2311 | return; | |
2312 | end if; | |
2313 | ||
6b567c71 | 2314 | if Nkind (Expr) = N_Type_Conversion then |
2315 | T := Etype (Expression (Expr)); | |
2316 | else | |
2317 | T := Etype (Expr); | |
2318 | end if; | |
2319 | ||
2320 | -- The Stack_Bounded_Pool is used internally for implementing | |
2321 | -- access types with a Storage_Size. Since it only work | |
2322 | -- properly when used on one specific type, we need to check | |
1a34e48c | 2323 | -- that it is not hijacked improperly: |
6b567c71 | 2324 | -- type T is access Integer; |
2325 | -- for T'Storage_Size use n; | |
2326 | -- type Q is access Float; | |
2327 | -- for Q'Storage_Size use T'Storage_Size; -- incorrect | |
2328 | ||
15ebb600 | 2329 | if RTE_Available (RE_Stack_Bounded_Pool) |
2330 | and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool) | |
2331 | then | |
2332 | Error_Msg_N ("non-shareable internal Pool", Expr); | |
6b567c71 | 2333 | return; |
2334 | end if; | |
2335 | ||
d6f39728 | 2336 | -- If the argument is a name that is not an entity name, then |
2337 | -- we construct a renaming operation to define an entity of | |
2338 | -- type storage pool. | |
2339 | ||
2340 | if not Is_Entity_Name (Expr) | |
2341 | and then Is_Object_Reference (Expr) | |
2342 | then | |
11deeeb6 | 2343 | Pool := Make_Temporary (Loc, 'P', Expr); |
d6f39728 | 2344 | |
2345 | declare | |
2346 | Rnode : constant Node_Id := | |
2347 | Make_Object_Renaming_Declaration (Loc, | |
2348 | Defining_Identifier => Pool, | |
2349 | Subtype_Mark => | |
2350 | New_Occurrence_Of (Etype (Expr), Loc), | |
11deeeb6 | 2351 | Name => Expr); |
d6f39728 | 2352 | |
2353 | begin | |
2354 | Insert_Before (N, Rnode); | |
2355 | Analyze (Rnode); | |
2356 | Set_Associated_Storage_Pool (U_Ent, Pool); | |
2357 | end; | |
2358 | ||
2359 | elsif Is_Entity_Name (Expr) then | |
2360 | Pool := Entity (Expr); | |
2361 | ||
2362 | -- If pool is a renamed object, get original one. This can | |
2363 | -- happen with an explicit renaming, and within instances. | |
2364 | ||
2365 | while Present (Renamed_Object (Pool)) | |
2366 | and then Is_Entity_Name (Renamed_Object (Pool)) | |
2367 | loop | |
2368 | Pool := Entity (Renamed_Object (Pool)); | |
2369 | end loop; | |
2370 | ||
2371 | if Present (Renamed_Object (Pool)) | |
2372 | and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion | |
2373 | and then Is_Entity_Name (Expression (Renamed_Object (Pool))) | |
2374 | then | |
2375 | Pool := Entity (Expression (Renamed_Object (Pool))); | |
2376 | end if; | |
2377 | ||
6b567c71 | 2378 | Set_Associated_Storage_Pool (U_Ent, Pool); |
d6f39728 | 2379 | |
2380 | elsif Nkind (Expr) = N_Type_Conversion | |
2381 | and then Is_Entity_Name (Expression (Expr)) | |
2382 | and then Nkind (Original_Node (Expr)) = N_Attribute_Reference | |
2383 | then | |
2384 | Pool := Entity (Expression (Expr)); | |
6b567c71 | 2385 | Set_Associated_Storage_Pool (U_Ent, Pool); |
d6f39728 | 2386 | |
2387 | else | |
2388 | Error_Msg_N ("incorrect reference to a Storage Pool", Expr); | |
2389 | return; | |
2390 | end if; | |
2391 | end Storage_Pool; | |
2392 | ||
44e4341e | 2393 | ------------------ |
2394 | -- Storage_Size -- | |
2395 | ------------------ | |
2396 | ||
2397 | -- Storage_Size attribute definition clause | |
2398 | ||
2399 | when Attribute_Storage_Size => Storage_Size : declare | |
2400 | Btype : constant Entity_Id := Base_Type (U_Ent); | |
2401 | Sprag : Node_Id; | |
2402 | ||
2403 | begin | |
2404 | if Is_Task_Type (U_Ent) then | |
2405 | Check_Restriction (No_Obsolescent_Features, N); | |
2406 | ||
2407 | if Warn_On_Obsolescent_Feature then | |
2408 | Error_Msg_N | |
2409 | ("storage size clause for task is an " & | |
fbc67f84 | 2410 | "obsolescent feature (RM J.9)?", N); |
503f7fd3 | 2411 | Error_Msg_N ("\use Storage_Size pragma instead?", N); |
44e4341e | 2412 | end if; |
2413 | ||
2414 | FOnly := True; | |
2415 | end if; | |
2416 | ||
2417 | if not Is_Access_Type (U_Ent) | |
2418 | and then Ekind (U_Ent) /= E_Task_Type | |
2419 | then | |
2420 | Error_Msg_N ("storage size cannot be given for &", Nam); | |
2421 | ||
2422 | elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then | |
2423 | Error_Msg_N | |
2424 | ("storage size cannot be given for a derived access type", | |
2425 | Nam); | |
2426 | ||
ae888dbd | 2427 | elsif Duplicate_Clause then |
2428 | null; | |
44e4341e | 2429 | |
2430 | else | |
2431 | Analyze_And_Resolve (Expr, Any_Integer); | |
2432 | ||
2433 | if Is_Access_Type (U_Ent) then | |
2434 | if Present (Associated_Storage_Pool (U_Ent)) then | |
2435 | Error_Msg_N ("storage pool already given for &", Nam); | |
2436 | return; | |
2437 | end if; | |
2438 | ||
5941a4e9 | 2439 | if Is_OK_Static_Expression (Expr) |
44e4341e | 2440 | and then Expr_Value (Expr) = 0 |
2441 | then | |
2442 | Set_No_Pool_Assigned (Btype); | |
2443 | end if; | |
2444 | ||
2445 | else -- Is_Task_Type (U_Ent) | |
2446 | Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size); | |
2447 | ||
2448 | if Present (Sprag) then | |
2449 | Error_Msg_Sloc := Sloc (Sprag); | |
2450 | Error_Msg_N | |
2451 | ("Storage_Size already specified#", Nam); | |
2452 | return; | |
2453 | end if; | |
2454 | end if; | |
2455 | ||
2456 | Set_Has_Storage_Size_Clause (Btype); | |
2457 | end if; | |
2458 | end Storage_Size; | |
2459 | ||
7189d17f | 2460 | ----------------- |
2461 | -- Stream_Size -- | |
2462 | ----------------- | |
2463 | ||
2464 | when Attribute_Stream_Size => Stream_Size : declare | |
2465 | Size : constant Uint := Static_Integer (Expr); | |
2466 | ||
2467 | begin | |
15ebb600 | 2468 | if Ada_Version <= Ada_95 then |
2469 | Check_Restriction (No_Implementation_Attributes, N); | |
2470 | end if; | |
2471 | ||
ae888dbd | 2472 | if Duplicate_Clause then |
2473 | null; | |
7189d17f | 2474 | |
2475 | elsif Is_Elementary_Type (U_Ent) then | |
2476 | if Size /= System_Storage_Unit | |
2477 | and then | |
2478 | Size /= System_Storage_Unit * 2 | |
2479 | and then | |
2480 | Size /= System_Storage_Unit * 4 | |
2481 | and then | |
2482 | Size /= System_Storage_Unit * 8 | |
2483 | then | |
2484 | Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit); | |
2485 | Error_Msg_N | |
2486 | ("stream size for elementary type must be a" | |
2487 | & " power of 2 and at least ^", N); | |
2488 | ||
2489 | elsif RM_Size (U_Ent) > Size then | |
2490 | Error_Msg_Uint_1 := RM_Size (U_Ent); | |
2491 | Error_Msg_N | |
2492 | ("stream size for elementary type must be a" | |
2493 | & " power of 2 and at least ^", N); | |
2494 | end if; | |
2495 | ||
2496 | Set_Has_Stream_Size_Clause (U_Ent); | |
2497 | ||
2498 | else | |
2499 | Error_Msg_N ("Stream_Size cannot be given for &", Nam); | |
2500 | end if; | |
2501 | end Stream_Size; | |
2502 | ||
d6f39728 | 2503 | ---------------- |
2504 | -- Value_Size -- | |
2505 | ---------------- | |
2506 | ||
2507 | -- Value_Size attribute definition clause | |
2508 | ||
2509 | when Attribute_Value_Size => Value_Size : declare | |
2510 | Size : constant Uint := Static_Integer (Expr); | |
2511 | Biased : Boolean; | |
2512 | ||
2513 | begin | |
2514 | if not Is_Type (U_Ent) then | |
2515 | Error_Msg_N ("Value_Size cannot be given for &", Nam); | |
2516 | ||
ae888dbd | 2517 | elsif Duplicate_Clause then |
2518 | null; | |
d6f39728 | 2519 | |
59ac57b5 | 2520 | elsif Is_Array_Type (U_Ent) |
2521 | and then not Is_Constrained (U_Ent) | |
2522 | then | |
2523 | Error_Msg_N | |
2524 | ("Value_Size cannot be given for unconstrained array", Nam); | |
2525 | ||
d6f39728 | 2526 | else |
2527 | if Is_Elementary_Type (U_Ent) then | |
2528 | Check_Size (Expr, U_Ent, Size, Biased); | |
b77e4501 | 2529 | Set_Biased (U_Ent, N, "value size clause", Biased); |
d6f39728 | 2530 | end if; |
2531 | ||
2532 | Set_RM_Size (U_Ent, Size); | |
2533 | end if; | |
2534 | end Value_Size; | |
2535 | ||
2536 | ----------- | |
2537 | -- Write -- | |
2538 | ----------- | |
2539 | ||
9f373bb8 | 2540 | when Attribute_Write => |
2541 | Analyze_Stream_TSS_Definition (TSS_Stream_Write); | |
2542 | Set_Has_Specified_Stream_Write (Ent); | |
d6f39728 | 2543 | |
2544 | -- All other attributes cannot be set | |
2545 | ||
2546 | when others => | |
2547 | Error_Msg_N | |
2548 | ("attribute& cannot be set with definition clause", N); | |
d6f39728 | 2549 | end case; |
2550 | ||
2551 | -- The test for the type being frozen must be performed after | |
2552 | -- any expression the clause has been analyzed since the expression | |
2553 | -- itself might cause freezing that makes the clause illegal. | |
2554 | ||
2555 | if Rep_Item_Too_Late (U_Ent, N, FOnly) then | |
2556 | return; | |
2557 | end if; | |
2558 | end Analyze_Attribute_Definition_Clause; | |
2559 | ||
2560 | ---------------------------- | |
2561 | -- Analyze_Code_Statement -- | |
2562 | ---------------------------- | |
2563 | ||
2564 | procedure Analyze_Code_Statement (N : Node_Id) is | |
2565 | HSS : constant Node_Id := Parent (N); | |
2566 | SBody : constant Node_Id := Parent (HSS); | |
2567 | Subp : constant Entity_Id := Current_Scope; | |
2568 | Stmt : Node_Id; | |
2569 | Decl : Node_Id; | |
2570 | StmtO : Node_Id; | |
2571 | DeclO : Node_Id; | |
2572 | ||
2573 | begin | |
2574 | -- Analyze and check we get right type, note that this implements the | |
2575 | -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that | |
2576 | -- is the only way that Asm_Insn could possibly be visible. | |
2577 | ||
2578 | Analyze_And_Resolve (Expression (N)); | |
2579 | ||
2580 | if Etype (Expression (N)) = Any_Type then | |
2581 | return; | |
2582 | elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then | |
2583 | Error_Msg_N ("incorrect type for code statement", N); | |
2584 | return; | |
2585 | end if; | |
2586 | ||
44e4341e | 2587 | Check_Code_Statement (N); |
2588 | ||
d6f39728 | 2589 | -- Make sure we appear in the handled statement sequence of a |
2590 | -- subprogram (RM 13.8(3)). | |
2591 | ||
2592 | if Nkind (HSS) /= N_Handled_Sequence_Of_Statements | |
2593 | or else Nkind (SBody) /= N_Subprogram_Body | |
2594 | then | |
2595 | Error_Msg_N | |
2596 | ("code statement can only appear in body of subprogram", N); | |
2597 | return; | |
2598 | end if; | |
2599 | ||
2600 | -- Do remaining checks (RM 13.8(3)) if not already done | |
2601 | ||
2602 | if not Is_Machine_Code_Subprogram (Subp) then | |
2603 | Set_Is_Machine_Code_Subprogram (Subp); | |
2604 | ||
2605 | -- No exception handlers allowed | |
2606 | ||
2607 | if Present (Exception_Handlers (HSS)) then | |
2608 | Error_Msg_N | |
2609 | ("exception handlers not permitted in machine code subprogram", | |
2610 | First (Exception_Handlers (HSS))); | |
2611 | end if; | |
2612 | ||
2613 | -- No declarations other than use clauses and pragmas (we allow | |
2614 | -- certain internally generated declarations as well). | |
2615 | ||
2616 | Decl := First (Declarations (SBody)); | |
2617 | while Present (Decl) loop | |
2618 | DeclO := Original_Node (Decl); | |
2619 | if Comes_From_Source (DeclO) | |
fdd294d1 | 2620 | and not Nkind_In (DeclO, N_Pragma, |
2621 | N_Use_Package_Clause, | |
2622 | N_Use_Type_Clause, | |
2623 | N_Implicit_Label_Declaration) | |
d6f39728 | 2624 | then |
2625 | Error_Msg_N | |
2626 | ("this declaration not allowed in machine code subprogram", | |
2627 | DeclO); | |
2628 | end if; | |
2629 | ||
2630 | Next (Decl); | |
2631 | end loop; | |
2632 | ||
2633 | -- No statements other than code statements, pragmas, and labels. | |
2634 | -- Again we allow certain internally generated statements. | |
2635 | ||
2636 | Stmt := First (Statements (HSS)); | |
2637 | while Present (Stmt) loop | |
2638 | StmtO := Original_Node (Stmt); | |
2639 | if Comes_From_Source (StmtO) | |
fdd294d1 | 2640 | and then not Nkind_In (StmtO, N_Pragma, |
2641 | N_Label, | |
2642 | N_Code_Statement) | |
d6f39728 | 2643 | then |
2644 | Error_Msg_N | |
2645 | ("this statement is not allowed in machine code subprogram", | |
2646 | StmtO); | |
2647 | end if; | |
2648 | ||
2649 | Next (Stmt); | |
2650 | end loop; | |
2651 | end if; | |
d6f39728 | 2652 | end Analyze_Code_Statement; |
2653 | ||
2654 | ----------------------------------------------- | |
2655 | -- Analyze_Enumeration_Representation_Clause -- | |
2656 | ----------------------------------------------- | |
2657 | ||
2658 | procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is | |
2659 | Ident : constant Node_Id := Identifier (N); | |
2660 | Aggr : constant Node_Id := Array_Aggregate (N); | |
2661 | Enumtype : Entity_Id; | |
2662 | Elit : Entity_Id; | |
2663 | Expr : Node_Id; | |
2664 | Assoc : Node_Id; | |
2665 | Choice : Node_Id; | |
2666 | Val : Uint; | |
2667 | Err : Boolean := False; | |
2668 | ||
e30c7d84 | 2669 | Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer)); |
2670 | Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer)); | |
2671 | -- Allowed range of universal integer (= allowed range of enum lit vals) | |
2672 | ||
d6f39728 | 2673 | Min : Uint; |
2674 | Max : Uint; | |
e30c7d84 | 2675 | -- Minimum and maximum values of entries |
2676 | ||
2677 | Max_Node : Node_Id; | |
2678 | -- Pointer to node for literal providing max value | |
d6f39728 | 2679 | |
2680 | begin | |
fbc67f84 | 2681 | if Ignore_Rep_Clauses then |
2682 | return; | |
2683 | end if; | |
2684 | ||
d6f39728 | 2685 | -- First some basic error checks |
2686 | ||
2687 | Find_Type (Ident); | |
2688 | Enumtype := Entity (Ident); | |
2689 | ||
2690 | if Enumtype = Any_Type | |
2691 | or else Rep_Item_Too_Early (Enumtype, N) | |
2692 | then | |
2693 | return; | |
2694 | else | |
2695 | Enumtype := Underlying_Type (Enumtype); | |
2696 | end if; | |
2697 | ||
2698 | if not Is_Enumeration_Type (Enumtype) then | |
2699 | Error_Msg_NE | |
2700 | ("enumeration type required, found}", | |
2701 | Ident, First_Subtype (Enumtype)); | |
2702 | return; | |
2703 | end if; | |
2704 | ||
9dfe12ae | 2705 | -- Ignore rep clause on generic actual type. This will already have |
2706 | -- been flagged on the template as an error, and this is the safest | |
2707 | -- way to ensure we don't get a junk cascaded message in the instance. | |
2708 | ||
2709 | if Is_Generic_Actual_Type (Enumtype) then | |
2710 | return; | |
2711 | ||
2712 | -- Type must be in current scope | |
2713 | ||
2714 | elsif Scope (Enumtype) /= Current_Scope then | |
d6f39728 | 2715 | Error_Msg_N ("type must be declared in this scope", Ident); |
2716 | return; | |
2717 | ||
9dfe12ae | 2718 | -- Type must be a first subtype |
2719 | ||
d6f39728 | 2720 | elsif not Is_First_Subtype (Enumtype) then |
2721 | Error_Msg_N ("cannot give enumeration rep clause for subtype", N); | |
2722 | return; | |
2723 | ||
9dfe12ae | 2724 | -- Ignore duplicate rep clause |
2725 | ||
d6f39728 | 2726 | elsif Has_Enumeration_Rep_Clause (Enumtype) then |
2727 | Error_Msg_N ("duplicate enumeration rep clause ignored", N); | |
2728 | return; | |
2729 | ||
7189d17f | 2730 | -- Don't allow rep clause for standard [wide_[wide_]]character |
9dfe12ae | 2731 | |
177675a7 | 2732 | elsif Is_Standard_Character_Type (Enumtype) then |
d6f39728 | 2733 | Error_Msg_N ("enumeration rep clause not allowed for this type", N); |
9dfe12ae | 2734 | return; |
2735 | ||
d9125581 | 2736 | -- Check that the expression is a proper aggregate (no parentheses) |
2737 | ||
2738 | elsif Paren_Count (Aggr) /= 0 then | |
2739 | Error_Msg | |
2740 | ("extra parentheses surrounding aggregate not allowed", | |
2741 | First_Sloc (Aggr)); | |
2742 | return; | |
2743 | ||
9dfe12ae | 2744 | -- All tests passed, so set rep clause in place |
d6f39728 | 2745 | |
2746 | else | |
2747 | Set_Has_Enumeration_Rep_Clause (Enumtype); | |
2748 | Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype)); | |
2749 | end if; | |
2750 | ||
2751 | -- Now we process the aggregate. Note that we don't use the normal | |
2752 | -- aggregate code for this purpose, because we don't want any of the | |
2753 | -- normal expansion activities, and a number of special semantic | |
2754 | -- rules apply (including the component type being any integer type) | |
2755 | ||
d6f39728 | 2756 | Elit := First_Literal (Enumtype); |
2757 | ||
2758 | -- First the positional entries if any | |
2759 | ||
2760 | if Present (Expressions (Aggr)) then | |
2761 | Expr := First (Expressions (Aggr)); | |
2762 | while Present (Expr) loop | |
2763 | if No (Elit) then | |
2764 | Error_Msg_N ("too many entries in aggregate", Expr); | |
2765 | return; | |
2766 | end if; | |
2767 | ||
2768 | Val := Static_Integer (Expr); | |
2769 | ||
d9125581 | 2770 | -- Err signals that we found some incorrect entries processing |
2771 | -- the list. The final checks for completeness and ordering are | |
2772 | -- skipped in this case. | |
2773 | ||
d6f39728 | 2774 | if Val = No_Uint then |
2775 | Err := True; | |
d6f39728 | 2776 | elsif Val < Lo or else Hi < Val then |
2777 | Error_Msg_N ("value outside permitted range", Expr); | |
2778 | Err := True; | |
2779 | end if; | |
2780 | ||
2781 | Set_Enumeration_Rep (Elit, Val); | |
2782 | Set_Enumeration_Rep_Expr (Elit, Expr); | |
2783 | Next (Expr); | |
2784 | Next (Elit); | |
2785 | end loop; | |
2786 | end if; | |
2787 | ||
2788 | -- Now process the named entries if present | |
2789 | ||
2790 | if Present (Component_Associations (Aggr)) then | |
2791 | Assoc := First (Component_Associations (Aggr)); | |
2792 | while Present (Assoc) loop | |
2793 | Choice := First (Choices (Assoc)); | |
2794 | ||
2795 | if Present (Next (Choice)) then | |
2796 | Error_Msg_N | |
2797 | ("multiple choice not allowed here", Next (Choice)); | |
2798 | Err := True; | |
2799 | end if; | |
2800 | ||
2801 | if Nkind (Choice) = N_Others_Choice then | |
2802 | Error_Msg_N ("others choice not allowed here", Choice); | |
2803 | Err := True; | |
2804 | ||
2805 | elsif Nkind (Choice) = N_Range then | |
2806 | -- ??? should allow zero/one element range here | |
2807 | Error_Msg_N ("range not allowed here", Choice); | |
2808 | Err := True; | |
2809 | ||
2810 | else | |
2811 | Analyze_And_Resolve (Choice, Enumtype); | |
2812 | ||
2813 | if Is_Entity_Name (Choice) | |
2814 | and then Is_Type (Entity (Choice)) | |
2815 | then | |
2816 | Error_Msg_N ("subtype name not allowed here", Choice); | |
2817 | Err := True; | |
2818 | -- ??? should allow static subtype with zero/one entry | |
2819 | ||
2820 | elsif Etype (Choice) = Base_Type (Enumtype) then | |
2821 | if not Is_Static_Expression (Choice) then | |
9dfe12ae | 2822 | Flag_Non_Static_Expr |
2823 | ("non-static expression used for choice!", Choice); | |
d6f39728 | 2824 | Err := True; |
2825 | ||
2826 | else | |
2827 | Elit := Expr_Value_E (Choice); | |
2828 | ||
2829 | if Present (Enumeration_Rep_Expr (Elit)) then | |
2830 | Error_Msg_Sloc := Sloc (Enumeration_Rep_Expr (Elit)); | |
2831 | Error_Msg_NE | |
2832 | ("representation for& previously given#", | |
2833 | Choice, Elit); | |
2834 | Err := True; | |
2835 | end if; | |
2836 | ||
e30c7d84 | 2837 | Set_Enumeration_Rep_Expr (Elit, Expression (Assoc)); |
d6f39728 | 2838 | |
2839 | Expr := Expression (Assoc); | |
2840 | Val := Static_Integer (Expr); | |
2841 | ||
2842 | if Val = No_Uint then | |
2843 | Err := True; | |
2844 | ||
2845 | elsif Val < Lo or else Hi < Val then | |
2846 | Error_Msg_N ("value outside permitted range", Expr); | |
2847 | Err := True; | |
2848 | end if; | |
2849 | ||
2850 | Set_Enumeration_Rep (Elit, Val); | |
2851 | end if; | |
2852 | end if; | |
2853 | end if; | |
2854 | ||
2855 | Next (Assoc); | |
2856 | end loop; | |
2857 | end if; | |
2858 | ||
2859 | -- Aggregate is fully processed. Now we check that a full set of | |
2860 | -- representations was given, and that they are in range and in order. | |
2861 | -- These checks are only done if no other errors occurred. | |
2862 | ||
2863 | if not Err then | |
2864 | Min := No_Uint; | |
2865 | Max := No_Uint; | |
2866 | ||
2867 | Elit := First_Literal (Enumtype); | |
2868 | while Present (Elit) loop | |
2869 | if No (Enumeration_Rep_Expr (Elit)) then | |
2870 | Error_Msg_NE ("missing representation for&!", N, Elit); | |
2871 | ||
2872 | else | |
2873 | Val := Enumeration_Rep (Elit); | |
2874 | ||
2875 | if Min = No_Uint then | |
2876 | Min := Val; | |
2877 | end if; | |
2878 | ||
2879 | if Val /= No_Uint then | |
2880 | if Max /= No_Uint and then Val <= Max then | |
2881 | Error_Msg_NE | |
2882 | ("enumeration value for& not ordered!", | |
e30c7d84 | 2883 | Enumeration_Rep_Expr (Elit), Elit); |
d6f39728 | 2884 | end if; |
2885 | ||
e30c7d84 | 2886 | Max_Node := Enumeration_Rep_Expr (Elit); |
d6f39728 | 2887 | Max := Val; |
2888 | end if; | |
2889 | ||
e30c7d84 | 2890 | -- If there is at least one literal whose representation is not |
2891 | -- equal to the Pos value, then note that this enumeration type | |
2892 | -- has a non-standard representation. | |
d6f39728 | 2893 | |
2894 | if Val /= Enumeration_Pos (Elit) then | |
2895 | Set_Has_Non_Standard_Rep (Base_Type (Enumtype)); | |
2896 | end if; | |
2897 | end if; | |
2898 | ||
2899 | Next (Elit); | |
2900 | end loop; | |
2901 | ||
2902 | -- Now set proper size information | |
2903 | ||
2904 | declare | |
2905 | Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype)); | |
2906 | ||
2907 | begin | |
2908 | if Has_Size_Clause (Enumtype) then | |
e30c7d84 | 2909 | |
2910 | -- All OK, if size is OK now | |
2911 | ||
2912 | if RM_Size (Enumtype) >= Minsize then | |
d6f39728 | 2913 | null; |
2914 | ||
2915 | else | |
e30c7d84 | 2916 | -- Try if we can get by with biasing |
2917 | ||
d6f39728 | 2918 | Minsize := |
2919 | UI_From_Int (Minimum_Size (Enumtype, Biased => True)); | |
2920 | ||
e30c7d84 | 2921 | -- Error message if even biasing does not work |
2922 | ||
2923 | if RM_Size (Enumtype) < Minsize then | |
2924 | Error_Msg_Uint_1 := RM_Size (Enumtype); | |
2925 | Error_Msg_Uint_2 := Max; | |
2926 | Error_Msg_N | |
2927 | ("previously given size (^) is too small " | |
2928 | & "for this value (^)", Max_Node); | |
2929 | ||
2930 | -- If biasing worked, indicate that we now have biased rep | |
d6f39728 | 2931 | |
2932 | else | |
b77e4501 | 2933 | Set_Biased |
2934 | (Enumtype, Size_Clause (Enumtype), "size clause"); | |
d6f39728 | 2935 | end if; |
2936 | end if; | |
2937 | ||
2938 | else | |
2939 | Set_RM_Size (Enumtype, Minsize); | |
2940 | Set_Enum_Esize (Enumtype); | |
2941 | end if; | |
2942 | ||
2943 | Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype)); | |
2944 | Set_Esize (Base_Type (Enumtype), Esize (Enumtype)); | |
2945 | Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype)); | |
2946 | end; | |
2947 | end if; | |
2948 | ||
2949 | -- We repeat the too late test in case it froze itself! | |
2950 | ||
2951 | if Rep_Item_Too_Late (Enumtype, N) then | |
2952 | null; | |
2953 | end if; | |
d6f39728 | 2954 | end Analyze_Enumeration_Representation_Clause; |
2955 | ||
2956 | ---------------------------- | |
2957 | -- Analyze_Free_Statement -- | |
2958 | ---------------------------- | |
2959 | ||
2960 | procedure Analyze_Free_Statement (N : Node_Id) is | |
2961 | begin | |
2962 | Analyze (Expression (N)); | |
2963 | end Analyze_Free_Statement; | |
2964 | ||
40ca69b9 | 2965 | --------------------------- |
2966 | -- Analyze_Freeze_Entity -- | |
2967 | --------------------------- | |
2968 | ||
2969 | procedure Analyze_Freeze_Entity (N : Node_Id) is | |
2970 | E : constant Entity_Id := Entity (N); | |
2971 | ||
2972 | begin | |
98f7db28 | 2973 | -- Remember that we are processing a freezing entity. Required to |
2974 | -- ensure correct decoration of internal entities associated with | |
2975 | -- interfaces (see New_Overloaded_Entity). | |
2976 | ||
2977 | Inside_Freezing_Actions := Inside_Freezing_Actions + 1; | |
2978 | ||
40ca69b9 | 2979 | -- For tagged types covering interfaces add internal entities that link |
2980 | -- the primitives of the interfaces with the primitives that cover them. | |
40ca69b9 | 2981 | -- Note: These entities were originally generated only when generating |
2982 | -- code because their main purpose was to provide support to initialize | |
2983 | -- the secondary dispatch tables. They are now generated also when | |
2984 | -- compiling with no code generation to provide ASIS the relationship | |
c8da6114 | 2985 | -- between interface primitives and tagged type primitives. They are |
2986 | -- also used to locate primitives covering interfaces when processing | |
2987 | -- generics (see Derive_Subprograms). | |
40ca69b9 | 2988 | |
de54c5ab | 2989 | if Ada_Version >= Ada_2005 |
40ca69b9 | 2990 | and then Ekind (E) = E_Record_Type |
2991 | and then Is_Tagged_Type (E) | |
2992 | and then not Is_Interface (E) | |
2993 | and then Has_Interfaces (E) | |
2994 | then | |
c8da6114 | 2995 | -- This would be a good common place to call the routine that checks |
2996 | -- overriding of interface primitives (and thus factorize calls to | |
2997 | -- Check_Abstract_Overriding located at different contexts in the | |
2998 | -- compiler). However, this is not possible because it causes | |
2999 | -- spurious errors in case of late overriding. | |
3000 | ||
40ca69b9 | 3001 | Add_Internal_Interface_Entities (E); |
3002 | end if; | |
d00681a7 | 3003 | |
3004 | -- Check CPP types | |
3005 | ||
3006 | if Ekind (E) = E_Record_Type | |
3007 | and then Is_CPP_Class (E) | |
3008 | and then Is_Tagged_Type (E) | |
3009 | and then Tagged_Type_Expansion | |
3010 | and then Expander_Active | |
3011 | then | |
3012 | if CPP_Num_Prims (E) = 0 then | |
3013 | ||
3014 | -- If the CPP type has user defined components then it must import | |
3015 | -- primitives from C++. This is required because if the C++ class | |
3016 | -- has no primitives then the C++ compiler does not added the _tag | |
3017 | -- component to the type. | |
3018 | ||
3019 | pragma Assert (Chars (First_Entity (E)) = Name_uTag); | |
3020 | ||
3021 | if First_Entity (E) /= Last_Entity (E) then | |
3022 | Error_Msg_N | |
3023 | ("?'C'P'P type must import at least one primitive from C++", | |
3024 | E); | |
3025 | end if; | |
3026 | end if; | |
3027 | ||
3028 | -- Check that all its primitives are abstract or imported from C++. | |
3029 | -- Check also availability of the C++ constructor. | |
3030 | ||
3031 | declare | |
3032 | Has_Constructors : constant Boolean := Has_CPP_Constructors (E); | |
3033 | Elmt : Elmt_Id; | |
3034 | Error_Reported : Boolean := False; | |
3035 | Prim : Node_Id; | |
3036 | ||
3037 | begin | |
3038 | Elmt := First_Elmt (Primitive_Operations (E)); | |
3039 | while Present (Elmt) loop | |
3040 | Prim := Node (Elmt); | |
3041 | ||
3042 | if Comes_From_Source (Prim) then | |
3043 | if Is_Abstract_Subprogram (Prim) then | |
3044 | null; | |
3045 | ||
3046 | elsif not Is_Imported (Prim) | |
3047 | or else Convention (Prim) /= Convention_CPP | |
3048 | then | |
3049 | Error_Msg_N | |
3050 | ("?primitives of 'C'P'P types must be imported from C++" | |
3051 | & " or abstract", Prim); | |
3052 | ||
3053 | elsif not Has_Constructors | |
3054 | and then not Error_Reported | |
3055 | then | |
3056 | Error_Msg_Name_1 := Chars (E); | |
3057 | Error_Msg_N | |
3058 | ("?'C'P'P constructor required for type %", Prim); | |
3059 | Error_Reported := True; | |
3060 | end if; | |
3061 | end if; | |
3062 | ||
3063 | Next_Elmt (Elmt); | |
3064 | end loop; | |
3065 | end; | |
3066 | end if; | |
98f7db28 | 3067 | |
3068 | Inside_Freezing_Actions := Inside_Freezing_Actions - 1; | |
9dc88aea | 3069 | |
3070 | -- If we have a type with predicates, build predicate function | |
3071 | ||
3072 | if Is_Type (E) and then Has_Predicates (E) then | |
3073 | declare | |
3074 | FDecl : Node_Id; | |
3075 | FBody : Node_Id; | |
3076 | ||
3077 | begin | |
3078 | Build_Predicate_Function (E, FDecl, FBody); | |
3079 | ||
3080 | if Present (FDecl) then | |
3081 | Insert_After (N, FBody); | |
3082 | Insert_After (N, FDecl); | |
3083 | end if; | |
3084 | end; | |
3085 | end if; | |
40ca69b9 | 3086 | end Analyze_Freeze_Entity; |
3087 | ||
d6f39728 | 3088 | ------------------------------------------ |
3089 | -- Analyze_Record_Representation_Clause -- | |
3090 | ------------------------------------------ | |
3091 | ||
67278d60 | 3092 | -- Note: we check as much as we can here, but we can't do any checks |
3093 | -- based on the position values (e.g. overlap checks) until freeze time | |
3094 | -- because especially in Ada 2005 (machine scalar mode), the processing | |
3095 | -- for non-standard bit order can substantially change the positions. | |
3096 | -- See procedure Check_Record_Representation_Clause (called from Freeze) | |
3097 | -- for the remainder of this processing. | |
3098 | ||
d6f39728 | 3099 | procedure Analyze_Record_Representation_Clause (N : Node_Id) is |
7800b920 | 3100 | Ident : constant Node_Id := Identifier (N); |
3101 | Biased : Boolean; | |
d6f39728 | 3102 | CC : Node_Id; |
7800b920 | 3103 | Comp : Entity_Id; |
d6f39728 | 3104 | Fbit : Uint; |
d6f39728 | 3105 | Hbit : Uint := Uint_0; |
7800b920 | 3106 | Lbit : Uint; |
d6f39728 | 3107 | Ocomp : Entity_Id; |
7800b920 | 3108 | Posit : Uint; |
3109 | Rectype : Entity_Id; | |
d6f39728 | 3110 | |
639e37b0 | 3111 | CR_Pragma : Node_Id := Empty; |
3112 | -- Points to N_Pragma node if Complete_Representation pragma present | |
3113 | ||
d6f39728 | 3114 | begin |
fbc67f84 | 3115 | if Ignore_Rep_Clauses then |
3116 | return; | |
3117 | end if; | |
3118 | ||
d6f39728 | 3119 | Find_Type (Ident); |
3120 | Rectype := Entity (Ident); | |
3121 | ||
3122 | if Rectype = Any_Type | |
3123 | or else Rep_Item_Too_Early (Rectype, N) | |
3124 | then | |
3125 | return; | |
3126 | else | |
3127 | Rectype := Underlying_Type (Rectype); | |
3128 | end if; | |
3129 | ||
3130 | -- First some basic error checks | |
3131 | ||
3132 | if not Is_Record_Type (Rectype) then | |
3133 | Error_Msg_NE | |
3134 | ("record type required, found}", Ident, First_Subtype (Rectype)); | |
3135 | return; | |
3136 | ||
d6f39728 | 3137 | elsif Scope (Rectype) /= Current_Scope then |
3138 | Error_Msg_N ("type must be declared in this scope", N); | |
3139 | return; | |
3140 | ||
3141 | elsif not Is_First_Subtype (Rectype) then | |
3142 | Error_Msg_N ("cannot give record rep clause for subtype", N); | |
3143 | return; | |
3144 | ||
3145 | elsif Has_Record_Rep_Clause (Rectype) then | |
3146 | Error_Msg_N ("duplicate record rep clause ignored", N); | |
3147 | return; | |
3148 | ||
3149 | elsif Rep_Item_Too_Late (Rectype, N) then | |
3150 | return; | |
3151 | end if; | |
3152 | ||
3153 | if Present (Mod_Clause (N)) then | |
3154 | declare | |
3155 | Loc : constant Source_Ptr := Sloc (N); | |
3156 | M : constant Node_Id := Mod_Clause (N); | |
3157 | P : constant List_Id := Pragmas_Before (M); | |
d6f39728 | 3158 | AtM_Nod : Node_Id; |
3159 | ||
9dfe12ae | 3160 | Mod_Val : Uint; |
3161 | pragma Warnings (Off, Mod_Val); | |
3162 | ||
d6f39728 | 3163 | begin |
e0521a36 | 3164 | Check_Restriction (No_Obsolescent_Features, Mod_Clause (N)); |
3165 | ||
9dfe12ae | 3166 | if Warn_On_Obsolescent_Feature then |
3167 | Error_Msg_N | |
fbc67f84 | 3168 | ("mod clause is an obsolescent feature (RM J.8)?", N); |
9dfe12ae | 3169 | Error_Msg_N |
d53a018a | 3170 | ("\use alignment attribute definition clause instead?", N); |
9dfe12ae | 3171 | end if; |
3172 | ||
d6f39728 | 3173 | if Present (P) then |
3174 | Analyze_List (P); | |
3175 | end if; | |
3176 | ||
fbc67f84 | 3177 | -- In ASIS_Mode mode, expansion is disabled, but we must convert |
3178 | -- the Mod clause into an alignment clause anyway, so that the | |
3179 | -- back-end can compute and back-annotate properly the size and | |
3180 | -- alignment of types that may include this record. | |
d6f39728 | 3181 | |
15ebb600 | 3182 | -- This seems dubious, this destroys the source tree in a manner |
3183 | -- not detectable by ASIS ??? | |
3184 | ||
d6f39728 | 3185 | if Operating_Mode = Check_Semantics |
9dfe12ae | 3186 | and then ASIS_Mode |
d6f39728 | 3187 | then |
3188 | AtM_Nod := | |
3189 | Make_Attribute_Definition_Clause (Loc, | |
3190 | Name => New_Reference_To (Base_Type (Rectype), Loc), | |
3191 | Chars => Name_Alignment, | |
3192 | Expression => Relocate_Node (Expression (M))); | |
3193 | ||
3194 | Set_From_At_Mod (AtM_Nod); | |
3195 | Insert_After (N, AtM_Nod); | |
3196 | Mod_Val := Get_Alignment_Value (Expression (AtM_Nod)); | |
3197 | Set_Mod_Clause (N, Empty); | |
3198 | ||
3199 | else | |
3200 | -- Get the alignment value to perform error checking | |
3201 | ||
3202 | Mod_Val := Get_Alignment_Value (Expression (M)); | |
d6f39728 | 3203 | end if; |
3204 | end; | |
3205 | end if; | |
3206 | ||
3062c401 | 3207 | -- For untagged types, clear any existing component clauses for the |
3208 | -- type. If the type is derived, this is what allows us to override | |
3209 | -- a rep clause for the parent. For type extensions, the representation | |
3210 | -- of the inherited components is inherited, so we want to keep previous | |
3211 | -- component clauses for completeness. | |
d6f39728 | 3212 | |
3062c401 | 3213 | if not Is_Tagged_Type (Rectype) then |
3214 | Comp := First_Component_Or_Discriminant (Rectype); | |
3215 | while Present (Comp) loop | |
3216 | Set_Component_Clause (Comp, Empty); | |
3217 | Next_Component_Or_Discriminant (Comp); | |
3218 | end loop; | |
3219 | end if; | |
d6f39728 | 3220 | |
3221 | -- All done if no component clauses | |
3222 | ||
3223 | CC := First (Component_Clauses (N)); | |
3224 | ||
3225 | if No (CC) then | |
3226 | return; | |
3227 | end if; | |
3228 | ||
f15731c4 | 3229 | -- A representation like this applies to the base type |
d6f39728 | 3230 | |
3231 | Set_Has_Record_Rep_Clause (Base_Type (Rectype)); | |
3232 | Set_Has_Non_Standard_Rep (Base_Type (Rectype)); | |
3233 | Set_Has_Specified_Layout (Base_Type (Rectype)); | |
3234 | ||
d6f39728 | 3235 | -- Process the component clauses |
3236 | ||
3237 | while Present (CC) loop | |
3238 | ||
639e37b0 | 3239 | -- Pragma |
d6f39728 | 3240 | |
3241 | if Nkind (CC) = N_Pragma then | |
3242 | Analyze (CC); | |
3243 | ||
639e37b0 | 3244 | -- The only pragma of interest is Complete_Representation |
3245 | ||
fdd294d1 | 3246 | if Pragma_Name (CC) = Name_Complete_Representation then |
639e37b0 | 3247 | CR_Pragma := CC; |
3248 | end if; | |
3249 | ||
d6f39728 | 3250 | -- Processing for real component clause |
3251 | ||
3252 | else | |
d6f39728 | 3253 | Posit := Static_Integer (Position (CC)); |
3254 | Fbit := Static_Integer (First_Bit (CC)); | |
3255 | Lbit := Static_Integer (Last_Bit (CC)); | |
3256 | ||
3257 | if Posit /= No_Uint | |
3258 | and then Fbit /= No_Uint | |
3259 | and then Lbit /= No_Uint | |
3260 | then | |
3261 | if Posit < 0 then | |
3262 | Error_Msg_N | |
3263 | ("position cannot be negative", Position (CC)); | |
3264 | ||
3265 | elsif Fbit < 0 then | |
3266 | Error_Msg_N | |
3267 | ("first bit cannot be negative", First_Bit (CC)); | |
3268 | ||
177675a7 | 3269 | -- The Last_Bit specified in a component clause must not be |
3270 | -- less than the First_Bit minus one (RM-13.5.1(10)). | |
3271 | ||
3272 | elsif Lbit < Fbit - 1 then | |
3273 | Error_Msg_N | |
3274 | ("last bit cannot be less than first bit minus one", | |
3275 | Last_Bit (CC)); | |
3276 | ||
d6f39728 | 3277 | -- Values look OK, so find the corresponding record component |
3278 | -- Even though the syntax allows an attribute reference for | |
3279 | -- implementation-defined components, GNAT does not allow the | |
3280 | -- tag to get an explicit position. | |
3281 | ||
3282 | elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then | |
d6f39728 | 3283 | if Attribute_Name (Component_Name (CC)) = Name_Tag then |
3284 | Error_Msg_N ("position of tag cannot be specified", CC); | |
3285 | else | |
3286 | Error_Msg_N ("illegal component name", CC); | |
3287 | end if; | |
3288 | ||
3289 | else | |
3290 | Comp := First_Entity (Rectype); | |
3291 | while Present (Comp) loop | |
3292 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
3293 | Next_Entity (Comp); | |
3294 | end loop; | |
3295 | ||
3296 | if No (Comp) then | |
3297 | ||
3298 | -- Maybe component of base type that is absent from | |
3299 | -- statically constrained first subtype. | |
3300 | ||
3301 | Comp := First_Entity (Base_Type (Rectype)); | |
3302 | while Present (Comp) loop | |
3303 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
3304 | Next_Entity (Comp); | |
3305 | end loop; | |
3306 | end if; | |
3307 | ||
3308 | if No (Comp) then | |
3309 | Error_Msg_N | |
3310 | ("component clause is for non-existent field", CC); | |
3311 | ||
7800b920 | 3312 | -- Ada 2012 (AI05-0026): Any name that denotes a |
3313 | -- discriminant of an object of an unchecked union type | |
3314 | -- shall not occur within a record_representation_clause. | |
3315 | ||
3316 | -- The general restriction of using record rep clauses on | |
3317 | -- Unchecked_Union types has now been lifted. Since it is | |
3318 | -- possible to introduce a record rep clause which mentions | |
3319 | -- the discriminant of an Unchecked_Union in non-Ada 2012 | |
3320 | -- code, this check is applied to all versions of the | |
3321 | -- language. | |
3322 | ||
3323 | elsif Ekind (Comp) = E_Discriminant | |
3324 | and then Is_Unchecked_Union (Rectype) | |
3325 | then | |
3326 | Error_Msg_N | |
3327 | ("cannot reference discriminant of Unchecked_Union", | |
3328 | Component_Name (CC)); | |
3329 | ||
d6f39728 | 3330 | elsif Present (Component_Clause (Comp)) then |
3062c401 | 3331 | |
1a34e48c | 3332 | -- Diagnose duplicate rep clause, or check consistency |
fdd294d1 | 3333 | -- if this is an inherited component. In a double fault, |
3062c401 | 3334 | -- there may be a duplicate inconsistent clause for an |
3335 | -- inherited component. | |
3336 | ||
fdd294d1 | 3337 | if Scope (Original_Record_Component (Comp)) = Rectype |
3338 | or else Parent (Component_Clause (Comp)) = N | |
3062c401 | 3339 | then |
3340 | Error_Msg_Sloc := Sloc (Component_Clause (Comp)); | |
3341 | Error_Msg_N ("component clause previously given#", CC); | |
3342 | ||
3343 | else | |
3344 | declare | |
3345 | Rep1 : constant Node_Id := Component_Clause (Comp); | |
3062c401 | 3346 | begin |
3347 | if Intval (Position (Rep1)) /= | |
3348 | Intval (Position (CC)) | |
3349 | or else Intval (First_Bit (Rep1)) /= | |
3350 | Intval (First_Bit (CC)) | |
3351 | or else Intval (Last_Bit (Rep1)) /= | |
3352 | Intval (Last_Bit (CC)) | |
3353 | then | |
3354 | Error_Msg_N ("component clause inconsistent " | |
3355 | & "with representation of ancestor", CC); | |
3062c401 | 3356 | elsif Warn_On_Redundant_Constructs then |
3357 | Error_Msg_N ("?redundant component clause " | |
3358 | & "for inherited component!", CC); | |
3359 | end if; | |
3360 | end; | |
3361 | end if; | |
d6f39728 | 3362 | |
d2b860b4 | 3363 | -- Normal case where this is the first component clause we |
3364 | -- have seen for this entity, so set it up properly. | |
3365 | ||
d6f39728 | 3366 | else |
83f8f0a6 | 3367 | -- Make reference for field in record rep clause and set |
3368 | -- appropriate entity field in the field identifier. | |
3369 | ||
3370 | Generate_Reference | |
3371 | (Comp, Component_Name (CC), Set_Ref => False); | |
3372 | Set_Entity (Component_Name (CC), Comp); | |
3373 | ||
2866d595 | 3374 | -- Update Fbit and Lbit to the actual bit number |
d6f39728 | 3375 | |
3376 | Fbit := Fbit + UI_From_Int (SSU) * Posit; | |
3377 | Lbit := Lbit + UI_From_Int (SSU) * Posit; | |
3378 | ||
d6f39728 | 3379 | if Has_Size_Clause (Rectype) |
3380 | and then Esize (Rectype) <= Lbit | |
3381 | then | |
3382 | Error_Msg_N | |
3383 | ("bit number out of range of specified size", | |
3384 | Last_Bit (CC)); | |
3385 | else | |
3386 | Set_Component_Clause (Comp, CC); | |
3387 | Set_Component_Bit_Offset (Comp, Fbit); | |
3388 | Set_Esize (Comp, 1 + (Lbit - Fbit)); | |
3389 | Set_Normalized_First_Bit (Comp, Fbit mod SSU); | |
3390 | Set_Normalized_Position (Comp, Fbit / SSU); | |
3391 | ||
a0fc8c5b | 3392 | if Warn_On_Overridden_Size |
3393 | and then Has_Size_Clause (Etype (Comp)) | |
3394 | and then RM_Size (Etype (Comp)) /= Esize (Comp) | |
3395 | then | |
3396 | Error_Msg_NE | |
3397 | ("?component size overrides size clause for&", | |
3398 | Component_Name (CC), Etype (Comp)); | |
3399 | end if; | |
3400 | ||
ea61a7ea | 3401 | -- This information is also set in the corresponding |
3402 | -- component of the base type, found by accessing the | |
3403 | -- Original_Record_Component link if it is present. | |
d6f39728 | 3404 | |
3405 | Ocomp := Original_Record_Component (Comp); | |
3406 | ||
3407 | if Hbit < Lbit then | |
3408 | Hbit := Lbit; | |
3409 | end if; | |
3410 | ||
3411 | Check_Size | |
3412 | (Component_Name (CC), | |
3413 | Etype (Comp), | |
3414 | Esize (Comp), | |
3415 | Biased); | |
3416 | ||
b77e4501 | 3417 | Set_Biased |
3418 | (Comp, First_Node (CC), "component clause", Biased); | |
cc46ff4b | 3419 | |
d6f39728 | 3420 | if Present (Ocomp) then |
3421 | Set_Component_Clause (Ocomp, CC); | |
3422 | Set_Component_Bit_Offset (Ocomp, Fbit); | |
3423 | Set_Normalized_First_Bit (Ocomp, Fbit mod SSU); | |
3424 | Set_Normalized_Position (Ocomp, Fbit / SSU); | |
3425 | Set_Esize (Ocomp, 1 + (Lbit - Fbit)); | |
3426 | ||
3427 | Set_Normalized_Position_Max | |
3428 | (Ocomp, Normalized_Position (Ocomp)); | |
3429 | ||
b77e4501 | 3430 | -- Note: we don't use Set_Biased here, because we |
3431 | -- already gave a warning above if needed, and we | |
3432 | -- would get a duplicate for the same name here. | |
3433 | ||
d6f39728 | 3434 | Set_Has_Biased_Representation |
3435 | (Ocomp, Has_Biased_Representation (Comp)); | |
3436 | end if; | |
3437 | ||
3438 | if Esize (Comp) < 0 then | |
3439 | Error_Msg_N ("component size is negative", CC); | |
3440 | end if; | |
3441 | end if; | |
3442 | end if; | |
3443 | end if; | |
3444 | end if; | |
3445 | end if; | |
3446 | ||
3447 | Next (CC); | |
3448 | end loop; | |
3449 | ||
67278d60 | 3450 | -- Check missing components if Complete_Representation pragma appeared |
d6f39728 | 3451 | |
67278d60 | 3452 | if Present (CR_Pragma) then |
3453 | Comp := First_Component_Or_Discriminant (Rectype); | |
3454 | while Present (Comp) loop | |
3455 | if No (Component_Clause (Comp)) then | |
3456 | Error_Msg_NE | |
3457 | ("missing component clause for &", CR_Pragma, Comp); | |
3458 | end if; | |
d6f39728 | 3459 | |
67278d60 | 3460 | Next_Component_Or_Discriminant (Comp); |
3461 | end loop; | |
d6f39728 | 3462 | |
67278d60 | 3463 | -- If no Complete_Representation pragma, warn if missing components |
15ebb600 | 3464 | |
fdd294d1 | 3465 | elsif Warn_On_Unrepped_Components then |
15ebb600 | 3466 | declare |
3467 | Num_Repped_Components : Nat := 0; | |
3468 | Num_Unrepped_Components : Nat := 0; | |
3469 | ||
3470 | begin | |
3471 | -- First count number of repped and unrepped components | |
3472 | ||
3473 | Comp := First_Component_Or_Discriminant (Rectype); | |
3474 | while Present (Comp) loop | |
3475 | if Present (Component_Clause (Comp)) then | |
3476 | Num_Repped_Components := Num_Repped_Components + 1; | |
3477 | else | |
3478 | Num_Unrepped_Components := Num_Unrepped_Components + 1; | |
3479 | end if; | |
3480 | ||
3481 | Next_Component_Or_Discriminant (Comp); | |
3482 | end loop; | |
3483 | ||
3484 | -- We are only interested in the case where there is at least one | |
3485 | -- unrepped component, and at least half the components have rep | |
3486 | -- clauses. We figure that if less than half have them, then the | |
87f9eef5 | 3487 | -- partial rep clause is really intentional. If the component |
3488 | -- type has no underlying type set at this point (as for a generic | |
3489 | -- formal type), we don't know enough to give a warning on the | |
3490 | -- component. | |
15ebb600 | 3491 | |
3492 | if Num_Unrepped_Components > 0 | |
3493 | and then Num_Unrepped_Components < Num_Repped_Components | |
3494 | then | |
3495 | Comp := First_Component_Or_Discriminant (Rectype); | |
3496 | while Present (Comp) loop | |
83f8f0a6 | 3497 | if No (Component_Clause (Comp)) |
3062c401 | 3498 | and then Comes_From_Source (Comp) |
87f9eef5 | 3499 | and then Present (Underlying_Type (Etype (Comp))) |
83f8f0a6 | 3500 | and then (Is_Scalar_Type (Underlying_Type (Etype (Comp))) |
67278d60 | 3501 | or else Size_Known_At_Compile_Time |
3502 | (Underlying_Type (Etype (Comp)))) | |
fdd294d1 | 3503 | and then not Has_Warnings_Off (Rectype) |
83f8f0a6 | 3504 | then |
15ebb600 | 3505 | Error_Msg_Sloc := Sloc (Comp); |
3506 | Error_Msg_NE | |
3507 | ("?no component clause given for & declared #", | |
3508 | N, Comp); | |
3509 | end if; | |
3510 | ||
3511 | Next_Component_Or_Discriminant (Comp); | |
3512 | end loop; | |
3513 | end if; | |
3514 | end; | |
d6f39728 | 3515 | end if; |
d6f39728 | 3516 | end Analyze_Record_Representation_Clause; |
3517 | ||
5b5df4a9 | 3518 | ------------------------------- |
3519 | -- Build_Invariant_Procedure -- | |
3520 | ------------------------------- | |
3521 | ||
3522 | -- The procedure that is constructed here has the form | |
3523 | ||
3524 | -- procedure typInvariant (Ixxx : typ) is | |
3525 | -- begin | |
3526 | -- pragma Check (Invariant, exp, "failed invariant from xxx"); | |
3527 | -- pragma Check (Invariant, exp, "failed invariant from xxx"); | |
3528 | -- ... | |
3529 | -- pragma Check (Invariant, exp, "failed inherited invariant from xxx"); | |
3530 | -- ... | |
3531 | -- end typInvariant; | |
3532 | ||
3533 | procedure Build_Invariant_Procedure | |
3534 | (Typ : Entity_Id; | |
3535 | PDecl : out Node_Id; | |
3536 | PBody : out Node_Id) | |
3537 | is | |
3538 | Loc : constant Source_Ptr := Sloc (Typ); | |
3539 | Stmts : List_Id; | |
3540 | Spec : Node_Id; | |
3541 | SId : Entity_Id; | |
3542 | ||
3543 | procedure Add_Invariants (T : Entity_Id; Inherit : Boolean); | |
3544 | -- Appends statements to Stmts for any invariants in the rep item chain | |
3545 | -- of the given type. If Inherit is False, then we only process entries | |
3546 | -- on the chain for the type Typ. If Inherit is True, then we ignore any | |
3547 | -- Invariant aspects, but we process all Invariant'Class aspects, adding | |
3548 | -- "inherited" to the exception message and generating an informational | |
3549 | -- message about the inheritance of an invariant. | |
3550 | ||
3551 | Object_Name : constant Name_Id := New_Internal_Name ('I'); | |
3552 | -- Name for argument of invariant procedure | |
3553 | ||
3554 | -------------------- | |
3555 | -- Add_Invariants -- | |
3556 | -------------------- | |
3557 | ||
3558 | procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is | |
3559 | Ritem : Node_Id; | |
3560 | Arg1 : Node_Id; | |
3561 | Arg2 : Node_Id; | |
3562 | Arg3 : Node_Id; | |
3563 | Exp : Node_Id; | |
3564 | Loc : Source_Ptr; | |
3565 | Assoc : List_Id; | |
3566 | Str : String_Id; | |
3567 | ||
3568 | function Replace_Node (N : Node_Id) return Traverse_Result; | |
3569 | -- Process single node for traversal to replace type references | |
3570 | ||
3571 | procedure Replace_Type is new Traverse_Proc (Replace_Node); | |
3572 | -- Traverse an expression changing every occurrence of an entity | |
3573 | -- reference to type T with a reference to the object argument. | |
3574 | ||
3575 | ------------------ | |
3576 | -- Replace_Node -- | |
3577 | ------------------ | |
3578 | ||
3579 | function Replace_Node (N : Node_Id) return Traverse_Result is | |
3580 | begin | |
3581 | -- Case of entity name referencing the type | |
3582 | ||
3583 | if Is_Entity_Name (N) | |
3584 | and then Entity (N) = T | |
3585 | then | |
3586 | -- Invariant'Class, replace with T'Class (obj) | |
3587 | ||
3588 | if Class_Present (Ritem) then | |
3589 | Rewrite (N, | |
3590 | Make_Type_Conversion (Loc, | |
3591 | Subtype_Mark => | |
3592 | Make_Attribute_Reference (Loc, | |
3593 | Prefix => | |
3594 | New_Occurrence_Of (T, Loc), | |
3595 | Attribute_Name => Name_Class), | |
3596 | Expression => | |
3597 | Make_Identifier (Loc, | |
3598 | Chars => Object_Name))); | |
3599 | ||
3600 | -- Invariant, replace with obj | |
3601 | ||
3602 | else | |
3603 | Rewrite (N, | |
3604 | Make_Identifier (Loc, | |
3605 | Chars => Object_Name)); | |
3606 | end if; | |
3607 | ||
3608 | -- All done with this node | |
3609 | ||
3610 | return Skip; | |
3611 | ||
3612 | -- Not an instance of the type entity, keep going | |
3613 | ||
3614 | else | |
3615 | return OK; | |
3616 | end if; | |
3617 | end Replace_Node; | |
3618 | ||
3619 | -- Start of processing for Add_Invariants | |
3620 | ||
3621 | begin | |
3622 | Ritem := First_Rep_Item (T); | |
3623 | while Present (Ritem) loop | |
3624 | if Nkind (Ritem) = N_Pragma | |
3625 | and then Pragma_Name (Ritem) = Name_Invariant | |
3626 | then | |
3627 | Arg1 := First (Pragma_Argument_Associations (Ritem)); | |
3628 | Arg2 := Next (Arg1); | |
3629 | Arg3 := Next (Arg2); | |
3630 | ||
3631 | Arg1 := Get_Pragma_Arg (Arg1); | |
3632 | Arg2 := Get_Pragma_Arg (Arg2); | |
3633 | ||
3634 | -- For Inherit case, ignore Invariant, process only Class case | |
3635 | ||
3636 | if Inherit then | |
3637 | if not Class_Present (Ritem) then | |
3638 | goto Continue; | |
3639 | end if; | |
3640 | ||
3641 | -- For Inherit false, process only item for right type | |
3642 | ||
3643 | else | |
3644 | if Entity (Arg1) /= Typ then | |
3645 | goto Continue; | |
3646 | end if; | |
3647 | end if; | |
3648 | ||
3649 | if No (Stmts) then | |
3650 | Stmts := Empty_List; | |
3651 | end if; | |
3652 | ||
3653 | Exp := New_Copy_Tree (Arg2); | |
3654 | Loc := Sloc (Exp); | |
3655 | ||
3656 | -- We need to replace any occurrences of the name of the type | |
3657 | -- with references to the object, converted to type'Class in | |
3658 | -- the case of Invariant'Class aspects. We do this by first | |
3659 | -- doing a preanalysis, to identify all the entities, then | |
3660 | -- we traverse looking for the type entity, and doing the | |
3661 | -- necessary substitution. The preanalysis is done with the | |
3662 | -- special OK_To_Reference flag set on the type, so that if | |
3663 | -- we get an occurrence of this type, it will be reognized | |
3664 | -- as legitimate. | |
3665 | ||
3666 | Set_OK_To_Reference (T, True); | |
3667 | Preanalyze_Spec_Expression (Exp, Standard_Boolean); | |
3668 | Set_OK_To_Reference (T, False); | |
3669 | ||
3670 | -- Do the traversal | |
3671 | ||
3672 | Replace_Type (Exp); | |
3673 | ||
3674 | -- Build first two arguments for Check pragma | |
3675 | ||
3676 | Assoc := New_List ( | |
3677 | Make_Pragma_Argument_Association (Loc, | |
3678 | Expression => | |
3679 | Make_Identifier (Loc, | |
3680 | Chars => Name_Invariant)), | |
3681 | Make_Pragma_Argument_Association (Loc, | |
3682 | Expression => Exp)); | |
3683 | ||
3684 | -- Add message if present in Invariant pragma | |
3685 | ||
3686 | if Present (Arg3) then | |
3687 | Str := Strval (Get_Pragma_Arg (Arg3)); | |
3688 | ||
3689 | -- If inherited case, and message starts "failed invariant", | |
3690 | -- change it to be "failed inherited invariant". | |
3691 | ||
3692 | if Inherit then | |
3693 | String_To_Name_Buffer (Str); | |
3694 | ||
3695 | if Name_Buffer (1 .. 16) = "failed invariant" then | |
3696 | Insert_Str_In_Name_Buffer ("inherited ", 8); | |
3697 | Str := String_From_Name_Buffer; | |
3698 | end if; | |
3699 | end if; | |
3700 | ||
3701 | Append_To (Assoc, | |
3702 | Make_Pragma_Argument_Association (Loc, | |
3703 | Expression => Make_String_Literal (Loc, Str))); | |
3704 | end if; | |
3705 | ||
3706 | -- Add Check pragma to list of statements | |
3707 | ||
3708 | Append_To (Stmts, | |
3709 | Make_Pragma (Loc, | |
3710 | Pragma_Identifier => | |
3711 | Make_Identifier (Loc, | |
3712 | Chars => Name_Check), | |
3713 | Pragma_Argument_Associations => Assoc)); | |
3714 | ||
3715 | -- If Inherited case and option enabled, output info msg. Note | |
3716 | -- that we know this is a case of Invariant'Class. | |
3717 | ||
3718 | if Inherit and Opt.List_Inherited_Aspects then | |
3719 | Error_Msg_Sloc := Sloc (Ritem); | |
3720 | Error_Msg_N | |
3721 | ("?info: & inherits `Invariant''Class` aspect from #", | |
3722 | Typ); | |
3723 | end if; | |
3724 | end if; | |
3725 | ||
3726 | <<Continue>> | |
3727 | Next_Rep_Item (Ritem); | |
3728 | end loop; | |
3729 | end Add_Invariants; | |
3730 | ||
3731 | -- Start of processing for Build_Invariant_Procedure | |
3732 | ||
3733 | begin | |
3734 | Stmts := No_List; | |
3735 | PDecl := Empty; | |
3736 | PBody := Empty; | |
3737 | ||
3738 | -- Add invariants for the current type | |
3739 | ||
3740 | Add_Invariants (Typ, Inherit => False); | |
3741 | ||
3742 | -- Add invariants for parent types | |
3743 | ||
3744 | declare | |
3745 | Current_Typ : Entity_Id; | |
3746 | Parent_Typ : Entity_Id; | |
3747 | ||
3748 | begin | |
3749 | Current_Typ := Typ; | |
3750 | loop | |
3751 | Parent_Typ := Etype (Current_Typ); | |
3752 | ||
3753 | if Is_Private_Type (Parent_Typ) | |
3754 | and then Present (Full_View (Base_Type (Parent_Typ))) | |
3755 | then | |
3756 | Parent_Typ := Full_View (Base_Type (Parent_Typ)); | |
3757 | end if; | |
3758 | ||
3759 | exit when Parent_Typ = Current_Typ; | |
3760 | ||
3761 | Current_Typ := Parent_Typ; | |
3762 | Add_Invariants (Current_Typ, Inherit => True); | |
3763 | end loop; | |
3764 | end; | |
3765 | ||
5b5df4a9 | 3766 | -- Build the procedure if we generated at least one Check pragma |
3767 | ||
3768 | if Stmts /= No_List then | |
3769 | ||
3770 | -- Build procedure declaration | |
3771 | ||
f54f1dff | 3772 | pragma Assert (Has_Invariants (Typ)); |
5b5df4a9 | 3773 | SId := |
3774 | Make_Defining_Identifier (Loc, | |
3775 | Chars => New_External_Name (Chars (Typ), "Invariant")); | |
f54f1dff | 3776 | Set_Has_Invariants (SId); |
5b5df4a9 | 3777 | Set_Invariant_Procedure (Typ, SId); |
3778 | ||
3779 | Spec := | |
3780 | Make_Procedure_Specification (Loc, | |
3781 | Defining_Unit_Name => SId, | |
3782 | Parameter_Specifications => New_List ( | |
3783 | Make_Parameter_Specification (Loc, | |
3784 | Defining_Identifier => | |
3785 | Make_Defining_Identifier (Loc, | |
3786 | Chars => Object_Name), | |
3787 | Parameter_Type => | |
3788 | New_Occurrence_Of (Typ, Loc)))); | |
3789 | ||
3790 | PDecl := | |
3791 | Make_Subprogram_Declaration (Loc, | |
3792 | Specification => Spec); | |
3793 | ||
3794 | -- Build procedure body | |
3795 | ||
3796 | SId := | |
3797 | Make_Defining_Identifier (Loc, | |
3798 | Chars => New_External_Name (Chars (Typ), "Invariant")); | |
3799 | ||
3800 | Spec := | |
3801 | Make_Procedure_Specification (Loc, | |
3802 | Defining_Unit_Name => SId, | |
3803 | Parameter_Specifications => New_List ( | |
3804 | Make_Parameter_Specification (Loc, | |
3805 | Defining_Identifier => | |
3806 | Make_Defining_Identifier (Loc, | |
3807 | Chars => Object_Name), | |
3808 | Parameter_Type => | |
3809 | New_Occurrence_Of (Typ, Loc)))); | |
3810 | ||
3811 | PBody := | |
3812 | Make_Subprogram_Body (Loc, | |
3813 | Specification => Spec, | |
3814 | Declarations => Empty_List, | |
3815 | Handled_Statement_Sequence => | |
3816 | Make_Handled_Sequence_Of_Statements (Loc, | |
3817 | Statements => Stmts)); | |
3818 | end if; | |
3819 | end Build_Invariant_Procedure; | |
3820 | ||
9dc88aea | 3821 | ------------------------------ |
3822 | -- Build_Predicate_Function -- | |
3823 | ------------------------------ | |
3824 | ||
3825 | -- The procedure that is constructed here has the form | |
3826 | ||
3827 | -- function typPredicate (Ixxx : typ) return Boolean is | |
3828 | -- begin | |
3829 | -- return | |
3830 | -- exp1 and then exp2 and then ... | |
3831 | -- and then typ1Predicate (typ1 (Ixxx)) | |
3832 | -- and then typ2Predicate (typ2 (Ixxx)) | |
3833 | -- and then ...; | |
3834 | -- end typPredicate; | |
3835 | ||
3836 | -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that | |
3837 | -- this is the point at which these expressions get analyzed, providing the | |
3838 | -- required delay, and typ1, typ2, are entities from which predicates are | |
3839 | -- inherited. Note that we do NOT generate Check pragmas, that's because we | |
3840 | -- use this function even if checks are off, e.g. for membership tests. | |
3841 | ||
3842 | procedure Build_Predicate_Function | |
3843 | (Typ : Entity_Id; | |
3844 | FDecl : out Node_Id; | |
3845 | FBody : out Node_Id) | |
3846 | is | |
3847 | Loc : constant Source_Ptr := Sloc (Typ); | |
3848 | Spec : Node_Id; | |
3849 | SId : Entity_Id; | |
3850 | ||
3851 | Expr : Node_Id; | |
3852 | -- This is the expression for the return statement in the function. It | |
3853 | -- is build by connecting the component predicates with AND THEN. | |
3854 | ||
3855 | procedure Add_Call (T : Entity_Id); | |
3856 | -- Includes a call to the predicate function for type T in Expr if T | |
3857 | -- has predicates and Predicate_Function (T) is non-empty. | |
3858 | ||
3859 | procedure Add_Predicates; | |
3860 | -- Appends expressions for any Predicate pragmas in the rep item chain | |
3861 | -- Typ to Expr. Note that we look only at items for this exact entity. | |
3862 | -- Inheritance of predicates for the parent type is done by calling the | |
3863 | -- Predicate_Function of the parent type, using Add_Call above. | |
3864 | ||
9dc88aea | 3865 | Object_Name : constant Name_Id := New_Internal_Name ('I'); |
3866 | -- Name for argument of Predicate procedure | |
3867 | ||
3868 | -------------- | |
3869 | -- Add_Call -- | |
3870 | -------------- | |
3871 | ||
3872 | procedure Add_Call (T : Entity_Id) is | |
3873 | Exp : Node_Id; | |
3874 | ||
3875 | begin | |
3876 | if Present (T) and then Present (Predicate_Function (T)) then | |
3877 | Set_Has_Predicates (Typ); | |
3878 | ||
3879 | -- Build the call to the predicate function of T | |
3880 | ||
3881 | Exp := | |
3882 | Make_Predicate_Call | |
3883 | (T, | |
3884 | Convert_To (T, | |
3885 | Make_Identifier (Loc, Chars => Object_Name))); | |
3886 | ||
3887 | -- Add call to evolving expression, using AND THEN if needed | |
3888 | ||
3889 | if No (Expr) then | |
3890 | Expr := Exp; | |
3891 | else | |
3892 | Expr := | |
3893 | Make_And_Then (Loc, | |
3894 | Left_Opnd => Relocate_Node (Expr), | |
3895 | Right_Opnd => Exp); | |
3896 | end if; | |
3897 | ||
2f32076c | 3898 | -- Output info message on inheritance if required. Note we do not |
3899 | -- give this information for generic actual types, since it is | |
55e8372b | 3900 | -- unwelcome noise in that case in instantiations. We also |
3901 | -- generally suppress the message in instantiations. | |
9dc88aea | 3902 | |
2f32076c | 3903 | if Opt.List_Inherited_Aspects |
3904 | and then not Is_Generic_Actual_Type (Typ) | |
55e8372b | 3905 | and then Instantiation_Depth (Sloc (Typ)) = 0 |
2f32076c | 3906 | then |
9dc88aea | 3907 | Error_Msg_Sloc := Sloc (Predicate_Function (T)); |
3908 | Error_Msg_Node_2 := T; | |
3909 | Error_Msg_N ("?info: & inherits predicate from & #", Typ); | |
3910 | end if; | |
3911 | end if; | |
3912 | end Add_Call; | |
3913 | ||
3914 | -------------------- | |
3915 | -- Add_Predicates -- | |
3916 | -------------------- | |
3917 | ||
3918 | procedure Add_Predicates is | |
3919 | Ritem : Node_Id; | |
3920 | Arg1 : Node_Id; | |
3921 | Arg2 : Node_Id; | |
3922 | ||
3923 | function Replace_Node (N : Node_Id) return Traverse_Result; | |
3924 | -- Process single node for traversal to replace type references | |
3925 | ||
3926 | procedure Replace_Type is new Traverse_Proc (Replace_Node); | |
3927 | -- Traverse an expression changing every occurrence of an entity | |
3928 | -- reference to type T with a reference to the object argument. | |
3929 | ||
3930 | ------------------ | |
3931 | -- Replace_Node -- | |
3932 | ------------------ | |
3933 | ||
3934 | function Replace_Node (N : Node_Id) return Traverse_Result is | |
3935 | begin | |
3936 | -- Case of entity name referencing the type | |
3937 | ||
3938 | if Is_Entity_Name (N) and then Entity (N) = Typ then | |
3939 | ||
3940 | -- Replace with object | |
3941 | ||
3942 | Rewrite (N, | |
3943 | Make_Identifier (Loc, | |
3944 | Chars => Object_Name)); | |
3945 | ||
3946 | -- All done with this node | |
3947 | ||
3948 | return Skip; | |
3949 | ||
3950 | -- Not an occurrence of the type entity, keep going | |
3951 | ||
3952 | else | |
3953 | return OK; | |
3954 | end if; | |
3955 | end Replace_Node; | |
3956 | ||
3957 | -- Start of processing for Add_Predicates | |
3958 | ||
3959 | begin | |
3960 | Ritem := First_Rep_Item (Typ); | |
3961 | while Present (Ritem) loop | |
3962 | if Nkind (Ritem) = N_Pragma | |
3963 | and then Pragma_Name (Ritem) = Name_Predicate | |
3964 | then | |
3965 | Arg1 := First (Pragma_Argument_Associations (Ritem)); | |
3966 | Arg2 := Next (Arg1); | |
3967 | ||
3968 | Arg1 := Get_Pragma_Arg (Arg1); | |
3969 | Arg2 := Get_Pragma_Arg (Arg2); | |
3970 | ||
3971 | -- See if this predicate pragma is for the current type | |
3972 | ||
3973 | if Entity (Arg1) = Typ then | |
3974 | ||
3975 | -- We have a match, this entry is for our subtype | |
3976 | ||
3977 | -- First We need to replace any occurrences of the name of | |
3978 | -- the type with references to the object. We do this by | |
3979 | -- first doing a preanalysis, to identify all the entities, | |
3980 | -- then we traverse looking for the type entity, doing the | |
3981 | -- needed substitution. The preanalysis is done with the | |
3982 | -- special OK_To_Reference flag set on the type, so that if | |
3983 | -- we get an occurrence of this type, it will be recognized | |
3984 | -- as legitimate. | |
3985 | ||
3986 | Set_OK_To_Reference (Typ, True); | |
3987 | Preanalyze_Spec_Expression (Arg2, Standard_Boolean); | |
3988 | Set_OK_To_Reference (Typ, False); | |
3989 | Replace_Type (Arg2); | |
3990 | ||
3991 | -- OK, replacement complete, now we can add the expression | |
3992 | ||
3993 | if No (Expr) then | |
3994 | Expr := Relocate_Node (Arg2); | |
3995 | ||
3996 | -- There already was a predicate, so add to it | |
3997 | ||
3998 | else | |
3999 | Expr := | |
4000 | Make_And_Then (Loc, | |
4001 | Left_Opnd => Relocate_Node (Expr), | |
4002 | Right_Opnd => Relocate_Node (Arg2)); | |
4003 | end if; | |
4004 | end if; | |
4005 | end if; | |
4006 | ||
4007 | Next_Rep_Item (Ritem); | |
4008 | end loop; | |
4009 | end Add_Predicates; | |
4010 | ||
d97beb2f | 4011 | -- Start of processing for Build_Predicate_Function |
9dc88aea | 4012 | |
d97beb2f | 4013 | begin |
4014 | -- Initialize for construction of statement list | |
4015 | ||
4016 | Expr := Empty; | |
4017 | FDecl := Empty; | |
4018 | FBody := Empty; | |
4019 | ||
4020 | -- Return if already built or if type does not have predicates | |
4021 | ||
4022 | if not Has_Predicates (Typ) | |
4023 | or else Present (Predicate_Function (Typ)) | |
4024 | then | |
4025 | return; | |
4026 | end if; | |
4027 | ||
4028 | -- Add Predicates for the current type | |
4029 | ||
4030 | Add_Predicates; | |
4031 | ||
4032 | -- Add predicates for ancestor if present | |
4033 | ||
4034 | declare | |
4035 | Atyp : constant Entity_Id := Nearest_Ancestor (Typ); | |
4036 | begin | |
4037 | if Present (Atyp) then | |
4038 | Add_Call (Atyp); | |
4039 | end if; | |
4040 | end; | |
4041 | ||
4042 | -- If we have predicates, build the function | |
4043 | ||
4044 | if Present (Expr) then | |
4045 | ||
4046 | -- Deal with static predicate case | |
4047 | ||
4048 | Build_Static_Predicate (Typ, Expr, Object_Name); | |
4049 | ||
4050 | -- Build function declaration | |
4051 | ||
4052 | pragma Assert (Has_Predicates (Typ)); | |
4053 | SId := | |
4054 | Make_Defining_Identifier (Loc, | |
4055 | Chars => New_External_Name (Chars (Typ), "Predicate")); | |
4056 | Set_Has_Predicates (SId); | |
4057 | Set_Predicate_Function (Typ, SId); | |
9dc88aea | 4058 | |
d97beb2f | 4059 | Spec := |
4060 | Make_Function_Specification (Loc, | |
4061 | Defining_Unit_Name => SId, | |
4062 | Parameter_Specifications => New_List ( | |
4063 | Make_Parameter_Specification (Loc, | |
4064 | Defining_Identifier => | |
4065 | Make_Defining_Identifier (Loc, Chars => Object_Name), | |
4066 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), | |
4067 | Result_Definition => | |
4068 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
9dc88aea | 4069 | |
d97beb2f | 4070 | FDecl := |
4071 | Make_Subprogram_Declaration (Loc, | |
4072 | Specification => Spec); | |
9dc88aea | 4073 | |
d97beb2f | 4074 | -- Build function body |
4075 | ||
4076 | SId := | |
4077 | Make_Defining_Identifier (Loc, | |
4078 | Chars => New_External_Name (Chars (Typ), "Predicate")); | |
4079 | ||
4080 | Spec := | |
4081 | Make_Function_Specification (Loc, | |
4082 | Defining_Unit_Name => SId, | |
4083 | Parameter_Specifications => New_List ( | |
4084 | Make_Parameter_Specification (Loc, | |
4085 | Defining_Identifier => | |
4086 | Make_Defining_Identifier (Loc, Chars => Object_Name), | |
4087 | Parameter_Type => | |
4088 | New_Occurrence_Of (Typ, Loc))), | |
4089 | Result_Definition => | |
4090 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
4091 | ||
4092 | FBody := | |
4093 | Make_Subprogram_Body (Loc, | |
4094 | Specification => Spec, | |
4095 | Declarations => Empty_List, | |
4096 | Handled_Statement_Sequence => | |
4097 | Make_Handled_Sequence_Of_Statements (Loc, | |
4098 | Statements => New_List ( | |
4099 | Make_Simple_Return_Statement (Loc, | |
4100 | Expression => Expr)))); | |
4101 | end if; | |
4102 | end Build_Predicate_Function; | |
4103 | ||
4104 | ---------------------------- | |
4105 | -- Build_Static_Predicate -- | |
4106 | ---------------------------- | |
4107 | ||
4108 | procedure Build_Static_Predicate | |
4109 | (Typ : Entity_Id; | |
4110 | Expr : Node_Id; | |
4111 | Nam : Name_Id) | |
4112 | is | |
4113 | Loc : constant Source_Ptr := Sloc (Expr); | |
4114 | ||
4115 | Non_Static : exception; | |
4116 | -- Raised if something non-static is found | |
4117 | ||
4118 | TLo, THi : Uint; | |
4119 | -- Low bound and high bound values of static subtype of Typ | |
4120 | ||
4121 | type REnt is record | |
9dc88aea | 4122 | Lo, Hi : Uint; |
d97beb2f | 4123 | end record; |
4124 | -- One entry in a Rlist value, a single REnt (range entry) value | |
4125 | -- denotes one range from Lo to Hi. To represent a single value | |
4126 | -- range Lo = Hi = value. | |
4127 | ||
4128 | type RList is array (Nat range <>) of REnt; | |
4129 | -- A list of ranges. The ranges are sorted in increasing order, | |
4130 | -- and are disjoint (there is a gap of at least one value between | |
4131 | -- each range in the table). | |
4132 | ||
4133 | Null_Range : constant RList := RList'(1 .. 0 => REnt'(No_Uint, No_Uint)); | |
4134 | True_Range : RList renames Null_Range; | |
4135 | -- Constant representing null list of ranges, used to represent a | |
4136 | -- predicate of True, since there are no ranges to be satisfied. | |
4137 | ||
4138 | False_Range : constant RList := RList'(1 => REnt'(Uint_1, Uint_0)); | |
4139 | -- Range representing false | |
4140 | ||
4141 | function "and" (Left, Right : RList) return RList; | |
4142 | -- And's together two range lists, returning a range list. This is | |
4143 | -- a set intersection operation. | |
4144 | ||
4145 | function "or" (Left, Right : RList) return RList; | |
4146 | -- Or's together two range lists, returning a range list. This is a | |
4147 | -- set union operation. | |
4148 | ||
4149 | function "not" (Right : RList) return RList; | |
4150 | -- Returns complement of a given range list, i.e. a range list | |
4151 | -- representing all the values in TLo .. THi that are not in the | |
4152 | -- input operand Right. | |
4153 | ||
4154 | function Build_Val (V : Uint) return Node_Id; | |
4155 | -- Return an analyzed N_Identifier node referencing this value, suitable | |
4156 | -- for use as an entry in the Static_Predicate list. | |
4157 | ||
4158 | function Build_Range (Lo, Hi : Uint) return Node_Id; | |
4159 | -- Return an analyzed N_Range node referencing this range, suitable | |
4160 | -- for use as an entry in the Static_Predicate list. | |
4161 | ||
4162 | function Get_RList (Exp : Node_Id) return RList; | |
4163 | -- This is a recursive routine that converts the given expression into | |
4164 | -- a list of ranges, suitable for use in building the static predicate. | |
4165 | ||
4166 | function Is_Type_Ref (N : Node_Id) return Boolean; | |
4167 | pragma Inline (Is_Type_Ref); | |
4168 | -- Returns if True if N is a reference to the type for the predicate in | |
4169 | -- the expression (i.e. if it is an identifier whose Chars field matches | |
4170 | -- the Nam given in the call). | |
4171 | ||
4172 | function Lo_Val (N : Node_Id) return Uint; | |
4173 | -- Given static expression or static range from a Static_Predicate list, | |
4174 | -- gets expression value or low bound of range. | |
4175 | ||
4176 | function Hi_Val (N : Node_Id) return Uint; | |
4177 | -- Given static expression or static range from a Static_Predicate list, | |
4178 | -- gets expression value of high bound of range. | |
4179 | ||
4180 | function Membership_Entry (N : Node_Id) return RList; | |
4181 | -- Given a single membership entry (range, value, or subtype), returns | |
4182 | -- the corresponding range list. Raises Static_Error if not static. | |
4183 | ||
4184 | function Membership_Entries (N : Node_Id) return RList; | |
4185 | -- Given an element on an alternatives list of a membership operation, | |
4186 | -- returns the range list corresponding to this entry and all following | |
4187 | -- entries (i.e. returns the "or" of this list of values). | |
4188 | ||
4189 | function Stat_Pred (Typ : Entity_Id) return RList; | |
4190 | -- Given a type, if it has a static predicate, then return the predicate | |
4191 | -- as a range list, otherwise raise Non_Static. | |
4192 | ||
4193 | ----------- | |
4194 | -- "and" -- | |
4195 | ----------- | |
4196 | ||
4197 | function "and" (Left, Right : RList) return RList is | |
4198 | FEnt : REnt; | |
4199 | -- First range of result | |
4200 | ||
4201 | SLeft : Nat := Left'First; | |
4202 | -- Start of rest of left entries | |
4203 | ||
4204 | SRight : Nat := Right'First; | |
4205 | -- Start of rest of right entries | |
9dc88aea | 4206 | |
d97beb2f | 4207 | begin |
4208 | -- If either range is True, return the other | |
9dc88aea | 4209 | |
d97beb2f | 4210 | if Left = True_Range then |
4211 | return Right; | |
4212 | elsif Right = True_Range then | |
4213 | return Left; | |
4214 | end if; | |
9dc88aea | 4215 | |
d97beb2f | 4216 | -- If either range is False, return False |
9dc88aea | 4217 | |
d97beb2f | 4218 | if Left = False_Range or else Right = False_Range then |
4219 | return False_Range; | |
4220 | end if; | |
9dc88aea | 4221 | |
d97beb2f | 4222 | -- If either range is empty, return False |
9dc88aea | 4223 | |
d97beb2f | 4224 | if Left'Length = 0 or else Right'Length = 0 then |
4225 | return False_Range; | |
4226 | end if; | |
9dc88aea | 4227 | |
d97beb2f | 4228 | -- Loop to remove entries at start that are disjoint, and thus |
4229 | -- just get discarded from the result entirely. | |
9dc88aea | 4230 | |
d97beb2f | 4231 | loop |
4232 | -- If no operands left in either operand, result is false | |
9dc88aea | 4233 | |
d97beb2f | 4234 | if SLeft > Left'Last or else SRight > Right'Last then |
4235 | return False_Range; | |
9dc88aea | 4236 | |
d97beb2f | 4237 | -- Discard first left operand entry if disjoint with right |
9dc88aea | 4238 | |
d97beb2f | 4239 | elsif Left (SLeft).Hi < Right (SRight).Lo then |
4240 | SLeft := SLeft + 1; | |
9dc88aea | 4241 | |
d97beb2f | 4242 | -- Discard first right operand entry if disjoint with left |
9dc88aea | 4243 | |
d97beb2f | 4244 | elsif Right (SRight).Hi < Left (SLeft).Lo then |
4245 | SRight := SRight + 1; | |
9dc88aea | 4246 | |
d97beb2f | 4247 | -- Otherwise we have an overlapping entry |
9dc88aea | 4248 | |
d97beb2f | 4249 | else |
4250 | exit; | |
4251 | end if; | |
4252 | end loop; | |
9dc88aea | 4253 | |
d97beb2f | 4254 | -- Now we have two non-null operands, and first entries overlap. |
4255 | -- The first entry in the result will be the overlapping part of | |
4256 | -- these two entries. | |
9dc88aea | 4257 | |
d97beb2f | 4258 | FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo), |
4259 | Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi)); | |
9dc88aea | 4260 | |
d97beb2f | 4261 | -- Now we can remove the entry that ended at a lower value, since |
4262 | -- its contribution is entirely contained in Fent. | |
4263 | ||
4264 | if Left (SLeft).Hi <= Right (SRight).Hi then | |
4265 | SLeft := SLeft + 1; | |
4266 | else | |
4267 | SRight := SRight + 1; | |
4268 | end if; | |
4269 | ||
4270 | -- If either operand is empty, that's the only entry | |
4271 | ||
4272 | if SLeft > Left'Last or else SRight > Right'Last then | |
4273 | return RList'(1 => FEnt); | |
4274 | ||
4275 | -- Else compute and of remaining entries and concatenate | |
4276 | ||
4277 | else | |
4278 | return | |
4279 | FEnt & | |
4280 | (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last)); | |
4281 | end if; | |
4282 | end "and"; | |
4283 | ||
4284 | ----------- | |
4285 | -- "not" -- | |
4286 | ----------- | |
4287 | ||
4288 | function "not" (Right : RList) return RList is | |
4289 | begin | |
4290 | -- Return True if False range | |
4291 | ||
4292 | if Right = False_Range then | |
4293 | return True_Range; | |
4294 | end if; | |
4295 | ||
4296 | -- Return False if True range | |
4297 | ||
4298 | if Right'Length = 0 then | |
4299 | return False_Range; | |
4300 | end if; | |
4301 | ||
4302 | -- Here if not trivial case | |
4303 | ||
4304 | declare | |
4305 | Result : RList (1 .. Right'Length + 1); | |
4306 | -- May need one more entry for gap at beginning and end | |
4307 | ||
4308 | Count : Nat := 0; | |
4309 | -- Number of entries stored in Result | |
4310 | ||
4311 | begin | |
4312 | -- Gap at start | |
4313 | ||
4314 | if Right (Right'First).Lo > TLo then | |
4315 | Count := Count + 1; | |
4316 | Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1); | |
4317 | end if; | |
4318 | ||
4319 | -- Gaps between ranges | |
4320 | ||
4321 | for J in Right'First .. Right'Last - 1 loop | |
4322 | Count := Count + 1; | |
4323 | Result (Count) := | |
4324 | REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1); | |
4325 | end loop; | |
4326 | ||
4327 | -- Gap at end | |
4328 | ||
4329 | if Right (Right'Last).Hi < THi then | |
4330 | Count := Count + 1; | |
4331 | Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi); | |
4332 | end if; | |
4333 | ||
4334 | return Result (1 .. Count); | |
4335 | end; | |
4336 | end "not"; | |
4337 | ||
4338 | ---------- | |
4339 | -- "or" -- | |
4340 | ---------- | |
4341 | ||
4342 | function "or" (Left, Right : RList) return RList is | |
4343 | begin | |
4344 | -- If either range is True, return True | |
4345 | ||
4346 | if Left = True_Range or else Right = True_Range then | |
4347 | return True_Range; | |
4348 | end if; | |
4349 | ||
4350 | -- If either range is False, return the other | |
9dc88aea | 4351 | |
d97beb2f | 4352 | if Left = False_Range then |
4353 | return Right; | |
4354 | elsif Right = False_Range then | |
4355 | return Left; | |
4356 | end if; | |
4357 | ||
4358 | -- If either operand is null, return the other one | |
4359 | ||
4360 | if Left'Length = 0 then | |
4361 | return Right; | |
4362 | elsif Right'Length = 0 then | |
4363 | return Left; | |
4364 | end if; | |
4365 | ||
4366 | -- Now we have two non-null ranges | |
4367 | ||
4368 | declare | |
4369 | FEnt : REnt; | |
4370 | -- First range of result | |
4371 | ||
4372 | SLeft : Nat := Left'First; | |
4373 | -- Start of rest of left entries | |
4374 | ||
4375 | SRight : Nat := Right'First; | |
4376 | -- Start of rest of right entries | |
9dc88aea | 4377 | |
4378 | begin | |
d97beb2f | 4379 | -- Initialize result first entry from left or right operand |
4380 | -- depending on which starts with the lower range. | |
4381 | ||
4382 | if Left (SLeft).Lo < Right (SRight).Lo then | |
4383 | FEnt := Left (SLeft); | |
4384 | SLeft := SLeft + 1; | |
4385 | else | |
4386 | FEnt := Right (SRight); | |
4387 | SRight := SRight + 1; | |
4388 | end if; | |
4389 | ||
4390 | -- This loop eats ranges from left and right operands that | |
4391 | -- are contiguous with the first range we are gathering. | |
4392 | ||
4393 | loop | |
4394 | -- Eat first entry in left operand if contiguous or | |
4395 | -- overlapped by gathered first operand of result. | |
9dc88aea | 4396 | |
d97beb2f | 4397 | if SLeft <= Left'Last |
4398 | and then Left (SLeft).Lo <= FEnt.Hi + 1 | |
9dc88aea | 4399 | then |
d97beb2f | 4400 | FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi); |
4401 | SLeft := SLeft + 1; | |
4402 | ||
4403 | -- Eat first entry in right operand if contiguous or | |
4404 | -- overlapped by gathered right operand of result. | |
4405 | ||
4406 | elsif SRight <= Right'Last | |
4407 | and then Right (SRight).Lo <= FEnt.Hi + 1 | |
4408 | then | |
4409 | FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi); | |
4410 | SRight := SRight + 1; | |
4411 | ||
4412 | -- All done if no more entries to eat! | |
4413 | ||
9dc88aea | 4414 | else |
d97beb2f | 4415 | exit; |
9dc88aea | 4416 | end if; |
d97beb2f | 4417 | end loop; |
9dc88aea | 4418 | |
d97beb2f | 4419 | -- If left operand now empty, concatenate our new entry to right |
9dc88aea | 4420 | |
d97beb2f | 4421 | if SLeft > Left'Last then |
4422 | return FEnt & Right (SRight .. Right'Last); | |
9dc88aea | 4423 | |
d97beb2f | 4424 | -- If right operand now empty, concatenate our new entry to left |
9dc88aea | 4425 | |
d97beb2f | 4426 | elsif SRight > Right'Last then |
4427 | return FEnt & Left (SLeft .. Left'Last); | |
9dc88aea | 4428 | |
d97beb2f | 4429 | -- Otherwise, compute or of what is left and concatenate |
9dc88aea | 4430 | |
d97beb2f | 4431 | else |
4432 | return | |
4433 | FEnt & | |
4434 | (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last)); | |
4435 | end if; | |
4436 | end; | |
4437 | end "or"; | |
9dc88aea | 4438 | |
d97beb2f | 4439 | ----------------- |
4440 | -- Build_Range -- | |
4441 | ----------------- | |
9dc88aea | 4442 | |
d97beb2f | 4443 | function Build_Range (Lo, Hi : Uint) return Node_Id is |
4444 | Result : Node_Id; | |
4445 | begin | |
4446 | if Lo = Hi then | |
4447 | return Build_Val (Hi); | |
4448 | else | |
4449 | Result := | |
4450 | Make_Range (Loc, | |
4451 | Low_Bound => Build_Val (Lo), | |
4452 | High_Bound => Build_Val (Hi)); | |
4453 | Set_Etype (Result, Typ); | |
4454 | Set_Analyzed (Result); | |
4455 | return Result; | |
4456 | end if; | |
4457 | end Build_Range; | |
9dc88aea | 4458 | |
d97beb2f | 4459 | --------------- |
4460 | -- Build_Val -- | |
4461 | --------------- | |
9dc88aea | 4462 | |
d97beb2f | 4463 | function Build_Val (V : Uint) return Node_Id is |
4464 | Result : Node_Id; | |
4465 | ||
4466 | begin | |
4467 | if Is_Enumeration_Type (Typ) then | |
4468 | Result := Get_Enum_Lit_From_Pos (Typ, V, Loc); | |
4469 | else | |
4470 | Result := Make_Integer_Literal (Loc, Intval => V); | |
4471 | end if; | |
9dc88aea | 4472 | |
d97beb2f | 4473 | Set_Etype (Result, Typ); |
4474 | Set_Is_Static_Expression (Result); | |
4475 | Set_Analyzed (Result); | |
4476 | return Result; | |
4477 | end Build_Val; | |
9dc88aea | 4478 | |
d97beb2f | 4479 | --------------- |
4480 | -- Get_RList -- | |
4481 | --------------- | |
9dc88aea | 4482 | |
d97beb2f | 4483 | function Get_RList (Exp : Node_Id) return RList is |
4484 | Op : Node_Kind; | |
4485 | Val : Uint; | |
9dc88aea | 4486 | |
d97beb2f | 4487 | begin |
4488 | -- Static expression can only be true or false | |
4489 | ||
4490 | if Is_OK_Static_Expression (Exp) then | |
4491 | ||
4492 | -- For False, return impossible range, which will always fail | |
4493 | ||
4494 | if Expr_Value (Exp) = 0 then | |
4495 | return False_Range; | |
4496 | ||
4497 | -- For True, null range | |
4498 | ||
4499 | else | |
4500 | return Null_Range; | |
4501 | end if; | |
4502 | end if; | |
4503 | ||
4504 | -- Otherwise test node type | |
4505 | ||
4506 | Op := Nkind (Exp); | |
4507 | ||
4508 | case Op is | |
4509 | ||
4510 | -- And | |
4511 | ||
4512 | when N_Op_And | N_And_Then => | |
4513 | return Get_RList (Left_Opnd (Exp)) | |
4514 | and | |
4515 | Get_RList (Right_Opnd (Exp)); | |
9dc88aea | 4516 | |
d97beb2f | 4517 | -- Or |
4518 | ||
4519 | when N_Op_Or | N_Or_Else => | |
4520 | return Get_RList (Left_Opnd (Exp)) | |
4521 | or | |
4522 | Get_RList (Right_Opnd (Exp)); | |
4523 | ||
4524 | -- Not | |
4525 | ||
4526 | when N_Op_Not => | |
4527 | return not Get_RList (Right_Opnd (Exp)); | |
4528 | ||
4529 | -- Comparisons of type with static value | |
4530 | ||
4531 | when N_Op_Compare => | |
4532 | -- Type is left operand | |
4533 | ||
4534 | if Is_Type_Ref (Left_Opnd (Exp)) | |
4535 | and then Is_OK_Static_Expression (Right_Opnd (Exp)) | |
4536 | then | |
4537 | Val := Expr_Value (Right_Opnd (Exp)); | |
4538 | ||
4539 | -- Typ is right operand | |
4540 | ||
4541 | elsif Is_Type_Ref (Right_Opnd (Exp)) | |
4542 | and then Is_OK_Static_Expression (Left_Opnd (Exp)) | |
4543 | then | |
4544 | Val := Expr_Value (Left_Opnd (Exp)); | |
4545 | ||
4546 | -- Invert sense of comparison | |
4547 | ||
4548 | case Op is | |
4549 | when N_Op_Gt => Op := N_Op_Lt; | |
4550 | when N_Op_Lt => Op := N_Op_Gt; | |
4551 | when N_Op_Ge => Op := N_Op_Le; | |
4552 | when N_Op_Le => Op := N_Op_Ge; | |
4553 | when others => null; | |
4554 | end case; | |
4555 | ||
4556 | -- Other cases are non-static | |
9dc88aea | 4557 | |
4558 | else | |
d97beb2f | 4559 | raise Non_Static; |
9dc88aea | 4560 | end if; |
9dc88aea | 4561 | |
d97beb2f | 4562 | -- Construct range according to comparison operation |
9dc88aea | 4563 | |
d97beb2f | 4564 | case Op is |
4565 | when N_Op_Eq => | |
4566 | return RList'(1 => REnt'(Val, Val)); | |
9dc88aea | 4567 | |
d97beb2f | 4568 | when N_Op_Ge => |
4569 | return RList'(1 => REnt'(Val, THi)); | |
9dc88aea | 4570 | |
d97beb2f | 4571 | when N_Op_Gt => |
4572 | return RList'(1 => REnt'(Val + 1, THi)); | |
9dc88aea | 4573 | |
d97beb2f | 4574 | when N_Op_Le => |
4575 | return RList'(1 => REnt'(TLo, Val)); | |
9dc88aea | 4576 | |
d97beb2f | 4577 | when N_Op_Lt => |
4578 | return RList'(1 => REnt'(TLo, Val - 1)); | |
9dc88aea | 4579 | |
d97beb2f | 4580 | when N_Op_Ne => |
4581 | return RList'(REnt'(TLo, Val - 1), | |
4582 | REnt'(Val + 1, THi)); | |
9dc88aea | 4583 | |
d97beb2f | 4584 | when others => |
4585 | raise Program_Error; | |
4586 | end case; | |
9dc88aea | 4587 | |
d97beb2f | 4588 | -- Membership (IN) |
9dc88aea | 4589 | |
d97beb2f | 4590 | when N_In => |
4591 | if not Is_Type_Ref (Left_Opnd (Exp)) then | |
4592 | raise Non_Static; | |
4593 | end if; | |
9dc88aea | 4594 | |
d97beb2f | 4595 | if Present (Right_Opnd (Exp)) then |
4596 | return Membership_Entry (Right_Opnd (Exp)); | |
4597 | else | |
4598 | return Membership_Entries (First (Alternatives (Exp))); | |
4599 | end if; | |
9dc88aea | 4600 | |
d97beb2f | 4601 | -- Negative membership (NOT IN) |
9dc88aea | 4602 | |
d97beb2f | 4603 | when N_Not_In => |
4604 | if not Is_Type_Ref (Left_Opnd (Exp)) then | |
4605 | raise Non_Static; | |
4606 | end if; | |
4607 | ||
4608 | if Present (Right_Opnd (Exp)) then | |
4609 | return not Membership_Entry (Right_Opnd (Exp)); | |
4610 | else | |
4611 | return not Membership_Entries (First (Alternatives (Exp))); | |
4612 | end if; | |
4613 | ||
4614 | -- Function call, may be call to static predicate | |
4615 | ||
4616 | when N_Function_Call => | |
4617 | if Is_Entity_Name (Name (Exp)) then | |
4618 | declare | |
4619 | Ent : constant Entity_Id := Entity (Name (Exp)); | |
4620 | begin | |
4621 | if Has_Predicates (Ent) then | |
4622 | return Stat_Pred (Etype (First_Formal (Ent))); | |
9dc88aea | 4623 | end if; |
d97beb2f | 4624 | end; |
4625 | end if; | |
9dc88aea | 4626 | |
d97beb2f | 4627 | -- Other function call cases are non-static |
9dc88aea | 4628 | |
d97beb2f | 4629 | raise Non_Static; |
9dc88aea | 4630 | |
d97beb2f | 4631 | -- Qualified expression, dig out the expression |
9dc88aea | 4632 | |
d97beb2f | 4633 | when N_Qualified_Expression => |
4634 | return Get_RList (Expression (Exp)); | |
9dc88aea | 4635 | |
d97beb2f | 4636 | -- Any other node type is non-static |
9dc88aea | 4637 | |
d97beb2f | 4638 | when others => |
4639 | raise Non_Static; | |
4640 | end case; | |
4641 | end Get_RList; | |
9dc88aea | 4642 | |
d97beb2f | 4643 | ------------ |
4644 | -- Hi_Val -- | |
4645 | ------------ | |
9dc88aea | 4646 | |
d97beb2f | 4647 | function Hi_Val (N : Node_Id) return Uint is |
9dc88aea | 4648 | begin |
d97beb2f | 4649 | if Is_Static_Expression (N) then |
4650 | return Expr_Value (N); | |
4651 | else | |
4652 | pragma Assert (Nkind (N) = N_Range); | |
4653 | return Expr_Value (High_Bound (N)); | |
9dc88aea | 4654 | end if; |
d97beb2f | 4655 | end Hi_Val; |
9dc88aea | 4656 | |
d97beb2f | 4657 | ----------------- |
4658 | -- Is_Type_Ref -- | |
4659 | ----------------- | |
9dc88aea | 4660 | |
d97beb2f | 4661 | function Is_Type_Ref (N : Node_Id) return Boolean is |
4662 | begin | |
4663 | return Nkind (N) = N_Identifier and then Chars (N) = Nam; | |
4664 | end Is_Type_Ref; | |
9dc88aea | 4665 | |
d97beb2f | 4666 | ------------ |
4667 | -- Lo_Val -- | |
4668 | ------------ | |
9dc88aea | 4669 | |
d97beb2f | 4670 | function Lo_Val (N : Node_Id) return Uint is |
4671 | begin | |
4672 | if Is_Static_Expression (N) then | |
4673 | return Expr_Value (N); | |
4674 | else | |
4675 | pragma Assert (Nkind (N) = N_Range); | |
4676 | return Expr_Value (Low_Bound (N)); | |
4677 | end if; | |
4678 | end Lo_Val; | |
9dc88aea | 4679 | |
d97beb2f | 4680 | ------------------------ |
4681 | -- Membership_Entries -- | |
4682 | ------------------------ | |
9dc88aea | 4683 | |
d97beb2f | 4684 | function Membership_Entries (N : Node_Id) return RList is |
4685 | begin | |
4686 | if No (Next (N)) then | |
4687 | return Membership_Entry (N); | |
9dc88aea | 4688 | else |
d97beb2f | 4689 | return Membership_Entry (N) or Membership_Entries (Next (N)); |
9dc88aea | 4690 | end if; |
d97beb2f | 4691 | end Membership_Entries; |
9dc88aea | 4692 | |
d97beb2f | 4693 | ---------------------- |
4694 | -- Membership_Entry -- | |
4695 | ---------------------- | |
9dc88aea | 4696 | |
d97beb2f | 4697 | function Membership_Entry (N : Node_Id) return RList is |
4698 | Val : Uint; | |
4699 | SLo : Uint; | |
4700 | SHi : Uint; | |
9dc88aea | 4701 | |
d97beb2f | 4702 | begin |
4703 | -- Range case | |
4704 | ||
4705 | if Nkind (N) = N_Range then | |
4706 | if not Is_Static_Expression (Low_Bound (N)) | |
4707 | or else | |
4708 | not Is_Static_Expression (High_Bound (N)) | |
4709 | then | |
4710 | raise Non_Static; | |
4711 | else | |
4712 | SLo := Expr_Value (Low_Bound (N)); | |
4713 | SHi := Expr_Value (High_Bound (N)); | |
4714 | return RList'(1 => REnt'(SLo, SHi)); | |
9dc88aea | 4715 | end if; |
4716 | ||
d97beb2f | 4717 | -- Static expression case |
9dc88aea | 4718 | |
d97beb2f | 4719 | elsif Is_Static_Expression (N) then |
4720 | Val := Expr_Value (N); | |
4721 | return RList'(1 => REnt'(Val, Val)); | |
9dc88aea | 4722 | |
d97beb2f | 4723 | -- Identifier (other than static expression) case |
9dc88aea | 4724 | |
d97beb2f | 4725 | else pragma Assert (Nkind (N) = N_Identifier); |
9dc88aea | 4726 | |
d97beb2f | 4727 | -- Type case |
55e8372b | 4728 | |
d97beb2f | 4729 | if Is_Type (Entity (N)) then |
55e8372b | 4730 | |
d97beb2f | 4731 | -- If type has predicates, process them |
55e8372b | 4732 | |
d97beb2f | 4733 | if Has_Predicates (Entity (N)) then |
4734 | return Stat_Pred (Entity (N)); | |
55e8372b | 4735 | |
d97beb2f | 4736 | -- For static subtype without predicates, get range |
55e8372b | 4737 | |
d97beb2f | 4738 | elsif Is_Static_Subtype (Entity (N)) then |
4739 | SLo := Expr_Value (Type_Low_Bound (Entity (N))); | |
4740 | SHi := Expr_Value (Type_High_Bound (Entity (N))); | |
4741 | return RList'(1 => REnt'(SLo, SHi)); | |
4742 | ||
4743 | -- Any other type makes us non-static | |
55e8372b | 4744 | |
d97beb2f | 4745 | else |
4746 | raise Non_Static; | |
4747 | end if; | |
55e8372b | 4748 | |
d97beb2f | 4749 | -- Any other kind of identifier in predicate (e.g. a non-static |
4750 | -- expression value) means this is not a static predicate. | |
55e8372b | 4751 | |
55e8372b | 4752 | else |
d97beb2f | 4753 | raise Non_Static; |
55e8372b | 4754 | end if; |
d97beb2f | 4755 | end if; |
4756 | end Membership_Entry; | |
9dc88aea | 4757 | |
d97beb2f | 4758 | --------------- |
4759 | -- Stat_Pred -- | |
4760 | --------------- | |
9dc88aea | 4761 | |
d97beb2f | 4762 | function Stat_Pred (Typ : Entity_Id) return RList is |
4763 | begin | |
4764 | -- Not static if type does not have static predicates | |
9dc88aea | 4765 | |
d97beb2f | 4766 | if not Has_Predicates (Typ) |
4767 | or else No (Static_Predicate (Typ)) | |
4768 | then | |
4769 | raise Non_Static; | |
4770 | end if; | |
9dc88aea | 4771 | |
d97beb2f | 4772 | -- Otherwise we convert the predicate list to a range list |
9dc88aea | 4773 | |
d97beb2f | 4774 | declare |
4775 | Result : RList (1 .. List_Length (Static_Predicate (Typ))); | |
4776 | P : Node_Id; | |
4777 | ||
4778 | begin | |
4779 | P := First (Static_Predicate (Typ)); | |
4780 | for J in Result'Range loop | |
4781 | Result (J) := REnt'(Lo_Val (P), Hi_Val (P)); | |
4782 | Next (P); | |
4783 | end loop; | |
4784 | ||
4785 | return Result; | |
4786 | end; | |
4787 | end Stat_Pred; | |
4788 | ||
4789 | -- Start of processing for Build_Static_Predicate | |
4790 | ||
4791 | begin | |
4792 | -- Immediately non-static if our subtype is non static, or we | |
4793 | -- do not have an appropriate discrete subtype in the first place. | |
4794 | ||
4795 | if not Ekind_In (Typ, E_Enumeration_Subtype, | |
4796 | E_Modular_Integer_Subtype, | |
4797 | E_Signed_Integer_Subtype) | |
4798 | or else not Is_Static_Subtype (Typ) | |
9dc88aea | 4799 | then |
4800 | return; | |
4801 | end if; | |
4802 | ||
d97beb2f | 4803 | -- Get bounds of the type |
9dc88aea | 4804 | |
d97beb2f | 4805 | TLo := Expr_Value (Type_Low_Bound (Typ)); |
4806 | THi := Expr_Value (Type_High_Bound (Typ)); | |
9dc88aea | 4807 | |
d97beb2f | 4808 | -- Now analyze the expression to see if it is a static predicate |
9dc88aea | 4809 | |
4810 | declare | |
d97beb2f | 4811 | Ranges : constant RList := Get_RList (Expr); |
4812 | -- Range list from expression if it is static | |
4813 | ||
4814 | Plist : List_Id; | |
4815 | ||
9dc88aea | 4816 | begin |
d97beb2f | 4817 | -- Convert range list into a form for the static predicate. In the |
4818 | -- Ranges array, we just have raw ranges, these must be converted | |
4819 | -- to properly typed and analyzed static expressions or range nodes. | |
9dc88aea | 4820 | |
d97beb2f | 4821 | Plist := New_List; |
9dc88aea | 4822 | |
d97beb2f | 4823 | for J in Ranges'Range loop |
4824 | declare | |
4825 | Lo : constant Uint := Ranges (J).Lo; | |
4826 | Hi : constant Uint := Ranges (J).Hi; | |
9dc88aea | 4827 | |
d97beb2f | 4828 | begin |
4829 | if Lo = Hi then | |
4830 | Append_To (Plist, Build_Val (Lo)); | |
4831 | else | |
4832 | Append_To (Plist, Build_Range (Lo, Hi)); | |
4833 | end if; | |
4834 | end; | |
4835 | end loop; | |
9dc88aea | 4836 | |
d97beb2f | 4837 | -- Processing was successful and all entries were static, so now we |
4838 | -- can store the result as the predicate list. | |
9dc88aea | 4839 | |
d97beb2f | 4840 | Set_Static_Predicate (Typ, Plist); |
9dc88aea | 4841 | |
d97beb2f | 4842 | -- The processing for static predicates put the expression into |
4843 | -- canonical form as a series of ranges. It also eliminated | |
4844 | -- duplicates and collapsed and combined ranges. We might as well | |
4845 | -- replace the alternatives list of the right operand of the | |
4846 | -- membership test with the static predicate list, which will | |
4847 | -- usually be more efficient. | |
9dc88aea | 4848 | |
d97beb2f | 4849 | declare |
4850 | New_Alts : constant List_Id := New_List; | |
4851 | Old_Node : Node_Id; | |
4852 | New_Node : Node_Id; | |
9dc88aea | 4853 | |
d97beb2f | 4854 | begin |
4855 | Old_Node := First (Plist); | |
4856 | while Present (Old_Node) loop | |
4857 | New_Node := New_Copy (Old_Node); | |
9dc88aea | 4858 | |
d97beb2f | 4859 | if Nkind (New_Node) = N_Range then |
4860 | Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node))); | |
4861 | Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node))); | |
4862 | end if; | |
9dc88aea | 4863 | |
d97beb2f | 4864 | Append_To (New_Alts, New_Node); |
4865 | Next (Old_Node); | |
4866 | end loop; | |
9dc88aea | 4867 | |
d97beb2f | 4868 | -- If empty list, replace by True |
9dc88aea | 4869 | |
d97beb2f | 4870 | if Is_Empty_List (New_Alts) then |
4871 | Rewrite (Expr, New_Occurrence_Of (Standard_True, Loc)); | |
4872 | ||
4873 | -- If singleton list, replace by simple membership test | |
4874 | ||
4875 | elsif List_Length (New_Alts) = 1 then | |
4876 | Rewrite (Expr, | |
4877 | Make_In (Loc, | |
4878 | Left_Opnd => Make_Identifier (Loc, Nam), | |
4879 | Right_Opnd => Relocate_Node (First (New_Alts)), | |
4880 | Alternatives => No_List)); | |
4881 | ||
4882 | -- If more than one range, replace by set membership test | |
4883 | ||
4884 | else | |
4885 | Rewrite (Expr, | |
4886 | Make_In (Loc, | |
4887 | Left_Opnd => Make_Identifier (Loc, Nam), | |
4888 | Right_Opnd => Empty, | |
4889 | Alternatives => New_Alts)); | |
4890 | end if; | |
4891 | end; | |
4892 | end; | |
4893 | ||
4894 | -- If non-static, return doing nothing | |
4895 | ||
4896 | exception | |
4897 | when Non_Static => | |
4898 | return; | |
4899 | end Build_Static_Predicate; | |
9dc88aea | 4900 | |
d6f39728 | 4901 | ----------------------------------- |
4902 | -- Check_Constant_Address_Clause -- | |
4903 | ----------------------------------- | |
4904 | ||
4905 | procedure Check_Constant_Address_Clause | |
4906 | (Expr : Node_Id; | |
4907 | U_Ent : Entity_Id) | |
4908 | is | |
4909 | procedure Check_At_Constant_Address (Nod : Node_Id); | |
fdd294d1 | 4910 | -- Checks that the given node N represents a name whose 'Address is |
4911 | -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the | |
4912 | -- address value is the same at the point of declaration of U_Ent and at | |
4913 | -- the time of elaboration of the address clause. | |
d6f39728 | 4914 | |
4915 | procedure Check_Expr_Constants (Nod : Node_Id); | |
fdd294d1 | 4916 | -- Checks that Nod meets the requirements for a constant address clause |
4917 | -- in the sense of the enclosing procedure. | |
d6f39728 | 4918 | |
4919 | procedure Check_List_Constants (Lst : List_Id); | |
4920 | -- Check that all elements of list Lst meet the requirements for a | |
4921 | -- constant address clause in the sense of the enclosing procedure. | |
4922 | ||
4923 | ------------------------------- | |
4924 | -- Check_At_Constant_Address -- | |
4925 | ------------------------------- | |
4926 | ||
4927 | procedure Check_At_Constant_Address (Nod : Node_Id) is | |
4928 | begin | |
4929 | if Is_Entity_Name (Nod) then | |
4930 | if Present (Address_Clause (Entity ((Nod)))) then | |
4931 | Error_Msg_NE | |
4932 | ("invalid address clause for initialized object &!", | |
4933 | Nod, U_Ent); | |
4934 | Error_Msg_NE | |
4935 | ("address for& cannot" & | |
fbc67f84 | 4936 | " depend on another address clause! (RM 13.1(22))!", |
d6f39728 | 4937 | Nod, U_Ent); |
4938 | ||
4939 | elsif In_Same_Source_Unit (Entity (Nod), U_Ent) | |
4940 | and then Sloc (U_Ent) < Sloc (Entity (Nod)) | |
4941 | then | |
4942 | Error_Msg_NE | |
4943 | ("invalid address clause for initialized object &!", | |
4944 | Nod, U_Ent); | |
2f582d72 | 4945 | Error_Msg_Node_2 := U_Ent; |
4946 | Error_Msg_NE | |
4947 | ("\& must be defined before & (RM 13.1(22))!", | |
4948 | Nod, Entity (Nod)); | |
d6f39728 | 4949 | end if; |
4950 | ||
4951 | elsif Nkind (Nod) = N_Selected_Component then | |
4952 | declare | |
4953 | T : constant Entity_Id := Etype (Prefix (Nod)); | |
4954 | ||
4955 | begin | |
4956 | if (Is_Record_Type (T) | |
4957 | and then Has_Discriminants (T)) | |
4958 | or else | |
4959 | (Is_Access_Type (T) | |
4960 | and then Is_Record_Type (Designated_Type (T)) | |
4961 | and then Has_Discriminants (Designated_Type (T))) | |
4962 | then | |
4963 | Error_Msg_NE | |
4964 | ("invalid address clause for initialized object &!", | |
4965 | Nod, U_Ent); | |
4966 | Error_Msg_N | |
4967 | ("\address cannot depend on component" & | |
fbc67f84 | 4968 | " of discriminated record (RM 13.1(22))!", |
d6f39728 | 4969 | Nod); |
4970 | else | |
4971 | Check_At_Constant_Address (Prefix (Nod)); | |
4972 | end if; | |
4973 | end; | |
4974 | ||
4975 | elsif Nkind (Nod) = N_Indexed_Component then | |
4976 | Check_At_Constant_Address (Prefix (Nod)); | |
4977 | Check_List_Constants (Expressions (Nod)); | |
4978 | ||
4979 | else | |
4980 | Check_Expr_Constants (Nod); | |
4981 | end if; | |
4982 | end Check_At_Constant_Address; | |
4983 | ||
4984 | -------------------------- | |
4985 | -- Check_Expr_Constants -- | |
4986 | -------------------------- | |
4987 | ||
4988 | procedure Check_Expr_Constants (Nod : Node_Id) is | |
e7b2d6bc | 4989 | Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent); |
4990 | Ent : Entity_Id := Empty; | |
4991 | ||
d6f39728 | 4992 | begin |
4993 | if Nkind (Nod) in N_Has_Etype | |
4994 | and then Etype (Nod) = Any_Type | |
4995 | then | |
4996 | return; | |
4997 | end if; | |
4998 | ||
4999 | case Nkind (Nod) is | |
5000 | when N_Empty | N_Error => | |
5001 | return; | |
5002 | ||
5003 | when N_Identifier | N_Expanded_Name => | |
e7b2d6bc | 5004 | Ent := Entity (Nod); |
9dfe12ae | 5005 | |
5006 | -- We need to look at the original node if it is different | |
5007 | -- from the node, since we may have rewritten things and | |
5008 | -- substituted an identifier representing the rewrite. | |
5009 | ||
5010 | if Original_Node (Nod) /= Nod then | |
5011 | Check_Expr_Constants (Original_Node (Nod)); | |
5012 | ||
5013 | -- If the node is an object declaration without initial | |
5014 | -- value, some code has been expanded, and the expression | |
5015 | -- is not constant, even if the constituents might be | |
fdd294d1 | 5016 | -- acceptable, as in A'Address + offset. |
9dfe12ae | 5017 | |
e7b2d6bc | 5018 | if Ekind (Ent) = E_Variable |
fdd294d1 | 5019 | and then |
5020 | Nkind (Declaration_Node (Ent)) = N_Object_Declaration | |
9dfe12ae | 5021 | and then |
e7b2d6bc | 5022 | No (Expression (Declaration_Node (Ent))) |
5023 | then | |
5024 | Error_Msg_NE | |
5025 | ("invalid address clause for initialized object &!", | |
5026 | Nod, U_Ent); | |
5027 | ||
5028 | -- If entity is constant, it may be the result of expanding | |
5029 | -- a check. We must verify that its declaration appears | |
5030 | -- before the object in question, else we also reject the | |
5031 | -- address clause. | |
5032 | ||
5033 | elsif Ekind (Ent) = E_Constant | |
5034 | and then In_Same_Source_Unit (Ent, U_Ent) | |
5035 | and then Sloc (Ent) > Loc_U_Ent | |
9dfe12ae | 5036 | then |
5037 | Error_Msg_NE | |
5038 | ("invalid address clause for initialized object &!", | |
5039 | Nod, U_Ent); | |
5040 | end if; | |
e7b2d6bc | 5041 | |
9dfe12ae | 5042 | return; |
5043 | end if; | |
5044 | ||
2866d595 | 5045 | -- Otherwise look at the identifier and see if it is OK |
9dfe12ae | 5046 | |
d3ef794c | 5047 | if Ekind_In (Ent, E_Named_Integer, E_Named_Real) |
5048 | or else Is_Type (Ent) | |
e7b2d6bc | 5049 | then |
5050 | return; | |
d6f39728 | 5051 | |
e7b2d6bc | 5052 | elsif |
5053 | Ekind (Ent) = E_Constant | |
5054 | or else | |
5055 | Ekind (Ent) = E_In_Parameter | |
5056 | then | |
fdd294d1 | 5057 | -- This is the case where we must have Ent defined before |
5058 | -- U_Ent. Clearly if they are in different units this | |
5059 | -- requirement is met since the unit containing Ent is | |
5060 | -- already processed. | |
d6f39728 | 5061 | |
e7b2d6bc | 5062 | if not In_Same_Source_Unit (Ent, U_Ent) then |
5063 | return; | |
d6f39728 | 5064 | |
fdd294d1 | 5065 | -- Otherwise location of Ent must be before the location |
5066 | -- of U_Ent, that's what prior defined means. | |
d6f39728 | 5067 | |
e7b2d6bc | 5068 | elsif Sloc (Ent) < Loc_U_Ent then |
5069 | return; | |
d6f39728 | 5070 | |
5071 | else | |
5072 | Error_Msg_NE | |
5073 | ("invalid address clause for initialized object &!", | |
5074 | Nod, U_Ent); | |
2f582d72 | 5075 | Error_Msg_Node_2 := U_Ent; |
5076 | Error_Msg_NE | |
5077 | ("\& must be defined before & (RM 13.1(22))!", | |
5078 | Nod, Ent); | |
e7b2d6bc | 5079 | end if; |
9dfe12ae | 5080 | |
e7b2d6bc | 5081 | elsif Nkind (Original_Node (Nod)) = N_Function_Call then |
5082 | Check_Expr_Constants (Original_Node (Nod)); | |
5083 | ||
5084 | else | |
5085 | Error_Msg_NE | |
5086 | ("invalid address clause for initialized object &!", | |
5087 | Nod, U_Ent); | |
5088 | ||
5089 | if Comes_From_Source (Ent) then | |
2f582d72 | 5090 | Error_Msg_NE |
5091 | ("\reference to variable& not allowed" | |
5092 | & " (RM 13.1(22))!", Nod, Ent); | |
e7b2d6bc | 5093 | else |
5094 | Error_Msg_N | |
5095 | ("non-static expression not allowed" | |
fbc67f84 | 5096 | & " (RM 13.1(22))!", Nod); |
d6f39728 | 5097 | end if; |
e7b2d6bc | 5098 | end if; |
d6f39728 | 5099 | |
93735cb8 | 5100 | when N_Integer_Literal => |
5101 | ||
5102 | -- If this is a rewritten unchecked conversion, in a system | |
5103 | -- where Address is an integer type, always use the base type | |
5104 | -- for a literal value. This is user-friendly and prevents | |
5105 | -- order-of-elaboration issues with instances of unchecked | |
5106 | -- conversion. | |
5107 | ||
5108 | if Nkind (Original_Node (Nod)) = N_Function_Call then | |
5109 | Set_Etype (Nod, Base_Type (Etype (Nod))); | |
5110 | end if; | |
5111 | ||
5112 | when N_Real_Literal | | |
d6f39728 | 5113 | N_String_Literal | |
5114 | N_Character_Literal => | |
5115 | return; | |
5116 | ||
5117 | when N_Range => | |
5118 | Check_Expr_Constants (Low_Bound (Nod)); | |
5119 | Check_Expr_Constants (High_Bound (Nod)); | |
5120 | ||
5121 | when N_Explicit_Dereference => | |
5122 | Check_Expr_Constants (Prefix (Nod)); | |
5123 | ||
5124 | when N_Indexed_Component => | |
5125 | Check_Expr_Constants (Prefix (Nod)); | |
5126 | Check_List_Constants (Expressions (Nod)); | |
5127 | ||
5128 | when N_Slice => | |
5129 | Check_Expr_Constants (Prefix (Nod)); | |
5130 | Check_Expr_Constants (Discrete_Range (Nod)); | |
5131 | ||
5132 | when N_Selected_Component => | |
5133 | Check_Expr_Constants (Prefix (Nod)); | |
5134 | ||
5135 | when N_Attribute_Reference => | |
9dfe12ae | 5136 | if Attribute_Name (Nod) = Name_Address |
5137 | or else | |
5138 | Attribute_Name (Nod) = Name_Access | |
d6f39728 | 5139 | or else |
9dfe12ae | 5140 | Attribute_Name (Nod) = Name_Unchecked_Access |
d6f39728 | 5141 | or else |
9dfe12ae | 5142 | Attribute_Name (Nod) = Name_Unrestricted_Access |
d6f39728 | 5143 | then |
5144 | Check_At_Constant_Address (Prefix (Nod)); | |
5145 | ||
5146 | else | |
5147 | Check_Expr_Constants (Prefix (Nod)); | |
5148 | Check_List_Constants (Expressions (Nod)); | |
5149 | end if; | |
5150 | ||
5151 | when N_Aggregate => | |
5152 | Check_List_Constants (Component_Associations (Nod)); | |
5153 | Check_List_Constants (Expressions (Nod)); | |
5154 | ||
5155 | when N_Component_Association => | |
5156 | Check_Expr_Constants (Expression (Nod)); | |
5157 | ||
5158 | when N_Extension_Aggregate => | |
5159 | Check_Expr_Constants (Ancestor_Part (Nod)); | |
5160 | Check_List_Constants (Component_Associations (Nod)); | |
5161 | Check_List_Constants (Expressions (Nod)); | |
5162 | ||
5163 | when N_Null => | |
5164 | return; | |
5165 | ||
e7771556 | 5166 | when N_Binary_Op | N_Short_Circuit | N_Membership_Test => |
d6f39728 | 5167 | Check_Expr_Constants (Left_Opnd (Nod)); |
5168 | Check_Expr_Constants (Right_Opnd (Nod)); | |
5169 | ||
5170 | when N_Unary_Op => | |
5171 | Check_Expr_Constants (Right_Opnd (Nod)); | |
5172 | ||
5173 | when N_Type_Conversion | | |
5174 | N_Qualified_Expression | | |
5175 | N_Allocator => | |
5176 | Check_Expr_Constants (Expression (Nod)); | |
5177 | ||
5178 | when N_Unchecked_Type_Conversion => | |
5179 | Check_Expr_Constants (Expression (Nod)); | |
5180 | ||
fdd294d1 | 5181 | -- If this is a rewritten unchecked conversion, subtypes in |
5182 | -- this node are those created within the instance. To avoid | |
5183 | -- order of elaboration issues, replace them with their base | |
5184 | -- types. Note that address clauses can cause order of | |
5185 | -- elaboration problems because they are elaborated by the | |
5186 | -- back-end at the point of definition, and may mention | |
5187 | -- entities declared in between (as long as everything is | |
5188 | -- static). It is user-friendly to allow unchecked conversions | |
5189 | -- in this context. | |
d6f39728 | 5190 | |
5191 | if Nkind (Original_Node (Nod)) = N_Function_Call then | |
5192 | Set_Etype (Expression (Nod), | |
5193 | Base_Type (Etype (Expression (Nod)))); | |
5194 | Set_Etype (Nod, Base_Type (Etype (Nod))); | |
5195 | end if; | |
5196 | ||
5197 | when N_Function_Call => | |
5198 | if not Is_Pure (Entity (Name (Nod))) then | |
5199 | Error_Msg_NE | |
5200 | ("invalid address clause for initialized object &!", | |
5201 | Nod, U_Ent); | |
5202 | ||
5203 | Error_Msg_NE | |
fbc67f84 | 5204 | ("\function & is not pure (RM 13.1(22))!", |
d6f39728 | 5205 | Nod, Entity (Name (Nod))); |
5206 | ||
5207 | else | |
5208 | Check_List_Constants (Parameter_Associations (Nod)); | |
5209 | end if; | |
5210 | ||
5211 | when N_Parameter_Association => | |
5212 | Check_Expr_Constants (Explicit_Actual_Parameter (Nod)); | |
5213 | ||
5214 | when others => | |
5215 | Error_Msg_NE | |
5216 | ("invalid address clause for initialized object &!", | |
5217 | Nod, U_Ent); | |
5218 | Error_Msg_NE | |
fbc67f84 | 5219 | ("\must be constant defined before& (RM 13.1(22))!", |
d6f39728 | 5220 | Nod, U_Ent); |
5221 | end case; | |
5222 | end Check_Expr_Constants; | |
5223 | ||
5224 | -------------------------- | |
5225 | -- Check_List_Constants -- | |
5226 | -------------------------- | |
5227 | ||
5228 | procedure Check_List_Constants (Lst : List_Id) is | |
5229 | Nod1 : Node_Id; | |
5230 | ||
5231 | begin | |
5232 | if Present (Lst) then | |
5233 | Nod1 := First (Lst); | |
5234 | while Present (Nod1) loop | |
5235 | Check_Expr_Constants (Nod1); | |
5236 | Next (Nod1); | |
5237 | end loop; | |
5238 | end if; | |
5239 | end Check_List_Constants; | |
5240 | ||
5241 | -- Start of processing for Check_Constant_Address_Clause | |
5242 | ||
5243 | begin | |
01cb2726 | 5244 | -- If rep_clauses are to be ignored, no need for legality checks. In |
5245 | -- particular, no need to pester user about rep clauses that violate | |
5246 | -- the rule on constant addresses, given that these clauses will be | |
5247 | -- removed by Freeze before they reach the back end. | |
5248 | ||
5249 | if not Ignore_Rep_Clauses then | |
5250 | Check_Expr_Constants (Expr); | |
5251 | end if; | |
d6f39728 | 5252 | end Check_Constant_Address_Clause; |
5253 | ||
67278d60 | 5254 | ---------------------------------------- |
5255 | -- Check_Record_Representation_Clause -- | |
5256 | ---------------------------------------- | |
5257 | ||
5258 | procedure Check_Record_Representation_Clause (N : Node_Id) is | |
5259 | Loc : constant Source_Ptr := Sloc (N); | |
5260 | Ident : constant Node_Id := Identifier (N); | |
5261 | Rectype : Entity_Id; | |
5262 | Fent : Entity_Id; | |
5263 | CC : Node_Id; | |
5264 | Fbit : Uint; | |
5265 | Lbit : Uint; | |
5266 | Hbit : Uint := Uint_0; | |
5267 | Comp : Entity_Id; | |
5268 | Pcomp : Entity_Id; | |
5269 | ||
5270 | Max_Bit_So_Far : Uint; | |
5271 | -- Records the maximum bit position so far. If all field positions | |
5272 | -- are monotonically increasing, then we can skip the circuit for | |
5273 | -- checking for overlap, since no overlap is possible. | |
5274 | ||
5275 | Tagged_Parent : Entity_Id := Empty; | |
5276 | -- This is set in the case of a derived tagged type for which we have | |
5277 | -- Is_Fully_Repped_Tagged_Type True (indicating that all components are | |
5278 | -- positioned by record representation clauses). In this case we must | |
5279 | -- check for overlap between components of this tagged type, and the | |
5280 | -- components of its parent. Tagged_Parent will point to this parent | |
5281 | -- type. For all other cases Tagged_Parent is left set to Empty. | |
5282 | ||
5283 | Parent_Last_Bit : Uint; | |
5284 | -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the | |
5285 | -- last bit position for any field in the parent type. We only need to | |
5286 | -- check overlap for fields starting below this point. | |
5287 | ||
5288 | Overlap_Check_Required : Boolean; | |
5289 | -- Used to keep track of whether or not an overlap check is required | |
5290 | ||
47495553 | 5291 | Overlap_Detected : Boolean := False; |
5292 | -- Set True if an overlap is detected | |
5293 | ||
67278d60 | 5294 | Ccount : Natural := 0; |
5295 | -- Number of component clauses in record rep clause | |
5296 | ||
5297 | procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id); | |
5298 | -- Given two entities for record components or discriminants, checks | |
5299 | -- if they have overlapping component clauses and issues errors if so. | |
5300 | ||
5301 | procedure Find_Component; | |
5302 | -- Finds component entity corresponding to current component clause (in | |
5303 | -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin | |
5304 | -- start/stop bits for the field. If there is no matching component or | |
5305 | -- if the matching component does not have a component clause, then | |
5306 | -- that's an error and Comp is set to Empty, but no error message is | |
5307 | -- issued, since the message was already given. Comp is also set to | |
5308 | -- Empty if the current "component clause" is in fact a pragma. | |
5309 | ||
5310 | ----------------------------- | |
5311 | -- Check_Component_Overlap -- | |
5312 | ----------------------------- | |
5313 | ||
5314 | procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is | |
5315 | CC1 : constant Node_Id := Component_Clause (C1_Ent); | |
5316 | CC2 : constant Node_Id := Component_Clause (C2_Ent); | |
47495553 | 5317 | |
67278d60 | 5318 | begin |
5319 | if Present (CC1) and then Present (CC2) then | |
5320 | ||
5321 | -- Exclude odd case where we have two tag fields in the same | |
5322 | -- record, both at location zero. This seems a bit strange, but | |
5323 | -- it seems to happen in some circumstances, perhaps on an error. | |
5324 | ||
5325 | if Chars (C1_Ent) = Name_uTag | |
5326 | and then | |
5327 | Chars (C2_Ent) = Name_uTag | |
5328 | then | |
5329 | return; | |
5330 | end if; | |
5331 | ||
5332 | -- Here we check if the two fields overlap | |
5333 | ||
5334 | declare | |
5335 | S1 : constant Uint := Component_Bit_Offset (C1_Ent); | |
5336 | S2 : constant Uint := Component_Bit_Offset (C2_Ent); | |
5337 | E1 : constant Uint := S1 + Esize (C1_Ent); | |
5338 | E2 : constant Uint := S2 + Esize (C2_Ent); | |
5339 | ||
5340 | begin | |
5341 | if E2 <= S1 or else E1 <= S2 then | |
5342 | null; | |
5343 | else | |
5344 | Error_Msg_Node_2 := Component_Name (CC2); | |
5345 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
5346 | Error_Msg_Node_1 := Component_Name (CC1); | |
5347 | Error_Msg_N | |
5348 | ("component& overlaps & #", Component_Name (CC1)); | |
47495553 | 5349 | Overlap_Detected := True; |
67278d60 | 5350 | end if; |
5351 | end; | |
5352 | end if; | |
5353 | end Check_Component_Overlap; | |
5354 | ||
5355 | -------------------- | |
5356 | -- Find_Component -- | |
5357 | -------------------- | |
5358 | ||
5359 | procedure Find_Component is | |
5360 | ||
5361 | procedure Search_Component (R : Entity_Id); | |
5362 | -- Search components of R for a match. If found, Comp is set. | |
5363 | ||
5364 | ---------------------- | |
5365 | -- Search_Component -- | |
5366 | ---------------------- | |
5367 | ||
5368 | procedure Search_Component (R : Entity_Id) is | |
5369 | begin | |
5370 | Comp := First_Component_Or_Discriminant (R); | |
5371 | while Present (Comp) loop | |
5372 | ||
5373 | -- Ignore error of attribute name for component name (we | |
5374 | -- already gave an error message for this, so no need to | |
5375 | -- complain here) | |
5376 | ||
5377 | if Nkind (Component_Name (CC)) = N_Attribute_Reference then | |
5378 | null; | |
5379 | else | |
5380 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
5381 | end if; | |
5382 | ||
5383 | Next_Component_Or_Discriminant (Comp); | |
5384 | end loop; | |
5385 | end Search_Component; | |
5386 | ||
5387 | -- Start of processing for Find_Component | |
5388 | ||
5389 | begin | |
5390 | -- Return with Comp set to Empty if we have a pragma | |
5391 | ||
5392 | if Nkind (CC) = N_Pragma then | |
5393 | Comp := Empty; | |
5394 | return; | |
5395 | end if; | |
5396 | ||
5397 | -- Search current record for matching component | |
5398 | ||
5399 | Search_Component (Rectype); | |
5400 | ||
5401 | -- If not found, maybe component of base type that is absent from | |
5402 | -- statically constrained first subtype. | |
5403 | ||
5404 | if No (Comp) then | |
5405 | Search_Component (Base_Type (Rectype)); | |
5406 | end if; | |
5407 | ||
5408 | -- If no component, or the component does not reference the component | |
5409 | -- clause in question, then there was some previous error for which | |
5410 | -- we already gave a message, so just return with Comp Empty. | |
5411 | ||
5412 | if No (Comp) | |
5413 | or else Component_Clause (Comp) /= CC | |
5414 | then | |
5415 | Comp := Empty; | |
5416 | ||
5417 | -- Normal case where we have a component clause | |
5418 | ||
5419 | else | |
5420 | Fbit := Component_Bit_Offset (Comp); | |
5421 | Lbit := Fbit + Esize (Comp) - 1; | |
5422 | end if; | |
5423 | end Find_Component; | |
5424 | ||
5425 | -- Start of processing for Check_Record_Representation_Clause | |
5426 | ||
5427 | begin | |
5428 | Find_Type (Ident); | |
5429 | Rectype := Entity (Ident); | |
5430 | ||
5431 | if Rectype = Any_Type then | |
5432 | return; | |
5433 | else | |
5434 | Rectype := Underlying_Type (Rectype); | |
5435 | end if; | |
5436 | ||
5437 | -- See if we have a fully repped derived tagged type | |
5438 | ||
5439 | declare | |
5440 | PS : constant Entity_Id := Parent_Subtype (Rectype); | |
5441 | ||
5442 | begin | |
5443 | if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then | |
5444 | Tagged_Parent := PS; | |
5445 | ||
5446 | -- Find maximum bit of any component of the parent type | |
5447 | ||
5448 | Parent_Last_Bit := UI_From_Int (System_Address_Size - 1); | |
5449 | Pcomp := First_Entity (Tagged_Parent); | |
5450 | while Present (Pcomp) loop | |
5451 | if Ekind_In (Pcomp, E_Discriminant, E_Component) then | |
5452 | if Component_Bit_Offset (Pcomp) /= No_Uint | |
5453 | and then Known_Static_Esize (Pcomp) | |
5454 | then | |
5455 | Parent_Last_Bit := | |
5456 | UI_Max | |
5457 | (Parent_Last_Bit, | |
5458 | Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1); | |
5459 | end if; | |
5460 | ||
5461 | Next_Entity (Pcomp); | |
5462 | end if; | |
5463 | end loop; | |
5464 | end if; | |
5465 | end; | |
5466 | ||
5467 | -- All done if no component clauses | |
5468 | ||
5469 | CC := First (Component_Clauses (N)); | |
5470 | ||
5471 | if No (CC) then | |
5472 | return; | |
5473 | end if; | |
5474 | ||
5475 | -- If a tag is present, then create a component clause that places it | |
5476 | -- at the start of the record (otherwise gigi may place it after other | |
5477 | -- fields that have rep clauses). | |
5478 | ||
5479 | Fent := First_Entity (Rectype); | |
5480 | ||
5481 | if Nkind (Fent) = N_Defining_Identifier | |
5482 | and then Chars (Fent) = Name_uTag | |
5483 | then | |
5484 | Set_Component_Bit_Offset (Fent, Uint_0); | |
5485 | Set_Normalized_Position (Fent, Uint_0); | |
5486 | Set_Normalized_First_Bit (Fent, Uint_0); | |
5487 | Set_Normalized_Position_Max (Fent, Uint_0); | |
5488 | Init_Esize (Fent, System_Address_Size); | |
5489 | ||
5490 | Set_Component_Clause (Fent, | |
5491 | Make_Component_Clause (Loc, | |
5492 | Component_Name => | |
5493 | Make_Identifier (Loc, | |
5494 | Chars => Name_uTag), | |
5495 | ||
5496 | Position => | |
5497 | Make_Integer_Literal (Loc, | |
5498 | Intval => Uint_0), | |
5499 | ||
5500 | First_Bit => | |
5501 | Make_Integer_Literal (Loc, | |
5502 | Intval => Uint_0), | |
5503 | ||
5504 | Last_Bit => | |
5505 | Make_Integer_Literal (Loc, | |
5506 | UI_From_Int (System_Address_Size)))); | |
5507 | ||
5508 | Ccount := Ccount + 1; | |
5509 | end if; | |
5510 | ||
5511 | Max_Bit_So_Far := Uint_Minus_1; | |
5512 | Overlap_Check_Required := False; | |
5513 | ||
5514 | -- Process the component clauses | |
5515 | ||
5516 | while Present (CC) loop | |
5517 | Find_Component; | |
5518 | ||
5519 | if Present (Comp) then | |
5520 | Ccount := Ccount + 1; | |
5521 | ||
47495553 | 5522 | -- We need a full overlap check if record positions non-monotonic |
5523 | ||
67278d60 | 5524 | if Fbit <= Max_Bit_So_Far then |
5525 | Overlap_Check_Required := True; | |
67278d60 | 5526 | end if; |
5527 | ||
47495553 | 5528 | Max_Bit_So_Far := Lbit; |
5529 | ||
67278d60 | 5530 | -- Check bit position out of range of specified size |
5531 | ||
5532 | if Has_Size_Clause (Rectype) | |
5533 | and then Esize (Rectype) <= Lbit | |
5534 | then | |
5535 | Error_Msg_N | |
5536 | ("bit number out of range of specified size", | |
5537 | Last_Bit (CC)); | |
5538 | ||
5539 | -- Check for overlap with tag field | |
5540 | ||
5541 | else | |
5542 | if Is_Tagged_Type (Rectype) | |
5543 | and then Fbit < System_Address_Size | |
5544 | then | |
5545 | Error_Msg_NE | |
5546 | ("component overlaps tag field of&", | |
5547 | Component_Name (CC), Rectype); | |
47495553 | 5548 | Overlap_Detected := True; |
67278d60 | 5549 | end if; |
5550 | ||
5551 | if Hbit < Lbit then | |
5552 | Hbit := Lbit; | |
5553 | end if; | |
5554 | end if; | |
5555 | ||
5556 | -- Check parent overlap if component might overlap parent field | |
5557 | ||
5558 | if Present (Tagged_Parent) | |
5559 | and then Fbit <= Parent_Last_Bit | |
5560 | then | |
5561 | Pcomp := First_Component_Or_Discriminant (Tagged_Parent); | |
5562 | while Present (Pcomp) loop | |
5563 | if not Is_Tag (Pcomp) | |
5564 | and then Chars (Pcomp) /= Name_uParent | |
5565 | then | |
5566 | Check_Component_Overlap (Comp, Pcomp); | |
5567 | end if; | |
5568 | ||
5569 | Next_Component_Or_Discriminant (Pcomp); | |
5570 | end loop; | |
5571 | end if; | |
5572 | end if; | |
5573 | ||
5574 | Next (CC); | |
5575 | end loop; | |
5576 | ||
5577 | -- Now that we have processed all the component clauses, check for | |
5578 | -- overlap. We have to leave this till last, since the components can | |
5579 | -- appear in any arbitrary order in the representation clause. | |
5580 | ||
5581 | -- We do not need this check if all specified ranges were monotonic, | |
5582 | -- as recorded by Overlap_Check_Required being False at this stage. | |
5583 | ||
5584 | -- This first section checks if there are any overlapping entries at | |
5585 | -- all. It does this by sorting all entries and then seeing if there are | |
5586 | -- any overlaps. If there are none, then that is decisive, but if there | |
5587 | -- are overlaps, they may still be OK (they may result from fields in | |
5588 | -- different variants). | |
5589 | ||
5590 | if Overlap_Check_Required then | |
5591 | Overlap_Check1 : declare | |
5592 | ||
5593 | OC_Fbit : array (0 .. Ccount) of Uint; | |
5594 | -- First-bit values for component clauses, the value is the offset | |
5595 | -- of the first bit of the field from start of record. The zero | |
5596 | -- entry is for use in sorting. | |
5597 | ||
5598 | OC_Lbit : array (0 .. Ccount) of Uint; | |
5599 | -- Last-bit values for component clauses, the value is the offset | |
5600 | -- of the last bit of the field from start of record. The zero | |
5601 | -- entry is for use in sorting. | |
5602 | ||
5603 | OC_Count : Natural := 0; | |
5604 | -- Count of entries in OC_Fbit and OC_Lbit | |
5605 | ||
5606 | function OC_Lt (Op1, Op2 : Natural) return Boolean; | |
5607 | -- Compare routine for Sort | |
5608 | ||
5609 | procedure OC_Move (From : Natural; To : Natural); | |
5610 | -- Move routine for Sort | |
5611 | ||
5612 | package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt); | |
5613 | ||
5614 | ----------- | |
5615 | -- OC_Lt -- | |
5616 | ----------- | |
5617 | ||
5618 | function OC_Lt (Op1, Op2 : Natural) return Boolean is | |
5619 | begin | |
5620 | return OC_Fbit (Op1) < OC_Fbit (Op2); | |
5621 | end OC_Lt; | |
5622 | ||
5623 | ------------- | |
5624 | -- OC_Move -- | |
5625 | ------------- | |
5626 | ||
5627 | procedure OC_Move (From : Natural; To : Natural) is | |
5628 | begin | |
5629 | OC_Fbit (To) := OC_Fbit (From); | |
5630 | OC_Lbit (To) := OC_Lbit (From); | |
5631 | end OC_Move; | |
5632 | ||
5633 | -- Start of processing for Overlap_Check | |
5634 | ||
5635 | begin | |
5636 | CC := First (Component_Clauses (N)); | |
5637 | while Present (CC) loop | |
5638 | ||
5639 | -- Exclude component clause already marked in error | |
5640 | ||
5641 | if not Error_Posted (CC) then | |
5642 | Find_Component; | |
5643 | ||
5644 | if Present (Comp) then | |
5645 | OC_Count := OC_Count + 1; | |
5646 | OC_Fbit (OC_Count) := Fbit; | |
5647 | OC_Lbit (OC_Count) := Lbit; | |
5648 | end if; | |
5649 | end if; | |
5650 | ||
5651 | Next (CC); | |
5652 | end loop; | |
5653 | ||
5654 | Sorting.Sort (OC_Count); | |
5655 | ||
5656 | Overlap_Check_Required := False; | |
5657 | for J in 1 .. OC_Count - 1 loop | |
5658 | if OC_Lbit (J) >= OC_Fbit (J + 1) then | |
5659 | Overlap_Check_Required := True; | |
5660 | exit; | |
5661 | end if; | |
5662 | end loop; | |
5663 | end Overlap_Check1; | |
5664 | end if; | |
5665 | ||
5666 | -- If Overlap_Check_Required is still True, then we have to do the full | |
5667 | -- scale overlap check, since we have at least two fields that do | |
5668 | -- overlap, and we need to know if that is OK since they are in | |
5669 | -- different variant, or whether we have a definite problem. | |
5670 | ||
5671 | if Overlap_Check_Required then | |
5672 | Overlap_Check2 : declare | |
5673 | C1_Ent, C2_Ent : Entity_Id; | |
5674 | -- Entities of components being checked for overlap | |
5675 | ||
5676 | Clist : Node_Id; | |
5677 | -- Component_List node whose Component_Items are being checked | |
5678 | ||
5679 | Citem : Node_Id; | |
5680 | -- Component declaration for component being checked | |
5681 | ||
5682 | begin | |
5683 | C1_Ent := First_Entity (Base_Type (Rectype)); | |
5684 | ||
5685 | -- Loop through all components in record. For each component check | |
5686 | -- for overlap with any of the preceding elements on the component | |
5687 | -- list containing the component and also, if the component is in | |
5688 | -- a variant, check against components outside the case structure. | |
5689 | -- This latter test is repeated recursively up the variant tree. | |
5690 | ||
5691 | Main_Component_Loop : while Present (C1_Ent) loop | |
5692 | if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then | |
5693 | goto Continue_Main_Component_Loop; | |
5694 | end if; | |
5695 | ||
5696 | -- Skip overlap check if entity has no declaration node. This | |
5697 | -- happens with discriminants in constrained derived types. | |
47495553 | 5698 | -- Possibly we are missing some checks as a result, but that |
5699 | -- does not seem terribly serious. | |
67278d60 | 5700 | |
5701 | if No (Declaration_Node (C1_Ent)) then | |
5702 | goto Continue_Main_Component_Loop; | |
5703 | end if; | |
5704 | ||
5705 | Clist := Parent (List_Containing (Declaration_Node (C1_Ent))); | |
5706 | ||
5707 | -- Loop through component lists that need checking. Check the | |
5708 | -- current component list and all lists in variants above us. | |
5709 | ||
5710 | Component_List_Loop : loop | |
5711 | ||
5712 | -- If derived type definition, go to full declaration | |
5713 | -- If at outer level, check discriminants if there are any. | |
5714 | ||
5715 | if Nkind (Clist) = N_Derived_Type_Definition then | |
5716 | Clist := Parent (Clist); | |
5717 | end if; | |
5718 | ||
5719 | -- Outer level of record definition, check discriminants | |
5720 | ||
5721 | if Nkind_In (Clist, N_Full_Type_Declaration, | |
5722 | N_Private_Type_Declaration) | |
5723 | then | |
5724 | if Has_Discriminants (Defining_Identifier (Clist)) then | |
5725 | C2_Ent := | |
5726 | First_Discriminant (Defining_Identifier (Clist)); | |
5727 | while Present (C2_Ent) loop | |
5728 | exit when C1_Ent = C2_Ent; | |
5729 | Check_Component_Overlap (C1_Ent, C2_Ent); | |
5730 | Next_Discriminant (C2_Ent); | |
5731 | end loop; | |
5732 | end if; | |
5733 | ||
5734 | -- Record extension case | |
5735 | ||
5736 | elsif Nkind (Clist) = N_Derived_Type_Definition then | |
5737 | Clist := Empty; | |
5738 | ||
5739 | -- Otherwise check one component list | |
5740 | ||
5741 | else | |
5742 | Citem := First (Component_Items (Clist)); | |
67278d60 | 5743 | while Present (Citem) loop |
5744 | if Nkind (Citem) = N_Component_Declaration then | |
5745 | C2_Ent := Defining_Identifier (Citem); | |
5746 | exit when C1_Ent = C2_Ent; | |
5747 | Check_Component_Overlap (C1_Ent, C2_Ent); | |
5748 | end if; | |
5749 | ||
5750 | Next (Citem); | |
5751 | end loop; | |
5752 | end if; | |
5753 | ||
5754 | -- Check for variants above us (the parent of the Clist can | |
5755 | -- be a variant, in which case its parent is a variant part, | |
5756 | -- and the parent of the variant part is a component list | |
5757 | -- whose components must all be checked against the current | |
5758 | -- component for overlap). | |
5759 | ||
5760 | if Nkind (Parent (Clist)) = N_Variant then | |
5761 | Clist := Parent (Parent (Parent (Clist))); | |
5762 | ||
5763 | -- Check for possible discriminant part in record, this | |
5764 | -- is treated essentially as another level in the | |
5765 | -- recursion. For this case the parent of the component | |
5766 | -- list is the record definition, and its parent is the | |
5767 | -- full type declaration containing the discriminant | |
5768 | -- specifications. | |
5769 | ||
5770 | elsif Nkind (Parent (Clist)) = N_Record_Definition then | |
5771 | Clist := Parent (Parent ((Clist))); | |
5772 | ||
5773 | -- If neither of these two cases, we are at the top of | |
5774 | -- the tree. | |
5775 | ||
5776 | else | |
5777 | exit Component_List_Loop; | |
5778 | end if; | |
5779 | end loop Component_List_Loop; | |
5780 | ||
5781 | <<Continue_Main_Component_Loop>> | |
5782 | Next_Entity (C1_Ent); | |
5783 | ||
5784 | end loop Main_Component_Loop; | |
5785 | end Overlap_Check2; | |
5786 | end if; | |
5787 | ||
47495553 | 5788 | -- The following circuit deals with warning on record holes (gaps). We |
5789 | -- skip this check if overlap was detected, since it makes sense for the | |
5790 | -- programmer to fix this illegality before worrying about warnings. | |
5791 | ||
5792 | if not Overlap_Detected and Warn_On_Record_Holes then | |
5793 | Record_Hole_Check : declare | |
5794 | Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype)); | |
5795 | -- Full declaration of record type | |
5796 | ||
5797 | procedure Check_Component_List | |
5798 | (CL : Node_Id; | |
5799 | Sbit : Uint; | |
5800 | DS : List_Id); | |
5801 | -- Check component list CL for holes. The starting bit should be | |
5802 | -- Sbit. which is zero for the main record component list and set | |
5803 | -- appropriately for recursive calls for variants. DS is set to | |
5804 | -- a list of discriminant specifications to be included in the | |
5805 | -- consideration of components. It is No_List if none to consider. | |
5806 | ||
5807 | -------------------------- | |
5808 | -- Check_Component_List -- | |
5809 | -------------------------- | |
5810 | ||
5811 | procedure Check_Component_List | |
5812 | (CL : Node_Id; | |
5813 | Sbit : Uint; | |
5814 | DS : List_Id) | |
5815 | is | |
5816 | Compl : Integer; | |
5817 | ||
5818 | begin | |
5819 | Compl := Integer (List_Length (Component_Items (CL))); | |
5820 | ||
5821 | if DS /= No_List then | |
5822 | Compl := Compl + Integer (List_Length (DS)); | |
5823 | end if; | |
5824 | ||
5825 | declare | |
5826 | Comps : array (Natural range 0 .. Compl) of Entity_Id; | |
5827 | -- Gather components (zero entry is for sort routine) | |
5828 | ||
5829 | Ncomps : Natural := 0; | |
5830 | -- Number of entries stored in Comps (starting at Comps (1)) | |
5831 | ||
5832 | Citem : Node_Id; | |
5833 | -- One component item or discriminant specification | |
5834 | ||
5835 | Nbit : Uint; | |
5836 | -- Starting bit for next component | |
5837 | ||
5838 | CEnt : Entity_Id; | |
5839 | -- Component entity | |
5840 | ||
5841 | Variant : Node_Id; | |
5842 | -- One variant | |
5843 | ||
5844 | function Lt (Op1, Op2 : Natural) return Boolean; | |
5845 | -- Compare routine for Sort | |
5846 | ||
5847 | procedure Move (From : Natural; To : Natural); | |
5848 | -- Move routine for Sort | |
5849 | ||
5850 | package Sorting is new GNAT.Heap_Sort_G (Move, Lt); | |
5851 | ||
5852 | -------- | |
5853 | -- Lt -- | |
5854 | -------- | |
5855 | ||
5856 | function Lt (Op1, Op2 : Natural) return Boolean is | |
5857 | begin | |
5858 | return Component_Bit_Offset (Comps (Op1)) | |
5859 | < | |
5860 | Component_Bit_Offset (Comps (Op2)); | |
5861 | end Lt; | |
5862 | ||
5863 | ---------- | |
5864 | -- Move -- | |
5865 | ---------- | |
5866 | ||
5867 | procedure Move (From : Natural; To : Natural) is | |
5868 | begin | |
5869 | Comps (To) := Comps (From); | |
5870 | end Move; | |
5871 | ||
5872 | begin | |
5873 | -- Gather discriminants into Comp | |
5874 | ||
5875 | if DS /= No_List then | |
5876 | Citem := First (DS); | |
5877 | while Present (Citem) loop | |
5878 | if Nkind (Citem) = N_Discriminant_Specification then | |
5879 | declare | |
5880 | Ent : constant Entity_Id := | |
5881 | Defining_Identifier (Citem); | |
5882 | begin | |
5883 | if Ekind (Ent) = E_Discriminant then | |
5884 | Ncomps := Ncomps + 1; | |
5885 | Comps (Ncomps) := Ent; | |
5886 | end if; | |
5887 | end; | |
5888 | end if; | |
5889 | ||
5890 | Next (Citem); | |
5891 | end loop; | |
5892 | end if; | |
5893 | ||
5894 | -- Gather component entities into Comp | |
5895 | ||
5896 | Citem := First (Component_Items (CL)); | |
5897 | while Present (Citem) loop | |
5898 | if Nkind (Citem) = N_Component_Declaration then | |
5899 | Ncomps := Ncomps + 1; | |
5900 | Comps (Ncomps) := Defining_Identifier (Citem); | |
5901 | end if; | |
5902 | ||
5903 | Next (Citem); | |
5904 | end loop; | |
5905 | ||
5906 | -- Now sort the component entities based on the first bit. | |
5907 | -- Note we already know there are no overlapping components. | |
5908 | ||
5909 | Sorting.Sort (Ncomps); | |
5910 | ||
5911 | -- Loop through entries checking for holes | |
5912 | ||
5913 | Nbit := Sbit; | |
5914 | for J in 1 .. Ncomps loop | |
5915 | CEnt := Comps (J); | |
5916 | Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit; | |
5917 | ||
5918 | if Error_Msg_Uint_1 > 0 then | |
5919 | Error_Msg_NE | |
5920 | ("?^-bit gap before component&", | |
5921 | Component_Name (Component_Clause (CEnt)), CEnt); | |
5922 | end if; | |
5923 | ||
5924 | Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt); | |
5925 | end loop; | |
5926 | ||
5927 | -- Process variant parts recursively if present | |
5928 | ||
5929 | if Present (Variant_Part (CL)) then | |
5930 | Variant := First (Variants (Variant_Part (CL))); | |
5931 | while Present (Variant) loop | |
5932 | Check_Component_List | |
5933 | (Component_List (Variant), Nbit, No_List); | |
5934 | Next (Variant); | |
5935 | end loop; | |
5936 | end if; | |
5937 | end; | |
5938 | end Check_Component_List; | |
5939 | ||
5940 | -- Start of processing for Record_Hole_Check | |
5941 | ||
5942 | begin | |
5943 | declare | |
5944 | Sbit : Uint; | |
5945 | ||
5946 | begin | |
5947 | if Is_Tagged_Type (Rectype) then | |
5948 | Sbit := UI_From_Int (System_Address_Size); | |
5949 | else | |
5950 | Sbit := Uint_0; | |
5951 | end if; | |
5952 | ||
5953 | if Nkind (Decl) = N_Full_Type_Declaration | |
5954 | and then Nkind (Type_Definition (Decl)) = N_Record_Definition | |
5955 | then | |
5956 | Check_Component_List | |
5957 | (Component_List (Type_Definition (Decl)), | |
5958 | Sbit, | |
5959 | Discriminant_Specifications (Decl)); | |
5960 | end if; | |
5961 | end; | |
5962 | end Record_Hole_Check; | |
5963 | end if; | |
5964 | ||
67278d60 | 5965 | -- For records that have component clauses for all components, and whose |
5966 | -- size is less than or equal to 32, we need to know the size in the | |
5967 | -- front end to activate possible packed array processing where the | |
5968 | -- component type is a record. | |
5969 | ||
5970 | -- At this stage Hbit + 1 represents the first unused bit from all the | |
5971 | -- component clauses processed, so if the component clauses are | |
5972 | -- complete, then this is the length of the record. | |
5973 | ||
5974 | -- For records longer than System.Storage_Unit, and for those where not | |
5975 | -- all components have component clauses, the back end determines the | |
5976 | -- length (it may for example be appropriate to round up the size | |
5977 | -- to some convenient boundary, based on alignment considerations, etc). | |
5978 | ||
5979 | if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then | |
5980 | ||
5981 | -- Nothing to do if at least one component has no component clause | |
5982 | ||
5983 | Comp := First_Component_Or_Discriminant (Rectype); | |
5984 | while Present (Comp) loop | |
5985 | exit when No (Component_Clause (Comp)); | |
5986 | Next_Component_Or_Discriminant (Comp); | |
5987 | end loop; | |
5988 | ||
5989 | -- If we fall out of loop, all components have component clauses | |
5990 | -- and so we can set the size to the maximum value. | |
5991 | ||
5992 | if No (Comp) then | |
5993 | Set_RM_Size (Rectype, Hbit + 1); | |
5994 | end if; | |
5995 | end if; | |
5996 | end Check_Record_Representation_Clause; | |
5997 | ||
d6f39728 | 5998 | ---------------- |
5999 | -- Check_Size -- | |
6000 | ---------------- | |
6001 | ||
6002 | procedure Check_Size | |
6003 | (N : Node_Id; | |
6004 | T : Entity_Id; | |
6005 | Siz : Uint; | |
6006 | Biased : out Boolean) | |
6007 | is | |
6008 | UT : constant Entity_Id := Underlying_Type (T); | |
6009 | M : Uint; | |
6010 | ||
6011 | begin | |
6012 | Biased := False; | |
6013 | ||
ea61a7ea | 6014 | -- Dismiss cases for generic types or types with previous errors |
d6f39728 | 6015 | |
6016 | if No (UT) | |
6017 | or else UT = Any_Type | |
6018 | or else Is_Generic_Type (UT) | |
6019 | or else Is_Generic_Type (Root_Type (UT)) | |
d6f39728 | 6020 | then |
6021 | return; | |
6022 | ||
ea61a7ea | 6023 | -- Check case of bit packed array |
6024 | ||
6025 | elsif Is_Array_Type (UT) | |
6026 | and then Known_Static_Component_Size (UT) | |
6027 | and then Is_Bit_Packed_Array (UT) | |
6028 | then | |
6029 | declare | |
6030 | Asiz : Uint; | |
6031 | Indx : Node_Id; | |
6032 | Ityp : Entity_Id; | |
6033 | ||
6034 | begin | |
6035 | Asiz := Component_Size (UT); | |
6036 | Indx := First_Index (UT); | |
6037 | loop | |
6038 | Ityp := Etype (Indx); | |
6039 | ||
6040 | -- If non-static bound, then we are not in the business of | |
6041 | -- trying to check the length, and indeed an error will be | |
6042 | -- issued elsewhere, since sizes of non-static array types | |
6043 | -- cannot be set implicitly or explicitly. | |
6044 | ||
6045 | if not Is_Static_Subtype (Ityp) then | |
6046 | return; | |
6047 | end if; | |
6048 | ||
6049 | -- Otherwise accumulate next dimension | |
6050 | ||
6051 | Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) - | |
6052 | Expr_Value (Type_Low_Bound (Ityp)) + | |
6053 | Uint_1); | |
6054 | ||
6055 | Next_Index (Indx); | |
6056 | exit when No (Indx); | |
6057 | end loop; | |
6058 | ||
6059 | if Asiz <= Siz then | |
6060 | return; | |
6061 | else | |
6062 | Error_Msg_Uint_1 := Asiz; | |
6063 | Error_Msg_NE | |
6064 | ("size for& too small, minimum allowed is ^", N, T); | |
37cb33b0 | 6065 | Set_Esize (T, Asiz); |
6066 | Set_RM_Size (T, Asiz); | |
ea61a7ea | 6067 | end if; |
6068 | end; | |
6069 | ||
6070 | -- All other composite types are ignored | |
6071 | ||
6072 | elsif Is_Composite_Type (UT) then | |
6073 | return; | |
6074 | ||
d6f39728 | 6075 | -- For fixed-point types, don't check minimum if type is not frozen, |
ea61a7ea | 6076 | -- since we don't know all the characteristics of the type that can |
6077 | -- affect the size (e.g. a specified small) till freeze time. | |
d6f39728 | 6078 | |
6079 | elsif Is_Fixed_Point_Type (UT) | |
6080 | and then not Is_Frozen (UT) | |
6081 | then | |
6082 | null; | |
6083 | ||
6084 | -- Cases for which a minimum check is required | |
6085 | ||
6086 | else | |
ea61a7ea | 6087 | -- Ignore if specified size is correct for the type |
6088 | ||
6089 | if Known_Esize (UT) and then Siz = Esize (UT) then | |
6090 | return; | |
6091 | end if; | |
6092 | ||
6093 | -- Otherwise get minimum size | |
6094 | ||
d6f39728 | 6095 | M := UI_From_Int (Minimum_Size (UT)); |
6096 | ||
6097 | if Siz < M then | |
6098 | ||
6099 | -- Size is less than minimum size, but one possibility remains | |
fdd294d1 | 6100 | -- that we can manage with the new size if we bias the type. |
d6f39728 | 6101 | |
6102 | M := UI_From_Int (Minimum_Size (UT, Biased => True)); | |
6103 | ||
6104 | if Siz < M then | |
6105 | Error_Msg_Uint_1 := M; | |
6106 | Error_Msg_NE | |
6107 | ("size for& too small, minimum allowed is ^", N, T); | |
37cb33b0 | 6108 | Set_Esize (T, M); |
6109 | Set_RM_Size (T, M); | |
d6f39728 | 6110 | else |
6111 | Biased := True; | |
6112 | end if; | |
6113 | end if; | |
6114 | end if; | |
6115 | end Check_Size; | |
6116 | ||
6117 | ------------------------- | |
6118 | -- Get_Alignment_Value -- | |
6119 | ------------------------- | |
6120 | ||
6121 | function Get_Alignment_Value (Expr : Node_Id) return Uint is | |
6122 | Align : constant Uint := Static_Integer (Expr); | |
6123 | ||
6124 | begin | |
6125 | if Align = No_Uint then | |
6126 | return No_Uint; | |
6127 | ||
6128 | elsif Align <= 0 then | |
6129 | Error_Msg_N ("alignment value must be positive", Expr); | |
6130 | return No_Uint; | |
6131 | ||
6132 | else | |
6133 | for J in Int range 0 .. 64 loop | |
6134 | declare | |
6135 | M : constant Uint := Uint_2 ** J; | |
6136 | ||
6137 | begin | |
6138 | exit when M = Align; | |
6139 | ||
6140 | if M > Align then | |
6141 | Error_Msg_N | |
6142 | ("alignment value must be power of 2", Expr); | |
6143 | return No_Uint; | |
6144 | end if; | |
6145 | end; | |
6146 | end loop; | |
6147 | ||
6148 | return Align; | |
6149 | end if; | |
6150 | end Get_Alignment_Value; | |
6151 | ||
d6f39728 | 6152 | ---------------- |
6153 | -- Initialize -- | |
6154 | ---------------- | |
6155 | ||
6156 | procedure Initialize is | |
6157 | begin | |
7717ea00 | 6158 | Address_Clause_Checks.Init; |
6159 | Independence_Checks.Init; | |
d6f39728 | 6160 | Unchecked_Conversions.Init; |
6161 | end Initialize; | |
6162 | ||
6163 | ------------------------- | |
6164 | -- Is_Operational_Item -- | |
6165 | ------------------------- | |
6166 | ||
6167 | function Is_Operational_Item (N : Node_Id) return Boolean is | |
6168 | begin | |
6169 | if Nkind (N) /= N_Attribute_Definition_Clause then | |
6170 | return False; | |
6171 | else | |
6172 | declare | |
6173 | Id : constant Attribute_Id := Get_Attribute_Id (Chars (N)); | |
d6f39728 | 6174 | begin |
fdd294d1 | 6175 | return Id = Attribute_Input |
d6f39728 | 6176 | or else Id = Attribute_Output |
6177 | or else Id = Attribute_Read | |
f15731c4 | 6178 | or else Id = Attribute_Write |
6179 | or else Id = Attribute_External_Tag; | |
d6f39728 | 6180 | end; |
6181 | end if; | |
6182 | end Is_Operational_Item; | |
6183 | ||
6184 | ------------------ | |
6185 | -- Minimum_Size -- | |
6186 | ------------------ | |
6187 | ||
6188 | function Minimum_Size | |
6189 | (T : Entity_Id; | |
d5b349fa | 6190 | Biased : Boolean := False) return Nat |
d6f39728 | 6191 | is |
6192 | Lo : Uint := No_Uint; | |
6193 | Hi : Uint := No_Uint; | |
6194 | LoR : Ureal := No_Ureal; | |
6195 | HiR : Ureal := No_Ureal; | |
6196 | LoSet : Boolean := False; | |
6197 | HiSet : Boolean := False; | |
6198 | B : Uint; | |
6199 | S : Nat; | |
6200 | Ancest : Entity_Id; | |
f15731c4 | 6201 | R_Typ : constant Entity_Id := Root_Type (T); |
d6f39728 | 6202 | |
6203 | begin | |
6204 | -- If bad type, return 0 | |
6205 | ||
6206 | if T = Any_Type then | |
6207 | return 0; | |
6208 | ||
6209 | -- For generic types, just return zero. There cannot be any legitimate | |
6210 | -- need to know such a size, but this routine may be called with a | |
6211 | -- generic type as part of normal processing. | |
6212 | ||
f15731c4 | 6213 | elsif Is_Generic_Type (R_Typ) |
6214 | or else R_Typ = Any_Type | |
6215 | then | |
d6f39728 | 6216 | return 0; |
6217 | ||
93735cb8 | 6218 | -- Access types. Normally an access type cannot have a size smaller |
6219 | -- than the size of System.Address. The exception is on VMS, where | |
6220 | -- we have short and long addresses, and it is possible for an access | |
6221 | -- type to have a short address size (and thus be less than the size | |
6222 | -- of System.Address itself). We simply skip the check for VMS, and | |
fdd294d1 | 6223 | -- leave it to the back end to do the check. |
d6f39728 | 6224 | |
6225 | elsif Is_Access_Type (T) then | |
93735cb8 | 6226 | if OpenVMS_On_Target then |
6227 | return 0; | |
6228 | else | |
6229 | return System_Address_Size; | |
6230 | end if; | |
d6f39728 | 6231 | |
6232 | -- Floating-point types | |
6233 | ||
6234 | elsif Is_Floating_Point_Type (T) then | |
f15731c4 | 6235 | return UI_To_Int (Esize (R_Typ)); |
d6f39728 | 6236 | |
6237 | -- Discrete types | |
6238 | ||
6239 | elsif Is_Discrete_Type (T) then | |
6240 | ||
fdd294d1 | 6241 | -- The following loop is looking for the nearest compile time known |
6242 | -- bounds following the ancestor subtype chain. The idea is to find | |
6243 | -- the most restrictive known bounds information. | |
d6f39728 | 6244 | |
6245 | Ancest := T; | |
6246 | loop | |
6247 | if Ancest = Any_Type or else Etype (Ancest) = Any_Type then | |
6248 | return 0; | |
6249 | end if; | |
6250 | ||
6251 | if not LoSet then | |
6252 | if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then | |
6253 | Lo := Expr_Rep_Value (Type_Low_Bound (Ancest)); | |
6254 | LoSet := True; | |
6255 | exit when HiSet; | |
6256 | end if; | |
6257 | end if; | |
6258 | ||
6259 | if not HiSet then | |
6260 | if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then | |
6261 | Hi := Expr_Rep_Value (Type_High_Bound (Ancest)); | |
6262 | HiSet := True; | |
6263 | exit when LoSet; | |
6264 | end if; | |
6265 | end if; | |
6266 | ||
6267 | Ancest := Ancestor_Subtype (Ancest); | |
6268 | ||
6269 | if No (Ancest) then | |
6270 | Ancest := Base_Type (T); | |
6271 | ||
6272 | if Is_Generic_Type (Ancest) then | |
6273 | return 0; | |
6274 | end if; | |
6275 | end if; | |
6276 | end loop; | |
6277 | ||
6278 | -- Fixed-point types. We can't simply use Expr_Value to get the | |
fdd294d1 | 6279 | -- Corresponding_Integer_Value values of the bounds, since these do not |
6280 | -- get set till the type is frozen, and this routine can be called | |
6281 | -- before the type is frozen. Similarly the test for bounds being static | |
6282 | -- needs to include the case where we have unanalyzed real literals for | |
6283 | -- the same reason. | |
d6f39728 | 6284 | |
6285 | elsif Is_Fixed_Point_Type (T) then | |
6286 | ||
fdd294d1 | 6287 | -- The following loop is looking for the nearest compile time known |
6288 | -- bounds following the ancestor subtype chain. The idea is to find | |
6289 | -- the most restrictive known bounds information. | |
d6f39728 | 6290 | |
6291 | Ancest := T; | |
6292 | loop | |
6293 | if Ancest = Any_Type or else Etype (Ancest) = Any_Type then | |
6294 | return 0; | |
6295 | end if; | |
6296 | ||
3062c401 | 6297 | -- Note: In the following two tests for LoSet and HiSet, it may |
6298 | -- seem redundant to test for N_Real_Literal here since normally | |
6299 | -- one would assume that the test for the value being known at | |
6300 | -- compile time includes this case. However, there is a glitch. | |
6301 | -- If the real literal comes from folding a non-static expression, | |
6302 | -- then we don't consider any non- static expression to be known | |
6303 | -- at compile time if we are in configurable run time mode (needed | |
6304 | -- in some cases to give a clearer definition of what is and what | |
6305 | -- is not accepted). So the test is indeed needed. Without it, we | |
6306 | -- would set neither Lo_Set nor Hi_Set and get an infinite loop. | |
6307 | ||
d6f39728 | 6308 | if not LoSet then |
6309 | if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal | |
6310 | or else Compile_Time_Known_Value (Type_Low_Bound (Ancest)) | |
6311 | then | |
6312 | LoR := Expr_Value_R (Type_Low_Bound (Ancest)); | |
6313 | LoSet := True; | |
6314 | exit when HiSet; | |
6315 | end if; | |
6316 | end if; | |
6317 | ||
6318 | if not HiSet then | |
6319 | if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal | |
6320 | or else Compile_Time_Known_Value (Type_High_Bound (Ancest)) | |
6321 | then | |
6322 | HiR := Expr_Value_R (Type_High_Bound (Ancest)); | |
6323 | HiSet := True; | |
6324 | exit when LoSet; | |
6325 | end if; | |
6326 | end if; | |
6327 | ||
6328 | Ancest := Ancestor_Subtype (Ancest); | |
6329 | ||
6330 | if No (Ancest) then | |
6331 | Ancest := Base_Type (T); | |
6332 | ||
6333 | if Is_Generic_Type (Ancest) then | |
6334 | return 0; | |
6335 | end if; | |
6336 | end if; | |
6337 | end loop; | |
6338 | ||
6339 | Lo := UR_To_Uint (LoR / Small_Value (T)); | |
6340 | Hi := UR_To_Uint (HiR / Small_Value (T)); | |
6341 | ||
6342 | -- No other types allowed | |
6343 | ||
6344 | else | |
6345 | raise Program_Error; | |
6346 | end if; | |
6347 | ||
2866d595 | 6348 | -- Fall through with Hi and Lo set. Deal with biased case |
d6f39728 | 6349 | |
cc46ff4b | 6350 | if (Biased |
6351 | and then not Is_Fixed_Point_Type (T) | |
6352 | and then not (Is_Enumeration_Type (T) | |
6353 | and then Has_Non_Standard_Rep (T))) | |
d6f39728 | 6354 | or else Has_Biased_Representation (T) |
6355 | then | |
6356 | Hi := Hi - Lo; | |
6357 | Lo := Uint_0; | |
6358 | end if; | |
6359 | ||
6360 | -- Signed case. Note that we consider types like range 1 .. -1 to be | |
fdd294d1 | 6361 | -- signed for the purpose of computing the size, since the bounds have |
1a34e48c | 6362 | -- to be accommodated in the base type. |
d6f39728 | 6363 | |
6364 | if Lo < 0 or else Hi < 0 then | |
6365 | S := 1; | |
6366 | B := Uint_1; | |
6367 | ||
da253936 | 6368 | -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1)) |
6369 | -- Note that we accommodate the case where the bounds cross. This | |
d6f39728 | 6370 | -- can happen either because of the way the bounds are declared |
6371 | -- or because of the algorithm in Freeze_Fixed_Point_Type. | |
6372 | ||
6373 | while Lo < -B | |
6374 | or else Hi < -B | |
6375 | or else Lo >= B | |
6376 | or else Hi >= B | |
6377 | loop | |
6378 | B := Uint_2 ** S; | |
6379 | S := S + 1; | |
6380 | end loop; | |
6381 | ||
6382 | -- Unsigned case | |
6383 | ||
6384 | else | |
6385 | -- If both bounds are positive, make sure that both are represen- | |
6386 | -- table in the case where the bounds are crossed. This can happen | |
6387 | -- either because of the way the bounds are declared, or because of | |
6388 | -- the algorithm in Freeze_Fixed_Point_Type. | |
6389 | ||
6390 | if Lo > Hi then | |
6391 | Hi := Lo; | |
6392 | end if; | |
6393 | ||
da253936 | 6394 | -- S = size, (can accommodate 0 .. (2**size - 1)) |
d6f39728 | 6395 | |
6396 | S := 0; | |
6397 | while Hi >= Uint_2 ** S loop | |
6398 | S := S + 1; | |
6399 | end loop; | |
6400 | end if; | |
6401 | ||
6402 | return S; | |
6403 | end Minimum_Size; | |
6404 | ||
44e4341e | 6405 | --------------------------- |
6406 | -- New_Stream_Subprogram -- | |
6407 | --------------------------- | |
d6f39728 | 6408 | |
44e4341e | 6409 | procedure New_Stream_Subprogram |
6410 | (N : Node_Id; | |
6411 | Ent : Entity_Id; | |
6412 | Subp : Entity_Id; | |
6413 | Nam : TSS_Name_Type) | |
d6f39728 | 6414 | is |
6415 | Loc : constant Source_Ptr := Sloc (N); | |
9dfe12ae | 6416 | Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam); |
f15731c4 | 6417 | Subp_Id : Entity_Id; |
d6f39728 | 6418 | Subp_Decl : Node_Id; |
6419 | F : Entity_Id; | |
6420 | Etyp : Entity_Id; | |
6421 | ||
44e4341e | 6422 | Defer_Declaration : constant Boolean := |
6423 | Is_Tagged_Type (Ent) or else Is_Private_Type (Ent); | |
6424 | -- For a tagged type, there is a declaration for each stream attribute | |
6425 | -- at the freeze point, and we must generate only a completion of this | |
6426 | -- declaration. We do the same for private types, because the full view | |
6427 | -- might be tagged. Otherwise we generate a declaration at the point of | |
6428 | -- the attribute definition clause. | |
6429 | ||
f15731c4 | 6430 | function Build_Spec return Node_Id; |
6431 | -- Used for declaration and renaming declaration, so that this is | |
6432 | -- treated as a renaming_as_body. | |
6433 | ||
6434 | ---------------- | |
6435 | -- Build_Spec -- | |
6436 | ---------------- | |
6437 | ||
d5b349fa | 6438 | function Build_Spec return Node_Id is |
44e4341e | 6439 | Out_P : constant Boolean := (Nam = TSS_Stream_Read); |
6440 | Formals : List_Id; | |
6441 | Spec : Node_Id; | |
6442 | T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc); | |
6443 | ||
f15731c4 | 6444 | begin |
9dfe12ae | 6445 | Subp_Id := Make_Defining_Identifier (Loc, Sname); |
f15731c4 | 6446 | |
44e4341e | 6447 | -- S : access Root_Stream_Type'Class |
6448 | ||
6449 | Formals := New_List ( | |
6450 | Make_Parameter_Specification (Loc, | |
6451 | Defining_Identifier => | |
6452 | Make_Defining_Identifier (Loc, Name_S), | |
6453 | Parameter_Type => | |
6454 | Make_Access_Definition (Loc, | |
6455 | Subtype_Mark => | |
6456 | New_Reference_To ( | |
6457 | Designated_Type (Etype (F)), Loc)))); | |
6458 | ||
6459 | if Nam = TSS_Stream_Input then | |
6460 | Spec := Make_Function_Specification (Loc, | |
6461 | Defining_Unit_Name => Subp_Id, | |
6462 | Parameter_Specifications => Formals, | |
6463 | Result_Definition => T_Ref); | |
6464 | else | |
6465 | -- V : [out] T | |
f15731c4 | 6466 | |
44e4341e | 6467 | Append_To (Formals, |
6468 | Make_Parameter_Specification (Loc, | |
6469 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), | |
6470 | Out_Present => Out_P, | |
6471 | Parameter_Type => T_Ref)); | |
f15731c4 | 6472 | |
d3ef794c | 6473 | Spec := |
6474 | Make_Procedure_Specification (Loc, | |
6475 | Defining_Unit_Name => Subp_Id, | |
6476 | Parameter_Specifications => Formals); | |
44e4341e | 6477 | end if; |
f15731c4 | 6478 | |
44e4341e | 6479 | return Spec; |
6480 | end Build_Spec; | |
d6f39728 | 6481 | |
44e4341e | 6482 | -- Start of processing for New_Stream_Subprogram |
d6f39728 | 6483 | |
44e4341e | 6484 | begin |
6485 | F := First_Formal (Subp); | |
6486 | ||
6487 | if Ekind (Subp) = E_Procedure then | |
6488 | Etyp := Etype (Next_Formal (F)); | |
d6f39728 | 6489 | else |
44e4341e | 6490 | Etyp := Etype (Subp); |
d6f39728 | 6491 | end if; |
f15731c4 | 6492 | |
44e4341e | 6493 | -- Prepare subprogram declaration and insert it as an action on the |
6494 | -- clause node. The visibility for this entity is used to test for | |
6495 | -- visibility of the attribute definition clause (in the sense of | |
6496 | -- 8.3(23) as amended by AI-195). | |
9dfe12ae | 6497 | |
44e4341e | 6498 | if not Defer_Declaration then |
f15731c4 | 6499 | Subp_Decl := |
6500 | Make_Subprogram_Declaration (Loc, | |
6501 | Specification => Build_Spec); | |
44e4341e | 6502 | |
6503 | -- For a tagged type, there is always a visible declaration for each | |
15ebb600 | 6504 | -- stream TSS (it is a predefined primitive operation), and the |
44e4341e | 6505 | -- completion of this declaration occurs at the freeze point, which is |
6506 | -- not always visible at places where the attribute definition clause is | |
6507 | -- visible. So, we create a dummy entity here for the purpose of | |
6508 | -- tracking the visibility of the attribute definition clause itself. | |
6509 | ||
6510 | else | |
6511 | Subp_Id := | |
6512 | Make_Defining_Identifier (Loc, | |
6513 | Chars => New_External_Name (Sname, 'V')); | |
6514 | Subp_Decl := | |
6515 | Make_Object_Declaration (Loc, | |
6516 | Defining_Identifier => Subp_Id, | |
6517 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc)); | |
f15731c4 | 6518 | end if; |
6519 | ||
44e4341e | 6520 | Insert_Action (N, Subp_Decl); |
6521 | Set_Entity (N, Subp_Id); | |
6522 | ||
d6f39728 | 6523 | Subp_Decl := |
6524 | Make_Subprogram_Renaming_Declaration (Loc, | |
f15731c4 | 6525 | Specification => Build_Spec, |
6526 | Name => New_Reference_To (Subp, Loc)); | |
d6f39728 | 6527 | |
44e4341e | 6528 | if Defer_Declaration then |
d6f39728 | 6529 | Set_TSS (Base_Type (Ent), Subp_Id); |
6530 | else | |
6531 | Insert_Action (N, Subp_Decl); | |
6532 | Copy_TSS (Subp_Id, Base_Type (Ent)); | |
6533 | end if; | |
44e4341e | 6534 | end New_Stream_Subprogram; |
d6f39728 | 6535 | |
d6f39728 | 6536 | ------------------------ |
6537 | -- Rep_Item_Too_Early -- | |
6538 | ------------------------ | |
6539 | ||
80d4fec4 | 6540 | function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is |
d6f39728 | 6541 | begin |
44e4341e | 6542 | -- Cannot apply non-operational rep items to generic types |
d6f39728 | 6543 | |
f15731c4 | 6544 | if Is_Operational_Item (N) then |
6545 | return False; | |
6546 | ||
6547 | elsif Is_Type (T) | |
d6f39728 | 6548 | and then Is_Generic_Type (Root_Type (T)) |
6549 | then | |
503f7fd3 | 6550 | Error_Msg_N ("representation item not allowed for generic type", N); |
d6f39728 | 6551 | return True; |
6552 | end if; | |
6553 | ||
fdd294d1 | 6554 | -- Otherwise check for incomplete type |
d6f39728 | 6555 | |
6556 | if Is_Incomplete_Or_Private_Type (T) | |
6557 | and then No (Underlying_Type (T)) | |
6558 | then | |
6559 | Error_Msg_N | |
6560 | ("representation item must be after full type declaration", N); | |
6561 | return True; | |
6562 | ||
1a34e48c | 6563 | -- If the type has incomplete components, a representation clause is |
d6f39728 | 6564 | -- illegal but stream attributes and Convention pragmas are correct. |
6565 | ||
6566 | elsif Has_Private_Component (T) then | |
f15731c4 | 6567 | if Nkind (N) = N_Pragma then |
d6f39728 | 6568 | return False; |
6569 | else | |
6570 | Error_Msg_N | |
6571 | ("representation item must appear after type is fully defined", | |
6572 | N); | |
6573 | return True; | |
6574 | end if; | |
6575 | else | |
6576 | return False; | |
6577 | end if; | |
6578 | end Rep_Item_Too_Early; | |
6579 | ||
6580 | ----------------------- | |
6581 | -- Rep_Item_Too_Late -- | |
6582 | ----------------------- | |
6583 | ||
6584 | function Rep_Item_Too_Late | |
6585 | (T : Entity_Id; | |
6586 | N : Node_Id; | |
d5b349fa | 6587 | FOnly : Boolean := False) return Boolean |
d6f39728 | 6588 | is |
6589 | S : Entity_Id; | |
6590 | Parent_Type : Entity_Id; | |
6591 | ||
6592 | procedure Too_Late; | |
d53a018a | 6593 | -- Output the too late message. Note that this is not considered a |
6594 | -- serious error, since the effect is simply that we ignore the | |
6595 | -- representation clause in this case. | |
6596 | ||
6597 | -------------- | |
6598 | -- Too_Late -- | |
6599 | -------------- | |
d6f39728 | 6600 | |
6601 | procedure Too_Late is | |
6602 | begin | |
d53a018a | 6603 | Error_Msg_N ("|representation item appears too late!", N); |
d6f39728 | 6604 | end Too_Late; |
6605 | ||
6606 | -- Start of processing for Rep_Item_Too_Late | |
6607 | ||
6608 | begin | |
6609 | -- First make sure entity is not frozen (RM 13.1(9)). Exclude imported | |
6610 | -- types, which may be frozen if they appear in a representation clause | |
6611 | -- for a local type. | |
6612 | ||
6613 | if Is_Frozen (T) | |
6614 | and then not From_With_Type (T) | |
6615 | then | |
6616 | Too_Late; | |
6617 | S := First_Subtype (T); | |
6618 | ||
6619 | if Present (Freeze_Node (S)) then | |
6620 | Error_Msg_NE | |
87d5c1d0 | 6621 | ("?no more representation items for }", Freeze_Node (S), S); |
d6f39728 | 6622 | end if; |
6623 | ||
6624 | return True; | |
6625 | ||
6626 | -- Check for case of non-tagged derived type whose parent either has | |
6627 | -- primitive operations, or is a by reference type (RM 13.1(10)). | |
6628 | ||
6629 | elsif Is_Type (T) | |
6630 | and then not FOnly | |
6631 | and then Is_Derived_Type (T) | |
6632 | and then not Is_Tagged_Type (T) | |
6633 | then | |
6634 | Parent_Type := Etype (Base_Type (T)); | |
6635 | ||
6636 | if Has_Primitive_Operations (Parent_Type) then | |
6637 | Too_Late; | |
6638 | Error_Msg_NE | |
6639 | ("primitive operations already defined for&!", N, Parent_Type); | |
6640 | return True; | |
6641 | ||
6642 | elsif Is_By_Reference_Type (Parent_Type) then | |
6643 | Too_Late; | |
6644 | Error_Msg_NE | |
6645 | ("parent type & is a by reference type!", N, Parent_Type); | |
6646 | return True; | |
6647 | end if; | |
6648 | end if; | |
6649 | ||
3062c401 | 6650 | -- No error, link item into head of chain of rep items for the entity, |
6651 | -- but avoid chaining if we have an overloadable entity, and the pragma | |
6652 | -- is one that can apply to multiple overloaded entities. | |
6653 | ||
6654 | if Is_Overloadable (T) | |
6655 | and then Nkind (N) = N_Pragma | |
3062c401 | 6656 | then |
fdd294d1 | 6657 | declare |
6658 | Pname : constant Name_Id := Pragma_Name (N); | |
6659 | begin | |
6660 | if Pname = Name_Convention or else | |
6661 | Pname = Name_Import or else | |
6662 | Pname = Name_Export or else | |
6663 | Pname = Name_External or else | |
6664 | Pname = Name_Interface | |
6665 | then | |
6666 | return False; | |
6667 | end if; | |
6668 | end; | |
3062c401 | 6669 | end if; |
6670 | ||
fdd294d1 | 6671 | Record_Rep_Item (T, N); |
d6f39728 | 6672 | return False; |
6673 | end Rep_Item_Too_Late; | |
6674 | ||
6675 | ------------------------- | |
6676 | -- Same_Representation -- | |
6677 | ------------------------- | |
6678 | ||
6679 | function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is | |
6680 | T1 : constant Entity_Id := Underlying_Type (Typ1); | |
6681 | T2 : constant Entity_Id := Underlying_Type (Typ2); | |
6682 | ||
6683 | begin | |
6684 | -- A quick check, if base types are the same, then we definitely have | |
6685 | -- the same representation, because the subtype specific representation | |
6686 | -- attributes (Size and Alignment) do not affect representation from | |
6687 | -- the point of view of this test. | |
6688 | ||
6689 | if Base_Type (T1) = Base_Type (T2) then | |
6690 | return True; | |
6691 | ||
6692 | elsif Is_Private_Type (Base_Type (T2)) | |
6693 | and then Base_Type (T1) = Full_View (Base_Type (T2)) | |
6694 | then | |
6695 | return True; | |
6696 | end if; | |
6697 | ||
6698 | -- Tagged types never have differing representations | |
6699 | ||
6700 | if Is_Tagged_Type (T1) then | |
6701 | return True; | |
6702 | end if; | |
6703 | ||
6704 | -- Representations are definitely different if conventions differ | |
6705 | ||
6706 | if Convention (T1) /= Convention (T2) then | |
6707 | return False; | |
6708 | end if; | |
6709 | ||
6710 | -- Representations are different if component alignments differ | |
6711 | ||
6712 | if (Is_Record_Type (T1) or else Is_Array_Type (T1)) | |
6713 | and then | |
6714 | (Is_Record_Type (T2) or else Is_Array_Type (T2)) | |
6715 | and then Component_Alignment (T1) /= Component_Alignment (T2) | |
6716 | then | |
6717 | return False; | |
6718 | end if; | |
6719 | ||
6720 | -- For arrays, the only real issue is component size. If we know the | |
6721 | -- component size for both arrays, and it is the same, then that's | |
6722 | -- good enough to know we don't have a change of representation. | |
6723 | ||
6724 | if Is_Array_Type (T1) then | |
6725 | if Known_Component_Size (T1) | |
6726 | and then Known_Component_Size (T2) | |
6727 | and then Component_Size (T1) = Component_Size (T2) | |
6728 | then | |
6729 | return True; | |
6730 | end if; | |
6731 | end if; | |
6732 | ||
6733 | -- Types definitely have same representation if neither has non-standard | |
6734 | -- representation since default representations are always consistent. | |
6735 | -- If only one has non-standard representation, and the other does not, | |
6736 | -- then we consider that they do not have the same representation. They | |
6737 | -- might, but there is no way of telling early enough. | |
6738 | ||
6739 | if Has_Non_Standard_Rep (T1) then | |
6740 | if not Has_Non_Standard_Rep (T2) then | |
6741 | return False; | |
6742 | end if; | |
6743 | else | |
6744 | return not Has_Non_Standard_Rep (T2); | |
6745 | end if; | |
6746 | ||
fdd294d1 | 6747 | -- Here the two types both have non-standard representation, and we need |
6748 | -- to determine if they have the same non-standard representation. | |
d6f39728 | 6749 | |
6750 | -- For arrays, we simply need to test if the component sizes are the | |
6751 | -- same. Pragma Pack is reflected in modified component sizes, so this | |
6752 | -- check also deals with pragma Pack. | |
6753 | ||
6754 | if Is_Array_Type (T1) then | |
6755 | return Component_Size (T1) = Component_Size (T2); | |
6756 | ||
6757 | -- Tagged types always have the same representation, because it is not | |
6758 | -- possible to specify different representations for common fields. | |
6759 | ||
6760 | elsif Is_Tagged_Type (T1) then | |
6761 | return True; | |
6762 | ||
6763 | -- Case of record types | |
6764 | ||
6765 | elsif Is_Record_Type (T1) then | |
6766 | ||
6767 | -- Packed status must conform | |
6768 | ||
6769 | if Is_Packed (T1) /= Is_Packed (T2) then | |
6770 | return False; | |
6771 | ||
6772 | -- Otherwise we must check components. Typ2 maybe a constrained | |
6773 | -- subtype with fewer components, so we compare the components | |
6774 | -- of the base types. | |
6775 | ||
6776 | else | |
6777 | Record_Case : declare | |
6778 | CD1, CD2 : Entity_Id; | |
6779 | ||
6780 | function Same_Rep return Boolean; | |
6781 | -- CD1 and CD2 are either components or discriminants. This | |
6782 | -- function tests whether the two have the same representation | |
6783 | ||
80d4fec4 | 6784 | -------------- |
6785 | -- Same_Rep -- | |
6786 | -------------- | |
6787 | ||
d6f39728 | 6788 | function Same_Rep return Boolean is |
6789 | begin | |
6790 | if No (Component_Clause (CD1)) then | |
6791 | return No (Component_Clause (CD2)); | |
6792 | ||
6793 | else | |
6794 | return | |
6795 | Present (Component_Clause (CD2)) | |
6796 | and then | |
6797 | Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2) | |
6798 | and then | |
6799 | Esize (CD1) = Esize (CD2); | |
6800 | end if; | |
6801 | end Same_Rep; | |
6802 | ||
1e35409d | 6803 | -- Start of processing for Record_Case |
d6f39728 | 6804 | |
6805 | begin | |
6806 | if Has_Discriminants (T1) then | |
6807 | CD1 := First_Discriminant (T1); | |
6808 | CD2 := First_Discriminant (T2); | |
6809 | ||
9dfe12ae | 6810 | -- The number of discriminants may be different if the |
6811 | -- derived type has fewer (constrained by values). The | |
6812 | -- invisible discriminants retain the representation of | |
6813 | -- the original, so the discrepancy does not per se | |
6814 | -- indicate a different representation. | |
6815 | ||
6816 | while Present (CD1) | |
6817 | and then Present (CD2) | |
6818 | loop | |
d6f39728 | 6819 | if not Same_Rep then |
6820 | return False; | |
6821 | else | |
6822 | Next_Discriminant (CD1); | |
6823 | Next_Discriminant (CD2); | |
6824 | end if; | |
6825 | end loop; | |
6826 | end if; | |
6827 | ||
6828 | CD1 := First_Component (Underlying_Type (Base_Type (T1))); | |
6829 | CD2 := First_Component (Underlying_Type (Base_Type (T2))); | |
6830 | ||
6831 | while Present (CD1) loop | |
6832 | if not Same_Rep then | |
6833 | return False; | |
6834 | else | |
6835 | Next_Component (CD1); | |
6836 | Next_Component (CD2); | |
6837 | end if; | |
6838 | end loop; | |
6839 | ||
6840 | return True; | |
6841 | end Record_Case; | |
6842 | end if; | |
6843 | ||
6844 | -- For enumeration types, we must check each literal to see if the | |
6845 | -- representation is the same. Note that we do not permit enumeration | |
1a34e48c | 6846 | -- representation clauses for Character and Wide_Character, so these |
d6f39728 | 6847 | -- cases were already dealt with. |
6848 | ||
6849 | elsif Is_Enumeration_Type (T1) then | |
d6f39728 | 6850 | Enumeration_Case : declare |
6851 | L1, L2 : Entity_Id; | |
6852 | ||
6853 | begin | |
6854 | L1 := First_Literal (T1); | |
6855 | L2 := First_Literal (T2); | |
6856 | ||
6857 | while Present (L1) loop | |
6858 | if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then | |
6859 | return False; | |
6860 | else | |
6861 | Next_Literal (L1); | |
6862 | Next_Literal (L2); | |
6863 | end if; | |
6864 | end loop; | |
6865 | ||
6866 | return True; | |
6867 | ||
6868 | end Enumeration_Case; | |
6869 | ||
6870 | -- Any other types have the same representation for these purposes | |
6871 | ||
6872 | else | |
6873 | return True; | |
6874 | end if; | |
d6f39728 | 6875 | end Same_Representation; |
6876 | ||
b77e4501 | 6877 | ---------------- |
6878 | -- Set_Biased -- | |
6879 | ---------------- | |
6880 | ||
6881 | procedure Set_Biased | |
6882 | (E : Entity_Id; | |
6883 | N : Node_Id; | |
6884 | Msg : String; | |
6885 | Biased : Boolean := True) | |
6886 | is | |
6887 | begin | |
6888 | if Biased then | |
6889 | Set_Has_Biased_Representation (E); | |
6890 | ||
6891 | if Warn_On_Biased_Representation then | |
6892 | Error_Msg_NE | |
6893 | ("?" & Msg & " forces biased representation for&", N, E); | |
6894 | end if; | |
6895 | end if; | |
6896 | end Set_Biased; | |
6897 | ||
d6f39728 | 6898 | -------------------- |
6899 | -- Set_Enum_Esize -- | |
6900 | -------------------- | |
6901 | ||
6902 | procedure Set_Enum_Esize (T : Entity_Id) is | |
6903 | Lo : Uint; | |
6904 | Hi : Uint; | |
6905 | Sz : Nat; | |
6906 | ||
6907 | begin | |
6908 | Init_Alignment (T); | |
6909 | ||
6910 | -- Find the minimum standard size (8,16,32,64) that fits | |
6911 | ||
6912 | Lo := Enumeration_Rep (Entity (Type_Low_Bound (T))); | |
6913 | Hi := Enumeration_Rep (Entity (Type_High_Bound (T))); | |
6914 | ||
6915 | if Lo < 0 then | |
6916 | if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then | |
f15731c4 | 6917 | Sz := Standard_Character_Size; -- May be > 8 on some targets |
d6f39728 | 6918 | |
6919 | elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then | |
6920 | Sz := 16; | |
6921 | ||
6922 | elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then | |
6923 | Sz := 32; | |
6924 | ||
6925 | else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63); | |
6926 | Sz := 64; | |
6927 | end if; | |
6928 | ||
6929 | else | |
6930 | if Hi < Uint_2**08 then | |
f15731c4 | 6931 | Sz := Standard_Character_Size; -- May be > 8 on some targets |
d6f39728 | 6932 | |
6933 | elsif Hi < Uint_2**16 then | |
6934 | Sz := 16; | |
6935 | ||
6936 | elsif Hi < Uint_2**32 then | |
6937 | Sz := 32; | |
6938 | ||
6939 | else pragma Assert (Hi < Uint_2**63); | |
6940 | Sz := 64; | |
6941 | end if; | |
6942 | end if; | |
6943 | ||
6944 | -- That minimum is the proper size unless we have a foreign convention | |
6945 | -- and the size required is 32 or less, in which case we bump the size | |
6946 | -- up to 32. This is required for C and C++ and seems reasonable for | |
6947 | -- all other foreign conventions. | |
6948 | ||
6949 | if Has_Foreign_Convention (T) | |
6950 | and then Esize (T) < Standard_Integer_Size | |
6951 | then | |
6952 | Init_Esize (T, Standard_Integer_Size); | |
d6f39728 | 6953 | else |
6954 | Init_Esize (T, Sz); | |
6955 | end if; | |
d6f39728 | 6956 | end Set_Enum_Esize; |
6957 | ||
83f8f0a6 | 6958 | ------------------------------ |
6959 | -- Validate_Address_Clauses -- | |
6960 | ------------------------------ | |
6961 | ||
6962 | procedure Validate_Address_Clauses is | |
6963 | begin | |
6964 | for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop | |
6965 | declare | |
6966 | ACCR : Address_Clause_Check_Record | |
6967 | renames Address_Clause_Checks.Table (J); | |
6968 | ||
d6da7448 | 6969 | Expr : Node_Id; |
6970 | ||
83f8f0a6 | 6971 | X_Alignment : Uint; |
6972 | Y_Alignment : Uint; | |
6973 | ||
6974 | X_Size : Uint; | |
6975 | Y_Size : Uint; | |
6976 | ||
6977 | begin | |
6978 | -- Skip processing of this entry if warning already posted | |
6979 | ||
6980 | if not Address_Warning_Posted (ACCR.N) then | |
6981 | ||
d6da7448 | 6982 | Expr := Original_Node (Expression (ACCR.N)); |
83f8f0a6 | 6983 | |
d6da7448 | 6984 | -- Get alignments |
83f8f0a6 | 6985 | |
d6da7448 | 6986 | X_Alignment := Alignment (ACCR.X); |
6987 | Y_Alignment := Alignment (ACCR.Y); | |
83f8f0a6 | 6988 | |
6989 | -- Similarly obtain sizes | |
6990 | ||
d6da7448 | 6991 | X_Size := Esize (ACCR.X); |
6992 | Y_Size := Esize (ACCR.Y); | |
83f8f0a6 | 6993 | |
6994 | -- Check for large object overlaying smaller one | |
6995 | ||
6996 | if Y_Size > Uint_0 | |
6997 | and then X_Size > Uint_0 | |
6998 | and then X_Size > Y_Size | |
6999 | then | |
d6da7448 | 7000 | Error_Msg_NE |
7001 | ("?& overlays smaller object", ACCR.N, ACCR.X); | |
83f8f0a6 | 7002 | Error_Msg_N |
d6da7448 | 7003 | ("\?program execution may be erroneous", ACCR.N); |
83f8f0a6 | 7004 | Error_Msg_Uint_1 := X_Size; |
7005 | Error_Msg_NE | |
7006 | ("\?size of & is ^", ACCR.N, ACCR.X); | |
7007 | Error_Msg_Uint_1 := Y_Size; | |
7008 | Error_Msg_NE | |
7009 | ("\?size of & is ^", ACCR.N, ACCR.Y); | |
7010 | ||
d6da7448 | 7011 | -- Check for inadequate alignment, both of the base object |
7012 | -- and of the offset, if any. | |
83f8f0a6 | 7013 | |
d6da7448 | 7014 | -- Note: we do not check the alignment if we gave a size |
7015 | -- warning, since it would likely be redundant. | |
83f8f0a6 | 7016 | |
7017 | elsif Y_Alignment /= Uint_0 | |
d6da7448 | 7018 | and then (Y_Alignment < X_Alignment |
7019 | or else (ACCR.Off | |
7020 | and then | |
7021 | Nkind (Expr) = N_Attribute_Reference | |
7022 | and then | |
7023 | Attribute_Name (Expr) = Name_Address | |
7024 | and then | |
7025 | Has_Compatible_Alignment | |
7026 | (ACCR.X, Prefix (Expr)) | |
7027 | /= Known_Compatible)) | |
83f8f0a6 | 7028 | then |
7029 | Error_Msg_NE | |
7030 | ("?specified address for& may be inconsistent " | |
7031 | & "with alignment", | |
7032 | ACCR.N, ACCR.X); | |
7033 | Error_Msg_N | |
7034 | ("\?program execution may be erroneous (RM 13.3(27))", | |
7035 | ACCR.N); | |
7036 | Error_Msg_Uint_1 := X_Alignment; | |
7037 | Error_Msg_NE | |
7038 | ("\?alignment of & is ^", | |
7039 | ACCR.N, ACCR.X); | |
7040 | Error_Msg_Uint_1 := Y_Alignment; | |
7041 | Error_Msg_NE | |
7042 | ("\?alignment of & is ^", | |
7043 | ACCR.N, ACCR.Y); | |
d6da7448 | 7044 | if Y_Alignment >= X_Alignment then |
7045 | Error_Msg_N | |
7046 | ("\?but offset is not multiple of alignment", | |
7047 | ACCR.N); | |
7048 | end if; | |
83f8f0a6 | 7049 | end if; |
7050 | end if; | |
7051 | end; | |
7052 | end loop; | |
7053 | end Validate_Address_Clauses; | |
7054 | ||
7717ea00 | 7055 | --------------------------- |
7056 | -- Validate_Independence -- | |
7057 | --------------------------- | |
7058 | ||
7059 | procedure Validate_Independence is | |
7060 | SU : constant Uint := UI_From_Int (System_Storage_Unit); | |
7061 | N : Node_Id; | |
7062 | E : Entity_Id; | |
7063 | IC : Boolean; | |
7064 | Comp : Entity_Id; | |
7065 | Addr : Node_Id; | |
7066 | P : Node_Id; | |
7067 | ||
7068 | procedure Check_Array_Type (Atyp : Entity_Id); | |
7069 | -- Checks if the array type Atyp has independent components, and | |
7070 | -- if not, outputs an appropriate set of error messages. | |
7071 | ||
7072 | procedure No_Independence; | |
7073 | -- Output message that independence cannot be guaranteed | |
7074 | ||
7075 | function OK_Component (C : Entity_Id) return Boolean; | |
7076 | -- Checks one component to see if it is independently accessible, and | |
7077 | -- if so yields True, otherwise yields False if independent access | |
7078 | -- cannot be guaranteed. This is a conservative routine, it only | |
7079 | -- returns True if it knows for sure, it returns False if it knows | |
7080 | -- there is a problem, or it cannot be sure there is no problem. | |
7081 | ||
7082 | procedure Reason_Bad_Component (C : Entity_Id); | |
7083 | -- Outputs continuation message if a reason can be determined for | |
7084 | -- the component C being bad. | |
7085 | ||
7086 | ---------------------- | |
7087 | -- Check_Array_Type -- | |
7088 | ---------------------- | |
7089 | ||
7090 | procedure Check_Array_Type (Atyp : Entity_Id) is | |
7091 | Ctyp : constant Entity_Id := Component_Type (Atyp); | |
7092 | ||
7093 | begin | |
7094 | -- OK if no alignment clause, no pack, and no component size | |
7095 | ||
7096 | if not Has_Component_Size_Clause (Atyp) | |
7097 | and then not Has_Alignment_Clause (Atyp) | |
7098 | and then not Is_Packed (Atyp) | |
7099 | then | |
7100 | return; | |
7101 | end if; | |
7102 | ||
7103 | -- Check actual component size | |
7104 | ||
7105 | if not Known_Component_Size (Atyp) | |
7106 | or else not (Addressable (Component_Size (Atyp)) | |
7107 | and then Component_Size (Atyp) < 64) | |
7108 | or else Component_Size (Atyp) mod Esize (Ctyp) /= 0 | |
7109 | then | |
7110 | No_Independence; | |
7111 | ||
7112 | -- Bad component size, check reason | |
7113 | ||
7114 | if Has_Component_Size_Clause (Atyp) then | |
7115 | P := | |
7116 | Get_Attribute_Definition_Clause | |
7117 | (Atyp, Attribute_Component_Size); | |
7118 | ||
7119 | if Present (P) then | |
7120 | Error_Msg_Sloc := Sloc (P); | |
7121 | Error_Msg_N ("\because of Component_Size clause#", N); | |
7122 | return; | |
7123 | end if; | |
7124 | end if; | |
7125 | ||
7126 | if Is_Packed (Atyp) then | |
7127 | P := Get_Rep_Pragma (Atyp, Name_Pack); | |
7128 | ||
7129 | if Present (P) then | |
7130 | Error_Msg_Sloc := Sloc (P); | |
7131 | Error_Msg_N ("\because of pragma Pack#", N); | |
7132 | return; | |
7133 | end if; | |
7134 | end if; | |
7135 | ||
7136 | -- No reason found, just return | |
7137 | ||
7138 | return; | |
7139 | end if; | |
7140 | ||
7141 | -- Array type is OK independence-wise | |
7142 | ||
7143 | return; | |
7144 | end Check_Array_Type; | |
7145 | ||
7146 | --------------------- | |
7147 | -- No_Independence -- | |
7148 | --------------------- | |
7149 | ||
7150 | procedure No_Independence is | |
7151 | begin | |
7152 | if Pragma_Name (N) = Name_Independent then | |
7153 | Error_Msg_NE | |
7154 | ("independence cannot be guaranteed for&", N, E); | |
7155 | else | |
7156 | Error_Msg_NE | |
7157 | ("independent components cannot be guaranteed for&", N, E); | |
7158 | end if; | |
7159 | end No_Independence; | |
7160 | ||
7161 | ------------------ | |
7162 | -- OK_Component -- | |
7163 | ------------------ | |
7164 | ||
7165 | function OK_Component (C : Entity_Id) return Boolean is | |
7166 | Rec : constant Entity_Id := Scope (C); | |
7167 | Ctyp : constant Entity_Id := Etype (C); | |
7168 | ||
7169 | begin | |
7170 | -- OK if no component clause, no Pack, and no alignment clause | |
7171 | ||
7172 | if No (Component_Clause (C)) | |
7173 | and then not Is_Packed (Rec) | |
7174 | and then not Has_Alignment_Clause (Rec) | |
7175 | then | |
7176 | return True; | |
7177 | end if; | |
7178 | ||
7179 | -- Here we look at the actual component layout. A component is | |
7180 | -- addressable if its size is a multiple of the Esize of the | |
7181 | -- component type, and its starting position in the record has | |
7182 | -- appropriate alignment, and the record itself has appropriate | |
7183 | -- alignment to guarantee the component alignment. | |
7184 | ||
7185 | -- Make sure sizes are static, always assume the worst for any | |
7186 | -- cases where we cannot check static values. | |
7187 | ||
7188 | if not (Known_Static_Esize (C) | |
7189 | and then Known_Static_Esize (Ctyp)) | |
7190 | then | |
7191 | return False; | |
7192 | end if; | |
7193 | ||
7194 | -- Size of component must be addressable or greater than 64 bits | |
7195 | -- and a multiple of bytes. | |
7196 | ||
7197 | if not Addressable (Esize (C)) | |
7198 | and then Esize (C) < Uint_64 | |
7199 | then | |
7200 | return False; | |
7201 | end if; | |
7202 | ||
7203 | -- Check size is proper multiple | |
7204 | ||
7205 | if Esize (C) mod Esize (Ctyp) /= 0 then | |
7206 | return False; | |
7207 | end if; | |
7208 | ||
7209 | -- Check alignment of component is OK | |
7210 | ||
7211 | if not Known_Component_Bit_Offset (C) | |
7212 | or else Component_Bit_Offset (C) < Uint_0 | |
7213 | or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0 | |
7214 | then | |
7215 | return False; | |
7216 | end if; | |
7217 | ||
7218 | -- Check alignment of record type is OK | |
7219 | ||
7220 | if not Known_Alignment (Rec) | |
7221 | or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0 | |
7222 | then | |
7223 | return False; | |
7224 | end if; | |
7225 | ||
7226 | -- All tests passed, component is addressable | |
7227 | ||
7228 | return True; | |
7229 | end OK_Component; | |
7230 | ||
7231 | -------------------------- | |
7232 | -- Reason_Bad_Component -- | |
7233 | -------------------------- | |
7234 | ||
7235 | procedure Reason_Bad_Component (C : Entity_Id) is | |
7236 | Rec : constant Entity_Id := Scope (C); | |
7237 | Ctyp : constant Entity_Id := Etype (C); | |
7238 | ||
7239 | begin | |
7240 | -- If component clause present assume that's the problem | |
7241 | ||
7242 | if Present (Component_Clause (C)) then | |
7243 | Error_Msg_Sloc := Sloc (Component_Clause (C)); | |
7244 | Error_Msg_N ("\because of Component_Clause#", N); | |
7245 | return; | |
7246 | end if; | |
7247 | ||
7248 | -- If pragma Pack clause present, assume that's the problem | |
7249 | ||
7250 | if Is_Packed (Rec) then | |
7251 | P := Get_Rep_Pragma (Rec, Name_Pack); | |
7252 | ||
7253 | if Present (P) then | |
7254 | Error_Msg_Sloc := Sloc (P); | |
7255 | Error_Msg_N ("\because of pragma Pack#", N); | |
7256 | return; | |
7257 | end if; | |
7258 | end if; | |
7259 | ||
7260 | -- See if record has bad alignment clause | |
7261 | ||
7262 | if Has_Alignment_Clause (Rec) | |
7263 | and then Known_Alignment (Rec) | |
7264 | and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0 | |
7265 | then | |
7266 | P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment); | |
7267 | ||
7268 | if Present (P) then | |
7269 | Error_Msg_Sloc := Sloc (P); | |
7270 | Error_Msg_N ("\because of Alignment clause#", N); | |
7271 | end if; | |
7272 | end if; | |
7273 | ||
7274 | -- Couldn't find a reason, so return without a message | |
7275 | ||
7276 | return; | |
7277 | end Reason_Bad_Component; | |
7278 | ||
7279 | -- Start of processing for Validate_Independence | |
7280 | ||
7281 | begin | |
7282 | for J in Independence_Checks.First .. Independence_Checks.Last loop | |
7283 | N := Independence_Checks.Table (J).N; | |
7284 | E := Independence_Checks.Table (J).E; | |
7285 | IC := Pragma_Name (N) = Name_Independent_Components; | |
7286 | ||
7287 | -- Deal with component case | |
7288 | ||
7289 | if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then | |
7290 | if not OK_Component (E) then | |
7291 | No_Independence; | |
7292 | Reason_Bad_Component (E); | |
7293 | goto Continue; | |
7294 | end if; | |
7295 | end if; | |
7296 | ||
7297 | -- Deal with record with Independent_Components | |
7298 | ||
7299 | if IC and then Is_Record_Type (E) then | |
7300 | Comp := First_Component_Or_Discriminant (E); | |
7301 | while Present (Comp) loop | |
7302 | if not OK_Component (Comp) then | |
7303 | No_Independence; | |
7304 | Reason_Bad_Component (Comp); | |
7305 | goto Continue; | |
7306 | end if; | |
7307 | ||
7308 | Next_Component_Or_Discriminant (Comp); | |
7309 | end loop; | |
7310 | end if; | |
7311 | ||
7312 | -- Deal with address clause case | |
7313 | ||
7314 | if Is_Object (E) then | |
7315 | Addr := Address_Clause (E); | |
7316 | ||
7317 | if Present (Addr) then | |
7318 | No_Independence; | |
7319 | Error_Msg_Sloc := Sloc (Addr); | |
7320 | Error_Msg_N ("\because of Address clause#", N); | |
7321 | goto Continue; | |
7322 | end if; | |
7323 | end if; | |
7324 | ||
7325 | -- Deal with independent components for array type | |
7326 | ||
7327 | if IC and then Is_Array_Type (E) then | |
7328 | Check_Array_Type (E); | |
7329 | end if; | |
7330 | ||
7331 | -- Deal with independent components for array object | |
7332 | ||
7333 | if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then | |
7334 | Check_Array_Type (Etype (E)); | |
7335 | end if; | |
7336 | ||
7337 | <<Continue>> null; | |
7338 | end loop; | |
7339 | end Validate_Independence; | |
7340 | ||
d6f39728 | 7341 | ----------------------------------- |
7342 | -- Validate_Unchecked_Conversion -- | |
7343 | ----------------------------------- | |
7344 | ||
7345 | procedure Validate_Unchecked_Conversion | |
7346 | (N : Node_Id; | |
7347 | Act_Unit : Entity_Id) | |
7348 | is | |
7349 | Source : Entity_Id; | |
7350 | Target : Entity_Id; | |
7351 | Vnode : Node_Id; | |
7352 | ||
7353 | begin | |
7354 | -- Obtain source and target types. Note that we call Ancestor_Subtype | |
7355 | -- here because the processing for generic instantiation always makes | |
7356 | -- subtypes, and we want the original frozen actual types. | |
7357 | ||
7358 | -- If we are dealing with private types, then do the check on their | |
7359 | -- fully declared counterparts if the full declarations have been | |
7360 | -- encountered (they don't have to be visible, but they must exist!) | |
7361 | ||
7362 | Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit))); | |
7363 | ||
7364 | if Is_Private_Type (Source) | |
7365 | and then Present (Underlying_Type (Source)) | |
7366 | then | |
7367 | Source := Underlying_Type (Source); | |
7368 | end if; | |
7369 | ||
7370 | Target := Ancestor_Subtype (Etype (Act_Unit)); | |
7371 | ||
fdd294d1 | 7372 | -- If either type is generic, the instantiation happens within a generic |
7373 | -- unit, and there is nothing to check. The proper check | |
d6f39728 | 7374 | -- will happen when the enclosing generic is instantiated. |
7375 | ||
7376 | if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then | |
7377 | return; | |
7378 | end if; | |
7379 | ||
7380 | if Is_Private_Type (Target) | |
7381 | and then Present (Underlying_Type (Target)) | |
7382 | then | |
7383 | Target := Underlying_Type (Target); | |
7384 | end if; | |
7385 | ||
7386 | -- Source may be unconstrained array, but not target | |
7387 | ||
7388 | if Is_Array_Type (Target) | |
7389 | and then not Is_Constrained (Target) | |
7390 | then | |
7391 | Error_Msg_N | |
7392 | ("unchecked conversion to unconstrained array not allowed", N); | |
7393 | return; | |
7394 | end if; | |
7395 | ||
fbc67f84 | 7396 | -- Warn if conversion between two different convention pointers |
7397 | ||
7398 | if Is_Access_Type (Target) | |
7399 | and then Is_Access_Type (Source) | |
7400 | and then Convention (Target) /= Convention (Source) | |
7401 | and then Warn_On_Unchecked_Conversion | |
7402 | then | |
fdd294d1 | 7403 | -- Give warnings for subprogram pointers only on most targets. The |
7404 | -- exception is VMS, where data pointers can have different lengths | |
7405 | -- depending on the pointer convention. | |
7406 | ||
7407 | if Is_Access_Subprogram_Type (Target) | |
7408 | or else Is_Access_Subprogram_Type (Source) | |
7409 | or else OpenVMS_On_Target | |
7410 | then | |
7411 | Error_Msg_N | |
7412 | ("?conversion between pointers with different conventions!", N); | |
7413 | end if; | |
fbc67f84 | 7414 | end if; |
7415 | ||
3062c401 | 7416 | -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a |
7417 | -- warning when compiling GNAT-related sources. | |
7418 | ||
7419 | if Warn_On_Unchecked_Conversion | |
7420 | and then not In_Predefined_Unit (N) | |
7421 | and then RTU_Loaded (Ada_Calendar) | |
7422 | and then | |
7423 | (Chars (Source) = Name_Time | |
7424 | or else | |
7425 | Chars (Target) = Name_Time) | |
7426 | then | |
7427 | -- If Ada.Calendar is loaded and the name of one of the operands is | |
7428 | -- Time, there is a good chance that this is Ada.Calendar.Time. | |
7429 | ||
7430 | declare | |
7431 | Calendar_Time : constant Entity_Id := | |
7432 | Full_View (RTE (RO_CA_Time)); | |
7433 | begin | |
7434 | pragma Assert (Present (Calendar_Time)); | |
7435 | ||
7436 | if Source = Calendar_Time | |
7437 | or else Target = Calendar_Time | |
7438 | then | |
7439 | Error_Msg_N | |
7440 | ("?representation of 'Time values may change between " & | |
7441 | "'G'N'A'T versions", N); | |
7442 | end if; | |
7443 | end; | |
7444 | end if; | |
7445 | ||
fdd294d1 | 7446 | -- Make entry in unchecked conversion table for later processing by |
7447 | -- Validate_Unchecked_Conversions, which will check sizes and alignments | |
7448 | -- (using values set by the back-end where possible). This is only done | |
7449 | -- if the appropriate warning is active. | |
d6f39728 | 7450 | |
9dfe12ae | 7451 | if Warn_On_Unchecked_Conversion then |
7452 | Unchecked_Conversions.Append | |
7453 | (New_Val => UC_Entry' | |
299480f9 | 7454 | (Eloc => Sloc (N), |
9dfe12ae | 7455 | Source => Source, |
7456 | Target => Target)); | |
7457 | ||
7458 | -- If both sizes are known statically now, then back end annotation | |
7459 | -- is not required to do a proper check but if either size is not | |
7460 | -- known statically, then we need the annotation. | |
7461 | ||
7462 | if Known_Static_RM_Size (Source) | |
7463 | and then Known_Static_RM_Size (Target) | |
7464 | then | |
7465 | null; | |
7466 | else | |
7467 | Back_Annotate_Rep_Info := True; | |
7468 | end if; | |
7469 | end if; | |
d6f39728 | 7470 | |
fdd294d1 | 7471 | -- If unchecked conversion to access type, and access type is declared |
7472 | -- in the same unit as the unchecked conversion, then set the | |
7473 | -- No_Strict_Aliasing flag (no strict aliasing is implicit in this | |
7474 | -- situation). | |
28ed91d4 | 7475 | |
7476 | if Is_Access_Type (Target) and then | |
7477 | In_Same_Source_Unit (Target, N) | |
7478 | then | |
7479 | Set_No_Strict_Aliasing (Implementation_Base_Type (Target)); | |
7480 | end if; | |
3d875462 | 7481 | |
7482 | -- Generate N_Validate_Unchecked_Conversion node for back end in | |
7483 | -- case the back end needs to perform special validation checks. | |
7484 | ||
fdd294d1 | 7485 | -- Shouldn't this be in Exp_Ch13, since the check only gets done |
3d875462 | 7486 | -- if we have full expansion and the back end is called ??? |
7487 | ||
7488 | Vnode := | |
7489 | Make_Validate_Unchecked_Conversion (Sloc (N)); | |
7490 | Set_Source_Type (Vnode, Source); | |
7491 | Set_Target_Type (Vnode, Target); | |
7492 | ||
fdd294d1 | 7493 | -- If the unchecked conversion node is in a list, just insert before it. |
7494 | -- If not we have some strange case, not worth bothering about. | |
3d875462 | 7495 | |
7496 | if Is_List_Member (N) then | |
d6f39728 | 7497 | Insert_After (N, Vnode); |
7498 | end if; | |
7499 | end Validate_Unchecked_Conversion; | |
7500 | ||
7501 | ------------------------------------ | |
7502 | -- Validate_Unchecked_Conversions -- | |
7503 | ------------------------------------ | |
7504 | ||
7505 | procedure Validate_Unchecked_Conversions is | |
7506 | begin | |
7507 | for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop | |
7508 | declare | |
7509 | T : UC_Entry renames Unchecked_Conversions.Table (N); | |
7510 | ||
299480f9 | 7511 | Eloc : constant Source_Ptr := T.Eloc; |
7512 | Source : constant Entity_Id := T.Source; | |
7513 | Target : constant Entity_Id := T.Target; | |
d6f39728 | 7514 | |
7515 | Source_Siz : Uint; | |
7516 | Target_Siz : Uint; | |
7517 | ||
7518 | begin | |
fdd294d1 | 7519 | -- This validation check, which warns if we have unequal sizes for |
7520 | -- unchecked conversion, and thus potentially implementation | |
d6f39728 | 7521 | -- dependent semantics, is one of the few occasions on which we |
fdd294d1 | 7522 | -- use the official RM size instead of Esize. See description in |
7523 | -- Einfo "Handling of Type'Size Values" for details. | |
d6f39728 | 7524 | |
f15731c4 | 7525 | if Serious_Errors_Detected = 0 |
d6f39728 | 7526 | and then Known_Static_RM_Size (Source) |
7527 | and then Known_Static_RM_Size (Target) | |
f25f4252 | 7528 | |
7529 | -- Don't do the check if warnings off for either type, note the | |
7530 | -- deliberate use of OR here instead of OR ELSE to get the flag | |
7531 | -- Warnings_Off_Used set for both types if appropriate. | |
7532 | ||
7533 | and then not (Has_Warnings_Off (Source) | |
7534 | or | |
7535 | Has_Warnings_Off (Target)) | |
d6f39728 | 7536 | then |
7537 | Source_Siz := RM_Size (Source); | |
7538 | Target_Siz := RM_Size (Target); | |
7539 | ||
7540 | if Source_Siz /= Target_Siz then | |
299480f9 | 7541 | Error_Msg |
fbc67f84 | 7542 | ("?types for unchecked conversion have different sizes!", |
299480f9 | 7543 | Eloc); |
d6f39728 | 7544 | |
7545 | if All_Errors_Mode then | |
7546 | Error_Msg_Name_1 := Chars (Source); | |
7547 | Error_Msg_Uint_1 := Source_Siz; | |
7548 | Error_Msg_Name_2 := Chars (Target); | |
7549 | Error_Msg_Uint_2 := Target_Siz; | |
299480f9 | 7550 | Error_Msg ("\size of % is ^, size of % is ^?", Eloc); |
d6f39728 | 7551 | |
7552 | Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz); | |
7553 | ||
7554 | if Is_Discrete_Type (Source) | |
7555 | and then Is_Discrete_Type (Target) | |
7556 | then | |
7557 | if Source_Siz > Target_Siz then | |
299480f9 | 7558 | Error_Msg |
fbc67f84 | 7559 | ("\?^ high order bits of source will be ignored!", |
299480f9 | 7560 | Eloc); |
d6f39728 | 7561 | |
9dfe12ae | 7562 | elsif Is_Unsigned_Type (Source) then |
299480f9 | 7563 | Error_Msg |
fbc67f84 | 7564 | ("\?source will be extended with ^ high order " & |
299480f9 | 7565 | "zero bits?!", Eloc); |
d6f39728 | 7566 | |
7567 | else | |
299480f9 | 7568 | Error_Msg |
fbc67f84 | 7569 | ("\?source will be extended with ^ high order " & |
7570 | "sign bits!", | |
299480f9 | 7571 | Eloc); |
d6f39728 | 7572 | end if; |
7573 | ||
7574 | elsif Source_Siz < Target_Siz then | |
7575 | if Is_Discrete_Type (Target) then | |
7576 | if Bytes_Big_Endian then | |
299480f9 | 7577 | Error_Msg |
fbc67f84 | 7578 | ("\?target value will include ^ undefined " & |
7579 | "low order bits!", | |
299480f9 | 7580 | Eloc); |
d6f39728 | 7581 | else |
299480f9 | 7582 | Error_Msg |
fbc67f84 | 7583 | ("\?target value will include ^ undefined " & |
7584 | "high order bits!", | |
299480f9 | 7585 | Eloc); |
d6f39728 | 7586 | end if; |
7587 | ||
7588 | else | |
299480f9 | 7589 | Error_Msg |
fbc67f84 | 7590 | ("\?^ trailing bits of target value will be " & |
299480f9 | 7591 | "undefined!", Eloc); |
d6f39728 | 7592 | end if; |
7593 | ||
7594 | else pragma Assert (Source_Siz > Target_Siz); | |
299480f9 | 7595 | Error_Msg |
fbc67f84 | 7596 | ("\?^ trailing bits of source will be ignored!", |
299480f9 | 7597 | Eloc); |
d6f39728 | 7598 | end if; |
7599 | end if; | |
d6f39728 | 7600 | end if; |
7601 | end if; | |
7602 | ||
7603 | -- If both types are access types, we need to check the alignment. | |
7604 | -- If the alignment of both is specified, we can do it here. | |
7605 | ||
f15731c4 | 7606 | if Serious_Errors_Detected = 0 |
d6f39728 | 7607 | and then Ekind (Source) in Access_Kind |
7608 | and then Ekind (Target) in Access_Kind | |
7609 | and then Target_Strict_Alignment | |
7610 | and then Present (Designated_Type (Source)) | |
7611 | and then Present (Designated_Type (Target)) | |
7612 | then | |
7613 | declare | |
7614 | D_Source : constant Entity_Id := Designated_Type (Source); | |
7615 | D_Target : constant Entity_Id := Designated_Type (Target); | |
7616 | ||
7617 | begin | |
7618 | if Known_Alignment (D_Source) | |
7619 | and then Known_Alignment (D_Target) | |
7620 | then | |
7621 | declare | |
7622 | Source_Align : constant Uint := Alignment (D_Source); | |
7623 | Target_Align : constant Uint := Alignment (D_Target); | |
7624 | ||
7625 | begin | |
7626 | if Source_Align < Target_Align | |
7627 | and then not Is_Tagged_Type (D_Source) | |
f25f4252 | 7628 | |
7629 | -- Suppress warning if warnings suppressed on either | |
7630 | -- type or either designated type. Note the use of | |
7631 | -- OR here instead of OR ELSE. That is intentional, | |
7632 | -- we would like to set flag Warnings_Off_Used in | |
7633 | -- all types for which warnings are suppressed. | |
7634 | ||
7635 | and then not (Has_Warnings_Off (D_Source) | |
7636 | or | |
7637 | Has_Warnings_Off (D_Target) | |
7638 | or | |
7639 | Has_Warnings_Off (Source) | |
7640 | or | |
7641 | Has_Warnings_Off (Target)) | |
d6f39728 | 7642 | then |
d6f39728 | 7643 | Error_Msg_Uint_1 := Target_Align; |
7644 | Error_Msg_Uint_2 := Source_Align; | |
299480f9 | 7645 | Error_Msg_Node_1 := D_Target; |
d6f39728 | 7646 | Error_Msg_Node_2 := D_Source; |
299480f9 | 7647 | Error_Msg |
fbc67f84 | 7648 | ("?alignment of & (^) is stricter than " & |
299480f9 | 7649 | "alignment of & (^)!", Eloc); |
f25f4252 | 7650 | Error_Msg |
7651 | ("\?resulting access value may have invalid " & | |
7652 | "alignment!", Eloc); | |
d6f39728 | 7653 | end if; |
7654 | end; | |
7655 | end if; | |
7656 | end; | |
7657 | end if; | |
7658 | end; | |
7659 | end loop; | |
7660 | end Validate_Unchecked_Conversions; | |
7661 | ||
d6f39728 | 7662 | end Sem_Ch13; |