OSDN Git Service

2011-09-06 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch2.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 2                               --
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 Ch2 is
32
33    --  Local functions, used only in this chapter
34
35    procedure Scan_Pragma_Argument_Association
36      (Identifier_Seen : in out Boolean;
37       Association     : out Node_Id);
38    --  Scans out a pragma argument association. Identifier_Seen is true on
39    --  entry if a previous association had an identifier, and gets set True if
40    --  the scanned association has an identifier (this is used to check the
41    --  rule that no associations without identifiers can follow an association
42    --  which has an identifier). The result is returned in Association.
43
44    ---------------------
45    -- 2.3  Identifier --
46    ---------------------
47
48    --  IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT}
49
50    --  LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT
51
52    --  An IDENTIFIER shall not be a reserved word
53
54    --  Error recovery: can raise Error_Resync (cannot return Error)
55
56    function P_Identifier (C : Id_Check := None) return Node_Id is
57       Ident_Node : Node_Id;
58
59    begin
60       --  All set if we do indeed have an identifier
61
62       if Token = Tok_Identifier then
63
64          --  Ada 2005 (AI-284): Compiling in Ada95 mode we warn that INTERFACE,
65          --  OVERRIDING, and SYNCHRONIZED are new reserved words.
66
67          if Ada_Version = Ada_95
68            and then Warn_On_Ada_2005_Compatibility
69          then
70             if Token_Name = Name_Overriding
71               or else Token_Name = Name_Synchronized
72               or else (Token_Name = Name_Interface
73                         and then Prev_Token /= Tok_Pragma)
74             then
75                Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
76             end if;
77          end if;
78
79          --  Similarly, warn about Ada 2012 reserved words
80
81          if Ada_Version in Ada_95 .. Ada_2005
82            and then Warn_On_Ada_2012_Compatibility
83          then
84             if Token_Name = Name_Some then
85                Error_Msg_N ("& is a reserved word in Ada 2012?", Token_Node);
86             end if;
87          end if;
88
89          Ident_Node := Token_Node;
90          Scan; -- past Identifier
91          return Ident_Node;
92
93       --  If we have a reserved identifier, manufacture an identifier with
94       --  a corresponding name after posting an appropriate error message
95
96       elsif Is_Reserved_Identifier (C) then
97          Scan_Reserved_Identifier (Force_Msg => False);
98          Ident_Node := Token_Node;
99          Scan; -- past the node
100          return Ident_Node;
101
102       --  Otherwise we have junk that cannot be interpreted as an identifier
103
104       else
105          T_Identifier; -- to give message
106          raise Error_Resync;
107       end if;
108    end P_Identifier;
109
110    --------------------------
111    -- 2.3  Letter Or Digit --
112    --------------------------
113
114    --  Parsed by P_Identifier (2.3)
115
116    --------------------------
117    -- 2.4  Numeric Literal --
118    --------------------------
119
120    --  NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL
121
122    --  Numeric literal is returned by the scanner as either
123    --  Tok_Integer_Literal or Tok_Real_Literal
124
125    ----------------------------
126    -- 2.4.1  Decimal Literal --
127    ----------------------------
128
129    --  DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT]
130
131    --  Handled by scanner as part of numeric literal handing (see 2.4)
132
133    --------------------
134    -- 2.4.1  Numeral --
135    --------------------
136
137    --  NUMERAL ::= DIGIT {[UNDERLINE] DIGIT}
138
139    --  Handled by scanner as part of numeric literal handling (see 2.4)
140
141    ---------------------
142    -- 2.4.1  Exponent --
143    ---------------------
144
145    --  EXPONENT ::= E [+] NUMERAL | E - NUMERAL
146
147    --  Handled by scanner as part of numeric literal handling (see 2.4)
148
149    --------------------------
150    -- 2.4.2  Based Literal --
151    --------------------------
152
153    --  BASED_LITERAL ::=
154    --   BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT]
155
156    --  Handled by scanner as part of numeric literal handling (see 2.4)
157
158    -----------------
159    -- 2.4.2  Base --
160    -----------------
161
162    --  BASE ::= NUMERAL
163
164    --  Handled by scanner as part of numeric literal handling (see 2.4)
165
166    --------------------------
167    -- 2.4.2  Based Numeral --
168    --------------------------
169
170    --  BASED_NUMERAL ::=
171    --    EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT}
172
173    --  Handled by scanner as part of numeric literal handling (see 2.4)
174
175    ---------------------------
176    -- 2.4.2  Extended Digit --
177    ---------------------------
178
179    --  EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F
180
181    --  Handled by scanner as part of numeric literal handling (see 2.4)
182
183    ----------------------------
184    -- 2.5  Character Literal --
185    ----------------------------
186
187    --  CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER '
188
189    --  Handled by the scanner and returned as Tok_Char_Literal
190
191    -------------------------
192    -- 2.6  String Literal --
193    -------------------------
194
195    --  STRING LITERAL ::= "{STRING_ELEMENT}"
196
197    --  Handled by the scanner and returned as Tok_String_Literal
198    --  or if the string looks like an operator as Tok_Operator_Symbol.
199
200    -------------------------
201    -- 2.6  String Element --
202    -------------------------
203
204    --  STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER
205
206    --  A STRING_ELEMENT is either a pair of quotation marks ("),
207    --  or a single GRAPHIC_CHARACTER other than a quotation mark.
208
209    --  Handled by scanner as part of string literal handling (see 2.4)
210
211    ------------------
212    -- 2.7  Comment --
213    ------------------
214
215    --  A COMMENT starts with two adjacent hyphens and extends up to the
216    --  end of the line. A COMMENT may appear on any line of a program.
217
218    --  Handled by the scanner which simply skips past encountered comments
219
220    -----------------
221    -- 2.8  Pragma --
222    -----------------
223
224    --  PRAGMA ::= pragma IDENTIFIER
225    --    [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
226
227    --  The caller has checked that the initial token is PRAGMA
228
229    --  Error recovery: cannot raise Error_Resync
230
231    --  One special piece of processing is needed in this routine. As described
232    --  in the section on "Handling semicolon used in place of IS" in module
233    --  Parse, the parser detects the case of missing subprogram bodies to
234    --  allow recovery from this syntactic error. Pragma INTERFACE (and, for
235    --  Ada 95, pragma IMPORT) can appear in place of the body. The parser must
236    --  recognize the use of these two pragmas in this context, otherwise it
237    --  will think there are missing bodies, and try to change ; to IS, when
238    --  in fact the bodies ARE present, supplied by these pragmas.
239
240    function P_Pragma (Skipping : Boolean := False) return Node_Id is
241       Interface_Check_Required : Boolean := False;
242       --  Set True if check of pragma INTERFACE is required
243
244       Import_Check_Required : Boolean := False;
245       --  Set True if check of pragma IMPORT is required
246
247       Arg_Count : Int := 0;
248       --  Number of argument associations processed
249
250       Identifier_Seen : Boolean := False;
251       --  Set True if an identifier is encountered for a pragma argument. Used
252       --  to check that there are no more arguments without identifiers.
253
254       Prag_Node     : Node_Id;
255       Prag_Name     : Name_Id;
256       Semicolon_Loc : Source_Ptr;
257       Ident_Node    : Node_Id;
258       Assoc_Node    : Node_Id;
259       Result        : Node_Id;
260
261       procedure Skip_Pragma_Semicolon;
262       --  Skip past semicolon at end of pragma
263
264       ---------------------------
265       -- Skip_Pragma_Semicolon --
266       ---------------------------
267
268       procedure Skip_Pragma_Semicolon is
269       begin
270          if Token /= Tok_Semicolon then
271
272             --  If skipping the pragma, ignore a missing semicolon
273
274             if Skipping then
275                null;
276
277             --  Otherwise demand a semicolon
278
279             else
280                T_Semicolon;
281             end if;
282
283          --  Scan past semicolon if present
284
285          else
286             Scan;
287          end if;
288       end Skip_Pragma_Semicolon;
289
290    --  Start of processing for P_Pragma
291
292    begin
293       Prag_Node := New_Node (N_Pragma, Token_Ptr);
294       Scan; -- past PRAGMA
295       Prag_Name := Token_Name;
296
297       if Style_Check then
298          Style.Check_Pragma_Name;
299       end if;
300
301       --  Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
302       --  allowed as a pragma name.
303
304       if Ada_Version >= Ada_2005
305         and then Token = Tok_Interface
306       then
307          Prag_Name := Name_Interface;
308          Ident_Node  := Make_Identifier (Token_Ptr, Name_Interface);
309          Scan; -- past INTERFACE
310       else
311          Ident_Node := P_Identifier;
312       end if;
313
314       Set_Pragma_Identifier (Prag_Node, Ident_Node);
315
316       --  See if special INTERFACE/IMPORT check is required
317
318       if SIS_Entry_Active then
319          Interface_Check_Required := (Prag_Name = Name_Interface);
320          Import_Check_Required    := (Prag_Name = Name_Import);
321       else
322          Interface_Check_Required := False;
323          Import_Check_Required    := False;
324       end if;
325
326       --  Scan arguments. We assume that arguments are present if there is
327       --  a left paren, or if a semicolon is missing and there is another
328       --  token on the same line as the pragma name.
329
330       if Token = Tok_Left_Paren
331         or else (Token /= Tok_Semicolon
332                    and then not Token_Is_At_Start_Of_Line)
333       then
334          Set_Pragma_Argument_Associations (Prag_Node, New_List);
335          T_Left_Paren;
336
337          loop
338             Arg_Count := Arg_Count + 1;
339             Scan_Pragma_Argument_Association (Identifier_Seen, Assoc_Node);
340
341             if Arg_Count = 2
342               and then (Interface_Check_Required or else Import_Check_Required)
343             then
344                --  Here is where we cancel the SIS active status if this pragma
345                --  supplies a body for the currently active subprogram spec.
346
347                if Nkind (Expression (Assoc_Node)) in N_Direct_Name
348                  and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
349                then
350                   SIS_Entry_Active := False;
351                end if;
352             end if;
353
354             Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node));
355             exit when Token /= Tok_Comma;
356             Scan; -- past comma
357          end loop;
358
359          --  If we have := for pragma Debug, it is worth special casing the
360          --  error message (it is easy to think of pragma Debug as taking a
361          --  statement, and an assignment statement is the most likely
362          --  candidate for this error)
363
364          if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then
365             Error_Msg_SC ("argument for pragma Debug must be procedure call");
366             Resync_To_Semicolon;
367
368          --  Normal case, we expect a right paren here
369
370          else
371             T_Right_Paren;
372          end if;
373       end if;
374
375       Semicolon_Loc := Token_Ptr;
376
377       --  Now we have two tasks left, we need to scan out the semicolon
378       --  following the pragma, and we have to call Par.Prag to process
379       --  the pragma. Normally we do them in this order, however, there
380       --  is one exception namely pragma Style_Checks where we like to
381       --  skip the semicolon after processing the pragma, since that way
382       --  the style checks for the scanning of the semicolon follow the
383       --  settings of the pragma.
384
385       --  You might think we could just unconditionally do things in
386       --  the opposite order, but there are other pragmas, notably the
387       --  case of pragma Source_File_Name, which assume the semicolon
388       --  is already scanned out.
389
390       if Prag_Name = Name_Style_Checks then
391          Result := Par.Prag (Prag_Node, Semicolon_Loc);
392          Skip_Pragma_Semicolon;
393          return Result;
394       else
395          Skip_Pragma_Semicolon;
396          return Par.Prag (Prag_Node, Semicolon_Loc);
397       end if;
398
399    exception
400       when Error_Resync =>
401          Resync_Past_Semicolon;
402          return Error;
403
404    end P_Pragma;
405
406    --  This routine is called if a pragma is encountered in an inappropriate
407    --  position, the pragma is scanned out and control returns to continue.
408
409    --  The caller has checked that the initial token is pragma
410
411    --  Error recovery: cannot raise Error_Resync
412
413    procedure P_Pragmas_Misplaced is
414    begin
415       while Token = Tok_Pragma loop
416          Error_Msg_SC ("pragma not allowed here");
417          Discard_Junk_Node (P_Pragma (Skipping => True));
418       end loop;
419    end P_Pragmas_Misplaced;
420
421    --  This function is called to scan out an optional sequence of pragmas.
422    --  If no pragmas are found, then No_List is returned.
423
424    --  Error recovery: Cannot raise Error_Resync
425
426    function P_Pragmas_Opt return List_Id is
427       L : List_Id;
428
429    begin
430       if Token = Tok_Pragma then
431          L := New_List;
432          P_Pragmas_Opt (L);
433          return L;
434
435       else
436          return No_List;
437       end if;
438    end P_Pragmas_Opt;
439
440    --  This procedure is called to scan out an optional sequence of pragmas.
441    --  Any pragmas found are appended to the list provided as an argument.
442
443    --  Error recovery: Cannot raise Error_Resync
444
445    procedure P_Pragmas_Opt (List : List_Id) is
446       P     : Node_Id;
447
448    begin
449       while Token = Tok_Pragma loop
450          P := P_Pragma;
451
452          if Nkind (P) /= N_Error
453           and then (Pragma_Name (P) = Name_Assert
454                       or else
455                     Pragma_Name (P) = Name_Debug)
456          then
457             Error_Msg_Name_1 := Pragma_Name (P);
458             Error_Msg_N
459               ("pragma% must be in declaration/statement context", P);
460          else
461             Append (P, List);
462          end if;
463       end loop;
464    end P_Pragmas_Opt;
465
466    --------------------------------------
467    -- 2.8  Pragma_Argument Association --
468    --------------------------------------
469
470    --  PRAGMA_ARGUMENT_ASSOCIATION ::=
471    --    [pragma_argument_IDENTIFIER =>] NAME
472    --  | [pragma_argument_IDENTIFIER =>] EXPRESSION
473
474    --  Error recovery: cannot raise Error_Resync
475
476    procedure Scan_Pragma_Argument_Association
477      (Identifier_Seen : in out Boolean;
478       Association     : out Node_Id)
479    is
480       Scan_State      : Saved_Scan_State;
481       Identifier_Node : Node_Id;
482       Id_Present      : Boolean;
483
484    begin
485       Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
486       Set_Chars (Association, No_Name);
487
488       --  Argument starts with identifier
489
490       if Token = Tok_Identifier then
491          Identifier_Node := Token_Node;
492          Save_Scan_State (Scan_State); -- at Identifier
493          Scan; -- past Identifier
494
495          if Token = Tok_Arrow then
496             Identifier_Seen := True;
497             Scan; -- past arrow
498             Set_Chars (Association, Chars (Identifier_Node));
499             Id_Present := True;
500
501          --  Case of argument with no identifier
502
503          else
504             Restore_Scan_State (Scan_State); -- to Identifier
505             Id_Present := False;
506          end if;
507
508       --  Argument does not start with identifier
509
510       else
511          Id_Present := False;
512       end if;
513
514       --  Diagnose error of "positional" argument for pragma appearing after
515       --  a "named" argument (quotes here are because that's not quite accurate
516       --  Ada RM terminology).
517
518       --  Since older GNAT versions did not generate this error, disable this
519       --  message in codepeer mode to help legacy code using codepeer.
520
521       if Identifier_Seen and not Id_Present and not CodePeer_Mode then
522          Error_Msg_SC ("|pragma argument identifier required here");
523          Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))");
524       end if;
525
526       if Id_Present then
527          Set_Expression (Association, P_Expression);
528       else
529          Set_Expression (Association, P_Expression_If_OK);
530       end if;
531    end Scan_Pragma_Argument_Association;
532
533 end Ch2;