From: Jean Privat Date: Thu, 13 Oct 2011 00:24:33 +0000 (-0400) Subject: Merge branch 'pu/class_name' into wip X-Git-Tag: v0.5~40 X-Git-Url: http://nitlanguage.org?hp=50db753cdb40501d9f9ea7db8e04b5a03f55b95e Merge branch 'pu/class_name' into wip --- diff --git a/clib/nit_common.h b/clib/nit_common.h index 08fb294..7e0fce8 100644 --- a/clib/nit_common.h +++ b/clib/nit_common.h @@ -22,9 +22,10 @@ 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 */ diff --git a/lib/standard/kernel.nit b/lib/standard/kernel.nit index 30c592d..17b3268 100644 --- a/lib/standard/kernel.nit +++ b/lib/standard/kernel.nit @@ -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 diff --git a/lib/standard/string.nit b/lib/standard/string.nit index d61b260..bcd0445 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -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 "" fun inspect: String do - var r = inspect_head - # r.add('>') - return r + return "<{inspect_head}>" end - # Return " +# +# 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 index 0000000..d51f048 --- /dev/null +++ b/tests/base_output_class_name.nit @@ -0,0 +1,34 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2009 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. + +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 index 0000000..84e61ef --- /dev/null +++ b/tests/sav/base_class_name.sav @@ -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 index 0000000..84e61ef --- /dev/null +++ b/tests/sav/base_output_class_name.sav @@ -0,0 +1,6 @@ +String +Int +Test +Test +MyArray +TotoToto