1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 ------------------------------------------------------------------------------
28 -- Generally the parser checks the basic syntax of pragmas, but does not
29 -- do specialized syntax checks for individual pragmas, these are deferred
30 -- to semantic analysis time (see unit Sem_Prag). There are some pragmas
31 -- which require recognition and either partial or complete processing
32 -- during parsing, and this unit performs this required processing.
34 with Fname.UF; use Fname.UF;
35 with Osint; use Osint;
36 with Stringt; use Stringt;
37 with Stylesw; use Stylesw;
38 with Uintp; use Uintp;
39 with Uname; use Uname;
43 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id is
44 Pragma_Name : constant Name_Id := Chars (Pragma_Node);
45 Pragma_Sloc : constant Source_Ptr := Sloc (Pragma_Node);
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 function Arg1 return Node_Id;
54 function Arg2 return Node_Id;
55 function Arg3 return Node_Id;
56 -- Obtain specified Pragma_Argument_Association. It is allowable to call
57 -- the routine for the argument one past the last present argument, but
58 -- that is the only case in which a non-present argument can be referenced.
60 procedure Check_Arg_Count (Required : Int);
61 -- Check argument count for pragma = Required.
62 -- If not give error and raise Error_Resync.
64 procedure Check_Arg_Is_String_Literal (Arg : Node_Id);
65 -- Check the expression of the specified argument to make sure that it
66 -- is a string literal. If not give error and raise Error_Resync.
68 procedure Check_Arg_Is_On_Or_Off (Arg : Node_Id);
69 -- Check the expression of the specified argument to make sure that it
70 -- is an identifier which is either ON or OFF, and if not, then issue
71 -- an error message and raise Error_Resync.
73 procedure Check_No_Identifier (Arg : Node_Id);
74 -- Checks that the given argument does not have an identifier. If an
75 -- identifier is present, then an error message is issued, and
76 -- Error_Resync is raised.
78 procedure Check_Optional_Identifier (Arg : Node_Id; Id : Name_Id);
79 -- Checks if the given argument has an identifier, and if so, requires
80 -- it to match the given identifier name. If there is a non-matching
81 -- identifier, then an error message is given and Error_Resync raised.
83 procedure Check_Required_Identifier (Arg : Node_Id; Id : Name_Id);
84 -- Same as Check_Optional_Identifier, except that the name is required
85 -- to be present and to match the given Id value.
91 function Arg1 return Node_Id is
93 return First (Pragma_Argument_Associations (Pragma_Node));
100 function Arg2 return Node_Id is
109 function Arg3 return Node_Id is
114 ---------------------
115 -- Check_Arg_Count --
116 ---------------------
118 procedure Check_Arg_Count (Required : Int) is
120 if Arg_Count /= Required then
121 Error_Msg ("wrong number of arguments for pragma%", Pragma_Sloc);
126 ----------------------------
127 -- Check_Arg_Is_On_Or_Off --
128 ----------------------------
130 procedure Check_Arg_Is_On_Or_Off (Arg : Node_Id) is
131 Argx : constant Node_Id := Expression (Arg);
134 if Nkind (Expression (Arg)) /= N_Identifier
135 or else (Chars (Argx) /= Name_On
137 Chars (Argx) /= Name_Off)
139 Error_Msg_Name_2 := Name_On;
140 Error_Msg_Name_3 := Name_Off;
143 ("argument for pragma% must be% or%", Sloc (Argx));
146 end Check_Arg_Is_On_Or_Off;
148 ---------------------------------
149 -- Check_Arg_Is_String_Literal --
150 ---------------------------------
152 procedure Check_Arg_Is_String_Literal (Arg : Node_Id) is
154 if Nkind (Expression (Arg)) /= N_String_Literal then
156 ("argument for pragma% must be string literal",
157 Sloc (Expression (Arg)));
160 end Check_Arg_Is_String_Literal;
162 -------------------------
163 -- Check_No_Identifier --
164 -------------------------
166 procedure Check_No_Identifier (Arg : Node_Id) is
168 if Chars (Arg) /= No_Name then
169 Error_Msg_N ("pragma% does not permit named arguments", Arg);
172 end Check_No_Identifier;
174 -------------------------------
175 -- Check_Optional_Identifier --
176 -------------------------------
178 procedure Check_Optional_Identifier (Arg : Node_Id; Id : Name_Id) is
180 if Present (Arg) and then Chars (Arg) /= No_Name then
181 if Chars (Arg) /= Id then
182 Error_Msg_Name_2 := Id;
183 Error_Msg_N ("pragma% argument expects identifier%", Arg);
186 end Check_Optional_Identifier;
188 -------------------------------
189 -- Check_Required_Identifier --
190 -------------------------------
192 procedure Check_Required_Identifier (Arg : Node_Id; Id : Name_Id) is
194 if Chars (Arg) /= Id then
195 Error_Msg_Name_2 := Id;
196 Error_Msg_N ("pragma% argument must have identifier%", Arg);
198 end Check_Required_Identifier;
205 Error_Msg_Name_1 := Pragma_Name;
207 -- Ignore unrecognized pragma. We let Sem post the warning for this, since
208 -- it is a semantic error, not a syntactic one (we have already checked
209 -- the syntax for the unrecognized pragma as required by (RM 2.8(11)).
211 if not Is_Pragma_Name (Chars (Pragma_Node)) then
215 -- Count number of arguments. This loop also checks if any of the arguments
216 -- are Error, indicating a syntax error as they were parsed. If so, we
217 -- simply return, because we get into trouble with cascaded errors if we
218 -- try to perform our error checks on junk arguments.
222 if Present (Pragma_Argument_Associations (Pragma_Node)) then
225 while Arg_Node /= Empty loop
226 Arg_Count := Arg_Count + 1;
228 if Expression (Arg_Node) = Error then
236 -- Remaining processing is pragma dependent
238 case Get_Pragma_Id (Pragma_Name) is
244 -- This pragma must be processed at parse time, since we want to set
245 -- the Ada 83 and Ada 95 switches properly at parse time to recognize
246 -- Ada 83 syntax or Ada 95 syntax as appropriate.
248 when Pragma_Ada_83 =>
256 -- This pragma must be processed at parse time, since we want to set
257 -- the Ada 83 and Ada_95 switches properly at parse time to recognize
258 -- Ada 83 syntax or Ada 95 syntax as appropriate.
260 when Pragma_Ada_95 =>
268 -- pragma Debug (PROCEDURE_CALL_STATEMENT);
270 -- This has to be processed by the parser because of the very peculiar
271 -- form of the second parameter, which is syntactically from a formal
272 -- point of view a function call (since it must be an expression), but
273 -- semantically we treat it as a procedure call (which has exactly the
274 -- same syntactic form, so that's why we can get away with this!)
278 Check_No_Identifier (Arg1);
281 Expr : constant Node_Id := New_Copy (Expression (Arg1));
284 if Nkind (Expr) /= N_Indexed_Component
285 and then Nkind (Expr) /= N_Function_Call
286 and then Nkind (Expr) /= N_Identifier
287 and then Nkind (Expr) /= N_Selected_Component
290 ("argument of pragma% is not procedure call", Sloc (Expr));
294 (Pragma_Node, P_Statement_Name (Expr));
298 -------------------------------
299 -- Extensions_Allowed (GNAT) --
300 -------------------------------
302 -- pragma Extensions_Allowed (Off | On)
304 -- The processing for pragma Extensions_Allowed must be done at
305 -- parse time, since extensions mode may affect what is accepted.
307 when Pragma_Extensions_Allowed =>
309 Check_No_Identifier (Arg1);
310 Check_Arg_Is_On_Or_Off (Arg1);
311 Opt.Extensions_Allowed := (Chars (Expression (Arg1)) = Name_On);
317 -- pragma List (Off | On)
319 -- The processing for pragma List must be done at parse time,
320 -- since a listing can be generated in parse only mode.
324 Check_No_Identifier (Arg1);
325 Check_Arg_Is_On_Or_Off (Arg1);
327 -- We unconditionally make a List_On entry for the pragma, so that
328 -- in the List (Off) case, the pragma will print even in a region
329 -- of code with listing turned off (this is required!)
331 List_Pragmas.Increment_Last;
332 List_Pragmas.Table (List_Pragmas.Last) :=
333 (Ptyp => List_On, Ploc => Sloc (Pragma_Node));
335 -- Now generate the list off entry for pragma List (Off)
337 if Chars (Expression (Arg1)) = Name_Off then
338 List_Pragmas.Increment_Last;
339 List_Pragmas.Table (List_Pragmas.Last) :=
340 (Ptyp => List_Off, Ploc => Semi);
349 -- Processing for this pragma must be done at parse time, since a
350 -- listing can be generated in parse only mode with semantics off.
354 List_Pragmas.Increment_Last;
355 List_Pragmas.Table (List_Pragmas.Last) := (Page, Semi);
357 -----------------------------
358 -- Source_File_Name (GNAT) --
359 -----------------------------
361 -- There are five forms of this pragma:
363 -- pragma Source_File_Name (
364 -- [UNIT_NAME =>] unit_NAME,
365 -- BODY_FILE_NAME => STRING_LITERAL);
367 -- pragma Source_File_Name (
368 -- [UNIT_NAME =>] unit_NAME,
369 -- SPEC_FILE_NAME => STRING_LITERAL);
371 -- pragma Source_File_Name (
372 -- BODY_FILE_NAME => STRING_LITERAL
373 -- [, DOT_REPLACEMENT => STRING_LITERAL]
374 -- [, CASING => CASING_SPEC]);
376 -- pragma Source_File_Name (
377 -- SPEC_FILE_NAME => STRING_LITERAL
378 -- [, DOT_REPLACEMENT => STRING_LITERAL]
379 -- [, CASING => CASING_SPEC]);
381 -- pragma Source_File_Name (
382 -- SUBUNIT_FILE_NAME => STRING_LITERAL
383 -- [, DOT_REPLACEMENT => STRING_LITERAL]
384 -- [, CASING => CASING_SPEC]);
386 -- CASING_SPEC ::= Uppercase | Lowercase | Mixedcase
388 -- Note: we process this during parsing, since we need to have the
389 -- source file names set well before the semantic analysis starts,
390 -- since we load the spec and with'ed packages before analysis.
392 when Pragma_Source_File_Name => Source_File_Name : declare
393 Unam : Unit_Name_Type;
401 function Get_Fname (Arg : Node_Id) return Name_Id;
402 -- Process file name from unit name form of pragma
404 function Get_String_Argument (Arg : Node_Id) return String_Ptr;
405 -- Process string literal value from argument
407 procedure Process_Casing (Arg : Node_Id);
408 -- Process Casing argument of pattern form of pragma
410 procedure Process_Dot_Replacement (Arg : Node_Id);
411 -- Process Dot_Replacement argument of patterm form of pragma
417 function Get_Fname (Arg : Node_Id) return Name_Id is
419 String_To_Name_Buffer (Strval (Expression (Arg)));
421 for J in 1 .. Name_Len loop
422 if Is_Directory_Separator (Name_Buffer (J)) then
424 ("directory separator character not allowed",
425 Sloc (Expression (Arg)) + Source_Ptr (J));
432 -------------------------
433 -- Get_String_Argument --
434 -------------------------
436 function Get_String_Argument (Arg : Node_Id) return String_Ptr is
440 if Nkind (Expression (Arg)) /= N_String_Literal
442 Nkind (Expression (Arg)) /= N_Operator_Symbol
445 ("argument for pragma% must be string literal", Arg);
449 Str := Strval (Expression (Arg));
451 -- Check string has no wide chars
453 for J in 1 .. String_Length (Str) loop
454 if Get_String_Char (Str, J) > 255 then
456 ("wide character not allowed in pattern for pragma%",
457 Sloc (Expression (Arg2)) + Text_Ptr (J) - 1);
463 String_To_Name_Buffer (Str);
464 return new String'(Name_Buffer (1 .. Name_Len));
465 end Get_String_Argument;
471 procedure Process_Casing (Arg : Node_Id) is
472 Expr : constant Node_Id := Expression (Arg);
475 Check_Required_Identifier (Arg, Name_Casing);
477 if Nkind (Expr) = N_Identifier then
478 if Chars (Expr) = Name_Lowercase then
479 Cas := All_Lower_Case;
481 elsif Chars (Expr) = Name_Uppercase then
482 Cas := All_Upper_Case;
484 elsif Chars (Expr) = Name_Mixedcase then
491 ("Casing argument for pragma% must be " &
492 "one of Mixedcase, Lowercase, Uppercase",
496 -----------------------------
497 -- Process_Dot_Replacement --
498 -----------------------------
500 procedure Process_Dot_Replacement (Arg : Node_Id) is
502 Check_Required_Identifier (Arg, Name_Dot_Replacement);
503 Dot := Get_String_Argument (Arg);
504 end Process_Dot_Replacement;
506 -- Start of processing for Source_File_Name pragma
509 -- We permit from 1 to 3 arguments
511 if Arg_Count not in 1 .. 3 then
515 Expr1 := Expression (Arg1);
517 -- If first argument is identifier or selected component, then
518 -- we have the specific file case of the Source_File_Name pragma,
519 -- and the first argument is a unit name.
521 if Nkind (Expr1) = N_Identifier
523 (Nkind (Expr1) = N_Selected_Component
525 Nkind (Selector_Name (Expr1)) = N_Identifier)
527 if Nkind (Expr1) = N_Identifier
528 and then Chars (Expr1) = Name_System
531 ("pragma Source_File_Name may not be used for System", Arg1);
537 Check_Optional_Identifier (Arg1, Name_Unit_Name);
538 Unam := Get_Unit_Name (Expr1);
540 Check_Arg_Is_String_Literal (Arg2);
542 if Chars (Arg2) = Name_Spec_File_Name then
543 Set_File_Name (Get_Spec_Name (Unam), Get_Fname (Arg2));
545 elsif Chars (Arg2) = Name_Body_File_Name then
546 Set_File_Name (Unam, Get_Fname (Arg2));
549 Error_Msg_N ("pragma% argument has incorrect identifier", Arg2);
553 -- If the first argument is not an identifier, then we must have
554 -- the pattern form of the pragma, and the first argument must be
555 -- the pattern string with an appropriate name.
558 if Chars (Arg1) = Name_Spec_File_Name then
561 elsif Chars (Arg1) = Name_Body_File_Name then
564 elsif Chars (Arg1) = Name_Subunit_File_Name then
567 elsif Chars (Arg1) = Name_Unit_Name then
569 ("Unit_Name parameter for pragma% must be an identifier",
574 Error_Msg_N ("pragma% argument has incorrect identifier", Arg1);
578 Pat := Get_String_Argument (Arg1);
580 -- Check pattern has exactly one asterisk
583 for J in Pat'Range loop
584 if Pat (J) = '*' then
591 ("file name pattern must have exactly one * character",
596 -- Set defaults for Casing and Dot_Separator parameters
598 Cas := All_Lower_Case;
600 Dot := new String'(".");
602 -- Process second and third arguments if present
604 if Arg_Count > 1 then
605 if Chars (Arg2) = Name_Casing then
606 Process_Casing (Arg2);
608 if Arg_Count = 3 then
609 Process_Dot_Replacement (Arg3);
613 Process_Dot_Replacement (Arg2);
615 if Arg_Count = 3 then
616 Process_Casing (Arg3);
621 Set_File_Name_Pattern (Pat, Typ, Dot, Cas);
623 end Source_File_Name;
625 -----------------------------
626 -- Source_Reference (GNAT) --
627 -----------------------------
629 -- pragma Source_Reference
630 -- (INTEGER_LITERAL [, STRING_LITERAL] );
632 -- Processing for this pragma must be done at parse time, since error
633 -- messages needing the proper line numbers can be generated in parse
634 -- only mode with semantic checking turned off, and indeed we usually
635 -- turn off semantic checking anyway if any parse errors are found.
637 when Pragma_Source_Reference => Source_Reference : declare
641 if Arg_Count /= 1 then
643 Check_No_Identifier (Arg2);
646 -- Check that this is first line of file. We skip this test if
647 -- we are in syntax check only mode, since we may be dealing with
648 -- multiple compilation units.
650 if Get_Physical_Line_Number (Pragma_Sloc) /= 1
651 and then Num_SRef_Pragmas (Current_Source_File) = 0
652 and then Operating_Mode /= Check_Syntax
655 ("first % pragma must be first line of file", Pragma_Sloc);
659 Check_No_Identifier (Arg1);
661 if Arg_Count = 1 then
662 if Num_SRef_Pragmas (Current_Source_File) = 0 then
664 ("file name required for first % pragma in file",
675 Check_Arg_Is_String_Literal (Arg2);
676 String_To_Name_Buffer (Strval (Expression (Arg2)));
679 if Num_SRef_Pragmas (Current_Source_File) > 0 then
680 if Fname /= Full_Ref_Name (Current_Source_File) then
682 ("file name must be same in all % pragmas", Pragma_Sloc);
688 if Nkind (Expression (Arg1)) /= N_Integer_Literal then
690 ("argument for pragma% must be integer literal",
691 Sloc (Expression (Arg1)));
694 -- OK, this source reference pragma is effective, however, we
695 -- ignore it if it is not in the first unit in the multiple unit
696 -- case. This is because the only purpose in this case is to
697 -- provide source pragmas for subsequent use by gnatchop.
700 if Num_Library_Units = 1 then
701 Register_Source_Ref_Pragma
703 Strip_Directory (Fname),
704 UI_To_Int (Intval (Expression (Arg1))),
705 Get_Physical_Line_Number (Pragma_Sloc) + 1);
708 end Source_Reference;
710 -------------------------
711 -- Style_Checks (GNAT) --
712 -------------------------
714 -- pragma Style_Checks (On | Off | ALL_CHECKS | STRING_LITERAL);
716 -- This is processed by the parser since some of the style
717 -- checks take place during source scanning and parsing.
719 when Pragma_Style_Checks => Style_Checks : declare
723 OK : Boolean := True;
726 -- Two argument case is only for semantics
728 if Arg_Count = 2 then
733 Check_No_Identifier (Arg1);
734 A := Expression (Arg1);
736 if Nkind (A) = N_String_Literal then
740 Slen : Natural := Natural (String_Length (S));
741 Options : String (1 .. Slen);
748 C := Get_String_Char (S, Int (J));
750 if not In_Character_Range (C) then
756 Options (J) := Get_Character (C);
760 Set_Style_Check_Options (Options, OK, Ptr);
770 ("invalid style check option",
771 Sloc (Expression (Arg1)) + Source_Ptr (Ptr));
776 elsif Nkind (A) /= N_Identifier then
779 elsif Chars (A) = Name_All_Checks then
780 Stylesw.Set_Default_Style_Check_Options;
782 elsif Chars (A) = Name_On then
785 elsif Chars (A) = Name_Off then
786 Style_Check := False;
793 Error_Msg ("incorrect argument for pragma%", Sloc (A));
799 ---------------------
800 -- Warnings (GNAT) --
801 ---------------------
803 -- pragma Warnings (On | Off, [LOCAL_NAME])
805 -- The one argument case is processed by the parser, since it may
806 -- control parser warnings as well as semantic warnings, and in any
807 -- case we want to be absolutely sure that the range in the warnings
808 -- table is set well before any semantic analysis is performed.
810 when Pragma_Warnings =>
811 if Arg_Count = 1 then
812 Check_No_Identifier (Arg1);
813 Check_Arg_Is_On_Or_Off (Arg1);
815 if Chars (Expression (Arg1)) = Name_On then
816 Set_Warnings_Mode_On (Pragma_Sloc);
818 Set_Warnings_Mode_Off (Pragma_Sloc);
822 -----------------------
823 -- All Other Pragmas --
824 -----------------------
826 -- For all other pragmas, checking and processing is handled
827 -- entirely in Sem_Prag, and no further checking is done by Par.
829 when Pragma_Abort_Defer |
831 Pragma_All_Calls_Remote |
834 Pragma_Asynchronous |
836 Pragma_Atomic_Components |
837 Pragma_Attach_Handler |
838 Pragma_Convention_Identifier |
840 Pragma_CPP_Constructor |
843 Pragma_C_Pass_By_Copy |
845 Pragma_Common_Object |
846 Pragma_Complex_Representation |
847 Pragma_Component_Alignment |
850 Pragma_Discard_Names |
853 Pragma_Elaborate_All |
854 Pragma_Elaborate_Body |
855 Pragma_Elaboration_Checks |
857 Pragma_Export_Exception |
858 Pragma_Export_Function |
859 Pragma_Export_Object |
860 Pragma_Export_Procedure |
861 Pragma_Export_Valued_Procedure |
862 Pragma_Extend_System |
864 Pragma_External_Name_Casing |
865 Pragma_Finalize_Storage_Only |
866 Pragma_Float_Representation |
869 Pragma_Import_Exception |
870 Pragma_Import_Function |
871 Pragma_Import_Object |
872 Pragma_Import_Procedure |
873 Pragma_Import_Valued_Procedure |
874 Pragma_Initialize_Scalars |
876 Pragma_Inline_Always |
877 Pragma_Inline_Generic |
878 Pragma_Inspection_Point |
880 Pragma_Interface_Name |
881 Pragma_Interrupt_Handler |
882 Pragma_Interrupt_Priority |
883 Pragma_Java_Constructor |
884 Pragma_Java_Interface |
887 Pragma_Linker_Alias |
888 Pragma_Linker_Options |
889 Pragma_Linker_Section |
890 Pragma_Locking_Policy |
892 Pragma_Machine_Attribute |
894 Pragma_Main_Storage |
898 Pragma_Normalize_Scalars |
903 Pragma_Preelaborate |
905 Pragma_Propagate_Exceptions |
906 Pragma_Psect_Object |
908 Pragma_Pure_Function |
909 Pragma_Queuing_Policy |
910 Pragma_Remote_Call_Interface |
911 Pragma_Remote_Types |
912 Pragma_Restrictions |
913 Pragma_Restricted_Run_Time |
916 Pragma_Share_Generic |
918 Pragma_Shared_Passive |
919 Pragma_Storage_Size |
920 Pragma_Storage_Unit |
921 Pragma_Stream_Convert |
924 Pragma_Suppress_All |
925 Pragma_Suppress_Debug_Info |
926 Pragma_Suppress_Initialization |
928 Pragma_Task_Dispatching_Policy |
931 Pragma_Task_Storage |
934 Pragma_Unchecked_Union |
935 Pragma_Unimplemented_Unit |
936 Pragma_Universal_Data |
937 Pragma_Unreferenced |
938 Pragma_Unreserve_All_Interrupts |
940 Pragma_Use_VADS_Size |
942 Pragma_Volatile_Components |
943 Pragma_Weak_External |
944 Pragma_Validity_Checks =>