]>
Commit | Line | Data |
---|---|---|
996ae0b0 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- S E M _ A G G R -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
8d0d46f4 | 9 | -- Copyright (C) 1992-2021, Free Software Foundation, Inc. -- |
996ae0b0 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
157a9bf5 | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
996ae0b0 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
157a9bf5 ES |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
996ae0b0 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
996ae0b0 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
104f58db BD |
26 | with Aspects; use Aspects; |
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Einfo; use Einfo; | |
76f9c7f4 | 30 | with Einfo.Entities; use Einfo.Entities; |
104f58db BD |
31 | with Einfo.Utils; use Einfo.Utils; |
32 | with Elists; use Elists; | |
33 | with Errout; use Errout; | |
34 | with Expander; use Expander; | |
35 | with Exp_Ch6; use Exp_Ch6; | |
36 | with Exp_Tss; use Exp_Tss; | |
37 | with Exp_Util; use Exp_Util; | |
38 | with Freeze; use Freeze; | |
39 | with Itypes; use Itypes; | |
40 | with Lib; use Lib; | |
41 | with Lib.Xref; use Lib.Xref; | |
42 | with Namet; use Namet; | |
43 | with Namet.Sp; use Namet.Sp; | |
44 | with Nmake; use Nmake; | |
45 | with Nlists; use Nlists; | |
46 | with Opt; use Opt; | |
47 | with Restrict; use Restrict; | |
48 | with Rident; use Rident; | |
49 | with Sem; use Sem; | |
50 | with Sem_Aux; use Sem_Aux; | |
51 | with Sem_Cat; use Sem_Cat; | |
52 | with Sem_Ch3; use Sem_Ch3; | |
53 | with Sem_Ch5; use Sem_Ch5; | |
54 | with Sem_Ch8; use Sem_Ch8; | |
55 | with Sem_Ch13; use Sem_Ch13; | |
56 | with Sem_Dim; use Sem_Dim; | |
57 | with Sem_Eval; use Sem_Eval; | |
58 | with Sem_Res; use Sem_Res; | |
59 | with Sem_Util; use Sem_Util; | |
60 | with Sem_Type; use Sem_Type; | |
61 | with Sem_Warn; use Sem_Warn; | |
62 | with Sinfo; use Sinfo; | |
63 | with Sinfo.Nodes; use Sinfo.Nodes; | |
64 | with Sinfo.Utils; use Sinfo.Utils; | |
65 | with Snames; use Snames; | |
66 | with Stringt; use Stringt; | |
67 | with Stand; use Stand; | |
68 | with Style; use Style; | |
69 | with Targparm; use Targparm; | |
70 | with Tbuild; use Tbuild; | |
71 | with Ttypes; use Ttypes; | |
72 | with Uintp; use Uintp; | |
996ae0b0 | 73 | |
996ae0b0 RK |
74 | package body Sem_Aggr is |
75 | ||
76 | type Case_Bounds is record | |
82893775 AC |
77 | Lo : Node_Id; |
78 | -- Low bound of choice. Once we sort the Case_Table, then entries | |
79 | -- will be in order of ascending Choice_Lo values. | |
80 | ||
81 | Hi : Node_Id; | |
82 | -- High Bound of choice. The sort does not pay any attention to the | |
83 | -- high bound, so choices 1 .. 4 and 1 .. 5 could be in either order. | |
84 | ||
85 | Highest : Uint; | |
86 | -- If there are duplicates or missing entries, then in the sorted | |
87 | -- table, this records the highest value among Choice_Hi values | |
88 | -- seen so far, including this entry. | |
89 | ||
90 | Choice : Node_Id; | |
91 | -- The node of the choice | |
996ae0b0 RK |
92 | end record; |
93 | ||
38f44fd6 PT |
94 | type Case_Table_Type is array (Pos range <>) of Case_Bounds; |
95 | -- Table type used by Check_Case_Choices procedure | |
996ae0b0 RK |
96 | |
97 | ----------------------- | |
98 | -- Local Subprograms -- | |
99 | ----------------------- | |
100 | ||
101 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type); | |
82893775 AC |
102 | -- Sort the Case Table using the Lower Bound of each Choice as the key. A |
103 | -- simple insertion sort is used since the choices in a case statement will | |
104 | -- usually be in near sorted order. | |
996ae0b0 | 105 | |
9b96e234 JM |
106 | procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id); |
107 | -- Ada 2005 (AI-231): Check bad usage of null for a component for which | |
108 | -- null exclusion (NOT NULL) is specified. Typ can be an E_Array_Type for | |
109 | -- the array case (the component type of the array will be used) or an | |
110 | -- E_Component/E_Discriminant entity in the record case, in which case the | |
111 | -- type of the component will be used for the test. If Typ is any other | |
112 | -- kind of entity, the call is ignored. Expr is the component node in the | |
8133b9d1 | 113 | -- aggregate which is known to have a null value. A warning message will be |
9b96e234 JM |
114 | -- issued if the component is null excluding. |
115 | -- | |
116 | -- It would be better to pass the proper type for Typ ??? | |
2820d220 | 117 | |
ca44152f ES |
118 | procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id); |
119 | -- Check that Expr is either not limited or else is one of the cases of | |
120 | -- expressions allowed for a limited component association (namely, an | |
121 | -- aggregate, function call, or <> notation). Report error for violations. | |
480156b2 | 122 | -- Expression is also OK in an instance or inlining context, because we |
812e6118 | 123 | -- have already preanalyzed and it is known to be type correct. |
ca44152f | 124 | |
996ae0b0 RK |
125 | ------------------------------------------------------ |
126 | -- Subprograms used for RECORD AGGREGATE Processing -- | |
127 | ------------------------------------------------------ | |
128 | ||
129 | procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id); | |
130 | -- This procedure performs all the semantic checks required for record | |
131 | -- aggregates. Note that for aggregates analysis and resolution go | |
132 | -- hand in hand. Aggregate analysis has been delayed up to here and | |
133 | -- it is done while resolving the aggregate. | |
134 | -- | |
135 | -- N is the N_Aggregate node. | |
136 | -- Typ is the record type for the aggregate resolution | |
137 | -- | |
9b96e234 JM |
138 | -- While performing the semantic checks, this procedure builds a new |
139 | -- Component_Association_List where each record field appears alone in a | |
140 | -- Component_Choice_List along with its corresponding expression. The | |
141 | -- record fields in the Component_Association_List appear in the same order | |
142 | -- in which they appear in the record type Typ. | |
996ae0b0 | 143 | -- |
9b96e234 JM |
144 | -- Once this new Component_Association_List is built and all the semantic |
145 | -- checks performed, the original aggregate subtree is replaced with the | |
0ad46f04 PT |
146 | -- new named record aggregate just built. This new record aggregate has no |
147 | -- positional associations, so its Expressions field is set to No_List. | |
148 | -- Note that subtree substitution is performed with Rewrite so as to be | |
149 | -- able to retrieve the original aggregate. | |
996ae0b0 RK |
150 | -- |
151 | -- The aggregate subtree manipulation performed by Resolve_Record_Aggregate | |
152 | -- yields the aggregate format expected by Gigi. Typically, this kind of | |
153 | -- tree manipulations are done in the expander. However, because the | |
9b96e234 JM |
154 | -- semantic checks that need to be performed on record aggregates really go |
155 | -- hand in hand with the record aggregate normalization, the aggregate | |
996ae0b0 | 156 | -- subtree transformation is performed during resolution rather than |
9b96e234 JM |
157 | -- expansion. Had we decided otherwise we would have had to duplicate most |
158 | -- of the code in the expansion procedure Expand_Record_Aggregate. Note, | |
c7ce71c2 | 159 | -- however, that all the expansion concerning aggregates for tagged records |
9b96e234 | 160 | -- is done in Expand_Record_Aggregate. |
996ae0b0 RK |
161 | -- |
162 | -- The algorithm of Resolve_Record_Aggregate proceeds as follows: | |
163 | -- | |
164 | -- 1. Make sure that the record type against which the record aggregate | |
c9a1acdc AC |
165 | -- has to be resolved is not abstract. Furthermore if the type is a |
166 | -- null aggregate make sure the input aggregate N is also null. | |
996ae0b0 RK |
167 | -- |
168 | -- 2. Verify that the structure of the aggregate is that of a record | |
169 | -- aggregate. Specifically, look for component associations and ensure | |
170 | -- that each choice list only has identifiers or the N_Others_Choice | |
171 | -- node. Also make sure that if present, the N_Others_Choice occurs | |
172 | -- last and by itself. | |
173 | -- | |
c9a1acdc AC |
174 | -- 3. If Typ contains discriminants, the values for each discriminant is |
175 | -- looked for. If the record type Typ has variants, we check that the | |
176 | -- expressions corresponding to each discriminant ruling the (possibly | |
177 | -- nested) variant parts of Typ, are static. This allows us to determine | |
178 | -- the variant parts to which the rest of the aggregate must conform. | |
179 | -- The names of discriminants with their values are saved in a new | |
180 | -- association list, New_Assoc_List which is later augmented with the | |
181 | -- names and values of the remaining components in the record type. | |
996ae0b0 RK |
182 | -- |
183 | -- During this phase we also make sure that every discriminant is | |
c9a1acdc AC |
184 | -- assigned exactly one value. Note that when several values for a given |
185 | -- discriminant are found, semantic processing continues looking for | |
186 | -- further errors. In this case it's the first discriminant value found | |
187 | -- which we will be recorded. | |
996ae0b0 RK |
188 | -- |
189 | -- IMPORTANT NOTE: For derived tagged types this procedure expects | |
190 | -- First_Discriminant and Next_Discriminant to give the correct list | |
191 | -- of discriminants, in the correct order. | |
192 | -- | |
c9a1acdc AC |
193 | -- 4. After all the discriminant values have been gathered, we can set the |
194 | -- Etype of the record aggregate. If Typ contains no discriminants this | |
195 | -- is straightforward: the Etype of N is just Typ, otherwise a new | |
196 | -- implicit constrained subtype of Typ is built to be the Etype of N. | |
996ae0b0 RK |
197 | -- |
198 | -- 5. Gather the remaining record components according to the discriminant | |
199 | -- values. This involves recursively traversing the record type | |
200 | -- structure to see what variants are selected by the given discriminant | |
201 | -- values. This processing is a little more convoluted if Typ is a | |
202 | -- derived tagged types since we need to retrieve the record structure | |
203 | -- of all the ancestors of Typ. | |
204 | -- | |
c9a1acdc AC |
205 | -- 6. After gathering the record components we look for their values in the |
206 | -- record aggregate and emit appropriate error messages should we not | |
207 | -- find such values or should they be duplicated. | |
208 | -- | |
209 | -- 7. We then make sure no illegal component names appear in the record | |
210 | -- aggregate and make sure that the type of the record components | |
211 | -- appearing in a same choice list is the same. Finally we ensure that | |
212 | -- the others choice, if present, is used to provide the value of at | |
213 | -- least a record component. | |
214 | -- | |
215 | -- 8. The original aggregate node is replaced with the new named aggregate | |
216 | -- built in steps 3 through 6, as explained earlier. | |
217 | -- | |
218 | -- Given the complexity of record aggregate resolution, the primary goal of | |
219 | -- this routine is clarity and simplicity rather than execution and storage | |
220 | -- efficiency. If there are only positional components in the aggregate the | |
221 | -- running time is linear. If there are associations the running time is | |
222 | -- still linear as long as the order of the associations is not too far off | |
223 | -- the order of the components in the record type. If this is not the case | |
224 | -- the running time is at worst quadratic in the size of the association | |
225 | -- list. | |
996ae0b0 RK |
226 | |
227 | procedure Check_Misspelled_Component | |
9c290e69 PO |
228 | (Elements : Elist_Id; |
229 | Component : Node_Id); | |
c9a1acdc AC |
230 | -- Give possible misspelling diagnostic if Component is likely to be a |
231 | -- misspelling of one of the components of the Assoc_List. This is called | |
232 | -- by Resolve_Aggr_Expr after producing an invalid component error message. | |
996ae0b0 | 233 | |
996ae0b0 RK |
234 | ----------------------------------------------------- |
235 | -- Subprograms used for ARRAY AGGREGATE Processing -- | |
236 | ----------------------------------------------------- | |
237 | ||
238 | function Resolve_Array_Aggregate | |
239 | (N : Node_Id; | |
240 | Index : Node_Id; | |
241 | Index_Constr : Node_Id; | |
242 | Component_Typ : Entity_Id; | |
ca44152f | 243 | Others_Allowed : Boolean) return Boolean; |
996ae0b0 RK |
244 | -- This procedure performs the semantic checks for an array aggregate. |
245 | -- True is returned if the aggregate resolution succeeds. | |
ca44152f | 246 | -- |
996ae0b0 | 247 | -- The procedure works by recursively checking each nested aggregate. |
9f4fd324 | 248 | -- Specifically, after checking a sub-aggregate nested at the i-th level |
996ae0b0 RK |
249 | -- we recursively check all the subaggregates at the i+1-st level (if any). |
250 | -- Note that for aggregates analysis and resolution go hand in hand. | |
251 | -- Aggregate analysis has been delayed up to here and it is done while | |
252 | -- resolving the aggregate. | |
253 | -- | |
254 | -- N is the current N_Aggregate node to be checked. | |
255 | -- | |
256 | -- Index is the index node corresponding to the array sub-aggregate that | |
257 | -- we are currently checking (RM 4.3.3 (8)). Its Etype is the | |
258 | -- corresponding index type (or subtype). | |
259 | -- | |
260 | -- Index_Constr is the node giving the applicable index constraint if | |
261 | -- any (RM 4.3.3 (10)). It "is a constraint provided by certain | |
262 | -- contexts [...] that can be used to determine the bounds of the array | |
263 | -- value specified by the aggregate". If Others_Allowed below is False | |
264 | -- there is no applicable index constraint and this node is set to Index. | |
265 | -- | |
266 | -- Component_Typ is the array component type. | |
267 | -- | |
268 | -- Others_Allowed indicates whether an others choice is allowed | |
269 | -- in the context where the top-level aggregate appeared. | |
270 | -- | |
271 | -- The algorithm of Resolve_Array_Aggregate proceeds as follows: | |
272 | -- | |
273 | -- 1. Make sure that the others choice, if present, is by itself and | |
274 | -- appears last in the sub-aggregate. Check that we do not have | |
275 | -- positional and named components in the array sub-aggregate (unless | |
276 | -- the named association is an others choice). Finally if an others | |
12a13f01 | 277 | -- choice is present, make sure it is allowed in the aggregate context. |
996ae0b0 RK |
278 | -- |
279 | -- 2. If the array sub-aggregate contains discrete_choices: | |
280 | -- | |
281 | -- (A) Verify their validity. Specifically verify that: | |
282 | -- | |
283 | -- (a) If a null range is present it must be the only possible | |
284 | -- choice in the array aggregate. | |
285 | -- | |
286 | -- (b) Ditto for a non static range. | |
287 | -- | |
288 | -- (c) Ditto for a non static expression. | |
289 | -- | |
290 | -- In addition this step analyzes and resolves each discrete_choice, | |
291 | -- making sure that its type is the type of the corresponding Index. | |
292 | -- If we are not at the lowest array aggregate level (in the case of | |
293 | -- multi-dimensional aggregates) then invoke Resolve_Array_Aggregate | |
294 | -- recursively on each component expression. Otherwise, resolve the | |
295 | -- bottom level component expressions against the expected component | |
296 | -- type ONLY IF the component corresponds to a single discrete choice | |
297 | -- which is not an others choice (to see why read the DELAYED | |
298 | -- COMPONENT RESOLUTION below). | |
299 | -- | |
300 | -- (B) Determine the bounds of the sub-aggregate and lowest and | |
301 | -- highest choice values. | |
302 | -- | |
303 | -- 3. For positional aggregates: | |
304 | -- | |
305 | -- (A) Loop over the component expressions either recursively invoking | |
306 | -- Resolve_Array_Aggregate on each of these for multi-dimensional | |
307 | -- array aggregates or resolving the bottom level component | |
308 | -- expressions against the expected component type. | |
309 | -- | |
310 | -- (B) Determine the bounds of the positional sub-aggregates. | |
311 | -- | |
312 | -- 4. Try to determine statically whether the evaluation of the array | |
313 | -- sub-aggregate raises Constraint_Error. If yes emit proper | |
314 | -- warnings. The precise checks are the following: | |
315 | -- | |
316 | -- (A) Check that the index range defined by aggregate bounds is | |
317 | -- compatible with corresponding index subtype. | |
318 | -- We also check against the base type. In fact it could be that | |
319 | -- Low/High bounds of the base type are static whereas those of | |
320 | -- the index subtype are not. Thus if we can statically catch | |
321 | -- a problem with respect to the base type we are guaranteed | |
322 | -- that the same problem will arise with the index subtype | |
323 | -- | |
324 | -- (B) If we are dealing with a named aggregate containing an others | |
325 | -- choice and at least one discrete choice then make sure the range | |
326 | -- specified by the discrete choices does not overflow the | |
327 | -- aggregate bounds. We also check against the index type and base | |
328 | -- type bounds for the same reasons given in (A). | |
329 | -- | |
330 | -- (C) If we are dealing with a positional aggregate with an others | |
331 | -- choice make sure the number of positional elements specified | |
332 | -- does not overflow the aggregate bounds. We also check against | |
333 | -- the index type and base type bounds as mentioned in (A). | |
334 | -- | |
335 | -- Finally construct an N_Range node giving the sub-aggregate bounds. | |
336 | -- Set the Aggregate_Bounds field of the sub-aggregate to be this | |
337 | -- N_Range. The routine Array_Aggr_Subtype below uses such N_Ranges | |
338 | -- to build the appropriate aggregate subtype. Aggregate_Bounds | |
339 | -- information is needed during expansion. | |
340 | -- | |
341 | -- DELAYED COMPONENT RESOLUTION: The resolution of bottom level component | |
342 | -- expressions in an array aggregate may call Duplicate_Subexpr or some | |
343 | -- other routine that inserts code just outside the outermost aggregate. | |
344 | -- If the array aggregate contains discrete choices or an others choice, | |
345 | -- this may be wrong. Consider for instance the following example. | |
346 | -- | |
347 | -- type Rec is record | |
348 | -- V : Integer := 0; | |
349 | -- end record; | |
350 | -- | |
351 | -- type Acc_Rec is access Rec; | |
352 | -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => new Rec); | |
353 | -- | |
354 | -- Then the transformation of "new Rec" that occurs during resolution | |
355 | -- entails the following code modifications | |
356 | -- | |
357 | -- P7b : constant Acc_Rec := new Rec; | |
fbf5a39b | 358 | -- RecIP (P7b.all); |
996ae0b0 RK |
359 | -- Arr : array (1..3) of Acc_Rec := (1 .. 3 => P7b); |
360 | -- | |
361 | -- This code transformation is clearly wrong, since we need to call | |
362 | -- "new Rec" for each of the 3 array elements. To avoid this problem we | |
363 | -- delay resolution of the components of non positional array aggregates | |
364 | -- to the expansion phase. As an optimization, if the discrete choice | |
365 | -- specifies a single value we do not delay resolution. | |
366 | ||
367 | function Array_Aggr_Subtype (N : Node_Id; Typ : Node_Id) return Entity_Id; | |
368 | -- This routine returns the type or subtype of an array aggregate. | |
369 | -- | |
370 | -- N is the array aggregate node whose type we return. | |
371 | -- | |
372 | -- Typ is the context type in which N occurs. | |
373 | -- | |
c45b6ae0 | 374 | -- This routine creates an implicit array subtype whose bounds are |
996ae0b0 RK |
375 | -- those defined by the aggregate. When this routine is invoked |
376 | -- Resolve_Array_Aggregate has already processed aggregate N. Thus the | |
377 | -- Aggregate_Bounds of each sub-aggregate, is an N_Range node giving the | |
c7ce71c2 | 378 | -- sub-aggregate bounds. When building the aggregate itype, this function |
996ae0b0 RK |
379 | -- traverses the array aggregate N collecting such Aggregate_Bounds and |
380 | -- constructs the proper array aggregate itype. | |
381 | -- | |
382 | -- Note that in the case of multidimensional aggregates each inner | |
383 | -- sub-aggregate corresponding to a given array dimension, may provide a | |
384 | -- different bounds. If it is possible to determine statically that | |
385 | -- some sub-aggregates corresponding to the same index do not have the | |
386 | -- same bounds, then a warning is emitted. If such check is not possible | |
387 | -- statically (because some sub-aggregate bounds are dynamic expressions) | |
388 | -- then this job is left to the expander. In all cases the particular | |
389 | -- bounds that this function will chose for a given dimension is the first | |
390 | -- N_Range node for a sub-aggregate corresponding to that dimension. | |
391 | -- | |
392 | -- Note that the Raises_Constraint_Error flag of an array aggregate | |
393 | -- whose evaluation is determined to raise CE by Resolve_Array_Aggregate, | |
394 | -- is set in Resolve_Array_Aggregate but the aggregate is not | |
395 | -- immediately replaced with a raise CE. In fact, Array_Aggr_Subtype must | |
396 | -- first construct the proper itype for the aggregate (Gigi needs | |
21d7ef70 | 397 | -- this). After constructing the proper itype we will eventually replace |
996ae0b0 RK |
398 | -- the top-level aggregate with a raise CE (done in Resolve_Aggregate). |
399 | -- Of course in cases such as: | |
400 | -- | |
401 | -- type Arr is array (integer range <>) of Integer; | |
402 | -- A : Arr := (positive range -1 .. 2 => 0); | |
403 | -- | |
404 | -- The bounds of the aggregate itype are cooked up to look reasonable | |
405 | -- (in this particular case the bounds will be 1 .. 2). | |
406 | ||
996ae0b0 | 407 | procedure Make_String_Into_Aggregate (N : Node_Id); |
21d7ef70 | 408 | -- A string literal can appear in a context in which a one dimensional |
996ae0b0 RK |
409 | -- array of characters is expected. This procedure simply rewrites the |
410 | -- string as an aggregate, prior to resolution. | |
411 | ||
8d9a1ba7 PMR |
412 | --------------------------------- |
413 | -- Delta aggregate processing -- | |
414 | --------------------------------- | |
415 | ||
416 | procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id); | |
417 | procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id); | |
418 | ||
996ae0b0 RK |
419 | ------------------------ |
420 | -- Array_Aggr_Subtype -- | |
421 | ------------------------ | |
422 | ||
423 | function Array_Aggr_Subtype | |
b87971f3 AC |
424 | (N : Node_Id; |
425 | Typ : Entity_Id) return Entity_Id | |
996ae0b0 RK |
426 | is |
427 | Aggr_Dimension : constant Pos := Number_Dimensions (Typ); | |
ec53a6da | 428 | -- Number of aggregate index dimensions |
996ae0b0 RK |
429 | |
430 | Aggr_Range : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty); | |
ec53a6da | 431 | -- Constrained N_Range of each index dimension in our aggregate itype |
996ae0b0 | 432 | |
58009744 AC |
433 | Aggr_Low : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty); |
434 | Aggr_High : array (1 .. Aggr_Dimension) of Node_Id := (others => Empty); | |
ec53a6da | 435 | -- Low and High bounds for each index dimension in our aggregate itype |
996ae0b0 RK |
436 | |
437 | Is_Fully_Positional : Boolean := True; | |
438 | ||
439 | procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos); | |
fb468a94 AC |
440 | -- N is an array (sub-)aggregate. Dim is the dimension corresponding |
441 | -- to (sub-)aggregate N. This procedure collects and removes the side | |
442 | -- effects of the constrained N_Range nodes corresponding to each index | |
2b3d67a5 AC |
443 | -- dimension of our aggregate itype. These N_Range nodes are collected |
444 | -- in Aggr_Range above. | |
ec53a6da | 445 | -- |
996ae0b0 RK |
446 | -- Likewise collect in Aggr_Low & Aggr_High above the low and high |
447 | -- bounds of each index dimension. If, when collecting, two bounds | |
448 | -- corresponding to the same dimension are static and found to differ, | |
449 | -- then emit a warning, and mark N as raising Constraint_Error. | |
450 | ||
451 | ------------------------- | |
452 | -- Collect_Aggr_Bounds -- | |
453 | ------------------------- | |
454 | ||
455 | procedure Collect_Aggr_Bounds (N : Node_Id; Dim : Pos) is | |
456 | This_Range : constant Node_Id := Aggregate_Bounds (N); | |
ec53a6da | 457 | -- The aggregate range node of this specific sub-aggregate |
996ae0b0 | 458 | |
fb00cc70 | 459 | This_Low : constant Node_Id := Low_Bound (Aggregate_Bounds (N)); |
996ae0b0 | 460 | This_High : constant Node_Id := High_Bound (Aggregate_Bounds (N)); |
ec53a6da | 461 | -- The aggregate bounds of this specific sub-aggregate |
996ae0b0 RK |
462 | |
463 | Assoc : Node_Id; | |
464 | Expr : Node_Id; | |
465 | ||
466 | begin | |
fb468a94 AC |
467 | Remove_Side_Effects (This_Low, Variable_Ref => True); |
468 | Remove_Side_Effects (This_High, Variable_Ref => True); | |
469 | ||
996ae0b0 RK |
470 | -- Collect the first N_Range for a given dimension that you find. |
471 | -- For a given dimension they must be all equal anyway. | |
472 | ||
473 | if No (Aggr_Range (Dim)) then | |
474 | Aggr_Low (Dim) := This_Low; | |
475 | Aggr_High (Dim) := This_High; | |
476 | Aggr_Range (Dim) := This_Range; | |
477 | ||
478 | else | |
479 | if Compile_Time_Known_Value (This_Low) then | |
480 | if not Compile_Time_Known_Value (Aggr_Low (Dim)) then | |
be035558 | 481 | Aggr_Low (Dim) := This_Low; |
996ae0b0 RK |
482 | |
483 | elsif Expr_Value (This_Low) /= Expr_Value (Aggr_Low (Dim)) then | |
484 | Set_Raises_Constraint_Error (N); | |
43417b90 | 485 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
486 | Error_Msg_N ("sub-aggregate low bound mismatch<<", N); |
487 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
488 | end if; |
489 | end if; | |
490 | ||
491 | if Compile_Time_Known_Value (This_High) then | |
492 | if not Compile_Time_Known_Value (Aggr_High (Dim)) then | |
be035558 | 493 | Aggr_High (Dim) := This_High; |
996ae0b0 RK |
494 | |
495 | elsif | |
496 | Expr_Value (This_High) /= Expr_Value (Aggr_High (Dim)) | |
497 | then | |
498 | Set_Raises_Constraint_Error (N); | |
43417b90 | 499 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
500 | Error_Msg_N ("sub-aggregate high bound mismatch<<", N); |
501 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
502 | end if; |
503 | end if; | |
504 | end if; | |
505 | ||
506 | if Dim < Aggr_Dimension then | |
507 | ||
508 | -- Process positional components | |
509 | ||
510 | if Present (Expressions (N)) then | |
511 | Expr := First (Expressions (N)); | |
512 | while Present (Expr) loop | |
513 | Collect_Aggr_Bounds (Expr, Dim + 1); | |
514 | Next (Expr); | |
515 | end loop; | |
516 | end if; | |
517 | ||
518 | -- Process component associations | |
519 | ||
520 | if Present (Component_Associations (N)) then | |
521 | Is_Fully_Positional := False; | |
522 | ||
523 | Assoc := First (Component_Associations (N)); | |
524 | while Present (Assoc) loop | |
525 | Expr := Expression (Assoc); | |
526 | Collect_Aggr_Bounds (Expr, Dim + 1); | |
527 | Next (Assoc); | |
528 | end loop; | |
529 | end if; | |
530 | end if; | |
531 | end Collect_Aggr_Bounds; | |
532 | ||
533 | -- Array_Aggr_Subtype variables | |
534 | ||
535 | Itype : Entity_Id; | |
b87971f3 | 536 | -- The final itype of the overall aggregate |
996ae0b0 | 537 | |
fbf5a39b | 538 | Index_Constraints : constant List_Id := New_List; |
ec53a6da | 539 | -- The list of index constraints of the aggregate itype |
996ae0b0 RK |
540 | |
541 | -- Start of processing for Array_Aggr_Subtype | |
542 | ||
543 | begin | |
b87971f3 AC |
544 | -- Make sure that the list of index constraints is properly attached to |
545 | -- the tree, and then collect the aggregate bounds. | |
996ae0b0 RK |
546 | |
547 | Set_Parent (Index_Constraints, N); | |
548 | Collect_Aggr_Bounds (N, 1); | |
549 | ||
3b42c566 | 550 | -- Build the list of constrained indexes of our aggregate itype |
996ae0b0 RK |
551 | |
552 | for J in 1 .. Aggr_Dimension loop | |
553 | Create_Index : declare | |
fbf5a39b AC |
554 | Index_Base : constant Entity_Id := |
555 | Base_Type (Etype (Aggr_Range (J))); | |
996ae0b0 RK |
556 | Index_Typ : Entity_Id; |
557 | ||
558 | begin | |
8133b9d1 ES |
559 | -- Construct the Index subtype, and associate it with the range |
560 | -- construct that generates it. | |
996ae0b0 | 561 | |
8133b9d1 ES |
562 | Index_Typ := |
563 | Create_Itype (Subtype_Kind (Ekind (Index_Base)), Aggr_Range (J)); | |
996ae0b0 RK |
564 | |
565 | Set_Etype (Index_Typ, Index_Base); | |
566 | ||
567 | if Is_Character_Type (Index_Base) then | |
568 | Set_Is_Character_Type (Index_Typ); | |
569 | end if; | |
570 | ||
571 | Set_Size_Info (Index_Typ, (Index_Base)); | |
572 | Set_RM_Size (Index_Typ, RM_Size (Index_Base)); | |
573 | Set_First_Rep_Item (Index_Typ, First_Rep_Item (Index_Base)); | |
574 | Set_Scalar_Range (Index_Typ, Aggr_Range (J)); | |
575 | ||
576 | if Is_Discrete_Or_Fixed_Point_Type (Index_Typ) then | |
577 | Set_RM_Size (Index_Typ, UI_From_Int (Minimum_Size (Index_Typ))); | |
578 | end if; | |
579 | ||
580 | Set_Etype (Aggr_Range (J), Index_Typ); | |
581 | ||
582 | Append (Aggr_Range (J), To => Index_Constraints); | |
583 | end Create_Index; | |
584 | end loop; | |
585 | ||
586 | -- Now build the Itype | |
587 | ||
588 | Itype := Create_Itype (E_Array_Subtype, N); | |
589 | ||
b87971f3 AC |
590 | Set_First_Rep_Item (Itype, First_Rep_Item (Typ)); |
591 | Set_Convention (Itype, Convention (Typ)); | |
592 | Set_Depends_On_Private (Itype, Has_Private_Component (Typ)); | |
593 | Set_Etype (Itype, Base_Type (Typ)); | |
594 | Set_Has_Alignment_Clause (Itype, Has_Alignment_Clause (Typ)); | |
595 | Set_Is_Aliased (Itype, Is_Aliased (Typ)); | |
a517d6c1 | 596 | Set_Is_Independent (Itype, Is_Independent (Typ)); |
b87971f3 | 597 | Set_Depends_On_Private (Itype, Depends_On_Private (Typ)); |
996ae0b0 | 598 | |
fbf5a39b AC |
599 | Copy_Suppress_Status (Index_Check, Typ, Itype); |
600 | Copy_Suppress_Status (Length_Check, Typ, Itype); | |
601 | ||
996ae0b0 RK |
602 | Set_First_Index (Itype, First (Index_Constraints)); |
603 | Set_Is_Constrained (Itype, True); | |
604 | Set_Is_Internal (Itype, True); | |
996ae0b0 | 605 | |
619bfd9f ES |
606 | if Has_Predicates (Typ) then |
607 | Set_Has_Predicates (Itype); | |
608 | ||
a096f12e ES |
609 | -- If the base type has a predicate, capture the predicated parent |
610 | -- or the existing predicate function for SPARK use. | |
611 | ||
619bfd9f ES |
612 | if Present (Predicate_Function (Typ)) then |
613 | Set_Predicate_Function (Itype, Predicate_Function (Typ)); | |
a096f12e ES |
614 | |
615 | elsif Is_Itype (Typ) then | |
619bfd9f | 616 | Set_Predicated_Parent (Itype, Predicated_Parent (Typ)); |
a096f12e ES |
617 | |
618 | else | |
619 | Set_Predicated_Parent (Itype, Typ); | |
619bfd9f ES |
620 | end if; |
621 | end if; | |
622 | ||
996ae0b0 | 623 | -- A simple optimization: purely positional aggregates of static |
b87971f3 AC |
624 | -- components should be passed to gigi unexpanded whenever possible, and |
625 | -- regardless of the staticness of the bounds themselves. Subsequent | |
626 | -- checks in exp_aggr verify that type is not packed, etc. | |
996ae0b0 | 627 | |
58009744 AC |
628 | Set_Size_Known_At_Compile_Time |
629 | (Itype, | |
8133b9d1 ES |
630 | Is_Fully_Positional |
631 | and then Comes_From_Source (N) | |
632 | and then Size_Known_At_Compile_Time (Component_Type (Typ))); | |
996ae0b0 | 633 | |
b87971f3 | 634 | -- We always need a freeze node for a packed array subtype, so that we |
8ca597af | 635 | -- can build the Packed_Array_Impl_Type corresponding to the subtype. If |
b87971f3 AC |
636 | -- expansion is disabled, the packed array subtype is not built, and we |
637 | -- must not generate a freeze node for the type, or else it will appear | |
638 | -- incomplete to gigi. | |
996ae0b0 | 639 | |
b87971f3 AC |
640 | if Is_Packed (Itype) |
641 | and then not In_Spec_Expression | |
996ae0b0 RK |
642 | and then Expander_Active |
643 | then | |
644 | Freeze_Itype (Itype, N); | |
645 | end if; | |
646 | ||
647 | return Itype; | |
648 | end Array_Aggr_Subtype; | |
649 | ||
650 | -------------------------------- | |
651 | -- Check_Misspelled_Component -- | |
652 | -------------------------------- | |
653 | ||
654 | procedure Check_Misspelled_Component | |
9c290e69 PO |
655 | (Elements : Elist_Id; |
656 | Component : Node_Id) | |
996ae0b0 RK |
657 | is |
658 | Max_Suggestions : constant := 2; | |
659 | ||
660 | Nr_Of_Suggestions : Natural := 0; | |
661 | Suggestion_1 : Entity_Id := Empty; | |
662 | Suggestion_2 : Entity_Id := Empty; | |
663 | Component_Elmt : Elmt_Id; | |
664 | ||
665 | begin | |
b87971f3 | 666 | -- All the components of List are matched against Component and a count |
e49de265 | 667 | -- is maintained of possible misspellings. When at the end of the |
a90bd866 | 668 | -- analysis there are one or two (not more) possible misspellings, |
e49de265 | 669 | -- these misspellings will be suggested as possible corrections. |
996ae0b0 | 670 | |
c80d4855 RD |
671 | Component_Elmt := First_Elmt (Elements); |
672 | while Nr_Of_Suggestions <= Max_Suggestions | |
673 | and then Present (Component_Elmt) | |
674 | loop | |
675 | if Is_Bad_Spelling_Of | |
676 | (Chars (Node (Component_Elmt)), | |
677 | Chars (Component)) | |
678 | then | |
679 | Nr_Of_Suggestions := Nr_Of_Suggestions + 1; | |
996ae0b0 | 680 | |
c80d4855 RD |
681 | case Nr_Of_Suggestions is |
682 | when 1 => Suggestion_1 := Node (Component_Elmt); | |
683 | when 2 => Suggestion_2 := Node (Component_Elmt); | |
e49de265 | 684 | when others => null; |
c80d4855 RD |
685 | end case; |
686 | end if; | |
996ae0b0 | 687 | |
c80d4855 RD |
688 | Next_Elmt (Component_Elmt); |
689 | end loop; | |
996ae0b0 | 690 | |
c80d4855 | 691 | -- Report at most two suggestions |
996ae0b0 | 692 | |
c80d4855 | 693 | if Nr_Of_Suggestions = 1 then |
4e7a4f6e | 694 | Error_Msg_NE -- CODEFIX |
c80d4855 | 695 | ("\possible misspelling of&", Component, Suggestion_1); |
996ae0b0 | 696 | |
c80d4855 RD |
697 | elsif Nr_Of_Suggestions = 2 then |
698 | Error_Msg_Node_2 := Suggestion_2; | |
4e7a4f6e | 699 | Error_Msg_NE -- CODEFIX |
c80d4855 RD |
700 | ("\possible misspelling of& or&", Component, Suggestion_1); |
701 | end if; | |
996ae0b0 RK |
702 | end Check_Misspelled_Component; |
703 | ||
ca44152f ES |
704 | ---------------------------------------- |
705 | -- Check_Expr_OK_In_Limited_Aggregate -- | |
706 | ---------------------------------------- | |
707 | ||
708 | procedure Check_Expr_OK_In_Limited_Aggregate (Expr : Node_Id) is | |
709 | begin | |
710 | if Is_Limited_Type (Etype (Expr)) | |
711 | and then Comes_From_Source (Expr) | |
ca44152f | 712 | then |
36428cc4 AC |
713 | if In_Instance_Body or else In_Inlined_Body then |
714 | null; | |
715 | ||
716 | elsif not OK_For_Limited_Init (Etype (Expr), Expr) then | |
717 | Error_Msg_N | |
718 | ("initialization not allowed for limited types", Expr); | |
ca44152f ES |
719 | Explain_Limited_Type (Etype (Expr), Expr); |
720 | end if; | |
721 | end if; | |
722 | end Check_Expr_OK_In_Limited_Aggregate; | |
723 | ||
9f90d123 AC |
724 | ------------------------- |
725 | -- Is_Others_Aggregate -- | |
726 | ------------------------- | |
727 | ||
728 | function Is_Others_Aggregate (Aggr : Node_Id) return Boolean is | |
27c3d986 EB |
729 | Assoc : constant List_Id := Component_Associations (Aggr); |
730 | ||
9f90d123 AC |
731 | begin |
732 | return No (Expressions (Aggr)) | |
27c3d986 | 733 | and then Nkind (First (Choice_List (First (Assoc)))) = N_Others_Choice; |
9f90d123 AC |
734 | end Is_Others_Aggregate; |
735 | ||
27c3d986 EB |
736 | ------------------------- |
737 | -- Is_Single_Aggregate -- | |
738 | ------------------------- | |
739 | ||
740 | function Is_Single_Aggregate (Aggr : Node_Id) return Boolean is | |
741 | Assoc : constant List_Id := Component_Associations (Aggr); | |
742 | ||
743 | begin | |
744 | return No (Expressions (Aggr)) | |
745 | and then No (Next (First (Assoc))) | |
746 | and then No (Next (First (Choice_List (First (Assoc))))); | |
747 | end Is_Single_Aggregate; | |
748 | ||
996ae0b0 RK |
749 | -------------------------------- |
750 | -- Make_String_Into_Aggregate -- | |
751 | -------------------------------- | |
752 | ||
753 | procedure Make_String_Into_Aggregate (N : Node_Id) is | |
fbf5a39b | 754 | Exprs : constant List_Id := New_List; |
996ae0b0 | 755 | Loc : constant Source_Ptr := Sloc (N); |
996ae0b0 RK |
756 | Str : constant String_Id := Strval (N); |
757 | Strlen : constant Nat := String_Length (Str); | |
fbf5a39b AC |
758 | C : Char_Code; |
759 | C_Node : Node_Id; | |
760 | New_N : Node_Id; | |
761 | P : Source_Ptr; | |
996ae0b0 RK |
762 | |
763 | begin | |
fbf5a39b | 764 | P := Loc + 1; |
996ae0b0 RK |
765 | for J in 1 .. Strlen loop |
766 | C := Get_String_Char (Str, J); | |
767 | Set_Character_Literal_Name (C); | |
768 | ||
82c80734 RD |
769 | C_Node := |
770 | Make_Character_Literal (P, | |
771 | Chars => Name_Find, | |
772 | Char_Literal_Value => UI_From_CC (C)); | |
996ae0b0 | 773 | Set_Etype (C_Node, Any_Character); |
996ae0b0 RK |
774 | Append_To (Exprs, C_Node); |
775 | ||
776 | P := P + 1; | |
b87971f3 | 777 | -- Something special for wide strings??? |
996ae0b0 RK |
778 | end loop; |
779 | ||
780 | New_N := Make_Aggregate (Loc, Expressions => Exprs); | |
781 | Set_Analyzed (New_N); | |
782 | Set_Etype (New_N, Any_Composite); | |
783 | ||
784 | Rewrite (N, New_N); | |
785 | end Make_String_Into_Aggregate; | |
786 | ||
787 | ----------------------- | |
788 | -- Resolve_Aggregate -- | |
789 | ----------------------- | |
790 | ||
791 | procedure Resolve_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
fb00cc70 | 792 | Loc : constant Source_Ptr := Sloc (N); |
996ae0b0 RK |
793 | |
794 | Aggr_Subtyp : Entity_Id; | |
795 | -- The actual aggregate subtype. This is not necessarily the same as Typ | |
796 | -- which is the subtype of the context in which the aggregate was found. | |
797 | ||
66b69678 AC |
798 | Others_Box : Boolean := False; |
799 | -- Set to True if N represents a simple aggregate with only | |
800 | -- (others => <>), not nested as part of another aggregate. | |
801 | ||
802 | function Within_Aggregate (N : Node_Id) return Boolean; | |
803 | -- Return True if N is part of an N_Aggregate | |
804 | ||
805 | ---------------------- | |
806 | -- Within_Aggregate -- | |
807 | ---------------------- | |
808 | ||
809 | function Within_Aggregate (N : Node_Id) return Boolean is | |
810 | P : Node_Id := Parent (N); | |
811 | begin | |
812 | while Present (P) loop | |
813 | if Nkind (P) = N_Aggregate then | |
814 | return True; | |
815 | end if; | |
816 | ||
817 | P := Parent (P); | |
818 | end loop; | |
819 | ||
820 | return False; | |
821 | end Within_Aggregate; | |
822 | ||
fb00cc70 ES |
823 | -- Start of processing for Resolve_Aggregate |
824 | ||
996ae0b0 | 825 | begin |
6d2a1120 RD |
826 | -- Ignore junk empty aggregate resulting from parser error |
827 | ||
828 | if No (Expressions (N)) | |
829 | and then No (Component_Associations (N)) | |
830 | and then not Null_Record_Present (N) | |
831 | then | |
832 | return; | |
833 | end if; | |
834 | ||
0180fd26 AC |
835 | -- If the aggregate has box-initialized components, its type must be |
836 | -- frozen so that initialization procedures can properly be called | |
64ac53f4 | 837 | -- in the resolution that follows. The replacement of boxes with |
0180fd26 | 838 | -- initialization calls is properly an expansion activity but it must |
f5da7a97 | 839 | -- be done during resolution. |
0180fd26 AC |
840 | |
841 | if Expander_Active | |
f5da7a97 | 842 | and then Present (Component_Associations (N)) |
0180fd26 AC |
843 | then |
844 | declare | |
66b69678 AC |
845 | Comp : Node_Id; |
846 | First_Comp : Boolean := True; | |
0180fd26 AC |
847 | |
848 | begin | |
849 | Comp := First (Component_Associations (N)); | |
850 | while Present (Comp) loop | |
851 | if Box_Present (Comp) then | |
66b69678 AC |
852 | if First_Comp |
853 | and then No (Expressions (N)) | |
854 | and then Nkind (First (Choices (Comp))) = N_Others_Choice | |
855 | and then not Within_Aggregate (N) | |
856 | then | |
857 | Others_Box := True; | |
858 | end if; | |
859 | ||
0180fd26 AC |
860 | Insert_Actions (N, Freeze_Entity (Typ, N)); |
861 | exit; | |
862 | end if; | |
fe0ec02f | 863 | |
66b69678 | 864 | First_Comp := False; |
0180fd26 AC |
865 | Next (Comp); |
866 | end loop; | |
867 | end; | |
868 | end if; | |
869 | ||
fbf5a39b | 870 | -- Check for aggregates not allowed in configurable run-time mode. |
b87971f3 AC |
871 | -- We allow all cases of aggregates that do not come from source, since |
872 | -- these are all assumed to be small (e.g. bounds of a string literal). | |
873 | -- We also allow aggregates of types we know to be small. | |
fbf5a39b AC |
874 | |
875 | if not Support_Aggregates_On_Target | |
876 | and then Comes_From_Source (N) | |
c7c7dd3a EB |
877 | and then (not Known_Static_Esize (Typ) |
878 | or else Esize (Typ) > System_Max_Integer_Size) | |
fbf5a39b AC |
879 | then |
880 | Error_Msg_CRT ("aggregate", N); | |
881 | end if; | |
996ae0b0 | 882 | |
0ab80019 | 883 | -- Ada 2005 (AI-287): Limited aggregates allowed |
579fda56 | 884 | |
67645bde AC |
885 | -- In an instance, ignore aggregate subcomponents tnat may be limited, |
886 | -- because they originate in view conflicts. If the original aggregate | |
887 | -- is legal and the actuals are legal, the aggregate itself is legal. | |
19f0526a | 888 | |
67645bde AC |
889 | if Is_Limited_Type (Typ) |
890 | and then Ada_Version < Ada_2005 | |
891 | and then not In_Instance | |
892 | then | |
fbf5a39b AC |
893 | Error_Msg_N ("aggregate type cannot be limited", N); |
894 | Explain_Limited_Type (Typ, N); | |
996ae0b0 RK |
895 | |
896 | elsif Is_Class_Wide_Type (Typ) then | |
897 | Error_Msg_N ("type of aggregate cannot be class-wide", N); | |
898 | ||
899 | elsif Typ = Any_String | |
900 | or else Typ = Any_Composite | |
901 | then | |
902 | Error_Msg_N ("no unique type for aggregate", N); | |
903 | Set_Etype (N, Any_Composite); | |
904 | ||
905 | elsif Is_Array_Type (Typ) and then Null_Record_Present (N) then | |
906 | Error_Msg_N ("null record forbidden in array aggregate", N); | |
907 | ||
3bb4836f ES |
908 | elsif Present (Find_Aspect (Typ, Aspect_Aggregate)) |
909 | and then Ekind (Typ) /= E_Record_Type | |
81e68a19 | 910 | and then Ada_Version >= Ada_2022 |
3bb4836f ES |
911 | then |
912 | Resolve_Container_Aggregate (N, Typ); | |
913 | ||
996ae0b0 RK |
914 | elsif Is_Record_Type (Typ) then |
915 | Resolve_Record_Aggregate (N, Typ); | |
916 | ||
917 | elsif Is_Array_Type (Typ) then | |
918 | ||
54740d7d AC |
919 | -- First a special test, for the case of a positional aggregate of |
920 | -- characters which can be replaced by a string literal. | |
ca44152f | 921 | |
54740d7d AC |
922 | -- Do not perform this transformation if this was a string literal |
923 | -- to start with, whose components needed constraint checks, or if | |
924 | -- the component type is non-static, because it will require those | |
925 | -- checks and be transformed back into an aggregate. If the index | |
926 | -- type is not Integer the aggregate may represent a user-defined | |
927 | -- string type but the context might need the original type so we | |
928 | -- do not perform the transformation at this point. | |
996ae0b0 RK |
929 | |
930 | if Number_Dimensions (Typ) = 1 | |
ca44152f | 931 | and then Is_Standard_Character_Type (Component_Type (Typ)) |
996ae0b0 RK |
932 | and then No (Component_Associations (N)) |
933 | and then not Is_Limited_Composite (Typ) | |
934 | and then not Is_Private_Composite (Typ) | |
935 | and then not Is_Bit_Packed_Array (Typ) | |
936 | and then Nkind (Original_Node (Parent (N))) /= N_String_Literal | |
edab6088 | 937 | and then Is_OK_Static_Subtype (Component_Type (Typ)) |
54740d7d AC |
938 | and then Base_Type (Etype (First_Index (Typ))) = |
939 | Base_Type (Standard_Integer) | |
996ae0b0 RK |
940 | then |
941 | declare | |
942 | Expr : Node_Id; | |
943 | ||
944 | begin | |
945 | Expr := First (Expressions (N)); | |
946 | while Present (Expr) loop | |
947 | exit when Nkind (Expr) /= N_Character_Literal; | |
948 | Next (Expr); | |
949 | end loop; | |
950 | ||
951 | if No (Expr) then | |
952 | Start_String; | |
953 | ||
954 | Expr := First (Expressions (N)); | |
955 | while Present (Expr) loop | |
82c80734 | 956 | Store_String_Char (UI_To_CC (Char_Literal_Value (Expr))); |
996ae0b0 RK |
957 | Next (Expr); |
958 | end loop; | |
959 | ||
f915704f | 960 | Rewrite (N, Make_String_Literal (Loc, End_String)); |
996ae0b0 RK |
961 | |
962 | Analyze_And_Resolve (N, Typ); | |
963 | return; | |
964 | end if; | |
965 | end; | |
966 | end if; | |
967 | ||
968 | -- Here if we have a real aggregate to deal with | |
969 | ||
970 | Array_Aggregate : declare | |
971 | Aggr_Resolved : Boolean; | |
fbf5a39b AC |
972 | |
973 | Aggr_Typ : constant Entity_Id := Etype (Typ); | |
b87971f3 AC |
974 | -- This is the unconstrained array type, which is the type against |
975 | -- which the aggregate is to be resolved. Typ itself is the array | |
976 | -- type of the context which may not be the same subtype as the | |
977 | -- subtype for the final aggregate. | |
996ae0b0 RK |
978 | |
979 | begin | |
f915704f | 980 | -- In the following we determine whether an OTHERS choice is |
996ae0b0 RK |
981 | -- allowed inside the array aggregate. The test checks the context |
982 | -- in which the array aggregate occurs. If the context does not | |
f915704f | 983 | -- permit it, or the aggregate type is unconstrained, an OTHERS |
56e94186 AC |
984 | -- choice is not allowed (except that it is always allowed on the |
985 | -- right-hand side of an assignment statement; in this case the | |
f18344b7 BD |
986 | -- constrainedness of the type doesn't matter, because an array |
987 | -- object is always constrained). | |
d8387153 ES |
988 | |
989 | -- If expansion is disabled (generic context, or semantics-only | |
b87971f3 AC |
990 | -- mode) actual subtypes cannot be constructed, and the type of an |
991 | -- object may be its unconstrained nominal type. However, if the | |
f18344b7 BD |
992 | -- context is an assignment statement, OTHERS is allowed, because |
993 | -- the target of the assignment will have a constrained subtype | |
994 | -- when fully compiled. Ditto if the context is an initialization | |
995 | -- procedure where a component may have a predicate function that | |
996 | -- carries the base type. | |
d8387153 | 997 | |
996ae0b0 RK |
998 | -- Note that there is no node for Explicit_Actual_Parameter. |
999 | -- To test for this context we therefore have to test for node | |
1000 | -- N_Parameter_Association which itself appears only if there is a | |
1001 | -- formal parameter. Consequently we also need to test for | |
1002 | -- N_Procedure_Call_Statement or N_Function_Call. | |
1003 | ||
5f6fb720 AC |
1004 | -- The context may be an N_Reference node, created by expansion. |
1005 | -- Legality of the others clause was established in the source, | |
1006 | -- so the context is legal. | |
1007 | ||
b87971f3 | 1008 | Set_Etype (N, Aggr_Typ); -- May be overridden later on |
c45b6ae0 | 1009 | |
f18344b7 | 1010 | if Nkind (Parent (N)) = N_Assignment_Statement |
ffdd5248 | 1011 | or else Inside_Init_Proc |
e917aec2 | 1012 | or else (Is_Constrained (Typ) |
4a08c95c AC |
1013 | and then Nkind (Parent (N)) in |
1014 | N_Parameter_Association | |
1015 | | N_Function_Call | |
1016 | | N_Procedure_Call_Statement | |
1017 | | N_Generic_Association | |
1018 | | N_Formal_Object_Declaration | |
1019 | | N_Simple_Return_Statement | |
1020 | | N_Object_Declaration | |
1021 | | N_Component_Declaration | |
1022 | | N_Parameter_Specification | |
1023 | | N_Qualified_Expression | |
1024 | | N_Reference | |
1025 | | N_Aggregate | |
1026 | | N_Extension_Aggregate | |
1027 | | N_Component_Association | |
1028 | | N_Case_Expression_Alternative | |
1029 | | N_If_Expression | |
1030 | | N_Expression_With_Actions) | |
996ae0b0 RK |
1031 | then |
1032 | Aggr_Resolved := | |
1033 | Resolve_Array_Aggregate | |
1034 | (N, | |
1035 | Index => First_Index (Aggr_Typ), | |
1036 | Index_Constr => First_Index (Typ), | |
1037 | Component_Typ => Component_Type (Typ), | |
1038 | Others_Allowed => True); | |
996ae0b0 RK |
1039 | else |
1040 | Aggr_Resolved := | |
1041 | Resolve_Array_Aggregate | |
1042 | (N, | |
1043 | Index => First_Index (Aggr_Typ), | |
1044 | Index_Constr => First_Index (Aggr_Typ), | |
1045 | Component_Typ => Component_Type (Typ), | |
1046 | Others_Allowed => False); | |
1047 | end if; | |
1048 | ||
1049 | if not Aggr_Resolved then | |
f5afb270 AC |
1050 | |
1051 | -- A parenthesized expression may have been intended as an | |
1052 | -- aggregate, leading to a type error when analyzing the | |
1053 | -- component. This can also happen for a nested component | |
1054 | -- (see Analyze_Aggr_Expr). | |
1055 | ||
1056 | if Paren_Count (N) > 0 then | |
1057 | Error_Msg_N | |
1058 | ("positional aggregate cannot have one component", N); | |
1059 | end if; | |
1060 | ||
996ae0b0 | 1061 | Aggr_Subtyp := Any_Composite; |
e917aec2 | 1062 | |
996ae0b0 RK |
1063 | else |
1064 | Aggr_Subtyp := Array_Aggr_Subtype (N, Typ); | |
1065 | end if; | |
1066 | ||
1067 | Set_Etype (N, Aggr_Subtyp); | |
1068 | end Array_Aggregate; | |
1069 | ||
d8387153 ES |
1070 | elsif Is_Private_Type (Typ) |
1071 | and then Present (Full_View (Typ)) | |
8256c1bf | 1072 | and then (In_Inlined_Body or In_Instance_Body) |
d8387153 ES |
1073 | and then Is_Composite_Type (Full_View (Typ)) |
1074 | then | |
1075 | Resolve (N, Full_View (Typ)); | |
1076 | ||
996ae0b0 RK |
1077 | else |
1078 | Error_Msg_N ("illegal context for aggregate", N); | |
996ae0b0 RK |
1079 | end if; |
1080 | ||
b87971f3 AC |
1081 | -- If we can determine statically that the evaluation of the aggregate |
1082 | -- raises Constraint_Error, then replace the aggregate with an | |
1083 | -- N_Raise_Constraint_Error node, but set the Etype to the right | |
1084 | -- aggregate subtype. Gigi needs this. | |
996ae0b0 RK |
1085 | |
1086 | if Raises_Constraint_Error (N) then | |
1087 | Aggr_Subtyp := Etype (N); | |
07fc65c4 | 1088 | Rewrite (N, |
bd65a2d7 | 1089 | Make_Raise_Constraint_Error (Loc, Reason => CE_Range_Check_Failed)); |
996ae0b0 RK |
1090 | Set_Raises_Constraint_Error (N); |
1091 | Set_Etype (N, Aggr_Subtyp); | |
1092 | Set_Analyzed (N); | |
1093 | end if; | |
d3820795 | 1094 | |
66b69678 AC |
1095 | if Warn_On_No_Value_Assigned |
1096 | and then Others_Box | |
1097 | and then not Is_Fully_Initialized_Type (Etype (N)) | |
1098 | then | |
1099 | Error_Msg_N ("?v?aggregate not fully initialized", N); | |
1100 | end if; | |
1101 | ||
22e89283 | 1102 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
1103 | end Resolve_Aggregate; |
1104 | ||
1105 | ----------------------------- | |
1106 | -- Resolve_Array_Aggregate -- | |
1107 | ----------------------------- | |
1108 | ||
1109 | function Resolve_Array_Aggregate | |
1110 | (N : Node_Id; | |
1111 | Index : Node_Id; | |
1112 | Index_Constr : Node_Id; | |
1113 | Component_Typ : Entity_Id; | |
ca44152f | 1114 | Others_Allowed : Boolean) return Boolean |
996ae0b0 RK |
1115 | is |
1116 | Loc : constant Source_Ptr := Sloc (N); | |
1117 | ||
1118 | Failure : constant Boolean := False; | |
1119 | Success : constant Boolean := True; | |
1120 | ||
1121 | Index_Typ : constant Entity_Id := Etype (Index); | |
1122 | Index_Typ_Low : constant Node_Id := Type_Low_Bound (Index_Typ); | |
1123 | Index_Typ_High : constant Node_Id := Type_High_Bound (Index_Typ); | |
b87971f3 AC |
1124 | -- The type of the index corresponding to the array sub-aggregate along |
1125 | -- with its low and upper bounds. | |
996ae0b0 RK |
1126 | |
1127 | Index_Base : constant Entity_Id := Base_Type (Index_Typ); | |
1128 | Index_Base_Low : constant Node_Id := Type_Low_Bound (Index_Base); | |
1129 | Index_Base_High : constant Node_Id := Type_High_Bound (Index_Base); | |
b87971f3 | 1130 | -- Ditto for the base type |
996ae0b0 | 1131 | |
ef74daea AC |
1132 | Others_Present : Boolean := False; |
1133 | ||
1134 | Nb_Choices : Nat := 0; | |
1135 | -- Contains the overall number of named choices in this sub-aggregate | |
1136 | ||
996ae0b0 RK |
1137 | function Add (Val : Uint; To : Node_Id) return Node_Id; |
1138 | -- Creates a new expression node where Val is added to expression To. | |
1139 | -- Tries to constant fold whenever possible. To must be an already | |
1140 | -- analyzed expression. | |
1141 | ||
1142 | procedure Check_Bound (BH : Node_Id; AH : in out Node_Id); | |
f915704f AC |
1143 | -- Checks that AH (the upper bound of an array aggregate) is less than |
1144 | -- or equal to BH (the upper bound of the index base type). If the check | |
1145 | -- fails, a warning is emitted, the Raises_Constraint_Error flag of N is | |
1146 | -- set, and AH is replaced with a duplicate of BH. | |
996ae0b0 RK |
1147 | |
1148 | procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id); | |
1149 | -- Checks that range AL .. AH is compatible with range L .. H. Emits a | |
b87971f3 | 1150 | -- warning if not and sets the Raises_Constraint_Error flag in N. |
996ae0b0 RK |
1151 | |
1152 | procedure Check_Length (L, H : Node_Id; Len : Uint); | |
1153 | -- Checks that range L .. H contains at least Len elements. Emits a | |
b87971f3 | 1154 | -- warning if not and sets the Raises_Constraint_Error flag in N. |
996ae0b0 RK |
1155 | |
1156 | function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean; | |
ec53a6da | 1157 | -- Returns True if range L .. H is dynamic or null |
996ae0b0 RK |
1158 | |
1159 | procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean); | |
1160 | -- Given expression node From, this routine sets OK to False if it | |
1161 | -- cannot statically evaluate From. Otherwise it stores this static | |
1162 | -- value into Value. | |
1163 | ||
1164 | function Resolve_Aggr_Expr | |
1165 | (Expr : Node_Id; | |
ca44152f | 1166 | Single_Elmt : Boolean) return Boolean; |
12a13f01 | 1167 | -- Resolves aggregate expression Expr. Returns False if resolution |
996ae0b0 | 1168 | -- fails. If Single_Elmt is set to False, the expression Expr may be |
b87971f3 | 1169 | -- used to initialize several array aggregate elements (this can happen |
f915704f | 1170 | -- for discrete choices such as "L .. H => Expr" or the OTHERS choice). |
b87971f3 AC |
1171 | -- In this event we do not resolve Expr unless expansion is disabled. |
1172 | -- To know why, see the DELAYED COMPONENT RESOLUTION note above. | |
ca5af305 AC |
1173 | -- |
1174 | -- NOTE: In the case of "... => <>", we pass the in the | |
1175 | -- N_Component_Association node as Expr, since there is no Expression in | |
1176 | -- that case, and we need a Sloc for the error message. | |
996ae0b0 | 1177 | |
ef74daea AC |
1178 | procedure Resolve_Iterated_Component_Association |
1179 | (N : Node_Id; | |
1180 | Index_Typ : Entity_Id); | |
1181 | -- For AI12-061 | |
1182 | ||
996ae0b0 RK |
1183 | --------- |
1184 | -- Add -- | |
1185 | --------- | |
1186 | ||
1187 | function Add (Val : Uint; To : Node_Id) return Node_Id is | |
1188 | Expr_Pos : Node_Id; | |
1189 | Expr : Node_Id; | |
1190 | To_Pos : Node_Id; | |
1191 | ||
1192 | begin | |
1193 | if Raises_Constraint_Error (To) then | |
1194 | return To; | |
1195 | end if; | |
1196 | ||
1197 | -- First test if we can do constant folding | |
1198 | ||
1199 | if Compile_Time_Known_Value (To) | |
1200 | or else Nkind (To) = N_Integer_Literal | |
1201 | then | |
1202 | Expr_Pos := Make_Integer_Literal (Loc, Expr_Value (To) + Val); | |
1203 | Set_Is_Static_Expression (Expr_Pos); | |
1204 | Set_Etype (Expr_Pos, Etype (To)); | |
1205 | Set_Analyzed (Expr_Pos, Analyzed (To)); | |
1206 | ||
1207 | if not Is_Enumeration_Type (Index_Typ) then | |
1208 | Expr := Expr_Pos; | |
1209 | ||
1210 | -- If we are dealing with enumeration return | |
1211 | -- Index_Typ'Val (Expr_Pos) | |
1212 | ||
1213 | else | |
1214 | Expr := | |
1215 | Make_Attribute_Reference | |
1216 | (Loc, | |
e4494292 | 1217 | Prefix => New_Occurrence_Of (Index_Typ, Loc), |
996ae0b0 RK |
1218 | Attribute_Name => Name_Val, |
1219 | Expressions => New_List (Expr_Pos)); | |
1220 | end if; | |
1221 | ||
1222 | return Expr; | |
1223 | end if; | |
1224 | ||
1225 | -- If we are here no constant folding possible | |
1226 | ||
1227 | if not Is_Enumeration_Type (Index_Base) then | |
1228 | Expr := | |
1229 | Make_Op_Add (Loc, | |
f915704f AC |
1230 | Left_Opnd => Duplicate_Subexpr (To), |
1231 | Right_Opnd => Make_Integer_Literal (Loc, Val)); | |
996ae0b0 RK |
1232 | |
1233 | -- If we are dealing with enumeration return | |
1234 | -- Index_Typ'Val (Index_Typ'Pos (To) + Val) | |
1235 | ||
1236 | else | |
1237 | To_Pos := | |
1238 | Make_Attribute_Reference | |
1239 | (Loc, | |
e4494292 | 1240 | Prefix => New_Occurrence_Of (Index_Typ, Loc), |
996ae0b0 RK |
1241 | Attribute_Name => Name_Pos, |
1242 | Expressions => New_List (Duplicate_Subexpr (To))); | |
1243 | ||
1244 | Expr_Pos := | |
1245 | Make_Op_Add (Loc, | |
58009744 AC |
1246 | Left_Opnd => To_Pos, |
1247 | Right_Opnd => Make_Integer_Literal (Loc, Val)); | |
996ae0b0 RK |
1248 | |
1249 | Expr := | |
1250 | Make_Attribute_Reference | |
1251 | (Loc, | |
e4494292 | 1252 | Prefix => New_Occurrence_Of (Index_Typ, Loc), |
996ae0b0 RK |
1253 | Attribute_Name => Name_Val, |
1254 | Expressions => New_List (Expr_Pos)); | |
f915704f AC |
1255 | |
1256 | -- If the index type has a non standard representation, the | |
1257 | -- attributes 'Val and 'Pos expand into function calls and the | |
1258 | -- resulting expression is considered non-safe for reevaluation | |
1259 | -- by the backend. Relocate it into a constant temporary in order | |
1260 | -- to make it safe for reevaluation. | |
1261 | ||
1262 | if Has_Non_Standard_Rep (Etype (N)) then | |
1263 | declare | |
1264 | Def_Id : Entity_Id; | |
1265 | ||
1266 | begin | |
1267 | Def_Id := Make_Temporary (Loc, 'R', Expr); | |
1268 | Set_Etype (Def_Id, Index_Typ); | |
1269 | Insert_Action (N, | |
1270 | Make_Object_Declaration (Loc, | |
1271 | Defining_Identifier => Def_Id, | |
e4494292 RD |
1272 | Object_Definition => |
1273 | New_Occurrence_Of (Index_Typ, Loc), | |
f915704f AC |
1274 | Constant_Present => True, |
1275 | Expression => Relocate_Node (Expr))); | |
1276 | ||
e4494292 | 1277 | Expr := New_Occurrence_Of (Def_Id, Loc); |
f915704f AC |
1278 | end; |
1279 | end if; | |
996ae0b0 RK |
1280 | end if; |
1281 | ||
1282 | return Expr; | |
1283 | end Add; | |
1284 | ||
1285 | ----------------- | |
1286 | -- Check_Bound -- | |
1287 | ----------------- | |
1288 | ||
1289 | procedure Check_Bound (BH : Node_Id; AH : in out Node_Id) is | |
1290 | Val_BH : Uint; | |
1291 | Val_AH : Uint; | |
1292 | ||
1293 | OK_BH : Boolean; | |
1294 | OK_AH : Boolean; | |
1295 | ||
1296 | begin | |
1297 | Get (Value => Val_BH, From => BH, OK => OK_BH); | |
1298 | Get (Value => Val_AH, From => AH, OK => OK_AH); | |
1299 | ||
1300 | if OK_BH and then OK_AH and then Val_BH < Val_AH then | |
1301 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1302 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1303 | Error_Msg_N ("upper bound out of range<<", AH); |
1304 | Error_Msg_N ("\Constraint_Error [<<", AH); | |
996ae0b0 RK |
1305 | |
1306 | -- You need to set AH to BH or else in the case of enumerations | |
3b42c566 | 1307 | -- indexes we will not be able to resolve the aggregate bounds. |
996ae0b0 RK |
1308 | |
1309 | AH := Duplicate_Subexpr (BH); | |
1310 | end if; | |
1311 | end Check_Bound; | |
1312 | ||
1313 | ------------------ | |
1314 | -- Check_Bounds -- | |
1315 | ------------------ | |
1316 | ||
1317 | procedure Check_Bounds (L, H : Node_Id; AL, AH : Node_Id) is | |
1318 | Val_L : Uint; | |
1319 | Val_H : Uint; | |
1320 | Val_AL : Uint; | |
1321 | Val_AH : Uint; | |
1322 | ||
f91e8020 GD |
1323 | OK_L : Boolean; |
1324 | OK_H : Boolean; | |
1325 | ||
996ae0b0 | 1326 | OK_AL : Boolean; |
f91e8020 GD |
1327 | OK_AH : Boolean; |
1328 | pragma Warnings (Off, OK_AL); | |
1329 | pragma Warnings (Off, OK_AH); | |
996ae0b0 RK |
1330 | |
1331 | begin | |
1332 | if Raises_Constraint_Error (N) | |
1333 | or else Dynamic_Or_Null_Range (AL, AH) | |
1334 | then | |
1335 | return; | |
1336 | end if; | |
1337 | ||
1338 | Get (Value => Val_L, From => L, OK => OK_L); | |
1339 | Get (Value => Val_H, From => H, OK => OK_H); | |
1340 | ||
1341 | Get (Value => Val_AL, From => AL, OK => OK_AL); | |
1342 | Get (Value => Val_AH, From => AH, OK => OK_AH); | |
1343 | ||
1344 | if OK_L and then Val_L > Val_AL then | |
1345 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1346 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1347 | Error_Msg_N ("lower bound of aggregate out of range<<", N); |
1348 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
1349 | end if; |
1350 | ||
1351 | if OK_H and then Val_H < Val_AH then | |
1352 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1353 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1354 | Error_Msg_N ("upper bound of aggregate out of range<<", N); |
1355 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
1356 | end if; |
1357 | end Check_Bounds; | |
1358 | ||
1359 | ------------------ | |
1360 | -- Check_Length -- | |
1361 | ------------------ | |
1362 | ||
1363 | procedure Check_Length (L, H : Node_Id; Len : Uint) is | |
1364 | Val_L : Uint; | |
1365 | Val_H : Uint; | |
1366 | ||
1367 | OK_L : Boolean; | |
1368 | OK_H : Boolean; | |
1369 | ||
1370 | Range_Len : Uint; | |
1371 | ||
1372 | begin | |
1373 | if Raises_Constraint_Error (N) then | |
1374 | return; | |
1375 | end if; | |
1376 | ||
1377 | Get (Value => Val_L, From => L, OK => OK_L); | |
1378 | Get (Value => Val_H, From => H, OK => OK_H); | |
1379 | ||
1380 | if not OK_L or else not OK_H then | |
1381 | return; | |
1382 | end if; | |
1383 | ||
1384 | -- If null range length is zero | |
1385 | ||
1386 | if Val_L > Val_H then | |
1387 | Range_Len := Uint_0; | |
1388 | else | |
1389 | Range_Len := Val_H - Val_L + 1; | |
1390 | end if; | |
1391 | ||
1392 | if Range_Len < Len then | |
1393 | Set_Raises_Constraint_Error (N); | |
43417b90 | 1394 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 AC |
1395 | Error_Msg_N ("too many elements<<", N); |
1396 | Error_Msg_N ("\Constraint_Error [<<", N); | |
996ae0b0 RK |
1397 | end if; |
1398 | end Check_Length; | |
1399 | ||
1400 | --------------------------- | |
1401 | -- Dynamic_Or_Null_Range -- | |
1402 | --------------------------- | |
1403 | ||
1404 | function Dynamic_Or_Null_Range (L, H : Node_Id) return Boolean is | |
1405 | Val_L : Uint; | |
1406 | Val_H : Uint; | |
1407 | ||
1408 | OK_L : Boolean; | |
1409 | OK_H : Boolean; | |
1410 | ||
1411 | begin | |
1412 | Get (Value => Val_L, From => L, OK => OK_L); | |
1413 | Get (Value => Val_H, From => H, OK => OK_H); | |
1414 | ||
1415 | return not OK_L or else not OK_H | |
1416 | or else not Is_OK_Static_Expression (L) | |
1417 | or else not Is_OK_Static_Expression (H) | |
1418 | or else Val_L > Val_H; | |
1419 | end Dynamic_Or_Null_Range; | |
1420 | ||
1421 | --------- | |
1422 | -- Get -- | |
1423 | --------- | |
1424 | ||
1425 | procedure Get (Value : out Uint; From : Node_Id; OK : out Boolean) is | |
1426 | begin | |
1427 | OK := True; | |
1428 | ||
1429 | if Compile_Time_Known_Value (From) then | |
1430 | Value := Expr_Value (From); | |
1431 | ||
1432 | -- If expression From is something like Some_Type'Val (10) then | |
1f0b1e48 | 1433 | -- Value = 10. |
996ae0b0 RK |
1434 | |
1435 | elsif Nkind (From) = N_Attribute_Reference | |
1436 | and then Attribute_Name (From) = Name_Val | |
1437 | and then Compile_Time_Known_Value (First (Expressions (From))) | |
1438 | then | |
1439 | Value := Expr_Value (First (Expressions (From))); | |
996ae0b0 RK |
1440 | else |
1441 | Value := Uint_0; | |
1442 | OK := False; | |
1443 | end if; | |
1444 | end Get; | |
1445 | ||
1446 | ----------------------- | |
1447 | -- Resolve_Aggr_Expr -- | |
1448 | ----------------------- | |
1449 | ||
1450 | function Resolve_Aggr_Expr | |
1451 | (Expr : Node_Id; | |
ca44152f | 1452 | Single_Elmt : Boolean) return Boolean |
996ae0b0 | 1453 | is |
fbf5a39b AC |
1454 | Nxt_Ind : constant Node_Id := Next_Index (Index); |
1455 | Nxt_Ind_Constr : constant Node_Id := Next_Index (Index_Constr); | |
12a13f01 | 1456 | -- Index is the current index corresponding to the expression |
996ae0b0 RK |
1457 | |
1458 | Resolution_OK : Boolean := True; | |
ec53a6da | 1459 | -- Set to False if resolution of the expression failed |
996ae0b0 RK |
1460 | |
1461 | begin | |
199c6a10 AC |
1462 | -- Defend against previous errors |
1463 | ||
1464 | if Nkind (Expr) = N_Error | |
1465 | or else Error_Posted (Expr) | |
1466 | then | |
1467 | return True; | |
1468 | end if; | |
1469 | ||
996ae0b0 RK |
1470 | -- If the array type against which we are resolving the aggregate |
1471 | -- has several dimensions, the expressions nested inside the | |
1472 | -- aggregate must be further aggregates (or strings). | |
1473 | ||
1474 | if Present (Nxt_Ind) then | |
1475 | if Nkind (Expr) /= N_Aggregate then | |
1476 | ||
1477 | -- A string literal can appear where a one-dimensional array | |
1478 | -- of characters is expected. If the literal looks like an | |
1479 | -- operator, it is still an operator symbol, which will be | |
1480 | -- transformed into a string when analyzed. | |
1481 | ||
1482 | if Is_Character_Type (Component_Typ) | |
1483 | and then No (Next_Index (Nxt_Ind)) | |
4a08c95c | 1484 | and then Nkind (Expr) in N_String_Literal | N_Operator_Symbol |
996ae0b0 RK |
1485 | then |
1486 | -- A string literal used in a multidimensional array | |
1487 | -- aggregate in place of the final one-dimensional | |
1488 | -- aggregate must not be enclosed in parentheses. | |
1489 | ||
1490 | if Paren_Count (Expr) /= 0 then | |
ed2233dc | 1491 | Error_Msg_N ("no parenthesis allowed here", Expr); |
996ae0b0 RK |
1492 | end if; |
1493 | ||
1494 | Make_String_Into_Aggregate (Expr); | |
1495 | ||
1496 | else | |
1497 | Error_Msg_N ("nested array aggregate expected", Expr); | |
9d0c3761 AC |
1498 | |
1499 | -- If the expression is parenthesized, this may be | |
1500 | -- a missing component association for a 1-aggregate. | |
1501 | ||
1502 | if Paren_Count (Expr) > 0 then | |
ed2233dc | 1503 | Error_Msg_N |
58009744 AC |
1504 | ("\if single-component aggregate is intended, " |
1505 | & "write e.g. (1 ='> ...)", Expr); | |
9d0c3761 | 1506 | end if; |
f5afb270 | 1507 | |
996ae0b0 RK |
1508 | return Failure; |
1509 | end if; | |
1510 | end if; | |
1511 | ||
ca5af305 AC |
1512 | -- If it's "... => <>", nothing to resolve |
1513 | ||
1514 | if Nkind (Expr) = N_Component_Association then | |
1515 | pragma Assert (Box_Present (Expr)); | |
1516 | return Success; | |
1517 | end if; | |
1518 | ||
0ab80019 | 1519 | -- Ada 2005 (AI-231): Propagate the type to the nested aggregate. |
35b7fa6a AC |
1520 | -- Required to check the null-exclusion attribute (if present). |
1521 | -- This value may be overridden later on. | |
1522 | ||
1523 | Set_Etype (Expr, Etype (N)); | |
1524 | ||
996ae0b0 RK |
1525 | Resolution_OK := Resolve_Array_Aggregate |
1526 | (Expr, Nxt_Ind, Nxt_Ind_Constr, Component_Typ, Others_Allowed); | |
1527 | ||
ca5af305 | 1528 | else |
ca5af305 AC |
1529 | -- If it's "... => <>", nothing to resolve |
1530 | ||
1531 | if Nkind (Expr) = N_Component_Association then | |
1532 | pragma Assert (Box_Present (Expr)); | |
1533 | return Success; | |
1534 | end if; | |
1535 | ||
1536 | -- Do not resolve the expressions of discrete or others choices | |
1537 | -- unless the expression covers a single component, or the | |
1538 | -- expander is inactive. | |
1539 | ||
92b751fd | 1540 | -- In SPARK mode, expressions that can perform side effects will |
06b599fd YM |
1541 | -- be recognized by the gnat2why back-end, and the whole |
1542 | -- subprogram will be ignored. So semantic analysis can be | |
1543 | -- performed safely. | |
9f8d1e5c | 1544 | |
ca5af305 | 1545 | if Single_Elmt |
4460a9bc | 1546 | or else not Expander_Active |
ca5af305 AC |
1547 | or else In_Spec_Expression |
1548 | then | |
1549 | Analyze_And_Resolve (Expr, Component_Typ); | |
1550 | Check_Expr_OK_In_Limited_Aggregate (Expr); | |
1551 | Check_Non_Static_Context (Expr); | |
1552 | Aggregate_Constraint_Checks (Expr, Component_Typ); | |
1553 | Check_Unset_Reference (Expr); | |
1554 | end if; | |
996ae0b0 RK |
1555 | end if; |
1556 | ||
dec6faf1 AC |
1557 | -- If an aggregate component has a type with predicates, an explicit |
1558 | -- predicate check must be applied, as for an assignment statement, | |
152f64c2 | 1559 | -- because the aggregate might not be expanded into individual |
07eb872e AC |
1560 | -- component assignments. If the expression covers several components |
1561 | -- the analysis and the predicate check take place later. | |
dec6faf1 | 1562 | |
619bfd9f | 1563 | if Has_Predicates (Component_Typ) |
07eb872e AC |
1564 | and then Analyzed (Expr) |
1565 | then | |
887d102a AC |
1566 | Apply_Predicate_Check (Expr, Component_Typ); |
1567 | end if; | |
1568 | ||
996ae0b0 RK |
1569 | if Raises_Constraint_Error (Expr) |
1570 | and then Nkind (Parent (Expr)) /= N_Component_Association | |
1571 | then | |
1572 | Set_Raises_Constraint_Error (N); | |
1573 | end if; | |
1574 | ||
d79e621a | 1575 | -- If the expression has been marked as requiring a range check, |
edab6088 RD |
1576 | -- then generate it here. It's a bit odd to be generating such |
1577 | -- checks in the analyzer, but harmless since Generate_Range_Check | |
1578 | -- does nothing (other than making sure Do_Range_Check is set) if | |
1579 | -- the expander is not active. | |
d79e621a GD |
1580 | |
1581 | if Do_Range_Check (Expr) then | |
d79e621a GD |
1582 | Generate_Range_Check (Expr, Component_Typ, CE_Range_Check_Failed); |
1583 | end if; | |
1584 | ||
996ae0b0 RK |
1585 | return Resolution_OK; |
1586 | end Resolve_Aggr_Expr; | |
1587 | ||
ef74daea AC |
1588 | -------------------------------------------- |
1589 | -- Resolve_Iterated_Component_Association -- | |
1590 | -------------------------------------------- | |
1591 | ||
1592 | procedure Resolve_Iterated_Component_Association | |
1593 | (N : Node_Id; | |
1594 | Index_Typ : Entity_Id) | |
1595 | is | |
ef74daea | 1596 | Loc : constant Source_Ptr := Sloc (N); |
fb00cc70 ES |
1597 | Id : constant Entity_Id := Defining_Identifier (N); |
1598 | ||
1599 | ----------------------- | |
1600 | -- Remove_References -- | |
1601 | ----------------------- | |
1602 | ||
1603 | function Remove_Ref (N : Node_Id) return Traverse_Result; | |
1604 | -- Remove references to the entity Id after analysis, so it can be | |
1605 | -- properly reanalyzed after construct is expanded into a loop. | |
1606 | ||
1607 | function Remove_Ref (N : Node_Id) return Traverse_Result is | |
1608 | begin | |
1609 | if Nkind (N) = N_Identifier | |
1610 | and then Present (Entity (N)) | |
1611 | and then Entity (N) = Id | |
1612 | then | |
1613 | Set_Entity (N, Empty); | |
1614 | Set_Etype (N, Empty); | |
1615 | end if; | |
1616 | Set_Analyzed (N, False); | |
1617 | return OK; | |
1618 | end Remove_Ref; | |
1619 | ||
1620 | procedure Remove_References is new Traverse_Proc (Remove_Ref); | |
1621 | ||
1622 | -- Local variables | |
ef74daea AC |
1623 | |
1624 | Choice : Node_Id; | |
1625 | Dummy : Boolean; | |
1626 | Ent : Entity_Id; | |
d940c627 | 1627 | Expr : Node_Id; |
fb00cc70 ES |
1628 | |
1629 | -- Start of processing for Resolve_Iterated_Component_Association | |
ef74daea AC |
1630 | |
1631 | begin | |
ff49b805 ES |
1632 | -- An element iterator specification cannot appear in |
1633 | -- an array aggregate because it does not provide index | |
1634 | -- values for the association. This must be a semantic | |
1635 | -- check because the parser cannot tell whether this is | |
1636 | -- an array aggregate or a container aggregate. | |
1637 | ||
1638 | if Present (Iterator_Specification (N)) then | |
1639 | Error_Msg_N ("container element Iterator cannot appear " | |
1640 | & "in an array aggregate", N); | |
1641 | return; | |
1642 | end if; | |
1643 | ||
ef74daea AC |
1644 | Choice := First (Discrete_Choices (N)); |
1645 | ||
1646 | while Present (Choice) loop | |
1647 | if Nkind (Choice) = N_Others_Choice then | |
ef74daea AC |
1648 | Others_Present := True; |
1649 | ||
1650 | else | |
b03d3f73 AC |
1651 | Analyze (Choice); |
1652 | ||
2bb988bb | 1653 | -- Choice can be a subtype name, a range, or an expression |
b03d3f73 AC |
1654 | |
1655 | if Is_Entity_Name (Choice) | |
1656 | and then Is_Type (Entity (Choice)) | |
1657 | and then Base_Type (Entity (Choice)) = Base_Type (Index_Typ) | |
1658 | then | |
1659 | null; | |
1660 | ||
1661 | else | |
1662 | Analyze_And_Resolve (Choice, Index_Typ); | |
1663 | end if; | |
ef74daea AC |
1664 | end if; |
1665 | ||
ef74daea AC |
1666 | Next (Choice); |
1667 | end loop; | |
1668 | ||
1669 | -- Create a scope in which to introduce an index, which is usually | |
b2c1aa8f AC |
1670 | -- visible in the expression for the component, and needed for its |
1671 | -- analysis. | |
ef74daea AC |
1672 | |
1673 | Ent := New_Internal_Entity (E_Loop, Current_Scope, Loc, 'L'); | |
1674 | Set_Etype (Ent, Standard_Void_Type); | |
1675 | Set_Parent (Ent, Parent (N)); | |
d940c627 | 1676 | Push_Scope (Ent); |
ef74daea | 1677 | |
d940c627 | 1678 | -- Insert and decorate the index variable in the current scope. |
b03d3f73 | 1679 | -- The expression has to be analyzed once the index variable is |
e22557ee | 1680 | -- directly visible. |
9eb8d5b4 | 1681 | |
d940c627 ES |
1682 | Enter_Name (Id); |
1683 | Set_Etype (Id, Index_Typ); | |
2e02ab86 | 1684 | Mutate_Ekind (Id, E_Variable); |
d940c627 | 1685 | Set_Scope (Id, Ent); |
d940c627 | 1686 | |
02fb1280 PT |
1687 | -- Analyze expression without expansion, to verify legality. |
1688 | -- When generating code, we then remove references to the index | |
1689 | -- variable, because the expression will be analyzed anew after | |
1690 | -- rewritting as a loop with a new index variable; when not | |
1691 | -- generating code we leave the analyzed expression as it is. | |
fb00cc70 ES |
1692 | |
1693 | Expr := Expression (N); | |
d940c627 | 1694 | |
fb00cc70 ES |
1695 | Expander_Mode_Save_And_Set (False); |
1696 | Dummy := Resolve_Aggr_Expr (Expr, Single_Elmt => False); | |
1697 | Expander_Mode_Restore; | |
02fb1280 PT |
1698 | |
1699 | if Operating_Mode /= Check_Semantics then | |
1700 | Remove_References (Expr); | |
1701 | end if; | |
d940c627 ES |
1702 | |
1703 | -- An iterated_component_association may appear in a nested | |
1704 | -- aggregate for a multidimensional structure: preserve the bounds | |
1705 | -- computed for the expression, as well as the anonymous array | |
1706 | -- type generated for it; both are needed during array expansion. | |
d940c627 ES |
1707 | |
1708 | if Nkind (Expr) = N_Aggregate then | |
1709 | Set_Aggregate_Bounds (Expression (N), Aggregate_Bounds (Expr)); | |
1710 | Set_Etype (Expression (N), Etype (Expr)); | |
9eb8d5b4 | 1711 | end if; |
ef74daea | 1712 | |
ef74daea AC |
1713 | End_Scope; |
1714 | end Resolve_Iterated_Component_Association; | |
1715 | ||
1716 | -- Local variables | |
996ae0b0 | 1717 | |
58009744 AC |
1718 | Assoc : Node_Id; |
1719 | Choice : Node_Id; | |
1720 | Expr : Node_Id; | |
f91e8020 | 1721 | Discard : Node_Id; |
996ae0b0 RK |
1722 | |
1723 | Aggr_Low : Node_Id := Empty; | |
1724 | Aggr_High : Node_Id := Empty; | |
c7ce71c2 | 1725 | -- The actual low and high bounds of this sub-aggregate |
996ae0b0 | 1726 | |
ef74daea AC |
1727 | Case_Table_Size : Nat; |
1728 | -- Contains the size of the case table needed to sort aggregate choices | |
1729 | ||
996ae0b0 RK |
1730 | Choices_Low : Node_Id := Empty; |
1731 | Choices_High : Node_Id := Empty; | |
1732 | -- The lowest and highest discrete choices values for a named aggregate | |
1733 | ||
ef74daea AC |
1734 | Delete_Choice : Boolean; |
1735 | -- Used when replacing a subtype choice with predicate by a list | |
1736 | ||
996ae0b0 | 1737 | Nb_Elements : Uint := Uint_0; |
c7ce71c2 | 1738 | -- The number of elements in a positional aggregate |
996ae0b0 | 1739 | |
996ae0b0 RK |
1740 | Nb_Discrete_Choices : Nat := 0; |
1741 | -- The overall number of discrete choices (not counting others choice) | |
1742 | ||
996ae0b0 RK |
1743 | -- Start of processing for Resolve_Array_Aggregate |
1744 | ||
1745 | begin | |
6d2a1120 RD |
1746 | -- Ignore junk empty aggregate resulting from parser error |
1747 | ||
1748 | if No (Expressions (N)) | |
1749 | and then No (Component_Associations (N)) | |
1750 | and then not Null_Record_Present (N) | |
1751 | then | |
1752 | return False; | |
1753 | end if; | |
1754 | ||
996ae0b0 RK |
1755 | -- STEP 1: make sure the aggregate is correctly formatted |
1756 | ||
1757 | if Present (Component_Associations (N)) then | |
1758 | Assoc := First (Component_Associations (N)); | |
1759 | while Present (Assoc) loop | |
ef74daea AC |
1760 | if Nkind (Assoc) = N_Iterated_Component_Association then |
1761 | Resolve_Iterated_Component_Association (Assoc, Index_Typ); | |
ef74daea AC |
1762 | end if; |
1763 | ||
b2c1aa8f | 1764 | Choice := First (Choice_List (Assoc)); |
2791be24 | 1765 | Delete_Choice := False; |
996ae0b0 RK |
1766 | while Present (Choice) loop |
1767 | if Nkind (Choice) = N_Others_Choice then | |
1768 | Others_Present := True; | |
1769 | ||
b2c1aa8f | 1770 | if Choice /= First (Choice_List (Assoc)) |
996ae0b0 RK |
1771 | or else Present (Next (Choice)) |
1772 | then | |
ed2233dc | 1773 | Error_Msg_N |
996ae0b0 RK |
1774 | ("OTHERS must appear alone in a choice list", Choice); |
1775 | return Failure; | |
1776 | end if; | |
1777 | ||
1778 | if Present (Next (Assoc)) then | |
ed2233dc | 1779 | Error_Msg_N |
996ae0b0 RK |
1780 | ("OTHERS must appear last in an aggregate", Choice); |
1781 | return Failure; | |
1782 | end if; | |
1783 | ||
0ab80019 | 1784 | if Ada_Version = Ada_83 |
996ae0b0 | 1785 | and then Assoc /= First (Component_Associations (N)) |
4a08c95c AC |
1786 | and then Nkind (Parent (N)) in |
1787 | N_Assignment_Statement | N_Object_Declaration | |
996ae0b0 RK |
1788 | then |
1789 | Error_Msg_N | |
1790 | ("(Ada 83) illegal context for OTHERS choice", N); | |
1791 | end if; | |
2791be24 AC |
1792 | |
1793 | elsif Is_Entity_Name (Choice) then | |
1794 | Analyze (Choice); | |
1795 | ||
1796 | declare | |
1797 | E : constant Entity_Id := Entity (Choice); | |
1798 | New_Cs : List_Id; | |
1799 | P : Node_Id; | |
1800 | C : Node_Id; | |
1801 | ||
1802 | begin | |
1803 | if Is_Type (E) and then Has_Predicates (E) then | |
1804 | Freeze_Before (N, E); | |
1805 | ||
24de083f | 1806 | if Has_Dynamic_Predicate_Aspect (E) then |
b330e3c8 AC |
1807 | Error_Msg_NE |
1808 | ("subtype& has dynamic predicate, not allowed " | |
1809 | & "in aggregate choice", Choice, E); | |
24de083f | 1810 | |
4b259b2d | 1811 | elsif not Is_OK_Static_Subtype (E) then |
b330e3c8 AC |
1812 | Error_Msg_NE |
1813 | ("non-static subtype& has predicate, not allowed " | |
1814 | & "in aggregate choice", Choice, E); | |
24de083f AC |
1815 | end if; |
1816 | ||
2791be24 AC |
1817 | -- If the subtype has a static predicate, replace the |
1818 | -- original choice with the list of individual values | |
65f1ca2e | 1819 | -- covered by the predicate. |
3abbc5c2 | 1820 | -- This should be deferred to expansion time ??? |
2791be24 | 1821 | |
65f1ca2e | 1822 | if Present (Static_Discrete_Predicate (E)) then |
2791be24 AC |
1823 | Delete_Choice := True; |
1824 | ||
1825 | New_Cs := New_List; | |
60f908dd | 1826 | P := First (Static_Discrete_Predicate (E)); |
2791be24 AC |
1827 | while Present (P) loop |
1828 | C := New_Copy (P); | |
1829 | Set_Sloc (C, Sloc (Choice)); | |
1830 | Append_To (New_Cs, C); | |
1831 | Next (P); | |
1832 | end loop; | |
1833 | ||
1834 | Insert_List_After (Choice, New_Cs); | |
1835 | end if; | |
1836 | end if; | |
1837 | end; | |
996ae0b0 RK |
1838 | end if; |
1839 | ||
1840 | Nb_Choices := Nb_Choices + 1; | |
2791be24 AC |
1841 | |
1842 | declare | |
1843 | C : constant Node_Id := Choice; | |
1844 | ||
1845 | begin | |
1846 | Next (Choice); | |
1847 | ||
1848 | if Delete_Choice then | |
1849 | Remove (C); | |
1850 | Nb_Choices := Nb_Choices - 1; | |
1851 | Delete_Choice := False; | |
1852 | end if; | |
1853 | end; | |
996ae0b0 RK |
1854 | end loop; |
1855 | ||
1856 | Next (Assoc); | |
1857 | end loop; | |
1858 | end if; | |
1859 | ||
1860 | -- At this point we know that the others choice, if present, is by | |
1861 | -- itself and appears last in the aggregate. Check if we have mixed | |
1862 | -- positional and discrete associations (other than the others choice). | |
1863 | ||
1864 | if Present (Expressions (N)) | |
1865 | and then (Nb_Choices > 1 | |
1866 | or else (Nb_Choices = 1 and then not Others_Present)) | |
1867 | then | |
1868 | Error_Msg_N | |
1869 | ("named association cannot follow positional association", | |
ef74daea | 1870 | First (Choice_List (First (Component_Associations (N))))); |
996ae0b0 RK |
1871 | return Failure; |
1872 | end if; | |
1873 | ||
1874 | -- Test for the validity of an others choice if present | |
1875 | ||
1876 | if Others_Present and then not Others_Allowed then | |
f456de4c YM |
1877 | declare |
1878 | Others_N : constant Node_Id := | |
1879 | First (Choice_List (First (Component_Associations (N)))); | |
1880 | begin | |
1881 | Error_Msg_N ("OTHERS choice not allowed here", Others_N); | |
1882 | Error_Msg_N ("\qualify the aggregate with a constrained subtype " | |
1883 | & "to provide bounds for it", Others_N); | |
1884 | return Failure; | |
1885 | end; | |
996ae0b0 RK |
1886 | end if; |
1887 | ||
07fc65c4 GB |
1888 | -- Protect against cascaded errors |
1889 | ||
1890 | if Etype (Index_Typ) = Any_Type then | |
1891 | return Failure; | |
1892 | end if; | |
1893 | ||
996ae0b0 RK |
1894 | -- STEP 2: Process named components |
1895 | ||
1896 | if No (Expressions (N)) then | |
996ae0b0 RK |
1897 | if Others_Present then |
1898 | Case_Table_Size := Nb_Choices - 1; | |
1899 | else | |
1900 | Case_Table_Size := Nb_Choices; | |
1901 | end if; | |
1902 | ||
1903 | Step_2 : declare | |
10edebe7 AC |
1904 | function Empty_Range (A : Node_Id) return Boolean; |
1905 | -- If an association covers an empty range, some warnings on the | |
1906 | -- expression of the association can be disabled. | |
1907 | ||
1908 | ----------------- | |
1909 | -- Empty_Range -- | |
1910 | ----------------- | |
1911 | ||
1912 | function Empty_Range (A : Node_Id) return Boolean is | |
1913 | R : constant Node_Id := First (Choices (A)); | |
1914 | begin | |
1915 | return No (Next (R)) | |
1916 | and then Nkind (R) = N_Range | |
1917 | and then Compile_Time_Compare | |
1918 | (Low_Bound (R), High_Bound (R), False) = GT; | |
1919 | end Empty_Range; | |
1920 | ||
1921 | -- Local variables | |
1922 | ||
996ae0b0 RK |
1923 | Low : Node_Id; |
1924 | High : Node_Id; | |
1925 | -- Denote the lowest and highest values in an aggregate choice | |
1926 | ||
996ae0b0 RK |
1927 | S_Low : Node_Id := Empty; |
1928 | S_High : Node_Id := Empty; | |
1929 | -- if a choice in an aggregate is a subtype indication these | |
1930 | -- denote the lowest and highest values of the subtype | |
1931 | ||
38f44fd6 PT |
1932 | Table : Case_Table_Type (1 .. Case_Table_Size); |
1933 | -- Used to sort all the different choice values | |
996ae0b0 RK |
1934 | |
1935 | Single_Choice : Boolean; | |
1936 | -- Set to true every time there is a single discrete choice in a | |
1937 | -- discrete association | |
1938 | ||
1939 | Prev_Nb_Discrete_Choices : Nat; | |
b87971f3 AC |
1940 | -- Used to keep track of the number of discrete choices in the |
1941 | -- current association. | |
996ae0b0 | 1942 | |
c0b11850 AC |
1943 | Errors_Posted_On_Choices : Boolean := False; |
1944 | -- Keeps track of whether any choices have semantic errors | |
1945 | ||
5a521b8a AC |
1946 | -- Start of processing for Step_2 |
1947 | ||
996ae0b0 | 1948 | begin |
ec53a6da | 1949 | -- STEP 2 (A): Check discrete choices validity |
996ae0b0 RK |
1950 | |
1951 | Assoc := First (Component_Associations (N)); | |
1952 | while Present (Assoc) loop | |
996ae0b0 | 1953 | Prev_Nb_Discrete_Choices := Nb_Discrete_Choices; |
ef74daea AC |
1954 | Choice := First (Choice_List (Assoc)); |
1955 | ||
996ae0b0 RK |
1956 | loop |
1957 | Analyze (Choice); | |
1958 | ||
1959 | if Nkind (Choice) = N_Others_Choice then | |
1960 | Single_Choice := False; | |
1961 | exit; | |
1962 | ||
1963 | -- Test for subtype mark without constraint | |
1964 | ||
1965 | elsif Is_Entity_Name (Choice) and then | |
1966 | Is_Type (Entity (Choice)) | |
1967 | then | |
1968 | if Base_Type (Entity (Choice)) /= Index_Base then | |
1969 | Error_Msg_N | |
1970 | ("invalid subtype mark in aggregate choice", | |
1971 | Choice); | |
1972 | return Failure; | |
1973 | end if; | |
1974 | ||
ca44152f ES |
1975 | -- Case of subtype indication |
1976 | ||
996ae0b0 RK |
1977 | elsif Nkind (Choice) = N_Subtype_Indication then |
1978 | Resolve_Discrete_Subtype_Indication (Choice, Index_Base); | |
1979 | ||
24de083f AC |
1980 | if Has_Dynamic_Predicate_Aspect |
1981 | (Entity (Subtype_Mark (Choice))) | |
1982 | then | |
58009744 AC |
1983 | Error_Msg_NE |
1984 | ("subtype& has dynamic predicate, " | |
1985 | & "not allowed in aggregate choice", | |
1986 | Choice, Entity (Subtype_Mark (Choice))); | |
24de083f AC |
1987 | end if; |
1988 | ||
324ac540 | 1989 | -- Does the subtype indication evaluation raise CE? |
996ae0b0 RK |
1990 | |
1991 | Get_Index_Bounds (Subtype_Mark (Choice), S_Low, S_High); | |
1992 | Get_Index_Bounds (Choice, Low, High); | |
1993 | Check_Bounds (S_Low, S_High, Low, High); | |
1994 | ||
ca44152f ES |
1995 | -- Case of range or expression |
1996 | ||
1997 | else | |
996ae0b0 | 1998 | Resolve (Choice, Index_Base); |
fbf5a39b | 1999 | Check_Unset_Reference (Choice); |
996ae0b0 RK |
2000 | Check_Non_Static_Context (Choice); |
2001 | ||
c0b11850 AC |
2002 | -- If semantic errors were posted on the choice, then |
2003 | -- record that for possible early return from later | |
2004 | -- processing (see handling of enumeration choices). | |
2005 | ||
2006 | if Error_Posted (Choice) then | |
2007 | Errors_Posted_On_Choices := True; | |
2008 | end if; | |
2009 | ||
996ae0b0 | 2010 | -- Do not range check a choice. This check is redundant |
b87971f3 AC |
2011 | -- since this test is already done when we check that the |
2012 | -- bounds of the array aggregate are within range. | |
996ae0b0 RK |
2013 | |
2014 | Set_Do_Range_Check (Choice, False); | |
2015 | end if; | |
2016 | ||
2017 | -- If we could not resolve the discrete choice stop here | |
2018 | ||
2019 | if Etype (Choice) = Any_Type then | |
2020 | return Failure; | |
2021 | ||
ec53a6da | 2022 | -- If the discrete choice raises CE get its original bounds |
996ae0b0 RK |
2023 | |
2024 | elsif Nkind (Choice) = N_Raise_Constraint_Error then | |
2025 | Set_Raises_Constraint_Error (N); | |
2026 | Get_Index_Bounds (Original_Node (Choice), Low, High); | |
2027 | ||
2028 | -- Otherwise get its bounds as usual | |
2029 | ||
2030 | else | |
2031 | Get_Index_Bounds (Choice, Low, High); | |
2032 | end if; | |
2033 | ||
2034 | if (Dynamic_Or_Null_Range (Low, High) | |
2035 | or else (Nkind (Choice) = N_Subtype_Indication | |
2036 | and then | |
2037 | Dynamic_Or_Null_Range (S_Low, S_High))) | |
2038 | and then Nb_Choices /= 1 | |
2039 | then | |
2040 | Error_Msg_N | |
58009744 AC |
2041 | ("dynamic or empty choice in aggregate " |
2042 | & "must be the only choice", Choice); | |
996ae0b0 RK |
2043 | return Failure; |
2044 | end if; | |
2045 | ||
2f7ae2aa BD |
2046 | if not (All_Composite_Constraints_Static (Low) |
2047 | and then All_Composite_Constraints_Static (High) | |
2048 | and then All_Composite_Constraints_Static (S_Low) | |
2049 | and then All_Composite_Constraints_Static (S_High)) | |
2050 | then | |
2051 | Check_Restriction (No_Dynamic_Sized_Objects, Choice); | |
2052 | end if; | |
2053 | ||
996ae0b0 | 2054 | Nb_Discrete_Choices := Nb_Discrete_Choices + 1; |
82893775 AC |
2055 | Table (Nb_Discrete_Choices).Lo := Low; |
2056 | Table (Nb_Discrete_Choices).Hi := High; | |
2057 | Table (Nb_Discrete_Choices).Choice := Choice; | |
996ae0b0 RK |
2058 | |
2059 | Next (Choice); | |
2060 | ||
2061 | if No (Choice) then | |
9b96e234 | 2062 | |
996ae0b0 RK |
2063 | -- Check if we have a single discrete choice and whether |
2064 | -- this discrete choice specifies a single value. | |
2065 | ||
2066 | Single_Choice := | |
2067 | (Nb_Discrete_Choices = Prev_Nb_Discrete_Choices + 1) | |
2068 | and then (Low = High); | |
2069 | ||
2070 | exit; | |
2071 | end if; | |
2072 | end loop; | |
2073 | ||
0ab80019 | 2074 | -- Ada 2005 (AI-231) |
2820d220 | 2075 | |
0791fbe9 | 2076 | if Ada_Version >= Ada_2005 |
8133b9d1 | 2077 | and then Known_Null (Expression (Assoc)) |
5a521b8a | 2078 | and then not Empty_Range (Assoc) |
ec53a6da | 2079 | then |
82c80734 RD |
2080 | Check_Can_Never_Be_Null (Etype (N), Expression (Assoc)); |
2081 | end if; | |
2820d220 | 2082 | |
0ab80019 | 2083 | -- Ada 2005 (AI-287): In case of default initialized component |
b87971f3 | 2084 | -- we delay the resolution to the expansion phase. |
c45b6ae0 AC |
2085 | |
2086 | if Box_Present (Assoc) then | |
2087 | ||
b87971f3 AC |
2088 | -- Ada 2005 (AI-287): In case of default initialization of a |
2089 | -- component the expander will generate calls to the | |
ca5af305 AC |
2090 | -- corresponding initialization subprogram. We need to call |
2091 | -- Resolve_Aggr_Expr to check the rules about | |
2092 | -- dimensionality. | |
c45b6ae0 | 2093 | |
882eadaf RD |
2094 | if not Resolve_Aggr_Expr |
2095 | (Assoc, Single_Elmt => Single_Choice) | |
ca5af305 AC |
2096 | then |
2097 | return Failure; | |
2098 | end if; | |
c45b6ae0 | 2099 | |
3b612313 PT |
2100 | -- ??? Checks for dynamically tagged expressions below will |
2101 | -- be only applied to iterated_component_association after | |
2102 | -- expansion; in particular, errors might not be reported when | |
2103 | -- -gnatc switch is used. | |
2104 | ||
9eb8d5b4 | 2105 | elsif Nkind (Assoc) = N_Iterated_Component_Association then |
3b612313 | 2106 | null; -- handled above, in a loop context |
9eb8d5b4 | 2107 | |
882eadaf RD |
2108 | elsif not Resolve_Aggr_Expr |
2109 | (Expression (Assoc), Single_Elmt => Single_Choice) | |
996ae0b0 RK |
2110 | then |
2111 | return Failure; | |
4755cce9 JM |
2112 | |
2113 | -- Check incorrect use of dynamically tagged expression | |
2114 | ||
2115 | -- We differentiate here two cases because the expression may | |
2116 | -- not be decorated. For example, the analysis and resolution | |
b87971f3 AC |
2117 | -- of the expression associated with the others choice will be |
2118 | -- done later with the full aggregate. In such case we | |
4755cce9 JM |
2119 | -- duplicate the expression tree to analyze the copy and |
2120 | -- perform the required check. | |
2121 | ||
2122 | elsif not Present (Etype (Expression (Assoc))) then | |
2123 | declare | |
2124 | Save_Analysis : constant Boolean := Full_Analysis; | |
2125 | Expr : constant Node_Id := | |
2126 | New_Copy_Tree (Expression (Assoc)); | |
2127 | ||
2128 | begin | |
2129 | Expander_Mode_Save_And_Set (False); | |
2130 | Full_Analysis := False; | |
6ff6152d ES |
2131 | |
2132 | -- Analyze the expression, making sure it is properly | |
2133 | -- attached to the tree before we do the analysis. | |
2134 | ||
2135 | Set_Parent (Expr, Parent (Expression (Assoc))); | |
4755cce9 | 2136 | Analyze (Expr); |
094cefda | 2137 | |
a6ac7311 AC |
2138 | -- Compute its dimensions now, rather than at the end of |
2139 | -- resolution, because in the case of multidimensional | |
888be6b1 AC |
2140 | -- aggregates subsequent expansion may lead to spurious |
2141 | -- errors. | |
2142 | ||
2143 | Check_Expression_Dimensions (Expr, Component_Typ); | |
2144 | ||
094cefda AC |
2145 | -- If the expression is a literal, propagate this info |
2146 | -- to the expression in the association, to enable some | |
2147 | -- optimizations downstream. | |
2148 | ||
2149 | if Is_Entity_Name (Expr) | |
2150 | and then Present (Entity (Expr)) | |
2151 | and then Ekind (Entity (Expr)) = E_Enumeration_Literal | |
2152 | then | |
2153 | Analyze_And_Resolve | |
2154 | (Expression (Assoc), Component_Typ); | |
2155 | end if; | |
2156 | ||
4755cce9 JM |
2157 | Full_Analysis := Save_Analysis; |
2158 | Expander_Mode_Restore; | |
2159 | ||
2160 | if Is_Tagged_Type (Etype (Expr)) then | |
2161 | Check_Dynamically_Tagged_Expression | |
2162 | (Expr => Expr, | |
2163 | Typ => Component_Type (Etype (N)), | |
2164 | Related_Nod => N); | |
2165 | end if; | |
2166 | end; | |
2167 | ||
2168 | elsif Is_Tagged_Type (Etype (Expression (Assoc))) then | |
2169 | Check_Dynamically_Tagged_Expression | |
1c612f29 RD |
2170 | (Expr => Expression (Assoc), |
2171 | Typ => Component_Type (Etype (N)), | |
4755cce9 | 2172 | Related_Nod => N); |
996ae0b0 RK |
2173 | end if; |
2174 | ||
2175 | Next (Assoc); | |
2176 | end loop; | |
2177 | ||
2178 | -- If aggregate contains more than one choice then these must be | |
882eadaf RD |
2179 | -- static. Check for duplicate and missing values. |
2180 | ||
2181 | -- Note: there is duplicated code here wrt Check_Choice_Set in | |
2182 | -- the body of Sem_Case, and it is possible we could just reuse | |
2183 | -- that procedure. To be checked ??? | |
996ae0b0 RK |
2184 | |
2185 | if Nb_Discrete_Choices > 1 then | |
882eadaf RD |
2186 | Check_Choices : declare |
2187 | Choice : Node_Id; | |
2188 | -- Location of choice for messages | |
996ae0b0 | 2189 | |
882eadaf RD |
2190 | Hi_Val : Uint; |
2191 | Lo_Val : Uint; | |
2192 | -- High end of one range and Low end of the next. Should be | |
2193 | -- contiguous if there is no hole in the list of values. | |
996ae0b0 | 2194 | |
82893775 AC |
2195 | Lo_Dup : Uint; |
2196 | Hi_Dup : Uint; | |
2197 | -- End points of duplicated range | |
2198 | ||
882eadaf RD |
2199 | Missing_Or_Duplicates : Boolean := False; |
2200 | -- Set True if missing or duplicate choices found | |
996ae0b0 | 2201 | |
882eadaf RD |
2202 | procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id); |
2203 | -- Output continuation message with a representation of the | |
2204 | -- bounds (just Lo if Lo = Hi, else Lo .. Hi). C is the | |
2205 | -- choice node where the message is to be posted. | |
996ae0b0 | 2206 | |
882eadaf RD |
2207 | ------------------------ |
2208 | -- Output_Bad_Choices -- | |
2209 | ------------------------ | |
996ae0b0 | 2210 | |
882eadaf RD |
2211 | procedure Output_Bad_Choices (Lo, Hi : Uint; C : Node_Id) is |
2212 | begin | |
2213 | -- Enumeration type case | |
996ae0b0 | 2214 | |
882eadaf RD |
2215 | if Is_Enumeration_Type (Index_Typ) then |
2216 | Error_Msg_Name_1 := | |
2217 | Chars (Get_Enum_Lit_From_Pos (Index_Typ, Lo, Loc)); | |
2218 | Error_Msg_Name_2 := | |
2219 | Chars (Get_Enum_Lit_From_Pos (Index_Typ, Hi, Loc)); | |
2220 | ||
2221 | if Lo = Hi then | |
2222 | Error_Msg_N ("\\ %!", C); | |
2223 | else | |
2224 | Error_Msg_N ("\\ % .. %!", C); | |
996ae0b0 RK |
2225 | end if; |
2226 | ||
882eadaf | 2227 | -- Integer types case |
996ae0b0 | 2228 | |
882eadaf RD |
2229 | else |
2230 | Error_Msg_Uint_1 := Lo; | |
2231 | Error_Msg_Uint_2 := Hi; | |
996ae0b0 | 2232 | |
882eadaf RD |
2233 | if Lo = Hi then |
2234 | Error_Msg_N ("\\ ^!", C); | |
2235 | else | |
2236 | Error_Msg_N ("\\ ^ .. ^!", C); | |
2237 | end if; | |
2238 | end if; | |
2239 | end Output_Bad_Choices; | |
996ae0b0 | 2240 | |
882eadaf | 2241 | -- Start of processing for Check_Choices |
996ae0b0 | 2242 | |
882eadaf RD |
2243 | begin |
2244 | Sort_Case_Table (Table); | |
996ae0b0 | 2245 | |
82893775 AC |
2246 | -- First we do a quick linear loop to find out if we have |
2247 | -- any duplicates or missing entries (usually we have a | |
2248 | -- legal aggregate, so this will get us out quickly). | |
996ae0b0 | 2249 | |
882eadaf | 2250 | for J in 1 .. Nb_Discrete_Choices - 1 loop |
82893775 AC |
2251 | Hi_Val := Expr_Value (Table (J).Hi); |
2252 | Lo_Val := Expr_Value (Table (J + 1).Lo); | |
882eadaf | 2253 | |
82893775 AC |
2254 | if Lo_Val <= Hi_Val |
2255 | or else (Lo_Val > Hi_Val + 1 | |
2256 | and then not Others_Present) | |
2257 | then | |
882eadaf | 2258 | Missing_Or_Duplicates := True; |
82893775 | 2259 | exit; |
882eadaf RD |
2260 | end if; |
2261 | end loop; | |
996ae0b0 | 2262 | |
82893775 AC |
2263 | -- If we have missing or duplicate entries, first fill in |
2264 | -- the Highest entries to make life easier in the following | |
2265 | -- loops to detect bad entries. | |
882eadaf | 2266 | |
82893775 AC |
2267 | if Missing_Or_Duplicates then |
2268 | Table (1).Highest := Expr_Value (Table (1).Hi); | |
882eadaf | 2269 | |
82893775 AC |
2270 | for J in 2 .. Nb_Discrete_Choices loop |
2271 | Table (J).Highest := | |
2272 | UI_Max | |
2273 | (Table (J - 1).Highest, Expr_Value (Table (J).Hi)); | |
2274 | end loop; | |
882eadaf | 2275 | |
82893775 AC |
2276 | -- Loop through table entries to find duplicate indexes |
2277 | ||
2278 | for J in 2 .. Nb_Discrete_Choices loop | |
2279 | Lo_Val := Expr_Value (Table (J).Lo); | |
2280 | Hi_Val := Expr_Value (Table (J).Hi); | |
2281 | ||
2282 | -- Case where we have duplicates (the lower bound of | |
2283 | -- this choice is less than or equal to the highest | |
2284 | -- high bound found so far). | |
2285 | ||
2286 | if Lo_Val <= Table (J - 1).Highest then | |
2287 | ||
2288 | -- We move backwards looking for duplicates. We can | |
2289 | -- abandon this loop as soon as we reach a choice | |
2290 | -- highest value that is less than Lo_Val. | |
2291 | ||
2292 | for K in reverse 1 .. J - 1 loop | |
2293 | exit when Table (K).Highest < Lo_Val; | |
2294 | ||
2295 | -- Here we may have duplicates between entries | |
2296 | -- for K and J. Get range of duplicates. | |
2297 | ||
2298 | Lo_Dup := | |
2299 | UI_Max (Lo_Val, Expr_Value (Table (K).Lo)); | |
2300 | Hi_Dup := | |
2301 | UI_Min (Hi_Val, Expr_Value (Table (K).Hi)); | |
2302 | ||
2303 | -- Nothing to do if duplicate range is null | |
882eadaf | 2304 | |
82893775 AC |
2305 | if Lo_Dup > Hi_Dup then |
2306 | null; | |
2307 | ||
65f1ca2e | 2308 | -- Otherwise place proper message |
82893775 AC |
2309 | |
2310 | else | |
2311 | -- We place message on later choice, with a | |
2312 | -- line reference to the earlier choice. | |
2313 | ||
2314 | if Sloc (Table (J).Choice) < | |
2315 | Sloc (Table (K).Choice) | |
2316 | then | |
2317 | Choice := Table (K).Choice; | |
2318 | Error_Msg_Sloc := Sloc (Table (J).Choice); | |
2319 | else | |
2320 | Choice := Table (J).Choice; | |
2321 | Error_Msg_Sloc := Sloc (Table (K).Choice); | |
2322 | end if; | |
2323 | ||
2324 | if Lo_Dup = Hi_Dup then | |
2325 | Error_Msg_N | |
2326 | ("index value in array aggregate " | |
2327 | & "duplicates the one given#!", Choice); | |
2328 | else | |
2329 | Error_Msg_N | |
2330 | ("index values in array aggregate " | |
2331 | & "duplicate those given#!", Choice); | |
2332 | end if; | |
2333 | ||
2334 | Output_Bad_Choices (Lo_Dup, Hi_Dup, Choice); | |
2335 | end if; | |
2336 | end loop; | |
996ae0b0 | 2337 | end if; |
882eadaf | 2338 | end loop; |
996ae0b0 | 2339 | |
82893775 AC |
2340 | -- Loop through entries in table to find missing indexes. |
2341 | -- Not needed if others, since missing impossible. | |
2342 | ||
2343 | if not Others_Present then | |
2344 | for J in 2 .. Nb_Discrete_Choices loop | |
2345 | Lo_Val := Expr_Value (Table (J).Lo); | |
2346 | Hi_Val := Table (J - 1).Highest; | |
2347 | ||
2348 | if Lo_Val > Hi_Val + 1 then | |
16a569d2 | 2349 | |
7b4ebba5 AC |
2350 | declare |
2351 | Error_Node : Node_Id; | |
16a569d2 | 2352 | |
7b4ebba5 AC |
2353 | begin |
2354 | -- If the choice is the bound of a range in | |
2355 | -- a subtype indication, it is not in the | |
2356 | -- source lists for the aggregate itself, so | |
2357 | -- post the error on the aggregate. Otherwise | |
2358 | -- post it on choice itself. | |
16a569d2 | 2359 | |
7b4ebba5 | 2360 | Choice := Table (J).Choice; |
16a569d2 | 2361 | |
7b4ebba5 AC |
2362 | if Is_List_Member (Choice) then |
2363 | Error_Node := Choice; | |
2364 | else | |
2365 | Error_Node := N; | |
2366 | end if; | |
82893775 | 2367 | |
7b4ebba5 AC |
2368 | if Hi_Val + 1 = Lo_Val - 1 then |
2369 | Error_Msg_N | |
2370 | ("missing index value " | |
2371 | & "in array aggregate!", Error_Node); | |
2372 | else | |
2373 | Error_Msg_N | |
2374 | ("missing index values " | |
2375 | & "in array aggregate!", Error_Node); | |
2376 | end if; | |
82893775 | 2377 | |
7b4ebba5 AC |
2378 | Output_Bad_Choices |
2379 | (Hi_Val + 1, Lo_Val - 1, Error_Node); | |
2380 | end; | |
82893775 AC |
2381 | end if; |
2382 | end loop; | |
2383 | end if; | |
2384 | ||
2385 | -- If either missing or duplicate values, return failure | |
882eadaf | 2386 | |
882eadaf RD |
2387 | Set_Etype (N, Any_Composite); |
2388 | return Failure; | |
2389 | end if; | |
2390 | end Check_Choices; | |
996ae0b0 RK |
2391 | end if; |
2392 | ||
2393 | -- STEP 2 (B): Compute aggregate bounds and min/max choices values | |
2394 | ||
2395 | if Nb_Discrete_Choices > 0 then | |
82893775 AC |
2396 | Choices_Low := Table (1).Lo; |
2397 | Choices_High := Table (Nb_Discrete_Choices).Hi; | |
996ae0b0 RK |
2398 | end if; |
2399 | ||
ca44152f ES |
2400 | -- If Others is present, then bounds of aggregate come from the |
2401 | -- index constraint (not the choices in the aggregate itself). | |
2402 | ||
996ae0b0 RK |
2403 | if Others_Present then |
2404 | Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High); | |
2405 | ||
596b25f9 AC |
2406 | -- Abandon processing if either bound is already signalled as |
2407 | -- an error (prevents junk cascaded messages and blow ups). | |
2408 | ||
2409 | if Nkind (Aggr_Low) = N_Error | |
2410 | or else | |
2411 | Nkind (Aggr_High) = N_Error | |
2412 | then | |
2413 | return False; | |
2414 | end if; | |
2415 | ||
ca44152f ES |
2416 | -- No others clause present |
2417 | ||
996ae0b0 | 2418 | else |
ca44152f ES |
2419 | -- Special processing if others allowed and not present. This |
2420 | -- means that the bounds of the aggregate come from the index | |
2421 | -- constraint (and the length must match). | |
2422 | ||
2423 | if Others_Allowed then | |
2424 | Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High); | |
2425 | ||
596b25f9 AC |
2426 | -- Abandon processing if either bound is already signalled |
2427 | -- as an error (stop junk cascaded messages and blow ups). | |
2428 | ||
2429 | if Nkind (Aggr_Low) = N_Error | |
2430 | or else | |
2431 | Nkind (Aggr_High) = N_Error | |
2432 | then | |
2433 | return False; | |
2434 | end if; | |
2435 | ||
ca44152f ES |
2436 | -- If others allowed, and no others present, then the array |
2437 | -- should cover all index values. If it does not, we will | |
2438 | -- get a length check warning, but there is two cases where | |
2439 | -- an additional warning is useful: | |
2440 | ||
2441 | -- If we have no positional components, and the length is | |
2442 | -- wrong (which we can tell by others being allowed with | |
2443 | -- missing components), and the index type is an enumeration | |
2444 | -- type, then issue appropriate warnings about these missing | |
2445 | -- components. They are only warnings, since the aggregate | |
2446 | -- is fine, it's just the wrong length. We skip this check | |
2447 | -- for standard character types (since there are no literals | |
2448 | -- and it is too much trouble to concoct them), and also if | |
50ea6357 AC |
2449 | -- any of the bounds have values that are not known at |
2450 | -- compile time. | |
ca44152f | 2451 | |
58009744 AC |
2452 | -- Another case warranting a warning is when the length |
2453 | -- is right, but as above we have an index type that is | |
2454 | -- an enumeration, and the bounds do not match. This is a | |
2455 | -- case where dubious sliding is allowed and we generate a | |
2456 | -- warning that the bounds do not match. | |
ca44152f ES |
2457 | |
2458 | if No (Expressions (N)) | |
2459 | and then Nkind (Index) = N_Range | |
2460 | and then Is_Enumeration_Type (Etype (Index)) | |
2461 | and then not Is_Standard_Character_Type (Etype (Index)) | |
2462 | and then Compile_Time_Known_Value (Aggr_Low) | |
2463 | and then Compile_Time_Known_Value (Aggr_High) | |
2464 | and then Compile_Time_Known_Value (Choices_Low) | |
2465 | and then Compile_Time_Known_Value (Choices_High) | |
2466 | then | |
c0b11850 AC |
2467 | -- If any of the expressions or range bounds in choices |
2468 | -- have semantic errors, then do not attempt further | |
2469 | -- resolution, to prevent cascaded errors. | |
d610088d | 2470 | |
c0b11850 AC |
2471 | if Errors_Posted_On_Choices then |
2472 | return Failure; | |
d610088d AC |
2473 | end if; |
2474 | ||
ca44152f ES |
2475 | declare |
2476 | ALo : constant Node_Id := Expr_Value_E (Aggr_Low); | |
2477 | AHi : constant Node_Id := Expr_Value_E (Aggr_High); | |
2478 | CLo : constant Node_Id := Expr_Value_E (Choices_Low); | |
2479 | CHi : constant Node_Id := Expr_Value_E (Choices_High); | |
2480 | ||
2481 | Ent : Entity_Id; | |
2482 | ||
2483 | begin | |
ebd34478 | 2484 | -- Warning case 1, missing values at start/end. Only |
ca44152f ES |
2485 | -- do the check if the number of entries is too small. |
2486 | ||
2487 | if (Enumeration_Pos (CHi) - Enumeration_Pos (CLo)) | |
2488 | < | |
2489 | (Enumeration_Pos (AHi) - Enumeration_Pos (ALo)) | |
2490 | then | |
2491 | Error_Msg_N | |
324ac540 AC |
2492 | ("missing index value(s) in array aggregate??", |
2493 | N); | |
ca44152f ES |
2494 | |
2495 | -- Output missing value(s) at start | |
2496 | ||
2497 | if Chars (ALo) /= Chars (CLo) then | |
2498 | Ent := Prev (CLo); | |
2499 | ||
2500 | if Chars (ALo) = Chars (Ent) then | |
2501 | Error_Msg_Name_1 := Chars (ALo); | |
324ac540 | 2502 | Error_Msg_N ("\ %??", N); |
ca44152f ES |
2503 | else |
2504 | Error_Msg_Name_1 := Chars (ALo); | |
2505 | Error_Msg_Name_2 := Chars (Ent); | |
324ac540 | 2506 | Error_Msg_N ("\ % .. %??", N); |
ca44152f ES |
2507 | end if; |
2508 | end if; | |
2509 | ||
2510 | -- Output missing value(s) at end | |
2511 | ||
2512 | if Chars (AHi) /= Chars (CHi) then | |
2513 | Ent := Next (CHi); | |
2514 | ||
2515 | if Chars (AHi) = Chars (Ent) then | |
2516 | Error_Msg_Name_1 := Chars (Ent); | |
324ac540 | 2517 | Error_Msg_N ("\ %??", N); |
ca44152f ES |
2518 | else |
2519 | Error_Msg_Name_1 := Chars (Ent); | |
2520 | Error_Msg_Name_2 := Chars (AHi); | |
324ac540 | 2521 | Error_Msg_N ("\ % .. %??", N); |
ca44152f ES |
2522 | end if; |
2523 | end if; | |
2524 | ||
2525 | -- Warning case 2, dubious sliding. The First_Subtype | |
2526 | -- test distinguishes between a constrained type where | |
2527 | -- sliding is not allowed (so we will get a warning | |
2528 | -- later that Constraint_Error will be raised), and | |
2529 | -- the unconstrained case where sliding is permitted. | |
2530 | ||
2531 | elsif (Enumeration_Pos (CHi) - Enumeration_Pos (CLo)) | |
2532 | = | |
2533 | (Enumeration_Pos (AHi) - Enumeration_Pos (ALo)) | |
2534 | and then Chars (ALo) /= Chars (CLo) | |
2535 | and then | |
2536 | not Is_Constrained (First_Subtype (Etype (N))) | |
2537 | then | |
2538 | Error_Msg_N | |
324ac540 | 2539 | ("bounds of aggregate do not match target??", N); |
ca44152f ES |
2540 | end if; |
2541 | end; | |
2542 | end if; | |
2543 | end if; | |
2544 | ||
f3d0f304 | 2545 | -- If no others, aggregate bounds come from aggregate |
ca44152f | 2546 | |
996ae0b0 RK |
2547 | Aggr_Low := Choices_Low; |
2548 | Aggr_High := Choices_High; | |
2549 | end if; | |
2550 | end Step_2; | |
2551 | ||
2552 | -- STEP 3: Process positional components | |
2553 | ||
2554 | else | |
2555 | -- STEP 3 (A): Process positional elements | |
2556 | ||
2557 | Expr := First (Expressions (N)); | |
2558 | Nb_Elements := Uint_0; | |
2559 | while Present (Expr) loop | |
2560 | Nb_Elements := Nb_Elements + 1; | |
2561 | ||
82c80734 RD |
2562 | -- Ada 2005 (AI-231) |
2563 | ||
58009744 | 2564 | if Ada_Version >= Ada_2005 and then Known_Null (Expr) then |
82c80734 RD |
2565 | Check_Can_Never_Be_Null (Etype (N), Expr); |
2566 | end if; | |
2820d220 | 2567 | |
ef74daea | 2568 | if not Resolve_Aggr_Expr (Expr, Single_Elmt => True) then |
996ae0b0 RK |
2569 | return Failure; |
2570 | end if; | |
2571 | ||
4755cce9 JM |
2572 | -- Check incorrect use of dynamically tagged expression |
2573 | ||
2574 | if Is_Tagged_Type (Etype (Expr)) then | |
2575 | Check_Dynamically_Tagged_Expression | |
2576 | (Expr => Expr, | |
2577 | Typ => Component_Type (Etype (N)), | |
2578 | Related_Nod => N); | |
2579 | end if; | |
2580 | ||
996ae0b0 RK |
2581 | Next (Expr); |
2582 | end loop; | |
2583 | ||
2584 | if Others_Present then | |
2585 | Assoc := Last (Component_Associations (N)); | |
c45b6ae0 | 2586 | |
82c80734 RD |
2587 | -- Ada 2005 (AI-231) |
2588 | ||
58009744 | 2589 | if Ada_Version >= Ada_2005 and then Known_Null (Assoc) then |
9b96e234 | 2590 | Check_Can_Never_Be_Null (Etype (N), Expression (Assoc)); |
82c80734 | 2591 | end if; |
2820d220 | 2592 | |
ebd34478 | 2593 | -- Ada 2005 (AI-287): In case of default initialized component, |
c45b6ae0 AC |
2594 | -- we delay the resolution to the expansion phase. |
2595 | ||
2596 | if Box_Present (Assoc) then | |
2597 | ||
ebd34478 AC |
2598 | -- Ada 2005 (AI-287): In case of default initialization of a |
2599 | -- component the expander will generate calls to the | |
ca5af305 AC |
2600 | -- corresponding initialization subprogram. We need to call |
2601 | -- Resolve_Aggr_Expr to check the rules about | |
2602 | -- dimensionality. | |
c45b6ae0 | 2603 | |
ca5af305 AC |
2604 | if not Resolve_Aggr_Expr (Assoc, Single_Elmt => False) then |
2605 | return Failure; | |
2606 | end if; | |
c45b6ae0 AC |
2607 | |
2608 | elsif not Resolve_Aggr_Expr (Expression (Assoc), | |
2609 | Single_Elmt => False) | |
996ae0b0 RK |
2610 | then |
2611 | return Failure; | |
4755cce9 JM |
2612 | |
2613 | -- Check incorrect use of dynamically tagged expression. The | |
2614 | -- expression of the others choice has not been resolved yet. | |
2615 | -- In order to diagnose the semantic error we create a duplicate | |
2616 | -- tree to analyze it and perform the check. | |
2617 | ||
fb00cc70 | 2618 | elsif Nkind (Assoc) /= N_Iterated_Component_Association then |
4755cce9 JM |
2619 | declare |
2620 | Save_Analysis : constant Boolean := Full_Analysis; | |
2621 | Expr : constant Node_Id := | |
2622 | New_Copy_Tree (Expression (Assoc)); | |
2623 | ||
2624 | begin | |
2625 | Expander_Mode_Save_And_Set (False); | |
2626 | Full_Analysis := False; | |
2627 | Analyze (Expr); | |
2628 | Full_Analysis := Save_Analysis; | |
2629 | Expander_Mode_Restore; | |
2630 | ||
2631 | if Is_Tagged_Type (Etype (Expr)) then | |
2632 | Check_Dynamically_Tagged_Expression | |
58009744 AC |
2633 | (Expr => Expr, |
2634 | Typ => Component_Type (Etype (N)), | |
4755cce9 JM |
2635 | Related_Nod => N); |
2636 | end if; | |
2637 | end; | |
996ae0b0 RK |
2638 | end if; |
2639 | end if; | |
2640 | ||
2641 | -- STEP 3 (B): Compute the aggregate bounds | |
2642 | ||
2643 | if Others_Present then | |
2644 | Get_Index_Bounds (Index_Constr, Aggr_Low, Aggr_High); | |
2645 | ||
2646 | else | |
2647 | if Others_Allowed then | |
f91e8020 | 2648 | Get_Index_Bounds (Index_Constr, Aggr_Low, Discard); |
996ae0b0 RK |
2649 | else |
2650 | Aggr_Low := Index_Typ_Low; | |
2651 | end if; | |
2652 | ||
2653 | Aggr_High := Add (Nb_Elements - 1, To => Aggr_Low); | |
2654 | Check_Bound (Index_Base_High, Aggr_High); | |
2655 | end if; | |
2656 | end if; | |
2657 | ||
2658 | -- STEP 4: Perform static aggregate checks and save the bounds | |
2659 | ||
2660 | -- Check (A) | |
2661 | ||
2662 | Check_Bounds (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High); | |
2663 | Check_Bounds (Index_Base_Low, Index_Base_High, Aggr_Low, Aggr_High); | |
2664 | ||
2665 | -- Check (B) | |
2666 | ||
2667 | if Others_Present and then Nb_Discrete_Choices > 0 then | |
2668 | Check_Bounds (Aggr_Low, Aggr_High, Choices_Low, Choices_High); | |
2669 | Check_Bounds (Index_Typ_Low, Index_Typ_High, | |
2670 | Choices_Low, Choices_High); | |
2671 | Check_Bounds (Index_Base_Low, Index_Base_High, | |
2672 | Choices_Low, Choices_High); | |
2673 | ||
2674 | -- Check (C) | |
2675 | ||
2676 | elsif Others_Present and then Nb_Elements > 0 then | |
2677 | Check_Length (Aggr_Low, Aggr_High, Nb_Elements); | |
2678 | Check_Length (Index_Typ_Low, Index_Typ_High, Nb_Elements); | |
2679 | Check_Length (Index_Base_Low, Index_Base_High, Nb_Elements); | |
996ae0b0 RK |
2680 | end if; |
2681 | ||
2682 | if Raises_Constraint_Error (Aggr_Low) | |
2683 | or else Raises_Constraint_Error (Aggr_High) | |
2684 | then | |
2685 | Set_Raises_Constraint_Error (N); | |
2686 | end if; | |
2687 | ||
2688 | Aggr_Low := Duplicate_Subexpr (Aggr_Low); | |
2689 | ||
2690 | -- Do not duplicate Aggr_High if Aggr_High = Aggr_Low + Nb_Elements | |
2691 | -- since the addition node returned by Add is not yet analyzed. Attach | |
ebd34478 | 2692 | -- to tree and analyze first. Reset analyzed flag to ensure it will get |
9b96e234 | 2693 | -- analyzed when it is a literal bound whose type must be properly set. |
996ae0b0 RK |
2694 | |
2695 | if Others_Present or else Nb_Discrete_Choices > 0 then | |
2696 | Aggr_High := Duplicate_Subexpr (Aggr_High); | |
2697 | ||
2698 | if Etype (Aggr_High) = Universal_Integer then | |
2699 | Set_Analyzed (Aggr_High, False); | |
2700 | end if; | |
2701 | end if; | |
2702 | ||
3d923671 AC |
2703 | -- If the aggregate already has bounds attached to it, it means this is |
2704 | -- a positional aggregate created as an optimization by | |
2705 | -- Exp_Aggr.Convert_To_Positional, so we don't want to change those | |
2706 | -- bounds. | |
2707 | ||
2708 | if Present (Aggregate_Bounds (N)) and then not Others_Allowed then | |
ebd34478 | 2709 | Aggr_Low := Low_Bound (Aggregate_Bounds (N)); |
3d923671 AC |
2710 | Aggr_High := High_Bound (Aggregate_Bounds (N)); |
2711 | end if; | |
2712 | ||
996ae0b0 RK |
2713 | Set_Aggregate_Bounds |
2714 | (N, Make_Range (Loc, Low_Bound => Aggr_Low, High_Bound => Aggr_High)); | |
2715 | ||
2716 | -- The bounds may contain expressions that must be inserted upwards. | |
2717 | -- Attach them fully to the tree. After analysis, remove side effects | |
2718 | -- from upper bound, if still needed. | |
2719 | ||
2720 | Set_Parent (Aggregate_Bounds (N), N); | |
2721 | Analyze_And_Resolve (Aggregate_Bounds (N), Index_Typ); | |
fbf5a39b | 2722 | Check_Unset_Reference (Aggregate_Bounds (N)); |
996ae0b0 RK |
2723 | |
2724 | if not Others_Present and then Nb_Discrete_Choices = 0 then | |
82893775 AC |
2725 | Set_High_Bound |
2726 | (Aggregate_Bounds (N), | |
2727 | Duplicate_Subexpr (High_Bound (Aggregate_Bounds (N)))); | |
996ae0b0 RK |
2728 | end if; |
2729 | ||
d976bf74 | 2730 | -- Check the dimensions of each component in the array aggregate |
0929eaeb AC |
2731 | |
2732 | Analyze_Dimension_Array_Aggregate (N, Component_Typ); | |
2733 | ||
996ae0b0 RK |
2734 | return Success; |
2735 | end Resolve_Array_Aggregate; | |
2736 | ||
745f5698 ES |
2737 | --------------------------------- |
2738 | -- Resolve_Container_Aggregate -- | |
2739 | --------------------------------- | |
2740 | ||
2741 | procedure Resolve_Container_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
c0bab60b | 2742 | procedure Resolve_Iterated_Association |
4f6ebe2a ES |
2743 | (Comp : Node_Id; |
2744 | Key_Type : Entity_Id; | |
2745 | Elmt_Type : Entity_Id); | |
c0bab60b ES |
2746 | -- Resolve choices and expression in an iterated component association |
2747 | -- or an iterated element association, which has a key_expression. | |
0b4034c0 GD |
2748 | -- This is similar but not identical to the handling of this construct |
2749 | -- in an array aggregate. | |
4f6ebe2a | 2750 | -- For a named container, the type of each choice must be compatible |
0b4034c0 | 2751 | -- with the key type. For a positional container, the choice must be |
4f6ebe2a ES |
2752 | -- a subtype indication or an iterator specification that determines |
2753 | -- an element type. | |
2754 | ||
745f5698 ES |
2755 | Asp : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Aggregate); |
2756 | ||
2757 | Empty_Subp : Node_Id := Empty; | |
2758 | Add_Named_Subp : Node_Id := Empty; | |
2759 | Add_Unnamed_Subp : Node_Id := Empty; | |
2760 | New_Indexed_Subp : Node_Id := Empty; | |
2761 | Assign_Indexed_Subp : Node_Id := Empty; | |
2762 | ||
c0bab60b ES |
2763 | ---------------------------------- |
2764 | -- Resolve_Iterated_Association -- | |
2765 | ---------------------------------- | |
4f6ebe2a | 2766 | |
c0bab60b | 2767 | procedure Resolve_Iterated_Association |
4f6ebe2a ES |
2768 | (Comp : Node_Id; |
2769 | Key_Type : Entity_Id; | |
2770 | Elmt_Type : Entity_Id) | |
2771 | is | |
c0bab60b ES |
2772 | Choice : Node_Id; |
2773 | Ent : Entity_Id; | |
2774 | Expr : Node_Id; | |
2775 | Key_Expr : Node_Id; | |
2776 | Id : Entity_Id; | |
2777 | Id_Name : Name_Id; | |
2778 | Iter : Node_Id; | |
2779 | Typ : Entity_Id := Empty; | |
4f6ebe2a ES |
2780 | |
2781 | begin | |
c0bab60b ES |
2782 | -- If this is an Iterated_Element_Association then either a |
2783 | -- an Iterator_Specification or a Loop_Parameter specification | |
2784 | -- is present. In both cases a Key_Expression is present. | |
2785 | ||
2786 | if Nkind (Comp) = N_Iterated_Element_Association then | |
2787 | if Present (Loop_Parameter_Specification (Comp)) then | |
2788 | Analyze_Loop_Parameter_Specification | |
2789 | (Loop_Parameter_Specification (Comp)); | |
2790 | Id_Name := Chars (Defining_Identifier | |
2791 | (Loop_Parameter_Specification (Comp))); | |
2792 | else | |
2793 | Iter := Copy_Separate_Tree (Iterator_Specification (Comp)); | |
2794 | Analyze (Iter); | |
2795 | Typ := Etype (Defining_Identifier (Iter)); | |
2796 | Id_Name := Chars (Defining_Identifier | |
2797 | (Iterator_Specification (Comp))); | |
2798 | end if; | |
2799 | ||
2800 | -- Key expression must have the type of the key. We analyze | |
2801 | -- a copy of the original expression, because it will be | |
2802 | -- reanalyzed and copied as needed during expansion of the | |
2803 | -- corresponding loop. | |
2804 | ||
2805 | Key_Expr := Key_Expression (Comp); | |
2806 | Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type); | |
2807 | ||
2808 | elsif Present (Iterator_Specification (Comp)) then | |
2809 | Iter := Copy_Separate_Tree (Iterator_Specification (Comp)); | |
2810 | Id_Name := Chars (Defining_Identifier (Comp)); | |
8092c199 AC |
2811 | Analyze (Iter); |
2812 | Typ := Etype (Defining_Identifier (Iter)); | |
4f6ebe2a | 2813 | |
8092c199 AC |
2814 | else |
2815 | Choice := First (Discrete_Choices (Comp)); | |
4f6ebe2a | 2816 | |
8092c199 AC |
2817 | while Present (Choice) loop |
2818 | Analyze (Choice); | |
4f6ebe2a | 2819 | |
8092c199 | 2820 | -- Choice can be a subtype name, a range, or an expression |
4f6ebe2a | 2821 | |
8092c199 AC |
2822 | if Is_Entity_Name (Choice) |
2823 | and then Is_Type (Entity (Choice)) | |
2824 | and then Base_Type (Entity (Choice)) = Base_Type (Key_Type) | |
2825 | then | |
2826 | null; | |
4f6ebe2a | 2827 | |
8092c199 AC |
2828 | elsif Present (Key_Type) then |
2829 | Analyze_And_Resolve (Choice, Key_Type); | |
4f6ebe2a | 2830 | |
8092c199 AC |
2831 | else |
2832 | Typ := Etype (Choice); -- assume unique for now | |
2833 | end if; | |
4f6ebe2a | 2834 | |
8092c199 AC |
2835 | Next (Choice); |
2836 | end loop; | |
c0bab60b ES |
2837 | |
2838 | Id_Name := Chars (Defining_Identifier (Comp)); | |
8092c199 | 2839 | end if; |
4f6ebe2a ES |
2840 | |
2841 | -- Create a scope in which to introduce an index, which is usually | |
2842 | -- visible in the expression for the component, and needed for its | |
2843 | -- analysis. | |
2844 | ||
c0bab60b | 2845 | Id := Make_Defining_Identifier (Sloc (Comp), Id_Name); |
4f6ebe2a ES |
2846 | Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L'); |
2847 | Set_Etype (Ent, Standard_Void_Type); | |
2848 | Set_Parent (Ent, Parent (Comp)); | |
2849 | Push_Scope (Ent); | |
4f6ebe2a ES |
2850 | |
2851 | -- Insert and decorate the loop variable in the current scope. | |
2852 | -- The expression has to be analyzed once the loop variable is | |
2853 | -- directly visible. Mark the variable as referenced to prevent | |
2854 | -- spurious warnings, given that subsequent uses of its name in the | |
2855 | -- expression will reference the internal (synonym) loop variable. | |
2856 | ||
2857 | Enter_Name (Id); | |
fa02302b | 2858 | |
4f6ebe2a | 2859 | if No (Key_Type) then |
fa02302b | 2860 | pragma Assert (Present (Typ)); |
4f6ebe2a ES |
2861 | Set_Etype (Id, Typ); |
2862 | else | |
2863 | Set_Etype (Id, Key_Type); | |
2864 | end if; | |
2865 | ||
2e02ab86 | 2866 | Mutate_Ekind (Id, E_Variable); |
4f6ebe2a ES |
2867 | Set_Scope (Id, Ent); |
2868 | Set_Referenced (Id); | |
2869 | ||
2870 | -- Analyze a copy of the expression, to verify legality. We use | |
2871 | -- a copy because the expression will be analyzed anew when the | |
2872 | -- enclosing aggregate is expanded, and the construct is rewritten | |
2873 | -- as a loop with a new index variable. | |
2874 | ||
2875 | Expr := New_Copy_Tree (Expression (Comp)); | |
2876 | Preanalyze_And_Resolve (Expr, Elmt_Type); | |
2877 | End_Scope; | |
c0bab60b ES |
2878 | |
2879 | end Resolve_Iterated_Association; | |
4f6ebe2a | 2880 | |
745f5698 | 2881 | begin |
4f6ebe2a | 2882 | pragma Assert (Nkind (Asp) = N_Aggregate); |
745f5698 | 2883 | |
4f6ebe2a ES |
2884 | Set_Etype (N, Typ); |
2885 | Parse_Aspect_Aggregate (Asp, | |
2886 | Empty_Subp, Add_Named_Subp, Add_Unnamed_Subp, | |
2887 | New_Indexed_Subp, Assign_Indexed_Subp); | |
2888 | ||
7a21651f ES |
2889 | if Present (Add_Unnamed_Subp) |
2890 | and then No (New_Indexed_Subp) | |
2891 | then | |
4f6ebe2a ES |
2892 | declare |
2893 | Elmt_Type : constant Entity_Id := | |
2894 | Etype (Next_Formal | |
2895 | (First_Formal (Entity (Add_Unnamed_Subp)))); | |
2896 | Comp : Node_Id; | |
2897 | ||
2898 | begin | |
2899 | if Present (Expressions (N)) then | |
2900 | -- positional aggregate | |
2901 | ||
2902 | Comp := First (Expressions (N)); | |
2903 | while Present (Comp) loop | |
2904 | Analyze_And_Resolve (Comp, Elmt_Type); | |
2905 | Next (Comp); | |
2906 | end loop; | |
2907 | end if; | |
745f5698 | 2908 | |
4f6ebe2a ES |
2909 | -- Empty aggregate, to be replaced by Empty during |
2910 | -- expansion, or iterated component association. | |
2911 | ||
2912 | if Present (Component_Associations (N)) then | |
2913 | declare | |
2914 | Comp : Node_Id := First (Component_Associations (N)); | |
2915 | begin | |
745f5698 | 2916 | while Present (Comp) loop |
4f6ebe2a ES |
2917 | if Nkind (Comp) /= |
2918 | N_Iterated_Component_Association | |
2919 | then | |
2920 | Error_Msg_N ("illegal component association " | |
2921 | & "for unnamed container aggregate", Comp); | |
2922 | return; | |
2923 | else | |
c0bab60b | 2924 | Resolve_Iterated_Association |
4f6ebe2a ES |
2925 | (Comp, Empty, Elmt_Type); |
2926 | end if; | |
2927 | ||
745f5698 ES |
2928 | Next (Comp); |
2929 | end loop; | |
4f6ebe2a ES |
2930 | end; |
2931 | end if; | |
2932 | end; | |
745f5698 | 2933 | |
4f6ebe2a ES |
2934 | elsif Present (Add_Named_Subp) then |
2935 | declare | |
2936 | -- Retrieves types of container, key, and element from the | |
2937 | -- specified insertion procedure. | |
2938 | ||
2939 | Container : constant Entity_Id := | |
2940 | First_Formal (Entity (Add_Named_Subp)); | |
2941 | Key_Type : constant Entity_Id := Etype (Next_Formal (Container)); | |
2942 | Elmt_Type : constant Entity_Id := | |
2943 | Etype (Next_Formal (Next_Formal (Container))); | |
2944 | Comp : Node_Id; | |
2945 | Choice : Node_Id; | |
2946 | ||
2947 | begin | |
2948 | Comp := First (Component_Associations (N)); | |
2949 | while Present (Comp) loop | |
2950 | if Nkind (Comp) = N_Component_Association then | |
2951 | Choice := First (Choices (Comp)); | |
2952 | ||
2953 | while Present (Choice) loop | |
2954 | Analyze_And_Resolve (Choice, Key_Type); | |
7a21651f | 2955 | if not Is_Static_Expression (Choice) then |
9ed2b86d | 2956 | Error_Msg_N ("choice must be static", Choice); |
7a21651f ES |
2957 | end if; |
2958 | ||
4f6ebe2a ES |
2959 | Next (Choice); |
2960 | end loop; | |
2961 | ||
2962 | Analyze_And_Resolve (Expression (Comp), Elmt_Type); | |
2963 | ||
c0bab60b ES |
2964 | elsif Nkind (Comp) in |
2965 | N_Iterated_Component_Association | | |
2966 | N_Iterated_Element_Association | |
2967 | then | |
2968 | Resolve_Iterated_Association | |
4f6ebe2a | 2969 | (Comp, Key_Type, Elmt_Type); |
745f5698 | 2970 | end if; |
4f6ebe2a ES |
2971 | |
2972 | Next (Comp); | |
2973 | end loop; | |
2974 | end; | |
7a21651f | 2975 | |
4f6ebe2a | 2976 | else |
08c8883f ES |
2977 | -- Indexed Aggregate. Positional or indexed component |
2978 | -- can be present, but not both. Choices must be static | |
2979 | -- values or ranges with static bounds. | |
7a21651f ES |
2980 | |
2981 | declare | |
2982 | Container : constant Entity_Id := | |
2983 | First_Formal (Entity (Assign_Indexed_Subp)); | |
2984 | Index_Type : constant Entity_Id := Etype (Next_Formal (Container)); | |
2985 | Comp_Type : constant Entity_Id := | |
2986 | Etype (Next_Formal (Next_Formal (Container))); | |
d4780139 ES |
2987 | Comp : Node_Id; |
2988 | Choice : Node_Id; | |
2989 | Num_Choices : Nat := 0; | |
7a21651f | 2990 | |
d4780139 ES |
2991 | Hi_Val : Uint; |
2992 | Lo_Val : Uint; | |
7a21651f ES |
2993 | begin |
2994 | if Present (Expressions (N)) then | |
2995 | Comp := First (Expressions (N)); | |
2996 | while Present (Comp) loop | |
2997 | Analyze_And_Resolve (Comp, Comp_Type); | |
2998 | Next (Comp); | |
2999 | end loop; | |
3000 | end if; | |
3001 | ||
3002 | if Present (Component_Associations (N)) then | |
08c8883f | 3003 | if Present (Expressions (N)) then |
9ed2b86d | 3004 | Error_Msg_N ("container aggregate cannot be " |
08c8883f ES |
3005 | & "both positional and named", N); |
3006 | return; | |
3007 | end if; | |
3008 | ||
d4780139 | 3009 | Comp := First (Component_Associations (N)); |
7a21651f ES |
3010 | |
3011 | while Present (Comp) loop | |
3012 | if Nkind (Comp) = N_Component_Association then | |
3013 | Choice := First (Choices (Comp)); | |
3014 | ||
3015 | while Present (Choice) loop | |
3016 | Analyze_And_Resolve (Choice, Index_Type); | |
d4780139 | 3017 | Num_Choices := Num_Choices + 1; |
7a21651f ES |
3018 | Next (Choice); |
3019 | end loop; | |
3020 | ||
3021 | Analyze_And_Resolve (Expression (Comp), Comp_Type); | |
3022 | ||
c0bab60b ES |
3023 | elsif Nkind (Comp) in |
3024 | N_Iterated_Component_Association | | |
3025 | N_Iterated_Element_Association | |
3026 | then | |
3027 | Resolve_Iterated_Association | |
7a21651f | 3028 | (Comp, Index_Type, Comp_Type); |
d4780139 | 3029 | Num_Choices := Num_Choices + 1; |
7a21651f ES |
3030 | end if; |
3031 | ||
3032 | Next (Comp); | |
3033 | end loop; | |
d4780139 ES |
3034 | |
3035 | -- The component associations in an indexed aggregate | |
3036 | -- must denote a contiguous set of static values. We | |
3037 | -- build a table of values/ranges and sort it, as is done | |
3038 | -- elsewhere for case statements and array aggregates. | |
3039 | -- If the aggregate has a single iterated association it | |
3040 | -- is allowed to be nonstatic and there is nothing to check. | |
3041 | ||
3042 | if Num_Choices > 1 then | |
3043 | declare | |
3044 | Table : Case_Table_Type (1 .. Num_Choices); | |
3045 | No_Choice : Pos := 1; | |
3046 | Lo, Hi : Node_Id; | |
3047 | ||
3048 | -- Traverse aggregate to determine size of needed table. | |
3049 | -- Verify that bounds are static and that loops have no | |
3050 | -- filters or key expressions. | |
3051 | ||
3052 | begin | |
3053 | Comp := First (Component_Associations (N)); | |
3054 | while Present (Comp) loop | |
3055 | if Nkind (Comp) = N_Iterated_Element_Association then | |
3056 | if Present | |
3057 | (Loop_Parameter_Specification (Comp)) | |
3058 | then | |
3059 | if Present (Iterator_Filter | |
3060 | (Loop_Parameter_Specification (Comp))) | |
3061 | then | |
3062 | Error_Msg_N | |
3063 | ("iterator filter not allowed " & | |
3064 | "in indexed aggregate", Comp); | |
3065 | return; | |
3066 | ||
3067 | elsif Present (Key_Expression | |
3068 | (Loop_Parameter_Specification (Comp))) | |
3069 | then | |
3070 | Error_Msg_N | |
3071 | ("key expression not allowed " & | |
3072 | "in indexed aggregate", Comp); | |
3073 | return; | |
3074 | end if; | |
3075 | end if; | |
3076 | else | |
3077 | Choice := First (Choices (Comp)); | |
3078 | ||
3079 | while Present (Choice) loop | |
3080 | Get_Index_Bounds (Choice, Lo, Hi); | |
3081 | Table (No_Choice).Choice := Choice; | |
3082 | Table (No_Choice).Lo := Lo; | |
3083 | Table (No_Choice).Hi := Hi; | |
3084 | ||
3085 | -- Verify staticness of value or range | |
3086 | ||
3087 | if not Is_Static_Expression (Lo) | |
3088 | or else not Is_Static_Expression (Hi) | |
3089 | then | |
3090 | Error_Msg_N | |
3091 | ("nonstatic expression for index " & | |
3092 | "for indexed aggregate", Choice); | |
3093 | return; | |
3094 | end if; | |
3095 | ||
3096 | No_Choice := No_Choice + 1; | |
3097 | Next (Choice); | |
3098 | end loop; | |
3099 | end if; | |
3100 | ||
3101 | Next (Comp); | |
3102 | end loop; | |
3103 | ||
3104 | Sort_Case_Table (Table); | |
3105 | ||
3106 | for J in 1 .. Num_Choices - 1 loop | |
3107 | Hi_Val := Expr_Value (Table (J).Hi); | |
3108 | Lo_Val := Expr_Value (Table (J + 1).Lo); | |
3109 | ||
3110 | if Lo_Val = Hi_Val then | |
3111 | Error_Msg_N | |
3112 | ("duplicate index in indexed aggregate", | |
3113 | Table (J + 1).Choice); | |
3114 | exit; | |
3115 | ||
3116 | elsif Lo_Val < Hi_Val then | |
3117 | Error_Msg_N | |
3118 | ("overlapping indices in indexed aggregate", | |
3119 | Table (J + 1).Choice); | |
3120 | exit; | |
3121 | ||
3122 | elsif Lo_Val > Hi_Val + 1 then | |
3123 | Error_Msg_N | |
3124 | ("missing index values", Table (J + 1).Choice); | |
3125 | exit; | |
3126 | end if; | |
3127 | end loop; | |
3128 | end; | |
3129 | end if; | |
7a21651f ES |
3130 | end if; |
3131 | end; | |
745f5698 ES |
3132 | end if; |
3133 | end Resolve_Container_Aggregate; | |
3134 | ||
9eb8d5b4 AC |
3135 | ----------------------------- |
3136 | -- Resolve_Delta_Aggregate -- | |
3137 | ----------------------------- | |
3138 | ||
3139 | procedure Resolve_Delta_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
d63199d8 | 3140 | Base : constant Node_Id := Expression (N); |
8d9a1ba7 PMR |
3141 | |
3142 | begin | |
81e68a19 | 3143 | Error_Msg_Ada_2022_Feature ("delta aggregate", Sloc (N)); |
32501d71 | 3144 | |
8d9a1ba7 PMR |
3145 | if not Is_Composite_Type (Typ) then |
3146 | Error_Msg_N ("not a composite type", N); | |
3147 | end if; | |
3148 | ||
3149 | Analyze_And_Resolve (Base, Typ); | |
3150 | ||
3151 | if Is_Array_Type (Typ) then | |
3152 | Resolve_Delta_Array_Aggregate (N, Typ); | |
3153 | else | |
3154 | Resolve_Delta_Record_Aggregate (N, Typ); | |
3155 | end if; | |
3156 | ||
3157 | Set_Etype (N, Typ); | |
3158 | end Resolve_Delta_Aggregate; | |
3159 | ||
3160 | ----------------------------------- | |
3161 | -- Resolve_Delta_Array_Aggregate -- | |
3162 | ----------------------------------- | |
3163 | ||
3164 | procedure Resolve_Delta_Array_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
845c4936 PT |
3165 | Deltas : constant List_Id := Component_Associations (N); |
3166 | Index_Type : constant Entity_Id := Etype (First_Index (Typ)); | |
d63199d8 | 3167 | |
845c4936 PT |
3168 | Assoc : Node_Id; |
3169 | Choice : Node_Id; | |
cfc3a1db | 3170 | Expr : Node_Id; |
9eb8d5b4 | 3171 | |
8d9a1ba7 | 3172 | begin |
8d9a1ba7 PMR |
3173 | Assoc := First (Deltas); |
3174 | while Present (Assoc) loop | |
3175 | if Nkind (Assoc) = N_Iterated_Component_Association then | |
3176 | Choice := First (Choice_List (Assoc)); | |
3177 | while Present (Choice) loop | |
3178 | if Nkind (Choice) = N_Others_Choice then | |
3179 | Error_Msg_N | |
9ed2b86d | 3180 | ("OTHERS not allowed in delta aggregate", Choice); |
9eb8d5b4 | 3181 | |
3e65b68d PT |
3182 | elsif Nkind (Choice) = N_Subtype_Indication then |
3183 | Resolve_Discrete_Subtype_Indication | |
3184 | (Choice, Base_Type (Index_Type)); | |
3185 | ||
8d9a1ba7 PMR |
3186 | else |
3187 | Analyze_And_Resolve (Choice, Index_Type); | |
3188 | end if; | |
9eb8d5b4 | 3189 | |
8d9a1ba7 PMR |
3190 | Next (Choice); |
3191 | end loop; | |
9eb8d5b4 | 3192 | |
8d9a1ba7 PMR |
3193 | declare |
3194 | Id : constant Entity_Id := Defining_Identifier (Assoc); | |
3195 | Ent : constant Entity_Id := | |
3196 | New_Internal_Entity | |
3197 | (E_Loop, Current_Scope, Sloc (Assoc), 'L'); | |
9eb8d5b4 | 3198 | |
8d9a1ba7 PMR |
3199 | begin |
3200 | Set_Etype (Ent, Standard_Void_Type); | |
3201 | Set_Parent (Ent, Assoc); | |
70b590e2 | 3202 | Push_Scope (Ent); |
8d9a1ba7 PMR |
3203 | |
3204 | if No (Scope (Id)) then | |
8d9a1ba7 | 3205 | Set_Etype (Id, Index_Type); |
2e02ab86 | 3206 | Mutate_Ekind (Id, E_Variable); |
8d9a1ba7 | 3207 | Set_Scope (Id, Ent); |
9eb8d5b4 | 3208 | end if; |
70b590e2 | 3209 | Enter_Name (Id); |
9eb8d5b4 | 3210 | |
cfc3a1db ES |
3211 | -- Resolve a copy of the expression, after setting |
3212 | -- its parent properly to preserve its context. | |
3213 | ||
3214 | Expr := New_Copy_Tree (Expression (Assoc)); | |
3215 | Set_Parent (Expr, Assoc); | |
3216 | Analyze_And_Resolve (Expr, Component_Type (Typ)); | |
8d9a1ba7 PMR |
3217 | End_Scope; |
3218 | end; | |
9eb8d5b4 | 3219 | |
8d9a1ba7 PMR |
3220 | else |
3221 | Choice := First (Choice_List (Assoc)); | |
3222 | while Present (Choice) loop | |
3e65b68d PT |
3223 | Analyze (Choice); |
3224 | ||
8d9a1ba7 PMR |
3225 | if Nkind (Choice) = N_Others_Choice then |
3226 | Error_Msg_N | |
9ed2b86d | 3227 | ("OTHERS not allowed in delta aggregate", Choice); |
9eb8d5b4 | 3228 | |
3e65b68d PT |
3229 | elsif Is_Entity_Name (Choice) |
3230 | and then Is_Type (Entity (Choice)) | |
3231 | then | |
3232 | -- Choice covers a range of values | |
d63199d8 | 3233 | |
3e65b68d PT |
3234 | if Base_Type (Entity (Choice)) /= |
3235 | Base_Type (Index_Type) | |
8d9a1ba7 | 3236 | then |
3e65b68d PT |
3237 | Error_Msg_NE |
3238 | ("choice does not match index type of &", | |
3239 | Choice, Typ); | |
8d9a1ba7 | 3240 | end if; |
3e65b68d PT |
3241 | |
3242 | elsif Nkind (Choice) = N_Subtype_Indication then | |
3243 | Resolve_Discrete_Subtype_Indication | |
3244 | (Choice, Base_Type (Index_Type)); | |
3245 | ||
3246 | else | |
3247 | Resolve (Choice, Index_Type); | |
8d9a1ba7 | 3248 | end if; |
9eb8d5b4 | 3249 | |
8d9a1ba7 PMR |
3250 | Next (Choice); |
3251 | end loop; | |
3252 | ||
3253 | Analyze_And_Resolve (Expression (Assoc), Component_Type (Typ)); | |
3254 | end if; | |
9313a26a | 3255 | |
8d9a1ba7 PMR |
3256 | Next (Assoc); |
3257 | end loop; | |
3258 | end Resolve_Delta_Array_Aggregate; | |
3259 | ||
3260 | ------------------------------------ | |
3261 | -- Resolve_Delta_Record_Aggregate -- | |
3262 | ------------------------------------ | |
3263 | ||
3ec54569 PMR |
3264 | procedure Resolve_Delta_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is |
3265 | ||
3266 | -- Variables used to verify that discriminant-dependent components | |
3267 | -- appear in the same variant. | |
3268 | ||
5612989e | 3269 | Comp_Ref : Entity_Id := Empty; -- init to avoid warning |
3ec54569 PMR |
3270 | Variant : Node_Id; |
3271 | ||
8d9a1ba7 PMR |
3272 | procedure Check_Variant (Id : Entity_Id); |
3273 | -- If a given component of the delta aggregate appears in a variant | |
3274 | -- part, verify that it is within the same variant as that of previous | |
3275 | -- specified variant components of the delta. | |
9313a26a | 3276 | |
484d58c5 PT |
3277 | function Get_Component (Nam : Node_Id) return Entity_Id; |
3278 | -- Locate component with a given name and return it. If none found then | |
3279 | -- report error and return Empty. | |
d63199d8 PMR |
3280 | |
3281 | function Nested_In (V1 : Node_Id; V2 : Node_Id) return Boolean; | |
3282 | -- Determine whether variant V1 is within variant V2 | |
9eb8d5b4 | 3283 | |
8d9a1ba7 PMR |
3284 | function Variant_Depth (N : Node_Id) return Integer; |
3285 | -- Determine the distance of a variant to the enclosing type | |
3286 | -- declaration. | |
3287 | ||
3288 | -------------------- | |
3289 | -- Check_Variant -- | |
3290 | -------------------- | |
3291 | ||
3292 | procedure Check_Variant (Id : Entity_Id) is | |
3293 | Comp : Entity_Id; | |
3294 | Comp_Variant : Node_Id; | |
3295 | ||
3296 | begin | |
3297 | if not Has_Discriminants (Typ) then | |
3298 | return; | |
3299 | end if; | |
9eb8d5b4 | 3300 | |
8d9a1ba7 PMR |
3301 | Comp := First_Entity (Typ); |
3302 | while Present (Comp) loop | |
3303 | exit when Chars (Comp) = Chars (Id); | |
3304 | Next_Component (Comp); | |
3305 | end loop; | |
3306 | ||
3307 | -- Find the variant, if any, whose component list includes the | |
3308 | -- component declaration. | |
3309 | ||
3310 | Comp_Variant := Parent (Parent (List_Containing (Parent (Comp)))); | |
3311 | if Nkind (Comp_Variant) = N_Variant then | |
3312 | if No (Variant) then | |
3313 | Variant := Comp_Variant; | |
3314 | Comp_Ref := Comp; | |
3315 | ||
3316 | elsif Variant /= Comp_Variant then | |
9eb8d5b4 | 3317 | declare |
8d9a1ba7 PMR |
3318 | D1 : constant Integer := Variant_Depth (Variant); |
3319 | D2 : constant Integer := Variant_Depth (Comp_Variant); | |
9eb8d5b4 AC |
3320 | |
3321 | begin | |
8d9a1ba7 PMR |
3322 | if D1 = D2 |
3323 | or else | |
d63199d8 | 3324 | (D1 > D2 and then not Nested_In (Variant, Comp_Variant)) |
8d9a1ba7 | 3325 | or else |
d63199d8 | 3326 | (D2 > D1 and then not Nested_In (Comp_Variant, Variant)) |
8d9a1ba7 | 3327 | then |
5612989e | 3328 | pragma Assert (Present (Comp_Ref)); |
8d9a1ba7 PMR |
3329 | Error_Msg_Node_2 := Comp_Ref; |
3330 | Error_Msg_NE | |
3331 | ("& and & appear in different variants", Id, Comp); | |
3332 | ||
3333 | -- Otherwise retain the deeper variant for subsequent tests | |
9eb8d5b4 | 3334 | |
8d9a1ba7 PMR |
3335 | elsif D2 > D1 then |
3336 | Variant := Comp_Variant; | |
3337 | end if; | |
9eb8d5b4 | 3338 | end; |
8d9a1ba7 PMR |
3339 | end if; |
3340 | end if; | |
3341 | end Check_Variant; | |
9eb8d5b4 | 3342 | |
484d58c5 PT |
3343 | ------------------- |
3344 | -- Get_Component -- | |
3345 | ------------------- | |
d63199d8 | 3346 | |
484d58c5 | 3347 | function Get_Component (Nam : Node_Id) return Entity_Id is |
d63199d8 PMR |
3348 | Comp : Entity_Id; |
3349 | ||
3350 | begin | |
3351 | Comp := First_Entity (Typ); | |
3352 | while Present (Comp) loop | |
3353 | if Chars (Comp) = Chars (Nam) then | |
3354 | if Ekind (Comp) = E_Discriminant then | |
3355 | Error_Msg_N ("delta cannot apply to discriminant", Nam); | |
3356 | end if; | |
3357 | ||
484d58c5 | 3358 | return Comp; |
d63199d8 PMR |
3359 | end if; |
3360 | ||
99859ea7 | 3361 | Next_Entity (Comp); |
d63199d8 PMR |
3362 | end loop; |
3363 | ||
3364 | Error_Msg_NE ("type& has no component with this name", Nam, Typ); | |
484d58c5 PT |
3365 | return Empty; |
3366 | end Get_Component; | |
d63199d8 | 3367 | |
8d9a1ba7 PMR |
3368 | --------------- |
3369 | -- Nested_In -- | |
3370 | --------------- | |
9eb8d5b4 | 3371 | |
8d9a1ba7 PMR |
3372 | function Nested_In (V1, V2 : Node_Id) return Boolean is |
3373 | Par : Node_Id; | |
d63199d8 | 3374 | |
8d9a1ba7 PMR |
3375 | begin |
3376 | Par := Parent (V1); | |
3377 | while Nkind (Par) /= N_Full_Type_Declaration loop | |
3378 | if Par = V2 then | |
3379 | return True; | |
3380 | end if; | |
d63199d8 | 3381 | |
8d9a1ba7 PMR |
3382 | Par := Parent (Par); |
3383 | end loop; | |
9eb8d5b4 | 3384 | |
8d9a1ba7 PMR |
3385 | return False; |
3386 | end Nested_In; | |
3387 | ||
3388 | ------------------- | |
3389 | -- Variant_Depth -- | |
3390 | ------------------- | |
9eb8d5b4 | 3391 | |
8d9a1ba7 PMR |
3392 | function Variant_Depth (N : Node_Id) return Integer is |
3393 | Depth : Integer; | |
3394 | Par : Node_Id; | |
d63199d8 | 3395 | |
8d9a1ba7 PMR |
3396 | begin |
3397 | Depth := 0; | |
3398 | Par := Parent (N); | |
3399 | while Nkind (Par) /= N_Full_Type_Declaration loop | |
3400 | Depth := Depth + 1; | |
d63199d8 | 3401 | Par := Parent (Par); |
8d9a1ba7 PMR |
3402 | end loop; |
3403 | ||
3404 | return Depth; | |
3405 | end Variant_Depth; | |
3406 | ||
d63199d8 | 3407 | -- Local variables |
9eb8d5b4 | 3408 | |
d63199d8 | 3409 | Deltas : constant List_Id := Component_Associations (N); |
9eb8d5b4 | 3410 | |
d63199d8 PMR |
3411 | Assoc : Node_Id; |
3412 | Choice : Node_Id; | |
484d58c5 | 3413 | Comp : Entity_Id; |
5612989e | 3414 | Comp_Type : Entity_Id := Empty; -- init to avoid warning |
9eb8d5b4 | 3415 | |
8d9a1ba7 PMR |
3416 | -- Start of processing for Resolve_Delta_Record_Aggregate |
3417 | ||
3418 | begin | |
3ec54569 PMR |
3419 | Variant := Empty; |
3420 | ||
8d9a1ba7 | 3421 | Assoc := First (Deltas); |
8d9a1ba7 PMR |
3422 | while Present (Assoc) loop |
3423 | Choice := First (Choice_List (Assoc)); | |
3424 | while Present (Choice) loop | |
484d58c5 | 3425 | Comp := Get_Component (Choice); |
d63199d8 | 3426 | |
484d58c5 | 3427 | if Present (Comp) then |
8d9a1ba7 | 3428 | Check_Variant (Choice); |
484d58c5 PT |
3429 | |
3430 | Comp_Type := Etype (Comp); | |
3431 | ||
3432 | -- Decorate the component reference by setting its entity and | |
3433 | -- type, as otherwise backends like GNATprove would have to | |
3434 | -- rediscover this information by themselves. | |
3435 | ||
3436 | Set_Entity (Choice, Comp); | |
3437 | Set_Etype (Choice, Comp_Type); | |
3438 | else | |
3439 | Comp_Type := Any_Type; | |
8d9a1ba7 PMR |
3440 | end if; |
3441 | ||
3442 | Next (Choice); | |
9eb8d5b4 | 3443 | end loop; |
9eb8d5b4 | 3444 | |
5612989e | 3445 | pragma Assert (Present (Comp_Type)); |
8d9a1ba7 PMR |
3446 | Analyze_And_Resolve (Expression (Assoc), Comp_Type); |
3447 | Next (Assoc); | |
3448 | end loop; | |
3449 | end Resolve_Delta_Record_Aggregate; | |
9eb8d5b4 | 3450 | |
996ae0b0 RK |
3451 | --------------------------------- |
3452 | -- Resolve_Extension_Aggregate -- | |
3453 | --------------------------------- | |
3454 | ||
3455 | -- There are two cases to consider: | |
3456 | ||
ebd34478 AC |
3457 | -- a) If the ancestor part is a type mark, the components needed are the |
3458 | -- difference between the components of the expected type and the | |
996ae0b0 RK |
3459 | -- components of the given type mark. |
3460 | ||
ebd34478 | 3461 | -- b) If the ancestor part is an expression, it must be unambiguous, and |
12f0c50c | 3462 | -- once we have its type we can also compute the needed components as in |
ebd34478 AC |
3463 | -- the previous case. In both cases, if the ancestor type is not the |
3464 | -- immediate ancestor, we have to build this ancestor recursively. | |
996ae0b0 | 3465 | |
12f0c50c | 3466 | -- In both cases, discriminants of the ancestor type do not play a role in |
ebd34478 AC |
3467 | -- the resolution of the needed components, because inherited discriminants |
3468 | -- cannot be used in a type extension. As a result we can compute | |
3469 | -- independently the list of components of the ancestor type and of the | |
3470 | -- expected type. | |
996ae0b0 RK |
3471 | |
3472 | procedure Resolve_Extension_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b AC |
3473 | A : constant Node_Id := Ancestor_Part (N); |
3474 | A_Type : Entity_Id; | |
3475 | I : Interp_Index; | |
3476 | It : Interp; | |
996ae0b0 | 3477 | |
ca44152f ES |
3478 | function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean; |
3479 | -- If the type is limited, verify that the ancestor part is a legal | |
ebd34478 AC |
3480 | -- expression (aggregate or function call, including 'Input)) that does |
3481 | -- not require a copy, as specified in 7.5(2). | |
ca44152f | 3482 | |
996ae0b0 RK |
3483 | function Valid_Ancestor_Type return Boolean; |
3484 | -- Verify that the type of the ancestor part is a non-private ancestor | |
1543e3ab | 3485 | -- of the expected type, which must be a type extension. |
996ae0b0 | 3486 | |
5168a9b3 PMR |
3487 | procedure Transform_BIP_Assignment (Typ : Entity_Id); |
3488 | -- For an extension aggregate whose ancestor part is a build-in-place | |
3489 | -- call returning a nonlimited type, this is used to transform the | |
3490 | -- assignment to the ancestor part to use a temp. | |
3491 | ||
ca44152f ES |
3492 | ---------------------------- |
3493 | -- Valid_Limited_Ancestor -- | |
3494 | ---------------------------- | |
3495 | ||
3496 | function Valid_Limited_Ancestor (Anc : Node_Id) return Boolean is | |
3497 | begin | |
d8d7e809 AC |
3498 | if Is_Entity_Name (Anc) and then Is_Type (Entity (Anc)) then |
3499 | return True; | |
3500 | ||
3501 | -- The ancestor must be a call or an aggregate, but a call may | |
3502 | -- have been expanded into a temporary, so check original node. | |
3503 | ||
4a08c95c AC |
3504 | elsif Nkind (Anc) in N_Aggregate |
3505 | | N_Extension_Aggregate | |
3506 | | N_Function_Call | |
ca44152f ES |
3507 | then |
3508 | return True; | |
3509 | ||
d8d7e809 | 3510 | elsif Nkind (Original_Node (Anc)) = N_Function_Call then |
ca44152f ES |
3511 | return True; |
3512 | ||
3513 | elsif Nkind (Anc) = N_Attribute_Reference | |
3514 | and then Attribute_Name (Anc) = Name_Input | |
3515 | then | |
3516 | return True; | |
3517 | ||
ebd34478 | 3518 | elsif Nkind (Anc) = N_Qualified_Expression then |
ca44152f ES |
3519 | return Valid_Limited_Ancestor (Expression (Anc)); |
3520 | ||
32794080 JM |
3521 | elsif Nkind (Anc) = N_Raise_Expression then |
3522 | return True; | |
3523 | ||
ca44152f ES |
3524 | else |
3525 | return False; | |
3526 | end if; | |
3527 | end Valid_Limited_Ancestor; | |
3528 | ||
fbf5a39b AC |
3529 | ------------------------- |
3530 | -- Valid_Ancestor_Type -- | |
3531 | ------------------------- | |
3532 | ||
996ae0b0 RK |
3533 | function Valid_Ancestor_Type return Boolean is |
3534 | Imm_Type : Entity_Id; | |
3535 | ||
3536 | begin | |
3537 | Imm_Type := Base_Type (Typ); | |
2af92e28 ES |
3538 | while Is_Derived_Type (Imm_Type) loop |
3539 | if Etype (Imm_Type) = Base_Type (A_Type) then | |
3540 | return True; | |
3541 | ||
21d7ef70 | 3542 | -- The base type of the parent type may appear as a private |
ebd34478 AC |
3543 | -- extension if it is declared as such in a parent unit of the |
3544 | -- current one. For consistency of the subsequent analysis use | |
3545 | -- the partial view for the ancestor part. | |
2af92e28 ES |
3546 | |
3547 | elsif Is_Private_Type (Etype (Imm_Type)) | |
3548 | and then Present (Full_View (Etype (Imm_Type))) | |
3549 | and then Base_Type (A_Type) = Full_View (Etype (Imm_Type)) | |
3550 | then | |
3551 | A_Type := Etype (Imm_Type); | |
3552 | return True; | |
4519314c AC |
3553 | |
3554 | -- The parent type may be a private extension. The aggregate is | |
3555 | -- legal if the type of the aggregate is an extension of it that | |
3556 | -- is not a private extension. | |
3557 | ||
3558 | elsif Is_Private_Type (A_Type) | |
3559 | and then not Is_Private_Type (Imm_Type) | |
3560 | and then Present (Full_View (A_Type)) | |
3561 | and then Base_Type (Full_View (A_Type)) = Etype (Imm_Type) | |
3562 | then | |
3563 | return True; | |
3564 | ||
32794080 JM |
3565 | -- The parent type may be a raise expression (which is legal in |
3566 | -- any expression context). | |
3567 | ||
3568 | elsif A_Type = Raise_Type then | |
3569 | A_Type := Etype (Imm_Type); | |
3570 | return True; | |
3571 | ||
2af92e28 ES |
3572 | else |
3573 | Imm_Type := Etype (Base_Type (Imm_Type)); | |
3574 | end if; | |
996ae0b0 RK |
3575 | end loop; |
3576 | ||
6d2a1120 | 3577 | -- If previous loop did not find a proper ancestor, report error |
2af92e28 ES |
3578 | |
3579 | Error_Msg_NE ("expect ancestor type of &", A, Typ); | |
3580 | return False; | |
996ae0b0 RK |
3581 | end Valid_Ancestor_Type; |
3582 | ||
3fc40cd7 PMR |
3583 | ------------------------------ |
3584 | -- Transform_BIP_Assignment -- | |
3585 | ------------------------------ | |
3586 | ||
5168a9b3 | 3587 | procedure Transform_BIP_Assignment (Typ : Entity_Id) is |
3fc40cd7 PMR |
3588 | Loc : constant Source_Ptr := Sloc (N); |
3589 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'Y', A); | |
3590 | Obj_Decl : constant Node_Id := | |
3591 | Make_Object_Declaration (Loc, | |
3592 | Defining_Identifier => Def_Id, | |
3593 | Constant_Present => True, | |
3594 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
3595 | Expression => A, | |
3596 | Has_Init_Expression => True); | |
5168a9b3 PMR |
3597 | begin |
3598 | Set_Etype (Def_Id, Typ); | |
3599 | Set_Ancestor_Part (N, New_Occurrence_Of (Def_Id, Loc)); | |
3600 | Insert_Action (N, Obj_Decl); | |
3601 | end Transform_BIP_Assignment; | |
3602 | ||
996ae0b0 RK |
3603 | -- Start of processing for Resolve_Extension_Aggregate |
3604 | ||
3605 | begin | |
ebd34478 AC |
3606 | -- Analyze the ancestor part and account for the case where it is a |
3607 | -- parameterless function call. | |
70b70ce8 | 3608 | |
996ae0b0 | 3609 | Analyze (A); |
70b70ce8 | 3610 | Check_Parameterless_Call (A); |
996ae0b0 | 3611 | |
58009744 | 3612 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then |
87729e5a | 3613 | |
5c6430d6 GD |
3614 | -- AI05-0115: If the ancestor part is a subtype mark, the ancestor |
3615 | -- must not have unknown discriminants. To catch cases where the | |
3616 | -- aggregate occurs at a place where the full view of the ancestor | |
3617 | -- type is visible and doesn't have unknown discriminants, but the | |
3618 | -- aggregate type was derived from a partial view that has unknown | |
3619 | -- discriminants, we check whether the aggregate type has unknown | |
3620 | -- discriminants (unknown discriminants were inherited), along | |
3621 | -- with checking that the partial view of the ancestor has unknown | |
3622 | -- discriminants. (It might be sufficient to replace the entire | |
3623 | -- condition with Has_Unknown_Discriminants (Typ), but that might | |
3624 | -- miss some cases, not clear, and causes error changes in some tests | |
3625 | -- such as class-wide cases, that aren't clearly improvements. ???) | |
3626 | ||
3627 | if Has_Unknown_Discriminants (Entity (A)) | |
3628 | or else (Has_Unknown_Discriminants (Typ) | |
3629 | and then Partial_View_Has_Unknown_Discr (Entity (A))) | |
3630 | then | |
87729e5a AC |
3631 | Error_Msg_NE |
3632 | ("aggregate not available for type& whose ancestor " | |
3633 | & "has unknown discriminants", N, Typ); | |
3634 | end if; | |
9f90d123 AC |
3635 | end if; |
3636 | ||
996ae0b0 RK |
3637 | if not Is_Tagged_Type (Typ) then |
3638 | Error_Msg_N ("type of extension aggregate must be tagged", N); | |
3639 | return; | |
3640 | ||
19f0526a AC |
3641 | elsif Is_Limited_Type (Typ) then |
3642 | ||
0ab80019 | 3643 | -- Ada 2005 (AI-287): Limited aggregates are allowed |
19f0526a | 3644 | |
0791fbe9 | 3645 | if Ada_Version < Ada_2005 then |
19f0526a AC |
3646 | Error_Msg_N ("aggregate type cannot be limited", N); |
3647 | Explain_Limited_Type (Typ, N); | |
3648 | return; | |
ca44152f ES |
3649 | |
3650 | elsif Valid_Limited_Ancestor (A) then | |
3651 | null; | |
3652 | ||
3653 | else | |
3654 | Error_Msg_N | |
3655 | ("limited ancestor part must be aggregate or function call", A); | |
19f0526a | 3656 | end if; |
996ae0b0 RK |
3657 | |
3658 | elsif Is_Class_Wide_Type (Typ) then | |
3659 | Error_Msg_N ("aggregate cannot be of a class-wide type", N); | |
3660 | return; | |
3661 | end if; | |
3662 | ||
58009744 | 3663 | if Is_Entity_Name (A) and then Is_Type (Entity (A)) then |
fbf5a39b | 3664 | A_Type := Get_Full_View (Entity (A)); |
996ae0b0 RK |
3665 | |
3666 | if Valid_Ancestor_Type then | |
3667 | Set_Entity (A, A_Type); | |
3668 | Set_Etype (A, A_Type); | |
3669 | ||
3670 | Validate_Ancestor_Part (N); | |
3671 | Resolve_Record_Aggregate (N, Typ); | |
3672 | end if; | |
3673 | ||
3674 | elsif Nkind (A) /= N_Aggregate then | |
3675 | if Is_Overloaded (A) then | |
3676 | A_Type := Any_Type; | |
996ae0b0 | 3677 | |
7f9747c6 | 3678 | Get_First_Interp (A, I, It); |
996ae0b0 | 3679 | while Present (It.Typ) loop |
58009744 | 3680 | |
5168a9b3 | 3681 | -- Consider limited interpretations if Ada 2005 or higher |
70b70ce8 | 3682 | |
996ae0b0 | 3683 | if Is_Tagged_Type (It.Typ) |
0791fbe9 | 3684 | and then (Ada_Version >= Ada_2005 |
70b70ce8 | 3685 | or else not Is_Limited_Type (It.Typ)) |
996ae0b0 RK |
3686 | then |
3687 | if A_Type /= Any_Type then | |
3688 | Error_Msg_N ("cannot resolve expression", A); | |
3689 | return; | |
3690 | else | |
3691 | A_Type := It.Typ; | |
3692 | end if; | |
3693 | end if; | |
3694 | ||
3695 | Get_Next_Interp (I, It); | |
3696 | end loop; | |
3697 | ||
3698 | if A_Type = Any_Type then | |
0791fbe9 | 3699 | if Ada_Version >= Ada_2005 then |
58009744 AC |
3700 | Error_Msg_N |
3701 | ("ancestor part must be of a tagged type", A); | |
70b70ce8 AC |
3702 | else |
3703 | Error_Msg_N | |
3704 | ("ancestor part must be of a nonlimited tagged type", A); | |
3705 | end if; | |
3706 | ||
996ae0b0 RK |
3707 | return; |
3708 | end if; | |
3709 | ||
3710 | else | |
3711 | A_Type := Etype (A); | |
3712 | end if; | |
3713 | ||
3714 | if Valid_Ancestor_Type then | |
3715 | Resolve (A, A_Type); | |
fbf5a39b | 3716 | Check_Unset_Reference (A); |
996ae0b0 | 3717 | Check_Non_Static_Context (A); |
fbf5a39b | 3718 | |
1646c947 GD |
3719 | -- The aggregate is illegal if the ancestor expression is a call |
3720 | -- to a function with a limited unconstrained result, unless the | |
3721 | -- type of the aggregate is a null extension. This restriction | |
3722 | -- was added in AI05-67 to simplify implementation. | |
3723 | ||
3724 | if Nkind (A) = N_Function_Call | |
3725 | and then Is_Limited_Type (A_Type) | |
3726 | and then not Is_Null_Extension (Typ) | |
3727 | and then not Is_Constrained (A_Type) | |
3728 | then | |
3729 | Error_Msg_N | |
3730 | ("type of limited ancestor part must be constrained", A); | |
3731 | ||
cefce34c JM |
3732 | -- Reject the use of CPP constructors that leave objects partially |
3733 | -- initialized. For example: | |
3734 | ||
3735 | -- type CPP_Root is tagged limited record ... | |
3736 | -- pragma Import (CPP, CPP_Root); | |
3737 | ||
3738 | -- type CPP_DT is new CPP_Root and Iface ... | |
3739 | -- pragma Import (CPP, CPP_DT); | |
3740 | ||
3741 | -- type Ada_DT is new CPP_DT with ... | |
3742 | ||
3743 | -- Obj : Ada_DT := Ada_DT'(New_CPP_Root with others => <>); | |
3744 | ||
3745 | -- Using the constructor of CPP_Root the slots of the dispatch | |
3746 | -- table of CPP_DT cannot be set, and the secondary tag of | |
3747 | -- CPP_DT is unknown. | |
3748 | ||
3749 | elsif Nkind (A) = N_Function_Call | |
3750 | and then Is_CPP_Constructor_Call (A) | |
3751 | and then Enclosing_CPP_Parent (Typ) /= A_Type | |
3752 | then | |
3753 | Error_Msg_NE | |
324ac540 | 3754 | ("??must use 'C'P'P constructor for type &", A, |
cefce34c JM |
3755 | Enclosing_CPP_Parent (Typ)); |
3756 | ||
3757 | -- The following call is not needed if the previous warning | |
3758 | -- is promoted to an error. | |
3759 | ||
3760 | Resolve_Record_Aggregate (N, Typ); | |
3761 | ||
1646c947 | 3762 | elsif Is_Class_Wide_Type (Etype (A)) |
fbf5a39b AC |
3763 | and then Nkind (Original_Node (A)) = N_Function_Call |
3764 | then | |
3765 | -- If the ancestor part is a dispatching call, it appears | |
ebd34478 AC |
3766 | -- statically to be a legal ancestor, but it yields any member |
3767 | -- of the class, and it is not possible to determine whether | |
3768 | -- it is an ancestor of the extension aggregate (much less | |
3769 | -- which ancestor). It is not possible to determine the | |
3770 | -- components of the extension part. | |
fbf5a39b | 3771 | |
ebd34478 AC |
3772 | -- This check implements AI-306, which in fact was motivated by |
3773 | -- an AdaCore query to the ARG after this test was added. | |
82c80734 | 3774 | |
fbf5a39b AC |
3775 | Error_Msg_N ("ancestor part must be statically tagged", A); |
3776 | else | |
5168a9b3 PMR |
3777 | -- We are using the build-in-place protocol, but we can't build |
3778 | -- in place, because we need to call the function before | |
3779 | -- allocating the aggregate. Could do better for null | |
3780 | -- extensions, and maybe for nondiscriminated types. | |
3781 | -- This is wrong for limited, but those were wrong already. | |
3782 | ||
3783 | if not Is_Limited_View (A_Type) | |
3784 | and then Is_Build_In_Place_Function_Call (A) | |
3785 | then | |
3786 | Transform_BIP_Assignment (A_Type); | |
3787 | end if; | |
3788 | ||
fbf5a39b AC |
3789 | Resolve_Record_Aggregate (N, Typ); |
3790 | end if; | |
996ae0b0 RK |
3791 | end if; |
3792 | ||
3793 | else | |
937e9676 | 3794 | Error_Msg_N ("no unique type for this aggregate", A); |
996ae0b0 | 3795 | end if; |
d3820795 | 3796 | |
22e89283 | 3797 | Check_Function_Writable_Actuals (N); |
996ae0b0 RK |
3798 | end Resolve_Extension_Aggregate; |
3799 | ||
3800 | ------------------------------ | |
3801 | -- Resolve_Record_Aggregate -- | |
3802 | ------------------------------ | |
3803 | ||
3804 | procedure Resolve_Record_Aggregate (N : Node_Id; Typ : Entity_Id) is | |
fbf5a39b | 3805 | New_Assoc_List : constant List_Id := New_List; |
996ae0b0 | 3806 | -- New_Assoc_List is the newly built list of N_Component_Association |
937e9676 | 3807 | -- nodes. |
996ae0b0 RK |
3808 | |
3809 | Others_Etype : Entity_Id := Empty; | |
3810 | -- This variable is used to save the Etype of the last record component | |
3811 | -- that takes its value from the others choice. Its purpose is: | |
3812 | -- | |
3813 | -- (a) make sure the others choice is useful | |
3814 | -- | |
3815 | -- (b) make sure the type of all the components whose value is | |
3816 | -- subsumed by the others choice are the same. | |
3817 | -- | |
ebd34478 | 3818 | -- This variable is updated as a side effect of function Get_Value. |
996ae0b0 | 3819 | |
f7937111 GD |
3820 | Box_Node : Node_Id := Empty; |
3821 | Is_Box_Present : Boolean := False; | |
3822 | Is_Box_Init_By_Default : Boolean := False; | |
3823 | Others_Box : Natural := 0; | |
0ab80019 | 3824 | -- Ada 2005 (AI-287): Variables used in case of default initialization |
9b96e234 | 3825 | -- to provide a functionality similar to Others_Etype. Box_Present |
19f0526a | 3826 | -- indicates that the component takes its default initialization; |
ec3c7387 AC |
3827 | -- Others_Box counts the number of components of the current aggregate |
3828 | -- (which may be a sub-aggregate of a larger one) that are default- | |
3829 | -- initialized. A value of One indicates that an others_box is present. | |
3830 | -- Any larger value indicates that the others_box is not redundant. | |
937e9676 AC |
3831 | -- These variables, similar to Others_Etype, are also updated as a side |
3832 | -- effect of function Get_Value. Box_Node is used to place a warning on | |
3833 | -- a redundant others_box. | |
65356e64 AC |
3834 | |
3835 | procedure Add_Association | |
9b96e234 JM |
3836 | (Component : Entity_Id; |
3837 | Expr : Node_Id; | |
107b023c | 3838 | Assoc_List : List_Id; |
9b96e234 | 3839 | Is_Box_Present : Boolean := False); |
ebd34478 AC |
3840 | -- Builds a new N_Component_Association node which associates Component |
3841 | -- to expression Expr and adds it to the association list being built, | |
3842 | -- either New_Assoc_List, or the association being built for an inner | |
3843 | -- aggregate. | |
996ae0b0 | 3844 | |
937e9676 AC |
3845 | procedure Add_Discriminant_Values |
3846 | (New_Aggr : Node_Id; | |
3847 | Assoc_List : List_Id); | |
3848 | -- The constraint to a component may be given by a discriminant of the | |
3849 | -- enclosing type, in which case we have to retrieve its value, which is | |
3850 | -- part of the enclosing aggregate. Assoc_List provides the discriminant | |
3851 | -- associations of the current type or of some enclosing record. | |
3852 | ||
3853 | function Discriminant_Present (Input_Discr : Entity_Id) return Boolean; | |
996ae0b0 | 3854 | -- If aggregate N is a regular aggregate this routine will return True. |
937e9676 AC |
3855 | -- Otherwise, if N is an extension aggregate, then Input_Discr denotes |
3856 | -- a discriminant whose value may already have been specified by N's | |
3857 | -- ancestor part. This routine checks whether this is indeed the case | |
3858 | -- and if so returns False, signaling that no value for Input_Discr | |
3859 | -- should appear in N's aggregate part. Also, in this case, the routine | |
3860 | -- appends to New_Assoc_List the discriminant value specified in the | |
3861 | -- ancestor part. | |
2383acbd | 3862 | -- |
f104fca1 | 3863 | -- If the aggregate is in a context with expansion delayed, it will be |
22cb89b5 AC |
3864 | -- reanalyzed. The inherited discriminant values must not be reinserted |
3865 | -- in the component list to prevent spurious errors, but they must be | |
f104fca1 AC |
3866 | -- present on first analysis to build the proper subtype indications. |
3867 | -- The flag Inherited_Discriminant is used to prevent the re-insertion. | |
996ae0b0 | 3868 | |
937e9676 AC |
3869 | function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id; |
3870 | -- AI05-0115: Find earlier ancestor in the derivation chain that is | |
3871 | -- derived from private view Typ. Whether the aggregate is legal depends | |
3872 | -- on the current visibility of the type as well as that of the parent | |
3873 | -- of the ancestor. | |
3874 | ||
996ae0b0 | 3875 | function Get_Value |
0bfa32f9 | 3876 | (Compon : Entity_Id; |
996ae0b0 | 3877 | From : List_Id; |
937e9676 | 3878 | Consider_Others_Choice : Boolean := False) return Node_Id; |
4519314c AC |
3879 | -- Given a record component stored in parameter Compon, this function |
3880 | -- returns its value as it appears in the list From, which is a list | |
3881 | -- of N_Component_Association nodes. | |
2383acbd | 3882 | -- |
ebd34478 AC |
3883 | -- If no component association has a choice for the searched component, |
3884 | -- the value provided by the others choice is returned, if there is one, | |
3885 | -- and Consider_Others_Choice is set to true. Otherwise Empty is | |
3886 | -- returned. If there is more than one component association giving a | |
3887 | -- value for the searched record component, an error message is emitted | |
3888 | -- and the first found value is returned. | |
996ae0b0 RK |
3889 | -- |
3890 | -- If Consider_Others_Choice is set and the returned expression comes | |
3891 | -- from the others choice, then Others_Etype is set as a side effect. | |
ebd34478 AC |
3892 | -- An error message is emitted if the components taking their value from |
3893 | -- the others choice do not have same type. | |
996ae0b0 | 3894 | |
937e9676 AC |
3895 | procedure Propagate_Discriminants |
3896 | (Aggr : Node_Id; | |
3897 | Assoc_List : List_Id); | |
3898 | -- Nested components may themselves be discriminated types constrained | |
3899 | -- by outer discriminants, whose values must be captured before the | |
3900 | -- aggregate is expanded into assignments. | |
3901 | ||
3902 | procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id); | |
996ae0b0 | 3903 | -- Analyzes and resolves expression Expr against the Etype of the |
638e383e | 3904 | -- Component. This routine also applies all appropriate checks to Expr. |
996ae0b0 RK |
3905 | -- It finally saves a Expr in the newly created association list that |
3906 | -- will be attached to the final record aggregate. Note that if the | |
3907 | -- Parent pointer of Expr is not set then Expr was produced with a | |
fbf5a39b | 3908 | -- New_Copy_Tree or some such. |
996ae0b0 | 3909 | |
333e4f86 AC |
3910 | procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id); |
3911 | -- Rewrite a range node Rge when its bounds refer to non-stored | |
3912 | -- discriminants from Root_Type, to replace them with the stored | |
3913 | -- discriminant values. This is required in GNATprove mode, and is | |
3914 | -- adopted in all modes to avoid special-casing GNATprove mode. | |
3915 | ||
996ae0b0 RK |
3916 | --------------------- |
3917 | -- Add_Association -- | |
3918 | --------------------- | |
3919 | ||
65356e64 | 3920 | procedure Add_Association |
9b96e234 JM |
3921 | (Component : Entity_Id; |
3922 | Expr : Node_Id; | |
107b023c | 3923 | Assoc_List : List_Id; |
9b96e234 | 3924 | Is_Box_Present : Boolean := False) |
65356e64 | 3925 | is |
fbf5a39b | 3926 | Choice_List : constant List_Id := New_List; |
937e9676 | 3927 | Loc : Source_Ptr; |
996ae0b0 RK |
3928 | |
3929 | begin | |
937e9676 AC |
3930 | -- If this is a box association the expression is missing, so use the |
3931 | -- Sloc of the aggregate itself for the new association. | |
f5afb270 | 3932 | |
0c25b783 PT |
3933 | pragma Assert (Present (Expr) xor Is_Box_Present); |
3934 | ||
f5afb270 AC |
3935 | if Present (Expr) then |
3936 | Loc := Sloc (Expr); | |
3937 | else | |
3938 | Loc := Sloc (N); | |
3939 | end if; | |
3940 | ||
937e9676 AC |
3941 | Append_To (Choice_List, New_Occurrence_Of (Component, Loc)); |
3942 | ||
3943 | Append_To (Assoc_List, | |
f5afb270 | 3944 | Make_Component_Association (Loc, |
65356e64 AC |
3945 | Choices => Choice_List, |
3946 | Expression => Expr, | |
937e9676 | 3947 | Box_Present => Is_Box_Present)); |
f7937111 GD |
3948 | |
3949 | -- If this association has a box for a component that is initialized | |
3950 | -- by default, then set flag on the new association to indicate that | |
3951 | -- the original association was for such a box-initialized component. | |
3952 | ||
097826df | 3953 | if Is_Box_Init_By_Default then |
f7937111 GD |
3954 | Set_Was_Default_Init_Box_Association (Last (Assoc_List)); |
3955 | end if; | |
996ae0b0 RK |
3956 | end Add_Association; |
3957 | ||
937e9676 AC |
3958 | ----------------------------- |
3959 | -- Add_Discriminant_Values -- | |
3960 | ----------------------------- | |
3961 | ||
3962 | procedure Add_Discriminant_Values | |
3963 | (New_Aggr : Node_Id; | |
3964 | Assoc_List : List_Id) | |
3965 | is | |
3966 | Assoc : Node_Id; | |
3967 | Discr : Entity_Id; | |
3968 | Discr_Elmt : Elmt_Id; | |
3969 | Discr_Val : Node_Id; | |
3970 | Val : Entity_Id; | |
3971 | ||
3972 | begin | |
3973 | Discr := First_Discriminant (Etype (New_Aggr)); | |
3974 | Discr_Elmt := First_Elmt (Discriminant_Constraint (Etype (New_Aggr))); | |
3975 | while Present (Discr_Elmt) loop | |
3976 | Discr_Val := Node (Discr_Elmt); | |
3977 | ||
3978 | -- If the constraint is given by a discriminant then it is a | |
3979 | -- discriminant of an enclosing record, and its value has already | |
3980 | -- been placed in the association list. | |
996ae0b0 | 3981 | |
937e9676 AC |
3982 | if Is_Entity_Name (Discr_Val) |
3983 | and then Ekind (Entity (Discr_Val)) = E_Discriminant | |
3984 | then | |
3985 | Val := Entity (Discr_Val); | |
3986 | ||
3987 | Assoc := First (Assoc_List); | |
3988 | while Present (Assoc) loop | |
3989 | if Present (Entity (First (Choices (Assoc)))) | |
3990 | and then Entity (First (Choices (Assoc))) = Val | |
3991 | then | |
3992 | Discr_Val := Expression (Assoc); | |
3993 | exit; | |
3994 | end if; | |
3995 | ||
3996 | Next (Assoc); | |
3997 | end loop; | |
3998 | end if; | |
3999 | ||
4000 | Add_Association | |
4001 | (Discr, New_Copy_Tree (Discr_Val), | |
4002 | Component_Associations (New_Aggr)); | |
4003 | ||
4004 | -- If the discriminant constraint is a current instance, mark the | |
4005 | -- current aggregate so that the self-reference can be expanded | |
4006 | -- later. The constraint may refer to the subtype of aggregate, so | |
4007 | -- use base type for comparison. | |
4008 | ||
4009 | if Nkind (Discr_Val) = N_Attribute_Reference | |
4010 | and then Is_Entity_Name (Prefix (Discr_Val)) | |
4011 | and then Is_Type (Entity (Prefix (Discr_Val))) | |
4012 | and then Base_Type (Etype (N)) = Entity (Prefix (Discr_Val)) | |
4013 | then | |
4014 | Set_Has_Self_Reference (N); | |
4015 | end if; | |
4016 | ||
4017 | Next_Elmt (Discr_Elmt); | |
4018 | Next_Discriminant (Discr); | |
4019 | end loop; | |
4020 | end Add_Discriminant_Values; | |
4021 | ||
4022 | -------------------------- | |
4023 | -- Discriminant_Present -- | |
4024 | -------------------------- | |
4025 | ||
4026 | function Discriminant_Present (Input_Discr : Entity_Id) return Boolean is | |
fbf5a39b AC |
4027 | Regular_Aggr : constant Boolean := Nkind (N) /= N_Extension_Aggregate; |
4028 | ||
937e9676 AC |
4029 | Ancestor_Is_Subtyp : Boolean; |
4030 | ||
996ae0b0 RK |
4031 | Loc : Source_Ptr; |
4032 | ||
4033 | Ancestor : Node_Id; | |
937e9676 | 4034 | Ancestor_Typ : Entity_Id; |
f104fca1 | 4035 | Comp_Assoc : Node_Id; |
937e9676 | 4036 | Discr : Entity_Id; |
996ae0b0 | 4037 | Discr_Expr : Node_Id; |
937e9676 | 4038 | Discr_Val : Elmt_Id := No_Elmt; |
996ae0b0 | 4039 | Orig_Discr : Entity_Id; |
996ae0b0 RK |
4040 | |
4041 | begin | |
4042 | if Regular_Aggr then | |
4043 | return True; | |
4044 | end if; | |
4045 | ||
f104fca1 AC |
4046 | -- Check whether inherited discriminant values have already been |
4047 | -- inserted in the aggregate. This will be the case if we are | |
4048 | -- re-analyzing an aggregate whose expansion was delayed. | |
4049 | ||
4050 | if Present (Component_Associations (N)) then | |
4051 | Comp_Assoc := First (Component_Associations (N)); | |
4052 | while Present (Comp_Assoc) loop | |
4053 | if Inherited_Discriminant (Comp_Assoc) then | |
4054 | return True; | |
4055 | end if; | |
2383acbd | 4056 | |
f104fca1 AC |
4057 | Next (Comp_Assoc); |
4058 | end loop; | |
4059 | end if; | |
4060 | ||
996ae0b0 RK |
4061 | Ancestor := Ancestor_Part (N); |
4062 | Ancestor_Typ := Etype (Ancestor); | |
4063 | Loc := Sloc (Ancestor); | |
4064 | ||
5987e59c AC |
4065 | -- For a private type with unknown discriminants, use the underlying |
4066 | -- record view if it is available. | |
9013065b AC |
4067 | |
4068 | if Has_Unknown_Discriminants (Ancestor_Typ) | |
4069 | and then Present (Full_View (Ancestor_Typ)) | |
4070 | and then Present (Underlying_Record_View (Full_View (Ancestor_Typ))) | |
4071 | then | |
4072 | Ancestor_Typ := Underlying_Record_View (Full_View (Ancestor_Typ)); | |
4073 | end if; | |
4074 | ||
996ae0b0 RK |
4075 | Ancestor_Is_Subtyp := |
4076 | Is_Entity_Name (Ancestor) and then Is_Type (Entity (Ancestor)); | |
4077 | ||
4078 | -- If the ancestor part has no discriminants clearly N's aggregate | |
4079 | -- part must provide a value for Discr. | |
4080 | ||
4081 | if not Has_Discriminants (Ancestor_Typ) then | |
4082 | return True; | |
4083 | ||
4084 | -- If the ancestor part is an unconstrained subtype mark then the | |
4085 | -- Discr must be present in N's aggregate part. | |
4086 | ||
4087 | elsif Ancestor_Is_Subtyp | |
4088 | and then not Is_Constrained (Entity (Ancestor)) | |
4089 | then | |
4090 | return True; | |
4091 | end if; | |
4092 | ||
ec53a6da | 4093 | -- Now look to see if Discr was specified in the ancestor part |
996ae0b0 RK |
4094 | |
4095 | if Ancestor_Is_Subtyp then | |
937e9676 AC |
4096 | Discr_Val := |
4097 | First_Elmt (Discriminant_Constraint (Entity (Ancestor))); | |
996ae0b0 RK |
4098 | end if; |
4099 | ||
937e9676 | 4100 | Orig_Discr := Original_Record_Component (Input_Discr); |
ec53a6da | 4101 | |
937e9676 AC |
4102 | Discr := First_Discriminant (Ancestor_Typ); |
4103 | while Present (Discr) loop | |
ec53a6da | 4104 | |
ebd34478 | 4105 | -- If Ancestor has already specified Disc value then insert its |
ec53a6da | 4106 | -- value in the final aggregate. |
996ae0b0 | 4107 | |
937e9676 | 4108 | if Original_Record_Component (Discr) = Orig_Discr then |
996ae0b0 | 4109 | if Ancestor_Is_Subtyp then |
937e9676 | 4110 | Discr_Expr := New_Copy_Tree (Node (Discr_Val)); |
996ae0b0 RK |
4111 | else |
4112 | Discr_Expr := | |
4113 | Make_Selected_Component (Loc, | |
4114 | Prefix => Duplicate_Subexpr (Ancestor), | |
937e9676 | 4115 | Selector_Name => New_Occurrence_Of (Input_Discr, Loc)); |
996ae0b0 RK |
4116 | end if; |
4117 | ||
937e9676 | 4118 | Resolve_Aggr_Expr (Discr_Expr, Input_Discr); |
f104fca1 | 4119 | Set_Inherited_Discriminant (Last (New_Assoc_List)); |
996ae0b0 RK |
4120 | return False; |
4121 | end if; | |
4122 | ||
937e9676 | 4123 | Next_Discriminant (Discr); |
996ae0b0 RK |
4124 | |
4125 | if Ancestor_Is_Subtyp then | |
937e9676 | 4126 | Next_Elmt (Discr_Val); |
996ae0b0 RK |
4127 | end if; |
4128 | end loop; | |
4129 | ||
4130 | return True; | |
937e9676 AC |
4131 | end Discriminant_Present; |
4132 | ||
4133 | --------------------------- | |
4134 | -- Find_Private_Ancestor -- | |
4135 | --------------------------- | |
4136 | ||
4137 | function Find_Private_Ancestor (Typ : Entity_Id) return Entity_Id is | |
4138 | Par : Entity_Id; | |
4139 | ||
4140 | begin | |
4141 | Par := Typ; | |
4142 | loop | |
4143 | if Has_Private_Ancestor (Par) | |
4144 | and then not Has_Private_Ancestor (Etype (Base_Type (Par))) | |
4145 | then | |
4146 | return Par; | |
4147 | ||
4148 | elsif not Is_Derived_Type (Par) then | |
4149 | return Empty; | |
4150 | ||
4151 | else | |
4152 | Par := Etype (Base_Type (Par)); | |
4153 | end if; | |
4154 | end loop; | |
4155 | end Find_Private_Ancestor; | |
996ae0b0 RK |
4156 | |
4157 | --------------- | |
4158 | -- Get_Value -- | |
4159 | --------------- | |
4160 | ||
4161 | function Get_Value | |
0bfa32f9 | 4162 | (Compon : Entity_Id; |
996ae0b0 | 4163 | From : List_Id; |
937e9676 | 4164 | Consider_Others_Choice : Boolean := False) return Node_Id |
996ae0b0 | 4165 | is |
d10bb9d5 | 4166 | Typ : constant Entity_Id := Etype (Compon); |
996ae0b0 RK |
4167 | Assoc : Node_Id; |
4168 | Expr : Node_Id := Empty; | |
4169 | Selector_Name : Node_Id; | |
4170 | ||
4171 | begin | |
9b96e234 | 4172 | Is_Box_Present := False; |
f7937111 | 4173 | Is_Box_Init_By_Default := False; |
65356e64 | 4174 | |
58009744 | 4175 | if No (From) then |
996ae0b0 RK |
4176 | return Empty; |
4177 | end if; | |
4178 | ||
58009744 | 4179 | Assoc := First (From); |
996ae0b0 RK |
4180 | while Present (Assoc) loop |
4181 | Selector_Name := First (Choices (Assoc)); | |
4182 | while Present (Selector_Name) loop | |
4183 | if Nkind (Selector_Name) = N_Others_Choice then | |
4184 | if Consider_Others_Choice and then No (Expr) then | |
996ae0b0 RK |
4185 | |
4186 | -- We need to duplicate the expression for each | |
4187 | -- successive component covered by the others choice. | |
fbf5a39b AC |
4188 | -- This is redundant if the others_choice covers only |
4189 | -- one component (small optimization possible???), but | |
4190 | -- indispensable otherwise, because each one must be | |
92b751fd | 4191 | -- expanded individually to preserve side effects. |
996ae0b0 | 4192 | |
0ab80019 AC |
4193 | -- Ada 2005 (AI-287): In case of default initialization |
4194 | -- of components, we duplicate the corresponding default | |
88b32fc3 BD |
4195 | -- expression (from the record type declaration). The |
4196 | -- copy must carry the sloc of the association (not the | |
4197 | -- original expression) to prevent spurious elaboration | |
4198 | -- checks when the default includes function calls. | |
19f0526a | 4199 | |
65356e64 | 4200 | if Box_Present (Assoc) then |
ec3c7387 | 4201 | Others_Box := Others_Box + 1; |
9b96e234 | 4202 | Is_Box_Present := True; |
65356e64 AC |
4203 | |
4204 | if Expander_Active then | |
88b32fc3 | 4205 | return |
ba914484 | 4206 | New_Copy_Tree_And_Copy_Dimensions |
88b32fc3 BD |
4207 | (Expression (Parent (Compon)), |
4208 | New_Sloc => Sloc (Assoc)); | |
65356e64 AC |
4209 | else |
4210 | return Expression (Parent (Compon)); | |
4211 | end if; | |
65356e64 | 4212 | |
d05ef0ab | 4213 | else |
d10bb9d5 AC |
4214 | if Present (Others_Etype) |
4215 | and then Base_Type (Others_Etype) /= Base_Type (Typ) | |
65356e64 | 4216 | then |
a921e83c AC |
4217 | -- If the components are of an anonymous access |
4218 | -- type they are distinct, but this is legal in | |
4219 | -- Ada 2012 as long as designated types match. | |
4220 | ||
4221 | if (Ekind (Typ) = E_Anonymous_Access_Type | |
4222 | or else Ekind (Typ) = | |
4223 | E_Anonymous_Access_Subprogram_Type) | |
4224 | and then Designated_Type (Typ) = | |
4225 | Designated_Type (Others_Etype) | |
4226 | then | |
4227 | null; | |
4228 | else | |
4229 | Error_Msg_N | |
937e9676 AC |
4230 | ("components in OTHERS choice must have same " |
4231 | & "type", Selector_Name); | |
a921e83c | 4232 | end if; |
65356e64 AC |
4233 | end if; |
4234 | ||
d10bb9d5 | 4235 | Others_Etype := Typ; |
65356e64 | 4236 | |
937e9676 | 4237 | -- Copy the expression so that it is resolved |
a921e83c | 4238 | -- independently for each component, This is needed |
23a9215f | 4239 | -- for accessibility checks on components of anonymous |
a921e83c AC |
4240 | -- access types, even in compile_only mode. |
4241 | ||
4242 | if not Inside_A_Generic then | |
ba914484 VP |
4243 | return |
4244 | New_Copy_Tree_And_Copy_Dimensions | |
4245 | (Expression (Assoc)); | |
65356e64 AC |
4246 | else |
4247 | return Expression (Assoc); | |
4248 | end if; | |
996ae0b0 RK |
4249 | end if; |
4250 | end if; | |
4251 | ||
4252 | elsif Chars (Compon) = Chars (Selector_Name) then | |
4253 | if No (Expr) then | |
fbf5a39b | 4254 | |
0ab80019 | 4255 | -- Ada 2005 (AI-231) |
2820d220 | 4256 | |
0791fbe9 | 4257 | if Ada_Version >= Ada_2005 |
8133b9d1 | 4258 | and then Known_Null (Expression (Assoc)) |
2820d220 | 4259 | then |
82c80734 | 4260 | Check_Can_Never_Be_Null (Compon, Expression (Assoc)); |
2820d220 AC |
4261 | end if; |
4262 | ||
996ae0b0 RK |
4263 | -- We need to duplicate the expression when several |
4264 | -- components are grouped together with a "|" choice. | |
4265 | -- For instance "filed1 | filed2 => Expr" | |
4266 | ||
0ab80019 | 4267 | -- Ada 2005 (AI-287) |
2820d220 | 4268 | |
65356e64 | 4269 | if Box_Present (Assoc) then |
9b96e234 | 4270 | Is_Box_Present := True; |
65356e64 AC |
4271 | |
4272 | -- Duplicate the default expression of the component | |
c7ce71c2 ES |
4273 | -- from the record type declaration, so a new copy |
4274 | -- can be attached to the association. | |
65356e64 | 4275 | |
c7ce71c2 ES |
4276 | -- Note that we always copy the default expression, |
4277 | -- even when the association has a single choice, in | |
4278 | -- order to create a proper association for the | |
4279 | -- expanded aggregate. | |
4280 | ||
8097203f AC |
4281 | -- Component may have no default, in which case the |
4282 | -- expression is empty and the component is default- | |
4283 | -- initialized, but an association for the component | |
4284 | -- exists, and it is not covered by an others clause. | |
4285 | ||
d10bb9d5 AC |
4286 | -- Scalar and private types have no initialization |
4287 | -- procedure, so they remain uninitialized. If the | |
4288 | -- target of the aggregate is a constant this | |
4289 | -- deserves a warning. | |
4290 | ||
4291 | if No (Expression (Parent (Compon))) | |
4292 | and then not Has_Non_Null_Base_Init_Proc (Typ) | |
4293 | and then not Has_Aspect (Typ, Aspect_Default_Value) | |
4294 | and then not Is_Concurrent_Type (Typ) | |
4295 | and then Nkind (Parent (N)) = N_Object_Declaration | |
4296 | and then Constant_Present (Parent (N)) | |
4297 | then | |
4298 | Error_Msg_Node_2 := Typ; | |
4299 | Error_Msg_NE | |
4300 | ("component&? of type& is uninitialized", | |
4301 | Assoc, Selector_Name); | |
4302 | ||
4303 | -- An additional reminder if the component type | |
4304 | -- is a generic formal. | |
4305 | ||
4306 | if Is_Generic_Type (Base_Type (Typ)) then | |
4307 | Error_Msg_NE | |
58009744 AC |
4308 | ("\instance should provide actual type with " |
4309 | & "initialization for&", Assoc, Typ); | |
d10bb9d5 AC |
4310 | end if; |
4311 | end if; | |
4312 | ||
ba914484 VP |
4313 | return |
4314 | New_Copy_Tree_And_Copy_Dimensions | |
4315 | (Expression (Parent (Compon))); | |
8097203f | 4316 | |
d05ef0ab | 4317 | else |
65356e64 | 4318 | if Present (Next (Selector_Name)) then |
d10bb9d5 AC |
4319 | Expr := New_Copy_Tree_And_Copy_Dimensions |
4320 | (Expression (Assoc)); | |
65356e64 AC |
4321 | else |
4322 | Expr := Expression (Assoc); | |
4323 | end if; | |
996ae0b0 RK |
4324 | end if; |
4325 | ||
55603e5e | 4326 | Generate_Reference (Compon, Selector_Name, 'm'); |
fbf5a39b | 4327 | |
996ae0b0 RK |
4328 | else |
4329 | Error_Msg_NE | |
4330 | ("more than one value supplied for &", | |
4331 | Selector_Name, Compon); | |
4332 | ||
4333 | end if; | |
4334 | end if; | |
4335 | ||
4336 | Next (Selector_Name); | |
4337 | end loop; | |
4338 | ||
4339 | Next (Assoc); | |
4340 | end loop; | |
4341 | ||
4342 | return Expr; | |
4343 | end Get_Value; | |
4344 | ||
937e9676 AC |
4345 | ----------------------------- |
4346 | -- Propagate_Discriminants -- | |
4347 | ----------------------------- | |
4348 | ||
4349 | procedure Propagate_Discriminants | |
4350 | (Aggr : Node_Id; | |
4351 | Assoc_List : List_Id) | |
4352 | is | |
4353 | Loc : constant Source_Ptr := Sloc (N); | |
4354 | ||
937e9676 AC |
4355 | procedure Process_Component (Comp : Entity_Id); |
4356 | -- Add one component with a box association to the inner aggregate, | |
4357 | -- and recurse if component is itself composite. | |
4358 | ||
4359 | ----------------------- | |
4360 | -- Process_Component -- | |
4361 | ----------------------- | |
4362 | ||
4363 | procedure Process_Component (Comp : Entity_Id) is | |
4364 | T : constant Entity_Id := Etype (Comp); | |
4365 | New_Aggr : Node_Id; | |
4366 | ||
4367 | begin | |
4368 | if Is_Record_Type (T) and then Has_Discriminants (T) then | |
0ad46f04 | 4369 | New_Aggr := Make_Aggregate (Loc, No_List, New_List); |
937e9676 AC |
4370 | Set_Etype (New_Aggr, T); |
4371 | ||
4372 | Add_Association | |
4373 | (Comp, New_Aggr, Component_Associations (Aggr)); | |
4374 | ||
4375 | -- Collect discriminant values and recurse | |
4376 | ||
4377 | Add_Discriminant_Values (New_Aggr, Assoc_List); | |
4378 | Propagate_Discriminants (New_Aggr, Assoc_List); | |
4379 | ||
fc4c7348 PT |
4380 | Build_Constrained_Itype |
4381 | (New_Aggr, T, Component_Associations (New_Aggr)); | |
937e9676 | 4382 | else |
0c25b783 PT |
4383 | Add_Association |
4384 | (Comp, Empty, Component_Associations (Aggr), | |
4385 | Is_Box_Present => True); | |
937e9676 AC |
4386 | end if; |
4387 | end Process_Component; | |
4388 | ||
4389 | -- Local variables | |
4390 | ||
4391 | Aggr_Type : constant Entity_Id := Base_Type (Etype (Aggr)); | |
4392 | Components : constant Elist_Id := New_Elmt_List; | |
4393 | Def_Node : constant Node_Id := | |
4394 | Type_Definition (Declaration_Node (Aggr_Type)); | |
4395 | ||
4396 | Comp : Node_Id; | |
4397 | Comp_Elmt : Elmt_Id; | |
4398 | Errors : Boolean; | |
4399 | ||
4400 | -- Start of processing for Propagate_Discriminants | |
4401 | ||
4402 | begin | |
4403 | -- The component type may be a variant type. Collect the components | |
4404 | -- that are ruled by the known values of the discriminants. Their | |
4405 | -- values have already been inserted into the component list of the | |
4406 | -- current aggregate. | |
4407 | ||
4408 | if Nkind (Def_Node) = N_Record_Definition | |
4409 | and then Present (Component_List (Def_Node)) | |
4410 | and then Present (Variant_Part (Component_List (Def_Node))) | |
4411 | then | |
4412 | Gather_Components (Aggr_Type, | |
4413 | Component_List (Def_Node), | |
4414 | Governed_By => Component_Associations (Aggr), | |
4415 | Into => Components, | |
4416 | Report_Errors => Errors); | |
4417 | ||
4418 | Comp_Elmt := First_Elmt (Components); | |
4419 | while Present (Comp_Elmt) loop | |
4420 | if Ekind (Node (Comp_Elmt)) /= E_Discriminant then | |
4421 | Process_Component (Node (Comp_Elmt)); | |
4422 | end if; | |
4423 | ||
4424 | Next_Elmt (Comp_Elmt); | |
4425 | end loop; | |
4426 | ||
4427 | -- No variant part, iterate over all components | |
4428 | ||
4429 | else | |
4430 | Comp := First_Component (Etype (Aggr)); | |
4431 | while Present (Comp) loop | |
4432 | Process_Component (Comp); | |
4433 | Next_Component (Comp); | |
4434 | end loop; | |
4435 | end if; | |
937e9676 AC |
4436 | end Propagate_Discriminants; |
4437 | ||
996ae0b0 RK |
4438 | ----------------------- |
4439 | -- Resolve_Aggr_Expr -- | |
4440 | ----------------------- | |
4441 | ||
937e9676 | 4442 | procedure Resolve_Aggr_Expr (Expr : Node_Id; Component : Entity_Id) is |
996ae0b0 RK |
4443 | function Has_Expansion_Delayed (Expr : Node_Id) return Boolean; |
4444 | -- If the expression is an aggregate (possibly qualified) then its | |
4445 | -- expansion is delayed until the enclosing aggregate is expanded | |
4446 | -- into assignments. In that case, do not generate checks on the | |
4447 | -- expression, because they will be generated later, and will other- | |
92b751fd | 4448 | -- wise force a copy (to remove side effects) that would leave a |
996ae0b0 RK |
4449 | -- dynamic-sized aggregate in the code, something that gigi cannot |
4450 | -- handle. | |
4451 | ||
22243c12 RD |
4452 | --------------------------- |
4453 | -- Has_Expansion_Delayed -- | |
4454 | --------------------------- | |
996ae0b0 RK |
4455 | |
4456 | function Has_Expansion_Delayed (Expr : Node_Id) return Boolean is | |
996ae0b0 | 4457 | begin |
937e9676 | 4458 | return |
4a08c95c | 4459 | (Nkind (Expr) in N_Aggregate | N_Extension_Aggregate |
937e9676 AC |
4460 | and then Present (Etype (Expr)) |
4461 | and then Is_Record_Type (Etype (Expr)) | |
4462 | and then Expansion_Delayed (Expr)) | |
4463 | or else | |
4464 | (Nkind (Expr) = N_Qualified_Expression | |
4465 | and then Has_Expansion_Delayed (Expression (Expr))); | |
996ae0b0 RK |
4466 | end Has_Expansion_Delayed; |
4467 | ||
10edebe7 AC |
4468 | -- Local variables |
4469 | ||
4470 | Expr_Type : Entity_Id := Empty; | |
4471 | New_C : Entity_Id := Component; | |
4472 | New_Expr : Node_Id; | |
4473 | ||
4474 | Relocate : Boolean; | |
4475 | -- Set to True if the resolved Expr node needs to be relocated when | |
4476 | -- attached to the newly created association list. This node need not | |
4477 | -- be relocated if its parent pointer is not set. In fact in this | |
4478 | -- case Expr is the output of a New_Copy_Tree call. If Relocate is | |
4479 | -- True then we have analyzed the expression node in the original | |
4480 | -- aggregate and hence it needs to be relocated when moved over to | |
4481 | -- the new association list. | |
4482 | ||
22243c12 | 4483 | -- Start of processing for Resolve_Aggr_Expr |
996ae0b0 RK |
4484 | |
4485 | begin | |
4486 | -- If the type of the component is elementary or the type of the | |
4487 | -- aggregate does not contain discriminants, use the type of the | |
4488 | -- component to resolve Expr. | |
4489 | ||
4490 | if Is_Elementary_Type (Etype (Component)) | |
4491 | or else not Has_Discriminants (Etype (N)) | |
4492 | then | |
4493 | Expr_Type := Etype (Component); | |
4494 | ||
4495 | -- Otherwise we have to pick up the new type of the component from | |
12a13f01 | 4496 | -- the new constrained subtype of the aggregate. In fact components |
996ae0b0 RK |
4497 | -- which are of a composite type might be constrained by a |
4498 | -- discriminant, and we want to resolve Expr against the subtype were | |
4499 | -- all discriminant occurrences are replaced with their actual value. | |
4500 | ||
4501 | else | |
4502 | New_C := First_Component (Etype (N)); | |
4503 | while Present (New_C) loop | |
4504 | if Chars (New_C) = Chars (Component) then | |
4505 | Expr_Type := Etype (New_C); | |
4506 | exit; | |
4507 | end if; | |
4508 | ||
4509 | Next_Component (New_C); | |
4510 | end loop; | |
4511 | ||
4512 | pragma Assert (Present (Expr_Type)); | |
4513 | ||
4514 | -- For each range in an array type where a discriminant has been | |
4515 | -- replaced with the constraint, check that this range is within | |
ec53a6da JM |
4516 | -- the range of the base type. This checks is done in the init |
4517 | -- proc for regular objects, but has to be done here for | |
fbf5a39b | 4518 | -- aggregates since no init proc is called for them. |
996ae0b0 RK |
4519 | |
4520 | if Is_Array_Type (Expr_Type) then | |
4521 | declare | |
7f9747c6 | 4522 | Index : Node_Id; |
ec53a6da | 4523 | -- Range of the current constrained index in the array |
996ae0b0 | 4524 | |
ec53a6da | 4525 | Orig_Index : Node_Id := First_Index (Etype (Component)); |
996ae0b0 RK |
4526 | -- Range corresponding to the range Index above in the |
4527 | -- original unconstrained record type. The bounds of this | |
4528 | -- range may be governed by discriminants. | |
4529 | ||
4530 | Unconstr_Index : Node_Id := First_Index (Etype (Expr_Type)); | |
4531 | -- Range corresponding to the range Index above for the | |
4532 | -- unconstrained array type. This range is needed to apply | |
4533 | -- range checks. | |
4534 | ||
4535 | begin | |
7f9747c6 | 4536 | Index := First_Index (Expr_Type); |
996ae0b0 RK |
4537 | while Present (Index) loop |
4538 | if Depends_On_Discriminant (Orig_Index) then | |
4539 | Apply_Range_Check (Index, Etype (Unconstr_Index)); | |
4540 | end if; | |
4541 | ||
4542 | Next_Index (Index); | |
4543 | Next_Index (Orig_Index); | |
4544 | Next_Index (Unconstr_Index); | |
4545 | end loop; | |
4546 | end; | |
4547 | end if; | |
4548 | end if; | |
4549 | ||
4550 | -- If the Parent pointer of Expr is not set, Expr is an expression | |
4551 | -- duplicated by New_Tree_Copy (this happens for record aggregates | |
4552 | -- that look like (Field1 | Filed2 => Expr) or (others => Expr)). | |
4553 | -- Such a duplicated expression must be attached to the tree | |
4554 | -- before analysis and resolution to enforce the rule that a tree | |
4555 | -- fragment should never be analyzed or resolved unless it is | |
4556 | -- attached to the current compilation unit. | |
4557 | ||
4558 | if No (Parent (Expr)) then | |
4559 | Set_Parent (Expr, N); | |
4560 | Relocate := False; | |
4561 | else | |
4562 | Relocate := True; | |
4563 | end if; | |
4564 | ||
4565 | Analyze_And_Resolve (Expr, Expr_Type); | |
ca44152f | 4566 | Check_Expr_OK_In_Limited_Aggregate (Expr); |
996ae0b0 | 4567 | Check_Non_Static_Context (Expr); |
fbf5a39b | 4568 | Check_Unset_Reference (Expr); |
996ae0b0 | 4569 | |
0c020dde AC |
4570 | -- Check wrong use of class-wide types |
4571 | ||
7b4db06c | 4572 | if Is_Class_Wide_Type (Etype (Expr)) then |
0c020dde AC |
4573 | Error_Msg_N ("dynamically tagged expression not allowed", Expr); |
4574 | end if; | |
4575 | ||
996ae0b0 RK |
4576 | if not Has_Expansion_Delayed (Expr) then |
4577 | Aggregate_Constraint_Checks (Expr, Expr_Type); | |
887d102a AC |
4578 | end if; |
4579 | ||
dec6faf1 AC |
4580 | -- If an aggregate component has a type with predicates, an explicit |
4581 | -- predicate check must be applied, as for an assignment statement, | |
152f64c2 | 4582 | -- because the aggregate might not be expanded into individual |
dec6faf1 AC |
4583 | -- component assignments. |
4584 | ||
619bfd9f | 4585 | if Has_Predicates (Expr_Type) |
07eb872e AC |
4586 | and then Analyzed (Expr) |
4587 | then | |
887d102a | 4588 | Apply_Predicate_Check (Expr, Expr_Type); |
996ae0b0 RK |
4589 | end if; |
4590 | ||
4591 | if Raises_Constraint_Error (Expr) then | |
4592 | Set_Raises_Constraint_Error (N); | |
4593 | end if; | |
4594 | ||
22243c12 | 4595 | -- If the expression has been marked as requiring a range check, then |
edab6088 RD |
4596 | -- generate it here. It's a bit odd to be generating such checks in |
4597 | -- the analyzer, but harmless since Generate_Range_Check does nothing | |
4598 | -- (other than making sure Do_Range_Check is set) if the expander is | |
4599 | -- not active. | |
d79e621a GD |
4600 | |
4601 | if Do_Range_Check (Expr) then | |
d79e621a GD |
4602 | Generate_Range_Check (Expr, Expr_Type, CE_Range_Check_Failed); |
4603 | end if; | |
4604 | ||
937e9676 AC |
4605 | -- Add association Component => Expr if the caller requests it |
4606 | ||
996ae0b0 | 4607 | if Relocate then |
0929eaeb AC |
4608 | New_Expr := Relocate_Node (Expr); |
4609 | ||
4610 | -- Since New_Expr is not gonna be analyzed later on, we need to | |
4611 | -- propagate here the dimensions form Expr to New_Expr. | |
4612 | ||
ba914484 | 4613 | Copy_Dimensions (Expr, New_Expr); |
d976bf74 | 4614 | |
996ae0b0 | 4615 | else |
0929eaeb | 4616 | New_Expr := Expr; |
996ae0b0 | 4617 | end if; |
0929eaeb AC |
4618 | |
4619 | Add_Association (New_C, New_Expr, New_Assoc_List); | |
996ae0b0 RK |
4620 | end Resolve_Aggr_Expr; |
4621 | ||
333e4f86 AC |
4622 | ------------------- |
4623 | -- Rewrite_Range -- | |
4624 | ------------------- | |
4625 | ||
4626 | procedure Rewrite_Range (Root_Type : Entity_Id; Rge : Node_Id) is | |
333e4f86 AC |
4627 | procedure Rewrite_Bound |
4628 | (Bound : Node_Id; | |
4629 | Disc : Entity_Id; | |
4630 | Expr_Disc : Node_Id); | |
4631 | -- Rewrite a bound of the range Bound, when it is equal to the | |
4632 | -- non-stored discriminant Disc, into the stored discriminant | |
4633 | -- value Expr_Disc. | |
4634 | ||
4635 | ------------------- | |
4636 | -- Rewrite_Bound -- | |
4637 | ------------------- | |
4638 | ||
4639 | procedure Rewrite_Bound | |
4640 | (Bound : Node_Id; | |
4641 | Disc : Entity_Id; | |
4642 | Expr_Disc : Node_Id) | |
4643 | is | |
4644 | begin | |
2c26d262 EB |
4645 | if Nkind (Bound) /= N_Identifier then |
4646 | return; | |
4647 | end if; | |
4648 | ||
4649 | -- We expect either the discriminant or the discriminal | |
4650 | ||
4651 | if Entity (Bound) = Disc | |
4652 | or else (Ekind (Entity (Bound)) = E_In_Parameter | |
4653 | and then Discriminal_Link (Entity (Bound)) = Disc) | |
333e4f86 AC |
4654 | then |
4655 | Rewrite (Bound, New_Copy_Tree (Expr_Disc)); | |
4656 | end if; | |
4657 | end Rewrite_Bound; | |
4658 | ||
9c5719f6 | 4659 | -- Local variables |
333e4f86 AC |
4660 | |
4661 | Low, High : Node_Id; | |
4662 | Disc : Entity_Id; | |
4663 | Expr_Disc : Elmt_Id; | |
4664 | ||
4665 | -- Start of processing for Rewrite_Range | |
4666 | ||
4667 | begin | |
2c26d262 | 4668 | if Has_Discriminants (Root_Type) and then Nkind (Rge) = N_Range then |
333e4f86 AC |
4669 | Low := Low_Bound (Rge); |
4670 | High := High_Bound (Rge); | |
4671 | ||
13126368 YM |
4672 | Disc := First_Discriminant (Root_Type); |
4673 | Expr_Disc := First_Elmt (Stored_Constraint (Etype (N))); | |
333e4f86 AC |
4674 | while Present (Disc) loop |
4675 | Rewrite_Bound (Low, Disc, Node (Expr_Disc)); | |
4676 | Rewrite_Bound (High, Disc, Node (Expr_Disc)); | |
4677 | Next_Discriminant (Disc); | |
4678 | Next_Elmt (Expr_Disc); | |
4679 | end loop; | |
4680 | end if; | |
4681 | end Rewrite_Range; | |
4682 | ||
937e9676 AC |
4683 | -- Local variables |
4684 | ||
4685 | Components : constant Elist_Id := New_Elmt_List; | |
4686 | -- Components is the list of the record components whose value must be | |
4687 | -- provided in the aggregate. This list does include discriminants. | |
4688 | ||
937e9676 AC |
4689 | Component : Entity_Id; |
4690 | Component_Elmt : Elmt_Id; | |
13126368 | 4691 | Expr : Node_Id; |
937e9676 AC |
4692 | Positional_Expr : Node_Id; |
4693 | ||
996ae0b0 RK |
4694 | -- Start of processing for Resolve_Record_Aggregate |
4695 | ||
4696 | begin | |
2ba431e5 | 4697 | -- A record aggregate is restricted in SPARK: |
15918371 | 4698 | |
bd65a2d7 AC |
4699 | -- Each named association can have only a single choice. |
4700 | -- OTHERS cannot be used. | |
4701 | -- Positional and named associations cannot be mixed. | |
9f90d123 | 4702 | |
fe5d3068 YM |
4703 | if Present (Component_Associations (N)) |
4704 | and then Present (First (Component_Associations (N))) | |
9f90d123 | 4705 | then |
9f90d123 AC |
4706 | declare |
4707 | Assoc : Node_Id; | |
bd65a2d7 | 4708 | |
9f90d123 AC |
4709 | begin |
4710 | Assoc := First (Component_Associations (N)); | |
4711 | while Present (Assoc) loop | |
ccd0ed95 | 4712 | if Nkind (Assoc) = N_Iterated_Component_Association then |
7f5e671b PMR |
4713 | Error_Msg_N |
4714 | ("iterated component association can only appear in an " | |
4715 | & "array aggregate", N); | |
ccd0ed95 | 4716 | raise Unrecoverable_Error; |
9f90d123 | 4717 | end if; |
bd65a2d7 | 4718 | |
cbbe41d1 | 4719 | Next (Assoc); |
9f90d123 AC |
4720 | end loop; |
4721 | end; | |
4722 | end if; | |
4723 | ||
996ae0b0 RK |
4724 | -- We may end up calling Duplicate_Subexpr on expressions that are |
4725 | -- attached to New_Assoc_List. For this reason we need to attach it | |
4726 | -- to the tree by setting its parent pointer to N. This parent point | |
4727 | -- will change in STEP 8 below. | |
4728 | ||
4729 | Set_Parent (New_Assoc_List, N); | |
4730 | ||
4731 | -- STEP 1: abstract type and null record verification | |
4732 | ||
aad93b55 | 4733 | if Is_Abstract_Type (Typ) then |
996ae0b0 RK |
4734 | Error_Msg_N ("type of aggregate cannot be abstract", N); |
4735 | end if; | |
4736 | ||
4737 | if No (First_Entity (Typ)) and then Null_Record_Present (N) then | |
4738 | Set_Etype (N, Typ); | |
4739 | return; | |
4740 | ||
4741 | elsif Present (First_Entity (Typ)) | |
4742 | and then Null_Record_Present (N) | |
4743 | and then not Is_Tagged_Type (Typ) | |
4744 | then | |
4745 | Error_Msg_N ("record aggregate cannot be null", N); | |
4746 | return; | |
4747 | ||
eff332d9 GD |
4748 | -- If the type has no components, then the aggregate should either |
4749 | -- have "null record", or in Ada 2005 it could instead have a single | |
22243c12 RD |
4750 | -- component association given by "others => <>". For Ada 95 we flag an |
4751 | -- error at this point, but for Ada 2005 we proceed with checking the | |
4752 | -- associations below, which will catch the case where it's not an | |
4753 | -- aggregate with "others => <>". Note that the legality of a <> | |
eff332d9 GD |
4754 | -- aggregate for a null record type was established by AI05-016. |
4755 | ||
4756 | elsif No (First_Entity (Typ)) | |
0791fbe9 | 4757 | and then Ada_Version < Ada_2005 |
eff332d9 | 4758 | then |
996ae0b0 RK |
4759 | Error_Msg_N ("record aggregate must be null", N); |
4760 | return; | |
4761 | end if; | |
4762 | ||
4763 | -- STEP 2: Verify aggregate structure | |
4764 | ||
4765 | Step_2 : declare | |
937e9676 | 4766 | Assoc : Node_Id; |
996ae0b0 | 4767 | Bad_Aggregate : Boolean := False; |
937e9676 | 4768 | Selector_Name : Node_Id; |
996ae0b0 RK |
4769 | |
4770 | begin | |
4771 | if Present (Component_Associations (N)) then | |
4772 | Assoc := First (Component_Associations (N)); | |
4773 | else | |
4774 | Assoc := Empty; | |
4775 | end if; | |
4776 | ||
4777 | while Present (Assoc) loop | |
4778 | Selector_Name := First (Choices (Assoc)); | |
4779 | while Present (Selector_Name) loop | |
4780 | if Nkind (Selector_Name) = N_Identifier then | |
4781 | null; | |
4782 | ||
4783 | elsif Nkind (Selector_Name) = N_Others_Choice then | |
4784 | if Selector_Name /= First (Choices (Assoc)) | |
4785 | or else Present (Next (Selector_Name)) | |
4786 | then | |
ed2233dc | 4787 | Error_Msg_N |
22cb89b5 AC |
4788 | ("OTHERS must appear alone in a choice list", |
4789 | Selector_Name); | |
996ae0b0 RK |
4790 | return; |
4791 | ||
4792 | elsif Present (Next (Assoc)) then | |
ed2233dc | 4793 | Error_Msg_N |
22cb89b5 AC |
4794 | ("OTHERS must appear last in an aggregate", |
4795 | Selector_Name); | |
996ae0b0 | 4796 | return; |
1ab9541b | 4797 | |
885c4871 | 4798 | -- (Ada 2005): If this is an association with a box, |
1ab9541b ES |
4799 | -- indicate that the association need not represent |
4800 | -- any component. | |
4801 | ||
4802 | elsif Box_Present (Assoc) then | |
ec3c7387 AC |
4803 | Others_Box := 1; |
4804 | Box_Node := Assoc; | |
996ae0b0 RK |
4805 | end if; |
4806 | ||
4807 | else | |
4808 | Error_Msg_N | |
4809 | ("selector name should be identifier or OTHERS", | |
4810 | Selector_Name); | |
4811 | Bad_Aggregate := True; | |
4812 | end if; | |
4813 | ||
4814 | Next (Selector_Name); | |
4815 | end loop; | |
4816 | ||
4817 | Next (Assoc); | |
4818 | end loop; | |
4819 | ||
4820 | if Bad_Aggregate then | |
4821 | return; | |
4822 | end if; | |
4823 | end Step_2; | |
4824 | ||
4825 | -- STEP 3: Find discriminant Values | |
4826 | ||
4827 | Step_3 : declare | |
4828 | Discrim : Entity_Id; | |
4829 | Missing_Discriminants : Boolean := False; | |
4830 | ||
4831 | begin | |
4832 | if Present (Expressions (N)) then | |
4833 | Positional_Expr := First (Expressions (N)); | |
4834 | else | |
4835 | Positional_Expr := Empty; | |
4836 | end if; | |
4837 | ||
87729e5a | 4838 | -- AI05-0115: if the ancestor part is a subtype mark, the ancestor |
33bd17e7 | 4839 | -- must not have unknown discriminants. |
51e2de47 AC |
4840 | -- ??? We are not checking any subtype mark here and this code is not |
4841 | -- exercised by any test, so it's likely wrong (in particular | |
4842 | -- we should not use Root_Type here but the subtype mark, if any), | |
4843 | -- and possibly not needed. | |
87729e5a AC |
4844 | |
4845 | if Is_Derived_Type (Typ) | |
4846 | and then Has_Unknown_Discriminants (Root_Type (Typ)) | |
4847 | and then Nkind (N) /= N_Extension_Aggregate | |
4848 | then | |
4849 | Error_Msg_NE | |
4850 | ("aggregate not available for type& whose ancestor " | |
0bfa2f3c | 4851 | & "has unknown discriminants", N, Typ); |
87729e5a AC |
4852 | end if; |
4853 | ||
9013065b AC |
4854 | if Has_Unknown_Discriminants (Typ) |
4855 | and then Present (Underlying_Record_View (Typ)) | |
4856 | then | |
4857 | Discrim := First_Discriminant (Underlying_Record_View (Typ)); | |
4858 | elsif Has_Discriminants (Typ) then | |
996ae0b0 RK |
4859 | Discrim := First_Discriminant (Typ); |
4860 | else | |
4861 | Discrim := Empty; | |
4862 | end if; | |
4863 | ||
4864 | -- First find the discriminant values in the positional components | |
4865 | ||
4866 | while Present (Discrim) and then Present (Positional_Expr) loop | |
937e9676 | 4867 | if Discriminant_Present (Discrim) then |
996ae0b0 | 4868 | Resolve_Aggr_Expr (Positional_Expr, Discrim); |
2820d220 | 4869 | |
0ab80019 | 4870 | -- Ada 2005 (AI-231) |
2820d220 | 4871 | |
0791fbe9 | 4872 | if Ada_Version >= Ada_2005 |
8133b9d1 | 4873 | and then Known_Null (Positional_Expr) |
ec53a6da | 4874 | then |
82c80734 | 4875 | Check_Can_Never_Be_Null (Discrim, Positional_Expr); |
2820d220 AC |
4876 | end if; |
4877 | ||
996ae0b0 RK |
4878 | Next (Positional_Expr); |
4879 | end if; | |
4880 | ||
4881 | if Present (Get_Value (Discrim, Component_Associations (N))) then | |
4882 | Error_Msg_NE | |
4883 | ("more than one value supplied for discriminant&", | |
4884 | N, Discrim); | |
4885 | end if; | |
4886 | ||
4887 | Next_Discriminant (Discrim); | |
4888 | end loop; | |
4889 | ||
50decc81 | 4890 | -- Find remaining discriminant values if any among named components |
996ae0b0 RK |
4891 | |
4892 | while Present (Discrim) loop | |
4893 | Expr := Get_Value (Discrim, Component_Associations (N), True); | |
4894 | ||
937e9676 | 4895 | if not Discriminant_Present (Discrim) then |
996ae0b0 RK |
4896 | if Present (Expr) then |
4897 | Error_Msg_NE | |
58009744 | 4898 | ("more than one value supplied for discriminant &", |
996ae0b0 RK |
4899 | N, Discrim); |
4900 | end if; | |
4901 | ||
4902 | elsif No (Expr) then | |
4903 | Error_Msg_NE | |
4904 | ("no value supplied for discriminant &", N, Discrim); | |
4905 | Missing_Discriminants := True; | |
4906 | ||
4907 | else | |
4908 | Resolve_Aggr_Expr (Expr, Discrim); | |
4909 | end if; | |
4910 | ||
4911 | Next_Discriminant (Discrim); | |
4912 | end loop; | |
4913 | ||
4914 | if Missing_Discriminants then | |
4915 | return; | |
4916 | end if; | |
4917 | ||
4918 | -- At this point and until the beginning of STEP 6, New_Assoc_List | |
4919 | -- contains only the discriminants and their values. | |
4920 | ||
4921 | end Step_3; | |
4922 | ||
4923 | -- STEP 4: Set the Etype of the record aggregate | |
4924 | ||
9013065b AC |
4925 | if Has_Discriminants (Typ) |
4926 | or else (Has_Unknown_Discriminants (Typ) | |
58009744 | 4927 | and then Present (Underlying_Record_View (Typ))) |
9013065b | 4928 | then |
fc4c7348 | 4929 | Build_Constrained_Itype (N, Typ, New_Assoc_List); |
996ae0b0 RK |
4930 | else |
4931 | Set_Etype (N, Typ); | |
4932 | end if; | |
4933 | ||
4934 | -- STEP 5: Get remaining components according to discriminant values | |
4935 | ||
4936 | Step_5 : declare | |
937e9676 AC |
4937 | Dnode : Node_Id; |
4938 | Errors_Found : Boolean := False; | |
996ae0b0 RK |
4939 | Record_Def : Node_Id; |
4940 | Parent_Typ : Entity_Id; | |
996ae0b0 RK |
4941 | Parent_Typ_List : Elist_Id; |
4942 | Parent_Elmt : Elmt_Id; | |
937e9676 | 4943 | Root_Typ : Entity_Id; |
15918371 | 4944 | |
996ae0b0 RK |
4945 | begin |
4946 | if Is_Derived_Type (Typ) and then Is_Tagged_Type (Typ) then | |
4947 | Parent_Typ_List := New_Elmt_List; | |
4948 | ||
4949 | -- If this is an extension aggregate, the component list must | |
965dbd5c AC |
4950 | -- include all components that are not in the given ancestor type. |
4951 | -- Otherwise, the component list must include components of all | |
4952 | -- ancestors, starting with the root. | |
996ae0b0 RK |
4953 | |
4954 | if Nkind (N) = N_Extension_Aggregate then | |
7b4db06c | 4955 | Root_Typ := Base_Type (Etype (Ancestor_Part (N))); |
69a0c174 | 4956 | |
996ae0b0 | 4957 | else |
937e9676 AC |
4958 | -- AI05-0115: check legality of aggregate for type with a |
4959 | -- private ancestor. | |
87729e5a | 4960 | |
996ae0b0 | 4961 | Root_Typ := Root_Type (Typ); |
87729e5a AC |
4962 | if Has_Private_Ancestor (Typ) then |
4963 | declare | |
4964 | Ancestor : constant Entity_Id := | |
937e9676 | 4965 | Find_Private_Ancestor (Typ); |
87729e5a | 4966 | Ancestor_Unit : constant Entity_Id := |
937e9676 AC |
4967 | Cunit_Entity |
4968 | (Get_Source_Unit (Ancestor)); | |
87729e5a | 4969 | Parent_Unit : constant Entity_Id := |
937e9676 AC |
4970 | Cunit_Entity (Get_Source_Unit |
4971 | (Base_Type (Etype (Ancestor)))); | |
87729e5a | 4972 | begin |
58009744 | 4973 | -- Check whether we are in a scope that has full view |
87729e5a AC |
4974 | -- over the private ancestor and its parent. This can |
4975 | -- only happen if the derivation takes place in a child | |
4976 | -- unit of the unit that declares the parent, and we are | |
4977 | -- in the private part or body of that child unit, else | |
4978 | -- the aggregate is illegal. | |
4979 | ||
4980 | if Is_Child_Unit (Ancestor_Unit) | |
4981 | and then Scope (Ancestor_Unit) = Parent_Unit | |
4982 | and then In_Open_Scopes (Scope (Ancestor)) | |
4983 | and then | |
4984 | (In_Private_Part (Scope (Ancestor)) | |
58009744 | 4985 | or else In_Package_Body (Scope (Ancestor))) |
87729e5a AC |
4986 | then |
4987 | null; | |
4988 | ||
4989 | else | |
4990 | Error_Msg_NE | |
4991 | ("type of aggregate has private ancestor&!", | |
58009744 | 4992 | N, Root_Typ); |
87729e5a AC |
4993 | Error_Msg_N ("must use extension aggregate!", N); |
4994 | return; | |
4995 | end if; | |
4996 | end; | |
996ae0b0 RK |
4997 | end if; |
4998 | ||
4999 | Dnode := Declaration_Node (Base_Type (Root_Typ)); | |
5000 | ||
4519314c AC |
5001 | -- If we don't get a full declaration, then we have some error |
5002 | -- which will get signalled later so skip this part. Otherwise | |
5003 | -- gather components of root that apply to the aggregate type. | |
5004 | -- We use the base type in case there is an applicable stored | |
5005 | -- constraint that renames the discriminants of the root. | |
996ae0b0 RK |
5006 | |
5007 | if Nkind (Dnode) = N_Full_Type_Declaration then | |
5008 | Record_Def := Type_Definition (Dnode); | |
15918371 AC |
5009 | Gather_Components |
5010 | (Base_Type (Typ), | |
5011 | Component_List (Record_Def), | |
5012 | Governed_By => New_Assoc_List, | |
5013 | Into => Components, | |
5014 | Report_Errors => Errors_Found); | |
4ffafd86 AC |
5015 | |
5016 | if Errors_Found then | |
5017 | Error_Msg_N | |
5018 | ("discriminant controlling variant part is not static", | |
5019 | N); | |
5020 | return; | |
5021 | end if; | |
996ae0b0 RK |
5022 | end if; |
5023 | end if; | |
5024 | ||
9013065b | 5025 | Parent_Typ := Base_Type (Typ); |
996ae0b0 | 5026 | while Parent_Typ /= Root_Typ loop |
996ae0b0 RK |
5027 | Prepend_Elmt (Parent_Typ, To => Parent_Typ_List); |
5028 | Parent_Typ := Etype (Parent_Typ); | |
5029 | ||
fbf5a39b | 5030 | if Nkind (Parent (Base_Type (Parent_Typ))) = |
996ae0b0 | 5031 | N_Private_Type_Declaration |
fbf5a39b AC |
5032 | or else Nkind (Parent (Base_Type (Parent_Typ))) = |
5033 | N_Private_Extension_Declaration | |
996ae0b0 RK |
5034 | then |
5035 | if Nkind (N) /= N_Extension_Aggregate then | |
ed2233dc | 5036 | Error_Msg_NE |
996ae0b0 RK |
5037 | ("type of aggregate has private ancestor&!", |
5038 | N, Parent_Typ); | |
ed2233dc | 5039 | Error_Msg_N ("must use extension aggregate!", N); |
996ae0b0 RK |
5040 | return; |
5041 | ||
5042 | elsif Parent_Typ /= Root_Typ then | |
5043 | Error_Msg_NE | |
5044 | ("ancestor part of aggregate must be private type&", | |
5045 | Ancestor_Part (N), Parent_Typ); | |
5046 | return; | |
5047 | end if; | |
4519314c AC |
5048 | |
5049 | -- The current view of ancestor part may be a private type, | |
5050 | -- while the context type is always non-private. | |
5051 | ||
5052 | elsif Is_Private_Type (Root_Typ) | |
5053 | and then Present (Full_View (Root_Typ)) | |
5054 | and then Nkind (N) = N_Extension_Aggregate | |
5055 | then | |
5056 | exit when Base_Type (Full_View (Root_Typ)) = Parent_Typ; | |
996ae0b0 RK |
5057 | end if; |
5058 | end loop; | |
5059 | ||
bf06d37f AC |
5060 | -- Now collect components from all other ancestors, beginning |
5061 | -- with the current type. If the type has unknown discriminants | |
349ff68f | 5062 | -- use the component list of the Underlying_Record_View, which |
bf06d37f AC |
5063 | -- needs to be used for the subsequent expansion of the aggregate |
5064 | -- into assignments. | |
996ae0b0 RK |
5065 | |
5066 | Parent_Elmt := First_Elmt (Parent_Typ_List); | |
5067 | while Present (Parent_Elmt) loop | |
5068 | Parent_Typ := Node (Parent_Elmt); | |
bf06d37f AC |
5069 | |
5070 | if Has_Unknown_Discriminants (Parent_Typ) | |
5071 | and then Present (Underlying_Record_View (Typ)) | |
5072 | then | |
5073 | Parent_Typ := Underlying_Record_View (Parent_Typ); | |
5074 | end if; | |
5075 | ||
996ae0b0 RK |
5076 | Record_Def := Type_Definition (Parent (Base_Type (Parent_Typ))); |
5077 | Gather_Components (Empty, | |
5078 | Component_List (Record_Extension_Part (Record_Def)), | |
5079 | Governed_By => New_Assoc_List, | |
5080 | Into => Components, | |
5081 | Report_Errors => Errors_Found); | |
5082 | ||
5083 | Next_Elmt (Parent_Elmt); | |
5084 | end loop; | |
5085 | ||
33bd17e7 ES |
5086 | -- Typ is not a derived tagged type |
5087 | ||
996ae0b0 | 5088 | else |
6bde3eb5 | 5089 | Record_Def := Type_Definition (Parent (Base_Type (Typ))); |
996ae0b0 RK |
5090 | |
5091 | if Null_Present (Record_Def) then | |
5092 | null; | |
bf06d37f AC |
5093 | |
5094 | elsif not Has_Unknown_Discriminants (Typ) then | |
15918371 AC |
5095 | Gather_Components |
5096 | (Base_Type (Typ), | |
5097 | Component_List (Record_Def), | |
5098 | Governed_By => New_Assoc_List, | |
5099 | Into => Components, | |
5100 | Report_Errors => Errors_Found); | |
bf06d37f AC |
5101 | |
5102 | else | |
5103 | Gather_Components | |
5104 | (Base_Type (Underlying_Record_View (Typ)), | |
15918371 AC |
5105 | Component_List (Record_Def), |
5106 | Governed_By => New_Assoc_List, | |
5107 | Into => Components, | |
5108 | Report_Errors => Errors_Found); | |
996ae0b0 RK |
5109 | end if; |
5110 | end if; | |
5111 | ||
5112 | if Errors_Found then | |
5113 | return; | |
5114 | end if; | |
5115 | end Step_5; | |
5116 | ||
5117 | -- STEP 6: Find component Values | |
5118 | ||
5119 | Component := Empty; | |
5120 | Component_Elmt := First_Elmt (Components); | |
5121 | ||
5122 | -- First scan the remaining positional associations in the aggregate. | |
5123 | -- Remember that at this point Positional_Expr contains the current | |
5124 | -- positional association if any is left after looking for discriminant | |
5125 | -- values in step 3. | |
5126 | ||
5127 | while Present (Positional_Expr) and then Present (Component_Elmt) loop | |
5128 | Component := Node (Component_Elmt); | |
5129 | Resolve_Aggr_Expr (Positional_Expr, Component); | |
5130 | ||
0ab80019 AC |
5131 | -- Ada 2005 (AI-231) |
5132 | ||
58009744 | 5133 | if Ada_Version >= Ada_2005 and then Known_Null (Positional_Expr) then |
82c80734 | 5134 | Check_Can_Never_Be_Null (Component, Positional_Expr); |
2820d220 AC |
5135 | end if; |
5136 | ||
996ae0b0 RK |
5137 | if Present (Get_Value (Component, Component_Associations (N))) then |
5138 | Error_Msg_NE | |
add27f7a | 5139 | ("more than one value supplied for component &", N, Component); |
996ae0b0 RK |
5140 | end if; |
5141 | ||
5142 | Next (Positional_Expr); | |
5143 | Next_Elmt (Component_Elmt); | |
5144 | end loop; | |
5145 | ||
5146 | if Present (Positional_Expr) then | |
5147 | Error_Msg_N | |
5148 | ("too many components for record aggregate", Positional_Expr); | |
5149 | end if; | |
5150 | ||
5151 | -- Now scan for the named arguments of the aggregate | |
5152 | ||
5153 | while Present (Component_Elmt) loop | |
5154 | Component := Node (Component_Elmt); | |
5155 | Expr := Get_Value (Component, Component_Associations (N), True); | |
5156 | ||
9b96e234 | 5157 | -- Note: The previous call to Get_Value sets the value of the |
f91e8020 | 5158 | -- variable Is_Box_Present. |
65356e64 | 5159 | |
9b96e234 JM |
5160 | -- Ada 2005 (AI-287): Handle components with default initialization. |
5161 | -- Note: This feature was originally added to Ada 2005 for limited | |
5162 | -- but it was finally allowed with any type. | |
65356e64 | 5163 | |
9b96e234 | 5164 | if Is_Box_Present then |
f91e8020 GD |
5165 | Check_Box_Component : declare |
5166 | Ctyp : constant Entity_Id := Etype (Component); | |
9b96e234 JM |
5167 | |
5168 | begin | |
f7937111 GD |
5169 | -- Initially assume that the box is for a default-initialized |
5170 | -- component and reset to False in cases where that's not true. | |
5171 | ||
5172 | Is_Box_Init_By_Default := True; | |
5173 | ||
c7ce71c2 | 5174 | -- If there is a default expression for the aggregate, copy |
0df7e2d0 AC |
5175 | -- it into a new association. This copy must modify the scopes |
5176 | -- of internal types that may be attached to the expression | |
5177 | -- (e.g. index subtypes of arrays) because in general the type | |
5178 | -- declaration and the aggregate appear in different scopes, | |
5179 | -- and the backend requires the scope of the type to match the | |
5180 | -- point at which it is elaborated. | |
c7ce71c2 | 5181 | |
9b96e234 JM |
5182 | -- If the component has an initialization procedure (IP) we |
5183 | -- pass the component to the expander, which will generate | |
5184 | -- the call to such IP. | |
5185 | ||
c7ce71c2 ES |
5186 | -- If the component has discriminants, their values must |
5187 | -- be taken from their subtype. This is indispensable for | |
5188 | -- constraints that are given by the current instance of an | |
50decc81 RD |
5189 | -- enclosing type, to allow the expansion of the aggregate to |
5190 | -- replace the reference to the current instance by the target | |
5191 | -- object of the aggregate. | |
c7ce71c2 ES |
5192 | |
5193 | if Present (Parent (Component)) | |
937e9676 | 5194 | and then Nkind (Parent (Component)) = N_Component_Declaration |
c7ce71c2 | 5195 | and then Present (Expression (Parent (Component))) |
aad93b55 | 5196 | then |
f7937111 GD |
5197 | -- If component declaration has an initialization expression |
5198 | -- then this is not a case of default initialization. | |
5199 | ||
5200 | Is_Box_Init_By_Default := False; | |
5201 | ||
c7ce71c2 | 5202 | Expr := |
ba914484 | 5203 | New_Copy_Tree_And_Copy_Dimensions |
2293611f AC |
5204 | (Expression (Parent (Component)), |
5205 | New_Scope => Current_Scope, | |
5206 | New_Sloc => Sloc (N)); | |
c7ce71c2 | 5207 | |
333e4f86 AC |
5208 | -- As the type of the copied default expression may refer |
5209 | -- to discriminants of the record type declaration, these | |
5210 | -- non-stored discriminants need to be rewritten into stored | |
5211 | -- discriminant values for the aggregate. This is required | |
5212 | -- in GNATprove mode, and is adopted in all modes to avoid | |
5213 | -- special-casing GNATprove mode. | |
5214 | ||
5215 | if Is_Array_Type (Etype (Expr)) then | |
5216 | declare | |
13126368 YM |
5217 | Rec_Typ : constant Entity_Id := Scope (Component); |
5218 | -- Root record type whose discriminants may be used as | |
5219 | -- bounds in range nodes. | |
5220 | ||
2c26d262 EB |
5221 | Assoc : Node_Id; |
5222 | Choice : Node_Id; | |
5223 | Index : Node_Id; | |
333e4f86 AC |
5224 | |
5225 | begin | |
5226 | -- Rewrite the range nodes occurring in the indexes | |
5227 | -- and their types. | |
5228 | ||
5229 | Index := First_Index (Etype (Expr)); | |
5230 | while Present (Index) loop | |
13126368 | 5231 | Rewrite_Range (Rec_Typ, Index); |
333e4f86 | 5232 | Rewrite_Range |
13126368 YM |
5233 | (Rec_Typ, Scalar_Range (Etype (Index))); |
5234 | ||
333e4f86 AC |
5235 | Next_Index (Index); |
5236 | end loop; | |
5237 | ||
5238 | -- Rewrite the range nodes occurring as aggregate | |
2c26d262 | 5239 | -- bounds and component associations. |
333e4f86 | 5240 | |
2c26d262 EB |
5241 | if Nkind (Expr) = N_Aggregate then |
5242 | if Present (Aggregate_Bounds (Expr)) then | |
5243 | Rewrite_Range (Rec_Typ, Aggregate_Bounds (Expr)); | |
5244 | end if; | |
5245 | ||
5246 | if Present (Component_Associations (Expr)) then | |
5247 | Assoc := First (Component_Associations (Expr)); | |
5248 | while Present (Assoc) loop | |
5249 | Choice := First (Choices (Assoc)); | |
5250 | while Present (Choice) loop | |
5251 | Rewrite_Range (Rec_Typ, Choice); | |
5252 | ||
5253 | Next (Choice); | |
5254 | end loop; | |
5255 | ||
5256 | Next (Assoc); | |
5257 | end loop; | |
5258 | end if; | |
333e4f86 AC |
5259 | end if; |
5260 | end; | |
5261 | end if; | |
5262 | ||
9b96e234 | 5263 | Add_Association |
107b023c AC |
5264 | (Component => Component, |
5265 | Expr => Expr, | |
5266 | Assoc_List => New_Assoc_List); | |
c7ce71c2 ES |
5267 | Set_Has_Self_Reference (N); |
5268 | ||
f91e8020 GD |
5269 | -- A box-defaulted access component gets the value null. Also |
5270 | -- included are components of private types whose underlying | |
c80d4855 RD |
5271 | -- type is an access type. In either case set the type of the |
5272 | -- literal, for subsequent use in semantic checks. | |
f91e8020 GD |
5273 | |
5274 | elsif Present (Underlying_Type (Ctyp)) | |
5275 | and then Is_Access_Type (Underlying_Type (Ctyp)) | |
5276 | then | |
f91e8020 GD |
5277 | -- If the component's type is private with an access type as |
5278 | -- its underlying type then we have to create an unchecked | |
5279 | -- conversion to satisfy type checking. | |
5280 | ||
937e9676 | 5281 | if Is_Private_Type (Ctyp) then |
f91e8020 GD |
5282 | declare |
5283 | Qual_Null : constant Node_Id := | |
5284 | Make_Qualified_Expression (Sloc (N), | |
5285 | Subtype_Mark => | |
5286 | New_Occurrence_Of | |
5287 | (Underlying_Type (Ctyp), Sloc (N)), | |
937e9676 | 5288 | Expression => Make_Null (Sloc (N))); |
f91e8020 GD |
5289 | |
5290 | Convert_Null : constant Node_Id := | |
5291 | Unchecked_Convert_To | |
5292 | (Ctyp, Qual_Null); | |
5293 | ||
5294 | begin | |
5295 | Analyze_And_Resolve (Convert_Null, Ctyp); | |
5296 | Add_Association | |
107b023c AC |
5297 | (Component => Component, |
5298 | Expr => Convert_Null, | |
5299 | Assoc_List => New_Assoc_List); | |
f91e8020 | 5300 | end; |
937e9676 AC |
5301 | |
5302 | -- Otherwise the component type is non-private | |
5303 | ||
5304 | else | |
5305 | Expr := Make_Null (Sloc (N)); | |
5306 | Set_Etype (Expr, Ctyp); | |
5307 | ||
5308 | Add_Association | |
5309 | (Component => Component, | |
5310 | Expr => Expr, | |
5311 | Assoc_List => New_Assoc_List); | |
f91e8020 GD |
5312 | end if; |
5313 | ||
7610fee8 | 5314 | -- Ada 2012: If component is scalar with default value, use it |
b6bcca6d AC |
5315 | -- by converting it to Ctyp, so that subtype constraints are |
5316 | -- checked. | |
7610fee8 AC |
5317 | |
5318 | elsif Is_Scalar_Type (Ctyp) | |
5319 | and then Has_Default_Aspect (Ctyp) | |
5320 | then | |
b6bcca6d AC |
5321 | declare |
5322 | Conv : constant Node_Id := | |
5323 | Convert_To | |
5324 | (Typ => Ctyp, | |
5325 | Expr => | |
5326 | New_Copy_Tree | |
5327 | (Default_Aspect_Value | |
5328 | (First_Subtype (Underlying_Type (Ctyp))))); | |
5329 | ||
5330 | begin | |
5331 | Analyze_And_Resolve (Conv, Ctyp); | |
5332 | Add_Association | |
5333 | (Component => Component, | |
5334 | Expr => Conv, | |
5335 | Assoc_List => New_Assoc_List); | |
5336 | end; | |
7610fee8 | 5337 | |
c7ce71c2 ES |
5338 | elsif Has_Non_Null_Base_Init_Proc (Ctyp) |
5339 | or else not Expander_Active | |
5340 | then | |
5341 | if Is_Record_Type (Ctyp) | |
5342 | and then Has_Discriminants (Ctyp) | |
6bde3eb5 | 5343 | and then not Is_Private_Type (Ctyp) |
c7ce71c2 ES |
5344 | then |
5345 | -- We build a partially initialized aggregate with the | |
5346 | -- values of the discriminants and box initialization | |
8133b9d1 | 5347 | -- for the rest, if other components are present. |
c7e152b5 | 5348 | |
51ec70b8 | 5349 | -- The type of the aggregate is the known subtype of |
937e9676 AC |
5350 | -- the component. The capture of discriminants must be |
5351 | -- recursive because subcomponents may be constrained | |
107b023c | 5352 | -- (transitively) by discriminants of enclosing types. |
6bde3eb5 AC |
5353 | -- For a private type with discriminants, a call to the |
5354 | -- initialization procedure will be generated, and no | |
5355 | -- subaggregate is needed. | |
c7ce71c2 | 5356 | |
107b023c | 5357 | Capture_Discriminants : declare |
719aaf4d AC |
5358 | Loc : constant Source_Ptr := Sloc (N); |
5359 | Expr : Node_Id; | |
c7ce71c2 | 5360 | |
107b023c | 5361 | begin |
0ad46f04 | 5362 | Expr := Make_Aggregate (Loc, No_List, New_List); |
107b023c AC |
5363 | Set_Etype (Expr, Ctyp); |
5364 | ||
3786bbd1 RD |
5365 | -- If the enclosing type has discriminants, they have |
5366 | -- been collected in the aggregate earlier, and they | |
5367 | -- may appear as constraints of subcomponents. | |
5368 | ||
107b023c | 5369 | -- Similarly if this component has discriminants, they |
2be0bff8 | 5370 | -- might in turn be propagated to their components. |
107b023c AC |
5371 | |
5372 | if Has_Discriminants (Typ) then | |
5373 | Add_Discriminant_Values (Expr, New_Assoc_List); | |
105b5e65 | 5374 | Propagate_Discriminants (Expr, New_Assoc_List); |
107b023c AC |
5375 | |
5376 | elsif Has_Discriminants (Ctyp) then | |
5377 | Add_Discriminant_Values | |
937e9676 | 5378 | (Expr, Component_Associations (Expr)); |
107b023c | 5379 | Propagate_Discriminants |
937e9676 | 5380 | (Expr, Component_Associations (Expr)); |
107b023c | 5381 | |
fc4c7348 PT |
5382 | Build_Constrained_Itype |
5383 | (Expr, Ctyp, Component_Associations (Expr)); | |
5384 | ||
107b023c AC |
5385 | else |
5386 | declare | |
719aaf4d | 5387 | Comp : Entity_Id; |
107b023c AC |
5388 | |
5389 | begin | |
5390 | -- If the type has additional components, create | |
2be0bff8 | 5391 | -- an OTHERS box association for them. |
107b023c AC |
5392 | |
5393 | Comp := First_Component (Ctyp); | |
5394 | while Present (Comp) loop | |
5395 | if Ekind (Comp) = E_Component then | |
5396 | if not Is_Record_Type (Etype (Comp)) then | |
37368818 RD |
5397 | Append_To |
5398 | (Component_Associations (Expr), | |
5399 | Make_Component_Association (Loc, | |
107b023c | 5400 | Choices => |
58009744 AC |
5401 | New_List ( |
5402 | Make_Others_Choice (Loc)), | |
107b023c | 5403 | Expression => Empty, |
58009744 | 5404 | Box_Present => True)); |
107b023c | 5405 | end if; |
937e9676 | 5406 | |
107b023c AC |
5407 | exit; |
5408 | end if; | |
5409 | ||
5410 | Next_Component (Comp); | |
5411 | end loop; | |
5412 | end; | |
5413 | end if; | |
c7ce71c2 ES |
5414 | |
5415 | Add_Association | |
107b023c AC |
5416 | (Component => Component, |
5417 | Expr => Expr, | |
5418 | Assoc_List => New_Assoc_List); | |
5419 | end Capture_Discriminants; | |
c7ce71c2 | 5420 | |
937e9676 AC |
5421 | -- Otherwise the component type is not a record, or it has |
5422 | -- not discriminants, or it is private. | |
5423 | ||
c7ce71c2 ES |
5424 | else |
5425 | Add_Association | |
5426 | (Component => Component, | |
5427 | Expr => Empty, | |
107b023c | 5428 | Assoc_List => New_Assoc_List, |
c7ce71c2 ES |
5429 | Is_Box_Present => True); |
5430 | end if; | |
9b96e234 JM |
5431 | |
5432 | -- Otherwise we only need to resolve the expression if the | |
5433 | -- component has partially initialized values (required to | |
5434 | -- expand the corresponding assignments and run-time checks). | |
5435 | ||
5436 | elsif Present (Expr) | |
f91e8020 | 5437 | and then Is_Partially_Initialized_Type (Ctyp) |
9b96e234 JM |
5438 | then |
5439 | Resolve_Aggr_Expr (Expr, Component); | |
5440 | end if; | |
f91e8020 | 5441 | end Check_Box_Component; |
615cbd95 | 5442 | |
65356e64 | 5443 | elsif No (Expr) then |
c7ce71c2 ES |
5444 | |
5445 | -- Ignore hidden components associated with the position of the | |
5446 | -- interface tags: these are initialized dynamically. | |
5447 | ||
c80d4855 | 5448 | if not Present (Related_Type (Component)) then |
c7ce71c2 ES |
5449 | Error_Msg_NE |
5450 | ("no value supplied for component &!", N, Component); | |
5451 | end if; | |
615cbd95 | 5452 | |
996ae0b0 RK |
5453 | else |
5454 | Resolve_Aggr_Expr (Expr, Component); | |
5455 | end if; | |
5456 | ||
5457 | Next_Elmt (Component_Elmt); | |
5458 | end loop; | |
5459 | ||
5460 | -- STEP 7: check for invalid components + check type in choice list | |
5461 | ||
5462 | Step_7 : declare | |
937e9676 AC |
5463 | Assoc : Node_Id; |
5464 | New_Assoc : Node_Id; | |
5465 | ||
996ae0b0 RK |
5466 | Selectr : Node_Id; |
5467 | -- Selector name | |
5468 | ||
9b96e234 | 5469 | Typech : Entity_Id; |
996ae0b0 RK |
5470 | -- Type of first component in choice list |
5471 | ||
5472 | begin | |
5473 | if Present (Component_Associations (N)) then | |
5474 | Assoc := First (Component_Associations (N)); | |
5475 | else | |
5476 | Assoc := Empty; | |
5477 | end if; | |
5478 | ||
5479 | Verification : while Present (Assoc) loop | |
5480 | Selectr := First (Choices (Assoc)); | |
5481 | Typech := Empty; | |
5482 | ||
5483 | if Nkind (Selectr) = N_Others_Choice then | |
19f0526a | 5484 | |
9b96e234 | 5485 | -- Ada 2005 (AI-287): others choice may have expression or box |
19f0526a | 5486 | |
ec3c7387 | 5487 | if No (Others_Etype) and then Others_Box = 0 then |
ed2233dc | 5488 | Error_Msg_N |
996ae0b0 | 5489 | ("OTHERS must represent at least one component", Selectr); |
ec3c7387 AC |
5490 | |
5491 | elsif Others_Box = 1 and then Warn_On_Redundant_Constructs then | |
9ed2b86d | 5492 | Error_Msg_N ("OTHERS choice is redundant?", Box_Node); |
bd717ec9 AC |
5493 | Error_Msg_N |
5494 | ("\previous choices cover all components?", Box_Node); | |
996ae0b0 RK |
5495 | end if; |
5496 | ||
5497 | exit Verification; | |
5498 | end if; | |
5499 | ||
5500 | while Present (Selectr) loop | |
5501 | New_Assoc := First (New_Assoc_List); | |
5502 | while Present (New_Assoc) loop | |
5503 | Component := First (Choices (New_Assoc)); | |
6989bc1f AC |
5504 | |
5505 | if Chars (Selectr) = Chars (Component) then | |
5506 | if Style_Check then | |
5507 | Check_Identifier (Selectr, Entity (Component)); | |
5508 | end if; | |
5509 | ||
5510 | exit; | |
5511 | end if; | |
5512 | ||
996ae0b0 RK |
5513 | Next (New_Assoc); |
5514 | end loop; | |
5515 | ||
c7e152b5 AC |
5516 | -- If no association, this is not a legal component of the type |
5517 | -- in question, unless its association is provided with a box. | |
996ae0b0 RK |
5518 | |
5519 | if No (New_Assoc) then | |
65356e64 | 5520 | if Box_Present (Parent (Selectr)) then |
aad93b55 ES |
5521 | |
5522 | -- This may still be a bogus component with a box. Scan | |
5523 | -- list of components to verify that a component with | |
5524 | -- that name exists. | |
5525 | ||
5526 | declare | |
5527 | C : Entity_Id; | |
5528 | ||
5529 | begin | |
5530 | C := First_Component (Typ); | |
5531 | while Present (C) loop | |
5532 | if Chars (C) = Chars (Selectr) then | |
ca44152f ES |
5533 | |
5534 | -- If the context is an extension aggregate, | |
5535 | -- the component must not be inherited from | |
5536 | -- the ancestor part of the aggregate. | |
5537 | ||
5538 | if Nkind (N) /= N_Extension_Aggregate | |
5539 | or else | |
5540 | Scope (Original_Record_Component (C)) /= | |
937e9676 | 5541 | Etype (Ancestor_Part (N)) |
ca44152f ES |
5542 | then |
5543 | exit; | |
5544 | end if; | |
aad93b55 ES |
5545 | end if; |
5546 | ||
5547 | Next_Component (C); | |
5548 | end loop; | |
5549 | ||
5550 | if No (C) then | |
5551 | Error_Msg_Node_2 := Typ; | |
5552 | Error_Msg_N ("& is not a component of}", Selectr); | |
5553 | end if; | |
5554 | end; | |
996ae0b0 | 5555 | |
65356e64 | 5556 | elsif Chars (Selectr) /= Name_uTag |
996ae0b0 | 5557 | and then Chars (Selectr) /= Name_uParent |
996ae0b0 RK |
5558 | then |
5559 | if not Has_Discriminants (Typ) then | |
5560 | Error_Msg_Node_2 := Typ; | |
aad93b55 | 5561 | Error_Msg_N ("& is not a component of}", Selectr); |
996ae0b0 RK |
5562 | else |
5563 | Error_Msg_N | |
5564 | ("& is not a component of the aggregate subtype", | |
5565 | Selectr); | |
5566 | end if; | |
5567 | ||
5568 | Check_Misspelled_Component (Components, Selectr); | |
5569 | end if; | |
5570 | ||
5571 | elsif No (Typech) then | |
5572 | Typech := Base_Type (Etype (Component)); | |
5573 | ||
feab3549 | 5574 | -- AI05-0199: In Ada 2012, several components of anonymous |
8da337c5 AC |
5575 | -- access types can appear in a choice list, as long as the |
5576 | -- designated types match. | |
5577 | ||
996ae0b0 | 5578 | elsif Typech /= Base_Type (Etype (Component)) then |
dbe945f1 | 5579 | if Ada_Version >= Ada_2012 |
8da337c5 AC |
5580 | and then Ekind (Typech) = E_Anonymous_Access_Type |
5581 | and then | |
5582 | Ekind (Etype (Component)) = E_Anonymous_Access_Type | |
5583 | and then Base_Type (Designated_Type (Typech)) = | |
5584 | Base_Type (Designated_Type (Etype (Component))) | |
5585 | and then | |
5586 | Subtypes_Statically_Match (Typech, (Etype (Component))) | |
5587 | then | |
5588 | null; | |
5589 | ||
5590 | elsif not Box_Present (Parent (Selectr)) then | |
65356e64 AC |
5591 | Error_Msg_N |
5592 | ("components in choice list must have same type", | |
5593 | Selectr); | |
5594 | end if; | |
996ae0b0 RK |
5595 | end if; |
5596 | ||
5597 | Next (Selectr); | |
5598 | end loop; | |
5599 | ||
5600 | Next (Assoc); | |
5601 | end loop Verification; | |
5602 | end Step_7; | |
5603 | ||
5604 | -- STEP 8: replace the original aggregate | |
5605 | ||
5606 | Step_8 : declare | |
fbf5a39b | 5607 | New_Aggregate : constant Node_Id := New_Copy (N); |
996ae0b0 RK |
5608 | |
5609 | begin | |
5610 | Set_Expressions (New_Aggregate, No_List); | |
5611 | Set_Etype (New_Aggregate, Etype (N)); | |
5612 | Set_Component_Associations (New_Aggregate, New_Assoc_List); | |
288cbbbd | 5613 | Set_Check_Actuals (New_Aggregate, Check_Actuals (N)); |
996ae0b0 RK |
5614 | |
5615 | Rewrite (N, New_Aggregate); | |
5616 | end Step_8; | |
0929eaeb | 5617 | |
d976bf74 | 5618 | -- Check the dimensions of the components in the record aggregate |
0929eaeb AC |
5619 | |
5620 | Analyze_Dimension_Extension_Or_Record_Aggregate (N); | |
996ae0b0 RK |
5621 | end Resolve_Record_Aggregate; |
5622 | ||
2820d220 AC |
5623 | ----------------------------- |
5624 | -- Check_Can_Never_Be_Null -- | |
5625 | ----------------------------- | |
5626 | ||
9b96e234 | 5627 | procedure Check_Can_Never_Be_Null (Typ : Entity_Id; Expr : Node_Id) is |
ec53a6da JM |
5628 | Comp_Typ : Entity_Id; |
5629 | ||
2820d220 | 5630 | begin |
9b96e234 | 5631 | pragma Assert |
0791fbe9 | 5632 | (Ada_Version >= Ada_2005 |
9b96e234 | 5633 | and then Present (Expr) |
8133b9d1 | 5634 | and then Known_Null (Expr)); |
82c80734 | 5635 | |
ec53a6da JM |
5636 | case Ekind (Typ) is |
5637 | when E_Array_Type => | |
5638 | Comp_Typ := Component_Type (Typ); | |
5639 | ||
d8f43ee6 HK |
5640 | when E_Component |
5641 | | E_Discriminant | |
5642 | => | |
ec53a6da JM |
5643 | Comp_Typ := Etype (Typ); |
5644 | ||
5645 | when others => | |
5646 | return; | |
5647 | end case; | |
5648 | ||
9b96e234 JM |
5649 | if Can_Never_Be_Null (Comp_Typ) then |
5650 | ||
5651 | -- Here we know we have a constraint error. Note that we do not use | |
5652 | -- Apply_Compile_Time_Constraint_Error here to the Expr, which might | |
5653 | -- seem the more natural approach. That's because in some cases the | |
5654 | -- components are rewritten, and the replacement would be missed. | |
5a521b8a AC |
5655 | -- We do not mark the whole aggregate as raising a constraint error, |
5656 | -- because the association may be a null array range. | |
9b96e234 | 5657 | |
5a521b8a | 5658 | Error_Msg_N |
9ed2b86d | 5659 | ("(Ada 2005) NULL not allowed in null-excluding component??", Expr); |
5a521b8a | 5660 | Error_Msg_N |
b785e0b8 | 5661 | ("\Constraint_Error will be raised at run time??", Expr); |
9b96e234 | 5662 | |
5a521b8a AC |
5663 | Rewrite (Expr, |
5664 | Make_Raise_Constraint_Error | |
5665 | (Sloc (Expr), Reason => CE_Access_Check_Failed)); | |
9b96e234 JM |
5666 | Set_Etype (Expr, Comp_Typ); |
5667 | Set_Analyzed (Expr); | |
2820d220 AC |
5668 | end if; |
5669 | end Check_Can_Never_Be_Null; | |
5670 | ||
996ae0b0 RK |
5671 | --------------------- |
5672 | -- Sort_Case_Table -- | |
5673 | --------------------- | |
5674 | ||
5675 | procedure Sort_Case_Table (Case_Table : in out Case_Table_Type) is | |
fbf5a39b | 5676 | U : constant Int := Case_Table'Last; |
996ae0b0 RK |
5677 | K : Int; |
5678 | J : Int; | |
5679 | T : Case_Bounds; | |
5680 | ||
5681 | begin | |
82893775 AC |
5682 | K := 1; |
5683 | while K < U loop | |
996ae0b0 | 5684 | T := Case_Table (K + 1); |
996ae0b0 | 5685 | |
7f9747c6 | 5686 | J := K + 1; |
82893775 AC |
5687 | while J > 1 |
5688 | and then Expr_Value (Case_Table (J - 1).Lo) > Expr_Value (T.Lo) | |
996ae0b0 RK |
5689 | loop |
5690 | Case_Table (J) := Case_Table (J - 1); | |
5691 | J := J - 1; | |
5692 | end loop; | |
5693 | ||
5694 | Case_Table (J) := T; | |
5695 | K := K + 1; | |
5696 | end loop; | |
5697 | end Sort_Case_Table; | |
5698 | ||
5699 | end Sem_Aggr; |