Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / exp_prag.adb
blob27ec905f1cdb832b9c2c6b59788db4be42572b5c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ P R A G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
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;
38 with Opt; use Opt;
39 with Rtsfind; use Rtsfind;
40 with Sem; use Sem;
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);
72 ----------
73 -- Arg1 --
74 ----------
76 function Arg1 (N : Node_Id) return Node_Id is
77 Arg : constant Node_Id := First (Pragma_Argument_Associations (N));
78 begin
79 if Present (Arg)
80 and then Nkind (Arg) = N_Pragma_Argument_Association
81 then
82 return Expression (Arg);
83 else
84 return Arg;
85 end if;
86 end Arg1;
88 ----------
89 -- Arg2 --
90 ----------
92 function Arg2 (N : Node_Id) return Node_Id is
93 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
94 begin
95 if No (Arg1) then
96 return Empty;
97 else
98 declare
99 Arg : constant Node_Id := Next (Arg1);
100 begin
101 if Present (Arg)
102 and then Nkind (Arg) = N_Pragma_Argument_Association
103 then
104 return Expression (Arg);
105 else
106 return Arg;
107 end if;
108 end;
109 end if;
110 end Arg2;
112 ---------------------
113 -- Expand_N_Pragma --
114 ---------------------
116 procedure Expand_N_Pragma (N : Node_Id) is
117 begin
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
155 when others => null;
156 end case;
157 end if;
159 end Expand_N_Pragma;
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;
173 -- statements;
175 -- into
177 -- begin
178 -- Abort_Defer.all;
179 -- statements
180 -- exception
181 -- when all others =>
182 -- Abort_Undefer.all;
183 -- raise;
184 -- at end
185 -- Abort_Undefer_Direct;
186 -- end;
188 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
189 Loc : constant Source_Ptr := Sloc (N);
190 Stm : Node_Id;
191 Stms : List_Id;
192 HSS : Node_Id;
193 Blk : constant Entity_Id :=
194 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
196 begin
197 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
199 loop
200 Stm := Remove_Next (N);
201 exit when No (Stm);
202 Append (Stm, Stms);
203 end loop;
205 HSS :=
206 Make_Handled_Sequence_Of_Statements (Loc,
207 Statements => Stms,
208 At_End_Proc =>
209 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
211 Rewrite (N,
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);
219 Analyze (N);
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);
229 Msg : String_Id;
231 begin
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]);
243 -- into
245 -- if not condition then
246 -- System.Assertions.Raise_Assert_Failure (Str);
247 -- end if;
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)));
256 else
257 Build_Location_String (Loc);
258 Msg := String_From_Name_Buffer;
259 end if;
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.
265 Rewrite (N,
266 Make_If_Statement (Loc,
267 Condition =>
268 Make_Op_Not (Loc,
269 Right_Opnd => Cond),
270 Then_Statements => New_List (
271 Make_Procedure_Call_Statement (Loc,
272 Name =>
273 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
274 Parameter_Associations => New_List (
275 Make_String_Literal (Loc, Msg))))));
277 Analyze (N);
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)
283 then
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
290 then
291 return;
292 else
293 Error_Msg_N ("?assertion will fail at run-time", N);
294 end if;
295 end if;
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);
321 Psect : Node_Id;
322 -- Psect value upper cased as string literal
324 Iloc : constant Source_Ptr := Sloc (Internal);
325 Eloc : constant Source_Ptr := Sloc (External);
326 Ploc : Source_Ptr;
328 begin
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));
334 else
335 Get_Name_String (Chars (External));
336 end if;
338 Set_All_Upper_Case;
340 Psect :=
341 Make_String_Literal (Eloc,
342 Strval => String_From_Name_Buffer);
344 else
345 Get_Name_String (Chars (Internal));
346 Set_All_Upper_Case;
347 Psect :=
348 Make_String_Literal (Iloc,
349 Strval => String_From_Name_Buffer);
350 end if;
352 Ploc := Sloc (Psect);
354 -- Insert pragmas
356 Insert_List_After_And_Analyze (N, New_List (
358 -- The Linker_Section pragma ensures the correct section
360 Make_Pragma (Loc,
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.
371 Make_Pragma (Loc,
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,
377 Expression =>
378 Make_String_Literal (Sloc => Ploc,
379 Strval => "overlaid")))),
381 -- Machine_Attribute "global" ensures that section is visible
383 Make_Pragma (Loc,
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,
389 Expression =>
390 Make_String_Literal (Sloc => Ploc,
391 Strval => "global")))),
393 -- Machine_Attribute "initialize" ensures section is demand zeroed
395 Make_Pragma (Loc,
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,
401 Expression =>
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));
423 Typ : Entity_Id;
424 Init_Call : Node_Id;
426 begin
427 if Ekind (Def_Id) = E_Variable then
428 Typ := Etype (Def_Id);
430 -- Loop to ???
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)
438 then
439 Remove (Init_Call);
440 exit;
441 else
442 Next (Init_Call);
443 end if;
444 end loop;
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.
452 if No (Init_Call)
453 and then Present (Expression (Parent (Def_Id)))
454 then
455 Set_Expression (Parent (Def_Id), Empty);
456 end if;
457 end if;
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
472 begin
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
477 return;
478 end if;
480 declare
481 Id : constant Entity_Id := Entity (Arg1 (N));
482 Call : constant Node_Id := Register_Exception_Call (Id);
483 Loc : constant Source_Ptr := Sloc (N);
485 begin
486 if Present (Call) then
487 declare
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;
495 Exdata : List_Id;
496 Lang1 : Node_Id;
497 Lang2 : Node_Id;
498 Lang3 : Node_Id;
499 Code : Node_Id;
501 begin
502 if Present (Interface_Name (Id)) then
503 Excep_Image := Strval (Interface_Name (Id));
504 else
505 Get_Name_String (Chars (Id));
506 Set_All_Upper_Case;
507 Excep_Image := String_From_Name_Buffer;
508 end if;
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,
519 Chars => Name_uV,
520 Char_Literal_Value =>
521 UI_From_Int (Character'Pos ('V'))));
522 Analyze (Expression (Lang1));
524 Rewrite (Expression (Lang2),
525 Make_Character_Literal (Loc,
526 Chars => Name_uM,
527 Char_Literal_Value =>
528 UI_From_Int (Character'Pos ('M'))));
529 Analyze (Expression (Lang2));
531 Rewrite (Expression (Lang3),
532 Make_Character_Literal (Loc,
533 Chars => Name_uS,
534 Char_Literal_Value =>
535 UI_From_Int (Character'Pos ('S'))));
536 Analyze (Expression (Lang3));
538 if Exception_Code (Id) /= No_Uint then
539 Code :=
540 Make_Integer_Literal (Loc,
541 Intval => Exception_Code (Id));
543 Excep_Object :=
544 Make_Object_Declaration (Loc,
545 Defining_Identifier => Excep_Internal,
546 Object_Definition =>
547 New_Reference_To (RTE (RE_Exception_Code), Loc));
549 Insert_Action (N, Excep_Object);
550 Analyze (Excep_Object);
552 Start_String;
553 Store_String_Int
554 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
556 Excep_Alias :=
557 Make_Pragma
558 (Loc,
559 Name_Linker_Alias,
560 New_List
561 (Make_Pragma_Argument_Association
562 (Sloc => Loc,
563 Expression =>
564 New_Reference_To (Excep_Internal, Loc)),
566 Make_Pragma_Argument_Association
567 (Sloc => Loc,
568 Expression =>
569 Make_String_Literal
570 (Sloc => Loc,
571 Strval => End_String))));
573 Insert_Action (N, Excep_Alias);
574 Analyze (Excep_Alias);
576 Export_Pragma :=
577 Make_Pragma
578 (Loc,
579 Name_Export,
580 New_List
581 (Make_Pragma_Argument_Association
582 (Sloc => Loc,
583 Expression => Make_Identifier (Loc, Name_C)),
585 Make_Pragma_Argument_Association
586 (Sloc => Loc,
587 Expression =>
588 New_Reference_To (Excep_Internal, Loc)),
590 Make_Pragma_Argument_Association
591 (Sloc => Loc,
592 Expression =>
593 Make_String_Literal
594 (Sloc => Loc,
595 Strval => Excep_Image)),
597 Make_Pragma_Argument_Association
598 (Sloc => Loc,
599 Expression =>
600 Make_String_Literal
601 (Sloc => Loc,
602 Strval => Excep_Image))));
604 Insert_Action (N, Export_Pragma);
605 Analyze (Export_Pragma);
607 else
608 Code :=
609 Unchecked_Convert_To (RTE (RE_Exception_Code),
610 Make_Function_Call (Loc,
611 Name =>
612 New_Reference_To (RTE (RE_Import_Value), Loc),
613 Parameter_Associations => New_List
614 (Make_String_Literal (Loc,
615 Strval => Excep_Image))));
616 end if;
618 Rewrite (Call,
619 Make_Procedure_Call_Statement (Loc,
620 Name => New_Reference_To
621 (RTE (RE_Register_VMS_Exception), Loc),
622 Parameter_Associations => New_List (
623 Code,
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));
630 Analyze (Call);
631 end if;
633 if not Present (Interface_Name (Id)) then
634 Set_Interface_Name (Id,
635 Make_String_Literal
636 (Sloc => Loc,
637 Strval => Excep_Image));
638 end if;
639 end;
640 end if;
641 end;
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);
654 A : List_Id;
655 Assoc : Node_Id;
656 S : Entity_Id;
657 E : Entity_Id;
659 begin
660 if No (Pragma_Argument_Associations (N)) then
661 A := New_List;
662 S := Current_Scope;
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
674 then
675 Append_To (A,
676 Make_Pragma_Argument_Association (Loc,
677 Expression => New_Occurrence_Of (E, Loc)));
678 end if;
680 Next_Entity (E);
681 end loop;
683 S := Scope (S);
684 end loop;
686 Set_Pragma_Argument_Associations (N, A);
687 end if;
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));
698 Next (Assoc);
699 end loop;
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);
711 begin
712 if No (Pragma_Argument_Associations (N)) then
713 Set_Pragma_Argument_Associations (N, New_List (
714 Make_Pragma_Argument_Association (Loc,
715 Expression =>
716 Make_Attribute_Reference (Loc,
717 Prefix =>
718 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
719 Attribute_Name => Name_Last))));
720 end if;
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
730 begin
731 Set_Chars (N, Name_Common_Object);
732 Expand_Pragma_Common_Object (N);
733 end Expand_Pragma_Psect_Object;
735 end Exp_Prag;