1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 pragma Style_Checks (All_Checks);
30 -- Turn off subprogram body ordering check. Subprograms are in order
31 -- by RM section rather than alphabetical
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
40 function P_Aggregate_Or_Paren_Expr return Node_Id;
41 function P_Allocator return Node_Id;
42 function P_Record_Or_Array_Component_Association return Node_Id;
43 function P_Factor return Node_Id;
44 function P_Primary return Node_Id;
45 function P_Relation return Node_Id;
46 function P_Term return Node_Id;
48 function P_Binary_Adding_Operator return Node_Kind;
49 function P_Logical_Operator return Node_Kind;
50 function P_Multiplying_Operator return Node_Kind;
51 function P_Relational_Operator return Node_Kind;
52 function P_Unary_Adding_Operator return Node_Kind;
54 procedure Bad_Range_Attribute (Loc : Source_Ptr);
55 -- Called to place complaint about bad range attribute at the given
56 -- source location. Terminates by raising Error_Resync.
58 function P_Range_Attribute_Reference
59 (Prefix_Node : Node_Id)
61 -- Scan a range attribute reference. The caller has scanned out the
62 -- prefix. The current token is known to be an apostrophe and the
63 -- following token is known to be RANGE.
65 procedure Set_Op_Name (Node : Node_Id);
66 -- Procedure to set name field (Chars) in operator node
68 -------------------------
69 -- Bad_Range_Attribute --
70 -------------------------
72 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
74 Error_Msg ("range attribute cannot be used in expression", Loc);
76 end Bad_Range_Attribute;
82 procedure Set_Op_Name (Node : Node_Id) is
83 type Name_Of_Type is array (N_Op) of Name_Id;
84 Name_Of : Name_Of_Type := Name_Of_Type'(
85 N_Op_And => Name_Op_And,
86 N_Op_Or => Name_Op_Or,
87 N_Op_Xor => Name_Op_Xor,
88 N_Op_Eq => Name_Op_Eq,
89 N_Op_Ne => Name_Op_Ne,
90 N_Op_Lt => Name_Op_Lt,
91 N_Op_Le => Name_Op_Le,
92 N_Op_Gt => Name_Op_Gt,
93 N_Op_Ge => Name_Op_Ge,
94 N_Op_Add => Name_Op_Add,
95 N_Op_Subtract => Name_Op_Subtract,
96 N_Op_Concat => Name_Op_Concat,
97 N_Op_Multiply => Name_Op_Multiply,
98 N_Op_Divide => Name_Op_Divide,
99 N_Op_Mod => Name_Op_Mod,
100 N_Op_Rem => Name_Op_Rem,
101 N_Op_Expon => Name_Op_Expon,
102 N_Op_Plus => Name_Op_Add,
103 N_Op_Minus => Name_Op_Subtract,
104 N_Op_Abs => Name_Op_Abs,
105 N_Op_Not => Name_Op_Not,
107 -- We don't really need these shift operators, since they never
108 -- appear as operators in the source, but the path of least
109 -- resistance is to put them in (the aggregate must be complete)
111 N_Op_Rotate_Left => Name_Rotate_Left,
112 N_Op_Rotate_Right => Name_Rotate_Right,
113 N_Op_Shift_Left => Name_Shift_Left,
114 N_Op_Shift_Right => Name_Shift_Right,
115 N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
118 if Nkind (Node) in N_Op then
119 Set_Chars (Node, Name_Of (Nkind (Node)));
123 --------------------------
124 -- 4.1 Name (also 6.4) --
125 --------------------------
128 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
129 -- | INDEXED_COMPONENT | SLICE
130 -- | SELECTED_COMPONENT | ATTRIBUTE
131 -- | TYPE_CONVERSION | FUNCTION_CALL
132 -- | CHARACTER_LITERAL
134 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
136 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
138 -- EXPLICIT_DEREFERENCE ::= NAME . all
140 -- IMPLICIT_DEREFERENCE ::= NAME
142 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
144 -- SLICE ::= PREFIX (DISCRETE_RANGE)
146 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
148 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
150 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
152 -- ATTRIBUTE_DESIGNATOR ::=
153 -- IDENTIFIER [(static_EXPRESSION)]
154 -- | access | delta | digits
158 -- | function_PREFIX ACTUAL_PARAMETER_PART
160 -- ACTUAL_PARAMETER_PART ::=
161 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
163 -- PARAMETER_ASSOCIATION ::=
164 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
166 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
168 -- Note: syntactically a procedure call looks just like a function call,
169 -- so this routine is in practice used to scan out procedure calls as well.
171 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
173 -- Error recovery: can raise Error_Resync
175 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
176 -- followed by either a left paren (qualified expression case), or by
177 -- range (range attribute case). All other uses of apostrophe (i.e. all
178 -- other attributes) are handled in this routine.
180 -- Error recovery: can raise Error_Resync
182 function P_Name return Node_Id is
183 Scan_State : Saved_Scan_State;
185 Prefix_Node : Node_Id;
186 Ident_Node : Node_Id;
188 Range_Node : Node_Id;
191 Arg_List : List_Id := No_List; -- kill junk warning
192 Attr_Name : Name_Id := No_Name; -- kill junk warning
195 if Token not in Token_Class_Name then
196 Error_Msg_AP ("name expected");
200 -- Loop through designators in qualified name
202 Name_Node := Token_Node;
205 Scan; -- past designator
206 exit when Token /= Tok_Dot;
207 Save_Scan_State (Scan_State); -- at dot
210 -- If we do not have another designator after the dot, then join
211 -- the normal circuit to handle a dot extension (may be .all or
212 -- character literal case). Otherwise loop back to scan the next
215 if Token not in Token_Class_Desig then
216 goto Scan_Name_Extension_Dot;
218 Prefix_Node := Name_Node;
219 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
220 Set_Prefix (Name_Node, Prefix_Node);
221 Set_Selector_Name (Name_Node, Token_Node);
225 -- We have now scanned out a qualified designator. If the last token is
226 -- an operator symbol, then we certainly do not have the Snam case, so
227 -- we can just use the normal name extension check circuit
229 if Prev_Token = Tok_Operator_Symbol then
230 goto Scan_Name_Extension;
233 -- We have scanned out a qualified simple name, check for name extension
234 -- Note that we know there is no dot here at this stage, so the only
235 -- possible cases of name extension are apostrophe and left paren.
237 if Token = Tok_Apostrophe then
238 Save_Scan_State (Scan_State); -- at apostrophe
239 Scan; -- past apostrophe
241 -- If left paren, then this might be a qualified expression, but we
242 -- are only in the business of scanning out names, so return with
243 -- Token backed up to point to the apostrophe. The treatment for
244 -- the range attribute is similar (we do not consider x'range to
245 -- be a name in this grammar).
247 if Token = Tok_Left_Paren or else Token = Tok_Range then
248 Restore_Scan_State (Scan_State); -- to apostrophe
249 Expr_Form := EF_Simple_Name;
252 -- Otherwise we have the case of a name extended by an attribute
255 goto Scan_Name_Extension_Apostrophe;
258 -- Check case of qualified simple name extended by a left parenthesis
260 elsif Token = Tok_Left_Paren then
261 Scan; -- past left paren
262 goto Scan_Name_Extension_Left_Paren;
264 -- Otherwise the qualified simple name is not extended, so return
267 Expr_Form := EF_Simple_Name;
271 -- Loop scanning past name extensions. A label is used for control
272 -- transfer for this loop for ease of interfacing with the finite state
273 -- machine in the parenthesis scanning circuit, and also to allow for
274 -- passing in control to the appropriate point from the above code.
276 <<Scan_Name_Extension>>
278 -- Character literal used as name cannot be extended. Also this
279 -- cannot be a call, since the name for a call must be a designator.
280 -- Return in these cases, or if there is no name extension
282 if Token not in Token_Class_Namext
283 or else Prev_Token = Tok_Char_Literal
285 Expr_Form := EF_Name;
289 -- Merge here when we know there is a name extension
291 <<Scan_Name_Extension_OK>>
293 if Token = Tok_Left_Paren then
294 Scan; -- past left paren
295 goto Scan_Name_Extension_Left_Paren;
297 elsif Token = Tok_Apostrophe then
298 Save_Scan_State (Scan_State); -- at apostrophe
299 Scan; -- past apostrophe
300 goto Scan_Name_Extension_Apostrophe;
302 else -- Token = Tok_Dot
303 Save_Scan_State (Scan_State); -- at dot
305 goto Scan_Name_Extension_Dot;
308 -- Case of name extended by dot (selection), dot is already skipped
309 -- and the scan state at the point of the dot is saved in Scan_State.
311 <<Scan_Name_Extension_Dot>>
313 -- Explicit dereference case
315 if Token = Tok_All then
316 Prefix_Node := Name_Node;
317 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
318 Set_Prefix (Name_Node, Prefix_Node);
320 goto Scan_Name_Extension;
322 -- Selected component case
324 elsif Token in Token_Class_Name then
325 Prefix_Node := Name_Node;
326 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
327 Set_Prefix (Name_Node, Prefix_Node);
328 Set_Selector_Name (Name_Node, Token_Node);
329 Scan; -- past selector
330 goto Scan_Name_Extension;
332 -- Reserved identifier as selector
334 elsif Is_Reserved_Identifier then
335 Scan_Reserved_Identifier (Force_Msg => False);
336 Prefix_Node := Name_Node;
337 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
338 Set_Prefix (Name_Node, Prefix_Node);
339 Set_Selector_Name (Name_Node, Token_Node);
340 Scan; -- past identifier used as selector
341 goto Scan_Name_Extension;
343 -- If dot is at end of line and followed by nothing legal,
344 -- then assume end of name and quit (dot will be taken as
345 -- an erroneous form of some other punctuation by our caller).
347 elsif Token_Is_At_Start_Of_Line then
348 Restore_Scan_State (Scan_State);
351 -- Here if nothing legal after the dot
354 Error_Msg_AP ("selector expected");
358 -- Here for an apostrophe as name extension. The scan position at the
359 -- apostrophe has already been saved, and the apostrophe scanned out.
361 <<Scan_Name_Extension_Apostrophe>>
363 Scan_Apostrophe : declare
364 function Apostrophe_Should_Be_Semicolon return Boolean;
365 -- Checks for case where apostrophe should probably be
366 -- a semicolon, and if so, gives appropriate message,
367 -- resets the scan pointer to the apostrophe, changes
368 -- the current token to Tok_Semicolon, and returns True.
369 -- Otherwise returns False.
371 function Apostrophe_Should_Be_Semicolon return Boolean is
373 if Token_Is_At_Start_Of_Line then
374 Restore_Scan_State (Scan_State); -- to apostrophe
375 Error_Msg_SC ("""''"" should be "";""");
376 Token := Tok_Semicolon;
381 end Apostrophe_Should_Be_Semicolon;
383 -- Start of processing for Scan_Apostrophe
386 -- If range attribute after apostrophe, then return with Token
387 -- pointing to the apostrophe. Note that in this case the prefix
388 -- need not be a simple name (cases like A.all'range). Similarly
389 -- if there is a left paren after the apostrophe, then we also
390 -- return with Token pointing to the apostrophe (this is the
391 -- qualified expression case).
393 if Token = Tok_Range or else Token = Tok_Left_Paren then
394 Restore_Scan_State (Scan_State); -- to apostrophe
395 Expr_Form := EF_Name;
398 -- Here for cases where attribute designator is an identifier
400 elsif Token = Tok_Identifier then
401 Attr_Name := Token_Name;
403 if not Is_Attribute_Name (Attr_Name) then
404 if Apostrophe_Should_Be_Semicolon then
405 Expr_Form := EF_Name;
408 Signal_Bad_Attribute;
413 Style.Check_Attribute_Name (False);
416 Delete_Node (Token_Node);
418 -- Here for case of attribute designator is not an identifier
421 if Token = Tok_Delta then
422 Attr_Name := Name_Delta;
424 elsif Token = Tok_Digits then
425 Attr_Name := Name_Digits;
427 elsif Token = Tok_Access then
428 Attr_Name := Name_Access;
430 elsif Apostrophe_Should_Be_Semicolon then
431 Expr_Form := EF_Name;
435 Error_Msg_AP ("attribute designator expected");
440 Style.Check_Attribute_Name (True);
444 -- We come here with an OK attribute scanned, and the
445 -- corresponding Attribute identifier node stored in Ident_Node.
447 Prefix_Node := Name_Node;
448 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
449 Scan; -- past attribute designator
450 Set_Prefix (Name_Node, Prefix_Node);
451 Set_Attribute_Name (Name_Node, Attr_Name);
453 -- Scan attribute arguments/designator
455 if Token = Tok_Left_Paren then
456 Set_Expressions (Name_Node, New_List);
457 Scan; -- past left paren
461 Expr : constant Node_Id := P_Expression;
464 if Token = Tok_Arrow then
466 ("named parameters not permitted for attributes");
467 Scan; -- past junk arrow
470 Append (Expr, Expressions (Name_Node));
471 exit when not Comma_Present;
479 goto Scan_Name_Extension;
482 -- Here for left parenthesis extending name (left paren skipped)
484 <<Scan_Name_Extension_Left_Paren>>
486 -- We now have to scan through a list of items, terminated by a
487 -- right parenthesis. The scan is handled by a finite state
488 -- machine. The possibilities are:
492 -- This is a slice. This case is handled in LP_State_Init.
494 -- (expression, expression, ..)
496 -- This is interpreted as an indexed component, i.e. as a
497 -- case of a name which can be extended in the normal manner.
498 -- This case is handled by LP_State_Name or LP_State_Expr.
500 -- (..., identifier => expression , ...)
502 -- If there is at least one occurence of identifier => (but
503 -- none of the other cases apply), then we have a call.
505 -- Test for Id => case
507 if Token = Tok_Identifier then
508 Save_Scan_State (Scan_State); -- at Id
511 -- Test for => (allow := as an error substitute)
513 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
514 Restore_Scan_State (Scan_State); -- to Id
515 Arg_List := New_List;
519 Restore_Scan_State (Scan_State); -- to Id
523 -- Here we have an expression after all
525 Expr_Node := P_Expression_Or_Range_Attribute;
527 -- Check cases of discrete range for a slice
529 -- First possibility: Range_Attribute_Reference
531 if Expr_Form = EF_Range_Attr then
532 Range_Node := Expr_Node;
534 -- Second possibility: Simple_expression .. Simple_expression
536 elsif Token = Tok_Dot_Dot then
537 Check_Simple_Expression (Expr_Node);
538 Range_Node := New_Node (N_Range, Token_Ptr);
539 Set_Low_Bound (Range_Node, Expr_Node);
541 Expr_Node := P_Expression;
542 Check_Simple_Expression (Expr_Node);
543 Set_High_Bound (Range_Node, Expr_Node);
545 -- Third possibility: Type_name range Range
547 elsif Token = Tok_Range then
548 if Expr_Form /= EF_Simple_Name then
549 Error_Msg_SC ("subtype mark must precede RANGE");
553 Range_Node := P_Subtype_Indication (Expr_Node);
555 -- Otherwise we just have an expression. It is true that we might
556 -- have a subtype mark without a range constraint but this case
557 -- is syntactically indistinguishable from the expression case.
560 Arg_List := New_List;
564 -- Fall through here with unmistakable Discrete range scanned,
565 -- which means that we definitely have the case of a slice. The
566 -- Discrete range is in Range_Node.
568 if Token = Tok_Comma then
569 Error_Msg_SC ("slice cannot have more than one dimension");
572 elsif Token /= Tok_Right_Paren then
577 Scan; -- past right paren
578 Prefix_Node := Name_Node;
579 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
580 Set_Prefix (Name_Node, Prefix_Node);
581 Set_Discrete_Range (Name_Node, Range_Node);
583 -- An operator node is legal as a prefix to other names,
584 -- but not for a slice.
586 if Nkind (Prefix_Node) = N_Operator_Symbol then
587 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
590 -- If we have a name extension, go scan it
592 if Token in Token_Class_Namext then
593 goto Scan_Name_Extension_OK;
595 -- Otherwise return (a slice is a name, but is not a call)
598 Expr_Form := EF_Name;
603 -- In LP_State_Expr, we have scanned one or more expressions, and
604 -- so we have a call or an indexed component which is a name. On
605 -- entry we have the expression just scanned in Expr_Node and
606 -- Arg_List contains the list of expressions encountered so far
609 Append (Expr_Node, Arg_List);
611 if Token = Tok_Arrow then
613 ("expect identifier in parameter association",
617 elsif not Comma_Present then
619 Prefix_Node := Name_Node;
620 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
621 Set_Prefix (Name_Node, Prefix_Node);
622 Set_Expressions (Name_Node, Arg_List);
623 goto Scan_Name_Extension;
626 -- Comma present (and scanned out), test for identifier => case
627 -- Test for identifer => case
629 if Token = Tok_Identifier then
630 Save_Scan_State (Scan_State); -- at Id
633 -- Test for => (allow := as error substitute)
635 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
636 Restore_Scan_State (Scan_State); -- to Id
639 -- Otherwise it's just an expression after all, so backup
642 Restore_Scan_State (Scan_State); -- to Id
646 -- Here we have an expression after all, so stay in this state
648 Expr_Node := P_Expression;
651 -- LP_State_Call corresponds to the situation in which at least
652 -- one instance of Id => Expression has been encountered, so we
653 -- know that we do not have a name, but rather a call. We enter
654 -- it with the scan pointer pointing to the next argument to scan,
655 -- and Arg_List containing the list of arguments scanned so far.
659 -- Test for case of Id => Expression (named parameter)
661 if Token = Tok_Identifier then
662 Save_Scan_State (Scan_State); -- at Id
663 Ident_Node := Token_Node;
666 -- Deal with => (allow := as erroneous substitute)
668 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
670 New_Node (N_Parameter_Association, Prev_Token_Ptr);
671 Set_Selector_Name (Arg_Node, Ident_Node);
673 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
674 Append (Arg_Node, Arg_List);
676 -- If a comma follows, go back and scan next entry
678 if Comma_Present then
681 -- Otherwise we have the end of a call
684 Prefix_Node := Name_Node;
686 New_Node (N_Function_Call, Sloc (Prefix_Node));
687 Set_Name (Name_Node, Prefix_Node);
688 Set_Parameter_Associations (Name_Node, Arg_List);
691 if Token in Token_Class_Namext then
692 goto Scan_Name_Extension_OK;
694 -- This is a case of a call which cannot be a name
697 Expr_Form := EF_Name;
702 -- Not named parameter: Id started an expression after all
705 Restore_Scan_State (Scan_State); -- to Id
709 -- Here if entry did not start with Id => which means that it
710 -- is a positional parameter, which is not allowed, since we
711 -- have seen at least one named parameter already.
714 ("positional parameter association " &
715 "not allowed after named one");
717 Expr_Node := P_Expression;
719 -- Leaving the '>' in an association is not unusual, so suggest
722 if Nkind (Expr_Node) = N_Op_Eq then
723 Error_Msg_N ("\maybe `=>` was intended", Expr_Node);
726 -- We go back to scanning out expressions, so that we do not get
727 -- multiple error messages when several positional parameters
728 -- follow a named parameter.
732 -- End of treatment for name extensions starting with left paren
734 -- End of loop through name extensions
738 -- This function parses a restricted form of Names which are either
739 -- designators, or designators preceded by a sequence of prefixes
740 -- that are direct names.
742 -- Error recovery: cannot raise Error_Resync
744 function P_Function_Name return Node_Id is
745 Designator_Node : Node_Id;
746 Prefix_Node : Node_Id;
747 Selector_Node : Node_Id;
748 Dot_Sloc : Source_Ptr := No_Location;
751 -- Prefix_Node is set to the gathered prefix so far, Empty means that
752 -- no prefix has been scanned. This allows us to build up the result
753 -- in the required right recursive manner.
755 Prefix_Node := Empty;
757 -- Loop through prefixes
760 Designator_Node := Token_Node;
762 if Token not in Token_Class_Desig then
763 return P_Identifier; -- let P_Identifier issue the error message
765 else -- Token in Token_Class_Desig
766 Scan; -- past designator
767 exit when Token /= Tok_Dot;
770 -- Here at a dot, with token just before it in Designator_Node
772 if No (Prefix_Node) then
773 Prefix_Node := Designator_Node;
775 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
776 Set_Prefix (Selector_Node, Prefix_Node);
777 Set_Selector_Name (Selector_Node, Designator_Node);
778 Prefix_Node := Selector_Node;
781 Dot_Sloc := Token_Ptr;
785 -- Fall out of the loop having just scanned a designator
787 if No (Prefix_Node) then
788 return Designator_Node;
790 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
791 Set_Prefix (Selector_Node, Prefix_Node);
792 Set_Selector_Name (Selector_Node, Designator_Node);
793 return Selector_Node;
802 -- This function parses a restricted form of Names which are either
803 -- identifiers, or identifiers preceded by a sequence of prefixes
804 -- that are direct names.
806 -- Error recovery: cannot raise Error_Resync
808 function P_Qualified_Simple_Name return Node_Id is
809 Designator_Node : Node_Id;
810 Prefix_Node : Node_Id;
811 Selector_Node : Node_Id;
812 Dot_Sloc : Source_Ptr := No_Location;
815 -- Prefix node is set to the gathered prefix so far, Empty means that
816 -- no prefix has been scanned. This allows us to build up the result
817 -- in the required right recursive manner.
819 Prefix_Node := Empty;
821 -- Loop through prefixes
824 Designator_Node := Token_Node;
826 if Token = Tok_Identifier then
827 Scan; -- past identifier
828 exit when Token /= Tok_Dot;
830 elsif Token not in Token_Class_Desig then
831 return P_Identifier; -- let P_Identifier issue the error message
834 Scan; -- past designator
836 if Token /= Tok_Dot then
837 Error_Msg_SP ("identifier expected");
842 -- Here at a dot, with token just before it in Designator_Node
844 if No (Prefix_Node) then
845 Prefix_Node := Designator_Node;
847 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
848 Set_Prefix (Selector_Node, Prefix_Node);
849 Set_Selector_Name (Selector_Node, Designator_Node);
850 Prefix_Node := Selector_Node;
853 Dot_Sloc := Token_Ptr;
857 -- Fall out of the loop having just scanned an identifier
859 if No (Prefix_Node) then
860 return Designator_Node;
862 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
863 Set_Prefix (Selector_Node, Prefix_Node);
864 Set_Selector_Name (Selector_Node, Designator_Node);
865 return Selector_Node;
872 end P_Qualified_Simple_Name;
874 -- This procedure differs from P_Qualified_Simple_Name only in that it
875 -- raises Error_Resync if any error is encountered. It only returns after
876 -- scanning a valid qualified simple name.
878 -- Error recovery: can raise Error_Resync
880 function P_Qualified_Simple_Name_Resync return Node_Id is
881 Designator_Node : Node_Id;
882 Prefix_Node : Node_Id;
883 Selector_Node : Node_Id;
884 Dot_Sloc : Source_Ptr := No_Location;
887 Prefix_Node := Empty;
889 -- Loop through prefixes
892 Designator_Node := Token_Node;
894 if Token = Tok_Identifier then
895 Scan; -- past identifier
896 exit when Token /= Tok_Dot;
898 elsif Token not in Token_Class_Desig then
899 Discard_Junk_Node (P_Identifier); -- to issue the error message
903 Scan; -- past designator
905 if Token /= Tok_Dot then
906 Error_Msg_SP ("identifier expected");
911 -- Here at a dot, with token just before it in Designator_Node
913 if No (Prefix_Node) then
914 Prefix_Node := Designator_Node;
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 Prefix_Node := Selector_Node;
922 Dot_Sloc := Token_Ptr;
926 -- Fall out of the loop having just scanned an identifier
928 if No (Prefix_Node) then
929 return Designator_Node;
931 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
932 Set_Prefix (Selector_Node, Prefix_Node);
933 Set_Selector_Name (Selector_Node, Designator_Node);
934 return Selector_Node;
937 end P_Qualified_Simple_Name_Resync;
939 ----------------------
940 -- 4.1 Direct_Name --
941 ----------------------
943 -- Parsed by P_Name and other functions in section 4.1
949 -- Parsed by P_Name (4.1)
951 -------------------------------
952 -- 4.1 Explicit Dereference --
953 -------------------------------
955 -- Parsed by P_Name (4.1)
957 -------------------------------
958 -- 4.1 Implicit_Dereference --
959 -------------------------------
961 -- Parsed by P_Name (4.1)
963 ----------------------------
964 -- 4.1 Indexed Component --
965 ----------------------------
967 -- Parsed by P_Name (4.1)
973 -- Parsed by P_Name (4.1)
975 -----------------------------
976 -- 4.1 Selected_Component --
977 -----------------------------
979 -- Parsed by P_Name (4.1)
981 ------------------------
982 -- 4.1 Selector Name --
983 ------------------------
985 -- Parsed by P_Name (4.1)
987 ------------------------------
988 -- 4.1 Attribute Reference --
989 ------------------------------
991 -- Parsed by P_Name (4.1)
993 -------------------------------
994 -- 4.1 Attribute Designator --
995 -------------------------------
997 -- Parsed by P_Name (4.1)
999 --------------------------------------
1000 -- 4.1.4 Range Attribute Reference --
1001 --------------------------------------
1003 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1005 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1007 -- In the grammar, a RANGE attribute is simply a name, but its use is
1008 -- highly restricted, so in the parser, we do not regard it as a name.
1009 -- Instead, P_Name returns without scanning the 'RANGE part of the
1010 -- attribute, and the caller uses the following function to construct
1011 -- a range attribute in places where it is appropriate.
1013 -- Note that RANGE here is treated essentially as an identifier,
1014 -- rather than a reserved word.
1016 -- The caller has parsed the prefix, i.e. a name, and Token points to
1017 -- the apostrophe. The token after the apostrophe is known to be RANGE
1018 -- at this point. The prefix node becomes the prefix of the attribute.
1020 -- Error_Recovery: Cannot raise Error_Resync
1022 function P_Range_Attribute_Reference
1023 (Prefix_Node : Node_Id)
1026 Attr_Node : Node_Id;
1029 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1030 Set_Prefix (Attr_Node, Prefix_Node);
1031 Scan; -- past apostrophe
1034 Style.Check_Attribute_Name (True);
1037 Set_Attribute_Name (Attr_Node, Name_Range);
1040 if Token = Tok_Left_Paren then
1041 Scan; -- past left paren
1042 Set_Expressions (Attr_Node, New_List (P_Expression));
1047 end P_Range_Attribute_Reference;
1049 ---------------------------------------
1050 -- 4.1.4 Range Attribute Designator --
1051 ---------------------------------------
1053 -- Parsed by P_Range_Attribute_Reference (4.4)
1055 --------------------
1057 --------------------
1059 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1061 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1062 -- an aggregate is known to be required (code statement, extension
1063 -- aggregate), in which cases this routine performs the necessary check
1064 -- that we have an aggregate rather than a parenthesized expression
1066 -- Error recovery: can raise Error_Resync
1068 function P_Aggregate return Node_Id is
1069 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1070 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1073 if Nkind (Aggr_Node) /= N_Aggregate
1075 Nkind (Aggr_Node) /= N_Extension_Aggregate
1078 ("aggregate may not have single positional component", Aggr_Sloc);
1085 -------------------------------------------------
1086 -- 4.3 Aggregate or Parenthesized Expresssion --
1087 -------------------------------------------------
1089 -- This procedure parses out either an aggregate or a parenthesized
1090 -- expression (these two constructs are closely related, since a
1091 -- parenthesized expression looks like an aggregate with a single
1092 -- positional component).
1095 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1097 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1099 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1100 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1103 -- RECORD_COMPONENT_ASSOCIATION ::=
1104 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1106 -- COMPONENT_CHOICE_LIST ::=
1107 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1110 -- EXTENSION_AGGREGATE ::=
1111 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1113 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1115 -- ARRAY_AGGREGATE ::=
1116 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1118 -- POSITIONAL_ARRAY_AGGREGATE ::=
1119 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1120 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1122 -- NAMED_ARRAY_AGGREGATE ::=
1123 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1125 -- PRIMARY ::= (EXPRESSION);
1127 -- Error recovery: can raise Error_Resync
1129 function P_Aggregate_Or_Paren_Expr return Node_Id is
1130 Aggregate_Node : Node_Id;
1131 Expr_List : List_Id;
1132 Assoc_List : List_Id;
1133 Expr_Node : Node_Id;
1134 Lparen_Sloc : Source_Ptr;
1135 Scan_State : Saved_Scan_State;
1138 Lparen_Sloc := Token_Ptr;
1141 -- Note: the mechanism used here of rescanning the initial expression
1142 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1143 -- out the discrete choice list.
1145 -- Deal with expression and extension aggregate cases first
1147 if Token /= Tok_Others then
1148 Save_Scan_State (Scan_State); -- at start of expression
1150 -- Deal with (NULL RECORD) case
1152 if Token = Tok_Null then
1155 if Token = Tok_Record then
1156 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1157 Set_Null_Record_Present (Aggregate_Node, True);
1158 Scan; -- past RECORD
1160 return Aggregate_Node;
1162 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1166 Expr_Node := P_Expression_Or_Range_Attribute;
1168 -- Extension aggregate case
1170 if Token = Tok_With then
1172 if Nkind (Expr_Node) = N_Attribute_Reference
1173 and then Attribute_Name (Expr_Node) = Name_Range
1175 Bad_Range_Attribute (Sloc (Expr_Node));
1180 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1183 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1184 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1187 -- Deal with WITH NULL RECORD case
1189 if Token = Tok_Null then
1190 Save_Scan_State (Scan_State); -- at NULL
1193 if Token = Tok_Record then
1194 Scan; -- past RECORD
1195 Set_Null_Record_Present (Aggregate_Node, True);
1197 return Aggregate_Node;
1200 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1204 if Token /= Tok_Others then
1205 Save_Scan_State (Scan_State);
1206 Expr_Node := P_Expression;
1213 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1215 if Nkind (Expr_Node) = N_Attribute_Reference
1216 and then Attribute_Name (Expr_Node) = Name_Range
1218 Bad_Range_Attribute (Sloc (Expr_Node));
1222 -- Bump paren count of expression, note that if the paren count
1223 -- is already at the maximum, then we leave it alone. This will
1224 -- cause some failures in pathalogical conformance tests, which
1225 -- we do not shed a tear over!
1227 if Expr_Node /= Error then
1228 if Paren_Count (Expr_Node) /= Paren_Count_Type'Last then
1229 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1233 T_Right_Paren; -- past right paren (error message if none)
1236 -- Normal aggregate case
1239 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1245 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1249 -- Prepare to scan list of component associations
1251 Expr_List := No_List; -- don't set yet, maybe all named entries
1252 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1254 -- This loop scans through component associations. On entry to the
1255 -- loop, an expression has been scanned at the start of the current
1256 -- association unless initial token was OTHERS, in which case
1257 -- Expr_Node is set to Empty.
1260 -- Deal with others association first. This is a named association
1262 if No (Expr_Node) then
1263 if No (Assoc_List) then
1264 Assoc_List := New_List;
1267 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1269 -- Improper use of WITH
1271 elsif Token = Tok_With then
1272 Error_Msg_SC ("WITH must be preceded by single expression in " &
1273 "extension aggregate");
1276 -- Assume positional case if comma, right paren, or literal or
1277 -- identifier or OTHERS follows (the latter cases are missing
1278 -- comma cases). Also assume positional if a semicolon follows,
1279 -- which can happen if there are missing parens
1281 elsif Token = Tok_Comma
1282 or else Token = Tok_Right_Paren
1283 or else Token = Tok_Others
1284 or else Token in Token_Class_Lit_Or_Name
1285 or else Token = Tok_Semicolon
1287 if Present (Assoc_List) then
1289 ("""=>"" expected (positional association cannot follow " &
1290 "named association)");
1293 if No (Expr_List) then
1294 Expr_List := New_List;
1297 Append (Expr_Node, Expr_List);
1299 -- Anything else is assumed to be a named association
1302 Restore_Scan_State (Scan_State); -- to start of expression
1304 if No (Assoc_List) then
1305 Assoc_List := New_List;
1308 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1311 exit when not Comma_Present;
1313 -- If we are at an expression terminator, something is seriously
1314 -- wrong, so let's get out now, before we start eating up stuff
1315 -- that doesn't belong to us!
1317 if Token in Token_Class_Eterm then
1318 Error_Msg_AP ("expecting expression or component association");
1322 -- Otherwise initiate for reentry to top of loop by scanning an
1323 -- initial expression, unless the first token is OTHERS.
1325 if Token = Tok_Others then
1328 Save_Scan_State (Scan_State); -- at start of expression
1329 Expr_Node := P_Expression;
1333 -- All component associations (positional and named) have been scanned
1336 Set_Expressions (Aggregate_Node, Expr_List);
1337 Set_Component_Associations (Aggregate_Node, Assoc_List);
1338 return Aggregate_Node;
1339 end P_Aggregate_Or_Paren_Expr;
1341 ------------------------------------------------
1342 -- 4.3 Record or Array Component Association --
1343 ------------------------------------------------
1345 -- RECORD_COMPONENT_ASSOCIATION ::=
1346 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1348 -- COMPONENT_CHOICE_LIST =>
1349 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1352 -- ARRAY_COMPONENT_ASSOCIATION ::=
1353 -- DISCRETE_CHOICE_LIST => EXPRESSION
1355 -- Note: this routine only handles the named cases, including others.
1356 -- Cases where the component choice list is not present have already
1357 -- been handled directly.
1359 -- Error recovery: can raise Error_Resync
1361 function P_Record_Or_Array_Component_Association return Node_Id is
1362 Assoc_Node : Node_Id;
1365 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1366 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1367 Set_Sloc (Assoc_Node, Token_Ptr);
1369 Set_Expression (Assoc_Node, P_Expression);
1371 end P_Record_Or_Array_Component_Association;
1373 -----------------------------
1374 -- 4.3.1 Record Aggregate --
1375 -----------------------------
1377 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1378 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1380 ----------------------------------------------
1381 -- 4.3.1 Record Component Association List --
1382 ----------------------------------------------
1384 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1386 ----------------------------------
1387 -- 4.3.1 Component Choice List --
1388 ----------------------------------
1390 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1392 --------------------------------
1393 -- 4.3.1 Extension Aggregate --
1394 --------------------------------
1396 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1398 --------------------------
1399 -- 4.3.1 Ancestor Part --
1400 --------------------------
1402 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1404 ----------------------------
1405 -- 4.3.1 Array Aggregate --
1406 ----------------------------
1408 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1410 ---------------------------------------
1411 -- 4.3.1 Positional Array Aggregate --
1412 ---------------------------------------
1414 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1416 ----------------------------------
1417 -- 4.3.1 Named Array Aggregate --
1418 ----------------------------------
1420 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1422 ----------------------------------------
1423 -- 4.3.1 Array Component Association --
1424 ----------------------------------------
1426 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1428 ---------------------
1429 -- 4.4 Expression --
1430 ---------------------
1433 -- RELATION {and RELATION} | RELATION {and then RELATION}
1434 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1435 -- | RELATION {xor RELATION}
1437 -- On return, Expr_Form indicates the categorization of the expression
1438 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1439 -- an error message is given, and Error is returned).
1441 -- Error recovery: cannot raise Error_Resync
1443 function P_Expression return Node_Id is
1444 Logical_Op : Node_Kind;
1445 Prev_Logical_Op : Node_Kind;
1446 Op_Location : Source_Ptr;
1451 Node1 := P_Relation;
1453 if Token in Token_Class_Logop then
1454 Prev_Logical_Op := N_Empty;
1457 Op_Location := Token_Ptr;
1458 Logical_Op := P_Logical_Operator;
1460 if Prev_Logical_Op /= N_Empty and then
1461 Logical_Op /= Prev_Logical_Op
1464 ("mixed logical operators in expression", Op_Location);
1465 Prev_Logical_Op := N_Empty;
1467 Prev_Logical_Op := Logical_Op;
1471 Node1 := New_Node (Logical_Op, Op_Location);
1472 Set_Left_Opnd (Node1, Node2);
1473 Set_Right_Opnd (Node1, P_Relation);
1474 Set_Op_Name (Node1);
1475 exit when Token not in Token_Class_Logop;
1478 Expr_Form := EF_Non_Simple;
1481 if Token = Tok_Apostrophe then
1482 Bad_Range_Attribute (Token_Ptr);
1490 -- This function is identical to the normal P_Expression, except that it
1491 -- checks that the expression scan did not stop on a right paren. It is
1492 -- called in all contexts where a right parenthesis cannot legitimately
1493 -- follow an expression.
1495 function P_Expression_No_Right_Paren return Node_Id is
1497 return No_Right_Paren (P_Expression);
1498 end P_Expression_No_Right_Paren;
1500 ----------------------------------------
1501 -- 4.4 Expression_Or_Range_Attribute --
1502 ----------------------------------------
1505 -- RELATION {and RELATION} | RELATION {and then RELATION}
1506 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1507 -- | RELATION {xor RELATION}
1509 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1511 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1513 -- On return, Expr_Form indicates the categorization of the expression
1514 -- and EF_Range_Attr is one of the possibilities.
1516 -- Error recovery: cannot raise Error_Resync
1518 -- In the grammar, a RANGE attribute is simply a name, but its use is
1519 -- highly restricted, so in the parser, we do not regard it as a name.
1520 -- Instead, P_Name returns without scanning the 'RANGE part of the
1521 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1522 -- attribute reference. In the normal case where a range attribute is
1523 -- not allowed, an error message is issued by P_Expression.
1525 function P_Expression_Or_Range_Attribute return Node_Id is
1526 Logical_Op : Node_Kind;
1527 Prev_Logical_Op : Node_Kind;
1528 Op_Location : Source_Ptr;
1531 Attr_Node : Node_Id;
1534 Node1 := P_Relation;
1536 if Token = Tok_Apostrophe then
1537 Attr_Node := P_Range_Attribute_Reference (Node1);
1538 Expr_Form := EF_Range_Attr;
1541 elsif Token in Token_Class_Logop then
1542 Prev_Logical_Op := N_Empty;
1545 Op_Location := Token_Ptr;
1546 Logical_Op := P_Logical_Operator;
1548 if Prev_Logical_Op /= N_Empty and then
1549 Logical_Op /= Prev_Logical_Op
1552 ("mixed logical operators in expression", Op_Location);
1553 Prev_Logical_Op := N_Empty;
1555 Prev_Logical_Op := Logical_Op;
1559 Node1 := New_Node (Logical_Op, Op_Location);
1560 Set_Left_Opnd (Node1, Node2);
1561 Set_Right_Opnd (Node1, P_Relation);
1562 Set_Op_Name (Node1);
1563 exit when Token not in Token_Class_Logop;
1566 Expr_Form := EF_Non_Simple;
1569 if Token = Tok_Apostrophe then
1570 Bad_Range_Attribute (Token_Ptr);
1575 end P_Expression_Or_Range_Attribute;
1582 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1583 -- | SIMPLE_EXPRESSION [not] in RANGE
1584 -- | SIMPLE_EXPRESSION [not] in SUBTYPE_MARK
1586 -- On return, Expr_Form indicates the categorization of the expression
1588 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1589 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1591 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1592 -- expression, then tokens are scanned until either a non-expression token,
1593 -- a right paren (not matched by a left paren) or a comma, is encountered.
1595 function P_Relation return Node_Id is
1596 Node1, Node2 : Node_Id;
1600 Node1 := P_Simple_Expression;
1602 if Token not in Token_Class_Relop then
1606 -- Here we have a relational operator following. If so then scan it
1607 -- out. Note that the assignment symbol := is treated as a relational
1608 -- operator to improve the error recovery when it is misused for =.
1609 -- P_Relational_Operator also parses the IN and NOT IN operations.
1612 Node2 := New_Node (P_Relational_Operator, Optok);
1613 Set_Left_Opnd (Node2, Node1);
1614 Set_Op_Name (Node2);
1616 -- Case of IN or NOT IN
1618 if Prev_Token = Tok_In then
1619 Set_Right_Opnd (Node2, P_Range_Or_Subtype_Mark);
1621 -- Case of relational operator (= /= < <= > >=)
1624 Set_Right_Opnd (Node2, P_Simple_Expression);
1627 Expr_Form := EF_Non_Simple;
1629 if Token in Token_Class_Relop then
1630 Error_Msg_SC ("unexpected relational operator");
1637 -- If any error occurs, then scan to the next expression terminator symbol
1638 -- or comma or right paren at the outer (i.e. current) parentheses level.
1639 -- The flags are set to indicate a normal simple expression.
1642 when Error_Resync =>
1644 Expr_Form := EF_Simple;
1648 ----------------------------
1649 -- 4.4 Simple Expression --
1650 ----------------------------
1652 -- SIMPLE_EXPRESSION ::=
1653 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1655 -- On return, Expr_Form indicates the categorization of the expression
1657 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1658 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1660 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1661 -- expression, then tokens are scanned until either a non-expression token,
1662 -- a right paren (not matched by a left paren) or a comma, is encountered.
1664 -- Note: P_Simple_Expression is called only internally by higher level
1665 -- expression routines. In cases in the grammar where a simple expression
1666 -- is required, the approach is to scan an expression, and then post an
1667 -- appropriate error message if the expression obtained is not simple. This
1668 -- gives better error recovery and treatment.
1670 function P_Simple_Expression return Node_Id is
1671 Scan_State : Saved_Scan_State;
1674 Tokptr : Source_Ptr;
1677 -- Check for cases starting with a name. There are two reasons for
1678 -- special casing. First speed things up by catching a common case
1679 -- without going through several routine layers. Second the caller must
1680 -- be informed via Expr_Form when the simple expression is a name.
1682 if Token in Token_Class_Name then
1685 -- Deal with apostrophe cases
1687 if Token = Tok_Apostrophe then
1688 Save_Scan_State (Scan_State); -- at apostrophe
1689 Scan; -- past apostrophe
1691 -- If qualified expression, scan it out and fall through
1693 if Token = Tok_Left_Paren then
1694 Node1 := P_Qualified_Expression (Node1);
1695 Expr_Form := EF_Simple;
1697 -- If range attribute, then we return with Token pointing to the
1698 -- apostrophe. Note: avoid the normal error check on exit. We
1699 -- know that the expression really is complete in this case!
1701 else -- Token = Tok_Range then
1702 Restore_Scan_State (Scan_State); -- to apostrophe
1703 Expr_Form := EF_Simple_Name;
1708 -- If an expression terminator follows, the previous processing
1709 -- completely scanned out the expression (a common case), and
1710 -- left Expr_Form set appropriately for returning to our caller.
1712 if Token in Token_Class_Sterm then
1715 -- If we do not have an expression terminator, then complete the
1716 -- scan of a simple expression. This code duplicates the code
1717 -- found in P_Term and P_Factor.
1720 if Token = Tok_Double_Asterisk then
1721 if Style_Check then Style.Check_Exponentiation_Operator; end if;
1722 Node2 := New_Node (N_Op_Expon, Token_Ptr);
1724 Set_Left_Opnd (Node2, Node1);
1725 Set_Right_Opnd (Node2, P_Primary);
1726 Set_Op_Name (Node2);
1731 exit when Token not in Token_Class_Mulop;
1732 Tokptr := Token_Ptr;
1733 Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1734 if Style_Check then Style.Check_Binary_Operator; end if;
1735 Scan; -- past operator
1736 Set_Left_Opnd (Node2, Node1);
1737 Set_Right_Opnd (Node2, P_Factor);
1738 Set_Op_Name (Node2);
1743 exit when Token not in Token_Class_Binary_Addop;
1744 Tokptr := Token_Ptr;
1745 Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1746 if Style_Check then Style.Check_Binary_Operator; end if;
1747 Scan; -- past operator
1748 Set_Left_Opnd (Node2, Node1);
1749 Set_Right_Opnd (Node2, P_Term);
1750 Set_Op_Name (Node2);
1754 Expr_Form := EF_Simple;
1757 -- Cases where simple expression does not start with a name
1760 -- Scan initial sign and initial Term
1762 if Token in Token_Class_Unary_Addop then
1763 Tokptr := Token_Ptr;
1764 Node1 := New_Node (P_Unary_Adding_Operator, Tokptr);
1765 if Style_Check then Style.Check_Unary_Plus_Or_Minus; end if;
1766 Scan; -- past operator
1767 Set_Right_Opnd (Node1, P_Term);
1768 Set_Op_Name (Node1);
1773 -- Scan out sequence of terms separated by binary adding operators
1776 exit when Token not in Token_Class_Binary_Addop;
1777 Tokptr := Token_Ptr;
1778 Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1779 Scan; -- past operator
1780 Set_Left_Opnd (Node2, Node1);
1781 Set_Right_Opnd (Node2, P_Term);
1782 Set_Op_Name (Node2);
1786 -- All done, we clearly do not have name or numeric literal so this
1787 -- is a case of a simple expression which is some other possibility.
1789 Expr_Form := EF_Simple;
1792 -- Come here at end of simple expression, where we do a couple of
1793 -- special checks to improve error recovery.
1795 -- Special test to improve error recovery. If the current token
1796 -- is a period, then someone is trying to do selection on something
1797 -- that is not a name, e.g. a qualified expression.
1799 if Token = Tok_Dot then
1800 Error_Msg_SC ("prefix for selection is not a name");
1804 -- Special test to improve error recovery: If the current token is
1805 -- not the first token on a line (as determined by checking the
1806 -- previous token position with the start of the current line),
1807 -- then we insist that we have an appropriate terminating token.
1808 -- Consider the following two examples:
1810 -- 1) if A nad B then ...
1815 -- In the first example, we would like to issue a binary operator
1816 -- expected message and resynchronize to the then. In the second
1817 -- example, we do not want to issue a binary operator message, so
1818 -- that instead we will get the missing semicolon message. This
1819 -- distinction is of course a heuristic which does not always work,
1820 -- but in practice it is quite effective.
1822 -- Note: the one case in which we do not go through this circuit is
1823 -- when we have scanned a range attribute and want to return with
1824 -- Token pointing to the apostrophe. The apostrophe is not normally
1825 -- an expression terminator, and is not in Token_Class_Sterm, but
1826 -- in this special case we know that the expression is complete.
1828 if not Token_Is_At_Start_Of_Line
1829 and then Token not in Token_Class_Sterm
1831 Error_Msg_AP ("binary operator expected");
1837 -- If any error occurs, then scan to next expression terminator symbol
1838 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
1839 -- level. Expr_Form is set to indicate a normal simple expression.
1842 when Error_Resync =>
1844 Expr_Form := EF_Simple;
1847 end P_Simple_Expression;
1849 -----------------------------------------------
1850 -- 4.4 Simple Expression or Range Attribute --
1851 -----------------------------------------------
1853 -- SIMPLE_EXPRESSION ::=
1854 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1856 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1858 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1860 -- Error recovery: cannot raise Error_Resync
1862 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
1864 Attr_Node : Node_Id;
1867 Sexpr := P_Simple_Expression;
1869 if Token = Tok_Apostrophe then
1870 Attr_Node := P_Range_Attribute_Reference (Sexpr);
1871 Expr_Form := EF_Range_Attr;
1877 end P_Simple_Expression_Or_Range_Attribute;
1883 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
1885 -- Error recovery: can raise Error_Resync
1887 function P_Term return Node_Id is
1888 Node1, Node2 : Node_Id;
1889 Tokptr : Source_Ptr;
1895 exit when Token not in Token_Class_Mulop;
1896 Tokptr := Token_Ptr;
1897 Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1898 Scan; -- past operator
1899 Set_Left_Opnd (Node2, Node1);
1900 Set_Right_Opnd (Node2, P_Factor);
1901 Set_Op_Name (Node2);
1912 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
1914 -- Error recovery: can raise Error_Resync
1916 function P_Factor return Node_Id is
1921 if Token = Tok_Abs then
1922 Node1 := New_Node (N_Op_Abs, Token_Ptr);
1923 if Style_Check then Style.Check_Abs_Not; end if;
1925 Set_Right_Opnd (Node1, P_Primary);
1926 Set_Op_Name (Node1);
1929 elsif Token = Tok_Not then
1930 Node1 := New_Node (N_Op_Not, Token_Ptr);
1931 if Style_Check then Style.Check_Abs_Not; end if;
1933 Set_Right_Opnd (Node1, P_Primary);
1934 Set_Op_Name (Node1);
1940 if Token = Tok_Double_Asterisk then
1941 Node2 := New_Node (N_Op_Expon, Token_Ptr);
1943 Set_Left_Opnd (Node2, Node1);
1944 Set_Right_Opnd (Node2, P_Primary);
1945 Set_Op_Name (Node2);
1958 -- NUMERIC_LITERAL | null
1959 -- | STRING_LITERAL | AGGREGATE
1960 -- | NAME | QUALIFIED_EXPRESSION
1961 -- | ALLOCATOR | (EXPRESSION)
1963 -- Error recovery: can raise Error_Resync
1965 function P_Primary return Node_Id is
1966 Scan_State : Saved_Scan_State;
1970 -- The loop runs more than once only if misplaced pragmas are found
1975 -- Name token can start a name, call or qualified expression, all
1976 -- of which are acceptable possibilities for primary. Note also
1977 -- that string literal is included in name (as operator symbol)
1978 -- and type conversion is included in name (as indexed component).
1980 when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
1983 -- All done unless apostrophe follows
1985 if Token /= Tok_Apostrophe then
1988 -- Apostrophe following means that we have either just parsed
1989 -- the subtype mark of a qualified expression, or the prefix
1990 -- or a range attribute.
1992 else -- Token = Tok_Apostrophe
1993 Save_Scan_State (Scan_State); -- at apostrophe
1994 Scan; -- past apostrophe
1996 -- If range attribute, then this is always an error, since
1997 -- the only legitimate case (where the scanned expression is
1998 -- a qualified simple name) is handled at the level of the
1999 -- Simple_Expression processing. This case corresponds to a
2000 -- usage such as 3 + A'Range, which is always illegal.
2002 if Token = Tok_Range then
2003 Restore_Scan_State (Scan_State); -- to apostrophe
2004 Bad_Range_Attribute (Token_Ptr);
2007 -- If left paren, then we have a qualified expression.
2008 -- Note that P_Name guarantees that in this case, where
2009 -- Token = Tok_Apostrophe on return, the only two possible
2010 -- tokens following the apostrophe are left paren and
2011 -- RANGE, so we know we have a left paren here.
2013 else -- Token = Tok_Left_Paren
2014 return P_Qualified_Expression (Node1);
2019 -- Numeric or string literal
2021 when Tok_Integer_Literal |
2023 Tok_String_Literal =>
2025 Node1 := Token_Node;
2026 Scan; -- past number
2029 -- Left paren, starts aggregate or parenthesized expression
2031 when Tok_Left_Paren =>
2032 return P_Aggregate_Or_Paren_Expr;
2043 return New_Node (N_Null, Prev_Token_Ptr);
2045 -- Pragma, not allowed here, so just skip past it
2048 P_Pragmas_Misplaced;
2050 -- Anything else is illegal as the first token of a primary, but
2051 -- we test for a reserved identifier so that it is treated nicely
2054 if Is_Reserved_Identifier then
2055 return P_Identifier;
2057 elsif Prev_Token = Tok_Comma then
2058 Error_Msg_SP ("extra "","" ignored");
2062 Error_Msg_AP ("missing operand");
2070 ---------------------------
2071 -- 4.5 Logical Operator --
2072 ---------------------------
2074 -- LOGICAL_OPERATOR ::= and | or | xor
2076 -- Note: AND THEN and OR ELSE are also treated as logical operators
2077 -- by the parser (even though they are not operators semantically)
2079 -- The value returned is the appropriate Node_Kind code for the operator
2080 -- On return, Token points to the token following the scanned operator.
2082 -- The caller has checked that the first token is a legitimate logical
2083 -- operator token (i.e. is either XOR, AND, OR).
2085 -- Error recovery: cannot raise Error_Resync
2087 function P_Logical_Operator return Node_Kind is
2089 if Token = Tok_And then
2090 if Style_Check then Style.Check_Binary_Operator; end if;
2093 if Token = Tok_Then then
2100 elsif Token = Tok_Or then
2101 if Style_Check then Style.Check_Binary_Operator; end if;
2104 if Token = Tok_Else then
2111 else -- Token = Tok_Xor
2112 if Style_Check then Style.Check_Binary_Operator; end if;
2116 end P_Logical_Operator;
2118 ------------------------------
2119 -- 4.5 Relational Operator --
2120 ------------------------------
2122 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2124 -- The value returned is the appropriate Node_Kind code for the operator.
2125 -- On return, Token points to the operator token, NOT past it.
2127 -- The caller has checked that the first token is a legitimate relational
2128 -- operator token (i.e. is one of the operator tokens listed above).
2130 -- Error recovery: cannot raise Error_Resync
2132 function P_Relational_Operator return Node_Kind is
2133 Op_Kind : Node_Kind;
2134 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2135 (Tok_Less => N_Op_Lt,
2136 Tok_Equal => N_Op_Eq,
2137 Tok_Greater => N_Op_Gt,
2138 Tok_Not_Equal => N_Op_Ne,
2139 Tok_Greater_Equal => N_Op_Ge,
2140 Tok_Less_Equal => N_Op_Le,
2142 Tok_Not => N_Not_In,
2143 Tok_Box => N_Op_Ne);
2146 if Token = Tok_Box then
2147 Error_Msg_SC ("""<>"" should be ""/=""");
2150 Op_Kind := Relop_Node (Token);
2151 if Style_Check then Style.Check_Binary_Operator; end if;
2152 Scan; -- past operator token
2154 if Prev_Token = Tok_Not then
2159 end P_Relational_Operator;
2161 ---------------------------------
2162 -- 4.5 Binary Adding Operator --
2163 ---------------------------------
2165 -- BINARY_ADDING_OPERATOR ::= + | - | &
2167 -- The value returned is the appropriate Node_Kind code for the operator.
2168 -- On return, Token points to the operator token (NOT past it).
2170 -- The caller has checked that the first token is a legitimate adding
2171 -- operator token (i.e. is one of the operator tokens listed above).
2173 -- Error recovery: cannot raise Error_Resync
2175 function P_Binary_Adding_Operator return Node_Kind is
2176 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2177 (Tok_Ampersand => N_Op_Concat,
2178 Tok_Minus => N_Op_Subtract,
2179 Tok_Plus => N_Op_Add);
2181 return Addop_Node (Token);
2182 end P_Binary_Adding_Operator;
2184 --------------------------------
2185 -- 4.5 Unary Adding Operator --
2186 --------------------------------
2188 -- UNARY_ADDING_OPERATOR ::= + | -
2190 -- The value returned is the appropriate Node_Kind code for the operator.
2191 -- On return, Token points to the operator token (NOT past it).
2193 -- The caller has checked that the first token is a legitimate adding
2194 -- operator token (i.e. is one of the operator tokens listed above).
2196 -- Error recovery: cannot raise Error_Resync
2198 function P_Unary_Adding_Operator return Node_Kind is
2199 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2200 (Tok_Minus => N_Op_Minus,
2201 Tok_Plus => N_Op_Plus);
2203 return Addop_Node (Token);
2204 end P_Unary_Adding_Operator;
2206 -------------------------------
2207 -- 4.5 Multiplying Operator --
2208 -------------------------------
2210 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
2212 -- The value returned is the appropriate Node_Kind code for the operator.
2213 -- On return, Token points to the operator token (NOT past it).
2215 -- The caller has checked that the first token is a legitimate multiplying
2216 -- operator token (i.e. is one of the operator tokens listed above).
2218 -- Error recovery: cannot raise Error_Resync
2220 function P_Multiplying_Operator return Node_Kind is
2221 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2222 (Tok_Asterisk => N_Op_Multiply,
2223 Tok_Mod => N_Op_Mod,
2224 Tok_Rem => N_Op_Rem,
2225 Tok_Slash => N_Op_Divide);
2227 return Mulop_Node (Token);
2228 end P_Multiplying_Operator;
2230 --------------------------------------
2231 -- 4.5 Highest Precedence Operator --
2232 --------------------------------------
2234 -- Parsed by P_Factor (4.4)
2236 -- Note: this rule is not in fact used by the grammar at any point!
2238 --------------------------
2239 -- 4.6 Type Conversion --
2240 --------------------------
2242 -- Parsed by P_Primary as a Name (4.1)
2244 -------------------------------
2245 -- 4.7 Qualified Expression --
2246 -------------------------------
2248 -- QUALIFIED_EXPRESSION ::=
2249 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2251 -- The caller has scanned the name which is the Subtype_Mark parameter
2252 -- and scanned past the single quote following the subtype mark. The
2253 -- caller has not checked that this name is in fact appropriate for
2254 -- a subtype mark name (i.e. it is a selected component or identifier).
2256 -- Error_Recovery: cannot raise Error_Resync
2258 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2259 Qual_Node : Node_Id;
2262 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2263 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2264 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2266 end P_Qualified_Expression;
2268 --------------------
2270 --------------------
2273 -- new SUBTYPE_INDICATION | new QUALIFIED_EXPRESSION
2275 -- The caller has checked that the initial token is NEW
2277 -- Error recovery: can raise Error_Resync
2279 function P_Allocator return Node_Id is
2280 Alloc_Node : Node_Id;
2281 Type_Node : Node_Id;
2284 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2286 Type_Node := P_Subtype_Mark_Resync;
2288 if Token = Tok_Apostrophe then
2289 Scan; -- past apostrophe
2290 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2292 Set_Expression (Alloc_Node, P_Subtype_Indication (Type_Node));