Properly regenerate gcc/configure.
[official-gcc.git] / gcc / ada / par-ch4.adb
blob8f6da4eb4c34a9e379b38087d519ad8b97b88088
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-2014, 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
51 -- string or a type. For those attributes, a left parenthesis after
52 -- the attribute should not be analyzed as the beginning of a parameters
53 -- list because it may denote a slice operation (X'Img (1 .. 2)) or
54 -- a type conversion (X'Class (Y)). The Ada2012 attribute 'Old is in
55 -- this category.
57 -- Note: Loop_Entry is in this list because, although it can take an
58 -- optional argument (the loop name), we can't distinguish that at parse
59 -- time from the case where no loop name is given and a legitimate index
60 -- expression is present. So we parse the argument as an indexed component
61 -- and the semantic analysis sorts out this syntactic ambiguity based on
62 -- the type and form of the expression.
64 -- Note that this map designates the minimum set of attributes where a
65 -- construct in parentheses that is not an argument can appear right
66 -- after the attribute. For attributes like 'Size, we do not put them
67 -- in the map. If someone writes X'Size (3), that's illegal in any case,
68 -- but we get a better error message by parsing the (3) as an illegal
69 -- argument to the attribute, rather than some meaningless junk that
70 -- follows the attribute.
72 -----------------------
73 -- Local Subprograms --
74 -----------------------
76 function P_Aggregate_Or_Paren_Expr return Node_Id;
77 function P_Allocator return Node_Id;
78 function P_Case_Expression_Alternative 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
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 -- Note: syntactically a procedure call looks just like a function call,
184 -- so this routine is in practice used to scan out procedure calls as well.
186 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
188 -- Error recovery: can raise Error_Resync
190 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
191 -- followed by either a left paren (qualified expression case), or by
192 -- range (range attribute case). All other uses of apostrophe (i.e. all
193 -- other attributes) are handled in this routine.
195 -- Error recovery: can raise Error_Resync
197 function P_Name return Node_Id is
198 Scan_State : Saved_Scan_State;
199 Name_Node : Node_Id;
200 Prefix_Node : Node_Id;
201 Ident_Node : Node_Id;
202 Expr_Node : Node_Id;
203 Range_Node : Node_Id;
204 Arg_Node : Node_Id;
206 Arg_List : List_Id := No_List; -- kill junk warning
207 Attr_Name : Name_Id := No_Name; -- kill junk warning
209 begin
210 -- Case of not a name
212 if Token not in Token_Class_Name then
214 -- If it looks like start of expression, complain and scan expression
216 if Token in Token_Class_Literal
217 or else Token = Tok_Left_Paren
218 then
219 Error_Msg_SC ("name expected");
220 return P_Expression;
222 -- Otherwise some other junk, not much we can do
224 else
225 Error_Msg_AP ("name expected");
226 raise Error_Resync;
227 end if;
228 end if;
230 -- Loop through designators in qualified name
232 Name_Node := Token_Node;
234 loop
235 Scan; -- past designator
236 exit when Token /= Tok_Dot;
237 Save_Scan_State (Scan_State); -- at dot
238 Scan; -- past dot
240 -- If we do not have another designator after the dot, then join
241 -- the normal circuit to handle a dot extension (may be .all or
242 -- character literal case). Otherwise loop back to scan the next
243 -- designator.
245 if Token not in Token_Class_Desig then
246 goto Scan_Name_Extension_Dot;
247 else
248 Prefix_Node := Name_Node;
249 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
250 Set_Prefix (Name_Node, Prefix_Node);
251 Set_Selector_Name (Name_Node, Token_Node);
252 end if;
253 end loop;
255 -- We have now scanned out a qualified designator. If the last token is
256 -- an operator symbol, then we certainly do not have the Snam case, so
257 -- we can just use the normal name extension check circuit
259 if Prev_Token = Tok_Operator_Symbol then
260 goto Scan_Name_Extension;
261 end if;
263 -- We have scanned out a qualified simple name, check for name extension
264 -- Note that we know there is no dot here at this stage, so the only
265 -- possible cases of name extension are apostrophe and left paren.
267 if Token = Tok_Apostrophe then
268 Save_Scan_State (Scan_State); -- at apostrophe
269 Scan; -- past apostrophe
271 -- Qualified expression in Ada 2012 mode (treated as a name)
273 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
274 goto Scan_Name_Extension_Apostrophe;
276 -- If left paren not in Ada 2012, then it is not part of the name,
277 -- since qualified expressions are not names in prior versions of
278 -- Ada, so return with Token backed up to point to the apostrophe.
279 -- The treatment for the range attribute is similar (we do not
280 -- consider x'range to be a name in this grammar).
282 elsif Token = Tok_Left_Paren or else Token = Tok_Range then
283 Restore_Scan_State (Scan_State); -- to apostrophe
284 Expr_Form := EF_Simple_Name;
285 return Name_Node;
287 -- Otherwise we have the case of a name extended by an attribute
289 else
290 goto Scan_Name_Extension_Apostrophe;
291 end if;
293 -- Check case of qualified simple name extended by a left parenthesis
295 elsif Token = Tok_Left_Paren then
296 Scan; -- past left paren
297 goto Scan_Name_Extension_Left_Paren;
299 -- Otherwise the qualified simple name is not extended, so return
301 else
302 Expr_Form := EF_Simple_Name;
303 return Name_Node;
304 end if;
306 -- Loop scanning past name extensions. A label is used for control
307 -- transfer for this loop for ease of interfacing with the finite state
308 -- machine in the parenthesis scanning circuit, and also to allow for
309 -- passing in control to the appropriate point from the above code.
311 <<Scan_Name_Extension>>
313 -- Character literal used as name cannot be extended. Also this
314 -- cannot be a call, since the name for a call must be a designator.
315 -- Return in these cases, or if there is no name extension
317 if Token not in Token_Class_Namext
318 or else Prev_Token = Tok_Char_Literal
319 then
320 Expr_Form := EF_Name;
321 return Name_Node;
322 end if;
324 -- Merge here when we know there is a name extension
326 <<Scan_Name_Extension_OK>>
328 if Token = Tok_Left_Paren then
329 Scan; -- past left paren
330 goto Scan_Name_Extension_Left_Paren;
332 elsif Token = Tok_Apostrophe then
333 Save_Scan_State (Scan_State); -- at apostrophe
334 Scan; -- past apostrophe
335 goto Scan_Name_Extension_Apostrophe;
337 else -- Token = Tok_Dot
338 Save_Scan_State (Scan_State); -- at dot
339 Scan; -- past dot
340 goto Scan_Name_Extension_Dot;
341 end if;
343 -- Case of name extended by dot (selection), dot is already skipped
344 -- and the scan state at the point of the dot is saved in Scan_State.
346 <<Scan_Name_Extension_Dot>>
348 -- Explicit dereference case
350 if Token = Tok_All then
351 Prefix_Node := Name_Node;
352 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
353 Set_Prefix (Name_Node, Prefix_Node);
354 Scan; -- past ALL
355 goto Scan_Name_Extension;
357 -- Selected component case
359 elsif Token in Token_Class_Name then
360 Prefix_Node := Name_Node;
361 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
362 Set_Prefix (Name_Node, Prefix_Node);
363 Set_Selector_Name (Name_Node, Token_Node);
364 Scan; -- past selector
365 goto Scan_Name_Extension;
367 -- Reserved identifier as selector
369 elsif Is_Reserved_Identifier then
370 Scan_Reserved_Identifier (Force_Msg => False);
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 identifier used as selector
376 goto Scan_Name_Extension;
378 -- If dot is at end of line and followed by nothing legal,
379 -- then assume end of name and quit (dot will be taken as
380 -- an incorrect form of some other punctuation by our caller).
382 elsif Token_Is_At_Start_Of_Line then
383 Restore_Scan_State (Scan_State);
384 return Name_Node;
386 -- Here if nothing legal after the dot
388 else
389 Error_Msg_AP ("selector expected");
390 raise Error_Resync;
391 end if;
393 -- Here for an apostrophe as name extension. The scan position at the
394 -- apostrophe has already been saved, and the apostrophe scanned out.
396 <<Scan_Name_Extension_Apostrophe>>
398 Scan_Apostrophe : declare
399 function Apostrophe_Should_Be_Semicolon return Boolean;
400 -- Checks for case where apostrophe should probably be
401 -- a semicolon, and if so, gives appropriate message,
402 -- resets the scan pointer to the apostrophe, changes
403 -- the current token to Tok_Semicolon, and returns True.
404 -- Otherwise returns False.
406 ------------------------------------
407 -- Apostrophe_Should_Be_Semicolon --
408 ------------------------------------
410 function Apostrophe_Should_Be_Semicolon return Boolean is
411 begin
412 if Token_Is_At_Start_Of_Line then
413 Restore_Scan_State (Scan_State); -- to apostrophe
414 Error_Msg_SC ("|""''"" should be "";""");
415 Token := Tok_Semicolon;
416 return True;
417 else
418 return False;
419 end if;
420 end Apostrophe_Should_Be_Semicolon;
422 -- Start of processing for Scan_Apostrophe
424 begin
425 -- Check for qualified expression case in Ada 2012 mode
427 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
428 Name_Node := P_Qualified_Expression (Name_Node);
429 goto Scan_Name_Extension;
431 -- If range attribute after apostrophe, then return with Token
432 -- pointing to the apostrophe. Note that in this case the prefix
433 -- need not be a simple name (cases like A.all'range). Similarly
434 -- if there is a left paren after the apostrophe, then we also
435 -- return with Token pointing to the apostrophe (this is the
436 -- aggregate case, or some error case).
438 elsif Token = Tok_Range or else Token = Tok_Left_Paren then
439 Restore_Scan_State (Scan_State); -- to apostrophe
440 Expr_Form := EF_Name;
441 return Name_Node;
443 -- Here for cases where attribute designator is an identifier
445 elsif Token = Tok_Identifier then
446 Attr_Name := Token_Name;
448 if not Is_Attribute_Name (Attr_Name) then
449 if Apostrophe_Should_Be_Semicolon then
450 Expr_Form := EF_Name;
451 return Name_Node;
453 -- Here for a bad attribute name
455 else
456 Signal_Bad_Attribute;
457 Scan; -- past bad identifier
459 if Token = Tok_Left_Paren then
460 Scan; -- past left paren
462 loop
463 Discard_Junk_Node (P_Expression_If_OK);
464 exit when not Comma_Present;
465 end loop;
467 T_Right_Paren;
468 end if;
470 return Error;
471 end if;
472 end if;
474 if Style_Check then
475 Style.Check_Attribute_Name (False);
476 end if;
478 -- Here for case of attribute designator is not an identifier
480 else
481 if Token = Tok_Delta then
482 Attr_Name := Name_Delta;
484 elsif Token = Tok_Digits then
485 Attr_Name := Name_Digits;
487 elsif Token = Tok_Access then
488 Attr_Name := Name_Access;
490 elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
491 Attr_Name := Name_Mod;
493 elsif Apostrophe_Should_Be_Semicolon then
494 Expr_Form := EF_Name;
495 return Name_Node;
497 else
498 Error_Msg_AP ("attribute designator expected");
499 raise Error_Resync;
500 end if;
502 if Style_Check then
503 Style.Check_Attribute_Name (True);
504 end if;
505 end if;
507 -- We come here with an OK attribute scanned, and corresponding
508 -- Attribute identifier node stored in Ident_Node.
510 Prefix_Node := Name_Node;
511 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
512 Scan; -- past attribute designator
513 Set_Prefix (Name_Node, Prefix_Node);
514 Set_Attribute_Name (Name_Node, Attr_Name);
516 -- Scan attribute arguments/designator. We skip this if we know
517 -- that the attribute cannot have an argument (see documentation
518 -- of Is_Parameterless_Attribute for further details).
520 if Token = Tok_Left_Paren
521 and then not
522 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
523 then
524 -- Attribute Update contains an array or record association
525 -- list which provides new values for various components or
526 -- elements. The list is parsed as an aggregate, and we get
527 -- better error handling by knowing that in the parser.
529 if Attr_Name = Name_Update then
530 Set_Expressions (Name_Node, New_List);
531 Append (P_Aggregate, Expressions (Name_Node));
533 -- All other cases of parsing attribute arguments
535 else
536 Set_Expressions (Name_Node, New_List);
537 Scan; -- past left paren
539 loop
540 declare
541 Expr : constant Node_Id := P_Expression_If_OK;
542 Rnam : Node_Id;
544 begin
545 -- Case of => for named notation
547 if Token = Tok_Arrow then
549 -- Named notation allowed only for the special
550 -- case of System'Restriction_Set (No_Dependence =>
551 -- unit_NAME), in which case construct a parameter
552 -- assocation node and append to the arguments.
554 if Attr_Name = Name_Restriction_Set
555 and then Nkind (Expr) = N_Identifier
556 and then Chars (Expr) = Name_No_Dependence
557 then
558 Scan; -- past arrow
559 Rnam := P_Name;
560 Append_To (Expressions (Name_Node),
561 Make_Parameter_Association (Sloc (Rnam),
562 Selector_Name => Expr,
563 Explicit_Actual_Parameter => Rnam));
564 exit;
566 -- For all other cases named notation is illegal
568 else
569 Error_Msg_SC
570 ("named parameters not permitted "
571 & "for attributes");
572 Scan; -- past junk arrow
573 end if;
575 -- Here for normal case (not => for named parameter)
577 else
578 Append (Expr, Expressions (Name_Node));
579 exit when not Comma_Present;
580 end if;
581 end;
582 end loop;
584 T_Right_Paren;
585 end if;
586 end if;
588 goto Scan_Name_Extension;
589 end Scan_Apostrophe;
591 -- Here for left parenthesis extending name (left paren skipped)
593 <<Scan_Name_Extension_Left_Paren>>
595 -- We now have to scan through a list of items, terminated by a
596 -- right parenthesis. The scan is handled by a finite state
597 -- machine. The possibilities are:
599 -- (discrete_range)
601 -- This is a slice. This case is handled in LP_State_Init
603 -- (expression, expression, ..)
605 -- This is interpreted as an indexed component, i.e. as a
606 -- case of a name which can be extended in the normal manner.
607 -- This case is handled by LP_State_Name or LP_State_Expr.
609 -- Note: if and case expressions (without an extra level of
610 -- parentheses) are permitted in this context).
612 -- (..., identifier => expression , ...)
614 -- If there is at least one occurrence of identifier => (but
615 -- none of the other cases apply), then we have a call.
617 -- Test for Id => case
619 if Token = Tok_Identifier then
620 Save_Scan_State (Scan_State); -- at Id
621 Scan; -- past Id
623 -- Test for => (allow := as an error substitute)
625 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
626 Restore_Scan_State (Scan_State); -- to Id
627 Arg_List := New_List;
628 goto LP_State_Call;
630 else
631 Restore_Scan_State (Scan_State); -- to Id
632 end if;
633 end if;
635 -- Here we have an expression after all
637 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
639 -- Check cases of discrete range for a slice
641 -- First possibility: Range_Attribute_Reference
643 if Expr_Form = EF_Range_Attr then
644 Range_Node := Expr_Node;
646 -- Second possibility: Simple_expression .. Simple_expression
648 elsif Token = Tok_Dot_Dot then
649 Check_Simple_Expression (Expr_Node);
650 Range_Node := New_Node (N_Range, Token_Ptr);
651 Set_Low_Bound (Range_Node, Expr_Node);
652 Scan; -- past ..
653 Expr_Node := P_Expression;
654 Check_Simple_Expression (Expr_Node);
655 Set_High_Bound (Range_Node, Expr_Node);
657 -- Third possibility: Type_name range Range
659 elsif Token = Tok_Range then
660 if Expr_Form /= EF_Simple_Name then
661 Error_Msg_SC ("subtype mark must precede RANGE");
662 raise Error_Resync;
663 end if;
665 Range_Node := P_Subtype_Indication (Expr_Node);
667 -- Otherwise we just have an expression. It is true that we might
668 -- have a subtype mark without a range constraint but this case
669 -- is syntactically indistinguishable from the expression case.
671 else
672 Arg_List := New_List;
673 goto LP_State_Expr;
674 end if;
676 -- Fall through here with unmistakable Discrete range scanned,
677 -- which means that we definitely have the case of a slice. The
678 -- Discrete range is in Range_Node.
680 if Token = Tok_Comma then
681 Error_Msg_SC ("slice cannot have more than one dimension");
682 raise Error_Resync;
684 elsif Token /= Tok_Right_Paren then
685 if Token = Tok_Arrow then
687 -- This may be an aggregate that is missing a qualification
689 Error_Msg_SC
690 ("context of aggregate must be a qualified expression");
691 raise Error_Resync;
693 else
694 T_Right_Paren;
695 raise Error_Resync;
696 end if;
698 else
699 Scan; -- past right paren
700 Prefix_Node := Name_Node;
701 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
702 Set_Prefix (Name_Node, Prefix_Node);
703 Set_Discrete_Range (Name_Node, Range_Node);
705 -- An operator node is legal as a prefix to other names,
706 -- but not for a slice.
708 if Nkind (Prefix_Node) = N_Operator_Symbol then
709 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
710 end if;
712 -- If we have a name extension, go scan it
714 if Token in Token_Class_Namext then
715 goto Scan_Name_Extension_OK;
717 -- Otherwise return (a slice is a name, but is not a call)
719 else
720 Expr_Form := EF_Name;
721 return Name_Node;
722 end if;
723 end if;
725 -- In LP_State_Expr, we have scanned one or more expressions, and
726 -- so we have a call or an indexed component which is a name. On
727 -- entry we have the expression just scanned in Expr_Node and
728 -- Arg_List contains the list of expressions encountered so far
730 <<LP_State_Expr>>
731 Append (Expr_Node, Arg_List);
733 if Token = Tok_Arrow then
734 Error_Msg
735 ("expect identifier in parameter association", Sloc (Expr_Node));
736 Scan; -- past arrow
738 elsif not Comma_Present then
739 T_Right_Paren;
741 Prefix_Node := Name_Node;
742 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
743 Set_Prefix (Name_Node, Prefix_Node);
744 Set_Expressions (Name_Node, Arg_List);
746 goto Scan_Name_Extension;
747 end if;
749 -- Comma present (and scanned out), test for identifier => case
750 -- Test for identifier => case
752 if Token = Tok_Identifier then
753 Save_Scan_State (Scan_State); -- at Id
754 Scan; -- past Id
756 -- Test for => (allow := as error substitute)
758 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
759 Restore_Scan_State (Scan_State); -- to Id
760 goto LP_State_Call;
762 -- Otherwise it's just an expression after all, so backup
764 else
765 Restore_Scan_State (Scan_State); -- to Id
766 end if;
767 end if;
769 -- Here we have an expression after all, so stay in this state
771 Expr_Node := P_Expression_If_OK;
772 goto LP_State_Expr;
774 -- LP_State_Call corresponds to the situation in which at least one
775 -- instance of Id => Expression has been encountered, so we know that
776 -- we do not have a name, but rather a call. We enter it with the
777 -- scan pointer pointing to the next argument to scan, and Arg_List
778 -- containing the list of arguments scanned so far.
780 <<LP_State_Call>>
782 -- Test for case of Id => Expression (named parameter)
784 if Token = Tok_Identifier then
785 Save_Scan_State (Scan_State); -- at Id
786 Ident_Node := Token_Node;
787 Scan; -- past Id
789 -- Deal with => (allow := as incorrect substitute)
791 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
792 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
793 Set_Selector_Name (Arg_Node, Ident_Node);
794 T_Arrow;
795 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
796 Append (Arg_Node, Arg_List);
798 -- If a comma follows, go back and scan next entry
800 if Comma_Present then
801 goto LP_State_Call;
803 -- Otherwise we have the end of a call
805 else
806 Prefix_Node := Name_Node;
807 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
808 Set_Name (Name_Node, Prefix_Node);
809 Set_Parameter_Associations (Name_Node, Arg_List);
810 T_Right_Paren;
812 if Token in Token_Class_Namext then
813 goto Scan_Name_Extension_OK;
815 -- This is a case of a call which cannot be a name
817 else
818 Expr_Form := EF_Name;
819 return Name_Node;
820 end if;
821 end if;
823 -- Not named parameter: Id started an expression after all
825 else
826 Restore_Scan_State (Scan_State); -- to Id
827 end if;
828 end if;
830 -- Here if entry did not start with Id => which means that it
831 -- is a positional parameter, which is not allowed, since we
832 -- have seen at least one named parameter already.
834 Error_Msg_SC
835 ("positional parameter association " &
836 "not allowed after named one");
838 Expr_Node := P_Expression_If_OK;
840 -- Leaving the '>' in an association is not unusual, so suggest
841 -- a possible fix.
843 if Nkind (Expr_Node) = N_Op_Eq then
844 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
845 end if;
847 -- We go back to scanning out expressions, so that we do not get
848 -- multiple error messages when several positional parameters
849 -- follow a named parameter.
851 goto LP_State_Expr;
853 -- End of treatment for name extensions starting with left paren
855 -- End of loop through name extensions
857 end P_Name;
859 -- This function parses a restricted form of Names which are either
860 -- designators, or designators preceded by a sequence of prefixes
861 -- that are direct names.
863 -- Error recovery: cannot raise Error_Resync
865 function P_Function_Name return Node_Id is
866 Designator_Node : Node_Id;
867 Prefix_Node : Node_Id;
868 Selector_Node : Node_Id;
869 Dot_Sloc : Source_Ptr := No_Location;
871 begin
872 -- Prefix_Node is set to the gathered prefix so far, Empty means that
873 -- no prefix has been scanned. This allows us to build up the result
874 -- in the required right recursive manner.
876 Prefix_Node := Empty;
878 -- Loop through prefixes
880 loop
881 Designator_Node := Token_Node;
883 if Token not in Token_Class_Desig then
884 return P_Identifier; -- let P_Identifier issue the error message
886 else -- Token in Token_Class_Desig
887 Scan; -- past designator
888 exit when Token /= Tok_Dot;
889 end if;
891 -- Here at a dot, with token just before it in Designator_Node
893 if No (Prefix_Node) then
894 Prefix_Node := Designator_Node;
895 else
896 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
897 Set_Prefix (Selector_Node, Prefix_Node);
898 Set_Selector_Name (Selector_Node, Designator_Node);
899 Prefix_Node := Selector_Node;
900 end if;
902 Dot_Sloc := Token_Ptr;
903 Scan; -- past dot
904 end loop;
906 -- Fall out of the loop having just scanned a designator
908 if No (Prefix_Node) then
909 return Designator_Node;
910 else
911 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
912 Set_Prefix (Selector_Node, Prefix_Node);
913 Set_Selector_Name (Selector_Node, Designator_Node);
914 return Selector_Node;
915 end if;
917 exception
918 when Error_Resync =>
919 return Error;
920 end P_Function_Name;
922 -- This function parses a restricted form of Names which are either
923 -- identifiers, or identifiers preceded by a sequence of prefixes
924 -- that are direct names.
926 -- Error recovery: cannot raise Error_Resync
928 function P_Qualified_Simple_Name return Node_Id is
929 Designator_Node : Node_Id;
930 Prefix_Node : Node_Id;
931 Selector_Node : Node_Id;
932 Dot_Sloc : Source_Ptr := No_Location;
934 begin
935 -- Prefix node is set to the gathered prefix so far, Empty means that
936 -- no prefix has been scanned. This allows us to build up the result
937 -- in the required right recursive manner.
939 Prefix_Node := Empty;
941 -- Loop through prefixes
943 loop
944 Designator_Node := Token_Node;
946 if Token = Tok_Identifier then
947 Scan; -- past identifier
948 exit when Token /= Tok_Dot;
950 elsif Token not in Token_Class_Desig then
951 return P_Identifier; -- let P_Identifier issue the error message
953 else
954 Scan; -- past designator
956 if Token /= Tok_Dot then
957 Error_Msg_SP ("identifier expected");
958 return Error;
959 end if;
960 end if;
962 -- Here at a dot, with token just before it in Designator_Node
964 if No (Prefix_Node) then
965 Prefix_Node := Designator_Node;
966 else
967 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
968 Set_Prefix (Selector_Node, Prefix_Node);
969 Set_Selector_Name (Selector_Node, Designator_Node);
970 Prefix_Node := Selector_Node;
971 end if;
973 Dot_Sloc := Token_Ptr;
974 Scan; -- past dot
975 end loop;
977 -- Fall out of the loop having just scanned an identifier
979 if No (Prefix_Node) then
980 return Designator_Node;
981 else
982 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
983 Set_Prefix (Selector_Node, Prefix_Node);
984 Set_Selector_Name (Selector_Node, Designator_Node);
985 return Selector_Node;
986 end if;
988 exception
989 when Error_Resync =>
990 return Error;
991 end P_Qualified_Simple_Name;
993 -- This procedure differs from P_Qualified_Simple_Name only in that it
994 -- raises Error_Resync if any error is encountered. It only returns after
995 -- scanning a valid qualified simple name.
997 -- Error recovery: can raise Error_Resync
999 function P_Qualified_Simple_Name_Resync return Node_Id is
1000 Designator_Node : Node_Id;
1001 Prefix_Node : Node_Id;
1002 Selector_Node : Node_Id;
1003 Dot_Sloc : Source_Ptr := No_Location;
1005 begin
1006 Prefix_Node := Empty;
1008 -- Loop through prefixes
1010 loop
1011 Designator_Node := Token_Node;
1013 if Token = Tok_Identifier then
1014 Scan; -- past identifier
1015 exit when Token /= Tok_Dot;
1017 elsif Token not in Token_Class_Desig then
1018 Discard_Junk_Node (P_Identifier); -- to issue the error message
1019 raise Error_Resync;
1021 else
1022 Scan; -- past designator
1024 if Token /= Tok_Dot then
1025 Error_Msg_SP ("identifier expected");
1026 raise Error_Resync;
1027 end if;
1028 end if;
1030 -- Here at a dot, with token just before it in Designator_Node
1032 if No (Prefix_Node) then
1033 Prefix_Node := Designator_Node;
1034 else
1035 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1036 Set_Prefix (Selector_Node, Prefix_Node);
1037 Set_Selector_Name (Selector_Node, Designator_Node);
1038 Prefix_Node := Selector_Node;
1039 end if;
1041 Dot_Sloc := Token_Ptr;
1042 Scan; -- past period
1043 end loop;
1045 -- Fall out of the loop having just scanned an identifier
1047 if No (Prefix_Node) then
1048 return Designator_Node;
1049 else
1050 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1051 Set_Prefix (Selector_Node, Prefix_Node);
1052 Set_Selector_Name (Selector_Node, Designator_Node);
1053 return Selector_Node;
1054 end if;
1055 end P_Qualified_Simple_Name_Resync;
1057 ----------------------
1058 -- 4.1 Direct_Name --
1059 ----------------------
1061 -- Parsed by P_Name and other functions in section 4.1
1063 -----------------
1064 -- 4.1 Prefix --
1065 -----------------
1067 -- Parsed by P_Name (4.1)
1069 -------------------------------
1070 -- 4.1 Explicit Dereference --
1071 -------------------------------
1073 -- Parsed by P_Name (4.1)
1075 -------------------------------
1076 -- 4.1 Implicit_Dereference --
1077 -------------------------------
1079 -- Parsed by P_Name (4.1)
1081 ----------------------------
1082 -- 4.1 Indexed Component --
1083 ----------------------------
1085 -- Parsed by P_Name (4.1)
1087 ----------------
1088 -- 4.1 Slice --
1089 ----------------
1091 -- Parsed by P_Name (4.1)
1093 -----------------------------
1094 -- 4.1 Selected_Component --
1095 -----------------------------
1097 -- Parsed by P_Name (4.1)
1099 ------------------------
1100 -- 4.1 Selector Name --
1101 ------------------------
1103 -- Parsed by P_Name (4.1)
1105 ------------------------------
1106 -- 4.1 Attribute Reference --
1107 ------------------------------
1109 -- Parsed by P_Name (4.1)
1111 -------------------------------
1112 -- 4.1 Attribute Designator --
1113 -------------------------------
1115 -- Parsed by P_Name (4.1)
1117 --------------------------------------
1118 -- 4.1.4 Range Attribute Reference --
1119 --------------------------------------
1121 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1123 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1125 -- In the grammar, a RANGE attribute is simply a name, but its use is
1126 -- highly restricted, so in the parser, we do not regard it as a name.
1127 -- Instead, P_Name returns without scanning the 'RANGE part of the
1128 -- attribute, and the caller uses the following function to construct
1129 -- a range attribute in places where it is appropriate.
1131 -- Note that RANGE here is treated essentially as an identifier,
1132 -- rather than a reserved word.
1134 -- The caller has parsed the prefix, i.e. a name, and Token points to
1135 -- the apostrophe. The token after the apostrophe is known to be RANGE
1136 -- at this point. The prefix node becomes the prefix of the attribute.
1138 -- Error_Recovery: Cannot raise Error_Resync
1140 function P_Range_Attribute_Reference
1141 (Prefix_Node : Node_Id)
1142 return Node_Id
1144 Attr_Node : Node_Id;
1146 begin
1147 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1148 Set_Prefix (Attr_Node, Prefix_Node);
1149 Scan; -- past apostrophe
1151 if Style_Check then
1152 Style.Check_Attribute_Name (True);
1153 end if;
1155 Set_Attribute_Name (Attr_Node, Name_Range);
1156 Scan; -- past RANGE
1158 if Token = Tok_Left_Paren then
1159 Scan; -- past left paren
1160 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1161 T_Right_Paren;
1162 end if;
1164 return Attr_Node;
1165 end P_Range_Attribute_Reference;
1167 ---------------------------------------
1168 -- 4.1.4 Range Attribute Designator --
1169 ---------------------------------------
1171 -- Parsed by P_Range_Attribute_Reference (4.4)
1173 --------------------
1174 -- 4.3 Aggregate --
1175 --------------------
1177 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1179 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1180 -- an aggregate is known to be required (code statement, extension
1181 -- aggregate), in which cases this routine performs the necessary check
1182 -- that we have an aggregate rather than a parenthesized expression
1184 -- Error recovery: can raise Error_Resync
1186 function P_Aggregate return Node_Id is
1187 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1188 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1190 begin
1191 if Nkind (Aggr_Node) /= N_Aggregate
1192 and then
1193 Nkind (Aggr_Node) /= N_Extension_Aggregate
1194 then
1195 Error_Msg
1196 ("aggregate may not have single positional component", Aggr_Sloc);
1197 return Error;
1198 else
1199 return Aggr_Node;
1200 end if;
1201 end P_Aggregate;
1203 ------------------------------------------------
1204 -- 4.3 Aggregate or Parenthesized Expression --
1205 ------------------------------------------------
1207 -- This procedure parses out either an aggregate or a parenthesized
1208 -- expression (these two constructs are closely related, since a
1209 -- parenthesized expression looks like an aggregate with a single
1210 -- positional component).
1212 -- AGGREGATE ::=
1213 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1215 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1217 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1218 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1219 -- | null record
1221 -- RECORD_COMPONENT_ASSOCIATION ::=
1222 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1224 -- COMPONENT_CHOICE_LIST ::=
1225 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1226 -- | others
1228 -- EXTENSION_AGGREGATE ::=
1229 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1231 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1233 -- ARRAY_AGGREGATE ::=
1234 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1236 -- POSITIONAL_ARRAY_AGGREGATE ::=
1237 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1238 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1239 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1241 -- NAMED_ARRAY_AGGREGATE ::=
1242 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1244 -- PRIMARY ::= (EXPRESSION);
1246 -- Error recovery: can raise Error_Resync
1248 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1249 -- to Ada 2005 limited aggregates (AI-287)
1251 function P_Aggregate_Or_Paren_Expr return Node_Id is
1252 Aggregate_Node : Node_Id;
1253 Expr_List : List_Id;
1254 Assoc_List : List_Id;
1255 Expr_Node : Node_Id;
1256 Lparen_Sloc : Source_Ptr;
1257 Scan_State : Saved_Scan_State;
1259 procedure Box_Error;
1260 -- Called if <> is encountered as positional aggregate element. Issues
1261 -- error message and sets Expr_Node to Error.
1263 ---------------
1264 -- Box_Error --
1265 ---------------
1267 procedure Box_Error is
1268 begin
1269 if Ada_Version < Ada_2005 then
1270 Error_Msg_SC ("box in aggregate is an Ada 2005 extension");
1271 end if;
1273 -- Ada 2005 (AI-287): The box notation is allowed only with named
1274 -- notation because positional notation might be error prone. For
1275 -- example, in "(X, <>, Y, <>)", there is no type associated with
1276 -- the boxes, so you might not be leaving out the components you
1277 -- thought you were leaving out.
1279 Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1280 Scan; -- past box
1281 Expr_Node := Error;
1282 end Box_Error;
1284 -- Start of processing for P_Aggregate_Or_Paren_Expr
1286 begin
1287 Lparen_Sloc := Token_Ptr;
1288 T_Left_Paren;
1290 -- Note on parentheses count. For cases like an if expression, the
1291 -- parens here really count as real parentheses for the paren count,
1292 -- so we adjust the paren count accordingly after scanning the expr.
1294 -- If expression
1296 if Token = Tok_If then
1297 Expr_Node := P_If_Expression;
1298 T_Right_Paren;
1299 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1300 return Expr_Node;
1302 -- Case expression
1304 elsif Token = Tok_Case then
1305 Expr_Node := P_Case_Expression;
1306 T_Right_Paren;
1307 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1308 return Expr_Node;
1310 -- Quantified expression
1312 elsif Token = Tok_For then
1313 Expr_Node := P_Quantified_Expression;
1314 T_Right_Paren;
1315 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1316 return Expr_Node;
1318 -- Note: the mechanism used here of rescanning the initial expression
1319 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1320 -- out the discrete choice list.
1322 -- Deal with expression and extension aggregates first
1324 elsif Token /= Tok_Others then
1325 Save_Scan_State (Scan_State); -- at start of expression
1327 -- Deal with (NULL RECORD)
1329 if Token = Tok_Null then
1330 Scan; -- past NULL
1332 if Token = Tok_Record then
1333 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1334 Set_Null_Record_Present (Aggregate_Node, True);
1335 Scan; -- past RECORD
1336 T_Right_Paren;
1337 return Aggregate_Node;
1338 else
1339 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1340 end if;
1341 end if;
1343 -- Scan expression, handling box appearing as positional argument
1345 if Token = Tok_Box then
1346 Box_Error;
1347 else
1348 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1349 end if;
1351 -- Extension aggregate
1353 if Token = Tok_With then
1354 if Nkind (Expr_Node) = N_Attribute_Reference
1355 and then Attribute_Name (Expr_Node) = Name_Range
1356 then
1357 Bad_Range_Attribute (Sloc (Expr_Node));
1358 return Error;
1359 end if;
1361 if Ada_Version = Ada_83 then
1362 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1363 end if;
1365 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1366 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1367 Scan; -- past WITH
1369 -- Deal with WITH NULL RECORD case
1371 if Token = Tok_Null then
1372 Save_Scan_State (Scan_State); -- at NULL
1373 Scan; -- past NULL
1375 if Token = Tok_Record then
1376 Scan; -- past RECORD
1377 Set_Null_Record_Present (Aggregate_Node, True);
1378 T_Right_Paren;
1379 return Aggregate_Node;
1381 else
1382 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1383 end if;
1384 end if;
1386 if Token /= Tok_Others then
1387 Save_Scan_State (Scan_State);
1388 Expr_Node := P_Expression;
1389 else
1390 Expr_Node := Empty;
1391 end if;
1393 -- Expression
1395 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1396 if Nkind (Expr_Node) = N_Attribute_Reference
1397 and then Attribute_Name (Expr_Node) = Name_Range
1398 then
1399 Error_Msg
1400 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1401 Scan; -- past right paren
1402 return Expr_Node;
1403 end if;
1405 -- Bump paren count of expression
1407 if Expr_Node /= Error then
1408 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1409 end if;
1411 T_Right_Paren; -- past right paren (error message if none)
1412 return Expr_Node;
1414 -- Normal aggregate
1416 else
1417 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1418 end if;
1420 -- Others
1422 else
1423 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1424 Expr_Node := Empty;
1425 end if;
1427 -- Prepare to scan list of component associations
1429 Expr_List := No_List; -- don't set yet, maybe all named entries
1430 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1432 -- This loop scans through component associations. On entry to the
1433 -- loop, an expression has been scanned at the start of the current
1434 -- association unless initial token was OTHERS, in which case
1435 -- Expr_Node is set to Empty.
1437 loop
1438 -- Deal with others association first. This is a named association
1440 if No (Expr_Node) then
1441 if No (Assoc_List) then
1442 Assoc_List := New_List;
1443 end if;
1445 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1447 -- Improper use of WITH
1449 elsif Token = Tok_With then
1450 Error_Msg_SC ("WITH must be preceded by single expression in " &
1451 "extension aggregate");
1452 raise Error_Resync;
1454 -- Range attribute can only appear as part of a discrete choice list
1456 elsif Nkind (Expr_Node) = N_Attribute_Reference
1457 and then Attribute_Name (Expr_Node) = Name_Range
1458 and then Token /= Tok_Arrow
1459 and then Token /= Tok_Vertical_Bar
1460 then
1461 Bad_Range_Attribute (Sloc (Expr_Node));
1462 return Error;
1464 -- Assume positional case if comma, right paren, or literal or
1465 -- identifier or OTHERS follows (the latter cases are missing
1466 -- comma cases). Also assume positional if a semicolon follows,
1467 -- which can happen if there are missing parens
1469 elsif Token = Tok_Comma
1470 or else Token = Tok_Right_Paren
1471 or else Token = Tok_Others
1472 or else Token in Token_Class_Lit_Or_Name
1473 or else Token = Tok_Semicolon
1474 then
1475 if Present (Assoc_List) then
1476 Error_Msg_BC -- CODEFIX
1477 ("""='>"" expected (positional association cannot follow " &
1478 "named association)");
1479 end if;
1481 if No (Expr_List) then
1482 Expr_List := New_List;
1483 end if;
1485 Append (Expr_Node, Expr_List);
1487 -- Check for aggregate followed by left parent, maybe missing comma
1489 elsif Nkind (Expr_Node) = N_Aggregate
1490 and then Token = Tok_Left_Paren
1491 then
1492 T_Comma;
1494 if No (Expr_List) then
1495 Expr_List := New_List;
1496 end if;
1498 Append (Expr_Node, Expr_List);
1500 -- Anything else is assumed to be a named association
1502 else
1503 Restore_Scan_State (Scan_State); -- to start of expression
1505 if No (Assoc_List) then
1506 Assoc_List := New_List;
1507 end if;
1509 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1510 end if;
1512 exit when not Comma_Present;
1514 -- If we are at an expression terminator, something is seriously
1515 -- wrong, so let's get out now, before we start eating up stuff
1516 -- that doesn't belong to us.
1518 if Token in Token_Class_Eterm then
1519 Error_Msg_AP
1520 ("expecting expression or component association");
1521 exit;
1522 end if;
1524 -- Deal with misused box
1526 if Token = Tok_Box then
1527 Box_Error;
1529 -- Otherwise initiate for reentry to top of loop by scanning an
1530 -- initial expression, unless the first token is OTHERS.
1532 elsif Token = Tok_Others then
1533 Expr_Node := Empty;
1535 else
1536 Save_Scan_State (Scan_State); -- at start of expression
1537 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1539 end if;
1540 end loop;
1542 -- All component associations (positional and named) have been scanned
1544 T_Right_Paren;
1545 Set_Expressions (Aggregate_Node, Expr_List);
1546 Set_Component_Associations (Aggregate_Node, Assoc_List);
1547 return Aggregate_Node;
1548 end P_Aggregate_Or_Paren_Expr;
1550 ------------------------------------------------
1551 -- 4.3 Record or Array Component Association --
1552 ------------------------------------------------
1554 -- RECORD_COMPONENT_ASSOCIATION ::=
1555 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1556 -- | COMPONENT_CHOICE_LIST => <>
1558 -- COMPONENT_CHOICE_LIST =>
1559 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1560 -- | others
1562 -- ARRAY_COMPONENT_ASSOCIATION ::=
1563 -- DISCRETE_CHOICE_LIST => EXPRESSION
1564 -- | DISCRETE_CHOICE_LIST => <>
1566 -- Note: this routine only handles the named cases, including others.
1567 -- Cases where the component choice list is not present have already
1568 -- been handled directly.
1570 -- Error recovery: can raise Error_Resync
1572 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1573 -- rules have been extended to give support to Ada 2005 limited
1574 -- aggregates (AI-287)
1576 function P_Record_Or_Array_Component_Association return Node_Id is
1577 Assoc_Node : Node_Id;
1579 begin
1580 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1581 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1582 Set_Sloc (Assoc_Node, Token_Ptr);
1583 TF_Arrow;
1585 if Token = Tok_Box then
1587 -- Ada 2005(AI-287): The box notation is used to indicate the
1588 -- default initialization of aggregate components
1590 if Ada_Version < Ada_2005 then
1591 Error_Msg_SP
1592 ("component association with '<'> is an Ada 2005 extension");
1593 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1594 end if;
1596 Set_Box_Present (Assoc_Node);
1597 Scan; -- Past box
1598 else
1599 Set_Expression (Assoc_Node, P_Expression);
1600 end if;
1602 return Assoc_Node;
1603 end P_Record_Or_Array_Component_Association;
1605 -----------------------------
1606 -- 4.3.1 Record Aggregate --
1607 -----------------------------
1609 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1610 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1612 ----------------------------------------------
1613 -- 4.3.1 Record Component Association List --
1614 ----------------------------------------------
1616 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1618 ----------------------------------
1619 -- 4.3.1 Component Choice List --
1620 ----------------------------------
1622 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1624 --------------------------------
1625 -- 4.3.1 Extension Aggregate --
1626 --------------------------------
1628 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1630 --------------------------
1631 -- 4.3.1 Ancestor Part --
1632 --------------------------
1634 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1636 ----------------------------
1637 -- 4.3.1 Array Aggregate --
1638 ----------------------------
1640 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1642 ---------------------------------------
1643 -- 4.3.1 Positional Array Aggregate --
1644 ---------------------------------------
1646 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1648 ----------------------------------
1649 -- 4.3.1 Named Array Aggregate --
1650 ----------------------------------
1652 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1654 ----------------------------------------
1655 -- 4.3.1 Array Component Association --
1656 ----------------------------------------
1658 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1660 ---------------------
1661 -- 4.4 Expression --
1662 ---------------------
1664 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1666 -- EXPRESSION ::=
1667 -- RELATION {LOGICAL_OPERATOR RELATION}
1669 -- CHOICE_EXPRESSION ::=
1670 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1672 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1674 -- On return, Expr_Form indicates the categorization of the expression
1675 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1676 -- an error message is given, and Error is returned).
1678 -- Error recovery: cannot raise Error_Resync
1680 function P_Expression return Node_Id is
1681 Logical_Op : Node_Kind;
1682 Prev_Logical_Op : Node_Kind;
1683 Op_Location : Source_Ptr;
1684 Node1 : Node_Id;
1685 Node2 : Node_Id;
1687 begin
1688 Node1 := P_Relation;
1690 if Token in Token_Class_Logop then
1691 Prev_Logical_Op := N_Empty;
1693 loop
1694 Op_Location := Token_Ptr;
1695 Logical_Op := P_Logical_Operator;
1697 if Prev_Logical_Op /= N_Empty and then
1698 Logical_Op /= Prev_Logical_Op
1699 then
1700 Error_Msg
1701 ("mixed logical operators in expression", Op_Location);
1702 Prev_Logical_Op := N_Empty;
1703 else
1704 Prev_Logical_Op := Logical_Op;
1705 end if;
1707 Node2 := Node1;
1708 Node1 := New_Op_Node (Logical_Op, Op_Location);
1709 Set_Left_Opnd (Node1, Node2);
1710 Set_Right_Opnd (Node1, P_Relation);
1711 exit when Token not in Token_Class_Logop;
1712 end loop;
1714 Expr_Form := EF_Non_Simple;
1715 end if;
1717 if Token = Tok_Apostrophe then
1718 Bad_Range_Attribute (Token_Ptr);
1719 return Error;
1720 else
1721 return Node1;
1722 end if;
1723 end P_Expression;
1725 -- This function is identical to the normal P_Expression, except that it
1726 -- also permits the appearance of a case, conditional, or quantified
1727 -- expression if the call immediately follows a left paren, and followed
1728 -- by a right parenthesis. These forms are allowed if these conditions
1729 -- are not met, but an error message will be issued.
1731 function P_Expression_If_OK return Node_Id is
1732 begin
1733 -- Case of conditional, case or quantified expression
1735 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1736 return P_Unparen_Cond_Case_Quant_Expression;
1738 -- Normal case, not case/conditional/quantified expression
1740 else
1741 return P_Expression;
1742 end if;
1743 end P_Expression_If_OK;
1745 -- This function is identical to the normal P_Expression, except that it
1746 -- checks that the expression scan did not stop on a right paren. It is
1747 -- called in all contexts where a right parenthesis cannot legitimately
1748 -- follow an expression.
1750 -- Error recovery: can not raise Error_Resync
1752 function P_Expression_No_Right_Paren return Node_Id is
1753 Expr : constant Node_Id := P_Expression;
1754 begin
1755 Ignore (Tok_Right_Paren);
1756 return Expr;
1757 end P_Expression_No_Right_Paren;
1759 ----------------------------------------
1760 -- 4.4 Expression_Or_Range_Attribute --
1761 ----------------------------------------
1763 -- EXPRESSION ::=
1764 -- RELATION {and RELATION} | RELATION {and then RELATION}
1765 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1766 -- | RELATION {xor RELATION}
1768 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1770 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1772 -- On return, Expr_Form indicates the categorization of the expression
1773 -- and EF_Range_Attr is one of the possibilities.
1775 -- Error recovery: cannot raise Error_Resync
1777 -- In the grammar, a RANGE attribute is simply a name, but its use is
1778 -- highly restricted, so in the parser, we do not regard it as a name.
1779 -- Instead, P_Name returns without scanning the 'RANGE part of the
1780 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1781 -- attribute reference. In the normal case where a range attribute is
1782 -- not allowed, an error message is issued by P_Expression.
1784 function P_Expression_Or_Range_Attribute return Node_Id is
1785 Logical_Op : Node_Kind;
1786 Prev_Logical_Op : Node_Kind;
1787 Op_Location : Source_Ptr;
1788 Node1 : Node_Id;
1789 Node2 : Node_Id;
1790 Attr_Node : Node_Id;
1792 begin
1793 Node1 := P_Relation;
1795 if Token = Tok_Apostrophe then
1796 Attr_Node := P_Range_Attribute_Reference (Node1);
1797 Expr_Form := EF_Range_Attr;
1798 return Attr_Node;
1800 elsif Token in Token_Class_Logop then
1801 Prev_Logical_Op := N_Empty;
1803 loop
1804 Op_Location := Token_Ptr;
1805 Logical_Op := P_Logical_Operator;
1807 if Prev_Logical_Op /= N_Empty and then
1808 Logical_Op /= Prev_Logical_Op
1809 then
1810 Error_Msg
1811 ("mixed logical operators in expression", Op_Location);
1812 Prev_Logical_Op := N_Empty;
1813 else
1814 Prev_Logical_Op := Logical_Op;
1815 end if;
1817 Node2 := Node1;
1818 Node1 := New_Op_Node (Logical_Op, Op_Location);
1819 Set_Left_Opnd (Node1, Node2);
1820 Set_Right_Opnd (Node1, P_Relation);
1821 exit when Token not in Token_Class_Logop;
1822 end loop;
1824 Expr_Form := EF_Non_Simple;
1825 end if;
1827 if Token = Tok_Apostrophe then
1828 Bad_Range_Attribute (Token_Ptr);
1829 return Error;
1830 else
1831 return Node1;
1832 end if;
1833 end P_Expression_Or_Range_Attribute;
1835 -- Version that allows a non-parenthesized case, conditional, or quantified
1836 -- expression if the call immediately follows a left paren, and followed
1837 -- by a right parenthesis. These forms are allowed if these conditions
1838 -- are not met, but an error message will be issued.
1840 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1841 begin
1842 -- Case of conditional, case or quantified expression
1844 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1845 return P_Unparen_Cond_Case_Quant_Expression;
1847 -- Normal case, not one of the above expression types
1849 else
1850 return P_Expression_Or_Range_Attribute;
1851 end if;
1852 end P_Expression_Or_Range_Attribute_If_OK;
1854 -------------------
1855 -- 4.4 Relation --
1856 -------------------
1858 -- This procedure scans both relations and choice relations
1860 -- CHOICE_RELATION ::=
1861 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1863 -- RELATION ::=
1864 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
1865 -- | RAISE_EXPRESSION
1867 -- MEMBERSHIP_CHOICE_LIST ::=
1868 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
1870 -- MEMBERSHIP_CHOICE ::=
1871 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
1873 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
1875 -- On return, Expr_Form indicates the categorization of the expression
1877 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1878 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1880 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1881 -- expression, then tokens are scanned until either a non-expression token,
1882 -- a right paren (not matched by a left paren) or a comma, is encountered.
1884 function P_Relation return Node_Id is
1885 Node1, Node2 : Node_Id;
1886 Optok : Source_Ptr;
1888 begin
1889 -- First check for raise expression
1891 if Token = Tok_Raise then
1892 Expr_Form := EF_Non_Simple;
1893 return P_Raise_Expression;
1894 end if;
1896 -- All other cases
1898 Node1 := P_Simple_Expression;
1900 if Token not in Token_Class_Relop then
1901 return Node1;
1903 else
1904 -- Here we have a relational operator following. If so then scan it
1905 -- out. Note that the assignment symbol := is treated as a relational
1906 -- operator to improve the error recovery when it is misused for =.
1907 -- P_Relational_Operator also parses the IN and NOT IN operations.
1909 Optok := Token_Ptr;
1910 Node2 := New_Op_Node (P_Relational_Operator, Optok);
1911 Set_Left_Opnd (Node2, Node1);
1913 -- Case of IN or NOT IN
1915 if Prev_Token = Tok_In then
1916 P_Membership_Test (Node2);
1918 -- Case of relational operator (= /= < <= > >=)
1920 else
1921 Set_Right_Opnd (Node2, P_Simple_Expression);
1922 end if;
1924 Expr_Form := EF_Non_Simple;
1926 if Token in Token_Class_Relop then
1927 Error_Msg_SC ("unexpected relational operator");
1928 raise Error_Resync;
1929 end if;
1931 return Node2;
1932 end if;
1934 -- If any error occurs, then scan to the next expression terminator symbol
1935 -- or comma or right paren at the outer (i.e. current) parentheses level.
1936 -- The flags are set to indicate a normal simple expression.
1938 exception
1939 when Error_Resync =>
1940 Resync_Expression;
1941 Expr_Form := EF_Simple;
1942 return Error;
1943 end P_Relation;
1945 ----------------------------
1946 -- 4.4 Simple Expression --
1947 ----------------------------
1949 -- SIMPLE_EXPRESSION ::=
1950 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1952 -- On return, Expr_Form indicates the categorization of the expression
1954 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1955 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1957 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1958 -- expression, then tokens are scanned until either a non-expression token,
1959 -- a right paren (not matched by a left paren) or a comma, is encountered.
1961 -- Note: P_Simple_Expression is called only internally by higher level
1962 -- expression routines. In cases in the grammar where a simple expression
1963 -- is required, the approach is to scan an expression, and then post an
1964 -- appropriate error message if the expression obtained is not simple. This
1965 -- gives better error recovery and treatment.
1967 function P_Simple_Expression return Node_Id is
1968 Scan_State : Saved_Scan_State;
1969 Node1 : Node_Id;
1970 Node2 : Node_Id;
1971 Tokptr : Source_Ptr;
1973 function At_Start_Of_Attribute return Boolean;
1974 -- Tests if we have quote followed by attribute name, if so, return True
1975 -- otherwise return False.
1977 ---------------------------
1978 -- At_Start_Of_Attribute --
1979 ---------------------------
1981 function At_Start_Of_Attribute return Boolean is
1982 begin
1983 if Token /= Tok_Apostrophe then
1984 return False;
1986 else
1987 declare
1988 Scan_State : Saved_Scan_State;
1990 begin
1991 Save_Scan_State (Scan_State);
1992 Scan; -- past quote
1994 if Token = Tok_Identifier
1995 and then Is_Attribute_Name (Chars (Token_Node))
1996 then
1997 Restore_Scan_State (Scan_State);
1998 return True;
1999 else
2000 Restore_Scan_State (Scan_State);
2001 return False;
2002 end if;
2003 end;
2004 end if;
2005 end At_Start_Of_Attribute;
2007 -- Start of processing for P_Simple_Expression
2009 begin
2010 -- Check for cases starting with a name. There are two reasons for
2011 -- special casing. First speed things up by catching a common case
2012 -- without going through several routine layers. Second the caller must
2013 -- be informed via Expr_Form when the simple expression is a name.
2015 if Token in Token_Class_Name then
2016 Node1 := P_Name;
2018 -- Deal with apostrophe cases
2020 if Token = Tok_Apostrophe then
2021 Save_Scan_State (Scan_State); -- at apostrophe
2022 Scan; -- past apostrophe
2024 -- If qualified expression, scan it out and fall through
2026 if Token = Tok_Left_Paren then
2027 Node1 := P_Qualified_Expression (Node1);
2028 Expr_Form := EF_Simple;
2030 -- If range attribute, then we return with Token pointing to the
2031 -- apostrophe. Note: avoid the normal error check on exit. We
2032 -- know that the expression really is complete in this case.
2034 else -- Token = Tok_Range then
2035 Restore_Scan_State (Scan_State); -- to apostrophe
2036 Expr_Form := EF_Simple_Name;
2037 return Node1;
2038 end if;
2039 end if;
2041 -- If an expression terminator follows, the previous processing
2042 -- completely scanned out the expression (a common case), and
2043 -- left Expr_Form set appropriately for returning to our caller.
2045 if Token in Token_Class_Sterm then
2046 null;
2048 -- If we do not have an expression terminator, then complete the
2049 -- scan of a simple expression. This code duplicates the code
2050 -- found in P_Term and P_Factor.
2052 else
2053 if Token = Tok_Double_Asterisk then
2054 if Style_Check then
2055 Style.Check_Exponentiation_Operator;
2056 end if;
2058 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2059 Scan; -- past **
2060 Set_Left_Opnd (Node2, Node1);
2061 Set_Right_Opnd (Node2, P_Primary);
2062 Check_Bad_Exp;
2063 Node1 := Node2;
2064 end if;
2066 loop
2067 exit when Token not in Token_Class_Mulop;
2068 Tokptr := Token_Ptr;
2069 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2071 if Style_Check then
2072 Style.Check_Binary_Operator;
2073 end if;
2075 Scan; -- past operator
2076 Set_Left_Opnd (Node2, Node1);
2077 Set_Right_Opnd (Node2, P_Factor);
2078 Node1 := Node2;
2079 end loop;
2081 loop
2082 exit when Token not in Token_Class_Binary_Addop;
2083 Tokptr := Token_Ptr;
2084 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2086 if Style_Check then
2087 Style.Check_Binary_Operator;
2088 end if;
2090 Scan; -- past operator
2091 Set_Left_Opnd (Node2, Node1);
2092 Set_Right_Opnd (Node2, P_Term);
2093 Node1 := Node2;
2094 end loop;
2096 Expr_Form := EF_Simple;
2097 end if;
2099 -- Cases where simple expression does not start with a name
2101 else
2102 -- Scan initial sign and initial Term
2104 if Token in Token_Class_Unary_Addop then
2105 Tokptr := Token_Ptr;
2106 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
2108 if Style_Check then
2109 Style.Check_Unary_Plus_Or_Minus (Inside_Depends);
2110 end if;
2112 Scan; -- past operator
2113 Set_Right_Opnd (Node1, P_Term);
2114 else
2115 Node1 := P_Term;
2116 end if;
2118 -- In the following, we special-case a sequence of concatenations of
2119 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2120 -- else mixed in. For such a sequence, we return a tree representing
2121 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2122 -- the number of concatenations is large. If semantic analysis
2123 -- resolves the "&" to a predefined one, then this folding gives the
2124 -- right answer. Otherwise, semantic analysis will complain about a
2125 -- capacity-exceeded error. The purpose of this trick is to avoid
2126 -- creating a deeply nested tree, which would cause deep recursion
2127 -- during semantics, causing stack overflow. This way, we can handle
2128 -- enormous concatenations in the normal case of predefined "&". We
2129 -- first build up the normal tree, and then rewrite it if
2130 -- appropriate.
2132 declare
2133 Num_Concats_Threshold : constant Positive := 1000;
2134 -- Arbitrary threshold value to enable optimization
2136 First_Node : constant Node_Id := Node1;
2137 Is_Strlit_Concat : Boolean;
2138 -- True iff we've parsed a sequence of concatenations of string
2139 -- literals, with nothing else mixed in.
2141 Num_Concats : Natural;
2142 -- Number of "&" operators if Is_Strlit_Concat is True
2144 begin
2145 Is_Strlit_Concat :=
2146 Nkind (Node1) = N_String_Literal
2147 and then Token = Tok_Ampersand;
2148 Num_Concats := 0;
2150 -- Scan out sequence of terms separated by binary adding operators
2152 loop
2153 exit when Token not in Token_Class_Binary_Addop;
2154 Tokptr := Token_Ptr;
2155 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2157 if Style_Check and then not Debug_Flag_Dot_QQ then
2158 Style.Check_Binary_Operator;
2159 end if;
2161 Scan; -- past operator
2162 Set_Left_Opnd (Node2, Node1);
2163 Node1 := P_Term;
2164 Set_Right_Opnd (Node2, Node1);
2166 -- Check if we're still concatenating string literals
2168 Is_Strlit_Concat :=
2169 Is_Strlit_Concat
2170 and then Nkind (Node2) = N_Op_Concat
2171 and then Nkind (Node1) = N_String_Literal;
2173 if Is_Strlit_Concat then
2174 Num_Concats := Num_Concats + 1;
2175 end if;
2177 Node1 := Node2;
2178 end loop;
2180 -- If we have an enormous series of concatenations of string
2181 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2182 -- flag tells semantic analysis that if the "&" is not predefined,
2183 -- the folded value is wrong.
2185 if Is_Strlit_Concat
2186 and then Num_Concats >= Num_Concats_Threshold
2187 then
2188 declare
2189 Empty_String_Val : String_Id;
2190 -- String_Id for ""
2192 Strlit_Concat_Val : String_Id;
2193 -- Contains the folded value (which will be correct if the
2194 -- "&" operators are the predefined ones).
2196 Cur_Node : Node_Id;
2197 -- For walking up the tree
2199 New_Node : Node_Id;
2200 -- Folded node to replace Node1
2202 Loc : constant Source_Ptr := Sloc (First_Node);
2204 begin
2205 -- Walk up the tree starting at the leftmost string literal
2206 -- (First_Node), building up the Strlit_Concat_Val as we
2207 -- go. Note that we do not use recursion here -- the whole
2208 -- point is to avoid recursively walking that enormous tree.
2210 Start_String;
2211 Store_String_Chars (Strval (First_Node));
2213 Cur_Node := Parent (First_Node);
2214 while Present (Cur_Node) loop
2215 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2216 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2218 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2219 Cur_Node := Parent (Cur_Node);
2220 end loop;
2222 Strlit_Concat_Val := End_String;
2224 -- Create new folded node, and rewrite result with a concat-
2225 -- enation of an empty string literal and the folded node.
2227 Start_String;
2228 Empty_String_Val := End_String;
2229 New_Node :=
2230 Make_Op_Concat (Loc,
2231 Make_String_Literal (Loc, Empty_String_Val),
2232 Make_String_Literal (Loc, Strlit_Concat_Val,
2233 Is_Folded_In_Parser => True));
2234 Rewrite (Node1, New_Node);
2235 end;
2236 end if;
2237 end;
2239 -- All done, we clearly do not have name or numeric literal so this
2240 -- is a case of a simple expression which is some other possibility.
2242 Expr_Form := EF_Simple;
2243 end if;
2245 -- Come here at end of simple expression, where we do a couple of
2246 -- special checks to improve error recovery.
2248 -- Special test to improve error recovery. If the current token
2249 -- is a period, then someone is trying to do selection on something
2250 -- that is not a name, e.g. a qualified expression.
2252 if Token = Tok_Dot then
2253 Error_Msg_SC ("prefix for selection is not a name");
2255 -- If qualified expression, comment and continue, otherwise something
2256 -- is pretty nasty so do an Error_Resync call.
2258 if Ada_Version < Ada_2012
2259 and then Nkind (Node1) = N_Qualified_Expression
2260 then
2261 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2262 else
2263 raise Error_Resync;
2264 end if;
2265 end if;
2267 -- Special test to improve error recovery: If the current token is
2268 -- not the first token on a line (as determined by checking the
2269 -- previous token position with the start of the current line),
2270 -- then we insist that we have an appropriate terminating token.
2271 -- Consider the following two examples:
2273 -- 1) if A nad B then ...
2275 -- 2) A := B
2276 -- C := D
2278 -- In the first example, we would like to issue a binary operator
2279 -- expected message and resynchronize to the then. In the second
2280 -- example, we do not want to issue a binary operator message, so
2281 -- that instead we will get the missing semicolon message. This
2282 -- distinction is of course a heuristic which does not always work,
2283 -- but in practice it is quite effective.
2285 -- Note: the one case in which we do not go through this circuit is
2286 -- when we have scanned a range attribute and want to return with
2287 -- Token pointing to the apostrophe. The apostrophe is not normally
2288 -- an expression terminator, and is not in Token_Class_Sterm, but
2289 -- in this special case we know that the expression is complete.
2291 if not Token_Is_At_Start_Of_Line
2292 and then Token not in Token_Class_Sterm
2293 then
2294 -- Normally the right error message is indeed that we expected a
2295 -- binary operator, but in the case of being between a right and left
2296 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2298 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2299 T_Comma;
2301 -- And if we have a quote, we may have a bad attribute
2303 elsif At_Start_Of_Attribute then
2304 Error_Msg_SC ("prefix of attribute must be a name");
2306 if Ada_Version >= Ada_2012 then
2307 Error_Msg_SC ("\qualify expression to turn it into a name");
2308 end if;
2310 -- Normal case for binary operator expected message
2312 else
2313 Error_Msg_AP ("binary operator expected");
2314 end if;
2316 raise Error_Resync;
2318 else
2319 return Node1;
2320 end if;
2322 -- If any error occurs, then scan to next expression terminator symbol
2323 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2324 -- level. Expr_Form is set to indicate a normal simple expression.
2326 exception
2327 when Error_Resync =>
2328 Resync_Expression;
2329 Expr_Form := EF_Simple;
2330 return Error;
2331 end P_Simple_Expression;
2333 -----------------------------------------------
2334 -- 4.4 Simple Expression or Range Attribute --
2335 -----------------------------------------------
2337 -- SIMPLE_EXPRESSION ::=
2338 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2340 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2342 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2344 -- Error recovery: cannot raise Error_Resync
2346 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2347 Sexpr : Node_Id;
2348 Attr_Node : Node_Id;
2350 begin
2351 -- We don't just want to roar ahead and call P_Simple_Expression
2352 -- here, since we want to handle the case of a parenthesized range
2353 -- attribute cleanly.
2355 if Token = Tok_Left_Paren then
2356 declare
2357 Lptr : constant Source_Ptr := Token_Ptr;
2358 Scan_State : Saved_Scan_State;
2360 begin
2361 Save_Scan_State (Scan_State);
2362 Scan; -- past left paren
2363 Sexpr := P_Simple_Expression;
2365 if Token = Tok_Apostrophe then
2366 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2367 Expr_Form := EF_Range_Attr;
2369 if Token = Tok_Right_Paren then
2370 Scan; -- scan past right paren if present
2371 end if;
2373 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2375 return Attr_Node;
2376 end if;
2378 Restore_Scan_State (Scan_State);
2379 end;
2380 end if;
2382 -- Here after dealing with parenthesized range attribute
2384 Sexpr := P_Simple_Expression;
2386 if Token = Tok_Apostrophe then
2387 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2388 Expr_Form := EF_Range_Attr;
2389 return Attr_Node;
2391 else
2392 return Sexpr;
2393 end if;
2394 end P_Simple_Expression_Or_Range_Attribute;
2396 ---------------
2397 -- 4.4 Term --
2398 ---------------
2400 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2402 -- Error recovery: can raise Error_Resync
2404 function P_Term return Node_Id is
2405 Node1, Node2 : Node_Id;
2406 Tokptr : Source_Ptr;
2408 begin
2409 Node1 := P_Factor;
2411 loop
2412 exit when Token not in Token_Class_Mulop;
2413 Tokptr := Token_Ptr;
2414 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2416 if Style_Check and then not Debug_Flag_Dot_QQ then
2417 Style.Check_Binary_Operator;
2418 end if;
2420 Scan; -- past operator
2421 Set_Left_Opnd (Node2, Node1);
2422 Set_Right_Opnd (Node2, P_Factor);
2423 Node1 := Node2;
2424 end loop;
2426 return Node1;
2427 end P_Term;
2429 -----------------
2430 -- 4.4 Factor --
2431 -----------------
2433 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2435 -- Error recovery: can raise Error_Resync
2437 function P_Factor return Node_Id is
2438 Node1 : Node_Id;
2439 Node2 : Node_Id;
2441 begin
2442 if Token = Tok_Abs then
2443 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2445 if Style_Check then
2446 Style.Check_Abs_Not;
2447 end if;
2449 Scan; -- past ABS
2450 Set_Right_Opnd (Node1, P_Primary);
2451 return Node1;
2453 elsif Token = Tok_Not then
2454 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2456 if Style_Check then
2457 Style.Check_Abs_Not;
2458 end if;
2460 Scan; -- past NOT
2461 Set_Right_Opnd (Node1, P_Primary);
2462 return Node1;
2464 else
2465 Node1 := P_Primary;
2467 if Token = Tok_Double_Asterisk then
2468 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2469 Scan; -- past **
2470 Set_Left_Opnd (Node2, Node1);
2471 Set_Right_Opnd (Node2, P_Primary);
2472 Check_Bad_Exp;
2473 return Node2;
2474 else
2475 return Node1;
2476 end if;
2477 end if;
2478 end P_Factor;
2480 ------------------
2481 -- 4.4 Primary --
2482 ------------------
2484 -- PRIMARY ::=
2485 -- NUMERIC_LITERAL | null
2486 -- | STRING_LITERAL | AGGREGATE
2487 -- | NAME | QUALIFIED_EXPRESSION
2488 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2490 -- Error recovery: can raise Error_Resync
2492 function P_Primary return Node_Id is
2493 Scan_State : Saved_Scan_State;
2494 Node1 : Node_Id;
2496 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
2497 -- Remember if previous token is a left parenthesis. This is used to
2498 -- deal with checking whether IF/CASE/FOR expressions appearing as
2499 -- primaries require extra parenthesization.
2501 begin
2502 -- The loop runs more than once only if misplaced pragmas are found
2503 -- or if a misplaced unary minus is skipped.
2505 loop
2506 case Token is
2508 -- Name token can start a name, call or qualified expression, all
2509 -- of which are acceptable possibilities for primary. Note also
2510 -- that string literal is included in name (as operator symbol)
2511 -- and type conversion is included in name (as indexed component).
2513 when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2514 Node1 := P_Name;
2516 -- All done unless apostrophe follows
2518 if Token /= Tok_Apostrophe then
2519 return Node1;
2521 -- Apostrophe following means that we have either just parsed
2522 -- the subtype mark of a qualified expression, or the prefix
2523 -- or a range attribute.
2525 else -- Token = Tok_Apostrophe
2526 Save_Scan_State (Scan_State); -- at apostrophe
2527 Scan; -- past apostrophe
2529 -- If range attribute, then this is always an error, since
2530 -- the only legitimate case (where the scanned expression is
2531 -- a qualified simple name) is handled at the level of the
2532 -- Simple_Expression processing. This case corresponds to a
2533 -- usage such as 3 + A'Range, which is always illegal.
2535 if Token = Tok_Range then
2536 Restore_Scan_State (Scan_State); -- to apostrophe
2537 Bad_Range_Attribute (Token_Ptr);
2538 return Error;
2540 -- If left paren, then we have a qualified expression.
2541 -- Note that P_Name guarantees that in this case, where
2542 -- Token = Tok_Apostrophe on return, the only two possible
2543 -- tokens following the apostrophe are left paren and
2544 -- RANGE, so we know we have a left paren here.
2546 else -- Token = Tok_Left_Paren
2547 return P_Qualified_Expression (Node1);
2549 end if;
2550 end if;
2552 -- Numeric or string literal
2554 when Tok_Integer_Literal |
2555 Tok_Real_Literal |
2556 Tok_String_Literal =>
2558 Node1 := Token_Node;
2559 Scan; -- past number
2560 return Node1;
2562 -- Left paren, starts aggregate or parenthesized expression
2564 when Tok_Left_Paren =>
2565 declare
2566 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2568 begin
2569 if Nkind (Expr) = N_Attribute_Reference
2570 and then Attribute_Name (Expr) = Name_Range
2571 then
2572 Bad_Range_Attribute (Sloc (Expr));
2573 end if;
2575 return Expr;
2576 end;
2578 -- Allocator
2580 when Tok_New =>
2581 return P_Allocator;
2583 -- Null
2585 when Tok_Null =>
2586 Scan; -- past NULL
2587 return New_Node (N_Null, Prev_Token_Ptr);
2589 -- Pragma, not allowed here, so just skip past it
2591 when Tok_Pragma =>
2592 P_Pragmas_Misplaced;
2594 -- Deal with IF (possible unparenthesized if expression)
2596 when Tok_If =>
2598 -- If this looks like a real if, defined as an IF appearing at
2599 -- the start of a new line, then we consider we have a missing
2600 -- operand. If in Ada 2012 and the IF is not properly indented
2601 -- for a statement, we prefer to issue a message about an ill-
2602 -- parenthesized if expression.
2604 if Token_Is_At_Start_Of_Line
2605 and then not
2606 (Ada_Version >= Ada_2012
2607 and then Style_Check_Indentation /= 0
2608 and then Start_Column rem Style_Check_Indentation /= 0)
2609 then
2610 Error_Msg_AP ("missing operand");
2611 return Error;
2613 -- If this looks like an if expression, then treat it that way
2614 -- with an error message if not explicitly surrounded by
2615 -- parentheses.
2617 elsif Ada_Version >= Ada_2012 then
2618 Node1 := P_If_Expression;
2620 if not (Lparen and then Token = Tok_Right_Paren) then
2621 Error_Msg
2622 ("if expression must be parenthesized", Sloc (Node1));
2623 end if;
2625 return Node1;
2627 -- Otherwise treat as misused identifier
2629 else
2630 return P_Identifier;
2631 end if;
2633 -- Deal with CASE (possible unparenthesized case expression)
2635 when Tok_Case =>
2637 -- If this looks like a real case, defined as a CASE appearing
2638 -- the start of a new line, then we consider we have a missing
2639 -- operand. If in Ada 2012 and the CASE is not properly
2640 -- indented for a statement, we prefer to issue a message about
2641 -- an ill-parenthesized case expression.
2643 if Token_Is_At_Start_Of_Line
2644 and then not
2645 (Ada_Version >= Ada_2012
2646 and then Style_Check_Indentation /= 0
2647 and then Start_Column rem Style_Check_Indentation /= 0)
2648 then
2649 Error_Msg_AP ("missing operand");
2650 return Error;
2652 -- If this looks like a case expression, then treat it that way
2653 -- with an error message if not within parentheses.
2655 elsif Ada_Version >= Ada_2012 then
2656 Node1 := P_Case_Expression;
2658 if not (Lparen and then Token = Tok_Right_Paren) then
2659 Error_Msg
2660 ("case expression must be parenthesized", Sloc (Node1));
2661 end if;
2663 return Node1;
2665 -- Otherwise treat as misused identifier
2667 else
2668 return P_Identifier;
2669 end if;
2671 -- For [all | some] indicates a quantified expression
2673 when Tok_For =>
2674 if Token_Is_At_Start_Of_Line then
2675 Error_Msg_AP ("misplaced loop");
2676 return Error;
2678 elsif Ada_Version >= Ada_2012 then
2679 Node1 := P_Quantified_Expression;
2681 if not (Lparen and then Token = Tok_Right_Paren) then
2682 Error_Msg
2683 ("quantified expression must be parenthesized",
2684 Sloc (Node1));
2685 end if;
2687 return Node1;
2689 -- Otherwise treat as misused identifier
2691 else
2692 return P_Identifier;
2693 end if;
2695 -- Minus may well be an improper attempt at a unary minus. Give
2696 -- a message, skip the minus and keep going.
2698 when Tok_Minus =>
2699 Error_Msg_SC ("parentheses required for unary minus");
2700 Scan; -- past minus
2702 -- Anything else is illegal as the first token of a primary, but
2703 -- we test for some common errors, to improve error messages.
2705 when others =>
2706 if Is_Reserved_Identifier then
2707 return P_Identifier;
2709 elsif Prev_Token = Tok_Comma then
2710 Error_Msg_SP -- CODEFIX
2711 ("|extra "","" ignored");
2712 raise Error_Resync;
2714 else
2715 Error_Msg_AP ("missing operand");
2716 raise Error_Resync;
2717 end if;
2719 end case;
2720 end loop;
2721 end P_Primary;
2723 -------------------------------
2724 -- 4.4 Quantified_Expression --
2725 -------------------------------
2727 -- QUANTIFIED_EXPRESSION ::=
2728 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
2729 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
2731 function P_Quantified_Expression return Node_Id is
2732 I_Spec : Node_Id;
2733 Node1 : Node_Id;
2735 begin
2736 Error_Msg_Ada_2012_Feature ("quantified expression", Token_Ptr);
2737 Scan; -- past FOR
2738 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
2740 if Token = Tok_All then
2741 Set_All_Present (Node1);
2742 elsif Token /= Tok_Some then
2743 Error_Msg_AP ("missing quantifier");
2744 raise Error_Resync;
2745 end if;
2747 Scan; -- past SOME
2748 I_Spec := P_Loop_Parameter_Specification;
2750 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
2751 Set_Loop_Parameter_Specification (Node1, I_Spec);
2752 else
2753 Set_Iterator_Specification (Node1, I_Spec);
2754 end if;
2756 if Token = Tok_Arrow then
2757 Scan;
2758 Set_Condition (Node1, P_Expression);
2759 return Node1;
2760 else
2761 Error_Msg_AP ("missing arrow");
2762 raise Error_Resync;
2763 end if;
2764 end P_Quantified_Expression;
2766 ---------------------------
2767 -- 4.5 Logical Operator --
2768 ---------------------------
2770 -- LOGICAL_OPERATOR ::= and | or | xor
2772 -- Note: AND THEN and OR ELSE are also treated as logical operators
2773 -- by the parser (even though they are not operators semantically)
2775 -- The value returned is the appropriate Node_Kind code for the operator
2776 -- On return, Token points to the token following the scanned operator.
2778 -- The caller has checked that the first token is a legitimate logical
2779 -- operator token (i.e. is either XOR, AND, OR).
2781 -- Error recovery: cannot raise Error_Resync
2783 function P_Logical_Operator return Node_Kind is
2784 begin
2785 if Token = Tok_And then
2786 if Style_Check then
2787 Style.Check_Binary_Operator;
2788 end if;
2790 Scan; -- past AND
2792 if Token = Tok_Then then
2793 Scan; -- past THEN
2794 return N_And_Then;
2795 else
2796 return N_Op_And;
2797 end if;
2799 elsif Token = Tok_Or then
2800 if Style_Check then
2801 Style.Check_Binary_Operator;
2802 end if;
2804 Scan; -- past OR
2806 if Token = Tok_Else then
2807 Scan; -- past ELSE
2808 return N_Or_Else;
2809 else
2810 return N_Op_Or;
2811 end if;
2813 else -- Token = Tok_Xor
2814 if Style_Check then
2815 Style.Check_Binary_Operator;
2816 end if;
2818 Scan; -- past XOR
2819 return N_Op_Xor;
2820 end if;
2821 end P_Logical_Operator;
2823 ------------------------------
2824 -- 4.5 Relational Operator --
2825 ------------------------------
2827 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2829 -- The value returned is the appropriate Node_Kind code for the operator.
2830 -- On return, Token points to the operator token, NOT past it.
2832 -- The caller has checked that the first token is a legitimate relational
2833 -- operator token (i.e. is one of the operator tokens listed above).
2835 -- Error recovery: cannot raise Error_Resync
2837 function P_Relational_Operator return Node_Kind is
2838 Op_Kind : Node_Kind;
2839 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2840 (Tok_Less => N_Op_Lt,
2841 Tok_Equal => N_Op_Eq,
2842 Tok_Greater => N_Op_Gt,
2843 Tok_Not_Equal => N_Op_Ne,
2844 Tok_Greater_Equal => N_Op_Ge,
2845 Tok_Less_Equal => N_Op_Le,
2846 Tok_In => N_In,
2847 Tok_Not => N_Not_In,
2848 Tok_Box => N_Op_Ne);
2850 begin
2851 if Token = Tok_Box then
2852 Error_Msg_SC -- CODEFIX
2853 ("|""'<'>"" should be ""/=""");
2854 end if;
2856 Op_Kind := Relop_Node (Token);
2858 if Style_Check then
2859 Style.Check_Binary_Operator;
2860 end if;
2862 Scan; -- past operator token
2864 -- Deal with NOT IN, if previous token was NOT, we must have IN now
2866 if Prev_Token = Tok_Not then
2868 -- Style check, for NOT IN, we require one space between NOT and IN
2870 if Style_Check and then Token = Tok_In then
2871 Style.Check_Not_In;
2872 end if;
2874 T_In;
2875 end if;
2877 return Op_Kind;
2878 end P_Relational_Operator;
2880 ---------------------------------
2881 -- 4.5 Binary Adding Operator --
2882 ---------------------------------
2884 -- BINARY_ADDING_OPERATOR ::= + | - | &
2886 -- The value returned is the appropriate Node_Kind code for the operator.
2887 -- On return, Token points to the operator token (NOT past it).
2889 -- The caller has checked that the first token is a legitimate adding
2890 -- operator token (i.e. is one of the operator tokens listed above).
2892 -- Error recovery: cannot raise Error_Resync
2894 function P_Binary_Adding_Operator return Node_Kind is
2895 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2896 (Tok_Ampersand => N_Op_Concat,
2897 Tok_Minus => N_Op_Subtract,
2898 Tok_Plus => N_Op_Add);
2899 begin
2900 return Addop_Node (Token);
2901 end P_Binary_Adding_Operator;
2903 --------------------------------
2904 -- 4.5 Unary Adding Operator --
2905 --------------------------------
2907 -- UNARY_ADDING_OPERATOR ::= + | -
2909 -- The value returned is the appropriate Node_Kind code for the operator.
2910 -- On return, Token points to the operator token (NOT past it).
2912 -- The caller has checked that the first token is a legitimate adding
2913 -- operator token (i.e. is one of the operator tokens listed above).
2915 -- Error recovery: cannot raise Error_Resync
2917 function P_Unary_Adding_Operator return Node_Kind is
2918 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2919 (Tok_Minus => N_Op_Minus,
2920 Tok_Plus => N_Op_Plus);
2921 begin
2922 return Addop_Node (Token);
2923 end P_Unary_Adding_Operator;
2925 -------------------------------
2926 -- 4.5 Multiplying Operator --
2927 -------------------------------
2929 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
2931 -- The value returned is the appropriate Node_Kind code for the operator.
2932 -- On return, Token points to the operator token (NOT past it).
2934 -- The caller has checked that the first token is a legitimate multiplying
2935 -- operator token (i.e. is one of the operator tokens listed above).
2937 -- Error recovery: cannot raise Error_Resync
2939 function P_Multiplying_Operator return Node_Kind is
2940 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2941 (Tok_Asterisk => N_Op_Multiply,
2942 Tok_Mod => N_Op_Mod,
2943 Tok_Rem => N_Op_Rem,
2944 Tok_Slash => N_Op_Divide);
2945 begin
2946 return Mulop_Node (Token);
2947 end P_Multiplying_Operator;
2949 --------------------------------------
2950 -- 4.5 Highest Precedence Operator --
2951 --------------------------------------
2953 -- Parsed by P_Factor (4.4)
2955 -- Note: this rule is not in fact used by the grammar at any point
2957 --------------------------
2958 -- 4.6 Type Conversion --
2959 --------------------------
2961 -- Parsed by P_Primary as a Name (4.1)
2963 -------------------------------
2964 -- 4.7 Qualified Expression --
2965 -------------------------------
2967 -- QUALIFIED_EXPRESSION ::=
2968 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2970 -- The caller has scanned the name which is the Subtype_Mark parameter
2971 -- and scanned past the single quote following the subtype mark. The
2972 -- caller has not checked that this name is in fact appropriate for
2973 -- a subtype mark name (i.e. it is a selected component or identifier).
2975 -- Error_Recovery: cannot raise Error_Resync
2977 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2978 Qual_Node : Node_Id;
2979 begin
2980 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2981 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2982 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2983 return Qual_Node;
2984 end P_Qualified_Expression;
2986 --------------------
2987 -- 4.8 Allocator --
2988 --------------------
2990 -- ALLOCATOR ::=
2991 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
2992 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
2994 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
2996 -- The caller has checked that the initial token is NEW
2998 -- Error recovery: can raise Error_Resync
3000 function P_Allocator return Node_Id is
3001 Alloc_Node : Node_Id;
3002 Type_Node : Node_Id;
3003 Null_Exclusion_Present : Boolean;
3005 begin
3006 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
3007 T_New;
3009 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3011 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3013 if Token = Tok_Left_Paren then
3014 Scan; -- past (
3015 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
3016 T_Right_Paren;
3018 Error_Msg_Ada_2012_Feature
3019 ("|subpool specification",
3020 Sloc (Subpool_Handle_Name (Alloc_Node)));
3021 end if;
3023 Null_Exclusion_Present := P_Null_Exclusion;
3024 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
3025 Type_Node := P_Subtype_Mark_Resync;
3027 if Token = Tok_Apostrophe then
3028 Scan; -- past apostrophe
3029 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
3030 else
3031 Set_Expression
3032 (Alloc_Node,
3033 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
3035 -- AI05-0104: An explicit null exclusion is not allowed for an
3036 -- allocator without initialization. In previous versions of the
3037 -- language it just raises constraint error.
3039 if Ada_Version >= Ada_2012 and then Null_Exclusion_Present then
3040 Error_Msg_N
3041 ("an allocator with a subtype indication "
3042 & "cannot have a null exclusion", Alloc_Node);
3043 end if;
3044 end if;
3046 return Alloc_Node;
3047 end P_Allocator;
3049 -----------------------
3050 -- P_Case_Expression --
3051 -----------------------
3053 function P_Case_Expression return Node_Id is
3054 Loc : constant Source_Ptr := Token_Ptr;
3055 Case_Node : Node_Id;
3056 Save_State : Saved_Scan_State;
3058 begin
3059 Error_Msg_Ada_2012_Feature ("|case expression", Token_Ptr);
3060 Scan; -- past CASE
3061 Case_Node :=
3062 Make_Case_Expression (Loc,
3063 Expression => P_Expression_No_Right_Paren,
3064 Alternatives => New_List);
3065 T_Is;
3067 -- We now have scanned out CASE expression IS, scan alternatives
3069 loop
3070 T_When;
3071 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
3073 -- Missing comma if WHEN (more alternatives present)
3075 if Token = Tok_When then
3076 T_Comma;
3078 -- If comma/WHEN, skip comma and we have another alternative
3080 elsif Token = Tok_Comma then
3081 Save_Scan_State (Save_State);
3082 Scan; -- past comma
3084 if Token /= Tok_When then
3085 Restore_Scan_State (Save_State);
3086 exit;
3087 end if;
3089 -- If no comma or WHEN, definitely done
3091 else
3092 exit;
3093 end if;
3094 end loop;
3096 -- If we have an END CASE, diagnose as not needed
3098 if Token = Tok_End then
3099 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
3100 Scan; -- past END
3102 if Token = Tok_Case then
3103 Scan; -- past CASE;
3104 end if;
3105 end if;
3107 -- Return the Case_Expression node
3109 return Case_Node;
3110 end P_Case_Expression;
3112 -----------------------------------
3113 -- P_Case_Expression_Alternative --
3114 -----------------------------------
3116 -- CASE_STATEMENT_ALTERNATIVE ::=
3117 -- when DISCRETE_CHOICE_LIST =>
3118 -- EXPRESSION
3120 -- The caller has checked that and scanned past the initial WHEN token
3121 -- Error recovery: can raise Error_Resync
3123 function P_Case_Expression_Alternative return Node_Id is
3124 Case_Alt_Node : Node_Id;
3125 begin
3126 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
3127 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
3128 TF_Arrow;
3129 Set_Expression (Case_Alt_Node, P_Expression);
3130 return Case_Alt_Node;
3131 end P_Case_Expression_Alternative;
3133 ---------------------
3134 -- P_If_Expression --
3135 ---------------------
3137 function P_If_Expression return Node_Id is
3139 function P_If_Expression_Internal
3140 (Loc : Source_Ptr;
3141 Cond : Node_Id) return Node_Id;
3142 -- This is the internal recursive routine that does all the work, it is
3143 -- recursive since it is used to process ELSIF parts, which internally
3144 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3145 -- sequence is like the outer function except that the caller passes
3146 -- the conditional expression (scanned using P_Expression), and the
3147 -- scan pointer points just past this expression. Loc points to the
3148 -- IF or ELSIF token.
3150 ------------------------------
3151 -- P_If_Expression_Internal --
3152 ------------------------------
3154 function P_If_Expression_Internal
3155 (Loc : Source_Ptr;
3156 Cond : Node_Id) return Node_Id
3158 Exprs : constant List_Id := New_List;
3159 Expr : Node_Id;
3160 State : Saved_Scan_State;
3161 Eptr : Source_Ptr;
3163 begin
3164 -- All cases except where we are at right paren
3166 if Token /= Tok_Right_Paren then
3167 TF_Then;
3168 Append_To (Exprs, P_Condition (Cond));
3169 Append_To (Exprs, P_Expression);
3171 -- Case of right paren (missing THEN phrase). Note that we know this
3172 -- is the IF case, since the caller dealt with this possibility in
3173 -- the ELSIF case.
3175 else
3176 Error_Msg_BC ("missing THEN phrase");
3177 Append_To (Exprs, P_Condition (Cond));
3178 end if;
3180 -- We now have scanned out IF expr THEN expr
3182 -- Check for common error of semicolon before the ELSE
3184 if Token = Tok_Semicolon then
3185 Save_Scan_State (State);
3186 Scan; -- past semicolon
3188 if Token = Tok_Else or else Token = Tok_Elsif then
3189 Error_Msg_SP -- CODEFIX
3190 ("|extra "";"" ignored");
3192 else
3193 Restore_Scan_State (State);
3194 end if;
3195 end if;
3197 -- Scan out ELSIF sequence if present
3199 if Token = Tok_Elsif then
3200 Eptr := Token_Ptr;
3201 Scan; -- past ELSIF
3202 Expr := P_Expression;
3204 -- If we are at a right paren, we assume the ELSIF should be ELSE
3206 if Token = Tok_Right_Paren then
3207 Error_Msg ("ELSIF should be ELSE", Eptr);
3208 Append_To (Exprs, Expr);
3210 -- Otherwise we have an OK ELSIF
3212 else
3213 Expr := P_If_Expression_Internal (Eptr, Expr);
3214 Set_Is_Elsif (Expr);
3215 Append_To (Exprs, Expr);
3216 end if;
3218 -- Scan out ELSE phrase if present
3220 elsif Token = Tok_Else then
3222 -- Scan out ELSE expression
3224 Scan; -- Past ELSE
3225 Append_To (Exprs, P_Expression);
3227 -- Skip redundant ELSE parts
3229 while Token = Tok_Else loop
3230 Error_Msg_SC ("only one ELSE part is allowed");
3231 Scan; -- past ELSE
3232 Discard_Junk_Node (P_Expression);
3233 end loop;
3235 -- Two expression case (implied True, filled in during semantics)
3237 else
3238 null;
3239 end if;
3241 -- If we have an END IF, diagnose as not needed
3243 if Token = Tok_End then
3244 Error_Msg_SC ("`END IF` not allowed at end of if expression");
3245 Scan; -- past END
3247 if Token = Tok_If then
3248 Scan; -- past IF;
3249 end if;
3250 end if;
3252 -- Return the If_Expression node
3254 return Make_If_Expression (Loc, Expressions => Exprs);
3255 end P_If_Expression_Internal;
3257 -- Local variables
3259 Loc : constant Source_Ptr := Token_Ptr;
3260 If_Expr : Node_Id;
3262 -- Start of processing for P_If_Expression
3264 begin
3265 Error_Msg_Ada_2012_Feature ("|if expression", Token_Ptr);
3266 Scan; -- past IF
3267 Inside_If_Expression := Inside_If_Expression + 1;
3268 If_Expr := P_If_Expression_Internal (Loc, P_Expression);
3269 Inside_If_Expression := Inside_If_Expression - 1;
3270 return If_Expr;
3271 end P_If_Expression;
3273 -----------------------
3274 -- P_Membership_Test --
3275 -----------------------
3277 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3278 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3280 procedure P_Membership_Test (N : Node_Id) is
3281 Alt : constant Node_Id :=
3282 P_Range_Or_Subtype_Mark
3283 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3285 begin
3286 -- Set case
3288 if Token = Tok_Vertical_Bar then
3289 Error_Msg_Ada_2012_Feature ("set notation", Token_Ptr);
3290 Set_Alternatives (N, New_List (Alt));
3291 Set_Right_Opnd (N, Empty);
3293 -- Loop to accumulate alternatives
3295 while Token = Tok_Vertical_Bar loop
3296 Scan; -- past vertical bar
3297 Append_To
3298 (Alternatives (N),
3299 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3300 end loop;
3302 -- Not set case
3304 else
3305 Set_Right_Opnd (N, Alt);
3306 Set_Alternatives (N, No_List);
3307 end if;
3308 end P_Membership_Test;
3310 ------------------------------------------
3311 -- P_Unparen_Cond_Case_Quant_Expression --
3312 ------------------------------------------
3314 function P_Unparen_Cond_Case_Quant_Expression return Node_Id is
3315 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3316 Result : Node_Id;
3318 begin
3319 -- Case expression
3321 if Token = Tok_Case then
3322 Result := P_Case_Expression;
3324 if not (Lparen and then Token = Tok_Right_Paren) then
3325 Error_Msg_N ("case expression must be parenthesized!", Result);
3326 end if;
3328 -- If expression
3330 elsif Token = Tok_If then
3331 Result := P_If_Expression;
3333 if not (Lparen and then Token = Tok_Right_Paren) then
3334 Error_Msg_N ("if expression must be parenthesized!", Result);
3335 end if;
3337 -- Quantified expression
3339 elsif Token = Tok_For then
3340 Result := P_Quantified_Expression;
3342 if not (Lparen and then Token = Tok_Right_Paren) then
3343 Error_Msg_N
3344 ("quantified expression must be parenthesized!", Result);
3345 end if;
3347 -- No other possibility should exist (caller was supposed to check)
3349 else
3350 raise Program_Error;
3351 end if;
3353 -- Return expression (possibly after having given message)
3355 return Result;
3356 end P_Unparen_Cond_Case_Quant_Expression;
3358 end Ch4;