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