1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 -- Generally the parser checks the basic syntax of pragmas, but does not
30 -- do specialized syntax checks for individual pragmas, these are deferred
31 -- to semantic analysis time (see unit Sem_Prag). There are some pragmas
32 -- which require recognition and either partial or complete processing
33 -- during parsing, and this unit performs this required processing.
35 with Fname
.UF
; use Fname
.UF
;
36 with Osint
; use Osint
;
37 with Stringt
; use Stringt
;
38 with Stylesw
; use Stylesw
;
39 with Uintp
; use Uintp
;
40 with Uname
; use Uname
;
44 function Prag
(Pragma_Node
: Node_Id
; Semi
: Source_Ptr
) return Node_Id
is
45 Pragma_Name
: constant Name_Id
:= Chars
(Pragma_Node
);
46 Pragma_Sloc
: constant Source_Ptr
:= Sloc
(Pragma_Node
);
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 function Arg1
return Node_Id
;
55 function Arg2
return Node_Id
;
56 function Arg3
return Node_Id
;
57 function Arg4
return Node_Id
;
58 -- Obtain specified Pragma_Argument_Association. It is allowable to call
59 -- the routine for the argument one past the last present argument, but
60 -- that is the only case in which a non-present argument can be referenced.
62 procedure Check_Arg_Count
(Required
: Int
);
63 -- Check argument count for pragma = Required.
64 -- If not give error and raise Error_Resync.
66 procedure Check_Arg_Is_String_Literal
(Arg
: Node_Id
);
67 -- Check the expression of the specified argument to make sure that it
68 -- is a string literal. If not give error and raise Error_Resync.
70 procedure Check_Arg_Is_On_Or_Off
(Arg
: Node_Id
);
71 -- Check the expression of the specified argument to make sure that it
72 -- is an identifier which is either ON or OFF, and if not, then issue
73 -- an error message and raise Error_Resync.
75 procedure Check_No_Identifier
(Arg
: Node_Id
);
76 -- Checks that the given argument does not have an identifier. If an
77 -- identifier is present, then an error message is issued, and
78 -- Error_Resync is raised.
80 procedure Check_Optional_Identifier
(Arg
: Node_Id
; Id
: Name_Id
);
81 -- Checks if the given argument has an identifier, and if so, requires
82 -- it to match the given identifier name. If there is a non-matching
83 -- identifier, then an error message is given and Error_Resync raised.
85 procedure Check_Required_Identifier
(Arg
: Node_Id
; Id
: Name_Id
);
86 -- Same as Check_Optional_Identifier, except that the name is required
87 -- to be present and to match the given Id value.
93 function Arg1
return Node_Id
is
95 return First
(Pragma_Argument_Associations
(Pragma_Node
));
102 function Arg2
return Node_Id
is
111 function Arg3
return Node_Id
is
120 function Arg4
return Node_Id
is
125 ---------------------
126 -- Check_Arg_Count --
127 ---------------------
129 procedure Check_Arg_Count
(Required
: Int
) is
131 if Arg_Count
/= Required
then
132 Error_Msg
("wrong number of arguments for pragma%", Pragma_Sloc
);
137 ----------------------------
138 -- Check_Arg_Is_On_Or_Off --
139 ----------------------------
141 procedure Check_Arg_Is_On_Or_Off
(Arg
: Node_Id
) is
142 Argx
: constant Node_Id
:= Expression
(Arg
);
145 if Nkind
(Expression
(Arg
)) /= N_Identifier
146 or else (Chars
(Argx
) /= Name_On
148 Chars
(Argx
) /= Name_Off
)
150 Error_Msg_Name_2
:= Name_On
;
151 Error_Msg_Name_3
:= Name_Off
;
154 ("argument for pragma% must be% or%", Sloc
(Argx
));
157 end Check_Arg_Is_On_Or_Off
;
159 ---------------------------------
160 -- Check_Arg_Is_String_Literal --
161 ---------------------------------
163 procedure Check_Arg_Is_String_Literal
(Arg
: Node_Id
) is
165 if Nkind
(Expression
(Arg
)) /= N_String_Literal
then
167 ("argument for pragma% must be string literal",
168 Sloc
(Expression
(Arg
)));
171 end Check_Arg_Is_String_Literal
;
173 -------------------------
174 -- Check_No_Identifier --
175 -------------------------
177 procedure Check_No_Identifier
(Arg
: Node_Id
) is
179 if Chars
(Arg
) /= No_Name
then
180 Error_Msg_N
("pragma% does not permit named arguments", Arg
);
183 end Check_No_Identifier
;
185 -------------------------------
186 -- Check_Optional_Identifier --
187 -------------------------------
189 procedure Check_Optional_Identifier
(Arg
: Node_Id
; Id
: Name_Id
) is
191 if Present
(Arg
) and then Chars
(Arg
) /= No_Name
then
192 if Chars
(Arg
) /= Id
then
193 Error_Msg_Name_2
:= Id
;
194 Error_Msg_N
("pragma% argument expects identifier%", Arg
);
197 end Check_Optional_Identifier
;
199 -------------------------------
200 -- Check_Required_Identifier --
201 -------------------------------
203 procedure Check_Required_Identifier
(Arg
: Node_Id
; Id
: Name_Id
) is
205 if Chars
(Arg
) /= Id
then
206 Error_Msg_Name_2
:= Id
;
207 Error_Msg_N
("pragma% argument must have identifier%", Arg
);
209 end Check_Required_Identifier
;
216 Error_Msg_Name_1
:= Pragma_Name
;
218 -- Count number of arguments. This loop also checks if any of the arguments
219 -- are Error, indicating a syntax error as they were parsed. If so, we
220 -- simply return, because we get into trouble with cascaded errors if we
221 -- try to perform our error checks on junk arguments.
225 if Present
(Pragma_Argument_Associations
(Pragma_Node
)) then
228 while Arg_Node
/= Empty
loop
229 Arg_Count
:= Arg_Count
+ 1;
231 if Expression
(Arg_Node
) = Error
then
239 -- Remaining processing is pragma dependent
241 case Get_Pragma_Id
(Pragma_Name
) is
247 -- This pragma must be processed at parse time, since we want to set
248 -- the Ada 83 and Ada 95 switches properly at parse time to recognize
249 -- Ada 83 syntax or Ada 95 syntax as appropriate.
251 when Pragma_Ada_83
=>
259 -- This pragma must be processed at parse time, since we want to set
260 -- the Ada 83 and Ada_95 switches properly at parse time to recognize
261 -- Ada 83 syntax or Ada 95 syntax as appropriate.
263 when Pragma_Ada_95
=>
271 -- pragma Debug (PROCEDURE_CALL_STATEMENT);
273 -- This has to be processed by the parser because of the very peculiar
274 -- form of the second parameter, which is syntactically from a formal
275 -- point of view a function call (since it must be an expression), but
276 -- semantically we treat it as a procedure call (which has exactly the
277 -- same syntactic form, so that's why we can get away with this!)
281 Check_No_Identifier
(Arg1
);
284 Expr
: constant Node_Id
:= New_Copy
(Expression
(Arg1
));
287 if Nkind
(Expr
) /= N_Indexed_Component
288 and then Nkind
(Expr
) /= N_Function_Call
289 and then Nkind
(Expr
) /= N_Identifier
290 and then Nkind
(Expr
) /= N_Selected_Component
293 ("argument of pragma% is not procedure call", Sloc
(Expr
));
297 (Pragma_Node
, P_Statement_Name
(Expr
));
301 -------------------------------
302 -- Extensions_Allowed (GNAT) --
303 -------------------------------
305 -- pragma Extensions_Allowed (Off | On)
307 -- The processing for pragma Extensions_Allowed must be done at
308 -- parse time, since extensions mode may affect what is accepted.
310 when Pragma_Extensions_Allowed
=>
312 Check_No_Identifier
(Arg1
);
313 Check_Arg_Is_On_Or_Off
(Arg1
);
314 Opt
.Extensions_Allowed
:= (Chars
(Expression
(Arg1
)) = Name_On
);
320 -- pragma List (Off | On)
322 -- The processing for pragma List must be done at parse time,
323 -- since a listing can be generated in parse only mode.
327 Check_No_Identifier
(Arg1
);
328 Check_Arg_Is_On_Or_Off
(Arg1
);
330 -- We unconditionally make a List_On entry for the pragma, so that
331 -- in the List (Off) case, the pragma will print even in a region
332 -- of code with listing turned off (this is required!)
334 List_Pragmas
.Increment_Last
;
335 List_Pragmas
.Table
(List_Pragmas
.Last
) :=
336 (Ptyp
=> List_On
, Ploc
=> Sloc
(Pragma_Node
));
338 -- Now generate the list off entry for pragma List (Off)
340 if Chars
(Expression
(Arg1
)) = Name_Off
then
341 List_Pragmas
.Increment_Last
;
342 List_Pragmas
.Table
(List_Pragmas
.Last
) :=
343 (Ptyp
=> List_Off
, Ploc
=> Semi
);
352 -- Processing for this pragma must be done at parse time, since a
353 -- listing can be generated in parse only mode with semantics off.
357 List_Pragmas
.Increment_Last
;
358 List_Pragmas
.Table
(List_Pragmas
.Last
) := (Page
, Semi
);
360 -----------------------------
361 -- Source_File_Name (GNAT) --
362 -----------------------------
364 -- There are five forms of this pragma:
366 -- pragma Source_File_Name (
367 -- [UNIT_NAME =>] unit_NAME,
368 -- BODY_FILE_NAME => STRING_LITERAL);
370 -- pragma Source_File_Name (
371 -- [UNIT_NAME =>] unit_NAME,
372 -- SPEC_FILE_NAME => STRING_LITERAL);
374 -- pragma Source_File_Name (
375 -- BODY_FILE_NAME => STRING_LITERAL
376 -- [, DOT_REPLACEMENT => STRING_LITERAL]
377 -- [, CASING => CASING_SPEC]);
379 -- pragma Source_File_Name (
380 -- SPEC_FILE_NAME => STRING_LITERAL
381 -- [, DOT_REPLACEMENT => STRING_LITERAL]
382 -- [, CASING => CASING_SPEC]);
384 -- pragma Source_File_Name (
385 -- SUBUNIT_FILE_NAME => STRING_LITERAL
386 -- [, DOT_REPLACEMENT => STRING_LITERAL]
387 -- [, CASING => CASING_SPEC]);
389 -- CASING_SPEC ::= Uppercase | Lowercase | Mixedcase
391 -- Note: we process this during parsing, since we need to have the
392 -- source file names set well before the semantic analysis starts,
393 -- since we load the spec and with'ed packages before analysis.
395 when Pragma_Source_File_Name
=> Source_File_Name
: declare
396 Unam
: Unit_Name_Type
;
404 function Get_Fname
(Arg
: Node_Id
) return Name_Id
;
405 -- Process file name from unit name form of pragma
407 function Get_String_Argument
(Arg
: Node_Id
) return String_Ptr
;
408 -- Process string literal value from argument
410 procedure Process_Casing
(Arg
: Node_Id
);
411 -- Process Casing argument of pattern form of pragma
413 procedure Process_Dot_Replacement
(Arg
: Node_Id
);
414 -- Process Dot_Replacement argument of patterm form of pragma
420 function Get_Fname
(Arg
: Node_Id
) return Name_Id
is
422 String_To_Name_Buffer
(Strval
(Expression
(Arg
)));
424 for J
in 1 .. Name_Len
loop
425 if Is_Directory_Separator
(Name_Buffer
(J
)) then
427 ("directory separator character not allowed",
428 Sloc
(Expression
(Arg
)) + Source_Ptr
(J
));
435 -------------------------
436 -- Get_String_Argument --
437 -------------------------
439 function Get_String_Argument
(Arg
: Node_Id
) return String_Ptr
is
443 if Nkind
(Expression
(Arg
)) /= N_String_Literal
445 Nkind
(Expression
(Arg
)) /= N_Operator_Symbol
448 ("argument for pragma% must be string literal", Arg
);
452 Str
:= Strval
(Expression
(Arg
));
454 -- Check string has no wide chars
456 for J
in 1 .. String_Length
(Str
) loop
457 if Get_String_Char
(Str
, J
) > 255 then
459 ("wide character not allowed in pattern for pragma%",
460 Sloc
(Expression
(Arg2
)) + Text_Ptr
(J
) - 1);
466 String_To_Name_Buffer
(Str
);
467 return new String'(Name_Buffer (1 .. Name_Len));
468 end Get_String_Argument;
474 procedure Process_Casing (Arg : Node_Id) is
475 Expr : constant Node_Id := Expression (Arg);
478 Check_Required_Identifier (Arg, Name_Casing);
480 if Nkind (Expr) = N_Identifier then
481 if Chars (Expr) = Name_Lowercase then
482 Cas := All_Lower_Case;
484 elsif Chars (Expr) = Name_Uppercase then
485 Cas := All_Upper_Case;
487 elsif Chars (Expr) = Name_Mixedcase then
494 ("Casing argument for pragma% must be " &
495 "one of Mixedcase, Lowercase, Uppercase",
499 -----------------------------
500 -- Process_Dot_Replacement --
501 -----------------------------
503 procedure Process_Dot_Replacement (Arg : Node_Id) is
505 Check_Required_Identifier (Arg, Name_Dot_Replacement);
506 Dot := Get_String_Argument (Arg);
507 end Process_Dot_Replacement;
509 -- Start of processing for Source_File_Name pragma
512 -- We permit from 1 to 3 arguments
514 if Arg_Count not in 1 .. 3 then
518 Expr1 := Expression (Arg1);
520 -- If first argument is identifier or selected component, then
521 -- we have the specific file case of the Source_File_Name pragma,
522 -- and the first argument is a unit name.
524 if Nkind (Expr1) = N_Identifier
526 (Nkind (Expr1) = N_Selected_Component
528 Nkind (Selector_Name (Expr1)) = N_Identifier)
532 Check_Optional_Identifier (Arg1, Name_Unit_Name);
533 Unam := Get_Unit_Name (Expr1);
535 Check_Arg_Is_String_Literal (Arg2);
537 if Chars (Arg2) = Name_Spec_File_Name then
538 Set_File_Name (Get_Spec_Name (Unam), Get_Fname (Arg2));
540 elsif Chars (Arg2) = Name_Body_File_Name then
541 Set_File_Name (Unam, Get_Fname (Arg2));
544 Error_Msg_N ("pragma% argument has incorrect identifier", Arg2);
548 -- If the first argument is not an identifier, then we must have
549 -- the pattern form of the pragma, and the first argument must be
550 -- the pattern string with an appropriate name.
553 if Chars (Arg1) = Name_Spec_File_Name then
556 elsif Chars (Arg1) = Name_Body_File_Name then
559 elsif Chars (Arg1) = Name_Subunit_File_Name then
562 elsif Chars (Arg1) = Name_Unit_Name then
564 ("Unit_Name parameter for pragma% must be an identifier",
569 Error_Msg_N ("pragma% argument has incorrect identifier", Arg1);
573 Pat := Get_String_Argument (Arg1);
575 -- Check pattern has exactly one asterisk
578 for J in Pat'Range loop
579 if Pat (J) = '*' then
586 ("file name pattern must have exactly one * character",
591 -- Set defaults for Casing and Dot_Separator parameters
593 Cas := All_Lower_Case;
595 Dot := new String'(".");
597 -- Process second and third arguments if present
599 if Arg_Count
> 1 then
600 if Chars
(Arg2
) = Name_Casing
then
601 Process_Casing
(Arg2
);
603 if Arg_Count
= 3 then
604 Process_Dot_Replacement
(Arg3
);
608 Process_Dot_Replacement
(Arg2
);
610 if Arg_Count
= 3 then
611 Process_Casing
(Arg3
);
616 Set_File_Name_Pattern
(Pat
, Typ
, Dot
, Cas
);
618 end Source_File_Name
;
620 -----------------------------
621 -- Source_Reference (GNAT) --
622 -----------------------------
624 -- pragma Source_Reference
625 -- (INTEGER_LITERAL [, STRING_LITERAL] );
627 -- Processing for this pragma must be done at parse time, since error
628 -- messages needing the proper line numbers can be generated in parse
629 -- only mode with semantic checking turned off, and indeed we usually
630 -- turn off semantic checking anyway if any parse errors are found.
632 when Pragma_Source_Reference
=> Source_Reference
: declare
636 if Arg_Count
/= 1 then
638 Check_No_Identifier
(Arg2
);
641 -- Check that this is first line of file. We skip this test if
642 -- we are in syntax check only mode, since we may be dealing with
643 -- multiple compilation units.
645 if Get_Physical_Line_Number
(Pragma_Sloc
) /= 1
646 and then Num_SRef_Pragmas
(Current_Source_File
) = 0
647 and then Operating_Mode
/= Check_Syntax
650 ("first % pragma must be first line of file", Pragma_Sloc
);
654 Check_No_Identifier
(Arg1
);
656 if Arg_Count
= 1 then
657 if Num_SRef_Pragmas
(Current_Source_File
) = 0 then
659 ("file name required for first % pragma in file",
670 Check_Arg_Is_String_Literal
(Arg2
);
671 String_To_Name_Buffer
(Strval
(Expression
(Arg2
)));
674 if Num_SRef_Pragmas
(Current_Source_File
) > 0 then
675 if Fname
/= Full_Ref_Name
(Current_Source_File
) then
677 ("file name must be same in all % pragmas", Pragma_Sloc
);
683 if Nkind
(Expression
(Arg1
)) /= N_Integer_Literal
then
685 ("argument for pragma% must be integer literal",
686 Sloc
(Expression
(Arg1
)));
689 -- OK, this source reference pragma is effective, however, we
690 -- ignore it if it is not in the first unit in the multiple unit
691 -- case. This is because the only purpose in this case is to
692 -- provide source pragmas for subsequent use by gnatchop.
695 if Num_Library_Units
= 1 then
696 Register_Source_Ref_Pragma
698 Strip_Directory
(Fname
),
699 UI_To_Int
(Intval
(Expression
(Arg1
))),
700 Get_Physical_Line_Number
(Pragma_Sloc
) + 1);
703 end Source_Reference
;
705 -------------------------
706 -- Style_Checks (GNAT) --
707 -------------------------
709 -- pragma Style_Checks (On | Off | ALL_CHECKS | STRING_LITERAL);
711 -- This is processed by the parser since some of the style
712 -- checks take place during source scanning and parsing.
714 when Pragma_Style_Checks
=> Style_Checks
: declare
718 OK
: Boolean := True;
721 -- Two argument case is only for semantics
723 if Arg_Count
= 2 then
728 Check_No_Identifier
(Arg1
);
729 A
:= Expression
(Arg1
);
731 if Nkind
(A
) = N_String_Literal
then
735 Slen
: Natural := Natural (String_Length
(S
));
736 Options
: String (1 .. Slen
);
743 C
:= Get_String_Char
(S
, Int
(J
));
745 if not In_Character_Range
(C
) then
751 Options
(J
) := Get_Character
(C
);
755 Set_Style_Check_Options
(Options
, OK
, Ptr
);
765 ("invalid style check option",
766 Sloc
(Expression
(Arg1
)) + Source_Ptr
(Ptr
));
771 elsif Nkind
(A
) /= N_Identifier
then
774 elsif Chars
(A
) = Name_All_Checks
then
775 Stylesw
.Set_Default_Style_Check_Options
;
777 elsif Chars
(A
) = Name_On
then
780 elsif Chars
(A
) = Name_Off
then
781 Style_Check
:= False;
788 Error_Msg
("incorrect argument for pragma%", Sloc
(A
));
794 ---------------------
795 -- Warnings (GNAT) --
796 ---------------------
798 -- pragma Warnings (On | Off, [LOCAL_NAME])
800 -- The one argument case is processed by the parser, since it may
801 -- control parser warnings as well as semantic warnings, and in any
802 -- case we want to be absolutely sure that the range in the warnings
803 -- table is set well before any semantic analysis is performed.
805 when Pragma_Warnings
=>
806 if Arg_Count
= 1 then
807 Check_No_Identifier
(Arg1
);
808 Check_Arg_Is_On_Or_Off
(Arg1
);
810 if Chars
(Expression
(Arg1
)) = Name_On
then
811 Set_Warnings_Mode_On
(Pragma_Sloc
);
813 Set_Warnings_Mode_Off
(Pragma_Sloc
);
817 -----------------------
818 -- All Other Pragmas --
819 -----------------------
821 -- For all other pragmas, checking and processing is handled
822 -- entirely in Sem_Prag, and no further checking is done by Par.
824 when Pragma_Abort_Defer |
826 Pragma_All_Calls_Remote |
829 Pragma_Asynchronous |
831 Pragma_Atomic_Components |
832 Pragma_Attach_Handler |
834 Pragma_CPP_Constructor |
837 Pragma_C_Pass_By_Copy |
839 Pragma_Common_Object |
840 Pragma_Complex_Representation |
841 Pragma_Component_Alignment |
844 Pragma_Discard_Names |
847 Pragma_Elaborate_All |
848 Pragma_Elaborate_Body |
849 Pragma_Elaboration_Checks |
851 Pragma_Export_Exception |
852 Pragma_Export_Function |
853 Pragma_Export_Object |
854 Pragma_Export_Procedure |
855 Pragma_Export_Valued_Procedure |
856 Pragma_Extend_System |
858 Pragma_External_Name_Casing |
859 Pragma_Finalize_Storage_Only |
860 Pragma_Float_Representation |
863 Pragma_Import_Exception |
864 Pragma_Import_Function |
865 Pragma_Import_Object |
866 Pragma_Import_Procedure |
867 Pragma_Import_Valued_Procedure |
868 Pragma_Initialize_Scalars |
870 Pragma_Inline_Always |
871 Pragma_Inline_Generic |
872 Pragma_Inspection_Point |
874 Pragma_Interface_Name |
875 Pragma_Interrupt_Handler |
876 Pragma_Interrupt_Priority |
877 Pragma_Java_Constructor |
878 Pragma_Java_Interface |
881 Pragma_Linker_Alias |
882 Pragma_Linker_Options |
883 Pragma_Linker_Section |
884 Pragma_Locking_Policy |
886 Pragma_Machine_Attribute |
888 Pragma_Main_Storage |
892 Pragma_Normalize_Scalars |
897 Pragma_Preelaborate |
899 Pragma_Propagate_Exceptions |
900 Pragma_Psect_Object |
902 Pragma_Pure_Function |
903 Pragma_Queuing_Policy |
904 Pragma_Remote_Call_Interface |
905 Pragma_Remote_Types |
906 Pragma_Restrictions |
907 Pragma_Restricted_Run_Time |
910 Pragma_Share_Generic |
912 Pragma_Shared_Passive |
913 Pragma_Storage_Size |
914 Pragma_Storage_Unit |
915 Pragma_Stream_Convert |
918 Pragma_Suppress_All |
919 Pragma_Suppress_Debug_Info |
920 Pragma_Suppress_Initialization |
922 Pragma_Task_Dispatching_Policy |
925 Pragma_Task_Storage |
928 Pragma_Unchecked_Union |
929 Pragma_Unimplemented_Unit |
930 Pragma_Unreserve_All_Interrupts |
932 Pragma_Use_VADS_Size |
934 Pragma_Volatile_Components |
935 Pragma_Weak_External |
936 Pragma_Validity_Checks
=>