]>
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 | -- -- | |
2625eb01 | 9 | -- Copyright (C) 1992-2014, Free Software Foundation, Inc. -- |
d6f39728 | 10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
80df182a | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
d6f39728 | 14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
80df182a | 18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
d6f39728 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
d6f39728 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
ae888dbd | 26 | with Aspects; use Aspects; |
d6f39728 | 27 | with Atree; use Atree; |
713c00d6 | 28 | with Checks; use Checks; |
175a6969 | 29 | with Debug; use Debug; |
d6f39728 | 30 | with Einfo; use Einfo; |
d00681a7 | 31 | with Elists; use Elists; |
d6f39728 | 32 | with Errout; use Errout; |
d00681a7 | 33 | with Exp_Disp; use Exp_Disp; |
d6f39728 | 34 | with Exp_Tss; use Exp_Tss; |
35 | with Exp_Util; use Exp_Util; | |
37c6552c | 36 | with Freeze; use Freeze; |
d6f39728 | 37 | with Lib; use Lib; |
83f8f0a6 | 38 | with Lib.Xref; use Lib.Xref; |
15ebb600 | 39 | with Namet; use Namet; |
d6f39728 | 40 | with Nlists; use Nlists; |
41 | with Nmake; use Nmake; | |
42 | with Opt; use Opt; | |
e0521a36 | 43 | with Restrict; use Restrict; |
44 | with Rident; use Rident; | |
d6f39728 | 45 | with Rtsfind; use Rtsfind; |
46 | with Sem; use Sem; | |
d60c9ff7 | 47 | with Sem_Aux; use Sem_Aux; |
be9124d0 | 48 | with Sem_Case; use Sem_Case; |
40ca69b9 | 49 | with Sem_Ch3; use Sem_Ch3; |
490beba6 | 50 | with Sem_Ch6; use Sem_Ch6; |
d6f39728 | 51 | with Sem_Ch8; use Sem_Ch8; |
85696508 | 52 | with Sem_Dim; use Sem_Dim; |
85377c9b | 53 | with Sem_Disp; use Sem_Disp; |
d6f39728 | 54 | with Sem_Eval; use Sem_Eval; |
51ea9c94 | 55 | with Sem_Prag; use Sem_Prag; |
d6f39728 | 56 | with Sem_Res; use Sem_Res; |
57 | with Sem_Type; use Sem_Type; | |
58 | with Sem_Util; use Sem_Util; | |
44e4341e | 59 | with Sem_Warn; use Sem_Warn; |
1e3c4ae6 | 60 | with Sinput; use Sinput; |
9dfe12ae | 61 | with Snames; use Snames; |
d6f39728 | 62 | with Stand; use Stand; |
63 | with Sinfo; use Sinfo; | |
5b5df4a9 | 64 | with Stringt; use Stringt; |
93735cb8 | 65 | with Targparm; use Targparm; |
d6f39728 | 66 | with Ttypes; use Ttypes; |
67 | with Tbuild; use Tbuild; | |
68 | with Urealp; use Urealp; | |
f42f24d7 | 69 | with Warnsw; use Warnsw; |
d6f39728 | 70 | |
bfa5a9d9 | 71 | with GNAT.Heap_Sort_G; |
d6f39728 | 72 | |
73 | package body Sem_Ch13 is | |
74 | ||
75 | SSU : constant Pos := System_Storage_Unit; | |
76 | -- Convenient short hand for commonly used constant | |
77 | ||
78 | ----------------------- | |
79 | -- Local Subprograms -- | |
80 | ----------------------- | |
81 | ||
1d366b32 | 82 | procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint); |
83 | -- This routine is called after setting one of the sizes of type entity | |
84 | -- Typ to Size. The purpose is to deal with the situation of a derived | |
85 | -- type whose inherited alignment is no longer appropriate for the new | |
86 | -- size value. In this case, we reset the Alignment to unknown. | |
d6f39728 | 87 | |
eb66e842 | 88 | procedure Build_Discrete_Static_Predicate |
d97beb2f | 89 | (Typ : Entity_Id; |
90 | Expr : Node_Id; | |
91 | Nam : Name_Id); | |
d7c2851f | 92 | -- Given a predicated type Typ, where Typ is a discrete static subtype, |
93 | -- whose predicate expression is Expr, tests if Expr is a static predicate, | |
94 | -- and if so, builds the predicate range list. Nam is the name of the one | |
95 | -- argument to the predicate function. Occurrences of the type name in the | |
6fb3c314 | 96 | -- predicate expression have been replaced by identifier references to this |
d7c2851f | 97 | -- name, which is unique, so any identifier with Chars matching Nam must be |
98 | -- a reference to the type. If the predicate is non-static, this procedure | |
99 | -- returns doing nothing. If the predicate is static, then the predicate | |
5c6a5792 | 100 | -- list is stored in Static_Discrete_Predicate (Typ), and the Expr is |
101 | -- rewritten as a canonicalized membership operation. | |
d97beb2f | 102 | |
eb66e842 | 103 | procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id); |
104 | -- If Typ has predicates (indicated by Has_Predicates being set for Typ), | |
105 | -- then either there are pragma Predicate entries on the rep chain for the | |
106 | -- type (note that Predicate aspects are converted to pragma Predicate), or | |
107 | -- there are inherited aspects from a parent type, or ancestor subtypes. | |
108 | -- This procedure builds the spec and body for the Predicate function that | |
109 | -- tests these predicates. N is the freeze node for the type. The spec of | |
110 | -- the function is inserted before the freeze node, and the body of the | |
111 | -- function is inserted after the freeze node. If the predicate expression | |
112 | -- has at least one Raise_Expression, then this procedure also builds the | |
113 | -- M version of the predicate function for use in membership tests. | |
114 | ||
6653b695 | 115 | procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id); |
116 | -- Called if both Storage_Pool and Storage_Size attribute definition | |
117 | -- clauses (SP and SS) are present for entity Ent. Issue error message. | |
118 | ||
d9f6a4ee | 119 | procedure Freeze_Entity_Checks (N : Node_Id); |
120 | -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity | |
121 | -- to generate appropriate semantic checks that are delayed until this | |
122 | -- point (they had to be delayed this long for cases of delayed aspects, | |
123 | -- e.g. analysis of statically predicated subtypes in choices, for which | |
124 | -- we have to be sure the subtypes in question are frozen before checking. | |
125 | ||
d6f39728 | 126 | function Get_Alignment_Value (Expr : Node_Id) return Uint; |
127 | -- Given the expression for an alignment value, returns the corresponding | |
128 | -- Uint value. If the value is inappropriate, then error messages are | |
129 | -- posted as required, and a value of No_Uint is returned. | |
130 | ||
131 | function Is_Operational_Item (N : Node_Id) return Boolean; | |
1e3c4ae6 | 132 | -- A specification for a stream attribute is allowed before the full type |
133 | -- is declared, as explained in AI-00137 and the corrigendum. Attributes | |
134 | -- that do not specify a representation characteristic are operational | |
135 | -- attributes. | |
d6f39728 | 136 | |
3b23aaa0 | 137 | function Is_Predicate_Static |
138 | (Expr : Node_Id; | |
139 | Nam : Name_Id) return Boolean; | |
140 | -- Given predicate expression Expr, tests if Expr is predicate-static in | |
141 | -- the sense of the rules in (RM 3.2.4 (15-24)). Occurrences of the type | |
142 | -- name in the predicate expression have been replaced by references to | |
143 | -- an identifier whose Chars field is Nam. This name is unique, so any | |
144 | -- identifier with Chars matching Nam must be a reference to the type. | |
145 | -- Returns True if the expression is predicate-static and False otherwise, | |
146 | -- but is not in the business of setting flags or issuing error messages. | |
147 | -- | |
148 | -- Only scalar types can have static predicates, so False is always | |
149 | -- returned for non-scalar types. | |
150 | -- | |
151 | -- Note: the RM seems to suggest that string types can also have static | |
152 | -- predicates. But that really makes lttle sense as very few useful | |
153 | -- predicates can be constructed for strings. Remember that: | |
154 | -- | |
155 | -- "ABC" < "DEF" | |
156 | -- | |
157 | -- is not a static expression. So even though the clearly faulty RM wording | |
158 | -- allows the following: | |
159 | -- | |
160 | -- subtype S is String with Static_Predicate => S < "DEF" | |
161 | -- | |
162 | -- We can't allow this, otherwise we have predicate-static applying to a | |
163 | -- larger class than static expressions, which was never intended. | |
164 | ||
44e4341e | 165 | procedure New_Stream_Subprogram |
d6f39728 | 166 | (N : Node_Id; |
167 | Ent : Entity_Id; | |
168 | Subp : Entity_Id; | |
9dfe12ae | 169 | Nam : TSS_Name_Type); |
44e4341e | 170 | -- Create a subprogram renaming of a given stream attribute to the |
171 | -- designated subprogram and then in the tagged case, provide this as a | |
d1a2e31b | 172 | -- primitive operation, or in the untagged case make an appropriate TSS |
44e4341e | 173 | -- entry. This is more properly an expansion activity than just semantics, |
d1a2e31b | 174 | -- but the presence of user-defined stream functions for limited types |
175 | -- is a legality check, which is why this takes place here rather than in | |
44e4341e | 176 | -- exp_ch13, where it was previously. Nam indicates the name of the TSS |
177 | -- function to be generated. | |
9dfe12ae | 178 | -- |
f15731c4 | 179 | -- To avoid elaboration anomalies with freeze nodes, for untagged types |
180 | -- we generate both a subprogram declaration and a subprogram renaming | |
181 | -- declaration, so that the attribute specification is handled as a | |
182 | -- renaming_as_body. For tagged types, the specification is one of the | |
183 | -- primitive specs. | |
184 | ||
3061ffde | 185 | procedure Resolve_Iterable_Operation |
186 | (N : Node_Id; | |
187 | Cursor : Entity_Id; | |
188 | Typ : Entity_Id; | |
189 | Nam : Name_Id); | |
190 | -- If the name of a primitive operation for an Iterable aspect is | |
191 | -- overloaded, resolve according to required signature. | |
192 | ||
b77e4501 | 193 | procedure Set_Biased |
194 | (E : Entity_Id; | |
195 | N : Node_Id; | |
196 | Msg : String; | |
197 | Biased : Boolean := True); | |
198 | -- If Biased is True, sets Has_Biased_Representation flag for E, and | |
199 | -- outputs a warning message at node N if Warn_On_Biased_Representation is | |
200 | -- is True. This warning inserts the string Msg to describe the construct | |
201 | -- causing biasing. | |
202 | ||
d6f39728 | 203 | ---------------------------------------------- |
204 | -- Table for Validate_Unchecked_Conversions -- | |
205 | ---------------------------------------------- | |
206 | ||
207 | -- The following table collects unchecked conversions for validation. | |
95deda50 | 208 | -- Entries are made by Validate_Unchecked_Conversion and then the call |
209 | -- to Validate_Unchecked_Conversions does the actual error checking and | |
210 | -- posting of warnings. The reason for this delayed processing is to take | |
211 | -- advantage of back-annotations of size and alignment values performed by | |
212 | -- the back end. | |
d6f39728 | 213 | |
95deda50 | 214 | -- Note: the reason we store a Source_Ptr value instead of a Node_Id is |
215 | -- that by the time Validate_Unchecked_Conversions is called, Sprint will | |
216 | -- already have modified all Sloc values if the -gnatD option is set. | |
299480f9 | 217 | |
d6f39728 | 218 | type UC_Entry is record |
86d32751 | 219 | Eloc : Source_Ptr; -- node used for posting warnings |
220 | Source : Entity_Id; -- source type for unchecked conversion | |
221 | Target : Entity_Id; -- target type for unchecked conversion | |
222 | Act_Unit : Entity_Id; -- actual function instantiated | |
d6f39728 | 223 | end record; |
224 | ||
225 | package Unchecked_Conversions is new Table.Table ( | |
226 | Table_Component_Type => UC_Entry, | |
227 | Table_Index_Type => Int, | |
228 | Table_Low_Bound => 1, | |
229 | Table_Initial => 50, | |
230 | Table_Increment => 200, | |
231 | Table_Name => "Unchecked_Conversions"); | |
232 | ||
83f8f0a6 | 233 | ---------------------------------------- |
234 | -- Table for Validate_Address_Clauses -- | |
235 | ---------------------------------------- | |
236 | ||
237 | -- If an address clause has the form | |
238 | ||
239 | -- for X'Address use Expr | |
240 | ||
95deda50 | 241 | -- where Expr is of the form Y'Address or recursively is a reference to a |
242 | -- constant of either of these forms, and X and Y are entities of objects, | |
243 | -- then if Y has a smaller alignment than X, that merits a warning about | |
244 | -- possible bad alignment. The following table collects address clauses of | |
245 | -- this kind. We put these in a table so that they can be checked after the | |
246 | -- back end has completed annotation of the alignments of objects, since we | |
247 | -- can catch more cases that way. | |
83f8f0a6 | 248 | |
249 | type Address_Clause_Check_Record is record | |
250 | N : Node_Id; | |
251 | -- The address clause | |
252 | ||
253 | X : Entity_Id; | |
254 | -- The entity of the object overlaying Y | |
255 | ||
256 | Y : Entity_Id; | |
257 | -- The entity of the object being overlaid | |
d6da7448 | 258 | |
259 | Off : Boolean; | |
6fb3c314 | 260 | -- Whether the address is offset within Y |
83f8f0a6 | 261 | end record; |
262 | ||
263 | package Address_Clause_Checks is new Table.Table ( | |
264 | Table_Component_Type => Address_Clause_Check_Record, | |
265 | Table_Index_Type => Int, | |
266 | Table_Low_Bound => 1, | |
267 | Table_Initial => 20, | |
268 | Table_Increment => 200, | |
269 | Table_Name => "Address_Clause_Checks"); | |
270 | ||
59ac57b5 | 271 | ----------------------------------------- |
272 | -- Adjust_Record_For_Reverse_Bit_Order -- | |
273 | ----------------------------------------- | |
274 | ||
275 | procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is | |
67278d60 | 276 | Comp : Node_Id; |
277 | CC : Node_Id; | |
59ac57b5 | 278 | |
279 | begin | |
67278d60 | 280 | -- Processing depends on version of Ada |
59ac57b5 | 281 | |
6797073f | 282 | -- For Ada 95, we just renumber bits within a storage unit. We do the |
568b0f6a | 283 | -- same for Ada 83 mode, since we recognize the Bit_Order attribute in |
ab19a652 | 284 | -- Ada 83, and are free to add this extension. |
6797073f | 285 | |
286 | if Ada_Version < Ada_2005 then | |
287 | Comp := First_Component_Or_Discriminant (R); | |
288 | while Present (Comp) loop | |
289 | CC := Component_Clause (Comp); | |
290 | ||
291 | -- If component clause is present, then deal with the non-default | |
292 | -- bit order case for Ada 95 mode. | |
293 | ||
294 | -- We only do this processing for the base type, and in fact that | |
295 | -- is important, since otherwise if there are record subtypes, we | |
296 | -- could reverse the bits once for each subtype, which is wrong. | |
297 | ||
b9e61b2a | 298 | if Present (CC) and then Ekind (R) = E_Record_Type then |
6797073f | 299 | declare |
300 | CFB : constant Uint := Component_Bit_Offset (Comp); | |
301 | CSZ : constant Uint := Esize (Comp); | |
302 | CLC : constant Node_Id := Component_Clause (Comp); | |
303 | Pos : constant Node_Id := Position (CLC); | |
304 | FB : constant Node_Id := First_Bit (CLC); | |
305 | ||
306 | Storage_Unit_Offset : constant Uint := | |
307 | CFB / System_Storage_Unit; | |
308 | ||
309 | Start_Bit : constant Uint := | |
310 | CFB mod System_Storage_Unit; | |
59ac57b5 | 311 | |
6797073f | 312 | begin |
313 | -- Cases where field goes over storage unit boundary | |
59ac57b5 | 314 | |
6797073f | 315 | if Start_Bit + CSZ > System_Storage_Unit then |
59ac57b5 | 316 | |
6797073f | 317 | -- Allow multi-byte field but generate warning |
59ac57b5 | 318 | |
6797073f | 319 | if Start_Bit mod System_Storage_Unit = 0 |
320 | and then CSZ mod System_Storage_Unit = 0 | |
321 | then | |
322 | Error_Msg_N | |
7a41db5b | 323 | ("info: multi-byte field specified with " |
324 | & "non-standard Bit_Order?V?", CLC); | |
31486bc0 | 325 | |
6797073f | 326 | if Bytes_Big_Endian then |
31486bc0 | 327 | Error_Msg_N |
7a41db5b | 328 | ("\bytes are not reversed " |
329 | & "(component is big-endian)?V?", CLC); | |
31486bc0 | 330 | else |
331 | Error_Msg_N | |
7a41db5b | 332 | ("\bytes are not reversed " |
333 | & "(component is little-endian)?V?", CLC); | |
31486bc0 | 334 | end if; |
59ac57b5 | 335 | |
6797073f | 336 | -- Do not allow non-contiguous field |
59ac57b5 | 337 | |
67278d60 | 338 | else |
6797073f | 339 | Error_Msg_N |
340 | ("attempt to specify non-contiguous field " | |
341 | & "not permitted", CLC); | |
342 | Error_Msg_N | |
343 | ("\caused by non-standard Bit_Order " | |
344 | & "specified", CLC); | |
345 | Error_Msg_N | |
346 | ("\consider possibility of using " | |
347 | & "Ada 2005 mode here", CLC); | |
348 | end if; | |
59ac57b5 | 349 | |
6797073f | 350 | -- Case where field fits in one storage unit |
59ac57b5 | 351 | |
6797073f | 352 | else |
353 | -- Give warning if suspicious component clause | |
59ac57b5 | 354 | |
6797073f | 355 | if Intval (FB) >= System_Storage_Unit |
356 | and then Warn_On_Reverse_Bit_Order | |
357 | then | |
358 | Error_Msg_N | |
7a41db5b | 359 | ("info: Bit_Order clause does not affect " & |
1e3532e7 | 360 | "byte ordering?V?", Pos); |
6797073f | 361 | Error_Msg_Uint_1 := |
362 | Intval (Pos) + Intval (FB) / | |
363 | System_Storage_Unit; | |
364 | Error_Msg_N | |
7a41db5b | 365 | ("info: position normalized to ^ before bit " & |
1e3532e7 | 366 | "order interpreted?V?", Pos); |
6797073f | 367 | end if; |
59ac57b5 | 368 | |
6797073f | 369 | -- Here is where we fix up the Component_Bit_Offset value |
370 | -- to account for the reverse bit order. Some examples of | |
371 | -- what needs to be done are: | |
bfa5a9d9 | 372 | |
6797073f | 373 | -- First_Bit .. Last_Bit Component_Bit_Offset |
374 | -- old new old new | |
59ac57b5 | 375 | |
6797073f | 376 | -- 0 .. 0 7 .. 7 0 7 |
377 | -- 0 .. 1 6 .. 7 0 6 | |
378 | -- 0 .. 2 5 .. 7 0 5 | |
379 | -- 0 .. 7 0 .. 7 0 4 | |
59ac57b5 | 380 | |
6797073f | 381 | -- 1 .. 1 6 .. 6 1 6 |
382 | -- 1 .. 4 3 .. 6 1 3 | |
383 | -- 4 .. 7 0 .. 3 4 0 | |
59ac57b5 | 384 | |
6797073f | 385 | -- The rule is that the first bit is is obtained by |
386 | -- subtracting the old ending bit from storage_unit - 1. | |
59ac57b5 | 387 | |
6797073f | 388 | Set_Component_Bit_Offset |
389 | (Comp, | |
390 | (Storage_Unit_Offset * System_Storage_Unit) + | |
391 | (System_Storage_Unit - 1) - | |
392 | (Start_Bit + CSZ - 1)); | |
59ac57b5 | 393 | |
6797073f | 394 | Set_Normalized_First_Bit |
395 | (Comp, | |
396 | Component_Bit_Offset (Comp) mod | |
397 | System_Storage_Unit); | |
398 | end if; | |
399 | end; | |
400 | end if; | |
401 | ||
402 | Next_Component_Or_Discriminant (Comp); | |
403 | end loop; | |
404 | ||
405 | -- For Ada 2005, we do machine scalar processing, as fully described In | |
406 | -- AI-133. This involves gathering all components which start at the | |
407 | -- same byte offset and processing them together. Same approach is still | |
408 | -- valid in later versions including Ada 2012. | |
409 | ||
410 | else | |
411 | declare | |
412 | Max_Machine_Scalar_Size : constant Uint := | |
413 | UI_From_Int | |
414 | (Standard_Long_Long_Integer_Size); | |
67278d60 | 415 | -- We use this as the maximum machine scalar size |
59ac57b5 | 416 | |
6797073f | 417 | Num_CC : Natural; |
418 | SSU : constant Uint := UI_From_Int (System_Storage_Unit); | |
59ac57b5 | 419 | |
6797073f | 420 | begin |
421 | -- This first loop through components does two things. First it | |
422 | -- deals with the case of components with component clauses whose | |
423 | -- length is greater than the maximum machine scalar size (either | |
424 | -- accepting them or rejecting as needed). Second, it counts the | |
425 | -- number of components with component clauses whose length does | |
426 | -- not exceed this maximum for later processing. | |
67278d60 | 427 | |
6797073f | 428 | Num_CC := 0; |
429 | Comp := First_Component_Or_Discriminant (R); | |
430 | while Present (Comp) loop | |
431 | CC := Component_Clause (Comp); | |
67278d60 | 432 | |
6797073f | 433 | if Present (CC) then |
434 | declare | |
1e3532e7 | 435 | Fbit : constant Uint := Static_Integer (First_Bit (CC)); |
436 | Lbit : constant Uint := Static_Integer (Last_Bit (CC)); | |
67278d60 | 437 | |
6797073f | 438 | begin |
b38e4131 | 439 | -- Case of component with last bit >= max machine scalar |
67278d60 | 440 | |
b38e4131 | 441 | if Lbit >= Max_Machine_Scalar_Size then |
67278d60 | 442 | |
b38e4131 | 443 | -- This is allowed only if first bit is zero, and |
444 | -- last bit + 1 is a multiple of storage unit size. | |
67278d60 | 445 | |
b38e4131 | 446 | if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then |
67278d60 | 447 | |
b38e4131 | 448 | -- This is the case to give a warning if enabled |
67278d60 | 449 | |
b38e4131 | 450 | if Warn_On_Reverse_Bit_Order then |
451 | Error_Msg_N | |
7a41db5b | 452 | ("info: multi-byte field specified with " |
1e3532e7 | 453 | & " non-standard Bit_Order?V?", CC); |
b38e4131 | 454 | |
455 | if Bytes_Big_Endian then | |
456 | Error_Msg_N | |
457 | ("\bytes are not reversed " | |
1e3532e7 | 458 | & "(component is big-endian)?V?", CC); |
b38e4131 | 459 | else |
460 | Error_Msg_N | |
461 | ("\bytes are not reversed " | |
1e3532e7 | 462 | & "(component is little-endian)?V?", CC); |
b38e4131 | 463 | end if; |
464 | end if; | |
67278d60 | 465 | |
7eb0e22f | 466 | -- Give error message for RM 13.5.1(10) violation |
67278d60 | 467 | |
b38e4131 | 468 | else |
469 | Error_Msg_FE | |
470 | ("machine scalar rules not followed for&", | |
471 | First_Bit (CC), Comp); | |
67278d60 | 472 | |
b38e4131 | 473 | Error_Msg_Uint_1 := Lbit; |
474 | Error_Msg_Uint_2 := Max_Machine_Scalar_Size; | |
475 | Error_Msg_F | |
476 | ("\last bit (^) exceeds maximum machine " | |
477 | & "scalar size (^)", | |
478 | First_Bit (CC)); | |
67278d60 | 479 | |
b38e4131 | 480 | if (Lbit + 1) mod SSU /= 0 then |
481 | Error_Msg_Uint_1 := SSU; | |
482 | Error_Msg_F | |
483 | ("\and is not a multiple of Storage_Unit (^) " | |
0cafb066 | 484 | & "(RM 13.4.1(10))", |
b38e4131 | 485 | First_Bit (CC)); |
6797073f | 486 | |
6797073f | 487 | else |
b38e4131 | 488 | Error_Msg_Uint_1 := Fbit; |
489 | Error_Msg_F | |
490 | ("\and first bit (^) is non-zero " | |
0cafb066 | 491 | & "(RM 13.4.1(10))", |
b38e4131 | 492 | First_Bit (CC)); |
67278d60 | 493 | end if; |
6797073f | 494 | end if; |
59ac57b5 | 495 | |
b38e4131 | 496 | -- OK case of machine scalar related component clause, |
497 | -- For now, just count them. | |
59ac57b5 | 498 | |
6797073f | 499 | else |
500 | Num_CC := Num_CC + 1; | |
501 | end if; | |
502 | end; | |
503 | end if; | |
59ac57b5 | 504 | |
6797073f | 505 | Next_Component_Or_Discriminant (Comp); |
506 | end loop; | |
59ac57b5 | 507 | |
6797073f | 508 | -- We need to sort the component clauses on the basis of the |
509 | -- Position values in the clause, so we can group clauses with | |
4a87c513 | 510 | -- the same Position together to determine the relevant machine |
6797073f | 511 | -- scalar size. |
59ac57b5 | 512 | |
6797073f | 513 | Sort_CC : declare |
514 | Comps : array (0 .. Num_CC) of Entity_Id; | |
515 | -- Array to collect component and discriminant entities. The | |
516 | -- data starts at index 1, the 0'th entry is for the sort | |
517 | -- routine. | |
59ac57b5 | 518 | |
6797073f | 519 | function CP_Lt (Op1, Op2 : Natural) return Boolean; |
520 | -- Compare routine for Sort | |
59ac57b5 | 521 | |
6797073f | 522 | procedure CP_Move (From : Natural; To : Natural); |
523 | -- Move routine for Sort | |
59ac57b5 | 524 | |
6797073f | 525 | package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt); |
59ac57b5 | 526 | |
6797073f | 527 | Start : Natural; |
528 | Stop : Natural; | |
529 | -- Start and stop positions in the component list of the set of | |
530 | -- components with the same starting position (that constitute | |
531 | -- components in a single machine scalar). | |
59ac57b5 | 532 | |
6797073f | 533 | MaxL : Uint; |
534 | -- Maximum last bit value of any component in this set | |
59ac57b5 | 535 | |
6797073f | 536 | MSS : Uint; |
537 | -- Corresponding machine scalar size | |
67278d60 | 538 | |
6797073f | 539 | ----------- |
540 | -- CP_Lt -- | |
541 | ----------- | |
67278d60 | 542 | |
6797073f | 543 | function CP_Lt (Op1, Op2 : Natural) return Boolean is |
544 | begin | |
545 | return Position (Component_Clause (Comps (Op1))) < | |
546 | Position (Component_Clause (Comps (Op2))); | |
547 | end CP_Lt; | |
67278d60 | 548 | |
6797073f | 549 | ------------- |
550 | -- CP_Move -- | |
551 | ------------- | |
67278d60 | 552 | |
6797073f | 553 | procedure CP_Move (From : Natural; To : Natural) is |
554 | begin | |
555 | Comps (To) := Comps (From); | |
556 | end CP_Move; | |
67278d60 | 557 | |
4a87c513 | 558 | -- Start of processing for Sort_CC |
59ac57b5 | 559 | |
6797073f | 560 | begin |
b38e4131 | 561 | -- Collect the machine scalar relevant component clauses |
59ac57b5 | 562 | |
6797073f | 563 | Num_CC := 0; |
564 | Comp := First_Component_Or_Discriminant (R); | |
565 | while Present (Comp) loop | |
b38e4131 | 566 | declare |
567 | CC : constant Node_Id := Component_Clause (Comp); | |
568 | ||
569 | begin | |
570 | -- Collect only component clauses whose last bit is less | |
571 | -- than machine scalar size. Any component clause whose | |
572 | -- last bit exceeds this value does not take part in | |
573 | -- machine scalar layout considerations. The test for | |
574 | -- Error_Posted makes sure we exclude component clauses | |
575 | -- for which we already posted an error. | |
576 | ||
577 | if Present (CC) | |
578 | and then not Error_Posted (Last_Bit (CC)) | |
579 | and then Static_Integer (Last_Bit (CC)) < | |
d64221a7 | 580 | Max_Machine_Scalar_Size |
b38e4131 | 581 | then |
582 | Num_CC := Num_CC + 1; | |
583 | Comps (Num_CC) := Comp; | |
584 | end if; | |
585 | end; | |
59ac57b5 | 586 | |
6797073f | 587 | Next_Component_Or_Discriminant (Comp); |
588 | end loop; | |
67278d60 | 589 | |
6797073f | 590 | -- Sort by ascending position number |
67278d60 | 591 | |
6797073f | 592 | Sorting.Sort (Num_CC); |
67278d60 | 593 | |
6797073f | 594 | -- We now have all the components whose size does not exceed |
595 | -- the max machine scalar value, sorted by starting position. | |
596 | -- In this loop we gather groups of clauses starting at the | |
597 | -- same position, to process them in accordance with AI-133. | |
67278d60 | 598 | |
6797073f | 599 | Stop := 0; |
600 | while Stop < Num_CC loop | |
601 | Start := Stop + 1; | |
602 | Stop := Start; | |
603 | MaxL := | |
604 | Static_Integer | |
605 | (Last_Bit (Component_Clause (Comps (Start)))); | |
67278d60 | 606 | while Stop < Num_CC loop |
6797073f | 607 | if Static_Integer |
608 | (Position (Component_Clause (Comps (Stop + 1)))) = | |
609 | Static_Integer | |
610 | (Position (Component_Clause (Comps (Stop)))) | |
611 | then | |
612 | Stop := Stop + 1; | |
613 | MaxL := | |
614 | UI_Max | |
615 | (MaxL, | |
616 | Static_Integer | |
617 | (Last_Bit | |
618 | (Component_Clause (Comps (Stop))))); | |
619 | else | |
620 | exit; | |
621 | end if; | |
622 | end loop; | |
67278d60 | 623 | |
6797073f | 624 | -- Now we have a group of component clauses from Start to |
625 | -- Stop whose positions are identical, and MaxL is the | |
626 | -- maximum last bit value of any of these components. | |
627 | ||
628 | -- We need to determine the corresponding machine scalar | |
629 | -- size. This loop assumes that machine scalar sizes are | |
630 | -- even, and that each possible machine scalar has twice | |
631 | -- as many bits as the next smaller one. | |
632 | ||
633 | MSS := Max_Machine_Scalar_Size; | |
634 | while MSS mod 2 = 0 | |
635 | and then (MSS / 2) >= SSU | |
636 | and then (MSS / 2) > MaxL | |
637 | loop | |
638 | MSS := MSS / 2; | |
639 | end loop; | |
67278d60 | 640 | |
6797073f | 641 | -- Here is where we fix up the Component_Bit_Offset value |
642 | -- to account for the reverse bit order. Some examples of | |
643 | -- what needs to be done for the case of a machine scalar | |
644 | -- size of 8 are: | |
67278d60 | 645 | |
6797073f | 646 | -- First_Bit .. Last_Bit Component_Bit_Offset |
647 | -- old new old new | |
67278d60 | 648 | |
6797073f | 649 | -- 0 .. 0 7 .. 7 0 7 |
650 | -- 0 .. 1 6 .. 7 0 6 | |
651 | -- 0 .. 2 5 .. 7 0 5 | |
652 | -- 0 .. 7 0 .. 7 0 4 | |
67278d60 | 653 | |
6797073f | 654 | -- 1 .. 1 6 .. 6 1 6 |
655 | -- 1 .. 4 3 .. 6 1 3 | |
656 | -- 4 .. 7 0 .. 3 4 0 | |
67278d60 | 657 | |
6797073f | 658 | -- The rule is that the first bit is obtained by subtracting |
659 | -- the old ending bit from machine scalar size - 1. | |
67278d60 | 660 | |
6797073f | 661 | for C in Start .. Stop loop |
662 | declare | |
663 | Comp : constant Entity_Id := Comps (C); | |
b9e61b2a | 664 | CC : constant Node_Id := Component_Clause (Comp); |
665 | ||
666 | LB : constant Uint := Static_Integer (Last_Bit (CC)); | |
6797073f | 667 | NFB : constant Uint := MSS - Uint_1 - LB; |
668 | NLB : constant Uint := NFB + Esize (Comp) - 1; | |
b9e61b2a | 669 | Pos : constant Uint := Static_Integer (Position (CC)); |
67278d60 | 670 | |
6797073f | 671 | begin |
672 | if Warn_On_Reverse_Bit_Order then | |
673 | Error_Msg_Uint_1 := MSS; | |
674 | Error_Msg_N | |
675 | ("info: reverse bit order in machine " & | |
1e3532e7 | 676 | "scalar of length^?V?", First_Bit (CC)); |
6797073f | 677 | Error_Msg_Uint_1 := NFB; |
678 | Error_Msg_Uint_2 := NLB; | |
679 | ||
680 | if Bytes_Big_Endian then | |
681 | Error_Msg_NE | |
7a41db5b | 682 | ("\big-endian range for component " |
683 | & "& is ^ .. ^?V?", First_Bit (CC), Comp); | |
6797073f | 684 | else |
685 | Error_Msg_NE | |
7a41db5b | 686 | ("\little-endian range for component" |
687 | & "& is ^ .. ^?V?", First_Bit (CC), Comp); | |
67278d60 | 688 | end if; |
6797073f | 689 | end if; |
67278d60 | 690 | |
6797073f | 691 | Set_Component_Bit_Offset (Comp, Pos * SSU + NFB); |
692 | Set_Normalized_First_Bit (Comp, NFB mod SSU); | |
693 | end; | |
67278d60 | 694 | end loop; |
6797073f | 695 | end loop; |
696 | end Sort_CC; | |
697 | end; | |
698 | end if; | |
59ac57b5 | 699 | end Adjust_Record_For_Reverse_Bit_Order; |
700 | ||
1d366b32 | 701 | ------------------------------------- |
702 | -- Alignment_Check_For_Size_Change -- | |
703 | ------------------------------------- | |
d6f39728 | 704 | |
1d366b32 | 705 | procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is |
d6f39728 | 706 | begin |
707 | -- If the alignment is known, and not set by a rep clause, and is | |
708 | -- inconsistent with the size being set, then reset it to unknown, | |
709 | -- we assume in this case that the size overrides the inherited | |
710 | -- alignment, and that the alignment must be recomputed. | |
711 | ||
712 | if Known_Alignment (Typ) | |
713 | and then not Has_Alignment_Clause (Typ) | |
1d366b32 | 714 | and then Size mod (Alignment (Typ) * SSU) /= 0 |
d6f39728 | 715 | then |
716 | Init_Alignment (Typ); | |
717 | end if; | |
1d366b32 | 718 | end Alignment_Check_For_Size_Change; |
d6f39728 | 719 | |
06ef5f86 | 720 | ------------------------------------- |
721 | -- Analyze_Aspects_At_Freeze_Point -- | |
722 | ------------------------------------- | |
723 | ||
724 | procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is | |
725 | ASN : Node_Id; | |
726 | A_Id : Aspect_Id; | |
727 | Ritem : Node_Id; | |
728 | ||
729 | procedure Analyze_Aspect_Default_Value (ASN : Node_Id); | |
730 | -- This routine analyzes an Aspect_Default_[Component_]Value denoted by | |
731 | -- the aspect specification node ASN. | |
732 | ||
37c6e44c | 733 | procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id); |
734 | -- As discussed in the spec of Aspects (see Aspect_Delay declaration), | |
735 | -- a derived type can inherit aspects from its parent which have been | |
736 | -- specified at the time of the derivation using an aspect, as in: | |
737 | -- | |
738 | -- type A is range 1 .. 10 | |
739 | -- with Size => Not_Defined_Yet; | |
740 | -- .. | |
741 | -- type B is new A; | |
742 | -- .. | |
743 | -- Not_Defined_Yet : constant := 64; | |
744 | -- | |
745 | -- In this example, the Size of A is considered to be specified prior | |
746 | -- to the derivation, and thus inherited, even though the value is not | |
747 | -- known at the time of derivation. To deal with this, we use two entity | |
748 | -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A | |
749 | -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in | |
750 | -- the derived type (B here). If this flag is set when the derived type | |
751 | -- is frozen, then this procedure is called to ensure proper inheritance | |
b21edad9 | 752 | -- of all delayed aspects from the parent type. The derived type is E, |
37c6e44c | 753 | -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first |
754 | -- aspect specification node in the Rep_Item chain for the parent type. | |
755 | ||
06ef5f86 | 756 | procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id); |
757 | -- Given an aspect specification node ASN whose expression is an | |
758 | -- optional Boolean, this routines creates the corresponding pragma | |
759 | -- at the freezing point. | |
760 | ||
761 | ---------------------------------- | |
762 | -- Analyze_Aspect_Default_Value -- | |
763 | ---------------------------------- | |
764 | ||
765 | procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is | |
766 | Ent : constant Entity_Id := Entity (ASN); | |
767 | Expr : constant Node_Id := Expression (ASN); | |
768 | Id : constant Node_Id := Identifier (ASN); | |
769 | ||
770 | begin | |
771 | Error_Msg_Name_1 := Chars (Id); | |
772 | ||
773 | if not Is_Type (Ent) then | |
774 | Error_Msg_N ("aspect% can only apply to a type", Id); | |
775 | return; | |
776 | ||
777 | elsif not Is_First_Subtype (Ent) then | |
778 | Error_Msg_N ("aspect% cannot apply to subtype", Id); | |
779 | return; | |
780 | ||
781 | elsif A_Id = Aspect_Default_Value | |
782 | and then not Is_Scalar_Type (Ent) | |
783 | then | |
784 | Error_Msg_N ("aspect% can only be applied to scalar type", Id); | |
785 | return; | |
786 | ||
787 | elsif A_Id = Aspect_Default_Component_Value then | |
788 | if not Is_Array_Type (Ent) then | |
789 | Error_Msg_N ("aspect% can only be applied to array type", Id); | |
790 | return; | |
791 | ||
792 | elsif not Is_Scalar_Type (Component_Type (Ent)) then | |
793 | Error_Msg_N ("aspect% requires scalar components", Id); | |
794 | return; | |
795 | end if; | |
796 | end if; | |
797 | ||
798 | Set_Has_Default_Aspect (Base_Type (Ent)); | |
799 | ||
800 | if Is_Scalar_Type (Ent) then | |
9f36e3fb | 801 | Set_Default_Aspect_Value (Base_Type (Ent), Expr); |
06ef5f86 | 802 | else |
f3d70f08 | 803 | Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr); |
06ef5f86 | 804 | end if; |
805 | end Analyze_Aspect_Default_Value; | |
806 | ||
37c6e44c | 807 | --------------------------------- |
808 | -- Inherit_Delayed_Rep_Aspects -- | |
809 | --------------------------------- | |
810 | ||
811 | procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is | |
812 | P : constant Entity_Id := Entity (ASN); | |
813 | -- Entithy for parent type | |
814 | ||
815 | N : Node_Id; | |
816 | -- Item from Rep_Item chain | |
817 | ||
818 | A : Aspect_Id; | |
819 | ||
820 | begin | |
821 | -- Loop through delayed aspects for the parent type | |
822 | ||
823 | N := ASN; | |
824 | while Present (N) loop | |
825 | if Nkind (N) = N_Aspect_Specification then | |
826 | exit when Entity (N) /= P; | |
827 | ||
828 | if Is_Delayed_Aspect (N) then | |
829 | A := Get_Aspect_Id (Chars (Identifier (N))); | |
830 | ||
831 | -- Process delayed rep aspect. For Boolean attributes it is | |
832 | -- not possible to cancel an attribute once set (the attempt | |
833 | -- to use an aspect with xxx => False is an error) for a | |
834 | -- derived type. So for those cases, we do not have to check | |
835 | -- if a clause has been given for the derived type, since it | |
836 | -- is harmless to set it again if it is already set. | |
837 | ||
838 | case A is | |
839 | ||
840 | -- Alignment | |
841 | ||
842 | when Aspect_Alignment => | |
843 | if not Has_Alignment_Clause (E) then | |
844 | Set_Alignment (E, Alignment (P)); | |
845 | end if; | |
846 | ||
847 | -- Atomic | |
848 | ||
849 | when Aspect_Atomic => | |
850 | if Is_Atomic (P) then | |
851 | Set_Is_Atomic (E); | |
852 | end if; | |
853 | ||
854 | -- Atomic_Components | |
855 | ||
856 | when Aspect_Atomic_Components => | |
857 | if Has_Atomic_Components (P) then | |
858 | Set_Has_Atomic_Components (Base_Type (E)); | |
859 | end if; | |
860 | ||
861 | -- Bit_Order | |
862 | ||
863 | when Aspect_Bit_Order => | |
864 | if Is_Record_Type (E) | |
865 | and then No (Get_Attribute_Definition_Clause | |
866 | (E, Attribute_Bit_Order)) | |
867 | and then Reverse_Bit_Order (P) | |
868 | then | |
869 | Set_Reverse_Bit_Order (Base_Type (E)); | |
870 | end if; | |
871 | ||
872 | -- Component_Size | |
873 | ||
874 | when Aspect_Component_Size => | |
875 | if Is_Array_Type (E) | |
876 | and then not Has_Component_Size_Clause (E) | |
877 | then | |
878 | Set_Component_Size | |
879 | (Base_Type (E), Component_Size (P)); | |
880 | end if; | |
881 | ||
882 | -- Machine_Radix | |
883 | ||
884 | when Aspect_Machine_Radix => | |
885 | if Is_Decimal_Fixed_Point_Type (E) | |
886 | and then not Has_Machine_Radix_Clause (E) | |
887 | then | |
888 | Set_Machine_Radix_10 (E, Machine_Radix_10 (P)); | |
889 | end if; | |
890 | ||
891 | -- Object_Size (also Size which also sets Object_Size) | |
892 | ||
893 | when Aspect_Object_Size | Aspect_Size => | |
894 | if not Has_Size_Clause (E) | |
895 | and then | |
896 | No (Get_Attribute_Definition_Clause | |
897 | (E, Attribute_Object_Size)) | |
898 | then | |
899 | Set_Esize (E, Esize (P)); | |
900 | end if; | |
901 | ||
902 | -- Pack | |
903 | ||
904 | when Aspect_Pack => | |
905 | if not Is_Packed (E) then | |
906 | Set_Is_Packed (Base_Type (E)); | |
907 | ||
908 | if Is_Bit_Packed_Array (P) then | |
909 | Set_Is_Bit_Packed_Array (Base_Type (E)); | |
a88a5773 | 910 | Set_Packed_Array_Impl_Type |
911 | (E, Packed_Array_Impl_Type (P)); | |
37c6e44c | 912 | end if; |
913 | end if; | |
914 | ||
915 | -- Scalar_Storage_Order | |
916 | ||
917 | when Aspect_Scalar_Storage_Order => | |
918 | if (Is_Record_Type (E) or else Is_Array_Type (E)) | |
919 | and then No (Get_Attribute_Definition_Clause | |
e163cac8 | 920 | (E, Attribute_Scalar_Storage_Order)) |
37c6e44c | 921 | and then Reverse_Storage_Order (P) |
922 | then | |
923 | Set_Reverse_Storage_Order (Base_Type (E)); | |
b64082f2 | 924 | |
925 | -- Clear default SSO indications, since the aspect | |
926 | -- overrides the default. | |
927 | ||
928 | Set_SSO_Set_Low_By_Default (Base_Type (E), False); | |
929 | Set_SSO_Set_High_By_Default (Base_Type (E), False); | |
37c6e44c | 930 | end if; |
931 | ||
932 | -- Small | |
933 | ||
934 | when Aspect_Small => | |
935 | if Is_Fixed_Point_Type (E) | |
936 | and then not Has_Small_Clause (E) | |
937 | then | |
938 | Set_Small_Value (E, Small_Value (P)); | |
939 | end if; | |
940 | ||
941 | -- Storage_Size | |
942 | ||
943 | when Aspect_Storage_Size => | |
944 | if (Is_Access_Type (E) or else Is_Task_Type (E)) | |
945 | and then not Has_Storage_Size_Clause (E) | |
946 | then | |
947 | Set_Storage_Size_Variable | |
948 | (Base_Type (E), Storage_Size_Variable (P)); | |
949 | end if; | |
950 | ||
951 | -- Value_Size | |
952 | ||
953 | when Aspect_Value_Size => | |
954 | ||
955 | -- Value_Size is never inherited, it is either set by | |
956 | -- default, or it is explicitly set for the derived | |
957 | -- type. So nothing to do here. | |
958 | ||
959 | null; | |
960 | ||
961 | -- Volatile | |
962 | ||
963 | when Aspect_Volatile => | |
964 | if Is_Volatile (P) then | |
965 | Set_Is_Volatile (E); | |
966 | end if; | |
967 | ||
968 | -- Volatile_Components | |
969 | ||
970 | when Aspect_Volatile_Components => | |
971 | if Has_Volatile_Components (P) then | |
972 | Set_Has_Volatile_Components (Base_Type (E)); | |
973 | end if; | |
974 | ||
975 | -- That should be all the Rep Aspects | |
976 | ||
977 | when others => | |
978 | pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect); | |
979 | null; | |
980 | ||
981 | end case; | |
982 | end if; | |
983 | end if; | |
984 | ||
985 | N := Next_Rep_Item (N); | |
986 | end loop; | |
987 | end Inherit_Delayed_Rep_Aspects; | |
988 | ||
06ef5f86 | 989 | ------------------------------------- |
990 | -- Make_Pragma_From_Boolean_Aspect -- | |
991 | ------------------------------------- | |
992 | ||
993 | procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is | |
994 | Ident : constant Node_Id := Identifier (ASN); | |
995 | A_Name : constant Name_Id := Chars (Ident); | |
996 | A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name); | |
997 | Ent : constant Entity_Id := Entity (ASN); | |
998 | Expr : constant Node_Id := Expression (ASN); | |
999 | Loc : constant Source_Ptr := Sloc (ASN); | |
1000 | ||
1001 | Prag : Node_Id; | |
1002 | ||
1003 | procedure Check_False_Aspect_For_Derived_Type; | |
1004 | -- This procedure checks for the case of a false aspect for a derived | |
1005 | -- type, which improperly tries to cancel an aspect inherited from | |
1006 | -- the parent. | |
1007 | ||
1008 | ----------------------------------------- | |
1009 | -- Check_False_Aspect_For_Derived_Type -- | |
1010 | ----------------------------------------- | |
1011 | ||
1012 | procedure Check_False_Aspect_For_Derived_Type is | |
1013 | Par : Node_Id; | |
1014 | ||
1015 | begin | |
1016 | -- We are only checking derived types | |
1017 | ||
1018 | if not Is_Derived_Type (E) then | |
1019 | return; | |
1020 | end if; | |
1021 | ||
1022 | Par := Nearest_Ancestor (E); | |
1023 | ||
1024 | case A_Id is | |
1025 | when Aspect_Atomic | Aspect_Shared => | |
1026 | if not Is_Atomic (Par) then | |
1027 | return; | |
1028 | end if; | |
1029 | ||
1030 | when Aspect_Atomic_Components => | |
1031 | if not Has_Atomic_Components (Par) then | |
1032 | return; | |
1033 | end if; | |
1034 | ||
1035 | when Aspect_Discard_Names => | |
1036 | if not Discard_Names (Par) then | |
1037 | return; | |
1038 | end if; | |
1039 | ||
1040 | when Aspect_Pack => | |
1041 | if not Is_Packed (Par) then | |
1042 | return; | |
1043 | end if; | |
1044 | ||
1045 | when Aspect_Unchecked_Union => | |
1046 | if not Is_Unchecked_Union (Par) then | |
1047 | return; | |
1048 | end if; | |
1049 | ||
1050 | when Aspect_Volatile => | |
1051 | if not Is_Volatile (Par) then | |
1052 | return; | |
1053 | end if; | |
1054 | ||
1055 | when Aspect_Volatile_Components => | |
1056 | if not Has_Volatile_Components (Par) then | |
1057 | return; | |
1058 | end if; | |
1059 | ||
1060 | when others => | |
1061 | return; | |
1062 | end case; | |
1063 | ||
1064 | -- Fall through means we are canceling an inherited aspect | |
1065 | ||
1066 | Error_Msg_Name_1 := A_Name; | |
37c6e44c | 1067 | Error_Msg_NE |
1068 | ("derived type& inherits aspect%, cannot cancel", Expr, E); | |
06ef5f86 | 1069 | |
1070 | end Check_False_Aspect_For_Derived_Type; | |
1071 | ||
1072 | -- Start of processing for Make_Pragma_From_Boolean_Aspect | |
1073 | ||
1074 | begin | |
37c6e44c | 1075 | -- Note that we know Expr is present, because for a missing Expr |
1076 | -- argument, we knew it was True and did not need to delay the | |
1077 | -- evaluation to the freeze point. | |
1078 | ||
06ef5f86 | 1079 | if Is_False (Static_Boolean (Expr)) then |
1080 | Check_False_Aspect_For_Derived_Type; | |
1081 | ||
1082 | else | |
1083 | Prag := | |
1084 | Make_Pragma (Loc, | |
1085 | Pragma_Argument_Associations => New_List ( | |
57cd943b | 1086 | Make_Pragma_Argument_Association (Sloc (Ident), |
1087 | Expression => New_Occurrence_Of (Ent, Sloc (Ident)))), | |
1088 | ||
06ef5f86 | 1089 | Pragma_Identifier => |
1090 | Make_Identifier (Sloc (Ident), Chars (Ident))); | |
1091 | ||
1092 | Set_From_Aspect_Specification (Prag, True); | |
1093 | Set_Corresponding_Aspect (Prag, ASN); | |
1094 | Set_Aspect_Rep_Item (ASN, Prag); | |
1095 | Set_Is_Delayed_Aspect (Prag); | |
1096 | Set_Parent (Prag, ASN); | |
1097 | end if; | |
06ef5f86 | 1098 | end Make_Pragma_From_Boolean_Aspect; |
1099 | ||
1100 | -- Start of processing for Analyze_Aspects_At_Freeze_Point | |
1101 | ||
1102 | begin | |
29a9d4be | 1103 | -- Must be visible in current scope |
06ef5f86 | 1104 | |
ace3389d | 1105 | if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then |
06ef5f86 | 1106 | return; |
1107 | end if; | |
1108 | ||
1109 | -- Look for aspect specification entries for this entity | |
1110 | ||
1111 | ASN := First_Rep_Item (E); | |
06ef5f86 | 1112 | while Present (ASN) loop |
37c6e44c | 1113 | if Nkind (ASN) = N_Aspect_Specification then |
1114 | exit when Entity (ASN) /= E; | |
06ef5f86 | 1115 | |
37c6e44c | 1116 | if Is_Delayed_Aspect (ASN) then |
1117 | A_Id := Get_Aspect_Id (ASN); | |
1118 | ||
1119 | case A_Id is | |
e4c87fa5 | 1120 | |
37c6e44c | 1121 | -- For aspects whose expression is an optional Boolean, make |
7d6fb253 | 1122 | -- the corresponding pragma at the freeze point. |
06ef5f86 | 1123 | |
7d6fb253 | 1124 | when Boolean_Aspects | |
1125 | Library_Unit_Aspects => | |
1126 | Make_Pragma_From_Boolean_Aspect (ASN); | |
06ef5f86 | 1127 | |
37c6e44c | 1128 | -- Special handling for aspects that don't correspond to |
1129 | -- pragmas/attributes. | |
06ef5f86 | 1130 | |
7d6fb253 | 1131 | when Aspect_Default_Value | |
1132 | Aspect_Default_Component_Value => | |
1133 | Analyze_Aspect_Default_Value (ASN); | |
06ef5f86 | 1134 | |
37c6e44c | 1135 | -- Ditto for iterator aspects, because the corresponding |
1136 | -- attributes may not have been analyzed yet. | |
af9fed8f | 1137 | |
7d6fb253 | 1138 | when Aspect_Constant_Indexing | |
1139 | Aspect_Variable_Indexing | | |
1140 | Aspect_Default_Iterator | | |
1141 | Aspect_Iterator_Element => | |
1142 | Analyze (Expression (ASN)); | |
af9fed8f | 1143 | |
7d6fb253 | 1144 | if Etype (Expression (ASN)) = Any_Type then |
1145 | Error_Msg_NE | |
1146 | ("\aspect must be fully defined before & is frozen", | |
1147 | ASN, E); | |
1148 | end if; | |
b3f8228a | 1149 | |
7d6fb253 | 1150 | when Aspect_Iterable => |
1151 | Validate_Iterable_Aspect (E, ASN); | |
1152 | ||
1153 | when others => | |
1154 | null; | |
37c6e44c | 1155 | end case; |
06ef5f86 | 1156 | |
37c6e44c | 1157 | Ritem := Aspect_Rep_Item (ASN); |
06ef5f86 | 1158 | |
37c6e44c | 1159 | if Present (Ritem) then |
1160 | Analyze (Ritem); | |
1161 | end if; | |
06ef5f86 | 1162 | end if; |
1163 | end if; | |
1164 | ||
1165 | Next_Rep_Item (ASN); | |
1166 | end loop; | |
37c6e44c | 1167 | |
1168 | -- This is where we inherit delayed rep aspects from our parent. Note | |
1169 | -- that if we fell out of the above loop with ASN non-empty, it means | |
1170 | -- we hit an aspect for an entity other than E, and it must be the | |
1171 | -- type from which we were derived. | |
1172 | ||
1173 | if May_Inherit_Delayed_Rep_Aspects (E) then | |
1174 | Inherit_Delayed_Rep_Aspects (ASN); | |
1175 | end if; | |
06ef5f86 | 1176 | end Analyze_Aspects_At_Freeze_Point; |
1177 | ||
ae888dbd | 1178 | ----------------------------------- |
1179 | -- Analyze_Aspect_Specifications -- | |
1180 | ----------------------------------- | |
1181 | ||
21ea3a4f | 1182 | procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is |
e2bf777d | 1183 | procedure Decorate (Asp : Node_Id; Prag : Node_Id); |
c4369687 | 1184 | -- Establish linkages between an aspect and its corresponding |
e2bf777d | 1185 | -- pragma. |
5ddd846b | 1186 | |
50e44732 | 1187 | procedure Insert_After_SPARK_Mode |
1188 | (Prag : Node_Id; | |
1189 | Ins_Nod : Node_Id; | |
1190 | Decls : List_Id); | |
3dbe7a69 | 1191 | -- Subsidiary to the analysis of aspects Abstract_State, Ghost, |
1192 | -- Initializes, Initial_Condition and Refined_State. Insert node Prag | |
1193 | -- before node Ins_Nod. If Ins_Nod is for pragma SPARK_Mode, then skip | |
1194 | -- SPARK_Mode. Decls is the associated declarative list where Prag is to | |
1195 | -- reside. | |
e2bf777d | 1196 | |
1197 | procedure Insert_Pragma (Prag : Node_Id); | |
1198 | -- Subsidiary to the analysis of aspects Attach_Handler, Contract_Cases, | |
1199 | -- Depends, Global, Post, Pre, Refined_Depends and Refined_Global. | |
1200 | -- Insert pragma Prag such that it mimics the placement of a source | |
1201 | -- pragma of the same kind. | |
1202 | -- | |
1203 | -- procedure Proc (Formal : ...) with Global => ...; | |
1204 | -- | |
1205 | -- procedure Proc (Formal : ...); | |
1206 | -- pragma Global (...); | |
1207 | ||
1208 | -------------- | |
1209 | -- Decorate -- | |
1210 | -------------- | |
1211 | ||
1212 | procedure Decorate (Asp : Node_Id; Prag : Node_Id) is | |
5ddd846b | 1213 | begin |
5cc6f0cf | 1214 | Set_Aspect_Rep_Item (Asp, Prag); |
5ddd846b | 1215 | Set_Corresponding_Aspect (Prag, Asp); |
1216 | Set_From_Aspect_Specification (Prag); | |
5ddd846b | 1217 | Set_Parent (Prag, Asp); |
e2bf777d | 1218 | end Decorate; |
f0813d71 | 1219 | |
50e44732 | 1220 | ----------------------------- |
1221 | -- Insert_After_SPARK_Mode -- | |
1222 | ----------------------------- | |
1223 | ||
1224 | procedure Insert_After_SPARK_Mode | |
1225 | (Prag : Node_Id; | |
1226 | Ins_Nod : Node_Id; | |
1227 | Decls : List_Id) | |
1228 | is | |
1229 | Decl : Node_Id := Ins_Nod; | |
1230 | ||
1231 | begin | |
1232 | -- Skip SPARK_Mode | |
1233 | ||
1234 | if Present (Decl) | |
1235 | and then Nkind (Decl) = N_Pragma | |
1236 | and then Pragma_Name (Decl) = Name_SPARK_Mode | |
1237 | then | |
1238 | Decl := Next (Decl); | |
1239 | end if; | |
1240 | ||
1241 | if Present (Decl) then | |
1242 | Insert_Before (Decl, Prag); | |
1243 | ||
1244 | -- Aitem acts as the last declaration | |
1245 | ||
1246 | else | |
1247 | Append_To (Decls, Prag); | |
1248 | end if; | |
1249 | end Insert_After_SPARK_Mode; | |
1250 | ||
e2bf777d | 1251 | ------------------- |
1252 | -- Insert_Pragma -- | |
1253 | ------------------- | |
c1006d6d | 1254 | |
e2bf777d | 1255 | procedure Insert_Pragma (Prag : Node_Id) is |
1256 | Aux : Node_Id; | |
1257 | Decl : Node_Id; | |
c1006d6d | 1258 | |
1259 | begin | |
1260 | -- When the context is a library unit, the pragma is added to the | |
1261 | -- Pragmas_After list. | |
1262 | ||
1263 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
1264 | Aux := Aux_Decls_Node (Parent (N)); | |
1265 | ||
1266 | if No (Pragmas_After (Aux)) then | |
1267 | Set_Pragmas_After (Aux, New_List); | |
1268 | end if; | |
1269 | ||
1270 | Prepend (Prag, Pragmas_After (Aux)); | |
1271 | ||
1272 | -- Pragmas associated with subprogram bodies are inserted in the | |
1273 | -- declarative part. | |
1274 | ||
1275 | elsif Nkind (N) = N_Subprogram_Body then | |
e2bf777d | 1276 | if Present (Declarations (N)) then |
d324c418 | 1277 | |
e2bf777d | 1278 | -- Skip other internally generated pragmas from aspects to find |
1279 | -- the proper insertion point. As a result the order of pragmas | |
1280 | -- is the same as the order of aspects. | |
d324c418 | 1281 | |
607bc8f5 | 1282 | -- As precondition pragmas generated from conjuncts in the |
1283 | -- precondition aspect are presented in reverse order to | |
1284 | -- Insert_Pragma, insert them in the correct order here by not | |
1285 | -- skipping previously inserted precondition pragmas when the | |
1286 | -- current pragma is a precondition. | |
1287 | ||
e2bf777d | 1288 | Decl := First (Declarations (N)); |
1289 | while Present (Decl) loop | |
1290 | if Nkind (Decl) = N_Pragma | |
1291 | and then From_Aspect_Specification (Decl) | |
607bc8f5 | 1292 | and then not (Get_Pragma_Id (Decl) = Pragma_Precondition |
1293 | and then | |
1294 | Get_Pragma_Id (Prag) = Pragma_Precondition) | |
e2bf777d | 1295 | then |
1296 | Next (Decl); | |
d324c418 | 1297 | else |
e2bf777d | 1298 | exit; |
d324c418 | 1299 | end if; |
e2bf777d | 1300 | end loop; |
1301 | ||
1302 | if Present (Decl) then | |
1303 | Insert_Before (Decl, Prag); | |
1304 | else | |
1305 | Append (Prag, Declarations (N)); | |
1306 | end if; | |
1307 | else | |
1308 | Set_Declarations (N, New_List (Prag)); | |
d324c418 | 1309 | end if; |
c1006d6d | 1310 | |
1311 | -- Default | |
1312 | ||
1313 | else | |
1314 | Insert_After (N, Prag); | |
c1006d6d | 1315 | end if; |
e2bf777d | 1316 | end Insert_Pragma; |
c1006d6d | 1317 | |
1318 | -- Local variables | |
1319 | ||
ae888dbd | 1320 | Aspect : Node_Id; |
d74fc39a | 1321 | Aitem : Node_Id; |
ae888dbd | 1322 | Ent : Node_Id; |
ae888dbd | 1323 | |
21ea3a4f | 1324 | L : constant List_Id := Aspect_Specifications (N); |
1325 | ||
ae888dbd | 1326 | Ins_Node : Node_Id := N; |
89f1e35c | 1327 | -- Insert pragmas/attribute definition clause after this node when no |
1328 | -- delayed analysis is required. | |
d74fc39a | 1329 | |
f0813d71 | 1330 | -- Start of processing for Analyze_Aspect_Specifications |
1331 | ||
d74fc39a | 1332 | -- The general processing involves building an attribute definition |
89f1e35c | 1333 | -- clause or a pragma node that corresponds to the aspect. Then in order |
1334 | -- to delay the evaluation of this aspect to the freeze point, we attach | |
1335 | -- the corresponding pragma/attribute definition clause to the aspect | |
1336 | -- specification node, which is then placed in the Rep Item chain. In | |
1337 | -- this case we mark the entity by setting the flag Has_Delayed_Aspects | |
1338 | -- and we evaluate the rep item at the freeze point. When the aspect | |
1339 | -- doesn't have a corresponding pragma/attribute definition clause, then | |
1340 | -- its analysis is simply delayed at the freeze point. | |
1341 | ||
1342 | -- Some special cases don't require delay analysis, thus the aspect is | |
1343 | -- analyzed right now. | |
1344 | ||
51ea9c94 | 1345 | -- Note that there is a special handling for Pre, Post, Test_Case, |
e66f4e2a | 1346 | -- Contract_Cases aspects. In these cases, we do not have to worry |
51ea9c94 | 1347 | -- about delay issues, since the pragmas themselves deal with delay |
1348 | -- of visibility for the expression analysis. Thus, we just insert | |
1349 | -- the pragma after the node N. | |
ae888dbd | 1350 | |
1351 | begin | |
21ea3a4f | 1352 | pragma Assert (Present (L)); |
1353 | ||
6fb3c314 | 1354 | -- Loop through aspects |
f93e7257 | 1355 | |
ae888dbd | 1356 | Aspect := First (L); |
21ea3a4f | 1357 | Aspect_Loop : while Present (Aspect) loop |
0fd13d32 | 1358 | Analyze_One_Aspect : declare |
94153a42 | 1359 | Expr : constant Node_Id := Expression (Aspect); |
89f1e35c | 1360 | Id : constant Node_Id := Identifier (Aspect); |
1361 | Loc : constant Source_Ptr := Sloc (Aspect); | |
94153a42 | 1362 | Nam : constant Name_Id := Chars (Id); |
1363 | A_Id : constant Aspect_Id := Get_Aspect_Id (Nam); | |
ae888dbd | 1364 | Anod : Node_Id; |
1365 | ||
37c6e44c | 1366 | Delay_Required : Boolean; |
89f1e35c | 1367 | -- Set False if delay is not required |
1368 | ||
c0793fff | 1369 | Eloc : Source_Ptr := No_Location; |
1370 | -- Source location of expression, modified when we split PPC's. It | |
1371 | -- is set below when Expr is present. | |
39e1f22f | 1372 | |
89f1e35c | 1373 | procedure Analyze_Aspect_External_Or_Link_Name; |
0fd13d32 | 1374 | -- Perform analysis of the External_Name or Link_Name aspects |
21ea3a4f | 1375 | |
89f1e35c | 1376 | procedure Analyze_Aspect_Implicit_Dereference; |
9ab32fe9 | 1377 | -- Perform analysis of the Implicit_Dereference aspects |
0fd13d32 | 1378 | |
1379 | procedure Make_Aitem_Pragma | |
1380 | (Pragma_Argument_Associations : List_Id; | |
1381 | Pragma_Name : Name_Id); | |
1382 | -- This is a wrapper for Make_Pragma used for converting aspects | |
1383 | -- to pragmas. It takes care of Sloc (set from Loc) and building | |
1384 | -- the pragma identifier from the given name. In addition the | |
1385 | -- flags Class_Present and Split_PPC are set from the aspect | |
1386 | -- node, as well as Is_Ignored. This routine also sets the | |
1387 | -- From_Aspect_Specification in the resulting pragma node to | |
1388 | -- True, and sets Corresponding_Aspect to point to the aspect. | |
1389 | -- The resulting pragma is assigned to Aitem. | |
21ea3a4f | 1390 | |
89f1e35c | 1391 | ------------------------------------------ |
1392 | -- Analyze_Aspect_External_Or_Link_Name -- | |
1393 | ------------------------------------------ | |
1394 | ||
1395 | procedure Analyze_Aspect_External_Or_Link_Name is | |
21ea3a4f | 1396 | begin |
89f1e35c | 1397 | -- Verify that there is an Import/Export aspect defined for the |
1398 | -- entity. The processing of that aspect in turn checks that | |
1399 | -- there is a Convention aspect declared. The pragma is | |
1400 | -- constructed when processing the Convention aspect. | |
21ea3a4f | 1401 | |
89f1e35c | 1402 | declare |
1403 | A : Node_Id; | |
21ea3a4f | 1404 | |
89f1e35c | 1405 | begin |
1406 | A := First (L); | |
89f1e35c | 1407 | while Present (A) loop |
18393965 | 1408 | exit when Nam_In (Chars (Identifier (A)), Name_Export, |
1409 | Name_Import); | |
89f1e35c | 1410 | Next (A); |
1411 | end loop; | |
21ea3a4f | 1412 | |
89f1e35c | 1413 | if No (A) then |
1414 | Error_Msg_N | |
51ea9c94 | 1415 | ("missing Import/Export for Link/External name", |
8a1e3cde | 1416 | Aspect); |
89f1e35c | 1417 | end if; |
1418 | end; | |
1419 | end Analyze_Aspect_External_Or_Link_Name; | |
21ea3a4f | 1420 | |
89f1e35c | 1421 | ----------------------------------------- |
1422 | -- Analyze_Aspect_Implicit_Dereference -- | |
1423 | ----------------------------------------- | |
21ea3a4f | 1424 | |
89f1e35c | 1425 | procedure Analyze_Aspect_Implicit_Dereference is |
1426 | begin | |
b9e61b2a | 1427 | if not Is_Type (E) or else not Has_Discriminants (E) then |
89f1e35c | 1428 | Error_Msg_N |
51ea9c94 | 1429 | ("aspect must apply to a type with discriminants", N); |
21ea3a4f | 1430 | |
89f1e35c | 1431 | else |
1432 | declare | |
1433 | Disc : Entity_Id; | |
21ea3a4f | 1434 | |
89f1e35c | 1435 | begin |
1436 | Disc := First_Discriminant (E); | |
89f1e35c | 1437 | while Present (Disc) loop |
1438 | if Chars (Expr) = Chars (Disc) | |
1439 | and then Ekind (Etype (Disc)) = | |
1440 | E_Anonymous_Access_Type | |
1441 | then | |
1442 | Set_Has_Implicit_Dereference (E); | |
1443 | Set_Has_Implicit_Dereference (Disc); | |
1444 | return; | |
1445 | end if; | |
21ea3a4f | 1446 | |
89f1e35c | 1447 | Next_Discriminant (Disc); |
1448 | end loop; | |
21ea3a4f | 1449 | |
89f1e35c | 1450 | -- Error if no proper access discriminant. |
21ea3a4f | 1451 | |
89f1e35c | 1452 | Error_Msg_NE |
1453 | ("not an access discriminant of&", Expr, E); | |
1454 | end; | |
1455 | end if; | |
1456 | end Analyze_Aspect_Implicit_Dereference; | |
21ea3a4f | 1457 | |
0fd13d32 | 1458 | ----------------------- |
1459 | -- Make_Aitem_Pragma -- | |
1460 | ----------------------- | |
1461 | ||
1462 | procedure Make_Aitem_Pragma | |
1463 | (Pragma_Argument_Associations : List_Id; | |
1464 | Pragma_Name : Name_Id) | |
1465 | is | |
b855559d | 1466 | Args : List_Id := Pragma_Argument_Associations; |
1467 | ||
0fd13d32 | 1468 | begin |
1469 | -- We should never get here if aspect was disabled | |
1470 | ||
1471 | pragma Assert (not Is_Disabled (Aspect)); | |
1472 | ||
056dc987 | 1473 | -- Certain aspects allow for an optional name or expression. Do |
1474 | -- not generate a pragma with empty argument association list. | |
b855559d | 1475 | |
1476 | if No (Args) or else No (Expression (First (Args))) then | |
1477 | Args := No_List; | |
1478 | end if; | |
1479 | ||
0fd13d32 | 1480 | -- Build the pragma |
1481 | ||
1482 | Aitem := | |
1483 | Make_Pragma (Loc, | |
b855559d | 1484 | Pragma_Argument_Associations => Args, |
0fd13d32 | 1485 | Pragma_Identifier => |
1486 | Make_Identifier (Sloc (Id), Pragma_Name), | |
9ab32fe9 | 1487 | Class_Present => Class_Present (Aspect), |
1488 | Split_PPC => Split_PPC (Aspect)); | |
0fd13d32 | 1489 | |
1490 | -- Set additional semantic fields | |
1491 | ||
1492 | if Is_Ignored (Aspect) then | |
1493 | Set_Is_Ignored (Aitem); | |
57d8d1f3 | 1494 | elsif Is_Checked (Aspect) then |
a5109493 | 1495 | Set_Is_Checked (Aitem); |
0fd13d32 | 1496 | end if; |
1497 | ||
1498 | Set_Corresponding_Aspect (Aitem, Aspect); | |
1499 | Set_From_Aspect_Specification (Aitem, True); | |
1500 | end Make_Aitem_Pragma; | |
1501 | ||
1502 | -- Start of processing for Analyze_One_Aspect | |
1503 | ||
ae888dbd | 1504 | begin |
2d1acfa7 | 1505 | -- Skip aspect if already analyzed, to avoid looping in some cases |
fb7f2fc4 | 1506 | |
1507 | if Analyzed (Aspect) then | |
1508 | goto Continue; | |
1509 | end if; | |
1510 | ||
ef957022 | 1511 | -- Skip looking at aspect if it is totally disabled. Just mark it |
1512 | -- as such for later reference in the tree. This also sets the | |
1513 | -- Is_Ignored and Is_Checked flags appropriately. | |
51ea9c94 | 1514 | |
1515 | Check_Applicable_Policy (Aspect); | |
1516 | ||
1517 | if Is_Disabled (Aspect) then | |
1518 | goto Continue; | |
1519 | end if; | |
1520 | ||
c0793fff | 1521 | -- Set the source location of expression, used in the case of |
1522 | -- a failed precondition/postcondition or invariant. Note that | |
1523 | -- the source location of the expression is not usually the best | |
1524 | -- choice here. For example, it gets located on the last AND | |
1525 | -- keyword in a chain of boolean expressiond AND'ed together. | |
1526 | -- It is best to put the message on the first character of the | |
1527 | -- assertion, which is the effect of the First_Node call here. | |
1528 | ||
1529 | if Present (Expr) then | |
1530 | Eloc := Sloc (First_Node (Expr)); | |
1531 | end if; | |
1532 | ||
d7ed83a2 | 1533 | -- Check restriction No_Implementation_Aspect_Specifications |
1534 | ||
c171e1be | 1535 | if Implementation_Defined_Aspect (A_Id) then |
d7ed83a2 | 1536 | Check_Restriction |
1537 | (No_Implementation_Aspect_Specifications, Aspect); | |
1538 | end if; | |
1539 | ||
1540 | -- Check restriction No_Specification_Of_Aspect | |
1541 | ||
1542 | Check_Restriction_No_Specification_Of_Aspect (Aspect); | |
1543 | ||
f67ed4f5 | 1544 | -- Mark aspect analyzed (actual analysis is delayed till later) |
d7ed83a2 | 1545 | |
fb7f2fc4 | 1546 | Set_Analyzed (Aspect); |
d74fc39a | 1547 | Set_Entity (Aspect, E); |
1548 | Ent := New_Occurrence_Of (E, Sloc (Id)); | |
1549 | ||
1e3c4ae6 | 1550 | -- Check for duplicate aspect. Note that the Comes_From_Source |
1551 | -- test allows duplicate Pre/Post's that we generate internally | |
1552 | -- to escape being flagged here. | |
ae888dbd | 1553 | |
6c545057 | 1554 | if No_Duplicates_Allowed (A_Id) then |
1555 | Anod := First (L); | |
1556 | while Anod /= Aspect loop | |
c171e1be | 1557 | if Comes_From_Source (Aspect) |
1558 | and then Same_Aspect (A_Id, Get_Aspect_Id (Anod)) | |
6c545057 | 1559 | then |
1560 | Error_Msg_Name_1 := Nam; | |
1561 | Error_Msg_Sloc := Sloc (Anod); | |
39e1f22f | 1562 | |
6c545057 | 1563 | -- Case of same aspect specified twice |
39e1f22f | 1564 | |
6c545057 | 1565 | if Class_Present (Anod) = Class_Present (Aspect) then |
1566 | if not Class_Present (Anod) then | |
1567 | Error_Msg_NE | |
1568 | ("aspect% for & previously given#", | |
1569 | Id, E); | |
1570 | else | |
1571 | Error_Msg_NE | |
1572 | ("aspect `%''Class` for & previously given#", | |
1573 | Id, E); | |
1574 | end if; | |
39e1f22f | 1575 | end if; |
6c545057 | 1576 | end if; |
ae888dbd | 1577 | |
6c545057 | 1578 | Next (Anod); |
1579 | end loop; | |
1580 | end if; | |
ae888dbd | 1581 | |
4db325e6 | 1582 | -- Check some general restrictions on language defined aspects |
1583 | ||
c171e1be | 1584 | if not Implementation_Defined_Aspect (A_Id) then |
4db325e6 | 1585 | Error_Msg_Name_1 := Nam; |
1586 | ||
1587 | -- Not allowed for renaming declarations | |
1588 | ||
1589 | if Nkind (N) in N_Renaming_Declaration then | |
1590 | Error_Msg_N | |
1591 | ("aspect % not allowed for renaming declaration", | |
1592 | Aspect); | |
1593 | end if; | |
1594 | ||
1595 | -- Not allowed for formal type declarations | |
1596 | ||
1597 | if Nkind (N) = N_Formal_Type_Declaration then | |
1598 | Error_Msg_N | |
1599 | ("aspect % not allowed for formal type declaration", | |
1600 | Aspect); | |
1601 | end if; | |
1602 | end if; | |
1603 | ||
7d20685d | 1604 | -- Copy expression for later processing by the procedures |
1605 | -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations] | |
1606 | ||
1607 | Set_Entity (Id, New_Copy_Tree (Expr)); | |
1608 | ||
37c6e44c | 1609 | -- Set Delay_Required as appropriate to aspect |
1610 | ||
1611 | case Aspect_Delay (A_Id) is | |
1612 | when Always_Delay => | |
1613 | Delay_Required := True; | |
1614 | ||
1615 | when Never_Delay => | |
1616 | Delay_Required := False; | |
1617 | ||
1618 | when Rep_Aspect => | |
1619 | ||
1620 | -- If expression has the form of an integer literal, then | |
1621 | -- do not delay, since we know the value cannot change. | |
1622 | -- This optimization catches most rep clause cases. | |
1623 | ||
1624 | if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal) | |
1625 | or else (A_Id in Boolean_Aspects and then No (Expr)) | |
1626 | then | |
1627 | Delay_Required := False; | |
1628 | else | |
1629 | Delay_Required := True; | |
1630 | Set_Has_Delayed_Rep_Aspects (E); | |
1631 | end if; | |
1632 | end case; | |
1633 | ||
ae888dbd | 1634 | -- Processing based on specific aspect |
1635 | ||
d74fc39a | 1636 | case A_Id is |
ae888dbd | 1637 | |
1638 | -- No_Aspect should be impossible | |
1639 | ||
1640 | when No_Aspect => | |
1641 | raise Program_Error; | |
1642 | ||
89f1e35c | 1643 | -- Case 1: Aspects corresponding to attribute definition |
1644 | -- clauses. | |
ae888dbd | 1645 | |
b7b74740 | 1646 | when Aspect_Address | |
1647 | Aspect_Alignment | | |
1648 | Aspect_Bit_Order | | |
1649 | Aspect_Component_Size | | |
89f1e35c | 1650 | Aspect_Constant_Indexing | |
89f1e35c | 1651 | Aspect_Default_Iterator | |
1652 | Aspect_Dispatching_Domain | | |
b7b74740 | 1653 | Aspect_External_Tag | |
1654 | Aspect_Input | | |
b3f8228a | 1655 | Aspect_Iterable | |
89f1e35c | 1656 | Aspect_Iterator_Element | |
b7b74740 | 1657 | Aspect_Machine_Radix | |
1658 | Aspect_Object_Size | | |
1659 | Aspect_Output | | |
1660 | Aspect_Read | | |
1661 | Aspect_Scalar_Storage_Order | | |
1662 | Aspect_Size | | |
1663 | Aspect_Small | | |
1664 | Aspect_Simple_Storage_Pool | | |
1665 | Aspect_Storage_Pool | | |
b7b74740 | 1666 | Aspect_Stream_Size | |
1667 | Aspect_Value_Size | | |
89f1e35c | 1668 | Aspect_Variable_Indexing | |
b7b74740 | 1669 | Aspect_Write => |
d74fc39a | 1670 | |
89f1e35c | 1671 | -- Indexing aspects apply only to tagged type |
1672 | ||
1673 | if (A_Id = Aspect_Constant_Indexing | |
37c6e44c | 1674 | or else |
1675 | A_Id = Aspect_Variable_Indexing) | |
89f1e35c | 1676 | and then not (Is_Type (E) |
1677 | and then Is_Tagged_Type (E)) | |
1678 | then | |
05987af3 | 1679 | Error_Msg_N |
1680 | ("indexing aspect can only apply to a tagged type", | |
3f4c9ffc | 1681 | Aspect); |
89f1e35c | 1682 | goto Continue; |
1683 | end if; | |
1684 | ||
39616053 | 1685 | -- For the case of aspect Address, we don't consider that we |
588e7f97 | 1686 | -- know the entity is never set in the source, since it is |
1687 | -- is likely aliasing is occurring. | |
1688 | ||
1689 | -- Note: one might think that the analysis of the resulting | |
1690 | -- attribute definition clause would take care of that, but | |
1691 | -- that's not the case since it won't be from source. | |
1692 | ||
1693 | if A_Id = Aspect_Address then | |
1694 | Set_Never_Set_In_Source (E, False); | |
1695 | end if; | |
1696 | ||
5ac76cee | 1697 | -- Correctness of the profile of a stream operation is |
1698 | -- verified at the freeze point, but we must detect the | |
1699 | -- illegal specification of this aspect for a subtype now, | |
1700 | -- to prevent malformed rep_item chains. | |
1701 | ||
cda40848 | 1702 | if (A_Id = Aspect_Input or else |
1703 | A_Id = Aspect_Output or else | |
1704 | A_Id = Aspect_Read or else | |
1705 | A_Id = Aspect_Write) | |
5ac76cee | 1706 | and not Is_First_Subtype (E) |
1707 | then | |
1708 | Error_Msg_N | |
1709 | ("local name must be a first subtype", Aspect); | |
1710 | goto Continue; | |
1711 | end if; | |
1712 | ||
d74fc39a | 1713 | -- Construct the attribute definition clause |
1714 | ||
1715 | Aitem := | |
94153a42 | 1716 | Make_Attribute_Definition_Clause (Loc, |
d74fc39a | 1717 | Name => Ent, |
ae888dbd | 1718 | Chars => Chars (Id), |
1719 | Expression => Relocate_Node (Expr)); | |
1720 | ||
af9a0cc3 | 1721 | -- If the address is specified, then we treat the entity as |
41f06abf | 1722 | -- referenced, to avoid spurious warnings. This is analogous |
1723 | -- to what is done with an attribute definition clause, but | |
1724 | -- here we don't want to generate a reference because this | |
1725 | -- is the point of definition of the entity. | |
1726 | ||
1727 | if A_Id = Aspect_Address then | |
1728 | Set_Referenced (E); | |
1729 | end if; | |
1730 | ||
51ea9c94 | 1731 | -- Case 2: Aspects corresponding to pragmas |
d74fc39a | 1732 | |
89f1e35c | 1733 | -- Case 2a: Aspects corresponding to pragmas with two |
1734 | -- arguments, where the first argument is a local name | |
1735 | -- referring to the entity, and the second argument is the | |
1736 | -- aspect definition expression. | |
ae888dbd | 1737 | |
04ae062f | 1738 | -- Linker_Section/Suppress/Unsuppress |
0fd13d32 | 1739 | |
04ae062f | 1740 | when Aspect_Linker_Section | |
1741 | Aspect_Suppress | | |
1742 | Aspect_Unsuppress => | |
ae888dbd | 1743 | |
0fd13d32 | 1744 | Make_Aitem_Pragma |
1745 | (Pragma_Argument_Associations => New_List ( | |
1746 | Make_Pragma_Argument_Association (Loc, | |
1747 | Expression => New_Occurrence_Of (E, Loc)), | |
1748 | Make_Pragma_Argument_Association (Sloc (Expr), | |
1749 | Expression => Relocate_Node (Expr))), | |
1750 | Pragma_Name => Chars (Id)); | |
57cd943b | 1751 | |
0fd13d32 | 1752 | -- Synchronization |
d74fc39a | 1753 | |
0fd13d32 | 1754 | -- Corresponds to pragma Implemented, construct the pragma |
49213728 | 1755 | |
5bbfbad2 | 1756 | when Aspect_Synchronization => |
0fd13d32 | 1757 | Make_Aitem_Pragma |
1758 | (Pragma_Argument_Associations => New_List ( | |
1759 | Make_Pragma_Argument_Association (Loc, | |
1760 | Expression => New_Occurrence_Of (E, Loc)), | |
1761 | Make_Pragma_Argument_Association (Sloc (Expr), | |
1762 | Expression => Relocate_Node (Expr))), | |
1763 | Pragma_Name => Name_Implemented); | |
49213728 | 1764 | |
e2bf777d | 1765 | -- Attach_Handler |
0fd13d32 | 1766 | |
89f1e35c | 1767 | when Aspect_Attach_Handler => |
0fd13d32 | 1768 | Make_Aitem_Pragma |
1769 | (Pragma_Argument_Associations => New_List ( | |
1770 | Make_Pragma_Argument_Association (Sloc (Ent), | |
1771 | Expression => Ent), | |
1772 | Make_Pragma_Argument_Association (Sloc (Expr), | |
1773 | Expression => Relocate_Node (Expr))), | |
1774 | Pragma_Name => Name_Attach_Handler); | |
1775 | ||
f67ed4f5 | 1776 | -- We need to insert this pragma into the tree to get proper |
1777 | -- processing and to look valid from a placement viewpoint. | |
1778 | ||
e2bf777d | 1779 | Insert_Pragma (Aitem); |
f67ed4f5 | 1780 | goto Continue; |
1781 | ||
0fd13d32 | 1782 | -- Dynamic_Predicate, Predicate, Static_Predicate |
89f1e35c | 1783 | |
1784 | when Aspect_Dynamic_Predicate | | |
1785 | Aspect_Predicate | | |
1786 | Aspect_Static_Predicate => | |
1787 | ||
a47ce82d | 1788 | -- These aspects apply only to subtypes |
1789 | ||
1790 | if not Is_Type (E) then | |
1791 | Error_Msg_N | |
1792 | ("predicate can only be specified for a subtype", | |
1793 | Aspect); | |
1794 | goto Continue; | |
7c0c95b8 | 1795 | |
1796 | elsif Is_Incomplete_Type (E) then | |
1797 | Error_Msg_N | |
1798 | ("predicate cannot apply to incomplete view", Aspect); | |
1799 | goto Continue; | |
a47ce82d | 1800 | end if; |
1801 | ||
89f1e35c | 1802 | -- Construct the pragma (always a pragma Predicate, with |
51ea9c94 | 1803 | -- flags recording whether it is static/dynamic). We also |
1804 | -- set flags recording this in the type itself. | |
89f1e35c | 1805 | |
0fd13d32 | 1806 | Make_Aitem_Pragma |
1807 | (Pragma_Argument_Associations => New_List ( | |
1808 | Make_Pragma_Argument_Association (Sloc (Ent), | |
1809 | Expression => Ent), | |
1810 | Make_Pragma_Argument_Association (Sloc (Expr), | |
1811 | Expression => Relocate_Node (Expr))), | |
1812 | Pragma_Name => Name_Predicate); | |
89f1e35c | 1813 | |
51ea9c94 | 1814 | -- Mark type has predicates, and remember what kind of |
1815 | -- aspect lead to this predicate (we need this to access | |
1816 | -- the right set of check policies later on). | |
1817 | ||
1818 | Set_Has_Predicates (E); | |
1819 | ||
1820 | if A_Id = Aspect_Dynamic_Predicate then | |
1821 | Set_Has_Dynamic_Predicate_Aspect (E); | |
1822 | elsif A_Id = Aspect_Static_Predicate then | |
1823 | Set_Has_Static_Predicate_Aspect (E); | |
1824 | end if; | |
1825 | ||
89f1e35c | 1826 | -- If the type is private, indicate that its completion |
6653b695 | 1827 | -- has a freeze node, because that is the one that will |
1828 | -- be visible at freeze time. | |
89f1e35c | 1829 | |
0fd13d32 | 1830 | if Is_Private_Type (E) and then Present (Full_View (E)) then |
89f1e35c | 1831 | Set_Has_Predicates (Full_View (E)); |
51ea9c94 | 1832 | |
1833 | if A_Id = Aspect_Dynamic_Predicate then | |
1834 | Set_Has_Dynamic_Predicate_Aspect (Full_View (E)); | |
1835 | elsif A_Id = Aspect_Static_Predicate then | |
1836 | Set_Has_Static_Predicate_Aspect (Full_View (E)); | |
1837 | end if; | |
1838 | ||
89f1e35c | 1839 | Set_Has_Delayed_Aspects (Full_View (E)); |
1840 | Ensure_Freeze_Node (Full_View (E)); | |
1841 | end if; | |
1842 | ||
1843 | -- Case 2b: Aspects corresponding to pragmas with two | |
1844 | -- arguments, where the second argument is a local name | |
1845 | -- referring to the entity, and the first argument is the | |
1846 | -- aspect definition expression. | |
ae888dbd | 1847 | |
0fd13d32 | 1848 | -- Convention |
1849 | ||
a5a64273 | 1850 | when Aspect_Convention => |
1851 | ||
1852 | -- The aspect may be part of the specification of an import | |
1853 | -- or export pragma. Scan the aspect list to gather the | |
1854 | -- other components, if any. The name of the generated | |
1855 | -- pragma is one of Convention/Import/Export. | |
1856 | ||
1857 | declare | |
97bf66e6 | 1858 | Args : constant List_Id := New_List ( |
1859 | Make_Pragma_Argument_Association (Sloc (Expr), | |
1860 | Expression => Relocate_Node (Expr)), | |
1861 | Make_Pragma_Argument_Association (Sloc (Ent), | |
1862 | Expression => Ent)); | |
1863 | ||
1864 | Imp_Exp_Seen : Boolean := False; | |
1865 | -- Flag set when aspect Import or Export has been seen | |
1866 | ||
1867 | Imp_Seen : Boolean := False; | |
1868 | -- Flag set when aspect Import has been seen | |
1869 | ||
1870 | Asp : Node_Id; | |
1871 | Asp_Nam : Name_Id; | |
1872 | Extern_Arg : Node_Id; | |
1873 | Link_Arg : Node_Id; | |
1874 | Prag_Nam : Name_Id; | |
a5a64273 | 1875 | |
1876 | begin | |
97bf66e6 | 1877 | Extern_Arg := Empty; |
1878 | Link_Arg := Empty; | |
1879 | Prag_Nam := Chars (Id); | |
1880 | ||
1881 | Asp := First (L); | |
1882 | while Present (Asp) loop | |
1883 | Asp_Nam := Chars (Identifier (Asp)); | |
1884 | ||
1885 | -- Aspects Import and Export take precedence over | |
1886 | -- aspect Convention. As a result the generated pragma | |
1887 | -- must carry the proper interfacing aspect's name. | |
1888 | ||
1889 | if Nam_In (Asp_Nam, Name_Import, Name_Export) then | |
1890 | if Imp_Exp_Seen then | |
1891 | Error_Msg_N ("conflicting", Asp); | |
a5a64273 | 1892 | else |
97bf66e6 | 1893 | Imp_Exp_Seen := True; |
1894 | ||
1895 | if Asp_Nam = Name_Import then | |
1896 | Imp_Seen := True; | |
1897 | end if; | |
a5a64273 | 1898 | end if; |
1899 | ||
97bf66e6 | 1900 | Prag_Nam := Asp_Nam; |
a5a64273 | 1901 | |
97bf66e6 | 1902 | -- Aspect External_Name adds an extra argument to the |
1903 | -- generated pragma. | |
1904 | ||
1905 | elsif Asp_Nam = Name_External_Name then | |
1906 | Extern_Arg := | |
4bba0a8d | 1907 | Make_Pragma_Argument_Association (Loc, |
97bf66e6 | 1908 | Chars => Asp_Nam, |
1909 | Expression => Relocate_Node (Expression (Asp))); | |
1910 | ||
1911 | -- Aspect Link_Name adds an extra argument to the | |
1912 | -- generated pragma. | |
a5a64273 | 1913 | |
97bf66e6 | 1914 | elsif Asp_Nam = Name_Link_Name then |
1915 | Link_Arg := | |
4bba0a8d | 1916 | Make_Pragma_Argument_Association (Loc, |
97bf66e6 | 1917 | Chars => Asp_Nam, |
1918 | Expression => Relocate_Node (Expression (Asp))); | |
a5a64273 | 1919 | end if; |
1920 | ||
97bf66e6 | 1921 | Next (Asp); |
a5a64273 | 1922 | end loop; |
1923 | ||
97bf66e6 | 1924 | -- Assemble the full argument list |
b9e61b2a | 1925 | |
97bf66e6 | 1926 | if Present (Extern_Arg) then |
1927 | Append_To (Args, Extern_Arg); | |
a5a64273 | 1928 | end if; |
1929 | ||
8a1e3cde | 1930 | if Present (Link_Arg) then |
1931 | Append_To (Args, Link_Arg); | |
1932 | end if; | |
1933 | ||
0fd13d32 | 1934 | Make_Aitem_Pragma |
97bf66e6 | 1935 | (Pragma_Argument_Associations => Args, |
1936 | Pragma_Name => Prag_Nam); | |
1937 | ||
1938 | -- Store the generated pragma Import in the related | |
1939 | -- subprogram. | |
1940 | ||
1941 | if Imp_Seen and then Is_Subprogram (E) then | |
1942 | Set_Import_Pragma (E, Aitem); | |
1943 | end if; | |
a5a64273 | 1944 | end; |
e1cedbae | 1945 | |
0fd13d32 | 1946 | -- CPU, Interrupt_Priority, Priority |
1947 | ||
d6814978 | 1948 | -- These three aspects can be specified for a subprogram spec |
1949 | -- or body, in which case we analyze the expression and export | |
1950 | -- the value of the aspect. | |
1951 | ||
1952 | -- Previously, we generated an equivalent pragma for bodies | |
1953 | -- (note that the specs cannot contain these pragmas). The | |
1954 | -- pragma was inserted ahead of local declarations, rather than | |
1955 | -- after the body. This leads to a certain duplication between | |
1956 | -- the processing performed for the aspect and the pragma, but | |
1957 | -- given the straightforward handling required it is simpler | |
1958 | -- to duplicate than to translate the aspect in the spec into | |
1959 | -- a pragma in the declarative part of the body. | |
3a72f9c3 | 1960 | |
1961 | when Aspect_CPU | | |
1962 | Aspect_Interrupt_Priority | | |
1963 | Aspect_Priority => | |
51ea9c94 | 1964 | |
d6814978 | 1965 | if Nkind_In (N, N_Subprogram_Body, |
1966 | N_Subprogram_Declaration) | |
1967 | then | |
1968 | -- Analyze the aspect expression | |
1969 | ||
1970 | Analyze_And_Resolve (Expr, Standard_Integer); | |
1971 | ||
1972 | -- Interrupt_Priority aspect not allowed for main | |
1973 | -- subprograms. ARM D.1 does not forbid this explicitly, | |
1974 | -- but ARM J.15.11 (6/3) does not permit pragma | |
1975 | -- Interrupt_Priority for subprograms. | |
1976 | ||
1977 | if A_Id = Aspect_Interrupt_Priority then | |
1978 | Error_Msg_N | |
1979 | ("Interrupt_Priority aspect cannot apply to " | |
1980 | & "subprogram", Expr); | |
1981 | ||
1982 | -- The expression must be static | |
1983 | ||
cda40848 | 1984 | elsif not Is_OK_Static_Expression (Expr) then |
d6814978 | 1985 | Flag_Non_Static_Expr |
1986 | ("aspect requires static expression!", Expr); | |
1987 | ||
24d7b9d6 | 1988 | -- Check whether this is the main subprogram. Issue a |
1989 | -- warning only if it is obviously not a main program | |
1990 | -- (when it has parameters or when the subprogram is | |
1991 | -- within a package). | |
1992 | ||
1993 | elsif Present (Parameter_Specifications | |
1994 | (Specification (N))) | |
1995 | or else not Is_Compilation_Unit (Defining_Entity (N)) | |
d6814978 | 1996 | then |
1997 | -- See ARM D.1 (14/3) and D.16 (12/3) | |
1998 | ||
1999 | Error_Msg_N | |
2000 | ("aspect applied to subprogram other than the " | |
2001 | & "main subprogram has no effect??", Expr); | |
2002 | ||
2003 | -- Otherwise check in range and export the value | |
2004 | ||
2005 | -- For the CPU aspect | |
2006 | ||
2007 | elsif A_Id = Aspect_CPU then | |
2008 | if Is_In_Range (Expr, RTE (RE_CPU_Range)) then | |
2009 | ||
2010 | -- Value is correct so we export the value to make | |
2011 | -- it available at execution time. | |
2012 | ||
2013 | Set_Main_CPU | |
2014 | (Main_Unit, UI_To_Int (Expr_Value (Expr))); | |
2015 | ||
2016 | else | |
2017 | Error_Msg_N | |
2018 | ("main subprogram CPU is out of range", Expr); | |
2019 | end if; | |
2020 | ||
2021 | -- For the Priority aspect | |
2022 | ||
2023 | elsif A_Id = Aspect_Priority then | |
2024 | if Is_In_Range (Expr, RTE (RE_Priority)) then | |
2025 | ||
2026 | -- Value is correct so we export the value to make | |
2027 | -- it available at execution time. | |
2028 | ||
2029 | Set_Main_Priority | |
2030 | (Main_Unit, UI_To_Int (Expr_Value (Expr))); | |
2031 | ||
32572384 | 2032 | -- Ignore pragma if Relaxed_RM_Semantics to support |
2033 | -- other targets/non GNAT compilers. | |
2034 | ||
2035 | elsif not Relaxed_RM_Semantics then | |
d6814978 | 2036 | Error_Msg_N |
2037 | ("main subprogram priority is out of range", | |
2038 | Expr); | |
2039 | end if; | |
2040 | end if; | |
2041 | ||
2042 | -- Load an arbitrary entity from System.Tasking.Stages | |
2043 | -- or System.Tasking.Restricted.Stages (depending on | |
2044 | -- the supported profile) to make sure that one of these | |
2045 | -- packages is implicitly with'ed, since we need to have | |
2046 | -- the tasking run time active for the pragma Priority to | |
a0c3eeb9 | 2047 | -- have any effect. Previously we with'ed the package |
d6814978 | 2048 | -- System.Tasking, but this package does not trigger the |
2049 | -- required initialization of the run-time library. | |
2050 | ||
2051 | declare | |
2052 | Discard : Entity_Id; | |
d6814978 | 2053 | begin |
2054 | if Restricted_Profile then | |
2055 | Discard := RTE (RE_Activate_Restricted_Tasks); | |
2056 | else | |
2057 | Discard := RTE (RE_Activate_Tasks); | |
2058 | end if; | |
2059 | end; | |
2060 | ||
2061 | -- Handling for these Aspects in subprograms is complete | |
2062 | ||
2063 | goto Continue; | |
2064 | ||
2065 | -- For tasks | |
0fd13d32 | 2066 | |
3a72f9c3 | 2067 | else |
d6814978 | 2068 | -- Pass the aspect as an attribute |
2069 | ||
3a72f9c3 | 2070 | Aitem := |
2071 | Make_Attribute_Definition_Clause (Loc, | |
2072 | Name => Ent, | |
2073 | Chars => Chars (Id), | |
2074 | Expression => Relocate_Node (Expr)); | |
2075 | end if; | |
2076 | ||
0fd13d32 | 2077 | -- Warnings |
2078 | ||
ae888dbd | 2079 | when Aspect_Warnings => |
0fd13d32 | 2080 | Make_Aitem_Pragma |
2081 | (Pragma_Argument_Associations => New_List ( | |
2082 | Make_Pragma_Argument_Association (Sloc (Expr), | |
2083 | Expression => Relocate_Node (Expr)), | |
2084 | Make_Pragma_Argument_Association (Loc, | |
2085 | Expression => New_Occurrence_Of (E, Loc))), | |
2086 | Pragma_Name => Chars (Id)); | |
ae888dbd | 2087 | |
89f1e35c | 2088 | -- Case 2c: Aspects corresponding to pragmas with three |
2089 | -- arguments. | |
d64221a7 | 2090 | |
89f1e35c | 2091 | -- Invariant aspects have a first argument that references the |
2092 | -- entity, a second argument that is the expression and a third | |
2093 | -- argument that is an appropriate message. | |
d64221a7 | 2094 | |
0fd13d32 | 2095 | -- Invariant, Type_Invariant |
2096 | ||
89f1e35c | 2097 | when Aspect_Invariant | |
2098 | Aspect_Type_Invariant => | |
d64221a7 | 2099 | |
89f1e35c | 2100 | -- Analysis of the pragma will verify placement legality: |
2101 | -- an invariant must apply to a private type, or appear in | |
2102 | -- the private part of a spec and apply to a completion. | |
d64221a7 | 2103 | |
0fd13d32 | 2104 | Make_Aitem_Pragma |
2105 | (Pragma_Argument_Associations => New_List ( | |
2106 | Make_Pragma_Argument_Association (Sloc (Ent), | |
2107 | Expression => Ent), | |
2108 | Make_Pragma_Argument_Association (Sloc (Expr), | |
2109 | Expression => Relocate_Node (Expr))), | |
2110 | Pragma_Name => Name_Invariant); | |
89f1e35c | 2111 | |
2112 | -- Add message unless exception messages are suppressed | |
2113 | ||
2114 | if not Opt.Exception_Locations_Suppressed then | |
2115 | Append_To (Pragma_Argument_Associations (Aitem), | |
2116 | Make_Pragma_Argument_Association (Eloc, | |
2117 | Chars => Name_Message, | |
2118 | Expression => | |
2119 | Make_String_Literal (Eloc, | |
2120 | Strval => "failed invariant from " | |
2121 | & Build_Location_String (Eloc)))); | |
d64221a7 | 2122 | end if; |
2123 | ||
89f1e35c | 2124 | -- For Invariant case, insert immediately after the entity |
2125 | -- declaration. We do not have to worry about delay issues | |
2126 | -- since the pragma processing takes care of this. | |
2127 | ||
89f1e35c | 2128 | Delay_Required := False; |
d64221a7 | 2129 | |
47a46747 | 2130 | -- Case 2d : Aspects that correspond to a pragma with one |
2131 | -- argument. | |
2132 | ||
0fd13d32 | 2133 | -- Abstract_State |
115f7b08 | 2134 | |
d4e369ad | 2135 | -- Aspect Abstract_State introduces implicit declarations for |
2136 | -- all state abstraction entities it defines. To emulate this | |
2137 | -- behavior, insert the pragma at the beginning of the visible | |
2138 | -- declarations of the related package so that it is analyzed | |
2139 | -- immediately. | |
2140 | ||
9129c28f | 2141 | when Aspect_Abstract_State => Abstract_State : declare |
eb4f7efa | 2142 | Context : Node_Id := N; |
630b6d55 | 2143 | Decl : Node_Id; |
eb4f7efa | 2144 | Decls : List_Id; |
9129c28f | 2145 | |
2146 | begin | |
eb4f7efa | 2147 | -- When aspect Abstract_State appears on a generic package, |
2148 | -- it is propageted to the package instance. The context in | |
2149 | -- this case is the instance spec. | |
2150 | ||
2151 | if Nkind (Context) = N_Package_Instantiation then | |
2152 | Context := Instance_Spec (Context); | |
2153 | end if; | |
2154 | ||
2155 | if Nkind_In (Context, N_Generic_Package_Declaration, | |
2156 | N_Package_Declaration) | |
9129c28f | 2157 | then |
9129c28f | 2158 | Make_Aitem_Pragma |
2159 | (Pragma_Argument_Associations => New_List ( | |
2160 | Make_Pragma_Argument_Association (Loc, | |
2161 | Expression => Relocate_Node (Expr))), | |
2162 | Pragma_Name => Name_Abstract_State); | |
e2bf777d | 2163 | Decorate (Aspect, Aitem); |
9129c28f | 2164 | |
630b6d55 | 2165 | Decls := Visible_Declarations (Specification (Context)); |
2166 | ||
2167 | -- In general pragma Abstract_State must be at the top | |
2168 | -- of the existing visible declarations to emulate its | |
2169 | -- source counterpart. The only exception to this is a | |
2170 | -- generic instance in which case the pragma must be | |
2171 | -- inserted after the association renamings. | |
2172 | ||
2173 | if Present (Decls) then | |
50e44732 | 2174 | Decl := First (Decls); |
630b6d55 | 2175 | |
2176 | -- The visible declarations of a generic instance have | |
2177 | -- the following structure: | |
2178 | ||
2179 | -- <renamings of generic formals> | |
2180 | -- <renamings of internally-generated spec and body> | |
2181 | -- <first source declaration> | |
2182 | ||
2183 | -- The pragma must be inserted before the first source | |
50e44732 | 2184 | -- declaration, skip the instance "header". |
630b6d55 | 2185 | |
2186 | if Is_Generic_Instance (Defining_Entity (Context)) then | |
630b6d55 | 2187 | while Present (Decl) |
2188 | and then not Comes_From_Source (Decl) | |
2189 | loop | |
2190 | Decl := Next (Decl); | |
2191 | end loop; | |
50e44732 | 2192 | end if; |
630b6d55 | 2193 | |
3dbe7a69 | 2194 | -- When aspects Abstract_State, Ghost, |
2195 | -- Initial_Condition and Initializes are out of order, | |
2196 | -- ensure that pragma SPARK_Mode is always at the top | |
2197 | -- of the declarations to properly enabled/suppress | |
2198 | -- errors. | |
630b6d55 | 2199 | |
50e44732 | 2200 | Insert_After_SPARK_Mode |
2201 | (Prag => Aitem, | |
2202 | Ins_Nod => Decl, | |
2203 | Decls => Decls); | |
630b6d55 | 2204 | |
2205 | -- Otherwise the pragma forms a new declarative list | |
2206 | ||
2207 | else | |
2208 | Set_Visible_Declarations | |
2209 | (Specification (Context), New_List (Aitem)); | |
2210 | end if; | |
9129c28f | 2211 | |
2212 | else | |
2213 | Error_Msg_NE | |
2214 | ("aspect & must apply to a package declaration", | |
2215 | Aspect, Id); | |
2216 | end if; | |
2217 | ||
2218 | goto Continue; | |
2219 | end Abstract_State; | |
115f7b08 | 2220 | |
ec6f6da5 | 2221 | -- Aspect Default_Internal_Condition is never delayed because |
2222 | -- it is equivalent to a source pragma which appears after the | |
2223 | -- related private type. To deal with forward references, the | |
2224 | -- generated pragma is stored in the rep chain of the related | |
2225 | -- private type as types do not carry contracts. The pragma is | |
2226 | -- wrapped inside of a procedure at the freeze point of the | |
2227 | -- private type's full view. | |
2228 | ||
2229 | when Aspect_Default_Initial_Condition => | |
2230 | Make_Aitem_Pragma | |
2231 | (Pragma_Argument_Associations => New_List ( | |
2232 | Make_Pragma_Argument_Association (Loc, | |
2233 | Expression => Relocate_Node (Expr))), | |
2234 | Pragma_Name => | |
2235 | Name_Default_Initial_Condition); | |
2236 | ||
2237 | Decorate (Aspect, Aitem); | |
2238 | Insert_Pragma (Aitem); | |
2239 | goto Continue; | |
2240 | ||
647fab54 | 2241 | -- Default_Storage_Pool |
2242 | ||
2243 | when Aspect_Default_Storage_Pool => | |
2244 | Make_Aitem_Pragma | |
2245 | (Pragma_Argument_Associations => New_List ( | |
2246 | Make_Pragma_Argument_Association (Loc, | |
2247 | Expression => Relocate_Node (Expr))), | |
2248 | Pragma_Name => | |
2249 | Name_Default_Storage_Pool); | |
2250 | ||
2251 | Decorate (Aspect, Aitem); | |
2252 | Insert_Pragma (Aitem); | |
2253 | goto Continue; | |
2254 | ||
0fd13d32 | 2255 | -- Depends |
2256 | ||
e2bf777d | 2257 | -- Aspect Depends is never delayed because it is equivalent to |
2258 | -- a source pragma which appears after the related subprogram. | |
2259 | -- To deal with forward references, the generated pragma is | |
2260 | -- stored in the contract of the related subprogram and later | |
2261 | -- analyzed at the end of the declarative region. See routine | |
2262 | -- Analyze_Depends_In_Decl_Part for details. | |
6144c105 | 2263 | |
12334c57 | 2264 | when Aspect_Depends => |
0fd13d32 | 2265 | Make_Aitem_Pragma |
2266 | (Pragma_Argument_Associations => New_List ( | |
2267 | Make_Pragma_Argument_Association (Loc, | |
2268 | Expression => Relocate_Node (Expr))), | |
2269 | Pragma_Name => Name_Depends); | |
2270 | ||
e2bf777d | 2271 | Decorate (Aspect, Aitem); |
2272 | Insert_Pragma (Aitem); | |
c1006d6d | 2273 | goto Continue; |
2274 | ||
cab27d2a | 2275 | -- Aspect Extensions_Visible is never delayed because it is |
2276 | -- equivalent to a source pragma which appears after the | |
2277 | -- related subprogram. | |
2278 | ||
2279 | when Aspect_Extensions_Visible => | |
2280 | Make_Aitem_Pragma | |
2281 | (Pragma_Argument_Associations => New_List ( | |
2282 | Make_Pragma_Argument_Association (Loc, | |
2283 | Expression => Relocate_Node (Expr))), | |
2284 | Pragma_Name => Name_Extensions_Visible); | |
2285 | ||
2286 | Decorate (Aspect, Aitem); | |
2287 | Insert_Pragma (Aitem); | |
2288 | goto Continue; | |
2289 | ||
3dbe7a69 | 2290 | -- Aspect Ghost is never delayed because it is equivalent to a |
2291 | -- source pragma which appears at the top of [generic] package | |
2292 | -- declarations or after an object, a [generic] subprogram, or | |
2293 | -- a type declaration. | |
2294 | ||
2295 | when Aspect_Ghost => Ghost : declare | |
2296 | Decls : List_Id; | |
2297 | ||
2298 | begin | |
2299 | Make_Aitem_Pragma | |
2300 | (Pragma_Argument_Associations => New_List ( | |
2301 | Make_Pragma_Argument_Association (Loc, | |
2302 | Expression => Relocate_Node (Expr))), | |
2303 | Pragma_Name => Name_Ghost); | |
2304 | ||
2305 | Decorate (Aspect, Aitem); | |
2306 | ||
2307 | -- When the aspect applies to a [generic] package, insert | |
2308 | -- the pragma at the top of the visible declarations. This | |
2309 | -- emulates the placement of a source pragma. | |
2310 | ||
2311 | if Nkind_In (N, N_Generic_Package_Declaration, | |
2312 | N_Package_Declaration) | |
2313 | then | |
2314 | Decls := Visible_Declarations (Specification (N)); | |
2315 | ||
2316 | if No (Decls) then | |
2317 | Decls := New_List; | |
2318 | Set_Visible_Declarations (N, Decls); | |
2319 | end if; | |
2320 | ||
2321 | -- When aspects Abstract_State, Ghost, Initial_Condition | |
2322 | -- and Initializes are out of order, ensure that pragma | |
2323 | -- SPARK_Mode is always at the top of the declarations to | |
2324 | -- properly enabled/suppress errors. | |
2325 | ||
2326 | Insert_After_SPARK_Mode | |
2327 | (Prag => Aitem, | |
2328 | Ins_Nod => First (Decls), | |
2329 | Decls => Decls); | |
2330 | ||
2331 | -- Otherwise the context is an object, [generic] subprogram | |
2332 | -- or type declaration. | |
2333 | ||
2334 | else | |
2335 | Insert_Pragma (Aitem); | |
2336 | end if; | |
2337 | ||
2338 | goto Continue; | |
2339 | end Ghost; | |
2340 | ||
0fd13d32 | 2341 | -- Global |
12334c57 | 2342 | |
e2bf777d | 2343 | -- Aspect Global is never delayed because it is equivalent to |
2344 | -- a source pragma which appears after the related subprogram. | |
2345 | -- To deal with forward references, the generated pragma is | |
2346 | -- stored in the contract of the related subprogram and later | |
2347 | -- analyzed at the end of the declarative region. See routine | |
2348 | -- Analyze_Global_In_Decl_Part for details. | |
3cdbaa5a | 2349 | |
2350 | when Aspect_Global => | |
0fd13d32 | 2351 | Make_Aitem_Pragma |
2352 | (Pragma_Argument_Associations => New_List ( | |
2353 | Make_Pragma_Argument_Association (Loc, | |
2354 | Expression => Relocate_Node (Expr))), | |
2355 | Pragma_Name => Name_Global); | |
2356 | ||
e2bf777d | 2357 | Decorate (Aspect, Aitem); |
2358 | Insert_Pragma (Aitem); | |
c1006d6d | 2359 | goto Continue; |
2360 | ||
9c138530 | 2361 | -- Initial_Condition |
2362 | ||
e2bf777d | 2363 | -- Aspect Initial_Condition is never delayed because it is |
2364 | -- equivalent to a source pragma which appears after the | |
2365 | -- related package. To deal with forward references, the | |
2366 | -- generated pragma is stored in the contract of the related | |
2367 | -- package and later analyzed at the end of the declarative | |
2368 | -- region. See routine Analyze_Initial_Condition_In_Decl_Part | |
2369 | -- for details. | |
9c138530 | 2370 | |
2371 | when Aspect_Initial_Condition => Initial_Condition : declare | |
eb4f7efa | 2372 | Context : Node_Id := N; |
2373 | Decls : List_Id; | |
9c138530 | 2374 | |
2375 | begin | |
e2bf777d | 2376 | -- When aspect Initial_Condition appears on a generic |
2377 | -- package, it is propageted to the package instance. The | |
2378 | -- context in this case is the instance spec. | |
eb4f7efa | 2379 | |
2380 | if Nkind (Context) = N_Package_Instantiation then | |
2381 | Context := Instance_Spec (Context); | |
2382 | end if; | |
2383 | ||
2384 | if Nkind_In (Context, N_Generic_Package_Declaration, | |
2385 | N_Package_Declaration) | |
9c138530 | 2386 | then |
eb4f7efa | 2387 | Decls := Visible_Declarations (Specification (Context)); |
9c138530 | 2388 | |
2389 | Make_Aitem_Pragma | |
2390 | (Pragma_Argument_Associations => New_List ( | |
2391 | Make_Pragma_Argument_Association (Loc, | |
2392 | Expression => Relocate_Node (Expr))), | |
2393 | Pragma_Name => | |
2394 | Name_Initial_Condition); | |
e2bf777d | 2395 | Decorate (Aspect, Aitem); |
9c138530 | 2396 | |
2397 | if No (Decls) then | |
2398 | Decls := New_List; | |
eb4f7efa | 2399 | Set_Visible_Declarations (Context, Decls); |
9c138530 | 2400 | end if; |
2401 | ||
3dbe7a69 | 2402 | -- When aspects Abstract_State, Ghost, Initial_Condition |
2403 | -- and Initializes are out of order, ensure that pragma | |
2404 | -- SPARK_Mode is always at the top of the declarations to | |
2405 | -- properly enabled/suppress errors. | |
50e44732 | 2406 | |
2407 | Insert_After_SPARK_Mode | |
2408 | (Prag => Aitem, | |
2409 | Ins_Nod => First (Decls), | |
2410 | Decls => Decls); | |
9c138530 | 2411 | |
2412 | else | |
2413 | Error_Msg_NE | |
2414 | ("aspect & must apply to a package declaration", | |
2415 | Aspect, Id); | |
2416 | end if; | |
2417 | ||
2418 | goto Continue; | |
2419 | end Initial_Condition; | |
2420 | ||
d4e369ad | 2421 | -- Initializes |
2422 | ||
e2bf777d | 2423 | -- Aspect Initializes is never delayed because it is equivalent |
2424 | -- to a source pragma appearing after the related package. To | |
2425 | -- deal with forward references, the generated pragma is stored | |
2426 | -- in the contract of the related package and later analyzed at | |
2427 | -- the end of the declarative region. For details, see routine | |
2428 | -- Analyze_Initializes_In_Decl_Part. | |
d4e369ad | 2429 | |
2430 | when Aspect_Initializes => Initializes : declare | |
eb4f7efa | 2431 | Context : Node_Id := N; |
2432 | Decls : List_Id; | |
d4e369ad | 2433 | |
2434 | begin | |
50e44732 | 2435 | -- When aspect Initializes appears on a generic package, |
2436 | -- it is propageted to the package instance. The context | |
2437 | -- in this case is the instance spec. | |
eb4f7efa | 2438 | |
2439 | if Nkind (Context) = N_Package_Instantiation then | |
2440 | Context := Instance_Spec (Context); | |
2441 | end if; | |
2442 | ||
2443 | if Nkind_In (Context, N_Generic_Package_Declaration, | |
2444 | N_Package_Declaration) | |
d4e369ad | 2445 | then |
eb4f7efa | 2446 | Decls := Visible_Declarations (Specification (Context)); |
d4e369ad | 2447 | |
2448 | Make_Aitem_Pragma | |
2449 | (Pragma_Argument_Associations => New_List ( | |
2450 | Make_Pragma_Argument_Association (Loc, | |
2451 | Expression => Relocate_Node (Expr))), | |
2452 | Pragma_Name => Name_Initializes); | |
e2bf777d | 2453 | Decorate (Aspect, Aitem); |
d4e369ad | 2454 | |
2455 | if No (Decls) then | |
2456 | Decls := New_List; | |
eb4f7efa | 2457 | Set_Visible_Declarations (Context, Decls); |
d4e369ad | 2458 | end if; |
2459 | ||
3dbe7a69 | 2460 | -- When aspects Abstract_State, Ghost, Initial_Condition |
2461 | -- and Initializes are out of order, ensure that pragma | |
2462 | -- SPARK_Mode is always at the top of the declarations to | |
2463 | -- properly enabled/suppress errors. | |
50e44732 | 2464 | |
2465 | Insert_After_SPARK_Mode | |
2466 | (Prag => Aitem, | |
2467 | Ins_Nod => First (Decls), | |
2468 | Decls => Decls); | |
d4e369ad | 2469 | |
2470 | else | |
2471 | Error_Msg_NE | |
2472 | ("aspect & must apply to a package declaration", | |
2473 | Aspect, Id); | |
2474 | end if; | |
2475 | ||
2476 | goto Continue; | |
2477 | end Initializes; | |
2478 | ||
1fd4313f | 2479 | -- Obsolescent |
2480 | ||
2481 | when Aspect_Obsolescent => declare | |
2482 | Args : List_Id; | |
2483 | ||
2484 | begin | |
2485 | if No (Expr) then | |
2486 | Args := No_List; | |
2487 | else | |
2488 | Args := New_List ( | |
2489 | Make_Pragma_Argument_Association (Sloc (Expr), | |
2490 | Expression => Relocate_Node (Expr))); | |
2491 | end if; | |
2492 | ||
2493 | Make_Aitem_Pragma | |
2494 | (Pragma_Argument_Associations => Args, | |
2495 | Pragma_Name => Chars (Id)); | |
2496 | end; | |
2497 | ||
5cc6f0cf | 2498 | -- Part_Of |
2499 | ||
2500 | when Aspect_Part_Of => | |
2501 | if Nkind_In (N, N_Object_Declaration, | |
2502 | N_Package_Instantiation) | |
2503 | then | |
2504 | Make_Aitem_Pragma | |
2505 | (Pragma_Argument_Associations => New_List ( | |
2506 | Make_Pragma_Argument_Association (Loc, | |
2507 | Expression => Relocate_Node (Expr))), | |
2508 | Pragma_Name => Name_Part_Of); | |
2509 | ||
2510 | else | |
2511 | Error_Msg_NE | |
2512 | ("aspect & must apply to a variable or package " | |
2513 | & "instantiation", Aspect, Id); | |
2514 | end if; | |
2515 | ||
5dd93a61 | 2516 | -- SPARK_Mode |
2517 | ||
778ebf56 | 2518 | when Aspect_SPARK_Mode => SPARK_Mode : declare |
2519 | Decls : List_Id; | |
2520 | ||
2521 | begin | |
5dd93a61 | 2522 | Make_Aitem_Pragma |
2523 | (Pragma_Argument_Associations => New_List ( | |
2524 | Make_Pragma_Argument_Association (Loc, | |
2525 | Expression => Relocate_Node (Expr))), | |
2526 | Pragma_Name => Name_SPARK_Mode); | |
5dd93a61 | 2527 | |
b90d9656 | 2528 | -- When the aspect appears on a package or a subprogram |
2529 | -- body, insert the generated pragma at the top of the body | |
2530 | -- declarations to emulate the behavior of a source pragma. | |
778ebf56 | 2531 | |
b90d9656 | 2532 | if Nkind_In (N, N_Package_Body, N_Subprogram_Body) then |
e2bf777d | 2533 | Decorate (Aspect, Aitem); |
5cc6f0cf | 2534 | |
778ebf56 | 2535 | Decls := Declarations (N); |
2536 | ||
2537 | if No (Decls) then | |
2538 | Decls := New_List; | |
2539 | Set_Declarations (N, Decls); | |
2540 | end if; | |
2541 | ||
a04f9d2e | 2542 | Prepend_To (Decls, Aitem); |
2543 | goto Continue; | |
2544 | ||
b90d9656 | 2545 | -- When the aspect is associated with a [generic] package |
2546 | -- declaration, insert the generated pragma at the top of | |
2547 | -- the visible declarations to emulate the behavior of a | |
2548 | -- source pragma. | |
a04f9d2e | 2549 | |
b90d9656 | 2550 | elsif Nkind_In (N, N_Generic_Package_Declaration, |
2551 | N_Package_Declaration) | |
2552 | then | |
e2bf777d | 2553 | Decorate (Aspect, Aitem); |
5cc6f0cf | 2554 | |
a04f9d2e | 2555 | Decls := Visible_Declarations (Specification (N)); |
2556 | ||
2557 | if No (Decls) then | |
2558 | Decls := New_List; | |
2559 | Set_Visible_Declarations (Specification (N), Decls); | |
2560 | end if; | |
2561 | ||
778ebf56 | 2562 | Prepend_To (Decls, Aitem); |
2563 | goto Continue; | |
2564 | end if; | |
2565 | end SPARK_Mode; | |
2566 | ||
4befb1a0 | 2567 | -- Refined_Depends |
2568 | ||
e2bf777d | 2569 | -- Aspect Refined_Depends is never delayed because it is |
2570 | -- equivalent to a source pragma which appears in the | |
2571 | -- declarations of the related subprogram body. To deal with | |
2572 | -- forward references, the generated pragma is stored in the | |
2573 | -- contract of the related subprogram body and later analyzed | |
2574 | -- at the end of the declarative region. For details, see | |
2575 | -- routine Analyze_Refined_Depends_In_Decl_Part. | |
4befb1a0 | 2576 | |
2577 | when Aspect_Refined_Depends => | |
422073ed | 2578 | Make_Aitem_Pragma |
2579 | (Pragma_Argument_Associations => New_List ( | |
2580 | Make_Pragma_Argument_Association (Loc, | |
2581 | Expression => Relocate_Node (Expr))), | |
2582 | Pragma_Name => Name_Refined_Depends); | |
2583 | ||
e2bf777d | 2584 | Decorate (Aspect, Aitem); |
2585 | Insert_Pragma (Aitem); | |
422073ed | 2586 | goto Continue; |
4befb1a0 | 2587 | |
2588 | -- Refined_Global | |
2589 | ||
e2bf777d | 2590 | -- Aspect Refined_Global is never delayed because it is |
2591 | -- equivalent to a source pragma which appears in the | |
2592 | -- declarations of the related subprogram body. To deal with | |
2593 | -- forward references, the generated pragma is stored in the | |
2594 | -- contract of the related subprogram body and later analyzed | |
2595 | -- at the end of the declarative region. For details, see | |
2596 | -- routine Analyze_Refined_Global_In_Decl_Part. | |
4befb1a0 | 2597 | |
2598 | when Aspect_Refined_Global => | |
28ff117f | 2599 | Make_Aitem_Pragma |
2600 | (Pragma_Argument_Associations => New_List ( | |
2601 | Make_Pragma_Argument_Association (Loc, | |
2602 | Expression => Relocate_Node (Expr))), | |
2603 | Pragma_Name => Name_Refined_Global); | |
2604 | ||
e2bf777d | 2605 | Decorate (Aspect, Aitem); |
2606 | Insert_Pragma (Aitem); | |
28ff117f | 2607 | goto Continue; |
4befb1a0 | 2608 | |
63b65b2d | 2609 | -- Refined_Post |
2610 | ||
2611 | when Aspect_Refined_Post => | |
2612 | Make_Aitem_Pragma | |
2613 | (Pragma_Argument_Associations => New_List ( | |
2614 | Make_Pragma_Argument_Association (Loc, | |
2615 | Expression => Relocate_Node (Expr))), | |
2616 | Pragma_Name => Name_Refined_Post); | |
2617 | ||
9129c28f | 2618 | -- Refined_State |
2619 | ||
2620 | when Aspect_Refined_State => Refined_State : declare | |
2621 | Decls : List_Id; | |
2622 | ||
2623 | begin | |
2624 | -- The corresponding pragma for Refined_State is inserted in | |
2625 | -- the declarations of the related package body. This action | |
2626 | -- synchronizes both the source and from-aspect versions of | |
2627 | -- the pragma. | |
2628 | ||
2629 | if Nkind (N) = N_Package_Body then | |
e2bf777d | 2630 | Decls := Declarations (N); |
2631 | ||
9129c28f | 2632 | Make_Aitem_Pragma |
2633 | (Pragma_Argument_Associations => New_List ( | |
2634 | Make_Pragma_Argument_Association (Loc, | |
2635 | Expression => Relocate_Node (Expr))), | |
2636 | Pragma_Name => Name_Refined_State); | |
e2bf777d | 2637 | Decorate (Aspect, Aitem); |
b9b2d6e5 | 2638 | |
e2bf777d | 2639 | if No (Decls) then |
2640 | Decls := New_List; | |
2641 | Set_Declarations (N, Decls); | |
2642 | end if; | |
b9b2d6e5 | 2643 | |
e2bf777d | 2644 | -- Pragma Refined_State must be inserted after pragma |
2645 | -- SPARK_Mode in the tree. This ensures that any error | |
2646 | -- messages dependent on SPARK_Mode will be properly | |
2647 | -- enabled/suppressed. | |
b9b2d6e5 | 2648 | |
e2bf777d | 2649 | Insert_After_SPARK_Mode |
2650 | (Prag => Aitem, | |
2651 | Ins_Nod => First (Decls), | |
2652 | Decls => Decls); | |
9129c28f | 2653 | |
2654 | else | |
2655 | Error_Msg_NE | |
2656 | ("aspect & must apply to a package body", Aspect, Id); | |
2657 | end if; | |
2658 | ||
2659 | goto Continue; | |
2660 | end Refined_State; | |
2661 | ||
0fd13d32 | 2662 | -- Relative_Deadline |
3cdbaa5a | 2663 | |
2664 | when Aspect_Relative_Deadline => | |
0fd13d32 | 2665 | Make_Aitem_Pragma |
2666 | (Pragma_Argument_Associations => New_List ( | |
2667 | Make_Pragma_Argument_Association (Loc, | |
2668 | Expression => Relocate_Node (Expr))), | |
2669 | Pragma_Name => Name_Relative_Deadline); | |
47a46747 | 2670 | |
2671 | -- If the aspect applies to a task, the corresponding pragma | |
2672 | -- must appear within its declarations, not after. | |
2673 | ||
2674 | if Nkind (N) = N_Task_Type_Declaration then | |
2675 | declare | |
2676 | Def : Node_Id; | |
2677 | V : List_Id; | |
2678 | ||
2679 | begin | |
2680 | if No (Task_Definition (N)) then | |
2681 | Set_Task_Definition (N, | |
2682 | Make_Task_Definition (Loc, | |
2683 | Visible_Declarations => New_List, | |
2684 | End_Label => Empty)); | |
2685 | end if; | |
2686 | ||
2687 | Def := Task_Definition (N); | |
2688 | V := Visible_Declarations (Def); | |
2689 | if not Is_Empty_List (V) then | |
2690 | Insert_Before (First (V), Aitem); | |
2691 | ||
2692 | else | |
2693 | Set_Visible_Declarations (Def, New_List (Aitem)); | |
2694 | end if; | |
2695 | ||
2696 | goto Continue; | |
2697 | end; | |
2698 | end if; | |
2699 | ||
956ffaf4 | 2700 | -- Case 2e: Annotate aspect |
2701 | ||
2702 | when Aspect_Annotate => | |
2703 | declare | |
2704 | Args : List_Id; | |
2705 | Pargs : List_Id; | |
2706 | Arg : Node_Id; | |
2707 | ||
2708 | begin | |
2709 | -- The argument can be a single identifier | |
2710 | ||
2711 | if Nkind (Expr) = N_Identifier then | |
2712 | ||
2713 | -- One level of parens is allowed | |
2714 | ||
2715 | if Paren_Count (Expr) > 1 then | |
2716 | Error_Msg_F ("extra parentheses ignored", Expr); | |
2717 | end if; | |
2718 | ||
2719 | Set_Paren_Count (Expr, 0); | |
2720 | ||
2721 | -- Add the single item to the list | |
2722 | ||
2723 | Args := New_List (Expr); | |
2724 | ||
2725 | -- Otherwise we must have an aggregate | |
2726 | ||
2727 | elsif Nkind (Expr) = N_Aggregate then | |
2728 | ||
2729 | -- Must be positional | |
2730 | ||
2731 | if Present (Component_Associations (Expr)) then | |
2732 | Error_Msg_F | |
2733 | ("purely positional aggregate required", Expr); | |
2734 | goto Continue; | |
2735 | end if; | |
2736 | ||
2737 | -- Must not be parenthesized | |
2738 | ||
2739 | if Paren_Count (Expr) /= 0 then | |
2740 | Error_Msg_F ("extra parentheses ignored", Expr); | |
2741 | end if; | |
2742 | ||
2743 | -- List of arguments is list of aggregate expressions | |
2744 | ||
2745 | Args := Expressions (Expr); | |
2746 | ||
2747 | -- Anything else is illegal | |
2748 | ||
2749 | else | |
2750 | Error_Msg_F ("wrong form for Annotate aspect", Expr); | |
2751 | goto Continue; | |
2752 | end if; | |
2753 | ||
2754 | -- Prepare pragma arguments | |
2755 | ||
2756 | Pargs := New_List; | |
2757 | Arg := First (Args); | |
2758 | while Present (Arg) loop | |
2759 | Append_To (Pargs, | |
2760 | Make_Pragma_Argument_Association (Sloc (Arg), | |
2761 | Expression => Relocate_Node (Arg))); | |
2762 | Next (Arg); | |
2763 | end loop; | |
2764 | ||
2765 | Append_To (Pargs, | |
2766 | Make_Pragma_Argument_Association (Sloc (Ent), | |
2767 | Chars => Name_Entity, | |
2768 | Expression => Ent)); | |
2769 | ||
2770 | Make_Aitem_Pragma | |
2771 | (Pragma_Argument_Associations => Pargs, | |
2772 | Pragma_Name => Name_Annotate); | |
2773 | end; | |
2774 | ||
89f1e35c | 2775 | -- Case 3 : Aspects that don't correspond to pragma/attribute |
2776 | -- definition clause. | |
7b9b2f05 | 2777 | |
89f1e35c | 2778 | -- Case 3a: The aspects listed below don't correspond to |
2779 | -- pragmas/attributes but do require delayed analysis. | |
7f694ca2 | 2780 | |
51fa2a45 | 2781 | -- Default_Value can only apply to a scalar type |
2782 | ||
2783 | when Aspect_Default_Value => | |
2784 | if not Is_Scalar_Type (E) then | |
2785 | Error_Msg_N | |
1089ff19 | 2786 | ("aspect Default_Value must apply to a scalar type", N); |
51fa2a45 | 2787 | end if; |
2788 | ||
2789 | Aitem := Empty; | |
2790 | ||
2791 | -- Default_Component_Value can only apply to an array type | |
2792 | -- with scalar components. | |
2793 | ||
2794 | when Aspect_Default_Component_Value => | |
2795 | if not (Is_Array_Type (E) | |
3f4c9ffc | 2796 | and then Is_Scalar_Type (Component_Type (E))) |
51fa2a45 | 2797 | then |
2798 | Error_Msg_N ("aspect Default_Component_Value can only " | |
2799 | & "apply to an array of scalar components", N); | |
2800 | end if; | |
0fd13d32 | 2801 | |
89f1e35c | 2802 | Aitem := Empty; |
7f694ca2 | 2803 | |
89f1e35c | 2804 | -- Case 3b: The aspects listed below don't correspond to |
2805 | -- pragmas/attributes and don't need delayed analysis. | |
95bc75fa | 2806 | |
0fd13d32 | 2807 | -- Implicit_Dereference |
2808 | ||
89f1e35c | 2809 | -- For Implicit_Dereference, External_Name and Link_Name, only |
2810 | -- the legality checks are done during the analysis, thus no | |
2811 | -- delay is required. | |
a8e38e1d | 2812 | |
89f1e35c | 2813 | when Aspect_Implicit_Dereference => |
2814 | Analyze_Aspect_Implicit_Dereference; | |
2815 | goto Continue; | |
7f694ca2 | 2816 | |
0fd13d32 | 2817 | -- External_Name, Link_Name |
2818 | ||
89f1e35c | 2819 | when Aspect_External_Name | |
2820 | Aspect_Link_Name => | |
2821 | Analyze_Aspect_External_Or_Link_Name; | |
2822 | goto Continue; | |
7f694ca2 | 2823 | |
0fd13d32 | 2824 | -- Dimension |
2825 | ||
89f1e35c | 2826 | when Aspect_Dimension => |
2827 | Analyze_Aspect_Dimension (N, Id, Expr); | |
2828 | goto Continue; | |
cb4c311d | 2829 | |
0fd13d32 | 2830 | -- Dimension_System |
2831 | ||
89f1e35c | 2832 | when Aspect_Dimension_System => |
2833 | Analyze_Aspect_Dimension_System (N, Id, Expr); | |
2834 | goto Continue; | |
7f694ca2 | 2835 | |
ceec4f7c | 2836 | -- Case 4: Aspects requiring special handling |
51ea9c94 | 2837 | |
e66f4e2a | 2838 | -- Pre/Post/Test_Case/Contract_Cases whose corresponding |
2839 | -- pragmas take care of the delay. | |
7f694ca2 | 2840 | |
0fd13d32 | 2841 | -- Pre/Post |
2842 | ||
1e3c4ae6 | 2843 | -- Aspects Pre/Post generate Precondition/Postcondition pragmas |
2844 | -- with a first argument that is the expression, and a second | |
2845 | -- argument that is an informative message if the test fails. | |
2846 | -- This is inserted right after the declaration, to get the | |
5b5df4a9 | 2847 | -- required pragma placement. The processing for the pragmas |
2848 | -- takes care of the required delay. | |
ae888dbd | 2849 | |
5ddd846b | 2850 | when Pre_Post_Aspects => Pre_Post : declare |
1e3c4ae6 | 2851 | Pname : Name_Id; |
ae888dbd | 2852 | |
1e3c4ae6 | 2853 | begin |
77ae6789 | 2854 | if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then |
1e3c4ae6 | 2855 | Pname := Name_Precondition; |
2856 | else | |
2857 | Pname := Name_Postcondition; | |
2858 | end if; | |
d74fc39a | 2859 | |
1e3c4ae6 | 2860 | -- If the expressions is of the form A and then B, then |
2861 | -- we generate separate Pre/Post aspects for the separate | |
2862 | -- clauses. Since we allow multiple pragmas, there is no | |
2863 | -- problem in allowing multiple Pre/Post aspects internally. | |
a273015d | 2864 | -- These should be treated in reverse order (B first and |
2865 | -- A second) since they are later inserted just after N in | |
2866 | -- the order they are treated. This way, the pragma for A | |
2867 | -- ends up preceding the pragma for B, which may have an | |
2868 | -- importance for the error raised (either constraint error | |
2869 | -- or precondition error). | |
1e3c4ae6 | 2870 | |
39e1f22f | 2871 | -- We do not do this for Pre'Class, since we have to put |
51fa2a45 | 2872 | -- these conditions together in a complex OR expression. |
ae888dbd | 2873 | |
4282d342 | 2874 | -- We do not do this in ASIS mode, as ASIS relies on the |
2875 | -- original node representing the complete expression, when | |
2876 | -- retrieving it through the source aspect table. | |
2877 | ||
2878 | if not ASIS_Mode | |
2879 | and then (Pname = Name_Postcondition | |
2880 | or else not Class_Present (Aspect)) | |
39e1f22f | 2881 | then |
2882 | while Nkind (Expr) = N_And_Then loop | |
2883 | Insert_After (Aspect, | |
a273015d | 2884 | Make_Aspect_Specification (Sloc (Left_Opnd (Expr)), |
39e1f22f | 2885 | Identifier => Identifier (Aspect), |
a273015d | 2886 | Expression => Relocate_Node (Left_Opnd (Expr)), |
39e1f22f | 2887 | Class_Present => Class_Present (Aspect), |
2888 | Split_PPC => True)); | |
a273015d | 2889 | Rewrite (Expr, Relocate_Node (Right_Opnd (Expr))); |
39e1f22f | 2890 | Eloc := Sloc (Expr); |
2891 | end loop; | |
2892 | end if; | |
ae888dbd | 2893 | |
48d6f069 | 2894 | -- Build the precondition/postcondition pragma |
2895 | ||
51fa2a45 | 2896 | -- Add note about why we do NOT need Copy_Tree here??? |
d74fc39a | 2897 | |
0fd13d32 | 2898 | Make_Aitem_Pragma |
2899 | (Pragma_Argument_Associations => New_List ( | |
2900 | Make_Pragma_Argument_Association (Eloc, | |
2901 | Chars => Name_Check, | |
a19e1763 | 2902 | Expression => Relocate_Node (Expr))), |
0fd13d32 | 2903 | Pragma_Name => Pname); |
39e1f22f | 2904 | |
2905 | -- Add message unless exception messages are suppressed | |
2906 | ||
2907 | if not Opt.Exception_Locations_Suppressed then | |
2908 | Append_To (Pragma_Argument_Associations (Aitem), | |
2909 | Make_Pragma_Argument_Association (Eloc, | |
2910 | Chars => Name_Message, | |
2911 | Expression => | |
2912 | Make_String_Literal (Eloc, | |
2913 | Strval => "failed " | |
2914 | & Get_Name_String (Pname) | |
2915 | & " from " | |
2916 | & Build_Location_String (Eloc)))); | |
2917 | end if; | |
d74fc39a | 2918 | |
7d20685d | 2919 | Set_Is_Delayed_Aspect (Aspect); |
d74fc39a | 2920 | |
1e3c4ae6 | 2921 | -- For Pre/Post cases, insert immediately after the entity |
2922 | -- declaration, since that is the required pragma placement. | |
2923 | -- Note that for these aspects, we do not have to worry | |
2924 | -- about delay issues, since the pragmas themselves deal | |
2925 | -- with delay of visibility for the expression analysis. | |
2926 | ||
e2bf777d | 2927 | Insert_Pragma (Aitem); |
299b347e | 2928 | |
1e3c4ae6 | 2929 | goto Continue; |
5ddd846b | 2930 | end Pre_Post; |
ae888dbd | 2931 | |
0fd13d32 | 2932 | -- Test_Case |
2933 | ||
e66f4e2a | 2934 | when Aspect_Test_Case => Test_Case : declare |
2935 | Args : List_Id; | |
2936 | Comp_Expr : Node_Id; | |
2937 | Comp_Assn : Node_Id; | |
2938 | New_Expr : Node_Id; | |
57cd943b | 2939 | |
e66f4e2a | 2940 | begin |
2941 | Args := New_List; | |
b0bc40fd | 2942 | |
e66f4e2a | 2943 | if Nkind (Parent (N)) = N_Compilation_Unit then |
2944 | Error_Msg_Name_1 := Nam; | |
2945 | Error_Msg_N ("incorrect placement of aspect `%`", E); | |
2946 | goto Continue; | |
2947 | end if; | |
6c545057 | 2948 | |
e66f4e2a | 2949 | if Nkind (Expr) /= N_Aggregate then |
2950 | Error_Msg_Name_1 := Nam; | |
2951 | Error_Msg_NE | |
2952 | ("wrong syntax for aspect `%` for &", Id, E); | |
2953 | goto Continue; | |
2954 | end if; | |
6c545057 | 2955 | |
e66f4e2a | 2956 | -- Make pragma expressions refer to the original aspect |
51fa2a45 | 2957 | -- expressions through the Original_Node link. This is used |
2958 | -- in semantic analysis for ASIS mode, so that the original | |
2959 | -- expression also gets analyzed. | |
e66f4e2a | 2960 | |
2961 | Comp_Expr := First (Expressions (Expr)); | |
2962 | while Present (Comp_Expr) loop | |
2963 | New_Expr := Relocate_Node (Comp_Expr); | |
2964 | Set_Original_Node (New_Expr, Comp_Expr); | |
2965 | Append_To (Args, | |
2966 | Make_Pragma_Argument_Association (Sloc (Comp_Expr), | |
2967 | Expression => New_Expr)); | |
2968 | Next (Comp_Expr); | |
2969 | end loop; | |
2970 | ||
2971 | Comp_Assn := First (Component_Associations (Expr)); | |
2972 | while Present (Comp_Assn) loop | |
2973 | if List_Length (Choices (Comp_Assn)) /= 1 | |
2974 | or else | |
2975 | Nkind (First (Choices (Comp_Assn))) /= N_Identifier | |
2976 | then | |
fad014fe | 2977 | Error_Msg_Name_1 := Nam; |
6c545057 | 2978 | Error_Msg_NE |
fad014fe | 2979 | ("wrong syntax for aspect `%` for &", Id, E); |
6c545057 | 2980 | goto Continue; |
2981 | end if; | |
2982 | ||
e66f4e2a | 2983 | New_Expr := Relocate_Node (Expression (Comp_Assn)); |
2984 | Set_Original_Node (New_Expr, Expression (Comp_Assn)); | |
2985 | Append_To (Args, | |
2986 | Make_Pragma_Argument_Association (Sloc (Comp_Assn), | |
2987 | Chars => Chars (First (Choices (Comp_Assn))), | |
2988 | Expression => New_Expr)); | |
2989 | Next (Comp_Assn); | |
2990 | end loop; | |
6c545057 | 2991 | |
e66f4e2a | 2992 | -- Build the test-case pragma |
6c545057 | 2993 | |
0fd13d32 | 2994 | Make_Aitem_Pragma |
2995 | (Pragma_Argument_Associations => Args, | |
2996 | Pragma_Name => Nam); | |
e66f4e2a | 2997 | end Test_Case; |
85696508 | 2998 | |
0fd13d32 | 2999 | -- Contract_Cases |
3000 | ||
5ddd846b | 3001 | when Aspect_Contract_Cases => |
0fd13d32 | 3002 | Make_Aitem_Pragma |
3003 | (Pragma_Argument_Associations => New_List ( | |
3004 | Make_Pragma_Argument_Association (Loc, | |
3005 | Expression => Relocate_Node (Expr))), | |
3006 | Pragma_Name => Nam); | |
3a128918 | 3007 | |
e2bf777d | 3008 | Decorate (Aspect, Aitem); |
3009 | Insert_Pragma (Aitem); | |
5ddd846b | 3010 | goto Continue; |
3a128918 | 3011 | |
89f1e35c | 3012 | -- Case 5: Special handling for aspects with an optional |
3013 | -- boolean argument. | |
85696508 | 3014 | |
89f1e35c | 3015 | -- In the general case, the corresponding pragma cannot be |
0fd13d32 | 3016 | -- generated yet because the evaluation of the boolean needs |
3017 | -- to be delayed till the freeze point. | |
3018 | ||
89f1e35c | 3019 | when Boolean_Aspects | |
3020 | Library_Unit_Aspects => | |
a5a64273 | 3021 | |
89f1e35c | 3022 | Set_Is_Boolean_Aspect (Aspect); |
a5a64273 | 3023 | |
89f1e35c | 3024 | -- Lock_Free aspect only apply to protected objects |
e1cedbae | 3025 | |
89f1e35c | 3026 | if A_Id = Aspect_Lock_Free then |
3027 | if Ekind (E) /= E_Protected_Type then | |
99a2d5bd | 3028 | Error_Msg_Name_1 := Nam; |
a5a64273 | 3029 | Error_Msg_N |
89f1e35c | 3030 | ("aspect % only applies to a protected object", |
3031 | Aspect); | |
3032 | ||
3033 | else | |
3034 | -- Set the Uses_Lock_Free flag to True if there is no | |
37c6e44c | 3035 | -- expression or if the expression is True. The |
89f1e35c | 3036 | -- evaluation of this aspect should be delayed to the |
37c6e44c | 3037 | -- freeze point (why???) |
89f1e35c | 3038 | |
e81df51c | 3039 | if No (Expr) |
3040 | or else Is_True (Static_Boolean (Expr)) | |
89f1e35c | 3041 | then |
3042 | Set_Uses_Lock_Free (E); | |
3043 | end if; | |
caf125ce | 3044 | |
3045 | Record_Rep_Item (E, Aspect); | |
a5a64273 | 3046 | end if; |
e1cedbae | 3047 | |
89f1e35c | 3048 | goto Continue; |
ae888dbd | 3049 | |
17631aa0 | 3050 | elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then |
d74fc39a | 3051 | |
39616053 | 3052 | -- For the case of aspects Import and Export, we don't |
3053 | -- consider that we know the entity is never set in the | |
3054 | -- source, since it is is likely modified outside the | |
3055 | -- program. | |
3056 | ||
3057 | -- Note: one might think that the analysis of the | |
3058 | -- resulting pragma would take care of that, but | |
3059 | -- that's not the case since it won't be from source. | |
3060 | ||
3061 | if Ekind (E) = E_Variable then | |
3062 | Set_Never_Set_In_Source (E, False); | |
3063 | end if; | |
3064 | ||
2e1821c9 | 3065 | -- In older versions of Ada the corresponding pragmas |
51fa2a45 | 3066 | -- specified a Convention. In Ada 2012 the convention is |
3067 | -- specified as a separate aspect, and it is optional, | |
2e1821c9 | 3068 | -- given that it defaults to Convention_Ada. The code |
3069 | -- that verifed that there was a matching convention | |
3070 | -- is now obsolete. | |
d74fc39a | 3071 | |
718d0d92 | 3072 | -- Resolve the expression of an Import or Export here, |
3073 | -- and require it to be of type Boolean and static. This | |
3074 | -- is not quite right, because in general this should be | |
3075 | -- delayed, but that seems tricky for these, because | |
3076 | -- normally Boolean aspects are replaced with pragmas at | |
3077 | -- the freeze point (in Make_Pragma_From_Boolean_Aspect), | |
3078 | -- but in the case of these aspects we can't generate | |
3079 | -- a simple pragma with just the entity name. ??? | |
3080 | ||
3081 | if not Present (Expr) | |
3082 | or else Is_True (Static_Boolean (Expr)) | |
3083 | then | |
3084 | if A_Id = Aspect_Import then | |
3085 | Set_Is_Imported (E); | |
dd4c44af | 3086 | |
718d0d92 | 3087 | -- An imported entity cannot have an explicit |
3088 | -- initialization. | |
dd4c44af | 3089 | |
718d0d92 | 3090 | if Nkind (N) = N_Object_Declaration |
3091 | and then Present (Expression (N)) | |
3092 | then | |
3093 | Error_Msg_N | |
3094 | ("imported entities cannot be initialized " | |
3095 | & "(RM B.1(24))", Expression (N)); | |
3096 | end if; | |
3097 | ||
3098 | elsif A_Id = Aspect_Export then | |
3099 | Set_Is_Exported (E); | |
dd4c44af | 3100 | end if; |
3101 | end if; | |
3102 | ||
89f1e35c | 3103 | goto Continue; |
3104 | end if; | |
d74fc39a | 3105 | |
37c6e44c | 3106 | -- Library unit aspects require special handling in the case |
3107 | -- of a package declaration, the pragma needs to be inserted | |
3108 | -- in the list of declarations for the associated package. | |
3109 | -- There is no issue of visibility delay for these aspects. | |
d64221a7 | 3110 | |
89f1e35c | 3111 | if A_Id in Library_Unit_Aspects |
178fec9b | 3112 | and then |
3113 | Nkind_In (N, N_Package_Declaration, | |
3114 | N_Generic_Package_Declaration) | |
89f1e35c | 3115 | and then Nkind (Parent (N)) /= N_Compilation_Unit |
3ad60f63 | 3116 | |
3117 | -- Aspect is legal on a local instantiation of a library- | |
3118 | -- level generic unit. | |
3119 | ||
b94a633e | 3120 | and then not Is_Generic_Instance (Defining_Entity (N)) |
89f1e35c | 3121 | then |
3122 | Error_Msg_N | |
dd4c44af | 3123 | ("incorrect context for library unit aspect&", Id); |
89f1e35c | 3124 | goto Continue; |
3125 | end if; | |
cce84b09 | 3126 | |
294709fa | 3127 | -- External property aspects are Boolean by nature, but |
3128 | -- their pragmas must contain two arguments, the second | |
3129 | -- being the optional Boolean expression. | |
3130 | ||
a7ed0410 | 3131 | if A_Id = Aspect_Async_Readers or else |
3132 | A_Id = Aspect_Async_Writers or else | |
3133 | A_Id = Aspect_Effective_Reads or else | |
3134 | A_Id = Aspect_Effective_Writes | |
294709fa | 3135 | then |
3136 | declare | |
3137 | Args : List_Id; | |
3138 | ||
3139 | begin | |
3140 | -- The first argument of the external property pragma | |
3141 | -- is the related object. | |
3142 | ||
a7ed0410 | 3143 | Args := |
3144 | New_List ( | |
3145 | Make_Pragma_Argument_Association (Sloc (Ent), | |
3146 | Expression => Ent)); | |
294709fa | 3147 | |
3148 | -- The second argument is the optional Boolean | |
3149 | -- expression which must be propagated even if it | |
3150 | -- evaluates to False as this has special semantic | |
3151 | -- meaning. | |
3152 | ||
3153 | if Present (Expr) then | |
3154 | Append_To (Args, | |
3155 | Make_Pragma_Argument_Association (Loc, | |
3156 | Expression => Relocate_Node (Expr))); | |
3157 | end if; | |
3158 | ||
3159 | Make_Aitem_Pragma | |
3160 | (Pragma_Argument_Associations => Args, | |
3161 | Pragma_Name => Nam); | |
3162 | end; | |
3163 | ||
51fa2a45 | 3164 | -- Cases where we do not delay, includes all cases where the |
3165 | -- expression is missing other than the above cases. | |
d74fc39a | 3166 | |
294709fa | 3167 | elsif not Delay_Required or else No (Expr) then |
0fd13d32 | 3168 | Make_Aitem_Pragma |
3169 | (Pragma_Argument_Associations => New_List ( | |
3170 | Make_Pragma_Argument_Association (Sloc (Ent), | |
3171 | Expression => Ent)), | |
3172 | Pragma_Name => Chars (Id)); | |
89f1e35c | 3173 | Delay_Required := False; |
ddf1337b | 3174 | |
89f1e35c | 3175 | -- In general cases, the corresponding pragma/attribute |
3176 | -- definition clause will be inserted later at the freezing | |
294709fa | 3177 | -- point, and we do not need to build it now. |
ddf1337b | 3178 | |
89f1e35c | 3179 | else |
3180 | Aitem := Empty; | |
3181 | end if; | |
ceec4f7c | 3182 | |
3183 | -- Storage_Size | |
3184 | ||
3185 | -- This is special because for access types we need to generate | |
3186 | -- an attribute definition clause. This also works for single | |
3187 | -- task declarations, but it does not work for task type | |
3188 | -- declarations, because we have the case where the expression | |
3189 | -- references a discriminant of the task type. That can't use | |
3190 | -- an attribute definition clause because we would not have | |
3191 | -- visibility on the discriminant. For that case we must | |
3192 | -- generate a pragma in the task definition. | |
3193 | ||
3194 | when Aspect_Storage_Size => | |
3195 | ||
3196 | -- Task type case | |
3197 | ||
3198 | if Ekind (E) = E_Task_Type then | |
3199 | declare | |
3200 | Decl : constant Node_Id := Declaration_Node (E); | |
3201 | ||
3202 | begin | |
3203 | pragma Assert (Nkind (Decl) = N_Task_Type_Declaration); | |
3204 | ||
3205 | -- If no task definition, create one | |
3206 | ||
3207 | if No (Task_Definition (Decl)) then | |
3208 | Set_Task_Definition (Decl, | |
3209 | Make_Task_Definition (Loc, | |
3210 | Visible_Declarations => Empty_List, | |
3211 | End_Label => Empty)); | |
3212 | end if; | |
3213 | ||
51fa2a45 | 3214 | -- Create a pragma and put it at the start of the task |
3215 | -- definition for the task type declaration. | |
ceec4f7c | 3216 | |
3217 | Make_Aitem_Pragma | |
3218 | (Pragma_Argument_Associations => New_List ( | |
3219 | Make_Pragma_Argument_Association (Loc, | |
3220 | Expression => Relocate_Node (Expr))), | |
3221 | Pragma_Name => Name_Storage_Size); | |
3222 | ||
3223 | Prepend | |
3224 | (Aitem, | |
3225 | Visible_Declarations (Task_Definition (Decl))); | |
3226 | goto Continue; | |
3227 | end; | |
3228 | ||
3229 | -- All other cases, generate attribute definition | |
3230 | ||
3231 | else | |
3232 | Aitem := | |
3233 | Make_Attribute_Definition_Clause (Loc, | |
3234 | Name => Ent, | |
3235 | Chars => Chars (Id), | |
3236 | Expression => Relocate_Node (Expr)); | |
3237 | end if; | |
89f1e35c | 3238 | end case; |
ddf1337b | 3239 | |
89f1e35c | 3240 | -- Attach the corresponding pragma/attribute definition clause to |
3241 | -- the aspect specification node. | |
d74fc39a | 3242 | |
89f1e35c | 3243 | if Present (Aitem) then |
e2bf777d | 3244 | Set_From_Aspect_Specification (Aitem); |
89f1e35c | 3245 | end if; |
53c179ea | 3246 | |
89f1e35c | 3247 | -- In the context of a compilation unit, we directly put the |
0fd13d32 | 3248 | -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux |
3249 | -- node (no delay is required here) except for aspects on a | |
51fa2a45 | 3250 | -- subprogram body (see below) and a generic package, for which we |
3251 | -- need to introduce the pragma before building the generic copy | |
3252 | -- (see sem_ch12), and for package instantiations, where the | |
3253 | -- library unit pragmas are better handled early. | |
ddf1337b | 3254 | |
9129c28f | 3255 | if Nkind (Parent (N)) = N_Compilation_Unit |
89f1e35c | 3256 | and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect)) |
3257 | then | |
3258 | declare | |
3259 | Aux : constant Node_Id := Aux_Decls_Node (Parent (N)); | |
7f694ca2 | 3260 | |
89f1e35c | 3261 | begin |
3262 | pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux); | |
7f694ca2 | 3263 | |
89f1e35c | 3264 | -- For a Boolean aspect, create the corresponding pragma if |
3265 | -- no expression or if the value is True. | |
7f694ca2 | 3266 | |
b9e61b2a | 3267 | if Is_Boolean_Aspect (Aspect) and then No (Aitem) then |
89f1e35c | 3268 | if Is_True (Static_Boolean (Expr)) then |
0fd13d32 | 3269 | Make_Aitem_Pragma |
3270 | (Pragma_Argument_Associations => New_List ( | |
3271 | Make_Pragma_Argument_Association (Sloc (Ent), | |
3272 | Expression => Ent)), | |
3273 | Pragma_Name => Chars (Id)); | |
7f694ca2 | 3274 | |
89f1e35c | 3275 | Set_From_Aspect_Specification (Aitem, True); |
3276 | Set_Corresponding_Aspect (Aitem, Aspect); | |
3277 | ||
3278 | else | |
3279 | goto Continue; | |
3280 | end if; | |
3281 | end if; | |
7f694ca2 | 3282 | |
d6814978 | 3283 | -- If the aspect is on a subprogram body (relevant aspect |
3284 | -- is Inline), add the pragma in front of the declarations. | |
3a72f9c3 | 3285 | |
3286 | if Nkind (N) = N_Subprogram_Body then | |
3287 | if No (Declarations (N)) then | |
3288 | Set_Declarations (N, New_List); | |
3289 | end if; | |
3290 | ||
3291 | Prepend (Aitem, Declarations (N)); | |
3292 | ||
178fec9b | 3293 | elsif Nkind (N) = N_Generic_Package_Declaration then |
3294 | if No (Visible_Declarations (Specification (N))) then | |
3295 | Set_Visible_Declarations (Specification (N), New_List); | |
3296 | end if; | |
3297 | ||
3298 | Prepend (Aitem, | |
3299 | Visible_Declarations (Specification (N))); | |
3300 | ||
c39cce40 | 3301 | elsif Nkind (N) = N_Package_Instantiation then |
df8b0dae | 3302 | declare |
3303 | Spec : constant Node_Id := | |
3304 | Specification (Instance_Spec (N)); | |
3305 | begin | |
3306 | if No (Visible_Declarations (Spec)) then | |
3307 | Set_Visible_Declarations (Spec, New_List); | |
3308 | end if; | |
3309 | ||
3310 | Prepend (Aitem, Visible_Declarations (Spec)); | |
3311 | end; | |
3312 | ||
3a72f9c3 | 3313 | else |
3314 | if No (Pragmas_After (Aux)) then | |
d4596fbe | 3315 | Set_Pragmas_After (Aux, New_List); |
3a72f9c3 | 3316 | end if; |
3317 | ||
3318 | Append (Aitem, Pragmas_After (Aux)); | |
89f1e35c | 3319 | end if; |
7f694ca2 | 3320 | |
89f1e35c | 3321 | goto Continue; |
3322 | end; | |
3323 | end if; | |
7f694ca2 | 3324 | |
89f1e35c | 3325 | -- The evaluation of the aspect is delayed to the freezing point. |
3326 | -- The pragma or attribute clause if there is one is then attached | |
37c6e44c | 3327 | -- to the aspect specification which is put in the rep item list. |
1a814552 | 3328 | |
89f1e35c | 3329 | if Delay_Required then |
3330 | if Present (Aitem) then | |
3331 | Set_Is_Delayed_Aspect (Aitem); | |
3332 | Set_Aspect_Rep_Item (Aspect, Aitem); | |
3333 | Set_Parent (Aitem, Aspect); | |
3334 | end if; | |
1a814552 | 3335 | |
89f1e35c | 3336 | Set_Is_Delayed_Aspect (Aspect); |
9f36e3fb | 3337 | |
cba2ae82 | 3338 | -- In the case of Default_Value, link the aspect to base type |
3339 | -- as well, even though it appears on a first subtype. This is | |
3340 | -- mandated by the semantics of the aspect. Do not establish | |
3341 | -- the link when processing the base type itself as this leads | |
3342 | -- to a rep item circularity. Verify that we are dealing with | |
3343 | -- a scalar type to prevent cascaded errors. | |
3344 | ||
3345 | if A_Id = Aspect_Default_Value | |
3346 | and then Is_Scalar_Type (E) | |
3347 | and then Base_Type (E) /= E | |
3348 | then | |
9f36e3fb | 3349 | Set_Has_Delayed_Aspects (Base_Type (E)); |
3350 | Record_Rep_Item (Base_Type (E), Aspect); | |
3351 | end if; | |
3352 | ||
89f1e35c | 3353 | Set_Has_Delayed_Aspects (E); |
3354 | Record_Rep_Item (E, Aspect); | |
ddf1337b | 3355 | |
b855559d | 3356 | -- When delay is not required and the context is a package or a |
3357 | -- subprogram body, insert the pragma in the body declarations. | |
f55ce169 | 3358 | |
b855559d | 3359 | elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then |
f55ce169 | 3360 | if No (Declarations (N)) then |
3361 | Set_Declarations (N, New_List); | |
3362 | end if; | |
3363 | ||
3364 | -- The pragma is added before source declarations | |
3365 | ||
3366 | Prepend_To (Declarations (N), Aitem); | |
3367 | ||
89f1e35c | 3368 | -- When delay is not required and the context is not a compilation |
3369 | -- unit, we simply insert the pragma/attribute definition clause | |
3370 | -- in sequence. | |
ddf1337b | 3371 | |
89f1e35c | 3372 | else |
3373 | Insert_After (Ins_Node, Aitem); | |
3374 | Ins_Node := Aitem; | |
d74fc39a | 3375 | end if; |
0fd13d32 | 3376 | end Analyze_One_Aspect; |
ae888dbd | 3377 | |
d64221a7 | 3378 | <<Continue>> |
3379 | Next (Aspect); | |
21ea3a4f | 3380 | end loop Aspect_Loop; |
89f1e35c | 3381 | |
3382 | if Has_Delayed_Aspects (E) then | |
3383 | Ensure_Freeze_Node (E); | |
3384 | end if; | |
21ea3a4f | 3385 | end Analyze_Aspect_Specifications; |
ae888dbd | 3386 | |
d6f39728 | 3387 | ----------------------- |
3388 | -- Analyze_At_Clause -- | |
3389 | ----------------------- | |
3390 | ||
3391 | -- An at clause is replaced by the corresponding Address attribute | |
3392 | -- definition clause that is the preferred approach in Ada 95. | |
3393 | ||
3394 | procedure Analyze_At_Clause (N : Node_Id) is | |
177675a7 | 3395 | CS : constant Boolean := Comes_From_Source (N); |
3396 | ||
d6f39728 | 3397 | begin |
177675a7 | 3398 | -- This is an obsolescent feature |
3399 | ||
e0521a36 | 3400 | Check_Restriction (No_Obsolescent_Features, N); |
3401 | ||
9dfe12ae | 3402 | if Warn_On_Obsolescent_Feature then |
3403 | Error_Msg_N | |
b174444e | 3404 | ("?j?at clause is an obsolescent feature (RM J.7(2))", N); |
9dfe12ae | 3405 | Error_Msg_N |
b174444e | 3406 | ("\?j?use address attribute definition clause instead", N); |
9dfe12ae | 3407 | end if; |
3408 | ||
177675a7 | 3409 | -- Rewrite as address clause |
3410 | ||
d6f39728 | 3411 | Rewrite (N, |
3412 | Make_Attribute_Definition_Clause (Sloc (N), | |
935e86e0 | 3413 | Name => Identifier (N), |
3414 | Chars => Name_Address, | |
d6f39728 | 3415 | Expression => Expression (N))); |
177675a7 | 3416 | |
2beb22b1 | 3417 | -- We preserve Comes_From_Source, since logically the clause still comes |
3418 | -- from the source program even though it is changed in form. | |
177675a7 | 3419 | |
3420 | Set_Comes_From_Source (N, CS); | |
3421 | ||
3422 | -- Analyze rewritten clause | |
3423 | ||
d6f39728 | 3424 | Analyze_Attribute_Definition_Clause (N); |
3425 | end Analyze_At_Clause; | |
3426 | ||
3427 | ----------------------------------------- | |
3428 | -- Analyze_Attribute_Definition_Clause -- | |
3429 | ----------------------------------------- | |
3430 | ||
3431 | procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is | |
3432 | Loc : constant Source_Ptr := Sloc (N); | |
3433 | Nam : constant Node_Id := Name (N); | |
3434 | Attr : constant Name_Id := Chars (N); | |
3435 | Expr : constant Node_Id := Expression (N); | |
3436 | Id : constant Attribute_Id := Get_Attribute_Id (Attr); | |
d64221a7 | 3437 | |
3438 | Ent : Entity_Id; | |
3439 | -- The entity of Nam after it is analyzed. In the case of an incomplete | |
3440 | -- type, this is the underlying type. | |
3441 | ||
d6f39728 | 3442 | U_Ent : Entity_Id; |
d64221a7 | 3443 | -- The underlying entity to which the attribute applies. Generally this |
3444 | -- is the Underlying_Type of Ent, except in the case where the clause | |
3445 | -- applies to full view of incomplete type or private type in which case | |
3446 | -- U_Ent is just a copy of Ent. | |
d6f39728 | 3447 | |
3448 | FOnly : Boolean := False; | |
3449 | -- Reset to True for subtype specific attribute (Alignment, Size) | |
51fa2a45 | 3450 | -- and for stream attributes, i.e. those cases where in the call to |
3451 | -- Rep_Item_Too_Late, FOnly is set True so that only the freezing rules | |
3452 | -- are checked. Note that the case of stream attributes is not clear | |
3453 | -- from the RM, but see AI95-00137. Also, the RM seems to disallow | |
3454 | -- Storage_Size for derived task types, but that is also clearly | |
3455 | -- unintentional. | |
d6f39728 | 3456 | |
9f373bb8 | 3457 | procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type); |
3458 | -- Common processing for 'Read, 'Write, 'Input and 'Output attribute | |
3459 | -- definition clauses. | |
3460 | ||
ae888dbd | 3461 | function Duplicate_Clause return Boolean; |
3462 | -- This routine checks if the aspect for U_Ent being given by attribute | |
3463 | -- definition clause N is for an aspect that has already been specified, | |
3464 | -- and if so gives an error message. If there is a duplicate, True is | |
3465 | -- returned, otherwise if there is no error, False is returned. | |
3466 | ||
81b424ac | 3467 | procedure Check_Indexing_Functions; |
3468 | -- Check that the function in Constant_Indexing or Variable_Indexing | |
3469 | -- attribute has the proper type structure. If the name is overloaded, | |
cac18f71 | 3470 | -- check that some interpretation is legal. |
81b424ac | 3471 | |
89cc7147 | 3472 | procedure Check_Iterator_Functions; |
3473 | -- Check that there is a single function in Default_Iterator attribute | |
8df4f2a5 | 3474 | -- has the proper type structure. |
89cc7147 | 3475 | |
3476 | function Check_Primitive_Function (Subp : Entity_Id) return Boolean; | |
d03bfaa1 | 3477 | -- Common legality check for the previous two |
89cc7147 | 3478 | |
177675a7 | 3479 | ----------------------------------- |
3480 | -- Analyze_Stream_TSS_Definition -- | |
3481 | ----------------------------------- | |
3482 | ||
9f373bb8 | 3483 | procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is |
3484 | Subp : Entity_Id := Empty; | |
3485 | I : Interp_Index; | |
3486 | It : Interp; | |
3487 | Pnam : Entity_Id; | |
3488 | ||
3489 | Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read); | |
d64221a7 | 3490 | -- True for Read attribute, false for other attributes |
9f373bb8 | 3491 | |
3492 | function Has_Good_Profile (Subp : Entity_Id) return Boolean; | |
3493 | -- Return true if the entity is a subprogram with an appropriate | |
3494 | -- profile for the attribute being defined. | |
3495 | ||
3496 | ---------------------- | |
3497 | -- Has_Good_Profile -- | |
3498 | ---------------------- | |
3499 | ||
3500 | function Has_Good_Profile (Subp : Entity_Id) return Boolean is | |
3501 | F : Entity_Id; | |
3502 | Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input); | |
3503 | Expected_Ekind : constant array (Boolean) of Entity_Kind := | |
3504 | (False => E_Procedure, True => E_Function); | |
3505 | Typ : Entity_Id; | |
3506 | ||
3507 | begin | |
3508 | if Ekind (Subp) /= Expected_Ekind (Is_Function) then | |
3509 | return False; | |
3510 | end if; | |
3511 | ||
3512 | F := First_Formal (Subp); | |
3513 | ||
3514 | if No (F) | |
3515 | or else Ekind (Etype (F)) /= E_Anonymous_Access_Type | |
3516 | or else Designated_Type (Etype (F)) /= | |
3517 | Class_Wide_Type (RTE (RE_Root_Stream_Type)) | |
3518 | then | |
3519 | return False; | |
3520 | end if; | |
3521 | ||
3522 | if not Is_Function then | |
3523 | Next_Formal (F); | |
3524 | ||
3525 | declare | |
3526 | Expected_Mode : constant array (Boolean) of Entity_Kind := | |
3527 | (False => E_In_Parameter, | |
3528 | True => E_Out_Parameter); | |
3529 | begin | |
3530 | if Parameter_Mode (F) /= Expected_Mode (Is_Read) then | |
3531 | return False; | |
3532 | end if; | |
3533 | end; | |
3534 | ||
3535 | Typ := Etype (F); | |
3536 | ||
b64082f2 | 3537 | -- If the attribute specification comes from an aspect |
51fa2a45 | 3538 | -- specification for a class-wide stream, the parameter must be |
3539 | -- a class-wide type of the entity to which the aspect applies. | |
b64082f2 | 3540 | |
3541 | if From_Aspect_Specification (N) | |
3542 | and then Class_Present (Parent (N)) | |
3543 | and then Is_Class_Wide_Type (Typ) | |
3544 | then | |
3545 | Typ := Etype (Typ); | |
3546 | end if; | |
3547 | ||
9f373bb8 | 3548 | else |
3549 | Typ := Etype (Subp); | |
3550 | end if; | |
3551 | ||
51fa2a45 | 3552 | -- Verify that the prefix of the attribute and the local name for |
3553 | -- the type of the formal match. | |
48680a09 | 3554 | |
3555 | if Base_Type (Typ) /= Base_Type (Ent) | |
299b347e | 3556 | or else Present (Next_Formal (F)) |
48680a09 | 3557 | then |
3558 | return False; | |
3559 | ||
3560 | elsif not Is_Scalar_Type (Typ) | |
3561 | and then not Is_First_Subtype (Typ) | |
3562 | and then not Is_Class_Wide_Type (Typ) | |
3563 | then | |
3564 | return False; | |
3565 | ||
3566 | else | |
3567 | return True; | |
3568 | end if; | |
9f373bb8 | 3569 | end Has_Good_Profile; |
3570 | ||
3571 | -- Start of processing for Analyze_Stream_TSS_Definition | |
3572 | ||
3573 | begin | |
3574 | FOnly := True; | |
3575 | ||
3576 | if not Is_Type (U_Ent) then | |
3577 | Error_Msg_N ("local name must be a subtype", Nam); | |
3578 | return; | |
48680a09 | 3579 | |
3580 | elsif not Is_First_Subtype (U_Ent) then | |
3581 | Error_Msg_N ("local name must be a first subtype", Nam); | |
3582 | return; | |
9f373bb8 | 3583 | end if; |
3584 | ||
3585 | Pnam := TSS (Base_Type (U_Ent), TSS_Nam); | |
3586 | ||
44e4341e | 3587 | -- If Pnam is present, it can be either inherited from an ancestor |
3588 | -- type (in which case it is legal to redefine it for this type), or | |
3589 | -- be a previous definition of the attribute for the same type (in | |
3590 | -- which case it is illegal). | |
3591 | ||
3592 | -- In the first case, it will have been analyzed already, and we | |
3593 | -- can check that its profile does not match the expected profile | |
3594 | -- for a stream attribute of U_Ent. In the second case, either Pnam | |
3595 | -- has been analyzed (and has the expected profile), or it has not | |
3596 | -- been analyzed yet (case of a type that has not been frozen yet | |
3597 | -- and for which the stream attribute has been set using Set_TSS). | |
3598 | ||
3599 | if Present (Pnam) | |
3600 | and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam)) | |
3601 | then | |
9f373bb8 | 3602 | Error_Msg_Sloc := Sloc (Pnam); |
3603 | Error_Msg_Name_1 := Attr; | |
3604 | Error_Msg_N ("% attribute already defined #", Nam); | |
3605 | return; | |
3606 | end if; | |
3607 | ||
3608 | Analyze (Expr); | |
3609 | ||
3610 | if Is_Entity_Name (Expr) then | |
3611 | if not Is_Overloaded (Expr) then | |
3612 | if Has_Good_Profile (Entity (Expr)) then | |
3613 | Subp := Entity (Expr); | |
3614 | end if; | |
3615 | ||
3616 | else | |
3617 | Get_First_Interp (Expr, I, It); | |
9f373bb8 | 3618 | while Present (It.Nam) loop |
3619 | if Has_Good_Profile (It.Nam) then | |
3620 | Subp := It.Nam; | |
3621 | exit; | |
3622 | end if; | |
3623 | ||
3624 | Get_Next_Interp (I, It); | |
3625 | end loop; | |
3626 | end if; | |
3627 | end if; | |
3628 | ||
3629 | if Present (Subp) then | |
59ac57b5 | 3630 | if Is_Abstract_Subprogram (Subp) then |
9f373bb8 | 3631 | Error_Msg_N ("stream subprogram must not be abstract", Expr); |
3632 | return; | |
e12b2502 | 3633 | |
299b347e | 3634 | -- A stream subprogram for an interface type must be a null |
3635 | -- procedure (RM 13.13.2 (38/3)). | |
e12b2502 | 3636 | |
3637 | elsif Is_Interface (U_Ent) | |
3638 | and then not Inside_A_Generic | |
3639 | and then Ekind (Subp) = E_Procedure | |
3640 | and then | |
3641 | not Null_Present | |
3642 | (Specification | |
3643 | (Unit_Declaration_Node (Ultimate_Alias (Subp)))) | |
e12b2502 | 3644 | then |
3645 | Error_Msg_N | |
3646 | ("stream subprogram for interface type " | |
3647 | & "must be null procedure", Expr); | |
9f373bb8 | 3648 | end if; |
3649 | ||
3650 | Set_Entity (Expr, Subp); | |
3651 | Set_Etype (Expr, Etype (Subp)); | |
3652 | ||
44e4341e | 3653 | New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam); |
9f373bb8 | 3654 | |
3655 | else | |
3656 | Error_Msg_Name_1 := Attr; | |
3657 | Error_Msg_N ("incorrect expression for% attribute", Expr); | |
3658 | end if; | |
3659 | end Analyze_Stream_TSS_Definition; | |
3660 | ||
81b424ac | 3661 | ------------------------------ |
3662 | -- Check_Indexing_Functions -- | |
3663 | ------------------------------ | |
3664 | ||
3665 | procedure Check_Indexing_Functions is | |
c8a2d809 | 3666 | Indexing_Found : Boolean := False; |
8df4f2a5 | 3667 | |
81b424ac | 3668 | procedure Check_One_Function (Subp : Entity_Id); |
7796365f | 3669 | -- Check one possible interpretation. Sets Indexing_Found True if a |
3670 | -- legal indexing function is found. | |
81b424ac | 3671 | |
05987af3 | 3672 | procedure Illegal_Indexing (Msg : String); |
3673 | -- Diagnose illegal indexing function if not overloaded. In the | |
3674 | -- overloaded case indicate that no legal interpretation exists. | |
3675 | ||
81b424ac | 3676 | ------------------------ |
3677 | -- Check_One_Function -- | |
3678 | ------------------------ | |
3679 | ||
3680 | procedure Check_One_Function (Subp : Entity_Id) is | |
05987af3 | 3681 | Default_Element : Node_Id; |
3682 | Ret_Type : constant Entity_Id := Etype (Subp); | |
1b7510f9 | 3683 | |
81b424ac | 3684 | begin |
05987af3 | 3685 | if not Is_Overloadable (Subp) then |
3686 | Illegal_Indexing ("illegal indexing function for type&"); | |
3687 | return; | |
3688 | ||
7796365f | 3689 | elsif Scope (Subp) /= Scope (Ent) then |
3690 | if Nkind (Expr) = N_Expanded_Name then | |
3691 | ||
3692 | -- Indexing function can't be declared elsewhere | |
3693 | ||
3694 | Illegal_Indexing | |
3695 | ("indexing function must be declared in scope of type&"); | |
3696 | end if; | |
3697 | ||
05987af3 | 3698 | return; |
3699 | ||
3700 | elsif No (First_Formal (Subp)) then | |
3701 | Illegal_Indexing | |
3702 | ("Indexing requires a function that applies to type&"); | |
3703 | return; | |
3704 | ||
3705 | elsif No (Next_Formal (First_Formal (Subp))) then | |
3706 | Illegal_Indexing | |
3707 | ("indexing function must have at least two parameters"); | |
3708 | return; | |
3709 | ||
3710 | elsif Is_Derived_Type (Ent) then | |
3711 | if (Attr = Name_Constant_Indexing | |
3712 | and then Present | |
3713 | (Find_Aspect (Etype (Ent), Aspect_Constant_Indexing))) | |
f02a9a9a | 3714 | or else |
3715 | (Attr = Name_Variable_Indexing | |
3716 | and then Present | |
3717 | (Find_Aspect (Etype (Ent), Aspect_Variable_Indexing))) | |
05987af3 | 3718 | then |
3719 | if Debug_Flag_Dot_XX then | |
3720 | null; | |
3721 | ||
3722 | else | |
3723 | Illegal_Indexing | |
3724 | ("indexing function already inherited " | |
3725 | & "from parent type"); | |
7796365f | 3726 | return; |
05987af3 | 3727 | end if; |
05987af3 | 3728 | end if; |
3729 | end if; | |
3730 | ||
e81df51c | 3731 | if not Check_Primitive_Function (Subp) then |
05987af3 | 3732 | Illegal_Indexing |
3733 | ("Indexing aspect requires a function that applies to type&"); | |
3734 | return; | |
81b424ac | 3735 | end if; |
3736 | ||
7796365f | 3737 | -- If partial declaration exists, verify that it is not tagged. |
3738 | ||
3739 | if Ekind (Current_Scope) = E_Package | |
3740 | and then Has_Private_Declaration (Ent) | |
3741 | and then From_Aspect_Specification (N) | |
7c0c95b8 | 3742 | and then |
3743 | List_Containing (Parent (Ent)) = | |
3744 | Private_Declarations | |
7796365f | 3745 | (Specification (Unit_Declaration_Node (Current_Scope))) |
3746 | and then Nkind (N) = N_Attribute_Definition_Clause | |
3747 | then | |
3748 | declare | |
3749 | Decl : Node_Id; | |
3750 | ||
3751 | begin | |
3752 | Decl := | |
3753 | First (Visible_Declarations | |
7c0c95b8 | 3754 | (Specification |
3755 | (Unit_Declaration_Node (Current_Scope)))); | |
7796365f | 3756 | |
3757 | while Present (Decl) loop | |
3758 | if Nkind (Decl) = N_Private_Type_Declaration | |
3759 | and then Ent = Full_View (Defining_Identifier (Decl)) | |
3760 | and then Tagged_Present (Decl) | |
3761 | and then No (Aspect_Specifications (Decl)) | |
3762 | then | |
3763 | Illegal_Indexing | |
3764 | ("Indexing aspect cannot be specified on full view " | |
7c0c95b8 | 3765 | & "if partial view is tagged"); |
7796365f | 3766 | return; |
3767 | end if; | |
3768 | ||
3769 | Next (Decl); | |
3770 | end loop; | |
3771 | end; | |
3772 | end if; | |
3773 | ||
1b7510f9 | 3774 | -- An indexing function must return either the default element of |
cac18f71 | 3775 | -- the container, or a reference type. For variable indexing it |
a45d946f | 3776 | -- must be the latter. |
1b7510f9 | 3777 | |
05987af3 | 3778 | Default_Element := |
3779 | Find_Value_Of_Aspect | |
3780 | (Etype (First_Formal (Subp)), Aspect_Iterator_Element); | |
3781 | ||
1b7510f9 | 3782 | if Present (Default_Element) then |
3783 | Analyze (Default_Element); | |
a45d946f | 3784 | |
1b7510f9 | 3785 | if Is_Entity_Name (Default_Element) |
05987af3 | 3786 | and then not Covers (Entity (Default_Element), Ret_Type) |
3787 | and then False | |
1b7510f9 | 3788 | then |
05987af3 | 3789 | Illegal_Indexing |
3790 | ("wrong return type for indexing function"); | |
1b7510f9 | 3791 | return; |
3792 | end if; | |
3793 | end if; | |
3794 | ||
a45d946f | 3795 | -- For variable_indexing the return type must be a reference type |
1b7510f9 | 3796 | |
05987af3 | 3797 | if Attr = Name_Variable_Indexing then |
3798 | if not Has_Implicit_Dereference (Ret_Type) then | |
3799 | Illegal_Indexing | |
3800 | ("variable indexing must return a reference type"); | |
3801 | return; | |
3802 | ||
423b89fd | 3803 | elsif Is_Access_Constant |
3804 | (Etype (First_Discriminant (Ret_Type))) | |
05987af3 | 3805 | then |
3806 | Illegal_Indexing | |
3807 | ("variable indexing must return an access to variable"); | |
3808 | return; | |
3809 | end if; | |
cac18f71 | 3810 | |
3811 | else | |
05987af3 | 3812 | if Has_Implicit_Dereference (Ret_Type) |
3813 | and then not | |
3814 | Is_Access_Constant (Etype (First_Discriminant (Ret_Type))) | |
3815 | then | |
3816 | Illegal_Indexing | |
3817 | ("constant indexing must return an access to constant"); | |
3818 | return; | |
3819 | ||
3820 | elsif Is_Access_Type (Etype (First_Formal (Subp))) | |
3821 | and then not Is_Access_Constant (Etype (First_Formal (Subp))) | |
3822 | then | |
3823 | Illegal_Indexing | |
3824 | ("constant indexing must apply to an access to constant"); | |
3825 | return; | |
3826 | end if; | |
81b424ac | 3827 | end if; |
05987af3 | 3828 | |
3829 | -- All checks succeeded. | |
3830 | ||
3831 | Indexing_Found := True; | |
81b424ac | 3832 | end Check_One_Function; |
3833 | ||
05987af3 | 3834 | ----------------------- |
3835 | -- Illegal_Indexing -- | |
3836 | ----------------------- | |
3837 | ||
3838 | procedure Illegal_Indexing (Msg : String) is | |
3839 | begin | |
7796365f | 3840 | Error_Msg_NE (Msg, N, Ent); |
05987af3 | 3841 | end Illegal_Indexing; |
3842 | ||
81b424ac | 3843 | -- Start of processing for Check_Indexing_Functions |
3844 | ||
3845 | begin | |
89cc7147 | 3846 | if In_Instance then |
3847 | return; | |
3848 | end if; | |
3849 | ||
81b424ac | 3850 | Analyze (Expr); |
3851 | ||
3852 | if not Is_Overloaded (Expr) then | |
3853 | Check_One_Function (Entity (Expr)); | |
3854 | ||
3855 | else | |
3856 | declare | |
2c5754de | 3857 | I : Interp_Index; |
81b424ac | 3858 | It : Interp; |
3859 | ||
3860 | begin | |
cac18f71 | 3861 | Indexing_Found := False; |
81b424ac | 3862 | Get_First_Interp (Expr, I, It); |
3863 | while Present (It.Nam) loop | |
3864 | ||
3865 | -- Note that analysis will have added the interpretation | |
3866 | -- that corresponds to the dereference. We only check the | |
3867 | -- subprogram itself. | |
3868 | ||
3869 | if Is_Overloadable (It.Nam) then | |
3870 | Check_One_Function (It.Nam); | |
3871 | end if; | |
3872 | ||
3873 | Get_Next_Interp (I, It); | |
3874 | end loop; | |
3875 | end; | |
3876 | end if; | |
7796365f | 3877 | |
7c0c95b8 | 3878 | if not Indexing_Found and then not Error_Posted (N) then |
7796365f | 3879 | Error_Msg_NE |
3880 | ("aspect Indexing requires a local function that " | |
3881 | & "applies to type&", Expr, Ent); | |
3882 | end if; | |
81b424ac | 3883 | end Check_Indexing_Functions; |
3884 | ||
89cc7147 | 3885 | ------------------------------ |
3886 | -- Check_Iterator_Functions -- | |
3887 | ------------------------------ | |
3888 | ||
3889 | procedure Check_Iterator_Functions is | |
3890 | Default : Entity_Id; | |
3891 | ||
3892 | function Valid_Default_Iterator (Subp : Entity_Id) return Boolean; | |
8df4f2a5 | 3893 | -- Check one possible interpretation for validity |
89cc7147 | 3894 | |
3895 | ---------------------------- | |
3896 | -- Valid_Default_Iterator -- | |
3897 | ---------------------------- | |
3898 | ||
3899 | function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is | |
3900 | Formal : Entity_Id; | |
3901 | ||
3902 | begin | |
3903 | if not Check_Primitive_Function (Subp) then | |
3904 | return False; | |
3905 | else | |
3906 | Formal := First_Formal (Subp); | |
3907 | end if; | |
3908 | ||
8df4f2a5 | 3909 | -- False if any subsequent formal has no default expression |
89cc7147 | 3910 | |
8df4f2a5 | 3911 | Formal := Next_Formal (Formal); |
3912 | while Present (Formal) loop | |
3913 | if No (Expression (Parent (Formal))) then | |
3914 | return False; | |
3915 | end if; | |
89cc7147 | 3916 | |
8df4f2a5 | 3917 | Next_Formal (Formal); |
3918 | end loop; | |
89cc7147 | 3919 | |
8df4f2a5 | 3920 | -- True if all subsequent formals have default expressions |
89cc7147 | 3921 | |
3922 | return True; | |
3923 | end Valid_Default_Iterator; | |
3924 | ||
3925 | -- Start of processing for Check_Iterator_Functions | |
3926 | ||
3927 | begin | |
3928 | Analyze (Expr); | |
3929 | ||
3930 | if not Is_Entity_Name (Expr) then | |
3931 | Error_Msg_N ("aspect Iterator must be a function name", Expr); | |
3932 | end if; | |
3933 | ||
3934 | if not Is_Overloaded (Expr) then | |
3935 | if not Check_Primitive_Function (Entity (Expr)) then | |
3936 | Error_Msg_NE | |
3937 | ("aspect Indexing requires a function that applies to type&", | |
3938 | Entity (Expr), Ent); | |
3939 | end if; | |
3940 | ||
3941 | if not Valid_Default_Iterator (Entity (Expr)) then | |
3942 | Error_Msg_N ("improper function for default iterator", Expr); | |
3943 | end if; | |
3944 | ||
3945 | else | |
3946 | Default := Empty; | |
3947 | declare | |
3948 | I : Interp_Index; | |
3949 | It : Interp; | |
3950 | ||
3951 | begin | |
3952 | Get_First_Interp (Expr, I, It); | |
3953 | while Present (It.Nam) loop | |
3954 | if not Check_Primitive_Function (It.Nam) | |
59f3e675 | 3955 | or else not Valid_Default_Iterator (It.Nam) |
89cc7147 | 3956 | then |
3957 | Remove_Interp (I); | |
3958 | ||
3959 | elsif Present (Default) then | |
3960 | Error_Msg_N ("default iterator must be unique", Expr); | |
3961 | ||
3962 | else | |
3963 | Default := It.Nam; | |
3964 | end if; | |
3965 | ||
3966 | Get_Next_Interp (I, It); | |
3967 | end loop; | |
3968 | end; | |
3969 | ||
3970 | if Present (Default) then | |
3971 | Set_Entity (Expr, Default); | |
3972 | Set_Is_Overloaded (Expr, False); | |
3973 | end if; | |
3974 | end if; | |
3975 | end Check_Iterator_Functions; | |
3976 | ||
3977 | ------------------------------- | |
3978 | -- Check_Primitive_Function -- | |
3979 | ------------------------------- | |
3980 | ||
3981 | function Check_Primitive_Function (Subp : Entity_Id) return Boolean is | |
3982 | Ctrl : Entity_Id; | |
3983 | ||
3984 | begin | |
3985 | if Ekind (Subp) /= E_Function then | |
3986 | return False; | |
3987 | end if; | |
3988 | ||
3989 | if No (First_Formal (Subp)) then | |
3990 | return False; | |
3991 | else | |
3992 | Ctrl := Etype (First_Formal (Subp)); | |
3993 | end if; | |
3994 | ||
7d6fb253 | 3995 | -- Type of formal may be the class-wide type, an access to such, |
3996 | -- or an incomplete view. | |
3997 | ||
89cc7147 | 3998 | if Ctrl = Ent |
3999 | or else Ctrl = Class_Wide_Type (Ent) | |
4000 | or else | |
4001 | (Ekind (Ctrl) = E_Anonymous_Access_Type | |
b85d62ec | 4002 | and then (Designated_Type (Ctrl) = Ent |
4003 | or else | |
4004 | Designated_Type (Ctrl) = Class_Wide_Type (Ent))) | |
7d6fb253 | 4005 | or else |
4006 | (Ekind (Ctrl) = E_Incomplete_Type | |
4007 | and then Full_View (Ctrl) = Ent) | |
89cc7147 | 4008 | then |
4009 | null; | |
89cc7147 | 4010 | else |
4011 | return False; | |
4012 | end if; | |
4013 | ||
4014 | return True; | |
4015 | end Check_Primitive_Function; | |
4016 | ||
ae888dbd | 4017 | ---------------------- |
4018 | -- Duplicate_Clause -- | |
4019 | ---------------------- | |
4020 | ||
4021 | function Duplicate_Clause return Boolean is | |
d74fc39a | 4022 | A : Node_Id; |
ae888dbd | 4023 | |
4024 | begin | |
c8969ba6 | 4025 | -- Nothing to do if this attribute definition clause comes from |
4026 | -- an aspect specification, since we could not be duplicating an | |
ae888dbd | 4027 | -- explicit clause, and we dealt with the case of duplicated aspects |
4028 | -- in Analyze_Aspect_Specifications. | |
4029 | ||
4030 | if From_Aspect_Specification (N) then | |
4031 | return False; | |
4032 | end if; | |
4033 | ||
89f1e35c | 4034 | -- Otherwise current clause may duplicate previous clause, or a |
4035 | -- previously given pragma or aspect specification for the same | |
4036 | -- aspect. | |
d74fc39a | 4037 | |
89b3b365 | 4038 | A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False); |
ae888dbd | 4039 | |
4040 | if Present (A) then | |
89f1e35c | 4041 | Error_Msg_Name_1 := Chars (N); |
4042 | Error_Msg_Sloc := Sloc (A); | |
4043 | ||
89b3b365 | 4044 | Error_Msg_NE ("aspect% for & previously given#", N, U_Ent); |
89f1e35c | 4045 | return True; |
ae888dbd | 4046 | end if; |
4047 | ||
4048 | return False; | |
4049 | end Duplicate_Clause; | |
4050 | ||
9f373bb8 | 4051 | -- Start of processing for Analyze_Attribute_Definition_Clause |
4052 | ||
d6f39728 | 4053 | begin |
d64221a7 | 4054 | -- The following code is a defense against recursion. Not clear that |
51fa2a45 | 4055 | -- this can happen legitimately, but perhaps some error situations can |
4056 | -- cause it, and we did see this recursion during testing. | |
d64221a7 | 4057 | |
4058 | if Analyzed (N) then | |
4059 | return; | |
4060 | else | |
4061 | Set_Analyzed (N, True); | |
4062 | end if; | |
4063 | ||
a29bc1d9 | 4064 | -- Ignore some selected attributes in CodePeer mode since they are not |
4065 | -- relevant in this context. | |
4066 | ||
4067 | if CodePeer_Mode then | |
4068 | case Id is | |
4069 | ||
4070 | -- Ignore Component_Size in CodePeer mode, to avoid changing the | |
4071 | -- internal representation of types by implicitly packing them. | |
4072 | ||
4073 | when Attribute_Component_Size => | |
4074 | Rewrite (N, Make_Null_Statement (Sloc (N))); | |
4075 | return; | |
4076 | ||
4077 | when others => | |
4078 | null; | |
4079 | end case; | |
4080 | end if; | |
4081 | ||
d8ba53a8 | 4082 | -- Process Ignore_Rep_Clauses option |
eef1ca1e | 4083 | |
d8ba53a8 | 4084 | if Ignore_Rep_Clauses then |
9d627c41 | 4085 | case Id is |
4086 | ||
eef1ca1e | 4087 | -- The following should be ignored. They do not affect legality |
4088 | -- and may be target dependent. The basic idea of -gnatI is to | |
4089 | -- ignore any rep clauses that may be target dependent but do not | |
4090 | -- affect legality (except possibly to be rejected because they | |
4091 | -- are incompatible with the compilation target). | |
9d627c41 | 4092 | |
2f1aac99 | 4093 | when Attribute_Alignment | |
9d627c41 | 4094 | Attribute_Bit_Order | |
4095 | Attribute_Component_Size | | |
4096 | Attribute_Machine_Radix | | |
4097 | Attribute_Object_Size | | |
4098 | Attribute_Size | | |
2ff55065 | 4099 | Attribute_Small | |
9d627c41 | 4100 | Attribute_Stream_Size | |
4101 | Attribute_Value_Size => | |
2ff55065 | 4102 | Kill_Rep_Clause (N); |
9d627c41 | 4103 | return; |
4104 | ||
eef1ca1e | 4105 | -- The following should not be ignored, because in the first place |
51fa2a45 | 4106 | -- they are reasonably portable, and should not cause problems |
4107 | -- in compiling code from another target, and also they do affect | |
4108 | -- legality, e.g. failing to provide a stream attribute for a type | |
4109 | -- may make a program illegal. | |
9d627c41 | 4110 | |
b55f7641 | 4111 | when Attribute_External_Tag | |
4112 | Attribute_Input | | |
4113 | Attribute_Output | | |
4114 | Attribute_Read | | |
4115 | Attribute_Simple_Storage_Pool | | |
4116 | Attribute_Storage_Pool | | |
4117 | Attribute_Storage_Size | | |
4118 | Attribute_Write => | |
9d627c41 | 4119 | null; |
4120 | ||
2ff55065 | 4121 | -- We do not do anything here with address clauses, they will be |
4122 | -- removed by Freeze later on, but for now, it works better to | |
4123 | -- keep then in the tree. | |
4124 | ||
4125 | when Attribute_Address => | |
4126 | null; | |
4127 | ||
b593a52c | 4128 | -- Other cases are errors ("attribute& cannot be set with |
4129 | -- definition clause"), which will be caught below. | |
9d627c41 | 4130 | |
4131 | when others => | |
4132 | null; | |
4133 | end case; | |
fbc67f84 | 4134 | end if; |
4135 | ||
d6f39728 | 4136 | Analyze (Nam); |
4137 | Ent := Entity (Nam); | |
4138 | ||
4139 | if Rep_Item_Too_Early (Ent, N) then | |
4140 | return; | |
4141 | end if; | |
4142 | ||
9f373bb8 | 4143 | -- Rep clause applies to full view of incomplete type or private type if |
4144 | -- we have one (if not, this is a premature use of the type). However, | |
4145 | -- certain semantic checks need to be done on the specified entity (i.e. | |
4146 | -- the private view), so we save it in Ent. | |
d6f39728 | 4147 | |
4148 | if Is_Private_Type (Ent) | |
4149 | and then Is_Derived_Type (Ent) | |
4150 | and then not Is_Tagged_Type (Ent) | |
4151 | and then No (Full_View (Ent)) | |
4152 | then | |
9f373bb8 | 4153 | -- If this is a private type whose completion is a derivation from |
4154 | -- another private type, there is no full view, and the attribute | |
4155 | -- belongs to the type itself, not its underlying parent. | |
d6f39728 | 4156 | |
4157 | U_Ent := Ent; | |
4158 | ||
4159 | elsif Ekind (Ent) = E_Incomplete_Type then | |
d5b349fa | 4160 | |
9f373bb8 | 4161 | -- The attribute applies to the full view, set the entity of the |
4162 | -- attribute definition accordingly. | |
d5b349fa | 4163 | |
d6f39728 | 4164 | Ent := Underlying_Type (Ent); |
4165 | U_Ent := Ent; | |
d5b349fa | 4166 | Set_Entity (Nam, Ent); |
4167 | ||
d6f39728 | 4168 | else |
4169 | U_Ent := Underlying_Type (Ent); | |
4170 | end if; | |
4171 | ||
44705307 | 4172 | -- Avoid cascaded error |
d6f39728 | 4173 | |
4174 | if Etype (Nam) = Any_Type then | |
4175 | return; | |
4176 | ||
89f1e35c | 4177 | -- Must be declared in current scope or in case of an aspect |
ace3389d | 4178 | -- specification, must be visible in current scope. |
44705307 | 4179 | |
89f1e35c | 4180 | elsif Scope (Ent) /= Current_Scope |
ace3389d | 4181 | and then |
4182 | not (From_Aspect_Specification (N) | |
4183 | and then Scope_Within_Or_Same (Current_Scope, Scope (Ent))) | |
89f1e35c | 4184 | then |
d6f39728 | 4185 | Error_Msg_N ("entity must be declared in this scope", Nam); |
4186 | return; | |
4187 | ||
44705307 | 4188 | -- Must not be a source renaming (we do have some cases where the |
4189 | -- expander generates a renaming, and those cases are OK, in such | |
a3248fc4 | 4190 | -- cases any attribute applies to the renamed object as well). |
44705307 | 4191 | |
4192 | elsif Is_Object (Ent) | |
4193 | and then Present (Renamed_Object (Ent)) | |
44705307 | 4194 | then |
a3248fc4 | 4195 | -- Case of renamed object from source, this is an error |
4196 | ||
4197 | if Comes_From_Source (Renamed_Object (Ent)) then | |
4198 | Get_Name_String (Chars (N)); | |
4199 | Error_Msg_Strlen := Name_Len; | |
4200 | Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len); | |
4201 | Error_Msg_N | |
4202 | ("~ clause not allowed for a renaming declaration " | |
4203 | & "(RM 13.1(6))", Nam); | |
4204 | return; | |
4205 | ||
4206 | -- For the case of a compiler generated renaming, the attribute | |
4207 | -- definition clause applies to the renamed object created by the | |
4208 | -- expander. The easiest general way to handle this is to create a | |
4209 | -- copy of the attribute definition clause for this object. | |
4210 | ||
9a48fc56 | 4211 | elsif Is_Entity_Name (Renamed_Object (Ent)) then |
a3248fc4 | 4212 | Insert_Action (N, |
4213 | Make_Attribute_Definition_Clause (Loc, | |
4214 | Name => | |
4215 | New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc), | |
4216 | Chars => Chars (N), | |
4217 | Expression => Duplicate_Subexpr (Expression (N)))); | |
9a48fc56 | 4218 | |
4219 | -- If the renamed object is not an entity, it must be a dereference | |
4220 | -- of an unconstrained function call, and we must introduce a new | |
4221 | -- declaration to capture the expression. This is needed in the case | |
4222 | -- of 'Alignment, where the original declaration must be rewritten. | |
4223 | ||
4224 | else | |
4225 | pragma Assert | |
4226 | (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference); | |
4227 | null; | |
a3248fc4 | 4228 | end if; |
44705307 | 4229 | |
4230 | -- If no underlying entity, use entity itself, applies to some | |
4231 | -- previously detected error cases ??? | |
4232 | ||
f15731c4 | 4233 | elsif No (U_Ent) then |
4234 | U_Ent := Ent; | |
4235 | ||
44705307 | 4236 | -- Cannot specify for a subtype (exception Object/Value_Size) |
4237 | ||
d6f39728 | 4238 | elsif Is_Type (U_Ent) |
4239 | and then not Is_First_Subtype (U_Ent) | |
4240 | and then Id /= Attribute_Object_Size | |
4241 | and then Id /= Attribute_Value_Size | |
4242 | and then not From_At_Mod (N) | |
4243 | then | |
4244 | Error_Msg_N ("cannot specify attribute for subtype", Nam); | |
4245 | return; | |
d6f39728 | 4246 | end if; |
4247 | ||
ae888dbd | 4248 | Set_Entity (N, U_Ent); |
25e23a77 | 4249 | Check_Restriction_No_Use_Of_Attribute (N); |
ae888dbd | 4250 | |
d6f39728 | 4251 | -- Switch on particular attribute |
4252 | ||
4253 | case Id is | |
4254 | ||
4255 | ------------- | |
4256 | -- Address -- | |
4257 | ------------- | |
4258 | ||
4259 | -- Address attribute definition clause | |
4260 | ||
4261 | when Attribute_Address => Address : begin | |
177675a7 | 4262 | |
4263 | -- A little error check, catch for X'Address use X'Address; | |
4264 | ||
4265 | if Nkind (Nam) = N_Identifier | |
4266 | and then Nkind (Expr) = N_Attribute_Reference | |
4267 | and then Attribute_Name (Expr) = Name_Address | |
4268 | and then Nkind (Prefix (Expr)) = N_Identifier | |
4269 | and then Chars (Nam) = Chars (Prefix (Expr)) | |
4270 | then | |
4271 | Error_Msg_NE | |
4272 | ("address for & is self-referencing", Prefix (Expr), Ent); | |
4273 | return; | |
4274 | end if; | |
4275 | ||
4276 | -- Not that special case, carry on with analysis of expression | |
4277 | ||
d6f39728 | 4278 | Analyze_And_Resolve (Expr, RTE (RE_Address)); |
4279 | ||
2f1aac99 | 4280 | -- Even when ignoring rep clauses we need to indicate that the |
4281 | -- entity has an address clause and thus it is legal to declare | |
2ff55065 | 4282 | -- it imported. Freeze will get rid of the address clause later. |
2f1aac99 | 4283 | |
4284 | if Ignore_Rep_Clauses then | |
d3ef794c | 4285 | if Ekind_In (U_Ent, E_Variable, E_Constant) then |
2f1aac99 | 4286 | Record_Rep_Item (U_Ent, N); |
4287 | end if; | |
4288 | ||
4289 | return; | |
4290 | end if; | |
4291 | ||
ae888dbd | 4292 | if Duplicate_Clause then |
4293 | null; | |
d6f39728 | 4294 | |
4295 | -- Case of address clause for subprogram | |
4296 | ||
4297 | elsif Is_Subprogram (U_Ent) then | |
d6f39728 | 4298 | if Has_Homonym (U_Ent) then |
4299 | Error_Msg_N | |
4300 | ("address clause cannot be given " & | |
4301 | "for overloaded subprogram", | |
4302 | Nam); | |
83f8f0a6 | 4303 | return; |
d6f39728 | 4304 | end if; |
4305 | ||
83f8f0a6 | 4306 | -- For subprograms, all address clauses are permitted, and we |
4307 | -- mark the subprogram as having a deferred freeze so that Gigi | |
4308 | -- will not elaborate it too soon. | |
d6f39728 | 4309 | |
4310 | -- Above needs more comments, what is too soon about??? | |
4311 | ||
4312 | Set_Has_Delayed_Freeze (U_Ent); | |
4313 | ||
4314 | -- Case of address clause for entry | |
4315 | ||
4316 | elsif Ekind (U_Ent) = E_Entry then | |
d6f39728 | 4317 | if Nkind (Parent (N)) = N_Task_Body then |
4318 | Error_Msg_N | |
4319 | ("entry address must be specified in task spec", Nam); | |
83f8f0a6 | 4320 | return; |
d6f39728 | 4321 | end if; |
4322 | ||
4323 | -- For entries, we require a constant address | |
4324 | ||
4325 | Check_Constant_Address_Clause (Expr, U_Ent); | |
4326 | ||
83f8f0a6 | 4327 | -- Special checks for task types |
4328 | ||
f15731c4 | 4329 | if Is_Task_Type (Scope (U_Ent)) |
4330 | and then Comes_From_Source (Scope (U_Ent)) | |
4331 | then | |
4332 | Error_Msg_N | |
1e3532e7 | 4333 | ("??entry address declared for entry in task type", N); |
f15731c4 | 4334 | Error_Msg_N |
1e3532e7 | 4335 | ("\??only one task can be declared of this type", N); |
f15731c4 | 4336 | end if; |
4337 | ||
83f8f0a6 | 4338 | -- Entry address clauses are obsolescent |
4339 | ||
e0521a36 | 4340 | Check_Restriction (No_Obsolescent_Features, N); |
4341 | ||
9dfe12ae | 4342 | if Warn_On_Obsolescent_Feature then |
4343 | Error_Msg_N | |
1e3532e7 | 4344 | ("?j?attaching interrupt to task entry is an " & |
4345 | "obsolescent feature (RM J.7.1)", N); | |
9dfe12ae | 4346 | Error_Msg_N |
1e3532e7 | 4347 | ("\?j?use interrupt procedure instead", N); |
9dfe12ae | 4348 | end if; |
4349 | ||
83f8f0a6 | 4350 | -- Case of an address clause for a controlled object which we |
4351 | -- consider to be erroneous. | |
9dfe12ae | 4352 | |
83f8f0a6 | 4353 | elsif Is_Controlled (Etype (U_Ent)) |
4354 | or else Has_Controlled_Component (Etype (U_Ent)) | |
4355 | then | |
9dfe12ae | 4356 | Error_Msg_NE |
1e3532e7 | 4357 | ("??controlled object& must not be overlaid", Nam, U_Ent); |
9dfe12ae | 4358 | Error_Msg_N |
1e3532e7 | 4359 | ("\??Program_Error will be raised at run time", Nam); |
9dfe12ae | 4360 | Insert_Action (Declaration_Node (U_Ent), |
4361 | Make_Raise_Program_Error (Loc, | |
4362 | Reason => PE_Overlaid_Controlled_Object)); | |
83f8f0a6 | 4363 | return; |
9dfe12ae | 4364 | |
4365 | -- Case of address clause for a (non-controlled) object | |
d6f39728 | 4366 | |
f02a9a9a | 4367 | elsif Ekind_In (U_Ent, E_Variable, E_Constant) then |
d6f39728 | 4368 | declare |
d6da7448 | 4369 | Expr : constant Node_Id := Expression (N); |
4370 | O_Ent : Entity_Id; | |
4371 | Off : Boolean; | |
d6f39728 | 4372 | |
4373 | begin | |
7ee315cc | 4374 | -- Exported variables cannot have an address clause, because |
4375 | -- this cancels the effect of the pragma Export. | |
d6f39728 | 4376 | |
4377 | if Is_Exported (U_Ent) then | |
4378 | Error_Msg_N | |
4379 | ("cannot export object with address clause", Nam); | |
83f8f0a6 | 4380 | return; |
d6da7448 | 4381 | end if; |
4382 | ||
4383 | Find_Overlaid_Entity (N, O_Ent, Off); | |
d6f39728 | 4384 | |
9dfe12ae | 4385 | -- Overlaying controlled objects is erroneous |
4386 | ||
d6da7448 | 4387 | if Present (O_Ent) |
4388 | and then (Has_Controlled_Component (Etype (O_Ent)) | |
f02a9a9a | 4389 | or else Is_Controlled (Etype (O_Ent))) |
9dfe12ae | 4390 | then |
4391 | Error_Msg_N | |
1e3532e7 | 4392 | ("??cannot overlay with controlled object", Expr); |
9dfe12ae | 4393 | Error_Msg_N |
1e3532e7 | 4394 | ("\??Program_Error will be raised at run time", Expr); |
9dfe12ae | 4395 | Insert_Action (Declaration_Node (U_Ent), |
4396 | Make_Raise_Program_Error (Loc, | |
4397 | Reason => PE_Overlaid_Controlled_Object)); | |
83f8f0a6 | 4398 | return; |
9dfe12ae | 4399 | |
d6da7448 | 4400 | elsif Present (O_Ent) |
9dfe12ae | 4401 | and then Ekind (U_Ent) = E_Constant |
d6da7448 | 4402 | and then not Is_Constant_Object (O_Ent) |
9dfe12ae | 4403 | then |
1e3532e7 | 4404 | Error_Msg_N ("??constant overlays a variable", Expr); |
9dfe12ae | 4405 | |
d6f39728 | 4406 | -- Imported variables can have an address clause, but then |
4407 | -- the import is pretty meaningless except to suppress | |
4408 | -- initializations, so we do not need such variables to | |
4409 | -- be statically allocated (and in fact it causes trouble | |
4410 | -- if the address clause is a local value). | |
4411 | ||
4412 | elsif Is_Imported (U_Ent) then | |
4413 | Set_Is_Statically_Allocated (U_Ent, False); | |
4414 | end if; | |
4415 | ||
4416 | -- We mark a possible modification of a variable with an | |
4417 | -- address clause, since it is likely aliasing is occurring. | |
4418 | ||
177675a7 | 4419 | Note_Possible_Modification (Nam, Sure => False); |
d6f39728 | 4420 | |
83f8f0a6 | 4421 | -- Here we are checking for explicit overlap of one variable |
4422 | -- by another, and if we find this then mark the overlapped | |
4423 | -- variable as also being volatile to prevent unwanted | |
d6da7448 | 4424 | -- optimizations. This is a significant pessimization so |
4425 | -- avoid it when there is an offset, i.e. when the object | |
4426 | -- is composite; they cannot be optimized easily anyway. | |
d6f39728 | 4427 | |
d6da7448 | 4428 | if Present (O_Ent) |
4429 | and then Is_Object (O_Ent) | |
4430 | and then not Off | |
ba5efa21 | 4431 | |
4432 | -- The following test is an expedient solution to what | |
4433 | -- is really a problem in CodePeer. Suppressing the | |
4434 | -- Set_Treat_As_Volatile call here prevents later | |
4435 | -- generation (in some cases) of trees that CodePeer | |
4436 | -- should, but currently does not, handle correctly. | |
4437 | -- This test should probably be removed when CodePeer | |
4438 | -- is improved, just because we want the tree CodePeer | |
4439 | -- analyzes to match the tree for which we generate code | |
4440 | -- as closely as is practical. ??? | |
4441 | ||
4442 | and then not CodePeer_Mode | |
d6da7448 | 4443 | then |
ba5efa21 | 4444 | -- ??? O_Ent might not be in current unit |
4445 | ||
d6da7448 | 4446 | Set_Treat_As_Volatile (O_Ent); |
d6f39728 | 4447 | end if; |
4448 | ||
9dfe12ae | 4449 | -- Legality checks on the address clause for initialized |
4450 | -- objects is deferred until the freeze point, because | |
2beb22b1 | 4451 | -- a subsequent pragma might indicate that the object |
42e09e36 | 4452 | -- is imported and thus not initialized. Also, the address |
4453 | -- clause might involve entities that have yet to be | |
4454 | -- elaborated. | |
9dfe12ae | 4455 | |
4456 | Set_Has_Delayed_Freeze (U_Ent); | |
4457 | ||
51ad5ad2 | 4458 | -- If an initialization call has been generated for this |
4459 | -- object, it needs to be deferred to after the freeze node | |
4460 | -- we have just now added, otherwise GIGI will see a | |
4461 | -- reference to the variable (as actual to the IP call) | |
4462 | -- before its definition. | |
4463 | ||
4464 | declare | |
df9fba45 | 4465 | Init_Call : constant Node_Id := |
4466 | Remove_Init_Call (U_Ent, N); | |
4bba0a8d | 4467 | |
51ad5ad2 | 4468 | begin |
4469 | if Present (Init_Call) then | |
28a4283c | 4470 | Append_Freeze_Action (U_Ent, Init_Call); |
df9fba45 | 4471 | |
28a4283c | 4472 | -- Reset Initialization_Statements pointer so that |
4473 | -- if there is a pragma Import further down, it can | |
4474 | -- clear any default initialization. | |
df9fba45 | 4475 | |
28a4283c | 4476 | Set_Initialization_Statements (U_Ent, Init_Call); |
51ad5ad2 | 4477 | end if; |
4478 | end; | |
4479 | ||
d6f39728 | 4480 | if Is_Exported (U_Ent) then |
4481 | Error_Msg_N | |
4482 | ("& cannot be exported if an address clause is given", | |
4483 | Nam); | |
4484 | Error_Msg_N | |
4bba0a8d | 4485 | ("\define and export a variable " |
4486 | & "that holds its address instead", Nam); | |
d6f39728 | 4487 | end if; |
4488 | ||
44e4341e | 4489 | -- Entity has delayed freeze, so we will generate an |
4490 | -- alignment check at the freeze point unless suppressed. | |
d6f39728 | 4491 | |
44e4341e | 4492 | if not Range_Checks_Suppressed (U_Ent) |
4493 | and then not Alignment_Checks_Suppressed (U_Ent) | |
4494 | then | |
4495 | Set_Check_Address_Alignment (N); | |
4496 | end if; | |
d6f39728 | 4497 | |
4498 | -- Kill the size check code, since we are not allocating | |
4499 | -- the variable, it is somewhere else. | |
4500 | ||
4501 | Kill_Size_Check_Code (U_Ent); | |
83f8f0a6 | 4502 | |
d6da7448 | 4503 | -- If the address clause is of the form: |
83f8f0a6 | 4504 | |
d6da7448 | 4505 | -- for Y'Address use X'Address |
83f8f0a6 | 4506 | |
d6da7448 | 4507 | -- or |
83f8f0a6 | 4508 | |
d6da7448 | 4509 | -- Const : constant Address := X'Address; |
4510 | -- ... | |
4511 | -- for Y'Address use Const; | |
83f8f0a6 | 4512 | |
d6da7448 | 4513 | -- then we make an entry in the table for checking the size |
4514 | -- and alignment of the overlaying variable. We defer this | |
4515 | -- check till after code generation to take full advantage | |
f4623c89 | 4516 | -- of the annotation done by the back end. |
d64221a7 | 4517 | |
9474aa9c | 4518 | -- If the entity has a generic type, the check will be |
43dd6937 | 4519 | -- performed in the instance if the actual type justifies |
4520 | -- it, and we do not insert the clause in the table to | |
4521 | -- prevent spurious warnings. | |
83f8f0a6 | 4522 | |
f4623c89 | 4523 | -- Note: we used to test Comes_From_Source and only give |
4524 | -- this warning for source entities, but we have removed | |
4525 | -- this test. It really seems bogus to generate overlays | |
4526 | -- that would trigger this warning in generated code. | |
4527 | -- Furthermore, by removing the test, we handle the | |
4528 | -- aspect case properly. | |
4529 | ||
d6da7448 | 4530 | if Address_Clause_Overlay_Warnings |
d6da7448 | 4531 | and then Present (O_Ent) |
4532 | and then Is_Object (O_Ent) | |
4533 | then | |
9474aa9c | 4534 | if not Is_Generic_Type (Etype (U_Ent)) then |
4535 | Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off)); | |
4536 | end if; | |
177675a7 | 4537 | |
d6da7448 | 4538 | -- If variable overlays a constant view, and we are |
4539 | -- warning on overlays, then mark the variable as | |
4540 | -- overlaying a constant (we will give warnings later | |
4541 | -- if this variable is assigned). | |
177675a7 | 4542 | |
d6da7448 | 4543 | if Is_Constant_Object (O_Ent) |
4544 | and then Ekind (U_Ent) = E_Variable | |
4545 | then | |
4546 | Set_Overlays_Constant (U_Ent); | |
83f8f0a6 | 4547 | end if; |
d6da7448 | 4548 | end if; |
4549 | end; | |
83f8f0a6 | 4550 | |
d6f39728 | 4551 | -- Not a valid entity for an address clause |
4552 | ||
4553 | else | |
4554 | Error_Msg_N ("address cannot be given for &", Nam); | |
4555 | end if; | |
4556 | end Address; | |
4557 | ||
4558 | --------------- | |
4559 | -- Alignment -- | |
4560 | --------------- | |
4561 | ||
4562 | -- Alignment attribute definition clause | |
4563 | ||
b47769f0 | 4564 | when Attribute_Alignment => Alignment : declare |
208fd589 | 4565 | Align : constant Uint := Get_Alignment_Value (Expr); |
4566 | Max_Align : constant Uint := UI_From_Int (Maximum_Alignment); | |
41331dcf | 4567 | |
d6f39728 | 4568 | begin |
4569 | FOnly := True; | |
4570 | ||
4571 | if not Is_Type (U_Ent) | |
4572 | and then Ekind (U_Ent) /= E_Variable | |
4573 | and then Ekind (U_Ent) /= E_Constant | |
4574 | then | |
4575 | Error_Msg_N ("alignment cannot be given for &", Nam); | |
4576 | ||
ae888dbd | 4577 | elsif Duplicate_Clause then |
4578 | null; | |
d6f39728 | 4579 | |
4580 | elsif Align /= No_Uint then | |
4581 | Set_Has_Alignment_Clause (U_Ent); | |
208fd589 | 4582 | |
44705307 | 4583 | -- Tagged type case, check for attempt to set alignment to a |
4584 | -- value greater than Max_Align, and reset if so. | |
4585 | ||
41331dcf | 4586 | if Is_Tagged_Type (U_Ent) and then Align > Max_Align then |
208fd589 | 4587 | Error_Msg_N |
1e3532e7 | 4588 | ("alignment for & set to Maximum_Aligment??", Nam); |
44705307 | 4589 | Set_Alignment (U_Ent, Max_Align); |
4590 | ||
4591 | -- All other cases | |
4592 | ||
208fd589 | 4593 | else |
4594 | Set_Alignment (U_Ent, Align); | |
4595 | end if; | |
b47769f0 | 4596 | |
4597 | -- For an array type, U_Ent is the first subtype. In that case, | |
4598 | -- also set the alignment of the anonymous base type so that | |
4599 | -- other subtypes (such as the itypes for aggregates of the | |
4600 | -- type) also receive the expected alignment. | |
4601 | ||
4602 | if Is_Array_Type (U_Ent) then | |
4603 | Set_Alignment (Base_Type (U_Ent), Align); | |
4604 | end if; | |
d6f39728 | 4605 | end if; |
b47769f0 | 4606 | end Alignment; |
d6f39728 | 4607 | |
4608 | --------------- | |
4609 | -- Bit_Order -- | |
4610 | --------------- | |
4611 | ||
4612 | -- Bit_Order attribute definition clause | |
4613 | ||
4614 | when Attribute_Bit_Order => Bit_Order : declare | |
4615 | begin | |
4616 | if not Is_Record_Type (U_Ent) then | |
4617 | Error_Msg_N | |
4618 | ("Bit_Order can only be defined for record type", Nam); | |
4619 | ||
ae888dbd | 4620 | elsif Duplicate_Clause then |
4621 | null; | |
4622 | ||
d6f39728 | 4623 | else |
4624 | Analyze_And_Resolve (Expr, RTE (RE_Bit_Order)); | |
4625 | ||
4626 | if Etype (Expr) = Any_Type then | |
4627 | return; | |
4628 | ||
cda40848 | 4629 | elsif not Is_OK_Static_Expression (Expr) then |
9dfe12ae | 4630 | Flag_Non_Static_Expr |
4631 | ("Bit_Order requires static expression!", Expr); | |
d6f39728 | 4632 | |
4633 | else | |
4634 | if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then | |
fae4ea1f | 4635 | Set_Reverse_Bit_Order (Base_Type (U_Ent), True); |
d6f39728 | 4636 | end if; |
4637 | end if; | |
4638 | end if; | |
4639 | end Bit_Order; | |
4640 | ||
4641 | -------------------- | |
4642 | -- Component_Size -- | |
4643 | -------------------- | |
4644 | ||
4645 | -- Component_Size attribute definition clause | |
4646 | ||
4647 | when Attribute_Component_Size => Component_Size_Case : declare | |
4648 | Csize : constant Uint := Static_Integer (Expr); | |
a0fc8c5b | 4649 | Ctyp : Entity_Id; |
d6f39728 | 4650 | Btype : Entity_Id; |
4651 | Biased : Boolean; | |
4652 | New_Ctyp : Entity_Id; | |
4653 | Decl : Node_Id; | |
4654 | ||
4655 | begin | |
4656 | if not Is_Array_Type (U_Ent) then | |
4657 | Error_Msg_N ("component size requires array type", Nam); | |
4658 | return; | |
4659 | end if; | |
4660 | ||
4661 | Btype := Base_Type (U_Ent); | |
a0fc8c5b | 4662 | Ctyp := Component_Type (Btype); |
d6f39728 | 4663 | |
ae888dbd | 4664 | if Duplicate_Clause then |
4665 | null; | |
d6f39728 | 4666 | |
f3e4db96 | 4667 | elsif Rep_Item_Too_Early (Btype, N) then |
4668 | null; | |
4669 | ||
d6f39728 | 4670 | elsif Csize /= No_Uint then |
a0fc8c5b | 4671 | Check_Size (Expr, Ctyp, Csize, Biased); |
d6f39728 | 4672 | |
d74fc39a | 4673 | -- For the biased case, build a declaration for a subtype that |
4674 | -- will be used to represent the biased subtype that reflects | |
4675 | -- the biased representation of components. We need the subtype | |
4676 | -- to get proper conversions on referencing elements of the | |
4677 | -- array. Note: component size clauses are ignored in VM mode. | |
3062c401 | 4678 | |
4679 | if VM_Target = No_VM then | |
4680 | if Biased then | |
4681 | New_Ctyp := | |
4682 | Make_Defining_Identifier (Loc, | |
4683 | Chars => | |
4684 | New_External_Name (Chars (U_Ent), 'C', 0, 'T')); | |
4685 | ||
4686 | Decl := | |
4687 | Make_Subtype_Declaration (Loc, | |
4688 | Defining_Identifier => New_Ctyp, | |
4689 | Subtype_Indication => | |
4690 | New_Occurrence_Of (Component_Type (Btype), Loc)); | |
4691 | ||
4692 | Set_Parent (Decl, N); | |
4693 | Analyze (Decl, Suppress => All_Checks); | |
4694 | ||
4695 | Set_Has_Delayed_Freeze (New_Ctyp, False); | |
4696 | Set_Esize (New_Ctyp, Csize); | |
4697 | Set_RM_Size (New_Ctyp, Csize); | |
4698 | Init_Alignment (New_Ctyp); | |
3062c401 | 4699 | Set_Is_Itype (New_Ctyp, True); |
4700 | Set_Associated_Node_For_Itype (New_Ctyp, U_Ent); | |
4701 | ||
4702 | Set_Component_Type (Btype, New_Ctyp); | |
b77e4501 | 4703 | Set_Biased (New_Ctyp, N, "component size clause"); |
3062c401 | 4704 | end if; |
4705 | ||
4706 | Set_Component_Size (Btype, Csize); | |
4707 | ||
4708 | -- For VM case, we ignore component size clauses | |
4709 | ||
4710 | else | |
4711 | -- Give a warning unless we are in GNAT mode, in which case | |
4712 | -- the warning is suppressed since it is not useful. | |
4713 | ||
4714 | if not GNAT_Mode then | |
4715 | Error_Msg_N | |
1e3532e7 | 4716 | ("component size ignored in this configuration??", N); |
3062c401 | 4717 | end if; |
d6f39728 | 4718 | end if; |
4719 | ||
a0fc8c5b | 4720 | -- Deal with warning on overridden size |
4721 | ||
4722 | if Warn_On_Overridden_Size | |
4723 | and then Has_Size_Clause (Ctyp) | |
4724 | and then RM_Size (Ctyp) /= Csize | |
4725 | then | |
4726 | Error_Msg_NE | |
1e3532e7 | 4727 | ("component size overrides size clause for&?S?", N, Ctyp); |
a0fc8c5b | 4728 | end if; |
4729 | ||
d6f39728 | 4730 | Set_Has_Component_Size_Clause (Btype, True); |
f3e4db96 | 4731 | Set_Has_Non_Standard_Rep (Btype, True); |
d6f39728 | 4732 | end if; |
4733 | end Component_Size_Case; | |
4734 | ||
81b424ac | 4735 | ----------------------- |
4736 | -- Constant_Indexing -- | |
4737 | ----------------------- | |
4738 | ||
4739 | when Attribute_Constant_Indexing => | |
4740 | Check_Indexing_Functions; | |
4741 | ||
89f1e35c | 4742 | --------- |
4743 | -- CPU -- | |
4744 | --------- | |
4745 | ||
4746 | when Attribute_CPU => CPU : | |
4747 | begin | |
4748 | -- CPU attribute definition clause not allowed except from aspect | |
4749 | -- specification. | |
4750 | ||
4751 | if From_Aspect_Specification (N) then | |
4752 | if not Is_Task_Type (U_Ent) then | |
4753 | Error_Msg_N ("CPU can only be defined for task", Nam); | |
4754 | ||
4755 | elsif Duplicate_Clause then | |
4756 | null; | |
4757 | ||
4758 | else | |
4759 | -- The expression must be analyzed in the special manner | |
4760 | -- described in "Handling of Default and Per-Object | |
4761 | -- Expressions" in sem.ads. | |
4762 | ||
4763 | -- The visibility to the discriminants must be restored | |
4764 | ||
4765 | Push_Scope_And_Install_Discriminants (U_Ent); | |
4766 | Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range)); | |
4767 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
4768 | ||
cda40848 | 4769 | if not Is_OK_Static_Expression (Expr) then |
89f1e35c | 4770 | Check_Restriction (Static_Priorities, Expr); |
4771 | end if; | |
4772 | end if; | |
4773 | ||
4774 | else | |
4775 | Error_Msg_N | |
4776 | ("attribute& cannot be set with definition clause", N); | |
4777 | end if; | |
4778 | end CPU; | |
4779 | ||
89cc7147 | 4780 | ---------------------- |
4781 | -- Default_Iterator -- | |
4782 | ---------------------- | |
4783 | ||
4784 | when Attribute_Default_Iterator => Default_Iterator : declare | |
4785 | Func : Entity_Id; | |
4786 | ||
4787 | begin | |
4788 | if not Is_Tagged_Type (U_Ent) then | |
4789 | Error_Msg_N | |
4790 | ("aspect Default_Iterator applies to tagged type", Nam); | |
4791 | end if; | |
4792 | ||
4793 | Check_Iterator_Functions; | |
4794 | ||
4795 | Analyze (Expr); | |
4796 | ||
4797 | if not Is_Entity_Name (Expr) | |
4798 | or else Ekind (Entity (Expr)) /= E_Function | |
4799 | then | |
4800 | Error_Msg_N ("aspect Iterator must be a function", Expr); | |
4801 | else | |
4802 | Func := Entity (Expr); | |
4803 | end if; | |
4804 | ||
4805 | if No (First_Formal (Func)) | |
4806 | or else Etype (First_Formal (Func)) /= U_Ent | |
4807 | then | |
4808 | Error_Msg_NE | |
4809 | ("Default Iterator must be a primitive of&", Func, U_Ent); | |
4810 | end if; | |
4811 | end Default_Iterator; | |
4812 | ||
89f1e35c | 4813 | ------------------------ |
4814 | -- Dispatching_Domain -- | |
4815 | ------------------------ | |
4816 | ||
4817 | when Attribute_Dispatching_Domain => Dispatching_Domain : | |
4818 | begin | |
4819 | -- Dispatching_Domain attribute definition clause not allowed | |
4820 | -- except from aspect specification. | |
4821 | ||
4822 | if From_Aspect_Specification (N) then | |
4823 | if not Is_Task_Type (U_Ent) then | |
4824 | Error_Msg_N ("Dispatching_Domain can only be defined" & | |
4825 | "for task", | |
4826 | Nam); | |
4827 | ||
4828 | elsif Duplicate_Clause then | |
4829 | null; | |
4830 | ||
4831 | else | |
4832 | -- The expression must be analyzed in the special manner | |
4833 | -- described in "Handling of Default and Per-Object | |
4834 | -- Expressions" in sem.ads. | |
4835 | ||
4836 | -- The visibility to the discriminants must be restored | |
4837 | ||
4838 | Push_Scope_And_Install_Discriminants (U_Ent); | |
4839 | ||
4840 | Preanalyze_Spec_Expression | |
4841 | (Expr, RTE (RE_Dispatching_Domain)); | |
4842 | ||
4843 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
4844 | end if; | |
4845 | ||
4846 | else | |
4847 | Error_Msg_N | |
4848 | ("attribute& cannot be set with definition clause", N); | |
4849 | end if; | |
4850 | end Dispatching_Domain; | |
4851 | ||
d6f39728 | 4852 | ------------------ |
4853 | -- External_Tag -- | |
4854 | ------------------ | |
4855 | ||
4856 | when Attribute_External_Tag => External_Tag : | |
4857 | begin | |
4858 | if not Is_Tagged_Type (U_Ent) then | |
4859 | Error_Msg_N ("should be a tagged type", Nam); | |
4860 | end if; | |
4861 | ||
ae888dbd | 4862 | if Duplicate_Clause then |
4863 | null; | |
d6f39728 | 4864 | |
9af0ddc7 | 4865 | else |
ae888dbd | 4866 | Analyze_And_Resolve (Expr, Standard_String); |
fbc67f84 | 4867 | |
cda40848 | 4868 | if not Is_OK_Static_Expression (Expr) then |
ae888dbd | 4869 | Flag_Non_Static_Expr |
4870 | ("static string required for tag name!", Nam); | |
4871 | end if; | |
4872 | ||
15a67a0a | 4873 | if VM_Target /= No_VM then |
ae888dbd | 4874 | Error_Msg_Name_1 := Attr; |
4875 | Error_Msg_N | |
4876 | ("% attribute unsupported in this configuration", Nam); | |
4877 | end if; | |
4878 | ||
4879 | if not Is_Library_Level_Entity (U_Ent) then | |
4880 | Error_Msg_NE | |
1e3532e7 | 4881 | ("??non-unique external tag supplied for &", N, U_Ent); |
ae888dbd | 4882 | Error_Msg_N |
1e3532e7 | 4883 | ("\??same external tag applies to all " |
4884 | & "subprogram calls", N); | |
ae888dbd | 4885 | Error_Msg_N |
1e3532e7 | 4886 | ("\??corresponding internal tag cannot be obtained", N); |
ae888dbd | 4887 | end if; |
fbc67f84 | 4888 | end if; |
d6f39728 | 4889 | end External_Tag; |
4890 | ||
b57530b8 | 4891 | -------------------------- |
4892 | -- Implicit_Dereference -- | |
4893 | -------------------------- | |
7947a439 | 4894 | |
b57530b8 | 4895 | when Attribute_Implicit_Dereference => |
7947a439 | 4896 | |
2beb22b1 | 4897 | -- Legality checks already performed at the point of the type |
4898 | -- declaration, aspect is not delayed. | |
7947a439 | 4899 | |
89cc7147 | 4900 | null; |
b57530b8 | 4901 | |
d6f39728 | 4902 | ----------- |
4903 | -- Input -- | |
4904 | ----------- | |
4905 | ||
9f373bb8 | 4906 | when Attribute_Input => |
4907 | Analyze_Stream_TSS_Definition (TSS_Stream_Input); | |
4908 | Set_Has_Specified_Stream_Input (Ent); | |
d6f39728 | 4909 | |
89f1e35c | 4910 | ------------------------ |
4911 | -- Interrupt_Priority -- | |
4912 | ------------------------ | |
4913 | ||
4914 | when Attribute_Interrupt_Priority => Interrupt_Priority : | |
4915 | begin | |
4916 | -- Interrupt_Priority attribute definition clause not allowed | |
4917 | -- except from aspect specification. | |
4918 | ||
4919 | if From_Aspect_Specification (N) then | |
f02a9a9a | 4920 | if not Is_Concurrent_Type (U_Ent) then |
89f1e35c | 4921 | Error_Msg_N |
f02a9a9a | 4922 | ("Interrupt_Priority can only be defined for task " |
4923 | & "and protected object", Nam); | |
89f1e35c | 4924 | |
4925 | elsif Duplicate_Clause then | |
4926 | null; | |
4927 | ||
4928 | else | |
4929 | -- The expression must be analyzed in the special manner | |
4930 | -- described in "Handling of Default and Per-Object | |
4931 | -- Expressions" in sem.ads. | |
4932 | ||
4933 | -- The visibility to the discriminants must be restored | |
4934 | ||
4935 | Push_Scope_And_Install_Discriminants (U_Ent); | |
4936 | ||
4937 | Preanalyze_Spec_Expression | |
4938 | (Expr, RTE (RE_Interrupt_Priority)); | |
4939 | ||
4940 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
4941 | end if; | |
4942 | ||
4943 | else | |
4944 | Error_Msg_N | |
4945 | ("attribute& cannot be set with definition clause", N); | |
4946 | end if; | |
4947 | end Interrupt_Priority; | |
4948 | ||
b3f8228a | 4949 | -------------- |
4950 | -- Iterable -- | |
4951 | -------------- | |
4952 | ||
4953 | when Attribute_Iterable => | |
4954 | Analyze (Expr); | |
bde03454 | 4955 | |
b3f8228a | 4956 | if Nkind (Expr) /= N_Aggregate then |
4957 | Error_Msg_N ("aspect Iterable must be an aggregate", Expr); | |
4958 | end if; | |
4959 | ||
4960 | declare | |
4961 | Assoc : Node_Id; | |
4962 | ||
4963 | begin | |
4964 | Assoc := First (Component_Associations (Expr)); | |
4965 | while Present (Assoc) loop | |
4966 | if not Is_Entity_Name (Expression (Assoc)) then | |
4967 | Error_Msg_N ("value must be a function", Assoc); | |
4968 | end if; | |
bde03454 | 4969 | |
b3f8228a | 4970 | Next (Assoc); |
4971 | end loop; | |
4972 | end; | |
4973 | ||
89cc7147 | 4974 | ---------------------- |
4975 | -- Iterator_Element -- | |
4976 | ---------------------- | |
4977 | ||
4978 | when Attribute_Iterator_Element => | |
4979 | Analyze (Expr); | |
4980 | ||
4981 | if not Is_Entity_Name (Expr) | |
4982 | or else not Is_Type (Entity (Expr)) | |
4983 | then | |
4984 | Error_Msg_N ("aspect Iterator_Element must be a type", Expr); | |
4985 | end if; | |
4986 | ||
d6f39728 | 4987 | ------------------- |
4988 | -- Machine_Radix -- | |
4989 | ------------------- | |
4990 | ||
4991 | -- Machine radix attribute definition clause | |
4992 | ||
4993 | when Attribute_Machine_Radix => Machine_Radix : declare | |
4994 | Radix : constant Uint := Static_Integer (Expr); | |
4995 | ||
4996 | begin | |
4997 | if not Is_Decimal_Fixed_Point_Type (U_Ent) then | |
4998 | Error_Msg_N ("decimal fixed-point type expected for &", Nam); | |
4999 | ||
ae888dbd | 5000 | elsif Duplicate_Clause then |
5001 | null; | |
d6f39728 | 5002 | |
5003 | elsif Radix /= No_Uint then | |
5004 | Set_Has_Machine_Radix_Clause (U_Ent); | |
5005 | Set_Has_Non_Standard_Rep (Base_Type (U_Ent)); | |
5006 | ||
5007 | if Radix = 2 then | |
5008 | null; | |
5009 | elsif Radix = 10 then | |
5010 | Set_Machine_Radix_10 (U_Ent); | |
5011 | else | |
5012 | Error_Msg_N ("machine radix value must be 2 or 10", Expr); | |
5013 | end if; | |
5014 | end if; | |
5015 | end Machine_Radix; | |
5016 | ||
5017 | ----------------- | |
5018 | -- Object_Size -- | |
5019 | ----------------- | |
5020 | ||
5021 | -- Object_Size attribute definition clause | |
5022 | ||
5023 | when Attribute_Object_Size => Object_Size : declare | |
bfa5a9d9 | 5024 | Size : constant Uint := Static_Integer (Expr); |
5025 | ||
d6f39728 | 5026 | Biased : Boolean; |
bfa5a9d9 | 5027 | pragma Warnings (Off, Biased); |
d6f39728 | 5028 | |
5029 | begin | |
5030 | if not Is_Type (U_Ent) then | |
5031 | Error_Msg_N ("Object_Size cannot be given for &", Nam); | |
5032 | ||
ae888dbd | 5033 | elsif Duplicate_Clause then |
5034 | null; | |
d6f39728 | 5035 | |
5036 | else | |
5037 | Check_Size (Expr, U_Ent, Size, Biased); | |
5038 | ||
829cd457 | 5039 | if Is_Scalar_Type (U_Ent) then |
5040 | if Size /= 8 and then Size /= 16 and then Size /= 32 | |
5041 | and then UI_Mod (Size, 64) /= 0 | |
5042 | then | |
5043 | Error_Msg_N | |
5044 | ("Object_Size must be 8, 16, 32, or multiple of 64", | |
5045 | Expr); | |
5046 | end if; | |
5047 | ||
5048 | elsif Size mod 8 /= 0 then | |
5049 | Error_Msg_N ("Object_Size must be a multiple of 8", Expr); | |
d6f39728 | 5050 | end if; |
5051 | ||
5052 | Set_Esize (U_Ent, Size); | |
5053 | Set_Has_Object_Size_Clause (U_Ent); | |
1d366b32 | 5054 | Alignment_Check_For_Size_Change (U_Ent, Size); |
d6f39728 | 5055 | end if; |
5056 | end Object_Size; | |
5057 | ||
5058 | ------------ | |
5059 | -- Output -- | |
5060 | ------------ | |
5061 | ||
9f373bb8 | 5062 | when Attribute_Output => |
5063 | Analyze_Stream_TSS_Definition (TSS_Stream_Output); | |
5064 | Set_Has_Specified_Stream_Output (Ent); | |
d6f39728 | 5065 | |
89f1e35c | 5066 | -------------- |
5067 | -- Priority -- | |
5068 | -------------- | |
5069 | ||
5070 | when Attribute_Priority => Priority : | |
5071 | begin | |
5072 | -- Priority attribute definition clause not allowed except from | |
5073 | -- aspect specification. | |
5074 | ||
5075 | if From_Aspect_Specification (N) then | |
f02a9a9a | 5076 | if not (Is_Concurrent_Type (U_Ent) |
3a72f9c3 | 5077 | or else Ekind (U_Ent) = E_Procedure) |
89f1e35c | 5078 | then |
5079 | Error_Msg_N | |
f02a9a9a | 5080 | ("Priority can only be defined for task and protected " |
5081 | & "object", Nam); | |
89f1e35c | 5082 | |
5083 | elsif Duplicate_Clause then | |
5084 | null; | |
5085 | ||
5086 | else | |
5087 | -- The expression must be analyzed in the special manner | |
5088 | -- described in "Handling of Default and Per-Object | |
5089 | -- Expressions" in sem.ads. | |
5090 | ||
5091 | -- The visibility to the discriminants must be restored | |
5092 | ||
5093 | Push_Scope_And_Install_Discriminants (U_Ent); | |
5094 | Preanalyze_Spec_Expression (Expr, Standard_Integer); | |
5095 | Uninstall_Discriminants_And_Pop_Scope (U_Ent); | |
5096 | ||
cda40848 | 5097 | if not Is_OK_Static_Expression (Expr) then |
89f1e35c | 5098 | Check_Restriction (Static_Priorities, Expr); |
5099 | end if; | |
5100 | end if; | |
5101 | ||
5102 | else | |
5103 | Error_Msg_N | |
5104 | ("attribute& cannot be set with definition clause", N); | |
5105 | end if; | |
5106 | end Priority; | |
5107 | ||
d6f39728 | 5108 | ---------- |
5109 | -- Read -- | |
5110 | ---------- | |
5111 | ||
9f373bb8 | 5112 | when Attribute_Read => |
5113 | Analyze_Stream_TSS_Definition (TSS_Stream_Read); | |
5114 | Set_Has_Specified_Stream_Read (Ent); | |
d6f39728 | 5115 | |
b7b74740 | 5116 | -------------------------- |
5117 | -- Scalar_Storage_Order -- | |
5118 | -------------------------- | |
5119 | ||
5120 | -- Scalar_Storage_Order attribute definition clause | |
5121 | ||
5122 | when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare | |
5123 | begin | |
b43a5770 | 5124 | if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then |
b7b74740 | 5125 | Error_Msg_N |
b43a5770 | 5126 | ("Scalar_Storage_Order can only be defined for " |
5127 | & "record or array type", Nam); | |
b7b74740 | 5128 | |
5129 | elsif Duplicate_Clause then | |
5130 | null; | |
5131 | ||
5132 | else | |
5133 | Analyze_And_Resolve (Expr, RTE (RE_Bit_Order)); | |
5134 | ||
5135 | if Etype (Expr) = Any_Type then | |
5136 | return; | |
5137 | ||
cda40848 | 5138 | elsif not Is_OK_Static_Expression (Expr) then |
b7b74740 | 5139 | Flag_Non_Static_Expr |
5140 | ("Scalar_Storage_Order requires static expression!", Expr); | |
5141 | ||
c0912570 | 5142 | elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then |
5143 | ||
5144 | -- Here for the case of a non-default (i.e. non-confirming) | |
5145 | -- Scalar_Storage_Order attribute definition. | |
5146 | ||
5147 | if Support_Nondefault_SSO_On_Target then | |
d0a9ea3b | 5148 | Set_Reverse_Storage_Order (Base_Type (U_Ent), True); |
c0912570 | 5149 | else |
5150 | Error_Msg_N | |
5151 | ("non-default Scalar_Storage_Order " | |
5152 | & "not supported on target", Expr); | |
b7b74740 | 5153 | end if; |
5154 | end if; | |
b64082f2 | 5155 | |
5156 | -- Clear SSO default indications since explicit setting of the | |
5157 | -- order overrides the defaults. | |
5158 | ||
5159 | Set_SSO_Set_Low_By_Default (Base_Type (U_Ent), False); | |
5160 | Set_SSO_Set_High_By_Default (Base_Type (U_Ent), False); | |
b7b74740 | 5161 | end if; |
5162 | end Scalar_Storage_Order; | |
5163 | ||
d6f39728 | 5164 | ---------- |
5165 | -- Size -- | |
5166 | ---------- | |
5167 | ||
5168 | -- Size attribute definition clause | |
5169 | ||
5170 | when Attribute_Size => Size : declare | |
5171 | Size : constant Uint := Static_Integer (Expr); | |
5172 | Etyp : Entity_Id; | |
5173 | Biased : Boolean; | |
5174 | ||
5175 | begin | |
5176 | FOnly := True; | |
5177 | ||
ae888dbd | 5178 | if Duplicate_Clause then |
5179 | null; | |
d6f39728 | 5180 | |
5181 | elsif not Is_Type (U_Ent) | |
5182 | and then Ekind (U_Ent) /= E_Variable | |
5183 | and then Ekind (U_Ent) /= E_Constant | |
5184 | then | |
5185 | Error_Msg_N ("size cannot be given for &", Nam); | |
5186 | ||
5187 | elsif Is_Array_Type (U_Ent) | |
5188 | and then not Is_Constrained (U_Ent) | |
5189 | then | |
5190 | Error_Msg_N | |
5191 | ("size cannot be given for unconstrained array", Nam); | |
5192 | ||
c2b89d6e | 5193 | elsif Size /= No_Uint then |
c2b89d6e | 5194 | if VM_Target /= No_VM and then not GNAT_Mode then |
47495553 | 5195 | |
c2b89d6e | 5196 | -- Size clause is not handled properly on VM targets. |
5197 | -- Display a warning unless we are in GNAT mode, in which | |
5198 | -- case this is useless. | |
47495553 | 5199 | |
682fa897 | 5200 | Error_Msg_N |
1e3532e7 | 5201 | ("size clauses are ignored in this configuration??", N); |
682fa897 | 5202 | end if; |
5203 | ||
d6f39728 | 5204 | if Is_Type (U_Ent) then |
5205 | Etyp := U_Ent; | |
5206 | else | |
5207 | Etyp := Etype (U_Ent); | |
5208 | end if; | |
5209 | ||
59ac57b5 | 5210 | -- Check size, note that Gigi is in charge of checking that the |
5211 | -- size of an array or record type is OK. Also we do not check | |
5212 | -- the size in the ordinary fixed-point case, since it is too | |
5213 | -- early to do so (there may be subsequent small clause that | |
5214 | -- affects the size). We can check the size if a small clause | |
5215 | -- has already been given. | |
d6f39728 | 5216 | |
5217 | if not Is_Ordinary_Fixed_Point_Type (U_Ent) | |
5218 | or else Has_Small_Clause (U_Ent) | |
5219 | then | |
5220 | Check_Size (Expr, Etyp, Size, Biased); | |
b77e4501 | 5221 | Set_Biased (U_Ent, N, "size clause", Biased); |
d6f39728 | 5222 | end if; |
5223 | ||
5224 | -- For types set RM_Size and Esize if possible | |
5225 | ||
5226 | if Is_Type (U_Ent) then | |
5227 | Set_RM_Size (U_Ent, Size); | |
5228 | ||
ada34def | 5229 | -- For elementary types, increase Object_Size to power of 2, |
5230 | -- but not less than a storage unit in any case (normally | |
59ac57b5 | 5231 | -- this means it will be byte addressable). |
d6f39728 | 5232 | |
ada34def | 5233 | -- For all other types, nothing else to do, we leave Esize |
5234 | -- (object size) unset, the back end will set it from the | |
5235 | -- size and alignment in an appropriate manner. | |
5236 | ||
1d366b32 | 5237 | -- In both cases, we check whether the alignment must be |
5238 | -- reset in the wake of the size change. | |
5239 | ||
ada34def | 5240 | if Is_Elementary_Type (U_Ent) then |
f15731c4 | 5241 | if Size <= System_Storage_Unit then |
5242 | Init_Esize (U_Ent, System_Storage_Unit); | |
d6f39728 | 5243 | elsif Size <= 16 then |
5244 | Init_Esize (U_Ent, 16); | |
5245 | elsif Size <= 32 then | |
5246 | Init_Esize (U_Ent, 32); | |
5247 | else | |
5248 | Set_Esize (U_Ent, (Size + 63) / 64 * 64); | |
5249 | end if; | |
5250 | ||
1d366b32 | 5251 | Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent)); |
5252 | else | |
5253 | Alignment_Check_For_Size_Change (U_Ent, Size); | |
d6f39728 | 5254 | end if; |
5255 | ||
d6f39728 | 5256 | -- For objects, set Esize only |
5257 | ||
5258 | else | |
9dfe12ae | 5259 | if Is_Elementary_Type (Etyp) then |
5260 | if Size /= System_Storage_Unit | |
5261 | and then | |
5262 | Size /= System_Storage_Unit * 2 | |
5263 | and then | |
5264 | Size /= System_Storage_Unit * 4 | |
5265 | and then | |
5266 | Size /= System_Storage_Unit * 8 | |
5267 | then | |
5c99c290 | 5268 | Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit); |
87d5c1d0 | 5269 | Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8; |
9dfe12ae | 5270 | Error_Msg_N |
5c99c290 | 5271 | ("size for primitive object must be a power of 2" |
87d5c1d0 | 5272 | & " in the range ^-^", N); |
9dfe12ae | 5273 | end if; |
5274 | end if; | |
5275 | ||
d6f39728 | 5276 | Set_Esize (U_Ent, Size); |
5277 | end if; | |
5278 | ||
5279 | Set_Has_Size_Clause (U_Ent); | |
5280 | end if; | |
5281 | end Size; | |
5282 | ||
5283 | ----------- | |
5284 | -- Small -- | |
5285 | ----------- | |
5286 | ||
5287 | -- Small attribute definition clause | |
5288 | ||
5289 | when Attribute_Small => Small : declare | |
5290 | Implicit_Base : constant Entity_Id := Base_Type (U_Ent); | |
5291 | Small : Ureal; | |
5292 | ||
5293 | begin | |
5294 | Analyze_And_Resolve (Expr, Any_Real); | |
5295 | ||
5296 | if Etype (Expr) = Any_Type then | |
5297 | return; | |
5298 | ||
cda40848 | 5299 | elsif not Is_OK_Static_Expression (Expr) then |
9dfe12ae | 5300 | Flag_Non_Static_Expr |
5301 | ("small requires static expression!", Expr); | |
d6f39728 | 5302 | return; |
5303 | ||
5304 | else | |
5305 | Small := Expr_Value_R (Expr); | |
5306 | ||
5307 | if Small <= Ureal_0 then | |
5308 | Error_Msg_N ("small value must be greater than zero", Expr); | |
5309 | return; | |
5310 | end if; | |
5311 | ||
5312 | end if; | |
5313 | ||
5314 | if not Is_Ordinary_Fixed_Point_Type (U_Ent) then | |
5315 | Error_Msg_N | |
5316 | ("small requires an ordinary fixed point type", Nam); | |
5317 | ||
5318 | elsif Has_Small_Clause (U_Ent) then | |
5319 | Error_Msg_N ("small already given for &", Nam); | |
5320 | ||
5321 | elsif Small > Delta_Value (U_Ent) then | |
5322 | Error_Msg_N | |
ce3e25d6 | 5323 | ("small value must not be greater than delta value", Nam); |
d6f39728 | 5324 | |
5325 | else | |
5326 | Set_Small_Value (U_Ent, Small); | |
5327 | Set_Small_Value (Implicit_Base, Small); | |
5328 | Set_Has_Small_Clause (U_Ent); | |
5329 | Set_Has_Small_Clause (Implicit_Base); | |
5330 | Set_Has_Non_Standard_Rep (Implicit_Base); | |
5331 | end if; | |
5332 | end Small; | |
5333 | ||
d6f39728 | 5334 | ------------------ |
5335 | -- Storage_Pool -- | |
5336 | ------------------ | |
5337 | ||
5338 | -- Storage_Pool attribute definition clause | |
5339 | ||
b55f7641 | 5340 | when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare |
d6f39728 | 5341 | Pool : Entity_Id; |
6b567c71 | 5342 | T : Entity_Id; |
d6f39728 | 5343 | |
5344 | begin | |
44e4341e | 5345 | if Ekind (U_Ent) = E_Access_Subprogram_Type then |
5346 | Error_Msg_N | |
5347 | ("storage pool cannot be given for access-to-subprogram type", | |
5348 | Nam); | |
5349 | return; | |
5350 | ||
d3ef794c | 5351 | elsif not |
5352 | Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type) | |
d6f39728 | 5353 | then |
44e4341e | 5354 | Error_Msg_N |
5355 | ("storage pool can only be given for access types", Nam); | |
d6f39728 | 5356 | return; |
5357 | ||
5358 | elsif Is_Derived_Type (U_Ent) then | |
5359 | Error_Msg_N | |
5360 | ("storage pool cannot be given for a derived access type", | |
5361 | Nam); | |
5362 | ||
ae888dbd | 5363 | elsif Duplicate_Clause then |
d6f39728 | 5364 | return; |
5365 | ||
5366 | elsif Present (Associated_Storage_Pool (U_Ent)) then | |
5367 | Error_Msg_N ("storage pool already given for &", Nam); | |
5368 | return; | |
5369 | end if; | |
5370 | ||
6653b695 | 5371 | -- Check for Storage_Size previously given |
5372 | ||
5373 | declare | |
5374 | SS : constant Node_Id := | |
5375 | Get_Attribute_Definition_Clause | |
5376 | (U_Ent, Attribute_Storage_Size); | |
5377 | begin | |
5378 | if Present (SS) then | |
5379 | Check_Pool_Size_Clash (U_Ent, N, SS); | |
5380 | end if; | |
5381 | end; | |
5382 | ||
5383 | -- Storage_Pool case | |
5384 | ||
b55f7641 | 5385 | if Id = Attribute_Storage_Pool then |
5386 | Analyze_And_Resolve | |
5387 | (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool))); | |
5388 | ||
5389 | -- In the Simple_Storage_Pool case, we allow a variable of any | |
b15003c3 | 5390 | -- simple storage pool type, so we Resolve without imposing an |
b55f7641 | 5391 | -- expected type. |
5392 | ||
5393 | else | |
5394 | Analyze_And_Resolve (Expr); | |
5395 | ||
5396 | if not Present (Get_Rep_Pragma | |
b15003c3 | 5397 | (Etype (Expr), Name_Simple_Storage_Pool_Type)) |
b55f7641 | 5398 | then |
5399 | Error_Msg_N | |
5400 | ("expression must be of a simple storage pool type", Expr); | |
5401 | end if; | |
5402 | end if; | |
d6f39728 | 5403 | |
8c5c7277 | 5404 | if not Denotes_Variable (Expr) then |
5405 | Error_Msg_N ("storage pool must be a variable", Expr); | |
5406 | return; | |
5407 | end if; | |
5408 | ||
6b567c71 | 5409 | if Nkind (Expr) = N_Type_Conversion then |
5410 | T := Etype (Expression (Expr)); | |
5411 | else | |
5412 | T := Etype (Expr); | |
5413 | end if; | |
5414 | ||
5415 | -- The Stack_Bounded_Pool is used internally for implementing | |
d64221a7 | 5416 | -- access types with a Storage_Size. Since it only work properly |
5417 | -- when used on one specific type, we need to check that it is not | |
5418 | -- hijacked improperly: | |
5419 | ||
6b567c71 | 5420 | -- type T is access Integer; |
5421 | -- for T'Storage_Size use n; | |
5422 | -- type Q is access Float; | |
5423 | -- for Q'Storage_Size use T'Storage_Size; -- incorrect | |
5424 | ||
15ebb600 | 5425 | if RTE_Available (RE_Stack_Bounded_Pool) |
5426 | and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool) | |
5427 | then | |
5428 | Error_Msg_N ("non-shareable internal Pool", Expr); | |
6b567c71 | 5429 | return; |
5430 | end if; | |
5431 | ||
d6f39728 | 5432 | -- If the argument is a name that is not an entity name, then |
5433 | -- we construct a renaming operation to define an entity of | |
5434 | -- type storage pool. | |
5435 | ||
5436 | if not Is_Entity_Name (Expr) | |
5437 | and then Is_Object_Reference (Expr) | |
5438 | then | |
11deeeb6 | 5439 | Pool := Make_Temporary (Loc, 'P', Expr); |
d6f39728 | 5440 | |
5441 | declare | |
5442 | Rnode : constant Node_Id := | |
5443 | Make_Object_Renaming_Declaration (Loc, | |
5444 | Defining_Identifier => Pool, | |
5445 | Subtype_Mark => | |
5446 | New_Occurrence_Of (Etype (Expr), Loc), | |
11deeeb6 | 5447 | Name => Expr); |
d6f39728 | 5448 | |
5449 | begin | |
f65f7fdf | 5450 | -- If the attribute definition clause comes from an aspect |
5451 | -- clause, then insert the renaming before the associated | |
5452 | -- entity's declaration, since the attribute clause has | |
5453 | -- not yet been appended to the declaration list. | |
5454 | ||
5455 | if From_Aspect_Specification (N) then | |
5456 | Insert_Before (Parent (Entity (N)), Rnode); | |
5457 | else | |
5458 | Insert_Before (N, Rnode); | |
5459 | end if; | |
5460 | ||
d6f39728 | 5461 | Analyze (Rnode); |
5462 | Set_Associated_Storage_Pool (U_Ent, Pool); | |
5463 | end; | |
5464 | ||
5465 | elsif Is_Entity_Name (Expr) then | |
5466 | Pool := Entity (Expr); | |
5467 | ||
5468 | -- If pool is a renamed object, get original one. This can | |
5469 | -- happen with an explicit renaming, and within instances. | |
5470 | ||
5471 | while Present (Renamed_Object (Pool)) | |
5472 | and then Is_Entity_Name (Renamed_Object (Pool)) | |
5473 | loop | |
5474 | Pool := Entity (Renamed_Object (Pool)); | |
5475 | end loop; | |
5476 | ||
5477 | if Present (Renamed_Object (Pool)) | |
5478 | and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion | |
5479 | and then Is_Entity_Name (Expression (Renamed_Object (Pool))) | |
5480 | then | |
5481 | Pool := Entity (Expression (Renamed_Object (Pool))); | |
5482 | end if; | |
5483 | ||
6b567c71 | 5484 | Set_Associated_Storage_Pool (U_Ent, Pool); |
d6f39728 | 5485 | |
5486 | elsif Nkind (Expr) = N_Type_Conversion | |
5487 | and then Is_Entity_Name (Expression (Expr)) | |
5488 | and then Nkind (Original_Node (Expr)) = N_Attribute_Reference | |
5489 | then | |
5490 | Pool := Entity (Expression (Expr)); | |
6b567c71 | 5491 | Set_Associated_Storage_Pool (U_Ent, Pool); |
d6f39728 | 5492 | |
5493 | else | |
5494 | Error_Msg_N ("incorrect reference to a Storage Pool", Expr); | |
5495 | return; | |
5496 | end if; | |
b55f7641 | 5497 | end; |
d6f39728 | 5498 | |
44e4341e | 5499 | ------------------ |
5500 | -- Storage_Size -- | |
5501 | ------------------ | |
5502 | ||
5503 | -- Storage_Size attribute definition clause | |
5504 | ||
5505 | when Attribute_Storage_Size => Storage_Size : declare | |
5506 | Btype : constant Entity_Id := Base_Type (U_Ent); | |
44e4341e | 5507 | |
5508 | begin | |
5509 | if Is_Task_Type (U_Ent) then | |
44e4341e | 5510 | |
39a0c1d3 | 5511 | -- Check obsolescent (but never obsolescent if from aspect) |
ceec4f7c | 5512 | |
5513 | if not From_Aspect_Specification (N) then | |
5514 | Check_Restriction (No_Obsolescent_Features, N); | |
5515 | ||
5516 | if Warn_On_Obsolescent_Feature then | |
5517 | Error_Msg_N | |
5518 | ("?j?storage size clause for task is an " & | |
5519 | "obsolescent feature (RM J.9)", N); | |
5520 | Error_Msg_N ("\?j?use Storage_Size pragma instead", N); | |
5521 | end if; | |
44e4341e | 5522 | end if; |
5523 | ||
5524 | FOnly := True; | |
5525 | end if; | |
5526 | ||
5527 | if not Is_Access_Type (U_Ent) | |
5528 | and then Ekind (U_Ent) /= E_Task_Type | |
5529 | then | |
5530 | Error_Msg_N ("storage size cannot be given for &", Nam); | |
5531 | ||
5532 | elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then | |
5533 | Error_Msg_N | |
5534 | ("storage size cannot be given for a derived access type", | |
5535 | Nam); | |
5536 | ||
ae888dbd | 5537 | elsif Duplicate_Clause then |
5538 | null; | |
44e4341e | 5539 | |
5540 | else | |
5541 | Analyze_And_Resolve (Expr, Any_Integer); | |
5542 | ||
5543 | if Is_Access_Type (U_Ent) then | |
6653b695 | 5544 | |
5545 | -- Check for Storage_Pool previously given | |
5546 | ||
5547 | declare | |
5548 | SP : constant Node_Id := | |
5549 | Get_Attribute_Definition_Clause | |
5550 | (U_Ent, Attribute_Storage_Pool); | |
5551 | ||
5552 | begin | |
5553 | if Present (SP) then | |
5554 | Check_Pool_Size_Clash (U_Ent, SP, N); | |
5555 | end if; | |
5556 | end; | |
5557 | ||
5558 | -- Special case of for x'Storage_Size use 0 | |
44e4341e | 5559 | |
5941a4e9 | 5560 | if Is_OK_Static_Expression (Expr) |
44e4341e | 5561 | and then Expr_Value (Expr) = 0 |
5562 | then | |
5563 | Set_No_Pool_Assigned (Btype); | |
5564 | end if; | |
44e4341e | 5565 | end if; |
5566 | ||
5567 | Set_Has_Storage_Size_Clause (Btype); | |
5568 | end if; | |
5569 | end Storage_Size; | |
5570 | ||
7189d17f | 5571 | ----------------- |
5572 | -- Stream_Size -- | |
5573 | ----------------- | |
5574 | ||
5575 | when Attribute_Stream_Size => Stream_Size : declare | |
5576 | Size : constant Uint := Static_Integer (Expr); | |
5577 | ||
5578 | begin | |
15ebb600 | 5579 | if Ada_Version <= Ada_95 then |
5580 | Check_Restriction (No_Implementation_Attributes, N); | |
5581 | end if; | |
5582 | ||
ae888dbd | 5583 | if Duplicate_Clause then |
5584 | null; | |
7189d17f | 5585 | |
5586 | elsif Is_Elementary_Type (U_Ent) then | |
5587 | if Size /= System_Storage_Unit | |
5588 | and then | |
5589 | Size /= System_Storage_Unit * 2 | |
5590 | and then | |
5591 | Size /= System_Storage_Unit * 4 | |
5592 | and then | |
5593 | Size /= System_Storage_Unit * 8 | |
5594 | then | |
5595 | Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit); | |
5596 | Error_Msg_N | |
5597 | ("stream size for elementary type must be a" | |
5598 | & " power of 2 and at least ^", N); | |
5599 | ||
5600 | elsif RM_Size (U_Ent) > Size then | |
5601 | Error_Msg_Uint_1 := RM_Size (U_Ent); | |
5602 | Error_Msg_N | |
5603 | ("stream size for elementary type must be a" | |
5604 | & " power of 2 and at least ^", N); | |
5605 | end if; | |
5606 | ||
5607 | Set_Has_Stream_Size_Clause (U_Ent); | |
5608 | ||
5609 | else | |
5610 | Error_Msg_N ("Stream_Size cannot be given for &", Nam); | |
5611 | end if; | |
5612 | end Stream_Size; | |
5613 | ||
d6f39728 | 5614 | ---------------- |
5615 | -- Value_Size -- | |
5616 | ---------------- | |
5617 | ||
5618 | -- Value_Size attribute definition clause | |
5619 | ||
5620 | when Attribute_Value_Size => Value_Size : declare | |
5621 | Size : constant Uint := Static_Integer (Expr); | |
5622 | Biased : Boolean; | |
5623 | ||
5624 | begin | |
5625 | if not Is_Type (U_Ent) then | |
5626 | Error_Msg_N ("Value_Size cannot be given for &", Nam); | |
5627 | ||
ae888dbd | 5628 | elsif Duplicate_Clause then |
5629 | null; | |
d6f39728 | 5630 | |
59ac57b5 | 5631 | elsif Is_Array_Type (U_Ent) |
5632 | and then not Is_Constrained (U_Ent) | |
5633 | then | |
5634 | Error_Msg_N | |
5635 | ("Value_Size cannot be given for unconstrained array", Nam); | |
5636 | ||
d6f39728 | 5637 | else |
5638 | if Is_Elementary_Type (U_Ent) then | |
5639 | Check_Size (Expr, U_Ent, Size, Biased); | |
b77e4501 | 5640 | Set_Biased (U_Ent, N, "value size clause", Biased); |
d6f39728 | 5641 | end if; |
5642 | ||
5643 | Set_RM_Size (U_Ent, Size); | |
5644 | end if; | |
5645 | end Value_Size; | |
5646 | ||
81b424ac | 5647 | ----------------------- |
5648 | -- Variable_Indexing -- | |
5649 | ----------------------- | |
5650 | ||
5651 | when Attribute_Variable_Indexing => | |
5652 | Check_Indexing_Functions; | |
5653 | ||
d6f39728 | 5654 | ----------- |
5655 | -- Write -- | |
5656 | ----------- | |
5657 | ||
9f373bb8 | 5658 | when Attribute_Write => |
5659 | Analyze_Stream_TSS_Definition (TSS_Stream_Write); | |
5660 | Set_Has_Specified_Stream_Write (Ent); | |
d6f39728 | 5661 | |
5662 | -- All other attributes cannot be set | |
5663 | ||
5664 | when others => | |
5665 | Error_Msg_N | |
5666 | ("attribute& cannot be set with definition clause", N); | |
d6f39728 | 5667 | end case; |
5668 | ||
d64221a7 | 5669 | -- The test for the type being frozen must be performed after any |
5670 | -- expression the clause has been analyzed since the expression itself | |
5671 | -- might cause freezing that makes the clause illegal. | |
d6f39728 | 5672 | |
5673 | if Rep_Item_Too_Late (U_Ent, N, FOnly) then | |
5674 | return; | |
5675 | end if; | |
5676 | end Analyze_Attribute_Definition_Clause; | |
5677 | ||
5678 | ---------------------------- | |
5679 | -- Analyze_Code_Statement -- | |
5680 | ---------------------------- | |
5681 | ||
5682 | procedure Analyze_Code_Statement (N : Node_Id) is | |
5683 | HSS : constant Node_Id := Parent (N); | |
5684 | SBody : constant Node_Id := Parent (HSS); | |
5685 | Subp : constant Entity_Id := Current_Scope; | |
5686 | Stmt : Node_Id; | |
5687 | Decl : Node_Id; | |
5688 | StmtO : Node_Id; | |
5689 | DeclO : Node_Id; | |
5690 | ||
5691 | begin | |
5692 | -- Analyze and check we get right type, note that this implements the | |
5693 | -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that | |
5694 | -- is the only way that Asm_Insn could possibly be visible. | |
5695 | ||
5696 | Analyze_And_Resolve (Expression (N)); | |
5697 | ||
5698 | if Etype (Expression (N)) = Any_Type then | |
5699 | return; | |
5700 | elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then | |
5701 | Error_Msg_N ("incorrect type for code statement", N); | |
5702 | return; | |
5703 | end if; | |
5704 | ||
44e4341e | 5705 | Check_Code_Statement (N); |
5706 | ||
d6f39728 | 5707 | -- Make sure we appear in the handled statement sequence of a |
5708 | -- subprogram (RM 13.8(3)). | |
5709 | ||
5710 | if Nkind (HSS) /= N_Handled_Sequence_Of_Statements | |
5711 | or else Nkind (SBody) /= N_Subprogram_Body | |
5712 | then | |
5713 | Error_Msg_N | |
5714 | ("code statement can only appear in body of subprogram", N); | |
5715 | return; | |
5716 | end if; | |
5717 | ||
5718 | -- Do remaining checks (RM 13.8(3)) if not already done | |
5719 | ||
5720 | if not Is_Machine_Code_Subprogram (Subp) then | |
5721 | Set_Is_Machine_Code_Subprogram (Subp); | |
5722 | ||
5723 | -- No exception handlers allowed | |
5724 | ||
5725 | if Present (Exception_Handlers (HSS)) then | |
5726 | Error_Msg_N | |
5727 | ("exception handlers not permitted in machine code subprogram", | |
5728 | First (Exception_Handlers (HSS))); | |
5729 | end if; | |
5730 | ||
5731 | -- No declarations other than use clauses and pragmas (we allow | |
5732 | -- certain internally generated declarations as well). | |
5733 | ||
5734 | Decl := First (Declarations (SBody)); | |
5735 | while Present (Decl) loop | |
5736 | DeclO := Original_Node (Decl); | |
5737 | if Comes_From_Source (DeclO) | |
fdd294d1 | 5738 | and not Nkind_In (DeclO, N_Pragma, |
5739 | N_Use_Package_Clause, | |
5740 | N_Use_Type_Clause, | |
5741 | N_Implicit_Label_Declaration) | |
d6f39728 | 5742 | then |
5743 | Error_Msg_N | |
5744 | ("this declaration not allowed in machine code subprogram", | |
5745 | DeclO); | |
5746 | end if; | |
5747 | ||
5748 | Next (Decl); | |
5749 | end loop; | |
5750 | ||
5751 | -- No statements other than code statements, pragmas, and labels. | |
5752 | -- Again we allow certain internally generated statements. | |
3ab42ff7 | 5753 | |
c3107527 | 5754 | -- In Ada 2012, qualified expressions are names, and the code |
5755 | -- statement is initially parsed as a procedure call. | |
d6f39728 | 5756 | |
5757 | Stmt := First (Statements (HSS)); | |
5758 | while Present (Stmt) loop | |
5759 | StmtO := Original_Node (Stmt); | |
c3107527 | 5760 | |
59f2fcab | 5761 | -- A procedure call transformed into a code statement is OK. |
5762 | ||
c3107527 | 5763 | if Ada_Version >= Ada_2012 |
5764 | and then Nkind (StmtO) = N_Procedure_Call_Statement | |
59f2fcab | 5765 | and then Nkind (Name (StmtO)) = N_Qualified_Expression |
c3107527 | 5766 | then |
5767 | null; | |
5768 | ||
5769 | elsif Comes_From_Source (StmtO) | |
fdd294d1 | 5770 | and then not Nkind_In (StmtO, N_Pragma, |
5771 | N_Label, | |
5772 | N_Code_Statement) | |
d6f39728 | 5773 | then |
5774 | Error_Msg_N | |
5775 | ("this statement is not allowed in machine code subprogram", | |
5776 | StmtO); | |
5777 | end if; | |
5778 | ||
5779 | Next (Stmt); | |
5780 | end loop; | |
5781 | end if; | |
d6f39728 | 5782 | end Analyze_Code_Statement; |
5783 | ||
5784 | ----------------------------------------------- | |
5785 | -- Analyze_Enumeration_Representation_Clause -- | |
5786 | ----------------------------------------------- | |
5787 | ||
5788 | procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is | |
5789 | Ident : constant Node_Id := Identifier (N); | |
5790 | Aggr : constant Node_Id := Array_Aggregate (N); | |
5791 | Enumtype : Entity_Id; | |
5792 | Elit : Entity_Id; | |
5793 | Expr : Node_Id; | |
5794 | Assoc : Node_Id; | |
5795 | Choice : Node_Id; | |
5796 | Val : Uint; | |
b3190af0 | 5797 | |
5798 | Err : Boolean := False; | |
098d3082 | 5799 | -- Set True to avoid cascade errors and crashes on incorrect source code |
d6f39728 | 5800 | |
e30c7d84 | 5801 | Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer)); |
5802 | Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer)); | |
5803 | -- Allowed range of universal integer (= allowed range of enum lit vals) | |
5804 | ||
d6f39728 | 5805 | Min : Uint; |
5806 | Max : Uint; | |
e30c7d84 | 5807 | -- Minimum and maximum values of entries |
5808 | ||
5809 | Max_Node : Node_Id; | |
5810 | -- Pointer to node for literal providing max value | |
d6f39728 | 5811 | |
5812 | begin | |
ca301e17 | 5813 | if Ignore_Rep_Clauses then |
2ff55065 | 5814 | Kill_Rep_Clause (N); |
fbc67f84 | 5815 | return; |
5816 | end if; | |
5817 | ||
175a6969 | 5818 | -- Ignore enumeration rep clauses by default in CodePeer mode, |
5819 | -- unless -gnatd.I is specified, as a work around for potential false | |
5820 | -- positive messages. | |
5821 | ||
5822 | if CodePeer_Mode and not Debug_Flag_Dot_II then | |
5823 | return; | |
5824 | end if; | |
5825 | ||
d6f39728 | 5826 | -- First some basic error checks |
5827 | ||
5828 | Find_Type (Ident); | |
5829 | Enumtype := Entity (Ident); | |
5830 | ||
5831 | if Enumtype = Any_Type | |
5832 | or else Rep_Item_Too_Early (Enumtype, N) | |
5833 | then | |
5834 | return; | |
5835 | else | |
5836 | Enumtype := Underlying_Type (Enumtype); | |
5837 | end if; | |
5838 | ||
5839 | if not Is_Enumeration_Type (Enumtype) then | |
5840 | Error_Msg_NE | |
5841 | ("enumeration type required, found}", | |
5842 | Ident, First_Subtype (Enumtype)); | |
5843 | return; | |
5844 | end if; | |
5845 | ||
9dfe12ae | 5846 | -- Ignore rep clause on generic actual type. This will already have |
5847 | -- been flagged on the template as an error, and this is the safest | |
5848 | -- way to ensure we don't get a junk cascaded message in the instance. | |
5849 | ||
5850 | if Is_Generic_Actual_Type (Enumtype) then | |
5851 | return; | |
5852 | ||
5853 | -- Type must be in current scope | |
5854 | ||
5855 | elsif Scope (Enumtype) /= Current_Scope then | |
d6f39728 | 5856 | Error_Msg_N ("type must be declared in this scope", Ident); |
5857 | return; | |
5858 | ||
9dfe12ae | 5859 | -- Type must be a first subtype |
5860 | ||
d6f39728 | 5861 | elsif not Is_First_Subtype (Enumtype) then |
5862 | Error_Msg_N ("cannot give enumeration rep clause for subtype", N); | |
5863 | return; | |
5864 | ||
9dfe12ae | 5865 | -- Ignore duplicate rep clause |
5866 | ||
d6f39728 | 5867 | elsif Has_Enumeration_Rep_Clause (Enumtype) then |
5868 | Error_Msg_N ("duplicate enumeration rep clause ignored", N); | |
5869 | return; | |
5870 | ||
7189d17f | 5871 | -- Don't allow rep clause for standard [wide_[wide_]]character |
9dfe12ae | 5872 | |
177675a7 | 5873 | elsif Is_Standard_Character_Type (Enumtype) then |
d6f39728 | 5874 | Error_Msg_N ("enumeration rep clause not allowed for this type", N); |
9dfe12ae | 5875 | return; |
5876 | ||
d9125581 | 5877 | -- Check that the expression is a proper aggregate (no parentheses) |
5878 | ||
5879 | elsif Paren_Count (Aggr) /= 0 then | |
5880 | Error_Msg | |
5881 | ("extra parentheses surrounding aggregate not allowed", | |
5882 | First_Sloc (Aggr)); | |
5883 | return; | |
5884 | ||
9dfe12ae | 5885 | -- All tests passed, so set rep clause in place |
d6f39728 | 5886 | |
5887 | else | |
5888 | Set_Has_Enumeration_Rep_Clause (Enumtype); | |
5889 | Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype)); | |
5890 | end if; | |
5891 | ||
5892 | -- Now we process the aggregate. Note that we don't use the normal | |
5893 | -- aggregate code for this purpose, because we don't want any of the | |
5894 | -- normal expansion activities, and a number of special semantic | |
5895 | -- rules apply (including the component type being any integer type) | |
5896 | ||
d6f39728 | 5897 | Elit := First_Literal (Enumtype); |
5898 | ||
5899 | -- First the positional entries if any | |
5900 | ||
5901 | if Present (Expressions (Aggr)) then | |
5902 | Expr := First (Expressions (Aggr)); | |
5903 | while Present (Expr) loop | |
5904 | if No (Elit) then | |
5905 | Error_Msg_N ("too many entries in aggregate", Expr); | |
5906 | return; | |
5907 | end if; | |
5908 | ||
5909 | Val := Static_Integer (Expr); | |
5910 | ||
d9125581 | 5911 | -- Err signals that we found some incorrect entries processing |
5912 | -- the list. The final checks for completeness and ordering are | |
5913 | -- skipped in this case. | |
5914 | ||
d6f39728 | 5915 | if Val = No_Uint then |
5916 | Err := True; | |
f02a9a9a | 5917 | |
d6f39728 | 5918 | elsif Val < Lo or else Hi < Val then |
5919 | Error_Msg_N ("value outside permitted range", Expr); | |
5920 | Err := True; | |
5921 | end if; | |
5922 | ||
5923 | Set_Enumeration_Rep (Elit, Val); | |
5924 | Set_Enumeration_Rep_Expr (Elit, Expr); | |
5925 | Next (Expr); | |
5926 | Next (Elit); | |
5927 | end loop; | |
5928 | end if; | |
5929 | ||
5930 | -- Now process the named entries if present | |
5931 | ||
5932 | if Present (Component_Associations (Aggr)) then | |
5933 | Assoc := First (Component_Associations (Aggr)); | |
5934 | while Present (Assoc) loop | |
5935 | Choice := First (Choices (Assoc)); | |
5936 | ||
5937 | if Present (Next (Choice)) then | |
5938 | Error_Msg_N | |
5939 | ("multiple choice not allowed here", Next (Choice)); | |
5940 | Err := True; | |
5941 | end if; | |
5942 | ||
5943 | if Nkind (Choice) = N_Others_Choice then | |
5944 | Error_Msg_N ("others choice not allowed here", Choice); | |
5945 | Err := True; | |
5946 | ||
5947 | elsif Nkind (Choice) = N_Range then | |
b3190af0 | 5948 | |
d6f39728 | 5949 | -- ??? should allow zero/one element range here |
b3190af0 | 5950 | |
d6f39728 | 5951 | Error_Msg_N ("range not allowed here", Choice); |
5952 | Err := True; | |
5953 | ||
5954 | else | |
5955 | Analyze_And_Resolve (Choice, Enumtype); | |
b3190af0 | 5956 | |
098d3082 | 5957 | if Error_Posted (Choice) then |
d6f39728 | 5958 | Err := True; |
098d3082 | 5959 | end if; |
d6f39728 | 5960 | |
098d3082 | 5961 | if not Err then |
5962 | if Is_Entity_Name (Choice) | |
5963 | and then Is_Type (Entity (Choice)) | |
5964 | then | |
5965 | Error_Msg_N ("subtype name not allowed here", Choice); | |
d6f39728 | 5966 | Err := True; |
b3190af0 | 5967 | |
098d3082 | 5968 | -- ??? should allow static subtype with zero/one entry |
d6f39728 | 5969 | |
098d3082 | 5970 | elsif Etype (Choice) = Base_Type (Enumtype) then |
cda40848 | 5971 | if not Is_OK_Static_Expression (Choice) then |
098d3082 | 5972 | Flag_Non_Static_Expr |
5973 | ("non-static expression used for choice!", Choice); | |
d6f39728 | 5974 | Err := True; |
d6f39728 | 5975 | |
098d3082 | 5976 | else |
5977 | Elit := Expr_Value_E (Choice); | |
5978 | ||
5979 | if Present (Enumeration_Rep_Expr (Elit)) then | |
5980 | Error_Msg_Sloc := | |
5981 | Sloc (Enumeration_Rep_Expr (Elit)); | |
5982 | Error_Msg_NE | |
5983 | ("representation for& previously given#", | |
5984 | Choice, Elit); | |
5985 | Err := True; | |
5986 | end if; | |
d6f39728 | 5987 | |
098d3082 | 5988 | Set_Enumeration_Rep_Expr (Elit, Expression (Assoc)); |
d6f39728 | 5989 | |
098d3082 | 5990 | Expr := Expression (Assoc); |
5991 | Val := Static_Integer (Expr); | |
d6f39728 | 5992 | |
098d3082 | 5993 | if Val = No_Uint then |
5994 | Err := True; | |
5995 | ||
5996 | elsif Val < Lo or else Hi < Val then | |
5997 | Error_Msg_N ("value outside permitted range", Expr); | |
5998 | Err := True; | |
5999 | end if; | |
d6f39728 | 6000 | |
098d3082 | 6001 | Set_Enumeration_Rep (Elit, Val); |
6002 | end if; | |
d6f39728 | 6003 | end if; |
6004 | end if; | |
6005 | end if; | |
6006 | ||
6007 | Next (Assoc); | |
6008 | end loop; | |
6009 | end if; | |
6010 | ||
6011 | -- Aggregate is fully processed. Now we check that a full set of | |
6012 | -- representations was given, and that they are in range and in order. | |
6013 | -- These checks are only done if no other errors occurred. | |
6014 | ||
6015 | if not Err then | |
6016 | Min := No_Uint; | |
6017 | Max := No_Uint; | |
6018 | ||
6019 | Elit := First_Literal (Enumtype); | |
6020 | while Present (Elit) loop | |
6021 | if No (Enumeration_Rep_Expr (Elit)) then | |
6022 | Error_Msg_NE ("missing representation for&!", N, Elit); | |
6023 | ||
6024 | else | |
6025 | Val := Enumeration_Rep (Elit); | |
6026 | ||
6027 | if Min = No_Uint then | |
6028 | Min := Val; | |
6029 | end if; | |
6030 | ||
6031 | if Val /= No_Uint then | |
6032 | if Max /= No_Uint and then Val <= Max then | |
6033 | Error_Msg_NE | |
6034 | ("enumeration value for& not ordered!", | |
e30c7d84 | 6035 | Enumeration_Rep_Expr (Elit), Elit); |
d6f39728 | 6036 | end if; |
6037 | ||
e30c7d84 | 6038 | Max_Node := Enumeration_Rep_Expr (Elit); |
d6f39728 | 6039 | Max := Val; |
6040 | end if; | |
6041 | ||
e30c7d84 | 6042 | -- If there is at least one literal whose representation is not |
6043 | -- equal to the Pos value, then note that this enumeration type | |
6044 | -- has a non-standard representation. | |
d6f39728 | 6045 | |
6046 | if Val /= Enumeration_Pos (Elit) then | |
6047 | Set_Has_Non_Standard_Rep (Base_Type (Enumtype)); | |
6048 | end if; | |
6049 | end if; | |
6050 | ||
6051 | Next (Elit); | |
6052 | end loop; | |
6053 | ||
6054 | -- Now set proper size information | |
6055 | ||
6056 | declare | |
6057 | Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype)); | |
6058 | ||
6059 | begin | |
6060 | if Has_Size_Clause (Enumtype) then | |
e30c7d84 | 6061 | |
6062 | -- All OK, if size is OK now | |
6063 | ||
6064 | if RM_Size (Enumtype) >= Minsize then | |
d6f39728 | 6065 | null; |
6066 | ||
6067 | else | |
e30c7d84 | 6068 | -- Try if we can get by with biasing |
6069 | ||
d6f39728 | 6070 | Minsize := |
6071 | UI_From_Int (Minimum_Size (Enumtype, Biased => True)); | |
6072 | ||
e30c7d84 | 6073 | -- Error message if even biasing does not work |
6074 | ||
6075 | if RM_Size (Enumtype) < Minsize then | |
6076 | Error_Msg_Uint_1 := RM_Size (Enumtype); | |
6077 | Error_Msg_Uint_2 := Max; | |
6078 | Error_Msg_N | |
6079 | ("previously given size (^) is too small " | |
6080 | & "for this value (^)", Max_Node); | |
6081 | ||
6082 | -- If biasing worked, indicate that we now have biased rep | |
d6f39728 | 6083 | |
6084 | else | |
b77e4501 | 6085 | Set_Biased |
6086 | (Enumtype, Size_Clause (Enumtype), "size clause"); | |
d6f39728 | 6087 | end if; |
6088 | end if; | |
6089 | ||
6090 | else | |
6091 | Set_RM_Size (Enumtype, Minsize); | |
6092 | Set_Enum_Esize (Enumtype); | |
6093 | end if; | |
6094 | ||
6095 | Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype)); | |
6096 | Set_Esize (Base_Type (Enumtype), Esize (Enumtype)); | |
6097 | Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype)); | |
6098 | end; | |
6099 | end if; | |
6100 | ||
39a0c1d3 | 6101 | -- We repeat the too late test in case it froze itself |
d6f39728 | 6102 | |
6103 | if Rep_Item_Too_Late (Enumtype, N) then | |
6104 | null; | |
6105 | end if; | |
d6f39728 | 6106 | end Analyze_Enumeration_Representation_Clause; |
6107 | ||
6108 | ---------------------------- | |
6109 | -- Analyze_Free_Statement -- | |
6110 | ---------------------------- | |
6111 | ||
6112 | procedure Analyze_Free_Statement (N : Node_Id) is | |
6113 | begin | |
6114 | Analyze (Expression (N)); | |
6115 | end Analyze_Free_Statement; | |
6116 | ||
40ca69b9 | 6117 | --------------------------- |
6118 | -- Analyze_Freeze_Entity -- | |
6119 | --------------------------- | |
6120 | ||
6121 | procedure Analyze_Freeze_Entity (N : Node_Id) is | |
40ca69b9 | 6122 | begin |
d9f6a4ee | 6123 | Freeze_Entity_Checks (N); |
6124 | end Analyze_Freeze_Entity; | |
98f7db28 | 6125 | |
d9f6a4ee | 6126 | ----------------------------------- |
6127 | -- Analyze_Freeze_Generic_Entity -- | |
6128 | ----------------------------------- | |
98f7db28 | 6129 | |
d9f6a4ee | 6130 | procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is |
6131 | begin | |
6132 | Freeze_Entity_Checks (N); | |
6133 | end Analyze_Freeze_Generic_Entity; | |
40ca69b9 | 6134 | |
d9f6a4ee | 6135 | ------------------------------------------ |
6136 | -- Analyze_Record_Representation_Clause -- | |
6137 | ------------------------------------------ | |
c8da6114 | 6138 | |
d9f6a4ee | 6139 | -- Note: we check as much as we can here, but we can't do any checks |
6140 | -- based on the position values (e.g. overlap checks) until freeze time | |
6141 | -- because especially in Ada 2005 (machine scalar mode), the processing | |
6142 | -- for non-standard bit order can substantially change the positions. | |
6143 | -- See procedure Check_Record_Representation_Clause (called from Freeze) | |
6144 | -- for the remainder of this processing. | |
d00681a7 | 6145 | |
d9f6a4ee | 6146 | procedure Analyze_Record_Representation_Clause (N : Node_Id) is |
6147 | Ident : constant Node_Id := Identifier (N); | |
6148 | Biased : Boolean; | |
6149 | CC : Node_Id; | |
6150 | Comp : Entity_Id; | |
6151 | Fbit : Uint; | |
6152 | Hbit : Uint := Uint_0; | |
6153 | Lbit : Uint; | |
6154 | Ocomp : Entity_Id; | |
6155 | Posit : Uint; | |
6156 | Rectype : Entity_Id; | |
6157 | Recdef : Node_Id; | |
d00681a7 | 6158 | |
d9f6a4ee | 6159 | function Is_Inherited (Comp : Entity_Id) return Boolean; |
6160 | -- True if Comp is an inherited component in a record extension | |
d00681a7 | 6161 | |
d9f6a4ee | 6162 | ------------------ |
6163 | -- Is_Inherited -- | |
6164 | ------------------ | |
d00681a7 | 6165 | |
d9f6a4ee | 6166 | function Is_Inherited (Comp : Entity_Id) return Boolean is |
6167 | Comp_Base : Entity_Id; | |
d00681a7 | 6168 | |
d9f6a4ee | 6169 | begin |
6170 | if Ekind (Rectype) = E_Record_Subtype then | |
6171 | Comp_Base := Original_Record_Component (Comp); | |
6172 | else | |
6173 | Comp_Base := Comp; | |
d00681a7 | 6174 | end if; |
6175 | ||
d9f6a4ee | 6176 | return Comp_Base /= Original_Record_Component (Comp_Base); |
6177 | end Is_Inherited; | |
d00681a7 | 6178 | |
d9f6a4ee | 6179 | -- Local variables |
d00681a7 | 6180 | |
d9f6a4ee | 6181 | Is_Record_Extension : Boolean; |
6182 | -- True if Rectype is a record extension | |
d00681a7 | 6183 | |
d9f6a4ee | 6184 | CR_Pragma : Node_Id := Empty; |
6185 | -- Points to N_Pragma node if Complete_Representation pragma present | |
d00681a7 | 6186 | |
d9f6a4ee | 6187 | -- Start of processing for Analyze_Record_Representation_Clause |
d00681a7 | 6188 | |
d9f6a4ee | 6189 | begin |
6190 | if Ignore_Rep_Clauses then | |
2ff55065 | 6191 | Kill_Rep_Clause (N); |
d9f6a4ee | 6192 | return; |
d00681a7 | 6193 | end if; |
98f7db28 | 6194 | |
d9f6a4ee | 6195 | Find_Type (Ident); |
6196 | Rectype := Entity (Ident); | |
85377c9b | 6197 | |
d9f6a4ee | 6198 | if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then |
6199 | return; | |
6200 | else | |
6201 | Rectype := Underlying_Type (Rectype); | |
6202 | end if; | |
85377c9b | 6203 | |
d9f6a4ee | 6204 | -- First some basic error checks |
85377c9b | 6205 | |
d9f6a4ee | 6206 | if not Is_Record_Type (Rectype) then |
6207 | Error_Msg_NE | |
6208 | ("record type required, found}", Ident, First_Subtype (Rectype)); | |
6209 | return; | |
85377c9b | 6210 | |
d9f6a4ee | 6211 | elsif Scope (Rectype) /= Current_Scope then |
6212 | Error_Msg_N ("type must be declared in this scope", N); | |
6213 | return; | |
85377c9b | 6214 | |
d9f6a4ee | 6215 | elsif not Is_First_Subtype (Rectype) then |
6216 | Error_Msg_N ("cannot give record rep clause for subtype", N); | |
6217 | return; | |
9dc88aea | 6218 | |
d9f6a4ee | 6219 | elsif Has_Record_Rep_Clause (Rectype) then |
6220 | Error_Msg_N ("duplicate record rep clause ignored", N); | |
6221 | return; | |
9dc88aea | 6222 | |
d9f6a4ee | 6223 | elsif Rep_Item_Too_Late (Rectype, N) then |
6224 | return; | |
9dc88aea | 6225 | end if; |
fb7f2fc4 | 6226 | |
d9f6a4ee | 6227 | -- We know we have a first subtype, now possibly go the the anonymous |
6228 | -- base type to determine whether Rectype is a record extension. | |
89f1e35c | 6229 | |
d9f6a4ee | 6230 | Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype))); |
6231 | Is_Record_Extension := | |
6232 | Nkind (Recdef) = N_Derived_Type_Definition | |
6233 | and then Present (Record_Extension_Part (Recdef)); | |
89f1e35c | 6234 | |
d9f6a4ee | 6235 | if Present (Mod_Clause (N)) then |
fb7f2fc4 | 6236 | declare |
d9f6a4ee | 6237 | Loc : constant Source_Ptr := Sloc (N); |
6238 | M : constant Node_Id := Mod_Clause (N); | |
6239 | P : constant List_Id := Pragmas_Before (M); | |
6240 | AtM_Nod : Node_Id; | |
6241 | ||
6242 | Mod_Val : Uint; | |
6243 | pragma Warnings (Off, Mod_Val); | |
fb7f2fc4 | 6244 | |
6245 | begin | |
d9f6a4ee | 6246 | Check_Restriction (No_Obsolescent_Features, Mod_Clause (N)); |
fb7f2fc4 | 6247 | |
d9f6a4ee | 6248 | if Warn_On_Obsolescent_Feature then |
6249 | Error_Msg_N | |
6250 | ("?j?mod clause is an obsolescent feature (RM J.8)", N); | |
6251 | Error_Msg_N | |
6252 | ("\?j?use alignment attribute definition clause instead", N); | |
6253 | end if; | |
fb7f2fc4 | 6254 | |
d9f6a4ee | 6255 | if Present (P) then |
6256 | Analyze_List (P); | |
6257 | end if; | |
89f1e35c | 6258 | |
d9f6a4ee | 6259 | -- In ASIS_Mode mode, expansion is disabled, but we must convert |
6260 | -- the Mod clause into an alignment clause anyway, so that the | |
6261 | -- back-end can compute and back-annotate properly the size and | |
6262 | -- alignment of types that may include this record. | |
be9124d0 | 6263 | |
d9f6a4ee | 6264 | -- This seems dubious, this destroys the source tree in a manner |
6265 | -- not detectable by ASIS ??? | |
be9124d0 | 6266 | |
d9f6a4ee | 6267 | if Operating_Mode = Check_Semantics and then ASIS_Mode then |
6268 | AtM_Nod := | |
6269 | Make_Attribute_Definition_Clause (Loc, | |
83c6c069 | 6270 | Name => New_Occurrence_Of (Base_Type (Rectype), Loc), |
d9f6a4ee | 6271 | Chars => Name_Alignment, |
6272 | Expression => Relocate_Node (Expression (M))); | |
be9124d0 | 6273 | |
d9f6a4ee | 6274 | Set_From_At_Mod (AtM_Nod); |
6275 | Insert_After (N, AtM_Nod); | |
6276 | Mod_Val := Get_Alignment_Value (Expression (AtM_Nod)); | |
6277 | Set_Mod_Clause (N, Empty); | |
be9124d0 | 6278 | |
d9f6a4ee | 6279 | else |
6280 | -- Get the alignment value to perform error checking | |
be9124d0 | 6281 | |
d9f6a4ee | 6282 | Mod_Val := Get_Alignment_Value (Expression (M)); |
6283 | end if; | |
6284 | end; | |
6285 | end if; | |
be9124d0 | 6286 | |
d9f6a4ee | 6287 | -- For untagged types, clear any existing component clauses for the |
6288 | -- type. If the type is derived, this is what allows us to override | |
6289 | -- a rep clause for the parent. For type extensions, the representation | |
6290 | -- of the inherited components is inherited, so we want to keep previous | |
6291 | -- component clauses for completeness. | |
be9124d0 | 6292 | |
d9f6a4ee | 6293 | if not Is_Tagged_Type (Rectype) then |
6294 | Comp := First_Component_Or_Discriminant (Rectype); | |
6295 | while Present (Comp) loop | |
6296 | Set_Component_Clause (Comp, Empty); | |
6297 | Next_Component_Or_Discriminant (Comp); | |
6298 | end loop; | |
6299 | end if; | |
be9124d0 | 6300 | |
d9f6a4ee | 6301 | -- All done if no component clauses |
be9124d0 | 6302 | |
d9f6a4ee | 6303 | CC := First (Component_Clauses (N)); |
be9124d0 | 6304 | |
d9f6a4ee | 6305 | if No (CC) then |
6306 | return; | |
6307 | end if; | |
be9124d0 | 6308 | |
d9f6a4ee | 6309 | -- A representation like this applies to the base type |
be9124d0 | 6310 | |
d9f6a4ee | 6311 | Set_Has_Record_Rep_Clause (Base_Type (Rectype)); |
6312 | Set_Has_Non_Standard_Rep (Base_Type (Rectype)); | |
6313 | Set_Has_Specified_Layout (Base_Type (Rectype)); | |
be9124d0 | 6314 | |
d9f6a4ee | 6315 | -- Process the component clauses |
be9124d0 | 6316 | |
d9f6a4ee | 6317 | while Present (CC) loop |
be9124d0 | 6318 | |
d9f6a4ee | 6319 | -- Pragma |
be9124d0 | 6320 | |
d9f6a4ee | 6321 | if Nkind (CC) = N_Pragma then |
6322 | Analyze (CC); | |
be9124d0 | 6323 | |
d9f6a4ee | 6324 | -- The only pragma of interest is Complete_Representation |
be9124d0 | 6325 | |
d9f6a4ee | 6326 | if Pragma_Name (CC) = Name_Complete_Representation then |
6327 | CR_Pragma := CC; | |
6328 | end if; | |
be9124d0 | 6329 | |
d9f6a4ee | 6330 | -- Processing for real component clause |
be9124d0 | 6331 | |
d9f6a4ee | 6332 | else |
6333 | Posit := Static_Integer (Position (CC)); | |
6334 | Fbit := Static_Integer (First_Bit (CC)); | |
6335 | Lbit := Static_Integer (Last_Bit (CC)); | |
be9124d0 | 6336 | |
d9f6a4ee | 6337 | if Posit /= No_Uint |
6338 | and then Fbit /= No_Uint | |
6339 | and then Lbit /= No_Uint | |
6340 | then | |
6341 | if Posit < 0 then | |
6342 | Error_Msg_N | |
6343 | ("position cannot be negative", Position (CC)); | |
be9124d0 | 6344 | |
d9f6a4ee | 6345 | elsif Fbit < 0 then |
6346 | Error_Msg_N | |
6347 | ("first bit cannot be negative", First_Bit (CC)); | |
be9124d0 | 6348 | |
d9f6a4ee | 6349 | -- The Last_Bit specified in a component clause must not be |
6350 | -- less than the First_Bit minus one (RM-13.5.1(10)). | |
be9124d0 | 6351 | |
d9f6a4ee | 6352 | elsif Lbit < Fbit - 1 then |
6353 | Error_Msg_N | |
6354 | ("last bit cannot be less than first bit minus one", | |
6355 | Last_Bit (CC)); | |
be9124d0 | 6356 | |
d9f6a4ee | 6357 | -- Values look OK, so find the corresponding record component |
6358 | -- Even though the syntax allows an attribute reference for | |
6359 | -- implementation-defined components, GNAT does not allow the | |
6360 | -- tag to get an explicit position. | |
be9124d0 | 6361 | |
d9f6a4ee | 6362 | elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then |
6363 | if Attribute_Name (Component_Name (CC)) = Name_Tag then | |
6364 | Error_Msg_N ("position of tag cannot be specified", CC); | |
6365 | else | |
6366 | Error_Msg_N ("illegal component name", CC); | |
6367 | end if; | |
be9124d0 | 6368 | |
d9f6a4ee | 6369 | else |
6370 | Comp := First_Entity (Rectype); | |
6371 | while Present (Comp) loop | |
6372 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
6373 | Next_Entity (Comp); | |
6374 | end loop; | |
be9124d0 | 6375 | |
d9f6a4ee | 6376 | if No (Comp) then |
be9124d0 | 6377 | |
d9f6a4ee | 6378 | -- Maybe component of base type that is absent from |
6379 | -- statically constrained first subtype. | |
be9124d0 | 6380 | |
d9f6a4ee | 6381 | Comp := First_Entity (Base_Type (Rectype)); |
6382 | while Present (Comp) loop | |
6383 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
6384 | Next_Entity (Comp); | |
6385 | end loop; | |
6386 | end if; | |
be9124d0 | 6387 | |
d9f6a4ee | 6388 | if No (Comp) then |
6389 | Error_Msg_N | |
6390 | ("component clause is for non-existent field", CC); | |
be9124d0 | 6391 | |
d9f6a4ee | 6392 | -- Ada 2012 (AI05-0026): Any name that denotes a |
6393 | -- discriminant of an object of an unchecked union type | |
6394 | -- shall not occur within a record_representation_clause. | |
be9124d0 | 6395 | |
d9f6a4ee | 6396 | -- The general restriction of using record rep clauses on |
6397 | -- Unchecked_Union types has now been lifted. Since it is | |
6398 | -- possible to introduce a record rep clause which mentions | |
6399 | -- the discriminant of an Unchecked_Union in non-Ada 2012 | |
6400 | -- code, this check is applied to all versions of the | |
6401 | -- language. | |
be9124d0 | 6402 | |
d9f6a4ee | 6403 | elsif Ekind (Comp) = E_Discriminant |
6404 | and then Is_Unchecked_Union (Rectype) | |
6405 | then | |
6406 | Error_Msg_N | |
6407 | ("cannot reference discriminant of unchecked union", | |
6408 | Component_Name (CC)); | |
be9124d0 | 6409 | |
d9f6a4ee | 6410 | elsif Is_Record_Extension and then Is_Inherited (Comp) then |
6411 | Error_Msg_NE | |
6412 | ("component clause not allowed for inherited " | |
6413 | & "component&", CC, Comp); | |
40ca69b9 | 6414 | |
d9f6a4ee | 6415 | elsif Present (Component_Clause (Comp)) then |
462a079f | 6416 | |
d9f6a4ee | 6417 | -- Diagnose duplicate rep clause, or check consistency |
6418 | -- if this is an inherited component. In a double fault, | |
6419 | -- there may be a duplicate inconsistent clause for an | |
6420 | -- inherited component. | |
462a079f | 6421 | |
d9f6a4ee | 6422 | if Scope (Original_Record_Component (Comp)) = Rectype |
6423 | or else Parent (Component_Clause (Comp)) = N | |
6424 | then | |
6425 | Error_Msg_Sloc := Sloc (Component_Clause (Comp)); | |
6426 | Error_Msg_N ("component clause previously given#", CC); | |
3062c401 | 6427 | |
6428 | else | |
6429 | declare | |
6430 | Rep1 : constant Node_Id := Component_Clause (Comp); | |
3062c401 | 6431 | begin |
6432 | if Intval (Position (Rep1)) /= | |
6433 | Intval (Position (CC)) | |
6434 | or else Intval (First_Bit (Rep1)) /= | |
6435 | Intval (First_Bit (CC)) | |
6436 | or else Intval (Last_Bit (Rep1)) /= | |
6437 | Intval (Last_Bit (CC)) | |
6438 | then | |
b9e61b2a | 6439 | Error_Msg_N |
6440 | ("component clause inconsistent " | |
6441 | & "with representation of ancestor", CC); | |
6a06584c | 6442 | |
3062c401 | 6443 | elsif Warn_On_Redundant_Constructs then |
b9e61b2a | 6444 | Error_Msg_N |
6a06584c | 6445 | ("?r?redundant confirming component clause " |
6446 | & "for component!", CC); | |
3062c401 | 6447 | end if; |
6448 | end; | |
6449 | end if; | |
d6f39728 | 6450 | |
d2b860b4 | 6451 | -- Normal case where this is the first component clause we |
6452 | -- have seen for this entity, so set it up properly. | |
6453 | ||
d6f39728 | 6454 | else |
83f8f0a6 | 6455 | -- Make reference for field in record rep clause and set |
6456 | -- appropriate entity field in the field identifier. | |
6457 | ||
6458 | Generate_Reference | |
6459 | (Comp, Component_Name (CC), Set_Ref => False); | |
6460 | Set_Entity (Component_Name (CC), Comp); | |
6461 | ||
2866d595 | 6462 | -- Update Fbit and Lbit to the actual bit number |
d6f39728 | 6463 | |
6464 | Fbit := Fbit + UI_From_Int (SSU) * Posit; | |
6465 | Lbit := Lbit + UI_From_Int (SSU) * Posit; | |
6466 | ||
d6f39728 | 6467 | if Has_Size_Clause (Rectype) |
ada34def | 6468 | and then RM_Size (Rectype) <= Lbit |
d6f39728 | 6469 | then |
6470 | Error_Msg_N | |
6471 | ("bit number out of range of specified size", | |
6472 | Last_Bit (CC)); | |
6473 | else | |
6474 | Set_Component_Clause (Comp, CC); | |
6475 | Set_Component_Bit_Offset (Comp, Fbit); | |
6476 | Set_Esize (Comp, 1 + (Lbit - Fbit)); | |
6477 | Set_Normalized_First_Bit (Comp, Fbit mod SSU); | |
6478 | Set_Normalized_Position (Comp, Fbit / SSU); | |
6479 | ||
a0fc8c5b | 6480 | if Warn_On_Overridden_Size |
6481 | and then Has_Size_Clause (Etype (Comp)) | |
6482 | and then RM_Size (Etype (Comp)) /= Esize (Comp) | |
6483 | then | |
6484 | Error_Msg_NE | |
1e3532e7 | 6485 | ("?S?component size overrides size clause for&", |
a0fc8c5b | 6486 | Component_Name (CC), Etype (Comp)); |
6487 | end if; | |
6488 | ||
ea61a7ea | 6489 | -- This information is also set in the corresponding |
6490 | -- component of the base type, found by accessing the | |
6491 | -- Original_Record_Component link if it is present. | |
d6f39728 | 6492 | |
6493 | Ocomp := Original_Record_Component (Comp); | |
6494 | ||
6495 | if Hbit < Lbit then | |
6496 | Hbit := Lbit; | |
6497 | end if; | |
6498 | ||
6499 | Check_Size | |
6500 | (Component_Name (CC), | |
6501 | Etype (Comp), | |
6502 | Esize (Comp), | |
6503 | Biased); | |
6504 | ||
b77e4501 | 6505 | Set_Biased |
6506 | (Comp, First_Node (CC), "component clause", Biased); | |
cc46ff4b | 6507 | |
d6f39728 | 6508 | if Present (Ocomp) then |
6509 | Set_Component_Clause (Ocomp, CC); | |
6510 | Set_Component_Bit_Offset (Ocomp, Fbit); | |
6511 | Set_Normalized_First_Bit (Ocomp, Fbit mod SSU); | |
6512 | Set_Normalized_Position (Ocomp, Fbit / SSU); | |
6513 | Set_Esize (Ocomp, 1 + (Lbit - Fbit)); | |
6514 | ||
6515 | Set_Normalized_Position_Max | |
6516 | (Ocomp, Normalized_Position (Ocomp)); | |
6517 | ||
b77e4501 | 6518 | -- Note: we don't use Set_Biased here, because we |
6519 | -- already gave a warning above if needed, and we | |
6520 | -- would get a duplicate for the same name here. | |
6521 | ||
d6f39728 | 6522 | Set_Has_Biased_Representation |
6523 | (Ocomp, Has_Biased_Representation (Comp)); | |
6524 | end if; | |
6525 | ||
6526 | if Esize (Comp) < 0 then | |
6527 | Error_Msg_N ("component size is negative", CC); | |
6528 | end if; | |
6529 | end if; | |
6530 | end if; | |
6531 | end if; | |
6532 | end if; | |
6533 | end if; | |
6534 | ||
6535 | Next (CC); | |
6536 | end loop; | |
6537 | ||
67278d60 | 6538 | -- Check missing components if Complete_Representation pragma appeared |
d6f39728 | 6539 | |
67278d60 | 6540 | if Present (CR_Pragma) then |
6541 | Comp := First_Component_Or_Discriminant (Rectype); | |
6542 | while Present (Comp) loop | |
6543 | if No (Component_Clause (Comp)) then | |
6544 | Error_Msg_NE | |
6545 | ("missing component clause for &", CR_Pragma, Comp); | |
6546 | end if; | |
d6f39728 | 6547 | |
67278d60 | 6548 | Next_Component_Or_Discriminant (Comp); |
6549 | end loop; | |
d6f39728 | 6550 | |
1e3532e7 | 6551 | -- Give missing components warning if required |
15ebb600 | 6552 | |
fdd294d1 | 6553 | elsif Warn_On_Unrepped_Components then |
15ebb600 | 6554 | declare |
6555 | Num_Repped_Components : Nat := 0; | |
6556 | Num_Unrepped_Components : Nat := 0; | |
6557 | ||
6558 | begin | |
6559 | -- First count number of repped and unrepped components | |
6560 | ||
6561 | Comp := First_Component_Or_Discriminant (Rectype); | |
6562 | while Present (Comp) loop | |
6563 | if Present (Component_Clause (Comp)) then | |
6564 | Num_Repped_Components := Num_Repped_Components + 1; | |
6565 | else | |
6566 | Num_Unrepped_Components := Num_Unrepped_Components + 1; | |
6567 | end if; | |
6568 | ||
6569 | Next_Component_Or_Discriminant (Comp); | |
6570 | end loop; | |
6571 | ||
6572 | -- We are only interested in the case where there is at least one | |
6573 | -- unrepped component, and at least half the components have rep | |
6574 | -- clauses. We figure that if less than half have them, then the | |
87f9eef5 | 6575 | -- partial rep clause is really intentional. If the component |
6576 | -- type has no underlying type set at this point (as for a generic | |
6577 | -- formal type), we don't know enough to give a warning on the | |
6578 | -- component. | |
15ebb600 | 6579 | |
6580 | if Num_Unrepped_Components > 0 | |
6581 | and then Num_Unrepped_Components < Num_Repped_Components | |
6582 | then | |
6583 | Comp := First_Component_Or_Discriminant (Rectype); | |
6584 | while Present (Comp) loop | |
83f8f0a6 | 6585 | if No (Component_Clause (Comp)) |
3062c401 | 6586 | and then Comes_From_Source (Comp) |
87f9eef5 | 6587 | and then Present (Underlying_Type (Etype (Comp))) |
83f8f0a6 | 6588 | and then (Is_Scalar_Type (Underlying_Type (Etype (Comp))) |
67278d60 | 6589 | or else Size_Known_At_Compile_Time |
6590 | (Underlying_Type (Etype (Comp)))) | |
fdd294d1 | 6591 | and then not Has_Warnings_Off (Rectype) |
83f8f0a6 | 6592 | then |
15ebb600 | 6593 | Error_Msg_Sloc := Sloc (Comp); |
6594 | Error_Msg_NE | |
1e3532e7 | 6595 | ("?C?no component clause given for & declared #", |
15ebb600 | 6596 | N, Comp); |
6597 | end if; | |
6598 | ||
6599 | Next_Component_Or_Discriminant (Comp); | |
6600 | end loop; | |
6601 | end if; | |
6602 | end; | |
d6f39728 | 6603 | end if; |
d6f39728 | 6604 | end Analyze_Record_Representation_Clause; |
6605 | ||
eb66e842 | 6606 | ------------------------------------- |
6607 | -- Build_Discrete_Static_Predicate -- | |
6608 | ------------------------------------- | |
9ea61fdd | 6609 | |
eb66e842 | 6610 | procedure Build_Discrete_Static_Predicate |
6611 | (Typ : Entity_Id; | |
6612 | Expr : Node_Id; | |
6613 | Nam : Name_Id) | |
9ea61fdd | 6614 | is |
eb66e842 | 6615 | Loc : constant Source_Ptr := Sloc (Expr); |
9ea61fdd | 6616 | |
eb66e842 | 6617 | Non_Static : exception; |
6618 | -- Raised if something non-static is found | |
9ea61fdd | 6619 | |
eb66e842 | 6620 | Btyp : constant Entity_Id := Base_Type (Typ); |
9ea61fdd | 6621 | |
eb66e842 | 6622 | BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp)); |
6623 | BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp)); | |
6624 | -- Low bound and high bound value of base type of Typ | |
9ea61fdd | 6625 | |
eb66e842 | 6626 | TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ)); |
6627 | THi : constant Uint := Expr_Value (Type_High_Bound (Typ)); | |
6628 | -- Low bound and high bound values of static subtype Typ | |
9ea61fdd | 6629 | |
eb66e842 | 6630 | type REnt is record |
6631 | Lo, Hi : Uint; | |
6632 | end record; | |
6633 | -- One entry in a Rlist value, a single REnt (range entry) value denotes | |
6634 | -- one range from Lo to Hi. To represent a single value range Lo = Hi = | |
6635 | -- value. | |
9ea61fdd | 6636 | |
eb66e842 | 6637 | type RList is array (Nat range <>) of REnt; |
6638 | -- A list of ranges. The ranges are sorted in increasing order, and are | |
6639 | -- disjoint (there is a gap of at least one value between each range in | |
6640 | -- the table). A value is in the set of ranges in Rlist if it lies | |
6641 | -- within one of these ranges. | |
9ea61fdd | 6642 | |
eb66e842 | 6643 | False_Range : constant RList := |
6644 | RList'(1 .. 0 => REnt'(No_Uint, No_Uint)); | |
6645 | -- An empty set of ranges represents a range list that can never be | |
6646 | -- satisfied, since there are no ranges in which the value could lie, | |
6647 | -- so it does not lie in any of them. False_Range is a canonical value | |
6648 | -- for this empty set, but general processing should test for an Rlist | |
6649 | -- with length zero (see Is_False predicate), since other null ranges | |
6650 | -- may appear which must be treated as False. | |
5b5df4a9 | 6651 | |
eb66e842 | 6652 | True_Range : constant RList := RList'(1 => REnt'(BLo, BHi)); |
6653 | -- Range representing True, value must be in the base range | |
5b5df4a9 | 6654 | |
eb66e842 | 6655 | function "and" (Left : RList; Right : RList) return RList; |
6656 | -- And's together two range lists, returning a range list. This is a set | |
6657 | -- intersection operation. | |
5b5df4a9 | 6658 | |
eb66e842 | 6659 | function "or" (Left : RList; Right : RList) return RList; |
6660 | -- Or's together two range lists, returning a range list. This is a set | |
6661 | -- union operation. | |
87f3d5d3 | 6662 | |
eb66e842 | 6663 | function "not" (Right : RList) return RList; |
6664 | -- Returns complement of a given range list, i.e. a range list | |
6665 | -- representing all the values in TLo .. THi that are not in the input | |
6666 | -- operand Right. | |
ed4adc99 | 6667 | |
eb66e842 | 6668 | function Build_Val (V : Uint) return Node_Id; |
6669 | -- Return an analyzed N_Identifier node referencing this value, suitable | |
5c6a5792 | 6670 | -- for use as an entry in the Static_Discrte_Predicate list. This node |
6671 | -- is typed with the base type. | |
5b5df4a9 | 6672 | |
eb66e842 | 6673 | function Build_Range (Lo : Uint; Hi : Uint) return Node_Id; |
6674 | -- Return an analyzed N_Range node referencing this range, suitable for | |
5c6a5792 | 6675 | -- use as an entry in the Static_Discrete_Predicate list. This node is |
6676 | -- typed with the base type. | |
5b5df4a9 | 6677 | |
eb66e842 | 6678 | function Get_RList (Exp : Node_Id) return RList; |
6679 | -- This is a recursive routine that converts the given expression into a | |
6680 | -- list of ranges, suitable for use in building the static predicate. | |
5b5df4a9 | 6681 | |
eb66e842 | 6682 | function Is_False (R : RList) return Boolean; |
6683 | pragma Inline (Is_False); | |
6684 | -- Returns True if the given range list is empty, and thus represents a | |
6685 | -- False list of ranges that can never be satisfied. | |
87f3d5d3 | 6686 | |
eb66e842 | 6687 | function Is_True (R : RList) return Boolean; |
6688 | -- Returns True if R trivially represents the True predicate by having a | |
6689 | -- single range from BLo to BHi. | |
5b5df4a9 | 6690 | |
eb66e842 | 6691 | function Is_Type_Ref (N : Node_Id) return Boolean; |
6692 | pragma Inline (Is_Type_Ref); | |
6693 | -- Returns if True if N is a reference to the type for the predicate in | |
6694 | -- the expression (i.e. if it is an identifier whose Chars field matches | |
7de4cba3 | 6695 | -- the Nam given in the call). N must not be parenthesized, if the type |
6696 | -- name appears in parens, this routine will return False. | |
5b5df4a9 | 6697 | |
eb66e842 | 6698 | function Lo_Val (N : Node_Id) return Uint; |
5c6a5792 | 6699 | -- Given an entry from a Static_Discrete_Predicate list that is either |
6700 | -- a static expression or static range, gets either the expression value | |
6701 | -- or the low bound of the range. | |
5b5df4a9 | 6702 | |
eb66e842 | 6703 | function Hi_Val (N : Node_Id) return Uint; |
5c6a5792 | 6704 | -- Given an entry from a Static_Discrete_Predicate list that is either |
6705 | -- a static expression or static range, gets either the expression value | |
6706 | -- or the high bound of the range. | |
5b5df4a9 | 6707 | |
eb66e842 | 6708 | function Membership_Entry (N : Node_Id) return RList; |
6709 | -- Given a single membership entry (range, value, or subtype), returns | |
6710 | -- the corresponding range list. Raises Static_Error if not static. | |
5b5df4a9 | 6711 | |
eb66e842 | 6712 | function Membership_Entries (N : Node_Id) return RList; |
6713 | -- Given an element on an alternatives list of a membership operation, | |
6714 | -- returns the range list corresponding to this entry and all following | |
6715 | -- entries (i.e. returns the "or" of this list of values). | |
b9e61b2a | 6716 | |
eb66e842 | 6717 | function Stat_Pred (Typ : Entity_Id) return RList; |
6718 | -- Given a type, if it has a static predicate, then return the predicate | |
6719 | -- as a range list, otherwise raise Non_Static. | |
c4968aa2 | 6720 | |
eb66e842 | 6721 | ----------- |
6722 | -- "and" -- | |
6723 | ----------- | |
c4968aa2 | 6724 | |
eb66e842 | 6725 | function "and" (Left : RList; Right : RList) return RList is |
6726 | FEnt : REnt; | |
6727 | -- First range of result | |
c4968aa2 | 6728 | |
eb66e842 | 6729 | SLeft : Nat := Left'First; |
6730 | -- Start of rest of left entries | |
c4968aa2 | 6731 | |
eb66e842 | 6732 | SRight : Nat := Right'First; |
6733 | -- Start of rest of right entries | |
2072eaa9 | 6734 | |
eb66e842 | 6735 | begin |
6736 | -- If either range is True, return the other | |
5b5df4a9 | 6737 | |
eb66e842 | 6738 | if Is_True (Left) then |
6739 | return Right; | |
6740 | elsif Is_True (Right) then | |
6741 | return Left; | |
6742 | end if; | |
87f3d5d3 | 6743 | |
eb66e842 | 6744 | -- If either range is False, return False |
5b5df4a9 | 6745 | |
eb66e842 | 6746 | if Is_False (Left) or else Is_False (Right) then |
6747 | return False_Range; | |
6748 | end if; | |
4c1fd062 | 6749 | |
eb66e842 | 6750 | -- Loop to remove entries at start that are disjoint, and thus just |
6751 | -- get discarded from the result entirely. | |
5b5df4a9 | 6752 | |
eb66e842 | 6753 | loop |
6754 | -- If no operands left in either operand, result is false | |
5b5df4a9 | 6755 | |
eb66e842 | 6756 | if SLeft > Left'Last or else SRight > Right'Last then |
6757 | return False_Range; | |
5b5df4a9 | 6758 | |
eb66e842 | 6759 | -- Discard first left operand entry if disjoint with right |
5b5df4a9 | 6760 | |
eb66e842 | 6761 | elsif Left (SLeft).Hi < Right (SRight).Lo then |
6762 | SLeft := SLeft + 1; | |
5b5df4a9 | 6763 | |
eb66e842 | 6764 | -- Discard first right operand entry if disjoint with left |
5b5df4a9 | 6765 | |
eb66e842 | 6766 | elsif Right (SRight).Hi < Left (SLeft).Lo then |
6767 | SRight := SRight + 1; | |
5b5df4a9 | 6768 | |
eb66e842 | 6769 | -- Otherwise we have an overlapping entry |
5b5df4a9 | 6770 | |
eb66e842 | 6771 | else |
6772 | exit; | |
6773 | end if; | |
6774 | end loop; | |
5b5df4a9 | 6775 | |
eb66e842 | 6776 | -- Now we have two non-null operands, and first entries overlap. The |
6777 | -- first entry in the result will be the overlapping part of these | |
6778 | -- two entries. | |
47a46747 | 6779 | |
eb66e842 | 6780 | FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo), |
6781 | Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi)); | |
47a46747 | 6782 | |
eb66e842 | 6783 | -- Now we can remove the entry that ended at a lower value, since its |
6784 | -- contribution is entirely contained in Fent. | |
5b5df4a9 | 6785 | |
eb66e842 | 6786 | if Left (SLeft).Hi <= Right (SRight).Hi then |
6787 | SLeft := SLeft + 1; | |
6788 | else | |
6789 | SRight := SRight + 1; | |
6790 | end if; | |
5b5df4a9 | 6791 | |
eb66e842 | 6792 | -- Compute result by concatenating this first entry with the "and" of |
6793 | -- the remaining parts of the left and right operands. Note that if | |
6794 | -- either of these is empty, "and" will yield empty, so that we will | |
6795 | -- end up with just Fent, which is what we want in that case. | |
5b5df4a9 | 6796 | |
eb66e842 | 6797 | return |
6798 | FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last)); | |
6799 | end "and"; | |
fb7f2fc4 | 6800 | |
eb66e842 | 6801 | ----------- |
6802 | -- "not" -- | |
6803 | ----------- | |
fb7f2fc4 | 6804 | |
eb66e842 | 6805 | function "not" (Right : RList) return RList is |
6806 | begin | |
6807 | -- Return True if False range | |
fb7f2fc4 | 6808 | |
eb66e842 | 6809 | if Is_False (Right) then |
6810 | return True_Range; | |
6811 | end if; | |
ed4adc99 | 6812 | |
eb66e842 | 6813 | -- Return False if True range |
fb7f2fc4 | 6814 | |
eb66e842 | 6815 | if Is_True (Right) then |
6816 | return False_Range; | |
6817 | end if; | |
fb7f2fc4 | 6818 | |
eb66e842 | 6819 | -- Here if not trivial case |
87f3d5d3 | 6820 | |
eb66e842 | 6821 | declare |
6822 | Result : RList (1 .. Right'Length + 1); | |
6823 | -- May need one more entry for gap at beginning and end | |
87f3d5d3 | 6824 | |
eb66e842 | 6825 | Count : Nat := 0; |
6826 | -- Number of entries stored in Result | |
4098232e | 6827 | |
eb66e842 | 6828 | begin |
6829 | -- Gap at start | |
4098232e | 6830 | |
eb66e842 | 6831 | if Right (Right'First).Lo > TLo then |
6832 | Count := Count + 1; | |
6833 | Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1); | |
6834 | end if; | |
ed4adc99 | 6835 | |
eb66e842 | 6836 | -- Gaps between ranges |
ed4adc99 | 6837 | |
eb66e842 | 6838 | for J in Right'First .. Right'Last - 1 loop |
6839 | Count := Count + 1; | |
6840 | Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1); | |
6841 | end loop; | |
5b5df4a9 | 6842 | |
eb66e842 | 6843 | -- Gap at end |
5b5df4a9 | 6844 | |
eb66e842 | 6845 | if Right (Right'Last).Hi < THi then |
6846 | Count := Count + 1; | |
6847 | Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi); | |
6848 | end if; | |
5b5df4a9 | 6849 | |
eb66e842 | 6850 | return Result (1 .. Count); |
6851 | end; | |
6852 | end "not"; | |
5b5df4a9 | 6853 | |
eb66e842 | 6854 | ---------- |
6855 | -- "or" -- | |
6856 | ---------- | |
5b5df4a9 | 6857 | |
eb66e842 | 6858 | function "or" (Left : RList; Right : RList) return RList is |
6859 | FEnt : REnt; | |
6860 | -- First range of result | |
5b5df4a9 | 6861 | |
eb66e842 | 6862 | SLeft : Nat := Left'First; |
6863 | -- Start of rest of left entries | |
5b5df4a9 | 6864 | |
eb66e842 | 6865 | SRight : Nat := Right'First; |
6866 | -- Start of rest of right entries | |
5b5df4a9 | 6867 | |
eb66e842 | 6868 | begin |
6869 | -- If either range is True, return True | |
5b5df4a9 | 6870 | |
eb66e842 | 6871 | if Is_True (Left) or else Is_True (Right) then |
6872 | return True_Range; | |
6873 | end if; | |
5b5df4a9 | 6874 | |
eb66e842 | 6875 | -- If either range is False (empty), return the other |
5b5df4a9 | 6876 | |
eb66e842 | 6877 | if Is_False (Left) then |
6878 | return Right; | |
6879 | elsif Is_False (Right) then | |
6880 | return Left; | |
6881 | end if; | |
5b5df4a9 | 6882 | |
eb66e842 | 6883 | -- Initialize result first entry from left or right operand depending |
6884 | -- on which starts with the lower range. | |
5b5df4a9 | 6885 | |
eb66e842 | 6886 | if Left (SLeft).Lo < Right (SRight).Lo then |
6887 | FEnt := Left (SLeft); | |
6888 | SLeft := SLeft + 1; | |
6889 | else | |
6890 | FEnt := Right (SRight); | |
6891 | SRight := SRight + 1; | |
6892 | end if; | |
5b5df4a9 | 6893 | |
eb66e842 | 6894 | -- This loop eats ranges from left and right operands that are |
6895 | -- contiguous with the first range we are gathering. | |
9ea61fdd | 6896 | |
eb66e842 | 6897 | loop |
6898 | -- Eat first entry in left operand if contiguous or overlapped by | |
6899 | -- gathered first operand of result. | |
9ea61fdd | 6900 | |
eb66e842 | 6901 | if SLeft <= Left'Last |
6902 | and then Left (SLeft).Lo <= FEnt.Hi + 1 | |
6903 | then | |
6904 | FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi); | |
6905 | SLeft := SLeft + 1; | |
9ea61fdd | 6906 | |
eb66e842 | 6907 | -- Eat first entry in right operand if contiguous or overlapped by |
6908 | -- gathered right operand of result. | |
9ea61fdd | 6909 | |
eb66e842 | 6910 | elsif SRight <= Right'Last |
6911 | and then Right (SRight).Lo <= FEnt.Hi + 1 | |
6912 | then | |
6913 | FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi); | |
6914 | SRight := SRight + 1; | |
9ea61fdd | 6915 | |
eb66e842 | 6916 | -- All done if no more entries to eat |
5b5df4a9 | 6917 | |
eb66e842 | 6918 | else |
6919 | exit; | |
6920 | end if; | |
6921 | end loop; | |
5b5df4a9 | 6922 | |
eb66e842 | 6923 | -- Obtain result as the first entry we just computed, concatenated |
6924 | -- to the "or" of the remaining results (if one operand is empty, | |
6925 | -- this will just concatenate with the other | |
5b5df4a9 | 6926 | |
eb66e842 | 6927 | return |
6928 | FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last)); | |
6929 | end "or"; | |
5b5df4a9 | 6930 | |
eb66e842 | 6931 | ----------------- |
6932 | -- Build_Range -- | |
6933 | ----------------- | |
5b5df4a9 | 6934 | |
eb66e842 | 6935 | function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is |
6936 | Result : Node_Id; | |
5b5df4a9 | 6937 | begin |
eb66e842 | 6938 | Result := |
6939 | Make_Range (Loc, | |
6940 | Low_Bound => Build_Val (Lo), | |
6941 | High_Bound => Build_Val (Hi)); | |
6942 | Set_Etype (Result, Btyp); | |
6943 | Set_Analyzed (Result); | |
6944 | return Result; | |
6945 | end Build_Range; | |
5b5df4a9 | 6946 | |
eb66e842 | 6947 | --------------- |
6948 | -- Build_Val -- | |
6949 | --------------- | |
5b5df4a9 | 6950 | |
eb66e842 | 6951 | function Build_Val (V : Uint) return Node_Id is |
6952 | Result : Node_Id; | |
5b5df4a9 | 6953 | |
eb66e842 | 6954 | begin |
6955 | if Is_Enumeration_Type (Typ) then | |
6956 | Result := Get_Enum_Lit_From_Pos (Typ, V, Loc); | |
6957 | else | |
6958 | Result := Make_Integer_Literal (Loc, V); | |
6959 | end if; | |
5b5df4a9 | 6960 | |
eb66e842 | 6961 | Set_Etype (Result, Btyp); |
6962 | Set_Is_Static_Expression (Result); | |
6963 | Set_Analyzed (Result); | |
6964 | return Result; | |
6965 | end Build_Val; | |
87f3d5d3 | 6966 | |
eb66e842 | 6967 | --------------- |
6968 | -- Get_RList -- | |
6969 | --------------- | |
87f3d5d3 | 6970 | |
eb66e842 | 6971 | function Get_RList (Exp : Node_Id) return RList is |
6972 | Op : Node_Kind; | |
6973 | Val : Uint; | |
87f3d5d3 | 6974 | |
eb66e842 | 6975 | begin |
6976 | -- Static expression can only be true or false | |
87f3d5d3 | 6977 | |
eb66e842 | 6978 | if Is_OK_Static_Expression (Exp) then |
6979 | if Expr_Value (Exp) = 0 then | |
6980 | return False_Range; | |
6981 | else | |
6982 | return True_Range; | |
9ea61fdd | 6983 | end if; |
eb66e842 | 6984 | end if; |
87f3d5d3 | 6985 | |
eb66e842 | 6986 | -- Otherwise test node type |
192b8dab | 6987 | |
eb66e842 | 6988 | Op := Nkind (Exp); |
192b8dab | 6989 | |
eb66e842 | 6990 | case Op is |
5d3fb947 | 6991 | |
eb66e842 | 6992 | -- And |
5d3fb947 | 6993 | |
eb66e842 | 6994 | when N_Op_And | N_And_Then => |
6995 | return Get_RList (Left_Opnd (Exp)) | |
6996 | and | |
6997 | Get_RList (Right_Opnd (Exp)); | |
5b5df4a9 | 6998 | |
eb66e842 | 6999 | -- Or |
9dc88aea | 7000 | |
eb66e842 | 7001 | when N_Op_Or | N_Or_Else => |
7002 | return Get_RList (Left_Opnd (Exp)) | |
7003 | or | |
7004 | Get_RList (Right_Opnd (Exp)); | |
7c443ae8 | 7005 | |
eb66e842 | 7006 | -- Not |
9dc88aea | 7007 | |
eb66e842 | 7008 | when N_Op_Not => |
7009 | return not Get_RList (Right_Opnd (Exp)); | |
9dc88aea | 7010 | |
eb66e842 | 7011 | -- Comparisons of type with static value |
84c8f0b8 | 7012 | |
eb66e842 | 7013 | when N_Op_Compare => |
490beba6 | 7014 | |
eb66e842 | 7015 | -- Type is left operand |
9dc88aea | 7016 | |
eb66e842 | 7017 | if Is_Type_Ref (Left_Opnd (Exp)) |
7018 | and then Is_OK_Static_Expression (Right_Opnd (Exp)) | |
7019 | then | |
7020 | Val := Expr_Value (Right_Opnd (Exp)); | |
84c8f0b8 | 7021 | |
eb66e842 | 7022 | -- Typ is right operand |
84c8f0b8 | 7023 | |
eb66e842 | 7024 | elsif Is_Type_Ref (Right_Opnd (Exp)) |
7025 | and then Is_OK_Static_Expression (Left_Opnd (Exp)) | |
7026 | then | |
7027 | Val := Expr_Value (Left_Opnd (Exp)); | |
84c8f0b8 | 7028 | |
eb66e842 | 7029 | -- Invert sense of comparison |
84c8f0b8 | 7030 | |
eb66e842 | 7031 | case Op is |
7032 | when N_Op_Gt => Op := N_Op_Lt; | |
7033 | when N_Op_Lt => Op := N_Op_Gt; | |
7034 | when N_Op_Ge => Op := N_Op_Le; | |
7035 | when N_Op_Le => Op := N_Op_Ge; | |
7036 | when others => null; | |
7037 | end case; | |
84c8f0b8 | 7038 | |
eb66e842 | 7039 | -- Other cases are non-static |
34d045d3 | 7040 | |
eb66e842 | 7041 | else |
7042 | raise Non_Static; | |
7043 | end if; | |
9dc88aea | 7044 | |
eb66e842 | 7045 | -- Construct range according to comparison operation |
9dc88aea | 7046 | |
eb66e842 | 7047 | case Op is |
7048 | when N_Op_Eq => | |
7049 | return RList'(1 => REnt'(Val, Val)); | |
9dc88aea | 7050 | |
eb66e842 | 7051 | when N_Op_Ge => |
7052 | return RList'(1 => REnt'(Val, BHi)); | |
84c8f0b8 | 7053 | |
eb66e842 | 7054 | when N_Op_Gt => |
7055 | return RList'(1 => REnt'(Val + 1, BHi)); | |
84c8f0b8 | 7056 | |
eb66e842 | 7057 | when N_Op_Le => |
7058 | return RList'(1 => REnt'(BLo, Val)); | |
fb7f2fc4 | 7059 | |
eb66e842 | 7060 | when N_Op_Lt => |
7061 | return RList'(1 => REnt'(BLo, Val - 1)); | |
9dc88aea | 7062 | |
eb66e842 | 7063 | when N_Op_Ne => |
7064 | return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi)); | |
9dc88aea | 7065 | |
eb66e842 | 7066 | when others => |
7067 | raise Program_Error; | |
7068 | end case; | |
9dc88aea | 7069 | |
eb66e842 | 7070 | -- Membership (IN) |
9dc88aea | 7071 | |
eb66e842 | 7072 | when N_In => |
7073 | if not Is_Type_Ref (Left_Opnd (Exp)) then | |
7074 | raise Non_Static; | |
7075 | end if; | |
9dc88aea | 7076 | |
eb66e842 | 7077 | if Present (Right_Opnd (Exp)) then |
7078 | return Membership_Entry (Right_Opnd (Exp)); | |
7079 | else | |
7080 | return Membership_Entries (First (Alternatives (Exp))); | |
7081 | end if; | |
9dc88aea | 7082 | |
eb66e842 | 7083 | -- Negative membership (NOT IN) |
9dc88aea | 7084 | |
eb66e842 | 7085 | when N_Not_In => |
7086 | if not Is_Type_Ref (Left_Opnd (Exp)) then | |
7087 | raise Non_Static; | |
7088 | end if; | |
9dc88aea | 7089 | |
eb66e842 | 7090 | if Present (Right_Opnd (Exp)) then |
7091 | return not Membership_Entry (Right_Opnd (Exp)); | |
7092 | else | |
7093 | return not Membership_Entries (First (Alternatives (Exp))); | |
7094 | end if; | |
9dc88aea | 7095 | |
eb66e842 | 7096 | -- Function call, may be call to static predicate |
9dc88aea | 7097 | |
eb66e842 | 7098 | when N_Function_Call => |
7099 | if Is_Entity_Name (Name (Exp)) then | |
7100 | declare | |
7101 | Ent : constant Entity_Id := Entity (Name (Exp)); | |
7102 | begin | |
7103 | if Is_Predicate_Function (Ent) | |
7104 | or else | |
7105 | Is_Predicate_Function_M (Ent) | |
7106 | then | |
7107 | return Stat_Pred (Etype (First_Formal (Ent))); | |
7108 | end if; | |
7109 | end; | |
7110 | end if; | |
9dc88aea | 7111 | |
eb66e842 | 7112 | -- Other function call cases are non-static |
9dc88aea | 7113 | |
eb66e842 | 7114 | raise Non_Static; |
490beba6 | 7115 | |
eb66e842 | 7116 | -- Qualified expression, dig out the expression |
c92e878b | 7117 | |
eb66e842 | 7118 | when N_Qualified_Expression => |
7119 | return Get_RList (Expression (Exp)); | |
4c1fd062 | 7120 | |
eb66e842 | 7121 | when N_Case_Expression => |
7122 | declare | |
7123 | Alt : Node_Id; | |
7124 | Choices : List_Id; | |
7125 | Dep : Node_Id; | |
4c1fd062 | 7126 | |
eb66e842 | 7127 | begin |
7128 | if not Is_Entity_Name (Expression (Expr)) | |
7129 | or else Etype (Expression (Expr)) /= Typ | |
7130 | then | |
7131 | Error_Msg_N | |
7132 | ("expression must denaote subtype", Expression (Expr)); | |
7133 | return False_Range; | |
7134 | end if; | |
9dc88aea | 7135 | |
eb66e842 | 7136 | -- Collect discrete choices in all True alternatives |
9dc88aea | 7137 | |
eb66e842 | 7138 | Choices := New_List; |
7139 | Alt := First (Alternatives (Exp)); | |
7140 | while Present (Alt) loop | |
7141 | Dep := Expression (Alt); | |
34d045d3 | 7142 | |
cda40848 | 7143 | if not Is_OK_Static_Expression (Dep) then |
eb66e842 | 7144 | raise Non_Static; |
ebbab42d | 7145 | |
eb66e842 | 7146 | elsif Is_True (Expr_Value (Dep)) then |
7147 | Append_List_To (Choices, | |
7148 | New_Copy_List (Discrete_Choices (Alt))); | |
7149 | end if; | |
fb7f2fc4 | 7150 | |
eb66e842 | 7151 | Next (Alt); |
7152 | end loop; | |
9dc88aea | 7153 | |
eb66e842 | 7154 | return Membership_Entries (First (Choices)); |
7155 | end; | |
9dc88aea | 7156 | |
eb66e842 | 7157 | -- Expression with actions: if no actions, dig out expression |
9dc88aea | 7158 | |
eb66e842 | 7159 | when N_Expression_With_Actions => |
7160 | if Is_Empty_List (Actions (Exp)) then | |
7161 | return Get_RList (Expression (Exp)); | |
7162 | else | |
7163 | raise Non_Static; | |
7164 | end if; | |
9dc88aea | 7165 | |
eb66e842 | 7166 | -- Xor operator |
490beba6 | 7167 | |
eb66e842 | 7168 | when N_Op_Xor => |
7169 | return (Get_RList (Left_Opnd (Exp)) | |
7170 | and not Get_RList (Right_Opnd (Exp))) | |
7171 | or (Get_RList (Right_Opnd (Exp)) | |
7172 | and not Get_RList (Left_Opnd (Exp))); | |
9dc88aea | 7173 | |
eb66e842 | 7174 | -- Any other node type is non-static |
fb7f2fc4 | 7175 | |
eb66e842 | 7176 | when others => |
7177 | raise Non_Static; | |
7178 | end case; | |
7179 | end Get_RList; | |
fb7f2fc4 | 7180 | |
eb66e842 | 7181 | ------------ |
7182 | -- Hi_Val -- | |
7183 | ------------ | |
fb7f2fc4 | 7184 | |
eb66e842 | 7185 | function Hi_Val (N : Node_Id) return Uint is |
7186 | begin | |
cda40848 | 7187 | if Is_OK_Static_Expression (N) then |
eb66e842 | 7188 | return Expr_Value (N); |
7189 | else | |
7190 | pragma Assert (Nkind (N) = N_Range); | |
7191 | return Expr_Value (High_Bound (N)); | |
7192 | end if; | |
7193 | end Hi_Val; | |
fb7f2fc4 | 7194 | |
eb66e842 | 7195 | -------------- |
7196 | -- Is_False -- | |
7197 | -------------- | |
fb7f2fc4 | 7198 | |
eb66e842 | 7199 | function Is_False (R : RList) return Boolean is |
7200 | begin | |
7201 | return R'Length = 0; | |
7202 | end Is_False; | |
9dc88aea | 7203 | |
eb66e842 | 7204 | ------------- |
7205 | -- Is_True -- | |
7206 | ------------- | |
9dc88aea | 7207 | |
eb66e842 | 7208 | function Is_True (R : RList) return Boolean is |
7209 | begin | |
7210 | return R'Length = 1 | |
7211 | and then R (R'First).Lo = BLo | |
7212 | and then R (R'First).Hi = BHi; | |
7213 | end Is_True; | |
9dc88aea | 7214 | |
eb66e842 | 7215 | ----------------- |
7216 | -- Is_Type_Ref -- | |
7217 | ----------------- | |
9dc88aea | 7218 | |
eb66e842 | 7219 | function Is_Type_Ref (N : Node_Id) return Boolean is |
7220 | begin | |
7de4cba3 | 7221 | return Nkind (N) = N_Identifier |
7222 | and then Chars (N) = Nam | |
7223 | and then Paren_Count (N) = 0; | |
eb66e842 | 7224 | end Is_Type_Ref; |
9dc88aea | 7225 | |
eb66e842 | 7226 | ------------ |
7227 | -- Lo_Val -- | |
7228 | ------------ | |
9dc88aea | 7229 | |
eb66e842 | 7230 | function Lo_Val (N : Node_Id) return Uint is |
84c8f0b8 | 7231 | begin |
cda40848 | 7232 | if Is_OK_Static_Expression (N) then |
eb66e842 | 7233 | return Expr_Value (N); |
84c8f0b8 | 7234 | else |
eb66e842 | 7235 | pragma Assert (Nkind (N) = N_Range); |
7236 | return Expr_Value (Low_Bound (N)); | |
84c8f0b8 | 7237 | end if; |
eb66e842 | 7238 | end Lo_Val; |
d97beb2f | 7239 | |
eb66e842 | 7240 | ------------------------ |
7241 | -- Membership_Entries -- | |
7242 | ------------------------ | |
d97beb2f | 7243 | |
eb66e842 | 7244 | function Membership_Entries (N : Node_Id) return RList is |
84c8f0b8 | 7245 | begin |
eb66e842 | 7246 | if No (Next (N)) then |
7247 | return Membership_Entry (N); | |
84c8f0b8 | 7248 | else |
eb66e842 | 7249 | return Membership_Entry (N) or Membership_Entries (Next (N)); |
84c8f0b8 | 7250 | end if; |
eb66e842 | 7251 | end Membership_Entries; |
84c8f0b8 | 7252 | |
eb66e842 | 7253 | ---------------------- |
7254 | -- Membership_Entry -- | |
7255 | ---------------------- | |
84c8f0b8 | 7256 | |
eb66e842 | 7257 | function Membership_Entry (N : Node_Id) return RList is |
7258 | Val : Uint; | |
7259 | SLo : Uint; | |
7260 | SHi : Uint; | |
d97beb2f | 7261 | |
eb66e842 | 7262 | begin |
7263 | -- Range case | |
d97beb2f | 7264 | |
eb66e842 | 7265 | if Nkind (N) = N_Range then |
cda40848 | 7266 | if not Is_OK_Static_Expression (Low_Bound (N)) |
eb66e842 | 7267 | or else |
cda40848 | 7268 | not Is_OK_Static_Expression (High_Bound (N)) |
eb66e842 | 7269 | then |
7270 | raise Non_Static; | |
7271 | else | |
7272 | SLo := Expr_Value (Low_Bound (N)); | |
7273 | SHi := Expr_Value (High_Bound (N)); | |
7274 | return RList'(1 => REnt'(SLo, SHi)); | |
7275 | end if; | |
84c8f0b8 | 7276 | |
eb66e842 | 7277 | -- Static expression case |
84c8f0b8 | 7278 | |
cda40848 | 7279 | elsif Is_OK_Static_Expression (N) then |
eb66e842 | 7280 | Val := Expr_Value (N); |
7281 | return RList'(1 => REnt'(Val, Val)); | |
d97beb2f | 7282 | |
eb66e842 | 7283 | -- Identifier (other than static expression) case |
d97beb2f | 7284 | |
eb66e842 | 7285 | else pragma Assert (Nkind (N) = N_Identifier); |
d97beb2f | 7286 | |
eb66e842 | 7287 | -- Type case |
d97beb2f | 7288 | |
eb66e842 | 7289 | if Is_Type (Entity (N)) then |
d97beb2f | 7290 | |
eb66e842 | 7291 | -- If type has predicates, process them |
d97beb2f | 7292 | |
eb66e842 | 7293 | if Has_Predicates (Entity (N)) then |
7294 | return Stat_Pred (Entity (N)); | |
d97beb2f | 7295 | |
eb66e842 | 7296 | -- For static subtype without predicates, get range |
9dc88aea | 7297 | |
cda40848 | 7298 | elsif Is_OK_Static_Subtype (Entity (N)) then |
eb66e842 | 7299 | SLo := Expr_Value (Type_Low_Bound (Entity (N))); |
7300 | SHi := Expr_Value (Type_High_Bound (Entity (N))); | |
7301 | return RList'(1 => REnt'(SLo, SHi)); | |
9f269bd8 | 7302 | |
eb66e842 | 7303 | -- Any other type makes us non-static |
9f269bd8 | 7304 | |
eb66e842 | 7305 | else |
7306 | raise Non_Static; | |
7307 | end if; | |
84c8f0b8 | 7308 | |
eb66e842 | 7309 | -- Any other kind of identifier in predicate (e.g. a non-static |
7310 | -- expression value) means this is not a static predicate. | |
84c8f0b8 | 7311 | |
eb66e842 | 7312 | else |
7313 | raise Non_Static; | |
7314 | end if; | |
7315 | end if; | |
7316 | end Membership_Entry; | |
84c8f0b8 | 7317 | |
eb66e842 | 7318 | --------------- |
7319 | -- Stat_Pred -- | |
7320 | --------------- | |
84c8f0b8 | 7321 | |
eb66e842 | 7322 | function Stat_Pred (Typ : Entity_Id) return RList is |
7323 | begin | |
7324 | -- Not static if type does not have static predicates | |
84c8f0b8 | 7325 | |
5c6a5792 | 7326 | if not Has_Static_Predicate (Typ) then |
eb66e842 | 7327 | raise Non_Static; |
7328 | end if; | |
84c8f0b8 | 7329 | |
eb66e842 | 7330 | -- Otherwise we convert the predicate list to a range list |
84c8f0b8 | 7331 | |
eb66e842 | 7332 | declare |
5c6a5792 | 7333 | Spred : constant List_Id := Static_Discrete_Predicate (Typ); |
7334 | Result : RList (1 .. List_Length (Spred)); | |
eb66e842 | 7335 | P : Node_Id; |
84c8f0b8 | 7336 | |
eb66e842 | 7337 | begin |
5c6a5792 | 7338 | P := First (Static_Discrete_Predicate (Typ)); |
eb66e842 | 7339 | for J in Result'Range loop |
7340 | Result (J) := REnt'(Lo_Val (P), Hi_Val (P)); | |
7341 | Next (P); | |
7342 | end loop; | |
84c8f0b8 | 7343 | |
eb66e842 | 7344 | return Result; |
7345 | end; | |
7346 | end Stat_Pred; | |
84c8f0b8 | 7347 | |
eb66e842 | 7348 | -- Start of processing for Build_Discrete_Static_Predicate |
84c8f0b8 | 7349 | |
eb66e842 | 7350 | begin |
7351 | -- Analyze the expression to see if it is a static predicate | |
84c8f0b8 | 7352 | |
eb66e842 | 7353 | declare |
7354 | Ranges : constant RList := Get_RList (Expr); | |
7355 | -- Range list from expression if it is static | |
84c8f0b8 | 7356 | |
eb66e842 | 7357 | Plist : List_Id; |
84c8f0b8 | 7358 | |
eb66e842 | 7359 | begin |
7360 | -- Convert range list into a form for the static predicate. In the | |
7361 | -- Ranges array, we just have raw ranges, these must be converted | |
7362 | -- to properly typed and analyzed static expressions or range nodes. | |
84c8f0b8 | 7363 | |
eb66e842 | 7364 | -- Note: here we limit ranges to the ranges of the subtype, so that |
7365 | -- a predicate is always false for values outside the subtype. That | |
7366 | -- seems fine, such values are invalid anyway, and considering them | |
7367 | -- to fail the predicate seems allowed and friendly, and furthermore | |
7368 | -- simplifies processing for case statements and loops. | |
84c8f0b8 | 7369 | |
eb66e842 | 7370 | Plist := New_List; |
7371 | ||
7372 | for J in Ranges'Range loop | |
84c8f0b8 | 7373 | declare |
eb66e842 | 7374 | Lo : Uint := Ranges (J).Lo; |
7375 | Hi : Uint := Ranges (J).Hi; | |
84c8f0b8 | 7376 | |
eb66e842 | 7377 | begin |
7378 | -- Ignore completely out of range entry | |
84c8f0b8 | 7379 | |
eb66e842 | 7380 | if Hi < TLo or else Lo > THi then |
7381 | null; | |
84c8f0b8 | 7382 | |
eb66e842 | 7383 | -- Otherwise process entry |
84c8f0b8 | 7384 | |
eb66e842 | 7385 | else |
7386 | -- Adjust out of range value to subtype range | |
490beba6 | 7387 | |
eb66e842 | 7388 | if Lo < TLo then |
7389 | Lo := TLo; | |
7390 | end if; | |
490beba6 | 7391 | |
eb66e842 | 7392 | if Hi > THi then |
7393 | Hi := THi; | |
7394 | end if; | |
84c8f0b8 | 7395 | |
eb66e842 | 7396 | -- Convert range into required form |
84c8f0b8 | 7397 | |
eb66e842 | 7398 | Append_To (Plist, Build_Range (Lo, Hi)); |
84c8f0b8 | 7399 | end if; |
eb66e842 | 7400 | end; |
7401 | end loop; | |
84c8f0b8 | 7402 | |
eb66e842 | 7403 | -- Processing was successful and all entries were static, so now we |
7404 | -- can store the result as the predicate list. | |
84c8f0b8 | 7405 | |
5c6a5792 | 7406 | Set_Static_Discrete_Predicate (Typ, Plist); |
84c8f0b8 | 7407 | |
eb66e842 | 7408 | -- The processing for static predicates put the expression into |
7409 | -- canonical form as a series of ranges. It also eliminated | |
7410 | -- duplicates and collapsed and combined ranges. We might as well | |
7411 | -- replace the alternatives list of the right operand of the | |
7412 | -- membership test with the static predicate list, which will | |
7413 | -- usually be more efficient. | |
84c8f0b8 | 7414 | |
eb66e842 | 7415 | declare |
7416 | New_Alts : constant List_Id := New_List; | |
7417 | Old_Node : Node_Id; | |
7418 | New_Node : Node_Id; | |
84c8f0b8 | 7419 | |
eb66e842 | 7420 | begin |
7421 | Old_Node := First (Plist); | |
7422 | while Present (Old_Node) loop | |
7423 | New_Node := New_Copy (Old_Node); | |
84c8f0b8 | 7424 | |
eb66e842 | 7425 | if Nkind (New_Node) = N_Range then |
7426 | Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node))); | |
7427 | Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node))); | |
7428 | end if; | |
84c8f0b8 | 7429 | |
eb66e842 | 7430 | Append_To (New_Alts, New_Node); |
7431 | Next (Old_Node); | |
7432 | end loop; | |
84c8f0b8 | 7433 | |
eb66e842 | 7434 | -- If empty list, replace by False |
84c8f0b8 | 7435 | |
eb66e842 | 7436 | if Is_Empty_List (New_Alts) then |
7437 | Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc)); | |
84c8f0b8 | 7438 | |
eb66e842 | 7439 | -- Else replace by set membership test |
84c8f0b8 | 7440 | |
eb66e842 | 7441 | else |
7442 | Rewrite (Expr, | |
7443 | Make_In (Loc, | |
7444 | Left_Opnd => Make_Identifier (Loc, Nam), | |
7445 | Right_Opnd => Empty, | |
7446 | Alternatives => New_Alts)); | |
490beba6 | 7447 | |
eb66e842 | 7448 | -- Resolve new expression in function context |
490beba6 | 7449 | |
eb66e842 | 7450 | Install_Formals (Predicate_Function (Typ)); |
7451 | Push_Scope (Predicate_Function (Typ)); | |
7452 | Analyze_And_Resolve (Expr, Standard_Boolean); | |
7453 | Pop_Scope; | |
7454 | end if; | |
7455 | end; | |
7456 | end; | |
9ab32fe9 | 7457 | |
eb66e842 | 7458 | -- If non-static, return doing nothing |
9ab32fe9 | 7459 | |
eb66e842 | 7460 | exception |
7461 | when Non_Static => | |
7462 | return; | |
7463 | end Build_Discrete_Static_Predicate; | |
64cc9e5d | 7464 | |
eb66e842 | 7465 | ------------------------------------------- |
7466 | -- Build_Invariant_Procedure_Declaration -- | |
7467 | ------------------------------------------- | |
9ab32fe9 | 7468 | |
eb66e842 | 7469 | function Build_Invariant_Procedure_Declaration |
7470 | (Typ : Entity_Id) return Node_Id | |
7471 | is | |
7472 | Loc : constant Source_Ptr := Sloc (Typ); | |
7473 | Object_Entity : constant Entity_Id := | |
7474 | Make_Defining_Identifier (Loc, New_Internal_Name ('I')); | |
7475 | Spec : Node_Id; | |
7476 | SId : Entity_Id; | |
9ab32fe9 | 7477 | |
eb66e842 | 7478 | begin |
7479 | Set_Etype (Object_Entity, Typ); | |
7480 | ||
7481 | -- Check for duplicate definiations. | |
7482 | ||
7483 | if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then | |
7484 | return Empty; | |
d97beb2f | 7485 | end if; |
d97beb2f | 7486 | |
eb66e842 | 7487 | SId := |
7488 | Make_Defining_Identifier (Loc, | |
7489 | Chars => New_External_Name (Chars (Typ), "Invariant")); | |
7490 | Set_Has_Invariants (Typ); | |
7491 | Set_Ekind (SId, E_Procedure); | |
856a9917 | 7492 | Set_Etype (SId, Standard_Void_Type); |
eb66e842 | 7493 | Set_Is_Invariant_Procedure (SId); |
7494 | Set_Invariant_Procedure (Typ, SId); | |
d97beb2f | 7495 | |
eb66e842 | 7496 | Spec := |
7497 | Make_Procedure_Specification (Loc, | |
7498 | Defining_Unit_Name => SId, | |
7499 | Parameter_Specifications => New_List ( | |
7500 | Make_Parameter_Specification (Loc, | |
7501 | Defining_Identifier => Object_Entity, | |
7502 | Parameter_Type => New_Occurrence_Of (Typ, Loc)))); | |
d97beb2f | 7503 | |
eb66e842 | 7504 | return Make_Subprogram_Declaration (Loc, Specification => Spec); |
7505 | end Build_Invariant_Procedure_Declaration; | |
7506 | ||
7507 | ------------------------------- | |
7508 | -- Build_Invariant_Procedure -- | |
7509 | ------------------------------- | |
7510 | ||
7511 | -- The procedure that is constructed here has the form | |
7512 | ||
7513 | -- procedure typInvariant (Ixxx : typ) is | |
7514 | -- begin | |
7515 | -- pragma Check (Invariant, exp, "failed invariant from xxx"); | |
7516 | -- pragma Check (Invariant, exp, "failed invariant from xxx"); | |
7517 | -- ... | |
7518 | -- pragma Check (Invariant, exp, "failed inherited invariant from xxx"); | |
7519 | -- ... | |
7520 | -- end typInvariant; | |
7521 | ||
7522 | procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is | |
7523 | Loc : constant Source_Ptr := Sloc (Typ); | |
7524 | Stmts : List_Id; | |
7525 | Spec : Node_Id; | |
7526 | SId : Entity_Id; | |
7527 | PDecl : Node_Id; | |
7528 | PBody : Node_Id; | |
d97beb2f | 7529 | |
eb66e842 | 7530 | Nam : Name_Id; |
7531 | -- Name for Check pragma, usually Invariant, but might be Type_Invariant | |
7532 | -- if we come from a Type_Invariant aspect, we make sure to build the | |
7533 | -- Check pragma with the right name, so that Check_Policy works right. | |
d7c2851f | 7534 | |
eb66e842 | 7535 | Visible_Decls : constant List_Id := Visible_Declarations (N); |
7536 | Private_Decls : constant List_Id := Private_Declarations (N); | |
d7c2851f | 7537 | |
eb66e842 | 7538 | procedure Add_Invariants (T : Entity_Id; Inherit : Boolean); |
7539 | -- Appends statements to Stmts for any invariants in the rep item chain | |
7540 | -- of the given type. If Inherit is False, then we only process entries | |
7541 | -- on the chain for the type Typ. If Inherit is True, then we ignore any | |
7542 | -- Invariant aspects, but we process all Invariant'Class aspects, adding | |
7543 | -- "inherited" to the exception message and generating an informational | |
7544 | -- message about the inheritance of an invariant. | |
d97beb2f | 7545 | |
eb66e842 | 7546 | Object_Name : Name_Id; |
7547 | -- Name for argument of invariant procedure | |
d97beb2f | 7548 | |
eb66e842 | 7549 | Object_Entity : Node_Id; |
7550 | -- The entity of the formal for the procedure | |
d97beb2f | 7551 | |
eb66e842 | 7552 | -------------------- |
7553 | -- Add_Invariants -- | |
7554 | -------------------- | |
d97beb2f | 7555 | |
eb66e842 | 7556 | procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is |
7557 | Ritem : Node_Id; | |
7558 | Arg1 : Node_Id; | |
7559 | Arg2 : Node_Id; | |
7560 | Arg3 : Node_Id; | |
7561 | Exp : Node_Id; | |
7562 | Loc : Source_Ptr; | |
7563 | Assoc : List_Id; | |
7564 | Str : String_Id; | |
d97beb2f | 7565 | |
eb66e842 | 7566 | procedure Replace_Type_Reference (N : Node_Id); |
7567 | -- Replace a single occurrence N of the subtype name with a reference | |
7568 | -- to the formal of the predicate function. N can be an identifier | |
7569 | -- referencing the subtype, or a selected component, representing an | |
7570 | -- appropriately qualified occurrence of the subtype name. | |
d97beb2f | 7571 | |
eb66e842 | 7572 | procedure Replace_Type_References is |
7573 | new Replace_Type_References_Generic (Replace_Type_Reference); | |
7574 | -- Traverse an expression replacing all occurrences of the subtype | |
7575 | -- name with appropriate references to the object that is the formal | |
7576 | -- parameter of the predicate function. Note that we must ensure | |
7577 | -- that the type and entity information is properly set in the | |
7578 | -- replacement node, since we will do a Preanalyze call of this | |
7579 | -- expression without proper visibility of the procedure argument. | |
d97beb2f | 7580 | |
eb66e842 | 7581 | ---------------------------- |
7582 | -- Replace_Type_Reference -- | |
7583 | ---------------------------- | |
d97beb2f | 7584 | |
eb66e842 | 7585 | -- Note: See comments in Add_Predicates.Replace_Type_Reference |
7586 | -- regarding handling of Sloc and Comes_From_Source. | |
d97beb2f | 7587 | |
eb66e842 | 7588 | procedure Replace_Type_Reference (N : Node_Id) is |
7589 | begin | |
d97beb2f | 7590 | |
eb66e842 | 7591 | -- Add semantic information to node to be rewritten, for ASIS |
7592 | -- navigation needs. | |
d97beb2f | 7593 | |
eb66e842 | 7594 | if Nkind (N) = N_Identifier then |
7595 | Set_Entity (N, T); | |
7596 | Set_Etype (N, T); | |
d7c2851f | 7597 | |
eb66e842 | 7598 | elsif Nkind (N) = N_Selected_Component then |
7599 | Analyze (Prefix (N)); | |
7600 | Set_Entity (Selector_Name (N), T); | |
7601 | Set_Etype (Selector_Name (N), T); | |
7602 | end if; | |
d7c2851f | 7603 | |
eb66e842 | 7604 | -- Invariant'Class, replace with T'Class (obj) |
69004fe6 | 7605 | -- In ASIS mode, an inherited item is analyzed already, and the |
7606 | -- replacement has been done, so do not repeat transformation | |
7607 | -- to prevent ill-formed tree. | |
d97beb2f | 7608 | |
eb66e842 | 7609 | if Class_Present (Ritem) then |
69004fe6 | 7610 | if ASIS_Mode |
7611 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
7612 | and then Attribute_Name (Parent (N)) = Name_Class | |
7613 | then | |
7614 | null; | |
7615 | ||
7616 | else | |
7617 | Rewrite (N, | |
7618 | Make_Type_Conversion (Sloc (N), | |
7619 | Subtype_Mark => | |
7620 | Make_Attribute_Reference (Sloc (N), | |
7621 | Prefix => New_Occurrence_Of (T, Sloc (N)), | |
7622 | Attribute_Name => Name_Class), | |
7623 | Expression => | |
7624 | Make_Identifier (Sloc (N), Object_Name))); | |
7625 | ||
7626 | Set_Entity (Expression (N), Object_Entity); | |
7627 | Set_Etype (Expression (N), Typ); | |
7628 | end if; | |
d9f6a4ee | 7629 | |
eb66e842 | 7630 | -- Invariant, replace with obj |
d9f6a4ee | 7631 | |
eb66e842 | 7632 | else |
7633 | Rewrite (N, Make_Identifier (Sloc (N), Object_Name)); | |
7634 | Set_Entity (N, Object_Entity); | |
7635 | Set_Etype (N, Typ); | |
7636 | end if; | |
d9f6a4ee | 7637 | |
eb66e842 | 7638 | Set_Comes_From_Source (N, True); |
7639 | end Replace_Type_Reference; | |
d9f6a4ee | 7640 | |
eb66e842 | 7641 | -- Start of processing for Add_Invariants |
d9f6a4ee | 7642 | |
eb66e842 | 7643 | begin |
7644 | Ritem := First_Rep_Item (T); | |
7645 | while Present (Ritem) loop | |
7646 | if Nkind (Ritem) = N_Pragma | |
7647 | and then Pragma_Name (Ritem) = Name_Invariant | |
7648 | then | |
7649 | Arg1 := First (Pragma_Argument_Associations (Ritem)); | |
7650 | Arg2 := Next (Arg1); | |
7651 | Arg3 := Next (Arg2); | |
d9f6a4ee | 7652 | |
eb66e842 | 7653 | Arg1 := Get_Pragma_Arg (Arg1); |
7654 | Arg2 := Get_Pragma_Arg (Arg2); | |
d9f6a4ee | 7655 | |
eb66e842 | 7656 | -- For Inherit case, ignore Invariant, process only Class case |
d9f6a4ee | 7657 | |
eb66e842 | 7658 | if Inherit then |
7659 | if not Class_Present (Ritem) then | |
7660 | goto Continue; | |
7661 | end if; | |
d9f6a4ee | 7662 | |
eb66e842 | 7663 | -- For Inherit false, process only item for right type |
d9f6a4ee | 7664 | |
eb66e842 | 7665 | else |
7666 | if Entity (Arg1) /= Typ then | |
7667 | goto Continue; | |
7668 | end if; | |
7669 | end if; | |
d9f6a4ee | 7670 | |
eb66e842 | 7671 | if No (Stmts) then |
7672 | Stmts := Empty_List; | |
7673 | end if; | |
d9f6a4ee | 7674 | |
eb66e842 | 7675 | Exp := New_Copy_Tree (Arg2); |
d9f6a4ee | 7676 | |
eb66e842 | 7677 | -- Preserve sloc of original pragma Invariant |
d9f6a4ee | 7678 | |
eb66e842 | 7679 | Loc := Sloc (Ritem); |
d9f6a4ee | 7680 | |
eb66e842 | 7681 | -- We need to replace any occurrences of the name of the type |
7682 | -- with references to the object, converted to type'Class in | |
7683 | -- the case of Invariant'Class aspects. | |
d9f6a4ee | 7684 | |
37c6552c | 7685 | Replace_Type_References (Exp, T); |
d9f6a4ee | 7686 | |
eb66e842 | 7687 | -- If this invariant comes from an aspect, find the aspect |
7688 | -- specification, and replace the saved expression because | |
7689 | -- we need the subtype references replaced for the calls to | |
7690 | -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point | |
7691 | -- and Check_Aspect_At_End_Of_Declarations. | |
d9f6a4ee | 7692 | |
eb66e842 | 7693 | if From_Aspect_Specification (Ritem) then |
7694 | declare | |
7695 | Aitem : Node_Id; | |
d9f6a4ee | 7696 | |
eb66e842 | 7697 | begin |
7698 | -- Loop to find corresponding aspect, note that this | |
7699 | -- must be present given the pragma is marked delayed. | |
d9f6a4ee | 7700 | |
eb66e842 | 7701 | -- Note: in practice Next_Rep_Item (Ritem) is Empty so |
7702 | -- this loop does nothing. Furthermore, why isn't this | |
7703 | -- simply Corresponding_Aspect ??? | |
d9f6a4ee | 7704 | |
eb66e842 | 7705 | Aitem := Next_Rep_Item (Ritem); |
7706 | while Present (Aitem) loop | |
7707 | if Nkind (Aitem) = N_Aspect_Specification | |
7708 | and then Aspect_Rep_Item (Aitem) = Ritem | |
7709 | then | |
7710 | Set_Entity | |
7711 | (Identifier (Aitem), New_Copy_Tree (Exp)); | |
7712 | exit; | |
7713 | end if; | |
d9f6a4ee | 7714 | |
eb66e842 | 7715 | Aitem := Next_Rep_Item (Aitem); |
7716 | end loop; | |
7717 | end; | |
7718 | end if; | |
d9f6a4ee | 7719 | |
eb66e842 | 7720 | -- Now we need to preanalyze the expression to properly capture |
7721 | -- the visibility in the visible part. The expression will not | |
7722 | -- be analyzed for real until the body is analyzed, but that is | |
7723 | -- at the end of the private part and has the wrong visibility. | |
d9f6a4ee | 7724 | |
eb66e842 | 7725 | Set_Parent (Exp, N); |
7726 | Preanalyze_Assert_Expression (Exp, Standard_Boolean); | |
d9f6a4ee | 7727 | |
f02a9a9a | 7728 | -- A class-wide invariant may be inherited in a separate unit, |
7729 | -- where the corresponding expression cannot be resolved by | |
7730 | -- visibility, because it refers to a local function. Propagate | |
7731 | -- semantic information to the original representation item, to | |
7732 | -- be used when an invariant procedure for a derived type is | |
7733 | -- constructed. | |
7734 | ||
7735 | -- Unclear how to handle class-wide invariants that are not | |
7736 | -- function calls ??? | |
7737 | ||
7738 | if not Inherit | |
7739 | and then Class_Present (Ritem) | |
7740 | and then Nkind (Exp) = N_Function_Call | |
7741 | and then Nkind (Arg2) = N_Indexed_Component | |
7742 | then | |
7743 | Rewrite (Arg2, | |
7744 | Make_Function_Call (Loc, | |
7745 | Name => | |
7746 | New_Occurrence_Of (Entity (Name (Exp)), Loc), | |
7747 | Parameter_Associations => | |
7748 | New_Copy_List (Expressions (Arg2)))); | |
7749 | end if; | |
7750 | ||
eb66e842 | 7751 | -- In ASIS mode, even if assertions are not enabled, we must |
7752 | -- analyze the original expression in the aspect specification | |
7753 | -- because it is part of the original tree. | |
d9f6a4ee | 7754 | |
eb66e842 | 7755 | if ASIS_Mode and then From_Aspect_Specification (Ritem) then |
7756 | declare | |
7757 | Inv : constant Node_Id := | |
7758 | Expression (Corresponding_Aspect (Ritem)); | |
7759 | begin | |
37c6552c | 7760 | Replace_Type_References (Inv, T); |
eb66e842 | 7761 | Preanalyze_Assert_Expression (Inv, Standard_Boolean); |
7762 | end; | |
7763 | end if; | |
d9f6a4ee | 7764 | |
eb66e842 | 7765 | -- Get name to be used for Check pragma |
d9f6a4ee | 7766 | |
eb66e842 | 7767 | if not From_Aspect_Specification (Ritem) then |
7768 | Nam := Name_Invariant; | |
7769 | else | |
7770 | Nam := Chars (Identifier (Corresponding_Aspect (Ritem))); | |
7771 | end if; | |
d9f6a4ee | 7772 | |
eb66e842 | 7773 | -- Build first two arguments for Check pragma |
d9f6a4ee | 7774 | |
eb66e842 | 7775 | Assoc := |
7776 | New_List ( | |
7777 | Make_Pragma_Argument_Association (Loc, | |
7778 | Expression => Make_Identifier (Loc, Chars => Nam)), | |
7779 | Make_Pragma_Argument_Association (Loc, | |
7780 | Expression => Exp)); | |
d9f6a4ee | 7781 | |
eb66e842 | 7782 | -- Add message if present in Invariant pragma |
d9f6a4ee | 7783 | |
eb66e842 | 7784 | if Present (Arg3) then |
7785 | Str := Strval (Get_Pragma_Arg (Arg3)); | |
d9f6a4ee | 7786 | |
eb66e842 | 7787 | -- If inherited case, and message starts "failed invariant", |
7788 | -- change it to be "failed inherited invariant". | |
d9f6a4ee | 7789 | |
eb66e842 | 7790 | if Inherit then |
7791 | String_To_Name_Buffer (Str); | |
d9f6a4ee | 7792 | |
eb66e842 | 7793 | if Name_Buffer (1 .. 16) = "failed invariant" then |
7794 | Insert_Str_In_Name_Buffer ("inherited ", 8); | |
7795 | Str := String_From_Name_Buffer; | |
7796 | end if; | |
7797 | end if; | |
d9f6a4ee | 7798 | |
eb66e842 | 7799 | Append_To (Assoc, |
7800 | Make_Pragma_Argument_Association (Loc, | |
7801 | Expression => Make_String_Literal (Loc, Str))); | |
7802 | end if; | |
d9f6a4ee | 7803 | |
eb66e842 | 7804 | -- Add Check pragma to list of statements |
d97beb2f | 7805 | |
eb66e842 | 7806 | Append_To (Stmts, |
7807 | Make_Pragma (Loc, | |
7808 | Pragma_Identifier => | |
7809 | Make_Identifier (Loc, Name_Check), | |
7810 | Pragma_Argument_Associations => Assoc)); | |
d97beb2f | 7811 | |
eb66e842 | 7812 | -- If Inherited case and option enabled, output info msg. Note |
7813 | -- that we know this is a case of Invariant'Class. | |
d97beb2f | 7814 | |
eb66e842 | 7815 | if Inherit and Opt.List_Inherited_Aspects then |
7816 | Error_Msg_Sloc := Sloc (Ritem); | |
7817 | Error_Msg_N | |
7818 | ("info: & inherits `Invariant''Class` aspect from #?L?", | |
7819 | Typ); | |
7820 | end if; | |
d9f6a4ee | 7821 | end if; |
d97beb2f | 7822 | |
eb66e842 | 7823 | <<Continue>> |
7824 | Next_Rep_Item (Ritem); | |
7825 | end loop; | |
7826 | end Add_Invariants; | |
d97beb2f | 7827 | |
eb66e842 | 7828 | -- Start of processing for Build_Invariant_Procedure |
d97beb2f | 7829 | |
eb66e842 | 7830 | begin |
7831 | Stmts := No_List; | |
7832 | PDecl := Empty; | |
7833 | PBody := Empty; | |
7834 | SId := Empty; | |
d97beb2f | 7835 | |
eb66e842 | 7836 | -- If the aspect specification exists for some view of the type, the |
7837 | -- declaration for the procedure has been created. | |
d97beb2f | 7838 | |
eb66e842 | 7839 | if Has_Invariants (Typ) then |
7840 | SId := Invariant_Procedure (Typ); | |
7841 | end if; | |
9dc88aea | 7842 | |
0c2bde47 | 7843 | -- If the body is already present, nothing to do. This will occur when |
7844 | -- the type is already frozen, which is the case when the invariant | |
7845 | -- appears in a private part, and the freezing takes place before the | |
7846 | -- final pass over full declarations. | |
aba11c12 | 7847 | |
7848 | -- See Exp_Ch3.Insert_Component_Invariant_Checks for details. | |
0c2bde47 | 7849 | |
eb66e842 | 7850 | if Present (SId) then |
7851 | PDecl := Unit_Declaration_Node (SId); | |
0c2bde47 | 7852 | |
7853 | if Present (PDecl) | |
7854 | and then Nkind (PDecl) = N_Subprogram_Declaration | |
7855 | and then Present (Corresponding_Body (PDecl)) | |
7856 | then | |
7857 | return; | |
7858 | end if; | |
7859 | ||
eb66e842 | 7860 | else |
7861 | PDecl := Build_Invariant_Procedure_Declaration (Typ); | |
7862 | end if; | |
9dc88aea | 7863 | |
eb66e842 | 7864 | -- Recover formal of procedure, for use in the calls to invariant |
7865 | -- functions (including inherited ones). | |
d9f6a4ee | 7866 | |
eb66e842 | 7867 | Object_Entity := |
7868 | Defining_Identifier | |
7869 | (First (Parameter_Specifications (Specification (PDecl)))); | |
7870 | Object_Name := Chars (Object_Entity); | |
d9f6a4ee | 7871 | |
eb66e842 | 7872 | -- Add invariants for the current type |
9dc88aea | 7873 | |
eb66e842 | 7874 | Add_Invariants (Typ, Inherit => False); |
9dc88aea | 7875 | |
eb66e842 | 7876 | -- Add invariants for parent types |
9dc88aea | 7877 | |
eb66e842 | 7878 | declare |
7879 | Current_Typ : Entity_Id; | |
7880 | Parent_Typ : Entity_Id; | |
9dc88aea | 7881 | |
eb66e842 | 7882 | begin |
7883 | Current_Typ := Typ; | |
d97beb2f | 7884 | loop |
eb66e842 | 7885 | Parent_Typ := Etype (Current_Typ); |
9dc88aea | 7886 | |
eb66e842 | 7887 | if Is_Private_Type (Parent_Typ) |
7888 | and then Present (Full_View (Base_Type (Parent_Typ))) | |
d9f6a4ee | 7889 | then |
eb66e842 | 7890 | Parent_Typ := Full_View (Base_Type (Parent_Typ)); |
7891 | end if; | |
9dc88aea | 7892 | |
eb66e842 | 7893 | exit when Parent_Typ = Current_Typ; |
9dc88aea | 7894 | |
eb66e842 | 7895 | Current_Typ := Parent_Typ; |
7896 | Add_Invariants (Current_Typ, Inherit => True); | |
7897 | end loop; | |
7898 | end; | |
9dc88aea | 7899 | |
eb66e842 | 7900 | -- Build the procedure if we generated at least one Check pragma |
9dc88aea | 7901 | |
eb66e842 | 7902 | if Stmts /= No_List then |
7903 | Spec := Copy_Separate_Tree (Specification (PDecl)); | |
9dc88aea | 7904 | |
eb66e842 | 7905 | PBody := |
7906 | Make_Subprogram_Body (Loc, | |
7907 | Specification => Spec, | |
7908 | Declarations => Empty_List, | |
7909 | Handled_Statement_Sequence => | |
7910 | Make_Handled_Sequence_Of_Statements (Loc, | |
7911 | Statements => Stmts)); | |
9dc88aea | 7912 | |
eb66e842 | 7913 | -- Insert procedure declaration and spec at the appropriate points. |
7914 | -- If declaration is already analyzed, it was processed by the | |
7915 | -- generated pragma. | |
9dc88aea | 7916 | |
eb66e842 | 7917 | if Present (Private_Decls) then |
d97beb2f | 7918 | |
eb66e842 | 7919 | -- The spec goes at the end of visible declarations, but they have |
7920 | -- already been analyzed, so we need to explicitly do the analyze. | |
d9f6a4ee | 7921 | |
eb66e842 | 7922 | if not Analyzed (PDecl) then |
7923 | Append_To (Visible_Decls, PDecl); | |
7924 | Analyze (PDecl); | |
7925 | end if; | |
d9f6a4ee | 7926 | |
eb66e842 | 7927 | -- The body goes at the end of the private declarations, which we |
7928 | -- have not analyzed yet, so we do not need to perform an explicit | |
7929 | -- analyze call. We skip this if there are no private declarations | |
7930 | -- (this is an error that will be caught elsewhere); | |
d9f6a4ee | 7931 | |
eb66e842 | 7932 | Append_To (Private_Decls, PBody); |
d9f6a4ee | 7933 | |
eb66e842 | 7934 | -- If the invariant appears on the full view of a type, the |
7935 | -- analysis of the private part is complete, and we must | |
7936 | -- analyze the new body explicitly. | |
d9f6a4ee | 7937 | |
eb66e842 | 7938 | if In_Private_Part (Current_Scope) then |
7939 | Analyze (PBody); | |
7940 | end if; | |
d97beb2f | 7941 | |
eb66e842 | 7942 | -- If there are no private declarations this may be an error that |
7943 | -- will be diagnosed elsewhere. However, if this is a non-private | |
7944 | -- type that inherits invariants, it needs no completion and there | |
7945 | -- may be no private part. In this case insert invariant procedure | |
7946 | -- at end of current declarative list, and analyze at once, given | |
7947 | -- that the type is about to be frozen. | |
d97beb2f | 7948 | |
eb66e842 | 7949 | elsif not Is_Private_Type (Typ) then |
7950 | Append_To (Visible_Decls, PDecl); | |
7951 | Append_To (Visible_Decls, PBody); | |
7952 | Analyze (PDecl); | |
7953 | Analyze (PBody); | |
7954 | end if; | |
7955 | end if; | |
7956 | end Build_Invariant_Procedure; | |
d9f6a4ee | 7957 | |
eb66e842 | 7958 | ------------------------------- |
7959 | -- Build_Predicate_Functions -- | |
7960 | ------------------------------- | |
d9f6a4ee | 7961 | |
eb66e842 | 7962 | -- The procedures that are constructed here have the form: |
d9f6a4ee | 7963 | |
eb66e842 | 7964 | -- function typPredicate (Ixxx : typ) return Boolean is |
7965 | -- begin | |
7966 | -- return | |
7967 | -- exp1 and then exp2 and then ... | |
7968 | -- and then typ1Predicate (typ1 (Ixxx)) | |
7969 | -- and then typ2Predicate (typ2 (Ixxx)) | |
7970 | -- and then ...; | |
7971 | -- end typPredicate; | |
d9f6a4ee | 7972 | |
eb66e842 | 7973 | -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that |
7974 | -- this is the point at which these expressions get analyzed, providing the | |
7975 | -- required delay, and typ1, typ2, are entities from which predicates are | |
7976 | -- inherited. Note that we do NOT generate Check pragmas, that's because we | |
7977 | -- use this function even if checks are off, e.g. for membership tests. | |
d9f6a4ee | 7978 | |
eb66e842 | 7979 | -- If the expression has at least one Raise_Expression, then we also build |
7980 | -- the typPredicateM version of the function, in which any occurrence of a | |
7981 | -- Raise_Expression is converted to "return False". | |
d9f6a4ee | 7982 | |
eb66e842 | 7983 | procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is |
7984 | Loc : constant Source_Ptr := Sloc (Typ); | |
d9f6a4ee | 7985 | |
eb66e842 | 7986 | Expr : Node_Id; |
7987 | -- This is the expression for the result of the function. It is | |
7988 | -- is build by connecting the component predicates with AND THEN. | |
d9f6a4ee | 7989 | |
eb66e842 | 7990 | Expr_M : Node_Id; |
7991 | -- This is the corresponding return expression for the Predicate_M | |
7992 | -- function. It differs in that raise expressions are marked for | |
7993 | -- special expansion (see Process_REs). | |
d9f6a4ee | 7994 | |
eb66e842 | 7995 | Object_Name : constant Name_Id := New_Internal_Name ('I'); |
7996 | -- Name for argument of Predicate procedure. Note that we use the same | |
499918a7 | 7997 | -- name for both predicate functions. That way the reference within the |
eb66e842 | 7998 | -- predicate expression is the same in both functions. |
d9f6a4ee | 7999 | |
eb66e842 | 8000 | Object_Entity : constant Entity_Id := |
8001 | Make_Defining_Identifier (Loc, Chars => Object_Name); | |
8002 | -- Entity for argument of Predicate procedure | |
d9f6a4ee | 8003 | |
eb66e842 | 8004 | Object_Entity_M : constant Entity_Id := |
8005 | Make_Defining_Identifier (Loc, Chars => Object_Name); | |
8006 | -- Entity for argument of Predicate_M procedure | |
d9f6a4ee | 8007 | |
eb66e842 | 8008 | Raise_Expression_Present : Boolean := False; |
8009 | -- Set True if Expr has at least one Raise_Expression | |
d9f6a4ee | 8010 | |
eb66e842 | 8011 | procedure Add_Call (T : Entity_Id); |
8012 | -- Includes a call to the predicate function for type T in Expr if T | |
8013 | -- has predicates and Predicate_Function (T) is non-empty. | |
d9f6a4ee | 8014 | |
eb66e842 | 8015 | procedure Add_Predicates; |
8016 | -- Appends expressions for any Predicate pragmas in the rep item chain | |
8017 | -- Typ to Expr. Note that we look only at items for this exact entity. | |
8018 | -- Inheritance of predicates for the parent type is done by calling the | |
8019 | -- Predicate_Function of the parent type, using Add_Call above. | |
d9f6a4ee | 8020 | |
eb66e842 | 8021 | function Test_RE (N : Node_Id) return Traverse_Result; |
8022 | -- Used in Test_REs, tests one node for being a raise expression, and if | |
8023 | -- so sets Raise_Expression_Present True. | |
d9f6a4ee | 8024 | |
eb66e842 | 8025 | procedure Test_REs is new Traverse_Proc (Test_RE); |
8026 | -- Tests to see if Expr contains any raise expressions | |
d9f6a4ee | 8027 | |
eb66e842 | 8028 | function Process_RE (N : Node_Id) return Traverse_Result; |
8029 | -- Used in Process REs, tests if node N is a raise expression, and if | |
8030 | -- so, marks it to be converted to return False. | |
d9f6a4ee | 8031 | |
eb66e842 | 8032 | procedure Process_REs is new Traverse_Proc (Process_RE); |
8033 | -- Marks any raise expressions in Expr_M to return False | |
d9f6a4ee | 8034 | |
eb66e842 | 8035 | -------------- |
8036 | -- Add_Call -- | |
8037 | -------------- | |
d9f6a4ee | 8038 | |
eb66e842 | 8039 | procedure Add_Call (T : Entity_Id) is |
8040 | Exp : Node_Id; | |
d9f6a4ee | 8041 | |
eb66e842 | 8042 | begin |
8043 | if Present (T) and then Present (Predicate_Function (T)) then | |
8044 | Set_Has_Predicates (Typ); | |
d9f6a4ee | 8045 | |
eb66e842 | 8046 | -- Build the call to the predicate function of T |
d9f6a4ee | 8047 | |
eb66e842 | 8048 | Exp := |
8049 | Make_Predicate_Call | |
8050 | (T, Convert_To (T, Make_Identifier (Loc, Object_Name))); | |
d9f6a4ee | 8051 | |
eb66e842 | 8052 | -- Add call to evolving expression, using AND THEN if needed |
d9f6a4ee | 8053 | |
eb66e842 | 8054 | if No (Expr) then |
8055 | Expr := Exp; | |
3b23aaa0 | 8056 | |
eb66e842 | 8057 | else |
8058 | Expr := | |
3b23aaa0 | 8059 | Make_And_Then (Sloc (Expr), |
eb66e842 | 8060 | Left_Opnd => Relocate_Node (Expr), |
8061 | Right_Opnd => Exp); | |
8062 | end if; | |
d9f6a4ee | 8063 | |
eb66e842 | 8064 | -- Output info message on inheritance if required. Note we do not |
8065 | -- give this information for generic actual types, since it is | |
8066 | -- unwelcome noise in that case in instantiations. We also | |
8067 | -- generally suppress the message in instantiations, and also | |
8068 | -- if it involves internal names. | |
d9f6a4ee | 8069 | |
eb66e842 | 8070 | if Opt.List_Inherited_Aspects |
8071 | and then not Is_Generic_Actual_Type (Typ) | |
8072 | and then Instantiation_Depth (Sloc (Typ)) = 0 | |
8073 | and then not Is_Internal_Name (Chars (T)) | |
8074 | and then not Is_Internal_Name (Chars (Typ)) | |
8075 | then | |
8076 | Error_Msg_Sloc := Sloc (Predicate_Function (T)); | |
8077 | Error_Msg_Node_2 := T; | |
8078 | Error_Msg_N ("info: & inherits predicate from & #?L?", Typ); | |
8079 | end if; | |
8080 | end if; | |
8081 | end Add_Call; | |
d9f6a4ee | 8082 | |
eb66e842 | 8083 | -------------------- |
8084 | -- Add_Predicates -- | |
8085 | -------------------- | |
d9f6a4ee | 8086 | |
eb66e842 | 8087 | procedure Add_Predicates is |
8088 | Ritem : Node_Id; | |
8089 | Arg1 : Node_Id; | |
8090 | Arg2 : Node_Id; | |
d9f6a4ee | 8091 | |
eb66e842 | 8092 | procedure Replace_Type_Reference (N : Node_Id); |
8093 | -- Replace a single occurrence N of the subtype name with a reference | |
8094 | -- to the formal of the predicate function. N can be an identifier | |
8095 | -- referencing the subtype, or a selected component, representing an | |
8096 | -- appropriately qualified occurrence of the subtype name. | |
d9f6a4ee | 8097 | |
eb66e842 | 8098 | procedure Replace_Type_References is |
8099 | new Replace_Type_References_Generic (Replace_Type_Reference); | |
8100 | -- Traverse an expression changing every occurrence of an identifier | |
8101 | -- whose name matches the name of the subtype with a reference to | |
8102 | -- the formal parameter of the predicate function. | |
d9f6a4ee | 8103 | |
eb66e842 | 8104 | ---------------------------- |
8105 | -- Replace_Type_Reference -- | |
8106 | ---------------------------- | |
d9f6a4ee | 8107 | |
eb66e842 | 8108 | procedure Replace_Type_Reference (N : Node_Id) is |
8109 | begin | |
8110 | Rewrite (N, Make_Identifier (Sloc (N), Object_Name)); | |
8111 | -- Use the Sloc of the usage name, not the defining name | |
d97beb2f | 8112 | |
eb66e842 | 8113 | Set_Etype (N, Typ); |
8114 | Set_Entity (N, Object_Entity); | |
d97beb2f | 8115 | |
eb66e842 | 8116 | -- We want to treat the node as if it comes from source, so that |
8117 | -- ASIS will not ignore it | |
d97beb2f | 8118 | |
eb66e842 | 8119 | Set_Comes_From_Source (N, True); |
8120 | end Replace_Type_Reference; | |
d97beb2f | 8121 | |
eb66e842 | 8122 | -- Start of processing for Add_Predicates |
d97beb2f | 8123 | |
eb66e842 | 8124 | begin |
8125 | Ritem := First_Rep_Item (Typ); | |
8126 | while Present (Ritem) loop | |
8127 | if Nkind (Ritem) = N_Pragma | |
8128 | and then Pragma_Name (Ritem) = Name_Predicate | |
8129 | then | |
eb66e842 | 8130 | -- Acquire arguments |
d97beb2f | 8131 | |
eb66e842 | 8132 | Arg1 := First (Pragma_Argument_Associations (Ritem)); |
8133 | Arg2 := Next (Arg1); | |
d97beb2f | 8134 | |
eb66e842 | 8135 | Arg1 := Get_Pragma_Arg (Arg1); |
8136 | Arg2 := Get_Pragma_Arg (Arg2); | |
d97beb2f | 8137 | |
eb66e842 | 8138 | -- See if this predicate pragma is for the current type or for |
8139 | -- its full view. A predicate on a private completion is placed | |
8140 | -- on the partial view beause this is the visible entity that | |
8141 | -- is frozen. | |
639c3741 | 8142 | |
eb66e842 | 8143 | if Entity (Arg1) = Typ |
8144 | or else Full_View (Entity (Arg1)) = Typ | |
639c3741 | 8145 | then |
eb66e842 | 8146 | -- We have a match, this entry is for our subtype |
639c3741 | 8147 | |
eb66e842 | 8148 | -- We need to replace any occurrences of the name of the |
8149 | -- type with references to the object. | |
639c3741 | 8150 | |
37c6552c | 8151 | Replace_Type_References (Arg2, Typ); |
639c3741 | 8152 | |
eb66e842 | 8153 | -- If this predicate comes from an aspect, find the aspect |
8154 | -- specification, and replace the saved expression because | |
8155 | -- we need the subtype references replaced for the calls to | |
8156 | -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point | |
8157 | -- and Check_Aspect_At_End_Of_Declarations. | |
639c3741 | 8158 | |
eb66e842 | 8159 | if From_Aspect_Specification (Ritem) then |
8160 | declare | |
8161 | Aitem : Node_Id; | |
639c3741 | 8162 | |
eb66e842 | 8163 | begin |
8164 | -- Loop to find corresponding aspect, note that this | |
8165 | -- must be present given the pragma is marked delayed. | |
639c3741 | 8166 | |
eb66e842 | 8167 | Aitem := Next_Rep_Item (Ritem); |
8168 | loop | |
8169 | if Nkind (Aitem) = N_Aspect_Specification | |
8170 | and then Aspect_Rep_Item (Aitem) = Ritem | |
8171 | then | |
8172 | Set_Entity | |
8173 | (Identifier (Aitem), New_Copy_Tree (Arg2)); | |
8174 | exit; | |
8175 | end if; | |
639c3741 | 8176 | |
eb66e842 | 8177 | Aitem := Next_Rep_Item (Aitem); |
8178 | end loop; | |
8179 | end; | |
8180 | end if; | |
737e8460 | 8181 | |
eb66e842 | 8182 | -- Now we can add the expression |
737e8460 | 8183 | |
eb66e842 | 8184 | if No (Expr) then |
8185 | Expr := Relocate_Node (Arg2); | |
d97beb2f | 8186 | |
eb66e842 | 8187 | -- There already was a predicate, so add to it |
d97beb2f | 8188 | |
eb66e842 | 8189 | else |
8190 | Expr := | |
8191 | Make_And_Then (Loc, | |
8192 | Left_Opnd => Relocate_Node (Expr), | |
8193 | Right_Opnd => Relocate_Node (Arg2)); | |
8194 | end if; | |
8195 | end if; | |
8196 | end if; | |
d97beb2f | 8197 | |
eb66e842 | 8198 | Next_Rep_Item (Ritem); |
8199 | end loop; | |
8200 | end Add_Predicates; | |
d97beb2f | 8201 | |
eb66e842 | 8202 | ---------------- |
8203 | -- Process_RE -- | |
8204 | ---------------- | |
d97beb2f | 8205 | |
eb66e842 | 8206 | function Process_RE (N : Node_Id) return Traverse_Result is |
d9f6a4ee | 8207 | begin |
eb66e842 | 8208 | if Nkind (N) = N_Raise_Expression then |
8209 | Set_Convert_To_Return_False (N); | |
8210 | return Skip; | |
d9f6a4ee | 8211 | else |
eb66e842 | 8212 | return OK; |
d9f6a4ee | 8213 | end if; |
eb66e842 | 8214 | end Process_RE; |
d7c2851f | 8215 | |
d9f6a4ee | 8216 | ------------- |
eb66e842 | 8217 | -- Test_RE -- |
d9f6a4ee | 8218 | ------------- |
d7c2851f | 8219 | |
eb66e842 | 8220 | function Test_RE (N : Node_Id) return Traverse_Result is |
d97beb2f | 8221 | begin |
eb66e842 | 8222 | if Nkind (N) = N_Raise_Expression then |
8223 | Raise_Expression_Present := True; | |
8224 | return Abandon; | |
8225 | else | |
8226 | return OK; | |
8227 | end if; | |
8228 | end Test_RE; | |
d97beb2f | 8229 | |
eb66e842 | 8230 | -- Start of processing for Build_Predicate_Functions |
d97beb2f | 8231 | |
eb66e842 | 8232 | begin |
8233 | -- Return if already built or if type does not have predicates | |
9dc88aea | 8234 | |
eb66e842 | 8235 | if not Has_Predicates (Typ) |
8236 | or else Present (Predicate_Function (Typ)) | |
8237 | then | |
8238 | return; | |
8239 | end if; | |
d9f6a4ee | 8240 | |
eb66e842 | 8241 | -- Prepare to construct predicate expression |
d97beb2f | 8242 | |
eb66e842 | 8243 | Expr := Empty; |
d97beb2f | 8244 | |
eb66e842 | 8245 | -- Add Predicates for the current type |
d97beb2f | 8246 | |
eb66e842 | 8247 | Add_Predicates; |
d97beb2f | 8248 | |
eb66e842 | 8249 | -- Add predicates for ancestor if present |
d97beb2f | 8250 | |
eb66e842 | 8251 | declare |
8252 | Atyp : constant Entity_Id := Nearest_Ancestor (Typ); | |
d9f6a4ee | 8253 | begin |
eb66e842 | 8254 | if Present (Atyp) then |
8255 | Add_Call (Atyp); | |
8256 | end if; | |
8257 | end; | |
9dc88aea | 8258 | |
eb66e842 | 8259 | -- Case where predicates are present |
9dc88aea | 8260 | |
eb66e842 | 8261 | if Present (Expr) then |
726fd56a | 8262 | |
eb66e842 | 8263 | -- Test for raise expression present |
726fd56a | 8264 | |
eb66e842 | 8265 | Test_REs (Expr); |
9dc88aea | 8266 | |
eb66e842 | 8267 | -- If raise expression is present, capture a copy of Expr for use |
8268 | -- in building the predicateM function version later on. For this | |
8269 | -- copy we replace references to Object_Entity by Object_Entity_M. | |
9dc88aea | 8270 | |
eb66e842 | 8271 | if Raise_Expression_Present then |
8272 | declare | |
299b347e | 8273 | Map : constant Elist_Id := New_Elmt_List; |
8274 | New_V : Entity_Id := Empty; | |
8275 | ||
8276 | -- The unanalyzed expression will be copied and appear in | |
8277 | -- both functions. Normally expressions do not declare new | |
8278 | -- entities, but quantified expressions do, so we need to | |
8279 | -- create new entities for their bound variables, to prevent | |
8280 | -- multiple definitions in gigi. | |
8281 | ||
8282 | function Reset_Loop_Variable (N : Node_Id) | |
8283 | return Traverse_Result; | |
8284 | ||
8285 | procedure Collect_Loop_Variables is | |
8286 | new Traverse_Proc (Reset_Loop_Variable); | |
8287 | ||
8288 | ------------------------ | |
8289 | -- Reset_Loop_Variable -- | |
8290 | ------------------------ | |
8291 | ||
8292 | function Reset_Loop_Variable (N : Node_Id) | |
8293 | return Traverse_Result | |
8294 | is | |
8295 | begin | |
8296 | if Nkind (N) = N_Iterator_Specification then | |
8297 | New_V := Make_Defining_Identifier | |
8298 | (Sloc (N), Chars (Defining_Identifier (N))); | |
8299 | ||
8300 | Set_Defining_Identifier (N, New_V); | |
8301 | end if; | |
8302 | ||
8303 | return OK; | |
8304 | end Reset_Loop_Variable; | |
8305 | ||
eb66e842 | 8306 | begin |
8307 | Append_Elmt (Object_Entity, Map); | |
8308 | Append_Elmt (Object_Entity_M, Map); | |
8309 | Expr_M := New_Copy_Tree (Expr, Map => Map); | |
299b347e | 8310 | Collect_Loop_Variables (Expr_M); |
eb66e842 | 8311 | end; |
8312 | end if; | |
d97beb2f | 8313 | |
eb66e842 | 8314 | -- Build the main predicate function |
9dc88aea | 8315 | |
eb66e842 | 8316 | declare |
8317 | SId : constant Entity_Id := | |
8318 | Make_Defining_Identifier (Loc, | |
8319 | Chars => New_External_Name (Chars (Typ), "Predicate")); | |
8320 | -- The entity for the the function spec | |
9dc88aea | 8321 | |
eb66e842 | 8322 | SIdB : constant Entity_Id := |
8323 | Make_Defining_Identifier (Loc, | |
8324 | Chars => New_External_Name (Chars (Typ), "Predicate")); | |
8325 | -- The entity for the function body | |
9dc88aea | 8326 | |
eb66e842 | 8327 | Spec : Node_Id; |
8328 | FDecl : Node_Id; | |
8329 | FBody : Node_Id; | |
9dc88aea | 8330 | |
eb66e842 | 8331 | begin |
8332 | -- Build function declaration | |
d97beb2f | 8333 | |
eb66e842 | 8334 | Set_Ekind (SId, E_Function); |
8335 | Set_Is_Internal (SId); | |
8336 | Set_Is_Predicate_Function (SId); | |
8337 | Set_Predicate_Function (Typ, SId); | |
d97beb2f | 8338 | |
eb66e842 | 8339 | -- The predicate function is shared between views of a type |
d97beb2f | 8340 | |
eb66e842 | 8341 | if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then |
8342 | Set_Predicate_Function (Full_View (Typ), SId); | |
d97beb2f | 8343 | end if; |
d97beb2f | 8344 | |
eb66e842 | 8345 | Spec := |
8346 | Make_Function_Specification (Loc, | |
8347 | Defining_Unit_Name => SId, | |
8348 | Parameter_Specifications => New_List ( | |
8349 | Make_Parameter_Specification (Loc, | |
8350 | Defining_Identifier => Object_Entity, | |
8351 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), | |
8352 | Result_Definition => | |
8353 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
d97beb2f | 8354 | |
eb66e842 | 8355 | FDecl := |
8356 | Make_Subprogram_Declaration (Loc, | |
8357 | Specification => Spec); | |
d97beb2f | 8358 | |
eb66e842 | 8359 | -- Build function body |
d97beb2f | 8360 | |
eb66e842 | 8361 | Spec := |
8362 | Make_Function_Specification (Loc, | |
8363 | Defining_Unit_Name => SIdB, | |
8364 | Parameter_Specifications => New_List ( | |
8365 | Make_Parameter_Specification (Loc, | |
8366 | Defining_Identifier => | |
8367 | Make_Defining_Identifier (Loc, Object_Name), | |
8368 | Parameter_Type => | |
8369 | New_Occurrence_Of (Typ, Loc))), | |
8370 | Result_Definition => | |
8371 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
d97beb2f | 8372 | |
eb66e842 | 8373 | FBody := |
8374 | Make_Subprogram_Body (Loc, | |
8375 | Specification => Spec, | |
8376 | Declarations => Empty_List, | |
8377 | Handled_Statement_Sequence => | |
8378 | Make_Handled_Sequence_Of_Statements (Loc, | |
8379 | Statements => New_List ( | |
8380 | Make_Simple_Return_Statement (Loc, | |
8381 | Expression => Expr)))); | |
9dc88aea | 8382 | |
eb66e842 | 8383 | -- Insert declaration before freeze node and body after |
d97beb2f | 8384 | |
eb66e842 | 8385 | Insert_Before_And_Analyze (N, FDecl); |
8386 | Insert_After_And_Analyze (N, FBody); | |
d9f6a4ee | 8387 | end; |
d97beb2f | 8388 | |
eb66e842 | 8389 | -- Test for raise expressions present and if so build M version |
d97beb2f | 8390 | |
eb66e842 | 8391 | if Raise_Expression_Present then |
8392 | declare | |
8393 | SId : constant Entity_Id := | |
8394 | Make_Defining_Identifier (Loc, | |
8395 | Chars => New_External_Name (Chars (Typ), "PredicateM")); | |
8396 | -- The entity for the the function spec | |
d97beb2f | 8397 | |
eb66e842 | 8398 | SIdB : constant Entity_Id := |
8399 | Make_Defining_Identifier (Loc, | |
8400 | Chars => New_External_Name (Chars (Typ), "PredicateM")); | |
8401 | -- The entity for the function body | |
b9e61b2a | 8402 | |
eb66e842 | 8403 | Spec : Node_Id; |
8404 | FDecl : Node_Id; | |
8405 | FBody : Node_Id; | |
8406 | BTemp : Entity_Id; | |
d97beb2f | 8407 | |
eb66e842 | 8408 | begin |
8409 | -- Mark any raise expressions for special expansion | |
d97beb2f | 8410 | |
eb66e842 | 8411 | Process_REs (Expr_M); |
d97beb2f | 8412 | |
eb66e842 | 8413 | -- Build function declaration |
d97beb2f | 8414 | |
eb66e842 | 8415 | Set_Ekind (SId, E_Function); |
8416 | Set_Is_Predicate_Function_M (SId); | |
8417 | Set_Predicate_Function_M (Typ, SId); | |
d97beb2f | 8418 | |
eb66e842 | 8419 | -- The predicate function is shared between views of a type |
d97beb2f | 8420 | |
eb66e842 | 8421 | if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then |
8422 | Set_Predicate_Function_M (Full_View (Typ), SId); | |
8423 | end if; | |
9dc88aea | 8424 | |
eb66e842 | 8425 | Spec := |
8426 | Make_Function_Specification (Loc, | |
8427 | Defining_Unit_Name => SId, | |
8428 | Parameter_Specifications => New_List ( | |
8429 | Make_Parameter_Specification (Loc, | |
8430 | Defining_Identifier => Object_Entity_M, | |
8431 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), | |
8432 | Result_Definition => | |
8433 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
9dc88aea | 8434 | |
eb66e842 | 8435 | FDecl := |
8436 | Make_Subprogram_Declaration (Loc, | |
8437 | Specification => Spec); | |
9dc88aea | 8438 | |
eb66e842 | 8439 | -- Build function body |
9dc88aea | 8440 | |
eb66e842 | 8441 | Spec := |
8442 | Make_Function_Specification (Loc, | |
8443 | Defining_Unit_Name => SIdB, | |
8444 | Parameter_Specifications => New_List ( | |
8445 | Make_Parameter_Specification (Loc, | |
8446 | Defining_Identifier => | |
8447 | Make_Defining_Identifier (Loc, Object_Name), | |
8448 | Parameter_Type => | |
8449 | New_Occurrence_Of (Typ, Loc))), | |
8450 | Result_Definition => | |
8451 | New_Occurrence_Of (Standard_Boolean, Loc)); | |
9dc88aea | 8452 | |
eb66e842 | 8453 | -- Build the body, we declare the boolean expression before |
8454 | -- doing the return, because we are not really confident of | |
8455 | -- what happens if a return appears within a return. | |
9dc88aea | 8456 | |
eb66e842 | 8457 | BTemp := |
8458 | Make_Defining_Identifier (Loc, | |
8459 | Chars => New_Internal_Name ('B')); | |
9dc88aea | 8460 | |
eb66e842 | 8461 | FBody := |
8462 | Make_Subprogram_Body (Loc, | |
8463 | Specification => Spec, | |
9dc88aea | 8464 | |
eb66e842 | 8465 | Declarations => New_List ( |
8466 | Make_Object_Declaration (Loc, | |
8467 | Defining_Identifier => BTemp, | |
8468 | Constant_Present => True, | |
8469 | Object_Definition => | |
8470 | New_Occurrence_Of (Standard_Boolean, Loc), | |
8471 | Expression => Expr_M)), | |
d97beb2f | 8472 | |
eb66e842 | 8473 | Handled_Statement_Sequence => |
8474 | Make_Handled_Sequence_Of_Statements (Loc, | |
8475 | Statements => New_List ( | |
8476 | Make_Simple_Return_Statement (Loc, | |
8477 | Expression => New_Occurrence_Of (BTemp, Loc))))); | |
d97beb2f | 8478 | |
eb66e842 | 8479 | -- Insert declaration before freeze node and body after |
d97beb2f | 8480 | |
eb66e842 | 8481 | Insert_Before_And_Analyze (N, FDecl); |
8482 | Insert_After_And_Analyze (N, FBody); | |
8483 | end; | |
8484 | end if; | |
9dc88aea | 8485 | |
3b23aaa0 | 8486 | -- See if we have a static predicate. Note that the answer may be |
8487 | -- yes even if we have an explicit Dynamic_Predicate present. | |
9dc88aea | 8488 | |
3b23aaa0 | 8489 | declare |
94d896aa | 8490 | PS : Boolean; |
3b23aaa0 | 8491 | EN : Node_Id; |
9dc88aea | 8492 | |
3b23aaa0 | 8493 | begin |
94d896aa | 8494 | if not Is_Scalar_Type (Typ) and then not Is_String_Type (Typ) then |
8495 | PS := False; | |
8496 | else | |
8497 | PS := Is_Predicate_Static (Expr, Object_Name); | |
8498 | end if; | |
8499 | ||
a360a0f7 | 8500 | -- Case where we have a predicate-static aspect |
9dc88aea | 8501 | |
3b23aaa0 | 8502 | if PS then |
9dc88aea | 8503 | |
3b23aaa0 | 8504 | -- We don't set Has_Static_Predicate_Aspect, since we can have |
8505 | -- any of the three cases (Predicate, Dynamic_Predicate, or | |
8506 | -- Static_Predicate) generating a predicate with an expression | |
a360a0f7 | 8507 | -- that is predicate-static. We just indicate that we have a |
3b23aaa0 | 8508 | -- predicate that can be treated as static. |
d7c2851f | 8509 | |
3b23aaa0 | 8510 | Set_Has_Static_Predicate (Typ); |
d7c2851f | 8511 | |
3b23aaa0 | 8512 | -- For discrete subtype, build the static predicate list |
9dc88aea | 8513 | |
3b23aaa0 | 8514 | if Is_Discrete_Type (Typ) then |
d0988351 | 8515 | if not Is_Static_Subtype (Typ) then |
8516 | ||
8517 | -- This can only happen in the presence of previous | |
8518 | -- semantic errors. | |
8519 | ||
8520 | pragma Assert (Serious_Errors_Detected > 0); | |
8521 | return; | |
8522 | end if; | |
8523 | ||
3b23aaa0 | 8524 | Build_Discrete_Static_Predicate (Typ, Expr, Object_Name); |
8525 | ||
8526 | -- If we don't get a static predicate list, it means that we | |
8527 | -- have a case where this is not possible, most typically in | |
8528 | -- the case where we inherit a dynamic predicate. We do not | |
8529 | -- consider this an error, we just leave the predicate as | |
8530 | -- dynamic. But if we do succeed in building the list, then | |
8531 | -- we mark the predicate as static. | |
8532 | ||
5c6a5792 | 8533 | if No (Static_Discrete_Predicate (Typ)) then |
3b23aaa0 | 8534 | Set_Has_Static_Predicate (Typ, False); |
8535 | end if; | |
94d896aa | 8536 | |
8537 | -- For real or string subtype, save predicate expression | |
8538 | ||
8539 | elsif Is_Real_Type (Typ) or else Is_String_Type (Typ) then | |
8540 | Set_Static_Real_Or_String_Predicate (Typ, Expr); | |
3b23aaa0 | 8541 | end if; |
8542 | ||
8543 | -- Case of dynamic predicate (expression is not predicate-static) | |
9dc88aea | 8544 | |
eb66e842 | 8545 | else |
3b23aaa0 | 8546 | -- Again, we don't set Has_Dynamic_Predicate_Aspect, since that |
8547 | -- is only set if we have an explicit Dynamic_Predicate aspect | |
8548 | -- given. Here we may simply have a Predicate aspect where the | |
8549 | -- expression happens not to be predicate-static. | |
8550 | ||
8551 | -- Emit an error when the predicate is categorized as static | |
8552 | -- but its expression is not predicate-static. | |
8553 | ||
8554 | -- First a little fiddling to get a nice location for the | |
8555 | -- message. If the expression is of the form (A and then B), | |
8556 | -- then use the left operand for the Sloc. This avoids getting | |
a360a0f7 | 8557 | -- confused by a call to a higher-level predicate with a less |
3b23aaa0 | 8558 | -- convenient source location. |
8559 | ||
8560 | EN := Expr; | |
8561 | while Nkind (EN) = N_And_Then loop | |
8562 | EN := Left_Opnd (EN); | |
8563 | end loop; | |
8564 | ||
8565 | -- Now post appropriate message | |
8566 | ||
8567 | if Has_Static_Predicate_Aspect (Typ) then | |
94d896aa | 8568 | if Is_Scalar_Type (Typ) or else Is_String_Type (Typ) then |
3b23aaa0 | 8569 | Error_Msg_F |
26279d91 | 8570 | ("expression is not predicate-static (RM 3.2.4(16-22))", |
3b23aaa0 | 8571 | EN); |
8572 | else | |
94d896aa | 8573 | Error_Msg_F |
8574 | ("static predicate requires scalar or string type", EN); | |
3b23aaa0 | 8575 | end if; |
8576 | end if; | |
eb66e842 | 8577 | end if; |
3b23aaa0 | 8578 | end; |
eb66e842 | 8579 | end if; |
8580 | end Build_Predicate_Functions; | |
9dc88aea | 8581 | |
d9f6a4ee | 8582 | ----------------------------------------- |
8583 | -- Check_Aspect_At_End_Of_Declarations -- | |
8584 | ----------------------------------------- | |
9dc88aea | 8585 | |
d9f6a4ee | 8586 | procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is |
8587 | Ent : constant Entity_Id := Entity (ASN); | |
8588 | Ident : constant Node_Id := Identifier (ASN); | |
8589 | A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident)); | |
d7c2851f | 8590 | |
d9f6a4ee | 8591 | End_Decl_Expr : constant Node_Id := Entity (Ident); |
8592 | -- Expression to be analyzed at end of declarations | |
d7c2851f | 8593 | |
d9f6a4ee | 8594 | Freeze_Expr : constant Node_Id := Expression (ASN); |
8595 | -- Expression from call to Check_Aspect_At_Freeze_Point | |
d7c2851f | 8596 | |
d9f6a4ee | 8597 | T : constant Entity_Id := Etype (Freeze_Expr); |
8598 | -- Type required for preanalyze call | |
d7c2851f | 8599 | |
d9f6a4ee | 8600 | Err : Boolean; |
8601 | -- Set False if error | |
9dc88aea | 8602 | |
d9f6a4ee | 8603 | -- On entry to this procedure, Entity (Ident) contains a copy of the |
8604 | -- original expression from the aspect, saved for this purpose, and | |
8605 | -- but Expression (Ident) is a preanalyzed copy of the expression, | |
8606 | -- preanalyzed just after the freeze point. | |
9dc88aea | 8607 | |
d9f6a4ee | 8608 | procedure Check_Overloaded_Name; |
8609 | -- For aspects whose expression is simply a name, this routine checks if | |
8610 | -- the name is overloaded or not. If so, it verifies there is an | |
8611 | -- interpretation that matches the entity obtained at the freeze point, | |
8612 | -- otherwise the compiler complains. | |
9dc88aea | 8613 | |
d9f6a4ee | 8614 | --------------------------- |
8615 | -- Check_Overloaded_Name -- | |
8616 | --------------------------- | |
8617 | ||
8618 | procedure Check_Overloaded_Name is | |
d97beb2f | 8619 | begin |
d9f6a4ee | 8620 | if not Is_Overloaded (End_Decl_Expr) then |
5ac76cee | 8621 | Err := not Is_Entity_Name (End_Decl_Expr) |
8622 | or else Entity (End_Decl_Expr) /= Entity (Freeze_Expr); | |
d9f6a4ee | 8623 | |
d97beb2f | 8624 | else |
d9f6a4ee | 8625 | Err := True; |
9dc88aea | 8626 | |
d9f6a4ee | 8627 | declare |
8628 | Index : Interp_Index; | |
8629 | It : Interp; | |
9dc88aea | 8630 | |
d9f6a4ee | 8631 | begin |
8632 | Get_First_Interp (End_Decl_Expr, Index, It); | |
8633 | while Present (It.Typ) loop | |
8634 | if It.Nam = Entity (Freeze_Expr) then | |
8635 | Err := False; | |
8636 | exit; | |
8637 | end if; | |
8638 | ||
8639 | Get_Next_Interp (Index, It); | |
8640 | end loop; | |
8641 | end; | |
9dc88aea | 8642 | end if; |
d9f6a4ee | 8643 | end Check_Overloaded_Name; |
9dc88aea | 8644 | |
d9f6a4ee | 8645 | -- Start of processing for Check_Aspect_At_End_Of_Declarations |
9dc88aea | 8646 | |
d9f6a4ee | 8647 | begin |
8648 | -- Case of aspects Dimension, Dimension_System and Synchronization | |
9dc88aea | 8649 | |
d9f6a4ee | 8650 | if A_Id = Aspect_Synchronization then |
8651 | return; | |
d97beb2f | 8652 | |
d9f6a4ee | 8653 | -- Case of stream attributes, just have to compare entities. However, |
8654 | -- the expression is just a name (possibly overloaded), and there may | |
8655 | -- be stream operations declared for unrelated types, so we just need | |
8656 | -- to verify that one of these interpretations is the one available at | |
8657 | -- at the freeze point. | |
9dc88aea | 8658 | |
d9f6a4ee | 8659 | elsif A_Id = Aspect_Input or else |
f02a9a9a | 8660 | A_Id = Aspect_Output or else |
8661 | A_Id = Aspect_Read or else | |
8662 | A_Id = Aspect_Write | |
d9f6a4ee | 8663 | then |
8664 | Analyze (End_Decl_Expr); | |
8665 | Check_Overloaded_Name; | |
9dc88aea | 8666 | |
d9f6a4ee | 8667 | elsif A_Id = Aspect_Variable_Indexing or else |
8668 | A_Id = Aspect_Constant_Indexing or else | |
8669 | A_Id = Aspect_Default_Iterator or else | |
8670 | A_Id = Aspect_Iterator_Element | |
8671 | then | |
8672 | -- Make type unfrozen before analysis, to prevent spurious errors | |
8673 | -- about late attributes. | |
9dc88aea | 8674 | |
d9f6a4ee | 8675 | Set_Is_Frozen (Ent, False); |
8676 | Analyze (End_Decl_Expr); | |
8677 | Set_Is_Frozen (Ent, True); | |
9dc88aea | 8678 | |
d9f6a4ee | 8679 | -- If the end of declarations comes before any other freeze |
8680 | -- point, the Freeze_Expr is not analyzed: no check needed. | |
9dc88aea | 8681 | |
d9f6a4ee | 8682 | if Analyzed (Freeze_Expr) and then not In_Instance then |
8683 | Check_Overloaded_Name; | |
8684 | else | |
8685 | Err := False; | |
8686 | end if; | |
55e8372b | 8687 | |
d9f6a4ee | 8688 | -- All other cases |
55e8372b | 8689 | |
d9f6a4ee | 8690 | else |
c1efebf9 | 8691 | -- Indicate that the expression comes from an aspect specification, |
8692 | -- which is used in subsequent analysis even if expansion is off. | |
8693 | ||
8694 | Set_Parent (End_Decl_Expr, ASN); | |
8695 | ||
d9f6a4ee | 8696 | -- In a generic context the aspect expressions have not been |
8697 | -- preanalyzed, so do it now. There are no conformance checks | |
8698 | -- to perform in this case. | |
55e8372b | 8699 | |
d9f6a4ee | 8700 | if No (T) then |
8701 | Check_Aspect_At_Freeze_Point (ASN); | |
8702 | return; | |
55e8372b | 8703 | |
d9f6a4ee | 8704 | -- The default values attributes may be defined in the private part, |
8705 | -- and the analysis of the expression may take place when only the | |
8706 | -- partial view is visible. The expression must be scalar, so use | |
8707 | -- the full view to resolve. | |
55e8372b | 8708 | |
d9f6a4ee | 8709 | elsif (A_Id = Aspect_Default_Value |
8710 | or else | |
8711 | A_Id = Aspect_Default_Component_Value) | |
8712 | and then Is_Private_Type (T) | |
8713 | then | |
8714 | Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T)); | |
c1efebf9 | 8715 | |
d9f6a4ee | 8716 | else |
8717 | Preanalyze_Spec_Expression (End_Decl_Expr, T); | |
8718 | end if; | |
d97beb2f | 8719 | |
d9f6a4ee | 8720 | Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr); |
8721 | end if; | |
55e8372b | 8722 | |
c1efebf9 | 8723 | -- Output error message if error. Force error on aspect specification |
8724 | -- even if there is an error on the expression itself. | |
55e8372b | 8725 | |
d9f6a4ee | 8726 | if Err then |
8727 | Error_Msg_NE | |
c1efebf9 | 8728 | ("!visibility of aspect for& changes after freeze point", |
d9f6a4ee | 8729 | ASN, Ent); |
8730 | Error_Msg_NE | |
8731 | ("info: & is frozen here, aspects evaluated at this point??", | |
8732 | Freeze_Node (Ent), Ent); | |
8733 | end if; | |
8734 | end Check_Aspect_At_End_Of_Declarations; | |
55e8372b | 8735 | |
d9f6a4ee | 8736 | ---------------------------------- |
8737 | -- Check_Aspect_At_Freeze_Point -- | |
8738 | ---------------------------------- | |
9dc88aea | 8739 | |
d9f6a4ee | 8740 | procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is |
8741 | Ident : constant Node_Id := Identifier (ASN); | |
8742 | -- Identifier (use Entity field to save expression) | |
9dc88aea | 8743 | |
d9f6a4ee | 8744 | A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident)); |
9dc88aea | 8745 | |
d9f6a4ee | 8746 | T : Entity_Id := Empty; |
8747 | -- Type required for preanalyze call | |
9dc88aea | 8748 | |
d9f6a4ee | 8749 | begin |
8750 | -- On entry to this procedure, Entity (Ident) contains a copy of the | |
8751 | -- original expression from the aspect, saved for this purpose. | |
9dc88aea | 8752 | |
d9f6a4ee | 8753 | -- On exit from this procedure Entity (Ident) is unchanged, still |
8754 | -- containing that copy, but Expression (Ident) is a preanalyzed copy | |
8755 | -- of the expression, preanalyzed just after the freeze point. | |
d97beb2f | 8756 | |
d9f6a4ee | 8757 | -- Make a copy of the expression to be preanalyzed |
d97beb2f | 8758 | |
d9f6a4ee | 8759 | Set_Expression (ASN, New_Copy_Tree (Entity (Ident))); |
d97beb2f | 8760 | |
d9f6a4ee | 8761 | -- Find type for preanalyze call |
d97beb2f | 8762 | |
d9f6a4ee | 8763 | case A_Id is |
9dc88aea | 8764 | |
d9f6a4ee | 8765 | -- No_Aspect should be impossible |
d97beb2f | 8766 | |
d9f6a4ee | 8767 | when No_Aspect => |
8768 | raise Program_Error; | |
8769 | ||
8770 | -- Aspects taking an optional boolean argument | |
d97beb2f | 8771 | |
d9f6a4ee | 8772 | when Boolean_Aspects | |
8773 | Library_Unit_Aspects => | |
9dc88aea | 8774 | |
d9f6a4ee | 8775 | T := Standard_Boolean; |
d7c2851f | 8776 | |
d9f6a4ee | 8777 | -- Aspects corresponding to attribute definition clauses |
9dc88aea | 8778 | |
d9f6a4ee | 8779 | when Aspect_Address => |
8780 | T := RTE (RE_Address); | |
9dc88aea | 8781 | |
d9f6a4ee | 8782 | when Aspect_Attach_Handler => |
8783 | T := RTE (RE_Interrupt_ID); | |
d7c2851f | 8784 | |
d9f6a4ee | 8785 | when Aspect_Bit_Order | Aspect_Scalar_Storage_Order => |
8786 | T := RTE (RE_Bit_Order); | |
d7c2851f | 8787 | |
d9f6a4ee | 8788 | when Aspect_Convention => |
8789 | return; | |
d7c2851f | 8790 | |
d9f6a4ee | 8791 | when Aspect_CPU => |
8792 | T := RTE (RE_CPU_Range); | |
d7c2851f | 8793 | |
d9f6a4ee | 8794 | -- Default_Component_Value is resolved with the component type |
d7c2851f | 8795 | |
d9f6a4ee | 8796 | when Aspect_Default_Component_Value => |
8797 | T := Component_Type (Entity (ASN)); | |
d7c2851f | 8798 | |
647fab54 | 8799 | when Aspect_Default_Storage_Pool => |
8800 | T := Class_Wide_Type (RTE (RE_Root_Storage_Pool)); | |
8801 | ||
d9f6a4ee | 8802 | -- Default_Value is resolved with the type entity in question |
d7c2851f | 8803 | |
d9f6a4ee | 8804 | when Aspect_Default_Value => |
8805 | T := Entity (ASN); | |
9dc88aea | 8806 | |
d9f6a4ee | 8807 | when Aspect_Dispatching_Domain => |
8808 | T := RTE (RE_Dispatching_Domain); | |
9dc88aea | 8809 | |
d9f6a4ee | 8810 | when Aspect_External_Tag => |
8811 | T := Standard_String; | |
9dc88aea | 8812 | |
d9f6a4ee | 8813 | when Aspect_External_Name => |
8814 | T := Standard_String; | |
9dc88aea | 8815 | |
d9f6a4ee | 8816 | when Aspect_Link_Name => |
8817 | T := Standard_String; | |
9dc88aea | 8818 | |
d9f6a4ee | 8819 | when Aspect_Priority | Aspect_Interrupt_Priority => |
8820 | T := Standard_Integer; | |
d97beb2f | 8821 | |
d9f6a4ee | 8822 | when Aspect_Relative_Deadline => |
8823 | T := RTE (RE_Time_Span); | |
d97beb2f | 8824 | |
d9f6a4ee | 8825 | when Aspect_Small => |
8826 | T := Universal_Real; | |
490beba6 | 8827 | |
d9f6a4ee | 8828 | -- For a simple storage pool, we have to retrieve the type of the |
8829 | -- pool object associated with the aspect's corresponding attribute | |
8830 | -- definition clause. | |
490beba6 | 8831 | |
d9f6a4ee | 8832 | when Aspect_Simple_Storage_Pool => |
8833 | T := Etype (Expression (Aspect_Rep_Item (ASN))); | |
d97beb2f | 8834 | |
d9f6a4ee | 8835 | when Aspect_Storage_Pool => |
8836 | T := Class_Wide_Type (RTE (RE_Root_Storage_Pool)); | |
d97beb2f | 8837 | |
d9f6a4ee | 8838 | when Aspect_Alignment | |
8839 | Aspect_Component_Size | | |
8840 | Aspect_Machine_Radix | | |
8841 | Aspect_Object_Size | | |
8842 | Aspect_Size | | |
8843 | Aspect_Storage_Size | | |
8844 | Aspect_Stream_Size | | |
8845 | Aspect_Value_Size => | |
8846 | T := Any_Integer; | |
9dc88aea | 8847 | |
04ae062f | 8848 | when Aspect_Linker_Section => |
8849 | T := Standard_String; | |
8850 | ||
d9f6a4ee | 8851 | when Aspect_Synchronization => |
8852 | return; | |
7d20685d | 8853 | |
d9f6a4ee | 8854 | -- Special case, the expression of these aspects is just an entity |
8855 | -- that does not need any resolution, so just analyze. | |
7d20685d | 8856 | |
d9f6a4ee | 8857 | when Aspect_Input | |
8858 | Aspect_Output | | |
8859 | Aspect_Read | | |
8860 | Aspect_Suppress | | |
8861 | Aspect_Unsuppress | | |
8862 | Aspect_Warnings | | |
8863 | Aspect_Write => | |
8864 | Analyze (Expression (ASN)); | |
8865 | return; | |
7d20685d | 8866 | |
d9f6a4ee | 8867 | -- Same for Iterator aspects, where the expression is a function |
8868 | -- name. Legality rules are checked separately. | |
89f1e35c | 8869 | |
d9f6a4ee | 8870 | when Aspect_Constant_Indexing | |
8871 | Aspect_Default_Iterator | | |
8872 | Aspect_Iterator_Element | | |
8873 | Aspect_Variable_Indexing => | |
8874 | Analyze (Expression (ASN)); | |
8875 | return; | |
7d20685d | 8876 | |
b3f8228a | 8877 | -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect. |
8878 | ||
8879 | when Aspect_Iterable => | |
3061ffde | 8880 | T := Entity (ASN); |
8881 | ||
b3f8228a | 8882 | declare |
a9f5fea7 | 8883 | Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T); |
3061ffde | 8884 | Assoc : Node_Id; |
8885 | Expr : Node_Id; | |
a9f5fea7 | 8886 | |
b3f8228a | 8887 | begin |
a9f5fea7 | 8888 | if Cursor = Any_Type then |
8889 | return; | |
8890 | end if; | |
8891 | ||
b3f8228a | 8892 | Assoc := First (Component_Associations (Expression (ASN))); |
8893 | while Present (Assoc) loop | |
3061ffde | 8894 | Expr := Expression (Assoc); |
8895 | Analyze (Expr); | |
a9f5fea7 | 8896 | |
8897 | if not Error_Posted (Expr) then | |
8898 | Resolve_Iterable_Operation | |
8899 | (Expr, Cursor, T, Chars (First (Choices (Assoc)))); | |
8900 | end if; | |
8901 | ||
b3f8228a | 8902 | Next (Assoc); |
8903 | end loop; | |
8904 | end; | |
3061ffde | 8905 | |
b3f8228a | 8906 | return; |
8907 | ||
d9f6a4ee | 8908 | -- Invariant/Predicate take boolean expressions |
7d20685d | 8909 | |
d9f6a4ee | 8910 | when Aspect_Dynamic_Predicate | |
8911 | Aspect_Invariant | | |
8912 | Aspect_Predicate | | |
8913 | Aspect_Static_Predicate | | |
8914 | Aspect_Type_Invariant => | |
8915 | T := Standard_Boolean; | |
7d20685d | 8916 | |
d9f6a4ee | 8917 | -- Here is the list of aspects that don't require delay analysis |
89f1e35c | 8918 | |
ec6f6da5 | 8919 | when Aspect_Abstract_State | |
8920 | Aspect_Annotate | | |
8921 | Aspect_Contract_Cases | | |
8922 | Aspect_Default_Initial_Condition | | |
3dbe7a69 | 8923 | Aspect_Depends | |
ec6f6da5 | 8924 | Aspect_Dimension | |
8925 | Aspect_Dimension_System | | |
cab27d2a | 8926 | Aspect_Extensions_Visible | |
3dbe7a69 | 8927 | Aspect_Ghost | |
8928 | Aspect_Global | | |
ec6f6da5 | 8929 | Aspect_Implicit_Dereference | |
8930 | Aspect_Initial_Condition | | |
8931 | Aspect_Initializes | | |
1fd4313f | 8932 | Aspect_Obsolescent | |
ec6f6da5 | 8933 | Aspect_Part_Of | |
8934 | Aspect_Post | | |
8935 | Aspect_Postcondition | | |
8936 | Aspect_Pre | | |
8937 | Aspect_Precondition | | |
8938 | Aspect_Refined_Depends | | |
8939 | Aspect_Refined_Global | | |
8940 | Aspect_Refined_Post | | |
8941 | Aspect_Refined_State | | |
8942 | Aspect_SPARK_Mode | | |
8943 | Aspect_Test_Case => | |
d9f6a4ee | 8944 | raise Program_Error; |
2b184b2f | 8945 | |
d9f6a4ee | 8946 | end case; |
2b184b2f | 8947 | |
d9f6a4ee | 8948 | -- Do the preanalyze call |
2b184b2f | 8949 | |
d9f6a4ee | 8950 | Preanalyze_Spec_Expression (Expression (ASN), T); |
8951 | end Check_Aspect_At_Freeze_Point; | |
2b184b2f | 8952 | |
d9f6a4ee | 8953 | ----------------------------------- |
8954 | -- Check_Constant_Address_Clause -- | |
8955 | ----------------------------------- | |
2b184b2f | 8956 | |
d9f6a4ee | 8957 | procedure Check_Constant_Address_Clause |
8958 | (Expr : Node_Id; | |
8959 | U_Ent : Entity_Id) | |
8960 | is | |
8961 | procedure Check_At_Constant_Address (Nod : Node_Id); | |
8962 | -- Checks that the given node N represents a name whose 'Address is | |
8963 | -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the | |
8964 | -- address value is the same at the point of declaration of U_Ent and at | |
8965 | -- the time of elaboration of the address clause. | |
84ed7523 | 8966 | |
d9f6a4ee | 8967 | procedure Check_Expr_Constants (Nod : Node_Id); |
8968 | -- Checks that Nod meets the requirements for a constant address clause | |
8969 | -- in the sense of the enclosing procedure. | |
84ed7523 | 8970 | |
d9f6a4ee | 8971 | procedure Check_List_Constants (Lst : List_Id); |
8972 | -- Check that all elements of list Lst meet the requirements for a | |
8973 | -- constant address clause in the sense of the enclosing procedure. | |
84ed7523 | 8974 | |
d9f6a4ee | 8975 | ------------------------------- |
8976 | -- Check_At_Constant_Address -- | |
8977 | ------------------------------- | |
84ed7523 | 8978 | |
d9f6a4ee | 8979 | procedure Check_At_Constant_Address (Nod : Node_Id) is |
8980 | begin | |
8981 | if Is_Entity_Name (Nod) then | |
8982 | if Present (Address_Clause (Entity ((Nod)))) then | |
8983 | Error_Msg_NE | |
8984 | ("invalid address clause for initialized object &!", | |
8985 | Nod, U_Ent); | |
8986 | Error_Msg_NE | |
8987 | ("address for& cannot" & | |
8988 | " depend on another address clause! (RM 13.1(22))!", | |
8989 | Nod, U_Ent); | |
84ed7523 | 8990 | |
d9f6a4ee | 8991 | elsif In_Same_Source_Unit (Entity (Nod), U_Ent) |
8992 | and then Sloc (U_Ent) < Sloc (Entity (Nod)) | |
8993 | then | |
8994 | Error_Msg_NE | |
8995 | ("invalid address clause for initialized object &!", | |
8996 | Nod, U_Ent); | |
8997 | Error_Msg_Node_2 := U_Ent; | |
8998 | Error_Msg_NE | |
8999 | ("\& must be defined before & (RM 13.1(22))!", | |
9000 | Nod, Entity (Nod)); | |
9001 | end if; | |
7d20685d | 9002 | |
d9f6a4ee | 9003 | elsif Nkind (Nod) = N_Selected_Component then |
9004 | declare | |
9005 | T : constant Entity_Id := Etype (Prefix (Nod)); | |
59f3e675 | 9006 | |
d9f6a4ee | 9007 | begin |
9008 | if (Is_Record_Type (T) | |
9009 | and then Has_Discriminants (T)) | |
9010 | or else | |
9011 | (Is_Access_Type (T) | |
f02a9a9a | 9012 | and then Is_Record_Type (Designated_Type (T)) |
9013 | and then Has_Discriminants (Designated_Type (T))) | |
d9f6a4ee | 9014 | then |
9015 | Error_Msg_NE | |
9016 | ("invalid address clause for initialized object &!", | |
9017 | Nod, U_Ent); | |
9018 | Error_Msg_N | |
9019 | ("\address cannot depend on component" & | |
9020 | " of discriminated record (RM 13.1(22))!", | |
9021 | Nod); | |
9022 | else | |
9023 | Check_At_Constant_Address (Prefix (Nod)); | |
9024 | end if; | |
9025 | end; | |
89cc7147 | 9026 | |
d9f6a4ee | 9027 | elsif Nkind (Nod) = N_Indexed_Component then |
9028 | Check_At_Constant_Address (Prefix (Nod)); | |
9029 | Check_List_Constants (Expressions (Nod)); | |
89cc7147 | 9030 | |
84ed7523 | 9031 | else |
d9f6a4ee | 9032 | Check_Expr_Constants (Nod); |
84ed7523 | 9033 | end if; |
d9f6a4ee | 9034 | end Check_At_Constant_Address; |
81b424ac | 9035 | |
d9f6a4ee | 9036 | -------------------------- |
9037 | -- Check_Expr_Constants -- | |
9038 | -------------------------- | |
7b9b2f05 | 9039 | |
d9f6a4ee | 9040 | procedure Check_Expr_Constants (Nod : Node_Id) is |
9041 | Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent); | |
9042 | Ent : Entity_Id := Empty; | |
7b9b2f05 | 9043 | |
d9f6a4ee | 9044 | begin |
9045 | if Nkind (Nod) in N_Has_Etype | |
9046 | and then Etype (Nod) = Any_Type | |
7b9b2f05 | 9047 | then |
d9f6a4ee | 9048 | return; |
309c3053 | 9049 | end if; |
9050 | ||
d9f6a4ee | 9051 | case Nkind (Nod) is |
9052 | when N_Empty | N_Error => | |
9053 | return; | |
7d20685d | 9054 | |
d9f6a4ee | 9055 | when N_Identifier | N_Expanded_Name => |
9056 | Ent := Entity (Nod); | |
7d20685d | 9057 | |
d9f6a4ee | 9058 | -- We need to look at the original node if it is different |
9059 | -- from the node, since we may have rewritten things and | |
9060 | -- substituted an identifier representing the rewrite. | |
7d20685d | 9061 | |
d9f6a4ee | 9062 | if Original_Node (Nod) /= Nod then |
9063 | Check_Expr_Constants (Original_Node (Nod)); | |
7d20685d | 9064 | |
d9f6a4ee | 9065 | -- If the node is an object declaration without initial |
9066 | -- value, some code has been expanded, and the expression | |
9067 | -- is not constant, even if the constituents might be | |
9068 | -- acceptable, as in A'Address + offset. | |
7d20685d | 9069 | |
d9f6a4ee | 9070 | if Ekind (Ent) = E_Variable |
9071 | and then | |
9072 | Nkind (Declaration_Node (Ent)) = N_Object_Declaration | |
9073 | and then | |
9074 | No (Expression (Declaration_Node (Ent))) | |
9075 | then | |
9076 | Error_Msg_NE | |
9077 | ("invalid address clause for initialized object &!", | |
9078 | Nod, U_Ent); | |
89f1e35c | 9079 | |
d9f6a4ee | 9080 | -- If entity is constant, it may be the result of expanding |
9081 | -- a check. We must verify that its declaration appears | |
9082 | -- before the object in question, else we also reject the | |
9083 | -- address clause. | |
7d20685d | 9084 | |
d9f6a4ee | 9085 | elsif Ekind (Ent) = E_Constant |
9086 | and then In_Same_Source_Unit (Ent, U_Ent) | |
9087 | and then Sloc (Ent) > Loc_U_Ent | |
9088 | then | |
9089 | Error_Msg_NE | |
9090 | ("invalid address clause for initialized object &!", | |
9091 | Nod, U_Ent); | |
9092 | end if; | |
7d20685d | 9093 | |
d9f6a4ee | 9094 | return; |
9095 | end if; | |
7d20685d | 9096 | |
d9f6a4ee | 9097 | -- Otherwise look at the identifier and see if it is OK |
7d20685d | 9098 | |
d9f6a4ee | 9099 | if Ekind_In (Ent, E_Named_Integer, E_Named_Real) |
9100 | or else Is_Type (Ent) | |
9101 | then | |
9102 | return; | |
7d20685d | 9103 | |
f02a9a9a | 9104 | elsif Ekind_In (Ent, E_Constant, E_In_Parameter) then |
9105 | ||
d9f6a4ee | 9106 | -- This is the case where we must have Ent defined before |
9107 | -- U_Ent. Clearly if they are in different units this | |
9108 | -- requirement is met since the unit containing Ent is | |
9109 | -- already processed. | |
7d20685d | 9110 | |
d9f6a4ee | 9111 | if not In_Same_Source_Unit (Ent, U_Ent) then |
9112 | return; | |
7d20685d | 9113 | |
d9f6a4ee | 9114 | -- Otherwise location of Ent must be before the location |
9115 | -- of U_Ent, that's what prior defined means. | |
7d20685d | 9116 | |
d9f6a4ee | 9117 | elsif Sloc (Ent) < Loc_U_Ent then |
9118 | return; | |
6c545057 | 9119 | |
d9f6a4ee | 9120 | else |
9121 | Error_Msg_NE | |
9122 | ("invalid address clause for initialized object &!", | |
9123 | Nod, U_Ent); | |
9124 | Error_Msg_Node_2 := U_Ent; | |
9125 | Error_Msg_NE | |
9126 | ("\& must be defined before & (RM 13.1(22))!", | |
9127 | Nod, Ent); | |
9128 | end if; | |
37c6e44c | 9129 | |
d9f6a4ee | 9130 | elsif Nkind (Original_Node (Nod)) = N_Function_Call then |
9131 | Check_Expr_Constants (Original_Node (Nod)); | |
6c545057 | 9132 | |
d9f6a4ee | 9133 | else |
9134 | Error_Msg_NE | |
9135 | ("invalid address clause for initialized object &!", | |
9136 | Nod, U_Ent); | |
3cdbaa5a | 9137 | |
d9f6a4ee | 9138 | if Comes_From_Source (Ent) then |
9139 | Error_Msg_NE | |
9140 | ("\reference to variable& not allowed" | |
9141 | & " (RM 13.1(22))!", Nod, Ent); | |
9142 | else | |
9143 | Error_Msg_N | |
9144 | ("non-static expression not allowed" | |
9145 | & " (RM 13.1(22))!", Nod); | |
9146 | end if; | |
9147 | end if; | |
3cdbaa5a | 9148 | |
d9f6a4ee | 9149 | when N_Integer_Literal => |
7f694ca2 | 9150 | |
d9f6a4ee | 9151 | -- If this is a rewritten unchecked conversion, in a system |
9152 | -- where Address is an integer type, always use the base type | |
9153 | -- for a literal value. This is user-friendly and prevents | |
9154 | -- order-of-elaboration issues with instances of unchecked | |
9155 | -- conversion. | |
3cdbaa5a | 9156 | |
d9f6a4ee | 9157 | if Nkind (Original_Node (Nod)) = N_Function_Call then |
9158 | Set_Etype (Nod, Base_Type (Etype (Nod))); | |
9159 | end if; | |
e1cedbae | 9160 | |
d9f6a4ee | 9161 | when N_Real_Literal | |
9162 | N_String_Literal | | |
9163 | N_Character_Literal => | |
9164 | return; | |
7d20685d | 9165 | |
d9f6a4ee | 9166 | when N_Range => |
9167 | Check_Expr_Constants (Low_Bound (Nod)); | |
9168 | Check_Expr_Constants (High_Bound (Nod)); | |
231eb581 | 9169 | |
d9f6a4ee | 9170 | when N_Explicit_Dereference => |
9171 | Check_Expr_Constants (Prefix (Nod)); | |
231eb581 | 9172 | |
d9f6a4ee | 9173 | when N_Indexed_Component => |
9174 | Check_Expr_Constants (Prefix (Nod)); | |
9175 | Check_List_Constants (Expressions (Nod)); | |
7d20685d | 9176 | |
d9f6a4ee | 9177 | when N_Slice => |
9178 | Check_Expr_Constants (Prefix (Nod)); | |
9179 | Check_Expr_Constants (Discrete_Range (Nod)); | |
cb4c311d | 9180 | |
d9f6a4ee | 9181 | when N_Selected_Component => |
9182 | Check_Expr_Constants (Prefix (Nod)); | |
6144c105 | 9183 | |
d9f6a4ee | 9184 | when N_Attribute_Reference => |
9185 | if Nam_In (Attribute_Name (Nod), Name_Address, | |
9186 | Name_Access, | |
9187 | Name_Unchecked_Access, | |
9188 | Name_Unrestricted_Access) | |
9189 | then | |
9190 | Check_At_Constant_Address (Prefix (Nod)); | |
6144c105 | 9191 | |
d9f6a4ee | 9192 | else |
9193 | Check_Expr_Constants (Prefix (Nod)); | |
9194 | Check_List_Constants (Expressions (Nod)); | |
9195 | end if; | |
a7a4a7c2 | 9196 | |
d9f6a4ee | 9197 | when N_Aggregate => |
9198 | Check_List_Constants (Component_Associations (Nod)); | |
9199 | Check_List_Constants (Expressions (Nod)); | |
7d20685d | 9200 | |
d9f6a4ee | 9201 | when N_Component_Association => |
9202 | Check_Expr_Constants (Expression (Nod)); | |
e1cedbae | 9203 | |
d9f6a4ee | 9204 | when N_Extension_Aggregate => |
9205 | Check_Expr_Constants (Ancestor_Part (Nod)); | |
9206 | Check_List_Constants (Component_Associations (Nod)); | |
9207 | Check_List_Constants (Expressions (Nod)); | |
3cdbaa5a | 9208 | |
d9f6a4ee | 9209 | when N_Null => |
9210 | return; | |
3cdbaa5a | 9211 | |
d9f6a4ee | 9212 | when N_Binary_Op | N_Short_Circuit | N_Membership_Test => |
9213 | Check_Expr_Constants (Left_Opnd (Nod)); | |
9214 | Check_Expr_Constants (Right_Opnd (Nod)); | |
e1cedbae | 9215 | |
d9f6a4ee | 9216 | when N_Unary_Op => |
9217 | Check_Expr_Constants (Right_Opnd (Nod)); | |
7f694ca2 | 9218 | |
d9f6a4ee | 9219 | when N_Type_Conversion | |
9220 | N_Qualified_Expression | | |
9221 | N_Allocator | | |
9222 | N_Unchecked_Type_Conversion => | |
9223 | Check_Expr_Constants (Expression (Nod)); | |
47a46747 | 9224 | |
d9f6a4ee | 9225 | when N_Function_Call => |
9226 | if not Is_Pure (Entity (Name (Nod))) then | |
9227 | Error_Msg_NE | |
9228 | ("invalid address clause for initialized object &!", | |
9229 | Nod, U_Ent); | |
7f694ca2 | 9230 | |
d9f6a4ee | 9231 | Error_Msg_NE |
9232 | ("\function & is not pure (RM 13.1(22))!", | |
9233 | Nod, Entity (Name (Nod))); | |
b55f7641 | 9234 | |
d9f6a4ee | 9235 | else |
9236 | Check_List_Constants (Parameter_Associations (Nod)); | |
9237 | end if; | |
b55f7641 | 9238 | |
d9f6a4ee | 9239 | when N_Parameter_Association => |
9240 | Check_Expr_Constants (Explicit_Actual_Parameter (Nod)); | |
7d20685d | 9241 | |
d9f6a4ee | 9242 | when others => |
9243 | Error_Msg_NE | |
9244 | ("invalid address clause for initialized object &!", | |
9245 | Nod, U_Ent); | |
9246 | Error_Msg_NE | |
9247 | ("\must be constant defined before& (RM 13.1(22))!", | |
9248 | Nod, U_Ent); | |
9249 | end case; | |
9250 | end Check_Expr_Constants; | |
7d20685d | 9251 | |
d9f6a4ee | 9252 | -------------------------- |
9253 | -- Check_List_Constants -- | |
9254 | -------------------------- | |
89f1e35c | 9255 | |
d9f6a4ee | 9256 | procedure Check_List_Constants (Lst : List_Id) is |
9257 | Nod1 : Node_Id; | |
7d20685d | 9258 | |
d9f6a4ee | 9259 | begin |
9260 | if Present (Lst) then | |
9261 | Nod1 := First (Lst); | |
9262 | while Present (Nod1) loop | |
9263 | Check_Expr_Constants (Nod1); | |
9264 | Next (Nod1); | |
9265 | end loop; | |
9266 | end if; | |
9267 | end Check_List_Constants; | |
81b424ac | 9268 | |
d9f6a4ee | 9269 | -- Start of processing for Check_Constant_Address_Clause |
81b424ac | 9270 | |
d9f6a4ee | 9271 | begin |
9272 | -- If rep_clauses are to be ignored, no need for legality checks. In | |
9c7948d7 | 9273 | -- particular, no need to pester user about rep clauses that violate the |
9274 | -- rule on constant addresses, given that these clauses will be removed | |
9275 | -- by Freeze before they reach the back end. Similarly in CodePeer mode, | |
9276 | -- we want to relax these checks. | |
7d20685d | 9277 | |
f1a9be43 | 9278 | if not Ignore_Rep_Clauses and not CodePeer_Mode then |
d9f6a4ee | 9279 | Check_Expr_Constants (Expr); |
9280 | end if; | |
9281 | end Check_Constant_Address_Clause; | |
7d20685d | 9282 | |
6653b695 | 9283 | --------------------------- |
9284 | -- Check_Pool_Size_Clash -- | |
9285 | --------------------------- | |
9286 | ||
9287 | procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is | |
9288 | Post : Node_Id; | |
9289 | ||
9290 | begin | |
9291 | -- We need to find out which one came first. Note that in the case of | |
9292 | -- aspects mixed with pragmas there are cases where the processing order | |
9293 | -- is reversed, which is why we do the check here. | |
9294 | ||
9295 | if Sloc (SP) < Sloc (SS) then | |
9296 | Error_Msg_Sloc := Sloc (SP); | |
9297 | Post := SS; | |
9298 | Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent); | |
9299 | ||
9300 | else | |
9301 | Error_Msg_Sloc := Sloc (SS); | |
9302 | Post := SP; | |
9303 | Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent); | |
9304 | end if; | |
9305 | ||
9306 | Error_Msg_N | |
9307 | ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post); | |
9308 | end Check_Pool_Size_Clash; | |
9309 | ||
d9f6a4ee | 9310 | ---------------------------------------- |
9311 | -- Check_Record_Representation_Clause -- | |
9312 | ---------------------------------------- | |
85696508 | 9313 | |
d9f6a4ee | 9314 | procedure Check_Record_Representation_Clause (N : Node_Id) is |
9315 | Loc : constant Source_Ptr := Sloc (N); | |
9316 | Ident : constant Node_Id := Identifier (N); | |
9317 | Rectype : Entity_Id; | |
9318 | Fent : Entity_Id; | |
9319 | CC : Node_Id; | |
9320 | Fbit : Uint; | |
9321 | Lbit : Uint; | |
9322 | Hbit : Uint := Uint_0; | |
9323 | Comp : Entity_Id; | |
9324 | Pcomp : Entity_Id; | |
89f1e35c | 9325 | |
d9f6a4ee | 9326 | Max_Bit_So_Far : Uint; |
9327 | -- Records the maximum bit position so far. If all field positions | |
9328 | -- are monotonically increasing, then we can skip the circuit for | |
9329 | -- checking for overlap, since no overlap is possible. | |
85696508 | 9330 | |
d9f6a4ee | 9331 | Tagged_Parent : Entity_Id := Empty; |
9332 | -- This is set in the case of a derived tagged type for which we have | |
9333 | -- Is_Fully_Repped_Tagged_Type True (indicating that all components are | |
9334 | -- positioned by record representation clauses). In this case we must | |
9335 | -- check for overlap between components of this tagged type, and the | |
9336 | -- components of its parent. Tagged_Parent will point to this parent | |
9337 | -- type. For all other cases Tagged_Parent is left set to Empty. | |
7d20685d | 9338 | |
d9f6a4ee | 9339 | Parent_Last_Bit : Uint; |
9340 | -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the | |
9341 | -- last bit position for any field in the parent type. We only need to | |
9342 | -- check overlap for fields starting below this point. | |
7d20685d | 9343 | |
d9f6a4ee | 9344 | Overlap_Check_Required : Boolean; |
9345 | -- Used to keep track of whether or not an overlap check is required | |
7d20685d | 9346 | |
d9f6a4ee | 9347 | Overlap_Detected : Boolean := False; |
9348 | -- Set True if an overlap is detected | |
d6f39728 | 9349 | |
d9f6a4ee | 9350 | Ccount : Natural := 0; |
9351 | -- Number of component clauses in record rep clause | |
d6f39728 | 9352 | |
d9f6a4ee | 9353 | procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id); |
9354 | -- Given two entities for record components or discriminants, checks | |
9355 | -- if they have overlapping component clauses and issues errors if so. | |
d6f39728 | 9356 | |
d9f6a4ee | 9357 | procedure Find_Component; |
9358 | -- Finds component entity corresponding to current component clause (in | |
9359 | -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin | |
9360 | -- start/stop bits for the field. If there is no matching component or | |
9361 | -- if the matching component does not have a component clause, then | |
9362 | -- that's an error and Comp is set to Empty, but no error message is | |
9363 | -- issued, since the message was already given. Comp is also set to | |
9364 | -- Empty if the current "component clause" is in fact a pragma. | |
d6f39728 | 9365 | |
d9f6a4ee | 9366 | ----------------------------- |
9367 | -- Check_Component_Overlap -- | |
9368 | ----------------------------- | |
9369 | ||
9370 | procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is | |
9371 | CC1 : constant Node_Id := Component_Clause (C1_Ent); | |
9372 | CC2 : constant Node_Id := Component_Clause (C2_Ent); | |
d6f39728 | 9373 | |
d6f39728 | 9374 | begin |
d9f6a4ee | 9375 | if Present (CC1) and then Present (CC2) then |
d6f39728 | 9376 | |
d9f6a4ee | 9377 | -- Exclude odd case where we have two tag components in the same |
9378 | -- record, both at location zero. This seems a bit strange, but | |
9379 | -- it seems to happen in some circumstances, perhaps on an error. | |
9380 | ||
9381 | if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then | |
9382 | return; | |
d6f39728 | 9383 | end if; |
9384 | ||
d9f6a4ee | 9385 | -- Here we check if the two fields overlap |
9386 | ||
d6f39728 | 9387 | declare |
d9f6a4ee | 9388 | S1 : constant Uint := Component_Bit_Offset (C1_Ent); |
9389 | S2 : constant Uint := Component_Bit_Offset (C2_Ent); | |
9390 | E1 : constant Uint := S1 + Esize (C1_Ent); | |
9391 | E2 : constant Uint := S2 + Esize (C2_Ent); | |
d6f39728 | 9392 | |
9393 | begin | |
d9f6a4ee | 9394 | if E2 <= S1 or else E1 <= S2 then |
9395 | null; | |
d6f39728 | 9396 | else |
d9f6a4ee | 9397 | Error_Msg_Node_2 := Component_Name (CC2); |
9398 | Error_Msg_Sloc := Sloc (Error_Msg_Node_2); | |
9399 | Error_Msg_Node_1 := Component_Name (CC1); | |
9400 | Error_Msg_N | |
9401 | ("component& overlaps & #", Component_Name (CC1)); | |
9402 | Overlap_Detected := True; | |
d6f39728 | 9403 | end if; |
9404 | end; | |
d6f39728 | 9405 | end if; |
d9f6a4ee | 9406 | end Check_Component_Overlap; |
d6f39728 | 9407 | |
d9f6a4ee | 9408 | -------------------- |
9409 | -- Find_Component -- | |
9410 | -------------------- | |
9dfe12ae | 9411 | |
d9f6a4ee | 9412 | procedure Find_Component is |
9dfe12ae | 9413 | |
d9f6a4ee | 9414 | procedure Search_Component (R : Entity_Id); |
9415 | -- Search components of R for a match. If found, Comp is set | |
9dfe12ae | 9416 | |
d9f6a4ee | 9417 | ---------------------- |
9418 | -- Search_Component -- | |
9419 | ---------------------- | |
e7b2d6bc | 9420 | |
d9f6a4ee | 9421 | procedure Search_Component (R : Entity_Id) is |
9422 | begin | |
9423 | Comp := First_Component_Or_Discriminant (R); | |
9424 | while Present (Comp) loop | |
e7b2d6bc | 9425 | |
d9f6a4ee | 9426 | -- Ignore error of attribute name for component name (we |
9427 | -- already gave an error message for this, so no need to | |
9428 | -- complain here) | |
e7b2d6bc | 9429 | |
d9f6a4ee | 9430 | if Nkind (Component_Name (CC)) = N_Attribute_Reference then |
9431 | null; | |
9432 | else | |
9433 | exit when Chars (Comp) = Chars (Component_Name (CC)); | |
9dfe12ae | 9434 | end if; |
9435 | ||
d9f6a4ee | 9436 | Next_Component_Or_Discriminant (Comp); |
9437 | end loop; | |
9438 | end Search_Component; | |
d6f39728 | 9439 | |
d9f6a4ee | 9440 | -- Start of processing for Find_Component |
d6f39728 | 9441 | |
d9f6a4ee | 9442 | begin |
9443 | -- Return with Comp set to Empty if we have a pragma | |
d6f39728 | 9444 | |
d9f6a4ee | 9445 | if Nkind (CC) = N_Pragma then |
9446 | Comp := Empty; | |
9447 | return; | |
9448 | end if; | |
d6f39728 | 9449 | |
d9f6a4ee | 9450 | -- Search current record for matching component |
d6f39728 | 9451 | |
d9f6a4ee | 9452 | Search_Component (Rectype); |
9dfe12ae | 9453 | |
d9f6a4ee | 9454 | -- If not found, maybe component of base type discriminant that is |
9455 | -- absent from statically constrained first subtype. | |
e7b2d6bc | 9456 | |
d9f6a4ee | 9457 | if No (Comp) then |
9458 | Search_Component (Base_Type (Rectype)); | |
9459 | end if; | |
e7b2d6bc | 9460 | |
d9f6a4ee | 9461 | -- If no component, or the component does not reference the component |
9462 | -- clause in question, then there was some previous error for which | |
9463 | -- we already gave a message, so just return with Comp Empty. | |
d6f39728 | 9464 | |
d9f6a4ee | 9465 | if No (Comp) or else Component_Clause (Comp) /= CC then |
9466 | Check_Error_Detected; | |
9467 | Comp := Empty; | |
93735cb8 | 9468 | |
d9f6a4ee | 9469 | -- Normal case where we have a component clause |
93735cb8 | 9470 | |
d9f6a4ee | 9471 | else |
9472 | Fbit := Component_Bit_Offset (Comp); | |
9473 | Lbit := Fbit + Esize (Comp) - 1; | |
9474 | end if; | |
9475 | end Find_Component; | |
93735cb8 | 9476 | |
d9f6a4ee | 9477 | -- Start of processing for Check_Record_Representation_Clause |
d6f39728 | 9478 | |
d9f6a4ee | 9479 | begin |
9480 | Find_Type (Ident); | |
9481 | Rectype := Entity (Ident); | |
d6f39728 | 9482 | |
d9f6a4ee | 9483 | if Rectype = Any_Type then |
9484 | return; | |
9485 | else | |
9486 | Rectype := Underlying_Type (Rectype); | |
9487 | end if; | |
d6f39728 | 9488 | |
d9f6a4ee | 9489 | -- See if we have a fully repped derived tagged type |
d6f39728 | 9490 | |
d9f6a4ee | 9491 | declare |
9492 | PS : constant Entity_Id := Parent_Subtype (Rectype); | |
d6f39728 | 9493 | |
d9f6a4ee | 9494 | begin |
9495 | if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then | |
9496 | Tagged_Parent := PS; | |
d6f39728 | 9497 | |
d9f6a4ee | 9498 | -- Find maximum bit of any component of the parent type |
d6f39728 | 9499 | |
d9f6a4ee | 9500 | Parent_Last_Bit := UI_From_Int (System_Address_Size - 1); |
9501 | Pcomp := First_Entity (Tagged_Parent); | |
9502 | while Present (Pcomp) loop | |
9503 | if Ekind_In (Pcomp, E_Discriminant, E_Component) then | |
9504 | if Component_Bit_Offset (Pcomp) /= No_Uint | |
9505 | and then Known_Static_Esize (Pcomp) | |
9506 | then | |
9507 | Parent_Last_Bit := | |
9508 | UI_Max | |
9509 | (Parent_Last_Bit, | |
9510 | Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1); | |
9511 | end if; | |
9512 | ||
9513 | Next_Entity (Pcomp); | |
d6f39728 | 9514 | end if; |
d9f6a4ee | 9515 | end loop; |
9516 | end if; | |
9517 | end; | |
d6f39728 | 9518 | |
d9f6a4ee | 9519 | -- All done if no component clauses |
d6f39728 | 9520 | |
d9f6a4ee | 9521 | CC := First (Component_Clauses (N)); |
d6f39728 | 9522 | |
d9f6a4ee | 9523 | if No (CC) then |
9524 | return; | |
9525 | end if; | |
d6f39728 | 9526 | |
d9f6a4ee | 9527 | -- If a tag is present, then create a component clause that places it |
9528 | -- at the start of the record (otherwise gigi may place it after other | |
9529 | -- fields that have rep clauses). | |
d6f39728 | 9530 | |
d9f6a4ee | 9531 | Fent := First_Entity (Rectype); |
d6f39728 | 9532 | |
d9f6a4ee | 9533 | if Nkind (Fent) = N_Defining_Identifier |
9534 | and then Chars (Fent) = Name_uTag | |
9535 | then | |
9536 | Set_Component_Bit_Offset (Fent, Uint_0); | |
9537 | Set_Normalized_Position (Fent, Uint_0); | |
9538 | Set_Normalized_First_Bit (Fent, Uint_0); | |
9539 | Set_Normalized_Position_Max (Fent, Uint_0); | |
9540 | Init_Esize (Fent, System_Address_Size); | |
d6f39728 | 9541 | |
d9f6a4ee | 9542 | Set_Component_Clause (Fent, |
9543 | Make_Component_Clause (Loc, | |
9544 | Component_Name => Make_Identifier (Loc, Name_uTag), | |
d6f39728 | 9545 | |
d9f6a4ee | 9546 | Position => Make_Integer_Literal (Loc, Uint_0), |
9547 | First_Bit => Make_Integer_Literal (Loc, Uint_0), | |
9548 | Last_Bit => | |
9549 | Make_Integer_Literal (Loc, | |
9550 | UI_From_Int (System_Address_Size)))); | |
d6f39728 | 9551 | |
d9f6a4ee | 9552 | Ccount := Ccount + 1; |
9553 | end if; | |
d6f39728 | 9554 | |
d9f6a4ee | 9555 | Max_Bit_So_Far := Uint_Minus_1; |
9556 | Overlap_Check_Required := False; | |
d6f39728 | 9557 | |
d9f6a4ee | 9558 | -- Process the component clauses |
d6f39728 | 9559 | |
d9f6a4ee | 9560 | while Present (CC) loop |
9561 | Find_Component; | |
d6f39728 | 9562 | |
d9f6a4ee | 9563 | if Present (Comp) then |
9564 | Ccount := Ccount + 1; | |
d6f39728 | 9565 | |
d9f6a4ee | 9566 | -- We need a full overlap check if record positions non-monotonic |
d6f39728 | 9567 | |
d9f6a4ee | 9568 | if Fbit <= Max_Bit_So_Far then |
9569 | Overlap_Check_Required := True; | |
9570 | end if; | |
d6f39728 | 9571 | |
d9f6a4ee | 9572 | Max_Bit_So_Far := Lbit; |
d6f39728 | 9573 | |
d9f6a4ee | 9574 | -- Check bit position out of range of specified size |
01cb2726 | 9575 | |
d9f6a4ee | 9576 | if Has_Size_Clause (Rectype) |
9577 | and then RM_Size (Rectype) <= Lbit | |
9578 | then | |
9579 | Error_Msg_N | |
9580 | ("bit number out of range of specified size", | |
9581 | Last_Bit (CC)); | |
d6f39728 | 9582 | |
d9f6a4ee | 9583 | -- Check for overlap with tag component |
67278d60 | 9584 | |
d9f6a4ee | 9585 | else |
9586 | if Is_Tagged_Type (Rectype) | |
9587 | and then Fbit < System_Address_Size | |
9588 | then | |
9589 | Error_Msg_NE | |
9590 | ("component overlaps tag field of&", | |
9591 | Component_Name (CC), Rectype); | |
9592 | Overlap_Detected := True; | |
9593 | end if; | |
67278d60 | 9594 | |
d9f6a4ee | 9595 | if Hbit < Lbit then |
9596 | Hbit := Lbit; | |
9597 | end if; | |
9598 | end if; | |
67278d60 | 9599 | |
d9f6a4ee | 9600 | -- Check parent overlap if component might overlap parent field |
67278d60 | 9601 | |
d9f6a4ee | 9602 | if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then |
9603 | Pcomp := First_Component_Or_Discriminant (Tagged_Parent); | |
9604 | while Present (Pcomp) loop | |
9605 | if not Is_Tag (Pcomp) | |
9606 | and then Chars (Pcomp) /= Name_uParent | |
9607 | then | |
9608 | Check_Component_Overlap (Comp, Pcomp); | |
9609 | end if; | |
67278d60 | 9610 | |
d9f6a4ee | 9611 | Next_Component_Or_Discriminant (Pcomp); |
9612 | end loop; | |
9613 | end if; | |
9614 | end if; | |
67278d60 | 9615 | |
d9f6a4ee | 9616 | Next (CC); |
9617 | end loop; | |
47495553 | 9618 | |
d9f6a4ee | 9619 | -- Now that we have processed all the component clauses, check for |
9620 | -- overlap. We have to leave this till last, since the components can | |
9621 | -- appear in any arbitrary order in the representation clause. | |
67278d60 | 9622 | |
d9f6a4ee | 9623 | -- We do not need this check if all specified ranges were monotonic, |
9624 | -- as recorded by Overlap_Check_Required being False at this stage. | |
67278d60 | 9625 | |
d9f6a4ee | 9626 | -- This first section checks if there are any overlapping entries at |
9627 | -- all. It does this by sorting all entries and then seeing if there are | |
9628 | -- any overlaps. If there are none, then that is decisive, but if there | |
9629 | -- are overlaps, they may still be OK (they may result from fields in | |
9630 | -- different variants). | |
67278d60 | 9631 | |
d9f6a4ee | 9632 | if Overlap_Check_Required then |
9633 | Overlap_Check1 : declare | |
67278d60 | 9634 | |
d9f6a4ee | 9635 | OC_Fbit : array (0 .. Ccount) of Uint; |
9636 | -- First-bit values for component clauses, the value is the offset | |
9637 | -- of the first bit of the field from start of record. The zero | |
9638 | -- entry is for use in sorting. | |
47495553 | 9639 | |
d9f6a4ee | 9640 | OC_Lbit : array (0 .. Ccount) of Uint; |
9641 | -- Last-bit values for component clauses, the value is the offset | |
9642 | -- of the last bit of the field from start of record. The zero | |
9643 | -- entry is for use in sorting. | |
9644 | ||
9645 | OC_Count : Natural := 0; | |
9646 | -- Count of entries in OC_Fbit and OC_Lbit | |
67278d60 | 9647 | |
d9f6a4ee | 9648 | function OC_Lt (Op1, Op2 : Natural) return Boolean; |
9649 | -- Compare routine for Sort | |
67278d60 | 9650 | |
d9f6a4ee | 9651 | procedure OC_Move (From : Natural; To : Natural); |
9652 | -- Move routine for Sort | |
67278d60 | 9653 | |
d9f6a4ee | 9654 | package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt); |
67278d60 | 9655 | |
d9f6a4ee | 9656 | ----------- |
9657 | -- OC_Lt -- | |
9658 | ----------- | |
67278d60 | 9659 | |
d9f6a4ee | 9660 | function OC_Lt (Op1, Op2 : Natural) return Boolean is |
67278d60 | 9661 | begin |
d9f6a4ee | 9662 | return OC_Fbit (Op1) < OC_Fbit (Op2); |
9663 | end OC_Lt; | |
67278d60 | 9664 | |
d9f6a4ee | 9665 | ------------- |
9666 | -- OC_Move -- | |
9667 | ------------- | |
67278d60 | 9668 | |
d9f6a4ee | 9669 | procedure OC_Move (From : Natural; To : Natural) is |
9670 | begin | |
9671 | OC_Fbit (To) := OC_Fbit (From); | |
9672 | OC_Lbit (To) := OC_Lbit (From); | |
9673 | end OC_Move; | |
67278d60 | 9674 | |
d9f6a4ee | 9675 | -- Start of processing for Overlap_Check |
67278d60 | 9676 | |
67278d60 | 9677 | begin |
d9f6a4ee | 9678 | CC := First (Component_Clauses (N)); |
9679 | while Present (CC) loop | |
67278d60 | 9680 | |
d9f6a4ee | 9681 | -- Exclude component clause already marked in error |
67278d60 | 9682 | |
d9f6a4ee | 9683 | if not Error_Posted (CC) then |
9684 | Find_Component; | |
9685 | ||
9686 | if Present (Comp) then | |
9687 | OC_Count := OC_Count + 1; | |
9688 | OC_Fbit (OC_Count) := Fbit; | |
9689 | OC_Lbit (OC_Count) := Lbit; | |
9690 | end if; | |
67278d60 | 9691 | end if; |
9692 | ||
d9f6a4ee | 9693 | Next (CC); |
67278d60 | 9694 | end loop; |
67278d60 | 9695 | |
d9f6a4ee | 9696 | Sorting.Sort (OC_Count); |
67278d60 | 9697 | |
d9f6a4ee | 9698 | Overlap_Check_Required := False; |
9699 | for J in 1 .. OC_Count - 1 loop | |
9700 | if OC_Lbit (J) >= OC_Fbit (J + 1) then | |
9701 | Overlap_Check_Required := True; | |
9702 | exit; | |
9703 | end if; | |
9704 | end loop; | |
9705 | end Overlap_Check1; | |
9706 | end if; | |
67278d60 | 9707 | |
d9f6a4ee | 9708 | -- If Overlap_Check_Required is still True, then we have to do the full |
9709 | -- scale overlap check, since we have at least two fields that do | |
9710 | -- overlap, and we need to know if that is OK since they are in | |
9711 | -- different variant, or whether we have a definite problem. | |
67278d60 | 9712 | |
d9f6a4ee | 9713 | if Overlap_Check_Required then |
9714 | Overlap_Check2 : declare | |
9715 | C1_Ent, C2_Ent : Entity_Id; | |
9716 | -- Entities of components being checked for overlap | |
67278d60 | 9717 | |
d9f6a4ee | 9718 | Clist : Node_Id; |
9719 | -- Component_List node whose Component_Items are being checked | |
67278d60 | 9720 | |
d9f6a4ee | 9721 | Citem : Node_Id; |
9722 | -- Component declaration for component being checked | |
67278d60 | 9723 | |
d9f6a4ee | 9724 | begin |
9725 | C1_Ent := First_Entity (Base_Type (Rectype)); | |
67278d60 | 9726 | |
d9f6a4ee | 9727 | -- Loop through all components in record. For each component check |
9728 | -- for overlap with any of the preceding elements on the component | |
9729 | -- list containing the component and also, if the component is in | |
9730 | -- a variant, check against components outside the case structure. | |
9731 | -- This latter test is repeated recursively up the variant tree. | |
67278d60 | 9732 | |
d9f6a4ee | 9733 | Main_Component_Loop : while Present (C1_Ent) loop |
9734 | if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then | |
9735 | goto Continue_Main_Component_Loop; | |
9736 | end if; | |
67278d60 | 9737 | |
d9f6a4ee | 9738 | -- Skip overlap check if entity has no declaration node. This |
9739 | -- happens with discriminants in constrained derived types. | |
9740 | -- Possibly we are missing some checks as a result, but that | |
9741 | -- does not seem terribly serious. | |
67278d60 | 9742 | |
d9f6a4ee | 9743 | if No (Declaration_Node (C1_Ent)) then |
9744 | goto Continue_Main_Component_Loop; | |
9745 | end if; | |
67278d60 | 9746 | |
d9f6a4ee | 9747 | Clist := Parent (List_Containing (Declaration_Node (C1_Ent))); |
67278d60 | 9748 | |
d9f6a4ee | 9749 | -- Loop through component lists that need checking. Check the |
9750 | -- current component list and all lists in variants above us. | |
67278d60 | 9751 | |
d9f6a4ee | 9752 | Component_List_Loop : loop |
67278d60 | 9753 | |
d9f6a4ee | 9754 | -- If derived type definition, go to full declaration |
9755 | -- If at outer level, check discriminants if there are any. | |
67278d60 | 9756 | |
d9f6a4ee | 9757 | if Nkind (Clist) = N_Derived_Type_Definition then |
9758 | Clist := Parent (Clist); | |
9759 | end if; | |
67278d60 | 9760 | |
d9f6a4ee | 9761 | -- Outer level of record definition, check discriminants |
67278d60 | 9762 | |
d9f6a4ee | 9763 | if Nkind_In (Clist, N_Full_Type_Declaration, |
9764 | N_Private_Type_Declaration) | |
67278d60 | 9765 | then |
d9f6a4ee | 9766 | if Has_Discriminants (Defining_Identifier (Clist)) then |
9767 | C2_Ent := | |
9768 | First_Discriminant (Defining_Identifier (Clist)); | |
9769 | while Present (C2_Ent) loop | |
9770 | exit when C1_Ent = C2_Ent; | |
9771 | Check_Component_Overlap (C1_Ent, C2_Ent); | |
9772 | Next_Discriminant (C2_Ent); | |
9773 | end loop; | |
9774 | end if; | |
67278d60 | 9775 | |
d9f6a4ee | 9776 | -- Record extension case |
67278d60 | 9777 | |
d9f6a4ee | 9778 | elsif Nkind (Clist) = N_Derived_Type_Definition then |
9779 | Clist := Empty; | |
67278d60 | 9780 | |
d9f6a4ee | 9781 | -- Otherwise check one component list |
67278d60 | 9782 | |
d9f6a4ee | 9783 | else |
9784 | Citem := First (Component_Items (Clist)); | |
9785 | while Present (Citem) loop | |
9786 | if Nkind (Citem) = N_Component_Declaration then | |
9787 | C2_Ent := Defining_Identifier (Citem); | |
9788 | exit when C1_Ent = C2_Ent; | |
9789 | Check_Component_Overlap (C1_Ent, C2_Ent); | |
9790 | end if; | |
67278d60 | 9791 | |
d9f6a4ee | 9792 | Next (Citem); |
9793 | end loop; | |
9794 | end if; | |
67278d60 | 9795 | |
d9f6a4ee | 9796 | -- Check for variants above us (the parent of the Clist can |
9797 | -- be a variant, in which case its parent is a variant part, | |
9798 | -- and the parent of the variant part is a component list | |
9799 | -- whose components must all be checked against the current | |
9800 | -- component for overlap). | |
67278d60 | 9801 | |
d9f6a4ee | 9802 | if Nkind (Parent (Clist)) = N_Variant then |
9803 | Clist := Parent (Parent (Parent (Clist))); | |
67278d60 | 9804 | |
d9f6a4ee | 9805 | -- Check for possible discriminant part in record, this |
9806 | -- is treated essentially as another level in the | |
9807 | -- recursion. For this case the parent of the component | |
9808 | -- list is the record definition, and its parent is the | |
9809 | -- full type declaration containing the discriminant | |
9810 | -- specifications. | |
9811 | ||
9812 | elsif Nkind (Parent (Clist)) = N_Record_Definition then | |
9813 | Clist := Parent (Parent ((Clist))); | |
9814 | ||
9815 | -- If neither of these two cases, we are at the top of | |
9816 | -- the tree. | |
9817 | ||
9818 | else | |
9819 | exit Component_List_Loop; | |
9820 | end if; | |
9821 | end loop Component_List_Loop; | |
67278d60 | 9822 | |
d9f6a4ee | 9823 | <<Continue_Main_Component_Loop>> |
9824 | Next_Entity (C1_Ent); | |
67278d60 | 9825 | |
d9f6a4ee | 9826 | end loop Main_Component_Loop; |
9827 | end Overlap_Check2; | |
67278d60 | 9828 | end if; |
9829 | ||
d9f6a4ee | 9830 | -- The following circuit deals with warning on record holes (gaps). We |
9831 | -- skip this check if overlap was detected, since it makes sense for the | |
9832 | -- programmer to fix this illegality before worrying about warnings. | |
67278d60 | 9833 | |
d9f6a4ee | 9834 | if not Overlap_Detected and Warn_On_Record_Holes then |
9835 | Record_Hole_Check : declare | |
9836 | Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype)); | |
9837 | -- Full declaration of record type | |
67278d60 | 9838 | |
d9f6a4ee | 9839 | procedure Check_Component_List |
9840 | (CL : Node_Id; | |
9841 | Sbit : Uint; | |
9842 | DS : List_Id); | |
9843 | -- Check component list CL for holes. The starting bit should be | |
9844 | -- Sbit. which is zero for the main record component list and set | |
9845 | -- appropriately for recursive calls for variants. DS is set to | |
9846 | -- a list of discriminant specifications to be included in the | |
9847 | -- consideration of components. It is No_List if none to consider. | |
67278d60 | 9848 | |
d9f6a4ee | 9849 | -------------------------- |
9850 | -- Check_Component_List -- | |
9851 | -------------------------- | |
47495553 | 9852 | |
d9f6a4ee | 9853 | procedure Check_Component_List |
9854 | (CL : Node_Id; | |
9855 | Sbit : Uint; | |
9856 | DS : List_Id) | |
9857 | is | |
9858 | Compl : Integer; | |
67278d60 | 9859 | |
d9f6a4ee | 9860 | begin |
9861 | Compl := Integer (List_Length (Component_Items (CL))); | |
47495553 | 9862 | |
d9f6a4ee | 9863 | if DS /= No_List then |
9864 | Compl := Compl + Integer (List_Length (DS)); | |
9865 | end if; | |
67278d60 | 9866 | |
d9f6a4ee | 9867 | declare |
9868 | Comps : array (Natural range 0 .. Compl) of Entity_Id; | |
9869 | -- Gather components (zero entry is for sort routine) | |
67278d60 | 9870 | |
d9f6a4ee | 9871 | Ncomps : Natural := 0; |
9872 | -- Number of entries stored in Comps (starting at Comps (1)) | |
67278d60 | 9873 | |
d9f6a4ee | 9874 | Citem : Node_Id; |
9875 | -- One component item or discriminant specification | |
67278d60 | 9876 | |
d9f6a4ee | 9877 | Nbit : Uint; |
9878 | -- Starting bit for next component | |
67278d60 | 9879 | |
d9f6a4ee | 9880 | CEnt : Entity_Id; |
9881 | -- Component entity | |
67278d60 | 9882 | |
d9f6a4ee | 9883 | Variant : Node_Id; |
9884 | -- One variant | |
67278d60 | 9885 | |
d9f6a4ee | 9886 | function Lt (Op1, Op2 : Natural) return Boolean; |
9887 | -- Compare routine for Sort | |
67278d60 | 9888 | |
d9f6a4ee | 9889 | procedure Move (From : Natural; To : Natural); |
9890 | -- Move routine for Sort | |
67278d60 | 9891 | |
d9f6a4ee | 9892 | package Sorting is new GNAT.Heap_Sort_G (Move, Lt); |
67278d60 | 9893 | |
d9f6a4ee | 9894 | -------- |
9895 | -- Lt -- | |
9896 | -------- | |
67278d60 | 9897 | |
d9f6a4ee | 9898 | function Lt (Op1, Op2 : Natural) return Boolean is |
9899 | begin | |
9900 | return Component_Bit_Offset (Comps (Op1)) | |
9901 | < | |
9902 | Component_Bit_Offset (Comps (Op2)); | |
9903 | end Lt; | |
67278d60 | 9904 | |
d9f6a4ee | 9905 | ---------- |
9906 | -- Move -- | |
9907 | ---------- | |
67278d60 | 9908 | |
d9f6a4ee | 9909 | procedure Move (From : Natural; To : Natural) is |
9910 | begin | |
9911 | Comps (To) := Comps (From); | |
9912 | end Move; | |
67278d60 | 9913 | |
d9f6a4ee | 9914 | begin |
9915 | -- Gather discriminants into Comp | |
67278d60 | 9916 | |
d9f6a4ee | 9917 | if DS /= No_List then |
9918 | Citem := First (DS); | |
9919 | while Present (Citem) loop | |
9920 | if Nkind (Citem) = N_Discriminant_Specification then | |
9921 | declare | |
9922 | Ent : constant Entity_Id := | |
9923 | Defining_Identifier (Citem); | |
9924 | begin | |
9925 | if Ekind (Ent) = E_Discriminant then | |
9926 | Ncomps := Ncomps + 1; | |
9927 | Comps (Ncomps) := Ent; | |
9928 | end if; | |
9929 | end; | |
9930 | end if; | |
67278d60 | 9931 | |
d9f6a4ee | 9932 | Next (Citem); |
9933 | end loop; | |
9934 | end if; | |
67278d60 | 9935 | |
d9f6a4ee | 9936 | -- Gather component entities into Comp |
67278d60 | 9937 | |
d9f6a4ee | 9938 | Citem := First (Component_Items (CL)); |
9939 | while Present (Citem) loop | |
9940 | if Nkind (Citem) = N_Component_Declaration then | |
9941 | Ncomps := Ncomps + 1; | |
9942 | Comps (Ncomps) := Defining_Identifier (Citem); | |
9943 | end if; | |
67278d60 | 9944 | |
d9f6a4ee | 9945 | Next (Citem); |
9946 | end loop; | |
67278d60 | 9947 | |
d9f6a4ee | 9948 | -- Now sort the component entities based on the first bit. |
9949 | -- Note we already know there are no overlapping components. | |
67278d60 | 9950 | |
d9f6a4ee | 9951 | Sorting.Sort (Ncomps); |
67278d60 | 9952 | |
d9f6a4ee | 9953 | -- Loop through entries checking for holes |
67278d60 | 9954 | |
d9f6a4ee | 9955 | Nbit := Sbit; |
9956 | for J in 1 .. Ncomps loop | |
9957 | CEnt := Comps (J); | |
9958 | Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit; | |
67278d60 | 9959 | |
d9f6a4ee | 9960 | if Error_Msg_Uint_1 > 0 then |
9961 | Error_Msg_NE | |
9962 | ("?H?^-bit gap before component&", | |
9963 | Component_Name (Component_Clause (CEnt)), CEnt); | |
9964 | end if; | |
67278d60 | 9965 | |
d9f6a4ee | 9966 | Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt); |
9967 | end loop; | |
67278d60 | 9968 | |
d9f6a4ee | 9969 | -- Process variant parts recursively if present |
67278d60 | 9970 | |
d9f6a4ee | 9971 | if Present (Variant_Part (CL)) then |
9972 | Variant := First (Variants (Variant_Part (CL))); | |
9973 | while Present (Variant) loop | |
9974 | Check_Component_List | |
9975 | (Component_List (Variant), Nbit, No_List); | |
9976 | Next (Variant); | |
9977 | end loop; | |
67278d60 | 9978 | end if; |
d9f6a4ee | 9979 | end; |
9980 | end Check_Component_List; | |
67278d60 | 9981 | |
d9f6a4ee | 9982 | -- Start of processing for Record_Hole_Check |
67278d60 | 9983 | |
d9f6a4ee | 9984 | begin |
9985 | declare | |
9986 | Sbit : Uint; | |
67278d60 | 9987 | |
d9f6a4ee | 9988 | begin |
9989 | if Is_Tagged_Type (Rectype) then | |
9990 | Sbit := UI_From_Int (System_Address_Size); | |
9991 | else | |
9992 | Sbit := Uint_0; | |
9993 | end if; | |
9994 | ||
9995 | if Nkind (Decl) = N_Full_Type_Declaration | |
9996 | and then Nkind (Type_Definition (Decl)) = N_Record_Definition | |
9997 | then | |
9998 | Check_Component_List | |
9999 | (Component_List (Type_Definition (Decl)), | |
10000 | Sbit, | |
10001 | Discriminant_Specifications (Decl)); | |
67278d60 | 10002 | end if; |
d9f6a4ee | 10003 | end; |
10004 | end Record_Hole_Check; | |
67278d60 | 10005 | end if; |
10006 | ||
d9f6a4ee | 10007 | -- For records that have component clauses for all components, and whose |
10008 | -- size is less than or equal to 32, we need to know the size in the | |
10009 | -- front end to activate possible packed array processing where the | |
10010 | -- component type is a record. | |
67278d60 | 10011 | |
d9f6a4ee | 10012 | -- At this stage Hbit + 1 represents the first unused bit from all the |
10013 | -- component clauses processed, so if the component clauses are | |
10014 | -- complete, then this is the length of the record. | |
67278d60 | 10015 | |
d9f6a4ee | 10016 | -- For records longer than System.Storage_Unit, and for those where not |
10017 | -- all components have component clauses, the back end determines the | |
10018 | -- length (it may for example be appropriate to round up the size | |
10019 | -- to some convenient boundary, based on alignment considerations, etc). | |
67278d60 | 10020 | |
d9f6a4ee | 10021 | if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then |
67278d60 | 10022 | |
d9f6a4ee | 10023 | -- Nothing to do if at least one component has no component clause |
67278d60 | 10024 | |
d9f6a4ee | 10025 | Comp := First_Component_Or_Discriminant (Rectype); |
10026 | while Present (Comp) loop | |
10027 | exit when No (Component_Clause (Comp)); | |
10028 | Next_Component_Or_Discriminant (Comp); | |
10029 | end loop; | |
67278d60 | 10030 | |
d9f6a4ee | 10031 | -- If we fall out of loop, all components have component clauses |
10032 | -- and so we can set the size to the maximum value. | |
67278d60 | 10033 | |
d9f6a4ee | 10034 | if No (Comp) then |
10035 | Set_RM_Size (Rectype, Hbit + 1); | |
10036 | end if; | |
10037 | end if; | |
10038 | end Check_Record_Representation_Clause; | |
67278d60 | 10039 | |
d9f6a4ee | 10040 | ---------------- |
10041 | -- Check_Size -- | |
10042 | ---------------- | |
67278d60 | 10043 | |
d9f6a4ee | 10044 | procedure Check_Size |
10045 | (N : Node_Id; | |
10046 | T : Entity_Id; | |
10047 | Siz : Uint; | |
10048 | Biased : out Boolean) | |
10049 | is | |
10050 | UT : constant Entity_Id := Underlying_Type (T); | |
10051 | M : Uint; | |
67278d60 | 10052 | |
d9f6a4ee | 10053 | begin |
10054 | Biased := False; | |
67278d60 | 10055 | |
d9f6a4ee | 10056 | -- Reject patently improper size values. |
67278d60 | 10057 | |
d9f6a4ee | 10058 | if Is_Elementary_Type (T) |
10059 | and then Siz > UI_From_Int (Int'Last) | |
10060 | then | |
10061 | Error_Msg_N ("Size value too large for elementary type", N); | |
67278d60 | 10062 | |
d9f6a4ee | 10063 | if Nkind (Original_Node (N)) = N_Op_Expon then |
10064 | Error_Msg_N | |
10065 | ("\maybe '* was meant, rather than '*'*", Original_Node (N)); | |
10066 | end if; | |
10067 | end if; | |
67278d60 | 10068 | |
d9f6a4ee | 10069 | -- Dismiss generic types |
67278d60 | 10070 | |
d9f6a4ee | 10071 | if Is_Generic_Type (T) |
10072 | or else | |
10073 | Is_Generic_Type (UT) | |
10074 | or else | |
10075 | Is_Generic_Type (Root_Type (UT)) | |
10076 | then | |
10077 | return; | |
67278d60 | 10078 | |
d9f6a4ee | 10079 | -- Guard against previous errors |
67278d60 | 10080 | |
d9f6a4ee | 10081 | elsif No (UT) or else UT = Any_Type then |
10082 | Check_Error_Detected; | |
10083 | return; | |
67278d60 | 10084 | |
d9f6a4ee | 10085 | -- Check case of bit packed array |
67278d60 | 10086 | |
d9f6a4ee | 10087 | elsif Is_Array_Type (UT) |
10088 | and then Known_Static_Component_Size (UT) | |
10089 | and then Is_Bit_Packed_Array (UT) | |
10090 | then | |
10091 | declare | |
10092 | Asiz : Uint; | |
10093 | Indx : Node_Id; | |
10094 | Ityp : Entity_Id; | |
67278d60 | 10095 | |
d9f6a4ee | 10096 | begin |
10097 | Asiz := Component_Size (UT); | |
10098 | Indx := First_Index (UT); | |
10099 | loop | |
10100 | Ityp := Etype (Indx); | |
67278d60 | 10101 | |
d9f6a4ee | 10102 | -- If non-static bound, then we are not in the business of |
10103 | -- trying to check the length, and indeed an error will be | |
10104 | -- issued elsewhere, since sizes of non-static array types | |
10105 | -- cannot be set implicitly or explicitly. | |
67278d60 | 10106 | |
cda40848 | 10107 | if not Is_OK_Static_Subtype (Ityp) then |
d9f6a4ee | 10108 | return; |
10109 | end if; | |
67278d60 | 10110 | |
d9f6a4ee | 10111 | -- Otherwise accumulate next dimension |
67278d60 | 10112 | |
d9f6a4ee | 10113 | Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) - |
10114 | Expr_Value (Type_Low_Bound (Ityp)) + | |
10115 | Uint_1); | |
67278d60 | 10116 | |
d9f6a4ee | 10117 | Next_Index (Indx); |
10118 | exit when No (Indx); | |
10119 | end loop; | |
67278d60 | 10120 | |
d9f6a4ee | 10121 | if Asiz <= Siz then |
10122 | return; | |
67278d60 | 10123 | |
d9f6a4ee | 10124 | else |
10125 | Error_Msg_Uint_1 := Asiz; | |
10126 | Error_Msg_NE | |
10127 | ("size for& too small, minimum allowed is ^", N, T); | |
10128 | Set_Esize (T, Asiz); | |
10129 | Set_RM_Size (T, Asiz); | |
10130 | end if; | |
10131 | end; | |
67278d60 | 10132 | |
d9f6a4ee | 10133 | -- All other composite types are ignored |
67278d60 | 10134 | |
d9f6a4ee | 10135 | elsif Is_Composite_Type (UT) then |
10136 | return; | |
47495553 | 10137 | |
d9f6a4ee | 10138 | -- For fixed-point types, don't check minimum if type is not frozen, |
10139 | -- since we don't know all the characteristics of the type that can | |
10140 | -- affect the size (e.g. a specified small) till freeze time. | |
47495553 | 10141 | |
d9f6a4ee | 10142 | elsif Is_Fixed_Point_Type (UT) |
10143 | and then not Is_Frozen (UT) | |
10144 | then | |
10145 | null; | |
47495553 | 10146 | |
d9f6a4ee | 10147 | -- Cases for which a minimum check is required |
47495553 | 10148 | |
d9f6a4ee | 10149 | else |
10150 | -- Ignore if specified size is correct for the type | |
47495553 | 10151 | |
d9f6a4ee | 10152 | if Known_Esize (UT) and then Siz = Esize (UT) then |
10153 | return; | |
10154 | end if; | |
47495553 | 10155 | |
d9f6a4ee | 10156 | -- Otherwise get minimum size |
47495553 | 10157 | |
d9f6a4ee | 10158 | M := UI_From_Int (Minimum_Size (UT)); |
47495553 | 10159 | |
d9f6a4ee | 10160 | if Siz < M then |
47495553 | 10161 | |
d9f6a4ee | 10162 | -- Size is less than minimum size, but one possibility remains |
10163 | -- that we can manage with the new size if we bias the type. | |
47495553 | 10164 | |
d9f6a4ee | 10165 | M := UI_From_Int (Minimum_Size (UT, Biased => True)); |
47495553 | 10166 | |
d9f6a4ee | 10167 | if Siz < M then |
10168 | Error_Msg_Uint_1 := M; | |
10169 | Error_Msg_NE | |
10170 | ("size for& too small, minimum allowed is ^", N, T); | |
10171 | Set_Esize (T, M); | |
10172 | Set_RM_Size (T, M); | |
10173 | else | |
10174 | Biased := True; | |
10175 | end if; | |
10176 | end if; | |
10177 | end if; | |
10178 | end Check_Size; | |
47495553 | 10179 | |
d9f6a4ee | 10180 | -------------------------- |
10181 | -- Freeze_Entity_Checks -- | |
10182 | -------------------------- | |
47495553 | 10183 | |
d9f6a4ee | 10184 | procedure Freeze_Entity_Checks (N : Node_Id) is |
8cf481c9 | 10185 | procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id); |
10186 | -- Inspect the primitive operations of type Typ and hide all pairs of | |
3118058b | 10187 | -- implicitly declared non-overridden non-fully conformant homographs |
10188 | -- (Ada RM 8.3 12.3/2). | |
8cf481c9 | 10189 | |
10190 | ------------------------------------- | |
10191 | -- Hide_Non_Overridden_Subprograms -- | |
10192 | ------------------------------------- | |
10193 | ||
10194 | procedure Hide_Non_Overridden_Subprograms (Typ : Entity_Id) is | |
10195 | procedure Hide_Matching_Homographs | |
10196 | (Subp_Id : Entity_Id; | |
10197 | Start_Elmt : Elmt_Id); | |
10198 | -- Inspect a list of primitive operations starting with Start_Elmt | |
3118058b | 10199 | -- and find matching implicitly declared non-overridden non-fully |
10200 | -- conformant homographs of Subp_Id. If found, all matches along | |
10201 | -- with Subp_Id are hidden from all visibility. | |
8cf481c9 | 10202 | |
10203 | function Is_Non_Overridden_Or_Null_Procedure | |
10204 | (Subp_Id : Entity_Id) return Boolean; | |
10205 | -- Determine whether subprogram Subp_Id is implicitly declared non- | |
10206 | -- overridden subprogram or an implicitly declared null procedure. | |
10207 | ||
10208 | ------------------------------ | |
10209 | -- Hide_Matching_Homographs -- | |
10210 | ------------------------------ | |
10211 | ||
10212 | procedure Hide_Matching_Homographs | |
10213 | (Subp_Id : Entity_Id; | |
10214 | Start_Elmt : Elmt_Id) | |
10215 | is | |
10216 | Prim : Entity_Id; | |
10217 | Prim_Elmt : Elmt_Id; | |
10218 | ||
10219 | begin | |
10220 | Prim_Elmt := Start_Elmt; | |
10221 | while Present (Prim_Elmt) loop | |
10222 | Prim := Node (Prim_Elmt); | |
10223 | ||
10224 | -- The current primitive is implicitly declared non-overridden | |
3118058b | 10225 | -- non-fully conformant homograph of Subp_Id. Both subprograms |
10226 | -- must be hidden from visibility. | |
8cf481c9 | 10227 | |
10228 | if Chars (Prim) = Chars (Subp_Id) | |
8cf481c9 | 10229 | and then Is_Non_Overridden_Or_Null_Procedure (Prim) |
3118058b | 10230 | and then not Fully_Conformant (Prim, Subp_Id) |
8cf481c9 | 10231 | then |
8c7ee4ac | 10232 | Set_Is_Hidden_Non_Overridden_Subpgm (Prim); |
10233 | Set_Is_Immediately_Visible (Prim, False); | |
10234 | Set_Is_Potentially_Use_Visible (Prim, False); | |
8cf481c9 | 10235 | |
8c7ee4ac | 10236 | Set_Is_Hidden_Non_Overridden_Subpgm (Subp_Id); |
10237 | Set_Is_Immediately_Visible (Subp_Id, False); | |
10238 | Set_Is_Potentially_Use_Visible (Subp_Id, False); | |
8cf481c9 | 10239 | end if; |
10240 | ||
10241 | Next_Elmt (Prim_Elmt); | |
10242 | end loop; | |
10243 | end Hide_Matching_Homographs; | |
10244 | ||
10245 | ----------------------------------------- | |
10246 | -- Is_Non_Overridden_Or_Null_Procedure -- | |
10247 | ----------------------------------------- | |
10248 | ||
10249 | function Is_Non_Overridden_Or_Null_Procedure | |
10250 | (Subp_Id : Entity_Id) return Boolean | |
10251 | is | |
10252 | Alias_Id : Entity_Id; | |
10253 | ||
10254 | begin | |
10255 | -- The subprogram is inherited (implicitly declared), it does not | |
10256 | -- override and does not cover a primitive of an interface. | |
10257 | ||
10258 | if Ekind_In (Subp_Id, E_Function, E_Procedure) | |
10259 | and then Present (Alias (Subp_Id)) | |
10260 | and then No (Interface_Alias (Subp_Id)) | |
10261 | and then No (Overridden_Operation (Subp_Id)) | |
10262 | then | |
10263 | Alias_Id := Alias (Subp_Id); | |
10264 | ||
10265 | if Requires_Overriding (Alias_Id) then | |
10266 | return True; | |
10267 | ||
10268 | elsif Nkind (Parent (Alias_Id)) = N_Procedure_Specification | |
10269 | and then Null_Present (Parent (Alias_Id)) | |
10270 | then | |
10271 | return True; | |
10272 | end if; | |
10273 | end if; | |
10274 | ||
10275 | return False; | |
10276 | end Is_Non_Overridden_Or_Null_Procedure; | |
10277 | ||
10278 | -- Local variables | |
10279 | ||
10280 | Prim_Ops : constant Elist_Id := Direct_Primitive_Operations (Typ); | |
10281 | Prim : Entity_Id; | |
10282 | Prim_Elmt : Elmt_Id; | |
10283 | ||
10284 | -- Start of processing for Hide_Non_Overridden_Subprograms | |
10285 | ||
10286 | begin | |
3118058b | 10287 | -- Inspect the list of primitives looking for non-overridden |
10288 | -- subprograms. | |
8cf481c9 | 10289 | |
10290 | if Present (Prim_Ops) then | |
10291 | Prim_Elmt := First_Elmt (Prim_Ops); | |
10292 | while Present (Prim_Elmt) loop | |
10293 | Prim := Node (Prim_Elmt); | |
10294 | Next_Elmt (Prim_Elmt); | |
10295 | ||
10296 | if Is_Non_Overridden_Or_Null_Procedure (Prim) then | |
10297 | Hide_Matching_Homographs | |
10298 | (Subp_Id => Prim, | |
10299 | Start_Elmt => Prim_Elmt); | |
10300 | end if; | |
10301 | end loop; | |
10302 | end if; | |
10303 | end Hide_Non_Overridden_Subprograms; | |
10304 | ||
10305 | --------------------- | |
10306 | -- Local variables -- | |
10307 | --------------------- | |
10308 | ||
d9f6a4ee | 10309 | E : constant Entity_Id := Entity (N); |
47495553 | 10310 | |
d9f6a4ee | 10311 | Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity; |
10312 | -- True in non-generic case. Some of the processing here is skipped | |
10313 | -- for the generic case since it is not needed. Basically in the | |
10314 | -- generic case, we only need to do stuff that might generate error | |
10315 | -- messages or warnings. | |
8cf481c9 | 10316 | |
10317 | -- Start of processing for Freeze_Entity_Checks | |
10318 | ||
d9f6a4ee | 10319 | begin |
10320 | -- Remember that we are processing a freezing entity. Required to | |
10321 | -- ensure correct decoration of internal entities associated with | |
10322 | -- interfaces (see New_Overloaded_Entity). | |
47495553 | 10323 | |
d9f6a4ee | 10324 | Inside_Freezing_Actions := Inside_Freezing_Actions + 1; |
47495553 | 10325 | |
d9f6a4ee | 10326 | -- For tagged types covering interfaces add internal entities that link |
10327 | -- the primitives of the interfaces with the primitives that cover them. | |
10328 | -- Note: These entities were originally generated only when generating | |
10329 | -- code because their main purpose was to provide support to initialize | |
10330 | -- the secondary dispatch tables. They are now generated also when | |
10331 | -- compiling with no code generation to provide ASIS the relationship | |
10332 | -- between interface primitives and tagged type primitives. They are | |
10333 | -- also used to locate primitives covering interfaces when processing | |
10334 | -- generics (see Derive_Subprograms). | |
47495553 | 10335 | |
d9f6a4ee | 10336 | -- This is not needed in the generic case |
47495553 | 10337 | |
d9f6a4ee | 10338 | if Ada_Version >= Ada_2005 |
10339 | and then Non_Generic_Case | |
10340 | and then Ekind (E) = E_Record_Type | |
10341 | and then Is_Tagged_Type (E) | |
10342 | and then not Is_Interface (E) | |
10343 | and then Has_Interfaces (E) | |
10344 | then | |
10345 | -- This would be a good common place to call the routine that checks | |
10346 | -- overriding of interface primitives (and thus factorize calls to | |
10347 | -- Check_Abstract_Overriding located at different contexts in the | |
10348 | -- compiler). However, this is not possible because it causes | |
10349 | -- spurious errors in case of late overriding. | |
47495553 | 10350 | |
d9f6a4ee | 10351 | Add_Internal_Interface_Entities (E); |
10352 | end if; | |
47495553 | 10353 | |
8cf481c9 | 10354 | -- After all forms of overriding have been resolved, a tagged type may |
10355 | -- be left with a set of implicitly declared and possibly erroneous | |
10356 | -- abstract subprograms, null procedures and subprograms that require | |
10357 | -- overriding. If this set contains fully conformat homographs, then one | |
10358 | -- is chosen arbitrarily (already done during resolution), otherwise all | |
3118058b | 10359 | -- remaining non-fully conformant homographs are hidden from visibility |
8cf481c9 | 10360 | -- (Ada RM 8.3 12.3/2). |
10361 | ||
10362 | if Is_Tagged_Type (E) then | |
10363 | Hide_Non_Overridden_Subprograms (E); | |
10364 | end if; | |
10365 | ||
d9f6a4ee | 10366 | -- Check CPP types |
47495553 | 10367 | |
d9f6a4ee | 10368 | if Ekind (E) = E_Record_Type |
10369 | and then Is_CPP_Class (E) | |
10370 | and then Is_Tagged_Type (E) | |
10371 | and then Tagged_Type_Expansion | |
d9f6a4ee | 10372 | then |
10373 | if CPP_Num_Prims (E) = 0 then | |
47495553 | 10374 | |
d9f6a4ee | 10375 | -- If the CPP type has user defined components then it must import |
10376 | -- primitives from C++. This is required because if the C++ class | |
10377 | -- has no primitives then the C++ compiler does not added the _tag | |
10378 | -- component to the type. | |
47495553 | 10379 | |
d9f6a4ee | 10380 | if First_Entity (E) /= Last_Entity (E) then |
10381 | Error_Msg_N | |
10382 | ("'C'P'P type must import at least one primitive from C++??", | |
10383 | E); | |
10384 | end if; | |
10385 | end if; | |
47495553 | 10386 | |
d9f6a4ee | 10387 | -- Check that all its primitives are abstract or imported from C++. |
10388 | -- Check also availability of the C++ constructor. | |
47495553 | 10389 | |
d9f6a4ee | 10390 | declare |
10391 | Has_Constructors : constant Boolean := Has_CPP_Constructors (E); | |
10392 | Elmt : Elmt_Id; | |
10393 | Error_Reported : Boolean := False; | |
10394 | Prim : Node_Id; | |
47495553 | 10395 | |
d9f6a4ee | 10396 | begin |
10397 | Elmt := First_Elmt (Primitive_Operations (E)); | |
10398 | while Present (Elmt) loop | |
10399 | Prim := Node (Elmt); | |
47495553 | 10400 | |
d9f6a4ee | 10401 | if Comes_From_Source (Prim) then |
10402 | if Is_Abstract_Subprogram (Prim) then | |
10403 | null; | |
47495553 | 10404 | |
d9f6a4ee | 10405 | elsif not Is_Imported (Prim) |
10406 | or else Convention (Prim) /= Convention_CPP | |
10407 | then | |
10408 | Error_Msg_N | |
10409 | ("primitives of 'C'P'P types must be imported from C++ " | |
10410 | & "or abstract??", Prim); | |
47495553 | 10411 | |
d9f6a4ee | 10412 | elsif not Has_Constructors |
10413 | and then not Error_Reported | |
10414 | then | |
10415 | Error_Msg_Name_1 := Chars (E); | |
10416 | Error_Msg_N | |
10417 | ("??'C'P'P constructor required for type %", Prim); | |
10418 | Error_Reported := True; | |
10419 | end if; | |
10420 | end if; | |
47495553 | 10421 | |
d9f6a4ee | 10422 | Next_Elmt (Elmt); |
10423 | end loop; | |
10424 | end; | |
10425 | end if; | |
47495553 | 10426 | |
d9f6a4ee | 10427 | -- Check Ada derivation of CPP type |
47495553 | 10428 | |
30ab103b | 10429 | if Expander_Active -- why? losing errors in -gnatc mode??? |
10430 | and then Present (Etype (E)) -- defend against errors | |
d9f6a4ee | 10431 | and then Tagged_Type_Expansion |
10432 | and then Ekind (E) = E_Record_Type | |
10433 | and then Etype (E) /= E | |
10434 | and then Is_CPP_Class (Etype (E)) | |
10435 | and then CPP_Num_Prims (Etype (E)) > 0 | |
10436 | and then not Is_CPP_Class (E) | |
10437 | and then not Has_CPP_Constructors (Etype (E)) | |
10438 | then | |
10439 | -- If the parent has C++ primitives but it has no constructor then | |
10440 | -- check that all the primitives are overridden in this derivation; | |
10441 | -- otherwise the constructor of the parent is needed to build the | |
10442 | -- dispatch table. | |
47495553 | 10443 | |
d9f6a4ee | 10444 | declare |
10445 | Elmt : Elmt_Id; | |
10446 | Prim : Node_Id; | |
47495553 | 10447 | |
10448 | begin | |
d9f6a4ee | 10449 | Elmt := First_Elmt (Primitive_Operations (E)); |
10450 | while Present (Elmt) loop | |
10451 | Prim := Node (Elmt); | |
47495553 | 10452 | |
d9f6a4ee | 10453 | if not Is_Abstract_Subprogram (Prim) |
10454 | and then No (Interface_Alias (Prim)) | |
10455 | and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E | |
47495553 | 10456 | then |
d9f6a4ee | 10457 | Error_Msg_Name_1 := Chars (Etype (E)); |
10458 | Error_Msg_N | |
10459 | ("'C'P'P constructor required for parent type %", E); | |
10460 | exit; | |
47495553 | 10461 | end if; |
d9f6a4ee | 10462 | |
10463 | Next_Elmt (Elmt); | |
10464 | end loop; | |
10465 | end; | |
47495553 | 10466 | end if; |
10467 | ||
d9f6a4ee | 10468 | Inside_Freezing_Actions := Inside_Freezing_Actions - 1; |
67278d60 | 10469 | |
d9f6a4ee | 10470 | -- If we have a type with predicates, build predicate function. This |
0e9014a7 | 10471 | -- is not needed in the generic case, and is not needed within TSS |
ea822fd4 | 10472 | -- subprograms and other predefined primitives. |
67278d60 | 10473 | |
ea822fd4 | 10474 | if Non_Generic_Case |
10475 | and then Is_Type (E) | |
10476 | and then Has_Predicates (E) | |
10477 | and then not Within_Internal_Subprogram | |
10478 | then | |
d9f6a4ee | 10479 | Build_Predicate_Functions (E, N); |
10480 | end if; | |
67278d60 | 10481 | |
d9f6a4ee | 10482 | -- If type has delayed aspects, this is where we do the preanalysis at |
10483 | -- the freeze point, as part of the consistent visibility check. Note | |
10484 | -- that this must be done after calling Build_Predicate_Functions or | |
10485 | -- Build_Invariant_Procedure since these subprograms fix occurrences of | |
10486 | -- the subtype name in the saved expression so that they will not cause | |
10487 | -- trouble in the preanalysis. | |
67278d60 | 10488 | |
d9f6a4ee | 10489 | -- This is also not needed in the generic case |
10490 | ||
10491 | if Non_Generic_Case | |
10492 | and then Has_Delayed_Aspects (E) | |
10493 | and then Scope (E) = Current_Scope | |
10494 | then | |
10495 | -- Retrieve the visibility to the discriminants in order to properly | |
10496 | -- analyze the aspects. | |
10497 | ||
10498 | Push_Scope_And_Install_Discriminants (E); | |
10499 | ||
10500 | declare | |
10501 | Ritem : Node_Id; | |
10502 | ||
10503 | begin | |
10504 | -- Look for aspect specification entries for this entity | |
67278d60 | 10505 | |
d9f6a4ee | 10506 | Ritem := First_Rep_Item (E); |
10507 | while Present (Ritem) loop | |
10508 | if Nkind (Ritem) = N_Aspect_Specification | |
10509 | and then Entity (Ritem) = E | |
10510 | and then Is_Delayed_Aspect (Ritem) | |
10511 | then | |
10512 | Check_Aspect_At_Freeze_Point (Ritem); | |
10513 | end if; | |
67278d60 | 10514 | |
d9f6a4ee | 10515 | Next_Rep_Item (Ritem); |
10516 | end loop; | |
10517 | end; | |
67278d60 | 10518 | |
d9f6a4ee | 10519 | Uninstall_Discriminants_And_Pop_Scope (E); |
67278d60 | 10520 | end if; |
67278d60 | 10521 | |
d9f6a4ee | 10522 | -- For a record type, deal with variant parts. This has to be delayed |
d0988351 | 10523 | -- to this point, because of the issue of statically predicated |
d9f6a4ee | 10524 | -- subtypes, which we have to ensure are frozen before checking |
10525 | -- choices, since we need to have the static choice list set. | |
d6f39728 | 10526 | |
d9f6a4ee | 10527 | if Is_Record_Type (E) then |
10528 | Check_Variant_Part : declare | |
10529 | D : constant Node_Id := Declaration_Node (E); | |
10530 | T : Node_Id; | |
10531 | C : Node_Id; | |
10532 | VP : Node_Id; | |
d6f39728 | 10533 | |
d9f6a4ee | 10534 | Others_Present : Boolean; |
10535 | pragma Warnings (Off, Others_Present); | |
10536 | -- Indicates others present, not used in this case | |
d6f39728 | 10537 | |
d9f6a4ee | 10538 | procedure Non_Static_Choice_Error (Choice : Node_Id); |
10539 | -- Error routine invoked by the generic instantiation below when | |
10540 | -- the variant part has a non static choice. | |
f117057b | 10541 | |
d9f6a4ee | 10542 | procedure Process_Declarations (Variant : Node_Id); |
10543 | -- Processes declarations associated with a variant. We analyzed | |
10544 | -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part), | |
10545 | -- but we still need the recursive call to Check_Choices for any | |
10546 | -- nested variant to get its choices properly processed. This is | |
10547 | -- also where we expand out the choices if expansion is active. | |
1f526845 | 10548 | |
d9f6a4ee | 10549 | package Variant_Choices_Processing is new |
10550 | Generic_Check_Choices | |
10551 | (Process_Empty_Choice => No_OP, | |
10552 | Process_Non_Static_Choice => Non_Static_Choice_Error, | |
10553 | Process_Associated_Node => Process_Declarations); | |
10554 | use Variant_Choices_Processing; | |
f117057b | 10555 | |
d9f6a4ee | 10556 | ----------------------------- |
10557 | -- Non_Static_Choice_Error -- | |
10558 | ----------------------------- | |
d6f39728 | 10559 | |
d9f6a4ee | 10560 | procedure Non_Static_Choice_Error (Choice : Node_Id) is |
10561 | begin | |
10562 | Flag_Non_Static_Expr | |
10563 | ("choice given in variant part is not static!", Choice); | |
10564 | end Non_Static_Choice_Error; | |
d6f39728 | 10565 | |
d9f6a4ee | 10566 | -------------------------- |
10567 | -- Process_Declarations -- | |
10568 | -------------------------- | |
dba36b60 | 10569 | |
d9f6a4ee | 10570 | procedure Process_Declarations (Variant : Node_Id) is |
10571 | CL : constant Node_Id := Component_List (Variant); | |
10572 | VP : Node_Id; | |
dba36b60 | 10573 | |
d9f6a4ee | 10574 | begin |
10575 | -- Check for static predicate present in this variant | |
ea61a7ea | 10576 | |
d9f6a4ee | 10577 | if Has_SP_Choice (Variant) then |
ea61a7ea | 10578 | |
d9f6a4ee | 10579 | -- Here we expand. You might expect to find this call in |
10580 | -- Expand_N_Variant_Part, but that is called when we first | |
10581 | -- see the variant part, and we cannot do this expansion | |
10582 | -- earlier than the freeze point, since for statically | |
10583 | -- predicated subtypes, the predicate is not known till | |
10584 | -- the freeze point. | |
ea61a7ea | 10585 | |
d9f6a4ee | 10586 | -- Furthermore, we do this expansion even if the expander |
10587 | -- is not active, because other semantic processing, e.g. | |
10588 | -- for aggregates, requires the expanded list of choices. | |
ea61a7ea | 10589 | |
d9f6a4ee | 10590 | -- If the expander is not active, then we can't just clobber |
10591 | -- the list since it would invalidate the ASIS -gnatct tree. | |
10592 | -- So we have to rewrite the variant part with a Rewrite | |
10593 | -- call that replaces it with a copy and clobber the copy. | |
10594 | ||
10595 | if not Expander_Active then | |
10596 | declare | |
10597 | NewV : constant Node_Id := New_Copy (Variant); | |
10598 | begin | |
10599 | Set_Discrete_Choices | |
10600 | (NewV, New_Copy_List (Discrete_Choices (Variant))); | |
10601 | Rewrite (Variant, NewV); | |
10602 | end; | |
10603 | end if; | |
10604 | ||
10605 | Expand_Static_Predicates_In_Choices (Variant); | |
ea61a7ea | 10606 | end if; |
10607 | ||
d9f6a4ee | 10608 | -- We don't need to worry about the declarations in the variant |
10609 | -- (since they were analyzed by Analyze_Choices when we first | |
10610 | -- encountered the variant), but we do need to take care of | |
10611 | -- expansion of any nested variants. | |
ea61a7ea | 10612 | |
d9f6a4ee | 10613 | if not Null_Present (CL) then |
10614 | VP := Variant_Part (CL); | |
ea61a7ea | 10615 | |
d9f6a4ee | 10616 | if Present (VP) then |
10617 | Check_Choices | |
10618 | (VP, Variants (VP), Etype (Name (VP)), Others_Present); | |
10619 | end if; | |
10620 | end if; | |
10621 | end Process_Declarations; | |
ea61a7ea | 10622 | |
d9f6a4ee | 10623 | -- Start of processing for Check_Variant_Part |
b9e61b2a | 10624 | |
d9f6a4ee | 10625 | begin |
10626 | -- Find component list | |
ea61a7ea | 10627 | |
d9f6a4ee | 10628 | C := Empty; |
ea61a7ea | 10629 | |
d9f6a4ee | 10630 | if Nkind (D) = N_Full_Type_Declaration then |
10631 | T := Type_Definition (D); | |
ea61a7ea | 10632 | |
d9f6a4ee | 10633 | if Nkind (T) = N_Record_Definition then |
10634 | C := Component_List (T); | |
d6f39728 | 10635 | |
d9f6a4ee | 10636 | elsif Nkind (T) = N_Derived_Type_Definition |
10637 | and then Present (Record_Extension_Part (T)) | |
10638 | then | |
10639 | C := Component_List (Record_Extension_Part (T)); | |
10640 | end if; | |
10641 | end if; | |
d6f39728 | 10642 | |
d9f6a4ee | 10643 | -- Case of variant part present |
d6f39728 | 10644 | |
d9f6a4ee | 10645 | if Present (C) and then Present (Variant_Part (C)) then |
10646 | VP := Variant_Part (C); | |
ea61a7ea | 10647 | |
d9f6a4ee | 10648 | -- Check choices |
ea61a7ea | 10649 | |
d9f6a4ee | 10650 | Check_Choices |
10651 | (VP, Variants (VP), Etype (Name (VP)), Others_Present); | |
ea61a7ea | 10652 | |
d9f6a4ee | 10653 | -- If the last variant does not contain the Others choice, |
10654 | -- replace it with an N_Others_Choice node since Gigi always | |
10655 | -- wants an Others. Note that we do not bother to call Analyze | |
10656 | -- on the modified variant part, since its only effect would be | |
10657 | -- to compute the Others_Discrete_Choices node laboriously, and | |
10658 | -- of course we already know the list of choices corresponding | |
39a0c1d3 | 10659 | -- to the others choice (it's the list we're replacing). |
d6f39728 | 10660 | |
d9f6a4ee | 10661 | -- We only want to do this if the expander is active, since |
39a0c1d3 | 10662 | -- we do not want to clobber the ASIS tree. |
d6f39728 | 10663 | |
d9f6a4ee | 10664 | if Expander_Active then |
10665 | declare | |
10666 | Last_Var : constant Node_Id := | |
10667 | Last_Non_Pragma (Variants (VP)); | |
d6f39728 | 10668 | |
d9f6a4ee | 10669 | Others_Node : Node_Id; |
d6f39728 | 10670 | |
d9f6a4ee | 10671 | begin |
10672 | if Nkind (First (Discrete_Choices (Last_Var))) /= | |
10673 | N_Others_Choice | |
10674 | then | |
10675 | Others_Node := Make_Others_Choice (Sloc (Last_Var)); | |
10676 | Set_Others_Discrete_Choices | |
10677 | (Others_Node, Discrete_Choices (Last_Var)); | |
10678 | Set_Discrete_Choices | |
10679 | (Last_Var, New_List (Others_Node)); | |
10680 | end if; | |
10681 | end; | |
10682 | end if; | |
d6f39728 | 10683 | end if; |
d9f6a4ee | 10684 | end Check_Variant_Part; |
d6f39728 | 10685 | end if; |
d9f6a4ee | 10686 | end Freeze_Entity_Checks; |
d6f39728 | 10687 | |
10688 | ------------------------- | |
10689 | -- Get_Alignment_Value -- | |
10690 | ------------------------- | |
10691 | ||
10692 | function Get_Alignment_Value (Expr : Node_Id) return Uint is | |
10693 | Align : constant Uint := Static_Integer (Expr); | |
10694 | ||
10695 | begin | |
10696 | if Align = No_Uint then | |
10697 | return No_Uint; | |
10698 | ||
10699 | elsif Align <= 0 then | |
10700 | Error_Msg_N ("alignment value must be positive", Expr); | |
10701 | return No_Uint; | |
10702 | ||
10703 | else | |
10704 | for J in Int range 0 .. 64 loop | |
10705 | declare | |
10706 | M : constant Uint := Uint_2 ** J; | |
10707 | ||
10708 | begin | |
10709 | exit when M = Align; | |
10710 | ||
10711 | if M > Align then | |
10712 | Error_Msg_N | |
10713 | ("alignment value must be power of 2", Expr); | |
10714 | return No_Uint; | |
10715 | end if; | |
10716 | end; | |
10717 | end loop; | |
10718 | ||
10719 | return Align; | |
10720 | end if; | |
10721 | end Get_Alignment_Value; | |
10722 | ||
99a2d5bd | 10723 | ------------------------------------- |
10724 | -- Inherit_Aspects_At_Freeze_Point -- | |
10725 | ------------------------------------- | |
10726 | ||
10727 | procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is | |
10728 | function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10729 | (Rep_Item : Node_Id) return Boolean; | |
10730 | -- This routine checks if Rep_Item is either a pragma or an aspect | |
10731 | -- specification node whose correponding pragma (if any) is present in | |
10732 | -- the Rep Item chain of the entity it has been specified to. | |
10733 | ||
10734 | -------------------------------------------------- | |
10735 | -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item -- | |
10736 | -------------------------------------------------- | |
10737 | ||
10738 | function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10739 | (Rep_Item : Node_Id) return Boolean | |
10740 | is | |
10741 | begin | |
ec6f6da5 | 10742 | return |
10743 | Nkind (Rep_Item) = N_Pragma | |
10744 | or else Present_In_Rep_Item | |
10745 | (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item)); | |
99a2d5bd | 10746 | end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item; |
10747 | ||
29a9d4be | 10748 | -- Start of processing for Inherit_Aspects_At_Freeze_Point |
10749 | ||
99a2d5bd | 10750 | begin |
10751 | -- A representation item is either subtype-specific (Size and Alignment | |
10752 | -- clauses) or type-related (all others). Subtype-specific aspects may | |
29a9d4be | 10753 | -- differ for different subtypes of the same type (RM 13.1.8). |
99a2d5bd | 10754 | |
10755 | -- A derived type inherits each type-related representation aspect of | |
10756 | -- its parent type that was directly specified before the declaration of | |
29a9d4be | 10757 | -- the derived type (RM 13.1.15). |
99a2d5bd | 10758 | |
10759 | -- A derived subtype inherits each subtype-specific representation | |
10760 | -- aspect of its parent subtype that was directly specified before the | |
29a9d4be | 10761 | -- declaration of the derived type (RM 13.1.15). |
99a2d5bd | 10762 | |
10763 | -- The general processing involves inheriting a representation aspect | |
10764 | -- from a parent type whenever the first rep item (aspect specification, | |
10765 | -- attribute definition clause, pragma) corresponding to the given | |
10766 | -- representation aspect in the rep item chain of Typ, if any, isn't | |
10767 | -- directly specified to Typ but to one of its parents. | |
10768 | ||
10769 | -- ??? Note that, for now, just a limited number of representation | |
29a9d4be | 10770 | -- aspects have been inherited here so far. Many of them are |
10771 | -- still inherited in Sem_Ch3. This will be fixed soon. Here is | |
10772 | -- a non- exhaustive list of aspects that likely also need to | |
10773 | -- be moved to this routine: Alignment, Component_Alignment, | |
10774 | -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates, | |
99a2d5bd | 10775 | -- Preelaborable_Initialization, RM_Size and Small. |
10776 | ||
8b6e9bf2 | 10777 | -- In addition, Convention must be propagated from base type to subtype, |
10778 | -- because the subtype may have been declared on an incomplete view. | |
10779 | ||
99a2d5bd | 10780 | if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then |
10781 | return; | |
10782 | end if; | |
10783 | ||
10784 | -- Ada_05/Ada_2005 | |
10785 | ||
10786 | if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False) | |
10787 | and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005) | |
10788 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10789 | (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)) | |
10790 | then | |
10791 | Set_Is_Ada_2005_Only (Typ); | |
10792 | end if; | |
10793 | ||
10794 | -- Ada_12/Ada_2012 | |
10795 | ||
10796 | if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False) | |
10797 | and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012) | |
10798 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10799 | (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)) | |
10800 | then | |
10801 | Set_Is_Ada_2012_Only (Typ); | |
10802 | end if; | |
10803 | ||
10804 | -- Atomic/Shared | |
10805 | ||
10806 | if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False) | |
10807 | and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared) | |
10808 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10809 | (Get_Rep_Item (Typ, Name_Atomic, Name_Shared)) | |
10810 | then | |
10811 | Set_Is_Atomic (Typ); | |
10812 | Set_Treat_As_Volatile (Typ); | |
10813 | Set_Is_Volatile (Typ); | |
10814 | end if; | |
10815 | ||
8b6e9bf2 | 10816 | -- Convention |
10817 | ||
7ac4254e | 10818 | if Is_Record_Type (Typ) |
10819 | and then Typ /= Base_Type (Typ) and then Is_Frozen (Base_Type (Typ)) | |
10820 | then | |
8b6e9bf2 | 10821 | Set_Convention (Typ, Convention (Base_Type (Typ))); |
10822 | end if; | |
10823 | ||
29a9d4be | 10824 | -- Default_Component_Value |
99a2d5bd | 10825 | |
10826 | if Is_Array_Type (Typ) | |
f3d70f08 | 10827 | and then Is_Base_Type (Typ) |
99a2d5bd | 10828 | and then Has_Rep_Item (Typ, Name_Default_Component_Value, False) |
10829 | and then Has_Rep_Item (Typ, Name_Default_Component_Value) | |
10830 | then | |
10831 | Set_Default_Aspect_Component_Value (Typ, | |
10832 | Default_Aspect_Component_Value | |
10833 | (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value)))); | |
10834 | end if; | |
10835 | ||
29a9d4be | 10836 | -- Default_Value |
99a2d5bd | 10837 | |
10838 | if Is_Scalar_Type (Typ) | |
f3d70f08 | 10839 | and then Is_Base_Type (Typ) |
99a2d5bd | 10840 | and then Has_Rep_Item (Typ, Name_Default_Value, False) |
10841 | and then Has_Rep_Item (Typ, Name_Default_Value) | |
10842 | then | |
10843 | Set_Default_Aspect_Value (Typ, | |
10844 | Default_Aspect_Value | |
10845 | (Entity (Get_Rep_Item (Typ, Name_Default_Value)))); | |
10846 | end if; | |
10847 | ||
10848 | -- Discard_Names | |
10849 | ||
10850 | if not Has_Rep_Item (Typ, Name_Discard_Names, False) | |
10851 | and then Has_Rep_Item (Typ, Name_Discard_Names) | |
10852 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10853 | (Get_Rep_Item (Typ, Name_Discard_Names)) | |
10854 | then | |
10855 | Set_Discard_Names (Typ); | |
10856 | end if; | |
10857 | ||
10858 | -- Invariants | |
10859 | ||
10860 | if not Has_Rep_Item (Typ, Name_Invariant, False) | |
10861 | and then Has_Rep_Item (Typ, Name_Invariant) | |
10862 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10863 | (Get_Rep_Item (Typ, Name_Invariant)) | |
10864 | then | |
10865 | Set_Has_Invariants (Typ); | |
10866 | ||
10867 | if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then | |
10868 | Set_Has_Inheritable_Invariants (Typ); | |
10869 | end if; | |
d7487d7d | 10870 | |
953378ae | 10871 | -- If we have a subtype with invariants, whose base type does not have |
10872 | -- invariants, copy these invariants to the base type. This happens for | |
10873 | -- the case of implicit base types created for scalar and array types. | |
d7487d7d | 10874 | |
953378ae | 10875 | elsif Has_Invariants (Typ) |
d7487d7d | 10876 | and then not Has_Invariants (Base_Type (Typ)) |
10877 | then | |
10878 | Set_Has_Invariants (Base_Type (Typ)); | |
10879 | Set_Invariant_Procedure (Base_Type (Typ), Invariant_Procedure (Typ)); | |
99a2d5bd | 10880 | end if; |
10881 | ||
10882 | -- Volatile | |
10883 | ||
10884 | if not Has_Rep_Item (Typ, Name_Volatile, False) | |
10885 | and then Has_Rep_Item (Typ, Name_Volatile) | |
10886 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10887 | (Get_Rep_Item (Typ, Name_Volatile)) | |
10888 | then | |
10889 | Set_Treat_As_Volatile (Typ); | |
10890 | Set_Is_Volatile (Typ); | |
10891 | end if; | |
10892 | ||
10893 | -- Inheritance for derived types only | |
10894 | ||
10895 | if Is_Derived_Type (Typ) then | |
10896 | declare | |
10897 | Bas_Typ : constant Entity_Id := Base_Type (Typ); | |
10898 | Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ); | |
10899 | ||
10900 | begin | |
10901 | -- Atomic_Components | |
10902 | ||
10903 | if not Has_Rep_Item (Typ, Name_Atomic_Components, False) | |
10904 | and then Has_Rep_Item (Typ, Name_Atomic_Components) | |
10905 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10906 | (Get_Rep_Item (Typ, Name_Atomic_Components)) | |
10907 | then | |
10908 | Set_Has_Atomic_Components (Imp_Bas_Typ); | |
10909 | end if; | |
10910 | ||
10911 | -- Volatile_Components | |
10912 | ||
10913 | if not Has_Rep_Item (Typ, Name_Volatile_Components, False) | |
10914 | and then Has_Rep_Item (Typ, Name_Volatile_Components) | |
10915 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10916 | (Get_Rep_Item (Typ, Name_Volatile_Components)) | |
10917 | then | |
10918 | Set_Has_Volatile_Components (Imp_Bas_Typ); | |
10919 | end if; | |
10920 | ||
e81df51c | 10921 | -- Finalize_Storage_Only |
99a2d5bd | 10922 | |
10923 | if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False) | |
10924 | and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only) | |
10925 | then | |
10926 | Set_Finalize_Storage_Only (Bas_Typ); | |
10927 | end if; | |
10928 | ||
10929 | -- Universal_Aliasing | |
10930 | ||
10931 | if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False) | |
10932 | and then Has_Rep_Item (Typ, Name_Universal_Aliasing) | |
10933 | and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item | |
10934 | (Get_Rep_Item (Typ, Name_Universal_Aliasing)) | |
10935 | then | |
10936 | Set_Universal_Aliasing (Imp_Bas_Typ); | |
10937 | end if; | |
10938 | ||
e81df51c | 10939 | -- Bit_Order |
99a2d5bd | 10940 | |
10941 | if Is_Record_Type (Typ) then | |
99a2d5bd | 10942 | if not Has_Rep_Item (Typ, Name_Bit_Order, False) |
10943 | and then Has_Rep_Item (Typ, Name_Bit_Order) | |
10944 | then | |
10945 | Set_Reverse_Bit_Order (Bas_Typ, | |
10946 | Reverse_Bit_Order (Entity (Name | |
10947 | (Get_Rep_Item (Typ, Name_Bit_Order))))); | |
10948 | end if; | |
e81df51c | 10949 | end if; |
10950 | ||
e9218716 | 10951 | -- Scalar_Storage_Order |
10952 | ||
10953 | -- Note: the aspect is specified on a first subtype, but recorded | |
10954 | -- in a flag of the base type! | |
e81df51c | 10955 | |
10956 | if (Is_Record_Type (Typ) or else Is_Array_Type (Typ)) | |
10957 | and then | |
e9218716 | 10958 | Typ = Bas_Typ |
e81df51c | 10959 | then |
99a2d5bd | 10960 | |
e81df51c | 10961 | -- For a type extension, always inherit from parent; otherwise |
10962 | -- inherit if no default applies. Note: we do not check for | |
10963 | -- an explicit rep item on the parent type when inheriting, | |
10964 | -- because the parent SSO may itself have been set by default. | |
99a2d5bd | 10965 | |
e9218716 | 10966 | if not Has_Rep_Item (First_Subtype (Typ), |
10967 | Name_Scalar_Storage_Order, False) | |
e81df51c | 10968 | and then (Is_Tagged_Type (Bas_Typ) |
10969 | or else | |
10970 | not (SSO_Set_Low_By_Default (Bas_Typ) | |
10971 | or else | |
10972 | SSO_Set_High_By_Default (Bas_Typ))) | |
99a2d5bd | 10973 | then |
10974 | Set_Reverse_Storage_Order (Bas_Typ, | |
423b89fd | 10975 | Reverse_Storage_Order |
10976 | (Implementation_Base_Type (Etype (Bas_Typ)))); | |
b64082f2 | 10977 | |
10978 | -- Clear default SSO indications, since the inherited aspect | |
10979 | -- which was set explicitly overrides the default. | |
10980 | ||
10981 | Set_SSO_Set_Low_By_Default (Bas_Typ, False); | |
10982 | Set_SSO_Set_High_By_Default (Bas_Typ, False); | |
99a2d5bd | 10983 | end if; |
10984 | end if; | |
10985 | end; | |
10986 | end if; | |
10987 | end Inherit_Aspects_At_Freeze_Point; | |
10988 | ||
d6f39728 | 10989 | ---------------- |
10990 | -- Initialize -- | |
10991 | ---------------- | |
10992 | ||
10993 | procedure Initialize is | |
10994 | begin | |
7717ea00 | 10995 | Address_Clause_Checks.Init; |
10996 | Independence_Checks.Init; | |
d6f39728 | 10997 | Unchecked_Conversions.Init; |
10998 | end Initialize; | |
10999 | ||
2625eb01 | 11000 | --------------------------- |
11001 | -- Install_Discriminants -- | |
11002 | --------------------------- | |
11003 | ||
11004 | procedure Install_Discriminants (E : Entity_Id) is | |
11005 | Disc : Entity_Id; | |
11006 | Prev : Entity_Id; | |
11007 | begin | |
11008 | Disc := First_Discriminant (E); | |
11009 | while Present (Disc) loop | |
11010 | Prev := Current_Entity (Disc); | |
11011 | Set_Current_Entity (Disc); | |
11012 | Set_Is_Immediately_Visible (Disc); | |
11013 | Set_Homonym (Disc, Prev); | |
11014 | Next_Discriminant (Disc); | |
11015 | end loop; | |
11016 | end Install_Discriminants; | |
11017 | ||
d6f39728 | 11018 | ------------------------- |
11019 | -- Is_Operational_Item -- | |
11020 | ------------------------- | |
11021 | ||
11022 | function Is_Operational_Item (N : Node_Id) return Boolean is | |
11023 | begin | |
11024 | if Nkind (N) /= N_Attribute_Definition_Clause then | |
11025 | return False; | |
b9e61b2a | 11026 | |
d6f39728 | 11027 | else |
11028 | declare | |
b9e61b2a | 11029 | Id : constant Attribute_Id := Get_Attribute_Id (Chars (N)); |
d6f39728 | 11030 | begin |
b9e61b2a | 11031 | return Id = Attribute_Input |
d6f39728 | 11032 | or else Id = Attribute_Output |
11033 | or else Id = Attribute_Read | |
f15731c4 | 11034 | or else Id = Attribute_Write |
11035 | or else Id = Attribute_External_Tag; | |
d6f39728 | 11036 | end; |
11037 | end if; | |
11038 | end Is_Operational_Item; | |
11039 | ||
3b23aaa0 | 11040 | ------------------------- |
11041 | -- Is_Predicate_Static -- | |
11042 | ------------------------- | |
11043 | ||
94d896aa | 11044 | -- Note: the basic legality of the expression has already been checked, so |
11045 | -- we don't need to worry about cases or ranges on strings for example. | |
11046 | ||
3b23aaa0 | 11047 | function Is_Predicate_Static |
11048 | (Expr : Node_Id; | |
11049 | Nam : Name_Id) return Boolean | |
11050 | is | |
11051 | function All_Static_Case_Alternatives (L : List_Id) return Boolean; | |
973c2fba | 11052 | -- Given a list of case expression alternatives, returns True if all |
11053 | -- the alternatives are static (have all static choices, and a static | |
11054 | -- expression). | |
3b23aaa0 | 11055 | |
11056 | function All_Static_Choices (L : List_Id) return Boolean; | |
a360a0f7 | 11057 | -- Returns true if all elements of the list are OK static choices |
3b23aaa0 | 11058 | -- as defined below for Is_Static_Choice. Used for case expression |
973c2fba | 11059 | -- alternatives and for the right operand of a membership test. An |
11060 | -- others_choice is static if the corresponding expression is static. | |
7c0c95b8 | 11061 | -- The staticness of the bounds is checked separately. |
3b23aaa0 | 11062 | |
11063 | function Is_Static_Choice (N : Node_Id) return Boolean; | |
11064 | -- Returns True if N represents a static choice (static subtype, or | |
a360a0f7 | 11065 | -- static subtype indication, or static expression, or static range). |
3b23aaa0 | 11066 | -- |
11067 | -- Note that this is a bit more inclusive than we actually need | |
11068 | -- (in particular membership tests do not allow the use of subtype | |
a360a0f7 | 11069 | -- indications). But that doesn't matter, we have already checked |
3b23aaa0 | 11070 | -- that the construct is legal to get this far. |
11071 | ||
11072 | function Is_Type_Ref (N : Node_Id) return Boolean; | |
11073 | pragma Inline (Is_Type_Ref); | |
973c2fba | 11074 | -- Returns True if N is a reference to the type for the predicate in the |
11075 | -- expression (i.e. if it is an identifier whose Chars field matches the | |
11076 | -- Nam given in the call). N must not be parenthesized, if the type name | |
11077 | -- appears in parens, this routine will return False. | |
3b23aaa0 | 11078 | |
11079 | ---------------------------------- | |
11080 | -- All_Static_Case_Alternatives -- | |
11081 | ---------------------------------- | |
11082 | ||
11083 | function All_Static_Case_Alternatives (L : List_Id) return Boolean is | |
11084 | N : Node_Id; | |
11085 | ||
11086 | begin | |
11087 | N := First (L); | |
11088 | while Present (N) loop | |
11089 | if not (All_Static_Choices (Discrete_Choices (N)) | |
11090 | and then Is_OK_Static_Expression (Expression (N))) | |
11091 | then | |
11092 | return False; | |
11093 | end if; | |
11094 | ||
11095 | Next (N); | |
11096 | end loop; | |
11097 | ||
11098 | return True; | |
11099 | end All_Static_Case_Alternatives; | |
11100 | ||
11101 | ------------------------ | |
11102 | -- All_Static_Choices -- | |
11103 | ------------------------ | |
11104 | ||
11105 | function All_Static_Choices (L : List_Id) return Boolean is | |
11106 | N : Node_Id; | |
11107 | ||
11108 | begin | |
11109 | N := First (L); | |
11110 | while Present (N) loop | |
11111 | if not Is_Static_Choice (N) then | |
11112 | return False; | |
11113 | end if; | |
11114 | ||
11115 | Next (N); | |
11116 | end loop; | |
11117 | ||
11118 | return True; | |
11119 | end All_Static_Choices; | |
11120 | ||
11121 | ---------------------- | |
11122 | -- Is_Static_Choice -- | |
11123 | ---------------------- | |
11124 | ||
11125 | function Is_Static_Choice (N : Node_Id) return Boolean is | |
11126 | begin | |
7c0c95b8 | 11127 | return Nkind (N) = N_Others_Choice |
11128 | or else Is_OK_Static_Expression (N) | |
3b23aaa0 | 11129 | or else (Is_Entity_Name (N) and then Is_Type (Entity (N)) |
11130 | and then Is_OK_Static_Subtype (Entity (N))) | |
11131 | or else (Nkind (N) = N_Subtype_Indication | |
11132 | and then Is_OK_Static_Subtype (Entity (N))) | |
11133 | or else (Nkind (N) = N_Range and then Is_OK_Static_Range (N)); | |
11134 | end Is_Static_Choice; | |
11135 | ||
11136 | ----------------- | |
11137 | -- Is_Type_Ref -- | |
11138 | ----------------- | |
11139 | ||
11140 | function Is_Type_Ref (N : Node_Id) return Boolean is | |
11141 | begin | |
11142 | return Nkind (N) = N_Identifier | |
11143 | and then Chars (N) = Nam | |
11144 | and then Paren_Count (N) = 0; | |
11145 | end Is_Type_Ref; | |
11146 | ||
11147 | -- Start of processing for Is_Predicate_Static | |
11148 | ||
11149 | begin | |
3b23aaa0 | 11150 | -- Predicate_Static means one of the following holds. Numbers are the |
11151 | -- corresponding paragraph numbers in (RM 3.2.4(16-22)). | |
11152 | ||
11153 | -- 16: A static expression | |
11154 | ||
11155 | if Is_OK_Static_Expression (Expr) then | |
11156 | return True; | |
11157 | ||
11158 | -- 17: A membership test whose simple_expression is the current | |
11159 | -- instance, and whose membership_choice_list meets the requirements | |
11160 | -- for a static membership test. | |
11161 | ||
11162 | elsif Nkind (Expr) in N_Membership_Test | |
11163 | and then ((Present (Right_Opnd (Expr)) | |
11164 | and then Is_Static_Choice (Right_Opnd (Expr))) | |
11165 | or else | |
11166 | (Present (Alternatives (Expr)) | |
11167 | and then All_Static_Choices (Alternatives (Expr)))) | |
11168 | then | |
11169 | return True; | |
11170 | ||
11171 | -- 18. A case_expression whose selecting_expression is the current | |
11172 | -- instance, and whose dependent expressions are static expressions. | |
11173 | ||
11174 | elsif Nkind (Expr) = N_Case_Expression | |
11175 | and then Is_Type_Ref (Expression (Expr)) | |
11176 | and then All_Static_Case_Alternatives (Alternatives (Expr)) | |
11177 | then | |
11178 | return True; | |
11179 | ||
11180 | -- 19. A call to a predefined equality or ordering operator, where one | |
11181 | -- operand is the current instance, and the other is a static | |
11182 | -- expression. | |
11183 | ||
94d896aa | 11184 | -- Note: the RM is clearly wrong here in not excluding string types. |
11185 | -- Without this exclusion, we would allow expressions like X > "ABC" | |
11186 | -- to be considered as predicate-static, which is clearly not intended, | |
11187 | -- since the idea is for predicate-static to be a subset of normal | |
11188 | -- static expressions (and "DEF" > "ABC" is not a static expression). | |
11189 | ||
11190 | -- However, we do allow internally generated (not from source) equality | |
11191 | -- and inequality operations to be valid on strings (this helps deal | |
11192 | -- with cases where we transform A in "ABC" to A = "ABC). | |
11193 | ||
3b23aaa0 | 11194 | elsif Nkind (Expr) in N_Op_Compare |
94d896aa | 11195 | and then ((not Is_String_Type (Etype (Left_Opnd (Expr)))) |
11196 | or else (Nkind_In (Expr, N_Op_Eq, N_Op_Ne) | |
11197 | and then not Comes_From_Source (Expr))) | |
3b23aaa0 | 11198 | and then ((Is_Type_Ref (Left_Opnd (Expr)) |
11199 | and then Is_OK_Static_Expression (Right_Opnd (Expr))) | |
11200 | or else | |
11201 | (Is_Type_Ref (Right_Opnd (Expr)) | |
11202 | and then Is_OK_Static_Expression (Left_Opnd (Expr)))) | |
11203 | then | |
11204 | return True; | |
11205 | ||
11206 | -- 20. A call to a predefined boolean logical operator, where each | |
11207 | -- operand is predicate-static. | |
11208 | ||
11209 | elsif (Nkind_In (Expr, N_Op_And, N_Op_Or, N_Op_Xor) | |
11210 | and then Is_Predicate_Static (Left_Opnd (Expr), Nam) | |
11211 | and then Is_Predicate_Static (Right_Opnd (Expr), Nam)) | |
11212 | or else | |
11213 | (Nkind (Expr) = N_Op_Not | |
11214 | and then Is_Predicate_Static (Right_Opnd (Expr), Nam)) | |
11215 | then | |
11216 | return True; | |
11217 | ||
11218 | -- 21. A short-circuit control form where both operands are | |
11219 | -- predicate-static. | |
11220 | ||
11221 | elsif Nkind (Expr) in N_Short_Circuit | |
11222 | and then Is_Predicate_Static (Left_Opnd (Expr), Nam) | |
11223 | and then Is_Predicate_Static (Right_Opnd (Expr), Nam) | |
11224 | then | |
11225 | return True; | |
11226 | ||
11227 | -- 22. A parenthesized predicate-static expression. This does not | |
11228 | -- require any special test, since we just ignore paren levels in | |
11229 | -- all the cases above. | |
11230 | ||
11231 | -- One more test that is an implementation artifact caused by the fact | |
499918a7 | 11232 | -- that we are analyzing not the original expression, but the generated |
3b23aaa0 | 11233 | -- expression in the body of the predicate function. This can include |
a360a0f7 | 11234 | -- references to inherited predicates, so that the expression we are |
3b23aaa0 | 11235 | -- processing looks like: |
11236 | ||
11237 | -- expression and then xxPredicate (typ (Inns)) | |
11238 | ||
11239 | -- Where the call is to a Predicate function for an inherited predicate. | |
11240 | -- We simply ignore such a call (which could be to either a dynamic or | |
499918a7 | 11241 | -- a static predicate, but remember that we can have a Static_Predicate |
3b23aaa0 | 11242 | -- for a non-static subtype). |
11243 | ||
11244 | elsif Nkind (Expr) = N_Function_Call | |
11245 | and then Is_Predicate_Function (Entity (Name (Expr))) | |
11246 | then | |
11247 | return True; | |
11248 | ||
11249 | -- That's an exhaustive list of tests, all other cases are not | |
a360a0f7 | 11250 | -- predicate-static, so we return False. |
3b23aaa0 | 11251 | |
11252 | else | |
11253 | return False; | |
11254 | end if; | |
11255 | end Is_Predicate_Static; | |
11256 | ||
2ff55065 | 11257 | --------------------- |
11258 | -- Kill_Rep_Clause -- | |
11259 | --------------------- | |
11260 | ||
11261 | procedure Kill_Rep_Clause (N : Node_Id) is | |
11262 | begin | |
11263 | pragma Assert (Ignore_Rep_Clauses); | |
360f426f | 11264 | |
11265 | -- Note: we use Replace rather than Rewrite, because we don't want | |
11266 | -- ASIS to be able to use Original_Node to dig out the (undecorated) | |
11267 | -- rep clause that is being replaced. | |
11268 | ||
4949ddd5 | 11269 | Replace (N, Make_Null_Statement (Sloc (N))); |
360f426f | 11270 | |
11271 | -- The null statement must be marked as not coming from source. This is | |
37c6552c | 11272 | -- so that ASIS ignores it, and also the back end does not expect bogus |
360f426f | 11273 | -- "from source" null statements in weird places (e.g. in declarative |
11274 | -- regions where such null statements are not allowed). | |
11275 | ||
11276 | Set_Comes_From_Source (N, False); | |
2ff55065 | 11277 | end Kill_Rep_Clause; |
11278 | ||
d6f39728 | 11279 | ------------------ |
11280 | -- Minimum_Size -- | |
11281 | ------------------ | |
11282 | ||
11283 | function Minimum_Size | |
11284 | (T : Entity_Id; | |
d5b349fa | 11285 | Biased : Boolean := False) return Nat |
d6f39728 | 11286 | is |
11287 | Lo : Uint := No_Uint; | |
11288 | Hi : Uint := No_Uint; | |
11289 | LoR : Ureal := No_Ureal; | |
11290 | HiR : Ureal := No_Ureal; | |
11291 | LoSet : Boolean := False; | |
11292 | HiSet : Boolean := False; | |
11293 | B : Uint; | |
11294 | S : Nat; | |
11295 | Ancest : Entity_Id; | |
f15731c4 | 11296 | R_Typ : constant Entity_Id := Root_Type (T); |
d6f39728 | 11297 | |
11298 | begin | |
11299 | -- If bad type, return 0 | |
11300 | ||
11301 | if T = Any_Type then | |
11302 | return 0; | |
11303 | ||
11304 | -- For generic types, just return zero. There cannot be any legitimate | |
11305 | -- need to know such a size, but this routine may be called with a | |
11306 | -- generic type as part of normal processing. | |
11307 | ||
f02a9a9a | 11308 | elsif Is_Generic_Type (R_Typ) or else R_Typ = Any_Type then |
d6f39728 | 11309 | return 0; |
11310 | ||
74c7ae52 | 11311 | -- Access types (cannot have size smaller than System.Address) |
d6f39728 | 11312 | |
11313 | elsif Is_Access_Type (T) then | |
74c7ae52 | 11314 | return System_Address_Size; |
d6f39728 | 11315 | |
11316 | -- Floating-point types | |
11317 | ||
11318 | elsif Is_Floating_Point_Type (T) then | |
f15731c4 | 11319 | return UI_To_Int (Esize (R_Typ)); |
d6f39728 | 11320 | |
11321 | -- Discrete types | |
11322 | ||
11323 | elsif Is_Discrete_Type (T) then | |
11324 | ||
fdd294d1 | 11325 | -- The following loop is looking for the nearest compile time known |
11326 | -- bounds following the ancestor subtype chain. The idea is to find | |
11327 | -- the most restrictive known bounds information. | |
d6f39728 | 11328 | |
11329 | Ancest := T; | |
11330 | loop | |
11331 | if Ancest = Any_Type or else Etype (Ancest) = Any_Type then | |
11332 | return 0; | |
11333 | end if; | |
11334 | ||
11335 | if not LoSet then | |
11336 | if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then | |
11337 | Lo := Expr_Rep_Value (Type_Low_Bound (Ancest)); | |
11338 | LoSet := True; | |
11339 | exit when HiSet; | |
11340 | end if; | |
11341 | end if; | |
11342 | ||
11343 | if not HiSet then | |
11344 | if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then | |
11345 | Hi := Expr_Rep_Value (Type_High_Bound (Ancest)); | |
11346 | HiSet := True; | |
11347 | exit when LoSet; | |
11348 | end if; | |
11349 | end if; | |
11350 | ||
11351 | Ancest := Ancestor_Subtype (Ancest); | |
11352 | ||
11353 | if No (Ancest) then | |
11354 | Ancest := Base_Type (T); | |
11355 | ||
11356 | if Is_Generic_Type (Ancest) then | |
11357 | return 0; | |
11358 | end if; | |
11359 | end if; | |
11360 | end loop; | |
11361 | ||
11362 | -- Fixed-point types. We can't simply use Expr_Value to get the | |
fdd294d1 | 11363 | -- Corresponding_Integer_Value values of the bounds, since these do not |
11364 | -- get set till the type is frozen, and this routine can be called | |
11365 | -- before the type is frozen. Similarly the test for bounds being static | |
11366 | -- needs to include the case where we have unanalyzed real literals for | |
11367 | -- the same reason. | |
d6f39728 | 11368 | |
11369 | elsif Is_Fixed_Point_Type (T) then | |
11370 | ||
fdd294d1 | 11371 | -- The following loop is looking for the nearest compile time known |
11372 | -- bounds following the ancestor subtype chain. The idea is to find | |
11373 | -- the most restrictive known bounds information. | |
d6f39728 | 11374 | |
11375 | Ancest := T; | |
11376 | loop | |
11377 | if Ancest = Any_Type or else Etype (Ancest) = Any_Type then | |
11378 | return 0; | |
11379 | end if; | |
11380 | ||
3062c401 | 11381 | -- Note: In the following two tests for LoSet and HiSet, it may |
11382 | -- seem redundant to test for N_Real_Literal here since normally | |
11383 | -- one would assume that the test for the value being known at | |
11384 | -- compile time includes this case. However, there is a glitch. | |
11385 | -- If the real literal comes from folding a non-static expression, | |
11386 | -- then we don't consider any non- static expression to be known | |
11387 | -- at compile time if we are in configurable run time mode (needed | |
11388 | -- in some cases to give a clearer definition of what is and what | |
11389 | -- is not accepted). So the test is indeed needed. Without it, we | |
11390 | -- would set neither Lo_Set nor Hi_Set and get an infinite loop. | |
11391 | ||
d6f39728 | 11392 | if not LoSet then |
11393 | if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal | |
11394 | or else Compile_Time_Known_Value (Type_Low_Bound (Ancest)) | |
11395 | then | |
11396 | LoR := Expr_Value_R (Type_Low_Bound (Ancest)); | |
11397 | LoSet := True; | |
11398 | exit when HiSet; | |
11399 | end if; | |
11400 | end if; | |
11401 | ||
11402 | if not HiSet then | |
11403 | if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal | |
11404 | or else Compile_Time_Known_Value (Type_High_Bound (Ancest)) | |
11405 | then | |
11406 | HiR := Expr_Value_R (Type_High_Bound (Ancest)); | |
11407 | HiSet := True; | |
11408 | exit when LoSet; | |
11409 | end if; | |
11410 | end if; | |
11411 | ||
11412 | Ancest := Ancestor_Subtype (Ancest); | |
11413 | ||
11414 | if No (Ancest) then | |
11415 | Ancest := Base_Type (T); | |
11416 | ||
11417 | if Is_Generic_Type (Ancest) then | |
11418 | return 0; | |
11419 | end if; | |
11420 | end if; | |
11421 | end loop; | |
11422 | ||
11423 | Lo := UR_To_Uint (LoR / Small_Value (T)); | |
11424 | Hi := UR_To_Uint (HiR / Small_Value (T)); | |
11425 | ||
11426 | -- No other types allowed | |
11427 | ||
11428 | else | |
11429 | raise Program_Error; | |
11430 | end if; | |
11431 | ||
2866d595 | 11432 | -- Fall through with Hi and Lo set. Deal with biased case |
d6f39728 | 11433 | |
cc46ff4b | 11434 | if (Biased |
11435 | and then not Is_Fixed_Point_Type (T) | |
11436 | and then not (Is_Enumeration_Type (T) | |
11437 | and then Has_Non_Standard_Rep (T))) | |
d6f39728 | 11438 | or else Has_Biased_Representation (T) |
11439 | then | |
11440 | Hi := Hi - Lo; | |
11441 | Lo := Uint_0; | |
11442 | end if; | |
11443 | ||
11444 | -- Signed case. Note that we consider types like range 1 .. -1 to be | |
fdd294d1 | 11445 | -- signed for the purpose of computing the size, since the bounds have |
1a34e48c | 11446 | -- to be accommodated in the base type. |
d6f39728 | 11447 | |
11448 | if Lo < 0 or else Hi < 0 then | |
11449 | S := 1; | |
11450 | B := Uint_1; | |
11451 | ||
da253936 | 11452 | -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1)) |
11453 | -- Note that we accommodate the case where the bounds cross. This | |
d6f39728 | 11454 | -- can happen either because of the way the bounds are declared |
11455 | -- or because of the algorithm in Freeze_Fixed_Point_Type. | |
11456 | ||
11457 | while Lo < -B | |
11458 | or else Hi < -B | |
11459 | or else Lo >= B | |
11460 | or else Hi >= B | |
11461 | loop | |
11462 | B := Uint_2 ** S; | |
11463 | S := S + 1; | |
11464 | end loop; | |
11465 | ||
11466 | -- Unsigned case | |
11467 | ||
11468 | else | |
11469 | -- If both bounds are positive, make sure that both are represen- | |
11470 | -- table in the case where the bounds are crossed. This can happen | |
11471 | -- either because of the way the bounds are declared, or because of | |
11472 | -- the algorithm in Freeze_Fixed_Point_Type. | |
11473 | ||
11474 | if Lo > Hi then | |
11475 | Hi := Lo; | |
11476 | end if; | |
11477 | ||
da253936 | 11478 | -- S = size, (can accommodate 0 .. (2**size - 1)) |
d6f39728 | 11479 | |
11480 | S := 0; | |
11481 | while Hi >= Uint_2 ** S loop | |
11482 | S := S + 1; | |
11483 | end loop; | |
11484 | end if; | |
11485 | ||
11486 | return S; | |
11487 | end Minimum_Size; | |
11488 | ||
44e4341e | 11489 | --------------------------- |
11490 | -- New_Stream_Subprogram -- | |
11491 | --------------------------- | |
d6f39728 | 11492 | |
44e4341e | 11493 | procedure New_Stream_Subprogram |
11494 | (N : Node_Id; | |
11495 | Ent : Entity_Id; | |
11496 | Subp : Entity_Id; | |
11497 | Nam : TSS_Name_Type) | |
d6f39728 | 11498 | is |
11499 | Loc : constant Source_Ptr := Sloc (N); | |
9dfe12ae | 11500 | Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam); |
f15731c4 | 11501 | Subp_Id : Entity_Id; |
d6f39728 | 11502 | Subp_Decl : Node_Id; |
11503 | F : Entity_Id; | |
11504 | Etyp : Entity_Id; | |
11505 | ||
44e4341e | 11506 | Defer_Declaration : constant Boolean := |
11507 | Is_Tagged_Type (Ent) or else Is_Private_Type (Ent); | |
11508 | -- For a tagged type, there is a declaration for each stream attribute | |
11509 | -- at the freeze point, and we must generate only a completion of this | |
11510 | -- declaration. We do the same for private types, because the full view | |
11511 | -- might be tagged. Otherwise we generate a declaration at the point of | |
11512 | -- the attribute definition clause. | |
11513 | ||
f15731c4 | 11514 | function Build_Spec return Node_Id; |
11515 | -- Used for declaration and renaming declaration, so that this is | |
11516 | -- treated as a renaming_as_body. | |
11517 | ||
11518 | ---------------- | |
11519 | -- Build_Spec -- | |
11520 | ---------------- | |
11521 | ||
d5b349fa | 11522 | function Build_Spec return Node_Id is |
44e4341e | 11523 | Out_P : constant Boolean := (Nam = TSS_Stream_Read); |
11524 | Formals : List_Id; | |
11525 | Spec : Node_Id; | |
83c6c069 | 11526 | T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc); |
44e4341e | 11527 | |
f15731c4 | 11528 | begin |
9dfe12ae | 11529 | Subp_Id := Make_Defining_Identifier (Loc, Sname); |
f15731c4 | 11530 | |
44e4341e | 11531 | -- S : access Root_Stream_Type'Class |
11532 | ||
11533 | Formals := New_List ( | |
11534 | Make_Parameter_Specification (Loc, | |
11535 | Defining_Identifier => | |
11536 | Make_Defining_Identifier (Loc, Name_S), | |
11537 | Parameter_Type => | |
11538 | Make_Access_Definition (Loc, | |
11539 | Subtype_Mark => | |
83c6c069 | 11540 | New_Occurrence_Of ( |
44e4341e | 11541 | Designated_Type (Etype (F)), Loc)))); |
11542 | ||
11543 | if Nam = TSS_Stream_Input then | |
4bba0a8d | 11544 | Spec := |
11545 | Make_Function_Specification (Loc, | |
11546 | Defining_Unit_Name => Subp_Id, | |
11547 | Parameter_Specifications => Formals, | |
11548 | Result_Definition => T_Ref); | |
44e4341e | 11549 | else |
11550 | -- V : [out] T | |
f15731c4 | 11551 | |
44e4341e | 11552 | Append_To (Formals, |
11553 | Make_Parameter_Specification (Loc, | |
11554 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), | |
11555 | Out_Present => Out_P, | |
11556 | Parameter_Type => T_Ref)); | |
f15731c4 | 11557 | |
d3ef794c | 11558 | Spec := |
11559 | Make_Procedure_Specification (Loc, | |
11560 | Defining_Unit_Name => Subp_Id, | |
11561 | Parameter_Specifications => Formals); | |
44e4341e | 11562 | end if; |
f15731c4 | 11563 | |
44e4341e | 11564 | return Spec; |
11565 | end Build_Spec; | |
d6f39728 | 11566 | |
44e4341e | 11567 | -- Start of processing for New_Stream_Subprogram |
d6f39728 | 11568 | |
44e4341e | 11569 | begin |
11570 | F := First_Formal (Subp); | |
11571 | ||
11572 | if Ekind (Subp) = E_Procedure then | |
11573 | Etyp := Etype (Next_Formal (F)); | |
d6f39728 | 11574 | else |
44e4341e | 11575 | Etyp := Etype (Subp); |
d6f39728 | 11576 | end if; |
f15731c4 | 11577 | |
44e4341e | 11578 | -- Prepare subprogram declaration and insert it as an action on the |
11579 | -- clause node. The visibility for this entity is used to test for | |
11580 | -- visibility of the attribute definition clause (in the sense of | |
11581 | -- 8.3(23) as amended by AI-195). | |
9dfe12ae | 11582 | |
44e4341e | 11583 | if not Defer_Declaration then |
f15731c4 | 11584 | Subp_Decl := |
11585 | Make_Subprogram_Declaration (Loc, | |
11586 | Specification => Build_Spec); | |
44e4341e | 11587 | |
11588 | -- For a tagged type, there is always a visible declaration for each | |
15ebb600 | 11589 | -- stream TSS (it is a predefined primitive operation), and the |
44e4341e | 11590 | -- completion of this declaration occurs at the freeze point, which is |
11591 | -- not always visible at places where the attribute definition clause is | |
11592 | -- visible. So, we create a dummy entity here for the purpose of | |
11593 | -- tracking the visibility of the attribute definition clause itself. | |
11594 | ||
11595 | else | |
11596 | Subp_Id := | |
55868293 | 11597 | Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V')); |
44e4341e | 11598 | Subp_Decl := |
11599 | Make_Object_Declaration (Loc, | |
11600 | Defining_Identifier => Subp_Id, | |
11601 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc)); | |
f15731c4 | 11602 | end if; |
11603 | ||
44e4341e | 11604 | Insert_Action (N, Subp_Decl); |
11605 | Set_Entity (N, Subp_Id); | |
11606 | ||
d6f39728 | 11607 | Subp_Decl := |
11608 | Make_Subprogram_Renaming_Declaration (Loc, | |
f15731c4 | 11609 | Specification => Build_Spec, |
83c6c069 | 11610 | Name => New_Occurrence_Of (Subp, Loc)); |
d6f39728 | 11611 | |
44e4341e | 11612 | if Defer_Declaration then |
d6f39728 | 11613 | Set_TSS (Base_Type (Ent), Subp_Id); |
11614 | else | |
11615 | Insert_Action (N, Subp_Decl); | |
11616 | Copy_TSS (Subp_Id, Base_Type (Ent)); | |
11617 | end if; | |
44e4341e | 11618 | end New_Stream_Subprogram; |
d6f39728 | 11619 | |
2625eb01 | 11620 | ------------------------------------------ |
11621 | -- Push_Scope_And_Install_Discriminants -- | |
11622 | ------------------------------------------ | |
11623 | ||
11624 | procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is | |
11625 | begin | |
11626 | if Has_Discriminants (E) then | |
11627 | Push_Scope (E); | |
11628 | ||
11629 | -- Make discriminants visible for type declarations and protected | |
11630 | -- type declarations, not for subtype declarations (RM 13.1.1 (12/3)) | |
11631 | ||
11632 | if Nkind (Parent (E)) /= N_Subtype_Declaration then | |
11633 | Install_Discriminants (E); | |
11634 | end if; | |
11635 | end if; | |
11636 | end Push_Scope_And_Install_Discriminants; | |
11637 | ||
d6f39728 | 11638 | ------------------------ |
11639 | -- Rep_Item_Too_Early -- | |
11640 | ------------------------ | |
11641 | ||
80d4fec4 | 11642 | function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is |
d6f39728 | 11643 | begin |
44e4341e | 11644 | -- Cannot apply non-operational rep items to generic types |
d6f39728 | 11645 | |
f15731c4 | 11646 | if Is_Operational_Item (N) then |
11647 | return False; | |
11648 | ||
11649 | elsif Is_Type (T) | |
d6f39728 | 11650 | and then Is_Generic_Type (Root_Type (T)) |
11651 | then | |
503f7fd3 | 11652 | Error_Msg_N ("representation item not allowed for generic type", N); |
d6f39728 | 11653 | return True; |
11654 | end if; | |
11655 | ||
fdd294d1 | 11656 | -- Otherwise check for incomplete type |
d6f39728 | 11657 | |
11658 | if Is_Incomplete_Or_Private_Type (T) | |
11659 | and then No (Underlying_Type (T)) | |
d64221a7 | 11660 | and then |
11661 | (Nkind (N) /= N_Pragma | |
60014bc9 | 11662 | or else Get_Pragma_Id (N) /= Pragma_Import) |
d6f39728 | 11663 | then |
11664 | Error_Msg_N | |
11665 | ("representation item must be after full type declaration", N); | |
11666 | return True; | |
11667 | ||
1a34e48c | 11668 | -- If the type has incomplete components, a representation clause is |
d6f39728 | 11669 | -- illegal but stream attributes and Convention pragmas are correct. |
11670 | ||
11671 | elsif Has_Private_Component (T) then | |
f15731c4 | 11672 | if Nkind (N) = N_Pragma then |
d6f39728 | 11673 | return False; |
b9e61b2a | 11674 | |
d6f39728 | 11675 | else |
11676 | Error_Msg_N | |
11677 | ("representation item must appear after type is fully defined", | |
11678 | N); | |
11679 | return True; | |
11680 | end if; | |
11681 | else | |
11682 | return False; | |
11683 | end if; | |
11684 | end Rep_Item_Too_Early; | |
11685 | ||
11686 | ----------------------- | |
11687 | -- Rep_Item_Too_Late -- | |
11688 | ----------------------- | |
11689 | ||
11690 | function Rep_Item_Too_Late | |
11691 | (T : Entity_Id; | |
11692 | N : Node_Id; | |
d5b349fa | 11693 | FOnly : Boolean := False) return Boolean |
d6f39728 | 11694 | is |
11695 | S : Entity_Id; | |
11696 | Parent_Type : Entity_Id; | |
11697 | ||
4d0944e9 | 11698 | procedure No_Type_Rep_Item; |
11699 | -- Output message indicating that no type-related aspects can be | |
11700 | -- specified due to some property of the parent type. | |
11701 | ||
d6f39728 | 11702 | procedure Too_Late; |
4d0944e9 | 11703 | -- Output message for an aspect being specified too late |
11704 | ||
11705 | -- Note that neither of the above errors is considered a serious one, | |
11706 | -- since the effect is simply that we ignore the representation clause | |
11707 | -- in these cases. | |
04d38ee4 | 11708 | -- Is this really true? In any case if we make this change we must |
11709 | -- document the requirement in the spec of Rep_Item_Too_Late that | |
11710 | -- if True is returned, then the rep item must be completely ignored??? | |
4d0944e9 | 11711 | |
11712 | ---------------------- | |
11713 | -- No_Type_Rep_Item -- | |
11714 | ---------------------- | |
11715 | ||
11716 | procedure No_Type_Rep_Item is | |
11717 | begin | |
11718 | Error_Msg_N ("|type-related representation item not permitted!", N); | |
11719 | end No_Type_Rep_Item; | |
d53a018a | 11720 | |
11721 | -------------- | |
11722 | -- Too_Late -- | |
11723 | -------------- | |
d6f39728 | 11724 | |
11725 | procedure Too_Late is | |
11726 | begin | |
ce4da1ed | 11727 | -- Other compilers seem more relaxed about rep items appearing too |
11728 | -- late. Since analysis tools typically don't care about rep items | |
11729 | -- anyway, no reason to be too strict about this. | |
11730 | ||
a9cd517c | 11731 | if not Relaxed_RM_Semantics then |
11732 | Error_Msg_N ("|representation item appears too late!", N); | |
11733 | end if; | |
d6f39728 | 11734 | end Too_Late; |
11735 | ||
11736 | -- Start of processing for Rep_Item_Too_Late | |
11737 | ||
11738 | begin | |
a3248fc4 | 11739 | -- First make sure entity is not frozen (RM 13.1(9)) |
d6f39728 | 11740 | |
11741 | if Is_Frozen (T) | |
a3248fc4 | 11742 | |
11743 | -- Exclude imported types, which may be frozen if they appear in a | |
11744 | -- representation clause for a local type. | |
11745 | ||
4aa270d8 | 11746 | and then not From_Limited_With (T) |
a3248fc4 | 11747 | |
a9cd517c | 11748 | -- Exclude generated entities (not coming from source). The common |
a3248fc4 | 11749 | -- case is when we generate a renaming which prematurely freezes the |
11750 | -- renamed internal entity, but we still want to be able to set copies | |
11751 | -- of attribute values such as Size/Alignment. | |
11752 | ||
11753 | and then Comes_From_Source (T) | |
d6f39728 | 11754 | then |
11755 | Too_Late; | |
11756 | S := First_Subtype (T); | |
11757 | ||
11758 | if Present (Freeze_Node (S)) then | |
04d38ee4 | 11759 | if not Relaxed_RM_Semantics then |
11760 | Error_Msg_NE | |
11761 | ("??no more representation items for }", Freeze_Node (S), S); | |
11762 | end if; | |
d6f39728 | 11763 | end if; |
11764 | ||
11765 | return True; | |
11766 | ||
d1a2e31b | 11767 | -- Check for case of untagged derived type whose parent either has |
4d0944e9 | 11768 | -- primitive operations, or is a by reference type (RM 13.1(10)). In |
11769 | -- this case we do not output a Too_Late message, since there is no | |
11770 | -- earlier point where the rep item could be placed to make it legal. | |
d6f39728 | 11771 | |
11772 | elsif Is_Type (T) | |
11773 | and then not FOnly | |
11774 | and then Is_Derived_Type (T) | |
11775 | and then not Is_Tagged_Type (T) | |
11776 | then | |
11777 | Parent_Type := Etype (Base_Type (T)); | |
11778 | ||
11779 | if Has_Primitive_Operations (Parent_Type) then | |
4d0944e9 | 11780 | No_Type_Rep_Item; |
04d38ee4 | 11781 | |
11782 | if not Relaxed_RM_Semantics then | |
11783 | Error_Msg_NE | |
11784 | ("\parent type & has primitive operations!", N, Parent_Type); | |
11785 | end if; | |
11786 | ||
d6f39728 | 11787 | return True; |
11788 | ||
11789 | elsif Is_By_Reference_Type (Parent_Type) then | |
4d0944e9 | 11790 | No_Type_Rep_Item; |
04d38ee4 | 11791 | |
11792 | if not Relaxed_RM_Semantics then | |
11793 | Error_Msg_NE | |
11794 | ("\parent type & is a by reference type!", N, Parent_Type); | |
11795 | end if; | |
11796 | ||
d6f39728 | 11797 | return True; |
11798 | end if; | |
11799 | end if; | |
11800 | ||
04d38ee4 | 11801 | -- No error, but one more warning to consider. The RM (surprisingly) |
11802 | -- allows this pattern: | |
11803 | ||
11804 | -- type S is ... | |
11805 | -- primitive operations for S | |
11806 | -- type R is new S; | |
11807 | -- rep clause for S | |
11808 | ||
11809 | -- Meaning that calls on the primitive operations of S for values of | |
11810 | -- type R may require possibly expensive implicit conversion operations. | |
11811 | -- This is not an error, but is worth a warning. | |
11812 | ||
11813 | if not Relaxed_RM_Semantics and then Is_Type (T) then | |
11814 | declare | |
11815 | DTL : constant Entity_Id := Derived_Type_Link (Base_Type (T)); | |
11816 | ||
11817 | begin | |
11818 | if Present (DTL) | |
11819 | and then Has_Primitive_Operations (Base_Type (T)) | |
11820 | ||
11821 | -- For now, do not generate this warning for the case of aspect | |
11822 | -- specification using Ada 2012 syntax, since we get wrong | |
11823 | -- messages we do not understand. The whole business of derived | |
11824 | -- types and rep items seems a bit confused when aspects are | |
11825 | -- used, since the aspects are not evaluated till freeze time. | |
11826 | ||
11827 | and then not From_Aspect_Specification (N) | |
11828 | then | |
11829 | Error_Msg_Sloc := Sloc (DTL); | |
11830 | Error_Msg_N | |
11831 | ("representation item for& appears after derived type " | |
11832 | & "declaration#??", N); | |
11833 | Error_Msg_NE | |
11834 | ("\may result in implicit conversions for primitive " | |
11835 | & "operations of&??", N, T); | |
11836 | Error_Msg_NE | |
11837 | ("\to change representations when called with arguments " | |
11838 | & "of type&??", N, DTL); | |
11839 | end if; | |
11840 | end; | |
11841 | end if; | |
11842 | ||
3062c401 | 11843 | -- No error, link item into head of chain of rep items for the entity, |
11844 | -- but avoid chaining if we have an overloadable entity, and the pragma | |
11845 | -- is one that can apply to multiple overloaded entities. | |
11846 | ||
b9e61b2a | 11847 | if Is_Overloadable (T) and then Nkind (N) = N_Pragma then |
fdd294d1 | 11848 | declare |
11849 | Pname : constant Name_Id := Pragma_Name (N); | |
11850 | begin | |
18393965 | 11851 | if Nam_In (Pname, Name_Convention, Name_Import, Name_Export, |
11852 | Name_External, Name_Interface) | |
fdd294d1 | 11853 | then |
11854 | return False; | |
11855 | end if; | |
11856 | end; | |
3062c401 | 11857 | end if; |
11858 | ||
fdd294d1 | 11859 | Record_Rep_Item (T, N); |
d6f39728 | 11860 | return False; |
11861 | end Rep_Item_Too_Late; | |
11862 | ||
2072eaa9 | 11863 | ------------------------------------- |
11864 | -- Replace_Type_References_Generic -- | |
11865 | ------------------------------------- | |
11866 | ||
37c6552c | 11867 | procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id) is |
11868 | TName : constant Name_Id := Chars (T); | |
2072eaa9 | 11869 | |
11870 | function Replace_Node (N : Node_Id) return Traverse_Result; | |
11871 | -- Processes a single node in the traversal procedure below, checking | |
11872 | -- if node N should be replaced, and if so, doing the replacement. | |
11873 | ||
11874 | procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node); | |
11875 | -- This instantiation provides the body of Replace_Type_References | |
11876 | ||
11877 | ------------------ | |
11878 | -- Replace_Node -- | |
11879 | ------------------ | |
11880 | ||
11881 | function Replace_Node (N : Node_Id) return Traverse_Result is | |
11882 | S : Entity_Id; | |
11883 | P : Node_Id; | |
11884 | ||
11885 | begin | |
11886 | -- Case of identifier | |
11887 | ||
11888 | if Nkind (N) = N_Identifier then | |
11889 | ||
37c6552c | 11890 | -- If not the type name, check whether it is a reference to |
11891 | -- some other type, which must be frozen before the predicate | |
11892 | -- function is analyzed, i.e. before the freeze node of the | |
11893 | -- type to which the predicate applies. | |
2072eaa9 | 11894 | |
11895 | if Chars (N) /= TName then | |
37c6552c | 11896 | if Present (Current_Entity (N)) |
11897 | and then Is_Type (Current_Entity (N)) | |
11898 | then | |
11899 | Freeze_Before (Freeze_Node (T), Current_Entity (N)); | |
11900 | end if; | |
11901 | ||
2072eaa9 | 11902 | return Skip; |
11903 | ||
11904 | -- Otherwise do the replacement and we are done with this node | |
11905 | ||
11906 | else | |
11907 | Replace_Type_Reference (N); | |
11908 | return Skip; | |
11909 | end if; | |
11910 | ||
11911 | -- Case of selected component (which is what a qualification | |
11912 | -- looks like in the unanalyzed tree, which is what we have. | |
11913 | ||
11914 | elsif Nkind (N) = N_Selected_Component then | |
11915 | ||
11916 | -- If selector name is not our type, keeping going (we might | |
11917 | -- still have an occurrence of the type in the prefix). | |
11918 | ||
11919 | if Nkind (Selector_Name (N)) /= N_Identifier | |
11920 | or else Chars (Selector_Name (N)) /= TName | |
11921 | then | |
11922 | return OK; | |
11923 | ||
11924 | -- Selector name is our type, check qualification | |
11925 | ||
11926 | else | |
11927 | -- Loop through scopes and prefixes, doing comparison | |
11928 | ||
11929 | S := Current_Scope; | |
11930 | P := Prefix (N); | |
11931 | loop | |
11932 | -- Continue if no more scopes or scope with no name | |
11933 | ||
11934 | if No (S) or else Nkind (S) not in N_Has_Chars then | |
11935 | return OK; | |
11936 | end if; | |
11937 | ||
11938 | -- Do replace if prefix is an identifier matching the | |
11939 | -- scope that we are currently looking at. | |
11940 | ||
11941 | if Nkind (P) = N_Identifier | |
11942 | and then Chars (P) = Chars (S) | |
11943 | then | |
11944 | Replace_Type_Reference (N); | |
11945 | return Skip; | |
11946 | end if; | |
11947 | ||
11948 | -- Go check scope above us if prefix is itself of the | |
11949 | -- form of a selected component, whose selector matches | |
11950 | -- the scope we are currently looking at. | |
11951 | ||
11952 | if Nkind (P) = N_Selected_Component | |
11953 | and then Nkind (Selector_Name (P)) = N_Identifier | |
11954 | and then Chars (Selector_Name (P)) = Chars (S) | |
11955 | then | |
11956 | S := Scope (S); | |
11957 | P := Prefix (P); | |
11958 | ||
11959 | -- For anything else, we don't have a match, so keep on | |
11960 | -- going, there are still some weird cases where we may | |
11961 | -- still have a replacement within the prefix. | |
11962 | ||
11963 | else | |
11964 | return OK; | |
11965 | end if; | |
11966 | end loop; | |
11967 | end if; | |
11968 | ||
ec6f6da5 | 11969 | -- Continue for any other node kind |
2072eaa9 | 11970 | |
11971 | else | |
11972 | return OK; | |
11973 | end if; | |
11974 | end Replace_Node; | |
11975 | ||
11976 | begin | |
11977 | Replace_Type_Refs (N); | |
11978 | end Replace_Type_References_Generic; | |
11979 | ||
d6f39728 | 11980 | ------------------------- |
11981 | -- Same_Representation -- | |
11982 | ------------------------- | |
11983 | ||
11984 | function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is | |
11985 | T1 : constant Entity_Id := Underlying_Type (Typ1); | |
11986 | T2 : constant Entity_Id := Underlying_Type (Typ2); | |
11987 | ||
11988 | begin | |
11989 | -- A quick check, if base types are the same, then we definitely have | |
11990 | -- the same representation, because the subtype specific representation | |
11991 | -- attributes (Size and Alignment) do not affect representation from | |
11992 | -- the point of view of this test. | |
11993 | ||
11994 | if Base_Type (T1) = Base_Type (T2) then | |
11995 | return True; | |
11996 | ||
11997 | elsif Is_Private_Type (Base_Type (T2)) | |
11998 | and then Base_Type (T1) = Full_View (Base_Type (T2)) | |
11999 | then | |
12000 | return True; | |
12001 | end if; | |
12002 | ||
12003 | -- Tagged types never have differing representations | |
12004 | ||
12005 | if Is_Tagged_Type (T1) then | |
12006 | return True; | |
12007 | end if; | |
12008 | ||
12009 | -- Representations are definitely different if conventions differ | |
12010 | ||
12011 | if Convention (T1) /= Convention (T2) then | |
12012 | return False; | |
12013 | end if; | |
12014 | ||
ef0772bc | 12015 | -- Representations are different if component alignments or scalar |
12016 | -- storage orders differ. | |
d6f39728 | 12017 | |
12018 | if (Is_Record_Type (T1) or else Is_Array_Type (T1)) | |
726fd56a | 12019 | and then |
d6f39728 | 12020 | (Is_Record_Type (T2) or else Is_Array_Type (T2)) |
ef0772bc | 12021 | and then |
12022 | (Component_Alignment (T1) /= Component_Alignment (T2) | |
f02a9a9a | 12023 | or else Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2)) |
d6f39728 | 12024 | then |
12025 | return False; | |
12026 | end if; | |
12027 | ||
12028 | -- For arrays, the only real issue is component size. If we know the | |
12029 | -- component size for both arrays, and it is the same, then that's | |
12030 | -- good enough to know we don't have a change of representation. | |
12031 | ||
12032 | if Is_Array_Type (T1) then | |
12033 | if Known_Component_Size (T1) | |
12034 | and then Known_Component_Size (T2) | |
12035 | and then Component_Size (T1) = Component_Size (T2) | |
12036 | then | |
9f1130cc | 12037 | if VM_Target = No_VM then |
12038 | return True; | |
12039 | ||
12040 | -- In VM targets the representation of arrays with aliased | |
12041 | -- components differs from arrays with non-aliased components | |
12042 | ||
12043 | else | |
12044 | return Has_Aliased_Components (Base_Type (T1)) | |
0ba3592b | 12045 | = |
12046 | Has_Aliased_Components (Base_Type (T2)); | |
9f1130cc | 12047 | end if; |
d6f39728 | 12048 | end if; |
12049 | end if; | |
12050 | ||
12051 | -- Types definitely have same representation if neither has non-standard | |
12052 | -- representation since default representations are always consistent. | |
12053 | -- If only one has non-standard representation, and the other does not, | |
12054 | -- then we consider that they do not have the same representation. They | |
12055 | -- might, but there is no way of telling early enough. | |
12056 | ||
12057 | if Has_Non_Standard_Rep (T1) then | |
12058 | if not Has_Non_Standard_Rep (T2) then | |
12059 | return False; | |
12060 | end if; | |
12061 | else | |
12062 | return not Has_Non_Standard_Rep (T2); | |
12063 | end if; | |
12064 | ||
fdd294d1 | 12065 | -- Here the two types both have non-standard representation, and we need |
12066 | -- to determine if they have the same non-standard representation. | |
d6f39728 | 12067 | |
12068 | -- For arrays, we simply need to test if the component sizes are the | |
12069 | -- same. Pragma Pack is reflected in modified component sizes, so this | |
12070 | -- check also deals with pragma Pack. | |
12071 | ||
12072 | if Is_Array_Type (T1) then | |
12073 | return Component_Size (T1) = Component_Size (T2); | |
12074 | ||
12075 | -- Tagged types always have the same representation, because it is not | |
12076 | -- possible to specify different representations for common fields. | |
12077 | ||
12078 | elsif Is_Tagged_Type (T1) then | |
12079 | return True; | |
12080 | ||
12081 | -- Case of record types | |
12082 | ||
12083 | elsif Is_Record_Type (T1) then | |
12084 | ||
12085 | -- Packed status must conform | |
12086 | ||
12087 | if Is_Packed (T1) /= Is_Packed (T2) then | |
12088 | return False; | |
12089 | ||
12090 | -- Otherwise we must check components. Typ2 maybe a constrained | |
12091 | -- subtype with fewer components, so we compare the components | |
12092 | -- of the base types. | |
12093 | ||
12094 | else | |
12095 | Record_Case : declare | |
12096 | CD1, CD2 : Entity_Id; | |
12097 | ||
12098 | function Same_Rep return Boolean; | |
12099 | -- CD1 and CD2 are either components or discriminants. This | |
ef0772bc | 12100 | -- function tests whether they have the same representation. |
d6f39728 | 12101 | |
80d4fec4 | 12102 | -------------- |
12103 | -- Same_Rep -- | |
12104 | -------------- | |
12105 | ||
d6f39728 | 12106 | function Same_Rep return Boolean is |
12107 | begin | |
12108 | if No (Component_Clause (CD1)) then | |
12109 | return No (Component_Clause (CD2)); | |
d6f39728 | 12110 | else |
ef0772bc | 12111 | -- Note: at this point, component clauses have been |
12112 | -- normalized to the default bit order, so that the | |
12113 | -- comparison of Component_Bit_Offsets is meaningful. | |
12114 | ||
d6f39728 | 12115 | return |
12116 | Present (Component_Clause (CD2)) | |
12117 | and then | |
12118 | Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2) | |
12119 | and then | |
12120 | Esize (CD1) = Esize (CD2); | |
12121 | end if; | |
12122 | end Same_Rep; | |
12123 | ||
1e35409d | 12124 | -- Start of processing for Record_Case |
d6f39728 | 12125 | |
12126 | begin | |
12127 | if Has_Discriminants (T1) then | |
d6f39728 | 12128 | |
9dfe12ae | 12129 | -- The number of discriminants may be different if the |
12130 | -- derived type has fewer (constrained by values). The | |
12131 | -- invisible discriminants retain the representation of | |
12132 | -- the original, so the discrepancy does not per se | |
12133 | -- indicate a different representation. | |
12134 | ||
b9e61b2a | 12135 | CD1 := First_Discriminant (T1); |
12136 | CD2 := First_Discriminant (T2); | |
12137 | while Present (CD1) and then Present (CD2) loop | |
d6f39728 | 12138 | if not Same_Rep then |
12139 | return False; | |
12140 | else | |
12141 | Next_Discriminant (CD1); | |
12142 | Next_Discriminant (CD2); | |
12143 | end if; | |
12144 | end loop; | |
12145 | end if; | |
12146 | ||
12147 | CD1 := First_Component (Underlying_Type (Base_Type (T1))); | |
12148 | CD2 := First_Component (Underlying_Type (Base_Type (T2))); | |
d6f39728 | 12149 | while Present (CD1) loop |
12150 | if not Same_Rep then | |
12151 | return False; | |
12152 | else | |
12153 | Next_Component (CD1); | |
12154 | Next_Component (CD2); | |
12155 | end if; | |
12156 | end loop; | |
12157 | ||
12158 | return True; | |
12159 | end Record_Case; | |
12160 | end if; | |
12161 | ||
12162 | -- For enumeration types, we must check each literal to see if the | |
12163 | -- representation is the same. Note that we do not permit enumeration | |
1a34e48c | 12164 | -- representation clauses for Character and Wide_Character, so these |
d6f39728 | 12165 | -- cases were already dealt with. |
12166 | ||
12167 | elsif Is_Enumeration_Type (T1) then | |
d6f39728 | 12168 | Enumeration_Case : declare |
12169 | L1, L2 : Entity_Id; | |
12170 | ||
12171 | begin | |
12172 | L1 := First_Literal (T1); | |
12173 | L2 := First_Literal (T2); | |
d6f39728 | 12174 | while Present (L1) loop |
12175 | if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then | |
12176 | return False; | |
12177 | else | |
12178 | Next_Literal (L1); | |
12179 | Next_Literal (L2); | |
12180 | end if; | |
12181 | end loop; | |
12182 | ||
12183 | return True; | |
d6f39728 | 12184 | end Enumeration_Case; |
12185 | ||
12186 | -- Any other types have the same representation for these purposes | |
12187 | ||
12188 | else | |
12189 | return True; | |
12190 | end if; | |
d6f39728 | 12191 | end Same_Representation; |
12192 | ||
3061ffde | 12193 | -------------------------------- |
12194 | -- Resolve_Iterable_Operation -- | |
12195 | -------------------------------- | |
12196 | ||
12197 | procedure Resolve_Iterable_Operation | |
12198 | (N : Node_Id; | |
12199 | Cursor : Entity_Id; | |
12200 | Typ : Entity_Id; | |
12201 | Nam : Name_Id) | |
12202 | is | |
12203 | Ent : Entity_Id; | |
12204 | F1 : Entity_Id; | |
12205 | F2 : Entity_Id; | |
12206 | ||
12207 | begin | |
12208 | if not Is_Overloaded (N) then | |
12209 | if not Is_Entity_Name (N) | |
12210 | or else Ekind (Entity (N)) /= E_Function | |
12211 | or else Scope (Entity (N)) /= Scope (Typ) | |
12212 | or else No (First_Formal (Entity (N))) | |
12213 | or else Etype (First_Formal (Entity (N))) /= Typ | |
12214 | then | |
12215 | Error_Msg_N ("iterable primitive must be local function name " | |
12216 | & "whose first formal is an iterable type", N); | |
a9f5fea7 | 12217 | return; |
3061ffde | 12218 | end if; |
12219 | ||
12220 | Ent := Entity (N); | |
12221 | F1 := First_Formal (Ent); | |
12222 | if Nam = Name_First then | |
12223 | ||
12224 | -- First (Container) => Cursor | |
12225 | ||
12226 | if Etype (Ent) /= Cursor then | |
12227 | Error_Msg_N ("primitive for First must yield a curosr", N); | |
12228 | end if; | |
12229 | ||
12230 | elsif Nam = Name_Next then | |
12231 | ||
12232 | -- Next (Container, Cursor) => Cursor | |
12233 | ||
12234 | F2 := Next_Formal (F1); | |
12235 | ||
12236 | if Etype (F2) /= Cursor | |
12237 | or else Etype (Ent) /= Cursor | |
12238 | or else Present (Next_Formal (F2)) | |
12239 | then | |
12240 | Error_Msg_N ("no match for Next iterable primitive", N); | |
12241 | end if; | |
12242 | ||
12243 | elsif Nam = Name_Has_Element then | |
12244 | ||
12245 | -- Has_Element (Container, Cursor) => Boolean | |
12246 | ||
12247 | F2 := Next_Formal (F1); | |
12248 | if Etype (F2) /= Cursor | |
12249 | or else Etype (Ent) /= Standard_Boolean | |
12250 | or else Present (Next_Formal (F2)) | |
12251 | then | |
12252 | Error_Msg_N ("no match for Has_Element iterable primitive", N); | |
12253 | end if; | |
12254 | ||
12255 | elsif Nam = Name_Element then | |
b9b03799 | 12256 | F2 := Next_Formal (F1); |
12257 | ||
12258 | if No (F2) | |
12259 | or else Etype (F2) /= Cursor | |
12260 | or else Present (Next_Formal (F2)) | |
12261 | then | |
12262 | Error_Msg_N ("no match for Element iterable primitive", N); | |
12263 | end if; | |
3061ffde | 12264 | null; |
12265 | ||
12266 | else | |
12267 | raise Program_Error; | |
12268 | end if; | |
12269 | ||
12270 | else | |
12271 | -- Overloaded case: find subprogram with proper signature. | |
12272 | -- Caller will report error if no match is found. | |
12273 | ||
12274 | declare | |
12275 | I : Interp_Index; | |
12276 | It : Interp; | |
12277 | ||
12278 | begin | |
12279 | Get_First_Interp (N, I, It); | |
12280 | while Present (It.Typ) loop | |
12281 | if Ekind (It.Nam) = E_Function | |
b9b03799 | 12282 | and then Scope (It.Nam) = Scope (Typ) |
3061ffde | 12283 | and then Etype (First_Formal (It.Nam)) = Typ |
12284 | then | |
12285 | F1 := First_Formal (It.Nam); | |
12286 | ||
12287 | if Nam = Name_First then | |
12288 | if Etype (It.Nam) = Cursor | |
12289 | and then No (Next_Formal (F1)) | |
12290 | then | |
12291 | Set_Entity (N, It.Nam); | |
12292 | exit; | |
12293 | end if; | |
12294 | ||
12295 | elsif Nam = Name_Next then | |
12296 | F2 := Next_Formal (F1); | |
12297 | ||
12298 | if Present (F2) | |
12299 | and then No (Next_Formal (F2)) | |
12300 | and then Etype (F2) = Cursor | |
12301 | and then Etype (It.Nam) = Cursor | |
12302 | then | |
12303 | Set_Entity (N, It.Nam); | |
12304 | exit; | |
12305 | end if; | |
12306 | ||
12307 | elsif Nam = Name_Has_Element then | |
12308 | F2 := Next_Formal (F1); | |
12309 | ||
12310 | if Present (F2) | |
12311 | and then No (Next_Formal (F2)) | |
12312 | and then Etype (F2) = Cursor | |
12313 | and then Etype (It.Nam) = Standard_Boolean | |
12314 | then | |
12315 | Set_Entity (N, It.Nam); | |
12316 | F2 := Next_Formal (F1); | |
12317 | exit; | |
12318 | end if; | |
12319 | ||
12320 | elsif Nam = Name_Element then | |
b9b03799 | 12321 | F2 := Next_Formal (F1); |
12322 | ||
3061ffde | 12323 | if Present (F2) |
12324 | and then No (Next_Formal (F2)) | |
12325 | and then Etype (F2) = Cursor | |
12326 | then | |
12327 | Set_Entity (N, It.Nam); | |
12328 | exit; | |
12329 | end if; | |
12330 | end if; | |
12331 | end if; | |
12332 | ||
12333 | Get_Next_Interp (I, It); | |
12334 | end loop; | |
12335 | end; | |
12336 | end if; | |
12337 | end Resolve_Iterable_Operation; | |
12338 | ||
b77e4501 | 12339 | ---------------- |
12340 | -- Set_Biased -- | |
12341 | ---------------- | |
12342 | ||
12343 | procedure Set_Biased | |
12344 | (E : Entity_Id; | |
12345 | N : Node_Id; | |
12346 | Msg : String; | |
12347 | Biased : Boolean := True) | |
12348 | is | |
12349 | begin | |
12350 | if Biased then | |
12351 | Set_Has_Biased_Representation (E); | |
12352 | ||
12353 | if Warn_On_Biased_Representation then | |
12354 | Error_Msg_NE | |
1e3532e7 | 12355 | ("?B?" & Msg & " forces biased representation for&", N, E); |
b77e4501 | 12356 | end if; |
12357 | end if; | |
12358 | end Set_Biased; | |
12359 | ||
d6f39728 | 12360 | -------------------- |
12361 | -- Set_Enum_Esize -- | |
12362 | -------------------- | |
12363 | ||
12364 | procedure Set_Enum_Esize (T : Entity_Id) is | |
12365 | Lo : Uint; | |
12366 | Hi : Uint; | |
12367 | Sz : Nat; | |
12368 | ||
12369 | begin | |
12370 | Init_Alignment (T); | |
12371 | ||
12372 | -- Find the minimum standard size (8,16,32,64) that fits | |
12373 | ||
12374 | Lo := Enumeration_Rep (Entity (Type_Low_Bound (T))); | |
12375 | Hi := Enumeration_Rep (Entity (Type_High_Bound (T))); | |
12376 | ||
12377 | if Lo < 0 then | |
12378 | if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then | |
f15731c4 | 12379 | Sz := Standard_Character_Size; -- May be > 8 on some targets |
d6f39728 | 12380 | |
12381 | elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then | |
12382 | Sz := 16; | |
12383 | ||
12384 | elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then | |
12385 | Sz := 32; | |
12386 | ||
12387 | else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63); | |
12388 | Sz := 64; | |
12389 | end if; | |
12390 | ||
12391 | else | |
12392 | if Hi < Uint_2**08 then | |
f15731c4 | 12393 | Sz := Standard_Character_Size; -- May be > 8 on some targets |
d6f39728 | 12394 | |
12395 | elsif Hi < Uint_2**16 then | |
12396 | Sz := 16; | |
12397 | ||
12398 | elsif Hi < Uint_2**32 then | |
12399 | Sz := 32; | |
12400 | ||
12401 | else pragma Assert (Hi < Uint_2**63); | |
12402 | Sz := 64; | |
12403 | end if; | |
12404 | end if; | |
12405 | ||
12406 | -- That minimum is the proper size unless we have a foreign convention | |
12407 | -- and the size required is 32 or less, in which case we bump the size | |
12408 | -- up to 32. This is required for C and C++ and seems reasonable for | |
12409 | -- all other foreign conventions. | |
12410 | ||
12411 | if Has_Foreign_Convention (T) | |
12412 | and then Esize (T) < Standard_Integer_Size | |
db1eed69 | 12413 | |
12414 | -- Don't do this if Short_Enums on target | |
12415 | ||
e9185b9d | 12416 | and then not Target_Short_Enums |
d6f39728 | 12417 | then |
12418 | Init_Esize (T, Standard_Integer_Size); | |
d6f39728 | 12419 | else |
12420 | Init_Esize (T, Sz); | |
12421 | end if; | |
d6f39728 | 12422 | end Set_Enum_Esize; |
12423 | ||
2625eb01 | 12424 | ----------------------------- |
12425 | -- Uninstall_Discriminants -- | |
12426 | ----------------------------- | |
12427 | ||
12428 | procedure Uninstall_Discriminants (E : Entity_Id) is | |
12429 | Disc : Entity_Id; | |
12430 | Prev : Entity_Id; | |
12431 | Outer : Entity_Id; | |
12432 | ||
12433 | begin | |
12434 | -- Discriminants have been made visible for type declarations and | |
12435 | -- protected type declarations, not for subtype declarations. | |
12436 | ||
12437 | if Nkind (Parent (E)) /= N_Subtype_Declaration then | |
12438 | Disc := First_Discriminant (E); | |
12439 | while Present (Disc) loop | |
12440 | if Disc /= Current_Entity (Disc) then | |
12441 | Prev := Current_Entity (Disc); | |
12442 | while Present (Prev) | |
12443 | and then Present (Homonym (Prev)) | |
12444 | and then Homonym (Prev) /= Disc | |
12445 | loop | |
12446 | Prev := Homonym (Prev); | |
12447 | end loop; | |
12448 | else | |
12449 | Prev := Empty; | |
12450 | end if; | |
12451 | ||
12452 | Set_Is_Immediately_Visible (Disc, False); | |
12453 | ||
12454 | Outer := Homonym (Disc); | |
12455 | while Present (Outer) and then Scope (Outer) = E loop | |
12456 | Outer := Homonym (Outer); | |
12457 | end loop; | |
12458 | ||
12459 | -- Reset homonym link of other entities, but do not modify link | |
12460 | -- between entities in current scope, so that the back-end can | |
12461 | -- have a proper count of local overloadings. | |
12462 | ||
12463 | if No (Prev) then | |
12464 | Set_Name_Entity_Id (Chars (Disc), Outer); | |
12465 | ||
12466 | elsif Scope (Prev) /= Scope (Disc) then | |
12467 | Set_Homonym (Prev, Outer); | |
12468 | end if; | |
12469 | ||
12470 | Next_Discriminant (Disc); | |
12471 | end loop; | |
12472 | end if; | |
12473 | end Uninstall_Discriminants; | |
12474 | ||
12475 | ------------------------------------------- | |
12476 | -- Uninstall_Discriminants_And_Pop_Scope -- | |
12477 | ------------------------------------------- | |
12478 | ||
12479 | procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is | |
12480 | begin | |
12481 | if Has_Discriminants (E) then | |
12482 | Uninstall_Discriminants (E); | |
12483 | Pop_Scope; | |
12484 | end if; | |
12485 | end Uninstall_Discriminants_And_Pop_Scope; | |
12486 | ||
83f8f0a6 | 12487 | ------------------------------ |
12488 | -- Validate_Address_Clauses -- | |
12489 | ------------------------------ | |
12490 | ||
12491 | procedure Validate_Address_Clauses is | |
12492 | begin | |
12493 | for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop | |
12494 | declare | |
12495 | ACCR : Address_Clause_Check_Record | |
12496 | renames Address_Clause_Checks.Table (J); | |
12497 | ||
d6da7448 | 12498 | Expr : Node_Id; |
12499 | ||
83f8f0a6 | 12500 | X_Alignment : Uint; |
12501 | Y_Alignment : Uint; | |
12502 | ||
12503 | X_Size : Uint; | |
12504 | Y_Size : Uint; | |
12505 | ||
12506 | begin | |
12507 | -- Skip processing of this entry if warning already posted | |
12508 | ||
12509 | if not Address_Warning_Posted (ACCR.N) then | |
d6da7448 | 12510 | Expr := Original_Node (Expression (ACCR.N)); |
83f8f0a6 | 12511 | |
d6da7448 | 12512 | -- Get alignments |
83f8f0a6 | 12513 | |
d6da7448 | 12514 | X_Alignment := Alignment (ACCR.X); |
12515 | Y_Alignment := Alignment (ACCR.Y); | |
83f8f0a6 | 12516 | |
12517 | -- Similarly obtain sizes | |
12518 | ||
d6da7448 | 12519 | X_Size := Esize (ACCR.X); |
12520 | Y_Size := Esize (ACCR.Y); | |
83f8f0a6 | 12521 | |
12522 | -- Check for large object overlaying smaller one | |
12523 | ||
12524 | if Y_Size > Uint_0 | |
12525 | and then X_Size > Uint_0 | |
12526 | and then X_Size > Y_Size | |
12527 | then | |
d6da7448 | 12528 | Error_Msg_NE |
1581f2d7 | 12529 | ("??& overlays smaller object", ACCR.N, ACCR.X); |
83f8f0a6 | 12530 | Error_Msg_N |
1e3532e7 | 12531 | ("\??program execution may be erroneous", ACCR.N); |
83f8f0a6 | 12532 | Error_Msg_Uint_1 := X_Size; |
12533 | Error_Msg_NE | |
1e3532e7 | 12534 | ("\??size of & is ^", ACCR.N, ACCR.X); |
83f8f0a6 | 12535 | Error_Msg_Uint_1 := Y_Size; |
12536 | Error_Msg_NE | |
1e3532e7 | 12537 | ("\??size of & is ^", ACCR.N, ACCR.Y); |
83f8f0a6 | 12538 | |
d6da7448 | 12539 | -- Check for inadequate alignment, both of the base object |
12540 | -- and of the offset, if any. | |
83f8f0a6 | 12541 | |
d6da7448 | 12542 | -- Note: we do not check the alignment if we gave a size |
12543 | -- warning, since it would likely be redundant. | |
83f8f0a6 | 12544 | |
12545 | elsif Y_Alignment /= Uint_0 | |
d6da7448 | 12546 | and then (Y_Alignment < X_Alignment |
12547 | or else (ACCR.Off | |
12548 | and then | |
12549 | Nkind (Expr) = N_Attribute_Reference | |
12550 | and then | |
12551 | Attribute_Name (Expr) = Name_Address | |
12552 | and then | |
12553 | Has_Compatible_Alignment | |
12554 | (ACCR.X, Prefix (Expr)) | |
12555 | /= Known_Compatible)) | |
83f8f0a6 | 12556 | then |
12557 | Error_Msg_NE | |
1e3532e7 | 12558 | ("??specified address for& may be inconsistent " |
12559 | & "with alignment", ACCR.N, ACCR.X); | |
83f8f0a6 | 12560 | Error_Msg_N |
1e3532e7 | 12561 | ("\??program execution may be erroneous (RM 13.3(27))", |
83f8f0a6 | 12562 | ACCR.N); |
12563 | Error_Msg_Uint_1 := X_Alignment; | |
12564 | Error_Msg_NE | |
1e3532e7 | 12565 | ("\??alignment of & is ^", ACCR.N, ACCR.X); |
83f8f0a6 | 12566 | Error_Msg_Uint_1 := Y_Alignment; |
12567 | Error_Msg_NE | |
1e3532e7 | 12568 | ("\??alignment of & is ^", ACCR.N, ACCR.Y); |
d6da7448 | 12569 | if Y_Alignment >= X_Alignment then |
12570 | Error_Msg_N | |
1e3532e7 | 12571 | ("\??but offset is not multiple of alignment", ACCR.N); |
d6da7448 | 12572 | end if; |
83f8f0a6 | 12573 | end if; |
12574 | end if; | |
12575 | end; | |
12576 | end loop; | |
12577 | end Validate_Address_Clauses; | |
12578 | ||
7717ea00 | 12579 | --------------------------- |
12580 | -- Validate_Independence -- | |
12581 | --------------------------- | |
12582 | ||
12583 | procedure Validate_Independence is | |
12584 | SU : constant Uint := UI_From_Int (System_Storage_Unit); | |
12585 | N : Node_Id; | |
12586 | E : Entity_Id; | |
12587 | IC : Boolean; | |
12588 | Comp : Entity_Id; | |
12589 | Addr : Node_Id; | |
12590 | P : Node_Id; | |
12591 | ||
12592 | procedure Check_Array_Type (Atyp : Entity_Id); | |
12593 | -- Checks if the array type Atyp has independent components, and | |
12594 | -- if not, outputs an appropriate set of error messages. | |
12595 | ||
12596 | procedure No_Independence; | |
12597 | -- Output message that independence cannot be guaranteed | |
12598 | ||
12599 | function OK_Component (C : Entity_Id) return Boolean; | |
12600 | -- Checks one component to see if it is independently accessible, and | |
12601 | -- if so yields True, otherwise yields False if independent access | |
12602 | -- cannot be guaranteed. This is a conservative routine, it only | |
12603 | -- returns True if it knows for sure, it returns False if it knows | |
12604 | -- there is a problem, or it cannot be sure there is no problem. | |
12605 | ||
12606 | procedure Reason_Bad_Component (C : Entity_Id); | |
12607 | -- Outputs continuation message if a reason can be determined for | |
12608 | -- the component C being bad. | |
12609 | ||
12610 | ---------------------- | |
12611 | -- Check_Array_Type -- | |
12612 | ---------------------- | |
12613 | ||
12614 | procedure Check_Array_Type (Atyp : Entity_Id) is | |
12615 | Ctyp : constant Entity_Id := Component_Type (Atyp); | |
12616 | ||
12617 | begin | |
12618 | -- OK if no alignment clause, no pack, and no component size | |
12619 | ||
12620 | if not Has_Component_Size_Clause (Atyp) | |
12621 | and then not Has_Alignment_Clause (Atyp) | |
12622 | and then not Is_Packed (Atyp) | |
12623 | then | |
12624 | return; | |
12625 | end if; | |
12626 | ||
aa0a69ab | 12627 | -- Case of component size is greater than or equal to 64 and the |
12628 | -- alignment of the array is at least as large as the alignment | |
12629 | -- of the component. We are definitely OK in this situation. | |
12630 | ||
12631 | if Known_Component_Size (Atyp) | |
12632 | and then Component_Size (Atyp) >= 64 | |
12633 | and then Known_Alignment (Atyp) | |
12634 | and then Known_Alignment (Ctyp) | |
12635 | and then Alignment (Atyp) >= Alignment (Ctyp) | |
12636 | then | |
12637 | return; | |
12638 | end if; | |
12639 | ||
7717ea00 | 12640 | -- Check actual component size |
12641 | ||
12642 | if not Known_Component_Size (Atyp) | |
12643 | or else not (Addressable (Component_Size (Atyp)) | |
aa0a69ab | 12644 | and then Component_Size (Atyp) < 64) |
7717ea00 | 12645 | or else Component_Size (Atyp) mod Esize (Ctyp) /= 0 |
12646 | then | |
12647 | No_Independence; | |
12648 | ||
12649 | -- Bad component size, check reason | |
12650 | ||
12651 | if Has_Component_Size_Clause (Atyp) then | |
b9e61b2a | 12652 | P := Get_Attribute_Definition_Clause |
12653 | (Atyp, Attribute_Component_Size); | |
7717ea00 | 12654 | |
12655 | if Present (P) then | |
12656 | Error_Msg_Sloc := Sloc (P); | |
12657 | Error_Msg_N ("\because of Component_Size clause#", N); | |
12658 | return; | |
12659 | end if; | |
12660 | end if; | |
12661 | ||
12662 | if Is_Packed (Atyp) then | |
12663 | P := Get_Rep_Pragma (Atyp, Name_Pack); | |
12664 | ||
12665 | if Present (P) then | |
12666 | Error_Msg_Sloc := Sloc (P); | |
12667 | Error_Msg_N ("\because of pragma Pack#", N); | |
12668 | return; | |
12669 | end if; | |
12670 | end if; | |
12671 | ||
12672 | -- No reason found, just return | |
12673 | ||
12674 | return; | |
12675 | end if; | |
12676 | ||
12677 | -- Array type is OK independence-wise | |
12678 | ||
12679 | return; | |
12680 | end Check_Array_Type; | |
12681 | ||
12682 | --------------------- | |
12683 | -- No_Independence -- | |
12684 | --------------------- | |
12685 | ||
12686 | procedure No_Independence is | |
12687 | begin | |
12688 | if Pragma_Name (N) = Name_Independent then | |
18393965 | 12689 | Error_Msg_NE ("independence cannot be guaranteed for&", N, E); |
7717ea00 | 12690 | else |
12691 | Error_Msg_NE | |
12692 | ("independent components cannot be guaranteed for&", N, E); | |
12693 | end if; | |
12694 | end No_Independence; | |
12695 | ||
12696 | ------------------ | |
12697 | -- OK_Component -- | |
12698 | ------------------ | |
12699 | ||
12700 | function OK_Component (C : Entity_Id) return Boolean is | |
12701 | Rec : constant Entity_Id := Scope (C); | |
12702 | Ctyp : constant Entity_Id := Etype (C); | |
12703 | ||
12704 | begin | |
12705 | -- OK if no component clause, no Pack, and no alignment clause | |
12706 | ||
12707 | if No (Component_Clause (C)) | |
12708 | and then not Is_Packed (Rec) | |
12709 | and then not Has_Alignment_Clause (Rec) | |
12710 | then | |
12711 | return True; | |
12712 | end if; | |
12713 | ||
12714 | -- Here we look at the actual component layout. A component is | |
12715 | -- addressable if its size is a multiple of the Esize of the | |
12716 | -- component type, and its starting position in the record has | |
12717 | -- appropriate alignment, and the record itself has appropriate | |
12718 | -- alignment to guarantee the component alignment. | |
12719 | ||
12720 | -- Make sure sizes are static, always assume the worst for any | |
12721 | -- cases where we cannot check static values. | |
12722 | ||
12723 | if not (Known_Static_Esize (C) | |
b9e61b2a | 12724 | and then |
12725 | Known_Static_Esize (Ctyp)) | |
7717ea00 | 12726 | then |
12727 | return False; | |
12728 | end if; | |
12729 | ||
12730 | -- Size of component must be addressable or greater than 64 bits | |
12731 | -- and a multiple of bytes. | |
12732 | ||
b9e61b2a | 12733 | if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then |
7717ea00 | 12734 | return False; |
12735 | end if; | |
12736 | ||
12737 | -- Check size is proper multiple | |
12738 | ||
12739 | if Esize (C) mod Esize (Ctyp) /= 0 then | |
12740 | return False; | |
12741 | end if; | |
12742 | ||
12743 | -- Check alignment of component is OK | |
12744 | ||
12745 | if not Known_Component_Bit_Offset (C) | |
12746 | or else Component_Bit_Offset (C) < Uint_0 | |
12747 | or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0 | |
12748 | then | |
12749 | return False; | |
12750 | end if; | |
12751 | ||
12752 | -- Check alignment of record type is OK | |
12753 | ||
12754 | if not Known_Alignment (Rec) | |
12755 | or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0 | |
12756 | then | |
12757 | return False; | |
12758 | end if; | |
12759 | ||
12760 | -- All tests passed, component is addressable | |
12761 | ||
12762 | return True; | |
12763 | end OK_Component; | |
12764 | ||
12765 | -------------------------- | |
12766 | -- Reason_Bad_Component -- | |
12767 | -------------------------- | |
12768 | ||
12769 | procedure Reason_Bad_Component (C : Entity_Id) is | |
12770 | Rec : constant Entity_Id := Scope (C); | |
12771 | Ctyp : constant Entity_Id := Etype (C); | |
12772 | ||
12773 | begin | |
12774 | -- If component clause present assume that's the problem | |
12775 | ||
12776 | if Present (Component_Clause (C)) then | |
12777 | Error_Msg_Sloc := Sloc (Component_Clause (C)); | |
12778 | Error_Msg_N ("\because of Component_Clause#", N); | |
12779 | return; | |
12780 | end if; | |
12781 | ||
12782 | -- If pragma Pack clause present, assume that's the problem | |
12783 | ||
12784 | if Is_Packed (Rec) then | |
12785 | P := Get_Rep_Pragma (Rec, Name_Pack); | |
12786 | ||
12787 | if Present (P) then | |
12788 | Error_Msg_Sloc := Sloc (P); | |
12789 | Error_Msg_N ("\because of pragma Pack#", N); | |
12790 | return; | |
12791 | end if; | |
12792 | end if; | |
12793 | ||
12794 | -- See if record has bad alignment clause | |
12795 | ||
12796 | if Has_Alignment_Clause (Rec) | |
12797 | and then Known_Alignment (Rec) | |
12798 | and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0 | |
12799 | then | |
12800 | P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment); | |
12801 | ||
12802 | if Present (P) then | |
12803 | Error_Msg_Sloc := Sloc (P); | |
12804 | Error_Msg_N ("\because of Alignment clause#", N); | |
12805 | end if; | |
12806 | end if; | |
12807 | ||
12808 | -- Couldn't find a reason, so return without a message | |
12809 | ||
12810 | return; | |
12811 | end Reason_Bad_Component; | |
12812 | ||
12813 | -- Start of processing for Validate_Independence | |
12814 | ||
12815 | begin | |
12816 | for J in Independence_Checks.First .. Independence_Checks.Last loop | |
12817 | N := Independence_Checks.Table (J).N; | |
12818 | E := Independence_Checks.Table (J).E; | |
12819 | IC := Pragma_Name (N) = Name_Independent_Components; | |
12820 | ||
12821 | -- Deal with component case | |
12822 | ||
12823 | if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then | |
12824 | if not OK_Component (E) then | |
12825 | No_Independence; | |
12826 | Reason_Bad_Component (E); | |
12827 | goto Continue; | |
12828 | end if; | |
12829 | end if; | |
12830 | ||
12831 | -- Deal with record with Independent_Components | |
12832 | ||
12833 | if IC and then Is_Record_Type (E) then | |
12834 | Comp := First_Component_Or_Discriminant (E); | |
12835 | while Present (Comp) loop | |
12836 | if not OK_Component (Comp) then | |
12837 | No_Independence; | |
12838 | Reason_Bad_Component (Comp); | |
12839 | goto Continue; | |
12840 | end if; | |
12841 | ||
12842 | Next_Component_Or_Discriminant (Comp); | |
12843 | end loop; | |
12844 | end if; | |
12845 | ||
12846 | -- Deal with address clause case | |
12847 | ||
12848 | if Is_Object (E) then | |
12849 | Addr := Address_Clause (E); | |
12850 | ||
12851 | if Present (Addr) then | |
12852 | No_Independence; | |
12853 | Error_Msg_Sloc := Sloc (Addr); | |
12854 | Error_Msg_N ("\because of Address clause#", N); | |
12855 | goto Continue; | |
12856 | end if; | |
12857 | end if; | |
12858 | ||
12859 | -- Deal with independent components for array type | |
12860 | ||
12861 | if IC and then Is_Array_Type (E) then | |
12862 | Check_Array_Type (E); | |
12863 | end if; | |
12864 | ||
12865 | -- Deal with independent components for array object | |
12866 | ||
12867 | if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then | |
12868 | Check_Array_Type (Etype (E)); | |
12869 | end if; | |
12870 | ||
12871 | <<Continue>> null; | |
12872 | end loop; | |
12873 | end Validate_Independence; | |
12874 | ||
b3f8228a | 12875 | ------------------------------ |
12876 | -- Validate_Iterable_Aspect -- | |
12877 | ------------------------------ | |
12878 | ||
12879 | procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is | |
3061ffde | 12880 | Assoc : Node_Id; |
12881 | Expr : Node_Id; | |
b3f8228a | 12882 | |
bde03454 | 12883 | Prim : Node_Id; |
a9f5fea7 | 12884 | Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ); |
b3f8228a | 12885 | |
12886 | First_Id : Entity_Id; | |
12887 | Next_Id : Entity_Id; | |
12888 | Has_Element_Id : Entity_Id; | |
12889 | Element_Id : Entity_Id; | |
12890 | ||
b3f8228a | 12891 | begin |
9698629c | 12892 | -- If previous error aspect is unusable |
a9f5fea7 | 12893 | |
12894 | if Cursor = Any_Type then | |
3061ffde | 12895 | return; |
12896 | end if; | |
b3f8228a | 12897 | |
12898 | First_Id := Empty; | |
12899 | Next_Id := Empty; | |
12900 | Has_Element_Id := Empty; | |
32de816b | 12901 | Element_Id := Empty; |
b3f8228a | 12902 | |
12903 | -- Each expression must resolve to a function with the proper signature | |
12904 | ||
12905 | Assoc := First (Component_Associations (Expression (ASN))); | |
12906 | while Present (Assoc) loop | |
12907 | Expr := Expression (Assoc); | |
12908 | Analyze (Expr); | |
12909 | ||
b3f8228a | 12910 | Prim := First (Choices (Assoc)); |
bde03454 | 12911 | |
f02a9a9a | 12912 | if Nkind (Prim) /= N_Identifier or else Present (Next (Prim)) then |
b3f8228a | 12913 | Error_Msg_N ("illegal name in association", Prim); |
12914 | ||
12915 | elsif Chars (Prim) = Name_First then | |
3061ffde | 12916 | Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First); |
b3f8228a | 12917 | First_Id := Entity (Expr); |
b3f8228a | 12918 | |
12919 | elsif Chars (Prim) = Name_Next then | |
3061ffde | 12920 | Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next); |
b3f8228a | 12921 | Next_Id := Entity (Expr); |
b3f8228a | 12922 | |
12923 | elsif Chars (Prim) = Name_Has_Element then | |
3061ffde | 12924 | Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element); |
b3f8228a | 12925 | Has_Element_Id := Entity (Expr); |
bde03454 | 12926 | |
b3f8228a | 12927 | elsif Chars (Prim) = Name_Element then |
3061ffde | 12928 | Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element); |
b3f8228a | 12929 | Element_Id := Entity (Expr); |
b3f8228a | 12930 | |
12931 | else | |
12932 | Error_Msg_N ("invalid name for iterable function", Prim); | |
12933 | end if; | |
12934 | ||
12935 | Next (Assoc); | |
12936 | end loop; | |
12937 | ||
12938 | if No (First_Id) then | |
3061ffde | 12939 | Error_Msg_N ("match for First primitive not found", ASN); |
b3f8228a | 12940 | |
12941 | elsif No (Next_Id) then | |
3061ffde | 12942 | Error_Msg_N ("match for Next primitive not found", ASN); |
b3f8228a | 12943 | |
12944 | elsif No (Has_Element_Id) then | |
3061ffde | 12945 | Error_Msg_N ("match for Has_Element primitive not found", ASN); |
12946 | ||
12947 | elsif No (Element_Id) then | |
12948 | null; -- Optional. | |
b3f8228a | 12949 | end if; |
12950 | end Validate_Iterable_Aspect; | |
12951 | ||
d6f39728 | 12952 | ----------------------------------- |
12953 | -- Validate_Unchecked_Conversion -- | |
12954 | ----------------------------------- | |
12955 | ||
12956 | procedure Validate_Unchecked_Conversion | |
12957 | (N : Node_Id; | |
12958 | Act_Unit : Entity_Id) | |
12959 | is | |
12960 | Source : Entity_Id; | |
12961 | Target : Entity_Id; | |
12962 | Vnode : Node_Id; | |
12963 | ||
12964 | begin | |
12965 | -- Obtain source and target types. Note that we call Ancestor_Subtype | |
12966 | -- here because the processing for generic instantiation always makes | |
12967 | -- subtypes, and we want the original frozen actual types. | |
12968 | ||
12969 | -- If we are dealing with private types, then do the check on their | |
12970 | -- fully declared counterparts if the full declarations have been | |
39a0c1d3 | 12971 | -- encountered (they don't have to be visible, but they must exist). |
d6f39728 | 12972 | |
12973 | Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit))); | |
12974 | ||
12975 | if Is_Private_Type (Source) | |
12976 | and then Present (Underlying_Type (Source)) | |
12977 | then | |
12978 | Source := Underlying_Type (Source); | |
12979 | end if; | |
12980 | ||
12981 | Target := Ancestor_Subtype (Etype (Act_Unit)); | |
12982 | ||
fdd294d1 | 12983 | -- If either type is generic, the instantiation happens within a generic |
95deda50 | 12984 | -- unit, and there is nothing to check. The proper check will happen |
12985 | -- when the enclosing generic is instantiated. | |
d6f39728 | 12986 | |
12987 | if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then | |
12988 | return; | |
12989 | end if; | |
12990 | ||
12991 | if Is_Private_Type (Target) | |
12992 | and then Present (Underlying_Type (Target)) | |
12993 | then | |
12994 | Target := Underlying_Type (Target); | |
12995 | end if; | |
12996 | ||
12997 | -- Source may be unconstrained array, but not target | |
12998 | ||
b9e61b2a | 12999 | if Is_Array_Type (Target) and then not Is_Constrained (Target) then |
d6f39728 | 13000 | Error_Msg_N |
13001 | ("unchecked conversion to unconstrained array not allowed", N); | |
13002 | return; | |
13003 | end if; | |
13004 | ||
fbc67f84 | 13005 | -- Warn if conversion between two different convention pointers |
13006 | ||
13007 | if Is_Access_Type (Target) | |
13008 | and then Is_Access_Type (Source) | |
13009 | and then Convention (Target) /= Convention (Source) | |
13010 | and then Warn_On_Unchecked_Conversion | |
13011 | then | |
74c7ae52 | 13012 | -- Give warnings for subprogram pointers only on most targets |
fdd294d1 | 13013 | |
13014 | if Is_Access_Subprogram_Type (Target) | |
13015 | or else Is_Access_Subprogram_Type (Source) | |
fdd294d1 | 13016 | then |
13017 | Error_Msg_N | |
cb97ae5c | 13018 | ("?z?conversion between pointers with different conventions!", |
1e3532e7 | 13019 | N); |
fdd294d1 | 13020 | end if; |
fbc67f84 | 13021 | end if; |
13022 | ||
3062c401 | 13023 | -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a |
13024 | -- warning when compiling GNAT-related sources. | |
13025 | ||
13026 | if Warn_On_Unchecked_Conversion | |
13027 | and then not In_Predefined_Unit (N) | |
13028 | and then RTU_Loaded (Ada_Calendar) | |
f02a9a9a | 13029 | and then (Chars (Source) = Name_Time |
13030 | or else | |
13031 | Chars (Target) = Name_Time) | |
3062c401 | 13032 | then |
13033 | -- If Ada.Calendar is loaded and the name of one of the operands is | |
13034 | -- Time, there is a good chance that this is Ada.Calendar.Time. | |
13035 | ||
13036 | declare | |
f02a9a9a | 13037 | Calendar_Time : constant Entity_Id := Full_View (RTE (RO_CA_Time)); |
3062c401 | 13038 | begin |
13039 | pragma Assert (Present (Calendar_Time)); | |
13040 | ||
b9e61b2a | 13041 | if Source = Calendar_Time or else Target = Calendar_Time then |
3062c401 | 13042 | Error_Msg_N |
f02a9a9a | 13043 | ("?z?representation of 'Time values may change between " |
13044 | & "'G'N'A'T versions", N); | |
3062c401 | 13045 | end if; |
13046 | end; | |
13047 | end if; | |
13048 | ||
fdd294d1 | 13049 | -- Make entry in unchecked conversion table for later processing by |
13050 | -- Validate_Unchecked_Conversions, which will check sizes and alignments | |
13051 | -- (using values set by the back-end where possible). This is only done | |
13052 | -- if the appropriate warning is active. | |
d6f39728 | 13053 | |
9dfe12ae | 13054 | if Warn_On_Unchecked_Conversion then |
13055 | Unchecked_Conversions.Append | |
86d32751 | 13056 | (New_Val => UC_Entry'(Eloc => Sloc (N), |
13057 | Source => Source, | |
13058 | Target => Target, | |
13059 | Act_Unit => Act_Unit)); | |
9dfe12ae | 13060 | |
13061 | -- If both sizes are known statically now, then back end annotation | |
13062 | -- is not required to do a proper check but if either size is not | |
13063 | -- known statically, then we need the annotation. | |
13064 | ||
13065 | if Known_Static_RM_Size (Source) | |
1e3532e7 | 13066 | and then |
13067 | Known_Static_RM_Size (Target) | |
9dfe12ae | 13068 | then |
13069 | null; | |
13070 | else | |
13071 | Back_Annotate_Rep_Info := True; | |
13072 | end if; | |
13073 | end if; | |
d6f39728 | 13074 | |
fdd294d1 | 13075 | -- If unchecked conversion to access type, and access type is declared |
95deda50 | 13076 | -- in the same unit as the unchecked conversion, then set the flag |
13077 | -- No_Strict_Aliasing (no strict aliasing is implicit here) | |
28ed91d4 | 13078 | |
13079 | if Is_Access_Type (Target) and then | |
13080 | In_Same_Source_Unit (Target, N) | |
13081 | then | |
13082 | Set_No_Strict_Aliasing (Implementation_Base_Type (Target)); | |
13083 | end if; | |
3d875462 | 13084 | |
95deda50 | 13085 | -- Generate N_Validate_Unchecked_Conversion node for back end in case |
13086 | -- the back end needs to perform special validation checks. | |
3d875462 | 13087 | |
95deda50 | 13088 | -- Shouldn't this be in Exp_Ch13, since the check only gets done if we |
13089 | -- have full expansion and the back end is called ??? | |
3d875462 | 13090 | |
13091 | Vnode := | |
13092 | Make_Validate_Unchecked_Conversion (Sloc (N)); | |
13093 | Set_Source_Type (Vnode, Source); | |
13094 | Set_Target_Type (Vnode, Target); | |
13095 | ||
fdd294d1 | 13096 | -- If the unchecked conversion node is in a list, just insert before it. |
13097 | -- If not we have some strange case, not worth bothering about. | |
3d875462 | 13098 | |
13099 | if Is_List_Member (N) then | |
d6f39728 | 13100 | Insert_After (N, Vnode); |
13101 | end if; | |
13102 | end Validate_Unchecked_Conversion; | |
13103 | ||
13104 | ------------------------------------ | |
13105 | -- Validate_Unchecked_Conversions -- | |
13106 | ------------------------------------ | |
13107 | ||
13108 | procedure Validate_Unchecked_Conversions is | |
13109 | begin | |
13110 | for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop | |
13111 | declare | |
13112 | T : UC_Entry renames Unchecked_Conversions.Table (N); | |
13113 | ||
86d32751 | 13114 | Eloc : constant Source_Ptr := T.Eloc; |
13115 | Source : constant Entity_Id := T.Source; | |
13116 | Target : constant Entity_Id := T.Target; | |
13117 | Act_Unit : constant Entity_Id := T.Act_Unit; | |
d6f39728 | 13118 | |
44705307 | 13119 | Source_Siz : Uint; |
13120 | Target_Siz : Uint; | |
d6f39728 | 13121 | |
13122 | begin | |
86d32751 | 13123 | -- Skip if function marked as warnings off |
13124 | ||
13125 | if Warnings_Off (Act_Unit) then | |
13126 | goto Continue; | |
13127 | end if; | |
13128 | ||
fdd294d1 | 13129 | -- This validation check, which warns if we have unequal sizes for |
13130 | -- unchecked conversion, and thus potentially implementation | |
d6f39728 | 13131 | -- dependent semantics, is one of the few occasions on which we |
fdd294d1 | 13132 | -- use the official RM size instead of Esize. See description in |
13133 | -- Einfo "Handling of Type'Size Values" for details. | |
d6f39728 | 13134 | |
f15731c4 | 13135 | if Serious_Errors_Detected = 0 |
d6f39728 | 13136 | and then Known_Static_RM_Size (Source) |
13137 | and then Known_Static_RM_Size (Target) | |
f25f4252 | 13138 | |
13139 | -- Don't do the check if warnings off for either type, note the | |
13140 | -- deliberate use of OR here instead of OR ELSE to get the flag | |
13141 | -- Warnings_Off_Used set for both types if appropriate. | |
13142 | ||
13143 | and then not (Has_Warnings_Off (Source) | |
13144 | or | |
13145 | Has_Warnings_Off (Target)) | |
d6f39728 | 13146 | then |
13147 | Source_Siz := RM_Size (Source); | |
13148 | Target_Siz := RM_Size (Target); | |
13149 | ||
13150 | if Source_Siz /= Target_Siz then | |
299480f9 | 13151 | Error_Msg |
cb97ae5c | 13152 | ("?z?types for unchecked conversion have different sizes!", |
299480f9 | 13153 | Eloc); |
d6f39728 | 13154 | |
13155 | if All_Errors_Mode then | |
13156 | Error_Msg_Name_1 := Chars (Source); | |
13157 | Error_Msg_Uint_1 := Source_Siz; | |
13158 | Error_Msg_Name_2 := Chars (Target); | |
13159 | Error_Msg_Uint_2 := Target_Siz; | |
cb97ae5c | 13160 | Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc); |
d6f39728 | 13161 | |
13162 | Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz); | |
13163 | ||
13164 | if Is_Discrete_Type (Source) | |
b9e61b2a | 13165 | and then |
13166 | Is_Discrete_Type (Target) | |
d6f39728 | 13167 | then |
13168 | if Source_Siz > Target_Siz then | |
299480f9 | 13169 | Error_Msg |
cb97ae5c | 13170 | ("\?z?^ high order bits of source will " |
1e3532e7 | 13171 | & "be ignored!", Eloc); |
d6f39728 | 13172 | |
9dfe12ae | 13173 | elsif Is_Unsigned_Type (Source) then |
299480f9 | 13174 | Error_Msg |
cb97ae5c | 13175 | ("\?z?source will be extended with ^ high order " |
1581f2d7 | 13176 | & "zero bits!", Eloc); |
d6f39728 | 13177 | |
13178 | else | |
299480f9 | 13179 | Error_Msg |
cb97ae5c | 13180 | ("\?z?source will be extended with ^ high order " |
1e3532e7 | 13181 | & "sign bits!", Eloc); |
d6f39728 | 13182 | end if; |
13183 | ||
13184 | elsif Source_Siz < Target_Siz then | |
13185 | if Is_Discrete_Type (Target) then | |
13186 | if Bytes_Big_Endian then | |
299480f9 | 13187 | Error_Msg |
cb97ae5c | 13188 | ("\?z?target value will include ^ undefined " |
1e3532e7 | 13189 | & "low order bits!", Eloc); |
d6f39728 | 13190 | else |
299480f9 | 13191 | Error_Msg |
cb97ae5c | 13192 | ("\?z?target value will include ^ undefined " |
1e3532e7 | 13193 | & "high order bits!", Eloc); |
d6f39728 | 13194 | end if; |
13195 | ||
13196 | else | |
299480f9 | 13197 | Error_Msg |
cb97ae5c | 13198 | ("\?z?^ trailing bits of target value will be " |
1e3532e7 | 13199 | & "undefined!", Eloc); |
d6f39728 | 13200 | end if; |
13201 | ||
13202 | else pragma Assert (Source_Siz > Target_Siz); | |
299480f9 | 13203 | Error_Msg |
cb97ae5c | 13204 | ("\?z?^ trailing bits of source will be ignored!", |
299480f9 | 13205 | Eloc); |
d6f39728 | 13206 | end if; |
13207 | end if; | |
d6f39728 | 13208 | end if; |
13209 | end if; | |
13210 | ||
13211 | -- If both types are access types, we need to check the alignment. | |
13212 | -- If the alignment of both is specified, we can do it here. | |
13213 | ||
f15731c4 | 13214 | if Serious_Errors_Detected = 0 |
2a10e737 | 13215 | and then Is_Access_Type (Source) |
13216 | and then Is_Access_Type (Target) | |
d6f39728 | 13217 | and then Target_Strict_Alignment |
13218 | and then Present (Designated_Type (Source)) | |
13219 | and then Present (Designated_Type (Target)) | |
13220 | then | |
13221 | declare | |
13222 | D_Source : constant Entity_Id := Designated_Type (Source); | |
13223 | D_Target : constant Entity_Id := Designated_Type (Target); | |
13224 | ||
13225 | begin | |
13226 | if Known_Alignment (D_Source) | |
b9e61b2a | 13227 | and then |
13228 | Known_Alignment (D_Target) | |
d6f39728 | 13229 | then |
13230 | declare | |
13231 | Source_Align : constant Uint := Alignment (D_Source); | |
13232 | Target_Align : constant Uint := Alignment (D_Target); | |
13233 | ||
13234 | begin | |
13235 | if Source_Align < Target_Align | |
13236 | and then not Is_Tagged_Type (D_Source) | |
f25f4252 | 13237 | |
13238 | -- Suppress warning if warnings suppressed on either | |
13239 | -- type or either designated type. Note the use of | |
13240 | -- OR here instead of OR ELSE. That is intentional, | |
13241 | -- we would like to set flag Warnings_Off_Used in | |
13242 | -- all types for which warnings are suppressed. | |
13243 | ||
13244 | and then not (Has_Warnings_Off (D_Source) | |
13245 | or | |
13246 | Has_Warnings_Off (D_Target) | |
13247 | or | |
13248 | Has_Warnings_Off (Source) | |
13249 | or | |
13250 | Has_Warnings_Off (Target)) | |
d6f39728 | 13251 | then |
d6f39728 | 13252 | Error_Msg_Uint_1 := Target_Align; |
13253 | Error_Msg_Uint_2 := Source_Align; | |
299480f9 | 13254 | Error_Msg_Node_1 := D_Target; |
d6f39728 | 13255 | Error_Msg_Node_2 := D_Source; |
299480f9 | 13256 | Error_Msg |
cb97ae5c | 13257 | ("?z?alignment of & (^) is stricter than " |
1e3532e7 | 13258 | & "alignment of & (^)!", Eloc); |
f25f4252 | 13259 | Error_Msg |
cb97ae5c | 13260 | ("\?z?resulting access value may have invalid " |
1e3532e7 | 13261 | & "alignment!", Eloc); |
d6f39728 | 13262 | end if; |
13263 | end; | |
13264 | end if; | |
13265 | end; | |
13266 | end if; | |
13267 | end; | |
86d32751 | 13268 | |
13269 | <<Continue>> | |
13270 | null; | |
d6f39728 | 13271 | end loop; |
13272 | end Validate_Unchecked_Conversions; | |
13273 | ||
d6f39728 | 13274 | end Sem_Ch13; |