Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / par-ch4.adb
blob2505eb629ab90bce7dc02c4c776b922296311330
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-2023, 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 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
30 with Stringt; use Stringt;
32 separate (Par)
33 package body Ch4 is
35 -- Attributes that cannot have arguments
37 Is_Parameterless_Attribute : constant Attribute_Class_Array :=
38 (Attribute_Base => True,
39 Attribute_Body_Version => True,
40 Attribute_Class => True,
41 Attribute_External_Tag => True,
42 Attribute_Img => True,
43 Attribute_Loop_Entry => True,
44 Attribute_Old => True,
45 Attribute_Result => True,
46 Attribute_Stub_Type => True,
47 Attribute_Version => True,
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)).
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.
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.
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
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;
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;
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.
98 procedure Check_Bad_Exp;
99 -- Called after scanning a**b, posts error if ** detected
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.
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.
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.
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.
124 -------------------------
125 -- Bad_Range_Attribute --
126 -------------------------
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;
134 -------------------
135 -- Check_Bad_Exp --
136 -------------------
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;
148 --------------------------
149 -- 4.1 Name (also 6.4) --
150 --------------------------
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
159 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
161 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
163 -- EXPLICIT_DEREFERENCE ::= NAME . all
165 -- IMPLICIT_DEREFERENCE ::= NAME
167 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
169 -- SLICE ::= PREFIX (DISCRETE_RANGE)
171 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
173 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
175 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
177 -- ATTRIBUTE_DESIGNATOR ::=
178 -- IDENTIFIER [(static_EXPRESSION)]
179 -- | access | delta | digits
181 -- FUNCTION_CALL ::=
182 -- function_NAME
183 -- | function_PREFIX ACTUAL_PARAMETER_PART
185 -- ACTUAL_PARAMETER_PART ::=
186 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
188 -- PARAMETER_ASSOCIATION ::=
189 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
191 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
193 -- TARGET_NAME ::= @ (AI12-0125-3: abbreviation for LHS)
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.
198 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
200 -- Error recovery: can raise Error_Resync
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.
207 -- Error recovery: can raise Error_Resync
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;
218 Arg_List : List_Id := No_List; -- kill junk warning
219 Attr_Name : Name_Id := No_Name; -- kill junk warning
221 begin
222 -- Case of not a name
224 if Token not in Token_Class_Name then
226 -- If it looks like start of expression, complain and scan expression
228 if Token in Token_Class_Literal | Tok_Left_Paren then
229 Error_Msg_SC ("name expected");
230 return P_Expression;
232 -- Otherwise some other junk, not much we can do
234 else
235 Error_Msg_AP ("name expected");
236 raise Error_Resync;
237 end if;
238 end if;
240 -- Loop through designators in qualified name
241 -- AI12-0125 : target_name
243 if Token = Tok_At_Sign then
244 Scan_Reserved_Identifier (Force_Msg => False);
246 if Present (Current_Assign_Node) then
247 Set_Has_Target_Names (Current_Assign_Node);
248 end if;
249 end if;
251 Name_Node := Token_Node;
253 loop
254 Scan; -- past designator
255 exit when Token /= Tok_Dot;
256 Save_Scan_State (Scan_State); -- at dot
257 Scan; -- past dot
259 -- If we do not have another designator after the dot, then join
260 -- the normal circuit to handle a dot extension (may be .all or
261 -- character literal case). Otherwise loop back to scan the next
262 -- designator.
264 if Token not in Token_Class_Desig then
265 goto Scan_Name_Extension_Dot;
266 else
267 Prefix_Node := Name_Node;
268 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
269 Set_Prefix (Name_Node, Prefix_Node);
270 Set_Selector_Name (Name_Node, Token_Node);
271 end if;
272 end loop;
274 -- We have now scanned out a qualified designator. If the last token is
275 -- an operator symbol, then we certainly do not have the Snam case, so
276 -- we can just use the normal name extension check circuit
278 if Prev_Token = Tok_Operator_Symbol then
279 goto Scan_Name_Extension;
280 end if;
282 -- We have scanned out a qualified simple name, check for name
283 -- extension. Note that we know there is no dot here at this stage,
284 -- so the only possible cases of name extension are apostrophe followed
285 -- by '(' or '['.
287 if Token = Tok_Apostrophe then
288 Save_Scan_State (Scan_State); -- at apostrophe
289 Scan; -- past apostrophe
291 -- Qualified expression in Ada 2012 mode (treated as a name)
293 if Ada_Version >= Ada_2012
294 and then Token in Tok_Left_Paren | Tok_Left_Bracket
295 then
296 goto Scan_Name_Extension_Apostrophe;
298 -- If left paren not in Ada 2012, then it is not part of the name,
299 -- since qualified expressions are not names in prior versions of
300 -- Ada, so return with Token backed up to point to the apostrophe.
301 -- The treatment for the range attribute is similar (we do not
302 -- consider x'range to be a name in this grammar).
304 elsif Token in Tok_Left_Paren | Tok_Range then
305 Restore_Scan_State (Scan_State); -- to apostrophe
306 Expr_Form := EF_Simple_Name;
307 return Name_Node;
309 -- Otherwise we have the case of a name extended by an attribute
311 else
312 goto Scan_Name_Extension_Apostrophe;
313 end if;
315 -- Check case of qualified simple name extended by a left parenthesis
317 elsif Token = Tok_Left_Paren then
318 Scan; -- past left paren
319 goto Scan_Name_Extension_Left_Paren;
321 -- Otherwise the qualified simple name is not extended, so return
323 else
324 Expr_Form := EF_Simple_Name;
325 return Name_Node;
326 end if;
328 -- Loop scanning past name extensions. A label is used for control
329 -- transfer for this loop for ease of interfacing with the finite state
330 -- machine in the parenthesis scanning circuit, and also to allow for
331 -- passing in control to the appropriate point from the above code.
333 <<Scan_Name_Extension>>
335 -- Character literal used as name cannot be extended. Also this
336 -- cannot be a call, since the name for a call must be a designator.
337 -- Return in these cases, or if there is no name extension
339 if Token not in Token_Class_Namext
340 or else Prev_Token = Tok_Char_Literal
341 then
342 Expr_Form := EF_Name;
343 return Name_Node;
344 end if;
346 -- Merge here when we know there is a name extension
348 <<Scan_Name_Extension_OK>>
350 case Token is
351 when Tok_Left_Paren =>
352 Scan; -- past left paren
353 goto Scan_Name_Extension_Left_Paren;
355 when Tok_Apostrophe =>
356 Save_Scan_State (Scan_State); -- at apostrophe
357 Scan; -- past apostrophe
358 goto Scan_Name_Extension_Apostrophe;
360 when Tok_Dot =>
361 Save_Scan_State (Scan_State); -- at dot
362 Scan; -- past dot
363 goto Scan_Name_Extension_Dot;
365 when others => raise Program_Error;
366 end case;
368 -- Case of name extended by dot (selection), dot is already skipped
369 -- and the scan state at the point of the dot is saved in Scan_State.
371 <<Scan_Name_Extension_Dot>>
373 -- Explicit dereference case
375 if Token = Tok_All then
376 Prefix_Node := Name_Node;
377 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
378 Set_Prefix (Name_Node, Prefix_Node);
379 Scan; -- past ALL
380 goto Scan_Name_Extension;
382 -- Selected component case
384 elsif Token in Token_Class_Name then
385 Prefix_Node := Name_Node;
386 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
387 Set_Prefix (Name_Node, Prefix_Node);
388 Set_Selector_Name (Name_Node, Token_Node);
389 Scan; -- past selector
390 goto Scan_Name_Extension;
392 -- Reserved identifier as selector
394 elsif Is_Reserved_Identifier then
395 Scan_Reserved_Identifier (Force_Msg => False);
396 Prefix_Node := Name_Node;
397 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
398 Set_Prefix (Name_Node, Prefix_Node);
399 Set_Selector_Name (Name_Node, Token_Node);
400 Scan; -- past identifier used as selector
401 goto Scan_Name_Extension;
403 -- If dot is at end of line and followed by nothing legal,
404 -- then assume end of name and quit (dot will be taken as
405 -- an incorrect form of some other punctuation by our caller).
407 elsif Token_Is_At_Start_Of_Line then
408 Restore_Scan_State (Scan_State);
409 return Name_Node;
411 -- Here if nothing legal after the dot
413 else
414 Error_Msg_AP ("selector expected");
415 raise Error_Resync;
416 end if;
418 -- Here for an apostrophe as name extension. The scan position at the
419 -- apostrophe has already been saved, and the apostrophe scanned out.
421 <<Scan_Name_Extension_Apostrophe>>
423 Scan_Apostrophe : declare
424 function Apostrophe_Should_Be_Semicolon return Boolean;
425 -- Checks for case where apostrophe should probably be
426 -- a semicolon, and if so, gives appropriate message,
427 -- resets the scan pointer to the apostrophe, changes
428 -- the current token to Tok_Semicolon, and returns True.
429 -- Otherwise returns False.
431 ------------------------------------
432 -- Apostrophe_Should_Be_Semicolon --
433 ------------------------------------
435 function Apostrophe_Should_Be_Semicolon return Boolean is
436 begin
437 if Token_Is_At_Start_Of_Line then
438 Restore_Scan_State (Scan_State); -- to apostrophe
439 Error_Msg_SC ("|""''"" should be "";""");
440 Token := Tok_Semicolon;
441 return True;
442 else
443 return False;
444 end if;
445 end Apostrophe_Should_Be_Semicolon;
447 -- Start of processing for Scan_Apostrophe
449 begin
450 -- Check for qualified expression case in Ada 2012 mode
452 if Ada_Version >= Ada_2012
453 and then Token in Tok_Left_Paren | Tok_Left_Bracket
454 then
455 Name_Node := P_Qualified_Expression (Name_Node);
456 goto Scan_Name_Extension;
458 -- If range attribute after apostrophe, then return with Token
459 -- pointing to the apostrophe. Note that in this case the prefix
460 -- need not be a simple name (cases like A.all'range). Similarly
461 -- if there is a left paren after the apostrophe, then we also
462 -- return with Token pointing to the apostrophe (this is the
463 -- aggregate case, or some error case).
465 elsif Token in Tok_Range | Tok_Left_Paren then
466 Restore_Scan_State (Scan_State); -- to apostrophe
467 Expr_Form := EF_Name;
468 return Name_Node;
470 -- Here for cases where attribute designator is an identifier
472 elsif Token = Tok_Identifier then
473 Attr_Name := Token_Name;
475 if not Is_Attribute_Name (Attr_Name) then
476 if Apostrophe_Should_Be_Semicolon then
477 Expr_Form := EF_Name;
478 return Name_Node;
480 -- Here for a bad attribute name
482 else
483 Signal_Bad_Attribute;
484 Scan; -- past bad identifier
486 if Token = Tok_Left_Paren then
487 Scan; -- past left paren
489 loop
490 Discard_Junk_Node (P_Expression_If_OK);
491 exit when not Comma_Present;
492 end loop;
494 T_Right_Paren;
495 end if;
497 return Error;
498 end if;
499 end if;
501 if Style_Check then
502 Style.Check_Attribute_Name (False);
503 end if;
505 -- Here for case of attribute designator is not an identifier
507 else
508 if Token = Tok_Delta then
509 Attr_Name := Name_Delta;
511 elsif Token = Tok_Digits then
512 Attr_Name := Name_Digits;
514 elsif Token = Tok_Access then
515 Attr_Name := Name_Access;
517 elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
518 Attr_Name := Name_Mod;
520 elsif Apostrophe_Should_Be_Semicolon then
521 Expr_Form := EF_Name;
522 return Name_Node;
524 else
525 Error_Msg_AP ("attribute designator expected");
526 raise Error_Resync;
527 end if;
529 if Style_Check then
530 Style.Check_Attribute_Name (True);
531 end if;
532 end if;
534 -- We come here with an OK attribute scanned, and corresponding
535 -- Attribute identifier node stored in Ident_Node.
537 Prefix_Node := Name_Node;
538 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
539 Scan; -- past attribute designator
540 Set_Prefix (Name_Node, Prefix_Node);
541 Set_Attribute_Name (Name_Node, Attr_Name);
543 -- Scan attribute arguments/designator. We skip this if we know
544 -- that the attribute cannot have an argument (see documentation
545 -- of Is_Parameterless_Attribute for further details).
547 if Token = Tok_Left_Paren
548 and then not
549 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
550 then
551 -- Attribute Update contains an array or record association
552 -- list which provides new values for various components or
553 -- elements. The list is parsed as an aggregate, and we get
554 -- better error handling by knowing that in the parser.
556 if Attr_Name = Name_Update then
557 Set_Expressions (Name_Node, New_List);
558 Append (P_Aggregate, Expressions (Name_Node));
560 -- All other cases of parsing attribute arguments
562 else
563 Set_Expressions (Name_Node, New_List);
564 Scan; -- past left paren
566 loop
567 declare
568 Expr : constant Node_Id := P_Expression_If_OK;
569 Rnam : Node_Id;
571 begin
572 -- Case of => for named notation
574 if Token = Tok_Arrow then
576 -- Named notation allowed only for the special
577 -- case of System'Restriction_Set (No_Dependence =>
578 -- unit_NAME), in which case construct a parameter
579 -- assocation node and append to the arguments.
581 if Attr_Name = Name_Restriction_Set
582 and then Nkind (Expr) = N_Identifier
583 and then Chars (Expr) = Name_No_Dependence
584 then
585 Scan; -- past arrow
586 Rnam := P_Name;
587 Append_To (Expressions (Name_Node),
588 Make_Parameter_Association (Sloc (Rnam),
589 Selector_Name => Expr,
590 Explicit_Actual_Parameter => Rnam));
591 exit;
593 -- For all other cases named notation is illegal
595 else
596 Error_Msg_SC
597 ("named parameters not permitted "
598 & "for attributes");
599 Scan; -- past junk arrow
600 end if;
602 -- Here for normal case (not => for named parameter)
604 else
605 -- Special handling for 'Image in Ada 2012, where
606 -- the attribute can be parameterless and its value
607 -- can be the prefix of a slice. Rewrite name as a
608 -- slice, Expr is its low bound.
610 if Token = Tok_Dot_Dot
611 and then Attr_Name = Name_Image
612 and then Ada_Version >= Ada_2012
613 then
614 Set_Expressions (Name_Node, No_List);
615 Prefix_Node := Name_Node;
616 Name_Node :=
617 New_Node (N_Slice, Sloc (Prefix_Node));
618 Set_Prefix (Name_Node, Prefix_Node);
619 Range_Node := New_Node (N_Range, Token_Ptr);
620 Set_Low_Bound (Range_Node, Expr);
621 Scan; -- past ..
622 Expr_Node := P_Expression;
623 Check_Simple_Expression (Expr_Node);
624 Set_High_Bound (Range_Node, Expr_Node);
625 Set_Discrete_Range (Name_Node, Range_Node);
626 T_Right_Paren;
628 goto Scan_Name_Extension;
630 else
631 Append (Expr, Expressions (Name_Node));
632 exit when not Comma_Present;
633 end if;
634 end if;
635 end;
636 end loop;
638 T_Right_Paren;
639 end if;
640 end if;
642 goto Scan_Name_Extension;
643 end Scan_Apostrophe;
645 -- Here for left parenthesis extending name (left paren skipped)
647 <<Scan_Name_Extension_Left_Paren>>
649 -- We now have to scan through a list of items, terminated by a
650 -- right parenthesis. The scan is handled by a finite state
651 -- machine. The possibilities are:
653 -- (discrete_range)
655 -- This is a slice. This case is handled in LP_State_Init
657 -- (expression, expression, ..)
659 -- This is interpreted as an indexed component, i.e. as a
660 -- case of a name which can be extended in the normal manner.
661 -- This case is handled by LP_State_Name or LP_State_Expr.
663 -- Note: if and case expressions (without an extra level of
664 -- parentheses) are permitted in this context).
666 -- (..., identifier => expression , ...)
668 -- If there is at least one occurrence of identifier => (but
669 -- none of the other cases apply), then we have a call.
671 -- Test for Id => case
673 if Token = Tok_Identifier then
674 Save_Scan_State (Scan_State); -- at Id
675 Scan; -- past Id
677 -- Test for => (allow := as an error substitute)
679 if Token in Tok_Arrow | Tok_Colon_Equal then
680 Restore_Scan_State (Scan_State); -- to Id
681 Arg_List := New_List;
682 goto LP_State_Call;
684 else
685 Restore_Scan_State (Scan_State); -- to Id
686 end if;
687 end if;
689 -- Here we have an expression after all
691 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
693 -- Check cases of discrete range for a slice
695 -- First possibility: Range_Attribute_Reference
697 if Expr_Form = EF_Range_Attr then
698 Range_Node := Expr_Node;
700 -- Second possibility: Simple_expression .. Simple_expression
702 elsif Token = Tok_Dot_Dot then
703 Check_Simple_Expression (Expr_Node);
704 Range_Node := New_Node (N_Range, Token_Ptr);
705 Set_Low_Bound (Range_Node, Expr_Node);
706 Scan; -- past ..
707 Expr_Node := P_Expression;
708 Check_Simple_Expression (Expr_Node);
709 Set_High_Bound (Range_Node, Expr_Node);
711 -- Third possibility: Type_name range Range
713 elsif Token = Tok_Range then
714 if Expr_Form /= EF_Simple_Name then
715 Error_Msg_SC ("subtype mark must precede RANGE");
716 raise Error_Resync;
717 end if;
719 Range_Node := P_Subtype_Indication (Expr_Node);
721 -- Otherwise we just have an expression. It is true that we might
722 -- have a subtype mark without a range constraint but this case
723 -- is syntactically indistinguishable from the expression case.
725 else
726 Arg_List := New_List;
727 goto LP_State_Expr;
728 end if;
730 -- Fall through here with unmistakable Discrete range scanned,
731 -- which means that we definitely have the case of a slice. The
732 -- Discrete range is in Range_Node.
734 if Token = Tok_Comma then
735 Error_Msg_SC ("slice cannot have more than one dimension");
736 raise Error_Resync;
738 elsif Token /= Tok_Right_Paren then
739 if Token = Tok_Arrow then
741 -- This may be an aggregate that is missing a qualification
743 Error_Msg_SC
744 ("context of aggregate must be a qualified expression");
745 raise Error_Resync;
747 else
748 T_Right_Paren;
749 raise Error_Resync;
750 end if;
752 else
753 Scan; -- past right paren
754 Prefix_Node := Name_Node;
755 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
756 Set_Prefix (Name_Node, Prefix_Node);
757 Set_Discrete_Range (Name_Node, Range_Node);
759 -- An operator node is legal as a prefix to other names,
760 -- but not for a slice.
762 if Nkind (Prefix_Node) = N_Operator_Symbol then
763 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
764 end if;
766 -- If we have a name extension, go scan it
768 if Token in Token_Class_Namext then
769 goto Scan_Name_Extension_OK;
771 -- Otherwise return (a slice is a name, but is not a call)
773 else
774 Expr_Form := EF_Name;
775 return Name_Node;
776 end if;
777 end if;
779 -- In LP_State_Expr, we have scanned one or more expressions, and
780 -- so we have a call or an indexed component which is a name. On
781 -- entry we have the expression just scanned in Expr_Node and
782 -- Arg_List contains the list of expressions encountered so far
784 <<LP_State_Expr>>
785 Append (Expr_Node, Arg_List);
787 if Token = Tok_Arrow then
788 Error_Msg
789 ("expect identifier in parameter association", Sloc (Expr_Node));
790 Scan; -- past arrow
792 elsif not Comma_Present then
793 T_Right_Paren;
795 Prefix_Node := Name_Node;
796 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
797 Set_Prefix (Name_Node, Prefix_Node);
798 Set_Expressions (Name_Node, Arg_List);
800 goto Scan_Name_Extension;
801 end if;
803 -- Comma present (and scanned out), test for identifier => case
804 -- Test for identifier => case
806 if Token = Tok_Identifier then
807 Save_Scan_State (Scan_State); -- at Id
808 Scan; -- past Id
810 -- Test for => (allow := as error substitute)
812 if Token in Tok_Arrow | Tok_Colon_Equal then
813 Restore_Scan_State (Scan_State); -- to Id
814 goto LP_State_Call;
816 -- Otherwise it's just an expression after all, so backup
818 else
819 Restore_Scan_State (Scan_State); -- to Id
820 end if;
821 end if;
823 -- Here we have an expression after all, so stay in this state
825 Expr_Node := P_Expression_If_OK;
826 goto LP_State_Expr;
828 -- LP_State_Call corresponds to the situation in which at least one
829 -- instance of Id => Expression has been encountered, so we know that
830 -- we do not have a name, but rather a call. We enter it with the
831 -- scan pointer pointing to the next argument to scan, and Arg_List
832 -- containing the list of arguments scanned so far.
834 <<LP_State_Call>>
836 -- Test for case of Id => Expression (named parameter)
838 if Token = Tok_Identifier then
839 Save_Scan_State (Scan_State); -- at Id
840 Ident_Node := Token_Node;
841 Scan; -- past Id
843 -- Deal with => (allow := as incorrect substitute)
845 if Token in Tok_Arrow | Tok_Colon_Equal then
846 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
847 Set_Selector_Name (Arg_Node, Ident_Node);
848 T_Arrow;
849 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
850 Append (Arg_Node, Arg_List);
852 -- If a comma follows, go back and scan next entry
854 if Comma_Present then
855 goto LP_State_Call;
857 -- Otherwise we have the end of a call
859 else
860 Prefix_Node := Name_Node;
861 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
862 Set_Name (Name_Node, Prefix_Node);
863 Set_Parameter_Associations (Name_Node, Arg_List);
864 T_Right_Paren;
866 if Token in Token_Class_Namext then
867 goto Scan_Name_Extension_OK;
869 -- This is a case of a call which cannot be a name
871 else
872 Expr_Form := EF_Name;
873 return Name_Node;
874 end if;
875 end if;
877 -- Not named parameter: Id started an expression after all
879 else
880 Restore_Scan_State (Scan_State); -- to Id
881 end if;
882 end if;
884 -- Here if entry did not start with Id => which means that it
885 -- is a positional parameter, which is not allowed, since we
886 -- have seen at least one named parameter already.
888 Error_Msg_SC
889 ("positional parameter association " &
890 "not allowed after named one");
892 Expr_Node := P_Expression_If_OK;
894 -- Leaving the '>' in an association is not unusual, so suggest
895 -- a possible fix.
897 if Nkind (Expr_Node) = N_Op_Eq then
898 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
899 end if;
901 -- We go back to scanning out expressions, so that we do not get
902 -- multiple error messages when several positional parameters
903 -- follow a named parameter.
905 goto LP_State_Expr;
907 -- End of treatment for name extensions starting with left paren
909 -- End of loop through name extensions
911 end P_Name;
913 -- This function parses a restricted form of Names which are either
914 -- designators, or designators preceded by a sequence of prefixes
915 -- that are direct names.
917 -- Error recovery: cannot raise Error_Resync
919 function P_Function_Name return Node_Id is
920 Designator_Node : Node_Id;
921 Prefix_Node : Node_Id;
922 Selector_Node : Node_Id;
923 Dot_Sloc : Source_Ptr := No_Location;
925 begin
926 -- Prefix_Node is set to the gathered prefix so far, Empty means that
927 -- no prefix has been scanned. This allows us to build up the result
928 -- in the required right recursive manner.
930 Prefix_Node := Empty;
932 -- Loop through prefixes
934 loop
935 Designator_Node := Token_Node;
937 if Token not in Token_Class_Desig then
938 return P_Identifier; -- let P_Identifier issue the error message
940 else -- Token in Token_Class_Desig
941 Scan; -- past designator
942 exit when Token /= Tok_Dot;
943 end if;
945 -- Here at a dot, with token just before it in Designator_Node
947 if No (Prefix_Node) then
948 Prefix_Node := Designator_Node;
949 else
950 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
951 Set_Prefix (Selector_Node, Prefix_Node);
952 Set_Selector_Name (Selector_Node, Designator_Node);
953 Prefix_Node := Selector_Node;
954 end if;
956 Dot_Sloc := Token_Ptr;
957 Scan; -- past dot
958 end loop;
960 -- Fall out of the loop having just scanned a designator
962 if No (Prefix_Node) then
963 return Designator_Node;
964 else
965 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
966 Set_Prefix (Selector_Node, Prefix_Node);
967 Set_Selector_Name (Selector_Node, Designator_Node);
968 return Selector_Node;
969 end if;
971 exception
972 when Error_Resync =>
973 return Error;
974 end P_Function_Name;
976 -- This function parses a restricted form of Names which are either
977 -- identifiers, or identifiers preceded by a sequence of prefixes
978 -- that are direct names.
980 -- Error recovery: cannot raise Error_Resync
982 function P_Qualified_Simple_Name return Node_Id is
983 Designator_Node : Node_Id;
984 Prefix_Node : Node_Id;
985 Selector_Node : Node_Id;
986 Dot_Sloc : Source_Ptr := No_Location;
988 begin
989 -- Prefix node is set to the gathered prefix so far, Empty means that
990 -- no prefix has been scanned. This allows us to build up the result
991 -- in the required right recursive manner.
993 Prefix_Node := Empty;
995 -- Loop through prefixes
997 loop
998 Designator_Node := Token_Node;
1000 if Token = Tok_Identifier then
1001 Scan; -- past identifier
1002 exit when Token /= Tok_Dot;
1004 elsif Token not in Token_Class_Desig then
1005 return P_Identifier; -- let P_Identifier issue the error message
1007 else
1008 Scan; -- past designator
1010 if Token /= Tok_Dot then
1011 Error_Msg_SP ("identifier expected");
1012 return Error;
1013 end if;
1014 end if;
1016 -- Here at a dot, with token just before it in Designator_Node
1018 if No (Prefix_Node) then
1019 Prefix_Node := Designator_Node;
1020 else
1021 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1022 Set_Prefix (Selector_Node, Prefix_Node);
1023 Set_Selector_Name (Selector_Node, Designator_Node);
1024 Prefix_Node := Selector_Node;
1025 end if;
1027 Dot_Sloc := Token_Ptr;
1028 Scan; -- past dot
1029 end loop;
1031 -- Fall out of the loop having just scanned an identifier
1033 if No (Prefix_Node) then
1034 return Designator_Node;
1035 else
1036 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1037 Set_Prefix (Selector_Node, Prefix_Node);
1038 Set_Selector_Name (Selector_Node, Designator_Node);
1039 return Selector_Node;
1040 end if;
1042 exception
1043 when Error_Resync =>
1044 return Error;
1045 end P_Qualified_Simple_Name;
1047 -- This procedure differs from P_Qualified_Simple_Name only in that it
1048 -- raises Error_Resync if any error is encountered. It only returns after
1049 -- scanning a valid qualified simple name.
1051 -- Error recovery: can raise Error_Resync
1053 function P_Qualified_Simple_Name_Resync return Node_Id is
1054 Designator_Node : Node_Id;
1055 Prefix_Node : Node_Id;
1056 Selector_Node : Node_Id;
1057 Dot_Sloc : Source_Ptr := No_Location;
1059 begin
1060 Prefix_Node := Empty;
1062 -- Loop through prefixes
1064 loop
1065 Designator_Node := Token_Node;
1067 if Token = Tok_Identifier then
1068 Scan; -- past identifier
1069 exit when Token /= Tok_Dot;
1071 elsif Token not in Token_Class_Desig then
1072 Discard_Junk_Node (P_Identifier); -- to issue the error message
1073 raise Error_Resync;
1075 else
1076 Scan; -- past designator
1078 if Token /= Tok_Dot then
1079 Error_Msg_SP ("identifier expected");
1080 raise Error_Resync;
1081 end if;
1082 end if;
1084 -- Here at a dot, with token just before it in Designator_Node
1086 if No (Prefix_Node) then
1087 Prefix_Node := Designator_Node;
1088 else
1089 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1090 Set_Prefix (Selector_Node, Prefix_Node);
1091 Set_Selector_Name (Selector_Node, Designator_Node);
1092 Prefix_Node := Selector_Node;
1093 end if;
1095 Dot_Sloc := Token_Ptr;
1096 Scan; -- past period
1097 end loop;
1099 -- Fall out of the loop having just scanned an identifier
1101 if No (Prefix_Node) then
1102 return Designator_Node;
1103 else
1104 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1105 Set_Prefix (Selector_Node, Prefix_Node);
1106 Set_Selector_Name (Selector_Node, Designator_Node);
1107 return Selector_Node;
1108 end if;
1109 end P_Qualified_Simple_Name_Resync;
1111 ----------------------
1112 -- 4.1 Direct_Name --
1113 ----------------------
1115 -- Parsed by P_Name and other functions in section 4.1
1117 -----------------
1118 -- 4.1 Prefix --
1119 -----------------
1121 -- Parsed by P_Name (4.1)
1123 -------------------------------
1124 -- 4.1 Explicit Dereference --
1125 -------------------------------
1127 -- Parsed by P_Name (4.1)
1129 -------------------------------
1130 -- 4.1 Implicit_Dereference --
1131 -------------------------------
1133 -- Parsed by P_Name (4.1)
1135 ----------------------------
1136 -- 4.1 Indexed Component --
1137 ----------------------------
1139 -- Parsed by P_Name (4.1)
1141 ----------------
1142 -- 4.1 Slice --
1143 ----------------
1145 -- Parsed by P_Name (4.1)
1147 -----------------------------
1148 -- 4.1 Selected_Component --
1149 -----------------------------
1151 -- Parsed by P_Name (4.1)
1153 ------------------------
1154 -- 4.1 Selector Name --
1155 ------------------------
1157 -- Parsed by P_Name (4.1)
1159 ------------------------------
1160 -- 4.1 Attribute Reference --
1161 ------------------------------
1163 -- Parsed by P_Name (4.1)
1165 -------------------------------
1166 -- 4.1 Attribute Designator --
1167 -------------------------------
1169 -- Parsed by P_Name (4.1)
1171 --------------------------------------
1172 -- 4.1.4 Range Attribute Reference --
1173 --------------------------------------
1175 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1177 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1179 -- In the grammar, a RANGE attribute is simply a name, but its use is
1180 -- highly restricted, so in the parser, we do not regard it as a name.
1181 -- Instead, P_Name returns without scanning the 'RANGE part of the
1182 -- attribute, and the caller uses the following function to construct
1183 -- a range attribute in places where it is appropriate.
1185 -- Note that RANGE here is treated essentially as an identifier,
1186 -- rather than a reserved word.
1188 -- The caller has parsed the prefix, i.e. a name, and Token points to
1189 -- the apostrophe. The token after the apostrophe is known to be RANGE
1190 -- at this point. The prefix node becomes the prefix of the attribute.
1192 -- Error_Recovery: Cannot raise Error_Resync
1194 function P_Range_Attribute_Reference
1195 (Prefix_Node : Node_Id)
1196 return Node_Id
1198 Attr_Node : Node_Id;
1200 begin
1201 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1202 Set_Prefix (Attr_Node, Prefix_Node);
1203 Scan; -- past apostrophe
1205 if Style_Check then
1206 Style.Check_Attribute_Name (True);
1207 end if;
1209 Set_Attribute_Name (Attr_Node, Name_Range);
1210 Scan; -- past RANGE
1212 if Token = Tok_Left_Paren then
1213 Scan; -- past left paren
1214 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1215 T_Right_Paren;
1216 end if;
1218 return Attr_Node;
1219 end P_Range_Attribute_Reference;
1221 -------------------------------------
1222 -- P_Reduction_Attribute_Reference --
1223 -------------------------------------
1225 function P_Reduction_Attribute_Reference (S : Node_Id)
1226 return Node_Id
1228 Attr_Node : Node_Id;
1229 Attr_Name : Name_Id;
1231 begin
1232 Attr_Name := Token_Name;
1233 Scan; -- past Reduce
1234 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1235 Set_Attribute_Name (Attr_Node, Attr_Name);
1236 if Attr_Name /= Name_Reduce then
1237 Error_Msg ("Reduce attribute expected", Prev_Token_Ptr);
1238 end if;
1240 Set_Prefix (Attr_Node, S);
1241 Set_Expressions (Attr_Node, New_List);
1242 T_Left_Paren;
1243 Append (P_Name, Expressions (Attr_Node));
1244 T_Comma;
1245 Append (P_Expression, Expressions (Attr_Node));
1246 T_Right_Paren;
1248 return Attr_Node;
1249 end P_Reduction_Attribute_Reference;
1251 ---------------------------------------
1252 -- 4.1.4 Range Attribute Designator --
1253 ---------------------------------------
1255 -- Parsed by P_Range_Attribute_Reference (4.4)
1257 ---------------------------------------------
1258 -- 4.1.4 (2) Reduction_Attribute_Reference --
1259 ---------------------------------------------
1261 -- parsed by P_Reduction_Attribute_Reference
1263 --------------------
1264 -- 4.3 Aggregate --
1265 --------------------
1267 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1269 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1270 -- an aggregate is known to be required (code statement, extension
1271 -- aggregate), in which cases this routine performs the necessary check
1272 -- that we have an aggregate rather than a parenthesized expression
1274 -- Error recovery: can raise Error_Resync
1276 function P_Aggregate return Node_Id is
1277 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1278 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1280 begin
1281 if Nkind (Aggr_Node) /= N_Aggregate
1282 and then
1283 Nkind (Aggr_Node) /= N_Extension_Aggregate
1284 and then Ada_Version < Ada_2022
1285 then
1286 Error_Msg
1287 ("aggregate may not have single positional component", Aggr_Sloc);
1288 return Error;
1289 else
1290 return Aggr_Node;
1291 end if;
1292 end P_Aggregate;
1294 ------------------------------------------------
1295 -- 4.3 Aggregate or Parenthesized Expression --
1296 ------------------------------------------------
1298 -- This procedure parses out either an aggregate or a parenthesized
1299 -- expression (these two constructs are closely related, since a
1300 -- parenthesized expression looks like an aggregate with a single
1301 -- positional component).
1303 -- AGGREGATE ::=
1304 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1306 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1308 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1309 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1310 -- | null record
1312 -- RECORD_COMPONENT_ASSOCIATION ::=
1313 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1315 -- COMPONENT_CHOICE_LIST ::=
1316 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1317 -- | others
1319 -- EXTENSION_AGGREGATE ::=
1320 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1322 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1324 -- ARRAY_AGGREGATE ::=
1325 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1327 -- POSITIONAL_ARRAY_AGGREGATE ::=
1328 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1329 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1330 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1332 -- NAMED_ARRAY_AGGREGATE ::=
1333 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1335 -- PRIMARY ::= (EXPRESSION);
1337 -- Error recovery: can raise Error_Resync
1339 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1340 -- to Ada 2005 limited aggregates (AI-287)
1342 function P_Aggregate_Or_Paren_Expr return Node_Id is
1343 Aggregate_Node : Node_Id;
1344 Expr_List : List_Id;
1345 Assoc_List : List_Id;
1346 Expr_Node : Node_Id;
1347 Lparen_Sloc : Source_Ptr;
1348 Scan_State : Saved_Scan_State;
1350 procedure Box_Error;
1351 -- Called if <> is encountered as positional aggregate element. Issues
1352 -- error message and sets Expr_Node to Error.
1354 function Is_Quantified_Expression return Boolean;
1355 -- The presence of iterated component associations requires a one
1356 -- token lookahead to distinguish it from quantified expressions.
1358 ---------------
1359 -- Box_Error --
1360 ---------------
1362 procedure Box_Error is
1363 begin
1364 Error_Msg_Ada_2005_Extension ("'<'> in aggregate");
1366 -- Ada 2005 (AI-287): The box notation is allowed only with named
1367 -- notation because positional notation might be error prone. For
1368 -- example, in "(X, <>, Y, <>)", there is no type associated with
1369 -- the boxes, so you might not be leaving out the components you
1370 -- thought you were leaving out.
1372 Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1373 Scan; -- past box
1374 Expr_Node := Error;
1375 end Box_Error;
1377 ------------------------------
1378 -- Is_Quantified_Expression --
1379 ------------------------------
1381 function Is_Quantified_Expression return Boolean is
1382 Maybe : Boolean;
1383 Scan_State : Saved_Scan_State;
1385 begin
1386 Save_Scan_State (Scan_State);
1387 Scan; -- past FOR
1388 Maybe := Token in Tok_All | Tok_Some;
1389 Restore_Scan_State (Scan_State); -- to FOR
1390 return Maybe;
1391 end Is_Quantified_Expression;
1393 Start_Token : constant Token_Type := Token;
1394 -- Used to prevent mismatches (...] and [...)
1396 -- Start of processing for P_Aggregate_Or_Paren_Expr
1398 begin
1399 Lparen_Sloc := Token_Ptr;
1400 if Token = Tok_Left_Bracket then
1401 Scan;
1403 -- Special case for null aggregate in Ada 2022
1405 if Token = Tok_Right_Bracket then
1406 Scan; -- past ]
1407 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1408 Set_Expressions (Aggregate_Node, New_List);
1409 Set_Component_Associations (Aggregate_Node, New_List);
1410 Set_Is_Homogeneous_Aggregate (Aggregate_Node);
1411 return Aggregate_Node;
1412 end if;
1413 else
1414 T_Left_Paren;
1415 end if;
1417 -- Note on parentheses count. For cases like an if expression, the
1418 -- parens here really count as real parentheses for the paren count,
1419 -- so we adjust the paren count accordingly after scanning the expr.
1421 -- If expression
1423 if Token = Tok_If then
1424 Expr_Node := P_If_Expression;
1425 T_Right_Paren;
1426 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1427 return Expr_Node;
1429 -- Case expression
1431 elsif Token = Tok_Case then
1432 Expr_Node := P_Case_Expression;
1433 T_Right_Paren;
1434 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1435 return Expr_Node;
1437 -- Quantified expression
1439 elsif Token = Tok_For and then Is_Quantified_Expression then
1440 Expr_Node := P_Quantified_Expression;
1441 T_Right_Paren;
1442 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1443 return Expr_Node;
1445 -- Note: the mechanism used here of rescanning the initial expression
1446 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1447 -- out the discrete choice list.
1449 -- Deal with expression and extension aggregates first
1451 elsif Token /= Tok_Others then
1452 Save_Scan_State (Scan_State); -- at start of expression
1454 -- Deal with (NULL RECORD)
1456 if Token = Tok_Null then
1457 Scan; -- past NULL
1459 if Token = Tok_Record then
1460 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1461 Set_Null_Record_Present (Aggregate_Node, True);
1462 Scan; -- past RECORD
1463 T_Right_Paren;
1464 return Aggregate_Node;
1465 else
1466 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1467 end if;
1469 elsif Token = Tok_For then
1470 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1471 Expr_Node := P_Iterated_Component_Association;
1472 goto Aggregate;
1473 end if;
1475 -- Scan expression, handling box appearing as positional argument
1477 if Token = Tok_Box then
1478 Box_Error;
1479 else
1480 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1481 end if;
1483 -- Extension or Delta aggregate
1485 if Token = Tok_With then
1486 if Nkind (Expr_Node) = N_Attribute_Reference
1487 and then Attribute_Name (Expr_Node) = Name_Range
1488 then
1489 Bad_Range_Attribute (Sloc (Expr_Node));
1490 return Error;
1491 end if;
1493 if Ada_Version = Ada_83 then
1494 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1495 end if;
1497 Scan; -- past WITH
1498 if Token = Tok_Delta then
1499 Scan; -- past DELTA
1500 Aggregate_Node := New_Node (N_Delta_Aggregate, Lparen_Sloc);
1501 Set_Expression (Aggregate_Node, Expr_Node);
1502 Expr_Node := Empty;
1504 goto Aggregate;
1506 else
1507 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1508 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1509 end if;
1511 -- Deal with WITH NULL RECORD case
1513 if Token = Tok_Null then
1514 Save_Scan_State (Scan_State); -- at NULL
1515 Scan; -- past NULL
1517 if Token = Tok_Record then
1518 Scan; -- past RECORD
1519 Set_Null_Record_Present (Aggregate_Node, True);
1520 T_Right_Paren;
1521 return Aggregate_Node;
1523 else
1524 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1525 end if;
1526 end if;
1528 if Token /= Tok_Others then
1529 Save_Scan_State (Scan_State);
1530 Expr_Node := P_Expression;
1531 else
1532 Expr_Node := Empty;
1533 end if;
1535 -- Expression
1537 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1538 if Nkind (Expr_Node) = N_Attribute_Reference
1539 and then Attribute_Name (Expr_Node) = Name_Range
1540 then
1541 Error_Msg
1542 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1543 Scan; -- past right paren
1544 return Expr_Node;
1545 end if;
1547 -- Bump paren count of expression
1549 if Expr_Node /= Error then
1550 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1551 end if;
1553 T_Right_Paren; -- past right paren (error message if none)
1554 return Expr_Node;
1556 -- Normal aggregate
1558 else
1559 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1560 end if;
1562 -- Others
1564 else
1565 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1566 Expr_Node := Empty;
1567 end if;
1569 -- Prepare to scan list of component associations
1570 <<Aggregate>>
1571 Expr_List := No_List; -- don't set yet, maybe all named entries
1572 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1574 -- This loop scans through component associations. On entry to the
1575 -- loop, an expression has been scanned at the start of the current
1576 -- association unless initial token was OTHERS, in which case
1577 -- Expr_Node is set to Empty.
1579 loop
1580 -- Deal with others association first. This is a named association
1582 if No (Expr_Node) then
1583 Append_New (P_Record_Or_Array_Component_Association, Assoc_List);
1585 -- Improper use of WITH
1587 elsif Token = Tok_With then
1588 Error_Msg_SC ("WITH must be preceded by single expression in " &
1589 "extension aggregate");
1590 raise Error_Resync;
1592 -- Range attribute can only appear as part of a discrete choice list
1594 elsif Nkind (Expr_Node) = N_Attribute_Reference
1595 and then Attribute_Name (Expr_Node) = Name_Range
1596 and then Token /= Tok_Arrow
1597 and then Token /= Tok_Vertical_Bar
1598 then
1599 Bad_Range_Attribute (Sloc (Expr_Node));
1600 return Error;
1602 -- Assume positional case if comma, right paren, or literal or
1603 -- identifier or OTHERS follows (the latter cases are missing
1604 -- comma cases). Also assume positional if a semicolon follows,
1605 -- which can happen if there are missing parens.
1606 -- In Ada 2012 and 2022 an iterated association can appear.
1608 elsif Nkind (Expr_Node) in
1609 N_Iterated_Component_Association | N_Iterated_Element_Association
1610 then
1611 Append_New (Expr_Node, Assoc_List);
1613 elsif Token in Tok_Comma | Tok_Right_Paren | Tok_Others
1614 | Token_Class_Lit_Or_Name | Tok_Semicolon
1615 then
1616 if Present (Assoc_List) then
1617 Error_Msg_BC -- CODEFIX
1618 ("""='>"" expected (positional association cannot follow "
1619 & "named association)");
1620 end if;
1622 Append_New (Expr_Node, Expr_List);
1624 -- Check for aggregate followed by left parent, maybe missing comma
1626 elsif Nkind (Expr_Node) = N_Aggregate
1627 and then Token = Tok_Left_Paren
1628 then
1629 T_Comma;
1631 Append_New (Expr_Node, Expr_List);
1633 elsif Token = Tok_Right_Bracket then
1634 Append_New (Expr_Node, Expr_List);
1635 exit;
1637 -- Anything else is assumed to be a named association
1639 else
1640 Restore_Scan_State (Scan_State); -- to start of expression
1642 Append_New (P_Record_Or_Array_Component_Association, Assoc_List);
1643 end if;
1645 exit when not Comma_Present;
1647 -- If we are at an expression terminator, something is seriously
1648 -- wrong, so let's get out now, before we start eating up stuff
1649 -- that doesn't belong to us.
1651 if Token in Token_Class_Eterm and then Token /= Tok_For then
1652 Error_Msg_AP
1653 ("expecting expression or component association");
1654 exit;
1655 end if;
1657 -- Deal with misused box
1659 if Token = Tok_Box then
1660 Box_Error;
1662 -- Otherwise initiate for reentry to top of loop by scanning an
1663 -- initial expression, unless the first token is OTHERS or FOR,
1664 -- which indicates an iterated component association.
1666 elsif Token = Tok_Others then
1667 Expr_Node := Empty;
1669 elsif Token = Tok_For then
1670 Expr_Node := P_Iterated_Component_Association;
1672 else
1673 Save_Scan_State (Scan_State); -- at start of expression
1674 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1676 end if;
1677 end loop;
1679 -- All component associations (positional and named) have been scanned.
1680 -- Scan ] or ) based on Start_Token.
1682 case Start_Token is
1683 when Tok_Left_Bracket =>
1685 Set_Component_Associations (Aggregate_Node, Assoc_List);
1686 Set_Is_Homogeneous_Aggregate (Aggregate_Node);
1687 T_Right_Bracket;
1689 if Token = Tok_Apostrophe then
1690 Scan;
1692 if Token = Tok_Identifier then
1693 return P_Reduction_Attribute_Reference (Aggregate_Node);
1694 end if;
1695 end if;
1696 when Tok_Left_Paren =>
1697 if Nkind (Aggregate_Node) = N_Aggregate then
1698 Set_Is_Parenthesis_Aggregate (Aggregate_Node);
1699 end if;
1701 T_Right_Paren;
1702 when others => raise Program_Error;
1703 end case;
1705 if Nkind (Aggregate_Node) /= N_Delta_Aggregate then
1706 Set_Expressions (Aggregate_Node, Expr_List);
1707 end if;
1709 Set_Component_Associations (Aggregate_Node, Assoc_List);
1710 return Aggregate_Node;
1711 end P_Aggregate_Or_Paren_Expr;
1713 ------------------------------------------------
1714 -- 4.3 Record or Array Component Association --
1715 ------------------------------------------------
1717 -- RECORD_COMPONENT_ASSOCIATION ::=
1718 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1719 -- | COMPONENT_CHOICE_LIST => <>
1721 -- COMPONENT_CHOICE_LIST =>
1722 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1723 -- | others
1725 -- ARRAY_COMPONENT_ASSOCIATION ::=
1726 -- DISCRETE_CHOICE_LIST => EXPRESSION
1727 -- | DISCRETE_CHOICE_LIST => <>
1728 -- | ITERATED_COMPONENT_ASSOCIATION
1730 -- Note: this routine only handles the named cases, including others.
1731 -- Cases where the component choice list is not present have already
1732 -- been handled directly.
1734 -- Error recovery: can raise Error_Resync
1736 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1737 -- rules have been extended to give support to Ada 2005 limited
1738 -- aggregates (AI-287)
1740 function P_Record_Or_Array_Component_Association return Node_Id is
1741 Assoc_Node : Node_Id;
1742 Box_Present : Boolean := False;
1743 Box_With_Identifier_Present : Boolean := False;
1744 begin
1745 -- A loop indicates an iterated_component_association
1747 if Token = Tok_For then
1748 return P_Iterated_Component_Association;
1749 end if;
1751 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1752 Set_Binding_Chars (Assoc_Node, No_Name);
1754 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1755 Set_Sloc (Assoc_Node, Token_Ptr);
1756 TF_Arrow;
1758 if Token = Tok_Box then
1760 -- Ada 2005(AI-287): The box notation is used to indicate the
1761 -- default initialization of aggregate components
1763 Error_Msg_Ada_2005_Extension ("component association with '<'>");
1765 Box_Present := True;
1766 Set_Box_Present (Assoc_Node);
1767 Scan; -- past box
1768 elsif Token = Tok_Less then
1769 declare
1770 Scan_State : Saved_Scan_State;
1771 Id : Node_Id;
1772 begin
1773 Save_Scan_State (Scan_State);
1774 Scan; -- past "<"
1775 if Token = Tok_Identifier then
1776 Id := P_Defining_Identifier;
1777 if Token = Tok_Greater then
1778 if Core_Extensions_Allowed then
1779 Set_Box_Present (Assoc_Node);
1780 Set_Binding_Chars (Assoc_Node, Chars (Id));
1781 Box_Present := True;
1782 Box_With_Identifier_Present := True;
1783 Scan; -- past ">"
1784 else
1785 Error_Msg_GNAT_Extension
1786 ("identifier within box", Token_Ptr);
1787 Box_Present := True;
1788 -- Avoid cascading errors by ignoring the identifier
1789 end if;
1790 end if;
1791 end if;
1792 if not Box_Present then
1793 -- it wasn't an "is <identifier>", so restore.
1794 Restore_Scan_State (Scan_State);
1795 end if;
1796 end;
1797 end if;
1799 if not Box_Present then
1800 Set_Expression (Assoc_Node, P_Expression);
1801 end if;
1803 -- Check for "is <identifier>" for aggregate that is part of
1804 -- a pattern for a general case statement.
1806 if Token = Tok_Is then
1807 declare
1808 Scan_State : Saved_Scan_State;
1809 Id : Node_Id;
1810 begin
1811 Save_Scan_State (Scan_State);
1812 Scan; -- past "is"
1813 if Token = Tok_Identifier then
1814 Id := P_Defining_Identifier;
1816 if not Core_Extensions_Allowed then
1817 Error_Msg_GNAT_Extension
1818 ("IS following component association", Token_Ptr);
1819 elsif Box_With_Identifier_Present then
1820 Error_Msg
1821 ("Both identifier-in-box and trailing identifier"
1822 & " specified for one component association",
1823 Token_Ptr);
1824 else
1825 Set_Binding_Chars (Assoc_Node, Chars (Id));
1826 end if;
1827 else
1828 -- It wasn't an "is <identifier>", so restore.
1829 Restore_Scan_State (Scan_State);
1830 end if;
1831 end;
1832 end if;
1834 return Assoc_Node;
1835 end P_Record_Or_Array_Component_Association;
1837 -----------------------------
1838 -- 4.3.1 Record Aggregate --
1839 -----------------------------
1841 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1842 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1844 ----------------------------------------------
1845 -- 4.3.1 Record Component Association List --
1846 ----------------------------------------------
1848 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1850 ----------------------------------
1851 -- 4.3.1 Component Choice List --
1852 ----------------------------------
1854 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1856 --------------------------------
1857 -- 4.3.1 Extension Aggregate --
1858 --------------------------------
1860 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1862 --------------------------
1863 -- 4.3.1 Ancestor Part --
1864 --------------------------
1866 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1868 ----------------------------
1869 -- 4.3.1 Array Aggregate --
1870 ----------------------------
1872 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1874 ---------------------------------------
1875 -- 4.3.1 Positional Array Aggregate --
1876 ---------------------------------------
1878 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1880 ----------------------------------
1881 -- 4.3.1 Named Array Aggregate --
1882 ----------------------------------
1884 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1886 ----------------------------------------
1887 -- 4.3.1 Array Component Association --
1888 ----------------------------------------
1890 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1892 ---------------------
1893 -- 4.4 Expression --
1894 ---------------------
1896 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1898 -- EXPRESSION ::=
1899 -- RELATION {LOGICAL_OPERATOR RELATION}
1901 -- CHOICE_EXPRESSION ::=
1902 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1904 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1906 -- On return, Expr_Form indicates the categorization of the expression
1907 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1908 -- an error message is given, and Error is returned).
1910 -- Error recovery: cannot raise Error_Resync
1912 function P_Expression return Node_Id is
1913 Logical_Op : Node_Kind;
1914 Prev_Logical_Op : Node_Kind;
1915 Op_Location : Source_Ptr;
1916 Node1 : Node_Id;
1917 Node2 : Node_Id;
1919 begin
1920 Node1 := P_Relation;
1922 if Token in Token_Class_Logop then
1923 Prev_Logical_Op := N_Empty;
1925 loop
1926 Op_Location := Token_Ptr;
1927 Logical_Op := P_Logical_Operator;
1929 if Prev_Logical_Op /= N_Empty and then
1930 Logical_Op /= Prev_Logical_Op
1931 then
1932 Error_Msg
1933 ("mixed logical operators in expression", Op_Location);
1934 Prev_Logical_Op := N_Empty;
1935 else
1936 Prev_Logical_Op := Logical_Op;
1937 end if;
1939 Node2 := Node1;
1940 Node1 := New_Op_Node (Logical_Op, Op_Location);
1941 Set_Left_Opnd (Node1, Node2);
1942 Set_Right_Opnd (Node1, P_Relation);
1944 -- Check for case of errant comma or semicolon
1946 if Token in Tok_Comma | Tok_Semicolon then
1947 declare
1948 Com : constant Boolean := Token = Tok_Comma;
1949 Scan_State : Saved_Scan_State;
1950 Logop : Node_Kind;
1952 begin
1953 Save_Scan_State (Scan_State); -- at comma/semicolon
1954 Scan; -- past comma/semicolon
1956 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1957 -- do not deal with AND/OR because those cases get mixed up
1958 -- with the select alternatives case.
1960 if Token in Tok_And | Tok_Or then
1961 Logop := P_Logical_Operator;
1962 Restore_Scan_State (Scan_State); -- to comma/semicolon
1964 if Logop in N_And_Then | N_Or_Else then
1965 Scan; -- past comma/semicolon
1967 if Com then
1968 Error_Msg_SP -- CODEFIX
1969 ("|extra "","" ignored");
1970 else
1971 Error_Msg_SP -- CODEFIX
1972 ("|extra "";"" ignored");
1973 end if;
1975 else
1976 Restore_Scan_State (Scan_State); -- to comma/semicolon
1977 end if;
1979 else
1980 Restore_Scan_State (Scan_State); -- to comma/semicolon
1981 end if;
1982 end;
1983 end if;
1985 exit when Token not in Token_Class_Logop;
1986 end loop;
1988 Expr_Form := EF_Non_Simple;
1989 end if;
1991 if Token = Tok_Apostrophe then
1992 Bad_Range_Attribute (Token_Ptr);
1993 return Error;
1994 else
1995 return Node1;
1996 end if;
1997 end P_Expression;
1999 -- This function is identical to the normal P_Expression, except that it
2000 -- also permits the appearance of a case, conditional, or quantified
2001 -- expression if the call immediately follows a left paren, and followed
2002 -- by a right parenthesis. These forms are allowed if these conditions
2003 -- are not met, but an error message will be issued.
2005 function P_Expression_If_OK return Node_Id is
2006 begin
2007 -- Case of conditional, case or quantified expression
2009 if Token in Tok_Case | Tok_If | Tok_For | Tok_Declare then
2010 return P_Unparen_Cond_Expr_Etc;
2012 -- Normal case, not case/conditional/quantified expression
2014 else
2015 return P_Expression;
2016 end if;
2017 end P_Expression_If_OK;
2019 -- This function is identical to the normal P_Expression, except that it
2020 -- checks that the expression scan did not stop on a right paren. It is
2021 -- called in all contexts where a right parenthesis cannot legitimately
2022 -- follow an expression.
2024 -- Error recovery: cannot raise Error_Resync
2026 function P_Expression_No_Right_Paren return Node_Id is
2027 Expr : constant Node_Id := P_Expression;
2028 begin
2029 Ignore (Tok_Right_Paren);
2030 return Expr;
2031 end P_Expression_No_Right_Paren;
2033 ----------------------------------------
2034 -- 4.4 Expression_Or_Range_Attribute --
2035 ----------------------------------------
2037 -- EXPRESSION ::=
2038 -- RELATION {and RELATION} | RELATION {and then RELATION}
2039 -- | RELATION {or RELATION} | RELATION {or else RELATION}
2040 -- | RELATION {xor RELATION}
2042 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2044 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2046 -- On return, Expr_Form indicates the categorization of the expression
2047 -- and EF_Range_Attr is one of the possibilities.
2049 -- Error recovery: cannot raise Error_Resync
2051 -- In the grammar, a RANGE attribute is simply a name, but its use is
2052 -- highly restricted, so in the parser, we do not regard it as a name.
2053 -- Instead, P_Name returns without scanning the 'RANGE part of the
2054 -- attribute, and P_Expression_Or_Range_Attribute handles the range
2055 -- attribute reference. In the normal case where a range attribute is
2056 -- not allowed, an error message is issued by P_Expression.
2058 function P_Expression_Or_Range_Attribute return Node_Id is
2059 Logical_Op : Node_Kind;
2060 Prev_Logical_Op : Node_Kind;
2061 Op_Location : Source_Ptr;
2062 Node1 : Node_Id;
2063 Node2 : Node_Id;
2064 Attr_Node : Node_Id;
2066 begin
2067 Node1 := P_Relation;
2069 if Token = Tok_Apostrophe then
2070 Attr_Node := P_Range_Attribute_Reference (Node1);
2071 Expr_Form := EF_Range_Attr;
2072 return Attr_Node;
2074 elsif Token in Token_Class_Logop then
2075 Prev_Logical_Op := N_Empty;
2077 loop
2078 Op_Location := Token_Ptr;
2079 Logical_Op := P_Logical_Operator;
2081 if Prev_Logical_Op /= N_Empty and then
2082 Logical_Op /= Prev_Logical_Op
2083 then
2084 Error_Msg
2085 ("mixed logical operators in expression", Op_Location);
2086 Prev_Logical_Op := N_Empty;
2087 else
2088 Prev_Logical_Op := Logical_Op;
2089 end if;
2091 Node2 := Node1;
2092 Node1 := New_Op_Node (Logical_Op, Op_Location);
2093 Set_Left_Opnd (Node1, Node2);
2094 Set_Right_Opnd (Node1, P_Relation);
2095 exit when Token not in Token_Class_Logop;
2096 end loop;
2098 Expr_Form := EF_Non_Simple;
2099 end if;
2101 if Token = Tok_Apostrophe then
2102 Bad_Range_Attribute (Token_Ptr);
2103 return Error;
2104 else
2105 return Node1;
2106 end if;
2107 end P_Expression_Or_Range_Attribute;
2109 -- Version that allows a non-parenthesized case, conditional, or quantified
2110 -- expression if the call immediately follows a left paren, and followed
2111 -- by a right parenthesis. These forms are allowed if these conditions
2112 -- are not met, but an error message will be issued.
2114 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
2115 begin
2116 -- Case of conditional, case or quantified expression
2118 if Token in Tok_Case | Tok_If | Tok_For | Tok_Declare then
2119 return P_Unparen_Cond_Expr_Etc;
2121 -- Normal case, not one of the above expression types
2123 else
2124 return P_Expression_Or_Range_Attribute;
2125 end if;
2126 end P_Expression_Or_Range_Attribute_If_OK;
2128 -------------------
2129 -- 4.4 Relation --
2130 -------------------
2132 -- This procedure scans both relations and choice relations
2134 -- CHOICE_RELATION ::=
2135 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
2137 -- RELATION ::=
2138 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
2139 -- | RAISE_EXPRESSION
2141 -- MEMBERSHIP_CHOICE_LIST ::=
2142 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
2144 -- MEMBERSHIP_CHOICE ::=
2145 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
2147 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
2149 -- On return, Expr_Form indicates the categorization of the expression
2151 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2152 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2154 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2155 -- expression, then tokens are scanned until either a non-expression token,
2156 -- a right paren (not matched by a left paren) or a comma, is encountered.
2158 function P_Relation return Node_Id is
2159 Node1, Node2 : Node_Id;
2160 Optok : Source_Ptr;
2162 begin
2163 -- First check for raise expression
2165 if Token = Tok_Raise then
2166 Expr_Form := EF_Non_Simple;
2167 return P_Raise_Expression;
2168 end if;
2170 -- All other cases
2172 Node1 := P_Simple_Expression;
2174 if Token not in Token_Class_Relop then
2175 return Node1;
2177 else
2178 -- Here we have a relational operator following. If so then scan it
2179 -- out. Note that the assignment symbol := is treated as a relational
2180 -- operator to improve the error recovery when it is misused for =.
2181 -- P_Relational_Operator also parses the IN and NOT IN operations.
2183 Optok := Token_Ptr;
2184 Node2 := New_Op_Node (P_Relational_Operator, Optok);
2185 Set_Left_Opnd (Node2, Node1);
2187 -- Case of IN or NOT IN
2189 if Prev_Token = Tok_In then
2190 P_Membership_Test (Node2);
2192 -- Case of relational operator (= /= < <= > >=)
2194 else
2195 Set_Right_Opnd (Node2, P_Simple_Expression);
2196 end if;
2198 Expr_Form := EF_Non_Simple;
2200 if Token in Token_Class_Relop then
2201 Error_Msg_SC ("unexpected relational operator");
2202 raise Error_Resync;
2203 end if;
2205 return Node2;
2206 end if;
2208 -- If any error occurs, then scan to the next expression terminator symbol
2209 -- or comma or right paren at the outer (i.e. current) parentheses level.
2210 -- The flags are set to indicate a normal simple expression.
2212 exception
2213 when Error_Resync =>
2214 Resync_Expression;
2215 Expr_Form := EF_Simple;
2216 return Error;
2217 end P_Relation;
2219 ----------------------------
2220 -- 4.4 Simple Expression --
2221 ----------------------------
2223 -- SIMPLE_EXPRESSION ::=
2224 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2226 -- On return, Expr_Form indicates the categorization of the expression
2228 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2229 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2231 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2232 -- expression, then tokens are scanned until either a non-expression token,
2233 -- a right paren (not matched by a left paren) or a comma, is encountered.
2235 -- Note: P_Simple_Expression is called only internally by higher level
2236 -- expression routines. In cases in the grammar where a simple expression
2237 -- is required, the approach is to scan an expression, and then post an
2238 -- appropriate error message if the expression obtained is not simple. This
2239 -- gives better error recovery and treatment.
2241 function P_Simple_Expression return Node_Id is
2242 Scan_State : Saved_Scan_State;
2243 Node1 : Node_Id;
2244 Node2 : Node_Id;
2245 Tokptr : Source_Ptr;
2247 function At_Start_Of_Attribute return Boolean;
2248 -- Tests if we have quote followed by attribute name, if so, return True
2249 -- otherwise return False.
2251 ---------------------------
2252 -- At_Start_Of_Attribute --
2253 ---------------------------
2255 function At_Start_Of_Attribute return Boolean is
2256 begin
2257 if Token /= Tok_Apostrophe then
2258 return False;
2260 else
2261 declare
2262 Scan_State : Saved_Scan_State;
2264 begin
2265 Save_Scan_State (Scan_State);
2266 Scan; -- past quote
2268 if Token = Tok_Identifier
2269 and then Is_Attribute_Name (Chars (Token_Node))
2270 then
2271 Restore_Scan_State (Scan_State);
2272 return True;
2273 else
2274 Restore_Scan_State (Scan_State);
2275 return False;
2276 end if;
2277 end;
2278 end if;
2279 end At_Start_Of_Attribute;
2281 -- Start of processing for P_Simple_Expression
2283 begin
2284 -- Check for cases starting with a name. There are two reasons for
2285 -- special casing. First speed things up by catching a common case
2286 -- without going through several routine layers. Second the caller must
2287 -- be informed via Expr_Form when the simple expression is a name.
2289 if Token in Token_Class_Name then
2290 Node1 := P_Name;
2292 -- Deal with apostrophe cases
2294 if Token = Tok_Apostrophe then
2295 Save_Scan_State (Scan_State); -- at apostrophe
2296 Scan; -- past apostrophe
2298 -- If qualified expression, scan it out and fall through
2300 if Token = Tok_Left_Paren then
2301 Node1 := P_Qualified_Expression (Node1);
2302 Expr_Form := EF_Simple;
2304 -- If range attribute, then we return with Token pointing to the
2305 -- apostrophe. Note: avoid the normal error check on exit. We
2306 -- know that the expression really is complete in this case.
2308 else -- Token = Tok_Range then
2309 Restore_Scan_State (Scan_State); -- to apostrophe
2310 Expr_Form := EF_Simple_Name;
2311 return Node1;
2312 end if;
2313 end if;
2315 -- If an expression terminator follows, the previous processing
2316 -- completely scanned out the expression (a common case), and
2317 -- left Expr_Form set appropriately for returning to our caller.
2319 if Token in Token_Class_Sterm then
2320 null;
2322 -- Handle '}' as expression terminator of an interpolated
2323 -- expression.
2325 elsif Inside_Interpolated_String_Literal
2326 and then Token = Tok_Right_Curly_Bracket
2327 then
2328 null;
2330 -- If we do not have an expression terminator, then complete the
2331 -- scan of a simple expression. This code duplicates the code
2332 -- found in P_Term and P_Factor.
2334 else
2335 if Token = Tok_Double_Asterisk then
2336 if Style_Check then
2337 Style.Check_Exponentiation_Operator;
2338 end if;
2340 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2341 Scan; -- past **
2342 Set_Left_Opnd (Node2, Node1);
2343 Set_Right_Opnd (Node2, P_Primary);
2344 Check_Bad_Exp;
2345 Node1 := Node2;
2346 end if;
2348 loop
2349 exit when Token not in Token_Class_Mulop;
2350 Tokptr := Token_Ptr;
2351 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2353 if Style_Check then
2354 Style.Check_Binary_Operator;
2355 end if;
2357 Scan; -- past operator
2358 Set_Left_Opnd (Node2, Node1);
2359 Set_Right_Opnd (Node2, P_Factor);
2360 Node1 := Node2;
2361 end loop;
2363 loop
2364 exit when Token not in Token_Class_Binary_Addop;
2365 Tokptr := Token_Ptr;
2366 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2368 if Style_Check then
2369 Style.Check_Binary_Operator;
2370 end if;
2372 Scan; -- past operator
2373 Set_Left_Opnd (Node2, Node1);
2374 Set_Right_Opnd (Node2, P_Term);
2375 Node1 := Node2;
2376 end loop;
2378 Expr_Form := EF_Simple;
2379 end if;
2381 -- Cases where simple expression does not start with a name
2383 else
2384 -- Scan initial sign and initial Term
2386 if Token in Token_Class_Unary_Addop then
2387 Tokptr := Token_Ptr;
2388 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
2390 if Style_Check then
2391 Style.Check_Unary_Plus_Or_Minus (Inside_Depends);
2392 end if;
2394 Scan; -- past operator
2395 Set_Right_Opnd (Node1, P_Term);
2396 else
2397 Node1 := P_Term;
2398 end if;
2400 -- In the following, we special-case a sequence of concatenations of
2401 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2402 -- else mixed in. For such a sequence, we return a tree representing
2403 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2404 -- the number of concatenations is large. If semantic analysis
2405 -- resolves the "&" to a predefined one, then this folding gives the
2406 -- right answer. Otherwise, semantic analysis will complain about a
2407 -- capacity-exceeded error. The purpose of this trick is to avoid
2408 -- creating a deeply nested tree, which would cause deep recursion
2409 -- during semantics, causing stack overflow. This way, we can handle
2410 -- enormous concatenations in the normal case of predefined "&". We
2411 -- first build up the normal tree, and then rewrite it if
2412 -- appropriate.
2414 declare
2415 Num_Concats_Threshold : constant Positive := 1000;
2416 -- Arbitrary threshold value to enable optimization
2418 First_Node : constant Node_Id := Node1;
2419 Is_Strlit_Concat : Boolean;
2420 -- True iff we've parsed a sequence of concatenations of string
2421 -- literals, with nothing else mixed in.
2423 Num_Concats : Natural;
2424 -- Number of "&" operators if Is_Strlit_Concat is True
2426 begin
2427 Is_Strlit_Concat :=
2428 Nkind (Node1) = N_String_Literal
2429 and then Token = Tok_Ampersand;
2430 Num_Concats := 0;
2432 -- Scan out sequence of terms separated by binary adding operators
2434 loop
2435 exit when Token not in Token_Class_Binary_Addop;
2436 Tokptr := Token_Ptr;
2437 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2439 if Style_Check and then not Debug_Flag_Dot_QQ then
2440 Style.Check_Binary_Operator;
2441 end if;
2443 Scan; -- past operator
2444 Set_Left_Opnd (Node2, Node1);
2445 Node1 := P_Term;
2446 Set_Right_Opnd (Node2, Node1);
2448 -- Check if we're still concatenating string literals
2450 Is_Strlit_Concat :=
2451 Is_Strlit_Concat
2452 and then Nkind (Node2) = N_Op_Concat
2453 and then Nkind (Node1) = N_String_Literal;
2455 if Is_Strlit_Concat then
2456 Num_Concats := Num_Concats + 1;
2457 end if;
2459 Node1 := Node2;
2460 end loop;
2462 -- If we have an enormous series of concatenations of string
2463 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2464 -- flag tells semantic analysis that if the "&" is not predefined,
2465 -- the folded value is wrong.
2467 if Is_Strlit_Concat
2468 and then Num_Concats >= Num_Concats_Threshold
2469 then
2470 declare
2471 Strlit_Concat_Val : String_Id;
2472 -- Contains the folded value (which will be correct if the
2473 -- "&" operators are the predefined ones).
2475 Cur_Node : Node_Id;
2476 -- For walking up the tree
2478 New_Node : Node_Id;
2479 -- Folded node to replace Node1
2481 Loc : constant Source_Ptr := Sloc (First_Node);
2483 begin
2484 -- Walk up the tree starting at the leftmost string literal
2485 -- (First_Node), building up the Strlit_Concat_Val as we
2486 -- go. Note that we do not use recursion here -- the whole
2487 -- point is to avoid recursively walking that enormous tree.
2489 Start_String;
2490 Store_String_Chars (Strval (First_Node));
2492 Cur_Node := Parent (First_Node);
2493 while Present (Cur_Node) loop
2494 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2495 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2497 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2498 Cur_Node := Parent (Cur_Node);
2499 end loop;
2501 Strlit_Concat_Val := End_String;
2503 -- Create new folded node, and rewrite result with a concat-
2504 -- enation of an empty string literal and the folded node.
2506 New_Node :=
2507 Make_Op_Concat (Loc,
2508 Make_String_Literal (Loc, Null_String_Id),
2509 Make_String_Literal (Loc, Strlit_Concat_Val,
2510 Is_Folded_In_Parser => True));
2511 Rewrite (Node1, New_Node);
2512 end;
2513 end if;
2514 end;
2516 -- All done, we clearly do not have name or numeric literal so this
2517 -- is a case of a simple expression which is some other possibility.
2519 Expr_Form := EF_Simple;
2520 end if;
2522 -- Come here at end of simple expression, where we do a couple of
2523 -- special checks to improve error recovery.
2525 -- Special test to improve error recovery. If the current token is a
2526 -- period, then someone is trying to do selection on something that is
2527 -- not a name, e.g. a qualified expression.
2529 if Token = Tok_Dot then
2530 Error_Msg_SC ("prefix for selection is not a name");
2532 -- If qualified expression, comment and continue, otherwise something
2533 -- is pretty nasty so do an Error_Resync call.
2535 if Ada_Version < Ada_2012
2536 and then Nkind (Node1) = N_Qualified_Expression
2537 then
2538 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2539 else
2540 raise Error_Resync;
2541 end if;
2542 end if;
2544 -- Special test to improve error recovery: If the current token is
2545 -- not the first token on a line (as determined by checking the
2546 -- previous token position with the start of the current line),
2547 -- then we insist that we have an appropriate terminating token.
2548 -- Consider the following two examples:
2550 -- 1) if A nad B then ...
2552 -- 2) A := B
2553 -- C := D
2555 -- In the first example, we would like to issue a binary operator
2556 -- expected message and resynchronize to the then. In the second
2557 -- example, we do not want to issue a binary operator message, so
2558 -- that instead we will get the missing semicolon message. This
2559 -- distinction is of course a heuristic which does not always work,
2560 -- but in practice it is quite effective.
2562 -- Note: the one case in which we do not go through this circuit is
2563 -- when we have scanned a range attribute and want to return with
2564 -- Token pointing to the apostrophe. The apostrophe is not normally
2565 -- an expression terminator, and is not in Token_Class_Sterm, but
2566 -- in this special case we know that the expression is complete.
2568 -- We disable this error recovery machinery when we are processing an
2569 -- interpolated string and we reach the expression terminator '}'.
2571 if not Token_Is_At_Start_Of_Line
2572 and then Token not in Token_Class_Sterm
2573 and then not (Inside_Interpolated_String_Literal
2574 and then Token = Tok_Right_Curly_Bracket)
2575 then
2576 -- Normally the right error message is indeed that we expected a
2577 -- binary operator, but in the case of being between a right and left
2578 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2580 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2581 T_Comma;
2583 -- And if we have a quote, we may have a bad attribute
2585 elsif At_Start_Of_Attribute then
2586 Error_Msg_SC ("prefix of attribute must be a name");
2588 if Ada_Version >= Ada_2012 then
2589 Error_Msg_SC ("\qualify expression to turn it into a name");
2590 end if;
2592 -- Normal case for binary operator expected message
2594 else
2595 Error_Msg_AP ("binary operator expected");
2596 end if;
2598 raise Error_Resync;
2600 else
2601 return Node1;
2602 end if;
2604 -- If any error occurs, then scan to next expression terminator symbol
2605 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2606 -- level. Expr_Form is set to indicate a normal simple expression.
2608 exception
2609 when Error_Resync =>
2610 Resync_Expression;
2611 Expr_Form := EF_Simple;
2612 return Error;
2613 end P_Simple_Expression;
2615 -----------------------------------------------
2616 -- 4.4 Simple Expression or Range Attribute --
2617 -----------------------------------------------
2619 -- SIMPLE_EXPRESSION ::=
2620 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2622 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2624 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2626 -- Error recovery: cannot raise Error_Resync
2628 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2629 Sexpr : Node_Id;
2630 Attr_Node : Node_Id;
2632 begin
2633 -- We don't just want to roar ahead and call P_Simple_Expression
2634 -- here, since we want to handle the case of a parenthesized range
2635 -- attribute cleanly.
2637 if Token = Tok_Left_Paren then
2638 declare
2639 Lptr : constant Source_Ptr := Token_Ptr;
2640 Scan_State : Saved_Scan_State;
2642 begin
2643 Save_Scan_State (Scan_State);
2644 Scan; -- past left paren
2645 Sexpr := P_Simple_Expression;
2647 if Token = Tok_Apostrophe then
2648 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2649 Expr_Form := EF_Range_Attr;
2651 if Token = Tok_Right_Paren then
2652 Scan; -- scan past right paren if present
2653 end if;
2655 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2657 return Attr_Node;
2658 end if;
2660 Restore_Scan_State (Scan_State);
2661 end;
2662 end if;
2664 -- Here after dealing with parenthesized range attribute
2666 Sexpr := P_Simple_Expression;
2668 if Token = Tok_Apostrophe then
2669 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2670 Expr_Form := EF_Range_Attr;
2671 return Attr_Node;
2673 else
2674 return Sexpr;
2675 end if;
2676 end P_Simple_Expression_Or_Range_Attribute;
2678 ---------------
2679 -- 4.4 Term --
2680 ---------------
2682 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2684 -- Error recovery: can raise Error_Resync
2686 function P_Term return Node_Id is
2687 Node1, Node2 : Node_Id;
2688 Tokptr : Source_Ptr;
2690 begin
2691 Node1 := P_Factor;
2693 loop
2694 exit when Token not in Token_Class_Mulop;
2695 Tokptr := Token_Ptr;
2696 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2698 if Style_Check and then not Debug_Flag_Dot_QQ then
2699 Style.Check_Binary_Operator;
2700 end if;
2702 Scan; -- past operator
2703 Set_Left_Opnd (Node2, Node1);
2704 Set_Right_Opnd (Node2, P_Factor);
2705 Node1 := Node2;
2706 end loop;
2708 return Node1;
2709 end P_Term;
2711 -----------------
2712 -- 4.4 Factor --
2713 -----------------
2715 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2717 -- Error recovery: can raise Error_Resync
2719 function P_Factor return Node_Id is
2720 Node1 : Node_Id;
2721 Node2 : Node_Id;
2723 begin
2724 if Token = Tok_Abs then
2725 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2727 if Style_Check then
2728 Style.Check_Abs_Not;
2729 end if;
2731 Scan; -- past ABS
2732 Set_Right_Opnd (Node1, P_Primary);
2733 return Node1;
2735 elsif Token = Tok_Not then
2736 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2738 if Style_Check then
2739 Style.Check_Abs_Not;
2740 end if;
2742 Scan; -- past NOT
2743 Set_Right_Opnd (Node1, P_Primary);
2744 return Node1;
2746 else
2747 Node1 := P_Primary;
2749 if Token = Tok_Double_Asterisk then
2750 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2751 Scan; -- past **
2752 Set_Left_Opnd (Node2, Node1);
2753 Set_Right_Opnd (Node2, P_Primary);
2754 Check_Bad_Exp;
2755 return Node2;
2756 else
2757 return Node1;
2758 end if;
2759 end if;
2760 end P_Factor;
2762 ------------------
2763 -- 4.4 Primary --
2764 ------------------
2766 -- PRIMARY ::=
2767 -- NUMERIC_LITERAL | null
2768 -- | STRING_LITERAL | AGGREGATE
2769 -- | NAME | QUALIFIED_EXPRESSION
2770 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2771 -- | REDUCTION_ATTRIBUTE_REFERENCE
2773 -- Error recovery: can raise Error_Resync
2775 function P_Primary return Node_Id is
2776 Scan_State : Saved_Scan_State;
2777 Node1 : Node_Id;
2779 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
2780 -- Remember if previous token is a left parenthesis. This is used to
2781 -- deal with checking whether IF/CASE/FOR expressions appearing as
2782 -- primaries require extra parenthesization.
2784 begin
2785 -- The loop runs more than once only if misplaced pragmas are found
2786 -- or if a misplaced unary minus is skipped.
2788 loop
2789 case Token is
2791 -- Name token can start a name, call or qualified expression, all
2792 -- of which are acceptable possibilities for primary. Note also
2793 -- that string literal is included in name (as operator symbol)
2794 -- and type conversion is included in name (as indexed component).
2796 when Tok_Char_Literal
2797 | Tok_Identifier
2798 | Tok_Operator_Symbol
2800 Node1 := P_Name;
2802 -- All done unless apostrophe follows
2804 if Token /= Tok_Apostrophe then
2805 return Node1;
2807 -- Apostrophe following means that we have either just parsed
2808 -- the subtype mark of a qualified expression, or the prefix
2809 -- or a range attribute.
2811 else -- Token = Tok_Apostrophe
2812 Save_Scan_State (Scan_State); -- at apostrophe
2813 Scan; -- past apostrophe
2815 -- If range attribute, then this is always an error, since
2816 -- the only legitimate case (where the scanned expression is
2817 -- a qualified simple name) is handled at the level of the
2818 -- Simple_Expression processing. This case corresponds to a
2819 -- usage such as 3 + A'Range, which is always illegal.
2821 if Token = Tok_Range then
2822 Restore_Scan_State (Scan_State); -- to apostrophe
2823 Bad_Range_Attribute (Token_Ptr);
2824 return Error;
2826 -- If left paren, then we have a qualified expression.
2827 -- Note that P_Name guarantees that in this case, where
2828 -- Token = Tok_Apostrophe on return, the only two possible
2829 -- tokens following the apostrophe are left paren and
2830 -- RANGE, so we know we have a left paren here.
2832 else -- Token = Tok_Left_Paren
2833 return P_Qualified_Expression (Node1);
2835 end if;
2836 end if;
2838 -- Numeric or string literal
2840 when Tok_Integer_Literal
2841 | Tok_Real_Literal
2842 | Tok_String_Literal
2844 Node1 := Token_Node;
2845 Scan; -- past number
2846 return Node1;
2848 -- Left paren, starts aggregate or parenthesized expression
2850 when Tok_Left_Paren =>
2851 declare
2852 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2854 begin
2855 if Nkind (Expr) = N_Attribute_Reference
2856 and then Attribute_Name (Expr) = Name_Range
2857 then
2858 Bad_Range_Attribute (Sloc (Expr));
2859 end if;
2861 return Expr;
2862 end;
2864 when Tok_Left_Bracket =>
2865 return P_Aggregate;
2867 when Tok_Left_Interpolated_String =>
2868 return P_Interpolated_String_Literal;
2870 -- Allocator
2872 when Tok_New =>
2873 return P_Allocator;
2875 -- Null
2877 when Tok_Null =>
2878 Scan; -- past NULL
2879 return New_Node (N_Null, Prev_Token_Ptr);
2881 -- Pragma, not allowed here, so just skip past it
2883 when Tok_Pragma =>
2884 P_Pragmas_Misplaced;
2886 -- Deal with IF (possible unparenthesized if expression)
2888 when Tok_If =>
2890 -- If this looks like a real if, defined as an IF appearing at
2891 -- the start of a new line, then we consider we have a missing
2892 -- operand. If in Ada 2012 and the IF is not properly indented
2893 -- for a statement, we prefer to issue a message about an ill-
2894 -- parenthesized if expression.
2896 if Token_Is_At_Start_Of_Line
2897 and then not
2898 (Ada_Version >= Ada_2012
2899 and then
2900 (Style_Check_Indentation = 0
2901 or else
2902 Start_Column rem Style_Check_Indentation /= 0))
2903 then
2904 Error_Msg_AP ("missing operand");
2905 return Error;
2907 -- If this looks like an if expression, then treat it that way
2908 -- with an error message if not explicitly surrounded by
2909 -- parentheses.
2911 elsif Ada_Version >= Ada_2012 then
2912 Node1 := P_If_Expression;
2914 if not (Lparen and then Token = Tok_Right_Paren) then
2915 Error_Msg
2916 ("if expression must be parenthesized", Sloc (Node1));
2917 end if;
2919 return Node1;
2921 -- Otherwise treat as misused identifier
2923 else
2924 return P_Identifier;
2925 end if;
2927 -- Deal with CASE (possible unparenthesized case expression)
2929 when Tok_Case =>
2931 -- If this looks like a real case, defined as a CASE appearing
2932 -- the start of a new line, then we consider we have a missing
2933 -- operand. If in Ada 2012 and the CASE is not properly
2934 -- indented for a statement, we prefer to issue a message about
2935 -- an ill-parenthesized case expression.
2937 if Token_Is_At_Start_Of_Line
2938 and then not
2939 (Ada_Version >= Ada_2012
2940 and then Style_Check_Indentation /= 0
2941 and then Start_Column rem Style_Check_Indentation /= 0)
2942 then
2943 Error_Msg_AP ("missing operand");
2944 return Error;
2946 -- If this looks like a case expression, then treat it that way
2947 -- with an error message if not within parentheses.
2949 elsif Ada_Version >= Ada_2012 then
2950 Node1 := P_Case_Expression;
2952 if not (Lparen and then Token = Tok_Right_Paren) then
2953 Error_Msg
2954 ("case expression must be parenthesized", Sloc (Node1));
2955 end if;
2957 return Node1;
2959 -- Otherwise treat as misused identifier
2961 else
2962 return P_Identifier;
2963 end if;
2965 -- For [all | some] indicates a quantified expression
2967 when Tok_For =>
2968 if Token_Is_At_Start_Of_Line then
2969 Error_Msg_AP ("misplaced loop");
2970 return Error;
2972 elsif Ada_Version >= Ada_2012 then
2973 Save_Scan_State (Scan_State);
2974 Scan; -- past FOR
2976 if Token in Tok_All | Tok_Some then
2977 Restore_Scan_State (Scan_State); -- To FOR
2978 Node1 := P_Quantified_Expression;
2980 if not (Lparen and then Token = Tok_Right_Paren) then
2981 Error_Msg
2982 ("quantified expression must be parenthesized",
2983 Sloc (Node1));
2984 end if;
2985 else
2986 Restore_Scan_State (Scan_State); -- To FOR
2987 Node1 := P_Iterated_Component_Association;
2988 end if;
2990 return Node1;
2992 -- Otherwise treat as misused identifier
2994 else
2995 return P_Identifier;
2996 end if;
2998 -- Minus may well be an improper attempt at a unary minus. Give
2999 -- a message, skip the minus and keep going.
3001 when Tok_Minus =>
3002 Error_Msg_SC ("parentheses required for unary minus");
3003 Scan; -- past minus
3005 when Tok_At_Sign => -- AI12-0125 : target_name
3006 Error_Msg_Ada_2022_Feature ("target name", Token_Ptr);
3008 Node1 := P_Name;
3009 return Node1;
3011 -- Anything else is illegal as the first token of a primary, but
3012 -- we test for some common errors, to improve error messages.
3014 when others =>
3015 if Is_Reserved_Identifier then
3016 return P_Identifier;
3018 elsif Prev_Token = Tok_Comma then
3019 Error_Msg_SP -- CODEFIX
3020 ("|extra "","" ignored");
3021 raise Error_Resync;
3023 else
3024 Error_Msg_AP ("missing operand");
3025 raise Error_Resync;
3026 end if;
3027 end case;
3028 end loop;
3029 end P_Primary;
3031 -------------------------------
3032 -- 4.4 Quantified_Expression --
3033 -------------------------------
3035 -- QUANTIFIED_EXPRESSION ::=
3036 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
3037 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
3039 function P_Quantified_Expression return Node_Id is
3040 I_Spec : Node_Id;
3041 Node1 : Node_Id;
3043 begin
3044 Error_Msg_Ada_2012_Feature ("quantified expression", Token_Ptr);
3045 Scan; -- past FOR
3046 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
3048 if Token = Tok_All then
3049 Set_All_Present (Node1);
3050 elsif Token /= Tok_Some then
3051 Error_Msg_AP ("missing quantifier");
3052 raise Error_Resync;
3053 end if;
3055 Scan; -- past ALL or SOME
3056 I_Spec := P_Loop_Parameter_Specification;
3058 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
3059 Set_Loop_Parameter_Specification (Node1, I_Spec);
3060 else
3061 Set_Iterator_Specification (Node1, I_Spec);
3062 end if;
3064 if Token = Tok_Arrow then
3065 Scan;
3066 Set_Condition (Node1, P_Expression);
3067 return Node1;
3068 else
3069 Error_Msg_AP ("missing arrow");
3070 raise Error_Resync;
3071 end if;
3072 end P_Quantified_Expression;
3074 ---------------------------
3075 -- 4.5 Logical Operator --
3076 ---------------------------
3078 -- LOGICAL_OPERATOR ::= and | or | xor
3080 -- Note: AND THEN and OR ELSE are also treated as logical operators
3081 -- by the parser (even though they are not operators semantically)
3083 -- The value returned is the appropriate Node_Kind code for the operator
3084 -- On return, Token points to the token following the scanned operator.
3086 -- The caller has checked that the first token is a legitimate logical
3087 -- operator token (i.e. is either XOR, AND, OR).
3089 -- Error recovery: cannot raise Error_Resync
3091 function P_Logical_Operator return Node_Kind is
3092 begin
3093 if Token = Tok_And then
3094 if Style_Check then
3095 Style.Check_Binary_Operator;
3096 end if;
3098 Scan; -- past AND
3100 if Token = Tok_Then then
3101 Scan; -- past THEN
3102 return N_And_Then;
3103 else
3104 return N_Op_And;
3105 end if;
3107 elsif Token = Tok_Or then
3108 if Style_Check then
3109 Style.Check_Binary_Operator;
3110 end if;
3112 Scan; -- past OR
3114 if Token = Tok_Else then
3115 Scan; -- past ELSE
3116 return N_Or_Else;
3117 else
3118 return N_Op_Or;
3119 end if;
3121 else -- Token = Tok_Xor
3122 if Style_Check then
3123 Style.Check_Binary_Operator;
3124 end if;
3126 Scan; -- past XOR
3127 return N_Op_Xor;
3128 end if;
3129 end P_Logical_Operator;
3131 ------------------------------
3132 -- 4.5 Relational Operator --
3133 ------------------------------
3135 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
3137 -- The value returned is the appropriate Node_Kind code for the operator.
3138 -- On return, Token points to the operator token, NOT past it.
3140 -- The caller has checked that the first token is a legitimate relational
3141 -- operator token (i.e. is one of the operator tokens listed above).
3143 -- Error recovery: cannot raise Error_Resync
3145 function P_Relational_Operator return Node_Kind is
3146 Op_Kind : Node_Kind;
3147 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
3148 (Tok_Less => N_Op_Lt,
3149 Tok_Equal => N_Op_Eq,
3150 Tok_Greater => N_Op_Gt,
3151 Tok_Not_Equal => N_Op_Ne,
3152 Tok_Greater_Equal => N_Op_Ge,
3153 Tok_Less_Equal => N_Op_Le,
3154 Tok_In => N_In,
3155 Tok_Not => N_Not_In,
3156 Tok_Box => N_Op_Ne);
3158 begin
3159 if Token = Tok_Box then
3160 Error_Msg_SC -- CODEFIX
3161 ("|""'<'>"" should be ""/=""");
3162 end if;
3164 Op_Kind := Relop_Node (Token);
3166 if Style_Check then
3167 Style.Check_Binary_Operator;
3168 end if;
3170 Scan; -- past operator token
3172 -- Deal with NOT IN, if previous token was NOT, we must have IN now
3174 if Prev_Token = Tok_Not then
3176 -- Style check, for NOT IN, we require one space between NOT and IN
3178 if Style_Check and then Token = Tok_In then
3179 Style.Check_Not_In;
3180 end if;
3182 T_In;
3183 end if;
3185 return Op_Kind;
3186 end P_Relational_Operator;
3188 ---------------------------------
3189 -- 4.5 Binary Adding Operator --
3190 ---------------------------------
3192 -- BINARY_ADDING_OPERATOR ::= + | - | &
3194 -- The value returned is the appropriate Node_Kind code for the operator.
3195 -- On return, Token points to the operator token (NOT past it).
3197 -- The caller has checked that the first token is a legitimate adding
3198 -- operator token (i.e. is one of the operator tokens listed above).
3200 -- Error recovery: cannot raise Error_Resync
3202 function P_Binary_Adding_Operator return Node_Kind is
3203 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
3204 (Tok_Ampersand => N_Op_Concat,
3205 Tok_Minus => N_Op_Subtract,
3206 Tok_Plus => N_Op_Add);
3207 begin
3208 return Addop_Node (Token);
3209 end P_Binary_Adding_Operator;
3211 --------------------------------
3212 -- 4.5 Unary Adding Operator --
3213 --------------------------------
3215 -- UNARY_ADDING_OPERATOR ::= + | -
3217 -- The value returned is the appropriate Node_Kind code for the operator.
3218 -- On return, Token points to the operator token (NOT past it).
3220 -- The caller has checked that the first token is a legitimate adding
3221 -- operator token (i.e. is one of the operator tokens listed above).
3223 -- Error recovery: cannot raise Error_Resync
3225 function P_Unary_Adding_Operator return Node_Kind is
3226 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
3227 (Tok_Minus => N_Op_Minus,
3228 Tok_Plus => N_Op_Plus);
3229 begin
3230 return Addop_Node (Token);
3231 end P_Unary_Adding_Operator;
3233 -------------------------------
3234 -- 4.5 Multiplying Operator --
3235 -------------------------------
3237 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3239 -- The value returned is the appropriate Node_Kind code for the operator.
3240 -- On return, Token points to the operator token (NOT past it).
3242 -- The caller has checked that the first token is a legitimate multiplying
3243 -- operator token (i.e. is one of the operator tokens listed above).
3245 -- Error recovery: cannot raise Error_Resync
3247 function P_Multiplying_Operator return Node_Kind is
3248 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
3249 (Tok_Asterisk => N_Op_Multiply,
3250 Tok_Mod => N_Op_Mod,
3251 Tok_Rem => N_Op_Rem,
3252 Tok_Slash => N_Op_Divide);
3253 begin
3254 return Mulop_Node (Token);
3255 end P_Multiplying_Operator;
3257 --------------------------------------
3258 -- 4.5 Highest Precedence Operator --
3259 --------------------------------------
3261 -- Parsed by P_Factor (4.4)
3263 -- Note: this rule is not in fact used by the grammar at any point
3265 --------------------------
3266 -- 4.6 Type Conversion --
3267 --------------------------
3269 -- Parsed by P_Primary as a Name (4.1)
3271 -------------------------------
3272 -- 4.7 Qualified Expression --
3273 -------------------------------
3275 -- QUALIFIED_EXPRESSION ::=
3276 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3278 -- The caller has scanned the name which is the Subtype_Mark parameter
3279 -- and scanned past the single quote following the subtype mark. The
3280 -- caller has not checked that this name is in fact appropriate for
3281 -- a subtype mark name (i.e. it is a selected component or identifier).
3283 -- Error_Recovery: cannot raise Error_Resync
3285 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
3286 Qual_Node : Node_Id;
3287 begin
3288 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
3289 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
3290 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
3291 return Qual_Node;
3292 end P_Qualified_Expression;
3294 --------------------
3295 -- 4.8 Allocator --
3296 --------------------
3298 -- ALLOCATOR ::=
3299 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3300 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3302 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3304 -- The caller has checked that the initial token is NEW
3306 -- Error recovery: can raise Error_Resync
3308 function P_Allocator return Node_Id is
3309 Alloc_Node : Node_Id;
3310 Type_Node : Node_Id;
3311 Null_Exclusion_Present : Boolean;
3313 begin
3314 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
3315 T_New;
3317 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3319 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3321 if Token = Tok_Left_Paren then
3322 Scan; -- past (
3323 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
3324 T_Right_Paren;
3326 Error_Msg_Ada_2012_Feature
3327 ("|subpool specification",
3328 Sloc (Subpool_Handle_Name (Alloc_Node)));
3329 end if;
3331 Null_Exclusion_Present := P_Null_Exclusion;
3332 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
3333 Type_Node := P_Subtype_Mark_Resync;
3335 if Token = Tok_Apostrophe then
3336 Scan; -- past apostrophe
3337 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
3338 else
3339 Set_Expression
3340 (Alloc_Node,
3341 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
3343 -- AI05-0104: An explicit null exclusion is not allowed for an
3344 -- allocator without initialization. In previous versions of the
3345 -- language it just raises constraint error.
3347 if Ada_Version >= Ada_2012 and then Null_Exclusion_Present then
3348 Error_Msg_N
3349 ("an allocator with a subtype indication "
3350 & "cannot have a null exclusion", Alloc_Node);
3351 end if;
3352 end if;
3354 return Alloc_Node;
3355 end P_Allocator;
3357 -----------------------
3358 -- P_Case_Expression --
3359 -----------------------
3361 function P_Case_Expression return Node_Id is
3362 Loc : constant Source_Ptr := Token_Ptr;
3363 Case_Node : Node_Id;
3364 Save_State : Saved_Scan_State;
3366 begin
3367 Error_Msg_Ada_2012_Feature ("|case expression", Token_Ptr);
3368 Scan; -- past CASE
3369 Case_Node :=
3370 Make_Case_Expression (Loc,
3371 Expression => P_Expression_No_Right_Paren,
3372 Alternatives => New_List);
3373 T_Is;
3375 -- We now have scanned out CASE expression IS, scan alternatives
3377 loop
3378 T_When;
3379 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
3381 -- Missing comma if WHEN (more alternatives present)
3383 if Token = Tok_When then
3384 T_Comma;
3386 -- A semicolon followed by "when" is probably meant to be a comma
3388 elsif Token = Tok_Semicolon then
3389 Save_Scan_State (Save_State);
3390 Scan; -- past the semicolon
3392 if Token /= Tok_When then
3393 Restore_Scan_State (Save_State);
3394 exit;
3395 end if;
3397 Error_Msg_SP -- CODEFIX
3398 ("|"";"" should be "",""");
3400 -- If comma/WHEN, skip comma and we have another alternative
3402 elsif Token = Tok_Comma then
3403 Save_Scan_State (Save_State);
3404 Scan; -- past comma
3406 if Token /= Tok_When then
3407 Restore_Scan_State (Save_State);
3408 exit;
3409 end if;
3411 -- If no comma or WHEN, definitely done
3413 else
3414 exit;
3415 end if;
3416 end loop;
3418 -- If we have an END CASE, diagnose as not needed
3420 if Token = Tok_End then
3421 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
3422 Scan; -- past END
3424 if Token = Tok_Case then
3425 Scan; -- past CASE;
3426 end if;
3427 end if;
3429 -- Return the Case_Expression node
3431 return Case_Node;
3432 end P_Case_Expression;
3434 -----------------------------------
3435 -- P_Case_Expression_Alternative --
3436 -----------------------------------
3438 -- CASE_STATEMENT_ALTERNATIVE ::=
3439 -- when DISCRETE_CHOICE_LIST =>
3440 -- EXPRESSION
3442 -- The caller has checked that and scanned past the initial WHEN token
3443 -- Error recovery: can raise Error_Resync
3445 function P_Case_Expression_Alternative return Node_Id is
3446 Case_Alt_Node : Node_Id;
3447 begin
3448 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
3449 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
3450 TF_Arrow;
3451 Set_Expression (Case_Alt_Node, P_Expression);
3452 return Case_Alt_Node;
3453 end P_Case_Expression_Alternative;
3455 --------------------------------------
3456 -- P_Iterated_Component_Association --
3457 --------------------------------------
3459 -- ITERATED_COMPONENT_ASSOCIATION ::=
3460 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3461 -- for ITERATOR_SPECIFICATION => EXPRESSION
3463 function P_Iterated_Component_Association return Node_Id is
3464 Assoc_Node : Node_Id;
3465 Choice : Node_Id;
3466 Filter : Node_Id := Empty;
3467 Id : Node_Id;
3468 Iter_Spec : Node_Id;
3469 Loop_Spec : Node_Id;
3470 State : Saved_Scan_State;
3472 procedure Build_Iterated_Element_Association;
3473 -- If the iterator includes a key expression or a filter, it is
3474 -- an Ada 2022 Iterator_Element_Association within a container
3475 -- aggregate.
3477 ----------------------------------------
3478 -- Build_Iterated_Element_Association --
3479 ----------------------------------------
3481 procedure Build_Iterated_Element_Association is
3482 begin
3483 -- Build loop_parameter_specification
3485 Loop_Spec :=
3486 New_Node (N_Loop_Parameter_Specification, Prev_Token_Ptr);
3487 Set_Defining_Identifier (Loop_Spec, Id);
3489 Choice := First (Discrete_Choices (Assoc_Node));
3490 Assoc_Node :=
3491 New_Node (N_Iterated_Element_Association, Prev_Token_Ptr);
3492 Set_Loop_Parameter_Specification (Assoc_Node, Loop_Spec);
3494 if Present (Next (Choice)) then
3495 Error_Msg_N ("expect loop parameter specification", Choice);
3496 end if;
3498 Remove (Choice);
3499 Set_Discrete_Subtype_Definition (Loop_Spec, Choice);
3500 Set_Iterator_Filter (Loop_Spec, Filter);
3501 end Build_Iterated_Element_Association;
3503 -- Start of processing for P_Iterated_Component_Association
3505 begin
3506 Scan; -- past FOR
3507 Save_Scan_State (State);
3509 -- A lookahead is necessary to differentiate between the
3510 -- Ada 2012 form with a choice list, and the Ada 2022 element
3511 -- iterator form, recognized by the presence of "OF". Other
3512 -- disambiguation requires context and is done during semantic
3513 -- analysis. Note that "for X in E" is syntactically ambiguous:
3514 -- if E is a subtype indication this is a loop parameter spec,
3515 -- while if E a name it is an iterator_specification, and the
3516 -- disambiguation takes place during semantic analysis.
3517 -- In addition, if "use" is present after the specification,
3518 -- this is an Iterated_Element_Association that carries a
3519 -- key_expression, and we generate the appropriate node.
3520 -- Finally, the Iterated_Element form is reserved for container
3521 -- aggregates, and is illegal in array aggregates.
3523 Id := P_Defining_Identifier;
3524 Assoc_Node :=
3525 New_Node (N_Iterated_Component_Association, Prev_Token_Ptr);
3527 case Token is
3528 when Tok_In =>
3529 Set_Defining_Identifier (Assoc_Node, Id);
3530 T_In;
3531 Set_Discrete_Choices (Assoc_Node, P_Discrete_Choice_List);
3533 -- The iterator may include a filter
3535 if Token = Tok_When then
3536 Scan; -- past WHEN
3537 Filter := P_Condition;
3538 end if;
3540 if Token = Tok_Use then
3542 -- Ada 2022 Key-expression is present, rewrite node as an
3543 -- Iterated_Element_Association.
3545 Scan; -- past USE
3546 Build_Iterated_Element_Association;
3547 Set_Key_Expression (Assoc_Node, P_Expression);
3549 elsif Present (Filter) then
3550 -- A loop_parameter_specification also indicates an Ada 2022
3551 -- construct, in contrast with a subtype indication used in
3552 -- array aggregates.
3554 Build_Iterated_Element_Association;
3555 end if;
3557 TF_Arrow;
3558 Set_Expression (Assoc_Node, P_Expression);
3560 when Tok_Of =>
3561 Restore_Scan_State (State);
3562 Scan; -- past OF
3563 Iter_Spec := P_Iterator_Specification (Id);
3564 Set_Iterator_Specification (Assoc_Node, Iter_Spec);
3566 if Token = Tok_Use then
3567 Scan; -- past USE
3568 -- This is an iterated_element_association
3570 Assoc_Node :=
3571 New_Node (N_Iterated_Element_Association, Prev_Token_Ptr);
3572 Set_Iterator_Specification (Assoc_Node, Iter_Spec);
3573 Set_Key_Expression (Assoc_Node, P_Expression);
3574 end if;
3576 TF_Arrow;
3577 Set_Expression (Assoc_Node, P_Expression);
3579 when others =>
3580 Error_Msg_AP ("missing IN or OF");
3581 end case;
3583 return Assoc_Node;
3584 end P_Iterated_Component_Association;
3586 ---------------------
3587 -- P_If_Expression --
3588 ---------------------
3590 -- IF_EXPRESSION ::=
3591 -- if CONDITION then DEPENDENT_EXPRESSION
3592 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3593 -- [else DEPENDENT_EXPRESSION]
3595 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3597 function P_If_Expression return Node_Id is
3598 function P_If_Expression_Internal
3599 (Loc : Source_Ptr;
3600 Cond : Node_Id) return Node_Id;
3601 -- This is the internal recursive routine that does all the work, it is
3602 -- recursive since it is used to process ELSIF parts, which internally
3603 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3604 -- sequence is like the outer function except that the caller passes
3605 -- the conditional expression (scanned using P_Expression), and the
3606 -- scan pointer points just past this expression. Loc points to the
3607 -- IF or ELSIF token.
3609 ------------------------------
3610 -- P_If_Expression_Internal --
3611 ------------------------------
3613 function P_If_Expression_Internal
3614 (Loc : Source_Ptr;
3615 Cond : Node_Id) return Node_Id
3617 Exprs : constant List_Id := New_List;
3618 Expr : Node_Id;
3619 State : Saved_Scan_State;
3620 Eptr : Source_Ptr;
3622 begin
3623 -- All cases except where we are at right paren
3625 if Token /= Tok_Right_Paren then
3626 TF_Then;
3627 Append_To (Exprs, P_Condition (Cond));
3628 Append_To (Exprs, P_Expression);
3630 -- Case of right paren (missing THEN phrase). Note that we know this
3631 -- is the IF case, since the caller dealt with this possibility in
3632 -- the ELSIF case.
3634 else
3635 Error_Msg_BC ("missing THEN phrase");
3636 Append_To (Exprs, P_Condition (Cond));
3637 end if;
3639 -- We now have scanned out IF expr THEN expr
3641 -- Check for common error of semicolon before the ELSE
3643 if Token = Tok_Semicolon then
3644 Save_Scan_State (State);
3645 Scan; -- past semicolon
3647 if Token in Tok_Else | Tok_Elsif then
3648 Error_Msg_SP -- CODEFIX
3649 ("|extra "";"" ignored");
3651 else
3652 Restore_Scan_State (State);
3653 end if;
3654 end if;
3656 -- Scan out ELSIF sequence if present
3658 if Token = Tok_Elsif then
3659 Eptr := Token_Ptr;
3660 Scan; -- past ELSIF
3661 Expr := P_Expression;
3663 -- If we are at a right paren, we assume the ELSIF should be ELSE
3665 if Token = Tok_Right_Paren then
3666 Error_Msg ("ELSIF should be ELSE", Eptr);
3667 Append_To (Exprs, Expr);
3669 -- Otherwise we have an OK ELSIF
3671 else
3672 Expr := P_If_Expression_Internal (Eptr, Expr);
3673 Set_Is_Elsif (Expr);
3674 Append_To (Exprs, Expr);
3675 end if;
3677 -- Scan out ELSE phrase if present
3679 elsif Token = Tok_Else then
3681 -- Scan out ELSE expression
3683 Scan; -- Past ELSE
3684 Append_To (Exprs, P_Expression);
3686 -- Skip redundant ELSE parts
3688 while Token = Tok_Else loop
3689 Error_Msg_SC ("only one ELSE part is allowed");
3690 Scan; -- past ELSE
3691 Discard_Junk_Node (P_Expression);
3692 end loop;
3694 -- Two expression case (implied True, filled in during semantics)
3696 else
3697 null;
3698 end if;
3700 -- If we have an END IF, diagnose as not needed
3702 if Token = Tok_End then
3703 Error_Msg_SC ("`END IF` not allowed at end of if expression");
3704 Scan; -- past END
3706 if Token = Tok_If then
3707 Scan; -- past IF;
3708 end if;
3709 end if;
3711 -- Return the If_Expression node
3713 return Make_If_Expression (Loc, Expressions => Exprs);
3714 end P_If_Expression_Internal;
3716 -- Local variables
3718 Loc : constant Source_Ptr := Token_Ptr;
3719 If_Expr : Node_Id;
3721 -- Start of processing for P_If_Expression
3723 begin
3724 Error_Msg_Ada_2012_Feature ("|if expression", Token_Ptr);
3725 Scan; -- past IF
3726 Inside_If_Expression := Inside_If_Expression + 1;
3727 If_Expr := P_If_Expression_Internal (Loc, P_Expression);
3728 Inside_If_Expression := Inside_If_Expression - 1;
3729 return If_Expr;
3730 end P_If_Expression;
3732 --------------------------
3733 -- P_Declare_Expression --
3734 --------------------------
3736 -- DECLARE_EXPRESSION ::=
3737 -- DECLARE {DECLARE_ITEM}
3738 -- begin BODY_EXPRESSION
3740 -- DECLARE_ITEM ::= OBJECT_DECLARATION
3741 -- | OBJECT_RENAMING_DECLARATION
3743 function P_Declare_Expression return Node_Id is
3744 Loc : constant Source_Ptr := Token_Ptr;
3745 begin
3746 Scan; -- past DECLARE
3748 declare
3749 Actions : constant List_Id := P_Basic_Declarative_Items
3750 (Declare_Expression => True);
3751 -- Most declarative items allowed by P_Basic_Declarative_Items are
3752 -- illegal; semantic analysis will deal with that.
3753 begin
3754 if Token = Tok_Begin then
3755 Scan;
3756 else
3757 Error_Msg_SC -- CODEFIX
3758 ("BEGIN expected!");
3759 end if;
3761 declare
3762 Expression : constant Node_Id := P_Expression;
3763 Result : constant Node_Id :=
3764 Make_Expression_With_Actions (Loc, Actions, Expression);
3765 begin
3766 Error_Msg_Ada_2022_Feature ("declare expression", Loc);
3768 return Result;
3769 end;
3770 end;
3771 end P_Declare_Expression;
3773 -----------------------
3774 -- P_Membership_Test --
3775 -----------------------
3777 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERSHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3778 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3780 procedure P_Membership_Test (N : Node_Id) is
3781 Alt : constant Node_Id :=
3782 P_Range_Or_Subtype_Mark
3783 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3785 begin
3786 -- Set case
3788 if Token = Tok_Vertical_Bar then
3789 Error_Msg_Ada_2012_Feature ("set notation", Token_Ptr);
3790 Set_Alternatives (N, New_List (Alt));
3791 Set_Right_Opnd (N, Empty);
3793 -- Loop to accumulate alternatives
3795 while Token = Tok_Vertical_Bar loop
3796 Scan; -- past vertical bar
3797 Append_To
3798 (Alternatives (N),
3799 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3800 end loop;
3802 -- Not set case
3804 else
3805 Set_Right_Opnd (N, Alt);
3806 Set_Alternatives (N, No_List);
3807 end if;
3808 end P_Membership_Test;
3810 -----------------------------
3811 -- P_Unparen_Cond_Expr_Etc --
3812 -----------------------------
3814 function P_Unparen_Cond_Expr_Etc return Node_Id is
3815 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3817 Result : Node_Id;
3818 Scan_State : Saved_Scan_State;
3820 begin
3821 -- Case expression
3823 if Token = Tok_Case then
3824 Result := P_Case_Expression;
3826 if not (Lparen and then Token = Tok_Right_Paren) then
3827 Error_Msg_N ("case expression must be parenthesized!", Result);
3828 end if;
3830 -- If expression
3832 elsif Token = Tok_If then
3833 Result := P_If_Expression;
3835 if not (Lparen and then Token = Tok_Right_Paren) then
3836 Error_Msg_N ("if expression must be parenthesized!", Result);
3837 end if;
3839 -- Quantified expression or iterated component association
3841 elsif Token = Tok_For then
3843 Save_Scan_State (Scan_State);
3844 Scan; -- past FOR
3846 if Token in Tok_All | Tok_Some then
3847 Restore_Scan_State (Scan_State);
3848 Result := P_Quantified_Expression;
3850 if not (Lparen and then Token = Tok_Right_Paren) then
3851 Error_Msg_N
3852 ("quantified expression must be parenthesized!", Result);
3853 end if;
3855 else
3856 -- If no quantifier keyword, this is an iterated component in
3857 -- an aggregate.
3859 Restore_Scan_State (Scan_State);
3860 Result := P_Iterated_Component_Association;
3861 end if;
3863 -- Declare expression
3865 elsif Token = Tok_Declare then
3866 Result := P_Declare_Expression;
3868 if not (Lparen and then Token = Tok_Right_Paren) then
3869 Error_Msg_N ("declare expression must be parenthesized!", Result);
3870 end if;
3872 -- No other possibility should exist (caller was supposed to check)
3874 else
3875 raise Program_Error;
3876 end if;
3878 -- Return expression (possibly after having given message)
3880 return Result;
3881 end P_Unparen_Cond_Expr_Etc;
3883 end Ch4;