OSDN Git Service

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