OSDN Git Service

2006-10-31 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_prag.adb
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-2006, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
26
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;
52
53 package body Exp_Prag is
54
55    -----------------------
56    -- Local Subprograms --
57    -----------------------
58
59    function Arg1 (N : Node_Id) return Node_Id;
60    function Arg2 (N : Node_Id) return Node_Id;
61    --  Obtain specified pragma argument expression
62
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);
71
72    ----------
73    -- Arg1 --
74    ----------
75
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;
87
88    ----------
89    -- Arg2 --
90    ----------
91
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;
111
112    ---------------------
113    -- Expand_N_Pragma --
114    ---------------------
115
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.
120
121       if Is_Pragma_Name (Chars (N)) then
122          case Get_Pragma_Id (Chars (N)) is
123
124             --  Pragmas requiring special expander action
125
126             when Pragma_Abort_Defer =>
127                Expand_Pragma_Abort_Defer (N);
128
129             when Pragma_Assert =>
130                Expand_Pragma_Assert (N);
131
132             when Pragma_Common_Object =>
133                Expand_Pragma_Common_Object (N);
134
135             when Pragma_Export_Exception =>
136                Expand_Pragma_Import_Export_Exception (N);
137
138             when Pragma_Import =>
139                Expand_Pragma_Import_Or_Interface (N);
140
141             when Pragma_Import_Exception =>
142                Expand_Pragma_Import_Export_Exception (N);
143
144             when Pragma_Inspection_Point =>
145                Expand_Pragma_Inspection_Point (N);
146
147             when Pragma_Interface =>
148                Expand_Pragma_Import_Or_Interface (N);
149
150             when Pragma_Interrupt_Priority =>
151                Expand_Pragma_Interrupt_Priority (N);
152
153             when Pragma_Psect_Object =>
154                Expand_Pragma_Psect_Object (N);
155
156             --  All other pragmas need no expander action
157
158             when others => null;
159          end case;
160       end if;
161
162    end Expand_N_Pragma;
163
164    -------------------------------
165    -- Expand_Pragma_Abort_Defer --
166    -------------------------------
167
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.
172
173    --  The transformation is to transform
174
175    --    pragma Abort_Defer;
176    --    statements;
177
178    --  into
179
180    --    begin
181    --       Abort_Defer.all;
182    --       statements
183    --    exception
184    --       when all others =>
185    --          Abort_Undefer.all;
186    --          raise;
187    --    at end
188    --       Abort_Undefer_Direct;
189    --    end;
190
191    procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
192       Loc  : constant Source_Ptr := Sloc (N);
193       Stm  : Node_Id;
194       Stms : List_Id;
195       HSS  : Node_Id;
196       Blk  : constant Entity_Id :=
197         New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
198
199    begin
200       Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
201
202       loop
203          Stm := Remove_Next (N);
204          exit when No (Stm);
205          Append (Stm, Stms);
206       end loop;
207
208       HSS :=
209         Make_Handled_Sequence_Of_Statements (Loc,
210           Statements => Stms,
211           At_End_Proc =>
212             New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
213
214       Rewrite (N,
215         Make_Block_Statement (Loc,
216           Handled_Statement_Sequence => HSS));
217
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);
222       Analyze (N);
223    end Expand_Pragma_Abort_Defer;
224
225    --------------------------
226    -- Expand_Pragma_Assert --
227    --------------------------
228
229    procedure Expand_Pragma_Assert (N : Node_Id) is
230       Loc  : constant Source_Ptr := Sloc (N);
231       Cond : constant Node_Id    := Arg1 (N);
232       Msg  : String_Id;
233
234    begin
235       --  We already know that assertions are enabled, because otherwise
236       --  the semantic pass dealt with rewriting the assertion (see Sem_Prag)
237
238       pragma Assert (Assertions_Enabled);
239
240       --  Since assertions are on, we rewrite the pragma with its
241       --  corresponding if statement, and then analyze the statement
242       --  The expansion transforms:
243
244       --    pragma Assert (condition [,message]);
245
246       --  into
247
248       --    if not condition then
249       --       System.Assertions.Raise_Assert_Failure (Str);
250       --    end if;
251
252       --  where Str is the message if one is present, or the default of
253       --  file:line if no message is given.
254
255       --  First, we need to prepare the character literal
256
257       if Present (Arg2 (N)) then
258          Msg := Strval (Expr_Value_S (Arg2 (N)));
259       else
260          Build_Location_String (Loc);
261          Msg := String_From_Name_Buffer;
262       end if;
263
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.
267
268       Rewrite (N,
269         Make_If_Statement (Loc,
270           Condition =>
271             Make_Op_Not (Loc,
272               Right_Opnd => Cond),
273           Then_Statements => New_List (
274             Make_Procedure_Call_Statement (Loc,
275               Name =>
276                 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
277               Parameter_Associations => New_List (
278                 Make_String_Literal (Loc, Msg))))));
279
280       Analyze (N);
281
282       --  If new condition is always false, give a warning
283
284       if Nkind (N) = N_Procedure_Call_Statement
285         and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
286       then
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.
290
291          if Is_Entity_Name (Original_Node (Cond))
292            and then Entity (Original_Node (Cond)) = Standard_False
293          then
294             return;
295          else
296             Error_Msg_N ("?assertion will fail at run-time", N);
297          end if;
298       end if;
299    end Expand_Pragma_Assert;
300
301    ---------------------------------
302    -- Expand_Pragma_Common_Object --
303    ---------------------------------
304
305    --  Use a machine attribute to replicate semantic effect in DEC Ada
306
307    --    pragma Machine_Attribute (intern_name, "common_object", extern_name);
308
309    --  For now we do nothing with the size attribute ???
310
311    procedure Expand_Pragma_Common_Object (N : Node_Id) is
312       Loc : constant Source_Ptr := Sloc (N);
313
314       Internal : constant Node_Id := Arg1 (N);
315       External : constant Node_Id := Arg2 (N);
316
317       Psect : Node_Id;
318       --  Psect value upper cased as string literal
319
320       Iloc : constant Source_Ptr := Sloc (Internal);
321       Eloc : constant Source_Ptr := Sloc (External);
322       Ploc : Source_Ptr;
323
324    begin
325       --  Acquire Psect value and fold to upper case
326
327       if Present (External) then
328          if Nkind (External) = N_String_Literal then
329             String_To_Name_Buffer (Strval (External));
330          else
331             Get_Name_String (Chars (External));
332          end if;
333
334          Set_All_Upper_Case;
335
336          Psect :=
337            Make_String_Literal (Eloc,
338              Strval => String_From_Name_Buffer);
339
340       else
341          Get_Name_String (Chars (Internal));
342          Set_All_Upper_Case;
343          Psect :=
344            Make_String_Literal (Iloc,
345              Strval => String_From_Name_Buffer);
346       end if;
347
348       Ploc := Sloc (Psect);
349
350       --  Insert the pragma
351
352       Insert_After_And_Analyze (N,
353
354          Make_Pragma (Loc,
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,
360                Expression =>
361                  Make_String_Literal (Sloc => Ploc,
362                    Strval => "common_object")),
363              Make_Pragma_Argument_Association (Ploc,
364                Expression => New_Copy_Tree (Psect)))));
365
366    end Expand_Pragma_Common_Object;
367
368    ---------------------------------------
369    -- Expand_Pragma_Import_Or_Interface --
370    ---------------------------------------
371
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.
378
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).
382
383    procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
384       Def_Id    : constant Entity_Id := Entity (Arg2 (N));
385       Typ       : Entity_Id;
386       Init_Call : Node_Id;
387
388    begin
389       if Ekind (Def_Id) = E_Variable then
390          Typ  := Etype (Def_Id);
391
392          --  Loop to ???
393
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)
400             then
401                Remove (Init_Call);
402                exit;
403             else
404                Next (Init_Call);
405             end if;
406          end loop;
407
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.
413
414          if No (Init_Call)
415            and then Present (Expression (Parent (Def_Id)))
416          then
417             Set_Expression (Parent (Def_Id), Empty);
418          end if;
419       end if;
420    end Expand_Pragma_Import_Or_Interface;
421
422    -------------------------------------------
423    -- Expand_Pragma_Import_Export_Exception --
424    -------------------------------------------
425
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).
432
433    procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
434    begin
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.
437
438       if not OpenVMS_On_Target then
439          return;
440       end if;
441
442       declare
443          Id     : constant Entity_Id := Entity (Arg1 (N));
444          Call   : constant Node_Id := Register_Exception_Call (Id);
445          Loc    : constant Source_Ptr := Sloc (N);
446
447       begin
448          if Present (Call) then
449             declare
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;
457                Exdata      : List_Id;
458                Lang1       : Node_Id;
459                Lang2       : Node_Id;
460                Lang3       : Node_Id;
461                Code        : Node_Id;
462
463             begin
464                if Present (Interface_Name (Id)) then
465                   Excep_Image := Strval (Interface_Name (Id));
466                else
467                   Get_Name_String (Chars (Id));
468                   Set_All_Upper_Case;
469                   Excep_Image := String_From_Name_Buffer;
470                end if;
471
472                Exdata := Component_Associations (Expression (Parent (Id)));
473
474                if Is_VMS_Exception (Id) then
475                   Lang1 := Next (First (Exdata));
476                   Lang2 := Next (Lang1);
477                   Lang3 := Next (Lang2);
478
479                   Rewrite (Expression (Lang1),
480                     Make_Character_Literal (Loc,
481                       Chars => Name_uV,
482                       Char_Literal_Value =>
483                         UI_From_Int (Character'Pos ('V'))));
484                   Analyze (Expression (Lang1));
485
486                   Rewrite (Expression (Lang2),
487                     Make_Character_Literal (Loc,
488                       Chars => Name_uM,
489                       Char_Literal_Value =>
490                         UI_From_Int (Character'Pos ('M'))));
491                   Analyze (Expression (Lang2));
492
493                   Rewrite (Expression (Lang3),
494                     Make_Character_Literal (Loc,
495                       Chars => Name_uS,
496                       Char_Literal_Value =>
497                         UI_From_Int (Character'Pos ('S'))));
498                   Analyze (Expression (Lang3));
499
500                   if Exception_Code (Id) /= No_Uint then
501                      Code :=
502                        Make_Integer_Literal (Loc,
503                          Intval => Exception_Code (Id));
504
505                      Excep_Object :=
506                        Make_Object_Declaration (Loc,
507                          Defining_Identifier => Excep_Internal,
508                          Object_Definition   =>
509                            New_Reference_To (RTE (RE_Exception_Code), Loc));
510
511                      Insert_Action (N, Excep_Object);
512                      Analyze (Excep_Object);
513
514                      Start_String;
515                      Store_String_Int
516                        (UI_To_Int (Exception_Code (Id)) / 8 * 8);
517
518                      Excep_Alias :=
519                        Make_Pragma
520                          (Loc,
521                           Name_Linker_Alias,
522                           New_List
523                             (Make_Pragma_Argument_Association
524                                (Sloc => Loc,
525                                 Expression =>
526                                   New_Reference_To (Excep_Internal, Loc)),
527
528                              Make_Pragma_Argument_Association
529                                (Sloc => Loc,
530                                 Expression =>
531                                   Make_String_Literal
532                                     (Sloc => Loc,
533                                      Strval => End_String))));
534
535                      Insert_Action (N, Excep_Alias);
536                      Analyze (Excep_Alias);
537
538                      Export_Pragma :=
539                        Make_Pragma
540                          (Loc,
541                           Name_Export,
542                           New_List
543                             (Make_Pragma_Argument_Association
544                                (Sloc => Loc,
545                                 Expression => Make_Identifier (Loc, Name_C)),
546
547                              Make_Pragma_Argument_Association
548                                (Sloc => Loc,
549                                 Expression =>
550                                   New_Reference_To (Excep_Internal, Loc)),
551
552                              Make_Pragma_Argument_Association
553                                (Sloc => Loc,
554                                 Expression =>
555                                   Make_String_Literal
556                                     (Sloc => Loc,
557                                      Strval => Excep_Image)),
558
559                              Make_Pragma_Argument_Association
560                                (Sloc => Loc,
561                                 Expression =>
562                                   Make_String_Literal
563                                     (Sloc => Loc,
564                                      Strval => Excep_Image))));
565
566                      Insert_Action (N, Export_Pragma);
567                      Analyze (Export_Pragma);
568
569                   else
570                      Code :=
571                         Unchecked_Convert_To (RTE (RE_Exception_Code),
572                           Make_Function_Call (Loc,
573                             Name =>
574                               New_Reference_To (RTE (RE_Import_Value), Loc),
575                             Parameter_Associations => New_List
576                               (Make_String_Literal (Loc,
577                                 Strval => Excep_Image))));
578                   end if;
579
580                   Rewrite (Call,
581                     Make_Procedure_Call_Statement (Loc,
582                       Name => New_Reference_To
583                                 (RTE (RE_Register_VMS_Exception), Loc),
584                       Parameter_Associations => New_List (
585                         Code,
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)))));
590
591                   Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
592                   Analyze (Call);
593                end if;
594
595                if No (Interface_Name (Id)) then
596                   Set_Interface_Name (Id,
597                      Make_String_Literal
598                        (Sloc => Loc,
599                         Strval => Excep_Image));
600                end if;
601             end;
602          end if;
603       end;
604    end Expand_Pragma_Import_Export_Exception;
605
606    ------------------------------------
607    -- Expand_Pragma_Inspection_Point --
608    ------------------------------------
609
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.
613
614    procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
615       Loc : constant Source_Ptr := Sloc (N);
616       A     : List_Id;
617       Assoc : Node_Id;
618       S     : Entity_Id;
619       E     : Entity_Id;
620
621    begin
622       if No (Pragma_Argument_Associations (N)) then
623          A := New_List;
624          S := Current_Scope;
625
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
636                then
637                   Append_To (A,
638                     Make_Pragma_Argument_Association (Loc,
639                       Expression => New_Occurrence_Of (E, Loc)));
640                end if;
641
642                Next_Entity (E);
643             end loop;
644
645             S := Scope (S);
646          end loop;
647
648          Set_Pragma_Argument_Associations (N, A);
649       end if;
650
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 ???
655
656       Assoc := First (Pragma_Argument_Associations (N));
657
658       while Present (Assoc) loop
659          Expand (Expression (Assoc));
660          Next (Assoc);
661       end loop;
662    end Expand_Pragma_Inspection_Point;
663
664    --------------------------------------
665    -- Expand_Pragma_Interrupt_Priority --
666    --------------------------------------
667
668    --  Supply default argument if none exists (System.Interrupt_Priority'Last)
669
670    procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
671       Loc : constant Source_Ptr := Sloc (N);
672
673    begin
674       if No (Pragma_Argument_Associations (N)) then
675          Set_Pragma_Argument_Associations (N, New_List (
676            Make_Pragma_Argument_Association (Loc,
677              Expression =>
678                Make_Attribute_Reference (Loc,
679                  Prefix =>
680                    New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
681                  Attribute_Name => Name_Last))));
682       end if;
683    end Expand_Pragma_Interrupt_Priority;
684
685    --------------------------------
686    -- Expand_Pragma_Psect_Object --
687    --------------------------------
688
689    --  Convert to Common_Object, and expand the resulting pragma
690
691    procedure Expand_Pragma_Psect_Object (N : Node_Id) is
692    begin
693       Set_Chars (N, Name_Common_Object);
694       Expand_Pragma_Common_Object (N);
695    end Expand_Pragma_Psect_Object;
696
697 end Exp_Prag;