lib : Add class name support
authorAlexandre Terrasa <alexandre@moz-concept.com>
Fri, 29 Apr 2011 18:26:14 +0000 (14:26 -0400)
committerAlexandre Terrasa <alexandre@moz-concept.com>
Fri, 29 Apr 2011 18:26:14 +0000 (14:26 -0400)
- module kernel : Add output_class_name method to Object that display the object class name in console (debug only)
- module String : Add native_class_name method to Object (refinement) that return the class name in NativeString format
- module String : Add class_name method to Object (refinement) that return the class name in String format using native_class_name

Signed-off-by: Alexandre Terrasa <alexandre@moz-concept.com>

clib/nit_common.h
lib/standard/kernel.nit
lib/standard/string.nit
src/compiling/compiling_global.nit
src/compiling/compiling_icode.nit
src/compiling/table_computation.nit
tests/base_class_name.nit [new file with mode: 0644]
tests/base_output_class_name.nit [new file with mode: 0644]
tests/sav/base_class_name.sav [new file with mode: 0644]
tests/sav/base_output_class_name.sav [new file with mode: 0644]

index 08fb294..7e0fce8 100644 (file)
 typedef signed long int bigint;        /* standard int value, must be larger that any poiner */
 typedef bigint (*fun_t) (bigint);                                      /* generic function pointer */
 typedef bigint cid_t;                                  /* class identifier */
+typedef char* cname_t;                                 /* class name */
 typedef bigint val_t;  /* value (everything is a val_t) */
 typedef union obj_tu {union classtable_elt_tu * vft; bigint object_id; val_t objectSize;} *obj_t; /* standard object */
-typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid;} classtable_elt_t;     /* classtable element */
+typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid; cname_t cname;} classtable_elt_t;      /* classtable element */
 typedef struct Nit_NativeArray {const classtable_elt_t * vft; bigint object_id; bigint size; val_t val[1];} * Nit_NativeArray;
 
 typedef classtable_elt_t * classtable_t;                       /* classtable */
index 30c592d..17b3268 100644 (file)
@@ -47,6 +47,9 @@ interface Object
                '>'.output
        end
 
+       # Display class name on stdout (debug only).
+       fun output_class_name is intern 
+
        protected fun exit(exit_value: Int) is intern # Quit the program.
        protected fun sys: Sys is intern # The global sys object
 end
index d61b260..bcd0445 100644 (file)
@@ -398,20 +398,25 @@ redef class Object
        # User redeable representation of `self'.
        fun to_s: String do return inspect
 
+       # The class name of the object in NativeString format.
+       private fun native_class_name: NativeString is intern
+
+       # The class name of the object.
+       # FIXME: real type information is not available at runtime. Therefore, for instance, an instance of List[Bool] has just "List" for classname
+       fun class_name: String do return new String.from_cstring(native_class_name)
+
        # Developper readable representation of `self'.
        # Usualy, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
        fun inspect: String
        do
-               var r = inspect_head
-               # r.add('>')
-               return r
+               return "<{inspect_head}>"
        end
 
-       # Return "<CLASSNAME:#OBJECTID".
-       # This fuction is mainly used with the redefinition of the inspect(0) method
+       # Return "CLASSNAME:#OBJECTID".
+       # This fuction is mainly used with the redefinition of the inspect method
        protected fun inspect_head: String
        do
-               return "<{object_id.to_hex}"
+               return "{class_name}:#{object_id.to_hex}"
        end
 
        protected fun args: Sequence[String]
index da63476..cc81b4e 100644 (file)
@@ -281,6 +281,14 @@ redef class TableEltClassSelfId
        end
 end
 
+redef class TableEltClassSelfName
+       redef fun compile_to_c(v, c)
+       do
+               var prog = v.program
+               return "\"{c.global.name}\" /* {prog.table_information.color(self)}: Class Name */"
+       end
+end
+
 redef class TableEltClassObjectSize
        redef fun compile_to_c(v, c)
        do
index 00e87c4..164bb22 100644 (file)
@@ -864,7 +864,14 @@ redef class INative
                        s = "NEW_NativeArray(UNTAG_Int({regs[1]}), sizeof(val_t))"
                else if n == once "calloc_string".to_symbol then
                        s = "BOX_NativeString((char*)raw_alloc((UNTAG_Int({regs[1]}) * sizeof(char))))"
+               # Add output_class_name native implementation
+               else if n == once "output_class_name".to_symbol then
+                       s = "printf(\"%s\\n\", VAL2VFT({regs[0]})[2].cname);"
+               # Add class_name implementation
+               else if n == once "native_class_name".to_symbol then
+                       s = "BOX_NativeString(VAL2VFT({regs[0]})[2].cname);"
                end
+
                if s == null then
                        var ll = location
                        if ll != null then v.add_instr("fprintf(stderr, \"{ll.to_s}: \");")
index 20461ff..b3c909e 100644 (file)
@@ -162,6 +162,7 @@ redef class Program
 
                ctab.add(new TableEltClassSelfId)
                ctab.add(new TableEltClassObjectSize)
+               ctab.add(new TableEltClassSelfName)
                itab.add(new TableEltVftPointer)
                itab.add(new TableEltObjectId)
 
@@ -536,6 +537,12 @@ class TableEltClassSelfId
        redef fun is_related_to(c) do return true
 end
 
+# The element that represent the class name
+class TableEltClassSelfName
+       super TableElt
+       redef fun is_related_to(c) do return true
+end
+
 # The element that represent the Object Size
 class TableEltClassObjectSize
        super TableElt
diff --git a/tests/base_class_name.nit b/tests/base_class_name.nit
new file mode 100644 (file)
index 0000000..6866ca0
--- /dev/null
@@ -0,0 +1,35 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# 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.
+
+class Test end
+class MyArray[E] end
+
+class Toto
+       redef fun class_name do return "TotoToto"
+end
+
+var test1 = new Test
+var test2: Object = new Test
+var test3 = new MyArray[Int]
+var test4 = new Toto
+
+print "".class_name
+print 1.class_name
+
+print test1.class_name
+print test2.class_name
+print test3.class_name
+print test4.class_name
diff --git a/tests/base_output_class_name.nit b/tests/base_output_class_name.nit
new file mode 100644 (file)
index 0000000..d51f048
--- /dev/null
@@ -0,0 +1,34 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# 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.
+
+class Test end
+class MyArray[E] end
+
+class Toto
+       redef fun output_class_name do print "TotoToto"
+end
+
+var test1 = new Test
+var test2: Object = new Test
+var test3 = new MyArray[Int]
+var test4 = new Toto
+
+"".output_class_name
+1.output_class_name
+test1.output_class_name
+test2.output_class_name
+test3.output_class_name
+test4.output_class_name
diff --git a/tests/sav/base_class_name.sav b/tests/sav/base_class_name.sav
new file mode 100644 (file)
index 0000000..84e61ef
--- /dev/null
@@ -0,0 +1,6 @@
+String
+Int
+Test
+Test
+MyArray
+TotoToto
diff --git a/tests/sav/base_output_class_name.sav b/tests/sav/base_output_class_name.sav
new file mode 100644 (file)
index 0000000..84e61ef
--- /dev/null
@@ -0,0 +1,6 @@
+String
+Int
+Test
+Test
+MyArray
+TotoToto