context: /*
    Copyright (C) 2002 Jeroen Frijters
  
    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.
  
    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:
  
    1. The origin of this software must not be misrepresented; you must not
       claim that you wrote the original software. If you use this software
       in a product, an acknowledgment in the product documentation would be
       appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be
       misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.
  
    Jeroen Frijters
    jeroen@frijters.net
    
  */
  using System;
  using System.IO;
  using System.Collections;
  using IKVM.Attributes;
  
  namespace IKVM.StubGen
  {
  	sealed class BigEndianStream
  	{
  		private Stream stream;
  
  		public BigEndianStream(Stream stream)
  		{
  			this.stream = stream;
  		}
  
  		public void WriteUInt16(ushort s)
  		{
  			stream.WriteByte((byte)(s >> 8));
  			stream.WriteByte((byte)s);
  		}
  
  		public void WriteUInt32(uint u)
  		{
  			stream.WriteByte((byte)(u >> 24));
  			stream.WriteByte((byte)(u >> 16));
  			stream.WriteByte((byte)(u >> 8));
  			stream.WriteByte((byte)u);
  		}
  
  		public void WriteInt64(long l)
  		{
  			WriteUInt32((uint)(l >> 32));
  			WriteUInt32((uint)l);
  		}
  
  		public void WriteFloat(float f)
  		{
  			WriteUInt32(BitConverter.ToUInt32(BitConverter.GetBytes(f), 0));
  		}
  
  		public void WriteDouble(double d)
  		{
  			WriteInt64(BitConverter.ToInt64(BitConverter.GetBytes(d), 0));
  		}
  
  		public void WriteByte(byte b)
  		{
  			stream.WriteByte(b);
  		}
  
  		public void WriteUtf8(string str)
  		{
  			byte[] buf = new byte[str.Length * 3 + 1];
  			int j = 0;
  			for (int i = 0, e = str.Length; i < e; i++)
  			{
  				char ch = str[i];
  				if ((ch != 0) && (ch <= 0x7f))
  				{
  					buf[j++] = (byte)ch;
  				}
  				else if (ch <= 0x7FF)
  				{
  					/* 11 bits or less. */
  					byte high_five = (byte)(ch >> 6);
  					byte low_six = (byte)(ch & 0x3F);
  					buf[j++] = (byte)(high_five | 0xC0); /* 110xxxxx */
  					buf[j++] = (byte)(low_six | 0x80);   /* 10xxxxxx */
  				}
  				else
  				{
  					/* possibly full 16 bits. */
  					byte high_four = (byte)(ch >> 12);
  					byte mid_six = (byte)((ch >> 6) & 0x3F);
  					byte low_six = (byte)(ch & 0x3f);
  					buf[j++] = (byte)(high_four | 0xE0); /* 1110xxxx */
  					buf[j++] = (byte)(mid_six | 0x80);   /* 10xxxxxx */
  					buf[j++] = (byte)(low_six | 0x80);   /* 10xxxxxx*/
  				}
  			}
  			WriteUInt16((ushort)j);
  			stream.Write(buf, 0, j);
  		}
  	}
  
  	enum Constant
  	{
  		Utf8 = 1,
  		Integer = 3,
  		Float = 4,
  		Long = 5,
  		Double = 6,
  		Class = 7,
  		String = 8,
  		Fieldref = 9,
  		Methodref = 10,
  		InterfaceMethodref = 11,
  		NameAndType = 12
  	}
  
  	abstract class ConstantPoolItem
  	{
  		public abstract void Write(BigEndianStream bes);
  	}
  
  	sealed class ConstantPoolItemClass : ConstantPoolItem
  	{
  		private ushort name_index;
  
  		public ConstantPoolItemClass(ushort name_index)
  		{
  			this.name_index = name_index;
  		}
  
  		public override int GetHashCode()
  		{
  			return name_index;
  		}
  
  		public override bool Equals(object o)
  		{
  			if (o != null && o.GetType() == typeof(ConstantPoolItemClass))
  			{
  				return ((ConstantPoolItemClass)o).name_index == name_index;
  			}
  			return false;
  		}
  
  		public override void Write(BigEndianStream bes)
  		{
  			bes.WriteByte((byte)Constant.Class);
  			bes.WriteUInt16(name_index);
  		}
  	}
  
  	sealed class ConstantPoolItemMethodref : ConstantPoolItem
  	{
  		private ushort class_index;
  		private ushort name_and_type_index;
  
  		public ConstantPoolItemMethodref(ushort class_index, ushort name_and_type_index)
  		{
  			this.class_index = class_index;
  			this.name_and_type_index = name_and_type_index;
  		}
  
  		public override int GetHashCode()
  		{
  			return class_index | (name_and_type_index << 16);
  		}
  
  		public override bool Equals(object o)
  		{
  			if (o != null && o.GetType() == typeof(ConstantPoolItemMethodref))
  			{
  				ConstantPoolItemMethodref m = (ConstantPoolItemMethodref)o;
  				return m.class_index == class_index && m.name_and_type_index == name_and_type_index;
  			}
  			return false;
  		}
  
  		public override void Write(BigEndianStream bes)
  		{
  			bes.WriteByte((byte)Constant.Methodref);
  			bes.WriteUInt16(class_index);
  			bes.WriteUInt16(name_and_type_index);
  		}
  	}
  
  	sealed class ConstantPoolItemNameAndType : ConstantPoolItem
  	{
  		private ushort name_index;
  		private ushort descriptor_index;
  
  		public ConstantPoolItemNameAndType(ushort name_index, ushort descriptor_index)
  		{
  			this.name_index = name_index;
  			this.descriptor_index = descriptor_index;
  		}
  
  		public override int GetHashCode()
  		{
  			return name_index | (descriptor_index << 16);
  		}
  
  		public override bool Equals(object o)
  		{
  			if (o != null && o.GetType() == typeof(ConstantPoolItemNameAndType))
  			{
  				ConstantPoolItemNameAndType n = (ConstantPoolItemNameAndType)o;
  				return n.name_index == name_index && n.descriptor_index == descriptor_index;
  			}
  			return false;
  		}
  
  		public override void Write(BigEndianStream bes)
  		{
  			bes.WriteByte((byte)Constant.NameAndType);
  			bes.WriteUInt16(name_index);
  			bes.WriteUInt16(descriptor_index);
  		}
  	}
  
  	sealed class ConstantPoolItemUtf8 : ConstantPoolItem
  	{
  		private string str;
  
  		public ConstantPoolItemUtf8(string str)
  		{
  			this.str = str;
  		}
  
  		public override int GetHashCode()
  		{
  			return str.GetHashCode();
  		}
  
  		public override bool Equals(object o)
  		{
  			if (o != null && o.GetType() == typeof(ConstantPoolItemUtf8))
  			{
  				return ((ConstantPoolItemUtf8)o).str == str;
  			}
  			return false;
  		}
  
  		public override void Write(BigEndianStream bes)
  		{
  			bes.WriteByte((byte)Constant.Utf8);
  			bes.WriteUtf8(str);
  		}
  	}
  
  	sealed class ConstantPoolItemInt : ConstantPoolItem
  	{
  		private int v;
  
  		public ConstantPoolItemInt(int v)
  		{
  			this.v = v;
  		}
  
  		public override int GetHashCode()
  		{
  			return v;
  		}
  
  		public override bool Equals(object o)
  		{
  			if (o != null && o.GetType() == typeof(ConstantPoolItemInt))
  			{
  
gt: 				return ((ConstantPoolItemInt)o).v == v;