1 -----------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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_Base
=> True,
39 Attribute_Body_Version
=> True,
40 Attribute_Class
=> True,
41 Attribute_External_Tag
=> True,
42 Attribute_Img
=> True,
43 Attribute_Loop_Entry
=> True,
44 Attribute_Old
=> True,
45 Attribute_Result
=> True,
46 Attribute_Stub_Type
=> True,
47 Attribute_Version
=> True,
48 Attribute_Type_Key
=> True,
50 -- This map contains True for parameterless attributes that return a string
51 -- or a type. For those attributes, a left parenthesis after the attribute
52 -- should not be analyzed as the beginning of a parameters list because it
53 -- may denote a slice operation (X'Img (1 .. 2)) or a type conversion
56 -- Note: Loop_Entry is in this list because, although it can take an
57 -- optional argument (the loop name), we can't distinguish that at parse
58 -- time from the case where no loop name is given and a legitimate index
59 -- expression is present. So we parse the argument as an indexed component
60 -- and the semantic analysis sorts out this syntactic ambiguity based on
61 -- the type and form of the expression.
63 -- Note that this map designates the minimum set of attributes where a
64 -- construct in parentheses that is not an argument can appear right
65 -- after the attribute. For attributes like 'Size, we do not put them
66 -- in the map. If someone writes X'Size (3), that's illegal in any case,
67 -- but we get a better error message by parsing the (3) as an illegal
68 -- argument to the attribute, rather than some meaningless junk that
69 -- follows the attribute.
71 -----------------------
72 -- Local Subprograms --
73 -----------------------
75 function P_Aggregate_Or_Paren_Expr
return Node_Id
;
76 function P_Allocator
return Node_Id
;
77 function P_Case_Expression_Alternative
return Node_Id
;
78 function P_Iterated_Component_Association
return Node_Id
;
79 function P_Record_Or_Array_Component_Association
return Node_Id
;
80 function P_Factor
return Node_Id
;
81 function P_Primary
return Node_Id
;
82 function P_Relation
return Node_Id
;
83 function P_Term
return Node_Id
;
84 function P_Declare_Expression
return Node_Id
;
85 function P_Reduction_Attribute_Reference
(S
: Node_Id
)
88 function P_Binary_Adding_Operator
return Node_Kind
;
89 function P_Logical_Operator
return Node_Kind
;
90 function P_Multiplying_Operator
return Node_Kind
;
91 function P_Relational_Operator
return Node_Kind
;
92 function P_Unary_Adding_Operator
return Node_Kind
;
94 procedure Bad_Range_Attribute
(Loc
: Source_Ptr
);
95 -- Called to place complaint about bad range attribute at the given
96 -- source location. Terminates by raising Error_Resync.
98 procedure Check_Bad_Exp
;
99 -- Called after scanning a**b, posts error if ** detected
101 procedure P_Membership_Test
(N
: Node_Id
);
102 -- N is the node for a N_In or N_Not_In node whose right operand has not
103 -- yet been processed. It is called just after scanning out the IN keyword.
104 -- On return, either Right_Opnd or Alternatives is set, as appropriate.
106 function P_Range_Attribute_Reference
(Prefix_Node
: Node_Id
) return Node_Id
;
107 -- Scan a range attribute reference. The caller has scanned out the
108 -- prefix. The current token is known to be an apostrophe and the
109 -- following token is known to be RANGE.
111 function P_Case_Expression
return Node_Id
;
112 -- Scans out a case expression. Called with Token pointing to the CASE
113 -- keyword, and returns pointing to the terminating right parent,
114 -- semicolon, or comma, but does not consume this terminating token.
116 function P_Unparen_Cond_Expr_Etc
return Node_Id
;
117 -- This function is called with Token pointing to IF, CASE, FOR, or
118 -- DECLARE, in a context that allows a conditional (if or case) expression,
119 -- a quantified expression, an iterated component association, or a declare
120 -- expression, if it is surrounded by parentheses. If not surrounded by
121 -- parentheses, the expression is still returned, but an error message is
124 -------------------------
125 -- Bad_Range_Attribute --
126 -------------------------
128 procedure Bad_Range_Attribute
(Loc
: Source_Ptr
) is
130 Error_Msg
("range attribute cannot be used in expression!", Loc
);
132 end Bad_Range_Attribute
;
138 procedure Check_Bad_Exp
is
140 if Token
= Tok_Double_Asterisk
then
141 Error_Msg_SC
("parenthesization required for '*'*");
143 Discard_Junk_Node
(P_Primary
);
148 --------------------------
149 -- 4.1 Name (also 6.4) --
150 --------------------------
153 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
154 -- | INDEXED_COMPONENT | SLICE
155 -- | SELECTED_COMPONENT | ATTRIBUTE
156 -- | TYPE_CONVERSION | FUNCTION_CALL
157 -- | CHARACTER_LITERAL | TARGET_NAME
159 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
161 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
163 -- EXPLICIT_DEREFERENCE ::= NAME . all
165 -- IMPLICIT_DEREFERENCE ::= NAME
167 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
169 -- SLICE ::= PREFIX (DISCRETE_RANGE)
171 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
173 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
175 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
177 -- ATTRIBUTE_DESIGNATOR ::=
178 -- IDENTIFIER [(static_EXPRESSION)]
179 -- | access | delta | digits
183 -- | function_PREFIX ACTUAL_PARAMETER_PART
185 -- ACTUAL_PARAMETER_PART ::=
186 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
188 -- PARAMETER_ASSOCIATION ::=
189 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
191 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
193 -- TARGET_NAME ::= @ (AI12-0125-3: abbreviation for LHS)
195 -- Note: syntactically a procedure call looks just like a function call,
196 -- so this routine is in practice used to scan out procedure calls as well.
198 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
200 -- Error recovery: can raise Error_Resync
202 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
203 -- followed by either a left paren (qualified expression case), or by
204 -- range (range attribute case). All other uses of apostrophe (i.e. all
205 -- other attributes) are handled in this routine.
207 -- Error recovery: can raise Error_Resync
209 function P_Name
return Node_Id
is
210 Scan_State
: Saved_Scan_State
;
212 Prefix_Node
: Node_Id
;
213 Ident_Node
: Node_Id
;
215 Range_Node
: Node_Id
;
218 Arg_List
: List_Id
:= No_List
; -- kill junk warning
219 Attr_Name
: Name_Id
:= No_Name
; -- kill junk warning
222 -- Case of not a name
224 if Token
not in Token_Class_Name
then
226 -- If it looks like start of expression, complain and scan expression
228 if Token
in Token_Class_Literal | Tok_Left_Paren
then
229 Error_Msg_SC
("name expected");
232 -- Otherwise some other junk, not much we can do
235 Error_Msg_AP
("name expected");
240 -- Loop through designators in qualified name
241 -- AI12-0125 : target_name
243 if Token
= Tok_At_Sign
then
244 Scan_Reserved_Identifier
(Force_Msg
=> False);
246 if Present
(Current_Assign_Node
) then
247 Set_Has_Target_Names
(Current_Assign_Node
);
251 Name_Node
:= Token_Node
;
254 Scan
; -- past designator
255 exit when Token
/= Tok_Dot
;
256 Save_Scan_State
(Scan_State
); -- at dot
259 -- If we do not have another designator after the dot, then join
260 -- the normal circuit to handle a dot extension (may be .all or
261 -- character literal case). Otherwise loop back to scan the next
264 if Token
not in Token_Class_Desig
then
265 goto Scan_Name_Extension_Dot
;
267 Prefix_Node
:= Name_Node
;
268 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
269 Set_Prefix
(Name_Node
, Prefix_Node
);
270 Set_Selector_Name
(Name_Node
, Token_Node
);
274 -- We have now scanned out a qualified designator. If the last token is
275 -- an operator symbol, then we certainly do not have the Snam case, so
276 -- we can just use the normal name extension check circuit
278 if Prev_Token
= Tok_Operator_Symbol
then
279 goto Scan_Name_Extension
;
282 -- We have scanned out a qualified simple name, check for name
283 -- extension. Note that we know there is no dot here at this stage,
284 -- so the only possible cases of name extension are apostrophe followed
287 if Token
= Tok_Apostrophe
then
288 Save_Scan_State
(Scan_State
); -- at apostrophe
289 Scan
; -- past apostrophe
291 -- Qualified expression in Ada 2012 mode (treated as a name)
293 if Ada_Version
>= Ada_2012
294 and then Token
in Tok_Left_Paren | Tok_Left_Bracket
296 goto Scan_Name_Extension_Apostrophe
;
298 -- If left paren not in Ada 2012, then it is not part of the name,
299 -- since qualified expressions are not names in prior versions of
300 -- Ada, so return with Token backed up to point to the apostrophe.
301 -- The treatment for the range attribute is similar (we do not
302 -- consider x'range to be a name in this grammar).
304 elsif Token
in Tok_Left_Paren | Tok_Range
then
305 Restore_Scan_State
(Scan_State
); -- to apostrophe
306 Expr_Form
:= EF_Simple_Name
;
309 -- Otherwise we have the case of a name extended by an attribute
312 goto Scan_Name_Extension_Apostrophe
;
315 -- Check case of qualified simple name extended by a left parenthesis
317 elsif Token
= Tok_Left_Paren
then
318 Scan
; -- past left paren
319 goto Scan_Name_Extension_Left_Paren
;
321 -- Otherwise the qualified simple name is not extended, so return
324 Expr_Form
:= EF_Simple_Name
;
328 -- Loop scanning past name extensions. A label is used for control
329 -- transfer for this loop for ease of interfacing with the finite state
330 -- machine in the parenthesis scanning circuit, and also to allow for
331 -- passing in control to the appropriate point from the above code.
333 <<Scan_Name_Extension
>>
335 -- Character literal used as name cannot be extended. Also this
336 -- cannot be a call, since the name for a call must be a designator.
337 -- Return in these cases, or if there is no name extension
339 if Token
not in Token_Class_Namext
340 or else Prev_Token
= Tok_Char_Literal
342 Expr_Form
:= EF_Name
;
346 -- Merge here when we know there is a name extension
348 <<Scan_Name_Extension_OK
>>
351 when Tok_Left_Paren
=>
352 Scan
; -- past left paren
353 goto Scan_Name_Extension_Left_Paren
;
355 when Tok_Apostrophe
=>
356 Save_Scan_State
(Scan_State
); -- at apostrophe
357 Scan
; -- past apostrophe
358 goto Scan_Name_Extension_Apostrophe
;
361 Save_Scan_State
(Scan_State
); -- at dot
363 goto Scan_Name_Extension_Dot
;
365 when others => raise Program_Error
;
368 -- Case of name extended by dot (selection), dot is already skipped
369 -- and the scan state at the point of the dot is saved in Scan_State.
371 <<Scan_Name_Extension_Dot
>>
373 -- Explicit dereference case
375 if Token
= Tok_All
then
376 Prefix_Node
:= Name_Node
;
377 Name_Node
:= New_Node
(N_Explicit_Dereference
, Token_Ptr
);
378 Set_Prefix
(Name_Node
, Prefix_Node
);
380 goto Scan_Name_Extension
;
382 -- Selected component case
384 elsif Token
in Token_Class_Name
then
385 Prefix_Node
:= Name_Node
;
386 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
387 Set_Prefix
(Name_Node
, Prefix_Node
);
388 Set_Selector_Name
(Name_Node
, Token_Node
);
389 Scan
; -- past selector
390 goto Scan_Name_Extension
;
392 -- Reserved identifier as selector
394 elsif Is_Reserved_Identifier
then
395 Scan_Reserved_Identifier
(Force_Msg
=> False);
396 Prefix_Node
:= Name_Node
;
397 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
398 Set_Prefix
(Name_Node
, Prefix_Node
);
399 Set_Selector_Name
(Name_Node
, Token_Node
);
400 Scan
; -- past identifier used as selector
401 goto Scan_Name_Extension
;
403 -- If dot is at end of line and followed by nothing legal,
404 -- then assume end of name and quit (dot will be taken as
405 -- an incorrect form of some other punctuation by our caller).
407 elsif Token_Is_At_Start_Of_Line
then
408 Restore_Scan_State
(Scan_State
);
411 -- Here if nothing legal after the dot
414 Error_Msg_AP
("selector expected");
418 -- Here for an apostrophe as name extension. The scan position at the
419 -- apostrophe has already been saved, and the apostrophe scanned out.
421 <<Scan_Name_Extension_Apostrophe
>>
423 Scan_Apostrophe
: declare
424 function Apostrophe_Should_Be_Semicolon
return Boolean;
425 -- Checks for case where apostrophe should probably be
426 -- a semicolon, and if so, gives appropriate message,
427 -- resets the scan pointer to the apostrophe, changes
428 -- the current token to Tok_Semicolon, and returns True.
429 -- Otherwise returns False.
431 ------------------------------------
432 -- Apostrophe_Should_Be_Semicolon --
433 ------------------------------------
435 function Apostrophe_Should_Be_Semicolon
return Boolean is
437 if Token_Is_At_Start_Of_Line
then
438 Restore_Scan_State
(Scan_State
); -- to apostrophe
439 Error_Msg_SC
("|""''"" should be "";""");
440 Token
:= Tok_Semicolon
;
445 end Apostrophe_Should_Be_Semicolon
;
447 -- Start of processing for Scan_Apostrophe
450 -- Check for qualified expression case in Ada 2012 mode
452 if Ada_Version
>= Ada_2012
453 and then Token
in Tok_Left_Paren | Tok_Left_Bracket
455 Name_Node
:= P_Qualified_Expression
(Name_Node
);
456 goto Scan_Name_Extension
;
458 -- If range attribute after apostrophe, then return with Token
459 -- pointing to the apostrophe. Note that in this case the prefix
460 -- need not be a simple name (cases like A.all'range). Similarly
461 -- if there is a left paren after the apostrophe, then we also
462 -- return with Token pointing to the apostrophe (this is the
463 -- aggregate case, or some error case).
465 elsif Token
in Tok_Range | Tok_Left_Paren
then
466 Restore_Scan_State
(Scan_State
); -- to apostrophe
467 Expr_Form
:= EF_Name
;
470 -- Here for cases where attribute designator is an identifier
472 elsif Token
= Tok_Identifier
then
473 Attr_Name
:= Token_Name
;
475 if not Is_Attribute_Name
(Attr_Name
) then
476 if Apostrophe_Should_Be_Semicolon
then
477 Expr_Form
:= EF_Name
;
480 -- Here for a bad attribute name
483 Signal_Bad_Attribute
;
484 Scan
; -- past bad identifier
486 if Token
= Tok_Left_Paren
then
487 Scan
; -- past left paren
490 Discard_Junk_Node
(P_Expression_If_OK
);
491 exit when not Comma_Present
;
502 Style
.Check_Attribute_Name
(False);
505 -- Here for case of attribute designator is not an identifier
508 if Token
= Tok_Delta
then
509 Attr_Name
:= Name_Delta
;
511 elsif Token
= Tok_Digits
then
512 Attr_Name
:= Name_Digits
;
514 elsif Token
= Tok_Access
then
515 Attr_Name
:= Name_Access
;
517 elsif Token
= Tok_Mod
and then Ada_Version
>= Ada_95
then
518 Attr_Name
:= Name_Mod
;
520 elsif Apostrophe_Should_Be_Semicolon
then
521 Expr_Form
:= EF_Name
;
525 Error_Msg_AP
("attribute designator expected");
530 Style
.Check_Attribute_Name
(True);
534 -- We come here with an OK attribute scanned, and corresponding
535 -- Attribute identifier node stored in Ident_Node.
537 Prefix_Node
:= Name_Node
;
538 Name_Node
:= New_Node
(N_Attribute_Reference
, Prev_Token_Ptr
);
539 Scan
; -- past attribute designator
540 Set_Prefix
(Name_Node
, Prefix_Node
);
541 Set_Attribute_Name
(Name_Node
, Attr_Name
);
543 -- Scan attribute arguments/designator. We skip this if we know
544 -- that the attribute cannot have an argument (see documentation
545 -- of Is_Parameterless_Attribute for further details).
547 if Token
= Tok_Left_Paren
549 Is_Parameterless_Attribute
(Get_Attribute_Id
(Attr_Name
))
551 -- Attribute Update contains an array or record association
552 -- list which provides new values for various components or
553 -- elements. The list is parsed as an aggregate, and we get
554 -- better error handling by knowing that in the parser.
556 if Attr_Name
= Name_Update
then
557 Set_Expressions
(Name_Node
, New_List
);
558 Append
(P_Aggregate
, Expressions
(Name_Node
));
560 -- All other cases of parsing attribute arguments
563 Set_Expressions
(Name_Node
, New_List
);
564 Scan
; -- past left paren
568 Expr
: constant Node_Id
:= P_Expression_If_OK
;
572 -- Case of => for named notation
574 if Token
= Tok_Arrow
then
576 -- Named notation allowed only for the special
577 -- case of System'Restriction_Set (No_Dependence =>
578 -- unit_NAME), in which case construct a parameter
579 -- assocation node and append to the arguments.
581 if Attr_Name
= Name_Restriction_Set
582 and then Nkind
(Expr
) = N_Identifier
583 and then Chars
(Expr
) = Name_No_Dependence
587 Append_To
(Expressions
(Name_Node
),
588 Make_Parameter_Association
(Sloc
(Rnam
),
589 Selector_Name
=> Expr
,
590 Explicit_Actual_Parameter
=> Rnam
));
593 -- For all other cases named notation is illegal
597 ("named parameters not permitted "
599 Scan
; -- past junk arrow
602 -- Here for normal case (not => for named parameter)
605 -- Special handling for 'Image in Ada 2012, where
606 -- the attribute can be parameterless and its value
607 -- can be the prefix of a slice. Rewrite name as a
608 -- slice, Expr is its low bound.
610 if Token
= Tok_Dot_Dot
611 and then Attr_Name
= Name_Image
612 and then Ada_Version
>= Ada_2012
614 Set_Expressions
(Name_Node
, No_List
);
615 Prefix_Node
:= Name_Node
;
617 New_Node
(N_Slice
, Sloc
(Prefix_Node
));
618 Set_Prefix
(Name_Node
, Prefix_Node
);
619 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
620 Set_Low_Bound
(Range_Node
, Expr
);
622 Expr_Node
:= P_Expression
;
623 Check_Simple_Expression
(Expr_Node
);
624 Set_High_Bound
(Range_Node
, Expr_Node
);
625 Set_Discrete_Range
(Name_Node
, Range_Node
);
628 goto Scan_Name_Extension
;
631 Append
(Expr
, Expressions
(Name_Node
));
632 exit when not Comma_Present
;
642 goto Scan_Name_Extension
;
645 -- Here for left parenthesis extending name (left paren skipped)
647 <<Scan_Name_Extension_Left_Paren
>>
649 -- We now have to scan through a list of items, terminated by a
650 -- right parenthesis. The scan is handled by a finite state
651 -- machine. The possibilities are:
655 -- This is a slice. This case is handled in LP_State_Init
657 -- (expression, expression, ..)
659 -- This is interpreted as an indexed component, i.e. as a
660 -- case of a name which can be extended in the normal manner.
661 -- This case is handled by LP_State_Name or LP_State_Expr.
663 -- Note: if and case expressions (without an extra level of
664 -- parentheses) are permitted in this context).
666 -- (..., identifier => expression , ...)
668 -- If there is at least one occurrence of identifier => (but
669 -- none of the other cases apply), then we have a call.
671 -- Test for Id => case
673 if Token
= Tok_Identifier
then
674 Save_Scan_State
(Scan_State
); -- at Id
677 -- Test for => (allow := as an error substitute)
679 if Token
in Tok_Arrow | Tok_Colon_Equal
then
680 Restore_Scan_State
(Scan_State
); -- to Id
681 Arg_List
:= New_List
;
685 Restore_Scan_State
(Scan_State
); -- to Id
689 -- Here we have an expression after all
691 Expr_Node
:= P_Expression_Or_Range_Attribute_If_OK
;
693 -- Check cases of discrete range for a slice
695 -- First possibility: Range_Attribute_Reference
697 if Expr_Form
= EF_Range_Attr
then
698 Range_Node
:= Expr_Node
;
700 -- Second possibility: Simple_expression .. Simple_expression
702 elsif Token
= Tok_Dot_Dot
then
703 Check_Simple_Expression
(Expr_Node
);
704 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
705 Set_Low_Bound
(Range_Node
, Expr_Node
);
707 Expr_Node
:= P_Expression
;
708 Check_Simple_Expression
(Expr_Node
);
709 Set_High_Bound
(Range_Node
, Expr_Node
);
711 -- Third possibility: Type_name range Range
713 elsif Token
= Tok_Range
then
714 if Expr_Form
/= EF_Simple_Name
then
715 Error_Msg_SC
("subtype mark must precede RANGE");
719 Range_Node
:= P_Subtype_Indication
(Expr_Node
);
721 -- Otherwise we just have an expression. It is true that we might
722 -- have a subtype mark without a range constraint but this case
723 -- is syntactically indistinguishable from the expression case.
726 Arg_List
:= New_List
;
730 -- Fall through here with unmistakable Discrete range scanned,
731 -- which means that we definitely have the case of a slice. The
732 -- Discrete range is in Range_Node.
734 if Token
= Tok_Comma
then
735 Error_Msg_SC
("slice cannot have more than one dimension");
738 elsif Token
/= Tok_Right_Paren
then
739 if Token
= Tok_Arrow
then
741 -- This may be an aggregate that is missing a qualification
744 ("context of aggregate must be a qualified expression");
753 Scan
; -- past right paren
754 Prefix_Node
:= Name_Node
;
755 Name_Node
:= New_Node
(N_Slice
, Sloc
(Prefix_Node
));
756 Set_Prefix
(Name_Node
, Prefix_Node
);
757 Set_Discrete_Range
(Name_Node
, Range_Node
);
759 -- An operator node is legal as a prefix to other names,
760 -- but not for a slice.
762 if Nkind
(Prefix_Node
) = N_Operator_Symbol
then
763 Error_Msg_N
("illegal prefix for slice", Prefix_Node
);
766 -- If we have a name extension, go scan it
768 if Token
in Token_Class_Namext
then
769 goto Scan_Name_Extension_OK
;
771 -- Otherwise return (a slice is a name, but is not a call)
774 Expr_Form
:= EF_Name
;
779 -- In LP_State_Expr, we have scanned one or more expressions, and
780 -- so we have a call or an indexed component which is a name. On
781 -- entry we have the expression just scanned in Expr_Node and
782 -- Arg_List contains the list of expressions encountered so far
785 Append
(Expr_Node
, Arg_List
);
787 if Token
= Tok_Arrow
then
789 ("expect identifier in parameter association", Sloc
(Expr_Node
));
792 elsif not Comma_Present
then
795 Prefix_Node
:= Name_Node
;
796 Name_Node
:= New_Node
(N_Indexed_Component
, Sloc
(Prefix_Node
));
797 Set_Prefix
(Name_Node
, Prefix_Node
);
798 Set_Expressions
(Name_Node
, Arg_List
);
800 goto Scan_Name_Extension
;
803 -- Comma present (and scanned out), test for identifier => case
804 -- Test for identifier => case
806 if Token
= Tok_Identifier
then
807 Save_Scan_State
(Scan_State
); -- at Id
810 -- Test for => (allow := as error substitute)
812 if Token
in Tok_Arrow | Tok_Colon_Equal
then
813 Restore_Scan_State
(Scan_State
); -- to Id
816 -- Otherwise it's just an expression after all, so backup
819 Restore_Scan_State
(Scan_State
); -- to Id
823 -- Here we have an expression after all, so stay in this state
825 Expr_Node
:= P_Expression_If_OK
;
828 -- LP_State_Call corresponds to the situation in which at least one
829 -- instance of Id => Expression has been encountered, so we know that
830 -- we do not have a name, but rather a call. We enter it with the
831 -- scan pointer pointing to the next argument to scan, and Arg_List
832 -- containing the list of arguments scanned so far.
836 -- Test for case of Id => Expression (named parameter)
838 if Token
= Tok_Identifier
then
839 Save_Scan_State
(Scan_State
); -- at Id
840 Ident_Node
:= Token_Node
;
843 -- Deal with => (allow := as incorrect substitute)
845 if Token
in Tok_Arrow | Tok_Colon_Equal
then
846 Arg_Node
:= New_Node
(N_Parameter_Association
, Prev_Token_Ptr
);
847 Set_Selector_Name
(Arg_Node
, Ident_Node
);
849 Set_Explicit_Actual_Parameter
(Arg_Node
, P_Expression
);
850 Append
(Arg_Node
, Arg_List
);
852 -- If a comma follows, go back and scan next entry
854 if Comma_Present
then
857 -- Otherwise we have the end of a call
860 Prefix_Node
:= Name_Node
;
861 Name_Node
:= New_Node
(N_Function_Call
, Sloc
(Prefix_Node
));
862 Set_Name
(Name_Node
, Prefix_Node
);
863 Set_Parameter_Associations
(Name_Node
, Arg_List
);
866 if Token
in Token_Class_Namext
then
867 goto Scan_Name_Extension_OK
;
869 -- This is a case of a call which cannot be a name
872 Expr_Form
:= EF_Name
;
877 -- Not named parameter: Id started an expression after all
880 Restore_Scan_State
(Scan_State
); -- to Id
884 -- Here if entry did not start with Id => which means that it
885 -- is a positional parameter, which is not allowed, since we
886 -- have seen at least one named parameter already.
889 ("positional parameter association " &
890 "not allowed after named one");
892 Expr_Node
:= P_Expression_If_OK
;
894 -- Leaving the '>' in an association is not unusual, so suggest
897 if Nkind
(Expr_Node
) = N_Op_Eq
then
898 Error_Msg_N
("\maybe `='>` was intended", Expr_Node
);
901 -- We go back to scanning out expressions, so that we do not get
902 -- multiple error messages when several positional parameters
903 -- follow a named parameter.
907 -- End of treatment for name extensions starting with left paren
909 -- End of loop through name extensions
913 -- This function parses a restricted form of Names which are either
914 -- designators, or designators preceded by a sequence of prefixes
915 -- that are direct names.
917 -- Error recovery: cannot raise Error_Resync
919 function P_Function_Name
return Node_Id
is
920 Designator_Node
: Node_Id
;
921 Prefix_Node
: Node_Id
;
922 Selector_Node
: Node_Id
;
923 Dot_Sloc
: Source_Ptr
:= No_Location
;
926 -- Prefix_Node is set to the gathered prefix so far, Empty means that
927 -- no prefix has been scanned. This allows us to build up the result
928 -- in the required right recursive manner.
930 Prefix_Node
:= Empty
;
932 -- Loop through prefixes
935 Designator_Node
:= Token_Node
;
937 if Token
not in Token_Class_Desig
then
938 return P_Identifier
; -- let P_Identifier issue the error message
940 else -- Token in Token_Class_Desig
941 Scan
; -- past designator
942 exit when Token
/= Tok_Dot
;
945 -- Here at a dot, with token just before it in Designator_Node
947 if No
(Prefix_Node
) then
948 Prefix_Node
:= Designator_Node
;
950 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
951 Set_Prefix
(Selector_Node
, Prefix_Node
);
952 Set_Selector_Name
(Selector_Node
, Designator_Node
);
953 Prefix_Node
:= Selector_Node
;
956 Dot_Sloc
:= Token_Ptr
;
960 -- Fall out of the loop having just scanned a designator
962 if No
(Prefix_Node
) then
963 return Designator_Node
;
965 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
966 Set_Prefix
(Selector_Node
, Prefix_Node
);
967 Set_Selector_Name
(Selector_Node
, Designator_Node
);
968 return Selector_Node
;
976 -- This function parses a restricted form of Names which are either
977 -- identifiers, or identifiers preceded by a sequence of prefixes
978 -- that are direct names.
980 -- Error recovery: cannot raise Error_Resync
982 function P_Qualified_Simple_Name
return Node_Id
is
983 Designator_Node
: Node_Id
;
984 Prefix_Node
: Node_Id
;
985 Selector_Node
: Node_Id
;
986 Dot_Sloc
: Source_Ptr
:= No_Location
;
989 -- Prefix node is set to the gathered prefix so far, Empty means that
990 -- no prefix has been scanned. This allows us to build up the result
991 -- in the required right recursive manner.
993 Prefix_Node
:= Empty
;
995 -- Loop through prefixes
998 Designator_Node
:= Token_Node
;
1000 if Token
= Tok_Identifier
then
1001 Scan
; -- past identifier
1002 exit when Token
/= Tok_Dot
;
1004 elsif Token
not in Token_Class_Desig
then
1005 return P_Identifier
; -- let P_Identifier issue the error message
1008 Scan
; -- past designator
1010 if Token
/= Tok_Dot
then
1011 Error_Msg_SP
("identifier expected");
1016 -- Here at a dot, with token just before it in Designator_Node
1018 if No
(Prefix_Node
) then
1019 Prefix_Node
:= Designator_Node
;
1021 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1022 Set_Prefix
(Selector_Node
, Prefix_Node
);
1023 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1024 Prefix_Node
:= Selector_Node
;
1027 Dot_Sloc
:= Token_Ptr
;
1031 -- Fall out of the loop having just scanned an identifier
1033 if No
(Prefix_Node
) then
1034 return Designator_Node
;
1036 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1037 Set_Prefix
(Selector_Node
, Prefix_Node
);
1038 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1039 return Selector_Node
;
1043 when Error_Resync
=>
1045 end P_Qualified_Simple_Name
;
1047 -- This procedure differs from P_Qualified_Simple_Name only in that it
1048 -- raises Error_Resync if any error is encountered. It only returns after
1049 -- scanning a valid qualified simple name.
1051 -- Error recovery: can raise Error_Resync
1053 function P_Qualified_Simple_Name_Resync
return Node_Id
is
1054 Designator_Node
: Node_Id
;
1055 Prefix_Node
: Node_Id
;
1056 Selector_Node
: Node_Id
;
1057 Dot_Sloc
: Source_Ptr
:= No_Location
;
1060 Prefix_Node
:= Empty
;
1062 -- Loop through prefixes
1065 Designator_Node
:= Token_Node
;
1067 if Token
= Tok_Identifier
then
1068 Scan
; -- past identifier
1069 exit when Token
/= Tok_Dot
;
1071 elsif Token
not in Token_Class_Desig
then
1072 Discard_Junk_Node
(P_Identifier
); -- to issue the error message
1076 Scan
; -- past designator
1078 if Token
/= Tok_Dot
then
1079 Error_Msg_SP
("identifier expected");
1084 -- Here at a dot, with token just before it in Designator_Node
1086 if No
(Prefix_Node
) then
1087 Prefix_Node
:= Designator_Node
;
1089 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1090 Set_Prefix
(Selector_Node
, Prefix_Node
);
1091 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1092 Prefix_Node
:= Selector_Node
;
1095 Dot_Sloc
:= Token_Ptr
;
1096 Scan
; -- past period
1099 -- Fall out of the loop having just scanned an identifier
1101 if No
(Prefix_Node
) then
1102 return Designator_Node
;
1104 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1105 Set_Prefix
(Selector_Node
, Prefix_Node
);
1106 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1107 return Selector_Node
;
1109 end P_Qualified_Simple_Name_Resync
;
1111 ----------------------
1112 -- 4.1 Direct_Name --
1113 ----------------------
1115 -- Parsed by P_Name and other functions in section 4.1
1121 -- Parsed by P_Name (4.1)
1123 -------------------------------
1124 -- 4.1 Explicit Dereference --
1125 -------------------------------
1127 -- Parsed by P_Name (4.1)
1129 -------------------------------
1130 -- 4.1 Implicit_Dereference --
1131 -------------------------------
1133 -- Parsed by P_Name (4.1)
1135 ----------------------------
1136 -- 4.1 Indexed Component --
1137 ----------------------------
1139 -- Parsed by P_Name (4.1)
1145 -- Parsed by P_Name (4.1)
1147 -----------------------------
1148 -- 4.1 Selected_Component --
1149 -----------------------------
1151 -- Parsed by P_Name (4.1)
1153 ------------------------
1154 -- 4.1 Selector Name --
1155 ------------------------
1157 -- Parsed by P_Name (4.1)
1159 ------------------------------
1160 -- 4.1 Attribute Reference --
1161 ------------------------------
1163 -- Parsed by P_Name (4.1)
1165 -------------------------------
1166 -- 4.1 Attribute Designator --
1167 -------------------------------
1169 -- Parsed by P_Name (4.1)
1171 --------------------------------------
1172 -- 4.1.4 Range Attribute Reference --
1173 --------------------------------------
1175 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1177 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1179 -- In the grammar, a RANGE attribute is simply a name, but its use is
1180 -- highly restricted, so in the parser, we do not regard it as a name.
1181 -- Instead, P_Name returns without scanning the 'RANGE part of the
1182 -- attribute, and the caller uses the following function to construct
1183 -- a range attribute in places where it is appropriate.
1185 -- Note that RANGE here is treated essentially as an identifier,
1186 -- rather than a reserved word.
1188 -- The caller has parsed the prefix, i.e. a name, and Token points to
1189 -- the apostrophe. The token after the apostrophe is known to be RANGE
1190 -- at this point. The prefix node becomes the prefix of the attribute.
1192 -- Error_Recovery: Cannot raise Error_Resync
1194 function P_Range_Attribute_Reference
1195 (Prefix_Node
: Node_Id
)
1198 Attr_Node
: Node_Id
;
1201 Attr_Node
:= New_Node
(N_Attribute_Reference
, Token_Ptr
);
1202 Set_Prefix
(Attr_Node
, Prefix_Node
);
1203 Scan
; -- past apostrophe
1206 Style
.Check_Attribute_Name
(True);
1209 Set_Attribute_Name
(Attr_Node
, Name_Range
);
1212 if Token
= Tok_Left_Paren
then
1213 Scan
; -- past left paren
1214 Set_Expressions
(Attr_Node
, New_List
(P_Expression_If_OK
));
1219 end P_Range_Attribute_Reference
;
1221 -------------------------------------
1222 -- P_Reduction_Attribute_Reference --
1223 -------------------------------------
1225 function P_Reduction_Attribute_Reference
(S
: Node_Id
)
1228 Attr_Node
: Node_Id
;
1229 Attr_Name
: Name_Id
;
1232 Attr_Name
:= Token_Name
;
1233 Scan
; -- past Reduce
1234 Attr_Node
:= New_Node
(N_Attribute_Reference
, Token_Ptr
);
1235 Set_Attribute_Name
(Attr_Node
, Attr_Name
);
1236 if Attr_Name
/= Name_Reduce
then
1237 Error_Msg
("Reduce attribute expected", Prev_Token_Ptr
);
1240 Set_Prefix
(Attr_Node
, S
);
1241 Set_Expressions
(Attr_Node
, New_List
);
1243 Append
(P_Name
, Expressions
(Attr_Node
));
1245 Append
(P_Expression
, Expressions
(Attr_Node
));
1249 end P_Reduction_Attribute_Reference
;
1251 ---------------------------------------
1252 -- 4.1.4 Range Attribute Designator --
1253 ---------------------------------------
1255 -- Parsed by P_Range_Attribute_Reference (4.4)
1257 ---------------------------------------------
1258 -- 4.1.4 (2) Reduction_Attribute_Reference --
1259 ---------------------------------------------
1261 -- parsed by P_Reduction_Attribute_Reference
1263 --------------------
1265 --------------------
1267 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1269 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1270 -- an aggregate is known to be required (code statement, extension
1271 -- aggregate), in which cases this routine performs the necessary check
1272 -- that we have an aggregate rather than a parenthesized expression
1274 -- Error recovery: can raise Error_Resync
1276 function P_Aggregate
return Node_Id
is
1277 Aggr_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1278 Aggr_Node
: constant Node_Id
:= P_Aggregate_Or_Paren_Expr
;
1281 if Nkind
(Aggr_Node
) /= N_Aggregate
1283 Nkind
(Aggr_Node
) /= N_Extension_Aggregate
1284 and then Ada_Version
< Ada_2022
1287 ("aggregate may not have single positional component", Aggr_Sloc
);
1294 ------------------------------------------------
1295 -- 4.3 Aggregate or Parenthesized Expression --
1296 ------------------------------------------------
1298 -- This procedure parses out either an aggregate or a parenthesized
1299 -- expression (these two constructs are closely related, since a
1300 -- parenthesized expression looks like an aggregate with a single
1301 -- positional component).
1304 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1306 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1308 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1309 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1312 -- RECORD_COMPONENT_ASSOCIATION ::=
1313 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1315 -- COMPONENT_CHOICE_LIST ::=
1316 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1319 -- EXTENSION_AGGREGATE ::=
1320 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1322 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1324 -- ARRAY_AGGREGATE ::=
1325 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1327 -- POSITIONAL_ARRAY_AGGREGATE ::=
1328 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1329 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1330 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1332 -- NAMED_ARRAY_AGGREGATE ::=
1333 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1335 -- PRIMARY ::= (EXPRESSION);
1337 -- Error recovery: can raise Error_Resync
1339 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1340 -- to Ada 2005 limited aggregates (AI-287)
1342 function P_Aggregate_Or_Paren_Expr
return Node_Id
is
1343 Aggregate_Node
: Node_Id
;
1344 Expr_List
: List_Id
;
1345 Assoc_List
: List_Id
;
1346 Expr_Node
: Node_Id
;
1347 Lparen_Sloc
: Source_Ptr
;
1348 Scan_State
: Saved_Scan_State
;
1350 procedure Box_Error
;
1351 -- Called if <> is encountered as positional aggregate element. Issues
1352 -- error message and sets Expr_Node to Error.
1354 function Is_Quantified_Expression
return Boolean;
1355 -- The presence of iterated component associations requires a one
1356 -- token lookahead to distinguish it from quantified expressions.
1362 procedure Box_Error
is
1364 Error_Msg_Ada_2005_Extension
("'<'> in aggregate");
1366 -- Ada 2005 (AI-287): The box notation is allowed only with named
1367 -- notation because positional notation might be error prone. For
1368 -- example, in "(X, <>, Y, <>)", there is no type associated with
1369 -- the boxes, so you might not be leaving out the components you
1370 -- thought you were leaving out.
1372 Error_Msg_SC
("(Ada 2005) box only allowed with named notation");
1377 ------------------------------
1378 -- Is_Quantified_Expression --
1379 ------------------------------
1381 function Is_Quantified_Expression
return Boolean is
1383 Scan_State
: Saved_Scan_State
;
1386 Save_Scan_State
(Scan_State
);
1388 Maybe
:= Token
in Tok_All | Tok_Some
;
1389 Restore_Scan_State
(Scan_State
); -- to FOR
1391 end Is_Quantified_Expression
;
1393 Start_Token
: constant Token_Type
:= Token
;
1394 -- Used to prevent mismatches (...] and [...)
1396 -- Start of processing for P_Aggregate_Or_Paren_Expr
1399 Lparen_Sloc
:= Token_Ptr
;
1400 if Token
= Tok_Left_Bracket
then
1403 -- Special case for null aggregate in Ada 2022
1405 if Token
= Tok_Right_Bracket
then
1407 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1408 Set_Expressions
(Aggregate_Node
, New_List
);
1409 Set_Component_Associations
(Aggregate_Node
, New_List
);
1410 Set_Is_Homogeneous_Aggregate
(Aggregate_Node
);
1411 return Aggregate_Node
;
1417 -- Note on parentheses count. For cases like an if expression, the
1418 -- parens here really count as real parentheses for the paren count,
1419 -- so we adjust the paren count accordingly after scanning the expr.
1423 if Token
= Tok_If
then
1424 Expr_Node
:= P_If_Expression
;
1426 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1431 elsif Token
= Tok_Case
then
1432 Expr_Node
:= P_Case_Expression
;
1434 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1437 -- Quantified expression
1439 elsif Token
= Tok_For
and then Is_Quantified_Expression
then
1440 Expr_Node
:= P_Quantified_Expression
;
1442 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1445 -- Note: the mechanism used here of rescanning the initial expression
1446 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1447 -- out the discrete choice list.
1449 -- Deal with expression and extension aggregates first
1451 elsif Token
/= Tok_Others
then
1452 Save_Scan_State
(Scan_State
); -- at start of expression
1454 -- Deal with (NULL RECORD)
1456 if Token
= Tok_Null
then
1459 if Token
= Tok_Record
then
1460 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1461 Set_Null_Record_Present
(Aggregate_Node
, True);
1462 Scan
; -- past RECORD
1464 return Aggregate_Node
;
1466 Restore_Scan_State
(Scan_State
); -- to NULL that must be expr
1469 elsif Token
= Tok_For
then
1470 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1471 Expr_Node
:= P_Iterated_Component_Association
;
1475 -- Scan expression, handling box appearing as positional argument
1477 if Token
= Tok_Box
then
1480 Expr_Node
:= P_Expression_Or_Range_Attribute_If_OK
;
1483 -- Extension or Delta aggregate
1485 if Token
= Tok_With
then
1486 if Nkind
(Expr_Node
) = N_Attribute_Reference
1487 and then Attribute_Name
(Expr_Node
) = Name_Range
1489 Bad_Range_Attribute
(Sloc
(Expr_Node
));
1493 if Ada_Version
= Ada_83
then
1494 Error_Msg_SC
("(Ada 83) extension aggregate not allowed");
1498 if Token
= Tok_Delta
then
1500 Aggregate_Node
:= New_Node
(N_Delta_Aggregate
, Lparen_Sloc
);
1501 Set_Expression
(Aggregate_Node
, Expr_Node
);
1507 Aggregate_Node
:= New_Node
(N_Extension_Aggregate
, Lparen_Sloc
);
1508 Set_Ancestor_Part
(Aggregate_Node
, Expr_Node
);
1511 -- Deal with WITH NULL RECORD case
1513 if Token
= Tok_Null
then
1514 Save_Scan_State
(Scan_State
); -- at NULL
1517 if Token
= Tok_Record
then
1518 Scan
; -- past RECORD
1519 Set_Null_Record_Present
(Aggregate_Node
, True);
1521 return Aggregate_Node
;
1524 Restore_Scan_State
(Scan_State
); -- to NULL that must be expr
1528 if Token
/= Tok_Others
then
1529 Save_Scan_State
(Scan_State
);
1530 Expr_Node
:= P_Expression
;
1537 elsif Token
= Tok_Right_Paren
or else Token
in Token_Class_Eterm
then
1538 if Nkind
(Expr_Node
) = N_Attribute_Reference
1539 and then Attribute_Name
(Expr_Node
) = Name_Range
1542 ("|parentheses not allowed for range attribute", Lparen_Sloc
);
1543 Scan
; -- past right paren
1547 -- Bump paren count of expression
1549 if Expr_Node
/= Error
then
1550 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1553 T_Right_Paren
; -- past right paren (error message if none)
1559 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1565 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1569 -- Prepare to scan list of component associations
1571 Expr_List
:= No_List
; -- don't set yet, maybe all named entries
1572 Assoc_List
:= No_List
; -- don't set yet, maybe all positional entries
1574 -- This loop scans through component associations. On entry to the
1575 -- loop, an expression has been scanned at the start of the current
1576 -- association unless initial token was OTHERS, in which case
1577 -- Expr_Node is set to Empty.
1580 -- Deal with others association first. This is a named association
1582 if No
(Expr_Node
) then
1583 Append_New
(P_Record_Or_Array_Component_Association
, Assoc_List
);
1585 -- Improper use of WITH
1587 elsif Token
= Tok_With
then
1588 Error_Msg_SC
("WITH must be preceded by single expression in " &
1589 "extension aggregate");
1592 -- Range attribute can only appear as part of a discrete choice list
1594 elsif Nkind
(Expr_Node
) = N_Attribute_Reference
1595 and then Attribute_Name
(Expr_Node
) = Name_Range
1596 and then Token
/= Tok_Arrow
1597 and then Token
/= Tok_Vertical_Bar
1599 Bad_Range_Attribute
(Sloc
(Expr_Node
));
1602 -- Assume positional case if comma, right paren, or literal or
1603 -- identifier or OTHERS follows (the latter cases are missing
1604 -- comma cases). Also assume positional if a semicolon follows,
1605 -- which can happen if there are missing parens.
1606 -- In Ada 2012 and 2022 an iterated association can appear.
1608 elsif Nkind
(Expr_Node
) in
1609 N_Iterated_Component_Association | N_Iterated_Element_Association
1611 Append_New
(Expr_Node
, Assoc_List
);
1613 elsif Token
in Tok_Comma | Tok_Right_Paren | Tok_Others
1614 | Token_Class_Lit_Or_Name | Tok_Semicolon
1616 if Present
(Assoc_List
) then
1617 Error_Msg_BC
-- CODEFIX
1618 ("""='>"" expected (positional association cannot follow "
1619 & "named association)");
1622 Append_New
(Expr_Node
, Expr_List
);
1624 -- Check for aggregate followed by left parent, maybe missing comma
1626 elsif Nkind
(Expr_Node
) = N_Aggregate
1627 and then Token
= Tok_Left_Paren
1631 Append_New
(Expr_Node
, Expr_List
);
1633 elsif Token
= Tok_Right_Bracket
then
1634 Append_New
(Expr_Node
, Expr_List
);
1637 -- Anything else is assumed to be a named association
1640 Restore_Scan_State
(Scan_State
); -- to start of expression
1642 Append_New
(P_Record_Or_Array_Component_Association
, Assoc_List
);
1645 exit when not Comma_Present
;
1647 -- If we are at an expression terminator, something is seriously
1648 -- wrong, so let's get out now, before we start eating up stuff
1649 -- that doesn't belong to us.
1651 if Token
in Token_Class_Eterm
and then Token
/= Tok_For
then
1653 ("expecting expression or component association");
1657 -- Deal with misused box
1659 if Token
= Tok_Box
then
1662 -- Otherwise initiate for reentry to top of loop by scanning an
1663 -- initial expression, unless the first token is OTHERS or FOR,
1664 -- which indicates an iterated component association.
1666 elsif Token
= Tok_Others
then
1669 elsif Token
= Tok_For
then
1670 Expr_Node
:= P_Iterated_Component_Association
;
1673 Save_Scan_State
(Scan_State
); -- at start of expression
1674 Expr_Node
:= P_Expression_Or_Range_Attribute_If_OK
;
1679 -- All component associations (positional and named) have been scanned.
1680 -- Scan ] or ) based on Start_Token.
1683 when Tok_Left_Bracket
=>
1685 Set_Component_Associations
(Aggregate_Node
, Assoc_List
);
1686 Set_Is_Homogeneous_Aggregate
(Aggregate_Node
);
1689 if Token
= Tok_Apostrophe
then
1692 if Token
= Tok_Identifier
then
1693 return P_Reduction_Attribute_Reference
(Aggregate_Node
);
1696 when Tok_Left_Paren
=>
1697 if Nkind
(Aggregate_Node
) = N_Aggregate
then
1698 Set_Is_Parenthesis_Aggregate
(Aggregate_Node
);
1702 when others => raise Program_Error
;
1705 if Nkind
(Aggregate_Node
) /= N_Delta_Aggregate
then
1706 Set_Expressions
(Aggregate_Node
, Expr_List
);
1709 Set_Component_Associations
(Aggregate_Node
, Assoc_List
);
1710 return Aggregate_Node
;
1711 end P_Aggregate_Or_Paren_Expr
;
1713 ------------------------------------------------
1714 -- 4.3 Record or Array Component Association --
1715 ------------------------------------------------
1717 -- RECORD_COMPONENT_ASSOCIATION ::=
1718 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1719 -- | COMPONENT_CHOICE_LIST => <>
1721 -- COMPONENT_CHOICE_LIST =>
1722 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1725 -- ARRAY_COMPONENT_ASSOCIATION ::=
1726 -- DISCRETE_CHOICE_LIST => EXPRESSION
1727 -- | DISCRETE_CHOICE_LIST => <>
1728 -- | ITERATED_COMPONENT_ASSOCIATION
1730 -- Note: this routine only handles the named cases, including others.
1731 -- Cases where the component choice list is not present have already
1732 -- been handled directly.
1734 -- Error recovery: can raise Error_Resync
1736 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1737 -- rules have been extended to give support to Ada 2005 limited
1738 -- aggregates (AI-287)
1740 function P_Record_Or_Array_Component_Association
return Node_Id
is
1741 Assoc_Node
: Node_Id
;
1742 Box_Present
: Boolean := False;
1743 Box_With_Identifier_Present
: Boolean := False;
1745 -- A loop indicates an iterated_component_association
1747 if Token
= Tok_For
then
1748 return P_Iterated_Component_Association
;
1751 Assoc_Node
:= New_Node
(N_Component_Association
, Token_Ptr
);
1752 Set_Binding_Chars
(Assoc_Node
, No_Name
);
1754 Set_Choices
(Assoc_Node
, P_Discrete_Choice_List
);
1755 Set_Sloc
(Assoc_Node
, Token_Ptr
);
1758 if Token
= Tok_Box
then
1760 -- Ada 2005(AI-287): The box notation is used to indicate the
1761 -- default initialization of aggregate components
1763 Error_Msg_Ada_2005_Extension
("component association with '<'>");
1765 Box_Present
:= True;
1766 Set_Box_Present
(Assoc_Node
);
1768 elsif Token
= Tok_Less
then
1770 Scan_State
: Saved_Scan_State
;
1773 Save_Scan_State
(Scan_State
);
1775 if Token
= Tok_Identifier
then
1776 Id
:= P_Defining_Identifier
;
1777 if Token
= Tok_Greater
then
1778 if Core_Extensions_Allowed
then
1779 Set_Box_Present
(Assoc_Node
);
1780 Set_Binding_Chars
(Assoc_Node
, Chars
(Id
));
1781 Box_Present
:= True;
1782 Box_With_Identifier_Present
:= True;
1785 Error_Msg_GNAT_Extension
1786 ("identifier within box", Token_Ptr
);
1787 Box_Present
:= True;
1788 -- Avoid cascading errors by ignoring the identifier
1792 if not Box_Present
then
1793 -- it wasn't an "is <identifier>", so restore.
1794 Restore_Scan_State
(Scan_State
);
1799 if not Box_Present
then
1800 Set_Expression
(Assoc_Node
, P_Expression
);
1803 -- Check for "is <identifier>" for aggregate that is part of
1804 -- a pattern for a general case statement.
1806 if Token
= Tok_Is
then
1808 Scan_State
: Saved_Scan_State
;
1811 Save_Scan_State
(Scan_State
);
1813 if Token
= Tok_Identifier
then
1814 Id
:= P_Defining_Identifier
;
1816 if not Core_Extensions_Allowed
then
1817 Error_Msg_GNAT_Extension
1818 ("IS following component association", Token_Ptr
);
1819 elsif Box_With_Identifier_Present
then
1821 ("Both identifier-in-box and trailing identifier"
1822 & " specified for one component association",
1825 Set_Binding_Chars
(Assoc_Node
, Chars
(Id
));
1828 -- It wasn't an "is <identifier>", so restore.
1829 Restore_Scan_State
(Scan_State
);
1835 end P_Record_Or_Array_Component_Association
;
1837 -----------------------------
1838 -- 4.3.1 Record Aggregate --
1839 -----------------------------
1841 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1842 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1844 ----------------------------------------------
1845 -- 4.3.1 Record Component Association List --
1846 ----------------------------------------------
1848 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1850 ----------------------------------
1851 -- 4.3.1 Component Choice List --
1852 ----------------------------------
1854 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1856 --------------------------------
1857 -- 4.3.1 Extension Aggregate --
1858 --------------------------------
1860 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1862 --------------------------
1863 -- 4.3.1 Ancestor Part --
1864 --------------------------
1866 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1868 ----------------------------
1869 -- 4.3.1 Array Aggregate --
1870 ----------------------------
1872 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1874 ---------------------------------------
1875 -- 4.3.1 Positional Array Aggregate --
1876 ---------------------------------------
1878 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1880 ----------------------------------
1881 -- 4.3.1 Named Array Aggregate --
1882 ----------------------------------
1884 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1886 ----------------------------------------
1887 -- 4.3.1 Array Component Association --
1888 ----------------------------------------
1890 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1892 ---------------------
1893 -- 4.4 Expression --
1894 ---------------------
1896 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1899 -- RELATION {LOGICAL_OPERATOR RELATION}
1901 -- CHOICE_EXPRESSION ::=
1902 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1904 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1906 -- On return, Expr_Form indicates the categorization of the expression
1907 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1908 -- an error message is given, and Error is returned).
1910 -- Error recovery: cannot raise Error_Resync
1912 function P_Expression
return Node_Id
is
1913 Logical_Op
: Node_Kind
;
1914 Prev_Logical_Op
: Node_Kind
;
1915 Op_Location
: Source_Ptr
;
1920 Node1
:= P_Relation
;
1922 if Token
in Token_Class_Logop
then
1923 Prev_Logical_Op
:= N_Empty
;
1926 Op_Location
:= Token_Ptr
;
1927 Logical_Op
:= P_Logical_Operator
;
1929 if Prev_Logical_Op
/= N_Empty
and then
1930 Logical_Op
/= Prev_Logical_Op
1933 ("mixed logical operators in expression", Op_Location
);
1934 Prev_Logical_Op
:= N_Empty
;
1936 Prev_Logical_Op
:= Logical_Op
;
1940 Node1
:= New_Op_Node
(Logical_Op
, Op_Location
);
1941 Set_Left_Opnd
(Node1
, Node2
);
1942 Set_Right_Opnd
(Node1
, P_Relation
);
1944 -- Check for case of errant comma or semicolon
1946 if Token
in Tok_Comma | Tok_Semicolon
then
1948 Com
: constant Boolean := Token
= Tok_Comma
;
1949 Scan_State
: Saved_Scan_State
;
1953 Save_Scan_State
(Scan_State
); -- at comma/semicolon
1954 Scan
; -- past comma/semicolon
1956 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1957 -- do not deal with AND/OR because those cases get mixed up
1958 -- with the select alternatives case.
1960 if Token
in Tok_And | Tok_Or
then
1961 Logop
:= P_Logical_Operator
;
1962 Restore_Scan_State
(Scan_State
); -- to comma/semicolon
1964 if Logop
in N_And_Then | N_Or_Else
then
1965 Scan
; -- past comma/semicolon
1968 Error_Msg_SP
-- CODEFIX
1969 ("|extra "","" ignored");
1971 Error_Msg_SP
-- CODEFIX
1972 ("|extra "";"" ignored");
1976 Restore_Scan_State
(Scan_State
); -- to comma/semicolon
1980 Restore_Scan_State
(Scan_State
); -- to comma/semicolon
1985 exit when Token
not in Token_Class_Logop
;
1988 Expr_Form
:= EF_Non_Simple
;
1991 if Token
= Tok_Apostrophe
then
1992 Bad_Range_Attribute
(Token_Ptr
);
1999 -- This function is identical to the normal P_Expression, except that it
2000 -- also permits the appearance of a case, conditional, or quantified
2001 -- expression if the call immediately follows a left paren, and followed
2002 -- by a right parenthesis. These forms are allowed if these conditions
2003 -- are not met, but an error message will be issued.
2005 function P_Expression_If_OK
return Node_Id
is
2007 -- Case of conditional, case or quantified expression
2009 if Token
in Tok_Case | Tok_If | Tok_For | Tok_Declare
then
2010 return P_Unparen_Cond_Expr_Etc
;
2012 -- Normal case, not case/conditional/quantified expression
2015 return P_Expression
;
2017 end P_Expression_If_OK
;
2019 -- This function is identical to the normal P_Expression, except that it
2020 -- checks that the expression scan did not stop on a right paren. It is
2021 -- called in all contexts where a right parenthesis cannot legitimately
2022 -- follow an expression.
2024 -- Error recovery: cannot raise Error_Resync
2026 function P_Expression_No_Right_Paren
return Node_Id
is
2027 Expr
: constant Node_Id
:= P_Expression
;
2029 Ignore
(Tok_Right_Paren
);
2031 end P_Expression_No_Right_Paren
;
2033 ----------------------------------------
2034 -- 4.4 Expression_Or_Range_Attribute --
2035 ----------------------------------------
2038 -- RELATION {and RELATION} | RELATION {and then RELATION}
2039 -- | RELATION {or RELATION} | RELATION {or else RELATION}
2040 -- | RELATION {xor RELATION}
2042 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2044 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2046 -- On return, Expr_Form indicates the categorization of the expression
2047 -- and EF_Range_Attr is one of the possibilities.
2049 -- Error recovery: cannot raise Error_Resync
2051 -- In the grammar, a RANGE attribute is simply a name, but its use is
2052 -- highly restricted, so in the parser, we do not regard it as a name.
2053 -- Instead, P_Name returns without scanning the 'RANGE part of the
2054 -- attribute, and P_Expression_Or_Range_Attribute handles the range
2055 -- attribute reference. In the normal case where a range attribute is
2056 -- not allowed, an error message is issued by P_Expression.
2058 function P_Expression_Or_Range_Attribute
return Node_Id
is
2059 Logical_Op
: Node_Kind
;
2060 Prev_Logical_Op
: Node_Kind
;
2061 Op_Location
: Source_Ptr
;
2064 Attr_Node
: Node_Id
;
2067 Node1
:= P_Relation
;
2069 if Token
= Tok_Apostrophe
then
2070 Attr_Node
:= P_Range_Attribute_Reference
(Node1
);
2071 Expr_Form
:= EF_Range_Attr
;
2074 elsif Token
in Token_Class_Logop
then
2075 Prev_Logical_Op
:= N_Empty
;
2078 Op_Location
:= Token_Ptr
;
2079 Logical_Op
:= P_Logical_Operator
;
2081 if Prev_Logical_Op
/= N_Empty
and then
2082 Logical_Op
/= Prev_Logical_Op
2085 ("mixed logical operators in expression", Op_Location
);
2086 Prev_Logical_Op
:= N_Empty
;
2088 Prev_Logical_Op
:= Logical_Op
;
2092 Node1
:= New_Op_Node
(Logical_Op
, Op_Location
);
2093 Set_Left_Opnd
(Node1
, Node2
);
2094 Set_Right_Opnd
(Node1
, P_Relation
);
2095 exit when Token
not in Token_Class_Logop
;
2098 Expr_Form
:= EF_Non_Simple
;
2101 if Token
= Tok_Apostrophe
then
2102 Bad_Range_Attribute
(Token_Ptr
);
2107 end P_Expression_Or_Range_Attribute
;
2109 -- Version that allows a non-parenthesized case, conditional, or quantified
2110 -- expression if the call immediately follows a left paren, and followed
2111 -- by a right parenthesis. These forms are allowed if these conditions
2112 -- are not met, but an error message will be issued.
2114 function P_Expression_Or_Range_Attribute_If_OK
return Node_Id
is
2116 -- Case of conditional, case or quantified expression
2118 if Token
in Tok_Case | Tok_If | Tok_For | Tok_Declare
then
2119 return P_Unparen_Cond_Expr_Etc
;
2121 -- Normal case, not one of the above expression types
2124 return P_Expression_Or_Range_Attribute
;
2126 end P_Expression_Or_Range_Attribute_If_OK
;
2132 -- This procedure scans both relations and choice relations
2134 -- CHOICE_RELATION ::=
2135 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
2138 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
2139 -- | RAISE_EXPRESSION
2141 -- MEMBERSHIP_CHOICE_LIST ::=
2142 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
2144 -- MEMBERSHIP_CHOICE ::=
2145 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
2147 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
2149 -- On return, Expr_Form indicates the categorization of the expression
2151 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2152 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2154 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2155 -- expression, then tokens are scanned until either a non-expression token,
2156 -- a right paren (not matched by a left paren) or a comma, is encountered.
2158 function P_Relation
return Node_Id
is
2159 Node1
, Node2
: Node_Id
;
2163 -- First check for raise expression
2165 if Token
= Tok_Raise
then
2166 Expr_Form
:= EF_Non_Simple
;
2167 return P_Raise_Expression
;
2172 Node1
:= P_Simple_Expression
;
2174 if Token
not in Token_Class_Relop
then
2178 -- Here we have a relational operator following. If so then scan it
2179 -- out. Note that the assignment symbol := is treated as a relational
2180 -- operator to improve the error recovery when it is misused for =.
2181 -- P_Relational_Operator also parses the IN and NOT IN operations.
2184 Node2
:= New_Op_Node
(P_Relational_Operator
, Optok
);
2185 Set_Left_Opnd
(Node2
, Node1
);
2187 -- Case of IN or NOT IN
2189 if Prev_Token
= Tok_In
then
2190 P_Membership_Test
(Node2
);
2192 -- Case of relational operator (= /= < <= > >=)
2195 Set_Right_Opnd
(Node2
, P_Simple_Expression
);
2198 Expr_Form
:= EF_Non_Simple
;
2200 if Token
in Token_Class_Relop
then
2201 Error_Msg_SC
("unexpected relational operator");
2208 -- If any error occurs, then scan to the next expression terminator symbol
2209 -- or comma or right paren at the outer (i.e. current) parentheses level.
2210 -- The flags are set to indicate a normal simple expression.
2213 when Error_Resync
=>
2215 Expr_Form
:= EF_Simple
;
2219 ----------------------------
2220 -- 4.4 Simple Expression --
2221 ----------------------------
2223 -- SIMPLE_EXPRESSION ::=
2224 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2226 -- On return, Expr_Form indicates the categorization of the expression
2228 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2229 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2231 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2232 -- expression, then tokens are scanned until either a non-expression token,
2233 -- a right paren (not matched by a left paren) or a comma, is encountered.
2235 -- Note: P_Simple_Expression is called only internally by higher level
2236 -- expression routines. In cases in the grammar where a simple expression
2237 -- is required, the approach is to scan an expression, and then post an
2238 -- appropriate error message if the expression obtained is not simple. This
2239 -- gives better error recovery and treatment.
2241 function P_Simple_Expression
return Node_Id
is
2242 Scan_State
: Saved_Scan_State
;
2245 Tokptr
: Source_Ptr
;
2247 function At_Start_Of_Attribute
return Boolean;
2248 -- Tests if we have quote followed by attribute name, if so, return True
2249 -- otherwise return False.
2251 ---------------------------
2252 -- At_Start_Of_Attribute --
2253 ---------------------------
2255 function At_Start_Of_Attribute
return Boolean is
2257 if Token
/= Tok_Apostrophe
then
2262 Scan_State
: Saved_Scan_State
;
2265 Save_Scan_State
(Scan_State
);
2268 if Token
= Tok_Identifier
2269 and then Is_Attribute_Name
(Chars
(Token_Node
))
2271 Restore_Scan_State
(Scan_State
);
2274 Restore_Scan_State
(Scan_State
);
2279 end At_Start_Of_Attribute
;
2281 -- Start of processing for P_Simple_Expression
2284 -- Check for cases starting with a name. There are two reasons for
2285 -- special casing. First speed things up by catching a common case
2286 -- without going through several routine layers. Second the caller must
2287 -- be informed via Expr_Form when the simple expression is a name.
2289 if Token
in Token_Class_Name
then
2292 -- Deal with apostrophe cases
2294 if Token
= Tok_Apostrophe
then
2295 Save_Scan_State
(Scan_State
); -- at apostrophe
2296 Scan
; -- past apostrophe
2298 -- If qualified expression, scan it out and fall through
2300 if Token
= Tok_Left_Paren
then
2301 Node1
:= P_Qualified_Expression
(Node1
);
2302 Expr_Form
:= EF_Simple
;
2304 -- If range attribute, then we return with Token pointing to the
2305 -- apostrophe. Note: avoid the normal error check on exit. We
2306 -- know that the expression really is complete in this case.
2308 else -- Token = Tok_Range then
2309 Restore_Scan_State
(Scan_State
); -- to apostrophe
2310 Expr_Form
:= EF_Simple_Name
;
2315 -- If an expression terminator follows, the previous processing
2316 -- completely scanned out the expression (a common case), and
2317 -- left Expr_Form set appropriately for returning to our caller.
2319 if Token
in Token_Class_Sterm
then
2322 -- Handle '}' as expression terminator of an interpolated
2325 elsif Inside_Interpolated_String_Literal
2326 and then Token
= Tok_Right_Curly_Bracket
2330 -- If we do not have an expression terminator, then complete the
2331 -- scan of a simple expression. This code duplicates the code
2332 -- found in P_Term and P_Factor.
2335 if Token
= Tok_Double_Asterisk
then
2337 Style
.Check_Exponentiation_Operator
;
2340 Node2
:= New_Op_Node
(N_Op_Expon
, Token_Ptr
);
2342 Set_Left_Opnd
(Node2
, Node1
);
2343 Set_Right_Opnd
(Node2
, P_Primary
);
2349 exit when Token
not in Token_Class_Mulop
;
2350 Tokptr
:= Token_Ptr
;
2351 Node2
:= New_Op_Node
(P_Multiplying_Operator
, Tokptr
);
2354 Style
.Check_Binary_Operator
;
2357 Scan
; -- past operator
2358 Set_Left_Opnd
(Node2
, Node1
);
2359 Set_Right_Opnd
(Node2
, P_Factor
);
2364 exit when Token
not in Token_Class_Binary_Addop
;
2365 Tokptr
:= Token_Ptr
;
2366 Node2
:= New_Op_Node
(P_Binary_Adding_Operator
, Tokptr
);
2369 Style
.Check_Binary_Operator
;
2372 Scan
; -- past operator
2373 Set_Left_Opnd
(Node2
, Node1
);
2374 Set_Right_Opnd
(Node2
, P_Term
);
2378 Expr_Form
:= EF_Simple
;
2381 -- Cases where simple expression does not start with a name
2384 -- Scan initial sign and initial Term
2386 if Token
in Token_Class_Unary_Addop
then
2387 Tokptr
:= Token_Ptr
;
2388 Node1
:= New_Op_Node
(P_Unary_Adding_Operator
, Tokptr
);
2391 Style
.Check_Unary_Plus_Or_Minus
(Inside_Depends
);
2394 Scan
; -- past operator
2395 Set_Right_Opnd
(Node1
, P_Term
);
2400 -- In the following, we special-case a sequence of concatenations of
2401 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2402 -- else mixed in. For such a sequence, we return a tree representing
2403 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2404 -- the number of concatenations is large. If semantic analysis
2405 -- resolves the "&" to a predefined one, then this folding gives the
2406 -- right answer. Otherwise, semantic analysis will complain about a
2407 -- capacity-exceeded error. The purpose of this trick is to avoid
2408 -- creating a deeply nested tree, which would cause deep recursion
2409 -- during semantics, causing stack overflow. This way, we can handle
2410 -- enormous concatenations in the normal case of predefined "&". We
2411 -- first build up the normal tree, and then rewrite it if
2415 Num_Concats_Threshold
: constant Positive := 1000;
2416 -- Arbitrary threshold value to enable optimization
2418 First_Node
: constant Node_Id
:= Node1
;
2419 Is_Strlit_Concat
: Boolean;
2420 -- True iff we've parsed a sequence of concatenations of string
2421 -- literals, with nothing else mixed in.
2423 Num_Concats
: Natural;
2424 -- Number of "&" operators if Is_Strlit_Concat is True
2428 Nkind
(Node1
) = N_String_Literal
2429 and then Token
= Tok_Ampersand
;
2432 -- Scan out sequence of terms separated by binary adding operators
2435 exit when Token
not in Token_Class_Binary_Addop
;
2436 Tokptr
:= Token_Ptr
;
2437 Node2
:= New_Op_Node
(P_Binary_Adding_Operator
, Tokptr
);
2439 if Style_Check
and then not Debug_Flag_Dot_QQ
then
2440 Style
.Check_Binary_Operator
;
2443 Scan
; -- past operator
2444 Set_Left_Opnd
(Node2
, Node1
);
2446 Set_Right_Opnd
(Node2
, Node1
);
2448 -- Check if we're still concatenating string literals
2452 and then Nkind
(Node2
) = N_Op_Concat
2453 and then Nkind
(Node1
) = N_String_Literal
;
2455 if Is_Strlit_Concat
then
2456 Num_Concats
:= Num_Concats
+ 1;
2462 -- If we have an enormous series of concatenations of string
2463 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2464 -- flag tells semantic analysis that if the "&" is not predefined,
2465 -- the folded value is wrong.
2468 and then Num_Concats
>= Num_Concats_Threshold
2471 Strlit_Concat_Val
: String_Id
;
2472 -- Contains the folded value (which will be correct if the
2473 -- "&" operators are the predefined ones).
2476 -- For walking up the tree
2479 -- Folded node to replace Node1
2481 Loc
: constant Source_Ptr
:= Sloc
(First_Node
);
2484 -- Walk up the tree starting at the leftmost string literal
2485 -- (First_Node), building up the Strlit_Concat_Val as we
2486 -- go. Note that we do not use recursion here -- the whole
2487 -- point is to avoid recursively walking that enormous tree.
2490 Store_String_Chars
(Strval
(First_Node
));
2492 Cur_Node
:= Parent
(First_Node
);
2493 while Present
(Cur_Node
) loop
2494 pragma Assert
(Nkind
(Cur_Node
) = N_Op_Concat
and then
2495 Nkind
(Right_Opnd
(Cur_Node
)) = N_String_Literal
);
2497 Store_String_Chars
(Strval
(Right_Opnd
(Cur_Node
)));
2498 Cur_Node
:= Parent
(Cur_Node
);
2501 Strlit_Concat_Val
:= End_String
;
2503 -- Create new folded node, and rewrite result with a concat-
2504 -- enation of an empty string literal and the folded node.
2507 Make_Op_Concat
(Loc
,
2508 Make_String_Literal
(Loc
, Null_String_Id
),
2509 Make_String_Literal
(Loc
, Strlit_Concat_Val
,
2510 Is_Folded_In_Parser
=> True));
2511 Rewrite
(Node1
, New_Node
);
2516 -- All done, we clearly do not have name or numeric literal so this
2517 -- is a case of a simple expression which is some other possibility.
2519 Expr_Form
:= EF_Simple
;
2522 -- Come here at end of simple expression, where we do a couple of
2523 -- special checks to improve error recovery.
2525 -- Special test to improve error recovery. If the current token is a
2526 -- period, then someone is trying to do selection on something that is
2527 -- not a name, e.g. a qualified expression.
2529 if Token
= Tok_Dot
then
2530 Error_Msg_SC
("prefix for selection is not a name");
2532 -- If qualified expression, comment and continue, otherwise something
2533 -- is pretty nasty so do an Error_Resync call.
2535 if Ada_Version
< Ada_2012
2536 and then Nkind
(Node1
) = N_Qualified_Expression
2538 Error_Msg_SC
("\would be legal in Ada 2012 mode");
2544 -- Special test to improve error recovery: If the current token is
2545 -- not the first token on a line (as determined by checking the
2546 -- previous token position with the start of the current line),
2547 -- then we insist that we have an appropriate terminating token.
2548 -- Consider the following two examples:
2550 -- 1) if A nad B then ...
2555 -- In the first example, we would like to issue a binary operator
2556 -- expected message and resynchronize to the then. In the second
2557 -- example, we do not want to issue a binary operator message, so
2558 -- that instead we will get the missing semicolon message. This
2559 -- distinction is of course a heuristic which does not always work,
2560 -- but in practice it is quite effective.
2562 -- Note: the one case in which we do not go through this circuit is
2563 -- when we have scanned a range attribute and want to return with
2564 -- Token pointing to the apostrophe. The apostrophe is not normally
2565 -- an expression terminator, and is not in Token_Class_Sterm, but
2566 -- in this special case we know that the expression is complete.
2568 -- We disable this error recovery machinery when we are processing an
2569 -- interpolated string and we reach the expression terminator '}'.
2571 if not Token_Is_At_Start_Of_Line
2572 and then Token
not in Token_Class_Sterm
2573 and then not (Inside_Interpolated_String_Literal
2574 and then Token
= Tok_Right_Curly_Bracket
)
2576 -- Normally the right error message is indeed that we expected a
2577 -- binary operator, but in the case of being between a right and left
2578 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2580 if Prev_Token
= Tok_Right_Paren
and then Token
= Tok_Left_Paren
then
2583 -- And if we have a quote, we may have a bad attribute
2585 elsif At_Start_Of_Attribute
then
2586 Error_Msg_SC
("prefix of attribute must be a name");
2588 if Ada_Version
>= Ada_2012
then
2589 Error_Msg_SC
("\qualify expression to turn it into a name");
2592 -- Normal case for binary operator expected message
2595 Error_Msg_AP
("binary operator expected");
2604 -- If any error occurs, then scan to next expression terminator symbol
2605 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2606 -- level. Expr_Form is set to indicate a normal simple expression.
2609 when Error_Resync
=>
2611 Expr_Form
:= EF_Simple
;
2613 end P_Simple_Expression
;
2615 -----------------------------------------------
2616 -- 4.4 Simple Expression or Range Attribute --
2617 -----------------------------------------------
2619 -- SIMPLE_EXPRESSION ::=
2620 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2622 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2624 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2626 -- Error recovery: cannot raise Error_Resync
2628 function P_Simple_Expression_Or_Range_Attribute
return Node_Id
is
2630 Attr_Node
: Node_Id
;
2633 -- We don't just want to roar ahead and call P_Simple_Expression
2634 -- here, since we want to handle the case of a parenthesized range
2635 -- attribute cleanly.
2637 if Token
= Tok_Left_Paren
then
2639 Lptr
: constant Source_Ptr
:= Token_Ptr
;
2640 Scan_State
: Saved_Scan_State
;
2643 Save_Scan_State
(Scan_State
);
2644 Scan
; -- past left paren
2645 Sexpr
:= P_Simple_Expression
;
2647 if Token
= Tok_Apostrophe
then
2648 Attr_Node
:= P_Range_Attribute_Reference
(Sexpr
);
2649 Expr_Form
:= EF_Range_Attr
;
2651 if Token
= Tok_Right_Paren
then
2652 Scan
; -- scan past right paren if present
2655 Error_Msg
("parentheses not allowed for range attribute", Lptr
);
2660 Restore_Scan_State
(Scan_State
);
2664 -- Here after dealing with parenthesized range attribute
2666 Sexpr
:= P_Simple_Expression
;
2668 if Token
= Tok_Apostrophe
then
2669 Attr_Node
:= P_Range_Attribute_Reference
(Sexpr
);
2670 Expr_Form
:= EF_Range_Attr
;
2676 end P_Simple_Expression_Or_Range_Attribute
;
2682 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2684 -- Error recovery: can raise Error_Resync
2686 function P_Term
return Node_Id
is
2687 Node1
, Node2
: Node_Id
;
2688 Tokptr
: Source_Ptr
;
2694 exit when Token
not in Token_Class_Mulop
;
2695 Tokptr
:= Token_Ptr
;
2696 Node2
:= New_Op_Node
(P_Multiplying_Operator
, Tokptr
);
2698 if Style_Check
and then not Debug_Flag_Dot_QQ
then
2699 Style
.Check_Binary_Operator
;
2702 Scan
; -- past operator
2703 Set_Left_Opnd
(Node2
, Node1
);
2704 Set_Right_Opnd
(Node2
, P_Factor
);
2715 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2717 -- Error recovery: can raise Error_Resync
2719 function P_Factor
return Node_Id
is
2724 if Token
= Tok_Abs
then
2725 Node1
:= New_Op_Node
(N_Op_Abs
, Token_Ptr
);
2728 Style
.Check_Abs_Not
;
2732 Set_Right_Opnd
(Node1
, P_Primary
);
2735 elsif Token
= Tok_Not
then
2736 Node1
:= New_Op_Node
(N_Op_Not
, Token_Ptr
);
2739 Style
.Check_Abs_Not
;
2743 Set_Right_Opnd
(Node1
, P_Primary
);
2749 if Token
= Tok_Double_Asterisk
then
2750 Node2
:= New_Op_Node
(N_Op_Expon
, Token_Ptr
);
2752 Set_Left_Opnd
(Node2
, Node1
);
2753 Set_Right_Opnd
(Node2
, P_Primary
);
2767 -- NUMERIC_LITERAL | null
2768 -- | STRING_LITERAL | AGGREGATE
2769 -- | NAME | QUALIFIED_EXPRESSION
2770 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2771 -- | REDUCTION_ATTRIBUTE_REFERENCE
2773 -- Error recovery: can raise Error_Resync
2775 function P_Primary
return Node_Id
is
2776 Scan_State
: Saved_Scan_State
;
2779 Lparen
: constant Boolean := Prev_Token
= Tok_Left_Paren
;
2780 -- Remember if previous token is a left parenthesis. This is used to
2781 -- deal with checking whether IF/CASE/FOR expressions appearing as
2782 -- primaries require extra parenthesization.
2785 -- The loop runs more than once only if misplaced pragmas are found
2786 -- or if a misplaced unary minus is skipped.
2791 -- Name token can start a name, call or qualified expression, all
2792 -- of which are acceptable possibilities for primary. Note also
2793 -- that string literal is included in name (as operator symbol)
2794 -- and type conversion is included in name (as indexed component).
2796 when Tok_Char_Literal
2798 | Tok_Operator_Symbol
2802 -- All done unless apostrophe follows
2804 if Token
/= Tok_Apostrophe
then
2807 -- Apostrophe following means that we have either just parsed
2808 -- the subtype mark of a qualified expression, or the prefix
2809 -- or a range attribute.
2811 else -- Token = Tok_Apostrophe
2812 Save_Scan_State
(Scan_State
); -- at apostrophe
2813 Scan
; -- past apostrophe
2815 -- If range attribute, then this is always an error, since
2816 -- the only legitimate case (where the scanned expression is
2817 -- a qualified simple name) is handled at the level of the
2818 -- Simple_Expression processing. This case corresponds to a
2819 -- usage such as 3 + A'Range, which is always illegal.
2821 if Token
= Tok_Range
then
2822 Restore_Scan_State
(Scan_State
); -- to apostrophe
2823 Bad_Range_Attribute
(Token_Ptr
);
2826 -- If left paren, then we have a qualified expression.
2827 -- Note that P_Name guarantees that in this case, where
2828 -- Token = Tok_Apostrophe on return, the only two possible
2829 -- tokens following the apostrophe are left paren and
2830 -- RANGE, so we know we have a left paren here.
2832 else -- Token = Tok_Left_Paren
2833 return P_Qualified_Expression
(Node1
);
2838 -- Numeric or string literal
2840 when Tok_Integer_Literal
2842 | Tok_String_Literal
2844 Node1
:= Token_Node
;
2845 Scan
; -- past number
2848 -- Left paren, starts aggregate or parenthesized expression
2850 when Tok_Left_Paren
=>
2852 Expr
: constant Node_Id
:= P_Aggregate_Or_Paren_Expr
;
2855 if Nkind
(Expr
) = N_Attribute_Reference
2856 and then Attribute_Name
(Expr
) = Name_Range
2858 Bad_Range_Attribute
(Sloc
(Expr
));
2864 when Tok_Left_Bracket
=>
2867 when Tok_Left_Interpolated_String
=>
2868 return P_Interpolated_String_Literal
;
2879 return New_Node
(N_Null
, Prev_Token_Ptr
);
2881 -- Pragma, not allowed here, so just skip past it
2884 P_Pragmas_Misplaced
;
2886 -- Deal with IF (possible unparenthesized if expression)
2890 -- If this looks like a real if, defined as an IF appearing at
2891 -- the start of a new line, then we consider we have a missing
2892 -- operand. If in Ada 2012 and the IF is not properly indented
2893 -- for a statement, we prefer to issue a message about an ill-
2894 -- parenthesized if expression.
2896 if Token_Is_At_Start_Of_Line
2898 (Ada_Version
>= Ada_2012
2900 (Style_Check_Indentation
= 0
2902 Start_Column
rem Style_Check_Indentation
/= 0))
2904 Error_Msg_AP
("missing operand");
2907 -- If this looks like an if expression, then treat it that way
2908 -- with an error message if not explicitly surrounded by
2911 elsif Ada_Version
>= Ada_2012
then
2912 Node1
:= P_If_Expression
;
2914 if not (Lparen
and then Token
= Tok_Right_Paren
) then
2916 ("if expression must be parenthesized", Sloc
(Node1
));
2921 -- Otherwise treat as misused identifier
2924 return P_Identifier
;
2927 -- Deal with CASE (possible unparenthesized case expression)
2931 -- If this looks like a real case, defined as a CASE appearing
2932 -- the start of a new line, then we consider we have a missing
2933 -- operand. If in Ada 2012 and the CASE is not properly
2934 -- indented for a statement, we prefer to issue a message about
2935 -- an ill-parenthesized case expression.
2937 if Token_Is_At_Start_Of_Line
2939 (Ada_Version
>= Ada_2012
2940 and then Style_Check_Indentation
/= 0
2941 and then Start_Column
rem Style_Check_Indentation
/= 0)
2943 Error_Msg_AP
("missing operand");
2946 -- If this looks like a case expression, then treat it that way
2947 -- with an error message if not within parentheses.
2949 elsif Ada_Version
>= Ada_2012
then
2950 Node1
:= P_Case_Expression
;
2952 if not (Lparen
and then Token
= Tok_Right_Paren
) then
2954 ("case expression must be parenthesized", Sloc
(Node1
));
2959 -- Otherwise treat as misused identifier
2962 return P_Identifier
;
2965 -- For [all | some] indicates a quantified expression
2968 if Token_Is_At_Start_Of_Line
then
2969 Error_Msg_AP
("misplaced loop");
2972 elsif Ada_Version
>= Ada_2012
then
2973 Save_Scan_State
(Scan_State
);
2976 if Token
in Tok_All | Tok_Some
then
2977 Restore_Scan_State
(Scan_State
); -- To FOR
2978 Node1
:= P_Quantified_Expression
;
2980 if not (Lparen
and then Token
= Tok_Right_Paren
) then
2982 ("quantified expression must be parenthesized",
2986 Restore_Scan_State
(Scan_State
); -- To FOR
2987 Node1
:= P_Iterated_Component_Association
;
2992 -- Otherwise treat as misused identifier
2995 return P_Identifier
;
2998 -- Minus may well be an improper attempt at a unary minus. Give
2999 -- a message, skip the minus and keep going.
3002 Error_Msg_SC
("parentheses required for unary minus");
3005 when Tok_At_Sign
=> -- AI12-0125 : target_name
3006 Error_Msg_Ada_2022_Feature
("target name", Token_Ptr
);
3011 -- Anything else is illegal as the first token of a primary, but
3012 -- we test for some common errors, to improve error messages.
3015 if Is_Reserved_Identifier
then
3016 return P_Identifier
;
3018 elsif Prev_Token
= Tok_Comma
then
3019 Error_Msg_SP
-- CODEFIX
3020 ("|extra "","" ignored");
3024 Error_Msg_AP
("missing operand");
3031 -------------------------------
3032 -- 4.4 Quantified_Expression --
3033 -------------------------------
3035 -- QUANTIFIED_EXPRESSION ::=
3036 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
3037 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
3039 function P_Quantified_Expression
return Node_Id
is
3044 Error_Msg_Ada_2012_Feature
("quantified expression", Token_Ptr
);
3046 Node1
:= New_Node
(N_Quantified_Expression
, Prev_Token_Ptr
);
3048 if Token
= Tok_All
then
3049 Set_All_Present
(Node1
);
3050 elsif Token
/= Tok_Some
then
3051 Error_Msg_AP
("missing quantifier");
3055 Scan
; -- past ALL or SOME
3056 I_Spec
:= P_Loop_Parameter_Specification
;
3058 if Nkind
(I_Spec
) = N_Loop_Parameter_Specification
then
3059 Set_Loop_Parameter_Specification
(Node1
, I_Spec
);
3061 Set_Iterator_Specification
(Node1
, I_Spec
);
3064 if Token
= Tok_Arrow
then
3066 Set_Condition
(Node1
, P_Expression
);
3069 Error_Msg_AP
("missing arrow");
3072 end P_Quantified_Expression
;
3074 ---------------------------
3075 -- 4.5 Logical Operator --
3076 ---------------------------
3078 -- LOGICAL_OPERATOR ::= and | or | xor
3080 -- Note: AND THEN and OR ELSE are also treated as logical operators
3081 -- by the parser (even though they are not operators semantically)
3083 -- The value returned is the appropriate Node_Kind code for the operator
3084 -- On return, Token points to the token following the scanned operator.
3086 -- The caller has checked that the first token is a legitimate logical
3087 -- operator token (i.e. is either XOR, AND, OR).
3089 -- Error recovery: cannot raise Error_Resync
3091 function P_Logical_Operator
return Node_Kind
is
3093 if Token
= Tok_And
then
3095 Style
.Check_Binary_Operator
;
3100 if Token
= Tok_Then
then
3107 elsif Token
= Tok_Or
then
3109 Style
.Check_Binary_Operator
;
3114 if Token
= Tok_Else
then
3121 else -- Token = Tok_Xor
3123 Style
.Check_Binary_Operator
;
3129 end P_Logical_Operator
;
3131 ------------------------------
3132 -- 4.5 Relational Operator --
3133 ------------------------------
3135 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
3137 -- The value returned is the appropriate Node_Kind code for the operator.
3138 -- On return, Token points to the operator token, NOT past it.
3140 -- The caller has checked that the first token is a legitimate relational
3141 -- operator token (i.e. is one of the operator tokens listed above).
3143 -- Error recovery: cannot raise Error_Resync
3145 function P_Relational_Operator
return Node_Kind
is
3146 Op_Kind
: Node_Kind
;
3147 Relop_Node
: constant array (Token_Class_Relop
) of Node_Kind
:=
3148 (Tok_Less
=> N_Op_Lt
,
3149 Tok_Equal
=> N_Op_Eq
,
3150 Tok_Greater
=> N_Op_Gt
,
3151 Tok_Not_Equal
=> N_Op_Ne
,
3152 Tok_Greater_Equal
=> N_Op_Ge
,
3153 Tok_Less_Equal
=> N_Op_Le
,
3155 Tok_Not
=> N_Not_In
,
3156 Tok_Box
=> N_Op_Ne
);
3159 if Token
= Tok_Box
then
3160 Error_Msg_SC
-- CODEFIX
3161 ("|""'<'>"" should be ""/=""");
3164 Op_Kind
:= Relop_Node
(Token
);
3167 Style
.Check_Binary_Operator
;
3170 Scan
; -- past operator token
3172 -- Deal with NOT IN, if previous token was NOT, we must have IN now
3174 if Prev_Token
= Tok_Not
then
3176 -- Style check, for NOT IN, we require one space between NOT and IN
3178 if Style_Check
and then Token
= Tok_In
then
3186 end P_Relational_Operator
;
3188 ---------------------------------
3189 -- 4.5 Binary Adding Operator --
3190 ---------------------------------
3192 -- BINARY_ADDING_OPERATOR ::= + | - | &
3194 -- The value returned is the appropriate Node_Kind code for the operator.
3195 -- On return, Token points to the operator token (NOT past it).
3197 -- The caller has checked that the first token is a legitimate adding
3198 -- operator token (i.e. is one of the operator tokens listed above).
3200 -- Error recovery: cannot raise Error_Resync
3202 function P_Binary_Adding_Operator
return Node_Kind
is
3203 Addop_Node
: constant array (Token_Class_Binary_Addop
) of Node_Kind
:=
3204 (Tok_Ampersand
=> N_Op_Concat
,
3205 Tok_Minus
=> N_Op_Subtract
,
3206 Tok_Plus
=> N_Op_Add
);
3208 return Addop_Node
(Token
);
3209 end P_Binary_Adding_Operator
;
3211 --------------------------------
3212 -- 4.5 Unary Adding Operator --
3213 --------------------------------
3215 -- UNARY_ADDING_OPERATOR ::= + | -
3217 -- The value returned is the appropriate Node_Kind code for the operator.
3218 -- On return, Token points to the operator token (NOT past it).
3220 -- The caller has checked that the first token is a legitimate adding
3221 -- operator token (i.e. is one of the operator tokens listed above).
3223 -- Error recovery: cannot raise Error_Resync
3225 function P_Unary_Adding_Operator
return Node_Kind
is
3226 Addop_Node
: constant array (Token_Class_Unary_Addop
) of Node_Kind
:=
3227 (Tok_Minus
=> N_Op_Minus
,
3228 Tok_Plus
=> N_Op_Plus
);
3230 return Addop_Node
(Token
);
3231 end P_Unary_Adding_Operator
;
3233 -------------------------------
3234 -- 4.5 Multiplying Operator --
3235 -------------------------------
3237 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3239 -- The value returned is the appropriate Node_Kind code for the operator.
3240 -- On return, Token points to the operator token (NOT past it).
3242 -- The caller has checked that the first token is a legitimate multiplying
3243 -- operator token (i.e. is one of the operator tokens listed above).
3245 -- Error recovery: cannot raise Error_Resync
3247 function P_Multiplying_Operator
return Node_Kind
is
3248 Mulop_Node
: constant array (Token_Class_Mulop
) of Node_Kind
:=
3249 (Tok_Asterisk
=> N_Op_Multiply
,
3250 Tok_Mod
=> N_Op_Mod
,
3251 Tok_Rem
=> N_Op_Rem
,
3252 Tok_Slash
=> N_Op_Divide
);
3254 return Mulop_Node
(Token
);
3255 end P_Multiplying_Operator
;
3257 --------------------------------------
3258 -- 4.5 Highest Precedence Operator --
3259 --------------------------------------
3261 -- Parsed by P_Factor (4.4)
3263 -- Note: this rule is not in fact used by the grammar at any point
3265 --------------------------
3266 -- 4.6 Type Conversion --
3267 --------------------------
3269 -- Parsed by P_Primary as a Name (4.1)
3271 -------------------------------
3272 -- 4.7 Qualified Expression --
3273 -------------------------------
3275 -- QUALIFIED_EXPRESSION ::=
3276 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3278 -- The caller has scanned the name which is the Subtype_Mark parameter
3279 -- and scanned past the single quote following the subtype mark. The
3280 -- caller has not checked that this name is in fact appropriate for
3281 -- a subtype mark name (i.e. it is a selected component or identifier).
3283 -- Error_Recovery: cannot raise Error_Resync
3285 function P_Qualified_Expression
(Subtype_Mark
: Node_Id
) return Node_Id
is
3286 Qual_Node
: Node_Id
;
3288 Qual_Node
:= New_Node
(N_Qualified_Expression
, Prev_Token_Ptr
);
3289 Set_Subtype_Mark
(Qual_Node
, Check_Subtype_Mark
(Subtype_Mark
));
3290 Set_Expression
(Qual_Node
, P_Aggregate_Or_Paren_Expr
);
3292 end P_Qualified_Expression
;
3294 --------------------
3296 --------------------
3299 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3300 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3302 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3304 -- The caller has checked that the initial token is NEW
3306 -- Error recovery: can raise Error_Resync
3308 function P_Allocator
return Node_Id
is
3309 Alloc_Node
: Node_Id
;
3310 Type_Node
: Node_Id
;
3311 Null_Exclusion_Present
: Boolean;
3314 Alloc_Node
:= New_Node
(N_Allocator
, Token_Ptr
);
3317 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3319 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3321 if Token
= Tok_Left_Paren
then
3323 Set_Subpool_Handle_Name
(Alloc_Node
, P_Name
);
3326 Error_Msg_Ada_2012_Feature
3327 ("|subpool specification",
3328 Sloc
(Subpool_Handle_Name
(Alloc_Node
)));
3331 Null_Exclusion_Present
:= P_Null_Exclusion
;
3332 Set_Null_Exclusion_Present
(Alloc_Node
, Null_Exclusion_Present
);
3333 Type_Node
:= P_Subtype_Mark_Resync
;
3335 if Token
= Tok_Apostrophe
then
3336 Scan
; -- past apostrophe
3337 Set_Expression
(Alloc_Node
, P_Qualified_Expression
(Type_Node
));
3341 P_Subtype_Indication
(Type_Node
, Null_Exclusion_Present
));
3343 -- AI05-0104: An explicit null exclusion is not allowed for an
3344 -- allocator without initialization. In previous versions of the
3345 -- language it just raises constraint error.
3347 if Ada_Version
>= Ada_2012
and then Null_Exclusion_Present
then
3349 ("an allocator with a subtype indication "
3350 & "cannot have a null exclusion", Alloc_Node
);
3357 -----------------------
3358 -- P_Case_Expression --
3359 -----------------------
3361 function P_Case_Expression
return Node_Id
is
3362 Loc
: constant Source_Ptr
:= Token_Ptr
;
3363 Case_Node
: Node_Id
;
3364 Save_State
: Saved_Scan_State
;
3367 Error_Msg_Ada_2012_Feature
("|case expression", Token_Ptr
);
3370 Make_Case_Expression
(Loc
,
3371 Expression
=> P_Expression_No_Right_Paren
,
3372 Alternatives
=> New_List
);
3375 -- We now have scanned out CASE expression IS, scan alternatives
3379 Append_To
(Alternatives
(Case_Node
), P_Case_Expression_Alternative
);
3381 -- Missing comma if WHEN (more alternatives present)
3383 if Token
= Tok_When
then
3386 -- A semicolon followed by "when" is probably meant to be a comma
3388 elsif Token
= Tok_Semicolon
then
3389 Save_Scan_State
(Save_State
);
3390 Scan
; -- past the semicolon
3392 if Token
/= Tok_When
then
3393 Restore_Scan_State
(Save_State
);
3397 Error_Msg_SP
-- CODEFIX
3398 ("|"";"" should be "",""");
3400 -- If comma/WHEN, skip comma and we have another alternative
3402 elsif Token
= Tok_Comma
then
3403 Save_Scan_State
(Save_State
);
3406 if Token
/= Tok_When
then
3407 Restore_Scan_State
(Save_State
);
3411 -- If no comma or WHEN, definitely done
3418 -- If we have an END CASE, diagnose as not needed
3420 if Token
= Tok_End
then
3421 Error_Msg_SC
("`END CASE` not allowed at end of case expression");
3424 if Token
= Tok_Case
then
3429 -- Return the Case_Expression node
3432 end P_Case_Expression
;
3434 -----------------------------------
3435 -- P_Case_Expression_Alternative --
3436 -----------------------------------
3438 -- CASE_STATEMENT_ALTERNATIVE ::=
3439 -- when DISCRETE_CHOICE_LIST =>
3442 -- The caller has checked that and scanned past the initial WHEN token
3443 -- Error recovery: can raise Error_Resync
3445 function P_Case_Expression_Alternative
return Node_Id
is
3446 Case_Alt_Node
: Node_Id
;
3448 Case_Alt_Node
:= New_Node
(N_Case_Expression_Alternative
, Token_Ptr
);
3449 Set_Discrete_Choices
(Case_Alt_Node
, P_Discrete_Choice_List
);
3451 Set_Expression
(Case_Alt_Node
, P_Expression
);
3452 return Case_Alt_Node
;
3453 end P_Case_Expression_Alternative
;
3455 --------------------------------------
3456 -- P_Iterated_Component_Association --
3457 --------------------------------------
3459 -- ITERATED_COMPONENT_ASSOCIATION ::=
3460 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3461 -- for ITERATOR_SPECIFICATION => EXPRESSION
3463 function P_Iterated_Component_Association
return Node_Id
is
3464 Assoc_Node
: Node_Id
;
3466 Filter
: Node_Id
:= Empty
;
3468 Iter_Spec
: Node_Id
;
3469 Loop_Spec
: Node_Id
;
3470 State
: Saved_Scan_State
;
3472 procedure Build_Iterated_Element_Association
;
3473 -- If the iterator includes a key expression or a filter, it is
3474 -- an Ada 2022 Iterator_Element_Association within a container
3477 ----------------------------------------
3478 -- Build_Iterated_Element_Association --
3479 ----------------------------------------
3481 procedure Build_Iterated_Element_Association
is
3483 -- Build loop_parameter_specification
3486 New_Node
(N_Loop_Parameter_Specification
, Prev_Token_Ptr
);
3487 Set_Defining_Identifier
(Loop_Spec
, Id
);
3489 Choice
:= First
(Discrete_Choices
(Assoc_Node
));
3491 New_Node
(N_Iterated_Element_Association
, Prev_Token_Ptr
);
3492 Set_Loop_Parameter_Specification
(Assoc_Node
, Loop_Spec
);
3494 if Present
(Next
(Choice
)) then
3495 Error_Msg_N
("expect loop parameter specification", Choice
);
3499 Set_Discrete_Subtype_Definition
(Loop_Spec
, Choice
);
3500 Set_Iterator_Filter
(Loop_Spec
, Filter
);
3501 end Build_Iterated_Element_Association
;
3503 -- Start of processing for P_Iterated_Component_Association
3507 Save_Scan_State
(State
);
3509 -- A lookahead is necessary to differentiate between the
3510 -- Ada 2012 form with a choice list, and the Ada 2022 element
3511 -- iterator form, recognized by the presence of "OF". Other
3512 -- disambiguation requires context and is done during semantic
3513 -- analysis. Note that "for X in E" is syntactically ambiguous:
3514 -- if E is a subtype indication this is a loop parameter spec,
3515 -- while if E a name it is an iterator_specification, and the
3516 -- disambiguation takes place during semantic analysis.
3517 -- In addition, if "use" is present after the specification,
3518 -- this is an Iterated_Element_Association that carries a
3519 -- key_expression, and we generate the appropriate node.
3520 -- Finally, the Iterated_Element form is reserved for container
3521 -- aggregates, and is illegal in array aggregates.
3523 Id
:= P_Defining_Identifier
;
3525 New_Node
(N_Iterated_Component_Association
, Prev_Token_Ptr
);
3529 Set_Defining_Identifier
(Assoc_Node
, Id
);
3531 Set_Discrete_Choices
(Assoc_Node
, P_Discrete_Choice_List
);
3533 -- The iterator may include a filter
3535 if Token
= Tok_When
then
3537 Filter
:= P_Condition
;
3540 if Token
= Tok_Use
then
3542 -- Ada 2022 Key-expression is present, rewrite node as an
3543 -- Iterated_Element_Association.
3546 Build_Iterated_Element_Association
;
3547 Set_Key_Expression
(Assoc_Node
, P_Expression
);
3549 elsif Present
(Filter
) then
3550 -- A loop_parameter_specification also indicates an Ada 2022
3551 -- construct, in contrast with a subtype indication used in
3552 -- array aggregates.
3554 Build_Iterated_Element_Association
;
3558 Set_Expression
(Assoc_Node
, P_Expression
);
3561 Restore_Scan_State
(State
);
3563 Iter_Spec
:= P_Iterator_Specification
(Id
);
3564 Set_Iterator_Specification
(Assoc_Node
, Iter_Spec
);
3566 if Token
= Tok_Use
then
3568 -- This is an iterated_element_association
3571 New_Node
(N_Iterated_Element_Association
, Prev_Token_Ptr
);
3572 Set_Iterator_Specification
(Assoc_Node
, Iter_Spec
);
3573 Set_Key_Expression
(Assoc_Node
, P_Expression
);
3577 Set_Expression
(Assoc_Node
, P_Expression
);
3580 Error_Msg_AP
("missing IN or OF");
3584 end P_Iterated_Component_Association
;
3586 ---------------------
3587 -- P_If_Expression --
3588 ---------------------
3590 -- IF_EXPRESSION ::=
3591 -- if CONDITION then DEPENDENT_EXPRESSION
3592 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3593 -- [else DEPENDENT_EXPRESSION]
3595 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3597 function P_If_Expression
return Node_Id
is
3598 function P_If_Expression_Internal
3600 Cond
: Node_Id
) return Node_Id
;
3601 -- This is the internal recursive routine that does all the work, it is
3602 -- recursive since it is used to process ELSIF parts, which internally
3603 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3604 -- sequence is like the outer function except that the caller passes
3605 -- the conditional expression (scanned using P_Expression), and the
3606 -- scan pointer points just past this expression. Loc points to the
3607 -- IF or ELSIF token.
3609 ------------------------------
3610 -- P_If_Expression_Internal --
3611 ------------------------------
3613 function P_If_Expression_Internal
3615 Cond
: Node_Id
) return Node_Id
3617 Exprs
: constant List_Id
:= New_List
;
3619 State
: Saved_Scan_State
;
3623 -- All cases except where we are at right paren
3625 if Token
/= Tok_Right_Paren
then
3627 Append_To
(Exprs
, P_Condition
(Cond
));
3628 Append_To
(Exprs
, P_Expression
);
3630 -- Case of right paren (missing THEN phrase). Note that we know this
3631 -- is the IF case, since the caller dealt with this possibility in
3635 Error_Msg_BC
("missing THEN phrase");
3636 Append_To
(Exprs
, P_Condition
(Cond
));
3639 -- We now have scanned out IF expr THEN expr
3641 -- Check for common error of semicolon before the ELSE
3643 if Token
= Tok_Semicolon
then
3644 Save_Scan_State
(State
);
3645 Scan
; -- past semicolon
3647 if Token
in Tok_Else | Tok_Elsif
then
3648 Error_Msg_SP
-- CODEFIX
3649 ("|extra "";"" ignored");
3652 Restore_Scan_State
(State
);
3656 -- Scan out ELSIF sequence if present
3658 if Token
= Tok_Elsif
then
3661 Expr
:= P_Expression
;
3663 -- If we are at a right paren, we assume the ELSIF should be ELSE
3665 if Token
= Tok_Right_Paren
then
3666 Error_Msg
("ELSIF should be ELSE", Eptr
);
3667 Append_To
(Exprs
, Expr
);
3669 -- Otherwise we have an OK ELSIF
3672 Expr
:= P_If_Expression_Internal
(Eptr
, Expr
);
3673 Set_Is_Elsif
(Expr
);
3674 Append_To
(Exprs
, Expr
);
3677 -- Scan out ELSE phrase if present
3679 elsif Token
= Tok_Else
then
3681 -- Scan out ELSE expression
3684 Append_To
(Exprs
, P_Expression
);
3686 -- Skip redundant ELSE parts
3688 while Token
= Tok_Else
loop
3689 Error_Msg_SC
("only one ELSE part is allowed");
3691 Discard_Junk_Node
(P_Expression
);
3694 -- Two expression case (implied True, filled in during semantics)
3700 -- If we have an END IF, diagnose as not needed
3702 if Token
= Tok_End
then
3703 Error_Msg_SC
("`END IF` not allowed at end of if expression");
3706 if Token
= Tok_If
then
3711 -- Return the If_Expression node
3713 return Make_If_Expression
(Loc
, Expressions
=> Exprs
);
3714 end P_If_Expression_Internal
;
3718 Loc
: constant Source_Ptr
:= Token_Ptr
;
3721 -- Start of processing for P_If_Expression
3724 Error_Msg_Ada_2012_Feature
("|if expression", Token_Ptr
);
3726 Inside_If_Expression
:= Inside_If_Expression
+ 1;
3727 If_Expr
:= P_If_Expression_Internal
(Loc
, P_Expression
);
3728 Inside_If_Expression
:= Inside_If_Expression
- 1;
3730 end P_If_Expression
;
3732 --------------------------
3733 -- P_Declare_Expression --
3734 --------------------------
3736 -- DECLARE_EXPRESSION ::=
3737 -- DECLARE {DECLARE_ITEM}
3738 -- begin BODY_EXPRESSION
3740 -- DECLARE_ITEM ::= OBJECT_DECLARATION
3741 -- | OBJECT_RENAMING_DECLARATION
3743 function P_Declare_Expression
return Node_Id
is
3744 Loc
: constant Source_Ptr
:= Token_Ptr
;
3746 Scan
; -- past DECLARE
3749 Actions
: constant List_Id
:= P_Basic_Declarative_Items
3750 (Declare_Expression
=> True);
3751 -- Most declarative items allowed by P_Basic_Declarative_Items are
3752 -- illegal; semantic analysis will deal with that.
3754 if Token
= Tok_Begin
then
3757 Error_Msg_SC
-- CODEFIX
3758 ("BEGIN expected!");
3762 Expression
: constant Node_Id
:= P_Expression
;
3763 Result
: constant Node_Id
:=
3764 Make_Expression_With_Actions
(Loc
, Actions
, Expression
);
3766 Error_Msg_Ada_2022_Feature
("declare expression", Loc
);
3771 end P_Declare_Expression
;
3773 -----------------------
3774 -- P_Membership_Test --
3775 -----------------------
3777 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERSHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3778 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3780 procedure P_Membership_Test
(N
: Node_Id
) is
3781 Alt
: constant Node_Id
:=
3782 P_Range_Or_Subtype_Mark
3783 (Allow_Simple_Expression
=> (Ada_Version
>= Ada_2012
));
3788 if Token
= Tok_Vertical_Bar
then
3789 Error_Msg_Ada_2012_Feature
("set notation", Token_Ptr
);
3790 Set_Alternatives
(N
, New_List
(Alt
));
3791 Set_Right_Opnd
(N
, Empty
);
3793 -- Loop to accumulate alternatives
3795 while Token
= Tok_Vertical_Bar
loop
3796 Scan
; -- past vertical bar
3799 P_Range_Or_Subtype_Mark
(Allow_Simple_Expression
=> True));
3805 Set_Right_Opnd
(N
, Alt
);
3806 Set_Alternatives
(N
, No_List
);
3808 end P_Membership_Test
;
3810 -----------------------------
3811 -- P_Unparen_Cond_Expr_Etc --
3812 -----------------------------
3814 function P_Unparen_Cond_Expr_Etc
return Node_Id
is
3815 Lparen
: constant Boolean := Prev_Token
= Tok_Left_Paren
;
3818 Scan_State
: Saved_Scan_State
;
3823 if Token
= Tok_Case
then
3824 Result
:= P_Case_Expression
;
3826 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3827 Error_Msg_N
("case expression must be parenthesized!", Result
);
3832 elsif Token
= Tok_If
then
3833 Result
:= P_If_Expression
;
3835 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3836 Error_Msg_N
("if expression must be parenthesized!", Result
);
3839 -- Quantified expression or iterated component association
3841 elsif Token
= Tok_For
then
3843 Save_Scan_State
(Scan_State
);
3846 if Token
in Tok_All | Tok_Some
then
3847 Restore_Scan_State
(Scan_State
);
3848 Result
:= P_Quantified_Expression
;
3850 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3852 ("quantified expression must be parenthesized!", Result
);
3856 -- If no quantifier keyword, this is an iterated component in
3859 Restore_Scan_State
(Scan_State
);
3860 Result
:= P_Iterated_Component_Association
;
3863 -- Declare expression
3865 elsif Token
= Tok_Declare
then
3866 Result
:= P_Declare_Expression
;
3868 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3869 Error_Msg_N
("declare expression must be parenthesized!", Result
);
3872 -- No other possibility should exist (caller was supposed to check)
3875 raise Program_Error
;
3878 -- Return expression (possibly after having given message)
3881 end P_Unparen_Cond_Expr_Etc
;