]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/par-ch4.adb
[multiple changes]
[thirdparty/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 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 Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1399 Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1400 Scan; -- past WITH
1401
1402 -- Deal with WITH NULL RECORD case
1403
1404 if Token = Tok_Null then
1405 Save_Scan_State (Scan_State); -- at NULL
1406 Scan; -- past NULL
1407
1408 if Token = Tok_Record then
1409 Scan; -- past RECORD
1410 Set_Null_Record_Present (Aggregate_Node, True);
1411 T_Right_Paren;
1412 return Aggregate_Node;
1413
1414 else
1415 Restore_Scan_State (Scan_State); -- to NULL that must be expr
1416 end if;
1417 end if;
1418
1419 if Token /= Tok_Others then
1420 Save_Scan_State (Scan_State);
1421 Expr_Node := P_Expression;
1422 else
1423 Expr_Node := Empty;
1424 end if;
1425
1426 -- Expression
1427
1428 elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1429 if Nkind (Expr_Node) = N_Attribute_Reference
1430 and then Attribute_Name (Expr_Node) = Name_Range
1431 then
1432 Error_Msg
1433 ("|parentheses not allowed for range attribute", Lparen_Sloc);
1434 Scan; -- past right paren
1435 return Expr_Node;
1436 end if;
1437
1438 -- Bump paren count of expression
1439
1440 if Expr_Node /= Error then
1441 Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1442 end if;
1443
1444 T_Right_Paren; -- past right paren (error message if none)
1445 return Expr_Node;
1446
1447 -- Normal aggregate
1448
1449 else
1450 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1451 end if;
1452
1453 -- Others
1454
1455 else
1456 Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1457 Expr_Node := Empty;
1458 end if;
1459
1460 -- Prepare to scan list of component associations
1461 <<Aggregate>>
1462 Expr_List := No_List; -- don't set yet, maybe all named entries
1463 Assoc_List := No_List; -- don't set yet, maybe all positional entries
1464
1465 -- This loop scans through component associations. On entry to the
1466 -- loop, an expression has been scanned at the start of the current
1467 -- association unless initial token was OTHERS, in which case
1468 -- Expr_Node is set to Empty.
1469
1470 loop
1471 -- Deal with others association first. This is a named association
1472
1473 if No (Expr_Node) then
1474 if No (Assoc_List) then
1475 Assoc_List := New_List;
1476 end if;
1477
1478 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1479
1480 -- Improper use of WITH
1481
1482 elsif Token = Tok_With then
1483 Error_Msg_SC ("WITH must be preceded by single expression in " &
1484 "extension aggregate");
1485 raise Error_Resync;
1486
1487 -- Range attribute can only appear as part of a discrete choice list
1488
1489 elsif Nkind (Expr_Node) = N_Attribute_Reference
1490 and then Attribute_Name (Expr_Node) = Name_Range
1491 and then Token /= Tok_Arrow
1492 and then Token /= Tok_Vertical_Bar
1493 then
1494 Bad_Range_Attribute (Sloc (Expr_Node));
1495 return Error;
1496
1497 -- Assume positional case if comma, right paren, or literal or
1498 -- identifier or OTHERS follows (the latter cases are missing
1499 -- comma cases). Also assume positional if a semicolon follows,
1500 -- which can happen if there are missing parens.
1501
1502 elsif Nkind (Expr_Node) = N_Iterated_Component_Association then
1503 if No (Assoc_List) then
1504 Assoc_List := New_List (Expr_Node);
1505 else
1506 Append_To (Assoc_List, Expr_Node);
1507 end if;
1508
1509 elsif Token = Tok_Comma
1510 or else Token = Tok_Right_Paren
1511 or else Token = Tok_Others
1512 or else Token in Token_Class_Lit_Or_Name
1513 or else Token = Tok_Semicolon
1514 then
1515 if Present (Assoc_List) then
1516 Error_Msg_BC -- CODEFIX
1517 ("""='>"" expected (positional association cannot follow "
1518 & "named association)");
1519 end if;
1520
1521 if No (Expr_List) then
1522 Expr_List := New_List;
1523 end if;
1524
1525 Append (Expr_Node, Expr_List);
1526
1527 -- Check for aggregate followed by left parent, maybe missing comma
1528
1529 elsif Nkind (Expr_Node) = N_Aggregate
1530 and then Token = Tok_Left_Paren
1531 then
1532 T_Comma;
1533
1534 if No (Expr_List) then
1535 Expr_List := New_List;
1536 end if;
1537
1538 Append (Expr_Node, Expr_List);
1539
1540 -- Anything else is assumed to be a named association
1541
1542 else
1543 Restore_Scan_State (Scan_State); -- to start of expression
1544
1545 if No (Assoc_List) then
1546 Assoc_List := New_List;
1547 end if;
1548
1549 Append (P_Record_Or_Array_Component_Association, Assoc_List);
1550 end if;
1551
1552 exit when not Comma_Present;
1553
1554 -- If we are at an expression terminator, something is seriously
1555 -- wrong, so let's get out now, before we start eating up stuff
1556 -- that doesn't belong to us.
1557
1558 if Token in Token_Class_Eterm and then Token /= Tok_For then
1559 Error_Msg_AP
1560 ("expecting expression or component association");
1561 exit;
1562 end if;
1563
1564 -- Deal with misused box
1565
1566 if Token = Tok_Box then
1567 Box_Error;
1568
1569 -- Otherwise initiate for reentry to top of loop by scanning an
1570 -- initial expression, unless the first token is OTHERS or FOR,
1571 -- which indicates an iterated component association.
1572
1573 elsif Token = Tok_Others then
1574 Expr_Node := Empty;
1575
1576 elsif Token = Tok_For then
1577 Expr_Node := P_Iterated_Component_Association;
1578
1579 else
1580 Save_Scan_State (Scan_State); -- at start of expression
1581 Expr_Node := P_Expression_Or_Range_Attribute_If_OK;
1582
1583 end if;
1584 end loop;
1585
1586 -- All component associations (positional and named) have been scanned
1587
1588 T_Right_Paren;
1589 Set_Expressions (Aggregate_Node, Expr_List);
1590 Set_Component_Associations (Aggregate_Node, Assoc_List);
1591 return Aggregate_Node;
1592 end P_Aggregate_Or_Paren_Expr;
1593
1594 ------------------------------------------------
1595 -- 4.3 Record or Array Component Association --
1596 ------------------------------------------------
1597
1598 -- RECORD_COMPONENT_ASSOCIATION ::=
1599 -- [COMPONENT_CHOICE_LIST =>] EXPRESSION
1600 -- | COMPONENT_CHOICE_LIST => <>
1601
1602 -- COMPONENT_CHOICE_LIST =>
1603 -- component_SELECTOR_NAME {| component_SELECTOR_NAME}
1604 -- | others
1605
1606 -- ARRAY_COMPONENT_ASSOCIATION ::=
1607 -- DISCRETE_CHOICE_LIST => EXPRESSION
1608 -- | DISCRETE_CHOICE_LIST => <>
1609 -- | ITERATED_COMPONENT_ASSOCIATION
1610
1611 -- Note: this routine only handles the named cases, including others.
1612 -- Cases where the component choice list is not present have already
1613 -- been handled directly.
1614
1615 -- Error recovery: can raise Error_Resync
1616
1617 -- Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1618 -- rules have been extended to give support to Ada 2005 limited
1619 -- aggregates (AI-287)
1620
1621 function P_Record_Or_Array_Component_Association return Node_Id is
1622 Assoc_Node : Node_Id;
1623
1624 begin
1625 Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1626 Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1627 Set_Sloc (Assoc_Node, Token_Ptr);
1628 TF_Arrow;
1629
1630 if Token = Tok_Box then
1631
1632 -- Ada 2005(AI-287): The box notation is used to indicate the
1633 -- default initialization of aggregate components
1634
1635 if Ada_Version < Ada_2005 then
1636 Error_Msg_SP
1637 ("component association with '<'> is an Ada 2005 extension");
1638 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1639 end if;
1640
1641 Set_Box_Present (Assoc_Node);
1642 Scan; -- Past box
1643 else
1644 Set_Expression (Assoc_Node, P_Expression);
1645 end if;
1646
1647 return Assoc_Node;
1648 end P_Record_Or_Array_Component_Association;
1649
1650 -----------------------------
1651 -- 4.3.1 Record Aggregate --
1652 -----------------------------
1653
1654 -- Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1655 -- All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1656
1657 ----------------------------------------------
1658 -- 4.3.1 Record Component Association List --
1659 ----------------------------------------------
1660
1661 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1662
1663 ----------------------------------
1664 -- 4.3.1 Component Choice List --
1665 ----------------------------------
1666
1667 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1668
1669 --------------------------------
1670 -- 4.3.1 Extension Aggregate --
1671 --------------------------------
1672
1673 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1674
1675 --------------------------
1676 -- 4.3.1 Ancestor Part --
1677 --------------------------
1678
1679 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1680
1681 ----------------------------
1682 -- 4.3.1 Array Aggregate --
1683 ----------------------------
1684
1685 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1686
1687 ---------------------------------------
1688 -- 4.3.1 Positional Array Aggregate --
1689 ---------------------------------------
1690
1691 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1692
1693 ----------------------------------
1694 -- 4.3.1 Named Array Aggregate --
1695 ----------------------------------
1696
1697 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1698
1699 ----------------------------------------
1700 -- 4.3.1 Array Component Association --
1701 ----------------------------------------
1702
1703 -- Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1704
1705 ---------------------
1706 -- 4.4 Expression --
1707 ---------------------
1708
1709 -- This procedure parses EXPRESSION or CHOICE_EXPRESSION
1710
1711 -- EXPRESSION ::=
1712 -- RELATION {LOGICAL_OPERATOR RELATION}
1713
1714 -- CHOICE_EXPRESSION ::=
1715 -- CHOICE_RELATION {LOGICAL_OPERATOR CHOICE_RELATION}
1716
1717 -- LOGICAL_OPERATOR ::= and | and then | or | or else | xor
1718
1719 -- On return, Expr_Form indicates the categorization of the expression
1720 -- EF_Range_Attr is not a possible value (if a range attribute is found,
1721 -- an error message is given, and Error is returned).
1722
1723 -- Error recovery: cannot raise Error_Resync
1724
1725 function P_Expression return Node_Id is
1726 Logical_Op : Node_Kind;
1727 Prev_Logical_Op : Node_Kind;
1728 Op_Location : Source_Ptr;
1729 Node1 : Node_Id;
1730 Node2 : Node_Id;
1731
1732 begin
1733 Node1 := P_Relation;
1734
1735 if Token in Token_Class_Logop then
1736 Prev_Logical_Op := N_Empty;
1737
1738 loop
1739 Op_Location := Token_Ptr;
1740 Logical_Op := P_Logical_Operator;
1741
1742 if Prev_Logical_Op /= N_Empty and then
1743 Logical_Op /= Prev_Logical_Op
1744 then
1745 Error_Msg
1746 ("mixed logical operators in expression", Op_Location);
1747 Prev_Logical_Op := N_Empty;
1748 else
1749 Prev_Logical_Op := Logical_Op;
1750 end if;
1751
1752 Node2 := Node1;
1753 Node1 := New_Op_Node (Logical_Op, Op_Location);
1754 Set_Left_Opnd (Node1, Node2);
1755 Set_Right_Opnd (Node1, P_Relation);
1756
1757 -- Check for case of errant comma or semicolon
1758
1759 if Token = Tok_Comma or else Token = Tok_Semicolon then
1760 declare
1761 Com : constant Boolean := Token = Tok_Comma;
1762 Scan_State : Saved_Scan_State;
1763 Logop : Node_Kind;
1764
1765 begin
1766 Save_Scan_State (Scan_State); -- at comma/semicolon
1767 Scan; -- past comma/semicolon
1768
1769 -- Check for AND THEN or OR ELSE after comma/semicolon. We
1770 -- do not deal with AND/OR because those cases get mixed up
1771 -- with the select alternatives case.
1772
1773 if Token = Tok_And or else Token = Tok_Or then
1774 Logop := P_Logical_Operator;
1775 Restore_Scan_State (Scan_State); -- to comma/semicolon
1776
1777 if Nkind_In (Logop, N_And_Then, N_Or_Else) then
1778 Scan; -- past comma/semicolon
1779
1780 if Com then
1781 Error_Msg_SP -- CODEFIX
1782 ("|extra "","" ignored");
1783 else
1784 Error_Msg_SP -- CODEFIX
1785 ("|extra "";"" ignored");
1786 end if;
1787
1788 else
1789 Restore_Scan_State (Scan_State); -- to comma/semicolon
1790 end if;
1791
1792 else
1793 Restore_Scan_State (Scan_State); -- to comma/semicolon
1794 end if;
1795 end;
1796 end if;
1797
1798 exit when Token not in Token_Class_Logop;
1799 end loop;
1800
1801 Expr_Form := EF_Non_Simple;
1802 end if;
1803
1804 if Token = Tok_Apostrophe then
1805 Bad_Range_Attribute (Token_Ptr);
1806 return Error;
1807 else
1808 return Node1;
1809 end if;
1810 end P_Expression;
1811
1812 -- This function is identical to the normal P_Expression, except that it
1813 -- also permits the appearance of a case, conditional, or quantified
1814 -- expression if the call immediately follows a left paren, and followed
1815 -- by a right parenthesis. These forms are allowed if these conditions
1816 -- are not met, but an error message will be issued.
1817
1818 function P_Expression_If_OK return Node_Id is
1819 begin
1820 -- Case of conditional, case or quantified expression
1821
1822 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1823 return P_Unparen_Cond_Case_Quant_Expression;
1824
1825 -- Normal case, not case/conditional/quantified expression
1826
1827 else
1828 return P_Expression;
1829 end if;
1830 end P_Expression_If_OK;
1831
1832 -- This function is identical to the normal P_Expression, except that it
1833 -- checks that the expression scan did not stop on a right paren. It is
1834 -- called in all contexts where a right parenthesis cannot legitimately
1835 -- follow an expression.
1836
1837 -- Error recovery: can not raise Error_Resync
1838
1839 function P_Expression_No_Right_Paren return Node_Id is
1840 Expr : constant Node_Id := P_Expression;
1841 begin
1842 Ignore (Tok_Right_Paren);
1843 return Expr;
1844 end P_Expression_No_Right_Paren;
1845
1846 ----------------------------------------
1847 -- 4.4 Expression_Or_Range_Attribute --
1848 ----------------------------------------
1849
1850 -- EXPRESSION ::=
1851 -- RELATION {and RELATION} | RELATION {and then RELATION}
1852 -- | RELATION {or RELATION} | RELATION {or else RELATION}
1853 -- | RELATION {xor RELATION}
1854
1855 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1856
1857 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1858
1859 -- On return, Expr_Form indicates the categorization of the expression
1860 -- and EF_Range_Attr is one of the possibilities.
1861
1862 -- Error recovery: cannot raise Error_Resync
1863
1864 -- In the grammar, a RANGE attribute is simply a name, but its use is
1865 -- highly restricted, so in the parser, we do not regard it as a name.
1866 -- Instead, P_Name returns without scanning the 'RANGE part of the
1867 -- attribute, and P_Expression_Or_Range_Attribute handles the range
1868 -- attribute reference. In the normal case where a range attribute is
1869 -- not allowed, an error message is issued by P_Expression.
1870
1871 function P_Expression_Or_Range_Attribute return Node_Id is
1872 Logical_Op : Node_Kind;
1873 Prev_Logical_Op : Node_Kind;
1874 Op_Location : Source_Ptr;
1875 Node1 : Node_Id;
1876 Node2 : Node_Id;
1877 Attr_Node : Node_Id;
1878
1879 begin
1880 Node1 := P_Relation;
1881
1882 if Token = Tok_Apostrophe then
1883 Attr_Node := P_Range_Attribute_Reference (Node1);
1884 Expr_Form := EF_Range_Attr;
1885 return Attr_Node;
1886
1887 elsif Token in Token_Class_Logop then
1888 Prev_Logical_Op := N_Empty;
1889
1890 loop
1891 Op_Location := Token_Ptr;
1892 Logical_Op := P_Logical_Operator;
1893
1894 if Prev_Logical_Op /= N_Empty and then
1895 Logical_Op /= Prev_Logical_Op
1896 then
1897 Error_Msg
1898 ("mixed logical operators in expression", Op_Location);
1899 Prev_Logical_Op := N_Empty;
1900 else
1901 Prev_Logical_Op := Logical_Op;
1902 end if;
1903
1904 Node2 := Node1;
1905 Node1 := New_Op_Node (Logical_Op, Op_Location);
1906 Set_Left_Opnd (Node1, Node2);
1907 Set_Right_Opnd (Node1, P_Relation);
1908 exit when Token not in Token_Class_Logop;
1909 end loop;
1910
1911 Expr_Form := EF_Non_Simple;
1912 end if;
1913
1914 if Token = Tok_Apostrophe then
1915 Bad_Range_Attribute (Token_Ptr);
1916 return Error;
1917 else
1918 return Node1;
1919 end if;
1920 end P_Expression_Or_Range_Attribute;
1921
1922 -- Version that allows a non-parenthesized case, conditional, or quantified
1923 -- expression if the call immediately follows a left paren, and followed
1924 -- by a right parenthesis. These forms are allowed if these conditions
1925 -- are not met, but an error message will be issued.
1926
1927 function P_Expression_Or_Range_Attribute_If_OK return Node_Id is
1928 begin
1929 -- Case of conditional, case or quantified expression
1930
1931 if Token = Tok_Case or else Token = Tok_If or else Token = Tok_For then
1932 return P_Unparen_Cond_Case_Quant_Expression;
1933
1934 -- Normal case, not one of the above expression types
1935
1936 else
1937 return P_Expression_Or_Range_Attribute;
1938 end if;
1939 end P_Expression_Or_Range_Attribute_If_OK;
1940
1941 -------------------
1942 -- 4.4 Relation --
1943 -------------------
1944
1945 -- This procedure scans both relations and choice relations
1946
1947 -- CHOICE_RELATION ::=
1948 -- SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1949
1950 -- RELATION ::=
1951 -- SIMPLE_EXPRESSION [not] in MEMBERSHIP_CHOICE_LIST
1952 -- | RAISE_EXPRESSION
1953
1954 -- MEMBERSHIP_CHOICE_LIST ::=
1955 -- MEMBERSHIP_CHOICE {'|' MEMBERSHIP CHOICE}
1956
1957 -- MEMBERSHIP_CHOICE ::=
1958 -- CHOICE_EXPRESSION | RANGE | SUBTYPE_MARK
1959
1960 -- RAISE_EXPRESSION ::= raise exception_NAME [with string_EXPRESSION]
1961
1962 -- On return, Expr_Form indicates the categorization of the expression
1963
1964 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1965 -- EF_Simple_Name and the following token is RANGE (range attribute case).
1966
1967 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
1968 -- expression, then tokens are scanned until either a non-expression token,
1969 -- a right paren (not matched by a left paren) or a comma, is encountered.
1970
1971 function P_Relation return Node_Id is
1972 Node1, Node2 : Node_Id;
1973 Optok : Source_Ptr;
1974
1975 begin
1976 -- First check for raise expression
1977
1978 if Token = Tok_Raise then
1979 Expr_Form := EF_Non_Simple;
1980 return P_Raise_Expression;
1981 end if;
1982
1983 -- All other cases
1984
1985 Node1 := P_Simple_Expression;
1986
1987 if Token not in Token_Class_Relop then
1988 return Node1;
1989
1990 else
1991 -- Here we have a relational operator following. If so then scan it
1992 -- out. Note that the assignment symbol := is treated as a relational
1993 -- operator to improve the error recovery when it is misused for =.
1994 -- P_Relational_Operator also parses the IN and NOT IN operations.
1995
1996 Optok := Token_Ptr;
1997 Node2 := New_Op_Node (P_Relational_Operator, Optok);
1998 Set_Left_Opnd (Node2, Node1);
1999
2000 -- Case of IN or NOT IN
2001
2002 if Prev_Token = Tok_In then
2003 P_Membership_Test (Node2);
2004
2005 -- Case of relational operator (= /= < <= > >=)
2006
2007 else
2008 Set_Right_Opnd (Node2, P_Simple_Expression);
2009 end if;
2010
2011 Expr_Form := EF_Non_Simple;
2012
2013 if Token in Token_Class_Relop then
2014 Error_Msg_SC ("unexpected relational operator");
2015 raise Error_Resync;
2016 end if;
2017
2018 return Node2;
2019 end if;
2020
2021 -- If any error occurs, then scan to the next expression terminator symbol
2022 -- or comma or right paren at the outer (i.e. current) parentheses level.
2023 -- The flags are set to indicate a normal simple expression.
2024
2025 exception
2026 when Error_Resync =>
2027 Resync_Expression;
2028 Expr_Form := EF_Simple;
2029 return Error;
2030 end P_Relation;
2031
2032 ----------------------------
2033 -- 4.4 Simple Expression --
2034 ----------------------------
2035
2036 -- SIMPLE_EXPRESSION ::=
2037 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2038
2039 -- On return, Expr_Form indicates the categorization of the expression
2040
2041 -- Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
2042 -- EF_Simple_Name and the following token is RANGE (range attribute case).
2043
2044 -- Error recovery: cannot raise Error_Resync. If an error occurs within an
2045 -- expression, then tokens are scanned until either a non-expression token,
2046 -- a right paren (not matched by a left paren) or a comma, is encountered.
2047
2048 -- Note: P_Simple_Expression is called only internally by higher level
2049 -- expression routines. In cases in the grammar where a simple expression
2050 -- is required, the approach is to scan an expression, and then post an
2051 -- appropriate error message if the expression obtained is not simple. This
2052 -- gives better error recovery and treatment.
2053
2054 function P_Simple_Expression return Node_Id is
2055 Scan_State : Saved_Scan_State;
2056 Node1 : Node_Id;
2057 Node2 : Node_Id;
2058 Tokptr : Source_Ptr;
2059
2060 function At_Start_Of_Attribute return Boolean;
2061 -- Tests if we have quote followed by attribute name, if so, return True
2062 -- otherwise return False.
2063
2064 ---------------------------
2065 -- At_Start_Of_Attribute --
2066 ---------------------------
2067
2068 function At_Start_Of_Attribute return Boolean is
2069 begin
2070 if Token /= Tok_Apostrophe then
2071 return False;
2072
2073 else
2074 declare
2075 Scan_State : Saved_Scan_State;
2076
2077 begin
2078 Save_Scan_State (Scan_State);
2079 Scan; -- past quote
2080
2081 if Token = Tok_Identifier
2082 and then Is_Attribute_Name (Chars (Token_Node))
2083 then
2084 Restore_Scan_State (Scan_State);
2085 return True;
2086 else
2087 Restore_Scan_State (Scan_State);
2088 return False;
2089 end if;
2090 end;
2091 end if;
2092 end At_Start_Of_Attribute;
2093
2094 -- Start of processing for P_Simple_Expression
2095
2096 begin
2097 -- Check for cases starting with a name. There are two reasons for
2098 -- special casing. First speed things up by catching a common case
2099 -- without going through several routine layers. Second the caller must
2100 -- be informed via Expr_Form when the simple expression is a name.
2101
2102 if Token in Token_Class_Name then
2103 Node1 := P_Name;
2104
2105 -- Deal with apostrophe cases
2106
2107 if Token = Tok_Apostrophe then
2108 Save_Scan_State (Scan_State); -- at apostrophe
2109 Scan; -- past apostrophe
2110
2111 -- If qualified expression, scan it out and fall through
2112
2113 if Token = Tok_Left_Paren then
2114 Node1 := P_Qualified_Expression (Node1);
2115 Expr_Form := EF_Simple;
2116
2117 -- If range attribute, then we return with Token pointing to the
2118 -- apostrophe. Note: avoid the normal error check on exit. We
2119 -- know that the expression really is complete in this case.
2120
2121 else -- Token = Tok_Range then
2122 Restore_Scan_State (Scan_State); -- to apostrophe
2123 Expr_Form := EF_Simple_Name;
2124 return Node1;
2125 end if;
2126 end if;
2127
2128 -- If an expression terminator follows, the previous processing
2129 -- completely scanned out the expression (a common case), and
2130 -- left Expr_Form set appropriately for returning to our caller.
2131
2132 if Token in Token_Class_Sterm then
2133 null;
2134
2135 -- If we do not have an expression terminator, then complete the
2136 -- scan of a simple expression. This code duplicates the code
2137 -- found in P_Term and P_Factor.
2138
2139 else
2140 if Token = Tok_Double_Asterisk then
2141 if Style_Check then
2142 Style.Check_Exponentiation_Operator;
2143 end if;
2144
2145 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2146 Scan; -- past **
2147 Set_Left_Opnd (Node2, Node1);
2148 Set_Right_Opnd (Node2, P_Primary);
2149 Check_Bad_Exp;
2150 Node1 := Node2;
2151 end if;
2152
2153 loop
2154 exit when Token not in Token_Class_Mulop;
2155 Tokptr := Token_Ptr;
2156 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2157
2158 if Style_Check then
2159 Style.Check_Binary_Operator;
2160 end if;
2161
2162 Scan; -- past operator
2163 Set_Left_Opnd (Node2, Node1);
2164 Set_Right_Opnd (Node2, P_Factor);
2165 Node1 := Node2;
2166 end loop;
2167
2168 loop
2169 exit when Token not in Token_Class_Binary_Addop;
2170 Tokptr := Token_Ptr;
2171 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2172
2173 if Style_Check then
2174 Style.Check_Binary_Operator;
2175 end if;
2176
2177 Scan; -- past operator
2178 Set_Left_Opnd (Node2, Node1);
2179 Set_Right_Opnd (Node2, P_Term);
2180 Node1 := Node2;
2181 end loop;
2182
2183 Expr_Form := EF_Simple;
2184 end if;
2185
2186 -- Cases where simple expression does not start with a name
2187
2188 else
2189 -- Scan initial sign and initial Term
2190
2191 if Token in Token_Class_Unary_Addop then
2192 Tokptr := Token_Ptr;
2193 Node1 := New_Op_Node (P_Unary_Adding_Operator, Tokptr);
2194
2195 if Style_Check then
2196 Style.Check_Unary_Plus_Or_Minus (Inside_Depends);
2197 end if;
2198
2199 Scan; -- past operator
2200 Set_Right_Opnd (Node1, P_Term);
2201 else
2202 Node1 := P_Term;
2203 end if;
2204
2205 -- In the following, we special-case a sequence of concatenations of
2206 -- string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
2207 -- else mixed in. For such a sequence, we return a tree representing
2208 -- "" & "aaabbb...ccc" (a single concatenation). This is done only if
2209 -- the number of concatenations is large. If semantic analysis
2210 -- resolves the "&" to a predefined one, then this folding gives the
2211 -- right answer. Otherwise, semantic analysis will complain about a
2212 -- capacity-exceeded error. The purpose of this trick is to avoid
2213 -- creating a deeply nested tree, which would cause deep recursion
2214 -- during semantics, causing stack overflow. This way, we can handle
2215 -- enormous concatenations in the normal case of predefined "&". We
2216 -- first build up the normal tree, and then rewrite it if
2217 -- appropriate.
2218
2219 declare
2220 Num_Concats_Threshold : constant Positive := 1000;
2221 -- Arbitrary threshold value to enable optimization
2222
2223 First_Node : constant Node_Id := Node1;
2224 Is_Strlit_Concat : Boolean;
2225 -- True iff we've parsed a sequence of concatenations of string
2226 -- literals, with nothing else mixed in.
2227
2228 Num_Concats : Natural;
2229 -- Number of "&" operators if Is_Strlit_Concat is True
2230
2231 begin
2232 Is_Strlit_Concat :=
2233 Nkind (Node1) = N_String_Literal
2234 and then Token = Tok_Ampersand;
2235 Num_Concats := 0;
2236
2237 -- Scan out sequence of terms separated by binary adding operators
2238
2239 loop
2240 exit when Token not in Token_Class_Binary_Addop;
2241 Tokptr := Token_Ptr;
2242 Node2 := New_Op_Node (P_Binary_Adding_Operator, Tokptr);
2243
2244 if Style_Check and then not Debug_Flag_Dot_QQ then
2245 Style.Check_Binary_Operator;
2246 end if;
2247
2248 Scan; -- past operator
2249 Set_Left_Opnd (Node2, Node1);
2250 Node1 := P_Term;
2251 Set_Right_Opnd (Node2, Node1);
2252
2253 -- Check if we're still concatenating string literals
2254
2255 Is_Strlit_Concat :=
2256 Is_Strlit_Concat
2257 and then Nkind (Node2) = N_Op_Concat
2258 and then Nkind (Node1) = N_String_Literal;
2259
2260 if Is_Strlit_Concat then
2261 Num_Concats := Num_Concats + 1;
2262 end if;
2263
2264 Node1 := Node2;
2265 end loop;
2266
2267 -- If we have an enormous series of concatenations of string
2268 -- literals, rewrite as explained above. The Is_Folded_In_Parser
2269 -- flag tells semantic analysis that if the "&" is not predefined,
2270 -- the folded value is wrong.
2271
2272 if Is_Strlit_Concat
2273 and then Num_Concats >= Num_Concats_Threshold
2274 then
2275 declare
2276 Empty_String_Val : String_Id;
2277 -- String_Id for ""
2278
2279 Strlit_Concat_Val : String_Id;
2280 -- Contains the folded value (which will be correct if the
2281 -- "&" operators are the predefined ones).
2282
2283 Cur_Node : Node_Id;
2284 -- For walking up the tree
2285
2286 New_Node : Node_Id;
2287 -- Folded node to replace Node1
2288
2289 Loc : constant Source_Ptr := Sloc (First_Node);
2290
2291 begin
2292 -- Walk up the tree starting at the leftmost string literal
2293 -- (First_Node), building up the Strlit_Concat_Val as we
2294 -- go. Note that we do not use recursion here -- the whole
2295 -- point is to avoid recursively walking that enormous tree.
2296
2297 Start_String;
2298 Store_String_Chars (Strval (First_Node));
2299
2300 Cur_Node := Parent (First_Node);
2301 while Present (Cur_Node) loop
2302 pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
2303 Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
2304
2305 Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
2306 Cur_Node := Parent (Cur_Node);
2307 end loop;
2308
2309 Strlit_Concat_Val := End_String;
2310
2311 -- Create new folded node, and rewrite result with a concat-
2312 -- enation of an empty string literal and the folded node.
2313
2314 Start_String;
2315 Empty_String_Val := End_String;
2316 New_Node :=
2317 Make_Op_Concat (Loc,
2318 Make_String_Literal (Loc, Empty_String_Val),
2319 Make_String_Literal (Loc, Strlit_Concat_Val,
2320 Is_Folded_In_Parser => True));
2321 Rewrite (Node1, New_Node);
2322 end;
2323 end if;
2324 end;
2325
2326 -- All done, we clearly do not have name or numeric literal so this
2327 -- is a case of a simple expression which is some other possibility.
2328
2329 Expr_Form := EF_Simple;
2330 end if;
2331
2332 -- Come here at end of simple expression, where we do a couple of
2333 -- special checks to improve error recovery.
2334
2335 -- Special test to improve error recovery. If the current token is a
2336 -- period, then someone is trying to do selection on something that is
2337 -- not a name, e.g. a qualified expression.
2338
2339 if Token = Tok_Dot then
2340 Error_Msg_SC ("prefix for selection is not a name");
2341
2342 -- If qualified expression, comment and continue, otherwise something
2343 -- is pretty nasty so do an Error_Resync call.
2344
2345 if Ada_Version < Ada_2012
2346 and then Nkind (Node1) = N_Qualified_Expression
2347 then
2348 Error_Msg_SC ("\would be legal in Ada 2012 mode");
2349 else
2350 raise Error_Resync;
2351 end if;
2352 end if;
2353
2354 -- Special test to improve error recovery: If the current token is
2355 -- not the first token on a line (as determined by checking the
2356 -- previous token position with the start of the current line),
2357 -- then we insist that we have an appropriate terminating token.
2358 -- Consider the following two examples:
2359
2360 -- 1) if A nad B then ...
2361
2362 -- 2) A := B
2363 -- C := D
2364
2365 -- In the first example, we would like to issue a binary operator
2366 -- expected message and resynchronize to the then. In the second
2367 -- example, we do not want to issue a binary operator message, so
2368 -- that instead we will get the missing semicolon message. This
2369 -- distinction is of course a heuristic which does not always work,
2370 -- but in practice it is quite effective.
2371
2372 -- Note: the one case in which we do not go through this circuit is
2373 -- when we have scanned a range attribute and want to return with
2374 -- Token pointing to the apostrophe. The apostrophe is not normally
2375 -- an expression terminator, and is not in Token_Class_Sterm, but
2376 -- in this special case we know that the expression is complete.
2377
2378 if not Token_Is_At_Start_Of_Line
2379 and then Token not in Token_Class_Sterm
2380 then
2381 -- Normally the right error message is indeed that we expected a
2382 -- binary operator, but in the case of being between a right and left
2383 -- paren, e.g. in an aggregate, a more likely error is missing comma.
2384
2385 if Prev_Token = Tok_Right_Paren and then Token = Tok_Left_Paren then
2386 T_Comma;
2387
2388 -- And if we have a quote, we may have a bad attribute
2389
2390 elsif At_Start_Of_Attribute then
2391 Error_Msg_SC ("prefix of attribute must be a name");
2392
2393 if Ada_Version >= Ada_2012 then
2394 Error_Msg_SC ("\qualify expression to turn it into a name");
2395 end if;
2396
2397 -- Normal case for binary operator expected message
2398
2399 else
2400 Error_Msg_AP ("binary operator expected");
2401 end if;
2402
2403 raise Error_Resync;
2404
2405 else
2406 return Node1;
2407 end if;
2408
2409 -- If any error occurs, then scan to next expression terminator symbol
2410 -- or comma, right paren or vertical bar at the outer (i.e. current) paren
2411 -- level. Expr_Form is set to indicate a normal simple expression.
2412
2413 exception
2414 when Error_Resync =>
2415 Resync_Expression;
2416 Expr_Form := EF_Simple;
2417 return Error;
2418 end P_Simple_Expression;
2419
2420 -----------------------------------------------
2421 -- 4.4 Simple Expression or Range Attribute --
2422 -----------------------------------------------
2423
2424 -- SIMPLE_EXPRESSION ::=
2425 -- [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2426
2427 -- RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2428
2429 -- RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2430
2431 -- Error recovery: cannot raise Error_Resync
2432
2433 function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2434 Sexpr : Node_Id;
2435 Attr_Node : Node_Id;
2436
2437 begin
2438 -- We don't just want to roar ahead and call P_Simple_Expression
2439 -- here, since we want to handle the case of a parenthesized range
2440 -- attribute cleanly.
2441
2442 if Token = Tok_Left_Paren then
2443 declare
2444 Lptr : constant Source_Ptr := Token_Ptr;
2445 Scan_State : Saved_Scan_State;
2446
2447 begin
2448 Save_Scan_State (Scan_State);
2449 Scan; -- past left paren
2450 Sexpr := P_Simple_Expression;
2451
2452 if Token = Tok_Apostrophe then
2453 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2454 Expr_Form := EF_Range_Attr;
2455
2456 if Token = Tok_Right_Paren then
2457 Scan; -- scan past right paren if present
2458 end if;
2459
2460 Error_Msg ("parentheses not allowed for range attribute", Lptr);
2461
2462 return Attr_Node;
2463 end if;
2464
2465 Restore_Scan_State (Scan_State);
2466 end;
2467 end if;
2468
2469 -- Here after dealing with parenthesized range attribute
2470
2471 Sexpr := P_Simple_Expression;
2472
2473 if Token = Tok_Apostrophe then
2474 Attr_Node := P_Range_Attribute_Reference (Sexpr);
2475 Expr_Form := EF_Range_Attr;
2476 return Attr_Node;
2477
2478 else
2479 return Sexpr;
2480 end if;
2481 end P_Simple_Expression_Or_Range_Attribute;
2482
2483 ---------------
2484 -- 4.4 Term --
2485 ---------------
2486
2487 -- TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2488
2489 -- Error recovery: can raise Error_Resync
2490
2491 function P_Term return Node_Id is
2492 Node1, Node2 : Node_Id;
2493 Tokptr : Source_Ptr;
2494
2495 begin
2496 Node1 := P_Factor;
2497
2498 loop
2499 exit when Token not in Token_Class_Mulop;
2500 Tokptr := Token_Ptr;
2501 Node2 := New_Op_Node (P_Multiplying_Operator, Tokptr);
2502
2503 if Style_Check and then not Debug_Flag_Dot_QQ then
2504 Style.Check_Binary_Operator;
2505 end if;
2506
2507 Scan; -- past operator
2508 Set_Left_Opnd (Node2, Node1);
2509 Set_Right_Opnd (Node2, P_Factor);
2510 Node1 := Node2;
2511 end loop;
2512
2513 return Node1;
2514 end P_Term;
2515
2516 -----------------
2517 -- 4.4 Factor --
2518 -----------------
2519
2520 -- FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2521
2522 -- Error recovery: can raise Error_Resync
2523
2524 function P_Factor return Node_Id is
2525 Node1 : Node_Id;
2526 Node2 : Node_Id;
2527
2528 begin
2529 if Token = Tok_Abs then
2530 Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
2531
2532 if Style_Check then
2533 Style.Check_Abs_Not;
2534 end if;
2535
2536 Scan; -- past ABS
2537 Set_Right_Opnd (Node1, P_Primary);
2538 return Node1;
2539
2540 elsif Token = Tok_Not then
2541 Node1 := New_Op_Node (N_Op_Not, Token_Ptr);
2542
2543 if Style_Check then
2544 Style.Check_Abs_Not;
2545 end if;
2546
2547 Scan; -- past NOT
2548 Set_Right_Opnd (Node1, P_Primary);
2549 return Node1;
2550
2551 else
2552 Node1 := P_Primary;
2553
2554 if Token = Tok_Double_Asterisk then
2555 Node2 := New_Op_Node (N_Op_Expon, Token_Ptr);
2556 Scan; -- past **
2557 Set_Left_Opnd (Node2, Node1);
2558 Set_Right_Opnd (Node2, P_Primary);
2559 Check_Bad_Exp;
2560 return Node2;
2561 else
2562 return Node1;
2563 end if;
2564 end if;
2565 end P_Factor;
2566
2567 ------------------
2568 -- 4.4 Primary --
2569 ------------------
2570
2571 -- PRIMARY ::=
2572 -- NUMERIC_LITERAL | null
2573 -- | STRING_LITERAL | AGGREGATE
2574 -- | NAME | QUALIFIED_EXPRESSION
2575 -- | ALLOCATOR | (EXPRESSION) | QUANTIFIED_EXPRESSION
2576
2577 -- Error recovery: can raise Error_Resync
2578
2579 function P_Primary return Node_Id is
2580 Scan_State : Saved_Scan_State;
2581 Node1 : Node_Id;
2582
2583 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
2584 -- Remember if previous token is a left parenthesis. This is used to
2585 -- deal with checking whether IF/CASE/FOR expressions appearing as
2586 -- primaries require extra parenthesization.
2587
2588 begin
2589 -- The loop runs more than once only if misplaced pragmas are found
2590 -- or if a misplaced unary minus is skipped.
2591
2592 loop
2593 case Token is
2594
2595 -- Name token can start a name, call or qualified expression, all
2596 -- of which are acceptable possibilities for primary. Note also
2597 -- that string literal is included in name (as operator symbol)
2598 -- and type conversion is included in name (as indexed component).
2599
2600 when Tok_Char_Literal
2601 | Tok_Identifier
2602 | Tok_Operator_Symbol
2603 =>
2604 Node1 := P_Name;
2605
2606 -- All done unless apostrophe follows
2607
2608 if Token /= Tok_Apostrophe then
2609 return Node1;
2610
2611 -- Apostrophe following means that we have either just parsed
2612 -- the subtype mark of a qualified expression, or the prefix
2613 -- or a range attribute.
2614
2615 else -- Token = Tok_Apostrophe
2616 Save_Scan_State (Scan_State); -- at apostrophe
2617 Scan; -- past apostrophe
2618
2619 -- If range attribute, then this is always an error, since
2620 -- the only legitimate case (where the scanned expression is
2621 -- a qualified simple name) is handled at the level of the
2622 -- Simple_Expression processing. This case corresponds to a
2623 -- usage such as 3 + A'Range, which is always illegal.
2624
2625 if Token = Tok_Range then
2626 Restore_Scan_State (Scan_State); -- to apostrophe
2627 Bad_Range_Attribute (Token_Ptr);
2628 return Error;
2629
2630 -- If left paren, then we have a qualified expression.
2631 -- Note that P_Name guarantees that in this case, where
2632 -- Token = Tok_Apostrophe on return, the only two possible
2633 -- tokens following the apostrophe are left paren and
2634 -- RANGE, so we know we have a left paren here.
2635
2636 else -- Token = Tok_Left_Paren
2637 return P_Qualified_Expression (Node1);
2638
2639 end if;
2640 end if;
2641
2642 -- Numeric or string literal
2643
2644 when Tok_Integer_Literal
2645 | Tok_Real_Literal
2646 | Tok_String_Literal
2647 =>
2648 Node1 := Token_Node;
2649 Scan; -- past number
2650 return Node1;
2651
2652 -- Left paren, starts aggregate or parenthesized expression
2653
2654 when Tok_Left_Paren =>
2655 declare
2656 Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2657
2658 begin
2659 if Nkind (Expr) = N_Attribute_Reference
2660 and then Attribute_Name (Expr) = Name_Range
2661 then
2662 Bad_Range_Attribute (Sloc (Expr));
2663 end if;
2664
2665 return Expr;
2666 end;
2667
2668 -- Allocator
2669
2670 when Tok_New =>
2671 return P_Allocator;
2672
2673 -- Null
2674
2675 when Tok_Null =>
2676 Scan; -- past NULL
2677 return New_Node (N_Null, Prev_Token_Ptr);
2678
2679 -- Pragma, not allowed here, so just skip past it
2680
2681 when Tok_Pragma =>
2682 P_Pragmas_Misplaced;
2683
2684 -- Deal with IF (possible unparenthesized if expression)
2685
2686 when Tok_If =>
2687
2688 -- If this looks like a real if, defined as an IF appearing at
2689 -- the start of a new line, then we consider we have a missing
2690 -- operand. If in Ada 2012 and the IF is not properly indented
2691 -- for a statement, we prefer to issue a message about an ill-
2692 -- parenthesized if expression.
2693
2694 if Token_Is_At_Start_Of_Line
2695 and then not
2696 (Ada_Version >= Ada_2012
2697 and then Style_Check_Indentation /= 0
2698 and then Start_Column rem Style_Check_Indentation /= 0)
2699 then
2700 Error_Msg_AP ("missing operand");
2701 return Error;
2702
2703 -- If this looks like an if expression, then treat it that way
2704 -- with an error message if not explicitly surrounded by
2705 -- parentheses.
2706
2707 elsif Ada_Version >= Ada_2012 then
2708 Node1 := P_If_Expression;
2709
2710 if not (Lparen and then Token = Tok_Right_Paren) then
2711 Error_Msg
2712 ("if expression must be parenthesized", Sloc (Node1));
2713 end if;
2714
2715 return Node1;
2716
2717 -- Otherwise treat as misused identifier
2718
2719 else
2720 return P_Identifier;
2721 end if;
2722
2723 -- Deal with CASE (possible unparenthesized case expression)
2724
2725 when Tok_Case =>
2726
2727 -- If this looks like a real case, defined as a CASE appearing
2728 -- the start of a new line, then we consider we have a missing
2729 -- operand. If in Ada 2012 and the CASE is not properly
2730 -- indented for a statement, we prefer to issue a message about
2731 -- an ill-parenthesized case expression.
2732
2733 if Token_Is_At_Start_Of_Line
2734 and then not
2735 (Ada_Version >= Ada_2012
2736 and then Style_Check_Indentation /= 0
2737 and then Start_Column rem Style_Check_Indentation /= 0)
2738 then
2739 Error_Msg_AP ("missing operand");
2740 return Error;
2741
2742 -- If this looks like a case expression, then treat it that way
2743 -- with an error message if not within parentheses.
2744
2745 elsif Ada_Version >= Ada_2012 then
2746 Node1 := P_Case_Expression;
2747
2748 if not (Lparen and then Token = Tok_Right_Paren) then
2749 Error_Msg
2750 ("case expression must be parenthesized", Sloc (Node1));
2751 end if;
2752
2753 return Node1;
2754
2755 -- Otherwise treat as misused identifier
2756
2757 else
2758 return P_Identifier;
2759 end if;
2760
2761 -- For [all | some] indicates a quantified expression
2762
2763 when Tok_For =>
2764 if Token_Is_At_Start_Of_Line then
2765 Error_Msg_AP ("misplaced loop");
2766 return Error;
2767
2768 elsif Ada_Version >= Ada_2012 then
2769 Save_Scan_State (Scan_State);
2770 Scan; -- past FOR
2771
2772 if Token = Tok_All or else Token = Tok_Some then
2773 Restore_Scan_State (Scan_State); -- To FOR
2774 Node1 := P_Quantified_Expression;
2775
2776 if not (Lparen and then Token = Tok_Right_Paren) then
2777 Error_Msg
2778 ("quantified expression must be parenthesized",
2779 Sloc (Node1));
2780 end if;
2781 else
2782 Restore_Scan_State (Scan_State); -- To FOR
2783 Node1 := P_Iterated_Component_Association;
2784 end if;
2785
2786 return Node1;
2787
2788 -- Otherwise treat as misused identifier
2789
2790 else
2791 return P_Identifier;
2792 end if;
2793
2794 -- Minus may well be an improper attempt at a unary minus. Give
2795 -- a message, skip the minus and keep going.
2796
2797 when Tok_Minus =>
2798 Error_Msg_SC ("parentheses required for unary minus");
2799 Scan; -- past minus
2800
2801 when Tok_At_Sign => -- AI12-0125 : target_name
2802 if Ada_Version < Ada_2020 then
2803 Error_Msg_SC ("target name is an Ada 2020 extension");
2804 Error_Msg_SC ("\compile with -gnatX");
2805 end if;
2806
2807 Node1 := P_Name;
2808 return Node1;
2809
2810 -- Anything else is illegal as the first token of a primary, but
2811 -- we test for some common errors, to improve error messages.
2812
2813 when others =>
2814 if Is_Reserved_Identifier then
2815 return P_Identifier;
2816
2817 elsif Prev_Token = Tok_Comma then
2818 Error_Msg_SP -- CODEFIX
2819 ("|extra "","" ignored");
2820 raise Error_Resync;
2821
2822 else
2823 Error_Msg_AP ("missing operand");
2824 raise Error_Resync;
2825 end if;
2826 end case;
2827 end loop;
2828 end P_Primary;
2829
2830 -------------------------------
2831 -- 4.4 Quantified_Expression --
2832 -------------------------------
2833
2834 -- QUANTIFIED_EXPRESSION ::=
2835 -- for QUANTIFIER LOOP_PARAMETER_SPECIFICATION => PREDICATE |
2836 -- for QUANTIFIER ITERATOR_SPECIFICATION => PREDICATE
2837
2838 function P_Quantified_Expression return Node_Id is
2839 I_Spec : Node_Id;
2840 Node1 : Node_Id;
2841
2842 begin
2843 Error_Msg_Ada_2012_Feature ("quantified expression", Token_Ptr);
2844 Scan; -- past FOR
2845 Node1 := New_Node (N_Quantified_Expression, Prev_Token_Ptr);
2846
2847 if Token = Tok_All then
2848 Set_All_Present (Node1);
2849 elsif Token /= Tok_Some then
2850 Error_Msg_AP ("missing quantifier");
2851 raise Error_Resync;
2852 end if;
2853
2854 Scan; -- past ALL or SOME
2855 I_Spec := P_Loop_Parameter_Specification;
2856
2857 if Nkind (I_Spec) = N_Loop_Parameter_Specification then
2858 Set_Loop_Parameter_Specification (Node1, I_Spec);
2859 else
2860 Set_Iterator_Specification (Node1, I_Spec);
2861 end if;
2862
2863 if Token = Tok_Arrow then
2864 Scan;
2865 Set_Condition (Node1, P_Expression);
2866 return Node1;
2867 else
2868 Error_Msg_AP ("missing arrow");
2869 raise Error_Resync;
2870 end if;
2871 end P_Quantified_Expression;
2872
2873 ---------------------------
2874 -- 4.5 Logical Operator --
2875 ---------------------------
2876
2877 -- LOGICAL_OPERATOR ::= and | or | xor
2878
2879 -- Note: AND THEN and OR ELSE are also treated as logical operators
2880 -- by the parser (even though they are not operators semantically)
2881
2882 -- The value returned is the appropriate Node_Kind code for the operator
2883 -- On return, Token points to the token following the scanned operator.
2884
2885 -- The caller has checked that the first token is a legitimate logical
2886 -- operator token (i.e. is either XOR, AND, OR).
2887
2888 -- Error recovery: cannot raise Error_Resync
2889
2890 function P_Logical_Operator return Node_Kind is
2891 begin
2892 if Token = Tok_And then
2893 if Style_Check then
2894 Style.Check_Binary_Operator;
2895 end if;
2896
2897 Scan; -- past AND
2898
2899 if Token = Tok_Then then
2900 Scan; -- past THEN
2901 return N_And_Then;
2902 else
2903 return N_Op_And;
2904 end if;
2905
2906 elsif Token = Tok_Or then
2907 if Style_Check then
2908 Style.Check_Binary_Operator;
2909 end if;
2910
2911 Scan; -- past OR
2912
2913 if Token = Tok_Else then
2914 Scan; -- past ELSE
2915 return N_Or_Else;
2916 else
2917 return N_Op_Or;
2918 end if;
2919
2920 else -- Token = Tok_Xor
2921 if Style_Check then
2922 Style.Check_Binary_Operator;
2923 end if;
2924
2925 Scan; -- past XOR
2926 return N_Op_Xor;
2927 end if;
2928 end P_Logical_Operator;
2929
2930 ------------------------------
2931 -- 4.5 Relational Operator --
2932 ------------------------------
2933
2934 -- RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2935
2936 -- The value returned is the appropriate Node_Kind code for the operator.
2937 -- On return, Token points to the operator token, NOT past it.
2938
2939 -- The caller has checked that the first token is a legitimate relational
2940 -- operator token (i.e. is one of the operator tokens listed above).
2941
2942 -- Error recovery: cannot raise Error_Resync
2943
2944 function P_Relational_Operator return Node_Kind is
2945 Op_Kind : Node_Kind;
2946 Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2947 (Tok_Less => N_Op_Lt,
2948 Tok_Equal => N_Op_Eq,
2949 Tok_Greater => N_Op_Gt,
2950 Tok_Not_Equal => N_Op_Ne,
2951 Tok_Greater_Equal => N_Op_Ge,
2952 Tok_Less_Equal => N_Op_Le,
2953 Tok_In => N_In,
2954 Tok_Not => N_Not_In,
2955 Tok_Box => N_Op_Ne);
2956
2957 begin
2958 if Token = Tok_Box then
2959 Error_Msg_SC -- CODEFIX
2960 ("|""'<'>"" should be ""/=""");
2961 end if;
2962
2963 Op_Kind := Relop_Node (Token);
2964
2965 if Style_Check then
2966 Style.Check_Binary_Operator;
2967 end if;
2968
2969 Scan; -- past operator token
2970
2971 -- Deal with NOT IN, if previous token was NOT, we must have IN now
2972
2973 if Prev_Token = Tok_Not then
2974
2975 -- Style check, for NOT IN, we require one space between NOT and IN
2976
2977 if Style_Check and then Token = Tok_In then
2978 Style.Check_Not_In;
2979 end if;
2980
2981 T_In;
2982 end if;
2983
2984 return Op_Kind;
2985 end P_Relational_Operator;
2986
2987 ---------------------------------
2988 -- 4.5 Binary Adding Operator --
2989 ---------------------------------
2990
2991 -- BINARY_ADDING_OPERATOR ::= + | - | &
2992
2993 -- The value returned is the appropriate Node_Kind code for the operator.
2994 -- On return, Token points to the operator token (NOT past it).
2995
2996 -- The caller has checked that the first token is a legitimate adding
2997 -- operator token (i.e. is one of the operator tokens listed above).
2998
2999 -- Error recovery: cannot raise Error_Resync
3000
3001 function P_Binary_Adding_Operator return Node_Kind is
3002 Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
3003 (Tok_Ampersand => N_Op_Concat,
3004 Tok_Minus => N_Op_Subtract,
3005 Tok_Plus => N_Op_Add);
3006 begin
3007 return Addop_Node (Token);
3008 end P_Binary_Adding_Operator;
3009
3010 --------------------------------
3011 -- 4.5 Unary Adding Operator --
3012 --------------------------------
3013
3014 -- UNARY_ADDING_OPERATOR ::= + | -
3015
3016 -- The value returned is the appropriate Node_Kind code for the operator.
3017 -- On return, Token points to the operator token (NOT past it).
3018
3019 -- The caller has checked that the first token is a legitimate adding
3020 -- operator token (i.e. is one of the operator tokens listed above).
3021
3022 -- Error recovery: cannot raise Error_Resync
3023
3024 function P_Unary_Adding_Operator return Node_Kind is
3025 Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
3026 (Tok_Minus => N_Op_Minus,
3027 Tok_Plus => N_Op_Plus);
3028 begin
3029 return Addop_Node (Token);
3030 end P_Unary_Adding_Operator;
3031
3032 -------------------------------
3033 -- 4.5 Multiplying Operator --
3034 -------------------------------
3035
3036 -- MULTIPLYING_OPERATOR ::= * | / | mod | rem
3037
3038 -- The value returned is the appropriate Node_Kind code for the operator.
3039 -- On return, Token points to the operator token (NOT past it).
3040
3041 -- The caller has checked that the first token is a legitimate multiplying
3042 -- operator token (i.e. is one of the operator tokens listed above).
3043
3044 -- Error recovery: cannot raise Error_Resync
3045
3046 function P_Multiplying_Operator return Node_Kind is
3047 Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
3048 (Tok_Asterisk => N_Op_Multiply,
3049 Tok_Mod => N_Op_Mod,
3050 Tok_Rem => N_Op_Rem,
3051 Tok_Slash => N_Op_Divide);
3052 begin
3053 return Mulop_Node (Token);
3054 end P_Multiplying_Operator;
3055
3056 --------------------------------------
3057 -- 4.5 Highest Precedence Operator --
3058 --------------------------------------
3059
3060 -- Parsed by P_Factor (4.4)
3061
3062 -- Note: this rule is not in fact used by the grammar at any point
3063
3064 --------------------------
3065 -- 4.6 Type Conversion --
3066 --------------------------
3067
3068 -- Parsed by P_Primary as a Name (4.1)
3069
3070 -------------------------------
3071 -- 4.7 Qualified Expression --
3072 -------------------------------
3073
3074 -- QUALIFIED_EXPRESSION ::=
3075 -- SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
3076
3077 -- The caller has scanned the name which is the Subtype_Mark parameter
3078 -- and scanned past the single quote following the subtype mark. The
3079 -- caller has not checked that this name is in fact appropriate for
3080 -- a subtype mark name (i.e. it is a selected component or identifier).
3081
3082 -- Error_Recovery: cannot raise Error_Resync
3083
3084 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
3085 Qual_Node : Node_Id;
3086 begin
3087 Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
3088 Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
3089 Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
3090 return Qual_Node;
3091 end P_Qualified_Expression;
3092
3093 --------------------
3094 -- 4.8 Allocator --
3095 --------------------
3096
3097 -- ALLOCATOR ::=
3098 -- new [SUBPOOL_SPECIFICATION] SUBTYPE_INDICATION
3099 -- | new [SUBPOOL_SPECIFICATION] QUALIFIED_EXPRESSION
3100 --
3101 -- SUBPOOL_SPECIFICATION ::= (subpool_handle_NAME)
3102
3103 -- The caller has checked that the initial token is NEW
3104
3105 -- Error recovery: can raise Error_Resync
3106
3107 function P_Allocator return Node_Id is
3108 Alloc_Node : Node_Id;
3109 Type_Node : Node_Id;
3110 Null_Exclusion_Present : Boolean;
3111
3112 begin
3113 Alloc_Node := New_Node (N_Allocator, Token_Ptr);
3114 T_New;
3115
3116 -- Scan subpool_specification if present (Ada 2012 (AI05-0111-3))
3117
3118 -- Scan Null_Exclusion if present (Ada 2005 (AI-231))
3119
3120 if Token = Tok_Left_Paren then
3121 Scan; -- past (
3122 Set_Subpool_Handle_Name (Alloc_Node, P_Name);
3123 T_Right_Paren;
3124
3125 Error_Msg_Ada_2012_Feature
3126 ("|subpool specification",
3127 Sloc (Subpool_Handle_Name (Alloc_Node)));
3128 end if;
3129
3130 Null_Exclusion_Present := P_Null_Exclusion;
3131 Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
3132 Type_Node := P_Subtype_Mark_Resync;
3133
3134 if Token = Tok_Apostrophe then
3135 Scan; -- past apostrophe
3136 Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
3137 else
3138 Set_Expression
3139 (Alloc_Node,
3140 P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
3141
3142 -- AI05-0104: An explicit null exclusion is not allowed for an
3143 -- allocator without initialization. In previous versions of the
3144 -- language it just raises constraint error.
3145
3146 if Ada_Version >= Ada_2012 and then Null_Exclusion_Present then
3147 Error_Msg_N
3148 ("an allocator with a subtype indication "
3149 & "cannot have a null exclusion", Alloc_Node);
3150 end if;
3151 end if;
3152
3153 return Alloc_Node;
3154 end P_Allocator;
3155
3156 -----------------------
3157 -- P_Case_Expression --
3158 -----------------------
3159
3160 function P_Case_Expression return Node_Id is
3161 Loc : constant Source_Ptr := Token_Ptr;
3162 Case_Node : Node_Id;
3163 Save_State : Saved_Scan_State;
3164
3165 begin
3166 Error_Msg_Ada_2012_Feature ("|case expression", Token_Ptr);
3167 Scan; -- past CASE
3168 Case_Node :=
3169 Make_Case_Expression (Loc,
3170 Expression => P_Expression_No_Right_Paren,
3171 Alternatives => New_List);
3172 T_Is;
3173
3174 -- We now have scanned out CASE expression IS, scan alternatives
3175
3176 loop
3177 T_When;
3178 Append_To (Alternatives (Case_Node), P_Case_Expression_Alternative);
3179
3180 -- Missing comma if WHEN (more alternatives present)
3181
3182 if Token = Tok_When then
3183 T_Comma;
3184
3185 -- If comma/WHEN, skip comma and we have another alternative
3186
3187 elsif Token = Tok_Comma then
3188 Save_Scan_State (Save_State);
3189 Scan; -- past comma
3190
3191 if Token /= Tok_When then
3192 Restore_Scan_State (Save_State);
3193 exit;
3194 end if;
3195
3196 -- If no comma or WHEN, definitely done
3197
3198 else
3199 exit;
3200 end if;
3201 end loop;
3202
3203 -- If we have an END CASE, diagnose as not needed
3204
3205 if Token = Tok_End then
3206 Error_Msg_SC ("`END CASE` not allowed at end of case expression");
3207 Scan; -- past END
3208
3209 if Token = Tok_Case then
3210 Scan; -- past CASE;
3211 end if;
3212 end if;
3213
3214 -- Return the Case_Expression node
3215
3216 return Case_Node;
3217 end P_Case_Expression;
3218
3219 -----------------------------------
3220 -- P_Case_Expression_Alternative --
3221 -----------------------------------
3222
3223 -- CASE_STATEMENT_ALTERNATIVE ::=
3224 -- when DISCRETE_CHOICE_LIST =>
3225 -- EXPRESSION
3226
3227 -- The caller has checked that and scanned past the initial WHEN token
3228 -- Error recovery: can raise Error_Resync
3229
3230 function P_Case_Expression_Alternative return Node_Id is
3231 Case_Alt_Node : Node_Id;
3232 begin
3233 Case_Alt_Node := New_Node (N_Case_Expression_Alternative, Token_Ptr);
3234 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
3235 TF_Arrow;
3236 Set_Expression (Case_Alt_Node, P_Expression);
3237 return Case_Alt_Node;
3238 end P_Case_Expression_Alternative;
3239
3240 --------------------------------------
3241 -- P_Iterated_Component_Association --
3242 --------------------------------------
3243
3244 -- ITERATED_COMPONENT_ASSOCIATION ::=
3245 -- for DEFINING_IDENTIFIER in DISCRETE_CHOICE_LIST => EXPRESSION
3246
3247 function P_Iterated_Component_Association return Node_Id is
3248 Assoc_Node : Node_Id;
3249
3250 begin
3251 Scan; -- past FOR
3252 Assoc_Node :=
3253 New_Node (N_Iterated_Component_Association, Prev_Token_Ptr);
3254 Set_Defining_Identifier (Assoc_Node, P_Defining_Identifier);
3255 T_In;
3256 Set_Discrete_Choices (Assoc_Node, P_Discrete_Choice_List);
3257 TF_Arrow;
3258 Set_Expression (Assoc_Node, P_Expression);
3259 return Assoc_Node;
3260 end P_Iterated_Component_Association;
3261
3262 ---------------------
3263 -- P_If_Expression --
3264 ---------------------
3265
3266 -- IF_EXPRESSION ::=
3267 -- if CONDITION then DEPENDENT_EXPRESSION
3268 -- {elsif CONDITION then DEPENDENT_EXPRESSION}
3269 -- [else DEPENDENT_EXPRESSION]
3270
3271 -- DEPENDENT_EXPRESSION ::= EXPRESSION
3272
3273 function P_If_Expression return Node_Id is
3274 function P_If_Expression_Internal
3275 (Loc : Source_Ptr;
3276 Cond : Node_Id) return Node_Id;
3277 -- This is the internal recursive routine that does all the work, it is
3278 -- recursive since it is used to process ELSIF parts, which internally
3279 -- are N_If_Expression nodes with the Is_Elsif flag set. The calling
3280 -- sequence is like the outer function except that the caller passes
3281 -- the conditional expression (scanned using P_Expression), and the
3282 -- scan pointer points just past this expression. Loc points to the
3283 -- IF or ELSIF token.
3284
3285 ------------------------------
3286 -- P_If_Expression_Internal --
3287 ------------------------------
3288
3289 function P_If_Expression_Internal
3290 (Loc : Source_Ptr;
3291 Cond : Node_Id) return Node_Id
3292 is
3293 Exprs : constant List_Id := New_List;
3294 Expr : Node_Id;
3295 State : Saved_Scan_State;
3296 Eptr : Source_Ptr;
3297
3298 begin
3299 -- All cases except where we are at right paren
3300
3301 if Token /= Tok_Right_Paren then
3302 TF_Then;
3303 Append_To (Exprs, P_Condition (Cond));
3304 Append_To (Exprs, P_Expression);
3305
3306 -- Case of right paren (missing THEN phrase). Note that we know this
3307 -- is the IF case, since the caller dealt with this possibility in
3308 -- the ELSIF case.
3309
3310 else
3311 Error_Msg_BC ("missing THEN phrase");
3312 Append_To (Exprs, P_Condition (Cond));
3313 end if;
3314
3315 -- We now have scanned out IF expr THEN expr
3316
3317 -- Check for common error of semicolon before the ELSE
3318
3319 if Token = Tok_Semicolon then
3320 Save_Scan_State (State);
3321 Scan; -- past semicolon
3322
3323 if Token = Tok_Else or else Token = Tok_Elsif then
3324 Error_Msg_SP -- CODEFIX
3325 ("|extra "";"" ignored");
3326
3327 else
3328 Restore_Scan_State (State);
3329 end if;
3330 end if;
3331
3332 -- Scan out ELSIF sequence if present
3333
3334 if Token = Tok_Elsif then
3335 Eptr := Token_Ptr;
3336 Scan; -- past ELSIF
3337 Expr := P_Expression;
3338
3339 -- If we are at a right paren, we assume the ELSIF should be ELSE
3340
3341 if Token = Tok_Right_Paren then
3342 Error_Msg ("ELSIF should be ELSE", Eptr);
3343 Append_To (Exprs, Expr);
3344
3345 -- Otherwise we have an OK ELSIF
3346
3347 else
3348 Expr := P_If_Expression_Internal (Eptr, Expr);
3349 Set_Is_Elsif (Expr);
3350 Append_To (Exprs, Expr);
3351 end if;
3352
3353 -- Scan out ELSE phrase if present
3354
3355 elsif Token = Tok_Else then
3356
3357 -- Scan out ELSE expression
3358
3359 Scan; -- Past ELSE
3360 Append_To (Exprs, P_Expression);
3361
3362 -- Skip redundant ELSE parts
3363
3364 while Token = Tok_Else loop
3365 Error_Msg_SC ("only one ELSE part is allowed");
3366 Scan; -- past ELSE
3367 Discard_Junk_Node (P_Expression);
3368 end loop;
3369
3370 -- Two expression case (implied True, filled in during semantics)
3371
3372 else
3373 null;
3374 end if;
3375
3376 -- If we have an END IF, diagnose as not needed
3377
3378 if Token = Tok_End then
3379 Error_Msg_SC ("`END IF` not allowed at end of if expression");
3380 Scan; -- past END
3381
3382 if Token = Tok_If then
3383 Scan; -- past IF;
3384 end if;
3385 end if;
3386
3387 -- Return the If_Expression node
3388
3389 return Make_If_Expression (Loc, Expressions => Exprs);
3390 end P_If_Expression_Internal;
3391
3392 -- Local variables
3393
3394 Loc : constant Source_Ptr := Token_Ptr;
3395 If_Expr : Node_Id;
3396
3397 -- Start of processing for P_If_Expression
3398
3399 begin
3400 Error_Msg_Ada_2012_Feature ("|if expression", Token_Ptr);
3401 Scan; -- past IF
3402 Inside_If_Expression := Inside_If_Expression + 1;
3403 If_Expr := P_If_Expression_Internal (Loc, P_Expression);
3404 Inside_If_Expression := Inside_If_Expression - 1;
3405 return If_Expr;
3406 end P_If_Expression;
3407
3408 -----------------------
3409 -- P_Membership_Test --
3410 -----------------------
3411
3412 -- MEMBERSHIP_CHOICE_LIST ::= MEMBERHIP_CHOICE {'|' MEMBERSHIP_CHOICE}
3413 -- MEMBERSHIP_CHOICE ::= CHOICE_EXPRESSION | range | subtype_mark
3414
3415 procedure P_Membership_Test (N : Node_Id) is
3416 Alt : constant Node_Id :=
3417 P_Range_Or_Subtype_Mark
3418 (Allow_Simple_Expression => (Ada_Version >= Ada_2012));
3419
3420 begin
3421 -- Set case
3422
3423 if Token = Tok_Vertical_Bar then
3424 Error_Msg_Ada_2012_Feature ("set notation", Token_Ptr);
3425 Set_Alternatives (N, New_List (Alt));
3426 Set_Right_Opnd (N, Empty);
3427
3428 -- Loop to accumulate alternatives
3429
3430 while Token = Tok_Vertical_Bar loop
3431 Scan; -- past vertical bar
3432 Append_To
3433 (Alternatives (N),
3434 P_Range_Or_Subtype_Mark (Allow_Simple_Expression => True));
3435 end loop;
3436
3437 -- Not set case
3438
3439 else
3440 Set_Right_Opnd (N, Alt);
3441 Set_Alternatives (N, No_List);
3442 end if;
3443 end P_Membership_Test;
3444
3445 ------------------------------------------
3446 -- P_Unparen_Cond_Case_Quant_Expression --
3447 ------------------------------------------
3448
3449 function P_Unparen_Cond_Case_Quant_Expression return Node_Id is
3450 Lparen : constant Boolean := Prev_Token = Tok_Left_Paren;
3451
3452 Result : Node_Id;
3453 Scan_State : Saved_Scan_State;
3454
3455 begin
3456 -- Case expression
3457
3458 if Token = Tok_Case then
3459 Result := P_Case_Expression;
3460
3461 if not (Lparen and then Token = Tok_Right_Paren) then
3462 Error_Msg_N ("case expression must be parenthesized!", Result);
3463 end if;
3464
3465 -- If expression
3466
3467 elsif Token = Tok_If then
3468 Result := P_If_Expression;
3469
3470 if not (Lparen and then Token = Tok_Right_Paren) then
3471 Error_Msg_N ("if expression must be parenthesized!", Result);
3472 end if;
3473
3474 -- Quantified expression or iterated component association
3475
3476 elsif Token = Tok_For then
3477
3478 Save_Scan_State (Scan_State);
3479 Scan; -- past FOR
3480
3481 if Token = Tok_All or else Token = Tok_Some then
3482 Restore_Scan_State (Scan_State);
3483 Result := P_Quantified_Expression;
3484
3485 if not (Lparen and then Token = Tok_Right_Paren) then
3486 Error_Msg_N
3487 ("quantified expression must be parenthesized!", Result);
3488 end if;
3489
3490 else
3491 -- If no quantifier keyword, this is an iterated component in
3492 -- an aggregate.
3493
3494 Restore_Scan_State (Scan_State);
3495 Result := P_Iterated_Component_Association;
3496 end if;
3497
3498 -- No other possibility should exist (caller was supposed to check)
3499
3500 else
3501 raise Program_Error;
3502 end if;
3503
3504 -- Return expression (possibly after having given message)
3505
3506 return Result;
3507 end P_Unparen_Cond_Case_Quant_Expression;
3508
3509 end Ch4;