1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 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 # Native interface related services (used underneath the FFI)
19 # All nitni properties may not be visible to the user but they are
20 # shared between engines
24 import modelbuilder
# builder only for externcalls
27 # Short name of this method in C (without the class name)
28 fun short_cname
: String do
31 if nit_name
== "+" then return "_plus"
32 if nit_name
== "-" then return "_minus"
33 if nit_name
== "unary -" then return "_unary_minus"
34 if nit_name
== "*" then return "_star"
35 if nit_name
== "/" then return "_slash"
36 if nit_name
== "%" then return "_percent"
37 if nit_name
== "[]" then return "_index"
38 if nit_name
== "[]=" then return "_index_assign"
39 if nit_name
== "==" then return "_equal"
40 if nit_name
== "<" then return "_less"
41 if nit_name
== ">" then return "_geater"
42 if nit_name
== "<=" then return "_less_or_equal"
43 if nit_name
== ">=" then return "_greater_or_equal"
44 if nit_name
== "!=" then return "_not_equal"
45 if nit_name
== "<<" then return "_left"
46 if nit_name
== ">>" then return "_right"
47 if nit_name
== "<=>" then return "_starship"
49 if nit_name
.chars
.last
== '=' then return "{nit_name.substring(0, nit_name.length-1)}__assign"
55 # Mangled name of this module in C
56 fun cname
: String do return name
59 redef class MMethodDef
60 # Name of the function to callback this method from C,
61 # also used in other functions names used for this method.
62 fun cname
: String do return "{mclassdef.mclass.name}_{mproperty.short_cname}"
66 # Representation of this type in pure C on the FFI extern side
69 fun cname
: String is abstract
71 # Representation of this type in C for the internal of the system
73 fun cname_blind
: String is abstract
75 # Representation of this type in mangled C
78 fun mangled_cname
: String is abstract
80 # Does this types has a primitive reprensentation
81 # type Object is_primitive? false
82 # type Pointer is_primitive? true
83 fun is_cprimitive
: Bool is abstract
86 redef class MClassType
89 var name
= mclass
.name
90 if name
== "Bool" then return "int"
91 if name
== "Char" then return "char"
92 if name
== "Float" then return "double"
93 if name
== "Int" then return "int"
94 if name
== "NativeString" then return "char*"
95 if mclass
.kind
== extern_kind
then
96 var ctype
= mclass
.ctype
103 redef fun cname_blind
do
104 var name
= mclass
.name
105 if name
== "Bool" then return "int"
106 if name
== "Char" then return "char"
107 if name
== "Float" then return "double"
108 if name
== "Int" then return "int"
109 if name
== "NativeString" then return "char*"
110 if mclass
.kind
== extern_kind
then return "void*"
111 return "struct nitni_instance *"
114 redef fun mangled_cname
do return mclass
.name
116 redef fun is_cprimitive
do return mclass
.kind
== extern_kind
or
117 (once
["Bool", "Char", "Float", "Int", "NativeString"]).has
(mclass
.name
)
120 redef class MNullableType
121 redef fun cname
do return mangled_cname
122 redef fun cname_blind
do return "struct nitni_instance *"
123 redef fun mangled_cname
do return "nullable_{mtype.mangled_cname}"
124 redef fun is_cprimitive
do return false
127 redef class MVirtualType
128 redef fun mangled_cname
: String do return to_s
131 redef class MGenericType
132 redef fun cname
do return mangled_cname
133 redef fun mangled_cname
137 var params
= new Array[String]
138 for arg
in arguments
do params
.add
(arg
.mangled_cname
)
140 return "{base}_of_{params.join("_")}"
145 fun ctype
: nullable String
147 assert kind
== extern_kind