2005-08-16 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / libjava / defineclass.cc
blobd12e32784817d8d9d55e8a2512057c0582d5ec64
1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 /*
12 Author: Kresten Krab Thorup <krab@gnu.org>
14 Written using the online versions of Java Language Specification (1st
15 ed.) and The Java Virtual Machine Specification (2nd ed.).
17 Future work may include reading (and handling) attributes which are
18 currently being ignored ("InnerClasses", "LineNumber", etc...).
21 #include <config.h>
23 #include <java-interp.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <java-cpool.h>
28 #include <gcj/cni.h>
29 #include <execution.h>
31 #include <java/lang/Class.h>
32 #include <java/lang/Float.h>
33 #include <java/lang/Double.h>
34 #include <java/lang/Character.h>
35 #include <java/lang/LinkageError.h>
36 #include <java/lang/InternalError.h>
37 #include <java/lang/ClassFormatError.h>
38 #include <java/lang/NoClassDefFoundError.h>
39 #include <java/lang/ClassCircularityError.h>
40 #include <java/lang/IncompatibleClassChangeError.h>
41 #include <java/lang/reflect/Modifier.h>
42 #include <java/security/ProtectionDomain.h>
44 using namespace gcj;
46 #ifdef INTERPRETER
48 // these go in some separate functions, to avoid having _Jv_InitClass
49 // inserted all over the place.
50 static void throw_internal_error (char *msg)
51 __attribute__ ((__noreturn__));
52 static void throw_no_class_def_found_error (jstring msg)
53 __attribute__ ((__noreturn__));
54 static void throw_no_class_def_found_error (char *msg)
55 __attribute__ ((__noreturn__));
56 static void throw_class_format_error (jstring msg)
57 __attribute__ ((__noreturn__));
58 static void throw_incompatible_class_change_error (jstring msg)
59 __attribute__ ((__noreturn__));
60 static void throw_class_circularity_error (jstring msg)
61 __attribute__ ((__noreturn__));
63 /**
64 * We define class reading using a class. It is practical, since then
65 * the entire class-reader can be a friend of class Class (it needs to
66 * write all it's different structures); but also because this makes it
67 * easy to make class definition reentrant, and thus two threads can be
68 * defining classes at the same time. This class (_Jv_ClassReader) is
69 * never exposed outside this file, so we don't have to worry about
70 * public or private members here.
73 struct _Jv_ClassReader
76 // do verification? Currently, there is no option to disable this.
77 // This flag just controls the verificaiton done by the class loader;
78 // i.e., checking the integrity of the constant pool; and it is
79 // allways on. You always want this as far as I can see, but it also
80 // controls weither identifiers and type descriptors/signatures are
81 // verified as legal. This could be somewhat more expensive since it
82 // will call Character.isJavaIdentifier{Start,Part} for each character
83 // in any identifier (field name or method name) it comes by. Thus,
84 // it might be useful to turn off this verification for classes that
85 // come from a trusted source. However, for GCJ, trusted classes are
86 // most likely to be linked in.
88 bool verify;
90 // input data.
91 unsigned char *bytes;
92 int len;
94 // current input position
95 int pos;
97 // the constant pool data
98 int pool_count;
99 unsigned char *tags;
100 unsigned int *offsets;
102 // the class to define (see java-interp.h)
103 jclass def;
105 // the classes associated interpreter data.
106 _Jv_InterpClass *def_interp;
108 // The name we found.
109 _Jv_Utf8Const **found_name;
111 /* check that the given number of input bytes are available */
112 inline void check (int num)
114 if (pos + num > len)
115 throw_class_format_error ("Premature end of data");
118 /* skip a given number of bytes in input */
119 inline void skip (int num)
121 check (num);
122 pos += num;
125 /* read an unsignend 1-byte unit */
126 inline static jint get1u (unsigned char* bytes)
128 return bytes[0];
131 /* read an unsigned 1-byte unit */
132 inline jint read1u ()
134 skip (1);
135 return get1u (bytes+pos-1);
138 /* read an unsigned 2-byte unit */
139 inline static jint get2u (unsigned char *bytes)
141 return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
144 /* read an unsigned 2-byte unit */
145 inline jint read2u ()
147 skip (2);
148 return get2u (bytes+pos-2);
151 /* read a 4-byte unit */
152 static jint get4 (unsigned char *bytes)
154 return (((jint)bytes[0]) << 24)
155 | (((jint)bytes[1]) << 16)
156 | (((jint)bytes[2]) << 8)
157 | (((jint)bytes[3]) << 0);
160 /* read a 4-byte unit, (we don't do that quite so often) */
161 inline jint read4 ()
163 skip (4);
164 return get4 (bytes+pos-4);
167 /* read a 8-byte unit */
168 static jlong get8 (unsigned char* bytes)
170 return (((jlong)bytes[0]) << 56)
171 | (((jlong)bytes[1]) << 48)
172 | (((jlong)bytes[2]) << 40)
173 | (((jlong)bytes[3]) << 32)
174 | (((jlong)bytes[4]) << 24)
175 | (((jlong)bytes[5]) << 16)
176 | (((jlong)bytes[6]) << 8)
177 | (((jlong)bytes[7]) << 0);
180 /* read a 8-byte unit */
181 inline jlong read8 ()
183 skip (8);
184 return get8 (bytes+pos-8);
187 inline void check_tag (int index, char expected_tag)
189 if (index < 0
190 || index > pool_count
191 || tags[index] != expected_tag)
192 throw_class_format_error ("erroneous constant pool tag");
195 inline void verify_identifier (_Jv_Utf8Const* name)
197 if (! _Jv_VerifyIdentifier (name))
198 throw_class_format_error ("erroneous identifier");
201 inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
203 if (! _Jv_VerifyClassName (ptr, length))
204 throw_class_format_error ("erroneous class name");
207 inline void verify_classname (_Jv_Utf8Const *name)
209 if (! _Jv_VerifyClassName (name))
210 throw_class_format_error ("erroneous class name");
213 inline void verify_field_signature (_Jv_Utf8Const *sig)
215 if (! _Jv_VerifyFieldSignature (sig))
216 throw_class_format_error ("erroneous type descriptor");
219 inline void verify_method_signature (_Jv_Utf8Const *sig)
221 if (! _Jv_VerifyMethodSignature (sig))
222 throw_class_format_error ("erroneous type descriptor");
225 _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length,
226 java::security::ProtectionDomain *pd,
227 _Jv_Utf8Const **name_result)
229 if (klass == 0 || length < 0 || offset+length > data->length)
230 throw_internal_error ("arguments to _Jv_DefineClass");
232 verify = true;
233 bytes = (unsigned char*) (elements (data)+offset);
234 len = length;
235 pos = 0;
236 def = klass;
237 found_name = name_result;
239 def->size_in_bytes = -1;
240 def->vtable_method_count = -1;
241 def->engine = &_Jv_soleInterpreterEngine;
242 def->protectionDomain = pd;
245 /** and here goes the parser members defined out-of-line */
246 void parse ();
247 void read_constpool ();
248 void prepare_pool_entry (int index, unsigned char tag);
249 void read_fields ();
250 void read_methods ();
251 void read_one_class_attribute ();
252 void read_one_method_attribute (int method);
253 void read_one_code_attribute (int method);
254 void read_one_field_attribute (int field);
255 void throw_class_format_error (char *msg);
257 /** check an utf8 entry, without creating a Utf8Const object */
258 bool is_attribute_name (int index, char *name);
260 /** here goes the class-loader members defined out-of-line */
261 void handleConstantPool ();
262 void handleClassBegin (int, int, int);
263 void handleInterfacesBegin (int);
264 void handleInterface (int, int);
265 void handleFieldsBegin (int);
266 void handleField (int, int, int, int);
267 void handleFieldsEnd ();
268 void handleConstantValueAttribute (int,int);
269 void handleMethodsBegin (int);
270 void handleMethod (int, int, int, int);
271 void handleMethodsEnd ();
272 void handleCodeAttribute (int, int, int, int, int, int);
273 void handleExceptionTableEntry (int, int, int, int, int, int);
275 void checkExtends (jclass sub, jclass super);
276 void checkImplements (jclass sub, jclass super);
279 * FIXME: we should keep a hash table of utf8-strings, since many will
280 * be the same. It's a little tricky, however, because the hash table
281 * needs to interact gracefully with the garbage collector. Much
282 * memory is to be saved by this, however! perhaps the improvement
283 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
284 * computes the hash value anyway.
288 // Note that *NAME_RESULT will only be set if the class is registered
289 // with the class loader. This is how the caller can know whether
290 // unregistration is require.
291 void
292 _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length,
293 java::security::ProtectionDomain *pd,
294 _Jv_Utf8Const **name_result)
296 _Jv_ClassReader reader (klass, data, offset, length, pd, name_result);
297 reader.parse();
299 /* that's it! */
303 /** This section defines the parsing/scanning of the class data */
305 void
306 _Jv_ClassReader::parse ()
308 int magic = read4 ();
310 /* FIXME: Decide which range of version numbers to allow */
312 /* int minor_version = */ read2u ();
313 /* int major_verson = */ read2u ();
315 if (magic != (int) 0xCAFEBABE)
316 throw_class_format_error ("bad magic number");
318 pool_count = read2u ();
320 read_constpool ();
322 int access_flags = read2u ();
323 int this_class = read2u ();
324 int super_class = read2u ();
326 check_tag (this_class, JV_CONSTANT_Class);
327 if (super_class != 0)
328 check_tag (super_class, JV_CONSTANT_Class);
330 handleClassBegin (access_flags, this_class, super_class);
332 // Allocate our aux_info here, after the name is set, to fulfill our
333 // contract with the collector interface.
334 def->aux_info = (void *) _Jv_AllocBytes (sizeof (_Jv_InterpClass));
335 def_interp = (_Jv_InterpClass *) def->aux_info;
337 int interfaces_count = read2u ();
339 handleInterfacesBegin (interfaces_count);
341 for (int i = 0; i < interfaces_count; i++)
343 int iface = read2u ();
344 check_tag (iface, JV_CONSTANT_Class);
345 handleInterface (i, iface);
348 read_fields ();
349 read_methods ();
351 int attributes_count = read2u ();
353 for (int i = 0; i < attributes_count; i++)
355 read_one_class_attribute ();
358 if (pos != len)
359 throw_class_format_error ("unused data before end of file");
361 // Tell everyone we're done.
362 def->state = JV_STATE_READ;
363 if (gcj::verbose_class_flag)
364 _Jv_Linker::print_class_loaded (def);
365 def->notifyAll ();
368 void _Jv_ClassReader::read_constpool ()
370 tags = (unsigned char*) _Jv_AllocBytes (pool_count);
371 offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int)
372 * pool_count) ;
374 /** first, we scan the constant pool, collecting tags and offsets */
375 tags[0] = JV_CONSTANT_Undefined;
376 offsets[0] = pos;
377 for (int c = 1; c < pool_count; c++)
379 tags[c] = read1u ();
380 offsets[c] = pos;
382 switch (tags[c])
384 case JV_CONSTANT_String:
385 case JV_CONSTANT_Class:
386 skip (2);
387 break;
389 case JV_CONSTANT_Fieldref:
390 case JV_CONSTANT_Methodref:
391 case JV_CONSTANT_InterfaceMethodref:
392 case JV_CONSTANT_NameAndType:
393 case JV_CONSTANT_Integer:
394 case JV_CONSTANT_Float:
395 skip (4);
396 break;
398 case JV_CONSTANT_Double:
399 case JV_CONSTANT_Long:
400 skip (8);
401 tags[++c] = JV_CONSTANT_Undefined;
402 break;
404 case JV_CONSTANT_Utf8:
406 int len = read2u ();
407 skip (len);
409 break;
411 case JV_CONSTANT_Unicode:
412 throw_class_format_error ("unicode not supported");
413 break;
415 default:
416 throw_class_format_error ("erroneous constant pool tag");
420 handleConstantPool ();
424 void _Jv_ClassReader::read_fields ()
426 int fields_count = read2u ();
427 handleFieldsBegin (fields_count);
429 for (int i = 0; i < fields_count; i++)
431 int access_flags = read2u ();
432 int name_index = read2u ();
433 int descriptor_index = read2u ();
434 int attributes_count = read2u ();
436 check_tag (name_index, JV_CONSTANT_Utf8);
437 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
439 check_tag (descriptor_index, JV_CONSTANT_Utf8);
440 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
442 handleField (i, access_flags, name_index, descriptor_index);
444 for (int j = 0; j < attributes_count; j++)
446 read_one_field_attribute (i);
450 handleFieldsEnd ();
453 bool
454 _Jv_ClassReader::is_attribute_name (int index, char *name)
456 check_tag (index, JV_CONSTANT_Utf8);
457 int len = get2u (bytes+offsets[index]);
458 if (len != (int) strlen (name))
459 return false;
460 else
461 return !memcmp (bytes+offsets[index]+2, name, len);
464 void _Jv_ClassReader::read_one_field_attribute (int field_index)
466 int name = read2u ();
467 int length = read4 ();
469 if (is_attribute_name (name, "ConstantValue"))
471 int cv = read2u ();
473 if (cv < pool_count
474 && cv > 0
475 && (tags[cv] == JV_CONSTANT_Integer
476 || tags[cv] == JV_CONSTANT_Float
477 || tags[cv] == JV_CONSTANT_Long
478 || tags[cv] == JV_CONSTANT_Double
479 || tags[cv] == JV_CONSTANT_String))
481 handleConstantValueAttribute (field_index, cv);
483 else
485 throw_class_format_error ("erroneous ConstantValue attribute");
488 if (length != 2)
489 throw_class_format_error ("erroneous ConstantValue attribute");
492 else
494 skip (length);
498 void _Jv_ClassReader::read_methods ()
500 int methods_count = read2u ();
502 handleMethodsBegin (methods_count);
504 for (int i = 0; i < methods_count; i++)
506 int access_flags = read2u ();
507 int name_index = read2u ();
508 int descriptor_index = read2u ();
509 int attributes_count = read2u ();
511 check_tag (name_index, JV_CONSTANT_Utf8);
512 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
514 check_tag (descriptor_index, JV_CONSTANT_Utf8);
515 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
517 handleMethod (i, access_flags, name_index,
518 descriptor_index);
520 for (int j = 0; j < attributes_count; j++)
522 read_one_method_attribute (i);
526 handleMethodsEnd ();
529 void _Jv_ClassReader::read_one_method_attribute (int method_index)
531 int name = read2u ();
532 int length = read4 ();
534 if (is_attribute_name (name, "Exceptions"))
536 _Jv_Method *method = reinterpret_cast<_Jv_Method *>
537 (&def->methods[method_index]);
538 if (method->throws != NULL)
539 throw_class_format_error ("only one Exceptions attribute allowed per method");
541 int num_exceptions = read2u ();
542 _Jv_Utf8Const **exceptions =
543 (_Jv_Utf8Const **) _Jv_AllocBytes ((num_exceptions + 1)
544 * sizeof (_Jv_Utf8Const *));
546 int out = 0;
547 _Jv_word *pool_data = def->constants.data;
548 for (int i = 0; i < num_exceptions; ++i)
550 int ndx = read2u ();
551 // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
552 if (ndx != 0)
554 check_tag (ndx, JV_CONSTANT_Class);
555 exceptions[out++] = pool_data[ndx].utf8;
558 exceptions[out] = NULL;
559 method->throws = exceptions;
562 else if (is_attribute_name (name, "Code"))
564 int start_off = pos;
565 int max_stack = read2u ();
566 int max_locals = read2u ();
567 int code_length = read4 ();
569 int code_start = pos;
570 skip (code_length);
571 int exception_table_length = read2u ();
573 handleCodeAttribute (method_index,
574 max_stack, max_locals,
575 code_start, code_length,
576 exception_table_length);
579 for (int i = 0; i < exception_table_length; i++)
581 int start_pc = read2u ();
582 int end_pc = read2u ();
583 int handler_pc = read2u ();
584 int catch_type = read2u ();
586 if (start_pc > end_pc
587 || start_pc < 0
588 // END_PC can be equal to CODE_LENGTH.
589 // See JVM Spec 4.7.4.
590 || end_pc > code_length
591 || handler_pc >= code_length)
592 throw_class_format_error ("erroneous exception handler info");
594 if (! (tags[catch_type] == JV_CONSTANT_Class
595 || tags[catch_type] == 0))
597 throw_class_format_error ("erroneous exception handler info");
600 handleExceptionTableEntry (method_index,
602 start_pc,
603 end_pc,
604 handler_pc,
605 catch_type);
609 int attributes_count = read2u ();
611 for (int i = 0; i < attributes_count; i++)
613 read_one_code_attribute (method_index);
616 if ((pos - start_off) != length)
617 throw_class_format_error ("code attribute too short");
620 else
622 /* ignore unknown attributes */
623 skip (length);
627 void _Jv_ClassReader::read_one_code_attribute (int method_index)
629 int name = read2u ();
630 int length = read4 ();
631 if (is_attribute_name (name, "LineNumberTable"))
633 _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
634 (def_interp->interpreted_methods[method_index]);
635 if (method->line_table != NULL)
636 throw_class_format_error ("Method already has LineNumberTable");
638 int table_len = read2u ();
639 _Jv_LineTableEntry* table
640 = (_Jv_LineTableEntry *) JvAllocBytes (table_len
641 * sizeof (_Jv_LineTableEntry));
642 for (int i = 0; i < table_len; i++)
644 table[i].bytecode_pc = read2u ();
645 table[i].line = read2u ();
647 method->line_table_len = table_len;
648 method->line_table = table;
650 else
652 /* ignore unknown code attributes */
653 skip (length);
657 void _Jv_ClassReader::read_one_class_attribute ()
659 int name = read2u ();
660 int length = read4 ();
661 if (is_attribute_name (name, "SourceFile"))
663 int source_index = read2u ();
664 check_tag (source_index, JV_CONSTANT_Utf8);
665 prepare_pool_entry (source_index, JV_CONSTANT_Utf8);
666 def_interp->source_file_name = _Jv_NewStringUtf8Const
667 (def->constants.data[source_index].utf8);
669 else
671 /* Currently, we ignore most class attributes.
672 FIXME: Add inner-classes attributes support. */
673 skip (length);
680 /* this section defines the semantic actions of the parser */
682 void _Jv_ClassReader::handleConstantPool ()
684 /** now, we actually define the class' constant pool */
686 // the pool is scanned explicitly by the collector
687 jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
688 _Jv_word *pool_data
689 = (_Jv_word*) _Jv_AllocBytes (pool_count * sizeof (_Jv_word));
691 def->constants.tags = pool_tags;
692 def->constants.data = pool_data;
693 def->constants.size = pool_count;
695 // Here we make a pass to collect the strings! We do this, because
696 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
697 // Therefore, we first collect the strings, and then translate the rest
698 // of the utf8-entries (thus not representing strings) from /-notation
699 // to .-notation.
700 for (int i = 1; i < pool_count; i++)
702 if (tags[i] == JV_CONSTANT_String)
704 unsigned char* str_data = bytes + offsets [i];
705 int utf_index = get2u (str_data);
706 check_tag (utf_index, JV_CONSTANT_Utf8);
707 unsigned char *utf_data = bytes + offsets[utf_index];
708 int len = get2u (utf_data);
709 pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
710 pool_tags[i] = JV_CONSTANT_String;
712 else
714 pool_tags[i] = JV_CONSTANT_Undefined;
718 // and now, we scan everything else but strings & utf8-entries. This
719 // leaves out those utf8-entries which are not used; which will be left
720 // with a tag of JV_CONSTANT_Undefined in the class definition.
721 for (int index = 1; index < pool_count; index++)
723 switch (tags[index])
725 case JV_CONSTANT_Undefined:
726 case JV_CONSTANT_String:
727 case JV_CONSTANT_Utf8:
728 continue;
730 default:
731 prepare_pool_entry (index, tags[index]);
737 /* this is a recursive procedure, which will prepare pool entries as needed.
738 Which is how we avoid initializing those entries which go unused. */
739 void
740 _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
742 /* these two, pool_data and pool_tags, point into the class
743 structure we are currently defining */
745 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
746 _Jv_word *pool_data = def->constants.data;
748 /* this entry was already prepared */
749 if (pool_tags[index] == this_tag)
750 return;
752 /* this_data points to the constant-pool information for the current
753 constant-pool entry */
755 unsigned char *this_data = bytes + offsets[index];
757 switch (this_tag)
759 case JV_CONSTANT_Utf8:
761 // If we came here, it is because some other tag needs this
762 // utf8-entry for type information! Thus, we translate /'s to .'s in
763 // order to accomondate gcj's internal representation.
765 int len = get2u (this_data);
766 char *buffer = (char*) __builtin_alloca (len);
767 char *s = ((char*) this_data)+2;
769 /* FIXME: avoid using a buffer here */
770 for (int i = 0; i < len; i++)
772 if (s[i] == '/')
773 buffer[i] = '.';
774 else
775 buffer[i] = (char) s[i];
778 pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
779 pool_tags[index] = JV_CONSTANT_Utf8;
781 break;
783 case JV_CONSTANT_Class:
785 int utf_index = get2u (this_data);
786 check_tag (utf_index, JV_CONSTANT_Utf8);
787 prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
789 if (verify)
790 verify_classname (pool_data[utf_index].utf8);
792 pool_data[index].utf8 = pool_data[utf_index].utf8;
793 pool_tags[index] = JV_CONSTANT_Class;
795 break;
797 case JV_CONSTANT_String:
798 // already handled before...
799 break;
801 case JV_CONSTANT_Fieldref:
802 case JV_CONSTANT_Methodref:
803 case JV_CONSTANT_InterfaceMethodref:
805 int class_index = get2u (this_data);
806 int nat_index = get2u (this_data+2);
808 check_tag (class_index, JV_CONSTANT_Class);
809 prepare_pool_entry (class_index, JV_CONSTANT_Class);
811 check_tag (nat_index, JV_CONSTANT_NameAndType);
812 prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
814 // here, verify the signature and identifier name
815 if (verify)
817 _Jv_ushort name_index, type_index;
818 _Jv_loadIndexes (&pool_data[nat_index],
819 name_index, type_index);
821 if (this_tag == JV_CONSTANT_Fieldref)
822 verify_field_signature (pool_data[type_index].utf8);
823 else
824 verify_method_signature (pool_data[type_index].utf8);
826 _Jv_Utf8Const* name = pool_data[name_index].utf8;
828 if (this_tag != JV_CONSTANT_Fieldref
829 && ( _Jv_equalUtf8Consts (name, clinit_name)
830 || _Jv_equalUtf8Consts (name, init_name)))
831 /* ignore */;
832 else
833 verify_identifier (pool_data[name_index].utf8);
836 _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
837 pool_tags[index] = this_tag;
839 break;
841 case JV_CONSTANT_NameAndType:
843 _Jv_ushort name_index = get2u (this_data);
844 _Jv_ushort type_index = get2u (this_data+2);
846 check_tag (name_index, JV_CONSTANT_Utf8);
847 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
849 check_tag (type_index, JV_CONSTANT_Utf8);
850 prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
852 _Jv_storeIndexes (&pool_data[index], name_index, type_index);
853 pool_tags[index] = JV_CONSTANT_NameAndType;
855 break;
857 case JV_CONSTANT_Float:
859 jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
860 _Jv_storeFloat (&pool_data[index], f);
861 pool_tags[index] = JV_CONSTANT_Float;
863 break;
865 case JV_CONSTANT_Integer:
867 int i = get4 (this_data);
868 _Jv_storeInt (&pool_data[index], i);
869 pool_tags[index] = JV_CONSTANT_Integer;
871 break;
873 case JV_CONSTANT_Double:
875 jdouble d
876 = java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
877 _Jv_storeDouble (&pool_data[index], d);
878 pool_tags[index] = JV_CONSTANT_Double;
880 break;
882 case JV_CONSTANT_Long:
884 jlong i = get8 (this_data);
885 _Jv_storeLong (&pool_data[index], i);
886 pool_tags[index] = JV_CONSTANT_Long;
888 break;
890 default:
891 throw_class_format_error ("erroneous constant pool tag");
896 void
897 _Jv_ClassReader::handleClassBegin (int access_flags, int this_class, int super_class)
899 using namespace java::lang::reflect;
901 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
902 _Jv_word *pool_data = def->constants.data;
904 check_tag (this_class, JV_CONSTANT_Class);
905 _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
907 // was ClassLoader.defineClass called with an expected class name?
908 if (def->name == 0)
910 jclass orig = def->loader->findLoadedClass(loadedName->toString());
912 if (orig == 0)
914 def->name = loadedName;
916 else
918 jstring msg = JvNewStringUTF ("anonymous "
919 "class data denotes "
920 "existing class ");
921 msg = msg->concat (orig->getName ());
923 throw_no_class_def_found_error (msg);
927 // assert that the loaded class has the expected name, 5.3.5
928 else if (! _Jv_equalUtf8Consts (loadedName, def->name))
930 jstring msg = JvNewStringUTF ("loaded class ");
931 msg = msg->concat (def->getName ());
932 msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
933 jstring klass_name = loadedName->toString();
934 msg = msg->concat (klass_name);
936 throw_no_class_def_found_error (msg);
939 def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
940 pool_data[this_class].clazz = def;
941 pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
943 if (super_class == 0)
945 // Note that this is ok if we are defining java.lang.Object.
946 // But there is no way to have this class be interpreted.
947 throw_class_format_error ("no superclass reference");
950 def->state = JV_STATE_PRELOADING;
952 // Register this class with its defining loader as well (despite the
953 // name of the function we're calling), so that super class lookups
954 // work properly. If there is an error, our caller will unregister
955 // this class from the class loader. Also, we don't need to hold a
956 // lock here, as our caller has acquired it.
957 _Jv_RegisterInitiatingLoader (def, def->loader);
959 // Note that we found a name so that unregistration can happen if
960 // needed.
961 *found_name = def->name;
963 if (super_class != 0)
965 // Load the superclass.
966 check_tag (super_class, JV_CONSTANT_Class);
967 _Jv_Utf8Const* super_name = pool_data[super_class].utf8;
969 // Load the superclass using our defining loader.
970 jclass the_super = _Jv_FindClass (super_name, def->loader);
972 // This will establish that we are allowed to be a subclass,
973 // and check for class circularity error.
974 checkExtends (def, the_super);
976 // Note: for an interface we will find Object as the
977 // superclass. We still check it above to ensure class file
978 // validity, but we simply assign `null' to the actual field in
979 // this case.
980 def->superclass = (((access_flags & Modifier::INTERFACE))
981 ? NULL : the_super);
982 pool_data[super_class].clazz = the_super;
983 pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
986 // Now we've come past the circularity problem, we can
987 // now say that we're loading.
989 def->state = JV_STATE_LOADING;
990 def->notifyAll ();
993 ///// Implements the checks described in sect. 5.3.5.3
994 void
995 _Jv_ClassReader::checkExtends (jclass sub, jclass super)
997 using namespace java::lang::reflect;
999 _Jv_Linker::wait_for_state (super, JV_STATE_LOADING);
1001 // Having an interface or a final class as a superclass is no good.
1002 if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
1004 throw_incompatible_class_change_error (sub->getName ());
1007 // If the super class is not public, we need to check some more.
1008 if ((super->accflags & Modifier::PUBLIC) == 0)
1010 // With package scope, the classes must have the same class
1011 // loader.
1012 if ( sub->loader != super->loader
1013 || !_Jv_ClassNameSamePackage (sub->name, super->name))
1015 throw_incompatible_class_change_error (sub->getName ());
1019 for (; super != 0; super = super->getSuperclass ())
1021 if (super == sub)
1022 throw_class_circularity_error (sub->getName ());
1028 void _Jv_ClassReader::handleInterfacesBegin (int count)
1030 def->interfaces = (jclass*) _Jv_AllocBytes (count*sizeof (jclass));
1031 def->interface_count = count;
1034 void _Jv_ClassReader::handleInterface (int if_number, int offset)
1036 _Jv_word * pool_data = def->constants.data;
1037 unsigned char * pool_tags = (unsigned char*) def->constants.tags;
1039 jclass the_interface;
1041 if (pool_tags[offset] == JV_CONSTANT_Class)
1043 _Jv_Utf8Const* name = pool_data[offset].utf8;
1044 the_interface = _Jv_FindClass (name, def->loader);
1046 else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
1048 the_interface = pool_data[offset].clazz;
1050 else
1052 throw_no_class_def_found_error ("erroneous constant pool tag");
1055 // checks the validity of the_interface, and that we are in fact
1056 // allowed to implement that interface.
1057 checkImplements (def, the_interface);
1059 pool_data[offset].clazz = the_interface;
1060 pool_tags[offset] = JV_CONSTANT_ResolvedClass;
1062 def->interfaces[if_number] = the_interface;
1065 void
1066 _Jv_ClassReader::checkImplements (jclass sub, jclass super)
1068 using namespace java::lang::reflect;
1070 // well, it *must* be an interface
1071 if ((super->accflags & Modifier::INTERFACE) == 0)
1073 throw_incompatible_class_change_error (sub->getName ());
1076 // if it has package scope, it must also be defined by the
1077 // same loader.
1078 if ((super->accflags & Modifier::PUBLIC) == 0)
1080 if ( sub->loader != super->loader
1081 || !_Jv_ClassNameSamePackage (sub->name, super->name))
1083 throw_incompatible_class_change_error (sub->getName ());
1087 // FIXME: add interface circularity check here
1088 if (sub == super)
1090 throw_class_circularity_error (sub->getName ());
1094 void _Jv_ClassReader::handleFieldsBegin (int count)
1096 def->fields = (_Jv_Field*)
1097 _Jv_AllocBytes (count * sizeof (_Jv_Field));
1098 def->field_count = count;
1099 def_interp->field_initializers = (_Jv_ushort*)
1100 _Jv_AllocBytes (count * sizeof (_Jv_ushort));
1101 for (int i = 0; i < count; i++)
1102 def_interp->field_initializers[i] = (_Jv_ushort) 0;
1105 void _Jv_ClassReader::handleField (int field_no,
1106 int flags,
1107 int name,
1108 int desc)
1110 using namespace java::lang::reflect;
1112 _Jv_word *pool_data = def->constants.data;
1114 _Jv_Field *field = &def->fields[field_no];
1115 _Jv_Utf8Const *field_name = pool_data[name].utf8;
1117 field->name = field_name;
1119 // Ignore flags we don't know about.
1120 field->flags = flags & Modifier::ALL_FLAGS;
1122 _Jv_Utf8Const* sig = pool_data[desc].utf8;
1124 if (verify)
1126 verify_identifier (field_name);
1128 for (int i = 0; i < field_no; ++i)
1130 if (_Jv_equalUtf8Consts (field_name, def->fields[i].name)
1131 && _Jv_equalUtf8Consts (sig,
1132 // We know the other fields are
1133 // unresolved.
1134 (_Jv_Utf8Const *) def->fields[i].type))
1135 throw_class_format_error ("duplicate field name");
1138 // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1139 if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
1140 +((field->flags & Modifier::PRIVATE) ? 1 : 0)
1141 +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
1142 throw_class_format_error ("erroneous field access flags");
1144 // FIXME: JVM spec S4.5: Verify ACC_FINAL and ACC_VOLATILE are not
1145 // both set. Verify modifiers for interface fields.
1149 if (verify)
1150 verify_field_signature (sig);
1152 // field->type is really a jclass, but while it is still
1153 // unresolved we keep an _Jv_Utf8Const* instead.
1154 field->type = (jclass) sig;
1155 field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
1156 field->u.boffset = 0;
1160 void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
1161 int value)
1163 using namespace java::lang::reflect;
1165 _Jv_Field *field = &def->fields[field_index];
1167 if ((field->flags & (Modifier::STATIC
1168 | Modifier::FINAL
1169 | Modifier::PRIVATE)) == 0)
1171 // Ignore, as per vmspec #4.7.2
1172 return;
1175 // do not allow multiple constant fields!
1176 if (field->flags & _Jv_FIELD_CONSTANT_VALUE)
1177 throw_class_format_error ("field has multiple ConstantValue attributes");
1179 field->flags |= _Jv_FIELD_CONSTANT_VALUE;
1180 def_interp->field_initializers[field_index] = value;
1182 /* type check the initializer */
1184 if (value <= 0 || value >= pool_count)
1185 throw_class_format_error ("erroneous ConstantValue attribute");
1187 /* FIXME: do the rest */
1190 void _Jv_ClassReader::handleFieldsEnd ()
1192 using namespace java::lang::reflect;
1194 // We need to reorganize the fields so that the static ones are first,
1195 // to conform to GCJ class layout.
1197 int low = 0;
1198 int high = def->field_count-1;
1199 _Jv_Field *fields = def->fields;
1200 _Jv_ushort *inits = def_interp->field_initializers;
1202 // this is kind of a raw version of quicksort.
1203 while (low < high)
1205 // go forward on low, while it's a static
1206 while (low < high && (fields[low].flags & Modifier::STATIC) != 0)
1207 low++;
1209 // go backwards on high, while it's a non-static
1210 while (low < high && (fields[high].flags & Modifier::STATIC) == 0)
1211 high--;
1213 if (low==high)
1214 break;
1216 _Jv_Field tmp = fields[low];
1217 _Jv_ushort itmp = inits[low];
1219 fields[low] = fields[high];
1220 inits[low] = inits[high];
1222 fields[high] = tmp;
1223 inits[high] = itmp;
1225 high -= 1;
1226 low += 1;
1229 if ((fields[low].flags & Modifier::STATIC) != 0)
1230 low += 1;
1232 def->static_field_count = low;
1237 void
1238 _Jv_ClassReader::handleMethodsBegin (int count)
1240 def->methods = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method) * count);
1242 def_interp->interpreted_methods
1243 = (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *)
1244 * count);
1246 for (int i = 0; i < count; i++)
1248 def_interp->interpreted_methods[i] = 0;
1249 def->methods[i].index = (_Jv_ushort) -1;
1252 def->method_count = count;
1256 void _Jv_ClassReader::handleMethod
1257 (int mth_index, int accflags, int name, int desc)
1259 using namespace java::lang::reflect;
1261 _Jv_word *pool_data = def->constants.data;
1262 _Jv_Method *method = &def->methods[mth_index];
1264 check_tag (name, JV_CONSTANT_Utf8);
1265 prepare_pool_entry (name, JV_CONSTANT_Utf8);
1266 method->name = pool_data[name].utf8;
1268 check_tag (desc, JV_CONSTANT_Utf8);
1269 prepare_pool_entry (desc, JV_CONSTANT_Utf8);
1270 method->signature = pool_data[desc].utf8;
1272 // ignore unknown flags
1273 method->accflags = accflags & Modifier::ALL_FLAGS;
1275 // Initialize...
1276 method->ncode = 0;
1277 method->throws = NULL;
1279 if (verify)
1281 if (_Jv_equalUtf8Consts (method->name, clinit_name)
1282 || _Jv_equalUtf8Consts (method->name, init_name))
1283 /* ignore */;
1284 else
1285 verify_identifier (method->name);
1287 verify_method_signature (method->signature);
1289 for (int i = 0; i < mth_index; ++i)
1291 if (_Jv_equalUtf8Consts (method->name, def->methods[i].name)
1292 && _Jv_equalUtf8Consts (method->signature,
1293 def->methods[i].signature))
1294 throw_class_format_error ("duplicate method");
1297 // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1298 if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
1299 +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
1300 +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
1301 throw_class_format_error ("erroneous method access flags");
1303 // FIXME: JVM spec S4.6: if ABSTRACT modifier is set, verify other
1304 // flags are not set. Verify flags for interface methods. Verify
1305 // modifiers for initializers.
1309 void _Jv_ClassReader::handleCodeAttribute
1310 (int method_index, int max_stack, int max_locals,
1311 int code_start, int code_length, int exc_table_length)
1313 int size = _Jv_InterpMethod::size (exc_table_length, code_length);
1314 _Jv_InterpMethod *method =
1315 (_Jv_InterpMethod*) (_Jv_AllocBytes (size));
1317 method->max_stack = max_stack;
1318 method->max_locals = max_locals;
1319 method->code_length = code_length;
1320 method->exc_count = exc_table_length;
1321 method->defining_class = def;
1322 method->self = &def->methods[method_index];
1323 method->prepared = NULL;
1324 method->line_table_len = 0;
1325 method->line_table = NULL;
1328 // grab the byte code!
1329 memcpy ((void*) method->bytecode (),
1330 (void*) (bytes+code_start),
1331 code_length);
1333 def_interp->interpreted_methods[method_index] = method;
1335 if ((method->self->accflags & java::lang::reflect::Modifier::STATIC))
1337 // Precompute the ncode field for a static method. This lets us
1338 // call a static method of an interpreted class from precompiled
1339 // code without first resolving the class (that will happen
1340 // during class initialization instead).
1341 method->self->ncode = method->ncode ();
1345 void _Jv_ClassReader::handleExceptionTableEntry
1346 (int method_index, int exc_index,
1347 int start_pc, int end_pc, int handler_pc, int catch_type)
1349 _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
1350 (def_interp->interpreted_methods[method_index]);
1351 _Jv_InterpException *exc = method->exceptions ();
1353 exc[exc_index].start_pc.i = start_pc;
1354 exc[exc_index].end_pc.i = end_pc;
1355 exc[exc_index].handler_pc.i = handler_pc;
1356 exc[exc_index].handler_type.i = catch_type;
1359 void _Jv_ClassReader::handleMethodsEnd ()
1361 using namespace java::lang::reflect;
1363 for (int i = 0; i < def->method_count; i++)
1365 _Jv_Method *method = &def->methods[i];
1366 if ((method->accflags & Modifier::NATIVE) != 0)
1368 if (def_interp->interpreted_methods[i] != 0)
1369 throw_class_format_error ("code provided for native method");
1370 else
1372 _Jv_JNIMethod *m = (_Jv_JNIMethod *)
1373 _Jv_AllocBytes (sizeof (_Jv_JNIMethod));
1374 m->defining_class = def;
1375 m->self = method;
1376 m->function = NULL;
1377 def_interp->interpreted_methods[i] = m;
1379 if ((method->accflags & Modifier::STATIC))
1381 // Precompute the ncode field for a static method.
1382 // This lets us call a static method of an
1383 // interpreted class from precompiled code without
1384 // first resolving the class (that will happen
1385 // during class initialization instead).
1386 method->ncode = m->ncode ();
1390 else if ((method->accflags & Modifier::ABSTRACT) != 0)
1392 if (def_interp->interpreted_methods[i] != 0)
1393 throw_class_format_error ("code provided for abstract method");
1394 method->ncode = (void *) &_Jv_ThrowAbstractMethodError;
1396 else
1398 if (def_interp->interpreted_methods[i] == 0)
1399 throw_class_format_error ("method with no code");
1404 void _Jv_ClassReader::throw_class_format_error (char *msg)
1406 jstring str;
1407 if (def->name != NULL)
1409 jsize mlen = strlen (msg);
1410 unsigned char* data = (unsigned char*) def->name->chars();
1411 int ulen = def->name->len();
1412 unsigned char* limit = data + ulen;
1413 jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen);
1414 jsize len = nlen + mlen + 3;
1415 str = JvAllocString(len);
1416 jchar *chrs = JvGetStringChars(str);
1417 while (data < limit)
1418 *chrs++ = UTF8_GET(data, limit);
1419 *chrs++ = ' ';
1420 *chrs++ = '(';
1421 for (;;)
1423 char c = *msg++;
1424 if (c == 0)
1425 break;
1426 *chrs++ = c & 0xFFFF;
1428 *chrs++ = ')';
1430 else
1431 str = JvNewStringLatin1 (msg);
1432 ::throw_class_format_error (str);
1435 /** Here we define the exceptions that can be thrown */
1437 static void
1438 throw_no_class_def_found_error (jstring msg)
1440 throw (msg
1441 ? new java::lang::NoClassDefFoundError (msg)
1442 : new java::lang::NoClassDefFoundError);
1445 static void
1446 throw_no_class_def_found_error (char *msg)
1448 throw_no_class_def_found_error (JvNewStringLatin1 (msg));
1451 static void
1452 throw_class_format_error (jstring msg)
1454 throw (msg
1455 ? new java::lang::ClassFormatError (msg)
1456 : new java::lang::ClassFormatError);
1459 static void
1460 throw_internal_error (char *msg)
1462 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
1465 static void
1466 throw_incompatible_class_change_error (jstring msg)
1468 throw new java::lang::IncompatibleClassChangeError (msg);
1471 static void
1472 throw_class_circularity_error (jstring msg)
1474 throw new java::lang::ClassCircularityError (msg);
1477 #endif /* INTERPRETER */
1481 /** This section takes care of verifying integrity of identifiers,
1482 signatures, field ddescriptors, and class names */
1484 #define UTF8_PEEK(PTR, LIMIT) \
1485 ({ unsigned char* xxkeep = (PTR); \
1486 int xxch = UTF8_GET(PTR,LIMIT); \
1487 PTR = xxkeep; xxch; })
1489 /* Verify one element of a type descriptor or signature. */
1490 static unsigned char*
1491 _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
1493 if (ptr >= limit)
1494 return 0;
1496 int ch = UTF8_GET (ptr, limit);
1498 switch (ch)
1500 case 'V':
1501 if (! void_ok)
1502 return 0;
1504 case 'S': case 'B': case 'I': case 'J':
1505 case 'Z': case 'C': case 'F': case 'D':
1506 break;
1508 case 'L':
1510 unsigned char *start = ptr, *end;
1513 if (ptr > limit)
1514 return 0;
1516 end = ptr;
1518 if ((ch = UTF8_GET (ptr, limit)) == -1)
1519 return 0;
1522 while (ch != ';');
1523 if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
1524 return 0;
1526 break;
1528 case '[':
1529 return _Jv_VerifyOne (ptr, limit, false);
1530 break;
1532 default:
1533 return 0;
1536 return ptr;
1539 /* Verification and loading procedures. */
1540 bool
1541 _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
1543 unsigned char* ptr = (unsigned char*) sig->chars();
1544 unsigned char* limit = ptr + sig->len();
1546 ptr = _Jv_VerifyOne (ptr, limit, false);
1548 return ptr == limit;
1551 bool
1552 _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
1554 unsigned char* ptr = (unsigned char*) sig->chars();
1555 unsigned char* limit = ptr + sig->len();
1557 if (ptr == limit || UTF8_GET(ptr,limit) != '(')
1558 return false;
1560 while (ptr && UTF8_PEEK (ptr, limit) != ')')
1561 ptr = _Jv_VerifyOne (ptr, limit, false);
1563 if (! ptr || UTF8_GET (ptr, limit) != ')')
1564 return false;
1566 // get the return type
1567 ptr = _Jv_VerifyOne (ptr, limit, true);
1569 return ptr == limit;
1572 /* We try to avoid calling the Character methods all the time, in
1573 fact, they will only be called for non-standard things. */
1574 static __inline__ int
1575 is_identifier_start (int c)
1577 unsigned int ch = (unsigned)c;
1579 if ((ch - 0x41U) < 29U) /* A ... Z */
1580 return 1;
1581 if ((ch - 0x61U) < 29U) /* a ... z */
1582 return 1;
1583 if (ch == 0x5FU) /* _ */
1584 return 1;
1586 return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
1589 static __inline__ int
1590 is_identifier_part (int c)
1592 unsigned int ch = (unsigned)c;
1594 if ((ch - 0x41U) < 29U) /* A ... Z */
1595 return 1;
1596 if ((ch - 0x61U) < 29U) /* a ... z */
1597 return 1;
1598 if ((ch - 0x30) < 10U) /* 0 .. 9 */
1599 return 1;
1600 if (ch == 0x5FU || ch == 0x24U) /* _ $ */
1601 return 1;
1603 return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
1606 bool
1607 _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
1609 unsigned char *ptr = (unsigned char*) name->chars();
1610 unsigned char *limit = (unsigned char*) name->limit();
1611 int ch;
1613 if ((ch = UTF8_GET (ptr, limit))==-1
1614 || ! is_identifier_start (ch))
1615 return false;
1617 while (ptr != limit)
1619 if ((ch = UTF8_GET (ptr, limit))==-1
1620 || ! is_identifier_part (ch))
1621 return false;
1623 return true;
1626 bool
1627 _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
1629 unsigned char *limit = ptr+length;
1630 int ch;
1632 if ('[' == UTF8_PEEK (ptr, limit))
1634 unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
1635 // _Jv_VerifyOne must leave us looking at the terminating nul
1636 // byte.
1637 if (! end || *end)
1638 return false;
1639 else
1640 return true;
1643 next_level:
1644 for (;;) {
1645 if ((ch = UTF8_GET (ptr, limit))==-1)
1646 return false;
1647 if (! is_identifier_start (ch))
1648 return false;
1649 for (;;) {
1650 if (ptr == limit)
1651 return true;
1652 else if ((ch = UTF8_GET (ptr, limit))==-1)
1653 return false;
1654 else if (ch == '.')
1655 goto next_level;
1656 else if (! is_identifier_part (ch))
1657 return false;
1662 bool
1663 _Jv_VerifyClassName (_Jv_Utf8Const *name)
1665 return _Jv_VerifyClassName ((unsigned char*)name->chars(), name->len());
1668 /* Returns true, if NAME1 and NAME2 represent classes in the same
1669 package. Neither NAME2 nor NAME2 may name an array type. */
1670 bool
1671 _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
1673 unsigned char* ptr1 = (unsigned char*) name1->chars();
1674 unsigned char* limit1 = (unsigned char*) name1->limit();
1676 unsigned char* last1 = ptr1;
1678 // scan name1, and find the last occurrence of '.'
1679 while (ptr1 < limit1) {
1680 int ch1 = UTF8_GET (ptr1, limit1);
1682 if (ch1 == '.')
1683 last1 = ptr1;
1685 else if (ch1 == -1)
1686 return false;
1689 // Now the length of NAME1's package name is LEN.
1690 int len = last1 - (unsigned char*) name1->chars();
1692 // If this is longer than NAME2, then we're off.
1693 if (len > name2->len())
1694 return false;
1696 // Then compare the first len bytes for equality.
1697 if (memcmp ((void*) name1->chars(), (void*) name2->chars(), len) == 0)
1699 // Check that there are no .'s after position LEN in NAME2.
1701 unsigned char* ptr2 = (unsigned char*) name2->chars() + len;
1702 unsigned char* limit2 = (unsigned char*) name2->limit();
1704 while (ptr2 < limit2)
1706 int ch2 = UTF8_GET (ptr2, limit2);
1707 if (ch2 == -1 || ch2 == '.')
1708 return false;
1710 return true;
1712 return false;