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