2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libjava / defineclass.cc
blob89e0636f4aa71d88607ff44e078b0723dce11e39
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 // True if this is a 1.5 class file.
112 bool is_15;
115 /* check that the given number of input bytes are available */
116 inline void check (int num)
118 if (pos + num > len)
119 throw_class_format_error ("Premature end of data");
122 /* skip a given number of bytes in input */
123 inline void skip (int num)
125 check (num);
126 pos += num;
129 /* read an unsignend 1-byte unit */
130 inline static jint get1u (unsigned char* bytes)
132 return bytes[0];
135 /* read an unsigned 1-byte unit */
136 inline jint read1u ()
138 skip (1);
139 return get1u (bytes+pos-1);
142 /* read an unsigned 2-byte unit */
143 inline static jint get2u (unsigned char *bytes)
145 return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
148 /* read an unsigned 2-byte unit */
149 inline jint read2u ()
151 skip (2);
152 return get2u (bytes+pos-2);
155 /* read a 4-byte unit */
156 static jint get4 (unsigned char *bytes)
158 return (((jint)bytes[0]) << 24)
159 | (((jint)bytes[1]) << 16)
160 | (((jint)bytes[2]) << 8)
161 | (((jint)bytes[3]) << 0);
164 /* read a 4-byte unit, (we don't do that quite so often) */
165 inline jint read4 ()
167 skip (4);
168 return get4 (bytes+pos-4);
171 /* read a 8-byte unit */
172 static jlong get8 (unsigned char* bytes)
174 return (((jlong)bytes[0]) << 56)
175 | (((jlong)bytes[1]) << 48)
176 | (((jlong)bytes[2]) << 40)
177 | (((jlong)bytes[3]) << 32)
178 | (((jlong)bytes[4]) << 24)
179 | (((jlong)bytes[5]) << 16)
180 | (((jlong)bytes[6]) << 8)
181 | (((jlong)bytes[7]) << 0);
184 /* read a 8-byte unit */
185 inline jlong read8 ()
187 skip (8);
188 return get8 (bytes+pos-8);
191 inline void check_tag (int index, char expected_tag)
193 if (index < 0
194 || index > pool_count
195 || tags[index] != expected_tag)
196 throw_class_format_error ("erroneous constant pool tag");
199 inline void verify_identifier (_Jv_Utf8Const* name)
201 if (! _Jv_VerifyIdentifier (name))
202 throw_class_format_error ("erroneous identifier");
205 inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
207 if (! _Jv_VerifyClassName (ptr, length))
208 throw_class_format_error ("erroneous class name");
211 inline void verify_classname (_Jv_Utf8Const *name)
213 if (! _Jv_VerifyClassName (name))
214 throw_class_format_error ("erroneous class name");
217 inline void verify_field_signature (_Jv_Utf8Const *sig)
219 if (! _Jv_VerifyFieldSignature (sig))
220 throw_class_format_error ("erroneous type descriptor");
223 inline void verify_method_signature (_Jv_Utf8Const *sig)
225 if (! _Jv_VerifyMethodSignature (sig))
226 throw_class_format_error ("erroneous type descriptor");
229 _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length,
230 java::security::ProtectionDomain *pd,
231 _Jv_Utf8Const **name_result)
233 if (klass == 0 || length < 0 || offset+length > data->length)
234 throw_internal_error ("arguments to _Jv_DefineClass");
236 verify = true;
237 bytes = (unsigned char*) (elements (data)+offset);
238 len = length;
239 pos = 0;
240 is_15 = false;
242 def = klass;
243 found_name = name_result;
245 def->size_in_bytes = -1;
246 def->vtable_method_count = -1;
247 def->engine = &_Jv_soleInterpreterEngine;
248 def->protectionDomain = pd;
251 /** and here goes the parser members defined out-of-line */
252 void parse ();
253 void read_constpool ();
254 void prepare_pool_entry (int index, unsigned char tag);
255 void read_fields ();
256 void read_methods ();
257 void read_one_class_attribute ();
258 void read_one_method_attribute (int method);
259 void read_one_code_attribute (int method);
260 void read_one_field_attribute (int field);
261 void throw_class_format_error (char *msg);
263 /** check an utf8 entry, without creating a Utf8Const object */
264 bool is_attribute_name (int index, char *name);
266 /** here goes the class-loader members defined out-of-line */
267 void handleConstantPool ();
268 void handleClassBegin (int, int, int);
269 void handleInterfacesBegin (int);
270 void handleInterface (int, int);
271 void handleFieldsBegin (int);
272 void handleField (int, int, int, int);
273 void handleFieldsEnd ();
274 void handleConstantValueAttribute (int,int);
275 void handleMethodsBegin (int);
276 void handleMethod (int, int, int, int);
277 void handleMethodsEnd ();
278 void handleCodeAttribute (int, int, int, int, int, int);
279 void handleExceptionTableEntry (int, int, int, int, int, int);
281 void checkExtends (jclass sub, jclass super);
282 void checkImplements (jclass sub, jclass super);
285 * FIXME: we should keep a hash table of utf8-strings, since many will
286 * be the same. It's a little tricky, however, because the hash table
287 * needs to interact gracefully with the garbage collector. Much
288 * memory is to be saved by this, however! perhaps the improvement
289 * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
290 * computes the hash value anyway.
294 // Note that *NAME_RESULT will only be set if the class is registered
295 // with the class loader. This is how the caller can know whether
296 // unregistration is require.
297 void
298 _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length,
299 java::security::ProtectionDomain *pd,
300 _Jv_Utf8Const **name_result)
302 _Jv_ClassReader reader (klass, data, offset, length, pd, name_result);
303 reader.parse();
305 /* that's it! */
309 /** This section defines the parsing/scanning of the class data */
311 // Major and minor version numbers for various releases.
312 #define MAJOR_1_1 45
313 #define MINOR_1_1 3
314 #define MAJOR_1_2 46
315 #define MINOR_1_2 0
316 #define MAJOR_1_3 47
317 #define MINOR_1_3 0
318 #define MAJOR_1_4 48
319 #define MINOR_1_4 0
320 #define MAJOR_1_5 49
321 #define MINOR_1_5 0
323 void
324 _Jv_ClassReader::parse ()
326 int magic = read4 ();
327 if (magic != (int) 0xCAFEBABE)
328 throw_class_format_error ("bad magic number");
330 int minor_version = read2u ();
331 int major_version = read2u ();
332 if (major_version < MAJOR_1_1 || major_version > MAJOR_1_5
333 || (major_version == MAJOR_1_5 && minor_version > MINOR_1_5))
334 throw_class_format_error ("unrecognized class file version");
335 is_15 = (major_version == MAJOR_1_5);
337 pool_count = read2u ();
339 read_constpool ();
341 int access_flags = read2u ();
342 int this_class = read2u ();
343 int super_class = read2u ();
345 check_tag (this_class, JV_CONSTANT_Class);
346 if (super_class != 0)
347 check_tag (super_class, JV_CONSTANT_Class);
349 handleClassBegin (access_flags, this_class, super_class);
351 // Allocate our aux_info here, after the name is set, to fulfill our
352 // contract with the collector interface.
353 def->aux_info = (void *) _Jv_AllocBytes (sizeof (_Jv_InterpClass));
354 def_interp = (_Jv_InterpClass *) def->aux_info;
356 int interfaces_count = read2u ();
358 handleInterfacesBegin (interfaces_count);
360 for (int i = 0; i < interfaces_count; i++)
362 int iface = read2u ();
363 check_tag (iface, JV_CONSTANT_Class);
364 handleInterface (i, iface);
367 read_fields ();
368 read_methods ();
370 int attributes_count = read2u ();
372 for (int i = 0; i < attributes_count; i++)
374 read_one_class_attribute ();
377 if (pos != len)
378 throw_class_format_error ("unused data before end of file");
380 // Tell everyone we're done.
381 def->state = JV_STATE_READ;
382 if (gcj::verbose_class_flag)
383 _Jv_Linker::print_class_loaded (def);
384 def->notifyAll ();
387 void _Jv_ClassReader::read_constpool ()
389 tags = (unsigned char*) _Jv_AllocBytes (pool_count);
390 offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int)
391 * pool_count) ;
393 /** first, we scan the constant pool, collecting tags and offsets */
394 tags[0] = JV_CONSTANT_Undefined;
395 offsets[0] = pos;
396 for (int c = 1; c < pool_count; c++)
398 tags[c] = read1u ();
399 offsets[c] = pos;
401 switch (tags[c])
403 case JV_CONSTANT_String:
404 case JV_CONSTANT_Class:
405 skip (2);
406 break;
408 case JV_CONSTANT_Fieldref:
409 case JV_CONSTANT_Methodref:
410 case JV_CONSTANT_InterfaceMethodref:
411 case JV_CONSTANT_NameAndType:
412 case JV_CONSTANT_Integer:
413 case JV_CONSTANT_Float:
414 skip (4);
415 break;
417 case JV_CONSTANT_Double:
418 case JV_CONSTANT_Long:
419 skip (8);
420 tags[++c] = JV_CONSTANT_Undefined;
421 break;
423 case JV_CONSTANT_Utf8:
425 int len = read2u ();
426 skip (len);
428 break;
430 case JV_CONSTANT_Unicode:
431 throw_class_format_error ("unicode not supported");
432 break;
434 default:
435 throw_class_format_error ("erroneous constant pool tag");
439 handleConstantPool ();
443 void _Jv_ClassReader::read_fields ()
445 int fields_count = read2u ();
446 handleFieldsBegin (fields_count);
448 for (int i = 0; i < fields_count; i++)
450 int access_flags = read2u ();
451 int name_index = read2u ();
452 int descriptor_index = read2u ();
453 int attributes_count = read2u ();
455 check_tag (name_index, JV_CONSTANT_Utf8);
456 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
458 check_tag (descriptor_index, JV_CONSTANT_Utf8);
459 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
461 handleField (i, access_flags, name_index, descriptor_index);
463 for (int j = 0; j < attributes_count; j++)
465 read_one_field_attribute (i);
469 handleFieldsEnd ();
472 bool
473 _Jv_ClassReader::is_attribute_name (int index, char *name)
475 check_tag (index, JV_CONSTANT_Utf8);
476 int len = get2u (bytes+offsets[index]);
477 if (len != (int) strlen (name))
478 return false;
479 else
480 return !memcmp (bytes+offsets[index]+2, name, len);
483 void _Jv_ClassReader::read_one_field_attribute (int field_index)
485 int name = read2u ();
486 int length = read4 ();
488 if (is_attribute_name (name, "ConstantValue"))
490 int cv = read2u ();
492 if (cv < pool_count
493 && cv > 0
494 && (tags[cv] == JV_CONSTANT_Integer
495 || tags[cv] == JV_CONSTANT_Float
496 || tags[cv] == JV_CONSTANT_Long
497 || tags[cv] == JV_CONSTANT_Double
498 || tags[cv] == JV_CONSTANT_String))
500 handleConstantValueAttribute (field_index, cv);
502 else
504 throw_class_format_error ("erroneous ConstantValue attribute");
507 if (length != 2)
508 throw_class_format_error ("erroneous ConstantValue attribute");
511 else
513 skip (length);
517 void _Jv_ClassReader::read_methods ()
519 int methods_count = read2u ();
521 handleMethodsBegin (methods_count);
523 for (int i = 0; i < methods_count; i++)
525 int access_flags = read2u ();
526 int name_index = read2u ();
527 int descriptor_index = read2u ();
528 int attributes_count = read2u ();
530 check_tag (name_index, JV_CONSTANT_Utf8);
531 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
533 check_tag (descriptor_index, JV_CONSTANT_Utf8);
534 prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
536 handleMethod (i, access_flags, name_index,
537 descriptor_index);
539 for (int j = 0; j < attributes_count; j++)
541 read_one_method_attribute (i);
545 handleMethodsEnd ();
548 void _Jv_ClassReader::read_one_method_attribute (int method_index)
550 int name = read2u ();
551 int length = read4 ();
553 if (is_attribute_name (name, "Exceptions"))
555 _Jv_Method *method = reinterpret_cast<_Jv_Method *>
556 (&def->methods[method_index]);
557 if (method->throws != NULL)
558 throw_class_format_error ("only one Exceptions attribute allowed per method");
560 int num_exceptions = read2u ();
561 _Jv_Utf8Const **exceptions =
562 (_Jv_Utf8Const **) _Jv_AllocBytes ((num_exceptions + 1)
563 * sizeof (_Jv_Utf8Const *));
565 int out = 0;
566 _Jv_word *pool_data = def->constants.data;
567 for (int i = 0; i < num_exceptions; ++i)
569 int ndx = read2u ();
570 // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
571 if (ndx != 0)
573 check_tag (ndx, JV_CONSTANT_Class);
574 exceptions[out++] = pool_data[ndx].utf8;
577 exceptions[out] = NULL;
578 method->throws = exceptions;
581 else if (is_attribute_name (name, "Code"))
583 int start_off = pos;
584 int max_stack = read2u ();
585 int max_locals = read2u ();
586 int code_length = read4 ();
588 int code_start = pos;
589 skip (code_length);
590 int exception_table_length = read2u ();
592 handleCodeAttribute (method_index,
593 max_stack, max_locals,
594 code_start, code_length,
595 exception_table_length);
598 for (int i = 0; i < exception_table_length; i++)
600 int start_pc = read2u ();
601 int end_pc = read2u ();
602 int handler_pc = read2u ();
603 int catch_type = read2u ();
605 if (start_pc > end_pc
606 || start_pc < 0
607 // END_PC can be equal to CODE_LENGTH.
608 // See JVM Spec 4.7.4.
609 || end_pc > code_length
610 || handler_pc >= code_length)
611 throw_class_format_error ("erroneous exception handler info");
613 if (! (tags[catch_type] == JV_CONSTANT_Class
614 || tags[catch_type] == 0))
616 throw_class_format_error ("erroneous exception handler info");
619 handleExceptionTableEntry (method_index,
621 start_pc,
622 end_pc,
623 handler_pc,
624 catch_type);
628 int attributes_count = read2u ();
630 for (int i = 0; i < attributes_count; i++)
632 read_one_code_attribute (method_index);
635 if ((pos - start_off) != length)
636 throw_class_format_error ("code attribute too short");
639 else
641 /* ignore unknown attributes */
642 skip (length);
646 void _Jv_ClassReader::read_one_code_attribute (int method_index)
648 int name = read2u ();
649 int length = read4 ();
650 if (is_attribute_name (name, "LineNumberTable"))
652 _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
653 (def_interp->interpreted_methods[method_index]);
654 if (method->line_table != NULL)
655 throw_class_format_error ("Method already has LineNumberTable");
657 int table_len = read2u ();
658 _Jv_LineTableEntry* table
659 = (_Jv_LineTableEntry *) JvAllocBytes (table_len
660 * sizeof (_Jv_LineTableEntry));
661 for (int i = 0; i < table_len; i++)
663 table[i].bytecode_pc = read2u ();
664 table[i].line = read2u ();
666 method->line_table_len = table_len;
667 method->line_table = table;
669 else
671 /* ignore unknown code attributes */
672 skip (length);
676 void _Jv_ClassReader::read_one_class_attribute ()
678 int name = read2u ();
679 int length = read4 ();
680 if (is_attribute_name (name, "SourceFile"))
682 int source_index = read2u ();
683 check_tag (source_index, JV_CONSTANT_Utf8);
684 prepare_pool_entry (source_index, JV_CONSTANT_Utf8);
685 def_interp->source_file_name = _Jv_NewStringUtf8Const
686 (def->constants.data[source_index].utf8);
688 else
690 /* Currently, we ignore most class attributes.
691 FIXME: Add inner-classes attributes support. */
692 skip (length);
699 /* this section defines the semantic actions of the parser */
701 void _Jv_ClassReader::handleConstantPool ()
703 /** now, we actually define the class' constant pool */
705 // the pool is scanned explicitly by the collector
706 jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
707 _Jv_word *pool_data
708 = (_Jv_word*) _Jv_AllocBytes (pool_count * sizeof (_Jv_word));
710 def->constants.tags = pool_tags;
711 def->constants.data = pool_data;
712 def->constants.size = pool_count;
714 // Here we make a pass to collect the strings! We do this, because
715 // internally in the GCJ runtime, classes are encoded with .'s not /'s.
716 // Therefore, we first collect the strings, and then translate the rest
717 // of the utf8-entries (thus not representing strings) from /-notation
718 // to .-notation.
719 for (int i = 1; i < pool_count; i++)
721 if (tags[i] == JV_CONSTANT_String)
723 unsigned char* str_data = bytes + offsets [i];
724 int utf_index = get2u (str_data);
725 check_tag (utf_index, JV_CONSTANT_Utf8);
726 unsigned char *utf_data = bytes + offsets[utf_index];
727 int len = get2u (utf_data);
728 pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
729 pool_tags[i] = JV_CONSTANT_String;
731 else
733 pool_tags[i] = JV_CONSTANT_Undefined;
737 // and now, we scan everything else but strings & utf8-entries. This
738 // leaves out those utf8-entries which are not used; which will be left
739 // with a tag of JV_CONSTANT_Undefined in the class definition.
740 for (int index = 1; index < pool_count; index++)
742 switch (tags[index])
744 case JV_CONSTANT_Undefined:
745 case JV_CONSTANT_String:
746 case JV_CONSTANT_Utf8:
747 continue;
749 default:
750 prepare_pool_entry (index, tags[index]);
756 /* this is a recursive procedure, which will prepare pool entries as needed.
757 Which is how we avoid initializing those entries which go unused. */
758 void
759 _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
761 /* these two, pool_data and pool_tags, point into the class
762 structure we are currently defining */
764 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
765 _Jv_word *pool_data = def->constants.data;
767 /* this entry was already prepared */
768 if (pool_tags[index] == this_tag)
769 return;
771 /* this_data points to the constant-pool information for the current
772 constant-pool entry */
774 unsigned char *this_data = bytes + offsets[index];
776 switch (this_tag)
778 case JV_CONSTANT_Utf8:
780 // If we came here, it is because some other tag needs this
781 // utf8-entry for type information! Thus, we translate /'s to .'s in
782 // order to accomondate gcj's internal representation.
784 int len = get2u (this_data);
785 char *buffer = (char*) __builtin_alloca (len);
786 char *s = ((char*) this_data)+2;
788 /* FIXME: avoid using a buffer here */
789 for (int i = 0; i < len; i++)
791 if (s[i] == '/')
792 buffer[i] = '.';
793 else
794 buffer[i] = (char) s[i];
797 pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
798 pool_tags[index] = JV_CONSTANT_Utf8;
800 break;
802 case JV_CONSTANT_Class:
804 int utf_index = get2u (this_data);
805 check_tag (utf_index, JV_CONSTANT_Utf8);
806 prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
808 if (verify)
809 verify_classname (pool_data[utf_index].utf8);
811 pool_data[index].utf8 = pool_data[utf_index].utf8;
812 pool_tags[index] = JV_CONSTANT_Class;
814 break;
816 case JV_CONSTANT_String:
817 // already handled before...
818 break;
820 case JV_CONSTANT_Fieldref:
821 case JV_CONSTANT_Methodref:
822 case JV_CONSTANT_InterfaceMethodref:
824 int class_index = get2u (this_data);
825 int nat_index = get2u (this_data+2);
827 check_tag (class_index, JV_CONSTANT_Class);
828 prepare_pool_entry (class_index, JV_CONSTANT_Class);
830 check_tag (nat_index, JV_CONSTANT_NameAndType);
831 prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
833 // here, verify the signature and identifier name
834 if (verify)
836 _Jv_ushort name_index, type_index;
837 _Jv_loadIndexes (&pool_data[nat_index],
838 name_index, type_index);
840 if (this_tag == JV_CONSTANT_Fieldref)
841 verify_field_signature (pool_data[type_index].utf8);
842 else
843 verify_method_signature (pool_data[type_index].utf8);
845 _Jv_Utf8Const* name = pool_data[name_index].utf8;
847 if (this_tag != JV_CONSTANT_Fieldref
848 && ( _Jv_equalUtf8Consts (name, clinit_name)
849 || _Jv_equalUtf8Consts (name, init_name)))
850 /* ignore */;
851 else
852 verify_identifier (pool_data[name_index].utf8);
855 _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
856 pool_tags[index] = this_tag;
858 break;
860 case JV_CONSTANT_NameAndType:
862 _Jv_ushort name_index = get2u (this_data);
863 _Jv_ushort type_index = get2u (this_data+2);
865 check_tag (name_index, JV_CONSTANT_Utf8);
866 prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
868 check_tag (type_index, JV_CONSTANT_Utf8);
869 prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
871 _Jv_storeIndexes (&pool_data[index], name_index, type_index);
872 pool_tags[index] = JV_CONSTANT_NameAndType;
874 break;
876 case JV_CONSTANT_Float:
878 jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
879 _Jv_storeFloat (&pool_data[index], f);
880 pool_tags[index] = JV_CONSTANT_Float;
882 break;
884 case JV_CONSTANT_Integer:
886 int i = get4 (this_data);
887 _Jv_storeInt (&pool_data[index], i);
888 pool_tags[index] = JV_CONSTANT_Integer;
890 break;
892 case JV_CONSTANT_Double:
894 jdouble d
895 = java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
896 _Jv_storeDouble (&pool_data[index], d);
897 pool_tags[index] = JV_CONSTANT_Double;
899 break;
901 case JV_CONSTANT_Long:
903 jlong i = get8 (this_data);
904 _Jv_storeLong (&pool_data[index], i);
905 pool_tags[index] = JV_CONSTANT_Long;
907 break;
909 default:
910 throw_class_format_error ("erroneous constant pool tag");
915 void
916 _Jv_ClassReader::handleClassBegin (int access_flags, int this_class, int super_class)
918 using namespace java::lang::reflect;
920 unsigned char *pool_tags = (unsigned char*) def->constants.tags;
921 _Jv_word *pool_data = def->constants.data;
923 check_tag (this_class, JV_CONSTANT_Class);
924 _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
926 // was ClassLoader.defineClass called with an expected class name?
927 if (def->name == 0)
929 jclass orig = def->loader->findLoadedClass(loadedName->toString());
931 if (orig == 0)
933 def->name = loadedName;
935 else
937 jstring msg = JvNewStringUTF ("anonymous "
938 "class data denotes "
939 "existing class ");
940 msg = msg->concat (orig->getName ());
942 throw_no_class_def_found_error (msg);
946 // assert that the loaded class has the expected name, 5.3.5
947 else if (! _Jv_equalUtf8Consts (loadedName, def->name))
949 jstring msg = JvNewStringUTF ("loaded class ");
950 msg = msg->concat (def->getName ());
951 msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
952 jstring klass_name = loadedName->toString();
953 msg = msg->concat (klass_name);
955 throw_no_class_def_found_error (msg);
958 def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
959 pool_data[this_class].clazz = def;
960 pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
962 if (super_class == 0)
964 // Note that this is ok if we are defining java.lang.Object.
965 // But there is no way to have this class be interpreted.
966 throw_class_format_error ("no superclass reference");
969 def->state = JV_STATE_PRELOADING;
971 // Register this class with its defining loader as well (despite the
972 // name of the function we're calling), so that super class lookups
973 // work properly. If there is an error, our caller will unregister
974 // this class from the class loader. Also, we don't need to hold a
975 // lock here, as our caller has acquired it.
976 _Jv_RegisterInitiatingLoader (def, def->loader);
978 // Note that we found a name so that unregistration can happen if
979 // needed.
980 *found_name = def->name;
982 if (super_class != 0)
984 // Load the superclass.
985 check_tag (super_class, JV_CONSTANT_Class);
986 _Jv_Utf8Const* super_name = pool_data[super_class].utf8;
988 // Load the superclass using our defining loader.
989 jclass the_super = _Jv_FindClass (super_name, def->loader);
991 // This will establish that we are allowed to be a subclass,
992 // and check for class circularity error.
993 checkExtends (def, the_super);
995 // Note: for an interface we will find Object as the
996 // superclass. We still check it above to ensure class file
997 // validity, but we simply assign `null' to the actual field in
998 // this case.
999 def->superclass = (((access_flags & Modifier::INTERFACE))
1000 ? NULL : the_super);
1001 pool_data[super_class].clazz = the_super;
1002 pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
1005 // Now we've come past the circularity problem, we can
1006 // now say that we're loading.
1008 def->state = JV_STATE_LOADING;
1009 def->notifyAll ();
1012 ///// Implements the checks described in sect. 5.3.5.3
1013 void
1014 _Jv_ClassReader::checkExtends (jclass sub, jclass super)
1016 using namespace java::lang::reflect;
1018 _Jv_Linker::wait_for_state (super, JV_STATE_LOADING);
1020 // Having an interface or a final class as a superclass is no good.
1021 if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
1023 throw_incompatible_class_change_error (sub->getName ());
1026 // If the super class is not public, we need to check some more.
1027 if ((super->accflags & Modifier::PUBLIC) == 0)
1029 // With package scope, the classes must have the same class
1030 // loader.
1031 if ( sub->loader != super->loader
1032 || !_Jv_ClassNameSamePackage (sub->name, super->name))
1034 throw_incompatible_class_change_error (sub->getName ());
1038 for (; super != 0; super = super->getSuperclass ())
1040 if (super == sub)
1041 throw_class_circularity_error (sub->getName ());
1047 void _Jv_ClassReader::handleInterfacesBegin (int count)
1049 def->interfaces = (jclass*) _Jv_AllocBytes (count*sizeof (jclass));
1050 def->interface_count = count;
1053 void _Jv_ClassReader::handleInterface (int if_number, int offset)
1055 _Jv_word * pool_data = def->constants.data;
1056 unsigned char * pool_tags = (unsigned char*) def->constants.tags;
1058 jclass the_interface;
1060 if (pool_tags[offset] == JV_CONSTANT_Class)
1062 _Jv_Utf8Const* name = pool_data[offset].utf8;
1063 the_interface = _Jv_FindClass (name, def->loader);
1065 else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
1067 the_interface = pool_data[offset].clazz;
1069 else
1071 throw_no_class_def_found_error ("erroneous constant pool tag");
1074 // checks the validity of the_interface, and that we are in fact
1075 // allowed to implement that interface.
1076 checkImplements (def, the_interface);
1078 pool_data[offset].clazz = the_interface;
1079 pool_tags[offset] = JV_CONSTANT_ResolvedClass;
1081 def->interfaces[if_number] = the_interface;
1084 void
1085 _Jv_ClassReader::checkImplements (jclass sub, jclass super)
1087 using namespace java::lang::reflect;
1089 // well, it *must* be an interface
1090 if ((super->accflags & Modifier::INTERFACE) == 0)
1092 throw_incompatible_class_change_error (sub->getName ());
1095 // if it has package scope, it must also be defined by the
1096 // same loader.
1097 if ((super->accflags & Modifier::PUBLIC) == 0)
1099 if ( sub->loader != super->loader
1100 || !_Jv_ClassNameSamePackage (sub->name, super->name))
1102 throw_incompatible_class_change_error (sub->getName ());
1106 // FIXME: add interface circularity check here
1107 if (sub == super)
1109 throw_class_circularity_error (sub->getName ());
1113 void _Jv_ClassReader::handleFieldsBegin (int count)
1115 def->fields = (_Jv_Field*)
1116 _Jv_AllocBytes (count * sizeof (_Jv_Field));
1117 def->field_count = count;
1118 def_interp->field_initializers = (_Jv_ushort*)
1119 _Jv_AllocBytes (count * sizeof (_Jv_ushort));
1120 for (int i = 0; i < count; i++)
1121 def_interp->field_initializers[i] = (_Jv_ushort) 0;
1124 void _Jv_ClassReader::handleField (int field_no,
1125 int flags,
1126 int name,
1127 int desc)
1129 using namespace java::lang::reflect;
1131 _Jv_word *pool_data = def->constants.data;
1133 _Jv_Field *field = &def->fields[field_no];
1134 _Jv_Utf8Const *field_name = pool_data[name].utf8;
1136 field->name = field_name;
1138 // Ignore flags we don't know about.
1139 field->flags = flags & Modifier::ALL_FLAGS;
1141 _Jv_Utf8Const* sig = pool_data[desc].utf8;
1143 if (verify)
1145 verify_identifier (field_name);
1147 for (int i = 0; i < field_no; ++i)
1149 if (_Jv_equalUtf8Consts (field_name, def->fields[i].name)
1150 && _Jv_equalUtf8Consts (sig,
1151 // We know the other fields are
1152 // unresolved.
1153 (_Jv_Utf8Const *) def->fields[i].type))
1154 throw_class_format_error ("duplicate field name");
1157 // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1158 if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
1159 +((field->flags & Modifier::PRIVATE) ? 1 : 0)
1160 +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
1161 throw_class_format_error ("erroneous field access flags");
1163 // FIXME: JVM spec S4.5: Verify ACC_FINAL and ACC_VOLATILE are not
1164 // both set. Verify modifiers for interface fields.
1168 if (verify)
1169 verify_field_signature (sig);
1171 // field->type is really a jclass, but while it is still
1172 // unresolved we keep an _Jv_Utf8Const* instead.
1173 field->type = (jclass) sig;
1174 field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
1175 field->u.boffset = 0;
1179 void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
1180 int value)
1182 using namespace java::lang::reflect;
1184 _Jv_Field *field = &def->fields[field_index];
1186 if ((field->flags & (Modifier::STATIC
1187 | Modifier::FINAL
1188 | Modifier::PRIVATE)) == 0)
1190 // Ignore, as per vmspec #4.7.2
1191 return;
1194 // do not allow multiple constant fields!
1195 if (field->flags & _Jv_FIELD_CONSTANT_VALUE)
1196 throw_class_format_error ("field has multiple ConstantValue attributes");
1198 field->flags |= _Jv_FIELD_CONSTANT_VALUE;
1199 def_interp->field_initializers[field_index] = value;
1201 /* type check the initializer */
1203 if (value <= 0 || value >= pool_count)
1204 throw_class_format_error ("erroneous ConstantValue attribute");
1206 /* FIXME: do the rest */
1209 void _Jv_ClassReader::handleFieldsEnd ()
1211 using namespace java::lang::reflect;
1213 // We need to reorganize the fields so that the static ones are first,
1214 // to conform to GCJ class layout.
1216 int low = 0;
1217 int high = def->field_count-1;
1218 _Jv_Field *fields = def->fields;
1219 _Jv_ushort *inits = def_interp->field_initializers;
1221 // this is kind of a raw version of quicksort.
1222 while (low < high)
1224 // go forward on low, while it's a static
1225 while (low < high && (fields[low].flags & Modifier::STATIC) != 0)
1226 low++;
1228 // go backwards on high, while it's a non-static
1229 while (low < high && (fields[high].flags & Modifier::STATIC) == 0)
1230 high--;
1232 if (low==high)
1233 break;
1235 _Jv_Field tmp = fields[low];
1236 _Jv_ushort itmp = inits[low];
1238 fields[low] = fields[high];
1239 inits[low] = inits[high];
1241 fields[high] = tmp;
1242 inits[high] = itmp;
1244 high -= 1;
1245 low += 1;
1248 if ((fields[low].flags & Modifier::STATIC) != 0)
1249 low += 1;
1251 def->static_field_count = low;
1256 void
1257 _Jv_ClassReader::handleMethodsBegin (int count)
1259 def->methods = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method) * count);
1261 def_interp->interpreted_methods
1262 = (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *)
1263 * count);
1265 for (int i = 0; i < count; i++)
1267 def_interp->interpreted_methods[i] = 0;
1268 def->methods[i].index = (_Jv_ushort) -1;
1271 def->method_count = count;
1275 void _Jv_ClassReader::handleMethod
1276 (int mth_index, int accflags, int name, int desc)
1278 using namespace java::lang::reflect;
1280 _Jv_word *pool_data = def->constants.data;
1281 _Jv_Method *method = &def->methods[mth_index];
1283 check_tag (name, JV_CONSTANT_Utf8);
1284 prepare_pool_entry (name, JV_CONSTANT_Utf8);
1285 method->name = pool_data[name].utf8;
1287 check_tag (desc, JV_CONSTANT_Utf8);
1288 prepare_pool_entry (desc, JV_CONSTANT_Utf8);
1289 method->signature = pool_data[desc].utf8;
1291 // ignore unknown flags
1292 method->accflags = accflags & Modifier::ALL_FLAGS;
1294 // Initialize...
1295 method->ncode = 0;
1296 method->throws = NULL;
1298 if (verify)
1300 if (_Jv_equalUtf8Consts (method->name, clinit_name)
1301 || _Jv_equalUtf8Consts (method->name, init_name))
1302 /* ignore */;
1303 else
1304 verify_identifier (method->name);
1306 verify_method_signature (method->signature);
1308 for (int i = 0; i < mth_index; ++i)
1310 if (_Jv_equalUtf8Consts (method->name, def->methods[i].name)
1311 && _Jv_equalUtf8Consts (method->signature,
1312 def->methods[i].signature))
1313 throw_class_format_error ("duplicate method");
1316 // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
1317 if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
1318 +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
1319 +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
1320 throw_class_format_error ("erroneous method access flags");
1322 // FIXME: JVM spec S4.6: if ABSTRACT modifier is set, verify other
1323 // flags are not set. Verify flags for interface methods. Verify
1324 // modifiers for initializers.
1328 void _Jv_ClassReader::handleCodeAttribute
1329 (int method_index, int max_stack, int max_locals,
1330 int code_start, int code_length, int exc_table_length)
1332 int size = _Jv_InterpMethod::size (exc_table_length, code_length);
1333 _Jv_InterpMethod *method =
1334 (_Jv_InterpMethod*) (_Jv_AllocBytes (size));
1336 method->max_stack = max_stack;
1337 method->max_locals = max_locals;
1338 method->code_length = code_length;
1339 method->exc_count = exc_table_length;
1340 method->is_15 = is_15;
1341 method->defining_class = def;
1342 method->self = &def->methods[method_index];
1343 method->prepared = NULL;
1344 method->line_table_len = 0;
1345 method->line_table = NULL;
1348 // grab the byte code!
1349 memcpy ((void*) method->bytecode (),
1350 (void*) (bytes+code_start),
1351 code_length);
1353 def_interp->interpreted_methods[method_index] = method;
1355 if ((method->self->accflags & java::lang::reflect::Modifier::STATIC))
1357 // Precompute the ncode field for a static method. This lets us
1358 // call a static method of an interpreted class from precompiled
1359 // code without first resolving the class (that will happen
1360 // during class initialization instead).
1361 method->self->ncode = method->ncode ();
1365 void _Jv_ClassReader::handleExceptionTableEntry
1366 (int method_index, int exc_index,
1367 int start_pc, int end_pc, int handler_pc, int catch_type)
1369 _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
1370 (def_interp->interpreted_methods[method_index]);
1371 _Jv_InterpException *exc = method->exceptions ();
1373 exc[exc_index].start_pc.i = start_pc;
1374 exc[exc_index].end_pc.i = end_pc;
1375 exc[exc_index].handler_pc.i = handler_pc;
1376 exc[exc_index].handler_type.i = catch_type;
1379 void _Jv_ClassReader::handleMethodsEnd ()
1381 using namespace java::lang::reflect;
1383 for (int i = 0; i < def->method_count; i++)
1385 _Jv_Method *method = &def->methods[i];
1386 if ((method->accflags & Modifier::NATIVE) != 0)
1388 if (def_interp->interpreted_methods[i] != 0)
1389 throw_class_format_error ("code provided for native method");
1390 else
1392 _Jv_JNIMethod *m = (_Jv_JNIMethod *)
1393 _Jv_AllocBytes (sizeof (_Jv_JNIMethod));
1394 m->defining_class = def;
1395 m->self = method;
1396 m->function = NULL;
1397 def_interp->interpreted_methods[i] = m;
1399 if ((method->accflags & Modifier::STATIC))
1401 // Precompute the ncode field for a static method.
1402 // This lets us call a static method of an
1403 // interpreted class from precompiled code without
1404 // first resolving the class (that will happen
1405 // during class initialization instead).
1406 method->ncode = m->ncode ();
1410 else if ((method->accflags & Modifier::ABSTRACT) != 0)
1412 if (def_interp->interpreted_methods[i] != 0)
1413 throw_class_format_error ("code provided for abstract method");
1414 method->ncode = (void *) &_Jv_ThrowAbstractMethodError;
1416 else
1418 if (def_interp->interpreted_methods[i] == 0)
1419 throw_class_format_error ("method with no code");
1424 void _Jv_ClassReader::throw_class_format_error (char *msg)
1426 jstring str;
1427 if (def->name != NULL)
1429 jsize mlen = strlen (msg);
1430 unsigned char* data = (unsigned char*) def->name->chars();
1431 int ulen = def->name->len();
1432 unsigned char* limit = data + ulen;
1433 jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen);
1434 jsize len = nlen + mlen + 3;
1435 str = JvAllocString(len);
1436 jchar *chrs = JvGetStringChars(str);
1437 while (data < limit)
1438 *chrs++ = UTF8_GET(data, limit);
1439 *chrs++ = ' ';
1440 *chrs++ = '(';
1441 for (;;)
1443 char c = *msg++;
1444 if (c == 0)
1445 break;
1446 *chrs++ = c & 0xFFFF;
1448 *chrs++ = ')';
1450 else
1451 str = JvNewStringLatin1 (msg);
1452 ::throw_class_format_error (str);
1455 /** Here we define the exceptions that can be thrown */
1457 static void
1458 throw_no_class_def_found_error (jstring msg)
1460 throw (msg
1461 ? new java::lang::NoClassDefFoundError (msg)
1462 : new java::lang::NoClassDefFoundError);
1465 static void
1466 throw_no_class_def_found_error (char *msg)
1468 throw_no_class_def_found_error (JvNewStringLatin1 (msg));
1471 static void
1472 throw_class_format_error (jstring msg)
1474 throw (msg
1475 ? new java::lang::ClassFormatError (msg)
1476 : new java::lang::ClassFormatError);
1479 static void
1480 throw_internal_error (char *msg)
1482 throw new java::lang::InternalError (JvNewStringLatin1 (msg));
1485 static void
1486 throw_incompatible_class_change_error (jstring msg)
1488 throw new java::lang::IncompatibleClassChangeError (msg);
1491 static void
1492 throw_class_circularity_error (jstring msg)
1494 throw new java::lang::ClassCircularityError (msg);
1497 #endif /* INTERPRETER */
1501 /** This section takes care of verifying integrity of identifiers,
1502 signatures, field ddescriptors, and class names */
1504 #define UTF8_PEEK(PTR, LIMIT) \
1505 ({ unsigned char* xxkeep = (PTR); \
1506 int xxch = UTF8_GET(PTR,LIMIT); \
1507 PTR = xxkeep; xxch; })
1509 /* Verify one element of a type descriptor or signature. */
1510 static unsigned char*
1511 _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
1513 if (ptr >= limit)
1514 return 0;
1516 int ch = UTF8_GET (ptr, limit);
1518 switch (ch)
1520 case 'V':
1521 if (! void_ok)
1522 return 0;
1524 case 'S': case 'B': case 'I': case 'J':
1525 case 'Z': case 'C': case 'F': case 'D':
1526 break;
1528 case 'L':
1530 unsigned char *start = ptr, *end;
1533 if (ptr > limit)
1534 return 0;
1536 end = ptr;
1538 if ((ch = UTF8_GET (ptr, limit)) == -1)
1539 return 0;
1542 while (ch != ';');
1543 if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
1544 return 0;
1546 break;
1548 case '[':
1549 return _Jv_VerifyOne (ptr, limit, false);
1550 break;
1552 default:
1553 return 0;
1556 return ptr;
1559 /* Verification and loading procedures. */
1560 bool
1561 _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
1563 unsigned char* ptr = (unsigned char*) sig->chars();
1564 unsigned char* limit = ptr + sig->len();
1566 ptr = _Jv_VerifyOne (ptr, limit, false);
1568 return ptr == limit;
1571 bool
1572 _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
1574 unsigned char* ptr = (unsigned char*) sig->chars();
1575 unsigned char* limit = ptr + sig->len();
1577 if (ptr == limit || UTF8_GET(ptr,limit) != '(')
1578 return false;
1580 while (ptr && UTF8_PEEK (ptr, limit) != ')')
1581 ptr = _Jv_VerifyOne (ptr, limit, false);
1583 if (! ptr || UTF8_GET (ptr, limit) != ')')
1584 return false;
1586 // get the return type
1587 ptr = _Jv_VerifyOne (ptr, limit, true);
1589 return ptr == limit;
1592 /* We try to avoid calling the Character methods all the time, in
1593 fact, they will only be called for non-standard things. */
1594 static __inline__ int
1595 is_identifier_start (int c)
1597 unsigned int ch = (unsigned)c;
1599 if ((ch - 0x41U) < 29U) /* A ... Z */
1600 return 1;
1601 if ((ch - 0x61U) < 29U) /* a ... z */
1602 return 1;
1603 if (ch == 0x5FU) /* _ */
1604 return 1;
1606 return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
1609 static __inline__ int
1610 is_identifier_part (int c)
1612 unsigned int ch = (unsigned)c;
1614 if ((ch - 0x41U) < 29U) /* A ... Z */
1615 return 1;
1616 if ((ch - 0x61U) < 29U) /* a ... z */
1617 return 1;
1618 if ((ch - 0x30) < 10U) /* 0 .. 9 */
1619 return 1;
1620 if (ch == 0x5FU || ch == 0x24U) /* _ $ */
1621 return 1;
1623 return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
1626 bool
1627 _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
1629 unsigned char *ptr = (unsigned char*) name->chars();
1630 unsigned char *limit = (unsigned char*) name->limit();
1631 int ch;
1633 if ((ch = UTF8_GET (ptr, limit))==-1
1634 || ! is_identifier_start (ch))
1635 return false;
1637 while (ptr != limit)
1639 if ((ch = UTF8_GET (ptr, limit))==-1
1640 || ! is_identifier_part (ch))
1641 return false;
1643 return true;
1646 bool
1647 _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
1649 unsigned char *limit = ptr+length;
1650 int ch;
1652 if ('[' == UTF8_PEEK (ptr, limit))
1654 unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
1655 // _Jv_VerifyOne must leave us looking at the terminating nul
1656 // byte.
1657 if (! end || *end)
1658 return false;
1659 else
1660 return true;
1663 next_level:
1664 for (;;) {
1665 if ((ch = UTF8_GET (ptr, limit))==-1)
1666 return false;
1667 if (! is_identifier_start (ch))
1668 return false;
1669 for (;;) {
1670 if (ptr == limit)
1671 return true;
1672 else if ((ch = UTF8_GET (ptr, limit))==-1)
1673 return false;
1674 else if (ch == '.')
1675 goto next_level;
1676 else if (! is_identifier_part (ch))
1677 return false;
1682 bool
1683 _Jv_VerifyClassName (_Jv_Utf8Const *name)
1685 return _Jv_VerifyClassName ((unsigned char*)name->chars(), name->len());
1688 /* Returns true, if NAME1 and NAME2 represent classes in the same
1689 package. Neither NAME2 nor NAME2 may name an array type. */
1690 bool
1691 _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
1693 unsigned char* ptr1 = (unsigned char*) name1->chars();
1694 unsigned char* limit1 = (unsigned char*) name1->limit();
1696 unsigned char* last1 = ptr1;
1698 // scan name1, and find the last occurrence of '.'
1699 while (ptr1 < limit1) {
1700 int ch1 = UTF8_GET (ptr1, limit1);
1702 if (ch1 == '.')
1703 last1 = ptr1;
1705 else if (ch1 == -1)
1706 return false;
1709 // Now the length of NAME1's package name is LEN.
1710 int len = last1 - (unsigned char*) name1->chars();
1712 // If this is longer than NAME2, then we're off.
1713 if (len > name2->len())
1714 return false;
1716 // Then compare the first len bytes for equality.
1717 if (memcmp ((void*) name1->chars(), (void*) name2->chars(), len) == 0)
1719 // Check that there are no .'s after position LEN in NAME2.
1721 unsigned char* ptr2 = (unsigned char*) name2->chars() + len;
1722 unsigned char* limit2 = (unsigned char*) name2->limit();
1724 while (ptr2 < limit2)
1726 int ch2 = UTF8_GET (ptr2, limit2);
1727 if (ch2 == -1 || ch2 == '.')
1728 return false;
1730 return true;
1732 return false;