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