Debian Codegraph @

gcc/ada/par-ch4.adb

raw
1 -----------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
29
30 with Stringt; use Stringt;
31
32 separate (Par)
33 package body Ch4 is
34
35 -- Attributes that cannot have arguments
36
37 Is_Parameterless_Attribute : constant Attribute_Set :=
38 (Attribute_Base |
39 Attribute_Body_Version |
40 Attribute_Class |
41 Attribute_External_Tag |
42 Attribute_Img |
43 Attribute_Loop_Entry |
44 Attribute_Old |
45 Attribute_Result |
46 Attribute_Stub_Type |
47 Attribute_Version |
48 Attribute_Type_Key => True,
49 others => False);
50 -- This map contains True for parameterless attributes that return a string
51 -- or a type. For those attributes, a left parenthesis after the attribute
52 -- should not be analyzed as the beginning of a parameters list because it
53 -- may denote a slice operation (X'Img (1 .. 2)) or a type conversion
54 -- (X'Class (Y)).
55
56 -- Note: Loop_Entry is in this list because, although it can take an
57 -- optional argument (the loop name), we can't distinguish that at parse
58 -- time from the case where no loop name is given and a legitimate index
59 -- expression is present. So we parse the argument as an indexed component
60 -- and the semantic analysis sorts out this syntactic ambiguity based on
61 -- the type and form of the expression.
62
63 -- Note that this map designates the minimum set of attributes where a
64 -- construct in parentheses that is not an argument can appear right
65 -- after the attribute. For attributes like 'Size, we do not put them
66 -- in the map. If someone writes X'Size (3), that's illegal in any case,
67 -- but we get a better error message by parsing the (3) as an illegal
68 -- argument to the attribute, rather than some meaningless junk that
69 -- follows the attribute.
70
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
74
75 function P_Aggregate_Or_Paren_Expr return Node_Id;
76 function P_Allocator return Node_Id;
77 function P_Case_Expression_Alternative return Node_Id;
78 function P_Iterated_Component_Association return Node_Id;
79 function P_Record_Or_Array_Component_Association return Node_Id;
80 function P_Factor return Node_Id;
81 function P_Primary return Node_Id;
82 function P_Relation return Node_Id;
83 function P_Term return Node_Id;
84 function P_Declare_Expression return Node_Id;
85 function P_Reduction_Attribute_Reference (S : Node_Id)
86 return Node_Id;
87
88 function P_Binary_Adding_Operator return Node_Kind;
89 function P_Logical_Operator return Node_Kind;
90 function P_Multiplying_Operator return Node_Kind;
91 function P_Relational_Operator return Node_Kind;
92 function P_Unary_Adding_Operator return Node_Kind;
93
94 procedure Bad_Range_Attribute (Loc : Source_Ptr);
95 -- Called to place complaint about bad range attribute at the given
96 -- source location. Terminates by raising Error_Resync.
97
98 procedure Check_Bad_Exp;
99 -- Called after scanning a**b, posts error if ** detected
100
101 procedure P_Membership_Test (N : Node_Id);
102 -- N is the node for a N_In or N_Not_In node whose right operand has not
103 -- yet been processed. It is called just after scanning out the IN keyword.
104 -- On return, either Right_Opnd or Alternatives is set, as appropriate.
105
106 function P_Range_Attribute_Reference (Prefix_Node : Node_Id) return Node_Id;
107 -- Scan a range attribute reference. The caller has scanned out the
108 -- prefix. The current token is known to be an apostrophe and the
109 -- following token is known to be RANGE.
110
111 function P_Case_Expression return Node_Id;
112 -- Scans out a case expression. Called with Token pointing to the CASE
113 -- keyword, and returns pointing to the terminating right parent,
114 -- semicolon, or comma, but does not consume this terminating token.
115
116 function P_Unparen_Cond_Expr_Etc return Node_Id;
117 -- This function is called with Token pointing to IF, CASE, FOR, or
118 -- DECLARE, in a context that allows a conditional (if or case) expression,
119 -- a quantified expression, an iterated component association, or a declare
120 -- expression, if it is surrounded by parentheses. If not surrounded by
121 -- parentheses, the expression is still returned, but an error message is
122 -- issued.
123
124 -------------------------
125 -- Bad_Range_Attribute --
126 -------------------------
127
128 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
129 begin
130 Error_Msg ("range attribute cannot be used in expression!", Loc);
131 Resync_Expression;
132 end Bad_Range_Attribute;
133
134 -------------------
135 -- Check_Bad_Exp --
136 -------------------
137
138 procedure Check_Bad_Exp is
139 begin
140 if Token = Tok_Double_Asterisk then
141 Error_Msg_SC ("parenthesization required for '*'*");
142 Scan; -- past **
143 Discard_Junk_Node (P_Primary);
144 Check_Bad_Exp;
145 end if;
146 end Check_Bad_Exp;
147
148 --------------------------
149 -- 4.1 Name (also 6.4) --
150 --------------------------
151
152 -- NAME ::=
153 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
154 -- | INDEXED_COMPONENT | SLICE
155 -- | SELECTED_COMPONENT | ATTRIBUTE
156 -- | TYPE_CONVERSION | FUNCTION_CALL
157 -- | CHARACTER_LITERAL | TARGET_NAME
158
159 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
160
161 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
162
163 -- EXPLICIT_DEREFERENCE ::= NAME . all
164
165 -- IMPLICIT_DEREFERENCE ::= NAME
166
167 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
168
169 -- SLICE ::= PREFIX (DISCRETE_RANGE)
170
171 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
172
173 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
174
175 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
176
177 -- ATTRIBUTE_DESIGNATOR ::=
178 -- IDENTIFIER [(static_EXPRESSION)]
179 -- | access | delta | digits
180
181 -- FUNCTION_CALL ::=
182 -- function_NAME
183 -- | function_PREFIX ACTUAL_PARAMETER_PART
184
185 -- ACTUAL_PARAMETER_PART ::=
186 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
187
188 -- PARAMETER_ASSOCIATION ::=
189 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
190
191 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
192
193 -- TARGET_NAME ::= @ (AI12-0125-3: abbreviation for LHS)
194
195 -- Note: syntactically a procedure call looks just like a function call,
196 -- so this routine is in practice used to scan out procedure calls as well.
197
198 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
199
200 -- Error recovery: can raise Error_Resync
201
202 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
203 -- followed by either a left paren (qualified expression case), or by
204 -- range (range attribute case). All other uses of apostrophe (i.e. all
205 -- other attributes) are handled in this routine.
206
207 -- Error recovery: can raise Error_Resync
208
209 function P_Name return Node_Id is
210 Scan_State : Saved_Scan_State;
211 Name_Node : Node_Id;
212 Prefix_Node : Node_Id;
213 Ident_Node : Node_Id;
214 Expr_Node : Node_Id;
215 Range_Node : Node_Id;
216 Arg_Node : Node_Id;
217
218 Arg_List : List_Id := No_List; -- kill junk warning
219 Attr_Name : Name_Id := No_Name; -- kill junk warning
220
221 Error_Loc : Source_Ptr;
222
223 begin
224 -- Case of not a name
225
226 if Token not in Token_Class_Name then
227
228 -- If it looks like start of expression, complain and scan expression
229
230 if Token in Token_Class_Literal | Tok_Left_Paren then
231 Error_Msg_SC ("name expected");
232 return P_Expression;
233
234 -- Otherwise some other junk, not much we can do
235
236 else
237 Error_Msg_AP ("name expected");
238 raise Error_Resync;
239 end if;
240 end if;
241
242 -- Loop through designators in qualified name
243 -- AI12-0125 : target_name
244
245 if Token = Tok_At_Sign then
246 Scan_Reserved_Identifier (Force_Msg => False);
247
248 if Present (Current_Assign_Node) then
249 Set_Has_Target_Names (Current_Assign_Node);
250 end if;
251 end if;
252
253 Name_Node := Token_Node;
254
255 loop
256 Scan; -- past designator
257 exit when Token /= Tok_Dot;
258 Save_Scan_State (Scan_State); -- at dot
259 Scan; -- past dot
260
261 -- If we do not have another designator after the dot, then join
262 -- the normal circuit to handle a dot extension (may be .all or
263 -- character literal case). Otherwise loop back to scan the next
264 -- designator.
265
266 if Token not in Token_Class_Desig then
267 goto Scan_Name_Extension_Dot;
268 else
269 Prefix_Node := Name_Node;
270 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
271 Set_Prefix (Name_Node, Prefix_Node);
272 Set_Selector_Name (Name_Node, Token_Node);
273 end if;
274 end loop;
275
276 -- We have now scanned out a qualified designator. If the last token is
277 -- an operator symbol, then we certainly do not have the Snam case, so
278 -- we can just use the normal name extension check circuit
279
280 if Prev_Token = Tok_Operator_Symbol then
281 goto Scan_Name_Extension;
282 end if;
283
284 -- We have scanned out a qualified simple name, check for name
285 -- extension. Note that we know there is no dot here at this stage,
286 -- so the only possible cases of name extension are apostrophe followed
287 -- by '(' or '['.
288
289 if Token = Tok_Apostrophe then
290 Save_Scan_State (Scan_State); -- at apostrophe
291 Scan; -- past apostrophe
292
293 -- Qualified expression in Ada 2012 mode (treated as a name)
294
295 if Ada_Version >= Ada_2012
296 and then Token in Tok_Left_Paren | Tok_Left_Bracket
297 then
298 goto Scan_Name_Extension_Apostrophe;
299
300 -- If left paren not in Ada 2012, then it is not part of the name,
301 -- since qualified expressions are not names in prior versions of
302 -- Ada, so return with Token backed up to point to the apostrophe.
303 -- The treatment for the range attribute is similar (we do not
304 -- consider x'range to be a name in this grammar).
305
306 elsif Token in Tok_Left_Paren | Tok_Range then
307 Restore_Scan_State (Scan_State); -- to apostrophe
308 Expr_Form := EF_Simple_Name;
309 return Name_Node;
310
311 -- Otherwise we have the case of a name extended by an attribute
312
313 else
314 goto Scan_Name_Extension_Apostrophe;
315 end if;
316
317 -- Check case of qualified simple name extended by a left parenthesis
318
319 elsif Token = Tok_Left_Paren then
320 Scan; -- past left paren
321 goto Scan_Name_Extension_Left_Paren;
322
323 -- Otherwise the qualified simple name is not extended, so return
324
325 else
326 Expr_Form := EF_Simple_Name;
327 return Name_Node;
328 end if;
329
330 -- Loop scanning past name extensions. A label is used for control
331 -- transfer for this loop for ease of interfacing with the finite state
332 -- machine in the parenthesis scanning circuit, and also to allow for
333 -- passing in control to the appropriate point from the above code.
334
335 <<Scan_Name_Extension>>
336
337 -- Character literal used as name cannot be extended. Also this
338 -- cannot be a call, since the name for a call must be a designator.
339 -- Return in these cases, or if there is no name extension
340
341 if Token not in Token_Class_Namext
342 or else Prev_Token = Tok_Char_Literal
343 then
344 Expr_Form := EF_Name;
345 return Name_Node;
346 end if;
347
348 -- Merge here when we know there is a name extension
349
350 <<Scan_Name_Extension_OK>>
351
352 case Token is
353 when Tok_Left_Paren =>
354 Scan; -- past left paren
355 goto Scan_Name_Extension_Left_Paren;
356
357 when Tok_Apostrophe =>
358 Save_Scan_State (Scan_State); -- at apostrophe
359 Scan; -- past apostrophe
360 goto Scan_Name_Extension_Apostrophe;
361
362 when Tok_Dot =>
363 Save_Scan_State (Scan_State); -- at dot
364 Scan; -- past dot
365 goto Scan_Name_Extension_Dot;
366
367 when others => raise Program_Error;
368 end case;
369
370 -- Case of name extended by dot (selection), dot is already skipped
371 -- and the scan state at the point of the dot is saved in Scan_State.
372
373 <<Scan_Name_Extension_Dot>>
374
375 -- Explicit dereference case
376
377 if Token = Tok_All then
378 Prefix_Node := Name_Node;
379 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
380 Set_Prefix (Name_Node, Prefix_Node);
381 Scan; -- past ALL
382 goto Scan_Name_Extension;
383
384 -- Selected component case
385
386 elsif Token in Token_Class_Name then
387 Prefix_Node := Name_Node;
388 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
389 Set_Prefix (Name_Node, Prefix_Node);
390 Set_Selector_Name (Name_Node, Token_Node);
391 Scan; -- past selector
392 goto Scan_Name_Extension;
393
394 -- Reserved identifier as selector
395
396 elsif Is_Reserved_Identifier then
397 Scan_Reserved_Identifier (Force_Msg => False);
398 Prefix_Node := Name_Node;
399 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
400 Set_Prefix (Name_Node, Prefix_Node);
401 Set_Selector_Name (Name_Node, Token_Node);
402 Scan; -- past identifier used as selector
403 goto Scan_Name_Extension;
404
405 -- If dot is at end of line and followed by nothing legal,
406 -- then assume end of name and quit (dot will be taken as
407 -- an incorrect form of some other punctuation by our caller).
408
409 elsif Token_Is_At_Start_Of_Line then
410 Restore_Scan_State (Scan_State);
411 return Name_Node;
412
413 -- Here if nothing legal after the dot
414
415 else
416 Error_Msg_AP ("selector expected");
417 raise Error_Resync;
418 end if;
419
420 -- Here for an apostrophe as name extension. The scan position at the
421 -- apostrophe has already been saved, and the apostrophe scanned out.
422
423 <<Scan_Name_Extension_Apostrophe>>
424
425 Scan_Apostrophe : declare
426 function Apostrophe_Should_Be_Semicolon return Boolean;
427 -- Checks for case where apostrophe should probably be
428 -- a semicolon, and if so, gives appropriate message,
429 -- resets the scan pointer to the apostrophe, changes
430 -- the current token to Tok_Semicolon, and returns True.
431 -- Otherwise returns False.
432
433 ------------------------------------
434 -- Apostrophe_Should_Be_Semicolon --
435 ------------------------------------
436
437 function Apostrophe_Should_Be_Semicolon return Boolean is
438 begin
439 if Token_Is_At_Start_Of_Line then
440 Restore_Scan_State (Scan_State); -- to apostrophe
441 Error_Msg_SC ("|""''"" should be "";""");
442 Token := Tok_Semicolon;
443 return True;
444 else
445 return False;
446 end if;
447 end Apostrophe_Should_Be_Semicolon;
448
449 -- Start of processing for Scan_Apostrophe
450
451 begin
452 -- Check for qualified expression case in Ada 2012 mode
453
454 if Ada_Version >= Ada_2012
455 and then Token in Tok_Left_Paren | Tok_Left_Bracket
456 then
457 Name_Node := P_Qualified_Expression (Name_Node);
458 goto Scan_Name_Extension;
459
460 -- If range attribute after apostrophe, then return with Token
461 -- pointing to the apostrophe. Note that in this case the prefix
462 -- need not be a simple name (cases like A.all'range). Similarly
463 -- if there is a left paren after the apostrophe, then we also
464 -- return with Token pointing to the apostrophe (this is the
465 -- aggregate case, or some error case).
466
467 elsif Token in Tok_Range | Tok_Left_Paren then
468 Restore_Scan_State (Scan_State); -- to apostrophe
469 Expr_Form := EF_Name;
470 return Name_Node;
471
472 -- Here for cases where attribute designator is an identifier
473
474 elsif Token = Tok_Identifier then
475 Attr_Name := Token_Name;
476
477 if not Is_Attribute_Name (Attr_Name) then
478 if Apostrophe_Should_Be_Semicolon then
479 Expr_Form := EF_Name;
480 return Name_Node;
481
482 -- Here for a bad attribute name
483
484 else
485 Signal_Bad_Attribute;
486 Scan; -- past bad identifier
487
488 if Token = Tok_Left_Paren then
489 Scan; -- past left paren
490
491 loop
492 Discard_Junk_Node (P_Expression_If_OK);
493 exit when not Comma_Present;
494 end loop;
495
496 T_Right_Paren;
497 end if;
498
499 return Error;
500 end if;
501 end if;
502
503 if Style_Check then
504 Style.Check_Attribute_Name (False);
505 end if;
506
507 -- Here for case of attribute designator is not an identifier
508
509 else
510 if Token = Tok_Delta then
511 Attr_Name := Name_Delta;
512
513 elsif Token = Tok_Digits then
514 Attr_Name := Name_Digits;
515
516 elsif Token = Tok_Access then
517 Attr_Name := Name_Access;
518
519 elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
520 Attr_Name := Name_Mod;
521
522 elsif Apostrophe_Should_Be_Semicolon then
523 Expr_Form := EF_Name;
524 return Name_Node;
525
526 else
527 Error_Msg_AP ("attribute designator expected");
528 raise Error_Resync;
529 end if;
530
531 if Style_Check then
532 Style.Check_Attribute_Name (True);
533 end if;
534 end if;
535
536 -- We come here with an OK attribute scanned, and corresponding
537 -- Attribute identifier node stored in Ident_Node.
538
539 Prefix_Node := Name_Node;
540 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
541 Scan; -- past attribute designator
542 Set_Prefix (Name_Node, Prefix_Node);
543 Set_Attribute_Name (Name_Node, Attr_Name);
544
545 -- Scan attribute arguments/designator. We skip this if we know
546 -- that the attribute cannot have an argument (see documentation
547 -- of Is_Parameterless_Attribute for further details).
548
549 if Token = Tok_Left_Paren
550 and then not
551 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
552 then
553 -- Attribute Update contains an array or record association
554 -- list which provides new values for various components or
555 -- elements. The list is parsed as an aggregate, and we get
556 -- better error handling by knowing that in the parser.
557
558 if Attr_Name = Name_Update then
559 Set_Expressions (Name_Node, New_List);
560 Append (P_Aggregate, Expressions (Name_Node));
561
562 -- All other cases of parsing attribute arguments
563
564 else
565 Set_Expressions (Name_Node, New_List);
566 Scan; -- past left paren
567
568 loop
569 declare
570 Expr : constant Node_Id := P_Expression_If_OK;
571 Rnam : Node_Id;
572
573 begin
574 -- Case of => for named notation
575
576 if Token = Tok_Arrow then
577
578 -- Named notation allowed only for the special
579 -- case of System'Restriction_Set (No_Dependence =>
580 -- unit_NAME), in which case construct a parameter
581 -- assocation node and append to the arguments.
582
583 if Attr_Name = Name_Restriction_Set
584 and then Nkind (Expr) = N_Identifier
585 and then Chars (Expr) = Name_No_Dependence
586 then
587 Scan; -- past arrow
588 Rnam := P_Name;
589 Append_To (Expressions (Name_Node),
590 Make_Parameter_Association (Sloc (Rnam),
591 Selector_Name => Expr,
592 Explicit_Actual_Parameter => Rnam));
593 exit;
594
595 -- For all other cases named notation is illegal
596
597 else
598 Error_Msg_SC
599 ("named parameters not permitted "
600 & "for attributes");
601 Scan; -- past junk arrow
602 end if;
603
604 -- Here for normal case (not => for named parameter)
605
606 else
607 -- Special handling for 'Image in Ada 2012, where
608 -- the attribute can be parameterless and its value
609 -- can be the prefix of a slice. Rewrite name as a
610 -- slice, Expr is its low bound.
611
612 if Token = Tok_Dot_Dot
613 and then Attr_Name = Name_Image
614 and then Ada_Version >= Ada_2012
615 then
616 Set_Expressions (Name_Node, No_List);
617 Prefix_Node := Name_Node;
618 Name_Node :=
619 New_Node (N_Slice, Sloc (Prefix_Node));
620 Set_Prefix (Name_Node, Prefix_Node);
621 Range_Node := New_Node (N_Range, Token_Ptr);
622 Set_Low_Bound (Range_Node, Expr);
623 Scan; -- past ..
624 Expr_Node := P_Expression;
625 Check_Simple_Expression (Expr_Node);
626 Set_High_Bound (Range_Node, Expr_Node);
627 Set_Discrete_Range (Name_Node, Range_Node);
628 T_Right_Paren;
629
630 goto Scan_Name_Extension;
631
632 else
633 Append (Expr, Expressions (Name_Node));
634 exit when not Comma_Present;
635 end if;
636 end if;
637 end;
638 end loop;
639
640 T_Right_Paren;
641 end if;
642 end if;
643
644 goto Scan_Name_Extension;
645 end Scan_Apostrophe;
646
647 -- Here for left parenthesis extending name (left paren skipped)
648
649 <<Scan_Name_Extension_Left_Paren>>
650
651 -- We now have to scan through a list of items, terminated by a
652 -- right parenthesis. The scan is handled by a finite state
653 -- machine. The possibilities are:
654
655 -- (discrete_range)
656
657 -- This is a slice. This case is handled in LP_State_Init
658
659 -- (expression, expression, ..)
660
661 -- This is interpreted as an indexed component, i.e. as a
662 -- case of a name which can be extended in the normal manner.
663 -- This case is handled by LP_State_Name or LP_State_Expr.
664
665 -- Note: if and case expressions (without an extra level of
666 -- parentheses) are permitted in this context).
667
668 -- (..., identifier => expression , ...)
669
670 -- If there is at least one occurrence of identifier => (but
671 -- none of the other cases apply), then we have a call.
672
673 -- Test for Id => case
674
675 if Token = Tok_Identifier then
676 Save_Scan_State (Scan_State); -- at Id
677 Scan; -- past Id
678
679 -- Test for => (allow := as an error substitute)
680
681 if Token in Tok_Arrow | Tok_Colon_Equal then
682 Restore_Scan_State (Scan_State); -- to Id
683 Arg_List := New_List;
684 goto LP_State_Call;
685
686 else
687 Restore_Scan_State (Scan_State); -- to Id
688 end if;
689 end if;
690
691 -- Here we have an expression after all
692
693 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
694
695 -- Check cases of discrete range for a slice
696
697 -- First possibility: Range_Attribute_Reference
698
699 if Expr_Form = EF_Range_Attr then
700 Range_Node := Expr_Node;
701
702 -- Second possibility: Simple_expression .. Simple_expression
703
704 elsif Token = Tok_Dot_Dot then
705 Check_Simple_Expression (Expr_Node);
706 Range_Node := New_Node (N_Range, Token_Ptr);
707 Set_Low_Bound (Range_Node, Expr_Node);
708 Scan; -- past ..
709 Expr_Node := P_Expression;
710 Check_Simple_Expression (Expr_Node);
711 Set_High_Bound (Range_Node, Expr_Node);
712
713 -- Third possibility: Type_name range Range
714
715 elsif Token = Tok_Range then
716 if Expr_Form /= EF_Simple_Name then
717 Error_Msg_SC ("subtype mark must precede RANGE");
718 raise Error_Resync;
719 end if;
720
721 Range_Node := P_Subtype_Indication (Expr_Node);
722
723 -- Otherwise we just have an expression. It is true that we might
724 -- have a subtype mark without a range constraint but this case
725 -- is syntactically indistinguishable from the expression case.
726
727 else
728 Arg_List := New_List;
729 goto LP_State_Expr;
730 end if;
731
732 -- Fall through here with unmistakable Discrete range scanned,
733 -- which means that we definitely have the case of a slice. The
734 -- Discrete range is in Range_Node.
735
736 if Token = Tok_Comma then
737 Error_Msg_SC ("slice cannot have more than one dimension");
738 raise Error_Resync;
739
740 elsif Token /= Tok_Right_Paren then
741 if Token = Tok_Arrow then
742
743 -- This may be an aggregate that is missing a qualification
744
745 Error_Msg_SC
746 ("context of aggregate must be a qualified expression");
747 raise Error_Resync;
748
749 else
750 T_Right_Paren;
751 raise Error_Resync;
752 end if;
753
754 else
755 Scan; -- past right paren
756 Prefix_Node := Name_Node;
757 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
758 Set_Prefix (Name_Node, Prefix_Node);
759 Set_Discrete_Range (Name_Node, Range_Node);
760
761 -- An operator node is legal as a prefix to other names,
762 -- but not for a slice.
763
764 if Nkind (Prefix_Node) = N_Operator_Symbol then
765 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
766 end if;
767
768 -- If we have a name extension, go scan it
769
770 if Token in Token_Class_Namext then
771 goto Scan_Name_Extension_OK;
772
773 -- Otherwise return (a slice is a name, but is not a call)
774
775 else
776 Expr_Form := EF_Name;
777 return Name_Node;
778 end if;
779 end if;
780
781 -- In LP_State_Expr, we have scanned one or more expressions, and
782 -- so we have a call or an indexed component which is a name. On
783 -- entry we have the expression just scanned in Expr_Node and
784 -- Arg_List contains the list of expressions encountered so far
785
786 <<LP_State_Expr>>
787 Append (Expr_Node, Arg_List);
788
789 if Token = Tok_Arrow then
790 Error_Msg
791 ("expect identifier in parameter association", Sloc (Expr_Node));
792 Scan; -- past arrow
793
794 elsif not Comma_Present then
795 T_Right_Paren;
796
797 Prefix_Node := Name_Node;
798 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
799 Set_Prefix (Name_Node, Prefix_Node);
800 Set_Expressions (Name_Node, Arg_List);
801
802 goto Scan_Name_Extension;
803 end if;
804
805 -- Comma present (and scanned out), test for identifier => case
806 -- Test for identifier => case
807
808 if Token = Tok_Identifier then
809 Save_Scan_State (Scan_State); -- at Id
810 Scan; -- past Id
811
812 -- Test for => (allow := as error substitute)
813
814 if Token in Tok_Arrow | Tok_Colon_Equal then
815 Restore_Scan_State (Scan_State); -- to Id
816 goto LP_State_Call;
817
818 -- Otherwise it's just an expression after all, so backup
819
820 else
821 Restore_Scan_State (Scan_State); -- to Id
822 end if;
823 end if;
824
825 -- Here we have an expression after all, so stay in this state
826
827 Expr_Node := P_Expression_If_OK;
828 goto LP_State_Expr;
829
830 -- LP_State_Call corresponds to the situation in which at least one
831 -- instance of Id => Expression has been encountered, so we know that
832 -- we do not have a name, but rather a call. We enter it with the
833 -- scan pointer pointing to the next argument to scan, and Arg_List
834 -- containing the list of arguments scanned so far.
835
836 <<LP_State_Call>>
837
838 -- Test for case of Id => Expression (named parameter)
839
840 if Token = Tok_Identifier then
841 Save_Scan_State (Scan_State); -- at Id
842 Ident_Node := Token_Node;
843 Scan; -- past Id
844
845 -- Deal with => (allow := as incorrect substitute)
846
847 if Token in Tok_Arrow | Tok_Colon_Equal then
848 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
849 Set_Selector_Name (Arg_Node, Ident_Node);
850 T_Arrow;
851 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
852 Append (Arg_Node, Arg_List);
853
854 -- If a comma follows, go back and scan next entry
855
856 if Comma_Present then
857 goto LP_State_Call;
858
859 -- Otherwise we have the end of a call
860
861 else
862 Prefix_Node := Name_Node;
863 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
864 Set_Name (Name_Node, Prefix_Node);
865 Set_Parameter_Associations (Name_Node, Arg_List);
866 T_Right_Paren;
867
868 if Token in Token_Class_Namext then
869 goto Scan_Name_Extension_OK;
870
871 -- This is a case of a call which cannot be a name
872
873 else
874 Expr_Form := EF_Name;
875 return Name_Node;
876 end if;
877 end if;
878
879 -- Not named parameter: Id started an expression after all
880
881 else
882 Restore_Scan_State (Scan_State); -- to Id
883 end if;
884 end if;
885
886 -- Here if entry did not start with Id => which means that it
887 -- is a positional parameter, which is not allowed, since we
888 -- have seen at least one named parameter already.
889
890 Error_Msg_SC
891 ("positional parameter association " &
892 "not allowed after named one");
893
894 Error_Loc := Token_Ptr;
895
896 Expr_Node := P_Expression_If_OK;
897
898 -- Leaving the '>' in an association is not unusual, so suggest
899 -- a possible fix.
900
901 if Nkind (Expr_Node) = N_Op_Eq then
902 Error_Msg_Sloc := Sloc (Expr_Node);
903 Error_Msg ("\maybe `='>` was intended #", Error_Loc);
904 end if;
905
906 -- We go back to scanning out expressions, so that we do not get
907 -- multiple error messages when several positional parameters
908 -- follow a named parameter.
909
910 goto LP_State_Expr;
911
912 -- End of treatment for name extensions starting with left paren
913
914 -- End of loop through name extensions
915
916 end P_Name;
917
918 -- This function parses a restricted form of Names which are either
919 -- designators, or designators preceded by a sequence of prefixes
920 -- that are direct names.
921
922 -- Error recovery: cannot raise Error_Resync
923
924 function P_Function_Name return Node_Id is
925 Designator_Node : Node_Id;
926 Prefix_Node : Node_Id;
927 Selector_Node : Node_Id;
928 Dot_Sloc : Source_Ptr := No_Location;
929
930 begin
931 -- Prefix_Node is set to the gathered prefix so far, Empty means that
932 -- no prefix has been scanned. This allows us to build up the result
933 -- in the required right recursive manner.
934
935 Prefix_Node := Empty;
936
937 -- Loop through prefixes
938
939 loop
940 Designator_Node := Token_Node;
941
942 if Token not in Token_Class_Desig then
943 return P_Identifier; -- let P_Identifier issue the error message
944
945 else -- Token in Token_Class_Desig
946 Scan; -- past designator
947 exit when Token /= Tok_Dot;
948 end if;
949
950 -- Here at a dot, with token just before it in Designator_Node
951
952 if No (Prefix_Node) then
953 Prefix_Node := Designator_Node;
954 else
955 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
956 Set_Prefix (Selector_Node, Prefix_Node);
957 Set_Selector_Name (Selector_Node, Designator_Node);
958 Prefix_Node := Selector_Node;
959 end if;
960
961 Dot_Sloc := Token_Ptr;
962 Scan; -- past dot
963 end loop;
964
965 -- Fall out of the loop having just scanned a designator
966
967 if No (Prefix_Node) then
968 return Designator_Node;
969 else
970 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
971 Set_Prefix (Selector_Node, Prefix_Node);
972 Set_Selector_Name (Selector_Node, Designator_Node);
973 return Selector_Node;
974 end if;
975
976 exception
977 when Error_Resync =>
978 return Error;
979 end P_Function_Name;
980
981 -- This function parses a restricted form of Names which are either
982 -- identifiers, or identifiers preceded by a sequence of prefixes
983 -- that are direct names.
984
985 -- Error recovery: cannot raise Error_Resync
986
987 function P_Qualified_Simple_Name return Node_Id is
988 Designator_Node : Node_Id;
989 Prefix_Node : Node_Id;
990 Selector_Node : Node_Id;
991 Dot_Sloc : Source_Ptr := No_Location;
992
993 begin
994 -- Prefix node is set to the gathered prefix so far, Empty means that
995 -- no prefix has been scanned. This allows us to build up the result
996 -- in the required right recursive manner.
997
998 Prefix_Node := Empty;
999
1000 -- Loop through prefixes
1001
1002 loop
1003 Designator_Node := Token_Node;
1004
1005 if Token = Tok_Identifier then
1006 Scan; -- past identifier
1007 exit when Token /= Tok_Dot;
1008
1009 elsif Token not in Token_Class_Desig then
1010 return P_Identifier; -- let P_Identifier issue the error message
1011
1012 else
1013 Scan; -- past designator
1014
1015 if Token /= Tok_Dot then
1016 Error_Msg_SP ("identifier expected");
1017 return Error;
1018 end if;
1019 end if;
1020
1021 -- Here at a dot, with token just before it in Designator_Node
1022
1023 if No (Prefix_Node) then
1024 Prefix_Node := Designator_Node;
1025 else
1026 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1027 Set_Prefix (Selector_Node, Prefix_Node);
1028 Set_Selector_Name (Selector_Node, Designator_Node);
1029 Prefix_Node := Selector_Node;
1030 end if;
1031
1032 Dot_Sloc := Token_Ptr;
1033 Scan; -- past dot
1034 end loop;
1035
1036 -- Fall out of the loop having just scanned an identifier
1037
1038 if No (Prefix_Node) then
1039 return Designator_Node;
1040 else
1041 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1042 Set_Prefix (Selector_Node, Prefix_Node);
1043 Set_Selector_Name (Selector_Node, Designator_Node);
1044 return Selector_Node;
1045 end if;
1046
1047 exception
1048 when Error_Resync =>
1049 return Error;
1050 end P_Qualified_Simple_Name;
1051
1052 -- This procedure differs from P_Qualified_Simple_Name only in that it
1053 -- raises Error_Resync if any error is encountered. It only returns after
1054 -- scanning a valid qualified simple name.
1055
1056 -- Error recovery: can raise Error_Resync
1057
1058 function P_Qualified_Simple_Name_Resync return Node_Id is
1059 Designator_Node : Node_Id;
1060 Prefix_Node : Node_Id;
1061 Selector_Node : Node_Id;
1062 Dot_Sloc : Source_Ptr := No_Location;
1063
1064 begin
1065 Prefix_Node := Empty;
1066
1067 -- Loop through prefixes
1068
1069 loop
1070 Designator_Node := Token_Node;
1071
1072 if Token = Tok_Identifier then
1073 Scan; -- past identifier
1074 exit when Token /= Tok_Dot;
1075
1076 elsif Token not in Token_Class_Desig then
1077 Discard_Junk_Node (P_Identifier); -- to issue the error message
1078 raise Error_Resync;
1079
1080 else
1081 Scan; -- past designator
1082
1083 if Token /= Tok_Dot then
1084 Error_Msg_SP ("identifier expected");
1085 raise Error_Resync;
1086 end if;
1087 end if;
1088
1089 -- Here at a dot, with token just before it in Designator_Node
1090
1091 if No (Prefix_Node) then
1092 Prefix_Node := Designator_Node;
1093 else
1094 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1095 Set_Prefix (Selector_Node, Prefix_Node);
1096 Set_Selector_Name (Selector_Node, Designator_Node);
1097 Prefix_Node := Selector_Node;
1098 end if;
1099
1100 Dot_Sloc := Token_Ptr;
1101 Scan; -- past period
1102 end loop;
1103
1104 -- Fall out of the loop having just scanned an identifier
1105
1106 if No (Prefix_Node) then
1107 return Designator_Node;
1108 else
1109 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1110 Set_Prefix (Selector_Node, Prefix_Node);
1111 Set_Selector_Name (Selector_Node, Designator_Node);
1112 return Selector_Node;
1113 end if;
1114 end P_Qualified_Simple_Name_Resync;
1115
1116 ----------------------
1117 -- 4.1 Direct_Name --
1118 ----------------------
1119
1120 -- Parsed by P_Name and other functions in section 4.1
1121
1122 -----------------
1123 -- 4.1 Prefix --
1124 -----------------
1125
1126 -- Parsed by P_Name (4.1)
1127
1128 -------------------------------
1129 -- 4.1 Explicit Dereference --
1130 -------------------------------
1131
1132 -- Parsed by P_Name (4.1)
1133
1134 -------------------------------
1135 -- 4.1 Implicit_Dereference --
1136 -------------------------------
1137
1138 -- Parsed by P_Name (4.1)
1139
1140 ----------------------------
1141 -- 4.1 Indexed Component --
1142 ----------------------------
1143
1144 -- Parsed by P_Name (4.1)
1145
1146 ----------------
1147 -- 4.1 Slice --
1148 ----------------
1149
1150 -- Parsed by P_Name (4.1)
1151
1152 -----------------------------
1153 -- 4.1 Selected_Component --
1154 -----------------------------
1155
1156 -- Parsed by P_Name (4.1)
1157
1158 ------------------------
1159 -- 4.1 Selector Name --
1160 ------------------------
1161
1162 -- Parsed by P_Name (4.1)
1163
1164 ------------------------------
1165 -- 4.1 Attribute Reference --
1166 ------------------------------
1167
1168 -- Parsed by P_Name (4.1)
1169
1170 -------------------------------
1171 -- 4.1 Attribute Designator --
1172 -------------------------------
1173
1174 -- Parsed by P_Name (4.1)
1175
1176 --------------------------------------
1177 -- 4.1.4 Range Attribute Reference --
1178 --------------------------------------
1179
1180 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1181
1182 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1183
1184 -- In the grammar, a RANGE attribute is simply a name, but its use is
1185 -- highly restricted, so in the parser, we do not regard it as a name.
1186 -- Instead, P_Name returns without scanning the 'RANGE part of the
1187 -- attribute, and the caller uses the following function to construct
1188 -- a range attribute in places where it is appropriate.
1189
1190 -- Note that RANGE here is treated essentially as an identifier,
1191 -- rather than a reserved word.
1192
1193 -- The caller has parsed the prefix, i.e. a name, and Token points to
1194 -- the apostrophe. The token after the apostrophe is known to be RANGE
1195 -- at this point. The prefix node becomes the prefix of the attribute.
1196
1197 -- Error_Recovery: Cannot raise Error_Resync
1198
1199 function P_Range_Attribute_Reference
1200 (Prefix_Node : Node_Id)
1201 return Node_Id
1202 is
1203 Attr_Node : Node_Id;
1204
1205 begin
1206 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1207 Set_Prefix (Attr_Node, Prefix_Node);
1208 Scan; -- past apostrophe
1209
1210 if Style_Check then
1211 Style.Check_Attribute_Name (True);
1212 end if;
1213
1214 Set_Attribute_Name (Attr_Node, Name_Range);
1215 Scan; -- past RANGE
1216
1217 if Token = Tok_Left_Paren then
1218 Scan; -- past left paren
1219 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1220 T_Right_Paren;
1221 end if;
1222
1223 return Attr_Node;
1224 end P_Range_Attribute_Reference;
1225
1226 -------------------------------------
1227 -- P_Reduction_Attribute_Reference --
1228 -------------------------------------
1229
1230 function P_Reduction_Attribute_Reference (S : Node_Id)
1231 return Node_Id
1232 is
1233 Attr_Node : Node_Id;
1234 Attr_Name : Name_Id;
1235
1236 begin
1237 Attr_Name := Token_Name;
1238 Scan; -- past Reduce
1239 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1240 Set_Attribute_Name (Attr_Node, Attr_Name);
1241 if Attr_Name /= Name_Reduce then
1242 Error_Msg ("Reduce attribute expected", Prev_Token_Ptr);
1243 end if;
1244
1245 Set_Prefix (Attr_Node, S);
1246 Set_Expressions (Attr_Node, New_List);
1247 T_Left_Paren;
1248 Append (P_Name, Expressions (Attr_Node));
1249 T_Comma;
1250 Append (P_Expression, Expressions (Attr_Node));
1251 T_Right_Paren;
1252
1253 return Attr_Node;
1254 end P_Reduction_Attribute_Reference;
1255
1256 ---------------------------------------
1257 -- 4.1.4 Range Attribute Designator --
1258 ---------------------------------------
1259
1260 -- Parsed by P_Range_Attribute_Reference (4.4)
1261
1262 ---------------------------------------------
1263 -- 4.1.4 (2) Reduction_Attribute_Reference --
1264 ---------------------------------------------
1265
1266 -- parsed by P_Reduction_Attribute_Reference
1267
1268 --------------------
1269 -- 4.3 Aggregate --
1270 --------------------
1271
1272 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1273
1274 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1275 -- an aggregate is known to be required (code statement, extension
1276 -- aggregate), in which cases this routine performs the necessary check
1277 -- that we have an aggregate rather than a parenthesized expression
1278
1279 -- Error recovery: can raise Error_Resync
1280
1281 function P_Aggregate return Node_Id is
1282 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1283 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1284
1285 begin
1286 if Nkind (Aggr_Node) /= N_Aggregate
1287 and then
1288 Nkind (Aggr_Node) /= N_Extension_Aggregate
1289 and then Ada_Version < Ada_2022
1290 then
1291 Error_Msg
1292 ("aggregate may not have single positional component", Aggr_Sloc);
1293 return Error;
1294 else
1295 return Aggr_Node;
1296 end if;
1297 end P_Aggregate;
1298
1299 ------------------------------------------------
1300 -- 4.3 Aggregate or Parenthesized Expression --
1301 ------------------------------------------------
1302
1303 -- This procedure parses out either an aggregate or a parenthesized
1304 -- expression (these two constructs are closely related, since a
1305 -- parenthesized expression looks like an aggregate with a single
1306 -- positional component).
1307
1308 -- AGGREGATE ::=
1309 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1310
1311 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1312
1313 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1314 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1315 -- | null record
1316
1317 -- RECORD_COMPONENT_ASSOCIATION ::=
1318 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1319
1320 -- COMPONENT_CHOICE_LIST ::=
1321 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1322 -- | others
1323
1324 -- EXTENSION_AGGREGATE ::=
1325 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1326
1327 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1328
1329 -- ARRAY_AGGREGATE ::=
1330 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1331
1332 -- POSITIONAL_ARRAY_AGGREGATE ::=
1333 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1334 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1335 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1336
1337 -- NAMED_ARRAY_AGGREGATE ::=
1338 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1339
1340 -- PRIMARY ::= (EXPRESSION);
1341
1342 -- Error recovery: can raise Error_Resync
1343
1344 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1345 -- to Ada 2005 limited aggregates (AI-287)
1346
1347 function P_Aggregate_Or_Paren_Expr return Node_Id is
1348 Aggregate_Node : Node_Id;
1349 Expr_List : List_Id;
1350 Assoc_List : List_Id;
1351 Expr_Node : Node_Id;
1352 Lparen_Sloc : Source_Ptr;
1353 Scan_State : Saved_Scan_State;
1354
1355 procedure Box_Error;
1356 -- Called if <> is encountered as positional aggregate element. Issues
1357 -- error message and sets Expr_Node to Error.
1358
1359 function Is_Quantified_Expression return Boolean;
1360 -- The presence of iterated component associations requires a one
1361 -- token lookahead to distinguish it from quantified expressions.
1362
1363 ---------------
1364 -- Box_Error --
1365 ---------------
1366
1367 procedure Box_Error is
1368 begin
1369 Error_Msg_Ada_2005_Extension ("'<'> in aggregate");
1370
1371 -- Ada 2005 (AI-287): The box notation is allowed only with named
1372 -- notation because positional notation might be error prone. For
1373 -- example, in "(X, <>, Y, <>)", there is no type associated with
1374 -- the boxes, so you might not be leaving out the components you
1375 -- thought you were leaving out.
1376
1377 Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1378 Scan; -- past box
1379 Expr_Node := Error;
1380 end Box_Error;
1381
1382 ------------------------------
1383 -- Is_Quantified_Expression --
1384 ------------------------------
1385
1386 function Is_Quantified_Expression return Boolean is
1387 Maybe : Boolean;
1388 Scan_State : Saved_Scan_State;
1389
1390 begin
1391 Save_Scan_State (Scan_State);
1392 Scan; -- past FOR
1393 Maybe := Token in Tok_All | Tok_Some;
1394 Restore_Scan_State (Scan_State); -- to FOR
1395 return Maybe;
1396 end Is_Quantified_Expression;
1397
1398 Start_Token : constant Token_Type := Token;
1399 -- Used to prevent mismatches (...] and [...)
1400
1401 Saved_Delta_Aggregate_Flag : constant Boolean := Inside_Delta_Aggregate;
1402
1403 -- Start of processing for P_Aggregate_Or_Paren_Expr
1404
1405 begin
1406 Lparen_Sloc := Token_Ptr;
1407 if Token = Tok_Left_Bracket then
1408 Scan;
1409
1410 -- Special case for null aggregate in Ada 2022
1411
1412 if Token = Tok_Right_Bracket then
1413 Scan; -- past ]
1414 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1415 Set_Expressions (Aggregate_Node, New_List);
1416 Set_Component_Associations (Aggregate_Node, New_List);
1417 Set_Is_Homogeneous_Aggregate (Aggregate_Node);
1418 return Aggregate_Node;
1419 end if;
1420 else
1421 T_Left_Paren;
1422 end if;
1423
1424 -- Note on parentheses count. For cases like an if expression, the
1425 -- parens here really count as real parentheses for the paren count,
1426 -- so we adjust the paren count accordingly after scanning the expr.
1427
1428 -- If expression
1429
1430 if Token = Tok_If then
1431 Expr_Node := P_If_Expression;
1432 T_Right_Paren;
1433 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1434 return Expr_Node;
1435
1436 -- Case expression
1437
1438 elsif Token = Tok_Case then
1439 Expr_Node := P_Case_Expression;
1440 T_Right_Paren;
1441 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1442 return Expr_Node;
1443
1444 -- Quantified expression
1445
1446 elsif Token = Tok_For and then Is_Quantified_Expression then
1447 Expr_Node := P_Quantified_Expression;
1448 T_Right_Paren;
1449 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1450 return Expr_Node;
1451
1452 -- Note: the mechanism used here of rescanning the initial expression
1453 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1454 -- out the discrete choice list.
1455
1456 -- Deal with expression and extension aggregates first
1457
1458 elsif Token /= Tok_Others then
1459 Save_Scan_State (Scan_State); -- at start of expression
1460
1461 -- Deal with (NULL RECORD)
1462
1463 if Token = Tok_Null then
1464 Scan; -- past NULL
1465
1466 if Token = Tok_Record then
1467 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1468 Set_Null_Record_Present (Aggregate_Node, True);
1469 Scan; -- past RECORD
1470 T_Right_Paren;
1471 return Aggregate_Node;
1472 else
1473 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1474 end if;
1475
1476 elsif Token = Tok_For then
1477 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1478 Expr_Node := P_Iterated_Component_Association;
1479 goto Aggregate;
1480 end if;
1481
1482 -- Scan expression, handling box appearing as positional argument
1483
1484 if Token = Tok_Box then
1485 Box_Error;
1486 else
1487 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1488 end if;
1489
1490 -- Extension or Delta aggregate
1491
1492 if Token = Tok_With then
1493 if Nkind (Expr_Node) = N_Attribute_Reference
1494 and then Attribute_Name (Expr_Node) = Name_Range
1495 then
1496 Bad_Range_Attribute (Sloc (Expr_Node));
1497 return Error;
1498 end if;
1499
1500 if Ada_Version = Ada_83 then
1501 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1502 end if;
1503
1504 Scan; -- past WITH
1505 if Token = Tok_Delta then
1506 Scan; -- past DELTA
1507 Inside_Delta_Aggregate := True;
1508 Aggregate_Node := New_Node (N_Delta_Aggregate, Lparen_Sloc);
1509 Set_Expression (Aggregate_Node, Expr_Node);
1510 Expr_Node := Empty;
1511
1512 goto Aggregate;
1513
1514 else
1515 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1516 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1517 end if;
1518
1519 -- Deal with WITH NULL RECORD case
1520
1521 if Token = Tok_Null then
1522 Save_Scan_State (Scan_State); -- at NULL
1523 Scan; -- past NULL
1524
1525 if Token = Tok_Record then
1526 Scan; -- past RECORD
1527 Set_Null_Record_Present (Aggregate_Node, True);
1528 T_Right_Paren;
1529 return Aggregate_Node;
1530
1531 else
1532 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1533 end if;
1534 end if;
1535
1536 if Token /= Tok_Others then
1537 Save_Scan_State (Scan_State);
1538 Expr_Node := P_Expression;
1539 else
1540 Expr_Node := Empty;
1541 end if;
1542
1543 -- Expression
1544
1545 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1546 if Nkind (Expr_Node) = N_Attribute_Reference
1547 and then Attribute_Name (Expr_Node) = Name_Range
1548 then
1549 Error_Msg
1550 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1551 Scan; -- past right paren
1552 return Expr_Node;
1553 end if;
1554
1555 -- Bump paren count of expression
1556
1557 if Expr_Node /= Error then
1558 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1559 end if;
1560
1561 T_Right_Paren; -- past right paren (error message if none)
1562 return Expr_Node;
1563
1564 -- Normal aggregate
1565
1566 else
1567 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1568 end if;
1569
1570 -- Others
1571
1572 else
1573 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1574 Expr_Node := Empty;
1575 end if;
1576
1577 -- Prepare to scan list of component associations
1578 <<Aggregate>>
1579 Expr_List := No_List; -- don't set yet, maybe all named entries
1580 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1581
1582 -- This loop scans through component associations. On entry to the
1583 -- loop, an expression has been scanned at the start of the current
1584 -- association unless initial token was OTHERS, in which case
1585 -- Expr_Node is set to Empty.
1586
1587 loop
1588 -- Deal with others association first. This is a named association
1589
1590 if No (Expr_Node) then
1591 Append_New (P_Record_Or_Array_Component_Association, Assoc_List);
1592
1593 -- Improper use of WITH
1594
1595 elsif Token = Tok_With then
1596 Error_Msg_SC ("WITH must be preceded by single expression in " &
1597 "extension aggregate");
1598 raise Error_Resync;
1599
1600 -- Range attribute can only appear as part of a discrete choice list
1601
1602 elsif Nkind (Expr_Node) = N_Attribute_Reference
1603 and then Attribute_Name (Expr_Node) = Name_Range
1604 and then Token /= Tok_Arrow
1605 and then Token /= Tok_Vertical_Bar
1606 then
1607 Bad_Range_Attribute (Sloc (Expr_Node));
1608 return Error;
1609
1610 -- Assume positional case if comma, right paren, or literal or
1611 -- identifier or OTHERS follows (the latter cases are missing
1612 -- comma cases). Also assume positional if a semicolon follows,
1613 -- which can happen if there are missing parens.
1614 -- In Ada 2012 and 2022 an iterated association can appear.
1615
1616 elsif Nkind (Expr_Node) in
1617 N_Iterated_Component_Association | N_Iterated_Element_Association
1618 then
1619 Append_New (Expr_Node, Assoc_List);
1620
1621 elsif Token in Tok_Comma | Tok_Right_Paren | Tok_Others
1622 | Token_Class_Lit_Or_Name | Tok_Semicolon
1623 then
1624 if Present (Assoc_List) then
1625 Error_Msg_BC -- CODEFIX
1626 ("""='>"" expected (positional association cannot follow "
1627 & "named association)");
1628 end if;
1629
1630 Append_New (Expr_Node, Expr_List);
1631
1632 -- Check for aggregate followed by left parent, maybe missing comma
1633
1634 elsif Nkind (Expr_Node) = N_Aggregate
1635 and then Token = Tok_Left_Paren
1636 then
1637 T_Comma;
1638
1639 Append_New (Expr_Node, Expr_List);
1640
1641 elsif Token = Tok_Right_Bracket then
1642 Append_New (Expr_Node, Expr_List);
1643 exit;
1644
1645 -- Anything else is assumed to be a named association
1646
1647 else
1648 Restore_Scan_State (Scan_State); -- to start of expression
1649
1650 Append_New (P_Record_Or_Array_Component_Association, Assoc_List);
1651 end if;
1652
1653 exit when not Comma_Present;
1654
1655 -- If we are at an expression terminator, something is seriously
1656 -- wrong, so let's get out now, before we start eating up stuff
1657 -- that doesn't belong to us.
1658
1659 if Token in Token_Class_Eterm and then Token /= Tok_For then
1660 Error_Msg_AP
1661 ("expecting expression or component association");
1662 exit;
1663 end if;
1664
1665 -- Deal with misused box
1666
1667 if Token = Tok_Box then
1668 Box_Error;
1669
1670 -- Otherwise initiate for reentry to top of loop by scanning an
1671 -- initial expression, unless the first token is OTHERS or FOR,
1672 -- which indicates an iterated component association.
1673
1674 elsif Token = Tok_Others then
1675 Expr_Node := Empty;
1676
1677 elsif Token = Tok_For then
1678 Expr_Node := P_Iterated_Component_Association;
1679
1680 else
1681 Save_Scan_State (Scan_State); -- at start of expression
1682 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1683
1684 end if;
1685 end loop;
1686
1687 -- All component associations (positional and named) have been scanned.
1688 -- Scan ] or ) based on Start_Token.
1689
1690 case Start_Token is
1691 when Tok_Left_Bracket =>
1692
1693 Set_Component_Associations (Aggregate_Node, Assoc_List);
1694 Set_Is_Homogeneous_Aggregate (Aggregate_Node);
1695 T_Right_Bracket;
1696
1697 if Token = Tok_Apostrophe then
1698 Scan;
1699
1700 if Token = Tok_Identifier then
1701 return P_Reduction_Attribute_Reference (Aggregate_Node);
1702 end if;
1703 end if;
1704 when Tok_Left_Paren =>
1705 if Nkind (Aggregate_Node) = N_Aggregate then
1706 Set_Is_Parenthesis_Aggregate (Aggregate_Node);
1707 end if;
1708
1709 T_Right_Paren;
1710 when others => raise Program_Error;
1711 end case;
1712
1713 if Nkind (Aggregate_Node) /= N_Delta_Aggregate then
1714 Set_Expressions (Aggregate_Node, Expr_List);
1715 end if;
1716
1717 Set_Component_Associations (Aggregate_Node, Assoc_List);
1718
1719 -- Inside_Delta_Aggregate is only tested if Serious_Errors = 0, so
1720 -- it is ok if we fail to restore the saved I_D_A value in an error
1721 -- path. In particular, it is ok that we do not restore it if
1722 -- Error_Resync is propagated. Earlier return statements (which return
1723 -- without restoring the saved I_D_A value) should either be in error
1724 -- paths or in paths where I_D_A could not have been modified.
1725
1726 Inside_Delta_Aggregate := Saved_Delta_Aggregate_Flag;
1727
1728 return Aggregate_Node;
1729 end P_Aggregate_Or_Paren_Expr;
1730
1731 ------------------------------------------------
1732 -- 4.3 Record or Array Component Association --
1733 ------------------------------------------------
1734
1735 -- RECORD_COMPONENT_ASSOCIATION ::=
1736 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1737 -- | COMPONENT_CHOICE_LIST => <>
1738
1739 -- COMPONENT_CHOICE_LIST =>
1740 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1741 -- | others
1742
1743 -- ARRAY_COMPONENT_ASSOCIATION ::=
1744 -- DISCRETE_CHOICE_LIST => EXPRESSION
1745 -- | DISCRETE_CHOICE_LIST => <>
1746 -- | ITERATED_COMPONENT_ASSOCIATION
1747
1748 -- Note: this routine only handles the named cases, including others.
1749 -- Cases where the component choice list is not present have already
1750 -- been handled directly.
1751
1752 -- Error recovery: can raise Error_Resync
1753
1754 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1755 -- rules have been extended to give support to Ada 2005 limited
1756 -- aggregates (AI-287)
1757
1758 function P_Record_Or_Array_Component_Association return Node_Id is
1759 Assoc_Node : Node_Id;
1760 Box_Present : Boolean := False;
1761 Box_With_Identifier_Present : Boolean := False;
1762 begin
1763 -- A loop indicates an iterated_component_association
1764
1765 if Token = Tok_For then
1766 return P_Iterated_Component_Association;
1767 end if;
1768
1769 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1770 Set_Binding_Chars (Assoc_Node, No_Name);
1771
1772 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1773 Set_Sloc (Assoc_Node, Token_Ptr);
1774 TF_Arrow;
1775
1776 if Token = Tok_Box then
1777
1778 -- Ada 2005(AI-287): The box notation is used to indicate the
1779 -- default initialization of aggregate components
1780
1781 Error_Msg_Ada_2005_Extension ("component association with '<'>");
1782
1783 Box_Present := True;
1784 Set_Box_Present (Assoc_Node);
1785 Scan; -- past box
1786 elsif Token = Tok_Less then
1787 declare
1788 Scan_State : Saved_Scan_State;
1789 Id : Node_Id;
1790 begin
1791 Save_Scan_State (Scan_State);
1792 Scan; -- past "<"
1793 if Token = Tok_Identifier then
1794 Id := P_Defining_Identifier;
1795 if Token = Tok_Greater then
1796 if Core_Extensions_Allowed then
1797 Set_Box_Present (Assoc_Node);
1798 Set_Binding_Chars (Assoc_Node, Chars (Id));
1799 Box_Present := True;
1800 Box_With_Identifier_Present := True;
1801 Scan; -- past ">"
1802 else
1803 Error_Msg_GNAT_Extension
1804 ("identifier within box", Token_Ptr);
1805 Box_Present := True;
1806 -- Avoid cascading errors by ignoring the identifier
1807 end if;
1808 end if;
1809 end if;
1810 if not Box_Present then
1811 -- it wasn't an "is <identifier>", so restore.
1812 Restore_Scan_State (Scan_State);
1813 end if;
1814 end;
1815 end if;
1816
1817 if not Box_Present then
1818 Set_Expression (Assoc_Node, P_Expression);
1819 end if;
1820
1821 -- Check for "is <identifier>" for aggregate that is part of
1822 -- a pattern for a general case statement.
1823
1824 if Token = Tok_Is then
1825 declare
1826 Scan_State : Saved_Scan_State;
1827 Id : Node_Id;
1828 begin
1829 Save_Scan_State (Scan_State);
1830 Scan; -- past "is"
1831 if Token = Tok_Identifier then
1832 Id := P_Defining_Identifier;
1833
1834 if not Core_Extensions_Allowed then
1835 Error_Msg_GNAT_Extension
1836 ("IS following component association", Token_Ptr);
1837 elsif Box_With_Identifier_Present then
1838 Error_Msg
1839 ("Both identifier-in-box and trailing identifier"
1840 & " specified for one component association",
1841 Token_Ptr);
1842 else
1843 Set_Binding_Chars (Assoc_Node, Chars (Id));
1844 end if;
1845 else
1846 -- It wasn't an "is <identifier>", so restore.
1847 Restore_Scan_State (Scan_State);
1848 end if;
1849 end;
1850 end if;
1851
1852 return Assoc_Node;
1853 end P_Record_Or_Array_Component_Association;
1854
1855 -----------------------------
1856 -- 4.3.1 Record Aggregate --
1857 -----------------------------
1858
1859 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1860 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1861
1862 ----------------------------------------------
1863 -- 4.3.1 Record Component Association List --
1864 ----------------------------------------------
1865
1866 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1867
1868 ----------------------------------
1869 -- 4.3.1 Component Choice List --
1870 ----------------------------------
1871
1872 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1873
1874 --------------------------------
1875 -- 4.3.1 Extension Aggregate --
1876 --------------------------------
1877
1878 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1879
1880 --------------------------
1881 -- 4.3.1 Ancestor Part --
1882 --------------------------
1883
1884 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1885
1886 ----------------------------
1887 -- 4.3.1 Array Aggregate --
1888 ----------------------------
1889
1890 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1891
1892 ---------------------------------------
1893 -- 4.3.1 Positional Array Aggregate --
1894 ---------------------------------------
1895
1896 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1897
1898 ----------------------------------
1899 -- 4.3.1 Named Array Aggregate --
1900 ----------------------------------
1901
1902 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1903
1904 ----------------------------------------
1905 -- 4.3.1 Array Component Association --
1906 ----------------------------------------
1907
1908 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1909
1910 ---------------------
1911 -- 4.4 Expression --
1912 ---------------------
1913
1914 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1915
1916 -- EXPRESSION ::=
1917 -- RELATION {LOGICAL_OPERATOR RELATION}
1918
1919 -- CHOICE_EXPRESSION ::=
1920 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1921
1922 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1923
1924 -- On return, Expr_Form indicates the categorization of the expression
1925 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1926 -- an error message is given, and Error is returned).
1927
1928 -- Error recovery: cannot raise Error_Resync
1929
1930 function P_Expression return Node_Id is
1931 Logical_Op : Node_Kind;
1932 Prev_Logical_Op : Node_Kind;
1933 Op_Location : Source_Ptr;
1934 Node1 : Node_Id;
1935 Node2 : Node_Id;
1936
1937 begin
1938 Node1 := P_Relation;
1939
1940 if Token in Token_Class_Logop then
1941 Prev_Logical_Op := N_Empty;
1942
1943 loop
1944 Op_Location := Token_Ptr;
1945 Logical_Op := P_Logical_Operator;
1946
1947 if Prev_Logical_Op /= N_Empty and then
1948 Logical_Op /= Prev_Logical_Op
1949 then
1950 Error_Msg
1951 ("mixed logical operators in expression", Op_Location);
1952 Prev_Logical_Op := N_Empty;
1953 else
1954 Prev_Logical_Op := Logical_Op;
1955 end if;
1956
1957 Node2 := Node1;
1958 Node1 := New_Op_Node (Logical_Op, Op_Location);
1959 Set_Left_Opnd (Node1, Node2);
1960 Set_Right_Opnd (Node1, P_Relation);
1961
1962 -- Check for case of errant comma or semicolon
1963
1964 if Token in Tok_Comma | Tok_Semicolon then
1965 declare
1966 Com : constant Boolean := Token = Tok_Comma;
1967 Scan_State : Saved_Scan_State;
1968 Logop : Node_Kind;
1969
1970 begin
1971 Save_Scan_State (Scan_State); -- at comma/semicolon
1972 Scan; -- past comma/semicolon
1973
1974 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1975 -- do not deal with AND/OR because those cases get mixed up
1976 -- with the select alternatives case.
1977
1978 if Token in Tok_And | Tok_Or then
1979 Logop := P_Logical_Operator;
1980 Restore_Scan_State (Scan_State); -- to comma/semicolon
1981
1982 if Logop in N_And_Then | N_Or_Else then
1983 Scan; -- past comma/semicolon
1984
1985 if Com then
1986 Error_Msg_SP -- CODEFIX
1987 ("|extra "","" ignored");
1988 else
1989 Error_Msg_SP -- CODEFIX
1990 ("|extra "";"" ignored");
1991 end if;
1992
1993 else
1994 Restore_Scan_State (Scan_State); -- to comma/semicolon
1995 end if;
1996
1997 else
1998 Restore_Scan_State (Scan_State); -- to comma/semicolon
1999 end if;
2000 end;
2001 end if;
2002
2003 exit when Token not in Token_Class_Logop;
2004 end loop;
2005
2006 Expr_Form := EF_Non_Simple;
2007 end if;
2008
2009 if Token = Tok_Apostrophe then
2010 Bad_Range_Attribute (Token_Ptr);
2011 return Error;
2012 else
2013 return Node1;
2014 end if;
2015 end P_Expression;
2016
2017 -- This function is identical to the normal P_Expression, except that it
2018 -- also permits the appearance of a case, conditional, or quantified
2019 -- expression if the call immediately follows a left paren, and followed
2020 -- by a right parenthesis. These forms are allowed if these conditions
2021 -- are not met, but an error message will be issued.
2022
2023 function P_Expression_If_OK return Node_Id is
2024 begin
2025 -- Case of conditional, case or quantified expression
2026
2027 if Token in Tok_Case | Tok_If | Tok_For | Tok_Declare then
2028 return P_Unparen_Cond_Expr_Etc;
2029
2030 -- Normal case, not case/conditional/quantified expression
2031
2032 else
2033 return P_Expression;
2034 end if;
2035 end P_Expression_If_OK;
2036
2037 -- This function is identical to the normal P_Expression, except that it
2038 -- checks that the expression scan did not stop on a right paren. It is
2039 -- called in all contexts where a right parenthesis cannot legitimately
2040 -- follow an expression.
2041
2042 -- Error recovery: cannot raise Error_Resync
2043
2044 function P_Expression_No_Right_Paren return Node_Id is
2045 Expr : constant Node_Id := P_Expression;
2046 begin
2047 Ignore (Tok_Right_Paren);
2048 return Expr;
2049 end P_Expression_No_Right_Paren;
2050
2051 ----------------------------------------
2052 -- 4.4 Expression_Or_Range_Attribute --
2053 ----------------------------------------
2054
2055 -- EXPRESSION ::=
2056 -- RELATION {and RELATION} | RELATION {and then RELATION}
2057 -- | RELATION {or RELATION} | RELATION {or else RELATION}
2058 -- | RELATION {xor RELATION}
2059
2060 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2061
2062 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2063
2064 -- On return, Expr_Form indicates the categorization of the expression
2065 -- and EF_Range_Attr is one of the possibilities.
2066
2067 -- Error recovery: cannot raise Error_Resync
2068
2069 -- In the grammar, a RANGE attribute is simply a name, but its use is
2070 -- highly restricted, so in the parser, we do not regard it as a name.
2071 -- Instead, P_Name returns without scanning the 'RANGE part of the
2072 -- attribute, and P_Expression_Or_Range_Attribute handles the range
2073 -- attribute reference. In the normal case where a range attribute is
2074 -- not allowed, an error message is issued by P_Expression.
2075
2076 function P_Expression_Or_Range_Attribute return Node_Id is
2077 Logical_Op : Node_Kind;
2078 Prev_Logical_Op : Node_Kind;
2079 Op_Location : Source_Ptr;
2080 Node1 : Node_Id;
2081 Node2 : Node_Id;
2082 Attr_Node : Node_Id;
2083
2084 begin
2085 Node1 := P_Relation;
2086
2087 if Token = Tok_Apostrophe then
2088 Attr_Node := P_Range_Attribute_Reference (Node1);
2089 Expr_Form := EF_Range_Attr;
2090 return Attr_Node;
2091
2092 elsif Token in Token_Class_Logop then
2093 Prev_Logical_Op := N_Empty;
2094
2095 loop
2096 Op_Location := Token_Ptr;
2097 Logical_Op := P_Logical_Operator;
2098
2099 if Prev_Logical_Op /= N_Empty and then
2100 Logical_Op /= Prev_Logical_Op
2101 then
2102 Error_Msg
2103 ("mixed logical operators in expression", Op_Location);
2104 Prev_Logical_Op := N_Empty;
2105 else
2106 Prev_Logical_Op := Logical_Op;
2107 end if;
2108
2109 Node2 := Node1;
2110 Node1 := New_Op_Node (Logical_Op, Op_Location);
2111 Set_Left_Opnd (Node1, Node2);
2112 Set_Right_Opnd (Node1, P_Relation);
2113 exit when Token not in Token_Class_Logop;
2114 end loop;
2115
2116 Expr_Form := EF_Non_Simple;
2117 end if;
2118
2119 if Token = Tok_Apostrophe then
2120 Bad_Range_Attribute (Token_Ptr);
2121 return Error;
2122 else
2123 return Node1;
2124 end if;
2125 end P_Expression_Or_Range_Attribute;
2126
2127 -- Version that allows a non-parenthesized case, conditional, or quantified
2128 -- expression if the call immediately follows a left paren, and followed
2129 -- by a right parenthesis. These forms are allowed if these conditions
2130 -- are not met, but an error message will be issued.
2131
2132 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
2133 begin
2134 -- Case of conditional, case or quantified expression
2135
2136 if Token in Tok_Case | Tok_If | Tok_For | Tok_Declare then
2137 return P_Unparen_Cond_Expr_Etc;
2138
2139 -- Normal case, not one of the above expression types
2140
2141 else
2142 return P_Expression_Or_Range_Attribute;
2143 end if;
2144 end P_Expression_Or_Range_Attribute_If_OK;
2145
2146 -------------------
2147 -- 4.4 Relation --
2148 -------------------
2149
2150 -- This procedure scans both relations and choice relations
2151
2152 -- CHOICE_RELATION ::=
2153 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
2154
2155 -- RELATION ::=
2156 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
2157 -- | RAISE_EXPRESSION
2158
2159 -- MEMBERSHIP_CHOICE_LIST ::=
2160 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
2161
2162 -- MEMBERSHIP_CHOICE ::=
2163 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
2164
2165 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
2166
2167 -- On return, Expr_Form indicates the categorization of the expression
2168
2169 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2170 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2171
2172 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2173 -- expression, then tokens are scanned until either a non-expression token,
2174 -- a right paren (not matched by a left paren) or a comma, is encountered.
2175
2176 function P_Relation return Node_Id is
2177 Node1, Node2 : Node_Id;
2178 Optok : Source_Ptr;
2179
2180 begin
2181 -- First check for raise expression
2182
2183 if Token = Tok_Raise then
2184 Node1 := P_Raise_Expression;
2185 Expr_Form := EF_Non_Simple;
2186 return Node1;
2187 end if;
2188
2189 -- All other cases
2190
2191 Node1 := P_Simple_Expression;
2192
2193 if Token not in Token_Class_Relop then
2194 return Node1;
2195
2196 else
2197 -- Here we have a relational operator following. If so then scan it
2198 -- out. Note that the assignment symbol := is treated as a relational
2199 -- operator to improve the error recovery when it is misused for =.
2200 -- P_Relational_Operator also parses the IN and NOT IN operations.
2201
2202 Optok := Token_Ptr;
2203 Node2 := New_Op_Node (P_Relational_Operator, Optok);
2204 Set_Left_Opnd (Node2, Node1);
2205
2206 -- Case of IN or NOT IN
2207
2208 if Prev_Token = Tok_In then
2209 P_Membership_Test (Node2);
2210
2211 -- Case of relational operator (= /= < <= > >=)
2212
2213 else
2214 Set_Right_Opnd (Node2, P_Simple_Expression);
2215 end if;
2216
2217 Expr_Form := EF_Non_Simple;
2218
2219 if Token in Token_Class_Relop then
2220 Error_Msg_SC ("unexpected relational operator");
2221 raise Error_Resync;
2222 end if;
2223
2224 return Node2;
2225 end if;
2226
2227 -- If any error occurs, then scan to the next expression terminator symbol
2228 -- or comma or right paren at the outer (i.e. current) parentheses level.
2229 -- The flags are set to indicate a normal simple expression.
2230
2231 exception
2232 when Error_Resync =>
2233 Resync_Expression;
2234 Expr_Form := EF_Simple;
2235 return Error;
2236 end P_Relation;
2237
2238 ----------------------------
2239 -- 4.4 Simple Expression --
2240 ----------------------------
2241
2242 -- SIMPLE_EXPRESSION ::=
2243 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2244
2245 -- On return, Expr_Form indicates the categorization of the expression
2246
2247 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2248 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2249
2250 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2251 -- expression, then tokens are scanned until either a non-expression token,
2252 -- a right paren (not matched by a left paren) or a comma, is encountered.
2253
2254 -- Note: P_Simple_Expression is called only internally by higher level
2255 -- expression routines. In cases in the grammar where a simple expression
2256 -- is required, the approach is to scan an expression, and then post an
2257 -- appropriate error message if the expression obtained is not simple. This
2258 -- gives better error recovery and treatment.
2259
2260 function P_Simple_Expression return Node_Id is
2261 Scan_State : Saved_Scan_State;
2262 Node1 : Node_Id;
2263 Node2 : Node_Id;
2264 Tokptr : Source_Ptr;
2265
2266 function At_Start_Of_Attribute return Boolean;
2267 -- Tests if we have quote followed by attribute name, if so, return True
2268 -- otherwise return False.
2269
2270 ---------------------------
2271 -- At_Start_Of_Attribute --
2272 ---------------------------
2273
2274 function At_Start_Of_Attribute return Boolean is
2275 begin
2276 if Token /= Tok_Apostrophe then
2277 return False;
2278
2279 else
2280 declare
2281 Scan_State : Saved_Scan_State;
2282
2283 begin
2284 Save_Scan_State (Scan_State);
2285 Scan; -- past quote
2286
2287 if Token = Tok_Identifier
2288 and then Is_Attribute_Name (Chars (Token_Node))
2289 then
2290 Restore_Scan_State (Scan_State);
2291 return True;
2292 else
2293 Restore_Scan_State (Scan_State);
2294 return False;
2295 end if;
2296 end;
2297 end if;
2298 end At_Start_Of_Attribute;
2299
2300 -- Start of processing for P_Simple_Expression
2301
2302 begin
2303 -- Check for cases starting with a name. There are two reasons for
2304 -- special casing. First speed things up by catching a common case
2305 -- without going through several routine layers. Second the caller must
2306 -- be informed via Expr_Form when the simple expression is a name.
2307
2308 if Token in Token_Class_Name then
2309 Node1 := P_Name;
2310
2311 -- Deal with apostrophe cases
2312
2313 if Token = Tok_Apostrophe then
2314 Save_Scan_State (Scan_State); -- at apostrophe
2315 Scan; -- past apostrophe
2316
2317 -- If qualified expression, scan it out and fall through
2318
2319 if Token = Tok_Left_Paren then
2320 Node1 := P_Qualified_Expression (Node1);
2321 Expr_Form := EF_Simple;
2322
2323 -- If range attribute, then we return with Token pointing to the
2324 -- apostrophe. Note: avoid the normal error check on exit. We
2325 -- know that the expression really is complete in this case.
2326
2327 else -- Token = Tok_Range then
2328 Restore_Scan_State (Scan_State); -- to apostrophe
2329 Expr_Form := EF_Simple_Name;
2330 return Node1;
2331 end if;
2332 end if;
2333
2334 -- If an expression terminator follows, the previous processing
2335 -- completely scanned out the expression (a common case), and
2336 -- left Expr_Form set appropriately for returning to our caller.
2337
2338 if Token in Token_Class_Sterm then
2339 null;
2340
2341 -- Handle '}' as expression terminator of an interpolated
2342 -- expression.
2343
2344 elsif Inside_Interpolated_String_Literal
2345 and then Token = Tok_Right_Curly_Bracket
2346 then
2347 null;
2348
2349 -- If we do not have an expression terminator, then complete the
2350 -- scan of a simple expression. This code duplicates the code
2351 -- found in P_Term and P_Factor.
2352
2353 else
2354 if Token = Tok_Double_Asterisk then
2355 if Style_Check then
2356 Style.Check_Exponentiation_Operator;
2357 end if;
2358
2359 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2360 Scan; -- past **
2361 Set_Left_Opnd (Node2, Node1);
2362 Set_Right_Opnd (Node2, P_Primary);
2363 Check_Bad_Exp;
2364 Node1 := Node2;
2365 end if;
2366
2367 loop
2368 exit when Token not in Token_Class_Mulop;
2369 Tokptr := Token_Ptr;
2370 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2371
2372 if Style_Check then
2373 Style.Check_Binary_Operator;
2374 end if;
2375
2376 Scan; -- past operator
2377 Set_Left_Opnd (Node2, Node1);
2378 Set_Right_Opnd (Node2, P_Factor);
2379 Node1 := Node2;
2380 end loop;
2381
2382 loop
2383 exit when Token not in Token_Class_Binary_Addop;
2384 Tokptr := Token_Ptr;
2385 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2386
2387 if Style_Check then
2388 Style.Check_Binary_Operator;
2389 end if;
2390
2391 Scan; -- past operator
2392 Set_Left_Opnd (Node2, Node1);
2393 Set_Right_Opnd (Node2, P_Term);
2394 Node1 := Node2;
2395 end loop;
2396
2397 Expr_Form := EF_Simple;
2398 end if;
2399
2400 -- Cases where simple expression does not start with a name
2401
2402 else
2403 -- Scan initial sign and initial Term
2404
2405 if Token in Token_Class_Unary_Addop then
2406 Tokptr := Token_Ptr;
2407 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
2408
2409 if Style_Check then
2410 Style.Check_Unary_Plus_Or_Minus (Inside_Depends);
2411 end if;
2412
2413 Scan; -- past operator
2414 Set_Right_Opnd (Node1, P_Term);
2415 else
2416 Node1 := P_Term;
2417 end if;
2418
2419 Expr_Form := EF_Simple;
2420
2421 -- In the following, we special-case a sequence of concatenations of
2422 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2423 -- else mixed in. For such a sequence, we return a tree representing
2424 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2425 -- the number of concatenations is large. If semantic analysis
2426 -- resolves the "&" to a predefined one, then this folding gives the
2427 -- right answer. Otherwise, semantic analysis will complain about a
2428 -- capacity-exceeded error. The purpose of this trick is to avoid
2429 -- creating a deeply nested tree, which would cause deep recursion
2430 -- during semantics, causing stack overflow. This way, we can handle
2431 -- enormous concatenations in the normal case of predefined "&". We
2432 -- first build up the normal tree, and then rewrite it if
2433 -- appropriate.
2434
2435 declare
2436 Num_Concats_Threshold : constant Positive := 1000;
2437 -- Arbitrary threshold value to enable optimization
2438
2439 First_Node : constant Node_Id := Node1;
2440 Is_Strlit_Concat : Boolean;
2441 -- True iff we've parsed a sequence of concatenations of string
2442 -- literals, with nothing else mixed in.
2443
2444 Num_Concats : Natural;
2445 -- Number of "&" operators if Is_Strlit_Concat is True
2446
2447 begin
2448 Is_Strlit_Concat :=
2449 Nkind (Node1) = N_String_Literal
2450 and then Token = Tok_Ampersand;
2451 Num_Concats := 0;
2452
2453 -- Scan out sequence of terms separated by binary adding operators
2454
2455 loop
2456 exit when Token not in Token_Class_Binary_Addop;
2457 Tokptr := Token_Ptr;
2458 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2459
2460 if Style_Check and then not Debug_Flag_Dot_QQ then
2461 Style.Check_Binary_Operator;
2462 end if;
2463
2464 Scan; -- past operator
2465 Set_Left_Opnd (Node2, Node1);
2466 Node1 := P_Term;
2467 Set_Right_Opnd (Node2, Node1);
2468
2469 -- Check if we're still concatenating string literals
2470
2471 Is_Strlit_Concat :=
2472 Is_Strlit_Concat
2473 and then Nkind (Node2) = N_Op_Concat
2474 and then Nkind (Node1) = N_String_Literal;
2475
2476 if Is_Strlit_Concat then
2477 Num_Concats := Num_Concats + 1;
2478 end if;
2479
2480 Node1 := Node2;
2481 end loop;
2482
2483 -- If we have an enormous series of concatenations of string
2484 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2485 -- flag tells semantic analysis that if the "&" is not predefined,
2486 -- the folded value is wrong.
2487
2488 if Is_Strlit_Concat
2489 and then Num_Concats >= Num_Concats_Threshold
2490 then
2491 declare
2492 Strlit_Concat_Val : String_Id;
2493 -- Contains the folded value (which will be correct if the
2494 -- "&" operators are the predefined ones).
2495
2496 Cur_Node : Node_Id;
2497 -- For walking up the tree
2498
2499 New_Node : Node_Id;
2500 -- Folded node to replace Node1
2501
2502 Loc : constant Source_Ptr := Sloc (First_Node);
2503
2504 begin
2505 -- Walk up the tree starting at the leftmost string literal
2506 -- (First_Node), building up the Strlit_Concat_Val as we
2507 -- go. Note that we do not use recursion here -- the whole
2508 -- point is to avoid recursively walking that enormous tree.
2509
2510 Start_String;
2511 Store_String_Chars (Strval (First_Node));
2512
2513 Cur_Node := Parent (First_Node);
2514 while Present (Cur_Node) loop
2515 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2516 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2517
2518 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2519 Cur_Node := Parent (Cur_Node);
2520 end loop;
2521
2522 Strlit_Concat_Val := End_String;
2523
2524 -- Create new folded node, and rewrite result with a concat-
2525 -- enation of an empty string literal and the folded node.
2526
2527 New_Node :=
2528 Make_Op_Concat (Loc,
2529 Make_String_Literal (Loc, Null_String_Id),
2530 Make_String_Literal (Loc, Strlit_Concat_Val,
2531 Is_Folded_In_Parser => True));
2532 Rewrite (Node1, New_Node);
2533 end;
2534 end if;
2535 end;
2536 end if;
2537
2538 -- If all extensions are enabled and we have a deep delta aggregate
2539 -- whose type is an array type with an element type that is a
2540 -- record type, then we can encounter legal things like
2541 -- with delta (Some_Index_Expression).Some_Component
2542 -- where a parenthesized expression precedes a dot.
2543 -- Similarly, if the element type is an array type then we can see
2544 -- with delta (Some_Index_Expression)(Another_Index_Expression)
2545 -- where a parenthesized expression precedes a left parenthesis.
2546
2547 if Token in Tok_Dot | Tok_Left_Paren
2548 and then Prev_Token = Tok_Right_Paren
2549 and then Serious_Errors_Detected = 0
2550 and then Inside_Delta_Aggregate
2551 and then All_Extensions_Allowed
2552 then
2553 if Token = Tok_Dot then
2554 Node2 := New_Node (N_Selected_Component, Token_Ptr);
2555 Scan; -- past dot
2556 declare
2557 Tail : constant Node_Id := P_Simple_Expression;
2558 -- remaining selectors occurring after the dot
2559
2560 Rover : Node_Id := Tail;
2561 Prev : Node_Id := Empty;
2562 begin
2563 -- If Tail already has a prefix, then we want to prepend
2564 -- Node1 onto that prefix and then return Tail.
2565 -- Otherwise, Tail should simply be an identifier so
2566 -- we want to build a Selected_Component with Tail as the
2567 -- selector name and return that.
2568
2569 Set_Prefix (Node2, Node1);
2570
2571 while Nkind (Rover)
2572 in N_Indexed_Component | N_Selected_Component loop
2573 Prev := Rover;
2574 Rover := Prefix (Rover);
2575 end loop;
2576
2577 case Nkind (Prev) is
2578 when N_Selected_Component | N_Indexed_Component =>
2579 -- We've scanned a dot, so an identifier should follow
2580 if Nkind (Prefix (Prev)) = N_Identifier then
2581 Set_Selector_Name (Node2, Prefix (Prev));
2582 Set_Prefix (Prev, Node2);
2583 return Tail;
2584 end if;
2585
2586 when N_Empty =>
2587 -- We've scanned a dot, so an identifier should follow
2588 if Nkind (Tail) = N_Identifier then
2589 Set_Selector_Name (Node2, Tail);
2590 return Node2;
2591 end if;
2592
2593 when others =>
2594 null;
2595 end case;
2596
2597 -- fall through to error case
2598 end;
2599 else
2600 Node2 := New_Node (N_Indexed_Component, Token_Ptr);
2601 declare
2602 Tail : constant Node_Id := P_Simple_Expression;
2603 -- remaining selectors
2604
2605 Rover : Node_Id := Tail;
2606 Prev : Node_Id := Empty;
2607 begin
2608 -- If Tail already has a prefix, then we want to prepend
2609 -- Node1 onto that prefix and then return Tail.
2610 -- Otherwise, Tail should be an index expression and
2611 -- we want to build an Indexed_Component with Tail as the
2612 -- index value and return that.
2613
2614 Set_Prefix (Node2, Node1);
2615
2616 while Nkind (Rover)
2617 in N_Indexed_Component | N_Selected_Component loop
2618 Prev := Rover;
2619 Rover := Prefix (Rover);
2620 end loop;
2621
2622 case Nkind (Prev) is
2623 when N_Selected_Component | N_Indexed_Component =>
2624 Set_Expressions (Node2, New_List (Prefix (Prev)));
2625 Set_Prefix (Prev, Node2);
2626 return Tail;
2627
2628 when N_Empty =>
2629 Set_Expressions (Node2, New_List (Tail));
2630 return Node2;
2631
2632 when others =>
2633 null;
2634 end case;
2635
2636 -- fall through to error case
2637 end;
2638 end if;
2639 end if;
2640
2641 -- Come here at end of simple expression, where we do a couple of
2642 -- special checks to improve error recovery.
2643
2644 -- Special test to improve error recovery. If the current token is a
2645 -- period, then someone is trying to do selection on something that is
2646 -- not a name, e.g. a qualified expression.
2647
2648 if Token = Tok_Dot then
2649 Error_Msg_SC ("prefix for selection is not a name");
2650
2651 -- If qualified expression, comment and continue, otherwise
2652 -- something is pretty nasty so do an Error_Resync call.
2653
2654 if Ada_Version < Ada_2012
2655 and then Nkind (Node1) = N_Qualified_Expression
2656 then
2657 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2658 else
2659 raise Error_Resync;
2660 end if;
2661 end if;
2662
2663 -- Special test to improve error recovery: If the current token is
2664 -- not the first token on a line (as determined by checking the
2665 -- previous token position with the start of the current line),
2666 -- then we insist that we have an appropriate terminating token.
2667 -- Consider the following two examples:
2668
2669 -- 1) if A nad B then ...
2670
2671 -- 2) A := B
2672 -- C := D
2673
2674 -- In the first example, we would like to issue a binary operator
2675 -- expected message and resynchronize to the then. In the second
2676 -- example, we do not want to issue a binary operator message, so
2677 -- that instead we will get the missing semicolon message. This
2678 -- distinction is of course a heuristic which does not always work,
2679 -- but in practice it is quite effective.
2680
2681 -- Note: the one case in which we do not go through this circuit is
2682 -- when we have scanned a range attribute and want to return with
2683 -- Token pointing to the apostrophe. The apostrophe is not normally
2684 -- an expression terminator, and is not in Token_Class_Sterm, but
2685 -- in this special case we know that the expression is complete.
2686
2687 -- We disable this error recovery machinery when we are processing an
2688 -- interpolated string and we reach the expression terminator '}'.
2689
2690 if not Token_Is_At_Start_Of_Line
2691 and then Token not in Token_Class_Sterm
2692 and then not (Inside_Interpolated_String_Literal
2693 and then Token = Tok_Right_Curly_Bracket)
2694 then
2695 -- Normally the right error message is indeed that we expected a
2696 -- binary operator, but in the case of being between a right and left
2697 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2698
2699 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2700 T_Comma;
2701
2702 -- And if we have a quote, we may have a bad attribute
2703
2704 elsif At_Start_Of_Attribute then
2705 Error_Msg_SC ("prefix of attribute must be a name");
2706
2707 if Ada_Version >= Ada_2012 then
2708 Error_Msg_SC ("\qualify expression to turn it into a name");
2709 end if;
2710
2711 -- Normal case for binary operator expected message
2712
2713 else
2714 Error_Msg_AP ("binary operator expected");
2715 end if;
2716
2717 raise Error_Resync;
2718
2719 else
2720 return Node1;
2721 end if;
2722
2723 -- If any error occurs, then scan to next expression terminator symbol
2724 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2725 -- level. Expr_Form is set to indicate a normal simple expression.
2726
2727 exception
2728 when Error_Resync =>
2729 Resync_Expression;
2730 Expr_Form := EF_Simple;
2731 return Error;
2732 end P_Simple_Expression;
2733
2734 -----------------------------------------------
2735 -- 4.4 Simple Expression or Range Attribute --
2736 -----------------------------------------------
2737
2738 -- SIMPLE_EXPRESSION ::=
2739 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2740
2741 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2742
2743 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2744
2745 -- Error recovery: cannot raise Error_Resync
2746
2747 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2748 Sexpr : Node_Id;
2749 Attr_Node : Node_Id;
2750
2751 begin
2752 -- We don't just want to roar ahead and call P_Simple_Expression
2753 -- here, since we want to handle the case of a parenthesized range
2754 -- attribute cleanly.
2755
2756 if Token = Tok_Left_Paren then
2757 declare
2758 Lptr : constant Source_Ptr := Token_Ptr;
2759 Scan_State : Saved_Scan_State;
2760
2761 begin
2762 Save_Scan_State (Scan_State);
2763 Scan; -- past left paren
2764 Sexpr := P_Simple_Expression;
2765
2766 if Token = Tok_Apostrophe then
2767 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2768 Expr_Form := EF_Range_Attr;
2769
2770 if Token = Tok_Right_Paren then
2771 Scan; -- scan past right paren if present
2772 end if;
2773
2774 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2775
2776 return Attr_Node;
2777 end if;
2778
2779 Restore_Scan_State (Scan_State);
2780 end;
2781 end if;
2782
2783 -- Here after dealing with parenthesized range attribute
2784
2785 Sexpr := P_Simple_Expression;
2786
2787 if Token = Tok_Apostrophe then
2788 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2789 Expr_Form := EF_Range_Attr;
2790 return Attr_Node;
2791
2792 else
2793 return Sexpr;
2794 end if;
2795 end P_Simple_Expression_Or_Range_Attribute;
2796
2797 ---------------
2798 -- 4.4 Term --
2799 ---------------
2800
2801 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2802
2803 -- Error recovery: can raise Error_Resync
2804
2805 function P_Term return Node_Id is
2806 Node1, Node2 : Node_Id;
2807 Tokptr : Source_Ptr;
2808
2809 begin
2810 Node1 := P_Factor;
2811
2812 loop
2813 exit when Token not in Token_Class_Mulop;
2814 Tokptr := Token_Ptr;
2815 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2816
2817 if Style_Check and then not Debug_Flag_Dot_QQ then
2818 Style.Check_Binary_Operator;
2819 end if;
2820
2821 Scan; -- past operator
2822 Set_Left_Opnd (Node2, Node1);
2823 Set_Right_Opnd (Node2, P_Factor);
2824 Node1 := Node2;
2825 end loop;
2826
2827 return Node1;
2828 end P_Term;
2829
2830 -----------------
2831 -- 4.4 Factor --
2832 -----------------
2833
2834 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2835
2836 -- Error recovery: can raise Error_Resync
2837
2838 function P_Factor return Node_Id is
2839 Node1 : Node_Id;
2840 Node2 : Node_Id;
2841
2842 subtype N_Primary is Node_Kind with Static_Predicate =>
2843 N_Primary in N_Aggregate
2844 | N_Allocator
2845 | N_Attribute_Reference
2846 | N_Case_Expression -- requires single parens
2847 | N_Delta_Aggregate
2848 | N_Direct_Name
2849 | N_Explicit_Dereference
2850 | N_Expression_With_Actions -- requires single parens
2851 | N_Extension_Aggregate
2852 | N_If_Expression -- requires single parens
2853 | N_Indexed_Component
2854 | N_Null
2855 | N_Numeric_Or_String_Literal
2856 | N_Qualified_Expression
2857 | N_Quantified_Expression -- requires single parens
2858 | N_Selected_Component
2859 | N_Slice
2860 | N_Subprogram_Call
2861 | N_Target_Name
2862 | N_Type_Conversion;
2863 -- Node kinds that represents a "primary" subexpression, which does not
2864 -- require parentheses when used as an operand of a unary operator.
2865
2866 begin
2867 if Token = Tok_Abs then
2868 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2869
2870 if Style_Check then
2871 Style.Check_Abs_Not;
2872 end if;
2873
2874 Scan; -- past ABS
2875 Set_Right_Opnd (Node1, P_Primary);
2876
2877 if Style_Check then
2878 if Nkind (Right_Opnd (Node1)) in N_Primary then
2879 Style.Check_Xtra_Parens_Precedence (Right_Opnd (Node1));
2880 end if;
2881 end if;
2882
2883 return Node1;
2884
2885 elsif Token = Tok_Not then
2886 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2887
2888 if Style_Check then
2889 Style.Check_Abs_Not;
2890 end if;
2891
2892 Scan; -- past NOT
2893 Set_Right_Opnd (Node1, P_Primary);
2894
2895 if Style_Check then
2896 if Nkind (Right_Opnd (Node1)) in N_Primary then
2897 Style.Check_Xtra_Parens_Precedence (Right_Opnd (Node1));
2898 end if;
2899 end if;
2900
2901 return Node1;
2902
2903 else
2904 Node1 := P_Primary;
2905
2906 if Token = Tok_Double_Asterisk then
2907 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2908 Scan; -- past **
2909 Set_Left_Opnd (Node2, Node1);
2910 Set_Right_Opnd (Node2, P_Primary);
2911 Check_Bad_Exp;
2912 return Node2;
2913 else
2914 return Node1;
2915 end if;
2916 end if;
2917 end P_Factor;
2918
2919 ------------------
2920 -- 4.4 Primary --
2921 ------------------
2922
2923 -- PRIMARY ::=
2924 -- NUMERIC_LITERAL | null
2925 -- | STRING_LITERAL | AGGREGATE
2926 -- | NAME | QUALIFIED_EXPRESSION
2927 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2928 -- | REDUCTION_ATTRIBUTE_REFERENCE
2929
2930 -- Error recovery: can raise Error_Resync
2931
2932 function P_Primary return Node_Id is
2933 Scan_State : Saved_Scan_State;
2934 Node1 : Node_Id;
2935
2936 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
2937 -- Remember if previous token is a left parenthesis. This is used to
2938 -- deal with checking whether IF/CASE/FOR expressions appearing as
2939 -- primaries require extra parenthesization.
2940
2941 begin
2942 -- The loop runs more than once only if misplaced pragmas are found
2943 -- or if a misplaced unary minus is skipped.
2944
2945 loop
2946 case Token is
2947
2948 -- Name token can start a name, call or qualified expression, all
2949 -- of which are acceptable possibilities for primary. Note also
2950 -- that string literal is included in name (as operator symbol)
2951 -- and type conversion is included in name (as indexed component).
2952
2953 when Tok_Char_Literal
2954 | Tok_Identifier
2955 | Tok_Operator_Symbol
2956 =>
2957 Node1 := P_Name;
2958
2959 -- All done unless apostrophe follows
2960
2961 if Token /= Tok_Apostrophe then
2962 return Node1;
2963
2964 -- Apostrophe following means that we have either just parsed
2965 -- the subtype mark of a qualified expression, or the prefix
2966 -- or a range attribute.
2967
2968 else -- Token = Tok_Apostrophe
2969 Save_Scan_State (Scan_State); -- at apostrophe
2970 Scan; -- past apostrophe
2971
2972 -- If range attribute, then this is always an error, since
2973 -- the only legitimate case (where the scanned expression is
2974 -- a qualified simple name) is handled at the level of the
2975 -- Simple_Expression processing. This case corresponds to a
2976 -- usage such as 3 + A'Range, which is always illegal.
2977
2978 if Token = Tok_Range then
2979 Restore_Scan_State (Scan_State); -- to apostrophe
2980 Bad_Range_Attribute (Token_Ptr);
2981 return Error;
2982
2983 -- If left paren, then we have a qualified expression.
2984 -- Note that P_Name guarantees that in this case, where
2985 -- Token = Tok_Apostrophe on return, the only two possible
2986 -- tokens following the apostrophe are left paren and
2987 -- RANGE, so we know we have a left paren here.
2988
2989 else -- Token = Tok_Left_Paren
2990 return P_Qualified_Expression (Node1);
2991
2992 end if;
2993 end if;
2994
2995 -- Numeric or string literal
2996
2997 when Tok_Integer_Literal
2998 | Tok_Real_Literal
2999 | Tok_String_Literal
3000 =>
3001 Node1 := Token_Node;
3002 Scan; -- past number
3003 return Node1;
3004
3005 -- Left paren, starts aggregate or parenthesized expression
3006
3007 when Tok_Left_Paren =>
3008 declare
3009 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
3010
3011 begin
3012 if Nkind (Expr) = N_Attribute_Reference
3013 and then Attribute_Name (Expr) = Name_Range
3014 then
3015 Bad_Range_Attribute (Sloc (Expr));
3016 end if;
3017
3018 return Expr;
3019 end;
3020
3021 when Tok_Left_Bracket =>
3022 return P_Aggregate;
3023
3024 when Tok_Left_Interpolated_String =>
3025 return P_Interpolated_String_Literal;
3026
3027 -- Allocator
3028
3029 when Tok_New =>
3030 return P_Allocator;
3031
3032 -- Null
3033
3034 when Tok_Null =>
3035 Scan; -- past NULL
3036 return New_Node (N_Null, Prev_Token_Ptr);
3037
3038 -- Pragma, not allowed here, so just skip past it
3039
3040 when Tok_Pragma =>
3041 P_Pragmas_Misplaced;
3042
3043 -- Deal with IF (possible unparenthesized if expression)
3044
3045 when Tok_If =>
3046
3047 -- If this looks like a real if, defined as an IF appearing at
3048 -- the start of a new line, then we consider we have a missing
3049 -- operand. If in Ada 2012 and the IF is not properly indented
3050 -- for a statement, we prefer to issue a message about an ill-
3051 -- parenthesized if expression.
3052
3053 if Token_Is_At_Start_Of_Line
3054 and then not
3055 (Ada_Version >= Ada_2012
3056 and then
3057 (Style_Check_Indentation = 0
3058 or else
3059 Start_Column rem Style_Check_Indentation /= 0))
3060 then
3061 Error_Msg_AP ("missing operand");
3062 return Error;
3063
3064 -- If this looks like an if expression, then treat it that way
3065 -- with an error message if not explicitly surrounded by
3066 -- parentheses.
3067
3068 elsif Ada_Version >= Ada_2012 then
3069 Node1 := P_If_Expression;
3070
3071 if not (Lparen and then Token = Tok_Right_Paren) then
3072 Error_Msg
3073 ("if expression must be parenthesized", Sloc (Node1));
3074 end if;
3075
3076 return Node1;
3077
3078 -- Otherwise treat as misused identifier
3079
3080 else
3081 return P_Identifier;
3082 end if;
3083
3084 -- Deal with CASE (possible unparenthesized case expression)
3085
3086 when Tok_Case =>
3087
3088 -- If this looks like a real case, defined as a CASE appearing
3089 -- the start of a new line, then we consider we have a missing
3090 -- operand. If in Ada 2012 and the CASE is not properly
3091 -- indented for a statement, we prefer to issue a message about
3092 -- an ill-parenthesized case expression.
3093
3094 if Token_Is_At_Start_Of_Line
3095 and then not
3096 (Ada_Version >= Ada_2012
3097 and then Style_Check_Indentation /= 0
3098 and then Start_Column rem Style_Check_Indentation /= 0)
3099 then
3100 Error_Msg_AP ("missing operand");
3101 return Error;
3102
3103 -- If this looks like a case expression, then treat it that way
3104 -- with an error message if not within parentheses.
3105
3106 elsif Ada_Version >= Ada_2012 then
3107 Node1 := P_Case_Expression;
3108
3109 if not (Lparen and then Token = Tok_Right_Paren) then
3110 Error_Msg
3111 ("case expression must be parenthesized", Sloc (Node1));
3112 end if;
3113
3114 return Node1;
3115
3116 -- Otherwise treat as misused identifier
3117
3118 else
3119 return P_Identifier;
3120 end if;
3121
3122 -- Quantified expression or iterated component association
3123
3124 when Tok_For =>
3125 if Token_Is_At_Start_Of_Line then
3126 Error_Msg_AP ("misplaced loop");
3127 return Error;
3128
3129 elsif Ada_Version >= Ada_2012 then
3130 Save_Scan_State (Scan_State);
3131 Scan; -- past FOR
3132
3133 if Token in Tok_All | Tok_Some then
3134 Restore_Scan_State (Scan_State); -- To FOR
3135 Node1 := P_Quantified_Expression;
3136
3137 if not (Lparen and then Token = Tok_Right_Paren) then
3138 Error_Msg
3139 ("quantified expression must be parenthesized",
3140 Sloc (Node1));
3141 end if;
3142
3143 -- If no quantifier keyword, this is an iterated component
3144 -- in an aggregate or an ill-formed quantified expression.
3145
3146 else
3147 Restore_Scan_State (Scan_State); -- To FOR
3148 Node1 := P_Iterated_Component_Association;
3149
3150 if not (Lparen and then Token = Tok_Right_Paren) then
3151 Error_Msg
3152 ("construct must be parenthesized", Sloc (Node1));
3153 end if;
3154 end if;
3155
3156 return Node1;
3157
3158 -- Otherwise treat as misused identifier
3159
3160 else
3161 return P_Identifier;
3162 end if;
3163
3164 -- Minus may well be an improper attempt at a unary minus. Give
3165 -- a message, skip the minus and keep going.
3166
3167 when Tok_Minus =>
3168 Error_Msg_SC ("parentheses required for unary minus");
3169 Scan; -- past minus
3170
3171 when Tok_At_Sign => -- AI12-0125 : target_name
3172 Error_Msg_Ada_2022_Feature ("target name", Token_Ptr);
3173
3174 Node1 := P_Name;
3175 return Node1;
3176
3177 -- Anything else is illegal as the first token of a primary, but
3178 -- we test for some common errors, to improve error messages.
3179
3180 when others =>
3181 if Is_Reserved_Identifier then
3182 return P_Identifier;
3183
3184 elsif Prev_Token = Tok_Comma then
3185 Error_Msg_SP -- CODEFIX
3186 ("|extra "","" ignored");
3187 raise Error_Resync;
3188
3189 else
3190 Error_Msg_AP ("missing operand");
3191 raise Error_Resync;
3192 end if;
3193 end case;
3194 end loop;
3195 end P_Primary;
3196
3197 -------------------------------
3198 -- 4.4 Quantified_Expression --
3199 -------------------------------
3200
3201 -- QUANTIFIED_EXPRESSION ::=
3202 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
3203 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
3204
3205 function P_Quantified_Expression return Node_Id is
3206 I_Spec : Node_Id;
3207 Node1 : Node_Id;
3208
3209 begin
3210 Error_Msg_Ada_2012_Feature ("quantified expression", Token_Ptr);
3211 Scan; -- past FOR
3212 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
3213
3214 if Token = Tok_All then
3215 Set_All_Present (Node1);
3216 elsif Token /= Tok_Some then
3217 Error_Msg_AP ("missing quantifier");
3218 raise Error_Resync;
3219 end if;
3220
3221 Scan; -- past ALL or SOME
3222 I_Spec := P_Loop_Parameter_Specification;
3223
3224 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
3225 Set_Loop_Parameter_Specification (Node1, I_Spec);
3226 else
3227 Set_Iterator_Specification (Node1, I_Spec);
3228 end if;
3229
3230 if Token = Tok_Arrow then
3231 Scan;
3232 Set_Condition (Node1, P_Expression);
3233 return Node1;
3234 else
3235 Error_Msg_AP ("missing arrow");
3236 raise Error_Resync;
3237 end if;
3238 end P_Quantified_Expression;
3239
3240 ---------------------------
3241 -- 4.5 Logical Operator --
3242 ---------------------------
3243
3244 -- LOGICAL_OPERATOR ::= and | or | xor
3245
3246 -- Note: AND THEN and OR ELSE are also treated as logical operators
3247 -- by the parser (even though they are not operators semantically)
3248
3249 -- The value returned is the appropriate Node_Kind code for the operator
3250 -- On return, Token points to the token following the scanned operator.
3251
3252 -- The caller has checked that the first token is a legitimate logical
3253 -- operator token (i.e. is either XOR, AND, OR).
3254
3255 -- Error recovery: cannot raise Error_Resync
3256
3257 function P_Logical_Operator return Node_Kind is
3258 begin
3259 if Token = Tok_And then
3260 if Style_Check then
3261 Style.Check_Binary_Operator;
3262 end if;
3263
3264 Scan; -- past AND
3265
3266 if Token = Tok_Then then
3267 Scan; -- past THEN
3268 return N_And_Then;
3269 else
3270 return N_Op_And;
3271 end if;
3272
3273 elsif Token = Tok_Or then
3274 if Style_Check then
3275 Style.Check_Binary_Operator;
3276 end if;
3277
3278 Scan; -- past OR
3279
3280 if Token = Tok_Else then
3281 Scan; -- past ELSE
3282 return N_Or_Else;
3283 else
3284 return N_Op_Or;
3285 end if;
3286
3287 else -- Token = Tok_Xor
3288 if Style_Check then
3289 Style.Check_Binary_Operator;
3290 end if;
3291
3292 Scan; -- past XOR
3293 return N_Op_Xor;
3294 end if;
3295 end P_Logical_Operator;
3296
3297 ------------------------------
3298 -- 4.5 Relational Operator --
3299 ------------------------------
3300
3301 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
3302
3303 -- The value returned is the appropriate Node_Kind code for the operator.
3304 -- On return, Token points to the operator token, NOT past it.
3305
3306 -- The caller has checked that the first token is a legitimate relational
3307 -- operator token (i.e. is one of the operator tokens listed above).
3308
3309 -- Error recovery: cannot raise Error_Resync
3310
3311 function P_Relational_Operator return Node_Kind is
3312 Op_Kind : Node_Kind;
3313 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
3314 (Tok_Less => N_Op_Lt,
3315 Tok_Equal => N_Op_Eq,
3316 Tok_Greater => N_Op_Gt,
3317 Tok_Not_Equal => N_Op_Ne,
3318 Tok_Greater_Equal => N_Op_Ge,
3319 Tok_Less_Equal => N_Op_Le,
3320 Tok_In => N_In,
3321 Tok_Not => N_Not_In,
3322 Tok_Box => N_Op_Ne);
3323
3324 begin
3325 if Token = Tok_Box then
3326 Error_Msg_SC -- CODEFIX
3327 ("|""'<'>"" should be ""/=""");
3328 end if;
3329
3330 Op_Kind := Relop_Node (Token);
3331
3332 if Style_Check then
3333 Style.Check_Binary_Operator;
3334 end if;
3335
3336 Scan; -- past operator token
3337
3338 -- Deal with NOT IN, if previous token was NOT, we must have IN now
3339
3340 if Prev_Token = Tok_Not then
3341
3342 -- Style check, for NOT IN, we require one space between NOT and IN
3343
3344 if Style_Check and then Token = Tok_In then
3345 Style.Check_Not_In;
3346 end if;
3347
3348 T_In;
3349 end if;
3350
3351 return Op_Kind;
3352 end P_Relational_Operator;
3353
3354 ---------------------------------
3355 -- 4.5 Binary Adding Operator --
3356 ---------------------------------
3357
3358 -- BINARY_ADDING_OPERATOR ::= + | - | &
3359
3360 -- The value returned is the appropriate Node_Kind code for the operator.
3361 -- On return, Token points to the operator token (NOT past it).
3362
3363 -- The caller has checked that the first token is a legitimate adding
3364 -- operator token (i.e. is one of the operator tokens listed above).
3365
3366 -- Error recovery: cannot raise Error_Resync
3367
3368 function P_Binary_Adding_Operator return Node_Kind is
3369 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
3370 (Tok_Ampersand => N_Op_Concat,
3371 Tok_Minus => N_Op_Subtract,
3372 Tok_Plus => N_Op_Add);
3373 begin
3374 return Addop_Node (Token);
3375 end P_Binary_Adding_Operator;
3376
3377 --------------------------------
3378 -- 4.5 Unary Adding Operator --
3379 --------------------------------
3380
3381 -- UNARY_ADDING_OPERATOR ::= + | -
3382
3383 -- The value returned is the appropriate Node_Kind code for the operator.
3384 -- On return, Token points to the operator token (NOT past it).
3385
3386 -- The caller has checked that the first token is a legitimate adding
3387 -- operator token (i.e. is one of the operator tokens listed above).
3388
3389 -- Error recovery: cannot raise Error_Resync
3390
3391 function P_Unary_Adding_Operator return Node_Kind is
3392 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
3393 (Tok_Minus => N_Op_Minus,
3394 Tok_Plus => N_Op_Plus);
3395 begin
3396 return Addop_Node (Token);
3397 end P_Unary_Adding_Operator;
3398
3399 -------------------------------
3400 -- 4.5 Multiplying Operator --
3401 -------------------------------
3402
3403 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3404
3405 -- The value returned is the appropriate Node_Kind code for the operator.
3406 -- On return, Token points to the operator token (NOT past it).
3407
3408 -- The caller has checked that the first token is a legitimate multiplying
3409 -- operator token (i.e. is one of the operator tokens listed above).
3410
3411 -- Error recovery: cannot raise Error_Resync
3412
3413 function P_Multiplying_Operator return Node_Kind is
3414 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
3415 (Tok_Asterisk => N_Op_Multiply,
3416 Tok_Mod => N_Op_Mod,
3417 Tok_Rem => N_Op_Rem,
3418 Tok_Slash => N_Op_Divide);
3419 begin
3420 return Mulop_Node (Token);
3421 end P_Multiplying_Operator;
3422
3423 --------------------------------------
3424 -- 4.5 Highest Precedence Operator --
3425 --------------------------------------
3426
3427 -- Parsed by P_Factor (4.4)
3428
3429 -- Note: this rule is not in fact used by the grammar at any point
3430
3431 --------------------------
3432 -- 4.6 Type Conversion --
3433 --------------------------
3434
3435 -- Parsed by P_Primary as a Name (4.1)
3436
3437 -------------------------------
3438 -- 4.7 Qualified Expression --
3439 -------------------------------
3440
3441 -- QUALIFIED_EXPRESSION ::=
3442 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3443
3444 -- The caller has scanned the name which is the Subtype_Mark parameter
3445 -- and scanned past the single quote following the subtype mark. The
3446 -- caller has not checked that this name is in fact appropriate for
3447 -- a subtype mark name (i.e. it is a selected component or identifier).
3448
3449 -- Error_Recovery: cannot raise Error_Resync
3450
3451 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
3452 Qual_Node : Node_Id;
3453 begin
3454 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
3455 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
3456 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
3457 return Qual_Node;
3458 end P_Qualified_Expression;
3459
3460 --------------------
3461 -- 4.8 Allocator --
3462 --------------------
3463
3464 -- ALLOCATOR ::=
3465 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3466 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3467 --
3468 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3469
3470 -- The caller has checked that the initial token is NEW
3471
3472 -- Error recovery: can raise Error_Resync
3473
3474 function P_Allocator return Node_Id is
3475 Alloc_Node : Node_Id;
3476 Type_Node : Node_Id;
3477 Null_Exclusion_Present : Boolean;
3478
3479 begin
3480 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
3481 T_New;
3482
3483 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3484
3485 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3486
3487 if Token = Tok_Left_Paren then
3488 Scan; -- past (
3489 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
3490 T_Right_Paren;
3491
3492 Error_Msg_Ada_2012_Feature
3493 ("|subpool specification",
3494 Sloc (Subpool_Handle_Name (Alloc_Node)));
3495 end if;
3496
3497 Null_Exclusion_Present := P_Null_Exclusion;
3498 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
3499 Type_Node := P_Subtype_Mark_Resync;
3500
3501 if Token = Tok_Apostrophe then
3502 Scan; -- past apostrophe
3503 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
3504 else
3505 Set_Expression
3506 (Alloc_Node,
3507 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
3508
3509 -- AI05-0104: An explicit null exclusion is not allowed for an
3510 -- allocator without initialization. In previous versions of the
3511 -- language it just raises constraint error.
3512
3513 if Ada_Version >= Ada_2012 and then Null_Exclusion_Present then
3514 Error_Msg_N
3515 ("an allocator with a subtype indication "
3516 & "cannot have a null exclusion", Alloc_Node);
3517 end if;
3518 end if;
3519
3520 return Alloc_Node;
3521 end P_Allocator;
3522
3523 -----------------------
3524 -- P_Case_Expression --
3525 -----------------------
3526
3527 function P_Case_Expression return Node_Id is
3528 Loc : constant Source_Ptr := Token_Ptr;
3529 Case_Node : Node_Id;
3530 Save_State : Saved_Scan_State;
3531
3532 begin
3533 Error_Msg_Ada_2012_Feature ("|case expression", Token_Ptr);
3534 Scan; -- past CASE
3535 Case_Node :=
3536 Make_Case_Expression (Loc,
3537 Expression => P_Expression_No_Right_Paren,
3538 Alternatives => New_List);
3539 T_Is;
3540
3541 -- We now have scanned out CASE expression IS, scan alternatives
3542
3543 loop
3544 T_When;
3545 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
3546
3547 -- Missing comma if WHEN (more alternatives present)
3548
3549 if Token = Tok_When then
3550 T_Comma;
3551
3552 -- A semicolon followed by "when" is probably meant to be a comma
3553
3554 elsif Token = Tok_Semicolon then
3555 Save_Scan_State (Save_State);
3556 Scan; -- past the semicolon
3557
3558 if Token /= Tok_When then
3559 Restore_Scan_State (Save_State);
3560 exit;
3561 end if;
3562
3563 Error_Msg_SP -- CODEFIX
3564 ("|"";"" should be "",""");
3565
3566 -- If comma/WHEN, skip comma and we have another alternative
3567
3568 elsif Token = Tok_Comma then
3569 Save_Scan_State (Save_State);
3570 Scan; -- past comma
3571
3572 if Token /= Tok_When then
3573 Restore_Scan_State (Save_State);
3574 exit;
3575 end if;
3576
3577 -- If no comma or WHEN, definitely done
3578
3579 else
3580 exit;
3581 end if;
3582 end loop;
3583
3584 -- If we have an END CASE, diagnose as not needed
3585
3586 if Token = Tok_End then
3587 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
3588 Scan; -- past END
3589
3590 if Token = Tok_Case then
3591 Scan; -- past CASE;
3592 end if;
3593 end if;
3594
3595 -- Return the Case_Expression node
3596
3597 return Case_Node;
3598 end P_Case_Expression;
3599
3600 -----------------------------------
3601 -- P_Case_Expression_Alternative --
3602 -----------------------------------
3603
3604 -- CASE_STATEMENT_ALTERNATIVE ::=
3605 -- when DISCRETE_CHOICE_LIST =>
3606 -- EXPRESSION
3607
3608 -- The caller has checked that and scanned past the initial WHEN token
3609 -- Error recovery: can raise Error_Resync
3610
3611 function P_Case_Expression_Alternative return Node_Id is
3612 Case_Alt_Node : Node_Id;
3613 begin
3614 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
3615 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
3616 TF_Arrow;
3617 Set_Expression (Case_Alt_Node, P_Expression);
3618 return Case_Alt_Node;
3619 end P_Case_Expression_Alternative;
3620
3621 --------------------------------------
3622 -- P_Iterated_Component_Association --
3623 --------------------------------------
3624
3625 -- ITERATED_COMPONENT_ASSOCIATION ::=
3626 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3627 -- for ITERATOR_SPECIFICATION => EXPRESSION
3628
3629 function P_Iterated_Component_Association return Node_Id is
3630 Assoc_Node : Node_Id;
3631 Choice : Node_Id;
3632 Filter : Node_Id := Empty;
3633 Id : Node_Id;
3634 Iter_Spec : Node_Id;
3635 Loop_Spec : Node_Id;
3636 State : Saved_Scan_State;
3637 In_Reverse : Boolean := False;
3638
3639 procedure Build_Iterated_Element_Association;
3640 -- If the iterator includes a key expression or a filter, it is
3641 -- an Ada 2022 Iterator_Element_Association within a container
3642 -- aggregate.
3643
3644 ----------------------------------------
3645 -- Build_Iterated_Element_Association --
3646 ----------------------------------------
3647
3648 procedure Build_Iterated_Element_Association is
3649 begin
3650 -- Build loop_parameter_specification
3651
3652 Loop_Spec :=
3653 New_Node (N_Loop_Parameter_Specification, Prev_Token_Ptr);
3654 Set_Defining_Identifier (Loop_Spec, Id);
3655
3656 Set_Reverse_Present (Loop_Spec, In_Reverse);
3657
3658 Choice := First (Discrete_Choices (Assoc_Node));
3659 Assoc_Node :=
3660 New_Node (N_Iterated_Element_Association, Prev_Token_Ptr);
3661 Set_Loop_Parameter_Specification (Assoc_Node, Loop_Spec);
3662
3663 if Present (Next (Choice)) then
3664 Error_Msg_N ("expect loop parameter specification", Choice);
3665 end if;
3666
3667 Remove (Choice);
3668 Set_Discrete_Subtype_Definition (Loop_Spec, Choice);
3669 Set_Iterator_Filter (Loop_Spec, Filter);
3670 end Build_Iterated_Element_Association;
3671
3672 -- Start of processing for P_Iterated_Component_Association
3673
3674 begin
3675 Scan; -- past FOR
3676 Save_Scan_State (State);
3677
3678 -- A lookahead is necessary to differentiate between the
3679 -- Ada 2012 form with a choice list, and the Ada 2022 element
3680 -- iterator form, recognized by the presence of "OF". Other
3681 -- disambiguation requires context and is done during semantic
3682 -- analysis. Note that "for X in E" is syntactically ambiguous:
3683 -- if E is a subtype indication this is a loop parameter spec,
3684 -- while if E a name it is an iterator_specification, and the
3685 -- disambiguation takes place during semantic analysis.
3686 -- In addition, if "use" is present after the specification,
3687 -- this is an Iterated_Element_Association that carries a
3688 -- key_expression, and we generate the appropriate node.
3689 -- Finally, the Iterated_Element form is reserved for container
3690 -- aggregates, and is illegal in array aggregates.
3691
3692 Id := P_Defining_Identifier;
3693 Assoc_Node :=
3694 New_Node (N_Iterated_Component_Association, Prev_Token_Ptr);
3695
3696 case Token is
3697 when Tok_In =>
3698 Set_Defining_Identifier (Assoc_Node, Id);
3699 T_In;
3700
3701 if Token = Tok_Reverse then
3702 Scan; -- past REVERSE
3703 Set_Reverse_Present (Assoc_Node, True);
3704 In_Reverse := True;
3705 end if;
3706
3707 Set_Discrete_Choices (Assoc_Node, P_Discrete_Choice_List);
3708
3709 -- The iterator may include a filter
3710
3711 if Token = Tok_When then
3712 Scan; -- past WHEN
3713 Filter := P_Condition;
3714 end if;
3715
3716 if Token = Tok_Use then
3717
3718 -- Ada 2022 Key-expression is present, rewrite node as an
3719 -- Iterated_Element_Association.
3720
3721 Scan; -- past USE
3722 Build_Iterated_Element_Association;
3723 Set_Key_Expression (Assoc_Node, P_Expression);
3724
3725 elsif Present (Filter) then
3726 -- A loop_parameter_specification also indicates an Ada 2022
3727 -- construct, in contrast with a subtype indication used in
3728 -- array aggregates.
3729
3730 Build_Iterated_Element_Association;
3731 end if;
3732
3733 TF_Arrow;
3734 Set_Expression (Assoc_Node, P_Expression);
3735
3736 when Tok_Colon | Tok_Of =>
3737 Restore_Scan_State (State);
3738 Scan; -- past OF
3739 Iter_Spec := P_Iterator_Specification (Id);
3740 Set_Iterator_Specification (Assoc_Node, Iter_Spec);
3741
3742 if Token = Tok_Use then
3743 Scan; -- past USE
3744 -- This is an iterated_element_association
3745
3746 Assoc_Node :=
3747 New_Node (N_Iterated_Element_Association, Prev_Token_Ptr);
3748 Set_Iterator_Specification (Assoc_Node, Iter_Spec);
3749 Set_Key_Expression (Assoc_Node, P_Expression);
3750 end if;
3751
3752 TF_Arrow;
3753 Set_Expression (Assoc_Node, P_Expression);
3754
3755 when others =>
3756 Error_Msg_AP ("missing IN or OF");
3757 end case;
3758
3759 return Assoc_Node;
3760 end P_Iterated_Component_Association;
3761
3762 ---------------------
3763 -- P_If_Expression --
3764 ---------------------
3765
3766 -- IF_EXPRESSION ::=
3767 -- if CONDITION then DEPENDENT_EXPRESSION
3768 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3769 -- [else DEPENDENT_EXPRESSION]
3770
3771 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3772
3773 function P_If_Expression return Node_Id is
3774 function P_If_Expression_Internal
3775 (Loc : Source_Ptr;
3776 Cond : Node_Id) return Node_Id;
3777 -- This is the internal recursive routine that does all the work, it is
3778 -- recursive since it is used to process ELSIF parts, which internally
3779 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3780 -- sequence is like the outer function except that the caller passes
3781 -- the conditional expression (scanned using P_Expression), and the
3782 -- scan pointer points just past this expression. Loc points to the
3783 -- IF or ELSIF token.
3784
3785 ------------------------------
3786 -- P_If_Expression_Internal --
3787 ------------------------------
3788
3789 function P_If_Expression_Internal
3790 (Loc : Source_Ptr;
3791 Cond : Node_Id) return Node_Id
3792 is
3793 Exprs : constant List_Id := New_List;
3794 Expr : Node_Id;
3795 State : Saved_Scan_State;
3796 Eptr : Source_Ptr;
3797
3798 begin
3799 -- All cases except where we are at right paren
3800
3801 if Token /= Tok_Right_Paren then
3802 TF_Then;
3803 Append_To (Exprs, P_Condition (Cond));
3804 Append_To (Exprs, P_Expression);
3805
3806 -- Case of right paren (missing THEN phrase). Note that we know this
3807 -- is the IF case, since the caller dealt with this possibility in
3808 -- the ELSIF case.
3809
3810 else
3811 Error_Msg_BC ("missing THEN phrase");
3812 Append_To (Exprs, P_Condition (Cond));
3813 end if;
3814
3815 -- We now have scanned out IF expr THEN expr
3816
3817 -- Check for common error of semicolon before the ELSE
3818
3819 if Token = Tok_Semicolon then
3820 Save_Scan_State (State);
3821 Scan; -- past semicolon
3822
3823 if Token in Tok_Else | Tok_Elsif then
3824 Error_Msg_SP -- CODEFIX
3825 ("|extra "";"" ignored");
3826
3827 else
3828 Restore_Scan_State (State);
3829 end if;
3830 end if;
3831
3832 -- Scan out ELSIF sequence if present
3833
3834 if Token = Tok_Elsif then
3835 Eptr := Token_Ptr;
3836 Scan; -- past ELSIF
3837 Expr := P_Expression;
3838
3839 -- If we are at a right paren, we assume the ELSIF should be ELSE
3840
3841 if Token = Tok_Right_Paren then
3842 Error_Msg ("ELSIF should be ELSE", Eptr);
3843 Append_To (Exprs, Expr);
3844
3845 -- Otherwise we have an OK ELSIF
3846
3847 else
3848 Expr := P_If_Expression_Internal (Eptr, Expr);
3849 Set_Is_Elsif (Expr);
3850 Append_To (Exprs, Expr);
3851 end if;
3852
3853 -- Scan out ELSE phrase if present
3854
3855 elsif Token = Tok_Else then
3856
3857 -- Scan out ELSE expression
3858
3859 Scan; -- Past ELSE
3860 Append_To (Exprs, P_Expression);
3861
3862 -- Skip redundant ELSE parts
3863
3864 while Token = Tok_Else loop
3865 Error_Msg_SC ("only one ELSE part is allowed");
3866 Scan; -- past ELSE
3867 Discard_Junk_Node (P_Expression);
3868 end loop;
3869
3870 -- Two expression case (implied True, filled in during semantics)
3871
3872 else
3873 null;
3874 end if;
3875
3876 -- If we have an END IF, diagnose as not needed
3877
3878 if Token = Tok_End then
3879 Error_Msg_SC ("`END IF` not allowed at end of if expression");
3880 Scan; -- past END
3881
3882 if Token = Tok_If then
3883 Scan; -- past IF;
3884 end if;
3885 end if;
3886
3887 -- Return the If_Expression node
3888
3889 return Make_If_Expression (Loc, Expressions => Exprs);
3890 end P_If_Expression_Internal;
3891
3892 -- Local variables
3893
3894 Loc : constant Source_Ptr := Token_Ptr;
3895 If_Expr : Node_Id;
3896
3897 -- Start of processing for P_If_Expression
3898
3899 begin
3900 Error_Msg_Ada_2012_Feature ("|if expression", Token_Ptr);
3901 Scan; -- past IF
3902 Inside_If_Expression := Inside_If_Expression + 1;
3903 If_Expr := P_If_Expression_Internal (Loc, P_Expression);
3904 Inside_If_Expression := Inside_If_Expression - 1;
3905 return If_Expr;
3906 end P_If_Expression;
3907
3908 --------------------------
3909 -- P_Declare_Expression --
3910 --------------------------
3911
3912 -- DECLARE_EXPRESSION ::=
3913 -- DECLARE {DECLARE_ITEM}
3914 -- begin BODY_EXPRESSION
3915
3916 -- DECLARE_ITEM ::= OBJECT_DECLARATION
3917 -- | OBJECT_RENAMING_DECLARATION
3918
3919 function P_Declare_Expression return Node_Id is
3920 Loc : constant Source_Ptr := Token_Ptr;
3921 begin
3922 Scan; -- past DECLARE
3923
3924 declare
3925 Actions : constant List_Id := P_Basic_Declarative_Items
3926 (Declare_Expression => True);
3927 -- Most declarative items allowed by P_Basic_Declarative_Items are
3928 -- illegal; semantic analysis will deal with that.
3929 begin
3930 if Token = Tok_Begin then
3931 Scan;
3932 else
3933 Error_Msg_SC -- CODEFIX
3934 ("BEGIN expected!");
3935 end if;
3936
3937 declare
3938 Expression : constant Node_Id := P_Expression;
3939 Result : constant Node_Id :=
3940 Make_Expression_With_Actions (Loc, Actions, Expression);
3941 begin
3942 Error_Msg_Ada_2022_Feature ("declare expression", Loc);
3943
3944 return Result;
3945 end;
3946 end;
3947 end P_Declare_Expression;
3948
3949 -----------------------
3950 -- P_Membership_Test --
3951 -----------------------
3952
3953 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERSHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3954 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3955
3956 procedure P_Membership_Test (N : Node_Id) is
3957 Alt : constant Node_Id :=
3958 P_Range_Or_Subtype_Mark
3959 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3960
3961 begin
3962 -- Set case
3963
3964 if Token = Tok_Vertical_Bar then
3965 Error_Msg_Ada_2012_Feature ("set notation", Token_Ptr);
3966 Set_Alternatives (N, New_List (Alt));
3967
3968 -- Loop to accumulate alternatives
3969
3970 while Token = Tok_Vertical_Bar loop
3971 Scan; -- past vertical bar
3972 Append_To
3973 (Alternatives (N),
3974 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3975 end loop;
3976
3977 -- Not set case
3978
3979 else
3980 Set_Right_Opnd (N, Alt);
3981 end if;
3982 end P_Membership_Test;
3983
3984 -----------------------------
3985 -- P_Unparen_Cond_Expr_Etc --
3986 -----------------------------
3987
3988 function P_Unparen_Cond_Expr_Etc return Node_Id is
3989 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3990
3991 Result : Node_Id;
3992 Scan_State : Saved_Scan_State;
3993
3994 begin
3995 -- Case expression
3996
3997 if Token = Tok_Case then
3998 Result := P_Case_Expression;
3999
4000 if not (Lparen and then Token = Tok_Right_Paren) then
4001 Error_Msg_N ("case expression must be parenthesized!", Result);
4002 end if;
4003
4004 -- If expression
4005
4006 elsif Token = Tok_If then
4007 Result := P_If_Expression;
4008
4009 if not (Lparen and then Token = Tok_Right_Paren) then
4010 Error_Msg_N ("if expression must be parenthesized!", Result);
4011 end if;
4012
4013 -- Quantified expression or iterated component association
4014
4015 elsif Token = Tok_For then
4016
4017 Save_Scan_State (Scan_State);
4018 Scan; -- past FOR
4019
4020 if Token in Tok_All | Tok_Some then
4021 Restore_Scan_State (Scan_State);
4022 Result := P_Quantified_Expression;
4023
4024 if not (Lparen and then Token = Tok_Right_Paren) then
4025 Error_Msg_N
4026 ("quantified expression must be parenthesized!", Result);
4027 end if;
4028
4029 -- If no quantifier keyword, this is an iterated component in
4030 -- an aggregate or an ill-formed quantified expression.
4031
4032 else
4033 Restore_Scan_State (Scan_State);
4034 Result := P_Iterated_Component_Association;
4035
4036 if not (Lparen and then Token = Tok_Right_Paren) then
4037 Error_Msg_N
4038 ("construct must be parenthesized!", Result);
4039 end if;
4040 end if;
4041
4042 -- Declare expression
4043
4044 elsif Token = Tok_Declare then
4045 Result := P_Declare_Expression;
4046
4047 if not (Lparen and then Token = Tok_Right_Paren) then
4048 Error_Msg_N ("declare expression must be parenthesized!", Result);
4049 end if;
4050
4051 -- No other possibility should exist (caller was supposed to check)
4052
4053 else
4054 raise Program_Error;
4055 end if;
4056
4057 -- Return expression (possibly after having given message)
4058
4059 return Result;
4060 end P_Unparen_Cond_Expr_Etc;
4061
4062 end Ch4;