/// <summary> /// </summary> /// <param name="type"> /// </param> /// <param name="excludingStructs"> /// </param> /// <returns> /// </returns> public static IEnumerable <MemberLocationInfo> GetFieldsSizes(this IType type, bool excludingStructs = false) { foreach (var field in IlReader.Fields(type).Where(t => !t.IsStatic).ToList()) { var fieldSize = 0; var fieldType = field.FieldType; if (fieldType.IsClass || fieldType.IsArray || fieldType.IsPointer || fieldType.IsDelegate) { // pointer size yield return(new MemberLocationInfo(field, LlvmWriter.PointerSize)); } else if (!excludingStructs && fieldType.IsStructureType()) { yield return(new MemberLocationInfo(field, fieldType.GetTypeSize())); } else if (fieldType.Namespace == "System" && SystemTypeSizes.TryGetValue(fieldType.Name, out fieldSize)) { yield return(new MemberLocationInfo(field, fieldSize)); } else { foreach (var item in fieldType.GetTypeSizes()) { item.SetMainMember(field); yield return(item); } } } }
/// <summary> /// </summary> /// <param name="codeWriter"> /// </param> /// <param name="type"> /// </param> /// <param name="genericContext"> /// </param> public static void WriteTypeDefinition(ICodeWriter codeWriter, IType type, IGenericContext genericContext) { var fields = IlReader.Fields(type); var count = fields.Count(); var number = 1; Debug.Assert(!type.IsGenericType || !type.IsArray); codeWriter.WriteTypeStart(type, genericContext); codeWriter.WriteBeforeFields(count); if (!type.ToNormal().IsEnum) { foreach (var field in fields) { codeWriter.WriteFieldStart(field, number, count); codeWriter.WriteFieldEnd(field, number, count); number++; } } else { codeWriter.WriteFieldType(type.GetEnumUnderlyingType()); } codeWriter.WriteAfterFields(count); }
private static IEnumerable <IType> IterateAllRequiredTypes(IType type, ReadingTypesContext readingTypesContext) { Debug.Assert(type != null, "Type is null"); if (type.BaseType != null) { DicoverGenericSpecializedTypesAndAdditionalTypes(type.BaseType, readingTypesContext); yield return(type.BaseType); } if (type.HasElementType) { DicoverGenericSpecializedTypesAndAdditionalTypes(type.GetElementType(), readingTypesContext); yield return(type.GetElementType()); } var interfaces = type.GetInterfaces(); if (interfaces != null) { foreach (var @interface in interfaces) { DicoverGenericSpecializedTypesAndAdditionalTypes(@interface, readingTypesContext); yield return(@interface); } } if (!type.IsInterface) { var fields = IlReader.Fields( type, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance, _codeWriter); foreach (var field in fields) { ////#if DEBUG //// Debug.WriteLine("Processing field: {0}, Type: {1}", field.FullName, field.FieldType); ////#endif DicoverGenericSpecializedTypesAndAdditionalTypes(field.FieldType, readingTypesContext); if (field.FieldType.IsStructureType() && !field.FieldType.IsPointer) { yield return(field.FieldType); } } var ctors = IlReader.Constructors( type, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance, _codeWriter); foreach (var requiredType in ctors.SelectMany(ctor => GetAllRequiredTypesForMethod(ctor, readingTypesContext))) { yield return(requiredType); } } var methods = IlReader.Methods( type, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance, _codeWriter); foreach (var requiredType in methods.SelectMany(method => GetAllRequiredTypesForMethod(method, readingTypesContext))) { yield return(requiredType); } }
public static IField GetFieldByName(this IType classType, string fieldName, ITypeResolver typeResolver) { var normalType = classType.ToNormal(); var field = IlReader.Fields(normalType, typeResolver).FirstOrDefault(f => f.Name == fieldName); Debug.Assert(field != null, string.Format("Field {0} could not be found", fieldName)); return(field); }
private CollectionMetadata DefineMembers(IType type, CollectionMetadata structureType) { var members = new CollectionMetadata(this.indexedMetadata); foreach (var field in IlReader.Fields(type, this.llvmWriter)) { members.Add(this.DefineMember(field, true, structureType)); } return(members); }
/// <summary> /// </summary> /// <param name="type"> /// </param> /// <returns> /// </returns> public static int CalculateFieldsShift(this IType type, ITypeResolver typeResolver) { var fieldsShift = IlReader.Fields(type, typeResolver).Count(t => !t.IsStatic); if (type.BaseType != null) { fieldsShift += type.BaseType.GetFieldsShift(typeResolver); } fieldsShiftByType[type.FullName] = fieldsShift; return(fieldsShift); }
public static IField GetFieldByFieldNumber(this IType classType, int number, ITypeResolver typeResolver) { var normalType = classType.ToNormal(); var field = IlReader.Fields(normalType, typeResolver).Where(t => !t.IsStatic).Skip(number).FirstOrDefault(); Debug.Assert(field != null); if (field == null) { return(null); } return(field); }
/// <summary> /// </summary> /// <param name="classType"> /// </param> /// <param name="number"> /// </param> /// <returns> /// </returns> public static IType GetFieldTypeByFieldNumber(this IType classType, int number) { var normalType = classType.ToNormal(); if (normalType.IsEnum) { return(normalType.GetEnumUnderlyingType()); } var field = IlReader.Fields(normalType).Where(t => !t.IsStatic).Skip(number).FirstOrDefault(); if (field == null) { return(null); } return(field.FieldType); }
/// <summary> /// </summary> /// <param name="codeWriter"> /// </param> /// <param name="type"> /// </param> /// <param name="genericContext"> /// </param> public static void WriteTypeDefinition(ICodeWriter codeWriter, IType type, IGenericContext genericContext) { codeWriter.WriteTypeStart(type, genericContext); var fields = IlReader.Fields(type, codeWriter); var count = fields.Count(); var number = 1; Debug.Assert(!type.IsGenericTypeDefinition); codeWriter.WriteBeforeFields(count); foreach (var field in fields) { codeWriter.WriteFieldStart(field, number, count); codeWriter.WriteFieldEnd(field, number, count); number++; } codeWriter.WriteAfterFields(count); }
public static void GetEnumToStringMethod( IType enumType, ITypeResolver typeResolver, out object[] code, out IList <object> tokenResolutions, out IList <IType> locals, out IList <IParameter> parameters) { Debug.Assert(enumType.ToNormal().IsEnum, "This is for enum only"); var codeList = new List <object>(); codeList.Add(Code.Ldarg_0); codeList.AppendInt(Code.Ldfld, 1); var sizeOfEnum = enumType.GetEnumUnderlyingType().IntTypeBitSize() / 8; // build cmp/jmp cases var stringValues = 1; foreach (var enumConstValue in IlReader.Fields(enumType, typeResolver).Where(f => f.IsConst)) { codeList.Add(Code.Dup); if (sizeOfEnum == 8) { codeList.AppendULong( Code.Ldc_I8, enumConstValue.ConstantValue.ToString() == "-1" ? 0xffffffffffffffff : Convert.ToUInt64(enumConstValue.ConstantValue)); } else { codeList.AppendUInt(Code.Ldc_I4, unchecked ((uint)Convert.ToInt64(enumConstValue.ConstantValue))); } codeList.Add(Code.Bne_Un_S); codeList.Add(6); codeList.AppendInt(Code.Ldstr, ++stringValues); codeList.Add(Code.Ret); } codeList.Add(Code.Pop); codeList.AppendInt(Code.Ldstr, ++stringValues); codeList.Add(Code.Ret); // End of Code // tokens tokenResolutions = new List <object>(); // data tokenResolutions.Add(enumType.GetFieldByName("Value", typeResolver)); foreach (var enumConstValue in IlReader.Fields(enumType, typeResolver).Where(f => f.IsConst)) { tokenResolutions.Add(enumConstValue.Name); } // default value tokenResolutions.Add(string.Empty); // locals locals = new List <IType>(); // code code = codeList.ToArray(); // parameters parameters = new List <IParameter>(); }