]>
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 | -- -- | |
49213728 | 9 | -- Copyright (C) 1992-2012, Free Software Foundation, Inc. -- |
d6f39728 | 10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
80df182a | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
d6f39728 | 14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
80df182a | 18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
d6f39728 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
d6f39728 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
ae888dbd | 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; |
490beba6 | 47 | with Sem_Ch6; use Sem_Ch6; |
d6f39728 | 48 | with Sem_Ch8; use Sem_Ch8; |
89f1e35c | 49 | with Sem_Ch9; use Sem_Ch9; |
85696508 | 50 | with Sem_Dim; use Sem_Dim; |
85377c9b | 51 | with Sem_Disp; use Sem_Disp; |
d6f39728 | 52 | with Sem_Eval; use Sem_Eval; |
53 | with Sem_Res; use Sem_Res; | |
54 | with Sem_Type; use Sem_Type; | |
55 | with Sem_Util; use Sem_Util; | |
44e4341e | 56 | with Sem_Warn; use Sem_Warn; |
1e3c4ae6 | 57 | with Sinput; use Sinput; |
9dfe12ae | 58 | with Snames; use Snames; |
d6f39728 | 59 | with Stand; use Stand; |
60 | with Sinfo; use Sinfo; | |
5b5df4a9 | 61 | with Stringt; use Stringt; |
93735cb8 | 62 | with Targparm; use Targparm; |
d6f39728 | 63 | with Ttypes; use Ttypes; |
64 | with Tbuild; use Tbuild; | |
65 | with Urealp; use Urealp; | |
f42f24d7 | 66 | with Warnsw; use Warnsw; |
d6f39728 | 67 | |
bfa5a9d9 | 68 | with GNAT.Heap_Sort_G; |
d6f39728 | 69 | |
70 | package body Sem_Ch13 is | |
71 | ||
72 | SSU : constant Pos := System_Storage_Unit; | |
73 | -- Convenient short hand for commonly used constant | |
74 | ||
75 | ----------------------- | |
76 | -- Local Subprograms -- | |
77 | ----------------------- | |
78 | ||
1d366b32 | 79 | procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint); |
80 | -- This routine is called after setting one of the sizes of type entity | |
81 | -- Typ to Size. The purpose is to deal with the situation of a derived | |
82 | -- type whose inherited alignment is no longer appropriate for the new | |
83 | -- size value. In this case, we reset the Alignment to unknown. | |
d6f39728 | 84 | |
490beba6 | 85 | procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id); |
9dc88aea | 86 | -- If Typ has predicates (indicated by Has_Predicates being set for Typ, |
9f727ad2 | 87 | -- then either there are pragma Predicate entries on the rep chain for the |
6fb3c314 | 88 | -- type (note that Predicate aspects are converted to pragma Predicate), or |
490beba6 | 89 | -- there are inherited aspects from a parent type, or ancestor subtypes. |
90 | -- This procedure builds the spec and body for the Predicate function that | |
91 | -- tests these predicates. N is the freeze node for the type. The spec of | |
92 | -- the function is inserted before the freeze node, and the body of the | |
6fb3c314 | 93 | -- function is inserted after the freeze node. |
9dc88aea | 94 | |
d97beb2f | 95 | procedure Build_Static_Predicate |
96 | (Typ : Entity_Id; | |
97 | Expr : Node_Id; | |
98 | Nam : Name_Id); | |
d7c2851f | 99 | -- Given a predicated type Typ, where Typ is a discrete static subtype, |
100 | -- whose predicate expression is Expr, tests if Expr is a static predicate, | |
101 | -- and if so, builds the predicate range list. Nam is the name of the one | |
102 | -- argument to the predicate function. Occurrences of the type name in the | |
6fb3c314 | 103 | -- predicate expression have been replaced by identifier references to this |
d7c2851f | 104 | -- name, which is unique, so any identifier with Chars matching Nam must be |
105 | -- a reference to the type. If the predicate is non-static, this procedure | |
106 | -- returns doing nothing. If the predicate is static, then the predicate | |
107 | -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as | |
108 | -- a canonicalized membership operation. | |
d97beb2f | 109 | |
d6f39728 | 110 | function Get_Alignment_Value (Expr : Node_Id) return Uint; |
111 | -- Given the expression for an alignment value, returns the corresponding | |
112 | -- Uint value. If the value is inappropriate, then error messages are | |
113 | -- posted as required, and a value of No_Uint is returned. | |
114 | ||
115 | function Is_Operational_Item (N : Node_Id) return Boolean; | |
1e3c4ae6 | 116 | -- A specification for a stream attribute is allowed before the full type |
117 | -- is declared, as explained in AI-00137 and the corrigendum. Attributes | |
118 | -- that do not specify a representation characteristic are operational | |
119 | -- attributes. | |
d6f39728 | 120 | |
44e4341e | 121 | procedure New_Stream_Subprogram |
d6f39728 | 122 | (N : Node_Id; |
123 | Ent : Entity_Id; | |
124 | Subp : Entity_Id; | |
9dfe12ae | 125 | Nam : TSS_Name_Type); |
44e4341e | 126 | -- Create a subprogram renaming of a given stream attribute to the |
127 | -- designated subprogram and then in the tagged case, provide this as a | |
128 | -- primitive operation, or in the non-tagged case make an appropriate TSS | |
129 | -- entry. This is more properly an expansion activity than just semantics, | |
130 | -- but the presence of user-defined stream functions for limited types is a | |
131 | -- legality check, which is why this takes place here rather than in | |
132 | -- exp_ch13, where it was previously. Nam indicates the name of the TSS | |
133 | -- function to be generated. | |
9dfe12ae | 134 | -- |
f15731c4 | 135 | -- To avoid elaboration anomalies with freeze nodes, for untagged types |
136 | -- we generate both a subprogram declaration and a subprogram renaming | |
137 | -- declaration, so that the attribute specification is handled as a | |
138 | -- renaming_as_body. For tagged types, the specification is one of the | |
139 | -- primitive specs. | |
140 | ||
2072eaa9 | 141 | generic |
142 | with procedure Replace_Type_Reference (N : Node_Id); | |
143 | procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id); | |
144 | -- This is used to scan an expression for a predicate or invariant aspect | |
145 | -- replacing occurrences of the name TName (the name of the subtype to | |
146 | -- which the aspect applies) with appropriate references to the parameter | |
147 | -- of the predicate function or invariant procedure. The procedure passed | |
148 | -- as a generic parameter does the actual replacement of node N, which is | |
149 | -- either a simple direct reference to TName, or a selected component that | |
150 | -- represents an appropriately qualified occurrence of TName. | |
151 | ||
b77e4501 | 152 | procedure Set_Biased |
153 | (E : Entity_Id; | |
154 | N : Node_Id; | |
155 | Msg : String; | |
156 | Biased : Boolean := True); | |
157 | -- If Biased is True, sets Has_Biased_Representation flag for E, and | |
158 | -- outputs a warning message at node N if Warn_On_Biased_Representation is | |
159 | -- is True. This warning inserts the string Msg to describe the construct | |
160 | -- causing biasing. | |
161 | ||
d6f39728 | 162 | ---------------------------------------------- |
163 | -- Table for Validate_Unchecked_Conversions -- | |
164 | ---------------------------------------------- | |
165 | ||
166 | -- The following table collects unchecked conversions for validation. | |
95deda50 | 167 | -- Entries are made by Validate_Unchecked_Conversion and then the call |
168 | -- to Validate_Unchecked_Conversions does the actual error checking and | |
169 | -- posting of warnings. The reason for this delayed processing is to take | |
170 | -- advantage of back-annotations of size and alignment values performed by | |
171 | -- the back end. | |
d6f39728 | 172 | |
95deda50 | 173 | -- Note: the reason we store a Source_Ptr value instead of a Node_Id is |
174 | -- that by the time Validate_Unchecked_Conversions is called, Sprint will | |
175 | -- already have modified all Sloc values if the -gnatD option is set. | |
299480f9 | 176 | |
d6f39728 | 177 | type UC_Entry is record |
299480f9 | 178 | Eloc : Source_Ptr; -- node used for posting warnings |
179 | Source : Entity_Id; -- source type for unchecked conversion | |
180 | Target : Entity_Id; -- target type for unchecked conversion | |
d6f39728 | 181 | end record; |
182 | ||
183 | package Unchecked_Conversions is new Table.Table ( | |
184 | Table_Component_Type => UC_Entry, | |
185 | Table_Index_Type => Int, | |
186 | Table_Low_Bound => 1, | |
187 | Table_Initial => 50, | |
188 | Table_Increment => 200, | |
189 | Table_Name => "Unchecked_Conversions"); | |
190 | ||
83f8f0a6 | 191 | ---------------------------------------- |
192 | -- Table for Validate_Address_Clauses -- | |
193 | ---------------------------------------- | |
194 | ||
195 | -- If an address clause has the form | |
196 | ||
197 | -- for X'Address use Expr | |
198 | ||
95deda50 | 199 | -- where Expr is of the form Y'Address or recursively is a reference to a |
200 | -- constant of either of these forms, and X and Y are entities of objects, | |
201 | -- then if Y has a smaller alignment than X, that merits a warning about | |
202 | -- possible bad alignment. The following table collects address clauses of | |
203 | -- this kind. We put these in a table so that they can be checked after the | |
204 | -- back end has completed annotation of the alignments of objects, since we | |
205 | -- can catch more cases that way. | |
83f8f0a6 | 206 | |
207 | type Address_Clause_Check_Record is record | |
208 | N : Node_Id; | |
209 | -- The address clause | |
210 | ||
211 | X : Entity_Id; | |
212 | -- The entity of the object overlaying Y | |
213 | ||
214 | Y : Entity_Id; | |
215 | -- The entity of the object being overlaid | |
d6da7448 | 216 | |
217 | Off : Boolean; | |
6fb3c314 | 218 | -- Whether the address is offset within Y |
83f8f0a6 | 219 | end record; |
220 | ||
221 | package Address_Clause_Checks is new Table.Table ( | |
222 | Table_Component_Type => Address_Clause_Check_Record, | |
223 | Table_Index_Type => Int, | |
224 | Table_Low_Bound => 1, | |
225 | Table_Initial => 20, | |
226 | Table_Increment => 200, | |
227 | Table_Name => "Address_Clause_Checks"); | |
228 | ||
59ac57b5 | 229 | ----------------------------------------- |
230 | -- Adjust_Record_For_Reverse_Bit_Order -- | |
231 | ----------------------------------------- | |
232 | ||
233 | procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is | |
67278d60 | 234 | Comp : Node_Id; |
235 | CC : Node_Id; | |
59ac57b5 | 236 | |
237 | begin | |
67278d60 | 238 | -- Processing depends on version of Ada |
59ac57b5 | 239 | |
6797073f | 240 | -- For Ada 95, we just renumber bits within a storage unit. We do the |
568b0f6a | 241 | -- same for Ada 83 mode, since we recognize the Bit_Order attribute in |
ab19a652 | 242 | -- Ada 83, and are free to add this extension. |
6797073f | 243 | |
244 | if Ada_Version < Ada_2005 then | |
245 | Comp := First_Component_Or_Discriminant (R); | |
246 | while Present (Comp) loop | |
247 | CC := Component_Clause (Comp); | |
248 | ||
249 | -- If component clause is present, then deal with the non-default | |
250 | -- bit order case for Ada 95 mode. | |
251 | ||
252 | -- We only do this processing for the base type, and in fact that | |
253 | -- is important, since otherwise if there are record subtypes, we | |
254 | -- could reverse the bits once for each subtype, which is wrong. | |
255 | ||
b9e61b2a | 256 | if Present (CC) and then Ekind (R) = E_Record_Type then |
6797073f | 257 | declare |
258 | CFB : constant Uint := Component_Bit_Offset (Comp); | |
259 | CSZ : constant Uint := Esize (Comp); | |
260 | CLC : constant Node_Id := Component_Clause (Comp); | |
261 | Pos : constant Node_Id := Position (CLC); | |
262 | FB : constant Node_Id := First_Bit (CLC); | |
263 | ||
264 | Storage_Unit_Offset : constant Uint := | |
265 | CFB / System_Storage_Unit; | |
266 | ||
267 | Start_Bit : constant Uint := | |
268 | CFB mod System_Storage_Unit; | |
59ac57b5 | 269 | |
6797073f | 270 | begin |
271 | -- Cases where field goes over storage unit boundary | |
59ac57b5 | 272 | |
6797073f | 273 | if Start_Bit + CSZ > System_Storage_Unit then |
59ac57b5 | 274 | |
6797073f | 275 | -- Allow multi-byte field but generate warning |
59ac57b5 | 276 | |
6797073f | 277 | if Start_Bit mod System_Storage_Unit = 0 |
278 | and then CSZ mod System_Storage_Unit = 0 | |
279 | then | |
280 | Error_Msg_N | |
281 | ("multi-byte field specified with non-standard" | |
1e3532e7 | 282 | & " Bit_Order??", CLC); |
31486bc0 | 283 | |
6797073f | 284 | if Bytes_Big_Endian then |
31486bc0 | 285 | Error_Msg_N |
6797073f | 286 | ("bytes are not reversed " |
1e3532e7 | 287 | & "(component is big-endian)??", CLC); |
31486bc0 | 288 | else |
289 | Error_Msg_N | |
6797073f | 290 | ("bytes are not reversed " |
1e3532e7 | 291 | & "(component is little-endian)??", CLC); |
31486bc0 | 292 | end if; |
59ac57b5 | 293 | |
6797073f | 294 | -- Do not allow non-contiguous field |
59ac57b5 | 295 | |
67278d60 | 296 | else |
6797073f | 297 | Error_Msg_N |
298 | ("attempt to specify non-contiguous field " | |
299 | & "not permitted", CLC); | |
300 | Error_Msg_N | |
301 | ("\caused by non-standard Bit_Order " | |
302 | & "specified", CLC); | |
303 | Error_Msg_N | |
304 | ("\consider possibility of using " | |
305 | & "Ada 2005 mode here", CLC); | |
306 | end if; | |
59ac57b5 | 307 | |
6797073f | 308 | -- Case where field fits in one storage unit |
59ac57b5 | 309 | |
6797073f | 310 | else |
311 | -- Give warning if suspicious component clause | |
59ac57b5 | 312 | |
6797073f | 313 | if Intval (FB) >= System_Storage_Unit |
314 | and then Warn_On_Reverse_Bit_Order | |
315 | then | |
316 | Error_Msg_N | |
1e3532e7 | 317 | ("Bit_Order clause does not affect " & |
318 | "byte ordering?V?", Pos); | |
6797073f | 319 | Error_Msg_Uint_1 := |
320 | Intval (Pos) + Intval (FB) / | |
321 | System_Storage_Unit; | |
322 | Error_Msg_N | |
1e3532e7 | 323 | ("position normalized to ^ before bit " & |
324 | "order interpreted?V?", Pos); | |
6797073f | 325 | end if; |
59ac57b5 | 326 | |
6797073f | 327 | -- Here is where we fix up the Component_Bit_Offset value |
328 | -- to account for the reverse bit order. Some examples of | |
329 | -- what needs to be done are: | |
bfa5a9d9 | 330 | |
6797073f | 331 | -- First_Bit .. Last_Bit Component_Bit_Offset |
332 | -- old new old new | |
59ac57b5 | 333 | |
6797073f | 334 | -- 0 .. 0 7 .. 7 0 7 |
335 | -- 0 .. 1 6 .. 7 0 6 | |
336 | -- 0 .. 2 5 .. 7 0 5 | |
337 | -- 0 .. 7 0 .. 7 0 4 | |
59ac57b5 | 338 | |
6797073f | 339 | -- 1 .. 1 6 .. 6 1 6 |
340 | -- 1 .. 4 3 .. 6 1 3 | |
341 | -- 4 .. 7 0 .. 3 4 0 | |
59ac57b5 | 342 | |
6797073f | 343 | -- The rule is that the first bit is is obtained by |
344 | -- subtracting the old ending bit from storage_unit - 1. | |
59ac57b5 | 345 | |
6797073f | 346 | Set_Component_Bit_Offset |
347 | (Comp, | |
348 | (Storage_Unit_Offset * System_Storage_Unit) + | |
349 | (System_Storage_Unit - 1) - | |
350 | (Start_Bit + CSZ - 1)); | |
59ac57b5 | 351 | |
6797073f | 352 | Set_Normalized_First_Bit |
353 | (Comp, | |
354 | Component_Bit_Offset (Comp) mod | |
355 | System_Storage_Unit); | |
356 | end if; | |
357 | end; | |
358 | end if; | |
359 | ||
360 | Next_Component_Or_Discriminant (Comp); | |
361 | end loop; | |
362 | ||
363 | -- For Ada 2005, we do machine scalar processing, as fully described In | |
364 | -- AI-133. This involves gathering all components which start at the | |
365 | -- same byte offset and processing them together. Same approach is still | |
366 | -- valid in later versions including Ada 2012. | |
367 | ||
368 | else | |
369 | declare | |
370 | Max_Machine_Scalar_Size : constant Uint := | |
371 | UI_From_Int | |
372 | (Standard_Long_Long_Integer_Size); | |
67278d60 | 373 | -- We use this as the maximum machine scalar size |
59ac57b5 | 374 | |
6797073f | 375 | Num_CC : Natural; |
376 | SSU : constant Uint := UI_From_Int (System_Storage_Unit); | |
59ac57b5 | 377 | |
6797073f | 378 | begin |
379 | -- This first loop through components does two things. First it | |
380 | -- deals with the case of components with component clauses whose | |
381 | -- length is greater than the maximum machine scalar size (either | |
382 | -- accepting them or rejecting as needed). Second, it counts the | |
383 | -- number of components with component clauses whose length does | |
384 | -- not exceed this maximum for later processing. | |
67278d60 | 385 | |
6797073f | 386 | Num_CC := 0; |
387 | Comp := First_Component_Or_Discriminant (R); | |
388 | while Present (Comp) loop | |
389 | CC := Component_Clause (Comp); | |
67278d60 | 390 | |
6797073f | 391 | if Present (CC) then |
392 | declare | |
1e3532e7 | 393 | Fbit : constant Uint := Static_Integer (First_Bit (CC)); |
394 | Lbit : constant Uint := Static_Integer (Last_Bit (CC)); | |
67278d60 | 395 | |
6797073f | 396 | begin |
b38e4131 | 397 | -- Case of component with last bit >= max machine scalar |
67278d60 | 398 | |
b38e4131 | 399 | if Lbit >= Max_Machine_Scalar_Size then |
67278d60 | 400 | |
b38e4131 | 401 | -- This is allowed only if first bit is zero, and |
402 | -- last bit + 1 is a multiple of storage unit size. | |
67278d60 | 403 | |
b38e4131 | 404 | if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then |
67278d60 | 405 | |
b38e4131 | 406 | -- This is the case to give a warning if enabled |
67278d60 | 407 | |
b38e4131 | 408 | if Warn_On_Reverse_Bit_Order then |
409 | Error_Msg_N | |
410 | ("multi-byte field specified with " | |
1e3532e7 | 411 | & " non-standard Bit_Order?V?", CC); |
b38e4131 | 412 | |
413 | if Bytes_Big_Endian then | |
414 | Error_Msg_N | |
415 | ("\bytes are not reversed " | |
1e3532e7 | 416 | & "(component is big-endian)?V?", CC); |
b38e4131 | 417 | else |
418 | Error_Msg_N | |
419 | ("\bytes are not reversed " | |
1e3532e7 | 420 | & "(component is little-endian)?V?", CC); |
b38e4131 | 421 | end if; |
422 | end if; | |
67278d60 | 423 | |
7eb0e22f | 424 | -- Give error message for RM 13.5.1(10) violation |
67278d60 | 425 | |
b38e4131 | 426 | else |
427 | Error_Msg_FE | |
428 | ("machine scalar rules not followed for&", | |
429 | First_Bit (CC), Comp); | |
67278d60 | 430 | |
b38e4131 | 431 | Error_Msg_Uint_1 := Lbit; |
432 | Error_Msg_Uint_2 := Max_Machine_Scalar_Size; | |
433 | Error_Msg_F | |
434 | ("\last bit (^) exceeds maximum machine " | |
435 | & "scalar size (^)", | |
436 | First_Bit (CC)); | |
67278d60 | 437 | |
b38e4131 | 438 | if (Lbit + 1) mod SSU /= 0 then |
439 | Error_Msg_Uint_1 := SSU; | |
440 | Error_Msg_F | |
441 | ("\and is not a multiple of Storage_Unit (^) " | |
0cafb066 | 442 | & "(RM 13.4.1(10))", |
b38e4131 | 443 | First_Bit (CC)); |
6797073f | 444 | |
6797073f | 445 | else |
b38e4131 | 446 | Error_Msg_Uint_1 := Fbit; |
447 | Error_Msg_F | |
448 | ("\and first bit (^) is non-zero " | |
0cafb066 | 449 | & "(RM 13.4.1(10))", |
b38e4131 | 450 | First_Bit (CC)); |
67278d60 | 451 | end if; |
6797073f | 452 | end if; |
59ac57b5 | 453 | |
b38e4131 | 454 | -- OK case of machine scalar related component clause, |
455 | -- For now, just count them. | |
59ac57b5 | 456 | |
6797073f | 457 | else |
458 | Num_CC := Num_CC + 1; | |
459 | end if; | |
460 | end; | |
461 | end if; | |
59ac57b5 | 462 | |
6797073f | 463 | Next_Component_Or_Discriminant (Comp); |
464 | end loop; | |
59ac57b5 | 465 | |
6797073f | 466 | -- We need to sort the component clauses on the basis of the |
467 | -- Position values in the clause, so we can group clauses with | |
468 | -- the same Position. together to determine the relevant machine | |
469 | -- scalar size. | |
59ac57b5 | 470 | |
6797073f | 471 | Sort_CC : declare |
472 | Comps : array (0 .. Num_CC) of Entity_Id; | |
473 | -- Array to collect component and discriminant entities. The | |
474 | -- data starts at index 1, the 0'th entry is for the sort | |
475 | -- routine. | |
59ac57b5 | 476 | |
6797073f | 477 | function CP_Lt (Op1, Op2 : Natural) return Boolean; |
478 | -- Compare routine for Sort | |
59ac57b5 | 479 | |
6797073f | 480 | procedure CP_Move (From : Natural; To : Natural); |
481 | -- Move routine for Sort | |
59ac57b5 | 482 | |
6797073f | 483 | package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt); |
59ac57b5 | 484 | |
6797073f | 485 | Start : Natural; |
486 | Stop : Natural; | |
487 | -- Start and stop positions in the component list of the set of | |
488 | -- components with the same starting position (that constitute | |
489 | -- components in a single machine scalar). | |
59ac57b5 | 490 | |
6797073f | 491 | MaxL : Uint; |
492 | -- Maximum last bit value of any component in this set | |
59ac57b5 | 493 | |
6797073f | 494 | MSS : Uint; |
495 | -- Corresponding machine scalar size | |
67278d60 | 496 | |
6797073f | 497 | ----------- |
498 | -- CP_Lt -- | |
499 | ----------- | |
67278d60 | 500 | |
6797073f | 501 | function CP_Lt (Op1, Op2 : Natural) return Boolean is |
502 | begin | |
503 | return Position (Component_Clause (Comps (Op1))) < | |
504 | Position (Component_Clause (Comps (Op2))); | |
505 | end CP_Lt; | |
67278d60 | 506 | |
6797073f | 507 | ------------- |
508 | -- CP_Move -- | |
509 | ------------- | |
67278d60 | 510 | |
6797073f | 511 | procedure CP_Move (From : Natural; To : Natural) is |
512 | begin | |
513 | Comps (To) := Comps (From); | |
514 | end CP_Move; | |
67278d60 | 515 | |
516 | -- Start of processing for Sort_CC | |
59ac57b5 | 517 | |
6797073f | 518 | begin |
b38e4131 | 519 | -- Collect the machine scalar relevant component clauses |
59ac57b5 | 520 | |
6797073f | 521 | Num_CC := 0; |
522 | Comp := First_Component_Or_Discriminant (R); | |
523 | while Present (Comp) loop | |
b38e4131 | 524 | declare |
525 | CC : constant Node_Id := Component_Clause (Comp); | |
526 | ||
527 | begin | |
528 | -- Collect only component clauses whose last bit is less | |
529 | -- than machine scalar size. Any component clause whose | |
530 | -- last bit exceeds this value does not take part in | |
531 | -- machine scalar layout considerations. The test for | |
532 | -- Error_Posted makes sure we exclude component clauses | |
533 | -- for which we already posted an error. | |
534 | ||
535 | if Present (CC) | |
536 | and then not Error_Posted (Last_Bit (CC)) | |
537 | and then Static_Integer (Last_Bit (CC)) < | |
d64221a7 | 538 | Max_Machine_Scalar_Size |
b38e4131 | 539 | then |
540 | Num_CC := Num_CC + 1; | |
541 | Comps (Num_CC) := Comp; | |
542 | end if; | |
543 | end; | |
59ac57b5 | 544 | |
6797073f | 545 | Next_Component_Or_Discriminant (Comp); |
546 | end loop; | |
67278d60 | 547 | |
6797073f | 548 | -- Sort by ascending position number |
67278d60 | 549 | |
6797073f | 550 | Sorting.Sort (Num_CC); |
67278d60 | 551 | |
6797073f | 552 | -- We now have all the components whose size does not exceed |
553 | -- the max machine scalar value, sorted by starting position. | |
554 | -- In this loop we gather groups of clauses starting at the | |
555 | -- same position, to process them in accordance with AI-133. | |
67278d60 | 556 | |
6797073f | 557 | Stop := 0; |
558 | while Stop < Num_CC loop | |
559 | Start := Stop + 1; | |
560 | Stop := Start; | |
561 | MaxL := | |
562 | Static_Integer | |
563 | (Last_Bit (Component_Clause (Comps (Start)))); | |
67278d60 | 564 | while Stop < Num_CC loop |
6797073f | 565 | if Static_Integer |
566 | (Position (Component_Clause (Comps (Stop + 1)))) = | |
567 | Static_Integer | |
568 | (Position (Component_Clause (Comps (Stop)))) | |
569 | then | |
570 | Stop := Stop + 1; | |
571 | MaxL := | |
572 | UI_Max | |
573 | (MaxL, | |
574 | Static_Integer | |
575 | (Last_Bit | |
576 | (Component_Clause (Comps (Stop))))); | |
577 | else | |
578 | exit; | |
579 | end if; | |
580 | end loop; | |
67278d60 | 581 | |
6797073f | 582 | -- Now we have a group of component clauses from Start to |
583 | -- Stop whose positions are identical, and MaxL is the | |
584 | -- maximum last bit value of any of these components. | |
585 | ||
586 | -- We need to determine the corresponding machine scalar | |
587 | -- size. This loop assumes that machine scalar sizes are | |
588 | -- even, and that each possible machine scalar has twice | |
589 | -- as many bits as the next smaller one. | |
590 | ||
591 | MSS := Max_Machine_Scalar_Size; | |
592 | while MSS mod 2 = 0 | |
593 | and then (MSS / 2) >= SSU | |
594 | and then (MSS / 2) > MaxL | |
595 | loop | |
596 | MSS := MSS / 2; | |
597 | end loop; | |
67278d60 | 598 | |
6797073f | 599 | -- Here is where we fix up the Component_Bit_Offset value |
600 | -- to account for the reverse bit order. Some examples of | |
601 | -- what needs to be done for the case of a machine scalar | |
602 | -- size of 8 are: | |
67278d60 | 603 | |
6797073f | 604 | -- First_Bit .. Last_Bit Component_Bit_Offset |
605 | -- old new old new | |
67278d60 | 606 | |
6797073f | 607 | -- 0 .. 0 7 .. 7 0 7 |
608 | -- 0 .. 1 6 .. 7 0 6 | |
609 | -- 0 .. 2 5 .. 7 0 5 | |
610 | -- 0 .. 7 0 .. 7 0 4 | |
67278d60 | 611 | |
6797073f | 612 | -- 1 .. 1 6 .. 6 1 6 |
613 | -- 1 .. 4 3 .. 6 1 3 | |
614 | -- 4 .. 7 0 .. 3 4 0 | |
67278d60 | 615 | |
6797073f | 616 | -- The rule is that the first bit is obtained by subtracting |
617 | -- the old ending bit from machine scalar size - 1. | |
67278d60 | 618 | |
6797073f | 619 | for C in Start .. Stop loop |
620 | declare | |
621 | Comp : constant Entity_Id := Comps (C); | |
b9e61b2a | 622 | CC : constant Node_Id := Component_Clause (Comp); |
623 | ||
624 | LB : constant Uint := Static_Integer (Last_Bit (CC)); | |
6797073f | 625 | NFB : constant Uint := MSS - Uint_1 - LB; |
626 | NLB : constant Uint := NFB + Esize (Comp) - 1; | |
b9e61b2a | 627 | Pos : constant Uint := Static_Integer (Position (CC)); |
67278d60 | 628 | |
6797073f | 629 | begin |
630 | if Warn_On_Reverse_Bit_Order then | |
631 | Error_Msg_Uint_1 := MSS; | |
632 | Error_Msg_N | |
633 | ("info: reverse bit order in machine " & | |
1e3532e7 | 634 | "scalar of length^?V?", First_Bit (CC)); |
6797073f | 635 | Error_Msg_Uint_1 := NFB; |
636 | Error_Msg_Uint_2 := NLB; | |
637 | ||
638 | if Bytes_Big_Endian then | |
639 | Error_Msg_NE | |
1e3532e7 | 640 | ("\info: big-endian range for " |
641 | & "component & is ^ .. ^?V?", | |
6797073f | 642 | First_Bit (CC), Comp); |
643 | else | |
644 | Error_Msg_NE | |
1e3532e7 | 645 | ("\info: little-endian range " |
646 | & "for component & is ^ .. ^?V?", | |
6797073f | 647 | First_Bit (CC), Comp); |
67278d60 | 648 | end if; |
6797073f | 649 | end if; |
67278d60 | 650 | |
6797073f | 651 | Set_Component_Bit_Offset (Comp, Pos * SSU + NFB); |
652 | Set_Normalized_First_Bit (Comp, NFB mod SSU); | |
653 | end; | |
67278d60 | 654 | end loop; |
6797073f | 655 | end loop; |
656 | end Sort_CC; | |
657 | end; | |
658 | end if; | |
59ac57b5 | 659 | end Adjust_Record_For_Reverse_Bit_Order; |
660 | ||
1d366b32 | 661 | ------------------------------------- |
662 | -- Alignment_Check_For_Size_Change -- | |
663 | ------------------------------------- | |
d6f39728 | 664 | |
1d366b32 | 665 | procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is |
d6f39728 | 666 | begin |
667 | -- If the alignment is known, and not set by a rep clause, and is | |
668 | -- inconsistent with the size being set, then reset it to unknown, | |
669 | -- we assume in this case that the size overrides the inherited | |
670 | -- alignment, and that the alignment must be recomputed. | |
671 | ||
672 | if Known_Alignment (Typ) | |
673 | and then not Has_Alignment_Clause (Typ) | |
1d366b32 | 674 | and then Size mod (Alignment (Typ) * SSU) /= 0 |
d6f39728 | 675 | then |
676 | Init_Alignment (Typ); | |
677 | end if; | |
1d366b32 | 678 | end Alignment_Check_For_Size_Change; |
d6f39728 | 679 | |
06ef5f86 | 680 | ------------------------------------- |
681 | -- Analyze_Aspects_At_Freeze_Point -- | |
682 | ------------------------------------- | |
683 | ||
684 | procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is | |
685 | ASN : Node_Id; | |
686 | A_Id : Aspect_Id; | |
687 | Ritem : Node_Id; | |
688 | ||
689 | procedure Analyze_Aspect_Default_Value (ASN : Node_Id); | |
690 | -- This routine analyzes an Aspect_Default_[Component_]Value denoted by | |
691 | -- the aspect specification node ASN. | |
692 | ||
693 | procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id); | |
694 | -- Given an aspect specification node ASN whose expression is an | |
695 | -- optional Boolean, this routines creates the corresponding pragma | |
696 | -- at the freezing point. | |
697 | ||
698 | ---------------------------------- | |
699 | -- Analyze_Aspect_Default_Value -- | |
700 | ---------------------------------- | |
701 | ||
702 | procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is | |
703 | Ent : constant Entity_Id := Entity (ASN); | |
704 | Expr : constant Node_Id := Expression (ASN); | |
705 | Id : constant Node_Id := Identifier (ASN); | |
706 | ||
707 | begin | |
708 | Error_Msg_Name_1 := Chars (Id); | |
709 | ||
710 | if not Is_Type (Ent) then | |
711 | Error_Msg_N ("aspect% can only apply to a type", Id); | |
712 | return; | |
713 | ||
714 | elsif not Is_First_Subtype (Ent) then | |
715 | Error_Msg_N ("aspect% cannot apply to subtype", Id); | |
716 | return; | |
717 | ||
718 | elsif A_Id = Aspect_Default_Value | |
719 | and then not Is_Scalar_Type (Ent) | |
720 | then | |
721 | Error_Msg_N ("aspect% can only be applied to scalar type", Id); | |
722 | return; | |
723 | ||
724 | elsif A_Id = Aspect_Default_Component_Value then | |
725 | if not Is_Array_Type (Ent) then | |
726 | Error_Msg_N ("aspect% can only be applied to array type", Id); | |
727 | return; | |
728 | ||
729 | elsif not Is_Scalar_Type (Component_Type (Ent)) then | |
730 | Error_Msg_N ("aspect% requires scalar components", Id); | |
731 | return; | |
732 | end if; | |
733 | end if; | |
734 | ||
735 | Set_Has_Default_Aspect (Base_Type (Ent)); | |
736 | ||
737 | if Is_Scalar_Type (Ent) then | |
738 | Set_Default_Aspect_Value (Ent, Expr); | |
9f36e3fb | 739 | |
740 | -- Place default value of base type as well, because that is | |
741 | -- the semantics of the aspect. It is convenient to link the | |
742 | -- aspect to both the (possibly anonymous) base type and to | |
743 | -- the given first subtype. | |
744 | ||
745 | Set_Default_Aspect_Value (Base_Type (Ent), Expr); | |
746 | ||
06ef5f86 | 747 | else |
748 | Set_Default_Aspect_Component_Value (Ent, Expr); | |
749 | end if; | |
750 | end Analyze_Aspect_Default_Value; | |
751 | ||
752 | ------------------------------------- | |
753 | -- Make_Pragma_From_Boolean_Aspect -- | |
754 | ------------------------------------- | |
755 | ||
756 | procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is | |
757 | Ident : constant Node_Id := Identifier (ASN); | |
758 | A_Name : constant Name_Id := Chars (Ident); | |
759 | A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name); | |
760 | Ent : constant Entity_Id := Entity (ASN); | |
761 | Expr : constant Node_Id := Expression (ASN); | |
762 | Loc : constant Source_Ptr := Sloc (ASN); | |
763 | ||
764 | Prag : Node_Id; | |
765 | ||
766 | procedure Check_False_Aspect_For_Derived_Type; | |
767 | -- This procedure checks for the case of a false aspect for a derived | |
768 | -- type, which improperly tries to cancel an aspect inherited from | |
769 | -- the parent. | |
770 | ||
771 | ----------------------------------------- | |
772 | -- Check_False_Aspect_For_Derived_Type -- | |
773 | ----------------------------------------- | |
774 | ||
775 | procedure Check_False_Aspect_For_Derived_Type is | |
776 | Par : Node_Id; | |
777 | ||
778 | begin | |
779 | -- We are only checking derived types | |
780 | ||
781 | if not Is_Derived_Type (E) then | |
782 | return; | |
783 | end if; | |
784 | ||
785 | Par := Nearest_Ancestor (E); | |
786 | ||
787 | case A_Id is | |
788 | when Aspect_Atomic | Aspect_Shared => | |
789 | if not Is_Atomic (Par) then | |
790 | return; | |
791 | end if; | |
792 | ||
793 | when Aspect_Atomic_Components => | |
794 | if not Has_Atomic_Components (Par) then | |
795 | return; | |
796 | end if; | |
797 | ||
798 | when Aspect_Discard_Names => | |
799 | if not Discard_Names (Par) then | |
800 | return; | |
801 | end if; | |
802 | ||
803 | when Aspect_Pack => | |
804 | if not Is_Packed (Par) then | |
805 | return; | |
806 | end if; | |
807 | ||
808 | when Aspect_Unchecked_Union => | |
809 | if not Is_Unchecked_Union (Par) then | |
810 | return; | |
811 | end if; | |
812 | ||
813 | when Aspect_Volatile => | |
814 | if not Is_Volatile (Par) then | |
815 | return; | |
816 | end if; | |
817 | ||
818 | when Aspect_Volatile_Components => | |
819 | if not Has_Volatile_Components (Par) then | |
820 | return; | |
821 | end if; | |
822 | ||
823 | when others => | |
824 | return; | |
825 | end case; | |
826 | ||
827 | -- Fall through means we are canceling an inherited aspect | |
828 | ||
829 | Error_Msg_Name_1 := A_Name; | |
830 | Error_Msg_NE ("derived type& inherits aspect%, cannot cancel", | |
831 | Expr, | |
832 | E); | |
833 | ||
834 | end Check_False_Aspect_For_Derived_Type; | |
835 | ||
836 | -- Start of processing for Make_Pragma_From_Boolean_Aspect | |
837 | ||
838 | begin | |
839 | if Is_False (Static_Boolean (Expr)) then | |
840 | Check_False_Aspect_For_Derived_Type; | |
841 | ||
842 | else | |
843 | Prag := | |
844 | Make_Pragma (Loc, | |
845 | Pragma_Argument_Associations => New_List ( | |
846 | New_Occurrence_Of (Ent, Sloc (Ident))), | |
847 | Pragma_Identifier => | |
848 | Make_Identifier (Sloc (Ident), Chars (Ident))); | |
849 | ||
850 | Set_From_Aspect_Specification (Prag, True); | |
851 | Set_Corresponding_Aspect (Prag, ASN); | |
852 | Set_Aspect_Rep_Item (ASN, Prag); | |
853 | Set_Is_Delayed_Aspect (Prag); | |
854 | Set_Parent (Prag, ASN); | |
855 | end if; | |
06ef5f86 | 856 | end Make_Pragma_From_Boolean_Aspect; |
857 | ||
858 | -- Start of processing for Analyze_Aspects_At_Freeze_Point | |
859 | ||
860 | begin | |
29a9d4be | 861 | -- Must be visible in current scope |
06ef5f86 | 862 | |
ace3389d | 863 | if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then |
06ef5f86 | 864 | return; |
865 | end if; | |
866 | ||
867 | -- Look for aspect specification entries for this entity | |
868 | ||
869 | ASN := First_Rep_Item (E); | |
06ef5f86 | 870 | while Present (ASN) loop |
871 | if Nkind (ASN) = N_Aspect_Specification | |
872 | and then Entity (ASN) = E | |
873 | and then Is_Delayed_Aspect (ASN) | |
874 | then | |
875 | A_Id := Get_Aspect_Id (Chars (Identifier (ASN))); | |
876 | ||
877 | case A_Id is | |
e4c87fa5 | 878 | |
06ef5f86 | 879 | -- For aspects whose expression is an optional Boolean, make |
880 | -- the corresponding pragma at the freezing point. | |
881 | ||
882 | when Boolean_Aspects | | |
883 | Library_Unit_Aspects => | |
884 | Make_Pragma_From_Boolean_Aspect (ASN); | |
885 | ||
886 | -- Special handling for aspects that don't correspond to | |
887 | -- pragmas/attributes. | |
888 | ||
889 | when Aspect_Default_Value | | |
890 | Aspect_Default_Component_Value => | |
891 | Analyze_Aspect_Default_Value (ASN); | |
892 | ||
af9fed8f | 893 | -- Ditto for iterator aspects, because the corresponding |
894 | -- attributes may not have been analyzed yet. | |
895 | ||
896 | when Aspect_Constant_Indexing | | |
897 | Aspect_Variable_Indexing | | |
898 | Aspect_Default_Iterator | | |
899 | Aspect_Iterator_Element => | |
900 | Analyze (Expression (ASN)); | |
901 | ||
e4c87fa5 | 902 | when others => |
903 | null; | |
06ef5f86 | 904 | end case; |
905 | ||
906 | Ritem := Aspect_Rep_Item (ASN); | |
907 | ||
908 | if Present (Ritem) then | |
909 | Analyze (Ritem); | |
910 | end if; | |
911 | end if; | |
912 | ||
913 | Next_Rep_Item (ASN); | |
914 | end loop; | |
915 | end Analyze_Aspects_At_Freeze_Point; | |
916 | ||
ae888dbd | 917 | ----------------------------------- |
918 | -- Analyze_Aspect_Specifications -- | |
919 | ----------------------------------- | |
920 | ||
21ea3a4f | 921 | procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is |
ae888dbd | 922 | Aspect : Node_Id; |
d74fc39a | 923 | Aitem : Node_Id; |
ae888dbd | 924 | Ent : Node_Id; |
ae888dbd | 925 | |
21ea3a4f | 926 | L : constant List_Id := Aspect_Specifications (N); |
927 | ||
ae888dbd | 928 | Ins_Node : Node_Id := N; |
89f1e35c | 929 | -- Insert pragmas/attribute definition clause after this node when no |
930 | -- delayed analysis is required. | |
d74fc39a | 931 | |
932 | -- The general processing involves building an attribute definition | |
89f1e35c | 933 | -- clause or a pragma node that corresponds to the aspect. Then in order |
934 | -- to delay the evaluation of this aspect to the freeze point, we attach | |
935 | -- the corresponding pragma/attribute definition clause to the aspect | |
936 | -- specification node, which is then placed in the Rep Item chain. In | |
937 | -- this case we mark the entity by setting the flag Has_Delayed_Aspects | |
938 | -- and we evaluate the rep item at the freeze point. When the aspect | |
939 | -- doesn't have a corresponding pragma/attribute definition clause, then | |
940 | -- its analysis is simply delayed at the freeze point. | |
941 | ||
942 | -- Some special cases don't require delay analysis, thus the aspect is | |
943 | -- analyzed right now. | |
944 | ||
945 | -- Note that there is a special handling for | |
946 | -- Pre/Post/Test_Case/Contract_Case aspects. In this case, we do not | |
947 | -- have to worry about delay issues, since the pragmas themselves deal | |
948 | -- with delay of visibility for the expression analysis. Thus, we just | |
949 | -- insert the pragma after the node N. | |
ae888dbd | 950 | |
951 | begin | |
21ea3a4f | 952 | pragma Assert (Present (L)); |
953 | ||
6fb3c314 | 954 | -- Loop through aspects |
f93e7257 | 955 | |
ae888dbd | 956 | Aspect := First (L); |
21ea3a4f | 957 | Aspect_Loop : while Present (Aspect) loop |
ae888dbd | 958 | declare |
94153a42 | 959 | Expr : constant Node_Id := Expression (Aspect); |
89f1e35c | 960 | Id : constant Node_Id := Identifier (Aspect); |
961 | Loc : constant Source_Ptr := Sloc (Aspect); | |
94153a42 | 962 | Nam : constant Name_Id := Chars (Id); |
963 | A_Id : constant Aspect_Id := Get_Aspect_Id (Nam); | |
ae888dbd | 964 | Anod : Node_Id; |
965 | ||
89f1e35c | 966 | Delay_Required : Boolean := True; |
967 | -- Set False if delay is not required | |
968 | ||
c0793fff | 969 | Eloc : Source_Ptr := No_Location; |
970 | -- Source location of expression, modified when we split PPC's. It | |
971 | -- is set below when Expr is present. | |
39e1f22f | 972 | |
89f1e35c | 973 | procedure Analyze_Aspect_External_Or_Link_Name; |
974 | -- This routine performs the analysis of the External_Name or | |
975 | -- Link_Name aspects. | |
21ea3a4f | 976 | |
89f1e35c | 977 | procedure Analyze_Aspect_Implicit_Dereference; |
978 | -- This routine performs the analysis of the Implicit_Dereference | |
979 | -- aspects. | |
21ea3a4f | 980 | |
89f1e35c | 981 | ------------------------------------------ |
982 | -- Analyze_Aspect_External_Or_Link_Name -- | |
983 | ------------------------------------------ | |
984 | ||
985 | procedure Analyze_Aspect_External_Or_Link_Name is | |
21ea3a4f | 986 | begin |
89f1e35c | 987 | -- Verify that there is an Import/Export aspect defined for the |
988 | -- entity. The processing of that aspect in turn checks that | |
989 | -- there is a Convention aspect declared. The pragma is | |
990 | -- constructed when processing the Convention aspect. | |
21ea3a4f | 991 | |
89f1e35c | 992 | declare |
993 | A : Node_Id; | |
21ea3a4f | 994 | |
89f1e35c | 995 | begin |
996 | A := First (L); | |
89f1e35c | 997 | while Present (A) loop |
998 | exit when Chars (Identifier (A)) = Name_Export | |
999 | or else Chars (Identifier (A)) = Name_Import; | |
1000 | Next (A); | |
1001 | end loop; | |
21ea3a4f | 1002 | |
89f1e35c | 1003 | if No (A) then |
1004 | Error_Msg_N | |
1005 | ("Missing Import/Export for Link/External name", | |
1006 | Aspect); | |
1007 | end if; | |
1008 | end; | |
1009 | end Analyze_Aspect_External_Or_Link_Name; | |
21ea3a4f | 1010 | |
89f1e35c | 1011 | ----------------------------------------- |
1012 | -- Analyze_Aspect_Implicit_Dereference -- | |
1013 | ----------------------------------------- | |
21ea3a4f | 1014 | |
89f1e35c | 1015 | procedure Analyze_Aspect_Implicit_Dereference is |
1016 | begin | |
b9e61b2a | 1017 | if not Is_Type (E) or else not Has_Discriminants (E) then |
89f1e35c | 1018 | Error_Msg_N |
1019 | ("Aspect must apply to a type with discriminants", N); | |
21ea3a4f | 1020 | |
89f1e35c | 1021 | else |
1022 | declare | |
1023 | Disc : Entity_Id; | |
21ea3a4f | 1024 | |
89f1e35c | 1025 | begin |
1026 | Disc := First_Discriminant (E); | |
89f1e35c | 1027 | while Present (Disc) loop |
1028 | if Chars (Expr) = Chars (Disc) | |
1029 | and then Ekind (Etype (Disc)) = | |
1030 | E_Anonymous_Access_Type | |
1031 | then | |
1032 | Set_Has_Implicit_Dereference (E); | |
1033 | Set_Has_Implicit_Dereference (Disc); | |
1034 | return; | |
1035 | end if; | |
21ea3a4f | 1036 | |
89f1e35c | 1037 | Next_Discriminant (Disc); |
1038 | end loop; | |
21ea3a4f | 1039 | |
89f1e35c | 1040 | -- Error if no proper access discriminant. |
21ea3a4f | 1041 | |
89f1e35c | 1042 | Error_Msg_NE |
1043 | ("not an access discriminant of&", Expr, E); | |
1044 | end; | |
1045 | end if; | |
1046 | end Analyze_Aspect_Implicit_Dereference; | |
21ea3a4f | 1047 | |
ae888dbd | 1048 | begin |
fb7f2fc4 | 1049 | -- Skip aspect if already analyzed (not clear if this is needed) |
1050 | ||
1051 | if Analyzed (Aspect) then | |
1052 | goto Continue; | |
1053 | end if; | |
1054 | ||
c0793fff | 1055 | -- Set the source location of expression, used in the case of |
1056 | -- a failed precondition/postcondition or invariant. Note that | |
1057 | -- the source location of the expression is not usually the best | |
1058 | -- choice here. For example, it gets located on the last AND | |
1059 | -- keyword in a chain of boolean expressiond AND'ed together. | |
1060 | -- It is best to put the message on the first character of the | |
1061 | -- assertion, which is the effect of the First_Node call here. | |
1062 | ||
1063 | if Present (Expr) then | |
1064 | Eloc := Sloc (First_Node (Expr)); | |
1065 | end if; | |
1066 | ||
d7ed83a2 | 1067 | -- Check restriction No_Implementation_Aspect_Specifications |
1068 | ||
1069 | if Impl_Defined_Aspects (A_Id) then | |
1070 | Check_Restriction | |
1071 | (No_Implementation_Aspect_Specifications, Aspect); | |
1072 | end if; | |
1073 | ||
1074 | -- Check restriction No_Specification_Of_Aspect | |
1075 | ||
1076 | Check_Restriction_No_Specification_Of_Aspect (Aspect); | |
1077 | ||
1078 | -- Analyze this aspect | |
1079 | ||
fb7f2fc4 | 1080 | Set_Analyzed (Aspect); |
d74fc39a | 1081 | Set_Entity (Aspect, E); |
1082 | Ent := New_Occurrence_Of (E, Sloc (Id)); | |
1083 | ||
1e3c4ae6 | 1084 | -- Check for duplicate aspect. Note that the Comes_From_Source |
1085 | -- test allows duplicate Pre/Post's that we generate internally | |
1086 | -- to escape being flagged here. | |
ae888dbd | 1087 | |
6c545057 | 1088 | if No_Duplicates_Allowed (A_Id) then |
1089 | Anod := First (L); | |
1090 | while Anod /= Aspect loop | |
1091 | if Same_Aspect | |
1092 | (A_Id, Get_Aspect_Id (Chars (Identifier (Anod)))) | |
1093 | and then Comes_From_Source (Aspect) | |
1094 | then | |
1095 | Error_Msg_Name_1 := Nam; | |
1096 | Error_Msg_Sloc := Sloc (Anod); | |
39e1f22f | 1097 | |
6c545057 | 1098 | -- Case of same aspect specified twice |
39e1f22f | 1099 | |
6c545057 | 1100 | if Class_Present (Anod) = Class_Present (Aspect) then |
1101 | if not Class_Present (Anod) then | |
1102 | Error_Msg_NE | |
1103 | ("aspect% for & previously given#", | |
1104 | Id, E); | |
1105 | else | |
1106 | Error_Msg_NE | |
1107 | ("aspect `%''Class` for & previously given#", | |
1108 | Id, E); | |
1109 | end if; | |
39e1f22f | 1110 | end if; |
6c545057 | 1111 | end if; |
ae888dbd | 1112 | |
6c545057 | 1113 | Next (Anod); |
1114 | end loop; | |
1115 | end if; | |
ae888dbd | 1116 | |
4db325e6 | 1117 | -- Check some general restrictions on language defined aspects |
1118 | ||
1119 | if not Impl_Defined_Aspects (A_Id) then | |
1120 | Error_Msg_Name_1 := Nam; | |
1121 | ||
1122 | -- Not allowed for renaming declarations | |
1123 | ||
1124 | if Nkind (N) in N_Renaming_Declaration then | |
1125 | Error_Msg_N | |
1126 | ("aspect % not allowed for renaming declaration", | |
1127 | Aspect); | |
1128 | end if; | |
1129 | ||
1130 | -- Not allowed for formal type declarations | |
1131 | ||
1132 | if Nkind (N) = N_Formal_Type_Declaration then | |
1133 | Error_Msg_N | |
1134 | ("aspect % not allowed for formal type declaration", | |
1135 | Aspect); | |
1136 | end if; | |
1137 | end if; | |
1138 | ||
7d20685d | 1139 | -- Copy expression for later processing by the procedures |
1140 | -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations] | |
1141 | ||
1142 | Set_Entity (Id, New_Copy_Tree (Expr)); | |
1143 | ||
ae888dbd | 1144 | -- Processing based on specific aspect |
1145 | ||
d74fc39a | 1146 | case A_Id is |
ae888dbd | 1147 | |
1148 | -- No_Aspect should be impossible | |
1149 | ||
1150 | when No_Aspect => | |
1151 | raise Program_Error; | |
1152 | ||
89f1e35c | 1153 | -- Case 1: Aspects corresponding to attribute definition |
1154 | -- clauses. | |
ae888dbd | 1155 | |
b7b74740 | 1156 | when Aspect_Address | |
1157 | Aspect_Alignment | | |
1158 | Aspect_Bit_Order | | |
1159 | Aspect_Component_Size | | |
89f1e35c | 1160 | Aspect_Constant_Indexing | |
89f1e35c | 1161 | Aspect_Default_Iterator | |
1162 | Aspect_Dispatching_Domain | | |
b7b74740 | 1163 | Aspect_External_Tag | |
1164 | Aspect_Input | | |
89f1e35c | 1165 | Aspect_Iterator_Element | |
b7b74740 | 1166 | Aspect_Machine_Radix | |
1167 | Aspect_Object_Size | | |
1168 | Aspect_Output | | |
1169 | Aspect_Read | | |
1170 | Aspect_Scalar_Storage_Order | | |
1171 | Aspect_Size | | |
1172 | Aspect_Small | | |
1173 | Aspect_Simple_Storage_Pool | | |
1174 | Aspect_Storage_Pool | | |
1175 | Aspect_Storage_Size | | |
1176 | Aspect_Stream_Size | | |
1177 | Aspect_Value_Size | | |
89f1e35c | 1178 | Aspect_Variable_Indexing | |
b7b74740 | 1179 | Aspect_Write => |
d74fc39a | 1180 | |
89f1e35c | 1181 | -- Indexing aspects apply only to tagged type |
1182 | ||
1183 | if (A_Id = Aspect_Constant_Indexing | |
1184 | or else A_Id = Aspect_Variable_Indexing) | |
1185 | and then not (Is_Type (E) | |
1186 | and then Is_Tagged_Type (E)) | |
1187 | then | |
1188 | Error_Msg_N ("indexing applies to a tagged type", N); | |
1189 | goto Continue; | |
1190 | end if; | |
1191 | ||
d74fc39a | 1192 | -- Construct the attribute definition clause |
1193 | ||
1194 | Aitem := | |
94153a42 | 1195 | Make_Attribute_Definition_Clause (Loc, |
d74fc39a | 1196 | Name => Ent, |
ae888dbd | 1197 | Chars => Chars (Id), |
1198 | Expression => Relocate_Node (Expr)); | |
1199 | ||
89f1e35c | 1200 | -- Case 2: Aspects cooresponding to pragmas |
d74fc39a | 1201 | |
89f1e35c | 1202 | -- Case 2a: Aspects corresponding to pragmas with two |
1203 | -- arguments, where the first argument is a local name | |
1204 | -- referring to the entity, and the second argument is the | |
1205 | -- aspect definition expression. | |
ae888dbd | 1206 | |
1207 | when Aspect_Suppress | | |
1208 | Aspect_Unsuppress => | |
1209 | ||
d74fc39a | 1210 | -- Construct the pragma |
1211 | ||
1212 | Aitem := | |
94153a42 | 1213 | Make_Pragma (Loc, |
ae888dbd | 1214 | Pragma_Argument_Associations => New_List ( |
231eb581 | 1215 | New_Occurrence_Of (E, Loc), |
ae888dbd | 1216 | Relocate_Node (Expr)), |
1217 | Pragma_Identifier => | |
e7823792 | 1218 | Make_Identifier (Sloc (Id), Chars (Id))); |
d74fc39a | 1219 | |
49213728 | 1220 | when Aspect_Synchronization => |
1221 | ||
1222 | -- The aspect corresponds to pragma Implemented. | |
1223 | -- Construct the pragma | |
1224 | ||
1225 | Aitem := | |
1226 | Make_Pragma (Loc, | |
1227 | Pragma_Argument_Associations => New_List ( | |
1228 | New_Occurrence_Of (E, Loc), | |
1229 | Relocate_Node (Expr)), | |
1230 | Pragma_Identifier => | |
1231 | Make_Identifier (Sloc (Id), Name_Implemented)); | |
1232 | ||
89f1e35c | 1233 | -- No delay is required since the only values are: By_Entry |
1234 | -- | By_Protected_Procedure | By_Any | Optional which don't | |
1235 | -- get analyzed anyway. | |
49213728 | 1236 | |
89f1e35c | 1237 | Delay_Required := False; |
1238 | ||
1239 | when Aspect_Attach_Handler => | |
1240 | Aitem := | |
1241 | Make_Pragma (Loc, | |
1242 | Pragma_Identifier => | |
1243 | Make_Identifier (Sloc (Id), Name_Attach_Handler), | |
1244 | Pragma_Argument_Associations => | |
1245 | New_List (Ent, Relocate_Node (Expr))); | |
1246 | ||
1247 | when Aspect_Dynamic_Predicate | | |
1248 | Aspect_Predicate | | |
1249 | Aspect_Static_Predicate => | |
1250 | ||
1251 | -- Construct the pragma (always a pragma Predicate, with | |
1252 | -- flags recording whether it is static/dynamic). | |
1253 | ||
1254 | Aitem := | |
1255 | Make_Pragma (Loc, | |
1256 | Pragma_Argument_Associations => | |
1257 | New_List (Ent, Relocate_Node (Expr)), | |
1258 | Class_Present => Class_Present (Aspect), | |
1259 | Pragma_Identifier => | |
1260 | Make_Identifier (Sloc (Id), Name_Predicate)); | |
1261 | ||
1262 | -- If the type is private, indicate that its completion | |
1263 | -- has a freeze node, because that is the one that will be | |
1264 | -- visible at freeze time. | |
1265 | ||
1266 | Set_Has_Predicates (E); | |
1267 | ||
1268 | if Is_Private_Type (E) | |
1269 | and then Present (Full_View (E)) | |
1270 | then | |
1271 | Set_Has_Predicates (Full_View (E)); | |
1272 | Set_Has_Delayed_Aspects (Full_View (E)); | |
1273 | Ensure_Freeze_Node (Full_View (E)); | |
1274 | end if; | |
1275 | ||
1276 | -- Case 2b: Aspects corresponding to pragmas with two | |
1277 | -- arguments, where the second argument is a local name | |
1278 | -- referring to the entity, and the first argument is the | |
1279 | -- aspect definition expression. | |
ae888dbd | 1280 | |
a5a64273 | 1281 | when Aspect_Convention => |
1282 | ||
1283 | -- The aspect may be part of the specification of an import | |
1284 | -- or export pragma. Scan the aspect list to gather the | |
1285 | -- other components, if any. The name of the generated | |
1286 | -- pragma is one of Convention/Import/Export. | |
1287 | ||
1288 | declare | |
1289 | P_Name : Name_Id; | |
1290 | A_Name : Name_Id; | |
1291 | A : Node_Id; | |
1292 | Arg_List : List_Id; | |
1293 | Found : Boolean; | |
1294 | L_Assoc : Node_Id; | |
1295 | E_Assoc : Node_Id; | |
1296 | ||
1297 | begin | |
1298 | P_Name := Chars (Id); | |
1299 | Found := False; | |
1300 | Arg_List := New_List; | |
1301 | L_Assoc := Empty; | |
1302 | E_Assoc := Empty; | |
1303 | ||
1304 | A := First (L); | |
1305 | while Present (A) loop | |
1306 | A_Name := Chars (Identifier (A)); | |
1307 | ||
1308 | if A_Name = Name_Import | |
b9e61b2a | 1309 | or else |
1310 | A_Name = Name_Export | |
a5a64273 | 1311 | then |
1312 | if Found then | |
89f1e35c | 1313 | Error_Msg_N ("conflicting", A); |
a5a64273 | 1314 | else |
1315 | Found := True; | |
1316 | end if; | |
1317 | ||
1318 | P_Name := A_Name; | |
1319 | ||
1320 | elsif A_Name = Name_Link_Name then | |
1321 | L_Assoc := Make_Pragma_Argument_Association (Loc, | |
89f1e35c | 1322 | Chars => A_Name, |
a5a64273 | 1323 | Expression => Relocate_Node (Expression (A))); |
1324 | ||
1325 | elsif A_Name = Name_External_Name then | |
1326 | E_Assoc := Make_Pragma_Argument_Association (Loc, | |
89f1e35c | 1327 | Chars => A_Name, |
a5a64273 | 1328 | Expression => Relocate_Node (Expression (A))); |
1329 | end if; | |
1330 | ||
1331 | Next (A); | |
1332 | end loop; | |
1333 | ||
1334 | Arg_List := New_List (Relocate_Node (Expr), Ent); | |
b9e61b2a | 1335 | |
a5a64273 | 1336 | if Present (L_Assoc) then |
1337 | Append_To (Arg_List, L_Assoc); | |
1338 | end if; | |
1339 | ||
1340 | if Present (E_Assoc) then | |
1341 | Append_To (Arg_List, E_Assoc); | |
1342 | end if; | |
1343 | ||
1344 | Aitem := | |
1345 | Make_Pragma (Loc, | |
1346 | Pragma_Argument_Associations => Arg_List, | |
1347 | Pragma_Identifier => | |
1348 | Make_Identifier (Loc, P_Name)); | |
1349 | end; | |
e1cedbae | 1350 | |
3a72f9c3 | 1351 | -- The following three aspects can be specified for a |
1352 | -- subprogram body, in which case we generate pragmas for them | |
1353 | -- and insert them ahead of local declarations, rather than | |
1354 | -- after the body. | |
1355 | ||
1356 | when Aspect_CPU | | |
1357 | Aspect_Interrupt_Priority | | |
1358 | Aspect_Priority => | |
1359 | if Nkind (N) = N_Subprogram_Body then | |
1360 | Aitem := | |
1361 | Make_Pragma (Loc, | |
1362 | Pragma_Argument_Associations => | |
1363 | New_List (Relocate_Node (Expr)), | |
1364 | Pragma_Identifier => | |
1365 | Make_Identifier (Sloc (Id), Chars (Id))); | |
1366 | else | |
1367 | Aitem := | |
1368 | Make_Attribute_Definition_Clause (Loc, | |
1369 | Name => Ent, | |
1370 | Chars => Chars (Id), | |
1371 | Expression => Relocate_Node (Expr)); | |
1372 | end if; | |
1373 | ||
ae888dbd | 1374 | when Aspect_Warnings => |
1375 | ||
d74fc39a | 1376 | -- Construct the pragma |
1377 | ||
1378 | Aitem := | |
94153a42 | 1379 | Make_Pragma (Loc, |
ae888dbd | 1380 | Pragma_Argument_Associations => New_List ( |
1381 | Relocate_Node (Expr), | |
231eb581 | 1382 | New_Occurrence_Of (E, Loc)), |
ae888dbd | 1383 | Pragma_Identifier => |
94153a42 | 1384 | Make_Identifier (Sloc (Id), Chars (Id)), |
1385 | Class_Present => Class_Present (Aspect)); | |
ae888dbd | 1386 | |
d74fc39a | 1387 | -- We don't have to play the delay game here, since the only |
0b424e9b | 1388 | -- values are ON/OFF which don't get analyzed anyway. |
d74fc39a | 1389 | |
89f1e35c | 1390 | Delay_Required := False; |
d74fc39a | 1391 | |
89f1e35c | 1392 | -- Case 2c: Aspects corresponding to pragmas with three |
1393 | -- arguments. | |
d64221a7 | 1394 | |
89f1e35c | 1395 | -- Invariant aspects have a first argument that references the |
1396 | -- entity, a second argument that is the expression and a third | |
1397 | -- argument that is an appropriate message. | |
d64221a7 | 1398 | |
89f1e35c | 1399 | when Aspect_Invariant | |
1400 | Aspect_Type_Invariant => | |
d64221a7 | 1401 | |
89f1e35c | 1402 | -- Analysis of the pragma will verify placement legality: |
1403 | -- an invariant must apply to a private type, or appear in | |
1404 | -- the private part of a spec and apply to a completion. | |
d64221a7 | 1405 | |
89f1e35c | 1406 | -- Construct the pragma |
d64221a7 | 1407 | |
89f1e35c | 1408 | Aitem := |
1409 | Make_Pragma (Loc, | |
1410 | Pragma_Argument_Associations => | |
1411 | New_List (Ent, Relocate_Node (Expr)), | |
1412 | Class_Present => Class_Present (Aspect), | |
1413 | Pragma_Identifier => | |
1414 | Make_Identifier (Sloc (Id), Name_Invariant)); | |
1415 | ||
1416 | -- Add message unless exception messages are suppressed | |
1417 | ||
1418 | if not Opt.Exception_Locations_Suppressed then | |
1419 | Append_To (Pragma_Argument_Associations (Aitem), | |
1420 | Make_Pragma_Argument_Association (Eloc, | |
1421 | Chars => Name_Message, | |
1422 | Expression => | |
1423 | Make_String_Literal (Eloc, | |
1424 | Strval => "failed invariant from " | |
1425 | & Build_Location_String (Eloc)))); | |
d64221a7 | 1426 | end if; |
1427 | ||
89f1e35c | 1428 | -- For Invariant case, insert immediately after the entity |
1429 | -- declaration. We do not have to worry about delay issues | |
1430 | -- since the pragma processing takes care of this. | |
1431 | ||
89f1e35c | 1432 | Delay_Required := False; |
d64221a7 | 1433 | |
47a46747 | 1434 | -- Case 2d : Aspects that correspond to a pragma with one |
1435 | -- argument. | |
1436 | ||
1437 | when Aspect_Relative_Deadline => | |
1438 | Aitem := | |
1439 | Make_Pragma (Loc, | |
1440 | Pragma_Argument_Associations => | |
1441 | New_List ( | |
1442 | Make_Pragma_Argument_Association (Loc, | |
1443 | Expression => Relocate_Node (Expr))), | |
1444 | Pragma_Identifier => | |
1445 | Make_Identifier (Sloc (Id), Name_Relative_Deadline)); | |
1446 | ||
1447 | -- If the aspect applies to a task, the corresponding pragma | |
1448 | -- must appear within its declarations, not after. | |
1449 | ||
1450 | if Nkind (N) = N_Task_Type_Declaration then | |
1451 | declare | |
1452 | Def : Node_Id; | |
1453 | V : List_Id; | |
1454 | ||
1455 | begin | |
1456 | if No (Task_Definition (N)) then | |
1457 | Set_Task_Definition (N, | |
1458 | Make_Task_Definition (Loc, | |
1459 | Visible_Declarations => New_List, | |
1460 | End_Label => Empty)); | |
1461 | end if; | |
1462 | ||
1463 | Def := Task_Definition (N); | |
1464 | V := Visible_Declarations (Def); | |
1465 | if not Is_Empty_List (V) then | |
1466 | Insert_Before (First (V), Aitem); | |
1467 | ||
1468 | else | |
1469 | Set_Visible_Declarations (Def, New_List (Aitem)); | |
1470 | end if; | |
1471 | ||
1472 | goto Continue; | |
1473 | end; | |
1474 | end if; | |
1475 | ||
89f1e35c | 1476 | -- Case 3 : Aspects that don't correspond to pragma/attribute |
1477 | -- definition clause. | |
7b9b2f05 | 1478 | |
89f1e35c | 1479 | -- Case 3a: The aspects listed below don't correspond to |
1480 | -- pragmas/attributes but do require delayed analysis. | |
7f694ca2 | 1481 | |
89f1e35c | 1482 | when Aspect_Default_Value | |
1483 | Aspect_Default_Component_Value => | |
1484 | Aitem := Empty; | |
7f694ca2 | 1485 | |
89f1e35c | 1486 | -- Case 3b: The aspects listed below don't correspond to |
1487 | -- pragmas/attributes and don't need delayed analysis. | |
95bc75fa | 1488 | |
89f1e35c | 1489 | -- For Implicit_Dereference, External_Name and Link_Name, only |
1490 | -- the legality checks are done during the analysis, thus no | |
1491 | -- delay is required. | |
a8e38e1d | 1492 | |
89f1e35c | 1493 | when Aspect_Implicit_Dereference => |
1494 | Analyze_Aspect_Implicit_Dereference; | |
1495 | goto Continue; | |
7f694ca2 | 1496 | |
89f1e35c | 1497 | when Aspect_External_Name | |
1498 | Aspect_Link_Name => | |
1499 | Analyze_Aspect_External_Or_Link_Name; | |
1500 | goto Continue; | |
7f694ca2 | 1501 | |
89f1e35c | 1502 | when Aspect_Dimension => |
1503 | Analyze_Aspect_Dimension (N, Id, Expr); | |
1504 | goto Continue; | |
cb4c311d | 1505 | |
89f1e35c | 1506 | when Aspect_Dimension_System => |
1507 | Analyze_Aspect_Dimension_System (N, Id, Expr); | |
1508 | goto Continue; | |
7f694ca2 | 1509 | |
89f1e35c | 1510 | -- Case 4: Special handling for aspects |
1511 | -- Pre/Post/Test_Case/Contract_Case whose corresponding pragmas | |
1512 | -- take care of the delay. | |
7f694ca2 | 1513 | |
1e3c4ae6 | 1514 | -- Aspects Pre/Post generate Precondition/Postcondition pragmas |
1515 | -- with a first argument that is the expression, and a second | |
1516 | -- argument that is an informative message if the test fails. | |
1517 | -- This is inserted right after the declaration, to get the | |
5b5df4a9 | 1518 | -- required pragma placement. The processing for the pragmas |
1519 | -- takes care of the required delay. | |
ae888dbd | 1520 | |
ddf1337b | 1521 | when Pre_Post_Aspects => declare |
1e3c4ae6 | 1522 | Pname : Name_Id; |
ae888dbd | 1523 | |
1e3c4ae6 | 1524 | begin |
77ae6789 | 1525 | if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then |
1e3c4ae6 | 1526 | Pname := Name_Precondition; |
1527 | else | |
1528 | Pname := Name_Postcondition; | |
1529 | end if; | |
d74fc39a | 1530 | |
1e3c4ae6 | 1531 | -- If the expressions is of the form A and then B, then |
1532 | -- we generate separate Pre/Post aspects for the separate | |
1533 | -- clauses. Since we allow multiple pragmas, there is no | |
1534 | -- problem in allowing multiple Pre/Post aspects internally. | |
a273015d | 1535 | -- These should be treated in reverse order (B first and |
1536 | -- A second) since they are later inserted just after N in | |
1537 | -- the order they are treated. This way, the pragma for A | |
1538 | -- ends up preceding the pragma for B, which may have an | |
1539 | -- importance for the error raised (either constraint error | |
1540 | -- or precondition error). | |
1e3c4ae6 | 1541 | |
39e1f22f | 1542 | -- We do not do this for Pre'Class, since we have to put |
1543 | -- these conditions together in a complex OR expression | |
ae888dbd | 1544 | |
4282d342 | 1545 | -- We do not do this in ASIS mode, as ASIS relies on the |
1546 | -- original node representing the complete expression, when | |
1547 | -- retrieving it through the source aspect table. | |
1548 | ||
1549 | if not ASIS_Mode | |
1550 | and then (Pname = Name_Postcondition | |
1551 | or else not Class_Present (Aspect)) | |
39e1f22f | 1552 | then |
1553 | while Nkind (Expr) = N_And_Then loop | |
1554 | Insert_After (Aspect, | |
a273015d | 1555 | Make_Aspect_Specification (Sloc (Left_Opnd (Expr)), |
39e1f22f | 1556 | Identifier => Identifier (Aspect), |
a273015d | 1557 | Expression => Relocate_Node (Left_Opnd (Expr)), |
39e1f22f | 1558 | Class_Present => Class_Present (Aspect), |
1559 | Split_PPC => True)); | |
a273015d | 1560 | Rewrite (Expr, Relocate_Node (Right_Opnd (Expr))); |
39e1f22f | 1561 | Eloc := Sloc (Expr); |
1562 | end loop; | |
1563 | end if; | |
ae888dbd | 1564 | |
39e1f22f | 1565 | -- Build the precondition/postcondition pragma |
d74fc39a | 1566 | |
1567 | Aitem := | |
1e3c4ae6 | 1568 | Make_Pragma (Loc, |
ae888dbd | 1569 | Pragma_Identifier => |
55868293 | 1570 | Make_Identifier (Sloc (Id), Pname), |
94153a42 | 1571 | Class_Present => Class_Present (Aspect), |
39e1f22f | 1572 | Split_PPC => Split_PPC (Aspect), |
94153a42 | 1573 | Pragma_Argument_Associations => New_List ( |
1e3c4ae6 | 1574 | Make_Pragma_Argument_Association (Eloc, |
94153a42 | 1575 | Chars => Name_Check, |
39e1f22f | 1576 | Expression => Relocate_Node (Expr)))); |
1577 | ||
1578 | -- Add message unless exception messages are suppressed | |
1579 | ||
1580 | if not Opt.Exception_Locations_Suppressed then | |
1581 | Append_To (Pragma_Argument_Associations (Aitem), | |
1582 | Make_Pragma_Argument_Association (Eloc, | |
1583 | Chars => Name_Message, | |
1584 | Expression => | |
1585 | Make_String_Literal (Eloc, | |
1586 | Strval => "failed " | |
1587 | & Get_Name_String (Pname) | |
1588 | & " from " | |
1589 | & Build_Location_String (Eloc)))); | |
1590 | end if; | |
d74fc39a | 1591 | |
1e3c4ae6 | 1592 | Set_From_Aspect_Specification (Aitem, True); |
cce84b09 | 1593 | Set_Corresponding_Aspect (Aitem, Aspect); |
7d20685d | 1594 | Set_Is_Delayed_Aspect (Aspect); |
d74fc39a | 1595 | |
1e3c4ae6 | 1596 | -- For Pre/Post cases, insert immediately after the entity |
1597 | -- declaration, since that is the required pragma placement. | |
1598 | -- Note that for these aspects, we do not have to worry | |
1599 | -- about delay issues, since the pragmas themselves deal | |
1600 | -- with delay of visibility for the expression analysis. | |
1601 | ||
d2be415f | 1602 | -- If the entity is a library-level subprogram, the pre/ |
d4596fbe | 1603 | -- postconditions must be treated as late pragmas. Note |
1604 | -- that they must be prepended, not appended, to the list, | |
1605 | -- so that split AND THEN sections are processed in the | |
1606 | -- correct order. | |
d2be415f | 1607 | |
1608 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
d4596fbe | 1609 | declare |
1610 | Aux : constant Node_Id := Aux_Decls_Node (Parent (N)); | |
b174444e | 1611 | |
d4596fbe | 1612 | begin |
1613 | if No (Pragmas_After (Aux)) then | |
1614 | Set_Pragmas_After (Aux, New_List); | |
1615 | end if; | |
1616 | ||
1617 | Prepend (Aitem, Pragmas_After (Aux)); | |
1618 | end; | |
01fa11f5 | 1619 | |
1620 | -- If it is a subprogram body, add pragmas to list of | |
1621 | -- declarations in body. | |
1622 | ||
1623 | elsif Nkind (N) = N_Subprogram_Body then | |
1624 | if No (Declarations (N)) then | |
1625 | Set_Declarations (N, New_List); | |
1626 | end if; | |
1627 | ||
1628 | Append (Aitem, Declarations (N)); | |
1629 | ||
d2be415f | 1630 | else |
1631 | Insert_After (N, Aitem); | |
1632 | end if; | |
1633 | ||
1e3c4ae6 | 1634 | goto Continue; |
1635 | end; | |
ae888dbd | 1636 | |
fad014fe | 1637 | when Aspect_Contract_Case | |
1638 | Aspect_Test_Case => | |
1639 | declare | |
1640 | Args : List_Id; | |
1641 | Comp_Expr : Node_Id; | |
1642 | Comp_Assn : Node_Id; | |
1643 | New_Expr : Node_Id; | |
b0bc40fd | 1644 | |
fad014fe | 1645 | begin |
1646 | Args := New_List; | |
6c545057 | 1647 | |
fad014fe | 1648 | if Nkind (Parent (N)) = N_Compilation_Unit then |
1649 | Error_Msg_Name_1 := Nam; | |
1650 | Error_Msg_N ("incorrect placement of aspect `%`", E); | |
1651 | goto Continue; | |
1652 | end if; | |
6c545057 | 1653 | |
fad014fe | 1654 | if Nkind (Expr) /= N_Aggregate then |
1655 | Error_Msg_Name_1 := Nam; | |
6c545057 | 1656 | Error_Msg_NE |
fad014fe | 1657 | ("wrong syntax for aspect `%` for &", Id, E); |
6c545057 | 1658 | goto Continue; |
1659 | end if; | |
1660 | ||
fad014fe | 1661 | -- Make pragma expressions refer to the original aspect |
1662 | -- expressions through the Original_Node link. This is | |
1663 | -- used in semantic analysis for ASIS mode, so that the | |
1664 | -- original expression also gets analyzed. | |
1665 | ||
1666 | Comp_Expr := First (Expressions (Expr)); | |
1667 | while Present (Comp_Expr) loop | |
1668 | New_Expr := Relocate_Node (Comp_Expr); | |
1669 | Set_Original_Node (New_Expr, Comp_Expr); | |
1670 | Append | |
1671 | (Make_Pragma_Argument_Association (Sloc (Comp_Expr), | |
1672 | Expression => New_Expr), | |
1673 | Args); | |
1674 | Next (Comp_Expr); | |
1675 | end loop; | |
6c545057 | 1676 | |
fad014fe | 1677 | Comp_Assn := First (Component_Associations (Expr)); |
1678 | while Present (Comp_Assn) loop | |
1679 | if List_Length (Choices (Comp_Assn)) /= 1 | |
1680 | or else | |
1681 | Nkind (First (Choices (Comp_Assn))) /= N_Identifier | |
1682 | then | |
1683 | Error_Msg_Name_1 := Nam; | |
1684 | Error_Msg_NE | |
1685 | ("wrong syntax for aspect `%` for &", Id, E); | |
1686 | goto Continue; | |
1687 | end if; | |
6c545057 | 1688 | |
fad014fe | 1689 | New_Expr := Relocate_Node (Expression (Comp_Assn)); |
1690 | Set_Original_Node (New_Expr, Expression (Comp_Assn)); | |
1691 | Append (Make_Pragma_Argument_Association ( | |
1692 | Sloc => Sloc (Comp_Assn), | |
1693 | Chars => Chars (First (Choices (Comp_Assn))), | |
1694 | Expression => New_Expr), | |
1695 | Args); | |
1696 | Next (Comp_Assn); | |
1697 | end loop; | |
6c545057 | 1698 | |
fad014fe | 1699 | -- Build the contract-case or test-case pragma |
6c545057 | 1700 | |
fad014fe | 1701 | Aitem := |
1702 | Make_Pragma (Loc, | |
3a128918 | 1703 | Pragma_Identifier => |
1704 | Make_Identifier (Sloc (Id), Nam), | |
1705 | Pragma_Argument_Associations => Args); | |
6c545057 | 1706 | |
89f1e35c | 1707 | Delay_Required := False; |
fad014fe | 1708 | end; |
85696508 | 1709 | |
3a128918 | 1710 | when Aspect_Contract_Cases => Contract_Cases : declare |
1711 | Case_Guard : Node_Id; | |
1712 | Extra : Node_Id; | |
1713 | Others_Seen : Boolean := False; | |
1714 | Post_Case : Node_Id; | |
1715 | ||
1716 | begin | |
1717 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1718 | Error_Msg_Name_1 := Nam; | |
1719 | Error_Msg_N ("incorrect placement of aspect `%`", E); | |
1720 | goto Continue; | |
1721 | end if; | |
1722 | ||
1723 | if Nkind (Expr) /= N_Aggregate then | |
1724 | Error_Msg_Name_1 := Nam; | |
1725 | Error_Msg_NE | |
1726 | ("wrong syntax for aspect `%` for &", Id, E); | |
1727 | goto Continue; | |
1728 | end if; | |
1729 | ||
1730 | -- Verify the legality of individual post cases | |
1731 | ||
1732 | Post_Case := First (Component_Associations (Expr)); | |
1733 | while Present (Post_Case) loop | |
1734 | if Nkind (Post_Case) /= N_Component_Association then | |
1735 | Error_Msg_N ("wrong syntax in post case", Post_Case); | |
1736 | goto Continue; | |
1737 | end if; | |
1738 | ||
1739 | -- Each post case must have exactly one case guard | |
1740 | ||
1741 | Case_Guard := First (Choices (Post_Case)); | |
1742 | Extra := Next (Case_Guard); | |
1743 | ||
1744 | if Present (Extra) then | |
1745 | Error_Msg_N | |
1746 | ("post case may have only one case guard", Extra); | |
1747 | goto Continue; | |
1748 | end if; | |
1749 | ||
1750 | -- Check the placement of "others" (if available) | |
1751 | ||
1752 | if Nkind (Case_Guard) = N_Others_Choice then | |
1753 | if Others_Seen then | |
1754 | Error_Msg_Name_1 := Nam; | |
1755 | Error_Msg_N | |
1756 | ("only one others choice allowed in aspect %", | |
1757 | Case_Guard); | |
1758 | goto Continue; | |
1759 | else | |
1760 | Others_Seen := True; | |
1761 | end if; | |
1762 | ||
1763 | elsif Others_Seen then | |
1764 | Error_Msg_Name_1 := Nam; | |
1765 | Error_Msg_N | |
1766 | ("others must be the last choice in aspect %", N); | |
1767 | goto Continue; | |
1768 | end if; | |
1769 | ||
1770 | Next (Post_Case); | |
1771 | end loop; | |
1772 | ||
1773 | -- Transform the aspect into a pragma | |
1774 | ||
1775 | Aitem := | |
1776 | Make_Pragma (Loc, | |
1777 | Pragma_Identifier => | |
1778 | Make_Identifier (Loc, Nam), | |
1779 | Pragma_Argument_Associations => New_List ( | |
1780 | Make_Pragma_Argument_Association (Loc, | |
1781 | Expression => Relocate_Node (Expr)))); | |
1782 | ||
1783 | Delay_Required := False; | |
1784 | end Contract_Cases; | |
1785 | ||
89f1e35c | 1786 | -- Case 5: Special handling for aspects with an optional |
1787 | -- boolean argument. | |
85696508 | 1788 | |
89f1e35c | 1789 | -- In the general case, the corresponding pragma cannot be |
1790 | -- generated yet because the evaluation of the boolean needs to | |
1791 | -- be delayed til the freeze point. | |
85696508 | 1792 | |
89f1e35c | 1793 | when Boolean_Aspects | |
1794 | Library_Unit_Aspects => | |
a5a64273 | 1795 | |
89f1e35c | 1796 | Set_Is_Boolean_Aspect (Aspect); |
a5a64273 | 1797 | |
89f1e35c | 1798 | -- Lock_Free aspect only apply to protected objects |
e1cedbae | 1799 | |
89f1e35c | 1800 | if A_Id = Aspect_Lock_Free then |
1801 | if Ekind (E) /= E_Protected_Type then | |
99a2d5bd | 1802 | Error_Msg_Name_1 := Nam; |
a5a64273 | 1803 | Error_Msg_N |
89f1e35c | 1804 | ("aspect % only applies to a protected object", |
1805 | Aspect); | |
1806 | ||
1807 | else | |
1808 | -- Set the Uses_Lock_Free flag to True if there is no | |
1809 | -- expression or if the expression is True. ??? The | |
1810 | -- evaluation of this aspect should be delayed to the | |
1811 | -- freeze point. | |
1812 | ||
1813 | if No (Expr) | |
1814 | or else Is_True (Static_Boolean (Expr)) | |
1815 | then | |
1816 | Set_Uses_Lock_Free (E); | |
1817 | end if; | |
caf125ce | 1818 | |
1819 | Record_Rep_Item (E, Aspect); | |
a5a64273 | 1820 | end if; |
e1cedbae | 1821 | |
89f1e35c | 1822 | goto Continue; |
ae888dbd | 1823 | |
17631aa0 | 1824 | elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then |
d74fc39a | 1825 | |
89f1e35c | 1826 | -- Verify that there is an aspect Convention that will |
1827 | -- incorporate the Import/Export aspect, and eventual | |
1828 | -- Link/External names. | |
cce84b09 | 1829 | |
89f1e35c | 1830 | declare |
1831 | A : Node_Id; | |
cce84b09 | 1832 | |
89f1e35c | 1833 | begin |
1834 | A := First (L); | |
1835 | while Present (A) loop | |
1836 | exit when Chars (Identifier (A)) = Name_Convention; | |
1837 | Next (A); | |
1838 | end loop; | |
d64221a7 | 1839 | |
89f1e35c | 1840 | if No (A) then |
1841 | Error_Msg_N | |
1842 | ("missing Convention aspect for Export/Import", | |
1843 | Aspect); | |
1844 | end if; | |
1845 | end; | |
d74fc39a | 1846 | |
89f1e35c | 1847 | goto Continue; |
1848 | end if; | |
d74fc39a | 1849 | |
89f1e35c | 1850 | -- This requires special handling in the case of a package |
1851 | -- declaration, the pragma needs to be inserted in the list | |
1852 | -- of declarations for the associated package. There is no | |
1853 | -- issue of visibility delay for these aspects. | |
d64221a7 | 1854 | |
89f1e35c | 1855 | if A_Id in Library_Unit_Aspects |
1856 | and then Nkind (N) = N_Package_Declaration | |
1857 | and then Nkind (Parent (N)) /= N_Compilation_Unit | |
1858 | then | |
1859 | Error_Msg_N | |
1860 | ("incorrect context for library unit aspect&", Id); | |
1861 | goto Continue; | |
1862 | end if; | |
cce84b09 | 1863 | |
89f1e35c | 1864 | -- Special handling when the aspect has no expression. In |
1865 | -- this case the value is considered to be True. Thus, we | |
1866 | -- simply insert the pragma, no delay is required. | |
d74fc39a | 1867 | |
89f1e35c | 1868 | if No (Expr) then |
1869 | Aitem := | |
1870 | Make_Pragma (Loc, | |
1871 | Pragma_Argument_Associations => New_List (Ent), | |
1872 | Pragma_Identifier => | |
1873 | Make_Identifier (Sloc (Id), Chars (Id))); | |
ddf1337b | 1874 | |
89f1e35c | 1875 | Delay_Required := False; |
ddf1337b | 1876 | |
89f1e35c | 1877 | -- In general cases, the corresponding pragma/attribute |
1878 | -- definition clause will be inserted later at the freezing | |
1879 | -- point. | |
ddf1337b | 1880 | |
89f1e35c | 1881 | else |
1882 | Aitem := Empty; | |
1883 | end if; | |
1884 | end case; | |
ddf1337b | 1885 | |
89f1e35c | 1886 | -- Attach the corresponding pragma/attribute definition clause to |
1887 | -- the aspect specification node. | |
d74fc39a | 1888 | |
89f1e35c | 1889 | if Present (Aitem) then |
1890 | Set_From_Aspect_Specification (Aitem, True); | |
d74fc39a | 1891 | |
89f1e35c | 1892 | if Nkind (Aitem) = N_Pragma then |
1893 | Set_Corresponding_Aspect (Aitem, Aspect); | |
1894 | end if; | |
1895 | end if; | |
53c179ea | 1896 | |
89f1e35c | 1897 | -- In the context of a compilation unit, we directly put the |
1898 | -- pragma in the Pragmas_After list of the | |
935e86e0 | 1899 | -- N_Compilation_Unit_Aux node (no delay is required here) |
3a72f9c3 | 1900 | -- except for aspects on a subprogram body (see below). |
ddf1337b | 1901 | |
89f1e35c | 1902 | if Nkind (Parent (N)) = N_Compilation_Unit |
1903 | and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect)) | |
1904 | then | |
1905 | declare | |
1906 | Aux : constant Node_Id := Aux_Decls_Node (Parent (N)); | |
7f694ca2 | 1907 | |
89f1e35c | 1908 | begin |
1909 | pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux); | |
7f694ca2 | 1910 | |
89f1e35c | 1911 | -- For a Boolean aspect, create the corresponding pragma if |
1912 | -- no expression or if the value is True. | |
7f694ca2 | 1913 | |
b9e61b2a | 1914 | if Is_Boolean_Aspect (Aspect) and then No (Aitem) then |
89f1e35c | 1915 | if Is_True (Static_Boolean (Expr)) then |
1916 | Aitem := | |
1917 | Make_Pragma (Loc, | |
1918 | Pragma_Argument_Associations => New_List (Ent), | |
1919 | Pragma_Identifier => | |
1920 | Make_Identifier (Sloc (Id), Chars (Id))); | |
7f694ca2 | 1921 | |
89f1e35c | 1922 | Set_From_Aspect_Specification (Aitem, True); |
1923 | Set_Corresponding_Aspect (Aitem, Aspect); | |
1924 | ||
1925 | else | |
1926 | goto Continue; | |
1927 | end if; | |
1928 | end if; | |
7f694ca2 | 1929 | |
3a72f9c3 | 1930 | -- If the aspect is on a subprogram body (relevant aspects |
1931 | -- are Inline and Priority), add the pragma in front of | |
1932 | -- the declarations. | |
1933 | ||
1934 | if Nkind (N) = N_Subprogram_Body then | |
1935 | if No (Declarations (N)) then | |
1936 | Set_Declarations (N, New_List); | |
1937 | end if; | |
1938 | ||
1939 | Prepend (Aitem, Declarations (N)); | |
1940 | ||
1941 | else | |
1942 | if No (Pragmas_After (Aux)) then | |
d4596fbe | 1943 | Set_Pragmas_After (Aux, New_List); |
3a72f9c3 | 1944 | end if; |
1945 | ||
1946 | Append (Aitem, Pragmas_After (Aux)); | |
89f1e35c | 1947 | end if; |
7f694ca2 | 1948 | |
89f1e35c | 1949 | goto Continue; |
1950 | end; | |
1951 | end if; | |
7f694ca2 | 1952 | |
89f1e35c | 1953 | -- The evaluation of the aspect is delayed to the freezing point. |
1954 | -- The pragma or attribute clause if there is one is then attached | |
1955 | -- to the aspect specification which is placed in the rep item | |
1956 | -- list. | |
1a814552 | 1957 | |
89f1e35c | 1958 | if Delay_Required then |
1959 | if Present (Aitem) then | |
1960 | Set_Is_Delayed_Aspect (Aitem); | |
1961 | Set_Aspect_Rep_Item (Aspect, Aitem); | |
1962 | Set_Parent (Aitem, Aspect); | |
1963 | end if; | |
1a814552 | 1964 | |
89f1e35c | 1965 | Set_Is_Delayed_Aspect (Aspect); |
9f36e3fb | 1966 | |
1967 | -- In the case of Default_Value, link aspect to base type | |
1968 | -- as well, even though it appears on a first subtype. This | |
1969 | -- is mandated by the semantics of the aspect. Verify that | |
1970 | -- this a scalar type, to prevent cascaded errors. | |
1971 | ||
c826347a | 1972 | if A_Id = Aspect_Default_Value and then Is_Scalar_Type (E) then |
9f36e3fb | 1973 | Set_Has_Delayed_Aspects (Base_Type (E)); |
1974 | Record_Rep_Item (Base_Type (E), Aspect); | |
1975 | end if; | |
1976 | ||
89f1e35c | 1977 | Set_Has_Delayed_Aspects (E); |
1978 | Record_Rep_Item (E, Aspect); | |
ddf1337b | 1979 | |
89f1e35c | 1980 | -- When delay is not required and the context is not a compilation |
1981 | -- unit, we simply insert the pragma/attribute definition clause | |
1982 | -- in sequence. | |
ddf1337b | 1983 | |
89f1e35c | 1984 | else |
1985 | Insert_After (Ins_Node, Aitem); | |
1986 | Ins_Node := Aitem; | |
d74fc39a | 1987 | end if; |
ae888dbd | 1988 | end; |
1989 | ||
d64221a7 | 1990 | <<Continue>> |
1991 | Next (Aspect); | |
21ea3a4f | 1992 | end loop Aspect_Loop; |
89f1e35c | 1993 | |
1994 | if Has_Delayed_Aspects (E) then | |
1995 | Ensure_Freeze_Node (E); | |
1996 | end if; | |
21ea3a4f | 1997 | end Analyze_Aspect_Specifications; |
ae888dbd | 1998 | |
d6f39728 | 1999 | ----------------------- |
2000 | -- Analyze_At_Clause -- | |
2001 | ----------------------- | |
2002 | ||
2003 | -- An at clause is replaced by the corresponding Address attribute | |
2004 | -- definition clause that is the preferred approach in Ada 95. | |
2005 | ||
2006 | procedure Analyze_At_Clause (N : Node_Id) is | |
177675a7 | 2007 | CS : constant Boolean := Comes_From_Source (N); |
2008 | ||
d6f39728 | 2009 | begin |
177675a7 | 2010 | -- This is an obsolescent feature |
2011 | ||
e0521a36 | 2012 | Check_Restriction (No_Obsolescent_Features, N); |
2013 | ||
9dfe12ae | 2014 | if Warn_On_Obsolescent_Feature then |
2015 | Error_Msg_N | |
b174444e | 2016 | ("?j?at clause is an obsolescent feature (RM J.7(2))", N); |
9dfe12ae | 2017 | Error_Msg_N |
b174444e | 2018 | ("\?j?use address attribute definition clause instead", N); |
9dfe12ae | 2019 | end if; |
2020 | ||
177675a7 | 2021 | -- Rewrite as address clause |
2022 | ||
d6f39728 | 2023 | Rewrite (N, |
2024 | Make_Attribute_Definition_Clause (Sloc (N), | |
935e86e0 | 2025 | Name => Identifier (N), |
2026 | Chars => Name_Address, | |
d6f39728 | 2027 | Expression => Expression (N))); |
177675a7 | 2028 | |
2beb22b1 | 2029 | -- We preserve Comes_From_Source, since logically the clause still comes |
2030 | -- from the source program even though it is changed in form. | |
177675a7 | 2031 | |
2032 | Set_Comes_From_Source (N, CS); | |
2033 | ||
2034 | -- Analyze rewritten clause | |
2035 | ||
d6f39728 | 2036 | Analyze_Attribute_Definition_Clause (N); |
2037 | end Analyze_At_Clause; | |
2038 | ||
2039 | ----------------------------------------- | |
2040 | -- Analyze_Attribute_Definition_Clause -- | |
2041 | ----------------------------------------- | |
2042 | ||
2043 | procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is | |
2044 | Loc : constant Source_Ptr := Sloc (N); | |
2045 | Nam : constant Node_Id := Name (N); | |
2046 | Attr : constant Name_Id := Chars (N); | |
2047 | Expr : constant Node_Id := Expression (N); | |
2048 | Id : constant Attribute_Id := Get_Attribute_Id (Attr); | |
d64221a7 | 2049 | |
2050 | Ent : Entity_Id; | |
2051 | -- The entity of Nam after it is analyzed. In the case of an incomplete | |
2052 | -- type, this is the underlying type. | |
2053 | ||
d6f39728 | 2054 | U_Ent : Entity_Id; |
d64221a7 | 2055 | -- The underlying entity to which the attribute applies. Generally this |
2056 | -- is the Underlying_Type of Ent, except in the case where the clause | |
2057 | -- applies to full view of incomplete type or private type in which case | |
2058 | -- U_Ent is just a copy of Ent. | |
d6f39728 | 2059 | |
2060 | FOnly : Boolean := False; | |
2061 | -- Reset to True for subtype specific attribute (Alignment, Size) | |
2062 | -- and for stream attributes, i.e. those cases where in the call | |
2063 | -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing | |
2064 | -- rules are checked. Note that the case of stream attributes is not | |
2065 | -- clear from the RM, but see AI95-00137. Also, the RM seems to | |
2066 | -- disallow Storage_Size for derived task types, but that is also | |
2067 | -- clearly unintentional. | |
2068 | ||
9f373bb8 | 2069 | procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type); |
2070 | -- Common processing for 'Read, 'Write, 'Input and 'Output attribute | |
2071 | -- definition clauses. | |
2072 | ||
ae888dbd | 2073 | function Duplicate_Clause return Boolean; |
2074 | -- This routine checks if the aspect for U_Ent being given by attribute | |
2075 | -- definition clause N is for an aspect that has already been specified, | |
2076 | -- and if so gives an error message. If there is a duplicate, True is | |
2077 | -- returned, otherwise if there is no error, False is returned. | |
2078 | ||
81b424ac | 2079 | procedure Check_Indexing_Functions; |
2080 | -- Check that the function in Constant_Indexing or Variable_Indexing | |
2081 | -- attribute has the proper type structure. If the name is overloaded, | |
cac18f71 | 2082 | -- check that some interpretation is legal. |
81b424ac | 2083 | |
89cc7147 | 2084 | procedure Check_Iterator_Functions; |
2085 | -- Check that there is a single function in Default_Iterator attribute | |
8df4f2a5 | 2086 | -- has the proper type structure. |
89cc7147 | 2087 | |
2088 | function Check_Primitive_Function (Subp : Entity_Id) return Boolean; | |
d03bfaa1 | 2089 | -- Common legality check for the previous two |
89cc7147 | 2090 | |
177675a7 | 2091 | ----------------------------------- |
2092 | -- Analyze_Stream_TSS_Definition -- | |
2093 | ----------------------------------- | |
2094 | ||
9f373bb8 | 2095 | procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is |
2096 | Subp : Entity_Id := Empty; | |
2097 | I : Interp_Index; | |
2098 | It : Interp; | |
2099 | Pnam : Entity_Id; | |
2100 | ||
2101 | Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read); | |
d64221a7 | 2102 | -- True for Read attribute, false for other attributes |
9f373bb8 | 2103 | |
2104 | function Has_Good_Profile (Subp : Entity_Id) return Boolean; | |
2105 | -- Return true if the entity is a subprogram with an appropriate | |
2106 | -- profile for the attribute being defined. | |
2107 | ||
2108 | ---------------------- | |
2109 | -- Has_Good_Profile -- | |
2110 | ---------------------- | |
2111 | ||
2112 | function Has_Good_Profile (Subp : Entity_Id) return Boolean is | |
2113 | F : Entity_Id; | |
2114 | Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input); | |
2115 | Expected_Ekind : constant array (Boolean) of Entity_Kind := | |
2116 | (False => E_Procedure, True => E_Function); | |
2117 | Typ : Entity_Id; | |
2118 | ||
2119 | begin | |
2120 | if Ekind (Subp) /= Expected_Ekind (Is_Function) then | |
2121 | return False; | |
2122 | end if; | |
2123 | ||
2124 | F := First_Formal (Subp); | |
2125 | ||
2126 | if No (F) | |
2127 | or else Ekind (Etype (F)) /= E_Anonymous_Access_Type | |
2128 | or else Designated_Type (Etype (F)) /= | |
2129 | Class_Wide_Type (RTE (RE_Root_Stream_Type)) | |
2130 | then | |
2131 | return False; | |
2132 | end if; | |
2133 | ||
2134 | if not Is_Function then | |
2135 | Next_Formal (F); | |
2136 | ||
2137 | declare | |
2138 | Expected_Mode : constant array (Boolean) of Entity_Kind := | |
2139 | (False => E_In_Parameter, | |
2140 | True => E_Out_Parameter); | |
2141 | begin | |
2142 | if Parameter_Mode (F) /= Expected_Mode (Is_Read) then | |
2143 | return False; | |
2144 | end if; | |
2145 | end; | |
2146 | ||
2147 | Typ := Etype (F); | |
2148 | ||
2149 | else | |
2150 | Typ := Etype (Subp); | |
2151 | end if; | |
2152 | ||
2153 | return Base_Type (Typ) = Base_Type (Ent) | |
2154 | and then No (Next_Formal (F)); | |
9f373bb8 | 2155 | end Has_Good_Profile; |
2156 | ||
2157 | -- Start of processing for Analyze_Stream_TSS_Definition | |
2158 | ||
2159 | begin | |
2160 | FOnly := True; | |
2161 | ||
2162 | if not Is_Type (U_Ent) then | |
2163 | Error_Msg_N ("local name must be a subtype", Nam); | |
2164 | return; | |
2165 | end if; | |
2166 | ||
2167 | Pnam := TSS (Base_Type (U_Ent), TSS_Nam); | |
2168 | ||
44e4341e | 2169 | -- If Pnam is present, it can be either inherited from an ancestor |
2170 | -- type (in which case it is legal to redefine it for this type), or | |
2171 | -- be a previous definition of the attribute for the same type (in | |
2172 | -- which case it is illegal). | |
2173 | ||
2174 | -- In the first case, it will have been analyzed already, and we | |
2175 | -- can check that its profile does not match the expected profile | |
2176 | -- for a stream attribute of U_Ent. In the second case, either Pnam | |
2177 | -- has been analyzed (and has the expected profile), or it has not | |
2178 | -- been analyzed yet (case of a type that has not been frozen yet | |
2179 | -- and for which the stream attribute has been set using Set_TSS). | |
2180 | ||
2181 | if Present (Pnam) | |
2182 | and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam)) | |
2183 | then | |
9f373bb8 | 2184 | Error_Msg_Sloc := Sloc (Pnam); |
2185 | Error_Msg_Name_1 := Attr; | |
2186 | Error_Msg_N ("% attribute already defined #", Nam); | |
2187 | return; | |
2188 | end if; | |
2189 | ||
2190 | Analyze (Expr); | |
2191 | ||
2192 | if Is_Entity_Name (Expr) then | |
2193 | if not Is_Overloaded (Expr) then | |
2194 | if Has_Good_Profile (Entity (Expr)) then | |
2195 | Subp := Entity (Expr); | |
2196 | end if; | |
2197 | ||
2198 | else | |
2199 | Get_First_Interp (Expr, I, It); | |
9f373bb8 | 2200 | while Present (It.Nam) loop |
2201 | if Has_Good_Profile (It.Nam) then | |
2202 | Subp := It.Nam; | |
2203 | exit; | |
2204 | end if; | |
2205 | ||
2206 | Get_Next_Interp (I, It); | |
2207 | end loop; | |
2208 | end if; | |
2209 | end if; | |
2210 | ||
2211 | if Present (Subp) then | |
59ac57b5 | 2212 | if Is_Abstract_Subprogram (Subp) then |
9f373bb8 | 2213 | Error_Msg_N ("stream subprogram must not be abstract", Expr); |
2214 | return; | |
2215 | end if; | |
2216 | ||
2217 | Set_Entity (Expr, Subp); | |
2218 | Set_Etype (Expr, Etype (Subp)); | |
2219 | ||
44e4341e | 2220 | New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam); |
9f373bb8 | 2221 | |
2222 | else | |
2223 | Error_Msg_Name_1 := Attr; | |
2224 | Error_Msg_N ("incorrect expression for% attribute", Expr); | |
2225 | end if; | |
2226 | end Analyze_Stream_TSS_Definition; | |
2227 | ||
81b424ac | 2228 | ------------------------------ |
2229 | -- Check_Indexing_Functions -- | |
2230 | ------------------------------ | |
2231 | ||
2232 | procedure Check_Indexing_Functions is | |
cac18f71 | 2233 | Indexing_Found : Boolean; |
8df4f2a5 | 2234 | |
81b424ac | 2235 | procedure Check_One_Function (Subp : Entity_Id); |
a45d946f | 2236 | -- Check one possible interpretation. Sets Indexing_Found True if an |
2237 | -- indexing function is found. | |
81b424ac | 2238 | |
2239 | ------------------------ | |
2240 | -- Check_One_Function -- | |
2241 | ------------------------ | |
2242 | ||
2243 | procedure Check_One_Function (Subp : Entity_Id) is | |
1b7510f9 | 2244 | Default_Element : constant Node_Id := |
2c5754de | 2245 | Find_Aspect |
2246 | (Etype (First_Formal (Subp)), | |
2247 | Aspect_Iterator_Element); | |
1b7510f9 | 2248 | |
81b424ac | 2249 | begin |
cac18f71 | 2250 | if not Check_Primitive_Function (Subp) |
2251 | and then not Is_Overloaded (Expr) | |
2252 | then | |
89cc7147 | 2253 | Error_Msg_NE |
2254 | ("aspect Indexing requires a function that applies to type&", | |
cac18f71 | 2255 | Subp, Ent); |
81b424ac | 2256 | end if; |
2257 | ||
1b7510f9 | 2258 | -- An indexing function must return either the default element of |
cac18f71 | 2259 | -- the container, or a reference type. For variable indexing it |
a45d946f | 2260 | -- must be the latter. |
1b7510f9 | 2261 | |
2262 | if Present (Default_Element) then | |
2263 | Analyze (Default_Element); | |
a45d946f | 2264 | |
1b7510f9 | 2265 | if Is_Entity_Name (Default_Element) |
2c5754de | 2266 | and then Covers (Entity (Default_Element), Etype (Subp)) |
1b7510f9 | 2267 | then |
cac18f71 | 2268 | Indexing_Found := True; |
1b7510f9 | 2269 | return; |
2270 | end if; | |
2271 | end if; | |
2272 | ||
a45d946f | 2273 | -- For variable_indexing the return type must be a reference type |
1b7510f9 | 2274 | |
cac18f71 | 2275 | if Attr = Name_Variable_Indexing |
2276 | and then not Has_Implicit_Dereference (Etype (Subp)) | |
2277 | then | |
81b424ac | 2278 | Error_Msg_N |
2279 | ("function for indexing must return a reference type", Subp); | |
cac18f71 | 2280 | |
2281 | else | |
2282 | Indexing_Found := True; | |
81b424ac | 2283 | end if; |
2284 | end Check_One_Function; | |
2285 | ||
2286 | -- Start of processing for Check_Indexing_Functions | |
2287 | ||
2288 | begin | |
89cc7147 | 2289 | if In_Instance then |
2290 | return; | |
2291 | end if; | |
2292 | ||
81b424ac | 2293 | Analyze (Expr); |
2294 | ||
2295 | if not Is_Overloaded (Expr) then | |
2296 | Check_One_Function (Entity (Expr)); | |
2297 | ||
2298 | else | |
2299 | declare | |
2c5754de | 2300 | I : Interp_Index; |
81b424ac | 2301 | It : Interp; |
2302 | ||
2303 | begin | |
cac18f71 | 2304 | Indexing_Found := False; |
81b424ac | 2305 | Get_First_Interp (Expr, I, It); |
2306 | while Present (It.Nam) loop | |
2307 | ||
2308 | -- Note that analysis will have added the interpretation | |
2309 | -- that corresponds to the dereference. We only check the | |
2310 | -- subprogram itself. | |
2311 | ||
2312 | if Is_Overloadable (It.Nam) then | |
2313 | Check_One_Function (It.Nam); | |
2314 | end if; | |
2315 | ||
2316 | Get_Next_Interp (I, It); | |
2317 | end loop; | |
a45d946f | 2318 | |
cac18f71 | 2319 | if not Indexing_Found then |
a45d946f | 2320 | Error_Msg_NE |
2321 | ("aspect Indexing requires a function that " | |
2322 | & "applies to type&", Expr, Ent); | |
cac18f71 | 2323 | end if; |
81b424ac | 2324 | end; |
2325 | end if; | |
2326 | end Check_Indexing_Functions; | |
2327 | ||
89cc7147 | 2328 | ------------------------------ |
2329 | -- Check_Iterator_Functions -- | |
2330 | ------------------------------ | |
2331 | ||
2332 | procedure Check_Iterator_Functions is | |
2333 | Default : Entity_Id; | |
2334 | ||
2335 | function Valid_Default_Iterator (Subp : Entity_Id) return Boolean; | |
8df4f2a5 | 2336 | -- Check one possible interpretation for validity |
89cc7147 | 2337 | |
2338 | ---------------------------- | |
2339 | -- Valid_Default_Iterator -- | |
2340 | ---------------------------- | |
2341 | ||
2342 | function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is | |
2343 | Formal : Entity_Id; | |
2344 | ||
2345 | begin | |
2346 | if not Check_Primitive_Function (Subp) then | |
2347 | return False; | |
2348 | else | |
2349 | Formal := First_Formal (Subp); | |
2350 | end if; | |
2351 | ||
8df4f2a5 | 2352 | -- False if any subsequent formal has no default expression |
89cc7147 | 2353 | |
8df4f2a5 | 2354 | Formal := Next_Formal (Formal); |
2355 | while Present (Formal) loop | |
2356 | if No (Expression (Parent (Formal))) then | |
2357 | return False; | |
2358 | end if; | |
89cc7147 | 2359 | |
8df4f2a5 | 2360 | Next_Formal (Formal); |
2361 | end loop; | |
89cc7147 | 2362 | |
8df4f2a5 | 2363 | -- True if all subsequent formals have default expressions |
89cc7147 | 2364 | |
2365 | return True; | |
2366 | end Valid_Default_Iterator; | |
2367 | ||
2368 | -- Start of processing for Check_Iterator_Functions | |
2369 | ||
2370 | begin | |
2371 | Analyze (Expr); | |
2372 | ||
2373 | if not Is_Entity_Name (Expr) then | |
2374 | Error_Msg_N ("aspect Iterator must be a function name", Expr); | |
2375 | end if; | |
2376 | ||
2377 | if not Is_Overloaded (Expr) then | |
2378 | if not Check_Primitive_Function (Entity (Expr)) then | |
2379 | Error_Msg_NE | |
2380 | ("aspect Indexing requires a function that applies to type&", | |
2381 | Entity (Expr), Ent); | |
2382 | end if; | |
2383 | ||
2384 | if not Valid_Default_Iterator (Entity (Expr)) then | |
2385 | Error_Msg_N ("improper function for default iterator", Expr); | |
2386 | end if; | |
2387 | ||
2388 | else | |
2389 | Default := Empty; | |
2390 | declare | |
2391 | I : Interp_Index; | |
2392 | It : Interp; | |
2393 | ||
2394 | begin | |
2395 | Get_First_Interp (Expr, I, It); | |
2396 | while Present (It.Nam) loop | |
2397 | if not Check_Primitive_Function (It.Nam) | |
59f3e675 | 2398 | or else not Valid_Default_Iterator (It.Nam) |
89cc7147 | 2399 | then |
2400 | Remove_Interp (I); | |
2401 | ||
2402 | elsif Present (Default) then | |
2403 | Error_Msg_N ("default iterator must be unique", Expr); | |
2404 | ||
2405 | else | |
2406 | Default := It.Nam; | |
2407 | end if; | |
2408 | ||
2409 | Get_Next_Interp (I, It); | |
2410 | end loop; | |
2411 | end; | |
2412 | ||
2413 | if Present (Default) then | |
2414 | Set_Entity (Expr, Default); | |
2415 | Set_Is_Overloaded (Expr, False); | |
2416 | end if; | |
2417 | end if; | |
2418 | end Check_Iterator_Functions; | |
2419 | ||
2420 | ------------------------------- | |
2421 | -- Check_Primitive_Function -- | |
2422 | ------------------------------- | |
2423 | ||
2424 | function Check_Primitive_Function (Subp : Entity_Id) return Boolean is | |
2425 | Ctrl : Entity_Id; | |
2426 | ||
2427 | begin | |
2428 | if Ekind (Subp) /= E_Function then | |
2429 | return False; | |
2430 | end if; | |
2431 | ||
2432 | if No (First_Formal (Subp)) then | |
2433 | return False; | |
2434 | else | |
2435 | Ctrl := Etype (First_Formal (Subp)); | |
2436 | end if; | |
2437 | ||
2438 | if Ctrl = Ent | |
2439 | or else Ctrl = Class_Wide_Type (Ent) | |
2440 | or else | |
2441 | (Ekind (Ctrl) = E_Anonymous_Access_Type | |
2442 | and then | |
2443 | (Designated_Type (Ctrl) = Ent | |
2444 | or else Designated_Type (Ctrl) = Class_Wide_Type (Ent))) | |
2445 | then | |
2446 | null; | |
2447 | ||
2448 | else | |
2449 | return False; | |
2450 | end if; | |
2451 | ||
2452 | return True; | |
2453 | end Check_Primitive_Function; | |
2454 | ||
ae888dbd | 2455 | ---------------------- |
2456 | -- Duplicate_Clause -- | |
2457 | ---------------------- | |
2458 | ||
2459 | function Duplicate_Clause return Boolean is | |
d74fc39a | 2460 | A : Node_Id; |
ae888dbd | 2461 | |
2462 | begin | |
c8969ba6 | 2463 | -- Nothing to do if this attribute definition clause comes from |
2464 | -- an aspect specification, since we could not be duplicating an | |
ae888dbd | 2465 | -- explicit clause, and we dealt with the case of duplicated aspects |
2466 | -- in Analyze_Aspect_Specifications. | |
2467 | ||
2468 | if From_Aspect_Specification (N) then | |
2469 | return False; | |
2470 | end if; | |
2471 | ||
89f1e35c | 2472 | -- Otherwise current clause may duplicate previous clause, or a |
2473 | -- previously given pragma or aspect specification for the same | |
2474 | -- aspect. | |
d74fc39a | 2475 | |
89b3b365 | 2476 | A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False); |
ae888dbd | 2477 | |
2478 | if Present (A) then | |
89f1e35c | 2479 | Error_Msg_Name_1 := Chars (N); |
2480 | Error_Msg_Sloc := Sloc (A); | |
2481 | ||
89b3b365 | 2482 | Error_Msg_NE ("aspect% for & previously given#", N, U_Ent); |
89f1e35c | 2483 | return True; |
ae888dbd | 2484 | end if; |
2485 | ||
2486 | return False; | |
2487 | end Duplicate_Clause; | |
2488 | ||
9f373bb8 | 2489 | -- Start of processing for Analyze_Attribute_Definition_Clause |
2490 | ||
d6f39728 | 2491 | begin |
d64221a7 | 2492 | -- The following code is a defense against recursion. Not clear that |
2493 | -- this can happen legitimately, but perhaps some error situations | |
2494 | -- can cause it, and we did see this recursion during testing. | |
2495 | ||
2496 | if Analyzed (N) then | |
2497 | return; | |
2498 | else | |
2499 | Set_Analyzed (N, True); | |
2500 | end if; | |
2501 | ||
a29bc1d9 | 2502 | -- Ignore some selected attributes in CodePeer mode since they are not |
2503 | -- relevant in this context. | |
2504 | ||
2505 | if CodePeer_Mode then | |
2506 | case Id is | |
2507 | ||
2508 | -- Ignore Component_Size in CodePeer mode, to avoid changing the | |
2509 | -- internal representation of types by implicitly packing them. | |
2510 | ||
2511 | when Attribute_Component_Size => | |
2512 | Rewrite (N, Make_Null_Statement (Sloc (N))); | |
2513 | return; | |
2514 | ||
2515 | when others => | |
2516 | null; | |
2517 | end case; | |
2518 | end if; | |
2519 | ||
d8ba53a8 | 2520 | -- Process Ignore_Rep_Clauses option |
eef1ca1e | 2521 | |
d8ba53a8 | 2522 | if Ignore_Rep_Clauses then |
9d627c41 | 2523 | case Id is |
2524 | ||
eef1ca1e | 2525 | -- The following should be ignored. They do not affect legality |
2526 | -- and may be target dependent. The basic idea of -gnatI is to | |
2527 | -- ignore any rep clauses that may be target dependent but do not | |
2528 | -- affect legality (except possibly to be rejected because they | |
2529 | -- are incompatible with the compilation target). | |
9d627c41 | 2530 | |
2f1aac99 | 2531 | when Attribute_Alignment | |
9d627c41 | 2532 | Attribute_Bit_Order | |
2533 | Attribute_Component_Size | | |
2534 | Attribute_Machine_Radix | | |
2535 | Attribute_Object_Size | | |
2536 | Attribute_Size | | |
9d627c41 | 2537 | Attribute_Stream_Size | |
2538 | Attribute_Value_Size => | |
9d627c41 | 2539 | Rewrite (N, Make_Null_Statement (Sloc (N))); |
2540 | return; | |
2541 | ||
d8ba53a8 | 2542 | -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ??? |
fe639c68 | 2543 | |
2544 | when Attribute_Small => | |
2545 | if Ignore_Rep_Clauses then | |
2546 | Rewrite (N, Make_Null_Statement (Sloc (N))); | |
2547 | return; | |
2548 | end if; | |
2549 | ||
eef1ca1e | 2550 | -- The following should not be ignored, because in the first place |
2551 | -- they are reasonably portable, and should not cause problems in | |
2552 | -- compiling code from another target, and also they do affect | |
2553 | -- legality, e.g. failing to provide a stream attribute for a | |
2554 | -- type may make a program illegal. | |
9d627c41 | 2555 | |
b55f7641 | 2556 | when Attribute_External_Tag | |
2557 | Attribute_Input | | |
2558 | Attribute_Output | | |
2559 | Attribute_Read | | |
2560 | Attribute_Simple_Storage_Pool | | |
2561 | Attribute_Storage_Pool | | |
2562 | Attribute_Storage_Size | | |
2563 | Attribute_Write => | |
9d627c41 | 2564 | null; |
2565 | ||
b593a52c | 2566 | -- Other cases are errors ("attribute& cannot be set with |
2567 | -- definition clause"), which will be caught below. | |
9d627c41 | 2568 | |
2569 | when others => | |
2570 | null; | |
2571 | end case; | |
fbc67f84 | 2572 | end if; |
2573 | ||
d6f39728 | 2574 | Analyze (Nam); |
2575 | Ent := Entity (Nam); | |
2576 | ||
2577 | if Rep_Item_Too_Early (Ent, N) then | |
2578 | return; | |
2579 | end if; | |
2580 | ||
9f373bb8 | 2581 | -- Rep clause applies to full view of incomplete type or private type if |
2582 | -- we have one (if not, this is a premature use of the type). However, | |
2583 | -- certain semantic checks need to be done on the specified entity (i.e. | |
2584 | -- the private view), so we save it in Ent. | |
d6f39728 | 2585 | |
2586 | if Is_Private_Type (Ent) | |
2587 | and then Is_Derived_Type (Ent) | |
2588 | and then not Is_Tagged_Type (Ent) | |
2589 | and then No (Full_View (Ent)) | |
2590 | then | |
9f373bb8 | 2591 | -- If this is a private type whose completion is a derivation from |
2592 | -- another private type, there is no full view, and the attribute | |
2593 | -- belongs to the type itself, not its underlying parent. | |
d6f39728 | 2594 | |
2595 | U_Ent := Ent; | |
2596 | ||
2597 | elsif Ekind (Ent) = E_Incomplete_Type then | |
d5b349fa | 2598 | |
9f373bb8 | 2599 | -- The attribute applies to the full view, set the entity of the |
2600 | -- attribute definition accordingly. | |
d5b349fa | 2601 | |
d6f39728 | 2602 | Ent := Underlying_Type (Ent); |
2603 | U_Ent := Ent; | |
d5b349fa | 2604 | Set_Entity (Nam, Ent); |
2605 | ||
d6f39728 | 2606 | else |
2607 | U_Ent := Underlying_Type (Ent); | |
2608 | end if; | |
2609 | ||
44705307 | 2610 | -- Avoid cascaded error |
d6f39728 | 2611 | |
2612 | if Etype (Nam) = Any_Type then | |
2613 | return; | |
2614 | ||
89f1e35c | 2615 | -- Must be declared in current scope or in case of an aspect |
ace3389d | 2616 | -- specification, must be visible in current scope. |
44705307 | 2617 | |
89f1e35c | 2618 | elsif Scope (Ent) /= Current_Scope |
ace3389d | 2619 | and then |
2620 | not (From_Aspect_Specification (N) | |
2621 | and then Scope_Within_Or_Same (Current_Scope, Scope (Ent))) | |
89f1e35c | 2622 | then |
d6f39728 | 2623 | Error_Msg_N ("entity must be declared in this scope", Nam); |
2624 | return; | |
2625 | ||
44705307 | 2626 | -- Must not be a source renaming (we do have some cases where the |
2627 | -- expander generates a renaming, and those cases are OK, in such | |
a3248fc4 | 2628 | -- cases any attribute applies to the renamed object as well). |
44705307 | 2629 | |
2630 | elsif Is_Object (Ent) | |
2631 | and then Present (Renamed_Object (Ent)) | |
44705307 | 2632 | then |
a3248fc4 | 2633 | -- Case of renamed object from source, this is an error |
2634 | ||
2635 | if Comes_From_Source (Renamed_Object (Ent)) then | |
2636 | Get_Name_String (Chars (N)); | |
2637 | Error_Msg_Strlen := Name_Len; | |
2638 | Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len); | |
2639 | Error_Msg_N | |
2640 | ("~ clause not allowed for a renaming declaration " | |
2641 | & "(RM 13.1(6))", Nam); | |
2642 | return; | |
2643 | ||
2644 | -- For the case of a compiler generated renaming, the attribute | |
2645 | -- definition clause applies to the renamed object created by the | |
2646 | -- expander. The easiest general way to handle this is to create a | |
2647 | -- copy of the attribute definition clause for this object. | |
2648 | ||
2649 | else | |
2650 | Insert_Action (N, | |
2651 | Make_Attribute_Definition_Clause (Loc, | |
2652 | Name => | |
2653 | New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc), | |
2654 | Chars => Chars (N), | |
2655 | Expression => Duplicate_Subexpr (Expression (N)))); | |
2656 | end if; | |
44705307 | 2657 | |
2658 | -- If no underlying entity, use entity itself, applies to some | |
2659 | -- previously detected error cases ??? | |
2660 | ||
f15731c4 | 2661 | elsif No (U_Ent) then |
2662 | U_Ent := Ent; | |
2663 | ||
44705307 | 2664 | -- Cannot specify for a subtype (exception Object/Value_Size) |
2665 | ||
d6f39728 | 2666 | elsif Is_Type (U_Ent) |
2667 | and then not Is_First_Subtype (U_Ent) | |
2668 | and then Id /= Attribute_Object_Size | |
2669 | and then Id /= Attribute_Value_Size | |
2670 | and then not From_At_Mod (N) | |
2671 | then | |
2672 | Error_Msg_N ("cannot specify attribute for subtype", Nam); | |
2673 | return; | |
d6f39728 | 2674 | end if; |
2675 | ||
ae888dbd | 2676 | Set_Entity (N, U_Ent); |
2677 | ||
d6f39728 | 2678 | -- Switch on particular attribute |
2679 | ||
2680 | case Id is | |
2681 | ||
2682 | ------------- | |
2683 | -- Address -- | |
2684 | ------------- | |
2685 | ||
2686 | -- Address attribute definition clause | |
2687 | ||
2688 | when Attribute_Address => Address : begin | |
177675a7 | 2689 | |
2690 | -- A little error check, catch for X'Address use X'Address; | |
2691 | ||
2692 | if Nkind (Nam) = N_Identifier | |
2693 | and then Nkind (Expr) = N_Attribute_Reference | |
2694 | and then Attribute_Name (Expr) = Name_Address | |
2695 | and then Nkind (Prefix (Expr)) = N_Identifier | |
2696 | and then Chars (Nam) = Chars (Prefix (Expr)) | |
2697 | then | |
2698 | Error_Msg_NE | |
2699 | ("address for & is self-referencing", Prefix (Expr), Ent); | |
2700 | return; | |
2701 | end if; | |
2702 | ||
2703 | -- Not that special case, carry on with analysis of expression | |
2704 | ||
d6f39728 | 2705 | Analyze_And_Resolve (Expr, RTE (RE_Address)); |
2706 | ||
2f1aac99 | 2707 | -- Even when ignoring rep clauses we need to indicate that the |
2708 | -- entity has an address clause and thus it is legal to declare | |
2709 | -- it imported. | |
2710 | ||
2711 | if Ignore_Rep_Clauses then | |
d3ef794c | 2712 | if Ekind_In (U_Ent, E_Variable, E_Constant) then |
2f1aac99 | 2713 | Record_Rep_Item (U_Ent, N); |
2714 | end if; | |
2715 | ||
2716 | return; | |
2717 | end if; | |
2718 | ||
ae888dbd | 2719 | if Duplicate_Clause then |
2720 | null; | |
d6f39728 | 2721 | |
2722 | -- Case of address clause for subprogram | |
2723 | ||
2724 | elsif Is_Subprogram (U_Ent) then | |
d6f39728 | 2725 | if Has_Homonym (U_Ent) then |
2726 | Error_Msg_N | |
2727 | ("address clause cannot be given " & | |
2728 | "for overloaded subprogram", | |
2729 | Nam); | |
83f8f0a6 | 2730 | return; |
d6f39728 | 2731 | end if; |
2732 | ||
83f8f0a6 | 2733 | -- For subprograms, all address clauses are permitted, and we |
2734 | -- mark the subprogram as having a deferred freeze so that Gigi | |
2735 | -- will not elaborate it too soon. | |
d6f39728 | 2736 | |
2737 | -- Above needs more comments, what is too soon about??? | |
2738 | ||
2739 | Set_Has_Delayed_Freeze (U_Ent); | |
2740 | ||
2741 | -- Case of address clause for entry | |
2742 | ||
2743 | elsif Ekind (U_Ent) = E_Entry then | |
d6f39728 | 2744 | if Nkind (Parent (N)) = N_Task_Body then |
2745 | Error_Msg_N | |
2746 | ("entry address must be specified in task spec", Nam); | |
83f8f0a6 | 2747 | return; |
d6f39728 | 2748 | end if; |
2749 | ||
2750 | -- For entries, we require a constant address | |
2751 | ||
2752 | Check_Constant_Address_Clause (Expr, U_Ent); | |
2753 | ||
83f8f0a6 | 2754 | -- Special checks for task types |
2755 | ||
f15731c4 | 2756 | if Is_Task_Type (Scope (U_Ent)) |
2757 | and then Comes_From_Source (Scope (U_Ent)) | |
2758 | then | |
2759 | Error_Msg_N | |
1e3532e7 | 2760 | ("??entry address declared for entry in task type", N); |
f15731c4 | 2761 | Error_Msg_N |
1e3532e7 | 2762 | ("\??only one task can be declared of this type", N); |
f15731c4 | 2763 | end if; |
2764 | ||
83f8f0a6 | 2765 | -- Entry address clauses are obsolescent |
2766 | ||
e0521a36 | 2767 | Check_Restriction (No_Obsolescent_Features, N); |
2768 | ||
9dfe12ae | 2769 | if Warn_On_Obsolescent_Feature then |
2770 | Error_Msg_N | |
1e3532e7 | 2771 | ("?j?attaching interrupt to task entry is an " & |
2772 | "obsolescent feature (RM J.7.1)", N); | |
9dfe12ae | 2773 | Error_Msg_N |
1e3532e7 | 2774 | ("\?j?use interrupt procedure instead", N); |
9dfe12ae | 2775 | end if; |
2776 | ||
83f8f0a6 | 2777 | -- Case of an address clause for a controlled object which we |
2778 | -- consider to be erroneous. | |
9dfe12ae | 2779 | |
83f8f0a6 | 2780 | elsif Is_Controlled (Etype (U_Ent)) |
2781 | or else Has_Controlled_Component (Etype (U_Ent)) | |
2782 | then | |
9dfe12ae | 2783 | Error_Msg_NE |
1e3532e7 | 2784 | ("??controlled object& must not be overlaid", Nam, U_Ent); |
9dfe12ae | 2785 | Error_Msg_N |
1e3532e7 | 2786 | ("\??Program_Error will be raised at run time", Nam); |
9dfe12ae | 2787 | Insert_Action (Declaration_Node (U_Ent), |
2788 | Make_Raise_Program_Error (Loc, | |
2789 | Reason => PE_Overlaid_Controlled_Object)); | |
83f8f0a6 | 2790 | return; |
9dfe12ae | 2791 | |
2792 | -- Case of address clause for a (non-controlled) object | |
d6f39728 | 2793 | |
2794 | elsif | |
2795 | Ekind (U_Ent) = E_Variable | |
2796 | or else | |
2797 | Ekind (U_Ent) = E_Constant | |
2798 | then | |
2799 | declare | |
d6da7448 | 2800 | Expr : constant Node_Id := Expression (N); |
2801 | O_Ent : Entity_Id; | |
2802 | Off : Boolean; | |
d6f39728 | 2803 | |
2804 | begin | |
7ee315cc | 2805 | -- Exported variables cannot have an address clause, because |
2806 | -- this cancels the effect of the pragma Export. | |
d6f39728 | 2807 | |
2808 | if Is_Exported (U_Ent) then | |
2809 | Error_Msg_N | |
2810 | ("cannot export object with address clause", Nam); | |
83f8f0a6 | 2811 | return; |
d6da7448 | 2812 | end if; |
2813 | ||
2814 | Find_Overlaid_Entity (N, O_Ent, Off); | |
d6f39728 | 2815 | |
9dfe12ae | 2816 | -- Overlaying controlled objects is erroneous |
2817 | ||
d6da7448 | 2818 | if Present (O_Ent) |
2819 | and then (Has_Controlled_Component (Etype (O_Ent)) | |
2820 | or else Is_Controlled (Etype (O_Ent))) | |
9dfe12ae | 2821 | then |
2822 | Error_Msg_N | |
1e3532e7 | 2823 | ("??cannot overlay with controlled object", Expr); |
9dfe12ae | 2824 | Error_Msg_N |
1e3532e7 | 2825 | ("\??Program_Error will be raised at run time", Expr); |
9dfe12ae | 2826 | Insert_Action (Declaration_Node (U_Ent), |
2827 | Make_Raise_Program_Error (Loc, | |
2828 | Reason => PE_Overlaid_Controlled_Object)); | |
83f8f0a6 | 2829 | return; |
9dfe12ae | 2830 | |
d6da7448 | 2831 | elsif Present (O_Ent) |
9dfe12ae | 2832 | and then Ekind (U_Ent) = E_Constant |
d6da7448 | 2833 | and then not Is_Constant_Object (O_Ent) |
9dfe12ae | 2834 | then |
1e3532e7 | 2835 | Error_Msg_N ("??constant overlays a variable", Expr); |
9dfe12ae | 2836 | |
d6f39728 | 2837 | -- Imported variables can have an address clause, but then |
2838 | -- the import is pretty meaningless except to suppress | |
2839 | -- initializations, so we do not need such variables to | |
2840 | -- be statically allocated (and in fact it causes trouble | |
2841 | -- if the address clause is a local value). | |
2842 | ||
2843 | elsif Is_Imported (U_Ent) then | |
2844 | Set_Is_Statically_Allocated (U_Ent, False); | |
2845 | end if; | |
2846 | ||
2847 | -- We mark a possible modification of a variable with an | |
2848 | -- address clause, since it is likely aliasing is occurring. | |
2849 | ||
177675a7 | 2850 | Note_Possible_Modification (Nam, Sure => False); |
d6f39728 | 2851 | |
83f8f0a6 | 2852 | -- Here we are checking for explicit overlap of one variable |
2853 | -- by another, and if we find this then mark the overlapped | |
2854 | -- variable as also being volatile to prevent unwanted | |
d6da7448 | 2855 | -- optimizations. This is a significant pessimization so |
2856 | -- avoid it when there is an offset, i.e. when the object | |
2857 | -- is composite; they cannot be optimized easily anyway. | |
d6f39728 | 2858 | |
d6da7448 | 2859 | if Present (O_Ent) |
2860 | and then Is_Object (O_Ent) | |
2861 | and then not Off | |
ba5efa21 | 2862 | |
2863 | -- The following test is an expedient solution to what | |
2864 | -- is really a problem in CodePeer. Suppressing the | |
2865 | -- Set_Treat_As_Volatile call here prevents later | |
2866 | -- generation (in some cases) of trees that CodePeer | |
2867 | -- should, but currently does not, handle correctly. | |
2868 | -- This test should probably be removed when CodePeer | |
2869 | -- is improved, just because we want the tree CodePeer | |
2870 | -- analyzes to match the tree for which we generate code | |
2871 | -- as closely as is practical. ??? | |
2872 | ||
2873 | and then not CodePeer_Mode | |
d6da7448 | 2874 | then |
ba5efa21 | 2875 | -- ??? O_Ent might not be in current unit |
2876 | ||
d6da7448 | 2877 | Set_Treat_As_Volatile (O_Ent); |
d6f39728 | 2878 | end if; |
2879 | ||
9dfe12ae | 2880 | -- Legality checks on the address clause for initialized |
2881 | -- objects is deferred until the freeze point, because | |
2beb22b1 | 2882 | -- a subsequent pragma might indicate that the object |
2883 | -- is imported and thus not initialized. | |
9dfe12ae | 2884 | |
2885 | Set_Has_Delayed_Freeze (U_Ent); | |
2886 | ||
51ad5ad2 | 2887 | -- If an initialization call has been generated for this |
2888 | -- object, it needs to be deferred to after the freeze node | |
2889 | -- we have just now added, otherwise GIGI will see a | |
2890 | -- reference to the variable (as actual to the IP call) | |
2891 | -- before its definition. | |
2892 | ||
2893 | declare | |
2894 | Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N); | |
2895 | begin | |
2896 | if Present (Init_Call) then | |
2897 | Remove (Init_Call); | |
2898 | Append_Freeze_Action (U_Ent, Init_Call); | |
2899 | end if; | |
2900 | end; | |
2901 | ||
d6f39728 | 2902 | if Is_Exported (U_Ent) then |
2903 | Error_Msg_N | |
2904 | ("& cannot be exported if an address clause is given", | |
2905 | Nam); | |
2906 | Error_Msg_N | |
2907 | ("\define and export a variable " & | |
2908 | "that holds its address instead", | |
2909 | Nam); | |
2910 | end if; | |
2911 | ||
44e4341e | 2912 | -- Entity has delayed freeze, so we will generate an |
2913 | -- alignment check at the freeze point unless suppressed. | |
d6f39728 | 2914 | |
44e4341e | 2915 | if not Range_Checks_Suppressed (U_Ent) |
2916 | and then not Alignment_Checks_Suppressed (U_Ent) | |
2917 | then | |
2918 | Set_Check_Address_Alignment (N); | |
2919 | end if; | |
d6f39728 | 2920 | |
2921 | -- Kill the size check code, since we are not allocating | |
2922 | -- the variable, it is somewhere else. | |
2923 | ||
2924 | Kill_Size_Check_Code (U_Ent); | |
83f8f0a6 | 2925 | |
d6da7448 | 2926 | -- If the address clause is of the form: |
83f8f0a6 | 2927 | |
d6da7448 | 2928 | -- for Y'Address use X'Address |
83f8f0a6 | 2929 | |
d6da7448 | 2930 | -- or |
83f8f0a6 | 2931 | |
d6da7448 | 2932 | -- Const : constant Address := X'Address; |
2933 | -- ... | |
2934 | -- for Y'Address use Const; | |
83f8f0a6 | 2935 | |
d6da7448 | 2936 | -- then we make an entry in the table for checking the size |
2937 | -- and alignment of the overlaying variable. We defer this | |
2938 | -- check till after code generation to take full advantage | |
2939 | -- of the annotation done by the back end. This entry is | |
2940 | -- only made if the address clause comes from source. | |
d64221a7 | 2941 | |
9474aa9c | 2942 | -- If the entity has a generic type, the check will be |
43dd6937 | 2943 | -- performed in the instance if the actual type justifies |
2944 | -- it, and we do not insert the clause in the table to | |
2945 | -- prevent spurious warnings. | |
83f8f0a6 | 2946 | |
d6da7448 | 2947 | if Address_Clause_Overlay_Warnings |
2948 | and then Comes_From_Source (N) | |
2949 | and then Present (O_Ent) | |
2950 | and then Is_Object (O_Ent) | |
2951 | then | |
9474aa9c | 2952 | if not Is_Generic_Type (Etype (U_Ent)) then |
2953 | Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off)); | |
2954 | end if; | |
177675a7 | 2955 | |
d6da7448 | 2956 | -- If variable overlays a constant view, and we are |
2957 | -- warning on overlays, then mark the variable as | |
2958 | -- overlaying a constant (we will give warnings later | |
2959 | -- if this variable is assigned). | |
177675a7 | 2960 | |
d6da7448 | 2961 | if Is_Constant_Object (O_Ent) |
2962 | and then Ekind (U_Ent) = E_Variable | |
2963 | then | |
2964 | Set_Overlays_Constant (U_Ent); | |
83f8f0a6 | 2965 | end if; |
d6da7448 | 2966 | end if; |
2967 | end; | |
83f8f0a6 | 2968 | |
d6f39728 | 2969 | -- Not a valid entity for an address clause |
2970 | ||
2971 | else | |
2972 | Error_Msg_N ("address cannot be given for &", Nam); | |
2973 | end if; | |
2974 | end Address; | |
2975 | ||
2976 | --------------- | |
2977 | -- Alignment -- | |
2978 | --------------- | |
2979 | ||
2980 | -- Alignment attribute definition clause | |
2981 | ||
b47769f0 | 2982 | when Attribute_Alignment => Alignment : declare |
208fd589 | 2983 | Align : constant Uint := Get_Alignment_Value (Expr); |
2984 | Max_Align : constant Uint := UI_From_Int (Maximum_Alignment); | |
41331dcf | 2985 | |
d6f39728 | 2986 | begin |
2987 | FOnly := True; | |
2988 | ||
2989 | if not Is_Type (U_Ent) | |
2990 | and then Ekind (U_Ent) /= E_Variable | |
2991 | and then Ekind (U_Ent) /= E_Constant | |
2992 | then | |
2993 | Error_Msg_N ("alignment cannot be given for &", Nam); | |
2994 | ||
ae888dbd | 2995 | elsif Duplicate_Clause then |
2996 | null; | |
d6f39728 | 2997 | |
2998 | elsif Align /= No_Uint then | |
2999 | Set_Has_Alignment_Clause (U_Ent); | |
208fd589 | 3000 | |
44705307 | 3001 | -- Tagged type case, check for attempt to set alignment to a |
3002 | -- value greater than Max_Align, and reset if so. | |
3003 | ||
41331dcf | 3004 | if Is_Tagged_Type (U_Ent) and then Align > Max_Align then |
208fd589 | 3005 | Error_Msg_N |
1e3532e7 | 3006 | ("alignment for & set to Maximum_Aligment??", Nam); |
44705307 | 3007 | Set_Alignment (U_Ent, Max_Align); |
3008 | ||
3009 | -- All other cases | |
3010 | ||
208fd589 | 3011 | else |
3012 | Set_Alignment (U_Ent, Align); | |
3013 | end if; | |
b47769f0 | 3014 | |
3015 | -- For an array type, U_Ent is the first subtype. In that case, | |
3016 | -- also set the alignment of the anonymous base type so that | |
3017 | -- other subtypes (such as the itypes for aggregates of the | |
3018 | -- type) also receive the expected alignment. | |
3019 | ||
3020 | if Is_Array_Type (U_Ent) then | |
3021 | Set_Alignment (Base_Type (U_Ent), Align); | |
3022 | end if; | |
d6f39728 | 3023 | end if; |
b47769f0 | 3024 | end Alignment; |
d6f39728 | 3025 | |
3026 | --------------- | |
3027 | -- Bit_Order -- | |
3028 | --------------- | |
3029 | ||
3030 | -- Bit_Order attribute definition clause | |
3031 | ||
3032 | when Attribute_Bit_Order => Bit_Order : declare | |
3033 | begin | |
3034 | if not Is_Record_Type (U_Ent) then | |
3035 | Error_Msg_N | |
3036 | ("Bit_Order can only be defined for record type", Nam); | |
3037 | ||
ae888dbd | 3038 | elsif Duplicate_Clause then |
3039 | null; | |
3040 | ||
d6f39728 | 3041 | else |
3042 | Analyze_And_Resolve (Expr, RTE (RE_Bit_Order)); | |
3043 | ||
3044 | if Etype (Expr) = Any_Type then | |
3045 | return; | |
3046 | ||
3047 | elsif not Is_Static_Expression (Expr) then | |
9dfe12ae | 3048 | Flag_Non_Static_Expr |
3049 | ("Bit_Order requires static expression!", Expr); | |
d6f39728 | 3050 | |
3051 | else | |
3052 | if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then | |
3053 | Set_Reverse_Bit_Order (U_Ent, True); | |
3054 | end if; | |
3055 | end if; | |
3056 | end if; | |
3057 | end Bit_Order; | |
3058 | ||
3059 | -------------------- | |
3060 | -- Component_Size -- | |
3061 | -------------------- | |
3062 | ||
3063 | -- Component_Size attribute definition clause | |
3064 | ||
3065 | when Attribute_Component_Size => Component_Size_Case : declare | |
3066 | Csize : constant Uint := Static_Integer (Expr); | |
a0fc8c5b | 3067 | Ctyp : Entity_Id; |
d6f39728 | 3068 | Btype : Entity_Id; |
3069 | Biased : Boolean; | |
3070 | New_Ctyp : Entity_Id; | |
3071 | Decl : Node_Id; | |
3072 | ||
3073 | begin | |
3074 | if not Is_Array_Type (U_Ent) then | |
3075 | Error_Msg_N ("component size requires array type", Nam); | |
3076 | return; | |
3077 | end if; | |
3078 | ||
3079 | Btype := Base_Type (U_Ent); | |
a0fc8c5b | 3080 | Ctyp := Component_Type (Btype); |
d6f39728 | 3081 | |
ae888dbd | 3082 | if Duplicate_Clause then |
3083 | null; | |
d6f39728 | 3084 | |
f3e4db96 | 3085 | elsif Rep_Item_Too_Early (Btype, N) then |
3086 | null; | |
3087 | ||
d6f39728 | 3088 | elsif Csize /= No_Uint then |
a0fc8c5b | 3089 | Check_Size (Expr, Ctyp, Csize, Biased); |
d6f39728 | 3090 | |
d74fc39a | 3091 | -- For the biased case, build a declaration for a subtype that |
3092 | -- will be used to represent the biased subtype that reflects | |
3093 | -- the biased representation of components. We need the subtype | |
3094 | -- to get proper conversions on referencing elements of the | |
3095 | -- array. Note: component size clauses are ignored in VM mode. | |
3062c401 | 3096 | |
3097 | if VM_Target = No_VM then | |
3098 | if Biased then | |
3099 | New_Ctyp := | |
3100 | Make_Defining_Identifier (Loc, | |
3101 | Chars => | |
3102 | New_External_Name (Chars (U_Ent), 'C', 0, 'T')); | |
3103 | ||
3104 | Decl := | |
3105 | Make_Subtype_Declaration (Loc, | |
3106 | Defining_Identifier => New_Ctyp, | |
3107 | Subtype_Indication => | |
3108 | New_Occurrence_Of (Component_Type (Btype), Loc)); | |
3109 | ||
3110 | Set_Parent (Decl, N); | |
3111 | Analyze (Decl, Suppress => All_Checks); | |
3112 | ||
3113 | Set_Has_Delayed_Freeze (New_Ctyp, False); | |
3114 | Set_Esize (New_Ctyp, Csize); | |
3115 | Set_RM_Size (New_Ctyp, Csize); | |
3116 | Init_Alignment (New_Ctyp); | |
3062c401 | 3117 | Set_Is_Itype (New_Ctyp, True); |
3118 | Set_Associated_Node_For_Itype (New_Ctyp, U_Ent); | |
3119 | ||
3120 | Set_Component_Type (Btype, New_Ctyp); | |
b77e4501 | 3121 | Set_Biased (New_Ctyp, N, "component size clause"); |
3062c401 | 3122 | end if; |
3123 | ||
3124 | Set_Component_Size (Btype, Csize); | |
3125 | ||
3126 | -- For VM case, we ignore component size clauses | |
3127 | ||
3128 | else | |
3129 | -- Give a warning unless we are in GNAT mode, in which case | |
3130 | -- the warning is suppressed since it is not useful. | |
3131 | ||
3132 | if not GNAT_Mode then | |
3133 | Error_Msg_N | |
1e3532e7 | 3134 | ("component size ignored in this configuration??", N); |
3062c401 | 3135 | end if; |
d6f39728 | 3136 | end if; |
3137 | ||
a0fc8c5b | 3138 | -- Deal with warning on overridden size |
3139 | ||
3140 | if Warn_On_Overridden_Size | |
3141 | and then Has_Size_Clause (Ctyp) | |
3142 | and then RM_Size (Ctyp) /= Csize | |
3143 | then | |
3144 | Error_Msg_NE | |
1e3532e7 | 3145 | ("component size overrides size clause for&?S?", N, Ctyp); |
a0fc8c5b | 3146 | end if; |
3147 | ||
d6f39728 | 3148 | Set_Has_Component_Size_Clause (Btype, True); |
f3e4db96 | 3149 | Set_Has_Non_Standard_Rep (Btype, True); |
d6f39728 | 3150 | end if; |
3151 | end Component_Size_Case; | |
3152 | ||
81b424ac | 3153 | ----------------------- |
3154 | -- Constant_Indexing -- | |
3155 | ----------------------- | |
3156 | ||
3157 | when Attribute_Constant_Indexing => | |
3158 | Check_Indexing_Functions; | |
3159 | ||
89f1e35c | 3160 | --------- |
3161 | -- CPU -- | |
3162 | --------- | |
3163 | ||
3164 | when Attribute_CPU => CPU : | |
3165 | begin | |
3166 | -- CPU attribute definition clause not allowed except from aspect | |
3167 | -- specification. | |
3168 | ||
3169 | if From_Aspect_Specification (N) then | |
3170 | if not Is_Task_Type (U_Ent) then | |
3171 | Error_Msg_N ("CPU can only be defined for task", Nam); | |
3172 | ||
3173 | elsif Duplicate_Clause then | |
3174 | null; | |
3175 | ||
3176 | else | |
3177 | -- The expression must be analyzed in the special manner | |
3178 | -- described in "Handling of Default and Per-Object | |
3179 | -- Expressions" in sem.ads. | |
3180 | ||
3181 | -- The visibility to the discriminants must be restored | |
3182 | ||
3183 | Push_Scope_And_Install_Discriminants (U_Ent); | |
3184 | Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range)); | |
3185 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
3186 | ||
3187 | if not Is_Static_Expression (Expr) then | |
3188 | Check_Restriction (Static_Priorities, Expr); | |
3189 | end if; | |
3190 | end if; | |
3191 | ||
3192 | else | |
3193 | Error_Msg_N | |
3194 | ("attribute& cannot be set with definition clause", N); | |
3195 | end if; | |
3196 | end CPU; | |
3197 | ||
89cc7147 | 3198 | ---------------------- |
3199 | -- Default_Iterator -- | |
3200 | ---------------------- | |
3201 | ||
3202 | when Attribute_Default_Iterator => Default_Iterator : declare | |
3203 | Func : Entity_Id; | |
3204 | ||
3205 | begin | |
3206 | if not Is_Tagged_Type (U_Ent) then | |
3207 | Error_Msg_N | |
3208 | ("aspect Default_Iterator applies to tagged type", Nam); | |
3209 | end if; | |
3210 | ||
3211 | Check_Iterator_Functions; | |
3212 | ||
3213 | Analyze (Expr); | |
3214 | ||
3215 | if not Is_Entity_Name (Expr) | |
3216 | or else Ekind (Entity (Expr)) /= E_Function | |
3217 | then | |
3218 | Error_Msg_N ("aspect Iterator must be a function", Expr); | |
3219 | else | |
3220 | Func := Entity (Expr); | |
3221 | end if; | |
3222 | ||
3223 | if No (First_Formal (Func)) | |
3224 | or else Etype (First_Formal (Func)) /= U_Ent | |
3225 | then | |
3226 | Error_Msg_NE | |
3227 | ("Default Iterator must be a primitive of&", Func, U_Ent); | |
3228 | end if; | |
3229 | end Default_Iterator; | |
3230 | ||
89f1e35c | 3231 | ------------------------ |
3232 | -- Dispatching_Domain -- | |
3233 | ------------------------ | |
3234 | ||
3235 | when Attribute_Dispatching_Domain => Dispatching_Domain : | |
3236 | begin | |
3237 | -- Dispatching_Domain attribute definition clause not allowed | |
3238 | -- except from aspect specification. | |
3239 | ||
3240 | if From_Aspect_Specification (N) then | |
3241 | if not Is_Task_Type (U_Ent) then | |
3242 | Error_Msg_N ("Dispatching_Domain can only be defined" & | |
3243 | "for task", | |
3244 | Nam); | |
3245 | ||
3246 | elsif Duplicate_Clause then | |
3247 | null; | |
3248 | ||
3249 | else | |
3250 | -- The expression must be analyzed in the special manner | |
3251 | -- described in "Handling of Default and Per-Object | |
3252 | -- Expressions" in sem.ads. | |
3253 | ||
3254 | -- The visibility to the discriminants must be restored | |
3255 | ||
3256 | Push_Scope_And_Install_Discriminants (U_Ent); | |
3257 | ||
3258 | Preanalyze_Spec_Expression | |
3259 | (Expr, RTE (RE_Dispatching_Domain)); | |
3260 | ||
3261 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
3262 | end if; | |
3263 | ||
3264 | else | |
3265 | Error_Msg_N | |
3266 | ("attribute& cannot be set with definition clause", N); | |
3267 | end if; | |
3268 | end Dispatching_Domain; | |
3269 | ||
d6f39728 | 3270 | ------------------ |
3271 | -- External_Tag -- | |
3272 | ------------------ | |
3273 | ||
3274 | when Attribute_External_Tag => External_Tag : | |
3275 | begin | |
3276 | if not Is_Tagged_Type (U_Ent) then | |
3277 | Error_Msg_N ("should be a tagged type", Nam); | |
3278 | end if; | |
3279 | ||
ae888dbd | 3280 | if Duplicate_Clause then |
3281 | null; | |
d6f39728 | 3282 | |
9af0ddc7 | 3283 | else |
ae888dbd | 3284 | Analyze_And_Resolve (Expr, Standard_String); |
fbc67f84 | 3285 | |
ae888dbd | 3286 | if not Is_Static_Expression (Expr) then |
3287 | Flag_Non_Static_Expr | |
3288 | ("static string required for tag name!", Nam); | |
3289 | end if; | |
3290 | ||
3291 | if VM_Target = No_VM then | |
3292 | Set_Has_External_Tag_Rep_Clause (U_Ent); | |
3293 | else | |
3294 | Error_Msg_Name_1 := Attr; | |
3295 | Error_Msg_N | |
3296 | ("% attribute unsupported in this configuration", Nam); | |
3297 | end if; | |
3298 | ||
3299 | if not Is_Library_Level_Entity (U_Ent) then | |
3300 | Error_Msg_NE | |
1e3532e7 | 3301 | ("??non-unique external tag supplied for &", N, U_Ent); |
ae888dbd | 3302 | Error_Msg_N |
1e3532e7 | 3303 | ("\??same external tag applies to all " |
3304 | & "subprogram calls", N); | |
ae888dbd | 3305 | Error_Msg_N |
1e3532e7 | 3306 | ("\??corresponding internal tag cannot be obtained", N); |
ae888dbd | 3307 | end if; |
fbc67f84 | 3308 | end if; |
d6f39728 | 3309 | end External_Tag; |
3310 | ||
b57530b8 | 3311 | -------------------------- |
3312 | -- Implicit_Dereference -- | |
3313 | -------------------------- | |
7947a439 | 3314 | |
b57530b8 | 3315 | when Attribute_Implicit_Dereference => |
7947a439 | 3316 | |
2beb22b1 | 3317 | -- Legality checks already performed at the point of the type |
3318 | -- declaration, aspect is not delayed. | |
7947a439 | 3319 | |
89cc7147 | 3320 | null; |
b57530b8 | 3321 | |
d6f39728 | 3322 | ----------- |
3323 | -- Input -- | |
3324 | ----------- | |
3325 | ||
9f373bb8 | 3326 | when Attribute_Input => |
3327 | Analyze_Stream_TSS_Definition (TSS_Stream_Input); | |
3328 | Set_Has_Specified_Stream_Input (Ent); | |
d6f39728 | 3329 | |
89f1e35c | 3330 | ------------------------ |
3331 | -- Interrupt_Priority -- | |
3332 | ------------------------ | |
3333 | ||
3334 | when Attribute_Interrupt_Priority => Interrupt_Priority : | |
3335 | begin | |
3336 | -- Interrupt_Priority attribute definition clause not allowed | |
3337 | -- except from aspect specification. | |
3338 | ||
3339 | if From_Aspect_Specification (N) then | |
3340 | if not (Is_Protected_Type (U_Ent) | |
3341 | or else Is_Task_Type (U_Ent)) | |
3342 | then | |
3343 | Error_Msg_N | |
3344 | ("Interrupt_Priority can only be defined for task" & | |
3345 | "and protected object", | |
3346 | Nam); | |
3347 | ||
3348 | elsif Duplicate_Clause then | |
3349 | null; | |
3350 | ||
3351 | else | |
3352 | -- The expression must be analyzed in the special manner | |
3353 | -- described in "Handling of Default and Per-Object | |
3354 | -- Expressions" in sem.ads. | |
3355 | ||
3356 | -- The visibility to the discriminants must be restored | |
3357 | ||
3358 | Push_Scope_And_Install_Discriminants (U_Ent); | |
3359 | ||
3360 | Preanalyze_Spec_Expression | |
3361 | (Expr, RTE (RE_Interrupt_Priority)); | |
3362 | ||
3363 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
3364 | end if; | |
3365 | ||
3366 | else | |
3367 | Error_Msg_N | |
3368 | ("attribute& cannot be set with definition clause", N); | |
3369 | end if; | |
3370 | end Interrupt_Priority; | |
3371 | ||
89cc7147 | 3372 | ---------------------- |
3373 | -- Iterator_Element -- | |
3374 | ---------------------- | |
3375 | ||
3376 | when Attribute_Iterator_Element => | |
3377 | Analyze (Expr); | |
3378 | ||
3379 | if not Is_Entity_Name (Expr) | |
3380 | or else not Is_Type (Entity (Expr)) | |
3381 | then | |
3382 | Error_Msg_N ("aspect Iterator_Element must be a type", Expr); | |
3383 | end if; | |
3384 | ||
d6f39728 | 3385 | ------------------- |
3386 | -- Machine_Radix -- | |
3387 | ------------------- | |
3388 | ||
3389 | -- Machine radix attribute definition clause | |
3390 | ||
3391 | when Attribute_Machine_Radix => Machine_Radix : declare | |
3392 | Radix : constant Uint := Static_Integer (Expr); | |
3393 | ||
3394 | begin | |
3395 | if not Is_Decimal_Fixed_Point_Type (U_Ent) then | |
3396 | Error_Msg_N ("decimal fixed-point type expected for &", Nam); | |
3397 | ||
ae888dbd | 3398 | elsif Duplicate_Clause then |
3399 | null; | |
d6f39728 | 3400 | |
3401 | elsif Radix /= No_Uint then | |
3402 | Set_Has_Machine_Radix_Clause (U_Ent); | |
3403 | Set_Has_Non_Standard_Rep (Base_Type (U_Ent)); | |
3404 | ||
3405 | if Radix = 2 then | |
3406 | null; | |
3407 | elsif Radix = 10 then | |
3408 | Set_Machine_Radix_10 (U_Ent); | |
3409 | else | |
3410 | Error_Msg_N ("machine radix value must be 2 or 10", Expr); | |
3411 | end if; | |
3412 | end if; | |
3413 | end Machine_Radix; | |
3414 | ||
3415 | ----------------- | |
3416 | -- Object_Size -- | |
3417 | ----------------- | |
3418 | ||
3419 | -- Object_Size attribute definition clause | |
3420 | ||
3421 | when Attribute_Object_Size => Object_Size : declare | |
bfa5a9d9 | 3422 | Size : constant Uint := Static_Integer (Expr); |
3423 | ||
d6f39728 | 3424 | Biased : Boolean; |
bfa5a9d9 | 3425 | pragma Warnings (Off, Biased); |
d6f39728 | 3426 | |
3427 | begin | |
3428 | if not Is_Type (U_Ent) then | |
3429 | Error_Msg_N ("Object_Size cannot be given for &", Nam); | |
3430 | ||
ae888dbd | 3431 | elsif Duplicate_Clause then |
3432 | null; | |
d6f39728 | 3433 | |
3434 | else | |
3435 | Check_Size (Expr, U_Ent, Size, Biased); | |
3436 | ||
3437 | if Size /= 8 | |
3438 | and then | |
3439 | Size /= 16 | |
3440 | and then | |
3441 | Size /= 32 | |
3442 | and then | |
3443 | UI_Mod (Size, 64) /= 0 | |
3444 | then | |
3445 | Error_Msg_N | |
3446 | ("Object_Size must be 8, 16, 32, or multiple of 64", | |
3447 | Expr); | |
3448 | end if; | |
3449 | ||
3450 | Set_Esize (U_Ent, Size); | |
3451 | Set_Has_Object_Size_Clause (U_Ent); | |
1d366b32 | 3452 | Alignment_Check_For_Size_Change (U_Ent, Size); |
d6f39728 | 3453 | end if; |
3454 | end Object_Size; | |
3455 | ||
3456 | ------------ | |
3457 | -- Output -- | |
3458 | ------------ | |
3459 | ||
9f373bb8 | 3460 | when Attribute_Output => |
3461 | Analyze_Stream_TSS_Definition (TSS_Stream_Output); | |
3462 | Set_Has_Specified_Stream_Output (Ent); | |
d6f39728 | 3463 | |
89f1e35c | 3464 | -------------- |
3465 | -- Priority -- | |
3466 | -------------- | |
3467 | ||
3468 | when Attribute_Priority => Priority : | |
3469 | begin | |
3470 | -- Priority attribute definition clause not allowed except from | |
3471 | -- aspect specification. | |
3472 | ||
3473 | if From_Aspect_Specification (N) then | |
3474 | if not (Is_Protected_Type (U_Ent) | |
3a72f9c3 | 3475 | or else Is_Task_Type (U_Ent) |
3476 | or else Ekind (U_Ent) = E_Procedure) | |
89f1e35c | 3477 | then |
3478 | Error_Msg_N | |
3a72f9c3 | 3479 | ("Priority can only be defined for task and protected " & |
89f1e35c | 3480 | "object", |
3481 | Nam); | |
3482 | ||
3483 | elsif Duplicate_Clause then | |
3484 | null; | |
3485 | ||
3486 | else | |
3487 | -- The expression must be analyzed in the special manner | |
3488 | -- described in "Handling of Default and Per-Object | |
3489 | -- Expressions" in sem.ads. | |
3490 | ||
3491 | -- The visibility to the discriminants must be restored | |
3492 | ||
3493 | Push_Scope_And_Install_Discriminants (U_Ent); | |
3494 | Preanalyze_Spec_Expression (Expr, Standard_Integer); | |
3495 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
3496 | ||
3497 | if not Is_Static_Expression (Expr) then | |
3498 | Check_Restriction (Static_Priorities, Expr); | |
3499 | end if; | |
3500 | end if; | |
3501 | ||
3502 | else | |
3503 | Error_Msg_N | |
3504 | ("attribute& cannot be set with definition clause", N); | |
3505 | end if; | |
3506 | end Priority; | |
3507 | ||
d6f39728 | 3508 | ---------- |
3509 | -- Read -- | |
3510 | ---------- | |
3511 | ||
9f373bb8 | 3512 | when Attribute_Read => |
3513 | Analyze_Stream_TSS_Definition (TSS_Stream_Read); | |
3514 | Set_Has_Specified_Stream_Read (Ent); | |
d6f39728 | 3515 | |
b7b74740 | 3516 | -------------------------- |
3517 | -- Scalar_Storage_Order -- | |
3518 | -------------------------- | |
3519 | ||
3520 | -- Scalar_Storage_Order attribute definition clause | |
3521 | ||
3522 | when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare | |
3523 | begin | |
b43a5770 | 3524 | if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then |
b7b74740 | 3525 | Error_Msg_N |
b43a5770 | 3526 | ("Scalar_Storage_Order can only be defined for " |
3527 | & "record or array type", Nam); | |
b7b74740 | 3528 | |
3529 | elsif Duplicate_Clause then | |
3530 | null; | |
3531 | ||
3532 | else | |
3533 | Analyze_And_Resolve (Expr, RTE (RE_Bit_Order)); | |
3534 | ||
3535 | if Etype (Expr) = Any_Type then | |
3536 | return; | |
3537 | ||
3538 | elsif not Is_Static_Expression (Expr) then | |
3539 | Flag_Non_Static_Expr | |
3540 | ("Scalar_Storage_Order requires static expression!", Expr); | |
3541 | ||
3542 | else | |
3543 | if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then | |
d0a9ea3b | 3544 | Set_Reverse_Storage_Order (Base_Type (U_Ent), True); |
b7b74740 | 3545 | end if; |
3546 | end if; | |
3547 | end if; | |
3548 | end Scalar_Storage_Order; | |
3549 | ||
d6f39728 | 3550 | ---------- |
3551 | -- Size -- | |
3552 | ---------- | |
3553 | ||
3554 | -- Size attribute definition clause | |
3555 | ||
3556 | when Attribute_Size => Size : declare | |
3557 | Size : constant Uint := Static_Integer (Expr); | |
3558 | Etyp : Entity_Id; | |
3559 | Biased : Boolean; | |
3560 | ||
3561 | begin | |
3562 | FOnly := True; | |
3563 | ||
ae888dbd | 3564 | if Duplicate_Clause then |
3565 | null; | |
d6f39728 | 3566 | |
3567 | elsif not Is_Type (U_Ent) | |
3568 | and then Ekind (U_Ent) /= E_Variable | |
3569 | and then Ekind (U_Ent) /= E_Constant | |
3570 | then | |
3571 | Error_Msg_N ("size cannot be given for &", Nam); | |
3572 | ||
3573 | elsif Is_Array_Type (U_Ent) | |
3574 | and then not Is_Constrained (U_Ent) | |
3575 | then | |
3576 | Error_Msg_N | |
3577 | ("size cannot be given for unconstrained array", Nam); | |
3578 | ||
c2b89d6e | 3579 | elsif Size /= No_Uint then |
c2b89d6e | 3580 | if VM_Target /= No_VM and then not GNAT_Mode then |
47495553 | 3581 | |
c2b89d6e | 3582 | -- Size clause is not handled properly on VM targets. |
3583 | -- Display a warning unless we are in GNAT mode, in which | |
3584 | -- case this is useless. | |
47495553 | 3585 | |
682fa897 | 3586 | Error_Msg_N |
1e3532e7 | 3587 | ("size clauses are ignored in this configuration??", N); |
682fa897 | 3588 | end if; |
3589 | ||
d6f39728 | 3590 | if Is_Type (U_Ent) then |
3591 | Etyp := U_Ent; | |
3592 | else | |
3593 | Etyp := Etype (U_Ent); | |
3594 | end if; | |
3595 | ||
59ac57b5 | 3596 | -- Check size, note that Gigi is in charge of checking that the |
3597 | -- size of an array or record type is OK. Also we do not check | |
3598 | -- the size in the ordinary fixed-point case, since it is too | |
3599 | -- early to do so (there may be subsequent small clause that | |
3600 | -- affects the size). We can check the size if a small clause | |
3601 | -- has already been given. | |
d6f39728 | 3602 | |
3603 | if not Is_Ordinary_Fixed_Point_Type (U_Ent) | |
3604 | or else Has_Small_Clause (U_Ent) | |
3605 | then | |
3606 | Check_Size (Expr, Etyp, Size, Biased); | |
b77e4501 | 3607 | Set_Biased (U_Ent, N, "size clause", Biased); |
d6f39728 | 3608 | end if; |
3609 | ||
3610 | -- For types set RM_Size and Esize if possible | |
3611 | ||
3612 | if Is_Type (U_Ent) then | |
3613 | Set_RM_Size (U_Ent, Size); | |
3614 | ||
ada34def | 3615 | -- For elementary types, increase Object_Size to power of 2, |
3616 | -- but not less than a storage unit in any case (normally | |
59ac57b5 | 3617 | -- this means it will be byte addressable). |
d6f39728 | 3618 | |
ada34def | 3619 | -- For all other types, nothing else to do, we leave Esize |
3620 | -- (object size) unset, the back end will set it from the | |
3621 | -- size and alignment in an appropriate manner. | |
3622 | ||
1d366b32 | 3623 | -- In both cases, we check whether the alignment must be |
3624 | -- reset in the wake of the size change. | |
3625 | ||
ada34def | 3626 | if Is_Elementary_Type (U_Ent) then |
f15731c4 | 3627 | if Size <= System_Storage_Unit then |
3628 | Init_Esize (U_Ent, System_Storage_Unit); | |
d6f39728 | 3629 | elsif Size <= 16 then |
3630 | Init_Esize (U_Ent, 16); | |
3631 | elsif Size <= 32 then | |
3632 | Init_Esize (U_Ent, 32); | |
3633 | else | |
3634 | Set_Esize (U_Ent, (Size + 63) / 64 * 64); | |
3635 | end if; | |
3636 | ||
1d366b32 | 3637 | Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent)); |
3638 | else | |
3639 | Alignment_Check_For_Size_Change (U_Ent, Size); | |
d6f39728 | 3640 | end if; |
3641 | ||
d6f39728 | 3642 | -- For objects, set Esize only |
3643 | ||
3644 | else | |
9dfe12ae | 3645 | if Is_Elementary_Type (Etyp) then |
3646 | if Size /= System_Storage_Unit | |
3647 | and then | |
3648 | Size /= System_Storage_Unit * 2 | |
3649 | and then | |
3650 | Size /= System_Storage_Unit * 4 | |
3651 | and then | |
3652 | Size /= System_Storage_Unit * 8 | |
3653 | then | |
5c99c290 | 3654 | Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit); |
87d5c1d0 | 3655 | Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8; |
9dfe12ae | 3656 | Error_Msg_N |
5c99c290 | 3657 | ("size for primitive object must be a power of 2" |
87d5c1d0 | 3658 | & " in the range ^-^", N); |
9dfe12ae | 3659 | end if; |
3660 | end if; | |
3661 | ||
d6f39728 | 3662 | Set_Esize (U_Ent, Size); |
3663 | end if; | |
3664 | ||
3665 | Set_Has_Size_Clause (U_Ent); | |
3666 | end if; | |
3667 | end Size; | |
3668 | ||
3669 | ----------- | |
3670 | -- Small -- | |
3671 | ----------- | |
3672 | ||
3673 | -- Small attribute definition clause | |
3674 | ||
3675 | when Attribute_Small => Small : declare | |
3676 | Implicit_Base : constant Entity_Id := Base_Type (U_Ent); | |
3677 | Small : Ureal; | |
3678 | ||
3679 | begin | |
3680 | Analyze_And_Resolve (Expr, Any_Real); | |
3681 | ||
3682 | if Etype (Expr) = Any_Type then | |
3683 | return; | |
3684 | ||
3685 | elsif not Is_Static_Expression (Expr) then | |
9dfe12ae | 3686 | Flag_Non_Static_Expr |
3687 | ("small requires static expression!", Expr); | |
d6f39728 | 3688 | return; |
3689 | ||
3690 | else | |
3691 | Small := Expr_Value_R (Expr); | |
3692 | ||
3693 | if Small <= Ureal_0 then | |
3694 | Error_Msg_N ("small value must be greater than zero", Expr); | |
3695 | return; | |
3696 | end if; | |
3697 | ||
3698 | end if; | |
3699 | ||
3700 | if not Is_Ordinary_Fixed_Point_Type (U_Ent) then | |
3701 | Error_Msg_N | |
3702 | ("small requires an ordinary fixed point type", Nam); | |
3703 | ||
3704 | elsif Has_Small_Clause (U_Ent) then | |
3705 | Error_Msg_N ("small already given for &", Nam); | |
3706 | ||
3707 | elsif Small > Delta_Value (U_Ent) then | |
3708 | Error_Msg_N | |
ce3e25d6 | 3709 | ("small value must not be greater than delta value", Nam); |
d6f39728 | 3710 | |
3711 | else | |
3712 | Set_Small_Value (U_Ent, Small); | |
3713 | Set_Small_Value (Implicit_Base, Small); | |
3714 | Set_Has_Small_Clause (U_Ent); | |
3715 | Set_Has_Small_Clause (Implicit_Base); | |
3716 | Set_Has_Non_Standard_Rep (Implicit_Base); | |
3717 | end if; | |
3718 | end Small; | |
3719 | ||
d6f39728 | 3720 | ------------------ |
3721 | -- Storage_Pool -- | |
3722 | ------------------ | |
3723 | ||
3724 | -- Storage_Pool attribute definition clause | |
3725 | ||
b55f7641 | 3726 | when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare |
d6f39728 | 3727 | Pool : Entity_Id; |
6b567c71 | 3728 | T : Entity_Id; |
d6f39728 | 3729 | |
3730 | begin | |
44e4341e | 3731 | if Ekind (U_Ent) = E_Access_Subprogram_Type then |
3732 | Error_Msg_N | |
3733 | ("storage pool cannot be given for access-to-subprogram type", | |
3734 | Nam); | |
3735 | return; | |
3736 | ||
d3ef794c | 3737 | elsif not |
3738 | Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type) | |
d6f39728 | 3739 | then |
44e4341e | 3740 | Error_Msg_N |
3741 | ("storage pool can only be given for access types", Nam); | |
d6f39728 | 3742 | return; |
3743 | ||
3744 | elsif Is_Derived_Type (U_Ent) then | |
3745 | Error_Msg_N | |
3746 | ("storage pool cannot be given for a derived access type", | |
3747 | Nam); | |
3748 | ||
ae888dbd | 3749 | elsif Duplicate_Clause then |
d6f39728 | 3750 | return; |
3751 | ||
3752 | elsif Present (Associated_Storage_Pool (U_Ent)) then | |
3753 | Error_Msg_N ("storage pool already given for &", Nam); | |
3754 | return; | |
3755 | end if; | |
3756 | ||
b55f7641 | 3757 | if Id = Attribute_Storage_Pool then |
3758 | Analyze_And_Resolve | |
3759 | (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool))); | |
3760 | ||
3761 | -- In the Simple_Storage_Pool case, we allow a variable of any | |
b15003c3 | 3762 | -- simple storage pool type, so we Resolve without imposing an |
b55f7641 | 3763 | -- expected type. |
3764 | ||
3765 | else | |
3766 | Analyze_And_Resolve (Expr); | |
3767 | ||
3768 | if not Present (Get_Rep_Pragma | |
b15003c3 | 3769 | (Etype (Expr), Name_Simple_Storage_Pool_Type)) |
b55f7641 | 3770 | then |
3771 | Error_Msg_N | |
3772 | ("expression must be of a simple storage pool type", Expr); | |
3773 | end if; | |
3774 | end if; | |
d6f39728 | 3775 | |
8c5c7277 | 3776 | if not Denotes_Variable (Expr) then |
3777 | Error_Msg_N ("storage pool must be a variable", Expr); | |
3778 | return; | |
3779 | end if; | |
3780 | ||
6b567c71 | 3781 | if Nkind (Expr) = N_Type_Conversion then |
3782 | T := Etype (Expression (Expr)); | |
3783 | else | |
3784 | T := Etype (Expr); | |
3785 | end if; | |
3786 | ||
3787 | -- The Stack_Bounded_Pool is used internally for implementing | |
d64221a7 | 3788 | -- access types with a Storage_Size. Since it only work properly |
3789 | -- when used on one specific type, we need to check that it is not | |
3790 | -- hijacked improperly: | |
3791 | ||
6b567c71 | 3792 | -- type T is access Integer; |
3793 | -- for T'Storage_Size use n; | |
3794 | -- type Q is access Float; | |
3795 | -- for Q'Storage_Size use T'Storage_Size; -- incorrect | |
3796 | ||
15ebb600 | 3797 | if RTE_Available (RE_Stack_Bounded_Pool) |
3798 | and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool) | |
3799 | then | |
3800 | Error_Msg_N ("non-shareable internal Pool", Expr); | |
6b567c71 | 3801 | return; |
3802 | end if; | |
3803 | ||
d6f39728 | 3804 | -- If the argument is a name that is not an entity name, then |
3805 | -- we construct a renaming operation to define an entity of | |
3806 | -- type storage pool. | |
3807 | ||
3808 | if not Is_Entity_Name (Expr) | |
3809 | and then Is_Object_Reference (Expr) | |
3810 | then | |
11deeeb6 | 3811 | Pool := Make_Temporary (Loc, 'P', Expr); |
d6f39728 | 3812 | |
3813 | declare | |
3814 | Rnode : constant Node_Id := | |
3815 | Make_Object_Renaming_Declaration (Loc, | |
3816 | Defining_Identifier => Pool, | |
3817 | Subtype_Mark => | |
3818 | New_Occurrence_Of (Etype (Expr), Loc), | |
11deeeb6 | 3819 | Name => Expr); |
d6f39728 | 3820 | |
3821 | begin | |
3822 | Insert_Before (N, Rnode); | |
3823 | Analyze (Rnode); | |
3824 | Set_Associated_Storage_Pool (U_Ent, Pool); | |
3825 | end; | |
3826 | ||
3827 | elsif Is_Entity_Name (Expr) then | |
3828 | Pool := Entity (Expr); | |
3829 | ||
3830 | -- If pool is a renamed object, get original one. This can | |
3831 | -- happen with an explicit renaming, and within instances. | |
3832 | ||
3833 | while Present (Renamed_Object (Pool)) | |
3834 | and then Is_Entity_Name (Renamed_Object (Pool)) | |
3835 | loop | |
3836 | Pool := Entity (Renamed_Object (Pool)); | |
3837 | end loop; | |
3838 | ||
3839 | if Present (Renamed_Object (Pool)) | |
3840 | and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion | |
3841 | and then Is_Entity_Name (Expression (Renamed_Object (Pool))) | |
3842 | then | |
3843 | Pool := Entity (Expression (Renamed_Object (Pool))); | |
3844 | end if; | |
3845 | ||
6b567c71 | 3846 | Set_Associated_Storage_Pool (U_Ent, Pool); |
d6f39728 | 3847 | |
3848 | elsif Nkind (Expr) = N_Type_Conversion | |
3849 | and then Is_Entity_Name (Expression (Expr)) | |
3850 | and then Nkind (Original_Node (Expr)) = N_Attribute_Reference | |
3851 | then | |
3852 | Pool := Entity (Expression (Expr)); | |
6b567c71 | 3853 | Set_Associated_Storage_Pool (U_Ent, Pool); |
d6f39728 | 3854 | |
3855 | else | |
3856 | Error_Msg_N ("incorrect reference to a Storage Pool", Expr); | |
3857 | return; | |
3858 | end if; | |
b55f7641 | 3859 | end; |
d6f39728 | 3860 | |
44e4341e | 3861 | ------------------ |
3862 | -- Storage_Size -- | |
3863 | ------------------ | |
3864 | ||
3865 | -- Storage_Size attribute definition clause | |
3866 | ||
3867 | when Attribute_Storage_Size => Storage_Size : declare | |
3868 | Btype : constant Entity_Id := Base_Type (U_Ent); | |
44e4341e | 3869 | |
3870 | begin | |
3871 | if Is_Task_Type (U_Ent) then | |
3872 | Check_Restriction (No_Obsolescent_Features, N); | |
3873 | ||
3874 | if Warn_On_Obsolescent_Feature then | |
3875 | Error_Msg_N | |
1e3532e7 | 3876 | ("?j?storage size clause for task is an " & |
3877 | "obsolescent feature (RM J.9)", N); | |
3878 | Error_Msg_N ("\?j?use Storage_Size pragma instead", N); | |
44e4341e | 3879 | end if; |
3880 | ||
3881 | FOnly := True; | |
3882 | end if; | |
3883 | ||
3884 | if not Is_Access_Type (U_Ent) | |
3885 | and then Ekind (U_Ent) /= E_Task_Type | |
3886 | then | |
3887 | Error_Msg_N ("storage size cannot be given for &", Nam); | |
3888 | ||
3889 | elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then | |
3890 | Error_Msg_N | |
3891 | ("storage size cannot be given for a derived access type", | |
3892 | Nam); | |
3893 | ||
ae888dbd | 3894 | elsif Duplicate_Clause then |
3895 | null; | |
44e4341e | 3896 | |
3897 | else | |
3898 | Analyze_And_Resolve (Expr, Any_Integer); | |
3899 | ||
3900 | if Is_Access_Type (U_Ent) then | |
3901 | if Present (Associated_Storage_Pool (U_Ent)) then | |
3902 | Error_Msg_N ("storage pool already given for &", Nam); | |
3903 | return; | |
3904 | end if; | |
3905 | ||
5941a4e9 | 3906 | if Is_OK_Static_Expression (Expr) |
44e4341e | 3907 | and then Expr_Value (Expr) = 0 |
3908 | then | |
3909 | Set_No_Pool_Assigned (Btype); | |
3910 | end if; | |
44e4341e | 3911 | end if; |
3912 | ||
3913 | Set_Has_Storage_Size_Clause (Btype); | |
3914 | end if; | |
3915 | end Storage_Size; | |
3916 | ||
7189d17f | 3917 | ----------------- |
3918 | -- Stream_Size -- | |
3919 | ----------------- | |
3920 | ||
3921 | when Attribute_Stream_Size => Stream_Size : declare | |
3922 | Size : constant Uint := Static_Integer (Expr); | |
3923 | ||
3924 | begin | |
15ebb600 | 3925 | if Ada_Version <= Ada_95 then |
3926 | Check_Restriction (No_Implementation_Attributes, N); | |
3927 | end if; | |
3928 | ||
ae888dbd | 3929 | if Duplicate_Clause then |
3930 | null; | |
7189d17f | 3931 | |
3932 | elsif Is_Elementary_Type (U_Ent) then | |
3933 | if Size /= System_Storage_Unit | |
3934 | and then | |
3935 | Size /= System_Storage_Unit * 2 | |
3936 | and then | |
3937 | Size /= System_Storage_Unit * 4 | |
3938 | and then | |
3939 | Size /= System_Storage_Unit * 8 | |
3940 | then | |
3941 | Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit); | |
3942 | Error_Msg_N | |
3943 | ("stream size for elementary type must be a" | |
3944 | & " power of 2 and at least ^", N); | |
3945 | ||
3946 | elsif RM_Size (U_Ent) > Size then | |
3947 | Error_Msg_Uint_1 := RM_Size (U_Ent); | |
3948 | Error_Msg_N | |
3949 | ("stream size for elementary type must be a" | |
3950 | & " power of 2 and at least ^", N); | |
3951 | end if; | |
3952 | ||
3953 | Set_Has_Stream_Size_Clause (U_Ent); | |
3954 | ||
3955 | else | |
3956 | Error_Msg_N ("Stream_Size cannot be given for &", Nam); | |
3957 | end if; | |
3958 | end Stream_Size; | |
3959 | ||
d6f39728 | 3960 | ---------------- |
3961 | -- Value_Size -- | |
3962 | ---------------- | |
3963 | ||
3964 | -- Value_Size attribute definition clause | |
3965 | ||
3966 | when Attribute_Value_Size => Value_Size : declare | |
3967 | Size : constant Uint := Static_Integer (Expr); | |
3968 | Biased : Boolean; | |
3969 | ||
3970 | begin | |
3971 | if not Is_Type (U_Ent) then | |
3972 | Error_Msg_N ("Value_Size cannot be given for &", Nam); | |
3973 | ||
ae888dbd | 3974 | elsif Duplicate_Clause then |
3975 | null; | |
d6f39728 | 3976 | |
59ac57b5 | 3977 | elsif Is_Array_Type (U_Ent) |
3978 | and then not Is_Constrained (U_Ent) | |
3979 | then | |
3980 | Error_Msg_N | |
3981 | ("Value_Size cannot be given for unconstrained array", Nam); | |
3982 | ||
d6f39728 | 3983 | else |
3984 | if Is_Elementary_Type (U_Ent) then | |
3985 | Check_Size (Expr, U_Ent, Size, Biased); | |
b77e4501 | 3986 | Set_Biased (U_Ent, N, "value size clause", Biased); |
d6f39728 | 3987 | end if; |
3988 | ||
3989 | Set_RM_Size (U_Ent, Size); | |
3990 | end if; | |
3991 | end Value_Size; | |
3992 | ||
81b424ac | 3993 | ----------------------- |
3994 | -- Variable_Indexing -- | |
3995 | ----------------------- | |
3996 | ||
3997 | when Attribute_Variable_Indexing => | |
3998 | Check_Indexing_Functions; | |
3999 | ||
d6f39728 | 4000 | ----------- |
4001 | -- Write -- | |
4002 | ----------- | |
4003 | ||
9f373bb8 | 4004 | when Attribute_Write => |
4005 | Analyze_Stream_TSS_Definition (TSS_Stream_Write); | |
4006 | Set_Has_Specified_Stream_Write (Ent); | |
d6f39728 | 4007 | |
4008 | -- All other attributes cannot be set | |
4009 | ||
4010 | when others => | |
4011 | Error_Msg_N | |
4012 | ("attribute& cannot be set with definition clause", N); | |
d6f39728 | 4013 | end case; |
4014 | ||
d64221a7 | 4015 | -- The test for the type being frozen must be performed after any |
4016 | -- expression the clause has been analyzed since the expression itself | |
4017 | -- might cause freezing that makes the clause illegal. | |
d6f39728 | 4018 | |
4019 | if Rep_Item_Too_Late (U_Ent, N, FOnly) then | |
4020 | return; | |
4021 | end if; | |
4022 | end Analyze_Attribute_Definition_Clause; | |
4023 | ||
4024 | ---------------------------- | |
4025 | -- Analyze_Code_Statement -- | |
4026 | ---------------------------- | |
4027 | ||
4028 | procedure Analyze_Code_Statement (N : Node_Id) is | |
4029 | HSS : constant Node_Id := Parent (N); | |
4030 | SBody : constant Node_Id := Parent (HSS); | |
4031 | Subp : constant Entity_Id := Current_Scope; | |
4032 | Stmt : Node_Id; | |
4033 | Decl : Node_Id; | |
4034 | StmtO : Node_Id; | |
4035 | DeclO : Node_Id; | |
4036 | ||
4037 | begin | |
4038 | -- Analyze and check we get right type, note that this implements the | |
4039 | -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that | |
4040 | -- is the only way that Asm_Insn could possibly be visible. | |
4041 | ||
4042 | Analyze_And_Resolve (Expression (N)); | |
4043 | ||
4044 | if Etype (Expression (N)) = Any_Type then | |
4045 | return; | |
4046 | elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then | |
4047 | Error_Msg_N ("incorrect type for code statement", N); | |
4048 | return; | |
4049 | end if; | |
4050 | ||
44e4341e | 4051 | Check_Code_Statement (N); |
4052 | ||
d6f39728 | 4053 | -- Make sure we appear in the handled statement sequence of a |
4054 | -- subprogram (RM 13.8(3)). | |
4055 | ||
4056 | if Nkind (HSS) /= N_Handled_Sequence_Of_Statements | |
4057 | or else Nkind (SBody) /= N_Subprogram_Body | |
4058 | then | |
4059 | Error_Msg_N | |
4060 | ("code statement can only appear in body of subprogram", N); | |
4061 | return; | |
4062 | end if; | |
4063 | ||
4064 | -- Do remaining checks (RM 13.8(3)) if not already done | |
4065 | ||
4066 | if not Is_Machine_Code_Subprogram (Subp) then | |
4067 | Set_Is_Machine_Code_Subprogram (Subp); | |
4068 | ||
4069 | -- No exception handlers allowed | |
4070 | ||
4071 | if Present (Exception_Handlers (HSS)) then | |
4072 | Error_Msg_N | |
4073 | ("exception handlers not permitted in machine code subprogram", | |
4074 | First (Exception_Handlers (HSS))); | |
4075 | end if; | |
4076 | ||
4077 | -- No declarations other than use clauses and pragmas (we allow | |
4078 | -- certain internally generated declarations as well). | |
4079 | ||
4080 | Decl := First (Declarations (SBody)); | |
4081 | while Present (Decl) loop | |
4082 | DeclO := Original_Node (Decl); | |
4083 | if Comes_From_Source (DeclO) | |
fdd294d1 | 4084 | and not Nkind_In (DeclO, N_Pragma, |
4085 | N_Use_Package_Clause, | |
4086 | N_Use_Type_Clause, | |
4087 | N_Implicit_Label_Declaration) | |
d6f39728 | 4088 | then |
4089 | Error_Msg_N | |
4090 | ("this declaration not allowed in machine code subprogram", | |
4091 | DeclO); | |
4092 | end if; | |
4093 | ||
4094 | Next (Decl); | |
4095 | end loop; | |
4096 | ||
4097 | -- No statements other than code statements, pragmas, and labels. | |
4098 | -- Again we allow certain internally generated statements. | |
3ab42ff7 | 4099 | |
c3107527 | 4100 | -- In Ada 2012, qualified expressions are names, and the code |
4101 | -- statement is initially parsed as a procedure call. | |
d6f39728 | 4102 | |
4103 | Stmt := First (Statements (HSS)); | |
4104 | while Present (Stmt) loop | |
4105 | StmtO := Original_Node (Stmt); | |
c3107527 | 4106 | |
59f2fcab | 4107 | -- A procedure call transformed into a code statement is OK. |
4108 | ||
c3107527 | 4109 | if Ada_Version >= Ada_2012 |
4110 | and then Nkind (StmtO) = N_Procedure_Call_Statement | |
59f2fcab | 4111 | and then Nkind (Name (StmtO)) = N_Qualified_Expression |
c3107527 | 4112 | then |
4113 | null; | |
4114 | ||
4115 | elsif Comes_From_Source (StmtO) | |
fdd294d1 | 4116 | and then not Nkind_In (StmtO, N_Pragma, |
4117 | N_Label, | |
4118 | N_Code_Statement) | |
d6f39728 | 4119 | then |
4120 | Error_Msg_N | |
4121 | ("this statement is not allowed in machine code subprogram", | |
4122 | StmtO); | |
4123 | end if; | |
4124 | ||
4125 | Next (Stmt); | |
4126 | end loop; | |
4127 | end if; | |
d6f39728 | 4128 | end Analyze_Code_Statement; |
4129 | ||
4130 | ----------------------------------------------- | |
4131 | -- Analyze_Enumeration_Representation_Clause -- | |
4132 | ----------------------------------------------- | |
4133 | ||
4134 | procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is | |
4135 | Ident : constant Node_Id := Identifier (N); | |
4136 | Aggr : constant Node_Id := Array_Aggregate (N); | |
4137 | Enumtype : Entity_Id; | |
4138 | Elit : Entity_Id; | |
4139 | Expr : Node_Id; | |
4140 | Assoc : Node_Id; | |
4141 | Choice : Node_Id; | |
4142 | Val : Uint; | |
b3190af0 | 4143 | |
4144 | Err : Boolean := False; | |
098d3082 | 4145 | -- Set True to avoid cascade errors and crashes on incorrect source code |
d6f39728 | 4146 | |
e30c7d84 | 4147 | Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer)); |
4148 | Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer)); | |
4149 | -- Allowed range of universal integer (= allowed range of enum lit vals) | |
4150 | ||
d6f39728 | 4151 | Min : Uint; |
4152 | Max : Uint; | |
e30c7d84 | 4153 | -- Minimum and maximum values of entries |
4154 | ||
4155 | Max_Node : Node_Id; | |
4156 | -- Pointer to node for literal providing max value | |
d6f39728 | 4157 | |
4158 | begin | |
ca301e17 | 4159 | if Ignore_Rep_Clauses then |
fbc67f84 | 4160 | return; |
4161 | end if; | |
4162 | ||
d6f39728 | 4163 | -- First some basic error checks |
4164 | ||
4165 | Find_Type (Ident); | |
4166 | Enumtype := Entity (Ident); | |
4167 | ||
4168 | if Enumtype = Any_Type | |
4169 | or else Rep_Item_Too_Early (Enumtype, N) | |
4170 | then | |
4171 | return; | |
4172 | else | |
4173 | Enumtype := Underlying_Type (Enumtype); | |
4174 | end if; | |
4175 | ||
4176 | if not Is_Enumeration_Type (Enumtype) then | |
4177 | Error_Msg_NE | |
4178 | ("enumeration type required, found}", | |
4179 | Ident, First_Subtype (Enumtype)); | |
4180 | return; | |
4181 | end if; | |
4182 | ||
9dfe12ae | 4183 | -- Ignore rep clause on generic actual type. This will already have |
4184 | -- been flagged on the template as an error, and this is the safest | |
4185 | -- way to ensure we don't get a junk cascaded message in the instance. | |
4186 | ||
4187 | if Is_Generic_Actual_Type (Enumtype) then | |
4188 | return; | |
4189 | ||
4190 | -- Type must be in current scope | |
4191 | ||
4192 | elsif Scope (Enumtype) /= Current_Scope then | |
d6f39728 | 4193 | Error_Msg_N ("type must be declared in this scope", Ident); |
4194 | return; | |
4195 | ||
9dfe12ae | 4196 | -- Type must be a first subtype |
4197 | ||
d6f39728 | 4198 | elsif not Is_First_Subtype (Enumtype) then |
4199 | Error_Msg_N ("cannot give enumeration rep clause for subtype", N); | |
4200 | return; | |
4201 | ||
9dfe12ae | 4202 | -- Ignore duplicate rep clause |
4203 | ||
d6f39728 | 4204 | elsif Has_Enumeration_Rep_Clause (Enumtype) then |
4205 | Error_Msg_N ("duplicate enumeration rep clause ignored", N); | |
4206 | return; | |
4207 | ||
7189d17f | 4208 | -- Don't allow rep clause for standard [wide_[wide_]]character |
9dfe12ae | 4209 | |
177675a7 | 4210 | elsif Is_Standard_Character_Type (Enumtype) then |
d6f39728 | 4211 | Error_Msg_N ("enumeration rep clause not allowed for this type", N); |
9dfe12ae | 4212 | return; |
4213 | ||
d9125581 | 4214 | -- Check that the expression is a proper aggregate (no parentheses) |
4215 | ||
4216 | elsif Paren_Count (Aggr) /= 0 then | |
4217 | Error_Msg | |
4218 | ("extra parentheses surrounding aggregate not allowed", | |
4219 | First_Sloc (Aggr)); | |
4220 | return; | |
4221 | ||
9dfe12ae | 4222 | -- All tests passed, so set rep clause in place |
d6f39728 | 4223 | |
4224 | else | |
4225 | Set_Has_Enumeration_Rep_Clause (Enumtype); | |
4226 | Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype)); | |
4227 | end if; | |
4228 | ||
4229 | -- Now we process the aggregate. Note that we don't use the normal | |
4230 | -- aggregate code for this purpose, because we don't want any of the | |
4231 | -- normal expansion activities, and a number of special semantic | |
4232 | -- rules apply (including the component type being any integer type) | |
4233 | ||
d6f39728 | 4234 | Elit := First_Literal (Enumtype); |
4235 | ||
4236 | -- First the positional entries if any | |
4237 | ||
4238 | if Present (Expressions (Aggr)) then | |
4239 | Expr := First (Expressions (Aggr)); | |
4240 | while Present (Expr) loop | |
4241 | if No (Elit) then | |
4242 | Error_Msg_N ("too many entries in aggregate", Expr); | |
4243 | return; | |
4244 | end if; | |
4245 | ||
4246 | Val := Static_Integer (Expr); | |
4247 | ||
d9125581 | 4248 | -- Err signals that we found some incorrect entries processing |
4249 | -- the list. The final checks for completeness and ordering are | |
4250 | -- skipped in this case. | |
4251 | ||
d6f39728 | 4252 | if Val = No_Uint then |
4253 | Err := True; | |
d6f39728 | 4254 | elsif Val < Lo or else Hi < Val then |
4255 | Error_Msg_N ("value outside permitted range", Expr); | |
4256 | Err := True; | |
4257 | end if; | |
4258 | ||
4259 | Set_Enumeration_Rep (Elit, Val); | |
4260 | Set_Enumeration_Rep_Expr (Elit, Expr); | |
4261 | Next (Expr); | |
4262 | Next (Elit); | |
4263 | end loop; | |
4264 | end if; | |
4265 | ||
4266 | -- Now process the named entries if present | |
4267 | ||
4268 | if Present (Component_Associations (Aggr)) then | |
4269 | Assoc := First (Component_Associations (Aggr)); | |
4270 | while Present (Assoc) loop | |
4271 | Choice := First (Choices (Assoc)); | |
4272 | ||
4273 | if Present (Next (Choice)) then | |
4274 | Error_Msg_N | |
4275 | ("multiple choice not allowed here", Next (Choice)); | |
4276 | Err := True; | |
4277 | end if; | |
4278 | ||
4279 | if Nkind (Choice) = N_Others_Choice then | |
4280 | Error_Msg_N ("others choice not allowed here", Choice); | |
4281 | Err := True; | |
4282 | ||
4283 | elsif Nkind (Choice) = N_Range then | |
b3190af0 | 4284 | |
d6f39728 | 4285 | -- ??? should allow zero/one element range here |
b3190af0 | 4286 | |
d6f39728 | 4287 | Error_Msg_N ("range not allowed here", Choice); |
4288 | Err := True; | |
4289 | ||
4290 | else | |
4291 | Analyze_And_Resolve (Choice, Enumtype); | |
b3190af0 | 4292 | |
098d3082 | 4293 | if Error_Posted (Choice) then |
d6f39728 | 4294 | Err := True; |
098d3082 | 4295 | end if; |
d6f39728 | 4296 | |
098d3082 | 4297 | if not Err then |
4298 | if Is_Entity_Name (Choice) | |
4299 | and then Is_Type (Entity (Choice)) | |
4300 | then | |
4301 | Error_Msg_N ("subtype name not allowed here", Choice); | |
d6f39728 | 4302 | Err := True; |
b3190af0 | 4303 | |
098d3082 | 4304 | -- ??? should allow static subtype with zero/one entry |
d6f39728 | 4305 | |
098d3082 | 4306 | elsif Etype (Choice) = Base_Type (Enumtype) then |
4307 | if not Is_Static_Expression (Choice) then | |
4308 | Flag_Non_Static_Expr | |
4309 | ("non-static expression used for choice!", Choice); | |
d6f39728 | 4310 | Err := True; |
d6f39728 | 4311 | |
098d3082 | 4312 | else |
4313 | Elit := Expr_Value_E (Choice); | |
4314 | ||
4315 | if Present (Enumeration_Rep_Expr (Elit)) then | |
4316 | Error_Msg_Sloc := | |
4317 | Sloc (Enumeration_Rep_Expr (Elit)); | |
4318 | Error_Msg_NE | |
4319 | ("representation for& previously given#", | |
4320 | Choice, Elit); | |
4321 | Err := True; | |
4322 | end if; | |
d6f39728 | 4323 | |
098d3082 | 4324 | Set_Enumeration_Rep_Expr (Elit, Expression (Assoc)); |
d6f39728 | 4325 | |
098d3082 | 4326 | Expr := Expression (Assoc); |
4327 | Val := Static_Integer (Expr); | |
d6f39728 | 4328 | |
098d3082 | 4329 | if Val = No_Uint then |
4330 | Err := True; | |
4331 | ||
4332 | elsif Val < Lo or else Hi < Val then | |
4333 | Error_Msg_N ("value outside permitted range", Expr); | |
4334 | Err := True; | |
4335 | end if; | |
d6f39728 | 4336 | |
098d3082 | 4337 | Set_Enumeration_Rep (Elit, Val); |
4338 | end if; | |
d6f39728 | 4339 | end if; |
4340 | end if; | |
4341 | end if; | |
4342 | ||
4343 | Next (Assoc); | |
4344 | end loop; | |
4345 | end if; | |
4346 | ||
4347 | -- Aggregate is fully processed. Now we check that a full set of | |
4348 | -- representations was given, and that they are in range and in order. | |
4349 | -- These checks are only done if no other errors occurred. | |
4350 | ||
4351 | if not Err then | |
4352 | Min := No_Uint; | |
4353 | Max := No_Uint; | |
4354 | ||
4355 | Elit := First_Literal (Enumtype); | |
4356 | while Present (Elit) loop | |
4357 | if No (Enumeration_Rep_Expr (Elit)) then | |
4358 | Error_Msg_NE ("missing representation for&!", N, Elit); | |
4359 | ||
4360 | else | |
4361 | Val := Enumeration_Rep (Elit); | |
4362 | ||
4363 | if Min = No_Uint then | |
4364 | Min := Val; | |
4365 | end if; | |
4366 | ||
4367 | if Val /= No_Uint then | |
4368 | if Max /= No_Uint and then Val <= Max then | |
4369 | Error_Msg_NE | |
4370 | ("enumeration value for& not ordered!", | |
e30c7d84 | 4371 | Enumeration_Rep_Expr (Elit), Elit); |
d6f39728 | 4372 | end if; |
4373 | ||
e30c7d84 | 4374 | Max_Node := Enumeration_Rep_Expr (Elit); |
d6f39728 | 4375 | Max := Val; |
4376 | end if; | |
4377 | ||
e30c7d84 | 4378 | -- If there is at least one literal whose representation is not |
4379 | -- equal to the Pos value, then note that this enumeration type | |
4380 | -- has a non-standard representation. | |
d6f39728 | 4381 | |
4382 | if Val /= Enumeration_Pos (Elit) then | |
4383 | Set_Has_Non_Standard_Rep (Base_Type (Enumtype)); | |
4384 | end if; | |
4385 | end if; | |
4386 | ||
4387 | Next (Elit); | |
4388 | end loop; | |
4389 | ||
4390 | -- Now set proper size information | |
4391 | ||
4392 | declare | |
4393 | Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype)); | |
4394 | ||
4395 | begin | |
4396 | if Has_Size_Clause (Enumtype) then | |
e30c7d84 | 4397 | |
4398 | -- All OK, if size is OK now | |
4399 | ||
4400 | if RM_Size (Enumtype) >= Minsize then | |
d6f39728 | 4401 | null; |
4402 | ||
4403 | else | |
e30c7d84 | 4404 | -- Try if we can get by with biasing |
4405 | ||
d6f39728 | 4406 | Minsize := |
4407 | UI_From_Int (Minimum_Size (Enumtype, Biased => True)); | |
4408 | ||
e30c7d84 | 4409 | -- Error message if even biasing does not work |
4410 | ||
4411 | if RM_Size (Enumtype) < Minsize then | |
4412 | Error_Msg_Uint_1 := RM_Size (Enumtype); | |
4413 | Error_Msg_Uint_2 := Max; | |
4414 | Error_Msg_N | |
4415 | ("previously given size (^) is too small " | |
4416 | & "for this value (^)", Max_Node); | |
4417 | ||
4418 | -- If biasing worked, indicate that we now have biased rep | |
d6f39728 | 4419 | |
4420 | else | |
b77e4501 | 4421 | Set_Biased |
4422 | (Enumtype, Size_Clause (Enumtype), "size clause"); | |
d6f39728 | 4423 | end if; |
4424 | end if; | |
4425 | ||
4426 | else | |
4427 | Set_RM_Size (Enumtype, Minsize); | |
4428 | Set_Enum_Esize (Enumtype); | |
4429 | end if; | |
4430 | ||
4431 | Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype)); | |
4432 | Set_Esize (Base_Type (Enumtype), Esize (Enumtype)); | |
4433 | Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype)); | |
4434 | end; | |
4435 | end if; | |
4436 | ||
4437 | -- We repeat the too late test in case it froze itself! | |
4438 | ||
4439 | if Rep_Item_Too_Late (Enumtype, N) then | |
4440 | null; | |
4441 | end if; | |
d6f39728 | 4442 | end Analyze_Enumeration_Representation_Clause; |
4443 | ||
4444 | ---------------------------- | |
4445 | -- Analyze_Free_Statement -- | |
4446 | ---------------------------- | |
4447 | ||
4448 | procedure Analyze_Free_Statement (N : Node_Id) is | |
4449 | begin | |
4450 | Analyze (Expression (N)); | |
4451 | end Analyze_Free_Statement; | |
4452 | ||
40ca69b9 | 4453 | --------------------------- |
4454 | -- Analyze_Freeze_Entity -- | |
4455 | --------------------------- | |
4456 | ||
4457 | procedure Analyze_Freeze_Entity (N : Node_Id) is | |
4458 | E : constant Entity_Id := Entity (N); | |
4459 | ||
4460 | begin | |
98f7db28 | 4461 | -- Remember that we are processing a freezing entity. Required to |
4462 | -- ensure correct decoration of internal entities associated with | |
4463 | -- interfaces (see New_Overloaded_Entity). | |
4464 | ||
4465 | Inside_Freezing_Actions := Inside_Freezing_Actions + 1; | |
4466 | ||
40ca69b9 | 4467 | -- For tagged types covering interfaces add internal entities that link |
4468 | -- the primitives of the interfaces with the primitives that cover them. | |
40ca69b9 | 4469 | -- Note: These entities were originally generated only when generating |
4470 | -- code because their main purpose was to provide support to initialize | |
4471 | -- the secondary dispatch tables. They are now generated also when | |
4472 | -- compiling with no code generation to provide ASIS the relationship | |
c8da6114 | 4473 | -- between interface primitives and tagged type primitives. They are |
4474 | -- also used to locate primitives covering interfaces when processing | |
4475 | -- generics (see Derive_Subprograms). | |
40ca69b9 | 4476 | |
de54c5ab | 4477 | if Ada_Version >= Ada_2005 |
40ca69b9 | 4478 | and then Ekind (E) = E_Record_Type |
4479 | and then Is_Tagged_Type (E) | |
4480 | and then not Is_Interface (E) | |
4481 | and then Has_Interfaces (E) | |
4482 | then | |
c8da6114 | 4483 | -- This would be a good common place to call the routine that checks |
4484 | -- overriding of interface primitives (and thus factorize calls to | |
4485 | -- Check_Abstract_Overriding located at different contexts in the | |
4486 | -- compiler). However, this is not possible because it causes | |
4487 | -- spurious errors in case of late overriding. | |
4488 | ||
40ca69b9 | 4489 | Add_Internal_Interface_Entities (E); |
4490 | end if; | |
d00681a7 | 4491 | |
4492 | -- Check CPP types | |
4493 | ||
4494 | if Ekind (E) = E_Record_Type | |
4495 | and then Is_CPP_Class (E) | |
4496 | and then Is_Tagged_Type (E) | |
4497 | and then Tagged_Type_Expansion | |
4498 | and then Expander_Active | |
4499 | then | |
4500 | if CPP_Num_Prims (E) = 0 then | |
4501 | ||
4502 | -- If the CPP type has user defined components then it must import | |
4503 | -- primitives from C++. This is required because if the C++ class | |
4504 | -- has no primitives then the C++ compiler does not added the _tag | |
4505 | -- component to the type. | |
4506 | ||
4507 | pragma Assert (Chars (First_Entity (E)) = Name_uTag); | |
4508 | ||
4509 | if First_Entity (E) /= Last_Entity (E) then | |
4510 | Error_Msg_N | |
1e3532e7 | 4511 | ("'C'P'P type must import at least one primitive from C++??", |
d00681a7 | 4512 | E); |
4513 | end if; | |
4514 | end if; | |
4515 | ||
4516 | -- Check that all its primitives are abstract or imported from C++. | |
4517 | -- Check also availability of the C++ constructor. | |
4518 | ||
4519 | declare | |
4520 | Has_Constructors : constant Boolean := Has_CPP_Constructors (E); | |
4521 | Elmt : Elmt_Id; | |
4522 | Error_Reported : Boolean := False; | |
4523 | Prim : Node_Id; | |
4524 | ||
4525 | begin | |
4526 | Elmt := First_Elmt (Primitive_Operations (E)); | |
4527 | while Present (Elmt) loop | |
4528 | Prim := Node (Elmt); | |
4529 | ||
4530 | if Comes_From_Source (Prim) then | |
4531 | if Is_Abstract_Subprogram (Prim) then | |
4532 | null; | |
4533 | ||
4534 | elsif not Is_Imported (Prim) | |
4535 | or else Convention (Prim) /= Convention_CPP | |
4536 | then | |
4537 | Error_Msg_N | |
1e3532e7 | 4538 | ("primitives of 'C'P'P types must be imported from C++ " |
4539 | & "or abstract??", Prim); | |
d00681a7 | 4540 | |
4541 | elsif not Has_Constructors | |
4542 | and then not Error_Reported | |
4543 | then | |
4544 | Error_Msg_Name_1 := Chars (E); | |
4545 | Error_Msg_N | |
1e3532e7 | 4546 | ("??'C'P'P constructor required for type %", Prim); |
d00681a7 | 4547 | Error_Reported := True; |
4548 | end if; | |
4549 | end if; | |
4550 | ||
4551 | Next_Elmt (Elmt); | |
4552 | end loop; | |
4553 | end; | |
4554 | end if; | |
98f7db28 | 4555 | |
85377c9b | 4556 | -- Check Ada derivation of CPP type |
4557 | ||
4558 | if Expander_Active | |
4559 | and then Tagged_Type_Expansion | |
4560 | and then Ekind (E) = E_Record_Type | |
4561 | and then Etype (E) /= E | |
4562 | and then Is_CPP_Class (Etype (E)) | |
4563 | and then CPP_Num_Prims (Etype (E)) > 0 | |
4564 | and then not Is_CPP_Class (E) | |
4565 | and then not Has_CPP_Constructors (Etype (E)) | |
4566 | then | |
4567 | -- If the parent has C++ primitives but it has no constructor then | |
4568 | -- check that all the primitives are overridden in this derivation; | |
4569 | -- otherwise the constructor of the parent is needed to build the | |
4570 | -- dispatch table. | |
4571 | ||
4572 | declare | |
4573 | Elmt : Elmt_Id; | |
4574 | Prim : Node_Id; | |
4575 | ||
4576 | begin | |
4577 | Elmt := First_Elmt (Primitive_Operations (E)); | |
4578 | while Present (Elmt) loop | |
4579 | Prim := Node (Elmt); | |
4580 | ||
4581 | if not Is_Abstract_Subprogram (Prim) | |
4582 | and then No (Interface_Alias (Prim)) | |
4583 | and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E | |
4584 | then | |
4585 | Error_Msg_Name_1 := Chars (Etype (E)); | |
4586 | Error_Msg_N | |
4587 | ("'C'P'P constructor required for parent type %", E); | |
4588 | exit; | |
4589 | end if; | |
4590 | ||
4591 | Next_Elmt (Elmt); | |
4592 | end loop; | |
4593 | end; | |
4594 | end if; | |
4595 | ||
98f7db28 | 4596 | Inside_Freezing_Actions := Inside_Freezing_Actions - 1; |
9dc88aea | 4597 | |
4598 | -- If we have a type with predicates, build predicate function | |
4599 | ||
4600 | if Is_Type (E) and then Has_Predicates (E) then | |
490beba6 | 4601 | Build_Predicate_Function (E, N); |
9dc88aea | 4602 | end if; |
fb7f2fc4 | 4603 | |
d64221a7 | 4604 | -- If type has delayed aspects, this is where we do the preanalysis at |
4605 | -- the freeze point, as part of the consistent visibility check. Note | |
4606 | -- that this must be done after calling Build_Predicate_Function or | |
4607 | -- Build_Invariant_Procedure since these subprograms fix occurrences of | |
4608 | -- the subtype name in the saved expression so that they will not cause | |
4609 | -- trouble in the preanalysis. | |
fb7f2fc4 | 4610 | |
89f1e35c | 4611 | if Has_Delayed_Aspects (E) |
4612 | and then Scope (E) = Current_Scope | |
4613 | then | |
4614 | -- Retrieve the visibility to the discriminants in order to properly | |
4615 | -- analyze the aspects. | |
4616 | ||
4617 | Push_Scope_And_Install_Discriminants (E); | |
4618 | ||
fb7f2fc4 | 4619 | declare |
4620 | Ritem : Node_Id; | |
4621 | ||
4622 | begin | |
4623 | -- Look for aspect specification entries for this entity | |
4624 | ||
4625 | Ritem := First_Rep_Item (E); | |
4626 | while Present (Ritem) loop | |
4627 | if Nkind (Ritem) = N_Aspect_Specification | |
4628 | and then Entity (Ritem) = E | |
4629 | and then Is_Delayed_Aspect (Ritem) | |
4630 | then | |
4631 | Check_Aspect_At_Freeze_Point (Ritem); | |
4632 | end if; | |
4633 | ||
4634 | Next_Rep_Item (Ritem); | |
4635 | end loop; | |
4636 | end; | |
89f1e35c | 4637 | |
4638 | Uninstall_Discriminants_And_Pop_Scope (E); | |
fb7f2fc4 | 4639 | end if; |
40ca69b9 | 4640 | end Analyze_Freeze_Entity; |
4641 | ||
d6f39728 | 4642 | ------------------------------------------ |
4643 | -- Analyze_Record_Representation_Clause -- | |
4644 | ------------------------------------------ | |
4645 | ||
67278d60 | 4646 | -- Note: we check as much as we can here, but we can't do any checks |
4647 | -- based on the position values (e.g. overlap checks) until freeze time | |
4648 | -- because especially in Ada 2005 (machine scalar mode), the processing | |
4649 | -- for non-standard bit order can substantially change the positions. | |
4650 | -- See procedure Check_Record_Representation_Clause (called from Freeze) | |
4651 | -- for the remainder of this processing. | |
4652 | ||
d6f39728 | 4653 | procedure Analyze_Record_Representation_Clause (N : Node_Id) is |
7800b920 | 4654 | Ident : constant Node_Id := Identifier (N); |
4655 | Biased : Boolean; | |
d6f39728 | 4656 | CC : Node_Id; |
7800b920 | 4657 | Comp : Entity_Id; |
d6f39728 | 4658 | Fbit : Uint; |
d6f39728 | 4659 | Hbit : Uint := Uint_0; |
7800b920 | 4660 | Lbit : Uint; |
d6f39728 | 4661 | Ocomp : Entity_Id; |
7800b920 | 4662 | Posit : Uint; |
4663 | Rectype : Entity_Id; | |
d6f39728 | 4664 | |
639e37b0 | 4665 | CR_Pragma : Node_Id := Empty; |
4666 | -- Points to N_Pragma node if Complete_Representation pragma present | |
4667 | ||
d6f39728 | 4668 | begin |
fbc67f84 | 4669 | if Ignore_Rep_Clauses then |
4670 | return; | |
4671 | end if; | |
4672 | ||
d6f39728 | 4673 | Find_Type (Ident); |
4674 | Rectype := Entity (Ident); | |
4675 | ||
4676 | if Rectype = Any_Type | |
4677 | or else Rep_Item_Too_Early (Rectype, N) | |
4678 | then | |
4679 | return; | |
4680 | else | |
4681 | Rectype := Underlying_Type (Rectype); | |
4682 | end if; | |
4683 | ||
4684 | -- First some basic error checks | |
4685 | ||
4686 | if not Is_Record_Type (Rectype) then | |
4687 | Error_Msg_NE | |
4688 | ("record type required, found}", Ident, First_Subtype (Rectype)); | |
4689 | return; | |
4690 | ||
d6f39728 | 4691 | elsif Scope (Rectype) /= Current_Scope then |
4692 | Error_Msg_N ("type must be declared in this scope", N); | |
4693 | return; | |
4694 | ||
4695 | elsif not Is_First_Subtype (Rectype) then | |
4696 | Error_Msg_N ("cannot give record rep clause for subtype", N); | |
4697 | return; | |
4698 | ||
4699 | elsif Has_Record_Rep_Clause (Rectype) then | |
4700 | Error_Msg_N ("duplicate record rep clause ignored", N); | |
4701 | return; | |
4702 | ||
4703 | elsif Rep_Item_Too_Late (Rectype, N) then | |
4704 | return; | |
4705 | end if; | |
4706 | ||
4707 | if Present (Mod_Clause (N)) then | |
4708 | declare | |
4709 | Loc : constant Source_Ptr := Sloc (N); | |
4710 | M : constant Node_Id := Mod_Clause (N); | |
4711 | P : constant List_Id := Pragmas_Before (M); | |
d6f39728 | 4712 | AtM_Nod : Node_Id; |
4713 | ||
9dfe12ae | 4714 | Mod_Val : Uint; |
4715 | pragma Warnings (Off, Mod_Val); | |
4716 | ||
d6f39728 | 4717 | begin |
e0521a36 | 4718 | Check_Restriction (No_Obsolescent_Features, Mod_Clause (N)); |
4719 | ||
9dfe12ae | 4720 | if Warn_On_Obsolescent_Feature then |
4721 | Error_Msg_N | |
b174444e | 4722 | ("?j?mod clause is an obsolescent feature (RM J.8)", N); |
9dfe12ae | 4723 | Error_Msg_N |
1e3532e7 | 4724 | ("\?j?use alignment attribute definition clause instead", N); |
9dfe12ae | 4725 | end if; |
4726 | ||
d6f39728 | 4727 | if Present (P) then |
4728 | Analyze_List (P); | |
4729 | end if; | |
4730 | ||
fbc67f84 | 4731 | -- In ASIS_Mode mode, expansion is disabled, but we must convert |
4732 | -- the Mod clause into an alignment clause anyway, so that the | |
4733 | -- back-end can compute and back-annotate properly the size and | |
4734 | -- alignment of types that may include this record. | |
d6f39728 | 4735 | |
15ebb600 | 4736 | -- This seems dubious, this destroys the source tree in a manner |
4737 | -- not detectable by ASIS ??? | |
4738 | ||
3157c4f3 | 4739 | if Operating_Mode = Check_Semantics and then ASIS_Mode then |
d6f39728 | 4740 | AtM_Nod := |
4741 | Make_Attribute_Definition_Clause (Loc, | |
4742 | Name => New_Reference_To (Base_Type (Rectype), Loc), | |
4743 | Chars => Name_Alignment, | |
4744 | Expression => Relocate_Node (Expression (M))); | |
4745 | ||
4746 | Set_From_At_Mod (AtM_Nod); | |
4747 | Insert_After (N, AtM_Nod); | |
4748 | Mod_Val := Get_Alignment_Value (Expression (AtM_Nod)); | |
4749 | Set_Mod_Clause (N, Empty); | |
4750 | ||
4751 | else | |
4752 | -- Get the alignment value to perform error checking | |
4753 | ||
4754 | Mod_Val := Get_Alignment_Value (Expression (M)); | |
d6f39728 | 4755 | end if; |
4756 | end; | |
4757 | end if; | |
4758 | ||
3062c401 | 4759 | -- For untagged types, clear any existing component clauses for the |
4760 | -- type. If the type is derived, this is what allows us to override | |
4761 | -- a rep clause for the parent. For type extensions, the representation | |
4762 | -- of the inherited components is inherited, so we want to keep previous | |
4763 | -- component clauses for completeness. | |
d6f39728 | 4764 | |
3062c401 | 4765 | if not Is_Tagged_Type (Rectype) then |
4766 | Comp := First_Component_Or_Discriminant (Rectype); | |
4767 | while Present (Comp) loop | |
4768 | Set_Component_Clause (Comp, Empty); | |
4769 | Next_Component_Or_Discriminant (Comp); | |
4770 | end loop; | |
4771 | end if; | |
d6f39728 | 4772 | |
4773 | -- All done if no component clauses | |
4774 | ||
4775 | CC := First (Component_Clauses (N)); | |
4776 | ||
4777 | if No (CC) then | |
4778 | return; | |
4779 | end if; | |
4780 | ||
f15731c4 | 4781 | -- A representation like this applies to the base type |
d6f39728 | 4782 | |
4783 | Set_Has_Record_Rep_Clause (Base_Type (Rectype)); | |
4784 | Set_Has_Non_Standard_Rep (Base_Type (Rectype)); | |
4785 | Set_Has_Specified_Layout (Base_Type (Rectype)); | |
4786 | ||
d6f39728 | 4787 | -- Process the component clauses |
4788 | ||
4789 | while Present (CC) loop | |
4790 | ||
639e37b0 | 4791 | -- Pragma |
d6f39728 | 4792 | |
4793 | if Nkind (CC) = N_Pragma then | |
4794 | Analyze (CC); | |
4795 | ||
639e37b0 | 4796 | -- The only pragma of interest is Complete_Representation |
4797 | ||
fdd294d1 | 4798 | if Pragma_Name (CC) = Name_Complete_Representation then |
639e37b0 | 4799 | CR_Pragma := CC; |
4800 | end if; | |
4801 | ||
d6f39728 | 4802 | -- Processing for real component clause |
4803 | ||
4804 | else | |
d6f39728 | 4805 | Posit := Static_Integer (Position (CC)); |
4806 | Fbit := Static_Integer (First_Bit (CC)); | |
4807 | Lbit := Static_Integer (Last_Bit (CC)); | |
4808 | ||
4809 | if Posit /= No_Uint | |
4810 | and then Fbit /= No_Uint | |
4811 | and then Lbit /= No_Uint | |
4812 | then | |
4813 | if Posit < 0 then | |
4814 | Error_Msg_N | |
4815 | ("position cannot be negative", Position (CC)); | |
4816 | ||
4817 | elsif Fbit < 0 then | |
4818 | Error_Msg_N | |
4819 | ("first bit cannot be negative", First_Bit (CC)); | |
4820 | ||
177675a7 | 4821 | -- The Last_Bit specified in a component clause must not be |
4822 | -- less than the First_Bit minus one (RM-13.5.1(10)). | |
4823 | ||
4824 | elsif Lbit < Fbit - 1 then | |
4825 | Error_Msg_N | |
4826 | ("last bit cannot be less than first bit minus one", | |
4827 | Last_Bit (CC)); | |
4828 | ||
d6f39728 | 4829 | -- Values look OK, so find the corresponding record component |
4830 | -- Even though the syntax allows an attribute reference for | |
4831 | -- implementation-defined components, GNAT does not allow the | |
4832 | -- tag to get an explicit position. | |
4833 | ||
4834 | elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then | |
d6f39728 | 4835 | if Attribute_Name (Component_Name (CC)) = Name_Tag then |
4836 | Error_Msg_N ("position of tag cannot be specified", CC); | |
4837 | else | |
4838 | Error_Msg_N ("illegal component name", CC); | |
4839 | end if; | |
4840 | ||
4841 | else | |
4842 | Comp := First_Entity (Rectype); | |
4843 | while Present (Comp) loop | |
4844 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
4845 | Next_Entity (Comp); | |
4846 | end loop; | |
4847 | ||
4848 | if No (Comp) then | |
4849 | ||
4850 | -- Maybe component of base type that is absent from | |
4851 | -- statically constrained first subtype. | |
4852 | ||
4853 | Comp := First_Entity (Base_Type (Rectype)); | |
4854 | while Present (Comp) loop | |
4855 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
4856 | Next_Entity (Comp); | |
4857 | end loop; | |
4858 | end if; | |
4859 | ||
4860 | if No (Comp) then | |
4861 | Error_Msg_N | |
4862 | ("component clause is for non-existent field", CC); | |
4863 | ||
7800b920 | 4864 | -- Ada 2012 (AI05-0026): Any name that denotes a |
4865 | -- discriminant of an object of an unchecked union type | |
4866 | -- shall not occur within a record_representation_clause. | |
4867 | ||
4868 | -- The general restriction of using record rep clauses on | |
4869 | -- Unchecked_Union types has now been lifted. Since it is | |
4870 | -- possible to introduce a record rep clause which mentions | |
4871 | -- the discriminant of an Unchecked_Union in non-Ada 2012 | |
4872 | -- code, this check is applied to all versions of the | |
4873 | -- language. | |
4874 | ||
4875 | elsif Ekind (Comp) = E_Discriminant | |
4876 | and then Is_Unchecked_Union (Rectype) | |
4877 | then | |
4878 | Error_Msg_N | |
41d55ffb | 4879 | ("cannot reference discriminant of unchecked union", |
7800b920 | 4880 | Component_Name (CC)); |
4881 | ||
d6f39728 | 4882 | elsif Present (Component_Clause (Comp)) then |
3062c401 | 4883 | |
1a34e48c | 4884 | -- Diagnose duplicate rep clause, or check consistency |
fdd294d1 | 4885 | -- if this is an inherited component. In a double fault, |
3062c401 | 4886 | -- there may be a duplicate inconsistent clause for an |
4887 | -- inherited component. | |
4888 | ||
fdd294d1 | 4889 | if Scope (Original_Record_Component (Comp)) = Rectype |
4890 | or else Parent (Component_Clause (Comp)) = N | |
3062c401 | 4891 | then |
4892 | Error_Msg_Sloc := Sloc (Component_Clause (Comp)); | |
4893 | Error_Msg_N ("component clause previously given#", CC); | |
4894 | ||
4895 | else | |
4896 | declare | |
4897 | Rep1 : constant Node_Id := Component_Clause (Comp); | |
3062c401 | 4898 | begin |
4899 | if Intval (Position (Rep1)) /= | |
4900 | Intval (Position (CC)) | |
4901 | or else Intval (First_Bit (Rep1)) /= | |
4902 | Intval (First_Bit (CC)) | |
4903 | or else Intval (Last_Bit (Rep1)) /= | |
4904 | Intval (Last_Bit (CC)) | |
4905 | then | |
b9e61b2a | 4906 | Error_Msg_N |
4907 | ("component clause inconsistent " | |
4908 | & "with representation of ancestor", CC); | |
3062c401 | 4909 | elsif Warn_On_Redundant_Constructs then |
b9e61b2a | 4910 | Error_Msg_N |
1e3532e7 | 4911 | ("?r?redundant component clause " |
b9e61b2a | 4912 | & "for inherited component!", CC); |
3062c401 | 4913 | end if; |
4914 | end; | |
4915 | end if; | |
d6f39728 | 4916 | |
d2b860b4 | 4917 | -- Normal case where this is the first component clause we |
4918 | -- have seen for this entity, so set it up properly. | |
4919 | ||
d6f39728 | 4920 | else |
83f8f0a6 | 4921 | -- Make reference for field in record rep clause and set |
4922 | -- appropriate entity field in the field identifier. | |
4923 | ||
4924 | Generate_Reference | |
4925 | (Comp, Component_Name (CC), Set_Ref => False); | |
4926 | Set_Entity (Component_Name (CC), Comp); | |
4927 | ||
2866d595 | 4928 | -- Update Fbit and Lbit to the actual bit number |
d6f39728 | 4929 | |
4930 | Fbit := Fbit + UI_From_Int (SSU) * Posit; | |
4931 | Lbit := Lbit + UI_From_Int (SSU) * Posit; | |
4932 | ||
d6f39728 | 4933 | if Has_Size_Clause (Rectype) |
ada34def | 4934 | and then RM_Size (Rectype) <= Lbit |
d6f39728 | 4935 | then |
4936 | Error_Msg_N | |
4937 | ("bit number out of range of specified size", | |
4938 | Last_Bit (CC)); | |
4939 | else | |
4940 | Set_Component_Clause (Comp, CC); | |
4941 | Set_Component_Bit_Offset (Comp, Fbit); | |
4942 | Set_Esize (Comp, 1 + (Lbit - Fbit)); | |
4943 | Set_Normalized_First_Bit (Comp, Fbit mod SSU); | |
4944 | Set_Normalized_Position (Comp, Fbit / SSU); | |
4945 | ||
a0fc8c5b | 4946 | if Warn_On_Overridden_Size |
4947 | and then Has_Size_Clause (Etype (Comp)) | |
4948 | and then RM_Size (Etype (Comp)) /= Esize (Comp) | |
4949 | then | |
4950 | Error_Msg_NE | |
1e3532e7 | 4951 | ("?S?component size overrides size clause for&", |
a0fc8c5b | 4952 | Component_Name (CC), Etype (Comp)); |
4953 | end if; | |
4954 | ||
ea61a7ea | 4955 | -- This information is also set in the corresponding |
4956 | -- component of the base type, found by accessing the | |
4957 | -- Original_Record_Component link if it is present. | |
d6f39728 | 4958 | |
4959 | Ocomp := Original_Record_Component (Comp); | |
4960 | ||
4961 | if Hbit < Lbit then | |
4962 | Hbit := Lbit; | |
4963 | end if; | |
4964 | ||
4965 | Check_Size | |
4966 | (Component_Name (CC), | |
4967 | Etype (Comp), | |
4968 | Esize (Comp), | |
4969 | Biased); | |
4970 | ||
b77e4501 | 4971 | Set_Biased |
4972 | (Comp, First_Node (CC), "component clause", Biased); | |
cc46ff4b | 4973 | |
d6f39728 | 4974 | if Present (Ocomp) then |
4975 | Set_Component_Clause (Ocomp, CC); | |
4976 | Set_Component_Bit_Offset (Ocomp, Fbit); | |
4977 | Set_Normalized_First_Bit (Ocomp, Fbit mod SSU); | |
4978 | Set_Normalized_Position (Ocomp, Fbit / SSU); | |
4979 | Set_Esize (Ocomp, 1 + (Lbit - Fbit)); | |
4980 | ||
4981 | Set_Normalized_Position_Max | |
4982 | (Ocomp, Normalized_Position (Ocomp)); | |
4983 | ||
b77e4501 | 4984 | -- Note: we don't use Set_Biased here, because we |
4985 | -- already gave a warning above if needed, and we | |
4986 | -- would get a duplicate for the same name here. | |
4987 | ||
d6f39728 | 4988 | Set_Has_Biased_Representation |
4989 | (Ocomp, Has_Biased_Representation (Comp)); | |
4990 | end if; | |
4991 | ||
4992 | if Esize (Comp) < 0 then | |
4993 | Error_Msg_N ("component size is negative", CC); | |
4994 | end if; | |
4995 | end if; | |
4996 | end if; | |
4997 | end if; | |
4998 | end if; | |
4999 | end if; | |
5000 | ||
5001 | Next (CC); | |
5002 | end loop; | |
5003 | ||
67278d60 | 5004 | -- Check missing components if Complete_Representation pragma appeared |
d6f39728 | 5005 | |
67278d60 | 5006 | if Present (CR_Pragma) then |
5007 | Comp := First_Component_Or_Discriminant (Rectype); | |
5008 | while Present (Comp) loop | |
5009 | if No (Component_Clause (Comp)) then | |
5010 | Error_Msg_NE | |
5011 | ("missing component clause for &", CR_Pragma, Comp); | |
5012 | end if; | |
d6f39728 | 5013 | |
67278d60 | 5014 | Next_Component_Or_Discriminant (Comp); |
5015 | end loop; | |
d6f39728 | 5016 | |
1e3532e7 | 5017 | -- Give missing components warning if required |
15ebb600 | 5018 | |
fdd294d1 | 5019 | elsif Warn_On_Unrepped_Components then |
15ebb600 | 5020 | declare |
5021 | Num_Repped_Components : Nat := 0; | |
5022 | Num_Unrepped_Components : Nat := 0; | |
5023 | ||
5024 | begin | |
5025 | -- First count number of repped and unrepped components | |
5026 | ||
5027 | Comp := First_Component_Or_Discriminant (Rectype); | |
5028 | while Present (Comp) loop | |
5029 | if Present (Component_Clause (Comp)) then | |
5030 | Num_Repped_Components := Num_Repped_Components + 1; | |
5031 | else | |
5032 | Num_Unrepped_Components := Num_Unrepped_Components + 1; | |
5033 | end if; | |
5034 | ||
5035 | Next_Component_Or_Discriminant (Comp); | |
5036 | end loop; | |
5037 | ||
5038 | -- We are only interested in the case where there is at least one | |
5039 | -- unrepped component, and at least half the components have rep | |
5040 | -- clauses. We figure that if less than half have them, then the | |
87f9eef5 | 5041 | -- partial rep clause is really intentional. If the component |
5042 | -- type has no underlying type set at this point (as for a generic | |
5043 | -- formal type), we don't know enough to give a warning on the | |
5044 | -- component. | |
15ebb600 | 5045 | |
5046 | if Num_Unrepped_Components > 0 | |
5047 | and then Num_Unrepped_Components < Num_Repped_Components | |
5048 | then | |
5049 | Comp := First_Component_Or_Discriminant (Rectype); | |
5050 | while Present (Comp) loop | |
83f8f0a6 | 5051 | if No (Component_Clause (Comp)) |
3062c401 | 5052 | and then Comes_From_Source (Comp) |
87f9eef5 | 5053 | and then Present (Underlying_Type (Etype (Comp))) |
83f8f0a6 | 5054 | and then (Is_Scalar_Type (Underlying_Type (Etype (Comp))) |
67278d60 | 5055 | or else Size_Known_At_Compile_Time |
5056 | (Underlying_Type (Etype (Comp)))) | |
fdd294d1 | 5057 | and then not Has_Warnings_Off (Rectype) |
83f8f0a6 | 5058 | then |
15ebb600 | 5059 | Error_Msg_Sloc := Sloc (Comp); |
5060 | Error_Msg_NE | |
1e3532e7 | 5061 | ("?C?no component clause given for & declared #", |
15ebb600 | 5062 | N, Comp); |
5063 | end if; | |
5064 | ||
5065 | Next_Component_Or_Discriminant (Comp); | |
5066 | end loop; | |
5067 | end if; | |
5068 | end; | |
d6f39728 | 5069 | end if; |
d6f39728 | 5070 | end Analyze_Record_Representation_Clause; |
5071 | ||
9ea61fdd | 5072 | ------------------------------------------- |
5073 | -- Build_Invariant_Procedure_Declaration -- | |
5074 | ------------------------------------------- | |
5075 | ||
5076 | function Build_Invariant_Procedure_Declaration | |
5077 | (Typ : Entity_Id) return Node_Id | |
5078 | is | |
5079 | Loc : constant Source_Ptr := Sloc (Typ); | |
5080 | Object_Entity : constant Entity_Id := | |
5081 | Make_Defining_Identifier (Loc, New_Internal_Name ('I')); | |
5082 | Spec : Node_Id; | |
5083 | SId : Entity_Id; | |
5084 | ||
5085 | begin | |
5086 | Set_Etype (Object_Entity, Typ); | |
5087 | ||
5088 | -- Check for duplicate definiations. | |
5089 | ||
1e3532e7 | 5090 | if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then |
9ea61fdd | 5091 | return Empty; |
5092 | end if; | |
5093 | ||
5094 | SId := Make_Defining_Identifier (Loc, | |
5095 | Chars => New_External_Name (Chars (Typ), "Invariant")); | |
5096 | Set_Has_Invariants (SId); | |
5097 | Set_Has_Invariants (Typ); | |
5098 | Set_Ekind (SId, E_Procedure); | |
5099 | Set_Invariant_Procedure (Typ, SId); | |
5100 | ||
5101 | Spec := | |
5102 | Make_Procedure_Specification (Loc, | |
5103 | Defining_Unit_Name => SId, | |
5104 | Parameter_Specifications => New_List ( | |
5105 | Make_Parameter_Specification (Loc, | |
5106 | Defining_Identifier => Object_Entity, | |
5107 | Parameter_Type => New_Occurrence_Of (Typ, Loc)))); | |
5108 | ||
5109 | return Make_Subprogram_Declaration (Loc, Specification => Spec); | |
5110 | end Build_Invariant_Procedure_Declaration; | |
5111 | ||
5b5df4a9 | 5112 | ------------------------------- |
5113 | -- Build_Invariant_Procedure -- | |
5114 | ------------------------------- | |
5115 | ||
5116 | -- The procedure that is constructed here has the form | |
5117 | ||
5118 | -- procedure typInvariant (Ixxx : typ) is | |
5119 | -- begin | |
5120 | -- pragma Check (Invariant, exp, "failed invariant from xxx"); | |
5121 | -- pragma Check (Invariant, exp, "failed invariant from xxx"); | |
5122 | -- ... | |
5123 | -- pragma Check (Invariant, exp, "failed inherited invariant from xxx"); | |
5124 | -- ... | |
5125 | -- end typInvariant; | |
5126 | ||
87f3d5d3 | 5127 | procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is |
5b5df4a9 | 5128 | Loc : constant Source_Ptr := Sloc (Typ); |
5129 | Stmts : List_Id; | |
5130 | Spec : Node_Id; | |
5131 | SId : Entity_Id; | |
87f3d5d3 | 5132 | PDecl : Node_Id; |
5133 | PBody : Node_Id; | |
5134 | ||
5135 | Visible_Decls : constant List_Id := Visible_Declarations (N); | |
5136 | Private_Decls : constant List_Id := Private_Declarations (N); | |
5b5df4a9 | 5137 | |
5138 | procedure Add_Invariants (T : Entity_Id; Inherit : Boolean); | |
5139 | -- Appends statements to Stmts for any invariants in the rep item chain | |
5140 | -- of the given type. If Inherit is False, then we only process entries | |
5141 | -- on the chain for the type Typ. If Inherit is True, then we ignore any | |
5142 | -- Invariant aspects, but we process all Invariant'Class aspects, adding | |
5143 | -- "inherited" to the exception message and generating an informational | |
5144 | -- message about the inheritance of an invariant. | |
5145 | ||
9ea61fdd | 5146 | Object_Name : Name_Id; |
5b5df4a9 | 5147 | -- Name for argument of invariant procedure |
5148 | ||
9ea61fdd | 5149 | Object_Entity : Node_Id; |
5150 | -- The entity of the formal for the procedure | |
87f3d5d3 | 5151 | |
5b5df4a9 | 5152 | -------------------- |
5153 | -- Add_Invariants -- | |
5154 | -------------------- | |
5155 | ||
5156 | procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is | |
5157 | Ritem : Node_Id; | |
5158 | Arg1 : Node_Id; | |
5159 | Arg2 : Node_Id; | |
5160 | Arg3 : Node_Id; | |
5161 | Exp : Node_Id; | |
5162 | Loc : Source_Ptr; | |
5163 | Assoc : List_Id; | |
5164 | Str : String_Id; | |
5165 | ||
2072eaa9 | 5166 | procedure Replace_Type_Reference (N : Node_Id); |
5167 | -- Replace a single occurrence N of the subtype name with a reference | |
5168 | -- to the formal of the predicate function. N can be an identifier | |
5169 | -- referencing the subtype, or a selected component, representing an | |
5170 | -- appropriately qualified occurrence of the subtype name. | |
5b5df4a9 | 5171 | |
2072eaa9 | 5172 | procedure Replace_Type_References is |
5173 | new Replace_Type_References_Generic (Replace_Type_Reference); | |
5174 | -- Traverse an expression replacing all occurrences of the subtype | |
5175 | -- name with appropriate references to the object that is the formal | |
87f3d5d3 | 5176 | -- parameter of the predicate function. Note that we must ensure |
5177 | -- that the type and entity information is properly set in the | |
5178 | -- replacement node, since we will do a Preanalyze call of this | |
5179 | -- expression without proper visibility of the procedure argument. | |
5b5df4a9 | 5180 | |
2072eaa9 | 5181 | ---------------------------- |
5182 | -- Replace_Type_Reference -- | |
5183 | ---------------------------- | |
5b5df4a9 | 5184 | |
b9e61b2a | 5185 | -- Note: See comments in Add_Predicates.Replace_Type_Reference |
5186 | -- regarding handling of Sloc and Comes_From_Source. | |
5187 | ||
2072eaa9 | 5188 | procedure Replace_Type_Reference (N : Node_Id) is |
5b5df4a9 | 5189 | begin |
2072eaa9 | 5190 | -- Invariant'Class, replace with T'Class (obj) |
5191 | ||
5192 | if Class_Present (Ritem) then | |
5193 | Rewrite (N, | |
c92e878b | 5194 | Make_Type_Conversion (Sloc (N), |
2072eaa9 | 5195 | Subtype_Mark => |
c92e878b | 5196 | Make_Attribute_Reference (Sloc (N), |
5197 | Prefix => New_Occurrence_Of (T, Sloc (N)), | |
2072eaa9 | 5198 | Attribute_Name => Name_Class), |
c92e878b | 5199 | Expression => Make_Identifier (Sloc (N), Object_Name))); |
5b5df4a9 | 5200 | |
87f3d5d3 | 5201 | Set_Entity (Expression (N), Object_Entity); |
5202 | Set_Etype (Expression (N), Typ); | |
5203 | ||
2072eaa9 | 5204 | -- Invariant, replace with obj |
5b5df4a9 | 5205 | |
5206 | else | |
c92e878b | 5207 | Rewrite (N, Make_Identifier (Sloc (N), Object_Name)); |
87f3d5d3 | 5208 | Set_Entity (N, Object_Entity); |
5209 | Set_Etype (N, Typ); | |
5b5df4a9 | 5210 | end if; |
4c1fd062 | 5211 | |
5212 | Set_Comes_From_Source (N, True); | |
2072eaa9 | 5213 | end Replace_Type_Reference; |
5b5df4a9 | 5214 | |
5215 | -- Start of processing for Add_Invariants | |
5216 | ||
5217 | begin | |
5218 | Ritem := First_Rep_Item (T); | |
5219 | while Present (Ritem) loop | |
5220 | if Nkind (Ritem) = N_Pragma | |
5221 | and then Pragma_Name (Ritem) = Name_Invariant | |
5222 | then | |
5223 | Arg1 := First (Pragma_Argument_Associations (Ritem)); | |
5224 | Arg2 := Next (Arg1); | |
5225 | Arg3 := Next (Arg2); | |
5226 | ||
5227 | Arg1 := Get_Pragma_Arg (Arg1); | |
5228 | Arg2 := Get_Pragma_Arg (Arg2); | |
5229 | ||
5230 | -- For Inherit case, ignore Invariant, process only Class case | |
5231 | ||
5232 | if Inherit then | |
5233 | if not Class_Present (Ritem) then | |
5234 | goto Continue; | |
5235 | end if; | |
5236 | ||
5237 | -- For Inherit false, process only item for right type | |
5238 | ||
5239 | else | |
5240 | if Entity (Arg1) /= Typ then | |
5241 | goto Continue; | |
5242 | end if; | |
5243 | end if; | |
5244 | ||
5245 | if No (Stmts) then | |
5246 | Stmts := Empty_List; | |
5247 | end if; | |
5248 | ||
5249 | Exp := New_Copy_Tree (Arg2); | |
47a46747 | 5250 | |
88254da4 | 5251 | -- Preserve sloc of original pragma Invariant |
47a46747 | 5252 | |
5253 | Loc := Sloc (Ritem); | |
5b5df4a9 | 5254 | |
5255 | -- We need to replace any occurrences of the name of the type | |
5256 | -- with references to the object, converted to type'Class in | |
2072eaa9 | 5257 | -- the case of Invariant'Class aspects. |
5b5df4a9 | 5258 | |
2072eaa9 | 5259 | Replace_Type_References (Exp, Chars (T)); |
5b5df4a9 | 5260 | |
fb7f2fc4 | 5261 | -- If this invariant comes from an aspect, find the aspect |
5262 | -- specification, and replace the saved expression because | |
5263 | -- we need the subtype references replaced for the calls to | |
5264 | -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point | |
5265 | -- and Check_Aspect_At_End_Of_Declarations. | |
5266 | ||
5267 | if From_Aspect_Specification (Ritem) then | |
5268 | declare | |
5269 | Aitem : Node_Id; | |
5270 | ||
5271 | begin | |
5272 | -- Loop to find corresponding aspect, note that this | |
5273 | -- must be present given the pragma is marked delayed. | |
5274 | ||
5275 | Aitem := Next_Rep_Item (Ritem); | |
5276 | while Present (Aitem) loop | |
5277 | if Nkind (Aitem) = N_Aspect_Specification | |
5278 | and then Aspect_Rep_Item (Aitem) = Ritem | |
5279 | then | |
5280 | Set_Entity | |
5281 | (Identifier (Aitem), New_Copy_Tree (Exp)); | |
5282 | exit; | |
5283 | end if; | |
5284 | ||
5285 | Aitem := Next_Rep_Item (Aitem); | |
5286 | end loop; | |
5287 | end; | |
5288 | end if; | |
5289 | ||
87f3d5d3 | 5290 | -- Now we need to preanalyze the expression to properly capture |
5291 | -- the visibility in the visible part. The expression will not | |
5292 | -- be analyzed for real until the body is analyzed, but that is | |
5293 | -- at the end of the private part and has the wrong visibility. | |
5294 | ||
5295 | Set_Parent (Exp, N); | |
d513339a | 5296 | Preanalyze_Assert_Expression (Exp, Standard_Boolean); |
87f3d5d3 | 5297 | |
5b5df4a9 | 5298 | -- Build first two arguments for Check pragma |
5299 | ||
5300 | Assoc := New_List ( | |
5301 | Make_Pragma_Argument_Association (Loc, | |
55868293 | 5302 | Expression => Make_Identifier (Loc, Name_Invariant)), |
b9e61b2a | 5303 | Make_Pragma_Argument_Association (Loc, |
5304 | Expression => Exp)); | |
5b5df4a9 | 5305 | |
5306 | -- Add message if present in Invariant pragma | |
5307 | ||
5308 | if Present (Arg3) then | |
5309 | Str := Strval (Get_Pragma_Arg (Arg3)); | |
5310 | ||
5311 | -- If inherited case, and message starts "failed invariant", | |
5312 | -- change it to be "failed inherited invariant". | |
5313 | ||
5314 | if Inherit then | |
5315 | String_To_Name_Buffer (Str); | |
5316 | ||
5317 | if Name_Buffer (1 .. 16) = "failed invariant" then | |
5318 | Insert_Str_In_Name_Buffer ("inherited ", 8); | |
5319 | Str := String_From_Name_Buffer; | |
5320 | end if; | |
5321 | end if; | |
5322 | ||
5323 | Append_To (Assoc, | |
5324 | Make_Pragma_Argument_Association (Loc, | |
5325 | Expression => Make_String_Literal (Loc, Str))); | |
5326 | end if; | |
5327 | ||
5328 | -- Add Check pragma to list of statements | |
5329 | ||
5330 | Append_To (Stmts, | |
5331 | Make_Pragma (Loc, | |
5332 | Pragma_Identifier => | |
55868293 | 5333 | Make_Identifier (Loc, Name_Check), |
5b5df4a9 | 5334 | Pragma_Argument_Associations => Assoc)); |
5335 | ||
5336 | -- If Inherited case and option enabled, output info msg. Note | |
5337 | -- that we know this is a case of Invariant'Class. | |
5338 | ||
5339 | if Inherit and Opt.List_Inherited_Aspects then | |
5340 | Error_Msg_Sloc := Sloc (Ritem); | |
5341 | Error_Msg_N | |
cb97ae5c | 5342 | ("?L?info: & inherits `Invariant''Class` aspect from #", |
5b5df4a9 | 5343 | Typ); |
5344 | end if; | |
5345 | end if; | |
5346 | ||
5347 | <<Continue>> | |
5348 | Next_Rep_Item (Ritem); | |
5349 | end loop; | |
5350 | end Add_Invariants; | |
5351 | ||
5352 | -- Start of processing for Build_Invariant_Procedure | |
5353 | ||
5354 | begin | |
5355 | Stmts := No_List; | |
5356 | PDecl := Empty; | |
5357 | PBody := Empty; | |
9ea61fdd | 5358 | SId := Empty; |
5359 | ||
5360 | -- If the aspect specification exists for some view of the type, the | |
5361 | -- declaration for the procedure has been created. | |
5362 | ||
5363 | if Has_Invariants (Typ) then | |
5364 | SId := Invariant_Procedure (Typ); | |
5365 | end if; | |
5366 | ||
5367 | if Present (SId) then | |
5368 | PDecl := Unit_Declaration_Node (SId); | |
5369 | ||
5370 | else | |
5371 | PDecl := Build_Invariant_Procedure_Declaration (Typ); | |
5372 | end if; | |
5373 | ||
5374 | -- Recover formal of procedure, for use in the calls to invariant | |
5375 | -- functions (including inherited ones). | |
5376 | ||
5377 | Object_Entity := | |
5378 | Defining_Identifier | |
5379 | (First (Parameter_Specifications (Specification (PDecl)))); | |
5380 | Object_Name := Chars (Object_Entity); | |
5b5df4a9 | 5381 | |
5382 | -- Add invariants for the current type | |
5383 | ||
5384 | Add_Invariants (Typ, Inherit => False); | |
5385 | ||
5386 | -- Add invariants for parent types | |
5387 | ||
5388 | declare | |
5389 | Current_Typ : Entity_Id; | |
5390 | Parent_Typ : Entity_Id; | |
5391 | ||
5392 | begin | |
5393 | Current_Typ := Typ; | |
5394 | loop | |
5395 | Parent_Typ := Etype (Current_Typ); | |
5396 | ||
5397 | if Is_Private_Type (Parent_Typ) | |
5398 | and then Present (Full_View (Base_Type (Parent_Typ))) | |
5399 | then | |
5400 | Parent_Typ := Full_View (Base_Type (Parent_Typ)); | |
5401 | end if; | |
5402 | ||
5403 | exit when Parent_Typ = Current_Typ; | |
5404 | ||
5405 | Current_Typ := Parent_Typ; | |
5406 | Add_Invariants (Current_Typ, Inherit => True); | |
5407 | end loop; | |
5408 | end; | |
5409 | ||
5b5df4a9 | 5410 | -- Build the procedure if we generated at least one Check pragma |
5411 | ||
5412 | if Stmts /= No_List then | |
9ea61fdd | 5413 | Spec := Copy_Separate_Tree (Specification (PDecl)); |
5b5df4a9 | 5414 | |
5415 | PBody := | |
5416 | Make_Subprogram_Body (Loc, | |
5417 | Specification => Spec, | |
5418 | Declarations => Empty_List, | |
5419 | Handled_Statement_Sequence => | |
5420 | Make_Handled_Sequence_Of_Statements (Loc, | |
5421 | Statements => Stmts)); | |
87f3d5d3 | 5422 | |
5423 | -- Insert procedure declaration and spec at the appropriate points. | |
9ea61fdd | 5424 | -- If declaration is already analyzed, it was processed by the |
5425 | -- generated pragma. | |
87f3d5d3 | 5426 | |
5427 | if Present (Private_Decls) then | |
5428 | ||
5429 | -- The spec goes at the end of visible declarations, but they have | |
5430 | -- already been analyzed, so we need to explicitly do the analyze. | |
5431 | ||
9ea61fdd | 5432 | if not Analyzed (PDecl) then |
5433 | Append_To (Visible_Decls, PDecl); | |
5434 | Analyze (PDecl); | |
5435 | end if; | |
87f3d5d3 | 5436 | |
5437 | -- The body goes at the end of the private declarations, which we | |
5438 | -- have not analyzed yet, so we do not need to perform an explicit | |
5439 | -- analyze call. We skip this if there are no private declarations | |
5440 | -- (this is an error that will be caught elsewhere); | |
5441 | ||
5442 | Append_To (Private_Decls, PBody); | |
192b8dab | 5443 | |
5444 | -- If the invariant appears on the full view of a type, the | |
5445 | -- analysis of the private part is complete, and we must | |
5446 | -- analyze the new body explicitly. | |
5447 | ||
5448 | if In_Private_Part (Current_Scope) then | |
5449 | Analyze (PBody); | |
5450 | end if; | |
5d3fb947 | 5451 | |
5452 | -- If there are no private declarations this may be an error that | |
5453 | -- will be diagnosed elsewhere. However, if this is a non-private | |
5454 | -- type that inherits invariants, it needs no completion and there | |
5455 | -- may be no private part. In this case insert invariant procedure | |
5456 | -- at end of current declarative list, and analyze at once, given | |
5457 | -- that the type is about to be frozen. | |
5458 | ||
5459 | elsif not Is_Private_Type (Typ) then | |
5460 | Append_To (Visible_Decls, PDecl); | |
5461 | Append_To (Visible_Decls, PBody); | |
5462 | Analyze (PDecl); | |
5463 | Analyze (PBody); | |
87f3d5d3 | 5464 | end if; |
5b5df4a9 | 5465 | end if; |
5466 | end Build_Invariant_Procedure; | |
5467 | ||
9dc88aea | 5468 | ------------------------------ |
5469 | -- Build_Predicate_Function -- | |
5470 | ------------------------------ | |
5471 | ||
7c443ae8 | 5472 | -- The procedure that is constructed here has the form: |
5473 | ||
5474 | -- function typPredicate (Ixxx : typ) return Boolean is | |
5475 | -- begin | |
5476 | -- return | |
5477 | -- exp1 and then exp2 and then ... | |
5478 | -- and then typ1Predicate (typ1 (Ixxx)) | |
5479 | -- and then typ2Predicate (typ2 (Ixxx)) | |
5480 | -- and then ...; | |
5481 | -- end typPredicate; | |
9dc88aea | 5482 | |
5483 | -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that | |
5484 | -- this is the point at which these expressions get analyzed, providing the | |
5485 | -- required delay, and typ1, typ2, are entities from which predicates are | |
5486 | -- inherited. Note that we do NOT generate Check pragmas, that's because we | |
5487 | -- use this function even if checks are off, e.g. for membership tests. | |
5488 | ||
490beba6 | 5489 | procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is |
9f727ad2 | 5490 | Loc : constant Source_Ptr := Sloc (Typ); |
5491 | Spec : Node_Id; | |
5492 | SId : Entity_Id; | |
490beba6 | 5493 | FDecl : Node_Id; |
5494 | FBody : Node_Id; | |
5495 | ||
9dc88aea | 5496 | Expr : Node_Id; |
5497 | -- This is the expression for the return statement in the function. It | |
5498 | -- is build by connecting the component predicates with AND THEN. | |
5499 | ||
5500 | procedure Add_Call (T : Entity_Id); | |
5501 | -- Includes a call to the predicate function for type T in Expr if T | |
5502 | -- has predicates and Predicate_Function (T) is non-empty. | |
5503 | ||
5504 | procedure Add_Predicates; | |
5505 | -- Appends expressions for any Predicate pragmas in the rep item chain | |
5506 | -- Typ to Expr. Note that we look only at items for this exact entity. | |
5507 | -- Inheritance of predicates for the parent type is done by calling the | |
5508 | -- Predicate_Function of the parent type, using Add_Call above. | |
5509 | ||
9dc88aea | 5510 | Object_Name : constant Name_Id := New_Internal_Name ('I'); |
5511 | -- Name for argument of Predicate procedure | |
5512 | ||
fb7f2fc4 | 5513 | Object_Entity : constant Entity_Id := |
5514 | Make_Defining_Identifier (Loc, Object_Name); | |
5515 | -- The entity for the spec entity for the argument | |
5516 | ||
ebbab42d | 5517 | Dynamic_Predicate_Present : Boolean := False; |
5518 | -- Set True if a dynamic predicate is present, results in the entire | |
5519 | -- predicate being considered dynamic even if it looks static | |
5520 | ||
5521 | Static_Predicate_Present : Node_Id := Empty; | |
5522 | -- Set to N_Pragma node for a static predicate if one is encountered. | |
5523 | ||
9dc88aea | 5524 | -------------- |
5525 | -- Add_Call -- | |
5526 | -------------- | |
5527 | ||
5528 | procedure Add_Call (T : Entity_Id) is | |
5529 | Exp : Node_Id; | |
5530 | ||
5531 | begin | |
5532 | if Present (T) and then Present (Predicate_Function (T)) then | |
5533 | Set_Has_Predicates (Typ); | |
5534 | ||
5535 | -- Build the call to the predicate function of T | |
5536 | ||
5537 | Exp := | |
5538 | Make_Predicate_Call | |
55868293 | 5539 | (T, Convert_To (T, Make_Identifier (Loc, Object_Name))); |
9dc88aea | 5540 | |
5541 | -- Add call to evolving expression, using AND THEN if needed | |
5542 | ||
5543 | if No (Expr) then | |
5544 | Expr := Exp; | |
5545 | else | |
5546 | Expr := | |
5547 | Make_And_Then (Loc, | |
5548 | Left_Opnd => Relocate_Node (Expr), | |
5549 | Right_Opnd => Exp); | |
5550 | end if; | |
5551 | ||
2f32076c | 5552 | -- Output info message on inheritance if required. Note we do not |
5553 | -- give this information for generic actual types, since it is | |
55e8372b | 5554 | -- unwelcome noise in that case in instantiations. We also |
490beba6 | 5555 | -- generally suppress the message in instantiations, and also |
5556 | -- if it involves internal names. | |
9dc88aea | 5557 | |
2f32076c | 5558 | if Opt.List_Inherited_Aspects |
5559 | and then not Is_Generic_Actual_Type (Typ) | |
55e8372b | 5560 | and then Instantiation_Depth (Sloc (Typ)) = 0 |
490beba6 | 5561 | and then not Is_Internal_Name (Chars (T)) |
5562 | and then not Is_Internal_Name (Chars (Typ)) | |
2f32076c | 5563 | then |
9dc88aea | 5564 | Error_Msg_Sloc := Sloc (Predicate_Function (T)); |
5565 | Error_Msg_Node_2 := T; | |
cb97ae5c | 5566 | Error_Msg_N ("info: & inherits predicate from & #?L?", Typ); |
9dc88aea | 5567 | end if; |
5568 | end if; | |
5569 | end Add_Call; | |
5570 | ||
5571 | -------------------- | |
5572 | -- Add_Predicates -- | |
5573 | -------------------- | |
5574 | ||
5575 | procedure Add_Predicates is | |
5576 | Ritem : Node_Id; | |
5577 | Arg1 : Node_Id; | |
5578 | Arg2 : Node_Id; | |
5579 | ||
2072eaa9 | 5580 | procedure Replace_Type_Reference (N : Node_Id); |
5581 | -- Replace a single occurrence N of the subtype name with a reference | |
5582 | -- to the formal of the predicate function. N can be an identifier | |
5583 | -- referencing the subtype, or a selected component, representing an | |
5584 | -- appropriately qualified occurrence of the subtype name. | |
9dc88aea | 5585 | |
2072eaa9 | 5586 | procedure Replace_Type_References is |
5587 | new Replace_Type_References_Generic (Replace_Type_Reference); | |
490beba6 | 5588 | -- Traverse an expression changing every occurrence of an identifier |
6fb3c314 | 5589 | -- whose name matches the name of the subtype with a reference to |
2072eaa9 | 5590 | -- the formal parameter of the predicate function. |
9dc88aea | 5591 | |
2072eaa9 | 5592 | ---------------------------- |
5593 | -- Replace_Type_Reference -- | |
5594 | ---------------------------- | |
490beba6 | 5595 | |
2072eaa9 | 5596 | procedure Replace_Type_Reference (N : Node_Id) is |
9dc88aea | 5597 | begin |
c92e878b | 5598 | Rewrite (N, Make_Identifier (Sloc (N), Object_Name)); |
5599 | -- Use the Sloc of the usage name, not the defining name | |
5600 | ||
fb7f2fc4 | 5601 | Set_Entity (N, Object_Entity); |
5602 | Set_Etype (N, Typ); | |
4c1fd062 | 5603 | |
5604 | -- We want to treat the node as if it comes from source, so that | |
5605 | -- ASIS will not ignore it | |
5606 | ||
5607 | Set_Comes_From_Source (N, True); | |
2072eaa9 | 5608 | end Replace_Type_Reference; |
9dc88aea | 5609 | |
5610 | -- Start of processing for Add_Predicates | |
5611 | ||
5612 | begin | |
5613 | Ritem := First_Rep_Item (Typ); | |
5614 | while Present (Ritem) loop | |
5615 | if Nkind (Ritem) = N_Pragma | |
5616 | and then Pragma_Name (Ritem) = Name_Predicate | |
5617 | then | |
cce84b09 | 5618 | if Present (Corresponding_Aspect (Ritem)) then |
5619 | case Chars (Identifier (Corresponding_Aspect (Ritem))) is | |
5620 | when Name_Dynamic_Predicate => | |
5621 | Dynamic_Predicate_Present := True; | |
5622 | when Name_Static_Predicate => | |
5623 | Static_Predicate_Present := Ritem; | |
5624 | when others => | |
5625 | null; | |
5626 | end case; | |
ebbab42d | 5627 | end if; |
5628 | ||
fb7f2fc4 | 5629 | -- Acquire arguments |
5630 | ||
9dc88aea | 5631 | Arg1 := First (Pragma_Argument_Associations (Ritem)); |
5632 | Arg2 := Next (Arg1); | |
5633 | ||
5634 | Arg1 := Get_Pragma_Arg (Arg1); | |
5635 | Arg2 := Get_Pragma_Arg (Arg2); | |
5636 | ||
ffc2539e | 5637 | -- See if this predicate pragma is for the current type or for |
5638 | -- its full view. A predicate on a private completion is placed | |
5639 | -- on the partial view beause this is the visible entity that | |
5640 | -- is frozen. | |
9dc88aea | 5641 | |
13dc58a7 | 5642 | if Entity (Arg1) = Typ |
5643 | or else Full_View (Entity (Arg1)) = Typ | |
5644 | then | |
9dc88aea | 5645 | -- We have a match, this entry is for our subtype |
5646 | ||
fb7f2fc4 | 5647 | -- We need to replace any occurrences of the name of the |
5648 | -- type with references to the object. | |
490beba6 | 5649 | |
2072eaa9 | 5650 | Replace_Type_References (Arg2, Chars (Typ)); |
9dc88aea | 5651 | |
fb7f2fc4 | 5652 | -- If this predicate comes from an aspect, find the aspect |
5653 | -- specification, and replace the saved expression because | |
5654 | -- we need the subtype references replaced for the calls to | |
5655 | -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point | |
5656 | -- and Check_Aspect_At_End_Of_Declarations. | |
5657 | ||
5658 | if From_Aspect_Specification (Ritem) then | |
5659 | declare | |
5660 | Aitem : Node_Id; | |
5661 | ||
5662 | begin | |
5663 | -- Loop to find corresponding aspect, note that this | |
5664 | -- must be present given the pragma is marked delayed. | |
5665 | ||
5666 | Aitem := Next_Rep_Item (Ritem); | |
5667 | loop | |
5668 | if Nkind (Aitem) = N_Aspect_Specification | |
5669 | and then Aspect_Rep_Item (Aitem) = Ritem | |
5670 | then | |
5671 | Set_Entity | |
5672 | (Identifier (Aitem), New_Copy_Tree (Arg2)); | |
5673 | exit; | |
5674 | end if; | |
5675 | ||
5676 | Aitem := Next_Rep_Item (Aitem); | |
5677 | end loop; | |
5678 | end; | |
5679 | end if; | |
5680 | ||
5681 | -- Now we can add the expression | |
9dc88aea | 5682 | |
5683 | if No (Expr) then | |
5684 | Expr := Relocate_Node (Arg2); | |
5685 | ||
5686 | -- There already was a predicate, so add to it | |
5687 | ||
5688 | else | |
5689 | Expr := | |
5690 | Make_And_Then (Loc, | |
5691 | Left_Opnd => Relocate_Node (Expr), | |
5692 | Right_Opnd => Relocate_Node (Arg2)); | |
5693 | end if; | |
5694 | end if; | |
5695 | end if; | |
5696 | ||
5697 | Next_Rep_Item (Ritem); | |
5698 | end loop; | |
5699 | end Add_Predicates; | |
5700 | ||
d97beb2f | 5701 | -- Start of processing for Build_Predicate_Function |
9dc88aea | 5702 | |
d97beb2f | 5703 | begin |
5704 | -- Initialize for construction of statement list | |
5705 | ||
ebbab42d | 5706 | Expr := Empty; |
d97beb2f | 5707 | |
5708 | -- Return if already built or if type does not have predicates | |
5709 | ||
5710 | if not Has_Predicates (Typ) | |
5711 | or else Present (Predicate_Function (Typ)) | |
5712 | then | |
5713 | return; | |
5714 | end if; | |
5715 | ||
5716 | -- Add Predicates for the current type | |
5717 | ||
5718 | Add_Predicates; | |
5719 | ||
5720 | -- Add predicates for ancestor if present | |
5721 | ||
5722 | declare | |
5723 | Atyp : constant Entity_Id := Nearest_Ancestor (Typ); | |
5724 | begin | |
5725 | if Present (Atyp) then | |
5726 | Add_Call (Atyp); | |
5727 | end if; | |
5728 | end; | |
5729 | ||
5730 | -- If we have predicates, build the function | |
5731 | ||
5732 | if Present (Expr) then | |
5733 | ||
d97beb2f | 5734 | -- Build function declaration |
5735 | ||
d97beb2f | 5736 | SId := |
5737 | Make_Defining_Identifier (Loc, | |
5738 | Chars => New_External_Name (Chars (Typ), "Predicate")); | |
5739 | Set_Has_Predicates (SId); | |
9ea61fdd | 5740 | Set_Ekind (SId, E_Function); |
d97beb2f | 5741 | Set_Predicate_Function (Typ, SId); |
9dc88aea | 5742 | |
9f269bd8 | 5743 | -- The predicate function is shared between views of a type. |
5744 | ||
30fe3fdc | 5745 | if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then |
9f269bd8 | 5746 | Set_Predicate_Function (Full_View (Typ), SId); |
5747 | end if; | |
5748 | ||
d97beb2f | 5749 | Spec := |
5750 | Make_Function_Specification (Loc, | |
5751 | Defining_Unit_Name => SId, | |
5752 | Parameter_Specifications => New_List ( | |
5753 | Make_Parameter_Specification (Loc, | |
fb7f2fc4 | 5754 | Defining_Identifier => Object_Entity, |
d97beb2f | 5755 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), |
5756 | Result_Definition => | |
5757 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
9dc88aea | 5758 | |
490beba6 | 5759 | FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec); |
9dc88aea | 5760 | |
d97beb2f | 5761 | -- Build function body |
5762 | ||
5763 | SId := | |
5764 | Make_Defining_Identifier (Loc, | |
5765 | Chars => New_External_Name (Chars (Typ), "Predicate")); | |
5766 | ||
5767 | Spec := | |
5768 | Make_Function_Specification (Loc, | |
5769 | Defining_Unit_Name => SId, | |
5770 | Parameter_Specifications => New_List ( | |
5771 | Make_Parameter_Specification (Loc, | |
5772 | Defining_Identifier => | |
55868293 | 5773 | Make_Defining_Identifier (Loc, Object_Name), |
d97beb2f | 5774 | Parameter_Type => |
5775 | New_Occurrence_Of (Typ, Loc))), | |
5776 | Result_Definition => | |
5777 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
5778 | ||
5779 | FBody := | |
5780 | Make_Subprogram_Body (Loc, | |
5781 | Specification => Spec, | |
5782 | Declarations => Empty_List, | |
5783 | Handled_Statement_Sequence => | |
5784 | Make_Handled_Sequence_Of_Statements (Loc, | |
5785 | Statements => New_List ( | |
5786 | Make_Simple_Return_Statement (Loc, | |
5787 | Expression => Expr)))); | |
490beba6 | 5788 | |
5789 | -- Insert declaration before freeze node and body after | |
5790 | ||
5791 | Insert_Before_And_Analyze (N, FDecl); | |
5792 | Insert_After_And_Analyze (N, FBody); | |
5793 | ||
5794 | -- Deal with static predicate case | |
5795 | ||
5796 | if Ekind_In (Typ, E_Enumeration_Subtype, | |
5797 | E_Modular_Integer_Subtype, | |
5798 | E_Signed_Integer_Subtype) | |
5799 | and then Is_Static_Subtype (Typ) | |
ebbab42d | 5800 | and then not Dynamic_Predicate_Present |
490beba6 | 5801 | then |
5802 | Build_Static_Predicate (Typ, Expr, Object_Name); | |
ebbab42d | 5803 | |
5804 | if Present (Static_Predicate_Present) | |
5805 | and No (Static_Predicate (Typ)) | |
5806 | then | |
5807 | Error_Msg_F | |
5808 | ("expression does not have required form for " | |
5809 | & "static predicate", | |
5810 | Next (First (Pragma_Argument_Associations | |
5811 | (Static_Predicate_Present)))); | |
5812 | end if; | |
490beba6 | 5813 | end if; |
d97beb2f | 5814 | end if; |
5815 | end Build_Predicate_Function; | |
5816 | ||
5817 | ---------------------------- | |
5818 | -- Build_Static_Predicate -- | |
5819 | ---------------------------- | |
5820 | ||
5821 | procedure Build_Static_Predicate | |
5822 | (Typ : Entity_Id; | |
5823 | Expr : Node_Id; | |
5824 | Nam : Name_Id) | |
5825 | is | |
5826 | Loc : constant Source_Ptr := Sloc (Expr); | |
5827 | ||
5828 | Non_Static : exception; | |
5829 | -- Raised if something non-static is found | |
5830 | ||
d7c2851f | 5831 | Btyp : constant Entity_Id := Base_Type (Typ); |
5832 | ||
5833 | BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp)); | |
5834 | BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp)); | |
5835 | -- Low bound and high bound value of base type of Typ | |
5836 | ||
5837 | TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ)); | |
5838 | THi : constant Uint := Expr_Value (Type_High_Bound (Typ)); | |
5839 | -- Low bound and high bound values of static subtype Typ | |
d97beb2f | 5840 | |
5841 | type REnt is record | |
9dc88aea | 5842 | Lo, Hi : Uint; |
d97beb2f | 5843 | end record; |
5844 | -- One entry in a Rlist value, a single REnt (range entry) value | |
5845 | -- denotes one range from Lo to Hi. To represent a single value | |
5846 | -- range Lo = Hi = value. | |
5847 | ||
5848 | type RList is array (Nat range <>) of REnt; | |
5849 | -- A list of ranges. The ranges are sorted in increasing order, | |
5850 | -- and are disjoint (there is a gap of at least one value between | |
d7c2851f | 5851 | -- each range in the table). A value is in the set of ranges in |
5852 | -- Rlist if it lies within one of these ranges | |
d97beb2f | 5853 | |
d7c2851f | 5854 | False_Range : constant RList := |
5855 | RList'(1 .. 0 => REnt'(No_Uint, No_Uint)); | |
5856 | -- An empty set of ranges represents a range list that can never be | |
5857 | -- satisfied, since there are no ranges in which the value could lie, | |
5858 | -- so it does not lie in any of them. False_Range is a canonical value | |
5859 | -- for this empty set, but general processing should test for an Rlist | |
5860 | -- with length zero (see Is_False predicate), since other null ranges | |
5861 | -- may appear which must be treated as False. | |
d97beb2f | 5862 | |
d7c2851f | 5863 | True_Range : constant RList := RList'(1 => REnt'(BLo, BHi)); |
5864 | -- Range representing True, value must be in the base range | |
d97beb2f | 5865 | |
5866 | function "and" (Left, Right : RList) return RList; | |
5867 | -- And's together two range lists, returning a range list. This is | |
5868 | -- a set intersection operation. | |
5869 | ||
5870 | function "or" (Left, Right : RList) return RList; | |
5871 | -- Or's together two range lists, returning a range list. This is a | |
5872 | -- set union operation. | |
5873 | ||
5874 | function "not" (Right : RList) return RList; | |
5875 | -- Returns complement of a given range list, i.e. a range list | |
5876 | -- representing all the values in TLo .. THi that are not in the | |
5877 | -- input operand Right. | |
5878 | ||
5879 | function Build_Val (V : Uint) return Node_Id; | |
5880 | -- Return an analyzed N_Identifier node referencing this value, suitable | |
d7c2851f | 5881 | -- for use as an entry in the Static_Predicate list. This node is typed |
5882 | -- with the base type. | |
d97beb2f | 5883 | |
5884 | function Build_Range (Lo, Hi : Uint) return Node_Id; | |
5885 | -- Return an analyzed N_Range node referencing this range, suitable | |
d7c2851f | 5886 | -- for use as an entry in the Static_Predicate list. This node is typed |
5887 | -- with the base type. | |
d97beb2f | 5888 | |
5889 | function Get_RList (Exp : Node_Id) return RList; | |
5890 | -- This is a recursive routine that converts the given expression into | |
5891 | -- a list of ranges, suitable for use in building the static predicate. | |
5892 | ||
d7c2851f | 5893 | function Is_False (R : RList) return Boolean; |
5894 | pragma Inline (Is_False); | |
5895 | -- Returns True if the given range list is empty, and thus represents | |
6fb3c314 | 5896 | -- a False list of ranges that can never be satisfied. |
d7c2851f | 5897 | |
5898 | function Is_True (R : RList) return Boolean; | |
5899 | -- Returns True if R trivially represents the True predicate by having | |
5900 | -- a single range from BLo to BHi. | |
5901 | ||
d97beb2f | 5902 | function Is_Type_Ref (N : Node_Id) return Boolean; |
5903 | pragma Inline (Is_Type_Ref); | |
5904 | -- Returns if True if N is a reference to the type for the predicate in | |
5905 | -- the expression (i.e. if it is an identifier whose Chars field matches | |
5906 | -- the Nam given in the call). | |
5907 | ||
5908 | function Lo_Val (N : Node_Id) return Uint; | |
5909 | -- Given static expression or static range from a Static_Predicate list, | |
5910 | -- gets expression value or low bound of range. | |
5911 | ||
5912 | function Hi_Val (N : Node_Id) return Uint; | |
5913 | -- Given static expression or static range from a Static_Predicate list, | |
5914 | -- gets expression value of high bound of range. | |
5915 | ||
5916 | function Membership_Entry (N : Node_Id) return RList; | |
5917 | -- Given a single membership entry (range, value, or subtype), returns | |
5918 | -- the corresponding range list. Raises Static_Error if not static. | |
5919 | ||
5920 | function Membership_Entries (N : Node_Id) return RList; | |
5921 | -- Given an element on an alternatives list of a membership operation, | |
5922 | -- returns the range list corresponding to this entry and all following | |
5923 | -- entries (i.e. returns the "or" of this list of values). | |
5924 | ||
5925 | function Stat_Pred (Typ : Entity_Id) return RList; | |
5926 | -- Given a type, if it has a static predicate, then return the predicate | |
5927 | -- as a range list, otherwise raise Non_Static. | |
5928 | ||
5929 | ----------- | |
5930 | -- "and" -- | |
5931 | ----------- | |
5932 | ||
5933 | function "and" (Left, Right : RList) return RList is | |
5934 | FEnt : REnt; | |
5935 | -- First range of result | |
5936 | ||
5937 | SLeft : Nat := Left'First; | |
5938 | -- Start of rest of left entries | |
5939 | ||
5940 | SRight : Nat := Right'First; | |
5941 | -- Start of rest of right entries | |
9dc88aea | 5942 | |
d97beb2f | 5943 | begin |
5944 | -- If either range is True, return the other | |
9dc88aea | 5945 | |
d7c2851f | 5946 | if Is_True (Left) then |
d97beb2f | 5947 | return Right; |
d7c2851f | 5948 | elsif Is_True (Right) then |
d97beb2f | 5949 | return Left; |
5950 | end if; | |
9dc88aea | 5951 | |
d97beb2f | 5952 | -- If either range is False, return False |
9dc88aea | 5953 | |
d7c2851f | 5954 | if Is_False (Left) or else Is_False (Right) then |
d97beb2f | 5955 | return False_Range; |
5956 | end if; | |
9dc88aea | 5957 | |
d97beb2f | 5958 | -- Loop to remove entries at start that are disjoint, and thus |
5959 | -- just get discarded from the result entirely. | |
9dc88aea | 5960 | |
d97beb2f | 5961 | loop |
5962 | -- If no operands left in either operand, result is false | |
9dc88aea | 5963 | |
d97beb2f | 5964 | if SLeft > Left'Last or else SRight > Right'Last then |
5965 | return False_Range; | |
9dc88aea | 5966 | |
d97beb2f | 5967 | -- Discard first left operand entry if disjoint with right |
9dc88aea | 5968 | |
d97beb2f | 5969 | elsif Left (SLeft).Hi < Right (SRight).Lo then |
5970 | SLeft := SLeft + 1; | |
9dc88aea | 5971 | |
d97beb2f | 5972 | -- Discard first right operand entry if disjoint with left |
9dc88aea | 5973 | |
d97beb2f | 5974 | elsif Right (SRight).Hi < Left (SLeft).Lo then |
5975 | SRight := SRight + 1; | |
9dc88aea | 5976 | |
d97beb2f | 5977 | -- Otherwise we have an overlapping entry |
9dc88aea | 5978 | |
d97beb2f | 5979 | else |
5980 | exit; | |
5981 | end if; | |
5982 | end loop; | |
9dc88aea | 5983 | |
d97beb2f | 5984 | -- Now we have two non-null operands, and first entries overlap. |
5985 | -- The first entry in the result will be the overlapping part of | |
5986 | -- these two entries. | |
9dc88aea | 5987 | |
d97beb2f | 5988 | FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo), |
5989 | Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi)); | |
9dc88aea | 5990 | |
d97beb2f | 5991 | -- Now we can remove the entry that ended at a lower value, since |
5992 | -- its contribution is entirely contained in Fent. | |
5993 | ||
5994 | if Left (SLeft).Hi <= Right (SRight).Hi then | |
5995 | SLeft := SLeft + 1; | |
5996 | else | |
5997 | SRight := SRight + 1; | |
5998 | end if; | |
5999 | ||
d7c2851f | 6000 | -- Compute result by concatenating this first entry with the "and" |
6001 | -- of the remaining parts of the left and right operands. Note that | |
6002 | -- if either of these is empty, "and" will yield empty, so that we | |
6003 | -- will end up with just Fent, which is what we want in that case. | |
d97beb2f | 6004 | |
d7c2851f | 6005 | return |
6006 | FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last)); | |
d97beb2f | 6007 | end "and"; |
6008 | ||
6009 | ----------- | |
6010 | -- "not" -- | |
6011 | ----------- | |
6012 | ||
6013 | function "not" (Right : RList) return RList is | |
6014 | begin | |
6015 | -- Return True if False range | |
6016 | ||
d7c2851f | 6017 | if Is_False (Right) then |
d97beb2f | 6018 | return True_Range; |
6019 | end if; | |
6020 | ||
6021 | -- Return False if True range | |
6022 | ||
d7c2851f | 6023 | if Is_True (Right) then |
d97beb2f | 6024 | return False_Range; |
6025 | end if; | |
6026 | ||
6027 | -- Here if not trivial case | |
6028 | ||
6029 | declare | |
6030 | Result : RList (1 .. Right'Length + 1); | |
6031 | -- May need one more entry for gap at beginning and end | |
6032 | ||
6033 | Count : Nat := 0; | |
6034 | -- Number of entries stored in Result | |
6035 | ||
6036 | begin | |
6037 | -- Gap at start | |
6038 | ||
6039 | if Right (Right'First).Lo > TLo then | |
6040 | Count := Count + 1; | |
6041 | Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1); | |
6042 | end if; | |
6043 | ||
6044 | -- Gaps between ranges | |
6045 | ||
6046 | for J in Right'First .. Right'Last - 1 loop | |
6047 | Count := Count + 1; | |
6048 | Result (Count) := | |
6049 | REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1); | |
6050 | end loop; | |
6051 | ||
6052 | -- Gap at end | |
6053 | ||
6054 | if Right (Right'Last).Hi < THi then | |
6055 | Count := Count + 1; | |
6056 | Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi); | |
6057 | end if; | |
6058 | ||
6059 | return Result (1 .. Count); | |
6060 | end; | |
6061 | end "not"; | |
6062 | ||
6063 | ---------- | |
6064 | -- "or" -- | |
6065 | ---------- | |
6066 | ||
6067 | function "or" (Left, Right : RList) return RList is | |
d7c2851f | 6068 | FEnt : REnt; |
6069 | -- First range of result | |
6070 | ||
6071 | SLeft : Nat := Left'First; | |
6072 | -- Start of rest of left entries | |
6073 | ||
6074 | SRight : Nat := Right'First; | |
6075 | -- Start of rest of right entries | |
6076 | ||
d97beb2f | 6077 | begin |
6078 | -- If either range is True, return True | |
6079 | ||
d7c2851f | 6080 | if Is_True (Left) or else Is_True (Right) then |
d97beb2f | 6081 | return True_Range; |
6082 | end if; | |
6083 | ||
d7c2851f | 6084 | -- If either range is False (empty), return the other |
9dc88aea | 6085 | |
d7c2851f | 6086 | if Is_False (Left) then |
d97beb2f | 6087 | return Right; |
d7c2851f | 6088 | elsif Is_False (Right) then |
d97beb2f | 6089 | return Left; |
6090 | end if; | |
6091 | ||
d7c2851f | 6092 | -- Initialize result first entry from left or right operand |
6093 | -- depending on which starts with the lower range. | |
d97beb2f | 6094 | |
d7c2851f | 6095 | if Left (SLeft).Lo < Right (SRight).Lo then |
6096 | FEnt := Left (SLeft); | |
6097 | SLeft := SLeft + 1; | |
6098 | else | |
6099 | FEnt := Right (SRight); | |
6100 | SRight := SRight + 1; | |
d97beb2f | 6101 | end if; |
6102 | ||
d7c2851f | 6103 | -- This loop eats ranges from left and right operands that |
6104 | -- are contiguous with the first range we are gathering. | |
9dc88aea | 6105 | |
d7c2851f | 6106 | loop |
6107 | -- Eat first entry in left operand if contiguous or | |
6108 | -- overlapped by gathered first operand of result. | |
d97beb2f | 6109 | |
d7c2851f | 6110 | if SLeft <= Left'Last |
6111 | and then Left (SLeft).Lo <= FEnt.Hi + 1 | |
6112 | then | |
6113 | FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi); | |
d97beb2f | 6114 | SLeft := SLeft + 1; |
d97beb2f | 6115 | |
6116 | -- Eat first entry in right operand if contiguous or | |
6117 | -- overlapped by gathered right operand of result. | |
6118 | ||
d7c2851f | 6119 | elsif SRight <= Right'Last |
6120 | and then Right (SRight).Lo <= FEnt.Hi + 1 | |
6121 | then | |
6122 | FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi); | |
6123 | SRight := SRight + 1; | |
d97beb2f | 6124 | |
6125 | -- All done if no more entries to eat! | |
6126 | ||
d97beb2f | 6127 | else |
d7c2851f | 6128 | exit; |
d97beb2f | 6129 | end if; |
d7c2851f | 6130 | end loop; |
6131 | ||
6132 | -- Obtain result as the first entry we just computed, concatenated | |
6133 | -- to the "or" of the remaining results (if one operand is empty, | |
6134 | -- this will just concatenate with the other | |
6135 | ||
6136 | return | |
6137 | FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last)); | |
d97beb2f | 6138 | end "or"; |
9dc88aea | 6139 | |
d97beb2f | 6140 | ----------------- |
6141 | -- Build_Range -- | |
6142 | ----------------- | |
9dc88aea | 6143 | |
d97beb2f | 6144 | function Build_Range (Lo, Hi : Uint) return Node_Id is |
6145 | Result : Node_Id; | |
6146 | begin | |
6147 | if Lo = Hi then | |
6148 | return Build_Val (Hi); | |
6149 | else | |
6150 | Result := | |
6151 | Make_Range (Loc, | |
6152 | Low_Bound => Build_Val (Lo), | |
6153 | High_Bound => Build_Val (Hi)); | |
d7c2851f | 6154 | Set_Etype (Result, Btyp); |
d97beb2f | 6155 | Set_Analyzed (Result); |
6156 | return Result; | |
6157 | end if; | |
6158 | end Build_Range; | |
9dc88aea | 6159 | |
d97beb2f | 6160 | --------------- |
6161 | -- Build_Val -- | |
6162 | --------------- | |
9dc88aea | 6163 | |
d97beb2f | 6164 | function Build_Val (V : Uint) return Node_Id is |
6165 | Result : Node_Id; | |
6166 | ||
6167 | begin | |
6168 | if Is_Enumeration_Type (Typ) then | |
6169 | Result := Get_Enum_Lit_From_Pos (Typ, V, Loc); | |
6170 | else | |
55868293 | 6171 | Result := Make_Integer_Literal (Loc, V); |
d97beb2f | 6172 | end if; |
9dc88aea | 6173 | |
d7c2851f | 6174 | Set_Etype (Result, Btyp); |
d97beb2f | 6175 | Set_Is_Static_Expression (Result); |
6176 | Set_Analyzed (Result); | |
6177 | return Result; | |
6178 | end Build_Val; | |
9dc88aea | 6179 | |
d97beb2f | 6180 | --------------- |
6181 | -- Get_RList -- | |
6182 | --------------- | |
9dc88aea | 6183 | |
d97beb2f | 6184 | function Get_RList (Exp : Node_Id) return RList is |
6185 | Op : Node_Kind; | |
6186 | Val : Uint; | |
9dc88aea | 6187 | |
d97beb2f | 6188 | begin |
6189 | -- Static expression can only be true or false | |
6190 | ||
6191 | if Is_OK_Static_Expression (Exp) then | |
6192 | ||
d7c2851f | 6193 | -- For False |
d97beb2f | 6194 | |
6195 | if Expr_Value (Exp) = 0 then | |
6196 | return False_Range; | |
d97beb2f | 6197 | else |
d7c2851f | 6198 | return True_Range; |
d97beb2f | 6199 | end if; |
6200 | end if; | |
6201 | ||
6202 | -- Otherwise test node type | |
6203 | ||
6204 | Op := Nkind (Exp); | |
6205 | ||
6206 | case Op is | |
6207 | ||
6208 | -- And | |
6209 | ||
6210 | when N_Op_And | N_And_Then => | |
6211 | return Get_RList (Left_Opnd (Exp)) | |
6212 | and | |
6213 | Get_RList (Right_Opnd (Exp)); | |
9dc88aea | 6214 | |
d97beb2f | 6215 | -- Or |
6216 | ||
6217 | when N_Op_Or | N_Or_Else => | |
6218 | return Get_RList (Left_Opnd (Exp)) | |
6219 | or | |
6220 | Get_RList (Right_Opnd (Exp)); | |
6221 | ||
6222 | -- Not | |
6223 | ||
6224 | when N_Op_Not => | |
6225 | return not Get_RList (Right_Opnd (Exp)); | |
6226 | ||
6227 | -- Comparisons of type with static value | |
6228 | ||
6229 | when N_Op_Compare => | |
b9e61b2a | 6230 | |
d97beb2f | 6231 | -- Type is left operand |
6232 | ||
6233 | if Is_Type_Ref (Left_Opnd (Exp)) | |
6234 | and then Is_OK_Static_Expression (Right_Opnd (Exp)) | |
6235 | then | |
6236 | Val := Expr_Value (Right_Opnd (Exp)); | |
6237 | ||
6238 | -- Typ is right operand | |
6239 | ||
6240 | elsif Is_Type_Ref (Right_Opnd (Exp)) | |
6241 | and then Is_OK_Static_Expression (Left_Opnd (Exp)) | |
6242 | then | |
6243 | Val := Expr_Value (Left_Opnd (Exp)); | |
6244 | ||
6245 | -- Invert sense of comparison | |
6246 | ||
6247 | case Op is | |
6248 | when N_Op_Gt => Op := N_Op_Lt; | |
6249 | when N_Op_Lt => Op := N_Op_Gt; | |
6250 | when N_Op_Ge => Op := N_Op_Le; | |
6251 | when N_Op_Le => Op := N_Op_Ge; | |
6252 | when others => null; | |
6253 | end case; | |
6254 | ||
6255 | -- Other cases are non-static | |
9dc88aea | 6256 | |
6257 | else | |
d97beb2f | 6258 | raise Non_Static; |
9dc88aea | 6259 | end if; |
9dc88aea | 6260 | |
d97beb2f | 6261 | -- Construct range according to comparison operation |
9dc88aea | 6262 | |
d97beb2f | 6263 | case Op is |
6264 | when N_Op_Eq => | |
6265 | return RList'(1 => REnt'(Val, Val)); | |
9dc88aea | 6266 | |
d97beb2f | 6267 | when N_Op_Ge => |
d7c2851f | 6268 | return RList'(1 => REnt'(Val, BHi)); |
9dc88aea | 6269 | |
d97beb2f | 6270 | when N_Op_Gt => |
d7c2851f | 6271 | return RList'(1 => REnt'(Val + 1, BHi)); |
9dc88aea | 6272 | |
d97beb2f | 6273 | when N_Op_Le => |
d7c2851f | 6274 | return RList'(1 => REnt'(BLo, Val)); |
9dc88aea | 6275 | |
d97beb2f | 6276 | when N_Op_Lt => |
d7c2851f | 6277 | return RList'(1 => REnt'(BLo, Val - 1)); |
9dc88aea | 6278 | |
d97beb2f | 6279 | when N_Op_Ne => |
d7c2851f | 6280 | return RList'(REnt'(BLo, Val - 1), |
6281 | REnt'(Val + 1, BHi)); | |
9dc88aea | 6282 | |
d97beb2f | 6283 | when others => |
6284 | raise Program_Error; | |
6285 | end case; | |
9dc88aea | 6286 | |
d97beb2f | 6287 | -- Membership (IN) |
9dc88aea | 6288 | |
d97beb2f | 6289 | when N_In => |
6290 | if not Is_Type_Ref (Left_Opnd (Exp)) then | |
6291 | raise Non_Static; | |
6292 | end if; | |
9dc88aea | 6293 | |
d97beb2f | 6294 | if Present (Right_Opnd (Exp)) then |
6295 | return Membership_Entry (Right_Opnd (Exp)); | |
6296 | else | |
6297 | return Membership_Entries (First (Alternatives (Exp))); | |
6298 | end if; | |
9dc88aea | 6299 | |
d97beb2f | 6300 | -- Negative membership (NOT IN) |
9dc88aea | 6301 | |
d97beb2f | 6302 | when N_Not_In => |
6303 | if not Is_Type_Ref (Left_Opnd (Exp)) then | |
6304 | raise Non_Static; | |
6305 | end if; | |
6306 | ||
6307 | if Present (Right_Opnd (Exp)) then | |
6308 | return not Membership_Entry (Right_Opnd (Exp)); | |
6309 | else | |
6310 | return not Membership_Entries (First (Alternatives (Exp))); | |
6311 | end if; | |
6312 | ||
6313 | -- Function call, may be call to static predicate | |
6314 | ||
6315 | when N_Function_Call => | |
6316 | if Is_Entity_Name (Name (Exp)) then | |
6317 | declare | |
6318 | Ent : constant Entity_Id := Entity (Name (Exp)); | |
6319 | begin | |
6320 | if Has_Predicates (Ent) then | |
6321 | return Stat_Pred (Etype (First_Formal (Ent))); | |
9dc88aea | 6322 | end if; |
d97beb2f | 6323 | end; |
6324 | end if; | |
9dc88aea | 6325 | |
d97beb2f | 6326 | -- Other function call cases are non-static |
9dc88aea | 6327 | |
d97beb2f | 6328 | raise Non_Static; |
9dc88aea | 6329 | |
d97beb2f | 6330 | -- Qualified expression, dig out the expression |
9dc88aea | 6331 | |
d97beb2f | 6332 | when N_Qualified_Expression => |
6333 | return Get_RList (Expression (Exp)); | |
9dc88aea | 6334 | |
d7c2851f | 6335 | -- Xor operator |
6336 | ||
6337 | when N_Op_Xor => | |
6338 | return (Get_RList (Left_Opnd (Exp)) | |
6339 | and not Get_RList (Right_Opnd (Exp))) | |
6340 | or (Get_RList (Right_Opnd (Exp)) | |
6341 | and not Get_RList (Left_Opnd (Exp))); | |
6342 | ||
d97beb2f | 6343 | -- Any other node type is non-static |
9dc88aea | 6344 | |
d97beb2f | 6345 | when others => |
6346 | raise Non_Static; | |
6347 | end case; | |
6348 | end Get_RList; | |
9dc88aea | 6349 | |
d97beb2f | 6350 | ------------ |
6351 | -- Hi_Val -- | |
6352 | ------------ | |
9dc88aea | 6353 | |
d97beb2f | 6354 | function Hi_Val (N : Node_Id) return Uint is |
9dc88aea | 6355 | begin |
d97beb2f | 6356 | if Is_Static_Expression (N) then |
6357 | return Expr_Value (N); | |
6358 | else | |
6359 | pragma Assert (Nkind (N) = N_Range); | |
6360 | return Expr_Value (High_Bound (N)); | |
9dc88aea | 6361 | end if; |
d97beb2f | 6362 | end Hi_Val; |
9dc88aea | 6363 | |
d7c2851f | 6364 | -------------- |
6365 | -- Is_False -- | |
6366 | -------------- | |
6367 | ||
6368 | function Is_False (R : RList) return Boolean is | |
6369 | begin | |
6370 | return R'Length = 0; | |
6371 | end Is_False; | |
6372 | ||
6373 | ------------- | |
6374 | -- Is_True -- | |
6375 | ------------- | |
6376 | ||
6377 | function Is_True (R : RList) return Boolean is | |
6378 | begin | |
6379 | return R'Length = 1 | |
6380 | and then R (R'First).Lo = BLo | |
6381 | and then R (R'First).Hi = BHi; | |
6382 | end Is_True; | |
6383 | ||
d97beb2f | 6384 | ----------------- |
6385 | -- Is_Type_Ref -- | |
6386 | ----------------- | |
9dc88aea | 6387 | |
d97beb2f | 6388 | function Is_Type_Ref (N : Node_Id) return Boolean is |
6389 | begin | |
6390 | return Nkind (N) = N_Identifier and then Chars (N) = Nam; | |
6391 | end Is_Type_Ref; | |
9dc88aea | 6392 | |
d97beb2f | 6393 | ------------ |
6394 | -- Lo_Val -- | |
6395 | ------------ | |
9dc88aea | 6396 | |
d97beb2f | 6397 | function Lo_Val (N : Node_Id) return Uint is |
6398 | begin | |
6399 | if Is_Static_Expression (N) then | |
6400 | return Expr_Value (N); | |
6401 | else | |
6402 | pragma Assert (Nkind (N) = N_Range); | |
6403 | return Expr_Value (Low_Bound (N)); | |
6404 | end if; | |
6405 | end Lo_Val; | |
9dc88aea | 6406 | |
d97beb2f | 6407 | ------------------------ |
6408 | -- Membership_Entries -- | |
6409 | ------------------------ | |
9dc88aea | 6410 | |
d97beb2f | 6411 | function Membership_Entries (N : Node_Id) return RList is |
6412 | begin | |
6413 | if No (Next (N)) then | |
6414 | return Membership_Entry (N); | |
9dc88aea | 6415 | else |
d97beb2f | 6416 | return Membership_Entry (N) or Membership_Entries (Next (N)); |
9dc88aea | 6417 | end if; |
d97beb2f | 6418 | end Membership_Entries; |
9dc88aea | 6419 | |
d97beb2f | 6420 | ---------------------- |
6421 | -- Membership_Entry -- | |
6422 | ---------------------- | |
9dc88aea | 6423 | |
d97beb2f | 6424 | function Membership_Entry (N : Node_Id) return RList is |
6425 | Val : Uint; | |
6426 | SLo : Uint; | |
6427 | SHi : Uint; | |
9dc88aea | 6428 | |
d97beb2f | 6429 | begin |
6430 | -- Range case | |
6431 | ||
6432 | if Nkind (N) = N_Range then | |
6433 | if not Is_Static_Expression (Low_Bound (N)) | |
6434 | or else | |
6435 | not Is_Static_Expression (High_Bound (N)) | |
6436 | then | |
6437 | raise Non_Static; | |
6438 | else | |
6439 | SLo := Expr_Value (Low_Bound (N)); | |
6440 | SHi := Expr_Value (High_Bound (N)); | |
6441 | return RList'(1 => REnt'(SLo, SHi)); | |
9dc88aea | 6442 | end if; |
6443 | ||
d97beb2f | 6444 | -- Static expression case |
9dc88aea | 6445 | |
d97beb2f | 6446 | elsif Is_Static_Expression (N) then |
6447 | Val := Expr_Value (N); | |
6448 | return RList'(1 => REnt'(Val, Val)); | |
9dc88aea | 6449 | |
d97beb2f | 6450 | -- Identifier (other than static expression) case |
9dc88aea | 6451 | |
d97beb2f | 6452 | else pragma Assert (Nkind (N) = N_Identifier); |
9dc88aea | 6453 | |
d97beb2f | 6454 | -- Type case |
55e8372b | 6455 | |
d97beb2f | 6456 | if Is_Type (Entity (N)) then |
55e8372b | 6457 | |
d97beb2f | 6458 | -- If type has predicates, process them |
55e8372b | 6459 | |
d97beb2f | 6460 | if Has_Predicates (Entity (N)) then |
6461 | return Stat_Pred (Entity (N)); | |
55e8372b | 6462 | |
d97beb2f | 6463 | -- For static subtype without predicates, get range |
55e8372b | 6464 | |
d97beb2f | 6465 | elsif Is_Static_Subtype (Entity (N)) then |
6466 | SLo := Expr_Value (Type_Low_Bound (Entity (N))); | |
6467 | SHi := Expr_Value (Type_High_Bound (Entity (N))); | |
6468 | return RList'(1 => REnt'(SLo, SHi)); | |
6469 | ||
6470 | -- Any other type makes us non-static | |
55e8372b | 6471 | |
d97beb2f | 6472 | else |
6473 | raise Non_Static; | |
6474 | end if; | |
55e8372b | 6475 | |
d97beb2f | 6476 | -- Any other kind of identifier in predicate (e.g. a non-static |
6477 | -- expression value) means this is not a static predicate. | |
55e8372b | 6478 | |
55e8372b | 6479 | else |
d97beb2f | 6480 | raise Non_Static; |
55e8372b | 6481 | end if; |
d97beb2f | 6482 | end if; |
6483 | end Membership_Entry; | |
9dc88aea | 6484 | |
d97beb2f | 6485 | --------------- |
6486 | -- Stat_Pred -- | |
6487 | --------------- | |
9dc88aea | 6488 | |
d97beb2f | 6489 | function Stat_Pred (Typ : Entity_Id) return RList is |
6490 | begin | |
6491 | -- Not static if type does not have static predicates | |
9dc88aea | 6492 | |
b9e61b2a | 6493 | if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then |
d97beb2f | 6494 | raise Non_Static; |
6495 | end if; | |
9dc88aea | 6496 | |
d97beb2f | 6497 | -- Otherwise we convert the predicate list to a range list |
9dc88aea | 6498 | |
d97beb2f | 6499 | declare |
6500 | Result : RList (1 .. List_Length (Static_Predicate (Typ))); | |
6501 | P : Node_Id; | |
6502 | ||
6503 | begin | |
6504 | P := First (Static_Predicate (Typ)); | |
6505 | for J in Result'Range loop | |
6506 | Result (J) := REnt'(Lo_Val (P), Hi_Val (P)); | |
6507 | Next (P); | |
6508 | end loop; | |
6509 | ||
6510 | return Result; | |
6511 | end; | |
6512 | end Stat_Pred; | |
6513 | ||
6514 | -- Start of processing for Build_Static_Predicate | |
6515 | ||
6516 | begin | |
d97beb2f | 6517 | -- Now analyze the expression to see if it is a static predicate |
9dc88aea | 6518 | |
6519 | declare | |
d97beb2f | 6520 | Ranges : constant RList := Get_RList (Expr); |
6521 | -- Range list from expression if it is static | |
6522 | ||
6523 | Plist : List_Id; | |
6524 | ||
9dc88aea | 6525 | begin |
d97beb2f | 6526 | -- Convert range list into a form for the static predicate. In the |
6527 | -- Ranges array, we just have raw ranges, these must be converted | |
6528 | -- to properly typed and analyzed static expressions or range nodes. | |
9dc88aea | 6529 | |
d7c2851f | 6530 | -- Note: here we limit ranges to the ranges of the subtype, so that |
6531 | -- a predicate is always false for values outside the subtype. That | |
6532 | -- seems fine, such values are invalid anyway, and considering them | |
6533 | -- to fail the predicate seems allowed and friendly, and furthermore | |
6534 | -- simplifies processing for case statements and loops. | |
6535 | ||
d97beb2f | 6536 | Plist := New_List; |
9dc88aea | 6537 | |
d97beb2f | 6538 | for J in Ranges'Range loop |
6539 | declare | |
d7c2851f | 6540 | Lo : Uint := Ranges (J).Lo; |
6541 | Hi : Uint := Ranges (J).Hi; | |
9dc88aea | 6542 | |
d97beb2f | 6543 | begin |
d7c2851f | 6544 | -- Ignore completely out of range entry |
6545 | ||
6546 | if Hi < TLo or else Lo > THi then | |
6547 | null; | |
6548 | ||
6549 | -- Otherwise process entry | |
6550 | ||
d97beb2f | 6551 | else |
d7c2851f | 6552 | -- Adjust out of range value to subtype range |
6553 | ||
6554 | if Lo < TLo then | |
6555 | Lo := TLo; | |
6556 | end if; | |
6557 | ||
6558 | if Hi > THi then | |
6559 | Hi := THi; | |
6560 | end if; | |
6561 | ||
6562 | -- Convert range into required form | |
6563 | ||
6564 | if Lo = Hi then | |
6565 | Append_To (Plist, Build_Val (Lo)); | |
6566 | else | |
6567 | Append_To (Plist, Build_Range (Lo, Hi)); | |
6568 | end if; | |
d97beb2f | 6569 | end if; |
6570 | end; | |
6571 | end loop; | |
9dc88aea | 6572 | |
d97beb2f | 6573 | -- Processing was successful and all entries were static, so now we |
6574 | -- can store the result as the predicate list. | |
9dc88aea | 6575 | |
d97beb2f | 6576 | Set_Static_Predicate (Typ, Plist); |
9dc88aea | 6577 | |
d97beb2f | 6578 | -- The processing for static predicates put the expression into |
6579 | -- canonical form as a series of ranges. It also eliminated | |
6580 | -- duplicates and collapsed and combined ranges. We might as well | |
6581 | -- replace the alternatives list of the right operand of the | |
6582 | -- membership test with the static predicate list, which will | |
6583 | -- usually be more efficient. | |
9dc88aea | 6584 | |
d97beb2f | 6585 | declare |
6586 | New_Alts : constant List_Id := New_List; | |
6587 | Old_Node : Node_Id; | |
6588 | New_Node : Node_Id; | |
9dc88aea | 6589 | |
d97beb2f | 6590 | begin |
6591 | Old_Node := First (Plist); | |
6592 | while Present (Old_Node) loop | |
6593 | New_Node := New_Copy (Old_Node); | |
9dc88aea | 6594 | |
d97beb2f | 6595 | if Nkind (New_Node) = N_Range then |
6596 | Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node))); | |
6597 | Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node))); | |
6598 | end if; | |
9dc88aea | 6599 | |
d97beb2f | 6600 | Append_To (New_Alts, New_Node); |
6601 | Next (Old_Node); | |
6602 | end loop; | |
9dc88aea | 6603 | |
d7c2851f | 6604 | -- If empty list, replace by False |
9dc88aea | 6605 | |
d97beb2f | 6606 | if Is_Empty_List (New_Alts) then |
d7c2851f | 6607 | Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc)); |
d97beb2f | 6608 | |
d7c2851f | 6609 | -- Else replace by set membership test |
d97beb2f | 6610 | |
6611 | else | |
6612 | Rewrite (Expr, | |
6613 | Make_In (Loc, | |
6614 | Left_Opnd => Make_Identifier (Loc, Nam), | |
6615 | Right_Opnd => Empty, | |
6616 | Alternatives => New_Alts)); | |
490beba6 | 6617 | |
6618 | -- Resolve new expression in function context | |
6619 | ||
6620 | Install_Formals (Predicate_Function (Typ)); | |
6621 | Push_Scope (Predicate_Function (Typ)); | |
6622 | Analyze_And_Resolve (Expr, Standard_Boolean); | |
6623 | Pop_Scope; | |
d97beb2f | 6624 | end if; |
6625 | end; | |
6626 | end; | |
6627 | ||
6628 | -- If non-static, return doing nothing | |
6629 | ||
6630 | exception | |
6631 | when Non_Static => | |
6632 | return; | |
6633 | end Build_Static_Predicate; | |
9dc88aea | 6634 | |
7d20685d | 6635 | ----------------------------------------- |
6636 | -- Check_Aspect_At_End_Of_Declarations -- | |
6637 | ----------------------------------------- | |
6638 | ||
6639 | procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is | |
6640 | Ent : constant Entity_Id := Entity (ASN); | |
6641 | Ident : constant Node_Id := Identifier (ASN); | |
89f1e35c | 6642 | A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident)); |
7d20685d | 6643 | |
6644 | End_Decl_Expr : constant Node_Id := Entity (Ident); | |
6645 | -- Expression to be analyzed at end of declarations | |
6646 | ||
89f1e35c | 6647 | Freeze_Expr : constant Node_Id := Expression (ASN); |
6648 | -- Expression from call to Check_Aspect_At_Freeze_Point | |
6649 | ||
7d20685d | 6650 | T : constant Entity_Id := Etype (Freeze_Expr); |
6651 | -- Type required for preanalyze call | |
6652 | ||
7d20685d | 6653 | Err : Boolean; |
6654 | -- Set False if error | |
6655 | ||
6656 | -- On entry to this procedure, Entity (Ident) contains a copy of the | |
6657 | -- original expression from the aspect, saved for this purpose, and | |
6658 | -- but Expression (Ident) is a preanalyzed copy of the expression, | |
6659 | -- preanalyzed just after the freeze point. | |
6660 | ||
84ed7523 | 6661 | procedure Check_Overloaded_Name; |
6662 | -- For aspects whose expression is simply a name, this routine checks if | |
6663 | -- the name is overloaded or not. If so, it verifies there is an | |
6664 | -- interpretation that matches the entity obtained at the freeze point, | |
6665 | -- otherwise the compiler complains. | |
89f1e35c | 6666 | |
84ed7523 | 6667 | --------------------------- |
6668 | -- Check_Overloaded_Name -- | |
6669 | --------------------------- | |
2b184b2f | 6670 | |
84ed7523 | 6671 | procedure Check_Overloaded_Name is |
6672 | begin | |
2b184b2f | 6673 | if not Is_Overloaded (End_Decl_Expr) then |
6674 | Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr); | |
6675 | ||
6676 | else | |
6677 | Err := True; | |
6678 | ||
6679 | declare | |
6680 | Index : Interp_Index; | |
6681 | It : Interp; | |
6682 | ||
6683 | begin | |
6684 | Get_First_Interp (End_Decl_Expr, Index, It); | |
6685 | while Present (It.Typ) loop | |
6686 | if It.Nam = Entity (Freeze_Expr) then | |
6687 | Err := False; | |
6688 | exit; | |
6689 | end if; | |
6690 | ||
6691 | Get_Next_Interp (Index, It); | |
6692 | end loop; | |
6693 | end; | |
6694 | end if; | |
84ed7523 | 6695 | end Check_Overloaded_Name; |
6696 | ||
6697 | -- Start of processing for Check_Aspect_At_End_Of_Declarations | |
6698 | ||
6699 | begin | |
6700 | -- Case of aspects Dimension, Dimension_System and Synchronization | |
6701 | ||
6702 | if A_Id = Aspect_Synchronization then | |
6703 | return; | |
6704 | ||
6705 | -- Case of stream attributes, just have to compare entities. However, | |
6706 | -- the expression is just a name (possibly overloaded), and there may | |
6707 | -- be stream operations declared for unrelated types, so we just need | |
6708 | -- to verify that one of these interpretations is the one available at | |
6709 | -- at the freeze point. | |
6710 | ||
6711 | elsif A_Id = Aspect_Input or else | |
6712 | A_Id = Aspect_Output or else | |
6713 | A_Id = Aspect_Read or else | |
6714 | A_Id = Aspect_Write | |
6715 | then | |
6716 | Analyze (End_Decl_Expr); | |
6717 | Check_Overloaded_Name; | |
7d20685d | 6718 | |
81b424ac | 6719 | elsif A_Id = Aspect_Variable_Indexing or else |
89cc7147 | 6720 | A_Id = Aspect_Constant_Indexing or else |
6721 | A_Id = Aspect_Default_Iterator or else | |
6722 | A_Id = Aspect_Iterator_Element | |
81b424ac | 6723 | then |
aabafdc2 | 6724 | -- Make type unfrozen before analysis, to prevent spurious errors |
6725 | -- about late attributes. | |
59f3e675 | 6726 | |
6727 | Set_Is_Frozen (Ent, False); | |
81b424ac | 6728 | Analyze (End_Decl_Expr); |
59f3e675 | 6729 | Set_Is_Frozen (Ent, True); |
89cc7147 | 6730 | |
6731 | -- If the end of declarations comes before any other freeze | |
6732 | -- point, the Freeze_Expr is not analyzed: no check needed. | |
6733 | ||
6b34a333 | 6734 | if Analyzed (Freeze_Expr) and then not In_Instance then |
84ed7523 | 6735 | Check_Overloaded_Name; |
84ed7523 | 6736 | else |
6737 | Err := False; | |
6738 | end if; | |
81b424ac | 6739 | |
7d20685d | 6740 | -- All other cases |
6741 | ||
6742 | else | |
d67a83fe | 6743 | -- In a generic context the aspect expressions have not been |
4055a532 | 6744 | -- preanalyzed, so do it now. There are no conformance checks |
6745 | -- to perform in this case. | |
309c3053 | 6746 | |
d67a83fe | 6747 | if No (T) then |
309c3053 | 6748 | Check_Aspect_At_Freeze_Point (ASN); |
6749 | return; | |
7b9b2f05 | 6750 | |
6751 | -- The default values attributes may be defined in the private part, | |
6752 | -- and the analysis of the expression may take place when only the | |
6753 | -- partial view is visible. The expression must be scalar, so use | |
6754 | -- the full view to resolve. | |
6755 | ||
30fe3fdc | 6756 | elsif (A_Id = Aspect_Default_Value |
6757 | or else | |
7b9b2f05 | 6758 | A_Id = Aspect_Default_Component_Value) |
6759 | and then Is_Private_Type (T) | |
6760 | then | |
6761 | Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T)); | |
309c3053 | 6762 | else |
6763 | Preanalyze_Spec_Expression (End_Decl_Expr, T); | |
6764 | end if; | |
6765 | ||
7d20685d | 6766 | Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr); |
6767 | end if; | |
6768 | ||
6769 | -- Output error message if error | |
6770 | ||
6771 | if Err then | |
6772 | Error_Msg_NE | |
6773 | ("visibility of aspect for& changes after freeze point", | |
6774 | ASN, Ent); | |
6775 | Error_Msg_NE | |
1e3532e7 | 6776 | ("info: & is frozen here, aspects evaluated at this point??", |
7d20685d | 6777 | Freeze_Node (Ent), Ent); |
6778 | end if; | |
6779 | end Check_Aspect_At_End_Of_Declarations; | |
6780 | ||
6781 | ---------------------------------- | |
6782 | -- Check_Aspect_At_Freeze_Point -- | |
6783 | ---------------------------------- | |
6784 | ||
6785 | procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is | |
6786 | Ident : constant Node_Id := Identifier (ASN); | |
6787 | -- Identifier (use Entity field to save expression) | |
6788 | ||
7d20685d | 6789 | A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident)); |
6790 | ||
17631aa0 | 6791 | T : Entity_Id := Empty; |
89f1e35c | 6792 | -- Type required for preanalyze call |
6793 | ||
7d20685d | 6794 | begin |
6795 | -- On entry to this procedure, Entity (Ident) contains a copy of the | |
6796 | -- original expression from the aspect, saved for this purpose. | |
6797 | ||
6798 | -- On exit from this procedure Entity (Ident) is unchanged, still | |
6799 | -- containing that copy, but Expression (Ident) is a preanalyzed copy | |
6800 | -- of the expression, preanalyzed just after the freeze point. | |
6801 | ||
c92e878b | 6802 | -- Make a copy of the expression to be preanalyzed |
7d20685d | 6803 | |
6804 | Set_Expression (ASN, New_Copy_Tree (Entity (Ident))); | |
6805 | ||
6806 | -- Find type for preanalyze call | |
6807 | ||
6808 | case A_Id is | |
6809 | ||
6810 | -- No_Aspect should be impossible | |
6811 | ||
6812 | when No_Aspect => | |
6813 | raise Program_Error; | |
6814 | ||
17631aa0 | 6815 | -- Aspects taking an optional boolean argument |
6c545057 | 6816 | |
89f1e35c | 6817 | when Boolean_Aspects | |
6818 | Library_Unit_Aspects => | |
6819 | T := Standard_Boolean; | |
6c545057 | 6820 | |
7f694ca2 | 6821 | when Aspect_Attach_Handler => |
6822 | T := RTE (RE_Interrupt_ID); | |
6823 | ||
e1cedbae | 6824 | when Aspect_Convention => |
89f1e35c | 6825 | return; |
e1cedbae | 6826 | |
231eb581 | 6827 | -- Default_Value is resolved with the type entity in question |
8398ba2c | 6828 | |
231eb581 | 6829 | when Aspect_Default_Value => |
8398ba2c | 6830 | T := Entity (ASN); |
7d20685d | 6831 | |
231eb581 | 6832 | -- Default_Component_Value is resolved with the component type |
6833 | ||
6834 | when Aspect_Default_Component_Value => | |
6835 | T := Component_Type (Entity (ASN)); | |
6836 | ||
7d20685d | 6837 | -- Aspects corresponding to attribute definition clauses |
6838 | ||
8398ba2c | 6839 | when Aspect_Address => |
7d20685d | 6840 | T := RTE (RE_Address); |
6841 | ||
b7b74740 | 6842 | when Aspect_Bit_Order | Aspect_Scalar_Storage_Order => |
7d20685d | 6843 | T := RTE (RE_Bit_Order); |
6844 | ||
cb4c311d | 6845 | when Aspect_CPU => |
6846 | T := RTE (RE_CPU_Range); | |
6847 | ||
a7a4a7c2 | 6848 | when Aspect_Dispatching_Domain => |
6849 | T := RTE (RE_Dispatching_Domain); | |
6850 | ||
7d20685d | 6851 | when Aspect_External_Tag => |
6852 | T := Standard_String; | |
6853 | ||
e1cedbae | 6854 | when Aspect_External_Name => |
6855 | T := Standard_String; | |
6856 | ||
6857 | when Aspect_Link_Name => | |
6858 | T := Standard_String; | |
6859 | ||
7f694ca2 | 6860 | when Aspect_Priority | Aspect_Interrupt_Priority => |
6861 | T := Standard_Integer; | |
6862 | ||
47a46747 | 6863 | when Aspect_Relative_Deadline => |
6864 | T := RTE (RE_Time_Span); | |
6865 | ||
7f694ca2 | 6866 | when Aspect_Small => |
6867 | T := Universal_Real; | |
6868 | ||
b55f7641 | 6869 | -- For a simple storage pool, we have to retrieve the type of the |
6870 | -- pool object associated with the aspect's corresponding attribute | |
6871 | -- definition clause. | |
6872 | ||
6873 | when Aspect_Simple_Storage_Pool => | |
6874 | T := Etype (Expression (Aspect_Rep_Item (ASN))); | |
6875 | ||
7d20685d | 6876 | when Aspect_Storage_Pool => |
6877 | T := Class_Wide_Type (RTE (RE_Root_Storage_Pool)); | |
6878 | ||
6c545057 | 6879 | when Aspect_Alignment | |
7d20685d | 6880 | Aspect_Component_Size | |
6881 | Aspect_Machine_Radix | | |
6882 | Aspect_Object_Size | | |
6883 | Aspect_Size | | |
6884 | Aspect_Storage_Size | | |
6885 | Aspect_Stream_Size | | |
6886 | Aspect_Value_Size => | |
6887 | T := Any_Integer; | |
6888 | ||
89f1e35c | 6889 | when Aspect_Synchronization => |
6890 | return; | |
6891 | ||
6892 | -- Special case, the expression of these aspects is just an entity | |
7d20685d | 6893 | -- that does not need any resolution, so just analyze. |
6894 | ||
17631aa0 | 6895 | when Aspect_Input | |
6896 | Aspect_Output | | |
6897 | Aspect_Read | | |
6898 | Aspect_Suppress | | |
6899 | Aspect_Unsuppress | | |
6900 | Aspect_Warnings | | |
6901 | Aspect_Write => | |
7d20685d | 6902 | Analyze (Expression (ASN)); |
81b424ac | 6903 | return; |
6904 | ||
6905 | -- Same for Iterator aspects, where the expression is a function | |
6906 | -- name. Legality rules are checked separately. | |
6907 | ||
17631aa0 | 6908 | when Aspect_Constant_Indexing | |
6909 | Aspect_Default_Iterator | | |
6910 | Aspect_Iterator_Element | | |
6911 | Aspect_Variable_Indexing => | |
81b424ac | 6912 | Analyze (Expression (ASN)); |
7d20685d | 6913 | return; |
6914 | ||
89f1e35c | 6915 | -- Invariant/Predicate take boolean expressions |
7d20685d | 6916 | |
ebbab42d | 6917 | when Aspect_Dynamic_Predicate | |
6918 | Aspect_Invariant | | |
ebbab42d | 6919 | Aspect_Predicate | |
77ae6789 | 6920 | Aspect_Static_Predicate | |
6921 | Aspect_Type_Invariant => | |
7d20685d | 6922 | T := Standard_Boolean; |
85696508 | 6923 | |
89f1e35c | 6924 | -- Here is the list of aspects that don't require delay analysis. |
6925 | ||
6926 | when Aspect_Contract_Case | | |
3a128918 | 6927 | Aspect_Contract_Cases | |
89f1e35c | 6928 | Aspect_Dimension | |
6929 | Aspect_Dimension_System | | |
6930 | Aspect_Implicit_Dereference | | |
6931 | Aspect_Post | | |
6932 | Aspect_Postcondition | | |
6933 | Aspect_Pre | | |
6934 | Aspect_Precondition | | |
6935 | Aspect_Test_Case => | |
85696508 | 6936 | raise Program_Error; |
6937 | ||
7d20685d | 6938 | end case; |
6939 | ||
6940 | -- Do the preanalyze call | |
6941 | ||
6942 | Preanalyze_Spec_Expression (Expression (ASN), T); | |
6943 | end Check_Aspect_At_Freeze_Point; | |
6944 | ||
d6f39728 | 6945 | ----------------------------------- |
6946 | -- Check_Constant_Address_Clause -- | |
6947 | ----------------------------------- | |
6948 | ||
6949 | procedure Check_Constant_Address_Clause | |
6950 | (Expr : Node_Id; | |
6951 | U_Ent : Entity_Id) | |
6952 | is | |
6953 | procedure Check_At_Constant_Address (Nod : Node_Id); | |
fdd294d1 | 6954 | -- Checks that the given node N represents a name whose 'Address is |
6955 | -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the | |
6956 | -- address value is the same at the point of declaration of U_Ent and at | |
6957 | -- the time of elaboration of the address clause. | |
d6f39728 | 6958 | |
6959 | procedure Check_Expr_Constants (Nod : Node_Id); | |
fdd294d1 | 6960 | -- Checks that Nod meets the requirements for a constant address clause |
6961 | -- in the sense of the enclosing procedure. | |
d6f39728 | 6962 | |
6963 | procedure Check_List_Constants (Lst : List_Id); | |
6964 | -- Check that all elements of list Lst meet the requirements for a | |
6965 | -- constant address clause in the sense of the enclosing procedure. | |
6966 | ||
6967 | ------------------------------- | |
6968 | -- Check_At_Constant_Address -- | |
6969 | ------------------------------- | |
6970 | ||
6971 | procedure Check_At_Constant_Address (Nod : Node_Id) is | |
6972 | begin | |
6973 | if Is_Entity_Name (Nod) then | |
6974 | if Present (Address_Clause (Entity ((Nod)))) then | |
6975 | Error_Msg_NE | |
6976 | ("invalid address clause for initialized object &!", | |
6977 | Nod, U_Ent); | |
6978 | Error_Msg_NE | |
6979 | ("address for& cannot" & | |
fbc67f84 | 6980 | " depend on another address clause! (RM 13.1(22))!", |
d6f39728 | 6981 | Nod, U_Ent); |
6982 | ||
6983 | elsif In_Same_Source_Unit (Entity (Nod), U_Ent) | |
6984 | and then Sloc (U_Ent) < Sloc (Entity (Nod)) | |
6985 | then | |
6986 | Error_Msg_NE | |
6987 | ("invalid address clause for initialized object &!", | |
6988 | Nod, U_Ent); | |
2f582d72 | 6989 | Error_Msg_Node_2 := U_Ent; |
6990 | Error_Msg_NE | |
6991 | ("\& must be defined before & (RM 13.1(22))!", | |
6992 | Nod, Entity (Nod)); | |
d6f39728 | 6993 | end if; |
6994 | ||
6995 | elsif Nkind (Nod) = N_Selected_Component then | |
6996 | declare | |
6997 | T : constant Entity_Id := Etype (Prefix (Nod)); | |
6998 | ||
6999 | begin | |
7000 | if (Is_Record_Type (T) | |
7001 | and then Has_Discriminants (T)) | |
7002 | or else | |
7003 | (Is_Access_Type (T) | |
7004 | and then Is_Record_Type (Designated_Type (T)) | |
7005 | and then Has_Discriminants (Designated_Type (T))) | |
7006 | then | |
7007 | Error_Msg_NE | |
7008 | ("invalid address clause for initialized object &!", | |
7009 | Nod, U_Ent); | |
7010 | Error_Msg_N | |
7011 | ("\address cannot depend on component" & | |
fbc67f84 | 7012 | " of discriminated record (RM 13.1(22))!", |
d6f39728 | 7013 | Nod); |
7014 | else | |
7015 | Check_At_Constant_Address (Prefix (Nod)); | |
7016 | end if; | |
7017 | end; | |
7018 | ||
7019 | elsif Nkind (Nod) = N_Indexed_Component then | |
7020 | Check_At_Constant_Address (Prefix (Nod)); | |
7021 | Check_List_Constants (Expressions (Nod)); | |
7022 | ||
7023 | else | |
7024 | Check_Expr_Constants (Nod); | |
7025 | end if; | |
7026 | end Check_At_Constant_Address; | |
7027 | ||
7028 | -------------------------- | |
7029 | -- Check_Expr_Constants -- | |
7030 | -------------------------- | |
7031 | ||
7032 | procedure Check_Expr_Constants (Nod : Node_Id) is | |
e7b2d6bc | 7033 | Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent); |
7034 | Ent : Entity_Id := Empty; | |
7035 | ||
d6f39728 | 7036 | begin |
7037 | if Nkind (Nod) in N_Has_Etype | |
7038 | and then Etype (Nod) = Any_Type | |
7039 | then | |
7040 | return; | |
7041 | end if; | |
7042 | ||
7043 | case Nkind (Nod) is | |
7044 | when N_Empty | N_Error => | |
7045 | return; | |
7046 | ||
7047 | when N_Identifier | N_Expanded_Name => | |
e7b2d6bc | 7048 | Ent := Entity (Nod); |
9dfe12ae | 7049 | |
7050 | -- We need to look at the original node if it is different | |
7051 | -- from the node, since we may have rewritten things and | |
7052 | -- substituted an identifier representing the rewrite. | |
7053 | ||
7054 | if Original_Node (Nod) /= Nod then | |
7055 | Check_Expr_Constants (Original_Node (Nod)); | |
7056 | ||
7057 | -- If the node is an object declaration without initial | |
7058 | -- value, some code has been expanded, and the expression | |
7059 | -- is not constant, even if the constituents might be | |
fdd294d1 | 7060 | -- acceptable, as in A'Address + offset. |
9dfe12ae | 7061 | |
e7b2d6bc | 7062 | if Ekind (Ent) = E_Variable |
fdd294d1 | 7063 | and then |
7064 | Nkind (Declaration_Node (Ent)) = N_Object_Declaration | |
9dfe12ae | 7065 | and then |
e7b2d6bc | 7066 | No (Expression (Declaration_Node (Ent))) |
7067 | then | |
7068 | Error_Msg_NE | |
7069 | ("invalid address clause for initialized object &!", | |
7070 | Nod, U_Ent); | |
7071 | ||
7072 | -- If entity is constant, it may be the result of expanding | |
7073 | -- a check. We must verify that its declaration appears | |
7074 | -- before the object in question, else we also reject the | |
7075 | -- address clause. | |
7076 | ||
7077 | elsif Ekind (Ent) = E_Constant | |
7078 | and then In_Same_Source_Unit (Ent, U_Ent) | |
7079 | and then Sloc (Ent) > Loc_U_Ent | |
9dfe12ae | 7080 | then |
7081 | Error_Msg_NE | |
7082 | ("invalid address clause for initialized object &!", | |
7083 | Nod, U_Ent); | |
7084 | end if; | |
e7b2d6bc | 7085 | |
9dfe12ae | 7086 | return; |
7087 | end if; | |
7088 | ||
2866d595 | 7089 | -- Otherwise look at the identifier and see if it is OK |
9dfe12ae | 7090 | |
d3ef794c | 7091 | if Ekind_In (Ent, E_Named_Integer, E_Named_Real) |
7092 | or else Is_Type (Ent) | |
e7b2d6bc | 7093 | then |
7094 | return; | |
d6f39728 | 7095 | |
e7b2d6bc | 7096 | elsif |
7097 | Ekind (Ent) = E_Constant | |
7098 | or else | |
7099 | Ekind (Ent) = E_In_Parameter | |
7100 | then | |
fdd294d1 | 7101 | -- This is the case where we must have Ent defined before |
7102 | -- U_Ent. Clearly if they are in different units this | |
7103 | -- requirement is met since the unit containing Ent is | |
7104 | -- already processed. | |
d6f39728 | 7105 | |
e7b2d6bc | 7106 | if not In_Same_Source_Unit (Ent, U_Ent) then |
7107 | return; | |
d6f39728 | 7108 | |
fdd294d1 | 7109 | -- Otherwise location of Ent must be before the location |
7110 | -- of U_Ent, that's what prior defined means. | |
d6f39728 | 7111 | |
e7b2d6bc | 7112 | elsif Sloc (Ent) < Loc_U_Ent then |
7113 | return; | |
d6f39728 | 7114 | |
7115 | else | |
7116 | Error_Msg_NE | |
7117 | ("invalid address clause for initialized object &!", | |
7118 | Nod, U_Ent); | |
2f582d72 | 7119 | Error_Msg_Node_2 := U_Ent; |
7120 | Error_Msg_NE | |
7121 | ("\& must be defined before & (RM 13.1(22))!", | |
7122 | Nod, Ent); | |
e7b2d6bc | 7123 | end if; |
9dfe12ae | 7124 | |
e7b2d6bc | 7125 | elsif Nkind (Original_Node (Nod)) = N_Function_Call then |
7126 | Check_Expr_Constants (Original_Node (Nod)); | |
7127 | ||
7128 | else | |
7129 | Error_Msg_NE | |
7130 | ("invalid address clause for initialized object &!", | |
7131 | Nod, U_Ent); | |
7132 | ||
7133 | if Comes_From_Source (Ent) then | |
2f582d72 | 7134 | Error_Msg_NE |
7135 | ("\reference to variable& not allowed" | |
7136 | & " (RM 13.1(22))!", Nod, Ent); | |
e7b2d6bc | 7137 | else |
7138 | Error_Msg_N | |
7139 | ("non-static expression not allowed" | |
fbc67f84 | 7140 | & " (RM 13.1(22))!", Nod); |
d6f39728 | 7141 | end if; |
e7b2d6bc | 7142 | end if; |
d6f39728 | 7143 | |
93735cb8 | 7144 | when N_Integer_Literal => |
7145 | ||
7146 | -- If this is a rewritten unchecked conversion, in a system | |
7147 | -- where Address is an integer type, always use the base type | |
7148 | -- for a literal value. This is user-friendly and prevents | |
7149 | -- order-of-elaboration issues with instances of unchecked | |
7150 | -- conversion. | |
7151 | ||
7152 | if Nkind (Original_Node (Nod)) = N_Function_Call then | |
7153 | Set_Etype (Nod, Base_Type (Etype (Nod))); | |
7154 | end if; | |
7155 | ||
7156 | when N_Real_Literal | | |
d6f39728 | 7157 | N_String_Literal | |
7158 | N_Character_Literal => | |
7159 | return; | |
7160 | ||
7161 | when N_Range => | |
7162 | Check_Expr_Constants (Low_Bound (Nod)); | |
7163 | Check_Expr_Constants (High_Bound (Nod)); | |
7164 | ||
7165 | when N_Explicit_Dereference => | |
7166 | Check_Expr_Constants (Prefix (Nod)); | |
7167 | ||
7168 | when N_Indexed_Component => | |
7169 | Check_Expr_Constants (Prefix (Nod)); | |
7170 | Check_List_Constants (Expressions (Nod)); | |
7171 | ||
7172 | when N_Slice => | |
7173 | Check_Expr_Constants (Prefix (Nod)); | |
7174 | Check_Expr_Constants (Discrete_Range (Nod)); | |
7175 | ||
7176 | when N_Selected_Component => | |
7177 | Check_Expr_Constants (Prefix (Nod)); | |
7178 | ||
7179 | when N_Attribute_Reference => | |
9dfe12ae | 7180 | if Attribute_Name (Nod) = Name_Address |
7181 | or else | |
7182 | Attribute_Name (Nod) = Name_Access | |
d6f39728 | 7183 | or else |
9dfe12ae | 7184 | Attribute_Name (Nod) = Name_Unchecked_Access |
d6f39728 | 7185 | or else |
9dfe12ae | 7186 | Attribute_Name (Nod) = Name_Unrestricted_Access |
d6f39728 | 7187 | then |
7188 | Check_At_Constant_Address (Prefix (Nod)); | |
7189 | ||
7190 | else | |
7191 | Check_Expr_Constants (Prefix (Nod)); | |
7192 | Check_List_Constants (Expressions (Nod)); | |
7193 | end if; | |
7194 | ||
7195 | when N_Aggregate => | |
7196 | Check_List_Constants (Component_Associations (Nod)); | |
7197 | Check_List_Constants (Expressions (Nod)); | |
7198 | ||
7199 | when N_Component_Association => | |
7200 | Check_Expr_Constants (Expression (Nod)); | |
7201 | ||
7202 | when N_Extension_Aggregate => | |
7203 | Check_Expr_Constants (Ancestor_Part (Nod)); | |
7204 | Check_List_Constants (Component_Associations (Nod)); | |
7205 | Check_List_Constants (Expressions (Nod)); | |
7206 | ||
7207 | when N_Null => | |
7208 | return; | |
7209 | ||
e7771556 | 7210 | when N_Binary_Op | N_Short_Circuit | N_Membership_Test => |
d6f39728 | 7211 | Check_Expr_Constants (Left_Opnd (Nod)); |
7212 | Check_Expr_Constants (Right_Opnd (Nod)); | |
7213 | ||
7214 | when N_Unary_Op => | |
7215 | Check_Expr_Constants (Right_Opnd (Nod)); | |
7216 | ||
7217 | when N_Type_Conversion | | |
7218 | N_Qualified_Expression | | |
7219 | N_Allocator => | |
7220 | Check_Expr_Constants (Expression (Nod)); | |
7221 | ||
7222 | when N_Unchecked_Type_Conversion => | |
7223 | Check_Expr_Constants (Expression (Nod)); | |
7224 | ||
fdd294d1 | 7225 | -- If this is a rewritten unchecked conversion, subtypes in |
7226 | -- this node are those created within the instance. To avoid | |
7227 | -- order of elaboration issues, replace them with their base | |
7228 | -- types. Note that address clauses can cause order of | |
7229 | -- elaboration problems because they are elaborated by the | |
7230 | -- back-end at the point of definition, and may mention | |
7231 | -- entities declared in between (as long as everything is | |
7232 | -- static). It is user-friendly to allow unchecked conversions | |
7233 | -- in this context. | |
d6f39728 | 7234 | |
7235 | if Nkind (Original_Node (Nod)) = N_Function_Call then | |
7236 | Set_Etype (Expression (Nod), | |
7237 | Base_Type (Etype (Expression (Nod)))); | |
7238 | Set_Etype (Nod, Base_Type (Etype (Nod))); | |
7239 | end if; | |
7240 | ||
7241 | when N_Function_Call => | |
7242 | if not Is_Pure (Entity (Name (Nod))) then | |
7243 | Error_Msg_NE | |
7244 | ("invalid address clause for initialized object &!", | |
7245 | Nod, U_Ent); | |
7246 | ||
7247 | Error_Msg_NE | |
fbc67f84 | 7248 | ("\function & is not pure (RM 13.1(22))!", |
d6f39728 | 7249 | Nod, Entity (Name (Nod))); |
7250 | ||
7251 | else | |
7252 | Check_List_Constants (Parameter_Associations (Nod)); | |
7253 | end if; | |
7254 | ||
7255 | when N_Parameter_Association => | |
7256 | Check_Expr_Constants (Explicit_Actual_Parameter (Nod)); | |
7257 | ||
7258 | when others => | |
7259 | Error_Msg_NE | |
7260 | ("invalid address clause for initialized object &!", | |
7261 | Nod, U_Ent); | |
7262 | Error_Msg_NE | |
fbc67f84 | 7263 | ("\must be constant defined before& (RM 13.1(22))!", |
d6f39728 | 7264 | Nod, U_Ent); |
7265 | end case; | |
7266 | end Check_Expr_Constants; | |
7267 | ||
7268 | -------------------------- | |
7269 | -- Check_List_Constants -- | |
7270 | -------------------------- | |
7271 | ||
7272 | procedure Check_List_Constants (Lst : List_Id) is | |
7273 | Nod1 : Node_Id; | |
7274 | ||
7275 | begin | |
7276 | if Present (Lst) then | |
7277 | Nod1 := First (Lst); | |
7278 | while Present (Nod1) loop | |
7279 | Check_Expr_Constants (Nod1); | |
7280 | Next (Nod1); | |
7281 | end loop; | |
7282 | end if; | |
7283 | end Check_List_Constants; | |
7284 | ||
7285 | -- Start of processing for Check_Constant_Address_Clause | |
7286 | ||
7287 | begin | |
01cb2726 | 7288 | -- If rep_clauses are to be ignored, no need for legality checks. In |
7289 | -- particular, no need to pester user about rep clauses that violate | |
7290 | -- the rule on constant addresses, given that these clauses will be | |
7291 | -- removed by Freeze before they reach the back end. | |
7292 | ||
7293 | if not Ignore_Rep_Clauses then | |
7294 | Check_Expr_Constants (Expr); | |
7295 | end if; | |
d6f39728 | 7296 | end Check_Constant_Address_Clause; |
7297 | ||
67278d60 | 7298 | ---------------------------------------- |
7299 | -- Check_Record_Representation_Clause -- | |
7300 | ---------------------------------------- | |
7301 | ||
7302 | procedure Check_Record_Representation_Clause (N : Node_Id) is | |
7303 | Loc : constant Source_Ptr := Sloc (N); | |
7304 | Ident : constant Node_Id := Identifier (N); | |
7305 | Rectype : Entity_Id; | |
7306 | Fent : Entity_Id; | |
7307 | CC : Node_Id; | |
7308 | Fbit : Uint; | |
7309 | Lbit : Uint; | |
7310 | Hbit : Uint := Uint_0; | |
7311 | Comp : Entity_Id; | |
7312 | Pcomp : Entity_Id; | |
7313 | ||
7314 | Max_Bit_So_Far : Uint; | |
7315 | -- Records the maximum bit position so far. If all field positions | |
7316 | -- are monotonically increasing, then we can skip the circuit for | |
7317 | -- checking for overlap, since no overlap is possible. | |
7318 | ||
7319 | Tagged_Parent : Entity_Id := Empty; | |
7320 | -- This is set in the case of a derived tagged type for which we have | |
7321 | -- Is_Fully_Repped_Tagged_Type True (indicating that all components are | |
7322 | -- positioned by record representation clauses). In this case we must | |
7323 | -- check for overlap between components of this tagged type, and the | |
7324 | -- components of its parent. Tagged_Parent will point to this parent | |
7325 | -- type. For all other cases Tagged_Parent is left set to Empty. | |
7326 | ||
7327 | Parent_Last_Bit : Uint; | |
7328 | -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the | |
7329 | -- last bit position for any field in the parent type. We only need to | |
7330 | -- check overlap for fields starting below this point. | |
7331 | ||
7332 | Overlap_Check_Required : Boolean; | |
7333 | -- Used to keep track of whether or not an overlap check is required | |
7334 | ||
47495553 | 7335 | Overlap_Detected : Boolean := False; |
7336 | -- Set True if an overlap is detected | |
7337 | ||
67278d60 | 7338 | Ccount : Natural := 0; |
7339 | -- Number of component clauses in record rep clause | |
7340 | ||
7341 | procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id); | |
7342 | -- Given two entities for record components or discriminants, checks | |
7343 | -- if they have overlapping component clauses and issues errors if so. | |
7344 | ||
7345 | procedure Find_Component; | |
7346 | -- Finds component entity corresponding to current component clause (in | |
7347 | -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin | |
7348 | -- start/stop bits for the field. If there is no matching component or | |
7349 | -- if the matching component does not have a component clause, then | |
7350 | -- that's an error and Comp is set to Empty, but no error message is | |
7351 | -- issued, since the message was already given. Comp is also set to | |
7352 | -- Empty if the current "component clause" is in fact a pragma. | |
7353 | ||
7354 | ----------------------------- | |
7355 | -- Check_Component_Overlap -- | |
7356 | ----------------------------- | |
7357 | ||
7358 | procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is | |
7359 | CC1 : constant Node_Id := Component_Clause (C1_Ent); | |
7360 | CC2 : constant Node_Id := Component_Clause (C2_Ent); | |
47495553 | 7361 | |
67278d60 | 7362 | begin |
7363 | if Present (CC1) and then Present (CC2) then | |
7364 | ||
7365 | -- Exclude odd case where we have two tag fields in the same | |
7366 | -- record, both at location zero. This seems a bit strange, but | |
7367 | -- it seems to happen in some circumstances, perhaps on an error. | |
7368 | ||
7369 | if Chars (C1_Ent) = Name_uTag | |
7370 | and then | |
7371 | Chars (C2_Ent) = Name_uTag | |
7372 | then | |
7373 | return; | |
7374 | end if; | |
7375 | ||
7376 | -- Here we check if the two fields overlap | |
7377 | ||
7378 | declare | |
7379 | S1 : constant Uint := Component_Bit_Offset (C1_Ent); | |
7380 | S2 : constant Uint := Component_Bit_Offset (C2_Ent); | |
7381 | E1 : constant Uint := S1 + Esize (C1_Ent); | |
7382 | E2 : constant Uint := S2 + Esize (C2_Ent); | |
7383 | ||
7384 | begin | |
7385 | if E2 <= S1 or else E1 <= S2 then | |
7386 | null; | |
7387 | else | |
7388 | Error_Msg_Node_2 := Component_Name (CC2); | |
7389 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
7390 | Error_Msg_Node_1 := Component_Name (CC1); | |
7391 | Error_Msg_N | |
7392 | ("component& overlaps & #", Component_Name (CC1)); | |
47495553 | 7393 | Overlap_Detected := True; |
67278d60 | 7394 | end if; |
7395 | end; | |
7396 | end if; | |
7397 | end Check_Component_Overlap; | |
7398 | ||
7399 | -------------------- | |
7400 | -- Find_Component -- | |
7401 | -------------------- | |
7402 | ||
7403 | procedure Find_Component is | |
7404 | ||
7405 | procedure Search_Component (R : Entity_Id); | |
7406 | -- Search components of R for a match. If found, Comp is set. | |
7407 | ||
7408 | ---------------------- | |
7409 | -- Search_Component -- | |
7410 | ---------------------- | |
7411 | ||
7412 | procedure Search_Component (R : Entity_Id) is | |
7413 | begin | |
7414 | Comp := First_Component_Or_Discriminant (R); | |
7415 | while Present (Comp) loop | |
7416 | ||
7417 | -- Ignore error of attribute name for component name (we | |
7418 | -- already gave an error message for this, so no need to | |
7419 | -- complain here) | |
7420 | ||
7421 | if Nkind (Component_Name (CC)) = N_Attribute_Reference then | |
7422 | null; | |
7423 | else | |
7424 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
7425 | end if; | |
7426 | ||
7427 | Next_Component_Or_Discriminant (Comp); | |
7428 | end loop; | |
7429 | end Search_Component; | |
7430 | ||
7431 | -- Start of processing for Find_Component | |
7432 | ||
7433 | begin | |
7434 | -- Return with Comp set to Empty if we have a pragma | |
7435 | ||
7436 | if Nkind (CC) = N_Pragma then | |
7437 | Comp := Empty; | |
7438 | return; | |
7439 | end if; | |
7440 | ||
7441 | -- Search current record for matching component | |
7442 | ||
7443 | Search_Component (Rectype); | |
7444 | ||
7445 | -- If not found, maybe component of base type that is absent from | |
7446 | -- statically constrained first subtype. | |
7447 | ||
7448 | if No (Comp) then | |
7449 | Search_Component (Base_Type (Rectype)); | |
7450 | end if; | |
7451 | ||
7452 | -- If no component, or the component does not reference the component | |
7453 | -- clause in question, then there was some previous error for which | |
7454 | -- we already gave a message, so just return with Comp Empty. | |
7455 | ||
dba36b60 | 7456 | if No (Comp) or else Component_Clause (Comp) /= CC then |
7457 | Check_Error_Detected; | |
67278d60 | 7458 | Comp := Empty; |
7459 | ||
7460 | -- Normal case where we have a component clause | |
7461 | ||
7462 | else | |
7463 | Fbit := Component_Bit_Offset (Comp); | |
7464 | Lbit := Fbit + Esize (Comp) - 1; | |
7465 | end if; | |
7466 | end Find_Component; | |
7467 | ||
7468 | -- Start of processing for Check_Record_Representation_Clause | |
7469 | ||
7470 | begin | |
7471 | Find_Type (Ident); | |
7472 | Rectype := Entity (Ident); | |
7473 | ||
7474 | if Rectype = Any_Type then | |
7475 | return; | |
7476 | else | |
7477 | Rectype := Underlying_Type (Rectype); | |
7478 | end if; | |
7479 | ||
7480 | -- See if we have a fully repped derived tagged type | |
7481 | ||
7482 | declare | |
7483 | PS : constant Entity_Id := Parent_Subtype (Rectype); | |
7484 | ||
7485 | begin | |
7486 | if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then | |
7487 | Tagged_Parent := PS; | |
7488 | ||
7489 | -- Find maximum bit of any component of the parent type | |
7490 | ||
7491 | Parent_Last_Bit := UI_From_Int (System_Address_Size - 1); | |
7492 | Pcomp := First_Entity (Tagged_Parent); | |
7493 | while Present (Pcomp) loop | |
7494 | if Ekind_In (Pcomp, E_Discriminant, E_Component) then | |
7495 | if Component_Bit_Offset (Pcomp) /= No_Uint | |
7496 | and then Known_Static_Esize (Pcomp) | |
7497 | then | |
7498 | Parent_Last_Bit := | |
7499 | UI_Max | |
7500 | (Parent_Last_Bit, | |
7501 | Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1); | |
7502 | end if; | |
7503 | ||
7504 | Next_Entity (Pcomp); | |
7505 | end if; | |
7506 | end loop; | |
7507 | end if; | |
7508 | end; | |
7509 | ||
7510 | -- All done if no component clauses | |
7511 | ||
7512 | CC := First (Component_Clauses (N)); | |
7513 | ||
7514 | if No (CC) then | |
7515 | return; | |
7516 | end if; | |
7517 | ||
7518 | -- If a tag is present, then create a component clause that places it | |
7519 | -- at the start of the record (otherwise gigi may place it after other | |
7520 | -- fields that have rep clauses). | |
7521 | ||
7522 | Fent := First_Entity (Rectype); | |
7523 | ||
7524 | if Nkind (Fent) = N_Defining_Identifier | |
7525 | and then Chars (Fent) = Name_uTag | |
7526 | then | |
7527 | Set_Component_Bit_Offset (Fent, Uint_0); | |
7528 | Set_Normalized_Position (Fent, Uint_0); | |
7529 | Set_Normalized_First_Bit (Fent, Uint_0); | |
7530 | Set_Normalized_Position_Max (Fent, Uint_0); | |
7531 | Init_Esize (Fent, System_Address_Size); | |
7532 | ||
7533 | Set_Component_Clause (Fent, | |
7534 | Make_Component_Clause (Loc, | |
55868293 | 7535 | Component_Name => Make_Identifier (Loc, Name_uTag), |
67278d60 | 7536 | |
55868293 | 7537 | Position => Make_Integer_Literal (Loc, Uint_0), |
7538 | First_Bit => Make_Integer_Literal (Loc, Uint_0), | |
67278d60 | 7539 | Last_Bit => |
7540 | Make_Integer_Literal (Loc, | |
7541 | UI_From_Int (System_Address_Size)))); | |
7542 | ||
7543 | Ccount := Ccount + 1; | |
7544 | end if; | |
7545 | ||
7546 | Max_Bit_So_Far := Uint_Minus_1; | |
7547 | Overlap_Check_Required := False; | |
7548 | ||
7549 | -- Process the component clauses | |
7550 | ||
7551 | while Present (CC) loop | |
7552 | Find_Component; | |
7553 | ||
7554 | if Present (Comp) then | |
7555 | Ccount := Ccount + 1; | |
7556 | ||
47495553 | 7557 | -- We need a full overlap check if record positions non-monotonic |
7558 | ||
67278d60 | 7559 | if Fbit <= Max_Bit_So_Far then |
7560 | Overlap_Check_Required := True; | |
67278d60 | 7561 | end if; |
7562 | ||
47495553 | 7563 | Max_Bit_So_Far := Lbit; |
7564 | ||
67278d60 | 7565 | -- Check bit position out of range of specified size |
7566 | ||
7567 | if Has_Size_Clause (Rectype) | |
ada34def | 7568 | and then RM_Size (Rectype) <= Lbit |
67278d60 | 7569 | then |
7570 | Error_Msg_N | |
7571 | ("bit number out of range of specified size", | |
7572 | Last_Bit (CC)); | |
7573 | ||
7574 | -- Check for overlap with tag field | |
7575 | ||
7576 | else | |
7577 | if Is_Tagged_Type (Rectype) | |
7578 | and then Fbit < System_Address_Size | |
7579 | then | |
7580 | Error_Msg_NE | |
7581 | ("component overlaps tag field of&", | |
7582 | Component_Name (CC), Rectype); | |
47495553 | 7583 | Overlap_Detected := True; |
67278d60 | 7584 | end if; |
7585 | ||
7586 | if Hbit < Lbit then | |
7587 | Hbit := Lbit; | |
7588 | end if; | |
7589 | end if; | |
7590 | ||
7591 | -- Check parent overlap if component might overlap parent field | |
7592 | ||
b9e61b2a | 7593 | if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then |
67278d60 | 7594 | Pcomp := First_Component_Or_Discriminant (Tagged_Parent); |
7595 | while Present (Pcomp) loop | |
7596 | if not Is_Tag (Pcomp) | |
7597 | and then Chars (Pcomp) /= Name_uParent | |
7598 | then | |
7599 | Check_Component_Overlap (Comp, Pcomp); | |
7600 | end if; | |
7601 | ||
7602 | Next_Component_Or_Discriminant (Pcomp); | |
7603 | end loop; | |
7604 | end if; | |
7605 | end if; | |
7606 | ||
7607 | Next (CC); | |
7608 | end loop; | |
7609 | ||
7610 | -- Now that we have processed all the component clauses, check for | |
7611 | -- overlap. We have to leave this till last, since the components can | |
7612 | -- appear in any arbitrary order in the representation clause. | |
7613 | ||
7614 | -- We do not need this check if all specified ranges were monotonic, | |
7615 | -- as recorded by Overlap_Check_Required being False at this stage. | |
7616 | ||
7617 | -- This first section checks if there are any overlapping entries at | |
7618 | -- all. It does this by sorting all entries and then seeing if there are | |
7619 | -- any overlaps. If there are none, then that is decisive, but if there | |
7620 | -- are overlaps, they may still be OK (they may result from fields in | |
7621 | -- different variants). | |
7622 | ||
7623 | if Overlap_Check_Required then | |
7624 | Overlap_Check1 : declare | |
7625 | ||
7626 | OC_Fbit : array (0 .. Ccount) of Uint; | |
7627 | -- First-bit values for component clauses, the value is the offset | |
7628 | -- of the first bit of the field from start of record. The zero | |
7629 | -- entry is for use in sorting. | |
7630 | ||
7631 | OC_Lbit : array (0 .. Ccount) of Uint; | |
7632 | -- Last-bit values for component clauses, the value is the offset | |
7633 | -- of the last bit of the field from start of record. The zero | |
7634 | -- entry is for use in sorting. | |
7635 | ||
7636 | OC_Count : Natural := 0; | |
7637 | -- Count of entries in OC_Fbit and OC_Lbit | |
7638 | ||
7639 | function OC_Lt (Op1, Op2 : Natural) return Boolean; | |
7640 | -- Compare routine for Sort | |
7641 | ||
7642 | procedure OC_Move (From : Natural; To : Natural); | |
7643 | -- Move routine for Sort | |
7644 | ||
7645 | package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt); | |
7646 | ||
7647 | ----------- | |
7648 | -- OC_Lt -- | |
7649 | ----------- | |
7650 | ||
7651 | function OC_Lt (Op1, Op2 : Natural) return Boolean is | |
7652 | begin | |
7653 | return OC_Fbit (Op1) < OC_Fbit (Op2); | |
7654 | end OC_Lt; | |
7655 | ||
7656 | ------------- | |
7657 | -- OC_Move -- | |
7658 | ------------- | |
7659 | ||
7660 | procedure OC_Move (From : Natural; To : Natural) is | |
7661 | begin | |
7662 | OC_Fbit (To) := OC_Fbit (From); | |
7663 | OC_Lbit (To) := OC_Lbit (From); | |
7664 | end OC_Move; | |
7665 | ||
7666 | -- Start of processing for Overlap_Check | |
7667 | ||
7668 | begin | |
7669 | CC := First (Component_Clauses (N)); | |
7670 | while Present (CC) loop | |
7671 | ||
7672 | -- Exclude component clause already marked in error | |
7673 | ||
7674 | if not Error_Posted (CC) then | |
7675 | Find_Component; | |
7676 | ||
7677 | if Present (Comp) then | |
7678 | OC_Count := OC_Count + 1; | |
7679 | OC_Fbit (OC_Count) := Fbit; | |
7680 | OC_Lbit (OC_Count) := Lbit; | |
7681 | end if; | |
7682 | end if; | |
7683 | ||
7684 | Next (CC); | |
7685 | end loop; | |
7686 | ||
7687 | Sorting.Sort (OC_Count); | |
7688 | ||
7689 | Overlap_Check_Required := False; | |
7690 | for J in 1 .. OC_Count - 1 loop | |
7691 | if OC_Lbit (J) >= OC_Fbit (J + 1) then | |
7692 | Overlap_Check_Required := True; | |
7693 | exit; | |
7694 | end if; | |
7695 | end loop; | |
7696 | end Overlap_Check1; | |
7697 | end if; | |
7698 | ||
7699 | -- If Overlap_Check_Required is still True, then we have to do the full | |
7700 | -- scale overlap check, since we have at least two fields that do | |
7701 | -- overlap, and we need to know if that is OK since they are in | |
7702 | -- different variant, or whether we have a definite problem. | |
7703 | ||
7704 | if Overlap_Check_Required then | |
7705 | Overlap_Check2 : declare | |
7706 | C1_Ent, C2_Ent : Entity_Id; | |
7707 | -- Entities of components being checked for overlap | |
7708 | ||
7709 | Clist : Node_Id; | |
7710 | -- Component_List node whose Component_Items are being checked | |
7711 | ||
7712 | Citem : Node_Id; | |
7713 | -- Component declaration for component being checked | |
7714 | ||
7715 | begin | |
7716 | C1_Ent := First_Entity (Base_Type (Rectype)); | |
7717 | ||
7718 | -- Loop through all components in record. For each component check | |
7719 | -- for overlap with any of the preceding elements on the component | |
7720 | -- list containing the component and also, if the component is in | |
7721 | -- a variant, check against components outside the case structure. | |
7722 | -- This latter test is repeated recursively up the variant tree. | |
7723 | ||
7724 | Main_Component_Loop : while Present (C1_Ent) loop | |
7725 | if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then | |
7726 | goto Continue_Main_Component_Loop; | |
7727 | end if; | |
7728 | ||
7729 | -- Skip overlap check if entity has no declaration node. This | |
7730 | -- happens with discriminants in constrained derived types. | |
47495553 | 7731 | -- Possibly we are missing some checks as a result, but that |
7732 | -- does not seem terribly serious. | |
67278d60 | 7733 | |
7734 | if No (Declaration_Node (C1_Ent)) then | |
7735 | goto Continue_Main_Component_Loop; | |
7736 | end if; | |
7737 | ||
7738 | Clist := Parent (List_Containing (Declaration_Node (C1_Ent))); | |
7739 | ||
7740 | -- Loop through component lists that need checking. Check the | |
7741 | -- current component list and all lists in variants above us. | |
7742 | ||
7743 | Component_List_Loop : loop | |
7744 | ||
7745 | -- If derived type definition, go to full declaration | |
7746 | -- If at outer level, check discriminants if there are any. | |
7747 | ||
7748 | if Nkind (Clist) = N_Derived_Type_Definition then | |
7749 | Clist := Parent (Clist); | |
7750 | end if; | |
7751 | ||
7752 | -- Outer level of record definition, check discriminants | |
7753 | ||
7754 | if Nkind_In (Clist, N_Full_Type_Declaration, | |
b9e61b2a | 7755 | N_Private_Type_Declaration) |
67278d60 | 7756 | then |
7757 | if Has_Discriminants (Defining_Identifier (Clist)) then | |
7758 | C2_Ent := | |
7759 | First_Discriminant (Defining_Identifier (Clist)); | |
7760 | while Present (C2_Ent) loop | |
7761 | exit when C1_Ent = C2_Ent; | |
7762 | Check_Component_Overlap (C1_Ent, C2_Ent); | |
7763 | Next_Discriminant (C2_Ent); | |
7764 | end loop; | |
7765 | end if; | |
7766 | ||
7767 | -- Record extension case | |
7768 | ||
7769 | elsif Nkind (Clist) = N_Derived_Type_Definition then | |
7770 | Clist := Empty; | |
7771 | ||
7772 | -- Otherwise check one component list | |
7773 | ||
7774 | else | |
7775 | Citem := First (Component_Items (Clist)); | |
67278d60 | 7776 | while Present (Citem) loop |
7777 | if Nkind (Citem) = N_Component_Declaration then | |
7778 | C2_Ent := Defining_Identifier (Citem); | |
7779 | exit when C1_Ent = C2_Ent; | |
7780 | Check_Component_Overlap (C1_Ent, C2_Ent); | |
7781 | end if; | |
7782 | ||
7783 | Next (Citem); | |
7784 | end loop; | |
7785 | end if; | |
7786 | ||
7787 | -- Check for variants above us (the parent of the Clist can | |
7788 | -- be a variant, in which case its parent is a variant part, | |
7789 | -- and the parent of the variant part is a component list | |
7790 | -- whose components must all be checked against the current | |
7791 | -- component for overlap). | |
7792 | ||
7793 | if Nkind (Parent (Clist)) = N_Variant then | |
7794 | Clist := Parent (Parent (Parent (Clist))); | |
7795 | ||
7796 | -- Check for possible discriminant part in record, this | |
7797 | -- is treated essentially as another level in the | |
7798 | -- recursion. For this case the parent of the component | |
7799 | -- list is the record definition, and its parent is the | |
7800 | -- full type declaration containing the discriminant | |
7801 | -- specifications. | |
7802 | ||
7803 | elsif Nkind (Parent (Clist)) = N_Record_Definition then | |
7804 | Clist := Parent (Parent ((Clist))); | |
7805 | ||
7806 | -- If neither of these two cases, we are at the top of | |
7807 | -- the tree. | |
7808 | ||
7809 | else | |
7810 | exit Component_List_Loop; | |
7811 | end if; | |
7812 | end loop Component_List_Loop; | |
7813 | ||
7814 | <<Continue_Main_Component_Loop>> | |
7815 | Next_Entity (C1_Ent); | |
7816 | ||
7817 | end loop Main_Component_Loop; | |
7818 | end Overlap_Check2; | |
7819 | end if; | |
7820 | ||
47495553 | 7821 | -- The following circuit deals with warning on record holes (gaps). We |
7822 | -- skip this check if overlap was detected, since it makes sense for the | |
7823 | -- programmer to fix this illegality before worrying about warnings. | |
7824 | ||
7825 | if not Overlap_Detected and Warn_On_Record_Holes then | |
7826 | Record_Hole_Check : declare | |
7827 | Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype)); | |
7828 | -- Full declaration of record type | |
7829 | ||
7830 | procedure Check_Component_List | |
7831 | (CL : Node_Id; | |
7832 | Sbit : Uint; | |
7833 | DS : List_Id); | |
7834 | -- Check component list CL for holes. The starting bit should be | |
7835 | -- Sbit. which is zero for the main record component list and set | |
7836 | -- appropriately for recursive calls for variants. DS is set to | |
7837 | -- a list of discriminant specifications to be included in the | |
7838 | -- consideration of components. It is No_List if none to consider. | |
7839 | ||
7840 | -------------------------- | |
7841 | -- Check_Component_List -- | |
7842 | -------------------------- | |
7843 | ||
7844 | procedure Check_Component_List | |
7845 | (CL : Node_Id; | |
7846 | Sbit : Uint; | |
7847 | DS : List_Id) | |
7848 | is | |
7849 | Compl : Integer; | |
7850 | ||
7851 | begin | |
7852 | Compl := Integer (List_Length (Component_Items (CL))); | |
7853 | ||
7854 | if DS /= No_List then | |
7855 | Compl := Compl + Integer (List_Length (DS)); | |
7856 | end if; | |
7857 | ||
7858 | declare | |
7859 | Comps : array (Natural range 0 .. Compl) of Entity_Id; | |
7860 | -- Gather components (zero entry is for sort routine) | |
7861 | ||
7862 | Ncomps : Natural := 0; | |
7863 | -- Number of entries stored in Comps (starting at Comps (1)) | |
7864 | ||
7865 | Citem : Node_Id; | |
7866 | -- One component item or discriminant specification | |
7867 | ||
7868 | Nbit : Uint; | |
7869 | -- Starting bit for next component | |
7870 | ||
7871 | CEnt : Entity_Id; | |
7872 | -- Component entity | |
7873 | ||
7874 | Variant : Node_Id; | |
7875 | -- One variant | |
7876 | ||
7877 | function Lt (Op1, Op2 : Natural) return Boolean; | |
7878 | -- Compare routine for Sort | |
7879 | ||
7880 | procedure Move (From : Natural; To : Natural); | |
7881 | -- Move routine for Sort | |
7882 | ||
7883 | package Sorting is new GNAT.Heap_Sort_G (Move, Lt); | |
7884 | ||
7885 | -------- | |
7886 | -- Lt -- | |
7887 | -------- | |
7888 | ||
7889 | function Lt (Op1, Op2 : Natural) return Boolean is | |
7890 | begin | |
7891 | return Component_Bit_Offset (Comps (Op1)) | |
7892 | < | |
7893 | Component_Bit_Offset (Comps (Op2)); | |
7894 | end Lt; | |
7895 | ||
7896 | ---------- | |
7897 | -- Move -- | |
7898 | ---------- | |
7899 | ||
7900 | procedure Move (From : Natural; To : Natural) is | |
7901 | begin | |
7902 | Comps (To) := Comps (From); | |
7903 | end Move; | |
7904 | ||
7905 | begin | |
7906 | -- Gather discriminants into Comp | |
7907 | ||
7908 | if DS /= No_List then | |
7909 | Citem := First (DS); | |
7910 | while Present (Citem) loop | |
7911 | if Nkind (Citem) = N_Discriminant_Specification then | |
7912 | declare | |
7913 | Ent : constant Entity_Id := | |
7914 | Defining_Identifier (Citem); | |
7915 | begin | |
7916 | if Ekind (Ent) = E_Discriminant then | |
7917 | Ncomps := Ncomps + 1; | |
7918 | Comps (Ncomps) := Ent; | |
7919 | end if; | |
7920 | end; | |
7921 | end if; | |
7922 | ||
7923 | Next (Citem); | |
7924 | end loop; | |
7925 | end if; | |
7926 | ||
7927 | -- Gather component entities into Comp | |
7928 | ||
7929 | Citem := First (Component_Items (CL)); | |
7930 | while Present (Citem) loop | |
7931 | if Nkind (Citem) = N_Component_Declaration then | |
7932 | Ncomps := Ncomps + 1; | |
7933 | Comps (Ncomps) := Defining_Identifier (Citem); | |
7934 | end if; | |
7935 | ||
7936 | Next (Citem); | |
7937 | end loop; | |
7938 | ||
7939 | -- Now sort the component entities based on the first bit. | |
7940 | -- Note we already know there are no overlapping components. | |
7941 | ||
7942 | Sorting.Sort (Ncomps); | |
7943 | ||
7944 | -- Loop through entries checking for holes | |
7945 | ||
7946 | Nbit := Sbit; | |
7947 | for J in 1 .. Ncomps loop | |
7948 | CEnt := Comps (J); | |
7949 | Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit; | |
7950 | ||
7951 | if Error_Msg_Uint_1 > 0 then | |
7952 | Error_Msg_NE | |
1e3532e7 | 7953 | ("?H?^-bit gap before component&", |
47495553 | 7954 | Component_Name (Component_Clause (CEnt)), CEnt); |
7955 | end if; | |
7956 | ||
7957 | Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt); | |
7958 | end loop; | |
7959 | ||
7960 | -- Process variant parts recursively if present | |
7961 | ||
7962 | if Present (Variant_Part (CL)) then | |
7963 | Variant := First (Variants (Variant_Part (CL))); | |
7964 | while Present (Variant) loop | |
7965 | Check_Component_List | |
7966 | (Component_List (Variant), Nbit, No_List); | |
7967 | Next (Variant); | |
7968 | end loop; | |
7969 | end if; | |
7970 | end; | |
7971 | end Check_Component_List; | |
7972 | ||
7973 | -- Start of processing for Record_Hole_Check | |
7974 | ||
7975 | begin | |
7976 | declare | |
7977 | Sbit : Uint; | |
7978 | ||
7979 | begin | |
7980 | if Is_Tagged_Type (Rectype) then | |
7981 | Sbit := UI_From_Int (System_Address_Size); | |
7982 | else | |
7983 | Sbit := Uint_0; | |
7984 | end if; | |
7985 | ||
7986 | if Nkind (Decl) = N_Full_Type_Declaration | |
7987 | and then Nkind (Type_Definition (Decl)) = N_Record_Definition | |
7988 | then | |
7989 | Check_Component_List | |
7990 | (Component_List (Type_Definition (Decl)), | |
7991 | Sbit, | |
7992 | Discriminant_Specifications (Decl)); | |
7993 | end if; | |
7994 | end; | |
7995 | end Record_Hole_Check; | |
7996 | end if; | |
7997 | ||
67278d60 | 7998 | -- For records that have component clauses for all components, and whose |
7999 | -- size is less than or equal to 32, we need to know the size in the | |
8000 | -- front end to activate possible packed array processing where the | |
8001 | -- component type is a record. | |
8002 | ||
8003 | -- At this stage Hbit + 1 represents the first unused bit from all the | |
8004 | -- component clauses processed, so if the component clauses are | |
8005 | -- complete, then this is the length of the record. | |
8006 | ||
8007 | -- For records longer than System.Storage_Unit, and for those where not | |
8008 | -- all components have component clauses, the back end determines the | |
8009 | -- length (it may for example be appropriate to round up the size | |
8010 | -- to some convenient boundary, based on alignment considerations, etc). | |
8011 | ||
8012 | if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then | |
8013 | ||
8014 | -- Nothing to do if at least one component has no component clause | |
8015 | ||
8016 | Comp := First_Component_Or_Discriminant (Rectype); | |
8017 | while Present (Comp) loop | |
8018 | exit when No (Component_Clause (Comp)); | |
8019 | Next_Component_Or_Discriminant (Comp); | |
8020 | end loop; | |
8021 | ||
8022 | -- If we fall out of loop, all components have component clauses | |
8023 | -- and so we can set the size to the maximum value. | |
8024 | ||
8025 | if No (Comp) then | |
8026 | Set_RM_Size (Rectype, Hbit + 1); | |
8027 | end if; | |
8028 | end if; | |
8029 | end Check_Record_Representation_Clause; | |
8030 | ||
d6f39728 | 8031 | ---------------- |
8032 | -- Check_Size -- | |
8033 | ---------------- | |
8034 | ||
8035 | procedure Check_Size | |
8036 | (N : Node_Id; | |
8037 | T : Entity_Id; | |
8038 | Siz : Uint; | |
8039 | Biased : out Boolean) | |
8040 | is | |
8041 | UT : constant Entity_Id := Underlying_Type (T); | |
8042 | M : Uint; | |
8043 | ||
8044 | begin | |
8045 | Biased := False; | |
8046 | ||
f117057b | 8047 | -- Reject patently improper size values. |
8048 | ||
e2f8db26 | 8049 | if Is_Elementary_Type (T) |
f117057b | 8050 | and then Siz > UI_From_Int (Int'Last) |
8051 | then | |
e2f8db26 | 8052 | Error_Msg_N ("Size value too large for elementary type", N); |
1f526845 | 8053 | |
f117057b | 8054 | if Nkind (Original_Node (N)) = N_Op_Expon then |
8055 | Error_Msg_N | |
8056 | ("\maybe '* was meant, rather than '*'*", Original_Node (N)); | |
8057 | end if; | |
8058 | end if; | |
8059 | ||
dba36b60 | 8060 | -- Dismiss generic types |
d6f39728 | 8061 | |
dba36b60 | 8062 | if Is_Generic_Type (T) |
8063 | or else | |
8064 | Is_Generic_Type (UT) | |
8065 | or else | |
8066 | Is_Generic_Type (Root_Type (UT)) | |
d6f39728 | 8067 | then |
8068 | return; | |
8069 | ||
dba36b60 | 8070 | -- Guard against previous errors |
8071 | ||
8072 | elsif No (UT) or else UT = Any_Type then | |
8073 | Check_Error_Detected; | |
8074 | return; | |
8075 | ||
ea61a7ea | 8076 | -- Check case of bit packed array |
8077 | ||
8078 | elsif Is_Array_Type (UT) | |
8079 | and then Known_Static_Component_Size (UT) | |
8080 | and then Is_Bit_Packed_Array (UT) | |
8081 | then | |
8082 | declare | |
8083 | Asiz : Uint; | |
8084 | Indx : Node_Id; | |
8085 | Ityp : Entity_Id; | |
8086 | ||
8087 | begin | |
8088 | Asiz := Component_Size (UT); | |
8089 | Indx := First_Index (UT); | |
8090 | loop | |
8091 | Ityp := Etype (Indx); | |
8092 | ||
8093 | -- If non-static bound, then we are not in the business of | |
8094 | -- trying to check the length, and indeed an error will be | |
8095 | -- issued elsewhere, since sizes of non-static array types | |
8096 | -- cannot be set implicitly or explicitly. | |
8097 | ||
8098 | if not Is_Static_Subtype (Ityp) then | |
8099 | return; | |
8100 | end if; | |
8101 | ||
8102 | -- Otherwise accumulate next dimension | |
8103 | ||
8104 | Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) - | |
8105 | Expr_Value (Type_Low_Bound (Ityp)) + | |
8106 | Uint_1); | |
8107 | ||
8108 | Next_Index (Indx); | |
8109 | exit when No (Indx); | |
8110 | end loop; | |
8111 | ||
8112 | if Asiz <= Siz then | |
8113 | return; | |
b9e61b2a | 8114 | |
ea61a7ea | 8115 | else |
8116 | Error_Msg_Uint_1 := Asiz; | |
8117 | Error_Msg_NE | |
8118 | ("size for& too small, minimum allowed is ^", N, T); | |
37cb33b0 | 8119 | Set_Esize (T, Asiz); |
8120 | Set_RM_Size (T, Asiz); | |
ea61a7ea | 8121 | end if; |
8122 | end; | |
8123 | ||
8124 | -- All other composite types are ignored | |
8125 | ||
8126 | elsif Is_Composite_Type (UT) then | |
8127 | return; | |
8128 | ||
d6f39728 | 8129 | -- For fixed-point types, don't check minimum if type is not frozen, |
ea61a7ea | 8130 | -- since we don't know all the characteristics of the type that can |
8131 | -- affect the size (e.g. a specified small) till freeze time. | |
d6f39728 | 8132 | |
8133 | elsif Is_Fixed_Point_Type (UT) | |
8134 | and then not Is_Frozen (UT) | |
8135 | then | |
8136 | null; | |
8137 | ||
8138 | -- Cases for which a minimum check is required | |
8139 | ||
8140 | else | |
ea61a7ea | 8141 | -- Ignore if specified size is correct for the type |
8142 | ||
8143 | if Known_Esize (UT) and then Siz = Esize (UT) then | |
8144 | return; | |
8145 | end if; | |
8146 | ||
8147 | -- Otherwise get minimum size | |
8148 | ||
d6f39728 | 8149 | M := UI_From_Int (Minimum_Size (UT)); |
8150 | ||
8151 | if Siz < M then | |
8152 | ||
8153 | -- Size is less than minimum size, but one possibility remains | |
fdd294d1 | 8154 | -- that we can manage with the new size if we bias the type. |
d6f39728 | 8155 | |
8156 | M := UI_From_Int (Minimum_Size (UT, Biased => True)); | |
8157 | ||
8158 | if Siz < M then | |
8159 | Error_Msg_Uint_1 := M; | |
8160 | Error_Msg_NE | |
8161 | ("size for& too small, minimum allowed is ^", N, T); | |
37cb33b0 | 8162 | Set_Esize (T, M); |
8163 | Set_RM_Size (T, M); | |
d6f39728 | 8164 | else |
8165 | Biased := True; | |
8166 | end if; | |
8167 | end if; | |
8168 | end if; | |
8169 | end Check_Size; | |
8170 | ||
8171 | ------------------------- | |
8172 | -- Get_Alignment_Value -- | |
8173 | ------------------------- | |
8174 | ||
8175 | function Get_Alignment_Value (Expr : Node_Id) return Uint is | |
8176 | Align : constant Uint := Static_Integer (Expr); | |
8177 | ||
8178 | begin | |
8179 | if Align = No_Uint then | |
8180 | return No_Uint; | |
8181 | ||
8182 | elsif Align <= 0 then | |
8183 | Error_Msg_N ("alignment value must be positive", Expr); | |
8184 | return No_Uint; | |
8185 | ||
8186 | else | |
8187 | for J in Int range 0 .. 64 loop | |
8188 | declare | |
8189 | M : constant Uint := Uint_2 ** J; | |
8190 | ||
8191 | begin | |
8192 | exit when M = Align; | |
8193 | ||
8194 | if M > Align then | |
8195 | Error_Msg_N | |
8196 | ("alignment value must be power of 2", Expr); | |
8197 | return No_Uint; | |
8198 | end if; | |
8199 | end; | |
8200 | end loop; | |
8201 | ||
8202 | return Align; | |
8203 | end if; | |
8204 | end Get_Alignment_Value; | |
8205 | ||
99a2d5bd | 8206 | ------------------------------------- |
8207 | -- Inherit_Aspects_At_Freeze_Point -- | |
8208 | ------------------------------------- | |
8209 | ||
8210 | procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is | |
8211 | function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8212 | (Rep_Item : Node_Id) return Boolean; | |
8213 | -- This routine checks if Rep_Item is either a pragma or an aspect | |
8214 | -- specification node whose correponding pragma (if any) is present in | |
8215 | -- the Rep Item chain of the entity it has been specified to. | |
8216 | ||
8217 | -------------------------------------------------- | |
8218 | -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item -- | |
8219 | -------------------------------------------------- | |
8220 | ||
8221 | function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8222 | (Rep_Item : Node_Id) return Boolean | |
8223 | is | |
8224 | begin | |
8225 | return Nkind (Rep_Item) = N_Pragma | |
8226 | or else Present_In_Rep_Item | |
8227 | (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item)); | |
8228 | end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item; | |
8229 | ||
29a9d4be | 8230 | -- Start of processing for Inherit_Aspects_At_Freeze_Point |
8231 | ||
99a2d5bd | 8232 | begin |
8233 | -- A representation item is either subtype-specific (Size and Alignment | |
8234 | -- clauses) or type-related (all others). Subtype-specific aspects may | |
29a9d4be | 8235 | -- differ for different subtypes of the same type (RM 13.1.8). |
99a2d5bd | 8236 | |
8237 | -- A derived type inherits each type-related representation aspect of | |
8238 | -- its parent type that was directly specified before the declaration of | |
29a9d4be | 8239 | -- the derived type (RM 13.1.15). |
99a2d5bd | 8240 | |
8241 | -- A derived subtype inherits each subtype-specific representation | |
8242 | -- aspect of its parent subtype that was directly specified before the | |
29a9d4be | 8243 | -- declaration of the derived type (RM 13.1.15). |
99a2d5bd | 8244 | |
8245 | -- The general processing involves inheriting a representation aspect | |
8246 | -- from a parent type whenever the first rep item (aspect specification, | |
8247 | -- attribute definition clause, pragma) corresponding to the given | |
8248 | -- representation aspect in the rep item chain of Typ, if any, isn't | |
8249 | -- directly specified to Typ but to one of its parents. | |
8250 | ||
8251 | -- ??? Note that, for now, just a limited number of representation | |
29a9d4be | 8252 | -- aspects have been inherited here so far. Many of them are |
8253 | -- still inherited in Sem_Ch3. This will be fixed soon. Here is | |
8254 | -- a non- exhaustive list of aspects that likely also need to | |
8255 | -- be moved to this routine: Alignment, Component_Alignment, | |
8256 | -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates, | |
99a2d5bd | 8257 | -- Preelaborable_Initialization, RM_Size and Small. |
8258 | ||
8259 | if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then | |
8260 | return; | |
8261 | end if; | |
8262 | ||
8263 | -- Ada_05/Ada_2005 | |
8264 | ||
8265 | if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False) | |
8266 | and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005) | |
8267 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8268 | (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)) | |
8269 | then | |
8270 | Set_Is_Ada_2005_Only (Typ); | |
8271 | end if; | |
8272 | ||
8273 | -- Ada_12/Ada_2012 | |
8274 | ||
8275 | if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False) | |
8276 | and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012) | |
8277 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8278 | (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)) | |
8279 | then | |
8280 | Set_Is_Ada_2012_Only (Typ); | |
8281 | end if; | |
8282 | ||
8283 | -- Atomic/Shared | |
8284 | ||
8285 | if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False) | |
8286 | and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared) | |
8287 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8288 | (Get_Rep_Item (Typ, Name_Atomic, Name_Shared)) | |
8289 | then | |
8290 | Set_Is_Atomic (Typ); | |
8291 | Set_Treat_As_Volatile (Typ); | |
8292 | Set_Is_Volatile (Typ); | |
8293 | end if; | |
8294 | ||
29a9d4be | 8295 | -- Default_Component_Value |
99a2d5bd | 8296 | |
8297 | if Is_Array_Type (Typ) | |
8298 | and then Has_Rep_Item (Typ, Name_Default_Component_Value, False) | |
8299 | and then Has_Rep_Item (Typ, Name_Default_Component_Value) | |
8300 | then | |
8301 | Set_Default_Aspect_Component_Value (Typ, | |
8302 | Default_Aspect_Component_Value | |
8303 | (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value)))); | |
8304 | end if; | |
8305 | ||
29a9d4be | 8306 | -- Default_Value |
99a2d5bd | 8307 | |
8308 | if Is_Scalar_Type (Typ) | |
8309 | and then Has_Rep_Item (Typ, Name_Default_Value, False) | |
8310 | and then Has_Rep_Item (Typ, Name_Default_Value) | |
8311 | then | |
8312 | Set_Default_Aspect_Value (Typ, | |
8313 | Default_Aspect_Value | |
8314 | (Entity (Get_Rep_Item (Typ, Name_Default_Value)))); | |
8315 | end if; | |
8316 | ||
8317 | -- Discard_Names | |
8318 | ||
8319 | if not Has_Rep_Item (Typ, Name_Discard_Names, False) | |
8320 | and then Has_Rep_Item (Typ, Name_Discard_Names) | |
8321 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8322 | (Get_Rep_Item (Typ, Name_Discard_Names)) | |
8323 | then | |
8324 | Set_Discard_Names (Typ); | |
8325 | end if; | |
8326 | ||
8327 | -- Invariants | |
8328 | ||
8329 | if not Has_Rep_Item (Typ, Name_Invariant, False) | |
8330 | and then Has_Rep_Item (Typ, Name_Invariant) | |
8331 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8332 | (Get_Rep_Item (Typ, Name_Invariant)) | |
8333 | then | |
8334 | Set_Has_Invariants (Typ); | |
8335 | ||
8336 | if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then | |
8337 | Set_Has_Inheritable_Invariants (Typ); | |
8338 | end if; | |
8339 | end if; | |
8340 | ||
8341 | -- Volatile | |
8342 | ||
8343 | if not Has_Rep_Item (Typ, Name_Volatile, False) | |
8344 | and then Has_Rep_Item (Typ, Name_Volatile) | |
8345 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8346 | (Get_Rep_Item (Typ, Name_Volatile)) | |
8347 | then | |
8348 | Set_Treat_As_Volatile (Typ); | |
8349 | Set_Is_Volatile (Typ); | |
8350 | end if; | |
8351 | ||
8352 | -- Inheritance for derived types only | |
8353 | ||
8354 | if Is_Derived_Type (Typ) then | |
8355 | declare | |
8356 | Bas_Typ : constant Entity_Id := Base_Type (Typ); | |
8357 | Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ); | |
8358 | ||
8359 | begin | |
8360 | -- Atomic_Components | |
8361 | ||
8362 | if not Has_Rep_Item (Typ, Name_Atomic_Components, False) | |
8363 | and then Has_Rep_Item (Typ, Name_Atomic_Components) | |
8364 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8365 | (Get_Rep_Item (Typ, Name_Atomic_Components)) | |
8366 | then | |
8367 | Set_Has_Atomic_Components (Imp_Bas_Typ); | |
8368 | end if; | |
8369 | ||
8370 | -- Volatile_Components | |
8371 | ||
8372 | if not Has_Rep_Item (Typ, Name_Volatile_Components, False) | |
8373 | and then Has_Rep_Item (Typ, Name_Volatile_Components) | |
8374 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8375 | (Get_Rep_Item (Typ, Name_Volatile_Components)) | |
8376 | then | |
8377 | Set_Has_Volatile_Components (Imp_Bas_Typ); | |
8378 | end if; | |
8379 | ||
8380 | -- Finalize_Storage_Only. | |
8381 | ||
8382 | if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False) | |
8383 | and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only) | |
8384 | then | |
8385 | Set_Finalize_Storage_Only (Bas_Typ); | |
8386 | end if; | |
8387 | ||
8388 | -- Universal_Aliasing | |
8389 | ||
8390 | if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False) | |
8391 | and then Has_Rep_Item (Typ, Name_Universal_Aliasing) | |
8392 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
8393 | (Get_Rep_Item (Typ, Name_Universal_Aliasing)) | |
8394 | then | |
8395 | Set_Universal_Aliasing (Imp_Bas_Typ); | |
8396 | end if; | |
8397 | ||
8398 | -- Record type specific aspects | |
8399 | ||
8400 | if Is_Record_Type (Typ) then | |
29a9d4be | 8401 | |
99a2d5bd | 8402 | -- Bit_Order |
8403 | ||
8404 | if not Has_Rep_Item (Typ, Name_Bit_Order, False) | |
8405 | and then Has_Rep_Item (Typ, Name_Bit_Order) | |
8406 | then | |
8407 | Set_Reverse_Bit_Order (Bas_Typ, | |
8408 | Reverse_Bit_Order (Entity (Name | |
8409 | (Get_Rep_Item (Typ, Name_Bit_Order))))); | |
8410 | end if; | |
8411 | ||
8412 | -- Scalar_Storage_Order | |
8413 | ||
8414 | if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False) | |
8415 | and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order) | |
8416 | then | |
8417 | Set_Reverse_Storage_Order (Bas_Typ, | |
8418 | Reverse_Storage_Order (Entity (Name | |
8419 | (Get_Rep_Item (Typ, Name_Scalar_Storage_Order))))); | |
8420 | end if; | |
8421 | end if; | |
8422 | end; | |
8423 | end if; | |
8424 | end Inherit_Aspects_At_Freeze_Point; | |
8425 | ||
d6f39728 | 8426 | ---------------- |
8427 | -- Initialize -- | |
8428 | ---------------- | |
8429 | ||
8430 | procedure Initialize is | |
8431 | begin | |
7717ea00 | 8432 | Address_Clause_Checks.Init; |
8433 | Independence_Checks.Init; | |
d6f39728 | 8434 | Unchecked_Conversions.Init; |
8435 | end Initialize; | |
8436 | ||
8437 | ------------------------- | |
8438 | -- Is_Operational_Item -- | |
8439 | ------------------------- | |
8440 | ||
8441 | function Is_Operational_Item (N : Node_Id) return Boolean is | |
8442 | begin | |
8443 | if Nkind (N) /= N_Attribute_Definition_Clause then | |
8444 | return False; | |
b9e61b2a | 8445 | |
d6f39728 | 8446 | else |
8447 | declare | |
b9e61b2a | 8448 | Id : constant Attribute_Id := Get_Attribute_Id (Chars (N)); |
d6f39728 | 8449 | begin |
b9e61b2a | 8450 | return Id = Attribute_Input |
d6f39728 | 8451 | or else Id = Attribute_Output |
8452 | or else Id = Attribute_Read | |
f15731c4 | 8453 | or else Id = Attribute_Write |
8454 | or else Id = Attribute_External_Tag; | |
d6f39728 | 8455 | end; |
8456 | end if; | |
8457 | end Is_Operational_Item; | |
8458 | ||
8459 | ------------------ | |
8460 | -- Minimum_Size -- | |
8461 | ------------------ | |
8462 | ||
8463 | function Minimum_Size | |
8464 | (T : Entity_Id; | |
d5b349fa | 8465 | Biased : Boolean := False) return Nat |
d6f39728 | 8466 | is |
8467 | Lo : Uint := No_Uint; | |
8468 | Hi : Uint := No_Uint; | |
8469 | LoR : Ureal := No_Ureal; | |
8470 | HiR : Ureal := No_Ureal; | |
8471 | LoSet : Boolean := False; | |
8472 | HiSet : Boolean := False; | |
8473 | B : Uint; | |
8474 | S : Nat; | |
8475 | Ancest : Entity_Id; | |
f15731c4 | 8476 | R_Typ : constant Entity_Id := Root_Type (T); |
d6f39728 | 8477 | |
8478 | begin | |
8479 | -- If bad type, return 0 | |
8480 | ||
8481 | if T = Any_Type then | |
8482 | return 0; | |
8483 | ||
8484 | -- For generic types, just return zero. There cannot be any legitimate | |
8485 | -- need to know such a size, but this routine may be called with a | |
8486 | -- generic type as part of normal processing. | |
8487 | ||
f15731c4 | 8488 | elsif Is_Generic_Type (R_Typ) |
8489 | or else R_Typ = Any_Type | |
8490 | then | |
d6f39728 | 8491 | return 0; |
8492 | ||
93735cb8 | 8493 | -- Access types. Normally an access type cannot have a size smaller |
8494 | -- than the size of System.Address. The exception is on VMS, where | |
8495 | -- we have short and long addresses, and it is possible for an access | |
8496 | -- type to have a short address size (and thus be less than the size | |
8497 | -- of System.Address itself). We simply skip the check for VMS, and | |
fdd294d1 | 8498 | -- leave it to the back end to do the check. |
d6f39728 | 8499 | |
8500 | elsif Is_Access_Type (T) then | |
93735cb8 | 8501 | if OpenVMS_On_Target then |
8502 | return 0; | |
8503 | else | |
8504 | return System_Address_Size; | |
8505 | end if; | |
d6f39728 | 8506 | |
8507 | -- Floating-point types | |
8508 | ||
8509 | elsif Is_Floating_Point_Type (T) then | |
f15731c4 | 8510 | return UI_To_Int (Esize (R_Typ)); |
d6f39728 | 8511 | |
8512 | -- Discrete types | |
8513 | ||
8514 | elsif Is_Discrete_Type (T) then | |
8515 | ||
fdd294d1 | 8516 | -- The following loop is looking for the nearest compile time known |
8517 | -- bounds following the ancestor subtype chain. The idea is to find | |
8518 | -- the most restrictive known bounds information. | |
d6f39728 | 8519 | |
8520 | Ancest := T; | |
8521 | loop | |
8522 | if Ancest = Any_Type or else Etype (Ancest) = Any_Type then | |
8523 | return 0; | |
8524 | end if; | |
8525 | ||
8526 | if not LoSet then | |
8527 | if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then | |
8528 | Lo := Expr_Rep_Value (Type_Low_Bound (Ancest)); | |
8529 | LoSet := True; | |
8530 | exit when HiSet; | |
8531 | end if; | |
8532 | end if; | |
8533 | ||
8534 | if not HiSet then | |
8535 | if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then | |
8536 | Hi := Expr_Rep_Value (Type_High_Bound (Ancest)); | |
8537 | HiSet := True; | |
8538 | exit when LoSet; | |
8539 | end if; | |
8540 | end if; | |
8541 | ||
8542 | Ancest := Ancestor_Subtype (Ancest); | |
8543 | ||
8544 | if No (Ancest) then | |
8545 | Ancest := Base_Type (T); | |
8546 | ||
8547 | if Is_Generic_Type (Ancest) then | |
8548 | return 0; | |
8549 | end if; | |
8550 | end if; | |
8551 | end loop; | |
8552 | ||
8553 | -- Fixed-point types. We can't simply use Expr_Value to get the | |
fdd294d1 | 8554 | -- Corresponding_Integer_Value values of the bounds, since these do not |
8555 | -- get set till the type is frozen, and this routine can be called | |
8556 | -- before the type is frozen. Similarly the test for bounds being static | |
8557 | -- needs to include the case where we have unanalyzed real literals for | |
8558 | -- the same reason. | |
d6f39728 | 8559 | |
8560 | elsif Is_Fixed_Point_Type (T) then | |
8561 | ||
fdd294d1 | 8562 | -- The following loop is looking for the nearest compile time known |
8563 | -- bounds following the ancestor subtype chain. The idea is to find | |
8564 | -- the most restrictive known bounds information. | |
d6f39728 | 8565 | |
8566 | Ancest := T; | |
8567 | loop | |
8568 | if Ancest = Any_Type or else Etype (Ancest) = Any_Type then | |
8569 | return 0; | |
8570 | end if; | |
8571 | ||
3062c401 | 8572 | -- Note: In the following two tests for LoSet and HiSet, it may |
8573 | -- seem redundant to test for N_Real_Literal here since normally | |
8574 | -- one would assume that the test for the value being known at | |
8575 | -- compile time includes this case. However, there is a glitch. | |
8576 | -- If the real literal comes from folding a non-static expression, | |
8577 | -- then we don't consider any non- static expression to be known | |
8578 | -- at compile time if we are in configurable run time mode (needed | |
8579 | -- in some cases to give a clearer definition of what is and what | |
8580 | -- is not accepted). So the test is indeed needed. Without it, we | |
8581 | -- would set neither Lo_Set nor Hi_Set and get an infinite loop. | |
8582 | ||
d6f39728 | 8583 | if not LoSet then |
8584 | if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal | |
8585 | or else Compile_Time_Known_Value (Type_Low_Bound (Ancest)) | |
8586 | then | |
8587 | LoR := Expr_Value_R (Type_Low_Bound (Ancest)); | |
8588 | LoSet := True; | |
8589 | exit when HiSet; | |
8590 | end if; | |
8591 | end if; | |
8592 | ||
8593 | if not HiSet then | |
8594 | if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal | |
8595 | or else Compile_Time_Known_Value (Type_High_Bound (Ancest)) | |
8596 | then | |
8597 | HiR := Expr_Value_R (Type_High_Bound (Ancest)); | |
8598 | HiSet := True; | |
8599 | exit when LoSet; | |
8600 | end if; | |
8601 | end if; | |
8602 | ||
8603 | Ancest := Ancestor_Subtype (Ancest); | |
8604 | ||
8605 | if No (Ancest) then | |
8606 | Ancest := Base_Type (T); | |
8607 | ||
8608 | if Is_Generic_Type (Ancest) then | |
8609 | return 0; | |
8610 | end if; | |
8611 | end if; | |
8612 | end loop; | |
8613 | ||
8614 | Lo := UR_To_Uint (LoR / Small_Value (T)); | |
8615 | Hi := UR_To_Uint (HiR / Small_Value (T)); | |
8616 | ||
8617 | -- No other types allowed | |
8618 | ||
8619 | else | |
8620 | raise Program_Error; | |
8621 | end if; | |
8622 | ||
2866d595 | 8623 | -- Fall through with Hi and Lo set. Deal with biased case |
d6f39728 | 8624 | |
cc46ff4b | 8625 | if (Biased |
8626 | and then not Is_Fixed_Point_Type (T) | |
8627 | and then not (Is_Enumeration_Type (T) | |
8628 | and then Has_Non_Standard_Rep (T))) | |
d6f39728 | 8629 | or else Has_Biased_Representation (T) |
8630 | then | |
8631 | Hi := Hi - Lo; | |
8632 | Lo := Uint_0; | |
8633 | end if; | |
8634 | ||
8635 | -- Signed case. Note that we consider types like range 1 .. -1 to be | |
fdd294d1 | 8636 | -- signed for the purpose of computing the size, since the bounds have |
1a34e48c | 8637 | -- to be accommodated in the base type. |
d6f39728 | 8638 | |
8639 | if Lo < 0 or else Hi < 0 then | |
8640 | S := 1; | |
8641 | B := Uint_1; | |
8642 | ||
da253936 | 8643 | -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1)) |
8644 | -- Note that we accommodate the case where the bounds cross. This | |
d6f39728 | 8645 | -- can happen either because of the way the bounds are declared |
8646 | -- or because of the algorithm in Freeze_Fixed_Point_Type. | |
8647 | ||
8648 | while Lo < -B | |
8649 | or else Hi < -B | |
8650 | or else Lo >= B | |
8651 | or else Hi >= B | |
8652 | loop | |
8653 | B := Uint_2 ** S; | |
8654 | S := S + 1; | |
8655 | end loop; | |
8656 | ||
8657 | -- Unsigned case | |
8658 | ||
8659 | else | |
8660 | -- If both bounds are positive, make sure that both are represen- | |
8661 | -- table in the case where the bounds are crossed. This can happen | |
8662 | -- either because of the way the bounds are declared, or because of | |
8663 | -- the algorithm in Freeze_Fixed_Point_Type. | |
8664 | ||
8665 | if Lo > Hi then | |
8666 | Hi := Lo; | |
8667 | end if; | |
8668 | ||
da253936 | 8669 | -- S = size, (can accommodate 0 .. (2**size - 1)) |
d6f39728 | 8670 | |
8671 | S := 0; | |
8672 | while Hi >= Uint_2 ** S loop | |
8673 | S := S + 1; | |
8674 | end loop; | |
8675 | end if; | |
8676 | ||
8677 | return S; | |
8678 | end Minimum_Size; | |
8679 | ||
44e4341e | 8680 | --------------------------- |
8681 | -- New_Stream_Subprogram -- | |
8682 | --------------------------- | |
d6f39728 | 8683 | |
44e4341e | 8684 | procedure New_Stream_Subprogram |
8685 | (N : Node_Id; | |
8686 | Ent : Entity_Id; | |
8687 | Subp : Entity_Id; | |
8688 | Nam : TSS_Name_Type) | |
d6f39728 | 8689 | is |
8690 | Loc : constant Source_Ptr := Sloc (N); | |
9dfe12ae | 8691 | Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam); |
f15731c4 | 8692 | Subp_Id : Entity_Id; |
d6f39728 | 8693 | Subp_Decl : Node_Id; |
8694 | F : Entity_Id; | |
8695 | Etyp : Entity_Id; | |
8696 | ||
44e4341e | 8697 | Defer_Declaration : constant Boolean := |
8698 | Is_Tagged_Type (Ent) or else Is_Private_Type (Ent); | |
8699 | -- For a tagged type, there is a declaration for each stream attribute | |
8700 | -- at the freeze point, and we must generate only a completion of this | |
8701 | -- declaration. We do the same for private types, because the full view | |
8702 | -- might be tagged. Otherwise we generate a declaration at the point of | |
8703 | -- the attribute definition clause. | |
8704 | ||
f15731c4 | 8705 | function Build_Spec return Node_Id; |
8706 | -- Used for declaration and renaming declaration, so that this is | |
8707 | -- treated as a renaming_as_body. | |
8708 | ||
8709 | ---------------- | |
8710 | -- Build_Spec -- | |
8711 | ---------------- | |
8712 | ||
d5b349fa | 8713 | function Build_Spec return Node_Id is |
44e4341e | 8714 | Out_P : constant Boolean := (Nam = TSS_Stream_Read); |
8715 | Formals : List_Id; | |
8716 | Spec : Node_Id; | |
8717 | T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc); | |
8718 | ||
f15731c4 | 8719 | begin |
9dfe12ae | 8720 | Subp_Id := Make_Defining_Identifier (Loc, Sname); |
f15731c4 | 8721 | |
44e4341e | 8722 | -- S : access Root_Stream_Type'Class |
8723 | ||
8724 | Formals := New_List ( | |
8725 | Make_Parameter_Specification (Loc, | |
8726 | Defining_Identifier => | |
8727 | Make_Defining_Identifier (Loc, Name_S), | |
8728 | Parameter_Type => | |
8729 | Make_Access_Definition (Loc, | |
8730 | Subtype_Mark => | |
8731 | New_Reference_To ( | |
8732 | Designated_Type (Etype (F)), Loc)))); | |
8733 | ||
8734 | if Nam = TSS_Stream_Input then | |
8735 | Spec := Make_Function_Specification (Loc, | |
8736 | Defining_Unit_Name => Subp_Id, | |
8737 | Parameter_Specifications => Formals, | |
8738 | Result_Definition => T_Ref); | |
8739 | else | |
8740 | -- V : [out] T | |
f15731c4 | 8741 | |
44e4341e | 8742 | Append_To (Formals, |
8743 | Make_Parameter_Specification (Loc, | |
8744 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), | |
8745 | Out_Present => Out_P, | |
8746 | Parameter_Type => T_Ref)); | |
f15731c4 | 8747 | |
d3ef794c | 8748 | Spec := |
8749 | Make_Procedure_Specification (Loc, | |
8750 | Defining_Unit_Name => Subp_Id, | |
8751 | Parameter_Specifications => Formals); | |
44e4341e | 8752 | end if; |
f15731c4 | 8753 | |
44e4341e | 8754 | return Spec; |
8755 | end Build_Spec; | |
d6f39728 | 8756 | |
44e4341e | 8757 | -- Start of processing for New_Stream_Subprogram |
d6f39728 | 8758 | |
44e4341e | 8759 | begin |
8760 | F := First_Formal (Subp); | |
8761 | ||
8762 | if Ekind (Subp) = E_Procedure then | |
8763 | Etyp := Etype (Next_Formal (F)); | |
d6f39728 | 8764 | else |
44e4341e | 8765 | Etyp := Etype (Subp); |
d6f39728 | 8766 | end if; |
f15731c4 | 8767 | |
44e4341e | 8768 | -- Prepare subprogram declaration and insert it as an action on the |
8769 | -- clause node. The visibility for this entity is used to test for | |
8770 | -- visibility of the attribute definition clause (in the sense of | |
8771 | -- 8.3(23) as amended by AI-195). | |
9dfe12ae | 8772 | |
44e4341e | 8773 | if not Defer_Declaration then |
f15731c4 | 8774 | Subp_Decl := |
8775 | Make_Subprogram_Declaration (Loc, | |
8776 | Specification => Build_Spec); | |
44e4341e | 8777 | |
8778 | -- For a tagged type, there is always a visible declaration for each | |
15ebb600 | 8779 | -- stream TSS (it is a predefined primitive operation), and the |
44e4341e | 8780 | -- completion of this declaration occurs at the freeze point, which is |
8781 | -- not always visible at places where the attribute definition clause is | |
8782 | -- visible. So, we create a dummy entity here for the purpose of | |
8783 | -- tracking the visibility of the attribute definition clause itself. | |
8784 | ||
8785 | else | |
8786 | Subp_Id := | |
55868293 | 8787 | Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V')); |
44e4341e | 8788 | Subp_Decl := |
8789 | Make_Object_Declaration (Loc, | |
8790 | Defining_Identifier => Subp_Id, | |
8791 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc)); | |
f15731c4 | 8792 | end if; |
8793 | ||
44e4341e | 8794 | Insert_Action (N, Subp_Decl); |
8795 | Set_Entity (N, Subp_Id); | |
8796 | ||
d6f39728 | 8797 | Subp_Decl := |
8798 | Make_Subprogram_Renaming_Declaration (Loc, | |
f15731c4 | 8799 | Specification => Build_Spec, |
8800 | Name => New_Reference_To (Subp, Loc)); | |
d6f39728 | 8801 | |
44e4341e | 8802 | if Defer_Declaration then |
d6f39728 | 8803 | Set_TSS (Base_Type (Ent), Subp_Id); |
8804 | else | |
8805 | Insert_Action (N, Subp_Decl); | |
8806 | Copy_TSS (Subp_Id, Base_Type (Ent)); | |
8807 | end if; | |
44e4341e | 8808 | end New_Stream_Subprogram; |
d6f39728 | 8809 | |
d6f39728 | 8810 | ------------------------ |
8811 | -- Rep_Item_Too_Early -- | |
8812 | ------------------------ | |
8813 | ||
80d4fec4 | 8814 | function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is |
d6f39728 | 8815 | begin |
44e4341e | 8816 | -- Cannot apply non-operational rep items to generic types |
d6f39728 | 8817 | |
f15731c4 | 8818 | if Is_Operational_Item (N) then |
8819 | return False; | |
8820 | ||
8821 | elsif Is_Type (T) | |
d6f39728 | 8822 | and then Is_Generic_Type (Root_Type (T)) |
8823 | then | |
503f7fd3 | 8824 | Error_Msg_N ("representation item not allowed for generic type", N); |
d6f39728 | 8825 | return True; |
8826 | end if; | |
8827 | ||
fdd294d1 | 8828 | -- Otherwise check for incomplete type |
d6f39728 | 8829 | |
8830 | if Is_Incomplete_Or_Private_Type (T) | |
8831 | and then No (Underlying_Type (T)) | |
d64221a7 | 8832 | and then |
8833 | (Nkind (N) /= N_Pragma | |
60014bc9 | 8834 | or else Get_Pragma_Id (N) /= Pragma_Import) |
d6f39728 | 8835 | then |
8836 | Error_Msg_N | |
8837 | ("representation item must be after full type declaration", N); | |
8838 | return True; | |
8839 | ||
1a34e48c | 8840 | -- If the type has incomplete components, a representation clause is |
d6f39728 | 8841 | -- illegal but stream attributes and Convention pragmas are correct. |
8842 | ||
8843 | elsif Has_Private_Component (T) then | |
f15731c4 | 8844 | if Nkind (N) = N_Pragma then |
d6f39728 | 8845 | return False; |
b9e61b2a | 8846 | |
d6f39728 | 8847 | else |
8848 | Error_Msg_N | |
8849 | ("representation item must appear after type is fully defined", | |
8850 | N); | |
8851 | return True; | |
8852 | end if; | |
8853 | else | |
8854 | return False; | |
8855 | end if; | |
8856 | end Rep_Item_Too_Early; | |
8857 | ||
8858 | ----------------------- | |
8859 | -- Rep_Item_Too_Late -- | |
8860 | ----------------------- | |
8861 | ||
8862 | function Rep_Item_Too_Late | |
8863 | (T : Entity_Id; | |
8864 | N : Node_Id; | |
d5b349fa | 8865 | FOnly : Boolean := False) return Boolean |
d6f39728 | 8866 | is |
8867 | S : Entity_Id; | |
8868 | Parent_Type : Entity_Id; | |
8869 | ||
8870 | procedure Too_Late; | |
d53a018a | 8871 | -- Output the too late message. Note that this is not considered a |
8872 | -- serious error, since the effect is simply that we ignore the | |
8873 | -- representation clause in this case. | |
8874 | ||
8875 | -------------- | |
8876 | -- Too_Late -- | |
8877 | -------------- | |
d6f39728 | 8878 | |
8879 | procedure Too_Late is | |
8880 | begin | |
d53a018a | 8881 | Error_Msg_N ("|representation item appears too late!", N); |
d6f39728 | 8882 | end Too_Late; |
8883 | ||
8884 | -- Start of processing for Rep_Item_Too_Late | |
8885 | ||
8886 | begin | |
a3248fc4 | 8887 | -- First make sure entity is not frozen (RM 13.1(9)) |
d6f39728 | 8888 | |
8889 | if Is_Frozen (T) | |
a3248fc4 | 8890 | |
8891 | -- Exclude imported types, which may be frozen if they appear in a | |
8892 | -- representation clause for a local type. | |
8893 | ||
d6f39728 | 8894 | and then not From_With_Type (T) |
a3248fc4 | 8895 | |
8896 | -- Exclude generated entitiesa (not coming from source). The common | |
8897 | -- case is when we generate a renaming which prematurely freezes the | |
8898 | -- renamed internal entity, but we still want to be able to set copies | |
8899 | -- of attribute values such as Size/Alignment. | |
8900 | ||
8901 | and then Comes_From_Source (T) | |
d6f39728 | 8902 | then |
8903 | Too_Late; | |
8904 | S := First_Subtype (T); | |
8905 | ||
8906 | if Present (Freeze_Node (S)) then | |
8907 | Error_Msg_NE | |
1e3532e7 | 8908 | ("??no more representation items for }", Freeze_Node (S), S); |
d6f39728 | 8909 | end if; |
8910 | ||
8911 | return True; | |
8912 | ||
8913 | -- Check for case of non-tagged derived type whose parent either has | |
8914 | -- primitive operations, or is a by reference type (RM 13.1(10)). | |
8915 | ||
8916 | elsif Is_Type (T) | |
8917 | and then not FOnly | |
8918 | and then Is_Derived_Type (T) | |
8919 | and then not Is_Tagged_Type (T) | |
8920 | then | |
8921 | Parent_Type := Etype (Base_Type (T)); | |
8922 | ||
8923 | if Has_Primitive_Operations (Parent_Type) then | |
8924 | Too_Late; | |
8925 | Error_Msg_NE | |
8926 | ("primitive operations already defined for&!", N, Parent_Type); | |
8927 | return True; | |
8928 | ||
8929 | elsif Is_By_Reference_Type (Parent_Type) then | |
8930 | Too_Late; | |
8931 | Error_Msg_NE | |
8932 | ("parent type & is a by reference type!", N, Parent_Type); | |
8933 | return True; | |
8934 | end if; | |
8935 | end if; | |
8936 | ||
3062c401 | 8937 | -- No error, link item into head of chain of rep items for the entity, |
8938 | -- but avoid chaining if we have an overloadable entity, and the pragma | |
8939 | -- is one that can apply to multiple overloaded entities. | |
8940 | ||
b9e61b2a | 8941 | if Is_Overloadable (T) and then Nkind (N) = N_Pragma then |
fdd294d1 | 8942 | declare |
8943 | Pname : constant Name_Id := Pragma_Name (N); | |
8944 | begin | |
8945 | if Pname = Name_Convention or else | |
8946 | Pname = Name_Import or else | |
8947 | Pname = Name_Export or else | |
8948 | Pname = Name_External or else | |
8949 | Pname = Name_Interface | |
8950 | then | |
8951 | return False; | |
8952 | end if; | |
8953 | end; | |
3062c401 | 8954 | end if; |
8955 | ||
fdd294d1 | 8956 | Record_Rep_Item (T, N); |
d6f39728 | 8957 | return False; |
8958 | end Rep_Item_Too_Late; | |
8959 | ||
2072eaa9 | 8960 | ------------------------------------- |
8961 | -- Replace_Type_References_Generic -- | |
8962 | ------------------------------------- | |
8963 | ||
8964 | procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is | |
8965 | ||
8966 | function Replace_Node (N : Node_Id) return Traverse_Result; | |
8967 | -- Processes a single node in the traversal procedure below, checking | |
8968 | -- if node N should be replaced, and if so, doing the replacement. | |
8969 | ||
8970 | procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node); | |
8971 | -- This instantiation provides the body of Replace_Type_References | |
8972 | ||
8973 | ------------------ | |
8974 | -- Replace_Node -- | |
8975 | ------------------ | |
8976 | ||
8977 | function Replace_Node (N : Node_Id) return Traverse_Result is | |
8978 | S : Entity_Id; | |
8979 | P : Node_Id; | |
8980 | ||
8981 | begin | |
8982 | -- Case of identifier | |
8983 | ||
8984 | if Nkind (N) = N_Identifier then | |
8985 | ||
8986 | -- If not the type name, all done with this node | |
8987 | ||
8988 | if Chars (N) /= TName then | |
8989 | return Skip; | |
8990 | ||
8991 | -- Otherwise do the replacement and we are done with this node | |
8992 | ||
8993 | else | |
8994 | Replace_Type_Reference (N); | |
8995 | return Skip; | |
8996 | end if; | |
8997 | ||
8998 | -- Case of selected component (which is what a qualification | |
8999 | -- looks like in the unanalyzed tree, which is what we have. | |
9000 | ||
9001 | elsif Nkind (N) = N_Selected_Component then | |
9002 | ||
9003 | -- If selector name is not our type, keeping going (we might | |
9004 | -- still have an occurrence of the type in the prefix). | |
9005 | ||
9006 | if Nkind (Selector_Name (N)) /= N_Identifier | |
9007 | or else Chars (Selector_Name (N)) /= TName | |
9008 | then | |
9009 | return OK; | |
9010 | ||
9011 | -- Selector name is our type, check qualification | |
9012 | ||
9013 | else | |
9014 | -- Loop through scopes and prefixes, doing comparison | |
9015 | ||
9016 | S := Current_Scope; | |
9017 | P := Prefix (N); | |
9018 | loop | |
9019 | -- Continue if no more scopes or scope with no name | |
9020 | ||
9021 | if No (S) or else Nkind (S) not in N_Has_Chars then | |
9022 | return OK; | |
9023 | end if; | |
9024 | ||
9025 | -- Do replace if prefix is an identifier matching the | |
9026 | -- scope that we are currently looking at. | |
9027 | ||
9028 | if Nkind (P) = N_Identifier | |
9029 | and then Chars (P) = Chars (S) | |
9030 | then | |
9031 | Replace_Type_Reference (N); | |
9032 | return Skip; | |
9033 | end if; | |
9034 | ||
9035 | -- Go check scope above us if prefix is itself of the | |
9036 | -- form of a selected component, whose selector matches | |
9037 | -- the scope we are currently looking at. | |
9038 | ||
9039 | if Nkind (P) = N_Selected_Component | |
9040 | and then Nkind (Selector_Name (P)) = N_Identifier | |
9041 | and then Chars (Selector_Name (P)) = Chars (S) | |
9042 | then | |
9043 | S := Scope (S); | |
9044 | P := Prefix (P); | |
9045 | ||
9046 | -- For anything else, we don't have a match, so keep on | |
9047 | -- going, there are still some weird cases where we may | |
9048 | -- still have a replacement within the prefix. | |
9049 | ||
9050 | else | |
9051 | return OK; | |
9052 | end if; | |
9053 | end loop; | |
9054 | end if; | |
9055 | ||
9056 | -- Continue for any other node kind | |
9057 | ||
9058 | else | |
9059 | return OK; | |
9060 | end if; | |
9061 | end Replace_Node; | |
9062 | ||
9063 | begin | |
9064 | Replace_Type_Refs (N); | |
9065 | end Replace_Type_References_Generic; | |
9066 | ||
d6f39728 | 9067 | ------------------------- |
9068 | -- Same_Representation -- | |
9069 | ------------------------- | |
9070 | ||
9071 | function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is | |
9072 | T1 : constant Entity_Id := Underlying_Type (Typ1); | |
9073 | T2 : constant Entity_Id := Underlying_Type (Typ2); | |
9074 | ||
9075 | begin | |
9076 | -- A quick check, if base types are the same, then we definitely have | |
9077 | -- the same representation, because the subtype specific representation | |
9078 | -- attributes (Size and Alignment) do not affect representation from | |
9079 | -- the point of view of this test. | |
9080 | ||
9081 | if Base_Type (T1) = Base_Type (T2) then | |
9082 | return True; | |
9083 | ||
9084 | elsif Is_Private_Type (Base_Type (T2)) | |
9085 | and then Base_Type (T1) = Full_View (Base_Type (T2)) | |
9086 | then | |
9087 | return True; | |
9088 | end if; | |
9089 | ||
9090 | -- Tagged types never have differing representations | |
9091 | ||
9092 | if Is_Tagged_Type (T1) then | |
9093 | return True; | |
9094 | end if; | |
9095 | ||
9096 | -- Representations are definitely different if conventions differ | |
9097 | ||
9098 | if Convention (T1) /= Convention (T2) then | |
9099 | return False; | |
9100 | end if; | |
9101 | ||
9102 | -- Representations are different if component alignments differ | |
9103 | ||
9104 | if (Is_Record_Type (T1) or else Is_Array_Type (T1)) | |
9105 | and then | |
9106 | (Is_Record_Type (T2) or else Is_Array_Type (T2)) | |
9107 | and then Component_Alignment (T1) /= Component_Alignment (T2) | |
9108 | then | |
9109 | return False; | |
9110 | end if; | |
9111 | ||
9112 | -- For arrays, the only real issue is component size. If we know the | |
9113 | -- component size for both arrays, and it is the same, then that's | |
9114 | -- good enough to know we don't have a change of representation. | |
9115 | ||
9116 | if Is_Array_Type (T1) then | |
9117 | if Known_Component_Size (T1) | |
9118 | and then Known_Component_Size (T2) | |
9119 | and then Component_Size (T1) = Component_Size (T2) | |
9120 | then | |
9f1130cc | 9121 | if VM_Target = No_VM then |
9122 | return True; | |
9123 | ||
9124 | -- In VM targets the representation of arrays with aliased | |
9125 | -- components differs from arrays with non-aliased components | |
9126 | ||
9127 | else | |
9128 | return Has_Aliased_Components (Base_Type (T1)) | |
0ba3592b | 9129 | = |
9130 | Has_Aliased_Components (Base_Type (T2)); | |
9f1130cc | 9131 | end if; |
d6f39728 | 9132 | end if; |
9133 | end if; | |
9134 | ||
9135 | -- Types definitely have same representation if neither has non-standard | |
9136 | -- representation since default representations are always consistent. | |
9137 | -- If only one has non-standard representation, and the other does not, | |
9138 | -- then we consider that they do not have the same representation. They | |
9139 | -- might, but there is no way of telling early enough. | |
9140 | ||
9141 | if Has_Non_Standard_Rep (T1) then | |
9142 | if not Has_Non_Standard_Rep (T2) then | |
9143 | return False; | |
9144 | end if; | |
9145 | else | |
9146 | return not Has_Non_Standard_Rep (T2); | |
9147 | end if; | |
9148 | ||
fdd294d1 | 9149 | -- Here the two types both have non-standard representation, and we need |
9150 | -- to determine if they have the same non-standard representation. | |
d6f39728 | 9151 | |
9152 | -- For arrays, we simply need to test if the component sizes are the | |
9153 | -- same. Pragma Pack is reflected in modified component sizes, so this | |
9154 | -- check also deals with pragma Pack. | |
9155 | ||
9156 | if Is_Array_Type (T1) then | |
9157 | return Component_Size (T1) = Component_Size (T2); | |
9158 | ||
9159 | -- Tagged types always have the same representation, because it is not | |
9160 | -- possible to specify different representations for common fields. | |
9161 | ||
9162 | elsif Is_Tagged_Type (T1) then | |
9163 | return True; | |
9164 | ||
9165 | -- Case of record types | |
9166 | ||
9167 | elsif Is_Record_Type (T1) then | |
9168 | ||
9169 | -- Packed status must conform | |
9170 | ||
9171 | if Is_Packed (T1) /= Is_Packed (T2) then | |
9172 | return False; | |
9173 | ||
9174 | -- Otherwise we must check components. Typ2 maybe a constrained | |
9175 | -- subtype with fewer components, so we compare the components | |
9176 | -- of the base types. | |
9177 | ||
9178 | else | |
9179 | Record_Case : declare | |
9180 | CD1, CD2 : Entity_Id; | |
9181 | ||
9182 | function Same_Rep return Boolean; | |
9183 | -- CD1 and CD2 are either components or discriminants. This | |
9184 | -- function tests whether the two have the same representation | |
9185 | ||
80d4fec4 | 9186 | -------------- |
9187 | -- Same_Rep -- | |
9188 | -------------- | |
9189 | ||
d6f39728 | 9190 | function Same_Rep return Boolean is |
9191 | begin | |
9192 | if No (Component_Clause (CD1)) then | |
9193 | return No (Component_Clause (CD2)); | |
9194 | ||
9195 | else | |
9196 | return | |
9197 | Present (Component_Clause (CD2)) | |
9198 | and then | |
9199 | Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2) | |
9200 | and then | |
9201 | Esize (CD1) = Esize (CD2); | |
9202 | end if; | |
9203 | end Same_Rep; | |
9204 | ||
1e35409d | 9205 | -- Start of processing for Record_Case |
d6f39728 | 9206 | |
9207 | begin | |
9208 | if Has_Discriminants (T1) then | |
d6f39728 | 9209 | |
9dfe12ae | 9210 | -- The number of discriminants may be different if the |
9211 | -- derived type has fewer (constrained by values). The | |
9212 | -- invisible discriminants retain the representation of | |
9213 | -- the original, so the discrepancy does not per se | |
9214 | -- indicate a different representation. | |
9215 | ||
b9e61b2a | 9216 | CD1 := First_Discriminant (T1); |
9217 | CD2 := First_Discriminant (T2); | |
9218 | while Present (CD1) and then Present (CD2) loop | |
d6f39728 | 9219 | if not Same_Rep then |
9220 | return False; | |
9221 | else | |
9222 | Next_Discriminant (CD1); | |
9223 | Next_Discriminant (CD2); | |
9224 | end if; | |
9225 | end loop; | |
9226 | end if; | |
9227 | ||
9228 | CD1 := First_Component (Underlying_Type (Base_Type (T1))); | |
9229 | CD2 := First_Component (Underlying_Type (Base_Type (T2))); | |
d6f39728 | 9230 | while Present (CD1) loop |
9231 | if not Same_Rep then | |
9232 | return False; | |
9233 | else | |
9234 | Next_Component (CD1); | |
9235 | Next_Component (CD2); | |
9236 | end if; | |
9237 | end loop; | |
9238 | ||
9239 | return True; | |
9240 | end Record_Case; | |
9241 | end if; | |
9242 | ||
9243 | -- For enumeration types, we must check each literal to see if the | |
9244 | -- representation is the same. Note that we do not permit enumeration | |
1a34e48c | 9245 | -- representation clauses for Character and Wide_Character, so these |
d6f39728 | 9246 | -- cases were already dealt with. |
9247 | ||
9248 | elsif Is_Enumeration_Type (T1) then | |
d6f39728 | 9249 | Enumeration_Case : declare |
9250 | L1, L2 : Entity_Id; | |
9251 | ||
9252 | begin | |
9253 | L1 := First_Literal (T1); | |
9254 | L2 := First_Literal (T2); | |
d6f39728 | 9255 | while Present (L1) loop |
9256 | if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then | |
9257 | return False; | |
9258 | else | |
9259 | Next_Literal (L1); | |
9260 | Next_Literal (L2); | |
9261 | end if; | |
9262 | end loop; | |
9263 | ||
9264 | return True; | |
d6f39728 | 9265 | end Enumeration_Case; |
9266 | ||
9267 | -- Any other types have the same representation for these purposes | |
9268 | ||
9269 | else | |
9270 | return True; | |
9271 | end if; | |
d6f39728 | 9272 | end Same_Representation; |
9273 | ||
b77e4501 | 9274 | ---------------- |
9275 | -- Set_Biased -- | |
9276 | ---------------- | |
9277 | ||
9278 | procedure Set_Biased | |
9279 | (E : Entity_Id; | |
9280 | N : Node_Id; | |
9281 | Msg : String; | |
9282 | Biased : Boolean := True) | |
9283 | is | |
9284 | begin | |
9285 | if Biased then | |
9286 | Set_Has_Biased_Representation (E); | |
9287 | ||
9288 | if Warn_On_Biased_Representation then | |
9289 | Error_Msg_NE | |
1e3532e7 | 9290 | ("?B?" & Msg & " forces biased representation for&", N, E); |
b77e4501 | 9291 | end if; |
9292 | end if; | |
9293 | end Set_Biased; | |
9294 | ||
d6f39728 | 9295 | -------------------- |
9296 | -- Set_Enum_Esize -- | |
9297 | -------------------- | |
9298 | ||
9299 | procedure Set_Enum_Esize (T : Entity_Id) is | |
9300 | Lo : Uint; | |
9301 | Hi : Uint; | |
9302 | Sz : Nat; | |
9303 | ||
9304 | begin | |
9305 | Init_Alignment (T); | |
9306 | ||
9307 | -- Find the minimum standard size (8,16,32,64) that fits | |
9308 | ||
9309 | Lo := Enumeration_Rep (Entity (Type_Low_Bound (T))); | |
9310 | Hi := Enumeration_Rep (Entity (Type_High_Bound (T))); | |
9311 | ||
9312 | if Lo < 0 then | |
9313 | if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then | |
f15731c4 | 9314 | Sz := Standard_Character_Size; -- May be > 8 on some targets |
d6f39728 | 9315 | |
9316 | elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then | |
9317 | Sz := 16; | |
9318 | ||
9319 | elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then | |
9320 | Sz := 32; | |
9321 | ||
9322 | else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63); | |
9323 | Sz := 64; | |
9324 | end if; | |
9325 | ||
9326 | else | |
9327 | if Hi < Uint_2**08 then | |
f15731c4 | 9328 | Sz := Standard_Character_Size; -- May be > 8 on some targets |
d6f39728 | 9329 | |
9330 | elsif Hi < Uint_2**16 then | |
9331 | Sz := 16; | |
9332 | ||
9333 | elsif Hi < Uint_2**32 then | |
9334 | Sz := 32; | |
9335 | ||
9336 | else pragma Assert (Hi < Uint_2**63); | |
9337 | Sz := 64; | |
9338 | end if; | |
9339 | end if; | |
9340 | ||
9341 | -- That minimum is the proper size unless we have a foreign convention | |
9342 | -- and the size required is 32 or less, in which case we bump the size | |
9343 | -- up to 32. This is required for C and C++ and seems reasonable for | |
9344 | -- all other foreign conventions. | |
9345 | ||
9346 | if Has_Foreign_Convention (T) | |
9347 | and then Esize (T) < Standard_Integer_Size | |
9348 | then | |
9349 | Init_Esize (T, Standard_Integer_Size); | |
d6f39728 | 9350 | else |
9351 | Init_Esize (T, Sz); | |
9352 | end if; | |
d6f39728 | 9353 | end Set_Enum_Esize; |
9354 | ||
83f8f0a6 | 9355 | ------------------------------ |
9356 | -- Validate_Address_Clauses -- | |
9357 | ------------------------------ | |
9358 | ||
9359 | procedure Validate_Address_Clauses is | |
9360 | begin | |
9361 | for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop | |
9362 | declare | |
9363 | ACCR : Address_Clause_Check_Record | |
9364 | renames Address_Clause_Checks.Table (J); | |
9365 | ||
d6da7448 | 9366 | Expr : Node_Id; |
9367 | ||
83f8f0a6 | 9368 | X_Alignment : Uint; |
9369 | Y_Alignment : Uint; | |
9370 | ||
9371 | X_Size : Uint; | |
9372 | Y_Size : Uint; | |
9373 | ||
9374 | begin | |
9375 | -- Skip processing of this entry if warning already posted | |
9376 | ||
9377 | if not Address_Warning_Posted (ACCR.N) then | |
d6da7448 | 9378 | Expr := Original_Node (Expression (ACCR.N)); |
83f8f0a6 | 9379 | |
d6da7448 | 9380 | -- Get alignments |
83f8f0a6 | 9381 | |
d6da7448 | 9382 | X_Alignment := Alignment (ACCR.X); |
9383 | Y_Alignment := Alignment (ACCR.Y); | |
83f8f0a6 | 9384 | |
9385 | -- Similarly obtain sizes | |
9386 | ||
d6da7448 | 9387 | X_Size := Esize (ACCR.X); |
9388 | Y_Size := Esize (ACCR.Y); | |
83f8f0a6 | 9389 | |
9390 | -- Check for large object overlaying smaller one | |
9391 | ||
9392 | if Y_Size > Uint_0 | |
9393 | and then X_Size > Uint_0 | |
9394 | and then X_Size > Y_Size | |
9395 | then | |
d6da7448 | 9396 | Error_Msg_NE |
9397 | ("?& overlays smaller object", ACCR.N, ACCR.X); | |
83f8f0a6 | 9398 | Error_Msg_N |
1e3532e7 | 9399 | ("\??program execution may be erroneous", ACCR.N); |
83f8f0a6 | 9400 | Error_Msg_Uint_1 := X_Size; |
9401 | Error_Msg_NE | |
1e3532e7 | 9402 | ("\??size of & is ^", ACCR.N, ACCR.X); |
83f8f0a6 | 9403 | Error_Msg_Uint_1 := Y_Size; |
9404 | Error_Msg_NE | |
1e3532e7 | 9405 | ("\??size of & is ^", ACCR.N, ACCR.Y); |
83f8f0a6 | 9406 | |
d6da7448 | 9407 | -- Check for inadequate alignment, both of the base object |
9408 | -- and of the offset, if any. | |
83f8f0a6 | 9409 | |
d6da7448 | 9410 | -- Note: we do not check the alignment if we gave a size |
9411 | -- warning, since it would likely be redundant. | |
83f8f0a6 | 9412 | |
9413 | elsif Y_Alignment /= Uint_0 | |
d6da7448 | 9414 | and then (Y_Alignment < X_Alignment |
9415 | or else (ACCR.Off | |
9416 | and then | |
9417 | Nkind (Expr) = N_Attribute_Reference | |
9418 | and then | |
9419 | Attribute_Name (Expr) = Name_Address | |
9420 | and then | |
9421 | Has_Compatible_Alignment | |
9422 | (ACCR.X, Prefix (Expr)) | |
9423 | /= Known_Compatible)) | |
83f8f0a6 | 9424 | then |
9425 | Error_Msg_NE | |
1e3532e7 | 9426 | ("??specified address for& may be inconsistent " |
9427 | & "with alignment", ACCR.N, ACCR.X); | |
83f8f0a6 | 9428 | Error_Msg_N |
1e3532e7 | 9429 | ("\??program execution may be erroneous (RM 13.3(27))", |
83f8f0a6 | 9430 | ACCR.N); |
9431 | Error_Msg_Uint_1 := X_Alignment; | |
9432 | Error_Msg_NE | |
1e3532e7 | 9433 | ("\??alignment of & is ^", ACCR.N, ACCR.X); |
83f8f0a6 | 9434 | Error_Msg_Uint_1 := Y_Alignment; |
9435 | Error_Msg_NE | |
1e3532e7 | 9436 | ("\??alignment of & is ^", ACCR.N, ACCR.Y); |
d6da7448 | 9437 | if Y_Alignment >= X_Alignment then |
9438 | Error_Msg_N | |
1e3532e7 | 9439 | ("\??but offset is not multiple of alignment", ACCR.N); |
d6da7448 | 9440 | end if; |
83f8f0a6 | 9441 | end if; |
9442 | end if; | |
9443 | end; | |
9444 | end loop; | |
9445 | end Validate_Address_Clauses; | |
9446 | ||
7717ea00 | 9447 | --------------------------- |
9448 | -- Validate_Independence -- | |
9449 | --------------------------- | |
9450 | ||
9451 | procedure Validate_Independence is | |
9452 | SU : constant Uint := UI_From_Int (System_Storage_Unit); | |
9453 | N : Node_Id; | |
9454 | E : Entity_Id; | |
9455 | IC : Boolean; | |
9456 | Comp : Entity_Id; | |
9457 | Addr : Node_Id; | |
9458 | P : Node_Id; | |
9459 | ||
9460 | procedure Check_Array_Type (Atyp : Entity_Id); | |
9461 | -- Checks if the array type Atyp has independent components, and | |
9462 | -- if not, outputs an appropriate set of error messages. | |
9463 | ||
9464 | procedure No_Independence; | |
9465 | -- Output message that independence cannot be guaranteed | |
9466 | ||
9467 | function OK_Component (C : Entity_Id) return Boolean; | |
9468 | -- Checks one component to see if it is independently accessible, and | |
9469 | -- if so yields True, otherwise yields False if independent access | |
9470 | -- cannot be guaranteed. This is a conservative routine, it only | |
9471 | -- returns True if it knows for sure, it returns False if it knows | |
9472 | -- there is a problem, or it cannot be sure there is no problem. | |
9473 | ||
9474 | procedure Reason_Bad_Component (C : Entity_Id); | |
9475 | -- Outputs continuation message if a reason can be determined for | |
9476 | -- the component C being bad. | |
9477 | ||
9478 | ---------------------- | |
9479 | -- Check_Array_Type -- | |
9480 | ---------------------- | |
9481 | ||
9482 | procedure Check_Array_Type (Atyp : Entity_Id) is | |
9483 | Ctyp : constant Entity_Id := Component_Type (Atyp); | |
9484 | ||
9485 | begin | |
9486 | -- OK if no alignment clause, no pack, and no component size | |
9487 | ||
9488 | if not Has_Component_Size_Clause (Atyp) | |
9489 | and then not Has_Alignment_Clause (Atyp) | |
9490 | and then not Is_Packed (Atyp) | |
9491 | then | |
9492 | return; | |
9493 | end if; | |
9494 | ||
9495 | -- Check actual component size | |
9496 | ||
9497 | if not Known_Component_Size (Atyp) | |
9498 | or else not (Addressable (Component_Size (Atyp)) | |
9499 | and then Component_Size (Atyp) < 64) | |
9500 | or else Component_Size (Atyp) mod Esize (Ctyp) /= 0 | |
9501 | then | |
9502 | No_Independence; | |
9503 | ||
9504 | -- Bad component size, check reason | |
9505 | ||
9506 | if Has_Component_Size_Clause (Atyp) then | |
b9e61b2a | 9507 | P := Get_Attribute_Definition_Clause |
9508 | (Atyp, Attribute_Component_Size); | |
7717ea00 | 9509 | |
9510 | if Present (P) then | |
9511 | Error_Msg_Sloc := Sloc (P); | |
9512 | Error_Msg_N ("\because of Component_Size clause#", N); | |
9513 | return; | |
9514 | end if; | |
9515 | end if; | |
9516 | ||
9517 | if Is_Packed (Atyp) then | |
9518 | P := Get_Rep_Pragma (Atyp, Name_Pack); | |
9519 | ||
9520 | if Present (P) then | |
9521 | Error_Msg_Sloc := Sloc (P); | |
9522 | Error_Msg_N ("\because of pragma Pack#", N); | |
9523 | return; | |
9524 | end if; | |
9525 | end if; | |
9526 | ||
9527 | -- No reason found, just return | |
9528 | ||
9529 | return; | |
9530 | end if; | |
9531 | ||
9532 | -- Array type is OK independence-wise | |
9533 | ||
9534 | return; | |
9535 | end Check_Array_Type; | |
9536 | ||
9537 | --------------------- | |
9538 | -- No_Independence -- | |
9539 | --------------------- | |
9540 | ||
9541 | procedure No_Independence is | |
9542 | begin | |
9543 | if Pragma_Name (N) = Name_Independent then | |
9544 | Error_Msg_NE | |
9545 | ("independence cannot be guaranteed for&", N, E); | |
9546 | else | |
9547 | Error_Msg_NE | |
9548 | ("independent components cannot be guaranteed for&", N, E); | |
9549 | end if; | |
9550 | end No_Independence; | |
9551 | ||
9552 | ------------------ | |
9553 | -- OK_Component -- | |
9554 | ------------------ | |
9555 | ||
9556 | function OK_Component (C : Entity_Id) return Boolean is | |
9557 | Rec : constant Entity_Id := Scope (C); | |
9558 | Ctyp : constant Entity_Id := Etype (C); | |
9559 | ||
9560 | begin | |
9561 | -- OK if no component clause, no Pack, and no alignment clause | |
9562 | ||
9563 | if No (Component_Clause (C)) | |
9564 | and then not Is_Packed (Rec) | |
9565 | and then not Has_Alignment_Clause (Rec) | |
9566 | then | |
9567 | return True; | |
9568 | end if; | |
9569 | ||
9570 | -- Here we look at the actual component layout. A component is | |
9571 | -- addressable if its size is a multiple of the Esize of the | |
9572 | -- component type, and its starting position in the record has | |
9573 | -- appropriate alignment, and the record itself has appropriate | |
9574 | -- alignment to guarantee the component alignment. | |
9575 | ||
9576 | -- Make sure sizes are static, always assume the worst for any | |
9577 | -- cases where we cannot check static values. | |
9578 | ||
9579 | if not (Known_Static_Esize (C) | |
b9e61b2a | 9580 | and then |
9581 | Known_Static_Esize (Ctyp)) | |
7717ea00 | 9582 | then |
9583 | return False; | |
9584 | end if; | |
9585 | ||
9586 | -- Size of component must be addressable or greater than 64 bits | |
9587 | -- and a multiple of bytes. | |
9588 | ||
b9e61b2a | 9589 | if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then |
7717ea00 | 9590 | return False; |
9591 | end if; | |
9592 | ||
9593 | -- Check size is proper multiple | |
9594 | ||
9595 | if Esize (C) mod Esize (Ctyp) /= 0 then | |
9596 | return False; | |
9597 | end if; | |
9598 | ||
9599 | -- Check alignment of component is OK | |
9600 | ||
9601 | if not Known_Component_Bit_Offset (C) | |
9602 | or else Component_Bit_Offset (C) < Uint_0 | |
9603 | or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0 | |
9604 | then | |
9605 | return False; | |
9606 | end if; | |
9607 | ||
9608 | -- Check alignment of record type is OK | |
9609 | ||
9610 | if not Known_Alignment (Rec) | |
9611 | or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0 | |
9612 | then | |
9613 | return False; | |
9614 | end if; | |
9615 | ||
9616 | -- All tests passed, component is addressable | |
9617 | ||
9618 | return True; | |
9619 | end OK_Component; | |
9620 | ||
9621 | -------------------------- | |
9622 | -- Reason_Bad_Component -- | |
9623 | -------------------------- | |
9624 | ||
9625 | procedure Reason_Bad_Component (C : Entity_Id) is | |
9626 | Rec : constant Entity_Id := Scope (C); | |
9627 | Ctyp : constant Entity_Id := Etype (C); | |
9628 | ||
9629 | begin | |
9630 | -- If component clause present assume that's the problem | |
9631 | ||
9632 | if Present (Component_Clause (C)) then | |
9633 | Error_Msg_Sloc := Sloc (Component_Clause (C)); | |
9634 | Error_Msg_N ("\because of Component_Clause#", N); | |
9635 | return; | |
9636 | end if; | |
9637 | ||
9638 | -- If pragma Pack clause present, assume that's the problem | |
9639 | ||
9640 | if Is_Packed (Rec) then | |
9641 | P := Get_Rep_Pragma (Rec, Name_Pack); | |
9642 | ||
9643 | if Present (P) then | |
9644 | Error_Msg_Sloc := Sloc (P); | |
9645 | Error_Msg_N ("\because of pragma Pack#", N); | |
9646 | return; | |
9647 | end if; | |
9648 | end if; | |
9649 | ||
9650 | -- See if record has bad alignment clause | |
9651 | ||
9652 | if Has_Alignment_Clause (Rec) | |
9653 | and then Known_Alignment (Rec) | |
9654 | and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0 | |
9655 | then | |
9656 | P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment); | |
9657 | ||
9658 | if Present (P) then | |
9659 | Error_Msg_Sloc := Sloc (P); | |
9660 | Error_Msg_N ("\because of Alignment clause#", N); | |
9661 | end if; | |
9662 | end if; | |
9663 | ||
9664 | -- Couldn't find a reason, so return without a message | |
9665 | ||
9666 | return; | |
9667 | end Reason_Bad_Component; | |
9668 | ||
9669 | -- Start of processing for Validate_Independence | |
9670 | ||
9671 | begin | |
9672 | for J in Independence_Checks.First .. Independence_Checks.Last loop | |
9673 | N := Independence_Checks.Table (J).N; | |
9674 | E := Independence_Checks.Table (J).E; | |
9675 | IC := Pragma_Name (N) = Name_Independent_Components; | |
9676 | ||
9677 | -- Deal with component case | |
9678 | ||
9679 | if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then | |
9680 | if not OK_Component (E) then | |
9681 | No_Independence; | |
9682 | Reason_Bad_Component (E); | |
9683 | goto Continue; | |
9684 | end if; | |
9685 | end if; | |
9686 | ||
9687 | -- Deal with record with Independent_Components | |
9688 | ||
9689 | if IC and then Is_Record_Type (E) then | |
9690 | Comp := First_Component_Or_Discriminant (E); | |
9691 | while Present (Comp) loop | |
9692 | if not OK_Component (Comp) then | |
9693 | No_Independence; | |
9694 | Reason_Bad_Component (Comp); | |
9695 | goto Continue; | |
9696 | end if; | |
9697 | ||
9698 | Next_Component_Or_Discriminant (Comp); | |
9699 | end loop; | |
9700 | end if; | |
9701 | ||
9702 | -- Deal with address clause case | |
9703 | ||
9704 | if Is_Object (E) then | |
9705 | Addr := Address_Clause (E); | |
9706 | ||
9707 | if Present (Addr) then | |
9708 | No_Independence; | |
9709 | Error_Msg_Sloc := Sloc (Addr); | |
9710 | Error_Msg_N ("\because of Address clause#", N); | |
9711 | goto Continue; | |
9712 | end if; | |
9713 | end if; | |
9714 | ||
9715 | -- Deal with independent components for array type | |
9716 | ||
9717 | if IC and then Is_Array_Type (E) then | |
9718 | Check_Array_Type (E); | |
9719 | end if; | |
9720 | ||
9721 | -- Deal with independent components for array object | |
9722 | ||
9723 | if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then | |
9724 | Check_Array_Type (Etype (E)); | |
9725 | end if; | |
9726 | ||
9727 | <<Continue>> null; | |
9728 | end loop; | |
9729 | end Validate_Independence; | |
9730 | ||
d6f39728 | 9731 | ----------------------------------- |
9732 | -- Validate_Unchecked_Conversion -- | |
9733 | ----------------------------------- | |
9734 | ||
9735 | procedure Validate_Unchecked_Conversion | |
9736 | (N : Node_Id; | |
9737 | Act_Unit : Entity_Id) | |
9738 | is | |
9739 | Source : Entity_Id; | |
9740 | Target : Entity_Id; | |
9741 | Vnode : Node_Id; | |
9742 | ||
9743 | begin | |
9744 | -- Obtain source and target types. Note that we call Ancestor_Subtype | |
9745 | -- here because the processing for generic instantiation always makes | |
9746 | -- subtypes, and we want the original frozen actual types. | |
9747 | ||
9748 | -- If we are dealing with private types, then do the check on their | |
9749 | -- fully declared counterparts if the full declarations have been | |
9750 | -- encountered (they don't have to be visible, but they must exist!) | |
9751 | ||
9752 | Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit))); | |
9753 | ||
9754 | if Is_Private_Type (Source) | |
9755 | and then Present (Underlying_Type (Source)) | |
9756 | then | |
9757 | Source := Underlying_Type (Source); | |
9758 | end if; | |
9759 | ||
9760 | Target := Ancestor_Subtype (Etype (Act_Unit)); | |
9761 | ||
fdd294d1 | 9762 | -- If either type is generic, the instantiation happens within a generic |
95deda50 | 9763 | -- unit, and there is nothing to check. The proper check will happen |
9764 | -- when the enclosing generic is instantiated. | |
d6f39728 | 9765 | |
9766 | if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then | |
9767 | return; | |
9768 | end if; | |
9769 | ||
9770 | if Is_Private_Type (Target) | |
9771 | and then Present (Underlying_Type (Target)) | |
9772 | then | |
9773 | Target := Underlying_Type (Target); | |
9774 | end if; | |
9775 | ||
9776 | -- Source may be unconstrained array, but not target | |
9777 | ||
b9e61b2a | 9778 | if Is_Array_Type (Target) and then not Is_Constrained (Target) then |
d6f39728 | 9779 | Error_Msg_N |
9780 | ("unchecked conversion to unconstrained array not allowed", N); | |
9781 | return; | |
9782 | end if; | |
9783 | ||
fbc67f84 | 9784 | -- Warn if conversion between two different convention pointers |
9785 | ||
9786 | if Is_Access_Type (Target) | |
9787 | and then Is_Access_Type (Source) | |
9788 | and then Convention (Target) /= Convention (Source) | |
9789 | and then Warn_On_Unchecked_Conversion | |
9790 | then | |
fdd294d1 | 9791 | -- Give warnings for subprogram pointers only on most targets. The |
9792 | -- exception is VMS, where data pointers can have different lengths | |
9793 | -- depending on the pointer convention. | |
9794 | ||
9795 | if Is_Access_Subprogram_Type (Target) | |
9796 | or else Is_Access_Subprogram_Type (Source) | |
9797 | or else OpenVMS_On_Target | |
9798 | then | |
9799 | Error_Msg_N | |
cb97ae5c | 9800 | ("?z?conversion between pointers with different conventions!", |
1e3532e7 | 9801 | N); |
fdd294d1 | 9802 | end if; |
fbc67f84 | 9803 | end if; |
9804 | ||
3062c401 | 9805 | -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a |
9806 | -- warning when compiling GNAT-related sources. | |
9807 | ||
9808 | if Warn_On_Unchecked_Conversion | |
9809 | and then not In_Predefined_Unit (N) | |
9810 | and then RTU_Loaded (Ada_Calendar) | |
9811 | and then | |
9812 | (Chars (Source) = Name_Time | |
9813 | or else | |
9814 | Chars (Target) = Name_Time) | |
9815 | then | |
9816 | -- If Ada.Calendar is loaded and the name of one of the operands is | |
9817 | -- Time, there is a good chance that this is Ada.Calendar.Time. | |
9818 | ||
9819 | declare | |
9820 | Calendar_Time : constant Entity_Id := | |
9821 | Full_View (RTE (RO_CA_Time)); | |
9822 | begin | |
9823 | pragma Assert (Present (Calendar_Time)); | |
9824 | ||
b9e61b2a | 9825 | if Source = Calendar_Time or else Target = Calendar_Time then |
3062c401 | 9826 | Error_Msg_N |
cb97ae5c | 9827 | ("?z?representation of 'Time values may change between " & |
3062c401 | 9828 | "'G'N'A'T versions", N); |
9829 | end if; | |
9830 | end; | |
9831 | end if; | |
9832 | ||
fdd294d1 | 9833 | -- Make entry in unchecked conversion table for later processing by |
9834 | -- Validate_Unchecked_Conversions, which will check sizes and alignments | |
9835 | -- (using values set by the back-end where possible). This is only done | |
9836 | -- if the appropriate warning is active. | |
d6f39728 | 9837 | |
9dfe12ae | 9838 | if Warn_On_Unchecked_Conversion then |
9839 | Unchecked_Conversions.Append | |
b9e61b2a | 9840 | (New_Val => UC_Entry'(Eloc => Sloc (N), |
9841 | Source => Source, | |
9842 | Target => Target)); | |
9dfe12ae | 9843 | |
9844 | -- If both sizes are known statically now, then back end annotation | |
9845 | -- is not required to do a proper check but if either size is not | |
9846 | -- known statically, then we need the annotation. | |
9847 | ||
9848 | if Known_Static_RM_Size (Source) | |
1e3532e7 | 9849 | and then |
9850 | Known_Static_RM_Size (Target) | |
9dfe12ae | 9851 | then |
9852 | null; | |
9853 | else | |
9854 | Back_Annotate_Rep_Info := True; | |
9855 | end if; | |
9856 | end if; | |
d6f39728 | 9857 | |
fdd294d1 | 9858 | -- If unchecked conversion to access type, and access type is declared |
95deda50 | 9859 | -- in the same unit as the unchecked conversion, then set the flag |
9860 | -- No_Strict_Aliasing (no strict aliasing is implicit here) | |
28ed91d4 | 9861 | |
9862 | if Is_Access_Type (Target) and then | |
9863 | In_Same_Source_Unit (Target, N) | |
9864 | then | |
9865 | Set_No_Strict_Aliasing (Implementation_Base_Type (Target)); | |
9866 | end if; | |
3d875462 | 9867 | |
95deda50 | 9868 | -- Generate N_Validate_Unchecked_Conversion node for back end in case |
9869 | -- the back end needs to perform special validation checks. | |
3d875462 | 9870 | |
95deda50 | 9871 | -- Shouldn't this be in Exp_Ch13, since the check only gets done if we |
9872 | -- have full expansion and the back end is called ??? | |
3d875462 | 9873 | |
9874 | Vnode := | |
9875 | Make_Validate_Unchecked_Conversion (Sloc (N)); | |
9876 | Set_Source_Type (Vnode, Source); | |
9877 | Set_Target_Type (Vnode, Target); | |
9878 | ||
fdd294d1 | 9879 | -- If the unchecked conversion node is in a list, just insert before it. |
9880 | -- If not we have some strange case, not worth bothering about. | |
3d875462 | 9881 | |
9882 | if Is_List_Member (N) then | |
d6f39728 | 9883 | Insert_After (N, Vnode); |
9884 | end if; | |
9885 | end Validate_Unchecked_Conversion; | |
9886 | ||
9887 | ------------------------------------ | |
9888 | -- Validate_Unchecked_Conversions -- | |
9889 | ------------------------------------ | |
9890 | ||
9891 | procedure Validate_Unchecked_Conversions is | |
9892 | begin | |
9893 | for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop | |
9894 | declare | |
9895 | T : UC_Entry renames Unchecked_Conversions.Table (N); | |
9896 | ||
299480f9 | 9897 | Eloc : constant Source_Ptr := T.Eloc; |
9898 | Source : constant Entity_Id := T.Source; | |
9899 | Target : constant Entity_Id := T.Target; | |
d6f39728 | 9900 | |
44705307 | 9901 | Source_Siz : Uint; |
9902 | Target_Siz : Uint; | |
d6f39728 | 9903 | |
9904 | begin | |
fdd294d1 | 9905 | -- This validation check, which warns if we have unequal sizes for |
9906 | -- unchecked conversion, and thus potentially implementation | |
d6f39728 | 9907 | -- dependent semantics, is one of the few occasions on which we |
fdd294d1 | 9908 | -- use the official RM size instead of Esize. See description in |
9909 | -- Einfo "Handling of Type'Size Values" for details. | |
d6f39728 | 9910 | |
f15731c4 | 9911 | if Serious_Errors_Detected = 0 |
d6f39728 | 9912 | and then Known_Static_RM_Size (Source) |
9913 | and then Known_Static_RM_Size (Target) | |
f25f4252 | 9914 | |
9915 | -- Don't do the check if warnings off for either type, note the | |
9916 | -- deliberate use of OR here instead of OR ELSE to get the flag | |
9917 | -- Warnings_Off_Used set for both types if appropriate. | |
9918 | ||
9919 | and then not (Has_Warnings_Off (Source) | |
9920 | or | |
9921 | Has_Warnings_Off (Target)) | |
d6f39728 | 9922 | then |
9923 | Source_Siz := RM_Size (Source); | |
9924 | Target_Siz := RM_Size (Target); | |
9925 | ||
9926 | if Source_Siz /= Target_Siz then | |
299480f9 | 9927 | Error_Msg |
cb97ae5c | 9928 | ("?z?types for unchecked conversion have different sizes!", |
299480f9 | 9929 | Eloc); |
d6f39728 | 9930 | |
9931 | if All_Errors_Mode then | |
9932 | Error_Msg_Name_1 := Chars (Source); | |
9933 | Error_Msg_Uint_1 := Source_Siz; | |
9934 | Error_Msg_Name_2 := Chars (Target); | |
9935 | Error_Msg_Uint_2 := Target_Siz; | |
cb97ae5c | 9936 | Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc); |
d6f39728 | 9937 | |
9938 | Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz); | |
9939 | ||
9940 | if Is_Discrete_Type (Source) | |
b9e61b2a | 9941 | and then |
9942 | Is_Discrete_Type (Target) | |
d6f39728 | 9943 | then |
9944 | if Source_Siz > Target_Siz then | |
299480f9 | 9945 | Error_Msg |
cb97ae5c | 9946 | ("\?z?^ high order bits of source will " |
1e3532e7 | 9947 | & "be ignored!", Eloc); |
d6f39728 | 9948 | |
9dfe12ae | 9949 | elsif Is_Unsigned_Type (Source) then |
299480f9 | 9950 | Error_Msg |
cb97ae5c | 9951 | ("\?z?source will be extended with ^ high order " |
1e3532e7 | 9952 | & "zero bits?!", Eloc); |
d6f39728 | 9953 | |
9954 | else | |
299480f9 | 9955 | Error_Msg |
cb97ae5c | 9956 | ("\?z?source will be extended with ^ high order " |
1e3532e7 | 9957 | & "sign bits!", Eloc); |
d6f39728 | 9958 | end if; |
9959 | ||
9960 | elsif Source_Siz < Target_Siz then | |
9961 | if Is_Discrete_Type (Target) then | |
9962 | if Bytes_Big_Endian then | |
299480f9 | 9963 | Error_Msg |
cb97ae5c | 9964 | ("\?z?target value will include ^ undefined " |
1e3532e7 | 9965 | & "low order bits!", Eloc); |
d6f39728 | 9966 | else |
299480f9 | 9967 | Error_Msg |
cb97ae5c | 9968 | ("\?z?target value will include ^ undefined " |
1e3532e7 | 9969 | & "high order bits!", Eloc); |
d6f39728 | 9970 | end if; |
9971 | ||
9972 | else | |
299480f9 | 9973 | Error_Msg |
cb97ae5c | 9974 | ("\?z?^ trailing bits of target value will be " |
1e3532e7 | 9975 | & "undefined!", Eloc); |
d6f39728 | 9976 | end if; |
9977 | ||
9978 | else pragma Assert (Source_Siz > Target_Siz); | |
299480f9 | 9979 | Error_Msg |
cb97ae5c | 9980 | ("\?z?^ trailing bits of source will be ignored!", |
299480f9 | 9981 | Eloc); |
d6f39728 | 9982 | end if; |
9983 | end if; | |
d6f39728 | 9984 | end if; |
9985 | end if; | |
9986 | ||
9987 | -- If both types are access types, we need to check the alignment. | |
9988 | -- If the alignment of both is specified, we can do it here. | |
9989 | ||
f15731c4 | 9990 | if Serious_Errors_Detected = 0 |
d6f39728 | 9991 | and then Ekind (Source) in Access_Kind |
9992 | and then Ekind (Target) in Access_Kind | |
9993 | and then Target_Strict_Alignment | |
9994 | and then Present (Designated_Type (Source)) | |
9995 | and then Present (Designated_Type (Target)) | |
9996 | then | |
9997 | declare | |
9998 | D_Source : constant Entity_Id := Designated_Type (Source); | |
9999 | D_Target : constant Entity_Id := Designated_Type (Target); | |
10000 | ||
10001 | begin | |
10002 | if Known_Alignment (D_Source) | |
b9e61b2a | 10003 | and then |
10004 | Known_Alignment (D_Target) | |
d6f39728 | 10005 | then |
10006 | declare | |
10007 | Source_Align : constant Uint := Alignment (D_Source); | |
10008 | Target_Align : constant Uint := Alignment (D_Target); | |
10009 | ||
10010 | begin | |
10011 | if Source_Align < Target_Align | |
10012 | and then not Is_Tagged_Type (D_Source) | |
f25f4252 | 10013 | |
10014 | -- Suppress warning if warnings suppressed on either | |
10015 | -- type or either designated type. Note the use of | |
10016 | -- OR here instead of OR ELSE. That is intentional, | |
10017 | -- we would like to set flag Warnings_Off_Used in | |
10018 | -- all types for which warnings are suppressed. | |
10019 | ||
10020 | and then not (Has_Warnings_Off (D_Source) | |
10021 | or | |
10022 | Has_Warnings_Off (D_Target) | |
10023 | or | |
10024 | Has_Warnings_Off (Source) | |
10025 | or | |
10026 | Has_Warnings_Off (Target)) | |
d6f39728 | 10027 | then |
d6f39728 | 10028 | Error_Msg_Uint_1 := Target_Align; |
10029 | Error_Msg_Uint_2 := Source_Align; | |
299480f9 | 10030 | Error_Msg_Node_1 := D_Target; |
d6f39728 | 10031 | Error_Msg_Node_2 := D_Source; |
299480f9 | 10032 | Error_Msg |
cb97ae5c | 10033 | ("?z?alignment of & (^) is stricter than " |
1e3532e7 | 10034 | & "alignment of & (^)!", Eloc); |
f25f4252 | 10035 | Error_Msg |
cb97ae5c | 10036 | ("\?z?resulting access value may have invalid " |
1e3532e7 | 10037 | & "alignment!", Eloc); |
d6f39728 | 10038 | end if; |
10039 | end; | |
10040 | end if; | |
10041 | end; | |
10042 | end if; | |
10043 | end; | |
10044 | end loop; | |
10045 | end Validate_Unchecked_Conversions; | |
10046 | ||
d6f39728 | 10047 | end Sem_Ch13; |