OSDN Git Service

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