OSDN Git Service

PR target/50678
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_intr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ I N T R                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, 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 ------------------------------------------------------------------------------
25
26 --  Processing for intrinsic subprogram declarations
27
28 with Atree;    use Atree;
29 with Einfo;    use Einfo;
30 with Errout;   use Errout;
31 with Fname;    use Fname;
32 with Lib;      use Lib;
33 with Namet;    use Namet;
34 with Sem_Aux;  use Sem_Aux;
35 with Sem_Eval; use Sem_Eval;
36 with Sem_Util; use Sem_Util;
37 with Sinfo;    use Sinfo;
38 with Snames;   use Snames;
39 with Stand;    use Stand;
40 with Stringt;  use Stringt;
41 with Targparm; use Targparm;
42 with Uintp;    use Uintp;
43
44 package body Sem_Intr is
45
46    -----------------------
47    -- Local Subprograms --
48    -----------------------
49
50    procedure Check_Exception_Function (E : Entity_Id; N : Node_Id);
51    --  Check use of intrinsic Exception_Message, Exception_Info or
52    --  Exception_Name, as used in the DEC compatible Current_Exceptions
53    --  package. In each case we must have a parameterless function that
54    --  returns type String.
55
56    procedure Check_Intrinsic_Operator (E : Entity_Id; N : Node_Id);
57    --  Check that operator is one of the binary arithmetic operators, and that
58    --  the types involved both have underlying integer types.
59
60    procedure Check_Shift (E : Entity_Id; N : Node_Id);
61    --  Check intrinsic shift subprogram, the two arguments are the same
62    --  as for Check_Intrinsic_Subprogram (i.e. the entity of the subprogram
63    --  declaration, and the node for the pragma argument, used for messages)
64
65    procedure Errint (Msg : String; S : Node_Id; N : Node_Id);
66    --  Post error message for bad intrinsic, the message itself is posted
67    --  on the appropriate spec node and another message is placed on the
68    --  pragma itself, referring to the spec. S is the node in the spec on
69    --  which the message is to be placed, and N is the pragma argument node.
70
71    ------------------------------
72    -- Check_Exception_Function --
73    ------------------------------
74
75    procedure Check_Exception_Function (E : Entity_Id; N : Node_Id) is
76    begin
77       if not Ekind_In (E, E_Function, E_Generic_Function) then
78          Errint
79            ("intrinsic exception subprogram must be a function", E, N);
80
81       elsif Present (First_Formal (E)) then
82          Errint
83            ("intrinsic exception subprogram may not have parameters",
84             E, First_Formal (E));
85          return;
86
87       elsif Etype (E) /= Standard_String then
88          Errint
89            ("return type of exception subprogram must be String", E, N);
90          return;
91       end if;
92    end Check_Exception_Function;
93
94    --------------------------
95    -- Check_Intrinsic_Call --
96    --------------------------
97
98    procedure Check_Intrinsic_Call (N : Node_Id) is
99       Nam  : constant Entity_Id := Entity (Name (N));
100       Arg1 : constant Node_Id   := First_Actual (N);
101       Typ  : Entity_Id;
102       Rtyp : Entity_Id;
103       Cnam : Name_Id;
104       Unam : Node_Id;
105
106    begin
107       --  Set argument type if argument present
108
109       if Present (Arg1) then
110          Typ := Etype (Arg1);
111          Rtyp := Underlying_Type (Root_Type (Typ));
112       end if;
113
114       --  Set intrinsic name (getting original name in the generic case)
115
116       Unam := Ultimate_Alias (Nam);
117
118       if Present (Parent (Unam))
119         and then Present (Generic_Parent (Parent (Unam)))
120       then
121          Cnam := Chars (Generic_Parent (Parent (Unam)));
122       else
123          Cnam := Chars (Nam);
124       end if;
125
126       --  For Import_xxx calls, argument must be static string. A string
127       --  literal is legal even in Ada83 mode, where such literals are
128       --  not static.
129
130       if Cnam = Name_Import_Address
131            or else
132          Cnam = Name_Import_Largest_Value
133            or else
134          Cnam = Name_Import_Value
135       then
136          if Etype (Arg1) = Any_Type
137            or else Raises_Constraint_Error (Arg1)
138          then
139             null;
140
141          elsif Nkind (Arg1) /= N_String_Literal
142            and then not Is_Static_Expression (Arg1)
143          then
144             Error_Msg_FE
145               ("call to & requires static string argument!", N, Nam);
146             Why_Not_Static (Arg1);
147
148          elsif String_Length (Strval (Expr_Value_S (Arg1))) = 0 then
149             Error_Msg_NE
150               ("call to & does not permit null string", N, Nam);
151
152          elsif OpenVMS_On_Target
153            and then String_Length (Strval (Expr_Value_S (Arg1))) > 31
154          then
155             Error_Msg_NE
156               ("argument in call to & must be 31 characters or less", N, Nam);
157          end if;
158
159       --  Check for the case of freeing a non-null object which will raise
160       --  Constraint_Error. Issue warning here, do the expansion in Exp_Intr.
161
162       elsif Cnam = Name_Unchecked_Deallocation
163         and then Can_Never_Be_Null (Etype (Arg1))
164       then
165          Error_Msg_N
166            ("freeing `NOT NULL` object will raise Constraint_Error?", N);
167
168       --  For unchecked deallocation, error to deallocate from empty pool.
169       --  Note: this test used to be in Exp_Intr as a warning, but AI 157
170       --  issues a binding interpretation that this should be an error, and
171       --  consequently it needs to be done in the semantic analysis so that
172       --  the error is issued even in semantics only mode.
173
174       elsif Cnam = Name_Unchecked_Deallocation
175         and then No_Pool_Assigned (Rtyp)
176       then
177          Error_Msg_N ("deallocation from empty storage pool!", N);
178
179       --  For now, no other special checks are required
180
181       else
182          return;
183       end if;
184    end Check_Intrinsic_Call;
185
186    ------------------------------
187    -- Check_Intrinsic_Operator --
188    ------------------------------
189
190    procedure Check_Intrinsic_Operator (E : Entity_Id; N : Node_Id) is
191       Ret : constant Entity_Id := Etype (E);
192       Nam : constant Name_Id   := Chars (E);
193       T1  : Entity_Id;
194       T2  : Entity_Id;
195
196    begin
197       --  Arithmetic operators
198
199       if Nam = Name_Op_Add
200            or else
201          Nam = Name_Op_Subtract
202            or else
203          Nam = Name_Op_Multiply
204            or else
205          Nam = Name_Op_Divide
206            or else
207          Nam = Name_Op_Rem
208            or else
209          Nam = Name_Op_Mod
210            or else
211          Nam = Name_Op_Abs
212       then
213          T1 := Etype (First_Formal (E));
214
215          if No (Next_Formal (First_Formal (E))) then
216
217             if Nam = Name_Op_Add
218                  or else
219                Nam = Name_Op_Subtract
220                  or else
221                Nam = Name_Op_Abs
222             then
223                T2 := T1;
224
225             --  Previous error in declaration
226
227             else
228                return;
229             end if;
230
231          else
232             T2 := Etype (Next_Formal (First_Formal (E)));
233          end if;
234
235          --  Same types, predefined operator will apply
236
237          if Root_Type (T1) = Root_Type (T2)
238            or else Root_Type (T1) = Root_Type (Ret)
239          then
240             null;
241
242          --  Expansion will introduce conversions if sizes are not equal
243
244          elsif Is_Integer_Type (Underlying_Type (T1))
245            and then Is_Integer_Type (Underlying_Type (T2))
246            and then Is_Integer_Type (Underlying_Type (Ret))
247          then
248             null;
249
250          else
251             Errint
252               ("types of intrinsic operator operands do not match", E, N);
253          end if;
254
255       --  Comparison operators
256
257       elsif Nam = Name_Op_Eq
258               or else
259             Nam = Name_Op_Ge
260               or else
261             Nam = Name_Op_Gt
262               or else
263             Nam = Name_Op_Le
264               or else
265             Nam = Name_Op_Lt
266               or else
267             Nam = Name_Op_Ne
268       then
269          T1 := Etype (First_Formal (E));
270
271          --  Return if previous error in declaration, otherwise get T2 type
272
273          if No (Next_Formal (First_Formal (E))) then
274             return;
275          else
276             T2 := Etype (Next_Formal (First_Formal (E)));
277          end if;
278
279          if Root_Type (T1) /= Root_Type (T2) then
280             Errint
281               ("types of intrinsic operator must have the same size", E, N);
282          end if;
283
284          if Root_Type (Ret) /= Standard_Boolean then
285             Errint
286               ("result type of intrinsic comparison must be boolean", E, N);
287          end if;
288
289       --  Exponentiation
290
291       elsif Nam = Name_Op_Expon then
292          T1 := Etype (First_Formal (E));
293
294          if No (Next_Formal (First_Formal (E))) then
295
296             --  Previous error in declaration
297
298             return;
299
300          else
301             T2 := Etype (Next_Formal (First_Formal (E)));
302          end if;
303
304          if not (Is_Integer_Type (T1)
305                    or else
306                  Is_Floating_Point_Type (T1))
307            or else Root_Type (T1) /= Root_Type (Ret)
308            or else Root_Type (T2) /= Root_Type (Standard_Integer)
309          then
310             Errint ("incorrect operands for intrinsic operator", N, E);
311          end if;
312
313       --  All other operators (are there any?) are not handled
314
315       else
316          Errint ("incorrect context for ""Intrinsic"" convention", E, N);
317          return;
318       end if;
319
320       if not Is_Numeric_Type (Underlying_Type (T1)) then
321          Errint ("intrinsic operator can only apply to numeric types", E, N);
322       end if;
323    end Check_Intrinsic_Operator;
324
325    --------------------------------
326    -- Check_Intrinsic_Subprogram --
327    --------------------------------
328
329    procedure Check_Intrinsic_Subprogram (E : Entity_Id; N : Node_Id) is
330       Spec : constant Node_Id := Specification (Unit_Declaration_Node (E));
331       Nam  : Name_Id;
332
333    begin
334       if Present (Spec)
335         and then Present (Generic_Parent (Spec))
336       then
337          Nam := Chars (Generic_Parent (Spec));
338       else
339          Nam := Chars (E);
340       end if;
341
342       --  Check name is valid intrinsic name
343
344       Get_Name_String (Nam);
345
346       if Name_Buffer (1) /= 'O'
347         and then Nam /= Name_Asm
348         and then Nam /= Name_To_Address
349         and then Nam not in First_Intrinsic_Name .. Last_Intrinsic_Name
350       then
351          Errint ("unrecognized intrinsic subprogram", E, N);
352
353       --  We always allow intrinsic specifications in language defined units
354       --  and in expanded code. We assume that the GNAT implementors know what
355       --  they are doing, and do not write or generate junk use of intrinsic!
356
357       elsif not Comes_From_Source (E)
358         or else not Comes_From_Source (N)
359         or else Is_Predefined_File_Name
360                   (Unit_File_Name (Get_Source_Unit (N)))
361       then
362          null;
363
364       --  Shift cases. We allow user specification of intrinsic shift
365       --  operators for any numeric types.
366
367       elsif
368         Nam = Name_Rotate_Left
369           or else
370         Nam = Name_Rotate_Right
371           or else
372         Nam = Name_Shift_Left
373           or else
374         Nam = Name_Shift_Right
375           or else
376         Nam = Name_Shift_Right_Arithmetic
377       then
378          Check_Shift (E, N);
379
380       elsif
381         Nam = Name_Exception_Information
382           or else
383         Nam = Name_Exception_Message
384           or else
385         Nam = Name_Exception_Name
386       then
387          Check_Exception_Function (E, N);
388
389       elsif Nkind (E) = N_Defining_Operator_Symbol then
390          Check_Intrinsic_Operator (E, N);
391
392       elsif Nam = Name_File
393         or else Nam = Name_Line
394         or else Nam = Name_Source_Location
395         or else Nam = Name_Enclosing_Entity
396       then
397          null;
398
399       --  For now, no other intrinsic subprograms are recognized in user code
400
401       else
402          Errint ("incorrect context for ""Intrinsic"" convention", E, N);
403       end if;
404    end Check_Intrinsic_Subprogram;
405
406    -----------------
407    -- Check_Shift --
408    -----------------
409
410    procedure Check_Shift (E : Entity_Id; N : Node_Id) is
411       Arg1  : Node_Id;
412       Arg2  : Node_Id;
413       Size  : Nat;
414       Typ1  : Entity_Id;
415       Typ2  : Entity_Id;
416       Ptyp1 : Node_Id;
417       Ptyp2 : Node_Id;
418
419    begin
420       if not Ekind_In (E, E_Function, E_Generic_Function) then
421          Errint ("intrinsic shift subprogram must be a function", E, N);
422          return;
423       end if;
424
425       Arg1 := First_Formal (E);
426
427       if Present (Arg1) then
428          Arg2 := Next_Formal (Arg1);
429       else
430          Arg2 := Empty;
431       end if;
432
433       if Arg1 = Empty or else Arg2 = Empty then
434          Errint ("intrinsic shift function must have two arguments", E, N);
435          return;
436       end if;
437
438       Typ1 := Etype (Arg1);
439       Typ2 := Etype (Arg2);
440
441       Ptyp1 := Parameter_Type (Parent (Arg1));
442       Ptyp2 := Parameter_Type (Parent (Arg2));
443
444       if not Is_Integer_Type (Typ1) then
445          Errint ("first argument to shift must be integer type", Ptyp1, N);
446          return;
447       end if;
448
449       if Typ2 /= Standard_Natural then
450          Errint ("second argument to shift must be type Natural", Ptyp2, N);
451          return;
452       end if;
453
454       Size := UI_To_Int (Esize (Typ1));
455
456       if Size /= 8
457         and then Size /= 16
458         and then Size /= 32
459         and then Size /= 64
460       then
461          Errint
462            ("first argument for shift must have size 8, 16, 32 or 64",
463              Ptyp1, N);
464          return;
465
466       elsif Non_Binary_Modulus (Typ1) then
467          Errint
468            ("shifts not allowed for non-binary modular types",
469             Ptyp1, N);
470
471       elsif Etype (Arg1) /= Etype (E) then
472          Errint
473            ("first argument of shift must match return type", Ptyp1, N);
474          return;
475       end if;
476    end Check_Shift;
477
478    ------------
479    -- Errint --
480    ------------
481
482    procedure Errint (Msg : String; S : Node_Id; N : Node_Id) is
483    begin
484       Error_Msg_N (Msg, S);
485       Error_Msg_N ("incorrect intrinsic subprogram, see spec", N);
486    end Errint;
487
488 end Sem_Intr;