ikvm-fork/runtime/attributes.cs

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

2002-12-18 19:00:25 +03:00
/*
2004-03-08 18:18:47 +03:00
Copyright (C) 2002, 2003, 2004 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;
using System.Reflection.Emit;
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
2004-11-23 20:46:39 +03:00
public LineNumberTableAttribute(byte[] table)
2004-10-19 17:43:55 +04:00
{
this.table = table;
}
2005-06-19 14:44:53 +04:00
internal 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;
internal 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
internal void AddMapping(int ilOffset, int linenumber)
2004-11-23 20:46:39 +03:00
{
2005-06-19 14:44:53 +04:00
WritePackedInteger(ilOffset - prevILOffset);
WritePackedInteger(linenumber - prevLineNum);
prevILOffset = ilOffset;
prevLineNum = linenumber;
2004-11-23 20:46:39 +03:00
}
2005-06-19 14:44:53 +04:00
internal 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-06-19 14:44:53 +04:00
int prevILOffset = 0;
int prevLineNum = 0;
2004-11-23 20:46:39 +03:00
int line = -1;
for(int i = 0; i < table.Length;)
{
2005-06-19 14:44:53 +04:00
int currILOffset = ReadPackedInteger(ref i) + prevILOffset;
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-06-19 14:44:53 +04:00
line = ReadPackedInteger(ref i) + prevLineNum;
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
{
}
2004-09-09 15:17:55 +04:00
[AttributeUsage(AttributeTargets.Method)]
public sealed class MirandaMethodAttribute : Attribute
{
}
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,
Transient = 0x0080,
Native = 0x0100,
Interface = 0x0200,
Abstract = 0x0400,
2004-12-22 11:04:10 +03:00
Strictfp = 0x0800,
// 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;
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
}
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
}
}
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;
}
}
2004-05-27 11:12:04 +04:00
}