/*
The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language
governing rights and limitations under the License.
The Original Code is RAIL(Runtime Assembly Instrumentation Library) Alpha Version.
The Initial Developer of the Original Code is University of Coimbra,
Computer Science Department, Dependable Systems Group. Copyright (C) University of Coimbra.
All Rights Reserved.
*/
using System;
using System.Reflection;
using Rail.Transformation;
namespace Rail.Reflect{
/// <summary>
///
/// TODO: A nice way to initialize the field and change its value.
/// Possible kind of values:
/// - not initialized
/// - null
/// - constant
/// - expression
/// - method call (probably only static methods)
/// - new object/array
/// </summary>
public abstract class RField : RMember
{
#region fields
/// <summary>
///
/// </summary>
protected FieldAttributes attributes;
/// <summary>
///
/// </summary>
protected RType fieldType;
/// <summary>
///
/// </summary>
protected Object fieldValue;
/// <summary>
///
/// </summary>
protected MarshalSpec marshalSpec;
#endregion
/// <summary>
/// Verify if the field has marshal information
/// </summary>
public bool HasMarshal
{
get
{
return this.marshalSpec!=null;
}
}
/// <summary>
/// Get the field marshaling information
/// </summary>
public virtual MarshalSpec MarshalSpec
{
get
{
return this.marshalSpec;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
/// Gets the attributes for this field
/// </summary>
public virtual FieldAttributes Attributes
{
get
{
return this.attributes;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
/// Gets or sets the type of this field
/// </summary>
public virtual RType FieldType
{
get
{
return this.fieldType;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
///
/// </summary>
public virtual Object Value
{
set
{
throw new NotSupportedException();
}
get
{
return this.fieldValue;
}
}
}
/// <summary>
///
/// </summary>
public class RFieldRef : RField
{
#region fields
/// <summary>
///
/// </summary>
private RAssemblyDef rad;
#endregion
/// <summary>
///
/// </summary>
/// <param name="fi"></param>
/// <param name="rad"></param>
internal RFieldRef(FieldInfo fi,RAssemblyDef rad)
{
this.rad = rad;
this.attributes = fi.Attributes;
this.declaringType = rad.GetRTypeRef(fi.DeclaringType);
this.fieldType = rad.GetRTypeRef(fi.FieldType);
this.memberType = fi.MemberType;
this.name = fi.Name;
//TODO: Set the field value
//this.fieldValue = fi.FieldType.
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="declaringType"></param>
/// <param name="fieldType"></param>
/// <param name="rad"></param>
internal RFieldRef(string name,RTypeRef declaringType,RType fieldType,RAssemblyDef rad)
{
this.rad = rad;
this.attributes = (FieldAttributes)0;
this.declaringType = declaringType;
this.fieldType = fieldType;
this.memberType = MemberTypes.Field;
this.name = name;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="declaringMethod"></param>
/// <param name="fieldType"></param>
/// <param name="rad"></param>
internal RFieldRef(string name,RMethodBase declaringMethod,RType fieldType,RAssemblyDef rad)
{
this.rad = rad;
this.attributes = (FieldAttributes)0;
this.declaringType = declaringMethod.DeclaringType;
this.fieldType = fieldType;
this.memberType = MemberTypes.Field;
this.name = name;
}
#region Custom Attributes
/// <summary>
///
/// </summary>
/// <param name="inherit"></param>
/// <returns></returns>
public override RCustomAttrib[] GetCustomAttributes(bool inherit) {
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="attributeType"></param>
/// <param name="inherit"></param>
/// <returns></returns>
public override RCustomAttrib[] GetCustomAttributes(RType attributeType, bool inherit){
throw new NotImplementedException();
}
public override bool IsDefined(RType attributeType,bool inherit){
throw new NotImplementedException();
}
#endregion
}
/// <summary>
///
/// </summary>
public sealed class RFieldDef : RField, IVisitable, ICustomAttributeOwnerDef
{
private int offset = -1;
/// <summary>
///
/// </summary>
public int Offset
{
get
{
return this.offset;
}
set
{
if (value<=0)
this.offset = -1;
else this.offset = value;
}
}
/// <summary>
///
/// </summary>
internal RFieldDef()
{
}
/// <summary>
/// Gets or sets the name of this member.
/// </summary>
public override String Name
{
set
{
this.name = value;
}
}
/// <summary>
/// Get or set the field marshaling information
/// </summary>
public override MarshalSpec MarshalSpec
{
set
{
this.marshalSpec = value;
}
}
/// <summary>
/// Define the MarshalSpec
/// </summary>
/// <param name="typ"></param>
/// <returns></returns>
public MarshalSpec DefineMarshalSpec(System.Runtime.InteropServices.UnmanagedType typ)
{
this.marshalSpec = new MarshalSpec(typ);
return this.marshalSpec;
}
/// <summary>
///
/// </summary>
/// <param name="arrayType"></param>
/// <param name="ParamNum"></param>
/// <param name="ElemMult"></param>
/// <param name="NumElem"></param>
/// <returns></returns>
public MarshalSpec DefineMarshalSpecAsArray(System.Runtime.InteropServices.UnmanagedType arrayType,
int ParamNum, int ElemMult, int NumElem)
{
this.marshalSpec = new MarshalSpec(System.Runtime.InteropServices.UnmanagedType.ByValArray);
this.marshalSpec.ElemMult = ElemMult;
this.marshalSpec.NumElem = NumElem;
this.marshalSpec.ParamNum = ParamNum;
return this.marshalSpec;
}
/// <summary>
/// Remove the marshal information
/// </summary>
public void RemoveMarshal()
{
this.marshalSpec = null;
}
/// <summary>
/// Gets or sets the attributes for this field
/// </summary>
public override FieldAttributes Attributes
{
set
{
this.attributes = value;
}
}
/// <summary>
/// Gets or sets the type of this field
/// </summary>
public override RType FieldType
{
set
{
this.fieldType = value;
}
}
/// <summary>
///
/// </summary>
public override Object Value
{
set
{
this.fieldValue = value;
}
get
{
return this.fieldValue;
}
}
#region Custom Attributes
// /// <summary>
// ///
// /// </summary>
// /// <param name="inherit"></param>
// /// <returns></returns>
// public override RCustomAttrib[] GetCustomAttributes(bool inherit) {
// throw new NotImplementedException();
// }
// /// <summary>
// ///
// /// </summary>
// /// <param name="attributeType"></param>
// /// <param name="inherit"></param>
// /// <returns></returns>
// public override RCustomAttrib[] GetCustomAttributes(RType attributeType, bool inherit){
// throw new NotImplementedException();
// }
// /// <summary>
// ///
// /// </summary>
// /// <param name="attributeType"></param>
// /// <param name="inherit"></param>
// /// <returns></returns>
// public override bool IsDefined(RType attributeType,bool inherit){
// throw new NotImplementedException();
// }
//
/// <summary>
///
/// </summary>
/// <param name="attribute"></param>
public void AddCustomAttributtes(RCustomAttrib attribute)
{
InternalAddCustomAttributtes(attribute);
}
/// <summary>
/// TODO: How should the attribute be specified?
/// </summary>
/// <param name="attribute"></param>
public void RemoveCustomAttribute(RCustomAttrib attribute)
{
InternalRemoveCustomAttribute(attribute);
}
#endregion
/// <summary>
/// Visits this field.
/// </summary>
/// <remarks>
/// A field has no subelements.
/// </remarks>
/// <param name="v">The visitor to be used.</param>
public void Accept(Visitor v)
{
// We could call directly v.VisitField(this), but this way is more flexible
// In the future, the Walker classes may be used for more than just choosing
// the traversal order.
v.GetWalker().WalkField(this);
}
}
}
|