1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2013, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
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
;
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
;
39 package body Sem_Mech
is
41 -------------------------
42 -- Set_Mechanism_Value --
43 -------------------------
45 procedure Set_Mechanism_Value
(Ent
: Entity_Id
; Mech_Name
: Node_Id
) is
50 -- Signal bad descriptor class name
52 procedure Bad_Mechanism
;
53 -- Signal bad mechanism name
55 procedure Bad_Class
is
57 Error_Msg_N
("unrecognized descriptor class name", Class
);
60 procedure Bad_Mechanism
is
62 Error_Msg_N
("unrecognized mechanism name", Mech_Name
);
65 -- Start of processing for Set_Mechanism_Value
68 if Mechanism
(Ent
) /= Default_Mechanism
then
70 ("mechanism for & has already been set", Mech_Name
, Ent
);
73 -- MECHANISM_NAME ::= value | reference | descriptor | short_descriptor
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
);
80 elsif Chars
(Mech_Name
) = Name_Reference
then
81 Set_Mechanism_With_Checks
(Ent
, By_Reference
, Mech_Name
);
84 elsif Chars
(Mech_Name
) = Name_Descriptor
then
85 Check_VMS
(Mech_Name
);
86 Set_Mechanism_With_Checks
(Ent
, By_Descriptor
, Mech_Name
);
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
);
94 elsif Chars
(Mech_Name
) = Name_Copy
then
95 Error_Msg_N
("bad mechanism name, Value assumed", Mech_Name
);
96 Set_Mechanism
(Ent
, By_Copy
);
103 -- MECHANISM_NAME ::= descriptor (CLASS_NAME) |
104 -- short_descriptor (CLASS_NAME)
105 -- CLASS_NAME ::= ubs | ubsb | uba | s | sb | a | nca
107 -- Note: this form is parsed as an indexed component
109 elsif Nkind
(Mech_Name
) = N_Indexed_Component
then
110 Class
:= First
(Expressions
(Mech_Name
));
112 if Nkind
(Prefix
(Mech_Name
)) /= N_Identifier
114 not Nam_In
(Chars
(Prefix
(Mech_Name
)), Name_Descriptor
,
115 Name_Short_Descriptor
)
116 or else Present
(Next
(Class
))
122 -- MECHANISM_NAME ::= descriptor (Class => CLASS_NAME) |
123 -- short_descriptor (Class => CLASS_NAME)
124 -- CLASS_NAME ::= ubs | ubsb | uba | s | sb | a | nca
126 -- Note: this form is parsed as a function call
128 elsif Nkind
(Mech_Name
) = N_Function_Call
then
130 Param
:= First
(Parameter_Associations
(Mech_Name
));
132 if Nkind
(Name
(Mech_Name
)) /= N_Identifier
134 not Nam_In
(Chars
(Name
(Mech_Name
)), Name_Descriptor
,
135 Name_Short_Descriptor
)
136 or else Present
(Next
(Param
))
137 or else No
(Selector_Name
(Param
))
138 or else Chars
(Selector_Name
(Param
)) /= Name_Class
143 Class
:= Explicit_Actual_Parameter
(Param
);
151 -- Fall through here with Class set to descriptor class name
153 Check_VMS
(Mech_Name
);
155 if Nkind
(Class
) /= N_Identifier
then
159 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
160 and then Chars
(Class
) = Name_UBS
162 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_UBS
, Mech_Name
);
164 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
165 and then Chars
(Class
) = Name_UBSB
167 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_UBSB
, Mech_Name
);
169 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
170 and then Chars
(Class
) = Name_UBA
172 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_UBA
, Mech_Name
);
174 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
175 and then Chars
(Class
) = Name_S
177 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_S
, Mech_Name
);
179 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
180 and then Chars
(Class
) = Name_SB
182 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_SB
, Mech_Name
);
184 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
185 and then Chars
(Class
) = Name_A
187 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_A
, Mech_Name
);
189 elsif Chars
(Name
(Mech_Name
)) = Name_Descriptor
190 and then Chars
(Class
) = Name_NCA
192 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_NCA
, Mech_Name
);
194 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
195 and then Chars
(Class
) = Name_UBS
197 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_UBS
, Mech_Name
);
199 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
200 and then Chars
(Class
) = Name_UBSB
202 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_UBSB
, Mech_Name
);
204 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
205 and then Chars
(Class
) = Name_UBA
207 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_UBA
, Mech_Name
);
209 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
210 and then Chars
(Class
) = Name_S
212 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_S
, Mech_Name
);
214 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
215 and then Chars
(Class
) = Name_SB
217 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_SB
, Mech_Name
);
219 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
220 and then Chars
(Class
) = Name_A
222 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_A
, Mech_Name
);
224 elsif Chars
(Name
(Mech_Name
)) = Name_Short_Descriptor
225 and then Chars
(Class
) = Name_NCA
227 Set_Mechanism_With_Checks
(Ent
, By_Short_Descriptor_NCA
, Mech_Name
);
233 end Set_Mechanism_Value
;
235 -------------------------------
236 -- Set_Mechanism_With_Checks --
237 -------------------------------
239 procedure Set_Mechanism_With_Checks
241 Mech
: Mechanism_Type
;
245 -- Right now we only do some checks for functions returning arguments
246 -- by descriptor. Probably mode checks need to be added here ???
248 if Mech
in Descriptor_Codes
and then not Is_Formal
(Ent
) then
249 if Is_Record_Type
(Etype
(Ent
)) then
250 Error_Msg_N
("??records cannot be returned by Descriptor", Enod
);
255 -- If we fall through, all checks have passed
257 Set_Mechanism
(Ent
, Mech
);
258 end Set_Mechanism_With_Checks
;
264 procedure Set_Mechanisms
(E
: Entity_Id
) is
269 -- Skip this processing if inside a generic template. Not only is
270 -- it unnecessary (since neither extra formals nor mechanisms are
271 -- relevant for the template itself), but at least at the moment,
272 -- procedures get frozen early inside a template so attempting to
273 -- look at the formal types does not work too well if they are
274 -- private types that have not been frozen yet.
276 if Inside_A_Generic
then
280 -- Loop through formals
282 Formal
:= First_Formal
(E
);
283 while Present
(Formal
) loop
285 if Mechanism
(Formal
) = Default_Mechanism
then
286 Typ
:= Underlying_Type
(Etype
(Formal
));
288 -- If there is no underlying type, then skip this processing and
289 -- leave the convention set to Default_Mechanism. It seems odd
290 -- that there should ever be such cases but there are (see
291 -- comments for filed regression tests 1418-001 and 1912-009) ???
297 case Convention
(E
) is
303 -- Note: all RM defined conventions are treated the same from
304 -- the point of view of parameter passing mechanism. Convention
305 -- Ghost has the same dynamic semantics as convention Ada.
307 when Convention_Ada |
308 Convention_Intrinsic |
311 Convention_Protected |
312 Convention_Stubbed
=>
314 -- By reference types are passed by reference (RM 6.2(4))
316 if Is_By_Reference_Type
(Typ
) then
317 Set_Mechanism
(Formal
, By_Reference
);
319 -- By copy types are passed by copy (RM 6.2(3))
321 elsif Is_By_Copy_Type
(Typ
) then
322 Set_Mechanism
(Formal
, By_Copy
);
324 -- All other types we leave the Default_Mechanism set, so
325 -- that the backend can choose the appropriate method.
331 -- Special Ada conventions specifying passing mechanism
333 when Convention_Ada_Pass_By_Copy
=>
334 Set_Mechanism
(Formal
, By_Copy
);
336 when Convention_Ada_Pass_By_Reference
=>
337 Set_Mechanism
(Formal
, By_Reference
);
343 -- Note: Assembler, C++, Java, Stdcall also use C conventions
345 when Convention_Assembler |
350 Convention_Stdcall
=>
352 -- The following values are passed by copy
354 -- IN Scalar parameters (RM B.3(66))
355 -- IN parameters of access types (RM B.3(67))
356 -- Access parameters (RM B.3(68))
357 -- Access to subprogram types (RM B.3(71))
359 -- Note: in the case of access parameters, it is the pointer
360 -- that is passed by value. In GNAT access parameters are
361 -- treated as IN parameters of an anonymous access type, so
362 -- this falls out free.
364 -- The bottom line is that all IN elementary types are
365 -- passed by copy in GNAT.
367 if Is_Elementary_Type
(Typ
) then
368 if Ekind
(Formal
) = E_In_Parameter
then
369 Set_Mechanism
(Formal
, By_Copy
);
371 -- OUT and IN OUT parameters of elementary types are
372 -- passed by reference (RM B.3(68)). Note that we are
373 -- not following the advice to pass the address of a
374 -- copy to preserve by copy semantics.
377 Set_Mechanism
(Formal
, By_Reference
);
380 -- Records are normally passed by reference (RM B.3(69)).
381 -- However, this can be overridden by the use of the
382 -- C_Pass_By_Copy pragma or C_Pass_By_Copy convention.
384 elsif Is_Record_Type
(Typ
) then
386 -- If the record is not convention C, then we always
387 -- pass by reference, C_Pass_By_Copy does not apply.
389 if Convention
(Typ
) /= Convention_C
then
390 Set_Mechanism
(Formal
, By_Reference
);
392 -- OUT and IN OUT parameters of record types are passed
393 -- by reference regardless of pragmas (RM B.3 (69/2)).
395 elsif Ekind_In
(Formal
, E_Out_Parameter
,
398 Set_Mechanism
(Formal
, By_Reference
);
400 -- IN parameters of record types are passed by copy only
401 -- when the related type has convention C_Pass_By_Copy
402 -- (RM B.3 (68.1/2)).
404 elsif Ekind
(Formal
) = E_In_Parameter
405 and then C_Pass_By_Copy
(Typ
)
407 Set_Mechanism
(Formal
, By_Copy
);
409 -- Otherwise, for a C convention record, we set the
410 -- convention in accordance with a possible use of
411 -- the C_Pass_By_Copy pragma. Note that the value of
412 -- Default_C_Record_Mechanism in the absence of such
413 -- a pragma is By_Reference.
416 Set_Mechanism
(Formal
, Default_C_Record_Mechanism
);
419 -- Array types are passed by reference (B.3 (71))
421 elsif Is_Array_Type
(Typ
) then
422 Set_Mechanism
(Formal
, By_Reference
);
424 -- For all other types, use Default_Mechanism mechanism
434 when Convention_COBOL
=>
436 -- Access parameters (which in GNAT look like IN parameters
437 -- of an access type) are passed by copy (RM B.4(96)) as
438 -- are all other IN parameters of scalar type (RM B.4(97)).
440 -- For now we pass these parameters by reference as well.
441 -- The RM specifies the intent BY_CONTENT, but gigi does
442 -- not currently transform By_Copy properly. If we pass by
443 -- reference, it will be imperative to introduce copies ???
445 if Is_Elementary_Type
(Typ
)
446 and then Ekind
(Formal
) = E_In_Parameter
448 Set_Mechanism
(Formal
, By_Reference
);
450 -- All other parameters (i.e. all non-scalar types, and
451 -- all OUT or IN OUT parameters) are passed by reference.
452 -- Note that at the moment we are not bothering to make
453 -- copies of scalar types as recommended in the RM.
456 Set_Mechanism
(Formal
, By_Reference
);
463 when Convention_Fortran
=>
465 -- In OpenVMS, pass character and string types using
466 -- Short_Descriptor(S)
469 and then (Root_Type
(Typ
) = Standard_Character
473 Root_Type
(Component_Type
(Typ
)) =
476 Set_Mechanism
(Formal
, By_Short_Descriptor_S
);
478 -- Access types are passed by default (presumably this
479 -- will mean they are passed by copy)
481 elsif Is_Access_Type
(Typ
) then
484 -- For now, we pass all other parameters by reference.
485 -- It is not clear that this is right in the long run,
486 -- but it seems to correspond to what gnu f77 wants.
489 Set_Mechanism
(Formal
, By_Reference
);
494 <<Skip_Formal
>> -- remove this when problem above is fixed ???
496 Next_Formal
(Formal
);
499 -- Note: there is nothing we need to do for the return type here.
500 -- We deal with returning by reference in the Ada sense, by use of
501 -- the flag By_Ref, rather than by messing with mechanisms.
503 -- A mechanism of Reference for the return means that an extra
504 -- parameter must be provided for the return value (that is the
505 -- DEC meaning of the pragma), and is unrelated to the Ada notion
506 -- of return by reference.
508 -- Note: there was originally code here to set the mechanism to
509 -- By_Reference for types that are "by reference" in the Ada sense,
510 -- but, in accordance with the discussion above, this is wrong, and
511 -- the code was removed.