1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1996-2002 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 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Einfo
; use Einfo
;
29 with Errout
; use Errout
;
30 with Targparm
; use Targparm
;
31 with Nlists
; use Nlists
;
33 with Sem_Util
; use Sem_Util
;
34 with Sinfo
; use Sinfo
;
35 with Snames
; use Snames
;
36 with Stand
; use Stand
;
38 package body Sem_Mech
is
40 -------------------------
41 -- Set_Mechanism_Value --
42 -------------------------
44 procedure Set_Mechanism_Value
(Ent
: Entity_Id
; Mech_Name
: Node_Id
) is
49 -- Signal bad descriptor class name
51 procedure Bad_Mechanism
;
52 -- Signal bad mechanism name
54 procedure Bad_Class
is
56 Error_Msg_N
("unrecognized descriptor class name", Class
);
59 procedure Bad_Mechanism
is
61 Error_Msg_N
("unrecognized mechanism name", Mech_Name
);
64 -- Start of processing for Set_Mechanism_Value
67 if Mechanism
(Ent
) /= Default_Mechanism
then
69 ("mechanism for & has already been set", Mech_Name
, Ent
);
72 -- MECHANISM_NAME ::= value | reference | descriptor
74 if Nkind
(Mech_Name
) = N_Identifier
then
75 if Chars
(Mech_Name
) = Name_Value
then
76 Set_Mechanism_With_Checks
(Ent
, By_Copy
, Mech_Name
);
79 elsif Chars
(Mech_Name
) = Name_Reference
then
80 Set_Mechanism_With_Checks
(Ent
, By_Reference
, Mech_Name
);
83 elsif Chars
(Mech_Name
) = Name_Descriptor
then
84 Check_VMS
(Mech_Name
);
85 Set_Mechanism_With_Checks
(Ent
, By_Descriptor
, Mech_Name
);
88 elsif Chars
(Mech_Name
) = Name_Copy
then
90 ("bad mechanism name, Value assumed", Mech_Name
);
91 Set_Mechanism
(Ent
, By_Copy
);
98 -- MECHANISM_NAME ::= descriptor (CLASS_NAME)
99 -- CLASS_NAME ::= ubs | ubsb | uba | s | sb | a | nca
101 -- Note: this form is parsed as an indexed component
103 elsif Nkind
(Mech_Name
) = N_Indexed_Component
then
104 Class
:= First
(Expressions
(Mech_Name
));
106 if Nkind
(Prefix
(Mech_Name
)) /= N_Identifier
107 or else Chars
(Prefix
(Mech_Name
)) /= Name_Descriptor
108 or else Present
(Next
(Class
))
114 -- MECHANISM_NAME ::= descriptor (Class => CLASS_NAME)
115 -- CLASS_NAME ::= ubs | ubsb | uba | s | sb | a | nca
117 -- Note: this form is parsed as a function call
119 elsif Nkind
(Mech_Name
) = N_Function_Call
then
121 Param
:= First
(Parameter_Associations
(Mech_Name
));
123 if Nkind
(Name
(Mech_Name
)) /= N_Identifier
124 or else Chars
(Name
(Mech_Name
)) /= Name_Descriptor
125 or else Present
(Next
(Param
))
126 or else No
(Selector_Name
(Param
))
127 or else Chars
(Selector_Name
(Param
)) /= Name_Class
132 Class
:= Explicit_Actual_Parameter
(Param
);
140 -- Fall through here with Class set to descriptor class name
142 Check_VMS
(Mech_Name
);
144 if Nkind
(Class
) /= N_Identifier
then
148 elsif Chars
(Class
) = Name_UBS
then
149 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_UBS
, Mech_Name
);
151 elsif Chars
(Class
) = Name_UBSB
then
152 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_UBSB
, Mech_Name
);
154 elsif Chars
(Class
) = Name_UBA
then
155 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_UBA
, Mech_Name
);
157 elsif Chars
(Class
) = Name_S
then
158 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_S
, Mech_Name
);
160 elsif Chars
(Class
) = Name_SB
then
161 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_SB
, Mech_Name
);
163 elsif Chars
(Class
) = Name_A
then
164 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_A
, Mech_Name
);
166 elsif Chars
(Class
) = Name_NCA
then
167 Set_Mechanism_With_Checks
(Ent
, By_Descriptor_NCA
, Mech_Name
);
174 end Set_Mechanism_Value
;
176 -------------------------------
177 -- Set_Mechanism_With_Checks --
178 -------------------------------
180 procedure Set_Mechanism_With_Checks
182 Mech
: Mechanism_Type
;
186 -- Right now we only do some checks for functions returning arguments
187 -- by desctiptor. Probably mode checks need to be added here ???
189 if Mech
in Descriptor_Codes
and then not Is_Formal
(Ent
) then
190 if Is_Record_Type
(Etype
(Ent
)) then
191 Error_Msg_N
("?records cannot be returned by Descriptor", Enod
);
196 -- If we fall through, all checks have passed
198 Set_Mechanism
(Ent
, Mech
);
199 end Set_Mechanism_With_Checks
;
205 procedure Set_Mechanisms
(E
: Entity_Id
) is
210 -- Skip this processing if inside a generic template. Not only is
211 -- it uneccessary (since neither extra formals nor mechanisms are
212 -- relevant for the template itself), but at least at the moment,
213 -- procedures get frozen early inside a template so attempting to
214 -- look at the formal types does not work too well if they are
215 -- private types that have not been frozen yet.
217 if Inside_A_Generic
then
221 -- Loop through formals
223 Formal
:= First_Formal
(E
);
224 while Present
(Formal
) loop
226 if Mechanism
(Formal
) = Default_Mechanism
then
227 Typ
:= Underlying_Type
(Etype
(Formal
));
229 -- If there is no underlying type, then skip this processing and
230 -- leave the convention set to Default_Mechanism. It seems odd
231 -- that there should ever be such cases but there are (see
232 -- comments for filed regression tests 1418-001 and 1912-009) ???
238 case Convention
(E
) is
244 -- Note: all RM defined conventions are treated the same
245 -- from the point of view of parameter passing mechanims
247 when Convention_Ada |
248 Convention_Intrinsic |
250 Convention_Protected |
251 Convention_Stubbed
=>
253 -- By reference types are passed by reference (RM 6.2(4))
255 if Is_By_Reference_Type
(Typ
) then
256 Set_Mechanism
(Formal
, By_Reference
);
258 -- By copy types are passed by copy (RM 6.2(3))
260 elsif Is_By_Copy_Type
(Typ
) then
261 Set_Mechanism
(Formal
, By_Copy
);
263 -- All other types we leave the Default_Mechanism set, so
264 -- that the backend can choose the appropriate method.
274 -- Note: Assembler, C++, Java, Stdcall also use C conventions
276 when Convention_Assembler |
280 Convention_Stdcall
=>
282 -- The following values are passed by copy
284 -- IN Scalar parameters (RM B.3(66))
285 -- IN parameters of access types (RM B.3(67))
286 -- Access parameters (RM B.3(68))
287 -- Access to subprogram types (RM B.3(71))
289 -- Note: in the case of access parameters, it is the
290 -- pointer that is passed by value. In GNAT access
291 -- parameters are treated as IN parameters of an
292 -- anonymous access type, so this falls out free.
294 -- The bottom line is that all IN elementary types
295 -- are passed by copy in GNAT.
297 if Is_Elementary_Type
(Typ
) then
298 if Ekind
(Formal
) = E_In_Parameter
then
299 Set_Mechanism
(Formal
, By_Copy
);
301 -- OUT and IN OUT parameters of elementary types are
302 -- passed by reference (RM B.3(68)). Note that we are
303 -- not following the advice to pass the address of a
304 -- copy to preserve by copy semantics.
307 Set_Mechanism
(Formal
, By_Reference
);
310 -- Records are normally passed by reference (RM B.3(69)).
311 -- However, this can be overridden by the use of the
312 -- C_Pass_By_Copy pragma or C_Pass_By_Copy convention.
314 elsif Is_Record_Type
(Typ
) then
316 -- If the record is not convention C, then we always
317 -- pass by reference, C_Pass_By_Copy does not apply.
319 if Convention
(Typ
) /= Convention_C
then
320 Set_Mechanism
(Formal
, By_Reference
);
322 -- If convention C_Pass_By_Copy was specified for
323 -- the record type, then we pass by copy.
325 elsif C_Pass_By_Copy
(Typ
) then
326 Set_Mechanism
(Formal
, By_Copy
);
328 -- Otherwise, for a C convention record, we set the
329 -- convention in accordance with a possible use of
330 -- the C_Pass_By_Copy pragma. Note that the value of
331 -- Default_C_Record_Mechanism in the absence of such
332 -- a pragma is By_Reference.
335 Set_Mechanism
(Formal
, Default_C_Record_Mechanism
);
338 -- Array types are passed by reference (B.3 (71))
340 elsif Is_Array_Type
(Typ
) then
341 Set_Mechanism
(Formal
, By_Reference
);
343 -- For all other types, use Default_Mechanism mechanism
353 when Convention_COBOL
=>
355 -- Access parameters (which in GNAT look like IN parameters
356 -- of an access type) are passed by copy (RM B.4(96)) as
357 -- are all other IN parameters of scalar type (RM B.4(97)).
359 -- For now we pass these parameters by reference as well.
360 -- The RM specifies the intent BY_CONTENT, but gigi does
361 -- not currently transform By_Copy properly. If we pass by
362 -- reference, it will be imperative to introduce copies ???
364 if Is_Elementary_Type
(Typ
)
365 and then Ekind
(Formal
) = E_In_Parameter
367 Set_Mechanism
(Formal
, By_Reference
);
369 -- All other parameters (i.e. all non-scalar types, and
370 -- all OUT or IN OUT parameters) are passed by reference.
371 -- Note that at the moment we are not bothering to make
372 -- copies of scalar types as recommended in the RM.
375 Set_Mechanism
(Formal
, By_Reference
);
382 when Convention_Fortran
=>
384 -- In OpenVMS, pass a character of array of character
385 -- value using Descriptor(S).
388 and then (Root_Type
(Typ
) = Standard_Character
392 Root_Type
(Component_Type
(Typ
)) =
395 Set_Mechanism
(Formal
, By_Descriptor_S
);
397 -- Access types are passed by default (presumably this
398 -- will mean they are passed by copy)
400 elsif Is_Access_Type
(Typ
) then
403 -- For now, we pass all other parameters by reference.
404 -- It is not clear that this is right in the long run,
405 -- but it seems to correspond to what gnu f77 wants.
408 Set_Mechanism
(Formal
, By_Reference
);
414 <<Skip_Formal
>> -- remove this when problem above is fixed ???
416 Next_Formal
(Formal
);
419 -- Now deal with return type, we always leave the default mechanism
420 -- set except for the case of returning a By_Reference type for an
421 -- Ada convention, where we force return by reference
423 if Ekind
(E
) = E_Function
424 and then Mechanism
(E
) = Default_Mechanism
425 and then not Has_Foreign_Convention
(E
)
426 and then Is_By_Reference_Type
(Etype
(E
))
428 Set_Mechanism
(E
, By_Reference
);