Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / par-ch2.adb
blob224c63b7eb9b2c3d847a9a190f805c058a2a621a
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-2013, 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 ------------------------------------------------------------------------------
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
30 separate (Par)
31 package body Ch2 is
33 -- Local functions, used only in this chapter
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.
44 -- Note: We allow attribute forms Pre'Class, Post'Class, Invariant'Class,
45 -- Type_Invariant'Class in place of a pragma argument identifier. Rather
46 -- than handle this case specially, we replace such references with
47 -- one of the special internal identifiers _Pre, _Post, _Invariant, or
48 -- _Type_Invariant, and this procedure is where this replacement occurs.
50 ---------------------
51 -- 2.3 Identifier --
52 ---------------------
54 -- IDENTIFIER ::= LETTER {[UNDERLINE] LETTER_OR_DIGIT}
56 -- LETTER_OR_DIGIT ::= IDENTIFIER_LETTER | DIGIT
58 -- An IDENTIFIER shall not be a reserved word
60 -- Error recovery: can raise Error_Resync (cannot return Error)
62 function P_Identifier (C : Id_Check := None) return Node_Id is
63 Ident_Node : Node_Id;
65 begin
66 -- All set if we do indeed have an identifier
68 -- Code duplication, see Par_Ch3.P_Defining_Identifier???
70 if Token = Tok_Identifier then
71 Check_Future_Keyword;
72 Ident_Node := Token_Node;
73 Scan; -- past Identifier
74 return Ident_Node;
76 -- If we have a reserved identifier, manufacture an identifier with
77 -- a corresponding name after posting an appropriate error message
79 elsif Is_Reserved_Identifier (C) then
80 Scan_Reserved_Identifier (Force_Msg => False);
81 Ident_Node := Token_Node;
82 Scan; -- past the node
83 return Ident_Node;
85 -- Otherwise we have junk that cannot be interpreted as an identifier
87 else
88 T_Identifier; -- to give message
89 raise Error_Resync;
90 end if;
91 end P_Identifier;
93 --------------------------
94 -- 2.3 Letter Or Digit --
95 --------------------------
97 -- Parsed by P_Identifier (2.3)
99 --------------------------
100 -- 2.4 Numeric Literal --
101 --------------------------
103 -- NUMERIC_LITERAL ::= DECIMAL_LITERAL | BASED_LITERAL
105 -- Numeric literal is returned by the scanner as either
106 -- Tok_Integer_Literal or Tok_Real_Literal
108 ----------------------------
109 -- 2.4.1 Decimal Literal --
110 ----------------------------
112 -- DECIMAL_LITERAL ::= NUMERAL [.NUMERAL] [EXPONENT]
114 -- Handled by scanner as part of numeric literal handing (see 2.4)
116 --------------------
117 -- 2.4.1 Numeral --
118 --------------------
120 -- NUMERAL ::= DIGIT {[UNDERLINE] DIGIT}
122 -- Handled by scanner as part of numeric literal handling (see 2.4)
124 ---------------------
125 -- 2.4.1 Exponent --
126 ---------------------
128 -- EXPONENT ::= E [+] NUMERAL | E - NUMERAL
130 -- Handled by scanner as part of numeric literal handling (see 2.4)
132 --------------------------
133 -- 2.4.2 Based Literal --
134 --------------------------
136 -- BASED_LITERAL ::=
137 -- BASE # BASED_NUMERAL [.BASED_NUMERAL] # [EXPONENT]
139 -- Handled by scanner as part of numeric literal handling (see 2.4)
141 -----------------
142 -- 2.4.2 Base --
143 -----------------
145 -- BASE ::= NUMERAL
147 -- Handled by scanner as part of numeric literal handling (see 2.4)
149 --------------------------
150 -- 2.4.2 Based Numeral --
151 --------------------------
153 -- BASED_NUMERAL ::=
154 -- EXTENDED_DIGIT {[UNDERLINE] EXTENDED_DIGIT}
156 -- Handled by scanner as part of numeric literal handling (see 2.4)
158 ---------------------------
159 -- 2.4.2 Extended Digit --
160 ---------------------------
162 -- EXTENDED_DIGIT ::= DIGIT | A | B | C | D | E | F
164 -- Handled by scanner as part of numeric literal handling (see 2.4)
166 ----------------------------
167 -- 2.5 Character Literal --
168 ----------------------------
170 -- CHARACTER_LITERAL ::= ' GRAPHIC_CHARACTER '
172 -- Handled by the scanner and returned as Tok_Char_Literal
174 -------------------------
175 -- 2.6 String Literal --
176 -------------------------
178 -- STRING LITERAL ::= "{STRING_ELEMENT}"
180 -- Handled by the scanner and returned as Tok_String_Literal
181 -- or if the string looks like an operator as Tok_Operator_Symbol.
183 -------------------------
184 -- 2.6 String Element --
185 -------------------------
187 -- STRING_ELEMENT ::= "" | non-quotation_mark_GRAPHIC_CHARACTER
189 -- A STRING_ELEMENT is either a pair of quotation marks ("),
190 -- or a single GRAPHIC_CHARACTER other than a quotation mark.
192 -- Handled by scanner as part of string literal handling (see 2.4)
194 ------------------
195 -- 2.7 Comment --
196 ------------------
198 -- A COMMENT starts with two adjacent hyphens and extends up to the
199 -- end of the line. A COMMENT may appear on any line of a program.
201 -- Handled by the scanner which simply skips past encountered comments
203 -----------------
204 -- 2.8 Pragma --
205 -----------------
207 -- PRAGMA ::= pragma IDENTIFIER
208 -- [(PRAGMA_ARGUMENT_ASSOCIATION {, PRAGMA_ARGUMENT_ASSOCIATION})];
210 -- The caller has checked that the initial token is PRAGMA
212 -- Error recovery: cannot raise Error_Resync
214 -- One special piece of processing is needed in this routine. As described
215 -- in the section on "Handling semicolon used in place of IS" in module
216 -- Parse, the parser detects the case of missing subprogram bodies to
217 -- allow recovery from this syntactic error. Pragma INTERFACE (and, for
218 -- Ada 95, pragma IMPORT) can appear in place of the body. The parser must
219 -- recognize the use of these two pragmas in this context, otherwise it
220 -- will think there are missing bodies, and try to change ; to IS, when
221 -- in fact the bodies ARE present, supplied by these pragmas.
223 function P_Pragma (Skipping : Boolean := False) return Node_Id is
224 Interface_Check_Required : Boolean := False;
225 -- Set True if check of pragma INTERFACE is required
227 Import_Check_Required : Boolean := False;
228 -- Set True if check of pragma IMPORT is required
230 Arg_Count : Int := 0;
231 -- Number of argument associations processed
233 Identifier_Seen : Boolean := False;
234 -- Set True if an identifier is encountered for a pragma argument. Used
235 -- to check that there are no more arguments without identifiers.
237 Prag_Node : Node_Id;
238 Prag_Name : Name_Id;
239 Semicolon_Loc : Source_Ptr;
240 Ident_Node : Node_Id;
241 Assoc_Node : Node_Id;
242 Result : Node_Id;
244 procedure Skip_Pragma_Semicolon;
245 -- Skip past semicolon at end of pragma
247 ---------------------------
248 -- Skip_Pragma_Semicolon --
249 ---------------------------
251 procedure Skip_Pragma_Semicolon is
252 begin
253 if Token /= Tok_Semicolon then
255 -- If skipping the pragma, ignore a missing semicolon
257 if Skipping then
258 null;
260 -- Otherwise demand a semicolon
262 else
263 T_Semicolon;
264 end if;
266 -- Scan past semicolon if present
268 else
269 Scan;
270 end if;
271 end Skip_Pragma_Semicolon;
273 -- Start of processing for P_Pragma
275 begin
276 Prag_Node := New_Node (N_Pragma, Token_Ptr);
277 Scan; -- past PRAGMA
278 Prag_Name := Token_Name;
280 if Style_Check then
281 Style.Check_Pragma_Name;
282 end if;
284 -- Ada 2005 (AI-284): INTERFACE is a new reserved word but it is
285 -- allowed as a pragma name.
287 if Ada_Version >= Ada_2005
288 and then Token = Tok_Interface
289 then
290 Prag_Name := Name_Interface;
291 Ident_Node := Make_Identifier (Token_Ptr, Name_Interface);
292 Scan; -- past INTERFACE
293 else
294 Ident_Node := P_Identifier;
295 end if;
297 Set_Pragma_Identifier (Prag_Node, Ident_Node);
299 -- See if special INTERFACE/IMPORT check is required
301 if SIS_Entry_Active then
302 Interface_Check_Required := (Prag_Name = Name_Interface);
303 Import_Check_Required := (Prag_Name = Name_Import);
304 else
305 Interface_Check_Required := False;
306 Import_Check_Required := False;
307 end if;
309 -- Scan arguments. We assume that arguments are present if there is
310 -- a left paren, or if a semicolon is missing and there is another
311 -- token on the same line as the pragma name.
313 if Token = Tok_Left_Paren
314 or else (Token /= Tok_Semicolon
315 and then not Token_Is_At_Start_Of_Line)
316 then
317 Set_Pragma_Argument_Associations (Prag_Node, New_List);
318 T_Left_Paren;
320 loop
321 Arg_Count := Arg_Count + 1;
322 Scan_Pragma_Argument_Association (Identifier_Seen, Assoc_Node);
324 if Arg_Count = 2
325 and then (Interface_Check_Required or else Import_Check_Required)
326 then
327 -- Here is where we cancel the SIS active status if this pragma
328 -- supplies a body for the currently active subprogram spec.
330 if Nkind (Expression (Assoc_Node)) in N_Direct_Name
331 and then Chars (Expression (Assoc_Node)) = Chars (SIS_Labl)
332 then
333 SIS_Entry_Active := False;
334 end if;
335 end if;
337 Append (Assoc_Node, Pragma_Argument_Associations (Prag_Node));
338 exit when Token /= Tok_Comma;
339 Scan; -- past comma
340 end loop;
342 -- If we have := for pragma Debug, it is worth special casing the
343 -- error message (it is easy to think of pragma Debug as taking a
344 -- statement, and an assignment statement is the most likely
345 -- candidate for this error)
347 if Token = Tok_Colon_Equal and then Prag_Name = Name_Debug then
348 Error_Msg_SC ("argument for pragma Debug must be procedure call");
349 Resync_To_Semicolon;
351 -- Normal case, we expect a right paren here
353 else
354 T_Right_Paren;
355 end if;
356 end if;
358 Semicolon_Loc := Token_Ptr;
360 -- Now we have two tasks left, we need to scan out the semicolon
361 -- following the pragma, and we have to call Par.Prag to process
362 -- the pragma. Normally we do them in this order, however, there
363 -- is one exception namely pragma Style_Checks where we like to
364 -- skip the semicolon after processing the pragma, since that way
365 -- the style checks for the scanning of the semicolon follow the
366 -- settings of the pragma.
368 -- You might think we could just unconditionally do things in
369 -- the opposite order, but there are other pragmas, notably the
370 -- case of pragma Source_File_Name, which assume the semicolon
371 -- is already scanned out.
373 if Prag_Name = Name_Style_Checks then
374 Result := Par.Prag (Prag_Node, Semicolon_Loc);
375 Skip_Pragma_Semicolon;
376 return Result;
377 else
378 Skip_Pragma_Semicolon;
379 return Par.Prag (Prag_Node, Semicolon_Loc);
380 end if;
382 exception
383 when Error_Resync =>
384 Resync_Past_Semicolon;
385 return Error;
387 end P_Pragma;
389 -- This routine is called if a pragma is encountered in an inappropriate
390 -- position, the pragma is scanned out and control returns to continue.
392 -- The caller has checked that the initial token is pragma
394 -- Error recovery: cannot raise Error_Resync
396 procedure P_Pragmas_Misplaced is
397 begin
398 while Token = Tok_Pragma loop
399 Error_Msg_SC ("pragma not allowed here");
400 Discard_Junk_Node (P_Pragma (Skipping => True));
401 end loop;
402 end P_Pragmas_Misplaced;
404 -- This function is called to scan out an optional sequence of pragmas.
405 -- If no pragmas are found, then No_List is returned.
407 -- Error recovery: Cannot raise Error_Resync
409 function P_Pragmas_Opt return List_Id is
410 L : List_Id;
412 begin
413 if Token = Tok_Pragma then
414 L := New_List;
415 P_Pragmas_Opt (L);
416 return L;
418 else
419 return No_List;
420 end if;
421 end P_Pragmas_Opt;
423 -- This procedure is called to scan out an optional sequence of pragmas.
424 -- Any pragmas found are appended to the list provided as an argument.
426 -- Error recovery: Cannot raise Error_Resync
428 procedure P_Pragmas_Opt (List : List_Id) is
429 P : Node_Id;
431 begin
432 while Token = Tok_Pragma loop
433 P := P_Pragma;
435 if Nkind (P) /= N_Error
436 and then Nam_In (Pragma_Name (P), Name_Assert, Name_Debug)
437 then
438 Error_Msg_Name_1 := Pragma_Name (P);
439 Error_Msg_N
440 ("pragma% must be in declaration/statement context", P);
441 else
442 Append (P, List);
443 end if;
444 end loop;
445 end P_Pragmas_Opt;
447 --------------------------------------
448 -- 2.8 Pragma_Argument Association --
449 --------------------------------------
451 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
452 -- [pragma_argument_IDENTIFIER =>] NAME
453 -- | [pragma_argument_IDENTIFIER =>] EXPRESSION
455 -- In Ada 2012, there are two more possibilities:
457 -- PRAGMA_ARGUMENT_ASSOCIATION ::=
458 -- [pragma_argument_ASPECT_MARK =>] NAME
459 -- | [pragma_argument_ASPECT_MARK =>] EXPRESSION
461 -- where the interesting allowed cases (which do not fit the syntax of the
462 -- first alternative above) are
464 -- ASPECT_MARK ::=
465 -- Pre'Class | Post'Class | Invariant'Class | Type_Invariant'Class
467 -- We allow this special usage in all Ada modes, but it would be a pain to
468 -- allow these aspects to pervade the pragma syntax, and the representation
469 -- of pragma nodes internally. So what we do is to replace these
470 -- ASPECT_MARK forms with identifiers whose name is one of the special
471 -- internal names _Pre, _Post, _Invariant, or _Type_Invariant.
473 -- Error recovery: cannot raise Error_Resync
475 procedure Scan_Pragma_Argument_Association
476 (Identifier_Seen : in out Boolean;
477 Association : out Node_Id)
479 Scan_State : Saved_Scan_State;
480 Identifier_Node : Node_Id;
481 Id_Present : Boolean;
483 begin
484 Association := New_Node (N_Pragma_Argument_Association, Token_Ptr);
485 Set_Chars (Association, No_Name);
486 Id_Present := False;
488 -- Argument starts with identifier
490 if Token = Tok_Identifier then
491 Identifier_Node := Token_Node;
492 Save_Scan_State (Scan_State); -- at Identifier
493 Scan; -- past Identifier
495 if Token = Tok_Arrow then
496 Scan; -- past arrow
497 Id_Present := True;
499 -- Case of one of the special aspect forms
501 elsif Token = Tok_Apostrophe then
502 Scan; -- past apostrophe
504 -- We have apostrophe, so check for identifier'Class
506 if Token /= Tok_Identifier or else Token_Name /= Name_Class then
507 null;
509 -- We have identifier'Class, check for arrow
511 else
512 Scan; -- Past Class
514 if Token /= Tok_Arrow then
515 null;
517 -- Here we have scanned identifier'Class =>
519 else
520 Id_Present := True;
521 Scan; -- past arrow
523 case Chars (Identifier_Node) is
524 when Name_Pre =>
525 Set_Chars (Identifier_Node, Name_uPre);
527 when Name_Post =>
528 Set_Chars (Identifier_Node, Name_uPost);
530 when Name_Type_Invariant =>
531 Set_Chars (Identifier_Node, Name_uType_Invariant);
533 when Name_Invariant =>
534 Set_Chars (Identifier_Node, Name_uInvariant);
536 -- If it is X'Class => for some invalid X, we will give
537 -- an error, and forget that 'Class was present, which
538 -- will give better error recovery. We could do a spell
539 -- check here, but it seems too much work.
541 when others =>
542 Error_Msg_SC ("invalid aspect id for pragma");
543 end case;
544 end if;
545 end if;
546 end if;
548 -- Identifier was present
550 if Id_Present then
551 Set_Chars (Association, Chars (Identifier_Node));
552 Identifier_Seen := True;
554 -- Identifier not present after all
556 else
557 Restore_Scan_State (Scan_State); -- to Identifier
558 end if;
559 end if;
561 -- Diagnose error of "positional" argument for pragma appearing after
562 -- a "named" argument (quotes here are because that's not quite accurate
563 -- Ada RM terminology).
565 -- Since older GNAT versions did not generate this error, disable this
566 -- message in Relaxed_RM_Semantics mode to help legacy code using e.g.
567 -- codepeer.
569 if Identifier_Seen and not Id_Present and not Relaxed_RM_Semantics then
570 Error_Msg_SC ("|pragma argument identifier required here");
571 Error_Msg_SC ("\since previous argument had identifier (RM 2.8(4))");
572 end if;
574 if Id_Present then
575 Set_Expression (Association, P_Expression);
576 else
577 Set_Expression (Association, P_Expression_If_OK);
578 end if;
579 end Scan_Pragma_Argument_Association;
581 end Ch2;