1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Casing
; use Casing
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Errout
; use Errout
;
31 with Exp_Ch11
; use Exp_Ch11
;
32 with Exp_Tss
; use Exp_Tss
;
33 with Exp_Util
; use Exp_Util
;
34 with Expander
; use Expander
;
35 with Namet
; use Namet
;
36 with Nlists
; use Nlists
;
37 with Nmake
; use Nmake
;
39 with Restrict
; use Restrict
;
40 with Rident
; use Rident
;
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 Targparm
; use Targparm
;
52 with Tbuild
; use Tbuild
;
53 with Uintp
; use Uintp
;
55 package body Exp_Prag
is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 function Arg1
(N
: Node_Id
) return Node_Id
;
62 function Arg2
(N
: Node_Id
) return Node_Id
;
63 -- Obtain specified pragma argument expression
65 procedure Expand_Pragma_Abort_Defer
(N
: Node_Id
);
66 procedure Expand_Pragma_Assert
(N
: Node_Id
);
67 procedure Expand_Pragma_Common_Object
(N
: Node_Id
);
68 procedure Expand_Pragma_Import_Or_Interface
(N
: Node_Id
);
69 procedure Expand_Pragma_Import_Export_Exception
(N
: Node_Id
);
70 procedure Expand_Pragma_Inspection_Point
(N
: Node_Id
);
71 procedure Expand_Pragma_Interrupt_Priority
(N
: Node_Id
);
72 procedure Expand_Pragma_Psect_Object
(N
: Node_Id
);
78 function Arg1
(N
: Node_Id
) return Node_Id
is
79 Arg
: constant Node_Id
:= First
(Pragma_Argument_Associations
(N
));
82 and then Nkind
(Arg
) = N_Pragma_Argument_Association
84 return Expression
(Arg
);
94 function Arg2
(N
: Node_Id
) return Node_Id
is
95 Arg1
: constant Node_Id
:= First
(Pragma_Argument_Associations
(N
));
101 Arg
: constant Node_Id
:= Next
(Arg1
);
104 and then Nkind
(Arg
) = N_Pragma_Argument_Association
106 return Expression
(Arg
);
114 ---------------------
115 -- Expand_N_Pragma --
116 ---------------------
118 procedure Expand_N_Pragma
(N
: Node_Id
) is
120 -- Note: we may have a pragma whose chars field is not a
121 -- recognized pragma, and we must ignore it at this stage.
123 if Is_Pragma_Name
(Chars
(N
)) then
124 case Get_Pragma_Id
(Chars
(N
)) is
126 -- Pragmas requiring special expander action
128 when Pragma_Abort_Defer
=>
129 Expand_Pragma_Abort_Defer
(N
);
131 when Pragma_Assert
=>
132 Expand_Pragma_Assert
(N
);
134 when Pragma_Common_Object
=>
135 Expand_Pragma_Common_Object
(N
);
137 when Pragma_Export_Exception
=>
138 Expand_Pragma_Import_Export_Exception
(N
);
140 when Pragma_Import
=>
141 Expand_Pragma_Import_Or_Interface
(N
);
143 when Pragma_Import_Exception
=>
144 Expand_Pragma_Import_Export_Exception
(N
);
146 when Pragma_Inspection_Point
=>
147 Expand_Pragma_Inspection_Point
(N
);
149 when Pragma_Interface
=>
150 Expand_Pragma_Import_Or_Interface
(N
);
152 when Pragma_Interrupt_Priority
=>
153 Expand_Pragma_Interrupt_Priority
(N
);
155 when Pragma_Psect_Object
=>
156 Expand_Pragma_Psect_Object
(N
);
158 -- All other pragmas need no expander action
166 -------------------------------
167 -- Expand_Pragma_Abort_Defer --
168 -------------------------------
170 -- An Abort_Defer pragma appears as the first statement in a handled
171 -- statement sequence (right after the begin). It defers aborts for
172 -- the entire statement sequence, but not for any declarations or
173 -- handlers (if any) associated with this statement sequence.
175 -- The transformation is to transform
177 -- pragma Abort_Defer;
186 -- when all others =>
187 -- Abort_Undefer.all;
190 -- Abort_Undefer_Direct;
193 procedure Expand_Pragma_Abort_Defer
(N
: Node_Id
) is
194 Loc
: constant Source_Ptr
:= Sloc
(N
);
198 Blk
: constant Entity_Id
:=
199 New_Internal_Entity
(E_Block
, Current_Scope
, Sloc
(N
), 'B');
202 Stms
:= New_List
(Build_Runtime_Call
(Loc
, RE_Abort_Defer
));
205 Stm
:= Remove_Next
(N
);
211 Make_Handled_Sequence_Of_Statements
(Loc
,
214 New_Occurrence_Of
(RTE
(RE_Abort_Undefer_Direct
), Loc
));
217 Make_Block_Statement
(Loc
,
218 Handled_Statement_Sequence
=> HSS
));
220 Set_Scope
(Blk
, Current_Scope
);
221 Set_Etype
(Blk
, Standard_Void_Type
);
222 Set_Identifier
(N
, New_Occurrence_Of
(Blk
, Sloc
(N
)));
223 Expand_At_End_Handler
(HSS
, Blk
);
225 end Expand_Pragma_Abort_Defer
;
227 --------------------------
228 -- Expand_Pragma_Assert --
229 --------------------------
231 procedure Expand_Pragma_Assert
(N
: Node_Id
) is
232 Loc
: constant Source_Ptr
:= Sloc
(N
);
233 Cond
: constant Node_Id
:= Arg1
(N
);
237 -- We already know that assertions are enabled, because otherwise
238 -- the semantic pass dealt with rewriting the assertion (see Sem_Prag)
240 pragma Assert
(Assertions_Enabled
);
242 -- Since assertions are on, we rewrite the pragma with its
243 -- corresponding if statement, and then analyze the statement
244 -- The normal case expansion transforms:
246 -- pragma Assert (condition [,message]);
250 -- if not condition then
251 -- System.Assertions.Raise_Assert_Failure (Str);
254 -- where Str is the message if one is present, or the default of
255 -- file:line if no message is given.
257 -- An alternative expansion is used when the No_Exception_Propagation
258 -- restriction is active and there is a local Assert_Failure handler.
259 -- This is not a common combination of circumstances, but it occurs in
260 -- the context of Aunit and the zero footprint profile. In this case we
263 -- if not condition then
264 -- raise Assert_Failure;
267 -- This will then be transformed into a goto, and the local handler will
268 -- be able to handle the assert error (which would not be the case if a
269 -- call is made to the Raise_Assert_Failure procedure).
271 -- Note that the reason we do not always generate a direct raise is that
272 -- the form in which the procedure is called allows for more efficient
273 -- breakpointing of assertion errors.
275 -- Generate the appropriate if statement. Note that we consider this to
276 -- be an explicit conditional in the source, not an implicit if, so we
277 -- do not call Make_Implicit_If_Statement.
279 -- Case where we generate a direct raise
282 or else Restriction_Active
(No_Exception_Propagation
))
283 and then Present
(Find_Local_Handler
(RTE
(RE_Assert_Failure
), N
))
286 Make_If_Statement
(Loc
,
290 Then_Statements
=> New_List
(
291 Make_Raise_Statement
(Loc
,
293 New_Reference_To
(RTE
(RE_Assert_Failure
), Loc
)))));
295 -- Case where we call the procedure
298 -- First, we need to prepare the string literal
300 if Present
(Arg2
(N
)) then
301 Msg
:= Strval
(Expr_Value_S
(Arg2
(N
)));
303 Build_Location_String
(Loc
);
304 Msg
:= String_From_Name_Buffer
;
307 -- Now rewrite as an if statement
310 Make_If_Statement
(Loc
,
314 Then_Statements
=> New_List
(
315 Make_Procedure_Call_Statement
(Loc
,
317 New_Reference_To
(RTE
(RE_Raise_Assert_Failure
), Loc
),
318 Parameter_Associations
=> New_List
(
319 Make_String_Literal
(Loc
, Msg
))))));
324 -- If new condition is always false, give a warning
326 if Nkind
(N
) = N_Procedure_Call_Statement
327 and then Is_RTE
(Entity
(Name
(N
)), RE_Raise_Assert_Failure
)
329 -- If original condition was a Standard.False, we assume that this is
330 -- indeed intented to raise assert error and no warning is required.
332 if Is_Entity_Name
(Original_Node
(Cond
))
333 and then Entity
(Original_Node
(Cond
)) = Standard_False
337 Error_Msg_N
("?assertion will fail at run-time", N
);
340 end Expand_Pragma_Assert
;
342 ---------------------------------
343 -- Expand_Pragma_Common_Object --
344 ---------------------------------
346 -- Use a machine attribute to replicate semantic effect in DEC Ada
348 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
350 -- For now we do nothing with the size attribute ???
352 procedure Expand_Pragma_Common_Object
(N
: Node_Id
) is
353 Loc
: constant Source_Ptr
:= Sloc
(N
);
355 Internal
: constant Node_Id
:= Arg1
(N
);
356 External
: constant Node_Id
:= Arg2
(N
);
359 -- Psect value upper cased as string literal
361 Iloc
: constant Source_Ptr
:= Sloc
(Internal
);
362 Eloc
: constant Source_Ptr
:= Sloc
(External
);
366 -- Acquire Psect value and fold to upper case
368 if Present
(External
) then
369 if Nkind
(External
) = N_String_Literal
then
370 String_To_Name_Buffer
(Strval
(External
));
372 Get_Name_String
(Chars
(External
));
378 Make_String_Literal
(Eloc
,
379 Strval
=> String_From_Name_Buffer
);
382 Get_Name_String
(Chars
(Internal
));
385 Make_String_Literal
(Iloc
,
386 Strval
=> String_From_Name_Buffer
);
389 Ploc
:= Sloc
(Psect
);
393 Insert_After_And_Analyze
(N
,
396 Chars
=> Name_Machine_Attribute
,
397 Pragma_Argument_Associations
=> New_List
(
398 Make_Pragma_Argument_Association
(Iloc
,
399 Expression
=> New_Copy_Tree
(Internal
)),
400 Make_Pragma_Argument_Association
(Eloc
,
402 Make_String_Literal
(Sloc
=> Ploc
,
403 Strval
=> "common_object")),
404 Make_Pragma_Argument_Association
(Ploc
,
405 Expression
=> New_Copy_Tree
(Psect
)))));
407 end Expand_Pragma_Common_Object
;
409 ---------------------------------------
410 -- Expand_Pragma_Import_Or_Interface --
411 ---------------------------------------
413 -- When applied to a variable, the default initialization must not be
414 -- done. As it is already done when the pragma is found, we just get rid
415 -- of the call the initialization procedure which followed the object
416 -- declaration. The call is inserted after the declaration, but validity
417 -- checks may also have been inserted and the initialization call does
418 -- not necessarily appear immediately after the object declaration.
420 -- We can't use the freezing mechanism for this purpose, since we
421 -- have to elaborate the initialization expression when it is first
422 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
424 procedure Expand_Pragma_Import_Or_Interface
(N
: Node_Id
) is
425 Def_Id
: constant Entity_Id
:= Entity
(Arg2
(N
));
430 if Ekind
(Def_Id
) = E_Variable
then
431 Typ
:= Etype
(Def_Id
);
433 -- Iterate from declaration of object to import pragma, to find
434 -- generated initialization call for object, if any.
436 Init_Call
:= Next
(Parent
(Def_Id
));
437 while Present
(Init_Call
) and then Init_Call
/= N
loop
438 if Has_Non_Null_Base_Init_Proc
(Typ
)
439 and then Nkind
(Init_Call
) = N_Procedure_Call_Statement
440 and then Is_Entity_Name
(Name
(Init_Call
))
441 and then Entity
(Name
(Init_Call
)) = Base_Init_Proc
(Typ
)
450 -- Any default initialization expression should be removed
451 -- (e.g., null defaults for access objects, zero initialization
452 -- of packed bit arrays). Imported objects aren't allowed to
453 -- have explicit initialization, so the expression must have
454 -- been generated by the compiler.
457 and then Present
(Expression
(Parent
(Def_Id
)))
459 Set_Expression
(Parent
(Def_Id
), Empty
);
462 end Expand_Pragma_Import_Or_Interface
;
464 -------------------------------------------
465 -- Expand_Pragma_Import_Export_Exception --
466 -------------------------------------------
468 -- For a VMS exception fix up the language field with "VMS"
469 -- instead of "Ada" (gigi needs this), create a constant that will be the
470 -- value of the VMS condition code and stuff the Interface_Name field
471 -- with the unexpanded name of the exception (if not already set).
472 -- For a Ada exception, just stuff the Interface_Name field
473 -- with the unexpanded name of the exception (if not already set).
475 procedure Expand_Pragma_Import_Export_Exception
(N
: Node_Id
) is
477 -- This pragma is only effective on OpenVMS systems, it was ignored
478 -- on non-VMS systems, and we need to ignore it here as well.
480 if not OpenVMS_On_Target
then
485 Id
: constant Entity_Id
:= Entity
(Arg1
(N
));
486 Call
: constant Node_Id
:= Register_Exception_Call
(Id
);
487 Loc
: constant Source_Ptr
:= Sloc
(N
);
490 if Present
(Call
) then
492 Excep_Internal
: constant Node_Id
:=
493 Make_Defining_Identifier
494 (Loc
, New_Internal_Name
('V'));
495 Export_Pragma
: Node_Id
;
496 Excep_Alias
: Node_Id
;
497 Excep_Object
: Node_Id
;
498 Excep_Image
: String_Id
;
506 if Present
(Interface_Name
(Id
)) then
507 Excep_Image
:= Strval
(Interface_Name
(Id
));
509 Get_Name_String
(Chars
(Id
));
511 Excep_Image
:= String_From_Name_Buffer
;
514 Exdata
:= Component_Associations
(Expression
(Parent
(Id
)));
516 if Is_VMS_Exception
(Id
) then
517 Lang1
:= Next
(First
(Exdata
));
518 Lang2
:= Next
(Lang1
);
519 Lang3
:= Next
(Lang2
);
521 Rewrite
(Expression
(Lang1
),
522 Make_Character_Literal
(Loc
,
524 Char_Literal_Value
=>
525 UI_From_Int
(Character'Pos ('V'))));
526 Analyze
(Expression
(Lang1
));
528 Rewrite
(Expression
(Lang2
),
529 Make_Character_Literal
(Loc
,
531 Char_Literal_Value
=>
532 UI_From_Int
(Character'Pos ('M'))));
533 Analyze
(Expression
(Lang2
));
535 Rewrite
(Expression
(Lang3
),
536 Make_Character_Literal
(Loc
,
538 Char_Literal_Value
=>
539 UI_From_Int
(Character'Pos ('S'))));
540 Analyze
(Expression
(Lang3
));
542 if Exception_Code
(Id
) /= No_Uint
then
544 Make_Integer_Literal
(Loc
,
545 Intval
=> Exception_Code
(Id
));
548 Make_Object_Declaration
(Loc
,
549 Defining_Identifier
=> Excep_Internal
,
551 New_Reference_To
(RTE
(RE_Exception_Code
), Loc
));
553 Insert_Action
(N
, Excep_Object
);
554 Analyze
(Excep_Object
);
558 (UI_To_Int
(Exception_Code
(Id
)) / 8 * 8);
565 (Make_Pragma_Argument_Association
568 New_Reference_To
(Excep_Internal
, Loc
)),
570 Make_Pragma_Argument_Association
575 Strval
=> End_String
))));
577 Insert_Action
(N
, Excep_Alias
);
578 Analyze
(Excep_Alias
);
585 (Make_Pragma_Argument_Association
587 Expression
=> Make_Identifier
(Loc
, Name_C
)),
589 Make_Pragma_Argument_Association
592 New_Reference_To
(Excep_Internal
, Loc
)),
594 Make_Pragma_Argument_Association
599 Strval
=> Excep_Image
)),
601 Make_Pragma_Argument_Association
606 Strval
=> Excep_Image
))));
608 Insert_Action
(N
, Export_Pragma
);
609 Analyze
(Export_Pragma
);
613 Unchecked_Convert_To
(RTE
(RE_Exception_Code
),
614 Make_Function_Call
(Loc
,
616 New_Reference_To
(RTE
(RE_Import_Value
), Loc
),
617 Parameter_Associations
=> New_List
618 (Make_String_Literal
(Loc
,
619 Strval
=> Excep_Image
))));
623 Make_Procedure_Call_Statement
(Loc
,
624 Name
=> New_Reference_To
625 (RTE
(RE_Register_VMS_Exception
), Loc
),
626 Parameter_Associations
=> New_List
(
628 Unchecked_Convert_To
(RTE
(RE_Exception_Data_Ptr
),
629 Make_Attribute_Reference
(Loc
,
630 Prefix
=> New_Occurrence_Of
(Id
, Loc
),
631 Attribute_Name
=> Name_Unrestricted_Access
)))));
633 Analyze_And_Resolve
(Code
, RTE
(RE_Exception_Code
));
637 if No
(Interface_Name
(Id
)) then
638 Set_Interface_Name
(Id
,
641 Strval
=> Excep_Image
));
646 end Expand_Pragma_Import_Export_Exception
;
648 ------------------------------------
649 -- Expand_Pragma_Inspection_Point --
650 ------------------------------------
652 -- If no argument is given, then we supply a default argument list that
653 -- includes all objects declared at the source level in all subprograms
654 -- that enclose the inspection point pragma.
656 procedure Expand_Pragma_Inspection_Point
(N
: Node_Id
) is
657 Loc
: constant Source_Ptr
:= Sloc
(N
);
664 if No
(Pragma_Argument_Associations
(N
)) then
668 while S
/= Standard_Standard
loop
669 E
:= First_Entity
(S
);
670 while Present
(E
) loop
671 if Comes_From_Source
(E
)
672 and then Is_Object
(E
)
673 and then not Is_Entry_Formal
(E
)
674 and then Ekind
(E
) /= E_Component
675 and then Ekind
(E
) /= E_Discriminant
676 and then Ekind
(E
) /= E_Generic_In_Parameter
677 and then Ekind
(E
) /= E_Generic_In_Out_Parameter
680 Make_Pragma_Argument_Association
(Loc
,
681 Expression
=> New_Occurrence_Of
(E
, Loc
)));
690 Set_Pragma_Argument_Associations
(N
, A
);
693 -- Expand the arguments of the pragma. Expanding an entity reference
694 -- is a noop, except in a protected operation, where a reference may
695 -- have to be transformed into a reference to the corresponding prival.
696 -- Are there other pragmas that may require this ???
698 Assoc
:= First
(Pragma_Argument_Associations
(N
));
700 while Present
(Assoc
) loop
701 Expand
(Expression
(Assoc
));
704 end Expand_Pragma_Inspection_Point
;
706 --------------------------------------
707 -- Expand_Pragma_Interrupt_Priority --
708 --------------------------------------
710 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
712 procedure Expand_Pragma_Interrupt_Priority
(N
: Node_Id
) is
713 Loc
: constant Source_Ptr
:= Sloc
(N
);
716 if No
(Pragma_Argument_Associations
(N
)) then
717 Set_Pragma_Argument_Associations
(N
, New_List
(
718 Make_Pragma_Argument_Association
(Loc
,
720 Make_Attribute_Reference
(Loc
,
722 New_Occurrence_Of
(RTE
(RE_Interrupt_Priority
), Loc
),
723 Attribute_Name
=> Name_Last
))));
725 end Expand_Pragma_Interrupt_Priority
;
727 --------------------------------
728 -- Expand_Pragma_Psect_Object --
729 --------------------------------
731 -- Convert to Common_Object, and expand the resulting pragma
733 procedure Expand_Pragma_Psect_Object
(N
: Node_Id
) is
735 Set_Chars
(N
, Name_Common_Object
);
736 Expand_Pragma_Common_Object
(N
);
737 end Expand_Pragma_Psect_Object
;