2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / ada / par-prag.adb
blob93cbf94cadbe2ba3fe39f3f72b2747136bdc1c2c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . P R A G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, 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 -- Generally the parser checks the basic syntax of pragmas, but does not
27 -- do specialized syntax checks for individual pragmas, these are deferred
28 -- to semantic analysis time (see unit Sem_Prag). There are some pragmas
29 -- which require recognition and either partial or complete processing
30 -- during parsing, and this unit performs this required processing.
32 with Fname.UF; use Fname.UF;
33 with Osint; use Osint;
34 with Rident; use Rident;
35 with Restrict; use Restrict;
36 with Stringt; use Stringt;
37 with Stylesw; use Stylesw;
38 with Uintp; use Uintp;
39 with Uname; use Uname;
41 with System.WCh_Con; use System.WCh_Con;
43 separate (Par)
45 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id is
46 Prag_Name : constant Name_Id := Pragma_Name (Pragma_Node);
47 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Prag_Name);
48 Pragma_Sloc : constant Source_Ptr := Sloc (Pragma_Node);
49 Arg_Count : Nat;
50 Arg_Node : Node_Id;
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
56 procedure Add_List_Pragma_Entry (PT : List_Pragma_Type; Loc : Source_Ptr);
57 -- Make a new entry in the List_Pragmas table if this entry is not already
58 -- in the table (it will always be the last one if there is a duplication
59 -- resulting from the use of Save/Restore_Scan_State).
61 function Arg1 return Node_Id;
62 function Arg2 return Node_Id;
63 function Arg3 return Node_Id;
64 -- Obtain specified Pragma_Argument_Association. It is allowable to call
65 -- the routine for the argument one past the last present argument, but
66 -- that is the only case in which a non-present argument can be referenced.
68 procedure Check_Arg_Count (Required : Int);
69 -- Check argument count for pragma = Required. If not give error and raise
70 -- Error_Resync.
72 procedure Check_Arg_Is_String_Literal (Arg : Node_Id);
73 -- Check the expression of the specified argument to make sure that it
74 -- is a string literal. If not give error and raise Error_Resync.
76 procedure Check_Arg_Is_On_Or_Off (Arg : Node_Id);
77 -- Check the expression of the specified argument to make sure that it
78 -- is an identifier which is either ON or OFF, and if not, then issue
79 -- an error message and raise Error_Resync.
81 procedure Check_No_Identifier (Arg : Node_Id);
82 -- Checks that the given argument does not have an identifier. If
83 -- an identifier is present, then an error message is issued, and
84 -- Error_Resync is raised.
86 procedure Check_Optional_Identifier (Arg : Node_Id; Id : Name_Id);
87 -- Checks if the given argument has an identifier, and if so, requires
88 -- it to match the given identifier name. If there is a non-matching
89 -- identifier, then an error message is given and Error_Resync raised.
91 procedure Check_Required_Identifier (Arg : Node_Id; Id : Name_Id);
92 -- Same as Check_Optional_Identifier, except that the name is required
93 -- to be present and to match the given Id value.
95 procedure Process_Restrictions_Or_Restriction_Warnings;
96 -- Common processing for Restrictions and Restriction_Warnings pragmas.
97 -- For the most part, restrictions need not be processed at parse time,
98 -- since they only affect semantic processing. This routine handles the
99 -- exceptions as follows
101 -- No_Obsolescent_Features must be processed at parse time, since there
102 -- are some obsolescent features (e.g. character replacements) which are
103 -- handled at parse time.
105 -- SPARK must be processed at parse time, since this restriction controls
106 -- whether the scanner recognizes a spark HIDE directive formatted as an
107 -- Ada comment (and generates a Tok_SPARK_Hide token for the directive).
109 -- No_Dependence must be processed at parse time, since otherwise it gets
110 -- handled too late.
112 -- Note that we don't need to do full error checking for badly formed cases
113 -- of restrictions, since these will be caught during semantic analysis.
115 ---------------------------
116 -- Add_List_Pragma_Entry --
117 ---------------------------
119 procedure Add_List_Pragma_Entry (PT : List_Pragma_Type; Loc : Source_Ptr) is
120 begin
121 if List_Pragmas.Last < List_Pragmas.First
122 or else (List_Pragmas.Table (List_Pragmas.Last)) /= ((PT, Loc))
123 then
124 List_Pragmas.Append ((PT, Loc));
125 end if;
126 end Add_List_Pragma_Entry;
128 ----------
129 -- Arg1 --
130 ----------
132 function Arg1 return Node_Id is
133 begin
134 return First (Pragma_Argument_Associations (Pragma_Node));
135 end Arg1;
137 ----------
138 -- Arg2 --
139 ----------
141 function Arg2 return Node_Id is
142 begin
143 return Next (Arg1);
144 end Arg2;
146 ----------
147 -- Arg3 --
148 ----------
150 function Arg3 return Node_Id is
151 begin
152 return Next (Arg2);
153 end Arg3;
155 ---------------------
156 -- Check_Arg_Count --
157 ---------------------
159 procedure Check_Arg_Count (Required : Int) is
160 begin
161 if Arg_Count /= Required then
162 Error_Msg ("wrong number of arguments for pragma%", Pragma_Sloc);
163 raise Error_Resync;
164 end if;
165 end Check_Arg_Count;
167 ----------------------------
168 -- Check_Arg_Is_On_Or_Off --
169 ----------------------------
171 procedure Check_Arg_Is_On_Or_Off (Arg : Node_Id) is
172 Argx : constant Node_Id := Expression (Arg);
174 begin
175 if Nkind (Expression (Arg)) /= N_Identifier
176 or else not Nam_In (Chars (Argx), Name_On, Name_Off)
177 then
178 Error_Msg_Name_2 := Name_On;
179 Error_Msg_Name_3 := Name_Off;
181 Error_Msg ("argument for pragma% must be% or%", Sloc (Argx));
182 raise Error_Resync;
183 end if;
184 end Check_Arg_Is_On_Or_Off;
186 ---------------------------------
187 -- Check_Arg_Is_String_Literal --
188 ---------------------------------
190 procedure Check_Arg_Is_String_Literal (Arg : Node_Id) is
191 begin
192 if Nkind (Expression (Arg)) /= N_String_Literal then
193 Error_Msg
194 ("argument for pragma% must be string literal",
195 Sloc (Expression (Arg)));
196 raise Error_Resync;
197 end if;
198 end Check_Arg_Is_String_Literal;
200 -------------------------
201 -- Check_No_Identifier --
202 -------------------------
204 procedure Check_No_Identifier (Arg : Node_Id) is
205 begin
206 if Chars (Arg) /= No_Name then
207 Error_Msg_N ("pragma% does not permit named arguments", Arg);
208 raise Error_Resync;
209 end if;
210 end Check_No_Identifier;
212 -------------------------------
213 -- Check_Optional_Identifier --
214 -------------------------------
216 procedure Check_Optional_Identifier (Arg : Node_Id; Id : Name_Id) is
217 begin
218 if Present (Arg) and then Chars (Arg) /= No_Name then
219 if Chars (Arg) /= Id then
220 Error_Msg_Name_2 := Id;
221 Error_Msg_N ("pragma% argument expects identifier%", Arg);
222 end if;
223 end if;
224 end Check_Optional_Identifier;
226 -------------------------------
227 -- Check_Required_Identifier --
228 -------------------------------
230 procedure Check_Required_Identifier (Arg : Node_Id; Id : Name_Id) is
231 begin
232 if Chars (Arg) /= Id then
233 Error_Msg_Name_2 := Id;
234 Error_Msg_N ("pragma% argument must have identifier%", Arg);
235 end if;
236 end Check_Required_Identifier;
238 --------------------------------------------------
239 -- Process_Restrictions_Or_Restriction_Warnings --
240 --------------------------------------------------
242 procedure Process_Restrictions_Or_Restriction_Warnings is
243 Arg : Node_Id;
244 Id : Name_Id;
245 Expr : Node_Id;
247 begin
248 Arg := Arg1;
249 while Present (Arg) loop
250 Id := Chars (Arg);
251 Expr := Expression (Arg);
253 if Id = No_Name and then Nkind (Expr) = N_Identifier then
254 case Chars (Expr) is
255 when Name_No_Obsolescent_Features =>
256 Set_Restriction (No_Obsolescent_Features, Pragma_Node);
257 Restriction_Warnings (No_Obsolescent_Features) :=
258 Prag_Id = Pragma_Restriction_Warnings;
260 when Name_SPARK | Name_SPARK_05 =>
261 Set_Restriction (SPARK_05, Pragma_Node);
262 Restriction_Warnings (SPARK_05) :=
263 Prag_Id = Pragma_Restriction_Warnings;
265 when others =>
266 null;
267 end case;
269 elsif Id = Name_No_Dependence then
270 Set_Restriction_No_Dependence
271 (Unit => Expr,
272 Warn => Prag_Id = Pragma_Restriction_Warnings
273 or else Treat_Restrictions_As_Warnings);
274 end if;
276 Next (Arg);
277 end loop;
278 end Process_Restrictions_Or_Restriction_Warnings;
280 -- Start of processing for Prag
282 begin
283 Error_Msg_Name_1 := Prag_Name;
285 -- Ignore unrecognized pragma. We let Sem post the warning for this, since
286 -- it is a semantic error, not a syntactic one (we have already checked
287 -- the syntax for the unrecognized pragma as required by (RM 2.8(11)).
289 if Prag_Id = Unknown_Pragma then
290 return Pragma_Node;
291 end if;
293 -- Count number of arguments. This loop also checks if any of the arguments
294 -- are Error, indicating a syntax error as they were parsed. If so, we
295 -- simply return, because we get into trouble with cascaded errors if we
296 -- try to perform our error checks on junk arguments.
298 Arg_Count := 0;
300 if Present (Pragma_Argument_Associations (Pragma_Node)) then
301 Arg_Node := Arg1;
302 while Arg_Node /= Empty loop
303 Arg_Count := Arg_Count + 1;
305 if Expression (Arg_Node) = Error then
306 return Error;
307 end if;
309 Next (Arg_Node);
310 end loop;
311 end if;
313 -- Remaining processing is pragma dependent
315 case Prag_Id is
317 ------------
318 -- Ada_83 --
319 ------------
321 -- This pragma must be processed at parse time, since we want to set
322 -- the Ada version properly at parse time to recognize the appropriate
323 -- Ada version syntax.
325 when Pragma_Ada_83 =>
326 Ada_Version := Ada_83;
327 Ada_Version_Explicit := Ada_83;
328 Ada_Version_Pragma := Pragma_Node;
330 ------------
331 -- Ada_95 --
332 ------------
334 -- This pragma must be processed at parse time, since we want to set
335 -- the Ada version properly at parse time to recognize the appropriate
336 -- Ada version syntax.
338 when Pragma_Ada_95 =>
339 Ada_Version := Ada_95;
340 Ada_Version_Explicit := Ada_95;
341 Ada_Version_Pragma := Pragma_Node;
343 ---------------------
344 -- Ada_05/Ada_2005 --
345 ---------------------
347 -- These pragmas must be processed at parse time, since we want to set
348 -- the Ada version properly at parse time to recognize the appropriate
349 -- Ada version syntax. However, it is only the zero argument form that
350 -- must be processed at parse time.
352 when Pragma_Ada_05 | Pragma_Ada_2005 =>
353 if Arg_Count = 0 then
354 Ada_Version := Ada_2005;
355 Ada_Version_Explicit := Ada_2005;
356 Ada_Version_Pragma := Pragma_Node;
357 end if;
359 ---------------------
360 -- Ada_12/Ada_2012 --
361 ---------------------
363 -- These pragmas must be processed at parse time, since we want to set
364 -- the Ada version properly at parse time to recognize the appropriate
365 -- Ada version syntax. However, it is only the zero argument form that
366 -- must be processed at parse time.
368 when Pragma_Ada_12 | Pragma_Ada_2012 =>
369 if Arg_Count = 0 then
370 Ada_Version := Ada_2012;
371 Ada_Version_Explicit := Ada_2012;
372 Ada_Version_Pragma := Pragma_Node;
373 end if;
375 ---------------------------
376 -- Compiler_Unit_Warning --
377 ---------------------------
379 -- This pragma must be processed at parse time, since the resulting
380 -- status may be tested during the parsing of the program.
382 when Pragma_Compiler_Unit | Pragma_Compiler_Unit_Warning =>
383 Check_Arg_Count (0);
385 -- Only recognized in main unit
387 if Current_Source_Unit = Main_Unit then
388 Compiler_Unit := True;
389 end if;
391 -----------
392 -- Debug --
393 -----------
395 -- pragma Debug ([boolean_EXPRESSION,] PROCEDURE_CALL_STATEMENT);
397 when Pragma_Debug =>
398 Check_No_Identifier (Arg1);
400 if Arg_Count = 2 then
401 Check_No_Identifier (Arg2);
402 else
403 Check_Arg_Count (1);
404 end if;
406 -------------------------------
407 -- Extensions_Allowed (GNAT) --
408 -------------------------------
410 -- pragma Extensions_Allowed (Off | On)
412 -- The processing for pragma Extensions_Allowed must be done at
413 -- parse time, since extensions mode may affect what is accepted.
415 when Pragma_Extensions_Allowed =>
416 Check_Arg_Count (1);
417 Check_No_Identifier (Arg1);
418 Check_Arg_Is_On_Or_Off (Arg1);
420 if Chars (Expression (Arg1)) = Name_On then
421 Extensions_Allowed := True;
422 Ada_Version := Ada_2012;
423 else
424 Extensions_Allowed := False;
425 Ada_Version := Ada_Version_Explicit;
426 end if;
428 ----------------
429 -- List (2.8) --
430 ----------------
432 -- pragma List (Off | On)
434 -- The processing for pragma List must be done at parse time, since a
435 -- listing can be generated in parse only mode.
437 when Pragma_List =>
438 Check_Arg_Count (1);
439 Check_No_Identifier (Arg1);
440 Check_Arg_Is_On_Or_Off (Arg1);
442 -- We unconditionally make a List_On entry for the pragma, so that
443 -- in the List (Off) case, the pragma will print even in a region
444 -- of code with listing turned off (this is required).
446 Add_List_Pragma_Entry (List_On, Sloc (Pragma_Node));
448 -- Now generate the list off entry for pragma List (Off)
450 if Chars (Expression (Arg1)) = Name_Off then
451 Add_List_Pragma_Entry (List_Off, Semi);
452 end if;
454 ----------------
455 -- Page (2.8) --
456 ----------------
458 -- pragma Page;
460 -- Processing for this pragma must be done at parse time, since a
461 -- listing can be generated in parse only mode with semantics off.
463 when Pragma_Page =>
464 Check_Arg_Count (0);
465 Add_List_Pragma_Entry (Page, Semi);
467 ------------------
468 -- Restrictions --
469 ------------------
471 -- pragma Restrictions (RESTRICTION {, RESTRICTION});
473 -- RESTRICTION ::=
474 -- restriction_IDENTIFIER
475 -- | restriction_parameter_IDENTIFIER => EXPRESSION
477 -- We process the case of No_Obsolescent_Features, since this has
478 -- a syntactic effect that we need to detect at parse time (the use
479 -- of replacement characters such as colon for pound sign).
481 when Pragma_Restrictions =>
482 Process_Restrictions_Or_Restriction_Warnings;
484 --------------------------
485 -- Restriction_Warnings --
486 --------------------------
488 -- pragma Restriction_Warnings (RESTRICTION {, RESTRICTION});
490 -- RESTRICTION ::=
491 -- restriction_IDENTIFIER
492 -- | restriction_parameter_IDENTIFIER => EXPRESSION
494 -- See above comment for pragma Restrictions
496 when Pragma_Restriction_Warnings =>
497 Process_Restrictions_Or_Restriction_Warnings;
499 ----------------------------------------------------------
500 -- Source_File_Name and Source_File_Name_Project (GNAT) --
501 ----------------------------------------------------------
503 -- These two pragmas have the same syntax and semantics.
504 -- There are five forms of these pragmas:
506 -- pragma Source_File_Name[_Project] (
507 -- [UNIT_NAME =>] unit_NAME,
508 -- BODY_FILE_NAME => STRING_LITERAL
509 -- [, [INDEX =>] INTEGER_LITERAL]);
511 -- pragma Source_File_Name[_Project] (
512 -- [UNIT_NAME =>] unit_NAME,
513 -- SPEC_FILE_NAME => STRING_LITERAL
514 -- [, [INDEX =>] INTEGER_LITERAL]);
516 -- pragma Source_File_Name[_Project] (
517 -- BODY_FILE_NAME => STRING_LITERAL
518 -- [, DOT_REPLACEMENT => STRING_LITERAL]
519 -- [, CASING => CASING_SPEC]);
521 -- pragma Source_File_Name[_Project] (
522 -- SPEC_FILE_NAME => STRING_LITERAL
523 -- [, DOT_REPLACEMENT => STRING_LITERAL]
524 -- [, CASING => CASING_SPEC]);
526 -- pragma Source_File_Name[_Project] (
527 -- SUBUNIT_FILE_NAME => STRING_LITERAL
528 -- [, DOT_REPLACEMENT => STRING_LITERAL]
529 -- [, CASING => CASING_SPEC]);
531 -- CASING_SPEC ::= Uppercase | Lowercase | Mixedcase
533 -- Pragma Source_File_Name_Project (SFNP) is equivalent to pragma
534 -- Source_File_Name (SFN), however their usage is exclusive:
535 -- SFN can only be used when no project file is used, while
536 -- SFNP can only be used when a project file is used.
538 -- The Project Manager produces a configuration pragmas file that
539 -- is communicated to the compiler with -gnatec switch. This file
540 -- contains only SFNP pragmas (at least two for the default naming
541 -- scheme. As this configuration pragmas file is always the first
542 -- processed by the compiler, it prevents the use of pragmas SFN in
543 -- other config files when a project file is in use.
545 -- Note: we process this during parsing, since we need to have the
546 -- source file names set well before the semantic analysis starts,
547 -- since we load the spec and with'ed packages before analysis.
549 when Pragma_Source_File_Name | Pragma_Source_File_Name_Project =>
550 Source_File_Name : declare
551 Unam : Unit_Name_Type;
552 Expr1 : Node_Id;
553 Pat : String_Ptr;
554 Typ : Character;
555 Dot : String_Ptr;
556 Cas : Casing_Type;
557 Nast : Nat;
558 Expr : Node_Id;
559 Index : Nat;
561 function Get_Fname (Arg : Node_Id) return File_Name_Type;
562 -- Process file name from unit name form of pragma
564 function Get_String_Argument (Arg : Node_Id) return String_Ptr;
565 -- Process string literal value from argument
567 procedure Process_Casing (Arg : Node_Id);
568 -- Process Casing argument of pattern form of pragma
570 procedure Process_Dot_Replacement (Arg : Node_Id);
571 -- Process Dot_Replacement argument of pattern form of pragma
573 ---------------
574 -- Get_Fname --
575 ---------------
577 function Get_Fname (Arg : Node_Id) return File_Name_Type is
578 begin
579 String_To_Name_Buffer (Strval (Expression (Arg)));
581 for J in 1 .. Name_Len loop
582 if Is_Directory_Separator (Name_Buffer (J)) then
583 Error_Msg
584 ("directory separator character not allowed",
585 Sloc (Expression (Arg)) + Source_Ptr (J));
586 end if;
587 end loop;
589 return Name_Find;
590 end Get_Fname;
592 -------------------------
593 -- Get_String_Argument --
594 -------------------------
596 function Get_String_Argument (Arg : Node_Id) return String_Ptr is
597 Str : String_Id;
599 begin
600 if Nkind (Expression (Arg)) /= N_String_Literal
601 and then
602 Nkind (Expression (Arg)) /= N_Operator_Symbol
603 then
604 Error_Msg_N
605 ("argument for pragma% must be string literal", Arg);
606 raise Error_Resync;
607 end if;
609 Str := Strval (Expression (Arg));
611 -- Check string has no wide chars
613 for J in 1 .. String_Length (Str) loop
614 if Get_String_Char (Str, J) > 255 then
615 Error_Msg
616 ("wide character not allowed in pattern for pragma%",
617 Sloc (Expression (Arg2)) + Text_Ptr (J) - 1);
618 end if;
619 end loop;
621 -- Acquire string
623 String_To_Name_Buffer (Str);
624 return new String'(Name_Buffer (1 .. Name_Len));
625 end Get_String_Argument;
627 --------------------
628 -- Process_Casing --
629 --------------------
631 procedure Process_Casing (Arg : Node_Id) is
632 Expr : constant Node_Id := Expression (Arg);
634 begin
635 Check_Required_Identifier (Arg, Name_Casing);
637 if Nkind (Expr) = N_Identifier then
638 if Chars (Expr) = Name_Lowercase then
639 Cas := All_Lower_Case;
640 return;
641 elsif Chars (Expr) = Name_Uppercase then
642 Cas := All_Upper_Case;
643 return;
644 elsif Chars (Expr) = Name_Mixedcase then
645 Cas := Mixed_Case;
646 return;
647 end if;
648 end if;
650 Error_Msg_N
651 ("Casing argument for pragma% must be " &
652 "one of Mixedcase, Lowercase, Uppercase",
653 Arg);
654 end Process_Casing;
656 -----------------------------
657 -- Process_Dot_Replacement --
658 -----------------------------
660 procedure Process_Dot_Replacement (Arg : Node_Id) is
661 begin
662 Check_Required_Identifier (Arg, Name_Dot_Replacement);
663 Dot := Get_String_Argument (Arg);
664 end Process_Dot_Replacement;
666 -- Start of processing for Source_File_Name and
667 -- Source_File_Name_Project pragmas.
669 begin
670 if Prag_Id = Pragma_Source_File_Name then
671 if Project_File_In_Use = In_Use then
672 Error_Msg
673 ("pragma Source_File_Name cannot be used " &
674 "with a project file", Pragma_Sloc);
676 else
677 Project_File_In_Use := Not_In_Use;
678 end if;
680 else
681 if Project_File_In_Use = Not_In_Use then
682 Error_Msg
683 ("pragma Source_File_Name_Project should only be used " &
684 "with a project file", Pragma_Sloc);
685 else
686 Project_File_In_Use := In_Use;
687 end if;
688 end if;
690 -- We permit from 1 to 3 arguments
692 if Arg_Count not in 1 .. 3 then
693 Check_Arg_Count (1);
694 end if;
696 Expr1 := Expression (Arg1);
698 -- If first argument is identifier or selected component, then
699 -- we have the specific file case of the Source_File_Name pragma,
700 -- and the first argument is a unit name.
702 if Nkind (Expr1) = N_Identifier
703 or else
704 (Nkind (Expr1) = N_Selected_Component
705 and then
706 Nkind (Selector_Name (Expr1)) = N_Identifier)
707 then
708 if Nkind (Expr1) = N_Identifier
709 and then Chars (Expr1) = Name_System
710 then
711 Error_Msg_N
712 ("pragma Source_File_Name may not be used for System",
713 Arg1);
714 return Error;
715 end if;
717 -- Process index argument if present
719 if Arg_Count = 3 then
720 Expr := Expression (Arg3);
722 if Nkind (Expr) /= N_Integer_Literal
723 or else not UI_Is_In_Int_Range (Intval (Expr))
724 or else Intval (Expr) > 999
725 or else Intval (Expr) <= 0
726 then
727 Error_Msg
728 ("pragma% index must be integer literal" &
729 " in range 1 .. 999", Sloc (Expr));
730 raise Error_Resync;
731 else
732 Index := UI_To_Int (Intval (Expr));
733 end if;
735 -- No index argument present
737 else
738 Check_Arg_Count (2);
739 Index := 0;
740 end if;
742 Check_Optional_Identifier (Arg1, Name_Unit_Name);
743 Unam := Get_Unit_Name (Expr1);
745 Check_Arg_Is_String_Literal (Arg2);
747 if Chars (Arg2) = Name_Spec_File_Name then
748 Set_File_Name
749 (Get_Spec_Name (Unam), Get_Fname (Arg2), Index);
751 elsif Chars (Arg2) = Name_Body_File_Name then
752 Set_File_Name
753 (Unam, Get_Fname (Arg2), Index);
755 else
756 Error_Msg_N
757 ("pragma% argument has incorrect identifier", Arg2);
758 return Pragma_Node;
759 end if;
761 -- If the first argument is not an identifier, then we must have
762 -- the pattern form of the pragma, and the first argument must be
763 -- the pattern string with an appropriate name.
765 else
766 if Chars (Arg1) = Name_Spec_File_Name then
767 Typ := 's';
769 elsif Chars (Arg1) = Name_Body_File_Name then
770 Typ := 'b';
772 elsif Chars (Arg1) = Name_Subunit_File_Name then
773 Typ := 'u';
775 elsif Chars (Arg1) = Name_Unit_Name then
776 Error_Msg_N
777 ("Unit_Name parameter for pragma% must be an identifier",
778 Arg1);
779 raise Error_Resync;
781 else
782 Error_Msg_N
783 ("pragma% argument has incorrect identifier", Arg1);
784 raise Error_Resync;
785 end if;
787 Pat := Get_String_Argument (Arg1);
789 -- Check pattern has exactly one asterisk
791 Nast := 0;
792 for J in Pat'Range loop
793 if Pat (J) = '*' then
794 Nast := Nast + 1;
795 end if;
796 end loop;
798 if Nast /= 1 then
799 Error_Msg_N
800 ("file name pattern must have exactly one * character",
801 Arg1);
802 return Pragma_Node;
803 end if;
805 -- Set defaults for Casing and Dot_Separator parameters
807 Cas := All_Lower_Case;
808 Dot := new String'(".");
810 -- Process second and third arguments if present
812 if Arg_Count > 1 then
813 if Chars (Arg2) = Name_Casing then
814 Process_Casing (Arg2);
816 if Arg_Count = 3 then
817 Process_Dot_Replacement (Arg3);
818 end if;
820 else
821 Process_Dot_Replacement (Arg2);
823 if Arg_Count = 3 then
824 Process_Casing (Arg3);
825 end if;
826 end if;
827 end if;
829 Set_File_Name_Pattern (Pat, Typ, Dot, Cas);
830 end if;
831 end Source_File_Name;
833 -----------------------------
834 -- Source_Reference (GNAT) --
835 -----------------------------
837 -- pragma Source_Reference
838 -- (INTEGER_LITERAL [, STRING_LITERAL] );
840 -- Processing for this pragma must be done at parse time, since error
841 -- messages needing the proper line numbers can be generated in parse
842 -- only mode with semantic checking turned off, and indeed we usually
843 -- turn off semantic checking anyway if any parse errors are found.
845 when Pragma_Source_Reference => Source_Reference : declare
846 Fname : File_Name_Type;
848 begin
849 if Arg_Count /= 1 then
850 Check_Arg_Count (2);
851 Check_No_Identifier (Arg2);
852 end if;
854 -- Check that this is first line of file. We skip this test if
855 -- we are in syntax check only mode, since we may be dealing with
856 -- multiple compilation units.
858 if Get_Physical_Line_Number (Pragma_Sloc) /= 1
859 and then Num_SRef_Pragmas (Current_Source_File) = 0
860 and then Operating_Mode /= Check_Syntax
861 then
862 Error_Msg -- CODEFIX
863 ("first % pragma must be first line of file", Pragma_Sloc);
864 raise Error_Resync;
865 end if;
867 Check_No_Identifier (Arg1);
869 if Arg_Count = 1 then
870 if Num_SRef_Pragmas (Current_Source_File) = 0 then
871 Error_Msg
872 ("file name required for first % pragma in file",
873 Pragma_Sloc);
874 raise Error_Resync;
875 else
876 Fname := No_File;
877 end if;
879 -- File name present
881 else
882 Check_Arg_Is_String_Literal (Arg2);
883 String_To_Name_Buffer (Strval (Expression (Arg2)));
884 Fname := Name_Find;
886 if Num_SRef_Pragmas (Current_Source_File) > 0 then
887 if Fname /= Full_Ref_Name (Current_Source_File) then
888 Error_Msg
889 ("file name must be same in all % pragmas", Pragma_Sloc);
890 raise Error_Resync;
891 end if;
892 end if;
893 end if;
895 if Nkind (Expression (Arg1)) /= N_Integer_Literal then
896 Error_Msg
897 ("argument for pragma% must be integer literal",
898 Sloc (Expression (Arg1)));
899 raise Error_Resync;
901 -- OK, this source reference pragma is effective, however, we
902 -- ignore it if it is not in the first unit in the multiple unit
903 -- case. This is because the only purpose in this case is to
904 -- provide source pragmas for subsequent use by gnatchop.
906 else
907 if Num_Library_Units = 1 then
908 Register_Source_Ref_Pragma
909 (Fname,
910 Strip_Directory (Fname),
911 UI_To_Int (Intval (Expression (Arg1))),
912 Get_Physical_Line_Number (Pragma_Sloc) + 1);
913 end if;
914 end if;
915 end Source_Reference;
917 -------------------------
918 -- Style_Checks (GNAT) --
919 -------------------------
921 -- pragma Style_Checks (On | Off | ALL_CHECKS | STRING_LITERAL);
923 -- This is processed by the parser since some of the style
924 -- checks take place during source scanning and parsing.
926 when Pragma_Style_Checks => Style_Checks : declare
927 A : Node_Id;
928 S : String_Id;
929 C : Char_Code;
930 OK : Boolean := True;
932 begin
933 -- Two argument case is only for semantics
935 if Arg_Count = 2 then
936 null;
938 else
939 Check_Arg_Count (1);
940 Check_No_Identifier (Arg1);
941 A := Expression (Arg1);
943 if Nkind (A) = N_String_Literal then
944 S := Strval (A);
946 declare
947 Slen : constant Natural := Natural (String_Length (S));
948 Options : String (1 .. Slen);
949 J : Natural;
950 Ptr : Natural;
952 begin
953 J := 1;
954 loop
955 C := Get_String_Char (S, Int (J));
957 if not In_Character_Range (C) then
958 OK := False;
959 Ptr := J;
960 exit;
962 else
963 Options (J) := Get_Character (C);
964 end if;
966 if J = Slen then
967 if not Ignore_Style_Checks_Pragmas then
968 Set_Style_Check_Options (Options, OK, Ptr);
969 end if;
971 exit;
973 else
974 J := J + 1;
975 end if;
976 end loop;
978 if not OK then
979 Error_Msg
980 (Style_Msg_Buf (1 .. Style_Msg_Len),
981 Sloc (Expression (Arg1)) + Source_Ptr (Ptr));
982 raise Error_Resync;
983 end if;
984 end;
986 elsif Nkind (A) /= N_Identifier then
987 OK := False;
989 elsif Chars (A) = Name_All_Checks then
990 if not Ignore_Style_Checks_Pragmas then
991 if GNAT_Mode then
992 Stylesw.Set_GNAT_Style_Check_Options;
993 else
994 Stylesw.Set_Default_Style_Check_Options;
995 end if;
996 end if;
998 elsif Chars (A) = Name_On then
999 if not Ignore_Style_Checks_Pragmas then
1000 Style_Check := True;
1001 end if;
1003 elsif Chars (A) = Name_Off then
1004 if not Ignore_Style_Checks_Pragmas then
1005 Style_Check := False;
1006 end if;
1008 else
1009 OK := False;
1010 end if;
1012 if not OK then
1013 Error_Msg ("incorrect argument for pragma%", Sloc (A));
1014 raise Error_Resync;
1015 end if;
1016 end if;
1017 end Style_Checks;
1019 -------------------------
1020 -- Suppress_All (GNAT) --
1021 -------------------------
1023 -- pragma Suppress_All
1025 -- This is a rather odd pragma, because other compilers allow it in
1026 -- strange places. DEC allows it at the end of units, and Rational
1027 -- allows it as a program unit pragma, when it would be more natural
1028 -- if it were a configuration pragma.
1030 -- Since the reason we provide this pragma is for compatibility with
1031 -- these other compilers, we want to accommodate these strange placement
1032 -- rules, and the easiest thing is simply to allow it anywhere in a
1033 -- unit. If this pragma appears anywhere within a unit, then the effect
1034 -- is as though a pragma Suppress (All_Checks) had appeared as the first
1035 -- line of the current file, i.e. as the first configuration pragma in
1036 -- the current unit.
1038 -- To get this effect, we set the flag Has_Pragma_Suppress_All in the
1039 -- compilation unit node for the current source file then in the last
1040 -- stage of parsing a file, if this flag is set, we materialize the
1041 -- Suppress (All_Checks) pragma, marked as not coming from Source.
1043 when Pragma_Suppress_All =>
1044 Set_Has_Pragma_Suppress_All (Cunit (Current_Source_Unit));
1046 ---------------------
1047 -- Warnings (GNAT) --
1048 ---------------------
1050 -- pragma Warnings (On | Off [,REASON]);
1051 -- pragma Warnings (On | Off, LOCAL_NAME [,REASON]);
1052 -- pragma Warnings (static_string_EXPRESSION [,REASON]);
1053 -- pragma Warnings (On | Off, static_string_EXPRESSION [,REASON]);
1055 -- The one argument ON/OFF case is processed by the parser, since it may
1056 -- control parser warnings as well as semantic warnings, and in any case
1057 -- we want to be absolutely sure that the range in the warnings table is
1058 -- set well before any semantic analysis is performed. Note that we
1059 -- ignore this pragma if debug flag -gnatd.i is set.
1061 -- Also note that the "one argument" case may have two arguments if the
1062 -- second one is a reason argument.
1064 when Pragma_Warnings =>
1065 if not Debug_Flag_Dot_I
1066 and then (Arg_Count = 1
1067 or else (Arg_Count = 2
1068 and then Chars (Arg2) = Name_Reason))
1069 then
1070 Check_No_Identifier (Arg1);
1072 declare
1073 Argx : constant Node_Id := Expression (Arg1);
1075 function Get_Reason return String_Id;
1076 -- Analyzes Reason argument and returns corresponding String_Id
1077 -- value, or null if there is no Reason argument, or if the
1078 -- argument is not of the required form.
1080 ----------------
1081 -- Get_Reason --
1082 ----------------
1084 function Get_Reason return String_Id is
1085 begin
1086 if Arg_Count = 1 then
1087 return Null_String_Id;
1088 else
1089 Start_String;
1090 Get_Reason_String (Expression (Arg2));
1091 return End_String;
1092 end if;
1093 end Get_Reason;
1095 begin
1096 if Nkind (Argx) = N_Identifier then
1097 if Chars (Argx) = Name_On then
1098 Set_Warnings_Mode_On (Pragma_Sloc);
1099 elsif Chars (Argx) = Name_Off then
1100 Set_Warnings_Mode_Off (Pragma_Sloc, Get_Reason);
1101 end if;
1102 end if;
1103 end;
1104 end if;
1106 -----------------------------
1107 -- Wide_Character_Encoding --
1108 -----------------------------
1110 -- pragma Wide_Character_Encoding (IDENTIFIER | CHARACTER_LITERAL);
1112 -- This is processed by the parser, since the scanner is affected
1114 when Pragma_Wide_Character_Encoding => Wide_Character_Encoding : declare
1115 A : Node_Id;
1117 begin
1118 Check_Arg_Count (1);
1119 Check_No_Identifier (Arg1);
1120 A := Expression (Arg1);
1122 if Nkind (A) = N_Identifier then
1123 Get_Name_String (Chars (A));
1124 Wide_Character_Encoding_Method :=
1125 Get_WC_Encoding_Method (Name_Buffer (1 .. Name_Len));
1127 elsif Nkind (A) = N_Character_Literal then
1128 declare
1129 R : constant Char_Code :=
1130 Char_Code (UI_To_Int (Char_Literal_Value (A)));
1131 begin
1132 if In_Character_Range (R) then
1133 Wide_Character_Encoding_Method :=
1134 Get_WC_Encoding_Method (Get_Character (R));
1135 else
1136 raise Constraint_Error;
1137 end if;
1138 end;
1140 else
1141 raise Constraint_Error;
1142 end if;
1144 Upper_Half_Encoding :=
1145 Wide_Character_Encoding_Method in
1146 WC_Upper_Half_Encoding_Method;
1148 exception
1149 when Constraint_Error =>
1150 Error_Msg_N ("invalid argument for pragma%", Arg1);
1151 end Wide_Character_Encoding;
1153 -----------------------
1154 -- All Other Pragmas --
1155 -----------------------
1157 -- For all other pragmas, checking and processing is handled
1158 -- entirely in Sem_Prag, and no further checking is done by Par.
1160 when Pragma_Abort_Defer |
1161 Pragma_Abstract_State |
1162 Pragma_Async_Readers |
1163 Pragma_Async_Writers |
1164 Pragma_Assertion_Policy |
1165 Pragma_Assume |
1166 Pragma_Assume_No_Invalid_Values |
1167 Pragma_All_Calls_Remote |
1168 Pragma_Allow_Integer_Address |
1169 Pragma_Annotate |
1170 Pragma_Assert |
1171 Pragma_Assert_And_Cut |
1172 Pragma_Asynchronous |
1173 Pragma_Atomic |
1174 Pragma_Atomic_Components |
1175 Pragma_Attach_Handler |
1176 Pragma_Attribute_Definition |
1177 Pragma_Check |
1178 Pragma_Check_Float_Overflow |
1179 Pragma_Check_Name |
1180 Pragma_Check_Policy |
1181 Pragma_CIL_Constructor |
1182 Pragma_Compile_Time_Error |
1183 Pragma_Compile_Time_Warning |
1184 Pragma_Contract_Cases |
1185 Pragma_Convention_Identifier |
1186 Pragma_CPP_Class |
1187 Pragma_CPP_Constructor |
1188 Pragma_CPP_Virtual |
1189 Pragma_CPP_Vtable |
1190 Pragma_CPU |
1191 Pragma_C_Pass_By_Copy |
1192 Pragma_Comment |
1193 Pragma_Common_Object |
1194 Pragma_Complete_Representation |
1195 Pragma_Complex_Representation |
1196 Pragma_Component_Alignment |
1197 Pragma_Controlled |
1198 Pragma_Convention |
1199 Pragma_Debug_Policy |
1200 Pragma_Depends |
1201 Pragma_Detect_Blocking |
1202 Pragma_Default_Initial_Condition |
1203 Pragma_Default_Scalar_Storage_Order |
1204 Pragma_Default_Storage_Pool |
1205 Pragma_Disable_Atomic_Synchronization |
1206 Pragma_Discard_Names |
1207 Pragma_Dispatching_Domain |
1208 Pragma_Effective_Reads |
1209 Pragma_Effective_Writes |
1210 Pragma_Eliminate |
1211 Pragma_Elaborate |
1212 Pragma_Elaborate_All |
1213 Pragma_Elaborate_Body |
1214 Pragma_Elaboration_Checks |
1215 Pragma_Enable_Atomic_Synchronization |
1216 Pragma_Export |
1217 Pragma_Export_Function |
1218 Pragma_Export_Object |
1219 Pragma_Export_Procedure |
1220 Pragma_Export_Value |
1221 Pragma_Export_Valued_Procedure |
1222 Pragma_Extend_System |
1223 Pragma_Extensions_Visible |
1224 Pragma_External |
1225 Pragma_External_Name_Casing |
1226 Pragma_Favor_Top_Level |
1227 Pragma_Fast_Math |
1228 Pragma_Finalize_Storage_Only |
1229 Pragma_Ghost |
1230 Pragma_Global |
1231 Pragma_Ident |
1232 Pragma_Implementation_Defined |
1233 Pragma_Implemented |
1234 Pragma_Implicit_Packing |
1235 Pragma_Import |
1236 Pragma_Import_Function |
1237 Pragma_Import_Object |
1238 Pragma_Import_Procedure |
1239 Pragma_Import_Valued_Procedure |
1240 Pragma_Independent |
1241 Pragma_Independent_Components |
1242 Pragma_Initial_Condition |
1243 Pragma_Initialize_Scalars |
1244 Pragma_Initializes |
1245 Pragma_Inline |
1246 Pragma_Inline_Always |
1247 Pragma_Inline_Generic |
1248 Pragma_Inspection_Point |
1249 Pragma_Interface |
1250 Pragma_Interface_Name |
1251 Pragma_Interrupt_Handler |
1252 Pragma_Interrupt_State |
1253 Pragma_Interrupt_Priority |
1254 Pragma_Invariant |
1255 Pragma_Java_Constructor |
1256 Pragma_Java_Interface |
1257 Pragma_Keep_Names |
1258 Pragma_License |
1259 Pragma_Link_With |
1260 Pragma_Linker_Alias |
1261 Pragma_Linker_Constructor |
1262 Pragma_Linker_Destructor |
1263 Pragma_Linker_Options |
1264 Pragma_Linker_Section |
1265 Pragma_Lock_Free |
1266 Pragma_Locking_Policy |
1267 Pragma_Loop_Invariant |
1268 Pragma_Loop_Optimize |
1269 Pragma_Loop_Variant |
1270 Pragma_Machine_Attribute |
1271 Pragma_Main |
1272 Pragma_Main_Storage |
1273 Pragma_Memory_Size |
1274 Pragma_No_Body |
1275 Pragma_No_Elaboration_Code_All |
1276 Pragma_No_Inline |
1277 Pragma_No_Return |
1278 Pragma_No_Run_Time |
1279 Pragma_No_Strict_Aliasing |
1280 Pragma_No_Tagged_Streams |
1281 Pragma_Normalize_Scalars |
1282 Pragma_Obsolescent |
1283 Pragma_Ordered |
1284 Pragma_Optimize |
1285 Pragma_Optimize_Alignment |
1286 Pragma_Overflow_Mode |
1287 Pragma_Overriding_Renamings |
1288 Pragma_Pack |
1289 Pragma_Part_Of |
1290 Pragma_Partition_Elaboration_Policy |
1291 Pragma_Passive |
1292 Pragma_Preelaborable_Initialization |
1293 Pragma_Polling |
1294 Pragma_Prefix_Exception_Messages |
1295 Pragma_Persistent_BSS |
1296 Pragma_Post |
1297 Pragma_Postcondition |
1298 Pragma_Post_Class |
1299 Pragma_Pre |
1300 Pragma_Precondition |
1301 Pragma_Predicate |
1302 Pragma_Preelaborate |
1303 Pragma_Pre_Class |
1304 Pragma_Priority |
1305 Pragma_Priority_Specific_Dispatching |
1306 Pragma_Profile |
1307 Pragma_Profile_Warnings |
1308 Pragma_Propagate_Exceptions |
1309 Pragma_Provide_Shift_Operators |
1310 Pragma_Psect_Object |
1311 Pragma_Pure |
1312 Pragma_Pure_Function |
1313 Pragma_Queuing_Policy |
1314 Pragma_Refined_Depends |
1315 Pragma_Refined_Global |
1316 Pragma_Refined_Post |
1317 Pragma_Refined_State |
1318 Pragma_Relative_Deadline |
1319 Pragma_Remote_Access_Type |
1320 Pragma_Remote_Call_Interface |
1321 Pragma_Remote_Types |
1322 Pragma_Restricted_Run_Time |
1323 Pragma_Rational |
1324 Pragma_Ravenscar |
1325 Pragma_Reviewable |
1326 Pragma_Share_Generic |
1327 Pragma_Shared |
1328 Pragma_Shared_Passive |
1329 Pragma_Short_Circuit_And_Or |
1330 Pragma_Short_Descriptors |
1331 Pragma_Simple_Storage_Pool_Type |
1332 Pragma_SPARK_Mode |
1333 Pragma_Storage_Size |
1334 Pragma_Storage_Unit |
1335 Pragma_Static_Elaboration_Desired |
1336 Pragma_Stream_Convert |
1337 Pragma_Subtitle |
1338 Pragma_Suppress |
1339 Pragma_Suppress_Debug_Info |
1340 Pragma_Suppress_Exception_Locations |
1341 Pragma_Suppress_Initialization |
1342 Pragma_System_Name |
1343 Pragma_Task_Dispatching_Policy |
1344 Pragma_Task_Info |
1345 Pragma_Task_Name |
1346 Pragma_Task_Storage |
1347 Pragma_Test_Case |
1348 Pragma_Thread_Local_Storage |
1349 Pragma_Time_Slice |
1350 Pragma_Title |
1351 Pragma_Type_Invariant |
1352 Pragma_Type_Invariant_Class |
1353 Pragma_Unchecked_Union |
1354 Pragma_Unevaluated_Use_Of_Old |
1355 Pragma_Unimplemented_Unit |
1356 Pragma_Universal_Aliasing |
1357 Pragma_Universal_Data |
1358 Pragma_Unmodified |
1359 Pragma_Unreferenced |
1360 Pragma_Unreferenced_Objects |
1361 Pragma_Unreserve_All_Interrupts |
1362 Pragma_Unsuppress |
1363 Pragma_Use_VADS_Size |
1364 Pragma_Volatile |
1365 Pragma_Volatile_Components |
1366 Pragma_Warning_As_Error |
1367 Pragma_Weak_External |
1368 Pragma_Validity_Checks =>
1369 null;
1371 --------------------
1372 -- Unknown_Pragma --
1373 --------------------
1375 -- Should be impossible, since we excluded this case earlier on
1377 when Unknown_Pragma =>
1378 raise Program_Error;
1380 end case;
1382 return Pragma_Node;
1384 --------------------
1385 -- Error Handling --
1386 --------------------
1388 exception
1389 when Error_Resync =>
1390 return Error;
1392 end Prag;