1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 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 (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 (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_Interrupt_Priority =>
148 Expand_Pragma_Interrupt_Priority (N);
150 when Pragma_Psect_Object =>
151 Expand_Pragma_Psect_Object (N);
153 -- All other pragmas need no expander action
161 -------------------------------
162 -- Expand_Pragma_Abort_Defer --
163 -------------------------------
165 -- An Abort_Defer pragma appears as the first statement in a handled
166 -- statement sequence (right after the begin). It defers aborts for
167 -- the entire statement sequence, but not for any declarations or
168 -- handlers (if any) associated with this statement sequence.
170 -- The transformation is to transform
172 -- pragma Abort_Defer;
181 -- when all others =>
182 -- Abort_Undefer.all;
185 -- Abort_Undefer_Direct;
188 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
189 Loc : constant Source_Ptr := Sloc (N);
193 Blk : constant Entity_Id :=
194 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
197 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
200 Stm := Remove_Next (N);
206 Make_Handled_Sequence_Of_Statements (Loc,
209 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
212 Make_Block_Statement (Loc,
213 Handled_Statement_Sequence => HSS));
215 Set_Scope (Blk, Current_Scope);
216 Set_Etype (Blk, Standard_Void_Type);
217 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
218 Expand_At_End_Handler (HSS, Blk);
220 end Expand_Pragma_Abort_Defer;
222 --------------------------
223 -- Expand_Pragma_Assert --
224 --------------------------
226 procedure Expand_Pragma_Assert (N : Node_Id) is
227 Loc : constant Source_Ptr := Sloc (N);
228 Cond : constant Node_Id := Arg1 (N);
232 -- We already know that assertions are enabled, because otherwise
233 -- the semantic pass dealt with rewriting the assertion (see Sem_Prag)
235 pragma Assert (Assertions_Enabled);
237 -- Since assertions are on, we rewrite the pragma with its
238 -- corresponding if statement, and then analyze the statement
239 -- The expansion transforms:
241 -- pragma Assert (condition [,message]);
245 -- if not condition then
246 -- System.Assertions.Raise_Assert_Failure (Str);
249 -- where Str is the message if one is present, or the default of
250 -- file:line if no message is given.
252 -- First, we need to prepare the character literal
254 if Present (Arg2 (N)) then
255 Msg := Strval (Expr_Value_S (Arg2 (N)));
257 Build_Location_String (Loc);
258 Msg := String_From_Name_Buffer;
261 -- Now generate the if statement. Note that we consider this to be
262 -- an explicit conditional in the source, not an implicit if, so we
263 -- do not call Make_Implicit_If_Statement.
266 Make_If_Statement (Loc,
270 Then_Statements => New_List (
271 Make_Procedure_Call_Statement (Loc,
273 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
274 Parameter_Associations => New_List (
275 Make_String_Literal (Loc, Msg))))));
279 -- If new condition is always false, give a warning
281 if Nkind (N) = N_Procedure_Call_Statement
282 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
284 -- If original condition was a Standard.False, we assume
285 -- that this is indeed intented to raise assert error
286 -- and no warning is required.
288 if Is_Entity_Name (Original_Node (Cond))
289 and then Entity (Original_Node (Cond)) = Standard_False
293 Error_Msg_N ("?assertion will fail at run-time", N);
296 end Expand_Pragma_Assert;
298 ---------------------------------
299 -- Expand_Pragma_Common_Object --
300 ---------------------------------
302 -- Add series of pragmas to replicate semantic effect in DEC Ada
304 -- pragma Linker_Section (internal_name, external_name);
305 -- pragma Machine_Attribute (internal_name, "overlaid");
306 -- pragma Machine_Attribute (internal_name, "global");
307 -- pragma Machine_Attribute (internal_name, "initialize");
309 -- For now we do nothing with the size attribute ???
311 -- Really this expansion would be much better in the back end. The
312 -- front end should not need to know about target dependent, back end
313 -- dependent semantics ???
315 procedure Expand_Pragma_Common_Object (N : Node_Id) is
316 Loc : constant Source_Ptr := Sloc (N);
318 Internal : constant Node_Id := Arg1 (N);
319 External : constant Node_Id := Arg2 (N);
322 -- Psect value upper cased as string literal
324 Iloc : constant Source_Ptr := Sloc (Internal);
325 Eloc : constant Source_Ptr := Sloc (External);
329 -- Acquire Psect value and fold to upper case
331 if Present (External) then
332 if Nkind (External) = N_String_Literal then
333 String_To_Name_Buffer (Strval (External));
335 Get_Name_String (Chars (External));
341 Make_String_Literal (Eloc,
342 Strval => String_From_Name_Buffer);
345 Get_Name_String (Chars (Internal));
348 Make_String_Literal (Iloc,
349 Strval => String_From_Name_Buffer);
352 Ploc := Sloc (Psect);
356 Insert_List_After_And_Analyze (N, New_List (
358 -- The Linker_Section pragma ensures the correct section
361 Chars => Name_Linker_Section,
362 Pragma_Argument_Associations => New_List (
363 Make_Pragma_Argument_Association (Iloc,
364 Expression => New_Copy_Tree (Internal)),
365 Make_Pragma_Argument_Association (Ploc,
366 Expression => New_Copy_Tree (Psect)))),
368 -- Machine_Attribute "overlaid" ensures that this section
369 -- overlays any other sections of the same name.
372 Chars => Name_Machine_Attribute,
373 Pragma_Argument_Associations => New_List (
374 Make_Pragma_Argument_Association (Iloc,
375 Expression => New_Copy_Tree (Internal)),
376 Make_Pragma_Argument_Association (Eloc,
378 Make_String_Literal (Sloc => Ploc,
379 Strval => "overlaid")))),
381 -- Machine_Attribute "global" ensures that section is visible
384 Chars => Name_Machine_Attribute,
385 Pragma_Argument_Associations => New_List (
386 Make_Pragma_Argument_Association (Iloc,
387 Expression => New_Copy_Tree (Internal)),
388 Make_Pragma_Argument_Association (Eloc,
390 Make_String_Literal (Sloc => Ploc,
391 Strval => "global")))),
393 -- Machine_Attribute "initialize" ensures section is demand zeroed
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 => "initialize"))))));
404 end Expand_Pragma_Common_Object;
406 --------------------------
407 -- Expand_Pragma_Import --
408 --------------------------
410 -- When applied to a variable, the default initialization must not be
411 -- done. As it is already done when the pragma is found, we just get rid
412 -- of the call the initialization procedure which followed the object
413 -- declaration. The call is inserted after the declaration, but validity
414 -- checks may also have been inserted and the initialization call does
415 -- not necessarily appear immediately after the object declaration.
417 -- We can't use the freezing mechanism for this purpose, since we
418 -- have to elaborate the initialization expression when it is first
419 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
421 procedure Expand_Pragma_Import (N : Node_Id) is
422 Def_Id : constant Entity_Id := Entity (Arg2 (N));
427 if Ekind (Def_Id) = E_Variable then
428 Typ := Etype (Def_Id);
432 Init_Call := Next (Parent (Def_Id));
433 while Present (Init_Call) and then Init_Call /= N loop
434 if Has_Non_Null_Base_Init_Proc (Typ)
435 and then Nkind (Init_Call) = N_Procedure_Call_Statement
436 and then Is_Entity_Name (Name (Init_Call))
437 and then Entity (Name (Init_Call)) = Base_Init_Proc (Typ)
446 -- Any default initialization expression should be removed
447 -- (e.g., null defaults for access objects, zero initialization
448 -- of packed bit arrays). Imported objects aren't allowed to
449 -- have explicit initialization, so the expression must have
450 -- been generated by the compiler.
453 and then Present (Expression (Parent (Def_Id)))
455 Set_Expression (Parent (Def_Id), Empty);
458 end Expand_Pragma_Import;
460 -------------------------------------------
461 -- Expand_Pragma_Import_Export_Exception --
462 -------------------------------------------
464 -- For a VMS exception fix up the language field with "VMS"
465 -- instead of "Ada" (gigi needs this), create a constant that will be the
466 -- value of the VMS condition code and stuff the Interface_Name field
467 -- with the unexpanded name of the exception (if not already set).
468 -- For a Ada exception, just stuff the Interface_Name field
469 -- with the unexpanded name of the exception (if not already set).
471 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
473 -- This pragma is only effective on OpenVMS systems, it was ignored
474 -- on non-VMS systems, and we need to ignore it here as well.
476 if not OpenVMS_On_Target then
481 Id : constant Entity_Id := Entity (Arg1 (N));
482 Call : constant Node_Id := Register_Exception_Call (Id);
483 Loc : constant Source_Ptr := Sloc (N);
486 if Present (Call) then
488 Excep_Internal : constant Node_Id :=
489 Make_Defining_Identifier
490 (Loc, New_Internal_Name ('V'));
491 Export_Pragma : Node_Id;
492 Excep_Alias : Node_Id;
493 Excep_Object : Node_Id;
494 Excep_Image : String_Id;
502 if Present (Interface_Name (Id)) then
503 Excep_Image := Strval (Interface_Name (Id));
505 Get_Name_String (Chars (Id));
507 Excep_Image := String_From_Name_Buffer;
510 Exdata := Component_Associations (Expression (Parent (Id)));
512 if Is_VMS_Exception (Id) then
513 Lang1 := Next (First (Exdata));
514 Lang2 := Next (Lang1);
515 Lang3 := Next (Lang2);
517 Rewrite (Expression (Lang1),
518 Make_Character_Literal (Loc,
520 Char_Literal_Value =>
521 UI_From_Int (Character'Pos ('V'))));
522 Analyze (Expression (Lang1));
524 Rewrite (Expression (Lang2),
525 Make_Character_Literal (Loc,
527 Char_Literal_Value =>
528 UI_From_Int (Character'Pos ('M'))));
529 Analyze (Expression (Lang2));
531 Rewrite (Expression (Lang3),
532 Make_Character_Literal (Loc,
534 Char_Literal_Value =>
535 UI_From_Int (Character'Pos ('S'))));
536 Analyze (Expression (Lang3));
538 if Exception_Code (Id) /= No_Uint then
540 Make_Integer_Literal (Loc,
541 Intval => Exception_Code (Id));
544 Make_Object_Declaration (Loc,
545 Defining_Identifier => Excep_Internal,
547 New_Reference_To (RTE (RE_Exception_Code), Loc));
549 Insert_Action (N, Excep_Object);
550 Analyze (Excep_Object);
554 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
561 (Make_Pragma_Argument_Association
564 New_Reference_To (Excep_Internal, Loc)),
566 Make_Pragma_Argument_Association
571 Strval => End_String))));
573 Insert_Action (N, Excep_Alias);
574 Analyze (Excep_Alias);
581 (Make_Pragma_Argument_Association
583 Expression => Make_Identifier (Loc, Name_C)),
585 Make_Pragma_Argument_Association
588 New_Reference_To (Excep_Internal, Loc)),
590 Make_Pragma_Argument_Association
595 Strval => Excep_Image)),
597 Make_Pragma_Argument_Association
602 Strval => Excep_Image))));
604 Insert_Action (N, Export_Pragma);
605 Analyze (Export_Pragma);
609 Unchecked_Convert_To (RTE (RE_Exception_Code),
610 Make_Function_Call (Loc,
612 New_Reference_To (RTE (RE_Import_Value), Loc),
613 Parameter_Associations => New_List
614 (Make_String_Literal (Loc,
615 Strval => Excep_Image))));
619 Make_Procedure_Call_Statement (Loc,
620 Name => New_Reference_To
621 (RTE (RE_Register_VMS_Exception), Loc),
622 Parameter_Associations => New_List (
624 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
625 Make_Attribute_Reference (Loc,
626 Prefix => New_Occurrence_Of (Id, Loc),
627 Attribute_Name => Name_Unrestricted_Access)))));
629 Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
633 if not Present (Interface_Name (Id)) then
634 Set_Interface_Name (Id,
637 Strval => Excep_Image));
642 end Expand_Pragma_Import_Export_Exception;
644 ------------------------------------
645 -- Expand_Pragma_Inspection_Point --
646 ------------------------------------
648 -- If no argument is given, then we supply a default argument list that
649 -- includes all objects declared at the source level in all subprograms
650 -- that enclose the inspection point pragma.
652 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
653 Loc : constant Source_Ptr := Sloc (N);
660 if No (Pragma_Argument_Associations (N)) then
664 while S /= Standard_Standard loop
665 E := First_Entity (S);
666 while Present (E) loop
667 if Comes_From_Source (E)
668 and then Is_Object (E)
669 and then not Is_Entry_Formal (E)
670 and then Ekind (E) /= E_Component
671 and then Ekind (E) /= E_Discriminant
672 and then Ekind (E) /= E_Generic_In_Parameter
673 and then Ekind (E) /= E_Generic_In_Out_Parameter
676 Make_Pragma_Argument_Association (Loc,
677 Expression => New_Occurrence_Of (E, Loc)));
686 Set_Pragma_Argument_Associations (N, A);
689 -- Expand the arguments of the pragma. Expanding an entity reference
690 -- is a noop, except in a protected operation, where a reference may
691 -- have to be transformed into a reference to the corresponding prival.
692 -- Are there other pragmas that may require this ???
694 Assoc := First (Pragma_Argument_Associations (N));
696 while Present (Assoc) loop
697 Expand (Expression (Assoc));
700 end Expand_Pragma_Inspection_Point;
702 --------------------------------------
703 -- Expand_Pragma_Interrupt_Priority --
704 --------------------------------------
706 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
708 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
709 Loc : constant Source_Ptr := Sloc (N);
712 if No (Pragma_Argument_Associations (N)) then
713 Set_Pragma_Argument_Associations (N, New_List (
714 Make_Pragma_Argument_Association (Loc,
716 Make_Attribute_Reference (Loc,
718 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
719 Attribute_Name => Name_Last))));
721 end Expand_Pragma_Interrupt_Priority;
723 --------------------------------
724 -- Expand_Pragma_Psect_Object --
725 --------------------------------
727 -- Convert to Common_Object, and expand the resulting pragma
729 procedure Expand_Pragma_Psect_Object (N : Node_Id) is
731 Set_Chars (N, Name_Common_Object);
732 Expand_Pragma_Common_Object (N);
733 end Expand_Pragma_Psect_Object;