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