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 with Atree
; use Atree
;
30 with Casing
; use Casing
;
31 with Einfo
; use Einfo
;
32 with Errout
; use Errout
;
33 with Exp_Ch11
; use Exp_Ch11
;
34 with Exp_Tss
; use Exp_Tss
;
35 with Exp_Util
; use Exp_Util
;
36 with Expander
; use Expander
;
37 with Namet
; use Namet
;
38 with Nlists
; use Nlists
;
39 with Nmake
; use Nmake
;
41 with Rtsfind
; use Rtsfind
;
43 with Sem_Eval
; use Sem_Eval
;
44 with Sem_Res
; use Sem_Res
;
45 with Sem_Util
; use Sem_Util
;
46 with Sinfo
; use Sinfo
;
47 with Sinput
; use Sinput
;
48 with Snames
; use Snames
;
49 with Stringt
; use Stringt
;
50 with Stand
; use Stand
;
51 with Tbuild
; use Tbuild
;
52 with Uintp
; use Uintp
;
54 package body Exp_Prag
is
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 function Arg1
(N
: Node_Id
) return Node_Id
;
61 function Arg2
(N
: Node_Id
) return Node_Id
;
62 function Arg3
(N
: Node_Id
) return Node_Id
;
63 -- Obtain specified Pragma_Argument_Association
65 procedure Expand_Pragma_Abort_Defer
(N
: Node_Id
);
66 procedure Expand_Pragma_Assert
(N
: Node_Id
);
67 procedure Expand_Pragma_Import
(N
: Node_Id
);
68 procedure Expand_Pragma_Import_Export_Exception
(N
: Node_Id
);
69 procedure Expand_Pragma_Inspection_Point
(N
: Node_Id
);
70 procedure Expand_Pragma_Interrupt_Priority
(N
: Node_Id
);
76 function Arg1
(N
: Node_Id
) return Node_Id
is
78 return First
(Pragma_Argument_Associations
(N
));
81 function Arg2
(N
: Node_Id
) return Node_Id
is
83 return Next
(Arg1
(N
));
86 function Arg3
(N
: Node_Id
) return Node_Id
is
88 return Next
(Arg2
(N
));
95 procedure Expand_N_Pragma
(N
: Node_Id
) is
97 -- Note: we may have a pragma whose chars field is not a
98 -- recognized pragma, and we must ignore it at this stage.
100 if Is_Pragma_Name
(Chars
(N
)) then
101 case Get_Pragma_Id
(Chars
(N
)) is
103 -- Pragmas requiring special expander action
105 when Pragma_Abort_Defer
=>
106 Expand_Pragma_Abort_Defer
(N
);
108 when Pragma_Assert
=>
109 Expand_Pragma_Assert
(N
);
111 when Pragma_Export_Exception
=>
112 Expand_Pragma_Import_Export_Exception
(N
);
114 when Pragma_Import
=>
115 Expand_Pragma_Import
(N
);
117 when Pragma_Import_Exception
=>
118 Expand_Pragma_Import_Export_Exception
(N
);
120 when Pragma_Inspection_Point
=>
121 Expand_Pragma_Inspection_Point
(N
);
123 when Pragma_Interrupt_Priority
=>
124 Expand_Pragma_Interrupt_Priority
(N
);
126 -- All other pragmas need no expander action
134 -------------------------------
135 -- Expand_Pragma_Abort_Defer --
136 -------------------------------
138 -- An Abort_Defer pragma appears as the first statement in a handled
139 -- statement sequence (right after the begin). It defers aborts for
140 -- the entire statement sequence, but not for any declarations or
141 -- handlers (if any) associated with this statement sequence.
143 -- The transformation is to transform
145 -- pragma Abort_Defer;
154 -- when all others =>
155 -- Abort_Undefer.all;
158 -- Abort_Undefer_Direct;
161 procedure Expand_Pragma_Abort_Defer
(N
: Node_Id
) is
162 Loc
: constant Source_Ptr
:= Sloc
(N
);
166 Blk
: constant Entity_Id
:=
167 New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
170 Stms
:= New_List
(Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
173 Stm
:= Remove_Next
(N
);
179 Make_Handled_Sequence_Of_Statements
(Loc
,
182 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
185 Make_Block_Statement
(Loc
,
186 Handled_Statement_Sequence
=> HSS
));
188 Set_Scope
(Blk
, Current_Scope
);
189 Set_Etype
(Blk
, Standard_Void_Type
);
190 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
191 Expand_At_End_Handler
(HSS
, Blk
);
193 end Expand_Pragma_Abort_Defer
;
195 --------------------------
196 -- Expand_Pragma_Assert --
197 --------------------------
199 procedure Expand_Pragma_Assert
(N
: Node_Id
) is
200 Loc
: constant Source_Ptr
:= Sloc
(N
);
201 Cond
: constant Node_Id
:= Expression
(Arg1
(N
));
205 -- We already know that assertions are enabled, because otherwise
206 -- the semantic pass dealt with rewriting the assertion (see Sem_Prag)
208 pragma Assert
(Assertions_Enabled
);
210 -- Since assertions are on, we rewrite the pragma with its
211 -- corresponding if statement, and then analyze the statement
212 -- The expansion transforms:
214 -- pragma Assert (condition [,message]);
218 -- if not condition then
219 -- System.Assertions.Raise_Assert_Failure (Str);
222 -- where Str is the message if one is present, or the default of
223 -- file:line if no message is given.
225 -- First, we need to prepare the character literal
227 if Present
(Arg2
(N
)) then
228 Msg
:= Strval
(Expr_Value_S
(Expression
(Arg2
(N
))));
230 Build_Location_String
(Loc
);
231 Msg
:= String_From_Name_Buffer
;
234 -- Now generate the if statement. Note that we consider this to be
235 -- an explicit conditional in the source, not an implicit if, so we
236 -- do not call Make_Implicit_If_Statement.
239 Make_If_Statement
(Loc
,
243 Then_Statements
=> New_List
(
244 Make_Procedure_Call_Statement
(Loc
,
246 New_Reference_To
(RTE
(RE_Raise_Assert_Failure
), Loc
),
247 Parameter_Associations
=> New_List
(
248 Make_String_Literal
(Loc
, Msg
))))));
252 -- If new condition is always false, give a warning
254 if Nkind
(N
) = N_Procedure_Call_Statement
255 and then Is_RTE
(Entity
(Name
(N
)), RE_Raise_Assert_Failure
)
257 -- If original condition was a Standard.False, we assume
258 -- that this is indeed intented to raise assert error
259 -- and no warning is required.
261 if Is_Entity_Name
(Original_Node
(Cond
))
262 and then Entity
(Original_Node
(Cond
)) = Standard_False
266 Error_Msg_N
("?assertion will fail at run-time", N
);
269 end Expand_Pragma_Assert
;
271 --------------------------
272 -- Expand_Pragma_Import --
273 --------------------------
275 -- When applied to a variable, the default initialization must not be
276 -- done. As it is already done when the pragma is found, we just get rid
277 -- of the call the initialization procedure which followed the object
280 -- We can't use the freezing mechanism for this purpose, since we
281 -- have to elaborate the initialization expression when it is first
282 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
284 procedure Expand_Pragma_Import
(N
: Node_Id
) is
285 Def_Id
: constant Entity_Id
:= Entity
(Expression
(Arg2
(N
)));
290 if Ekind
(Def_Id
) = E_Variable
then
291 Typ
:= Etype
(Def_Id
);
292 After_Def
:= Next
(Parent
(Def_Id
));
294 if Has_Non_Null_Base_Init_Proc
(Typ
)
295 and then Nkind
(After_Def
) = N_Procedure_Call_Statement
296 and then Is_Entity_Name
(Name
(After_Def
))
297 and then Entity
(Name
(After_Def
)) = Base_Init_Proc
(Typ
)
301 elsif Is_Access_Type
(Typ
) then
302 Set_Expression
(Parent
(Def_Id
), Empty
);
305 end Expand_Pragma_Import
;
307 -------------------------------------------
308 -- Expand_Pragma_Import_Export_Exception --
309 -------------------------------------------
311 -- For a VMS exception fix up the language field with "VMS"
312 -- instead of "Ada" (gigi needs this), create a constant that will be the
313 -- value of the VMS condition code and stuff the Interface_Name field
314 -- with the unexpanded name of the exception (if not already set).
315 -- For a Ada exception, just stuff the Interface_Name field
316 -- with the unexpanded name of the exception (if not already set).
318 procedure Expand_Pragma_Import_Export_Exception
(N
: Node_Id
) is
319 Id
: constant Entity_Id
:= Entity
(Expression
(Arg1
(N
)));
320 Call
: constant Node_Id
:= Register_Exception_Call
(Id
);
321 Loc
: constant Source_Ptr
:= Sloc
(N
);
323 if Present
(Call
) then
325 Excep_Internal
: constant Node_Id
:=
326 Make_Defining_Identifier
327 (Loc
, New_Internal_Name
('V'));
328 Export_Pragma
: Node_Id
;
329 Excep_Alias
: Node_Id
;
330 Excep_Object
: Node_Id
;
331 Excep_Image
: String_Id
;
338 if Present
(Interface_Name
(Id
)) then
339 Excep_Image
:= Strval
(Interface_Name
(Id
));
341 Get_Name_String
(Chars
(Id
));
343 Excep_Image
:= String_From_Name_Buffer
;
346 Exdata
:= Component_Associations
(Expression
(Parent
(Id
)));
348 if Is_VMS_Exception
(Id
) then
350 Lang1
:= Next
(First
(Exdata
));
351 Lang2
:= Next
(Lang1
);
352 Lang3
:= Next
(Lang2
);
354 Rewrite
(Expression
(Lang1
),
355 Make_Character_Literal
(Loc
, Name_uV
, Get_Char_Code
('V')));
356 Analyze
(Expression
(Lang1
));
358 Rewrite
(Expression
(Lang2
),
359 Make_Character_Literal
(Loc
, Name_uM
, Get_Char_Code
('M')));
360 Analyze
(Expression
(Lang2
));
362 Rewrite
(Expression
(Lang3
),
363 Make_Character_Literal
(Loc
, Name_uS
, Get_Char_Code
('S')));
364 Analyze
(Expression
(Lang3
));
366 if Exception_Code
(Id
) /= No_Uint
then
367 Code
:= Make_Integer_Literal
(Loc
, Exception_Code
(Id
));
370 Make_Object_Declaration
(Loc
,
371 Defining_Identifier
=> Excep_Internal
,
373 New_Reference_To
(Standard_Integer
, Loc
));
375 Insert_Action
(N
, Excep_Object
);
376 Analyze
(Excep_Object
);
379 Store_String_Int
(UI_To_Int
(Exception_Code
(Id
)) / 8 * 8);
386 (Make_Pragma_Argument_Association
389 New_Reference_To
(Excep_Internal
, Loc
)),
390 Make_Pragma_Argument_Association
395 Strval
=> End_String
))));
397 Insert_Action
(N
, Excep_Alias
);
398 Analyze
(Excep_Alias
);
405 (Make_Pragma_Argument_Association
407 Expression
=> Make_Identifier
(Loc
, Name_C
)),
408 Make_Pragma_Argument_Association
411 New_Reference_To
(Excep_Internal
, Loc
)),
412 Make_Pragma_Argument_Association
417 Strval
=> Excep_Image
)),
418 Make_Pragma_Argument_Association
423 Strval
=> Excep_Image
))));
425 Insert_Action
(N
, Export_Pragma
);
426 Analyze
(Export_Pragma
);
430 Unchecked_Convert_To
(Standard_Integer
,
431 Make_Function_Call
(Loc
,
433 New_Reference_To
(RTE
(RE_Import_Value
), Loc
),
434 Parameter_Associations
=> New_List
435 (Make_String_Literal
(Loc
,
436 Strval
=> Excep_Image
))));
440 Make_Procedure_Call_Statement
(Loc
,
441 Name
=> New_Reference_To
442 (RTE
(RE_Register_VMS_Exception
), Loc
),
443 Parameter_Associations
=> New_List
(Code
)));
445 Analyze_And_Resolve
(Code
, Standard_Integer
);
450 if not Present
(Interface_Name
(Id
)) then
451 Set_Interface_Name
(Id
,
454 Strval
=> Excep_Image
));
458 end Expand_Pragma_Import_Export_Exception
;
460 ------------------------------------
461 -- Expand_Pragma_Inspection_Point --
462 ------------------------------------
464 -- If no argument is given, then we supply a default argument list that
465 -- includes all objects declared at the source level in all subprograms
466 -- that enclose the inspection point pragma.
468 procedure Expand_Pragma_Inspection_Point
(N
: Node_Id
) is
469 Loc
: constant Source_Ptr
:= Sloc
(N
);
476 if No
(Pragma_Argument_Associations
(N
)) then
480 while S
/= Standard_Standard
loop
481 E
:= First_Entity
(S
);
482 while Present
(E
) loop
483 if Comes_From_Source
(E
)
484 and then Is_Object
(E
)
485 and then not Is_Entry_Formal
(E
)
486 and then Ekind
(E
) /= E_Component
487 and then Ekind
(E
) /= E_Discriminant
488 and then Ekind
(E
) /= E_Generic_In_Parameter
489 and then Ekind
(E
) /= E_Generic_In_Out_Parameter
492 Make_Pragma_Argument_Association
(Loc
,
493 Expression
=> New_Occurrence_Of
(E
, Loc
)));
502 Set_Pragma_Argument_Associations
(N
, A
);
505 -- Expand the arguments of the pragma. Expanding an entity reference
506 -- is a noop, except in a protected operation, where a reference may
507 -- have to be transformed into a reference to the corresponding prival.
508 -- Are there other pragmas that may require this ???
510 Assoc
:= First
(Pragma_Argument_Associations
(N
));
512 while Present
(Assoc
) loop
513 Expand
(Expression
(Assoc
));
516 end Expand_Pragma_Inspection_Point
;
518 --------------------------------------
519 -- Expand_Pragma_Interrupt_Priority --
520 --------------------------------------
522 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
524 procedure Expand_Pragma_Interrupt_Priority
(N
: Node_Id
) is
525 Loc
: constant Source_Ptr
:= Sloc
(N
);
528 if No
(Pragma_Argument_Associations
(N
)) then
529 Set_Pragma_Argument_Associations
(N
, New_List
(
530 Make_Pragma_Argument_Association
(Loc
,
532 Make_Attribute_Reference
(Loc
,
534 New_Occurrence_Of
(RTE
(RE_Interrupt_Priority
), Loc
),
535 Attribute_Name
=> Name_Last
))));
537 end Expand_Pragma_Interrupt_Priority
;