Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / ada / par-ch4.adb
blob0db6d20a2ba1b407796e558593b3450156eef352
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-2007, 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 ---------------
36 -- Local map --
37 ---------------
39 Is_Parameterless_Attribute : constant Attribute_Class_Array :=
40 (Attribute_Body_Version => True,
41 Attribute_External_Tag => True,
42 Attribute_Img => True,
43 Attribute_Version => True,
44 Attribute_Base => True,
45 Attribute_Class => True,
46 Attribute_Stub_Type => True,
47 others => False);
48 -- This map contains True for parameterless attributes that return a
49 -- string or a type. For those attributes, a left parenthesis after
50 -- the attribute should not be analyzed as the beginning of a parameters
51 -- list because it may denote a slice operation (X'Img (1 .. 2)) or
52 -- a type conversion (X'Class (Y)).
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 function P_Aggregate_Or_Paren_Expr return Node_Id;
59 function P_Allocator return Node_Id;
60 function P_Record_Or_Array_Component_Association return Node_Id;
61 function P_Factor return Node_Id;
62 function P_Primary return Node_Id;
63 function P_Relation return Node_Id;
64 function P_Term return Node_Id;
66 function P_Binary_Adding_Operator return Node_Kind;
67 function P_Logical_Operator return Node_Kind;
68 function P_Multiplying_Operator return Node_Kind;
69 function P_Relational_Operator return Node_Kind;
70 function P_Unary_Adding_Operator return Node_Kind;
72 procedure Bad_Range_Attribute (Loc : Source_Ptr);
73 -- Called to place complaint about bad range attribute at the given
74 -- source location. Terminates by raising Error_Resync.
76 function P_Range_Attribute_Reference
77 (Prefix_Node : Node_Id)
78 return Node_Id;
79 -- Scan a range attribute reference. The caller has scanned out the
80 -- prefix. The current token is known to be an apostrophe and the
81 -- following token is known to be RANGE.
83 procedure Set_Op_Name (Node : Node_Id);
84 -- Procedure to set name field (Chars) in operator node
86 -------------------------
87 -- Bad_Range_Attribute --
88 -------------------------
90 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
91 begin
92 Error_Msg ("range attribute cannot be used in expression!", Loc);
93 Resync_Expression;
94 end Bad_Range_Attribute;
96 ------------------
97 -- Set_Op_Name --
98 ------------------
100 procedure Set_Op_Name (Node : Node_Id) is
101 type Name_Of_Type is array (N_Op) of Name_Id;
102 Name_Of : constant Name_Of_Type := Name_Of_Type'(
103 N_Op_And => Name_Op_And,
104 N_Op_Or => Name_Op_Or,
105 N_Op_Xor => Name_Op_Xor,
106 N_Op_Eq => Name_Op_Eq,
107 N_Op_Ne => Name_Op_Ne,
108 N_Op_Lt => Name_Op_Lt,
109 N_Op_Le => Name_Op_Le,
110 N_Op_Gt => Name_Op_Gt,
111 N_Op_Ge => Name_Op_Ge,
112 N_Op_Add => Name_Op_Add,
113 N_Op_Subtract => Name_Op_Subtract,
114 N_Op_Concat => Name_Op_Concat,
115 N_Op_Multiply => Name_Op_Multiply,
116 N_Op_Divide => Name_Op_Divide,
117 N_Op_Mod => Name_Op_Mod,
118 N_Op_Rem => Name_Op_Rem,
119 N_Op_Expon => Name_Op_Expon,
120 N_Op_Plus => Name_Op_Add,
121 N_Op_Minus => Name_Op_Subtract,
122 N_Op_Abs => Name_Op_Abs,
123 N_Op_Not => Name_Op_Not,
125 -- We don't really need these shift operators, since they never
126 -- appear as operators in the source, but the path of least
127 -- resistance is to put them in (the aggregate must be complete)
129 N_Op_Rotate_Left => Name_Rotate_Left,
130 N_Op_Rotate_Right => Name_Rotate_Right,
131 N_Op_Shift_Left => Name_Shift_Left,
132 N_Op_Shift_Right => Name_Shift_Right,
133 N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
135 begin
136 if Nkind (Node) in N_Op then
137 Set_Chars (Node, Name_Of (Nkind (Node)));
138 end if;
139 end Set_Op_Name;
141 --------------------------
142 -- 4.1 Name (also 6.4) --
143 --------------------------
145 -- NAME ::=
146 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
147 -- | INDEXED_COMPONENT | SLICE
148 -- | SELECTED_COMPONENT | ATTRIBUTE
149 -- | TYPE_CONVERSION | FUNCTION_CALL
150 -- | CHARACTER_LITERAL
152 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
154 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
156 -- EXPLICIT_DEREFERENCE ::= NAME . all
158 -- IMPLICIT_DEREFERENCE ::= NAME
160 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
162 -- SLICE ::= PREFIX (DISCRETE_RANGE)
164 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
166 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
168 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
170 -- ATTRIBUTE_DESIGNATOR ::=
171 -- IDENTIFIER [(static_EXPRESSION)]
172 -- | access | delta | digits
174 -- FUNCTION_CALL ::=
175 -- function_NAME
176 -- | function_PREFIX ACTUAL_PARAMETER_PART
178 -- ACTUAL_PARAMETER_PART ::=
179 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
181 -- PARAMETER_ASSOCIATION ::=
182 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
184 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
186 -- Note: syntactically a procedure call looks just like a function call,
187 -- so this routine is in practice used to scan out procedure calls as well.
189 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
191 -- Error recovery: can raise Error_Resync
193 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
194 -- followed by either a left paren (qualified expression case), or by
195 -- range (range attribute case). All other uses of apostrophe (i.e. all
196 -- other attributes) are handled in this routine.
198 -- Error recovery: can raise Error_Resync
200 function P_Name return Node_Id is
201 Scan_State : Saved_Scan_State;
202 Name_Node : Node_Id;
203 Prefix_Node : Node_Id;
204 Ident_Node : Node_Id;
205 Expr_Node : Node_Id;
206 Range_Node : Node_Id;
207 Arg_Node : Node_Id;
209 Arg_List : List_Id := No_List; -- kill junk warning
210 Attr_Name : Name_Id := No_Name; -- kill junk warning
212 begin
213 -- Case of not a name
215 if Token not in Token_Class_Name then
217 -- If it looks like start of expression, complain and scan expression
219 if Token in Token_Class_Literal
220 or else Token = Tok_Left_Paren
221 then
222 Error_Msg_SC ("name expected");
223 return P_Expression;
225 -- Otherwise some other junk, not much we can do
227 else
228 Error_Msg_AP ("name expected");
229 raise Error_Resync;
230 end if;
231 end if;
233 -- Loop through designators in qualified name
235 Name_Node := Token_Node;
237 loop
238 Scan; -- past designator
239 exit when Token /= Tok_Dot;
240 Save_Scan_State (Scan_State); -- at dot
241 Scan; -- past dot
243 -- If we do not have another designator after the dot, then join
244 -- the normal circuit to handle a dot extension (may be .all or
245 -- character literal case). Otherwise loop back to scan the next
246 -- designator.
248 if Token not in Token_Class_Desig then
249 goto Scan_Name_Extension_Dot;
250 else
251 Prefix_Node := Name_Node;
252 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
253 Set_Prefix (Name_Node, Prefix_Node);
254 Set_Selector_Name (Name_Node, Token_Node);
255 end if;
256 end loop;
258 -- We have now scanned out a qualified designator. If the last token is
259 -- an operator symbol, then we certainly do not have the Snam case, so
260 -- we can just use the normal name extension check circuit
262 if Prev_Token = Tok_Operator_Symbol then
263 goto Scan_Name_Extension;
264 end if;
266 -- We have scanned out a qualified simple name, check for name extension
267 -- Note that we know there is no dot here at this stage, so the only
268 -- possible cases of name extension are apostrophe and left paren.
270 if Token = Tok_Apostrophe then
271 Save_Scan_State (Scan_State); -- at apostrophe
272 Scan; -- past apostrophe
274 -- If left paren, then this might be a qualified expression, but we
275 -- are only in the business of scanning out names, so return with
276 -- Token backed up to point to the apostrophe. The treatment for
277 -- the range attribute is similar (we do not consider x'range to
278 -- be a name in this grammar).
280 if Token = Tok_Left_Paren or else Token = Tok_Range then
281 Restore_Scan_State (Scan_State); -- to apostrophe
282 Expr_Form := EF_Simple_Name;
283 return Name_Node;
285 -- Otherwise we have the case of a name extended by an attribute
287 else
288 goto Scan_Name_Extension_Apostrophe;
289 end if;
291 -- Check case of qualified simple name extended by a left parenthesis
293 elsif Token = Tok_Left_Paren then
294 Scan; -- past left paren
295 goto Scan_Name_Extension_Left_Paren;
297 -- Otherwise the qualified simple name is not extended, so return
299 else
300 Expr_Form := EF_Simple_Name;
301 return Name_Node;
302 end if;
304 -- Loop scanning past name extensions. A label is used for control
305 -- transfer for this loop for ease of interfacing with the finite state
306 -- machine in the parenthesis scanning circuit, and also to allow for
307 -- passing in control to the appropriate point from the above code.
309 <<Scan_Name_Extension>>
311 -- Character literal used as name cannot be extended. Also this
312 -- cannot be a call, since the name for a call must be a designator.
313 -- Return in these cases, or if there is no name extension
315 if Token not in Token_Class_Namext
316 or else Prev_Token = Tok_Char_Literal
317 then
318 Expr_Form := EF_Name;
319 return Name_Node;
320 end if;
322 -- Merge here when we know there is a name extension
324 <<Scan_Name_Extension_OK>>
326 if Token = Tok_Left_Paren then
327 Scan; -- past left paren
328 goto Scan_Name_Extension_Left_Paren;
330 elsif Token = Tok_Apostrophe then
331 Save_Scan_State (Scan_State); -- at apostrophe
332 Scan; -- past apostrophe
333 goto Scan_Name_Extension_Apostrophe;
335 else -- Token = Tok_Dot
336 Save_Scan_State (Scan_State); -- at dot
337 Scan; -- past dot
338 goto Scan_Name_Extension_Dot;
339 end if;
341 -- Case of name extended by dot (selection), dot is already skipped
342 -- and the scan state at the point of the dot is saved in Scan_State.
344 <<Scan_Name_Extension_Dot>>
346 -- Explicit dereference case
348 if Token = Tok_All then
349 Prefix_Node := Name_Node;
350 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
351 Set_Prefix (Name_Node, Prefix_Node);
352 Scan; -- past ALL
353 goto Scan_Name_Extension;
355 -- Selected component case
357 elsif Token in Token_Class_Name then
358 Prefix_Node := Name_Node;
359 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
360 Set_Prefix (Name_Node, Prefix_Node);
361 Set_Selector_Name (Name_Node, Token_Node);
362 Scan; -- past selector
363 goto Scan_Name_Extension;
365 -- Reserved identifier as selector
367 elsif Is_Reserved_Identifier then
368 Scan_Reserved_Identifier (Force_Msg => False);
369 Prefix_Node := Name_Node;
370 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
371 Set_Prefix (Name_Node, Prefix_Node);
372 Set_Selector_Name (Name_Node, Token_Node);
373 Scan; -- past identifier used as selector
374 goto Scan_Name_Extension;
376 -- If dot is at end of line and followed by nothing legal,
377 -- then assume end of name and quit (dot will be taken as
378 -- an erroneous form of some other punctuation by our caller).
380 elsif Token_Is_At_Start_Of_Line then
381 Restore_Scan_State (Scan_State);
382 return Name_Node;
384 -- Here if nothing legal after the dot
386 else
387 Error_Msg_AP ("selector expected");
388 raise Error_Resync;
389 end if;
391 -- Here for an apostrophe as name extension. The scan position at the
392 -- apostrophe has already been saved, and the apostrophe scanned out.
394 <<Scan_Name_Extension_Apostrophe>>
396 Scan_Apostrophe : declare
397 function Apostrophe_Should_Be_Semicolon return Boolean;
398 -- Checks for case where apostrophe should probably be
399 -- a semicolon, and if so, gives appropriate message,
400 -- resets the scan pointer to the apostrophe, changes
401 -- the current token to Tok_Semicolon, and returns True.
402 -- Otherwise returns False.
404 function Apostrophe_Should_Be_Semicolon return Boolean is
405 begin
406 if Token_Is_At_Start_Of_Line then
407 Restore_Scan_State (Scan_State); -- to apostrophe
408 Error_Msg_SC ("""''"" should be "";""");
409 Token := Tok_Semicolon;
410 return True;
411 else
412 return False;
413 end if;
414 end Apostrophe_Should_Be_Semicolon;
416 -- Start of processing for Scan_Apostrophe
418 begin
419 -- If range attribute after apostrophe, then return with Token
420 -- pointing to the apostrophe. Note that in this case the prefix
421 -- need not be a simple name (cases like A.all'range). Similarly
422 -- if there is a left paren after the apostrophe, then we also
423 -- return with Token pointing to the apostrophe (this is the
424 -- qualified expression case).
426 if Token = Tok_Range or else Token = Tok_Left_Paren then
427 Restore_Scan_State (Scan_State); -- to apostrophe
428 Expr_Form := EF_Name;
429 return Name_Node;
431 -- Here for cases where attribute designator is an identifier
433 elsif Token = Tok_Identifier then
434 Attr_Name := Token_Name;
436 if not Is_Attribute_Name (Attr_Name) then
437 if Apostrophe_Should_Be_Semicolon then
438 Expr_Form := EF_Name;
439 return Name_Node;
441 -- Here for a bad attribute name
443 else
444 Signal_Bad_Attribute;
445 Scan; -- past bad identifier
447 if Token = Tok_Left_Paren then
448 Scan; -- past left paren
450 loop
451 Discard_Junk_Node (P_Expression);
452 exit when not Comma_Present;
453 end loop;
455 T_Right_Paren;
456 end if;
458 return Error;
459 end if;
460 end if;
462 if Style_Check then
463 Style.Check_Attribute_Name (False);
464 end if;
466 -- Here for case of attribute designator is not an identifier
468 else
469 if Token = Tok_Delta then
470 Attr_Name := Name_Delta;
472 elsif Token = Tok_Digits then
473 Attr_Name := Name_Digits;
475 elsif Token = Tok_Access then
476 Attr_Name := Name_Access;
478 elsif Token = Tok_Mod and then Ada_Version = Ada_05 then
479 Attr_Name := Name_Mod;
481 elsif Apostrophe_Should_Be_Semicolon then
482 Expr_Form := EF_Name;
483 return Name_Node;
485 else
486 Error_Msg_AP ("attribute designator expected");
487 raise Error_Resync;
488 end if;
490 if Style_Check then
491 Style.Check_Attribute_Name (True);
492 end if;
493 end if;
495 -- We come here with an OK attribute scanned, and the
496 -- corresponding Attribute identifier node stored in Ident_Node.
498 Prefix_Node := Name_Node;
499 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
500 Scan; -- past attribute designator
501 Set_Prefix (Name_Node, Prefix_Node);
502 Set_Attribute_Name (Name_Node, Attr_Name);
504 -- Scan attribute arguments/designator
506 if Token = Tok_Left_Paren
507 and then
508 not Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
509 then
510 Set_Expressions (Name_Node, New_List);
511 Scan; -- past left paren
513 loop
514 declare
515 Expr : constant Node_Id := P_Expression;
517 begin
518 if Token = Tok_Arrow then
519 Error_Msg_SC
520 ("named parameters not permitted for attributes");
521 Scan; -- past junk arrow
523 else
524 Append (Expr, Expressions (Name_Node));
525 exit when not Comma_Present;
526 end if;
527 end;
528 end loop;
530 T_Right_Paren;
531 end if;
533 goto Scan_Name_Extension;
534 end Scan_Apostrophe;
536 -- Here for left parenthesis extending name (left paren skipped)
538 <<Scan_Name_Extension_Left_Paren>>
540 -- We now have to scan through a list of items, terminated by a
541 -- right parenthesis. The scan is handled by a finite state
542 -- machine. The possibilities are:
544 -- (discrete_range)
546 -- This is a slice. This case is handled in LP_State_Init
548 -- (expression, expression, ..)
550 -- This is interpreted as an indexed component, i.e. as a
551 -- case of a name which can be extended in the normal manner.
552 -- This case is handled by LP_State_Name or LP_State_Expr.
554 -- (..., identifier => expression , ...)
556 -- If there is at least one occurrence of identifier => (but
557 -- none of the other cases apply), then we have a call.
559 -- Test for Id => case
561 if Token = Tok_Identifier then
562 Save_Scan_State (Scan_State); -- at Id
563 Scan; -- past Id
565 -- Test for => (allow := as an error substitute)
567 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
568 Restore_Scan_State (Scan_State); -- to Id
569 Arg_List := New_List;
570 goto LP_State_Call;
572 else
573 Restore_Scan_State (Scan_State); -- to Id
574 end if;
575 end if;
577 -- Here we have an expression after all
579 Expr_Node := P_Expression_Or_Range_Attribute;
581 -- Check cases of discrete range for a slice
583 -- First possibility: Range_Attribute_Reference
585 if Expr_Form = EF_Range_Attr then
586 Range_Node := Expr_Node;
588 -- Second possibility: Simple_expression .. Simple_expression
590 elsif Token = Tok_Dot_Dot then
591 Check_Simple_Expression (Expr_Node);
592 Range_Node := New_Node (N_Range, Token_Ptr);
593 Set_Low_Bound (Range_Node, Expr_Node);
594 Scan; -- past ..
595 Expr_Node := P_Expression;
596 Check_Simple_Expression (Expr_Node);
597 Set_High_Bound (Range_Node, Expr_Node);
599 -- Third possibility: Type_name range Range
601 elsif Token = Tok_Range then
602 if Expr_Form /= EF_Simple_Name then
603 Error_Msg_SC ("subtype mark must precede RANGE");
604 raise Error_Resync;
605 end if;
607 Range_Node := P_Subtype_Indication (Expr_Node);
609 -- Otherwise we just have an expression. It is true that we might
610 -- have a subtype mark without a range constraint but this case
611 -- is syntactically indistinguishable from the expression case.
613 else
614 Arg_List := New_List;
615 goto LP_State_Expr;
616 end if;
618 -- Fall through here with unmistakable Discrete range scanned,
619 -- which means that we definitely have the case of a slice. The
620 -- Discrete range is in Range_Node.
622 if Token = Tok_Comma then
623 Error_Msg_SC ("slice cannot have more than one dimension");
624 raise Error_Resync;
626 elsif Token /= Tok_Right_Paren then
627 T_Right_Paren;
628 raise Error_Resync;
630 else
631 Scan; -- past right paren
632 Prefix_Node := Name_Node;
633 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
634 Set_Prefix (Name_Node, Prefix_Node);
635 Set_Discrete_Range (Name_Node, Range_Node);
637 -- An operator node is legal as a prefix to other names,
638 -- but not for a slice.
640 if Nkind (Prefix_Node) = N_Operator_Symbol then
641 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
642 end if;
644 -- If we have a name extension, go scan it
646 if Token in Token_Class_Namext then
647 goto Scan_Name_Extension_OK;
649 -- Otherwise return (a slice is a name, but is not a call)
651 else
652 Expr_Form := EF_Name;
653 return Name_Node;
654 end if;
655 end if;
657 -- In LP_State_Expr, we have scanned one or more expressions, and
658 -- so we have a call or an indexed component which is a name. On
659 -- entry we have the expression just scanned in Expr_Node and
660 -- Arg_List contains the list of expressions encountered so far
662 <<LP_State_Expr>>
663 Append (Expr_Node, Arg_List);
665 if Token = Tok_Arrow then
666 Error_Msg
667 ("expect identifier in parameter association",
668 Sloc (Expr_Node));
669 Scan; -- past arrow.
671 elsif not Comma_Present then
672 T_Right_Paren;
673 Prefix_Node := Name_Node;
674 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
675 Set_Prefix (Name_Node, Prefix_Node);
676 Set_Expressions (Name_Node, Arg_List);
677 goto Scan_Name_Extension;
678 end if;
680 -- Comma present (and scanned out), test for identifier => case
681 -- Test for identifier => case
683 if Token = Tok_Identifier then
684 Save_Scan_State (Scan_State); -- at Id
685 Scan; -- past Id
687 -- Test for => (allow := as error substitute)
689 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
690 Restore_Scan_State (Scan_State); -- to Id
691 goto LP_State_Call;
693 -- Otherwise it's just an expression after all, so backup
695 else
696 Restore_Scan_State (Scan_State); -- to Id
697 end if;
698 end if;
700 -- Here we have an expression after all, so stay in this state
702 Expr_Node := P_Expression;
703 goto LP_State_Expr;
705 -- LP_State_Call corresponds to the situation in which at least
706 -- one instance of Id => Expression has been encountered, so we
707 -- know that we do not have a name, but rather a call. We enter
708 -- it with the scan pointer pointing to the next argument to scan,
709 -- and Arg_List containing the list of arguments scanned so far.
711 <<LP_State_Call>>
713 -- Test for case of Id => Expression (named parameter)
715 if Token = Tok_Identifier then
716 Save_Scan_State (Scan_State); -- at Id
717 Ident_Node := Token_Node;
718 Scan; -- past Id
720 -- Deal with => (allow := as erroneous substitute)
722 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
723 Arg_Node :=
724 New_Node (N_Parameter_Association, Prev_Token_Ptr);
725 Set_Selector_Name (Arg_Node, Ident_Node);
726 T_Arrow;
727 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
728 Append (Arg_Node, Arg_List);
730 -- If a comma follows, go back and scan next entry
732 if Comma_Present then
733 goto LP_State_Call;
735 -- Otherwise we have the end of a call
737 else
738 Prefix_Node := Name_Node;
739 Name_Node :=
740 New_Node (N_Function_Call, Sloc (Prefix_Node));
741 Set_Name (Name_Node, Prefix_Node);
742 Set_Parameter_Associations (Name_Node, Arg_List);
743 T_Right_Paren;
745 if Token in Token_Class_Namext then
746 goto Scan_Name_Extension_OK;
748 -- This is a case of a call which cannot be a name
750 else
751 Expr_Form := EF_Name;
752 return Name_Node;
753 end if;
754 end if;
756 -- Not named parameter: Id started an expression after all
758 else
759 Restore_Scan_State (Scan_State); -- to Id
760 end if;
761 end if;
763 -- Here if entry did not start with Id => which means that it
764 -- is a positional parameter, which is not allowed, since we
765 -- have seen at least one named parameter already.
767 Error_Msg_SC
768 ("positional parameter association " &
769 "not allowed after named one");
771 Expr_Node := P_Expression;
773 -- Leaving the '>' in an association is not unusual, so suggest
774 -- a possible fix.
776 if Nkind (Expr_Node) = N_Op_Eq then
777 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
778 end if;
780 -- We go back to scanning out expressions, so that we do not get
781 -- multiple error messages when several positional parameters
782 -- follow a named parameter.
784 goto LP_State_Expr;
786 -- End of treatment for name extensions starting with left paren
788 -- End of loop through name extensions
790 end P_Name;
792 -- This function parses a restricted form of Names which are either
793 -- designators, or designators preceded by a sequence of prefixes
794 -- that are direct names.
796 -- Error recovery: cannot raise Error_Resync
798 function P_Function_Name return Node_Id is
799 Designator_Node : Node_Id;
800 Prefix_Node : Node_Id;
801 Selector_Node : Node_Id;
802 Dot_Sloc : Source_Ptr := No_Location;
804 begin
805 -- Prefix_Node is set to the gathered prefix so far, Empty means that
806 -- no prefix has been scanned. This allows us to build up the result
807 -- in the required right recursive manner.
809 Prefix_Node := Empty;
811 -- Loop through prefixes
813 loop
814 Designator_Node := Token_Node;
816 if Token not in Token_Class_Desig then
817 return P_Identifier; -- let P_Identifier issue the error message
819 else -- Token in Token_Class_Desig
820 Scan; -- past designator
821 exit when Token /= Tok_Dot;
822 end if;
824 -- Here at a dot, with token just before it in Designator_Node
826 if No (Prefix_Node) then
827 Prefix_Node := Designator_Node;
828 else
829 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
830 Set_Prefix (Selector_Node, Prefix_Node);
831 Set_Selector_Name (Selector_Node, Designator_Node);
832 Prefix_Node := Selector_Node;
833 end if;
835 Dot_Sloc := Token_Ptr;
836 Scan; -- past dot
837 end loop;
839 -- Fall out of the loop having just scanned a designator
841 if No (Prefix_Node) then
842 return Designator_Node;
843 else
844 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
845 Set_Prefix (Selector_Node, Prefix_Node);
846 Set_Selector_Name (Selector_Node, Designator_Node);
847 return Selector_Node;
848 end if;
850 exception
851 when Error_Resync =>
852 return Error;
854 end P_Function_Name;
856 -- This function parses a restricted form of Names which are either
857 -- identifiers, or identifiers preceded by a sequence of prefixes
858 -- that are direct names.
860 -- Error recovery: cannot raise Error_Resync
862 function P_Qualified_Simple_Name return Node_Id is
863 Designator_Node : Node_Id;
864 Prefix_Node : Node_Id;
865 Selector_Node : Node_Id;
866 Dot_Sloc : Source_Ptr := No_Location;
868 begin
869 -- Prefix node is set to the gathered prefix so far, Empty means that
870 -- no prefix has been scanned. This allows us to build up the result
871 -- in the required right recursive manner.
873 Prefix_Node := Empty;
875 -- Loop through prefixes
877 loop
878 Designator_Node := Token_Node;
880 if Token = Tok_Identifier then
881 Scan; -- past identifier
882 exit when Token /= Tok_Dot;
884 elsif Token not in Token_Class_Desig then
885 return P_Identifier; -- let P_Identifier issue the error message
887 else
888 Scan; -- past designator
890 if Token /= Tok_Dot then
891 Error_Msg_SP ("identifier expected");
892 return Error;
893 end if;
894 end if;
896 -- Here at a dot, with token just before it in Designator_Node
898 if No (Prefix_Node) then
899 Prefix_Node := Designator_Node;
900 else
901 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
902 Set_Prefix (Selector_Node, Prefix_Node);
903 Set_Selector_Name (Selector_Node, Designator_Node);
904 Prefix_Node := Selector_Node;
905 end if;
907 Dot_Sloc := Token_Ptr;
908 Scan; -- past dot
909 end loop;
911 -- Fall out of the loop having just scanned an identifier
913 if No (Prefix_Node) then
914 return Designator_Node;
915 else
916 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
917 Set_Prefix (Selector_Node, Prefix_Node);
918 Set_Selector_Name (Selector_Node, Designator_Node);
919 return Selector_Node;
920 end if;
922 exception
923 when Error_Resync =>
924 return Error;
926 end P_Qualified_Simple_Name;
928 -- This procedure differs from P_Qualified_Simple_Name only in that it
929 -- raises Error_Resync if any error is encountered. It only returns after
930 -- scanning a valid qualified simple name.
932 -- Error recovery: can raise Error_Resync
934 function P_Qualified_Simple_Name_Resync return Node_Id is
935 Designator_Node : Node_Id;
936 Prefix_Node : Node_Id;
937 Selector_Node : Node_Id;
938 Dot_Sloc : Source_Ptr := No_Location;
940 begin
941 Prefix_Node := Empty;
943 -- Loop through prefixes
945 loop
946 Designator_Node := Token_Node;
948 if Token = Tok_Identifier then
949 Scan; -- past identifier
950 exit when Token /= Tok_Dot;
952 elsif Token not in Token_Class_Desig then
953 Discard_Junk_Node (P_Identifier); -- to issue the error message
954 raise Error_Resync;
956 else
957 Scan; -- past designator
959 if Token /= Tok_Dot then
960 Error_Msg_SP ("identifier expected");
961 raise Error_Resync;
962 end if;
963 end if;
965 -- Here at a dot, with token just before it in Designator_Node
967 if No (Prefix_Node) then
968 Prefix_Node := Designator_Node;
969 else
970 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
971 Set_Prefix (Selector_Node, Prefix_Node);
972 Set_Selector_Name (Selector_Node, Designator_Node);
973 Prefix_Node := Selector_Node;
974 end if;
976 Dot_Sloc := Token_Ptr;
977 Scan; -- past period
978 end loop;
980 -- Fall out of the loop having just scanned an identifier
982 if No (Prefix_Node) then
983 return Designator_Node;
984 else
985 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
986 Set_Prefix (Selector_Node, Prefix_Node);
987 Set_Selector_Name (Selector_Node, Designator_Node);
988 return Selector_Node;
989 end if;
991 end P_Qualified_Simple_Name_Resync;
993 ----------------------
994 -- 4.1 Direct_Name --
995 ----------------------
997 -- Parsed by P_Name and other functions in section 4.1
999 -----------------
1000 -- 4.1 Prefix --
1001 -----------------
1003 -- Parsed by P_Name (4.1)
1005 -------------------------------
1006 -- 4.1 Explicit Dereference --
1007 -------------------------------
1009 -- Parsed by P_Name (4.1)
1011 -------------------------------
1012 -- 4.1 Implicit_Dereference --
1013 -------------------------------
1015 -- Parsed by P_Name (4.1)
1017 ----------------------------
1018 -- 4.1 Indexed Component --
1019 ----------------------------
1021 -- Parsed by P_Name (4.1)
1023 ----------------
1024 -- 4.1 Slice --
1025 ----------------
1027 -- Parsed by P_Name (4.1)
1029 -----------------------------
1030 -- 4.1 Selected_Component --
1031 -----------------------------
1033 -- Parsed by P_Name (4.1)
1035 ------------------------
1036 -- 4.1 Selector Name --
1037 ------------------------
1039 -- Parsed by P_Name (4.1)
1041 ------------------------------
1042 -- 4.1 Attribute Reference --
1043 ------------------------------
1045 -- Parsed by P_Name (4.1)
1047 -------------------------------
1048 -- 4.1 Attribute Designator --
1049 -------------------------------
1051 -- Parsed by P_Name (4.1)
1053 --------------------------------------
1054 -- 4.1.4 Range Attribute Reference --
1055 --------------------------------------
1057 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1059 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1061 -- In the grammar, a RANGE attribute is simply a name, but its use is
1062 -- highly restricted, so in the parser, we do not regard it as a name.
1063 -- Instead, P_Name returns without scanning the 'RANGE part of the
1064 -- attribute, and the caller uses the following function to construct
1065 -- a range attribute in places where it is appropriate.
1067 -- Note that RANGE here is treated essentially as an identifier,
1068 -- rather than a reserved word.
1070 -- The caller has parsed the prefix, i.e. a name, and Token points to
1071 -- the apostrophe. The token after the apostrophe is known to be RANGE
1072 -- at this point. The prefix node becomes the prefix of the attribute.
1074 -- Error_Recovery: Cannot raise Error_Resync
1076 function P_Range_Attribute_Reference
1077 (Prefix_Node : Node_Id)
1078 return Node_Id
1080 Attr_Node : Node_Id;
1082 begin
1083 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1084 Set_Prefix (Attr_Node, Prefix_Node);
1085 Scan; -- past apostrophe
1087 if Style_Check then
1088 Style.Check_Attribute_Name (True);
1089 end if;
1091 Set_Attribute_Name (Attr_Node, Name_Range);
1092 Scan; -- past RANGE
1094 if Token = Tok_Left_Paren then
1095 Scan; -- past left paren
1096 Set_Expressions (Attr_Node, New_List (P_Expression));
1097 T_Right_Paren;
1098 end if;
1100 return Attr_Node;
1101 end P_Range_Attribute_Reference;
1103 ---------------------------------------
1104 -- 4.1.4 Range Attribute Designator --
1105 ---------------------------------------
1107 -- Parsed by P_Range_Attribute_Reference (4.4)
1109 --------------------
1110 -- 4.3 Aggregate --
1111 --------------------
1113 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1115 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1116 -- an aggregate is known to be required (code statement, extension
1117 -- aggregate), in which cases this routine performs the necessary check
1118 -- that we have an aggregate rather than a parenthesized expression
1120 -- Error recovery: can raise Error_Resync
1122 function P_Aggregate return Node_Id is
1123 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1124 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1126 begin
1127 if Nkind (Aggr_Node) /= N_Aggregate
1128 and then
1129 Nkind (Aggr_Node) /= N_Extension_Aggregate
1130 then
1131 Error_Msg
1132 ("aggregate may not have single positional component", Aggr_Sloc);
1133 return Error;
1134 else
1135 return Aggr_Node;
1136 end if;
1137 end P_Aggregate;
1139 -------------------------------------------------
1140 -- 4.3 Aggregate or Parenthesized Expresssion --
1141 -------------------------------------------------
1143 -- This procedure parses out either an aggregate or a parenthesized
1144 -- expression (these two constructs are closely related, since a
1145 -- parenthesized expression looks like an aggregate with a single
1146 -- positional component).
1148 -- AGGREGATE ::=
1149 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1151 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1153 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1154 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1155 -- | null record
1157 -- RECORD_COMPONENT_ASSOCIATION ::=
1158 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1160 -- COMPONENT_CHOICE_LIST ::=
1161 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1162 -- | others
1164 -- EXTENSION_AGGREGATE ::=
1165 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1167 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1169 -- ARRAY_AGGREGATE ::=
1170 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1172 -- POSITIONAL_ARRAY_AGGREGATE ::=
1173 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1174 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1175 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1177 -- NAMED_ARRAY_AGGREGATE ::=
1178 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1180 -- PRIMARY ::= (EXPRESSION);
1182 -- Error recovery: can raise Error_Resync
1184 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1185 -- to Ada 2005 limited aggregates (AI-287)
1187 function P_Aggregate_Or_Paren_Expr return Node_Id is
1188 Aggregate_Node : Node_Id;
1189 Expr_List : List_Id;
1190 Assoc_List : List_Id;
1191 Expr_Node : Node_Id;
1192 Lparen_Sloc : Source_Ptr;
1193 Scan_State : Saved_Scan_State;
1195 begin
1196 Lparen_Sloc := Token_Ptr;
1197 T_Left_Paren;
1199 -- Note: the mechanism used here of rescanning the initial expression
1200 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1201 -- out the discrete choice list.
1203 -- Deal with expression and extension aggregate cases first
1205 if Token /= Tok_Others then
1206 Save_Scan_State (Scan_State); -- at start of expression
1208 -- Deal with (NULL RECORD) case
1210 if Token = Tok_Null then
1211 Scan; -- past NULL
1213 if Token = Tok_Record then
1214 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1215 Set_Null_Record_Present (Aggregate_Node, True);
1216 Scan; -- past RECORD
1217 T_Right_Paren;
1218 return Aggregate_Node;
1219 else
1220 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1221 end if;
1222 end if;
1224 -- Ada 2005 (AI-287): The box notation is allowed only with named
1225 -- notation because positional notation might be error prone. For
1226 -- example, in "(X, <>, Y, <>)", there is no type associated with
1227 -- the boxes, so you might not be leaving out the components you
1228 -- thought you were leaving out.
1230 if Ada_Version >= Ada_05 and then Token = Tok_Box then
1231 Error_Msg_SC ("(Ada 2005) box notation only allowed with "
1232 & "named notation");
1233 Scan; -- past BOX
1234 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1235 return Aggregate_Node;
1236 end if;
1238 Expr_Node := P_Expression_Or_Range_Attribute;
1240 -- Extension aggregate case
1242 if Token = Tok_With then
1244 if Nkind (Expr_Node) = N_Attribute_Reference
1245 and then Attribute_Name (Expr_Node) = Name_Range
1246 then
1247 Bad_Range_Attribute (Sloc (Expr_Node));
1248 return Error;
1249 end if;
1251 if Ada_Version = Ada_83 then
1252 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1253 end if;
1255 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1256 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1257 Scan; -- past WITH
1259 -- Deal with WITH NULL RECORD case
1261 if Token = Tok_Null then
1262 Save_Scan_State (Scan_State); -- at NULL
1263 Scan; -- past NULL
1265 if Token = Tok_Record then
1266 Scan; -- past RECORD
1267 Set_Null_Record_Present (Aggregate_Node, True);
1268 T_Right_Paren;
1269 return Aggregate_Node;
1271 else
1272 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1273 end if;
1274 end if;
1276 if Token /= Tok_Others then
1277 Save_Scan_State (Scan_State);
1278 Expr_Node := P_Expression;
1279 else
1280 Expr_Node := Empty;
1281 end if;
1283 -- Expression case
1285 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1286 if Nkind (Expr_Node) = N_Attribute_Reference
1287 and then Attribute_Name (Expr_Node) = Name_Range
1288 then
1289 Error_Msg
1290 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1291 Scan; -- past right paren
1292 return Expr_Node;
1293 end if;
1295 -- Bump paren count of expression
1297 if Expr_Node /= Error then
1298 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1299 end if;
1301 T_Right_Paren; -- past right paren (error message if none)
1302 return Expr_Node;
1304 -- Normal aggregate case
1306 else
1307 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1308 end if;
1310 -- Others case
1312 else
1313 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1314 Expr_Node := Empty;
1315 end if;
1317 -- Prepare to scan list of component associations
1319 Expr_List := No_List; -- don't set yet, maybe all named entries
1320 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1322 -- This loop scans through component associations. On entry to the
1323 -- loop, an expression has been scanned at the start of the current
1324 -- association unless initial token was OTHERS, in which case
1325 -- Expr_Node is set to Empty.
1327 loop
1328 -- Deal with others association first. This is a named association
1330 if No (Expr_Node) then
1331 if No (Assoc_List) then
1332 Assoc_List := New_List;
1333 end if;
1335 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1337 -- Improper use of WITH
1339 elsif Token = Tok_With then
1340 Error_Msg_SC ("WITH must be preceded by single expression in " &
1341 "extension aggregate");
1342 raise Error_Resync;
1344 -- A range attribute can only appear as part of a discrete choice
1345 -- list.
1347 elsif Nkind (Expr_Node) = N_Attribute_Reference
1348 and then Attribute_Name (Expr_Node) = Name_Range
1349 and then Token /= Tok_Arrow
1350 and then Token /= Tok_Vertical_Bar
1351 then
1352 Bad_Range_Attribute (Sloc (Expr_Node));
1353 return Error;
1355 -- Assume positional case if comma, right paren, or literal or
1356 -- identifier or OTHERS follows (the latter cases are missing
1357 -- comma cases). Also assume positional if a semicolon follows,
1358 -- which can happen if there are missing parens
1360 elsif Token = Tok_Comma
1361 or else Token = Tok_Right_Paren
1362 or else Token = Tok_Others
1363 or else Token in Token_Class_Lit_Or_Name
1364 or else Token = Tok_Semicolon
1365 then
1366 if Present (Assoc_List) then
1367 Error_Msg_BC
1368 ("""='>"" expected (positional association cannot follow " &
1369 "named association)");
1370 end if;
1372 if No (Expr_List) then
1373 Expr_List := New_List;
1374 end if;
1376 Append (Expr_Node, Expr_List);
1378 -- Anything else is assumed to be a named association
1380 else
1381 Restore_Scan_State (Scan_State); -- to start of expression
1383 if No (Assoc_List) then
1384 Assoc_List := New_List;
1385 end if;
1387 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1388 end if;
1390 exit when not Comma_Present;
1392 -- If we are at an expression terminator, something is seriously
1393 -- wrong, so let's get out now, before we start eating up stuff
1394 -- that doesn't belong to us!
1396 if Token in Token_Class_Eterm then
1397 Error_Msg_AP ("expecting expression or component association");
1398 exit;
1399 end if;
1401 -- Otherwise initiate for reentry to top of loop by scanning an
1402 -- initial expression, unless the first token is OTHERS.
1404 if Token = Tok_Others then
1405 Expr_Node := Empty;
1406 else
1407 Save_Scan_State (Scan_State); -- at start of expression
1408 Expr_Node := P_Expression_Or_Range_Attribute;
1410 end if;
1411 end loop;
1413 -- All component associations (positional and named) have been scanned
1415 T_Right_Paren;
1416 Set_Expressions (Aggregate_Node, Expr_List);
1417 Set_Component_Associations (Aggregate_Node, Assoc_List);
1418 return Aggregate_Node;
1419 end P_Aggregate_Or_Paren_Expr;
1421 ------------------------------------------------
1422 -- 4.3 Record or Array Component Association --
1423 ------------------------------------------------
1425 -- RECORD_COMPONENT_ASSOCIATION ::=
1426 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1427 -- | COMPONENT_CHOICE_LIST => <>
1429 -- COMPONENT_CHOICE_LIST =>
1430 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1431 -- | others
1433 -- ARRAY_COMPONENT_ASSOCIATION ::=
1434 -- DISCRETE_CHOICE_LIST => EXPRESSION
1435 -- | DISCRETE_CHOICE_LIST => <>
1437 -- Note: this routine only handles the named cases, including others.
1438 -- Cases where the component choice list is not present have already
1439 -- been handled directly.
1441 -- Error recovery: can raise Error_Resync
1443 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1444 -- rules have been extended to give support to Ada 2005 limited
1445 -- aggregates (AI-287)
1447 function P_Record_Or_Array_Component_Association return Node_Id is
1448 Assoc_Node : Node_Id;
1450 begin
1451 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1452 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1453 Set_Sloc (Assoc_Node, Token_Ptr);
1454 TF_Arrow;
1456 if Token = Tok_Box then
1458 -- Ada 2005(AI-287): The box notation is used to indicate the
1459 -- default initialization of aggregate components
1461 if Ada_Version < Ada_05 then
1462 Error_Msg_SP
1463 ("component association with '<'> is an Ada 2005 extension");
1464 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1465 end if;
1467 Set_Box_Present (Assoc_Node);
1468 Scan; -- Past box
1469 else
1470 Set_Expression (Assoc_Node, P_Expression);
1471 end if;
1473 return Assoc_Node;
1474 end P_Record_Or_Array_Component_Association;
1476 -----------------------------
1477 -- 4.3.1 Record Aggregate --
1478 -----------------------------
1480 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1481 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1483 ----------------------------------------------
1484 -- 4.3.1 Record Component Association List --
1485 ----------------------------------------------
1487 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1489 ----------------------------------
1490 -- 4.3.1 Component Choice List --
1491 ----------------------------------
1493 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1495 --------------------------------
1496 -- 4.3.1 Extension Aggregate --
1497 --------------------------------
1499 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1501 --------------------------
1502 -- 4.3.1 Ancestor Part --
1503 --------------------------
1505 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1507 ----------------------------
1508 -- 4.3.1 Array Aggregate --
1509 ----------------------------
1511 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1513 ---------------------------------------
1514 -- 4.3.1 Positional Array Aggregate --
1515 ---------------------------------------
1517 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1519 ----------------------------------
1520 -- 4.3.1 Named Array Aggregate --
1521 ----------------------------------
1523 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1525 ----------------------------------------
1526 -- 4.3.1 Array Component Association --
1527 ----------------------------------------
1529 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1531 ---------------------
1532 -- 4.4 Expression --
1533 ---------------------
1535 -- EXPRESSION ::=
1536 -- RELATION {and RELATION} | RELATION {and then RELATION}
1537 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1538 -- | RELATION {xor RELATION}
1540 -- On return, Expr_Form indicates the categorization of the expression
1541 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1542 -- an error message is given, and Error is returned).
1544 -- Error recovery: cannot raise Error_Resync
1546 function P_Expression return Node_Id is
1547 Logical_Op : Node_Kind;
1548 Prev_Logical_Op : Node_Kind;
1549 Op_Location : Source_Ptr;
1550 Node1 : Node_Id;
1551 Node2 : Node_Id;
1553 begin
1554 Node1 := P_Relation;
1556 if Token in Token_Class_Logop then
1557 Prev_Logical_Op := N_Empty;
1559 loop
1560 Op_Location := Token_Ptr;
1561 Logical_Op := P_Logical_Operator;
1563 if Prev_Logical_Op /= N_Empty and then
1564 Logical_Op /= Prev_Logical_Op
1565 then
1566 Error_Msg
1567 ("mixed logical operators in expression", Op_Location);
1568 Prev_Logical_Op := N_Empty;
1569 else
1570 Prev_Logical_Op := Logical_Op;
1571 end if;
1573 Node2 := Node1;
1574 Node1 := New_Node (Logical_Op, Op_Location);
1575 Set_Left_Opnd (Node1, Node2);
1576 Set_Right_Opnd (Node1, P_Relation);
1577 Set_Op_Name (Node1);
1578 exit when Token not in Token_Class_Logop;
1579 end loop;
1581 Expr_Form := EF_Non_Simple;
1582 end if;
1584 if Token = Tok_Apostrophe then
1585 Bad_Range_Attribute (Token_Ptr);
1586 return Error;
1587 else
1588 return Node1;
1589 end if;
1590 end P_Expression;
1592 -- This function is identical to the normal P_Expression, except that it
1593 -- checks that the expression scan did not stop on a right paren. It is
1594 -- called in all contexts where a right parenthesis cannot legitimately
1595 -- follow an expression.
1597 -- Error recovery: can not raise Error_Resync
1599 function P_Expression_No_Right_Paren return Node_Id is
1600 Expr : constant Node_Id := P_Expression;
1601 begin
1602 Check_No_Right_Paren;
1603 return Expr;
1604 end P_Expression_No_Right_Paren;
1606 ----------------------------------------
1607 -- 4.4 Expression_Or_Range_Attribute --
1608 ----------------------------------------
1610 -- EXPRESSION ::=
1611 -- RELATION {and RELATION} | RELATION {and then RELATION}
1612 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1613 -- | RELATION {xor RELATION}
1615 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1617 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1619 -- On return, Expr_Form indicates the categorization of the expression
1620 -- and EF_Range_Attr is one of the possibilities.
1622 -- Error recovery: cannot raise Error_Resync
1624 -- In the grammar, a RANGE attribute is simply a name, but its use is
1625 -- highly restricted, so in the parser, we do not regard it as a name.
1626 -- Instead, P_Name returns without scanning the 'RANGE part of the
1627 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1628 -- attribute reference. In the normal case where a range attribute is
1629 -- not allowed, an error message is issued by P_Expression.
1631 function P_Expression_Or_Range_Attribute return Node_Id is
1632 Logical_Op : Node_Kind;
1633 Prev_Logical_Op : Node_Kind;
1634 Op_Location : Source_Ptr;
1635 Node1 : Node_Id;
1636 Node2 : Node_Id;
1637 Attr_Node : Node_Id;
1639 begin
1640 Node1 := P_Relation;
1642 if Token = Tok_Apostrophe then
1643 Attr_Node := P_Range_Attribute_Reference (Node1);
1644 Expr_Form := EF_Range_Attr;
1645 return Attr_Node;
1647 elsif Token in Token_Class_Logop then
1648 Prev_Logical_Op := N_Empty;
1650 loop
1651 Op_Location := Token_Ptr;
1652 Logical_Op := P_Logical_Operator;
1654 if Prev_Logical_Op /= N_Empty and then
1655 Logical_Op /= Prev_Logical_Op
1656 then
1657 Error_Msg
1658 ("mixed logical operators in expression", Op_Location);
1659 Prev_Logical_Op := N_Empty;
1660 else
1661 Prev_Logical_Op := Logical_Op;
1662 end if;
1664 Node2 := Node1;
1665 Node1 := New_Node (Logical_Op, Op_Location);
1666 Set_Left_Opnd (Node1, Node2);
1667 Set_Right_Opnd (Node1, P_Relation);
1668 Set_Op_Name (Node1);
1669 exit when Token not in Token_Class_Logop;
1670 end loop;
1672 Expr_Form := EF_Non_Simple;
1673 end if;
1675 if Token = Tok_Apostrophe then
1676 Bad_Range_Attribute (Token_Ptr);
1677 return Error;
1678 else
1679 return Node1;
1680 end if;
1681 end P_Expression_Or_Range_Attribute;
1683 -------------------
1684 -- 4.4 Relation --
1685 -------------------
1687 -- RELATION ::=
1688 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1689 -- | SIMPLE_EXPRESSION [not] in RANGE
1690 -- | SIMPLE_EXPRESSION [not] in SUBTYPE_MARK
1692 -- On return, Expr_Form indicates the categorization of the expression
1694 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1695 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1697 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1698 -- expression, then tokens are scanned until either a non-expression token,
1699 -- a right paren (not matched by a left paren) or a comma, is encountered.
1701 function P_Relation return Node_Id is
1702 Node1, Node2 : Node_Id;
1703 Optok : Source_Ptr;
1705 begin
1706 Node1 := P_Simple_Expression;
1708 if Token not in Token_Class_Relop then
1709 return Node1;
1711 else
1712 -- Here we have a relational operator following. If so then scan it
1713 -- out. Note that the assignment symbol := is treated as a relational
1714 -- operator to improve the error recovery when it is misused for =.
1715 -- P_Relational_Operator also parses the IN and NOT IN operations.
1717 Optok := Token_Ptr;
1718 Node2 := New_Node (P_Relational_Operator, Optok);
1719 Set_Left_Opnd (Node2, Node1);
1720 Set_Op_Name (Node2);
1722 -- Case of IN or NOT IN
1724 if Prev_Token = Tok_In then
1725 Set_Right_Opnd (Node2, P_Range_Or_Subtype_Mark);
1727 -- Case of relational operator (= /= < <= > >=)
1729 else
1730 Set_Right_Opnd (Node2, P_Simple_Expression);
1731 end if;
1733 Expr_Form := EF_Non_Simple;
1735 if Token in Token_Class_Relop then
1736 Error_Msg_SC ("unexpected relational operator");
1737 raise Error_Resync;
1738 end if;
1740 return Node2;
1741 end if;
1743 -- If any error occurs, then scan to the next expression terminator symbol
1744 -- or comma or right paren at the outer (i.e. current) parentheses level.
1745 -- The flags are set to indicate a normal simple expression.
1747 exception
1748 when Error_Resync =>
1749 Resync_Expression;
1750 Expr_Form := EF_Simple;
1751 return Error;
1752 end P_Relation;
1754 ----------------------------
1755 -- 4.4 Simple Expression --
1756 ----------------------------
1758 -- SIMPLE_EXPRESSION ::=
1759 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1761 -- On return, Expr_Form indicates the categorization of the expression
1763 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1764 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1766 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1767 -- expression, then tokens are scanned until either a non-expression token,
1768 -- a right paren (not matched by a left paren) or a comma, is encountered.
1770 -- Note: P_Simple_Expression is called only internally by higher level
1771 -- expression routines. In cases in the grammar where a simple expression
1772 -- is required, the approach is to scan an expression, and then post an
1773 -- appropriate error message if the expression obtained is not simple. This
1774 -- gives better error recovery and treatment.
1776 function P_Simple_Expression return Node_Id is
1777 Scan_State : Saved_Scan_State;
1778 Node1 : Node_Id;
1779 Node2 : Node_Id;
1780 Tokptr : Source_Ptr;
1782 begin
1783 -- Check for cases starting with a name. There are two reasons for
1784 -- special casing. First speed things up by catching a common case
1785 -- without going through several routine layers. Second the caller must
1786 -- be informed via Expr_Form when the simple expression is a name.
1788 if Token in Token_Class_Name then
1789 Node1 := P_Name;
1791 -- Deal with apostrophe cases
1793 if Token = Tok_Apostrophe then
1794 Save_Scan_State (Scan_State); -- at apostrophe
1795 Scan; -- past apostrophe
1797 -- If qualified expression, scan it out and fall through
1799 if Token = Tok_Left_Paren then
1800 Node1 := P_Qualified_Expression (Node1);
1801 Expr_Form := EF_Simple;
1803 -- If range attribute, then we return with Token pointing to the
1804 -- apostrophe. Note: avoid the normal error check on exit. We
1805 -- know that the expression really is complete in this case!
1807 else -- Token = Tok_Range then
1808 Restore_Scan_State (Scan_State); -- to apostrophe
1809 Expr_Form := EF_Simple_Name;
1810 return Node1;
1811 end if;
1812 end if;
1814 -- If an expression terminator follows, the previous processing
1815 -- completely scanned out the expression (a common case), and
1816 -- left Expr_Form set appropriately for returning to our caller.
1818 if Token in Token_Class_Sterm then
1819 null;
1821 -- If we do not have an expression terminator, then complete the
1822 -- scan of a simple expression. This code duplicates the code
1823 -- found in P_Term and P_Factor.
1825 else
1826 if Token = Tok_Double_Asterisk then
1827 if Style_Check then
1828 Style.Check_Exponentiation_Operator;
1829 end if;
1831 Node2 := New_Node (N_Op_Expon, Token_Ptr);
1832 Scan; -- past **
1833 Set_Left_Opnd (Node2, Node1);
1834 Set_Right_Opnd (Node2, P_Primary);
1835 Set_Op_Name (Node2);
1836 Node1 := Node2;
1837 end if;
1839 loop
1840 exit when Token not in Token_Class_Mulop;
1841 Tokptr := Token_Ptr;
1842 Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1844 if Style_Check then
1845 Style.Check_Binary_Operator;
1846 end if;
1848 Scan; -- past operator
1849 Set_Left_Opnd (Node2, Node1);
1850 Set_Right_Opnd (Node2, P_Factor);
1851 Set_Op_Name (Node2);
1852 Node1 := Node2;
1853 end loop;
1855 loop
1856 exit when Token not in Token_Class_Binary_Addop;
1857 Tokptr := Token_Ptr;
1858 Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1860 if Style_Check then
1861 Style.Check_Binary_Operator;
1862 end if;
1864 Scan; -- past operator
1865 Set_Left_Opnd (Node2, Node1);
1866 Set_Right_Opnd (Node2, P_Term);
1867 Set_Op_Name (Node2);
1868 Node1 := Node2;
1869 end loop;
1871 Expr_Form := EF_Simple;
1872 end if;
1874 -- Cases where simple expression does not start with a name
1876 else
1877 -- Scan initial sign and initial Term
1879 if Token in Token_Class_Unary_Addop then
1880 Tokptr := Token_Ptr;
1881 Node1 := New_Node (P_Unary_Adding_Operator, Tokptr);
1883 if Style_Check then
1884 Style.Check_Unary_Plus_Or_Minus;
1885 end if;
1887 Scan; -- past operator
1888 Set_Right_Opnd (Node1, P_Term);
1889 Set_Op_Name (Node1);
1890 else
1891 Node1 := P_Term;
1892 end if;
1894 -- In the following, we special-case a sequence of concatentations of
1895 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
1896 -- else mixed in. For such a sequence, we return a tree representing
1897 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
1898 -- the number of concatenations is large. If semantic analysis
1899 -- resolves the "&" to a predefined one, then this folding gives the
1900 -- right answer. Otherwise, semantic analysis will complain about a
1901 -- capacity-exceeded error. The purpose of this trick is to avoid
1902 -- creating a deeply nested tree, which would cause deep recursion
1903 -- during semantics, causing stack overflow. This way, we can handle
1904 -- enormous concatenations in the normal case of predefined "&". We
1905 -- first build up the normal tree, and then rewrite it if
1906 -- appropriate.
1908 declare
1909 Num_Concats_Threshold : constant Positive := 1000;
1910 -- Arbitrary threshold value to enable optimization
1912 First_Node : constant Node_Id := Node1;
1913 Is_Strlit_Concat : Boolean;
1914 -- True iff we've parsed a sequence of concatenations of string
1915 -- literals, with nothing else mixed in.
1917 Num_Concats : Natural;
1918 -- Number of "&" operators if Is_Strlit_Concat is True
1920 begin
1921 Is_Strlit_Concat :=
1922 Nkind (Node1) = N_String_Literal
1923 and then Token = Tok_Ampersand;
1924 Num_Concats := 0;
1926 -- Scan out sequence of terms separated by binary adding operators
1928 loop
1929 exit when Token not in Token_Class_Binary_Addop;
1930 Tokptr := Token_Ptr;
1931 Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1932 Scan; -- past operator
1933 Set_Left_Opnd (Node2, Node1);
1934 Node1 := P_Term;
1935 Set_Right_Opnd (Node2, Node1);
1936 Set_Op_Name (Node2);
1938 -- Check if we're still concatenating string literals
1940 Is_Strlit_Concat :=
1941 Is_Strlit_Concat
1942 and then Nkind (Node2) = N_Op_Concat
1943 and then Nkind (Node1) = N_String_Literal;
1945 if Is_Strlit_Concat then
1946 Num_Concats := Num_Concats + 1;
1947 end if;
1949 Node1 := Node2;
1950 end loop;
1952 -- If we have an enormous series of concatenations of string
1953 -- literals, rewrite as explained above. The Is_Folded_In_Parser
1954 -- flag tells semantic analysis that if the "&" is not predefined,
1955 -- the folded value is wrong.
1957 if Is_Strlit_Concat
1958 and then Num_Concats >= Num_Concats_Threshold
1959 then
1960 declare
1961 Empty_String_Val : String_Id;
1962 -- String_Id for ""
1964 Strlit_Concat_Val : String_Id;
1965 -- Contains the folded value (which will be correct if the
1966 -- "&" operators are the predefined ones).
1968 Cur_Node : Node_Id;
1969 -- For walking up the tree
1971 New_Node : Node_Id;
1972 -- Folded node to replace Node1
1974 Loc : constant Source_Ptr := Sloc (First_Node);
1976 begin
1977 -- Walk up the tree starting at the leftmost string literal
1978 -- (First_Node), building up the Strlit_Concat_Val as we
1979 -- go. Note that we do not use recursion here -- the whole
1980 -- point is to avoid recursively walking that enormous tree.
1982 Start_String;
1983 Store_String_Chars (Strval (First_Node));
1985 Cur_Node := Parent (First_Node);
1986 while Present (Cur_Node) loop
1987 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
1988 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
1990 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
1991 Cur_Node := Parent (Cur_Node);
1992 end loop;
1994 Strlit_Concat_Val := End_String;
1996 -- Create new folded node, and rewrite result with a concat-
1997 -- enation of an empty string literal and the folded node.
1999 Start_String;
2000 Empty_String_Val := End_String;
2001 New_Node :=
2002 Make_Op_Concat (Loc,
2003 Make_String_Literal (Loc, Empty_String_Val),
2004 Make_String_Literal (Loc, Strlit_Concat_Val,
2005 Is_Folded_In_Parser => True));
2006 Rewrite (Node1, New_Node);
2007 end;
2008 end if;
2009 end;
2011 -- All done, we clearly do not have name or numeric literal so this
2012 -- is a case of a simple expression which is some other possibility.
2014 Expr_Form := EF_Simple;
2015 end if;
2017 -- Come here at end of simple expression, where we do a couple of
2018 -- special checks to improve error recovery.
2020 -- Special test to improve error recovery. If the current token
2021 -- is a period, then someone is trying to do selection on something
2022 -- that is not a name, e.g. a qualified expression.
2024 if Token = Tok_Dot then
2025 Error_Msg_SC ("prefix for selection is not a name");
2026 raise Error_Resync;
2027 end if;
2029 -- Special test to improve error recovery: If the current token is
2030 -- not the first token on a line (as determined by checking the
2031 -- previous token position with the start of the current line),
2032 -- then we insist that we have an appropriate terminating token.
2033 -- Consider the following two examples:
2035 -- 1) if A nad B then ...
2037 -- 2) A := B
2038 -- C := D
2040 -- In the first example, we would like to issue a binary operator
2041 -- expected message and resynchronize to the then. In the second
2042 -- example, we do not want to issue a binary operator message, so
2043 -- that instead we will get the missing semicolon message. This
2044 -- distinction is of course a heuristic which does not always work,
2045 -- but in practice it is quite effective.
2047 -- Note: the one case in which we do not go through this circuit is
2048 -- when we have scanned a range attribute and want to return with
2049 -- Token pointing to the apostrophe. The apostrophe is not normally
2050 -- an expression terminator, and is not in Token_Class_Sterm, but
2051 -- in this special case we know that the expression is complete.
2053 if not Token_Is_At_Start_Of_Line
2054 and then Token not in Token_Class_Sterm
2055 then
2056 Error_Msg_AP ("binary operator expected");
2057 raise Error_Resync;
2058 else
2059 return Node1;
2060 end if;
2062 -- If any error occurs, then scan to next expression terminator symbol
2063 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2064 -- level. Expr_Form is set to indicate a normal simple expression.
2066 exception
2067 when Error_Resync =>
2068 Resync_Expression;
2069 Expr_Form := EF_Simple;
2070 return Error;
2072 end P_Simple_Expression;
2074 -----------------------------------------------
2075 -- 4.4 Simple Expression or Range Attribute --
2076 -----------------------------------------------
2078 -- SIMPLE_EXPRESSION ::=
2079 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2081 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2083 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2085 -- Error recovery: cannot raise Error_Resync
2087 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2088 Sexpr : Node_Id;
2089 Attr_Node : Node_Id;
2091 begin
2092 -- We don't just want to roar ahead and call P_Simple_Expression
2093 -- here, since we want to handle the case of a parenthesized range
2094 -- attribute cleanly.
2096 if Token = Tok_Left_Paren then
2097 declare
2098 Lptr : constant Source_Ptr := Token_Ptr;
2099 Scan_State : Saved_Scan_State;
2101 begin
2102 Save_Scan_State (Scan_State);
2103 Scan; -- past left paren
2104 Sexpr := P_Simple_Expression;
2106 if Token = Tok_Apostrophe then
2107 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2108 Expr_Form := EF_Range_Attr;
2110 if Token = Tok_Right_Paren then
2111 Scan; -- scan past right paren if present
2112 end if;
2114 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2116 return Attr_Node;
2117 end if;
2119 Restore_Scan_State (Scan_State);
2120 end;
2121 end if;
2123 -- Here after dealing with parenthesized range attribute
2125 Sexpr := P_Simple_Expression;
2127 if Token = Tok_Apostrophe then
2128 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2129 Expr_Form := EF_Range_Attr;
2130 return Attr_Node;
2132 else
2133 return Sexpr;
2134 end if;
2135 end P_Simple_Expression_Or_Range_Attribute;
2137 ---------------
2138 -- 4.4 Term --
2139 ---------------
2141 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2143 -- Error recovery: can raise Error_Resync
2145 function P_Term return Node_Id is
2146 Node1, Node2 : Node_Id;
2147 Tokptr : Source_Ptr;
2149 begin
2150 Node1 := P_Factor;
2152 loop
2153 exit when Token not in Token_Class_Mulop;
2154 Tokptr := Token_Ptr;
2155 Node2 := New_Node (P_Multiplying_Operator, Tokptr);
2156 Scan; -- past operator
2157 Set_Left_Opnd (Node2, Node1);
2158 Set_Right_Opnd (Node2, P_Factor);
2159 Set_Op_Name (Node2);
2160 Node1 := Node2;
2161 end loop;
2163 return Node1;
2164 end P_Term;
2166 -----------------
2167 -- 4.4 Factor --
2168 -----------------
2170 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2172 -- Error recovery: can raise Error_Resync
2174 function P_Factor return Node_Id is
2175 Node1 : Node_Id;
2176 Node2 : Node_Id;
2178 begin
2179 if Token = Tok_Abs then
2180 Node1 := New_Node (N_Op_Abs, Token_Ptr);
2182 if Style_Check then
2183 Style.Check_Abs_Not;
2184 end if;
2186 Scan; -- past ABS
2187 Set_Right_Opnd (Node1, P_Primary);
2188 Set_Op_Name (Node1);
2189 return Node1;
2191 elsif Token = Tok_Not then
2192 Node1 := New_Node (N_Op_Not, Token_Ptr);
2194 if Style_Check then
2195 Style.Check_Abs_Not;
2196 end if;
2198 Scan; -- past NOT
2199 Set_Right_Opnd (Node1, P_Primary);
2200 Set_Op_Name (Node1);
2201 return Node1;
2203 else
2204 Node1 := P_Primary;
2206 if Token = Tok_Double_Asterisk then
2207 Node2 := New_Node (N_Op_Expon, Token_Ptr);
2208 Scan; -- past **
2209 Set_Left_Opnd (Node2, Node1);
2210 Set_Right_Opnd (Node2, P_Primary);
2211 Set_Op_Name (Node2);
2212 return Node2;
2213 else
2214 return Node1;
2215 end if;
2216 end if;
2217 end P_Factor;
2219 ------------------
2220 -- 4.4 Primary --
2221 ------------------
2223 -- PRIMARY ::=
2224 -- NUMERIC_LITERAL | null
2225 -- | STRING_LITERAL | AGGREGATE
2226 -- | NAME | QUALIFIED_EXPRESSION
2227 -- | ALLOCATOR | (EXPRESSION)
2229 -- Error recovery: can raise Error_Resync
2231 function P_Primary return Node_Id is
2232 Scan_State : Saved_Scan_State;
2233 Node1 : Node_Id;
2235 begin
2236 -- The loop runs more than once only if misplaced pragmas are found
2238 loop
2239 case Token is
2241 -- Name token can start a name, call or qualified expression, all
2242 -- of which are acceptable possibilities for primary. Note also
2243 -- that string literal is included in name (as operator symbol)
2244 -- and type conversion is included in name (as indexed component).
2246 when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2247 Node1 := P_Name;
2249 -- All done unless apostrophe follows
2251 if Token /= Tok_Apostrophe then
2252 return Node1;
2254 -- Apostrophe following means that we have either just parsed
2255 -- the subtype mark of a qualified expression, or the prefix
2256 -- or a range attribute.
2258 else -- Token = Tok_Apostrophe
2259 Save_Scan_State (Scan_State); -- at apostrophe
2260 Scan; -- past apostrophe
2262 -- If range attribute, then this is always an error, since
2263 -- the only legitimate case (where the scanned expression is
2264 -- a qualified simple name) is handled at the level of the
2265 -- Simple_Expression processing. This case corresponds to a
2266 -- usage such as 3 + A'Range, which is always illegal.
2268 if Token = Tok_Range then
2269 Restore_Scan_State (Scan_State); -- to apostrophe
2270 Bad_Range_Attribute (Token_Ptr);
2271 return Error;
2273 -- If left paren, then we have a qualified expression.
2274 -- Note that P_Name guarantees that in this case, where
2275 -- Token = Tok_Apostrophe on return, the only two possible
2276 -- tokens following the apostrophe are left paren and
2277 -- RANGE, so we know we have a left paren here.
2279 else -- Token = Tok_Left_Paren
2280 return P_Qualified_Expression (Node1);
2282 end if;
2283 end if;
2285 -- Numeric or string literal
2287 when Tok_Integer_Literal |
2288 Tok_Real_Literal |
2289 Tok_String_Literal =>
2291 Node1 := Token_Node;
2292 Scan; -- past number
2293 return Node1;
2295 -- Left paren, starts aggregate or parenthesized expression
2297 when Tok_Left_Paren =>
2298 declare
2299 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2301 begin
2302 if Nkind (Expr) = N_Attribute_Reference
2303 and then Attribute_Name (Expr) = Name_Range
2304 then
2305 Bad_Range_Attribute (Sloc (Expr));
2306 end if;
2308 return Expr;
2309 end;
2311 -- Allocator
2313 when Tok_New =>
2314 return P_Allocator;
2316 -- Null
2318 when Tok_Null =>
2319 Scan; -- past NULL
2320 return New_Node (N_Null, Prev_Token_Ptr);
2322 -- Pragma, not allowed here, so just skip past it
2324 when Tok_Pragma =>
2325 P_Pragmas_Misplaced;
2327 -- Anything else is illegal as the first token of a primary, but
2328 -- we test for a reserved identifier so that it is treated nicely
2330 when others =>
2331 if Is_Reserved_Identifier then
2332 return P_Identifier;
2334 elsif Prev_Token = Tok_Comma then
2335 Error_Msg_SP ("extra "","" ignored");
2336 raise Error_Resync;
2338 else
2339 Error_Msg_AP ("missing operand");
2340 raise Error_Resync;
2341 end if;
2343 end case;
2344 end loop;
2345 end P_Primary;
2347 ---------------------------
2348 -- 4.5 Logical Operator --
2349 ---------------------------
2351 -- LOGICAL_OPERATOR ::= and | or | xor
2353 -- Note: AND THEN and OR ELSE are also treated as logical operators
2354 -- by the parser (even though they are not operators semantically)
2356 -- The value returned is the appropriate Node_Kind code for the operator
2357 -- On return, Token points to the token following the scanned operator.
2359 -- The caller has checked that the first token is a legitimate logical
2360 -- operator token (i.e. is either XOR, AND, OR).
2362 -- Error recovery: cannot raise Error_Resync
2364 function P_Logical_Operator return Node_Kind is
2365 begin
2366 if Token = Tok_And then
2367 if Style_Check then
2368 Style.Check_Binary_Operator;
2369 end if;
2371 Scan; -- past AND
2373 if Token = Tok_Then then
2374 Scan; -- past THEN
2375 return N_And_Then;
2376 else
2377 return N_Op_And;
2378 end if;
2380 elsif Token = Tok_Or then
2381 if Style_Check then
2382 Style.Check_Binary_Operator;
2383 end if;
2385 Scan; -- past OR
2387 if Token = Tok_Else then
2388 Scan; -- past ELSE
2389 return N_Or_Else;
2390 else
2391 return N_Op_Or;
2392 end if;
2394 else -- Token = Tok_Xor
2395 if Style_Check then
2396 Style.Check_Binary_Operator;
2397 end if;
2399 Scan; -- past XOR
2400 return N_Op_Xor;
2401 end if;
2402 end P_Logical_Operator;
2404 ------------------------------
2405 -- 4.5 Relational Operator --
2406 ------------------------------
2408 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2410 -- The value returned is the appropriate Node_Kind code for the operator.
2411 -- On return, Token points to the operator token, NOT past it.
2413 -- The caller has checked that the first token is a legitimate relational
2414 -- operator token (i.e. is one of the operator tokens listed above).
2416 -- Error recovery: cannot raise Error_Resync
2418 function P_Relational_Operator return Node_Kind is
2419 Op_Kind : Node_Kind;
2420 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2421 (Tok_Less => N_Op_Lt,
2422 Tok_Equal => N_Op_Eq,
2423 Tok_Greater => N_Op_Gt,
2424 Tok_Not_Equal => N_Op_Ne,
2425 Tok_Greater_Equal => N_Op_Ge,
2426 Tok_Less_Equal => N_Op_Le,
2427 Tok_In => N_In,
2428 Tok_Not => N_Not_In,
2429 Tok_Box => N_Op_Ne);
2431 begin
2432 if Token = Tok_Box then
2433 Error_Msg_SC ("""'<'>"" should be ""/=""");
2434 end if;
2436 Op_Kind := Relop_Node (Token);
2438 if Style_Check then
2439 Style.Check_Binary_Operator;
2440 end if;
2442 Scan; -- past operator token
2444 if Prev_Token = Tok_Not then
2445 T_In;
2446 end if;
2448 return Op_Kind;
2449 end P_Relational_Operator;
2451 ---------------------------------
2452 -- 4.5 Binary Adding Operator --
2453 ---------------------------------
2455 -- BINARY_ADDING_OPERATOR ::= + | - | &
2457 -- The value returned is the appropriate Node_Kind code for the operator.
2458 -- On return, Token points to the operator token (NOT past it).
2460 -- The caller has checked that the first token is a legitimate adding
2461 -- operator token (i.e. is one of the operator tokens listed above).
2463 -- Error recovery: cannot raise Error_Resync
2465 function P_Binary_Adding_Operator return Node_Kind is
2466 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2467 (Tok_Ampersand => N_Op_Concat,
2468 Tok_Minus => N_Op_Subtract,
2469 Tok_Plus => N_Op_Add);
2470 begin
2471 return Addop_Node (Token);
2472 end P_Binary_Adding_Operator;
2474 --------------------------------
2475 -- 4.5 Unary Adding Operator --
2476 --------------------------------
2478 -- UNARY_ADDING_OPERATOR ::= + | -
2480 -- The value returned is the appropriate Node_Kind code for the operator.
2481 -- On return, Token points to the operator token (NOT past it).
2483 -- The caller has checked that the first token is a legitimate adding
2484 -- operator token (i.e. is one of the operator tokens listed above).
2486 -- Error recovery: cannot raise Error_Resync
2488 function P_Unary_Adding_Operator return Node_Kind is
2489 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2490 (Tok_Minus => N_Op_Minus,
2491 Tok_Plus => N_Op_Plus);
2492 begin
2493 return Addop_Node (Token);
2494 end P_Unary_Adding_Operator;
2496 -------------------------------
2497 -- 4.5 Multiplying Operator --
2498 -------------------------------
2500 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
2502 -- The value returned is the appropriate Node_Kind code for the operator.
2503 -- On return, Token points to the operator token (NOT past it).
2505 -- The caller has checked that the first token is a legitimate multiplying
2506 -- operator token (i.e. is one of the operator tokens listed above).
2508 -- Error recovery: cannot raise Error_Resync
2510 function P_Multiplying_Operator return Node_Kind is
2511 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2512 (Tok_Asterisk => N_Op_Multiply,
2513 Tok_Mod => N_Op_Mod,
2514 Tok_Rem => N_Op_Rem,
2515 Tok_Slash => N_Op_Divide);
2516 begin
2517 return Mulop_Node (Token);
2518 end P_Multiplying_Operator;
2520 --------------------------------------
2521 -- 4.5 Highest Precedence Operator --
2522 --------------------------------------
2524 -- Parsed by P_Factor (4.4)
2526 -- Note: this rule is not in fact used by the grammar at any point!
2528 --------------------------
2529 -- 4.6 Type Conversion --
2530 --------------------------
2532 -- Parsed by P_Primary as a Name (4.1)
2534 -------------------------------
2535 -- 4.7 Qualified Expression --
2536 -------------------------------
2538 -- QUALIFIED_EXPRESSION ::=
2539 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2541 -- The caller has scanned the name which is the Subtype_Mark parameter
2542 -- and scanned past the single quote following the subtype mark. The
2543 -- caller has not checked that this name is in fact appropriate for
2544 -- a subtype mark name (i.e. it is a selected component or identifier).
2546 -- Error_Recovery: cannot raise Error_Resync
2548 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2549 Qual_Node : Node_Id;
2550 begin
2551 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2552 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2553 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2554 return Qual_Node;
2555 end P_Qualified_Expression;
2557 --------------------
2558 -- 4.8 Allocator --
2559 --------------------
2561 -- ALLOCATOR ::=
2562 -- new [NULL_EXCLUSION] SUBTYPE_INDICATION | new QUALIFIED_EXPRESSION
2564 -- The caller has checked that the initial token is NEW
2566 -- Error recovery: can raise Error_Resync
2568 function P_Allocator return Node_Id is
2569 Alloc_Node : Node_Id;
2570 Type_Node : Node_Id;
2571 Null_Exclusion_Present : Boolean;
2573 begin
2574 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2575 T_New;
2577 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
2579 Null_Exclusion_Present := P_Null_Exclusion;
2580 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
2581 Type_Node := P_Subtype_Mark_Resync;
2583 if Token = Tok_Apostrophe then
2584 Scan; -- past apostrophe
2585 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2586 else
2587 Set_Expression
2588 (Alloc_Node,
2589 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
2590 end if;
2592 return Alloc_Node;
2593 end P_Allocator;
2595 end Ch4;