[multiple changes]
[gcc.git] / gcc / ada / par-ch4.adb
1 -----------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
29
30 with Stringt; use Stringt;
31
32 separate (Par)
33 package body Ch4 is
34
35 -- Attributes that cannot have arguments
36
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,
49 others => False);
50 -- This map contains True for parameterless attributes that return a
51 -- string or a type. For those attributes, a left parenthesis after
52 -- the attribute should not be analyzed as the beginning of a parameters
53 -- list because it may denote a slice operation (X'Img (1 .. 2)) or
54 -- a type conversion (X'Class (Y)). The Ada2012 attribute 'Old is in
55 -- this category.
56
57 -- Note: Loop_Entry is in this list because, although it can take an
58 -- optional argument (the loop name), we can't distinguish that at parse
59 -- time from the case where no loop name is given and a legitimate index
60 -- expression is present. So we parse the argument as an indexed component
61 -- and the semantic analysis sorts out this syntactic ambiguity based on
62 -- the type and form of the expression.
63
64 -- Note that this map designates the minimum set of attributes where a
65 -- construct in parentheses that is not an argument can appear right
66 -- after the attribute. For attributes like 'Size, we do not put them
67 -- in the map. If someone writes X'Size (3), that's illegal in any case,
68 -- but we get a better error message by parsing the (3) as an illegal
69 -- argument to the attribute, rather than some meaningless junk that
70 -- follows the attribute.
71
72 -----------------------
73 -- Local Subprograms --
74 -----------------------
75
76 function P_Aggregate_Or_Paren_Expr return Node_Id;
77 function P_Allocator return Node_Id;
78 function P_Case_Expression_Alternative return Node_Id;
79 function P_Iterated_Component_Association return Node_Id;
80 function P_Record_Or_Array_Component_Association return Node_Id;
81 function P_Factor return Node_Id;
82 function P_Primary return Node_Id;
83 function P_Relation return Node_Id;
84 function P_Term return Node_Id;
85
86 function P_Binary_Adding_Operator return Node_Kind;
87 function P_Logical_Operator return Node_Kind;
88 function P_Multiplying_Operator return Node_Kind;
89 function P_Relational_Operator return Node_Kind;
90 function P_Unary_Adding_Operator return Node_Kind;
91
92 procedure Bad_Range_Attribute (Loc : Source_Ptr);
93 -- Called to place complaint about bad range attribute at the given
94 -- source location. Terminates by raising Error_Resync.
95
96 procedure Check_Bad_Exp;
97 -- Called after scanning a**b, posts error if ** detected
98
99 procedure P_Membership_Test (N : Node_Id);
100 -- N is the node for a N_In or N_Not_In node whose right operand has not
101 -- yet been processed. It is called just after scanning out the IN keyword.
102 -- On return, either Right_Opnd or Alternatives is set, as appropriate.
103
104 function P_Range_Attribute_Reference (Prefix_Node : Node_Id) return Node_Id;
105 -- Scan a range attribute reference. The caller has scanned out the
106 -- prefix. The current token is known to be an apostrophe and the
107 -- following token is known to be RANGE.
108
109 function P_Unparen_Cond_Case_Quant_Expression return Node_Id;
110 -- This function is called with Token pointing to IF, CASE, or FOR, in a
111 -- context that allows a case, conditional, or quantified expression if
112 -- it is surrounded by parentheses. If not surrounded by parentheses, the
113 -- expression is still returned, but an error message is issued.
114
115 -------------------------
116 -- Bad_Range_Attribute --
117 -------------------------
118
119 procedure Bad_Range_Attribute (Loc : Source_Ptr) is
120 begin
121 Error_Msg ("range attribute cannot be used in expression!", Loc);
122 Resync_Expression;
123 end Bad_Range_Attribute;
124
125 -------------------
126 -- Check_Bad_Exp --
127 -------------------
128
129 procedure Check_Bad_Exp is
130 begin
131 if Token = Tok_Double_Asterisk then
132 Error_Msg_SC ("parenthesization required for '*'*");
133 Scan; -- past **
134 Discard_Junk_Node (P_Primary);
135 Check_Bad_Exp;
136 end if;
137 end Check_Bad_Exp;
138
139 --------------------------
140 -- 4.1 Name (also 6.4) --
141 --------------------------
142
143 -- NAME ::=
144 -- DIRECT_NAME | EXPLICIT_DEREFERENCE
145 -- | INDEXED_COMPONENT | SLICE
146 -- | SELECTED_COMPONENT | ATTRIBUTE
147 -- | TYPE_CONVERSION | FUNCTION_CALL
148 -- | CHARACTER_LITERAL | TARGET_NAME
149
150 -- DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
151
152 -- PREFIX ::= NAME | IMPLICIT_DEREFERENCE
153
154 -- EXPLICIT_DEREFERENCE ::= NAME . all
155
156 -- IMPLICIT_DEREFERENCE ::= NAME
157
158 -- INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
159
160 -- SLICE ::= PREFIX (DISCRETE_RANGE)
161
162 -- SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
163
164 -- SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
165
166 -- ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
167
168 -- ATTRIBUTE_DESIGNATOR ::=
169 -- IDENTIFIER [(static_EXPRESSION)]
170 -- | access | delta | digits
171
172 -- FUNCTION_CALL ::=
173 -- function_NAME
174 -- | function_PREFIX ACTUAL_PARAMETER_PART
175
176 -- ACTUAL_PARAMETER_PART ::=
177 -- (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
178
179 -- PARAMETER_ASSOCIATION ::=
180 -- [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
181
182 -- EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
183
184 -- TARGET_NAME ::= @ (AI12-0125-3: abbreviation for LHS)
185
186 -- Note: syntactically a procedure call looks just like a function call,
187 -- so this routine is in practice used to scan out procedure calls as well.
188
189 -- On return, Expr_Form is set to either EF_Name or EF_Simple_Name
190
191 -- Error recovery: can raise Error_Resync
192
193 -- Note: if on return Token = Tok_Apostrophe, then the apostrophe must be
194 -- followed by either a left paren (qualified expression case), or by
195 -- range (range attribute case). All other uses of apostrophe (i.e. all
196 -- other attributes) are handled in this routine.
197
198 -- Error recovery: can raise Error_Resync
199
200 function P_Name return Node_Id is
201 Scan_State : Saved_Scan_State;
202 Name_Node : Node_Id;
203 Prefix_Node : Node_Id;
204 Ident_Node : Node_Id;
205 Expr_Node : Node_Id;
206 Range_Node : Node_Id;
207 Arg_Node : Node_Id;
208
209 Arg_List : List_Id := No_List; -- kill junk warning
210 Attr_Name : Name_Id := No_Name; -- kill junk warning
211
212 begin
213 -- Case of not a name
214
215 if Token not in Token_Class_Name then
216
217 -- If it looks like start of expression, complain and scan expression
218
219 if Token in Token_Class_Literal
220 or else Token = Tok_Left_Paren
221 then
222 Error_Msg_SC ("name expected");
223 return P_Expression;
224
225 -- Otherwise some other junk, not much we can do
226
227 else
228 Error_Msg_AP ("name expected");
229 raise Error_Resync;
230 end if;
231 end if;
232
233 -- Loop through designators in qualified name
234 -- AI12-0125 : target_name
235
236 if Token = Tok_At_Sign then
237 Scan_Reserved_Identifier (Force_Msg => False);
238 end if;
239
240 Name_Node := Token_Node;
241
242 loop
243 Scan; -- past designator
244 exit when Token /= Tok_Dot;
245 Save_Scan_State (Scan_State); -- at dot
246 Scan; -- past dot
247
248 -- If we do not have another designator after the dot, then join
249 -- the normal circuit to handle a dot extension (may be .all or
250 -- character literal case). Otherwise loop back to scan the next
251 -- designator.
252
253 if Token not in Token_Class_Desig then
254 goto Scan_Name_Extension_Dot;
255 else
256 Prefix_Node := Name_Node;
257 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
258 Set_Prefix (Name_Node, Prefix_Node);
259 Set_Selector_Name (Name_Node, Token_Node);
260 end if;
261 end loop;
262
263 -- We have now scanned out a qualified designator. If the last token is
264 -- an operator symbol, then we certainly do not have the Snam case, so
265 -- we can just use the normal name extension check circuit
266
267 if Prev_Token = Tok_Operator_Symbol then
268 goto Scan_Name_Extension;
269 end if;
270
271 -- We have scanned out a qualified simple name, check for name extension
272 -- Note that we know there is no dot here at this stage, so the only
273 -- possible cases of name extension are apostrophe and left paren.
274
275 if Token = Tok_Apostrophe then
276 Save_Scan_State (Scan_State); -- at apostrophe
277 Scan; -- past apostrophe
278
279 -- Qualified expression in Ada 2012 mode (treated as a name)
280
281 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
282 goto Scan_Name_Extension_Apostrophe;
283
284 -- If left paren not in Ada 2012, then it is not part of the name,
285 -- since qualified expressions are not names in prior versions of
286 -- Ada, so return with Token backed up to point to the apostrophe.
287 -- The treatment for the range attribute is similar (we do not
288 -- consider x'range to be a name in this grammar).
289
290 elsif Token = Tok_Left_Paren or else Token = Tok_Range then
291 Restore_Scan_State (Scan_State); -- to apostrophe
292 Expr_Form := EF_Simple_Name;
293 return Name_Node;
294
295 -- Otherwise we have the case of a name extended by an attribute
296
297 else
298 goto Scan_Name_Extension_Apostrophe;
299 end if;
300
301 -- Check case of qualified simple name extended by a left parenthesis
302
303 elsif Token = Tok_Left_Paren then
304 Scan; -- past left paren
305 goto Scan_Name_Extension_Left_Paren;
306
307 -- Otherwise the qualified simple name is not extended, so return
308
309 else
310 Expr_Form := EF_Simple_Name;
311 return Name_Node;
312 end if;
313
314 -- Loop scanning past name extensions. A label is used for control
315 -- transfer for this loop for ease of interfacing with the finite state
316 -- machine in the parenthesis scanning circuit, and also to allow for
317 -- passing in control to the appropriate point from the above code.
318
319 <<Scan_Name_Extension>>
320
321 -- Character literal used as name cannot be extended. Also this
322 -- cannot be a call, since the name for a call must be a designator.
323 -- Return in these cases, or if there is no name extension
324
325 if Token not in Token_Class_Namext
326 or else Prev_Token = Tok_Char_Literal
327 then
328 Expr_Form := EF_Name;
329 return Name_Node;
330 end if;
331
332 -- Merge here when we know there is a name extension
333
334 <<Scan_Name_Extension_OK>>
335
336 if Token = Tok_Left_Paren then
337 Scan; -- past left paren
338 goto Scan_Name_Extension_Left_Paren;
339
340 elsif Token = Tok_Apostrophe then
341 Save_Scan_State (Scan_State); -- at apostrophe
342 Scan; -- past apostrophe
343 goto Scan_Name_Extension_Apostrophe;
344
345 else -- Token = Tok_Dot
346 Save_Scan_State (Scan_State); -- at dot
347 Scan; -- past dot
348 goto Scan_Name_Extension_Dot;
349 end if;
350
351 -- Case of name extended by dot (selection), dot is already skipped
352 -- and the scan state at the point of the dot is saved in Scan_State.
353
354 <<Scan_Name_Extension_Dot>>
355
356 -- Explicit dereference case
357
358 if Token = Tok_All then
359 Prefix_Node := Name_Node;
360 Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
361 Set_Prefix (Name_Node, Prefix_Node);
362 Scan; -- past ALL
363 goto Scan_Name_Extension;
364
365 -- Selected component case
366
367 elsif Token in Token_Class_Name then
368 Prefix_Node := Name_Node;
369 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
370 Set_Prefix (Name_Node, Prefix_Node);
371 Set_Selector_Name (Name_Node, Token_Node);
372 Scan; -- past selector
373 goto Scan_Name_Extension;
374
375 -- Reserved identifier as selector
376
377 elsif Is_Reserved_Identifier then
378 Scan_Reserved_Identifier (Force_Msg => False);
379 Prefix_Node := Name_Node;
380 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
381 Set_Prefix (Name_Node, Prefix_Node);
382 Set_Selector_Name (Name_Node, Token_Node);
383 Scan; -- past identifier used as selector
384 goto Scan_Name_Extension;
385
386 -- If dot is at end of line and followed by nothing legal,
387 -- then assume end of name and quit (dot will be taken as
388 -- an incorrect form of some other punctuation by our caller).
389
390 elsif Token_Is_At_Start_Of_Line then
391 Restore_Scan_State (Scan_State);
392 return Name_Node;
393
394 -- Here if nothing legal after the dot
395
396 else
397 Error_Msg_AP ("selector expected");
398 raise Error_Resync;
399 end if;
400
401 -- Here for an apostrophe as name extension. The scan position at the
402 -- apostrophe has already been saved, and the apostrophe scanned out.
403
404 <<Scan_Name_Extension_Apostrophe>>
405
406 Scan_Apostrophe : declare
407 function Apostrophe_Should_Be_Semicolon return Boolean;
408 -- Checks for case where apostrophe should probably be
409 -- a semicolon, and if so, gives appropriate message,
410 -- resets the scan pointer to the apostrophe, changes
411 -- the current token to Tok_Semicolon, and returns True.
412 -- Otherwise returns False.
413
414 ------------------------------------
415 -- Apostrophe_Should_Be_Semicolon --
416 ------------------------------------
417
418 function Apostrophe_Should_Be_Semicolon return Boolean is
419 begin
420 if Token_Is_At_Start_Of_Line then
421 Restore_Scan_State (Scan_State); -- to apostrophe
422 Error_Msg_SC ("|""''"" should be "";""");
423 Token := Tok_Semicolon;
424 return True;
425 else
426 return False;
427 end if;
428 end Apostrophe_Should_Be_Semicolon;
429
430 -- Start of processing for Scan_Apostrophe
431
432 begin
433 -- Check for qualified expression case in Ada 2012 mode
434
435 if Ada_Version >= Ada_2012 and then Token = Tok_Left_Paren then
436 Name_Node := P_Qualified_Expression (Name_Node);
437 goto Scan_Name_Extension;
438
439 -- If range attribute after apostrophe, then return with Token
440 -- pointing to the apostrophe. Note that in this case the prefix
441 -- need not be a simple name (cases like A.all'range). Similarly
442 -- if there is a left paren after the apostrophe, then we also
443 -- return with Token pointing to the apostrophe (this is the
444 -- aggregate case, or some error case).
445
446 elsif Token = Tok_Range or else Token = Tok_Left_Paren then
447 Restore_Scan_State (Scan_State); -- to apostrophe
448 Expr_Form := EF_Name;
449 return Name_Node;
450
451 -- Here for cases where attribute designator is an identifier
452
453 elsif Token = Tok_Identifier then
454 Attr_Name := Token_Name;
455
456 if not Is_Attribute_Name (Attr_Name) then
457 if Apostrophe_Should_Be_Semicolon then
458 Expr_Form := EF_Name;
459 return Name_Node;
460
461 -- Here for a bad attribute name
462
463 else
464 Signal_Bad_Attribute;
465 Scan; -- past bad identifier
466
467 if Token = Tok_Left_Paren then
468 Scan; -- past left paren
469
470 loop
471 Discard_Junk_Node (P_Expression_If_OK);
472 exit when not Comma_Present;
473 end loop;
474
475 T_Right_Paren;
476 end if;
477
478 return Error;
479 end if;
480 end if;
481
482 if Style_Check then
483 Style.Check_Attribute_Name (False);
484 end if;
485
486 -- Here for case of attribute designator is not an identifier
487
488 else
489 if Token = Tok_Delta then
490 Attr_Name := Name_Delta;
491
492 elsif Token = Tok_Digits then
493 Attr_Name := Name_Digits;
494
495 elsif Token = Tok_Access then
496 Attr_Name := Name_Access;
497
498 elsif Token = Tok_Mod and then Ada_Version >= Ada_95 then
499 Attr_Name := Name_Mod;
500
501 elsif Apostrophe_Should_Be_Semicolon then
502 Expr_Form := EF_Name;
503 return Name_Node;
504
505 else
506 Error_Msg_AP ("attribute designator expected");
507 raise Error_Resync;
508 end if;
509
510 if Style_Check then
511 Style.Check_Attribute_Name (True);
512 end if;
513 end if;
514
515 -- We come here with an OK attribute scanned, and corresponding
516 -- Attribute identifier node stored in Ident_Node.
517
518 Prefix_Node := Name_Node;
519 Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
520 Scan; -- past attribute designator
521 Set_Prefix (Name_Node, Prefix_Node);
522 Set_Attribute_Name (Name_Node, Attr_Name);
523
524 -- Scan attribute arguments/designator. We skip this if we know
525 -- that the attribute cannot have an argument (see documentation
526 -- of Is_Parameterless_Attribute for further details).
527
528 if Token = Tok_Left_Paren
529 and then not
530 Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
531 then
532 -- Attribute Update contains an array or record association
533 -- list which provides new values for various components or
534 -- elements. The list is parsed as an aggregate, and we get
535 -- better error handling by knowing that in the parser.
536
537 if Attr_Name = Name_Update then
538 Set_Expressions (Name_Node, New_List);
539 Append (P_Aggregate, Expressions (Name_Node));
540
541 -- All other cases of parsing attribute arguments
542
543 else
544 Set_Expressions (Name_Node, New_List);
545 Scan; -- past left paren
546
547 loop
548 declare
549 Expr : constant Node_Id := P_Expression_If_OK;
550 Rnam : Node_Id;
551
552 begin
553 -- Case of => for named notation
554
555 if Token = Tok_Arrow then
556
557 -- Named notation allowed only for the special
558 -- case of System'Restriction_Set (No_Dependence =>
559 -- unit_NAME), in which case construct a parameter
560 -- assocation node and append to the arguments.
561
562 if Attr_Name = Name_Restriction_Set
563 and then Nkind (Expr) = N_Identifier
564 and then Chars (Expr) = Name_No_Dependence
565 then
566 Scan; -- past arrow
567 Rnam := P_Name;
568 Append_To (Expressions (Name_Node),
569 Make_Parameter_Association (Sloc (Rnam),
570 Selector_Name => Expr,
571 Explicit_Actual_Parameter => Rnam));
572 exit;
573
574 -- For all other cases named notation is illegal
575
576 else
577 Error_Msg_SC
578 ("named parameters not permitted "
579 & "for attributes");
580 Scan; -- past junk arrow
581 end if;
582
583 -- Here for normal case (not => for named parameter)
584
585 else
586 Append (Expr, Expressions (Name_Node));
587 exit when not Comma_Present;
588 end if;
589 end;
590 end loop;
591
592 T_Right_Paren;
593 end if;
594 end if;
595
596 goto Scan_Name_Extension;
597 end Scan_Apostrophe;
598
599 -- Here for left parenthesis extending name (left paren skipped)
600
601 <<Scan_Name_Extension_Left_Paren>>
602
603 -- We now have to scan through a list of items, terminated by a
604 -- right parenthesis. The scan is handled by a finite state
605 -- machine. The possibilities are:
606
607 -- (discrete_range)
608
609 -- This is a slice. This case is handled in LP_State_Init
610
611 -- (expression, expression, ..)
612
613 -- This is interpreted as an indexed component, i.e. as a
614 -- case of a name which can be extended in the normal manner.
615 -- This case is handled by LP_State_Name or LP_State_Expr.
616
617 -- Note: if and case expressions (without an extra level of
618 -- parentheses) are permitted in this context).
619
620 -- (..., identifier => expression , ...)
621
622 -- If there is at least one occurrence of identifier => (but
623 -- none of the other cases apply), then we have a call.
624
625 -- Test for Id => case
626
627 if Token = Tok_Identifier then
628 Save_Scan_State (Scan_State); -- at Id
629 Scan; -- past Id
630
631 -- Test for => (allow := as an error substitute)
632
633 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
634 Restore_Scan_State (Scan_State); -- to Id
635 Arg_List := New_List;
636 goto LP_State_Call;
637
638 else
639 Restore_Scan_State (Scan_State); -- to Id
640 end if;
641 end if;
642
643 -- Here we have an expression after all
644
645 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
646
647 -- Check cases of discrete range for a slice
648
649 -- First possibility: Range_Attribute_Reference
650
651 if Expr_Form = EF_Range_Attr then
652 Range_Node := Expr_Node;
653
654 -- Second possibility: Simple_expression .. Simple_expression
655
656 elsif Token = Tok_Dot_Dot then
657 Check_Simple_Expression (Expr_Node);
658 Range_Node := New_Node (N_Range, Token_Ptr);
659 Set_Low_Bound (Range_Node, Expr_Node);
660 Scan; -- past ..
661 Expr_Node := P_Expression;
662 Check_Simple_Expression (Expr_Node);
663 Set_High_Bound (Range_Node, Expr_Node);
664
665 -- Third possibility: Type_name range Range
666
667 elsif Token = Tok_Range then
668 if Expr_Form /= EF_Simple_Name then
669 Error_Msg_SC ("subtype mark must precede RANGE");
670 raise Error_Resync;
671 end if;
672
673 Range_Node := P_Subtype_Indication (Expr_Node);
674
675 -- Otherwise we just have an expression. It is true that we might
676 -- have a subtype mark without a range constraint but this case
677 -- is syntactically indistinguishable from the expression case.
678
679 else
680 Arg_List := New_List;
681 goto LP_State_Expr;
682 end if;
683
684 -- Fall through here with unmistakable Discrete range scanned,
685 -- which means that we definitely have the case of a slice. The
686 -- Discrete range is in Range_Node.
687
688 if Token = Tok_Comma then
689 Error_Msg_SC ("slice cannot have more than one dimension");
690 raise Error_Resync;
691
692 elsif Token /= Tok_Right_Paren then
693 if Token = Tok_Arrow then
694
695 -- This may be an aggregate that is missing a qualification
696
697 Error_Msg_SC
698 ("context of aggregate must be a qualified expression");
699 raise Error_Resync;
700
701 else
702 T_Right_Paren;
703 raise Error_Resync;
704 end if;
705
706 else
707 Scan; -- past right paren
708 Prefix_Node := Name_Node;
709 Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
710 Set_Prefix (Name_Node, Prefix_Node);
711 Set_Discrete_Range (Name_Node, Range_Node);
712
713 -- An operator node is legal as a prefix to other names,
714 -- but not for a slice.
715
716 if Nkind (Prefix_Node) = N_Operator_Symbol then
717 Error_Msg_N ("illegal prefix for slice", Prefix_Node);
718 end if;
719
720 -- If we have a name extension, go scan it
721
722 if Token in Token_Class_Namext then
723 goto Scan_Name_Extension_OK;
724
725 -- Otherwise return (a slice is a name, but is not a call)
726
727 else
728 Expr_Form := EF_Name;
729 return Name_Node;
730 end if;
731 end if;
732
733 -- In LP_State_Expr, we have scanned one or more expressions, and
734 -- so we have a call or an indexed component which is a name. On
735 -- entry we have the expression just scanned in Expr_Node and
736 -- Arg_List contains the list of expressions encountered so far
737
738 <<LP_State_Expr>>
739 Append (Expr_Node, Arg_List);
740
741 if Token = Tok_Arrow then
742 Error_Msg
743 ("expect identifier in parameter association", Sloc (Expr_Node));
744 Scan; -- past arrow
745
746 elsif not Comma_Present then
747 T_Right_Paren;
748
749 Prefix_Node := Name_Node;
750 Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
751 Set_Prefix (Name_Node, Prefix_Node);
752 Set_Expressions (Name_Node, Arg_List);
753
754 goto Scan_Name_Extension;
755 end if;
756
757 -- Comma present (and scanned out), test for identifier => case
758 -- Test for identifier => case
759
760 if Token = Tok_Identifier then
761 Save_Scan_State (Scan_State); -- at Id
762 Scan; -- past Id
763
764 -- Test for => (allow := as error substitute)
765
766 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
767 Restore_Scan_State (Scan_State); -- to Id
768 goto LP_State_Call;
769
770 -- Otherwise it's just an expression after all, so backup
771
772 else
773 Restore_Scan_State (Scan_State); -- to Id
774 end if;
775 end if;
776
777 -- Here we have an expression after all, so stay in this state
778
779 Expr_Node := P_Expression_If_OK;
780 goto LP_State_Expr;
781
782 -- LP_State_Call corresponds to the situation in which at least one
783 -- instance of Id => Expression has been encountered, so we know that
784 -- we do not have a name, but rather a call. We enter it with the
785 -- scan pointer pointing to the next argument to scan, and Arg_List
786 -- containing the list of arguments scanned so far.
787
788 <<LP_State_Call>>
789
790 -- Test for case of Id => Expression (named parameter)
791
792 if Token = Tok_Identifier then
793 Save_Scan_State (Scan_State); -- at Id
794 Ident_Node := Token_Node;
795 Scan; -- past Id
796
797 -- Deal with => (allow := as incorrect substitute)
798
799 if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
800 Arg_Node := New_Node (N_Parameter_Association, Prev_Token_Ptr);
801 Set_Selector_Name (Arg_Node, Ident_Node);
802 T_Arrow;
803 Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
804 Append (Arg_Node, Arg_List);
805
806 -- If a comma follows, go back and scan next entry
807
808 if Comma_Present then
809 goto LP_State_Call;
810
811 -- Otherwise we have the end of a call
812
813 else
814 Prefix_Node := Name_Node;
815 Name_Node := New_Node (N_Function_Call, Sloc (Prefix_Node));
816 Set_Name (Name_Node, Prefix_Node);
817 Set_Parameter_Associations (Name_Node, Arg_List);
818 T_Right_Paren;
819
820 if Token in Token_Class_Namext then
821 goto Scan_Name_Extension_OK;
822
823 -- This is a case of a call which cannot be a name
824
825 else
826 Expr_Form := EF_Name;
827 return Name_Node;
828 end if;
829 end if;
830
831 -- Not named parameter: Id started an expression after all
832
833 else
834 Restore_Scan_State (Scan_State); -- to Id
835 end if;
836 end if;
837
838 -- Here if entry did not start with Id => which means that it
839 -- is a positional parameter, which is not allowed, since we
840 -- have seen at least one named parameter already.
841
842 Error_Msg_SC
843 ("positional parameter association " &
844 "not allowed after named one");
845
846 Expr_Node := P_Expression_If_OK;
847
848 -- Leaving the '>' in an association is not unusual, so suggest
849 -- a possible fix.
850
851 if Nkind (Expr_Node) = N_Op_Eq then
852 Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
853 end if;
854
855 -- We go back to scanning out expressions, so that we do not get
856 -- multiple error messages when several positional parameters
857 -- follow a named parameter.
858
859 goto LP_State_Expr;
860
861 -- End of treatment for name extensions starting with left paren
862
863 -- End of loop through name extensions
864
865 end P_Name;
866
867 -- This function parses a restricted form of Names which are either
868 -- designators, or designators preceded by a sequence of prefixes
869 -- that are direct names.
870
871 -- Error recovery: cannot raise Error_Resync
872
873 function P_Function_Name return Node_Id is
874 Designator_Node : Node_Id;
875 Prefix_Node : Node_Id;
876 Selector_Node : Node_Id;
877 Dot_Sloc : Source_Ptr := No_Location;
878
879 begin
880 -- Prefix_Node is set to the gathered prefix so far, Empty means that
881 -- no prefix has been scanned. This allows us to build up the result
882 -- in the required right recursive manner.
883
884 Prefix_Node := Empty;
885
886 -- Loop through prefixes
887
888 loop
889 Designator_Node := Token_Node;
890
891 if Token not in Token_Class_Desig then
892 return P_Identifier; -- let P_Identifier issue the error message
893
894 else -- Token in Token_Class_Desig
895 Scan; -- past designator
896 exit when Token /= Tok_Dot;
897 end if;
898
899 -- Here at a dot, with token just before it in Designator_Node
900
901 if No (Prefix_Node) then
902 Prefix_Node := Designator_Node;
903 else
904 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
905 Set_Prefix (Selector_Node, Prefix_Node);
906 Set_Selector_Name (Selector_Node, Designator_Node);
907 Prefix_Node := Selector_Node;
908 end if;
909
910 Dot_Sloc := Token_Ptr;
911 Scan; -- past dot
912 end loop;
913
914 -- Fall out of the loop having just scanned a designator
915
916 if No (Prefix_Node) then
917 return Designator_Node;
918 else
919 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
920 Set_Prefix (Selector_Node, Prefix_Node);
921 Set_Selector_Name (Selector_Node, Designator_Node);
922 return Selector_Node;
923 end if;
924
925 exception
926 when Error_Resync =>
927 return Error;
928 end P_Function_Name;
929
930 -- This function parses a restricted form of Names which are either
931 -- identifiers, or identifiers preceded by a sequence of prefixes
932 -- that are direct names.
933
934 -- Error recovery: cannot raise Error_Resync
935
936 function P_Qualified_Simple_Name return Node_Id is
937 Designator_Node : Node_Id;
938 Prefix_Node : Node_Id;
939 Selector_Node : Node_Id;
940 Dot_Sloc : Source_Ptr := No_Location;
941
942 begin
943 -- Prefix node is set to the gathered prefix so far, Empty means that
944 -- no prefix has been scanned. This allows us to build up the result
945 -- in the required right recursive manner.
946
947 Prefix_Node := Empty;
948
949 -- Loop through prefixes
950
951 loop
952 Designator_Node := Token_Node;
953
954 if Token = Tok_Identifier then
955 Scan; -- past identifier
956 exit when Token /= Tok_Dot;
957
958 elsif Token not in Token_Class_Desig then
959 return P_Identifier; -- let P_Identifier issue the error message
960
961 else
962 Scan; -- past designator
963
964 if Token /= Tok_Dot then
965 Error_Msg_SP ("identifier expected");
966 return Error;
967 end if;
968 end if;
969
970 -- Here at a dot, with token just before it in Designator_Node
971
972 if No (Prefix_Node) then
973 Prefix_Node := Designator_Node;
974 else
975 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
976 Set_Prefix (Selector_Node, Prefix_Node);
977 Set_Selector_Name (Selector_Node, Designator_Node);
978 Prefix_Node := Selector_Node;
979 end if;
980
981 Dot_Sloc := Token_Ptr;
982 Scan; -- past dot
983 end loop;
984
985 -- Fall out of the loop having just scanned an identifier
986
987 if No (Prefix_Node) then
988 return Designator_Node;
989 else
990 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
991 Set_Prefix (Selector_Node, Prefix_Node);
992 Set_Selector_Name (Selector_Node, Designator_Node);
993 return Selector_Node;
994 end if;
995
996 exception
997 when Error_Resync =>
998 return Error;
999 end P_Qualified_Simple_Name;
1000
1001 -- This procedure differs from P_Qualified_Simple_Name only in that it
1002 -- raises Error_Resync if any error is encountered. It only returns after
1003 -- scanning a valid qualified simple name.
1004
1005 -- Error recovery: can raise Error_Resync
1006
1007 function P_Qualified_Simple_Name_Resync return Node_Id is
1008 Designator_Node : Node_Id;
1009 Prefix_Node : Node_Id;
1010 Selector_Node : Node_Id;
1011 Dot_Sloc : Source_Ptr := No_Location;
1012
1013 begin
1014 Prefix_Node := Empty;
1015
1016 -- Loop through prefixes
1017
1018 loop
1019 Designator_Node := Token_Node;
1020
1021 if Token = Tok_Identifier then
1022 Scan; -- past identifier
1023 exit when Token /= Tok_Dot;
1024
1025 elsif Token not in Token_Class_Desig then
1026 Discard_Junk_Node (P_Identifier); -- to issue the error message
1027 raise Error_Resync;
1028
1029 else
1030 Scan; -- past designator
1031
1032 if Token /= Tok_Dot then
1033 Error_Msg_SP ("identifier expected");
1034 raise Error_Resync;
1035 end if;
1036 end if;
1037
1038 -- Here at a dot, with token just before it in Designator_Node
1039
1040 if No (Prefix_Node) then
1041 Prefix_Node := Designator_Node;
1042 else
1043 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1044 Set_Prefix (Selector_Node, Prefix_Node);
1045 Set_Selector_Name (Selector_Node, Designator_Node);
1046 Prefix_Node := Selector_Node;
1047 end if;
1048
1049 Dot_Sloc := Token_Ptr;
1050 Scan; -- past period
1051 end loop;
1052
1053 -- Fall out of the loop having just scanned an identifier
1054
1055 if No (Prefix_Node) then
1056 return Designator_Node;
1057 else
1058 Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
1059 Set_Prefix (Selector_Node, Prefix_Node);
1060 Set_Selector_Name (Selector_Node, Designator_Node);
1061 return Selector_Node;
1062 end if;
1063 end P_Qualified_Simple_Name_Resync;
1064
1065 ----------------------
1066 -- 4.1 Direct_Name --
1067 ----------------------
1068
1069 -- Parsed by P_Name and other functions in section 4.1
1070
1071 -----------------
1072 -- 4.1 Prefix --
1073 -----------------
1074
1075 -- Parsed by P_Name (4.1)
1076
1077 -------------------------------
1078 -- 4.1 Explicit Dereference --
1079 -------------------------------
1080
1081 -- Parsed by P_Name (4.1)
1082
1083 -------------------------------
1084 -- 4.1 Implicit_Dereference --
1085 -------------------------------
1086
1087 -- Parsed by P_Name (4.1)
1088
1089 ----------------------------
1090 -- 4.1 Indexed Component --
1091 ----------------------------
1092
1093 -- Parsed by P_Name (4.1)
1094
1095 ----------------
1096 -- 4.1 Slice --
1097 ----------------
1098
1099 -- Parsed by P_Name (4.1)
1100
1101 -----------------------------
1102 -- 4.1 Selected_Component --
1103 -----------------------------
1104
1105 -- Parsed by P_Name (4.1)
1106
1107 ------------------------
1108 -- 4.1 Selector Name --
1109 ------------------------
1110
1111 -- Parsed by P_Name (4.1)
1112
1113 ------------------------------
1114 -- 4.1 Attribute Reference --
1115 ------------------------------
1116
1117 -- Parsed by P_Name (4.1)
1118
1119 -------------------------------
1120 -- 4.1 Attribute Designator --
1121 -------------------------------
1122
1123 -- Parsed by P_Name (4.1)
1124
1125 --------------------------------------
1126 -- 4.1.4 Range Attribute Reference --
1127 --------------------------------------
1128
1129 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1130
1131 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1132
1133 -- In the grammar, a RANGE attribute is simply a name, but its use is
1134 -- highly restricted, so in the parser, we do not regard it as a name.
1135 -- Instead, P_Name returns without scanning the 'RANGE part of the
1136 -- attribute, and the caller uses the following function to construct
1137 -- a range attribute in places where it is appropriate.
1138
1139 -- Note that RANGE here is treated essentially as an identifier,
1140 -- rather than a reserved word.
1141
1142 -- The caller has parsed the prefix, i.e. a name, and Token points to
1143 -- the apostrophe. The token after the apostrophe is known to be RANGE
1144 -- at this point. The prefix node becomes the prefix of the attribute.
1145
1146 -- Error_Recovery: Cannot raise Error_Resync
1147
1148 function P_Range_Attribute_Reference
1149 (Prefix_Node : Node_Id)
1150 return Node_Id
1151 is
1152 Attr_Node : Node_Id;
1153
1154 begin
1155 Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1156 Set_Prefix (Attr_Node, Prefix_Node);
1157 Scan; -- past apostrophe
1158
1159 if Style_Check then
1160 Style.Check_Attribute_Name (True);
1161 end if;
1162
1163 Set_Attribute_Name (Attr_Node, Name_Range);
1164 Scan; -- past RANGE
1165
1166 if Token = Tok_Left_Paren then
1167 Scan; -- past left paren
1168 Set_Expressions (Attr_Node, New_List (P_Expression_If_OK));
1169 T_Right_Paren;
1170 end if;
1171
1172 return Attr_Node;
1173 end P_Range_Attribute_Reference;
1174
1175 ---------------------------------------
1176 -- 4.1.4 Range Attribute Designator --
1177 ---------------------------------------
1178
1179 -- Parsed by P_Range_Attribute_Reference (4.4)
1180
1181 --------------------
1182 -- 4.3 Aggregate --
1183 --------------------
1184
1185 -- AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1186
1187 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1188 -- an aggregate is known to be required (code statement, extension
1189 -- aggregate), in which cases this routine performs the necessary check
1190 -- that we have an aggregate rather than a parenthesized expression
1191
1192 -- Error recovery: can raise Error_Resync
1193
1194 function P_Aggregate return Node_Id is
1195 Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1196 Aggr_Node : constant Node_Id := P_Aggregate_Or_Paren_Expr;
1197
1198 begin
1199 if Nkind (Aggr_Node) /= N_Aggregate
1200 and then
1201 Nkind (Aggr_Node) /= N_Extension_Aggregate
1202 then
1203 Error_Msg
1204 ("aggregate may not have single positional component", Aggr_Sloc);
1205 return Error;
1206 else
1207 return Aggr_Node;
1208 end if;
1209 end P_Aggregate;
1210
1211 ------------------------------------------------
1212 -- 4.3 Aggregate or Parenthesized Expression --
1213 ------------------------------------------------
1214
1215 -- This procedure parses out either an aggregate or a parenthesized
1216 -- expression (these two constructs are closely related, since a
1217 -- parenthesized expression looks like an aggregate with a single
1218 -- positional component).
1219
1220 -- AGGREGATE ::=
1221 -- RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1222
1223 -- RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1224
1225 -- RECORD_COMPONENT_ASSOCIATION_LIST ::=
1226 -- RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1227 -- | null record
1228
1229 -- RECORD_COMPONENT_ASSOCIATION ::=
1230 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1231
1232 -- COMPONENT_CHOICE_LIST ::=
1233 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1234 -- | others
1235
1236 -- EXTENSION_AGGREGATE ::=
1237 -- (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1238
1239 -- ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1240
1241 -- ARRAY_AGGREGATE ::=
1242 -- POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1243
1244 -- POSITIONAL_ARRAY_AGGREGATE ::=
1245 -- (EXPRESSION, EXPRESSION {, EXPRESSION})
1246 -- | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1247 -- | (EXPRESSION {, EXPRESSION}, others => <>)
1248
1249 -- NAMED_ARRAY_AGGREGATE ::=
1250 -- (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1251
1252 -- PRIMARY ::= (EXPRESSION);
1253
1254 -- Error recovery: can raise Error_Resync
1255
1256 -- Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1257 -- to Ada 2005 limited aggregates (AI-287)
1258
1259 function P_Aggregate_Or_Paren_Expr return Node_Id is
1260 Aggregate_Node : Node_Id;
1261 Expr_List : List_Id;
1262 Assoc_List : List_Id;
1263 Expr_Node : Node_Id;
1264 Lparen_Sloc : Source_Ptr;
1265 Scan_State : Saved_Scan_State;
1266
1267 procedure Box_Error;
1268 -- Called if <> is encountered as positional aggregate element. Issues
1269 -- error message and sets Expr_Node to Error.
1270
1271 function Is_Quantified_Expression return Boolean;
1272 -- The presence of iterated component associations requires a one
1273 -- token lookahead to distinguish it from quantified expressions.
1274
1275 ---------------
1276 -- Box_Error --
1277 ---------------
1278
1279 procedure Box_Error is
1280 begin
1281 if Ada_Version < Ada_2005 then
1282 Error_Msg_SC ("box in aggregate is an Ada 2005 extension");
1283 end if;
1284
1285 -- Ada 2005 (AI-287): The box notation is allowed only with named
1286 -- notation because positional notation might be error prone. For
1287 -- example, in "(X, <>, Y, <>)", there is no type associated with
1288 -- the boxes, so you might not be leaving out the components you
1289 -- thought you were leaving out.
1290
1291 Error_Msg_SC ("(Ada 2005) box only allowed with named notation");
1292 Scan; -- past box
1293 Expr_Node := Error;
1294 end Box_Error;
1295
1296 ------------------------------
1297 -- Is_Quantified_Expression --
1298 ------------------------------
1299
1300 function Is_Quantified_Expression return Boolean is
1301 Maybe : Boolean;
1302 Scan_State : Saved_Scan_State;
1303
1304 begin
1305 Save_Scan_State (Scan_State);
1306 Scan; -- past FOR
1307 Maybe := Token = Tok_All or else Token = Tok_Some;
1308 Restore_Scan_State (Scan_State); -- to FOR
1309 return Maybe;
1310 end Is_Quantified_Expression;
1311
1312 -- Start of processing for P_Aggregate_Or_Paren_Expr
1313
1314 begin
1315 Lparen_Sloc := Token_Ptr;
1316 T_Left_Paren;
1317
1318 -- Note on parentheses count. For cases like an if expression, the
1319 -- parens here really count as real parentheses for the paren count,
1320 -- so we adjust the paren count accordingly after scanning the expr.
1321
1322 -- If expression
1323
1324 if Token = Tok_If then
1325 Expr_Node := P_If_Expression;
1326 T_Right_Paren;
1327 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1328 return Expr_Node;
1329
1330 -- Case expression
1331
1332 elsif Token = Tok_Case then
1333 Expr_Node := P_Case_Expression;
1334 T_Right_Paren;
1335 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1336 return Expr_Node;
1337
1338 -- Quantified expression
1339
1340 elsif Token = Tok_For and then Is_Quantified_Expression then
1341 Expr_Node := P_Quantified_Expression;
1342 T_Right_Paren;
1343 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1344 return Expr_Node;
1345
1346 -- Note: the mechanism used here of rescanning the initial expression
1347 -- is distinctly unpleasant, but it saves a lot of fiddling in scanning
1348 -- out the discrete choice list.
1349
1350 -- Deal with expression and extension aggregates first
1351
1352 elsif Token /= Tok_Others then
1353 Save_Scan_State (Scan_State); -- at start of expression
1354
1355 -- Deal with (NULL RECORD)
1356
1357 if Token = Tok_Null then
1358 Scan; -- past NULL
1359
1360 if Token = Tok_Record then
1361 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1362 Set_Null_Record_Present (Aggregate_Node, True);
1363 Scan; -- past RECORD
1364 T_Right_Paren;
1365 return Aggregate_Node;
1366 else
1367 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1368 end if;
1369
1370 elsif Token = Tok_For then
1371 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1372 Expr_Node := P_Iterated_Component_Association;
1373 goto Aggregate;
1374 end if;
1375
1376 -- Scan expression, handling box appearing as positional argument
1377
1378 if Token = Tok_Box then
1379 Box_Error;
1380 else
1381 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1382 end if;
1383
1384 -- Extension or Delta aggregate
1385
1386 if Token = Tok_With then
1387 if Nkind (Expr_Node) = N_Attribute_Reference
1388 and then Attribute_Name (Expr_Node) = Name_Range
1389 then
1390 Bad_Range_Attribute (Sloc (Expr_Node));
1391 return Error;
1392 end if;
1393
1394 if Ada_Version = Ada_83 then
1395 Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1396 end if;
1397
1398 Scan; -- past WITH
1399 if Token = Tok_Delta then
1400 Scan; -- past DELTA
1401 Aggregate_Node := New_Node (N_Delta_Aggregate, Lparen_Sloc);
1402 Set_Expression (Aggregate_Node, Expr_Node);
1403 Expr_Node := Empty;
1404 goto Aggregate;
1405
1406 else
1407 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1408 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1409 end if;
1410
1411 -- Deal with WITH NULL RECORD case
1412
1413 if Token = Tok_Null then
1414 Save_Scan_State (Scan_State); -- at NULL
1415 Scan; -- past NULL
1416
1417 if Token = Tok_Record then
1418 Scan; -- past RECORD
1419 Set_Null_Record_Present (Aggregate_Node, True);
1420 T_Right_Paren;
1421 return Aggregate_Node;
1422
1423 else
1424 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1425 end if;
1426 end if;
1427
1428 if Token /= Tok_Others then
1429 Save_Scan_State (Scan_State);
1430 Expr_Node := P_Expression;
1431 else
1432 Expr_Node := Empty;
1433 end if;
1434
1435 -- Expression
1436
1437 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1438 if Nkind (Expr_Node) = N_Attribute_Reference
1439 and then Attribute_Name (Expr_Node) = Name_Range
1440 then
1441 Error_Msg
1442 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1443 Scan; -- past right paren
1444 return Expr_Node;
1445 end if;
1446
1447 -- Bump paren count of expression
1448
1449 if Expr_Node /= Error then
1450 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1451 end if;
1452
1453 T_Right_Paren; -- past right paren (error message if none)
1454 return Expr_Node;
1455
1456 -- Normal aggregate
1457
1458 else
1459 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1460 end if;
1461
1462 -- Others
1463
1464 else
1465 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1466 Expr_Node := Empty;
1467 end if;
1468
1469 -- Prepare to scan list of component associations
1470 <<Aggregate>>
1471 Expr_List := No_List; -- don't set yet, maybe all named entries
1472 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1473
1474 -- This loop scans through component associations. On entry to the
1475 -- loop, an expression has been scanned at the start of the current
1476 -- association unless initial token was OTHERS, in which case
1477 -- Expr_Node is set to Empty.
1478
1479 loop
1480 -- Deal with others association first. This is a named association
1481
1482 if No (Expr_Node) then
1483 if No (Assoc_List) then
1484 Assoc_List := New_List;
1485 end if;
1486
1487 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1488
1489 -- Improper use of WITH
1490
1491 elsif Token = Tok_With then
1492 Error_Msg_SC ("WITH must be preceded by single expression in " &
1493 "extension aggregate");
1494 raise Error_Resync;
1495
1496 -- Range attribute can only appear as part of a discrete choice list
1497
1498 elsif Nkind (Expr_Node) = N_Attribute_Reference
1499 and then Attribute_Name (Expr_Node) = Name_Range
1500 and then Token /= Tok_Arrow
1501 and then Token /= Tok_Vertical_Bar
1502 then
1503 Bad_Range_Attribute (Sloc (Expr_Node));
1504 return Error;
1505
1506 -- Assume positional case if comma, right paren, or literal or
1507 -- identifier or OTHERS follows (the latter cases are missing
1508 -- comma cases). Also assume positional if a semicolon follows,
1509 -- which can happen if there are missing parens.
1510
1511 elsif Nkind (Expr_Node) = N_Iterated_Component_Association then
1512 if No (Assoc_List) then
1513 Assoc_List := New_List (Expr_Node);
1514 else
1515 Append_To (Assoc_List, Expr_Node);
1516 end if;
1517
1518 elsif Token = Tok_Comma
1519 or else Token = Tok_Right_Paren
1520 or else Token = Tok_Others
1521 or else Token in Token_Class_Lit_Or_Name
1522 or else Token = Tok_Semicolon
1523 then
1524 if Present (Assoc_List) then
1525 Error_Msg_BC -- CODEFIX
1526 ("""='>"" expected (positional association cannot follow "
1527 & "named association)");
1528 end if;
1529
1530 if No (Expr_List) then
1531 Expr_List := New_List;
1532 end if;
1533
1534 Append (Expr_Node, Expr_List);
1535
1536 -- Check for aggregate followed by left parent, maybe missing comma
1537
1538 elsif Nkind (Expr_Node) = N_Aggregate
1539 and then Token = Tok_Left_Paren
1540 then
1541 T_Comma;
1542
1543 if No (Expr_List) then
1544 Expr_List := New_List;
1545 end if;
1546
1547 Append (Expr_Node, Expr_List);
1548
1549 -- Anything else is assumed to be a named association
1550
1551 else
1552 Restore_Scan_State (Scan_State); -- to start of expression
1553
1554 if No (Assoc_List) then
1555 Assoc_List := New_List;
1556 end if;
1557
1558 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1559 end if;
1560
1561 exit when not Comma_Present;
1562
1563 -- If we are at an expression terminator, something is seriously
1564 -- wrong, so let's get out now, before we start eating up stuff
1565 -- that doesn't belong to us.
1566
1567 if Token in Token_Class_Eterm and then Token /= Tok_For then
1568 Error_Msg_AP
1569 ("expecting expression or component association");
1570 exit;
1571 end if;
1572
1573 -- Deal with misused box
1574
1575 if Token = Tok_Box then
1576 Box_Error;
1577
1578 -- Otherwise initiate for reentry to top of loop by scanning an
1579 -- initial expression, unless the first token is OTHERS or FOR,
1580 -- which indicates an iterated component association.
1581
1582 elsif Token = Tok_Others then
1583 Expr_Node := Empty;
1584
1585 elsif Token = Tok_For then
1586 Expr_Node := P_Iterated_Component_Association;
1587
1588 else
1589 Save_Scan_State (Scan_State); -- at start of expression
1590 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1591
1592 end if;
1593 end loop;
1594
1595 -- All component associations (positional and named) have been scanned
1596
1597 T_Right_Paren;
1598
1599 if Nkind (Aggregate_Node) /= N_Delta_Aggregate then
1600 Set_Expressions (Aggregate_Node, Expr_List);
1601 end if;
1602
1603 Set_Component_Associations (Aggregate_Node, Assoc_List);
1604 return Aggregate_Node;
1605 end P_Aggregate_Or_Paren_Expr;
1606
1607 ------------------------------------------------
1608 -- 4.3 Record or Array Component Association --
1609 ------------------------------------------------
1610
1611 -- RECORD_COMPONENT_ASSOCIATION ::=
1612 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1613 -- | COMPONENT_CHOICE_LIST => <>
1614
1615 -- COMPONENT_CHOICE_LIST =>
1616 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1617 -- | others
1618
1619 -- ARRAY_COMPONENT_ASSOCIATION ::=
1620 -- DISCRETE_CHOICE_LIST => EXPRESSION
1621 -- | DISCRETE_CHOICE_LIST => <>
1622 -- | ITERATED_COMPONENT_ASSOCIATION
1623
1624 -- Note: this routine only handles the named cases, including others.
1625 -- Cases where the component choice list is not present have already
1626 -- been handled directly.
1627
1628 -- Error recovery: can raise Error_Resync
1629
1630 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1631 -- rules have been extended to give support to Ada 2005 limited
1632 -- aggregates (AI-287)
1633
1634 function P_Record_Or_Array_Component_Association return Node_Id is
1635 Assoc_Node : Node_Id;
1636
1637 begin
1638 if Token = Tok_For then
1639 return P_Iterated_Component_Association;
1640 end if;
1641
1642 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1643 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1644 Set_Sloc (Assoc_Node, Token_Ptr);
1645 TF_Arrow;
1646
1647 if Token = Tok_Box then
1648
1649 -- Ada 2005(AI-287): The box notation is used to indicate the
1650 -- default initialization of aggregate components
1651
1652 if Ada_Version < Ada_2005 then
1653 Error_Msg_SP
1654 ("component association with '<'> is an Ada 2005 extension");
1655 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1656 end if;
1657
1658 Set_Box_Present (Assoc_Node);
1659 Scan; -- Past box
1660 else
1661 Set_Expression (Assoc_Node, P_Expression);
1662 end if;
1663
1664 return Assoc_Node;
1665 end P_Record_Or_Array_Component_Association;
1666
1667 -----------------------------
1668 -- 4.3.1 Record Aggregate --
1669 -----------------------------
1670
1671 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1672 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1673
1674 ----------------------------------------------
1675 -- 4.3.1 Record Component Association List --
1676 ----------------------------------------------
1677
1678 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1679
1680 ----------------------------------
1681 -- 4.3.1 Component Choice List --
1682 ----------------------------------
1683
1684 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1685
1686 --------------------------------
1687 -- 4.3.1 Extension Aggregate --
1688 --------------------------------
1689
1690 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1691
1692 --------------------------
1693 -- 4.3.1 Ancestor Part --
1694 --------------------------
1695
1696 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1697
1698 ----------------------------
1699 -- 4.3.1 Array Aggregate --
1700 ----------------------------
1701
1702 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1703
1704 ---------------------------------------
1705 -- 4.3.1 Positional Array Aggregate --
1706 ---------------------------------------
1707
1708 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1709
1710 ----------------------------------
1711 -- 4.3.1 Named Array Aggregate --
1712 ----------------------------------
1713
1714 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1715
1716 ----------------------------------------
1717 -- 4.3.1 Array Component Association --
1718 ----------------------------------------
1719
1720 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1721
1722 ---------------------
1723 -- 4.4 Expression --
1724 ---------------------
1725
1726 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1727
1728 -- EXPRESSION ::=
1729 -- RELATION {LOGICAL_OPERATOR RELATION}
1730
1731 -- CHOICE_EXPRESSION ::=
1732 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1733
1734 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1735
1736 -- On return, Expr_Form indicates the categorization of the expression
1737 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1738 -- an error message is given, and Error is returned).
1739
1740 -- Error recovery: cannot raise Error_Resync
1741
1742 function P_Expression return Node_Id is
1743 Logical_Op : Node_Kind;
1744 Prev_Logical_Op : Node_Kind;
1745 Op_Location : Source_Ptr;
1746 Node1 : Node_Id;
1747 Node2 : Node_Id;
1748
1749 begin
1750 Node1 := P_Relation;
1751
1752 if Token in Token_Class_Logop then
1753 Prev_Logical_Op := N_Empty;
1754
1755 loop
1756 Op_Location := Token_Ptr;
1757 Logical_Op := P_Logical_Operator;
1758
1759 if Prev_Logical_Op /= N_Empty and then
1760 Logical_Op /= Prev_Logical_Op
1761 then
1762 Error_Msg
1763 ("mixed logical operators in expression", Op_Location);
1764 Prev_Logical_Op := N_Empty;
1765 else
1766 Prev_Logical_Op := Logical_Op;
1767 end if;
1768
1769 Node2 := Node1;
1770 Node1 := New_Op_Node (Logical_Op, Op_Location);
1771 Set_Left_Opnd (Node1, Node2);
1772 Set_Right_Opnd (Node1, P_Relation);
1773
1774 -- Check for case of errant comma or semicolon
1775
1776 if Token = Tok_Comma or else Token = Tok_Semicolon then
1777 declare
1778 Com : constant Boolean := Token = Tok_Comma;
1779 Scan_State : Saved_Scan_State;
1780 Logop : Node_Kind;
1781
1782 begin
1783 Save_Scan_State (Scan_State); -- at comma/semicolon
1784 Scan; -- past comma/semicolon
1785
1786 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1787 -- do not deal with AND/OR because those cases get mixed up
1788 -- with the select alternatives case.
1789
1790 if Token = Tok_And or else Token = Tok_Or then
1791 Logop := P_Logical_Operator;
1792 Restore_Scan_State (Scan_State); -- to comma/semicolon
1793
1794 if Nkind_In (Logop, N_And_Then, N_Or_Else) then
1795 Scan; -- past comma/semicolon
1796
1797 if Com then
1798 Error_Msg_SP -- CODEFIX
1799 ("|extra "","" ignored");
1800 else
1801 Error_Msg_SP -- CODEFIX
1802 ("|extra "";"" ignored");
1803 end if;
1804
1805 else
1806 Restore_Scan_State (Scan_State); -- to comma/semicolon
1807 end if;
1808
1809 else
1810 Restore_Scan_State (Scan_State); -- to comma/semicolon
1811 end if;
1812 end;
1813 end if;
1814
1815 exit when Token not in Token_Class_Logop;
1816 end loop;
1817
1818 Expr_Form := EF_Non_Simple;
1819 end if;
1820
1821 if Token = Tok_Apostrophe then
1822 Bad_Range_Attribute (Token_Ptr);
1823 return Error;
1824 else
1825 return Node1;
1826 end if;
1827 end P_Expression;
1828
1829 -- This function is identical to the normal P_Expression, except that it
1830 -- also permits the appearance of a case, conditional, or quantified
1831 -- expression if the call immediately follows a left paren, and followed
1832 -- by a right parenthesis. These forms are allowed if these conditions
1833 -- are not met, but an error message will be issued.
1834
1835 function P_Expression_If_OK return Node_Id is
1836 begin
1837 -- Case of conditional, case or quantified expression
1838
1839 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1840 return P_Unparen_Cond_Case_Quant_Expression;
1841
1842 -- Normal case, not case/conditional/quantified expression
1843
1844 else
1845 return P_Expression;
1846 end if;
1847 end P_Expression_If_OK;
1848
1849 -- This function is identical to the normal P_Expression, except that it
1850 -- checks that the expression scan did not stop on a right paren. It is
1851 -- called in all contexts where a right parenthesis cannot legitimately
1852 -- follow an expression.
1853
1854 -- Error recovery: can not raise Error_Resync
1855
1856 function P_Expression_No_Right_Paren return Node_Id is
1857 Expr : constant Node_Id := P_Expression;
1858 begin
1859 Ignore (Tok_Right_Paren);
1860 return Expr;
1861 end P_Expression_No_Right_Paren;
1862
1863 ----------------------------------------
1864 -- 4.4 Expression_Or_Range_Attribute --
1865 ----------------------------------------
1866
1867 -- EXPRESSION ::=
1868 -- RELATION {and RELATION} | RELATION {and then RELATION}
1869 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1870 -- | RELATION {xor RELATION}
1871
1872 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1873
1874 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1875
1876 -- On return, Expr_Form indicates the categorization of the expression
1877 -- and EF_Range_Attr is one of the possibilities.
1878
1879 -- Error recovery: cannot raise Error_Resync
1880
1881 -- In the grammar, a RANGE attribute is simply a name, but its use is
1882 -- highly restricted, so in the parser, we do not regard it as a name.
1883 -- Instead, P_Name returns without scanning the 'RANGE part of the
1884 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1885 -- attribute reference. In the normal case where a range attribute is
1886 -- not allowed, an error message is issued by P_Expression.
1887
1888 function P_Expression_Or_Range_Attribute return Node_Id is
1889 Logical_Op : Node_Kind;
1890 Prev_Logical_Op : Node_Kind;
1891 Op_Location : Source_Ptr;
1892 Node1 : Node_Id;
1893 Node2 : Node_Id;
1894 Attr_Node : Node_Id;
1895
1896 begin
1897 Node1 := P_Relation;
1898
1899 if Token = Tok_Apostrophe then
1900 Attr_Node := P_Range_Attribute_Reference (Node1);
1901 Expr_Form := EF_Range_Attr;
1902 return Attr_Node;
1903
1904 elsif Token in Token_Class_Logop then
1905 Prev_Logical_Op := N_Empty;
1906
1907 loop
1908 Op_Location := Token_Ptr;
1909 Logical_Op := P_Logical_Operator;
1910
1911 if Prev_Logical_Op /= N_Empty and then
1912 Logical_Op /= Prev_Logical_Op
1913 then
1914 Error_Msg
1915 ("mixed logical operators in expression", Op_Location);
1916 Prev_Logical_Op := N_Empty;
1917 else
1918 Prev_Logical_Op := Logical_Op;
1919 end if;
1920
1921 Node2 := Node1;
1922 Node1 := New_Op_Node (Logical_Op, Op_Location);
1923 Set_Left_Opnd (Node1, Node2);
1924 Set_Right_Opnd (Node1, P_Relation);
1925 exit when Token not in Token_Class_Logop;
1926 end loop;
1927
1928 Expr_Form := EF_Non_Simple;
1929 end if;
1930
1931 if Token = Tok_Apostrophe then
1932 Bad_Range_Attribute (Token_Ptr);
1933 return Error;
1934 else
1935 return Node1;
1936 end if;
1937 end P_Expression_Or_Range_Attribute;
1938
1939 -- Version that allows a non-parenthesized case, conditional, or quantified
1940 -- expression if the call immediately follows a left paren, and followed
1941 -- by a right parenthesis. These forms are allowed if these conditions
1942 -- are not met, but an error message will be issued.
1943
1944 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1945 begin
1946 -- Case of conditional, case or quantified expression
1947
1948 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1949 return P_Unparen_Cond_Case_Quant_Expression;
1950
1951 -- Normal case, not one of the above expression types
1952
1953 else
1954 return P_Expression_Or_Range_Attribute;
1955 end if;
1956 end P_Expression_Or_Range_Attribute_If_OK;
1957
1958 -------------------
1959 -- 4.4 Relation --
1960 -------------------
1961
1962 -- This procedure scans both relations and choice relations
1963
1964 -- CHOICE_RELATION ::=
1965 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1966
1967 -- RELATION ::=
1968 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
1969 -- | RAISE_EXPRESSION
1970
1971 -- MEMBERSHIP_CHOICE_LIST ::=
1972 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
1973
1974 -- MEMBERSHIP_CHOICE ::=
1975 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
1976
1977 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
1978
1979 -- On return, Expr_Form indicates the categorization of the expression
1980
1981 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1982 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1983
1984 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1985 -- expression, then tokens are scanned until either a non-expression token,
1986 -- a right paren (not matched by a left paren) or a comma, is encountered.
1987
1988 function P_Relation return Node_Id is
1989 Node1, Node2 : Node_Id;
1990 Optok : Source_Ptr;
1991
1992 begin
1993 -- First check for raise expression
1994
1995 if Token = Tok_Raise then
1996 Expr_Form := EF_Non_Simple;
1997 return P_Raise_Expression;
1998 end if;
1999
2000 -- All other cases
2001
2002 Node1 := P_Simple_Expression;
2003
2004 if Token not in Token_Class_Relop then
2005 return Node1;
2006
2007 else
2008 -- Here we have a relational operator following. If so then scan it
2009 -- out. Note that the assignment symbol := is treated as a relational
2010 -- operator to improve the error recovery when it is misused for =.
2011 -- P_Relational_Operator also parses the IN and NOT IN operations.
2012
2013 Optok := Token_Ptr;
2014 Node2 := New_Op_Node (P_Relational_Operator, Optok);
2015 Set_Left_Opnd (Node2, Node1);
2016
2017 -- Case of IN or NOT IN
2018
2019 if Prev_Token = Tok_In then
2020 P_Membership_Test (Node2);
2021
2022 -- Case of relational operator (= /= < <= > >=)
2023
2024 else
2025 Set_Right_Opnd (Node2, P_Simple_Expression);
2026 end if;
2027
2028 Expr_Form := EF_Non_Simple;
2029
2030 if Token in Token_Class_Relop then
2031 Error_Msg_SC ("unexpected relational operator");
2032 raise Error_Resync;
2033 end if;
2034
2035 return Node2;
2036 end if;
2037
2038 -- If any error occurs, then scan to the next expression terminator symbol
2039 -- or comma or right paren at the outer (i.e. current) parentheses level.
2040 -- The flags are set to indicate a normal simple expression.
2041
2042 exception
2043 when Error_Resync =>
2044 Resync_Expression;
2045 Expr_Form := EF_Simple;
2046 return Error;
2047 end P_Relation;
2048
2049 ----------------------------
2050 -- 4.4 Simple Expression --
2051 ----------------------------
2052
2053 -- SIMPLE_EXPRESSION ::=
2054 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2055
2056 -- On return, Expr_Form indicates the categorization of the expression
2057
2058 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2059 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2060
2061 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2062 -- expression, then tokens are scanned until either a non-expression token,
2063 -- a right paren (not matched by a left paren) or a comma, is encountered.
2064
2065 -- Note: P_Simple_Expression is called only internally by higher level
2066 -- expression routines. In cases in the grammar where a simple expression
2067 -- is required, the approach is to scan an expression, and then post an
2068 -- appropriate error message if the expression obtained is not simple. This
2069 -- gives better error recovery and treatment.
2070
2071 function P_Simple_Expression return Node_Id is
2072 Scan_State : Saved_Scan_State;
2073 Node1 : Node_Id;
2074 Node2 : Node_Id;
2075 Tokptr : Source_Ptr;
2076
2077 function At_Start_Of_Attribute return Boolean;
2078 -- Tests if we have quote followed by attribute name, if so, return True
2079 -- otherwise return False.
2080
2081 ---------------------------
2082 -- At_Start_Of_Attribute --
2083 ---------------------------
2084
2085 function At_Start_Of_Attribute return Boolean is
2086 begin
2087 if Token /= Tok_Apostrophe then
2088 return False;
2089
2090 else
2091 declare
2092 Scan_State : Saved_Scan_State;
2093
2094 begin
2095 Save_Scan_State (Scan_State);
2096 Scan; -- past quote
2097
2098 if Token = Tok_Identifier
2099 and then Is_Attribute_Name (Chars (Token_Node))
2100 then
2101 Restore_Scan_State (Scan_State);
2102 return True;
2103 else
2104 Restore_Scan_State (Scan_State);
2105 return False;
2106 end if;
2107 end;
2108 end if;
2109 end At_Start_Of_Attribute;
2110
2111 -- Start of processing for P_Simple_Expression
2112
2113 begin
2114 -- Check for cases starting with a name. There are two reasons for
2115 -- special casing. First speed things up by catching a common case
2116 -- without going through several routine layers. Second the caller must
2117 -- be informed via Expr_Form when the simple expression is a name.
2118
2119 if Token in Token_Class_Name then
2120 Node1 := P_Name;
2121
2122 -- Deal with apostrophe cases
2123
2124 if Token = Tok_Apostrophe then
2125 Save_Scan_State (Scan_State); -- at apostrophe
2126 Scan; -- past apostrophe
2127
2128 -- If qualified expression, scan it out and fall through
2129
2130 if Token = Tok_Left_Paren then
2131 Node1 := P_Qualified_Expression (Node1);
2132 Expr_Form := EF_Simple;
2133
2134 -- If range attribute, then we return with Token pointing to the
2135 -- apostrophe. Note: avoid the normal error check on exit. We
2136 -- know that the expression really is complete in this case.
2137
2138 else -- Token = Tok_Range then
2139 Restore_Scan_State (Scan_State); -- to apostrophe
2140 Expr_Form := EF_Simple_Name;
2141 return Node1;
2142 end if;
2143 end if;
2144
2145 -- If an expression terminator follows, the previous processing
2146 -- completely scanned out the expression (a common case), and
2147 -- left Expr_Form set appropriately for returning to our caller.
2148
2149 if Token in Token_Class_Sterm then
2150 null;
2151
2152 -- If we do not have an expression terminator, then complete the
2153 -- scan of a simple expression. This code duplicates the code
2154 -- found in P_Term and P_Factor.
2155
2156 else
2157 if Token = Tok_Double_Asterisk then
2158 if Style_Check then
2159 Style.Check_Exponentiation_Operator;
2160 end if;
2161
2162 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2163 Scan; -- past **
2164 Set_Left_Opnd (Node2, Node1);
2165 Set_Right_Opnd (Node2, P_Primary);
2166 Check_Bad_Exp;
2167 Node1 := Node2;
2168 end if;
2169
2170 loop
2171 exit when Token not in Token_Class_Mulop;
2172 Tokptr := Token_Ptr;
2173 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2174
2175 if Style_Check then
2176 Style.Check_Binary_Operator;
2177 end if;
2178
2179 Scan; -- past operator
2180 Set_Left_Opnd (Node2, Node1);
2181 Set_Right_Opnd (Node2, P_Factor);
2182 Node1 := Node2;
2183 end loop;
2184
2185 loop
2186 exit when Token not in Token_Class_Binary_Addop;
2187 Tokptr := Token_Ptr;
2188 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2189
2190 if Style_Check then
2191 Style.Check_Binary_Operator;
2192 end if;
2193
2194 Scan; -- past operator
2195 Set_Left_Opnd (Node2, Node1);
2196 Set_Right_Opnd (Node2, P_Term);
2197 Node1 := Node2;
2198 end loop;
2199
2200 Expr_Form := EF_Simple;
2201 end if;
2202
2203 -- Cases where simple expression does not start with a name
2204
2205 else
2206 -- Scan initial sign and initial Term
2207
2208 if Token in Token_Class_Unary_Addop then
2209 Tokptr := Token_Ptr;
2210 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
2211
2212 if Style_Check then
2213 Style.Check_Unary_Plus_Or_Minus (Inside_Depends);
2214 end if;
2215
2216 Scan; -- past operator
2217 Set_Right_Opnd (Node1, P_Term);
2218 else
2219 Node1 := P_Term;
2220 end if;
2221
2222 -- In the following, we special-case a sequence of concatenations of
2223 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2224 -- else mixed in. For such a sequence, we return a tree representing
2225 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2226 -- the number of concatenations is large. If semantic analysis
2227 -- resolves the "&" to a predefined one, then this folding gives the
2228 -- right answer. Otherwise, semantic analysis will complain about a
2229 -- capacity-exceeded error. The purpose of this trick is to avoid
2230 -- creating a deeply nested tree, which would cause deep recursion
2231 -- during semantics, causing stack overflow. This way, we can handle
2232 -- enormous concatenations in the normal case of predefined "&". We
2233 -- first build up the normal tree, and then rewrite it if
2234 -- appropriate.
2235
2236 declare
2237 Num_Concats_Threshold : constant Positive := 1000;
2238 -- Arbitrary threshold value to enable optimization
2239
2240 First_Node : constant Node_Id := Node1;
2241 Is_Strlit_Concat : Boolean;
2242 -- True iff we've parsed a sequence of concatenations of string
2243 -- literals, with nothing else mixed in.
2244
2245 Num_Concats : Natural;
2246 -- Number of "&" operators if Is_Strlit_Concat is True
2247
2248 begin
2249 Is_Strlit_Concat :=
2250 Nkind (Node1) = N_String_Literal
2251 and then Token = Tok_Ampersand;
2252 Num_Concats := 0;
2253
2254 -- Scan out sequence of terms separated by binary adding operators
2255
2256 loop
2257 exit when Token not in Token_Class_Binary_Addop;
2258 Tokptr := Token_Ptr;
2259 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2260
2261 if Style_Check and then not Debug_Flag_Dot_QQ then
2262 Style.Check_Binary_Operator;
2263 end if;
2264
2265 Scan; -- past operator
2266 Set_Left_Opnd (Node2, Node1);
2267 Node1 := P_Term;
2268 Set_Right_Opnd (Node2, Node1);
2269
2270 -- Check if we're still concatenating string literals
2271
2272 Is_Strlit_Concat :=
2273 Is_Strlit_Concat
2274 and then Nkind (Node2) = N_Op_Concat
2275 and then Nkind (Node1) = N_String_Literal;
2276
2277 if Is_Strlit_Concat then
2278 Num_Concats := Num_Concats + 1;
2279 end if;
2280
2281 Node1 := Node2;
2282 end loop;
2283
2284 -- If we have an enormous series of concatenations of string
2285 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2286 -- flag tells semantic analysis that if the "&" is not predefined,
2287 -- the folded value is wrong.
2288
2289 if Is_Strlit_Concat
2290 and then Num_Concats >= Num_Concats_Threshold
2291 then
2292 declare
2293 Empty_String_Val : String_Id;
2294 -- String_Id for ""
2295
2296 Strlit_Concat_Val : String_Id;
2297 -- Contains the folded value (which will be correct if the
2298 -- "&" operators are the predefined ones).
2299
2300 Cur_Node : Node_Id;
2301 -- For walking up the tree
2302
2303 New_Node : Node_Id;
2304 -- Folded node to replace Node1
2305
2306 Loc : constant Source_Ptr := Sloc (First_Node);
2307
2308 begin
2309 -- Walk up the tree starting at the leftmost string literal
2310 -- (First_Node), building up the Strlit_Concat_Val as we
2311 -- go. Note that we do not use recursion here -- the whole
2312 -- point is to avoid recursively walking that enormous tree.
2313
2314 Start_String;
2315 Store_String_Chars (Strval (First_Node));
2316
2317 Cur_Node := Parent (First_Node);
2318 while Present (Cur_Node) loop
2319 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2320 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2321
2322 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2323 Cur_Node := Parent (Cur_Node);
2324 end loop;
2325
2326 Strlit_Concat_Val := End_String;
2327
2328 -- Create new folded node, and rewrite result with a concat-
2329 -- enation of an empty string literal and the folded node.
2330
2331 Start_String;
2332 Empty_String_Val := End_String;
2333 New_Node :=
2334 Make_Op_Concat (Loc,
2335 Make_String_Literal (Loc, Empty_String_Val),
2336 Make_String_Literal (Loc, Strlit_Concat_Val,
2337 Is_Folded_In_Parser => True));
2338 Rewrite (Node1, New_Node);
2339 end;
2340 end if;
2341 end;
2342
2343 -- All done, we clearly do not have name or numeric literal so this
2344 -- is a case of a simple expression which is some other possibility.
2345
2346 Expr_Form := EF_Simple;
2347 end if;
2348
2349 -- Come here at end of simple expression, where we do a couple of
2350 -- special checks to improve error recovery.
2351
2352 -- Special test to improve error recovery. If the current token is a
2353 -- period, then someone is trying to do selection on something that is
2354 -- not a name, e.g. a qualified expression.
2355
2356 if Token = Tok_Dot then
2357 Error_Msg_SC ("prefix for selection is not a name");
2358
2359 -- If qualified expression, comment and continue, otherwise something
2360 -- is pretty nasty so do an Error_Resync call.
2361
2362 if Ada_Version < Ada_2012
2363 and then Nkind (Node1) = N_Qualified_Expression
2364 then
2365 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2366 else
2367 raise Error_Resync;
2368 end if;
2369 end if;
2370
2371 -- Special test to improve error recovery: If the current token is
2372 -- not the first token on a line (as determined by checking the
2373 -- previous token position with the start of the current line),
2374 -- then we insist that we have an appropriate terminating token.
2375 -- Consider the following two examples:
2376
2377 -- 1) if A nad B then ...
2378
2379 -- 2) A := B
2380 -- C := D
2381
2382 -- In the first example, we would like to issue a binary operator
2383 -- expected message and resynchronize to the then. In the second
2384 -- example, we do not want to issue a binary operator message, so
2385 -- that instead we will get the missing semicolon message. This
2386 -- distinction is of course a heuristic which does not always work,
2387 -- but in practice it is quite effective.
2388
2389 -- Note: the one case in which we do not go through this circuit is
2390 -- when we have scanned a range attribute and want to return with
2391 -- Token pointing to the apostrophe. The apostrophe is not normally
2392 -- an expression terminator, and is not in Token_Class_Sterm, but
2393 -- in this special case we know that the expression is complete.
2394
2395 if not Token_Is_At_Start_Of_Line
2396 and then Token not in Token_Class_Sterm
2397 then
2398 -- Normally the right error message is indeed that we expected a
2399 -- binary operator, but in the case of being between a right and left
2400 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2401
2402 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2403 T_Comma;
2404
2405 -- And if we have a quote, we may have a bad attribute
2406
2407 elsif At_Start_Of_Attribute then
2408 Error_Msg_SC ("prefix of attribute must be a name");
2409
2410 if Ada_Version >= Ada_2012 then
2411 Error_Msg_SC ("\qualify expression to turn it into a name");
2412 end if;
2413
2414 -- Normal case for binary operator expected message
2415
2416 else
2417 Error_Msg_AP ("binary operator expected");
2418 end if;
2419
2420 raise Error_Resync;
2421
2422 else
2423 return Node1;
2424 end if;
2425
2426 -- If any error occurs, then scan to next expression terminator symbol
2427 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2428 -- level. Expr_Form is set to indicate a normal simple expression.
2429
2430 exception
2431 when Error_Resync =>
2432 Resync_Expression;
2433 Expr_Form := EF_Simple;
2434 return Error;
2435 end P_Simple_Expression;
2436
2437 -----------------------------------------------
2438 -- 4.4 Simple Expression or Range Attribute --
2439 -----------------------------------------------
2440
2441 -- SIMPLE_EXPRESSION ::=
2442 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2443
2444 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2445
2446 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2447
2448 -- Error recovery: cannot raise Error_Resync
2449
2450 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2451 Sexpr : Node_Id;
2452 Attr_Node : Node_Id;
2453
2454 begin
2455 -- We don't just want to roar ahead and call P_Simple_Expression
2456 -- here, since we want to handle the case of a parenthesized range
2457 -- attribute cleanly.
2458
2459 if Token = Tok_Left_Paren then
2460 declare
2461 Lptr : constant Source_Ptr := Token_Ptr;
2462 Scan_State : Saved_Scan_State;
2463
2464 begin
2465 Save_Scan_State (Scan_State);
2466 Scan; -- past left paren
2467 Sexpr := P_Simple_Expression;
2468
2469 if Token = Tok_Apostrophe then
2470 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2471 Expr_Form := EF_Range_Attr;
2472
2473 if Token = Tok_Right_Paren then
2474 Scan; -- scan past right paren if present
2475 end if;
2476
2477 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2478
2479 return Attr_Node;
2480 end if;
2481
2482 Restore_Scan_State (Scan_State);
2483 end;
2484 end if;
2485
2486 -- Here after dealing with parenthesized range attribute
2487
2488 Sexpr := P_Simple_Expression;
2489
2490 if Token = Tok_Apostrophe then
2491 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2492 Expr_Form := EF_Range_Attr;
2493 return Attr_Node;
2494
2495 else
2496 return Sexpr;
2497 end if;
2498 end P_Simple_Expression_Or_Range_Attribute;
2499
2500 ---------------
2501 -- 4.4 Term --
2502 ---------------
2503
2504 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2505
2506 -- Error recovery: can raise Error_Resync
2507
2508 function P_Term return Node_Id is
2509 Node1, Node2 : Node_Id;
2510 Tokptr : Source_Ptr;
2511
2512 begin
2513 Node1 := P_Factor;
2514
2515 loop
2516 exit when Token not in Token_Class_Mulop;
2517 Tokptr := Token_Ptr;
2518 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2519
2520 if Style_Check and then not Debug_Flag_Dot_QQ then
2521 Style.Check_Binary_Operator;
2522 end if;
2523
2524 Scan; -- past operator
2525 Set_Left_Opnd (Node2, Node1);
2526 Set_Right_Opnd (Node2, P_Factor);
2527 Node1 := Node2;
2528 end loop;
2529
2530 return Node1;
2531 end P_Term;
2532
2533 -----------------
2534 -- 4.4 Factor --
2535 -----------------
2536
2537 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2538
2539 -- Error recovery: can raise Error_Resync
2540
2541 function P_Factor return Node_Id is
2542 Node1 : Node_Id;
2543 Node2 : Node_Id;
2544
2545 begin
2546 if Token = Tok_Abs then
2547 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2548
2549 if Style_Check then
2550 Style.Check_Abs_Not;
2551 end if;
2552
2553 Scan; -- past ABS
2554 Set_Right_Opnd (Node1, P_Primary);
2555 return Node1;
2556
2557 elsif Token = Tok_Not then
2558 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2559
2560 if Style_Check then
2561 Style.Check_Abs_Not;
2562 end if;
2563
2564 Scan; -- past NOT
2565 Set_Right_Opnd (Node1, P_Primary);
2566 return Node1;
2567
2568 else
2569 Node1 := P_Primary;
2570
2571 if Token = Tok_Double_Asterisk then
2572 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2573 Scan; -- past **
2574 Set_Left_Opnd (Node2, Node1);
2575 Set_Right_Opnd (Node2, P_Primary);
2576 Check_Bad_Exp;
2577 return Node2;
2578 else
2579 return Node1;
2580 end if;
2581 end if;
2582 end P_Factor;
2583
2584 ------------------
2585 -- 4.4 Primary --
2586 ------------------
2587
2588 -- PRIMARY ::=
2589 -- NUMERIC_LITERAL | null
2590 -- | STRING_LITERAL | AGGREGATE
2591 -- | NAME | QUALIFIED_EXPRESSION
2592 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2593
2594 -- Error recovery: can raise Error_Resync
2595
2596 function P_Primary return Node_Id is
2597 Scan_State : Saved_Scan_State;
2598 Node1 : Node_Id;
2599
2600 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
2601 -- Remember if previous token is a left parenthesis. This is used to
2602 -- deal with checking whether IF/CASE/FOR expressions appearing as
2603 -- primaries require extra parenthesization.
2604
2605 begin
2606 -- The loop runs more than once only if misplaced pragmas are found
2607 -- or if a misplaced unary minus is skipped.
2608
2609 loop
2610 case Token is
2611
2612 -- Name token can start a name, call or qualified expression, all
2613 -- of which are acceptable possibilities for primary. Note also
2614 -- that string literal is included in name (as operator symbol)
2615 -- and type conversion is included in name (as indexed component).
2616
2617 when Tok_Char_Literal
2618 | Tok_Identifier
2619 | Tok_Operator_Symbol
2620 =>
2621 Node1 := P_Name;
2622
2623 -- All done unless apostrophe follows
2624
2625 if Token /= Tok_Apostrophe then
2626 return Node1;
2627
2628 -- Apostrophe following means that we have either just parsed
2629 -- the subtype mark of a qualified expression, or the prefix
2630 -- or a range attribute.
2631
2632 else -- Token = Tok_Apostrophe
2633 Save_Scan_State (Scan_State); -- at apostrophe
2634 Scan; -- past apostrophe
2635
2636 -- If range attribute, then this is always an error, since
2637 -- the only legitimate case (where the scanned expression is
2638 -- a qualified simple name) is handled at the level of the
2639 -- Simple_Expression processing. This case corresponds to a
2640 -- usage such as 3 + A'Range, which is always illegal.
2641
2642 if Token = Tok_Range then
2643 Restore_Scan_State (Scan_State); -- to apostrophe
2644 Bad_Range_Attribute (Token_Ptr);
2645 return Error;
2646
2647 -- If left paren, then we have a qualified expression.
2648 -- Note that P_Name guarantees that in this case, where
2649 -- Token = Tok_Apostrophe on return, the only two possible
2650 -- tokens following the apostrophe are left paren and
2651 -- RANGE, so we know we have a left paren here.
2652
2653 else -- Token = Tok_Left_Paren
2654 return P_Qualified_Expression (Node1);
2655
2656 end if;
2657 end if;
2658
2659 -- Numeric or string literal
2660
2661 when Tok_Integer_Literal
2662 | Tok_Real_Literal
2663 | Tok_String_Literal
2664 =>
2665 Node1 := Token_Node;
2666 Scan; -- past number
2667 return Node1;
2668
2669 -- Left paren, starts aggregate or parenthesized expression
2670
2671 when Tok_Left_Paren =>
2672 declare
2673 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2674
2675 begin
2676 if Nkind (Expr) = N_Attribute_Reference
2677 and then Attribute_Name (Expr) = Name_Range
2678 then
2679 Bad_Range_Attribute (Sloc (Expr));
2680 end if;
2681
2682 return Expr;
2683 end;
2684
2685 -- Allocator
2686
2687 when Tok_New =>
2688 return P_Allocator;
2689
2690 -- Null
2691
2692 when Tok_Null =>
2693 Scan; -- past NULL
2694 return New_Node (N_Null, Prev_Token_Ptr);
2695
2696 -- Pragma, not allowed here, so just skip past it
2697
2698 when Tok_Pragma =>
2699 P_Pragmas_Misplaced;
2700
2701 -- Deal with IF (possible unparenthesized if expression)
2702
2703 when Tok_If =>
2704
2705 -- If this looks like a real if, defined as an IF appearing at
2706 -- the start of a new line, then we consider we have a missing
2707 -- operand. If in Ada 2012 and the IF is not properly indented
2708 -- for a statement, we prefer to issue a message about an ill-
2709 -- parenthesized if expression.
2710
2711 if Token_Is_At_Start_Of_Line
2712 and then not
2713 (Ada_Version >= Ada_2012
2714 and then Style_Check_Indentation /= 0
2715 and then Start_Column rem Style_Check_Indentation /= 0)
2716 then
2717 Error_Msg_AP ("missing operand");
2718 return Error;
2719
2720 -- If this looks like an if expression, then treat it that way
2721 -- with an error message if not explicitly surrounded by
2722 -- parentheses.
2723
2724 elsif Ada_Version >= Ada_2012 then
2725 Node1 := P_If_Expression;
2726
2727 if not (Lparen and then Token = Tok_Right_Paren) then
2728 Error_Msg
2729 ("if expression must be parenthesized", Sloc (Node1));
2730 end if;
2731
2732 return Node1;
2733
2734 -- Otherwise treat as misused identifier
2735
2736 else
2737 return P_Identifier;
2738 end if;
2739
2740 -- Deal with CASE (possible unparenthesized case expression)
2741
2742 when Tok_Case =>
2743
2744 -- If this looks like a real case, defined as a CASE appearing
2745 -- the start of a new line, then we consider we have a missing
2746 -- operand. If in Ada 2012 and the CASE is not properly
2747 -- indented for a statement, we prefer to issue a message about
2748 -- an ill-parenthesized case expression.
2749
2750 if Token_Is_At_Start_Of_Line
2751 and then not
2752 (Ada_Version >= Ada_2012
2753 and then Style_Check_Indentation /= 0
2754 and then Start_Column rem Style_Check_Indentation /= 0)
2755 then
2756 Error_Msg_AP ("missing operand");
2757 return Error;
2758
2759 -- If this looks like a case expression, then treat it that way
2760 -- with an error message if not within parentheses.
2761
2762 elsif Ada_Version >= Ada_2012 then
2763 Node1 := P_Case_Expression;
2764
2765 if not (Lparen and then Token = Tok_Right_Paren) then
2766 Error_Msg
2767 ("case expression must be parenthesized", Sloc (Node1));
2768 end if;
2769
2770 return Node1;
2771
2772 -- Otherwise treat as misused identifier
2773
2774 else
2775 return P_Identifier;
2776 end if;
2777
2778 -- For [all | some] indicates a quantified expression
2779
2780 when Tok_For =>
2781 if Token_Is_At_Start_Of_Line then
2782 Error_Msg_AP ("misplaced loop");
2783 return Error;
2784
2785 elsif Ada_Version >= Ada_2012 then
2786 Save_Scan_State (Scan_State);
2787 Scan; -- past FOR
2788
2789 if Token = Tok_All or else Token = Tok_Some then
2790 Restore_Scan_State (Scan_State); -- To FOR
2791 Node1 := P_Quantified_Expression;
2792
2793 if not (Lparen and then Token = Tok_Right_Paren) then
2794 Error_Msg
2795 ("quantified expression must be parenthesized",
2796 Sloc (Node1));
2797 end if;
2798 else
2799 Restore_Scan_State (Scan_State); -- To FOR
2800 Node1 := P_Iterated_Component_Association;
2801 end if;
2802
2803 return Node1;
2804
2805 -- Otherwise treat as misused identifier
2806
2807 else
2808 return P_Identifier;
2809 end if;
2810
2811 -- Minus may well be an improper attempt at a unary minus. Give
2812 -- a message, skip the minus and keep going.
2813
2814 when Tok_Minus =>
2815 Error_Msg_SC ("parentheses required for unary minus");
2816 Scan; -- past minus
2817
2818 when Tok_At_Sign => -- AI12-0125 : target_name
2819 if Ada_Version < Ada_2020 then
2820 Error_Msg_SC ("target name is an Ada 2020 extension");
2821 Error_Msg_SC ("\compile with -gnatX");
2822 end if;
2823
2824 Node1 := P_Name;
2825 return Node1;
2826
2827 -- Anything else is illegal as the first token of a primary, but
2828 -- we test for some common errors, to improve error messages.
2829
2830 when others =>
2831 if Is_Reserved_Identifier then
2832 return P_Identifier;
2833
2834 elsif Prev_Token = Tok_Comma then
2835 Error_Msg_SP -- CODEFIX
2836 ("|extra "","" ignored");
2837 raise Error_Resync;
2838
2839 else
2840 Error_Msg_AP ("missing operand");
2841 raise Error_Resync;
2842 end if;
2843 end case;
2844 end loop;
2845 end P_Primary;
2846
2847 -------------------------------
2848 -- 4.4 Quantified_Expression --
2849 -------------------------------
2850
2851 -- QUANTIFIED_EXPRESSION ::=
2852 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
2853 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
2854
2855 function P_Quantified_Expression return Node_Id is
2856 I_Spec : Node_Id;
2857 Node1 : Node_Id;
2858
2859 begin
2860 Error_Msg_Ada_2012_Feature ("quantified expression", Token_Ptr);
2861 Scan; -- past FOR
2862 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
2863
2864 if Token = Tok_All then
2865 Set_All_Present (Node1);
2866 elsif Token /= Tok_Some then
2867 Error_Msg_AP ("missing quantifier");
2868 raise Error_Resync;
2869 end if;
2870
2871 Scan; -- past ALL or SOME
2872 I_Spec := P_Loop_Parameter_Specification;
2873
2874 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
2875 Set_Loop_Parameter_Specification (Node1, I_Spec);
2876 else
2877 Set_Iterator_Specification (Node1, I_Spec);
2878 end if;
2879
2880 if Token = Tok_Arrow then
2881 Scan;
2882 Set_Condition (Node1, P_Expression);
2883 return Node1;
2884 else
2885 Error_Msg_AP ("missing arrow");
2886 raise Error_Resync;
2887 end if;
2888 end P_Quantified_Expression;
2889
2890 ---------------------------
2891 -- 4.5 Logical Operator --
2892 ---------------------------
2893
2894 -- LOGICAL_OPERATOR ::= and | or | xor
2895
2896 -- Note: AND THEN and OR ELSE are also treated as logical operators
2897 -- by the parser (even though they are not operators semantically)
2898
2899 -- The value returned is the appropriate Node_Kind code for the operator
2900 -- On return, Token points to the token following the scanned operator.
2901
2902 -- The caller has checked that the first token is a legitimate logical
2903 -- operator token (i.e. is either XOR, AND, OR).
2904
2905 -- Error recovery: cannot raise Error_Resync
2906
2907 function P_Logical_Operator return Node_Kind is
2908 begin
2909 if Token = Tok_And then
2910 if Style_Check then
2911 Style.Check_Binary_Operator;
2912 end if;
2913
2914 Scan; -- past AND
2915
2916 if Token = Tok_Then then
2917 Scan; -- past THEN
2918 return N_And_Then;
2919 else
2920 return N_Op_And;
2921 end if;
2922
2923 elsif Token = Tok_Or then
2924 if Style_Check then
2925 Style.Check_Binary_Operator;
2926 end if;
2927
2928 Scan; -- past OR
2929
2930 if Token = Tok_Else then
2931 Scan; -- past ELSE
2932 return N_Or_Else;
2933 else
2934 return N_Op_Or;
2935 end if;
2936
2937 else -- Token = Tok_Xor
2938 if Style_Check then
2939 Style.Check_Binary_Operator;
2940 end if;
2941
2942 Scan; -- past XOR
2943 return N_Op_Xor;
2944 end if;
2945 end P_Logical_Operator;
2946
2947 ------------------------------
2948 -- 4.5 Relational Operator --
2949 ------------------------------
2950
2951 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2952
2953 -- The value returned is the appropriate Node_Kind code for the operator.
2954 -- On return, Token points to the operator token, NOT past it.
2955
2956 -- The caller has checked that the first token is a legitimate relational
2957 -- operator token (i.e. is one of the operator tokens listed above).
2958
2959 -- Error recovery: cannot raise Error_Resync
2960
2961 function P_Relational_Operator return Node_Kind is
2962 Op_Kind : Node_Kind;
2963 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2964 (Tok_Less => N_Op_Lt,
2965 Tok_Equal => N_Op_Eq,
2966 Tok_Greater => N_Op_Gt,
2967 Tok_Not_Equal => N_Op_Ne,
2968 Tok_Greater_Equal => N_Op_Ge,
2969 Tok_Less_Equal => N_Op_Le,
2970 Tok_In => N_In,
2971 Tok_Not => N_Not_In,
2972 Tok_Box => N_Op_Ne);
2973
2974 begin
2975 if Token = Tok_Box then
2976 Error_Msg_SC -- CODEFIX
2977 ("|""'<'>"" should be ""/=""");
2978 end if;
2979
2980 Op_Kind := Relop_Node (Token);
2981
2982 if Style_Check then
2983 Style.Check_Binary_Operator;
2984 end if;
2985
2986 Scan; -- past operator token
2987
2988 -- Deal with NOT IN, if previous token was NOT, we must have IN now
2989
2990 if Prev_Token = Tok_Not then
2991
2992 -- Style check, for NOT IN, we require one space between NOT and IN
2993
2994 if Style_Check and then Token = Tok_In then
2995 Style.Check_Not_In;
2996 end if;
2997
2998 T_In;
2999 end if;
3000
3001 return Op_Kind;
3002 end P_Relational_Operator;
3003
3004 ---------------------------------
3005 -- 4.5 Binary Adding Operator --
3006 ---------------------------------
3007
3008 -- BINARY_ADDING_OPERATOR ::= + | - | &
3009
3010 -- The value returned is the appropriate Node_Kind code for the operator.
3011 -- On return, Token points to the operator token (NOT past it).
3012
3013 -- The caller has checked that the first token is a legitimate adding
3014 -- operator token (i.e. is one of the operator tokens listed above).
3015
3016 -- Error recovery: cannot raise Error_Resync
3017
3018 function P_Binary_Adding_Operator return Node_Kind is
3019 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
3020 (Tok_Ampersand => N_Op_Concat,
3021 Tok_Minus => N_Op_Subtract,
3022 Tok_Plus => N_Op_Add);
3023 begin
3024 return Addop_Node (Token);
3025 end P_Binary_Adding_Operator;
3026
3027 --------------------------------
3028 -- 4.5 Unary Adding Operator --
3029 --------------------------------
3030
3031 -- UNARY_ADDING_OPERATOR ::= + | -
3032
3033 -- The value returned is the appropriate Node_Kind code for the operator.
3034 -- On return, Token points to the operator token (NOT past it).
3035
3036 -- The caller has checked that the first token is a legitimate adding
3037 -- operator token (i.e. is one of the operator tokens listed above).
3038
3039 -- Error recovery: cannot raise Error_Resync
3040
3041 function P_Unary_Adding_Operator return Node_Kind is
3042 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
3043 (Tok_Minus => N_Op_Minus,
3044 Tok_Plus => N_Op_Plus);
3045 begin
3046 return Addop_Node (Token);
3047 end P_Unary_Adding_Operator;
3048
3049 -------------------------------
3050 -- 4.5 Multiplying Operator --
3051 -------------------------------
3052
3053 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3054
3055 -- The value returned is the appropriate Node_Kind code for the operator.
3056 -- On return, Token points to the operator token (NOT past it).
3057
3058 -- The caller has checked that the first token is a legitimate multiplying
3059 -- operator token (i.e. is one of the operator tokens listed above).
3060
3061 -- Error recovery: cannot raise Error_Resync
3062
3063 function P_Multiplying_Operator return Node_Kind is
3064 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
3065 (Tok_Asterisk => N_Op_Multiply,
3066 Tok_Mod => N_Op_Mod,
3067 Tok_Rem => N_Op_Rem,
3068 Tok_Slash => N_Op_Divide);
3069 begin
3070 return Mulop_Node (Token);
3071 end P_Multiplying_Operator;
3072
3073 --------------------------------------
3074 -- 4.5 Highest Precedence Operator --
3075 --------------------------------------
3076
3077 -- Parsed by P_Factor (4.4)
3078
3079 -- Note: this rule is not in fact used by the grammar at any point
3080
3081 --------------------------
3082 -- 4.6 Type Conversion --
3083 --------------------------
3084
3085 -- Parsed by P_Primary as a Name (4.1)
3086
3087 -------------------------------
3088 -- 4.7 Qualified Expression --
3089 -------------------------------
3090
3091 -- QUALIFIED_EXPRESSION ::=
3092 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3093
3094 -- The caller has scanned the name which is the Subtype_Mark parameter
3095 -- and scanned past the single quote following the subtype mark. The
3096 -- caller has not checked that this name is in fact appropriate for
3097 -- a subtype mark name (i.e. it is a selected component or identifier).
3098
3099 -- Error_Recovery: cannot raise Error_Resync
3100
3101 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
3102 Qual_Node : Node_Id;
3103 begin
3104 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
3105 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
3106 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
3107 return Qual_Node;
3108 end P_Qualified_Expression;
3109
3110 --------------------
3111 -- 4.8 Allocator --
3112 --------------------
3113
3114 -- ALLOCATOR ::=
3115 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3116 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3117 --
3118 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3119
3120 -- The caller has checked that the initial token is NEW
3121
3122 -- Error recovery: can raise Error_Resync
3123
3124 function P_Allocator return Node_Id is
3125 Alloc_Node : Node_Id;
3126 Type_Node : Node_Id;
3127 Null_Exclusion_Present : Boolean;
3128
3129 begin
3130 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
3131 T_New;
3132
3133 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3134
3135 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3136
3137 if Token = Tok_Left_Paren then
3138 Scan; -- past (
3139 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
3140 T_Right_Paren;
3141
3142 Error_Msg_Ada_2012_Feature
3143 ("|subpool specification",
3144 Sloc (Subpool_Handle_Name (Alloc_Node)));
3145 end if;
3146
3147 Null_Exclusion_Present := P_Null_Exclusion;
3148 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
3149 Type_Node := P_Subtype_Mark_Resync;
3150
3151 if Token = Tok_Apostrophe then
3152 Scan; -- past apostrophe
3153 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
3154 else
3155 Set_Expression
3156 (Alloc_Node,
3157 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
3158
3159 -- AI05-0104: An explicit null exclusion is not allowed for an
3160 -- allocator without initialization. In previous versions of the
3161 -- language it just raises constraint error.
3162
3163 if Ada_Version >= Ada_2012 and then Null_Exclusion_Present then
3164 Error_Msg_N
3165 ("an allocator with a subtype indication "
3166 & "cannot have a null exclusion", Alloc_Node);
3167 end if;
3168 end if;
3169
3170 return Alloc_Node;
3171 end P_Allocator;
3172
3173 -----------------------
3174 -- P_Case_Expression --
3175 -----------------------
3176
3177 function P_Case_Expression return Node_Id is
3178 Loc : constant Source_Ptr := Token_Ptr;
3179 Case_Node : Node_Id;
3180 Save_State : Saved_Scan_State;
3181
3182 begin
3183 Error_Msg_Ada_2012_Feature ("|case expression", Token_Ptr);
3184 Scan; -- past CASE
3185 Case_Node :=
3186 Make_Case_Expression (Loc,
3187 Expression => P_Expression_No_Right_Paren,
3188 Alternatives => New_List);
3189 T_Is;
3190
3191 -- We now have scanned out CASE expression IS, scan alternatives
3192
3193 loop
3194 T_When;
3195 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
3196
3197 -- Missing comma if WHEN (more alternatives present)
3198
3199 if Token = Tok_When then
3200 T_Comma;
3201
3202 -- If comma/WHEN, skip comma and we have another alternative
3203
3204 elsif Token = Tok_Comma then
3205 Save_Scan_State (Save_State);
3206 Scan; -- past comma
3207
3208 if Token /= Tok_When then
3209 Restore_Scan_State (Save_State);
3210 exit;
3211 end if;
3212
3213 -- If no comma or WHEN, definitely done
3214
3215 else
3216 exit;
3217 end if;
3218 end loop;
3219
3220 -- If we have an END CASE, diagnose as not needed
3221
3222 if Token = Tok_End then
3223 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
3224 Scan; -- past END
3225
3226 if Token = Tok_Case then
3227 Scan; -- past CASE;
3228 end if;
3229 end if;
3230
3231 -- Return the Case_Expression node
3232
3233 return Case_Node;
3234 end P_Case_Expression;
3235
3236 -----------------------------------
3237 -- P_Case_Expression_Alternative --
3238 -----------------------------------
3239
3240 -- CASE_STATEMENT_ALTERNATIVE ::=
3241 -- when DISCRETE_CHOICE_LIST =>
3242 -- EXPRESSION
3243
3244 -- The caller has checked that and scanned past the initial WHEN token
3245 -- Error recovery: can raise Error_Resync
3246
3247 function P_Case_Expression_Alternative return Node_Id is
3248 Case_Alt_Node : Node_Id;
3249 begin
3250 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
3251 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
3252 TF_Arrow;
3253 Set_Expression (Case_Alt_Node, P_Expression);
3254 return Case_Alt_Node;
3255 end P_Case_Expression_Alternative;
3256
3257 --------------------------------------
3258 -- P_Iterated_Component_Association --
3259 --------------------------------------
3260
3261 -- ITERATED_COMPONENT_ASSOCIATION ::=
3262 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3263
3264 function P_Iterated_Component_Association return Node_Id is
3265 Assoc_Node : Node_Id;
3266
3267 begin
3268 Scan; -- past FOR
3269 Assoc_Node :=
3270 New_Node (N_Iterated_Component_Association, Prev_Token_Ptr);
3271 Set_Defining_Identifier (Assoc_Node, P_Defining_Identifier);
3272 T_In;
3273 Set_Discrete_Choices (Assoc_Node, P_Discrete_Choice_List);
3274 TF_Arrow;
3275 Set_Expression (Assoc_Node, P_Expression);
3276 return Assoc_Node;
3277 end P_Iterated_Component_Association;
3278
3279 ---------------------
3280 -- P_If_Expression --
3281 ---------------------
3282
3283 -- IF_EXPRESSION ::=
3284 -- if CONDITION then DEPENDENT_EXPRESSION
3285 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3286 -- [else DEPENDENT_EXPRESSION]
3287
3288 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3289
3290 function P_If_Expression return Node_Id is
3291 function P_If_Expression_Internal
3292 (Loc : Source_Ptr;
3293 Cond : Node_Id) return Node_Id;
3294 -- This is the internal recursive routine that does all the work, it is
3295 -- recursive since it is used to process ELSIF parts, which internally
3296 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3297 -- sequence is like the outer function except that the caller passes
3298 -- the conditional expression (scanned using P_Expression), and the
3299 -- scan pointer points just past this expression. Loc points to the
3300 -- IF or ELSIF token.
3301
3302 ------------------------------
3303 -- P_If_Expression_Internal --
3304 ------------------------------
3305
3306 function P_If_Expression_Internal
3307 (Loc : Source_Ptr;
3308 Cond : Node_Id) return Node_Id
3309 is
3310 Exprs : constant List_Id := New_List;
3311 Expr : Node_Id;
3312 State : Saved_Scan_State;
3313 Eptr : Source_Ptr;
3314
3315 begin
3316 -- All cases except where we are at right paren
3317
3318 if Token /= Tok_Right_Paren then
3319 TF_Then;
3320 Append_To (Exprs, P_Condition (Cond));
3321 Append_To (Exprs, P_Expression);
3322
3323 -- Case of right paren (missing THEN phrase). Note that we know this
3324 -- is the IF case, since the caller dealt with this possibility in
3325 -- the ELSIF case.
3326
3327 else
3328 Error_Msg_BC ("missing THEN phrase");
3329 Append_To (Exprs, P_Condition (Cond));
3330 end if;
3331
3332 -- We now have scanned out IF expr THEN expr
3333
3334 -- Check for common error of semicolon before the ELSE
3335
3336 if Token = Tok_Semicolon then
3337 Save_Scan_State (State);
3338 Scan; -- past semicolon
3339
3340 if Token = Tok_Else or else Token = Tok_Elsif then
3341 Error_Msg_SP -- CODEFIX
3342 ("|extra "";"" ignored");
3343
3344 else
3345 Restore_Scan_State (State);
3346 end if;
3347 end if;
3348
3349 -- Scan out ELSIF sequence if present
3350
3351 if Token = Tok_Elsif then
3352 Eptr := Token_Ptr;
3353 Scan; -- past ELSIF
3354 Expr := P_Expression;
3355
3356 -- If we are at a right paren, we assume the ELSIF should be ELSE
3357
3358 if Token = Tok_Right_Paren then
3359 Error_Msg ("ELSIF should be ELSE", Eptr);
3360 Append_To (Exprs, Expr);
3361
3362 -- Otherwise we have an OK ELSIF
3363
3364 else
3365 Expr := P_If_Expression_Internal (Eptr, Expr);
3366 Set_Is_Elsif (Expr);
3367 Append_To (Exprs, Expr);
3368 end if;
3369
3370 -- Scan out ELSE phrase if present
3371
3372 elsif Token = Tok_Else then
3373
3374 -- Scan out ELSE expression
3375
3376 Scan; -- Past ELSE
3377 Append_To (Exprs, P_Expression);
3378
3379 -- Skip redundant ELSE parts
3380
3381 while Token = Tok_Else loop
3382 Error_Msg_SC ("only one ELSE part is allowed");
3383 Scan; -- past ELSE
3384 Discard_Junk_Node (P_Expression);
3385 end loop;
3386
3387 -- Two expression case (implied True, filled in during semantics)
3388
3389 else
3390 null;
3391 end if;
3392
3393 -- If we have an END IF, diagnose as not needed
3394
3395 if Token = Tok_End then
3396 Error_Msg_SC ("`END IF` not allowed at end of if expression");
3397 Scan; -- past END
3398
3399 if Token = Tok_If then
3400 Scan; -- past IF;
3401 end if;
3402 end if;
3403
3404 -- Return the If_Expression node
3405
3406 return Make_If_Expression (Loc, Expressions => Exprs);
3407 end P_If_Expression_Internal;
3408
3409 -- Local variables
3410
3411 Loc : constant Source_Ptr := Token_Ptr;
3412 If_Expr : Node_Id;
3413
3414 -- Start of processing for P_If_Expression
3415
3416 begin
3417 Error_Msg_Ada_2012_Feature ("|if expression", Token_Ptr);
3418 Scan; -- past IF
3419 Inside_If_Expression := Inside_If_Expression + 1;
3420 If_Expr := P_If_Expression_Internal (Loc, P_Expression);
3421 Inside_If_Expression := Inside_If_Expression - 1;
3422 return If_Expr;
3423 end P_If_Expression;
3424
3425 -----------------------
3426 -- P_Membership_Test --
3427 -----------------------
3428
3429 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3430 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3431
3432 procedure P_Membership_Test (N : Node_Id) is
3433 Alt : constant Node_Id :=
3434 P_Range_Or_Subtype_Mark
3435 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3436
3437 begin
3438 -- Set case
3439
3440 if Token = Tok_Vertical_Bar then
3441 Error_Msg_Ada_2012_Feature ("set notation", Token_Ptr);
3442 Set_Alternatives (N, New_List (Alt));
3443 Set_Right_Opnd (N, Empty);
3444
3445 -- Loop to accumulate alternatives
3446
3447 while Token = Tok_Vertical_Bar loop
3448 Scan; -- past vertical bar
3449 Append_To
3450 (Alternatives (N),
3451 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3452 end loop;
3453
3454 -- Not set case
3455
3456 else
3457 Set_Right_Opnd (N, Alt);
3458 Set_Alternatives (N, No_List);
3459 end if;
3460 end P_Membership_Test;
3461
3462 ------------------------------------------
3463 -- P_Unparen_Cond_Case_Quant_Expression --
3464 ------------------------------------------
3465
3466 function P_Unparen_Cond_Case_Quant_Expression return Node_Id is
3467 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3468
3469 Result : Node_Id;
3470 Scan_State : Saved_Scan_State;
3471
3472 begin
3473 -- Case expression
3474
3475 if Token = Tok_Case then
3476 Result := P_Case_Expression;
3477
3478 if not (Lparen and then Token = Tok_Right_Paren) then
3479 Error_Msg_N ("case expression must be parenthesized!", Result);
3480 end if;
3481
3482 -- If expression
3483
3484 elsif Token = Tok_If then
3485 Result := P_If_Expression;
3486
3487 if not (Lparen and then Token = Tok_Right_Paren) then
3488 Error_Msg_N ("if expression must be parenthesized!", Result);
3489 end if;
3490
3491 -- Quantified expression or iterated component association
3492
3493 elsif Token = Tok_For then
3494
3495 Save_Scan_State (Scan_State);
3496 Scan; -- past FOR
3497
3498 if Token = Tok_All or else Token = Tok_Some then
3499 Restore_Scan_State (Scan_State);
3500 Result := P_Quantified_Expression;
3501
3502 if not (Lparen and then Token = Tok_Right_Paren) then
3503 Error_Msg_N
3504 ("quantified expression must be parenthesized!", Result);
3505 end if;
3506
3507 else
3508 -- If no quantifier keyword, this is an iterated component in
3509 -- an aggregate.
3510
3511 Restore_Scan_State (Scan_State);
3512 Result := P_Iterated_Component_Association;
3513 end if;
3514
3515 -- No other possibility should exist (caller was supposed to check)
3516
3517 else
3518 raise Program_Error;
3519 end if;
3520
3521 -- Return expression (possibly after having given message)
3522
3523 return Result;
3524 end P_Unparen_Cond_Case_Quant_Expression;
3525
3526 end Ch4;