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