ikvm-fork/runtime/attributes.cs

707 строки
14 KiB
C#
Исходник Обычный вид История

2002-12-18 19:00:25 +03:00
/*
2006-04-10 13:09:09 +04:00
Copyright (C) 2002, 2003, 2004, 2005, 2006 Jeroen Frijters
2002-12-18 19:00:25 +03:00
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.Reflection;
2004-09-09 15:17:55 +04:00
namespace IKVM.Attributes
2004-03-08 18:18:47 +03:00
{
2004-10-19 17:43:55 +04:00
public sealed class SourceFileAttribute : Attribute
{
private string file;
public SourceFileAttribute(string file)
{
this.file = file;
}
public string SourceFile
{
get
{
return file;
}
}
}
public sealed class LineNumberTableAttribute : Attribute
{
2004-11-23 20:46:39 +03:00
private byte[] table;
2004-10-19 17:43:55 +04:00
2005-08-22 16:42:02 +04:00
public LineNumberTableAttribute(ushort lineno)
{
LineNumberWriter w = new LineNumberWriter(1);
w.AddMapping(0, lineno);
table = w.ToArray();
}
2004-11-23 20:46:39 +03:00
public LineNumberTableAttribute(byte[] table)
2004-10-19 17:43:55 +04:00
{
this.table = table;
}
2006-04-10 13:09:09 +04:00
public class LineNumberWriter
2004-10-19 17:43:55 +04:00
{
2005-06-19 14:44:53 +04:00
private System.IO.MemoryStream stream;
private int prevILOffset;
private int prevLineNum;
2005-08-22 16:42:02 +04:00
private int count;
2005-06-19 14:44:53 +04:00
2006-04-10 13:09:09 +04:00
public LineNumberWriter(int estimatedCount)
2004-11-23 20:46:39 +03:00
{
2005-06-19 14:44:53 +04:00
stream = new System.IO.MemoryStream(estimatedCount * 2);
2004-11-23 20:46:39 +03:00
}
2005-06-19 14:44:53 +04:00
2006-04-10 13:09:09 +04:00
public void AddMapping(int ilOffset, int linenumber)
2004-11-23 20:46:39 +03:00
{
2005-08-22 16:42:02 +04:00
if(count == 0)
{
if(ilOffset == 0 && linenumber != 0)
{
prevLineNum = linenumber;
count++;
WritePackedInteger(linenumber - (64 + 50));
return;
}
else
{
prevLineNum = linenumber & ~3;
WritePackedInteger(((-prevLineNum / 4) - (64 + 50)));
}
}
bool pc_overflow;
bool lineno_overflow;
byte lead;
int deltaPC = ilOffset - prevILOffset;
if(deltaPC >= 0 && deltaPC < 31)
{
lead = (byte)deltaPC;
pc_overflow = false;
}
else
{
lead = (byte)31;
pc_overflow = true;
}
int deltaLineNo = linenumber - prevLineNum;
const int bias = 2;
if(deltaLineNo >= -bias && deltaLineNo < 7 - bias)
{
lead |= (byte)((deltaLineNo + bias) << 5);
lineno_overflow = false;
}
else
{
lead |= (byte)(7 << 5);
lineno_overflow = true;
}
stream.WriteByte(lead);
if(pc_overflow)
{
WritePackedInteger(deltaPC - (64 + 31));
}
if(lineno_overflow)
{
WritePackedInteger(deltaLineNo);
}
2005-06-19 14:44:53 +04:00
prevILOffset = ilOffset;
prevLineNum = linenumber;
2005-08-22 16:42:02 +04:00
count++;
}
2006-04-10 13:09:09 +04:00
public int Count
2005-08-22 16:42:02 +04:00
{
get
{
return count;
}
}
2006-04-10 13:09:09 +04:00
public int LineNo
2005-08-22 16:42:02 +04:00
{
get
{
return prevLineNum;
}
2004-11-23 20:46:39 +03:00
}
2005-06-19 14:44:53 +04:00
2006-04-10 13:09:09 +04:00
public byte[] ToArray()
2004-11-23 20:46:39 +03:00
{
2005-06-19 14:44:53 +04:00
return stream.ToArray();
2004-11-23 20:46:39 +03:00
}
2005-06-19 14:44:53 +04:00
/*
* packed integer format:
* ----------------------
*
* First byte:
* 00 - 7F Single byte integer (-64 - 63)
* 80 - BF Double byte integer (-8192 - 8191)
* C0 - DF Triple byte integer (-2097152 - 2097151)
* E0 - FE Reserved
* FF Five byte integer
*/
private void WritePackedInteger(int val)
2004-11-23 20:46:39 +03:00
{
2005-06-19 14:44:53 +04:00
if(val >= -64 && val < 64)
{
val += 64;
stream.WriteByte((byte)val);
}
else if(val >= -8192 && val < 8192)
{
val += 8192;
stream.WriteByte((byte)(0x80 + (val >> 8)));
stream.WriteByte((byte)val);
}
else if(val >= -2097152 && val < 2097152)
{
val += 2097152;
stream.WriteByte((byte)(0xC0 + (val >> 16)));
stream.WriteByte((byte)(val >> 8));
stream.WriteByte((byte)val);
}
else
{
stream.WriteByte(0xFF);
stream.WriteByte((byte)(val >> 24));
stream.WriteByte((byte)(val >> 16));
stream.WriteByte((byte)(val >> 8));
stream.WriteByte((byte)(val >> 0));
}
2004-11-23 20:46:39 +03:00
}
2004-10-19 17:43:55 +04:00
}
2004-11-23 20:46:39 +03:00
private int ReadPackedInteger(ref int position)
2004-10-19 17:43:55 +04:00
{
2004-11-23 20:46:39 +03:00
byte b = table[position++];
if(b < 128)
2004-10-19 17:43:55 +04:00
{
2005-06-19 14:44:53 +04:00
return b - 64;
2004-11-23 20:46:39 +03:00
}
else if((b & 0xC0) == 0x80)
{
2005-06-19 14:44:53 +04:00
return ((b & 0x7F) << 8) + table[position++] - 8192;
2004-11-23 20:46:39 +03:00
}
else if((b & 0xE0) == 0xC0)
{
int val = ((b & 0x3F) << 16);
val += (table[position++] << 8);
val += table[position++];
2005-06-19 14:44:53 +04:00
return val - 2097152;
2004-11-23 20:46:39 +03:00
}
else if(b == 0xFF)
{
int val = table[position++] << 24;
val += table[position++] << 16;
val += table[position++] << 8;
val += table[position++] << 0;
return val;
2004-10-19 17:43:55 +04:00
}
else
{
2004-11-23 20:46:39 +03:00
throw new InvalidProgramException();
}
}
public int GetLineNumber(int ilOffset)
{
2005-08-22 16:42:02 +04:00
int i = 0;
2005-06-19 14:44:53 +04:00
int prevILOffset = 0;
2005-08-22 16:42:02 +04:00
int prevLineNum = ReadPackedInteger(ref i) + (64 + 50);
int line;
if(prevLineNum > 0)
2004-11-23 20:46:39 +03:00
{
2005-08-22 16:42:02 +04:00
line = prevLineNum;
}
else
{
prevLineNum = 4 * -prevLineNum;
line = -1;
}
while(i < table.Length)
{
byte lead = table[i++];
int deltaPC = lead & 31;
int deltaLineNo = (lead >> 5) - 2;
if(deltaPC == 31)
{
deltaPC = ReadPackedInteger(ref i) + (64 + 31);
}
if(deltaLineNo == 5)
{
deltaLineNo = ReadPackedInteger(ref i);
}
int currILOffset = prevILOffset + deltaPC;
2005-06-19 14:44:53 +04:00
if(currILOffset > ilOffset)
2004-10-19 17:43:55 +04:00
{
2004-11-23 20:46:39 +03:00
return line;
2004-10-19 17:43:55 +04:00
}
2005-08-22 16:42:02 +04:00
line = prevLineNum + deltaLineNo;
2005-06-19 14:44:53 +04:00
prevILOffset = currILOffset;
prevLineNum = line;
2004-10-19 17:43:55 +04:00
}
return line;
}
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Class)]
public sealed class ExceptionIsUnsafeForMappingAttribute : Attribute
2004-03-08 18:18:47 +03:00
{
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Method)]
public sealed class RemappedInterfaceMethodAttribute : Attribute
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
private string name;
private string mappedTo;
public RemappedInterfaceMethodAttribute(string name, string mappedTo)
{
this.name = name;
this.mappedTo = mappedTo;
}
public string Name
{
get
{
return name;
}
}
public string MappedTo
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
get
{
return mappedTo;
}
2004-03-08 18:18:47 +03:00
}
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class RemappedClassAttribute : Attribute
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
private string name;
private Type remappedType;
public RemappedClassAttribute(string name, Type remappedType)
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
this.name = name;
this.remappedType = remappedType;
2004-03-08 18:18:47 +03:00
}
2004-09-09 15:17:55 +04:00
public string Name
{
get
{
return name;
}
}
2004-03-08 18:18:47 +03:00
2004-09-09 15:17:55 +04:00
public Type RemappedType
{
get
{
return remappedType;
}
}
2004-03-08 18:18:47 +03:00
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public sealed class RemappedTypeAttribute : Attribute
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
private Type type;
public RemappedTypeAttribute(Type type)
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
this.type = type;
2004-03-08 18:18:47 +03:00
}
2004-09-09 15:17:55 +04:00
public Type Type
2004-03-08 18:18:47 +03:00
{
2004-09-09 15:17:55 +04:00
get
{
return type;
}
2004-03-08 18:18:47 +03:00
}
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Module)]
public sealed class JavaModuleAttribute : Attribute
2004-03-08 18:18:47 +03:00
{
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Assembly)]
public sealed class NoPackagePrefixAttribute : Attribute
2004-03-08 18:18:47 +03:00
{
}
2004-05-27 11:12:04 +04:00
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Struct)]
public sealed class GhostInterfaceAttribute : Attribute
2004-05-27 11:12:04 +04:00
{
}
2004-09-09 15:17:55 +04:00
// Whenever the VM or compiler generates a helper class/method/field, it should be marked
// with this custom attribute, so that it can be hidden from Java.
[AttributeUsage(AttributeTargets.All)]
public sealed class HideFromJavaAttribute : Attribute
2004-05-27 11:12:04 +04:00
{
}
2005-08-14 19:49:50 +04:00
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
public sealed class HideFromReflectionAttribute : Attribute
2004-09-09 15:17:55 +04:00
{
}
2004-08-17 13:05:21 +04:00
2004-09-09 15:17:55 +04:00
[Flags]
public enum Modifiers : ushort
2004-08-17 13:05:21 +04:00
{
2004-09-09 15:17:55 +04:00
Public = 0x0001,
Private = 0x0002,
Protected = 0x0004,
Static = 0x0008,
Final = 0x0010,
Super = 0x0020,
Synchronized = 0x0020,
Volatile = 0x0040,
2005-10-01 15:16:11 +04:00
Bridge = 0x0040,
2004-09-09 15:17:55 +04:00
Transient = 0x0080,
2005-10-01 15:16:11 +04:00
VarArgs = 0x0080,
2004-09-09 15:17:55 +04:00
Native = 0x0100,
Interface = 0x0200,
Abstract = 0x0400,
2004-12-22 11:04:10 +03:00
Strictfp = 0x0800,
2005-10-01 15:16:11 +04:00
Synthetic = 0x1000,
Annotation = 0x2000,
Enum = 0x4000,
2004-12-22 11:04:10 +03:00
// Masks
AccessMask = Public | Private | Protected
2004-08-17 13:05:21 +04:00
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.All)]
public sealed class ModifiersAttribute : Attribute
2004-08-17 13:05:21 +04:00
{
2004-09-09 15:17:55 +04:00
private Modifiers modifiers;
2006-04-05 12:18:58 +04:00
private bool isInternal;
2004-09-09 15:17:55 +04:00
public ModifiersAttribute(Modifiers modifiers)
2004-08-17 13:05:21 +04:00
{
2004-09-09 15:17:55 +04:00
this.modifiers = modifiers;
2004-08-17 13:05:21 +04:00
}
2006-04-05 12:18:58 +04:00
public ModifiersAttribute(Modifiers modifiers, bool isInternal)
{
this.modifiers = modifiers;
this.isInternal = isInternal;
}
public bool IsInternal
{
get
{
return isInternal;
}
}
2004-09-09 15:17:55 +04:00
public Modifiers Modifiers
2004-08-17 13:05:21 +04:00
{
2004-09-09 15:17:55 +04:00
get
{
return modifiers;
}
2004-08-17 13:05:21 +04:00
}
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Field)]
public sealed class NameSigAttribute : Attribute
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
private string name;
private string sig;
2004-05-27 11:12:04 +04:00
2004-09-09 15:17:55 +04:00
public NameSigAttribute(string name, string sig)
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
this.name = name;
this.sig = sig;
2004-05-27 11:12:04 +04:00
}
2004-09-09 15:17:55 +04:00
public string Name
{
get
{
return name;
}
}
2004-05-27 11:12:04 +04:00
2004-09-09 15:17:55 +04:00
public string Sig
{
get
{
return sig;
}
}
2004-05-27 11:12:04 +04:00
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method)]
public sealed class ThrowsAttribute : Attribute
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
private string[] classes;
// NOTE this is not CLS compliant, so maybe we should have a couple of overloads
public ThrowsAttribute(string[] classes)
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
this.classes = classes;
2004-05-27 11:12:04 +04:00
}
2004-09-09 15:17:55 +04:00
// dotted Java class names (e.g. java.lang.Throwable)
public string[] Classes
{
get
{
return classes;
}
}
2004-05-27 11:12:04 +04:00
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public sealed class ImplementsAttribute : Attribute
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
private string[] interfaces;
// NOTE this is not CLS compliant, so maybe we should have a couple of overloads
public ImplementsAttribute(string[] interfaces)
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
this.interfaces = interfaces;
2004-05-27 11:12:04 +04:00
}
2004-09-09 15:17:55 +04:00
public string[] Interfaces
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
get
{
return interfaces;
}
2004-05-27 11:12:04 +04:00
}
}
2005-11-01 17:01:42 +03:00
// NOTE this attribute is also used by annotation attribute classes,
// to give them a different name in the Java world ($Proxy[Annotation]).
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public sealed class InnerClassAttribute : Attribute
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
private string innerClassName;
private Modifiers modifiers;
2005-07-07 15:24:08 +04:00
public InnerClassAttribute(string innerClassName, Modifiers modifiers)
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
this.innerClassName = innerClassName;
this.modifiers = modifiers;
2004-05-27 11:12:04 +04:00
}
2004-09-09 15:17:55 +04:00
public string InnerClassName
{
get
{
return innerClassName;
}
}
public Modifiers Modifiers
2004-05-27 11:12:04 +04:00
{
2004-09-09 15:17:55 +04:00
get
{
return modifiers;
}
2004-05-27 11:12:04 +04:00
}
}
2004-11-16 14:11:53 +03:00
[AttributeUsage(AttributeTargets.Field)]
public sealed class ConstantValueAttribute : Attribute
{
private object val;
2004-12-21 13:26:51 +03:00
public ConstantValueAttribute(bool val)
{
this.val = val;
}
2005-02-02 18:11:26 +03:00
public ConstantValueAttribute(byte val)
2004-12-21 13:26:51 +03:00
{
this.val = val;
}
public ConstantValueAttribute(short val)
{
this.val = val;
}
public ConstantValueAttribute(char val)
{
this.val = val;
}
2004-11-16 14:11:53 +03:00
public ConstantValueAttribute(int val)
{
this.val = val;
}
public ConstantValueAttribute(long val)
{
this.val = val;
}
public ConstantValueAttribute(float val)
{
this.val = val;
}
public ConstantValueAttribute(double val)
{
this.val = val;
}
public ConstantValueAttribute(string val)
{
this.val = val;
}
public object GetConstantValue()
{
return val;
}
}
2005-10-01 15:16:11 +04:00
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Field)]
public sealed class SignatureAttribute : Attribute
{
private string signature;
public SignatureAttribute(string signature)
{
this.signature = signature;
}
public string Signature
{
get
{
return signature;
}
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public sealed class EnclosingMethodAttribute : Attribute
{
private string className;
private string methodName;
private string methodSig;
public EnclosingMethodAttribute(string className, string methodName, string methodSig)
{
this.className = className;
this.methodName = methodName;
this.methodSig = methodSig;
}
public string ClassName
{
get
{
return className;
}
}
public string MethodName
{
get
{
return methodName;
}
}
public string MethodSignature
{
get
{
return methodSig;
}
}
}
2005-11-01 17:01:42 +03:00
[AttributeUsage(AttributeTargets.Method)]
public sealed class AnnotationDefaultAttribute : Attribute
{
public const byte TAG_ENUM = (byte)'e';
public const byte TAG_CLASS = (byte)'c';
public const byte TAG_ANNOTATION = (byte)'@';
public const byte TAG_ARRAY = (byte)'[';
private object defaultValue;
// element_value encoding:
// primitives:
// boxed values
// string:
// string
// enum:
// new object[] { (byte)'e', "<EnumType>", "<enumvalue>" }
// class:
// new object[] { (byte)'c', "<Type>" }
// annotation:
// new object[] { (byte)'@', "<AnnotationType>", ("name", (element_value))* }
// array:
// new object[] { (byte)'[', (element_value)* }
public AnnotationDefaultAttribute(object defaultValue)
{
this.defaultValue = defaultValue;
}
public object Value
{
get
{
return defaultValue;
}
}
}
[AttributeUsage(AttributeTargets.Interface)]
public sealed class AnnotationAttributeAttribute : Attribute
{
private string attributeType;
public AnnotationAttributeAttribute(string attributeType)
{
this.attributeType = attributeType;
}
public string AttributeType
{
get
{
return attributeType;
}
}
}
2004-05-27 11:12:04 +04:00
}