Improve max_insns_skipped logic
[official-gcc.git] / gcc / ada / par-ch4.adb
blob2844b4ea03d0f7548bfcb4144e6e4f758e357711
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-2017, 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)). The Ada 2012 attribute 'Old is in this category.
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;
85 function P_Binary_Adding_Operator return Node_Kind;
86 function P_Logical_Operator return Node_Kind;
87 function P_Multiplying_Operator return Node_Kind;
88 function P_Relational_Operator return Node_Kind;
89 function P_Unary_Adding_Operator return Node_Kind;
91 procedure Bad_Range_Attribute (Loc : Source_Ptr);
92 -- Called to place complaint about bad range attribute at the given
93 -- source location. Terminates by raising Error_Resync.
95 procedure Check_Bad_Exp;
96 -- Called after scanning a**b, posts error if ** detected
98 procedure P_Membership_Test (N : Node_Id);
99 -- N is the node for a N_In or N_Not_In node whose right operand has not
100 -- yet been processed. It is called just after scanning out the IN keyword.
101 -- On return, either Right_Opnd or Alternatives is set, as appropriate.
103 function P_Range_Attribute_Reference (Prefix_Node : Node_Id) return Node_Id;
104 -- Scan a range attribute reference. The caller has scanned out the
105 -- prefix. The current token is known to be an apostrophe and the
106 -- following token is known to be RANGE.
108 function P_Unparen_Cond_Case_Quant_Expression return Node_Id;
109 -- This function is called with Token pointing to IF, CASE, or FOR, in a
110 -- context that allows a case, conditional, or quantified expression if
111 -- it is surrounded by parentheses. If not surrounded by parentheses, the
112 -- expression is still returned, but an error message is issued.
114 -------------------------
115 -- Bad_Range_Attribute --
116 -------------------------
118 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
119 begin
120 Error_Msg ("range attribute cannot be used in expression!", Loc);
121 Resync_Expression;
122 end Bad_Range_Attribute;
124 -------------------
125 -- Check_Bad_Exp --
126 -------------------
128 procedure Check_Bad_Exp is
129 begin
130 if Token = Tok_Double_Asterisk then
131 Error_Msg_SC ("parenthesization required for '*'*");
132 Scan; -- past **
133 Discard_Junk_Node (P_Primary);
134 Check_Bad_Exp;
135 end if;
136 end Check_Bad_Exp;
138 --------------------------
139 -- 4.1 Name (also 6.4) --
140 --------------------------
142 -- NAME ::=
143 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
144 -- | INDEXED_COMPONENT | SLICE
145 -- | SELECTED_COMPONENT | ATTRIBUTE
146 -- | TYPE_CONVERSION | FUNCTION_CALL
147 -- | CHARACTER_LITERAL | TARGET_NAME
149 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
151 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
153 -- EXPLICIT_DEREFERENCE ::= NAME . all
155 -- IMPLICIT_DEREFERENCE ::= NAME
157 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
159 -- SLICE ::= PREFIX (DISCRETE_RANGE)
161 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
163 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
165 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
167 -- ATTRIBUTE_DESIGNATOR ::=
168 -- IDENTIFIER [(static_EXPRESSION)]
169 -- | access | delta | digits
171 -- FUNCTION_CALL ::=
172 -- function_NAME
173 -- | function_PREFIX ACTUAL_PARAMETER_PART
175 -- ACTUAL_PARAMETER_PART ::=
176 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
178 -- PARAMETER_ASSOCIATION ::=
179 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
181 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
183 -- TARGET_NAME ::= @ (AI12-0125-3: abbreviation for LHS)
185 -- Note: syntactically a procedure call looks just like a function call,
186 -- so this routine is in practice used to scan out procedure calls as well.
188 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
190 -- Error recovery: can raise Error_Resync
192 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
193 -- followed by either a left paren (qualified expression case), or by
194 -- range (range attribute case). All other uses of apostrophe (i.e. all
195 -- other attributes) are handled in this routine.
197 -- Error recovery: can raise Error_Resync
199 function P_Name return Node_Id is
200 Scan_State : Saved_Scan_State;
201 Name_Node : Node_Id;
202 Prefix_Node : Node_Id;
203 Ident_Node : Node_Id;
204 Expr_Node : Node_Id;
205 Range_Node : Node_Id;
206 Arg_Node : Node_Id;
208 Arg_List : List_Id := No_List; -- kill junk warning
209 Attr_Name : Name_Id := No_Name; -- kill junk warning
211 begin
212 -- Case of not a name
214 if Token not in Token_Class_Name then
216 -- If it looks like start of expression, complain and scan expression
218 if Token in Token_Class_Literal
219 or else Token = Tok_Left_Paren
220 then
221 Error_Msg_SC ("name expected");
222 return P_Expression;
224 -- Otherwise some other junk, not much we can do
226 else
227 Error_Msg_AP ("name expected");
228 raise Error_Resync;
229 end if;
230 end if;
232 -- Loop through designators in qualified name
233 -- AI12-0125 : target_name
235 if Token = Tok_At_Sign then
236 Scan_Reserved_Identifier (Force_Msg => False);
238 if Present (Current_Assign_Node) then
239 Set_Has_Target_Names (Current_Assign_Node);
240 end if;
241 end if;
243 Name_Node := Token_Node;
245 loop
246 Scan; -- past designator
247 exit when Token /= Tok_Dot;
248 Save_Scan_State (Scan_State); -- at dot
249 Scan; -- past dot
251 -- If we do not have another designator after the dot, then join
252 -- the normal circuit to handle a dot extension (may be .all or
253 -- character literal case). Otherwise loop back to scan the next
254 -- designator.
256 if Token not in Token_Class_Desig then
257 goto Scan_Name_Extension_Dot;
258 else
259 Prefix_Node := Name_Node;
260 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
261 Set_Prefix (Name_Node, Prefix_Node);
262 Set_Selector_Name (Name_Node, Token_Node);
263 end if;
264 end loop;
266 -- We have now scanned out a qualified designator. If the last token is
267 -- an operator symbol, then we certainly do not have the Snam case, so
268 -- we can just use the normal name extension check circuit
270 if Prev_Token = Tok_Operator_Symbol then
271 goto Scan_Name_Extension;
272 end if;
274 -- We have scanned out a qualified simple name, check for name extension
275 -- Note that we know there is no dot here at this stage, so the only
276 -- possible cases of name extension are apostrophe and left paren.
278 if Token = Tok_Apostrophe then
279 Save_Scan_State (Scan_State); -- at apostrophe
280 Scan; -- past apostrophe
282 -- Qualified expression in Ada 2012 mode (treated as a name)
284 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
285 goto Scan_Name_Extension_Apostrophe;
287 -- If left paren not in Ada 2012, then it is not part of the name,
288 -- since qualified expressions are not names in prior versions of
289 -- Ada, so return with Token backed up to point to the apostrophe.
290 -- The treatment for the range attribute is similar (we do not
291 -- consider x'range to be a name in this grammar).
293 elsif Token = Tok_Left_Paren or else Token = Tok_Range then
294 Restore_Scan_State (Scan_State); -- to apostrophe
295 Expr_Form := EF_Simple_Name;
296 return Name_Node;
298 -- Otherwise we have the case of a name extended by an attribute
300 else
301 goto Scan_Name_Extension_Apostrophe;
302 end if;
304 -- Check case of qualified simple name extended by a left parenthesis
306 elsif Token = Tok_Left_Paren then
307 Scan; -- past left paren
308 goto Scan_Name_Extension_Left_Paren;
310 -- Otherwise the qualified simple name is not extended, so return
312 else
313 Expr_Form := EF_Simple_Name;
314 return Name_Node;
315 end if;
317 -- Loop scanning past name extensions. A label is used for control
318 -- transfer for this loop for ease of interfacing with the finite state
319 -- machine in the parenthesis scanning circuit, and also to allow for
320 -- passing in control to the appropriate point from the above code.
322 <<Scan_Name_Extension>>
324 -- Character literal used as name cannot be extended. Also this
325 -- cannot be a call, since the name for a call must be a designator.
326 -- Return in these cases, or if there is no name extension
328 if Token not in Token_Class_Namext
329 or else Prev_Token = Tok_Char_Literal
330 then
331 Expr_Form := EF_Name;
332 return Name_Node;
333 end if;
335 -- Merge here when we know there is a name extension
337 <<Scan_Name_Extension_OK>>
339 if Token = Tok_Left_Paren then
340 Scan; -- past left paren
341 goto Scan_Name_Extension_Left_Paren;
343 elsif Token = Tok_Apostrophe then
344 Save_Scan_State (Scan_State); -- at apostrophe
345 Scan; -- past apostrophe
346 goto Scan_Name_Extension_Apostrophe;
348 else -- Token = Tok_Dot
349 Save_Scan_State (Scan_State); -- at dot
350 Scan; -- past dot
351 goto Scan_Name_Extension_Dot;
352 end if;
354 -- Case of name extended by dot (selection), dot is already skipped
355 -- and the scan state at the point of the dot is saved in Scan_State.
357 <<Scan_Name_Extension_Dot>>
359 -- Explicit dereference case
361 if Token = Tok_All then
362 Prefix_Node := Name_Node;
363 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
364 Set_Prefix (Name_Node, Prefix_Node);
365 Scan; -- past ALL
366 goto Scan_Name_Extension;
368 -- Selected component case
370 elsif Token in Token_Class_Name then
371 Prefix_Node := Name_Node;
372 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
373 Set_Prefix (Name_Node, Prefix_Node);
374 Set_Selector_Name (Name_Node, Token_Node);
375 Scan; -- past selector
376 goto Scan_Name_Extension;
378 -- Reserved identifier as selector
380 elsif Is_Reserved_Identifier then
381 Scan_Reserved_Identifier (Force_Msg => False);
382 Prefix_Node := Name_Node;
383 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
384 Set_Prefix (Name_Node, Prefix_Node);
385 Set_Selector_Name (Name_Node, Token_Node);
386 Scan; -- past identifier used as selector
387 goto Scan_Name_Extension;
389 -- If dot is at end of line and followed by nothing legal,
390 -- then assume end of name and quit (dot will be taken as
391 -- an incorrect form of some other punctuation by our caller).
393 elsif Token_Is_At_Start_Of_Line then
394 Restore_Scan_State (Scan_State);
395 return Name_Node;
397 -- Here if nothing legal after the dot
399 else
400 Error_Msg_AP ("selector expected");
401 raise Error_Resync;
402 end if;
404 -- Here for an apostrophe as name extension. The scan position at the
405 -- apostrophe has already been saved, and the apostrophe scanned out.
407 <<Scan_Name_Extension_Apostrophe>>
409 Scan_Apostrophe : declare
410 function Apostrophe_Should_Be_Semicolon return Boolean;
411 -- Checks for case where apostrophe should probably be
412 -- a semicolon, and if so, gives appropriate message,
413 -- resets the scan pointer to the apostrophe, changes
414 -- the current token to Tok_Semicolon, and returns True.
415 -- Otherwise returns False.
417 ------------------------------------
418 -- Apostrophe_Should_Be_Semicolon --
419 ------------------------------------
421 function Apostrophe_Should_Be_Semicolon return Boolean is
422 begin
423 if Token_Is_At_Start_Of_Line then
424 Restore_Scan_State (Scan_State); -- to apostrophe
425 Error_Msg_SC ("|""''"" should be "";""");
426 Token := Tok_Semicolon;
427 return True;
428 else
429 return False;
430 end if;
431 end Apostrophe_Should_Be_Semicolon;
433 -- Start of processing for Scan_Apostrophe
435 begin
436 -- Check for qualified expression case in Ada 2012 mode
438 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
439 Name_Node := P_Qualified_Expression (Name_Node);
440 goto Scan_Name_Extension;
442 -- If range attribute after apostrophe, then return with Token
443 -- pointing to the apostrophe. Note that in this case the prefix
444 -- need not be a simple name (cases like A.all'range). Similarly
445 -- if there is a left paren after the apostrophe, then we also
446 -- return with Token pointing to the apostrophe (this is the
447 -- aggregate case, or some error case).
449 elsif Token = Tok_Range or else Token = Tok_Left_Paren then
450 Restore_Scan_State (Scan_State); -- to apostrophe
451 Expr_Form := EF_Name;
452 return Name_Node;
454 -- Here for cases where attribute designator is an identifier
456 elsif Token = Tok_Identifier then
457 Attr_Name := Token_Name;
459 if not Is_Attribute_Name (Attr_Name) then
460 if Apostrophe_Should_Be_Semicolon then
461 Expr_Form := EF_Name;
462 return Name_Node;
464 -- Here for a bad attribute name
466 else
467 Signal_Bad_Attribute;
468 Scan; -- past bad identifier
470 if Token = Tok_Left_Paren then
471 Scan; -- past left paren
473 loop
474 Discard_Junk_Node (P_Expression_If_OK);
475 exit when not Comma_Present;
476 end loop;
478 T_Right_Paren;
479 end if;
481 return Error;
482 end if;
483 end if;
485 if Style_Check then
486 Style.Check_Attribute_Name (False);
487 end if;
489 -- Here for case of attribute designator is not an identifier
491 else
492 if Token = Tok_Delta then
493 Attr_Name := Name_Delta;
495 elsif Token = Tok_Digits then
496 Attr_Name := Name_Digits;
498 elsif Token = Tok_Access then
499 Attr_Name := Name_Access;
501 elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
502 Attr_Name := Name_Mod;
504 elsif Apostrophe_Should_Be_Semicolon then
505 Expr_Form := EF_Name;
506 return Name_Node;
508 else
509 Error_Msg_AP ("attribute designator expected");
510 raise Error_Resync;
511 end if;
513 if Style_Check then
514 Style.Check_Attribute_Name (True);
515 end if;
516 end if;
518 -- We come here with an OK attribute scanned, and corresponding
519 -- Attribute identifier node stored in Ident_Node.
521 Prefix_Node := Name_Node;
522 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
523 Scan; -- past attribute designator
524 Set_Prefix (Name_Node, Prefix_Node);
525 Set_Attribute_Name (Name_Node, Attr_Name);
527 -- Scan attribute arguments/designator. We skip this if we know
528 -- that the attribute cannot have an argument (see documentation
529 -- of Is_Parameterless_Attribute for further details).
531 if Token = Tok_Left_Paren
532 and then not
533 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
534 then
535 -- Attribute Update contains an array or record association
536 -- list which provides new values for various components or
537 -- elements. The list is parsed as an aggregate, and we get
538 -- better error handling by knowing that in the parser.
540 if Attr_Name = Name_Update then
541 Set_Expressions (Name_Node, New_List);
542 Append (P_Aggregate, Expressions (Name_Node));
544 -- All other cases of parsing attribute arguments
546 else
547 Set_Expressions (Name_Node, New_List);
548 Scan; -- past left paren
550 loop
551 declare
552 Expr : constant Node_Id := P_Expression_If_OK;
553 Rnam : Node_Id;
555 begin
556 -- Case of => for named notation
558 if Token = Tok_Arrow then
560 -- Named notation allowed only for the special
561 -- case of System'Restriction_Set (No_Dependence =>
562 -- unit_NAME), in which case construct a parameter
563 -- assocation node and append to the arguments.
565 if Attr_Name = Name_Restriction_Set
566 and then Nkind (Expr) = N_Identifier
567 and then Chars (Expr) = Name_No_Dependence
568 then
569 Scan; -- past arrow
570 Rnam := P_Name;
571 Append_To (Expressions (Name_Node),
572 Make_Parameter_Association (Sloc (Rnam),
573 Selector_Name => Expr,
574 Explicit_Actual_Parameter => Rnam));
575 exit;
577 -- For all other cases named notation is illegal
579 else
580 Error_Msg_SC
581 ("named parameters not permitted "
582 & "for attributes");
583 Scan; -- past junk arrow
584 end if;
586 -- Here for normal case (not => for named parameter)
588 else
589 -- Special handling for 'Image in Ada 2012, where
590 -- the attribute can be parameterless and its value
591 -- can be the prefix of a slice. Rewrite name as a
592 -- slice, Expr is its low bound.
594 if Token = Tok_Dot_Dot
595 and then Attr_Name = Name_Image
596 and then Ada_Version >= Ada_2012
597 then
598 Set_Expressions (Name_Node, No_List);
599 Prefix_Node := Name_Node;
600 Name_Node :=
601 New_Node (N_Slice, Sloc (Prefix_Node));
602 Set_Prefix (Name_Node, Prefix_Node);
603 Range_Node := New_Node (N_Range, Token_Ptr);
604 Set_Low_Bound (Range_Node, Expr);
605 Scan; -- past ..
606 Expr_Node := P_Expression;
607 Check_Simple_Expression (Expr_Node);
608 Set_High_Bound (Range_Node, Expr_Node);
609 Set_Discrete_Range (Name_Node, Range_Node);
610 T_Right_Paren;
612 goto Scan_Name_Extension;
614 else
615 Append (Expr, Expressions (Name_Node));
616 exit when not Comma_Present;
617 end if;
618 end if;
619 end;
620 end loop;
622 T_Right_Paren;
623 end if;
624 end if;
626 goto Scan_Name_Extension;
627 end Scan_Apostrophe;
629 -- Here for left parenthesis extending name (left paren skipped)
631 <<Scan_Name_Extension_Left_Paren>>
633 -- We now have to scan through a list of items, terminated by a
634 -- right parenthesis. The scan is handled by a finite state
635 -- machine. The possibilities are:
637 -- (discrete_range)
639 -- This is a slice. This case is handled in LP_State_Init
641 -- (expression, expression, ..)
643 -- This is interpreted as an indexed component, i.e. as a
644 -- case of a name which can be extended in the normal manner.
645 -- This case is handled by LP_State_Name or LP_State_Expr.
647 -- Note: if and case expressions (without an extra level of
648 -- parentheses) are permitted in this context).
650 -- (..., identifier => expression , ...)
652 -- If there is at least one occurrence of identifier => (but
653 -- none of the other cases apply), then we have a call.
655 -- Test for Id => case
657 if Token = Tok_Identifier then
658 Save_Scan_State (Scan_State); -- at Id
659 Scan; -- past Id
661 -- Test for => (allow := as an error substitute)
663 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
664 Restore_Scan_State (Scan_State); -- to Id
665 Arg_List := New_List;
666 goto LP_State_Call;
668 else
669 Restore_Scan_State (Scan_State); -- to Id
670 end if;
671 end if;
673 -- Here we have an expression after all
675 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
677 -- Check cases of discrete range for a slice
679 -- First possibility: Range_Attribute_Reference
681 if Expr_Form = EF_Range_Attr then
682 Range_Node := Expr_Node;
684 -- Second possibility: Simple_expression .. Simple_expression
686 elsif Token = Tok_Dot_Dot then
687 Check_Simple_Expression (Expr_Node);
688 Range_Node := New_Node (N_Range, Token_Ptr);
689 Set_Low_Bound (Range_Node, Expr_Node);
690 Scan; -- past ..
691 Expr_Node := P_Expression;
692 Check_Simple_Expression (Expr_Node);
693 Set_High_Bound (Range_Node, Expr_Node);
695 -- Third possibility: Type_name range Range
697 elsif Token = Tok_Range then
698 if Expr_Form /= EF_Simple_Name then
699 Error_Msg_SC ("subtype mark must precede RANGE");
700 raise Error_Resync;
701 end if;
703 Range_Node := P_Subtype_Indication (Expr_Node);
705 -- Otherwise we just have an expression. It is true that we might
706 -- have a subtype mark without a range constraint but this case
707 -- is syntactically indistinguishable from the expression case.
709 else
710 Arg_List := New_List;
711 goto LP_State_Expr;
712 end if;
714 -- Fall through here with unmistakable Discrete range scanned,
715 -- which means that we definitely have the case of a slice. The
716 -- Discrete range is in Range_Node.
718 if Token = Tok_Comma then
719 Error_Msg_SC ("slice cannot have more than one dimension");
720 raise Error_Resync;
722 elsif Token /= Tok_Right_Paren then
723 if Token = Tok_Arrow then
725 -- This may be an aggregate that is missing a qualification
727 Error_Msg_SC
728 ("context of aggregate must be a qualified expression");
729 raise Error_Resync;
731 else
732 T_Right_Paren;
733 raise Error_Resync;
734 end if;
736 else
737 Scan; -- past right paren
738 Prefix_Node := Name_Node;
739 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
740 Set_Prefix (Name_Node, Prefix_Node);
741 Set_Discrete_Range (Name_Node, Range_Node);
743 -- An operator node is legal as a prefix to other names,
744 -- but not for a slice.
746 if Nkind (Prefix_Node) = N_Operator_Symbol then
747 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
748 end if;
750 -- If we have a name extension, go scan it
752 if Token in Token_Class_Namext then
753 goto Scan_Name_Extension_OK;
755 -- Otherwise return (a slice is a name, but is not a call)
757 else
758 Expr_Form := EF_Name;
759 return Name_Node;
760 end if;
761 end if;
763 -- In LP_State_Expr, we have scanned one or more expressions, and
764 -- so we have a call or an indexed component which is a name. On
765 -- entry we have the expression just scanned in Expr_Node and
766 -- Arg_List contains the list of expressions encountered so far
768 <<LP_State_Expr>>
769 Append (Expr_Node, Arg_List);
771 if Token = Tok_Arrow then
772 Error_Msg
773 ("expect identifier in parameter association", Sloc (Expr_Node));
774 Scan; -- past arrow
776 elsif not Comma_Present then
777 T_Right_Paren;
779 Prefix_Node := Name_Node;
780 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
781 Set_Prefix (Name_Node, Prefix_Node);
782 Set_Expressions (Name_Node, Arg_List);
784 goto Scan_Name_Extension;
785 end if;
787 -- Comma present (and scanned out), test for identifier => case
788 -- Test for identifier => case
790 if Token = Tok_Identifier then
791 Save_Scan_State (Scan_State); -- at Id
792 Scan; -- past Id
794 -- Test for => (allow := as error substitute)
796 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
797 Restore_Scan_State (Scan_State); -- to Id
798 goto LP_State_Call;
800 -- Otherwise it's just an expression after all, so backup
802 else
803 Restore_Scan_State (Scan_State); -- to Id
804 end if;
805 end if;
807 -- Here we have an expression after all, so stay in this state
809 Expr_Node := P_Expression_If_OK;
810 goto LP_State_Expr;
812 -- LP_State_Call corresponds to the situation in which at least one
813 -- instance of Id => Expression has been encountered, so we know that
814 -- we do not have a name, but rather a call. We enter it with the
815 -- scan pointer pointing to the next argument to scan, and Arg_List
816 -- containing the list of arguments scanned so far.
818 <<LP_State_Call>>
820 -- Test for case of Id => Expression (named parameter)
822 if Token = Tok_Identifier then
823 Save_Scan_State (Scan_State); -- at Id
824 Ident_Node := Token_Node;
825 Scan; -- past Id
827 -- Deal with => (allow := as incorrect substitute)
829 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
830 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
831 Set_Selector_Name (Arg_Node, Ident_Node);
832 T_Arrow;
833 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
834 Append (Arg_Node, Arg_List);
836 -- If a comma follows, go back and scan next entry
838 if Comma_Present then
839 goto LP_State_Call;
841 -- Otherwise we have the end of a call
843 else
844 Prefix_Node := Name_Node;
845 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
846 Set_Name (Name_Node, Prefix_Node);
847 Set_Parameter_Associations (Name_Node, Arg_List);
848 T_Right_Paren;
850 if Token in Token_Class_Namext then
851 goto Scan_Name_Extension_OK;
853 -- This is a case of a call which cannot be a name
855 else
856 Expr_Form := EF_Name;
857 return Name_Node;
858 end if;
859 end if;
861 -- Not named parameter: Id started an expression after all
863 else
864 Restore_Scan_State (Scan_State); -- to Id
865 end if;
866 end if;
868 -- Here if entry did not start with Id => which means that it
869 -- is a positional parameter, which is not allowed, since we
870 -- have seen at least one named parameter already.
872 Error_Msg_SC
873 ("positional parameter association " &
874 "not allowed after named one");
876 Expr_Node := P_Expression_If_OK;
878 -- Leaving the '>' in an association is not unusual, so suggest
879 -- a possible fix.
881 if Nkind (Expr_Node) = N_Op_Eq then
882 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
883 end if;
885 -- We go back to scanning out expressions, so that we do not get
886 -- multiple error messages when several positional parameters
887 -- follow a named parameter.
889 goto LP_State_Expr;
891 -- End of treatment for name extensions starting with left paren
893 -- End of loop through name extensions
895 end P_Name;
897 -- This function parses a restricted form of Names which are either
898 -- designators, or designators preceded by a sequence of prefixes
899 -- that are direct names.
901 -- Error recovery: cannot raise Error_Resync
903 function P_Function_Name return Node_Id is
904 Designator_Node : Node_Id;
905 Prefix_Node : Node_Id;
906 Selector_Node : Node_Id;
907 Dot_Sloc : Source_Ptr := No_Location;
909 begin
910 -- Prefix_Node is set to the gathered prefix so far, Empty means that
911 -- no prefix has been scanned. This allows us to build up the result
912 -- in the required right recursive manner.
914 Prefix_Node := Empty;
916 -- Loop through prefixes
918 loop
919 Designator_Node := Token_Node;
921 if Token not in Token_Class_Desig then
922 return P_Identifier; -- let P_Identifier issue the error message
924 else -- Token in Token_Class_Desig
925 Scan; -- past designator
926 exit when Token /= Tok_Dot;
927 end if;
929 -- Here at a dot, with token just before it in Designator_Node
931 if No (Prefix_Node) then
932 Prefix_Node := Designator_Node;
933 else
934 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
935 Set_Prefix (Selector_Node, Prefix_Node);
936 Set_Selector_Name (Selector_Node, Designator_Node);
937 Prefix_Node := Selector_Node;
938 end if;
940 Dot_Sloc := Token_Ptr;
941 Scan; -- past dot
942 end loop;
944 -- Fall out of the loop having just scanned a designator
946 if No (Prefix_Node) then
947 return Designator_Node;
948 else
949 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
950 Set_Prefix (Selector_Node, Prefix_Node);
951 Set_Selector_Name (Selector_Node, Designator_Node);
952 return Selector_Node;
953 end if;
955 exception
956 when Error_Resync =>
957 return Error;
958 end P_Function_Name;
960 -- This function parses a restricted form of Names which are either
961 -- identifiers, or identifiers preceded by a sequence of prefixes
962 -- that are direct names.
964 -- Error recovery: cannot raise Error_Resync
966 function P_Qualified_Simple_Name return Node_Id is
967 Designator_Node : Node_Id;
968 Prefix_Node : Node_Id;
969 Selector_Node : Node_Id;
970 Dot_Sloc : Source_Ptr := No_Location;
972 begin
973 -- Prefix node is set to the gathered prefix so far, Empty means that
974 -- no prefix has been scanned. This allows us to build up the result
975 -- in the required right recursive manner.
977 Prefix_Node := Empty;
979 -- Loop through prefixes
981 loop
982 Designator_Node := Token_Node;
984 if Token = Tok_Identifier then
985 Scan; -- past identifier
986 exit when Token /= Tok_Dot;
988 elsif Token not in Token_Class_Desig then
989 return P_Identifier; -- let P_Identifier issue the error message
991 else
992 Scan; -- past designator
994 if Token /= Tok_Dot then
995 Error_Msg_SP ("identifier expected");
996 return Error;
997 end if;
998 end if;
1000 -- Here at a dot, with token just before it in Designator_Node
1002 if No (Prefix_Node) then
1003 Prefix_Node := Designator_Node;
1004 else
1005 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1006 Set_Prefix (Selector_Node, Prefix_Node);
1007 Set_Selector_Name (Selector_Node, Designator_Node);
1008 Prefix_Node := Selector_Node;
1009 end if;
1011 Dot_Sloc := Token_Ptr;
1012 Scan; -- past dot
1013 end loop;
1015 -- Fall out of the loop having just scanned an identifier
1017 if No (Prefix_Node) then
1018 return Designator_Node;
1019 else
1020 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1021 Set_Prefix (Selector_Node, Prefix_Node);
1022 Set_Selector_Name (Selector_Node, Designator_Node);
1023 return Selector_Node;
1024 end if;
1026 exception
1027 when Error_Resync =>
1028 return Error;
1029 end P_Qualified_Simple_Name;
1031 -- This procedure differs from P_Qualified_Simple_Name only in that it
1032 -- raises Error_Resync if any error is encountered. It only returns after
1033 -- scanning a valid qualified simple name.
1035 -- Error recovery: can raise Error_Resync
1037 function P_Qualified_Simple_Name_Resync return Node_Id is
1038 Designator_Node : Node_Id;
1039 Prefix_Node : Node_Id;
1040 Selector_Node : Node_Id;
1041 Dot_Sloc : Source_Ptr := No_Location;
1043 begin
1044 Prefix_Node := Empty;
1046 -- Loop through prefixes
1048 loop
1049 Designator_Node := Token_Node;
1051 if Token = Tok_Identifier then
1052 Scan; -- past identifier
1053 exit when Token /= Tok_Dot;
1055 elsif Token not in Token_Class_Desig then
1056 Discard_Junk_Node (P_Identifier); -- to issue the error message
1057 raise Error_Resync;
1059 else
1060 Scan; -- past designator
1062 if Token /= Tok_Dot then
1063 Error_Msg_SP ("identifier expected");
1064 raise Error_Resync;
1065 end if;
1066 end if;
1068 -- Here at a dot, with token just before it in Designator_Node
1070 if No (Prefix_Node) then
1071 Prefix_Node := Designator_Node;
1072 else
1073 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1074 Set_Prefix (Selector_Node, Prefix_Node);
1075 Set_Selector_Name (Selector_Node, Designator_Node);
1076 Prefix_Node := Selector_Node;
1077 end if;
1079 Dot_Sloc := Token_Ptr;
1080 Scan; -- past period
1081 end loop;
1083 -- Fall out of the loop having just scanned an identifier
1085 if No (Prefix_Node) then
1086 return Designator_Node;
1087 else
1088 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1089 Set_Prefix (Selector_Node, Prefix_Node);
1090 Set_Selector_Name (Selector_Node, Designator_Node);
1091 return Selector_Node;
1092 end if;
1093 end P_Qualified_Simple_Name_Resync;
1095 ----------------------
1096 -- 4.1 Direct_Name --
1097 ----------------------
1099 -- Parsed by P_Name and other functions in section 4.1
1101 -----------------
1102 -- 4.1 Prefix --
1103 -----------------
1105 -- Parsed by P_Name (4.1)
1107 -------------------------------
1108 -- 4.1 Explicit Dereference --
1109 -------------------------------
1111 -- Parsed by P_Name (4.1)
1113 -------------------------------
1114 -- 4.1 Implicit_Dereference --
1115 -------------------------------
1117 -- Parsed by P_Name (4.1)
1119 ----------------------------
1120 -- 4.1 Indexed Component --
1121 ----------------------------
1123 -- Parsed by P_Name (4.1)
1125 ----------------
1126 -- 4.1 Slice --
1127 ----------------
1129 -- Parsed by P_Name (4.1)
1131 -----------------------------
1132 -- 4.1 Selected_Component --
1133 -----------------------------
1135 -- Parsed by P_Name (4.1)
1137 ------------------------
1138 -- 4.1 Selector Name --
1139 ------------------------
1141 -- Parsed by P_Name (4.1)
1143 ------------------------------
1144 -- 4.1 Attribute Reference --
1145 ------------------------------
1147 -- Parsed by P_Name (4.1)
1149 -------------------------------
1150 -- 4.1 Attribute Designator --
1151 -------------------------------
1153 -- Parsed by P_Name (4.1)
1155 --------------------------------------
1156 -- 4.1.4 Range Attribute Reference --
1157 --------------------------------------
1159 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1161 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1163 -- In the grammar, a RANGE attribute is simply a name, but its use is
1164 -- highly restricted, so in the parser, we do not regard it as a name.
1165 -- Instead, P_Name returns without scanning the 'RANGE part of the
1166 -- attribute, and the caller uses the following function to construct
1167 -- a range attribute in places where it is appropriate.
1169 -- Note that RANGE here is treated essentially as an identifier,
1170 -- rather than a reserved word.
1172 -- The caller has parsed the prefix, i.e. a name, and Token points to
1173 -- the apostrophe. The token after the apostrophe is known to be RANGE
1174 -- at this point. The prefix node becomes the prefix of the attribute.
1176 -- Error_Recovery: Cannot raise Error_Resync
1178 function P_Range_Attribute_Reference
1179 (Prefix_Node : Node_Id)
1180 return Node_Id
1182 Attr_Node : Node_Id;
1184 begin
1185 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1186 Set_Prefix (Attr_Node, Prefix_Node);
1187 Scan; -- past apostrophe
1189 if Style_Check then
1190 Style.Check_Attribute_Name (True);
1191 end if;
1193 Set_Attribute_Name (Attr_Node, Name_Range);
1194 Scan; -- past RANGE
1196 if Token = Tok_Left_Paren then
1197 Scan; -- past left paren
1198 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1199 T_Right_Paren;
1200 end if;
1202 return Attr_Node;
1203 end P_Range_Attribute_Reference;
1205 ---------------------------------------
1206 -- 4.1.4 Range Attribute Designator --
1207 ---------------------------------------
1209 -- Parsed by P_Range_Attribute_Reference (4.4)
1211 --------------------
1212 -- 4.3 Aggregate --
1213 --------------------
1215 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1217 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1218 -- an aggregate is known to be required (code statement, extension
1219 -- aggregate), in which cases this routine performs the necessary check
1220 -- that we have an aggregate rather than a parenthesized expression
1222 -- Error recovery: can raise Error_Resync
1224 function P_Aggregate return Node_Id is
1225 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1226 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1228 begin
1229 if Nkind (Aggr_Node) /= N_Aggregate
1230 and then
1231 Nkind (Aggr_Node) /= N_Extension_Aggregate
1232 then
1233 Error_Msg
1234 ("aggregate may not have single positional component", Aggr_Sloc);
1235 return Error;
1236 else
1237 return Aggr_Node;
1238 end if;
1239 end P_Aggregate;
1241 ------------------------------------------------
1242 -- 4.3 Aggregate or Parenthesized Expression --
1243 ------------------------------------------------
1245 -- This procedure parses out either an aggregate or a parenthesized
1246 -- expression (these two constructs are closely related, since a
1247 -- parenthesized expression looks like an aggregate with a single
1248 -- positional component).
1250 -- AGGREGATE ::=
1251 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1253 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1255 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1256 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1257 -- | null record
1259 -- RECORD_COMPONENT_ASSOCIATION ::=
1260 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1262 -- COMPONENT_CHOICE_LIST ::=
1263 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1264 -- | others
1266 -- EXTENSION_AGGREGATE ::=
1267 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1269 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1271 -- ARRAY_AGGREGATE ::=
1272 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1274 -- POSITIONAL_ARRAY_AGGREGATE ::=
1275 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1276 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1277 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1279 -- NAMED_ARRAY_AGGREGATE ::=
1280 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1282 -- PRIMARY ::= (EXPRESSION);
1284 -- Error recovery: can raise Error_Resync
1286 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1287 -- to Ada 2005 limited aggregates (AI-287)
1289 function P_Aggregate_Or_Paren_Expr return Node_Id is
1290 Aggregate_Node : Node_Id;
1291 Expr_List : List_Id;
1292 Assoc_List : List_Id;
1293 Expr_Node : Node_Id;
1294 Lparen_Sloc : Source_Ptr;
1295 Scan_State : Saved_Scan_State;
1297 procedure Box_Error;
1298 -- Called if <> is encountered as positional aggregate element. Issues
1299 -- error message and sets Expr_Node to Error.
1301 function Is_Quantified_Expression return Boolean;
1302 -- The presence of iterated component associations requires a one
1303 -- token lookahead to distinguish it from quantified expressions.
1305 ---------------
1306 -- Box_Error --
1307 ---------------
1309 procedure Box_Error is
1310 begin
1311 if Ada_Version < Ada_2005 then
1312 Error_Msg_SC ("box in aggregate is an Ada 2005 extension");
1313 end if;
1315 -- Ada 2005 (AI-287): The box notation is allowed only with named
1316 -- notation because positional notation might be error prone. For
1317 -- example, in "(X, <>, Y, <>)", there is no type associated with
1318 -- the boxes, so you might not be leaving out the components you
1319 -- thought you were leaving out.
1321 Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1322 Scan; -- past box
1323 Expr_Node := Error;
1324 end Box_Error;
1326 ------------------------------
1327 -- Is_Quantified_Expression --
1328 ------------------------------
1330 function Is_Quantified_Expression return Boolean is
1331 Maybe : Boolean;
1332 Scan_State : Saved_Scan_State;
1334 begin
1335 Save_Scan_State (Scan_State);
1336 Scan; -- past FOR
1337 Maybe := Token = Tok_All or else Token = Tok_Some;
1338 Restore_Scan_State (Scan_State); -- to FOR
1339 return Maybe;
1340 end Is_Quantified_Expression;
1342 -- Start of processing for P_Aggregate_Or_Paren_Expr
1344 begin
1345 Lparen_Sloc := Token_Ptr;
1346 T_Left_Paren;
1348 -- Note on parentheses count. For cases like an if expression, the
1349 -- parens here really count as real parentheses for the paren count,
1350 -- so we adjust the paren count accordingly after scanning the expr.
1352 -- If expression
1354 if Token = Tok_If then
1355 Expr_Node := P_If_Expression;
1356 T_Right_Paren;
1357 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1358 return Expr_Node;
1360 -- Case expression
1362 elsif Token = Tok_Case then
1363 Expr_Node := P_Case_Expression;
1364 T_Right_Paren;
1365 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1366 return Expr_Node;
1368 -- Quantified expression
1370 elsif Token = Tok_For and then Is_Quantified_Expression then
1371 Expr_Node := P_Quantified_Expression;
1372 T_Right_Paren;
1373 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1374 return Expr_Node;
1376 -- Note: the mechanism used here of rescanning the initial expression
1377 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1378 -- out the discrete choice list.
1380 -- Deal with expression and extension aggregates first
1382 elsif Token /= Tok_Others then
1383 Save_Scan_State (Scan_State); -- at start of expression
1385 -- Deal with (NULL RECORD)
1387 if Token = Tok_Null then
1388 Scan; -- past NULL
1390 if Token = Tok_Record then
1391 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1392 Set_Null_Record_Present (Aggregate_Node, True);
1393 Scan; -- past RECORD
1394 T_Right_Paren;
1395 return Aggregate_Node;
1396 else
1397 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1398 end if;
1400 elsif Token = Tok_For then
1401 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1402 Expr_Node := P_Iterated_Component_Association;
1403 goto Aggregate;
1404 end if;
1406 -- Scan expression, handling box appearing as positional argument
1408 if Token = Tok_Box then
1409 Box_Error;
1410 else
1411 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1412 end if;
1414 -- Extension or Delta aggregate
1416 if Token = Tok_With then
1417 if Nkind (Expr_Node) = N_Attribute_Reference
1418 and then Attribute_Name (Expr_Node) = Name_Range
1419 then
1420 Bad_Range_Attribute (Sloc (Expr_Node));
1421 return Error;
1422 end if;
1424 if Ada_Version = Ada_83 then
1425 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1426 end if;
1428 Scan; -- past WITH
1429 if Token = Tok_Delta then
1430 Scan; -- past DELTA
1431 Aggregate_Node := New_Node (N_Delta_Aggregate, Lparen_Sloc);
1432 Set_Expression (Aggregate_Node, Expr_Node);
1433 Expr_Node := Empty;
1434 goto Aggregate;
1436 else
1437 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1438 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1439 end if;
1441 -- Deal with WITH NULL RECORD case
1443 if Token = Tok_Null then
1444 Save_Scan_State (Scan_State); -- at NULL
1445 Scan; -- past NULL
1447 if Token = Tok_Record then
1448 Scan; -- past RECORD
1449 Set_Null_Record_Present (Aggregate_Node, True);
1450 T_Right_Paren;
1451 return Aggregate_Node;
1453 else
1454 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1455 end if;
1456 end if;
1458 if Token /= Tok_Others then
1459 Save_Scan_State (Scan_State);
1460 Expr_Node := P_Expression;
1461 else
1462 Expr_Node := Empty;
1463 end if;
1465 -- Expression
1467 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1468 if Nkind (Expr_Node) = N_Attribute_Reference
1469 and then Attribute_Name (Expr_Node) = Name_Range
1470 then
1471 Error_Msg
1472 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1473 Scan; -- past right paren
1474 return Expr_Node;
1475 end if;
1477 -- Bump paren count of expression
1479 if Expr_Node /= Error then
1480 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1481 end if;
1483 T_Right_Paren; -- past right paren (error message if none)
1484 return Expr_Node;
1486 -- Normal aggregate
1488 else
1489 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1490 end if;
1492 -- Others
1494 else
1495 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1496 Expr_Node := Empty;
1497 end if;
1499 -- Prepare to scan list of component associations
1500 <<Aggregate>>
1501 Expr_List := No_List; -- don't set yet, maybe all named entries
1502 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1504 -- This loop scans through component associations. On entry to the
1505 -- loop, an expression has been scanned at the start of the current
1506 -- association unless initial token was OTHERS, in which case
1507 -- Expr_Node is set to Empty.
1509 loop
1510 -- Deal with others association first. This is a named association
1512 if No (Expr_Node) then
1513 if No (Assoc_List) then
1514 Assoc_List := New_List;
1515 end if;
1517 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1519 -- Improper use of WITH
1521 elsif Token = Tok_With then
1522 Error_Msg_SC ("WITH must be preceded by single expression in " &
1523 "extension aggregate");
1524 raise Error_Resync;
1526 -- Range attribute can only appear as part of a discrete choice list
1528 elsif Nkind (Expr_Node) = N_Attribute_Reference
1529 and then Attribute_Name (Expr_Node) = Name_Range
1530 and then Token /= Tok_Arrow
1531 and then Token /= Tok_Vertical_Bar
1532 then
1533 Bad_Range_Attribute (Sloc (Expr_Node));
1534 return Error;
1536 -- Assume positional case if comma, right paren, or literal or
1537 -- identifier or OTHERS follows (the latter cases are missing
1538 -- comma cases). Also assume positional if a semicolon follows,
1539 -- which can happen if there are missing parens.
1541 elsif Nkind (Expr_Node) = N_Iterated_Component_Association then
1542 if No (Assoc_List) then
1543 Assoc_List := New_List (Expr_Node);
1544 else
1545 Append_To (Assoc_List, Expr_Node);
1546 end if;
1548 elsif Token = Tok_Comma
1549 or else Token = Tok_Right_Paren
1550 or else Token = Tok_Others
1551 or else Token in Token_Class_Lit_Or_Name
1552 or else Token = Tok_Semicolon
1553 then
1554 if Present (Assoc_List) then
1555 Error_Msg_BC -- CODEFIX
1556 ("""='>"" expected (positional association cannot follow "
1557 & "named association)");
1558 end if;
1560 if No (Expr_List) then
1561 Expr_List := New_List;
1562 end if;
1564 Append (Expr_Node, Expr_List);
1566 -- Check for aggregate followed by left parent, maybe missing comma
1568 elsif Nkind (Expr_Node) = N_Aggregate
1569 and then Token = Tok_Left_Paren
1570 then
1571 T_Comma;
1573 if No (Expr_List) then
1574 Expr_List := New_List;
1575 end if;
1577 Append (Expr_Node, Expr_List);
1579 -- Anything else is assumed to be a named association
1581 else
1582 Restore_Scan_State (Scan_State); -- to start of expression
1584 if No (Assoc_List) then
1585 Assoc_List := New_List;
1586 end if;
1588 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1589 end if;
1591 exit when not Comma_Present;
1593 -- If we are at an expression terminator, something is seriously
1594 -- wrong, so let's get out now, before we start eating up stuff
1595 -- that doesn't belong to us.
1597 if Token in Token_Class_Eterm and then Token /= Tok_For then
1598 Error_Msg_AP
1599 ("expecting expression or component association");
1600 exit;
1601 end if;
1603 -- Deal with misused box
1605 if Token = Tok_Box then
1606 Box_Error;
1608 -- Otherwise initiate for reentry to top of loop by scanning an
1609 -- initial expression, unless the first token is OTHERS or FOR,
1610 -- which indicates an iterated component association.
1612 elsif Token = Tok_Others then
1613 Expr_Node := Empty;
1615 elsif Token = Tok_For then
1616 Expr_Node := P_Iterated_Component_Association;
1618 else
1619 Save_Scan_State (Scan_State); -- at start of expression
1620 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1622 end if;
1623 end loop;
1625 -- All component associations (positional and named) have been scanned
1627 T_Right_Paren;
1629 if Nkind (Aggregate_Node) /= N_Delta_Aggregate then
1630 Set_Expressions (Aggregate_Node, Expr_List);
1631 end if;
1633 Set_Component_Associations (Aggregate_Node, Assoc_List);
1634 return Aggregate_Node;
1635 end P_Aggregate_Or_Paren_Expr;
1637 ------------------------------------------------
1638 -- 4.3 Record or Array Component Association --
1639 ------------------------------------------------
1641 -- RECORD_COMPONENT_ASSOCIATION ::=
1642 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1643 -- | COMPONENT_CHOICE_LIST => <>
1645 -- COMPONENT_CHOICE_LIST =>
1646 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1647 -- | others
1649 -- ARRAY_COMPONENT_ASSOCIATION ::=
1650 -- DISCRETE_CHOICE_LIST => EXPRESSION
1651 -- | DISCRETE_CHOICE_LIST => <>
1652 -- | ITERATED_COMPONENT_ASSOCIATION
1654 -- Note: this routine only handles the named cases, including others.
1655 -- Cases where the component choice list is not present have already
1656 -- been handled directly.
1658 -- Error recovery: can raise Error_Resync
1660 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1661 -- rules have been extended to give support to Ada 2005 limited
1662 -- aggregates (AI-287)
1664 function P_Record_Or_Array_Component_Association return Node_Id is
1665 Assoc_Node : Node_Id;
1667 begin
1668 if Token = Tok_For then
1669 return P_Iterated_Component_Association;
1670 end if;
1672 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1673 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1674 Set_Sloc (Assoc_Node, Token_Ptr);
1675 TF_Arrow;
1677 if Token = Tok_Box then
1679 -- Ada 2005(AI-287): The box notation is used to indicate the
1680 -- default initialization of aggregate components
1682 if Ada_Version < Ada_2005 then
1683 Error_Msg_SP
1684 ("component association with '<'> is an Ada 2005 extension");
1685 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1686 end if;
1688 Set_Box_Present (Assoc_Node);
1689 Scan; -- Past box
1690 else
1691 Set_Expression (Assoc_Node, P_Expression);
1692 end if;
1694 return Assoc_Node;
1695 end P_Record_Or_Array_Component_Association;
1697 -----------------------------
1698 -- 4.3.1 Record Aggregate --
1699 -----------------------------
1701 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1702 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1704 ----------------------------------------------
1705 -- 4.3.1 Record Component Association List --
1706 ----------------------------------------------
1708 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1710 ----------------------------------
1711 -- 4.3.1 Component Choice List --
1712 ----------------------------------
1714 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1716 --------------------------------
1717 -- 4.3.1 Extension Aggregate --
1718 --------------------------------
1720 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1722 --------------------------
1723 -- 4.3.1 Ancestor Part --
1724 --------------------------
1726 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1728 ----------------------------
1729 -- 4.3.1 Array Aggregate --
1730 ----------------------------
1732 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1734 ---------------------------------------
1735 -- 4.3.1 Positional Array Aggregate --
1736 ---------------------------------------
1738 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1740 ----------------------------------
1741 -- 4.3.1 Named Array Aggregate --
1742 ----------------------------------
1744 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1746 ----------------------------------------
1747 -- 4.3.1 Array Component Association --
1748 ----------------------------------------
1750 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1752 ---------------------
1753 -- 4.4 Expression --
1754 ---------------------
1756 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1758 -- EXPRESSION ::=
1759 -- RELATION {LOGICAL_OPERATOR RELATION}
1761 -- CHOICE_EXPRESSION ::=
1762 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1764 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1766 -- On return, Expr_Form indicates the categorization of the expression
1767 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1768 -- an error message is given, and Error is returned).
1770 -- Error recovery: cannot raise Error_Resync
1772 function P_Expression return Node_Id is
1773 Logical_Op : Node_Kind;
1774 Prev_Logical_Op : Node_Kind;
1775 Op_Location : Source_Ptr;
1776 Node1 : Node_Id;
1777 Node2 : Node_Id;
1779 begin
1780 Node1 := P_Relation;
1782 if Token in Token_Class_Logop then
1783 Prev_Logical_Op := N_Empty;
1785 loop
1786 Op_Location := Token_Ptr;
1787 Logical_Op := P_Logical_Operator;
1789 if Prev_Logical_Op /= N_Empty and then
1790 Logical_Op /= Prev_Logical_Op
1791 then
1792 Error_Msg
1793 ("mixed logical operators in expression", Op_Location);
1794 Prev_Logical_Op := N_Empty;
1795 else
1796 Prev_Logical_Op := Logical_Op;
1797 end if;
1799 Node2 := Node1;
1800 Node1 := New_Op_Node (Logical_Op, Op_Location);
1801 Set_Left_Opnd (Node1, Node2);
1802 Set_Right_Opnd (Node1, P_Relation);
1804 -- Check for case of errant comma or semicolon
1806 if Token = Tok_Comma or else Token = Tok_Semicolon then
1807 declare
1808 Com : constant Boolean := Token = Tok_Comma;
1809 Scan_State : Saved_Scan_State;
1810 Logop : Node_Kind;
1812 begin
1813 Save_Scan_State (Scan_State); -- at comma/semicolon
1814 Scan; -- past comma/semicolon
1816 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1817 -- do not deal with AND/OR because those cases get mixed up
1818 -- with the select alternatives case.
1820 if Token = Tok_And or else Token = Tok_Or then
1821 Logop := P_Logical_Operator;
1822 Restore_Scan_State (Scan_State); -- to comma/semicolon
1824 if Nkind_In (Logop, N_And_Then, N_Or_Else) then
1825 Scan; -- past comma/semicolon
1827 if Com then
1828 Error_Msg_SP -- CODEFIX
1829 ("|extra "","" ignored");
1830 else
1831 Error_Msg_SP -- CODEFIX
1832 ("|extra "";"" ignored");
1833 end if;
1835 else
1836 Restore_Scan_State (Scan_State); -- to comma/semicolon
1837 end if;
1839 else
1840 Restore_Scan_State (Scan_State); -- to comma/semicolon
1841 end if;
1842 end;
1843 end if;
1845 exit when Token not in Token_Class_Logop;
1846 end loop;
1848 Expr_Form := EF_Non_Simple;
1849 end if;
1851 if Token = Tok_Apostrophe then
1852 Bad_Range_Attribute (Token_Ptr);
1853 return Error;
1854 else
1855 return Node1;
1856 end if;
1857 end P_Expression;
1859 -- This function is identical to the normal P_Expression, except that it
1860 -- also permits the appearance of a case, conditional, or quantified
1861 -- expression if the call immediately follows a left paren, and followed
1862 -- by a right parenthesis. These forms are allowed if these conditions
1863 -- are not met, but an error message will be issued.
1865 function P_Expression_If_OK return Node_Id is
1866 begin
1867 -- Case of conditional, case or quantified expression
1869 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1870 return P_Unparen_Cond_Case_Quant_Expression;
1872 -- Normal case, not case/conditional/quantified expression
1874 else
1875 return P_Expression;
1876 end if;
1877 end P_Expression_If_OK;
1879 -- This function is identical to the normal P_Expression, except that it
1880 -- checks that the expression scan did not stop on a right paren. It is
1881 -- called in all contexts where a right parenthesis cannot legitimately
1882 -- follow an expression.
1884 -- Error recovery: can not raise Error_Resync
1886 function P_Expression_No_Right_Paren return Node_Id is
1887 Expr : constant Node_Id := P_Expression;
1888 begin
1889 Ignore (Tok_Right_Paren);
1890 return Expr;
1891 end P_Expression_No_Right_Paren;
1893 ----------------------------------------
1894 -- 4.4 Expression_Or_Range_Attribute --
1895 ----------------------------------------
1897 -- EXPRESSION ::=
1898 -- RELATION {and RELATION} | RELATION {and then RELATION}
1899 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1900 -- | RELATION {xor RELATION}
1902 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1904 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1906 -- On return, Expr_Form indicates the categorization of the expression
1907 -- and EF_Range_Attr is one of the possibilities.
1909 -- Error recovery: cannot raise Error_Resync
1911 -- In the grammar, a RANGE attribute is simply a name, but its use is
1912 -- highly restricted, so in the parser, we do not regard it as a name.
1913 -- Instead, P_Name returns without scanning the 'RANGE part of the
1914 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1915 -- attribute reference. In the normal case where a range attribute is
1916 -- not allowed, an error message is issued by P_Expression.
1918 function P_Expression_Or_Range_Attribute return Node_Id is
1919 Logical_Op : Node_Kind;
1920 Prev_Logical_Op : Node_Kind;
1921 Op_Location : Source_Ptr;
1922 Node1 : Node_Id;
1923 Node2 : Node_Id;
1924 Attr_Node : Node_Id;
1926 begin
1927 Node1 := P_Relation;
1929 if Token = Tok_Apostrophe then
1930 Attr_Node := P_Range_Attribute_Reference (Node1);
1931 Expr_Form := EF_Range_Attr;
1932 return Attr_Node;
1934 elsif Token in Token_Class_Logop then
1935 Prev_Logical_Op := N_Empty;
1937 loop
1938 Op_Location := Token_Ptr;
1939 Logical_Op := P_Logical_Operator;
1941 if Prev_Logical_Op /= N_Empty and then
1942 Logical_Op /= Prev_Logical_Op
1943 then
1944 Error_Msg
1945 ("mixed logical operators in expression", Op_Location);
1946 Prev_Logical_Op := N_Empty;
1947 else
1948 Prev_Logical_Op := Logical_Op;
1949 end if;
1951 Node2 := Node1;
1952 Node1 := New_Op_Node (Logical_Op, Op_Location);
1953 Set_Left_Opnd (Node1, Node2);
1954 Set_Right_Opnd (Node1, P_Relation);
1955 exit when Token not in Token_Class_Logop;
1956 end loop;
1958 Expr_Form := EF_Non_Simple;
1959 end if;
1961 if Token = Tok_Apostrophe then
1962 Bad_Range_Attribute (Token_Ptr);
1963 return Error;
1964 else
1965 return Node1;
1966 end if;
1967 end P_Expression_Or_Range_Attribute;
1969 -- Version that allows a non-parenthesized case, conditional, or quantified
1970 -- expression if the call immediately follows a left paren, and followed
1971 -- by a right parenthesis. These forms are allowed if these conditions
1972 -- are not met, but an error message will be issued.
1974 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1975 begin
1976 -- Case of conditional, case or quantified expression
1978 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1979 return P_Unparen_Cond_Case_Quant_Expression;
1981 -- Normal case, not one of the above expression types
1983 else
1984 return P_Expression_Or_Range_Attribute;
1985 end if;
1986 end P_Expression_Or_Range_Attribute_If_OK;
1988 -------------------
1989 -- 4.4 Relation --
1990 -------------------
1992 -- This procedure scans both relations and choice relations
1994 -- CHOICE_RELATION ::=
1995 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1997 -- RELATION ::=
1998 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
1999 -- | RAISE_EXPRESSION
2001 -- MEMBERSHIP_CHOICE_LIST ::=
2002 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
2004 -- MEMBERSHIP_CHOICE ::=
2005 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
2007 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
2009 -- On return, Expr_Form indicates the categorization of the expression
2011 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2012 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2014 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2015 -- expression, then tokens are scanned until either a non-expression token,
2016 -- a right paren (not matched by a left paren) or a comma, is encountered.
2018 function P_Relation return Node_Id is
2019 Node1, Node2 : Node_Id;
2020 Optok : Source_Ptr;
2022 begin
2023 -- First check for raise expression
2025 if Token = Tok_Raise then
2026 Expr_Form := EF_Non_Simple;
2027 return P_Raise_Expression;
2028 end if;
2030 -- All other cases
2032 Node1 := P_Simple_Expression;
2034 if Token not in Token_Class_Relop then
2035 return Node1;
2037 else
2038 -- Here we have a relational operator following. If so then scan it
2039 -- out. Note that the assignment symbol := is treated as a relational
2040 -- operator to improve the error recovery when it is misused for =.
2041 -- P_Relational_Operator also parses the IN and NOT IN operations.
2043 Optok := Token_Ptr;
2044 Node2 := New_Op_Node (P_Relational_Operator, Optok);
2045 Set_Left_Opnd (Node2, Node1);
2047 -- Case of IN or NOT IN
2049 if Prev_Token = Tok_In then
2050 P_Membership_Test (Node2);
2052 -- Case of relational operator (= /= < <= > >=)
2054 else
2055 Set_Right_Opnd (Node2, P_Simple_Expression);
2056 end if;
2058 Expr_Form := EF_Non_Simple;
2060 if Token in Token_Class_Relop then
2061 Error_Msg_SC ("unexpected relational operator");
2062 raise Error_Resync;
2063 end if;
2065 return Node2;
2066 end if;
2068 -- If any error occurs, then scan to the next expression terminator symbol
2069 -- or comma or right paren at the outer (i.e. current) parentheses level.
2070 -- The flags are set to indicate a normal simple expression.
2072 exception
2073 when Error_Resync =>
2074 Resync_Expression;
2075 Expr_Form := EF_Simple;
2076 return Error;
2077 end P_Relation;
2079 ----------------------------
2080 -- 4.4 Simple Expression --
2081 ----------------------------
2083 -- SIMPLE_EXPRESSION ::=
2084 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2086 -- On return, Expr_Form indicates the categorization of the expression
2088 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2089 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2091 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2092 -- expression, then tokens are scanned until either a non-expression token,
2093 -- a right paren (not matched by a left paren) or a comma, is encountered.
2095 -- Note: P_Simple_Expression is called only internally by higher level
2096 -- expression routines. In cases in the grammar where a simple expression
2097 -- is required, the approach is to scan an expression, and then post an
2098 -- appropriate error message if the expression obtained is not simple. This
2099 -- gives better error recovery and treatment.
2101 function P_Simple_Expression return Node_Id is
2102 Scan_State : Saved_Scan_State;
2103 Node1 : Node_Id;
2104 Node2 : Node_Id;
2105 Tokptr : Source_Ptr;
2107 function At_Start_Of_Attribute return Boolean;
2108 -- Tests if we have quote followed by attribute name, if so, return True
2109 -- otherwise return False.
2111 ---------------------------
2112 -- At_Start_Of_Attribute --
2113 ---------------------------
2115 function At_Start_Of_Attribute return Boolean is
2116 begin
2117 if Token /= Tok_Apostrophe then
2118 return False;
2120 else
2121 declare
2122 Scan_State : Saved_Scan_State;
2124 begin
2125 Save_Scan_State (Scan_State);
2126 Scan; -- past quote
2128 if Token = Tok_Identifier
2129 and then Is_Attribute_Name (Chars (Token_Node))
2130 then
2131 Restore_Scan_State (Scan_State);
2132 return True;
2133 else
2134 Restore_Scan_State (Scan_State);
2135 return False;
2136 end if;
2137 end;
2138 end if;
2139 end At_Start_Of_Attribute;
2141 -- Start of processing for P_Simple_Expression
2143 begin
2144 -- Check for cases starting with a name. There are two reasons for
2145 -- special casing. First speed things up by catching a common case
2146 -- without going through several routine layers. Second the caller must
2147 -- be informed via Expr_Form when the simple expression is a name.
2149 if Token in Token_Class_Name then
2150 Node1 := P_Name;
2152 -- Deal with apostrophe cases
2154 if Token = Tok_Apostrophe then
2155 Save_Scan_State (Scan_State); -- at apostrophe
2156 Scan; -- past apostrophe
2158 -- If qualified expression, scan it out and fall through
2160 if Token = Tok_Left_Paren then
2161 Node1 := P_Qualified_Expression (Node1);
2162 Expr_Form := EF_Simple;
2164 -- If range attribute, then we return with Token pointing to the
2165 -- apostrophe. Note: avoid the normal error check on exit. We
2166 -- know that the expression really is complete in this case.
2168 else -- Token = Tok_Range then
2169 Restore_Scan_State (Scan_State); -- to apostrophe
2170 Expr_Form := EF_Simple_Name;
2171 return Node1;
2172 end if;
2173 end if;
2175 -- If an expression terminator follows, the previous processing
2176 -- completely scanned out the expression (a common case), and
2177 -- left Expr_Form set appropriately for returning to our caller.
2179 if Token in Token_Class_Sterm then
2180 null;
2182 -- If we do not have an expression terminator, then complete the
2183 -- scan of a simple expression. This code duplicates the code
2184 -- found in P_Term and P_Factor.
2186 else
2187 if Token = Tok_Double_Asterisk then
2188 if Style_Check then
2189 Style.Check_Exponentiation_Operator;
2190 end if;
2192 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2193 Scan; -- past **
2194 Set_Left_Opnd (Node2, Node1);
2195 Set_Right_Opnd (Node2, P_Primary);
2196 Check_Bad_Exp;
2197 Node1 := Node2;
2198 end if;
2200 loop
2201 exit when Token not in Token_Class_Mulop;
2202 Tokptr := Token_Ptr;
2203 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2205 if Style_Check then
2206 Style.Check_Binary_Operator;
2207 end if;
2209 Scan; -- past operator
2210 Set_Left_Opnd (Node2, Node1);
2211 Set_Right_Opnd (Node2, P_Factor);
2212 Node1 := Node2;
2213 end loop;
2215 loop
2216 exit when Token not in Token_Class_Binary_Addop;
2217 Tokptr := Token_Ptr;
2218 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2220 if Style_Check then
2221 Style.Check_Binary_Operator;
2222 end if;
2224 Scan; -- past operator
2225 Set_Left_Opnd (Node2, Node1);
2226 Set_Right_Opnd (Node2, P_Term);
2227 Node1 := Node2;
2228 end loop;
2230 Expr_Form := EF_Simple;
2231 end if;
2233 -- Cases where simple expression does not start with a name
2235 else
2236 -- Scan initial sign and initial Term
2238 if Token in Token_Class_Unary_Addop then
2239 Tokptr := Token_Ptr;
2240 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
2242 if Style_Check then
2243 Style.Check_Unary_Plus_Or_Minus (Inside_Depends);
2244 end if;
2246 Scan; -- past operator
2247 Set_Right_Opnd (Node1, P_Term);
2248 else
2249 Node1 := P_Term;
2250 end if;
2252 -- In the following, we special-case a sequence of concatenations of
2253 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2254 -- else mixed in. For such a sequence, we return a tree representing
2255 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2256 -- the number of concatenations is large. If semantic analysis
2257 -- resolves the "&" to a predefined one, then this folding gives the
2258 -- right answer. Otherwise, semantic analysis will complain about a
2259 -- capacity-exceeded error. The purpose of this trick is to avoid
2260 -- creating a deeply nested tree, which would cause deep recursion
2261 -- during semantics, causing stack overflow. This way, we can handle
2262 -- enormous concatenations in the normal case of predefined "&". We
2263 -- first build up the normal tree, and then rewrite it if
2264 -- appropriate.
2266 declare
2267 Num_Concats_Threshold : constant Positive := 1000;
2268 -- Arbitrary threshold value to enable optimization
2270 First_Node : constant Node_Id := Node1;
2271 Is_Strlit_Concat : Boolean;
2272 -- True iff we've parsed a sequence of concatenations of string
2273 -- literals, with nothing else mixed in.
2275 Num_Concats : Natural;
2276 -- Number of "&" operators if Is_Strlit_Concat is True
2278 begin
2279 Is_Strlit_Concat :=
2280 Nkind (Node1) = N_String_Literal
2281 and then Token = Tok_Ampersand;
2282 Num_Concats := 0;
2284 -- Scan out sequence of terms separated by binary adding operators
2286 loop
2287 exit when Token not in Token_Class_Binary_Addop;
2288 Tokptr := Token_Ptr;
2289 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2291 if Style_Check and then not Debug_Flag_Dot_QQ then
2292 Style.Check_Binary_Operator;
2293 end if;
2295 Scan; -- past operator
2296 Set_Left_Opnd (Node2, Node1);
2297 Node1 := P_Term;
2298 Set_Right_Opnd (Node2, Node1);
2300 -- Check if we're still concatenating string literals
2302 Is_Strlit_Concat :=
2303 Is_Strlit_Concat
2304 and then Nkind (Node2) = N_Op_Concat
2305 and then Nkind (Node1) = N_String_Literal;
2307 if Is_Strlit_Concat then
2308 Num_Concats := Num_Concats + 1;
2309 end if;
2311 Node1 := Node2;
2312 end loop;
2314 -- If we have an enormous series of concatenations of string
2315 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2316 -- flag tells semantic analysis that if the "&" is not predefined,
2317 -- the folded value is wrong.
2319 if Is_Strlit_Concat
2320 and then Num_Concats >= Num_Concats_Threshold
2321 then
2322 declare
2323 Empty_String_Val : String_Id;
2324 -- String_Id for ""
2326 Strlit_Concat_Val : String_Id;
2327 -- Contains the folded value (which will be correct if the
2328 -- "&" operators are the predefined ones).
2330 Cur_Node : Node_Id;
2331 -- For walking up the tree
2333 New_Node : Node_Id;
2334 -- Folded node to replace Node1
2336 Loc : constant Source_Ptr := Sloc (First_Node);
2338 begin
2339 -- Walk up the tree starting at the leftmost string literal
2340 -- (First_Node), building up the Strlit_Concat_Val as we
2341 -- go. Note that we do not use recursion here -- the whole
2342 -- point is to avoid recursively walking that enormous tree.
2344 Start_String;
2345 Store_String_Chars (Strval (First_Node));
2347 Cur_Node := Parent (First_Node);
2348 while Present (Cur_Node) loop
2349 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2350 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2352 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2353 Cur_Node := Parent (Cur_Node);
2354 end loop;
2356 Strlit_Concat_Val := End_String;
2358 -- Create new folded node, and rewrite result with a concat-
2359 -- enation of an empty string literal and the folded node.
2361 Start_String;
2362 Empty_String_Val := End_String;
2363 New_Node :=
2364 Make_Op_Concat (Loc,
2365 Make_String_Literal (Loc, Empty_String_Val),
2366 Make_String_Literal (Loc, Strlit_Concat_Val,
2367 Is_Folded_In_Parser => True));
2368 Rewrite (Node1, New_Node);
2369 end;
2370 end if;
2371 end;
2373 -- All done, we clearly do not have name or numeric literal so this
2374 -- is a case of a simple expression which is some other possibility.
2376 Expr_Form := EF_Simple;
2377 end if;
2379 -- Come here at end of simple expression, where we do a couple of
2380 -- special checks to improve error recovery.
2382 -- Special test to improve error recovery. If the current token is a
2383 -- period, then someone is trying to do selection on something that is
2384 -- not a name, e.g. a qualified expression.
2386 if Token = Tok_Dot then
2387 Error_Msg_SC ("prefix for selection is not a name");
2389 -- If qualified expression, comment and continue, otherwise something
2390 -- is pretty nasty so do an Error_Resync call.
2392 if Ada_Version < Ada_2012
2393 and then Nkind (Node1) = N_Qualified_Expression
2394 then
2395 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2396 else
2397 raise Error_Resync;
2398 end if;
2399 end if;
2401 -- Special test to improve error recovery: If the current token is
2402 -- not the first token on a line (as determined by checking the
2403 -- previous token position with the start of the current line),
2404 -- then we insist that we have an appropriate terminating token.
2405 -- Consider the following two examples:
2407 -- 1) if A nad B then ...
2409 -- 2) A := B
2410 -- C := D
2412 -- In the first example, we would like to issue a binary operator
2413 -- expected message and resynchronize to the then. In the second
2414 -- example, we do not want to issue a binary operator message, so
2415 -- that instead we will get the missing semicolon message. This
2416 -- distinction is of course a heuristic which does not always work,
2417 -- but in practice it is quite effective.
2419 -- Note: the one case in which we do not go through this circuit is
2420 -- when we have scanned a range attribute and want to return with
2421 -- Token pointing to the apostrophe. The apostrophe is not normally
2422 -- an expression terminator, and is not in Token_Class_Sterm, but
2423 -- in this special case we know that the expression is complete.
2425 if not Token_Is_At_Start_Of_Line
2426 and then Token not in Token_Class_Sterm
2427 then
2428 -- Normally the right error message is indeed that we expected a
2429 -- binary operator, but in the case of being between a right and left
2430 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2432 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2433 T_Comma;
2435 -- And if we have a quote, we may have a bad attribute
2437 elsif At_Start_Of_Attribute then
2438 Error_Msg_SC ("prefix of attribute must be a name");
2440 if Ada_Version >= Ada_2012 then
2441 Error_Msg_SC ("\qualify expression to turn it into a name");
2442 end if;
2444 -- Normal case for binary operator expected message
2446 else
2447 Error_Msg_AP ("binary operator expected");
2448 end if;
2450 raise Error_Resync;
2452 else
2453 return Node1;
2454 end if;
2456 -- If any error occurs, then scan to next expression terminator symbol
2457 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2458 -- level. Expr_Form is set to indicate a normal simple expression.
2460 exception
2461 when Error_Resync =>
2462 Resync_Expression;
2463 Expr_Form := EF_Simple;
2464 return Error;
2465 end P_Simple_Expression;
2467 -----------------------------------------------
2468 -- 4.4 Simple Expression or Range Attribute --
2469 -----------------------------------------------
2471 -- SIMPLE_EXPRESSION ::=
2472 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2474 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2476 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2478 -- Error recovery: cannot raise Error_Resync
2480 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2481 Sexpr : Node_Id;
2482 Attr_Node : Node_Id;
2484 begin
2485 -- We don't just want to roar ahead and call P_Simple_Expression
2486 -- here, since we want to handle the case of a parenthesized range
2487 -- attribute cleanly.
2489 if Token = Tok_Left_Paren then
2490 declare
2491 Lptr : constant Source_Ptr := Token_Ptr;
2492 Scan_State : Saved_Scan_State;
2494 begin
2495 Save_Scan_State (Scan_State);
2496 Scan; -- past left paren
2497 Sexpr := P_Simple_Expression;
2499 if Token = Tok_Apostrophe then
2500 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2501 Expr_Form := EF_Range_Attr;
2503 if Token = Tok_Right_Paren then
2504 Scan; -- scan past right paren if present
2505 end if;
2507 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2509 return Attr_Node;
2510 end if;
2512 Restore_Scan_State (Scan_State);
2513 end;
2514 end if;
2516 -- Here after dealing with parenthesized range attribute
2518 Sexpr := P_Simple_Expression;
2520 if Token = Tok_Apostrophe then
2521 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2522 Expr_Form := EF_Range_Attr;
2523 return Attr_Node;
2525 else
2526 return Sexpr;
2527 end if;
2528 end P_Simple_Expression_Or_Range_Attribute;
2530 ---------------
2531 -- 4.4 Term --
2532 ---------------
2534 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2536 -- Error recovery: can raise Error_Resync
2538 function P_Term return Node_Id is
2539 Node1, Node2 : Node_Id;
2540 Tokptr : Source_Ptr;
2542 begin
2543 Node1 := P_Factor;
2545 loop
2546 exit when Token not in Token_Class_Mulop;
2547 Tokptr := Token_Ptr;
2548 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2550 if Style_Check and then not Debug_Flag_Dot_QQ then
2551 Style.Check_Binary_Operator;
2552 end if;
2554 Scan; -- past operator
2555 Set_Left_Opnd (Node2, Node1);
2556 Set_Right_Opnd (Node2, P_Factor);
2557 Node1 := Node2;
2558 end loop;
2560 return Node1;
2561 end P_Term;
2563 -----------------
2564 -- 4.4 Factor --
2565 -----------------
2567 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2569 -- Error recovery: can raise Error_Resync
2571 function P_Factor return Node_Id is
2572 Node1 : Node_Id;
2573 Node2 : Node_Id;
2575 begin
2576 if Token = Tok_Abs then
2577 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2579 if Style_Check then
2580 Style.Check_Abs_Not;
2581 end if;
2583 Scan; -- past ABS
2584 Set_Right_Opnd (Node1, P_Primary);
2585 return Node1;
2587 elsif Token = Tok_Not then
2588 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2590 if Style_Check then
2591 Style.Check_Abs_Not;
2592 end if;
2594 Scan; -- past NOT
2595 Set_Right_Opnd (Node1, P_Primary);
2596 return Node1;
2598 else
2599 Node1 := P_Primary;
2601 if Token = Tok_Double_Asterisk then
2602 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2603 Scan; -- past **
2604 Set_Left_Opnd (Node2, Node1);
2605 Set_Right_Opnd (Node2, P_Primary);
2606 Check_Bad_Exp;
2607 return Node2;
2608 else
2609 return Node1;
2610 end if;
2611 end if;
2612 end P_Factor;
2614 ------------------
2615 -- 4.4 Primary --
2616 ------------------
2618 -- PRIMARY ::=
2619 -- NUMERIC_LITERAL | null
2620 -- | STRING_LITERAL | AGGREGATE
2621 -- | NAME | QUALIFIED_EXPRESSION
2622 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2624 -- Error recovery: can raise Error_Resync
2626 function P_Primary return Node_Id is
2627 Scan_State : Saved_Scan_State;
2628 Node1 : Node_Id;
2630 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
2631 -- Remember if previous token is a left parenthesis. This is used to
2632 -- deal with checking whether IF/CASE/FOR expressions appearing as
2633 -- primaries require extra parenthesization.
2635 begin
2636 -- The loop runs more than once only if misplaced pragmas are found
2637 -- or if a misplaced unary minus is skipped.
2639 loop
2640 case Token is
2642 -- Name token can start a name, call or qualified expression, all
2643 -- of which are acceptable possibilities for primary. Note also
2644 -- that string literal is included in name (as operator symbol)
2645 -- and type conversion is included in name (as indexed component).
2647 when Tok_Char_Literal
2648 | Tok_Identifier
2649 | Tok_Operator_Symbol
2651 Node1 := P_Name;
2653 -- All done unless apostrophe follows
2655 if Token /= Tok_Apostrophe then
2656 return Node1;
2658 -- Apostrophe following means that we have either just parsed
2659 -- the subtype mark of a qualified expression, or the prefix
2660 -- or a range attribute.
2662 else -- Token = Tok_Apostrophe
2663 Save_Scan_State (Scan_State); -- at apostrophe
2664 Scan; -- past apostrophe
2666 -- If range attribute, then this is always an error, since
2667 -- the only legitimate case (where the scanned expression is
2668 -- a qualified simple name) is handled at the level of the
2669 -- Simple_Expression processing. This case corresponds to a
2670 -- usage such as 3 + A'Range, which is always illegal.
2672 if Token = Tok_Range then
2673 Restore_Scan_State (Scan_State); -- to apostrophe
2674 Bad_Range_Attribute (Token_Ptr);
2675 return Error;
2677 -- If left paren, then we have a qualified expression.
2678 -- Note that P_Name guarantees that in this case, where
2679 -- Token = Tok_Apostrophe on return, the only two possible
2680 -- tokens following the apostrophe are left paren and
2681 -- RANGE, so we know we have a left paren here.
2683 else -- Token = Tok_Left_Paren
2684 return P_Qualified_Expression (Node1);
2686 end if;
2687 end if;
2689 -- Numeric or string literal
2691 when Tok_Integer_Literal
2692 | Tok_Real_Literal
2693 | Tok_String_Literal
2695 Node1 := Token_Node;
2696 Scan; -- past number
2697 return Node1;
2699 -- Left paren, starts aggregate or parenthesized expression
2701 when Tok_Left_Paren =>
2702 declare
2703 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2705 begin
2706 if Nkind (Expr) = N_Attribute_Reference
2707 and then Attribute_Name (Expr) = Name_Range
2708 then
2709 Bad_Range_Attribute (Sloc (Expr));
2710 end if;
2712 return Expr;
2713 end;
2715 -- Allocator
2717 when Tok_New =>
2718 return P_Allocator;
2720 -- Null
2722 when Tok_Null =>
2723 Scan; -- past NULL
2724 return New_Node (N_Null, Prev_Token_Ptr);
2726 -- Pragma, not allowed here, so just skip past it
2728 when Tok_Pragma =>
2729 P_Pragmas_Misplaced;
2731 -- Deal with IF (possible unparenthesized if expression)
2733 when Tok_If =>
2735 -- If this looks like a real if, defined as an IF appearing at
2736 -- the start of a new line, then we consider we have a missing
2737 -- operand. If in Ada 2012 and the IF is not properly indented
2738 -- for a statement, we prefer to issue a message about an ill-
2739 -- parenthesized if expression.
2741 if Token_Is_At_Start_Of_Line
2742 and then not
2743 (Ada_Version >= Ada_2012
2744 and then Style_Check_Indentation /= 0
2745 and then Start_Column rem Style_Check_Indentation /= 0)
2746 then
2747 Error_Msg_AP ("missing operand");
2748 return Error;
2750 -- If this looks like an if expression, then treat it that way
2751 -- with an error message if not explicitly surrounded by
2752 -- parentheses.
2754 elsif Ada_Version >= Ada_2012 then
2755 Node1 := P_If_Expression;
2757 if not (Lparen and then Token = Tok_Right_Paren) then
2758 Error_Msg
2759 ("if expression must be parenthesized", Sloc (Node1));
2760 end if;
2762 return Node1;
2764 -- Otherwise treat as misused identifier
2766 else
2767 return P_Identifier;
2768 end if;
2770 -- Deal with CASE (possible unparenthesized case expression)
2772 when Tok_Case =>
2774 -- If this looks like a real case, defined as a CASE appearing
2775 -- the start of a new line, then we consider we have a missing
2776 -- operand. If in Ada 2012 and the CASE is not properly
2777 -- indented for a statement, we prefer to issue a message about
2778 -- an ill-parenthesized case expression.
2780 if Token_Is_At_Start_Of_Line
2781 and then not
2782 (Ada_Version >= Ada_2012
2783 and then Style_Check_Indentation /= 0
2784 and then Start_Column rem Style_Check_Indentation /= 0)
2785 then
2786 Error_Msg_AP ("missing operand");
2787 return Error;
2789 -- If this looks like a case expression, then treat it that way
2790 -- with an error message if not within parentheses.
2792 elsif Ada_Version >= Ada_2012 then
2793 Node1 := P_Case_Expression;
2795 if not (Lparen and then Token = Tok_Right_Paren) then
2796 Error_Msg
2797 ("case expression must be parenthesized", Sloc (Node1));
2798 end if;
2800 return Node1;
2802 -- Otherwise treat as misused identifier
2804 else
2805 return P_Identifier;
2806 end if;
2808 -- For [all | some] indicates a quantified expression
2810 when Tok_For =>
2811 if Token_Is_At_Start_Of_Line then
2812 Error_Msg_AP ("misplaced loop");
2813 return Error;
2815 elsif Ada_Version >= Ada_2012 then
2816 Save_Scan_State (Scan_State);
2817 Scan; -- past FOR
2819 if Token = Tok_All or else Token = Tok_Some then
2820 Restore_Scan_State (Scan_State); -- To FOR
2821 Node1 := P_Quantified_Expression;
2823 if not (Lparen and then Token = Tok_Right_Paren) then
2824 Error_Msg
2825 ("quantified expression must be parenthesized",
2826 Sloc (Node1));
2827 end if;
2828 else
2829 Restore_Scan_State (Scan_State); -- To FOR
2830 Node1 := P_Iterated_Component_Association;
2831 end if;
2833 return Node1;
2835 -- Otherwise treat as misused identifier
2837 else
2838 return P_Identifier;
2839 end if;
2841 -- Minus may well be an improper attempt at a unary minus. Give
2842 -- a message, skip the minus and keep going.
2844 when Tok_Minus =>
2845 Error_Msg_SC ("parentheses required for unary minus");
2846 Scan; -- past minus
2848 when Tok_At_Sign => -- AI12-0125 : target_name
2849 if Ada_Version < Ada_2020 then
2850 Error_Msg_SC ("target name is an Ada 2020 extension");
2851 Error_Msg_SC ("\compile with -gnatX");
2852 end if;
2854 Node1 := P_Name;
2855 return Node1;
2857 -- Anything else is illegal as the first token of a primary, but
2858 -- we test for some common errors, to improve error messages.
2860 when others =>
2861 if Is_Reserved_Identifier then
2862 return P_Identifier;
2864 elsif Prev_Token = Tok_Comma then
2865 Error_Msg_SP -- CODEFIX
2866 ("|extra "","" ignored");
2867 raise Error_Resync;
2869 else
2870 Error_Msg_AP ("missing operand");
2871 raise Error_Resync;
2872 end if;
2873 end case;
2874 end loop;
2875 end P_Primary;
2877 -------------------------------
2878 -- 4.4 Quantified_Expression --
2879 -------------------------------
2881 -- QUANTIFIED_EXPRESSION ::=
2882 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
2883 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
2885 function P_Quantified_Expression return Node_Id is
2886 I_Spec : Node_Id;
2887 Node1 : Node_Id;
2889 begin
2890 Error_Msg_Ada_2012_Feature ("quantified expression", Token_Ptr);
2891 Scan; -- past FOR
2892 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
2894 if Token = Tok_All then
2895 Set_All_Present (Node1);
2896 elsif Token /= Tok_Some then
2897 Error_Msg_AP ("missing quantifier");
2898 raise Error_Resync;
2899 end if;
2901 Scan; -- past ALL or SOME
2902 I_Spec := P_Loop_Parameter_Specification;
2904 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
2905 Set_Loop_Parameter_Specification (Node1, I_Spec);
2906 else
2907 Set_Iterator_Specification (Node1, I_Spec);
2908 end if;
2910 if Token = Tok_Arrow then
2911 Scan;
2912 Set_Condition (Node1, P_Expression);
2913 return Node1;
2914 else
2915 Error_Msg_AP ("missing arrow");
2916 raise Error_Resync;
2917 end if;
2918 end P_Quantified_Expression;
2920 ---------------------------
2921 -- 4.5 Logical Operator --
2922 ---------------------------
2924 -- LOGICAL_OPERATOR ::= and | or | xor
2926 -- Note: AND THEN and OR ELSE are also treated as logical operators
2927 -- by the parser (even though they are not operators semantically)
2929 -- The value returned is the appropriate Node_Kind code for the operator
2930 -- On return, Token points to the token following the scanned operator.
2932 -- The caller has checked that the first token is a legitimate logical
2933 -- operator token (i.e. is either XOR, AND, OR).
2935 -- Error recovery: cannot raise Error_Resync
2937 function P_Logical_Operator return Node_Kind is
2938 begin
2939 if Token = Tok_And then
2940 if Style_Check then
2941 Style.Check_Binary_Operator;
2942 end if;
2944 Scan; -- past AND
2946 if Token = Tok_Then then
2947 Scan; -- past THEN
2948 return N_And_Then;
2949 else
2950 return N_Op_And;
2951 end if;
2953 elsif Token = Tok_Or then
2954 if Style_Check then
2955 Style.Check_Binary_Operator;
2956 end if;
2958 Scan; -- past OR
2960 if Token = Tok_Else then
2961 Scan; -- past ELSE
2962 return N_Or_Else;
2963 else
2964 return N_Op_Or;
2965 end if;
2967 else -- Token = Tok_Xor
2968 if Style_Check then
2969 Style.Check_Binary_Operator;
2970 end if;
2972 Scan; -- past XOR
2973 return N_Op_Xor;
2974 end if;
2975 end P_Logical_Operator;
2977 ------------------------------
2978 -- 4.5 Relational Operator --
2979 ------------------------------
2981 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2983 -- The value returned is the appropriate Node_Kind code for the operator.
2984 -- On return, Token points to the operator token, NOT past it.
2986 -- The caller has checked that the first token is a legitimate relational
2987 -- operator token (i.e. is one of the operator tokens listed above).
2989 -- Error recovery: cannot raise Error_Resync
2991 function P_Relational_Operator return Node_Kind is
2992 Op_Kind : Node_Kind;
2993 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2994 (Tok_Less => N_Op_Lt,
2995 Tok_Equal => N_Op_Eq,
2996 Tok_Greater => N_Op_Gt,
2997 Tok_Not_Equal => N_Op_Ne,
2998 Tok_Greater_Equal => N_Op_Ge,
2999 Tok_Less_Equal => N_Op_Le,
3000 Tok_In => N_In,
3001 Tok_Not => N_Not_In,
3002 Tok_Box => N_Op_Ne);
3004 begin
3005 if Token = Tok_Box then
3006 Error_Msg_SC -- CODEFIX
3007 ("|""'<'>"" should be ""/=""");
3008 end if;
3010 Op_Kind := Relop_Node (Token);
3012 if Style_Check then
3013 Style.Check_Binary_Operator;
3014 end if;
3016 Scan; -- past operator token
3018 -- Deal with NOT IN, if previous token was NOT, we must have IN now
3020 if Prev_Token = Tok_Not then
3022 -- Style check, for NOT IN, we require one space between NOT and IN
3024 if Style_Check and then Token = Tok_In then
3025 Style.Check_Not_In;
3026 end if;
3028 T_In;
3029 end if;
3031 return Op_Kind;
3032 end P_Relational_Operator;
3034 ---------------------------------
3035 -- 4.5 Binary Adding Operator --
3036 ---------------------------------
3038 -- BINARY_ADDING_OPERATOR ::= + | - | &
3040 -- The value returned is the appropriate Node_Kind code for the operator.
3041 -- On return, Token points to the operator token (NOT past it).
3043 -- The caller has checked that the first token is a legitimate adding
3044 -- operator token (i.e. is one of the operator tokens listed above).
3046 -- Error recovery: cannot raise Error_Resync
3048 function P_Binary_Adding_Operator return Node_Kind is
3049 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
3050 (Tok_Ampersand => N_Op_Concat,
3051 Tok_Minus => N_Op_Subtract,
3052 Tok_Plus => N_Op_Add);
3053 begin
3054 return Addop_Node (Token);
3055 end P_Binary_Adding_Operator;
3057 --------------------------------
3058 -- 4.5 Unary Adding Operator --
3059 --------------------------------
3061 -- UNARY_ADDING_OPERATOR ::= + | -
3063 -- The value returned is the appropriate Node_Kind code for the operator.
3064 -- On return, Token points to the operator token (NOT past it).
3066 -- The caller has checked that the first token is a legitimate adding
3067 -- operator token (i.e. is one of the operator tokens listed above).
3069 -- Error recovery: cannot raise Error_Resync
3071 function P_Unary_Adding_Operator return Node_Kind is
3072 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
3073 (Tok_Minus => N_Op_Minus,
3074 Tok_Plus => N_Op_Plus);
3075 begin
3076 return Addop_Node (Token);
3077 end P_Unary_Adding_Operator;
3079 -------------------------------
3080 -- 4.5 Multiplying Operator --
3081 -------------------------------
3083 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3085 -- The value returned is the appropriate Node_Kind code for the operator.
3086 -- On return, Token points to the operator token (NOT past it).
3088 -- The caller has checked that the first token is a legitimate multiplying
3089 -- operator token (i.e. is one of the operator tokens listed above).
3091 -- Error recovery: cannot raise Error_Resync
3093 function P_Multiplying_Operator return Node_Kind is
3094 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
3095 (Tok_Asterisk => N_Op_Multiply,
3096 Tok_Mod => N_Op_Mod,
3097 Tok_Rem => N_Op_Rem,
3098 Tok_Slash => N_Op_Divide);
3099 begin
3100 return Mulop_Node (Token);
3101 end P_Multiplying_Operator;
3103 --------------------------------------
3104 -- 4.5 Highest Precedence Operator --
3105 --------------------------------------
3107 -- Parsed by P_Factor (4.4)
3109 -- Note: this rule is not in fact used by the grammar at any point
3111 --------------------------
3112 -- 4.6 Type Conversion --
3113 --------------------------
3115 -- Parsed by P_Primary as a Name (4.1)
3117 -------------------------------
3118 -- 4.7 Qualified Expression --
3119 -------------------------------
3121 -- QUALIFIED_EXPRESSION ::=
3122 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3124 -- The caller has scanned the name which is the Subtype_Mark parameter
3125 -- and scanned past the single quote following the subtype mark. The
3126 -- caller has not checked that this name is in fact appropriate for
3127 -- a subtype mark name (i.e. it is a selected component or identifier).
3129 -- Error_Recovery: cannot raise Error_Resync
3131 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
3132 Qual_Node : Node_Id;
3133 begin
3134 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
3135 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
3136 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
3137 return Qual_Node;
3138 end P_Qualified_Expression;
3140 --------------------
3141 -- 4.8 Allocator --
3142 --------------------
3144 -- ALLOCATOR ::=
3145 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3146 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3148 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3150 -- The caller has checked that the initial token is NEW
3152 -- Error recovery: can raise Error_Resync
3154 function P_Allocator return Node_Id is
3155 Alloc_Node : Node_Id;
3156 Type_Node : Node_Id;
3157 Null_Exclusion_Present : Boolean;
3159 begin
3160 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
3161 T_New;
3163 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3165 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3167 if Token = Tok_Left_Paren then
3168 Scan; -- past (
3169 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
3170 T_Right_Paren;
3172 Error_Msg_Ada_2012_Feature
3173 ("|subpool specification",
3174 Sloc (Subpool_Handle_Name (Alloc_Node)));
3175 end if;
3177 Null_Exclusion_Present := P_Null_Exclusion;
3178 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
3179 Type_Node := P_Subtype_Mark_Resync;
3181 if Token = Tok_Apostrophe then
3182 Scan; -- past apostrophe
3183 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
3184 else
3185 Set_Expression
3186 (Alloc_Node,
3187 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
3189 -- AI05-0104: An explicit null exclusion is not allowed for an
3190 -- allocator without initialization. In previous versions of the
3191 -- language it just raises constraint error.
3193 if Ada_Version >= Ada_2012 and then Null_Exclusion_Present then
3194 Error_Msg_N
3195 ("an allocator with a subtype indication "
3196 & "cannot have a null exclusion", Alloc_Node);
3197 end if;
3198 end if;
3200 return Alloc_Node;
3201 end P_Allocator;
3203 -----------------------
3204 -- P_Case_Expression --
3205 -----------------------
3207 function P_Case_Expression return Node_Id is
3208 Loc : constant Source_Ptr := Token_Ptr;
3209 Case_Node : Node_Id;
3210 Save_State : Saved_Scan_State;
3212 begin
3213 Error_Msg_Ada_2012_Feature ("|case expression", Token_Ptr);
3214 Scan; -- past CASE
3215 Case_Node :=
3216 Make_Case_Expression (Loc,
3217 Expression => P_Expression_No_Right_Paren,
3218 Alternatives => New_List);
3219 T_Is;
3221 -- We now have scanned out CASE expression IS, scan alternatives
3223 loop
3224 T_When;
3225 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
3227 -- Missing comma if WHEN (more alternatives present)
3229 if Token = Tok_When then
3230 T_Comma;
3232 -- A semicolon followed by "when" is probably meant to be a comma
3234 elsif Token = Tok_Semicolon then
3235 Save_Scan_State (Save_State);
3236 Scan; -- past the semicolon
3238 if Token /= Tok_When then
3239 Restore_Scan_State (Save_State);
3240 exit;
3241 end if;
3243 Error_Msg_SP -- CODEFIX
3244 ("|"";"" should be "",""");
3246 -- If comma/WHEN, skip comma and we have another alternative
3248 elsif Token = Tok_Comma then
3249 Save_Scan_State (Save_State);
3250 Scan; -- past comma
3252 if Token /= Tok_When then
3253 Restore_Scan_State (Save_State);
3254 exit;
3255 end if;
3257 -- If no comma or WHEN, definitely done
3259 else
3260 exit;
3261 end if;
3262 end loop;
3264 -- If we have an END CASE, diagnose as not needed
3266 if Token = Tok_End then
3267 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
3268 Scan; -- past END
3270 if Token = Tok_Case then
3271 Scan; -- past CASE;
3272 end if;
3273 end if;
3275 -- Return the Case_Expression node
3277 return Case_Node;
3278 end P_Case_Expression;
3280 -----------------------------------
3281 -- P_Case_Expression_Alternative --
3282 -----------------------------------
3284 -- CASE_STATEMENT_ALTERNATIVE ::=
3285 -- when DISCRETE_CHOICE_LIST =>
3286 -- EXPRESSION
3288 -- The caller has checked that and scanned past the initial WHEN token
3289 -- Error recovery: can raise Error_Resync
3291 function P_Case_Expression_Alternative return Node_Id is
3292 Case_Alt_Node : Node_Id;
3293 begin
3294 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
3295 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
3296 TF_Arrow;
3297 Set_Expression (Case_Alt_Node, P_Expression);
3298 return Case_Alt_Node;
3299 end P_Case_Expression_Alternative;
3301 --------------------------------------
3302 -- P_Iterated_Component_Association --
3303 --------------------------------------
3305 -- ITERATED_COMPONENT_ASSOCIATION ::=
3306 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3308 function P_Iterated_Component_Association return Node_Id is
3309 Assoc_Node : Node_Id;
3311 begin
3312 Scan; -- past FOR
3313 Assoc_Node :=
3314 New_Node (N_Iterated_Component_Association, Prev_Token_Ptr);
3315 Set_Defining_Identifier (Assoc_Node, P_Defining_Identifier);
3316 T_In;
3317 Set_Discrete_Choices (Assoc_Node, P_Discrete_Choice_List);
3318 TF_Arrow;
3319 Set_Expression (Assoc_Node, P_Expression);
3320 return Assoc_Node;
3321 end P_Iterated_Component_Association;
3323 ---------------------
3324 -- P_If_Expression --
3325 ---------------------
3327 -- IF_EXPRESSION ::=
3328 -- if CONDITION then DEPENDENT_EXPRESSION
3329 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3330 -- [else DEPENDENT_EXPRESSION]
3332 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3334 function P_If_Expression return Node_Id is
3335 function P_If_Expression_Internal
3336 (Loc : Source_Ptr;
3337 Cond : Node_Id) return Node_Id;
3338 -- This is the internal recursive routine that does all the work, it is
3339 -- recursive since it is used to process ELSIF parts, which internally
3340 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3341 -- sequence is like the outer function except that the caller passes
3342 -- the conditional expression (scanned using P_Expression), and the
3343 -- scan pointer points just past this expression. Loc points to the
3344 -- IF or ELSIF token.
3346 ------------------------------
3347 -- P_If_Expression_Internal --
3348 ------------------------------
3350 function P_If_Expression_Internal
3351 (Loc : Source_Ptr;
3352 Cond : Node_Id) return Node_Id
3354 Exprs : constant List_Id := New_List;
3355 Expr : Node_Id;
3356 State : Saved_Scan_State;
3357 Eptr : Source_Ptr;
3359 begin
3360 -- All cases except where we are at right paren
3362 if Token /= Tok_Right_Paren then
3363 TF_Then;
3364 Append_To (Exprs, P_Condition (Cond));
3365 Append_To (Exprs, P_Expression);
3367 -- Case of right paren (missing THEN phrase). Note that we know this
3368 -- is the IF case, since the caller dealt with this possibility in
3369 -- the ELSIF case.
3371 else
3372 Error_Msg_BC ("missing THEN phrase");
3373 Append_To (Exprs, P_Condition (Cond));
3374 end if;
3376 -- We now have scanned out IF expr THEN expr
3378 -- Check for common error of semicolon before the ELSE
3380 if Token = Tok_Semicolon then
3381 Save_Scan_State (State);
3382 Scan; -- past semicolon
3384 if Token = Tok_Else or else Token = Tok_Elsif then
3385 Error_Msg_SP -- CODEFIX
3386 ("|extra "";"" ignored");
3388 else
3389 Restore_Scan_State (State);
3390 end if;
3391 end if;
3393 -- Scan out ELSIF sequence if present
3395 if Token = Tok_Elsif then
3396 Eptr := Token_Ptr;
3397 Scan; -- past ELSIF
3398 Expr := P_Expression;
3400 -- If we are at a right paren, we assume the ELSIF should be ELSE
3402 if Token = Tok_Right_Paren then
3403 Error_Msg ("ELSIF should be ELSE", Eptr);
3404 Append_To (Exprs, Expr);
3406 -- Otherwise we have an OK ELSIF
3408 else
3409 Expr := P_If_Expression_Internal (Eptr, Expr);
3410 Set_Is_Elsif (Expr);
3411 Append_To (Exprs, Expr);
3412 end if;
3414 -- Scan out ELSE phrase if present
3416 elsif Token = Tok_Else then
3418 -- Scan out ELSE expression
3420 Scan; -- Past ELSE
3421 Append_To (Exprs, P_Expression);
3423 -- Skip redundant ELSE parts
3425 while Token = Tok_Else loop
3426 Error_Msg_SC ("only one ELSE part is allowed");
3427 Scan; -- past ELSE
3428 Discard_Junk_Node (P_Expression);
3429 end loop;
3431 -- Two expression case (implied True, filled in during semantics)
3433 else
3434 null;
3435 end if;
3437 -- If we have an END IF, diagnose as not needed
3439 if Token = Tok_End then
3440 Error_Msg_SC ("`END IF` not allowed at end of if expression");
3441 Scan; -- past END
3443 if Token = Tok_If then
3444 Scan; -- past IF;
3445 end if;
3446 end if;
3448 -- Return the If_Expression node
3450 return Make_If_Expression (Loc, Expressions => Exprs);
3451 end P_If_Expression_Internal;
3453 -- Local variables
3455 Loc : constant Source_Ptr := Token_Ptr;
3456 If_Expr : Node_Id;
3458 -- Start of processing for P_If_Expression
3460 begin
3461 Error_Msg_Ada_2012_Feature ("|if expression", Token_Ptr);
3462 Scan; -- past IF
3463 Inside_If_Expression := Inside_If_Expression + 1;
3464 If_Expr := P_If_Expression_Internal (Loc, P_Expression);
3465 Inside_If_Expression := Inside_If_Expression - 1;
3466 return If_Expr;
3467 end P_If_Expression;
3469 -----------------------
3470 -- P_Membership_Test --
3471 -----------------------
3473 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3474 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3476 procedure P_Membership_Test (N : Node_Id) is
3477 Alt : constant Node_Id :=
3478 P_Range_Or_Subtype_Mark
3479 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3481 begin
3482 -- Set case
3484 if Token = Tok_Vertical_Bar then
3485 Error_Msg_Ada_2012_Feature ("set notation", Token_Ptr);
3486 Set_Alternatives (N, New_List (Alt));
3487 Set_Right_Opnd (N, Empty);
3489 -- Loop to accumulate alternatives
3491 while Token = Tok_Vertical_Bar loop
3492 Scan; -- past vertical bar
3493 Append_To
3494 (Alternatives (N),
3495 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3496 end loop;
3498 -- Not set case
3500 else
3501 Set_Right_Opnd (N, Alt);
3502 Set_Alternatives (N, No_List);
3503 end if;
3504 end P_Membership_Test;
3506 ------------------------------------------
3507 -- P_Unparen_Cond_Case_Quant_Expression --
3508 ------------------------------------------
3510 function P_Unparen_Cond_Case_Quant_Expression return Node_Id is
3511 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3513 Result : Node_Id;
3514 Scan_State : Saved_Scan_State;
3516 begin
3517 -- Case expression
3519 if Token = Tok_Case then
3520 Result := P_Case_Expression;
3522 if not (Lparen and then Token = Tok_Right_Paren) then
3523 Error_Msg_N ("case expression must be parenthesized!", Result);
3524 end if;
3526 -- If expression
3528 elsif Token = Tok_If then
3529 Result := P_If_Expression;
3531 if not (Lparen and then Token = Tok_Right_Paren) then
3532 Error_Msg_N ("if expression must be parenthesized!", Result);
3533 end if;
3535 -- Quantified expression or iterated component association
3537 elsif Token = Tok_For then
3539 Save_Scan_State (Scan_State);
3540 Scan; -- past FOR
3542 if Token = Tok_All or else Token = Tok_Some then
3543 Restore_Scan_State (Scan_State);
3544 Result := P_Quantified_Expression;
3546 if not (Lparen and then Token = Tok_Right_Paren) then
3547 Error_Msg_N
3548 ("quantified expression must be parenthesized!", Result);
3549 end if;
3551 else
3552 -- If no quantifier keyword, this is an iterated component in
3553 -- an aggregate.
3555 Restore_Scan_State (Scan_State);
3556 Result := P_Iterated_Component_Association;
3557 end if;
3559 -- No other possibility should exist (caller was supposed to check)
3561 else
3562 raise Program_Error;
3563 end if;
3565 -- Return expression (possibly after having given message)
3567 return Result;
3568 end P_Unparen_Cond_Case_Quant_Expression;
3570 end Ch4;