391a2b03bb93973ace663e7a66cc54db350cd1e9
[nit.git] / src / nitni / nitni_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Native interface related services (used underneath the FFI)
18 #
19 # All nitni properties may not be visible to the user but they are
20 # shared between engines
21 module nitni_base
22
23 import parser
24 import modelbuilder # builder only for externcalls
25
26 redef class MMethod
27 # Short name of this method in C (without the class name)
28 fun short_cname: String do
29 var nit_name = name
30
31 if nit_name == "+" then return "_plus"
32 if nit_name == "-" then return "_minus"
33 if nit_name == "*" then return "_star"
34 if nit_name == "/" then return "_slash"
35 if nit_name == "%" then return "_percent"
36 if nit_name == "[]" then return "_index"
37 if nit_name == "[]=" then return "_index_assign"
38 if nit_name == "==" then return "_equal"
39 if nit_name == "<" then return "_less"
40 if nit_name == ">" then return "_geater"
41 if nit_name == "<=" then return "_less_or_equal"
42 if nit_name == ">=" then return "_greater_or_equal"
43 if nit_name == "!=" then return "_not_equal"
44 if nit_name == "<<" then return "_left"
45 if nit_name == ">>" then return "_right"
46 if nit_name == "<=>" then return "_starship"
47
48 if nit_name.chars.last == '=' then return "{nit_name.substring(0, nit_name.length-1)}__assign"
49 return nit_name
50 end
51 end
52
53 redef class MModule
54 # Mangled name of this module in C
55 fun cname: String do return name
56 end
57
58 redef class MMethodDef
59 # Name of the function to callback this method from C,
60 # also used in other functions names used for this method.
61 fun cname: String do return "{mclassdef.mclass.name}_{mproperty.short_cname}"
62 end
63
64 redef class MType
65 # Representation of this type in pure C on the FFI extern side
66 # Object -> Object
67 # Pointer -> void*
68 fun cname: String is abstract
69
70 # Representation of this type in C for the internal of the system
71 # Hides extern types.
72 fun cname_blind: String is abstract
73
74 # Representation of this type in mangled C
75 # Object -> Object
76 # Pointer -> Pointer
77 fun mangled_cname: String is abstract
78
79 # Does this types has a primitive reprensentation
80 # type Object is_primitive? false
81 # type Pointer is_primitive? true
82 fun is_cprimitive: Bool is abstract
83 end
84
85 redef class MClassType
86 redef fun cname
87 do
88 var name = mclass.name
89 if name == "Bool" then return "int"
90 if name == "Char" then return "char"
91 if name == "Float" then return "double"
92 if name == "Int" then return "int"
93 if name == "NativeString" then return "char*"
94 if mclass.kind == extern_kind then
95 var ctype = mclass.ctype
96 assert ctype != null
97 return ctype
98 end
99 return mangled_cname
100 end
101
102 redef fun cname_blind do
103 var name = mclass.name
104 if name == "Bool" then return "int"
105 if name == "Char" then return "char"
106 if name == "Float" then return "double"
107 if name == "Int" then return "int"
108 if name == "NativeString" then return "char*"
109 if mclass.kind == extern_kind then return "void*"
110 return "struct nitni_instance *"
111 end
112
113 redef fun mangled_cname do return mclass.name
114
115 redef fun is_cprimitive do return mclass.kind == extern_kind or
116 (once ["Bool", "Char", "Float", "Int", "NativeString"]).has(mclass.name)
117 end
118
119 redef class MNullableType
120 redef fun cname do return mangled_cname
121 redef fun cname_blind do return "struct nitni_instance *"
122 redef fun mangled_cname do return "nullable_{mtype.mangled_cname}"
123 redef fun is_cprimitive do return false
124 end
125
126 redef class MVirtualType
127 redef fun mangled_cname: String do return to_s
128 end
129
130 redef class MGenericType
131 redef fun cname do return mangled_cname
132 redef fun mangled_cname
133 do
134 var base = super
135
136 var params = new Array[String]
137 for arg in arguments do params.add(arg.mangled_cname)
138
139 return "{base}_of_{params.join("_")}"
140 end
141 end
142
143 redef class MClass
144 fun ctype: nullable String
145 do
146 assert kind == extern_kind
147 return "void*"
148 end
149 end