From 8cca8e5e53a8e84cbc74201817150fcea0190cab Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 15 Jul 2009 11:16:49 -0400 Subject: [PATCH] compile: new package primitive_info Signed-off-by: Jean Privat --- src/compiling/compiling_base.nit | 126 +------------------------------- src/primitive_info.nit | 148 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 125 deletions(-) create mode 100644 src/primitive_info.nit diff --git a/src/compiling/compiling_base.nit b/src/compiling/compiling_base.nit index cddf846..4debc4f 100644 --- a/src/compiling/compiling_base.nit +++ b/src/compiling/compiling_base.nit @@ -19,6 +19,7 @@ package compiling_base import syntax private import utils +import primitive_info redef class ToolContext readable writable var _global: Bool = false @@ -161,131 +162,6 @@ redef class MMGlobalClass end end -redef class MMLocalClass - # Cached primitive_info result - var _primitive_info_cache: nullable PrimitiveInfo = null - - # If primitive_info result cached? - var _primitive_info_b: Bool = false - - # Return the primitive information of the class. - # Return null if the class is not primitive - # FIXME: Only here since there is no universal type yet - fun primitive_info: nullable PrimitiveInfo - do - if _primitive_info_b == true then return _primitive_info_cache - - var ctypes = once primitive_ctypes - if ctypes.has_key(name) then - _primitive_info_cache = ctypes[name] - _primitive_info_b = true - return _primitive_info_cache - end - var i = ctypes.iterator - while i.is_ok do - var n = i.key - if module.has_global_class_named(n) then - var c = module.class_by_name(n) - if cshe < c then - _primitive_info_cache = i.item - _primitive_info_b = true - return _primitive_info_cache - end - end - i.next - end - _primitive_info_b = true - return null - end - - # Static information of primitive types - private fun primitive_ctypes: HashMap[Symbol, PrimitiveInfo] - do - var res = new HashMap[Symbol, PrimitiveInfo] - var pnames = ["Int", "Char", "Bool", "Float", "NativeString", "NativeArray", "Pointer"] - var tagged = [true, true, true, false, false, false, false] - var cnames = ["bigint", "char", "int", "float", "char *", "val_t *", "void *"] - for i in [0..pnames.length[ do - var n = pnames[i].to_symbol - var pi = new PrimitiveInfo(n, tagged[i], cnames[i]) - res[n] = pi - end - return res - end -end - -# Information about a primitive class -class PrimitiveInfo - # The name of the class - readable var _name: Symbol - - # Is the class tagged (aka not boxed) - readable var _tagged: Bool - - # The corresponding c type for the primitive value - readable var _cname: String - - private init(n: Symbol, t: Bool, c: String) - do - _name = n - _tagged = t - _cname = c - end -end - -redef class MMType - # The corresponding c type - fun cname: String - do - var pi = local_class.primitive_info - if pi == null then - return "val_t" - else - return pi.cname - end - end - - # The default c value for uninitialized types. - # Return "null" for non primitive types and something more specific for primitive types - fun default_cvalue: String - do - var pi = local_class.primitive_info - if pi != null and pi.tagged then - return "TAG_{local_class.name}(({pi.cname})0)" - else - return "NIT_NULL" - end - end - - # Box (or tag) a primitive value - # Is identity if not primitive - fun boxtype(s: String): String - do - var pi = local_class.primitive_info - if pi == null then - return s - else if pi.tagged then - return "TAG_{local_class.name}({s})" - else - return "BOX_{local_class.name}({s})" - end - end - - # Unbox (or untag) a primitive value - # Is identity if not primitive - fun unboxtype(s: String): String - do - var pi = local_class.primitive_info - if pi == null then - return s - else if pi.tagged then - return "UNTAG_{local_class.name}({s})" - else - return "UNBOX_{local_class.name}({s})" - end - end -end - redef class MMLocalProperty # Cacher result of cname var _cname_cache: nullable String diff --git a/src/primitive_info.nit b/src/primitive_info.nit new file mode 100644 index 0000000..f597e4b --- /dev/null +++ b/src/primitive_info.nit @@ -0,0 +1,148 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2008 Jean Privat +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Common things for NIT compilation and C generation +package primitive_info + +#FIXME Split this package into 2: one in metamodel and one in compiling + +import metamodel + +redef class MMLocalClass + # Cached primitive_info result + var _primitive_info_cache: nullable PrimitiveInfo = null + + # If primitive_info result cached? + var _primitive_info_b: Bool = false + + # Return the primitive information of the class. + # Return null if the class is not primitive + # FIXME: Only here since there is no universal type yet + fun primitive_info: nullable PrimitiveInfo + do + if _primitive_info_b == true then return _primitive_info_cache + + var ctypes = once primitive_ctypes + if ctypes.has_key(name) then + _primitive_info_cache = ctypes[name] + _primitive_info_b = true + return _primitive_info_cache + end + var i = ctypes.iterator + while i.is_ok do + var n = i.key + if module.has_global_class_named(n) then + var c = module.class_by_name(n) + if cshe < c then + _primitive_info_cache = i.item + _primitive_info_b = true + return _primitive_info_cache + end + end + i.next + end + _primitive_info_b = true + return null + end + + # Static information of primitive types + private fun primitive_ctypes: HashMap[Symbol, PrimitiveInfo] + do + var res = new HashMap[Symbol, PrimitiveInfo] + var pnames = ["Int", "Char", "Bool", "Float", "NativeString", "NativeArray", "Pointer"] + var tagged = [true, true, true, false, false, false, false] + var cnames = ["bigint", "char", "int", "float", "char *", "val_t *", "void *"] + for i in [0..pnames.length[ do + var n = pnames[i].to_symbol + var pi = new PrimitiveInfo(n, tagged[i], cnames[i]) + res[n] = pi + end + return res + end +end + +# Information about a primitive class +class PrimitiveInfo + # The name of the class + readable var _name: Symbol + + # Is the class tagged (aka not boxed) + readable var _tagged: Bool + + # The corresponding c type for the primitive value + readable var _cname: String + + private init(n: Symbol, t: Bool, c: String) + do + _name = n + _tagged = t + _cname = c + end +end + +redef class MMType + # The corresponding c type + fun cname: String + do + var pi = local_class.primitive_info + if pi == null then + return "val_t" + else + return pi.cname + end + end + + # The default c value for uninitialized types. + # Return "null" for non primitive types and something more specific for primitive types + fun default_cvalue: String + do + var pi = local_class.primitive_info + if pi != null and pi.tagged then + return "TAG_{local_class.name}(({pi.cname})0)" + else + return "NIT_NULL" + end + end + + # Box (or tag) a primitive value + # Is identity if not primitive + fun boxtype(s: String): String + do + var pi = local_class.primitive_info + if pi == null then + return s + else if pi.tagged then + return "TAG_{local_class.name}({s})" + else + return "BOX_{local_class.name}({s})" + end + end + + # Unbox (or untag) a primitive value + # Is identity if not primitive + fun unboxtype(s: String): String + do + var pi = local_class.primitive_info + if pi == null then + return s + else if pi.tagged then + return "UNTAG_{local_class.name}({s})" + else + return "UNBOX_{local_class.name}({s})" + end + end +end + -- 1.7.9.5