Daily bump.
[official-gcc.git] / gcc / ada / par-ch4.adb
blob79aa85fad2d806827e0ed6b08d3630e896f7a684
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-2012, 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_Stub_Type => True,
44 Attribute_Version => True,
45 Attribute_Type_Key => True,
46 others => False);
47 -- This map contains True for parameterless attributes that return a
48 -- string or a type. For those attributes, a left parenthesis after
49 -- the attribute should not be analyzed as the beginning of a parameters
50 -- list because it may denote a slice operation (X'Img (1 .. 2)) or
51 -- a type conversion (X'Class (Y)).
53 -- Note that this map designates the minimum set of attributes where a
54 -- construct in parentheses that is not an argument can appear right
55 -- after the attribute. For attributes like 'Size, we do not put them
56 -- in the map. If someone writes X'Size (3), that's illegal in any case,
57 -- but we get a better error message by parsing the (3) as an illegal
58 -- argument to the attribute, rather than some meaningless junk that
59 -- follows the attribute.
61 -----------------------
62 -- Local Subprograms --
63 -----------------------
65 function P_Aggregate_Or_Paren_Expr return Node_Id;
66 function P_Allocator return Node_Id;
67 function P_Case_Expression_Alternative return Node_Id;
68 function P_Record_Or_Array_Component_Association return Node_Id;
69 function P_Factor return Node_Id;
70 function P_Primary return Node_Id;
71 function P_Relation return Node_Id;
72 function P_Term return Node_Id;
74 function P_Binary_Adding_Operator return Node_Kind;
75 function P_Logical_Operator return Node_Kind;
76 function P_Multiplying_Operator return Node_Kind;
77 function P_Relational_Operator return Node_Kind;
78 function P_Unary_Adding_Operator return Node_Kind;
80 procedure Bad_Range_Attribute (Loc : Source_Ptr);
81 -- Called to place complaint about bad range attribute at the given
82 -- source location. Terminates by raising Error_Resync.
84 procedure Check_Bad_Exp;
85 -- Called after scanning a**b, posts error if ** detected
87 procedure P_Membership_Test (N : Node_Id);
88 -- N is the node for a N_In or N_Not_In node whose right operand has not
89 -- yet been processed. It is called just after scanning out the IN keyword.
90 -- On return, either Right_Opnd or Alternatives is set, as appropriate.
92 function P_Range_Attribute_Reference (Prefix_Node : Node_Id) return Node_Id;
93 -- Scan a range attribute reference. The caller has scanned out the
94 -- prefix. The current token is known to be an apostrophe and the
95 -- following token is known to be RANGE.
97 function P_Unparen_Cond_Case_Quant_Expression return Node_Id;
98 -- This function is called with Token pointing to IF, CASE, or FOR, in a
99 -- context that allows a case, conditional, or quantified expression if
100 -- it is surrounded by parentheses. If not surrounded by parentheses, the
101 -- expression is still returned, but an error message is issued.
103 -------------------------
104 -- Bad_Range_Attribute --
105 -------------------------
107 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
108 begin
109 Error_Msg ("range attribute cannot be used in expression!", Loc);
110 Resync_Expression;
111 end Bad_Range_Attribute;
113 -------------------
114 -- Check_Bad_Exp --
115 -------------------
117 procedure Check_Bad_Exp is
118 begin
119 if Token = Tok_Double_Asterisk then
120 Error_Msg_SC ("parenthesization required for '*'*");
121 Scan; -- past **
122 Discard_Junk_Node (P_Primary);
123 Check_Bad_Exp;
124 end if;
125 end Check_Bad_Exp;
127 --------------------------
128 -- 4.1 Name (also 6.4) --
129 --------------------------
131 -- NAME ::=
132 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
133 -- | INDEXED_COMPONENT | SLICE
134 -- | SELECTED_COMPONENT | ATTRIBUTE
135 -- | TYPE_CONVERSION | FUNCTION_CALL
136 -- | CHARACTER_LITERAL
138 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
140 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
142 -- EXPLICIT_DEREFERENCE ::= NAME . all
144 -- IMPLICIT_DEREFERENCE ::= NAME
146 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
148 -- SLICE ::= PREFIX (DISCRETE_RANGE)
150 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
152 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
154 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
156 -- ATTRIBUTE_DESIGNATOR ::=
157 -- IDENTIFIER [(static_EXPRESSION)]
158 -- | access | delta | digits
160 -- FUNCTION_CALL ::=
161 -- function_NAME
162 -- | function_PREFIX ACTUAL_PARAMETER_PART
164 -- ACTUAL_PARAMETER_PART ::=
165 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
167 -- PARAMETER_ASSOCIATION ::=
168 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
170 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
172 -- Note: syntactically a procedure call looks just like a function call,
173 -- so this routine is in practice used to scan out procedure calls as well.
175 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
177 -- Error recovery: can raise Error_Resync
179 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
180 -- followed by either a left paren (qualified expression case), or by
181 -- range (range attribute case). All other uses of apostrophe (i.e. all
182 -- other attributes) are handled in this routine.
184 -- Error recovery: can raise Error_Resync
186 function P_Name return Node_Id is
187 Scan_State : Saved_Scan_State;
188 Name_Node : Node_Id;
189 Prefix_Node : Node_Id;
190 Ident_Node : Node_Id;
191 Expr_Node : Node_Id;
192 Range_Node : Node_Id;
193 Arg_Node : Node_Id;
195 Arg_List : List_Id := No_List; -- kill junk warning
196 Attr_Name : Name_Id := No_Name; -- kill junk warning
198 begin
199 -- Case of not a name
201 if Token not in Token_Class_Name then
203 -- If it looks like start of expression, complain and scan expression
205 if Token in Token_Class_Literal
206 or else Token = Tok_Left_Paren
207 then
208 Error_Msg_SC ("name expected");
209 return P_Expression;
211 -- Otherwise some other junk, not much we can do
213 else
214 Error_Msg_AP ("name expected");
215 raise Error_Resync;
216 end if;
217 end if;
219 -- Loop through designators in qualified name
221 Name_Node := Token_Node;
223 loop
224 Scan; -- past designator
225 exit when Token /= Tok_Dot;
226 Save_Scan_State (Scan_State); -- at dot
227 Scan; -- past dot
229 -- If we do not have another designator after the dot, then join
230 -- the normal circuit to handle a dot extension (may be .all or
231 -- character literal case). Otherwise loop back to scan the next
232 -- designator.
234 if Token not in Token_Class_Desig then
235 goto Scan_Name_Extension_Dot;
236 else
237 Prefix_Node := Name_Node;
238 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
239 Set_Prefix (Name_Node, Prefix_Node);
240 Set_Selector_Name (Name_Node, Token_Node);
241 end if;
242 end loop;
244 -- We have now scanned out a qualified designator. If the last token is
245 -- an operator symbol, then we certainly do not have the Snam case, so
246 -- we can just use the normal name extension check circuit
248 if Prev_Token = Tok_Operator_Symbol then
249 goto Scan_Name_Extension;
250 end if;
252 -- We have scanned out a qualified simple name, check for name extension
253 -- Note that we know there is no dot here at this stage, so the only
254 -- possible cases of name extension are apostrophe and left paren.
256 if Token = Tok_Apostrophe then
257 Save_Scan_State (Scan_State); -- at apostrophe
258 Scan; -- past apostrophe
260 -- Qualified expression in Ada 2012 mode (treated as a name)
262 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
263 goto Scan_Name_Extension_Apostrophe;
265 -- If left paren not in Ada 2012, then it is not part of the name,
266 -- since qualified expressions are not names in prior versions of
267 -- Ada, so return with Token backed up to point to the apostrophe.
268 -- The treatment for the range attribute is similar (we do not
269 -- consider x'range to be a name in this grammar).
271 elsif Token = Tok_Left_Paren or else Token = Tok_Range then
272 Restore_Scan_State (Scan_State); -- to apostrophe
273 Expr_Form := EF_Simple_Name;
274 return Name_Node;
276 -- Otherwise we have the case of a name extended by an attribute
278 else
279 goto Scan_Name_Extension_Apostrophe;
280 end if;
282 -- Check case of qualified simple name extended by a left parenthesis
284 elsif Token = Tok_Left_Paren then
285 Scan; -- past left paren
286 goto Scan_Name_Extension_Left_Paren;
288 -- Otherwise the qualified simple name is not extended, so return
290 else
291 Expr_Form := EF_Simple_Name;
292 return Name_Node;
293 end if;
295 -- Loop scanning past name extensions. A label is used for control
296 -- transfer for this loop for ease of interfacing with the finite state
297 -- machine in the parenthesis scanning circuit, and also to allow for
298 -- passing in control to the appropriate point from the above code.
300 <<Scan_Name_Extension>>
302 -- Character literal used as name cannot be extended. Also this
303 -- cannot be a call, since the name for a call must be a designator.
304 -- Return in these cases, or if there is no name extension
306 if Token not in Token_Class_Namext
307 or else Prev_Token = Tok_Char_Literal
308 then
309 Expr_Form := EF_Name;
310 return Name_Node;
311 end if;
313 -- Merge here when we know there is a name extension
315 <<Scan_Name_Extension_OK>>
317 if Token = Tok_Left_Paren then
318 Scan; -- past left paren
319 goto Scan_Name_Extension_Left_Paren;
321 elsif Token = Tok_Apostrophe then
322 Save_Scan_State (Scan_State); -- at apostrophe
323 Scan; -- past apostrophe
324 goto Scan_Name_Extension_Apostrophe;
326 else -- Token = Tok_Dot
327 Save_Scan_State (Scan_State); -- at dot
328 Scan; -- past dot
329 goto Scan_Name_Extension_Dot;
330 end if;
332 -- Case of name extended by dot (selection), dot is already skipped
333 -- and the scan state at the point of the dot is saved in Scan_State.
335 <<Scan_Name_Extension_Dot>>
337 -- Explicit dereference case
339 if Token = Tok_All then
340 Prefix_Node := Name_Node;
341 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
342 Set_Prefix (Name_Node, Prefix_Node);
343 Scan; -- past ALL
344 goto Scan_Name_Extension;
346 -- Selected component case
348 elsif Token in Token_Class_Name then
349 Prefix_Node := Name_Node;
350 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
351 Set_Prefix (Name_Node, Prefix_Node);
352 Set_Selector_Name (Name_Node, Token_Node);
353 Scan; -- past selector
354 goto Scan_Name_Extension;
356 -- Reserved identifier as selector
358 elsif Is_Reserved_Identifier then
359 Scan_Reserved_Identifier (Force_Msg => False);
360 Prefix_Node := Name_Node;
361 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
362 Set_Prefix (Name_Node, Prefix_Node);
363 Set_Selector_Name (Name_Node, Token_Node);
364 Scan; -- past identifier used as selector
365 goto Scan_Name_Extension;
367 -- If dot is at end of line and followed by nothing legal,
368 -- then assume end of name and quit (dot will be taken as
369 -- an erroneous form of some other punctuation by our caller).
371 elsif Token_Is_At_Start_Of_Line then
372 Restore_Scan_State (Scan_State);
373 return Name_Node;
375 -- Here if nothing legal after the dot
377 else
378 Error_Msg_AP ("selector expected");
379 raise Error_Resync;
380 end if;
382 -- Here for an apostrophe as name extension. The scan position at the
383 -- apostrophe has already been saved, and the apostrophe scanned out.
385 <<Scan_Name_Extension_Apostrophe>>
387 Scan_Apostrophe : declare
388 function Apostrophe_Should_Be_Semicolon return Boolean;
389 -- Checks for case where apostrophe should probably be
390 -- a semicolon, and if so, gives appropriate message,
391 -- resets the scan pointer to the apostrophe, changes
392 -- the current token to Tok_Semicolon, and returns True.
393 -- Otherwise returns False.
395 ------------------------------------
396 -- Apostrophe_Should_Be_Semicolon --
397 ------------------------------------
399 function Apostrophe_Should_Be_Semicolon return Boolean is
400 begin
401 if Token_Is_At_Start_Of_Line then
402 Restore_Scan_State (Scan_State); -- to apostrophe
403 Error_Msg_SC ("|""''"" should be "";""");
404 Token := Tok_Semicolon;
405 return True;
406 else
407 return False;
408 end if;
409 end Apostrophe_Should_Be_Semicolon;
411 -- Start of processing for Scan_Apostrophe
413 begin
414 -- Check for qualified expression case in Ada 2012 mode
416 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
417 Name_Node := P_Qualified_Expression (Name_Node);
418 goto Scan_Name_Extension;
420 -- If range attribute after apostrophe, then return with Token
421 -- pointing to the apostrophe. Note that in this case the prefix
422 -- need not be a simple name (cases like A.all'range). Similarly
423 -- if there is a left paren after the apostrophe, then we also
424 -- return with Token pointing to the apostrophe (this is the
425 -- aggregate case, or some error case).
427 elsif Token = Tok_Range or else Token = Tok_Left_Paren then
428 Restore_Scan_State (Scan_State); -- to apostrophe
429 Expr_Form := EF_Name;
430 return Name_Node;
432 -- Here for cases where attribute designator is an identifier
434 elsif Token = Tok_Identifier then
435 Attr_Name := Token_Name;
437 if not Is_Attribute_Name (Attr_Name) then
438 if Apostrophe_Should_Be_Semicolon then
439 Expr_Form := EF_Name;
440 return Name_Node;
442 -- Here for a bad attribute name
444 else
445 Signal_Bad_Attribute;
446 Scan; -- past bad identifier
448 if Token = Tok_Left_Paren then
449 Scan; -- past left paren
451 loop
452 Discard_Junk_Node (P_Expression_If_OK);
453 exit when not Comma_Present;
454 end loop;
456 T_Right_Paren;
457 end if;
459 return Error;
460 end if;
461 end if;
463 if Style_Check then
464 Style.Check_Attribute_Name (False);
465 end if;
467 -- Here for case of attribute designator is not an identifier
469 else
470 if Token = Tok_Delta then
471 Attr_Name := Name_Delta;
473 elsif Token = Tok_Digits then
474 Attr_Name := Name_Digits;
476 elsif Token = Tok_Access then
477 Attr_Name := Name_Access;
479 elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
480 Attr_Name := Name_Mod;
482 elsif Apostrophe_Should_Be_Semicolon then
483 Expr_Form := EF_Name;
484 return Name_Node;
486 else
487 Error_Msg_AP ("attribute designator expected");
488 raise Error_Resync;
489 end if;
491 if Style_Check then
492 Style.Check_Attribute_Name (True);
493 end if;
494 end if;
496 -- We come here with an OK attribute scanned, and corresponding
497 -- Attribute identifier node stored in Ident_Node.
499 Prefix_Node := Name_Node;
500 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
501 Scan; -- past attribute designator
502 Set_Prefix (Name_Node, Prefix_Node);
503 Set_Attribute_Name (Name_Node, Attr_Name);
505 -- Scan attribute arguments/designator. We skip this if we know
506 -- that the attribute cannot have an argument.
508 if Token = Tok_Left_Paren
509 and then not
510 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
511 then
512 Set_Expressions (Name_Node, New_List);
513 Scan; -- past left paren
515 loop
516 declare
517 Expr : constant Node_Id := P_Expression_If_OK;
519 begin
520 if Token = Tok_Arrow then
521 Error_Msg_SC
522 ("named parameters not permitted for attributes");
523 Scan; -- past junk arrow
525 else
526 Append (Expr, Expressions (Name_Node));
527 exit when not Comma_Present;
528 end if;
529 end;
530 end loop;
532 T_Right_Paren;
533 end if;
535 goto Scan_Name_Extension;
536 end Scan_Apostrophe;
538 -- Here for left parenthesis extending name (left paren skipped)
540 <<Scan_Name_Extension_Left_Paren>>
542 -- We now have to scan through a list of items, terminated by a
543 -- right parenthesis. The scan is handled by a finite state
544 -- machine. The possibilities are:
546 -- (discrete_range)
548 -- This is a slice. This case is handled in LP_State_Init
550 -- (expression, expression, ..)
552 -- This is interpreted as an indexed component, i.e. as a
553 -- case of a name which can be extended in the normal manner.
554 -- This case is handled by LP_State_Name or LP_State_Expr.
556 -- Note: conditional expressions (without an extra level of
557 -- parentheses) are permitted in this context).
559 -- (..., identifier => expression , ...)
561 -- If there is at least one occurrence of identifier => (but
562 -- none of the other cases apply), then we have a call.
564 -- Test for Id => case
566 if Token = Tok_Identifier then
567 Save_Scan_State (Scan_State); -- at Id
568 Scan; -- past Id
570 -- Test for => (allow := as an error substitute)
572 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
573 Restore_Scan_State (Scan_State); -- to Id
574 Arg_List := New_List;
575 goto LP_State_Call;
577 else
578 Restore_Scan_State (Scan_State); -- to Id
579 end if;
580 end if;
582 -- Here we have an expression after all
584 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
586 -- Check cases of discrete range for a slice
588 -- First possibility: Range_Attribute_Reference
590 if Expr_Form = EF_Range_Attr then
591 Range_Node := Expr_Node;
593 -- Second possibility: Simple_expression .. Simple_expression
595 elsif Token = Tok_Dot_Dot then
596 Check_Simple_Expression (Expr_Node);
597 Range_Node := New_Node (N_Range, Token_Ptr);
598 Set_Low_Bound (Range_Node, Expr_Node);
599 Scan; -- past ..
600 Expr_Node := P_Expression;
601 Check_Simple_Expression (Expr_Node);
602 Set_High_Bound (Range_Node, Expr_Node);
604 -- Third possibility: Type_name range Range
606 elsif Token = Tok_Range then
607 if Expr_Form /= EF_Simple_Name then
608 Error_Msg_SC ("subtype mark must precede RANGE");
609 raise Error_Resync;
610 end if;
612 Range_Node := P_Subtype_Indication (Expr_Node);
614 -- Otherwise we just have an expression. It is true that we might
615 -- have a subtype mark without a range constraint but this case
616 -- is syntactically indistinguishable from the expression case.
618 else
619 Arg_List := New_List;
620 goto LP_State_Expr;
621 end if;
623 -- Fall through here with unmistakable Discrete range scanned,
624 -- which means that we definitely have the case of a slice. The
625 -- Discrete range is in Range_Node.
627 if Token = Tok_Comma then
628 Error_Msg_SC ("slice cannot have more than one dimension");
629 raise Error_Resync;
631 elsif Token /= Tok_Right_Paren then
632 if Token = Tok_Arrow then
634 -- This may be an aggregate that is missing a qualification
636 Error_Msg_SC
637 ("context of aggregate must be a qualified expression");
638 raise Error_Resync;
640 else
641 T_Right_Paren;
642 raise Error_Resync;
643 end if;
645 else
646 Scan; -- past right paren
647 Prefix_Node := Name_Node;
648 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
649 Set_Prefix (Name_Node, Prefix_Node);
650 Set_Discrete_Range (Name_Node, Range_Node);
652 -- An operator node is legal as a prefix to other names,
653 -- but not for a slice.
655 if Nkind (Prefix_Node) = N_Operator_Symbol then
656 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
657 end if;
659 -- If we have a name extension, go scan it
661 if Token in Token_Class_Namext then
662 goto Scan_Name_Extension_OK;
664 -- Otherwise return (a slice is a name, but is not a call)
666 else
667 Expr_Form := EF_Name;
668 return Name_Node;
669 end if;
670 end if;
672 -- In LP_State_Expr, we have scanned one or more expressions, and
673 -- so we have a call or an indexed component which is a name. On
674 -- entry we have the expression just scanned in Expr_Node and
675 -- Arg_List contains the list of expressions encountered so far
677 <<LP_State_Expr>>
678 Append (Expr_Node, Arg_List);
680 if Token = Tok_Arrow then
681 Error_Msg
682 ("expect identifier in parameter association",
683 Sloc (Expr_Node));
684 Scan; -- past arrow
686 elsif not Comma_Present then
687 T_Right_Paren;
688 Prefix_Node := Name_Node;
689 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
690 Set_Prefix (Name_Node, Prefix_Node);
691 Set_Expressions (Name_Node, Arg_List);
692 goto Scan_Name_Extension;
693 end if;
695 -- Comma present (and scanned out), test for identifier => case
696 -- Test for identifier => case
698 if Token = Tok_Identifier then
699 Save_Scan_State (Scan_State); -- at Id
700 Scan; -- past Id
702 -- Test for => (allow := as error substitute)
704 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
705 Restore_Scan_State (Scan_State); -- to Id
706 goto LP_State_Call;
708 -- Otherwise it's just an expression after all, so backup
710 else
711 Restore_Scan_State (Scan_State); -- to Id
712 end if;
713 end if;
715 -- Here we have an expression after all, so stay in this state
717 Expr_Node := P_Expression_If_OK;
718 goto LP_State_Expr;
720 -- LP_State_Call corresponds to the situation in which at least
721 -- one instance of Id => Expression has been encountered, so we
722 -- know that we do not have a name, but rather a call. We enter
723 -- it with the scan pointer pointing to the next argument to scan,
724 -- and Arg_List containing the list of arguments scanned so far.
726 <<LP_State_Call>>
728 -- Test for case of Id => Expression (named parameter)
730 if Token = Tok_Identifier then
731 Save_Scan_State (Scan_State); -- at Id
732 Ident_Node := Token_Node;
733 Scan; -- past Id
735 -- Deal with => (allow := as erroneous substitute)
737 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
738 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
739 Set_Selector_Name (Arg_Node, Ident_Node);
740 T_Arrow;
741 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
742 Append (Arg_Node, Arg_List);
744 -- If a comma follows, go back and scan next entry
746 if Comma_Present then
747 goto LP_State_Call;
749 -- Otherwise we have the end of a call
751 else
752 Prefix_Node := Name_Node;
753 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
754 Set_Name (Name_Node, Prefix_Node);
755 Set_Parameter_Associations (Name_Node, Arg_List);
756 T_Right_Paren;
758 if Token in Token_Class_Namext then
759 goto Scan_Name_Extension_OK;
761 -- This is a case of a call which cannot be a name
763 else
764 Expr_Form := EF_Name;
765 return Name_Node;
766 end if;
767 end if;
769 -- Not named parameter: Id started an expression after all
771 else
772 Restore_Scan_State (Scan_State); -- to Id
773 end if;
774 end if;
776 -- Here if entry did not start with Id => which means that it
777 -- is a positional parameter, which is not allowed, since we
778 -- have seen at least one named parameter already.
780 Error_Msg_SC
781 ("positional parameter association " &
782 "not allowed after named one");
784 Expr_Node := P_Expression_If_OK;
786 -- Leaving the '>' in an association is not unusual, so suggest
787 -- a possible fix.
789 if Nkind (Expr_Node) = N_Op_Eq then
790 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
791 end if;
793 -- We go back to scanning out expressions, so that we do not get
794 -- multiple error messages when several positional parameters
795 -- follow a named parameter.
797 goto LP_State_Expr;
799 -- End of treatment for name extensions starting with left paren
801 -- End of loop through name extensions
803 end P_Name;
805 -- This function parses a restricted form of Names which are either
806 -- designators, or designators preceded by a sequence of prefixes
807 -- that are direct names.
809 -- Error recovery: cannot raise Error_Resync
811 function P_Function_Name return Node_Id is
812 Designator_Node : Node_Id;
813 Prefix_Node : Node_Id;
814 Selector_Node : Node_Id;
815 Dot_Sloc : Source_Ptr := No_Location;
817 begin
818 -- Prefix_Node is set to the gathered prefix so far, Empty means that
819 -- no prefix has been scanned. This allows us to build up the result
820 -- in the required right recursive manner.
822 Prefix_Node := Empty;
824 -- Loop through prefixes
826 loop
827 Designator_Node := Token_Node;
829 if Token not in Token_Class_Desig then
830 return P_Identifier; -- let P_Identifier issue the error message
832 else -- Token in Token_Class_Desig
833 Scan; -- past designator
834 exit when Token /= Tok_Dot;
835 end if;
837 -- Here at a dot, with token just before it in Designator_Node
839 if No (Prefix_Node) then
840 Prefix_Node := Designator_Node;
841 else
842 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
843 Set_Prefix (Selector_Node, Prefix_Node);
844 Set_Selector_Name (Selector_Node, Designator_Node);
845 Prefix_Node := Selector_Node;
846 end if;
848 Dot_Sloc := Token_Ptr;
849 Scan; -- past dot
850 end loop;
852 -- Fall out of the loop having just scanned a designator
854 if No (Prefix_Node) then
855 return Designator_Node;
856 else
857 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
858 Set_Prefix (Selector_Node, Prefix_Node);
859 Set_Selector_Name (Selector_Node, Designator_Node);
860 return Selector_Node;
861 end if;
863 exception
864 when Error_Resync =>
865 return Error;
866 end P_Function_Name;
868 -- This function parses a restricted form of Names which are either
869 -- identifiers, or identifiers preceded by a sequence of prefixes
870 -- that are direct names.
872 -- Error recovery: cannot raise Error_Resync
874 function P_Qualified_Simple_Name return Node_Id is
875 Designator_Node : Node_Id;
876 Prefix_Node : Node_Id;
877 Selector_Node : Node_Id;
878 Dot_Sloc : Source_Ptr := No_Location;
880 begin
881 -- Prefix node is set to the gathered prefix so far, Empty means that
882 -- no prefix has been scanned. This allows us to build up the result
883 -- in the required right recursive manner.
885 Prefix_Node := Empty;
887 -- Loop through prefixes
889 loop
890 Designator_Node := Token_Node;
892 if Token = Tok_Identifier then
893 Scan; -- past identifier
894 exit when Token /= Tok_Dot;
896 elsif Token not in Token_Class_Desig then
897 return P_Identifier; -- let P_Identifier issue the error message
899 else
900 Scan; -- past designator
902 if Token /= Tok_Dot then
903 Error_Msg_SP ("identifier expected");
904 return Error;
905 end if;
906 end if;
908 -- Here at a dot, with token just before it in Designator_Node
910 if No (Prefix_Node) then
911 Prefix_Node := Designator_Node;
912 else
913 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
914 Set_Prefix (Selector_Node, Prefix_Node);
915 Set_Selector_Name (Selector_Node, Designator_Node);
916 Prefix_Node := Selector_Node;
917 end if;
919 Dot_Sloc := Token_Ptr;
920 Scan; -- past dot
921 end loop;
923 -- Fall out of the loop having just scanned an identifier
925 if No (Prefix_Node) then
926 return Designator_Node;
927 else
928 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
929 Set_Prefix (Selector_Node, Prefix_Node);
930 Set_Selector_Name (Selector_Node, Designator_Node);
931 return Selector_Node;
932 end if;
934 exception
935 when Error_Resync =>
936 return Error;
937 end P_Qualified_Simple_Name;
939 -- This procedure differs from P_Qualified_Simple_Name only in that it
940 -- raises Error_Resync if any error is encountered. It only returns after
941 -- scanning a valid qualified simple name.
943 -- Error recovery: can raise Error_Resync
945 function P_Qualified_Simple_Name_Resync return Node_Id is
946 Designator_Node : Node_Id;
947 Prefix_Node : Node_Id;
948 Selector_Node : Node_Id;
949 Dot_Sloc : Source_Ptr := No_Location;
951 begin
952 Prefix_Node := Empty;
954 -- Loop through prefixes
956 loop
957 Designator_Node := Token_Node;
959 if Token = Tok_Identifier then
960 Scan; -- past identifier
961 exit when Token /= Tok_Dot;
963 elsif Token not in Token_Class_Desig then
964 Discard_Junk_Node (P_Identifier); -- to issue the error message
965 raise Error_Resync;
967 else
968 Scan; -- past designator
970 if Token /= Tok_Dot then
971 Error_Msg_SP ("identifier expected");
972 raise Error_Resync;
973 end if;
974 end if;
976 -- Here at a dot, with token just before it in Designator_Node
978 if No (Prefix_Node) then
979 Prefix_Node := Designator_Node;
980 else
981 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
982 Set_Prefix (Selector_Node, Prefix_Node);
983 Set_Selector_Name (Selector_Node, Designator_Node);
984 Prefix_Node := Selector_Node;
985 end if;
987 Dot_Sloc := Token_Ptr;
988 Scan; -- past period
989 end loop;
991 -- Fall out of the loop having just scanned an identifier
993 if No (Prefix_Node) then
994 return Designator_Node;
995 else
996 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
997 Set_Prefix (Selector_Node, Prefix_Node);
998 Set_Selector_Name (Selector_Node, Designator_Node);
999 return Selector_Node;
1000 end if;
1001 end P_Qualified_Simple_Name_Resync;
1003 ----------------------
1004 -- 4.1 Direct_Name --
1005 ----------------------
1007 -- Parsed by P_Name and other functions in section 4.1
1009 -----------------
1010 -- 4.1 Prefix --
1011 -----------------
1013 -- Parsed by P_Name (4.1)
1015 -------------------------------
1016 -- 4.1 Explicit Dereference --
1017 -------------------------------
1019 -- Parsed by P_Name (4.1)
1021 -------------------------------
1022 -- 4.1 Implicit_Dereference --
1023 -------------------------------
1025 -- Parsed by P_Name (4.1)
1027 ----------------------------
1028 -- 4.1 Indexed Component --
1029 ----------------------------
1031 -- Parsed by P_Name (4.1)
1033 ----------------
1034 -- 4.1 Slice --
1035 ----------------
1037 -- Parsed by P_Name (4.1)
1039 -----------------------------
1040 -- 4.1 Selected_Component --
1041 -----------------------------
1043 -- Parsed by P_Name (4.1)
1045 ------------------------
1046 -- 4.1 Selector Name --
1047 ------------------------
1049 -- Parsed by P_Name (4.1)
1051 ------------------------------
1052 -- 4.1 Attribute Reference --
1053 ------------------------------
1055 -- Parsed by P_Name (4.1)
1057 -------------------------------
1058 -- 4.1 Attribute Designator --
1059 -------------------------------
1061 -- Parsed by P_Name (4.1)
1063 --------------------------------------
1064 -- 4.1.4 Range Attribute Reference --
1065 --------------------------------------
1067 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1069 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1071 -- In the grammar, a RANGE attribute is simply a name, but its use is
1072 -- highly restricted, so in the parser, we do not regard it as a name.
1073 -- Instead, P_Name returns without scanning the 'RANGE part of the
1074 -- attribute, and the caller uses the following function to construct
1075 -- a range attribute in places where it is appropriate.
1077 -- Note that RANGE here is treated essentially as an identifier,
1078 -- rather than a reserved word.
1080 -- The caller has parsed the prefix, i.e. a name, and Token points to
1081 -- the apostrophe. The token after the apostrophe is known to be RANGE
1082 -- at this point. The prefix node becomes the prefix of the attribute.
1084 -- Error_Recovery: Cannot raise Error_Resync
1086 function P_Range_Attribute_Reference
1087 (Prefix_Node : Node_Id)
1088 return Node_Id
1090 Attr_Node : Node_Id;
1092 begin
1093 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1094 Set_Prefix (Attr_Node, Prefix_Node);
1095 Scan; -- past apostrophe
1097 if Style_Check then
1098 Style.Check_Attribute_Name (True);
1099 end if;
1101 Set_Attribute_Name (Attr_Node, Name_Range);
1102 Scan; -- past RANGE
1104 if Token = Tok_Left_Paren then
1105 Scan; -- past left paren
1106 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1107 T_Right_Paren;
1108 end if;
1110 return Attr_Node;
1111 end P_Range_Attribute_Reference;
1113 ---------------------------------------
1114 -- 4.1.4 Range Attribute Designator --
1115 ---------------------------------------
1117 -- Parsed by P_Range_Attribute_Reference (4.4)
1119 --------------------
1120 -- 4.3 Aggregate --
1121 --------------------
1123 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1125 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1126 -- an aggregate is known to be required (code statement, extension
1127 -- aggregate), in which cases this routine performs the necessary check
1128 -- that we have an aggregate rather than a parenthesized expression
1130 -- Error recovery: can raise Error_Resync
1132 function P_Aggregate return Node_Id is
1133 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1134 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1136 begin
1137 if Nkind (Aggr_Node) /= N_Aggregate
1138 and then
1139 Nkind (Aggr_Node) /= N_Extension_Aggregate
1140 then
1141 Error_Msg
1142 ("aggregate may not have single positional component", Aggr_Sloc);
1143 return Error;
1144 else
1145 return Aggr_Node;
1146 end if;
1147 end P_Aggregate;
1149 ------------------------------------------------
1150 -- 4.3 Aggregate or Parenthesized Expression --
1151 ------------------------------------------------
1153 -- This procedure parses out either an aggregate or a parenthesized
1154 -- expression (these two constructs are closely related, since a
1155 -- parenthesized expression looks like an aggregate with a single
1156 -- positional component).
1158 -- AGGREGATE ::=
1159 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1161 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1163 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1164 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1165 -- | null record
1167 -- RECORD_COMPONENT_ASSOCIATION ::=
1168 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1170 -- COMPONENT_CHOICE_LIST ::=
1171 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1172 -- | others
1174 -- EXTENSION_AGGREGATE ::=
1175 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1177 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1179 -- ARRAY_AGGREGATE ::=
1180 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1182 -- POSITIONAL_ARRAY_AGGREGATE ::=
1183 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1184 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1185 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1187 -- NAMED_ARRAY_AGGREGATE ::=
1188 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1190 -- PRIMARY ::= (EXPRESSION);
1192 -- Error recovery: can raise Error_Resync
1194 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1195 -- to Ada 2005 limited aggregates (AI-287)
1197 function P_Aggregate_Or_Paren_Expr return Node_Id is
1198 Aggregate_Node : Node_Id;
1199 Expr_List : List_Id;
1200 Assoc_List : List_Id;
1201 Expr_Node : Node_Id;
1202 Lparen_Sloc : Source_Ptr;
1203 Scan_State : Saved_Scan_State;
1205 procedure Box_Error;
1206 -- Called if <> is encountered as positional aggregate element. Issues
1207 -- error message and sets Expr_Node to Error.
1209 ---------------
1210 -- Box_Error --
1211 ---------------
1213 procedure Box_Error is
1214 begin
1215 if Ada_Version < Ada_2005 then
1216 Error_Msg_SC ("box in aggregate is an Ada 2005 extension");
1217 end if;
1219 -- Ada 2005 (AI-287): The box notation is allowed only with named
1220 -- notation because positional notation might be error prone. For
1221 -- example, in "(X, <>, Y, <>)", there is no type associated with
1222 -- the boxes, so you might not be leaving out the components you
1223 -- thought you were leaving out.
1225 Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1226 Scan; -- past box
1227 Expr_Node := Error;
1228 end Box_Error;
1230 -- Start of processing for P_Aggregate_Or_Paren_Expr
1232 begin
1233 Lparen_Sloc := Token_Ptr;
1234 T_Left_Paren;
1236 -- Conditional expression case
1238 if Token = Tok_If then
1239 Expr_Node := P_Conditional_Expression;
1240 T_Right_Paren;
1241 return Expr_Node;
1243 -- Case expression case
1245 elsif Token = Tok_Case then
1246 Expr_Node := P_Case_Expression;
1247 T_Right_Paren;
1248 return Expr_Node;
1250 -- Quantified expression case
1252 elsif Token = Tok_For then
1253 Expr_Node := P_Quantified_Expression;
1254 T_Right_Paren;
1255 return Expr_Node;
1257 -- Note: the mechanism used here of rescanning the initial expression
1258 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1259 -- out the discrete choice list.
1261 -- Deal with expression and extension aggregate cases first
1263 elsif Token /= Tok_Others then
1264 Save_Scan_State (Scan_State); -- at start of expression
1266 -- Deal with (NULL RECORD) case
1268 if Token = Tok_Null then
1269 Scan; -- past NULL
1271 if Token = Tok_Record then
1272 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1273 Set_Null_Record_Present (Aggregate_Node, True);
1274 Scan; -- past RECORD
1275 T_Right_Paren;
1276 return Aggregate_Node;
1277 else
1278 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1279 end if;
1280 end if;
1282 -- Scan expression, handling box appearing as positional argument
1284 if Token = Tok_Box then
1285 Box_Error;
1286 else
1287 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1288 end if;
1290 -- Extension aggregate case
1292 if Token = Tok_With then
1293 if Nkind (Expr_Node) = N_Attribute_Reference
1294 and then Attribute_Name (Expr_Node) = Name_Range
1295 then
1296 Bad_Range_Attribute (Sloc (Expr_Node));
1297 return Error;
1298 end if;
1300 if Ada_Version = Ada_83 then
1301 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1302 end if;
1304 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1305 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1306 Scan; -- past WITH
1308 -- Deal with WITH NULL RECORD case
1310 if Token = Tok_Null then
1311 Save_Scan_State (Scan_State); -- at NULL
1312 Scan; -- past NULL
1314 if Token = Tok_Record then
1315 Scan; -- past RECORD
1316 Set_Null_Record_Present (Aggregate_Node, True);
1317 T_Right_Paren;
1318 return Aggregate_Node;
1320 else
1321 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1322 end if;
1323 end if;
1325 if Token /= Tok_Others then
1326 Save_Scan_State (Scan_State);
1327 Expr_Node := P_Expression;
1328 else
1329 Expr_Node := Empty;
1330 end if;
1332 -- Expression case
1334 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1335 if Nkind (Expr_Node) = N_Attribute_Reference
1336 and then Attribute_Name (Expr_Node) = Name_Range
1337 then
1338 Error_Msg
1339 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1340 Scan; -- past right paren
1341 return Expr_Node;
1342 end if;
1344 -- Bump paren count of expression
1346 if Expr_Node /= Error then
1347 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1348 end if;
1350 T_Right_Paren; -- past right paren (error message if none)
1351 return Expr_Node;
1353 -- Normal aggregate case
1355 else
1356 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1357 end if;
1359 -- Others case
1361 else
1362 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1363 Expr_Node := Empty;
1364 end if;
1366 -- Prepare to scan list of component associations
1368 Expr_List := No_List; -- don't set yet, maybe all named entries
1369 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1371 -- This loop scans through component associations. On entry to the
1372 -- loop, an expression has been scanned at the start of the current
1373 -- association unless initial token was OTHERS, in which case
1374 -- Expr_Node is set to Empty.
1376 loop
1377 -- Deal with others association first. This is a named association
1379 if No (Expr_Node) then
1380 if No (Assoc_List) then
1381 Assoc_List := New_List;
1382 end if;
1384 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1386 -- Improper use of WITH
1388 elsif Token = Tok_With then
1389 Error_Msg_SC ("WITH must be preceded by single expression in " &
1390 "extension aggregate");
1391 raise Error_Resync;
1393 -- Range attribute can only appear as part of a discrete choice list
1395 elsif Nkind (Expr_Node) = N_Attribute_Reference
1396 and then Attribute_Name (Expr_Node) = Name_Range
1397 and then Token /= Tok_Arrow
1398 and then Token /= Tok_Vertical_Bar
1399 then
1400 Bad_Range_Attribute (Sloc (Expr_Node));
1401 return Error;
1403 -- Assume positional case if comma, right paren, or literal or
1404 -- identifier or OTHERS follows (the latter cases are missing
1405 -- comma cases). Also assume positional if a semicolon follows,
1406 -- which can happen if there are missing parens
1408 elsif Token = Tok_Comma
1409 or else Token = Tok_Right_Paren
1410 or else Token = Tok_Others
1411 or else Token in Token_Class_Lit_Or_Name
1412 or else Token = Tok_Semicolon
1413 then
1414 if Present (Assoc_List) then
1415 Error_Msg_BC -- CODEFIX
1416 ("""='>"" expected (positional association cannot follow " &
1417 "named association)");
1418 end if;
1420 if No (Expr_List) then
1421 Expr_List := New_List;
1422 end if;
1424 Append (Expr_Node, Expr_List);
1426 -- Check for aggregate followed by left parent, maybe missing comma
1428 elsif Nkind (Expr_Node) = N_Aggregate
1429 and then Token = Tok_Left_Paren
1430 then
1431 T_Comma;
1433 if No (Expr_List) then
1434 Expr_List := New_List;
1435 end if;
1437 Append (Expr_Node, Expr_List);
1439 -- Anything else is assumed to be a named association
1441 else
1442 Restore_Scan_State (Scan_State); -- to start of expression
1444 if No (Assoc_List) then
1445 Assoc_List := New_List;
1446 end if;
1448 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1449 end if;
1451 exit when not Comma_Present;
1453 -- If we are at an expression terminator, something is seriously
1454 -- wrong, so let's get out now, before we start eating up stuff
1455 -- that doesn't belong to us!
1457 if Token in Token_Class_Eterm then
1458 Error_Msg_AP
1459 ("expecting expression or component association");
1460 exit;
1461 end if;
1463 -- Deal with misused box
1465 if Token = Tok_Box then
1466 Box_Error;
1468 -- Otherwise initiate for reentry to top of loop by scanning an
1469 -- initial expression, unless the first token is OTHERS.
1471 elsif Token = Tok_Others then
1472 Expr_Node := Empty;
1474 else
1475 Save_Scan_State (Scan_State); -- at start of expression
1476 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1478 end if;
1479 end loop;
1481 -- All component associations (positional and named) have been scanned
1483 T_Right_Paren;
1484 Set_Expressions (Aggregate_Node, Expr_List);
1485 Set_Component_Associations (Aggregate_Node, Assoc_List);
1486 return Aggregate_Node;
1487 end P_Aggregate_Or_Paren_Expr;
1489 ------------------------------------------------
1490 -- 4.3 Record or Array Component Association --
1491 ------------------------------------------------
1493 -- RECORD_COMPONENT_ASSOCIATION ::=
1494 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1495 -- | COMPONENT_CHOICE_LIST => <>
1497 -- COMPONENT_CHOICE_LIST =>
1498 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1499 -- | others
1501 -- ARRAY_COMPONENT_ASSOCIATION ::=
1502 -- DISCRETE_CHOICE_LIST => EXPRESSION
1503 -- | DISCRETE_CHOICE_LIST => <>
1505 -- Note: this routine only handles the named cases, including others.
1506 -- Cases where the component choice list is not present have already
1507 -- been handled directly.
1509 -- Error recovery: can raise Error_Resync
1511 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1512 -- rules have been extended to give support to Ada 2005 limited
1513 -- aggregates (AI-287)
1515 function P_Record_Or_Array_Component_Association return Node_Id is
1516 Assoc_Node : Node_Id;
1518 begin
1519 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1520 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1521 Set_Sloc (Assoc_Node, Token_Ptr);
1522 TF_Arrow;
1524 if Token = Tok_Box then
1526 -- Ada 2005(AI-287): The box notation is used to indicate the
1527 -- default initialization of aggregate components
1529 if Ada_Version < Ada_2005 then
1530 Error_Msg_SP
1531 ("component association with '<'> is an Ada 2005 extension");
1532 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1533 end if;
1535 Set_Box_Present (Assoc_Node);
1536 Scan; -- Past box
1537 else
1538 Set_Expression (Assoc_Node, P_Expression);
1539 end if;
1541 return Assoc_Node;
1542 end P_Record_Or_Array_Component_Association;
1544 -----------------------------
1545 -- 4.3.1 Record Aggregate --
1546 -----------------------------
1548 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1549 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1551 ----------------------------------------------
1552 -- 4.3.1 Record Component Association List --
1553 ----------------------------------------------
1555 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1557 ----------------------------------
1558 -- 4.3.1 Component Choice List --
1559 ----------------------------------
1561 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1563 --------------------------------
1564 -- 4.3.1 Extension Aggregate --
1565 --------------------------------
1567 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1569 --------------------------
1570 -- 4.3.1 Ancestor Part --
1571 --------------------------
1573 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1575 ----------------------------
1576 -- 4.3.1 Array Aggregate --
1577 ----------------------------
1579 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1581 ---------------------------------------
1582 -- 4.3.1 Positional Array Aggregate --
1583 ---------------------------------------
1585 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1587 ----------------------------------
1588 -- 4.3.1 Named Array Aggregate --
1589 ----------------------------------
1591 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1593 ----------------------------------------
1594 -- 4.3.1 Array Component Association --
1595 ----------------------------------------
1597 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1599 ---------------------
1600 -- 4.4 Expression --
1601 ---------------------
1603 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1605 -- EXPRESSION ::=
1606 -- RELATION {LOGICAL_OPERATOR RELATION}
1608 -- CHOICE_EXPRESSION ::=
1609 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1611 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1613 -- On return, Expr_Form indicates the categorization of the expression
1614 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1615 -- an error message is given, and Error is returned).
1617 -- Error recovery: cannot raise Error_Resync
1619 function P_Expression return Node_Id is
1620 Logical_Op : Node_Kind;
1621 Prev_Logical_Op : Node_Kind;
1622 Op_Location : Source_Ptr;
1623 Node1 : Node_Id;
1624 Node2 : Node_Id;
1626 begin
1627 Node1 := P_Relation;
1629 if Token in Token_Class_Logop then
1630 Prev_Logical_Op := N_Empty;
1632 loop
1633 Op_Location := Token_Ptr;
1634 Logical_Op := P_Logical_Operator;
1636 if Prev_Logical_Op /= N_Empty and then
1637 Logical_Op /= Prev_Logical_Op
1638 then
1639 Error_Msg
1640 ("mixed logical operators in expression", Op_Location);
1641 Prev_Logical_Op := N_Empty;
1642 else
1643 Prev_Logical_Op := Logical_Op;
1644 end if;
1646 Node2 := Node1;
1647 Node1 := New_Op_Node (Logical_Op, Op_Location);
1648 Set_Left_Opnd (Node1, Node2);
1649 Set_Right_Opnd (Node1, P_Relation);
1650 exit when Token not in Token_Class_Logop;
1651 end loop;
1653 Expr_Form := EF_Non_Simple;
1654 end if;
1656 if Token = Tok_Apostrophe then
1657 Bad_Range_Attribute (Token_Ptr);
1658 return Error;
1659 else
1660 return Node1;
1661 end if;
1662 end P_Expression;
1664 -- This function is identical to the normal P_Expression, except that it
1665 -- also permits the appearance of a case, conditional, or quantified
1666 -- expression if the call immediately follows a left paren, and followed
1667 -- by a right parenthesis. These forms are allowed if these conditions
1668 -- are not met, but an error message will be issued.
1670 function P_Expression_If_OK return Node_Id is
1671 begin
1672 -- Case of conditional, case or quantified expression
1674 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1675 return P_Unparen_Cond_Case_Quant_Expression;
1677 -- Normal case, not case/conditional/quantified expression
1679 else
1680 return P_Expression;
1681 end if;
1682 end P_Expression_If_OK;
1684 -- This function is identical to the normal P_Expression, except that it
1685 -- checks that the expression scan did not stop on a right paren. It is
1686 -- called in all contexts where a right parenthesis cannot legitimately
1687 -- follow an expression.
1689 -- Error recovery: can not raise Error_Resync
1691 function P_Expression_No_Right_Paren return Node_Id is
1692 Expr : constant Node_Id := P_Expression;
1693 begin
1694 Ignore (Tok_Right_Paren);
1695 return Expr;
1696 end P_Expression_No_Right_Paren;
1698 ----------------------------------------
1699 -- 4.4 Expression_Or_Range_Attribute --
1700 ----------------------------------------
1702 -- EXPRESSION ::=
1703 -- RELATION {and RELATION} | RELATION {and then RELATION}
1704 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1705 -- | RELATION {xor RELATION}
1707 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1709 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1711 -- On return, Expr_Form indicates the categorization of the expression
1712 -- and EF_Range_Attr is one of the possibilities.
1714 -- Error recovery: cannot raise Error_Resync
1716 -- In the grammar, a RANGE attribute is simply a name, but its use is
1717 -- highly restricted, so in the parser, we do not regard it as a name.
1718 -- Instead, P_Name returns without scanning the 'RANGE part of the
1719 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1720 -- attribute reference. In the normal case where a range attribute is
1721 -- not allowed, an error message is issued by P_Expression.
1723 function P_Expression_Or_Range_Attribute return Node_Id is
1724 Logical_Op : Node_Kind;
1725 Prev_Logical_Op : Node_Kind;
1726 Op_Location : Source_Ptr;
1727 Node1 : Node_Id;
1728 Node2 : Node_Id;
1729 Attr_Node : Node_Id;
1731 begin
1732 Node1 := P_Relation;
1734 if Token = Tok_Apostrophe then
1735 Attr_Node := P_Range_Attribute_Reference (Node1);
1736 Expr_Form := EF_Range_Attr;
1737 return Attr_Node;
1739 elsif Token in Token_Class_Logop then
1740 Prev_Logical_Op := N_Empty;
1742 loop
1743 Op_Location := Token_Ptr;
1744 Logical_Op := P_Logical_Operator;
1746 if Prev_Logical_Op /= N_Empty and then
1747 Logical_Op /= Prev_Logical_Op
1748 then
1749 Error_Msg
1750 ("mixed logical operators in expression", Op_Location);
1751 Prev_Logical_Op := N_Empty;
1752 else
1753 Prev_Logical_Op := Logical_Op;
1754 end if;
1756 Node2 := Node1;
1757 Node1 := New_Op_Node (Logical_Op, Op_Location);
1758 Set_Left_Opnd (Node1, Node2);
1759 Set_Right_Opnd (Node1, P_Relation);
1760 exit when Token not in Token_Class_Logop;
1761 end loop;
1763 Expr_Form := EF_Non_Simple;
1764 end if;
1766 if Token = Tok_Apostrophe then
1767 Bad_Range_Attribute (Token_Ptr);
1768 return Error;
1769 else
1770 return Node1;
1771 end if;
1772 end P_Expression_Or_Range_Attribute;
1774 -- Version that allows a non-parenthesized case, conditional, or quantified
1775 -- expression if the call immediately follows a left paren, and followed
1776 -- by a right parenthesis. These forms are allowed if these conditions
1777 -- are not met, but an error message will be issued.
1779 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1780 begin
1781 -- Case of conditional, case or quantified expression
1783 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1784 return P_Unparen_Cond_Case_Quant_Expression;
1786 -- Normal case, not one of the above expression types
1788 else
1789 return P_Expression_Or_Range_Attribute;
1790 end if;
1791 end P_Expression_Or_Range_Attribute_If_OK;
1793 -------------------
1794 -- 4.4 Relation --
1795 -------------------
1797 -- This procedure scans both relations and choice relations
1799 -- CHOICE_RELATION ::=
1800 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1802 -- RELATION ::=
1803 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
1805 -- MEMBERSHIP_CHOICE_LIST ::=
1806 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
1808 -- MEMBERSHIP_CHOICE ::=
1809 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
1811 -- On return, Expr_Form indicates the categorization of the expression
1813 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1814 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1816 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1817 -- expression, then tokens are scanned until either a non-expression token,
1818 -- a right paren (not matched by a left paren) or a comma, is encountered.
1820 function P_Relation return Node_Id is
1821 Node1, Node2 : Node_Id;
1822 Optok : Source_Ptr;
1824 begin
1825 Node1 := P_Simple_Expression;
1827 if Token not in Token_Class_Relop then
1828 return Node1;
1830 else
1831 -- Here we have a relational operator following. If so then scan it
1832 -- out. Note that the assignment symbol := is treated as a relational
1833 -- operator to improve the error recovery when it is misused for =.
1834 -- P_Relational_Operator also parses the IN and NOT IN operations.
1836 Optok := Token_Ptr;
1837 Node2 := New_Op_Node (P_Relational_Operator, Optok);
1838 Set_Left_Opnd (Node2, Node1);
1840 -- Case of IN or NOT IN
1842 if Prev_Token = Tok_In then
1843 P_Membership_Test (Node2);
1845 -- Case of relational operator (= /= < <= > >=)
1847 else
1848 Set_Right_Opnd (Node2, P_Simple_Expression);
1849 end if;
1851 Expr_Form := EF_Non_Simple;
1853 if Token in Token_Class_Relop then
1854 Error_Msg_SC ("unexpected relational operator");
1855 raise Error_Resync;
1856 end if;
1858 return Node2;
1859 end if;
1861 -- If any error occurs, then scan to the next expression terminator symbol
1862 -- or comma or right paren at the outer (i.e. current) parentheses level.
1863 -- The flags are set to indicate a normal simple expression.
1865 exception
1866 when Error_Resync =>
1867 Resync_Expression;
1868 Expr_Form := EF_Simple;
1869 return Error;
1870 end P_Relation;
1872 ----------------------------
1873 -- 4.4 Simple Expression --
1874 ----------------------------
1876 -- SIMPLE_EXPRESSION ::=
1877 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1879 -- On return, Expr_Form indicates the categorization of the expression
1881 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1882 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1884 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1885 -- expression, then tokens are scanned until either a non-expression token,
1886 -- a right paren (not matched by a left paren) or a comma, is encountered.
1888 -- Note: P_Simple_Expression is called only internally by higher level
1889 -- expression routines. In cases in the grammar where a simple expression
1890 -- is required, the approach is to scan an expression, and then post an
1891 -- appropriate error message if the expression obtained is not simple. This
1892 -- gives better error recovery and treatment.
1894 function P_Simple_Expression return Node_Id is
1895 Scan_State : Saved_Scan_State;
1896 Node1 : Node_Id;
1897 Node2 : Node_Id;
1898 Tokptr : Source_Ptr;
1900 begin
1901 -- Check for cases starting with a name. There are two reasons for
1902 -- special casing. First speed things up by catching a common case
1903 -- without going through several routine layers. Second the caller must
1904 -- be informed via Expr_Form when the simple expression is a name.
1906 if Token in Token_Class_Name then
1907 Node1 := P_Name;
1909 -- Deal with apostrophe cases
1911 if Token = Tok_Apostrophe then
1912 Save_Scan_State (Scan_State); -- at apostrophe
1913 Scan; -- past apostrophe
1915 -- If qualified expression, scan it out and fall through
1917 if Token = Tok_Left_Paren then
1918 Node1 := P_Qualified_Expression (Node1);
1919 Expr_Form := EF_Simple;
1921 -- If range attribute, then we return with Token pointing to the
1922 -- apostrophe. Note: avoid the normal error check on exit. We
1923 -- know that the expression really is complete in this case!
1925 else -- Token = Tok_Range then
1926 Restore_Scan_State (Scan_State); -- to apostrophe
1927 Expr_Form := EF_Simple_Name;
1928 return Node1;
1929 end if;
1930 end if;
1932 -- If an expression terminator follows, the previous processing
1933 -- completely scanned out the expression (a common case), and
1934 -- left Expr_Form set appropriately for returning to our caller.
1936 if Token in Token_Class_Sterm then
1937 null;
1939 -- If we do not have an expression terminator, then complete the
1940 -- scan of a simple expression. This code duplicates the code
1941 -- found in P_Term and P_Factor.
1943 else
1944 if Token = Tok_Double_Asterisk then
1945 if Style_Check then
1946 Style.Check_Exponentiation_Operator;
1947 end if;
1949 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
1950 Scan; -- past **
1951 Set_Left_Opnd (Node2, Node1);
1952 Set_Right_Opnd (Node2, P_Primary);
1953 Check_Bad_Exp;
1954 Node1 := Node2;
1955 end if;
1957 loop
1958 exit when Token not in Token_Class_Mulop;
1959 Tokptr := Token_Ptr;
1960 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
1962 if Style_Check then
1963 Style.Check_Binary_Operator;
1964 end if;
1966 Scan; -- past operator
1967 Set_Left_Opnd (Node2, Node1);
1968 Set_Right_Opnd (Node2, P_Factor);
1969 Node1 := Node2;
1970 end loop;
1972 loop
1973 exit when Token not in Token_Class_Binary_Addop;
1974 Tokptr := Token_Ptr;
1975 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
1977 if Style_Check then
1978 Style.Check_Binary_Operator;
1979 end if;
1981 Scan; -- past operator
1982 Set_Left_Opnd (Node2, Node1);
1983 Set_Right_Opnd (Node2, P_Term);
1984 Node1 := Node2;
1985 end loop;
1987 Expr_Form := EF_Simple;
1988 end if;
1990 -- Cases where simple expression does not start with a name
1992 else
1993 -- Scan initial sign and initial Term
1995 if Token in Token_Class_Unary_Addop then
1996 Tokptr := Token_Ptr;
1997 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
1999 if Style_Check then
2000 Style.Check_Unary_Plus_Or_Minus;
2001 end if;
2003 Scan; -- past operator
2004 Set_Right_Opnd (Node1, P_Term);
2005 else
2006 Node1 := P_Term;
2007 end if;
2009 -- In the following, we special-case a sequence of concatenations of
2010 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2011 -- else mixed in. For such a sequence, we return a tree representing
2012 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2013 -- the number of concatenations is large. If semantic analysis
2014 -- resolves the "&" to a predefined one, then this folding gives the
2015 -- right answer. Otherwise, semantic analysis will complain about a
2016 -- capacity-exceeded error. The purpose of this trick is to avoid
2017 -- creating a deeply nested tree, which would cause deep recursion
2018 -- during semantics, causing stack overflow. This way, we can handle
2019 -- enormous concatenations in the normal case of predefined "&". We
2020 -- first build up the normal tree, and then rewrite it if
2021 -- appropriate.
2023 declare
2024 Num_Concats_Threshold : constant Positive := 1000;
2025 -- Arbitrary threshold value to enable optimization
2027 First_Node : constant Node_Id := Node1;
2028 Is_Strlit_Concat : Boolean;
2029 -- True iff we've parsed a sequence of concatenations of string
2030 -- literals, with nothing else mixed in.
2032 Num_Concats : Natural;
2033 -- Number of "&" operators if Is_Strlit_Concat is True
2035 begin
2036 Is_Strlit_Concat :=
2037 Nkind (Node1) = N_String_Literal
2038 and then Token = Tok_Ampersand;
2039 Num_Concats := 0;
2041 -- Scan out sequence of terms separated by binary adding operators
2043 loop
2044 exit when Token not in Token_Class_Binary_Addop;
2045 Tokptr := Token_Ptr;
2046 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2047 Scan; -- past operator
2048 Set_Left_Opnd (Node2, Node1);
2049 Node1 := P_Term;
2050 Set_Right_Opnd (Node2, Node1);
2052 -- Check if we're still concatenating string literals
2054 Is_Strlit_Concat :=
2055 Is_Strlit_Concat
2056 and then Nkind (Node2) = N_Op_Concat
2057 and then Nkind (Node1) = N_String_Literal;
2059 if Is_Strlit_Concat then
2060 Num_Concats := Num_Concats + 1;
2061 end if;
2063 Node1 := Node2;
2064 end loop;
2066 -- If we have an enormous series of concatenations of string
2067 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2068 -- flag tells semantic analysis that if the "&" is not predefined,
2069 -- the folded value is wrong.
2071 if Is_Strlit_Concat
2072 and then Num_Concats >= Num_Concats_Threshold
2073 then
2074 declare
2075 Empty_String_Val : String_Id;
2076 -- String_Id for ""
2078 Strlit_Concat_Val : String_Id;
2079 -- Contains the folded value (which will be correct if the
2080 -- "&" operators are the predefined ones).
2082 Cur_Node : Node_Id;
2083 -- For walking up the tree
2085 New_Node : Node_Id;
2086 -- Folded node to replace Node1
2088 Loc : constant Source_Ptr := Sloc (First_Node);
2090 begin
2091 -- Walk up the tree starting at the leftmost string literal
2092 -- (First_Node), building up the Strlit_Concat_Val as we
2093 -- go. Note that we do not use recursion here -- the whole
2094 -- point is to avoid recursively walking that enormous tree.
2096 Start_String;
2097 Store_String_Chars (Strval (First_Node));
2099 Cur_Node := Parent (First_Node);
2100 while Present (Cur_Node) loop
2101 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2102 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2104 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2105 Cur_Node := Parent (Cur_Node);
2106 end loop;
2108 Strlit_Concat_Val := End_String;
2110 -- Create new folded node, and rewrite result with a concat-
2111 -- enation of an empty string literal and the folded node.
2113 Start_String;
2114 Empty_String_Val := End_String;
2115 New_Node :=
2116 Make_Op_Concat (Loc,
2117 Make_String_Literal (Loc, Empty_String_Val),
2118 Make_String_Literal (Loc, Strlit_Concat_Val,
2119 Is_Folded_In_Parser => True));
2120 Rewrite (Node1, New_Node);
2121 end;
2122 end if;
2123 end;
2125 -- All done, we clearly do not have name or numeric literal so this
2126 -- is a case of a simple expression which is some other possibility.
2128 Expr_Form := EF_Simple;
2129 end if;
2131 -- Come here at end of simple expression, where we do a couple of
2132 -- special checks to improve error recovery.
2134 -- Special test to improve error recovery. If the current token
2135 -- is a period, then someone is trying to do selection on something
2136 -- that is not a name, e.g. a qualified expression.
2138 if Token = Tok_Dot then
2139 Error_Msg_SC ("prefix for selection is not a name");
2141 -- If qualified expression, comment and continue, otherwise something
2142 -- is pretty nasty so do an Error_Resync call.
2144 if Ada_Version < Ada_2012
2145 and then Nkind (Node1) = N_Qualified_Expression
2146 then
2147 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2148 else
2149 raise Error_Resync;
2150 end if;
2151 end if;
2153 -- Special test to improve error recovery: If the current token is
2154 -- not the first token on a line (as determined by checking the
2155 -- previous token position with the start of the current line),
2156 -- then we insist that we have an appropriate terminating token.
2157 -- Consider the following two examples:
2159 -- 1) if A nad B then ...
2161 -- 2) A := B
2162 -- C := D
2164 -- In the first example, we would like to issue a binary operator
2165 -- expected message and resynchronize to the then. In the second
2166 -- example, we do not want to issue a binary operator message, so
2167 -- that instead we will get the missing semicolon message. This
2168 -- distinction is of course a heuristic which does not always work,
2169 -- but in practice it is quite effective.
2171 -- Note: the one case in which we do not go through this circuit is
2172 -- when we have scanned a range attribute and want to return with
2173 -- Token pointing to the apostrophe. The apostrophe is not normally
2174 -- an expression terminator, and is not in Token_Class_Sterm, but
2175 -- in this special case we know that the expression is complete.
2177 if not Token_Is_At_Start_Of_Line
2178 and then Token not in Token_Class_Sterm
2179 then
2180 -- Normally the right error message is indeed that we expected a
2181 -- binary operator, but in the case of being between a right and left
2182 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2184 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2185 T_Comma;
2186 else
2187 Error_Msg_AP ("binary operator expected");
2188 end if;
2190 raise Error_Resync;
2192 else
2193 return Node1;
2194 end if;
2196 -- If any error occurs, then scan to next expression terminator symbol
2197 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2198 -- level. Expr_Form is set to indicate a normal simple expression.
2200 exception
2201 when Error_Resync =>
2202 Resync_Expression;
2203 Expr_Form := EF_Simple;
2204 return Error;
2205 end P_Simple_Expression;
2207 -----------------------------------------------
2208 -- 4.4 Simple Expression or Range Attribute --
2209 -----------------------------------------------
2211 -- SIMPLE_EXPRESSION ::=
2212 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2214 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2216 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2218 -- Error recovery: cannot raise Error_Resync
2220 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2221 Sexpr : Node_Id;
2222 Attr_Node : Node_Id;
2224 begin
2225 -- We don't just want to roar ahead and call P_Simple_Expression
2226 -- here, since we want to handle the case of a parenthesized range
2227 -- attribute cleanly.
2229 if Token = Tok_Left_Paren then
2230 declare
2231 Lptr : constant Source_Ptr := Token_Ptr;
2232 Scan_State : Saved_Scan_State;
2234 begin
2235 Save_Scan_State (Scan_State);
2236 Scan; -- past left paren
2237 Sexpr := P_Simple_Expression;
2239 if Token = Tok_Apostrophe then
2240 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2241 Expr_Form := EF_Range_Attr;
2243 if Token = Tok_Right_Paren then
2244 Scan; -- scan past right paren if present
2245 end if;
2247 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2249 return Attr_Node;
2250 end if;
2252 Restore_Scan_State (Scan_State);
2253 end;
2254 end if;
2256 -- Here after dealing with parenthesized range attribute
2258 Sexpr := P_Simple_Expression;
2260 if Token = Tok_Apostrophe then
2261 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2262 Expr_Form := EF_Range_Attr;
2263 return Attr_Node;
2265 else
2266 return Sexpr;
2267 end if;
2268 end P_Simple_Expression_Or_Range_Attribute;
2270 ---------------
2271 -- 4.4 Term --
2272 ---------------
2274 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2276 -- Error recovery: can raise Error_Resync
2278 function P_Term return Node_Id is
2279 Node1, Node2 : Node_Id;
2280 Tokptr : Source_Ptr;
2282 begin
2283 Node1 := P_Factor;
2285 loop
2286 exit when Token not in Token_Class_Mulop;
2287 Tokptr := Token_Ptr;
2288 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2289 Scan; -- past operator
2290 Set_Left_Opnd (Node2, Node1);
2291 Set_Right_Opnd (Node2, P_Factor);
2292 Node1 := Node2;
2293 end loop;
2295 return Node1;
2296 end P_Term;
2298 -----------------
2299 -- 4.4 Factor --
2300 -----------------
2302 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2304 -- Error recovery: can raise Error_Resync
2306 function P_Factor return Node_Id is
2307 Node1 : Node_Id;
2308 Node2 : Node_Id;
2310 begin
2311 if Token = Tok_Abs then
2312 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2314 if Style_Check then
2315 Style.Check_Abs_Not;
2316 end if;
2318 Scan; -- past ABS
2319 Set_Right_Opnd (Node1, P_Primary);
2320 return Node1;
2322 elsif Token = Tok_Not then
2323 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2325 if Style_Check then
2326 Style.Check_Abs_Not;
2327 end if;
2329 Scan; -- past NOT
2330 Set_Right_Opnd (Node1, P_Primary);
2331 return Node1;
2333 else
2334 Node1 := P_Primary;
2336 if Token = Tok_Double_Asterisk then
2337 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2338 Scan; -- past **
2339 Set_Left_Opnd (Node2, Node1);
2340 Set_Right_Opnd (Node2, P_Primary);
2341 Check_Bad_Exp;
2342 return Node2;
2343 else
2344 return Node1;
2345 end if;
2346 end if;
2347 end P_Factor;
2349 ------------------
2350 -- 4.4 Primary --
2351 ------------------
2353 -- PRIMARY ::=
2354 -- NUMERIC_LITERAL | null
2355 -- | STRING_LITERAL | AGGREGATE
2356 -- | NAME | QUALIFIED_EXPRESSION
2357 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2359 -- Error recovery: can raise Error_Resync
2361 function P_Primary return Node_Id is
2362 Scan_State : Saved_Scan_State;
2363 Node1 : Node_Id;
2365 begin
2366 -- The loop runs more than once only if misplaced pragmas are found
2368 loop
2369 case Token is
2371 -- Name token can start a name, call or qualified expression, all
2372 -- of which are acceptable possibilities for primary. Note also
2373 -- that string literal is included in name (as operator symbol)
2374 -- and type conversion is included in name (as indexed component).
2376 when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2377 Node1 := P_Name;
2379 -- All done unless apostrophe follows
2381 if Token /= Tok_Apostrophe then
2382 return Node1;
2384 -- Apostrophe following means that we have either just parsed
2385 -- the subtype mark of a qualified expression, or the prefix
2386 -- or a range attribute.
2388 else -- Token = Tok_Apostrophe
2389 Save_Scan_State (Scan_State); -- at apostrophe
2390 Scan; -- past apostrophe
2392 -- If range attribute, then this is always an error, since
2393 -- the only legitimate case (where the scanned expression is
2394 -- a qualified simple name) is handled at the level of the
2395 -- Simple_Expression processing. This case corresponds to a
2396 -- usage such as 3 + A'Range, which is always illegal.
2398 if Token = Tok_Range then
2399 Restore_Scan_State (Scan_State); -- to apostrophe
2400 Bad_Range_Attribute (Token_Ptr);
2401 return Error;
2403 -- If left paren, then we have a qualified expression.
2404 -- Note that P_Name guarantees that in this case, where
2405 -- Token = Tok_Apostrophe on return, the only two possible
2406 -- tokens following the apostrophe are left paren and
2407 -- RANGE, so we know we have a left paren here.
2409 else -- Token = Tok_Left_Paren
2410 return P_Qualified_Expression (Node1);
2412 end if;
2413 end if;
2415 -- Numeric or string literal
2417 when Tok_Integer_Literal |
2418 Tok_Real_Literal |
2419 Tok_String_Literal =>
2421 Node1 := Token_Node;
2422 Scan; -- past number
2423 return Node1;
2425 -- Left paren, starts aggregate or parenthesized expression
2427 when Tok_Left_Paren =>
2428 declare
2429 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2431 begin
2432 if Nkind (Expr) = N_Attribute_Reference
2433 and then Attribute_Name (Expr) = Name_Range
2434 then
2435 Bad_Range_Attribute (Sloc (Expr));
2436 end if;
2438 return Expr;
2439 end;
2441 -- Allocator
2443 when Tok_New =>
2444 return P_Allocator;
2446 -- Null
2448 when Tok_Null =>
2449 Scan; -- past NULL
2450 return New_Node (N_Null, Prev_Token_Ptr);
2452 -- Pragma, not allowed here, so just skip past it
2454 when Tok_Pragma =>
2455 P_Pragmas_Misplaced;
2457 -- Deal with IF (possible unparenthesized conditional expression)
2459 when Tok_If =>
2461 -- If this looks like a real if, defined as an IF appearing at
2462 -- the start of a new line, then we consider we have a missing
2463 -- operand. If in Ada 2012 and the IF is not properly indented
2464 -- for a statement, we prefer to issue a message about an ill-
2465 -- parenthesized conditional expression.
2467 if Token_Is_At_Start_Of_Line
2468 and then not
2469 (Ada_Version >= Ada_2012
2470 and then Style_Check_Indentation /= 0
2471 and then Start_Column rem Style_Check_Indentation /= 0)
2472 then
2473 Error_Msg_AP ("missing operand");
2474 return Error;
2476 -- If this looks like a conditional expression, then treat it
2477 -- that way with an error message.
2479 elsif Ada_Version >= Ada_2012 then
2480 Error_Msg_SC
2481 ("conditional expression must be parenthesized");
2482 return P_Conditional_Expression;
2484 -- Otherwise treat as misused identifier
2486 else
2487 return P_Identifier;
2488 end if;
2490 -- Deal with CASE (possible unparenthesized case expression)
2492 when Tok_Case =>
2494 -- If this looks like a real case, defined as a CASE appearing
2495 -- the start of a new line, then we consider we have a missing
2496 -- operand. If in Ada 2012 and the CASE is not properly
2497 -- indented for a statement, we prefer to issue a message about
2498 -- an ill-parenthesized case expression.
2500 if Token_Is_At_Start_Of_Line
2501 and then not
2502 (Ada_Version >= Ada_2012
2503 and then Style_Check_Indentation /= 0
2504 and then Start_Column rem Style_Check_Indentation /= 0)
2505 then
2506 Error_Msg_AP ("missing operand");
2507 return Error;
2509 -- If this looks like a case expression, then treat it that way
2510 -- with an error message.
2512 elsif Ada_Version >= Ada_2012 then
2513 Error_Msg_SC ("case expression must be parenthesized");
2514 return P_Case_Expression;
2516 -- Otherwise treat as misused identifier
2518 else
2519 return P_Identifier;
2520 end if;
2522 -- For [all | some] indicates a quantified expression
2524 when Tok_For =>
2526 if Token_Is_At_Start_Of_Line then
2527 Error_Msg_AP ("misplaced loop");
2528 return Error;
2530 elsif Ada_Version >= Ada_2012 then
2531 Error_Msg_SC ("quantified expression must be parenthesized");
2532 return P_Quantified_Expression;
2534 else
2536 -- Otherwise treat as misused identifier
2538 return P_Identifier;
2539 end if;
2541 -- Anything else is illegal as the first token of a primary, but
2542 -- we test for a reserved identifier so that it is treated nicely
2544 when others =>
2545 if Is_Reserved_Identifier then
2546 return P_Identifier;
2548 elsif Prev_Token = Tok_Comma then
2549 Error_Msg_SP -- CODEFIX
2550 ("|extra "","" ignored");
2551 raise Error_Resync;
2553 else
2554 Error_Msg_AP ("missing operand");
2555 raise Error_Resync;
2556 end if;
2558 end case;
2559 end loop;
2560 end P_Primary;
2562 -------------------------------
2563 -- 4.4 Quantified_Expression --
2564 -------------------------------
2566 -- QUANTIFIED_EXPRESSION ::=
2567 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
2568 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
2570 function P_Quantified_Expression return Node_Id is
2571 I_Spec : Node_Id;
2572 Node1 : Node_Id;
2574 begin
2575 if Ada_Version < Ada_2012 then
2576 Error_Msg_SC ("quantified expression is an Ada 2012 feature");
2577 Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
2578 end if;
2580 Scan; -- past FOR
2582 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
2584 if Token = Tok_All then
2585 Set_All_Present (Node1);
2587 elsif Token /= Tok_Some then
2588 Error_Msg_AP ("missing quantifier");
2589 raise Error_Resync;
2590 end if;
2592 Scan; -- past SOME
2593 I_Spec := P_Loop_Parameter_Specification;
2595 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
2596 Set_Loop_Parameter_Specification (Node1, I_Spec);
2597 else
2598 Set_Iterator_Specification (Node1, I_Spec);
2599 end if;
2601 if Token = Tok_Arrow then
2602 Scan;
2603 Set_Condition (Node1, P_Expression);
2604 return Node1;
2605 else
2606 Error_Msg_AP ("missing arrow");
2607 raise Error_Resync;
2608 end if;
2609 end P_Quantified_Expression;
2611 ---------------------------
2612 -- 4.5 Logical Operator --
2613 ---------------------------
2615 -- LOGICAL_OPERATOR ::= and | or | xor
2617 -- Note: AND THEN and OR ELSE are also treated as logical operators
2618 -- by the parser (even though they are not operators semantically)
2620 -- The value returned is the appropriate Node_Kind code for the operator
2621 -- On return, Token points to the token following the scanned operator.
2623 -- The caller has checked that the first token is a legitimate logical
2624 -- operator token (i.e. is either XOR, AND, OR).
2626 -- Error recovery: cannot raise Error_Resync
2628 function P_Logical_Operator return Node_Kind is
2629 begin
2630 if Token = Tok_And then
2631 if Style_Check then
2632 Style.Check_Binary_Operator;
2633 end if;
2635 Scan; -- past AND
2637 if Token = Tok_Then then
2638 Scan; -- past THEN
2639 return N_And_Then;
2640 else
2641 return N_Op_And;
2642 end if;
2644 elsif Token = Tok_Or then
2645 if Style_Check then
2646 Style.Check_Binary_Operator;
2647 end if;
2649 Scan; -- past OR
2651 if Token = Tok_Else then
2652 Scan; -- past ELSE
2653 return N_Or_Else;
2654 else
2655 return N_Op_Or;
2656 end if;
2658 else -- Token = Tok_Xor
2659 if Style_Check then
2660 Style.Check_Binary_Operator;
2661 end if;
2663 Scan; -- past XOR
2664 return N_Op_Xor;
2665 end if;
2666 end P_Logical_Operator;
2668 ------------------------------
2669 -- 4.5 Relational Operator --
2670 ------------------------------
2672 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2674 -- The value returned is the appropriate Node_Kind code for the operator.
2675 -- On return, Token points to the operator token, NOT past it.
2677 -- The caller has checked that the first token is a legitimate relational
2678 -- operator token (i.e. is one of the operator tokens listed above).
2680 -- Error recovery: cannot raise Error_Resync
2682 function P_Relational_Operator return Node_Kind is
2683 Op_Kind : Node_Kind;
2684 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2685 (Tok_Less => N_Op_Lt,
2686 Tok_Equal => N_Op_Eq,
2687 Tok_Greater => N_Op_Gt,
2688 Tok_Not_Equal => N_Op_Ne,
2689 Tok_Greater_Equal => N_Op_Ge,
2690 Tok_Less_Equal => N_Op_Le,
2691 Tok_In => N_In,
2692 Tok_Not => N_Not_In,
2693 Tok_Box => N_Op_Ne);
2695 begin
2696 if Token = Tok_Box then
2697 Error_Msg_SC -- CODEFIX
2698 ("|""'<'>"" should be ""/=""");
2699 end if;
2701 Op_Kind := Relop_Node (Token);
2703 if Style_Check then
2704 Style.Check_Binary_Operator;
2705 end if;
2707 Scan; -- past operator token
2709 if Prev_Token = Tok_Not then
2710 T_In;
2711 end if;
2713 return Op_Kind;
2714 end P_Relational_Operator;
2716 ---------------------------------
2717 -- 4.5 Binary Adding Operator --
2718 ---------------------------------
2720 -- BINARY_ADDING_OPERATOR ::= + | - | &
2722 -- The value returned is the appropriate Node_Kind code for the operator.
2723 -- On return, Token points to the operator token (NOT past it).
2725 -- The caller has checked that the first token is a legitimate adding
2726 -- operator token (i.e. is one of the operator tokens listed above).
2728 -- Error recovery: cannot raise Error_Resync
2730 function P_Binary_Adding_Operator return Node_Kind is
2731 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2732 (Tok_Ampersand => N_Op_Concat,
2733 Tok_Minus => N_Op_Subtract,
2734 Tok_Plus => N_Op_Add);
2735 begin
2736 return Addop_Node (Token);
2737 end P_Binary_Adding_Operator;
2739 --------------------------------
2740 -- 4.5 Unary Adding Operator --
2741 --------------------------------
2743 -- UNARY_ADDING_OPERATOR ::= + | -
2745 -- The value returned is the appropriate Node_Kind code for the operator.
2746 -- On return, Token points to the operator token (NOT past it).
2748 -- The caller has checked that the first token is a legitimate adding
2749 -- operator token (i.e. is one of the operator tokens listed above).
2751 -- Error recovery: cannot raise Error_Resync
2753 function P_Unary_Adding_Operator return Node_Kind is
2754 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2755 (Tok_Minus => N_Op_Minus,
2756 Tok_Plus => N_Op_Plus);
2757 begin
2758 return Addop_Node (Token);
2759 end P_Unary_Adding_Operator;
2761 -------------------------------
2762 -- 4.5 Multiplying Operator --
2763 -------------------------------
2765 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
2767 -- The value returned is the appropriate Node_Kind code for the operator.
2768 -- On return, Token points to the operator token (NOT past it).
2770 -- The caller has checked that the first token is a legitimate multiplying
2771 -- operator token (i.e. is one of the operator tokens listed above).
2773 -- Error recovery: cannot raise Error_Resync
2775 function P_Multiplying_Operator return Node_Kind is
2776 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2777 (Tok_Asterisk => N_Op_Multiply,
2778 Tok_Mod => N_Op_Mod,
2779 Tok_Rem => N_Op_Rem,
2780 Tok_Slash => N_Op_Divide);
2781 begin
2782 return Mulop_Node (Token);
2783 end P_Multiplying_Operator;
2785 --------------------------------------
2786 -- 4.5 Highest Precedence Operator --
2787 --------------------------------------
2789 -- Parsed by P_Factor (4.4)
2791 -- Note: this rule is not in fact used by the grammar at any point!
2793 --------------------------
2794 -- 4.6 Type Conversion --
2795 --------------------------
2797 -- Parsed by P_Primary as a Name (4.1)
2799 -------------------------------
2800 -- 4.7 Qualified Expression --
2801 -------------------------------
2803 -- QUALIFIED_EXPRESSION ::=
2804 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2806 -- The caller has scanned the name which is the Subtype_Mark parameter
2807 -- and scanned past the single quote following the subtype mark. The
2808 -- caller has not checked that this name is in fact appropriate for
2809 -- a subtype mark name (i.e. it is a selected component or identifier).
2811 -- Error_Recovery: cannot raise Error_Resync
2813 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2814 Qual_Node : Node_Id;
2815 begin
2816 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2817 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2818 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2819 return Qual_Node;
2820 end P_Qualified_Expression;
2822 --------------------
2823 -- 4.8 Allocator --
2824 --------------------
2826 -- ALLOCATOR ::=
2827 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
2828 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
2830 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
2832 -- The caller has checked that the initial token is NEW
2834 -- Error recovery: can raise Error_Resync
2836 function P_Allocator return Node_Id is
2837 Alloc_Node : Node_Id;
2838 Type_Node : Node_Id;
2839 Null_Exclusion_Present : Boolean;
2841 begin
2842 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2843 T_New;
2845 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
2847 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
2849 if Token = Tok_Left_Paren then
2850 Scan; -- past (
2851 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
2852 T_Right_Paren;
2854 if Ada_Version < Ada_2012 then
2855 Error_Msg_N
2856 ("|subpool specification is an Ada 2012 feature",
2857 Subpool_Handle_Name (Alloc_Node));
2858 Error_Msg_N
2859 ("\|unit must be compiled with -gnat2012 switch",
2860 Subpool_Handle_Name (Alloc_Node));
2861 end if;
2862 end if;
2864 Null_Exclusion_Present := P_Null_Exclusion;
2865 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
2866 Type_Node := P_Subtype_Mark_Resync;
2868 if Token = Tok_Apostrophe then
2869 Scan; -- past apostrophe
2870 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2871 else
2872 Set_Expression
2873 (Alloc_Node,
2874 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
2875 end if;
2877 return Alloc_Node;
2878 end P_Allocator;
2880 -----------------------
2881 -- P_Case_Expression --
2882 -----------------------
2884 function P_Case_Expression return Node_Id is
2885 Loc : constant Source_Ptr := Token_Ptr;
2886 Case_Node : Node_Id;
2887 Save_State : Saved_Scan_State;
2889 begin
2890 if Ada_Version < Ada_2012 then
2891 Error_Msg_SC ("|case expression is an Ada 2012 feature");
2892 Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
2893 end if;
2895 Scan; -- past CASE
2896 Case_Node :=
2897 Make_Case_Expression (Loc,
2898 Expression => P_Expression_No_Right_Paren,
2899 Alternatives => New_List);
2900 T_Is;
2902 -- We now have scanned out CASE expression IS, scan alternatives
2904 loop
2905 T_When;
2906 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
2908 -- Missing comma if WHEN (more alternatives present)
2910 if Token = Tok_When then
2911 T_Comma;
2913 -- If comma/WHEN, skip comma and we have another alternative
2915 elsif Token = Tok_Comma then
2916 Save_Scan_State (Save_State);
2917 Scan; -- past comma
2919 if Token /= Tok_When then
2920 Restore_Scan_State (Save_State);
2921 exit;
2922 end if;
2924 -- If no comma or WHEN, definitely done
2926 else
2927 exit;
2928 end if;
2929 end loop;
2931 -- If we have an END CASE, diagnose as not needed
2933 if Token = Tok_End then
2934 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
2935 Scan; -- past END
2937 if Token = Tok_Case then
2938 Scan; -- past CASE;
2939 end if;
2940 end if;
2942 -- Return the Case_Expression node
2944 return Case_Node;
2945 end P_Case_Expression;
2947 -----------------------------------
2948 -- P_Case_Expression_Alternative --
2949 -----------------------------------
2951 -- CASE_STATEMENT_ALTERNATIVE ::=
2952 -- when DISCRETE_CHOICE_LIST =>
2953 -- EXPRESSION
2955 -- The caller has checked that and scanned past the initial WHEN token
2956 -- Error recovery: can raise Error_Resync
2958 function P_Case_Expression_Alternative return Node_Id is
2959 Case_Alt_Node : Node_Id;
2960 begin
2961 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
2962 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
2963 TF_Arrow;
2964 Set_Expression (Case_Alt_Node, P_Expression);
2965 return Case_Alt_Node;
2966 end P_Case_Expression_Alternative;
2968 ------------------------------
2969 -- P_Conditional_Expression --
2970 ------------------------------
2972 function P_Conditional_Expression return Node_Id is
2973 Exprs : constant List_Id := New_List;
2974 Loc : constant Source_Ptr := Token_Ptr;
2975 Expr : Node_Id;
2976 State : Saved_Scan_State;
2978 begin
2979 Inside_Conditional_Expression := Inside_Conditional_Expression + 1;
2981 if Token = Tok_If and then Ada_Version < Ada_2012 then
2982 Error_Msg_SC ("|conditional expression is an Ada 2012 feature");
2983 Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
2984 end if;
2986 Scan; -- past IF or ELSIF
2987 Append_To (Exprs, P_Condition);
2988 TF_Then;
2989 Append_To (Exprs, P_Expression);
2991 -- We now have scanned out IF expr THEN expr
2993 -- Check for common error of semicolon before the ELSE
2995 if Token = Tok_Semicolon then
2996 Save_Scan_State (State);
2997 Scan; -- past semicolon
2999 if Token = Tok_Else or else Token = Tok_Elsif then
3000 Error_Msg_SP -- CODEFIX
3001 ("|extra "";"" ignored");
3003 else
3004 Restore_Scan_State (State);
3005 end if;
3006 end if;
3008 -- Scan out ELSIF sequence if present
3010 if Token = Tok_Elsif then
3011 Expr := P_Conditional_Expression;
3012 Set_Is_Elsif (Expr);
3013 Append_To (Exprs, Expr);
3015 -- Scan out ELSE phrase if present
3017 elsif Token = Tok_Else then
3019 -- Scan out ELSE expression
3021 Scan; -- Past ELSE
3022 Append_To (Exprs, P_Expression);
3024 -- Two expression case (implied True, filled in during semantics)
3026 else
3027 null;
3028 end if;
3030 -- If we have an END IF, diagnose as not needed
3032 if Token = Tok_End then
3033 Error_Msg_SC
3034 ("`END IF` not allowed at end of conditional expression");
3035 Scan; -- past END
3037 if Token = Tok_If then
3038 Scan; -- past IF;
3039 end if;
3040 end if;
3042 Inside_Conditional_Expression := Inside_Conditional_Expression - 1;
3044 -- Return the Conditional_Expression node
3046 return
3047 Make_Conditional_Expression (Loc,
3048 Expressions => Exprs);
3049 end P_Conditional_Expression;
3051 -----------------------
3052 -- P_Membership_Test --
3053 -----------------------
3055 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3056 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3058 procedure P_Membership_Test (N : Node_Id) is
3059 Alt : constant Node_Id :=
3060 P_Range_Or_Subtype_Mark
3061 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3063 begin
3064 -- Set case
3066 if Token = Tok_Vertical_Bar then
3067 if Ada_Version < Ada_2012 then
3068 Error_Msg_SC ("set notation is an Ada 2012 feature");
3069 Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
3070 end if;
3072 Set_Alternatives (N, New_List (Alt));
3073 Set_Right_Opnd (N, Empty);
3075 -- Loop to accumulate alternatives
3077 while Token = Tok_Vertical_Bar loop
3078 Scan; -- past vertical bar
3079 Append_To
3080 (Alternatives (N),
3081 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3082 end loop;
3084 -- Not set case
3086 else
3087 Set_Right_Opnd (N, Alt);
3088 Set_Alternatives (N, No_List);
3089 end if;
3090 end P_Membership_Test;
3092 ------------------------------------------
3093 -- P_Unparen_Cond_Case_Quant_Expression --
3094 ------------------------------------------
3096 function P_Unparen_Cond_Case_Quant_Expression return Node_Id is
3097 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3098 Result : Node_Id;
3100 begin
3101 -- Case expression
3103 if Token = Tok_Case then
3104 Result := P_Case_Expression;
3106 if not (Lparen and then Token = Tok_Right_Paren) then
3107 Error_Msg_N
3108 ("case expression must be parenthesized!", Result);
3109 end if;
3111 -- Conditional expression
3113 elsif Token = Tok_If then
3114 Result := P_Conditional_Expression;
3116 if not (Lparen and then Token = Tok_Right_Paren) then
3117 Error_Msg_N
3118 ("conditional expression must be parenthesized!", Result);
3119 end if;
3121 -- Quantified expression
3123 elsif Token = Tok_For then
3124 Result := P_Quantified_Expression;
3126 if not (Lparen and then Token = Tok_Right_Paren) then
3127 Error_Msg_N
3128 ("quantified expression must be parenthesized!", Result);
3129 end if;
3131 -- No other possibility should exist (caller was supposed to check)
3133 else
3134 raise Program_Error;
3135 end if;
3137 -- Return expression (possibly after having given message)
3139 return Result;
3140 end P_Unparen_Cond_Case_Quant_Expression;
3142 end Ch4;