OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.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-2007, 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    ---------------
36    -- Local map --
37    ---------------
38
39    Is_Parameterless_Attribute : constant Attribute_Class_Array :=
40      (Attribute_Body_Version => True,
41       Attribute_External_Tag => True,
42       Attribute_Img          => True,
43       Attribute_Version      => True,
44       Attribute_Base         => True,
45       Attribute_Class        => True,
46       Attribute_Stub_Type    => True,
47       others                 => False);
48    --  This map contains True for parameterless attributes that return a
49    --  string or a type. For those attributes, a left parenthesis after
50    --  the attribute should not be analyzed as the beginning of a parameters
51    --  list because it may denote a slice operation (X'Img (1 .. 2)) or
52    --  a type conversion (X'Class (Y)).
53
54    -----------------------
55    -- Local Subprograms --
56    -----------------------
57
58    function P_Aggregate_Or_Paren_Expr                 return Node_Id;
59    function P_Allocator                               return Node_Id;
60    function P_Record_Or_Array_Component_Association   return Node_Id;
61    function P_Factor                                  return Node_Id;
62    function P_Primary                                 return Node_Id;
63    function P_Relation                                return Node_Id;
64    function P_Term                                    return Node_Id;
65
66    function P_Binary_Adding_Operator                  return Node_Kind;
67    function P_Logical_Operator                        return Node_Kind;
68    function P_Multiplying_Operator                    return Node_Kind;
69    function P_Relational_Operator                     return Node_Kind;
70    function P_Unary_Adding_Operator                   return Node_Kind;
71
72    procedure Bad_Range_Attribute (Loc : Source_Ptr);
73    --  Called to place complaint about bad range attribute at the given
74    --  source location. Terminates by raising Error_Resync.
75
76    function P_Range_Attribute_Reference
77      (Prefix_Node : Node_Id)
78       return        Node_Id;
79    --  Scan a range attribute reference. The caller has scanned out the
80    --  prefix. The current token is known to be an apostrophe and the
81    --  following token is known to be RANGE.
82
83    procedure Set_Op_Name (Node : Node_Id);
84    --  Procedure to set name field (Chars) in operator node
85
86    -------------------------
87    -- Bad_Range_Attribute --
88    -------------------------
89
90    procedure Bad_Range_Attribute (Loc : Source_Ptr) is
91    begin
92       Error_Msg ("range attribute cannot be used in expression!", Loc);
93       Resync_Expression;
94    end Bad_Range_Attribute;
95
96    ------------------
97    -- Set_Op_Name --
98    ------------------
99
100    procedure Set_Op_Name (Node : Node_Id) is
101       type Name_Of_Type is array (N_Op) of Name_Id;
102       Name_Of : constant Name_Of_Type := Name_Of_Type'(
103          N_Op_And                    => Name_Op_And,
104          N_Op_Or                     => Name_Op_Or,
105          N_Op_Xor                    => Name_Op_Xor,
106          N_Op_Eq                     => Name_Op_Eq,
107          N_Op_Ne                     => Name_Op_Ne,
108          N_Op_Lt                     => Name_Op_Lt,
109          N_Op_Le                     => Name_Op_Le,
110          N_Op_Gt                     => Name_Op_Gt,
111          N_Op_Ge                     => Name_Op_Ge,
112          N_Op_Add                    => Name_Op_Add,
113          N_Op_Subtract               => Name_Op_Subtract,
114          N_Op_Concat                 => Name_Op_Concat,
115          N_Op_Multiply               => Name_Op_Multiply,
116          N_Op_Divide                 => Name_Op_Divide,
117          N_Op_Mod                    => Name_Op_Mod,
118          N_Op_Rem                    => Name_Op_Rem,
119          N_Op_Expon                  => Name_Op_Expon,
120          N_Op_Plus                   => Name_Op_Add,
121          N_Op_Minus                  => Name_Op_Subtract,
122          N_Op_Abs                    => Name_Op_Abs,
123          N_Op_Not                    => Name_Op_Not,
124
125          --  We don't really need these shift operators, since they never
126          --  appear as operators in the source, but the path of least
127          --  resistance is to put them in (the aggregate must be complete)
128
129          N_Op_Rotate_Left            => Name_Rotate_Left,
130          N_Op_Rotate_Right           => Name_Rotate_Right,
131          N_Op_Shift_Left             => Name_Shift_Left,
132          N_Op_Shift_Right            => Name_Shift_Right,
133          N_Op_Shift_Right_Arithmetic => Name_Shift_Right_Arithmetic);
134
135    begin
136       if Nkind (Node) in N_Op then
137          Set_Chars (Node, Name_Of (Nkind (Node)));
138       end if;
139    end Set_Op_Name;
140
141    --------------------------
142    -- 4.1  Name (also 6.4) --
143    --------------------------
144
145    --  NAME ::=
146    --    DIRECT_NAME        | EXPLICIT_DEREFERENCE
147    --  | INDEXED_COMPONENT  | SLICE
148    --  | SELECTED_COMPONENT | ATTRIBUTE
149    --  | TYPE_CONVERSION    | FUNCTION_CALL
150    --  | CHARACTER_LITERAL
151
152    --  DIRECT_NAME ::= IDENTIFIER | OPERATOR_SYMBOL
153
154    --  PREFIX ::= NAME | IMPLICIT_DEREFERENCE
155
156    --  EXPLICIT_DEREFERENCE ::= NAME . all
157
158    --  IMPLICIT_DEREFERENCE ::= NAME
159
160    --  INDEXED_COMPONENT ::= PREFIX (EXPRESSION {, EXPRESSION})
161
162    --  SLICE ::= PREFIX (DISCRETE_RANGE)
163
164    --  SELECTED_COMPONENT ::= PREFIX . SELECTOR_NAME
165
166    --  SELECTOR_NAME ::= IDENTIFIER | CHARACTER_LITERAL | OPERATOR_SYMBOL
167
168    --  ATTRIBUTE_REFERENCE ::= PREFIX ' ATTRIBUTE_DESIGNATOR
169
170    --  ATTRIBUTE_DESIGNATOR ::=
171    --    IDENTIFIER [(static_EXPRESSION)]
172    --  | access | delta | digits
173
174    --  FUNCTION_CALL ::=
175    --    function_NAME
176    --  | function_PREFIX ACTUAL_PARAMETER_PART
177
178    --  ACTUAL_PARAMETER_PART ::=
179    --    (PARAMETER_ASSOCIATION {,PARAMETER_ASSOCIATION})
180
181    --  PARAMETER_ASSOCIATION ::=
182    --    [formal_parameter_SELECTOR_NAME =>] EXPLICIT_ACTUAL_PARAMETER
183
184    --  EXPLICIT_ACTUAL_PARAMETER ::= EXPRESSION | variable_NAME
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
235       Name_Node := Token_Node;
236
237       loop
238          Scan; -- past designator
239          exit when Token /= Tok_Dot;
240          Save_Scan_State (Scan_State); -- at dot
241          Scan; -- past dot
242
243          --  If we do not have another designator after the dot, then join
244          --  the normal circuit to handle a dot extension (may be .all or
245          --  character literal case). Otherwise loop back to scan the next
246          --  designator.
247
248          if Token not in Token_Class_Desig then
249             goto Scan_Name_Extension_Dot;
250          else
251             Prefix_Node := Name_Node;
252             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
253             Set_Prefix (Name_Node, Prefix_Node);
254             Set_Selector_Name (Name_Node, Token_Node);
255          end if;
256       end loop;
257
258       --  We have now scanned out a qualified designator. If the last token is
259       --  an operator symbol, then we certainly do not have the Snam case, so
260       --  we can just use the normal name extension check circuit
261
262       if Prev_Token = Tok_Operator_Symbol then
263          goto Scan_Name_Extension;
264       end if;
265
266       --  We have scanned out a qualified simple name, check for name extension
267       --  Note that we know there is no dot here at this stage, so the only
268       --  possible cases of name extension are apostrophe and left paren.
269
270       if Token = Tok_Apostrophe then
271          Save_Scan_State (Scan_State); -- at apostrophe
272          Scan; -- past apostrophe
273
274          --  If left paren, then this might be a qualified expression, but we
275          --  are only in the business of scanning out names, so return with
276          --  Token backed up to point to the apostrophe. The treatment for
277          --  the range attribute is similar (we do not consider x'range to
278          --  be a name in this grammar).
279
280          if Token = Tok_Left_Paren or else Token = Tok_Range then
281             Restore_Scan_State (Scan_State); -- to apostrophe
282             Expr_Form := EF_Simple_Name;
283             return Name_Node;
284
285          --  Otherwise we have the case of a name extended by an attribute
286
287          else
288             goto Scan_Name_Extension_Apostrophe;
289          end if;
290
291       --  Check case of qualified simple name extended by a left parenthesis
292
293       elsif Token = Tok_Left_Paren then
294          Scan; -- past left paren
295          goto Scan_Name_Extension_Left_Paren;
296
297       --  Otherwise the qualified simple name is not extended, so return
298
299       else
300          Expr_Form := EF_Simple_Name;
301          return Name_Node;
302       end if;
303
304       --  Loop scanning past name extensions. A label is used for control
305       --  transfer for this loop for ease of interfacing with the finite state
306       --  machine in the parenthesis scanning circuit, and also to allow for
307       --  passing in control to the appropriate point from the above code.
308
309       <<Scan_Name_Extension>>
310
311          --  Character literal used as name cannot be extended. Also this
312          --  cannot be a call, since the name for a call must be a designator.
313          --  Return in these cases, or if there is no name extension
314
315          if Token not in Token_Class_Namext
316            or else Prev_Token = Tok_Char_Literal
317          then
318             Expr_Form := EF_Name;
319             return Name_Node;
320          end if;
321
322       --  Merge here when we know there is a name extension
323
324       <<Scan_Name_Extension_OK>>
325
326          if Token = Tok_Left_Paren then
327             Scan; -- past left paren
328             goto Scan_Name_Extension_Left_Paren;
329
330          elsif Token = Tok_Apostrophe then
331             Save_Scan_State (Scan_State); -- at apostrophe
332             Scan; -- past apostrophe
333             goto Scan_Name_Extension_Apostrophe;
334
335          else -- Token = Tok_Dot
336             Save_Scan_State (Scan_State); -- at dot
337             Scan; -- past dot
338             goto Scan_Name_Extension_Dot;
339          end if;
340
341       --  Case of name extended by dot (selection), dot is already skipped
342       --  and the scan state at the point of the dot is saved in Scan_State.
343
344       <<Scan_Name_Extension_Dot>>
345
346          --  Explicit dereference case
347
348          if Token = Tok_All then
349             Prefix_Node := Name_Node;
350             Name_Node := New_Node (N_Explicit_Dereference, Token_Ptr);
351             Set_Prefix (Name_Node, Prefix_Node);
352             Scan; -- past ALL
353             goto Scan_Name_Extension;
354
355          --  Selected component case
356
357          elsif Token in Token_Class_Name then
358             Prefix_Node := Name_Node;
359             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
360             Set_Prefix (Name_Node, Prefix_Node);
361             Set_Selector_Name (Name_Node, Token_Node);
362             Scan; -- past selector
363             goto Scan_Name_Extension;
364
365          --  Reserved identifier as selector
366
367          elsif Is_Reserved_Identifier then
368             Scan_Reserved_Identifier (Force_Msg => False);
369             Prefix_Node := Name_Node;
370             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
371             Set_Prefix (Name_Node, Prefix_Node);
372             Set_Selector_Name (Name_Node, Token_Node);
373             Scan; -- past identifier used as selector
374             goto Scan_Name_Extension;
375
376          --  If dot is at end of line and followed by nothing legal,
377          --  then assume end of name and quit (dot will be taken as
378          --  an erroneous form of some other punctuation by our caller).
379
380          elsif Token_Is_At_Start_Of_Line then
381             Restore_Scan_State (Scan_State);
382             return Name_Node;
383
384          --  Here if nothing legal after the dot
385
386          else
387             Error_Msg_AP ("selector expected");
388             raise Error_Resync;
389          end if;
390
391       --  Here for an apostrophe as name extension. The scan position at the
392       --  apostrophe has already been saved, and the apostrophe scanned out.
393
394       <<Scan_Name_Extension_Apostrophe>>
395
396          Scan_Apostrophe : declare
397             function Apostrophe_Should_Be_Semicolon return Boolean;
398             --  Checks for case where apostrophe should probably be
399             --  a semicolon, and if so, gives appropriate message,
400             --  resets the scan pointer to the apostrophe, changes
401             --  the current token to Tok_Semicolon, and returns True.
402             --  Otherwise returns False.
403
404             function Apostrophe_Should_Be_Semicolon return Boolean is
405             begin
406                if Token_Is_At_Start_Of_Line then
407                   Restore_Scan_State (Scan_State); -- to apostrophe
408                   Error_Msg_SC ("""''"" should be "";""");
409                   Token := Tok_Semicolon;
410                   return True;
411                else
412                   return False;
413                end if;
414             end Apostrophe_Should_Be_Semicolon;
415
416          --  Start of processing for Scan_Apostrophe
417
418          begin
419             --  If range attribute after apostrophe, then return with Token
420             --  pointing to the apostrophe. Note that in this case the prefix
421             --  need not be a simple name (cases like A.all'range). Similarly
422             --  if there is a left paren after the apostrophe, then we also
423             --  return with Token pointing to the apostrophe (this is the
424             --  qualified expression case).
425
426             if Token = Tok_Range or else Token = Tok_Left_Paren then
427                Restore_Scan_State (Scan_State); -- to apostrophe
428                Expr_Form := EF_Name;
429                return Name_Node;
430
431             --  Here for cases where attribute designator is an identifier
432
433             elsif Token = Tok_Identifier then
434                Attr_Name := Token_Name;
435
436                if not Is_Attribute_Name (Attr_Name) then
437                   if Apostrophe_Should_Be_Semicolon then
438                      Expr_Form := EF_Name;
439                      return Name_Node;
440
441                   --  Here for a bad attribute name
442
443                   else
444                      Signal_Bad_Attribute;
445                      Scan; -- past bad identifier
446
447                      if Token = Tok_Left_Paren then
448                         Scan; -- past left paren
449
450                         loop
451                            Discard_Junk_Node (P_Expression);
452                            exit when not Comma_Present;
453                         end loop;
454
455                         T_Right_Paren;
456                      end if;
457
458                      return Error;
459                   end if;
460                end if;
461
462                if Style_Check then
463                   Style.Check_Attribute_Name (False);
464                end if;
465
466                Delete_Node (Token_Node);
467
468             --  Here for case of attribute designator is not an identifier
469
470             else
471                if Token = Tok_Delta then
472                   Attr_Name := Name_Delta;
473
474                elsif Token = Tok_Digits then
475                   Attr_Name := Name_Digits;
476
477                elsif Token = Tok_Access then
478                   Attr_Name := Name_Access;
479
480                elsif Token = Tok_Mod and then Ada_Version = Ada_05 then
481                   Attr_Name := Name_Mod;
482
483                elsif Apostrophe_Should_Be_Semicolon then
484                   Expr_Form := EF_Name;
485                   return Name_Node;
486
487                else
488                   Error_Msg_AP ("attribute designator expected");
489                   raise Error_Resync;
490                end if;
491
492                if Style_Check then
493                   Style.Check_Attribute_Name (True);
494                end if;
495             end if;
496
497             --  We come here with an OK attribute scanned, and the
498             --  corresponding Attribute identifier node stored in Ident_Node.
499
500             Prefix_Node := Name_Node;
501             Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
502             Scan; -- past attribute designator
503             Set_Prefix (Name_Node, Prefix_Node);
504             Set_Attribute_Name (Name_Node, Attr_Name);
505
506             --  Scan attribute arguments/designator
507
508             if Token = Tok_Left_Paren
509                  and then
510                not Is_Parameterless_Attribute (Get_Attribute_Id (Attr_Name))
511             then
512                Set_Expressions (Name_Node, New_List);
513                Scan; -- past left paren
514
515                loop
516                   declare
517                      Expr : constant Node_Id := P_Expression;
518
519                   begin
520                      if Token = Tok_Arrow then
521                         Error_Msg_SC
522                           ("named parameters not permitted for attributes");
523                         Scan; -- past junk arrow
524
525                      else
526                         Append (Expr, Expressions (Name_Node));
527                         exit when not Comma_Present;
528                      end if;
529                   end;
530                end loop;
531
532                T_Right_Paren;
533             end if;
534
535             goto Scan_Name_Extension;
536          end Scan_Apostrophe;
537
538       --  Here for left parenthesis extending name (left paren skipped)
539
540       <<Scan_Name_Extension_Left_Paren>>
541
542          --  We now have to scan through a list of items, terminated by a
543          --  right parenthesis. The scan is handled by a finite state
544          --  machine. The possibilities are:
545
546          --   (discrete_range)
547
548          --      This is a slice. This case is handled in LP_State_Init
549
550          --   (expression, expression, ..)
551
552          --      This is interpreted as an indexed component, i.e. as a
553          --      case of a name which can be extended in the normal manner.
554          --      This case is handled by LP_State_Name or LP_State_Expr.
555
556          --   (..., identifier => expression , ...)
557
558          --      If there is at least one occurrence of identifier => (but
559          --      none of the other cases apply), then we have a call.
560
561          --  Test for Id => case
562
563          if Token = Tok_Identifier then
564             Save_Scan_State (Scan_State); -- at Id
565             Scan; -- past Id
566
567             --  Test for => (allow := as an error substitute)
568
569             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
570                Restore_Scan_State (Scan_State); -- to Id
571                Arg_List := New_List;
572                goto LP_State_Call;
573
574             else
575                Restore_Scan_State (Scan_State); -- to Id
576             end if;
577          end if;
578
579          --  Here we have an expression after all
580
581          Expr_Node := P_Expression_Or_Range_Attribute;
582
583          --  Check cases of discrete range for a slice
584
585          --  First possibility: Range_Attribute_Reference
586
587          if Expr_Form = EF_Range_Attr then
588             Range_Node := Expr_Node;
589
590          --  Second possibility: Simple_expression .. Simple_expression
591
592          elsif Token = Tok_Dot_Dot then
593             Check_Simple_Expression (Expr_Node);
594             Range_Node := New_Node (N_Range, Token_Ptr);
595             Set_Low_Bound (Range_Node, Expr_Node);
596             Scan; -- past ..
597             Expr_Node := P_Expression;
598             Check_Simple_Expression (Expr_Node);
599             Set_High_Bound (Range_Node, Expr_Node);
600
601          --  Third possibility: Type_name range Range
602
603          elsif Token = Tok_Range then
604             if Expr_Form /= EF_Simple_Name then
605                Error_Msg_SC ("subtype mark must precede RANGE");
606                raise Error_Resync;
607             end if;
608
609             Range_Node := P_Subtype_Indication (Expr_Node);
610
611          --  Otherwise we just have an expression. It is true that we might
612          --  have a subtype mark without a range constraint but this case
613          --  is syntactically indistinguishable from the expression case.
614
615          else
616             Arg_List := New_List;
617             goto LP_State_Expr;
618          end if;
619
620          --  Fall through here with unmistakable Discrete range scanned,
621          --  which means that we definitely have the case of a slice. The
622          --  Discrete range is in Range_Node.
623
624          if Token = Tok_Comma then
625             Error_Msg_SC ("slice cannot have more than one dimension");
626             raise Error_Resync;
627
628          elsif Token /= Tok_Right_Paren then
629             T_Right_Paren;
630             raise Error_Resync;
631
632          else
633             Scan; -- past right paren
634             Prefix_Node := Name_Node;
635             Name_Node := New_Node (N_Slice, Sloc (Prefix_Node));
636             Set_Prefix (Name_Node, Prefix_Node);
637             Set_Discrete_Range (Name_Node, Range_Node);
638
639             --  An operator node is legal as a prefix to other names,
640             --  but not for a slice.
641
642             if Nkind (Prefix_Node) = N_Operator_Symbol then
643                Error_Msg_N ("illegal prefix for slice", Prefix_Node);
644             end if;
645
646             --  If we have a name extension, go scan it
647
648             if Token in Token_Class_Namext then
649                goto Scan_Name_Extension_OK;
650
651             --  Otherwise return (a slice is a name, but is not a call)
652
653             else
654                Expr_Form := EF_Name;
655                return Name_Node;
656             end if;
657          end if;
658
659       --  In LP_State_Expr, we have scanned one or more expressions, and
660       --  so we have a call or an indexed component which is a name. On
661       --  entry we have the expression just scanned in Expr_Node and
662       --  Arg_List contains the list of expressions encountered so far
663
664       <<LP_State_Expr>>
665          Append (Expr_Node, Arg_List);
666
667          if Token = Tok_Arrow then
668             Error_Msg
669               ("expect identifier in parameter association",
670                 Sloc (Expr_Node));
671             Scan;  --   past arrow.
672
673          elsif not Comma_Present then
674             T_Right_Paren;
675             Prefix_Node := Name_Node;
676             Name_Node := New_Node (N_Indexed_Component, Sloc (Prefix_Node));
677             Set_Prefix (Name_Node, Prefix_Node);
678             Set_Expressions (Name_Node, Arg_List);
679             goto Scan_Name_Extension;
680          end if;
681
682          --  Comma present (and scanned out), test for identifier => case
683          --  Test for identifier => case
684
685          if Token = Tok_Identifier then
686             Save_Scan_State (Scan_State); -- at Id
687             Scan; -- past Id
688
689             --  Test for => (allow := as error substitute)
690
691             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
692                Restore_Scan_State (Scan_State); -- to Id
693                goto LP_State_Call;
694
695             --  Otherwise it's just an expression after all, so backup
696
697             else
698                Restore_Scan_State (Scan_State); -- to Id
699             end if;
700          end if;
701
702          --  Here we have an expression after all, so stay in this state
703
704          Expr_Node := P_Expression;
705          goto LP_State_Expr;
706
707       --  LP_State_Call corresponds to the situation in which at least
708       --  one instance of Id => Expression has been encountered, so we
709       --  know that we do not have a name, but rather a call. We enter
710       --  it with the scan pointer pointing to the next argument to scan,
711       --  and Arg_List containing the list of arguments scanned so far.
712
713       <<LP_State_Call>>
714
715          --  Test for case of Id => Expression (named parameter)
716
717          if Token = Tok_Identifier then
718             Save_Scan_State (Scan_State); -- at Id
719             Ident_Node := Token_Node;
720             Scan; -- past Id
721
722             --  Deal with => (allow := as erroneous substitute)
723
724             if Token = Tok_Arrow or else Token = Tok_Colon_Equal then
725                Arg_Node :=
726                  New_Node (N_Parameter_Association, Prev_Token_Ptr);
727                Set_Selector_Name (Arg_Node, Ident_Node);
728                T_Arrow;
729                Set_Explicit_Actual_Parameter (Arg_Node, P_Expression);
730                Append (Arg_Node, Arg_List);
731
732                --  If a comma follows, go back and scan next entry
733
734                if Comma_Present then
735                   goto LP_State_Call;
736
737                --  Otherwise we have the end of a call
738
739                else
740                   Prefix_Node := Name_Node;
741                   Name_Node :=
742                     New_Node (N_Function_Call, Sloc (Prefix_Node));
743                   Set_Name (Name_Node, Prefix_Node);
744                   Set_Parameter_Associations (Name_Node, Arg_List);
745                   T_Right_Paren;
746
747                   if Token in Token_Class_Namext then
748                      goto Scan_Name_Extension_OK;
749
750                   --  This is a case of a call which cannot be a name
751
752                   else
753                      Expr_Form := EF_Name;
754                      return Name_Node;
755                   end if;
756                end if;
757
758             --  Not named parameter: Id started an expression after all
759
760             else
761                Restore_Scan_State (Scan_State); -- to Id
762             end if;
763          end if;
764
765          --  Here if entry did not start with Id => which means that it
766          --  is a positional parameter, which is not allowed, since we
767          --  have seen at least one named parameter already.
768
769          Error_Msg_SC
770             ("positional parameter association " &
771               "not allowed after named one");
772
773          Expr_Node := P_Expression;
774
775          --  Leaving the '>' in an association is not unusual, so suggest
776          --  a possible fix.
777
778          if Nkind (Expr_Node) = N_Op_Eq then
779             Error_Msg_N ("\maybe `='>` was intended", Expr_Node);
780          end if;
781
782          --  We go back to scanning out expressions, so that we do not get
783          --  multiple error messages when several positional parameters
784          --  follow a named parameter.
785
786          goto LP_State_Expr;
787
788          --  End of treatment for name extensions starting with left paren
789
790       --  End of loop through name extensions
791
792    end P_Name;
793
794    --  This function parses a restricted form of Names which are either
795    --  designators, or designators preceded by a sequence of prefixes
796    --  that are direct names.
797
798    --  Error recovery: cannot raise Error_Resync
799
800    function P_Function_Name return Node_Id is
801       Designator_Node : Node_Id;
802       Prefix_Node     : Node_Id;
803       Selector_Node   : Node_Id;
804       Dot_Sloc        : Source_Ptr := No_Location;
805
806    begin
807       --  Prefix_Node is set to the gathered prefix so far, Empty means that
808       --  no prefix has been scanned. This allows us to build up the result
809       --  in the required right recursive manner.
810
811       Prefix_Node := Empty;
812
813       --  Loop through prefixes
814
815       loop
816          Designator_Node := Token_Node;
817
818          if Token not in Token_Class_Desig then
819             return P_Identifier; -- let P_Identifier issue the error message
820
821          else -- Token in Token_Class_Desig
822             Scan; -- past designator
823             exit when Token /= Tok_Dot;
824          end if;
825
826          --  Here at a dot, with token just before it in Designator_Node
827
828          if No (Prefix_Node) then
829             Prefix_Node := Designator_Node;
830          else
831             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
832             Set_Prefix (Selector_Node, Prefix_Node);
833             Set_Selector_Name (Selector_Node, Designator_Node);
834             Prefix_Node := Selector_Node;
835          end if;
836
837          Dot_Sloc := Token_Ptr;
838          Scan; -- past dot
839       end loop;
840
841       --  Fall out of the loop having just scanned a designator
842
843       if No (Prefix_Node) then
844          return Designator_Node;
845       else
846          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
847          Set_Prefix (Selector_Node, Prefix_Node);
848          Set_Selector_Name (Selector_Node, Designator_Node);
849          return Selector_Node;
850       end if;
851
852    exception
853       when Error_Resync =>
854          return Error;
855
856    end P_Function_Name;
857
858    --  This function parses a restricted form of Names which are either
859    --  identifiers, or identifiers preceded by a sequence of prefixes
860    --  that are direct names.
861
862    --  Error recovery: cannot raise Error_Resync
863
864    function P_Qualified_Simple_Name return Node_Id is
865       Designator_Node : Node_Id;
866       Prefix_Node     : Node_Id;
867       Selector_Node   : Node_Id;
868       Dot_Sloc        : Source_Ptr := No_Location;
869
870    begin
871       --  Prefix node is set to the gathered prefix so far, Empty means that
872       --  no prefix has been scanned. This allows us to build up the result
873       --  in the required right recursive manner.
874
875       Prefix_Node := Empty;
876
877       --  Loop through prefixes
878
879       loop
880          Designator_Node := Token_Node;
881
882          if Token = Tok_Identifier then
883             Scan; -- past identifier
884             exit when Token /= Tok_Dot;
885
886          elsif Token not in Token_Class_Desig then
887             return P_Identifier; -- let P_Identifier issue the error message
888
889          else
890             Scan; -- past designator
891
892             if Token /= Tok_Dot then
893                Error_Msg_SP ("identifier expected");
894                return Error;
895             end if;
896          end if;
897
898          --  Here at a dot, with token just before it in Designator_Node
899
900          if No (Prefix_Node) then
901             Prefix_Node := Designator_Node;
902          else
903             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
904             Set_Prefix (Selector_Node, Prefix_Node);
905             Set_Selector_Name (Selector_Node, Designator_Node);
906             Prefix_Node := Selector_Node;
907          end if;
908
909          Dot_Sloc := Token_Ptr;
910          Scan; -- past dot
911       end loop;
912
913       --  Fall out of the loop having just scanned an identifier
914
915       if No (Prefix_Node) then
916          return Designator_Node;
917       else
918          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
919          Set_Prefix (Selector_Node, Prefix_Node);
920          Set_Selector_Name (Selector_Node, Designator_Node);
921          return Selector_Node;
922       end if;
923
924    exception
925       when Error_Resync =>
926          return Error;
927
928    end P_Qualified_Simple_Name;
929
930    --  This procedure differs from P_Qualified_Simple_Name only in that it
931    --  raises Error_Resync if any error is encountered. It only returns after
932    --  scanning a valid qualified simple name.
933
934    --  Error recovery: can raise Error_Resync
935
936    function P_Qualified_Simple_Name_Resync 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 := Empty;
944
945       --  Loop through prefixes
946
947       loop
948          Designator_Node := Token_Node;
949
950          if Token = Tok_Identifier then
951             Scan; -- past identifier
952             exit when Token /= Tok_Dot;
953
954          elsif Token not in Token_Class_Desig then
955             Discard_Junk_Node (P_Identifier); -- to issue the error message
956             raise Error_Resync;
957
958          else
959             Scan; -- past designator
960
961             if Token /= Tok_Dot then
962                Error_Msg_SP ("identifier expected");
963                raise Error_Resync;
964             end if;
965          end if;
966
967          --  Here at a dot, with token just before it in Designator_Node
968
969          if No (Prefix_Node) then
970             Prefix_Node := Designator_Node;
971          else
972             Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
973             Set_Prefix (Selector_Node, Prefix_Node);
974             Set_Selector_Name (Selector_Node, Designator_Node);
975             Prefix_Node := Selector_Node;
976          end if;
977
978          Dot_Sloc := Token_Ptr;
979          Scan; -- past period
980       end loop;
981
982       --  Fall out of the loop having just scanned an identifier
983
984       if No (Prefix_Node) then
985          return Designator_Node;
986       else
987          Selector_Node := New_Node (N_Selected_Component, Dot_Sloc);
988          Set_Prefix (Selector_Node, Prefix_Node);
989          Set_Selector_Name (Selector_Node, Designator_Node);
990          return Selector_Node;
991       end if;
992
993    end P_Qualified_Simple_Name_Resync;
994
995    ----------------------
996    -- 4.1  Direct_Name --
997    ----------------------
998
999    --  Parsed by P_Name and other functions in section 4.1
1000
1001    -----------------
1002    -- 4.1  Prefix --
1003    -----------------
1004
1005    --  Parsed by P_Name (4.1)
1006
1007    -------------------------------
1008    -- 4.1  Explicit Dereference --
1009    -------------------------------
1010
1011    --  Parsed by P_Name (4.1)
1012
1013    -------------------------------
1014    -- 4.1  Implicit_Dereference --
1015    -------------------------------
1016
1017    --  Parsed by P_Name (4.1)
1018
1019    ----------------------------
1020    -- 4.1  Indexed Component --
1021    ----------------------------
1022
1023    --  Parsed by P_Name (4.1)
1024
1025    ----------------
1026    -- 4.1  Slice --
1027    ----------------
1028
1029    --  Parsed by P_Name (4.1)
1030
1031    -----------------------------
1032    -- 4.1  Selected_Component --
1033    -----------------------------
1034
1035    --  Parsed by P_Name (4.1)
1036
1037    ------------------------
1038    -- 4.1  Selector Name --
1039    ------------------------
1040
1041    --  Parsed by P_Name (4.1)
1042
1043    ------------------------------
1044    -- 4.1  Attribute Reference --
1045    ------------------------------
1046
1047    --  Parsed by P_Name (4.1)
1048
1049    -------------------------------
1050    -- 4.1  Attribute Designator --
1051    -------------------------------
1052
1053    --  Parsed by P_Name (4.1)
1054
1055    --------------------------------------
1056    -- 4.1.4  Range Attribute Reference --
1057    --------------------------------------
1058
1059    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1060
1061    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1062
1063    --  In the grammar, a RANGE attribute is simply a name, but its use is
1064    --  highly restricted, so in the parser, we do not regard it as a name.
1065    --  Instead, P_Name returns without scanning the 'RANGE part of the
1066    --  attribute, and the caller uses the following function to construct
1067    --  a range attribute in places where it is appropriate.
1068
1069    --  Note that RANGE here is treated essentially as an identifier,
1070    --  rather than a reserved word.
1071
1072    --  The caller has parsed the prefix, i.e. a name, and Token points to
1073    --  the apostrophe. The token after the apostrophe is known to be RANGE
1074    --  at this point. The prefix node becomes the prefix of the attribute.
1075
1076    --  Error_Recovery: Cannot raise Error_Resync
1077
1078    function P_Range_Attribute_Reference
1079      (Prefix_Node : Node_Id)
1080       return        Node_Id
1081    is
1082       Attr_Node  : Node_Id;
1083
1084    begin
1085       Attr_Node := New_Node (N_Attribute_Reference, Token_Ptr);
1086       Set_Prefix (Attr_Node, Prefix_Node);
1087       Scan; -- past apostrophe
1088
1089       if Style_Check then
1090          Style.Check_Attribute_Name (True);
1091       end if;
1092
1093       Set_Attribute_Name (Attr_Node, Name_Range);
1094       Scan; -- past RANGE
1095
1096       if Token = Tok_Left_Paren then
1097          Scan; -- past left paren
1098          Set_Expressions (Attr_Node, New_List (P_Expression));
1099          T_Right_Paren;
1100       end if;
1101
1102       return Attr_Node;
1103    end P_Range_Attribute_Reference;
1104
1105    ---------------------------------------
1106    -- 4.1.4  Range Attribute Designator --
1107    ---------------------------------------
1108
1109    --  Parsed by P_Range_Attribute_Reference (4.4)
1110
1111    --------------------
1112    -- 4.3  Aggregate --
1113    --------------------
1114
1115    --  AGGREGATE ::= RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1116
1117    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3), except in the case where
1118    --  an aggregate is known to be required (code statement, extension
1119    --  aggregate), in which cases this routine performs the necessary check
1120    --  that we have an aggregate rather than a parenthesized expression
1121
1122    --  Error recovery: can raise Error_Resync
1123
1124    function P_Aggregate return Node_Id is
1125       Aggr_Sloc : constant Source_Ptr := Token_Ptr;
1126       Aggr_Node : constant Node_Id    := P_Aggregate_Or_Paren_Expr;
1127
1128    begin
1129       if Nkind (Aggr_Node) /= N_Aggregate
1130            and then
1131          Nkind (Aggr_Node) /= N_Extension_Aggregate
1132       then
1133          Error_Msg
1134            ("aggregate may not have single positional component", Aggr_Sloc);
1135          return Error;
1136       else
1137          return Aggr_Node;
1138       end if;
1139    end P_Aggregate;
1140
1141    -------------------------------------------------
1142    -- 4.3  Aggregate or Parenthesized Expresssion --
1143    -------------------------------------------------
1144
1145    --  This procedure parses out either an aggregate or a parenthesized
1146    --  expression (these two constructs are closely related, since a
1147    --  parenthesized expression looks like an aggregate with a single
1148    --  positional component).
1149
1150    --  AGGREGATE ::=
1151    --    RECORD_AGGREGATE | EXTENSION_AGGREGATE | ARRAY_AGGREGATE
1152
1153    --  RECORD_AGGREGATE ::= (RECORD_COMPONENT_ASSOCIATION_LIST)
1154
1155    --  RECORD_COMPONENT_ASSOCIATION_LIST ::=
1156    --     RECORD_COMPONENT_ASSOCIATION {, RECORD_COMPONENT_ASSOCIATION}
1157    --   | null record
1158
1159    --  RECORD_COMPONENT_ASSOCIATION ::=
1160    --    [COMPONENT_CHOICE_LIST =>] EXPRESSION
1161
1162    --  COMPONENT_CHOICE_LIST ::=
1163    --    component_SELECTOR_NAME {| component_SELECTOR_NAME}
1164    --  | others
1165
1166    --  EXTENSION_AGGREGATE ::=
1167    --    (ANCESTOR_PART with RECORD_COMPONENT_ASSOCIATION_LIST)
1168
1169    --  ANCESTOR_PART ::= EXPRESSION | SUBTYPE_MARK
1170
1171    --  ARRAY_AGGREGATE ::=
1172    --    POSITIONAL_ARRAY_AGGREGATE | NAMED_ARRAY_AGGREGATE
1173
1174    --  POSITIONAL_ARRAY_AGGREGATE ::=
1175    --    (EXPRESSION, EXPRESSION {, EXPRESSION})
1176    --  | (EXPRESSION {, EXPRESSION}, others => EXPRESSION)
1177    --  | (EXPRESSION {, EXPRESSION}, others => <>)
1178
1179    --  NAMED_ARRAY_AGGREGATE ::=
1180    --    (ARRAY_COMPONENT_ASSOCIATION {, ARRAY_COMPONENT_ASSOCIATION})
1181
1182    --  PRIMARY ::= (EXPRESSION);
1183
1184    --  Error recovery: can raise Error_Resync
1185
1186    --  Note: POSITIONAL_ARRAY_AGGREGATE rule has been extended to give support
1187    --        to Ada 2005 limited aggregates (AI-287)
1188
1189    function P_Aggregate_Or_Paren_Expr return Node_Id is
1190       Aggregate_Node : Node_Id;
1191       Expr_List      : List_Id;
1192       Assoc_List     : List_Id;
1193       Expr_Node      : Node_Id;
1194       Lparen_Sloc    : Source_Ptr;
1195       Scan_State     : Saved_Scan_State;
1196
1197    begin
1198       Lparen_Sloc := Token_Ptr;
1199       T_Left_Paren;
1200
1201       --  Note: the mechanism used here of rescanning the initial expression
1202       --  is distinctly unpleasant, but it saves a lot of fiddling in scanning
1203       --  out the discrete choice list.
1204
1205       --  Deal with expression and extension aggregate cases first
1206
1207       if Token /= Tok_Others then
1208          Save_Scan_State (Scan_State); -- at start of expression
1209
1210          --  Deal with (NULL RECORD) case
1211
1212          if Token = Tok_Null then
1213             Scan; -- past NULL
1214
1215             if Token = Tok_Record then
1216                Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1217                Set_Null_Record_Present (Aggregate_Node, True);
1218                Scan; -- past RECORD
1219                T_Right_Paren;
1220                return Aggregate_Node;
1221             else
1222                Restore_Scan_State (Scan_State); -- to NULL that must be expr
1223             end if;
1224          end if;
1225
1226          --  Ada 2005 (AI-287): The box notation is allowed only with named
1227          --  notation because positional notation might be error prone. For
1228          --  example, in "(X, <>, Y, <>)", there is no type associated with
1229          --  the boxes, so you might not be leaving out the components you
1230          --  thought you were leaving out.
1231
1232          if Ada_Version >= Ada_05 and then Token = Tok_Box then
1233             Error_Msg_SC ("(Ada 2005) box notation only allowed with "
1234                           & "named notation");
1235             Scan; --  past BOX
1236             Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1237             return Aggregate_Node;
1238          end if;
1239
1240          Expr_Node := P_Expression_Or_Range_Attribute;
1241
1242          --  Extension aggregate case
1243
1244          if Token = Tok_With then
1245
1246             if Nkind (Expr_Node) = N_Attribute_Reference
1247               and then Attribute_Name (Expr_Node) = Name_Range
1248             then
1249                Bad_Range_Attribute (Sloc (Expr_Node));
1250                return Error;
1251             end if;
1252
1253             if Ada_Version = Ada_83 then
1254                Error_Msg_SC ("(Ada 83) extension aggregate not allowed");
1255             end if;
1256
1257             Aggregate_Node := New_Node (N_Extension_Aggregate, Lparen_Sloc);
1258             Set_Ancestor_Part (Aggregate_Node, Expr_Node);
1259             Scan; -- past WITH
1260
1261             --  Deal with WITH NULL RECORD case
1262
1263             if Token = Tok_Null then
1264                Save_Scan_State (Scan_State); -- at NULL
1265                Scan; -- past NULL
1266
1267                if Token = Tok_Record then
1268                   Scan; -- past RECORD
1269                   Set_Null_Record_Present (Aggregate_Node, True);
1270                   T_Right_Paren;
1271                   return Aggregate_Node;
1272
1273                else
1274                   Restore_Scan_State (Scan_State); -- to NULL that must be expr
1275                end if;
1276             end if;
1277
1278             if Token /= Tok_Others then
1279                Save_Scan_State (Scan_State);
1280                Expr_Node := P_Expression;
1281             else
1282                Expr_Node := Empty;
1283             end if;
1284
1285          --  Expression case
1286
1287          elsif Token = Tok_Right_Paren or else Token in Token_Class_Eterm then
1288             if Nkind (Expr_Node) = N_Attribute_Reference
1289               and then Attribute_Name (Expr_Node) = Name_Range
1290             then
1291                Error_Msg
1292                  ("|parentheses not allowed for range attribute", Lparen_Sloc);
1293                Scan; -- past right paren
1294                return Expr_Node;
1295             end if;
1296
1297             --  Bump paren count of expression
1298
1299             if Expr_Node /= Error then
1300                Set_Paren_Count (Expr_Node, Paren_Count (Expr_Node) + 1);
1301             end if;
1302
1303             T_Right_Paren; -- past right paren (error message if none)
1304             return Expr_Node;
1305
1306          --  Normal aggregate case
1307
1308          else
1309             Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1310          end if;
1311
1312       --  Others case
1313
1314       else
1315          Aggregate_Node := New_Node (N_Aggregate, Lparen_Sloc);
1316          Expr_Node := Empty;
1317       end if;
1318
1319       --  Prepare to scan list of component associations
1320
1321       Expr_List  := No_List; -- don't set yet, maybe all named entries
1322       Assoc_List := No_List; -- don't set yet, maybe all positional entries
1323
1324       --  This loop scans through component associations. On entry to the
1325       --  loop, an expression has been scanned at the start of the current
1326       --  association unless initial token was OTHERS, in which case
1327       --  Expr_Node is set to Empty.
1328
1329       loop
1330          --  Deal with others association first. This is a named association
1331
1332          if No (Expr_Node) then
1333             if No (Assoc_List) then
1334                Assoc_List := New_List;
1335             end if;
1336
1337             Append (P_Record_Or_Array_Component_Association, Assoc_List);
1338
1339          --  Improper use of WITH
1340
1341          elsif Token = Tok_With then
1342             Error_Msg_SC ("WITH must be preceded by single expression in " &
1343                              "extension aggregate");
1344             raise Error_Resync;
1345
1346          --  A range attribute can only appear as part of a discrete choice
1347          --  list.
1348
1349          elsif Nkind (Expr_Node) = N_Attribute_Reference
1350            and then Attribute_Name (Expr_Node) = Name_Range
1351            and then Token /= Tok_Arrow
1352            and then Token /= Tok_Vertical_Bar
1353          then
1354             Bad_Range_Attribute (Sloc (Expr_Node));
1355             return Error;
1356
1357          --  Assume positional case if comma, right paren, or literal or
1358          --  identifier or OTHERS follows (the latter cases are missing
1359          --  comma cases). Also assume positional if a semicolon follows,
1360          --  which can happen if there are missing parens
1361
1362          elsif Token = Tok_Comma
1363            or else Token = Tok_Right_Paren
1364            or else Token = Tok_Others
1365            or else Token in Token_Class_Lit_Or_Name
1366            or else Token = Tok_Semicolon
1367          then
1368             if Present (Assoc_List) then
1369                Error_Msg_BC
1370                   ("""='>"" expected (positional association cannot follow " &
1371                    "named association)");
1372             end if;
1373
1374             if No (Expr_List) then
1375                Expr_List := New_List;
1376             end if;
1377
1378             Append (Expr_Node, Expr_List);
1379
1380          --  Anything else is assumed to be a named association
1381
1382          else
1383             Restore_Scan_State (Scan_State); -- to start of expression
1384
1385             if No (Assoc_List) then
1386                Assoc_List := New_List;
1387             end if;
1388
1389             Append (P_Record_Or_Array_Component_Association, Assoc_List);
1390          end if;
1391
1392          exit when not Comma_Present;
1393
1394          --  If we are at an expression terminator, something is seriously
1395          --  wrong, so let's get out now, before we start eating up stuff
1396          --  that doesn't belong to us!
1397
1398          if Token in Token_Class_Eterm then
1399             Error_Msg_AP ("expecting expression or component association");
1400             exit;
1401          end if;
1402
1403          --  Otherwise initiate for reentry to top of loop by scanning an
1404          --  initial expression, unless the first token is OTHERS.
1405
1406          if Token = Tok_Others then
1407             Expr_Node := Empty;
1408          else
1409             Save_Scan_State (Scan_State); -- at start of expression
1410             Expr_Node := P_Expression_Or_Range_Attribute;
1411
1412          end if;
1413       end loop;
1414
1415       --  All component associations (positional and named) have been scanned
1416
1417       T_Right_Paren;
1418       Set_Expressions (Aggregate_Node, Expr_List);
1419       Set_Component_Associations (Aggregate_Node, Assoc_List);
1420       return Aggregate_Node;
1421    end P_Aggregate_Or_Paren_Expr;
1422
1423    ------------------------------------------------
1424    -- 4.3  Record or Array Component Association --
1425    ------------------------------------------------
1426
1427    --  RECORD_COMPONENT_ASSOCIATION ::=
1428    --    [COMPONENT_CHOICE_LIST =>] EXPRESSION
1429    --  | COMPONENT_CHOICE_LIST => <>
1430
1431    --  COMPONENT_CHOICE_LIST =>
1432    --    component_SELECTOR_NAME {| component_SELECTOR_NAME}
1433    --  | others
1434
1435    --  ARRAY_COMPONENT_ASSOCIATION ::=
1436    --    DISCRETE_CHOICE_LIST => EXPRESSION
1437    --  | DISCRETE_CHOICE_LIST => <>
1438
1439    --  Note: this routine only handles the named cases, including others.
1440    --  Cases where the component choice list is not present have already
1441    --  been handled directly.
1442
1443    --  Error recovery: can raise Error_Resync
1444
1445    --  Note: RECORD_COMPONENT_ASSOCIATION and ARRAY_COMPONENT_ASSOCIATION
1446    --        rules have been extended to give support to Ada 2005 limited
1447    --        aggregates (AI-287)
1448
1449    function P_Record_Or_Array_Component_Association return Node_Id is
1450       Assoc_Node : Node_Id;
1451
1452    begin
1453       Assoc_Node := New_Node (N_Component_Association, Token_Ptr);
1454       Set_Choices (Assoc_Node, P_Discrete_Choice_List);
1455       Set_Sloc (Assoc_Node, Token_Ptr);
1456       TF_Arrow;
1457
1458       if Token = Tok_Box then
1459
1460          --  Ada 2005(AI-287): The box notation is used to indicate the
1461          --  default initialization of aggregate components
1462
1463          if Ada_Version < Ada_05 then
1464             Error_Msg_SP
1465               ("component association with '<'> is an Ada 2005 extension");
1466             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1467          end if;
1468
1469          Set_Box_Present (Assoc_Node);
1470          Scan; -- Past box
1471       else
1472          Set_Expression (Assoc_Node, P_Expression);
1473       end if;
1474
1475       return Assoc_Node;
1476    end P_Record_Or_Array_Component_Association;
1477
1478    -----------------------------
1479    -- 4.3.1  Record Aggregate --
1480    -----------------------------
1481
1482    --  Case of enumeration aggregate is parsed by P_Aggregate (4.3)
1483    --  All other cases are parsed by P_Aggregate_Or_Paren_Expr (4.3)
1484
1485    ----------------------------------------------
1486    -- 4.3.1  Record Component Association List --
1487    ----------------------------------------------
1488
1489    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1490
1491    ----------------------------------
1492    -- 4.3.1  Component Choice List --
1493    ----------------------------------
1494
1495    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1496
1497    --------------------------------
1498    -- 4.3.1  Extension Aggregate --
1499    --------------------------------
1500
1501    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1502
1503    --------------------------
1504    -- 4.3.1  Ancestor Part --
1505    --------------------------
1506
1507    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1508
1509    ----------------------------
1510    -- 4.3.1  Array Aggregate --
1511    ----------------------------
1512
1513    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1514
1515    ---------------------------------------
1516    -- 4.3.1  Positional Array Aggregate --
1517    ---------------------------------------
1518
1519    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1520
1521    ----------------------------------
1522    -- 4.3.1  Named Array Aggregate --
1523    ----------------------------------
1524
1525    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1526
1527    ----------------------------------------
1528    -- 4.3.1  Array Component Association --
1529    ----------------------------------------
1530
1531    --  Parsed by P_Aggregate_Or_Paren_Expr (4.3)
1532
1533    ---------------------
1534    -- 4.4  Expression --
1535    ---------------------
1536
1537    --  EXPRESSION ::=
1538    --    RELATION {and RELATION} | RELATION {and then RELATION}
1539    --  | RELATION {or RELATION}  | RELATION {or else RELATION}
1540    --  | RELATION {xor RELATION}
1541
1542    --  On return, Expr_Form indicates the categorization of the expression
1543    --  EF_Range_Attr is not a possible value (if a range attribute is found,
1544    --  an error message is given, and Error is returned).
1545
1546    --  Error recovery: cannot raise Error_Resync
1547
1548    function P_Expression return Node_Id is
1549       Logical_Op      : Node_Kind;
1550       Prev_Logical_Op : Node_Kind;
1551       Op_Location     : Source_Ptr;
1552       Node1           : Node_Id;
1553       Node2           : Node_Id;
1554
1555    begin
1556       Node1 := P_Relation;
1557
1558       if Token in Token_Class_Logop then
1559          Prev_Logical_Op := N_Empty;
1560
1561          loop
1562             Op_Location := Token_Ptr;
1563             Logical_Op := P_Logical_Operator;
1564
1565             if Prev_Logical_Op /= N_Empty and then
1566                Logical_Op /= Prev_Logical_Op
1567             then
1568                Error_Msg
1569                  ("mixed logical operators in expression", Op_Location);
1570                Prev_Logical_Op := N_Empty;
1571             else
1572                Prev_Logical_Op := Logical_Op;
1573             end if;
1574
1575             Node2 := Node1;
1576             Node1 := New_Node (Logical_Op, Op_Location);
1577             Set_Left_Opnd (Node1, Node2);
1578             Set_Right_Opnd (Node1, P_Relation);
1579             Set_Op_Name (Node1);
1580             exit when Token not in Token_Class_Logop;
1581          end loop;
1582
1583          Expr_Form := EF_Non_Simple;
1584       end if;
1585
1586       if Token = Tok_Apostrophe then
1587          Bad_Range_Attribute (Token_Ptr);
1588          return Error;
1589       else
1590          return Node1;
1591       end if;
1592    end P_Expression;
1593
1594    --  This function is identical to the normal P_Expression, except that it
1595    --  checks that the expression scan did not stop on a right paren. It is
1596    --  called in all contexts where a right parenthesis cannot legitimately
1597    --  follow an expression.
1598
1599    --  Error recovery: can not raise Error_Resync
1600
1601    function P_Expression_No_Right_Paren return Node_Id is
1602       Expr : constant Node_Id := P_Expression;
1603    begin
1604       Check_No_Right_Paren;
1605       return Expr;
1606    end P_Expression_No_Right_Paren;
1607
1608    ----------------------------------------
1609    -- 4.4  Expression_Or_Range_Attribute --
1610    ----------------------------------------
1611
1612    --  EXPRESSION ::=
1613    --    RELATION {and RELATION} | RELATION {and then RELATION}
1614    --  | RELATION {or RELATION}  | RELATION {or else RELATION}
1615    --  | RELATION {xor RELATION}
1616
1617    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
1618
1619    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
1620
1621    --  On return, Expr_Form indicates the categorization of the expression
1622    --  and EF_Range_Attr is one of the possibilities.
1623
1624    --  Error recovery: cannot raise Error_Resync
1625
1626    --  In the grammar, a RANGE attribute is simply a name, but its use is
1627    --  highly restricted, so in the parser, we do not regard it as a name.
1628    --  Instead, P_Name returns without scanning the 'RANGE part of the
1629    --  attribute, and P_Expression_Or_Range_Attribute handles the range
1630    --  attribute reference. In the normal case where a range attribute is
1631    --  not allowed, an error message is issued by P_Expression.
1632
1633    function P_Expression_Or_Range_Attribute return Node_Id is
1634       Logical_Op      : Node_Kind;
1635       Prev_Logical_Op : Node_Kind;
1636       Op_Location     : Source_Ptr;
1637       Node1           : Node_Id;
1638       Node2           : Node_Id;
1639       Attr_Node       : Node_Id;
1640
1641    begin
1642       Node1 := P_Relation;
1643
1644       if Token = Tok_Apostrophe then
1645          Attr_Node := P_Range_Attribute_Reference (Node1);
1646          Expr_Form := EF_Range_Attr;
1647          return Attr_Node;
1648
1649       elsif Token in Token_Class_Logop then
1650          Prev_Logical_Op := N_Empty;
1651
1652          loop
1653             Op_Location := Token_Ptr;
1654             Logical_Op := P_Logical_Operator;
1655
1656             if Prev_Logical_Op /= N_Empty and then
1657                Logical_Op /= Prev_Logical_Op
1658             then
1659                Error_Msg
1660                  ("mixed logical operators in expression", Op_Location);
1661                Prev_Logical_Op := N_Empty;
1662             else
1663                Prev_Logical_Op := Logical_Op;
1664             end if;
1665
1666             Node2 := Node1;
1667             Node1 := New_Node (Logical_Op, Op_Location);
1668             Set_Left_Opnd (Node1, Node2);
1669             Set_Right_Opnd (Node1, P_Relation);
1670             Set_Op_Name (Node1);
1671             exit when Token not in Token_Class_Logop;
1672          end loop;
1673
1674          Expr_Form := EF_Non_Simple;
1675       end if;
1676
1677       if Token = Tok_Apostrophe then
1678          Bad_Range_Attribute (Token_Ptr);
1679          return Error;
1680       else
1681          return Node1;
1682       end if;
1683    end P_Expression_Or_Range_Attribute;
1684
1685    -------------------
1686    -- 4.4  Relation --
1687    -------------------
1688
1689    --  RELATION ::=
1690    --    SIMPLE_EXPRESSION [RELATIONAL_OPERATOR SIMPLE_EXPRESSION]
1691    --  | SIMPLE_EXPRESSION [not] in RANGE
1692    --  | SIMPLE_EXPRESSION [not] in SUBTYPE_MARK
1693
1694    --  On return, Expr_Form indicates the categorization of the expression
1695
1696    --  Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1697    --  EF_Simple_Name and the following token is RANGE (range attribute case).
1698
1699    --  Error recovery: cannot raise Error_Resync. If an error occurs within an
1700    --  expression, then tokens are scanned until either a non-expression token,
1701    --  a right paren (not matched by a left paren) or a comma, is encountered.
1702
1703    function P_Relation return Node_Id is
1704       Node1, Node2 : Node_Id;
1705       Optok        : Source_Ptr;
1706
1707    begin
1708       Node1 := P_Simple_Expression;
1709
1710       if Token not in Token_Class_Relop then
1711          return Node1;
1712
1713       else
1714          --  Here we have a relational operator following. If so then scan it
1715          --  out. Note that the assignment symbol := is treated as a relational
1716          --  operator to improve the error recovery when it is misused for =.
1717          --  P_Relational_Operator also parses the IN and NOT IN operations.
1718
1719          Optok := Token_Ptr;
1720          Node2 := New_Node (P_Relational_Operator, Optok);
1721          Set_Left_Opnd (Node2, Node1);
1722          Set_Op_Name (Node2);
1723
1724          --  Case of IN or NOT IN
1725
1726          if Prev_Token = Tok_In then
1727             Set_Right_Opnd (Node2, P_Range_Or_Subtype_Mark);
1728
1729          --  Case of relational operator (= /= < <= > >=)
1730
1731          else
1732             Set_Right_Opnd (Node2, P_Simple_Expression);
1733          end if;
1734
1735          Expr_Form := EF_Non_Simple;
1736
1737          if Token in Token_Class_Relop then
1738             Error_Msg_SC ("unexpected relational operator");
1739             raise Error_Resync;
1740          end if;
1741
1742          return Node2;
1743       end if;
1744
1745    --  If any error occurs, then scan to the next expression terminator symbol
1746    --  or comma or right paren at the outer (i.e. current) parentheses level.
1747    --  The flags are set to indicate a normal simple expression.
1748
1749    exception
1750       when Error_Resync =>
1751          Resync_Expression;
1752          Expr_Form := EF_Simple;
1753          return Error;
1754    end P_Relation;
1755
1756    ----------------------------
1757    -- 4.4  Simple Expression --
1758    ----------------------------
1759
1760    --  SIMPLE_EXPRESSION ::=
1761    --    [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
1762
1763    --  On return, Expr_Form indicates the categorization of the expression
1764
1765    --  Note: if Token = Tok_Apostrophe on return, then Expr_Form is set to
1766    --  EF_Simple_Name and the following token is RANGE (range attribute case).
1767
1768    --  Error recovery: cannot raise Error_Resync. If an error occurs within an
1769    --  expression, then tokens are scanned until either a non-expression token,
1770    --  a right paren (not matched by a left paren) or a comma, is encountered.
1771
1772    --  Note: P_Simple_Expression is called only internally by higher level
1773    --  expression routines. In cases in the grammar where a simple expression
1774    --  is required, the approach is to scan an expression, and then post an
1775    --  appropriate error message if the expression obtained is not simple. This
1776    --  gives better error recovery and treatment.
1777
1778    function P_Simple_Expression return Node_Id is
1779       Scan_State : Saved_Scan_State;
1780       Node1      : Node_Id;
1781       Node2      : Node_Id;
1782       Tokptr     : Source_Ptr;
1783
1784    begin
1785       --  Check for cases starting with a name. There are two reasons for
1786       --  special casing. First speed things up by catching a common case
1787       --  without going through several routine layers. Second the caller must
1788       --  be informed via Expr_Form when the simple expression is a name.
1789
1790       if Token in Token_Class_Name then
1791          Node1 := P_Name;
1792
1793          --  Deal with apostrophe cases
1794
1795          if Token = Tok_Apostrophe then
1796             Save_Scan_State (Scan_State); -- at apostrophe
1797             Scan; -- past apostrophe
1798
1799             --  If qualified expression, scan it out and fall through
1800
1801             if Token = Tok_Left_Paren then
1802                Node1 := P_Qualified_Expression (Node1);
1803                Expr_Form := EF_Simple;
1804
1805             --  If range attribute, then we return with Token pointing to the
1806             --  apostrophe. Note: avoid the normal error check on exit. We
1807             --  know that the expression really is complete in this case!
1808
1809             else -- Token = Tok_Range then
1810                Restore_Scan_State (Scan_State); -- to apostrophe
1811                Expr_Form := EF_Simple_Name;
1812                return Node1;
1813             end if;
1814          end if;
1815
1816          --  If an expression terminator follows, the previous processing
1817          --  completely scanned out the expression (a common case), and
1818          --  left Expr_Form set appropriately for returning to our caller.
1819
1820          if Token in Token_Class_Sterm then
1821             null;
1822
1823          --  If we do not have an expression terminator, then complete the
1824          --  scan of a simple expression. This code duplicates the code
1825          --  found in P_Term and P_Factor.
1826
1827          else
1828             if Token = Tok_Double_Asterisk then
1829                if Style_Check then
1830                   Style.Check_Exponentiation_Operator;
1831                end if;
1832
1833                Node2 := New_Node (N_Op_Expon, Token_Ptr);
1834                Scan; -- past **
1835                Set_Left_Opnd (Node2, Node1);
1836                Set_Right_Opnd (Node2, P_Primary);
1837                Set_Op_Name (Node2);
1838                Node1 := Node2;
1839             end if;
1840
1841             loop
1842                exit when Token not in Token_Class_Mulop;
1843                Tokptr := Token_Ptr;
1844                Node2 := New_Node (P_Multiplying_Operator, Tokptr);
1845
1846                if Style_Check then
1847                   Style.Check_Binary_Operator;
1848                end if;
1849
1850                Scan; -- past operator
1851                Set_Left_Opnd (Node2, Node1);
1852                Set_Right_Opnd (Node2, P_Factor);
1853                Set_Op_Name (Node2);
1854                Node1 := Node2;
1855             end loop;
1856
1857             loop
1858                exit when Token not in Token_Class_Binary_Addop;
1859                Tokptr := Token_Ptr;
1860                Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1861
1862                if Style_Check then
1863                   Style.Check_Binary_Operator;
1864                end if;
1865
1866                Scan; -- past operator
1867                Set_Left_Opnd (Node2, Node1);
1868                Set_Right_Opnd (Node2, P_Term);
1869                Set_Op_Name (Node2);
1870                Node1 := Node2;
1871             end loop;
1872
1873             Expr_Form := EF_Simple;
1874          end if;
1875
1876       --  Cases where simple expression does not start with a name
1877
1878       else
1879          --  Scan initial sign and initial Term
1880
1881          if Token in Token_Class_Unary_Addop then
1882             Tokptr := Token_Ptr;
1883             Node1 := New_Node (P_Unary_Adding_Operator, Tokptr);
1884
1885             if Style_Check then
1886                Style.Check_Unary_Plus_Or_Minus;
1887             end if;
1888
1889             Scan; -- past operator
1890             Set_Right_Opnd (Node1, P_Term);
1891             Set_Op_Name (Node1);
1892          else
1893             Node1 := P_Term;
1894          end if;
1895
1896          --  In the following, we special-case a sequence of concatentations of
1897          --  string literals, such as "aaa" & "bbb" & ... & "ccc", with nothing
1898          --  else mixed in. For such a sequence, we return a tree representing
1899          --  "" & "aaabbb...ccc" (a single concatenation). This is done only if
1900          --  the number of concatenations is large. If semantic analysis
1901          --  resolves the "&" to a predefined one, then this folding gives the
1902          --  right answer. Otherwise, semantic analysis will complain about a
1903          --  capacity-exceeded error. The purpose of this trick is to avoid
1904          --  creating a deeply nested tree, which would cause deep recursion
1905          --  during semantics, causing stack overflow. This way, we can handle
1906          --  enormous concatenations in the normal case of predefined "&".  We
1907          --  first build up the normal tree, and then rewrite it if
1908          --  appropriate.
1909
1910          declare
1911             Num_Concats_Threshold : constant Positive := 1000;
1912             --  Arbitrary threshold value to enable optimization
1913
1914             First_Node : constant Node_Id := Node1;
1915             Is_Strlit_Concat : Boolean;
1916             --  True iff we've parsed a sequence of concatenations of string
1917             --  literals, with nothing else mixed in.
1918
1919             Num_Concats : Natural;
1920             --  Number of "&" operators if Is_Strlit_Concat is True
1921
1922          begin
1923             Is_Strlit_Concat :=
1924               Nkind (Node1) = N_String_Literal
1925                 and then Token = Tok_Ampersand;
1926             Num_Concats := 0;
1927
1928             --  Scan out sequence of terms separated by binary adding operators
1929
1930             loop
1931                exit when Token not in Token_Class_Binary_Addop;
1932                Tokptr := Token_Ptr;
1933                Node2 := New_Node (P_Binary_Adding_Operator, Tokptr);
1934                Scan; -- past operator
1935                Set_Left_Opnd (Node2, Node1);
1936                Node1 := P_Term;
1937                Set_Right_Opnd (Node2, Node1);
1938                Set_Op_Name (Node2);
1939
1940                --  Check if we're still concatenating string literals
1941
1942                Is_Strlit_Concat :=
1943                  Is_Strlit_Concat
1944                    and then Nkind (Node2) = N_Op_Concat
1945                  and then Nkind (Node1) = N_String_Literal;
1946
1947                if Is_Strlit_Concat then
1948                   Num_Concats := Num_Concats + 1;
1949                end if;
1950
1951                Node1 := Node2;
1952             end loop;
1953
1954             --  If we have an enormous series of concatenations of string
1955             --  literals, rewrite as explained above. The Is_Folded_In_Parser
1956             --  flag tells semantic analysis that if the "&" is not predefined,
1957             --  the folded value is wrong.
1958
1959             if Is_Strlit_Concat
1960               and then Num_Concats >= Num_Concats_Threshold
1961             then
1962                declare
1963                   Empty_String_Val : String_Id;
1964                   --  String_Id for ""
1965
1966                   Strlit_Concat_Val : String_Id;
1967                   --  Contains the folded value (which will be correct if the
1968                   --  "&" operators are the predefined ones).
1969
1970                   Cur_Node : Node_Id;
1971                   --  For walking up the tree
1972
1973                   New_Node : Node_Id;
1974                   --  Folded node to replace Node1
1975
1976                   Loc : constant Source_Ptr := Sloc (First_Node);
1977
1978                begin
1979                   --  Walk up the tree starting at the leftmost string literal
1980                   --  (First_Node), building up the Strlit_Concat_Val as we
1981                   --  go. Note that we do not use recursion here -- the whole
1982                   --  point is to avoid recursively walking that enormous tree.
1983
1984                   Start_String;
1985                   Store_String_Chars (Strval (First_Node));
1986
1987                   Cur_Node := Parent (First_Node);
1988                   while Present (Cur_Node) loop
1989                      pragma Assert (Nkind (Cur_Node) = N_Op_Concat and then
1990                         Nkind (Right_Opnd (Cur_Node)) = N_String_Literal);
1991
1992                      Store_String_Chars (Strval (Right_Opnd (Cur_Node)));
1993                      Cur_Node := Parent (Cur_Node);
1994                   end loop;
1995
1996                   Strlit_Concat_Val := End_String;
1997
1998                   --  Create new folded node, and rewrite result with a concat-
1999                   --  enation of an empty string literal and the folded node.
2000
2001                   Start_String;
2002                   Empty_String_Val := End_String;
2003                   New_Node :=
2004                     Make_Op_Concat (Loc,
2005                       Make_String_Literal (Loc, Empty_String_Val),
2006                       Make_String_Literal (Loc, Strlit_Concat_Val,
2007                         Is_Folded_In_Parser => True));
2008                   Rewrite (Node1, New_Node);
2009                end;
2010             end if;
2011          end;
2012
2013          --  All done, we clearly do not have name or numeric literal so this
2014          --  is a case of a simple expression which is some other possibility.
2015
2016          Expr_Form := EF_Simple;
2017       end if;
2018
2019       --  Come here at end of simple expression, where we do a couple of
2020       --  special checks to improve error recovery.
2021
2022       --  Special test to improve error recovery. If the current token
2023       --  is a period, then someone is trying to do selection on something
2024       --  that is not a name, e.g. a qualified expression.
2025
2026       if Token = Tok_Dot then
2027          Error_Msg_SC ("prefix for selection is not a name");
2028          raise Error_Resync;
2029       end if;
2030
2031       --  Special test to improve error recovery: If the current token is
2032       --  not the first token on a line (as determined by checking the
2033       --  previous token position with the start of the current line),
2034       --  then we insist that we have an appropriate terminating token.
2035       --  Consider the following two examples:
2036
2037       --   1)  if A nad B then ...
2038
2039       --   2)  A := B
2040       --       C := D
2041
2042       --  In the first example, we would like to issue a binary operator
2043       --  expected message and resynchronize to the then. In the second
2044       --  example, we do not want to issue a binary operator message, so
2045       --  that instead we will get the missing semicolon message. This
2046       --  distinction is of course a heuristic which does not always work,
2047       --  but in practice it is quite effective.
2048
2049       --  Note: the one case in which we do not go through this circuit is
2050       --  when we have scanned a range attribute and want to return with
2051       --  Token pointing to the apostrophe. The apostrophe is not normally
2052       --  an expression terminator, and is not in Token_Class_Sterm, but
2053       --  in this special case we know that the expression is complete.
2054
2055       if not Token_Is_At_Start_Of_Line
2056          and then Token not in Token_Class_Sterm
2057       then
2058          Error_Msg_AP ("binary operator expected");
2059          raise Error_Resync;
2060       else
2061          return Node1;
2062       end if;
2063
2064    --  If any error occurs, then scan to next expression terminator symbol
2065    --  or comma, right paren or vertical bar at the outer (i.e. current) paren
2066    --  level. Expr_Form is set to indicate a normal simple expression.
2067
2068    exception
2069       when Error_Resync =>
2070          Resync_Expression;
2071          Expr_Form := EF_Simple;
2072          return Error;
2073
2074    end P_Simple_Expression;
2075
2076    -----------------------------------------------
2077    -- 4.4  Simple Expression or Range Attribute --
2078    -----------------------------------------------
2079
2080    --  SIMPLE_EXPRESSION ::=
2081    --    [UNARY_ADDING_OPERATOR] TERM {BINARY_ADDING_OPERATOR TERM}
2082
2083    --  RANGE_ATTRIBUTE_REFERENCE ::= PREFIX ' RANGE_ATTRIBUTE_DESIGNATOR
2084
2085    --  RANGE_ATTRIBUTE_DESIGNATOR ::= range [(static_EXPRESSION)]
2086
2087    --  Error recovery: cannot raise Error_Resync
2088
2089    function P_Simple_Expression_Or_Range_Attribute return Node_Id is
2090       Sexpr     : Node_Id;
2091       Attr_Node : Node_Id;
2092
2093    begin
2094       --  We don't just want to roar ahead and call P_Simple_Expression
2095       --  here, since we want to handle the case of a parenthesized range
2096       --  attribute cleanly.
2097
2098       if Token = Tok_Left_Paren then
2099          declare
2100             Lptr       : constant Source_Ptr := Token_Ptr;
2101             Scan_State : Saved_Scan_State;
2102
2103          begin
2104             Save_Scan_State (Scan_State);
2105             Scan; -- past left paren
2106             Sexpr := P_Simple_Expression;
2107
2108             if Token = Tok_Apostrophe then
2109                Attr_Node := P_Range_Attribute_Reference (Sexpr);
2110                Expr_Form := EF_Range_Attr;
2111
2112                if Token = Tok_Right_Paren then
2113                   Scan; -- scan past right paren if present
2114                end if;
2115
2116                Error_Msg ("parentheses not allowed for range attribute", Lptr);
2117
2118                return Attr_Node;
2119             end if;
2120
2121             Restore_Scan_State (Scan_State);
2122          end;
2123       end if;
2124
2125       --  Here after dealing with parenthesized range attribute
2126
2127       Sexpr := P_Simple_Expression;
2128
2129       if Token = Tok_Apostrophe then
2130          Attr_Node := P_Range_Attribute_Reference (Sexpr);
2131          Expr_Form := EF_Range_Attr;
2132          return Attr_Node;
2133
2134       else
2135          return Sexpr;
2136       end if;
2137    end P_Simple_Expression_Or_Range_Attribute;
2138
2139    ---------------
2140    -- 4.4  Term --
2141    ---------------
2142
2143    --  TERM ::= FACTOR {MULTIPLYING_OPERATOR FACTOR}
2144
2145    --  Error recovery: can raise Error_Resync
2146
2147    function P_Term return Node_Id is
2148       Node1, Node2 : Node_Id;
2149       Tokptr       : Source_Ptr;
2150
2151    begin
2152       Node1 := P_Factor;
2153
2154       loop
2155          exit when Token not in Token_Class_Mulop;
2156          Tokptr := Token_Ptr;
2157          Node2 := New_Node (P_Multiplying_Operator, Tokptr);
2158          Scan; -- past operator
2159          Set_Left_Opnd (Node2, Node1);
2160          Set_Right_Opnd (Node2, P_Factor);
2161          Set_Op_Name (Node2);
2162          Node1 := Node2;
2163       end loop;
2164
2165       return Node1;
2166    end P_Term;
2167
2168    -----------------
2169    -- 4.4  Factor --
2170    -----------------
2171
2172    --  FACTOR ::= PRIMARY [** PRIMARY] | abs PRIMARY | not PRIMARY
2173
2174    --  Error recovery: can raise Error_Resync
2175
2176    function P_Factor return Node_Id is
2177       Node1 : Node_Id;
2178       Node2 : Node_Id;
2179
2180    begin
2181       if Token = Tok_Abs then
2182          Node1 := New_Node (N_Op_Abs, Token_Ptr);
2183
2184          if Style_Check then
2185             Style.Check_Abs_Not;
2186          end if;
2187
2188          Scan; -- past ABS
2189          Set_Right_Opnd (Node1, P_Primary);
2190          Set_Op_Name (Node1);
2191          return Node1;
2192
2193       elsif Token = Tok_Not then
2194          Node1 := New_Node (N_Op_Not, Token_Ptr);
2195
2196          if Style_Check then
2197             Style.Check_Abs_Not;
2198          end if;
2199
2200          Scan; -- past NOT
2201          Set_Right_Opnd (Node1, P_Primary);
2202          Set_Op_Name (Node1);
2203          return Node1;
2204
2205       else
2206          Node1 := P_Primary;
2207
2208          if Token = Tok_Double_Asterisk then
2209             Node2 := New_Node (N_Op_Expon, Token_Ptr);
2210             Scan; -- past **
2211             Set_Left_Opnd (Node2, Node1);
2212             Set_Right_Opnd (Node2, P_Primary);
2213             Set_Op_Name (Node2);
2214             return Node2;
2215          else
2216             return Node1;
2217          end if;
2218       end if;
2219    end P_Factor;
2220
2221    ------------------
2222    -- 4.4  Primary --
2223    ------------------
2224
2225    --  PRIMARY ::=
2226    --    NUMERIC_LITERAL  | null
2227    --  | STRING_LITERAL   | AGGREGATE
2228    --  | NAME             | QUALIFIED_EXPRESSION
2229    --  | ALLOCATOR        | (EXPRESSION)
2230
2231    --  Error recovery: can raise Error_Resync
2232
2233    function P_Primary return Node_Id is
2234       Scan_State : Saved_Scan_State;
2235       Node1      : Node_Id;
2236
2237    begin
2238       --  The loop runs more than once only if misplaced pragmas are found
2239
2240       loop
2241          case Token is
2242
2243             --  Name token can start a name, call or qualified expression, all
2244             --  of which are acceptable possibilities for primary. Note also
2245             --  that string literal is included in name (as operator symbol)
2246             --  and type conversion is included in name (as indexed component).
2247
2248             when Tok_Char_Literal | Tok_Operator_Symbol | Tok_Identifier =>
2249                Node1 := P_Name;
2250
2251                --  All done unless apostrophe follows
2252
2253                if Token /= Tok_Apostrophe then
2254                   return Node1;
2255
2256                --  Apostrophe following means that we have either just parsed
2257                --  the subtype mark of a qualified expression, or the prefix
2258                --  or a range attribute.
2259
2260                else -- Token = Tok_Apostrophe
2261                   Save_Scan_State (Scan_State); -- at apostrophe
2262                   Scan; -- past apostrophe
2263
2264                   --  If range attribute, then this is always an error, since
2265                   --  the only legitimate case (where the scanned expression is
2266                   --  a qualified simple name) is handled at the level of the
2267                   --  Simple_Expression processing. This case corresponds to a
2268                   --  usage such as 3 + A'Range, which is always illegal.
2269
2270                   if Token = Tok_Range then
2271                      Restore_Scan_State (Scan_State); -- to apostrophe
2272                      Bad_Range_Attribute (Token_Ptr);
2273                      return Error;
2274
2275                   --  If left paren, then we have a qualified expression.
2276                   --  Note that P_Name guarantees that in this case, where
2277                   --  Token = Tok_Apostrophe on return, the only two possible
2278                   --  tokens following the apostrophe are left paren and
2279                   --  RANGE, so we know we have a left paren here.
2280
2281                   else -- Token = Tok_Left_Paren
2282                      return P_Qualified_Expression (Node1);
2283
2284                   end if;
2285                end if;
2286
2287             --  Numeric or string literal
2288
2289             when Tok_Integer_Literal |
2290                  Tok_Real_Literal    |
2291                  Tok_String_Literal  =>
2292
2293                Node1 := Token_Node;
2294                Scan; -- past number
2295                return Node1;
2296
2297             --  Left paren, starts aggregate or parenthesized expression
2298
2299             when Tok_Left_Paren =>
2300                declare
2301                   Expr : constant Node_Id := P_Aggregate_Or_Paren_Expr;
2302
2303                begin
2304                   if Nkind (Expr) = N_Attribute_Reference
2305                     and then Attribute_Name (Expr) = Name_Range
2306                   then
2307                      Bad_Range_Attribute (Sloc (Expr));
2308                   end if;
2309
2310                   return Expr;
2311                end;
2312
2313             --  Allocator
2314
2315             when Tok_New =>
2316                return P_Allocator;
2317
2318             --  Null
2319
2320             when Tok_Null =>
2321                Scan; -- past NULL
2322                return New_Node (N_Null, Prev_Token_Ptr);
2323
2324             --  Pragma, not allowed here, so just skip past it
2325
2326             when Tok_Pragma =>
2327                P_Pragmas_Misplaced;
2328
2329             --  Anything else is illegal as the first token of a primary, but
2330             --  we test for a reserved identifier so that it is treated nicely
2331
2332             when others =>
2333                if Is_Reserved_Identifier then
2334                   return P_Identifier;
2335
2336                elsif Prev_Token = Tok_Comma then
2337                   Error_Msg_SP ("extra "","" ignored");
2338                   raise Error_Resync;
2339
2340                else
2341                   Error_Msg_AP ("missing operand");
2342                   raise Error_Resync;
2343                end if;
2344
2345          end case;
2346       end loop;
2347    end P_Primary;
2348
2349    ---------------------------
2350    -- 4.5  Logical Operator --
2351    ---------------------------
2352
2353    --  LOGICAL_OPERATOR  ::=  and | or | xor
2354
2355    --  Note: AND THEN and OR ELSE are also treated as logical operators
2356    --  by the parser (even though they are not operators semantically)
2357
2358    --  The value returned is the appropriate Node_Kind code for the operator
2359    --  On return, Token points to the token following the scanned operator.
2360
2361    --  The caller has checked that the first token is a legitimate logical
2362    --  operator token (i.e. is either XOR, AND, OR).
2363
2364    --  Error recovery: cannot raise Error_Resync
2365
2366    function P_Logical_Operator return Node_Kind is
2367    begin
2368       if Token = Tok_And then
2369          if Style_Check then
2370             Style.Check_Binary_Operator;
2371          end if;
2372
2373          Scan; -- past AND
2374
2375          if Token = Tok_Then then
2376             Scan; -- past THEN
2377             return N_And_Then;
2378          else
2379             return N_Op_And;
2380          end if;
2381
2382       elsif Token = Tok_Or then
2383          if Style_Check then
2384             Style.Check_Binary_Operator;
2385          end if;
2386
2387          Scan; -- past OR
2388
2389          if Token = Tok_Else then
2390             Scan; -- past ELSE
2391             return N_Or_Else;
2392          else
2393             return N_Op_Or;
2394          end if;
2395
2396       else -- Token = Tok_Xor
2397          if Style_Check then
2398             Style.Check_Binary_Operator;
2399          end if;
2400
2401          Scan; -- past XOR
2402          return N_Op_Xor;
2403       end if;
2404    end P_Logical_Operator;
2405
2406    ------------------------------
2407    -- 4.5  Relational Operator --
2408    ------------------------------
2409
2410    --  RELATIONAL_OPERATOR ::= = | /= | < | <= | > | >=
2411
2412    --  The value returned is the appropriate Node_Kind code for the operator.
2413    --  On return, Token points to the operator token, NOT past it.
2414
2415    --  The caller has checked that the first token is a legitimate relational
2416    --  operator token (i.e. is one of the operator tokens listed above).
2417
2418    --  Error recovery: cannot raise Error_Resync
2419
2420    function P_Relational_Operator return Node_Kind is
2421       Op_Kind : Node_Kind;
2422       Relop_Node : constant array (Token_Class_Relop) of Node_Kind :=
2423         (Tok_Less           => N_Op_Lt,
2424          Tok_Equal          => N_Op_Eq,
2425          Tok_Greater        => N_Op_Gt,
2426          Tok_Not_Equal      => N_Op_Ne,
2427          Tok_Greater_Equal  => N_Op_Ge,
2428          Tok_Less_Equal     => N_Op_Le,
2429          Tok_In             => N_In,
2430          Tok_Not            => N_Not_In,
2431          Tok_Box            => N_Op_Ne);
2432
2433    begin
2434       if Token = Tok_Box then
2435          Error_Msg_SC ("""'<'>"" should be ""/=""");
2436       end if;
2437
2438       Op_Kind := Relop_Node (Token);
2439
2440       if Style_Check then
2441          Style.Check_Binary_Operator;
2442       end if;
2443
2444       Scan; -- past operator token
2445
2446       if Prev_Token = Tok_Not then
2447          T_In;
2448       end if;
2449
2450       return Op_Kind;
2451    end P_Relational_Operator;
2452
2453    ---------------------------------
2454    -- 4.5  Binary Adding Operator --
2455    ---------------------------------
2456
2457    --  BINARY_ADDING_OPERATOR ::= + | - | &
2458
2459    --  The value returned is the appropriate Node_Kind code for the operator.
2460    --  On return, Token points to the operator token (NOT past it).
2461
2462    --  The caller has checked that the first token is a legitimate adding
2463    --  operator token (i.e. is one of the operator tokens listed above).
2464
2465    --  Error recovery: cannot raise Error_Resync
2466
2467    function P_Binary_Adding_Operator return Node_Kind is
2468       Addop_Node : constant array (Token_Class_Binary_Addop) of Node_Kind :=
2469         (Tok_Ampersand      => N_Op_Concat,
2470          Tok_Minus          => N_Op_Subtract,
2471          Tok_Plus           => N_Op_Add);
2472    begin
2473       return Addop_Node (Token);
2474    end P_Binary_Adding_Operator;
2475
2476    --------------------------------
2477    -- 4.5  Unary Adding Operator --
2478    --------------------------------
2479
2480    --  UNARY_ADDING_OPERATOR ::= + | -
2481
2482    --  The value returned is the appropriate Node_Kind code for the operator.
2483    --  On return, Token points to the operator token (NOT past it).
2484
2485    --  The caller has checked that the first token is a legitimate adding
2486    --  operator token (i.e. is one of the operator tokens listed above).
2487
2488    --  Error recovery: cannot raise Error_Resync
2489
2490    function P_Unary_Adding_Operator return Node_Kind is
2491       Addop_Node : constant array (Token_Class_Unary_Addop) of Node_Kind :=
2492         (Tok_Minus          => N_Op_Minus,
2493          Tok_Plus           => N_Op_Plus);
2494    begin
2495       return Addop_Node (Token);
2496    end P_Unary_Adding_Operator;
2497
2498    -------------------------------
2499    -- 4.5  Multiplying Operator --
2500    -------------------------------
2501
2502    --  MULTIPLYING_OPERATOR ::= * | / | mod | rem
2503
2504    --  The value returned is the appropriate Node_Kind code for the operator.
2505    --  On return, Token points to the operator token (NOT past it).
2506
2507    --  The caller has checked that the first token is a legitimate multiplying
2508    --  operator token (i.e. is one of the operator tokens listed above).
2509
2510    --  Error recovery: cannot raise Error_Resync
2511
2512    function P_Multiplying_Operator return Node_Kind is
2513       Mulop_Node : constant array (Token_Class_Mulop) of Node_Kind :=
2514         (Tok_Asterisk       => N_Op_Multiply,
2515          Tok_Mod            => N_Op_Mod,
2516          Tok_Rem            => N_Op_Rem,
2517          Tok_Slash          => N_Op_Divide);
2518    begin
2519       return Mulop_Node (Token);
2520    end P_Multiplying_Operator;
2521
2522    --------------------------------------
2523    -- 4.5  Highest Precedence Operator --
2524    --------------------------------------
2525
2526    --  Parsed by P_Factor (4.4)
2527
2528    --  Note: this rule is not in fact used by the grammar at any point!
2529
2530    --------------------------
2531    -- 4.6  Type Conversion --
2532    --------------------------
2533
2534    --  Parsed by P_Primary as a Name (4.1)
2535
2536    -------------------------------
2537    -- 4.7  Qualified Expression --
2538    -------------------------------
2539
2540    --  QUALIFIED_EXPRESSION ::=
2541    --    SUBTYPE_MARK ' (EXPRESSION) | SUBTYPE_MARK ' AGGREGATE
2542
2543    --  The caller has scanned the name which is the Subtype_Mark parameter
2544    --  and scanned past the single quote following the subtype mark. The
2545    --  caller has not checked that this name is in fact appropriate for
2546    --  a subtype mark name (i.e. it is a selected component or identifier).
2547
2548    --  Error_Recovery: cannot raise Error_Resync
2549
2550    function  P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id is
2551       Qual_Node : Node_Id;
2552    begin
2553       Qual_Node := New_Node (N_Qualified_Expression, Prev_Token_Ptr);
2554       Set_Subtype_Mark (Qual_Node, Check_Subtype_Mark (Subtype_Mark));
2555       Set_Expression (Qual_Node, P_Aggregate_Or_Paren_Expr);
2556       return Qual_Node;
2557    end P_Qualified_Expression;
2558
2559    --------------------
2560    -- 4.8  Allocator --
2561    --------------------
2562
2563    --  ALLOCATOR ::=
2564    --    new [NULL_EXCLUSION] SUBTYPE_INDICATION | new QUALIFIED_EXPRESSION
2565
2566    --  The caller has checked that the initial token is NEW
2567
2568    --  Error recovery: can raise Error_Resync
2569
2570    function P_Allocator return Node_Id is
2571       Alloc_Node             : Node_Id;
2572       Type_Node              : Node_Id;
2573       Null_Exclusion_Present : Boolean;
2574
2575    begin
2576       Alloc_Node := New_Node (N_Allocator, Token_Ptr);
2577       T_New;
2578
2579       --  Scan Null_Exclusion if present (Ada 2005 (AI-231))
2580
2581       Null_Exclusion_Present := P_Null_Exclusion;
2582       Set_Null_Exclusion_Present (Alloc_Node, Null_Exclusion_Present);
2583       Type_Node := P_Subtype_Mark_Resync;
2584
2585       if Token = Tok_Apostrophe then
2586          Scan; -- past apostrophe
2587          Set_Expression (Alloc_Node, P_Qualified_Expression (Type_Node));
2588       else
2589          Set_Expression
2590            (Alloc_Node,
2591             P_Subtype_Indication (Type_Node, Null_Exclusion_Present));
2592       end if;
2593
2594       return Alloc_Node;
2595    end P_Allocator;
2596
2597 end Ch4;