public Type EmitStruct(SmallType pDefinition) { Type t = null; var ta = TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout; if (pDefinition.Exported) { ta |= TypeAttributes.Public; } var tb = _builder.DefineType(pDefinition.Name, ta, typeof(ValueType)); GenericTypeParameterBuilder[] typeParms = null; if (pDefinition.IsGenericDefinition) { typeParms = tb.DefineGenericParameters(pDefinition.GenericTypeParameters.Select((pSt) => pSt.Name).ToArray()); //eww } if (pDefinition.Exported) { var c = typeof(TypePrefixAttribute).GetConstructor(new Type[] { typeof(string) }); var cb = new CustomAttributeBuilder(c, new object[] { pDefinition.Name }); tb.SetCustomAttribute(cb); } foreach (var f in pDefinition.GetFields()) { var found = false; if (typeParms != null) { foreach (var tp in typeParms) { foreach (var a in f.Type.GenericTypeParameters) { if (tp.Name == a.Name) { Type tt = tp; if (f.Type.IsArray) { tt = tt.MakeArrayType(); } f.Info = tb.DefineField(f.Name, tt, FieldAttributes.Public); found = true; break; } } } } if (!found) { f.Info = tb.DefineField(f.Name, f.Type.ToSystemType(), FieldAttributes.Public); } } if (pDefinition.HasInitializer) { var mb = tb.DefineMethod("Initialize", MethodAttributes.HideBySig | MethodAttributes.Public, null, null); pDefinition.Initializer.SetBuilder(mb); } //t = tb.CreateType(); pDefinition.SetSystemType(tb); return(t); }
public MethodInfo EmitFunction(MethodDefinition pDefinition) { if (pDefinition.ExternMethod != null) { pDefinition.SetExternMethod(pDefinition.ExternMethod); if (pDefinition.ExternMethod.ContainsGenericParameters) { for (int i = 0; i < pDefinition.TypeHints.Count; i++) { var args = pDefinition.ExternMethod.GetGenericArguments(); for (int j = 0; j < args.Length; j++) { if (pDefinition.TypeHints[i].Name == args[j].Name) { pDefinition.TypeHints[i].SetSystemType(args[j]); } } } } return(pDefinition.CallSite); } var ma = MethodAttributes.HideBySig | MethodAttributes.Static; switch (pDefinition.Scope) { case Scope.Private: ma |= MethodAttributes.Private; break; case Scope.Public: ma |= MethodAttributes.Public; break; } CurrentMethod = pDefinition; var mb = _currentType.DefineMethod(pDefinition.Name, ma, null, null); CurrentMethod.SetBuilder(mb); Type[] types = new Type[pDefinition.Parameters.Length]; Type[] typeParms = null; if (pDefinition.TypeHints.Count > 0) { string[] typeNames = new string[pDefinition.TypeHints.Count]; for (int i = 0; i < typeNames.Length; i++) { typeNames[i] = pDefinition.TypeHints[i].Name; } typeParms = mb.DefineGenericParameters(typeNames); } for (int i = 0; i < pDefinition.Parameters.Length; i++) { var st = pDefinition.Parameters[i].Type; Type t = null; if (typeParms != null) { for (int j = 0; j < typeParms.Length; j++) { for (int k = 0; k < st.GenericTypeArgs.Length; k++) { var stt = st.GenericTypeArgs[k]; if (stt.Name == typeParms[j].Name) { t = typeParms[j]; stt.SetSystemType(t); } } } if (st.IsGenericTypeParameter) { t = typeParms[0]; st.SetSystemType(t); } } types[i] = st.ToSystemType(); if (pDefinition.Parameters[i].IsRef) { types[i] = types[i].MakeByRefType(); } } SmallType srt = pDefinition.GetReturnType(); if (typeParms != null) { for (int j = 0; j < typeParms.Length; j++) { if (srt.Name == typeParms[j].Name) { srt.SetSystemType(typeParms[j]); } } } mb.SetParameters(types); mb.SetReturnType(srt.ToSystemType()); if (pDefinition.IsMain) { _assembly.SetEntryPoint(CurrentMethod.CallSite, PEFileKinds.ConsoleApplication); var cb = new CustomAttributeBuilder(typeof(STAThreadAttribute).GetConstructor(new Type[] { }), new object[] { }); mb.SetCustomAttribute(cb); } for (short i = 0; i < pDefinition.Parameters.Length; i++) { CreateParameter(i, pDefinition.Parameters[i].Name, pDefinition.Parameters[i].Type, pDefinition.Parameters[i].IsRef); } return(CurrentMethod.CallSite); }