1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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_Association
63 procedure Expand_Pragma_Abort_Defer (N : Node_Id);
64 procedure Expand_Pragma_Assert (N : Node_Id);
65 procedure Expand_Pragma_Import (N : Node_Id);
66 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id);
67 procedure Expand_Pragma_Inspection_Point (N : Node_Id);
68 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id);
74 function Arg1 (N : Node_Id) return Node_Id is
76 return First (Pragma_Argument_Associations (N));
83 function Arg2 (N : Node_Id) return Node_Id is
85 return Next (Arg1 (N));
92 procedure Expand_N_Pragma (N : Node_Id) is
94 -- Note: we may have a pragma whose chars field is not a
95 -- recognized pragma, and we must ignore it at this stage.
97 if Is_Pragma_Name (Chars (N)) then
98 case Get_Pragma_Id (Chars (N)) is
100 -- Pragmas requiring special expander action
102 when Pragma_Abort_Defer =>
103 Expand_Pragma_Abort_Defer (N);
105 when Pragma_Assert =>
106 Expand_Pragma_Assert (N);
108 when Pragma_Export_Exception =>
109 Expand_Pragma_Import_Export_Exception (N);
111 when Pragma_Import =>
112 Expand_Pragma_Import (N);
114 when Pragma_Import_Exception =>
115 Expand_Pragma_Import_Export_Exception (N);
117 when Pragma_Inspection_Point =>
118 Expand_Pragma_Inspection_Point (N);
120 when Pragma_Interrupt_Priority =>
121 Expand_Pragma_Interrupt_Priority (N);
123 -- All other pragmas need no expander action
131 -------------------------------
132 -- Expand_Pragma_Abort_Defer --
133 -------------------------------
135 -- An Abort_Defer pragma appears as the first statement in a handled
136 -- statement sequence (right after the begin). It defers aborts for
137 -- the entire statement sequence, but not for any declarations or
138 -- handlers (if any) associated with this statement sequence.
140 -- The transformation is to transform
142 -- pragma Abort_Defer;
151 -- when all others =>
152 -- Abort_Undefer.all;
155 -- Abort_Undefer_Direct;
158 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
159 Loc : constant Source_Ptr := Sloc (N);
163 Blk : constant Entity_Id :=
164 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
167 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
170 Stm := Remove_Next (N);
176 Make_Handled_Sequence_Of_Statements (Loc,
179 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
182 Make_Block_Statement (Loc,
183 Handled_Statement_Sequence => HSS));
185 Set_Scope (Blk, Current_Scope);
186 Set_Etype (Blk, Standard_Void_Type);
187 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
188 Expand_At_End_Handler (HSS, Blk);
190 end Expand_Pragma_Abort_Defer;
192 --------------------------
193 -- Expand_Pragma_Assert --
194 --------------------------
196 procedure Expand_Pragma_Assert (N : Node_Id) is
197 Loc : constant Source_Ptr := Sloc (N);
198 Cond : constant Node_Id := Expression (Arg1 (N));
202 -- We already know that assertions are enabled, because otherwise
203 -- the semantic pass dealt with rewriting the assertion (see Sem_Prag)
205 pragma Assert (Assertions_Enabled);
207 -- Since assertions are on, we rewrite the pragma with its
208 -- corresponding if statement, and then analyze the statement
209 -- The expansion transforms:
211 -- pragma Assert (condition [,message]);
215 -- if not condition then
216 -- System.Assertions.Raise_Assert_Failure (Str);
219 -- where Str is the message if one is present, or the default of
220 -- file:line if no message is given.
222 -- First, we need to prepare the character literal
224 if Present (Arg2 (N)) then
225 Msg := Strval (Expr_Value_S (Expression (Arg2 (N))));
227 Build_Location_String (Loc);
228 Msg := String_From_Name_Buffer;
231 -- Now generate the if statement. Note that we consider this to be
232 -- an explicit conditional in the source, not an implicit if, so we
233 -- do not call Make_Implicit_If_Statement.
236 Make_If_Statement (Loc,
240 Then_Statements => New_List (
241 Make_Procedure_Call_Statement (Loc,
243 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
244 Parameter_Associations => New_List (
245 Make_String_Literal (Loc, Msg))))));
249 -- If new condition is always false, give a warning
251 if Nkind (N) = N_Procedure_Call_Statement
252 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
254 -- If original condition was a Standard.False, we assume
255 -- that this is indeed intented to raise assert error
256 -- and no warning is required.
258 if Is_Entity_Name (Original_Node (Cond))
259 and then Entity (Original_Node (Cond)) = Standard_False
263 Error_Msg_N ("?assertion will fail at run-time", N);
266 end Expand_Pragma_Assert;
268 --------------------------
269 -- Expand_Pragma_Import --
270 --------------------------
272 -- When applied to a variable, the default initialization must not be
273 -- done. As it is already done when the pragma is found, we just get rid
274 -- of the call the initialization procedure which followed the object
277 -- We can't use the freezing mechanism for this purpose, since we
278 -- have to elaborate the initialization expression when it is first
279 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
281 procedure Expand_Pragma_Import (N : Node_Id) is
282 Def_Id : constant Entity_Id := Entity (Expression (Arg2 (N)));
287 if Ekind (Def_Id) = E_Variable then
288 Typ := Etype (Def_Id);
289 After_Def := Next (Parent (Def_Id));
291 if Has_Non_Null_Base_Init_Proc (Typ)
292 and then Nkind (After_Def) = N_Procedure_Call_Statement
293 and then Is_Entity_Name (Name (After_Def))
294 and then Entity (Name (After_Def)) = Base_Init_Proc (Typ)
298 -- Any default initialization expression should be removed
299 -- (e.g., null defaults for access objects, zero initialization
300 -- of packed bit arrays). Imported objects aren't allowed to
301 -- have explicit initialization, so the expression must have
302 -- been generated by the compiler.
304 elsif Present (Expression (Parent (Def_Id))) then
305 Set_Expression (Parent (Def_Id), Empty);
308 end Expand_Pragma_Import;
310 -------------------------------------------
311 -- Expand_Pragma_Import_Export_Exception --
312 -------------------------------------------
314 -- For a VMS exception fix up the language field with "VMS"
315 -- instead of "Ada" (gigi needs this), create a constant that will be the
316 -- value of the VMS condition code and stuff the Interface_Name field
317 -- with the unexpanded name of the exception (if not already set).
318 -- For a Ada exception, just stuff the Interface_Name field
319 -- with the unexpanded name of the exception (if not already set).
321 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
323 -- This pragma is only effective on OpenVMS systems, it was ignored
324 -- on non-VMS systems, and we need to ignore it here as well.
326 if not OpenVMS_On_Target then
331 Id : constant Entity_Id := Entity (Expression (Arg1 (N)));
332 Call : constant Node_Id := Register_Exception_Call (Id);
333 Loc : constant Source_Ptr := Sloc (N);
336 if Present (Call) then
338 Excep_Internal : constant Node_Id :=
339 Make_Defining_Identifier
340 (Loc, New_Internal_Name ('V'));
341 Export_Pragma : Node_Id;
342 Excep_Alias : Node_Id;
343 Excep_Object : Node_Id;
344 Excep_Image : String_Id;
352 if Present (Interface_Name (Id)) then
353 Excep_Image := Strval (Interface_Name (Id));
355 Get_Name_String (Chars (Id));
357 Excep_Image := String_From_Name_Buffer;
360 Exdata := Component_Associations (Expression (Parent (Id)));
362 if Is_VMS_Exception (Id) then
363 Lang1 := Next (First (Exdata));
364 Lang2 := Next (Lang1);
365 Lang3 := Next (Lang2);
367 Rewrite (Expression (Lang1),
368 Make_Character_Literal (Loc,
370 Char_Literal_Value => Get_Char_Code ('V')));
371 Analyze (Expression (Lang1));
373 Rewrite (Expression (Lang2),
374 Make_Character_Literal (Loc,
376 Char_Literal_Value => Get_Char_Code ('M')));
377 Analyze (Expression (Lang2));
379 Rewrite (Expression (Lang3),
380 Make_Character_Literal (Loc,
382 Char_Literal_Value => Get_Char_Code ('S')));
383 Analyze (Expression (Lang3));
385 if Exception_Code (Id) /= No_Uint then
387 Make_Integer_Literal (Loc,
388 Intval => Exception_Code (Id));
391 Make_Object_Declaration (Loc,
392 Defining_Identifier => Excep_Internal,
394 New_Reference_To (Standard_Integer, Loc));
396 Insert_Action (N, Excep_Object);
397 Analyze (Excep_Object);
401 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
408 (Make_Pragma_Argument_Association
411 New_Reference_To (Excep_Internal, Loc)),
413 Make_Pragma_Argument_Association
418 Strval => End_String))));
420 Insert_Action (N, Excep_Alias);
421 Analyze (Excep_Alias);
428 (Make_Pragma_Argument_Association
430 Expression => Make_Identifier (Loc, Name_C)),
432 Make_Pragma_Argument_Association
435 New_Reference_To (Excep_Internal, Loc)),
437 Make_Pragma_Argument_Association
442 Strval => Excep_Image)),
444 Make_Pragma_Argument_Association
449 Strval => Excep_Image))));
451 Insert_Action (N, Export_Pragma);
452 Analyze (Export_Pragma);
456 Unchecked_Convert_To (Standard_Integer,
457 Make_Function_Call (Loc,
459 New_Reference_To (RTE (RE_Import_Value), Loc),
460 Parameter_Associations => New_List
461 (Make_String_Literal (Loc,
462 Strval => Excep_Image))));
466 Make_Procedure_Call_Statement (Loc,
467 Name => New_Reference_To
468 (RTE (RE_Register_VMS_Exception), Loc),
469 Parameter_Associations => New_List (Code)));
471 Analyze_And_Resolve (Code, Standard_Integer);
475 if not Present (Interface_Name (Id)) then
476 Set_Interface_Name (Id,
479 Strval => Excep_Image));
484 end Expand_Pragma_Import_Export_Exception;
486 ------------------------------------
487 -- Expand_Pragma_Inspection_Point --
488 ------------------------------------
490 -- If no argument is given, then we supply a default argument list that
491 -- includes all objects declared at the source level in all subprograms
492 -- that enclose the inspection point pragma.
494 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
495 Loc : constant Source_Ptr := Sloc (N);
502 if No (Pragma_Argument_Associations (N)) then
506 while S /= Standard_Standard loop
507 E := First_Entity (S);
508 while Present (E) loop
509 if Comes_From_Source (E)
510 and then Is_Object (E)
511 and then not Is_Entry_Formal (E)
512 and then Ekind (E) /= E_Component
513 and then Ekind (E) /= E_Discriminant
514 and then Ekind (E) /= E_Generic_In_Parameter
515 and then Ekind (E) /= E_Generic_In_Out_Parameter
518 Make_Pragma_Argument_Association (Loc,
519 Expression => New_Occurrence_Of (E, Loc)));
528 Set_Pragma_Argument_Associations (N, A);
531 -- Expand the arguments of the pragma. Expanding an entity reference
532 -- is a noop, except in a protected operation, where a reference may
533 -- have to be transformed into a reference to the corresponding prival.
534 -- Are there other pragmas that may require this ???
536 Assoc := First (Pragma_Argument_Associations (N));
538 while Present (Assoc) loop
539 Expand (Expression (Assoc));
542 end Expand_Pragma_Inspection_Point;
544 --------------------------------------
545 -- Expand_Pragma_Interrupt_Priority --
546 --------------------------------------
548 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
550 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
551 Loc : constant Source_Ptr := Sloc (N);
554 if No (Pragma_Argument_Associations (N)) then
555 Set_Pragma_Argument_Associations (N, New_List (
556 Make_Pragma_Argument_Association (Loc,
558 Make_Attribute_Reference (Loc,
560 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
561 Attribute_Name => Name_Last))));
563 end Expand_Pragma_Interrupt_Priority;