PR c++/33620
[official-gcc.git] / gcc / ada / exp_prag.adb
blob962dc7cc428744c383373633179a794a06c121ff
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-2007, 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 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
38 with Opt; use Opt;
39 with Restrict; use Restrict;
40 with Rident; use Rident;
41 with Rtsfind; use Rtsfind;
42 with Sem; use Sem;
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);
74 ----------
75 -- Arg1 --
76 ----------
78 function Arg1 (N : Node_Id) return Node_Id is
79 Arg : constant Node_Id := First (Pragma_Argument_Associations (N));
80 begin
81 if Present (Arg)
82 and then Nkind (Arg) = N_Pragma_Argument_Association
83 then
84 return Expression (Arg);
85 else
86 return Arg;
87 end if;
88 end Arg1;
90 ----------
91 -- Arg2 --
92 ----------
94 function Arg2 (N : Node_Id) return Node_Id is
95 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
96 begin
97 if No (Arg1) then
98 return Empty;
99 else
100 declare
101 Arg : constant Node_Id := Next (Arg1);
102 begin
103 if Present (Arg)
104 and then Nkind (Arg) = N_Pragma_Argument_Association
105 then
106 return Expression (Arg);
107 else
108 return Arg;
109 end if;
110 end;
111 end if;
112 end Arg2;
114 ---------------------
115 -- Expand_N_Pragma --
116 ---------------------
118 procedure Expand_N_Pragma (N : Node_Id) is
119 begin
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
160 when others => null;
161 end case;
162 end if;
164 end Expand_N_Pragma;
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;
178 -- statements;
180 -- into
182 -- begin
183 -- Abort_Defer.all;
184 -- statements
185 -- exception
186 -- when all others =>
187 -- Abort_Undefer.all;
188 -- raise;
189 -- at end
190 -- Abort_Undefer_Direct;
191 -- end;
193 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
194 Loc : constant Source_Ptr := Sloc (N);
195 Stm : Node_Id;
196 Stms : List_Id;
197 HSS : Node_Id;
198 Blk : constant Entity_Id :=
199 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
201 begin
202 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
204 loop
205 Stm := Remove_Next (N);
206 exit when No (Stm);
207 Append (Stm, Stms);
208 end loop;
210 HSS :=
211 Make_Handled_Sequence_Of_Statements (Loc,
212 Statements => Stms,
213 At_End_Proc =>
214 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
216 Rewrite (N,
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);
224 Analyze (N);
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);
234 Msg : String_Id;
236 begin
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]);
248 -- into
250 -- if not condition then
251 -- System.Assertions.Raise_Assert_Failure (Str);
252 -- end if;
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
261 -- generate:
263 -- if not condition then
264 -- raise Assert_Failure;
265 -- end if;
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
281 if (Debug_Flag_Dot_G
282 or else Restriction_Active (No_Exception_Propagation))
283 and then Present (Find_Local_Handler (RTE (RE_Assert_Failure), N))
284 then
285 Rewrite (N,
286 Make_If_Statement (Loc,
287 Condition =>
288 Make_Op_Not (Loc,
289 Right_Opnd => Cond),
290 Then_Statements => New_List (
291 Make_Raise_Statement (Loc,
292 Name =>
293 New_Reference_To (RTE (RE_Assert_Failure), Loc)))));
295 -- Case where we call the procedure
297 else
298 -- First, we need to prepare the string literal
300 if Present (Arg2 (N)) then
301 Msg := Strval (Expr_Value_S (Arg2 (N)));
302 else
303 Build_Location_String (Loc);
304 Msg := String_From_Name_Buffer;
305 end if;
307 -- Now rewrite as an if statement
309 Rewrite (N,
310 Make_If_Statement (Loc,
311 Condition =>
312 Make_Op_Not (Loc,
313 Right_Opnd => Cond),
314 Then_Statements => New_List (
315 Make_Procedure_Call_Statement (Loc,
316 Name =>
317 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
318 Parameter_Associations => New_List (
319 Make_String_Literal (Loc, Msg))))));
320 end if;
322 Analyze (N);
324 -- If new condition is always false, give a warning
326 if Nkind (N) = N_Procedure_Call_Statement
327 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
328 then
329 -- If original condition was a Standard.False, we assume that this is
330 -- indeed intented to raise assert error and no warning is required.
332 if Is_Entity_Name (Original_Node (Cond))
333 and then Entity (Original_Node (Cond)) = Standard_False
334 then
335 return;
336 else
337 Error_Msg_N ("?assertion will fail at run-time", N);
338 end if;
339 end if;
340 end Expand_Pragma_Assert;
342 ---------------------------------
343 -- Expand_Pragma_Common_Object --
344 ---------------------------------
346 -- Use a machine attribute to replicate semantic effect in DEC Ada
348 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
350 -- For now we do nothing with the size attribute ???
352 procedure Expand_Pragma_Common_Object (N : Node_Id) is
353 Loc : constant Source_Ptr := Sloc (N);
355 Internal : constant Node_Id := Arg1 (N);
356 External : constant Node_Id := Arg2 (N);
358 Psect : Node_Id;
359 -- Psect value upper cased as string literal
361 Iloc : constant Source_Ptr := Sloc (Internal);
362 Eloc : constant Source_Ptr := Sloc (External);
363 Ploc : Source_Ptr;
365 begin
366 -- Acquire Psect value and fold to upper case
368 if Present (External) then
369 if Nkind (External) = N_String_Literal then
370 String_To_Name_Buffer (Strval (External));
371 else
372 Get_Name_String (Chars (External));
373 end if;
375 Set_All_Upper_Case;
377 Psect :=
378 Make_String_Literal (Eloc,
379 Strval => String_From_Name_Buffer);
381 else
382 Get_Name_String (Chars (Internal));
383 Set_All_Upper_Case;
384 Psect :=
385 Make_String_Literal (Iloc,
386 Strval => String_From_Name_Buffer);
387 end if;
389 Ploc := Sloc (Psect);
391 -- Insert the pragma
393 Insert_After_And_Analyze (N,
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 => "common_object")),
404 Make_Pragma_Argument_Association (Ploc,
405 Expression => New_Copy_Tree (Psect)))));
407 end Expand_Pragma_Common_Object;
409 ---------------------------------------
410 -- Expand_Pragma_Import_Or_Interface --
411 ---------------------------------------
413 -- When applied to a variable, the default initialization must not be
414 -- done. As it is already done when the pragma is found, we just get rid
415 -- of the call the initialization procedure which followed the object
416 -- declaration. The call is inserted after the declaration, but validity
417 -- checks may also have been inserted and the initialization call does
418 -- not necessarily appear immediately after the object declaration.
420 -- We can't use the freezing mechanism for this purpose, since we
421 -- have to elaborate the initialization expression when it is first
422 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
424 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
425 Def_Id : constant Entity_Id := Entity (Arg2 (N));
426 Typ : Entity_Id;
427 Init_Call : Node_Id;
429 begin
430 if Ekind (Def_Id) = E_Variable then
431 Typ := Etype (Def_Id);
433 -- Iterate from declaration of object to import pragma, to find
434 -- generated initialization call for object, if any.
436 Init_Call := Next (Parent (Def_Id));
437 while Present (Init_Call) and then Init_Call /= N loop
438 if Has_Non_Null_Base_Init_Proc (Typ)
439 and then Nkind (Init_Call) = N_Procedure_Call_Statement
440 and then Is_Entity_Name (Name (Init_Call))
441 and then Entity (Name (Init_Call)) = Base_Init_Proc (Typ)
442 then
443 Remove (Init_Call);
444 exit;
445 else
446 Next (Init_Call);
447 end if;
448 end loop;
450 -- Any default initialization expression should be removed
451 -- (e.g., null defaults for access objects, zero initialization
452 -- of packed bit arrays). Imported objects aren't allowed to
453 -- have explicit initialization, so the expression must have
454 -- been generated by the compiler.
456 if Init_Call = N
457 and then Present (Expression (Parent (Def_Id)))
458 then
459 Set_Expression (Parent (Def_Id), Empty);
460 end if;
461 end if;
462 end Expand_Pragma_Import_Or_Interface;
464 -------------------------------------------
465 -- Expand_Pragma_Import_Export_Exception --
466 -------------------------------------------
468 -- For a VMS exception fix up the language field with "VMS"
469 -- instead of "Ada" (gigi needs this), create a constant that will be the
470 -- value of the VMS condition code and stuff the Interface_Name field
471 -- with the unexpanded name of the exception (if not already set).
472 -- For a Ada exception, just stuff the Interface_Name field
473 -- with the unexpanded name of the exception (if not already set).
475 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
476 begin
477 -- This pragma is only effective on OpenVMS systems, it was ignored
478 -- on non-VMS systems, and we need to ignore it here as well.
480 if not OpenVMS_On_Target then
481 return;
482 end if;
484 declare
485 Id : constant Entity_Id := Entity (Arg1 (N));
486 Call : constant Node_Id := Register_Exception_Call (Id);
487 Loc : constant Source_Ptr := Sloc (N);
489 begin
490 if Present (Call) then
491 declare
492 Excep_Internal : constant Node_Id :=
493 Make_Defining_Identifier
494 (Loc, New_Internal_Name ('V'));
495 Export_Pragma : Node_Id;
496 Excep_Alias : Node_Id;
497 Excep_Object : Node_Id;
498 Excep_Image : String_Id;
499 Exdata : List_Id;
500 Lang1 : Node_Id;
501 Lang2 : Node_Id;
502 Lang3 : Node_Id;
503 Code : Node_Id;
505 begin
506 if Present (Interface_Name (Id)) then
507 Excep_Image := Strval (Interface_Name (Id));
508 else
509 Get_Name_String (Chars (Id));
510 Set_All_Upper_Case;
511 Excep_Image := String_From_Name_Buffer;
512 end if;
514 Exdata := Component_Associations (Expression (Parent (Id)));
516 if Is_VMS_Exception (Id) then
517 Lang1 := Next (First (Exdata));
518 Lang2 := Next (Lang1);
519 Lang3 := Next (Lang2);
521 Rewrite (Expression (Lang1),
522 Make_Character_Literal (Loc,
523 Chars => Name_uV,
524 Char_Literal_Value =>
525 UI_From_Int (Character'Pos ('V'))));
526 Analyze (Expression (Lang1));
528 Rewrite (Expression (Lang2),
529 Make_Character_Literal (Loc,
530 Chars => Name_uM,
531 Char_Literal_Value =>
532 UI_From_Int (Character'Pos ('M'))));
533 Analyze (Expression (Lang2));
535 Rewrite (Expression (Lang3),
536 Make_Character_Literal (Loc,
537 Chars => Name_uS,
538 Char_Literal_Value =>
539 UI_From_Int (Character'Pos ('S'))));
540 Analyze (Expression (Lang3));
542 if Exception_Code (Id) /= No_Uint then
543 Code :=
544 Make_Integer_Literal (Loc,
545 Intval => Exception_Code (Id));
547 Excep_Object :=
548 Make_Object_Declaration (Loc,
549 Defining_Identifier => Excep_Internal,
550 Object_Definition =>
551 New_Reference_To (RTE (RE_Exception_Code), Loc));
553 Insert_Action (N, Excep_Object);
554 Analyze (Excep_Object);
556 Start_String;
557 Store_String_Int
558 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
560 Excep_Alias :=
561 Make_Pragma
562 (Loc,
563 Name_Linker_Alias,
564 New_List
565 (Make_Pragma_Argument_Association
566 (Sloc => Loc,
567 Expression =>
568 New_Reference_To (Excep_Internal, Loc)),
570 Make_Pragma_Argument_Association
571 (Sloc => Loc,
572 Expression =>
573 Make_String_Literal
574 (Sloc => Loc,
575 Strval => End_String))));
577 Insert_Action (N, Excep_Alias);
578 Analyze (Excep_Alias);
580 Export_Pragma :=
581 Make_Pragma
582 (Loc,
583 Name_Export,
584 New_List
585 (Make_Pragma_Argument_Association
586 (Sloc => Loc,
587 Expression => Make_Identifier (Loc, Name_C)),
589 Make_Pragma_Argument_Association
590 (Sloc => Loc,
591 Expression =>
592 New_Reference_To (Excep_Internal, Loc)),
594 Make_Pragma_Argument_Association
595 (Sloc => Loc,
596 Expression =>
597 Make_String_Literal
598 (Sloc => Loc,
599 Strval => Excep_Image)),
601 Make_Pragma_Argument_Association
602 (Sloc => Loc,
603 Expression =>
604 Make_String_Literal
605 (Sloc => Loc,
606 Strval => Excep_Image))));
608 Insert_Action (N, Export_Pragma);
609 Analyze (Export_Pragma);
611 else
612 Code :=
613 Unchecked_Convert_To (RTE (RE_Exception_Code),
614 Make_Function_Call (Loc,
615 Name =>
616 New_Reference_To (RTE (RE_Import_Value), Loc),
617 Parameter_Associations => New_List
618 (Make_String_Literal (Loc,
619 Strval => Excep_Image))));
620 end if;
622 Rewrite (Call,
623 Make_Procedure_Call_Statement (Loc,
624 Name => New_Reference_To
625 (RTE (RE_Register_VMS_Exception), Loc),
626 Parameter_Associations => New_List (
627 Code,
628 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
629 Make_Attribute_Reference (Loc,
630 Prefix => New_Occurrence_Of (Id, Loc),
631 Attribute_Name => Name_Unrestricted_Access)))));
633 Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
634 Analyze (Call);
635 end if;
637 if No (Interface_Name (Id)) then
638 Set_Interface_Name (Id,
639 Make_String_Literal
640 (Sloc => Loc,
641 Strval => Excep_Image));
642 end if;
643 end;
644 end if;
645 end;
646 end Expand_Pragma_Import_Export_Exception;
648 ------------------------------------
649 -- Expand_Pragma_Inspection_Point --
650 ------------------------------------
652 -- If no argument is given, then we supply a default argument list that
653 -- includes all objects declared at the source level in all subprograms
654 -- that enclose the inspection point pragma.
656 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
657 Loc : constant Source_Ptr := Sloc (N);
658 A : List_Id;
659 Assoc : Node_Id;
660 S : Entity_Id;
661 E : Entity_Id;
663 begin
664 if No (Pragma_Argument_Associations (N)) then
665 A := New_List;
666 S := Current_Scope;
668 while S /= Standard_Standard loop
669 E := First_Entity (S);
670 while Present (E) loop
671 if Comes_From_Source (E)
672 and then Is_Object (E)
673 and then not Is_Entry_Formal (E)
674 and then Ekind (E) /= E_Component
675 and then Ekind (E) /= E_Discriminant
676 and then Ekind (E) /= E_Generic_In_Parameter
677 and then Ekind (E) /= E_Generic_In_Out_Parameter
678 then
679 Append_To (A,
680 Make_Pragma_Argument_Association (Loc,
681 Expression => New_Occurrence_Of (E, Loc)));
682 end if;
684 Next_Entity (E);
685 end loop;
687 S := Scope (S);
688 end loop;
690 Set_Pragma_Argument_Associations (N, A);
691 end if;
693 -- Expand the arguments of the pragma. Expanding an entity reference
694 -- is a noop, except in a protected operation, where a reference may
695 -- have to be transformed into a reference to the corresponding prival.
696 -- Are there other pragmas that may require this ???
698 Assoc := First (Pragma_Argument_Associations (N));
700 while Present (Assoc) loop
701 Expand (Expression (Assoc));
702 Next (Assoc);
703 end loop;
704 end Expand_Pragma_Inspection_Point;
706 --------------------------------------
707 -- Expand_Pragma_Interrupt_Priority --
708 --------------------------------------
710 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
712 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
713 Loc : constant Source_Ptr := Sloc (N);
715 begin
716 if No (Pragma_Argument_Associations (N)) then
717 Set_Pragma_Argument_Associations (N, New_List (
718 Make_Pragma_Argument_Association (Loc,
719 Expression =>
720 Make_Attribute_Reference (Loc,
721 Prefix =>
722 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
723 Attribute_Name => Name_Last))));
724 end if;
725 end Expand_Pragma_Interrupt_Priority;
727 --------------------------------
728 -- Expand_Pragma_Psect_Object --
729 --------------------------------
731 -- Convert to Common_Object, and expand the resulting pragma
733 procedure Expand_Pragma_Psect_Object (N : Node_Id) is
734 begin
735 Set_Chars (N, Name_Common_Object);
736 Expand_Pragma_Common_Object (N);
737 end Expand_Pragma_Psect_Object;
739 end Exp_Prag;