OSDN Git Service

gcc/ChangeLog:
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_mech.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ M E C H                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1996-2008, 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 with Atree;    use Atree;
27 with Einfo;    use Einfo;
28 with Errout;   use Errout;
29 with Namet;    use Namet;
30 with Nlists;   use Nlists;
31 with Sem;      use Sem;
32 with Sem_Aux;  use Sem_Aux;
33 with Sem_Util; use Sem_Util;
34 with Sinfo;    use Sinfo;
35 with Snames;   use Snames;
36 with Stand;    use Stand;
37 with Targparm; use Targparm;
38
39 package body Sem_Mech is
40
41    -------------------------
42    -- Set_Mechanism_Value --
43    -------------------------
44
45    procedure Set_Mechanism_Value (Ent : Entity_Id; Mech_Name : Node_Id) is
46       Class : Node_Id;
47       Param : Node_Id;
48
49       procedure Bad_Class;
50       --  Signal bad descriptor class name
51
52       procedure Bad_Mechanism;
53       --  Signal bad mechanism name
54
55       procedure Bad_Class is
56       begin
57          Error_Msg_N ("unrecognized descriptor class name", Class);
58       end Bad_Class;
59
60       procedure Bad_Mechanism is
61       begin
62          Error_Msg_N ("unrecognized mechanism name", Mech_Name);
63       end Bad_Mechanism;
64
65    --  Start of processing for Set_Mechanism_Value
66
67    begin
68       if Mechanism (Ent) /= Default_Mechanism then
69          Error_Msg_NE
70            ("mechanism for & has already been set", Mech_Name, Ent);
71       end if;
72
73       --  MECHANISM_NAME ::= value | reference | descriptor | short_descriptor
74
75       if Nkind (Mech_Name) = N_Identifier then
76          if Chars (Mech_Name) = Name_Value then
77             Set_Mechanism_With_Checks (Ent, By_Copy, Mech_Name);
78             return;
79
80          elsif Chars (Mech_Name) = Name_Reference then
81             Set_Mechanism_With_Checks (Ent, By_Reference, Mech_Name);
82             return;
83
84          elsif Chars (Mech_Name) = Name_Descriptor then
85             Check_VMS (Mech_Name);
86             Set_Mechanism_With_Checks (Ent, By_Descriptor, Mech_Name);
87             return;
88
89          elsif Chars (Mech_Name) = Name_Short_Descriptor then
90             Check_VMS (Mech_Name);
91             Set_Mechanism_With_Checks (Ent, By_Short_Descriptor, Mech_Name);
92             return;
93
94          elsif Chars (Mech_Name) = Name_Copy then
95             Error_Msg_N
96               ("bad mechanism name, Value assumed", Mech_Name);
97             Set_Mechanism (Ent, By_Copy);
98
99          else
100             Bad_Mechanism;
101             return;
102          end if;
103
104       --  MECHANISM_NAME ::= descriptor (CLASS_NAME) |
105       --                     short_descriptor (CLASS_NAME)
106       --  CLASS_NAME     ::= ubs | ubsb | uba | s | sb | a | nca
107
108       --  Note: this form is parsed as an indexed component
109
110       elsif Nkind (Mech_Name) = N_Indexed_Component then
111          Class := First (Expressions (Mech_Name));
112
113          if Nkind (Prefix (Mech_Name)) /= N_Identifier
114            or else not (Chars (Prefix (Mech_Name)) = Name_Descriptor or else
115                         Chars (Prefix (Mech_Name)) = Name_Short_Descriptor)
116            or else Present (Next (Class))
117          then
118             Bad_Mechanism;
119             return;
120          end if;
121
122       --  MECHANISM_NAME ::= descriptor (Class => CLASS_NAME) |
123       --                     short_descriptor (Class => CLASS_NAME)
124       --  CLASS_NAME     ::= ubs | ubsb | uba | s | sb | a | nca
125
126       --  Note: this form is parsed as a function call
127
128       elsif Nkind (Mech_Name) = N_Function_Call then
129
130          Param := First (Parameter_Associations (Mech_Name));
131
132          if Nkind (Name (Mech_Name)) /= N_Identifier
133            or else not (Chars (Name (Mech_Name)) = Name_Descriptor or else
134                         Chars (Name (Mech_Name)) = Name_Short_Descriptor)
135            or else Present (Next (Param))
136            or else No (Selector_Name (Param))
137            or else Chars (Selector_Name (Param)) /= Name_Class
138          then
139             Bad_Mechanism;
140             return;
141          else
142             Class := Explicit_Actual_Parameter (Param);
143          end if;
144
145       else
146          Bad_Mechanism;
147          return;
148       end if;
149
150       --  Fall through here with Class set to descriptor class name
151
152       Check_VMS (Mech_Name);
153
154       if Nkind (Class) /= N_Identifier then
155          Bad_Class;
156          return;
157
158       elsif Chars (Name (Mech_Name)) = Name_Descriptor
159         and then Chars (Class) = Name_UBS
160       then
161          Set_Mechanism_With_Checks (Ent, By_Descriptor_UBS,  Mech_Name);
162
163       elsif Chars (Name (Mech_Name)) = Name_Descriptor
164         and then Chars (Class) = Name_UBSB
165       then
166          Set_Mechanism_With_Checks (Ent, By_Descriptor_UBSB, Mech_Name);
167
168       elsif Chars (Name (Mech_Name)) = Name_Descriptor
169         and then Chars (Class) = Name_UBA
170       then
171          Set_Mechanism_With_Checks (Ent, By_Descriptor_UBA,  Mech_Name);
172
173       elsif Chars (Name (Mech_Name)) = Name_Descriptor
174         and then Chars (Class) = Name_S
175       then
176          Set_Mechanism_With_Checks (Ent, By_Descriptor_S,    Mech_Name);
177
178       elsif Chars (Name (Mech_Name)) = Name_Descriptor
179         and then Chars (Class) = Name_SB
180       then
181          Set_Mechanism_With_Checks (Ent, By_Descriptor_SB,   Mech_Name);
182
183       elsif Chars (Name (Mech_Name)) = Name_Descriptor
184         and then Chars (Class) = Name_A
185       then
186          Set_Mechanism_With_Checks (Ent, By_Descriptor_A,    Mech_Name);
187
188       elsif Chars (Name (Mech_Name)) = Name_Descriptor
189         and then Chars (Class) = Name_NCA
190       then
191          Set_Mechanism_With_Checks (Ent, By_Descriptor_NCA,  Mech_Name);
192
193       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
194         and then Chars (Class) = Name_UBS
195       then
196          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_UBS,  Mech_Name);
197
198       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
199         and then Chars (Class) = Name_UBSB
200       then
201          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_UBSB, Mech_Name);
202
203       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
204         and then Chars (Class) = Name_UBA
205       then
206          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_UBA,  Mech_Name);
207
208       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
209         and then Chars (Class) = Name_S
210       then
211          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_S,    Mech_Name);
212
213       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
214         and then Chars (Class) = Name_SB
215       then
216          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_SB,   Mech_Name);
217
218       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
219         and then Chars (Class) = Name_A
220       then
221          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_A,    Mech_Name);
222
223       elsif Chars (Name (Mech_Name)) = Name_Short_Descriptor
224         and then Chars (Class) = Name_NCA
225       then
226          Set_Mechanism_With_Checks (Ent, By_Short_Descriptor_NCA,  Mech_Name);
227
228       else
229          Bad_Class;
230          return;
231       end if;
232    end Set_Mechanism_Value;
233
234    -------------------------------
235    -- Set_Mechanism_With_Checks --
236    -------------------------------
237
238    procedure Set_Mechanism_With_Checks
239      (Ent  : Entity_Id;
240       Mech : Mechanism_Type;
241       Enod : Node_Id)
242    is
243    begin
244       --  Right now we only do some checks for functions returning arguments
245       --  by descriptor. Probably mode checks need to be added here ???
246
247       if Mech in Descriptor_Codes and then not Is_Formal (Ent) then
248          if Is_Record_Type (Etype (Ent)) then
249             Error_Msg_N ("?records cannot be returned by Descriptor", Enod);
250             return;
251          end if;
252       end if;
253
254       --  If we fall through, all checks have passed
255
256       Set_Mechanism (Ent, Mech);
257    end Set_Mechanism_With_Checks;
258
259    --------------------
260    -- Set_Mechanisms --
261    --------------------
262
263    procedure Set_Mechanisms (E : Entity_Id) is
264       Formal : Entity_Id;
265       Typ    : Entity_Id;
266
267    begin
268       --  Skip this processing if inside a generic template. Not only is
269       --  it unnecessary (since neither extra formals nor mechanisms are
270       --  relevant for the template itself), but at least at the moment,
271       --  procedures get frozen early inside a template so attempting to
272       --  look at the formal types does not work too well if they are
273       --  private types that have not been frozen yet.
274
275       if Inside_A_Generic then
276          return;
277       end if;
278
279       --  Loop through formals
280
281       Formal := First_Formal (E);
282       while Present (Formal) loop
283
284          if Mechanism (Formal) = Default_Mechanism then
285             Typ := Underlying_Type (Etype (Formal));
286
287             --  If there is no underlying type, then skip this processing and
288             --  leave the convention set to Default_Mechanism. It seems odd
289             --  that there should ever be such cases but there are (see
290             --  comments for filed regression tests 1418-001 and 1912-009) ???
291
292             if No (Typ) then
293                goto Skip_Formal;
294             end if;
295
296             case Convention (E) is
297
298                ---------
299                -- Ada --
300                ---------
301
302                --  Note: all RM defined conventions are treated the same
303                --  from the point of view of parameter passing mechanism
304
305                when Convention_Ada       |
306                     Convention_Intrinsic |
307                     Convention_Entry     |
308                     Convention_Protected |
309                     Convention_Stubbed   =>
310
311                   --  By reference types are passed by reference (RM 6.2(4))
312
313                   if Is_By_Reference_Type (Typ) then
314                      Set_Mechanism (Formal, By_Reference);
315
316                   --  By copy types are passed by copy (RM 6.2(3))
317
318                   elsif Is_By_Copy_Type (Typ) then
319                      Set_Mechanism (Formal, By_Copy);
320
321                   --  All other types we leave the Default_Mechanism set, so
322                   --  that the backend can choose the appropriate method.
323
324                   else
325                      null;
326                   end if;
327
328                -------
329                -- C --
330                -------
331
332                --  Note: Assembler, C++, Java, Stdcall also use C conventions
333
334                when Convention_Assembler |
335                     Convention_C         |
336                     Convention_CIL       |
337                     Convention_CPP       |
338                     Convention_Java      |
339                     Convention_Stdcall   =>
340
341                   --  The following values are passed by copy
342
343                   --    IN Scalar parameters (RM B.3(66))
344                   --    IN parameters of access types (RM B.3(67))
345                   --    Access parameters (RM B.3(68))
346                   --    Access to subprogram types (RM B.3(71))
347
348                   --  Note: in the case of access parameters, it is the
349                   --  pointer that is passed by value. In GNAT access
350                   --  parameters are treated as IN parameters of an
351                   --  anonymous access type, so this falls out free.
352
353                   --  The bottom line is that all IN elementary types
354                   --  are passed by copy in GNAT.
355
356                   if Is_Elementary_Type (Typ) then
357                      if Ekind (Formal) = E_In_Parameter then
358                         Set_Mechanism (Formal, By_Copy);
359
360                      --  OUT and IN OUT parameters of elementary types are
361                      --  passed by reference (RM B.3(68)). Note that we are
362                      --  not following the advice to pass the address of a
363                      --  copy to preserve by copy semantics.
364
365                      else
366                         Set_Mechanism (Formal, By_Reference);
367                      end if;
368
369                   --  Records are normally passed by reference (RM B.3(69)).
370                   --  However, this can be overridden by the use of the
371                   --  C_Pass_By_Copy pragma or C_Pass_By_Copy convention.
372
373                   elsif Is_Record_Type (Typ) then
374
375                      --  If the record is not convention C, then we always
376                      --  pass by reference, C_Pass_By_Copy does not apply.
377
378                      if Convention (Typ) /= Convention_C then
379                         Set_Mechanism (Formal, By_Reference);
380
381                      --  If convention C_Pass_By_Copy was specified for
382                      --  the record type, then we pass by copy.
383
384                      elsif C_Pass_By_Copy (Typ) then
385                         Set_Mechanism (Formal, By_Copy);
386
387                      --  Otherwise, for a C convention record, we set the
388                      --  convention in accordance with a possible use of
389                      --  the C_Pass_By_Copy pragma. Note that the value of
390                      --  Default_C_Record_Mechanism in the absence of such
391                      --  a pragma is By_Reference.
392
393                      else
394                         Set_Mechanism (Formal, Default_C_Record_Mechanism);
395                      end if;
396
397                   --  Array types are passed by reference (B.3 (71))
398
399                   elsif Is_Array_Type (Typ) then
400                      Set_Mechanism (Formal, By_Reference);
401
402                   --  For all other types, use Default_Mechanism mechanism
403
404                   else
405                      null;
406                   end if;
407
408                -----------
409                -- COBOL --
410                -----------
411
412                when Convention_COBOL =>
413
414                   --  Access parameters (which in GNAT look like IN parameters
415                   --  of an access type) are passed by copy (RM B.4(96)) as
416                   --  are all other IN parameters of scalar type (RM B.4(97)).
417
418                   --  For now we pass these parameters by reference as well.
419                   --  The RM specifies the intent BY_CONTENT, but gigi does
420                   --  not currently transform By_Copy properly. If we pass by
421                   --  reference, it will be imperative to introduce copies ???
422
423                   if Is_Elementary_Type (Typ)
424                     and then Ekind (Formal) = E_In_Parameter
425                   then
426                      Set_Mechanism (Formal, By_Reference);
427
428                   --  All other parameters (i.e. all non-scalar types, and
429                   --  all OUT or IN OUT parameters) are passed by reference.
430                   --  Note that at the moment we are not bothering to make
431                   --  copies of scalar types as recommended in the RM.
432
433                   else
434                      Set_Mechanism (Formal, By_Reference);
435                   end if;
436
437                -------------
438                -- Fortran --
439                -------------
440
441                when Convention_Fortran =>
442
443                   --  In OpenVMS, pass a character of array of character
444                   --  value using Descriptor(S).
445
446                   if OpenVMS_On_Target
447                     and then (Root_Type (Typ) = Standard_Character
448                                or else
449                                  (Is_Array_Type (Typ)
450                                    and then
451                                      Root_Type (Component_Type (Typ)) =
452                                                      Standard_Character))
453                   then
454                      Set_Mechanism (Formal, By_Descriptor_S);
455
456                   --  Access types are passed by default (presumably this
457                   --  will mean they are passed by copy)
458
459                   elsif Is_Access_Type (Typ) then
460                      null;
461
462                   --  For now, we pass all other parameters by reference.
463                   --  It is not clear that this is right in the long run,
464                   --  but it seems to correspond to what gnu f77 wants.
465
466                   else
467                      Set_Mechanism (Formal, By_Reference);
468                   end if;
469
470             end case;
471          end if;
472
473          <<Skip_Formal>> -- remove this when problem above is fixed ???
474
475          Next_Formal (Formal);
476       end loop;
477
478       --  Note: there is nothing we need to do for the return type here.
479       --  We deal with returning by reference in the Ada sense, by use of
480       --  the flag By_Ref, rather than by messing with mechanisms.
481
482       --  A mechanism of Reference for the return means that an extra
483       --  parameter must be provided for the return value (that is the
484       --  DEC meaning of the pragma), and is unrelated to the Ada notion
485       --  of return by reference.
486
487       --  Note: there was originally code here to set the mechanism to
488       --  By_Reference for types that are "by reference" in the Ada sense,
489       --  but, in accordance with the discussion above, this is wrong, and
490       --  the code was removed.
491
492    end Set_Mechanisms;
493
494 end Sem_Mech;