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