1 -----------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2022, 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
229 or else Token
= Tok_Left_Paren
231 Error_Msg_SC
("name expected");
234 -- Otherwise some other junk, not much we can do
237 Error_Msg_AP
("name expected");
242 -- Loop through designators in qualified name
243 -- AI12-0125 : target_name
245 if Token
= Tok_At_Sign
then
246 Scan_Reserved_Identifier
(Force_Msg
=> False);
248 if Present
(Current_Assign_Node
) then
249 Set_Has_Target_Names
(Current_Assign_Node
);
253 Name_Node
:= Token_Node
;
256 Scan
; -- past designator
257 exit when Token
/= Tok_Dot
;
258 Save_Scan_State
(Scan_State
); -- at dot
261 -- If we do not have another designator after the dot, then join
262 -- the normal circuit to handle a dot extension (may be .all or
263 -- character literal case). Otherwise loop back to scan the next
266 if Token
not in Token_Class_Desig
then
267 goto Scan_Name_Extension_Dot
;
269 Prefix_Node
:= Name_Node
;
270 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
271 Set_Prefix
(Name_Node
, Prefix_Node
);
272 Set_Selector_Name
(Name_Node
, Token_Node
);
276 -- We have now scanned out a qualified designator. If the last token is
277 -- an operator symbol, then we certainly do not have the Snam case, so
278 -- we can just use the normal name extension check circuit
280 if Prev_Token
= Tok_Operator_Symbol
then
281 goto Scan_Name_Extension
;
284 -- We have scanned out a qualified simple name, check for name
285 -- extension. Note that we know there is no dot here at this stage,
286 -- so the only possible cases of name extension are apostrophe followed
289 if Token
= Tok_Apostrophe
then
290 Save_Scan_State
(Scan_State
); -- at apostrophe
291 Scan
; -- past apostrophe
293 -- Qualified expression in Ada 2012 mode (treated as a name)
295 if Ada_Version
>= Ada_2012
296 and then Token
in Tok_Left_Paren | Tok_Left_Bracket
298 goto Scan_Name_Extension_Apostrophe
;
300 -- If left paren not in Ada 2012, then it is not part of the name,
301 -- since qualified expressions are not names in prior versions of
302 -- Ada, so return with Token backed up to point to the apostrophe.
303 -- The treatment for the range attribute is similar (we do not
304 -- consider x'range to be a name in this grammar).
306 elsif Token
= Tok_Left_Paren
or else Token
= Tok_Range
then
307 Restore_Scan_State
(Scan_State
); -- to apostrophe
308 Expr_Form
:= EF_Simple_Name
;
311 -- Otherwise we have the case of a name extended by an attribute
314 goto Scan_Name_Extension_Apostrophe
;
317 -- Check case of qualified simple name extended by a left parenthesis
319 elsif Token
= Tok_Left_Paren
then
320 Scan
; -- past left paren
321 goto Scan_Name_Extension_Left_Paren
;
323 -- Otherwise the qualified simple name is not extended, so return
326 Expr_Form
:= EF_Simple_Name
;
330 -- Loop scanning past name extensions. A label is used for control
331 -- transfer for this loop for ease of interfacing with the finite state
332 -- machine in the parenthesis scanning circuit, and also to allow for
333 -- passing in control to the appropriate point from the above code.
335 <<Scan_Name_Extension
>>
337 -- Character literal used as name cannot be extended. Also this
338 -- cannot be a call, since the name for a call must be a designator.
339 -- Return in these cases, or if there is no name extension
341 if Token
not in Token_Class_Namext
342 or else Prev_Token
= Tok_Char_Literal
344 Expr_Form
:= EF_Name
;
348 -- Merge here when we know there is a name extension
350 <<Scan_Name_Extension_OK
>>
352 if Token
= Tok_Left_Paren
then
353 Scan
; -- past left paren
354 goto Scan_Name_Extension_Left_Paren
;
356 elsif Token
= Tok_Apostrophe
then
357 Save_Scan_State
(Scan_State
); -- at apostrophe
358 Scan
; -- past apostrophe
359 goto Scan_Name_Extension_Apostrophe
;
361 else -- Token = Tok_Dot
362 Save_Scan_State
(Scan_State
); -- at dot
364 goto Scan_Name_Extension_Dot
;
367 -- Case of name extended by dot (selection), dot is already skipped
368 -- and the scan state at the point of the dot is saved in Scan_State.
370 <<Scan_Name_Extension_Dot
>>
372 -- Explicit dereference case
374 if Token
= Tok_All
then
375 Prefix_Node
:= Name_Node
;
376 Name_Node
:= New_Node
(N_Explicit_Dereference
, Token_Ptr
);
377 Set_Prefix
(Name_Node
, Prefix_Node
);
379 goto Scan_Name_Extension
;
381 -- Selected component case
383 elsif Token
in Token_Class_Name
then
384 Prefix_Node
:= Name_Node
;
385 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
386 Set_Prefix
(Name_Node
, Prefix_Node
);
387 Set_Selector_Name
(Name_Node
, Token_Node
);
388 Scan
; -- past selector
389 goto Scan_Name_Extension
;
391 -- Reserved identifier as selector
393 elsif Is_Reserved_Identifier
then
394 Scan_Reserved_Identifier
(Force_Msg
=> False);
395 Prefix_Node
:= Name_Node
;
396 Name_Node
:= New_Node
(N_Selected_Component
, Prev_Token_Ptr
);
397 Set_Prefix
(Name_Node
, Prefix_Node
);
398 Set_Selector_Name
(Name_Node
, Token_Node
);
399 Scan
; -- past identifier used as selector
400 goto Scan_Name_Extension
;
402 -- If dot is at end of line and followed by nothing legal,
403 -- then assume end of name and quit (dot will be taken as
404 -- an incorrect form of some other punctuation by our caller).
406 elsif Token_Is_At_Start_Of_Line
then
407 Restore_Scan_State
(Scan_State
);
410 -- Here if nothing legal after the dot
413 Error_Msg_AP
("selector expected");
417 -- Here for an apostrophe as name extension. The scan position at the
418 -- apostrophe has already been saved, and the apostrophe scanned out.
420 <<Scan_Name_Extension_Apostrophe
>>
422 Scan_Apostrophe
: declare
423 function Apostrophe_Should_Be_Semicolon
return Boolean;
424 -- Checks for case where apostrophe should probably be
425 -- a semicolon, and if so, gives appropriate message,
426 -- resets the scan pointer to the apostrophe, changes
427 -- the current token to Tok_Semicolon, and returns True.
428 -- Otherwise returns False.
430 ------------------------------------
431 -- Apostrophe_Should_Be_Semicolon --
432 ------------------------------------
434 function Apostrophe_Should_Be_Semicolon
return Boolean is
436 if Token_Is_At_Start_Of_Line
then
437 Restore_Scan_State
(Scan_State
); -- to apostrophe
438 Error_Msg_SC
("|""''"" should be "";""");
439 Token
:= Tok_Semicolon
;
444 end Apostrophe_Should_Be_Semicolon
;
446 -- Start of processing for Scan_Apostrophe
449 -- Check for qualified expression case in Ada 2012 mode
451 if Ada_Version
>= Ada_2012
452 and then Token
in Tok_Left_Paren | Tok_Left_Bracket
454 Name_Node
:= P_Qualified_Expression
(Name_Node
);
455 goto Scan_Name_Extension
;
457 -- If range attribute after apostrophe, then return with Token
458 -- pointing to the apostrophe. Note that in this case the prefix
459 -- need not be a simple name (cases like A.all'range). Similarly
460 -- if there is a left paren after the apostrophe, then we also
461 -- return with Token pointing to the apostrophe (this is the
462 -- aggregate case, or some error case).
464 elsif Token
= Tok_Range
or else Token
= Tok_Left_Paren
then
465 Restore_Scan_State
(Scan_State
); -- to apostrophe
466 Expr_Form
:= EF_Name
;
469 -- Here for cases where attribute designator is an identifier
471 elsif Token
= Tok_Identifier
then
472 Attr_Name
:= Token_Name
;
474 if not Is_Attribute_Name
(Attr_Name
) then
475 if Apostrophe_Should_Be_Semicolon
then
476 Expr_Form
:= EF_Name
;
479 -- Here for a bad attribute name
482 Signal_Bad_Attribute
;
483 Scan
; -- past bad identifier
485 if Token
= Tok_Left_Paren
then
486 Scan
; -- past left paren
489 Discard_Junk_Node
(P_Expression_If_OK
);
490 exit when not Comma_Present
;
501 Style
.Check_Attribute_Name
(False);
504 -- Here for case of attribute designator is not an identifier
507 if Token
= Tok_Delta
then
508 Attr_Name
:= Name_Delta
;
510 elsif Token
= Tok_Digits
then
511 Attr_Name
:= Name_Digits
;
513 elsif Token
= Tok_Access
then
514 Attr_Name
:= Name_Access
;
516 elsif Token
= Tok_Mod
and then Ada_Version
>= Ada_95
then
517 Attr_Name
:= Name_Mod
;
519 elsif Apostrophe_Should_Be_Semicolon
then
520 Expr_Form
:= EF_Name
;
524 Error_Msg_AP
("attribute designator expected");
529 Style
.Check_Attribute_Name
(True);
533 -- We come here with an OK attribute scanned, and corresponding
534 -- Attribute identifier node stored in Ident_Node.
536 Prefix_Node
:= Name_Node
;
537 Name_Node
:= New_Node
(N_Attribute_Reference
, Prev_Token_Ptr
);
538 Scan
; -- past attribute designator
539 Set_Prefix
(Name_Node
, Prefix_Node
);
540 Set_Attribute_Name
(Name_Node
, Attr_Name
);
542 -- Scan attribute arguments/designator. We skip this if we know
543 -- that the attribute cannot have an argument (see documentation
544 -- of Is_Parameterless_Attribute for further details).
546 if Token
= Tok_Left_Paren
548 Is_Parameterless_Attribute
(Get_Attribute_Id
(Attr_Name
))
550 -- Attribute Update contains an array or record association
551 -- list which provides new values for various components or
552 -- elements. The list is parsed as an aggregate, and we get
553 -- better error handling by knowing that in the parser.
555 if Attr_Name
= Name_Update
then
556 Set_Expressions
(Name_Node
, New_List
);
557 Append
(P_Aggregate
, Expressions
(Name_Node
));
559 -- All other cases of parsing attribute arguments
562 Set_Expressions
(Name_Node
, New_List
);
563 Scan
; -- past left paren
567 Expr
: constant Node_Id
:= P_Expression_If_OK
;
571 -- Case of => for named notation
573 if Token
= Tok_Arrow
then
575 -- Named notation allowed only for the special
576 -- case of System'Restriction_Set (No_Dependence =>
577 -- unit_NAME), in which case construct a parameter
578 -- assocation node and append to the arguments.
580 if Attr_Name
= Name_Restriction_Set
581 and then Nkind
(Expr
) = N_Identifier
582 and then Chars
(Expr
) = Name_No_Dependence
586 Append_To
(Expressions
(Name_Node
),
587 Make_Parameter_Association
(Sloc
(Rnam
),
588 Selector_Name
=> Expr
,
589 Explicit_Actual_Parameter
=> Rnam
));
592 -- For all other cases named notation is illegal
596 ("named parameters not permitted "
598 Scan
; -- past junk arrow
601 -- Here for normal case (not => for named parameter)
604 -- Special handling for 'Image in Ada 2012, where
605 -- the attribute can be parameterless and its value
606 -- can be the prefix of a slice. Rewrite name as a
607 -- slice, Expr is its low bound.
609 if Token
= Tok_Dot_Dot
610 and then Attr_Name
= Name_Image
611 and then Ada_Version
>= Ada_2012
613 Set_Expressions
(Name_Node
, No_List
);
614 Prefix_Node
:= Name_Node
;
616 New_Node
(N_Slice
, Sloc
(Prefix_Node
));
617 Set_Prefix
(Name_Node
, Prefix_Node
);
618 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
619 Set_Low_Bound
(Range_Node
, Expr
);
621 Expr_Node
:= P_Expression
;
622 Check_Simple_Expression
(Expr_Node
);
623 Set_High_Bound
(Range_Node
, Expr_Node
);
624 Set_Discrete_Range
(Name_Node
, Range_Node
);
627 goto Scan_Name_Extension
;
630 Append
(Expr
, Expressions
(Name_Node
));
631 exit when not Comma_Present
;
641 goto Scan_Name_Extension
;
644 -- Here for left parenthesis extending name (left paren skipped)
646 <<Scan_Name_Extension_Left_Paren
>>
648 -- We now have to scan through a list of items, terminated by a
649 -- right parenthesis. The scan is handled by a finite state
650 -- machine. The possibilities are:
654 -- This is a slice. This case is handled in LP_State_Init
656 -- (expression, expression, ..)
658 -- This is interpreted as an indexed component, i.e. as a
659 -- case of a name which can be extended in the normal manner.
660 -- This case is handled by LP_State_Name or LP_State_Expr.
662 -- Note: if and case expressions (without an extra level of
663 -- parentheses) are permitted in this context).
665 -- (..., identifier => expression , ...)
667 -- If there is at least one occurrence of identifier => (but
668 -- none of the other cases apply), then we have a call.
670 -- Test for Id => case
672 if Token
= Tok_Identifier
then
673 Save_Scan_State
(Scan_State
); -- at Id
676 -- Test for => (allow := as an error substitute)
678 if Token
= Tok_Arrow
or else Token
= Tok_Colon_Equal
then
679 Restore_Scan_State
(Scan_State
); -- to Id
680 Arg_List
:= New_List
;
684 Restore_Scan_State
(Scan_State
); -- to Id
688 -- Here we have an expression after all
690 Expr_Node
:= P_Expression_Or_Range_Attribute_If_OK
;
692 -- Check cases of discrete range for a slice
694 -- First possibility: Range_Attribute_Reference
696 if Expr_Form
= EF_Range_Attr
then
697 Range_Node
:= Expr_Node
;
699 -- Second possibility: Simple_expression .. Simple_expression
701 elsif Token
= Tok_Dot_Dot
then
702 Check_Simple_Expression
(Expr_Node
);
703 Range_Node
:= New_Node
(N_Range
, Token_Ptr
);
704 Set_Low_Bound
(Range_Node
, Expr_Node
);
706 Expr_Node
:= P_Expression
;
707 Check_Simple_Expression
(Expr_Node
);
708 Set_High_Bound
(Range_Node
, Expr_Node
);
710 -- Third possibility: Type_name range Range
712 elsif Token
= Tok_Range
then
713 if Expr_Form
/= EF_Simple_Name
then
714 Error_Msg_SC
("subtype mark must precede RANGE");
718 Range_Node
:= P_Subtype_Indication
(Expr_Node
);
720 -- Otherwise we just have an expression. It is true that we might
721 -- have a subtype mark without a range constraint but this case
722 -- is syntactically indistinguishable from the expression case.
725 Arg_List
:= New_List
;
729 -- Fall through here with unmistakable Discrete range scanned,
730 -- which means that we definitely have the case of a slice. The
731 -- Discrete range is in Range_Node.
733 if Token
= Tok_Comma
then
734 Error_Msg_SC
("slice cannot have more than one dimension");
737 elsif Token
/= Tok_Right_Paren
then
738 if Token
= Tok_Arrow
then
740 -- This may be an aggregate that is missing a qualification
743 ("context of aggregate must be a qualified expression");
752 Scan
; -- past right paren
753 Prefix_Node
:= Name_Node
;
754 Name_Node
:= New_Node
(N_Slice
, Sloc
(Prefix_Node
));
755 Set_Prefix
(Name_Node
, Prefix_Node
);
756 Set_Discrete_Range
(Name_Node
, Range_Node
);
758 -- An operator node is legal as a prefix to other names,
759 -- but not for a slice.
761 if Nkind
(Prefix_Node
) = N_Operator_Symbol
then
762 Error_Msg_N
("illegal prefix for slice", Prefix_Node
);
765 -- If we have a name extension, go scan it
767 if Token
in Token_Class_Namext
then
768 goto Scan_Name_Extension_OK
;
770 -- Otherwise return (a slice is a name, but is not a call)
773 Expr_Form
:= EF_Name
;
778 -- In LP_State_Expr, we have scanned one or more expressions, and
779 -- so we have a call or an indexed component which is a name. On
780 -- entry we have the expression just scanned in Expr_Node and
781 -- Arg_List contains the list of expressions encountered so far
784 Append
(Expr_Node
, Arg_List
);
786 if Token
= Tok_Arrow
then
788 ("expect identifier in parameter association", Sloc
(Expr_Node
));
791 elsif not Comma_Present
then
794 Prefix_Node
:= Name_Node
;
795 Name_Node
:= New_Node
(N_Indexed_Component
, Sloc
(Prefix_Node
));
796 Set_Prefix
(Name_Node
, Prefix_Node
);
797 Set_Expressions
(Name_Node
, Arg_List
);
799 goto Scan_Name_Extension
;
802 -- Comma present (and scanned out), test for identifier => case
803 -- Test for identifier => case
805 if Token
= Tok_Identifier
then
806 Save_Scan_State
(Scan_State
); -- at Id
809 -- Test for => (allow := as error substitute)
811 if Token
= Tok_Arrow
or else Token
= Tok_Colon_Equal
then
812 Restore_Scan_State
(Scan_State
); -- to Id
815 -- Otherwise it's just an expression after all, so backup
818 Restore_Scan_State
(Scan_State
); -- to Id
822 -- Here we have an expression after all, so stay in this state
824 Expr_Node
:= P_Expression_If_OK
;
827 -- LP_State_Call corresponds to the situation in which at least one
828 -- instance of Id => Expression has been encountered, so we know that
829 -- we do not have a name, but rather a call. We enter it with the
830 -- scan pointer pointing to the next argument to scan, and Arg_List
831 -- containing the list of arguments scanned so far.
835 -- Test for case of Id => Expression (named parameter)
837 if Token
= Tok_Identifier
then
838 Save_Scan_State
(Scan_State
); -- at Id
839 Ident_Node
:= Token_Node
;
842 -- Deal with => (allow := as incorrect substitute)
844 if Token
= Tok_Arrow
or else Token
= Tok_Colon_Equal
then
845 Arg_Node
:= New_Node
(N_Parameter_Association
, Prev_Token_Ptr
);
846 Set_Selector_Name
(Arg_Node
, Ident_Node
);
848 Set_Explicit_Actual_Parameter
(Arg_Node
, P_Expression
);
849 Append
(Arg_Node
, Arg_List
);
851 -- If a comma follows, go back and scan next entry
853 if Comma_Present
then
856 -- Otherwise we have the end of a call
859 Prefix_Node
:= Name_Node
;
860 Name_Node
:= New_Node
(N_Function_Call
, Sloc
(Prefix_Node
));
861 Set_Name
(Name_Node
, Prefix_Node
);
862 Set_Parameter_Associations
(Name_Node
, Arg_List
);
865 if Token
in Token_Class_Namext
then
866 goto Scan_Name_Extension_OK
;
868 -- This is a case of a call which cannot be a name
871 Expr_Form
:= EF_Name
;
876 -- Not named parameter: Id started an expression after all
879 Restore_Scan_State
(Scan_State
); -- to Id
883 -- Here if entry did not start with Id => which means that it
884 -- is a positional parameter, which is not allowed, since we
885 -- have seen at least one named parameter already.
888 ("positional parameter association " &
889 "not allowed after named one");
891 Expr_Node
:= P_Expression_If_OK
;
893 -- Leaving the '>' in an association is not unusual, so suggest
896 if Nkind
(Expr_Node
) = N_Op_Eq
then
897 Error_Msg_N
("\maybe `='>` was intended", Expr_Node
);
900 -- We go back to scanning out expressions, so that we do not get
901 -- multiple error messages when several positional parameters
902 -- follow a named parameter.
906 -- End of treatment for name extensions starting with left paren
908 -- End of loop through name extensions
912 -- This function parses a restricted form of Names which are either
913 -- designators, or designators preceded by a sequence of prefixes
914 -- that are direct names.
916 -- Error recovery: cannot raise Error_Resync
918 function P_Function_Name
return Node_Id
is
919 Designator_Node
: Node_Id
;
920 Prefix_Node
: Node_Id
;
921 Selector_Node
: Node_Id
;
922 Dot_Sloc
: Source_Ptr
:= No_Location
;
925 -- Prefix_Node is set to the gathered prefix so far, Empty means that
926 -- no prefix has been scanned. This allows us to build up the result
927 -- in the required right recursive manner.
929 Prefix_Node
:= Empty
;
931 -- Loop through prefixes
934 Designator_Node
:= Token_Node
;
936 if Token
not in Token_Class_Desig
then
937 return P_Identifier
; -- let P_Identifier issue the error message
939 else -- Token in Token_Class_Desig
940 Scan
; -- past designator
941 exit when Token
/= Tok_Dot
;
944 -- Here at a dot, with token just before it in Designator_Node
946 if No
(Prefix_Node
) then
947 Prefix_Node
:= Designator_Node
;
949 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
950 Set_Prefix
(Selector_Node
, Prefix_Node
);
951 Set_Selector_Name
(Selector_Node
, Designator_Node
);
952 Prefix_Node
:= Selector_Node
;
955 Dot_Sloc
:= Token_Ptr
;
959 -- Fall out of the loop having just scanned a designator
961 if No
(Prefix_Node
) then
962 return Designator_Node
;
964 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
965 Set_Prefix
(Selector_Node
, Prefix_Node
);
966 Set_Selector_Name
(Selector_Node
, Designator_Node
);
967 return Selector_Node
;
975 -- This function parses a restricted form of Names which are either
976 -- identifiers, or identifiers preceded by a sequence of prefixes
977 -- that are direct names.
979 -- Error recovery: cannot raise Error_Resync
981 function P_Qualified_Simple_Name
return Node_Id
is
982 Designator_Node
: Node_Id
;
983 Prefix_Node
: Node_Id
;
984 Selector_Node
: Node_Id
;
985 Dot_Sloc
: Source_Ptr
:= No_Location
;
988 -- Prefix node is set to the gathered prefix so far, Empty means that
989 -- no prefix has been scanned. This allows us to build up the result
990 -- in the required right recursive manner.
992 Prefix_Node
:= Empty
;
994 -- Loop through prefixes
997 Designator_Node
:= Token_Node
;
999 if Token
= Tok_Identifier
then
1000 Scan
; -- past identifier
1001 exit when Token
/= Tok_Dot
;
1003 elsif Token
not in Token_Class_Desig
then
1004 return P_Identifier
; -- let P_Identifier issue the error message
1007 Scan
; -- past designator
1009 if Token
/= Tok_Dot
then
1010 Error_Msg_SP
("identifier expected");
1015 -- Here at a dot, with token just before it in Designator_Node
1017 if No
(Prefix_Node
) then
1018 Prefix_Node
:= Designator_Node
;
1020 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1021 Set_Prefix
(Selector_Node
, Prefix_Node
);
1022 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1023 Prefix_Node
:= Selector_Node
;
1026 Dot_Sloc
:= Token_Ptr
;
1030 -- Fall out of the loop having just scanned an identifier
1032 if No
(Prefix_Node
) then
1033 return Designator_Node
;
1035 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1036 Set_Prefix
(Selector_Node
, Prefix_Node
);
1037 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1038 return Selector_Node
;
1042 when Error_Resync
=>
1044 end P_Qualified_Simple_Name
;
1046 -- This procedure differs from P_Qualified_Simple_Name only in that it
1047 -- raises Error_Resync if any error is encountered. It only returns after
1048 -- scanning a valid qualified simple name.
1050 -- Error recovery: can raise Error_Resync
1052 function P_Qualified_Simple_Name_Resync
return Node_Id
is
1053 Designator_Node
: Node_Id
;
1054 Prefix_Node
: Node_Id
;
1055 Selector_Node
: Node_Id
;
1056 Dot_Sloc
: Source_Ptr
:= No_Location
;
1059 Prefix_Node
:= Empty
;
1061 -- Loop through prefixes
1064 Designator_Node
:= Token_Node
;
1066 if Token
= Tok_Identifier
then
1067 Scan
; -- past identifier
1068 exit when Token
/= Tok_Dot
;
1070 elsif Token
not in Token_Class_Desig
then
1071 Discard_Junk_Node
(P_Identifier
); -- to issue the error message
1075 Scan
; -- past designator
1077 if Token
/= Tok_Dot
then
1078 Error_Msg_SP
("identifier expected");
1083 -- Here at a dot, with token just before it in Designator_Node
1085 if No
(Prefix_Node
) then
1086 Prefix_Node
:= Designator_Node
;
1088 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1089 Set_Prefix
(Selector_Node
, Prefix_Node
);
1090 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1091 Prefix_Node
:= Selector_Node
;
1094 Dot_Sloc
:= Token_Ptr
;
1095 Scan
; -- past period
1098 -- Fall out of the loop having just scanned an identifier
1100 if No
(Prefix_Node
) then
1101 return Designator_Node
;
1103 Selector_Node
:= New_Node
(N_Selected_Component
, Dot_Sloc
);
1104 Set_Prefix
(Selector_Node
, Prefix_Node
);
1105 Set_Selector_Name
(Selector_Node
, Designator_Node
);
1106 return Selector_Node
;
1108 end P_Qualified_Simple_Name_Resync
;
1110 ----------------------
1111 -- 4.1 Direct_Name --
1112 ----------------------
1114 -- Parsed by P_Name and other functions in section 4.1
1120 -- Parsed by P_Name (4.1)
1122 -------------------------------
1123 -- 4.1 Explicit Dereference --
1124 -------------------------------
1126 -- Parsed by P_Name (4.1)
1128 -------------------------------
1129 -- 4.1 Implicit_Dereference --
1130 -------------------------------
1132 -- Parsed by P_Name (4.1)
1134 ----------------------------
1135 -- 4.1 Indexed Component --
1136 ----------------------------
1138 -- Parsed by P_Name (4.1)
1144 -- Parsed by P_Name (4.1)
1146 -----------------------------
1147 -- 4.1 Selected_Component --
1148 -----------------------------
1150 -- Parsed by P_Name (4.1)
1152 ------------------------
1153 -- 4.1 Selector Name --
1154 ------------------------
1156 -- Parsed by P_Name (4.1)
1158 ------------------------------
1159 -- 4.1 Attribute Reference --
1160 ------------------------------
1162 -- Parsed by P_Name (4.1)
1164 -------------------------------
1165 -- 4.1 Attribute Designator --
1166 -------------------------------
1168 -- Parsed by P_Name (4.1)
1170 --------------------------------------
1171 -- 4.1.4 Range Attribute Reference --
1172 --------------------------------------
1174 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1176 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1178 -- In the grammar, a RANGE attribute is simply a name, but its use is
1179 -- highly restricted, so in the parser, we do not regard it as a name.
1180 -- Instead, P_Name returns without scanning the 'RANGE part of the
1181 -- attribute, and the caller uses the following function to construct
1182 -- a range attribute in places where it is appropriate.
1184 -- Note that RANGE here is treated essentially as an identifier,
1185 -- rather than a reserved word.
1187 -- The caller has parsed the prefix, i.e. a name, and Token points to
1188 -- the apostrophe. The token after the apostrophe is known to be RANGE
1189 -- at this point. The prefix node becomes the prefix of the attribute.
1191 -- Error_Recovery: Cannot raise Error_Resync
1193 function P_Range_Attribute_Reference
1194 (Prefix_Node
: Node_Id
)
1197 Attr_Node
: Node_Id
;
1200 Attr_Node
:= New_Node
(N_Attribute_Reference
, Token_Ptr
);
1201 Set_Prefix
(Attr_Node
, Prefix_Node
);
1202 Scan
; -- past apostrophe
1205 Style
.Check_Attribute_Name
(True);
1208 Set_Attribute_Name
(Attr_Node
, Name_Range
);
1211 if Token
= Tok_Left_Paren
then
1212 Scan
; -- past left paren
1213 Set_Expressions
(Attr_Node
, New_List
(P_Expression_If_OK
));
1218 end P_Range_Attribute_Reference
;
1220 -------------------------------------
1221 -- P_Reduction_Attribute_Reference --
1222 -------------------------------------
1224 function P_Reduction_Attribute_Reference
(S
: Node_Id
)
1227 Attr_Node
: Node_Id
;
1228 Attr_Name
: Name_Id
;
1231 Attr_Name
:= Token_Name
;
1232 Scan
; -- past Reduce
1233 Attr_Node
:= New_Node
(N_Attribute_Reference
, Token_Ptr
);
1234 Set_Attribute_Name
(Attr_Node
, Attr_Name
);
1235 if Attr_Name
/= Name_Reduce
then
1236 Error_Msg
("Reduce attribute expected", Prev_Token_Ptr
);
1239 Set_Prefix
(Attr_Node
, S
);
1240 Set_Expressions
(Attr_Node
, New_List
);
1242 Append
(P_Name
, Expressions
(Attr_Node
));
1244 Append
(P_Expression
, Expressions
(Attr_Node
));
1248 end P_Reduction_Attribute_Reference
;
1250 ---------------------------------------
1251 -- 4.1.4 Range Attribute Designator --
1252 ---------------------------------------
1254 -- Parsed by P_Range_Attribute_Reference (4.4)
1256 ---------------------------------------------
1257 -- 4.1.4 (2) Reduction_Attribute_Reference --
1258 ---------------------------------------------
1260 -- parsed by P_Reduction_Attribute_Reference
1262 --------------------
1264 --------------------
1266 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1268 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1269 -- an aggregate is known to be required (code statement, extension
1270 -- aggregate), in which cases this routine performs the necessary check
1271 -- that we have an aggregate rather than a parenthesized expression
1273 -- Error recovery: can raise Error_Resync
1275 function P_Aggregate
return Node_Id
is
1276 Aggr_Sloc
: constant Source_Ptr
:= Token_Ptr
;
1277 Aggr_Node
: constant Node_Id
:= P_Aggregate_Or_Paren_Expr
;
1280 if Nkind
(Aggr_Node
) /= N_Aggregate
1282 Nkind
(Aggr_Node
) /= N_Extension_Aggregate
1283 and then Ada_Version
< Ada_2022
1286 ("aggregate may not have single positional component", Aggr_Sloc
);
1293 ------------------------------------------------
1294 -- 4.3 Aggregate or Parenthesized Expression --
1295 ------------------------------------------------
1297 -- This procedure parses out either an aggregate or a parenthesized
1298 -- expression (these two constructs are closely related, since a
1299 -- parenthesized expression looks like an aggregate with a single
1300 -- positional component).
1303 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1305 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1307 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1308 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1311 -- RECORD_COMPONENT_ASSOCIATION ::=
1312 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1314 -- COMPONENT_CHOICE_LIST ::=
1315 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1318 -- EXTENSION_AGGREGATE ::=
1319 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1321 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1323 -- ARRAY_AGGREGATE ::=
1324 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1326 -- POSITIONAL_ARRAY_AGGREGATE ::=
1327 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1328 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1329 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1331 -- NAMED_ARRAY_AGGREGATE ::=
1332 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1334 -- PRIMARY ::= (EXPRESSION);
1336 -- Error recovery: can raise Error_Resync
1338 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1339 -- to Ada 2005 limited aggregates (AI-287)
1341 function P_Aggregate_Or_Paren_Expr
return Node_Id
is
1342 Aggregate_Node
: Node_Id
;
1343 Expr_List
: List_Id
;
1344 Assoc_List
: List_Id
;
1345 Expr_Node
: Node_Id
;
1346 Lparen_Sloc
: Source_Ptr
;
1347 Scan_State
: Saved_Scan_State
;
1349 procedure Box_Error
;
1350 -- Called if <> is encountered as positional aggregate element. Issues
1351 -- error message and sets Expr_Node to Error.
1353 function Is_Quantified_Expression
return Boolean;
1354 -- The presence of iterated component associations requires a one
1355 -- token lookahead to distinguish it from quantified expressions.
1361 procedure Box_Error
is
1363 Error_Msg_Ada_2005_Extension
("'<'> in aggregate");
1365 -- Ada 2005 (AI-287): The box notation is allowed only with named
1366 -- notation because positional notation might be error prone. For
1367 -- example, in "(X, <>, Y, <>)", there is no type associated with
1368 -- the boxes, so you might not be leaving out the components you
1369 -- thought you were leaving out.
1371 Error_Msg_SC
("(Ada 2005) box only allowed with named notation");
1376 ------------------------------
1377 -- Is_Quantified_Expression --
1378 ------------------------------
1380 function Is_Quantified_Expression
return Boolean is
1382 Scan_State
: Saved_Scan_State
;
1385 Save_Scan_State
(Scan_State
);
1387 Maybe
:= Token
= Tok_All
or else Token
= Tok_Some
;
1388 Restore_Scan_State
(Scan_State
); -- to FOR
1390 end Is_Quantified_Expression
;
1392 Start_Token
: constant Token_Type
:= Token
;
1393 -- Used to prevent mismatches (...] and [...)
1395 -- Start of processing for P_Aggregate_Or_Paren_Expr
1398 Lparen_Sloc
:= Token_Ptr
;
1399 if Token
= Tok_Left_Bracket
then
1402 -- Special case for null aggregate in Ada 2022
1404 if Token
= Tok_Right_Bracket
then
1406 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1407 Set_Expressions
(Aggregate_Node
, New_List
);
1408 Set_Component_Associations
(Aggregate_Node
, New_List
);
1409 Set_Is_Homogeneous_Aggregate
(Aggregate_Node
);
1410 return Aggregate_Node
;
1416 -- Note on parentheses count. For cases like an if expression, the
1417 -- parens here really count as real parentheses for the paren count,
1418 -- so we adjust the paren count accordingly after scanning the expr.
1422 if Token
= Tok_If
then
1423 Expr_Node
:= P_If_Expression
;
1425 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1430 elsif Token
= Tok_Case
then
1431 Expr_Node
:= P_Case_Expression
;
1433 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1436 -- Quantified expression
1438 elsif Token
= Tok_For
and then Is_Quantified_Expression
then
1439 Expr_Node
:= P_Quantified_Expression
;
1441 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1444 -- Note: the mechanism used here of rescanning the initial expression
1445 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1446 -- out the discrete choice list.
1448 -- Deal with expression and extension aggregates first
1450 elsif Token
/= Tok_Others
then
1451 Save_Scan_State
(Scan_State
); -- at start of expression
1453 -- Deal with (NULL RECORD)
1455 if Token
= Tok_Null
then
1458 if Token
= Tok_Record
then
1459 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1460 Set_Null_Record_Present
(Aggregate_Node
, True);
1461 Scan
; -- past RECORD
1463 return Aggregate_Node
;
1465 Restore_Scan_State
(Scan_State
); -- to NULL that must be expr
1468 elsif Token
= Tok_For
then
1469 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1470 Expr_Node
:= P_Iterated_Component_Association
;
1474 -- Scan expression, handling box appearing as positional argument
1476 if Token
= Tok_Box
then
1479 Expr_Node
:= P_Expression_Or_Range_Attribute_If_OK
;
1482 -- Extension or Delta aggregate
1484 if Token
= Tok_With
then
1485 if Nkind
(Expr_Node
) = N_Attribute_Reference
1486 and then Attribute_Name
(Expr_Node
) = Name_Range
1488 Bad_Range_Attribute
(Sloc
(Expr_Node
));
1492 if Ada_Version
= Ada_83
then
1493 Error_Msg_SC
("(Ada 83) extension aggregate not allowed");
1497 if Token
= Tok_Delta
then
1499 Aggregate_Node
:= New_Node
(N_Delta_Aggregate
, Lparen_Sloc
);
1500 Set_Expression
(Aggregate_Node
, Expr_Node
);
1506 Aggregate_Node
:= New_Node
(N_Extension_Aggregate
, Lparen_Sloc
);
1507 Set_Ancestor_Part
(Aggregate_Node
, Expr_Node
);
1510 -- Deal with WITH NULL RECORD case
1512 if Token
= Tok_Null
then
1513 Save_Scan_State
(Scan_State
); -- at NULL
1516 if Token
= Tok_Record
then
1517 Scan
; -- past RECORD
1518 Set_Null_Record_Present
(Aggregate_Node
, True);
1520 return Aggregate_Node
;
1523 Restore_Scan_State
(Scan_State
); -- to NULL that must be expr
1527 if Token
/= Tok_Others
then
1528 Save_Scan_State
(Scan_State
);
1529 Expr_Node
:= P_Expression
;
1536 elsif Token
= Tok_Right_Paren
or else Token
in Token_Class_Eterm
then
1537 if Nkind
(Expr_Node
) = N_Attribute_Reference
1538 and then Attribute_Name
(Expr_Node
) = Name_Range
1541 ("|parentheses not allowed for range attribute", Lparen_Sloc
);
1542 Scan
; -- past right paren
1546 -- Bump paren count of expression
1548 if Expr_Node
/= Error
then
1549 Set_Paren_Count
(Expr_Node
, Paren_Count
(Expr_Node
) + 1);
1552 T_Right_Paren
; -- past right paren (error message if none)
1558 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1564 Aggregate_Node
:= New_Node
(N_Aggregate
, Lparen_Sloc
);
1568 -- Prepare to scan list of component associations
1570 Expr_List
:= No_List
; -- don't set yet, maybe all named entries
1571 Assoc_List
:= No_List
; -- don't set yet, maybe all positional entries
1573 -- This loop scans through component associations. On entry to the
1574 -- loop, an expression has been scanned at the start of the current
1575 -- association unless initial token was OTHERS, in which case
1576 -- Expr_Node is set to Empty.
1579 -- Deal with others association first. This is a named association
1581 if No
(Expr_Node
) then
1582 Append_New
(P_Record_Or_Array_Component_Association
, Assoc_List
);
1584 -- Improper use of WITH
1586 elsif Token
= Tok_With
then
1587 Error_Msg_SC
("WITH must be preceded by single expression in " &
1588 "extension aggregate");
1591 -- Range attribute can only appear as part of a discrete choice list
1593 elsif Nkind
(Expr_Node
) = N_Attribute_Reference
1594 and then Attribute_Name
(Expr_Node
) = Name_Range
1595 and then Token
/= Tok_Arrow
1596 and then Token
/= Tok_Vertical_Bar
1598 Bad_Range_Attribute
(Sloc
(Expr_Node
));
1601 -- Assume positional case if comma, right paren, or literal or
1602 -- identifier or OTHERS follows (the latter cases are missing
1603 -- comma cases). Also assume positional if a semicolon follows,
1604 -- which can happen if there are missing parens.
1605 -- In Ada 2012 and 2022 an iterated association can appear.
1607 elsif Nkind
(Expr_Node
) in
1608 N_Iterated_Component_Association | N_Iterated_Element_Association
1610 Append_New
(Expr_Node
, Assoc_List
);
1612 elsif Token
= Tok_Comma
1613 or else Token
= Tok_Right_Paren
1614 or else Token
= Tok_Others
1615 or else Token
in Token_Class_Lit_Or_Name
1616 or else Token
= Tok_Semicolon
1618 if Present
(Assoc_List
) then
1619 Error_Msg_BC
-- CODEFIX
1620 ("""='>"" expected (positional association cannot follow "
1621 & "named association)");
1624 Append_New
(Expr_Node
, Expr_List
);
1626 -- Check for aggregate followed by left parent, maybe missing comma
1628 elsif Nkind
(Expr_Node
) = N_Aggregate
1629 and then Token
= Tok_Left_Paren
1633 Append_New
(Expr_Node
, Expr_List
);
1635 elsif Token
= Tok_Right_Bracket
then
1636 Append_New
(Expr_Node
, Expr_List
);
1639 -- Anything else is assumed to be a named association
1642 Restore_Scan_State
(Scan_State
); -- to start of expression
1644 Append_New
(P_Record_Or_Array_Component_Association
, Assoc_List
);
1647 exit when not Comma_Present
;
1649 -- If we are at an expression terminator, something is seriously
1650 -- wrong, so let's get out now, before we start eating up stuff
1651 -- that doesn't belong to us.
1653 if Token
in Token_Class_Eterm
and then Token
/= Tok_For
then
1655 ("expecting expression or component association");
1659 -- Deal with misused box
1661 if Token
= Tok_Box
then
1664 -- Otherwise initiate for reentry to top of loop by scanning an
1665 -- initial expression, unless the first token is OTHERS or FOR,
1666 -- which indicates an iterated component association.
1668 elsif Token
= Tok_Others
then
1671 elsif Token
= Tok_For
then
1672 Expr_Node
:= P_Iterated_Component_Association
;
1675 Save_Scan_State
(Scan_State
); -- at start of expression
1676 Expr_Node
:= P_Expression_Or_Range_Attribute_If_OK
;
1681 -- All component associations (positional and named) have been scanned.
1682 -- Scan ] or ) based on Start_Token.
1685 when Tok_Left_Bracket
=>
1687 Set_Component_Associations
(Aggregate_Node
, Assoc_List
);
1688 Set_Is_Homogeneous_Aggregate
(Aggregate_Node
);
1691 if Token
= Tok_Apostrophe
then
1694 if Token
= Tok_Identifier
then
1695 return P_Reduction_Attribute_Reference
(Aggregate_Node
);
1698 when Tok_Left_Paren
=>
1699 if Nkind
(Aggregate_Node
) = N_Aggregate
then
1700 Set_Is_Parenthesis_Aggregate
(Aggregate_Node
);
1704 when others => raise Program_Error
;
1707 if Nkind
(Aggregate_Node
) /= N_Delta_Aggregate
then
1708 Set_Expressions
(Aggregate_Node
, Expr_List
);
1711 Set_Component_Associations
(Aggregate_Node
, Assoc_List
);
1712 return Aggregate_Node
;
1713 end P_Aggregate_Or_Paren_Expr
;
1715 ------------------------------------------------
1716 -- 4.3 Record or Array Component Association --
1717 ------------------------------------------------
1719 -- RECORD_COMPONENT_ASSOCIATION ::=
1720 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1721 -- | COMPONENT_CHOICE_LIST => <>
1723 -- COMPONENT_CHOICE_LIST =>
1724 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1727 -- ARRAY_COMPONENT_ASSOCIATION ::=
1728 -- DISCRETE_CHOICE_LIST => EXPRESSION
1729 -- | DISCRETE_CHOICE_LIST => <>
1730 -- | ITERATED_COMPONENT_ASSOCIATION
1732 -- Note: this routine only handles the named cases, including others.
1733 -- Cases where the component choice list is not present have already
1734 -- been handled directly.
1736 -- Error recovery: can raise Error_Resync
1738 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1739 -- rules have been extended to give support to Ada 2005 limited
1740 -- aggregates (AI-287)
1742 function P_Record_Or_Array_Component_Association
return Node_Id
is
1743 Assoc_Node
: Node_Id
;
1744 Box_Present
: Boolean := False;
1745 Box_With_Identifier_Present
: Boolean := False;
1747 -- A loop indicates an iterated_component_association
1749 if Token
= Tok_For
then
1750 return P_Iterated_Component_Association
;
1753 Assoc_Node
:= New_Node
(N_Component_Association
, Token_Ptr
);
1754 Set_Binding_Chars
(Assoc_Node
, No_Name
);
1756 Set_Choices
(Assoc_Node
, P_Discrete_Choice_List
);
1757 Set_Sloc
(Assoc_Node
, Token_Ptr
);
1760 if Token
= Tok_Box
then
1762 -- Ada 2005(AI-287): The box notation is used to indicate the
1763 -- default initialization of aggregate components
1765 Error_Msg_Ada_2005_Extension
("component association with '<'>");
1767 Box_Present
:= True;
1768 Set_Box_Present
(Assoc_Node
);
1770 elsif Token
= Tok_Less
then
1772 Scan_State
: Saved_Scan_State
;
1775 Save_Scan_State
(Scan_State
);
1777 if Token
= Tok_Identifier
then
1778 Id
:= P_Defining_Identifier
;
1779 if Token
= Tok_Greater
then
1780 if Extensions_Allowed
then
1781 Set_Box_Present
(Assoc_Node
);
1782 Set_Binding_Chars
(Assoc_Node
, Chars
(Id
));
1783 Box_Present
:= True;
1784 Box_With_Identifier_Present
:= True;
1787 Error_Msg_GNAT_Extension
1788 ("identifier within box", Token_Ptr
);
1789 Box_Present
:= True;
1790 -- Avoid cascading errors by ignoring the identifier
1794 if not Box_Present
then
1795 -- it wasn't an "is <identifier>", so restore.
1796 Restore_Scan_State
(Scan_State
);
1801 if not Box_Present
then
1802 Set_Expression
(Assoc_Node
, P_Expression
);
1805 -- Check for "is <identifier>" for aggregate that is part of
1806 -- a pattern for a general case statement.
1808 if Token
= Tok_Is
then
1810 Scan_State
: Saved_Scan_State
;
1813 Save_Scan_State
(Scan_State
);
1815 if Token
= Tok_Identifier
then
1816 Id
:= P_Defining_Identifier
;
1818 if not Extensions_Allowed
then
1819 Error_Msg_GNAT_Extension
1820 ("IS following component association", Token_Ptr
);
1821 elsif Box_With_Identifier_Present
then
1823 ("Both identifier-in-box and trailing identifier"
1824 & " specified for one component association",
1827 Set_Binding_Chars
(Assoc_Node
, Chars
(Id
));
1830 -- It wasn't an "is <identifier>", so restore.
1831 Restore_Scan_State
(Scan_State
);
1837 end P_Record_Or_Array_Component_Association
;
1839 -----------------------------
1840 -- 4.3.1 Record Aggregate --
1841 -----------------------------
1843 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1844 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1846 ----------------------------------------------
1847 -- 4.3.1 Record Component Association List --
1848 ----------------------------------------------
1850 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1852 ----------------------------------
1853 -- 4.3.1 Component Choice List --
1854 ----------------------------------
1856 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1858 --------------------------------
1859 -- 4.3.1 Extension Aggregate --
1860 --------------------------------
1862 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1864 --------------------------
1865 -- 4.3.1 Ancestor Part --
1866 --------------------------
1868 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1870 ----------------------------
1871 -- 4.3.1 Array Aggregate --
1872 ----------------------------
1874 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1876 ---------------------------------------
1877 -- 4.3.1 Positional Array Aggregate --
1878 ---------------------------------------
1880 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1882 ----------------------------------
1883 -- 4.3.1 Named Array Aggregate --
1884 ----------------------------------
1886 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1888 ----------------------------------------
1889 -- 4.3.1 Array Component Association --
1890 ----------------------------------------
1892 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1894 ---------------------
1895 -- 4.4 Expression --
1896 ---------------------
1898 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1901 -- RELATION {LOGICAL_OPERATOR RELATION}
1903 -- CHOICE_EXPRESSION ::=
1904 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1906 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1908 -- On return, Expr_Form indicates the categorization of the expression
1909 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1910 -- an error message is given, and Error is returned).
1912 -- Error recovery: cannot raise Error_Resync
1914 function P_Expression
return Node_Id
is
1915 Logical_Op
: Node_Kind
;
1916 Prev_Logical_Op
: Node_Kind
;
1917 Op_Location
: Source_Ptr
;
1922 Node1
:= P_Relation
;
1924 if Token
in Token_Class_Logop
then
1925 Prev_Logical_Op
:= N_Empty
;
1928 Op_Location
:= Token_Ptr
;
1929 Logical_Op
:= P_Logical_Operator
;
1931 if Prev_Logical_Op
/= N_Empty
and then
1932 Logical_Op
/= Prev_Logical_Op
1935 ("mixed logical operators in expression", Op_Location
);
1936 Prev_Logical_Op
:= N_Empty
;
1938 Prev_Logical_Op
:= Logical_Op
;
1942 Node1
:= New_Op_Node
(Logical_Op
, Op_Location
);
1943 Set_Left_Opnd
(Node1
, Node2
);
1944 Set_Right_Opnd
(Node1
, P_Relation
);
1946 -- Check for case of errant comma or semicolon
1948 if Token
= Tok_Comma
or else Token
= Tok_Semicolon
then
1950 Com
: constant Boolean := Token
= Tok_Comma
;
1951 Scan_State
: Saved_Scan_State
;
1955 Save_Scan_State
(Scan_State
); -- at comma/semicolon
1956 Scan
; -- past comma/semicolon
1958 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1959 -- do not deal with AND/OR because those cases get mixed up
1960 -- with the select alternatives case.
1962 if Token
= Tok_And
or else Token
= Tok_Or
then
1963 Logop
:= P_Logical_Operator
;
1964 Restore_Scan_State
(Scan_State
); -- to comma/semicolon
1966 if Logop
in N_And_Then | N_Or_Else
then
1967 Scan
; -- past comma/semicolon
1970 Error_Msg_SP
-- CODEFIX
1971 ("|extra "","" ignored");
1973 Error_Msg_SP
-- CODEFIX
1974 ("|extra "";"" ignored");
1978 Restore_Scan_State
(Scan_State
); -- to comma/semicolon
1982 Restore_Scan_State
(Scan_State
); -- to comma/semicolon
1987 exit when Token
not in Token_Class_Logop
;
1990 Expr_Form
:= EF_Non_Simple
;
1993 if Token
= Tok_Apostrophe
then
1994 Bad_Range_Attribute
(Token_Ptr
);
2001 -- This function is identical to the normal P_Expression, except that it
2002 -- also permits the appearance of a case, conditional, or quantified
2003 -- expression if the call immediately follows a left paren, and followed
2004 -- by a right parenthesis. These forms are allowed if these conditions
2005 -- are not met, but an error message will be issued.
2007 function P_Expression_If_OK
return Node_Id
is
2009 -- Case of conditional, case or quantified expression
2012 or else Token
= Tok_If
2013 or else Token
= Tok_For
2014 or else Token
= Tok_Declare
2016 return P_Unparen_Cond_Expr_Etc
;
2018 -- Normal case, not case/conditional/quantified expression
2021 return P_Expression
;
2023 end P_Expression_If_OK
;
2025 -- This function is identical to the normal P_Expression, except that it
2026 -- checks that the expression scan did not stop on a right paren. It is
2027 -- called in all contexts where a right parenthesis cannot legitimately
2028 -- follow an expression.
2030 -- Error recovery: cannot raise Error_Resync
2032 function P_Expression_No_Right_Paren
return Node_Id
is
2033 Expr
: constant Node_Id
:= P_Expression
;
2035 Ignore
(Tok_Right_Paren
);
2037 end P_Expression_No_Right_Paren
;
2039 ----------------------------------------
2040 -- 4.4 Expression_Or_Range_Attribute --
2041 ----------------------------------------
2044 -- RELATION {and RELATION} | RELATION {and then RELATION}
2045 -- | RELATION {or RELATION} | RELATION {or else RELATION}
2046 -- | RELATION {xor RELATION}
2048 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2050 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2052 -- On return, Expr_Form indicates the categorization of the expression
2053 -- and EF_Range_Attr is one of the possibilities.
2055 -- Error recovery: cannot raise Error_Resync
2057 -- In the grammar, a RANGE attribute is simply a name, but its use is
2058 -- highly restricted, so in the parser, we do not regard it as a name.
2059 -- Instead, P_Name returns without scanning the 'RANGE part of the
2060 -- attribute, and P_Expression_Or_Range_Attribute handles the range
2061 -- attribute reference. In the normal case where a range attribute is
2062 -- not allowed, an error message is issued by P_Expression.
2064 function P_Expression_Or_Range_Attribute
return Node_Id
is
2065 Logical_Op
: Node_Kind
;
2066 Prev_Logical_Op
: Node_Kind
;
2067 Op_Location
: Source_Ptr
;
2070 Attr_Node
: Node_Id
;
2073 Node1
:= P_Relation
;
2075 if Token
= Tok_Apostrophe
then
2076 Attr_Node
:= P_Range_Attribute_Reference
(Node1
);
2077 Expr_Form
:= EF_Range_Attr
;
2080 elsif Token
in Token_Class_Logop
then
2081 Prev_Logical_Op
:= N_Empty
;
2084 Op_Location
:= Token_Ptr
;
2085 Logical_Op
:= P_Logical_Operator
;
2087 if Prev_Logical_Op
/= N_Empty
and then
2088 Logical_Op
/= Prev_Logical_Op
2091 ("mixed logical operators in expression", Op_Location
);
2092 Prev_Logical_Op
:= N_Empty
;
2094 Prev_Logical_Op
:= Logical_Op
;
2098 Node1
:= New_Op_Node
(Logical_Op
, Op_Location
);
2099 Set_Left_Opnd
(Node1
, Node2
);
2100 Set_Right_Opnd
(Node1
, P_Relation
);
2101 exit when Token
not in Token_Class_Logop
;
2104 Expr_Form
:= EF_Non_Simple
;
2107 if Token
= Tok_Apostrophe
then
2108 Bad_Range_Attribute
(Token_Ptr
);
2113 end P_Expression_Or_Range_Attribute
;
2115 -- Version that allows a non-parenthesized case, conditional, or quantified
2116 -- expression if the call immediately follows a left paren, and followed
2117 -- by a right parenthesis. These forms are allowed if these conditions
2118 -- are not met, but an error message will be issued.
2120 function P_Expression_Or_Range_Attribute_If_OK
return Node_Id
is
2122 -- Case of conditional, case or quantified expression
2125 or else Token
= Tok_If
2126 or else Token
= Tok_For
2127 or else Token
= Tok_Declare
2129 return P_Unparen_Cond_Expr_Etc
;
2131 -- Normal case, not one of the above expression types
2134 return P_Expression_Or_Range_Attribute
;
2136 end P_Expression_Or_Range_Attribute_If_OK
;
2142 -- This procedure scans both relations and choice relations
2144 -- CHOICE_RELATION ::=
2145 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
2148 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
2149 -- | RAISE_EXPRESSION
2151 -- MEMBERSHIP_CHOICE_LIST ::=
2152 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
2154 -- MEMBERSHIP_CHOICE ::=
2155 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
2157 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
2159 -- On return, Expr_Form indicates the categorization of the expression
2161 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2162 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2164 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2165 -- expression, then tokens are scanned until either a non-expression token,
2166 -- a right paren (not matched by a left paren) or a comma, is encountered.
2168 function P_Relation
return Node_Id
is
2169 Node1
, Node2
: Node_Id
;
2173 -- First check for raise expression
2175 if Token
= Tok_Raise
then
2176 Expr_Form
:= EF_Non_Simple
;
2177 return P_Raise_Expression
;
2182 Node1
:= P_Simple_Expression
;
2184 if Token
not in Token_Class_Relop
then
2188 -- Here we have a relational operator following. If so then scan it
2189 -- out. Note that the assignment symbol := is treated as a relational
2190 -- operator to improve the error recovery when it is misused for =.
2191 -- P_Relational_Operator also parses the IN and NOT IN operations.
2194 Node2
:= New_Op_Node
(P_Relational_Operator
, Optok
);
2195 Set_Left_Opnd
(Node2
, Node1
);
2197 -- Case of IN or NOT IN
2199 if Prev_Token
= Tok_In
then
2200 P_Membership_Test
(Node2
);
2202 -- Case of relational operator (= /= < <= > >=)
2205 Set_Right_Opnd
(Node2
, P_Simple_Expression
);
2208 Expr_Form
:= EF_Non_Simple
;
2210 if Token
in Token_Class_Relop
then
2211 Error_Msg_SC
("unexpected relational operator");
2218 -- If any error occurs, then scan to the next expression terminator symbol
2219 -- or comma or right paren at the outer (i.e. current) parentheses level.
2220 -- The flags are set to indicate a normal simple expression.
2223 when Error_Resync
=>
2225 Expr_Form
:= EF_Simple
;
2229 ----------------------------
2230 -- 4.4 Simple Expression --
2231 ----------------------------
2233 -- SIMPLE_EXPRESSION ::=
2234 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2236 -- On return, Expr_Form indicates the categorization of the expression
2238 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2239 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2241 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2242 -- expression, then tokens are scanned until either a non-expression token,
2243 -- a right paren (not matched by a left paren) or a comma, is encountered.
2245 -- Note: P_Simple_Expression is called only internally by higher level
2246 -- expression routines. In cases in the grammar where a simple expression
2247 -- is required, the approach is to scan an expression, and then post an
2248 -- appropriate error message if the expression obtained is not simple. This
2249 -- gives better error recovery and treatment.
2251 function P_Simple_Expression
return Node_Id
is
2252 Scan_State
: Saved_Scan_State
;
2255 Tokptr
: Source_Ptr
;
2257 function At_Start_Of_Attribute
return Boolean;
2258 -- Tests if we have quote followed by attribute name, if so, return True
2259 -- otherwise return False.
2261 ---------------------------
2262 -- At_Start_Of_Attribute --
2263 ---------------------------
2265 function At_Start_Of_Attribute
return Boolean is
2267 if Token
/= Tok_Apostrophe
then
2272 Scan_State
: Saved_Scan_State
;
2275 Save_Scan_State
(Scan_State
);
2278 if Token
= Tok_Identifier
2279 and then Is_Attribute_Name
(Chars
(Token_Node
))
2281 Restore_Scan_State
(Scan_State
);
2284 Restore_Scan_State
(Scan_State
);
2289 end At_Start_Of_Attribute
;
2291 -- Start of processing for P_Simple_Expression
2294 -- Check for cases starting with a name. There are two reasons for
2295 -- special casing. First speed things up by catching a common case
2296 -- without going through several routine layers. Second the caller must
2297 -- be informed via Expr_Form when the simple expression is a name.
2299 if Token
in Token_Class_Name
then
2302 -- Deal with apostrophe cases
2304 if Token
= Tok_Apostrophe
then
2305 Save_Scan_State
(Scan_State
); -- at apostrophe
2306 Scan
; -- past apostrophe
2308 -- If qualified expression, scan it out and fall through
2310 if Token
= Tok_Left_Paren
then
2311 Node1
:= P_Qualified_Expression
(Node1
);
2312 Expr_Form
:= EF_Simple
;
2314 -- If range attribute, then we return with Token pointing to the
2315 -- apostrophe. Note: avoid the normal error check on exit. We
2316 -- know that the expression really is complete in this case.
2318 else -- Token = Tok_Range then
2319 Restore_Scan_State
(Scan_State
); -- to apostrophe
2320 Expr_Form
:= EF_Simple_Name
;
2325 -- If an expression terminator follows, the previous processing
2326 -- completely scanned out the expression (a common case), and
2327 -- left Expr_Form set appropriately for returning to our caller.
2329 if Token
in Token_Class_Sterm
then
2332 -- If we do not have an expression terminator, then complete the
2333 -- scan of a simple expression. This code duplicates the code
2334 -- found in P_Term and P_Factor.
2337 if Token
= Tok_Double_Asterisk
then
2339 Style
.Check_Exponentiation_Operator
;
2342 Node2
:= New_Op_Node
(N_Op_Expon
, Token_Ptr
);
2344 Set_Left_Opnd
(Node2
, Node1
);
2345 Set_Right_Opnd
(Node2
, P_Primary
);
2351 exit when Token
not in Token_Class_Mulop
;
2352 Tokptr
:= Token_Ptr
;
2353 Node2
:= New_Op_Node
(P_Multiplying_Operator
, Tokptr
);
2356 Style
.Check_Binary_Operator
;
2359 Scan
; -- past operator
2360 Set_Left_Opnd
(Node2
, Node1
);
2361 Set_Right_Opnd
(Node2
, P_Factor
);
2366 exit when Token
not in Token_Class_Binary_Addop
;
2367 Tokptr
:= Token_Ptr
;
2368 Node2
:= New_Op_Node
(P_Binary_Adding_Operator
, Tokptr
);
2371 Style
.Check_Binary_Operator
;
2374 Scan
; -- past operator
2375 Set_Left_Opnd
(Node2
, Node1
);
2376 Set_Right_Opnd
(Node2
, P_Term
);
2380 Expr_Form
:= EF_Simple
;
2383 -- Cases where simple expression does not start with a name
2386 -- Scan initial sign and initial Term
2388 if Token
in Token_Class_Unary_Addop
then
2389 Tokptr
:= Token_Ptr
;
2390 Node1
:= New_Op_Node
(P_Unary_Adding_Operator
, Tokptr
);
2393 Style
.Check_Unary_Plus_Or_Minus
(Inside_Depends
);
2396 Scan
; -- past operator
2397 Set_Right_Opnd
(Node1
, P_Term
);
2402 -- In the following, we special-case a sequence of concatenations of
2403 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2404 -- else mixed in. For such a sequence, we return a tree representing
2405 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2406 -- the number of concatenations is large. If semantic analysis
2407 -- resolves the "&" to a predefined one, then this folding gives the
2408 -- right answer. Otherwise, semantic analysis will complain about a
2409 -- capacity-exceeded error. The purpose of this trick is to avoid
2410 -- creating a deeply nested tree, which would cause deep recursion
2411 -- during semantics, causing stack overflow. This way, we can handle
2412 -- enormous concatenations in the normal case of predefined "&". We
2413 -- first build up the normal tree, and then rewrite it if
2417 Num_Concats_Threshold
: constant Positive := 1000;
2418 -- Arbitrary threshold value to enable optimization
2420 First_Node
: constant Node_Id
:= Node1
;
2421 Is_Strlit_Concat
: Boolean;
2422 -- True iff we've parsed a sequence of concatenations of string
2423 -- literals, with nothing else mixed in.
2425 Num_Concats
: Natural;
2426 -- Number of "&" operators if Is_Strlit_Concat is True
2430 Nkind
(Node1
) = N_String_Literal
2431 and then Token
= Tok_Ampersand
;
2434 -- Scan out sequence of terms separated by binary adding operators
2437 exit when Token
not in Token_Class_Binary_Addop
;
2438 Tokptr
:= Token_Ptr
;
2439 Node2
:= New_Op_Node
(P_Binary_Adding_Operator
, Tokptr
);
2441 if Style_Check
and then not Debug_Flag_Dot_QQ
then
2442 Style
.Check_Binary_Operator
;
2445 Scan
; -- past operator
2446 Set_Left_Opnd
(Node2
, Node1
);
2448 Set_Right_Opnd
(Node2
, Node1
);
2450 -- Check if we're still concatenating string literals
2454 and then Nkind
(Node2
) = N_Op_Concat
2455 and then Nkind
(Node1
) = N_String_Literal
;
2457 if Is_Strlit_Concat
then
2458 Num_Concats
:= Num_Concats
+ 1;
2464 -- If we have an enormous series of concatenations of string
2465 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2466 -- flag tells semantic analysis that if the "&" is not predefined,
2467 -- the folded value is wrong.
2470 and then Num_Concats
>= Num_Concats_Threshold
2473 Strlit_Concat_Val
: String_Id
;
2474 -- Contains the folded value (which will be correct if the
2475 -- "&" operators are the predefined ones).
2478 -- For walking up the tree
2481 -- Folded node to replace Node1
2483 Loc
: constant Source_Ptr
:= Sloc
(First_Node
);
2486 -- Walk up the tree starting at the leftmost string literal
2487 -- (First_Node), building up the Strlit_Concat_Val as we
2488 -- go. Note that we do not use recursion here -- the whole
2489 -- point is to avoid recursively walking that enormous tree.
2492 Store_String_Chars
(Strval
(First_Node
));
2494 Cur_Node
:= Parent
(First_Node
);
2495 while Present
(Cur_Node
) loop
2496 pragma Assert
(Nkind
(Cur_Node
) = N_Op_Concat
and then
2497 Nkind
(Right_Opnd
(Cur_Node
)) = N_String_Literal
);
2499 Store_String_Chars
(Strval
(Right_Opnd
(Cur_Node
)));
2500 Cur_Node
:= Parent
(Cur_Node
);
2503 Strlit_Concat_Val
:= End_String
;
2505 -- Create new folded node, and rewrite result with a concat-
2506 -- enation of an empty string literal and the folded node.
2509 Make_Op_Concat
(Loc
,
2510 Make_String_Literal
(Loc
, Null_String_Id
),
2511 Make_String_Literal
(Loc
, Strlit_Concat_Val
,
2512 Is_Folded_In_Parser
=> True));
2513 Rewrite
(Node1
, New_Node
);
2518 -- All done, we clearly do not have name or numeric literal so this
2519 -- is a case of a simple expression which is some other possibility.
2521 Expr_Form
:= EF_Simple
;
2524 -- Come here at end of simple expression, where we do a couple of
2525 -- special checks to improve error recovery.
2527 -- Special test to improve error recovery. If the current token is a
2528 -- period, then someone is trying to do selection on something that is
2529 -- not a name, e.g. a qualified expression.
2531 if Token
= Tok_Dot
then
2532 Error_Msg_SC
("prefix for selection is not a name");
2534 -- If qualified expression, comment and continue, otherwise something
2535 -- is pretty nasty so do an Error_Resync call.
2537 if Ada_Version
< Ada_2012
2538 and then Nkind
(Node1
) = N_Qualified_Expression
2540 Error_Msg_SC
("\would be legal in Ada 2012 mode");
2546 -- Special test to improve error recovery: If the current token is
2547 -- not the first token on a line (as determined by checking the
2548 -- previous token position with the start of the current line),
2549 -- then we insist that we have an appropriate terminating token.
2550 -- Consider the following two examples:
2552 -- 1) if A nad B then ...
2557 -- In the first example, we would like to issue a binary operator
2558 -- expected message and resynchronize to the then. In the second
2559 -- example, we do not want to issue a binary operator message, so
2560 -- that instead we will get the missing semicolon message. This
2561 -- distinction is of course a heuristic which does not always work,
2562 -- but in practice it is quite effective.
2564 -- Note: the one case in which we do not go through this circuit is
2565 -- when we have scanned a range attribute and want to return with
2566 -- Token pointing to the apostrophe. The apostrophe is not normally
2567 -- an expression terminator, and is not in Token_Class_Sterm, but
2568 -- in this special case we know that the expression is complete.
2570 if not Token_Is_At_Start_Of_Line
2571 and then Token
not in Token_Class_Sterm
2573 -- Normally the right error message is indeed that we expected a
2574 -- binary operator, but in the case of being between a right and left
2575 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2577 if Prev_Token
= Tok_Right_Paren
and then Token
= Tok_Left_Paren
then
2580 -- And if we have a quote, we may have a bad attribute
2582 elsif At_Start_Of_Attribute
then
2583 Error_Msg_SC
("prefix of attribute must be a name");
2585 if Ada_Version
>= Ada_2012
then
2586 Error_Msg_SC
("\qualify expression to turn it into a name");
2589 -- Normal case for binary operator expected message
2592 Error_Msg_AP
("binary operator expected");
2601 -- If any error occurs, then scan to next expression terminator symbol
2602 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2603 -- level. Expr_Form is set to indicate a normal simple expression.
2606 when Error_Resync
=>
2608 Expr_Form
:= EF_Simple
;
2610 end P_Simple_Expression
;
2612 -----------------------------------------------
2613 -- 4.4 Simple Expression or Range Attribute --
2614 -----------------------------------------------
2616 -- SIMPLE_EXPRESSION ::=
2617 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2619 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2621 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2623 -- Error recovery: cannot raise Error_Resync
2625 function P_Simple_Expression_Or_Range_Attribute
return Node_Id
is
2627 Attr_Node
: Node_Id
;
2630 -- We don't just want to roar ahead and call P_Simple_Expression
2631 -- here, since we want to handle the case of a parenthesized range
2632 -- attribute cleanly.
2634 if Token
= Tok_Left_Paren
then
2636 Lptr
: constant Source_Ptr
:= Token_Ptr
;
2637 Scan_State
: Saved_Scan_State
;
2640 Save_Scan_State
(Scan_State
);
2641 Scan
; -- past left paren
2642 Sexpr
:= P_Simple_Expression
;
2644 if Token
= Tok_Apostrophe
then
2645 Attr_Node
:= P_Range_Attribute_Reference
(Sexpr
);
2646 Expr_Form
:= EF_Range_Attr
;
2648 if Token
= Tok_Right_Paren
then
2649 Scan
; -- scan past right paren if present
2652 Error_Msg
("parentheses not allowed for range attribute", Lptr
);
2657 Restore_Scan_State
(Scan_State
);
2661 -- Here after dealing with parenthesized range attribute
2663 Sexpr
:= P_Simple_Expression
;
2665 if Token
= Tok_Apostrophe
then
2666 Attr_Node
:= P_Range_Attribute_Reference
(Sexpr
);
2667 Expr_Form
:= EF_Range_Attr
;
2673 end P_Simple_Expression_Or_Range_Attribute
;
2679 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2681 -- Error recovery: can raise Error_Resync
2683 function P_Term
return Node_Id
is
2684 Node1
, Node2
: Node_Id
;
2685 Tokptr
: Source_Ptr
;
2691 exit when Token
not in Token_Class_Mulop
;
2692 Tokptr
:= Token_Ptr
;
2693 Node2
:= New_Op_Node
(P_Multiplying_Operator
, Tokptr
);
2695 if Style_Check
and then not Debug_Flag_Dot_QQ
then
2696 Style
.Check_Binary_Operator
;
2699 Scan
; -- past operator
2700 Set_Left_Opnd
(Node2
, Node1
);
2701 Set_Right_Opnd
(Node2
, P_Factor
);
2712 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2714 -- Error recovery: can raise Error_Resync
2716 function P_Factor
return Node_Id
is
2721 if Token
= Tok_Abs
then
2722 Node1
:= New_Op_Node
(N_Op_Abs
, Token_Ptr
);
2725 Style
.Check_Abs_Not
;
2729 Set_Right_Opnd
(Node1
, P_Primary
);
2732 elsif Token
= Tok_Not
then
2733 Node1
:= New_Op_Node
(N_Op_Not
, Token_Ptr
);
2736 Style
.Check_Abs_Not
;
2740 Set_Right_Opnd
(Node1
, P_Primary
);
2746 if Token
= Tok_Double_Asterisk
then
2747 Node2
:= New_Op_Node
(N_Op_Expon
, Token_Ptr
);
2749 Set_Left_Opnd
(Node2
, Node1
);
2750 Set_Right_Opnd
(Node2
, P_Primary
);
2764 -- NUMERIC_LITERAL | null
2765 -- | STRING_LITERAL | AGGREGATE
2766 -- | NAME | QUALIFIED_EXPRESSION
2767 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2768 -- | REDUCTION_ATTRIBUTE_REFERENCE
2770 -- Error recovery: can raise Error_Resync
2772 function P_Primary
return Node_Id
is
2773 Scan_State
: Saved_Scan_State
;
2776 Lparen
: constant Boolean := Prev_Token
= Tok_Left_Paren
;
2777 -- Remember if previous token is a left parenthesis. This is used to
2778 -- deal with checking whether IF/CASE/FOR expressions appearing as
2779 -- primaries require extra parenthesization.
2782 -- The loop runs more than once only if misplaced pragmas are found
2783 -- or if a misplaced unary minus is skipped.
2788 -- Name token can start a name, call or qualified expression, all
2789 -- of which are acceptable possibilities for primary. Note also
2790 -- that string literal is included in name (as operator symbol)
2791 -- and type conversion is included in name (as indexed component).
2793 when Tok_Char_Literal
2795 | Tok_Operator_Symbol
2799 -- All done unless apostrophe follows
2801 if Token
/= Tok_Apostrophe
then
2804 -- Apostrophe following means that we have either just parsed
2805 -- the subtype mark of a qualified expression, or the prefix
2806 -- or a range attribute.
2808 else -- Token = Tok_Apostrophe
2809 Save_Scan_State
(Scan_State
); -- at apostrophe
2810 Scan
; -- past apostrophe
2812 -- If range attribute, then this is always an error, since
2813 -- the only legitimate case (where the scanned expression is
2814 -- a qualified simple name) is handled at the level of the
2815 -- Simple_Expression processing. This case corresponds to a
2816 -- usage such as 3 + A'Range, which is always illegal.
2818 if Token
= Tok_Range
then
2819 Restore_Scan_State
(Scan_State
); -- to apostrophe
2820 Bad_Range_Attribute
(Token_Ptr
);
2823 -- If left paren, then we have a qualified expression.
2824 -- Note that P_Name guarantees that in this case, where
2825 -- Token = Tok_Apostrophe on return, the only two possible
2826 -- tokens following the apostrophe are left paren and
2827 -- RANGE, so we know we have a left paren here.
2829 else -- Token = Tok_Left_Paren
2830 return P_Qualified_Expression
(Node1
);
2835 -- Numeric or string literal
2837 when Tok_Integer_Literal
2839 | Tok_String_Literal
2841 Node1
:= Token_Node
;
2842 Scan
; -- past number
2845 -- Left paren, starts aggregate or parenthesized expression
2847 when Tok_Left_Paren
=>
2849 Expr
: constant Node_Id
:= P_Aggregate_Or_Paren_Expr
;
2852 if Nkind
(Expr
) = N_Attribute_Reference
2853 and then Attribute_Name
(Expr
) = Name_Range
2855 Bad_Range_Attribute
(Sloc
(Expr
));
2861 when Tok_Left_Bracket
=>
2873 return New_Node
(N_Null
, Prev_Token_Ptr
);
2875 -- Pragma, not allowed here, so just skip past it
2878 P_Pragmas_Misplaced
;
2880 -- Deal with IF (possible unparenthesized if expression)
2884 -- If this looks like a real if, defined as an IF appearing at
2885 -- the start of a new line, then we consider we have a missing
2886 -- operand. If in Ada 2012 and the IF is not properly indented
2887 -- for a statement, we prefer to issue a message about an ill-
2888 -- parenthesized if expression.
2890 if Token_Is_At_Start_Of_Line
2892 (Ada_Version
>= Ada_2012
2894 (Style_Check_Indentation
= 0
2896 Start_Column
rem Style_Check_Indentation
/= 0))
2898 Error_Msg_AP
("missing operand");
2901 -- If this looks like an if expression, then treat it that way
2902 -- with an error message if not explicitly surrounded by
2905 elsif Ada_Version
>= Ada_2012
then
2906 Node1
:= P_If_Expression
;
2908 if not (Lparen
and then Token
= Tok_Right_Paren
) then
2910 ("if expression must be parenthesized", Sloc
(Node1
));
2915 -- Otherwise treat as misused identifier
2918 return P_Identifier
;
2921 -- Deal with CASE (possible unparenthesized case expression)
2925 -- If this looks like a real case, defined as a CASE appearing
2926 -- the start of a new line, then we consider we have a missing
2927 -- operand. If in Ada 2012 and the CASE is not properly
2928 -- indented for a statement, we prefer to issue a message about
2929 -- an ill-parenthesized case expression.
2931 if Token_Is_At_Start_Of_Line
2933 (Ada_Version
>= Ada_2012
2934 and then Style_Check_Indentation
/= 0
2935 and then Start_Column
rem Style_Check_Indentation
/= 0)
2937 Error_Msg_AP
("missing operand");
2940 -- If this looks like a case expression, then treat it that way
2941 -- with an error message if not within parentheses.
2943 elsif Ada_Version
>= Ada_2012
then
2944 Node1
:= P_Case_Expression
;
2946 if not (Lparen
and then Token
= Tok_Right_Paren
) then
2948 ("case expression must be parenthesized", Sloc
(Node1
));
2953 -- Otherwise treat as misused identifier
2956 return P_Identifier
;
2959 -- For [all | some] indicates a quantified expression
2962 if Token_Is_At_Start_Of_Line
then
2963 Error_Msg_AP
("misplaced loop");
2966 elsif Ada_Version
>= Ada_2012
then
2967 Save_Scan_State
(Scan_State
);
2970 if Token
= Tok_All
or else Token
= Tok_Some
then
2971 Restore_Scan_State
(Scan_State
); -- To FOR
2972 Node1
:= P_Quantified_Expression
;
2974 if not (Lparen
and then Token
= Tok_Right_Paren
) then
2976 ("quantified expression must be parenthesized",
2980 Restore_Scan_State
(Scan_State
); -- To FOR
2981 Node1
:= P_Iterated_Component_Association
;
2986 -- Otherwise treat as misused identifier
2989 return P_Identifier
;
2992 -- Minus may well be an improper attempt at a unary minus. Give
2993 -- a message, skip the minus and keep going.
2996 Error_Msg_SC
("parentheses required for unary minus");
2999 when Tok_At_Sign
=> -- AI12-0125 : target_name
3000 Error_Msg_Ada_2022_Feature
("target name", Token_Ptr
);
3005 -- Anything else is illegal as the first token of a primary, but
3006 -- we test for some common errors, to improve error messages.
3009 if Is_Reserved_Identifier
then
3010 return P_Identifier
;
3012 elsif Prev_Token
= Tok_Comma
then
3013 Error_Msg_SP
-- CODEFIX
3014 ("|extra "","" ignored");
3018 Error_Msg_AP
("missing operand");
3025 -------------------------------
3026 -- 4.4 Quantified_Expression --
3027 -------------------------------
3029 -- QUANTIFIED_EXPRESSION ::=
3030 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
3031 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
3033 function P_Quantified_Expression
return Node_Id
is
3038 Error_Msg_Ada_2012_Feature
("quantified expression", Token_Ptr
);
3040 Node1
:= New_Node
(N_Quantified_Expression
, Prev_Token_Ptr
);
3042 if Token
= Tok_All
then
3043 Set_All_Present
(Node1
);
3044 elsif Token
/= Tok_Some
then
3045 Error_Msg_AP
("missing quantifier");
3049 Scan
; -- past ALL or SOME
3050 I_Spec
:= P_Loop_Parameter_Specification
;
3052 if Nkind
(I_Spec
) = N_Loop_Parameter_Specification
then
3053 Set_Loop_Parameter_Specification
(Node1
, I_Spec
);
3055 Set_Iterator_Specification
(Node1
, I_Spec
);
3058 if Token
= Tok_Arrow
then
3060 Set_Condition
(Node1
, P_Expression
);
3063 Error_Msg_AP
("missing arrow");
3066 end P_Quantified_Expression
;
3068 ---------------------------
3069 -- 4.5 Logical Operator --
3070 ---------------------------
3072 -- LOGICAL_OPERATOR ::= and | or | xor
3074 -- Note: AND THEN and OR ELSE are also treated as logical operators
3075 -- by the parser (even though they are not operators semantically)
3077 -- The value returned is the appropriate Node_Kind code for the operator
3078 -- On return, Token points to the token following the scanned operator.
3080 -- The caller has checked that the first token is a legitimate logical
3081 -- operator token (i.e. is either XOR, AND, OR).
3083 -- Error recovery: cannot raise Error_Resync
3085 function P_Logical_Operator
return Node_Kind
is
3087 if Token
= Tok_And
then
3089 Style
.Check_Binary_Operator
;
3094 if Token
= Tok_Then
then
3101 elsif Token
= Tok_Or
then
3103 Style
.Check_Binary_Operator
;
3108 if Token
= Tok_Else
then
3115 else -- Token = Tok_Xor
3117 Style
.Check_Binary_Operator
;
3123 end P_Logical_Operator
;
3125 ------------------------------
3126 -- 4.5 Relational Operator --
3127 ------------------------------
3129 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
3131 -- The value returned is the appropriate Node_Kind code for the operator.
3132 -- On return, Token points to the operator token, NOT past it.
3134 -- The caller has checked that the first token is a legitimate relational
3135 -- operator token (i.e. is one of the operator tokens listed above).
3137 -- Error recovery: cannot raise Error_Resync
3139 function P_Relational_Operator
return Node_Kind
is
3140 Op_Kind
: Node_Kind
;
3141 Relop_Node
: constant array (Token_Class_Relop
) of Node_Kind
:=
3142 (Tok_Less
=> N_Op_Lt
,
3143 Tok_Equal
=> N_Op_Eq
,
3144 Tok_Greater
=> N_Op_Gt
,
3145 Tok_Not_Equal
=> N_Op_Ne
,
3146 Tok_Greater_Equal
=> N_Op_Ge
,
3147 Tok_Less_Equal
=> N_Op_Le
,
3149 Tok_Not
=> N_Not_In
,
3150 Tok_Box
=> N_Op_Ne
);
3153 if Token
= Tok_Box
then
3154 Error_Msg_SC
-- CODEFIX
3155 ("|""'<'>"" should be ""/=""");
3158 Op_Kind
:= Relop_Node
(Token
);
3161 Style
.Check_Binary_Operator
;
3164 Scan
; -- past operator token
3166 -- Deal with NOT IN, if previous token was NOT, we must have IN now
3168 if Prev_Token
= Tok_Not
then
3170 -- Style check, for NOT IN, we require one space between NOT and IN
3172 if Style_Check
and then Token
= Tok_In
then
3180 end P_Relational_Operator
;
3182 ---------------------------------
3183 -- 4.5 Binary Adding Operator --
3184 ---------------------------------
3186 -- BINARY_ADDING_OPERATOR ::= + | - | &
3188 -- The value returned is the appropriate Node_Kind code for the operator.
3189 -- On return, Token points to the operator token (NOT past it).
3191 -- The caller has checked that the first token is a legitimate adding
3192 -- operator token (i.e. is one of the operator tokens listed above).
3194 -- Error recovery: cannot raise Error_Resync
3196 function P_Binary_Adding_Operator
return Node_Kind
is
3197 Addop_Node
: constant array (Token_Class_Binary_Addop
) of Node_Kind
:=
3198 (Tok_Ampersand
=> N_Op_Concat
,
3199 Tok_Minus
=> N_Op_Subtract
,
3200 Tok_Plus
=> N_Op_Add
);
3202 return Addop_Node
(Token
);
3203 end P_Binary_Adding_Operator
;
3205 --------------------------------
3206 -- 4.5 Unary Adding Operator --
3207 --------------------------------
3209 -- UNARY_ADDING_OPERATOR ::= + | -
3211 -- The value returned is the appropriate Node_Kind code for the operator.
3212 -- On return, Token points to the operator token (NOT past it).
3214 -- The caller has checked that the first token is a legitimate adding
3215 -- operator token (i.e. is one of the operator tokens listed above).
3217 -- Error recovery: cannot raise Error_Resync
3219 function P_Unary_Adding_Operator
return Node_Kind
is
3220 Addop_Node
: constant array (Token_Class_Unary_Addop
) of Node_Kind
:=
3221 (Tok_Minus
=> N_Op_Minus
,
3222 Tok_Plus
=> N_Op_Plus
);
3224 return Addop_Node
(Token
);
3225 end P_Unary_Adding_Operator
;
3227 -------------------------------
3228 -- 4.5 Multiplying Operator --
3229 -------------------------------
3231 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3233 -- The value returned is the appropriate Node_Kind code for the operator.
3234 -- On return, Token points to the operator token (NOT past it).
3236 -- The caller has checked that the first token is a legitimate multiplying
3237 -- operator token (i.e. is one of the operator tokens listed above).
3239 -- Error recovery: cannot raise Error_Resync
3241 function P_Multiplying_Operator
return Node_Kind
is
3242 Mulop_Node
: constant array (Token_Class_Mulop
) of Node_Kind
:=
3243 (Tok_Asterisk
=> N_Op_Multiply
,
3244 Tok_Mod
=> N_Op_Mod
,
3245 Tok_Rem
=> N_Op_Rem
,
3246 Tok_Slash
=> N_Op_Divide
);
3248 return Mulop_Node
(Token
);
3249 end P_Multiplying_Operator
;
3251 --------------------------------------
3252 -- 4.5 Highest Precedence Operator --
3253 --------------------------------------
3255 -- Parsed by P_Factor (4.4)
3257 -- Note: this rule is not in fact used by the grammar at any point
3259 --------------------------
3260 -- 4.6 Type Conversion --
3261 --------------------------
3263 -- Parsed by P_Primary as a Name (4.1)
3265 -------------------------------
3266 -- 4.7 Qualified Expression --
3267 -------------------------------
3269 -- QUALIFIED_EXPRESSION ::=
3270 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3272 -- The caller has scanned the name which is the Subtype_Mark parameter
3273 -- and scanned past the single quote following the subtype mark. The
3274 -- caller has not checked that this name is in fact appropriate for
3275 -- a subtype mark name (i.e. it is a selected component or identifier).
3277 -- Error_Recovery: cannot raise Error_Resync
3279 function P_Qualified_Expression
(Subtype_Mark
: Node_Id
) return Node_Id
is
3280 Qual_Node
: Node_Id
;
3282 Qual_Node
:= New_Node
(N_Qualified_Expression
, Prev_Token_Ptr
);
3283 Set_Subtype_Mark
(Qual_Node
, Check_Subtype_Mark
(Subtype_Mark
));
3284 Set_Expression
(Qual_Node
, P_Aggregate_Or_Paren_Expr
);
3286 end P_Qualified_Expression
;
3288 --------------------
3290 --------------------
3293 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3294 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3296 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3298 -- The caller has checked that the initial token is NEW
3300 -- Error recovery: can raise Error_Resync
3302 function P_Allocator
return Node_Id
is
3303 Alloc_Node
: Node_Id
;
3304 Type_Node
: Node_Id
;
3305 Null_Exclusion_Present
: Boolean;
3308 Alloc_Node
:= New_Node
(N_Allocator
, Token_Ptr
);
3311 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3313 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3315 if Token
= Tok_Left_Paren
then
3317 Set_Subpool_Handle_Name
(Alloc_Node
, P_Name
);
3320 Error_Msg_Ada_2012_Feature
3321 ("|subpool specification",
3322 Sloc
(Subpool_Handle_Name
(Alloc_Node
)));
3325 Null_Exclusion_Present
:= P_Null_Exclusion
;
3326 Set_Null_Exclusion_Present
(Alloc_Node
, Null_Exclusion_Present
);
3327 Type_Node
:= P_Subtype_Mark_Resync
;
3329 if Token
= Tok_Apostrophe
then
3330 Scan
; -- past apostrophe
3331 Set_Expression
(Alloc_Node
, P_Qualified_Expression
(Type_Node
));
3335 P_Subtype_Indication
(Type_Node
, Null_Exclusion_Present
));
3337 -- AI05-0104: An explicit null exclusion is not allowed for an
3338 -- allocator without initialization. In previous versions of the
3339 -- language it just raises constraint error.
3341 if Ada_Version
>= Ada_2012
and then Null_Exclusion_Present
then
3343 ("an allocator with a subtype indication "
3344 & "cannot have a null exclusion", Alloc_Node
);
3351 -----------------------
3352 -- P_Case_Expression --
3353 -----------------------
3355 function P_Case_Expression
return Node_Id
is
3356 Loc
: constant Source_Ptr
:= Token_Ptr
;
3357 Case_Node
: Node_Id
;
3358 Save_State
: Saved_Scan_State
;
3361 Error_Msg_Ada_2012_Feature
("|case expression", Token_Ptr
);
3364 Make_Case_Expression
(Loc
,
3365 Expression
=> P_Expression_No_Right_Paren
,
3366 Alternatives
=> New_List
);
3369 -- We now have scanned out CASE expression IS, scan alternatives
3373 Append_To
(Alternatives
(Case_Node
), P_Case_Expression_Alternative
);
3375 -- Missing comma if WHEN (more alternatives present)
3377 if Token
= Tok_When
then
3380 -- A semicolon followed by "when" is probably meant to be a comma
3382 elsif Token
= Tok_Semicolon
then
3383 Save_Scan_State
(Save_State
);
3384 Scan
; -- past the semicolon
3386 if Token
/= Tok_When
then
3387 Restore_Scan_State
(Save_State
);
3391 Error_Msg_SP
-- CODEFIX
3392 ("|"";"" should be "",""");
3394 -- If comma/WHEN, skip comma and we have another alternative
3396 elsif Token
= Tok_Comma
then
3397 Save_Scan_State
(Save_State
);
3400 if Token
/= Tok_When
then
3401 Restore_Scan_State
(Save_State
);
3405 -- If no comma or WHEN, definitely done
3412 -- If we have an END CASE, diagnose as not needed
3414 if Token
= Tok_End
then
3415 Error_Msg_SC
("`END CASE` not allowed at end of case expression");
3418 if Token
= Tok_Case
then
3423 -- Return the Case_Expression node
3426 end P_Case_Expression
;
3428 -----------------------------------
3429 -- P_Case_Expression_Alternative --
3430 -----------------------------------
3432 -- CASE_STATEMENT_ALTERNATIVE ::=
3433 -- when DISCRETE_CHOICE_LIST =>
3436 -- The caller has checked that and scanned past the initial WHEN token
3437 -- Error recovery: can raise Error_Resync
3439 function P_Case_Expression_Alternative
return Node_Id
is
3440 Case_Alt_Node
: Node_Id
;
3442 Case_Alt_Node
:= New_Node
(N_Case_Expression_Alternative
, Token_Ptr
);
3443 Set_Discrete_Choices
(Case_Alt_Node
, P_Discrete_Choice_List
);
3445 Set_Expression
(Case_Alt_Node
, P_Expression
);
3446 return Case_Alt_Node
;
3447 end P_Case_Expression_Alternative
;
3449 --------------------------------------
3450 -- P_Iterated_Component_Association --
3451 --------------------------------------
3453 -- ITERATED_COMPONENT_ASSOCIATION ::=
3454 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3455 -- for ITERATOR_SPECIFICATION => EXPRESSION
3457 function P_Iterated_Component_Association
return Node_Id
is
3458 Assoc_Node
: Node_Id
;
3460 Filter
: Node_Id
:= Empty
;
3462 Iter_Spec
: Node_Id
;
3463 Loop_Spec
: Node_Id
;
3464 State
: Saved_Scan_State
;
3466 procedure Build_Iterated_Element_Association
;
3467 -- If the iterator includes a key expression or a filter, it is
3468 -- an Ada 2022 Iterator_Element_Association within a container
3471 ----------------------------------------
3472 -- Build_Iterated_Element_Association --
3473 ----------------------------------------
3475 procedure Build_Iterated_Element_Association
is
3477 -- Build loop_parameter_specification
3480 New_Node
(N_Loop_Parameter_Specification
, Prev_Token_Ptr
);
3481 Set_Defining_Identifier
(Loop_Spec
, Id
);
3483 Choice
:= First
(Discrete_Choices
(Assoc_Node
));
3485 New_Node
(N_Iterated_Element_Association
, Prev_Token_Ptr
);
3486 Set_Loop_Parameter_Specification
(Assoc_Node
, Loop_Spec
);
3488 if Present
(Next
(Choice
)) then
3489 Error_Msg_N
("expect loop parameter specification", Choice
);
3493 Set_Discrete_Subtype_Definition
(Loop_Spec
, Choice
);
3494 Set_Iterator_Filter
(Loop_Spec
, Filter
);
3495 end Build_Iterated_Element_Association
;
3497 -- Start of processing for P_Iterated_Component_Association
3501 Save_Scan_State
(State
);
3503 -- A lookahead is necessary to differentiate between the
3504 -- Ada 2012 form with a choice list, and the Ada 2022 element
3505 -- iterator form, recognized by the presence of "OF". Other
3506 -- disambiguation requires context and is done during semantic
3507 -- analysis. Note that "for X in E" is syntactically ambiguous:
3508 -- if E is a subtype indication this is a loop parameter spec,
3509 -- while if E a name it is an iterator_specification, and the
3510 -- disambiguation takes place during semantic analysis.
3511 -- In addition, if "use" is present after the specification,
3512 -- this is an Iterated_Element_Association that carries a
3513 -- key_expression, and we generate the appropriate node.
3514 -- Finally, the Iterated_Element form is reserved for container
3515 -- aggregates, and is illegal in array aggregates.
3517 Id
:= P_Defining_Identifier
;
3519 New_Node
(N_Iterated_Component_Association
, Prev_Token_Ptr
);
3523 Set_Defining_Identifier
(Assoc_Node
, Id
);
3525 Set_Discrete_Choices
(Assoc_Node
, P_Discrete_Choice_List
);
3527 -- The iterator may include a filter
3529 if Token
= Tok_When
then
3531 Filter
:= P_Condition
;
3534 if Token
= Tok_Use
then
3536 -- Ada 2022 Key-expression is present, rewrite node as an
3537 -- Iterated_Element_Association.
3540 Build_Iterated_Element_Association
;
3541 Set_Key_Expression
(Assoc_Node
, P_Expression
);
3543 elsif Present
(Filter
) then
3544 -- A loop_parameter_specification also indicates an Ada 2022
3545 -- construct, in contrast with a subtype indication used in
3546 -- array aggregates.
3548 Build_Iterated_Element_Association
;
3552 Set_Expression
(Assoc_Node
, P_Expression
);
3555 Restore_Scan_State
(State
);
3557 Set_Defining_Identifier
(Assoc_Node
, Id
);
3558 Iter_Spec
:= P_Iterator_Specification
(Id
);
3559 Set_Iterator_Specification
(Assoc_Node
, Iter_Spec
);
3561 if Token
= Tok_Use
then
3563 -- This is an iterated_element_association
3566 New_Node
(N_Iterated_Element_Association
, Prev_Token_Ptr
);
3567 Set_Iterator_Specification
(Assoc_Node
, Iter_Spec
);
3568 Set_Key_Expression
(Assoc_Node
, P_Expression
);
3572 Set_Expression
(Assoc_Node
, P_Expression
);
3575 Error_Msg_AP
("missing IN or OF");
3579 end P_Iterated_Component_Association
;
3581 ---------------------
3582 -- P_If_Expression --
3583 ---------------------
3585 -- IF_EXPRESSION ::=
3586 -- if CONDITION then DEPENDENT_EXPRESSION
3587 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3588 -- [else DEPENDENT_EXPRESSION]
3590 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3592 function P_If_Expression
return Node_Id
is
3593 function P_If_Expression_Internal
3595 Cond
: Node_Id
) return Node_Id
;
3596 -- This is the internal recursive routine that does all the work, it is
3597 -- recursive since it is used to process ELSIF parts, which internally
3598 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3599 -- sequence is like the outer function except that the caller passes
3600 -- the conditional expression (scanned using P_Expression), and the
3601 -- scan pointer points just past this expression. Loc points to the
3602 -- IF or ELSIF token.
3604 ------------------------------
3605 -- P_If_Expression_Internal --
3606 ------------------------------
3608 function P_If_Expression_Internal
3610 Cond
: Node_Id
) return Node_Id
3612 Exprs
: constant List_Id
:= New_List
;
3614 State
: Saved_Scan_State
;
3618 -- All cases except where we are at right paren
3620 if Token
/= Tok_Right_Paren
then
3622 Append_To
(Exprs
, P_Condition
(Cond
));
3623 Append_To
(Exprs
, P_Expression
);
3625 -- Case of right paren (missing THEN phrase). Note that we know this
3626 -- is the IF case, since the caller dealt with this possibility in
3630 Error_Msg_BC
("missing THEN phrase");
3631 Append_To
(Exprs
, P_Condition
(Cond
));
3634 -- We now have scanned out IF expr THEN expr
3636 -- Check for common error of semicolon before the ELSE
3638 if Token
= Tok_Semicolon
then
3639 Save_Scan_State
(State
);
3640 Scan
; -- past semicolon
3642 if Token
= Tok_Else
or else Token
= Tok_Elsif
then
3643 Error_Msg_SP
-- CODEFIX
3644 ("|extra "";"" ignored");
3647 Restore_Scan_State
(State
);
3651 -- Scan out ELSIF sequence if present
3653 if Token
= Tok_Elsif
then
3656 Expr
:= P_Expression
;
3658 -- If we are at a right paren, we assume the ELSIF should be ELSE
3660 if Token
= Tok_Right_Paren
then
3661 Error_Msg
("ELSIF should be ELSE", Eptr
);
3662 Append_To
(Exprs
, Expr
);
3664 -- Otherwise we have an OK ELSIF
3667 Expr
:= P_If_Expression_Internal
(Eptr
, Expr
);
3668 Set_Is_Elsif
(Expr
);
3669 Append_To
(Exprs
, Expr
);
3672 -- Scan out ELSE phrase if present
3674 elsif Token
= Tok_Else
then
3676 -- Scan out ELSE expression
3679 Append_To
(Exprs
, P_Expression
);
3681 -- Skip redundant ELSE parts
3683 while Token
= Tok_Else
loop
3684 Error_Msg_SC
("only one ELSE part is allowed");
3686 Discard_Junk_Node
(P_Expression
);
3689 -- Two expression case (implied True, filled in during semantics)
3695 -- If we have an END IF, diagnose as not needed
3697 if Token
= Tok_End
then
3698 Error_Msg_SC
("`END IF` not allowed at end of if expression");
3701 if Token
= Tok_If
then
3706 -- Return the If_Expression node
3708 return Make_If_Expression
(Loc
, Expressions
=> Exprs
);
3709 end P_If_Expression_Internal
;
3713 Loc
: constant Source_Ptr
:= Token_Ptr
;
3716 -- Start of processing for P_If_Expression
3719 Error_Msg_Ada_2012_Feature
("|if expression", Token_Ptr
);
3721 Inside_If_Expression
:= Inside_If_Expression
+ 1;
3722 If_Expr
:= P_If_Expression_Internal
(Loc
, P_Expression
);
3723 Inside_If_Expression
:= Inside_If_Expression
- 1;
3725 end P_If_Expression
;
3727 --------------------------
3728 -- P_Declare_Expression --
3729 --------------------------
3731 -- DECLARE_EXPRESSION ::=
3732 -- DECLARE {DECLARE_ITEM}
3733 -- begin BODY_EXPRESSION
3735 -- DECLARE_ITEM ::= OBJECT_DECLARATION
3736 -- | OBJECT_RENAMING_DECLARATION
3738 function P_Declare_Expression
return Node_Id
is
3739 Loc
: constant Source_Ptr
:= Token_Ptr
;
3741 Scan
; -- past DECLARE
3744 Actions
: constant List_Id
:= P_Basic_Declarative_Items
3745 (Declare_Expression
=> True);
3746 -- Most declarative items allowed by P_Basic_Declarative_Items are
3747 -- illegal; semantic analysis will deal with that.
3749 if Token
= Tok_Begin
then
3752 Error_Msg_SC
-- CODEFIX
3753 ("BEGIN expected!");
3757 Expression
: constant Node_Id
:= P_Expression
;
3758 Result
: constant Node_Id
:=
3759 Make_Expression_With_Actions
(Loc
, Actions
, Expression
);
3761 Error_Msg_Ada_2022_Feature
("declare expression", Loc
);
3766 end P_Declare_Expression
;
3768 -----------------------
3769 -- P_Membership_Test --
3770 -----------------------
3772 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERSHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3773 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3775 procedure P_Membership_Test
(N
: Node_Id
) is
3776 Alt
: constant Node_Id
:=
3777 P_Range_Or_Subtype_Mark
3778 (Allow_Simple_Expression
=> (Ada_Version
>= Ada_2012
));
3783 if Token
= Tok_Vertical_Bar
then
3784 Error_Msg_Ada_2012_Feature
("set notation", Token_Ptr
);
3785 Set_Alternatives
(N
, New_List
(Alt
));
3786 Set_Right_Opnd
(N
, Empty
);
3788 -- Loop to accumulate alternatives
3790 while Token
= Tok_Vertical_Bar
loop
3791 Scan
; -- past vertical bar
3794 P_Range_Or_Subtype_Mark
(Allow_Simple_Expression
=> True));
3800 Set_Right_Opnd
(N
, Alt
);
3801 Set_Alternatives
(N
, No_List
);
3803 end P_Membership_Test
;
3805 -----------------------------
3806 -- P_Unparen_Cond_Expr_Etc --
3807 -----------------------------
3809 function P_Unparen_Cond_Expr_Etc
return Node_Id
is
3810 Lparen
: constant Boolean := Prev_Token
= Tok_Left_Paren
;
3813 Scan_State
: Saved_Scan_State
;
3818 if Token
= Tok_Case
then
3819 Result
:= P_Case_Expression
;
3821 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3822 Error_Msg_N
("case expression must be parenthesized!", Result
);
3827 elsif Token
= Tok_If
then
3828 Result
:= P_If_Expression
;
3830 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3831 Error_Msg_N
("if expression must be parenthesized!", Result
);
3834 -- Quantified expression or iterated component association
3836 elsif Token
= Tok_For
then
3838 Save_Scan_State
(Scan_State
);
3841 if Token
= Tok_All
or else Token
= Tok_Some
then
3842 Restore_Scan_State
(Scan_State
);
3843 Result
:= P_Quantified_Expression
;
3845 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3847 ("quantified expression must be parenthesized!", Result
);
3851 -- If no quantifier keyword, this is an iterated component in
3854 Restore_Scan_State
(Scan_State
);
3855 Result
:= P_Iterated_Component_Association
;
3858 -- Declare expression
3860 elsif Token
= Tok_Declare
then
3861 Result
:= P_Declare_Expression
;
3863 if not (Lparen
and then Token
= Tok_Right_Paren
) then
3864 Error_Msg_N
("declare expression must be parenthesized!", Result
);
3867 -- No other possibility should exist (caller was supposed to check)
3870 raise Program_Error
;
3873 -- Return expression (possibly after having given message)
3876 end P_Unparen_Cond_Expr_Etc
;