1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
35 -- Attributes that cannot have arguments
37 Is_Parameterless_Attribute : constant Attribute_Class_Array :=
38 (Attribute_Body_Version => True,
39 Attribute_External_Tag => True,
40 Attribute_Img => True,
41 Attribute_Version => True,
42 Attribute_Base => True,
43 Attribute_Class => True,
44 Attribute_Stub_Type => True,
46 -- This map contains True for parameterless attributes that return a
47 -- string or a type. For those attributes, a left parenthesis after
48 -- the attribute should not be analyzed as the beginning of a parameters
49 -- list because it may denote a slice operation (X'Img (1 .. 2)) or
50 -- a type conversion (X'Class (Y)).
52 -- Note that this map designates the minimum set of attributes where a
53 -- construct in parentheses that is not an argument can appear right
54 -- after the attribute. For attributes like 'Size, we do not put them
55 -- in the map. If someone writes X'Size (3), that's illegal in any case,
56 -- but we get a better error message by parsing the (3) as an illegal
57 -- argument to the attribute, rather than some meaningless junk that
58 -- follows the attribute.
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
64 function P_Aggregate_Or_Paren_Expr return Node_Id;
65 function P_Allocator return Node_Id;
66 function P_Record_Or_Array_Component_Association return Node_Id;
67 function P_Factor return Node_Id;
68 function P_Primary return Node_Id;
69 function P_Relation return Node_Id;
70 function P_Term return Node_Id;
72 function P_Binary_Adding_Operator return Node_Kind;
73 function P_Logical_Operator return Node_Kind;
74 function P_Multiplying_Operator return Node_Kind;
75 function P_Relational_Operator return Node_Kind;
76 function P_Unary_Adding_Operator return Node_Kind;
78 procedure Bad_Range_Attribute (Loc : Source_Ptr);
79 -- Called to place complaint about bad range attribute at the given
80 -- source location. Terminates by raising Error_Resync.
82 function P_Range_Attribute_Reference (Prefix_Node : Node_Id) return Node_Id;
83 -- Scan a range attribute reference. The caller has scanned out the
84 -- prefix. The current token is known to be an apostrophe and the
85 -- following token is known to be RANGE.
87 procedure Set_Op_Name (Node : Node_Id);
88 -- Procedure to set name field (Chars) in operator node
90 -------------------------
91 -- Bad_Range_Attribute --
92 -------------------------
94 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
96 Error_Msg ("range attribute cannot be used in expression!", Loc);
98 end Bad_Range_Attribute;
104 procedure Set_Op_Name (Node : Node_Id) is
105 type Name_Of_Type is array (N_Op) of Name_Id;
106 Name_Of : constant Name_Of_Type := Name_Of_Type'(
107 N_Op_And => Name_Op_And,
108 N_Op_Or => Name_Op_Or,
109 N_Op_Xor => Name_Op_Xor,
110 N_Op_Eq => Name_Op_Eq,
111 N_Op_Ne => Name_Op_Ne,
112 N_Op_Lt => Name_Op_Lt,
113 N_Op_Le => Name_Op_Le,
114 N_Op_Gt => Name_Op_Gt,
115 N_Op_Ge => Name_Op_Ge,
116 N_Op_Add => Name_Op_Add,
117 N_Op_Subtract => Name_Op_Subtract,
118 N_Op_Concat => Name_Op_Concat,
119 N_Op_Multiply => Name_Op_Multiply,
120 N_Op_Divide => Name_Op_Divide,
121 N_Op_Mod => Name_Op_Mod,
122 N_Op_Rem => Name_Op_Rem,
123 N_Op_Expon => Name_Op_Expon,
124 N_Op_Plus => Name_Op_Add,
125 N_Op_Minus => Name_Op_Subtract,
126 N_Op_Abs => Name_Op_Abs,
127 N_Op_Not => Name_Op_Not,
129 -- We don't really need these shift operators, since they never
130 -- appear as operators in the source, but the path of least
131 -- resistance is to put them in (the aggregate must be complete)
133 N_Op_Rotate_Left => Name_Rotate_Left,
134 N_Op_Rotate_Right => Name_Rotate_Right,
135 N_Op_Shift_Left => Name_Shift_Left,
136 N_Op_Shift_Right => Name_Shift_Right,
137 N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
140 if Nkind (Node) in N_Op then
141 Set_Chars (Node, Name_Of (Nkind (Node)));
145 --------------------------
146 -- 4.1 Name (also 6.4) --
147 --------------------------
150 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
151 -- | INDEXED_COMPONENT | SLICE
152 -- | SELECTED_COMPONENT | ATTRIBUTE
153 -- | TYPE_CONVERSION | FUNCTION_CALL
154 -- | CHARACTER_LITERAL
156 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
158 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
160 -- EXPLICIT_DEREFERENCE ::= NAME . all
162 -- IMPLICIT_DEREFERENCE ::= NAME
164 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
166 -- SLICE ::= PREFIX (DISCRETE_RANGE)
168 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
170 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
172 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
174 -- ATTRIBUTE_DESIGNATOR ::=
175 -- IDENTIFIER [(static_EXPRESSION)]
176 -- | access | delta | digits
180 -- | function_PREFIX ACTUAL_PARAMETER_PART
182 -- ACTUAL_PARAMETER_PART ::=
183 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
185 -- PARAMETER_ASSOCIATION ::=
186 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
188 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
190 -- Note: syntactically a procedure call looks just like a function call,
191 -- so this routine is in practice used to scan out procedure calls as well.
193 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
195 -- Error recovery: can raise Error_Resync
197 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
198 -- followed by either a left paren (qualified expression case), or by
199 -- range (range attribute case). All other uses of apostrophe (i.e. all
200 -- other attributes) are handled in this routine.
202 -- Error recovery: can raise Error_Resync
204 function P_Name return Node_Id is
205 Scan_State : Saved_Scan_State;
207 Prefix_Node : Node_Id;
208 Ident_Node : Node_Id;
210 Range_Node : Node_Id;
213 Arg_List : List_Id := No_List; -- kill junk warning
214 Attr_Name : Name_Id := No_Name; -- kill junk warning
217 -- Case of not a name
219 if Token not in Token_Class_Name then
221 -- If it looks like start of expression, complain and scan expression
223 if Token in Token_Class_Literal
224 or else Token = Tok_Left_Paren
226 Error_Msg_SC ("name expected");
229 -- Otherwise some other junk, not much we can do
232 Error_Msg_AP ("name expected");
237 -- Loop through designators in qualified name
239 Name_Node := Token_Node;
242 Scan; -- past designator
243 exit when Token /= Tok_Dot;
244 Save_Scan_State (Scan_State); -- at dot
247 -- If we do not have another designator after the dot, then join
248 -- the normal circuit to handle a dot extension (may be .all or
249 -- character literal case). Otherwise loop back to scan the next
252 if Token not in Token_Class_Desig then
253 goto Scan_Name_Extension_Dot;
255 Prefix_Node := Name_Node;
256 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
257 Set_Prefix (Name_Node, Prefix_Node);
258 Set_Selector_Name (Name_Node, Token_Node);
262 -- We have now scanned out a qualified designator. If the last token is
263 -- an operator symbol, then we certainly do not have the Snam case, so
264 -- we can just use the normal name extension check circuit
266 if Prev_Token = Tok_Operator_Symbol then
267 goto Scan_Name_Extension;
270 -- We have scanned out a qualified simple name, check for name extension
271 -- Note that we know there is no dot here at this stage, so the only
272 -- possible cases of name extension are apostrophe and left paren.
274 if Token = Tok_Apostrophe then
275 Save_Scan_State (Scan_State); -- at apostrophe
276 Scan; -- past apostrophe
278 -- If left paren, then this might be a qualified expression, but we
279 -- are only in the business of scanning out names, so return with
280 -- Token backed up to point to the apostrophe. The treatment for
281 -- the range attribute is similar (we do not consider x'range to
282 -- be a name in this grammar).
284 if Token = Tok_Left_Paren or else Token = Tok_Range then
285 Restore_Scan_State (Scan_State); -- to apostrophe
286 Expr_Form := EF_Simple_Name;
289 -- Otherwise we have the case of a name extended by an attribute
292 goto Scan_Name_Extension_Apostrophe;
295 -- Check case of qualified simple name extended by a left parenthesis
297 elsif Token = Tok_Left_Paren then
298 Scan; -- past left paren
299 goto Scan_Name_Extension_Left_Paren;
301 -- Otherwise the qualified simple name is not extended, so return
304 Expr_Form := EF_Simple_Name;
308 -- Loop scanning past name extensions. A label is used for control
309 -- transfer for this loop for ease of interfacing with the finite state
310 -- machine in the parenthesis scanning circuit, and also to allow for
311 -- passing in control to the appropriate point from the above code.
313 <<Scan_Name_Extension>>
315 -- Character literal used as name cannot be extended. Also this
316 -- cannot be a call, since the name for a call must be a designator.
317 -- Return in these cases, or if there is no name extension
319 if Token not in Token_Class_Namext
320 or else Prev_Token = Tok_Char_Literal
322 Expr_Form := EF_Name;
326 -- Merge here when we know there is a name extension
328 <<Scan_Name_Extension_OK>>
330 if Token = Tok_Left_Paren then
331 Scan; -- past left paren
332 goto Scan_Name_Extension_Left_Paren;
334 elsif Token = Tok_Apostrophe then
335 Save_Scan_State (Scan_State); -- at apostrophe
336 Scan; -- past apostrophe
337 goto Scan_Name_Extension_Apostrophe;
339 else -- Token = Tok_Dot
340 Save_Scan_State (Scan_State); -- at dot
342 goto Scan_Name_Extension_Dot;
345 -- Case of name extended by dot (selection), dot is already skipped
346 -- and the scan state at the point of the dot is saved in Scan_State.
348 <<Scan_Name_Extension_Dot>>
350 -- Explicit dereference case
352 if Token = Tok_All then
353 Prefix_Node := Name_Node;
354 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
355 Set_Prefix (Name_Node, Prefix_Node);
357 goto Scan_Name_Extension;
359 -- Selected component case
361 elsif Token in Token_Class_Name then
362 Prefix_Node := Name_Node;
363 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
364 Set_Prefix (Name_Node, Prefix_Node);
365 Set_Selector_Name (Name_Node, Token_Node);
366 Scan; -- past selector
367 goto Scan_Name_Extension;
369 -- Reserved identifier as selector
371 elsif Is_Reserved_Identifier then
372 Scan_Reserved_Identifier (Force_Msg => False);
373 Prefix_Node := Name_Node;
374 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
375 Set_Prefix (Name_Node, Prefix_Node);
376 Set_Selector_Name (Name_Node, Token_Node);
377 Scan; -- past identifier used as selector
378 goto Scan_Name_Extension;
380 -- If dot is at end of line and followed by nothing legal,
381 -- then assume end of name and quit (dot will be taken as
382 -- an erroneous form of some other punctuation by our caller).
384 elsif Token_Is_At_Start_Of_Line then
385 Restore_Scan_State (Scan_State);
388 -- Here if nothing legal after the dot
391 Error_Msg_AP ("selector expected");
395 -- Here for an apostrophe as name extension. The scan position at the
396 -- apostrophe has already been saved, and the apostrophe scanned out.
398 <<Scan_Name_Extension_Apostrophe>>
400 Scan_Apostrophe : declare
401 function Apostrophe_Should_Be_Semicolon return Boolean;
402 -- Checks for case where apostrophe should probably be
403 -- a semicolon, and if so, gives appropriate message,
404 -- resets the scan pointer to the apostrophe, changes
405 -- the current token to Tok_Semicolon, and returns True.
406 -- Otherwise returns False.
408 function Apostrophe_Should_Be_Semicolon return Boolean is
410 if Token_Is_At_Start_Of_Line then
411 Restore_Scan_State (Scan_State); -- to apostrophe
412 Error_Msg_SC ("|""''"" should be "";""");
413 Token := Tok_Semicolon;
418 end Apostrophe_Should_Be_Semicolon;
420 -- Start of processing for Scan_Apostrophe
423 -- If range attribute after apostrophe, then return with Token
424 -- pointing to the apostrophe. Note that in this case the prefix
425 -- need not be a simple name (cases like A.all'range). Similarly
426 -- if there is a left paren after the apostrophe, then we also
427 -- return with Token pointing to the apostrophe (this is the
428 -- qualified expression case).
430 if Token = Tok_Range or else Token = Tok_Left_Paren then
431 Restore_Scan_State (Scan_State); -- to apostrophe
432 Expr_Form := EF_Name;
435 -- Here for cases where attribute designator is an identifier
437 elsif Token = Tok_Identifier then
438 Attr_Name := Token_Name;
440 if not Is_Attribute_Name (Attr_Name) then
441 if Apostrophe_Should_Be_Semicolon then
442 Expr_Form := EF_Name;
445 -- Here for a bad attribute name
448 Signal_Bad_Attribute;
449 Scan; -- past bad identifier
451 if Token = Tok_Left_Paren then
452 Scan; -- past left paren
455 Discard_Junk_Node (P_Expression_If_OK);
456 exit when not Comma_Present;
467 Style.Check_Attribute_Name (False);
470 -- Here for case of attribute designator is not an identifier
473 if Token = Tok_Delta then
474 Attr_Name := Name_Delta;
476 elsif Token = Tok_Digits then
477 Attr_Name := Name_Digits;
479 elsif Token = Tok_Access then
480 Attr_Name := Name_Access;
482 elsif Token = Tok_Mod and then Ada_Version = Ada_05 then
483 Attr_Name := Name_Mod;
485 elsif Apostrophe_Should_Be_Semicolon then
486 Expr_Form := EF_Name;
490 Error_Msg_AP ("attribute designator expected");
495 Style.Check_Attribute_Name (True);
499 -- We come here with an OK attribute scanned, and the
500 -- corresponding Attribute identifier node stored in Ident_Node.
502 Prefix_Node := Name_Node;
503 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
504 Scan; -- past attribute designator
505 Set_Prefix (Name_Node, Prefix_Node);
506 Set_Attribute_Name (Name_Node, Attr_Name);
508 -- Scan attribute arguments/designator. We skip this if we know
509 -- that the attribute cannot have an argument.
511 if Token = Tok_Left_Paren
513 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
515 Set_Expressions (Name_Node, New_List);
516 Scan; -- past left paren
520 Expr : constant Node_Id := P_Expression_If_OK;
523 if Token = Tok_Arrow then
525 ("named parameters not permitted for attributes");
526 Scan; -- past junk arrow
529 Append (Expr, Expressions (Name_Node));
530 exit when not Comma_Present;
538 goto Scan_Name_Extension;
541 -- Here for left parenthesis extending name (left paren skipped)
543 <<Scan_Name_Extension_Left_Paren>>
545 -- We now have to scan through a list of items, terminated by a
546 -- right parenthesis. The scan is handled by a finite state
547 -- machine. The possibilities are:
551 -- This is a slice. This case is handled in LP_State_Init
553 -- (expression, expression, ..)
555 -- This is interpreted as an indexed component, i.e. as a
556 -- case of a name which can be extended in the normal manner.
557 -- This case is handled by LP_State_Name or LP_State_Expr.
559 -- Note: conditional expressions (without an extra level of
560 -- parentheses) are permitted in this context).
562 -- (..., identifier => expression , ...)
564 -- If there is at least one occurrence of identifier => (but
565 -- none of the other cases apply), then we have a call.
567 -- Test for Id => case
569 if Token = Tok_Identifier then
570 Save_Scan_State (Scan_State); -- at Id
573 -- Test for => (allow := as an error substitute)
575 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
576 Restore_Scan_State (Scan_State); -- to Id
577 Arg_List := New_List;
581 Restore_Scan_State (Scan_State); -- to Id
585 -- Here we have an expression after all
587 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
589 -- Check cases of discrete range for a slice
591 -- First possibility: Range_Attribute_Reference
593 if Expr_Form = EF_Range_Attr then
594 Range_Node := Expr_Node;
596 -- Second possibility: Simple_expression .. Simple_expression
598 elsif Token = Tok_Dot_Dot then
599 Check_Simple_Expression (Expr_Node);
600 Range_Node := New_Node (N_Range, Token_Ptr);
601 Set_Low_Bound (Range_Node, Expr_Node);
603 Expr_Node := P_Expression;
604 Check_Simple_Expression (Expr_Node);
605 Set_High_Bound (Range_Node, Expr_Node);
607 -- Third possibility: Type_name range Range
609 elsif Token = Tok_Range then
610 if Expr_Form /= EF_Simple_Name then
611 Error_Msg_SC -- CODEFIX???
612 ("subtype mark must precede RANGE");
616 Range_Node := P_Subtype_Indication (Expr_Node);
618 -- Otherwise we just have an expression. It is true that we might
619 -- have a subtype mark without a range constraint but this case
620 -- is syntactically indistinguishable from the expression case.
623 Arg_List := New_List;
627 -- Fall through here with unmistakable Discrete range scanned,
628 -- which means that we definitely have the case of a slice. The
629 -- Discrete range is in Range_Node.
631 if Token = Tok_Comma then
632 Error_Msg_SC ("slice cannot have more than one dimension");
635 elsif Token /= Tok_Right_Paren then
640 Scan; -- past right paren
641 Prefix_Node := Name_Node;
642 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
643 Set_Prefix (Name_Node, Prefix_Node);
644 Set_Discrete_Range (Name_Node, Range_Node);
646 -- An operator node is legal as a prefix to other names,
647 -- but not for a slice.
649 if Nkind (Prefix_Node) = N_Operator_Symbol then
650 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
653 -- If we have a name extension, go scan it
655 if Token in Token_Class_Namext then
656 goto Scan_Name_Extension_OK;
658 -- Otherwise return (a slice is a name, but is not a call)
661 Expr_Form := EF_Name;
666 -- In LP_State_Expr, we have scanned one or more expressions, and
667 -- so we have a call or an indexed component which is a name. On
668 -- entry we have the expression just scanned in Expr_Node and
669 -- Arg_List contains the list of expressions encountered so far
672 Append (Expr_Node, Arg_List);
674 if Token = Tok_Arrow then
676 ("expect identifier in parameter association",
680 elsif not Comma_Present then
682 Prefix_Node := Name_Node;
683 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
684 Set_Prefix (Name_Node, Prefix_Node);
685 Set_Expressions (Name_Node, Arg_List);
686 goto Scan_Name_Extension;
689 -- Comma present (and scanned out), test for identifier => case
690 -- Test for identifier => case
692 if Token = Tok_Identifier then
693 Save_Scan_State (Scan_State); -- at Id
696 -- Test for => (allow := as error substitute)
698 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
699 Restore_Scan_State (Scan_State); -- to Id
702 -- Otherwise it's just an expression after all, so backup
705 Restore_Scan_State (Scan_State); -- to Id
709 -- Here we have an expression after all, so stay in this state
711 Expr_Node := P_Expression_If_OK;
714 -- LP_State_Call corresponds to the situation in which at least
715 -- one instance of Id => Expression has been encountered, so we
716 -- know that we do not have a name, but rather a call. We enter
717 -- it with the scan pointer pointing to the next argument to scan,
718 -- and Arg_List containing the list of arguments scanned so far.
722 -- Test for case of Id => Expression (named parameter)
724 if Token = Tok_Identifier then
725 Save_Scan_State (Scan_State); -- at Id
726 Ident_Node := Token_Node;
729 -- Deal with => (allow := as erroneous substitute)
731 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
732 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
733 Set_Selector_Name (Arg_Node, Ident_Node);
735 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
736 Append (Arg_Node, Arg_List);
738 -- If a comma follows, go back and scan next entry
740 if Comma_Present then
743 -- Otherwise we have the end of a call
746 Prefix_Node := Name_Node;
747 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
748 Set_Name (Name_Node, Prefix_Node);
749 Set_Parameter_Associations (Name_Node, Arg_List);
752 if Token in Token_Class_Namext then
753 goto Scan_Name_Extension_OK;
755 -- This is a case of a call which cannot be a name
758 Expr_Form := EF_Name;
763 -- Not named parameter: Id started an expression after all
766 Restore_Scan_State (Scan_State); -- to Id
770 -- Here if entry did not start with Id => which means that it
771 -- is a positional parameter, which is not allowed, since we
772 -- have seen at least one named parameter already.
775 ("positional parameter association " &
776 "not allowed after named one");
778 Expr_Node := P_Expression_If_OK;
780 -- Leaving the '>' in an association is not unusual, so suggest
783 if Nkind (Expr_Node) = N_Op_Eq then
784 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
787 -- We go back to scanning out expressions, so that we do not get
788 -- multiple error messages when several positional parameters
789 -- follow a named parameter.
793 -- End of treatment for name extensions starting with left paren
795 -- End of loop through name extensions
799 -- This function parses a restricted form of Names which are either
800 -- designators, or designators preceded by a sequence of prefixes
801 -- that are direct names.
803 -- Error recovery: cannot raise Error_Resync
805 function P_Function_Name return Node_Id is
806 Designator_Node : Node_Id;
807 Prefix_Node : Node_Id;
808 Selector_Node : Node_Id;
809 Dot_Sloc : Source_Ptr := No_Location;
812 -- Prefix_Node is set to the gathered prefix so far, Empty means that
813 -- no prefix has been scanned. This allows us to build up the result
814 -- in the required right recursive manner.
816 Prefix_Node := Empty;
818 -- Loop through prefixes
821 Designator_Node := Token_Node;
823 if Token not in Token_Class_Desig then
824 return P_Identifier; -- let P_Identifier issue the error message
826 else -- Token in Token_Class_Desig
827 Scan; -- past designator
828 exit when Token /= Tok_Dot;
831 -- Here at a dot, with token just before it in Designator_Node
833 if No (Prefix_Node) then
834 Prefix_Node := Designator_Node;
836 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
837 Set_Prefix (Selector_Node, Prefix_Node);
838 Set_Selector_Name (Selector_Node, Designator_Node);
839 Prefix_Node := Selector_Node;
842 Dot_Sloc := Token_Ptr;
846 -- Fall out of the loop having just scanned a designator
848 if No (Prefix_Node) then
849 return Designator_Node;
851 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
852 Set_Prefix (Selector_Node, Prefix_Node);
853 Set_Selector_Name (Selector_Node, Designator_Node);
854 return Selector_Node;
862 -- This function parses a restricted form of Names which are either
863 -- identifiers, or identifiers preceded by a sequence of prefixes
864 -- that are direct names.
866 -- Error recovery: cannot raise Error_Resync
868 function P_Qualified_Simple_Name return Node_Id is
869 Designator_Node : Node_Id;
870 Prefix_Node : Node_Id;
871 Selector_Node : Node_Id;
872 Dot_Sloc : Source_Ptr := No_Location;
875 -- Prefix node is set to the gathered prefix so far, Empty means that
876 -- no prefix has been scanned. This allows us to build up the result
877 -- in the required right recursive manner.
879 Prefix_Node := Empty;
881 -- Loop through prefixes
884 Designator_Node := Token_Node;
886 if Token = Tok_Identifier then
887 Scan; -- past identifier
888 exit when Token /= Tok_Dot;
890 elsif Token not in Token_Class_Desig then
891 return P_Identifier; -- let P_Identifier issue the error message
894 Scan; -- past designator
896 if Token /= Tok_Dot then
897 Error_Msg_SP ("identifier expected");
902 -- Here at a dot, with token just before it in Designator_Node
904 if No (Prefix_Node) then
905 Prefix_Node := Designator_Node;
907 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
908 Set_Prefix (Selector_Node, Prefix_Node);
909 Set_Selector_Name (Selector_Node, Designator_Node);
910 Prefix_Node := Selector_Node;
913 Dot_Sloc := Token_Ptr;
917 -- Fall out of the loop having just scanned an identifier
919 if No (Prefix_Node) then
920 return Designator_Node;
922 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
923 Set_Prefix (Selector_Node, Prefix_Node);
924 Set_Selector_Name (Selector_Node, Designator_Node);
925 return Selector_Node;
931 end P_Qualified_Simple_Name;
933 -- This procedure differs from P_Qualified_Simple_Name only in that it
934 -- raises Error_Resync if any error is encountered. It only returns after
935 -- scanning a valid qualified simple name.
937 -- Error recovery: can raise Error_Resync
939 function P_Qualified_Simple_Name_Resync return Node_Id is
940 Designator_Node : Node_Id;
941 Prefix_Node : Node_Id;
942 Selector_Node : Node_Id;
943 Dot_Sloc : Source_Ptr := No_Location;
946 Prefix_Node := Empty;
948 -- Loop through prefixes
951 Designator_Node := Token_Node;
953 if Token = Tok_Identifier then
954 Scan; -- past identifier
955 exit when Token /= Tok_Dot;
957 elsif Token not in Token_Class_Desig then
958 Discard_Junk_Node (P_Identifier); -- to issue the error message
962 Scan; -- past designator
964 if Token /= Tok_Dot then
965 Error_Msg_SP ("identifier expected");
970 -- Here at a dot, with token just before it in Designator_Node
972 if No (Prefix_Node) then
973 Prefix_Node := Designator_Node;
975 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
976 Set_Prefix (Selector_Node, Prefix_Node);
977 Set_Selector_Name (Selector_Node, Designator_Node);
978 Prefix_Node := Selector_Node;
981 Dot_Sloc := Token_Ptr;
985 -- Fall out of the loop having just scanned an identifier
987 if No (Prefix_Node) then
988 return Designator_Node;
990 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
991 Set_Prefix (Selector_Node, Prefix_Node);
992 Set_Selector_Name (Selector_Node, Designator_Node);
993 return Selector_Node;
995 end P_Qualified_Simple_Name_Resync;
997 ----------------------
998 -- 4.1 Direct_Name --
999 ----------------------
1001 -- Parsed by P_Name and other functions in section 4.1
1007 -- Parsed by P_Name (4.1)
1009 -------------------------------
1010 -- 4.1 Explicit Dereference --
1011 -------------------------------
1013 -- Parsed by P_Name (4.1)
1015 -------------------------------
1016 -- 4.1 Implicit_Dereference --
1017 -------------------------------
1019 -- Parsed by P_Name (4.1)
1021 ----------------------------
1022 -- 4.1 Indexed Component --
1023 ----------------------------
1025 -- Parsed by P_Name (4.1)
1031 -- Parsed by P_Name (4.1)
1033 -----------------------------
1034 -- 4.1 Selected_Component --
1035 -----------------------------
1037 -- Parsed by P_Name (4.1)
1039 ------------------------
1040 -- 4.1 Selector Name --
1041 ------------------------
1043 -- Parsed by P_Name (4.1)
1045 ------------------------------
1046 -- 4.1 Attribute Reference --
1047 ------------------------------
1049 -- Parsed by P_Name (4.1)
1051 -------------------------------
1052 -- 4.1 Attribute Designator --
1053 -------------------------------
1055 -- Parsed by P_Name (4.1)
1057 --------------------------------------
1058 -- 4.1.4 Range Attribute Reference --
1059 --------------------------------------
1061 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1063 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1065 -- In the grammar, a RANGE attribute is simply a name, but its use is
1066 -- highly restricted, so in the parser, we do not regard it as a name.
1067 -- Instead, P_Name returns without scanning the 'RANGE part of the
1068 -- attribute, and the caller uses the following function to construct
1069 -- a range attribute in places where it is appropriate.
1071 -- Note that RANGE here is treated essentially as an identifier,
1072 -- rather than a reserved word.
1074 -- The caller has parsed the prefix, i.e. a name, and Token points to
1075 -- the apostrophe. The token after the apostrophe is known to be RANGE
1076 -- at this point. The prefix node becomes the prefix of the attribute.
1078 -- Error_Recovery: Cannot raise Error_Resync
1080 function P_Range_Attribute_Reference
1081 (Prefix_Node : Node_Id)
1084 Attr_Node : Node_Id;
1087 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1088 Set_Prefix (Attr_Node, Prefix_Node);
1089 Scan; -- past apostrophe
1092 Style.Check_Attribute_Name (True);
1095 Set_Attribute_Name (Attr_Node, Name_Range);
1098 if Token = Tok_Left_Paren then
1099 Scan; -- past left paren
1100 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1105 end P_Range_Attribute_Reference;
1107 ---------------------------------------
1108 -- 4.1.4 Range Attribute Designator --
1109 ---------------------------------------
1111 -- Parsed by P_Range_Attribute_Reference (4.4)
1113 --------------------
1115 --------------------
1117 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1119 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1120 -- an aggregate is known to be required (code statement, extension
1121 -- aggregate), in which cases this routine performs the necessary check
1122 -- that we have an aggregate rather than a parenthesized expression
1124 -- Error recovery: can raise Error_Resync
1126 function P_Aggregate return Node_Id is
1127 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1128 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1131 if Nkind (Aggr_Node) /= N_Aggregate
1133 Nkind (Aggr_Node) /= N_Extension_Aggregate
1136 ("aggregate may not have single positional component", Aggr_Sloc);
1143 ------------------------------------------------
1144 -- 4.3 Aggregate or Parenthesized Expression --
1145 ------------------------------------------------
1147 -- This procedure parses out either an aggregate or a parenthesized
1148 -- expression (these two constructs are closely related, since a
1149 -- parenthesized expression looks like an aggregate with a single
1150 -- positional component).
1153 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1155 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1157 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1158 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1161 -- RECORD_COMPONENT_ASSOCIATION ::=
1162 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1164 -- COMPONENT_CHOICE_LIST ::=
1165 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1168 -- EXTENSION_AGGREGATE ::=
1169 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1171 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1173 -- ARRAY_AGGREGATE ::=
1174 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1176 -- POSITIONAL_ARRAY_AGGREGATE ::=
1177 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1178 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1179 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1181 -- NAMED_ARRAY_AGGREGATE ::=
1182 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1184 -- PRIMARY ::= (EXPRESSION);
1186 -- Error recovery: can raise Error_Resync
1188 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1189 -- to Ada 2005 limited aggregates (AI-287)
1191 function P_Aggregate_Or_Paren_Expr return Node_Id is
1192 Aggregate_Node : Node_Id;
1193 Expr_List : List_Id;
1194 Assoc_List : List_Id;
1195 Expr_Node : Node_Id;
1196 Lparen_Sloc : Source_Ptr;
1197 Scan_State : Saved_Scan_State;
1200 Lparen_Sloc := Token_Ptr;
1203 -- Conditional expression case
1205 if Token = Tok_If then
1206 Expr_Node := P_Conditional_Expression;
1210 -- Note: the mechanism used here of rescanning the initial expression
1211 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1212 -- out the discrete choice list.
1214 -- Deal with expression and extension aggregate cases first
1216 elsif Token /= Tok_Others then
1217 Save_Scan_State (Scan_State); -- at start of expression
1219 -- Deal with (NULL RECORD) case
1221 if Token = Tok_Null then
1224 if Token = Tok_Record then
1225 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1226 Set_Null_Record_Present (Aggregate_Node, True);
1227 Scan; -- past RECORD
1229 return Aggregate_Node;
1231 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1235 -- Ada 2005 (AI-287): The box notation is allowed only with named
1236 -- notation because positional notation might be error prone. For
1237 -- example, in "(X, <>, Y, <>)", there is no type associated with
1238 -- the boxes, so you might not be leaving out the components you
1239 -- thought you were leaving out.
1241 if Ada_Version >= Ada_05 and then Token = Tok_Box then
1242 Error_Msg_SC ("(Ada 2005) box notation only allowed with "
1243 & "named notation");
1245 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1246 return Aggregate_Node;
1249 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1251 -- Extension aggregate case
1253 if Token = Tok_With then
1255 if Nkind (Expr_Node) = N_Attribute_Reference
1256 and then Attribute_Name (Expr_Node) = Name_Range
1258 Bad_Range_Attribute (Sloc (Expr_Node));
1262 if Ada_Version = Ada_83 then
1263 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1266 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1267 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1270 -- Deal with WITH NULL RECORD case
1272 if Token = Tok_Null then
1273 Save_Scan_State (Scan_State); -- at NULL
1276 if Token = Tok_Record then
1277 Scan; -- past RECORD
1278 Set_Null_Record_Present (Aggregate_Node, True);
1280 return Aggregate_Node;
1283 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1287 if Token /= Tok_Others then
1288 Save_Scan_State (Scan_State);
1289 Expr_Node := P_Expression;
1296 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1297 if Nkind (Expr_Node) = N_Attribute_Reference
1298 and then Attribute_Name (Expr_Node) = Name_Range
1301 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1302 Scan; -- past right paren
1306 -- Bump paren count of expression
1308 if Expr_Node /= Error then
1309 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1312 T_Right_Paren; -- past right paren (error message if none)
1315 -- Normal aggregate case
1318 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1324 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1328 -- Prepare to scan list of component associations
1330 Expr_List := No_List; -- don't set yet, maybe all named entries
1331 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1333 -- This loop scans through component associations. On entry to the
1334 -- loop, an expression has been scanned at the start of the current
1335 -- association unless initial token was OTHERS, in which case
1336 -- Expr_Node is set to Empty.
1339 -- Deal with others association first. This is a named association
1341 if No (Expr_Node) then
1342 if No (Assoc_List) then
1343 Assoc_List := New_List;
1346 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1348 -- Improper use of WITH
1350 elsif Token = Tok_With then
1351 Error_Msg_SC ("WITH must be preceded by single expression in " &
1352 "extension aggregate");
1355 -- A range attribute can only appear as part of a discrete choice
1358 elsif Nkind (Expr_Node) = N_Attribute_Reference
1359 and then Attribute_Name (Expr_Node) = Name_Range
1360 and then Token /= Tok_Arrow
1361 and then Token /= Tok_Vertical_Bar
1363 Bad_Range_Attribute (Sloc (Expr_Node));
1366 -- Assume positional case if comma, right paren, or literal or
1367 -- identifier or OTHERS follows (the latter cases are missing
1368 -- comma cases). Also assume positional if a semicolon follows,
1369 -- which can happen if there are missing parens
1371 elsif Token = Tok_Comma
1372 or else Token = Tok_Right_Paren
1373 or else Token = Tok_Others
1374 or else Token in Token_Class_Lit_Or_Name
1375 or else Token = Tok_Semicolon
1377 if Present (Assoc_List) then
1379 ("""='>"" expected (positional association cannot follow " &
1380 "named association)");
1383 if No (Expr_List) then
1384 Expr_List := New_List;
1387 Append (Expr_Node, Expr_List);
1389 -- Anything else is assumed to be a named association
1392 Restore_Scan_State (Scan_State); -- to start of expression
1394 if No (Assoc_List) then
1395 Assoc_List := New_List;
1398 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1401 exit when not Comma_Present;
1403 -- If we are at an expression terminator, something is seriously
1404 -- wrong, so let's get out now, before we start eating up stuff
1405 -- that doesn't belong to us!
1407 if Token in Token_Class_Eterm then
1408 Error_Msg_AP ("expecting expression or component association");
1412 -- Otherwise initiate for reentry to top of loop by scanning an
1413 -- initial expression, unless the first token is OTHERS.
1415 if Token = Tok_Others then
1418 Save_Scan_State (Scan_State); -- at start of expression
1419 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1424 -- All component associations (positional and named) have been scanned
1427 Set_Expressions (Aggregate_Node, Expr_List);
1428 Set_Component_Associations (Aggregate_Node, Assoc_List);
1429 return Aggregate_Node;
1430 end P_Aggregate_Or_Paren_Expr;
1432 ------------------------------------------------
1433 -- 4.3 Record or Array Component Association --
1434 ------------------------------------------------
1436 -- RECORD_COMPONENT_ASSOCIATION ::=
1437 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1438 -- | COMPONENT_CHOICE_LIST => <>
1440 -- COMPONENT_CHOICE_LIST =>
1441 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1444 -- ARRAY_COMPONENT_ASSOCIATION ::=
1445 -- DISCRETE_CHOICE_LIST => EXPRESSION
1446 -- | DISCRETE_CHOICE_LIST => <>
1448 -- Note: this routine only handles the named cases, including others.
1449 -- Cases where the component choice list is not present have already
1450 -- been handled directly.
1452 -- Error recovery: can raise Error_Resync
1454 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1455 -- rules have been extended to give support to Ada 2005 limited
1456 -- aggregates (AI-287)
1458 function P_Record_Or_Array_Component_Association return Node_Id is
1459 Assoc_Node : Node_Id;
1462 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1463 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1464 Set_Sloc (Assoc_Node, Token_Ptr);
1467 if Token = Tok_Box then
1469 -- Ada 2005(AI-287): The box notation is used to indicate the
1470 -- default initialization of aggregate components
1472 if Ada_Version < Ada_05 then
1474 ("component association with '<'> is an Ada 2005 extension");
1475 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1478 Set_Box_Present (Assoc_Node);
1481 Set_Expression (Assoc_Node, P_Expression);
1485 end P_Record_Or_Array_Component_Association;
1487 -----------------------------
1488 -- 4.3.1 Record Aggregate --
1489 -----------------------------
1491 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1492 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1494 ----------------------------------------------
1495 -- 4.3.1 Record Component Association List --
1496 ----------------------------------------------
1498 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1500 ----------------------------------
1501 -- 4.3.1 Component Choice List --
1502 ----------------------------------
1504 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1506 --------------------------------
1507 -- 4.3.1 Extension Aggregate --
1508 --------------------------------
1510 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1512 --------------------------
1513 -- 4.3.1 Ancestor Part --
1514 --------------------------
1516 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1518 ----------------------------
1519 -- 4.3.1 Array Aggregate --
1520 ----------------------------
1522 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1524 ---------------------------------------
1525 -- 4.3.1 Positional Array Aggregate --
1526 ---------------------------------------
1528 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1530 ----------------------------------
1531 -- 4.3.1 Named Array Aggregate --
1532 ----------------------------------
1534 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1536 ----------------------------------------
1537 -- 4.3.1 Array Component Association --
1538 ----------------------------------------
1540 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1542 ---------------------
1543 -- 4.4 Expression --
1544 ---------------------
1547 -- RELATION {and RELATION} | RELATION {and then RELATION}
1548 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1549 -- | RELATION {xor RELATION}
1551 -- On return, Expr_Form indicates the categorization of the expression
1552 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1553 -- an error message is given, and Error is returned).
1555 -- Error recovery: cannot raise Error_Resync
1557 function P_Expression return Node_Id is
1558 Logical_Op : Node_Kind;
1559 Prev_Logical_Op : Node_Kind;
1560 Op_Location : Source_Ptr;
1565 Node1 := P_Relation;
1567 if Token in Token_Class_Logop then
1568 Prev_Logical_Op := N_Empty;
1571 Op_Location := Token_Ptr;
1572 Logical_Op := P_Logical_Operator;
1574 if Prev_Logical_Op /= N_Empty and then
1575 Logical_Op /= Prev_Logical_Op
1578 ("mixed logical operators in expression", Op_Location);
1579 Prev_Logical_Op := N_Empty;
1581 Prev_Logical_Op := Logical_Op;
1585 Node1 := New_Node (Logical_Op, Op_Location);
1586 Set_Left_Opnd (Node1, Node2);
1587 Set_Right_Opnd (Node1, P_Relation);
1588 Set_Op_Name (Node1);
1589 exit when Token not in Token_Class_Logop;
1592 Expr_Form := EF_Non_Simple;
1595 if Token = Tok_Apostrophe then
1596 Bad_Range_Attribute (Token_Ptr);
1603 -- This function is identical to the normal P_Expression, except that it
1604 -- also permits the appearence of a conditional expression without the
1605 -- usual surrounding parentheses.
1607 function P_Expression_If_OK return Node_Id is
1609 if Token = Tok_If then
1610 return P_Conditional_Expression;
1612 return P_Expression;
1614 end P_Expression_If_OK;
1616 -- This function is identical to the normal P_Expression, except that it
1617 -- checks that the expression scan did not stop on a right paren. It is
1618 -- called in all contexts where a right parenthesis cannot legitimately
1619 -- follow an expression.
1621 -- Error recovery: can not raise Error_Resync
1623 function P_Expression_No_Right_Paren return Node_Id is
1624 Expr : constant Node_Id := P_Expression;
1626 Ignore (Tok_Right_Paren);
1628 end P_Expression_No_Right_Paren;
1630 ----------------------------------------
1631 -- 4.4 Expression_Or_Range_Attribute --
1632 ----------------------------------------
1635 -- RELATION {and RELATION} | RELATION {and then RELATION}
1636 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1637 -- | RELATION {xor RELATION}
1639 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1641 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1643 -- On return, Expr_Form indicates the categorization of the expression
1644 -- and EF_Range_Attr is one of the possibilities.
1646 -- Error recovery: cannot raise Error_Resync
1648 -- In the grammar, a RANGE attribute is simply a name, but its use is
1649 -- highly restricted, so in the parser, we do not regard it as a name.
1650 -- Instead, P_Name returns without scanning the 'RANGE part of the
1651 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1652 -- attribute reference. In the normal case where a range attribute is
1653 -- not allowed, an error message is issued by P_Expression.
1655 function P_Expression_Or_Range_Attribute return Node_Id is
1656 Logical_Op : Node_Kind;
1657 Prev_Logical_Op : Node_Kind;
1658 Op_Location : Source_Ptr;
1661 Attr_Node : Node_Id;
1664 Node1 := P_Relation;
1666 if Token = Tok_Apostrophe then
1667 Attr_Node := P_Range_Attribute_Reference (Node1);
1668 Expr_Form := EF_Range_Attr;
1671 elsif Token in Token_Class_Logop then
1672 Prev_Logical_Op := N_Empty;
1675 Op_Location := Token_Ptr;
1676 Logical_Op := P_Logical_Operator;
1678 if Prev_Logical_Op /= N_Empty and then
1679 Logical_Op /= Prev_Logical_Op
1682 ("mixed logical operators in expression", Op_Location);
1683 Prev_Logical_Op := N_Empty;
1685 Prev_Logical_Op := Logical_Op;
1689 Node1 := New_Node (Logical_Op, Op_Location);
1690 Set_Left_Opnd (Node1, Node2);
1691 Set_Right_Opnd (Node1, P_Relation);
1692 Set_Op_Name (Node1);
1693 exit when Token not in Token_Class_Logop;
1696 Expr_Form := EF_Non_Simple;
1699 if Token = Tok_Apostrophe then
1700 Bad_Range_Attribute (Token_Ptr);
1705 end P_Expression_Or_Range_Attribute;
1707 -- Version that allows a non-parenthesized conditional expression
1709 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1711 if Token = Tok_If then
1712 return P_Conditional_Expression;
1714 return P_Expression_Or_Range_Attribute;
1716 end P_Expression_Or_Range_Attribute_If_OK;
1723 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1724 -- | SIMPLE_EXPRESSION [not] in RANGE
1725 -- | SIMPLE_EXPRESSION [not] in SUBTYPE_MARK
1727 -- On return, Expr_Form indicates the categorization of the expression
1729 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1730 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1732 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1733 -- expression, then tokens are scanned until either a non-expression token,
1734 -- a right paren (not matched by a left paren) or a comma, is encountered.
1736 function P_Relation return Node_Id is
1737 Node1, Node2 : Node_Id;
1741 Node1 := P_Simple_Expression;
1743 if Token not in Token_Class_Relop then
1747 -- Here we have a relational operator following. If so then scan it
1748 -- out. Note that the assignment symbol := is treated as a relational
1749 -- operator to improve the error recovery when it is misused for =.
1750 -- P_Relational_Operator also parses the IN and NOT IN operations.
1753 Node2 := New_Node (P_Relational_Operator, Optok);
1754 Set_Left_Opnd (Node2, Node1);
1755 Set_Op_Name (Node2);
1757 -- Case of IN or NOT IN
1759 if Prev_Token = Tok_In then
1760 Set_Right_Opnd (Node2, P_Range_Or_Subtype_Mark);
1762 -- Case of relational operator (= /= < <= > >=)
1765 Set_Right_Opnd (Node2, P_Simple_Expression);
1768 Expr_Form := EF_Non_Simple;
1770 if Token in Token_Class_Relop then
1771 Error_Msg_SC ("unexpected relational operator");
1778 -- If any error occurs, then scan to the next expression terminator symbol
1779 -- or comma or right paren at the outer (i.e. current) parentheses level.
1780 -- The flags are set to indicate a normal simple expression.
1783 when Error_Resync =>
1785 Expr_Form := EF_Simple;
1789 ----------------------------
1790 -- 4.4 Simple Expression --
1791 ----------------------------
1793 -- SIMPLE_EXPRESSION ::=
1794 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1796 -- On return, Expr_Form indicates the categorization of the expression
1798 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1799 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1801 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1802 -- expression, then tokens are scanned until either a non-expression token,
1803 -- a right paren (not matched by a left paren) or a comma, is encountered.
1805 -- Note: P_Simple_Expression is called only internally by higher level
1806 -- expression routines. In cases in the grammar where a simple expression
1807 -- is required, the approach is to scan an expression, and then post an
1808 -- appropriate error message if the expression obtained is not simple. This
1809 -- gives better error recovery and treatment.
1811 function P_Simple_Expression return Node_Id is
1812 Scan_State : Saved_Scan_State;
1815 Tokptr : Source_Ptr;
1818 -- Check for cases starting with a name. There are two reasons for
1819 -- special casing. First speed things up by catching a common case
1820 -- without going through several routine layers. Second the caller must
1821 -- be informed via Expr_Form when the simple expression is a name.
1823 if Token in Token_Class_Name then
1826 -- Deal with apostrophe cases
1828 if Token = Tok_Apostrophe then
1829 Save_Scan_State (Scan_State); -- at apostrophe
1830 Scan; -- past apostrophe
1832 -- If qualified expression, scan it out and fall through
1834 if Token = Tok_Left_Paren then
1835 Node1 := P_Qualified_Expression (Node1);
1836 Expr_Form := EF_Simple;
1838 -- If range attribute, then we return with Token pointing to the
1839 -- apostrophe. Note: avoid the normal error check on exit. We
1840 -- know that the expression really is complete in this case!
1842 else -- Token = Tok_Range then
1843 Restore_Scan_State (Scan_State); -- to apostrophe
1844 Expr_Form := EF_Simple_Name;
1849 -- If an expression terminator follows, the previous processing
1850 -- completely scanned out the expression (a common case), and
1851 -- left Expr_Form set appropriately for returning to our caller.
1853 if Token in Token_Class_Sterm then
1856 -- If we do not have an expression terminator, then complete the
1857 -- scan of a simple expression. This code duplicates the code
1858 -- found in P_Term and P_Factor.
1861 if Token = Tok_Double_Asterisk then
1863 Style.Check_Exponentiation_Operator;
1866 Node2 := New_Node (N_Op_Expon, Token_Ptr);
1868 Set_Left_Opnd (Node2, Node1);
1869 Set_Right_Opnd (Node2, P_Primary);
1870 Set_Op_Name (Node2);
1875 exit when Token not in Token_Class_Mulop;
1876 Tokptr := Token_Ptr;
1877 Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1880 Style.Check_Binary_Operator;
1883 Scan; -- past operator
1884 Set_Left_Opnd (Node2, Node1);
1885 Set_Right_Opnd (Node2, P_Factor);
1886 Set_Op_Name (Node2);
1891 exit when Token not in Token_Class_Binary_Addop;
1892 Tokptr := Token_Ptr;
1893 Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1896 Style.Check_Binary_Operator;
1899 Scan; -- past operator
1900 Set_Left_Opnd (Node2, Node1);
1901 Set_Right_Opnd (Node2, P_Term);
1902 Set_Op_Name (Node2);
1906 Expr_Form := EF_Simple;
1909 -- Cases where simple expression does not start with a name
1912 -- Scan initial sign and initial Term
1914 if Token in Token_Class_Unary_Addop then
1915 Tokptr := Token_Ptr;
1916 Node1 := New_Node (P_Unary_Adding_Operator, Tokptr);
1919 Style.Check_Unary_Plus_Or_Minus;
1922 Scan; -- past operator
1923 Set_Right_Opnd (Node1, P_Term);
1924 Set_Op_Name (Node1);
1929 -- In the following, we special-case a sequence of concatenations of
1930 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
1931 -- else mixed in. For such a sequence, we return a tree representing
1932 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
1933 -- the number of concatenations is large. If semantic analysis
1934 -- resolves the "&" to a predefined one, then this folding gives the
1935 -- right answer. Otherwise, semantic analysis will complain about a
1936 -- capacity-exceeded error. The purpose of this trick is to avoid
1937 -- creating a deeply nested tree, which would cause deep recursion
1938 -- during semantics, causing stack overflow. This way, we can handle
1939 -- enormous concatenations in the normal case of predefined "&". We
1940 -- first build up the normal tree, and then rewrite it if
1944 Num_Concats_Threshold : constant Positive := 1000;
1945 -- Arbitrary threshold value to enable optimization
1947 First_Node : constant Node_Id := Node1;
1948 Is_Strlit_Concat : Boolean;
1949 -- True iff we've parsed a sequence of concatenations of string
1950 -- literals, with nothing else mixed in.
1952 Num_Concats : Natural;
1953 -- Number of "&" operators if Is_Strlit_Concat is True
1957 Nkind (Node1) = N_String_Literal
1958 and then Token = Tok_Ampersand;
1961 -- Scan out sequence of terms separated by binary adding operators
1964 exit when Token not in Token_Class_Binary_Addop;
1965 Tokptr := Token_Ptr;
1966 Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1967 Scan; -- past operator
1968 Set_Left_Opnd (Node2, Node1);
1970 Set_Right_Opnd (Node2, Node1);
1971 Set_Op_Name (Node2);
1973 -- Check if we're still concatenating string literals
1977 and then Nkind (Node2) = N_Op_Concat
1978 and then Nkind (Node1) = N_String_Literal;
1980 if Is_Strlit_Concat then
1981 Num_Concats := Num_Concats + 1;
1987 -- If we have an enormous series of concatenations of string
1988 -- literals, rewrite as explained above. The Is_Folded_In_Parser
1989 -- flag tells semantic analysis that if the "&" is not predefined,
1990 -- the folded value is wrong.
1993 and then Num_Concats >= Num_Concats_Threshold
1996 Empty_String_Val : String_Id;
1999 Strlit_Concat_Val : String_Id;
2000 -- Contains the folded value (which will be correct if the
2001 -- "&" operators are the predefined ones).
2004 -- For walking up the tree
2007 -- Folded node to replace Node1
2009 Loc : constant Source_Ptr := Sloc (First_Node);
2012 -- Walk up the tree starting at the leftmost string literal
2013 -- (First_Node), building up the Strlit_Concat_Val as we
2014 -- go. Note that we do not use recursion here -- the whole
2015 -- point is to avoid recursively walking that enormous tree.
2018 Store_String_Chars (Strval (First_Node));
2020 Cur_Node := Parent (First_Node);
2021 while Present (Cur_Node) loop
2022 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2023 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2025 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2026 Cur_Node := Parent (Cur_Node);
2029 Strlit_Concat_Val := End_String;
2031 -- Create new folded node, and rewrite result with a concat-
2032 -- enation of an empty string literal and the folded node.
2035 Empty_String_Val := End_String;
2037 Make_Op_Concat (Loc,
2038 Make_String_Literal (Loc, Empty_String_Val),
2039 Make_String_Literal (Loc, Strlit_Concat_Val,
2040 Is_Folded_In_Parser => True));
2041 Rewrite (Node1, New_Node);
2046 -- All done, we clearly do not have name or numeric literal so this
2047 -- is a case of a simple expression which is some other possibility.
2049 Expr_Form := EF_Simple;
2052 -- Come here at end of simple expression, where we do a couple of
2053 -- special checks to improve error recovery.
2055 -- Special test to improve error recovery. If the current token
2056 -- is a period, then someone is trying to do selection on something
2057 -- that is not a name, e.g. a qualified expression.
2059 if Token = Tok_Dot then
2060 Error_Msg_SC ("prefix for selection is not a name");
2064 -- Special test to improve error recovery: If the current token is
2065 -- not the first token on a line (as determined by checking the
2066 -- previous token position with the start of the current line),
2067 -- then we insist that we have an appropriate terminating token.
2068 -- Consider the following two examples:
2070 -- 1) if A nad B then ...
2075 -- In the first example, we would like to issue a binary operator
2076 -- expected message and resynchronize to the then. In the second
2077 -- example, we do not want to issue a binary operator message, so
2078 -- that instead we will get the missing semicolon message. This
2079 -- distinction is of course a heuristic which does not always work,
2080 -- but in practice it is quite effective.
2082 -- Note: the one case in which we do not go through this circuit is
2083 -- when we have scanned a range attribute and want to return with
2084 -- Token pointing to the apostrophe. The apostrophe is not normally
2085 -- an expression terminator, and is not in Token_Class_Sterm, but
2086 -- in this special case we know that the expression is complete.
2088 if not Token_Is_At_Start_Of_Line
2089 and then Token not in Token_Class_Sterm
2091 Error_Msg_AP ("binary operator expected");
2097 -- If any error occurs, then scan to next expression terminator symbol
2098 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2099 -- level. Expr_Form is set to indicate a normal simple expression.
2102 when Error_Resync =>
2104 Expr_Form := EF_Simple;
2106 end P_Simple_Expression;
2108 -----------------------------------------------
2109 -- 4.4 Simple Expression or Range Attribute --
2110 -----------------------------------------------
2112 -- SIMPLE_EXPRESSION ::=
2113 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2115 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2117 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2119 -- Error recovery: cannot raise Error_Resync
2121 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2123 Attr_Node : Node_Id;
2126 -- We don't just want to roar ahead and call P_Simple_Expression
2127 -- here, since we want to handle the case of a parenthesized range
2128 -- attribute cleanly.
2130 if Token = Tok_Left_Paren then
2132 Lptr : constant Source_Ptr := Token_Ptr;
2133 Scan_State : Saved_Scan_State;
2136 Save_Scan_State (Scan_State);
2137 Scan; -- past left paren
2138 Sexpr := P_Simple_Expression;
2140 if Token = Tok_Apostrophe then
2141 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2142 Expr_Form := EF_Range_Attr;
2144 if Token = Tok_Right_Paren then
2145 Scan; -- scan past right paren if present
2148 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2153 Restore_Scan_State (Scan_State);
2157 -- Here after dealing with parenthesized range attribute
2159 Sexpr := P_Simple_Expression;
2161 if Token = Tok_Apostrophe then
2162 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2163 Expr_Form := EF_Range_Attr;
2169 end P_Simple_Expression_Or_Range_Attribute;
2175 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2177 -- Error recovery: can raise Error_Resync
2179 function P_Term return Node_Id is
2180 Node1, Node2 : Node_Id;
2181 Tokptr : Source_Ptr;
2187 exit when Token not in Token_Class_Mulop;
2188 Tokptr := Token_Ptr;
2189 Node2 := New_Node (P_Multiplying_Operator, Tokptr);
2190 Scan; -- past operator
2191 Set_Left_Opnd (Node2, Node1);
2192 Set_Right_Opnd (Node2, P_Factor);
2193 Set_Op_Name (Node2);
2204 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2206 -- Error recovery: can raise Error_Resync
2208 function P_Factor return Node_Id is
2213 if Token = Tok_Abs then
2214 Node1 := New_Node (N_Op_Abs, Token_Ptr);
2217 Style.Check_Abs_Not;
2221 Set_Right_Opnd (Node1, P_Primary);
2222 Set_Op_Name (Node1);
2225 elsif Token = Tok_Not then
2226 Node1 := New_Node (N_Op_Not, Token_Ptr);
2229 Style.Check_Abs_Not;
2233 Set_Right_Opnd (Node1, P_Primary);
2234 Set_Op_Name (Node1);
2240 if Token = Tok_Double_Asterisk then
2241 Node2 := New_Node (N_Op_Expon, Token_Ptr);
2243 Set_Left_Opnd (Node2, Node1);
2244 Set_Right_Opnd (Node2, P_Primary);
2245 Set_Op_Name (Node2);
2258 -- NUMERIC_LITERAL | null
2259 -- | STRING_LITERAL | AGGREGATE
2260 -- | NAME | QUALIFIED_EXPRESSION
2261 -- | ALLOCATOR | (EXPRESSION)
2263 -- Error recovery: can raise Error_Resync
2265 function P_Primary return Node_Id is
2266 Scan_State : Saved_Scan_State;
2270 -- The loop runs more than once only if misplaced pragmas are found
2275 -- Name token can start a name, call or qualified expression, all
2276 -- of which are acceptable possibilities for primary. Note also
2277 -- that string literal is included in name (as operator symbol)
2278 -- and type conversion is included in name (as indexed component).
2280 when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2283 -- All done unless apostrophe follows
2285 if Token /= Tok_Apostrophe then
2288 -- Apostrophe following means that we have either just parsed
2289 -- the subtype mark of a qualified expression, or the prefix
2290 -- or a range attribute.
2292 else -- Token = Tok_Apostrophe
2293 Save_Scan_State (Scan_State); -- at apostrophe
2294 Scan; -- past apostrophe
2296 -- If range attribute, then this is always an error, since
2297 -- the only legitimate case (where the scanned expression is
2298 -- a qualified simple name) is handled at the level of the
2299 -- Simple_Expression processing. This case corresponds to a
2300 -- usage such as 3 + A'Range, which is always illegal.
2302 if Token = Tok_Range then
2303 Restore_Scan_State (Scan_State); -- to apostrophe
2304 Bad_Range_Attribute (Token_Ptr);
2307 -- If left paren, then we have a qualified expression.
2308 -- Note that P_Name guarantees that in this case, where
2309 -- Token = Tok_Apostrophe on return, the only two possible
2310 -- tokens following the apostrophe are left paren and
2311 -- RANGE, so we know we have a left paren here.
2313 else -- Token = Tok_Left_Paren
2314 return P_Qualified_Expression (Node1);
2319 -- Numeric or string literal
2321 when Tok_Integer_Literal |
2323 Tok_String_Literal =>
2325 Node1 := Token_Node;
2326 Scan; -- past number
2329 -- Left paren, starts aggregate or parenthesized expression
2331 when Tok_Left_Paren =>
2333 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2336 if Nkind (Expr) = N_Attribute_Reference
2337 and then Attribute_Name (Expr) = Name_Range
2339 Bad_Range_Attribute (Sloc (Expr));
2354 return New_Node (N_Null, Prev_Token_Ptr);
2356 -- Pragma, not allowed here, so just skip past it
2359 P_Pragmas_Misplaced;
2361 -- Deal with IF (possible unparenthesized conditional expression)
2365 -- If this looks like a real if, defined as an IF appearing at
2366 -- the start of a new line, then we consider we have a missing
2369 if Token_Is_At_Start_Of_Line then
2370 Error_Msg_AP ("missing operand");
2373 -- If this looks like a conditional expression, then treat it
2374 -- that way with an error messasge.
2376 elsif Extensions_Allowed then
2378 ("conditional expression must be parenthesized");
2379 return P_Conditional_Expression;
2381 -- Otherwise treat as misused identifier
2384 return P_Identifier;
2387 -- Anything else is illegal as the first token of a primary, but
2388 -- we test for a reserved identifier so that it is treated nicely
2391 if Is_Reserved_Identifier then
2392 return P_Identifier;
2394 elsif Prev_Token = Tok_Comma then
2395 Error_Msg_SP ("|extra "","" ignored");
2399 Error_Msg_AP ("missing operand");
2407 ---------------------------
2408 -- 4.5 Logical Operator --
2409 ---------------------------
2411 -- LOGICAL_OPERATOR ::= and | or | xor
2413 -- Note: AND THEN and OR ELSE are also treated as logical operators
2414 -- by the parser (even though they are not operators semantically)
2416 -- The value returned is the appropriate Node_Kind code for the operator
2417 -- On return, Token points to the token following the scanned operator.
2419 -- The caller has checked that the first token is a legitimate logical
2420 -- operator token (i.e. is either XOR, AND, OR).
2422 -- Error recovery: cannot raise Error_Resync
2424 function P_Logical_Operator return Node_Kind is
2426 if Token = Tok_And then
2428 Style.Check_Binary_Operator;
2433 if Token = Tok_Then then
2440 elsif Token = Tok_Or then
2442 Style.Check_Binary_Operator;
2447 if Token = Tok_Else then
2454 else -- Token = Tok_Xor
2456 Style.Check_Binary_Operator;
2462 end P_Logical_Operator;
2464 ------------------------------
2465 -- 4.5 Relational Operator --
2466 ------------------------------
2468 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2470 -- The value returned is the appropriate Node_Kind code for the operator.
2471 -- On return, Token points to the operator token, NOT past it.
2473 -- The caller has checked that the first token is a legitimate relational
2474 -- operator token (i.e. is one of the operator tokens listed above).
2476 -- Error recovery: cannot raise Error_Resync
2478 function P_Relational_Operator return Node_Kind is
2479 Op_Kind : Node_Kind;
2480 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2481 (Tok_Less => N_Op_Lt,
2482 Tok_Equal => N_Op_Eq,
2483 Tok_Greater => N_Op_Gt,
2484 Tok_Not_Equal => N_Op_Ne,
2485 Tok_Greater_Equal => N_Op_Ge,
2486 Tok_Less_Equal => N_Op_Le,
2488 Tok_Not => N_Not_In,
2489 Tok_Box => N_Op_Ne);
2492 if Token = Tok_Box then
2493 Error_Msg_SC ("|""'<'>"" should be ""/=""");
2496 Op_Kind := Relop_Node (Token);
2499 Style.Check_Binary_Operator;
2502 Scan; -- past operator token
2504 if Prev_Token = Tok_Not then
2509 end P_Relational_Operator;
2511 ---------------------------------
2512 -- 4.5 Binary Adding Operator --
2513 ---------------------------------
2515 -- BINARY_ADDING_OPERATOR ::= + | - | &
2517 -- The value returned is the appropriate Node_Kind code for the operator.
2518 -- On return, Token points to the operator token (NOT past it).
2520 -- The caller has checked that the first token is a legitimate adding
2521 -- operator token (i.e. is one of the operator tokens listed above).
2523 -- Error recovery: cannot raise Error_Resync
2525 function P_Binary_Adding_Operator return Node_Kind is
2526 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2527 (Tok_Ampersand => N_Op_Concat,
2528 Tok_Minus => N_Op_Subtract,
2529 Tok_Plus => N_Op_Add);
2531 return Addop_Node (Token);
2532 end P_Binary_Adding_Operator;
2534 --------------------------------
2535 -- 4.5 Unary Adding Operator --
2536 --------------------------------
2538 -- UNARY_ADDING_OPERATOR ::= + | -
2540 -- The value returned is the appropriate Node_Kind code for the operator.
2541 -- On return, Token points to the operator token (NOT past it).
2543 -- The caller has checked that the first token is a legitimate adding
2544 -- operator token (i.e. is one of the operator tokens listed above).
2546 -- Error recovery: cannot raise Error_Resync
2548 function P_Unary_Adding_Operator return Node_Kind is
2549 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2550 (Tok_Minus => N_Op_Minus,
2551 Tok_Plus => N_Op_Plus);
2553 return Addop_Node (Token);
2554 end P_Unary_Adding_Operator;
2556 -------------------------------
2557 -- 4.5 Multiplying Operator --
2558 -------------------------------
2560 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
2562 -- The value returned is the appropriate Node_Kind code for the operator.
2563 -- On return, Token points to the operator token (NOT past it).
2565 -- The caller has checked that the first token is a legitimate multiplying
2566 -- operator token (i.e. is one of the operator tokens listed above).
2568 -- Error recovery: cannot raise Error_Resync
2570 function P_Multiplying_Operator return Node_Kind is
2571 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2572 (Tok_Asterisk => N_Op_Multiply,
2573 Tok_Mod => N_Op_Mod,
2574 Tok_Rem => N_Op_Rem,
2575 Tok_Slash => N_Op_Divide);
2577 return Mulop_Node (Token);
2578 end P_Multiplying_Operator;
2580 --------------------------------------
2581 -- 4.5 Highest Precedence Operator --
2582 --------------------------------------
2584 -- Parsed by P_Factor (4.4)
2586 -- Note: this rule is not in fact used by the grammar at any point!
2588 --------------------------
2589 -- 4.6 Type Conversion --
2590 --------------------------
2592 -- Parsed by P_Primary as a Name (4.1)
2594 -------------------------------
2595 -- 4.7 Qualified Expression --
2596 -------------------------------
2598 -- QUALIFIED_EXPRESSION ::=
2599 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2601 -- The caller has scanned the name which is the Subtype_Mark parameter
2602 -- and scanned past the single quote following the subtype mark. The
2603 -- caller has not checked that this name is in fact appropriate for
2604 -- a subtype mark name (i.e. it is a selected component or identifier).
2606 -- Error_Recovery: cannot raise Error_Resync
2608 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2609 Qual_Node : Node_Id;
2611 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2612 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2613 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2615 end P_Qualified_Expression;
2617 --------------------
2619 --------------------
2622 -- new [NULL_EXCLUSION] SUBTYPE_INDICATION | new QUALIFIED_EXPRESSION
2624 -- The caller has checked that the initial token is NEW
2626 -- Error recovery: can raise Error_Resync
2628 function P_Allocator return Node_Id is
2629 Alloc_Node : Node_Id;
2630 Type_Node : Node_Id;
2631 Null_Exclusion_Present : Boolean;
2634 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2637 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
2639 Null_Exclusion_Present := P_Null_Exclusion;
2640 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
2641 Type_Node := P_Subtype_Mark_Resync;
2643 if Token = Tok_Apostrophe then
2644 Scan; -- past apostrophe
2645 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2649 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
2655 ------------------------------
2656 -- P_Conditional_Expression --
2657 ------------------------------
2659 function P_Conditional_Expression return Node_Id is
2660 Exprs : constant List_Id := New_List;
2661 Loc : constant Source_Ptr := Token_Ptr;
2663 State : Saved_Scan_State;
2666 Inside_Conditional_Expression := Inside_Conditional_Expression + 1;
2668 if Token = Tok_If and then not Extensions_Allowed then
2669 Error_Msg_SC ("|conditional expression is an Ada extension");
2670 Error_Msg_SC ("\|use -gnatX switch to compile this unit");
2673 Scan; -- past IF or ELSIF
2674 Append_To (Exprs, P_Expression_No_Right_Paren);
2676 Append_To (Exprs, P_Expression);
2678 -- We now have scanned out IF expr THEN expr
2680 -- Check for common error of semicolon before the ELSE
2682 if Token = Tok_Semicolon then
2683 Save_Scan_State (State);
2684 Scan; -- past semicolon
2686 if Token = Tok_Else or else Token = Tok_Elsif then
2687 Error_Msg_SP ("|extra "";"" ignored");
2690 Restore_Scan_State (State);
2694 -- Scan out ELSIF sequence if present
2696 if Token = Tok_Elsif then
2697 Expr := P_Conditional_Expression;
2698 Set_Is_Elsif (Expr);
2699 Append_To (Exprs, Expr);
2701 -- Scan out ELSE phrase if present
2703 elsif Token = Tok_Else then
2705 -- Scan out ELSE expression
2708 Append_To (Exprs, P_Expression);
2710 -- Two expression case (implied True, filled in during semantics)
2716 -- If we have an END IF, diagnose as not needed
2718 if Token = Tok_End then
2720 ("`END IF` not allowed at end of conditional expression");
2723 if Token = Tok_If then
2728 Inside_Conditional_Expression := Inside_Conditional_Expression - 1;
2730 -- Return the Conditional_Expression node
2733 Make_Conditional_Expression (Loc,
2734 Expressions => Exprs);
2735 end P_Conditional_Expression;