1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Casing; use Casing;
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 Rtsfind; use Rtsfind;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Sinput; use Sinput;
46 with Snames; use Snames;
47 with Stringt; use Stringt;
48 with Stand; use Stand;
49 with Targparm; use Targparm;
50 with Tbuild; use Tbuild;
51 with Uintp; use Uintp;
53 package body Exp_Prag is
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 function Arg1 (N : Node_Id) return Node_Id;
60 function Arg2 (N : Node_Id) return Node_Id;
61 -- Obtain specified pragma argument expression
63 procedure Expand_Pragma_Abort_Defer (N : Node_Id);
64 procedure Expand_Pragma_Assert (N : Node_Id);
65 procedure Expand_Pragma_Common_Object (N : Node_Id);
66 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id);
67 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id);
68 procedure Expand_Pragma_Inspection_Point (N : Node_Id);
69 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id);
70 procedure Expand_Pragma_Psect_Object (N : Node_Id);
76 function Arg1 (N : Node_Id) return Node_Id is
77 Arg : constant Node_Id := First (Pragma_Argument_Associations (N));
80 and then Nkind (Arg) = N_Pragma_Argument_Association
82 return Expression (Arg);
92 function Arg2 (N : Node_Id) return Node_Id is
93 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
99 Arg : constant Node_Id := Next (Arg1);
102 and then Nkind (Arg) = N_Pragma_Argument_Association
104 return Expression (Arg);
112 ---------------------
113 -- Expand_N_Pragma --
114 ---------------------
116 procedure Expand_N_Pragma (N : Node_Id) is
118 -- Note: we may have a pragma whose chars field is not a
119 -- recognized pragma, and we must ignore it at this stage.
121 if Is_Pragma_Name (Chars (N)) then
122 case Get_Pragma_Id (Chars (N)) is
124 -- Pragmas requiring special expander action
126 when Pragma_Abort_Defer =>
127 Expand_Pragma_Abort_Defer (N);
129 when Pragma_Assert =>
130 Expand_Pragma_Assert (N);
132 when Pragma_Common_Object =>
133 Expand_Pragma_Common_Object (N);
135 when Pragma_Export_Exception =>
136 Expand_Pragma_Import_Export_Exception (N);
138 when Pragma_Import =>
139 Expand_Pragma_Import_Or_Interface (N);
141 when Pragma_Import_Exception =>
142 Expand_Pragma_Import_Export_Exception (N);
144 when Pragma_Inspection_Point =>
145 Expand_Pragma_Inspection_Point (N);
147 when Pragma_Interface =>
148 Expand_Pragma_Import_Or_Interface (N);
150 when Pragma_Interrupt_Priority =>
151 Expand_Pragma_Interrupt_Priority (N);
153 when Pragma_Psect_Object =>
154 Expand_Pragma_Psect_Object (N);
156 -- All other pragmas need no expander action
164 -------------------------------
165 -- Expand_Pragma_Abort_Defer --
166 -------------------------------
168 -- An Abort_Defer pragma appears as the first statement in a handled
169 -- statement sequence (right after the begin). It defers aborts for
170 -- the entire statement sequence, but not for any declarations or
171 -- handlers (if any) associated with this statement sequence.
173 -- The transformation is to transform
175 -- pragma Abort_Defer;
184 -- when all others =>
185 -- Abort_Undefer.all;
188 -- Abort_Undefer_Direct;
191 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
192 Loc : constant Source_Ptr := Sloc (N);
196 Blk : constant Entity_Id :=
197 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
200 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
203 Stm := Remove_Next (N);
209 Make_Handled_Sequence_Of_Statements (Loc,
212 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
215 Make_Block_Statement (Loc,
216 Handled_Statement_Sequence => HSS));
218 Set_Scope (Blk, Current_Scope);
219 Set_Etype (Blk, Standard_Void_Type);
220 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
221 Expand_At_End_Handler (HSS, Blk);
223 end Expand_Pragma_Abort_Defer;
225 --------------------------
226 -- Expand_Pragma_Assert --
227 --------------------------
229 procedure Expand_Pragma_Assert (N : Node_Id) is
230 Loc : constant Source_Ptr := Sloc (N);
231 Cond : constant Node_Id := Arg1 (N);
235 -- We already know that assertions are enabled, because otherwise
236 -- the semantic pass dealt with rewriting the assertion (see Sem_Prag)
238 pragma Assert (Assertions_Enabled);
240 -- Since assertions are on, we rewrite the pragma with its
241 -- corresponding if statement, and then analyze the statement
242 -- The expansion transforms:
244 -- pragma Assert (condition [,message]);
248 -- if not condition then
249 -- System.Assertions.Raise_Assert_Failure (Str);
252 -- where Str is the message if one is present, or the default of
253 -- file:line if no message is given.
255 -- First, we need to prepare the character literal
257 if Present (Arg2 (N)) then
258 Msg := Strval (Expr_Value_S (Arg2 (N)));
260 Build_Location_String (Loc);
261 Msg := String_From_Name_Buffer;
264 -- Now generate the if statement. Note that we consider this to be
265 -- an explicit conditional in the source, not an implicit if, so we
266 -- do not call Make_Implicit_If_Statement.
269 Make_If_Statement (Loc,
273 Then_Statements => New_List (
274 Make_Procedure_Call_Statement (Loc,
276 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
277 Parameter_Associations => New_List (
278 Make_String_Literal (Loc, Msg))))));
282 -- If new condition is always false, give a warning
284 if Nkind (N) = N_Procedure_Call_Statement
285 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
287 -- If original condition was a Standard.False, we assume
288 -- that this is indeed intented to raise assert error
289 -- and no warning is required.
291 if Is_Entity_Name (Original_Node (Cond))
292 and then Entity (Original_Node (Cond)) = Standard_False
296 Error_Msg_N ("?assertion will fail at run-time", N);
299 end Expand_Pragma_Assert;
301 ---------------------------------
302 -- Expand_Pragma_Common_Object --
303 ---------------------------------
305 -- Use a machine attribute to replicate semantic effect in DEC Ada
307 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
309 -- For now we do nothing with the size attribute ???
311 procedure Expand_Pragma_Common_Object (N : Node_Id) is
312 Loc : constant Source_Ptr := Sloc (N);
314 Internal : constant Node_Id := Arg1 (N);
315 External : constant Node_Id := Arg2 (N);
318 -- Psect value upper cased as string literal
320 Iloc : constant Source_Ptr := Sloc (Internal);
321 Eloc : constant Source_Ptr := Sloc (External);
325 -- Acquire Psect value and fold to upper case
327 if Present (External) then
328 if Nkind (External) = N_String_Literal then
329 String_To_Name_Buffer (Strval (External));
331 Get_Name_String (Chars (External));
337 Make_String_Literal (Eloc,
338 Strval => String_From_Name_Buffer);
341 Get_Name_String (Chars (Internal));
344 Make_String_Literal (Iloc,
345 Strval => String_From_Name_Buffer);
348 Ploc := Sloc (Psect);
352 Insert_After_And_Analyze (N,
355 Chars => Name_Machine_Attribute,
356 Pragma_Argument_Associations => New_List (
357 Make_Pragma_Argument_Association (Iloc,
358 Expression => New_Copy_Tree (Internal)),
359 Make_Pragma_Argument_Association (Eloc,
361 Make_String_Literal (Sloc => Ploc,
362 Strval => "common_object")),
363 Make_Pragma_Argument_Association (Ploc,
364 Expression => New_Copy_Tree (Psect)))));
366 end Expand_Pragma_Common_Object;
368 ---------------------------------------
369 -- Expand_Pragma_Import_Or_Interface --
370 ---------------------------------------
372 -- When applied to a variable, the default initialization must not be
373 -- done. As it is already done when the pragma is found, we just get rid
374 -- of the call the initialization procedure which followed the object
375 -- declaration. The call is inserted after the declaration, but validity
376 -- checks may also have been inserted and the initialization call does
377 -- not necessarily appear immediately after the object declaration.
379 -- We can't use the freezing mechanism for this purpose, since we
380 -- have to elaborate the initialization expression when it is first
381 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
383 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
384 Def_Id : constant Entity_Id := Entity (Arg2 (N));
389 if Ekind (Def_Id) = E_Variable then
390 Typ := Etype (Def_Id);
394 Init_Call := Next (Parent (Def_Id));
395 while Present (Init_Call) and then Init_Call /= N loop
396 if Has_Non_Null_Base_Init_Proc (Typ)
397 and then Nkind (Init_Call) = N_Procedure_Call_Statement
398 and then Is_Entity_Name (Name (Init_Call))
399 and then Entity (Name (Init_Call)) = Base_Init_Proc (Typ)
408 -- Any default initialization expression should be removed
409 -- (e.g., null defaults for access objects, zero initialization
410 -- of packed bit arrays). Imported objects aren't allowed to
411 -- have explicit initialization, so the expression must have
412 -- been generated by the compiler.
415 and then Present (Expression (Parent (Def_Id)))
417 Set_Expression (Parent (Def_Id), Empty);
420 end Expand_Pragma_Import_Or_Interface;
422 -------------------------------------------
423 -- Expand_Pragma_Import_Export_Exception --
424 -------------------------------------------
426 -- For a VMS exception fix up the language field with "VMS"
427 -- instead of "Ada" (gigi needs this), create a constant that will be the
428 -- value of the VMS condition code and stuff the Interface_Name field
429 -- with the unexpanded name of the exception (if not already set).
430 -- For a Ada exception, just stuff the Interface_Name field
431 -- with the unexpanded name of the exception (if not already set).
433 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
435 -- This pragma is only effective on OpenVMS systems, it was ignored
436 -- on non-VMS systems, and we need to ignore it here as well.
438 if not OpenVMS_On_Target then
443 Id : constant Entity_Id := Entity (Arg1 (N));
444 Call : constant Node_Id := Register_Exception_Call (Id);
445 Loc : constant Source_Ptr := Sloc (N);
448 if Present (Call) then
450 Excep_Internal : constant Node_Id :=
451 Make_Defining_Identifier
452 (Loc, New_Internal_Name ('V'));
453 Export_Pragma : Node_Id;
454 Excep_Alias : Node_Id;
455 Excep_Object : Node_Id;
456 Excep_Image : String_Id;
464 if Present (Interface_Name (Id)) then
465 Excep_Image := Strval (Interface_Name (Id));
467 Get_Name_String (Chars (Id));
469 Excep_Image := String_From_Name_Buffer;
472 Exdata := Component_Associations (Expression (Parent (Id)));
474 if Is_VMS_Exception (Id) then
475 Lang1 := Next (First (Exdata));
476 Lang2 := Next (Lang1);
477 Lang3 := Next (Lang2);
479 Rewrite (Expression (Lang1),
480 Make_Character_Literal (Loc,
482 Char_Literal_Value =>
483 UI_From_Int (Character'Pos ('V'))));
484 Analyze (Expression (Lang1));
486 Rewrite (Expression (Lang2),
487 Make_Character_Literal (Loc,
489 Char_Literal_Value =>
490 UI_From_Int (Character'Pos ('M'))));
491 Analyze (Expression (Lang2));
493 Rewrite (Expression (Lang3),
494 Make_Character_Literal (Loc,
496 Char_Literal_Value =>
497 UI_From_Int (Character'Pos ('S'))));
498 Analyze (Expression (Lang3));
500 if Exception_Code (Id) /= No_Uint then
502 Make_Integer_Literal (Loc,
503 Intval => Exception_Code (Id));
506 Make_Object_Declaration (Loc,
507 Defining_Identifier => Excep_Internal,
509 New_Reference_To (RTE (RE_Exception_Code), Loc));
511 Insert_Action (N, Excep_Object);
512 Analyze (Excep_Object);
516 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
523 (Make_Pragma_Argument_Association
526 New_Reference_To (Excep_Internal, Loc)),
528 Make_Pragma_Argument_Association
533 Strval => End_String))));
535 Insert_Action (N, Excep_Alias);
536 Analyze (Excep_Alias);
543 (Make_Pragma_Argument_Association
545 Expression => Make_Identifier (Loc, Name_C)),
547 Make_Pragma_Argument_Association
550 New_Reference_To (Excep_Internal, Loc)),
552 Make_Pragma_Argument_Association
557 Strval => Excep_Image)),
559 Make_Pragma_Argument_Association
564 Strval => Excep_Image))));
566 Insert_Action (N, Export_Pragma);
567 Analyze (Export_Pragma);
571 Unchecked_Convert_To (RTE (RE_Exception_Code),
572 Make_Function_Call (Loc,
574 New_Reference_To (RTE (RE_Import_Value), Loc),
575 Parameter_Associations => New_List
576 (Make_String_Literal (Loc,
577 Strval => Excep_Image))));
581 Make_Procedure_Call_Statement (Loc,
582 Name => New_Reference_To
583 (RTE (RE_Register_VMS_Exception), Loc),
584 Parameter_Associations => New_List (
586 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
587 Make_Attribute_Reference (Loc,
588 Prefix => New_Occurrence_Of (Id, Loc),
589 Attribute_Name => Name_Unrestricted_Access)))));
591 Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
595 if No (Interface_Name (Id)) then
596 Set_Interface_Name (Id,
599 Strval => Excep_Image));
604 end Expand_Pragma_Import_Export_Exception;
606 ------------------------------------
607 -- Expand_Pragma_Inspection_Point --
608 ------------------------------------
610 -- If no argument is given, then we supply a default argument list that
611 -- includes all objects declared at the source level in all subprograms
612 -- that enclose the inspection point pragma.
614 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
615 Loc : constant Source_Ptr := Sloc (N);
622 if No (Pragma_Argument_Associations (N)) then
626 while S /= Standard_Standard loop
627 E := First_Entity (S);
628 while Present (E) loop
629 if Comes_From_Source (E)
630 and then Is_Object (E)
631 and then not Is_Entry_Formal (E)
632 and then Ekind (E) /= E_Component
633 and then Ekind (E) /= E_Discriminant
634 and then Ekind (E) /= E_Generic_In_Parameter
635 and then Ekind (E) /= E_Generic_In_Out_Parameter
638 Make_Pragma_Argument_Association (Loc,
639 Expression => New_Occurrence_Of (E, Loc)));
648 Set_Pragma_Argument_Associations (N, A);
651 -- Expand the arguments of the pragma. Expanding an entity reference
652 -- is a noop, except in a protected operation, where a reference may
653 -- have to be transformed into a reference to the corresponding prival.
654 -- Are there other pragmas that may require this ???
656 Assoc := First (Pragma_Argument_Associations (N));
658 while Present (Assoc) loop
659 Expand (Expression (Assoc));
662 end Expand_Pragma_Inspection_Point;
664 --------------------------------------
665 -- Expand_Pragma_Interrupt_Priority --
666 --------------------------------------
668 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
670 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
671 Loc : constant Source_Ptr := Sloc (N);
674 if No (Pragma_Argument_Associations (N)) then
675 Set_Pragma_Argument_Associations (N, New_List (
676 Make_Pragma_Argument_Association (Loc,
678 Make_Attribute_Reference (Loc,
680 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
681 Attribute_Name => Name_Last))));
683 end Expand_Pragma_Interrupt_Priority;
685 --------------------------------
686 -- Expand_Pragma_Psect_Object --
687 --------------------------------
689 -- Convert to Common_Object, and expand the resulting pragma
691 procedure Expand_Pragma_Psect_Object (N : Node_Id) is
693 Set_Chars (N, Name_Common_Object);
694 Expand_Pragma_Common_Object (N);
695 end Expand_Pragma_Psect_Object;