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 Warn_On_Assertion_Failure
327 and then Nkind (N) = N_Procedure_Call_Statement
328 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
330 -- If original condition was a Standard.False, we assume that this is
331 -- indeed intented to raise assert error and no warning is required.
333 if Is_Entity_Name (Original_Node (Cond))
334 and then Entity (Original_Node (Cond)) = Standard_False
338 Error_Msg_N ("?assertion will fail at run-time", N);
341 end Expand_Pragma_Assert;
343 ---------------------------------
344 -- Expand_Pragma_Common_Object --
345 ---------------------------------
347 -- Use a machine attribute to replicate semantic effect in DEC Ada
349 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
351 -- For now we do nothing with the size attribute ???
353 procedure Expand_Pragma_Common_Object (N : Node_Id) is
354 Loc : constant Source_Ptr := Sloc (N);
356 Internal : constant Node_Id := Arg1 (N);
357 External : constant Node_Id := Arg2 (N);
360 -- Psect value upper cased as string literal
362 Iloc : constant Source_Ptr := Sloc (Internal);
363 Eloc : constant Source_Ptr := Sloc (External);
367 -- Acquire Psect value and fold to upper case
369 if Present (External) then
370 if Nkind (External) = N_String_Literal then
371 String_To_Name_Buffer (Strval (External));
373 Get_Name_String (Chars (External));
379 Make_String_Literal (Eloc,
380 Strval => String_From_Name_Buffer);
383 Get_Name_String (Chars (Internal));
386 Make_String_Literal (Iloc,
387 Strval => String_From_Name_Buffer);
390 Ploc := Sloc (Psect);
394 Insert_After_And_Analyze (N,
397 Chars => Name_Machine_Attribute,
398 Pragma_Argument_Associations => New_List (
399 Make_Pragma_Argument_Association (Iloc,
400 Expression => New_Copy_Tree (Internal)),
401 Make_Pragma_Argument_Association (Eloc,
403 Make_String_Literal (Sloc => Ploc,
404 Strval => "common_object")),
405 Make_Pragma_Argument_Association (Ploc,
406 Expression => New_Copy_Tree (Psect)))));
408 end Expand_Pragma_Common_Object;
410 ---------------------------------------
411 -- Expand_Pragma_Import_Or_Interface --
412 ---------------------------------------
414 -- When applied to a variable, the default initialization must not be
415 -- done. As it is already done when the pragma is found, we just get rid
416 -- of the call the initialization procedure which followed the object
417 -- declaration. The call is inserted after the declaration, but validity
418 -- checks may also have been inserted and the initialization call does
419 -- not necessarily appear immediately after the object declaration.
421 -- We can't use the freezing mechanism for this purpose, since we
422 -- have to elaborate the initialization expression when it is first
423 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
425 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
426 Def_Id : constant Entity_Id := Entity (Arg2 (N));
431 if Ekind (Def_Id) = E_Variable then
432 Typ := Etype (Def_Id);
434 -- Iterate from declaration of object to import pragma, to find
435 -- generated initialization call for object, if any.
437 Init_Call := Next (Parent (Def_Id));
438 while Present (Init_Call) and then Init_Call /= N loop
439 if Has_Non_Null_Base_Init_Proc (Typ)
440 and then Nkind (Init_Call) = N_Procedure_Call_Statement
441 and then Is_Entity_Name (Name (Init_Call))
442 and then Entity (Name (Init_Call)) = Base_Init_Proc (Typ)
451 -- Any default initialization expression should be removed
452 -- (e.g., null defaults for access objects, zero initialization
453 -- of packed bit arrays). Imported objects aren't allowed to
454 -- have explicit initialization, so the expression must have
455 -- been generated by the compiler.
458 and then Present (Expression (Parent (Def_Id)))
460 Set_Expression (Parent (Def_Id), Empty);
463 end Expand_Pragma_Import_Or_Interface;
465 -------------------------------------------
466 -- Expand_Pragma_Import_Export_Exception --
467 -------------------------------------------
469 -- For a VMS exception fix up the language field with "VMS"
470 -- instead of "Ada" (gigi needs this), create a constant that will be the
471 -- value of the VMS condition code and stuff the Interface_Name field
472 -- with the unexpanded name of the exception (if not already set).
473 -- For a Ada exception, just stuff the Interface_Name field
474 -- with the unexpanded name of the exception (if not already set).
476 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
478 -- This pragma is only effective on OpenVMS systems, it was ignored
479 -- on non-VMS systems, and we need to ignore it here as well.
481 if not OpenVMS_On_Target then
486 Id : constant Entity_Id := Entity (Arg1 (N));
487 Call : constant Node_Id := Register_Exception_Call (Id);
488 Loc : constant Source_Ptr := Sloc (N);
491 if Present (Call) then
493 Excep_Internal : constant Node_Id :=
494 Make_Defining_Identifier
495 (Loc, New_Internal_Name ('V'));
496 Export_Pragma : Node_Id;
497 Excep_Alias : Node_Id;
498 Excep_Object : Node_Id;
499 Excep_Image : String_Id;
507 if Present (Interface_Name (Id)) then
508 Excep_Image := Strval (Interface_Name (Id));
510 Get_Name_String (Chars (Id));
512 Excep_Image := String_From_Name_Buffer;
515 Exdata := Component_Associations (Expression (Parent (Id)));
517 if Is_VMS_Exception (Id) then
518 Lang1 := Next (First (Exdata));
519 Lang2 := Next (Lang1);
520 Lang3 := Next (Lang2);
522 Rewrite (Expression (Lang1),
523 Make_Character_Literal (Loc,
525 Char_Literal_Value =>
526 UI_From_Int (Character'Pos ('V'))));
527 Analyze (Expression (Lang1));
529 Rewrite (Expression (Lang2),
530 Make_Character_Literal (Loc,
532 Char_Literal_Value =>
533 UI_From_Int (Character'Pos ('M'))));
534 Analyze (Expression (Lang2));
536 Rewrite (Expression (Lang3),
537 Make_Character_Literal (Loc,
539 Char_Literal_Value =>
540 UI_From_Int (Character'Pos ('S'))));
541 Analyze (Expression (Lang3));
543 if Exception_Code (Id) /= No_Uint then
545 Make_Integer_Literal (Loc,
546 Intval => Exception_Code (Id));
549 Make_Object_Declaration (Loc,
550 Defining_Identifier => Excep_Internal,
552 New_Reference_To (RTE (RE_Exception_Code), Loc));
554 Insert_Action (N, Excep_Object);
555 Analyze (Excep_Object);
559 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
566 (Make_Pragma_Argument_Association
569 New_Reference_To (Excep_Internal, Loc)),
571 Make_Pragma_Argument_Association
576 Strval => End_String))));
578 Insert_Action (N, Excep_Alias);
579 Analyze (Excep_Alias);
586 (Make_Pragma_Argument_Association
588 Expression => Make_Identifier (Loc, Name_C)),
590 Make_Pragma_Argument_Association
593 New_Reference_To (Excep_Internal, Loc)),
595 Make_Pragma_Argument_Association
600 Strval => Excep_Image)),
602 Make_Pragma_Argument_Association
607 Strval => Excep_Image))));
609 Insert_Action (N, Export_Pragma);
610 Analyze (Export_Pragma);
614 Unchecked_Convert_To (RTE (RE_Exception_Code),
615 Make_Function_Call (Loc,
617 New_Reference_To (RTE (RE_Import_Value), Loc),
618 Parameter_Associations => New_List
619 (Make_String_Literal (Loc,
620 Strval => Excep_Image))));
624 Make_Procedure_Call_Statement (Loc,
625 Name => New_Reference_To
626 (RTE (RE_Register_VMS_Exception), Loc),
627 Parameter_Associations => New_List (
629 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
630 Make_Attribute_Reference (Loc,
631 Prefix => New_Occurrence_Of (Id, Loc),
632 Attribute_Name => Name_Unrestricted_Access)))));
634 Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
638 if No (Interface_Name (Id)) then
639 Set_Interface_Name (Id,
642 Strval => Excep_Image));
647 end Expand_Pragma_Import_Export_Exception;
649 ------------------------------------
650 -- Expand_Pragma_Inspection_Point --
651 ------------------------------------
653 -- If no argument is given, then we supply a default argument list that
654 -- includes all objects declared at the source level in all subprograms
655 -- that enclose the inspection point pragma.
657 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
658 Loc : constant Source_Ptr := Sloc (N);
665 if No (Pragma_Argument_Associations (N)) then
669 while S /= Standard_Standard loop
670 E := First_Entity (S);
671 while Present (E) loop
672 if Comes_From_Source (E)
673 and then Is_Object (E)
674 and then not Is_Entry_Formal (E)
675 and then Ekind (E) /= E_Component
676 and then Ekind (E) /= E_Discriminant
677 and then Ekind (E) /= E_Generic_In_Parameter
678 and then Ekind (E) /= E_Generic_In_Out_Parameter
681 Make_Pragma_Argument_Association (Loc,
682 Expression => New_Occurrence_Of (E, Loc)));
691 Set_Pragma_Argument_Associations (N, A);
694 -- Expand the arguments of the pragma. Expanding an entity reference
695 -- is a noop, except in a protected operation, where a reference may
696 -- have to be transformed into a reference to the corresponding prival.
697 -- Are there other pragmas that may require this ???
699 Assoc := First (Pragma_Argument_Associations (N));
701 while Present (Assoc) loop
702 Expand (Expression (Assoc));
705 end Expand_Pragma_Inspection_Point;
707 --------------------------------------
708 -- Expand_Pragma_Interrupt_Priority --
709 --------------------------------------
711 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
713 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
714 Loc : constant Source_Ptr := Sloc (N);
717 if No (Pragma_Argument_Associations (N)) then
718 Set_Pragma_Argument_Associations (N, New_List (
719 Make_Pragma_Argument_Association (Loc,
721 Make_Attribute_Reference (Loc,
723 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
724 Attribute_Name => Name_Last))));
726 end Expand_Pragma_Interrupt_Priority;
728 --------------------------------
729 -- Expand_Pragma_Psect_Object --
730 --------------------------------
732 -- Convert to Common_Object, and expand the resulting pragma
734 procedure Expand_Pragma_Psect_Object (N : Node_Id) is
736 Set_Chars (N, Name_Common_Object);
737 Expand_Pragma_Common_Object (N);
738 end Expand_Pragma_Psect_Object;