OSDN Git Service

libitm: Remove unused code.
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch13.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P A R . C H 1 3                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 --  Turn off subprogram body ordering check. Subprograms are in order
28 --  by RM section rather than alphabetical
29
30 separate (Par)
31 package body Ch13 is
32
33    --  Local functions, used only in this chapter
34
35    function P_Component_Clause return Node_Id;
36    function P_Mod_Clause return Node_Id;
37
38    -----------------------------------
39    -- Aspect_Specifications_Present --
40    -----------------------------------
41
42    function Aspect_Specifications_Present
43      (Strict : Boolean := Ada_Version < Ada_2012) return Boolean
44    is
45       Scan_State : Saved_Scan_State;
46       Result     : Boolean;
47
48    begin
49       --  Definitely must have WITH to consider aspect specs to be present
50
51       --  Note that this means that if we have a semicolon, we immediately
52       --  return False. There is a case in which this is not optimal, namely
53       --  something like
54
55       --    type R is new Integer;
56       --      with bla bla;
57
58       --  where the semicolon is redundant, but scanning forward for it would
59       --  be too expensive. Instead we pick up the aspect specifications later
60       --  as a bogus declaration, and diagnose the semicolon at that point.
61
62       if Token /= Tok_With then
63          return False;
64       end if;
65
66       --  Have a WITH, see if it looks like an aspect specification
67
68       Save_Scan_State (Scan_State);
69       Scan; -- past WITH
70
71       --  If no identifier, then consider that we definitely do not have an
72       --  aspect specification.
73
74       if Token /= Tok_Identifier then
75          Result := False;
76
77       --  This is where we pay attention to the Strict mode. Normally when we
78       --  are in Ada 2012 mode, Strict is False, and we consider that we have
79       --  an aspect specification if the identifier is an aspect name (even if
80       --  not followed by =>) or the identifier is not an aspect name but is
81       --  followed by =>. P_Aspect_Specifications will generate messages if the
82       --  aspect specification is ill-formed.
83
84       elsif not Strict then
85          if Get_Aspect_Id (Token_Name) /= No_Aspect then
86             Result := True;
87          else
88             Scan; -- past identifier
89             Result := Token = Tok_Arrow;
90          end if;
91
92       --  If earlier than Ada 2012, check for valid aspect identifier (possibly
93       --  completed with 'CLASS) followed by an arrow, and consider that this
94       --  is still an aspect specification so we give an appropriate message.
95
96       else
97          if Get_Aspect_Id (Token_Name) = No_Aspect then
98             Result := False;
99
100          else
101             Scan; -- past aspect name
102
103             Result := False;
104
105             if Token = Tok_Arrow then
106                Result := True;
107
108             elsif Token = Tok_Apostrophe then
109                Scan; -- past apostrophe
110
111                if Token = Tok_Identifier
112                  and then Token_Name = Name_Class
113                then
114                   Scan; -- past CLASS
115
116                   if Token = Tok_Arrow then
117                      Result := True;
118                   end if;
119                end if;
120             end if;
121
122             if Result then
123                Restore_Scan_State (Scan_State);
124                Error_Msg_SC ("|aspect specification is an Ada 2012 feature");
125                Error_Msg_SC ("\|unit must be compiled with -gnat2012 switch");
126                return True;
127             end if;
128          end if;
129       end if;
130
131       Restore_Scan_State (Scan_State);
132       return Result;
133    end Aspect_Specifications_Present;
134
135    --------------------------------------------
136    -- 13.1  Representation Clause (also I.7) --
137    --------------------------------------------
138
139    --  REPRESENTATION_CLAUSE ::=
140    --    ATTRIBUTE_DEFINITION_CLAUSE
141    --  | ENUMERATION_REPRESENTATION_CLAUSE
142    --  | RECORD_REPRESENTATION_CLAUSE
143    --  | AT_CLAUSE
144
145    --  ATTRIBUTE_DEFINITION_CLAUSE ::=
146    --    for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use EXPRESSION;
147    --  | for LOCAL_NAME'ATTRIBUTE_DESIGNATOR use NAME;
148
149    --  Note: in Ada 83, the expression must be a simple expression
150
151    --  AT_CLAUSE ::= for DIRECT_NAME use at EXPRESSION;
152
153    --  Note: in Ada 83, the expression must be a simple expression
154
155    --  ENUMERATION_REPRESENTATION_CLAUSE ::=
156    --    for first_subtype_LOCAL_NAME use ENUMERATION_AGGREGATE;
157
158    --  ENUMERATION_AGGREGATE ::= ARRAY_AGGREGATE
159
160    --  RECORD_REPRESENTATION_CLAUSE ::=
161    --    for first_subtype_LOCAL_NAME use
162    --      record [MOD_CLAUSE]
163    --        {COMPONENT_CLAUSE}
164    --      end record;
165
166    --  Note: for now we allow only a direct name as the local name in the
167    --  above constructs. This probably needs changing later on ???
168
169    --  The caller has checked that the initial token is FOR
170
171    --  Error recovery: cannot raise Error_Resync, if an error occurs,
172    --  the scan is repositioned past the next semicolon.
173
174    function P_Representation_Clause return Node_Id is
175       For_Loc         : Source_Ptr;
176       Name_Node       : Node_Id;
177       Prefix_Node     : Node_Id;
178       Attr_Name       : Name_Id;
179       Identifier_Node : Node_Id;
180       Rep_Clause_Node : Node_Id;
181       Expr_Node       : Node_Id;
182       Record_Items    : List_Id;
183
184    begin
185       For_Loc := Token_Ptr;
186       Scan; -- past FOR
187
188       --  Note that the name in a representation clause is always a simple
189       --  name, even in the attribute case, see AI-300 which made this so!
190
191       Identifier_Node := P_Identifier (C_Use);
192
193       --  Check case of qualified name to give good error message
194
195       if Token = Tok_Dot then
196          Error_Msg_SC
197             ("representation clause requires simple name!");
198
199          loop
200             exit when Token /= Tok_Dot;
201             Scan; -- past dot
202             Discard_Junk_Node (P_Identifier);
203          end loop;
204       end if;
205
206       --  Attribute Definition Clause
207
208       if Token = Tok_Apostrophe then
209
210          --  Allow local names of the form a'b'.... This enables
211          --  us to parse class-wide streams attributes correctly.
212
213          Name_Node := Identifier_Node;
214          while Token = Tok_Apostrophe loop
215
216             Scan; -- past apostrophe
217
218             Identifier_Node := Token_Node;
219             Attr_Name := No_Name;
220
221             if Token = Tok_Identifier then
222                Attr_Name := Token_Name;
223
224                if not Is_Attribute_Name (Attr_Name) then
225                   Signal_Bad_Attribute;
226                end if;
227
228                if Style_Check then
229                   Style.Check_Attribute_Name (False);
230                end if;
231
232             --  Here for case of attribute designator is not an identifier
233
234             else
235                if Token = Tok_Delta then
236                   Attr_Name := Name_Delta;
237
238                elsif Token = Tok_Digits then
239                   Attr_Name := Name_Digits;
240
241                elsif Token = Tok_Access then
242                   Attr_Name := Name_Access;
243
244                else
245                   Error_Msg_AP ("attribute designator expected");
246                   raise Error_Resync;
247                end if;
248
249                if Style_Check then
250                   Style.Check_Attribute_Name (True);
251                end if;
252             end if;
253
254             --  We come here with an OK attribute scanned, and the
255             --  corresponding Attribute identifier node stored in Ident_Node.
256
257             Prefix_Node := Name_Node;
258             Name_Node := New_Node (N_Attribute_Reference, Prev_Token_Ptr);
259             Set_Prefix (Name_Node, Prefix_Node);
260             Set_Attribute_Name (Name_Node, Attr_Name);
261             Scan;
262          end loop;
263
264          Rep_Clause_Node := New_Node (N_Attribute_Definition_Clause, For_Loc);
265          Set_Name (Rep_Clause_Node, Prefix_Node);
266          Set_Chars (Rep_Clause_Node, Attr_Name);
267          T_Use;
268
269          Expr_Node := P_Expression_No_Right_Paren;
270          Check_Simple_Expression_In_Ada_83 (Expr_Node);
271          Set_Expression (Rep_Clause_Node, Expr_Node);
272
273       else
274          TF_Use;
275          Rep_Clause_Node := Empty;
276
277          --  AT follows USE (At Clause)
278
279          if Token = Tok_At then
280             Scan; -- past AT
281             Rep_Clause_Node := New_Node (N_At_Clause, For_Loc);
282             Set_Identifier (Rep_Clause_Node, Identifier_Node);
283             Expr_Node := P_Expression_No_Right_Paren;
284             Check_Simple_Expression_In_Ada_83 (Expr_Node);
285             Set_Expression (Rep_Clause_Node, Expr_Node);
286
287          --  RECORD follows USE (Record Representation Clause)
288
289          elsif Token = Tok_Record then
290             Record_Items := P_Pragmas_Opt;
291             Rep_Clause_Node :=
292               New_Node (N_Record_Representation_Clause, For_Loc);
293             Set_Identifier (Rep_Clause_Node, Identifier_Node);
294
295             Push_Scope_Stack;
296             Scope.Table (Scope.Last).Etyp := E_Record;
297             Scope.Table (Scope.Last).Ecol := Start_Column;
298             Scope.Table (Scope.Last).Sloc := Token_Ptr;
299             Scan; -- past RECORD
300             Record_Items := P_Pragmas_Opt;
301
302             --  Possible Mod Clause
303
304             if Token = Tok_At then
305                Set_Mod_Clause (Rep_Clause_Node, P_Mod_Clause);
306                Set_Pragmas_Before (Mod_Clause (Rep_Clause_Node), Record_Items);
307                Record_Items := P_Pragmas_Opt;
308             end if;
309
310             if No (Record_Items) then
311                Record_Items := New_List;
312             end if;
313
314             Set_Component_Clauses (Rep_Clause_Node, Record_Items);
315
316             --  Loop through component clauses
317
318             loop
319                if Token not in Token_Class_Name then
320                   exit when Check_End;
321                end if;
322
323                Append (P_Component_Clause, Record_Items);
324                P_Pragmas_Opt (Record_Items);
325             end loop;
326
327          --  Left paren follows USE (Enumeration Representation Clause)
328
329          elsif Token = Tok_Left_Paren then
330             Rep_Clause_Node :=
331               New_Node (N_Enumeration_Representation_Clause, For_Loc);
332             Set_Identifier (Rep_Clause_Node, Identifier_Node);
333             Set_Array_Aggregate (Rep_Clause_Node, P_Aggregate);
334
335          --  Some other token follows FOR (invalid representation clause)
336
337          else
338             Error_Msg_SC ("invalid representation clause");
339             raise Error_Resync;
340          end if;
341       end if;
342
343       TF_Semicolon;
344       return Rep_Clause_Node;
345
346    exception
347       when Error_Resync =>
348          Resync_Past_Semicolon;
349          return Error;
350
351    end P_Representation_Clause;
352
353    ----------------------
354    -- 13.1  Local Name --
355    ----------------------
356
357    --  Local name is always parsed by its parent. In the case of its use in
358    --  pragmas, the check for a local name is handled in Par.Prag and allows
359    --  all the possible forms of local name. For the uses in chapter 13, we
360    --  currently only allow a direct name, but this should probably change???
361
362    ---------------------------
363    -- 13.1  At Clause (I.7) --
364    ---------------------------
365
366    --  Parsed by P_Representation_Clause (13.1)
367
368    ---------------------------------------
369    -- 13.3  Attribute Definition Clause --
370    ---------------------------------------
371
372    --  Parsed by P_Representation_Clause (13.1)
373
374    --------------------------------
375    -- 13.1  Aspect Specification --
376    --------------------------------
377
378    --  ASPECT_SPECIFICATION ::=
379    --    with ASPECT_MARK [=> ASPECT_DEFINITION] {.
380    --         ASPECT_MARK [=> ASPECT_DEFINITION] }
381
382    --  ASPECT_MARK ::= aspect_IDENTIFIER['Class]
383
384    --  ASPECT_DEFINITION ::= NAME | EXPRESSION
385
386    --  Error recovery: cannot raise Error_Resync
387
388    procedure P_Aspect_Specifications
389      (Decl      : Node_Id;
390       Semicolon : Boolean := True)
391    is
392       Aspects : List_Id;
393       Aspect  : Node_Id;
394       A_Id    : Aspect_Id;
395       OK      : Boolean;
396       Ptr     : Source_Ptr;
397
398    begin
399       --  Check if aspect specification present
400
401       if not Aspect_Specifications_Present then
402          if Semicolon then
403             TF_Semicolon;
404          end if;
405
406          return;
407       end if;
408
409       --  Aspect Specification is present
410
411       Ptr := Token_Ptr;
412       Scan; -- past WITH
413
414       --  Here we have an aspect specification to scan, note that we don;t
415       --  set the flag till later, because it may turn out that we have no
416       --  valid aspects in the list.
417
418       Aspects := Empty_List;
419       loop
420          OK := True;
421
422          if Token /= Tok_Identifier then
423             Error_Msg_SC ("aspect identifier expected");
424
425             if Semicolon then
426                Resync_Past_Semicolon;
427             end if;
428
429             return;
430          end if;
431
432          --  We have an identifier (which should be an aspect identifier)
433
434          A_Id := Get_Aspect_Id (Token_Name);
435          Aspect :=
436            Make_Aspect_Specification (Token_Ptr,
437              Identifier => Token_Node);
438
439          --  No valid aspect identifier present
440
441          if A_Id = No_Aspect then
442             Error_Msg_SC ("aspect identifier expected");
443
444             --  Check bad spelling
445
446             for J in Aspect_Id loop
447                if Is_Bad_Spelling_Of (Token_Name, Aspect_Names (J)) then
448                   Error_Msg_Name_1 := Aspect_Names (J);
449                   Error_Msg_SC -- CODEFIX
450                     ("\possible misspelling of%");
451                   exit;
452                end if;
453             end loop;
454
455             Scan; -- past incorrect identifier
456
457             if Token = Tok_Apostrophe then
458                Scan; -- past '
459                Scan; -- past presumably CLASS
460             end if;
461
462             if Token = Tok_Arrow then
463                Scan; -- Past arrow
464                Set_Expression (Aspect, P_Expression);
465                OK := False;
466
467             elsif Token = Tok_Comma then
468                OK := False;
469
470             else
471                if Semicolon then
472                   Resync_Past_Semicolon;
473                end if;
474
475                return;
476             end if;
477
478          --  OK aspect scanned
479
480          else
481             Scan; -- past identifier
482
483             --  Check for 'Class present
484
485             if Token = Tok_Apostrophe then
486                if not Class_Aspect_OK (A_Id) then
487                   Error_Msg_Node_1 := Identifier (Aspect);
488                   Error_Msg_SC ("aspect& does not permit attribute here");
489                   Scan; -- past apostrophe
490                   Scan; -- past presumed CLASS
491                   OK := False;
492
493                else
494                   Scan; -- past apostrophe
495
496                   if Token /= Tok_Identifier
497                     or else Token_Name /= Name_Class
498                   then
499                      Error_Msg_SC ("Class attribute expected here");
500                      OK := False;
501
502                      if Token = Tok_Identifier then
503                         Scan; -- past identifier not CLASS
504                      end if;
505
506                   else
507                      Scan; -- past CLASS
508                      Set_Class_Present (Aspect);
509                   end if;
510                end if;
511             end if;
512
513             --  Test case of missing aspect definition
514
515             if Token = Tok_Comma
516               or else Token = Tok_Semicolon
517               or else (not Semicolon and then Token /= Tok_Arrow)
518             then
519                if Aspect_Argument (A_Id) /= Optional then
520                   Error_Msg_Node_1 := Aspect;
521                   Error_Msg_AP ("aspect& requires an aspect definition");
522                   OK := False;
523                end if;
524
525             --  Here we have an aspect definition
526
527             else
528                if Token = Tok_Arrow then
529                   Scan; -- past arrow
530                else
531                   T_Arrow;
532                   OK := False;
533                end if;
534
535                if Aspect_Argument (A_Id) = Name then
536                   Set_Expression (Aspect, P_Name);
537                else
538                   Set_Expression (Aspect, P_Expression);
539                end if;
540             end if;
541
542             --  If OK clause scanned, add it to the list
543
544             if OK then
545                Append (Aspect, Aspects);
546             end if;
547
548             if Token = Tok_Comma then
549                Scan; -- past comma
550
551             --  Must be terminator character
552
553             else
554                if Semicolon then
555                   T_Semicolon;
556                end if;
557
558                exit;
559             end if;
560          end if;
561       end loop;
562
563       --  Here if aspects present
564
565       if Is_Non_Empty_List (Aspects) then
566
567          --  If Decl is Empty, we just ignore the aspects (the caller in this
568          --  case has always issued an appropriate error message).
569
570          if Decl = Empty then
571             null;
572
573          --  If Decl is Error, we ignore the aspects, and issue a message
574
575          elsif Decl = Error then
576             Error_Msg ("aspect specifications not allowed here", Ptr);
577
578          --  Here aspects are allowed, and we store them
579
580          else
581             Set_Parent (Aspects, Decl);
582             Set_Aspect_Specifications (Decl, Aspects);
583          end if;
584       end if;
585    end P_Aspect_Specifications;
586
587    ---------------------------------------------
588    -- 13.4  Enumeration Representation Clause --
589    ---------------------------------------------
590
591    --  Parsed by P_Representation_Clause (13.1)
592
593    ---------------------------------
594    -- 13.4  Enumeration Aggregate --
595    ---------------------------------
596
597    --  Parsed by P_Representation_Clause (13.1)
598
599    ------------------------------------------
600    -- 13.5.1  Record Representation Clause --
601    ------------------------------------------
602
603    --  Parsed by P_Representation_Clause (13.1)
604
605    ------------------------------
606    -- 13.5.1  Mod Clause (I.8) --
607    ------------------------------
608
609    --  MOD_CLAUSE ::= at mod static_EXPRESSION;
610
611    --  Note: in Ada 83, the expression must be a simple expression
612
613    --  The caller has checked that the initial Token is AT
614
615    --  Error recovery: cannot raise Error_Resync
616
617    --  Note: the caller is responsible for setting the Pragmas_Before field
618
619    function P_Mod_Clause return Node_Id is
620       Mod_Node  : Node_Id;
621       Expr_Node : Node_Id;
622
623    begin
624       Mod_Node := New_Node (N_Mod_Clause, Token_Ptr);
625       Scan; -- past AT
626       T_Mod;
627       Expr_Node := P_Expression_No_Right_Paren;
628       Check_Simple_Expression_In_Ada_83 (Expr_Node);
629       Set_Expression (Mod_Node, Expr_Node);
630       TF_Semicolon;
631       return Mod_Node;
632    end P_Mod_Clause;
633
634    ------------------------------
635    -- 13.5.1  Component Clause --
636    ------------------------------
637
638    --  COMPONENT_CLAUSE ::=
639    --    COMPONENT_CLAUSE_COMPONENT_NAME at POSITION
640    --      range FIRST_BIT .. LAST_BIT;
641
642    --  COMPONENT_CLAUSE_COMPONENT_NAME ::=
643    --    component_DIRECT_NAME
644    --  | component_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
645    --  | FIRST_SUBTYPE_DIRECT_NAME'ATTRIBUTE_DESIGNATOR
646
647    --  POSITION ::= static_EXPRESSION
648
649    --  Note: in Ada 83, the expression must be a simple expression
650
651    --  FIRST_BIT ::= static_SIMPLE_EXPRESSION
652    --  LAST_BIT ::= static_SIMPLE_EXPRESSION
653
654    --  Note: the AARM V2.0 grammar has an error at this point, it uses
655    --  EXPRESSION instead of SIMPLE_EXPRESSION for FIRST_BIT and LAST_BIT
656
657    --  Error recovery: cannot raise Error_Resync
658
659    function P_Component_Clause return Node_Id is
660       Component_Node : Node_Id;
661       Comp_Name      : Node_Id;
662       Expr_Node      : Node_Id;
663
664    begin
665       Component_Node := New_Node (N_Component_Clause, Token_Ptr);
666       Comp_Name := P_Name;
667
668       if Nkind (Comp_Name) = N_Identifier
669         or else Nkind (Comp_Name) = N_Attribute_Reference
670       then
671          Set_Component_Name (Component_Node, Comp_Name);
672       else
673          Error_Msg_N
674            ("component name must be direct name or attribute", Comp_Name);
675          Set_Component_Name (Component_Node, Error);
676       end if;
677
678       Set_Sloc (Component_Node, Token_Ptr);
679       T_At;
680       Expr_Node := P_Expression_No_Right_Paren;
681       Check_Simple_Expression_In_Ada_83 (Expr_Node);
682       Set_Position (Component_Node, Expr_Node);
683       T_Range;
684       Expr_Node := P_Expression_No_Right_Paren;
685       Check_Simple_Expression_In_Ada_83 (Expr_Node);
686       Set_First_Bit (Component_Node, Expr_Node);
687       T_Dot_Dot;
688       Expr_Node := P_Expression_No_Right_Paren;
689       Check_Simple_Expression_In_Ada_83 (Expr_Node);
690       Set_Last_Bit (Component_Node, Expr_Node);
691       TF_Semicolon;
692       return Component_Node;
693    end P_Component_Clause;
694
695    ----------------------
696    -- 13.5.1  Position --
697    ----------------------
698
699    --  Parsed by P_Component_Clause (13.5.1)
700
701    -----------------------
702    -- 13.5.1  First Bit --
703    -----------------------
704
705    --  Parsed by P_Component_Clause (13.5.1)
706
707    ----------------------
708    -- 13.5.1  Last Bit --
709    ----------------------
710
711    --  Parsed by P_Component_Clause (13.5.1)
712
713    --------------------------
714    -- 13.8  Code Statement --
715    --------------------------
716
717    --  CODE_STATEMENT ::= QUALIFIED_EXPRESSION
718
719    --  On entry the caller has scanned the SUBTYPE_MARK (passed in as the
720    --  single argument, and the scan points to the apostrophe.
721
722    --  Error recovery: can raise Error_Resync
723
724    function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id is
725       Node1 : Node_Id;
726
727    begin
728       Scan; -- past apostrophe
729
730       --  If left paren, then we have a possible code statement
731
732       if Token = Tok_Left_Paren then
733          Node1 := New_Node (N_Code_Statement, Sloc (Subtype_Mark));
734          Set_Expression (Node1, P_Qualified_Expression (Subtype_Mark));
735          TF_Semicolon;
736          return Node1;
737
738       --  Otherwise we have an illegal range attribute. Note that P_Name
739       --  ensures that Token = Tok_Range is the only possibility left here.
740
741       else -- Token = Tok_Range
742          Error_Msg_SC ("RANGE attribute illegal here!");
743          raise Error_Resync;
744       end if;
745
746    end P_Code_Statement;
747
748 end Ch13;