5e3b7a6f260609bbd1c7aa062d795a755df53146
[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 == "unary -" then return "_unary_minus"
34 if nit_name == "unary +" then return "_unary_plus"
35 if nit_name == "*" then return "_star"
36 if nit_name == "/" then return "_slash"
37 if nit_name == "%" then return "_percent"
38 if nit_name == "[]" then return "_index"
39 if nit_name == "[]=" then return "_index_assign"
40 if nit_name == "==" then return "_equal"
41 if nit_name == "<" then return "_less"
42 if nit_name == ">" then return "_geater"
43 if nit_name == "<=" then return "_less_or_equal"
44 if nit_name == ">=" then return "_greater_or_equal"
45 if nit_name == "!=" then return "_not_equal"
46 if nit_name == "<<" then return "_left"
47 if nit_name == ">>" then return "_right"
48 if nit_name == "<=>" then return "_starship"
49
50 if nit_name.chars.last == '=' then return "{nit_name.substring(0, nit_name.length-1)}__assign"
51 return nit_name
52 end
53 end
54
55 redef class MMethodDef
56 # Name of the function to callback this method from C,
57 # also used in other functions names used for this method.
58 fun cname: String do return "{mclassdef.mclass.name}_{mproperty.short_cname}"
59 end
60
61 redef class MType
62 # Representation of this type in pure C on the FFI extern side
63 # Object -> Object
64 # Pointer -> void*
65 fun cname: String is abstract
66
67 # Representation of this type in C for the internal of the system
68 # Hides extern types.
69 fun cname_blind: String is abstract
70
71 # Representation of this type in mangled C
72 # Object -> Object
73 # Pointer -> Pointer
74 fun mangled_cname: String is abstract
75
76 # Does this types has a primitive reprensentation
77 # type Object is_primitive? false
78 # type Pointer is_primitive? true
79 fun is_cprimitive: Bool is abstract
80 end
81
82 redef class MClassType
83 redef fun cname
84 do
85 var name = mclass.name
86 if name == "Bool" then return "int"
87 if name == "Char" then return "char"
88 if name == "Float" then return "double"
89 if name == "Int" then return "long"
90 if name == "NativeString" then return "char*"
91 if mclass.kind == extern_kind then
92 var ctype = mclass.ctype
93 assert ctype != null
94 return ctype
95 end
96 return mangled_cname
97 end
98
99 redef fun cname_blind do
100 var name = mclass.name
101 if name == "Bool" then return "int"
102 if name == "Char" then return "char"
103 if name == "Float" then return "double"
104 if name == "Int" then return "long"
105 if name == "NativeString" then return "char*"
106 if mclass.kind == extern_kind then return "void*"
107 return "struct nitni_instance *"
108 end
109
110 redef fun mangled_cname do return mclass.name
111
112 redef fun is_cprimitive do return mclass.kind == extern_kind or
113 (once ["Bool", "Char", "Float", "Int", "NativeString"]).has(mclass.name)
114 end
115
116 redef class MNullableType
117 redef fun cname do return mangled_cname
118 redef fun cname_blind do return "struct nitni_instance *"
119 redef fun mangled_cname do return "nullable_{mtype.mangled_cname}"
120 redef fun is_cprimitive do return false
121 end
122
123 redef class MVirtualType
124 redef fun mangled_cname: String do return to_s
125 end
126
127 redef class MGenericType
128 redef fun cname do return mangled_cname
129 redef fun mangled_cname
130 do
131 var base = super
132
133 var params = new Array[String]
134 for arg in arguments do params.add(arg.mangled_cname)
135
136 return "{base}_of_{params.join("_")}"
137 end
138 end
139
140 redef class MClass
141 fun ctype: nullable String
142 do
143 assert kind == extern_kind
144 return "void*"
145 end
146 end