comparison gcc/ada/exp_ch5.adb @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
4 -- -- 4 -- --
5 -- E X P _ C H 5 -- 5 -- E X P _ C H 5 --
6 -- -- 6 -- --
7 -- B o d y -- 7 -- B o d y --
8 -- -- 8 -- --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. -- 9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
10 -- -- 10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under -- 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- -- 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- -- 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- -- 14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
112 -- Rev indicates if the loops run normally (Rev = False), or reversed 112 -- Rev indicates if the loops run normally (Rev = False), or reversed
113 -- (Rev = True). The value returned is the constructed loop statement. 113 -- (Rev = True). The value returned is the constructed loop statement.
114 -- Auxiliary declarations are inserted before node N using the standard 114 -- Auxiliary declarations are inserted before node N using the standard
115 -- Insert_Actions mechanism. 115 -- Insert_Actions mechanism.
116 116
117 function Expand_Assign_Array_Bitfield
118 (N : Node_Id;
119 Larray : Entity_Id;
120 Rarray : Entity_Id;
121 L_Type : Entity_Id;
122 R_Type : Entity_Id;
123 Rev : Boolean) return Node_Id;
124 -- Alternative to Expand_Assign_Array_Loop for packed bitfields. Generates
125 -- a call to the System.Bitfields.Copy_Bitfield, which is more efficient
126 -- than copying component-by-component.
127
128 function Expand_Assign_Array_Loop_Or_Bitfield
129 (N : Node_Id;
130 Larray : Entity_Id;
131 Rarray : Entity_Id;
132 L_Type : Entity_Id;
133 R_Type : Entity_Id;
134 Ndim : Pos;
135 Rev : Boolean) return Node_Id;
136 -- Calls either Expand_Assign_Array_Loop or Expand_Assign_Array_Bitfield as
137 -- appropriate.
138
117 procedure Expand_Assign_Record (N : Node_Id); 139 procedure Expand_Assign_Record (N : Node_Id);
118 -- N is an assignment of an untagged record value. This routine handles 140 -- N is an assignment of an untagged record value. This routine handles
119 -- the case where the assignment must be made component by component, 141 -- the case where the assignment must be made component by component,
120 -- either because the target is not byte aligned, or there is a change 142 -- either because the target is not byte aligned, or there is a change
121 -- of representation, or when we have a tagged type with a representation 143 -- of representation, or when we have a tagged type with a representation
311 333
312 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice; 334 L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
313 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice; 335 R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
314 336
315 Crep : constant Boolean := Change_Of_Representation (N); 337 Crep : constant Boolean := Change_Of_Representation (N);
338
339 pragma Assert
340 (Crep
341 or else Is_Bit_Packed_Array (L_Type) = Is_Bit_Packed_Array (R_Type));
316 342
317 Larray : Node_Id; 343 Larray : Node_Id;
318 Rarray : Node_Id; 344 Rarray : Node_Id;
319 345
320 Ndim : constant Pos := Number_Dimensions (L_Type); 346 Ndim : constant Pos := Number_Dimensions (L_Type);
937 Parameter_Associations => Actuals)); 963 Parameter_Associations => Actuals));
938 end; 964 end;
939 965
940 else 966 else
941 Rewrite (N, 967 Rewrite (N,
942 Expand_Assign_Array_Loop 968 Expand_Assign_Array_Loop_Or_Bitfield
943 (N, Larray, Rarray, L_Type, R_Type, Ndim, 969 (N, Larray, Rarray, L_Type, R_Type, Ndim,
944 Rev => not Forwards_OK (N))); 970 Rev => not Forwards_OK (N)));
945 end if; 971 end if;
946 972
947 -- Case of both are false with No_Implicit_Conditionals 973 -- Case of both are false with No_Implicit_Conditionals
1037 Cright_Lo := 1063 Cright_Lo :=
1038 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo); 1064 Unchecked_Convert_To (Etype (Left_Lo), Cright_Lo);
1039 end if; 1065 end if;
1040 1066
1041 -- Reset the Analyzed flag, because the bounds of the index 1067 -- Reset the Analyzed flag, because the bounds of the index
1042 -- type itself may be universal, and must must be reanalyzed 1068 -- type itself may be universal, and must be reanalyzed to
1043 -- to acquire the proper type for the back end. 1069 -- acquire the proper type for the back end.
1044 1070
1045 Set_Analyzed (Cleft_Lo, False); 1071 Set_Analyzed (Cleft_Lo, False);
1046 Set_Analyzed (Cright_Lo, False); 1072 Set_Analyzed (Cright_Lo, False);
1047 1073
1048 Condition := 1074 Condition :=
1090 Rewrite (N, 1116 Rewrite (N,
1091 Make_Implicit_If_Statement (N, 1117 Make_Implicit_If_Statement (N,
1092 Condition => Condition, 1118 Condition => Condition,
1093 1119
1094 Then_Statements => New_List ( 1120 Then_Statements => New_List (
1095 Expand_Assign_Array_Loop 1121 Expand_Assign_Array_Loop_Or_Bitfield
1096 (N, Larray, Rarray, L_Type, R_Type, Ndim, 1122 (N, Larray, Rarray, L_Type, R_Type, Ndim,
1097 Rev => False)), 1123 Rev => False)),
1098 1124
1099 Else_Statements => New_List ( 1125 Else_Statements => New_List (
1100 Expand_Assign_Array_Loop 1126 Expand_Assign_Array_Loop_Or_Bitfield
1101 (N, Larray, Rarray, L_Type, R_Type, Ndim, 1127 (N, Larray, Rarray, L_Type, R_Type, Ndim,
1102 Rev => True)))); 1128 Rev => True))));
1103 end if; 1129 end if;
1104 end if; 1130 end if;
1105 1131
1317 Statements => New_List (Assign, Build_Step (J)))))); 1343 Statements => New_List (Assign, Build_Step (J))))));
1318 end loop; 1344 end loop;
1319 1345
1320 return Assign; 1346 return Assign;
1321 end Expand_Assign_Array_Loop; 1347 end Expand_Assign_Array_Loop;
1348
1349 ----------------------------------
1350 -- Expand_Assign_Array_Bitfield --
1351 ----------------------------------
1352
1353 function Expand_Assign_Array_Bitfield
1354 (N : Node_Id;
1355 Larray : Entity_Id;
1356 Rarray : Entity_Id;
1357 L_Type : Entity_Id;
1358 R_Type : Entity_Id;
1359 Rev : Boolean) return Node_Id
1360 is
1361 pragma Assert (not Rev);
1362 -- Reverse copying is not yet supported by Copy_Bitfield.
1363
1364 pragma Assert (not Change_Of_Representation (N));
1365 -- This won't work, for example, to copy a packed array to an unpacked
1366 -- array.
1367
1368 Loc : constant Source_Ptr := Sloc (N);
1369
1370 L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
1371 R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
1372 Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
1373 Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
1374
1375 L_Addr : constant Node_Id :=
1376 Make_Attribute_Reference (Loc,
1377 Prefix =>
1378 Make_Indexed_Component (Loc,
1379 Prefix =>
1380 Duplicate_Subexpr (Larray, True),
1381 Expressions => New_List (New_Copy_Tree (Left_Lo))),
1382 Attribute_Name => Name_Address);
1383
1384 L_Bit : constant Node_Id :=
1385 Make_Attribute_Reference (Loc,
1386 Prefix =>
1387 Make_Indexed_Component (Loc,
1388 Prefix =>
1389 Duplicate_Subexpr (Larray, True),
1390 Expressions => New_List (New_Copy_Tree (Left_Lo))),
1391 Attribute_Name => Name_Bit);
1392
1393 R_Addr : constant Node_Id :=
1394 Make_Attribute_Reference (Loc,
1395 Prefix =>
1396 Make_Indexed_Component (Loc,
1397 Prefix =>
1398 Duplicate_Subexpr (Rarray, True),
1399 Expressions => New_List (New_Copy_Tree (Right_Lo))),
1400 Attribute_Name => Name_Address);
1401
1402 R_Bit : constant Node_Id :=
1403 Make_Attribute_Reference (Loc,
1404 Prefix =>
1405 Make_Indexed_Component (Loc,
1406 Prefix =>
1407 Duplicate_Subexpr (Rarray, True),
1408 Expressions => New_List (New_Copy_Tree (Right_Lo))),
1409 Attribute_Name => Name_Bit);
1410
1411 -- Compute the Size of the bitfield
1412
1413 -- Note that the length check has already been done, so we can use the
1414 -- size of either L or R; they are equal. We can't use 'Size here,
1415 -- because sometimes bit fields get copied into a temp, and the 'Size
1416 -- ends up being the size of the temp (e.g. an 8-bit temp containing
1417 -- a 4-bit bit field).
1418
1419 Size : constant Node_Id :=
1420 Make_Op_Multiply (Loc,
1421 Make_Attribute_Reference (Loc,
1422 Prefix =>
1423 Duplicate_Subexpr (Name (N), True),
1424 Attribute_Name => Name_Length),
1425 Make_Attribute_Reference (Loc,
1426 Prefix =>
1427 Duplicate_Subexpr (Name (N), True),
1428 Attribute_Name => Name_Component_Size));
1429
1430 begin
1431 return Make_Procedure_Call_Statement (Loc,
1432 Name => New_Occurrence_Of (RTE (RE_Copy_Bitfield), Loc),
1433 Parameter_Associations => New_List (
1434 R_Addr, R_Bit, L_Addr, L_Bit, Size));
1435 end Expand_Assign_Array_Bitfield;
1436
1437 ------------------------------------------
1438 -- Expand_Assign_Array_Loop_Or_Bitfield --
1439 ------------------------------------------
1440
1441 function Expand_Assign_Array_Loop_Or_Bitfield
1442 (N : Node_Id;
1443 Larray : Entity_Id;
1444 Rarray : Entity_Id;
1445 L_Type : Entity_Id;
1446 R_Type : Entity_Id;
1447 Ndim : Pos;
1448 Rev : Boolean) return Node_Id
1449 is
1450 Slices : constant Boolean :=
1451 Nkind (Name (N)) = N_Slice or else Nkind (Expression (N)) = N_Slice;
1452 L_Prefix_Comp : constant Boolean :=
1453 -- True if the left-hand side is a slice of a component or slice
1454 Nkind (Name (N)) = N_Slice
1455 and then Nkind_In (Prefix (Name (N)),
1456 N_Selected_Component,
1457 N_Indexed_Component,
1458 N_Slice);
1459 R_Prefix_Comp : constant Boolean :=
1460 -- Likewise for the right-hand side
1461 Nkind (Expression (N)) = N_Slice
1462 and then Nkind_In (Prefix (Expression (N)),
1463 N_Selected_Component,
1464 N_Indexed_Component,
1465 N_Slice);
1466 begin
1467 -- Determine whether Copy_Bitfield is appropriate (will work, and will
1468 -- be more efficient than component-by-component copy). Copy_Bitfield
1469 -- doesn't work for reversed storage orders. It is efficient for slices
1470 -- of bit-packed arrays. Copy_Bitfield can read and write bits that are
1471 -- not part of the objects being copied, so we don't want to use it if
1472 -- there are volatile or independent components. If the Prefix of the
1473 -- slice is a component or slice, then it might be a part of an object
1474 -- with some other volatile or independent components, so we disable the
1475 -- optimization in that case as well. We could complicate this code by
1476 -- actually looking for such volatile and independent components.
1477
1478 if Is_Bit_Packed_Array (L_Type)
1479 and then Is_Bit_Packed_Array (R_Type)
1480 and then not Reverse_Storage_Order (L_Type)
1481 and then not Reverse_Storage_Order (R_Type)
1482 and then Ndim = 1
1483 and then not Rev
1484 and then Slices
1485 and then not Has_Volatile_Component (L_Type)
1486 and then not Has_Volatile_Component (R_Type)
1487 and then not Has_Independent_Components (L_Type)
1488 and then not Has_Independent_Components (R_Type)
1489 and then not L_Prefix_Comp
1490 and then not R_Prefix_Comp
1491 and then RTE_Available (RE_Copy_Bitfield)
1492 then
1493 return Expand_Assign_Array_Bitfield
1494 (N, Larray, Rarray, L_Type, R_Type, Rev);
1495 else
1496 return Expand_Assign_Array_Loop
1497 (N, Larray, Rarray, L_Type, R_Type, Ndim, Rev);
1498 end if;
1499 end Expand_Assign_Array_Loop_Or_Bitfield;
1322 1500
1323 -------------------------- 1501 --------------------------
1324 -- Expand_Assign_Record -- 1502 -- Expand_Assign_Record --
1325 -------------------------- 1503 --------------------------
1326 1504
2019 2197
2020 -- Deal with assignment checks unless suppressed 2198 -- Deal with assignment checks unless suppressed
2021 2199
2022 if not Suppress_Assignment_Checks (N) then 2200 if not Suppress_Assignment_Checks (N) then
2023 2201
2024 -- First deal with generation of range check if required 2202 -- First deal with generation of range check if required,
2025 2203 -- and then predicate checks if the type carries a predicate.
2026 if Do_Range_Check (Rhs) then 2204 -- If the Rhs is an expression these tests may have been applied
2027 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed); 2205 -- already. This is the case if the RHS is a type conversion.
2206 -- Other such redundant checks could be removed ???
2207
2208 if Nkind (Rhs) /= N_Type_Conversion
2209 or else Entity (Subtype_Mark (Rhs)) /= Typ
2210 then
2211 if Do_Range_Check (Rhs) then
2212 Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
2213 end if;
2214
2215 Apply_Predicate_Check (Rhs, Typ);
2028 end if; 2216 end if;
2029
2030 -- Then generate predicate check if required
2031
2032 Apply_Predicate_Check (Rhs, Typ);
2033 end if; 2217 end if;
2034 2218
2035 -- Check for a special case where a high level transformation is 2219 -- Check for a special case where a high level transformation is
2036 -- required. If we have either of: 2220 -- required. If we have either of:
2037 2221
2223 -- have a full view with discriminants, but those are nameable only 2407 -- have a full view with discriminants, but those are nameable only
2224 -- in the underlying type, so convert the Rhs to it before potential 2408 -- in the underlying type, so convert the Rhs to it before potential
2225 -- checking. Convert Lhs as well, otherwise the actual subtype might 2409 -- checking. Convert Lhs as well, otherwise the actual subtype might
2226 -- not be constructible. If the discriminants have defaults the type 2410 -- not be constructible. If the discriminants have defaults the type
2227 -- is unconstrained and there is nothing to check. 2411 -- is unconstrained and there is nothing to check.
2228 2412 -- Ditto if a private type with unknown discriminants has a full view
2229 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs))) 2413 -- that is an unconstrained array, in which case a length check is
2230 and then Has_Discriminants (Typ) 2414 -- needed.
2231 and then not Has_Defaulted_Discriminants (Typ) 2415
2232 then 2416 elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs))) then
2233 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs)); 2417 if Has_Discriminants (Typ)
2234 Rewrite (Lhs, OK_Convert_To (Base_Type (Typ), Lhs)); 2418 and then not Has_Defaulted_Discriminants (Typ)
2235 Apply_Discriminant_Check (Rhs, Typ, Lhs); 2419 then
2420 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
2421 Rewrite (Lhs, OK_Convert_To (Base_Type (Typ), Lhs));
2422 Apply_Discriminant_Check (Rhs, Typ, Lhs);
2423
2424 elsif Is_Array_Type (Typ) and then Is_Constrained (Typ) then
2425 Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
2426 Rewrite (Lhs, OK_Convert_To (Base_Type (Typ), Lhs));
2427 Apply_Length_Check (Rhs, Typ);
2428 end if;
2236 2429
2237 -- In the access type case, we need the same discriminant check, and 2430 -- In the access type case, we need the same discriminant check, and
2238 -- also range checks if we have an access to constrained array. 2431 -- also range checks if we have an access to constrained array.
2239 2432
2240 elsif Is_Access_Type (Etype (Lhs)) 2433 elsif Is_Access_Type (Etype (Lhs))
2848 ----------------------------- 3041 -----------------------------
2849 -- Expand_N_Case_Statement -- 3042 -- Expand_N_Case_Statement --
2850 ----------------------------- 3043 -----------------------------
2851 3044
2852 procedure Expand_N_Case_Statement (N : Node_Id) is 3045 procedure Expand_N_Case_Statement (N : Node_Id) is
2853 Loc : constant Source_Ptr := Sloc (N); 3046 Loc : constant Source_Ptr := Sloc (N);
2854 Expr : constant Node_Id := Expression (N); 3047 Expr : constant Node_Id := Expression (N);
2855 Alt : Node_Id; 3048 From_Cond_Expr : constant Boolean := From_Conditional_Expression (N);
2856 Len : Nat; 3049 Alt : Node_Id;
2857 Cond : Node_Id; 3050 Len : Nat;
2858 Choice : Node_Id; 3051 Cond : Node_Id;
2859 Chlist : List_Id; 3052 Choice : Node_Id;
3053 Chlist : List_Id;
2860 3054
2861 begin 3055 begin
2862 -- Check for the situation where we know at compile time which branch 3056 -- Check for the situation where we know at compile time which branch
2863 -- will be taken. 3057 -- will be taken.
2864 3058
3065 Rewrite (N, 3259 Rewrite (N,
3066 Make_If_Statement (Loc, 3260 Make_If_Statement (Loc,
3067 Condition => Cond, 3261 Condition => Cond,
3068 Then_Statements => Then_Stms, 3262 Then_Statements => Then_Stms,
3069 Else_Statements => Else_Stms)); 3263 Else_Statements => Else_Stms));
3264
3265 -- The rewritten if statement needs to inherit whether the
3266 -- case statement was expanded from a conditional expression,
3267 -- for proper handling of nested controlled objects.
3268
3269 Set_From_Conditional_Expression (N, From_Cond_Expr);
3270
3070 Analyze (N); 3271 Analyze (N);
3272
3071 return; 3273 return;
3072 end if; 3274 end if;
3073 end if; 3275 end if;
3074 3276
3075 -- If the last alternative is not an Others choice, replace it with 3277 -- If the last alternative is not an Others choice, replace it with
3302 New_List 3504 New_List
3303 (Make_Block_Statement (Loc, 3505 (Make_Block_Statement (Loc,
3304 Declarations => New_List (Elmt_Decl), 3506 Declarations => New_List (Elmt_Decl),
3305 Handled_Statement_Sequence => 3507 Handled_Statement_Sequence =>
3306 Make_Handled_Sequence_Of_Statements (Loc, 3508 Make_Handled_Sequence_Of_Statements (Loc,
3307 Statements => Stats)))); 3509 Statements => Stats))));
3308 3510
3309 else 3511 else
3310 Elmt_Ref := 3512 Elmt_Ref :=
3311 Make_Assignment_Statement (Loc, 3513 Make_Assignment_Statement (Loc,
3312 Name => New_Occurrence_Of (Element, Loc), 3514 Name => New_Occurrence_Of (Element, Loc),
3328 New_Loop := 3530 New_Loop :=
3329 Make_Block_Statement (Loc, 3531 Make_Block_Statement (Loc,
3330 Declarations => New_List (Elmt_Decl), 3532 Declarations => New_List (Elmt_Decl),
3331 Handled_Statement_Sequence => 3533 Handled_Statement_Sequence =>
3332 Make_Handled_Sequence_Of_Statements (Loc, 3534 Make_Handled_Sequence_Of_Statements (Loc,
3333 Statements => New_List (New_Loop))); 3535 Statements => New_List (New_Loop)));
3334 end if; 3536 end if;
3335 3537
3336 -- The element is only modified in expanded code, so it appears as 3538 -- The element is only modified in expanded code, so it appears as
3337 -- unassigned to the warning machinery. We must suppress this spurious 3539 -- unassigned to the warning machinery. We must suppress this spurious
3338 -- warning explicitly. 3540 -- warning explicitly.
3917 -- Iter : Reversible_Iterator'Class := Iterate (My_Vector); 4119 -- Iter : Reversible_Iterator'Class := Iterate (My_Vector);
3918 -- -- Reversible_Iterator is an interface. Iterate is the 4120 -- -- Reversible_Iterator is an interface. Iterate is the
3919 -- -- Default_Iterator aspect of Vector. This increments Lock, 4121 -- -- Default_Iterator aspect of Vector. This increments Lock,
3920 -- -- disallowing tampering with cursors. Unfortunately, it does not 4122 -- -- disallowing tampering with cursors. Unfortunately, it does not
3921 -- -- increment Busy. The result of Iterate is Limited_Controlled; 4123 -- -- increment Busy. The result of Iterate is Limited_Controlled;
3922 -- -- finalization will decrement Lock. This is a build-in-place 4124 -- -- finalization will decrement Lock. This is a build-in-place
3923 -- -- dispatching call to Iterate. 4125 -- -- dispatching call to Iterate.
3924 4126
3925 -- Cur : Cursor := First (Iter); -- or Last 4127 -- Cur : Cursor := First (Iter); -- or Last
3926 -- -- Dispatching call via interface. 4128 -- -- Dispatching call via interface.
3927 4129