1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2011 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Information relevant to the native interface
26 # Friendly name used by frontier and custom code.
27 # It's mainly the nit class name or the primitive type when available.
28 fun friendly_extern_name
: String
30 var pi
= local_class
.primitive_info
32 return "nullable_{local_class.name.to_s}"
33 else if pi
== null then
34 return local_class
.name
.to_s
35 else if local_class
.global
.is_extern
then
36 return local_class
.name
.to_s
42 fun mangled_name
: String
44 var pi
= local_class
.primitive_info
46 return "nullable_{local_class.name.to_s}"
48 return local_class
.name
.to_s
52 # Return the expression to convert this type from its native version.
53 fun from_native
( name
: String ) : String
55 if ( local_class
.primitive_info
!= null or local_class
.global
.is_extern
) and
57 return boxtype
( name
)
59 var getter
= "{name}->ref.val"
61 return boxtype
( getter
)
65 # Return the expression to convert and assign this type to its
66 # friendly/native version.
67 fun assign_to_friendly
( native_name
, nit_name
: String ) : String
69 if ( local_class
.primitive_info
!= null or local_class
.global
.is_extern
) and
71 return "{native_name} = {unboxtype( nit_name )}"
73 return "{native_name}->ref.val = {unboxtype( nit_name )}"
77 # Return the expression to convert and assign this type to its
78 # generated C version, from friendly/native.
79 fun assign_from_friendly
( nit_name
, native_name
: String ) : String
81 if ( local_class
.primitive_info
!= null or local_class
.global
.is_extern
) and
82 not is_nullable
then # int, float, point/void* ...
83 return "{nit_name} = {boxtype(native_name)}"
85 return "{nit_name} = {native_name}->ref.val"
89 fun uses_nitni_ref
: Bool do return local_class
.primitive_info
== null or is_nullable
91 fun friendly_null_getter
: String
93 return "null_{as_notnull.mangled_name}"
96 fun local_friendly_null_getter_from
( m
: MMModule ) : String
98 return "{m.to_s}_{friendly_null_getter}"
103 # Local friendly name from a given module.
104 # Used to avoid conflits between modules.
105 fun local_friendly_name_from
( m
: MMModule, local_class
: MMLocalClass ) : String
107 return "{m.to_s}_{friendly_extern_name(local_class)}"
110 fun friendly_super_cname
: String
112 return "{friendly_extern_name(local_class)}___super"
115 fun local_friendly_super_name_from
( m
: MMModule ) : String
117 return "{m.to_s}_{friendly_super_cname}"
120 private fun friendly_cparams
( local_class
: MMLocalClass ) : List[ String ]
122 var params
= new List[String]
124 if not global
.is_init
then
125 params
.add
( "{local_class.get_type.friendly_extern_name} recv" )
128 for p
in signature
.params
do
129 params
.add
( "{p.mmtype.friendly_extern_name} {p.name}" )
135 private fun csignature_with_suffix
( suffix
: String, local_class
: MMLocalClass ) : String
137 var name
= "{friendly_extern_name(local_class)}{suffix}"
139 return csignature_with_name
( name
, local_class
)
142 private fun csignature_with_name
( name
: nullable String, local_class
: MMLocalClass ) : String
147 if signature
.return_type
!= null then
148 s
.append
( "{signature.return_type.friendly_extern_name} " )
149 else if global
.is_init
then
150 s
.append
( "{local_class.get_type.friendly_extern_name} " )
157 s
.append
( friendly_extern_name
( local_class
) )
163 var params
= friendly_cparams
( local_class
)
165 s
.append
( "( {params.join( ", " )} )" )
170 fun friendly_csignature
( local_class
: MMLocalClass ) : String
172 return csignature_with_name
( null, local_class
)
175 fun frontier_csignature_from
( m
: MMModule, local_class
: MMLocalClass ) : String
177 return csignature_with_name
( local_friendly_name_from
(m
, local_class
), local_class
)
180 fun friendly_super_csignature
: String
182 return csignature_with_suffix
( "___super", local_class
)
185 fun frontier_super_csignature_from
( m
: MMModule ) : String
187 return csignature_with_name
( local_friendly_super_name_from
(m
), local_class
)
190 fun impl_csignature
: String
192 return csignature_with_suffix
( "___impl", local_class
)
195 # below is for frontier only
196 private fun cparams
: List[ String ]
198 var params
= new List[String]
199 if not global
.is_init
then params
.add
( "val_t recv" )
200 for p
in signature
.params
do params
.add
( "val_t {p.name}" )
204 fun out_csignature
: String
209 if signature
.return_type
!= null or global
.is_init
then
216 if global
.is_init
then
217 s
.append
( "NEW_{local_class}_{cname}" )
219 s
.append
( "{friendly_extern_name(local_class)}___out" )
223 s
.append
( "( {cparams.join( ", " )} )" )
229 redef class MMImportedCast
231 var in_name
: String = "value"
233 fun as_friendly_extern_name
: String do
234 if is_about_nullable_only
then
235 if is_not_null_to_nullable
then # to null
236 # nullable_Object Object_as_nullable( Object o )
237 return "{from.mangled_name}_as_nullable"
238 else if is_nullable_to_not_null
then # from null
239 # Object Object_as_not_null( nullable_Object o )
240 return "{to.mangled_name}_as_not_null"
245 # String Object_as_String( Object o )
246 return "{from.mangled_name}_as_{to.mangled_name}"
250 fun as_local_cname
( m
: MMModule ) : String do
251 return "{m.to_s}_{as_friendly_extern_name}"
254 fun as_local_csignature
( m
: MMModule ) : String
256 return "{to.friendly_extern_name} {as_local_cname( m )}( {from.friendly_extern_name} {in_name} )"
259 fun as_friendly_csignature
: String
261 return "{to.friendly_extern_name} {as_friendly_extern_name}( {from.friendly_extern_name} {in_name} )"
264 fun is_a_friendly_extern_name
: String do
265 if is_about_nullable_only
then
266 if is_not_null_to_nullable
then # to null
267 abort # would always be true
268 else if is_nullable_to_not_null
then # from null
269 # Object_is_null( nullable_Object o )
270 # is opposite from others
271 return "{to.mangled_name}_is_null"
276 # Object_is_a_String( Object o )
277 return "{from.mangled_name}_is_a_{to.mangled_name}"
281 fun is_a_local_cname
( m
: MMModule ) : String do
282 return "{m.to_s}_{is_a_friendly_extern_name}"
285 # Signature of function to check if an object of the "from" type is of
287 fun is_a_local_csignature
( m
: MMModule ) : String
289 return "int {is_a_local_cname( m )}( {from.friendly_extern_name} {in_name} )"
292 fun is_a_friendly_csignature
: String
294 return "int {is_a_friendly_extern_name}( {from.friendly_extern_name} {in_name} )"
299 return from
.hash
+to
.hash