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
40 fun mangled_name
: String
42 var pi
= local_class
.primitive_info
44 return "nullable_{local_class.name.to_s}"
46 return local_class
.name
.to_s
50 # Return the expression to convert this type from its native version.
51 fun from_native
( name
: String ) : String
53 if ( local_class
.primitive_info
!= null or local_class
.global
.is_extern
) and
55 return boxtype
( name
)
57 var getter
= "{name}->ref.val"
59 return boxtype
( getter
)
63 # Return the expression to convert and assign this type to its
64 # friendly/native version.
65 fun assign_to_friendly
( native_name
, nit_name
: String ) : String
67 if ( local_class
.primitive_info
!= null or local_class
.global
.is_extern
) and
69 return "{native_name} = {unboxtype( nit_name )}"
71 return "{native_name}->ref.val = {unboxtype( nit_name )}"
75 # Return the expression to convert and assign this type to its
76 # generated C version, from friendly/native.
77 fun assign_from_friendly
( nit_name
, native_name
: String ) : String
79 if ( local_class
.primitive_info
!= null or local_class
.global
.is_extern
) and
80 not is_nullable
then # int, float, point/void* ...
81 return "{nit_name} = {boxtype(native_name)}"
83 return "{nit_name} = {native_name}->ref.val"
87 fun uses_nitni_ref
: Bool do return local_class
.primitive_info
== null or is_nullable
89 fun friendly_null_getter
: String
91 return "null_{as_notnull.mangled_name}"
94 fun local_friendly_null_getter_from
( m
: MMModule ) : String
96 return "{m.to_s}_{friendly_null_getter}"
101 # Local friendly name from a given module.
102 # Used to avoid conflits between modules.
103 fun local_friendly_name_from
( m
: MMModule, local_class
: MMLocalClass ) : String
105 return "{m.to_s}_{friendly_extern_name(local_class)}"
108 fun friendly_super_cname
: String
110 return "{friendly_extern_name(local_class)}___super"
113 fun local_friendly_super_name_from
( m
: MMModule ) : String
115 return "{m.to_s}_{friendly_super_cname}"
118 private fun friendly_cparams
( local_class
: MMLocalClass ) : List[ String ]
120 var params
= new List[String]
122 if not global
.is_init
then
123 params
.add
( "{local_class.get_type.friendly_extern_name} recv" )
126 for p
in signature
.params
do
127 params
.add
( "{p.mmtype.friendly_extern_name} {p.name}" )
133 private fun csignature_with_suffix
( suffix
: String, local_class
: MMLocalClass ) : String
135 var name
= "{friendly_extern_name(local_class)}{suffix}"
137 return csignature_with_name
( name
, local_class
)
140 private fun csignature_with_name
( name
: nullable String, local_class
: MMLocalClass ) : String
145 if signature
.return_type
!= null then
146 s
.append
( "{signature.return_type.friendly_extern_name} " )
147 else if global
.is_init
then
148 s
.append
( "{local_class.get_type.friendly_extern_name} " )
155 s
.append
( friendly_extern_name
( local_class
) )
161 var params
= friendly_cparams
( local_class
)
163 s
.append
( "( {params.join( ", " )} )" )
168 fun friendly_csignature
( local_class
: MMLocalClass ) : String
170 return csignature_with_name
( null, local_class
)
173 fun frontier_csignature_from
( m
: MMModule, local_class
: MMLocalClass ) : String
175 return csignature_with_name
( local_friendly_name_from
(m
, local_class
), local_class
)
178 fun friendly_super_csignature
: String
180 return csignature_with_suffix
( "___super", local_class
)
183 fun frontier_super_csignature_from
( m
: MMModule ) : String
185 return csignature_with_name
( local_friendly_super_name_from
(m
), local_class
)
188 fun impl_csignature
: String
190 return csignature_with_suffix
( "___impl", local_class
)
193 # below is for frontier only
194 private fun cparams
: List[ String ]
196 var params
= new List[String]
197 if not global
.is_init
then params
.add
( "val_t recv" )
198 for p
in signature
.params
do params
.add
( "val_t {p.name}" )
202 fun out_csignature
: String
207 if signature
.return_type
!= null or global
.is_init
then
214 if global
.is_init
then
215 s
.append
( "NEW_{local_class}_{cname}" )
217 s
.append
( "{friendly_extern_name(local_class)}___out" )
221 s
.append
( "( {cparams.join( ", " )} )" )
227 redef class MMImportedCast
229 var in_name
: String = "value"
231 fun as_friendly_extern_name
: String do
232 if is_about_nullable_only
then
233 if is_not_null_to_nullable
then # to null
234 # nullable_Object Object_as_nullable( Object o )
235 return "{from.mangled_name}_as_nullable"
236 else if is_nullable_to_not_null
then # from null
237 # Object Object_as_not_null( nullable_Object o )
238 return "{to.mangled_name}_as_not_null"
243 # String Object_as_String( Object o )
244 return "{from.mangled_name}_as_{to.mangled_name}"
248 fun as_local_cname
( m
: MMModule ) : String do
249 return "{m.to_s}_{as_friendly_extern_name}"
252 fun as_local_csignature
( m
: MMModule ) : String
254 return "{to.friendly_extern_name} {as_local_cname( m )}( {from.friendly_extern_name} {in_name} )"
257 fun as_friendly_csignature
: String
259 return "{to.friendly_extern_name} {as_friendly_extern_name}( {from.friendly_extern_name} {in_name} )"
262 fun is_a_friendly_extern_name
: String do
263 if is_about_nullable_only
then
264 if is_not_null_to_nullable
then # to null
265 abort # would always be true
266 else if is_nullable_to_not_null
then # from null
267 # Object_is_null( nullable_Object o )
268 # is opposite from others
269 return "{to.mangled_name}_is_null"
274 # Object_is_a_String( Object o )
275 return "{from.mangled_name}_is_a_{to.mangled_name}"
279 fun is_a_local_cname
( m
: MMModule ) : String do
280 return "{m.to_s}_{is_a_friendly_extern_name}"
283 # Signature of function to check if an object of the "from" type is of
285 fun is_a_local_csignature
( m
: MMModule ) : String
287 return "int {is_a_local_cname( m )}( {from.friendly_extern_name} {in_name} )"
290 fun is_a_friendly_csignature
: String
292 return "int {is_a_friendly_extern_name}( {from.friendly_extern_name} {in_name} )"
297 return from
.hash
+to
.hash